mmu_notifier.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_MMU_NOTIFIER_H
  3. #define _LINUX_MMU_NOTIFIER_H
  4. #include <linux/types.h>
  5. #include <linux/list.h>
  6. #include <linux/spinlock.h>
  7. #include <linux/mm_types.h>
  8. #include <linux/srcu.h>
  9. struct mmu_notifier;
  10. struct mmu_notifier_ops;
  11. /* mmu_notifier_ops flags */
  12. #define MMU_INVALIDATE_DOES_NOT_BLOCK (0x01)
  13. #ifdef CONFIG_MMU_NOTIFIER
  14. /*
  15. * The mmu notifier_mm structure is allocated and installed in
  16. * mm->mmu_notifier_mm inside the mm_take_all_locks() protected
  17. * critical section and it's released only when mm_count reaches zero
  18. * in mmdrop().
  19. */
  20. struct mmu_notifier_mm {
  21. /* all mmu notifiers registerd in this mm are queued in this list */
  22. struct hlist_head list;
  23. /* to serialize the list modifications and hlist_unhashed */
  24. spinlock_t lock;
  25. };
  26. struct mmu_notifier_ops {
  27. /*
  28. * Flags to specify behavior of callbacks for this MMU notifier.
  29. * Used to determine which context an operation may be called.
  30. *
  31. * MMU_INVALIDATE_DOES_NOT_BLOCK: invalidate_range_* callbacks do not
  32. * block
  33. */
  34. int flags;
  35. /*
  36. * Called either by mmu_notifier_unregister or when the mm is
  37. * being destroyed by exit_mmap, always before all pages are
  38. * freed. This can run concurrently with other mmu notifier
  39. * methods (the ones invoked outside the mm context) and it
  40. * should tear down all secondary mmu mappings and freeze the
  41. * secondary mmu. If this method isn't implemented you've to
  42. * be sure that nothing could possibly write to the pages
  43. * through the secondary mmu by the time the last thread with
  44. * tsk->mm == mm exits.
  45. *
  46. * As side note: the pages freed after ->release returns could
  47. * be immediately reallocated by the gart at an alias physical
  48. * address with a different cache model, so if ->release isn't
  49. * implemented because all _software_ driven memory accesses
  50. * through the secondary mmu are terminated by the time the
  51. * last thread of this mm quits, you've also to be sure that
  52. * speculative _hardware_ operations can't allocate dirty
  53. * cachelines in the cpu that could not be snooped and made
  54. * coherent with the other read and write operations happening
  55. * through the gart alias address, so leading to memory
  56. * corruption.
  57. */
  58. void (*release)(struct mmu_notifier *mn,
  59. struct mm_struct *mm);
  60. /*
  61. * clear_flush_young is called after the VM is
  62. * test-and-clearing the young/accessed bitflag in the
  63. * pte. This way the VM will provide proper aging to the
  64. * accesses to the page through the secondary MMUs and not
  65. * only to the ones through the Linux pte.
  66. * Start-end is necessary in case the secondary MMU is mapping the page
  67. * at a smaller granularity than the primary MMU.
  68. */
  69. int (*clear_flush_young)(struct mmu_notifier *mn,
  70. struct mm_struct *mm,
  71. unsigned long start,
  72. unsigned long end);
  73. /*
  74. * clear_young is a lightweight version of clear_flush_young. Like the
  75. * latter, it is supposed to test-and-clear the young/accessed bitflag
  76. * in the secondary pte, but it may omit flushing the secondary tlb.
  77. */
  78. int (*clear_young)(struct mmu_notifier *mn,
  79. struct mm_struct *mm,
  80. unsigned long start,
  81. unsigned long end);
  82. /*
  83. * test_young is called to check the young/accessed bitflag in
  84. * the secondary pte. This is used to know if the page is
  85. * frequently used without actually clearing the flag or tearing
  86. * down the secondary mapping on the page.
  87. */
  88. int (*test_young)(struct mmu_notifier *mn,
  89. struct mm_struct *mm,
  90. unsigned long address);
  91. /*
  92. * change_pte is called in cases that pte mapping to page is changed:
  93. * for example, when ksm remaps pte to point to a new shared page.
  94. */
  95. void (*change_pte)(struct mmu_notifier *mn,
  96. struct mm_struct *mm,
  97. unsigned long address,
  98. pte_t pte);
  99. /*
  100. * invalidate_range_start() and invalidate_range_end() must be
  101. * paired and are called only when the mmap_sem and/or the
  102. * locks protecting the reverse maps are held. If the subsystem
  103. * can't guarantee that no additional references are taken to
  104. * the pages in the range, it has to implement the
  105. * invalidate_range() notifier to remove any references taken
  106. * after invalidate_range_start().
  107. *
  108. * Invalidation of multiple concurrent ranges may be
  109. * optionally permitted by the driver. Either way the
  110. * establishment of sptes is forbidden in the range passed to
  111. * invalidate_range_begin/end for the whole duration of the
  112. * invalidate_range_begin/end critical section.
  113. *
  114. * invalidate_range_start() is called when all pages in the
  115. * range are still mapped and have at least a refcount of one.
  116. *
  117. * invalidate_range_end() is called when all pages in the
  118. * range have been unmapped and the pages have been freed by
  119. * the VM.
  120. *
  121. * The VM will remove the page table entries and potentially
  122. * the page between invalidate_range_start() and
  123. * invalidate_range_end(). If the page must not be freed
  124. * because of pending I/O or other circumstances then the
  125. * invalidate_range_start() callback (or the initial mapping
  126. * by the driver) must make sure that the refcount is kept
  127. * elevated.
  128. *
  129. * If the driver increases the refcount when the pages are
  130. * initially mapped into an address space then either
  131. * invalidate_range_start() or invalidate_range_end() may
  132. * decrease the refcount. If the refcount is decreased on
  133. * invalidate_range_start() then the VM can free pages as page
  134. * table entries are removed. If the refcount is only
  135. * droppped on invalidate_range_end() then the driver itself
  136. * will drop the last refcount but it must take care to flush
  137. * any secondary tlb before doing the final free on the
  138. * page. Pages will no longer be referenced by the linux
  139. * address space but may still be referenced by sptes until
  140. * the last refcount is dropped.
  141. *
  142. * If both of these callbacks cannot block, and invalidate_range
  143. * cannot block, mmu_notifier_ops.flags should have
  144. * MMU_INVALIDATE_DOES_NOT_BLOCK set.
  145. */
  146. void (*invalidate_range_start)(struct mmu_notifier *mn,
  147. struct mm_struct *mm,
  148. unsigned long start, unsigned long end);
  149. void (*invalidate_range_end)(struct mmu_notifier *mn,
  150. struct mm_struct *mm,
  151. unsigned long start, unsigned long end);
  152. /*
  153. * invalidate_range() is either called between
  154. * invalidate_range_start() and invalidate_range_end() when the
  155. * VM has to free pages that where unmapped, but before the
  156. * pages are actually freed, or outside of _start()/_end() when
  157. * a (remote) TLB is necessary.
  158. *
  159. * If invalidate_range() is used to manage a non-CPU TLB with
  160. * shared page-tables, it not necessary to implement the
  161. * invalidate_range_start()/end() notifiers, as
  162. * invalidate_range() alread catches the points in time when an
  163. * external TLB range needs to be flushed. For more in depth
  164. * discussion on this see Documentation/vm/mmu_notifier.txt
  165. *
  166. * Note that this function might be called with just a sub-range
  167. * of what was passed to invalidate_range_start()/end(), if
  168. * called between those functions.
  169. *
  170. * If this callback cannot block, and invalidate_range_{start,end}
  171. * cannot block, mmu_notifier_ops.flags should have
  172. * MMU_INVALIDATE_DOES_NOT_BLOCK set.
  173. */
  174. void (*invalidate_range)(struct mmu_notifier *mn, struct mm_struct *mm,
  175. unsigned long start, unsigned long end);
  176. };
  177. /*
  178. * The notifier chains are protected by mmap_sem and/or the reverse map
  179. * semaphores. Notifier chains are only changed when all reverse maps and
  180. * the mmap_sem locks are taken.
  181. *
  182. * Therefore notifier chains can only be traversed when either
  183. *
  184. * 1. mmap_sem is held.
  185. * 2. One of the reverse map locks is held (i_mmap_rwsem or anon_vma->rwsem).
  186. * 3. No other concurrent thread can access the list (release)
  187. */
  188. struct mmu_notifier {
  189. struct hlist_node hlist;
  190. const struct mmu_notifier_ops *ops;
  191. };
  192. static inline int mm_has_notifiers(struct mm_struct *mm)
  193. {
  194. return unlikely(mm->mmu_notifier_mm);
  195. }
  196. extern int mmu_notifier_register(struct mmu_notifier *mn,
  197. struct mm_struct *mm);
  198. extern int __mmu_notifier_register(struct mmu_notifier *mn,
  199. struct mm_struct *mm);
  200. extern void mmu_notifier_unregister(struct mmu_notifier *mn,
  201. struct mm_struct *mm);
  202. extern void mmu_notifier_unregister_no_release(struct mmu_notifier *mn,
  203. struct mm_struct *mm);
  204. extern void __mmu_notifier_mm_destroy(struct mm_struct *mm);
  205. extern void __mmu_notifier_release(struct mm_struct *mm);
  206. extern int __mmu_notifier_clear_flush_young(struct mm_struct *mm,
  207. unsigned long start,
  208. unsigned long end);
  209. extern int __mmu_notifier_clear_young(struct mm_struct *mm,
  210. unsigned long start,
  211. unsigned long end);
  212. extern int __mmu_notifier_test_young(struct mm_struct *mm,
  213. unsigned long address);
  214. extern void __mmu_notifier_change_pte(struct mm_struct *mm,
  215. unsigned long address, pte_t pte);
  216. extern void __mmu_notifier_invalidate_range_start(struct mm_struct *mm,
  217. unsigned long start, unsigned long end);
  218. extern void __mmu_notifier_invalidate_range_end(struct mm_struct *mm,
  219. unsigned long start, unsigned long end,
  220. bool only_end);
  221. extern void __mmu_notifier_invalidate_range(struct mm_struct *mm,
  222. unsigned long start, unsigned long end);
  223. extern bool mm_has_blockable_invalidate_notifiers(struct mm_struct *mm);
  224. static inline void mmu_notifier_release(struct mm_struct *mm)
  225. {
  226. if (mm_has_notifiers(mm))
  227. __mmu_notifier_release(mm);
  228. }
  229. static inline int mmu_notifier_clear_flush_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_flush_young(mm, start, end);
  235. return 0;
  236. }
  237. static inline int mmu_notifier_clear_young(struct mm_struct *mm,
  238. unsigned long start,
  239. unsigned long end)
  240. {
  241. if (mm_has_notifiers(mm))
  242. return __mmu_notifier_clear_young(mm, start, end);
  243. return 0;
  244. }
  245. static inline int mmu_notifier_test_young(struct mm_struct *mm,
  246. unsigned long address)
  247. {
  248. if (mm_has_notifiers(mm))
  249. return __mmu_notifier_test_young(mm, address);
  250. return 0;
  251. }
  252. static inline void mmu_notifier_change_pte(struct mm_struct *mm,
  253. unsigned long address, pte_t pte)
  254. {
  255. if (mm_has_notifiers(mm))
  256. __mmu_notifier_change_pte(mm, address, pte);
  257. }
  258. static inline void mmu_notifier_invalidate_range_start(struct mm_struct *mm,
  259. unsigned long start, unsigned long end)
  260. {
  261. if (mm_has_notifiers(mm))
  262. __mmu_notifier_invalidate_range_start(mm, start, end);
  263. }
  264. static inline void mmu_notifier_invalidate_range_end(struct mm_struct *mm,
  265. unsigned long start, unsigned long end)
  266. {
  267. if (mm_has_notifiers(mm))
  268. __mmu_notifier_invalidate_range_end(mm, start, end, false);
  269. }
  270. static inline void mmu_notifier_invalidate_range_only_end(struct mm_struct *mm,
  271. unsigned long start, unsigned long end)
  272. {
  273. if (mm_has_notifiers(mm))
  274. __mmu_notifier_invalidate_range_end(mm, start, end, true);
  275. }
  276. static inline void mmu_notifier_invalidate_range(struct mm_struct *mm,
  277. unsigned long start, unsigned long end)
  278. {
  279. if (mm_has_notifiers(mm))
  280. __mmu_notifier_invalidate_range(mm, start, end);
  281. }
  282. static inline void mmu_notifier_mm_init(struct mm_struct *mm)
  283. {
  284. mm->mmu_notifier_mm = NULL;
  285. }
  286. static inline void mmu_notifier_mm_destroy(struct mm_struct *mm)
  287. {
  288. if (mm_has_notifiers(mm))
  289. __mmu_notifier_mm_destroy(mm);
  290. }
  291. #define ptep_clear_flush_young_notify(__vma, __address, __ptep) \
  292. ({ \
  293. int __young; \
  294. struct vm_area_struct *___vma = __vma; \
  295. unsigned long ___address = __address; \
  296. __young = ptep_clear_flush_young(___vma, ___address, __ptep); \
  297. __young |= mmu_notifier_clear_flush_young(___vma->vm_mm, \
  298. ___address, \
  299. ___address + \
  300. PAGE_SIZE); \
  301. __young; \
  302. })
  303. #define pmdp_clear_flush_young_notify(__vma, __address, __pmdp) \
  304. ({ \
  305. int __young; \
  306. struct vm_area_struct *___vma = __vma; \
  307. unsigned long ___address = __address; \
  308. __young = pmdp_clear_flush_young(___vma, ___address, __pmdp); \
  309. __young |= mmu_notifier_clear_flush_young(___vma->vm_mm, \
  310. ___address, \
  311. ___address + \
  312. PMD_SIZE); \
  313. __young; \
  314. })
  315. #define ptep_clear_young_notify(__vma, __address, __ptep) \
  316. ({ \
  317. int __young; \
  318. struct vm_area_struct *___vma = __vma; \
  319. unsigned long ___address = __address; \
  320. __young = ptep_test_and_clear_young(___vma, ___address, __ptep);\
  321. __young |= mmu_notifier_clear_young(___vma->vm_mm, ___address, \
  322. ___address + PAGE_SIZE); \
  323. __young; \
  324. })
  325. #define pmdp_clear_young_notify(__vma, __address, __pmdp) \
  326. ({ \
  327. int __young; \
  328. struct vm_area_struct *___vma = __vma; \
  329. unsigned long ___address = __address; \
  330. __young = pmdp_test_and_clear_young(___vma, ___address, __pmdp);\
  331. __young |= mmu_notifier_clear_young(___vma->vm_mm, ___address, \
  332. ___address + PMD_SIZE); \
  333. __young; \
  334. })
  335. #define ptep_clear_flush_notify(__vma, __address, __ptep) \
  336. ({ \
  337. unsigned long ___addr = __address & PAGE_MASK; \
  338. struct mm_struct *___mm = (__vma)->vm_mm; \
  339. pte_t ___pte; \
  340. \
  341. ___pte = ptep_clear_flush(__vma, __address, __ptep); \
  342. mmu_notifier_invalidate_range(___mm, ___addr, \
  343. ___addr + PAGE_SIZE); \
  344. \
  345. ___pte; \
  346. })
  347. #define pmdp_huge_clear_flush_notify(__vma, __haddr, __pmd) \
  348. ({ \
  349. unsigned long ___haddr = __haddr & HPAGE_PMD_MASK; \
  350. struct mm_struct *___mm = (__vma)->vm_mm; \
  351. pmd_t ___pmd; \
  352. \
  353. ___pmd = pmdp_huge_clear_flush(__vma, __haddr, __pmd); \
  354. mmu_notifier_invalidate_range(___mm, ___haddr, \
  355. ___haddr + HPAGE_PMD_SIZE); \
  356. \
  357. ___pmd; \
  358. })
  359. #define pudp_huge_clear_flush_notify(__vma, __haddr, __pud) \
  360. ({ \
  361. unsigned long ___haddr = __haddr & HPAGE_PUD_MASK; \
  362. struct mm_struct *___mm = (__vma)->vm_mm; \
  363. pud_t ___pud; \
  364. \
  365. ___pud = pudp_huge_clear_flush(__vma, __haddr, __pud); \
  366. mmu_notifier_invalidate_range(___mm, ___haddr, \
  367. ___haddr + HPAGE_PUD_SIZE); \
  368. \
  369. ___pud; \
  370. })
  371. /*
  372. * set_pte_at_notify() sets the pte _after_ running the notifier.
  373. * This is safe to start by updating the secondary MMUs, because the primary MMU
  374. * pte invalidate must have already happened with a ptep_clear_flush() before
  375. * set_pte_at_notify() has been invoked. Updating the secondary MMUs first is
  376. * required when we change both the protection of the mapping from read-only to
  377. * read-write and the pfn (like during copy on write page faults). Otherwise the
  378. * old page would remain mapped readonly in the secondary MMUs after the new
  379. * page is already writable by some CPU through the primary MMU.
  380. */
  381. #define set_pte_at_notify(__mm, __address, __ptep, __pte) \
  382. ({ \
  383. struct mm_struct *___mm = __mm; \
  384. unsigned long ___address = __address; \
  385. pte_t ___pte = __pte; \
  386. \
  387. mmu_notifier_change_pte(___mm, ___address, ___pte); \
  388. set_pte_at(___mm, ___address, __ptep, ___pte); \
  389. })
  390. extern void mmu_notifier_call_srcu(struct rcu_head *rcu,
  391. void (*func)(struct rcu_head *rcu));
  392. extern void mmu_notifier_synchronize(void);
  393. #else /* CONFIG_MMU_NOTIFIER */
  394. static inline int mm_has_notifiers(struct mm_struct *mm)
  395. {
  396. return 0;
  397. }
  398. static inline void mmu_notifier_release(struct mm_struct *mm)
  399. {
  400. }
  401. static inline int mmu_notifier_clear_flush_young(struct mm_struct *mm,
  402. unsigned long start,
  403. unsigned long end)
  404. {
  405. return 0;
  406. }
  407. static inline int mmu_notifier_test_young(struct mm_struct *mm,
  408. unsigned long address)
  409. {
  410. return 0;
  411. }
  412. static inline void mmu_notifier_change_pte(struct mm_struct *mm,
  413. unsigned long address, pte_t pte)
  414. {
  415. }
  416. static inline void mmu_notifier_invalidate_range_start(struct mm_struct *mm,
  417. unsigned long start, unsigned long end)
  418. {
  419. }
  420. static inline void mmu_notifier_invalidate_range_end(struct mm_struct *mm,
  421. unsigned long start, unsigned long end)
  422. {
  423. }
  424. static inline void mmu_notifier_invalidate_range_only_end(struct mm_struct *mm,
  425. unsigned long start, unsigned long end)
  426. {
  427. }
  428. static inline void mmu_notifier_invalidate_range(struct mm_struct *mm,
  429. unsigned long start, unsigned long end)
  430. {
  431. }
  432. static inline bool mm_has_blockable_invalidate_notifiers(struct mm_struct *mm)
  433. {
  434. return false;
  435. }
  436. static inline void mmu_notifier_mm_init(struct mm_struct *mm)
  437. {
  438. }
  439. static inline void mmu_notifier_mm_destroy(struct mm_struct *mm)
  440. {
  441. }
  442. #define ptep_clear_flush_young_notify ptep_clear_flush_young
  443. #define pmdp_clear_flush_young_notify pmdp_clear_flush_young
  444. #define ptep_clear_young_notify ptep_test_and_clear_young
  445. #define pmdp_clear_young_notify pmdp_test_and_clear_young
  446. #define ptep_clear_flush_notify ptep_clear_flush
  447. #define pmdp_huge_clear_flush_notify pmdp_huge_clear_flush
  448. #define pudp_huge_clear_flush_notify pudp_huge_clear_flush
  449. #define set_pte_at_notify set_pte_at
  450. #endif /* CONFIG_MMU_NOTIFIER */
  451. #endif /* _LINUX_MMU_NOTIFIER_H */