xref: /linux/mm/mmu_notifier.c (revision 0fc8f6200d2313278fbf4539bbab74677c685531)
120c8ccb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2cddb8a5cSAndrea Arcangeli /*
3cddb8a5cSAndrea Arcangeli  *  linux/mm/mmu_notifier.c
4cddb8a5cSAndrea Arcangeli  *
5cddb8a5cSAndrea Arcangeli  *  Copyright (C) 2008  Qumranet, Inc.
6cddb8a5cSAndrea Arcangeli  *  Copyright (C) 2008  SGI
7786d5cc2SChristoph Lameter (Ampere)  *             Christoph Lameter <cl@gentwo.org>
8cddb8a5cSAndrea Arcangeli  */
9cddb8a5cSAndrea Arcangeli 
10cddb8a5cSAndrea Arcangeli #include <linux/rculist.h>
11cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h>
12b95f1b31SPaul Gortmaker #include <linux/export.h>
13cddb8a5cSAndrea Arcangeli #include <linux/mm.h>
14cddb8a5cSAndrea Arcangeli #include <linux/err.h>
1599cb252fSJason Gunthorpe #include <linux/interval_tree.h>
1621a92735SSagi Grimberg #include <linux/srcu.h>
17cddb8a5cSAndrea Arcangeli #include <linux/rcupdate.h>
18cddb8a5cSAndrea Arcangeli #include <linux/sched.h>
196e84f315SIngo Molnar #include <linux/sched/mm.h>
205a0e3ad6STejun Heo #include <linux/slab.h>
21cddb8a5cSAndrea Arcangeli 
2249b1b8d6SLorenzo Stoakes #include "vma.h"
2349b1b8d6SLorenzo Stoakes 
2421a92735SSagi Grimberg /* global SRCU for all MMs */
25dde8da6cSPaul E. McKenney DEFINE_STATIC_SRCU(srcu);
2621a92735SSagi Grimberg 
2723b68395SDaniel Vetter #ifdef CONFIG_LOCKDEP
2823b68395SDaniel Vetter struct lockdep_map __mmu_notifier_invalidate_range_start_map = {
2923b68395SDaniel Vetter 	.name = "mmu_notifier_invalidate_range_start"
3023b68395SDaniel Vetter };
3123b68395SDaniel Vetter #endif
3223b68395SDaniel Vetter 
33cddb8a5cSAndrea Arcangeli /*
34984cfe4eSJason Gunthorpe  * The mmu_notifier_subscriptions structure is allocated and installed in
35984cfe4eSJason Gunthorpe  * mm->notifier_subscriptions inside the mm_take_all_locks() protected
3656f434f4SJason Gunthorpe  * critical section and it's released only when mm_count reaches zero
3756f434f4SJason Gunthorpe  * in mmdrop().
3856f434f4SJason Gunthorpe  */
39984cfe4eSJason Gunthorpe struct mmu_notifier_subscriptions {
4056f434f4SJason Gunthorpe 	/* all mmu notifiers registered in this mm are queued in this list */
4156f434f4SJason Gunthorpe 	struct hlist_head list;
4299cb252fSJason Gunthorpe 	bool has_itree;
4356f434f4SJason Gunthorpe 	/* to serialize the list modifications and hlist_unhashed */
4456f434f4SJason Gunthorpe 	spinlock_t lock;
4599cb252fSJason Gunthorpe 	unsigned long invalidate_seq;
4699cb252fSJason Gunthorpe 	unsigned long active_invalidate_ranges;
4799cb252fSJason Gunthorpe 	struct rb_root_cached itree;
4899cb252fSJason Gunthorpe 	wait_queue_head_t wq;
4999cb252fSJason Gunthorpe 	struct hlist_head deferred_list;
5056f434f4SJason Gunthorpe };
5156f434f4SJason Gunthorpe 
5256f434f4SJason Gunthorpe /*
5399cb252fSJason Gunthorpe  * This is a collision-retry read-side/write-side 'lock', a lot like a
5499cb252fSJason Gunthorpe  * seqcount, however this allows multiple write-sides to hold it at
5599cb252fSJason Gunthorpe  * once. Conceptually the write side is protecting the values of the PTEs in
5699cb252fSJason Gunthorpe  * this mm, such that PTES cannot be read into SPTEs (shadow PTEs) while any
5799cb252fSJason Gunthorpe  * writer exists.
5899cb252fSJason Gunthorpe  *
5999cb252fSJason Gunthorpe  * Note that the core mm creates nested invalidate_range_start()/end() regions
6099cb252fSJason Gunthorpe  * within the same thread, and runs invalidate_range_start()/end() in parallel
6199cb252fSJason Gunthorpe  * on multiple CPUs. This is designed to not reduce concurrency or block
6299cb252fSJason Gunthorpe  * progress on the mm side.
6399cb252fSJason Gunthorpe  *
6499cb252fSJason Gunthorpe  * As a secondary function, holding the full write side also serves to prevent
6599cb252fSJason Gunthorpe  * writers for the itree, this is an optimization to avoid extra locking
6699cb252fSJason Gunthorpe  * during invalidate_range_start/end notifiers.
6799cb252fSJason Gunthorpe  *
6899cb252fSJason Gunthorpe  * The write side has two states, fully excluded:
6999cb252fSJason Gunthorpe  *  - mm->active_invalidate_ranges != 0
70984cfe4eSJason Gunthorpe  *  - subscriptions->invalidate_seq & 1 == True (odd)
7199cb252fSJason Gunthorpe  *  - some range on the mm_struct is being invalidated
7299cb252fSJason Gunthorpe  *  - the itree is not allowed to change
7399cb252fSJason Gunthorpe  *
7499cb252fSJason Gunthorpe  * And partially excluded:
7599cb252fSJason Gunthorpe  *  - mm->active_invalidate_ranges != 0
76984cfe4eSJason Gunthorpe  *  - subscriptions->invalidate_seq & 1 == False (even)
7799cb252fSJason Gunthorpe  *  - some range on the mm_struct is being invalidated
7899cb252fSJason Gunthorpe  *  - the itree is allowed to change
7999cb252fSJason Gunthorpe  *
80984cfe4eSJason Gunthorpe  * Operations on notifier_subscriptions->invalidate_seq (under spinlock):
8199cb252fSJason Gunthorpe  *    seq |= 1  # Begin writing
8299cb252fSJason Gunthorpe  *    seq++     # Release the writing state
8399cb252fSJason Gunthorpe  *    seq & 1   # True if a writer exists
8499cb252fSJason Gunthorpe  *
8599cb252fSJason Gunthorpe  * The later state avoids some expensive work on inv_end in the common case of
865292e24aSJason Gunthorpe  * no mmu_interval_notifier monitoring the VA.
8799cb252fSJason Gunthorpe  */
88984cfe4eSJason Gunthorpe static bool
mn_itree_is_invalidating(struct mmu_notifier_subscriptions * subscriptions)89984cfe4eSJason Gunthorpe mn_itree_is_invalidating(struct mmu_notifier_subscriptions *subscriptions)
9099cb252fSJason Gunthorpe {
91984cfe4eSJason Gunthorpe 	lockdep_assert_held(&subscriptions->lock);
92984cfe4eSJason Gunthorpe 	return subscriptions->invalidate_seq & 1;
9399cb252fSJason Gunthorpe }
9499cb252fSJason Gunthorpe 
9599cb252fSJason Gunthorpe static struct mmu_interval_notifier *
mn_itree_inv_start_range(struct mmu_notifier_subscriptions * subscriptions,const struct mmu_notifier_range * range,unsigned long * seq)96984cfe4eSJason Gunthorpe mn_itree_inv_start_range(struct mmu_notifier_subscriptions *subscriptions,
9799cb252fSJason Gunthorpe 			 const struct mmu_notifier_range *range,
9899cb252fSJason Gunthorpe 			 unsigned long *seq)
9999cb252fSJason Gunthorpe {
10099cb252fSJason Gunthorpe 	struct interval_tree_node *node;
10199cb252fSJason Gunthorpe 	struct mmu_interval_notifier *res = NULL;
10299cb252fSJason Gunthorpe 
103984cfe4eSJason Gunthorpe 	spin_lock(&subscriptions->lock);
104984cfe4eSJason Gunthorpe 	subscriptions->active_invalidate_ranges++;
105984cfe4eSJason Gunthorpe 	node = interval_tree_iter_first(&subscriptions->itree, range->start,
10699cb252fSJason Gunthorpe 					range->end - 1);
10799cb252fSJason Gunthorpe 	if (node) {
108984cfe4eSJason Gunthorpe 		subscriptions->invalidate_seq |= 1;
10999cb252fSJason Gunthorpe 		res = container_of(node, struct mmu_interval_notifier,
11099cb252fSJason Gunthorpe 				   interval_tree);
11199cb252fSJason Gunthorpe 	}
11299cb252fSJason Gunthorpe 
113984cfe4eSJason Gunthorpe 	*seq = subscriptions->invalidate_seq;
114984cfe4eSJason Gunthorpe 	spin_unlock(&subscriptions->lock);
11599cb252fSJason Gunthorpe 	return res;
11699cb252fSJason Gunthorpe }
11799cb252fSJason Gunthorpe 
11899cb252fSJason Gunthorpe static struct mmu_interval_notifier *
mn_itree_inv_next(struct mmu_interval_notifier * interval_sub,const struct mmu_notifier_range * range)1195292e24aSJason Gunthorpe mn_itree_inv_next(struct mmu_interval_notifier *interval_sub,
12099cb252fSJason Gunthorpe 		  const struct mmu_notifier_range *range)
12199cb252fSJason Gunthorpe {
12299cb252fSJason Gunthorpe 	struct interval_tree_node *node;
12399cb252fSJason Gunthorpe 
1245292e24aSJason Gunthorpe 	node = interval_tree_iter_next(&interval_sub->interval_tree,
1255292e24aSJason Gunthorpe 				       range->start, range->end - 1);
12699cb252fSJason Gunthorpe 	if (!node)
12799cb252fSJason Gunthorpe 		return NULL;
12899cb252fSJason Gunthorpe 	return container_of(node, struct mmu_interval_notifier, interval_tree);
12999cb252fSJason Gunthorpe }
13099cb252fSJason Gunthorpe 
mn_itree_inv_end(struct mmu_notifier_subscriptions * subscriptions)131984cfe4eSJason Gunthorpe static void mn_itree_inv_end(struct mmu_notifier_subscriptions *subscriptions)
13299cb252fSJason Gunthorpe {
1335292e24aSJason Gunthorpe 	struct mmu_interval_notifier *interval_sub;
13499cb252fSJason Gunthorpe 	struct hlist_node *next;
13599cb252fSJason Gunthorpe 
136984cfe4eSJason Gunthorpe 	spin_lock(&subscriptions->lock);
137984cfe4eSJason Gunthorpe 	if (--subscriptions->active_invalidate_ranges ||
138984cfe4eSJason Gunthorpe 	    !mn_itree_is_invalidating(subscriptions)) {
139984cfe4eSJason Gunthorpe 		spin_unlock(&subscriptions->lock);
14099cb252fSJason Gunthorpe 		return;
14199cb252fSJason Gunthorpe 	}
14299cb252fSJason Gunthorpe 
14399cb252fSJason Gunthorpe 	/* Make invalidate_seq even */
144984cfe4eSJason Gunthorpe 	subscriptions->invalidate_seq++;
14599cb252fSJason Gunthorpe 
14699cb252fSJason Gunthorpe 	/*
14799cb252fSJason Gunthorpe 	 * The inv_end incorporates a deferred mechanism like rtnl_unlock().
14899cb252fSJason Gunthorpe 	 * Adds and removes are queued until the final inv_end happens then
14999cb252fSJason Gunthorpe 	 * they are progressed. This arrangement for tree updates is used to
15099cb252fSJason Gunthorpe 	 * avoid using a blocking lock during invalidate_range_start.
15199cb252fSJason Gunthorpe 	 */
1525292e24aSJason Gunthorpe 	hlist_for_each_entry_safe(interval_sub, next,
1535292e24aSJason Gunthorpe 				  &subscriptions->deferred_list,
15499cb252fSJason Gunthorpe 				  deferred_item) {
1555292e24aSJason Gunthorpe 		if (RB_EMPTY_NODE(&interval_sub->interval_tree.rb))
1565292e24aSJason Gunthorpe 			interval_tree_insert(&interval_sub->interval_tree,
157984cfe4eSJason Gunthorpe 					     &subscriptions->itree);
15899cb252fSJason Gunthorpe 		else
1595292e24aSJason Gunthorpe 			interval_tree_remove(&interval_sub->interval_tree,
160984cfe4eSJason Gunthorpe 					     &subscriptions->itree);
1615292e24aSJason Gunthorpe 		hlist_del(&interval_sub->deferred_item);
16299cb252fSJason Gunthorpe 	}
163984cfe4eSJason Gunthorpe 	spin_unlock(&subscriptions->lock);
16499cb252fSJason Gunthorpe 
165984cfe4eSJason Gunthorpe 	wake_up_all(&subscriptions->wq);
16699cb252fSJason Gunthorpe }
16799cb252fSJason Gunthorpe 
16899cb252fSJason Gunthorpe /**
16999cb252fSJason Gunthorpe  * mmu_interval_read_begin - Begin a read side critical section against a VA
17099cb252fSJason Gunthorpe  *                           range
171d49653f3SKrzysztof Kozlowski  * @interval_sub: The interval subscription
17299cb252fSJason Gunthorpe  *
17399cb252fSJason Gunthorpe  * mmu_iterval_read_begin()/mmu_iterval_read_retry() implement a
1745292e24aSJason Gunthorpe  * collision-retry scheme similar to seqcount for the VA range under
1755292e24aSJason Gunthorpe  * subscription. If the mm invokes invalidation during the critical section
1765292e24aSJason Gunthorpe  * then mmu_interval_read_retry() will return true.
17799cb252fSJason Gunthorpe  *
17899cb252fSJason Gunthorpe  * This is useful to obtain shadow PTEs where teardown or setup of the SPTEs
17999cb252fSJason Gunthorpe  * require a blocking context.  The critical region formed by this can sleep,
18099cb252fSJason Gunthorpe  * and the required 'user_lock' can also be a sleeping lock.
18199cb252fSJason Gunthorpe  *
18299cb252fSJason Gunthorpe  * The caller is required to provide a 'user_lock' to serialize both teardown
18399cb252fSJason Gunthorpe  * and setup.
18499cb252fSJason Gunthorpe  *
18599cb252fSJason Gunthorpe  * The return value should be passed to mmu_interval_read_retry().
18699cb252fSJason Gunthorpe  */
1875292e24aSJason Gunthorpe unsigned long
mmu_interval_read_begin(struct mmu_interval_notifier * interval_sub)1885292e24aSJason Gunthorpe mmu_interval_read_begin(struct mmu_interval_notifier *interval_sub)
18999cb252fSJason Gunthorpe {
190984cfe4eSJason Gunthorpe 	struct mmu_notifier_subscriptions *subscriptions =
1915292e24aSJason Gunthorpe 		interval_sub->mm->notifier_subscriptions;
19299cb252fSJason Gunthorpe 	unsigned long seq;
19399cb252fSJason Gunthorpe 	bool is_invalidating;
19499cb252fSJason Gunthorpe 
19599cb252fSJason Gunthorpe 	/*
1965292e24aSJason Gunthorpe 	 * If the subscription has a different seq value under the user_lock
1975292e24aSJason Gunthorpe 	 * than we started with then it has collided.
19899cb252fSJason Gunthorpe 	 *
1995292e24aSJason Gunthorpe 	 * If the subscription currently has the same seq value as the
2005292e24aSJason Gunthorpe 	 * subscriptions seq, then it is currently between
2015292e24aSJason Gunthorpe 	 * invalidate_start/end and is colliding.
20299cb252fSJason Gunthorpe 	 *
20399cb252fSJason Gunthorpe 	 * The locking looks broadly like this:
20457b037dbSAlistair Popple 	 *   mn_itree_inv_start():                 mmu_interval_read_begin():
20599cb252fSJason Gunthorpe 	 *                                         spin_lock
2065292e24aSJason Gunthorpe 	 *                                          seq = READ_ONCE(interval_sub->invalidate_seq);
207984cfe4eSJason Gunthorpe 	 *                                          seq == subs->invalidate_seq
20899cb252fSJason Gunthorpe 	 *                                         spin_unlock
20999cb252fSJason Gunthorpe 	 *    spin_lock
210984cfe4eSJason Gunthorpe 	 *     seq = ++subscriptions->invalidate_seq
21199cb252fSJason Gunthorpe 	 *    spin_unlock
21257b037dbSAlistair Popple 	 *     op->invalidate():
21399cb252fSJason Gunthorpe 	 *       user_lock
21499cb252fSJason Gunthorpe 	 *        mmu_interval_set_seq()
2155292e24aSJason Gunthorpe 	 *         interval_sub->invalidate_seq = seq
21699cb252fSJason Gunthorpe 	 *       user_unlock
21799cb252fSJason Gunthorpe 	 *
21899cb252fSJason Gunthorpe 	 *                          [Required: mmu_interval_read_retry() == true]
21999cb252fSJason Gunthorpe 	 *
22099cb252fSJason Gunthorpe 	 *   mn_itree_inv_end():
22199cb252fSJason Gunthorpe 	 *    spin_lock
222984cfe4eSJason Gunthorpe 	 *     seq = ++subscriptions->invalidate_seq
22399cb252fSJason Gunthorpe 	 *    spin_unlock
22499cb252fSJason Gunthorpe 	 *
22599cb252fSJason Gunthorpe 	 *                                        user_lock
22699cb252fSJason Gunthorpe 	 *                                         mmu_interval_read_retry():
2275292e24aSJason Gunthorpe 	 *                                          interval_sub->invalidate_seq != seq
22899cb252fSJason Gunthorpe 	 *                                        user_unlock
22999cb252fSJason Gunthorpe 	 *
23099cb252fSJason Gunthorpe 	 * Barriers are not needed here as any races here are closed by an
23199cb252fSJason Gunthorpe 	 * eventual mmu_interval_read_retry(), which provides a barrier via the
23299cb252fSJason Gunthorpe 	 * user_lock.
23399cb252fSJason Gunthorpe 	 */
234984cfe4eSJason Gunthorpe 	spin_lock(&subscriptions->lock);
23599cb252fSJason Gunthorpe 	/* Pairs with the WRITE_ONCE in mmu_interval_set_seq() */
2365292e24aSJason Gunthorpe 	seq = READ_ONCE(interval_sub->invalidate_seq);
237984cfe4eSJason Gunthorpe 	is_invalidating = seq == subscriptions->invalidate_seq;
238984cfe4eSJason Gunthorpe 	spin_unlock(&subscriptions->lock);
23999cb252fSJason Gunthorpe 
24099cb252fSJason Gunthorpe 	/*
2415292e24aSJason Gunthorpe 	 * interval_sub->invalidate_seq must always be set to an odd value via
24299cb252fSJason Gunthorpe 	 * mmu_interval_set_seq() using the provided cur_seq from
24399cb252fSJason Gunthorpe 	 * mn_itree_inv_start_range(). This ensures that if seq does wrap we
24499cb252fSJason Gunthorpe 	 * will always clear the below sleep in some reasonable time as
245984cfe4eSJason Gunthorpe 	 * subscriptions->invalidate_seq is even in the idle state.
24699cb252fSJason Gunthorpe 	 */
24799cb252fSJason Gunthorpe 	lock_map_acquire(&__mmu_notifier_invalidate_range_start_map);
24899cb252fSJason Gunthorpe 	lock_map_release(&__mmu_notifier_invalidate_range_start_map);
24999cb252fSJason Gunthorpe 	if (is_invalidating)
250984cfe4eSJason Gunthorpe 		wait_event(subscriptions->wq,
251984cfe4eSJason Gunthorpe 			   READ_ONCE(subscriptions->invalidate_seq) != seq);
25299cb252fSJason Gunthorpe 
25399cb252fSJason Gunthorpe 	/*
25499cb252fSJason Gunthorpe 	 * Notice that mmu_interval_read_retry() can already be true at this
25599cb252fSJason Gunthorpe 	 * point, avoiding loops here allows the caller to provide a global
25699cb252fSJason Gunthorpe 	 * time bound.
25799cb252fSJason Gunthorpe 	 */
25899cb252fSJason Gunthorpe 
25999cb252fSJason Gunthorpe 	return seq;
26099cb252fSJason Gunthorpe }
26199cb252fSJason Gunthorpe EXPORT_SYMBOL_GPL(mmu_interval_read_begin);
26299cb252fSJason Gunthorpe 
mn_itree_finish_pass(struct llist_head * finish_passes)2637aba71dbSThomas Hellström static void mn_itree_finish_pass(struct llist_head *finish_passes)
2647aba71dbSThomas Hellström {
2657aba71dbSThomas Hellström 	struct llist_node *first = llist_reverse_order(__llist_del_all(finish_passes));
2667aba71dbSThomas Hellström 	struct mmu_interval_notifier_finish *f, *next;
2677aba71dbSThomas Hellström 
2687aba71dbSThomas Hellström 	llist_for_each_entry_safe(f, next, first, link)
2697aba71dbSThomas Hellström 		f->notifier->ops->invalidate_finish(f);
2707aba71dbSThomas Hellström }
2717aba71dbSThomas Hellström 
mn_itree_release(struct mmu_notifier_subscriptions * subscriptions,struct mm_struct * mm)272984cfe4eSJason Gunthorpe static void mn_itree_release(struct mmu_notifier_subscriptions *subscriptions,
27399cb252fSJason Gunthorpe 			     struct mm_struct *mm)
27499cb252fSJason Gunthorpe {
27599cb252fSJason Gunthorpe 	struct mmu_notifier_range range = {
27699cb252fSJason Gunthorpe 		.flags = MMU_NOTIFIER_RANGE_BLOCKABLE,
27799cb252fSJason Gunthorpe 		.event = MMU_NOTIFY_RELEASE,
27899cb252fSJason Gunthorpe 		.mm = mm,
27999cb252fSJason Gunthorpe 		.start = 0,
28099cb252fSJason Gunthorpe 		.end = ULONG_MAX,
28199cb252fSJason Gunthorpe 	};
2825292e24aSJason Gunthorpe 	struct mmu_interval_notifier *interval_sub;
2837aba71dbSThomas Hellström 	LLIST_HEAD(finish_passes);
28499cb252fSJason Gunthorpe 	unsigned long cur_seq;
28599cb252fSJason Gunthorpe 	bool ret;
28699cb252fSJason Gunthorpe 
2875292e24aSJason Gunthorpe 	for (interval_sub =
2885292e24aSJason Gunthorpe 		     mn_itree_inv_start_range(subscriptions, &range, &cur_seq);
2895292e24aSJason Gunthorpe 	     interval_sub;
2905292e24aSJason Gunthorpe 	     interval_sub = mn_itree_inv_next(interval_sub, &range)) {
2917aba71dbSThomas Hellström 		if (interval_sub->ops->invalidate_start) {
2927aba71dbSThomas Hellström 			struct mmu_interval_notifier_finish *finish = NULL;
2937aba71dbSThomas Hellström 
2947aba71dbSThomas Hellström 			ret = interval_sub->ops->invalidate_start(interval_sub,
2957aba71dbSThomas Hellström 								  &range,
2967aba71dbSThomas Hellström 								  cur_seq,
2977aba71dbSThomas Hellström 								  &finish);
2987aba71dbSThomas Hellström 			if (ret && finish) {
2997aba71dbSThomas Hellström 				finish->notifier = interval_sub;
3007aba71dbSThomas Hellström 				__llist_add(&finish->link, &finish_passes);
3017aba71dbSThomas Hellström 			}
3027aba71dbSThomas Hellström 
3037aba71dbSThomas Hellström 		} else {
3047aba71dbSThomas Hellström 			ret = interval_sub->ops->invalidate(interval_sub,
3057aba71dbSThomas Hellström 							    &range,
3065292e24aSJason Gunthorpe 							    cur_seq);
3077aba71dbSThomas Hellström 		}
30899cb252fSJason Gunthorpe 		WARN_ON(!ret);
30999cb252fSJason Gunthorpe 	}
31099cb252fSJason Gunthorpe 
3117aba71dbSThomas Hellström 	mn_itree_finish_pass(&finish_passes);
312984cfe4eSJason Gunthorpe 	mn_itree_inv_end(subscriptions);
31399cb252fSJason Gunthorpe }
31499cb252fSJason Gunthorpe 
31599cb252fSJason Gunthorpe /*
316cddb8a5cSAndrea Arcangeli  * This function can't run concurrently against mmu_notifier_register
317cddb8a5cSAndrea Arcangeli  * because mm->mm_users > 0 during mmu_notifier_register and exit_mmap
318cddb8a5cSAndrea Arcangeli  * runs with mm_users == 0. Other tasks may still invoke mmu notifiers
319cddb8a5cSAndrea Arcangeli  * in parallel despite there being no task using this mm any more,
320cddb8a5cSAndrea Arcangeli  * through the vmas outside of the exit_mmap context, such as with
321cddb8a5cSAndrea Arcangeli  * vmtruncate. This serializes against mmu_notifier_unregister with
322984cfe4eSJason Gunthorpe  * the notifier_subscriptions->lock in addition to SRCU and it serializes
323984cfe4eSJason Gunthorpe  * against the other mmu notifiers with SRCU. struct mmu_notifier_subscriptions
324cddb8a5cSAndrea Arcangeli  * can't go away from under us as exit_mmap holds an mm_count pin
325cddb8a5cSAndrea Arcangeli  * itself.
326cddb8a5cSAndrea Arcangeli  */
mn_hlist_release(struct mmu_notifier_subscriptions * subscriptions,struct mm_struct * mm)327984cfe4eSJason Gunthorpe static void mn_hlist_release(struct mmu_notifier_subscriptions *subscriptions,
32899cb252fSJason Gunthorpe 			     struct mm_struct *mm)
329cddb8a5cSAndrea Arcangeli {
3301991722aSJason Gunthorpe 	struct mmu_notifier *subscription;
33121a92735SSagi Grimberg 	int id;
3323ad3d901SXiao Guangrong 
3333ad3d901SXiao Guangrong 	/*
334d34883d4SXiao Guangrong 	 * SRCU here will block mmu_notifier_unregister until
335d34883d4SXiao Guangrong 	 * ->release returns.
3363ad3d901SXiao Guangrong 	 */
33721a92735SSagi Grimberg 	id = srcu_read_lock(&srcu);
338b0fbe8c3SLi RongQing 	hlist_for_each_entry_srcu(subscription, &subscriptions->list, hlist,
33963886badSQian Cai 				 srcu_read_lock_held(&srcu))
340d34883d4SXiao Guangrong 		/*
341d34883d4SXiao Guangrong 		 * If ->release runs before mmu_notifier_unregister it must be
342d34883d4SXiao Guangrong 		 * handled, as it's the only way for the driver to flush all
343d34883d4SXiao Guangrong 		 * existing sptes and stop the driver from establishing any more
344d34883d4SXiao Guangrong 		 * sptes before all the pages in the mm are freed.
345d34883d4SXiao Guangrong 		 */
3461991722aSJason Gunthorpe 		if (subscription->ops->release)
3471991722aSJason Gunthorpe 			subscription->ops->release(subscription, mm);
348d34883d4SXiao Guangrong 
349984cfe4eSJason Gunthorpe 	spin_lock(&subscriptions->lock);
350984cfe4eSJason Gunthorpe 	while (unlikely(!hlist_empty(&subscriptions->list))) {
3511991722aSJason Gunthorpe 		subscription = hlist_entry(subscriptions->list.first,
3521991722aSJason Gunthorpe 					   struct mmu_notifier, hlist);
353cddb8a5cSAndrea Arcangeli 		/*
354d34883d4SXiao Guangrong 		 * We arrived before mmu_notifier_unregister so
355d34883d4SXiao Guangrong 		 * mmu_notifier_unregister will do nothing other than to wait
356d34883d4SXiao Guangrong 		 * for ->release to finish and for mmu_notifier_unregister to
357d34883d4SXiao Guangrong 		 * return.
358cddb8a5cSAndrea Arcangeli 		 */
3591991722aSJason Gunthorpe 		hlist_del_init_rcu(&subscription->hlist);
360cddb8a5cSAndrea Arcangeli 	}
361984cfe4eSJason Gunthorpe 	spin_unlock(&subscriptions->lock);
362b972216eSPeter Zijlstra 	srcu_read_unlock(&srcu, id);
363cddb8a5cSAndrea Arcangeli 
364cddb8a5cSAndrea Arcangeli 	/*
365d34883d4SXiao Guangrong 	 * synchronize_srcu here prevents mmu_notifier_release from returning to
366d34883d4SXiao Guangrong 	 * exit_mmap (which would proceed with freeing all pages in the mm)
367d34883d4SXiao Guangrong 	 * until the ->release method returns, if it was invoked by
368d34883d4SXiao Guangrong 	 * mmu_notifier_unregister.
369d34883d4SXiao Guangrong 	 *
370984cfe4eSJason Gunthorpe 	 * The notifier_subscriptions can't go away from under us because
371984cfe4eSJason Gunthorpe 	 * one mm_count is held by exit_mmap.
372cddb8a5cSAndrea Arcangeli 	 */
37321a92735SSagi Grimberg 	synchronize_srcu(&srcu);
374cddb8a5cSAndrea Arcangeli }
375cddb8a5cSAndrea Arcangeli 
__mmu_notifier_release(struct mm_struct * mm)37699cb252fSJason Gunthorpe void __mmu_notifier_release(struct mm_struct *mm)
37799cb252fSJason Gunthorpe {
378984cfe4eSJason Gunthorpe 	struct mmu_notifier_subscriptions *subscriptions =
379984cfe4eSJason Gunthorpe 		mm->notifier_subscriptions;
38099cb252fSJason Gunthorpe 
381984cfe4eSJason Gunthorpe 	if (subscriptions->has_itree)
382984cfe4eSJason Gunthorpe 		mn_itree_release(subscriptions, mm);
38399cb252fSJason Gunthorpe 
384984cfe4eSJason Gunthorpe 	if (!hlist_empty(&subscriptions->list))
385984cfe4eSJason Gunthorpe 		mn_hlist_release(subscriptions, mm);
38699cb252fSJason Gunthorpe }
38799cb252fSJason Gunthorpe 
388cddb8a5cSAndrea Arcangeli /*
389cddb8a5cSAndrea Arcangeli  * If no young bitflag is supported by the hardware, ->clear_flush_young can
390cddb8a5cSAndrea Arcangeli  * unmap the address and return 1 or 0 depending if the mapping previously
391cddb8a5cSAndrea Arcangeli  * existed or not.
392cddb8a5cSAndrea Arcangeli  */
__mmu_notifier_clear_flush_young(struct mm_struct * mm,unsigned long start,unsigned long end)393*1fc7dc67SBaolin Wang bool __mmu_notifier_clear_flush_young(struct mm_struct *mm,
394*1fc7dc67SBaolin Wang 		unsigned long start, unsigned long end)
395cddb8a5cSAndrea Arcangeli {
3961991722aSJason Gunthorpe 	struct mmu_notifier *subscription;
397*1fc7dc67SBaolin Wang 	bool young = false;
398*1fc7dc67SBaolin Wang 	int id;
399cddb8a5cSAndrea Arcangeli 
40021a92735SSagi Grimberg 	id = srcu_read_lock(&srcu);
401b0fbe8c3SLi RongQing 	hlist_for_each_entry_srcu(subscription,
40263886badSQian Cai 				 &mm->notifier_subscriptions->list, hlist,
40363886badSQian Cai 				 srcu_read_lock_held(&srcu)) {
4041991722aSJason Gunthorpe 		if (subscription->ops->clear_flush_young)
4051991722aSJason Gunthorpe 			young |= subscription->ops->clear_flush_young(
4061991722aSJason Gunthorpe 				subscription, mm, start, end);
407cddb8a5cSAndrea Arcangeli 	}
40821a92735SSagi Grimberg 	srcu_read_unlock(&srcu, id);
409cddb8a5cSAndrea Arcangeli 
410cddb8a5cSAndrea Arcangeli 	return young;
411cddb8a5cSAndrea Arcangeli }
412cddb8a5cSAndrea Arcangeli 
__mmu_notifier_clear_young(struct mm_struct * mm,unsigned long start,unsigned long end)413*1fc7dc67SBaolin Wang bool __mmu_notifier_clear_young(struct mm_struct *mm,
414*1fc7dc67SBaolin Wang 		unsigned long start, unsigned long end)
4151d7715c6SVladimir Davydov {
4161991722aSJason Gunthorpe 	struct mmu_notifier *subscription;
417*1fc7dc67SBaolin Wang 	bool young = false;
418*1fc7dc67SBaolin Wang 	int id;
4191d7715c6SVladimir Davydov 
4201d7715c6SVladimir Davydov 	id = srcu_read_lock(&srcu);
421b0fbe8c3SLi RongQing 	hlist_for_each_entry_srcu(subscription,
42263886badSQian Cai 				 &mm->notifier_subscriptions->list, hlist,
42363886badSQian Cai 				 srcu_read_lock_held(&srcu)) {
4241991722aSJason Gunthorpe 		if (subscription->ops->clear_young)
4251991722aSJason Gunthorpe 			young |= subscription->ops->clear_young(subscription,
4261991722aSJason Gunthorpe 								mm, start, end);
4271d7715c6SVladimir Davydov 	}
4281d7715c6SVladimir Davydov 	srcu_read_unlock(&srcu, id);
4291d7715c6SVladimir Davydov 
4301d7715c6SVladimir Davydov 	return young;
4311d7715c6SVladimir Davydov }
4321d7715c6SVladimir Davydov 
__mmu_notifier_test_young(struct mm_struct * mm,unsigned long address)433*1fc7dc67SBaolin Wang bool __mmu_notifier_test_young(struct mm_struct *mm,
4348ee53820SAndrea Arcangeli 		unsigned long address)
4358ee53820SAndrea Arcangeli {
4361991722aSJason Gunthorpe 	struct mmu_notifier *subscription;
437*1fc7dc67SBaolin Wang 	bool young = false;
438*1fc7dc67SBaolin Wang 	int id;
4398ee53820SAndrea Arcangeli 
44021a92735SSagi Grimberg 	id = srcu_read_lock(&srcu);
441b0fbe8c3SLi RongQing 	hlist_for_each_entry_srcu(subscription,
44263886badSQian Cai 				 &mm->notifier_subscriptions->list, hlist,
44363886badSQian Cai 				 srcu_read_lock_held(&srcu)) {
4441991722aSJason Gunthorpe 		if (subscription->ops->test_young) {
4451991722aSJason Gunthorpe 			young = subscription->ops->test_young(subscription, mm,
4461991722aSJason Gunthorpe 							      address);
4478ee53820SAndrea Arcangeli 			if (young)
4488ee53820SAndrea Arcangeli 				break;
4498ee53820SAndrea Arcangeli 		}
4508ee53820SAndrea Arcangeli 	}
45121a92735SSagi Grimberg 	srcu_read_unlock(&srcu, id);
4528ee53820SAndrea Arcangeli 
4538ee53820SAndrea Arcangeli 	return young;
4548ee53820SAndrea Arcangeli }
4558ee53820SAndrea Arcangeli 
mn_itree_invalidate(struct mmu_notifier_subscriptions * subscriptions,const struct mmu_notifier_range * range)456984cfe4eSJason Gunthorpe static int mn_itree_invalidate(struct mmu_notifier_subscriptions *subscriptions,
45799cb252fSJason Gunthorpe 			       const struct mmu_notifier_range *range)
45899cb252fSJason Gunthorpe {
4595292e24aSJason Gunthorpe 	struct mmu_interval_notifier *interval_sub;
4607aba71dbSThomas Hellström 	LLIST_HEAD(finish_passes);
46199cb252fSJason Gunthorpe 	unsigned long cur_seq;
4627aba71dbSThomas Hellström 	int err = 0;
46399cb252fSJason Gunthorpe 
4645292e24aSJason Gunthorpe 	for (interval_sub =
4655292e24aSJason Gunthorpe 		     mn_itree_inv_start_range(subscriptions, range, &cur_seq);
4665292e24aSJason Gunthorpe 	     interval_sub;
4675292e24aSJason Gunthorpe 	     interval_sub = mn_itree_inv_next(interval_sub, range)) {
46899cb252fSJason Gunthorpe 		bool ret;
46999cb252fSJason Gunthorpe 
4707aba71dbSThomas Hellström 		if (interval_sub->ops->invalidate_start) {
4717aba71dbSThomas Hellström 			struct mmu_interval_notifier_finish *finish = NULL;
4727aba71dbSThomas Hellström 
4737aba71dbSThomas Hellström 			ret = interval_sub->ops->invalidate_start(interval_sub,
4747aba71dbSThomas Hellström 								  range,
4757aba71dbSThomas Hellström 								  cur_seq,
4767aba71dbSThomas Hellström 								  &finish);
4777aba71dbSThomas Hellström 			if (ret && finish) {
4787aba71dbSThomas Hellström 				finish->notifier = interval_sub;
4797aba71dbSThomas Hellström 				__llist_add(&finish->link, &finish_passes);
4807aba71dbSThomas Hellström 			}
4817aba71dbSThomas Hellström 
4827aba71dbSThomas Hellström 		} else {
4837aba71dbSThomas Hellström 			ret = interval_sub->ops->invalidate(interval_sub,
4847aba71dbSThomas Hellström 							    range,
4855292e24aSJason Gunthorpe 							    cur_seq);
4867aba71dbSThomas Hellström 		}
48799cb252fSJason Gunthorpe 		if (!ret) {
48899cb252fSJason Gunthorpe 			if (WARN_ON(mmu_notifier_range_blockable(range)))
48999cb252fSJason Gunthorpe 				continue;
4907aba71dbSThomas Hellström 			err = -EAGAIN;
4917aba71dbSThomas Hellström 			break;
49299cb252fSJason Gunthorpe 		}
49399cb252fSJason Gunthorpe 	}
49499cb252fSJason Gunthorpe 
4957aba71dbSThomas Hellström 	mn_itree_finish_pass(&finish_passes);
4967aba71dbSThomas Hellström 
49799cb252fSJason Gunthorpe 	/*
49899cb252fSJason Gunthorpe 	 * On -EAGAIN the non-blocking caller is not allowed to call
49999cb252fSJason Gunthorpe 	 * invalidate_range_end()
50099cb252fSJason Gunthorpe 	 */
5017aba71dbSThomas Hellström 	if (err)
502984cfe4eSJason Gunthorpe 		mn_itree_inv_end(subscriptions);
5037aba71dbSThomas Hellström 
5047aba71dbSThomas Hellström 	return err;
50599cb252fSJason Gunthorpe }
50699cb252fSJason Gunthorpe 
mn_hlist_invalidate_range_start(struct mmu_notifier_subscriptions * subscriptions,struct mmu_notifier_range * range)507984cfe4eSJason Gunthorpe static int mn_hlist_invalidate_range_start(
508984cfe4eSJason Gunthorpe 	struct mmu_notifier_subscriptions *subscriptions,
50999cb252fSJason Gunthorpe 	struct mmu_notifier_range *range)
510cddb8a5cSAndrea Arcangeli {
5111991722aSJason Gunthorpe 	struct mmu_notifier *subscription;
51293065ac7SMichal Hocko 	int ret = 0;
51321a92735SSagi Grimberg 	int id;
514cddb8a5cSAndrea Arcangeli 
51521a92735SSagi Grimberg 	id = srcu_read_lock(&srcu);
516b0fbe8c3SLi RongQing 	hlist_for_each_entry_srcu(subscription, &subscriptions->list, hlist,
51763886badSQian Cai 				 srcu_read_lock_held(&srcu)) {
5181991722aSJason Gunthorpe 		const struct mmu_notifier_ops *ops = subscription->ops;
5191991722aSJason Gunthorpe 
5201991722aSJason Gunthorpe 		if (ops->invalidate_range_start) {
521ba170f76SDaniel Vetter 			int _ret;
522ba170f76SDaniel Vetter 
523ba170f76SDaniel Vetter 			if (!mmu_notifier_range_blockable(range))
524ba170f76SDaniel Vetter 				non_block_start();
5251991722aSJason Gunthorpe 			_ret = ops->invalidate_range_start(subscription, range);
526ba170f76SDaniel Vetter 			if (!mmu_notifier_range_blockable(range))
527ba170f76SDaniel Vetter 				non_block_end();
52893065ac7SMichal Hocko 			if (_ret) {
52993065ac7SMichal Hocko 				pr_info("%pS callback failed with %d in %sblockable context.\n",
5301991722aSJason Gunthorpe 					ops->invalidate_range_start, _ret,
5311991722aSJason Gunthorpe 					!mmu_notifier_range_blockable(range) ?
5321991722aSJason Gunthorpe 						"non-" :
5331991722aSJason Gunthorpe 						"");
5348402ce61SDaniel Vetter 				WARN_ON(mmu_notifier_range_blockable(range) ||
535df2ec764SJason Gunthorpe 					_ret != -EAGAIN);
536c2655835SSean Christopherson 				/*
537c2655835SSean Christopherson 				 * We call all the notifiers on any EAGAIN,
538c2655835SSean Christopherson 				 * there is no way for a notifier to know if
539c2655835SSean Christopherson 				 * its start method failed, thus a start that
540c2655835SSean Christopherson 				 * does EAGAIN can't also do end.
541c2655835SSean Christopherson 				 */
542c2655835SSean Christopherson 				WARN_ON(ops->invalidate_range_end);
54393065ac7SMichal Hocko 				ret = _ret;
54493065ac7SMichal Hocko 			}
54593065ac7SMichal Hocko 		}
546cddb8a5cSAndrea Arcangeli 	}
547c2655835SSean Christopherson 
548c2655835SSean Christopherson 	if (ret) {
549c2655835SSean Christopherson 		/*
550c2655835SSean Christopherson 		 * Must be non-blocking to get here.  If there are multiple
551c2655835SSean Christopherson 		 * notifiers and one or more failed start, any that succeeded
552c2655835SSean Christopherson 		 * start are expecting their end to be called.  Do so now.
553c2655835SSean Christopherson 		 */
554b0fbe8c3SLi RongQing 		hlist_for_each_entry_srcu(subscription, &subscriptions->list,
555c2655835SSean Christopherson 					 hlist, srcu_read_lock_held(&srcu)) {
556c2655835SSean Christopherson 			if (!subscription->ops->invalidate_range_end)
557c2655835SSean Christopherson 				continue;
558c2655835SSean Christopherson 
559c2655835SSean Christopherson 			subscription->ops->invalidate_range_end(subscription,
560c2655835SSean Christopherson 								range);
561c2655835SSean Christopherson 		}
562c2655835SSean Christopherson 	}
56321a92735SSagi Grimberg 	srcu_read_unlock(&srcu, id);
56493065ac7SMichal Hocko 
56593065ac7SMichal Hocko 	return ret;
566cddb8a5cSAndrea Arcangeli }
567cddb8a5cSAndrea Arcangeli 
__mmu_notifier_invalidate_range_start(struct mmu_notifier_range * range)56899cb252fSJason Gunthorpe int __mmu_notifier_invalidate_range_start(struct mmu_notifier_range *range)
56999cb252fSJason Gunthorpe {
570984cfe4eSJason Gunthorpe 	struct mmu_notifier_subscriptions *subscriptions =
571984cfe4eSJason Gunthorpe 		range->mm->notifier_subscriptions;
57299cb252fSJason Gunthorpe 	int ret;
57399cb252fSJason Gunthorpe 
574984cfe4eSJason Gunthorpe 	if (subscriptions->has_itree) {
575984cfe4eSJason Gunthorpe 		ret = mn_itree_invalidate(subscriptions, range);
57699cb252fSJason Gunthorpe 		if (ret)
57799cb252fSJason Gunthorpe 			return ret;
57899cb252fSJason Gunthorpe 	}
579984cfe4eSJason Gunthorpe 	if (!hlist_empty(&subscriptions->list))
580984cfe4eSJason Gunthorpe 		return mn_hlist_invalidate_range_start(subscriptions, range);
58199cb252fSJason Gunthorpe 	return 0;
58299cb252fSJason Gunthorpe }
58399cb252fSJason Gunthorpe 
584984cfe4eSJason Gunthorpe static void
mn_hlist_invalidate_end(struct mmu_notifier_subscriptions * subscriptions,struct mmu_notifier_range * range)585984cfe4eSJason Gunthorpe mn_hlist_invalidate_end(struct mmu_notifier_subscriptions *subscriptions,
586ec8832d0SAlistair Popple 			struct mmu_notifier_range *range)
587cddb8a5cSAndrea Arcangeli {
5881991722aSJason Gunthorpe 	struct mmu_notifier *subscription;
58921a92735SSagi Grimberg 	int id;
590cddb8a5cSAndrea Arcangeli 
59121a92735SSagi Grimberg 	id = srcu_read_lock(&srcu);
592b0fbe8c3SLi RongQing 	hlist_for_each_entry_srcu(subscription, &subscriptions->list, hlist,
59363886badSQian Cai 				 srcu_read_lock_held(&srcu)) {
5941991722aSJason Gunthorpe 		if (subscription->ops->invalidate_range_end) {
595ba170f76SDaniel Vetter 			if (!mmu_notifier_range_blockable(range))
596ba170f76SDaniel Vetter 				non_block_start();
5971991722aSJason Gunthorpe 			subscription->ops->invalidate_range_end(subscription,
5981991722aSJason Gunthorpe 								range);
599ba170f76SDaniel Vetter 			if (!mmu_notifier_range_blockable(range))
600ba170f76SDaniel Vetter 				non_block_end();
601ba170f76SDaniel Vetter 		}
602cddb8a5cSAndrea Arcangeli 	}
60321a92735SSagi Grimberg 	srcu_read_unlock(&srcu, id);
60499cb252fSJason Gunthorpe }
60599cb252fSJason Gunthorpe 
__mmu_notifier_invalidate_range_end(struct mmu_notifier_range * range)606ec8832d0SAlistair Popple void __mmu_notifier_invalidate_range_end(struct mmu_notifier_range *range)
60799cb252fSJason Gunthorpe {
608984cfe4eSJason Gunthorpe 	struct mmu_notifier_subscriptions *subscriptions =
609984cfe4eSJason Gunthorpe 		range->mm->notifier_subscriptions;
61099cb252fSJason Gunthorpe 
61199cb252fSJason Gunthorpe 	lock_map_acquire(&__mmu_notifier_invalidate_range_start_map);
612984cfe4eSJason Gunthorpe 	if (subscriptions->has_itree)
613984cfe4eSJason Gunthorpe 		mn_itree_inv_end(subscriptions);
61499cb252fSJason Gunthorpe 
615984cfe4eSJason Gunthorpe 	if (!hlist_empty(&subscriptions->list))
616ec8832d0SAlistair Popple 		mn_hlist_invalidate_end(subscriptions, range);
61723b68395SDaniel Vetter 	lock_map_release(&__mmu_notifier_invalidate_range_start_map);
618cddb8a5cSAndrea Arcangeli }
619cddb8a5cSAndrea Arcangeli 
__mmu_notifier_arch_invalidate_secondary_tlbs(struct mm_struct * mm,unsigned long start,unsigned long end)6201af5a810SAlistair Popple void __mmu_notifier_arch_invalidate_secondary_tlbs(struct mm_struct *mm,
6210f0a327fSJoerg Roedel 					unsigned long start, unsigned long end)
6220f0a327fSJoerg Roedel {
6231991722aSJason Gunthorpe 	struct mmu_notifier *subscription;
6240f0a327fSJoerg Roedel 	int id;
6250f0a327fSJoerg Roedel 
6260f0a327fSJoerg Roedel 	id = srcu_read_lock(&srcu);
627b0fbe8c3SLi RongQing 	hlist_for_each_entry_srcu(subscription,
62863886badSQian Cai 				 &mm->notifier_subscriptions->list, hlist,
62963886badSQian Cai 				 srcu_read_lock_held(&srcu)) {
6301af5a810SAlistair Popple 		if (subscription->ops->arch_invalidate_secondary_tlbs)
6311af5a810SAlistair Popple 			subscription->ops->arch_invalidate_secondary_tlbs(
6321af5a810SAlistair Popple 				subscription, mm,
6331991722aSJason Gunthorpe 				start, end);
6340f0a327fSJoerg Roedel 	}
6350f0a327fSJoerg Roedel 	srcu_read_unlock(&srcu, id);
6360f0a327fSJoerg Roedel }
6370f0a327fSJoerg Roedel 
63856c57103SJason Gunthorpe /*
639c1e8d7c6SMichel Lespinasse  * Same as mmu_notifier_register but here the caller must hold the mmap_lock in
64099cb252fSJason Gunthorpe  * write mode. A NULL mn signals the notifier is being registered for itree
64199cb252fSJason Gunthorpe  * mode.
64256c57103SJason Gunthorpe  */
__mmu_notifier_register(struct mmu_notifier * subscription,struct mm_struct * mm)6431991722aSJason Gunthorpe int __mmu_notifier_register(struct mmu_notifier *subscription,
6441991722aSJason Gunthorpe 			    struct mm_struct *mm)
645cddb8a5cSAndrea Arcangeli {
646984cfe4eSJason Gunthorpe 	struct mmu_notifier_subscriptions *subscriptions = NULL;
647cddb8a5cSAndrea Arcangeli 	int ret;
648cddb8a5cSAndrea Arcangeli 
64942fc5414SMichel Lespinasse 	mmap_assert_write_locked(mm);
650cddb8a5cSAndrea Arcangeli 	BUG_ON(atomic_read(&mm->mm_users) <= 0);
651cddb8a5cSAndrea Arcangeli 
6521af5a810SAlistair Popple 	/*
6531af5a810SAlistair Popple 	 * Subsystems should only register for invalidate_secondary_tlbs() or
6541af5a810SAlistair Popple 	 * invalidate_range_start()/end() callbacks, not both.
6551af5a810SAlistair Popple 	 */
6561af5a810SAlistair Popple 	if (WARN_ON_ONCE(subscription &&
6571af5a810SAlistair Popple 			 (subscription->ops->arch_invalidate_secondary_tlbs &&
6581af5a810SAlistair Popple 			 (subscription->ops->invalidate_range_start ||
6591af5a810SAlistair Popple 			  subscription->ops->invalidate_range_end))))
6601af5a810SAlistair Popple 		return -EINVAL;
6611af5a810SAlistair Popple 
662984cfe4eSJason Gunthorpe 	if (!mm->notifier_subscriptions) {
66370df291bSJason Gunthorpe 		/*
66470df291bSJason Gunthorpe 		 * kmalloc cannot be called under mm_take_all_locks(), but we
665984cfe4eSJason Gunthorpe 		 * know that mm->notifier_subscriptions can't change while we
666c1e8d7c6SMichel Lespinasse 		 * hold the write side of the mmap_lock.
66770df291bSJason Gunthorpe 		 */
66832a92f8cSLinus Torvalds 		subscriptions = kzalloc_obj(struct mmu_notifier_subscriptions);
669984cfe4eSJason Gunthorpe 		if (!subscriptions)
67056c57103SJason Gunthorpe 			return -ENOMEM;
67135cfa2b0SGavin Shan 
672984cfe4eSJason Gunthorpe 		INIT_HLIST_HEAD(&subscriptions->list);
673984cfe4eSJason Gunthorpe 		spin_lock_init(&subscriptions->lock);
674984cfe4eSJason Gunthorpe 		subscriptions->invalidate_seq = 2;
675984cfe4eSJason Gunthorpe 		subscriptions->itree = RB_ROOT_CACHED;
676984cfe4eSJason Gunthorpe 		init_waitqueue_head(&subscriptions->wq);
677984cfe4eSJason Gunthorpe 		INIT_HLIST_HEAD(&subscriptions->deferred_list);
67870df291bSJason Gunthorpe 	}
67970df291bSJason Gunthorpe 
680cddb8a5cSAndrea Arcangeli 	ret = mm_take_all_locks(mm);
681cddb8a5cSAndrea Arcangeli 	if (unlikely(ret))
68235cfa2b0SGavin Shan 		goto out_clean;
683cddb8a5cSAndrea Arcangeli 
684cddb8a5cSAndrea Arcangeli 	/*
685cddb8a5cSAndrea Arcangeli 	 * Serialize the update against mmu_notifier_unregister. A
686cddb8a5cSAndrea Arcangeli 	 * side note: mmu_notifier_release can't run concurrently with
687cddb8a5cSAndrea Arcangeli 	 * us because we hold the mm_users pin (either implicitly as
688cddb8a5cSAndrea Arcangeli 	 * current->mm or explicitly with get_task_mm() or similar).
689cddb8a5cSAndrea Arcangeli 	 * We can't race against any other mmu notifier method either
690cddb8a5cSAndrea Arcangeli 	 * thanks to mm_take_all_locks().
69199cb252fSJason Gunthorpe 	 *
692984cfe4eSJason Gunthorpe 	 * release semantics on the initialization of the
693984cfe4eSJason Gunthorpe 	 * mmu_notifier_subscriptions's contents are provided for unlocked
694984cfe4eSJason Gunthorpe 	 * readers.  acquire can only be used while holding the mmgrab or
695984cfe4eSJason Gunthorpe 	 * mmget, and is safe because once created the
696984cfe4eSJason Gunthorpe 	 * mmu_notifier_subscriptions is not freed until the mm is destroyed.
697c1e8d7c6SMichel Lespinasse 	 * As above, users holding the mmap_lock or one of the
69899cb252fSJason Gunthorpe 	 * mm_take_all_locks() do not need to use acquire semantics.
699cddb8a5cSAndrea Arcangeli 	 */
700984cfe4eSJason Gunthorpe 	if (subscriptions)
701984cfe4eSJason Gunthorpe 		smp_store_release(&mm->notifier_subscriptions, subscriptions);
70299cb252fSJason Gunthorpe 
7031991722aSJason Gunthorpe 	if (subscription) {
70499cb252fSJason Gunthorpe 		/* Pairs with the mmdrop in mmu_notifier_unregister_* */
70599cb252fSJason Gunthorpe 		mmgrab(mm);
7061991722aSJason Gunthorpe 		subscription->mm = mm;
7071991722aSJason Gunthorpe 		subscription->users = 1;
70870df291bSJason Gunthorpe 
709984cfe4eSJason Gunthorpe 		spin_lock(&mm->notifier_subscriptions->lock);
7101991722aSJason Gunthorpe 		hlist_add_head_rcu(&subscription->hlist,
711984cfe4eSJason Gunthorpe 				   &mm->notifier_subscriptions->list);
712984cfe4eSJason Gunthorpe 		spin_unlock(&mm->notifier_subscriptions->lock);
71399cb252fSJason Gunthorpe 	} else
714984cfe4eSJason Gunthorpe 		mm->notifier_subscriptions->has_itree = true;
715cddb8a5cSAndrea Arcangeli 
716cddb8a5cSAndrea Arcangeli 	mm_drop_all_locks(mm);
71770df291bSJason Gunthorpe 	BUG_ON(atomic_read(&mm->mm_users) <= 0);
71870df291bSJason Gunthorpe 	return 0;
71970df291bSJason Gunthorpe 
72035cfa2b0SGavin Shan out_clean:
721984cfe4eSJason Gunthorpe 	kfree(subscriptions);
722cddb8a5cSAndrea Arcangeli 	return ret;
723cddb8a5cSAndrea Arcangeli }
72456c57103SJason Gunthorpe EXPORT_SYMBOL_GPL(__mmu_notifier_register);
725cddb8a5cSAndrea Arcangeli 
7262c7933f5SJason Gunthorpe /**
7272c7933f5SJason Gunthorpe  * mmu_notifier_register - Register a notifier on a mm
728d49653f3SKrzysztof Kozlowski  * @subscription: The notifier to attach
7292c7933f5SJason Gunthorpe  * @mm: The mm to attach the notifier to
7302c7933f5SJason Gunthorpe  *
731c1e8d7c6SMichel Lespinasse  * Must not hold mmap_lock nor any other VM related lock when calling
732cddb8a5cSAndrea Arcangeli  * this registration function. Must also ensure mm_users can't go down
733cddb8a5cSAndrea Arcangeli  * to zero while this runs to avoid races with mmu_notifier_release,
734cddb8a5cSAndrea Arcangeli  * so mm has to be current->mm or the mm should be pinned safely such
735cddb8a5cSAndrea Arcangeli  * as with get_task_mm(). If the mm is not current->mm, the mm_users
736cddb8a5cSAndrea Arcangeli  * pin should be released by calling mmput after mmu_notifier_register
7372c7933f5SJason Gunthorpe  * returns.
7382c7933f5SJason Gunthorpe  *
7392c7933f5SJason Gunthorpe  * mmu_notifier_unregister() or mmu_notifier_put() must be always called to
7402c7933f5SJason Gunthorpe  * unregister the notifier.
7412c7933f5SJason Gunthorpe  *
7421991722aSJason Gunthorpe  * While the caller has a mmu_notifier get the subscription->mm pointer will remain
7432c7933f5SJason Gunthorpe  * valid, and can be converted to an active mm pointer via mmget_not_zero().
744cddb8a5cSAndrea Arcangeli  */
mmu_notifier_register(struct mmu_notifier * subscription,struct mm_struct * mm)7451991722aSJason Gunthorpe int mmu_notifier_register(struct mmu_notifier *subscription,
7461991722aSJason Gunthorpe 			  struct mm_struct *mm)
747cddb8a5cSAndrea Arcangeli {
74856c57103SJason Gunthorpe 	int ret;
74956c57103SJason Gunthorpe 
750d8ed45c5SMichel Lespinasse 	mmap_write_lock(mm);
7511991722aSJason Gunthorpe 	ret = __mmu_notifier_register(subscription, mm);
752d8ed45c5SMichel Lespinasse 	mmap_write_unlock(mm);
75356c57103SJason Gunthorpe 	return ret;
754cddb8a5cSAndrea Arcangeli }
755cddb8a5cSAndrea Arcangeli EXPORT_SYMBOL_GPL(mmu_notifier_register);
756cddb8a5cSAndrea Arcangeli 
7572c7933f5SJason Gunthorpe static struct mmu_notifier *
find_get_mmu_notifier(struct mm_struct * mm,const struct mmu_notifier_ops * ops)7582c7933f5SJason Gunthorpe find_get_mmu_notifier(struct mm_struct *mm, const struct mmu_notifier_ops *ops)
7592c7933f5SJason Gunthorpe {
7601991722aSJason Gunthorpe 	struct mmu_notifier *subscription;
7612c7933f5SJason Gunthorpe 
762984cfe4eSJason Gunthorpe 	spin_lock(&mm->notifier_subscriptions->lock);
763b0fbe8c3SLi RongQing 	hlist_for_each_entry_srcu(subscription,
76463886badSQian Cai 				 &mm->notifier_subscriptions->list, hlist,
76563886badSQian Cai 				 lockdep_is_held(&mm->notifier_subscriptions->lock)) {
7661991722aSJason Gunthorpe 		if (subscription->ops != ops)
7672c7933f5SJason Gunthorpe 			continue;
7682c7933f5SJason Gunthorpe 
7691991722aSJason Gunthorpe 		if (likely(subscription->users != UINT_MAX))
7701991722aSJason Gunthorpe 			subscription->users++;
7712c7933f5SJason Gunthorpe 		else
7721991722aSJason Gunthorpe 			subscription = ERR_PTR(-EOVERFLOW);
773984cfe4eSJason Gunthorpe 		spin_unlock(&mm->notifier_subscriptions->lock);
7741991722aSJason Gunthorpe 		return subscription;
7752c7933f5SJason Gunthorpe 	}
776984cfe4eSJason Gunthorpe 	spin_unlock(&mm->notifier_subscriptions->lock);
7772c7933f5SJason Gunthorpe 	return NULL;
7782c7933f5SJason Gunthorpe }
7792c7933f5SJason Gunthorpe 
7802c7933f5SJason Gunthorpe /**
7812c7933f5SJason Gunthorpe  * mmu_notifier_get_locked - Return the single struct mmu_notifier for
7822c7933f5SJason Gunthorpe  *                           the mm & ops
7832c7933f5SJason Gunthorpe  * @ops: The operations struct being subscribe with
7842c7933f5SJason Gunthorpe  * @mm : The mm to attach notifiers too
7852c7933f5SJason Gunthorpe  *
7862c7933f5SJason Gunthorpe  * This function either allocates a new mmu_notifier via
7872c7933f5SJason Gunthorpe  * ops->alloc_notifier(), or returns an already existing notifier on the
7882c7933f5SJason Gunthorpe  * list. The value of the ops pointer is used to determine when two notifiers
7892c7933f5SJason Gunthorpe  * are the same.
7902c7933f5SJason Gunthorpe  *
7912c7933f5SJason Gunthorpe  * Each call to mmu_notifier_get() must be paired with a call to
792c1e8d7c6SMichel Lespinasse  * mmu_notifier_put(). The caller must hold the write side of mm->mmap_lock.
7932c7933f5SJason Gunthorpe  *
7942c7933f5SJason Gunthorpe  * While the caller has a mmu_notifier get the mm pointer will remain valid,
7952c7933f5SJason Gunthorpe  * and can be converted to an active mm pointer via mmget_not_zero().
7962c7933f5SJason Gunthorpe  */
mmu_notifier_get_locked(const struct mmu_notifier_ops * ops,struct mm_struct * mm)7972c7933f5SJason Gunthorpe struct mmu_notifier *mmu_notifier_get_locked(const struct mmu_notifier_ops *ops,
7982c7933f5SJason Gunthorpe 					     struct mm_struct *mm)
7992c7933f5SJason Gunthorpe {
8001991722aSJason Gunthorpe 	struct mmu_notifier *subscription;
8012c7933f5SJason Gunthorpe 	int ret;
8022c7933f5SJason Gunthorpe 
80342fc5414SMichel Lespinasse 	mmap_assert_write_locked(mm);
8042c7933f5SJason Gunthorpe 
805984cfe4eSJason Gunthorpe 	if (mm->notifier_subscriptions) {
8061991722aSJason Gunthorpe 		subscription = find_get_mmu_notifier(mm, ops);
8071991722aSJason Gunthorpe 		if (subscription)
8081991722aSJason Gunthorpe 			return subscription;
8092c7933f5SJason Gunthorpe 	}
8102c7933f5SJason Gunthorpe 
8111991722aSJason Gunthorpe 	subscription = ops->alloc_notifier(mm);
8121991722aSJason Gunthorpe 	if (IS_ERR(subscription))
8131991722aSJason Gunthorpe 		return subscription;
8141991722aSJason Gunthorpe 	subscription->ops = ops;
8151991722aSJason Gunthorpe 	ret = __mmu_notifier_register(subscription, mm);
8162c7933f5SJason Gunthorpe 	if (ret)
8172c7933f5SJason Gunthorpe 		goto out_free;
8181991722aSJason Gunthorpe 	return subscription;
8192c7933f5SJason Gunthorpe out_free:
8201991722aSJason Gunthorpe 	subscription->ops->free_notifier(subscription);
8212c7933f5SJason Gunthorpe 	return ERR_PTR(ret);
8222c7933f5SJason Gunthorpe }
8232c7933f5SJason Gunthorpe EXPORT_SYMBOL_GPL(mmu_notifier_get_locked);
8242c7933f5SJason Gunthorpe 
825cddb8a5cSAndrea Arcangeli /* this is called after the last mmu_notifier_unregister() returned */
__mmu_notifier_subscriptions_destroy(struct mm_struct * mm)826984cfe4eSJason Gunthorpe void __mmu_notifier_subscriptions_destroy(struct mm_struct *mm)
827cddb8a5cSAndrea Arcangeli {
828984cfe4eSJason Gunthorpe 	BUG_ON(!hlist_empty(&mm->notifier_subscriptions->list));
829984cfe4eSJason Gunthorpe 	kfree(mm->notifier_subscriptions);
830984cfe4eSJason Gunthorpe 	mm->notifier_subscriptions = LIST_POISON1; /* debug */
831cddb8a5cSAndrea Arcangeli }
832cddb8a5cSAndrea Arcangeli 
833cddb8a5cSAndrea Arcangeli /*
834cddb8a5cSAndrea Arcangeli  * This releases the mm_count pin automatically and frees the mm
835cddb8a5cSAndrea Arcangeli  * structure if it was the last user of it. It serializes against
83621a92735SSagi Grimberg  * running mmu notifiers with SRCU and against mmu_notifier_unregister
83721a92735SSagi Grimberg  * with the unregister lock + SRCU. All sptes must be dropped before
838cddb8a5cSAndrea Arcangeli  * calling mmu_notifier_unregister. ->release or any other notifier
839cddb8a5cSAndrea Arcangeli  * method may be invoked concurrently with mmu_notifier_unregister,
840cddb8a5cSAndrea Arcangeli  * and only after mmu_notifier_unregister returned we're guaranteed
841cddb8a5cSAndrea Arcangeli  * that ->release or any other method can't run anymore.
842cddb8a5cSAndrea Arcangeli  */
mmu_notifier_unregister(struct mmu_notifier * subscription,struct mm_struct * mm)8431991722aSJason Gunthorpe void mmu_notifier_unregister(struct mmu_notifier *subscription,
8441991722aSJason Gunthorpe 			     struct mm_struct *mm)
845cddb8a5cSAndrea Arcangeli {
846cddb8a5cSAndrea Arcangeli 	BUG_ON(atomic_read(&mm->mm_count) <= 0);
847cddb8a5cSAndrea Arcangeli 
8481991722aSJason Gunthorpe 	if (!hlist_unhashed(&subscription->hlist)) {
849d34883d4SXiao Guangrong 		/*
850d34883d4SXiao Guangrong 		 * SRCU here will force exit_mmap to wait for ->release to
851d34883d4SXiao Guangrong 		 * finish before freeing the pages.
852d34883d4SXiao Guangrong 		 */
85321a92735SSagi Grimberg 		int id;
8543ad3d901SXiao Guangrong 
855751efd86SRobin Holt 		id = srcu_read_lock(&srcu);
856d34883d4SXiao Guangrong 		/*
857d34883d4SXiao Guangrong 		 * exit_mmap will block in mmu_notifier_release to guarantee
858d34883d4SXiao Guangrong 		 * that ->release is called before freeing the pages.
859d34883d4SXiao Guangrong 		 */
8601991722aSJason Gunthorpe 		if (subscription->ops->release)
8611991722aSJason Gunthorpe 			subscription->ops->release(subscription, mm);
862751efd86SRobin Holt 		srcu_read_unlock(&srcu, id);
863d34883d4SXiao Guangrong 
864984cfe4eSJason Gunthorpe 		spin_lock(&mm->notifier_subscriptions->lock);
865d34883d4SXiao Guangrong 		/*
866d34883d4SXiao Guangrong 		 * Can not use list_del_rcu() since __mmu_notifier_release
867d34883d4SXiao Guangrong 		 * can delete it before we hold the lock.
868d34883d4SXiao Guangrong 		 */
8691991722aSJason Gunthorpe 		hlist_del_init_rcu(&subscription->hlist);
870984cfe4eSJason Gunthorpe 		spin_unlock(&mm->notifier_subscriptions->lock);
871d34883d4SXiao Guangrong 	}
872751efd86SRobin Holt 
873751efd86SRobin Holt 	/*
874d34883d4SXiao Guangrong 	 * Wait for any running method to finish, of course including
87583a35e36SGeert Uytterhoeven 	 * ->release if it was run by mmu_notifier_release instead of us.
876cddb8a5cSAndrea Arcangeli 	 */
87721a92735SSagi Grimberg 	synchronize_srcu(&srcu);
878cddb8a5cSAndrea Arcangeli 
879cddb8a5cSAndrea Arcangeli 	BUG_ON(atomic_read(&mm->mm_count) <= 0);
880cddb8a5cSAndrea Arcangeli 
881cddb8a5cSAndrea Arcangeli 	mmdrop(mm);
882cddb8a5cSAndrea Arcangeli }
883cddb8a5cSAndrea Arcangeli EXPORT_SYMBOL_GPL(mmu_notifier_unregister);
88421a92735SSagi Grimberg 
mmu_notifier_free_rcu(struct rcu_head * rcu)8852c7933f5SJason Gunthorpe static void mmu_notifier_free_rcu(struct rcu_head *rcu)
8862c7933f5SJason Gunthorpe {
8871991722aSJason Gunthorpe 	struct mmu_notifier *subscription =
8881991722aSJason Gunthorpe 		container_of(rcu, struct mmu_notifier, rcu);
8891991722aSJason Gunthorpe 	struct mm_struct *mm = subscription->mm;
8902c7933f5SJason Gunthorpe 
8911991722aSJason Gunthorpe 	subscription->ops->free_notifier(subscription);
8922c7933f5SJason Gunthorpe 	/* Pairs with the get in __mmu_notifier_register() */
8932c7933f5SJason Gunthorpe 	mmdrop(mm);
8942c7933f5SJason Gunthorpe }
8952c7933f5SJason Gunthorpe 
8962c7933f5SJason Gunthorpe /**
8972c7933f5SJason Gunthorpe  * mmu_notifier_put - Release the reference on the notifier
898d49653f3SKrzysztof Kozlowski  * @subscription: The notifier to act on
8992c7933f5SJason Gunthorpe  *
9002c7933f5SJason Gunthorpe  * This function must be paired with each mmu_notifier_get(), it releases the
9012c7933f5SJason Gunthorpe  * reference obtained by the get. If this is the last reference then process
9022c7933f5SJason Gunthorpe  * to free the notifier will be run asynchronously.
9032c7933f5SJason Gunthorpe  *
9042c7933f5SJason Gunthorpe  * Unlike mmu_notifier_unregister() the get/put flow only calls ops->release
9052c7933f5SJason Gunthorpe  * when the mm_struct is destroyed. Instead free_notifier is always called to
9062c7933f5SJason Gunthorpe  * release any resources held by the user.
9072c7933f5SJason Gunthorpe  *
9082c7933f5SJason Gunthorpe  * As ops->release is not guaranteed to be called, the user must ensure that
9092c7933f5SJason Gunthorpe  * all sptes are dropped, and no new sptes can be established before
9102c7933f5SJason Gunthorpe  * mmu_notifier_put() is called.
9112c7933f5SJason Gunthorpe  *
9122c7933f5SJason Gunthorpe  * This function can be called from the ops->release callback, however the
9132c7933f5SJason Gunthorpe  * caller must still ensure it is called pairwise with mmu_notifier_get().
9142c7933f5SJason Gunthorpe  *
9152c7933f5SJason Gunthorpe  * Modules calling this function must call mmu_notifier_synchronize() in
9162c7933f5SJason Gunthorpe  * their __exit functions to ensure the async work is completed.
9172c7933f5SJason Gunthorpe  */
mmu_notifier_put(struct mmu_notifier * subscription)9181991722aSJason Gunthorpe void mmu_notifier_put(struct mmu_notifier *subscription)
9192c7933f5SJason Gunthorpe {
9201991722aSJason Gunthorpe 	struct mm_struct *mm = subscription->mm;
9212c7933f5SJason Gunthorpe 
922984cfe4eSJason Gunthorpe 	spin_lock(&mm->notifier_subscriptions->lock);
9231991722aSJason Gunthorpe 	if (WARN_ON(!subscription->users) || --subscription->users)
9242c7933f5SJason Gunthorpe 		goto out_unlock;
9251991722aSJason Gunthorpe 	hlist_del_init_rcu(&subscription->hlist);
926984cfe4eSJason Gunthorpe 	spin_unlock(&mm->notifier_subscriptions->lock);
9272c7933f5SJason Gunthorpe 
9281991722aSJason Gunthorpe 	call_srcu(&srcu, &subscription->rcu, mmu_notifier_free_rcu);
9292c7933f5SJason Gunthorpe 	return;
9302c7933f5SJason Gunthorpe 
9312c7933f5SJason Gunthorpe out_unlock:
932984cfe4eSJason Gunthorpe 	spin_unlock(&mm->notifier_subscriptions->lock);
9332c7933f5SJason Gunthorpe }
9342c7933f5SJason Gunthorpe EXPORT_SYMBOL_GPL(mmu_notifier_put);
9352c7933f5SJason Gunthorpe 
__mmu_interval_notifier_insert(struct mmu_interval_notifier * interval_sub,struct mm_struct * mm,struct mmu_notifier_subscriptions * subscriptions,unsigned long start,unsigned long length,const struct mmu_interval_notifier_ops * ops)93699cb252fSJason Gunthorpe static int __mmu_interval_notifier_insert(
9375292e24aSJason Gunthorpe 	struct mmu_interval_notifier *interval_sub, struct mm_struct *mm,
938984cfe4eSJason Gunthorpe 	struct mmu_notifier_subscriptions *subscriptions, unsigned long start,
93999cb252fSJason Gunthorpe 	unsigned long length, const struct mmu_interval_notifier_ops *ops)
94099cb252fSJason Gunthorpe {
9415292e24aSJason Gunthorpe 	interval_sub->mm = mm;
9425292e24aSJason Gunthorpe 	interval_sub->ops = ops;
9435292e24aSJason Gunthorpe 	RB_CLEAR_NODE(&interval_sub->interval_tree.rb);
9445292e24aSJason Gunthorpe 	interval_sub->interval_tree.start = start;
94599cb252fSJason Gunthorpe 	/*
94699cb252fSJason Gunthorpe 	 * Note that the representation of the intervals in the interval tree
94799cb252fSJason Gunthorpe 	 * considers the ending point as contained in the interval.
94899cb252fSJason Gunthorpe 	 */
94999cb252fSJason Gunthorpe 	if (length == 0 ||
9505292e24aSJason Gunthorpe 	    check_add_overflow(start, length - 1,
9515292e24aSJason Gunthorpe 			       &interval_sub->interval_tree.last))
95299cb252fSJason Gunthorpe 		return -EOVERFLOW;
95399cb252fSJason Gunthorpe 
95499cb252fSJason Gunthorpe 	/* Must call with a mmget() held */
955c9682d10SJann Horn 	if (WARN_ON(atomic_read(&mm->mm_users) <= 0))
95699cb252fSJason Gunthorpe 		return -EINVAL;
95799cb252fSJason Gunthorpe 
95899cb252fSJason Gunthorpe 	/* pairs with mmdrop in mmu_interval_notifier_remove() */
95999cb252fSJason Gunthorpe 	mmgrab(mm);
96099cb252fSJason Gunthorpe 
96199cb252fSJason Gunthorpe 	/*
96299cb252fSJason Gunthorpe 	 * If some invalidate_range_start/end region is going on in parallel
96399cb252fSJason Gunthorpe 	 * we don't know what VA ranges are affected, so we must assume this
96499cb252fSJason Gunthorpe 	 * new range is included.
96599cb252fSJason Gunthorpe 	 *
96699cb252fSJason Gunthorpe 	 * If the itree is invalidating then we are not allowed to change
96799cb252fSJason Gunthorpe 	 * it. Retrying until invalidation is done is tricky due to the
96899cb252fSJason Gunthorpe 	 * possibility for live lock, instead defer the add to
96999cb252fSJason Gunthorpe 	 * mn_itree_inv_end() so this algorithm is deterministic.
97099cb252fSJason Gunthorpe 	 *
9715292e24aSJason Gunthorpe 	 * In all cases the value for the interval_sub->invalidate_seq should be
97299cb252fSJason Gunthorpe 	 * odd, see mmu_interval_read_begin()
97399cb252fSJason Gunthorpe 	 */
974984cfe4eSJason Gunthorpe 	spin_lock(&subscriptions->lock);
975984cfe4eSJason Gunthorpe 	if (subscriptions->active_invalidate_ranges) {
976984cfe4eSJason Gunthorpe 		if (mn_itree_is_invalidating(subscriptions))
9775292e24aSJason Gunthorpe 			hlist_add_head(&interval_sub->deferred_item,
978984cfe4eSJason Gunthorpe 				       &subscriptions->deferred_list);
97999cb252fSJason Gunthorpe 		else {
980984cfe4eSJason Gunthorpe 			subscriptions->invalidate_seq |= 1;
9815292e24aSJason Gunthorpe 			interval_tree_insert(&interval_sub->interval_tree,
982984cfe4eSJason Gunthorpe 					     &subscriptions->itree);
98399cb252fSJason Gunthorpe 		}
9845292e24aSJason Gunthorpe 		interval_sub->invalidate_seq = subscriptions->invalidate_seq;
98599cb252fSJason Gunthorpe 	} else {
986984cfe4eSJason Gunthorpe 		WARN_ON(mn_itree_is_invalidating(subscriptions));
98799cb252fSJason Gunthorpe 		/*
9885292e24aSJason Gunthorpe 		 * The starting seq for a subscription not under invalidation
9895292e24aSJason Gunthorpe 		 * should be odd, not equal to the current invalidate_seq and
99099cb252fSJason Gunthorpe 		 * invalidate_seq should not 'wrap' to the new seq any time
99199cb252fSJason Gunthorpe 		 * soon.
99299cb252fSJason Gunthorpe 		 */
9935292e24aSJason Gunthorpe 		interval_sub->invalidate_seq =
9945292e24aSJason Gunthorpe 			subscriptions->invalidate_seq - 1;
9955292e24aSJason Gunthorpe 		interval_tree_insert(&interval_sub->interval_tree,
996984cfe4eSJason Gunthorpe 				     &subscriptions->itree);
99799cb252fSJason Gunthorpe 	}
998984cfe4eSJason Gunthorpe 	spin_unlock(&subscriptions->lock);
99999cb252fSJason Gunthorpe 	return 0;
100099cb252fSJason Gunthorpe }
100199cb252fSJason Gunthorpe 
100299cb252fSJason Gunthorpe /**
100399cb252fSJason Gunthorpe  * mmu_interval_notifier_insert - Insert an interval notifier
10045292e24aSJason Gunthorpe  * @interval_sub: Interval subscription to register
100599cb252fSJason Gunthorpe  * @start: Starting virtual address to monitor
100699cb252fSJason Gunthorpe  * @length: Length of the range to monitor
100799cb252fSJason Gunthorpe  * @mm: mm_struct to attach to
1008d49653f3SKrzysztof Kozlowski  * @ops: Interval notifier operations to be called on matching events
100999cb252fSJason Gunthorpe  *
101099cb252fSJason Gunthorpe  * This function subscribes the interval notifier for notifications from the
101199cb252fSJason Gunthorpe  * mm.  Upon return the ops related to mmu_interval_notifier will be called
101299cb252fSJason Gunthorpe  * whenever an event that intersects with the given range occurs.
101399cb252fSJason Gunthorpe  *
101499cb252fSJason Gunthorpe  * Upon return the range_notifier may not be present in the interval tree yet.
101599cb252fSJason Gunthorpe  * The caller must use the normal interval notifier read flow via
101699cb252fSJason Gunthorpe  * mmu_interval_read_begin() to establish SPTEs for this range.
101799cb252fSJason Gunthorpe  */
mmu_interval_notifier_insert(struct mmu_interval_notifier * interval_sub,struct mm_struct * mm,unsigned long start,unsigned long length,const struct mmu_interval_notifier_ops * ops)10185292e24aSJason Gunthorpe int mmu_interval_notifier_insert(struct mmu_interval_notifier *interval_sub,
101999cb252fSJason Gunthorpe 				 struct mm_struct *mm, unsigned long start,
102099cb252fSJason Gunthorpe 				 unsigned long length,
102199cb252fSJason Gunthorpe 				 const struct mmu_interval_notifier_ops *ops)
102299cb252fSJason Gunthorpe {
1023984cfe4eSJason Gunthorpe 	struct mmu_notifier_subscriptions *subscriptions;
102499cb252fSJason Gunthorpe 	int ret;
102599cb252fSJason Gunthorpe 
10267aba71dbSThomas Hellström 	WARN_ON_ONCE(ops->invalidate_start && !ops->invalidate_finish);
1027da1c55f1SMichel Lespinasse 	might_lock(&mm->mmap_lock);
102899cb252fSJason Gunthorpe 
1029984cfe4eSJason Gunthorpe 	subscriptions = smp_load_acquire(&mm->notifier_subscriptions);
1030984cfe4eSJason Gunthorpe 	if (!subscriptions || !subscriptions->has_itree) {
103199cb252fSJason Gunthorpe 		ret = mmu_notifier_register(NULL, mm);
103299cb252fSJason Gunthorpe 		if (ret)
103399cb252fSJason Gunthorpe 			return ret;
1034984cfe4eSJason Gunthorpe 		subscriptions = mm->notifier_subscriptions;
103599cb252fSJason Gunthorpe 	}
10365292e24aSJason Gunthorpe 	return __mmu_interval_notifier_insert(interval_sub, mm, subscriptions,
10375292e24aSJason Gunthorpe 					      start, length, ops);
103899cb252fSJason Gunthorpe }
103999cb252fSJason Gunthorpe EXPORT_SYMBOL_GPL(mmu_interval_notifier_insert);
104099cb252fSJason Gunthorpe 
mmu_interval_notifier_insert_locked(struct mmu_interval_notifier * interval_sub,struct mm_struct * mm,unsigned long start,unsigned long length,const struct mmu_interval_notifier_ops * ops)104199cb252fSJason Gunthorpe int mmu_interval_notifier_insert_locked(
10425292e24aSJason Gunthorpe 	struct mmu_interval_notifier *interval_sub, struct mm_struct *mm,
104399cb252fSJason Gunthorpe 	unsigned long start, unsigned long length,
104499cb252fSJason Gunthorpe 	const struct mmu_interval_notifier_ops *ops)
104599cb252fSJason Gunthorpe {
1046984cfe4eSJason Gunthorpe 	struct mmu_notifier_subscriptions *subscriptions =
1047984cfe4eSJason Gunthorpe 		mm->notifier_subscriptions;
104899cb252fSJason Gunthorpe 	int ret;
104999cb252fSJason Gunthorpe 
105042fc5414SMichel Lespinasse 	mmap_assert_write_locked(mm);
105199cb252fSJason Gunthorpe 
1052984cfe4eSJason Gunthorpe 	if (!subscriptions || !subscriptions->has_itree) {
105399cb252fSJason Gunthorpe 		ret = __mmu_notifier_register(NULL, mm);
105499cb252fSJason Gunthorpe 		if (ret)
105599cb252fSJason Gunthorpe 			return ret;
1056984cfe4eSJason Gunthorpe 		subscriptions = mm->notifier_subscriptions;
105799cb252fSJason Gunthorpe 	}
10585292e24aSJason Gunthorpe 	return __mmu_interval_notifier_insert(interval_sub, mm, subscriptions,
10595292e24aSJason Gunthorpe 					      start, length, ops);
106099cb252fSJason Gunthorpe }
106199cb252fSJason Gunthorpe EXPORT_SYMBOL_GPL(mmu_interval_notifier_insert_locked);
106299cb252fSJason Gunthorpe 
106331956166SAlistair Popple static bool
mmu_interval_seq_released(struct mmu_notifier_subscriptions * subscriptions,unsigned long seq)106431956166SAlistair Popple mmu_interval_seq_released(struct mmu_notifier_subscriptions *subscriptions,
106531956166SAlistair Popple 			  unsigned long seq)
106631956166SAlistair Popple {
106731956166SAlistair Popple 	bool ret;
106831956166SAlistair Popple 
106931956166SAlistair Popple 	spin_lock(&subscriptions->lock);
107031956166SAlistair Popple 	ret = subscriptions->invalidate_seq != seq;
107131956166SAlistair Popple 	spin_unlock(&subscriptions->lock);
107231956166SAlistair Popple 	return ret;
107331956166SAlistair Popple }
107431956166SAlistair Popple 
107599cb252fSJason Gunthorpe /**
107699cb252fSJason Gunthorpe  * mmu_interval_notifier_remove - Remove a interval notifier
10775292e24aSJason Gunthorpe  * @interval_sub: Interval subscription to unregister
107899cb252fSJason Gunthorpe  *
107999cb252fSJason Gunthorpe  * This function must be paired with mmu_interval_notifier_insert(). It cannot
108099cb252fSJason Gunthorpe  * be called from any ops callback.
108199cb252fSJason Gunthorpe  *
108299cb252fSJason Gunthorpe  * Once this returns ops callbacks are no longer running on other CPUs and
108399cb252fSJason Gunthorpe  * will not be called in future.
108499cb252fSJason Gunthorpe  */
mmu_interval_notifier_remove(struct mmu_interval_notifier * interval_sub)10855292e24aSJason Gunthorpe void mmu_interval_notifier_remove(struct mmu_interval_notifier *interval_sub)
108699cb252fSJason Gunthorpe {
10875292e24aSJason Gunthorpe 	struct mm_struct *mm = interval_sub->mm;
1088984cfe4eSJason Gunthorpe 	struct mmu_notifier_subscriptions *subscriptions =
1089984cfe4eSJason Gunthorpe 		mm->notifier_subscriptions;
109099cb252fSJason Gunthorpe 	unsigned long seq = 0;
109199cb252fSJason Gunthorpe 
109299cb252fSJason Gunthorpe 	might_sleep();
109399cb252fSJason Gunthorpe 
1094984cfe4eSJason Gunthorpe 	spin_lock(&subscriptions->lock);
1095984cfe4eSJason Gunthorpe 	if (mn_itree_is_invalidating(subscriptions)) {
109699cb252fSJason Gunthorpe 		/*
109799cb252fSJason Gunthorpe 		 * remove is being called after insert put this on the
109899cb252fSJason Gunthorpe 		 * deferred list, but before the deferred list was processed.
109999cb252fSJason Gunthorpe 		 */
11005292e24aSJason Gunthorpe 		if (RB_EMPTY_NODE(&interval_sub->interval_tree.rb)) {
11015292e24aSJason Gunthorpe 			hlist_del(&interval_sub->deferred_item);
110299cb252fSJason Gunthorpe 		} else {
11035292e24aSJason Gunthorpe 			hlist_add_head(&interval_sub->deferred_item,
1104984cfe4eSJason Gunthorpe 				       &subscriptions->deferred_list);
1105984cfe4eSJason Gunthorpe 			seq = subscriptions->invalidate_seq;
110699cb252fSJason Gunthorpe 		}
110799cb252fSJason Gunthorpe 	} else {
11085292e24aSJason Gunthorpe 		WARN_ON(RB_EMPTY_NODE(&interval_sub->interval_tree.rb));
11095292e24aSJason Gunthorpe 		interval_tree_remove(&interval_sub->interval_tree,
1110984cfe4eSJason Gunthorpe 				     &subscriptions->itree);
111199cb252fSJason Gunthorpe 	}
1112984cfe4eSJason Gunthorpe 	spin_unlock(&subscriptions->lock);
111399cb252fSJason Gunthorpe 
111499cb252fSJason Gunthorpe 	/*
111599cb252fSJason Gunthorpe 	 * The possible sleep on progress in the invalidation requires the
111699cb252fSJason Gunthorpe 	 * caller not hold any locks held by invalidation callbacks.
111799cb252fSJason Gunthorpe 	 */
111899cb252fSJason Gunthorpe 	lock_map_acquire(&__mmu_notifier_invalidate_range_start_map);
111999cb252fSJason Gunthorpe 	lock_map_release(&__mmu_notifier_invalidate_range_start_map);
112099cb252fSJason Gunthorpe 	if (seq)
1121984cfe4eSJason Gunthorpe 		wait_event(subscriptions->wq,
112231956166SAlistair Popple 			   mmu_interval_seq_released(subscriptions, seq));
112399cb252fSJason Gunthorpe 
112499cb252fSJason Gunthorpe 	/* pairs with mmgrab in mmu_interval_notifier_insert() */
112599cb252fSJason Gunthorpe 	mmdrop(mm);
112699cb252fSJason Gunthorpe }
112799cb252fSJason Gunthorpe EXPORT_SYMBOL_GPL(mmu_interval_notifier_remove);
112899cb252fSJason Gunthorpe 
11292c7933f5SJason Gunthorpe /**
11302c7933f5SJason Gunthorpe  * mmu_notifier_synchronize - Ensure all mmu_notifiers are freed
11312c7933f5SJason Gunthorpe  *
11322c7933f5SJason Gunthorpe  * This function ensures that all outstanding async SRU work from
11332c7933f5SJason Gunthorpe  * mmu_notifier_put() is completed. After it returns any mmu_notifier_ops
11342c7933f5SJason Gunthorpe  * associated with an unused mmu_notifier will no longer be called.
11352c7933f5SJason Gunthorpe  *
11362c7933f5SJason Gunthorpe  * Before using the caller must ensure that all of its mmu_notifiers have been
11372c7933f5SJason Gunthorpe  * fully released via mmu_notifier_put().
11382c7933f5SJason Gunthorpe  *
11392c7933f5SJason Gunthorpe  * Modules using the mmu_notifier_put() API should call this in their __exit
11402c7933f5SJason Gunthorpe  * function to avoid module unloading races.
11412c7933f5SJason Gunthorpe  */
mmu_notifier_synchronize(void)11422c7933f5SJason Gunthorpe void mmu_notifier_synchronize(void)
11432c7933f5SJason Gunthorpe {
11442c7933f5SJason Gunthorpe 	synchronize_srcu(&srcu);
11452c7933f5SJason Gunthorpe }
11462c7933f5SJason Gunthorpe EXPORT_SYMBOL_GPL(mmu_notifier_synchronize);
1147