Commit 433c38f4 authored by Peter Collingbourne's avatar Peter Collingbourne Committed by Catalin Marinas

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/Icc5923c85a8ea284588cc399ae74fd19ec291230Signed-off-by: default avatarPeter Collingbourne <pcc@google.com>
Reviewed-by: default avatarCatalin Marinas <catalin.marinas@arm.com>
Link: https://lore.kernel.org/r/20210727205300.2554659-3-pcc@google.comAcked-by: default avatarWill Deacon <will@kernel.org>
Signed-off-by: default avatarCatalin Marinas <catalin.marinas@arm.com>
parent 638982a0
...@@ -19,6 +19,9 @@ ...@@ -19,6 +19,9 @@
#define MTE_CTRL_GCR_USER_EXCL_SHIFT 0 #define MTE_CTRL_GCR_USER_EXCL_SHIFT 0
#define MTE_CTRL_GCR_USER_EXCL_MASK 0xffff #define MTE_CTRL_GCR_USER_EXCL_MASK 0xffff
#define MTE_CTRL_TCF_SYNC (1UL << 16)
#define MTE_CTRL_TCF_ASYNC (1UL << 17)
#ifndef __ASSEMBLY__ #ifndef __ASSEMBLY__
#include <linux/build_bug.h> #include <linux/build_bug.h>
......
...@@ -193,14 +193,19 @@ void mte_check_tfsr_el1(void) ...@@ -193,14 +193,19 @@ void mte_check_tfsr_el1(void)
} }
#endif #endif
static void set_gcr_el1_excl(u64 excl) static void mte_update_sctlr_user(struct task_struct *task)
{ {
current->thread.mte_ctrl = excl; unsigned long sctlr = task->thread.sctlr_user;
unsigned long pref = MTE_CTRL_TCF_ASYNC;
unsigned long mte_ctrl = task->thread.mte_ctrl;
unsigned long resolved_mte_tcf = (mte_ctrl & pref) ? pref : mte_ctrl;
/* sctlr &= ~SCTLR_EL1_TCF0_MASK;
* SYS_GCR_EL1 will be set to current->thread.gcr_user_excl value if (resolved_mte_tcf & MTE_CTRL_TCF_ASYNC)
* by mte_set_user_gcr() in kernel_exit, sctlr |= SCTLR_EL1_TCF0_ASYNC;
*/ else if (resolved_mte_tcf & MTE_CTRL_TCF_SYNC)
sctlr |= SCTLR_EL1_TCF0_SYNC;
task->thread.sctlr_user = sctlr;
} }
void mte_thread_init_user(void) void mte_thread_init_user(void)
...@@ -212,15 +217,16 @@ void mte_thread_init_user(void) ...@@ -212,15 +217,16 @@ void mte_thread_init_user(void)
dsb(ish); dsb(ish);
write_sysreg_s(0, SYS_TFSRE0_EL1); write_sysreg_s(0, SYS_TFSRE0_EL1);
clear_thread_flag(TIF_MTE_ASYNC_FAULT); clear_thread_flag(TIF_MTE_ASYNC_FAULT);
/* disable tag checking */ /* disable tag checking and reset tag generation mask */
set_task_sctlr_el1((current->thread.sctlr_user & ~SCTLR_EL1_TCF0_MASK) | current->thread.mte_ctrl = MTE_CTRL_GCR_USER_EXCL_MASK;
SCTLR_EL1_TCF0_NONE); mte_update_sctlr_user(current);
/* reset tag generation mask */ set_task_sctlr_el1(current->thread.sctlr_user);
set_gcr_el1_excl(SYS_GCR_EL1_EXCL_MASK);
} }
void mte_thread_switch(struct task_struct *next) void mte_thread_switch(struct task_struct *next)
{ {
mte_update_sctlr_user(next);
/* /*
* Check if an async tag exception occurred at EL1. * Check if an async tag exception occurred at EL1.
* *
...@@ -259,33 +265,21 @@ void mte_suspend_exit(void) ...@@ -259,33 +265,21 @@ void mte_suspend_exit(void)
long set_mte_ctrl(struct task_struct *task, unsigned long arg) long set_mte_ctrl(struct task_struct *task, unsigned long arg)
{ {
u64 sctlr = task->thread.sctlr_user & ~SCTLR_EL1_TCF0_MASK;
u64 mte_ctrl = (~((arg & PR_MTE_TAG_MASK) >> PR_MTE_TAG_SHIFT) & u64 mte_ctrl = (~((arg & PR_MTE_TAG_MASK) >> PR_MTE_TAG_SHIFT) &
SYS_GCR_EL1_EXCL_MASK) << MTE_CTRL_GCR_USER_EXCL_SHIFT; SYS_GCR_EL1_EXCL_MASK) << MTE_CTRL_GCR_USER_EXCL_SHIFT;
if (!system_supports_mte()) if (!system_supports_mte())
return 0; return 0;
switch (arg & PR_MTE_TCF_MASK) { if (arg & PR_MTE_TCF_ASYNC)
case PR_MTE_TCF_NONE: mte_ctrl |= MTE_CTRL_TCF_ASYNC;
sctlr |= SCTLR_EL1_TCF0_NONE; if (arg & PR_MTE_TCF_SYNC)
break; mte_ctrl |= MTE_CTRL_TCF_SYNC;
case PR_MTE_TCF_SYNC:
sctlr |= SCTLR_EL1_TCF0_SYNC;
break;
case PR_MTE_TCF_ASYNC:
sctlr |= SCTLR_EL1_TCF0_ASYNC;
break;
default:
return -EINVAL;
}
if (task != current) {
task->thread.sctlr_user = sctlr;
task->thread.mte_ctrl = mte_ctrl; task->thread.mte_ctrl = mte_ctrl;
} else { if (task == current) {
set_task_sctlr_el1(sctlr); mte_update_sctlr_user(task);
set_gcr_el1_excl(mte_ctrl); set_task_sctlr_el1(task->thread.sctlr_user);
} }
return 0; return 0;
...@@ -302,18 +296,10 @@ long get_mte_ctrl(struct task_struct *task) ...@@ -302,18 +296,10 @@ long get_mte_ctrl(struct task_struct *task)
return 0; return 0;
ret = incl << PR_MTE_TAG_SHIFT; ret = incl << PR_MTE_TAG_SHIFT;
if (mte_ctrl & MTE_CTRL_TCF_ASYNC)
switch (task->thread.sctlr_user & SCTLR_EL1_TCF0_MASK) {
case SCTLR_EL1_TCF0_NONE:
ret |= PR_MTE_TCF_NONE;
break;
case SCTLR_EL1_TCF0_SYNC:
ret |= PR_MTE_TCF_SYNC;
break;
case SCTLR_EL1_TCF0_ASYNC:
ret |= PR_MTE_TCF_ASYNC; ret |= PR_MTE_TCF_ASYNC;
break; if (mte_ctrl & MTE_CTRL_TCF_SYNC)
} ret |= PR_MTE_TCF_SYNC;
return ret; return ret;
} }
......
...@@ -234,14 +234,15 @@ struct prctl_mm_map { ...@@ -234,14 +234,15 @@ struct prctl_mm_map {
#define PR_GET_TAGGED_ADDR_CTRL 56 #define PR_GET_TAGGED_ADDR_CTRL 56
# define PR_TAGGED_ADDR_ENABLE (1UL << 0) # define PR_TAGGED_ADDR_ENABLE (1UL << 0)
/* MTE tag check fault modes */ /* MTE tag check fault modes */
# define PR_MTE_TCF_SHIFT 1 # define PR_MTE_TCF_NONE 0
# define PR_MTE_TCF_NONE (0UL << PR_MTE_TCF_SHIFT) # define PR_MTE_TCF_SYNC (1UL << 1)
# define PR_MTE_TCF_SYNC (1UL << PR_MTE_TCF_SHIFT) # define PR_MTE_TCF_ASYNC (1UL << 2)
# define PR_MTE_TCF_ASYNC (2UL << PR_MTE_TCF_SHIFT) # define PR_MTE_TCF_MASK (PR_MTE_TCF_SYNC | PR_MTE_TCF_ASYNC)
# define PR_MTE_TCF_MASK (3UL << PR_MTE_TCF_SHIFT)
/* MTE tag inclusion mask */ /* MTE tag inclusion mask */
# define PR_MTE_TAG_SHIFT 3 # define PR_MTE_TAG_SHIFT 3
# define PR_MTE_TAG_MASK (0xffffUL << PR_MTE_TAG_SHIFT) # define PR_MTE_TAG_MASK (0xffffUL << PR_MTE_TAG_SHIFT)
/* Unused; kept only for source compatibility */
# define PR_MTE_TCF_SHIFT 1
/* Control reclaim behavior when allocating memory */ /* Control reclaim behavior when allocating memory */
#define PR_SET_IO_FLUSHER 57 #define PR_SET_IO_FLUSHER 57
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment