blob: a399b8fa23ffc2d7cca79de926fdf07bfaadfd34 [file] [log] [blame]
Thomas Gleixner20c8ccb2019-06-04 10:11:32 +02001// SPDX-License-Identifier: GPL-2.0-only
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07002/*
3 * linux/mm/mmu_notifier.c
4 *
5 * Copyright (C) 2008 Qumranet, Inc.
6 * Copyright (C) 2008 SGI
Christoph Lameter93e205a2016-03-17 14:21:15 -07007 * Christoph Lameter <cl@linux.com>
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07008 */
9
10#include <linux/rculist.h>
11#include <linux/mmu_notifier.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040012#include <linux/export.h>
Andrea Arcangelicddb8a52008-07-28 15:46:29 -070013#include <linux/mm.h>
14#include <linux/err.h>
Jason Gunthorpe99cb2522019-11-12 16:22:19 -040015#include <linux/interval_tree.h>
Sagi Grimberg21a92732012-10-08 16:29:24 -070016#include <linux/srcu.h>
Andrea Arcangelicddb8a52008-07-28 15:46:29 -070017#include <linux/rcupdate.h>
18#include <linux/sched.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010019#include <linux/sched/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Andrea Arcangelicddb8a52008-07-28 15:46:29 -070021
Sagi Grimberg21a92732012-10-08 16:29:24 -070022/* global SRCU for all MMs */
Paul E. McKenneydde8da62017-03-25 10:42:07 -070023DEFINE_STATIC_SRCU(srcu);
Sagi Grimberg21a92732012-10-08 16:29:24 -070024
Daniel Vetter23b683952019-08-26 22:14:21 +020025#ifdef CONFIG_LOCKDEP
26struct lockdep_map __mmu_notifier_invalidate_range_start_map = {
27 .name = "mmu_notifier_invalidate_range_start"
28};
29#endif
30
Andrea Arcangelicddb8a52008-07-28 15:46:29 -070031/*
Jason Gunthorpe984cfe42019-12-18 13:40:35 -040032 * The mmu_notifier_subscriptions structure is allocated and installed in
33 * mm->notifier_subscriptions inside the mm_take_all_locks() protected
Jason Gunthorpe56f434f2019-11-12 16:22:18 -040034 * critical section and it's released only when mm_count reaches zero
35 * in mmdrop().
36 */
Jason Gunthorpe984cfe42019-12-18 13:40:35 -040037struct mmu_notifier_subscriptions {
Suren Baghdasaryan5d8520b2021-11-24 07:56:04 -080038 /*
39 * WARNING: hdr should be the first member of this structure
40 * so that it can be typecasted into mmu_notifier_subscriptions_hdr.
41 * This is required to avoid KMI CRC breakage.
42 */
43 struct mmu_notifier_subscriptions_hdr hdr;
Jason Gunthorpe56f434f2019-11-12 16:22:18 -040044 /* all mmu notifiers registered in this mm are queued in this list */
45 struct hlist_head list;
Jason Gunthorpe99cb2522019-11-12 16:22:19 -040046 bool has_itree;
Jason Gunthorpe56f434f2019-11-12 16:22:18 -040047 /* to serialize the list modifications and hlist_unhashed */
48 spinlock_t lock;
Jason Gunthorpe99cb2522019-11-12 16:22:19 -040049 unsigned long invalidate_seq;
50 unsigned long active_invalidate_ranges;
51 struct rb_root_cached itree;
52 wait_queue_head_t wq;
53 struct hlist_head deferred_list;
Jason Gunthorpe56f434f2019-11-12 16:22:18 -040054};
55
56/*
Jason Gunthorpe99cb2522019-11-12 16:22:19 -040057 * This is a collision-retry read-side/write-side 'lock', a lot like a
58 * seqcount, however this allows multiple write-sides to hold it at
59 * once. Conceptually the write side is protecting the values of the PTEs in
60 * this mm, such that PTES cannot be read into SPTEs (shadow PTEs) while any
61 * writer exists.
62 *
63 * Note that the core mm creates nested invalidate_range_start()/end() regions
64 * within the same thread, and runs invalidate_range_start()/end() in parallel
65 * on multiple CPUs. This is designed to not reduce concurrency or block
66 * progress on the mm side.
67 *
68 * As a secondary function, holding the full write side also serves to prevent
69 * writers for the itree, this is an optimization to avoid extra locking
70 * during invalidate_range_start/end notifiers.
71 *
72 * The write side has two states, fully excluded:
73 * - mm->active_invalidate_ranges != 0
Jason Gunthorpe984cfe42019-12-18 13:40:35 -040074 * - subscriptions->invalidate_seq & 1 == True (odd)
Jason Gunthorpe99cb2522019-11-12 16:22:19 -040075 * - some range on the mm_struct is being invalidated
76 * - the itree is not allowed to change
77 *
78 * And partially excluded:
79 * - mm->active_invalidate_ranges != 0
Jason Gunthorpe984cfe42019-12-18 13:40:35 -040080 * - subscriptions->invalidate_seq & 1 == False (even)
Jason Gunthorpe99cb2522019-11-12 16:22:19 -040081 * - some range on the mm_struct is being invalidated
82 * - the itree is allowed to change
83 *
Jason Gunthorpe984cfe42019-12-18 13:40:35 -040084 * Operations on notifier_subscriptions->invalidate_seq (under spinlock):
Jason Gunthorpe99cb2522019-11-12 16:22:19 -040085 * seq |= 1 # Begin writing
86 * seq++ # Release the writing state
87 * seq & 1 # True if a writer exists
88 *
89 * The later state avoids some expensive work on inv_end in the common case of
Jason Gunthorpe5292e242020-01-14 11:29:52 -040090 * no mmu_interval_notifier monitoring the VA.
Jason Gunthorpe99cb2522019-11-12 16:22:19 -040091 */
Jason Gunthorpe984cfe42019-12-18 13:40:35 -040092static bool
93mn_itree_is_invalidating(struct mmu_notifier_subscriptions *subscriptions)
Jason Gunthorpe99cb2522019-11-12 16:22:19 -040094{
Jason Gunthorpe984cfe42019-12-18 13:40:35 -040095 lockdep_assert_held(&subscriptions->lock);
96 return subscriptions->invalidate_seq & 1;
Jason Gunthorpe99cb2522019-11-12 16:22:19 -040097}
98
99static struct mmu_interval_notifier *
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400100mn_itree_inv_start_range(struct mmu_notifier_subscriptions *subscriptions,
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400101 const struct mmu_notifier_range *range,
102 unsigned long *seq)
103{
104 struct interval_tree_node *node;
105 struct mmu_interval_notifier *res = NULL;
106
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400107 spin_lock(&subscriptions->lock);
108 subscriptions->active_invalidate_ranges++;
109 node = interval_tree_iter_first(&subscriptions->itree, range->start,
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400110 range->end - 1);
111 if (node) {
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400112 subscriptions->invalidate_seq |= 1;
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400113 res = container_of(node, struct mmu_interval_notifier,
114 interval_tree);
115 }
116
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400117 *seq = subscriptions->invalidate_seq;
118 spin_unlock(&subscriptions->lock);
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400119 return res;
120}
121
122static struct mmu_interval_notifier *
Jason Gunthorpe5292e242020-01-14 11:29:52 -0400123mn_itree_inv_next(struct mmu_interval_notifier *interval_sub,
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400124 const struct mmu_notifier_range *range)
125{
126 struct interval_tree_node *node;
127
Jason Gunthorpe5292e242020-01-14 11:29:52 -0400128 node = interval_tree_iter_next(&interval_sub->interval_tree,
129 range->start, range->end - 1);
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400130 if (!node)
131 return NULL;
132 return container_of(node, struct mmu_interval_notifier, interval_tree);
133}
134
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400135static void mn_itree_inv_end(struct mmu_notifier_subscriptions *subscriptions)
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400136{
Jason Gunthorpe5292e242020-01-14 11:29:52 -0400137 struct mmu_interval_notifier *interval_sub;
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400138 struct hlist_node *next;
139
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400140 spin_lock(&subscriptions->lock);
141 if (--subscriptions->active_invalidate_ranges ||
142 !mn_itree_is_invalidating(subscriptions)) {
143 spin_unlock(&subscriptions->lock);
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400144 return;
145 }
146
147 /* Make invalidate_seq even */
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400148 subscriptions->invalidate_seq++;
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400149
150 /*
151 * The inv_end incorporates a deferred mechanism like rtnl_unlock().
152 * Adds and removes are queued until the final inv_end happens then
153 * they are progressed. This arrangement for tree updates is used to
154 * avoid using a blocking lock during invalidate_range_start.
155 */
Jason Gunthorpe5292e242020-01-14 11:29:52 -0400156 hlist_for_each_entry_safe(interval_sub, next,
157 &subscriptions->deferred_list,
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400158 deferred_item) {
Jason Gunthorpe5292e242020-01-14 11:29:52 -0400159 if (RB_EMPTY_NODE(&interval_sub->interval_tree.rb))
160 interval_tree_insert(&interval_sub->interval_tree,
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400161 &subscriptions->itree);
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400162 else
Jason Gunthorpe5292e242020-01-14 11:29:52 -0400163 interval_tree_remove(&interval_sub->interval_tree,
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400164 &subscriptions->itree);
Jason Gunthorpe5292e242020-01-14 11:29:52 -0400165 hlist_del(&interval_sub->deferred_item);
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400166 }
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400167 spin_unlock(&subscriptions->lock);
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400168
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400169 wake_up_all(&subscriptions->wq);
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400170}
171
172/**
173 * mmu_interval_read_begin - Begin a read side critical section against a VA
174 * range
Krzysztof Kozlowskid49653f2020-08-11 18:32:09 -0700175 * @interval_sub: The interval subscription
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400176 *
177 * mmu_iterval_read_begin()/mmu_iterval_read_retry() implement a
Jason Gunthorpe5292e242020-01-14 11:29:52 -0400178 * collision-retry scheme similar to seqcount for the VA range under
179 * subscription. If the mm invokes invalidation during the critical section
180 * then mmu_interval_read_retry() will return true.
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400181 *
182 * This is useful to obtain shadow PTEs where teardown or setup of the SPTEs
183 * require a blocking context. The critical region formed by this can sleep,
184 * and the required 'user_lock' can also be a sleeping lock.
185 *
186 * The caller is required to provide a 'user_lock' to serialize both teardown
187 * and setup.
188 *
189 * The return value should be passed to mmu_interval_read_retry().
190 */
Jason Gunthorpe5292e242020-01-14 11:29:52 -0400191unsigned long
192mmu_interval_read_begin(struct mmu_interval_notifier *interval_sub)
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400193{
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400194 struct mmu_notifier_subscriptions *subscriptions =
Jason Gunthorpe5292e242020-01-14 11:29:52 -0400195 interval_sub->mm->notifier_subscriptions;
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400196 unsigned long seq;
197 bool is_invalidating;
198
199 /*
Jason Gunthorpe5292e242020-01-14 11:29:52 -0400200 * If the subscription has a different seq value under the user_lock
201 * than we started with then it has collided.
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400202 *
Jason Gunthorpe5292e242020-01-14 11:29:52 -0400203 * If the subscription currently has the same seq value as the
204 * subscriptions seq, then it is currently between
205 * invalidate_start/end and is colliding.
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400206 *
207 * The locking looks broadly like this:
208 * mn_tree_invalidate_start(): mmu_interval_read_begin():
209 * spin_lock
Jason Gunthorpe5292e242020-01-14 11:29:52 -0400210 * seq = READ_ONCE(interval_sub->invalidate_seq);
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400211 * seq == subs->invalidate_seq
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400212 * spin_unlock
213 * spin_lock
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400214 * seq = ++subscriptions->invalidate_seq
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400215 * spin_unlock
216 * op->invalidate_range():
217 * user_lock
218 * mmu_interval_set_seq()
Jason Gunthorpe5292e242020-01-14 11:29:52 -0400219 * interval_sub->invalidate_seq = seq
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400220 * user_unlock
221 *
222 * [Required: mmu_interval_read_retry() == true]
223 *
224 * mn_itree_inv_end():
225 * spin_lock
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400226 * seq = ++subscriptions->invalidate_seq
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400227 * spin_unlock
228 *
229 * user_lock
230 * mmu_interval_read_retry():
Jason Gunthorpe5292e242020-01-14 11:29:52 -0400231 * interval_sub->invalidate_seq != seq
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400232 * user_unlock
233 *
234 * Barriers are not needed here as any races here are closed by an
235 * eventual mmu_interval_read_retry(), which provides a barrier via the
236 * user_lock.
237 */
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400238 spin_lock(&subscriptions->lock);
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400239 /* Pairs with the WRITE_ONCE in mmu_interval_set_seq() */
Jason Gunthorpe5292e242020-01-14 11:29:52 -0400240 seq = READ_ONCE(interval_sub->invalidate_seq);
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400241 is_invalidating = seq == subscriptions->invalidate_seq;
242 spin_unlock(&subscriptions->lock);
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400243
244 /*
Jason Gunthorpe5292e242020-01-14 11:29:52 -0400245 * interval_sub->invalidate_seq must always be set to an odd value via
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400246 * mmu_interval_set_seq() using the provided cur_seq from
247 * mn_itree_inv_start_range(). This ensures that if seq does wrap we
248 * will always clear the below sleep in some reasonable time as
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400249 * subscriptions->invalidate_seq is even in the idle state.
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400250 */
251 lock_map_acquire(&__mmu_notifier_invalidate_range_start_map);
252 lock_map_release(&__mmu_notifier_invalidate_range_start_map);
253 if (is_invalidating)
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400254 wait_event(subscriptions->wq,
255 READ_ONCE(subscriptions->invalidate_seq) != seq);
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400256
257 /*
258 * Notice that mmu_interval_read_retry() can already be true at this
259 * point, avoiding loops here allows the caller to provide a global
260 * time bound.
261 */
262
263 return seq;
264}
265EXPORT_SYMBOL_GPL(mmu_interval_read_begin);
266
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400267static void mn_itree_release(struct mmu_notifier_subscriptions *subscriptions,
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400268 struct mm_struct *mm)
269{
270 struct mmu_notifier_range range = {
271 .flags = MMU_NOTIFIER_RANGE_BLOCKABLE,
272 .event = MMU_NOTIFY_RELEASE,
273 .mm = mm,
274 .start = 0,
275 .end = ULONG_MAX,
276 };
Jason Gunthorpe5292e242020-01-14 11:29:52 -0400277 struct mmu_interval_notifier *interval_sub;
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400278 unsigned long cur_seq;
279 bool ret;
280
Jason Gunthorpe5292e242020-01-14 11:29:52 -0400281 for (interval_sub =
282 mn_itree_inv_start_range(subscriptions, &range, &cur_seq);
283 interval_sub;
284 interval_sub = mn_itree_inv_next(interval_sub, &range)) {
285 ret = interval_sub->ops->invalidate(interval_sub, &range,
286 cur_seq);
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400287 WARN_ON(!ret);
288 }
289
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400290 mn_itree_inv_end(subscriptions);
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400291}
292
293/*
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700294 * This function can't run concurrently against mmu_notifier_register
295 * because mm->mm_users > 0 during mmu_notifier_register and exit_mmap
296 * runs with mm_users == 0. Other tasks may still invoke mmu notifiers
297 * in parallel despite there being no task using this mm any more,
298 * through the vmas outside of the exit_mmap context, such as with
299 * vmtruncate. This serializes against mmu_notifier_unregister with
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400300 * the notifier_subscriptions->lock in addition to SRCU and it serializes
301 * against the other mmu notifiers with SRCU. struct mmu_notifier_subscriptions
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700302 * can't go away from under us as exit_mmap holds an mm_count pin
303 * itself.
304 */
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400305static void mn_hlist_release(struct mmu_notifier_subscriptions *subscriptions,
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400306 struct mm_struct *mm)
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700307{
Jason Gunthorpe19917222020-01-14 11:11:17 -0400308 struct mmu_notifier *subscription;
Sagi Grimberg21a92732012-10-08 16:29:24 -0700309 int id;
Xiao Guangrong3ad3d902012-07-31 16:45:52 -0700310
311 /*
Xiao Guangrongd34883d2013-05-24 15:55:11 -0700312 * SRCU here will block mmu_notifier_unregister until
313 * ->release returns.
Xiao Guangrong3ad3d902012-07-31 16:45:52 -0700314 */
Sagi Grimberg21a92732012-10-08 16:29:24 -0700315 id = srcu_read_lock(&srcu);
Qian Cai63886ba2020-03-21 18:22:34 -0700316 hlist_for_each_entry_rcu(subscription, &subscriptions->list, hlist,
317 srcu_read_lock_held(&srcu))
Xiao Guangrongd34883d2013-05-24 15:55:11 -0700318 /*
319 * If ->release runs before mmu_notifier_unregister it must be
320 * handled, as it's the only way for the driver to flush all
321 * existing sptes and stop the driver from establishing any more
322 * sptes before all the pages in the mm are freed.
323 */
Jason Gunthorpe19917222020-01-14 11:11:17 -0400324 if (subscription->ops->release)
325 subscription->ops->release(subscription, mm);
Xiao Guangrongd34883d2013-05-24 15:55:11 -0700326
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400327 spin_lock(&subscriptions->lock);
328 while (unlikely(!hlist_empty(&subscriptions->list))) {
Jason Gunthorpe19917222020-01-14 11:11:17 -0400329 subscription = hlist_entry(subscriptions->list.first,
330 struct mmu_notifier, hlist);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700331 /*
Xiao Guangrongd34883d2013-05-24 15:55:11 -0700332 * We arrived before mmu_notifier_unregister so
333 * mmu_notifier_unregister will do nothing other than to wait
334 * for ->release to finish and for mmu_notifier_unregister to
335 * return.
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700336 */
Jason Gunthorpe19917222020-01-14 11:11:17 -0400337 hlist_del_init_rcu(&subscription->hlist);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700338 }
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400339 spin_unlock(&subscriptions->lock);
Peter Zijlstrab9722162014-08-06 16:08:20 -0700340 srcu_read_unlock(&srcu, id);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700341
342 /*
Xiao Guangrongd34883d2013-05-24 15:55:11 -0700343 * synchronize_srcu here prevents mmu_notifier_release from returning to
344 * exit_mmap (which would proceed with freeing all pages in the mm)
345 * until the ->release method returns, if it was invoked by
346 * mmu_notifier_unregister.
347 *
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400348 * The notifier_subscriptions can't go away from under us because
349 * one mm_count is held by exit_mmap.
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700350 */
Sagi Grimberg21a92732012-10-08 16:29:24 -0700351 synchronize_srcu(&srcu);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700352}
353
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400354void __mmu_notifier_release(struct mm_struct *mm)
355{
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400356 struct mmu_notifier_subscriptions *subscriptions =
357 mm->notifier_subscriptions;
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400358
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400359 if (subscriptions->has_itree)
360 mn_itree_release(subscriptions, mm);
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400361
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400362 if (!hlist_empty(&subscriptions->list))
363 mn_hlist_release(subscriptions, mm);
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400364}
365
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700366/*
367 * If no young bitflag is supported by the hardware, ->clear_flush_young can
368 * unmap the address and return 1 or 0 depending if the mapping previously
369 * existed or not.
370 */
371int __mmu_notifier_clear_flush_young(struct mm_struct *mm,
Andres Lagar-Cavilla57128462014-09-22 14:54:42 -0700372 unsigned long start,
373 unsigned long end)
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700374{
Jason Gunthorpe19917222020-01-14 11:11:17 -0400375 struct mmu_notifier *subscription;
Sagi Grimberg21a92732012-10-08 16:29:24 -0700376 int young = 0, id;
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700377
Sagi Grimberg21a92732012-10-08 16:29:24 -0700378 id = srcu_read_lock(&srcu);
Jason Gunthorpe19917222020-01-14 11:11:17 -0400379 hlist_for_each_entry_rcu(subscription,
Qian Cai63886ba2020-03-21 18:22:34 -0700380 &mm->notifier_subscriptions->list, hlist,
381 srcu_read_lock_held(&srcu)) {
Jason Gunthorpe19917222020-01-14 11:11:17 -0400382 if (subscription->ops->clear_flush_young)
383 young |= subscription->ops->clear_flush_young(
384 subscription, mm, start, end);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700385 }
Sagi Grimberg21a92732012-10-08 16:29:24 -0700386 srcu_read_unlock(&srcu, id);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700387
388 return young;
389}
390
Vladimir Davydov1d7715c2015-09-09 15:35:41 -0700391int __mmu_notifier_clear_young(struct mm_struct *mm,
392 unsigned long start,
393 unsigned long end)
394{
Jason Gunthorpe19917222020-01-14 11:11:17 -0400395 struct mmu_notifier *subscription;
Vladimir Davydov1d7715c2015-09-09 15:35:41 -0700396 int young = 0, id;
397
398 id = srcu_read_lock(&srcu);
Jason Gunthorpe19917222020-01-14 11:11:17 -0400399 hlist_for_each_entry_rcu(subscription,
Qian Cai63886ba2020-03-21 18:22:34 -0700400 &mm->notifier_subscriptions->list, hlist,
401 srcu_read_lock_held(&srcu)) {
Jason Gunthorpe19917222020-01-14 11:11:17 -0400402 if (subscription->ops->clear_young)
403 young |= subscription->ops->clear_young(subscription,
404 mm, start, end);
Vladimir Davydov1d7715c2015-09-09 15:35:41 -0700405 }
406 srcu_read_unlock(&srcu, id);
407
408 return young;
409}
410
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800411int __mmu_notifier_test_young(struct mm_struct *mm,
412 unsigned long address)
413{
Jason Gunthorpe19917222020-01-14 11:11:17 -0400414 struct mmu_notifier *subscription;
Sagi Grimberg21a92732012-10-08 16:29:24 -0700415 int young = 0, id;
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800416
Sagi Grimberg21a92732012-10-08 16:29:24 -0700417 id = srcu_read_lock(&srcu);
Jason Gunthorpe19917222020-01-14 11:11:17 -0400418 hlist_for_each_entry_rcu(subscription,
Qian Cai63886ba2020-03-21 18:22:34 -0700419 &mm->notifier_subscriptions->list, hlist,
420 srcu_read_lock_held(&srcu)) {
Jason Gunthorpe19917222020-01-14 11:11:17 -0400421 if (subscription->ops->test_young) {
422 young = subscription->ops->test_young(subscription, mm,
423 address);
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800424 if (young)
425 break;
426 }
427 }
Sagi Grimberg21a92732012-10-08 16:29:24 -0700428 srcu_read_unlock(&srcu, id);
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800429
430 return young;
431}
432
Izik Eidus828502d2009-09-21 17:01:51 -0700433void __mmu_notifier_change_pte(struct mm_struct *mm, unsigned long address,
434 pte_t pte)
435{
Jason Gunthorpe19917222020-01-14 11:11:17 -0400436 struct mmu_notifier *subscription;
Sagi Grimberg21a92732012-10-08 16:29:24 -0700437 int id;
Izik Eidus828502d2009-09-21 17:01:51 -0700438
Sagi Grimberg21a92732012-10-08 16:29:24 -0700439 id = srcu_read_lock(&srcu);
Jason Gunthorpe19917222020-01-14 11:11:17 -0400440 hlist_for_each_entry_rcu(subscription,
Qian Cai63886ba2020-03-21 18:22:34 -0700441 &mm->notifier_subscriptions->list, hlist,
442 srcu_read_lock_held(&srcu)) {
Jason Gunthorpe19917222020-01-14 11:11:17 -0400443 if (subscription->ops->change_pte)
444 subscription->ops->change_pte(subscription, mm, address,
445 pte);
Izik Eidus828502d2009-09-21 17:01:51 -0700446 }
Sagi Grimberg21a92732012-10-08 16:29:24 -0700447 srcu_read_unlock(&srcu, id);
Izik Eidus828502d2009-09-21 17:01:51 -0700448}
449
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400450static int mn_itree_invalidate(struct mmu_notifier_subscriptions *subscriptions,
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400451 const struct mmu_notifier_range *range)
452{
Jason Gunthorpe5292e242020-01-14 11:29:52 -0400453 struct mmu_interval_notifier *interval_sub;
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400454 unsigned long cur_seq;
455
Jason Gunthorpe5292e242020-01-14 11:29:52 -0400456 for (interval_sub =
457 mn_itree_inv_start_range(subscriptions, range, &cur_seq);
458 interval_sub;
459 interval_sub = mn_itree_inv_next(interval_sub, range)) {
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400460 bool ret;
461
Jason Gunthorpe5292e242020-01-14 11:29:52 -0400462 ret = interval_sub->ops->invalidate(interval_sub, range,
463 cur_seq);
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400464 if (!ret) {
465 if (WARN_ON(mmu_notifier_range_blockable(range)))
466 continue;
467 goto out_would_block;
468 }
469 }
470 return 0;
471
472out_would_block:
473 /*
474 * On -EAGAIN the non-blocking caller is not allowed to call
475 * invalidate_range_end()
476 */
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400477 mn_itree_inv_end(subscriptions);
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400478 return -EAGAIN;
479}
480
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400481static int mn_hlist_invalidate_range_start(
482 struct mmu_notifier_subscriptions *subscriptions,
483 struct mmu_notifier_range *range)
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700484{
Jason Gunthorpe19917222020-01-14 11:11:17 -0400485 struct mmu_notifier *subscription;
Michal Hocko93065ac2018-08-21 21:52:33 -0700486 int ret = 0;
Sagi Grimberg21a92732012-10-08 16:29:24 -0700487 int id;
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700488
Sagi Grimberg21a92732012-10-08 16:29:24 -0700489 id = srcu_read_lock(&srcu);
Qian Cai63886ba2020-03-21 18:22:34 -0700490 hlist_for_each_entry_rcu(subscription, &subscriptions->list, hlist,
491 srcu_read_lock_held(&srcu)) {
Jason Gunthorpe19917222020-01-14 11:11:17 -0400492 const struct mmu_notifier_ops *ops = subscription->ops;
493
494 if (ops->invalidate_range_start) {
Daniel Vetterba170f72019-08-26 22:14:24 +0200495 int _ret;
496
497 if (!mmu_notifier_range_blockable(range))
498 non_block_start();
Jason Gunthorpe19917222020-01-14 11:11:17 -0400499 _ret = ops->invalidate_range_start(subscription, range);
Daniel Vetterba170f72019-08-26 22:14:24 +0200500 if (!mmu_notifier_range_blockable(range))
501 non_block_end();
Michal Hocko93065ac2018-08-21 21:52:33 -0700502 if (_ret) {
503 pr_info("%pS callback failed with %d in %sblockable context.\n",
Jason Gunthorpe19917222020-01-14 11:11:17 -0400504 ops->invalidate_range_start, _ret,
505 !mmu_notifier_range_blockable(range) ?
506 "non-" :
507 "");
Daniel Vetter8402ce62019-08-14 22:20:23 +0200508 WARN_ON(mmu_notifier_range_blockable(range) ||
Jason Gunthorpedf2ec762019-11-05 21:16:37 -0800509 _ret != -EAGAIN);
Sean Christophersonde2e6b42021-03-24 21:37:23 -0700510 /*
511 * We call all the notifiers on any EAGAIN,
512 * there is no way for a notifier to know if
513 * its start method failed, thus a start that
514 * does EAGAIN can't also do end.
515 */
516 WARN_ON(ops->invalidate_range_end);
Michal Hocko93065ac2018-08-21 21:52:33 -0700517 ret = _ret;
518 }
519 }
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700520 }
Sean Christophersonde2e6b42021-03-24 21:37:23 -0700521
522 if (ret) {
523 /*
524 * Must be non-blocking to get here. If there are multiple
525 * notifiers and one or more failed start, any that succeeded
526 * start are expecting their end to be called. Do so now.
527 */
528 hlist_for_each_entry_rcu(subscription, &subscriptions->list,
529 hlist, srcu_read_lock_held(&srcu)) {
530 if (!subscription->ops->invalidate_range_end)
531 continue;
532
533 subscription->ops->invalidate_range_end(subscription,
534 range);
535 }
536 }
Sagi Grimberg21a92732012-10-08 16:29:24 -0700537 srcu_read_unlock(&srcu, id);
Michal Hocko93065ac2018-08-21 21:52:33 -0700538
539 return ret;
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700540}
541
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400542int __mmu_notifier_invalidate_range_start(struct mmu_notifier_range *range)
543{
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400544 struct mmu_notifier_subscriptions *subscriptions =
545 range->mm->notifier_subscriptions;
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400546 int ret;
547
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400548 if (subscriptions->has_itree) {
549 ret = mn_itree_invalidate(subscriptions, range);
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400550 if (ret)
551 return ret;
552 }
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400553 if (!hlist_empty(&subscriptions->list))
554 return mn_hlist_invalidate_range_start(subscriptions, range);
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400555 return 0;
556}
557
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400558static void
559mn_hlist_invalidate_end(struct mmu_notifier_subscriptions *subscriptions,
560 struct mmu_notifier_range *range, bool only_end)
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700561{
Jason Gunthorpe19917222020-01-14 11:11:17 -0400562 struct mmu_notifier *subscription;
Sagi Grimberg21a92732012-10-08 16:29:24 -0700563 int id;
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700564
Sagi Grimberg21a92732012-10-08 16:29:24 -0700565 id = srcu_read_lock(&srcu);
Qian Cai63886ba2020-03-21 18:22:34 -0700566 hlist_for_each_entry_rcu(subscription, &subscriptions->list, hlist,
567 srcu_read_lock_held(&srcu)) {
Joerg Roedel0f0a3272014-11-13 13:46:09 +1100568 /*
569 * Call invalidate_range here too to avoid the need for the
570 * subsystem of having to register an invalidate_range_end
571 * call-back when there is invalidate_range already. Usually a
572 * subsystem registers either invalidate_range_start()/end() or
573 * invalidate_range(), so this will be no additional overhead
574 * (besides the pointer check).
Jérôme Glisse4645b9f2017-11-15 17:34:11 -0800575 *
576 * We skip call to invalidate_range() if we know it is safe ie
577 * call site use mmu_notifier_invalidate_range_only_end() which
578 * is safe to do when we know that a call to invalidate_range()
579 * already happen under page table lock.
Joerg Roedel0f0a3272014-11-13 13:46:09 +1100580 */
Jason Gunthorpe19917222020-01-14 11:11:17 -0400581 if (!only_end && subscription->ops->invalidate_range)
582 subscription->ops->invalidate_range(subscription,
583 range->mm,
584 range->start,
585 range->end);
586 if (subscription->ops->invalidate_range_end) {
Daniel Vetterba170f72019-08-26 22:14:24 +0200587 if (!mmu_notifier_range_blockable(range))
588 non_block_start();
Jason Gunthorpe19917222020-01-14 11:11:17 -0400589 subscription->ops->invalidate_range_end(subscription,
590 range);
Daniel Vetterba170f72019-08-26 22:14:24 +0200591 if (!mmu_notifier_range_blockable(range))
592 non_block_end();
593 }
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700594 }
Sagi Grimberg21a92732012-10-08 16:29:24 -0700595 srcu_read_unlock(&srcu, id);
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400596}
597
598void __mmu_notifier_invalidate_range_end(struct mmu_notifier_range *range,
599 bool only_end)
600{
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400601 struct mmu_notifier_subscriptions *subscriptions =
602 range->mm->notifier_subscriptions;
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400603
604 lock_map_acquire(&__mmu_notifier_invalidate_range_start_map);
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400605 if (subscriptions->has_itree)
606 mn_itree_inv_end(subscriptions);
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400607
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400608 if (!hlist_empty(&subscriptions->list))
609 mn_hlist_invalidate_end(subscriptions, range, only_end);
Daniel Vetter23b683952019-08-26 22:14:21 +0200610 lock_map_release(&__mmu_notifier_invalidate_range_start_map);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700611}
612
Joerg Roedel0f0a3272014-11-13 13:46:09 +1100613void __mmu_notifier_invalidate_range(struct mm_struct *mm,
614 unsigned long start, unsigned long end)
615{
Jason Gunthorpe19917222020-01-14 11:11:17 -0400616 struct mmu_notifier *subscription;
Joerg Roedel0f0a3272014-11-13 13:46:09 +1100617 int id;
618
619 id = srcu_read_lock(&srcu);
Jason Gunthorpe19917222020-01-14 11:11:17 -0400620 hlist_for_each_entry_rcu(subscription,
Qian Cai63886ba2020-03-21 18:22:34 -0700621 &mm->notifier_subscriptions->list, hlist,
622 srcu_read_lock_held(&srcu)) {
Jason Gunthorpe19917222020-01-14 11:11:17 -0400623 if (subscription->ops->invalidate_range)
624 subscription->ops->invalidate_range(subscription, mm,
625 start, end);
Joerg Roedel0f0a3272014-11-13 13:46:09 +1100626 }
627 srcu_read_unlock(&srcu, id);
628}
Joerg Roedel0f0a3272014-11-13 13:46:09 +1100629
Suren Baghdasaryan69713502021-11-04 13:42:56 -0700630#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
631
632static inline void mmu_notifier_write_lock(struct mm_struct *mm)
633{
Suren Baghdasaryan5d8520b2021-11-24 07:56:04 -0800634 percpu_down_write(
635 &mm->notifier_subscriptions->hdr.mmu_notifier_lock->rw_sem);
Suren Baghdasaryan69713502021-11-04 13:42:56 -0700636}
637
638static inline void mmu_notifier_write_unlock(struct mm_struct *mm)
639{
Suren Baghdasaryan5d8520b2021-11-24 07:56:04 -0800640 percpu_up_write(
641 &mm->notifier_subscriptions->hdr.mmu_notifier_lock->rw_sem);
Suren Baghdasaryan69713502021-11-04 13:42:56 -0700642}
643
644#else /* CONFIG_SPECULATIVE_PAGE_FAULT */
645
646static inline void mmu_notifier_write_lock(struct mm_struct *mm) {}
647static inline void mmu_notifier_write_unlock(struct mm_struct *mm) {}
648
649#endif /* CONFIG_SPECULATIVE_PAGE_FAULT */
650
Suren Baghdasaryan5d8520b2021-11-24 07:56:04 -0800651static void init_subscriptions(struct mmu_notifier_subscriptions *subscriptions)
652{
653 INIT_HLIST_HEAD(&subscriptions->list);
654 spin_lock_init(&subscriptions->lock);
655 subscriptions->invalidate_seq = 2;
656 subscriptions->itree = RB_ROOT_CACHED;
657 init_waitqueue_head(&subscriptions->wq);
658 INIT_HLIST_HEAD(&subscriptions->deferred_list);
659}
660
Jason Gunthorpe56c571032019-08-06 20:15:38 -0300661/*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700662 * Same as mmu_notifier_register but here the caller must hold the mmap_lock in
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400663 * write mode. A NULL mn signals the notifier is being registered for itree
664 * mode.
Jason Gunthorpe56c571032019-08-06 20:15:38 -0300665 */
Jason Gunthorpe19917222020-01-14 11:11:17 -0400666int __mmu_notifier_register(struct mmu_notifier *subscription,
667 struct mm_struct *mm)
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700668{
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400669 struct mmu_notifier_subscriptions *subscriptions = NULL;
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700670 int ret;
671
Michel Lespinasse42fc5412020-06-08 21:33:44 -0700672 mmap_assert_write_locked(mm);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700673 BUG_ON(atomic_read(&mm->mm_users) <= 0);
674
Daniel Vetter66204f1d2019-08-26 22:14:22 +0200675 if (IS_ENABLED(CONFIG_LOCKDEP)) {
676 fs_reclaim_acquire(GFP_KERNEL);
677 lock_map_acquire(&__mmu_notifier_invalidate_range_start_map);
678 lock_map_release(&__mmu_notifier_invalidate_range_start_map);
679 fs_reclaim_release(GFP_KERNEL);
680 }
681
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400682 if (!mm->notifier_subscriptions) {
Jason Gunthorpe70df2912019-08-06 20:15:39 -0300683 /*
684 * kmalloc cannot be called under mm_take_all_locks(), but we
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400685 * know that mm->notifier_subscriptions can't change while we
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700686 * hold the write side of the mmap_lock.
Jason Gunthorpe70df2912019-08-06 20:15:39 -0300687 */
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400688 subscriptions = kzalloc(
689 sizeof(struct mmu_notifier_subscriptions), GFP_KERNEL);
690 if (!subscriptions)
Jason Gunthorpe70df2912019-08-06 20:15:39 -0300691 return -ENOMEM;
692
Suren Baghdasaryan5d8520b2021-11-24 07:56:04 -0800693 init_subscriptions(subscriptions);
Jason Gunthorpe70df2912019-08-06 20:15:39 -0300694 }
Gavin Shan35cfa2b2012-10-25 13:38:01 -0700695
Suren Baghdasaryan69713502021-11-04 13:42:56 -0700696 mmu_notifier_write_lock(mm);
697
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700698 ret = mm_take_all_locks(mm);
Suren Baghdasaryan69713502021-11-04 13:42:56 -0700699 if (unlikely(ret)) {
700 mmu_notifier_write_unlock(mm);
Gavin Shan35cfa2b2012-10-25 13:38:01 -0700701 goto out_clean;
Suren Baghdasaryan69713502021-11-04 13:42:56 -0700702 }
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700703
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700704 /*
705 * Serialize the update against mmu_notifier_unregister. A
706 * side note: mmu_notifier_release can't run concurrently with
707 * us because we hold the mm_users pin (either implicitly as
708 * current->mm or explicitly with get_task_mm() or similar).
709 * We can't race against any other mmu notifier method either
710 * thanks to mm_take_all_locks().
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400711 *
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400712 * release semantics on the initialization of the
713 * mmu_notifier_subscriptions's contents are provided for unlocked
714 * readers. acquire can only be used while holding the mmgrab or
715 * mmget, and is safe because once created the
716 * mmu_notifier_subscriptions is not freed until the mm is destroyed.
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700717 * As above, users holding the mmap_lock or one of the
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400718 * mm_take_all_locks() do not need to use acquire semantics.
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700719 */
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400720 if (subscriptions)
721 smp_store_release(&mm->notifier_subscriptions, subscriptions);
Suren Baghdasaryan5d8520b2021-11-24 07:56:04 -0800722 mm->notifier_subscriptions->hdr.valid = true;
Jason Gunthorpe70df2912019-08-06 20:15:39 -0300723
Jason Gunthorpe19917222020-01-14 11:11:17 -0400724 if (subscription) {
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400725 /* Pairs with the mmdrop in mmu_notifier_unregister_* */
726 mmgrab(mm);
Jason Gunthorpe19917222020-01-14 11:11:17 -0400727 subscription->mm = mm;
728 subscription->users = 1;
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400729
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400730 spin_lock(&mm->notifier_subscriptions->lock);
Jason Gunthorpe19917222020-01-14 11:11:17 -0400731 hlist_add_head_rcu(&subscription->hlist,
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400732 &mm->notifier_subscriptions->list);
733 spin_unlock(&mm->notifier_subscriptions->lock);
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400734 } else
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400735 mm->notifier_subscriptions->has_itree = true;
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700736
737 mm_drop_all_locks(mm);
Suren Baghdasaryan69713502021-11-04 13:42:56 -0700738 mmu_notifier_write_unlock(mm);
Jason Gunthorpe70df2912019-08-06 20:15:39 -0300739 BUG_ON(atomic_read(&mm->mm_users) <= 0);
740 return 0;
741
Gavin Shan35cfa2b2012-10-25 13:38:01 -0700742out_clean:
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400743 kfree(subscriptions);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700744 return ret;
745}
Jason Gunthorpe56c571032019-08-06 20:15:38 -0300746EXPORT_SYMBOL_GPL(__mmu_notifier_register);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700747
Jason Gunthorpe2c7933f2019-08-06 20:15:40 -0300748/**
749 * mmu_notifier_register - Register a notifier on a mm
Krzysztof Kozlowskid49653f2020-08-11 18:32:09 -0700750 * @subscription: The notifier to attach
Jason Gunthorpe2c7933f2019-08-06 20:15:40 -0300751 * @mm: The mm to attach the notifier to
752 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700753 * Must not hold mmap_lock nor any other VM related lock when calling
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700754 * this registration function. Must also ensure mm_users can't go down
755 * to zero while this runs to avoid races with mmu_notifier_release,
756 * so mm has to be current->mm or the mm should be pinned safely such
757 * as with get_task_mm(). If the mm is not current->mm, the mm_users
758 * pin should be released by calling mmput after mmu_notifier_register
Jason Gunthorpe2c7933f2019-08-06 20:15:40 -0300759 * returns.
760 *
761 * mmu_notifier_unregister() or mmu_notifier_put() must be always called to
762 * unregister the notifier.
763 *
Jason Gunthorpe19917222020-01-14 11:11:17 -0400764 * While the caller has a mmu_notifier get the subscription->mm pointer will remain
Jason Gunthorpe2c7933f2019-08-06 20:15:40 -0300765 * valid, and can be converted to an active mm pointer via mmget_not_zero().
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700766 */
Jason Gunthorpe19917222020-01-14 11:11:17 -0400767int mmu_notifier_register(struct mmu_notifier *subscription,
768 struct mm_struct *mm)
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700769{
Jason Gunthorpe56c571032019-08-06 20:15:38 -0300770 int ret;
771
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700772 mmap_write_lock(mm);
Jason Gunthorpe19917222020-01-14 11:11:17 -0400773 ret = __mmu_notifier_register(subscription, mm);
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700774 mmap_write_unlock(mm);
Jason Gunthorpe56c571032019-08-06 20:15:38 -0300775 return ret;
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700776}
777EXPORT_SYMBOL_GPL(mmu_notifier_register);
778
Jason Gunthorpe2c7933f2019-08-06 20:15:40 -0300779static struct mmu_notifier *
780find_get_mmu_notifier(struct mm_struct *mm, const struct mmu_notifier_ops *ops)
781{
Jason Gunthorpe19917222020-01-14 11:11:17 -0400782 struct mmu_notifier *subscription;
Jason Gunthorpe2c7933f2019-08-06 20:15:40 -0300783
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400784 spin_lock(&mm->notifier_subscriptions->lock);
Jason Gunthorpe19917222020-01-14 11:11:17 -0400785 hlist_for_each_entry_rcu(subscription,
Qian Cai63886ba2020-03-21 18:22:34 -0700786 &mm->notifier_subscriptions->list, hlist,
787 lockdep_is_held(&mm->notifier_subscriptions->lock)) {
Jason Gunthorpe19917222020-01-14 11:11:17 -0400788 if (subscription->ops != ops)
Jason Gunthorpe2c7933f2019-08-06 20:15:40 -0300789 continue;
790
Jason Gunthorpe19917222020-01-14 11:11:17 -0400791 if (likely(subscription->users != UINT_MAX))
792 subscription->users++;
Jason Gunthorpe2c7933f2019-08-06 20:15:40 -0300793 else
Jason Gunthorpe19917222020-01-14 11:11:17 -0400794 subscription = ERR_PTR(-EOVERFLOW);
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400795 spin_unlock(&mm->notifier_subscriptions->lock);
Jason Gunthorpe19917222020-01-14 11:11:17 -0400796 return subscription;
Jason Gunthorpe2c7933f2019-08-06 20:15:40 -0300797 }
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400798 spin_unlock(&mm->notifier_subscriptions->lock);
Jason Gunthorpe2c7933f2019-08-06 20:15:40 -0300799 return NULL;
800}
801
802/**
803 * mmu_notifier_get_locked - Return the single struct mmu_notifier for
804 * the mm & ops
805 * @ops: The operations struct being subscribe with
806 * @mm : The mm to attach notifiers too
807 *
808 * This function either allocates a new mmu_notifier via
809 * ops->alloc_notifier(), or returns an already existing notifier on the
810 * list. The value of the ops pointer is used to determine when two notifiers
811 * are the same.
812 *
813 * Each call to mmu_notifier_get() must be paired with a call to
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700814 * mmu_notifier_put(). The caller must hold the write side of mm->mmap_lock.
Jason Gunthorpe2c7933f2019-08-06 20:15:40 -0300815 *
816 * While the caller has a mmu_notifier get the mm pointer will remain valid,
817 * and can be converted to an active mm pointer via mmget_not_zero().
818 */
819struct mmu_notifier *mmu_notifier_get_locked(const struct mmu_notifier_ops *ops,
820 struct mm_struct *mm)
821{
Jason Gunthorpe19917222020-01-14 11:11:17 -0400822 struct mmu_notifier *subscription;
Jason Gunthorpe2c7933f2019-08-06 20:15:40 -0300823 int ret;
824
Michel Lespinasse42fc5412020-06-08 21:33:44 -0700825 mmap_assert_write_locked(mm);
Jason Gunthorpe2c7933f2019-08-06 20:15:40 -0300826
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400827 if (mm->notifier_subscriptions) {
Jason Gunthorpe19917222020-01-14 11:11:17 -0400828 subscription = find_get_mmu_notifier(mm, ops);
829 if (subscription)
830 return subscription;
Jason Gunthorpe2c7933f2019-08-06 20:15:40 -0300831 }
832
Jason Gunthorpe19917222020-01-14 11:11:17 -0400833 subscription = ops->alloc_notifier(mm);
834 if (IS_ERR(subscription))
835 return subscription;
836 subscription->ops = ops;
837 ret = __mmu_notifier_register(subscription, mm);
Jason Gunthorpe2c7933f2019-08-06 20:15:40 -0300838 if (ret)
839 goto out_free;
Jason Gunthorpe19917222020-01-14 11:11:17 -0400840 return subscription;
Jason Gunthorpe2c7933f2019-08-06 20:15:40 -0300841out_free:
Jason Gunthorpe19917222020-01-14 11:11:17 -0400842 subscription->ops->free_notifier(subscription);
Jason Gunthorpe2c7933f2019-08-06 20:15:40 -0300843 return ERR_PTR(ret);
844}
845EXPORT_SYMBOL_GPL(mmu_notifier_get_locked);
846
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700847/* this is called after the last mmu_notifier_unregister() returned */
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400848void __mmu_notifier_subscriptions_destroy(struct mm_struct *mm)
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700849{
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400850 BUG_ON(!hlist_empty(&mm->notifier_subscriptions->list));
851 kfree(mm->notifier_subscriptions);
852 mm->notifier_subscriptions = LIST_POISON1; /* debug */
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700853}
854
855/*
856 * This releases the mm_count pin automatically and frees the mm
857 * structure if it was the last user of it. It serializes against
Sagi Grimberg21a92732012-10-08 16:29:24 -0700858 * running mmu notifiers with SRCU and against mmu_notifier_unregister
859 * with the unregister lock + SRCU. All sptes must be dropped before
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700860 * calling mmu_notifier_unregister. ->release or any other notifier
861 * method may be invoked concurrently with mmu_notifier_unregister,
862 * and only after mmu_notifier_unregister returned we're guaranteed
863 * that ->release or any other method can't run anymore.
864 */
Jason Gunthorpe19917222020-01-14 11:11:17 -0400865void mmu_notifier_unregister(struct mmu_notifier *subscription,
866 struct mm_struct *mm)
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700867{
868 BUG_ON(atomic_read(&mm->mm_count) <= 0);
869
Jason Gunthorpe19917222020-01-14 11:11:17 -0400870 if (!hlist_unhashed(&subscription->hlist)) {
Xiao Guangrongd34883d2013-05-24 15:55:11 -0700871 /*
872 * SRCU here will force exit_mmap to wait for ->release to
873 * finish before freeing the pages.
874 */
Sagi Grimberg21a92732012-10-08 16:29:24 -0700875 int id;
Xiao Guangrong3ad3d902012-07-31 16:45:52 -0700876
Robin Holt751efd82013-02-22 16:35:34 -0800877 id = srcu_read_lock(&srcu);
Xiao Guangrongd34883d2013-05-24 15:55:11 -0700878 /*
879 * exit_mmap will block in mmu_notifier_release to guarantee
880 * that ->release is called before freeing the pages.
881 */
Jason Gunthorpe19917222020-01-14 11:11:17 -0400882 if (subscription->ops->release)
883 subscription->ops->release(subscription, mm);
Robin Holt751efd82013-02-22 16:35:34 -0800884 srcu_read_unlock(&srcu, id);
Xiao Guangrongd34883d2013-05-24 15:55:11 -0700885
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400886 spin_lock(&mm->notifier_subscriptions->lock);
Xiao Guangrongd34883d2013-05-24 15:55:11 -0700887 /*
888 * Can not use list_del_rcu() since __mmu_notifier_release
889 * can delete it before we hold the lock.
890 */
Jason Gunthorpe19917222020-01-14 11:11:17 -0400891 hlist_del_init_rcu(&subscription->hlist);
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400892 spin_unlock(&mm->notifier_subscriptions->lock);
Xiao Guangrongd34883d2013-05-24 15:55:11 -0700893 }
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700894
895 /*
Xiao Guangrongd34883d2013-05-24 15:55:11 -0700896 * Wait for any running method to finish, of course including
Geert Uytterhoeven83a35e32013-06-28 11:27:31 +0200897 * ->release if it was run by mmu_notifier_release instead of us.
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700898 */
Sagi Grimberg21a92732012-10-08 16:29:24 -0700899 synchronize_srcu(&srcu);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700900
901 BUG_ON(atomic_read(&mm->mm_count) <= 0);
902
903 mmdrop(mm);
904}
905EXPORT_SYMBOL_GPL(mmu_notifier_unregister);
Sagi Grimberg21a92732012-10-08 16:29:24 -0700906
Jason Gunthorpe2c7933f2019-08-06 20:15:40 -0300907static void mmu_notifier_free_rcu(struct rcu_head *rcu)
908{
Jason Gunthorpe19917222020-01-14 11:11:17 -0400909 struct mmu_notifier *subscription =
910 container_of(rcu, struct mmu_notifier, rcu);
911 struct mm_struct *mm = subscription->mm;
Jason Gunthorpe2c7933f2019-08-06 20:15:40 -0300912
Jason Gunthorpe19917222020-01-14 11:11:17 -0400913 subscription->ops->free_notifier(subscription);
Jason Gunthorpe2c7933f2019-08-06 20:15:40 -0300914 /* Pairs with the get in __mmu_notifier_register() */
915 mmdrop(mm);
916}
917
918/**
919 * mmu_notifier_put - Release the reference on the notifier
Krzysztof Kozlowskid49653f2020-08-11 18:32:09 -0700920 * @subscription: The notifier to act on
Jason Gunthorpe2c7933f2019-08-06 20:15:40 -0300921 *
922 * This function must be paired with each mmu_notifier_get(), it releases the
923 * reference obtained by the get. If this is the last reference then process
924 * to free the notifier will be run asynchronously.
925 *
926 * Unlike mmu_notifier_unregister() the get/put flow only calls ops->release
927 * when the mm_struct is destroyed. Instead free_notifier is always called to
928 * release any resources held by the user.
929 *
930 * As ops->release is not guaranteed to be called, the user must ensure that
931 * all sptes are dropped, and no new sptes can be established before
932 * mmu_notifier_put() is called.
933 *
934 * This function can be called from the ops->release callback, however the
935 * caller must still ensure it is called pairwise with mmu_notifier_get().
936 *
937 * Modules calling this function must call mmu_notifier_synchronize() in
938 * their __exit functions to ensure the async work is completed.
939 */
Jason Gunthorpe19917222020-01-14 11:11:17 -0400940void mmu_notifier_put(struct mmu_notifier *subscription)
Jason Gunthorpe2c7933f2019-08-06 20:15:40 -0300941{
Jason Gunthorpe19917222020-01-14 11:11:17 -0400942 struct mm_struct *mm = subscription->mm;
Jason Gunthorpe2c7933f2019-08-06 20:15:40 -0300943
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400944 spin_lock(&mm->notifier_subscriptions->lock);
Jason Gunthorpe19917222020-01-14 11:11:17 -0400945 if (WARN_ON(!subscription->users) || --subscription->users)
Jason Gunthorpe2c7933f2019-08-06 20:15:40 -0300946 goto out_unlock;
Jason Gunthorpe19917222020-01-14 11:11:17 -0400947 hlist_del_init_rcu(&subscription->hlist);
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400948 spin_unlock(&mm->notifier_subscriptions->lock);
Jason Gunthorpe2c7933f2019-08-06 20:15:40 -0300949
Jason Gunthorpe19917222020-01-14 11:11:17 -0400950 call_srcu(&srcu, &subscription->rcu, mmu_notifier_free_rcu);
Jason Gunthorpe2c7933f2019-08-06 20:15:40 -0300951 return;
952
953out_unlock:
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400954 spin_unlock(&mm->notifier_subscriptions->lock);
Jason Gunthorpe2c7933f2019-08-06 20:15:40 -0300955}
956EXPORT_SYMBOL_GPL(mmu_notifier_put);
957
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400958static int __mmu_interval_notifier_insert(
Jason Gunthorpe5292e242020-01-14 11:29:52 -0400959 struct mmu_interval_notifier *interval_sub, struct mm_struct *mm,
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400960 struct mmu_notifier_subscriptions *subscriptions, unsigned long start,
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400961 unsigned long length, const struct mmu_interval_notifier_ops *ops)
962{
Jason Gunthorpe5292e242020-01-14 11:29:52 -0400963 interval_sub->mm = mm;
964 interval_sub->ops = ops;
965 RB_CLEAR_NODE(&interval_sub->interval_tree.rb);
966 interval_sub->interval_tree.start = start;
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400967 /*
968 * Note that the representation of the intervals in the interval tree
969 * considers the ending point as contained in the interval.
970 */
971 if (length == 0 ||
Jason Gunthorpe5292e242020-01-14 11:29:52 -0400972 check_add_overflow(start, length - 1,
973 &interval_sub->interval_tree.last))
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400974 return -EOVERFLOW;
975
976 /* Must call with a mmget() held */
Jann Hornc9682d12020-10-15 20:07:43 -0700977 if (WARN_ON(atomic_read(&mm->mm_users) <= 0))
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400978 return -EINVAL;
979
980 /* pairs with mmdrop in mmu_interval_notifier_remove() */
981 mmgrab(mm);
982
983 /*
984 * If some invalidate_range_start/end region is going on in parallel
985 * we don't know what VA ranges are affected, so we must assume this
986 * new range is included.
987 *
988 * If the itree is invalidating then we are not allowed to change
989 * it. Retrying until invalidation is done is tricky due to the
990 * possibility for live lock, instead defer the add to
991 * mn_itree_inv_end() so this algorithm is deterministic.
992 *
Jason Gunthorpe5292e242020-01-14 11:29:52 -0400993 * In all cases the value for the interval_sub->invalidate_seq should be
Jason Gunthorpe99cb2522019-11-12 16:22:19 -0400994 * odd, see mmu_interval_read_begin()
995 */
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400996 spin_lock(&subscriptions->lock);
997 if (subscriptions->active_invalidate_ranges) {
998 if (mn_itree_is_invalidating(subscriptions))
Jason Gunthorpe5292e242020-01-14 11:29:52 -0400999 hlist_add_head(&interval_sub->deferred_item,
Jason Gunthorpe984cfe42019-12-18 13:40:35 -04001000 &subscriptions->deferred_list);
Jason Gunthorpe99cb2522019-11-12 16:22:19 -04001001 else {
Jason Gunthorpe984cfe42019-12-18 13:40:35 -04001002 subscriptions->invalidate_seq |= 1;
Jason Gunthorpe5292e242020-01-14 11:29:52 -04001003 interval_tree_insert(&interval_sub->interval_tree,
Jason Gunthorpe984cfe42019-12-18 13:40:35 -04001004 &subscriptions->itree);
Jason Gunthorpe99cb2522019-11-12 16:22:19 -04001005 }
Jason Gunthorpe5292e242020-01-14 11:29:52 -04001006 interval_sub->invalidate_seq = subscriptions->invalidate_seq;
Jason Gunthorpe99cb2522019-11-12 16:22:19 -04001007 } else {
Jason Gunthorpe984cfe42019-12-18 13:40:35 -04001008 WARN_ON(mn_itree_is_invalidating(subscriptions));
Jason Gunthorpe99cb2522019-11-12 16:22:19 -04001009 /*
Jason Gunthorpe5292e242020-01-14 11:29:52 -04001010 * The starting seq for a subscription not under invalidation
1011 * should be odd, not equal to the current invalidate_seq and
Jason Gunthorpe99cb2522019-11-12 16:22:19 -04001012 * invalidate_seq should not 'wrap' to the new seq any time
1013 * soon.
1014 */
Jason Gunthorpe5292e242020-01-14 11:29:52 -04001015 interval_sub->invalidate_seq =
1016 subscriptions->invalidate_seq - 1;
1017 interval_tree_insert(&interval_sub->interval_tree,
Jason Gunthorpe984cfe42019-12-18 13:40:35 -04001018 &subscriptions->itree);
Jason Gunthorpe99cb2522019-11-12 16:22:19 -04001019 }
Jason Gunthorpe984cfe42019-12-18 13:40:35 -04001020 spin_unlock(&subscriptions->lock);
Jason Gunthorpe99cb2522019-11-12 16:22:19 -04001021 return 0;
1022}
1023
1024/**
1025 * mmu_interval_notifier_insert - Insert an interval notifier
Jason Gunthorpe5292e242020-01-14 11:29:52 -04001026 * @interval_sub: Interval subscription to register
Jason Gunthorpe99cb2522019-11-12 16:22:19 -04001027 * @start: Starting virtual address to monitor
1028 * @length: Length of the range to monitor
Krzysztof Kozlowskid49653f2020-08-11 18:32:09 -07001029 * @mm: mm_struct to attach to
1030 * @ops: Interval notifier operations to be called on matching events
Jason Gunthorpe99cb2522019-11-12 16:22:19 -04001031 *
1032 * This function subscribes the interval notifier for notifications from the
1033 * mm. Upon return the ops related to mmu_interval_notifier will be called
1034 * whenever an event that intersects with the given range occurs.
1035 *
1036 * Upon return the range_notifier may not be present in the interval tree yet.
1037 * The caller must use the normal interval notifier read flow via
1038 * mmu_interval_read_begin() to establish SPTEs for this range.
1039 */
Jason Gunthorpe5292e242020-01-14 11:29:52 -04001040int mmu_interval_notifier_insert(struct mmu_interval_notifier *interval_sub,
Jason Gunthorpe99cb2522019-11-12 16:22:19 -04001041 struct mm_struct *mm, unsigned long start,
1042 unsigned long length,
1043 const struct mmu_interval_notifier_ops *ops)
1044{
Jason Gunthorpe984cfe42019-12-18 13:40:35 -04001045 struct mmu_notifier_subscriptions *subscriptions;
Jason Gunthorpe99cb2522019-11-12 16:22:19 -04001046 int ret;
1047
Michel Lespinasseda1c55f2020-06-08 21:33:47 -07001048 might_lock(&mm->mmap_lock);
Jason Gunthorpe99cb2522019-11-12 16:22:19 -04001049
Jason Gunthorpe984cfe42019-12-18 13:40:35 -04001050 subscriptions = smp_load_acquire(&mm->notifier_subscriptions);
1051 if (!subscriptions || !subscriptions->has_itree) {
Jason Gunthorpe99cb2522019-11-12 16:22:19 -04001052 ret = mmu_notifier_register(NULL, mm);
1053 if (ret)
1054 return ret;
Jason Gunthorpe984cfe42019-12-18 13:40:35 -04001055 subscriptions = mm->notifier_subscriptions;
Jason Gunthorpe99cb2522019-11-12 16:22:19 -04001056 }
Jason Gunthorpe5292e242020-01-14 11:29:52 -04001057 return __mmu_interval_notifier_insert(interval_sub, mm, subscriptions,
1058 start, length, ops);
Jason Gunthorpe99cb2522019-11-12 16:22:19 -04001059}
1060EXPORT_SYMBOL_GPL(mmu_interval_notifier_insert);
1061
1062int mmu_interval_notifier_insert_locked(
Jason Gunthorpe5292e242020-01-14 11:29:52 -04001063 struct mmu_interval_notifier *interval_sub, struct mm_struct *mm,
Jason Gunthorpe99cb2522019-11-12 16:22:19 -04001064 unsigned long start, unsigned long length,
1065 const struct mmu_interval_notifier_ops *ops)
1066{
Jason Gunthorpe984cfe42019-12-18 13:40:35 -04001067 struct mmu_notifier_subscriptions *subscriptions =
1068 mm->notifier_subscriptions;
Jason Gunthorpe99cb2522019-11-12 16:22:19 -04001069 int ret;
1070
Michel Lespinasse42fc5412020-06-08 21:33:44 -07001071 mmap_assert_write_locked(mm);
Jason Gunthorpe99cb2522019-11-12 16:22:19 -04001072
Jason Gunthorpe984cfe42019-12-18 13:40:35 -04001073 if (!subscriptions || !subscriptions->has_itree) {
Jason Gunthorpe99cb2522019-11-12 16:22:19 -04001074 ret = __mmu_notifier_register(NULL, mm);
1075 if (ret)
1076 return ret;
Jason Gunthorpe984cfe42019-12-18 13:40:35 -04001077 subscriptions = mm->notifier_subscriptions;
Jason Gunthorpe99cb2522019-11-12 16:22:19 -04001078 }
Jason Gunthorpe5292e242020-01-14 11:29:52 -04001079 return __mmu_interval_notifier_insert(interval_sub, mm, subscriptions,
1080 start, length, ops);
Jason Gunthorpe99cb2522019-11-12 16:22:19 -04001081}
1082EXPORT_SYMBOL_GPL(mmu_interval_notifier_insert_locked);
1083
Alistair Popple9ca66d72022-04-21 16:36:10 -07001084static bool
1085mmu_interval_seq_released(struct mmu_notifier_subscriptions *subscriptions,
1086 unsigned long seq)
1087{
1088 bool ret;
1089
1090 spin_lock(&subscriptions->lock);
1091 ret = subscriptions->invalidate_seq != seq;
1092 spin_unlock(&subscriptions->lock);
1093 return ret;
1094}
1095
Jason Gunthorpe99cb2522019-11-12 16:22:19 -04001096/**
1097 * mmu_interval_notifier_remove - Remove a interval notifier
Jason Gunthorpe5292e242020-01-14 11:29:52 -04001098 * @interval_sub: Interval subscription to unregister
Jason Gunthorpe99cb2522019-11-12 16:22:19 -04001099 *
1100 * This function must be paired with mmu_interval_notifier_insert(). It cannot
1101 * be called from any ops callback.
1102 *
1103 * Once this returns ops callbacks are no longer running on other CPUs and
1104 * will not be called in future.
1105 */
Jason Gunthorpe5292e242020-01-14 11:29:52 -04001106void mmu_interval_notifier_remove(struct mmu_interval_notifier *interval_sub)
Jason Gunthorpe99cb2522019-11-12 16:22:19 -04001107{
Jason Gunthorpe5292e242020-01-14 11:29:52 -04001108 struct mm_struct *mm = interval_sub->mm;
Jason Gunthorpe984cfe42019-12-18 13:40:35 -04001109 struct mmu_notifier_subscriptions *subscriptions =
1110 mm->notifier_subscriptions;
Jason Gunthorpe99cb2522019-11-12 16:22:19 -04001111 unsigned long seq = 0;
1112
1113 might_sleep();
1114
Jason Gunthorpe984cfe42019-12-18 13:40:35 -04001115 spin_lock(&subscriptions->lock);
1116 if (mn_itree_is_invalidating(subscriptions)) {
Jason Gunthorpe99cb2522019-11-12 16:22:19 -04001117 /*
1118 * remove is being called after insert put this on the
1119 * deferred list, but before the deferred list was processed.
1120 */
Jason Gunthorpe5292e242020-01-14 11:29:52 -04001121 if (RB_EMPTY_NODE(&interval_sub->interval_tree.rb)) {
1122 hlist_del(&interval_sub->deferred_item);
Jason Gunthorpe99cb2522019-11-12 16:22:19 -04001123 } else {
Jason Gunthorpe5292e242020-01-14 11:29:52 -04001124 hlist_add_head(&interval_sub->deferred_item,
Jason Gunthorpe984cfe42019-12-18 13:40:35 -04001125 &subscriptions->deferred_list);
1126 seq = subscriptions->invalidate_seq;
Jason Gunthorpe99cb2522019-11-12 16:22:19 -04001127 }
1128 } else {
Jason Gunthorpe5292e242020-01-14 11:29:52 -04001129 WARN_ON(RB_EMPTY_NODE(&interval_sub->interval_tree.rb));
1130 interval_tree_remove(&interval_sub->interval_tree,
Jason Gunthorpe984cfe42019-12-18 13:40:35 -04001131 &subscriptions->itree);
Jason Gunthorpe99cb2522019-11-12 16:22:19 -04001132 }
Jason Gunthorpe984cfe42019-12-18 13:40:35 -04001133 spin_unlock(&subscriptions->lock);
Jason Gunthorpe99cb2522019-11-12 16:22:19 -04001134
1135 /*
1136 * The possible sleep on progress in the invalidation requires the
1137 * caller not hold any locks held by invalidation callbacks.
1138 */
1139 lock_map_acquire(&__mmu_notifier_invalidate_range_start_map);
1140 lock_map_release(&__mmu_notifier_invalidate_range_start_map);
1141 if (seq)
Jason Gunthorpe984cfe42019-12-18 13:40:35 -04001142 wait_event(subscriptions->wq,
Alistair Popple9ca66d72022-04-21 16:36:10 -07001143 mmu_interval_seq_released(subscriptions, seq));
Jason Gunthorpe99cb2522019-11-12 16:22:19 -04001144
1145 /* pairs with mmgrab in mmu_interval_notifier_insert() */
1146 mmdrop(mm);
1147}
1148EXPORT_SYMBOL_GPL(mmu_interval_notifier_remove);
1149
Jason Gunthorpe2c7933f2019-08-06 20:15:40 -03001150/**
1151 * mmu_notifier_synchronize - Ensure all mmu_notifiers are freed
1152 *
1153 * This function ensures that all outstanding async SRU work from
1154 * mmu_notifier_put() is completed. After it returns any mmu_notifier_ops
1155 * associated with an unused mmu_notifier will no longer be called.
1156 *
1157 * Before using the caller must ensure that all of its mmu_notifiers have been
1158 * fully released via mmu_notifier_put().
1159 *
1160 * Modules using the mmu_notifier_put() API should call this in their __exit
1161 * function to avoid module unloading races.
1162 */
1163void mmu_notifier_synchronize(void)
1164{
1165 synchronize_srcu(&srcu);
1166}
1167EXPORT_SYMBOL_GPL(mmu_notifier_synchronize);
1168
Jérôme Glissec6d23412019-05-13 17:21:00 -07001169bool
1170mmu_notifier_range_update_to_read_only(const struct mmu_notifier_range *range)
1171{
1172 if (!range->vma || range->event != MMU_NOTIFY_PROTECTION_VMA)
1173 return false;
1174 /* Return true if the vma still have the read flag set. */
1175 return range->vma->vm_flags & VM_READ;
1176}
1177EXPORT_SYMBOL_GPL(mmu_notifier_range_update_to_read_only);
Suren Baghdasaryan5d8520b2021-11-24 07:56:04 -08001178
1179#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
1180
1181bool mmu_notifier_subscriptions_init(struct mm_struct *mm)
1182{
1183 struct mmu_notifier_subscriptions *subscriptions;
1184 struct percpu_rw_semaphore_atomic *sem;
1185
1186 subscriptions = kzalloc(
1187 sizeof(struct mmu_notifier_subscriptions), GFP_KERNEL);
1188 if (!subscriptions)
1189 return false;
1190
1191 sem = kzalloc(sizeof(struct percpu_rw_semaphore_atomic), GFP_KERNEL);
1192 if (!sem) {
1193 kfree(subscriptions);
1194 return false;
1195 }
1196 percpu_init_rwsem(&sem->rw_sem);
1197
1198 init_subscriptions(subscriptions);
1199 subscriptions->has_itree = true;
1200 subscriptions->hdr.valid = false;
1201 subscriptions->hdr.mmu_notifier_lock = sem;
1202 mm->notifier_subscriptions = subscriptions;
1203
1204 return true;
1205}
1206
1207void mmu_notifier_subscriptions_destroy(struct mm_struct *mm)
1208{
1209 percpu_rwsem_async_destroy(
1210 mm->notifier_subscriptions->hdr.mmu_notifier_lock);
1211 kfree(mm->notifier_subscriptions);
1212 mm->notifier_subscriptions = NULL;
1213}
1214
1215#endif /* CONFIG_SPECULATIVE_PAGE_FAULT */