Skip to content

Commit 433c38f

Browse files
pccctmarinas
authored andcommitted
arm64: mte: change ASYNC and SYNC TCF settings into bitfields
Allow the user program to specify both ASYNC and SYNC TCF modes by repurposing the existing constants as bitfields. This will allow the kernel to select one of the modes on behalf of the user program. With this patch the kernel will always select async mode, but a subsequent patch will make this configurable. Link: https://linux-review.googlesource.com/id/Icc5923c85a8ea284588cc399ae74fd19ec291230 Signed-off-by: Peter Collingbourne <[email protected]> Reviewed-by: Catalin Marinas <[email protected]> Link: https://lore.kernel.org/r/[email protected] Acked-by: Will Deacon <[email protected]> Signed-off-by: Catalin Marinas <[email protected]>
1 parent 638982a commit 433c38f

File tree

3 files changed

+37
-47
lines changed

3 files changed

+37
-47
lines changed

arch/arm64/include/asm/processor.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
#define MTE_CTRL_GCR_USER_EXCL_SHIFT 0
2020
#define MTE_CTRL_GCR_USER_EXCL_MASK 0xffff
2121

22+
#define MTE_CTRL_TCF_SYNC (1UL << 16)
23+
#define MTE_CTRL_TCF_ASYNC (1UL << 17)
24+
2225
#ifndef __ASSEMBLY__
2326

2427
#include <linux/build_bug.h>

arch/arm64/kernel/mte.c

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -193,14 +193,19 @@ void mte_check_tfsr_el1(void)
193193
}
194194
#endif
195195

196-
static void set_gcr_el1_excl(u64 excl)
196+
static void mte_update_sctlr_user(struct task_struct *task)
197197
{
198-
current->thread.mte_ctrl = excl;
198+
unsigned long sctlr = task->thread.sctlr_user;
199+
unsigned long pref = MTE_CTRL_TCF_ASYNC;
200+
unsigned long mte_ctrl = task->thread.mte_ctrl;
201+
unsigned long resolved_mte_tcf = (mte_ctrl & pref) ? pref : mte_ctrl;
199202

200-
/*
201-
* SYS_GCR_EL1 will be set to current->thread.gcr_user_excl value
202-
* by mte_set_user_gcr() in kernel_exit,
203-
*/
203+
sctlr &= ~SCTLR_EL1_TCF0_MASK;
204+
if (resolved_mte_tcf & MTE_CTRL_TCF_ASYNC)
205+
sctlr |= SCTLR_EL1_TCF0_ASYNC;
206+
else if (resolved_mte_tcf & MTE_CTRL_TCF_SYNC)
207+
sctlr |= SCTLR_EL1_TCF0_SYNC;
208+
task->thread.sctlr_user = sctlr;
204209
}
205210

206211
void mte_thread_init_user(void)
@@ -212,15 +217,16 @@ void mte_thread_init_user(void)
212217
dsb(ish);
213218
write_sysreg_s(0, SYS_TFSRE0_EL1);
214219
clear_thread_flag(TIF_MTE_ASYNC_FAULT);
215-
/* disable tag checking */
216-
set_task_sctlr_el1((current->thread.sctlr_user & ~SCTLR_EL1_TCF0_MASK) |
217-
SCTLR_EL1_TCF0_NONE);
218-
/* reset tag generation mask */
219-
set_gcr_el1_excl(SYS_GCR_EL1_EXCL_MASK);
220+
/* disable tag checking and reset tag generation mask */
221+
current->thread.mte_ctrl = MTE_CTRL_GCR_USER_EXCL_MASK;
222+
mte_update_sctlr_user(current);
223+
set_task_sctlr_el1(current->thread.sctlr_user);
220224
}
221225

