mmu_notifier.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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->rwsem).
  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. #define ptep_clear_flush_young_notify(__vma, __address, __ptep) \
  227. ({ \
  228. int __young; \
  229. struct vm_area_struct *___vma = __vma; \
  230. unsigned long ___address = __address; \
  231. __young = ptep_clear_flush_young(___vma, ___address, __ptep); \
  232. __young |= mmu_notifier_clear_flush_young(___vma->vm_mm, \
  233. ___address); \
  234. __young; \
  235. })
  236. #define pmdp_clear_flush_young_notify(__vma, __address, __pmdp) \
  237. ({ \
  238. int __young; \
  239. struct vm_area_struct *___vma = __vma; \
  240. unsigned long ___address = __address; \
  241. __young = pmdp_clear_flush_young(___vma, ___address, __pmdp); \
  242. __young |= mmu_notifier_clear_flush_young(___vma->vm_mm, \
  243. ___address); \
  244. __young; \
  245. })
  246. /*
  247. * set_pte_at_notify() sets the pte _after_ running the notifier.
  248. * This is safe to start by updating the secondary MMUs, because the primary MMU
  249. * pte invalidate must have already happened with a ptep_clear_flush() before
  250. * set_pte_at_notify() has been invoked. Updating the secondary MMUs first is
  251. * required when we change both the protection of the mapping from read-only to
  252. * read-write and the pfn (like during copy on write page faults). Otherwise the
  253. * old page would remain mapped readonly in the secondary MMUs after the new
  254. * page is already writable by some CPU through the primary MMU.
  255. */
  256. #define set_pte_at_notify(__mm, __address, __ptep, __pte) \
  257. ({ \
  258. struct mm_struct *___mm = __mm; \
  259. unsigned long ___address = __address; \
  260. pte_t ___pte = __pte; \
  261. \
  262. mmu_notifier_change_pte(___mm, ___address, ___pte); \
  263. set_pte_at(___mm, ___address, __ptep, ___pte); \
  264. })
  265. #else /* CONFIG_MMU_NOTIFIER */
  266. static inline void mmu_notifier_release(struct mm_struct *mm)
  267. {
  268. }
  269. static inline int mmu_notifier_clear_flush_young(struct mm_struct *mm,
  270. unsigned long address)
  271. {
  272. return 0;
  273. }
  274. static inline int mmu_notifier_test_young(struct mm_struct *mm,
  275. unsigned long address)
  276. {
  277. return 0;
  278. }
  279. static inline void mmu_notifier_change_pte(struct mm_struct *mm,
  280. unsigned long address, pte_t pte)
  281. {
  282. }
  283. static inline void mmu_notifier_invalidate_page(struct mm_struct *mm,
  284. unsigned long address)
  285. {
  286. }
  287. static inline void mmu_notifier_invalidate_range_start(struct mm_struct *mm,
  288. unsigned long start, unsigned long end)
  289. {
  290. }
  291. static inline void mmu_notifier_invalidate_range_end(struct mm_struct *mm,
  292. unsigned long start, unsigned long end)
  293. {
  294. }
  295. static inline void mmu_notifier_mm_init(struct mm_struct *mm)
  296. {
  297. }
  298. static inline void mmu_notifier_mm_destroy(struct mm_struct *mm)
  299. {
  300. }
  301. #define ptep_clear_flush_young_notify ptep_clear_flush_young
  302. #define pmdp_clear_flush_young_notify pmdp_clear_flush_young
  303. #define set_pte_at_notify set_pte_at
  304. #endif /* CONFIG_MMU_NOTIFIER */
  305. #endif /* _LINUX_MMU_NOTIFIER_H */