mmu_notifier.h 13 KB

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