1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (C) 2020 ARM Ltd.
8 #include <asm/compiler.h>
9 #include <asm/mte-def.h>
13 #include <linux/bitfield.h>
14 #include <linux/kasan-enabled.h>
15 #include <linux/page-flags.h>
16 #include <linux/sched.h>
17 #include <linux/types.h>
19 #include <asm/pgtable-types.h>
21 void mte_clear_page_tags(void *addr
);
22 unsigned long mte_copy_tags_from_user(void *to
, const void __user
*from
,
24 unsigned long mte_copy_tags_to_user(void __user
*to
, void *from
,
26 int mte_save_tags(struct page
*page
);
27 void mte_save_page_tags(const void *page_addr
, void *tag_storage
);
28 void mte_restore_tags(swp_entry_t entry
, struct page
*page
);
29 void mte_restore_page_tags(void *page_addr
, const void *tag_storage
);
30 void mte_invalidate_tags(int type
, pgoff_t offset
);
31 void mte_invalidate_tags_area(int type
);
32 void *mte_allocate_tag_storage(void);
33 void mte_free_tag_storage(char *storage
);
35 #ifdef CONFIG_ARM64_MTE
37 /* track which pages have valid allocation tags */
38 #define PG_mte_tagged PG_arch_2
39 /* simple lock to avoid multiple threads tagging the same page */
40 #define PG_mte_lock PG_arch_3
42 static inline void set_page_mte_tagged(struct page
*page
)
44 VM_WARN_ON_ONCE(folio_test_hugetlb(page_folio(page
)));
47 * Ensure that the tags written prior to this function are visible
48 * before the page flags update.
51 set_bit(PG_mte_tagged
, &page
->flags
);
54 static inline bool page_mte_tagged(struct page
*page
)
56 bool ret
= test_bit(PG_mte_tagged
, &page
->flags
);
58 VM_WARN_ON_ONCE(folio_test_hugetlb(page_folio(page
)));
61 * If the page is tagged, ensure ordering with a likely subsequent
70 * Lock the page for tagging and return 'true' if the page can be tagged,
71 * 'false' if already tagged. PG_mte_tagged is never cleared and therefore the
72 * locking only happens once for page initialisation.
74 * The page MTE lock state:
76 * Locked: PG_mte_lock && !PG_mte_tagged
77 * Unlocked: !PG_mte_lock || PG_mte_tagged
79 * Acquire semantics only if the page is tagged (returning 'false').
81 static inline bool try_page_mte_tagging(struct page
*page
)
83 VM_WARN_ON_ONCE(folio_test_hugetlb(page_folio(page
)));
85 if (!test_and_set_bit(PG_mte_lock
, &page
->flags
))
89 * The tags are either being initialised or may have been initialised
90 * already. Check if the PG_mte_tagged flag has been set or wait
93 smp_cond_load_acquire(&page
->flags
, VAL
& (1UL << PG_mte_tagged
));
98 void mte_zero_clear_page_tags(void *addr
);
99 void mte_sync_tags(pte_t pte
, unsigned int nr_pages
);
100 void mte_copy_page_tags(void *kto
, const void *kfrom
);
101 void mte_thread_init_user(void);
102 void mte_thread_switch(struct task_struct
*next
);
103 void mte_cpu_setup(void);
104 void mte_suspend_enter(void);
105 void mte_suspend_exit(void);
106 long set_mte_ctrl(struct task_struct
*task
, unsigned long arg
);
107 long get_mte_ctrl(struct task_struct
*task
);
108 int mte_ptrace_copy_tags(struct task_struct
*child
, long request
,
109 unsigned long addr
, unsigned long data
);
110 size_t mte_probe_user_range(const char __user
*uaddr
, size_t size
);
112 #else /* CONFIG_ARM64_MTE */
114 /* unused if !CONFIG_ARM64_MTE, silence the compiler */
115 #define PG_mte_tagged 0
117 static inline void set_page_mte_tagged(struct page
*page
)
120 static inline bool page_mte_tagged(struct page
*page
)
124 static inline bool try_page_mte_tagging(struct page
*page
)
128 static inline void mte_zero_clear_page_tags(void *addr
)
131 static inline void mte_sync_tags(pte_t pte
, unsigned int nr_pages
)
134 static inline void mte_copy_page_tags(void *kto
, const void *kfrom
)
137 static inline void mte_thread_init_user(void)
140 static inline void mte_thread_switch(struct task_struct
*next
)
143 static inline void mte_suspend_enter(void)
146 static inline void mte_suspend_exit(void)
149 static inline long set_mte_ctrl(struct task_struct
*task
, unsigned long arg
)
153 static inline long get_mte_ctrl(struct task_struct
*task
)
157 static inline int mte_ptrace_copy_tags(struct task_struct
*child
,
158 long request
, unsigned long addr
,
164 #endif /* CONFIG_ARM64_MTE */
166 #if defined(CONFIG_HUGETLB_PAGE) && defined(CONFIG_ARM64_MTE)
167 static inline void folio_set_hugetlb_mte_tagged(struct folio
*folio
)
169 VM_WARN_ON_ONCE(!folio_test_hugetlb(folio
));
172 * Ensure that the tags written prior to this function are visible
173 * before the folio flags update.
176 set_bit(PG_mte_tagged
, &folio
->flags
);
180 static inline bool folio_test_hugetlb_mte_tagged(struct folio
*folio
)
182 bool ret
= test_bit(PG_mte_tagged
, &folio
->flags
);
184 VM_WARN_ON_ONCE(!folio_test_hugetlb(folio
));
187 * If the folio is tagged, ensure ordering with a likely subsequent
195 static inline bool folio_try_hugetlb_mte_tagging(struct folio
*folio
)
197 VM_WARN_ON_ONCE(!folio_test_hugetlb(folio
));
199 if (!test_and_set_bit(PG_mte_lock
, &folio
->flags
))
203 * The tags are either being initialised or may have been initialised
204 * already. Check if the PG_mte_tagged flag has been set or wait
207 smp_cond_load_acquire(&folio
->flags
, VAL
& (1UL << PG_mte_tagged
));
212 static inline void folio_set_hugetlb_mte_tagged(struct folio
*folio
)
216 static inline bool folio_test_hugetlb_mte_tagged(struct folio
*folio
)
221 static inline bool folio_try_hugetlb_mte_tagging(struct folio
*folio
)
227 static inline void mte_disable_tco_entry(struct task_struct
*task
)
229 if (!system_supports_mte())
233 * Re-enable tag checking (TCO set on exception entry). This is only
234 * necessary if MTE is enabled in either the kernel or the userspace
235 * task in synchronous or asymmetric mode (SCTLR_EL1.TCF0 bit 0 is set
236 * for both). With MTE disabled in the kernel and disabled or
237 * asynchronous in userspace, tag check faults (including in uaccesses)
238 * are not reported, therefore there is no need to re-enable checking.
239 * This is beneficial on microarchitectures where re-enabling TCO is
242 if (kasan_hw_tags_enabled() ||
243 (task
->thread
.sctlr_user
& (1UL << SCTLR_EL1_TCF0_SHIFT
)))
244 asm volatile(SET_PSTATE_TCO(0));
247 #ifdef CONFIG_KASAN_HW_TAGS
248 void mte_check_tfsr_el1(void);
250 static inline void mte_check_tfsr_entry(void)
252 if (!kasan_hw_tags_enabled())
255 mte_check_tfsr_el1();
258 static inline void mte_check_tfsr_exit(void)
260 if (!kasan_hw_tags_enabled())
264 * The asynchronous faults are sync'ed automatically with
265 * TFSR_EL1 on kernel entry but for exit an explicit dsb()
271 mte_check_tfsr_el1();
274 static inline void mte_check_tfsr_el1(void)
277 static inline void mte_check_tfsr_entry(void)
280 static inline void mte_check_tfsr_exit(void)
283 #endif /* CONFIG_KASAN_HW_TAGS */
285 #endif /* __ASSEMBLY__ */
286 #endif /* __ASM_MTE_H */