1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2020 ARM Ltd.
6 #include <linux/bitops.h>
8 #include <linux/kernel.h>
10 #include <linux/prctl.h>
11 #include <linux/sched.h>
12 #include <linux/sched/mm.h>
13 #include <linux/string.h>
14 #include <linux/swap.h>
15 #include <linux/swapops.h>
16 #include <linux/thread_info.h>
17 #include <linux/types.h>
18 #include <linux/uaccess.h>
19 #include <linux/uio.h>
21 #include <asm/barrier.h>
22 #include <asm/cpufeature.h>
24 #include <asm/ptrace.h>
25 #include <asm/sysreg.h>
27 static DEFINE_PER_CPU_READ_MOSTLY(u64
, mte_tcf_preferred
);
29 #ifdef CONFIG_KASAN_HW_TAGS
31 * The asynchronous and asymmetric MTE modes have the same behavior for
32 * store operations. This flag is set when either of these modes is enabled.
34 DEFINE_STATIC_KEY_FALSE(mte_async_or_asymm_mode
);
35 EXPORT_SYMBOL_GPL(mte_async_or_asymm_mode
);
38 void mte_sync_tags(pte_t pte
, unsigned int nr_pages
)
40 struct page
*page
= pte_page(pte
);
41 struct folio
*folio
= page_folio(page
);
44 if (folio_test_hugetlb(folio
)) {
45 unsigned long nr
= folio_nr_pages(folio
);
47 /* Hugetlb MTE flags are set for head page only */
48 if (folio_try_hugetlb_mte_tagging(folio
)) {
49 for (i
= 0; i
< nr
; i
++, page
++)
50 mte_clear_page_tags(page_address(page
));
51 folio_set_hugetlb_mte_tagged(folio
);
54 /* ensure the tags are visible before the PTE is set */
60 /* if PG_mte_tagged is set, tags have already been initialised */
61 for (i
= 0; i
< nr_pages
; i
++, page
++) {
62 if (try_page_mte_tagging(page
)) {
63 mte_clear_page_tags(page_address(page
));
64 set_page_mte_tagged(page
);
68 /* ensure the tags are visible before the PTE is set */
72 int memcmp_pages(struct page
*page1
, struct page
*page2
)
77 addr1
= page_address(page1
);
78 addr2
= page_address(page2
);
79 ret
= memcmp(addr1
, addr2
, PAGE_SIZE
);
81 if (!system_supports_mte() || ret
)
85 * If the page content is identical but at least one of the pages is
86 * tagged, return non-zero to avoid KSM merging. If only one of the
87 * pages is tagged, __set_ptes() may zero or change the tags of the
88 * other page via mte_sync_tags().
90 if (page_mte_tagged(page1
) || page_mte_tagged(page2
))
91 return addr1
!= addr2
;
96 static inline void __mte_enable_kernel(const char *mode
, unsigned long tcf
)
98 /* Enable MTE Sync Mode for EL1. */
99 sysreg_clear_set(sctlr_el1
, SCTLR_EL1_TCF_MASK
,
100 SYS_FIELD_PREP(SCTLR_EL1
, TCF
, tcf
));
103 pr_info_once("MTE: enabled in %s mode at EL1\n", mode
);
106 #ifdef CONFIG_KASAN_HW_TAGS
107 void mte_enable_kernel_sync(void)
110 * Make sure we enter this function when no PE has set
111 * async mode previously.
113 WARN_ONCE(system_uses_mte_async_or_asymm_mode(),
114 "MTE async mode enabled system wide!");
116 __mte_enable_kernel("synchronous", SCTLR_EL1_TCF_SYNC
);
119 void mte_enable_kernel_async(void)
121 __mte_enable_kernel("asynchronous", SCTLR_EL1_TCF_ASYNC
);
124 * MTE async mode is set system wide by the first PE that
125 * executes this function.
127 * Note: If in future KASAN acquires a runtime switching
128 * mode in between sync and async, this strategy needs
131 if (!system_uses_mte_async_or_asymm_mode())
132 static_branch_enable(&mte_async_or_asymm_mode
);
135 void mte_enable_kernel_asymm(void)
137 if (cpus_have_cap(ARM64_MTE_ASYMM
)) {
138 __mte_enable_kernel("asymmetric", SCTLR_EL1_TCF_ASYMM
);
141 * MTE asymm mode behaves as async mode for store
142 * operations. The mode is set system wide by the
143 * first PE that executes this function.
145 * Note: If in future KASAN acquires a runtime switching
146 * mode in between sync and async, this strategy needs
149 if (!system_uses_mte_async_or_asymm_mode())
150 static_branch_enable(&mte_async_or_asymm_mode
);
153 * If the CPU does not support MTE asymmetric mode the
154 * kernel falls back on synchronous mode which is the
155 * default for kasan=on.
157 mte_enable_kernel_sync();
162 #ifdef CONFIG_KASAN_HW_TAGS
163 void mte_check_tfsr_el1(void)
165 u64 tfsr_el1
= read_sysreg_s(SYS_TFSR_EL1
);
167 if (unlikely(tfsr_el1
& SYS_TFSR_EL1_TF1
)) {
169 * Note: isb() is not required after this direct write
170 * because there is no indirect read subsequent to it
171 * (per ARM DDI 0487F.c table D13-1).
173 write_sysreg_s(0, SYS_TFSR_EL1
);
175 kasan_report_async();
181 * This is where we actually resolve the system and process MTE mode
182 * configuration into an actual value in SCTLR_EL1 that affects
185 static void mte_update_sctlr_user(struct task_struct
*task
)
188 * This must be called with preemption disabled and can only be called
189 * on the current or next task since the CPU must match where the thread
190 * is going to run. The caller is responsible for calling
191 * update_sctlr_el1() later in the same preemption disabled block.
193 unsigned long sctlr
= task
->thread
.sctlr_user
;
194 unsigned long mte_ctrl
= task
->thread
.mte_ctrl
;
195 unsigned long pref
, resolved_mte_tcf
;
197 pref
= __this_cpu_read(mte_tcf_preferred
);
199 * If there is no overlap between the system preferred and
200 * program requested values go with what was requested.
202 resolved_mte_tcf
= (mte_ctrl
& pref
) ? pref
: mte_ctrl
;
203 sctlr
&= ~SCTLR_EL1_TCF0_MASK
;
205 * Pick an actual setting. The order in which we check for
206 * set bits and map into register values determines our
209 if (resolved_mte_tcf
& MTE_CTRL_TCF_ASYMM
)
210 sctlr
|= SYS_FIELD_PREP_ENUM(SCTLR_EL1
, TCF0
, ASYMM
);
211 else if (resolved_mte_tcf
& MTE_CTRL_TCF_ASYNC
)
212 sctlr
|= SYS_FIELD_PREP_ENUM(SCTLR_EL1
, TCF0
, ASYNC
);
213 else if (resolved_mte_tcf
& MTE_CTRL_TCF_SYNC
)
214 sctlr
|= SYS_FIELD_PREP_ENUM(SCTLR_EL1
, TCF0
, SYNC
);
215 task
->thread
.sctlr_user
= sctlr
;
218 static void mte_update_gcr_excl(struct task_struct
*task
)
221 * SYS_GCR_EL1 will be set to current->thread.mte_ctrl value by
222 * mte_set_user_gcr() in kernel_exit, but only if KASAN is enabled.
224 if (kasan_hw_tags_enabled())
228 ((task
->thread
.mte_ctrl
>> MTE_CTRL_GCR_USER_EXCL_SHIFT
) &
229 SYS_GCR_EL1_EXCL_MASK
) | SYS_GCR_EL1_RRND
,
233 #ifdef CONFIG_KASAN_HW_TAGS
234 /* Only called from assembly, silence sparse */
235 void __init
kasan_hw_tags_enable(struct alt_instr
*alt
, __le32
*origptr
,
236 __le32
*updptr
, int nr_inst
);
238 void __init
kasan_hw_tags_enable(struct alt_instr
*alt
, __le32
*origptr
,
239 __le32
*updptr
, int nr_inst
)
241 BUG_ON(nr_inst
!= 1); /* Branch -> NOP */
243 if (kasan_hw_tags_enabled())
244 *updptr
= cpu_to_le32(aarch64_insn_gen_nop());
248 void mte_thread_init_user(void)
250 if (!system_supports_mte())
253 /* clear any pending asynchronous tag fault */
255 write_sysreg_s(0, SYS_TFSRE0_EL1
);
256 clear_thread_flag(TIF_MTE_ASYNC_FAULT
);
257 /* disable tag checking and reset tag generation mask */
258 set_mte_ctrl(current
, 0);
261 void mte_thread_switch(struct task_struct
*next
)
263 if (!system_supports_mte())
266 mte_update_sctlr_user(next
);
267 mte_update_gcr_excl(next
);
269 /* TCO may not have been disabled on exception entry for the current task. */
270 mte_disable_tco_entry(next
);
273 * Check if an async tag exception occurred at EL1.
275 * Note: On the context switch path we rely on the dsb() present
276 * in __switch_to() to guarantee that the indirect writes to TFSR_EL1
277 * are synchronized before this point.
280 mte_check_tfsr_el1();
283 void mte_cpu_setup(void)
288 * CnP must be enabled only after the MAIR_EL1 register has been set
289 * up. Inconsistent MAIR_EL1 between CPUs sharing the same TLB may
290 * lead to the wrong memory type being used for a brief window during
293 * CnP is not a boot feature so MTE gets enabled before CnP, but let's
294 * make sure that is the case.
296 BUG_ON(read_sysreg(ttbr0_el1
) & TTBR_CNP_BIT
);
297 BUG_ON(read_sysreg(ttbr1_el1
) & TTBR_CNP_BIT
);
299 /* Normal Tagged memory type at the corresponding MAIR index */
300 sysreg_clear_set(mair_el1
,
301 MAIR_ATTRIDX(MAIR_ATTR_MASK
, MT_NORMAL_TAGGED
),
302 MAIR_ATTRIDX(MAIR_ATTR_NORMAL_TAGGED
,
305 write_sysreg_s(KERNEL_GCR_EL1
, SYS_GCR_EL1
);
308 * If GCR_EL1.RRND=1 is implemented the same way as RRND=0, then
309 * RGSR_EL1.SEED must be non-zero for IRG to produce
310 * pseudorandom numbers. As RGSR_EL1 is UNKNOWN out of reset, we
311 * must initialize it.
313 rgsr
= (read_sysreg(CNTVCT_EL0
) & SYS_RGSR_EL1_SEED_MASK
) <<
314 SYS_RGSR_EL1_SEED_SHIFT
;
316 rgsr
= 1 << SYS_RGSR_EL1_SEED_SHIFT
;
317 write_sysreg_s(rgsr
, SYS_RGSR_EL1
);
319 /* clear any pending tag check faults in TFSR*_EL1 */
320 write_sysreg_s(0, SYS_TFSR_EL1
);
321 write_sysreg_s(0, SYS_TFSRE0_EL1
);
323 local_flush_tlb_all();
326 void mte_suspend_enter(void)
328 if (!system_supports_mte())
332 * The barriers are required to guarantee that the indirect writes
333 * to TFSR_EL1 are synchronized before we report the state.
338 /* Report SYS_TFSR_EL1 before suspend entry */
339 mte_check_tfsr_el1();
342 void mte_suspend_exit(void)
344 if (!system_supports_mte())
350 long set_mte_ctrl(struct task_struct
*task
, unsigned long arg
)
352 u64 mte_ctrl
= (~((arg
& PR_MTE_TAG_MASK
) >> PR_MTE_TAG_SHIFT
) &
353 SYS_GCR_EL1_EXCL_MASK
) << MTE_CTRL_GCR_USER_EXCL_SHIFT
;
355 if (!system_supports_mte())
358 if (arg
& PR_MTE_TCF_ASYNC
)
359 mte_ctrl
|= MTE_CTRL_TCF_ASYNC
;
360 if (arg
& PR_MTE_TCF_SYNC
)
361 mte_ctrl
|= MTE_CTRL_TCF_SYNC
;
364 * If the system supports it and both sync and async modes are
365 * specified then implicitly enable asymmetric mode.
366 * Userspace could see a mix of both sync and async anyway due
367 * to differing or changing defaults on CPUs.
369 if (cpus_have_cap(ARM64_MTE_ASYMM
) &&
370 (arg
& PR_MTE_TCF_ASYNC
) &&
371 (arg
& PR_MTE_TCF_SYNC
))
372 mte_ctrl
|= MTE_CTRL_TCF_ASYMM
;
374 task
->thread
.mte_ctrl
= mte_ctrl
;
375 if (task
== current
) {
377 mte_update_sctlr_user(task
);
378 mte_update_gcr_excl(task
);
379 update_sctlr_el1(task
->thread
.sctlr_user
);
386 long get_mte_ctrl(struct task_struct
*task
)
389 u64 mte_ctrl
= task
->thread
.mte_ctrl
;
390 u64 incl
= (~mte_ctrl
>> MTE_CTRL_GCR_USER_EXCL_SHIFT
) &
391 SYS_GCR_EL1_EXCL_MASK
;
393 if (!system_supports_mte())
396 ret
= incl
<< PR_MTE_TAG_SHIFT
;
397 if (mte_ctrl
& MTE_CTRL_TCF_ASYNC
)
398 ret
|= PR_MTE_TCF_ASYNC
;
399 if (mte_ctrl
& MTE_CTRL_TCF_SYNC
)
400 ret
|= PR_MTE_TCF_SYNC
;
406 * Access MTE tags in another process' address space as given in mm. Update
407 * the number of tags copied. Return 0 if any tags copied, error otherwise.
408 * Inspired by __access_remote_vm().
410 static int __access_remote_tags(struct mm_struct
*mm
, unsigned long addr
,
411 struct iovec
*kiov
, unsigned int gup_flags
)
413 void __user
*buf
= kiov
->iov_base
;
414 size_t len
= kiov
->iov_len
;
416 int write
= gup_flags
& FOLL_WRITE
;
418 if (!access_ok(buf
, len
))
421 if (mmap_read_lock_killable(mm
))
425 struct vm_area_struct
*vma
;
426 unsigned long tags
, offset
;
428 struct page
*page
= get_user_page_vma_remote(mm
, addr
,
438 * Only copy tags if the page has been mapped as PROT_MTE
439 * (PG_mte_tagged set). Otherwise the tags are not valid and
440 * not accessible to user. Moreover, an mprotect(PROT_MTE)
441 * would cause the existing tags to be cleared if the page
442 * was never mapped with PROT_MTE.
444 if (!(vma
->vm_flags
& VM_MTE
)) {
450 folio
= page_folio(page
);
451 if (folio_test_hugetlb(folio
))
452 WARN_ON_ONCE(!folio_test_hugetlb_mte_tagged(folio
));
454 WARN_ON_ONCE(!page_mte_tagged(page
));
456 /* limit access to the end of the page */
457 offset
= offset_in_page(addr
);
458 tags
= min(len
, (PAGE_SIZE
- offset
) / MTE_GRANULE_SIZE
);
460 maddr
= page_address(page
);
462 tags
= mte_copy_tags_from_user(maddr
+ offset
, buf
, tags
);
463 set_page_dirty_lock(page
);
465 tags
= mte_copy_tags_to_user(buf
, maddr
+ offset
, tags
);
469 /* error accessing the tracer's buffer */
475 addr
+= tags
* MTE_GRANULE_SIZE
;
477 mmap_read_unlock(mm
);
479 /* return an error if no tags copied */
480 kiov
->iov_len
= buf
- kiov
->iov_base
;
481 if (!kiov
->iov_len
) {
482 /* check for error accessing the tracee's address space */
493 * Copy MTE tags in another process' address space at 'addr' to/from tracer's
494 * iovec buffer. Return 0 on success. Inspired by ptrace_access_vm().
496 static int access_remote_tags(struct task_struct
*tsk
, unsigned long addr
,
497 struct iovec
*kiov
, unsigned int gup_flags
)
499 struct mm_struct
*mm
;
502 mm
= get_task_mm(tsk
);
506 if (!tsk
->ptrace
|| (current
!= tsk
->parent
) ||
507 ((get_dumpable(mm
) != SUID_DUMP_USER
) &&
508 !ptracer_capable(tsk
, mm
->user_ns
))) {
513 ret
= __access_remote_tags(mm
, addr
, kiov
, gup_flags
);
519 int mte_ptrace_copy_tags(struct task_struct
*child
, long request
,
520 unsigned long addr
, unsigned long data
)
524 struct iovec __user
*uiov
= (void __user
*)data
;
525 unsigned int gup_flags
= FOLL_FORCE
;
527 if (!system_supports_mte())
530 if (get_user(kiov
.iov_base
, &uiov
->iov_base
) ||
531 get_user(kiov
.iov_len
, &uiov
->iov_len
))
534 if (request
== PTRACE_POKEMTETAGS
)
535 gup_flags
|= FOLL_WRITE
;
537 /* align addr to the MTE tag granule */
538 addr
&= MTE_GRANULE_MASK
;
540 ret
= access_remote_tags(child
, addr
, &kiov
, gup_flags
);
542 ret
= put_user(kiov
.iov_len
, &uiov
->iov_len
);
547 static ssize_t
mte_tcf_preferred_show(struct device
*dev
,
548 struct device_attribute
*attr
, char *buf
)
550 switch (per_cpu(mte_tcf_preferred
, dev
->id
)) {
551 case MTE_CTRL_TCF_ASYNC
:
552 return sysfs_emit(buf
, "async\n");
553 case MTE_CTRL_TCF_SYNC
:
554 return sysfs_emit(buf
, "sync\n");
555 case MTE_CTRL_TCF_ASYMM
:
556 return sysfs_emit(buf
, "asymm\n");
558 return sysfs_emit(buf
, "???\n");
562 static ssize_t
mte_tcf_preferred_store(struct device
*dev
,
563 struct device_attribute
*attr
,
564 const char *buf
, size_t count
)
568 if (sysfs_streq(buf
, "async"))
569 tcf
= MTE_CTRL_TCF_ASYNC
;
570 else if (sysfs_streq(buf
, "sync"))
571 tcf
= MTE_CTRL_TCF_SYNC
;
572 else if (cpus_have_cap(ARM64_MTE_ASYMM
) && sysfs_streq(buf
, "asymm"))
573 tcf
= MTE_CTRL_TCF_ASYMM
;
578 per_cpu(mte_tcf_preferred
, dev
->id
) = tcf
;
583 static DEVICE_ATTR_RW(mte_tcf_preferred
);
585 static int register_mte_tcf_preferred_sysctl(void)
589 if (!system_supports_mte())
592 for_each_possible_cpu(cpu
) {
593 per_cpu(mte_tcf_preferred
, cpu
) = MTE_CTRL_TCF_ASYNC
;
594 device_create_file(get_cpu_device(cpu
),
595 &dev_attr_mte_tcf_preferred
);
600 subsys_initcall(register_mte_tcf_preferred_sysctl
);
603 * Return 0 on success, the number of bytes not probed otherwise.
605 size_t mte_probe_user_range(const char __user
*uaddr
, size_t size
)
607 const char __user
*end
= uaddr
+ size
;
610 __raw_get_user(val
, uaddr
, efault
);
612 uaddr
= PTR_ALIGN(uaddr
, MTE_GRANULE_SIZE
);
613 while (uaddr
< end
) {
615 * A read is sufficient for mte, the caller should have probed
616 * for the pte write permission if required.
618 __raw_get_user(val
, uaddr
, efault
);
619 uaddr
+= MTE_GRANULE_SIZE
;