mmu_notifier.h 16 KB

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