mmu_notifier.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. * Start-end is necessary in case the secondary MMU is mapping the page
  55. * at a smaller granularity than the primary MMU.
  56. */
  57. int (*clear_flush_young)(struct mmu_notifier *mn,
  58. struct mm_struct *mm,
  59. unsigned long start,
  60. unsigned long end);
  61. /*
  62. * test_young is called to check the young/accessed bitflag in
  63. * the secondary pte. This is used to know if the page is
  64. * frequently used without actually clearing the flag or tearing
  65. * down the secondary mapping on the page.
  66. */
  67. int (*test_young)(struct mmu_notifier *mn,
  68. struct mm_struct *mm,
  69. unsigned long address);
  70. /*
  71. * change_pte is called in cases that pte mapping to page is changed:
  72. * for example, when ksm remaps pte to point to a new shared page.
  73. */
  74. void (*change_pte)(struct mmu_notifier *mn,
  75. struct mm_struct *mm,
  76. unsigned long address,
  77. pte_t pte);
  78. /*
  79. * Before this is invoked any secondary MMU is still ok to
  80. * read/write to the page previously pointed to by the Linux
  81. * pte because the page hasn't been freed yet and it won't be
  82. * freed until this returns. If required set_page_dirty has to
  83. * be called internally to this method.
  84. */
  85. void (*invalidate_page)(struct mmu_notifier *mn,
  86. struct mm_struct *mm,
  87. unsigned long address);
  88. /*
  89. * invalidate_range_start() and invalidate_range_end() must be
  90. * paired and are called only when the mmap_sem and/or the
  91. * locks protecting the reverse maps are held. The subsystem
  92. * must guarantee that no additional references are taken to
  93. * the pages in the range established between the call to
  94. * invalidate_range_start() and the matching call to
  95. * invalidate_range_end().
  96. *
  97. * Invalidation of multiple concurrent ranges may be
  98. * optionally permitted by the driver. Either way the
  99. * establishment of sptes is forbidden in the range passed to
  100. * invalidate_range_begin/end for the whole duration of the
  101. * invalidate_range_begin/end critical section.
  102. *
  103. * invalidate_range_start() is called when all pages in the
  104. * range are still mapped and have at least a refcount of one.
  105. *
  106. * invalidate_range_end() is called when all pages in the
  107. * range have been unmapped and the pages have been freed by
  108. * the VM.
  109. *
  110. * The VM will remove the page table entries and potentially
  111. * the page between invalidate_range_start() and
  112. * invalidate_range_end(). If the page must not be freed
  113. * because of pending I/O or other circumstances then the
  114. * invalidate_range_start() callback (or the initial mapping
  115. * by the driver) must make sure that the refcount is kept
  116. * elevated.
  117. *
  118. * If the driver increases the refcount when the pages are
  119. * initially mapped into an address space then either
  120. * invalidate_range_start() or invalidate_range_end() may
  121. * decrease the refcount. If the refcount is decreased on
  122. * invalidate_range_start() then the VM can free pages as page
  123. * table entries are removed. If the refcount is only
  124. * droppped on invalidate_range_end() then the driver itself
  125. * will drop the last refcount but it must take care to flush
  126. * any secondary tlb before doing the final free on the
  127. * page. Pages will no longer be referenced by the linux
  128. * address space but may still be referenced by sptes until
  129. * the last refcount is dropped.
  130. */
  131. void (*invalidate_range_start)(struct mmu_notifier *mn,
  132. struct mm_struct *mm,
  133. unsigned long start, unsigned long end);
  134. void (*invalidate_range_end)(struct mmu_notifier *mn,
  135. struct mm_struct *mm,
  136. unsigned long start, unsigned long end);
  137. };
  138. /*
  139. * The notifier chains are protected by mmap_sem and/or the reverse map
  140. * semaphores. Notifier chains are only changed when all reverse maps and
  141. * the mmap_sem locks are taken.
  142. *
  143. * Therefore notifier chains can only be traversed when either
  144. *
  145. * 1. mmap_sem is held.
  146. * 2. One of the reverse map locks is held (i_mmap_mutex or anon_vma->rwsem).
  147. * 3. No other concurrent thread can access the list (release)
  148. */
  149. struct mmu_notifier {
  150. struct hlist_node hlist;
  151. const struct mmu_notifier_ops *ops;
  152. };
  153. static inline int mm_has_notifiers(struct mm_struct *mm)
  154. {
  155. return unlikely(mm->mmu_notifier_mm);
  156. }
  157. extern int mmu_notifier_register(struct mmu_notifier *mn,
  158. struct mm_struct *mm);
  159. extern int __mmu_notifier_register(struct mmu_notifier *mn,
  160. struct mm_struct *mm);
  161. extern void mmu_notifier_unregister(struct mmu_notifier *mn,
  162. struct mm_struct *mm);
  163. extern void mmu_notifier_unregister_no_release(struct mmu_notifier *mn,
  164. struct mm_struct *mm);
  165. extern void __mmu_notifier_mm_destroy(struct mm_struct *mm);
  166. extern void __mmu_notifier_release(struct mm_struct *mm);
  167. extern int __mmu_notifier_clear_flush_young(struct mm_struct *mm,
  168. unsigned long start,
  169. unsigned long end);
  170. extern int __mmu_notifier_test_young(struct mm_struct *mm,
  171. unsigned long address);
  172. extern void __mmu_notifier_change_pte(struct mm_struct *mm,
  173. unsigned long address, pte_t pte);
  174. extern void __mmu_notifier_invalidate_page(struct mm_struct *mm,
  175. unsigned long address);
  176. extern void __mmu_notifier_invalidate_range_start(struct mm_struct *mm,
  177. unsigned long start, unsigned long end);
  178. extern void __mmu_notifier_invalidate_range_end(struct mm_struct *mm,
  179. unsigned long start, unsigned long end);
  180. static inline void mmu_notifier_release(struct mm_struct *mm)
  181. {
  182. if (mm_has_notifiers(mm))
  183. __mmu_notifier_release(mm);
  184. }
  185. static inline int mmu_notifier_clear_flush_young(struct mm_struct *mm,
  186. unsigned long start,
  187. unsigned long end)
  188. {
  189. if (mm_has_notifiers(mm))
  190. return __mmu_notifier_clear_flush_young(mm, start, end);
  191. return 0;
  192. }
  193. static inline int mmu_notifier_test_young(struct mm_struct *mm,
  194. unsigned long address)
  195. {
  196. if (mm_has_notifiers(mm))
  197. return __mmu_notifier_test_young(mm, address);
  198. return 0;
  199. }
  200. static inline void mmu_notifier_change_pte(struct mm_struct *mm,
  201. unsigned long address, pte_t pte)
  202. {
  203. if (mm_has_notifiers(mm))
  204. __mmu_notifier_change_pte(mm, address, pte);
  205. }
  206. static inline void mmu_notifier_invalidate_page(struct mm_struct *mm,
  207. unsigned long address)
  208. {
  209. if (mm_has_notifiers(mm))
  210. __mmu_notifier_invalidate_page(mm, address);
  211. }
  212. static inline void mmu_notifier_invalidate_range_start(struct mm_struct *mm,
  213. unsigned long start, unsigned long end)
  214. {
  215. if (mm_has_notifiers(mm))
  216. __mmu_notifier_invalidate_range_start(mm, start, end);
  217. }
  218. static inline void mmu_notifier_invalidate_range_end(struct mm_struct *mm,
  219. unsigned long start, unsigned long end)
  220. {
  221. if (mm_has_notifiers(mm))
  222. __mmu_notifier_invalidate_range_end(mm, start, end);
  223. }
  224. static inline void mmu_notifier_mm_init(struct mm_struct *mm)
  225. {
  226. mm->mmu_notifier_mm = NULL;
  227. }
  228. static inline void mmu_notifier_mm_destroy(struct mm_struct *mm)
  229. {
  230. if (mm_has_notifiers(mm))
  231. __mmu_notifier_mm_destroy(mm);
  232. }
  233. #define ptep_clear_flush_young_notify(__vma, __address, __ptep) \
  234. ({ \
  235. int __young; \
  236. struct vm_area_struct *___vma = __vma; \
  237. unsigned long ___address = __address; \
  238. __young = ptep_clear_flush_young(___vma, ___address, __ptep); \
  239. __young |= mmu_notifier_clear_flush_young(___vma->vm_mm, \
  240. ___address, \
  241. ___address + \
  242. PAGE_SIZE); \
  243. __young; \
  244. })
  245. #define pmdp_clear_flush_young_notify(__vma, __address, __pmdp) \
  246. ({ \
  247. int __young; \
  248. struct vm_area_struct *___vma = __vma; \
  249. unsigned long ___address = __address; \
  250. __young = pmdp_clear_flush_young(___vma, ___address, __pmdp); \
  251. __young |= mmu_notifier_clear_flush_young(___vma->vm_mm, \
  252. ___address, \
  253. ___address + \
  254. PMD_SIZE); \
  255. __young; \
  256. })
  257. /*
  258. * set_pte_at_notify() sets the pte _after_ running the notifier.
  259. * This is safe to start by updating the secondary MMUs, because the primary MMU
  260. * pte invalidate must have already happened with a ptep_clear_flush() before
  261. * set_pte_at_notify() has been invoked. Updating the secondary MMUs first is
  262. * required when we change both the protection of the mapping from read-only to
  263. * read-write and the pfn (like during copy on write page faults). Otherwise the
  264. * old page would remain mapped readonly in the secondary MMUs after the new
  265. * page is already writable by some CPU through the primary MMU.
  266. */
  267. #define set_pte_at_notify(__mm, __address, __ptep, __pte) \
  268. ({ \
  269. struct mm_struct *___mm = __mm; \
  270. unsigned long ___address = __address; \
  271. pte_t ___pte = __pte; \
  272. \
  273. mmu_notifier_change_pte(___mm, ___address, ___pte); \
  274. set_pte_at(___mm, ___address, __ptep, ___pte); \
  275. })
  276. extern void mmu_notifier_call_srcu(struct rcu_head *rcu,
  277. void (*func)(struct rcu_head *rcu));
  278. extern void mmu_notifier_synchronize(void);
  279. #else /* CONFIG_MMU_NOTIFIER */
  280. static inline void mmu_notifier_release(struct mm_struct *mm)
  281. {
  282. }
  283. static inline int mmu_notifier_clear_flush_young(struct mm_struct *mm,
  284. unsigned long start,
  285. unsigned long end)
  286. {
  287. return 0;
  288. }
  289. static inline int mmu_notifier_test_young(struct mm_struct *mm,
  290. unsigned long address)
  291. {
  292. return 0;
  293. }
  294. static inline void mmu_notifier_change_pte(struct mm_struct *mm,
  295. unsigned long address, pte_t pte)
  296. {
  297. }
  298. static inline void mmu_notifier_invalidate_page(struct mm_struct *mm,
  299. unsigned long address)
  300. {
  301. }
  302. static inline void mmu_notifier_invalidate_range_start(struct mm_struct *mm,
  303. unsigned long start, unsigned long end)
  304. {
  305. }
  306. static inline void mmu_notifier_invalidate_range_end(struct mm_struct *mm,
  307. unsigned long start, unsigned long end)
  308. {
  309. }
  310. static inline void mmu_notifier_mm_init(struct mm_struct *mm)
  311. {
  312. }
  313. static inline void mmu_notifier_mm_destroy(struct mm_struct *mm)
  314. {
  315. }
  316. #define ptep_clear_flush_young_notify ptep_clear_flush_young
  317. #define pmdp_clear_flush_young_notify pmdp_clear_flush_young
  318. #define set_pte_at_notify set_pte_at
  319. #endif /* CONFIG_MMU_NOTIFIER */
  320. #endif /* _LINUX_MMU_NOTIFIER_H */