xref: /linux-6.15/include/linux/mmu_notifier.h (revision ee65728e)
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
3606c88398SZhen Lei  * sure that anyone relying on soft dirtiness 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.
41998427b3SRalph Campbell  *
42998427b3SRalph Campbell  * @MMU_NOTIFY_MIGRATE: used during migrate_vma_collect() invalidate to signal
43998427b3SRalph Campbell  * a device driver to possibly ignore the invalidation if the
446b49bf6dSAlistair Popple  * owner field matches the driver's device private pgmap owner.
45b756a3b5SAlistair Popple  *
46b756a3b5SAlistair Popple  * @MMU_NOTIFY_EXCLUSIVE: to signal a device driver that the device will no
47b756a3b5SAlistair Popple  * longer have exclusive access to the page. When sent during creation of an
48b756a3b5SAlistair Popple  * exclusive range the owner will be initialised to the value provided by the
49b756a3b5SAlistair Popple  * caller of make_device_exclusive_range(), otherwise the owner will be NULL.
50d87f055bSJérôme Glisse  */
51d87f055bSJérôme Glisse enum mmu_notifier_event {
52d87f055bSJérôme Glisse 	MMU_NOTIFY_UNMAP = 0,
53d87f055bSJérôme Glisse 	MMU_NOTIFY_CLEAR,
54d87f055bSJérôme Glisse 	MMU_NOTIFY_PROTECTION_VMA,
55d87f055bSJérôme Glisse 	MMU_NOTIFY_PROTECTION_PAGE,
56d87f055bSJérôme Glisse 	MMU_NOTIFY_SOFT_DIRTY,
5799cb252fSJason Gunthorpe 	MMU_NOTIFY_RELEASE,
58998427b3SRalph Campbell 	MMU_NOTIFY_MIGRATE,
59b756a3b5SAlistair Popple 	MMU_NOTIFY_EXCLUSIVE,
60d87f055bSJérôme Glisse };
61d87f055bSJérôme Glisse 
6227560ee9SJérôme Glisse #define MMU_NOTIFIER_RANGE_BLOCKABLE (1 << 0)
6327560ee9SJérôme Glisse 
64cddb8a5cSAndrea Arcangeli struct mmu_notifier_ops {
65cddb8a5cSAndrea Arcangeli 	/*
66cddb8a5cSAndrea Arcangeli 	 * Called either by mmu_notifier_unregister or when the mm is
67cddb8a5cSAndrea Arcangeli 	 * being destroyed by exit_mmap, always before all pages are
68cddb8a5cSAndrea Arcangeli 	 * freed. This can run concurrently with other mmu notifier
69cddb8a5cSAndrea Arcangeli 	 * methods (the ones invoked outside the mm context) and it
70cddb8a5cSAndrea Arcangeli 	 * should tear down all secondary mmu mappings and freeze the
71cddb8a5cSAndrea Arcangeli 	 * secondary mmu. If this method isn't implemented you've to
72cddb8a5cSAndrea Arcangeli 	 * be sure that nothing could possibly write to the pages
73cddb8a5cSAndrea Arcangeli 	 * through the secondary mmu by the time the last thread with
74cddb8a5cSAndrea Arcangeli 	 * tsk->mm == mm exits.
75cddb8a5cSAndrea Arcangeli 	 *
76cddb8a5cSAndrea Arcangeli 	 * As side note: the pages freed after ->release returns could
77cddb8a5cSAndrea Arcangeli 	 * be immediately reallocated by the gart at an alias physical
78cddb8a5cSAndrea Arcangeli 	 * address with a different cache model, so if ->release isn't
79cddb8a5cSAndrea Arcangeli 	 * implemented because all _software_ driven memory accesses
80cddb8a5cSAndrea Arcangeli 	 * through the secondary mmu are terminated by the time the
81cddb8a5cSAndrea Arcangeli 	 * last thread of this mm quits, you've also to be sure that
82cddb8a5cSAndrea Arcangeli 	 * speculative _hardware_ operations can't allocate dirty
83cddb8a5cSAndrea Arcangeli 	 * cachelines in the cpu that could not be snooped and made
84cddb8a5cSAndrea Arcangeli 	 * coherent with the other read and write operations happening
85cddb8a5cSAndrea Arcangeli 	 * through the gart alias address, so leading to memory
86cddb8a5cSAndrea Arcangeli 	 * corruption.
87cddb8a5cSAndrea Arcangeli 	 */
881991722aSJason Gunthorpe 	void (*release)(struct mmu_notifier *subscription,
89cddb8a5cSAndrea Arcangeli 			struct mm_struct *mm);
90cddb8a5cSAndrea Arcangeli 
91cddb8a5cSAndrea Arcangeli 	/*
92cddb8a5cSAndrea Arcangeli 	 * clear_flush_young is called after the VM is
93cddb8a5cSAndrea Arcangeli 	 * test-and-clearing the young/accessed bitflag in the
94cddb8a5cSAndrea Arcangeli 	 * pte. This way the VM will provide proper aging to the
95cddb8a5cSAndrea Arcangeli 	 * accesses to the page through the secondary MMUs and not
96cddb8a5cSAndrea Arcangeli 	 * only to the ones through the Linux pte.
9757128468SAndres Lagar-Cavilla 	 * Start-end is necessary in case the secondary MMU is mapping the page
9857128468SAndres Lagar-Cavilla 	 * at a smaller granularity than the primary MMU.
99cddb8a5cSAndrea Arcangeli 	 */
1001991722aSJason Gunthorpe 	int (*clear_flush_young)(struct mmu_notifier *subscription,
101cddb8a5cSAndrea Arcangeli 				 struct mm_struct *mm,
10257128468SAndres Lagar-Cavilla 				 unsigned long start,
10357128468SAndres Lagar-Cavilla 				 unsigned long end);
104cddb8a5cSAndrea Arcangeli 
105cddb8a5cSAndrea Arcangeli 	/*
1061d7715c6SVladimir Davydov 	 * clear_young is a lightweight version of clear_flush_young. Like the
1071d7715c6SVladimir Davydov 	 * latter, it is supposed to test-and-clear the young/accessed bitflag
1081d7715c6SVladimir Davydov 	 * in the secondary pte, but it may omit flushing the secondary tlb.
1091d7715c6SVladimir Davydov 	 */
1101991722aSJason Gunthorpe 	int (*clear_young)(struct mmu_notifier *subscription,
1111d7715c6SVladimir Davydov 			   struct mm_struct *mm,
1121d7715c6SVladimir Davydov 			   unsigned long start,
1131d7715c6SVladimir Davydov 			   unsigned long end);
1141d7715c6SVladimir Davydov 
1151d7715c6SVladimir Davydov 	/*
1168ee53820SAndrea Arcangeli 	 * test_young is called to check the young/accessed bitflag in
1178ee53820SAndrea Arcangeli 	 * the secondary pte. This is used to know if the page is
1188ee53820SAndrea Arcangeli 	 * frequently used without actually clearing the flag or tearing
1198ee53820SAndrea Arcangeli 	 * down the secondary mapping on the page.
1208ee53820SAndrea Arcangeli 	 */
1211991722aSJason Gunthorpe 	int (*test_young)(struct mmu_notifier *subscription,
1228ee53820SAndrea Arcangeli 			  struct mm_struct *mm,
1238ee53820SAndrea Arcangeli 			  unsigned long address);
1248ee53820SAndrea Arcangeli 
1258ee53820SAndrea Arcangeli 	/*
126828502d3SIzik Eidus 	 * change_pte is called in cases that pte mapping to page is changed:
127828502d3SIzik Eidus 	 * for example, when ksm remaps pte to point to a new shared page.
128828502d3SIzik Eidus 	 */
1291991722aSJason Gunthorpe 	void (*change_pte)(struct mmu_notifier *subscription,
130828502d3SIzik Eidus 			   struct mm_struct *mm,
131828502d3SIzik Eidus 			   unsigned long address,
132828502d3SIzik Eidus 			   pte_t pte);
133828502d3SIzik Eidus 
134828502d3SIzik Eidus 	/*
135cddb8a5cSAndrea Arcangeli 	 * invalidate_range_start() and invalidate_range_end() must be
136c1e8d7c6SMichel Lespinasse 	 * paired and are called only when the mmap_lock and/or the
1370f0a327fSJoerg Roedel 	 * locks protecting the reverse maps are held. If the subsystem
1380f0a327fSJoerg Roedel 	 * can't guarantee that no additional references are taken to
1390f0a327fSJoerg Roedel 	 * the pages in the range, it has to implement the
1400f0a327fSJoerg Roedel 	 * invalidate_range() notifier to remove any references taken
1410f0a327fSJoerg Roedel 	 * after invalidate_range_start().
142cddb8a5cSAndrea Arcangeli 	 *
143cddb8a5cSAndrea Arcangeli 	 * Invalidation of multiple concurrent ranges may be
144cddb8a5cSAndrea Arcangeli 	 * optionally permitted by the driver. Either way the
145cddb8a5cSAndrea Arcangeli 	 * establishment of sptes is forbidden in the range passed to
146cddb8a5cSAndrea Arcangeli 	 * invalidate_range_begin/end for the whole duration of the
147cddb8a5cSAndrea Arcangeli 	 * invalidate_range_begin/end critical section.
148cddb8a5cSAndrea Arcangeli 	 *
149cddb8a5cSAndrea Arcangeli 	 * invalidate_range_start() is called when all pages in the
150cddb8a5cSAndrea Arcangeli 	 * range are still mapped and have at least a refcount of one.
151cddb8a5cSAndrea Arcangeli 	 *
152cddb8a5cSAndrea Arcangeli 	 * invalidate_range_end() is called when all pages in the
153cddb8a5cSAndrea Arcangeli 	 * range have been unmapped and the pages have been freed by
154cddb8a5cSAndrea Arcangeli 	 * the VM.
155cddb8a5cSAndrea Arcangeli 	 *
156cddb8a5cSAndrea Arcangeli 	 * The VM will remove the page table entries and potentially
157cddb8a5cSAndrea Arcangeli 	 * the page between invalidate_range_start() and
158cddb8a5cSAndrea Arcangeli 	 * invalidate_range_end(). If the page must not be freed
159cddb8a5cSAndrea Arcangeli 	 * because of pending I/O or other circumstances then the
160cddb8a5cSAndrea Arcangeli 	 * invalidate_range_start() callback (or the initial mapping
161cddb8a5cSAndrea Arcangeli 	 * by the driver) must make sure that the refcount is kept
162cddb8a5cSAndrea Arcangeli 	 * elevated.
163cddb8a5cSAndrea Arcangeli 	 *
164cddb8a5cSAndrea Arcangeli 	 * If the driver increases the refcount when the pages are
165cddb8a5cSAndrea Arcangeli 	 * initially mapped into an address space then either
166cddb8a5cSAndrea Arcangeli 	 * invalidate_range_start() or invalidate_range_end() may
167cddb8a5cSAndrea Arcangeli 	 * decrease the refcount. If the refcount is decreased on
168cddb8a5cSAndrea Arcangeli 	 * invalidate_range_start() then the VM can free pages as page
169cddb8a5cSAndrea Arcangeli 	 * table entries are removed.  If the refcount is only
17006c88398SZhen Lei 	 * dropped on invalidate_range_end() then the driver itself
171cddb8a5cSAndrea Arcangeli 	 * will drop the last refcount but it must take care to flush
172cddb8a5cSAndrea Arcangeli 	 * any secondary tlb before doing the final free on the
173cddb8a5cSAndrea Arcangeli 	 * page. Pages will no longer be referenced by the linux
174cddb8a5cSAndrea Arcangeli 	 * address space but may still be referenced by sptes until
175cddb8a5cSAndrea Arcangeli 	 * the last refcount is dropped.
1765ff7091fSDavid Rientjes 	 *
17793065ac7SMichal Hocko 	 * If blockable argument is set to false then the callback cannot
178c2655835SSean Christopherson 	 * sleep and has to return with -EAGAIN if sleeping would be required.
179c2655835SSean Christopherson 	 * 0 should be returned otherwise. Please note that notifiers that can
180c2655835SSean Christopherson 	 * fail invalidate_range_start are not allowed to implement
181c2655835SSean Christopherson 	 * invalidate_range_end, as there is no mechanism for informing the
182c2655835SSean Christopherson 	 * notifier that its start failed.
183cddb8a5cSAndrea Arcangeli 	 */
1841991722aSJason Gunthorpe 	int (*invalidate_range_start)(struct mmu_notifier *subscription,
1855d6527a7SJérôme Glisse 				      const struct mmu_notifier_range *range);
1861991722aSJason Gunthorpe 	void (*invalidate_range_end)(struct mmu_notifier *subscription,
1875d6527a7SJérôme Glisse 				     const struct mmu_notifier_range *range);
1880f0a327fSJoerg Roedel 
1890f0a327fSJoerg Roedel 	/*
1900f0a327fSJoerg Roedel 	 * invalidate_range() is either called between
1910f0a327fSJoerg Roedel 	 * invalidate_range_start() and invalidate_range_end() when the
1920f0a327fSJoerg Roedel 	 * VM has to free pages that where unmapped, but before the
1930f0a327fSJoerg Roedel 	 * pages are actually freed, or outside of _start()/_end() when
1940f0a327fSJoerg Roedel 	 * a (remote) TLB is necessary.
1950f0a327fSJoerg Roedel 	 *
1960f0a327fSJoerg Roedel 	 * If invalidate_range() is used to manage a non-CPU TLB with
1970f0a327fSJoerg Roedel 	 * shared page-tables, it not necessary to implement the
1980f0a327fSJoerg Roedel 	 * invalidate_range_start()/end() notifiers, as
19906c88398SZhen Lei 	 * invalidate_range() already catches the points in time when an
2000f10851eSJérôme Glisse 	 * external TLB range needs to be flushed. For more in depth
201*ee65728eSMike Rapoport 	 * discussion on this see Documentation/mm/mmu_notifier.rst
2020f0a327fSJoerg Roedel 	 *
2030f0a327fSJoerg Roedel 	 * Note that this function might be called with just a sub-range
2040f0a327fSJoerg Roedel 	 * of what was passed to invalidate_range_start()/end(), if
2050f0a327fSJoerg Roedel 	 * called between those functions.
2060f0a327fSJoerg Roedel 	 */
2071991722aSJason Gunthorpe 	void (*invalidate_range)(struct mmu_notifier *subscription,
2081991722aSJason Gunthorpe 				 struct mm_struct *mm,
2091991722aSJason Gunthorpe 				 unsigned long start,
2101991722aSJason Gunthorpe 				 unsigned long end);
2112c7933f5SJason Gunthorpe 
2122c7933f5SJason Gunthorpe 	/*
2132c7933f5SJason Gunthorpe 	 * These callbacks are used with the get/put interface to manage the
2142c7933f5SJason Gunthorpe 	 * lifetime of the mmu_notifier memory. alloc_notifier() returns a new
2152c7933f5SJason Gunthorpe 	 * notifier for use with the mm.
2162c7933f5SJason Gunthorpe 	 *
2172c7933f5SJason Gunthorpe 	 * free_notifier() is only called after the mmu_notifier has been
2182c7933f5SJason Gunthorpe 	 * fully put, calls to any ops callback are prevented and no ops
2192c7933f5SJason Gunthorpe 	 * callbacks are currently running. It is called from a SRCU callback
2202c7933f5SJason Gunthorpe 	 * and cannot sleep.
2212c7933f5SJason Gunthorpe 	 */
2222c7933f5SJason Gunthorpe 	struct mmu_notifier *(*alloc_notifier)(struct mm_struct *mm);
2231991722aSJason Gunthorpe 	void (*free_notifier)(struct mmu_notifier *subscription);
224cddb8a5cSAndrea Arcangeli };
225cddb8a5cSAndrea Arcangeli 
226cddb8a5cSAndrea Arcangeli /*
227c1e8d7c6SMichel Lespinasse  * The notifier chains are protected by mmap_lock and/or the reverse map
228cddb8a5cSAndrea Arcangeli  * semaphores. Notifier chains are only changed when all reverse maps and
229c1e8d7c6SMichel Lespinasse  * the mmap_lock locks are taken.
230cddb8a5cSAndrea Arcangeli  *
231cddb8a5cSAndrea Arcangeli  * Therefore notifier chains can only be traversed when either
232cddb8a5cSAndrea Arcangeli  *
233c1e8d7c6SMichel Lespinasse  * 1. mmap_lock is held.
234c8c06efaSDavidlohr Bueso  * 2. One of the reverse map locks is held (i_mmap_rwsem or anon_vma->rwsem).
235cddb8a5cSAndrea Arcangeli  * 3. No other concurrent thread can access the list (release)
236cddb8a5cSAndrea Arcangeli  */
237cddb8a5cSAndrea Arcangeli struct mmu_notifier {
238cddb8a5cSAndrea Arcangeli 	struct hlist_node hlist;
239cddb8a5cSAndrea Arcangeli 	const struct mmu_notifier_ops *ops;
2402c7933f5SJason Gunthorpe 	struct mm_struct *mm;
2412c7933f5SJason Gunthorpe 	struct rcu_head rcu;
2422c7933f5SJason Gunthorpe 	unsigned int users;
243cddb8a5cSAndrea Arcangeli };
244cddb8a5cSAndrea Arcangeli 
24599cb252fSJason Gunthorpe /**
24699cb252fSJason Gunthorpe  * struct mmu_interval_notifier_ops
24799cb252fSJason Gunthorpe  * @invalidate: Upon return the caller must stop using any SPTEs within this
24899cb252fSJason Gunthorpe  *              range. This function can sleep. Return false only if sleeping
24999cb252fSJason Gunthorpe  *              was required but mmu_notifier_range_blockable(range) is false.
25099cb252fSJason Gunthorpe  */
25199cb252fSJason Gunthorpe struct mmu_interval_notifier_ops {
2525292e24aSJason Gunthorpe 	bool (*invalidate)(struct mmu_interval_notifier *interval_sub,
25399cb252fSJason Gunthorpe 			   const struct mmu_notifier_range *range,
25499cb252fSJason Gunthorpe 			   unsigned long cur_seq);
25599cb252fSJason Gunthorpe };
25699cb252fSJason Gunthorpe 
25799cb252fSJason Gunthorpe struct mmu_interval_notifier {
25899cb252fSJason Gunthorpe 	struct interval_tree_node interval_tree;
25999cb252fSJason Gunthorpe 	const struct mmu_interval_notifier_ops *ops;
26099cb252fSJason Gunthorpe 	struct mm_struct *mm;
26199cb252fSJason Gunthorpe 	struct hlist_node deferred_item;
26299cb252fSJason Gunthorpe 	unsigned long invalidate_seq;
26399cb252fSJason Gunthorpe };
26499cb252fSJason Gunthorpe 
26556f434f4SJason Gunthorpe #ifdef CONFIG_MMU_NOTIFIER
26656f434f4SJason Gunthorpe 
26756f434f4SJason Gunthorpe #ifdef CONFIG_LOCKDEP
26856f434f4SJason Gunthorpe extern struct lockdep_map __mmu_notifier_invalidate_range_start_map;
26956f434f4SJason Gunthorpe #endif
27056f434f4SJason Gunthorpe 
27156f434f4SJason Gunthorpe struct mmu_notifier_range {
27256f434f4SJason Gunthorpe 	struct vm_area_struct *vma;
27356f434f4SJason Gunthorpe 	struct mm_struct *mm;
27456f434f4SJason Gunthorpe 	unsigned long start;
27556f434f4SJason Gunthorpe 	unsigned long end;
27656f434f4SJason Gunthorpe 	unsigned flags;
27756f434f4SJason Gunthorpe 	enum mmu_notifier_event event;
2786b49bf6dSAlistair Popple 	void *owner;
27956f434f4SJason Gunthorpe };
28056f434f4SJason Gunthorpe 
281cddb8a5cSAndrea Arcangeli static inline int mm_has_notifiers(struct mm_struct *mm)
282cddb8a5cSAndrea Arcangeli {
283984cfe4eSJason Gunthorpe 	return unlikely(mm->notifier_subscriptions);
284cddb8a5cSAndrea Arcangeli }
285cddb8a5cSAndrea Arcangeli 
2862c7933f5SJason Gunthorpe struct mmu_notifier *mmu_notifier_get_locked(const struct mmu_notifier_ops *ops,
2872c7933f5SJason Gunthorpe 					     struct mm_struct *mm);
2882c7933f5SJason Gunthorpe static inline struct mmu_notifier *
2892c7933f5SJason Gunthorpe mmu_notifier_get(const struct mmu_notifier_ops *ops, struct mm_struct *mm)
2902c7933f5SJason Gunthorpe {
2912c7933f5SJason Gunthorpe 	struct mmu_notifier *ret;
2922c7933f5SJason Gunthorpe 
293b72327fcSMichel Lespinasse 	mmap_write_lock(mm);
2942c7933f5SJason Gunthorpe 	ret = mmu_notifier_get_locked(ops, mm);
295b72327fcSMichel Lespinasse 	mmap_write_unlock(mm);
2962c7933f5SJason Gunthorpe 	return ret;
2972c7933f5SJason Gunthorpe }
2981991722aSJason Gunthorpe void mmu_notifier_put(struct mmu_notifier *subscription);
2992c7933f5SJason Gunthorpe void mmu_notifier_synchronize(void);
3002c7933f5SJason Gunthorpe 
3011991722aSJason Gunthorpe extern int mmu_notifier_register(struct mmu_notifier *subscription,
302cddb8a5cSAndrea Arcangeli 				 struct mm_struct *mm);
3031991722aSJason Gunthorpe extern int __mmu_notifier_register(struct mmu_notifier *subscription,
304cddb8a5cSAndrea Arcangeli 				   struct mm_struct *mm);
3051991722aSJason Gunthorpe extern void mmu_notifier_unregister(struct mmu_notifier *subscription,
306cddb8a5cSAndrea Arcangeli 				    struct mm_struct *mm);
30799cb252fSJason Gunthorpe 
3085292e24aSJason Gunthorpe unsigned long
3095292e24aSJason Gunthorpe mmu_interval_read_begin(struct mmu_interval_notifier *interval_sub);
3105292e24aSJason Gunthorpe int mmu_interval_notifier_insert(struct mmu_interval_notifier *interval_sub,
31199cb252fSJason Gunthorpe 				 struct mm_struct *mm, unsigned long start,
31299cb252fSJason Gunthorpe 				 unsigned long length,
31399cb252fSJason Gunthorpe 				 const struct mmu_interval_notifier_ops *ops);
31499cb252fSJason Gunthorpe int mmu_interval_notifier_insert_locked(
3155292e24aSJason Gunthorpe 	struct mmu_interval_notifier *interval_sub, struct mm_struct *mm,
31699cb252fSJason Gunthorpe 	unsigned long start, unsigned long length,
31799cb252fSJason Gunthorpe 	const struct mmu_interval_notifier_ops *ops);
3185292e24aSJason Gunthorpe void mmu_interval_notifier_remove(struct mmu_interval_notifier *interval_sub);
31999cb252fSJason Gunthorpe 
32099cb252fSJason Gunthorpe /**
32199cb252fSJason Gunthorpe  * mmu_interval_set_seq - Save the invalidation sequence
3225292e24aSJason Gunthorpe  * @interval_sub - The subscription passed to invalidate
32399cb252fSJason Gunthorpe  * @cur_seq - The cur_seq passed to the invalidate() callback
32499cb252fSJason Gunthorpe  *
32599cb252fSJason Gunthorpe  * This must be called unconditionally from the invalidate callback of a
32699cb252fSJason Gunthorpe  * struct mmu_interval_notifier_ops under the same lock that is used to call
32799cb252fSJason Gunthorpe  * mmu_interval_read_retry(). It updates the sequence number for later use by
32899cb252fSJason Gunthorpe  * mmu_interval_read_retry(). The provided cur_seq will always be odd.
32999cb252fSJason Gunthorpe  *
33099cb252fSJason Gunthorpe  * If the caller does not call mmu_interval_read_begin() or
33199cb252fSJason Gunthorpe  * mmu_interval_read_retry() then this call is not required.
33299cb252fSJason Gunthorpe  */
3335292e24aSJason Gunthorpe static inline void
3345292e24aSJason Gunthorpe mmu_interval_set_seq(struct mmu_interval_notifier *interval_sub,
33599cb252fSJason Gunthorpe 		     unsigned long cur_seq)
33699cb252fSJason Gunthorpe {
3375292e24aSJason Gunthorpe 	WRITE_ONCE(interval_sub->invalidate_seq, cur_seq);
33899cb252fSJason Gunthorpe }
33999cb252fSJason Gunthorpe 
34099cb252fSJason Gunthorpe /**
34199cb252fSJason Gunthorpe  * mmu_interval_read_retry - End a read side critical section against a VA range
3425292e24aSJason Gunthorpe  * interval_sub: The subscription
34399cb252fSJason Gunthorpe  * seq: The return of the paired mmu_interval_read_begin()
34499cb252fSJason Gunthorpe  *
34599cb252fSJason Gunthorpe  * This MUST be called under a user provided lock that is also held
34699cb252fSJason Gunthorpe  * unconditionally by op->invalidate() when it calls mmu_interval_set_seq().
34799cb252fSJason Gunthorpe  *
34899cb252fSJason Gunthorpe  * Each call should be paired with a single mmu_interval_read_begin() and
34999cb252fSJason Gunthorpe  * should be used to conclude the read side.
35099cb252fSJason Gunthorpe  *
35199cb252fSJason Gunthorpe  * Returns true if an invalidation collided with this critical section, and
35299cb252fSJason Gunthorpe  * the caller should retry.
35399cb252fSJason Gunthorpe  */
3545292e24aSJason Gunthorpe static inline bool
3555292e24aSJason Gunthorpe mmu_interval_read_retry(struct mmu_interval_notifier *interval_sub,
35699cb252fSJason Gunthorpe 			unsigned long seq)
35799cb252fSJason Gunthorpe {
3585292e24aSJason Gunthorpe 	return interval_sub->invalidate_seq != seq;
35999cb252fSJason Gunthorpe }
36099cb252fSJason Gunthorpe 
36199cb252fSJason Gunthorpe /**
36299cb252fSJason Gunthorpe  * mmu_interval_check_retry - Test if a collision has occurred
3635292e24aSJason Gunthorpe  * interval_sub: The subscription
36499cb252fSJason Gunthorpe  * seq: The return of the matching mmu_interval_read_begin()
36599cb252fSJason Gunthorpe  *
36699cb252fSJason Gunthorpe  * This can be used in the critical section between mmu_interval_read_begin()
36799cb252fSJason Gunthorpe  * and mmu_interval_read_retry().  A return of true indicates an invalidation
36899cb252fSJason Gunthorpe  * has collided with this critical region and a future
36999cb252fSJason Gunthorpe  * mmu_interval_read_retry() will return true.
37099cb252fSJason Gunthorpe  *
37199cb252fSJason Gunthorpe  * False is not reliable and only suggests a collision may not have
37206c88398SZhen Lei  * occurred. It can be called many times and does not have to hold the user
37399cb252fSJason Gunthorpe  * provided lock.
37499cb252fSJason Gunthorpe  *
37599cb252fSJason Gunthorpe  * This call can be used as part of loops and other expensive operations to
37699cb252fSJason Gunthorpe  * expedite a retry.
37799cb252fSJason Gunthorpe  */
3785292e24aSJason Gunthorpe static inline bool
3795292e24aSJason Gunthorpe mmu_interval_check_retry(struct mmu_interval_notifier *interval_sub,
38099cb252fSJason Gunthorpe 			 unsigned long seq)
38199cb252fSJason Gunthorpe {
38299cb252fSJason Gunthorpe 	/* Pairs with the WRITE_ONCE in mmu_interval_set_seq() */
3835292e24aSJason Gunthorpe 	return READ_ONCE(interval_sub->invalidate_seq) != seq;
38499cb252fSJason Gunthorpe }
38599cb252fSJason Gunthorpe 
386984cfe4eSJason Gunthorpe extern void __mmu_notifier_subscriptions_destroy(struct mm_struct *mm);
387cddb8a5cSAndrea Arcangeli extern void __mmu_notifier_release(struct mm_struct *mm);
388cddb8a5cSAndrea Arcangeli extern int __mmu_notifier_clear_flush_young(struct mm_struct *mm,
38957128468SAndres Lagar-Cavilla 					  unsigned long start,
39057128468SAndres Lagar-Cavilla 					  unsigned long end);
3911d7715c6SVladimir Davydov extern int __mmu_notifier_clear_young(struct mm_struct *mm,
3921d7715c6SVladimir Davydov 				      unsigned long start,
3931d7715c6SVladimir Davydov 				      unsigned long end);
3948ee53820SAndrea Arcangeli extern int __mmu_notifier_test_young(struct mm_struct *mm,
3958ee53820SAndrea Arcangeli 				     unsigned long address);
396828502d3SIzik Eidus extern void __mmu_notifier_change_pte(struct mm_struct *mm,
397828502d3SIzik Eidus 				      unsigned long address, pte_t pte);
398ac46d4f3SJérôme Glisse extern int __mmu_notifier_invalidate_range_start(struct mmu_notifier_range *r);
399ac46d4f3SJérôme Glisse extern void __mmu_notifier_invalidate_range_end(struct mmu_notifier_range *r,
4004645b9feSJérôme Glisse 				  bool only_end);
4010f0a327fSJoerg Roedel extern void __mmu_notifier_invalidate_range(struct mm_struct *mm,
4020f0a327fSJoerg Roedel 				  unsigned long start, unsigned long end);
403c6d23413SJérôme Glisse extern bool
404c6d23413SJérôme Glisse mmu_notifier_range_update_to_read_only(const struct mmu_notifier_range *range);
405cddb8a5cSAndrea Arcangeli 
4064a83bfe9SJérôme Glisse static inline bool
4074a83bfe9SJérôme Glisse mmu_notifier_range_blockable(const struct mmu_notifier_range *range)
4084a83bfe9SJérôme Glisse {
40927560ee9SJérôme Glisse 	return (range->flags & MMU_NOTIFIER_RANGE_BLOCKABLE);
4104a83bfe9SJérôme Glisse }
4114a83bfe9SJérôme Glisse 
412cddb8a5cSAndrea Arcangeli static inline void mmu_notifier_release(struct mm_struct *mm)
413cddb8a5cSAndrea Arcangeli {
414cddb8a5cSAndrea Arcangeli 	if (mm_has_notifiers(mm))
415cddb8a5cSAndrea Arcangeli 		__mmu_notifier_release(mm);
416cddb8a5cSAndrea Arcangeli }
417cddb8a5cSAndrea Arcangeli 
418cddb8a5cSAndrea Arcangeli static inline int mmu_notifier_clear_flush_young(struct mm_struct *mm,
41957128468SAndres Lagar-Cavilla 					  unsigned long start,
42057128468SAndres Lagar-Cavilla 					  unsigned long end)
421cddb8a5cSAndrea Arcangeli {
422cddb8a5cSAndrea Arcangeli 	if (mm_has_notifiers(mm))
42357128468SAndres Lagar-Cavilla 		return __mmu_notifier_clear_flush_young(mm, start, end);
424cddb8a5cSAndrea Arcangeli 	return 0;
425cddb8a5cSAndrea Arcangeli }
426cddb8a5cSAndrea Arcangeli 
4271d7715c6SVladimir Davydov static inline int mmu_notifier_clear_young(struct mm_struct *mm,
4281d7715c6SVladimir Davydov 					   unsigned long start,
4291d7715c6SVladimir Davydov 					   unsigned long end)
4301d7715c6SVladimir Davydov {
4311d7715c6SVladimir Davydov 	if (mm_has_notifiers(mm))
4321d7715c6SVladimir Davydov 		return __mmu_notifier_clear_young(mm, start, end);
4331d7715c6SVladimir Davydov 	return 0;
4341d7715c6SVladimir Davydov }
4351d7715c6SVladimir Davydov 
4368ee53820SAndrea Arcangeli static inline int mmu_notifier_test_young(struct mm_struct *mm,
4378ee53820SAndrea Arcangeli 					  unsigned long address)
4388ee53820SAndrea Arcangeli {
4398ee53820SAndrea Arcangeli 	if (mm_has_notifiers(mm))
4408ee53820SAndrea Arcangeli 		return __mmu_notifier_test_young(mm, address);
4418ee53820SAndrea Arcangeli 	return 0;
4428ee53820SAndrea Arcangeli }
4438ee53820SAndrea Arcangeli 
444828502d3SIzik Eidus static inline void mmu_notifier_change_pte(struct mm_struct *mm,
445828502d3SIzik Eidus 					   unsigned long address, pte_t pte)
446828502d3SIzik Eidus {
447828502d3SIzik Eidus 	if (mm_has_notifiers(mm))
448828502d3SIzik Eidus 		__mmu_notifier_change_pte(mm, address, pte);
449828502d3SIzik Eidus }
450828502d3SIzik Eidus 
451ac46d4f3SJérôme Glisse static inline void
452ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(struct mmu_notifier_range *range)
453cddb8a5cSAndrea Arcangeli {
454810e24e0SDaniel Vetter 	might_sleep();
455810e24e0SDaniel Vetter 
45623b68395SDaniel Vetter 	lock_map_acquire(&__mmu_notifier_invalidate_range_start_map);
457ac46d4f3SJérôme Glisse 	if (mm_has_notifiers(range->mm)) {
45827560ee9SJérôme Glisse 		range->flags |= MMU_NOTIFIER_RANGE_BLOCKABLE;
459ac46d4f3SJérôme Glisse 		__mmu_notifier_invalidate_range_start(range);
460ac46d4f3SJérôme Glisse 	}
46123b68395SDaniel Vetter 	lock_map_release(&__mmu_notifier_invalidate_range_start_map);
46293065ac7SMichal Hocko }
46393065ac7SMichal Hocko 
464ac46d4f3SJérôme Glisse static inline int
465ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start_nonblock(struct mmu_notifier_range *range)
46693065ac7SMichal Hocko {
46723b68395SDaniel Vetter 	int ret = 0;
46823b68395SDaniel Vetter 
46923b68395SDaniel Vetter 	lock_map_acquire(&__mmu_notifier_invalidate_range_start_map);
470ac46d4f3SJérôme Glisse 	if (mm_has_notifiers(range->mm)) {
47127560ee9SJérôme Glisse 		range->flags &= ~MMU_NOTIFIER_RANGE_BLOCKABLE;
47223b68395SDaniel Vetter 		ret = __mmu_notifier_invalidate_range_start(range);
473ac46d4f3SJérôme Glisse 	}
47423b68395SDaniel Vetter 	lock_map_release(&__mmu_notifier_invalidate_range_start_map);
47523b68395SDaniel Vetter 	return ret;
476cddb8a5cSAndrea Arcangeli }
477cddb8a5cSAndrea Arcangeli 
478ac46d4f3SJérôme Glisse static inline void
479ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(struct mmu_notifier_range *range)
480cddb8a5cSAndrea Arcangeli {
481810e24e0SDaniel Vetter 	if (mmu_notifier_range_blockable(range))
482810e24e0SDaniel Vetter 		might_sleep();
483810e24e0SDaniel Vetter 
484ac46d4f3SJérôme Glisse 	if (mm_has_notifiers(range->mm))
485ac46d4f3SJérôme Glisse 		__mmu_notifier_invalidate_range_end(range, false);
4864645b9feSJérôme Glisse }
4874645b9feSJérôme Glisse 
488ac46d4f3SJérôme Glisse static inline void
489ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_only_end(struct mmu_notifier_range *range)
4904645b9feSJérôme Glisse {
491ac46d4f3SJérôme Glisse 	if (mm_has_notifiers(range->mm))
492ac46d4f3SJérôme Glisse 		__mmu_notifier_invalidate_range_end(range, true);
493cddb8a5cSAndrea Arcangeli }
494cddb8a5cSAndrea Arcangeli 
4951897bdc4SJoerg Roedel static inline void mmu_notifier_invalidate_range(struct mm_struct *mm,
4961897bdc4SJoerg Roedel 				  unsigned long start, unsigned long end)
4971897bdc4SJoerg Roedel {
4980f0a327fSJoerg Roedel 	if (mm_has_notifiers(mm))
4990f0a327fSJoerg Roedel 		__mmu_notifier_invalidate_range(mm, start, end);
5001897bdc4SJoerg Roedel }
5011897bdc4SJoerg Roedel 
502984cfe4eSJason Gunthorpe static inline void mmu_notifier_subscriptions_init(struct mm_struct *mm)
503cddb8a5cSAndrea Arcangeli {
504984cfe4eSJason Gunthorpe 	mm->notifier_subscriptions = NULL;
505cddb8a5cSAndrea Arcangeli }
506cddb8a5cSAndrea Arcangeli 
507984cfe4eSJason Gunthorpe static inline void mmu_notifier_subscriptions_destroy(struct mm_struct *mm)
508cddb8a5cSAndrea Arcangeli {
509cddb8a5cSAndrea Arcangeli 	if (mm_has_notifiers(mm))
510984cfe4eSJason Gunthorpe 		__mmu_notifier_subscriptions_destroy(mm);
511cddb8a5cSAndrea Arcangeli }
512cddb8a5cSAndrea Arcangeli 
513ac46d4f3SJérôme Glisse 
514ac46d4f3SJérôme Glisse static inline void mmu_notifier_range_init(struct mmu_notifier_range *range,
5156f4f13e8SJérôme Glisse 					   enum mmu_notifier_event event,
5166f4f13e8SJérôme Glisse 					   unsigned flags,
5176f4f13e8SJérôme Glisse 					   struct vm_area_struct *vma,
518ac46d4f3SJérôme Glisse 					   struct mm_struct *mm,
519ac46d4f3SJérôme Glisse 					   unsigned long start,
520ac46d4f3SJérôme Glisse 					   unsigned long end)
521ac46d4f3SJérôme Glisse {
522bf198b2bSJérôme Glisse 	range->vma = vma;
523bf198b2bSJérôme Glisse 	range->event = event;
524ac46d4f3SJérôme Glisse 	range->mm = mm;
525ac46d4f3SJérôme Glisse 	range->start = start;
526ac46d4f3SJérôme Glisse 	range->end = end;
527bf198b2bSJérôme Glisse 	range->flags = flags;
528ac46d4f3SJérôme Glisse }
529ac46d4f3SJérôme Glisse 
5306b49bf6dSAlistair Popple static inline void mmu_notifier_range_init_owner(
5316b49bf6dSAlistair Popple 			struct mmu_notifier_range *range,
5326b49bf6dSAlistair Popple 			enum mmu_notifier_event event, unsigned int flags,
533c1a06df6SRalph Campbell 			struct vm_area_struct *vma, struct mm_struct *mm,
5346b49bf6dSAlistair Popple 			unsigned long start, unsigned long end, void *owner)
535c1a06df6SRalph Campbell {
5366b49bf6dSAlistair Popple 	mmu_notifier_range_init(range, event, flags, vma, mm, start, end);
5376b49bf6dSAlistair Popple 	range->owner = owner;
538c1a06df6SRalph Campbell }
539c1a06df6SRalph Campbell 
540cddb8a5cSAndrea Arcangeli #define ptep_clear_flush_young_notify(__vma, __address, __ptep)		\
541cddb8a5cSAndrea Arcangeli ({									\
542cddb8a5cSAndrea Arcangeli 	int __young;							\
543cddb8a5cSAndrea Arcangeli 	struct vm_area_struct *___vma = __vma;				\
544cddb8a5cSAndrea Arcangeli 	unsigned long ___address = __address;				\
545cddb8a5cSAndrea Arcangeli 	__young = ptep_clear_flush_young(___vma, ___address, __ptep);	\
546cddb8a5cSAndrea Arcangeli 	__young |= mmu_notifier_clear_flush_young(___vma->vm_mm,	\
54757128468SAndres Lagar-Cavilla 						  ___address,		\
54857128468SAndres Lagar-Cavilla 						  ___address +		\
54957128468SAndres Lagar-Cavilla 							PAGE_SIZE);	\
550cddb8a5cSAndrea Arcangeli 	__young;							\
551cddb8a5cSAndrea Arcangeli })
552cddb8a5cSAndrea Arcangeli 
55391a4ee26SAndrea Arcangeli #define pmdp_clear_flush_young_notify(__vma, __address, __pmdp)		\
55491a4ee26SAndrea Arcangeli ({									\
55591a4ee26SAndrea Arcangeli 	int __young;							\
55691a4ee26SAndrea Arcangeli 	struct vm_area_struct *___vma = __vma;				\
55791a4ee26SAndrea Arcangeli 	unsigned long ___address = __address;				\
55891a4ee26SAndrea Arcangeli 	__young = pmdp_clear_flush_young(___vma, ___address, __pmdp);	\
55991a4ee26SAndrea Arcangeli 	__young |= mmu_notifier_clear_flush_young(___vma->vm_mm,	\
56057128468SAndres Lagar-Cavilla 						  ___address,		\
56157128468SAndres Lagar-Cavilla 						  ___address +		\
56257128468SAndres Lagar-Cavilla 							PMD_SIZE);	\
56391a4ee26SAndrea Arcangeli 	__young;							\
56491a4ee26SAndrea Arcangeli })
56591a4ee26SAndrea Arcangeli 
5661d7715c6SVladimir Davydov #define ptep_clear_young_notify(__vma, __address, __ptep)		\
5671d7715c6SVladimir Davydov ({									\
5681d7715c6SVladimir Davydov 	int __young;							\
5691d7715c6SVladimir Davydov 	struct vm_area_struct *___vma = __vma;				\
5701d7715c6SVladimir Davydov 	unsigned long ___address = __address;				\
5711d7715c6SVladimir Davydov 	__young = ptep_test_and_clear_young(___vma, ___address, __ptep);\
5721d7715c6SVladimir Davydov 	__young |= mmu_notifier_clear_young(___vma->vm_mm, ___address,	\
5731d7715c6SVladimir Davydov 					    ___address + PAGE_SIZE);	\
5741d7715c6SVladimir Davydov 	__young;							\
5751d7715c6SVladimir Davydov })
5761d7715c6SVladimir Davydov 
5771d7715c6SVladimir Davydov #define pmdp_clear_young_notify(__vma, __address, __pmdp)		\
5781d7715c6SVladimir Davydov ({									\
5791d7715c6SVladimir Davydov 	int __young;							\
5801d7715c6SVladimir Davydov 	struct vm_area_struct *___vma = __vma;				\
5811d7715c6SVladimir Davydov 	unsigned long ___address = __address;				\
5821d7715c6SVladimir Davydov 	__young = pmdp_test_and_clear_young(___vma, ___address, __pmdp);\
5831d7715c6SVladimir Davydov 	__young |= mmu_notifier_clear_young(___vma->vm_mm, ___address,	\
5841d7715c6SVladimir Davydov 					    ___address + PMD_SIZE);	\
5851d7715c6SVladimir Davydov 	__young;							\
5861d7715c6SVladimir Davydov })
5871d7715c6SVladimir Davydov 
58834ee645eSJoerg Roedel #define	ptep_clear_flush_notify(__vma, __address, __ptep)		\
58934ee645eSJoerg Roedel ({									\
59034ee645eSJoerg Roedel 	unsigned long ___addr = __address & PAGE_MASK;			\
59134ee645eSJoerg Roedel 	struct mm_struct *___mm = (__vma)->vm_mm;			\
59234ee645eSJoerg Roedel 	pte_t ___pte;							\
59334ee645eSJoerg Roedel 									\
59434ee645eSJoerg Roedel 	___pte = ptep_clear_flush(__vma, __address, __ptep);		\
59534ee645eSJoerg Roedel 	mmu_notifier_invalidate_range(___mm, ___addr,			\
59634ee645eSJoerg Roedel 					___addr + PAGE_SIZE);		\
59734ee645eSJoerg Roedel 									\
59834ee645eSJoerg Roedel 	___pte;								\
59934ee645eSJoerg Roedel })
60034ee645eSJoerg Roedel 
6018809aa2dSAneesh Kumar K.V #define pmdp_huge_clear_flush_notify(__vma, __haddr, __pmd)		\
60234ee645eSJoerg Roedel ({									\
60334ee645eSJoerg Roedel 	unsigned long ___haddr = __haddr & HPAGE_PMD_MASK;		\
60434ee645eSJoerg Roedel 	struct mm_struct *___mm = (__vma)->vm_mm;			\
60534ee645eSJoerg Roedel 	pmd_t ___pmd;							\
60634ee645eSJoerg Roedel 									\
6078809aa2dSAneesh Kumar K.V 	___pmd = pmdp_huge_clear_flush(__vma, __haddr, __pmd);		\
60834ee645eSJoerg Roedel 	mmu_notifier_invalidate_range(___mm, ___haddr,			\
60934ee645eSJoerg Roedel 				      ___haddr + HPAGE_PMD_SIZE);	\
61034ee645eSJoerg Roedel 									\
61134ee645eSJoerg Roedel 	___pmd;								\
61234ee645eSJoerg Roedel })
61334ee645eSJoerg Roedel 
614a00cc7d9SMatthew Wilcox #define pudp_huge_clear_flush_notify(__vma, __haddr, __pud)		\
615a00cc7d9SMatthew Wilcox ({									\
616a00cc7d9SMatthew Wilcox 	unsigned long ___haddr = __haddr & HPAGE_PUD_MASK;		\
617a00cc7d9SMatthew Wilcox 	struct mm_struct *___mm = (__vma)->vm_mm;			\
618a00cc7d9SMatthew Wilcox 	pud_t ___pud;							\
619a00cc7d9SMatthew Wilcox 									\
620a00cc7d9SMatthew Wilcox 	___pud = pudp_huge_clear_flush(__vma, __haddr, __pud);		\
621a00cc7d9SMatthew Wilcox 	mmu_notifier_invalidate_range(___mm, ___haddr,			\
622a00cc7d9SMatthew Wilcox 				      ___haddr + HPAGE_PUD_SIZE);	\
623a00cc7d9SMatthew Wilcox 									\
624a00cc7d9SMatthew Wilcox 	___pud;								\
625a00cc7d9SMatthew Wilcox })
626a00cc7d9SMatthew Wilcox 
62748af0d7cSXiao Guangrong /*
62848af0d7cSXiao Guangrong  * set_pte_at_notify() sets the pte _after_ running the notifier.
62948af0d7cSXiao Guangrong  * This is safe to start by updating the secondary MMUs, because the primary MMU
63048af0d7cSXiao Guangrong  * pte invalidate must have already happened with a ptep_clear_flush() before
63148af0d7cSXiao Guangrong  * set_pte_at_notify() has been invoked.  Updating the secondary MMUs first is
63248af0d7cSXiao Guangrong  * required when we change both the protection of the mapping from read-only to
63348af0d7cSXiao Guangrong  * read-write and the pfn (like during copy on write page faults). Otherwise the
63448af0d7cSXiao Guangrong  * old page would remain mapped readonly in the secondary MMUs after the new
63548af0d7cSXiao Guangrong  * page is already writable by some CPU through the primary MMU.
63648af0d7cSXiao Guangrong  */
637828502d3SIzik Eidus #define set_pte_at_notify(__mm, __address, __ptep, __pte)		\
638828502d3SIzik Eidus ({									\
639828502d3SIzik Eidus 	struct mm_struct *___mm = __mm;					\
640828502d3SIzik Eidus 	unsigned long ___address = __address;				\
641828502d3SIzik Eidus 	pte_t ___pte = __pte;						\
642828502d3SIzik Eidus 									\
643828502d3SIzik Eidus 	mmu_notifier_change_pte(___mm, ___address, ___pte);		\
64448af0d7cSXiao Guangrong 	set_pte_at(___mm, ___address, __ptep, ___pte);			\
645828502d3SIzik Eidus })
646828502d3SIzik Eidus 
647cddb8a5cSAndrea Arcangeli #else /* CONFIG_MMU_NOTIFIER */
648cddb8a5cSAndrea Arcangeli 
649ac46d4f3SJérôme Glisse struct mmu_notifier_range {
650ac46d4f3SJérôme Glisse 	unsigned long start;
651ac46d4f3SJérôme Glisse 	unsigned long end;
652ac46d4f3SJérôme Glisse };
653ac46d4f3SJérôme Glisse 
654ac46d4f3SJérôme Glisse static inline void _mmu_notifier_range_init(struct mmu_notifier_range *range,
655ac46d4f3SJérôme Glisse 					    unsigned long start,
656ac46d4f3SJérôme Glisse 					    unsigned long end)
657ac46d4f3SJérôme Glisse {
658ac46d4f3SJérôme Glisse 	range->start = start;
659ac46d4f3SJérôme Glisse 	range->end = end;
660ac46d4f3SJérôme Glisse }
661ac46d4f3SJérôme Glisse 
6626f4f13e8SJérôme Glisse #define mmu_notifier_range_init(range,event,flags,vma,mm,start,end)  \
663ac46d4f3SJérôme Glisse 	_mmu_notifier_range_init(range, start, end)
6646b49bf6dSAlistair Popple #define mmu_notifier_range_init_owner(range, event, flags, vma, mm, start, \
6656b49bf6dSAlistair Popple 					end, owner) \
666c1a06df6SRalph Campbell 	_mmu_notifier_range_init(range, start, end)
667ac46d4f3SJérôme Glisse 
6684a83bfe9SJérôme Glisse static inline bool
6694a83bfe9SJérôme Glisse mmu_notifier_range_blockable(const struct mmu_notifier_range *range)
6704a83bfe9SJérôme Glisse {
6714a83bfe9SJérôme Glisse 	return true;
6724a83bfe9SJérôme Glisse }
673ac46d4f3SJérôme Glisse 
6744d4bbd85SMichal Hocko static inline int mm_has_notifiers(struct mm_struct *mm)
6754d4bbd85SMichal Hocko {
6764d4bbd85SMichal Hocko 	return 0;
6774d4bbd85SMichal Hocko }
6784d4bbd85SMichal Hocko 
679cddb8a5cSAndrea Arcangeli static inline void mmu_notifier_release(struct mm_struct *mm)
680cddb8a5cSAndrea Arcangeli {
681cddb8a5cSAndrea Arcangeli }
682cddb8a5cSAndrea Arcangeli 
683cddb8a5cSAndrea Arcangeli static inline int mmu_notifier_clear_flush_young(struct mm_struct *mm,
68457128468SAndres Lagar-Cavilla 					  unsigned long start,
68557128468SAndres Lagar-Cavilla 					  unsigned long end)
686cddb8a5cSAndrea Arcangeli {
687cddb8a5cSAndrea Arcangeli 	return 0;
688cddb8a5cSAndrea Arcangeli }
689cddb8a5cSAndrea Arcangeli 
6908ee53820SAndrea Arcangeli static inline int mmu_notifier_test_young(struct mm_struct *mm,
6918ee53820SAndrea Arcangeli 					  unsigned long address)
6928ee53820SAndrea Arcangeli {
6938ee53820SAndrea Arcangeli 	return 0;
6948ee53820SAndrea Arcangeli }
6958ee53820SAndrea Arcangeli 
696828502d3SIzik Eidus static inline void mmu_notifier_change_pte(struct mm_struct *mm,
697828502d3SIzik Eidus 					   unsigned long address, pte_t pte)
698828502d3SIzik Eidus {
699828502d3SIzik Eidus }
700828502d3SIzik Eidus 
701ac46d4f3SJérôme Glisse static inline void
702ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(struct mmu_notifier_range *range)
703cddb8a5cSAndrea Arcangeli {
704cddb8a5cSAndrea Arcangeli }
705cddb8a5cSAndrea Arcangeli 
706ac46d4f3SJérôme Glisse static inline int
707ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start_nonblock(struct mmu_notifier_range *range)
70893065ac7SMichal Hocko {
70993065ac7SMichal Hocko 	return 0;
71093065ac7SMichal Hocko }
71193065ac7SMichal Hocko 
712ac46d4f3SJérôme Glisse static inline
713ac46d4f3SJérôme Glisse void mmu_notifier_invalidate_range_end(struct mmu_notifier_range *range)
714cddb8a5cSAndrea Arcangeli {
715cddb8a5cSAndrea Arcangeli }
716cddb8a5cSAndrea Arcangeli 
717ac46d4f3SJérôme Glisse static inline void
718ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_only_end(struct mmu_notifier_range *range)
7194645b9feSJérôme Glisse {
7204645b9feSJérôme Glisse }
7214645b9feSJérôme Glisse 
7221897bdc4SJoerg Roedel static inline void mmu_notifier_invalidate_range(struct mm_struct *mm,
7231897bdc4SJoerg Roedel 				  unsigned long start, unsigned long end)
7241897bdc4SJoerg Roedel {
7251897bdc4SJoerg Roedel }
7261897bdc4SJoerg Roedel 
727984cfe4eSJason Gunthorpe static inline void mmu_notifier_subscriptions_init(struct mm_struct *mm)
728cddb8a5cSAndrea Arcangeli {
729cddb8a5cSAndrea Arcangeli }
730cddb8a5cSAndrea Arcangeli 
731984cfe4eSJason Gunthorpe static inline void mmu_notifier_subscriptions_destroy(struct mm_struct *mm)
732cddb8a5cSAndrea Arcangeli {
733cddb8a5cSAndrea Arcangeli }
734cddb8a5cSAndrea Arcangeli 
735c6d23413SJérôme Glisse #define mmu_notifier_range_update_to_read_only(r) false
736c6d23413SJérôme Glisse 
737cddb8a5cSAndrea Arcangeli #define ptep_clear_flush_young_notify ptep_clear_flush_young
73891a4ee26SAndrea Arcangeli #define pmdp_clear_flush_young_notify pmdp_clear_flush_young
73933c3fc71SVladimir Davydov #define ptep_clear_young_notify ptep_test_and_clear_young
74033c3fc71SVladimir Davydov #define pmdp_clear_young_notify pmdp_test_and_clear_young
74134ee645eSJoerg Roedel #define	ptep_clear_flush_notify ptep_clear_flush
7428809aa2dSAneesh Kumar K.V #define pmdp_huge_clear_flush_notify pmdp_huge_clear_flush
743a00cc7d9SMatthew Wilcox #define pudp_huge_clear_flush_notify pudp_huge_clear_flush
744828502d3SIzik Eidus #define set_pte_at_notify set_pte_at
745cddb8a5cSAndrea Arcangeli 
7462c7933f5SJason Gunthorpe static inline void mmu_notifier_synchronize(void)
7472c7933f5SJason Gunthorpe {
7482c7933f5SJason Gunthorpe }
7492c7933f5SJason Gunthorpe 
750cddb8a5cSAndrea Arcangeli #endif /* CONFIG_MMU_NOTIFIER */
751cddb8a5cSAndrea Arcangeli 
752cddb8a5cSAndrea Arcangeli #endif /* _LINUX_MMU_NOTIFIER_H */
753