Skip to content

Commit ec02883

Browse files
fvincenzowilldeacon
authored andcommitted
arm64: mte: Add asymmetric mode support
MTE provides an asymmetric mode for detecting tag exceptions. In particular, when such a mode is present, the CPU triggers a fault on a tag mismatch during a load operation and asynchronously updates a register when a tag mismatch is detected during a store operation. Add support for MTE asymmetric mode. Note: If the CPU does not support MTE asymmetric mode the kernel falls back on synchronous mode which is the default for kasan=on. Cc: Will Deacon <[email protected]> Cc: Catalin Marinas <[email protected]> Cc: Andrey Konovalov <[email protected]> Signed-off-by: Vincenzo Frascino <[email protected]> Reviewed-by: Catalin Marinas <[email protected]> Acked-by: Andrey Konovalov <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Will Deacon <[email protected]>
1 parent d73c162 commit ec02883

File tree

5 files changed

+47
-12
lines changed

5 files changed

+47
-12
lines changed

arch/arm64/include/asm/memory.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ static inline const void *__tag_set(const void *addr, u8 tag)
243243
#ifdef CONFIG_KASAN_HW_TAGS
244244
#define arch_enable_tagging_sync() mte_enable_kernel_sync()
245245
#define arch_enable_tagging_async() mte_enable_kernel_async()
246+
#define arch_enable_tagging_asymm() mte_enable_kernel_asymm()
246247
#define arch_force_async_tag_fault() mte_check_tfsr_exit()
247248
#define arch_get_random_tag() mte_get_random_tag()
248249
#define arch_get_mem_tag(addr) mte_get_mem_tag(addr)

arch/arm64/include/asm/mte-kasan.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ static inline void mte_set_mem_tag_range(void *addr, size_t size, u8 tag,
130130

131131
void mte_enable_kernel_sync(void);
132132
void mte_enable_kernel_async(void);
133+
void mte_enable_kernel_asymm(void);
133134

134135
#else /* CONFIG_ARM64_MTE */
135136

@@ -161,6 +162,10 @@ static inline void mte_enable_kernel_async(void)
161162
{
162163
}
163164

165+
static inline void mte_enable_kernel_asymm(void)
166+
{
167+
}
168+
164169
#endif /* CONFIG_ARM64_MTE */
165170

166171
#endif /* __ASSEMBLY__ */

arch/arm64/include/asm/mte.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ static inline int mte_ptrace_copy_tags(struct task_struct *child,
8888

8989
#ifdef CONFIG_KASAN_HW_TAGS
9090
/* Whether the MTE asynchronous mode is enabled. */
91-
DECLARE_STATIC_KEY_FALSE(mte_async_mode);
91+
DECLARE_STATIC_KEY_FALSE(mte_async_or_asymm_mode);
9292

93-
static inline bool system_uses_mte_async_mode(void)
93+
static inline bool system_uses_mte_async_or_asymm_mode(void)
9494
{
95-
return static_branch_unlikely(&mte_async_mode);
95+
return static_branch_unlikely(&mte_async_or_asymm_mode);
9696
}
9797

9898
void mte_check_tfsr_el1(void);
@@ -121,7 +121,7 @@ static inline void mte_check_tfsr_exit(void)
121121
mte_check_tfsr_el1();
122122
}
123123
#else
124-
static inline bool system_uses_mte_async_mode(void)
124+
static inline bool system_uses_mte_async_or_asymm_mode(void)
125125
{
126126
return false;
127127
}

arch/arm64/include/asm/uaccess.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,13 @@ static inline void __uaccess_enable_tco(void)
196196
*/
197197
static inline void __uaccess_disable_tco_async(void)
198198
{
199-
if (system_uses_mte_async_mode())
199+
if (system_uses_mte_async_or_asymm_mode())
200200
__uaccess_disable_tco();
201201
}
202202

203203
static inline void __uaccess_enable_tco_async(void)
204204
{
205-
if (system_uses_mte_async_mode())
205+
if (system_uses_mte_async_or_asymm_mode())
206206
__uaccess_enable_tco();
207207
}
208208

arch/arm64/kernel/mte.c

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@
2626
static DEFINE_PER_CPU_READ_MOSTLY(u64, mte_tcf_preferred);
2727

2828
#ifdef CONFIG_KASAN_HW_TAGS
29-
/* Whether the MTE asynchronous mode is enabled. */
30-
DEFINE_STATIC_KEY_FALSE(mte_async_mode);
31-
EXPORT_SYMBOL_GPL(mte_async_mode);
29+
/*
30+
* The asynchronous and asymmetric MTE modes have the same behavior for
31+
* store operations. This flag is set when either of these modes is enabled.
32+
*/
33+
DEFINE_STATIC_KEY_FALSE(mte_async_or_asymm_mode);
34+
EXPORT_SYMBOL_GPL(mte_async_or_asymm_mode);
3235
#endif
3336

3437
static void mte_sync_page_tags(struct page *page, pte_t old_pte,
@@ -116,7 +119,7 @@ void mte_enable_kernel_sync(void)
116119
* Make sure we enter this function when no PE has set
117120
* async mode previously.
118121
*/
119-
WARN_ONCE(system_uses_mte_async_mode(),
122+
WARN_ONCE(system_uses_mte_async_or_asymm_mode(),
120123
"MTE async mode enabled system wide!");
121124

122125
__mte_enable_kernel("synchronous", SCTLR_ELx_TCF_SYNC);
@@ -134,8 +137,34 @@ void mte_enable_kernel_async(void)
134137
* mode in between sync and async, this strategy needs
135138
* to be reviewed.
136139
*/
137-
if (!system_uses_mte_async_mode())
138-
static_branch_enable(&mte_async_mode);
140+
if (!system_uses_mte_async_or_asymm_mode())
141+
static_branch_enable(&mte_async_or_asymm_mode);
142+
}
143+
144+
void mte_enable_kernel_asymm(void)
145+
{
146+
if (cpus_have_cap(ARM64_MTE_ASYMM)) {
147+
__mte_enable_kernel("asymmetric", SCTLR_ELx_TCF_ASYMM);
148+
149+
/*
150+
* MTE asymm mode behaves as async mode for store
151+
* operations. The mode is set system wide by the
152+
* first PE that executes this function.
153+
*
154+
* Note: If in future KASAN acquires a runtime switching
155+
* mode in between sync and async, this strategy needs
156+
* to be reviewed.
157+
*/
158+
if (!system_uses_mte_async_or_asymm_mode())
159+
static_branch_enable(&mte_async_or_asymm_mode);
160+
} else {
161+
/*
162+
* If the CPU does not support MTE asymmetric mode the
163+
* kernel falls back on synchronous mode which is the
164+
* default for kasan=on.
165+
*/
166+
mte_enable_kernel_sync();
167+
}
139168
}
140169
#endif
141170

0 commit comments

Comments
 (0)