mmu_notifier.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  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/mmap_lock.h>
  8. #include <linux/srcu.h>
  9. #include <linux/interval_tree.h>
  10. #include <linux/android_kabi.h>
  11. struct mmu_notifier_subscriptions;
  12. struct mmu_notifier;
  13. struct mmu_notifier_range;
  14. struct mmu_interval_notifier;
  15. /**
  16. * enum mmu_notifier_event - reason for the mmu notifier callback
  17. * @MMU_NOTIFY_UNMAP: either munmap() that unmap the range or a mremap() that
  18. * move the range
  19. *
  20. * @MMU_NOTIFY_CLEAR: clear page table entry (many reasons for this like
  21. * madvise() or replacing a page by another one, ...).
  22. *
  23. * @MMU_NOTIFY_PROTECTION_VMA: update is due to protection change for the range
  24. * ie using the vma access permission (vm_page_prot) to update the whole range
  25. * is enough no need to inspect changes to the CPU page table (mprotect()
  26. * syscall)
  27. *
  28. * @MMU_NOTIFY_PROTECTION_PAGE: update is due to change in read/write flag for
  29. * pages in the range so to mirror those changes the user must inspect the CPU
  30. * page table (from the end callback).
  31. *
  32. * @MMU_NOTIFY_SOFT_DIRTY: soft dirty accounting (still same page and same
  33. * access flags). User should soft dirty the page in the end callback to make
  34. * sure that anyone relying on soft dirtiness catch pages that might be written
  35. * through non CPU mappings.
  36. *
  37. * @MMU_NOTIFY_RELEASE: used during mmu_interval_notifier invalidate to signal
  38. * that the mm refcount is zero and the range is no longer accessible.
  39. *
  40. * @MMU_NOTIFY_MIGRATE: used during migrate_vma_collect() invalidate to signal
  41. * a device driver to possibly ignore the invalidation if the
  42. * owner field matches the driver's device private pgmap owner.
  43. *
  44. * @MMU_NOTIFY_EXCLUSIVE: to signal a device driver that the device will no
  45. * longer have exclusive access to the page. When sent during creation of an
  46. * exclusive range the owner will be initialised to the value provided by the
  47. * caller of make_device_exclusive_range(), otherwise the owner will be NULL.
  48. */
  49. enum mmu_notifier_event {
  50. MMU_NOTIFY_UNMAP = 0,
  51. MMU_NOTIFY_CLEAR,
  52. MMU_NOTIFY_PROTECTION_VMA,
  53. MMU_NOTIFY_PROTECTION_PAGE,
  54. MMU_NOTIFY_SOFT_DIRTY,
  55. MMU_NOTIFY_RELEASE,
  56. MMU_NOTIFY_MIGRATE,
  57. MMU_NOTIFY_EXCLUSIVE,
  58. };
  59. #define MMU_NOTIFIER_RANGE_BLOCKABLE (1 << 0)
  60. struct mmu_notifier_ops {
  61. /*
  62. * Called either by mmu_notifier_unregister or when the mm is
  63. * being destroyed by exit_mmap, always before all pages are
  64. * freed. This can run concurrently with other mmu notifier
  65. * methods (the ones invoked outside the mm context) and it
  66. * should tear down all secondary mmu mappings and freeze the
  67. * secondary mmu. If this method isn't implemented you've to
  68. * be sure that nothing could possibly write to the pages
  69. * through the secondary mmu by the time the last thread with
  70. * tsk->mm == mm exits.
  71. *
  72. * As side note: the pages freed after ->release returns could
  73. * be immediately reallocated by the gart at an alias physical
  74. * address with a different cache model, so if ->release isn't
  75. * implemented because all _software_ driven memory accesses
  76. * through the secondary mmu are terminated by the time the
  77. * last thread of this mm quits, you've also to be sure that
  78. * speculative _hardware_ operations can't allocate dirty
  79. * cachelines in the cpu that could not be snooped and made
  80. * coherent with the other read and write operations happening
  81. * through the gart alias address, so leading to memory
  82. * corruption.
  83. */
  84. void (*release)(struct mmu_notifier *subscription,
  85. struct mm_struct *mm);
  86. /*
  87. * clear_flush_young is called after the VM is
  88. * test-and-clearing the young/accessed bitflag in the
  89. * pte. This way the VM will provide proper aging to the
  90. * accesses to the page through the secondary MMUs and not
  91. * only to the ones through the Linux pte.
  92. * Start-end is necessary in case the secondary MMU is mapping the page
  93. * at a smaller granularity than the primary MMU.
  94. */
  95. int (*clear_flush_young)(struct mmu_notifier *subscription,
  96. struct mm_struct *mm,
  97. unsigned long start,
  98. unsigned long end);
  99. /*
  100. * clear_young is a lightweight version of clear_flush_young. Like the
  101. * latter, it is supposed to test-and-clear the young/accessed bitflag
  102. * in the secondary pte, but it may omit flushing the secondary tlb.
  103. */
  104. int (*clear_young)(struct mmu_notifier *subscription,
  105. struct mm_struct *mm,
  106. unsigned long start,
  107. unsigned long end);
  108. /*
  109. * test_young is called to check the young/accessed bitflag in
  110. * the secondary pte. This is used to know if the page is
  111. * frequently used without actually clearing the flag or tearing
  112. * down the secondary mapping on the page.
  113. */
  114. int (*test_young)(struct mmu_notifier *subscription,
  115. struct mm_struct *mm,
  116. unsigned long address);
  117. /*
  118. * change_pte is called in cases that pte mapping to page is changed:
  119. * for example, when ksm remaps pte to point to a new shared page.
  120. */
  121. void (*change_pte)(struct mmu_notifier *subscription,
  122. struct mm_struct *mm,
  123. unsigned long address,
  124. pte_t pte);
  125. /*
  126. * invalidate_range_start() and invalidate_range_end() must be
  127. * paired and are called only when the mmap_lock and/or the
  128. * locks protecting the reverse maps are held. If the subsystem
  129. * can't guarantee that no additional references are taken to
  130. * the pages in the range, it has to implement the
  131. * invalidate_range() notifier to remove any references taken
  132. * after invalidate_range_start().
  133. *
  134. * Invalidation of multiple concurrent ranges may be
  135. * optionally permitted by the driver. Either way the
  136. * establishment of sptes is forbidden in the range passed to
  137. * invalidate_range_begin/end for the whole duration of the
  138. * invalidate_range_begin/end critical section.
  139. *
  140. * invalidate_range_start() is called when all pages in the
  141. * range are still mapped and have at least a refcount of one.
  142. *
  143. * invalidate_range_end() is called when all pages in the
  144. * range have been unmapped and the pages have been freed by
  145. * the VM.
  146. *
  147. * The VM will remove the page table entries and potentially
  148. * the page between invalidate_range_start() and
  149. * invalidate_range_end(). If the page must not be freed
  150. * because of pending I/O or other circumstances then the
  151. * invalidate_range_start() callback (or the initial mapping
  152. * by the driver) must make sure that the refcount is kept
  153. * elevated.
  154. *
  155. * If the driver increases the refcount when the pages are
  156. * initially mapped into an address space then either
  157. * invalidate_range_start() or invalidate_range_end() may
  158. * decrease the refcount. If the refcount is decreased on
  159. * invalidate_range_start() then the VM can free pages as page
  160. * table entries are removed. If the refcount is only
  161. * dropped on invalidate_range_end() then the driver itself
  162. * will drop the last refcount but it must take care to flush
  163. * any secondary tlb before doing the final free on the
  164. * page. Pages will no longer be referenced by the linux
  165. * address space but may still be referenced by sptes until
  166. * the last refcount is dropped.
  167. *
  168. * If blockable argument is set to false then the callback cannot
  169. * sleep and has to return with -EAGAIN if sleeping would be required.
  170. * 0 should be returned otherwise. Please note that notifiers that can
  171. * fail invalidate_range_start are not allowed to implement
  172. * invalidate_range_end, as there is no mechanism for informing the
  173. * notifier that its start failed.
  174. */
  175. int (*invalidate_range_start)(struct mmu_notifier *subscription,
  176. const struct mmu_notifier_range *range);
  177. void (*invalidate_range_end)(struct mmu_notifier *subscription,
  178. const struct mmu_notifier_range *range);
  179. /*
  180. * invalidate_range() is either called between
  181. * invalidate_range_start() and invalidate_range_end() when the
  182. * VM has to free pages that where unmapped, but before the
  183. * pages are actually freed, or outside of _start()/_end() when
  184. * a (remote) TLB is necessary.
  185. *
  186. * If invalidate_range() is used to manage a non-CPU TLB with
  187. * shared page-tables, it not necessary to implement the
  188. * invalidate_range_start()/end() notifiers, as
  189. * invalidate_range() already catches the points in time when an
  190. * external TLB range needs to be flushed. For more in depth
  191. * discussion on this see Documentation/mm/mmu_notifier.rst
  192. *
  193. * Note that this function might be called with just a sub-range
  194. * of what was passed to invalidate_range_start()/end(), if
  195. * called between those functions.
  196. */
  197. void (*invalidate_range)(struct mmu_notifier *subscription,
  198. struct mm_struct *mm,
  199. unsigned long start,
  200. unsigned long end);
  201. /*
  202. * These callbacks are used with the get/put interface to manage the
  203. * lifetime of the mmu_notifier memory. alloc_notifier() returns a new
  204. * notifier for use with the mm.
  205. *
  206. * free_notifier() is only called after the mmu_notifier has been
  207. * fully put, calls to any ops callback are prevented and no ops
  208. * callbacks are currently running. It is called from a SRCU callback
  209. * and cannot sleep.
  210. */
  211. struct mmu_notifier *(*alloc_notifier)(struct mm_struct *mm);
  212. void (*free_notifier)(struct mmu_notifier *subscription);
  213. ANDROID_KABI_RESERVE(1);
  214. ANDROID_KABI_RESERVE(2);
  215. ANDROID_KABI_RESERVE(3);
  216. ANDROID_KABI_RESERVE(4);
  217. };
  218. /*
  219. * The notifier chains are protected by mmap_lock and/or the reverse map
  220. * semaphores. Notifier chains are only changed when all reverse maps and
  221. * the mmap_lock locks are taken.
  222. *
  223. * Therefore notifier chains can only be traversed when either
  224. *
  225. * 1. mmap_lock is held.
  226. * 2. One of the reverse map locks is held (i_mmap_rwsem or anon_vma->rwsem).
  227. * 3. No other concurrent thread can access the list (release)
  228. */
  229. struct mmu_notifier {
  230. struct hlist_node hlist;
  231. const struct mmu_notifier_ops *ops;
  232. struct mm_struct *mm;
  233. struct rcu_head rcu;
  234. unsigned int users;
  235. ANDROID_KABI_RESERVE(1);
  236. ANDROID_KABI_RESERVE(2);
  237. };
  238. /**
  239. * struct mmu_interval_notifier_ops
  240. * @invalidate: Upon return the caller must stop using any SPTEs within this
  241. * range. This function can sleep. Return false only if sleeping
  242. * was required but mmu_notifier_range_blockable(range) is false.
  243. */
  244. struct mmu_interval_notifier_ops {
  245. bool (*invalidate)(struct mmu_interval_notifier *interval_sub,
  246. const struct mmu_notifier_range *range,
  247. unsigned long cur_seq);
  248. };
  249. struct mmu_interval_notifier {
  250. struct interval_tree_node interval_tree;
  251. const struct mmu_interval_notifier_ops *ops;
  252. struct mm_struct *mm;
  253. struct hlist_node deferred_item;
  254. unsigned long invalidate_seq;
  255. };
  256. #ifdef CONFIG_MMU_NOTIFIER
  257. #ifdef CONFIG_LOCKDEP
  258. extern struct lockdep_map __mmu_notifier_invalidate_range_start_map;
  259. #endif
  260. struct mmu_notifier_range {
  261. struct vm_area_struct *vma;
  262. struct mm_struct *mm;
  263. unsigned long start;
  264. unsigned long end;
  265. unsigned flags;
  266. enum mmu_notifier_event event;
  267. void *owner;
  268. };
  269. static inline int mm_has_notifiers(struct mm_struct *mm)
  270. {
  271. return unlikely(mm->notifier_subscriptions);
  272. }
  273. struct mmu_notifier *mmu_notifier_get_locked(const struct mmu_notifier_ops *ops,
  274. struct mm_struct *mm);
  275. static inline struct mmu_notifier *
  276. mmu_notifier_get(const struct mmu_notifier_ops *ops, struct mm_struct *mm)
  277. {
  278. struct mmu_notifier *ret;
  279. mmap_write_lock(mm);
  280. ret = mmu_notifier_get_locked(ops, mm);
  281. mmap_write_unlock(mm);
  282. return ret;
  283. }
  284. void mmu_notifier_put(struct mmu_notifier *subscription);
  285. void mmu_notifier_synchronize(void);
  286. extern int mmu_notifier_register(struct mmu_notifier *subscription,
  287. struct mm_struct *mm);
  288. extern int __mmu_notifier_register(struct mmu_notifier *subscription,
  289. struct mm_struct *mm);
  290. extern void mmu_notifier_unregister(struct mmu_notifier *subscription,
  291. struct mm_struct *mm);
  292. unsigned long
  293. mmu_interval_read_begin(struct mmu_interval_notifier *interval_sub);
  294. int mmu_interval_notifier_insert(struct mmu_interval_notifier *interval_sub,
  295. struct mm_struct *mm, unsigned long start,
  296. unsigned long length,
  297. const struct mmu_interval_notifier_ops *ops);
  298. int mmu_interval_notifier_insert_locked(
  299. struct mmu_interval_notifier *interval_sub, struct mm_struct *mm,
  300. unsigned long start, unsigned long length,
  301. const struct mmu_interval_notifier_ops *ops);
  302. void mmu_interval_notifier_remove(struct mmu_interval_notifier *interval_sub);
  303. /**
  304. * mmu_interval_set_seq - Save the invalidation sequence
  305. * @interval_sub - The subscription passed to invalidate
  306. * @cur_seq - The cur_seq passed to the invalidate() callback
  307. *
  308. * This must be called unconditionally from the invalidate callback of a
  309. * struct mmu_interval_notifier_ops under the same lock that is used to call
  310. * mmu_interval_read_retry(). It updates the sequence number for later use by
  311. * mmu_interval_read_retry(). The provided cur_seq will always be odd.
  312. *
  313. * If the caller does not call mmu_interval_read_begin() or
  314. * mmu_interval_read_retry() then this call is not required.
  315. */
  316. static inline void
  317. mmu_interval_set_seq(struct mmu_interval_notifier *interval_sub,
  318. unsigned long cur_seq)
  319. {
  320. WRITE_ONCE(interval_sub->invalidate_seq, cur_seq);
  321. }
  322. /**
  323. * mmu_interval_read_retry - End a read side critical section against a VA range
  324. * interval_sub: The subscription
  325. * seq: The return of the paired mmu_interval_read_begin()
  326. *
  327. * This MUST be called under a user provided lock that is also held
  328. * unconditionally by op->invalidate() when it calls mmu_interval_set_seq().
  329. *
  330. * Each call should be paired with a single mmu_interval_read_begin() and
  331. * should be used to conclude the read side.
  332. *
  333. * Returns true if an invalidation collided with this critical section, and
  334. * the caller should retry.
  335. */
  336. static inline bool
  337. mmu_interval_read_retry(struct mmu_interval_notifier *interval_sub,
  338. unsigned long seq)
  339. {
  340. return interval_sub->invalidate_seq != seq;
  341. }
  342. /**
  343. * mmu_interval_check_retry - Test if a collision has occurred
  344. * interval_sub: The subscription
  345. * seq: The return of the matching mmu_interval_read_begin()
  346. *
  347. * This can be used in the critical section between mmu_interval_read_begin()
  348. * and mmu_interval_read_retry(). A return of true indicates an invalidation
  349. * has collided with this critical region and a future
  350. * mmu_interval_read_retry() will return true.
  351. *
  352. * False is not reliable and only suggests a collision may not have
  353. * occurred. It can be called many times and does not have to hold the user
  354. * provided lock.
  355. *
  356. * This call can be used as part of loops and other expensive operations to
  357. * expedite a retry.
  358. */
  359. static inline bool
  360. mmu_interval_check_retry(struct mmu_interval_notifier *interval_sub,
  361. unsigned long seq)
  362. {
  363. /* Pairs with the WRITE_ONCE in mmu_interval_set_seq() */
  364. return READ_ONCE(interval_sub->invalidate_seq) != seq;
  365. }
  366. extern void __mmu_notifier_subscriptions_destroy(struct mm_struct *mm);
  367. extern void __mmu_notifier_release(struct mm_struct *mm);
  368. extern int __mmu_notifier_clear_flush_young(struct mm_struct *mm,
  369. unsigned long start,
  370. unsigned long end);
  371. extern int __mmu_notifier_clear_young(struct mm_struct *mm,
  372. unsigned long start,
  373. unsigned long end);
  374. extern int __mmu_notifier_test_young(struct mm_struct *mm,
  375. unsigned long address);
  376. extern void __mmu_notifier_change_pte(struct mm_struct *mm,
  377. unsigned long address, pte_t pte);
  378. extern int __mmu_notifier_invalidate_range_start(struct mmu_notifier_range *r);
  379. extern void __mmu_notifier_invalidate_range_end(struct mmu_notifier_range *r,
  380. bool only_end);
  381. extern void __mmu_notifier_invalidate_range(struct mm_struct *mm,
  382. unsigned long start, unsigned long end);
  383. extern bool
  384. mmu_notifier_range_update_to_read_only(const struct mmu_notifier_range *range);
  385. static inline bool
  386. mmu_notifier_range_blockable(const struct mmu_notifier_range *range)
  387. {
  388. return (range->flags & MMU_NOTIFIER_RANGE_BLOCKABLE);
  389. }
  390. static inline void mmu_notifier_release(struct mm_struct *mm)
  391. {
  392. if (mm_has_notifiers(mm))
  393. __mmu_notifier_release(mm);
  394. }
  395. static inline int mmu_notifier_clear_flush_young(struct mm_struct *mm,
  396. unsigned long start,
  397. unsigned long end)
  398. {
  399. if (mm_has_notifiers(mm))
  400. return __mmu_notifier_clear_flush_young(mm, start, end);
  401. return 0;
  402. }
  403. static inline int mmu_notifier_clear_young(struct mm_struct *mm,
  404. unsigned long start,
  405. unsigned long end)
  406. {
  407. if (mm_has_notifiers(mm))
  408. return __mmu_notifier_clear_young(mm, start, end);
  409. return 0;
  410. }
  411. static inline int mmu_notifier_test_young(struct mm_struct *mm,
  412. unsigned long address)
  413. {
  414. if (mm_has_notifiers(mm))
  415. return __mmu_notifier_test_young(mm, address);
  416. return 0;
  417. }
  418. static inline void mmu_notifier_change_pte(struct mm_struct *mm,
  419. unsigned long address, pte_t pte)
  420. {
  421. if (mm_has_notifiers(mm))
  422. __mmu_notifier_change_pte(mm, address, pte);
  423. }
  424. static inline void
  425. mmu_notifier_invalidate_range_start(struct mmu_notifier_range *range)
  426. {
  427. might_sleep();
  428. lock_map_acquire(&__mmu_notifier_invalidate_range_start_map);
  429. if (mm_has_notifiers(range->mm)) {
  430. range->flags |= MMU_NOTIFIER_RANGE_BLOCKABLE;
  431. __mmu_notifier_invalidate_range_start(range);
  432. }
  433. lock_map_release(&__mmu_notifier_invalidate_range_start_map);
  434. }
  435. static inline int
  436. mmu_notifier_invalidate_range_start_nonblock(struct mmu_notifier_range *range)
  437. {
  438. int ret = 0;
  439. lock_map_acquire(&__mmu_notifier_invalidate_range_start_map);
  440. if (mm_has_notifiers(range->mm)) {
  441. range->flags &= ~MMU_NOTIFIER_RANGE_BLOCKABLE;
  442. ret = __mmu_notifier_invalidate_range_start(range);
  443. }
  444. lock_map_release(&__mmu_notifier_invalidate_range_start_map);
  445. return ret;
  446. }
  447. static inline void
  448. mmu_notifier_invalidate_range_end(struct mmu_notifier_range *range)
  449. {
  450. if (mmu_notifier_range_blockable(range))
  451. might_sleep();
  452. if (mm_has_notifiers(range->mm))
  453. __mmu_notifier_invalidate_range_end(range, false);
  454. }
  455. static inline void
  456. mmu_notifier_invalidate_range_only_end(struct mmu_notifier_range *range)
  457. {
  458. if (mm_has_notifiers(range->mm))
  459. __mmu_notifier_invalidate_range_end(range, true);
  460. }
  461. static inline void mmu_notifier_invalidate_range(struct mm_struct *mm,
  462. unsigned long start, unsigned long end)
  463. {
  464. if (mm_has_notifiers(mm))
  465. __mmu_notifier_invalidate_range(mm, start, end);
  466. }
  467. static inline void mmu_notifier_subscriptions_init(struct mm_struct *mm)
  468. {
  469. mm->notifier_subscriptions = NULL;
  470. }
  471. static inline void mmu_notifier_subscriptions_destroy(struct mm_struct *mm)
  472. {
  473. if (mm_has_notifiers(mm))
  474. __mmu_notifier_subscriptions_destroy(mm);
  475. }
  476. static inline void mmu_notifier_range_init(struct mmu_notifier_range *range,
  477. enum mmu_notifier_event event,
  478. unsigned flags,
  479. struct vm_area_struct *vma,
  480. struct mm_struct *mm,
  481. unsigned long start,
  482. unsigned long end)
  483. {
  484. range->vma = vma;
  485. range->event = event;
  486. range->mm = mm;
  487. range->start = start;
  488. range->end = end;
  489. range->flags = flags;
  490. }
  491. static inline void mmu_notifier_range_init_owner(
  492. struct mmu_notifier_range *range,
  493. enum mmu_notifier_event event, unsigned int flags,
  494. struct vm_area_struct *vma, struct mm_struct *mm,
  495. unsigned long start, unsigned long end, void *owner)
  496. {
  497. mmu_notifier_range_init(range, event, flags, vma, mm, start, end);
  498. range->owner = owner;
  499. }
  500. #define ptep_clear_flush_young_notify(__vma, __address, __ptep) \
  501. ({ \
  502. int __young; \
  503. struct vm_area_struct *___vma = __vma; \
  504. unsigned long ___address = __address; \
  505. __young = ptep_clear_flush_young(___vma, ___address, __ptep); \
  506. __young |= mmu_notifier_clear_flush_young(___vma->vm_mm, \
  507. ___address, \
  508. ___address + \
  509. PAGE_SIZE); \
  510. __young; \
  511. })
  512. #define pmdp_clear_flush_young_notify(__vma, __address, __pmdp) \
  513. ({ \
  514. int __young; \
  515. struct vm_area_struct *___vma = __vma; \
  516. unsigned long ___address = __address; \
  517. __young = pmdp_clear_flush_young(___vma, ___address, __pmdp); \
  518. __young |= mmu_notifier_clear_flush_young(___vma->vm_mm, \
  519. ___address, \
  520. ___address + \
  521. PMD_SIZE); \
  522. __young; \
  523. })
  524. #define ptep_clear_young_notify(__vma, __address, __ptep) \
  525. ({ \
  526. int __young; \
  527. struct vm_area_struct *___vma = __vma; \
  528. unsigned long ___address = __address; \
  529. __young = ptep_test_and_clear_young(___vma, ___address, __ptep);\
  530. __young |= mmu_notifier_clear_young(___vma->vm_mm, ___address, \
  531. ___address + PAGE_SIZE); \
  532. __young; \
  533. })
  534. #define pmdp_clear_young_notify(__vma, __address, __pmdp) \
  535. ({ \
  536. int __young; \
  537. struct vm_area_struct *___vma = __vma; \
  538. unsigned long ___address = __address; \
  539. __young = pmdp_test_and_clear_young(___vma, ___address, __pmdp);\
  540. __young |= mmu_notifier_clear_young(___vma->vm_mm, ___address, \
  541. ___address + PMD_SIZE); \
  542. __young; \
  543. })
  544. #define ptep_clear_flush_notify(__vma, __address, __ptep) \
  545. ({ \
  546. unsigned long ___addr = __address & PAGE_MASK; \
  547. struct mm_struct *___mm = (__vma)->vm_mm; \
  548. pte_t ___pte; \
  549. \
  550. ___pte = ptep_clear_flush(__vma, __address, __ptep); \
  551. mmu_notifier_invalidate_range(___mm, ___addr, \
  552. ___addr + PAGE_SIZE); \
  553. \
  554. ___pte; \
  555. })
  556. #define pmdp_huge_clear_flush_notify(__vma, __haddr, __pmd) \
  557. ({ \
  558. unsigned long ___haddr = __haddr & HPAGE_PMD_MASK; \
  559. struct mm_struct *___mm = (__vma)->vm_mm; \
  560. pmd_t ___pmd; \
  561. \
  562. ___pmd = pmdp_huge_clear_flush(__vma, __haddr, __pmd); \
  563. mmu_notifier_invalidate_range(___mm, ___haddr, \
  564. ___haddr + HPAGE_PMD_SIZE); \
  565. \
  566. ___pmd; \
  567. })
  568. #define pudp_huge_clear_flush_notify(__vma, __haddr, __pud) \
  569. ({ \
  570. unsigned long ___haddr = __haddr & HPAGE_PUD_MASK; \
  571. struct mm_struct *___mm = (__vma)->vm_mm; \
  572. pud_t ___pud; \
  573. \
  574. ___pud = pudp_huge_clear_flush(__vma, __haddr, __pud); \
  575. mmu_notifier_invalidate_range(___mm, ___haddr, \
  576. ___haddr + HPAGE_PUD_SIZE); \
  577. \
  578. ___pud; \
  579. })
  580. /*
  581. * set_pte_at_notify() sets the pte _after_ running the notifier.
  582. * This is safe to start by updating the secondary MMUs, because the primary MMU
  583. * pte invalidate must have already happened with a ptep_clear_flush() before
  584. * set_pte_at_notify() has been invoked. Updating the secondary MMUs first is
  585. * required when we change both the protection of the mapping from read-only to
  586. * read-write and the pfn (like during copy on write page faults). Otherwise the
  587. * old page would remain mapped readonly in the secondary MMUs after the new
  588. * page is already writable by some CPU through the primary MMU.
  589. */
  590. #define set_pte_at_notify(__mm, __address, __ptep, __pte) \
  591. ({ \
  592. struct mm_struct *___mm = __mm; \
  593. unsigned long ___address = __address; \
  594. pte_t ___pte = __pte; \
  595. \
  596. mmu_notifier_change_pte(___mm, ___address, ___pte); \
  597. set_pte_at(___mm, ___address, __ptep, ___pte); \
  598. })
  599. #else /* CONFIG_MMU_NOTIFIER */
  600. struct mmu_notifier_range {
  601. unsigned long start;
  602. unsigned long end;
  603. };
  604. static inline void _mmu_notifier_range_init(struct mmu_notifier_range *range,
  605. unsigned long start,
  606. unsigned long end)
  607. {
  608. range->start = start;
  609. range->end = end;
  610. }
  611. #define mmu_notifier_range_init(range,event,flags,vma,mm,start,end) \
  612. _mmu_notifier_range_init(range, start, end)
  613. #define mmu_notifier_range_init_owner(range, event, flags, vma, mm, start, \
  614. end, owner) \
  615. _mmu_notifier_range_init(range, start, end)
  616. static inline bool
  617. mmu_notifier_range_blockable(const struct mmu_notifier_range *range)
  618. {
  619. return true;
  620. }
  621. static inline int mm_has_notifiers(struct mm_struct *mm)
  622. {
  623. return 0;
  624. }
  625. static inline void mmu_notifier_release(struct mm_struct *mm)
  626. {
  627. }
  628. static inline int mmu_notifier_clear_flush_young(struct mm_struct *mm,
  629. unsigned long start,
  630. unsigned long end)
  631. {
  632. return 0;
  633. }
  634. static inline int mmu_notifier_test_young(struct mm_struct *mm,
  635. unsigned long address)
  636. {
  637. return 0;
  638. }
  639. static inline void mmu_notifier_change_pte(struct mm_struct *mm,
  640. unsigned long address, pte_t pte)
  641. {
  642. }
  643. static inline void
  644. mmu_notifier_invalidate_range_start(struct mmu_notifier_range *range)
  645. {
  646. }
  647. static inline int
  648. mmu_notifier_invalidate_range_start_nonblock(struct mmu_notifier_range *range)
  649. {
  650. return 0;
  651. }
  652. static inline
  653. void mmu_notifier_invalidate_range_end(struct mmu_notifier_range *range)
  654. {
  655. }
  656. static inline void
  657. mmu_notifier_invalidate_range_only_end(struct mmu_notifier_range *range)
  658. {
  659. }
  660. static inline void mmu_notifier_invalidate_range(struct mm_struct *mm,
  661. unsigned long start, unsigned long end)
  662. {
  663. }
  664. static inline void mmu_notifier_subscriptions_init(struct mm_struct *mm)
  665. {
  666. }
  667. static inline void mmu_notifier_subscriptions_destroy(struct mm_struct *mm)
  668. {
  669. }
  670. #define mmu_notifier_range_update_to_read_only(r) false
  671. #define ptep_clear_flush_young_notify ptep_clear_flush_young
  672. #define pmdp_clear_flush_young_notify pmdp_clear_flush_young
  673. #define ptep_clear_young_notify ptep_test_and_clear_young
  674. #define pmdp_clear_young_notify pmdp_test_and_clear_young
  675. #define ptep_clear_flush_notify ptep_clear_flush
  676. #define pmdp_huge_clear_flush_notify pmdp_huge_clear_flush
  677. #define pudp_huge_clear_flush_notify pudp_huge_clear_flush
  678. #define set_pte_at_notify set_pte_at
  679. static inline void mmu_notifier_synchronize(void)
  680. {
  681. }
  682. #endif /* CONFIG_MMU_NOTIFIER */
  683. #endif /* _LINUX_MMU_NOTIFIER_H */