Skip to content

Commit 8286824

Browse files
mrutland-armctmarinas
authored andcommitted
arm64: kasan: mte: use a constant kernel GCR_EL1 value
When KASAN_HW_TAGS is selected, KASAN is enabled at boot time, and the hardware supports MTE, we'll initialize `kernel_gcr_excl` with a value dependent on KASAN_TAG_MAX. While the resulting value is a constant which depends on KASAN_TAG_MAX, we have to perform some runtime work to generate the value, and have to read the value from memory during the exception entry path. It would be better if we could generate this as a constant at compile-time, and use it as such directly. Early in boot within __cpu_setup(), we initialize GCR_EL1 to a safe value, and later override this with the value required by KASAN. If CONFIG_KASAN_HW_TAGS is not selected, or if KASAN is disabeld at boot time, the kernel will not use IRG instructions, and so the initial value of GCR_EL1 is does not matter to the kernel. Thus, we can instead have __cpu_setup() initialize GCR_EL1 to a value consistent with KASAN_TAG_MAX, and avoid the need to re-initialize it during hotplug and resume form suspend. This patch makes arem64 use a compile-time constant KERNEL_GCR_EL1 value, which is compatible with KASAN_HW_TAGS when this is selected. This removes the need to re-initialize GCR_EL1 dynamically, and acts as an optimization to the entry assembly, which no longer needs to load this value from memory. The redundant initialization hooks are removed. In order to do this, KASAN_TAG_MAX needs to be visible outside of the core KASAN code. To do this, I've moved the KASAN_TAG_* values into <linux/kasan-tags.h>. There should be no functional change as a result of this patch. Signed-off-by: Mark Rutland <[email protected]> Cc: Alexander Potapenko <[email protected]> Cc: Andrey Konovalov <[email protected]> Cc: Andrey Ryabinin <[email protected]> Cc: Dmitry Vyukov <[email protected]> Cc: Peter Collingbourne <[email protected]> Cc: Vincenzo Frascino <[email protected]> Cc: Will Deacon <[email protected]> Reviewed-by: Catalin Marinas <[email protected]> Reviewed-by: Andrey Konovalov <[email protected]> Tested-by: Andrey Konovalov <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Catalin Marinas <[email protected]>
1 parent d914b80 commit 8286824

File tree

11 files changed

+35
-65
lines changed

11 files changed

+35
-65
lines changed

arch/arm64/include/asm/memory.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,6 @@ static inline const void *__tag_set(const void *addr, u8 tag)
245245
#define arch_enable_tagging_async() mte_enable_kernel_async()
246246
#define arch_set_tagging_report_once(state) mte_set_report_once(state)
247247
#define arch_force_async_tag_fault() mte_check_tfsr_exit()
248-
#define arch_init_tags(max_tag) mte_init_tags(max_tag)
249248
#define arch_get_random_tag() mte_get_random_tag()
250249
#define arch_get_mem_tag(addr) mte_get_mem_tag(addr)
251250
#define arch_set_mem_tag_range(addr, size, tag, init) \

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ 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_init_tags(u64 max_tag);
134133

135134
void mte_set_report_once(bool state);
136135
bool mte_report_once(void);
@@ -165,10 +164,6 @@ static inline void mte_enable_kernel_async(void)
165164
{
166165
}
167166

168-
static inline void mte_init_tags(u64 max_tag)
169-
{
170-
}
171-
172167
static inline void mte_set_report_once(bool state)
173168
{
174169
}

arch/arm64/include/asm/mte.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
#include <asm/pgtable-types.h>
1818