222226
void mte_thread_switch(struct task_struct *next)
223227
{
228+
mte_update_sctlr_user(next);
229+
224230
/*
225231
* Check if an async tag exception occurred at EL1.
226232
*
@@ -259,33 +265,21 @@ void mte_suspend_exit(void)
259265

260266
long set_mte_ctrl(struct task_struct *task, unsigned long arg)
261267
{
262-
u64 sctlr = task->thread.sctlr_user & ~SCTLR_EL1_TCF0_MASK;
263268
u64 mte_ctrl = (~((arg & PR_MTE_TAG_MASK) >> PR_MTE_TAG_SHIFT) &
264269
SYS_GCR_EL1_EXCL_MASK) << MTE_CTRL_GCR_USER_EXCL_SHIFT;
265270

266271
if (!system_supports_mte())
267272
return 0;
268273

269-
switch (arg & PR_MTE_TCF_MASK) {
270-
case PR_MTE_TCF_NONE:
271-
sctlr |= SCTLR_EL1_TCF0_NONE;
272-
break;
273-
case PR_MTE_TCF_SYNC:
274-
sctlr |= SCTLR_EL1_TCF0_SYNC;
275-
break;
276-
case PR_MTE_TCF_ASYNC:
277-
sctlr |= SCTLR_EL1_TCF0_ASYNC;
278-
break;
279-
default:
280-
return -EINVAL;
281-
}
274+
if (arg & PR_MTE_TCF_ASYNC)
275+
mte_ctrl |= MTE_CTRL_TCF_ASYNC;
276+
if (arg & PR_MTE_TCF_SYNC)
277+
mte_ctrl |= MTE_CTRL_TCF_SYNC;
282278

283-
if (task != current) {
284-
task->thread.sctlr_user = sctlr;
285-
task->thread.mte_ctrl = mte_ctrl;
286-
} else {
287-
set_task_sctlr_el1(sctlr);
288-
set_gcr_el1_excl(mte_ctrl);
279+
task->thread.mte_ctrl = mte_ctrl;
280+
if (task == current) {
281+
mte_update_sctlr_user(task);
282+
set_task_sctlr_el1(task->thread.sctlr_user);
289283
}
290284

291285
return 0;
@@ -302,18 +296,10 @@ long get_mte_ctrl(struct task_struct *task)
302296
return 0;
303297

304298
ret = incl << PR_MTE_TAG_SHIFT;
305-
306-
switch (task->thread.sctlr_user & SCTLR_EL1_TCF0_MASK) {
307-
case SCTLR_EL1_TCF0_NONE:
308-
ret |= PR_MTE_TCF_NONE;
309-
break;
310-
case SCTLR_EL1_TCF0_SYNC:
311-
ret |= PR_MTE_TCF_SYNC;
312-
break;
313-
case SCTLR_EL1_TCF0_ASYNC:
299+
if (mte_ctrl & MTE_CTRL_TCF_ASYNC)
314300
ret |= PR_MTE_TCF_ASYNC;
315-
break;
316-
}
301+
if (mte_ctrl & MTE_CTRL_TCF_SYNC)
302+
ret |= PR_MTE_TCF_SYNC;
317303

318304
return ret;
319305
}

include/uapi/linux/prctl.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,14 +234,15 @@ struct prctl_mm_map {
234234
#define PR_GET_TAGGED_ADDR_CTRL 56
235235
# define PR_TAGGED_ADDR_ENABLE (1UL << 0)
236236
/* MTE tag check fault modes */
237-
# define PR_MTE_TCF_SHIFT 1
238-
# define PR_MTE_TCF_NONE (0UL << PR_MTE_TCF_SHIFT)
239-
# define PR_MTE_TCF_SYNC (1UL << PR_MTE_TCF_SHIFT)
240-
# define PR_MTE_TCF_ASYNC (2UL << PR_MTE_TCF_SHIFT)
241-
# define PR_MTE_TCF_MASK (3UL << PR_MTE_TCF_SHIFT)
237+
# define PR_MTE_TCF_NONE 0
238+
# define PR_MTE_TCF_SYNC (1UL << 1)
239+
# define PR_MTE_TCF_ASYNC (1UL << 2)
240+
# define PR_MTE_TCF_MASK (PR_MTE_TCF_SYNC | PR_MTE_TCF_ASYNC)
242241
/* MTE tag inclusion mask */
243242
# define PR_MTE_TAG_SHIFT 3
244243
# define PR_MTE_TAG_MASK (0xffffUL << PR_MTE_TAG_SHIFT)
244+
/* Unused; kept only for source compatibility */
245+
# define PR_MTE_TCF_SHIFT 1
245246

246247
/* Control reclaim behavior when allocating memory */
247248
#define PR_SET_IO_FLUSHER 57

0 commit comments

Comments
 (0)