xref: /linux-6.15/include/linux/mmu_notifier.h (revision c1e8d7c6)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2cddb8a5cSAndrea Arcangeli #ifndef _LINUX_MMU_NOTIFIER_H
3cddb8a5cSAndrea Arcangeli #define _LINUX_MMU_NOTIFIER_H
4cddb8a5cSAndrea Arcangeli 
5cddb8a5cSAndrea Arcangeli #include <linux/list.h>
6cddb8a5cSAndrea Arcangeli #include <linux/spinlock.h>
7cddb8a5cSAndrea Arcangeli #include <linux/mm_types.h>
8b72327fcSMichel Lespinasse #include <linux/mmap_lock.h>
921a92735SSagi Grimberg #include <linux/srcu.h>
1099cb252fSJason Gunthorpe #include <linux/interval_tree.h>
11cddb8a5cSAndrea Arcangeli 
12984cfe4eSJason Gunthorpe struct mmu_notifier_subscriptions;
13cddb8a5cSAndrea Arcangeli struct mmu_notifier;
1456f434f4SJason Gunthorpe struct mmu_notifier_range;
1599cb252fSJason Gunthorpe struct mmu_interval_notifier;
16cddb8a5cSAndrea Arcangeli 
17d87f055bSJérôme Glisse /**
18d87f055bSJérôme Glisse  * enum mmu_notifier_event - reason for the mmu notifier callback
19d87f055bSJérôme Glisse  * @MMU_NOTIFY_UNMAP: either munmap() that unmap the range or a mremap() that
20d87f055bSJérôme Glisse  * move the range
21d87f055bSJérôme Glisse  *
22d87f055bSJérôme Glisse  * @MMU_NOTIFY_CLEAR: clear page table entry (many reasons for this like
23d87f055bSJérôme Glisse  * madvise() or replacing a page by another one, ...).
24d87f055bSJérôme Glisse  *
25d87f055bSJérôme Glisse  * @MMU_NOTIFY_PROTECTION_VMA: update is due to protection change for the range
26d87f055bSJérôme Glisse  * ie using the vma access permission (vm_page_prot) to update the whole range
27d87f055bSJérôme Glisse  * is enough no need to inspect changes to the CPU page table (mprotect()
28d87f055bSJérôme Glisse  * syscall)
29d87f055bSJérôme Glisse  *
30d87f055bSJérôme Glisse  * @MMU_NOTIFY_PROTECTION_PAGE: update is due to change in read/write flag for
31d87f055bSJérôme Glisse  * pages in the range so to mirror those changes the user must inspect the CPU
32d87f055bSJérôme Glisse  * page table (from the end callback).
33d87f055bSJérôme Glisse  *
34d87f055bSJérôme Glisse  * @MMU_NOTIFY_SOFT_DIRTY: soft dirty accounting (still same page and same
35d87f055bSJérôme Glisse  * access flags). User should soft dirty the page in the end callback to make
36d87f055bSJérôme Glisse  * sure that anyone relying on soft dirtyness catch pages that might be written
37d87f055bSJérôme Glisse  * through non CPU mappings.
3899cb252fSJason Gunthorpe  *
3999cb252fSJason Gunthorpe  * @MMU_NOTIFY_RELEASE: used during mmu_interval_notifier invalidate to signal
4099cb252fSJason Gunthorpe  * that the mm refcount is zero and the range is no longer accessible.
41d87f055bSJérôme Glisse  */
42d87f055bSJérôme Glisse enum mmu_notifier_event {
43d87f055bSJérôme Glisse 	MMU_NOTIFY_UNMAP = 0,
44d87f055bSJérôme Glisse 	MMU_NOTIFY_CLEAR,
45d87f055bSJérôme Glisse 	MMU_NOTIFY_PROTECTION_VMA,
46d87f055bSJérôme Glisse 	MMU_NOTIFY_PROTECTION_PAGE,
47d87f055bSJérôme Glisse 	MMU_NOTIFY_SOFT_DIRTY,
4899cb252fSJason Gunthorpe 	MMU_NOTIFY_RELEASE,
49d87f055bSJérôme Glisse };
50d87f055bSJérôme Glisse 
5127560ee9SJérôme Glisse #define MMU_NOTIFIER_RANGE_BLOCKABLE (1 << 0)
5227560ee9SJérôme Glisse 
53cddb8a5cSAndrea Arcangeli struct mmu_notifier_ops {
54cddb8a5cSAndrea Arcangeli 	/*
55cddb8a5cSAndrea Arcangeli 	 * Called either by mmu_notifier_unregister or when the mm is
56cddb8a5cSAndrea Arcangeli 	 * being destroyed by exit_mmap, always before all pages are
57cddb8a5cSAndrea Arcangeli 	 * freed. This can run concurrently with other mmu notifier
58cddb8a5cSAndrea Arcangeli 	 * methods (the ones invoked outside the mm context) and it
59cddb8a5cSAndrea Arcangeli 	 * should tear down all secondary mmu mappings and freeze the
60cddb8a5cSAndrea Arcangeli 	 * secondary mmu. If this method isn't implemented you've to
61cddb8a5cSAndrea Arcangeli 	 * be sure that nothing could possibly write to the pages
62cddb8a5cSAndrea Arcangeli 	 * through the secondary mmu by the time the last thread with
63cddb8a5cSAndrea Arcangeli 	 * tsk->mm == mm exits.
64cddb8a5cSAndrea Arcangeli 	 *
65cddb8a5cSAndrea Arcangeli 	 * As side note: the pages freed after ->release returns could
66cddb8a5cSAndrea Arcangeli 	 * be immediately reallocated by the gart at an alias physical
67cddb8a5cSAndrea Arcangeli 	 * address with a different cache model, so if ->release isn't
68cddb8a5cSAndrea Arcangeli 	 * implemented because all _software_ driven memory accesses
69cddb8a5cSAndrea Arcangeli 	 * through the secondary mmu are terminated by the time the
70cddb8a5cSAndrea Arcangeli 	 * last thread of this mm quits, you've also to be sure that
71cddb8a5cSAndrea Arcangeli 	 * speculative _hardware_ operations can't allocate dirty
72cddb8a5cSAndrea Arcangeli 	 * cachelines in the cpu that could not be snooped and made
73cddb8a5cSAndrea Arcangeli 	 * coherent with the other read and write operations happening
74cddb8a5cSAndrea Arcangeli 	 * through the gart alias address, so leading to memory
75cddb8a5cSAndrea Arcangeli 	 * corruption.
76cddb8a5cSAndrea Arcangeli 	 */
771991722aSJason Gunthorpe 	void (*release)(struct mmu_notifier *subscription,
78cddb8a5cSAndrea Arcangeli 			struct mm_struct *mm);
79cddb8a5cSAndrea Arcangeli 
80cddb8a5cSAndrea Arcangeli 	/*
81cddb8a5cSAndrea Arcangeli 	 * clear_flush_young is called after the VM is
82cddb8a5cSAndrea Arcangeli 	 * test-and-clearing the young/accessed bitflag in the
83cddb8a5cSAndrea Arcangeli 	 * pte. This way the VM will provide proper aging to the
84cddb8a5cSAndrea Arcangeli 	 * accesses to the page through the secondary MMUs and not
85cddb8a5cSAndrea Arcangeli 	 * only to the ones through the Linux pte.
8657128468SAndres Lagar-Cavilla 	 * Start-end is necessary in case the secondary MMU is mapping the page
8757128468SAndres Lagar-Cavilla 	 * at a smaller granularity than the primary MMU.
88cddb8a5cSAndrea Arcangeli 	 */
891991722aSJason Gunthorpe 	int (*clear_flush_young)(struct mmu_notifier *subscription,
90cddb8a5cSAndrea Arcangeli 				 struct mm_struct *mm,
9157128468SAndres Lagar-Cavilla 				 unsigned long start,
9257128468SAndres Lagar-Cavilla 				 unsigned long end);
93cddb8a5cSAndrea Arcangeli 
94cddb8a5cSAndrea Arcangeli 	/*
951d7715c6SVladimir Davydov 	 * clear_young is a lightweight version of clear_flush_young. Like the
961d7715c6SVladimir Davydov 	 * latter, it is supposed to test-and-clear the young/accessed bitflag
971d7715c6SVladimir Davydov 	 * in the secondary pte, but it may omit flushing the secondary tlb.
981d7715c6SVladimir Davydov 	 */
991991722aSJason Gunthorpe 	int (*clear_young)(struct mmu_notifier *subscription,
1001d7715c6SVladimir Davydov 			   struct mm_struct *mm,
1011d7715c6SVladimir Davydov 			   unsigned long start,
1021d7715c6SVladimir Davydov 			   unsigned long end);
1031d7715c6SVladimir Davydov 
1041d7715c6SVladimir Davydov 	/*
1058ee53820SAndrea Arcangeli 	 * test_young is called to check the young/accessed bitflag in
1068ee53820SAndrea Arcangeli 	 * the secondary pte. This is used to know if the page is
1078ee53820SAndrea Arcangeli 	 * frequently used without actually clearing the flag or tearing
1088ee53820SAndrea Arcangeli 	 * down the secondary mapping on the page.
1098ee53820SAndrea Arcangeli 	 */
1101991722aSJason Gunthorpe 	int (*test_young)(struct mmu_notifier *subscription,
1118ee53820SAndrea Arcangeli 			  struct mm_struct *mm,
1128ee53820SAndrea Arcangeli 			  unsigned long address);
1138ee53820SAndrea Arcangeli 
1148ee53820SAndrea Arcangeli 	/*
115828502d3SIzik Eidus 	 * change_pte is called in cases that pte mapping to page is changed:
116828502d3SIzik Eidus 	 * for example, when ksm remaps pte to point to a new shared page.
117828502d3SIzik Eidus 	 */
1181991722aSJason Gunthorpe 	void (*change_pte)(struct mmu_notifier *subscription,
119828502d3SIzik Eidus 			   struct mm_struct *mm,
120828502d3SIzik Eidus 			   unsigned long address,
121828502d3SIzik Eidus 			   pte_t pte);
122828502d3SIzik Eidus 
123828502d3SIzik Eidus 	/*
124cddb8a5cSAndrea Arcangeli 	 * invalidate_range_start() and invalidate_range_end() must be
125*c1e8d7c6SMichel Lespinasse 	 * paired and are called only when the mmap_lock and/or the
1260f0a327fSJoerg Roedel 	 * locks protecting the reverse maps are held. If the subsystem
1270f0a327fSJoerg Roedel 	 * can't guarantee that no additional references are taken to
1280f0a327fSJoerg Roedel 	 * the pages in the range, it has to implement the
1290f0a327fSJoerg Roedel 	 * invalidate_range() notifier to remove any references taken
1300f0a327fSJoerg Roedel 	 * after invalidate_range_start().
131cddb8a5cSAndrea Arcangeli 	 *
132cddb8a5cSAndrea Arcangeli 	 * Invalidation of multiple concurrent ranges may be
133cddb8a5cSAndrea Arcangeli 	 * optionally permitted by the driver. Either way the
134cddb8a5cSAndrea Arcangeli 	 * establishment of sptes is forbidden in the range passed to
135cddb8a5cSAndrea Arcangeli 	 * invalidate_range_begin/end for the whole duration of the
136cddb8a5cSAndrea Arcangeli 	 * invalidate_range_begin/end critical section.
137cddb8a5cSAndrea Arcangeli 	 *
138cddb8a5cSAndrea Arcangeli 	 * invalidate_range_start() is called when all pages in the
139cddb8a5cSAndrea Arcangeli 	 * range are still mapped and have at least a refcount of one.
140cddb8a5cSAndrea Arcangeli 	 *
141cddb8a5cSAndrea Arcangeli 	 * invalidate_range_end() is called when all pages in the
142cddb8a5cSAndrea Arcangeli 	 * range have been unmapped and the pages have been freed by
143cddb8a5cSAndrea Arcangeli 	 * the VM.
144cddb8a5cSAndrea Arcangeli 	 *
145cddb8a5cSAndrea Arcangeli 	 * The VM will remove the page table entries and potentially
146cddb8a5cSAndrea Arcangeli 	 * the page between invalidate_range_start() and
147cddb8a5cSAndrea Arcangeli 	 * invalidate_range_end(). If the page must not be freed
148cddb8a5cSAndrea Arcangeli 	 * because of pending I/O or other circumstances then the
149cddb8a5cSAndrea Arcangeli 	 * invalidate_range_start() callback (or the initial mapping
150cddb8a5cSAndrea Arcangeli 	 * by the driver) must make sure that the refcount is kept
151cddb8a5cSAndrea Arcangeli 	 * elevated.
152cddb8a5cSAndrea Arcangeli 	 *
153cddb8a5cSAndrea Arcangeli 	 * If the driver increases the refcount when the pages are
154cddb8a5cSAndrea Arcangeli 	 * initially mapped into an address space then either
155cddb8a5cSAndrea Arcangeli 	 * invalidate_range_start() or invalidate_range_end() may
156cddb8a5cSAndrea Arcangeli 	 * decrease the refcount. If the refcount is decreased on
157cddb8a5cSAndrea Arcangeli 	 * invalidate_range_start() then the VM can free pages as page
158cddb8a5cSAndrea Arcangeli 	 * table entries are removed.  If the refcount is only
159cddb8a5cSAndrea Arcangeli 	 * droppped on invalidate_range_end() then the driver itself
160cddb8a5cSAndrea Arcangeli 	 * will drop the last refcount but it must take care to flush
161cddb8a5cSAndrea Arcangeli 	 * any secondary tlb before doing the final free on the
162cddb8a5cSAndrea Arcangeli 	 * page. Pages will no longer be referenced by the linux
163cddb8a5cSAndrea Arcangeli 	 * address space but may still be referenced by sptes until
164cddb8a5cSAndrea Arcangeli 	 * the last refcount is dropped.
1655ff7091fSDavid Rientjes 	 *
16693065ac7SMichal Hocko 	 * If blockable argument is set to false then the callback cannot
16793065ac7SMichal Hocko 	 * sleep and has to return with -EAGAIN. 0 should be returned
16833490af3SMichal Hocko 	 * otherwise. Please note that if invalidate_range_start approves
16933490af3SMichal Hocko 	 * a non-blocking behavior then the same applies to
17033490af3SMichal Hocko 	 * invalidate_range_end.
17193065ac7SMichal Hocko 	 *
172cddb8a5cSAndrea Arcangeli 	 */
1731991722aSJason Gunthorpe 	int (*invalidate_range_start)(struct mmu_notifier *subscription,
1745d6527a7SJérôme Glisse 				      const struct mmu_notifier_range *range);
1751991722aSJason Gunthorpe 	void (*invalidate_range_end)(struct mmu_notifier *subscription,
1765d6527a7SJérôme Glisse 				     const struct mmu_notifier_range *range);
1770f0a327fSJoerg Roedel 
1780f0a327fSJoerg Roedel 	/*
1790f0a327fSJoerg Roedel 	 * invalidate_range() is either called between
1800f0a327fSJoerg Roedel 	 * invalidate_range_start() and invalidate_range_end() when the
1810f0a327fSJoerg Roedel 	 * VM has to free pages that where unmapped, but before the
1820f0a327fSJoerg Roedel 	 * pages are actually freed, or outside of _start()/_end() when
1830f0a327fSJoerg Roedel 	 * a (remote) TLB is necessary.
1840f0a327fSJoerg Roedel 	 *
1850f0a327fSJoerg Roedel 	 * If invalidate_range() is used to manage a non-CPU TLB with
1860f0a327fSJoerg Roedel 	 * shared page-tables, it not necessary to implement the
1870f0a327fSJoerg Roedel 	 * invalidate_range_start()/end() notifiers, as
1880f0a327fSJoerg Roedel 	 * invalidate_range() alread catches the points in time when an
1890f10851eSJérôme Glisse 	 * external TLB range needs to be flushed. For more in depth
190ad56b738SMike Rapoport 	 * discussion on this see Documentation/vm/mmu_notifier.rst
1910f0a327fSJoerg Roedel 	 *
1920f0a327fSJoerg Roedel 	 * Note that this function might be called with just a sub-range
1930f0a327fSJoerg Roedel 	 * of what was passed to invalidate_range_start()/end(), if
1940f0a327fSJoerg Roedel 	 * called between those functions.
1950f0a327fSJoerg Roedel 	 */
1961991722aSJason Gunthorpe 	void (*invalidate_range)(struct mmu_notifier *subscription,
1971991722aSJason Gunthorpe 				 struct mm_struct *mm,
1981991722aSJason Gunthorpe 				 unsigned long start,
1991991722aSJason Gunthorpe 				 unsigned long end);
2002c7933f5SJason Gunthorpe 
2012c7933f5SJason Gunthorpe 	/*
2022c7933f5SJason Gunthorpe 	 * These callbacks are used with the get/put interface to manage the
2032c7933f5SJason Gunthorpe 	 * lifetime of the mmu_notifier memory. alloc_notifier() returns a new
2042c7933f5SJason Gunthorpe 	 * notifier for use with the mm.
2052c7933f5SJason Gunthorpe 	 *
2062c7933f5SJason Gunthorpe 	 * free_notifier() is only called after the mmu_notifier has been
2072c7933f5SJason Gunthorpe 	 * fully put, calls to any ops callback are prevented and no ops
2082c7933f5SJason Gunthorpe 	 * callbacks are currently running. It is called from a SRCU callback
2092c7933f5SJason Gunthorpe 	 * and cannot sleep.
2102c7933f5SJason Gunthorpe 	 */
2112c7933f5SJason Gunthorpe 	struct mmu_notifier *(*alloc_notifier)(struct mm_struct *mm);
2121991722aSJason Gunthorpe 	void (*free_notifier)(struct mmu_notifier *subscription);
213cddb8a5cSAndrea Arcangeli };
214cddb8a5cSAndrea Arcangeli 
215cddb8a5cSAndrea Arcangeli /*
216*c1e8d7c6SMichel Lespinasse  * The notifier chains are protected by mmap_lock and/or the reverse map
217cddb8a5cSAndrea Arcangeli  * semaphores. Notifier chains are only changed when all reverse maps and
218*c1e8d7c6SMichel Lespinasse  * the mmap_lock locks are taken.
219cddb8a5cSAndrea Arcangeli  *
220cddb8a5cSAndrea Arcangeli  * Therefore notifier chains can only be traversed when either
221cddb8a5cSAndrea Arcangeli  *
222*c1e8d7c6SMichel Lespinasse  * 1. mmap_lock is held.
223c8c06efaSDavidlohr Bueso  * 2. One of the reverse map locks is held (i_mmap_rwsem or anon_vma->rwsem).
224cddb8a5cSAndrea Arcangeli  * 3. No other concurrent thread can access the list (release)
225cddb8a5cSAndrea Arcangeli  */
226cddb8a5cSAndrea Arcangeli struct mmu_notifier {
227cddb8a5cSAndrea Arcangeli 	struct hlist_node hlist;
228cddb8a5cSAndrea Arcangeli 	const struct mmu_notifier_ops *ops;
2292c7933f5SJason Gunthorpe 	struct mm_struct *mm;
2302c7933f5SJason Gunthorpe 	struct rcu_head rcu;
2312c7933f5SJason Gunthorpe 	unsigned int users;
232cddb8a5cSAndrea Arcangeli };
233cddb8a5cSAndrea Arcangeli 
23499cb252fSJason Gunthorpe /**
23599cb252fSJason Gunthorpe  * struct mmu_interval_notifier_ops
23699cb252fSJason Gunthorpe  * @invalidate: Upon return the caller must stop using any SPTEs within this
23799cb252fSJason Gunthorpe  *              range. This function can sleep. Return false only if sleeping
23899cb252fSJason Gunthorpe  *              was required but mmu_notifier_range_blockable(range) is false.
23999cb252fSJason Gunthorpe  */
24099cb252fSJason Gunthorpe struct mmu_interval_notifier_ops {
2415292e24aSJason Gunthorpe 	bool (*invalidate)(struct mmu_interval_notifier *interval_sub,
24299cb252fSJason Gunthorpe 			   const struct mmu_notifier_range *range,
24399cb252fSJason Gunthorpe 			   unsigned long cur_seq);
24499cb252fSJason Gunthorpe };
24599cb252fSJason Gunthorpe 
24699cb252fSJason Gunthorpe struct mmu_interval_notifier {
24799cb252fSJason Gunthorpe 	struct interval_tree_node interval_tree;
24899cb252fSJason Gunthorpe 	const struct mmu_interval_notifier_ops *ops;
24999cb252fSJason Gunthorpe 	struct mm_struct *mm;
25099cb252fSJason Gunthorpe 	struct hlist_node deferred_item;
25199cb252fSJason Gunthorpe 	unsigned long invalidate_seq;
25299cb252fSJason Gunthorpe };
25399cb252fSJason Gunthorpe 
25456f434f4SJason Gunthorpe #ifdef CONFIG_MMU_NOTIFIER
25556f434f4SJason Gunthorpe 
25656f434f4SJason Gunthorpe #ifdef CONFIG_LOCKDEP
25756f434f4SJason Gunthorpe extern struct lockdep_map __mmu_notifier_invalidate_range_start_map;
25856f434f4SJason Gunthorpe #endif
25956f434f4SJason Gunthorpe 
26056f434f4SJason Gunthorpe struct mmu_notifier_range {
26156f434f4SJason Gunthorpe 	struct vm_area_struct *vma;
26256f434f4SJason Gunthorpe 	struct mm_struct *mm;
26356f434f4SJason Gunthorpe 	unsigned long start;
26456f434f4SJason Gunthorpe 	unsigned long end;
26556f434f4SJason Gunthorpe 	unsigned flags;
26656f434f4SJason Gunthorpe 	enum mmu_notifier_event event;
26756f434f4SJason Gunthorpe };
26856f434f4SJason Gunthorpe 
269cddb8a5cSAndrea Arcangeli static inline int mm_has_notifiers(struct mm_struct *mm)
270cddb8a5cSAndrea Arcangeli {
271984cfe4eSJason Gunthorpe 	return unlikely(mm->notifier_subscriptions);
272cddb8a5cSAndrea Arcangeli }
273cddb8a5cSAndrea Arcangeli 
2742c7933f5SJason Gunthorpe struct mmu_notifier *mmu_notifier_get_locked(const struct mmu_notifier_ops *ops,
2752c7933f5SJason Gunthorpe 					     struct mm_struct *mm);
2762c7933f5SJason Gunthorpe static inline struct mmu_notifier *
2772c7933f5SJason Gunthorpe mmu_notifier_get(const struct mmu_notifier_ops *ops, struct mm_struct *mm)
2782c7933f5SJason Gunthorpe {
2792c7933f5SJason Gunthorpe 	struct mmu_notifier *ret;
2802c7933f5SJason Gunthorpe 
281b72327fcSMichel Lespinasse 	mmap_write_lock(mm);
2822c7933f5SJason Gunthorpe 	ret = mmu_notifier_get_locked(ops, mm);
283b72327fcSMichel Lespinasse 	mmap_write_unlock(mm);
2842c7933f5SJason Gunthorpe 	return ret;
2852c7933f5SJason Gunthorpe }
2861991722aSJason Gunthorpe void mmu_notifier_put(struct mmu_notifier *subscription);
2872c7933f5SJason Gunthorpe void mmu_notifier_synchronize(void);
2882c7933f5SJason Gunthorpe 
2891991722aSJason Gunthorpe extern int mmu_notifier_register(struct mmu_notifier *subscription,
290cddb8a5cSAndrea Arcangeli 				 struct mm_struct *mm);
2911991722aSJason Gunthorpe extern int __mmu_notifier_register(struct mmu_notifier *subscription,
292cddb8a5cSAndrea Arcangeli 				   struct mm_struct *mm);
2931991722aSJason Gunthorpe extern void mmu_notifier_unregister(struct mmu_notifier *subscription,
294cddb8a5cSAndrea Arcangeli 				    struct mm_struct *mm);
29599cb252fSJason Gunthorpe 
2965292e24aSJason Gunthorpe unsigned long
2975292e24aSJason Gunthorpe mmu_interval_read_begin(struct mmu_interval_notifier *interval_sub);
2985292e24aSJason Gunthorpe int mmu_interval_notifier_insert(struct mmu_interval_notifier *interval_sub,
29999cb252fSJason Gunthorpe 				 struct mm_struct *mm, unsigned long start,
30099cb252fSJason Gunthorpe 				 unsigned long length,
30199cb252fSJason Gunthorpe 				 const struct mmu_interval_notifier_ops *ops);
30299cb252fSJason Gunthorpe int mmu_interval_notifier_insert_locked(
3035292e24aSJason Gunthorpe 	struct mmu_interval_notifier *interval_sub, struct mm_struct *mm,
30499cb252fSJason Gunthorpe 	unsigned long start, unsigned long length,
30599cb252fSJason Gunthorpe 	const struct mmu_interval_notifier_ops *ops);
3065292e24aSJason Gunthorpe void mmu_interval_notifier_remove(struct mmu_interval_notifier *interval_sub);
30799cb252fSJason Gunthorpe 
30899cb252fSJason Gunthorpe /**
30999cb252fSJason Gunthorpe  * mmu_interval_set_seq - Save the invalidation sequence
3105292e24aSJason Gunthorpe  * @interval_sub - The subscription passed to invalidate
31199cb252fSJason Gunthorpe  * @cur_seq - The cur_seq passed to the invalidate() callback
31299cb252fSJason Gunthorpe  *
31399cb252fSJason Gunthorpe  * This must be called unconditionally from the invalidate callback of a
31499cb252fSJason Gunthorpe  * struct mmu_interval_notifier_ops under the same lock that is used to call
31599cb252fSJason Gunthorpe  * mmu_interval_read_retry(). It updates the sequence number for later use by
31699cb252fSJason Gunthorpe  * mmu_interval_read_retry(). The provided cur_seq will always be odd.
31799cb252fSJason Gunthorpe  *
31899cb252fSJason Gunthorpe  * If the caller does not call mmu_interval_read_begin() or
31999cb252fSJason Gunthorpe  * mmu_interval_read_retry() then this call is not required.
32099cb252fSJason Gunthorpe  */
3215292e24aSJason Gunthorpe static inline void
3225292e24aSJason Gunthorpe mmu_interval_set_seq(struct mmu_interval_notifier *interval_sub,
32399cb252fSJason Gunthorpe 		     unsigned long cur_seq)
32499cb252fSJason Gunthorpe {
3255292e24aSJason Gunthorpe 	WRITE_ONCE(interval_sub->invalidate_seq, cur_seq);
32699cb252fSJason Gunthorpe }
32799cb252fSJason Gunthorpe 
32899cb252fSJason Gunthorpe /**
32999cb252fSJason Gunthorpe  * mmu_interval_read_retry - End a read side critical section against a VA range
3305292e24aSJason Gunthorpe  * interval_sub: The subscription
33199cb252fSJason Gunthorpe  * seq: The return of the paired mmu_interval_read_begin()
33299cb252fSJason Gunthorpe  *
33399cb252fSJason Gunthorpe  * This MUST be called under a user provided lock that is also held
33499cb252fSJason Gunthorpe  * unconditionally by op->invalidate() when it calls mmu_interval_set_seq().
33599cb252fSJason Gunthorpe  *
33699cb252fSJason Gunthorpe  * Each call should be paired with a single mmu_interval_read_begin() and
33799cb252fSJason Gunthorpe  * should be used to conclude the read side.
33899cb252fSJason Gunthorpe  *
33999cb252fSJason Gunthorpe  * Returns true if an invalidation collided with this critical section, and
34099cb252fSJason Gunthorpe  * the caller should retry.
34199cb252fSJason Gunthorpe  */
3425292e24aSJason Gunthorpe static inline bool
3435292e24aSJason Gunthorpe mmu_interval_read_retry(struct mmu_interval_notifier *interval_sub,
34499cb252fSJason Gunthorpe 			unsigned long seq)
34599cb252fSJason Gunthorpe {
3465292e24aSJason Gunthorpe 	return interval_sub->invalidate_seq != seq;
34799cb252fSJason Gunthorpe }
34899cb252fSJason Gunthorpe 
34999cb252fSJason Gunthorpe /**
35099cb252fSJason Gunthorpe  * mmu_interval_check_retry - Test if a collision has occurred
3515292e24aSJason Gunthorpe  * interval_sub: The subscription
35299cb252fSJason Gunthorpe  * seq: The return of the matching mmu_interval_read_begin()
35399cb252fSJason Gunthorpe  *
35499cb252fSJason Gunthorpe  * This can be used in the critical section between mmu_interval_read_begin()
35599cb252fSJason Gunthorpe  * and mmu_interval_read_retry().  A return of true indicates an invalidation
35699cb252fSJason Gunthorpe  * has collided with this critical region and a future
35799cb252fSJason Gunthorpe  * mmu_interval_read_retry() will return true.
35899cb252fSJason Gunthorpe  *
35999cb252fSJason Gunthorpe  * False is not reliable and only suggests a collision may not have
36099cb252fSJason Gunthorpe  * occured. It can be called many times and does not have to hold the user
36199cb252fSJason Gunthorpe  * provided lock.
36299cb252fSJason Gunthorpe  *
36399cb252fSJason Gunthorpe  * This call can be used as part of loops and other expensive operations to
36499cb252fSJason Gunthorpe  * expedite a retry.
36599cb252fSJason Gunthorpe  */
3665292e24aSJason Gunthorpe static inline bool
3675292e24aSJason Gunthorpe mmu_interval_check_retry(struct mmu_interval_notifier *interval_sub,
36899cb252fSJason Gunthorpe 			 unsigned long seq)
36999cb252fSJason Gunthorpe {
37099cb252fSJason Gunthorpe 	/* Pairs with the WRITE_ONCE in mmu_interval_set_seq() */
3715292e24aSJason Gunthorpe 	return READ_ONCE(interval_sub->invalidate_seq) != seq;
37299cb252fSJason Gunthorpe }
37399cb252fSJason Gunthorpe 
374984cfe4eSJason Gunthorpe extern void __mmu_notifier_subscriptions_destroy(struct mm_struct *mm);
375cddb8a5cSAndrea Arcangeli extern void __mmu_notifier_release(struct mm_struct *mm);
376cddb8a5cSAndrea Arcangeli extern int __mmu_notifier_clear_flush_young(struct mm_struct *mm,
37757128468SAndres Lagar-Cavilla 					  unsigned long start,
37857128468SAndres Lagar-Cavilla 					  unsigned long end);
3791d7715c6SVladimir Davydov extern int __mmu_notifier_clear_young(struct mm_struct *mm,
3801d7715c6SVladimir Davydov 				      unsigned long start,
3811d7715c6SVladimir Davydov 				      unsigned long end);
3828ee53820SAndrea Arcangeli extern int __mmu_notifier_test_young(struct mm_struct *mm,
3838ee53820SAndrea Arcangeli 				     unsigned long address);
384828502d3SIzik Eidus extern void __mmu_notifier_change_pte(struct mm_struct *mm,
385828502d3SIzik Eidus 				      unsigned long address, pte_t pte);
386ac46d4f3SJérôme Glisse extern int __mmu_notifier_invalidate_range_start(struct mmu_notifier_range *r);
387ac46d4f3SJérôme Glisse extern void __mmu_notifier_invalidate_range_end(struct mmu_notifier_range *r,
3884645b9feSJérôme Glisse 				  bool only_end);
3890f0a327fSJoerg Roedel extern void __mmu_notifier_invalidate_range(struct mm_struct *mm,
3900f0a327fSJoerg Roedel 				  unsigned long start, unsigned long end);
391c6d23413SJérôme Glisse extern bool
392c6d23413SJérôme Glisse mmu_notifier_range_update_to_read_only(const struct mmu_notifier_range *range);
393cddb8a5cSAndrea Arcangeli 
3944a83bfe9SJérôme Glisse static inline bool
3954a83bfe9SJérôme Glisse mmu_notifier_range_blockable(const struct mmu_notifier_range *range)
3964a83bfe9SJérôme Glisse {
39727560ee9SJérôme Glisse 	return (range->flags & MMU_NOTIFIER_RANGE_BLOCKABLE);
3984a83bfe9SJérôme Glisse }
3994a83bfe9SJérôme Glisse 
400cddb8a5cSAndrea Arcangeli static inline void mmu_notifier_release(struct mm_struct *mm)
401cddb8a5cSAndrea Arcangeli {
402cddb8a5cSAndrea Arcangeli 	if (mm_has_notifiers(mm))
403cddb8a5cSAndrea Arcangeli 		__mmu_notifier_release(mm);
404cddb8a5cSAndrea Arcangeli }
405cddb8a5cSAndrea Arcangeli 
406cddb8a5cSAndrea Arcangeli static inline int mmu_notifier_clear_flush_young(struct mm_struct *mm,
40757128468SAndres Lagar-Cavilla 					  unsigned long start,
40857128468SAndres Lagar-Cavilla 					  unsigned long end)
409cddb8a5cSAndrea Arcangeli {
410cddb8a5cSAndrea Arcangeli 	if (mm_has_notifiers(mm))
41157128468SAndres Lagar-Cavilla 		return __mmu_notifier_clear_flush_young(mm, start, end);
412cddb8a5cSAndrea Arcangeli 	return 0;
413cddb8a5cSAndrea Arcangeli }
414cddb8a5cSAndrea Arcangeli 
4151d7715c6SVladimir Davydov static inline int mmu_notifier_clear_young(struct mm_struct *mm,
4161d7715c6SVladimir Davydov 					   unsigned long start,
4171d7715c6SVladimir Davydov 					   unsigned long end)
4181d7715c6SVladimir Davydov {
4191d7715c6SVladimir Davydov 	if (mm_has_notifiers(mm))
4201d7715c6SVladimir Davydov 		return __mmu_notifier_clear_young(mm, start, end);
4211d7715c6SVladimir Davydov 	return 0;
4221d7715c6SVladimir Davydov }
4231d7715c6SVladimir Davydov 
4248ee53820SAndrea Arcangeli static inline int mmu_notifier_test_young(struct mm_struct *mm,
4258ee53820SAndrea Arcangeli 					  unsigned long address)
4268ee53820SAndrea Arcangeli {
4278ee53820SAndrea Arcangeli 	if (mm_has_notifiers(mm))
4288ee53820SAndrea Arcangeli 		return __mmu_notifier_test_young(mm, address);
4298ee53820SAndrea Arcangeli 	return 0;
4308ee53820SAndrea Arcangeli }
4318ee53820SAndrea Arcangeli 
432828502d3SIzik Eidus static inline void mmu_notifier_change_pte(struct mm_struct *mm,
433828502d3SIzik Eidus 					   unsigned long address, pte_t pte)
434828502d3SIzik Eidus {
435828502d3SIzik Eidus 	if (mm_has_notifiers(mm))
436828502d3SIzik Eidus 		__mmu_notifier_change_pte(mm, address, pte);
437828502d3SIzik Eidus }
438828502d3SIzik Eidus 
439ac46d4f3SJérôme Glisse static inline void
440ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(struct mmu_notifier_range *range)
441cddb8a5cSAndrea Arcangeli {
442810e24e0SDaniel Vetter 	might_sleep();
443810e24e0SDaniel Vetter 
44423b68395SDaniel Vetter 	lock_map_acquire(&__mmu_notifier_invalidate_range_start_map);
445ac46d4f3SJérôme Glisse 	if (mm_has_notifiers(range->mm)) {
44627560ee9SJérôme Glisse 		range->flags |= MMU_NOTIFIER_RANGE_BLOCKABLE;
447ac46d4f3SJérôme Glisse 		__mmu_notifier_invalidate_range_start(range);
448ac46d4f3SJérôme Glisse 	}
44923b68395SDaniel Vetter 	lock_map_release(&__mmu_notifier_invalidate_range_start_map);
45093065ac7SMichal Hocko }
45193065ac7SMichal Hocko 
452ac46d4f3SJérôme Glisse static inline int
453ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start_nonblock(struct mmu_notifier_range *range)
45493065ac7SMichal Hocko {
45523b68395SDaniel Vetter 	int ret = 0;
45623b68395SDaniel Vetter 
45723b68395SDaniel Vetter 	lock_map_acquire(&__mmu_notifier_invalidate_range_start_map);
458ac46d4f3SJérôme Glisse 	if (mm_has_notifiers(range->mm)) {
45927560ee9SJérôme Glisse 		range->flags &= ~MMU_NOTIFIER_RANGE_BLOCKABLE;
46023b68395SDaniel Vetter 		ret = __mmu_notifier_invalidate_range_start(range);
461ac46d4f3SJérôme Glisse 	}
46223b68395SDaniel Vetter 	lock_map_release(&__mmu_notifier_invalidate_range_start_map);
46323b68395SDaniel Vetter 	return ret;
464cddb8a5cSAndrea Arcangeli }
465cddb8a5cSAndrea Arcangeli 
466ac46d4f3SJérôme Glisse static inline void
467ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(struct mmu_notifier_range *range)
468cddb8a5cSAndrea Arcangeli {
469810e24e0SDaniel Vetter 	if (mmu_notifier_range_blockable(range))
470810e24e0SDaniel Vetter 		might_sleep();
471810e24e0SDaniel Vetter 
472ac46d4f3SJérôme Glisse 	if (mm_has_notifiers(range->mm))
473ac46d4f3SJérôme Glisse 		__mmu_notifier_invalidate_range_end(range, false);
4744645b9feSJérôme Glisse }
4754645b9feSJérôme Glisse 
476ac46d4f3SJérôme Glisse static inline void
477ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_only_end(struct mmu_notifier_range *range)
4784645b9feSJérôme Glisse {
479ac46d4f3SJérôme Glisse 	if (mm_has_notifiers(range->mm))
480ac46d4f3SJérôme Glisse 		__mmu_notifier_invalidate_range_end(range, true);
481cddb8a5cSAndrea Arcangeli }
482cddb8a5cSAndrea Arcangeli 
4831897bdc4SJoerg Roedel static inline void mmu_notifier_invalidate_range(struct mm_struct *mm,
4841897bdc4SJoerg Roedel 				  unsigned long start, unsigned long end)
4851897bdc4SJoerg Roedel {
4860f0a327fSJoerg Roedel 	if (mm_has_notifiers(mm))
4870f0a327fSJoerg Roedel 		__mmu_notifier_invalidate_range(mm, start, end);
4881897bdc4SJoerg Roedel }
4891897bdc4SJoerg Roedel 
490984cfe4eSJason Gunthorpe static inline void mmu_notifier_subscriptions_init(struct mm_struct *mm)
491cddb8a5cSAndrea Arcangeli {
492984cfe4eSJason Gunthorpe 	mm->notifier_subscriptions = NULL;
493cddb8a5cSAndrea Arcangeli }
494cddb8a5cSAndrea Arcangeli 
495984cfe4eSJason Gunthorpe static inline void mmu_notifier_subscriptions_destroy(struct mm_struct *mm)
496cddb8a5cSAndrea Arcangeli {
497cddb8a5cSAndrea Arcangeli 	if (mm_has_notifiers(mm))
498984cfe4eSJason Gunthorpe 		__mmu_notifier_subscriptions_destroy(mm);
499cddb8a5cSAndrea Arcangeli }
500cddb8a5cSAndrea Arcangeli 
501ac46d4f3SJérôme Glisse 
502ac46d4f3SJérôme Glisse static inline void mmu_notifier_range_init(struct mmu_notifier_range *range,
5036f4f13e8SJérôme Glisse 					   enum mmu_notifier_event event,
5046f4f13e8SJérôme Glisse 					   unsigned flags,
5056f4f13e8SJérôme Glisse 					   struct vm_area_struct *vma,
506ac46d4f3SJérôme Glisse 					   struct mm_struct *mm,
507ac46d4f3SJérôme Glisse 					   unsigned long start,
508ac46d4f3SJérôme Glisse 					   unsigned long end)
509ac46d4f3SJérôme Glisse {
510bf198b2bSJérôme Glisse 	range->vma = vma;
511bf198b2bSJérôme Glisse 	range->event = event;
512ac46d4f3SJérôme Glisse 	range->mm = mm;
513ac46d4f3SJérôme Glisse 	range->start = start;
514ac46d4f3SJérôme Glisse 	range->end = end;
515bf198b2bSJérôme Glisse 	range->flags = flags;
516ac46d4f3SJérôme Glisse }
517ac46d4f3SJérôme Glisse 
518cddb8a5cSAndrea Arcangeli #define ptep_clear_flush_young_notify(__vma, __address, __ptep)		\
519cddb8a5cSAndrea Arcangeli ({									\
520cddb8a5cSAndrea Arcangeli 	int __young;							\
521cddb8a5cSAndrea Arcangeli 	struct vm_area_struct *___vma = __vma;				\
522cddb8a5cSAndrea Arcangeli 	unsigned long ___address = __address;				\
523cddb8a5cSAndrea Arcangeli 	__young = ptep_clear_flush_young(___vma, ___address, __ptep);	\
524cddb8a5cSAndrea Arcangeli 	__young |= mmu_notifier_clear_flush_young(___vma->vm_mm,	\
52557128468SAndres Lagar-Cavilla 						  ___address,		\
52657128468SAndres Lagar-Cavilla 						  ___address +		\
52757128468SAndres Lagar-Cavilla 							PAGE_SIZE);	\
528cddb8a5cSAndrea Arcangeli 	__young;							\
529cddb8a5cSAndrea Arcangeli })
530cddb8a5cSAndrea Arcangeli 
53191a4ee26SAndrea Arcangeli #define pmdp_clear_flush_young_notify(__vma, __address, __pmdp)		\
53291a4ee26SAndrea Arcangeli ({									\
53391a4ee26SAndrea Arcangeli 	int __young;							\
53491a4ee26SAndrea Arcangeli 	struct vm_area_struct *___vma = __vma;				\
53591a4ee26SAndrea Arcangeli 	unsigned long ___address = __address;				\
53691a4ee26SAndrea Arcangeli 	__young = pmdp_clear_flush_young(___vma, ___address, __pmdp);	\
53791a4ee26SAndrea Arcangeli 	__young |= mmu_notifier_clear_flush_young(___vma->vm_mm,	\
53857128468SAndres Lagar-Cavilla 						  ___address,		\
53957128468SAndres Lagar-Cavilla 						  ___address +		\
54057128468SAndres Lagar-Cavilla 							PMD_SIZE);	\
54191a4ee26SAndrea Arcangeli 	__young;							\
54291a4ee26SAndrea Arcangeli })
54391a4ee26SAndrea Arcangeli 
5441d7715c6SVladimir Davydov #define ptep_clear_young_notify(__vma, __address, __ptep)		\
5451d7715c6SVladimir Davydov ({									\
5461d7715c6SVladimir Davydov 	int __young;							\
5471d7715c6SVladimir Davydov 	struct vm_area_struct *___vma = __vma;				\
5481d7715c6SVladimir Davydov 	unsigned long ___address = __address;				\
5491d7715c6SVladimir Davydov 	__young = ptep_test_and_clear_young(___vma, ___address, __ptep);\
5501d7715c6SVladimir Davydov 	__young |= mmu_notifier_clear_young(___vma->vm_mm, ___address,	\
5511d7715c6SVladimir Davydov 					    ___address + PAGE_SIZE);	\
5521d7715c6SVladimir Davydov 	__young;							\
5531d7715c6SVladimir Davydov })
5541d7715c6SVladimir Davydov 
5551d7715c6SVladimir Davydov #define pmdp_clear_young_notify(__vma, __address, __pmdp)		\
5561d7715c6SVladimir Davydov ({									\
5571d7715c6SVladimir Davydov 	int __young;							\
5581d7715c6SVladimir Davydov 	struct vm_area_struct *___vma = __vma;				\
5591d7715c6SVladimir Davydov 	unsigned long ___address = __address;				\
5601d7715c6SVladimir Davydov 	__young = pmdp_test_and_clear_young(___vma, ___address, __pmdp);\
5611d7715c6SVladimir Davydov 	__young |= mmu_notifier_clear_young(___vma->vm_mm, ___address,	\
5621d7715c6SVladimir Davydov 					    ___address + PMD_SIZE);	\
5631d7715c6SVladimir Davydov 	__young;							\
5641d7715c6SVladimir Davydov })
5651d7715c6SVladimir Davydov 
56634ee645eSJoerg Roedel #define	ptep_clear_flush_notify(__vma, __address, __ptep)		\
56734ee645eSJoerg Roedel ({									\
56834ee645eSJoerg Roedel 	unsigned long ___addr = __address & PAGE_MASK;			\
56934ee645eSJoerg Roedel 	struct mm_struct *___mm = (__vma)->vm_mm;			\
57034ee645eSJoerg Roedel 	pte_t ___pte;							\
57134ee645eSJoerg Roedel 									\
57234ee645eSJoerg Roedel 	___pte = ptep_clear_flush(__vma, __address, __ptep);		\
57334ee645eSJoerg Roedel 	mmu_notifier_invalidate_range(___mm, ___addr,			\
57434ee645eSJoerg Roedel 					___addr + PAGE_SIZE);		\
57534ee645eSJoerg Roedel 									\
57634ee645eSJoerg Roedel 	___pte;								\
57734ee645eSJoerg Roedel })
57834ee645eSJoerg Roedel 
5798809aa2dSAneesh Kumar K.V #define pmdp_huge_clear_flush_notify(__vma, __haddr, __pmd)		\
58034ee645eSJoerg Roedel ({									\
58134ee645eSJoerg Roedel 	unsigned long ___haddr = __haddr & HPAGE_PMD_MASK;		\
58234ee645eSJoerg Roedel 	struct mm_struct *___mm = (__vma)->vm_mm;			\
58334ee645eSJoerg Roedel 	pmd_t ___pmd;							\
58434ee645eSJoerg Roedel 									\
5858809aa2dSAneesh Kumar K.V 	___pmd = pmdp_huge_clear_flush(__vma, __haddr, __pmd);		\
58634ee645eSJoerg Roedel 	mmu_notifier_invalidate_range(___mm, ___haddr,			\
58734ee645eSJoerg Roedel 				      ___haddr + HPAGE_PMD_SIZE);	\
58834ee645eSJoerg Roedel 									\
58934ee645eSJoerg Roedel 	___pmd;								\
59034ee645eSJoerg Roedel })
59134ee645eSJoerg Roedel 
592a00cc7d9SMatthew Wilcox #define pudp_huge_clear_flush_notify(__vma, __haddr, __pud)		\
593a00cc7d9SMatthew Wilcox ({									\
594a00cc7d9SMatthew Wilcox 	unsigned long ___haddr = __haddr & HPAGE_PUD_MASK;		\
595a00cc7d9SMatthew Wilcox 	struct mm_struct *___mm = (__vma)->vm_mm;			\
596a00cc7d9SMatthew Wilcox 	pud_t ___pud;							\
597a00cc7d9SMatthew Wilcox 									\
598a00cc7d9SMatthew Wilcox 	___pud = pudp_huge_clear_flush(__vma, __haddr, __pud);		\
599a00cc7d9SMatthew Wilcox 	mmu_notifier_invalidate_range(___mm, ___haddr,			\
600a00cc7d9SMatthew Wilcox 				      ___haddr + HPAGE_PUD_SIZE);	\
601a00cc7d9SMatthew Wilcox 									\
602a00cc7d9SMatthew Wilcox 	___pud;								\
603a00cc7d9SMatthew Wilcox })
604a00cc7d9SMatthew Wilcox 
60548af0d7cSXiao Guangrong /*
60648af0d7cSXiao Guangrong  * set_pte_at_notify() sets the pte _after_ running the notifier.
60748af0d7cSXiao Guangrong  * This is safe to start by updating the secondary MMUs, because the primary MMU
60848af0d7cSXiao Guangrong  * pte invalidate must have already happened with a ptep_clear_flush() before
60948af0d7cSXiao Guangrong  * set_pte_at_notify() has been invoked.  Updating the secondary MMUs first is
61048af0d7cSXiao Guangrong  * required when we change both the protection of the mapping from read-only to
61148af0d7cSXiao Guangrong  * read-write and the pfn (like during copy on write page faults). Otherwise the
61248af0d7cSXiao Guangrong  * old page would remain mapped readonly in the secondary MMUs after the new
61348af0d7cSXiao Guangrong  * page is already writable by some CPU through the primary MMU.
61448af0d7cSXiao Guangrong  */
615828502d3SIzik Eidus #define set_pte_at_notify(__mm, __address, __ptep, __pte)		\
616828502d3SIzik Eidus ({									\
617828502d3SIzik Eidus 	struct mm_struct *___mm = __mm;					\
618828502d3SIzik Eidus 	unsigned long ___address = __address;				\
619828502d3SIzik Eidus 	pte_t ___pte = __pte;						\
620828502d3SIzik Eidus 									\
621828502d3SIzik Eidus 	mmu_notifier_change_pte(___mm, ___address, ___pte);		\
62248af0d7cSXiao Guangrong 	set_pte_at(___mm, ___address, __ptep, ___pte);			\
623828502d3SIzik Eidus })
624828502d3SIzik Eidus 
625cddb8a5cSAndrea Arcangeli #else /* CONFIG_MMU_NOTIFIER */
626cddb8a5cSAndrea Arcangeli 
627ac46d4f3SJérôme Glisse struct mmu_notifier_range {
628ac46d4f3SJérôme Glisse 	unsigned long start;
629ac46d4f3SJérôme Glisse 	unsigned long end;
630ac46d4f3SJérôme Glisse };
631ac46d4f3SJérôme Glisse 
632ac46d4f3SJérôme Glisse static inline void _mmu_notifier_range_init(struct mmu_notifier_range *range,
633ac46d4f3SJérôme Glisse 					    unsigned long start,
634ac46d4f3SJérôme Glisse 					    unsigned long end)
635ac46d4f3SJérôme Glisse {
636ac46d4f3SJérôme Glisse 	range->start = start;
637ac46d4f3SJérôme Glisse 	range->end = end;
638ac46d4f3SJérôme Glisse }
639ac46d4f3SJérôme Glisse 
6406f4f13e8SJérôme Glisse #define mmu_notifier_range_init(range,event,flags,vma,mm,start,end)  \
641ac46d4f3SJérôme Glisse 	_mmu_notifier_range_init(range, start, end)
642ac46d4f3SJérôme Glisse 
6434a83bfe9SJérôme Glisse static inline bool
6444a83bfe9SJérôme Glisse mmu_notifier_range_blockable(const struct mmu_notifier_range *range)
6454a83bfe9SJérôme Glisse {
6464a83bfe9SJérôme Glisse 	return true;
6474a83bfe9SJérôme Glisse }
648ac46d4f3SJérôme Glisse 
6494d4bbd85SMichal Hocko static inline int mm_has_notifiers(struct mm_struct *mm)
6504d4bbd85SMichal Hocko {
6514d4bbd85SMichal Hocko 	return 0;
6524d4bbd85SMichal Hocko }
6534d4bbd85SMichal Hocko 
654cddb8a5cSAndrea Arcangeli static inline void mmu_notifier_release(struct mm_struct *mm)
655cddb8a5cSAndrea Arcangeli {
656cddb8a5cSAndrea Arcangeli }
657cddb8a5cSAndrea Arcangeli 
658cddb8a5cSAndrea Arcangeli static inline int mmu_notifier_clear_flush_young(struct mm_struct *mm,
65957128468SAndres Lagar-Cavilla 					  unsigned long start,
66057128468SAndres Lagar-Cavilla 					  unsigned long end)
661cddb8a5cSAndrea Arcangeli {
662cddb8a5cSAndrea Arcangeli 	return 0;
663cddb8a5cSAndrea Arcangeli }
664cddb8a5cSAndrea Arcangeli 
6658ee53820SAndrea Arcangeli static inline int mmu_notifier_test_young(struct mm_struct *mm,
6668ee53820SAndrea Arcangeli 					  unsigned long address)
6678ee53820SAndrea Arcangeli {
6688ee53820SAndrea Arcangeli 	return 0;
6698ee53820SAndrea Arcangeli }
6708ee53820SAndrea Arcangeli 
671828502d3SIzik Eidus static inline void mmu_notifier_change_pte(struct mm_struct *mm,
672828502d3SIzik Eidus 					   unsigned long address, pte_t pte)
673828502d3SIzik Eidus {
674828502d3SIzik Eidus }
675828502d3SIzik Eidus 
676ac46d4f3SJérôme Glisse static inline void
677ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(struct mmu_notifier_range *range)
678cddb8a5cSAndrea Arcangeli {
679cddb8a5cSAndrea Arcangeli }
680cddb8a5cSAndrea Arcangeli 
681ac46d4f3SJérôme Glisse static inline int
682ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start_nonblock(struct mmu_notifier_range *range)
68393065ac7SMichal Hocko {
68493065ac7SMichal Hocko 	return 0;
68593065ac7SMichal Hocko }
68693065ac7SMichal Hocko 
687ac46d4f3SJérôme Glisse static inline
688ac46d4f3SJérôme Glisse void mmu_notifier_invalidate_range_end(struct mmu_notifier_range *range)
689cddb8a5cSAndrea Arcangeli {
690cddb8a5cSAndrea Arcangeli }
691cddb8a5cSAndrea Arcangeli 
692ac46d4f3SJérôme Glisse static inline void
693ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_only_end(struct mmu_notifier_range *range)
6944645b9feSJérôme Glisse {
6954645b9feSJérôme Glisse }
6964645b9feSJérôme Glisse 
6971897bdc4SJoerg Roedel static inline void mmu_notifier_invalidate_range(struct mm_struct *mm,
6981897bdc4SJoerg Roedel 				  unsigned long start, unsigned long end)
6991897bdc4SJoerg Roedel {
7001897bdc4SJoerg Roedel }
7011897bdc4SJoerg Roedel 
702984cfe4eSJason Gunthorpe static inline void mmu_notifier_subscriptions_init(struct mm_struct *mm)
703cddb8a5cSAndrea Arcangeli {
704cddb8a5cSAndrea Arcangeli }
705cddb8a5cSAndrea Arcangeli 
706984cfe4eSJason Gunthorpe static inline void mmu_notifier_subscriptions_destroy(struct mm_struct *mm)
707cddb8a5cSAndrea Arcangeli {
708cddb8a5cSAndrea Arcangeli }
709cddb8a5cSAndrea Arcangeli 
710c6d23413SJérôme Glisse #define mmu_notifier_range_update_to_read_only(r) false
711c6d23413SJérôme Glisse 
712cddb8a5cSAndrea Arcangeli #define ptep_clear_flush_young_notify ptep_clear_flush_young
71391a4ee26SAndrea Arcangeli #define pmdp_clear_flush_young_notify pmdp_clear_flush_young
71433c3fc71SVladimir Davydov #define ptep_clear_young_notify ptep_test_and_clear_young
71533c3fc71SVladimir Davydov #define pmdp_clear_young_notify pmdp_test_and_clear_young
71634ee645eSJoerg Roedel #define	ptep_clear_flush_notify ptep_clear_flush
7178809aa2dSAneesh Kumar K.V #define pmdp_huge_clear_flush_notify pmdp_huge_clear_flush
718a00cc7d9SMatthew Wilcox #define pudp_huge_clear_flush_notify pudp_huge_clear_flush
719828502d3SIzik Eidus #define set_pte_at_notify set_pte_at
720cddb8a5cSAndrea Arcangeli 
7212c7933f5SJason Gunthorpe static inline void mmu_notifier_synchronize(void)
7222c7933f5SJason Gunthorpe {
7232c7933f5SJason Gunthorpe }
7242c7933f5SJason Gunthorpe 
725cddb8a5cSAndrea Arcangeli #endif /* CONFIG_MMU_NOTIFIER */
726cddb8a5cSAndrea Arcangeli 
727cddb8a5cSAndrea Arcangeli #endif /* _LINUX_MMU_NOTIFIER_H */
728