mte.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 __ASSEMBLY__
  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. /*
  37. * Ensure that the tags written prior to this function are visible
  38. * before the page flags update.
  39. */
  40. smp_wmb();
  41. set_bit(PG_mte_tagged, &page->flags);
  42. }
  43. static inline bool page_mte_tagged(struct page *page)
  44. {
  45. bool ret = test_bit(PG_mte_tagged, &page->flags);
  46. /*
  47. * If the page is tagged, ensure ordering with a likely subsequent
  48. * read of the tags.
  49. */
  50. if (ret)
  51. smp_rmb();
  52. return ret;
  53. }
  54. /*
  55. * Lock the page for tagging and return 'true' if the page can be tagged,
  56. * 'false' if already tagged. PG_mte_tagged is never cleared and therefore the
  57. * locking only happens once for page initialisation.
  58. *
  59. * The page MTE lock state:
  60. *
  61. * Locked: PG_mte_lock && !PG_mte_tagged
  62. * Unlocked: !PG_mte_lock || PG_mte_tagged
  63. *
  64. * Acquire semantics only if the page is tagged (returning 'false').
  65. */
  66. static inline bool try_page_mte_tagging(struct page *page)
  67. {
  68. if (!test_and_set_bit(PG_mte_lock, &page->flags))
  69. return true;
  70. /*
  71. * The tags are either being initialised or may have been initialised
  72. * already. Check if the PG_mte_tagged flag has been set or wait
  73. * otherwise.
  74. */
  75. smp_cond_load_acquire(&page->flags, VAL & (1UL << PG_mte_tagged));
  76. return false;
  77. }
  78. void mte_zero_clear_page_tags(void *addr);
  79. void mte_sync_tags(pte_t pte, unsigned int nr_pages);
  80. void mte_copy_page_tags(void *kto, const void *kfrom);
  81. void mte_thread_init_user(void);
  82. void mte_thread_switch(struct task_struct *next);
  83. void mte_cpu_setup(void);
  84. void mte_suspend_enter(void);
  85. void mte_suspend_exit(void);
  86. long set_mte_ctrl(struct task_struct *task, unsigned long arg);
  87. long get_mte_ctrl(struct task_struct *task);
  88. int mte_ptrace_copy_tags(struct task_struct *child, long request,
  89. unsigned long addr, unsigned long data);
  90. size_t mte_probe_user_range(const char __user *uaddr, size_t size);
  91. #else /* CONFIG_ARM64_MTE */
  92. /* unused if !CONFIG_ARM64_MTE, silence the compiler */
  93. #define PG_mte_tagged 0
  94. static inline void set_page_mte_tagged(struct page *page)
  95. {
  96. }
  97. static inline bool page_mte_tagged(struct page *page)
  98. {
  99. return false;
  100. }
  101. static inline bool try_page_mte_tagging(struct page *page)
  102. {
  103. return false;
  104. }
  105. static inline void mte_zero_clear_page_tags(void *addr)
  106. {
  107. }
  108. static inline void mte_sync_tags(pte_t pte, unsigned int nr_pages)
  109. {
  110. }
  111. static inline void mte_copy_page_tags(void *kto, const void *kfrom)
  112. {
  113. }
  114. static inline void mte_thread_init_user(void)
  115. {
  116. }
  117. static inline void mte_thread_switch(struct task_struct *next)
  118. {
  119. }
  120. static inline void mte_suspend_enter(void)
  121. {
  122. }
  123. static inline void mte_suspend_exit(void)
  124. {
  125. }
  126. static inline long set_mte_ctrl(struct task_struct *task, unsigned long arg)
  127. {
  128. return 0;
  129. }
  130. static inline long get_mte_ctrl(struct task_struct *task)
  131. {
  132. return 0;
  133. }
  134. static inline int mte_ptrace_copy_tags(struct task_struct *child,
  135. long request, unsigned long addr,
  136. unsigned long data)
  137. {
  138. return -EIO;
  139. }
  140. #endif /* CONFIG_ARM64_MTE */
  141. static inline void mte_disable_tco_entry(struct task_struct *task)
  142. {
  143. if (!system_supports_mte())
  144. return;
  145. /*
  146. * Re-enable tag checking (TCO set on exception entry). This is only
  147. * necessary if MTE is enabled in either the kernel or the userspace
  148. * task in synchronous or asymmetric mode (SCTLR_EL1.TCF0 bit 0 is set
  149. * for both). With MTE disabled in the kernel and disabled or
  150. * asynchronous in userspace, tag check faults (including in uaccesses)
  151. * are not reported, therefore there is no need to re-enable checking.
  152. * This is beneficial on microarchitectures where re-enabling TCO is
  153. * expensive.
  154. */
  155. if (kasan_hw_tags_enabled() ||
  156. (task->thread.sctlr_user & (1UL << SCTLR_EL1_TCF0_SHIFT)))
  157. asm volatile(SET_PSTATE_TCO(0));
  158. }
  159. #ifdef CONFIG_KASAN_HW_TAGS
  160. void mte_check_tfsr_el1(void);
  161. static inline void mte_check_tfsr_entry(void)
  162. {
  163. if (!kasan_hw_tags_enabled())
  164. return;
  165. mte_check_tfsr_el1();
  166. }
  167. static inline void mte_check_tfsr_exit(void)
  168. {
  169. if (!kasan_hw_tags_enabled())
  170. return;
  171. /*
  172. * The asynchronous faults are sync'ed automatically with
  173. * TFSR_EL1 on kernel entry but for exit an explicit dsb()
  174. * is required.
  175. */
  176. dsb(nsh);
  177. isb();
  178. mte_check_tfsr_el1();
  179. }
  180. #else
  181. static inline void mte_check_tfsr_el1(void)
  182. {
  183. }
  184. static inline void mte_check_tfsr_entry(void)
  185. {
  186. }
  187. static inline void mte_check_tfsr_exit(void)
  188. {
  189. }
  190. #endif /* CONFIG_KASAN_HW_TAGS */
  191. #endif /* __ASSEMBLY__ */
  192. #endif /* __ASM_MTE_H */