1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_MMU_NOTIFIER_H
3 #define _LINUX_MMU_NOTIFIER_H
5 #include <linux/list.h>
6 #include <linux/spinlock.h>
7 #include <linux/mm_types.h>
8 #include <linux/srcu.h>
11 struct mmu_notifier_ops;
14 * enum mmu_notifier_event - reason for the mmu notifier callback
15 * @MMU_NOTIFY_UNMAP: either munmap() that unmap the range or a mremap() that
18 * @MMU_NOTIFY_CLEAR: clear page table entry (many reasons for this like
19 * madvise() or replacing a page by another one, ...).
21 * @MMU_NOTIFY_PROTECTION_VMA: update is due to protection change for the range
22 * ie using the vma access permission (vm_page_prot) to update the whole range
23 * is enough no need to inspect changes to the CPU page table (mprotect()
26 * @MMU_NOTIFY_PROTECTION_PAGE: update is due to change in read/write flag for
27 * pages in the range so to mirror those changes the user must inspect the CPU
28 * page table (from the end callback).
30 * @MMU_NOTIFY_SOFT_DIRTY: soft dirty accounting (still same page and same
31 * access flags). User should soft dirty the page in the end callback to make
32 * sure that anyone relying on soft dirtyness catch pages that might be written
33 * through non CPU mappings.
35 enum mmu_notifier_event {
38 MMU_NOTIFY_PROTECTION_VMA,
39 MMU_NOTIFY_PROTECTION_PAGE,
40 MMU_NOTIFY_SOFT_DIRTY,
43 #ifdef CONFIG_MMU_NOTIFIER
46 * The mmu notifier_mm structure is allocated and installed in
47 * mm->mmu_notifier_mm inside the mm_take_all_locks() protected
48 * critical section and it's released only when mm_count reaches zero
51 struct mmu_notifier_mm {
52 /* all mmu notifiers registerd in this mm are queued in this list */
53 struct hlist_head list;
54 /* to serialize the list modifications and hlist_unhashed */
58 #define MMU_NOTIFIER_RANGE_BLOCKABLE (1 << 0)
60 struct mmu_notifier_range {
67 struct mmu_notifier_ops {
69 * Called either by mmu_notifier_unregister or when the mm is
70 * being destroyed by exit_mmap, always before all pages are
71 * freed. This can run concurrently with other mmu notifier
72 * methods (the ones invoked outside the mm context) and it
73 * should tear down all secondary mmu mappings and freeze the
74 * secondary mmu. If this method isn't implemented you've to
75 * be sure that nothing could possibly write to the pages
76 * through the secondary mmu by the time the last thread with
77 * tsk->mm == mm exits.
79 * As side note: the pages freed after ->release returns could
80 * be immediately reallocated by the gart at an alias physical
81 * address with a different cache model, so if ->release isn't
82 * implemented because all _software_ driven memory accesses
83 * through the secondary mmu are terminated by the time the
84 * last thread of this mm quits, you've also to be sure that
85 * speculative _hardware_ operations can't allocate dirty
86 * cachelines in the cpu that could not be snooped and made
87 * coherent with the other read and write operations happening
88 * through the gart alias address, so leading to memory
91 void (*release)(struct mmu_notifier *mn,
92 struct mm_struct *mm);
95 * clear_flush_young is called after the VM is
96 * test-and-clearing the young/accessed bitflag in the
97 * pte. This way the VM will provide proper aging to the
98 * accesses to the page through the secondary MMUs and not
99 * only to the ones through the Linux pte.
100 * Start-end is necessary in case the secondary MMU is mapping the page
101 * at a smaller granularity than the primary MMU.
103 int (*clear_flush_young)(struct mmu_notifier *mn,
104 struct mm_struct *mm,
109 * clear_young is a lightweight version of clear_flush_young. Like the
110 * latter, it is supposed to test-and-clear the young/accessed bitflag
111 * in the secondary pte, but it may omit flushing the secondary tlb.
113 int (*clear_young)(struct mmu_notifier *mn,
114 struct mm_struct *mm,
119 * test_young is called to check the young/accessed bitflag in
120 * the secondary pte. This is used to know if the page is
121 * frequently used without actually clearing the flag or tearing
122 * down the secondary mapping on the page.
124 int (*test_young)(struct mmu_notifier *mn,
125 struct mm_struct *mm,
126 unsigned long address);
129 * change_pte is called in cases that pte mapping to page is changed:
130 * for example, when ksm remaps pte to point to a new shared page.
132 void (*change_pte)(struct mmu_notifier *mn,
133 struct mm_struct *mm,
134 unsigned long address,
138 * invalidate_range_start() and invalidate_range_end() must be
139 * paired and are called only when the mmap_sem and/or the
140 * locks protecting the reverse maps are held. If the subsystem
141 * can't guarantee that no additional references are taken to
142 * the pages in the range, it has to implement the
143 * invalidate_range() notifier to remove any references taken
144 * after invalidate_range_start().
146 * Invalidation of multiple concurrent ranges may be
147 * optionally permitted by the driver. Either way the
148 * establishment of sptes is forbidden in the range passed to
149 * invalidate_range_begin/end for the whole duration of the
150 * invalidate_range_begin/end critical section.
152 * invalidate_range_start() is called when all pages in the
153 * range are still mapped and have at least a refcount of one.
155 * invalidate_range_end() is called when all pages in the
156 * range have been unmapped and the pages have been freed by
159 * The VM will remove the page table entries and potentially
160 * the page between invalidate_range_start() and
161 * invalidate_range_end(). If the page must not be freed
162 * because of pending I/O or other circumstances then the
163 * invalidate_range_start() callback (or the initial mapping
164 * by the driver) must make sure that the refcount is kept
167 * If the driver increases the refcount when the pages are
168 * initially mapped into an address space then either
169 * invalidate_range_start() or invalidate_range_end() may
170 * decrease the refcount. If the refcount is decreased on
171 * invalidate_range_start() then the VM can free pages as page
172 * table entries are removed. If the refcount is only
173 * droppped on invalidate_range_end() then the driver itself
174 * will drop the last refcount but it must take care to flush
175 * any secondary tlb before doing the final free on the
176 * page. Pages will no longer be referenced by the linux
177 * address space but may still be referenced by sptes until
178 * the last refcount is dropped.
180 * If blockable argument is set to false then the callback cannot
181 * sleep and has to return with -EAGAIN. 0 should be returned
182 * otherwise. Please note that if invalidate_range_start approves
183 * a non-blocking behavior then the same applies to
184 * invalidate_range_end.
187 int (*invalidate_range_start)(struct mmu_notifier *mn,
188 const struct mmu_notifier_range *range);
189 void (*invalidate_range_end)(struct mmu_notifier *mn,
190 const struct mmu_notifier_range *range);
193 * invalidate_range() is either called between
194 * invalidate_range_start() and invalidate_range_end() when the
195 * VM has to free pages that where unmapped, but before the
196 * pages are actually freed, or outside of _start()/_end() when
197 * a (remote) TLB is necessary.
199 * If invalidate_range() is used to manage a non-CPU TLB with
200 * shared page-tables, it not necessary to implement the
201 * invalidate_range_start()/end() notifiers, as
202 * invalidate_range() alread catches the points in time when an
203 * external TLB range needs to be flushed. For more in depth
204 * discussion on this see Documentation/vm/mmu_notifier.rst
206 * Note that this function might be called with just a sub-range
207 * of what was passed to invalidate_range_start()/end(), if
208 * called between those functions.
210 void (*invalidate_range)(struct mmu_notifier *mn, struct mm_struct *mm,
211 unsigned long start, unsigned long end);
215 * The notifier chains are protected by mmap_sem and/or the reverse map
216 * semaphores. Notifier chains are only changed when all reverse maps and
217 * the mmap_sem locks are taken.
219 * Therefore notifier chains can only be traversed when either
221 * 1. mmap_sem is held.
222 * 2. One of the reverse map locks is held (i_mmap_rwsem or anon_vma->rwsem).
223 * 3. No other concurrent thread can access the list (release)
225 struct mmu_notifier {
226 struct hlist_node hlist;
227 const struct mmu_notifier_ops *ops;
230 static inline int mm_has_notifiers(struct mm_struct *mm)
232 return unlikely(mm->mmu_notifier_mm);
235 extern int mmu_notifier_register(struct mmu_notifier *mn,
236 struct mm_struct *mm);
237 extern int __mmu_notifier_register(struct mmu_notifier *mn,
238 struct mm_struct *mm);
239 extern void mmu_notifier_unregister(struct mmu_notifier *mn,
240 struct mm_struct *mm);
241 extern void mmu_notifier_unregister_no_release(struct mmu_notifier *mn,
242 struct mm_struct *mm);
243 extern void __mmu_notifier_mm_destroy(struct mm_struct *mm);
244 extern void __mmu_notifier_release(struct mm_struct *mm);
245 extern int __mmu_notifier_clear_flush_young(struct mm_struct *mm,
248 extern int __mmu_notifier_clear_young(struct mm_struct *mm,
251 extern int __mmu_notifier_test_young(struct mm_struct *mm,
252 unsigned long address);
253 extern void __mmu_notifier_change_pte(struct mm_struct *mm,
254 unsigned long address, pte_t pte);
255 extern int __mmu_notifier_invalidate_range_start(struct mmu_notifier_range *r);
256 extern void __mmu_notifier_invalidate_range_end(struct mmu_notifier_range *r,
258 extern void __mmu_notifier_invalidate_range(struct mm_struct *mm,
259 unsigned long start, unsigned long end);
262 mmu_notifier_range_blockable(const struct mmu_notifier_range *range)
264 return (range->flags & MMU_NOTIFIER_RANGE_BLOCKABLE);
267 static inline void mmu_notifier_release(struct mm_struct *mm)
269 if (mm_has_notifiers(mm))
270 __mmu_notifier_release(mm);
273 static inline int mmu_notifier_clear_flush_young(struct mm_struct *mm,
277 if (mm_has_notifiers(mm))
278 return __mmu_notifier_clear_flush_young(mm, start, end);
282 static inline int mmu_notifier_clear_young(struct mm_struct *mm,
286 if (mm_has_notifiers(mm))
287 return __mmu_notifier_clear_young(mm, start, end);
291 static inline int mmu_notifier_test_young(struct mm_struct *mm,
292 unsigned long address)
294 if (mm_has_notifiers(mm))
295 return __mmu_notifier_test_young(mm, address);
299 static inline void mmu_notifier_change_pte(struct mm_struct *mm,
300 unsigned long address, pte_t pte)
302 if (mm_has_notifiers(mm))
303 __mmu_notifier_change_pte(mm, address, pte);
307 mmu_notifier_invalidate_range_start(struct mmu_notifier_range *range)
309 if (mm_has_notifiers(range->mm)) {
310 range->flags |= MMU_NOTIFIER_RANGE_BLOCKABLE;
311 __mmu_notifier_invalidate_range_start(range);
316 mmu_notifier_invalidate_range_start_nonblock(struct mmu_notifier_range *range)
318 if (mm_has_notifiers(range->mm)) {
319 range->flags &= ~MMU_NOTIFIER_RANGE_BLOCKABLE;
320 return __mmu_notifier_invalidate_range_start(range);
326 mmu_notifier_invalidate_range_end(struct mmu_notifier_range *range)
328 if (mm_has_notifiers(range->mm))
329 __mmu_notifier_invalidate_range_end(range, false);
333 mmu_notifier_invalidate_range_only_end(struct mmu_notifier_range *range)
335 if (mm_has_notifiers(range->mm))
336 __mmu_notifier_invalidate_range_end(range, true);
339 static inline void mmu_notifier_invalidate_range(struct mm_struct *mm,
340 unsigned long start, unsigned long end)
342 if (mm_has_notifiers(mm))
343 __mmu_notifier_invalidate_range(mm, start, end);
346 static inline void mmu_notifier_mm_init(struct mm_struct *mm)
348 mm->mmu_notifier_mm = NULL;
351 static inline void mmu_notifier_mm_destroy(struct mm_struct *mm)
353 if (mm_has_notifiers(mm))
354 __mmu_notifier_mm_destroy(mm);
358 static inline void mmu_notifier_range_init(struct mmu_notifier_range *range,
359 enum mmu_notifier_event event,
361 struct vm_area_struct *vma,
362 struct mm_struct *mm,
367 range->start = start;
372 #define ptep_clear_flush_young_notify(__vma, __address, __ptep) \
375 struct vm_area_struct *___vma = __vma; \
376 unsigned long ___address = __address; \
377 __young = ptep_clear_flush_young(___vma, ___address, __ptep); \
378 __young |= mmu_notifier_clear_flush_young(___vma->vm_mm, \
385 #define pmdp_clear_flush_young_notify(__vma, __address, __pmdp) \
388 struct vm_area_struct *___vma = __vma; \
389 unsigned long ___address = __address; \
390 __young = pmdp_clear_flush_young(___vma, ___address, __pmdp); \
391 __young |= mmu_notifier_clear_flush_young(___vma->vm_mm, \
398 #define ptep_clear_young_notify(__vma, __address, __ptep) \
401 struct vm_area_struct *___vma = __vma; \
402 unsigned long ___address = __address; \
403 __young = ptep_test_and_clear_young(___vma, ___address, __ptep);\
404 __young |= mmu_notifier_clear_young(___vma->vm_mm, ___address, \
405 ___address + PAGE_SIZE); \
409 #define pmdp_clear_young_notify(__vma, __address, __pmdp) \
412 struct vm_area_struct *___vma = __vma; \
413 unsigned long ___address = __address; \
414 __young = pmdp_test_and_clear_young(___vma, ___address, __pmdp);\
415 __young |= mmu_notifier_clear_young(___vma->vm_mm, ___address, \
416 ___address + PMD_SIZE); \
420 #define ptep_clear_flush_notify(__vma, __address, __ptep) \
422 unsigned long ___addr = __address & PAGE_MASK; \
423 struct mm_struct *___mm = (__vma)->vm_mm; \
426 ___pte = ptep_clear_flush(__vma, __address, __ptep); \
427 mmu_notifier_invalidate_range(___mm, ___addr, \
428 ___addr + PAGE_SIZE); \
433 #define pmdp_huge_clear_flush_notify(__vma, __haddr, __pmd) \
435 unsigned long ___haddr = __haddr & HPAGE_PMD_MASK; \
436 struct mm_struct *___mm = (__vma)->vm_mm; \
439 ___pmd = pmdp_huge_clear_flush(__vma, __haddr, __pmd); \
440 mmu_notifier_invalidate_range(___mm, ___haddr, \
441 ___haddr + HPAGE_PMD_SIZE); \
446 #define pudp_huge_clear_flush_notify(__vma, __haddr, __pud) \
448 unsigned long ___haddr = __haddr & HPAGE_PUD_MASK; \
449 struct mm_struct *___mm = (__vma)->vm_mm; \
452 ___pud = pudp_huge_clear_flush(__vma, __haddr, __pud); \
453 mmu_notifier_invalidate_range(___mm, ___haddr, \
454 ___haddr + HPAGE_PUD_SIZE); \
460 * set_pte_at_notify() sets the pte _after_ running the notifier.
461 * This is safe to start by updating the secondary MMUs, because the primary MMU
462 * pte invalidate must have already happened with a ptep_clear_flush() before
463 * set_pte_at_notify() has been invoked. Updating the secondary MMUs first is
464 * required when we change both the protection of the mapping from read-only to
465 * read-write and the pfn (like during copy on write page faults). Otherwise the
466 * old page would remain mapped readonly in the secondary MMUs after the new
467 * page is already writable by some CPU through the primary MMU.
469 #define set_pte_at_notify(__mm, __address, __ptep, __pte) \
471 struct mm_struct *___mm = __mm; \
472 unsigned long ___address = __address; \
473 pte_t ___pte = __pte; \
475 mmu_notifier_change_pte(___mm, ___address, ___pte); \
476 set_pte_at(___mm, ___address, __ptep, ___pte); \
479 extern void mmu_notifier_call_srcu(struct rcu_head *rcu,
480 void (*func)(struct rcu_head *rcu));
482 #else /* CONFIG_MMU_NOTIFIER */
484 struct mmu_notifier_range {
489 static inline void _mmu_notifier_range_init(struct mmu_notifier_range *range,
493 range->start = start;
497 #define mmu_notifier_range_init(range,event,flags,vma,mm,start,end) \
498 _mmu_notifier_range_init(range, start, end)
501 mmu_notifier_range_blockable(const struct mmu_notifier_range *range)
506 static inline int mm_has_notifiers(struct mm_struct *mm)
511 static inline void mmu_notifier_release(struct mm_struct *mm)
515 static inline int mmu_notifier_clear_flush_young(struct mm_struct *mm,
522 static inline int mmu_notifier_test_young(struct mm_struct *mm,
523 unsigned long address)
528 static inline void mmu_notifier_change_pte(struct mm_struct *mm,
529 unsigned long address, pte_t pte)
534 mmu_notifier_invalidate_range_start(struct mmu_notifier_range *range)
539 mmu_notifier_invalidate_range_start_nonblock(struct mmu_notifier_range *range)
545 void mmu_notifier_invalidate_range_end(struct mmu_notifier_range *range)
550 mmu_notifier_invalidate_range_only_end(struct mmu_notifier_range *range)
554 static inline void mmu_notifier_invalidate_range(struct mm_struct *mm,
555 unsigned long start, unsigned long end)
559 static inline void mmu_notifier_mm_init(struct mm_struct *mm)
563 static inline void mmu_notifier_mm_destroy(struct mm_struct *mm)
567 #define ptep_clear_flush_young_notify ptep_clear_flush_young
568 #define pmdp_clear_flush_young_notify pmdp_clear_flush_young
569 #define ptep_clear_young_notify ptep_test_and_clear_young
570 #define pmdp_clear_young_notify pmdp_test_and_clear_young
571 #define ptep_clear_flush_notify ptep_clear_flush
572 #define pmdp_huge_clear_flush_notify pmdp_huge_clear_flush
573 #define pudp_huge_clear_flush_notify pudp_huge_clear_flush
574 #define set_pte_at_notify set_pte_at
576 #endif /* CONFIG_MMU_NOTIFIER */
578 #endif /* _LINUX_MMU_NOTIFIER_H */