mmu_notifier.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. * clear_young is a lightweight version of clear_flush_young. Like the
  63. * latter, it is supposed to test-and-clear the young/accessed bitflag
  64. * in the secondary pte, but it may omit flushing the secondary tlb.
  65. */
  66. int (*clear_young)(struct mmu_notifier *mn,
  67. struct mm_struct *mm,
  68. unsigned long start,
  69. unsigned long end);
  70. /*
  71. * test_young is called to check the young/accessed bitflag in
  72. * the secondary pte. This is used to know if the page is
  73. * frequently used without actually clearing the flag or tearing
  74. * down the secondary mapping on the page.
  75. */
  76. int (*test_young)(struct mmu_notifier *mn,
  77. struct mm_struct *mm,
  78. unsigned long address);
  79. /*
  80. * change_pte is called in cases that pte mapping to page is changed:
  81. * for example, when ksm remaps pte to point to a new shared page.
  82. */
  83. void (*change_pte)(struct mmu_notifier *mn,
  84. struct mm_struct *mm,
  85. unsigned long address,
  86. pte_t pte);
  87. /*
  88. * Before this is invoked any secondary MMU is still ok to
  89. * read/write to the page previously pointed to by the Linux
  90. * pte because the page hasn't been freed yet and it won't be
  91. * freed until this returns. If required set_page_dirty has to
  92. * be called internally to this method.
  93. */
  94. void (*invalidate_page)(struct mmu_notifier *mn,
  95. struct mm_struct *mm,
  96. unsigned long address);
  97. /*
  98. * invalidate_range_start() and invalidate_range_end() must be
  99. * paired and are called only when the mmap_sem and/or the
  100. * locks protecting the reverse maps are held. If the subsystem
  101. * can't guarantee that no additional references are taken to
  102. * the pages in the range, it has to implement the
  103. * invalidate_range() notifier to remove any references taken
  104. * after invalidate_range_start().
  105. *
  106. * Invalidation of multiple concurrent ranges may be
  107. * optionally permitted by the driver. Either way the
  108. * establishment of sptes is forbidden in the range passed to
  109. * invalidate_range_begin/end for the whole duration of the
  110. * invalidate_range_begin/end critical section.
  111. *
  112. * invalidate_range_start() is called when all pages in the
  113. * range are still mapped and have at least a refcount of one.
  114. *
  115. * invalidate_range_end() is called when all pages in the
  116. * range have been unmapped and the pages have been freed by
  117. * the VM.
  118. *
  119. * The VM will remove the page table entries and potentially
  120. * the page between invalidate_range_start() and
  121. * invalidate_range_end(). If the page must not be freed
  122. * because of pending I/O or other circumstances then the
  123. * invalidate_range_start() callback (or the initial mapping
  124. * by the driver) must make sure that the refcount is kept
  125. * elevated.
  126. *
  127. * If the driver increases the refcount when the pages are
  128. * initially mapped into an address space then either
  129. * invalidate_range_start() or invalidate_range_end() may
  130. * decrease the refcount. If the refcount is decreased on
  131. * invalidate_range_start() then the VM can free pages as page
  132. * table entries are removed. If the refcount is only
  133. * droppped on invalidate_range_end() then the driver itself
  134. * will drop the last refcount but it must take care to flush
  135. * any secondary tlb before doing the final free on the
  136. * page. Pages will no longer be referenced by the linux
  137. * address space but may still be referenced by sptes until
  138. * the last refcount is dropped.
  139. */
  140. void (*invalidate_range_start)(struct mmu_notifier *mn,
  141. struct mm_struct *mm,
  142. unsigned long start, unsigned long end);
  143. void (*invalidate_range_end)(struct mmu_notifier *mn,
  144. struct mm_struct *mm,
  145. unsigned long start, unsigned long end);
  146. /*
  147. * invalidate_range() is either called between
  148. * invalidate_range_start() and invalidate_range_end() when the
  149. * VM has to free pages that where unmapped, but before the
  150. * pages are actually freed, or outside of _start()/_end() when
  151. * a (remote) TLB is necessary.
  152. *
  153. * If invalidate_range() is used to manage a non-CPU TLB with
  154. * shared page-tables, it not necessary to implement the
  155. * invalidate_range_start()/end() notifiers, as
  156. * invalidate_range() alread catches the points in time when an
  157. * external TLB range needs to be flushed.
  158. *
  159. * The invalidate_range() function is called under the ptl
  160. * spin-lock and not allowed to sleep.
  161. *
  162. * Note that this function might be called with just a sub-range
  163. * of what was passed to invalidate_range_start()/end(), if
  164. * called between those functions.
  165. */
  166. void (*invalidate_range)(struct mmu_notifier *mn, struct mm_struct *mm,
  167. unsigned long start, unsigned long end);
  168. };
  169. /*
  170. * The notifier chains are protected by mmap_sem and/or the reverse map
  171. * semaphores. Notifier chains are only changed when all reverse maps and
  172. * the mmap_sem locks are taken.
  173. *
  174. * Therefore notifier chains can only be traversed when either
  175. *
  176. * 1. mmap_sem is held.
  177. * 2. One of the reverse map locks is held (i_mmap_rwsem or anon_vma->rwsem).
  178. * 3. No other concurrent thread can access the list (release)
  179. */
  180. struct mmu_notifier {
  181. struct hlist_node hlist;
  182. const struct mmu_notifier_ops *ops;
  183. };
  184. static inline int mm_has_notifiers(struct mm_struct *mm)
  185. {
  186. return unlikely(mm->mmu_notifier_mm);
  187. }
  188. extern int mmu_notifier_register(struct mmu_notifier *mn,
  189. struct mm_struct *mm);
  190. extern int __mmu_notifier_register(struct mmu_notifier *mn,
  191. struct mm_struct *mm);
  192. extern void mmu_notifier_unregister(struct mmu_notifier *mn,
  193. struct mm_struct *mm);
  194. extern void mmu_notifier_unregister_no_release(struct mmu_notifier *mn,
  195. struct mm_struct *mm);
  196. extern void __mmu_notifier_mm_destroy(struct mm_struct *mm);
  197. extern void __mmu_notifier_release(struct mm_struct *mm);
  198. extern int __mmu_notifier_clear_flush_young(struct mm_struct *mm,
  199. unsigned long start,
  200. unsigned long end);
  201. extern int __mmu_notifier_clear_young(struct mm_struct *mm,
  202. unsigned long start,
  203. unsigned long end);
  204. extern int __mmu_notifier_test_young(struct mm_struct *mm,
  205. unsigned long address);
  206. extern void __mmu_notifier_change_pte(struct mm_struct *mm,
  207. unsigned long address, pte_t pte);
  208. extern void __mmu_notifier_invalidate_page(struct mm_struct *mm,
  209. unsigned long address);
  210. extern void __mmu_notifier_invalidate_range_start(struct mm_struct *mm,
  211. unsigned long start, unsigned long end);
  212. extern void __mmu_notifier_invalidate_range_end(struct mm_struct *mm,
  213. unsigned long start, unsigned long end);
  214. extern void __mmu_notifier_invalidate_range(struct mm_struct *mm,
  215. unsigned long start, unsigned long end);
  216. static inline void mmu_notifier_release(struct mm_struct *mm)
  217. {
  218. if (mm_has_notifiers(mm))
  219. __mmu_notifier_release(mm);
  220. }
  221. static inline int mmu_notifier_clear_flush_young(struct mm_struct *mm,
  222. unsigned long start,
  223. unsigned long end)
  224. {
  225. if (mm_has_notifiers(mm))
  226. return __mmu_notifier_clear_flush_young(mm, start, end);
  227. return 0;
  228. }
  229. static inline int mmu_notifier_clear_young(struct mm_struct *mm,
  230. unsigned long start,
  231. unsigned long end)
  232. {
  233. if (mm_has_notifiers(mm))
  234. return __mmu_notifier_clear_young(mm, start, end);
  235. return 0;
  236. }
  237. static inline int mmu_notifier_test_young(struct mm_struct *mm,
  238. unsigned long address)
  239. {
  240. if (mm_has_notifiers(mm))
  241. return __mmu_notifier_test_young(mm, address);
  242. return 0;
  243. }
  244. static inline void mmu_notifier_change_pte(struct mm_struct *mm,
  245. unsigned long address, pte_t pte)
  246. {
  247. if (mm_has_notifiers(mm))
  248. __mmu_notifier_change_pte(mm, address, pte);
  249. }
  250. static inline void mmu_notifier_invalidate_page(struct mm_struct *mm,
  251. unsigned long address)
  252. {
  253. if (mm_has_notifiers(mm))
  254. __mmu_notifier_invalidate_page(mm, address);
  255. }
  256. static inline void mmu_notifier_invalidate_range_start(struct mm_struct *mm,
  257. unsigned long start, unsigned long end)
  258. {
  259. if (mm_has_notifiers(mm))
  260. __mmu_notifier_invalidate_range_start(mm, start, end);
  261. }
  262. static inline void mmu_notifier_invalidate_range_end(struct mm_struct *mm,
  263. unsigned long start, unsigned long end)
  264. {
  265. if (mm_has_notifiers(mm))
  266. __mmu_notifier_invalidate_range_end(mm, start, end);
  267. }
  268. static inline void mmu_notifier_invalidate_range(struct mm_struct *mm,
  269. unsigned long start, unsigned long end)
  270. {
  271. if (mm_has_notifiers(mm))
  272. __mmu_notifier_invalidate_range(mm, start, end);
  273. }
  274. static inline void mmu_notifier_mm_init(struct mm_struct *mm)
  275. {
  276. mm->mmu_notifier_mm = NULL;
  277. }
  278. static inline void mmu_notifier_mm_destroy(struct mm_struct *mm)
  279. {
  280. if (mm_has_notifiers(mm))
  281. __mmu_notifier_mm_destroy(mm);
  282. }
  283. #define ptep_clear_flush_young_notify(__vma, __address, __ptep) \
  284. ({ \
  285. int __young; \
  286. struct vm_area_struct *___vma = __vma; \
  287. unsigned long ___address = __address; \
  288. __young = ptep_clear_flush_young(___vma, ___address, __ptep); \
  289. __young |= mmu_notifier_clear_flush_young(___vma->vm_mm, \
  290. ___address, \
  291. ___address + \
  292. PAGE_SIZE); \
  293. __young; \
  294. })
  295. #define pmdp_clear_flush_young_notify(__vma, __address, __pmdp) \
  296. ({ \
  297. int __young; \
  298. struct vm_area_struct *___vma = __vma; \
  299. unsigned long ___address = __address; \
  300. __young = pmdp_clear_flush_young(___vma, ___address, __pmdp); \
  301. __young |= mmu_notifier_clear_flush_young(___vma->vm_mm, \
  302. ___address, \
  303. ___address + \
  304. PMD_SIZE); \
  305. __young; \
  306. })
  307. #define ptep_clear_young_notify(__vma, __address, __ptep) \
  308. ({ \
  309. int __young; \
  310. struct vm_area_struct *___vma = __vma; \
  311. unsigned long ___address = __address; \
  312. __young = ptep_test_and_clear_young(___vma, ___address, __ptep);\
  313. __young |= mmu_notifier_clear_young(___vma->vm_mm, ___address, \
  314. ___address + PAGE_SIZE); \
  315. __young; \
  316. })
  317. #define pmdp_clear_young_notify(__vma, __address, __pmdp) \
  318. ({ \
  319. int __young; \
  320. struct vm_area_struct *___vma = __vma; \
  321. unsigned long ___address = __address; \
  322. __young = pmdp_test_and_clear_young(___vma, ___address, __pmdp);\
  323. __young |= mmu_notifier_clear_young(___vma->vm_mm, ___address, \
  324. ___address + PMD_SIZE); \
  325. __young; \
  326. })
  327. #define ptep_clear_flush_notify(__vma, __address, __ptep) \
  328. ({ \
  329. unsigned long ___addr = __address & PAGE_MASK; \
  330. struct mm_struct *___mm = (__vma)->vm_mm; \
  331. pte_t ___pte; \
  332. \
  333. ___pte = ptep_clear_flush(__vma, __address, __ptep); \
  334. mmu_notifier_invalidate_range(___mm, ___addr, \
  335. ___addr + PAGE_SIZE); \
  336. \
  337. ___pte; \
  338. })
  339. #define pmdp_huge_clear_flush_notify(__vma, __haddr, __pmd) \
  340. ({ \
  341. unsigned long ___haddr = __haddr & HPAGE_PMD_MASK; \
  342. struct mm_struct *___mm = (__vma)->vm_mm; \
  343. pmd_t ___pmd; \
  344. \
  345. ___pmd = pmdp_huge_clear_flush(__vma, __haddr, __pmd); \
  346. mmu_notifier_invalidate_range(___mm, ___haddr, \
  347. ___haddr + HPAGE_PMD_SIZE); \
  348. \
  349. ___pmd; \
  350. })
  351. /*
  352. * set_pte_at_notify() sets the pte _after_ running the notifier.
  353. * This is safe to start by updating the secondary MMUs, because the primary MMU
  354. * pte invalidate must have already happened with a ptep_clear_flush() before
  355. * set_pte_at_notify() has been invoked. Updating the secondary MMUs first is
  356. * required when we change both the protection of the mapping from read-only to
  357. * read-write and the pfn (like during copy on write page faults). Otherwise the
  358. * old page would remain mapped readonly in the secondary MMUs after the new
  359. * page is already writable by some CPU through the primary MMU.
  360. */
  361. #define set_pte_at_notify(__mm, __address, __ptep, __pte) \
  362. ({ \
  363. struct mm_struct *___mm = __mm; \
  364. unsigned long ___address = __address; \
  365. pte_t ___pte = __pte; \
  366. \
  367. mmu_notifier_change_pte(___mm, ___address, ___pte); \
  368. set_pte_at(___mm, ___address, __ptep, ___pte); \
  369. })
  370. extern void mmu_notifier_call_srcu(struct rcu_head *rcu,
  371. void (*func)(struct rcu_head *rcu));
  372. extern void mmu_notifier_synchronize(void);
  373. #else /* CONFIG_MMU_NOTIFIER */
  374. static inline void mmu_notifier_release(struct mm_struct *mm)
  375. {
  376. }
  377. static inline int mmu_notifier_clear_flush_young(struct mm_struct *mm,
  378. unsigned long start,
  379. unsigned long end)
  380. {
  381. return 0;
  382. }
  383. static inline int mmu_notifier_test_young(struct mm_struct *mm,
  384. unsigned long address)
  385. {
  386. return 0;
  387. }
  388. static inline void mmu_notifier_change_pte(struct mm_struct *mm,
  389. unsigned long address, pte_t pte)
  390. {
  391. }
  392. static inline void mmu_notifier_invalidate_page(struct mm_struct *mm,
  393. unsigned long address)
  394. {
  395. }
  396. static inline void mmu_notifier_invalidate_range_start(struct mm_struct *mm,
  397. unsigned long start, unsigned long end)
  398. {
  399. }
  400. static inline void mmu_notifier_invalidate_range_end(struct mm_struct *mm,
  401. unsigned long start, unsigned long end)
  402. {
  403. }
  404. static inline void mmu_notifier_invalidate_range(struct mm_struct *mm,
  405. unsigned long start, unsigned long end)
  406. {
  407. }
  408. static inline void mmu_notifier_mm_init(struct mm_struct *mm)
  409. {
  410. }
  411. static inline void mmu_notifier_mm_destroy(struct mm_struct *mm)
  412. {
  413. }
  414. #define ptep_clear_flush_young_notify ptep_clear_flush_young
  415. #define pmdp_clear_flush_young_notify pmdp_clear_flush_young
  416. #define ptep_clear_young_notify ptep_test_and_clear_young
  417. #define pmdp_clear_young_notify pmdp_test_and_clear_young
  418. #define ptep_clear_flush_notify ptep_clear_flush
  419. #define pmdp_huge_clear_flush_notify pmdp_huge_clear_flush
  420. #define set_pte_at_notify set_pte_at
  421. #endif /* CONFIG_MMU_NOTIFIER */
  422. #endif /* _LINUX_MMU_NOTIFIER_H */