1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * Copyright (C) 2020 ARM Ltd. 4 */ 5 #ifndef __ASM_MTE_H 6 #define __ASM_MTE_H 7 8 #include <asm/compiler.h> 9 #include <asm/mte-def.h> 10 11 #ifndef __ASSEMBLER__ 12 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> 18 19 #include <asm/pgtable-types.h> 20 21 void mte_clear_page_tags(void *addr); 22 unsigned long mte_copy_tags_from_user(void *to, const void __user *from, 23 unsigned long n); 24 unsigned long mte_copy_tags_to_user(void __user *to, void *from, 25 unsigned long n); 26 void mte_save_page_tags(const void *page_addr, void *tag_storage); 27 void mte_restore_page_tags(void *page_addr, const void *tag_storage); 28 void mte_invalidate_tags(int type, pgoff_t offset); 29 void mte_invalidate_tags_area(int type); 30 void *mte_allocate_tag_storage(void); 31 void mte_free_tag_storage(char *storage); 32 33 #ifdef CONFIG_ARM64_MTE 34 35 /* track which pages have valid allocation tags */ 36 #define PG_mte_tagged PG_arch_2 37 /* simple lock to avoid multiple threads tagging the same page */ 38 #define PG_mte_lock PG_arch_3 39 40 static inline void set_page_mte_tagged(struct page *page) 41 { 42 VM_WARN_ON_ONCE(folio_test_hugetlb(page_folio(page))); 43 44 /* 45 * Ensure that the tags written prior to this function are visible 46 * before the page flags update. 47 */ 48 smp_wmb(); 49 set_bit(PG_mte_tagged, &page->flags.f); 50 } 51 52 static inline bool page_mte_tagged(struct page *page) 53 { 54 bool ret = test_bit(PG_mte_tagged, &page->flags.f); 55 56 VM_WARN_ON_ONCE(folio_test_hugetlb(page_folio(page))); 57 58 /* 59 * If the page is tagged, ensure ordering with a likely subsequent 60 * read of the tags. 61 */ 62 if (ret) 63 smp_rmb(); 64 return ret; 65 } 66 67 /* 68 * Lock the page for tagging and return 'true' if the page can be tagged, 69 * 'false' if already tagged. PG_mte_tagged is never cleared and therefore the 70 * locking only happens once for page initialisation. 71 * 72 * The page MTE lock state: 73 * 74 * Locked: PG_mte_lock && !PG_mte_tagged 75 * Unlocked: !PG_mte_lock || PG_mte_tagged 76 * 77 * Acquire semantics only if the page is tagged (returning 'false'). 78 */ 79 static inline bool try_page_mte_tagging(struct page *page) 80 { 81 VM_WARN_ON_ONCE(folio_test_hugetlb(page_folio(page))); 82 83 if (!test_and_set_bit(PG_mte_lock, &page->flags.f)) 84 return true; 85 86 /* 87 * The tags are either being initialised or may have been initialised 88 * already. Check if the PG_mte_tagged flag has been set or wait 89 * otherwise. 90 */ 91 smp_cond_load_acquire(&page->flags.f, VAL & (1UL << PG_mte_tagged)); 92 93 return false; 94 } 95 96 void mte_zero_clear_page_tags(void *addr); 97 void mte_sync_tags(pte_t pte, unsigned int nr_pages); 98 void mte_copy_page_tags(void *kto, const void *kfrom); 99 void mte_thread_init_user(void); 100 void mte_thread_switch(struct task_struct *next); 101 void mte_cpu_setup(void); 102 void mte_suspend_enter(void); 103 void mte_suspend_exit(void); 104 long set_mte_ctrl(struct task_struct *task, unsigned long arg); 105 long get_mte_ctrl(struct task_struct *task); 106 int mte_ptrace_copy_tags(struct task_struct *child, long request, 107 unsigned long addr, unsigned long data); 108 size_t mte_probe_user_range(const char __user *uaddr, size_t size); 109 110 #else /* CONFIG_ARM64_MTE */ 111 112 /* unused if !CONFIG_ARM64_MTE, silence the compiler */ 113 #define PG_mte_tagged 0 114 115 static inline void set_page_mte_tagged(struct page *page) 116 { 117 } 118 static inline bool page_mte_tagged(struct page *page) 119 { 120 return false; 121 } 122 static inline bool try_page_mte_tagging(struct page *page) 123 { 124 return false; 125 } 126 static inline void mte_zero_clear_page_tags(void *addr) 127 { 128 } 129 static inline void mte_sync_tags(pte_t pte, unsigned int nr_pages) 130 { 131 } 132 static inline void mte_copy_page_tags(void *kto, const void *kfrom) 133 { 134 } 135 static inline void mte_thread_init_user(void) 136 { 137 } 138 static inline void mte_thread_switch(struct task_struct *next) 139 { 140 } 141 static inline void mte_suspend_enter(void) 142 { 143 } 144 static inline void mte_suspend_exit(void) 145 { 146 } 147 static inline long set_mte_ctrl(struct task_struct *task, unsigned long arg) 148 { 149 return 0; 150 } 151 static inline long get_mte_ctrl(struct task_struct *task) 152 { 153 return 0; 154 } 155 static inline int mte_ptrace_copy_tags(struct task_struct *child, 156 long request, unsigned long addr, 157 unsigned long data) 158 { 159 return -EIO; 160 } 161 162 #endif /* CONFIG_ARM64_MTE */ 163 164 #if defined(CONFIG_HUGETLB_PAGE) && defined(CONFIG_ARM64_MTE) 165 static inline void folio_set_hugetlb_mte_tagged(struct folio *folio) 166 { 167 VM_WARN_ON_ONCE(!folio_test_hugetlb(folio)); 168 169 /* 170 * Ensure that the tags written prior to this function are visible 171 * before the folio flags update. 172 */ 173 smp_wmb(); 174 set_bit(PG_mte_tagged, &folio->flags.f); 175 176 } 177 178 static inline bool folio_test_hugetlb_mte_tagged(struct folio *folio) 179 { 180 bool ret = test_bit(PG_mte_tagged, &folio->flags.f); 181 182 VM_WARN_ON_ONCE(!folio_test_hugetlb(folio)); 183 184 /* 185 * If the folio is tagged, ensure ordering with a likely subsequent 186 * read of the tags. 187 */ 188 if (ret) 189 smp_rmb(); 190 return ret; 191 } 192 193 static inline bool folio_try_hugetlb_mte_tagging(struct folio *folio) 194 { 195 VM_WARN_ON_ONCE(!folio_test_hugetlb(folio)); 196 197 if (!test_and_set_bit(PG_mte_lock, &folio->flags.f)) 198 return true; 199 200 /* 201 * The tags are either being initialised or may have been initialised 202 * already. Check if the PG_mte_tagged flag has been set or wait 203 * otherwise. 204 */ 205 smp_cond_load_acquire(&folio->flags.f, VAL & (1UL << PG_mte_tagged)); 206 207 return false; 208 } 209 #else 210 static inline void folio_set_hugetlb_mte_tagged(struct folio *folio) 211 { 212 } 213 214 static inline bool folio_test_hugetlb_mte_tagged(struct folio *folio) 215 { 216 return false; 217 } 218 219 static inline bool folio_try_hugetlb_mte_tagging(struct folio *folio) 220 { 221 return false; 222 } 223 #endif 224 225 static inline void mte_disable_tco_entry(struct task_struct *task) 226 { 227 if (!system_supports_mte()) 228 return; 229 230 /* 231 * Re-enable tag checking (TCO set on exception entry). This is only 232 * necessary if MTE is enabled in either the kernel or the userspace 233 * task in synchronous or asymmetric mode (SCTLR_EL1.TCF0 bit 0 is set 234 * for both). With MTE disabled in the kernel and disabled or 235 * asynchronous in userspace, tag check faults (including in uaccesses) 236 * are not reported, therefore there is no need to re-enable checking. 237 * This is beneficial on microarchitectures where re-enabling TCO is 238 * expensive. 239 */ 240 if (kasan_hw_tags_enabled() || 241 (task->thread.sctlr_user & (1UL << SCTLR_EL1_TCF0_SHIFT))) 242 asm volatile(SET_PSTATE_TCO(0)); 243 } 244 245 #ifdef CONFIG_KASAN_HW_TAGS 246 void mte_check_tfsr_el1(void); 247 248 static inline void mte_check_tfsr_entry(void) 249 { 250 if (!kasan_hw_tags_enabled()) 251 return; 252 253 if (!system_uses_mte_async_or_asymm_mode()) 254 return; 255 256 mte_check_tfsr_el1(); 257 } 258 259 static inline void mte_check_tfsr_exit(void) 260 { 261 if (!kasan_hw_tags_enabled()) 262 return; 263 264 if (!system_uses_mte_async_or_asymm_mode()) 265 return; 266 267 /* 268 * The asynchronous faults are sync'ed automatically with 269 * TFSR_EL1 on kernel entry but for exit an explicit dsb() 270 * is required. 271 */ 272 dsb(nsh); 273 isb(); 274 275 mte_check_tfsr_el1(); 276 } 277 #else 278 static inline void mte_check_tfsr_el1(void) 279 { 280 } 281 static inline void mte_check_tfsr_entry(void) 282 { 283 } 284 static inline void mte_check_tfsr_exit(void) 285 { 286 } 287 #endif /* CONFIG_KASAN_HW_TAGS */ 288 289 #endif /* __ASSEMBLER__ */ 290 #endif /* __ASM_MTE_H */ 291