mte.h 7.0 KB

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