mmu_notifier.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. #ifndef _LINUX_MMU_NOTIFIER_H
  2. #define _LINUX_MMU_NOTIFIER_H
  3. #include <linux/list.h>
  4. #include <linux/spinlock.h>
  5. #include <linux/mm_types.h>
  6. #include <linux/srcu.h>
  7. struct mmu_notifier;
  8. struct mmu_notifier_ops;
  9. #ifdef CONFIG_MMU_NOTIFIER
  10. /*
  11. * The mmu notifier_mm structure is allocated and installed in
  12. * mm->mmu_notifier_mm inside the mm_take_all_locks() protected
  13. * critical section and it's released only when mm_count reaches zero
  14. * in mmdrop().
  15. */
  16. struct mmu_notifier_mm {
  17. /* all mmu notifiers registerd in this mm are queued in this list */
  18. struct hlist_head list;
  19. /* to serialize the list modifications and hlist_unhashed */
  20. spinlock_t lock;
  21. };
  22. struct mmu_notifier_ops {
  23. /*
  24. * Called either by mmu_notifier_unregister or when the mm is
  25. * being destroyed by exit_mmap, always before all pages are
  26. * freed. This can run concurrently with other mmu notifier
  27. * methods (the ones invoked outside the mm context) and it
  28. * should tear down all secondary mmu mappings and freeze the
  29. * secondary mmu. If this method isn't implemented you've to
  30. * be sure that nothing could possibly write to the pages
  31. * through the secondary mmu by the time the last thread with
  32. * tsk->mm == mm exits.
  33. *
  34. * As side note: the pages freed after ->release returns could
  35. * be immediately reallocated by the gart at an alias physical
  36. * address with a different cache model, so if ->release isn't
  37. * implemented because all _software_ driven memory accesses
  38. * through the secondary mmu are terminated by the time the
  39. * last thread of this mm quits, you've also to be sure that
  40. * speculative _hardware_ operations can't allocate dirty
  41. * cachelines in the cpu that could not be snooped and made
  42. * coherent with the other read and write operations happening
  43. * through the gart alias address, so leading to memory
  44. * corruption.
  45. */
  46. void (*release)(struct mmu_notifier *mn,
  47. struct mm_struct *mm);
  48. /*
  49. * clear_flush_young is called after the VM is
  50. * test-and-clearing the young/accessed bitflag in the
  51. * pte. This way the VM will provide proper aging to the
  52. * accesses to the page through the secondary MMUs and not
  53. * only to the ones through the Linux pte.
  54. */
  55. int (*clear_flush_young)(struct mmu_notifier *mn,
  56. struct mm_struct *mm,
  57. unsigned long address);
  58. /*
  59. * test_young is called to check the young/accessed bitflag in
  60. * the secondary pte. This is used to know if the page is
  61. * frequently used without actually clearing the flag or tearing
  62. * down the secondary mapping on the page.
  63. */
  64. int (*test_young)(struct mmu_notifier *mn,
  65. struct mm_struct *mm,
  66. unsigned long address);
  67. /*
  68. * change_pte is called in cases that pte mapping to page is changed:
  69. * for example, when ksm remaps pte to point to a new shared page.
  70. */
  71. void (*change_pte)(struct mmu_notifier *mn,
  72. struct mm_struct *mm,
  73. unsigned long address,
  74. pte_t pte);
  75. /*
  76. * Before this is invoked any secondary MMU is still ok to
  77. * read/write to the page previously pointed to by the Linux
  78. * pte because the page hasn't been freed yet and it won't be
  79. * freed until this returns. If required set_page_dirty has to
  80. * be called internally to this method.
  81. */
  82. void (*invalidate_page)(struct mmu_notifier *mn,
  83. struct mm_struct *mm,
  84. unsigned long address);
  85. /*
  86. * invalidate_range_start() and invalidate_range_end() must be
  87. * paired and are called only when the mmap_sem and/or the
  88. * locks protecting the reverse maps are held. The subsystem
  89. * must guarantee that no additional references are taken to
  90. * the pages in the range established between the call to
  91. * invalidate_range_start() and the matching call to
  92. * invalidate_range_end().
  93. *
  94. * Invalidation of multiple concurrent ranges may be
  95. * optionally permitted by the driver. Either way the
  96. * establishment of sptes is forbidden in the range passed to
  97. * invalidate_range_begin/end for the whole duration of the
  98. * invalidate_range_begin/end critical section.
  99. *
  100. * invalidate_range_start() is called when all pages in the
  101. * range are still mapped and have at least a refcount of one.
  102. *
  103. * invalidate_range_end() is called when all pages in the
  104. * range have been unmapped and the pages have been freed by
  105. * the VM.
  106. *
  107. * The VM will remove the page table entries and potentially
  108. * the page between invalidate_range_start() and
  109. * invalidate_range_end(). If the page must not be freed
  110. * because of pending I/O or other circumstances then the
  111. * invalidate_range_start() callback (or the initial mapping
  112. * by the driver) must make sure that the refcount is kept
  113. * elevated.
  114. *
  115. * If the driver increases the refcount when the pages are
  116. * initially mapped into an address space then either
  117. * invalidate_range_start() or invalidate_range_end() may
  118. * decrease the refcount. If the refcount is decreased on
  119. * invalidate_range_start() then the VM can free pages as page
  120. * table entries are removed. If the refcount is only
  121. * droppped on invalidate_range_end() then the driver itself
  122. * will drop the last refcount but it must take care to flush
  123. * any secondary tlb before doing the final free on the
  124. * page. Pages will no longer be referenced by the linux
  125. * address space but may still be referenced by sptes until
  126. * the last refcount is dropped.
  127. */
  128. void (*invalidate_range_start)(struct mmu_notifier *mn,
  129. struct mm_struct *mm,
  130. unsigned long start, unsigned long end);
  131. void (*invalidate_range_end)(struct mmu_notifier *mn,
  132. struct mm_struct *mm,
  133. unsigned long start, unsigned long end);
  134. };
  135. /*
  136. * The notifier chains are protected by mmap_sem and/or the reverse map
  137. * semaphores. Notifier chains are only changed when all reverse maps and
  138. * the mmap_sem locks are taken.
  139. *
  140. * Therefore notifier chains can only be traversed when either
  141. *
  142. * 1. mmap_sem is held.
  143. * 2. One of the reverse map locks is held (i_mmap_mutex or anon_vma->mutex).
  144. * 3. No other concurrent thread can access the list (release)
  145. */
  146. struct mmu_notifier {
  147. struct hlist_node hlist;
  148. const struct mmu_notifier_ops *ops;
  149. };
  150. static inline int mm_has_notifiers(struct mm_struct *mm)
  151. {
  152. return unlikely(mm->mmu_notifier_mm);
  153. }
  154. extern int mmu_notifier_register(struct mmu_notifier *mn,
  155. struct mm_struct *mm);
  156. extern int __mmu_notifier_register(struct mmu_notifier *mn,
  157. struct mm_struct *mm);
  158. extern void mmu_notifier_unregister(struct mmu_notifier *mn,
  159. struct mm_struct *mm);
  160. extern void __mmu_notifier_mm_destroy(struct mm_struct *mm);
  161. extern void __mmu_notifier_release(struct mm_struct *mm);
  162. extern int __mmu_notifier_clear_flush_young(struct mm_struct *mm,
  163. unsigned long address);
  164. extern int __mmu_notifier_test_young(struct mm_struct *mm,
  165. unsigned long address);
  166. extern void __mmu_notifier_change_pte(struct mm_struct *mm,
  167. unsigned long address, pte_t pte);
  168. extern void __mmu_notifier_invalidate_page(struct mm_struct *mm,
  169. unsigned long address);
  170. extern void __mmu_notifier_invalidate_range_start(struct mm_struct *mm,
  171. unsigned long start, unsigned long end);
  172. extern void __mmu_notifier_invalidate_range_end(struct mm_struct *mm,
  173. unsigned long start, unsigned long end);
  174. static inline void mmu_notifier_release(struct mm_struct *mm)
  175. {
  176. if (mm_has_notifiers(mm))
  177. __mmu_notifier_release(mm);
  178. }
  179. static inline int mmu_notifier_clear_flush_young(struct mm_struct *mm,
  180. unsigned long address)
  181. {
  182. if (mm_has_notifiers(mm))
  183. return __mmu_notifier_clear_flush_young(mm, address);
  184. return 0;
  185. }
  186. static inline int mmu_notifier_test_young(struct mm_struct *mm,
  187. unsigned long address)
  188. {
  189. if (mm_has_notifiers(mm))
  190. return __mmu_notifier_test_young(mm, address);
  191. return 0;
  192. }
  193. static inline void mmu_notifier_change_pte(struct mm_struct *mm,
  194. unsigned long address, pte_t pte)
  195. {
  196. if (mm_has_notifiers(mm))
  197. __mmu_notifier_change_pte(mm, address, pte);
  198. }
  199. static inline void mmu_notifier_invalidate_page(struct mm_struct *mm,
  200. unsigned long address)
  201. {
  202. if (mm_has_notifiers(mm))
  203. __mmu_notifier_invalidate_page(mm, address);
  204. }
  205. static inline void mmu_notifier_invalidate_range_start(struct mm_struct *mm,
  206. unsigned long start, unsigned long end)
  207. {
  208. if (mm_has_notifiers(mm))
  209. __mmu_notifier_invalidate_range_start(mm, start, end);
  210. }
  211. static inline void mmu_notifier_invalidate_range_end(struct mm_struct *mm,
  212. unsigned long start, unsigned long end)
  213. {
  214. if (mm_has_notifiers(mm))
  215. __mmu_notifier_invalidate_range_end(mm, start, end);
  216. }
  217. static inline void mmu_notifier_mm_init(struct mm_struct *mm)
  218. {
  219. mm->mmu_notifier_mm = NULL;
  220. }
  221. static inline void mmu_notifier_mm_destroy(struct mm_struct *mm)
  222. {
  223. if (mm_has_notifiers(mm))
  224. __mmu_notifier_mm_destroy(mm);
  225. }
  226. /*
  227. * These two macros will sometime replace ptep_clear_flush.
  228. * ptep_clear_flush is implemented as macro itself, so this also is
  229. * implemented as a macro until ptep_clear_flush will converted to an
  230. * inline function, to diminish the risk of compilation failure. The
  231. * invalidate_page method over time can be moved outside the PT lock
  232. * and these two macros can be later removed.
  233. */
  234. #define ptep_clear_flush_notify(__vma, __address, __ptep) \
  235. ({ \
  236. pte_t __pte; \
  237. struct vm_area_struct *___vma = __vma; \
  238. unsigned long ___address = __address; \
  239. __pte = ptep_clear_flush(___vma, ___address, __ptep); \
  240. mmu_notifier_invalidate_page(___vma->vm_mm, ___address); \
  241. __pte; \
  242. })
  243. #define pmdp_clear_flush_notify(__vma, __address, __pmdp) \
  244. ({ \
  245. pmd_t __pmd; \
  246. struct vm_area_struct *___vma = __vma; \
  247. unsigned long ___address = __address; \
  248. VM_BUG_ON(__address & ~HPAGE_PMD_MASK); \
  249. mmu_notifier_invalidate_range_start(___vma->vm_mm, ___address, \
  250. (__address)+HPAGE_PMD_SIZE);\
  251. __pmd = pmdp_clear_flush(___vma, ___address, __pmdp); \
  252. mmu_notifier_invalidate_range_end(___vma->vm_mm, ___address, \
  253. (__address)+HPAGE_PMD_SIZE); \
  254. __pmd; \
  255. })
  256. #define pmdp_splitting_flush_notify(__vma, __address, __pmdp) \
  257. ({ \
  258. struct vm_area_struct *___vma = __vma; \
  259. unsigned long ___address = __address; \
  260. VM_BUG_ON(__address & ~HPAGE_PMD_MASK); \
  261. mmu_notifier_invalidate_range_start(___vma->vm_mm, ___address, \
  262. (__address)+HPAGE_PMD_SIZE);\
  263. pmdp_splitting_flush(___vma, ___address, __pmdp); \
  264. mmu_notifier_invalidate_range_end(___vma->vm_mm, ___address, \
  265. (__address)+HPAGE_PMD_SIZE); \
  266. })
  267. #define ptep_clear_flush_young_notify(__vma, __address, __ptep) \
  268. ({ \
  269. int __young; \
  270. struct vm_area_struct *___vma = __vma; \
  271. unsigned long ___address = __address; \
  272. __young = ptep_clear_flush_young(___vma, ___address, __ptep); \
  273. __young |= mmu_notifier_clear_flush_young(___vma->vm_mm, \
  274. ___address); \
  275. __young; \
  276. })
  277. #define pmdp_clear_flush_young_notify(__vma, __address, __pmdp) \
  278. ({ \
  279. int __young; \
  280. struct vm_area_struct *___vma = __vma; \
  281. unsigned long ___address = __address; \
  282. __young = pmdp_clear_flush_young(___vma, ___address, __pmdp); \
  283. __young |= mmu_notifier_clear_flush_young(___vma->vm_mm, \
  284. ___address); \
  285. __young; \
  286. })
  287. #define set_pte_at_notify(__mm, __address, __ptep, __pte) \
  288. ({ \
  289. struct mm_struct *___mm = __mm; \
  290. unsigned long ___address = __address; \
  291. pte_t ___pte = __pte; \
  292. \
  293. set_pte_at(___mm, ___address, __ptep, ___pte); \
  294. mmu_notifier_change_pte(___mm, ___address, ___pte); \
  295. })
  296. #else /* CONFIG_MMU_NOTIFIER */
  297. static inline void mmu_notifier_release(struct mm_struct *mm)
  298. {
  299. }
  300. static inline int mmu_notifier_clear_flush_young(struct mm_struct *mm,
  301. unsigned long address)
  302. {
  303. return 0;
  304. }
  305. static inline int mmu_notifier_test_young(struct mm_struct *mm,
  306. unsigned long address)
  307. {
  308. return 0;
  309. }
  310. static inline void mmu_notifier_change_pte(struct mm_struct *mm,
  311. unsigned long address, pte_t pte)
  312. {
  313. }
  314. static inline void mmu_notifier_invalidate_page(struct mm_struct *mm,
  315. unsigned long address)
  316. {
  317. }
  318. static inline void mmu_notifier_invalidate_range_start(struct mm_struct *mm,
  319. unsigned long start, unsigned long end)
  320. {
  321. }
  322. static inline void mmu_notifier_invalidate_range_end(struct mm_struct *mm,
  323. unsigned long start, unsigned long end)
  324. {
  325. }
  326. static inline void mmu_notifier_mm_init(struct mm_struct *mm)
  327. {
  328. }
  329. static inline void mmu_notifier_mm_destroy(struct mm_struct *mm)
  330. {
  331. }
  332. #define ptep_clear_flush_young_notify ptep_clear_flush_young
  333. #define pmdp_clear_flush_young_notify pmdp_clear_flush_young
  334. #define ptep_clear_flush_notify ptep_clear_flush
  335. #define pmdp_clear_flush_notify pmdp_clear_flush
  336. #define pmdp_splitting_flush_notify pmdp_splitting_flush
  337. #define set_pte_at_notify set_pte_at
  338. #endif /* CONFIG_MMU_NOTIFIER */
  339. #endif /* _LINUX_MMU_NOTIFIER_H */