19-
extern u64 gcr_kernel_excl;
20-
2119
void mte_clear_page_tags(void *addr);
2220
unsigned long mte_copy_tags_from_user(void *to, const void __user *from,
2321
unsigned long n);
@@ -43,7 +41,6 @@ void mte_copy_page_tags(void *kto, const void *kfrom);
4341
void mte_thread_init_user(void);
4442
void mte_thread_switch(struct task_struct *next);
4543
void mte_suspend_enter(void);
46-
void mte_suspend_exit(void);
4744
long set_mte_ctrl(struct task_struct *task, unsigned long arg);
4845
long get_mte_ctrl(struct task_struct *task);
4946
int mte_ptrace_copy_tags(struct task_struct *child, long request,
@@ -72,9 +69,6 @@ static inline void mte_thread_switch(struct task_struct *next)
7269
static inline void mte_suspend_enter(void)
7370
{
7471
}
75-
static inline void mte_suspend_exit(void)
76-
{
77-
}
7872
static inline long set_mte_ctrl(struct task_struct *task, unsigned long arg)
7973
{
8074
return 0;

arch/arm64/include/asm/sysreg.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <linux/bits.h>
1313
#include <linux/stringify.h>
14+
#include <linux/kasan-tags.h>
1415

1516
/*
1617
* ARMv8 ARM reserves the following encoding for system registers:
@@ -1067,6 +1068,21 @@
10671068
#define SYS_GCR_EL1_RRND (BIT(16))
10681069
#define SYS_GCR_EL1_EXCL_MASK 0xffffUL
10691070

1071+
#ifdef CONFIG_KASAN_HW_TAGS
1072+
/*
1073+
* KASAN always uses a whole byte for its tags. With CONFIG_KASAN_HW_TAGS it
1074+
* only uses tags in the range 0xF0-0xFF, which we map to MTE tags 0x0-0xF.
1075+
*/
1076+
#define __MTE_TAG_MIN (KASAN_TAG_MIN & 0xf)
1077+
#define __MTE_TAG_MAX (KASAN_TAG_MAX & 0xf)
1078+
#define __MTE_TAG_INCL GENMASK(__MTE_TAG_MAX, __MTE_TAG_MIN)
1079+
#define KERNEL_GCR_EL1_EXCL (SYS_GCR_EL1_EXCL_MASK & ~__MTE_TAG_INCL)
1080+
#else
1081+
#define KERNEL_GCR_EL1_EXCL SYS_GCR_EL1_EXCL_MASK
1082+
#endif
1083+
1084+
#define KERNEL_GCR_EL1 (SYS_GCR_EL1_RRND | KERNEL_GCR_EL1_EXCL)
1085+
10701086
/* RGSR_EL1 Definitions */
10711087
#define SYS_RGSR_EL1_TAG_MASK 0xfUL
10721088
#define SYS_RGSR_EL1_SEED_SHIFT 8

arch/arm64/kernel/entry.S

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,8 @@ alternative_else_nop_endif
181181
alternative_if_not ARM64_MTE
182182
b 1f
183183
alternative_else_nop_endif
184-
ldr_l \tmp, gcr_kernel_excl
185-
186-
mte_set_gcr \tmp, \tmp2
184+
mov \tmp, KERNEL_GCR_EL1
185+
msr_s SYS_GCR_EL1, \tmp
187186
1:
188187
#endif
189188
.endm

arch/arm64/kernel/mte.c

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
#include <asm/ptrace.h>
2424
#include <asm/sysreg.h>
2525

26-
u64 gcr_kernel_excl __ro_after_init;
27-
2826
static bool report_fault_once = true;
2927

3028
static DEFINE_PER_CPU_READ_MOSTLY(u64, mte_tcf_preferred);
@@ -104,26 +102,6 @@ int memcmp_pages(struct page *page1, struct page *page2)
104102
return ret;
105103
}
106104

107-
void mte_init_tags(u64 max_tag)
108-
{
109-
static bool gcr_kernel_excl_initialized;
110-
111-
if (!gcr_kernel_excl_initialized) {
112-
/*
113-
* The format of the tags in KASAN is 0xFF and in MTE is 0xF.
114-
* This conversion extracts an MTE tag from a KASAN tag.
115-
*/
116-
u64 incl = GENMASK(FIELD_GET(MTE_TAG_MASK >> MTE_TAG_SHIFT,
117-
max_tag), 0);
118-
119-
gcr_kernel_excl = ~incl & SYS_GCR_EL1_EXCL_MASK;
120-
gcr_kernel_excl_initialized = true;
121-
}
122-
123-
/* Enable the kernel exclude mask for random tags generation. */
124-
write_sysreg_s(SYS_GCR_EL1_RRND | gcr_kernel_excl, SYS_GCR_EL1);
125-
}
126-
127105
static inline void __mte_enable_kernel(const char *mode, unsigned long tcf)
128106
{
129107
/* Enable MTE Sync Mode for EL1. */
@@ -262,15 +240,6 @@ void mte_suspend_enter(void)
262240
mte_check_tfsr_el1();
263241
}
264242

265-
void mte_suspend_exit(void)
266-
{
267-
if (!system_supports_mte())
268-
return;
269-
270-
sysreg_clear_set_s(SYS_GCR_EL1, SYS_GCR_EL1_EXCL_MASK, gcr_kernel_excl);
271-
isb();
272-
}
273-
274243
long set_mte_ctrl(struct task_struct *task, unsigned long arg)
275244
{
276245
u64 mte_ctrl = (~((arg & PR_MTE_TAG_MASK) >> PR_MTE_TAG_SHIFT) &

arch/arm64/kernel/suspend.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ void notrace __cpu_suspend_exit(void)
7676
spectre_v4_enable_mitigation(NULL);
7777

7878
/* Restore additional feature-specific configuration */
79-
mte_suspend_exit();
8079
ptrauth_suspend_exit();
8180
}
8281

arch/arm64/mm/proc.S

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,7 @@ SYM_FUNC_START(__cpu_setup)
437437
mov x10, #MAIR_ATTR_NORMAL_TAGGED
438438
bfi mair, x10, #(8 * MT_NORMAL_TAGGED), #8
439439

440-
/* initialize GCR_EL1: all non-zero tags excluded by default */
441-
mov x10, #(SYS_GCR_EL1_RRND | SYS_GCR_EL1_EXCL_MASK)
440+
mov x10, #KERNEL_GCR_EL1
442441
msr_s SYS_GCR_EL1, x10
443442

444443
/*

include/linux/kasan-tags.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef _LINUX_KASAN_TAGS_H
3+
#define _LINUX_KASAN_TAGS_H
4+
5+
#define KASAN_TAG_KERNEL 0xFF /* native kernel pointers tag */
6+
#define KASAN_TAG_INVALID 0xFE /* inaccessible memory tag */
7+
#define KASAN_TAG_MAX 0xFD /* maximum value for random tags */
8+
9+
#ifdef CONFIG_KASAN_HW_TAGS
10+
#define KASAN_TAG_MIN 0xF0 /* minimum value for random tags */
11+
#else
12+
#define KASAN_TAG_MIN 0x00 /* minimum value for random tags */
13+
#endif
14+
15+
#endif /* LINUX_KASAN_TAGS_H */

mm/kasan/hw_tags.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,6 @@ void kasan_init_hw_tags_cpu(void)
142142
if (kasan_arg == KASAN_ARG_OFF)
143143
return;
144144

145-
hw_init_tags(KASAN_TAG_MAX);
146-
147145
/*
148146
* Enable async mode only when explicitly requested through
149147
* the command line.

0 commit comments

Comments
 (0)