]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - mm/mmu_notifier.c
KVM: s390: move s390-specific structs to uapi/asm/kvm.h
[thirdparty/kernel/stable.git] / mm / mmu_notifier.c
CommitLineData
20c8ccb1 1// SPDX-License-Identifier: GPL-2.0-only
cddb8a5c
AA
2/*
3 * linux/mm/mmu_notifier.c
4 *
5 * Copyright (C) 2008 Qumranet, Inc.
6 * Copyright (C) 2008 SGI
93e205a7 7 * Christoph Lameter <cl@linux.com>
cddb8a5c
AA
8 */
9
10#include <linux/rculist.h>
11#include <linux/mmu_notifier.h>
b95f1b31 12#include <linux/export.h>
cddb8a5c
AA
13#include <linux/mm.h>
14#include <linux/err.h>
99cb252f 15#include <linux/interval_tree.h>
21a92735 16#include <linux/srcu.h>
cddb8a5c
AA
17#include <linux/rcupdate.h>
18#include <linux/sched.h>
6e84f315 19#include <linux/sched/mm.h>
5a0e3ad6 20#include <linux/slab.h>
cddb8a5c 21
21a92735 22/* global SRCU for all MMs */
dde8da6c 23DEFINE_STATIC_SRCU(srcu);
21a92735 24
23b68395
DV
25#ifdef CONFIG_LOCKDEP
26struct lockdep_map __mmu_notifier_invalidate_range_start_map = {
27 .name = "mmu_notifier_invalidate_range_start"
28};
29#endif
30
56f434f4 31/*
984cfe4e
JG
32 * The mmu_notifier_subscriptions structure is allocated and installed in
33 * mm->notifier_subscriptions inside the mm_take_all_locks() protected
56f434f4
JG
34 * critical section and it's released only when mm_count reaches zero
35 * in mmdrop().
36 */
984cfe4e 37struct mmu_notifier_subscriptions {
56f434f4
JG
38 /* all mmu notifiers registered in this mm are queued in this list */
39 struct hlist_head list;
99cb252f 40 bool has_itree;
56f434f4
JG
41 /* to serialize the list modifications and hlist_unhashed */
42 spinlock_t lock;
99cb252f
JG
43 unsigned long invalidate_seq;
44 unsigned long active_invalidate_ranges;
45 struct rb_root_cached itree;
46 wait_queue_head_t wq;
47 struct hlist_head deferred_list;
56f434f4
JG
48};
49
99cb252f
JG
50/*
51 * This is a collision-retry read-side/write-side 'lock', a lot like a
52 * seqcount, however this allows multiple write-sides to hold it at
53 * once. Conceptually the write side is protecting the values of the PTEs in
54 * this mm, such that PTES cannot be read into SPTEs (shadow PTEs) while any
55 * writer exists.
56 *
57 * Note that the core mm creates nested invalidate_range_start()/end() regions
58 * within the same thread, and runs invalidate_range_start()/end() in parallel
59 * on multiple CPUs. This is designed to not reduce concurrency or block
60 * progress on the mm side.
61 *
62 * As a secondary function, holding the full write side also serves to prevent
63 * writers for the itree, this is an optimization to avoid extra locking
64 * during invalidate_range_start/end notifiers.
65 *
66 * The write side has two states, fully excluded:
67 * - mm->active_invalidate_ranges != 0
984cfe4e 68 * - subscriptions->invalidate_seq & 1 == True (odd)
99cb252f
JG
69 * - some range on the mm_struct is being invalidated
70 * - the itree is not allowed to change
71 *
72 * And partially excluded:
73 * - mm->active_invalidate_ranges != 0
984cfe4e 74 * - subscriptions->invalidate_seq & 1 == False (even)
99cb252f
JG
75 * - some range on the mm_struct is being invalidated
76 * - the itree is allowed to change
77 *
984cfe4e 78 * Operations on notifier_subscriptions->invalidate_seq (under spinlock):
99cb252f
JG
79 * seq |= 1 # Begin writing
80 * seq++ # Release the writing state
81 * seq & 1 # True if a writer exists
82 *
83 * The later state avoids some expensive work on inv_end in the common case of
5292e24a 84 * no mmu_interval_notifier monitoring the VA.
99cb252f 85 */
984cfe4e
JG
86static bool
87mn_itree_is_invalidating(struct mmu_notifier_subscriptions *subscriptions)
99cb252f 88{
984cfe4e
JG
89 lockdep_assert_held(&subscriptions->lock);
90 return subscriptions->invalidate_seq & 1;
99cb252f
JG
91}
92
93static struct mmu_interval_notifier *
984cfe4e 94mn_itree_inv_start_range(struct mmu_notifier_subscriptions *subscriptions,
99cb252f
JG
95 const struct mmu_notifier_range *range,
96 unsigned long *seq)
97{
98 struct interval_tree_node *node;
99 struct mmu_interval_notifier *res = NULL;
100
984cfe4e
JG
101 spin_lock(&subscriptions->lock);
102 subscriptions->active_invalidate_ranges++;
103 node = interval_tree_iter_first(&subscriptions->itree, range->start,
99cb252f
JG
104 range->end - 1);
105 if (node) {
984cfe4e 106 subscriptions->invalidate_seq |= 1;
99cb252f
JG
107 res = container_of(node, struct mmu_interval_notifier,
108 interval_tree);
109 }
110
984cfe4e
JG
111 *seq = subscriptions->invalidate_seq;
112 spin_unlock(&subscriptions->lock);
99cb252f
JG
113 return res;
114}
115
116static struct mmu_interval_notifier *
5292e24a 117mn_itree_inv_next(struct mmu_interval_notifier *interval_sub,
99cb252f
JG
118 const struct mmu_notifier_range *range)
119{
120 struct interval_tree_node *node;
121
5292e24a
JG
122 node = interval_tree_iter_next(&interval_sub->interval_tree,
123 range->start, range->end - 1);
99cb252f
JG
124 if (!node)
125 return NULL;
126 return container_of(node, struct mmu_interval_notifier, interval_tree);
127}
128
984cfe4e 129static void mn_itree_inv_end(struct mmu_notifier_subscriptions *subscriptions)
99cb252f 130{
5292e24a 131 struct mmu_interval_notifier *interval_sub;
99cb252f
JG
132 struct hlist_node *next;
133
984cfe4e
JG
134 spin_lock(&subscriptions->lock);
135 if (--subscriptions->active_invalidate_ranges ||
136 !mn_itree_is_invalidating(subscriptions)) {
137 spin_unlock(&subscriptions->lock);
99cb252f
JG
138 return;
139 }
140
141 /* Make invalidate_seq even */
984cfe4e 142 subscriptions->invalidate_seq++;
99cb252f
JG
143
144 /*
145 * The inv_end incorporates a deferred mechanism like rtnl_unlock().
146 * Adds and removes are queued until the final inv_end happens then
147 * they are progressed. This arrangement for tree updates is used to
148 * avoid using a blocking lock during invalidate_range_start.
149 */
5292e24a
JG
150 hlist_for_each_entry_safe(interval_sub, next,
151 &subscriptions->deferred_list,
99cb252f 152 deferred_item) {
5292e24a
JG
153 if (RB_EMPTY_NODE(&interval_sub->interval_tree.rb))
154 interval_tree_insert(&interval_sub->interval_tree,
984cfe4e 155 &subscriptions->itree);
99cb252f 156 else
5292e24a 157 interval_tree_remove(&interval_sub->interval_tree,
984cfe4e 158 &subscriptions->itree);
5292e24a 159 hlist_del(&interval_sub->deferred_item);
99cb252f 160 }
984cfe4e 161 spin_unlock(&subscriptions->lock);
99cb252f 162
984cfe4e 163 wake_up_all(&subscriptions->wq);
99cb252f
JG
164}
165
166/**
167 * mmu_interval_read_begin - Begin a read side critical section against a VA
168 * range
d49653f3 169 * @interval_sub: The interval subscription
99cb252f
JG
170 *
171 * mmu_iterval_read_begin()/mmu_iterval_read_retry() implement a
5292e24a
JG
172 * collision-retry scheme similar to seqcount for the VA range under
173 * subscription. If the mm invokes invalidation during the critical section
174 * then mmu_interval_read_retry() will return true.
99cb252f
JG
175 *
176 * This is useful to obtain shadow PTEs where teardown or setup of the SPTEs
177 * require a blocking context. The critical region formed by this can sleep,
178 * and the required 'user_lock' can also be a sleeping lock.
179 *
180 * The caller is required to provide a 'user_lock' to serialize both teardown
181 * and setup.
182 *
183 * The return value should be passed to mmu_interval_read_retry().
184 */
5292e24a
JG
185unsigned long
186mmu_interval_read_begin(struct mmu_interval_notifier *interval_sub)
99cb252f 187{
984cfe4e 188 struct mmu_notifier_subscriptions *subscriptions =
5292e24a 189 interval_sub->mm->notifier_subscriptions;
99cb252f
JG
190 unsigned long seq;
191 bool is_invalidating;
192
193 /*
5292e24a
JG
194 * If the subscription has a different seq value under the user_lock
195 * than we started with then it has collided.
99cb252f 196 *
5292e24a
JG
197 * If the subscription currently has the same seq value as the
198 * subscriptions seq, then it is currently between
199 * invalidate_start/end and is colliding.
99cb252f
JG
200 *
201 * The locking looks broadly like this:
57b037db 202 * mn_itree_inv_start(): mmu_interval_read_begin():
99cb252f 203 * spin_lock
5292e24a 204 * seq = READ_ONCE(interval_sub->invalidate_seq);
984cfe4e 205 * seq == subs->invalidate_seq
99cb252f
JG
206 * spin_unlock
207 * spin_lock
984cfe4e 208 * seq = ++subscriptions->invalidate_seq
99cb252f 209 * spin_unlock
57b037db 210 * op->invalidate():
99cb252f
JG
211 * user_lock
212 * mmu_interval_set_seq()
5292e24a 213 * interval_sub->invalidate_seq = seq
99cb252f
JG
214 * user_unlock
215 *
216 * [Required: mmu_interval_read_retry() == true]
217 *
218 * mn_itree_inv_end():
219 * spin_lock
984cfe4e 220 * seq = ++subscriptions->invalidate_seq
99cb252f
JG
221 * spin_unlock
222 *
223 * user_lock
224 * mmu_interval_read_retry():
5292e24a 225 * interval_sub->invalidate_seq != seq
99cb252f
JG
226 * user_unlock
227 *
228 * Barriers are not needed here as any races here are closed by an
229 * eventual mmu_interval_read_retry(), which provides a barrier via the
230 * user_lock.
231 */
984cfe4e 232 spin_lock(&subscriptions->lock);
99cb252f 233 /* Pairs with the WRITE_ONCE in mmu_interval_set_seq() */
5292e24a 234 seq = READ_ONCE(interval_sub->invalidate_seq);
984cfe4e
JG
235 is_invalidating = seq == subscriptions->invalidate_seq;
236 spin_unlock(&subscriptions->lock);
99cb252f
JG
237
238 /*
5292e24a 239 * interval_sub->invalidate_seq must always be set to an odd value via
99cb252f
JG
240 * mmu_interval_set_seq() using the provided cur_seq from
241 * mn_itree_inv_start_range(). This ensures that if seq does wrap we
242 * will always clear the below sleep in some reasonable time as
984cfe4e 243 * subscriptions->invalidate_seq is even in the idle state.
99cb252f
JG
244 */
245 lock_map_acquire(&__mmu_notifier_invalidate_range_start_map);
246 lock_map_release(&__mmu_notifier_invalidate_range_start_map);
247 if (is_invalidating)
984cfe4e
JG
248 wait_event(subscriptions->wq,
249 READ_ONCE(subscriptions->invalidate_seq) != seq);
99cb252f
JG
250
251 /*
252 * Notice that mmu_interval_read_retry() can already be true at this
253 * point, avoiding loops here allows the caller to provide a global
254 * time bound.
255 */
256
257 return seq;
258}
259EXPORT_SYMBOL_GPL(mmu_interval_read_begin);
260
984cfe4e 261static void mn_itree_release(struct mmu_notifier_subscriptions *subscriptions,
99cb252f
JG
262 struct mm_struct *mm)
263{
264 struct mmu_notifier_range range = {
265 .flags = MMU_NOTIFIER_RANGE_BLOCKABLE,
266 .event = MMU_NOTIFY_RELEASE,
267 .mm = mm,
268 .start = 0,
269 .end = ULONG_MAX,
270 };
5292e24a 271 struct mmu_interval_notifier *interval_sub;
99cb252f
JG
272 unsigned long cur_seq;
273 bool ret;
274
5292e24a
JG
275 for (interval_sub =
276 mn_itree_inv_start_range(subscriptions, &range, &cur_seq);
277 interval_sub;
278 interval_sub = mn_itree_inv_next(interval_sub, &range)) {
279 ret = interval_sub->ops->invalidate(interval_sub, &range,
280 cur_seq);
99cb252f
JG
281 WARN_ON(!ret);
282 }
283
984cfe4e 284 mn_itree_inv_end(subscriptions);
99cb252f
JG
285}
286
cddb8a5c
AA
287/*
288 * This function can't run concurrently against mmu_notifier_register
289 * because mm->mm_users > 0 during mmu_notifier_register and exit_mmap
290 * runs with mm_users == 0. Other tasks may still invoke mmu notifiers
291 * in parallel despite there being no task using this mm any more,
292 * through the vmas outside of the exit_mmap context, such as with
293 * vmtruncate. This serializes against mmu_notifier_unregister with
984cfe4e
JG
294 * the notifier_subscriptions->lock in addition to SRCU and it serializes
295 * against the other mmu notifiers with SRCU. struct mmu_notifier_subscriptions
cddb8a5c
AA
296 * can't go away from under us as exit_mmap holds an mm_count pin
297 * itself.
298 */
984cfe4e 299static void mn_hlist_release(struct mmu_notifier_subscriptions *subscriptions,
99cb252f 300 struct mm_struct *mm)
cddb8a5c 301{
1991722a 302 struct mmu_notifier *subscription;
21a92735 303 int id;
3ad3d901
XG
304
305 /*
d34883d4
XG
306 * SRCU here will block mmu_notifier_unregister until
307 * ->release returns.
3ad3d901 308 */
21a92735 309 id = srcu_read_lock(&srcu);
63886bad
QC
310 hlist_for_each_entry_rcu(subscription, &subscriptions->list, hlist,
311 srcu_read_lock_held(&srcu))
d34883d4
XG
312 /*
313 * If ->release runs before mmu_notifier_unregister it must be
314 * handled, as it's the only way for the driver to flush all
315 * existing sptes and stop the driver from establishing any more
316 * sptes before all the pages in the mm are freed.
317 */
1991722a
JG
318 if (subscription->ops->release)
319 subscription->ops->release(subscription, mm);
d34883d4 320
984cfe4e
JG
321 spin_lock(&subscriptions->lock);
322 while (unlikely(!hlist_empty(&subscriptions->list))) {
1991722a
JG
323 subscription = hlist_entry(subscriptions->list.first,
324 struct mmu_notifier, hlist);
cddb8a5c 325 /*
d34883d4
XG
326 * We arrived before mmu_notifier_unregister so
327 * mmu_notifier_unregister will do nothing other than to wait
328 * for ->release to finish and for mmu_notifier_unregister to
329 * return.
cddb8a5c 330 */
1991722a 331 hlist_del_init_rcu(&subscription->hlist);
cddb8a5c 332 }
984cfe4e 333 spin_unlock(&subscriptions->lock);
b972216e 334 srcu_read_unlock(&srcu, id);
cddb8a5c
AA
335
336 /*
d34883d4
XG
337 * synchronize_srcu here prevents mmu_notifier_release from returning to
338 * exit_mmap (which would proceed with freeing all pages in the mm)
339 * until the ->release method returns, if it was invoked by
340 * mmu_notifier_unregister.
341 *
984cfe4e
JG
342 * The notifier_subscriptions can't go away from under us because
343 * one mm_count is held by exit_mmap.
cddb8a5c 344 */
21a92735 345 synchronize_srcu(&srcu);
cddb8a5c
AA
346}
347
99cb252f
JG
348void __mmu_notifier_release(struct mm_struct *mm)
349{
984cfe4e
JG
350 struct mmu_notifier_subscriptions *subscriptions =
351 mm->notifier_subscriptions;
99cb252f 352
984cfe4e
JG
353 if (subscriptions->has_itree)
354 mn_itree_release(subscriptions, mm);
99cb252f 355
984cfe4e
JG
356 if (!hlist_empty(&subscriptions->list))
357 mn_hlist_release(subscriptions, mm);
99cb252f
JG
358}
359
cddb8a5c
AA
360/*
361 * If no young bitflag is supported by the hardware, ->clear_flush_young can
362 * unmap the address and return 1 or 0 depending if the mapping previously
363 * existed or not.
364 */
365int __mmu_notifier_clear_flush_young(struct mm_struct *mm,
57128468
ALC
366 unsigned long start,
367 unsigned long end)
cddb8a5c 368{
1991722a 369 struct mmu_notifier *subscription;
21a92735 370 int young = 0, id;
cddb8a5c 371
21a92735 372 id = srcu_read_lock(&srcu);
1991722a 373 hlist_for_each_entry_rcu(subscription,
63886bad
QC
374 &mm->notifier_subscriptions->list, hlist,
375 srcu_read_lock_held(&srcu)) {
1991722a
JG
376 if (subscription->ops->clear_flush_young)
377 young |= subscription->ops->clear_flush_young(
378 subscription, mm, start, end);
cddb8a5c 379 }
21a92735 380 srcu_read_unlock(&srcu, id);
cddb8a5c
AA
381
382 return young;
383}
384
1d7715c6
VD
385int __mmu_notifier_clear_young(struct mm_struct *mm,
386 unsigned long start,
387 unsigned long end)
388{
1991722a 389 struct mmu_notifier *subscription;
1d7715c6
VD
390 int young = 0, id;
391
392 id = srcu_read_lock(&srcu);
1991722a 393 hlist_for_each_entry_rcu(subscription,
63886bad
QC
394 &mm->notifier_subscriptions->list, hlist,
395 srcu_read_lock_held(&srcu)) {
1991722a
JG
396 if (subscription->ops->clear_young)
397 young |= subscription->ops->clear_young(subscription,
398 mm, start, end);
1d7715c6
VD
399 }
400 srcu_read_unlock(&srcu, id);
401
402 return young;
403}
404
8ee53820
AA
405int __mmu_notifier_test_young(struct mm_struct *mm,
406 unsigned long address)
407{
1991722a 408 struct mmu_notifier *subscription;
21a92735 409 int young = 0, id;
8ee53820 410
21a92735 411 id = srcu_read_lock(&srcu);
1991722a 412 hlist_for_each_entry_rcu(subscription,
63886bad
QC
413 &mm->notifier_subscriptions->list, hlist,
414 srcu_read_lock_held(&srcu)) {
1991722a
JG
415 if (subscription->ops->test_young) {
416 young = subscription->ops->test_young(subscription, mm,
417 address);
8ee53820
AA
418 if (young)
419 break;
420 }
421 }
21a92735 422 srcu_read_unlock(&srcu, id);
8ee53820
AA
423
424 return young;
425}
426
828502d3
IE
427void __mmu_notifier_change_pte(struct mm_struct *mm, unsigned long address,
428 pte_t pte)
429{
1991722a 430 struct mmu_notifier *subscription;
21a92735 431 int id;
828502d3 432
21a92735 433 id = srcu_read_lock(&srcu);
1991722a 434 hlist_for_each_entry_rcu(subscription,
63886bad
QC
435 &mm->notifier_subscriptions->list, hlist,
436 srcu_read_lock_held(&srcu)) {
1991722a
JG
437 if (subscription->ops->change_pte)
438 subscription->ops->change_pte(subscription, mm, address,
439 pte);
828502d3 440 }
21a92735 441 srcu_read_unlock(&srcu, id);
828502d3
IE
442}
443
984cfe4e 444static int mn_itree_invalidate(struct mmu_notifier_subscriptions *subscriptions,
99cb252f
JG
445 const struct mmu_notifier_range *range)
446{
5292e24a 447 struct mmu_interval_notifier *interval_sub;
99cb252f
JG
448 unsigned long cur_seq;
449
5292e24a
JG
450 for (interval_sub =
451 mn_itree_inv_start_range(subscriptions, range, &cur_seq);
452 interval_sub;
453 interval_sub = mn_itree_inv_next(interval_sub, range)) {
99cb252f
JG
454 bool ret;
455
5292e24a
JG
456 ret = interval_sub->ops->invalidate(interval_sub, range,
457 cur_seq);
99cb252f
JG
458 if (!ret) {
459 if (WARN_ON(mmu_notifier_range_blockable(range)))
460 continue;
461 goto out_would_block;
462 }
463 }
464 return 0;
465
466out_would_block:
467 /*
468 * On -EAGAIN the non-blocking caller is not allowed to call
469 * invalidate_range_end()
470 */
984cfe4e 471 mn_itree_inv_end(subscriptions);
99cb252f
JG
472 return -EAGAIN;
473}
474
984cfe4e
JG
475static int mn_hlist_invalidate_range_start(
476 struct mmu_notifier_subscriptions *subscriptions,
477 struct mmu_notifier_range *range)
cddb8a5c 478{
1991722a 479 struct mmu_notifier *subscription;
93065ac7 480 int ret = 0;
21a92735 481 int id;
cddb8a5c 482
21a92735 483 id = srcu_read_lock(&srcu);
63886bad
QC
484 hlist_for_each_entry_rcu(subscription, &subscriptions->list, hlist,
485 srcu_read_lock_held(&srcu)) {
1991722a
JG
486 const struct mmu_notifier_ops *ops = subscription->ops;
487
488 if (ops->invalidate_range_start) {
ba170f76
DV
489 int _ret;
490
491 if (!mmu_notifier_range_blockable(range))
492 non_block_start();
1991722a 493 _ret = ops->invalidate_range_start(subscription, range);
ba170f76
DV
494 if (!mmu_notifier_range_blockable(range))
495 non_block_end();
93065ac7
MH
496 if (_ret) {
497 pr_info("%pS callback failed with %d in %sblockable context.\n",
1991722a
JG
498 ops->invalidate_range_start, _ret,
499 !mmu_notifier_range_blockable(range) ?
500 "non-" :
501 "");
8402ce61 502 WARN_ON(mmu_notifier_range_blockable(range) ||
df2ec764 503 _ret != -EAGAIN);
c2655835
SC
504 /*
505 * We call all the notifiers on any EAGAIN,
506 * there is no way for a notifier to know if
507 * its start method failed, thus a start that
508 * does EAGAIN can't also do end.
509 */
510 WARN_ON(ops->invalidate_range_end);
93065ac7
MH
511 ret = _ret;
512 }
513 }
cddb8a5c 514 }
c2655835
SC
515
516 if (ret) {
517 /*
518 * Must be non-blocking to get here. If there are multiple
519 * notifiers and one or more failed start, any that succeeded
520 * start are expecting their end to be called. Do so now.
521 */
522 hlist_for_each_entry_rcu(subscription, &subscriptions->list,
523 hlist, srcu_read_lock_held(&srcu)) {
524 if (!subscription->ops->invalidate_range_end)
525 continue;
526
527 subscription->ops->invalidate_range_end(subscription,
528 range);
529 }
530 }
21a92735 531 srcu_read_unlock(&srcu, id);
93065ac7
MH
532
533 return ret;
cddb8a5c
AA
534}
535
99cb252f
JG
536int __mmu_notifier_invalidate_range_start(struct mmu_notifier_range *range)
537{
984cfe4e
JG
538 struct mmu_notifier_subscriptions *subscriptions =
539 range->mm->notifier_subscriptions;
99cb252f
JG
540 int ret;
541
984cfe4e
JG
542 if (subscriptions->has_itree) {
543 ret = mn_itree_invalidate(subscriptions, range);
99cb252f
JG
544 if (ret)
545 return ret;
546 }
984cfe4e
JG
547 if (!hlist_empty(&subscriptions->list))
548 return mn_hlist_invalidate_range_start(subscriptions, range);
99cb252f
JG
549 return 0;
550}
551
984cfe4e
JG
552static void
553mn_hlist_invalidate_end(struct mmu_notifier_subscriptions *subscriptions,
ec8832d0 554 struct mmu_notifier_range *range)
cddb8a5c 555{
1991722a 556 struct mmu_notifier *subscription;
21a92735 557 int id;
cddb8a5c 558
21a92735 559 id = srcu_read_lock(&srcu);
63886bad
QC
560 hlist_for_each_entry_rcu(subscription, &subscriptions->list, hlist,
561 srcu_read_lock_held(&srcu)) {
1991722a 562 if (subscription->ops->invalidate_range_end) {
ba170f76
DV
563 if (!mmu_notifier_range_blockable(range))
564 non_block_start();
1991722a
JG
565 subscription->ops->invalidate_range_end(subscription,
566 range);
ba170f76
DV
567 if (!mmu_notifier_range_blockable(range))
568 non_block_end();
569 }
cddb8a5c 570 }
21a92735 571 srcu_read_unlock(&srcu, id);
99cb252f
JG
572}
573
ec8832d0 574void __mmu_notifier_invalidate_range_end(struct mmu_notifier_range *range)
99cb252f 575{
984cfe4e
JG
576 struct mmu_notifier_subscriptions *subscriptions =
577 range->mm->notifier_subscriptions;
99cb252f
JG
578
579 lock_map_acquire(&__mmu_notifier_invalidate_range_start_map);
984cfe4e
JG
580 if (subscriptions->has_itree)
581 mn_itree_inv_end(subscriptions);
99cb252f 582
984cfe4e 583 if (!hlist_empty(&subscriptions->list))
ec8832d0 584 mn_hlist_invalidate_end(subscriptions, range);
23b68395 585 lock_map_release(&__mmu_notifier_invalidate_range_start_map);
cddb8a5c
AA
586}
587
1af5a810
AP
588void __mmu_notifier_arch_invalidate_secondary_tlbs(struct mm_struct *mm,
589 unsigned long start, unsigned long end)
0f0a327f 590{
1991722a 591 struct mmu_notifier *subscription;
0f0a327f
JR
592 int id;
593
594 id = srcu_read_lock(&srcu);
1991722a 595 hlist_for_each_entry_rcu(subscription,
63886bad
QC
596 &mm->notifier_subscriptions->list, hlist,
597 srcu_read_lock_held(&srcu)) {
1af5a810
AP
598 if (subscription->ops->arch_invalidate_secondary_tlbs)
599 subscription->ops->arch_invalidate_secondary_tlbs(
600 subscription, mm,
601 start, end);
0f0a327f
JR
602 }
603 srcu_read_unlock(&srcu, id);
604}
0f0a327f 605
56c57103 606/*
c1e8d7c6 607 * Same as mmu_notifier_register but here the caller must hold the mmap_lock in
99cb252f
JG
608 * write mode. A NULL mn signals the notifier is being registered for itree
609 * mode.
56c57103 610 */
1991722a
JG
611int __mmu_notifier_register(struct mmu_notifier *subscription,
612 struct mm_struct *mm)
cddb8a5c 613{
984cfe4e 614 struct mmu_notifier_subscriptions *subscriptions = NULL;
cddb8a5c
AA
615 int ret;
616
42fc5414 617 mmap_assert_write_locked(mm);
cddb8a5c
AA
618 BUG_ON(atomic_read(&mm->mm_users) <= 0);
619
1af5a810
AP
620 /*
621 * Subsystems should only register for invalidate_secondary_tlbs() or
622 * invalidate_range_start()/end() callbacks, not both.
623 */
624 if (WARN_ON_ONCE(subscription &&
625 (subscription->ops->arch_invalidate_secondary_tlbs &&
626 (subscription->ops->invalidate_range_start ||
627 subscription->ops->invalidate_range_end))))
628 return -EINVAL;
629
984cfe4e 630 if (!mm->notifier_subscriptions) {
70df291b
JG
631 /*
632 * kmalloc cannot be called under mm_take_all_locks(), but we
984cfe4e 633 * know that mm->notifier_subscriptions can't change while we
c1e8d7c6 634 * hold the write side of the mmap_lock.
70df291b 635 */
984cfe4e
JG
636 subscriptions = kzalloc(
637 sizeof(struct mmu_notifier_subscriptions), GFP_KERNEL);
638 if (!subscriptions)
70df291b
JG
639 return -ENOMEM;
640
984cfe4e
JG
641 INIT_HLIST_HEAD(&subscriptions->list);
642 spin_lock_init(&subscriptions->lock);
643 subscriptions->invalidate_seq = 2;
644 subscriptions->itree = RB_ROOT_CACHED;
645 init_waitqueue_head(&subscriptions->wq);
646 INIT_HLIST_HEAD(&subscriptions->deferred_list);
70df291b 647 }
35cfa2b0 648
cddb8a5c
AA
649 ret = mm_take_all_locks(mm);
650 if (unlikely(ret))
35cfa2b0 651 goto out_clean;
cddb8a5c 652
cddb8a5c
AA
653 /*
654 * Serialize the update against mmu_notifier_unregister. A
655 * side note: mmu_notifier_release can't run concurrently with
656 * us because we hold the mm_users pin (either implicitly as
657 * current->mm or explicitly with get_task_mm() or similar).
658 * We can't race against any other mmu notifier method either
659 * thanks to mm_take_all_locks().
99cb252f 660 *
984cfe4e
JG
661 * release semantics on the initialization of the
662 * mmu_notifier_subscriptions's contents are provided for unlocked
663 * readers. acquire can only be used while holding the mmgrab or
664 * mmget, and is safe because once created the
665 * mmu_notifier_subscriptions is not freed until the mm is destroyed.
c1e8d7c6 666 * As above, users holding the mmap_lock or one of the
99cb252f 667 * mm_take_all_locks() do not need to use acquire semantics.
cddb8a5c 668 */
984cfe4e
JG
669 if (subscriptions)
670 smp_store_release(&mm->notifier_subscriptions, subscriptions);
70df291b 671
1991722a 672 if (subscription) {
99cb252f
JG
673 /* Pairs with the mmdrop in mmu_notifier_unregister_* */
674 mmgrab(mm);
1991722a
JG
675 subscription->mm = mm;
676 subscription->users = 1;
99cb252f 677
984cfe4e 678 spin_lock(&mm->notifier_subscriptions->lock);
1991722a 679 hlist_add_head_rcu(&subscription->hlist,
984cfe4e
JG
680 &mm->notifier_subscriptions->list);
681 spin_unlock(&mm->notifier_subscriptions->lock);
99cb252f 682 } else
984cfe4e 683 mm->notifier_subscriptions->has_itree = true;
cddb8a5c
AA
684
685 mm_drop_all_locks(mm);
70df291b
JG
686 BUG_ON(atomic_read(&mm->mm_users) <= 0);
687 return 0;
688
35cfa2b0 689out_clean:
984cfe4e 690 kfree(subscriptions);
cddb8a5c
AA
691 return ret;
692}
56c57103 693EXPORT_SYMBOL_GPL(__mmu_notifier_register);
cddb8a5c 694
2c7933f5
JG
695/**
696 * mmu_notifier_register - Register a notifier on a mm
d49653f3 697 * @subscription: The notifier to attach
2c7933f5
JG
698 * @mm: The mm to attach the notifier to
699 *
c1e8d7c6 700 * Must not hold mmap_lock nor any other VM related lock when calling
cddb8a5c
AA
701 * this registration function. Must also ensure mm_users can't go down
702 * to zero while this runs to avoid races with mmu_notifier_release,
703 * so mm has to be current->mm or the mm should be pinned safely such
704 * as with get_task_mm(). If the mm is not current->mm, the mm_users
705 * pin should be released by calling mmput after mmu_notifier_register
2c7933f5
JG
706 * returns.
707 *
708 * mmu_notifier_unregister() or mmu_notifier_put() must be always called to
709 * unregister the notifier.
710 *
1991722a 711 * While the caller has a mmu_notifier get the subscription->mm pointer will remain
2c7933f5 712 * valid, and can be converted to an active mm pointer via mmget_not_zero().
cddb8a5c 713 */
1991722a
JG
714int mmu_notifier_register(struct mmu_notifier *subscription,
715 struct mm_struct *mm)
cddb8a5c 716{
56c57103 717 int ret;
cddb8a5c 718
d8ed45c5 719 mmap_write_lock(mm);
1991722a 720 ret = __mmu_notifier_register(subscription, mm);
d8ed45c5 721 mmap_write_unlock(mm);
56c57103 722 return ret;
cddb8a5c 723}
56c57103 724EXPORT_SYMBOL_GPL(mmu_notifier_register);
cddb8a5c 725
2c7933f5
JG
726static struct mmu_notifier *
727find_get_mmu_notifier(struct mm_struct *mm, const struct mmu_notifier_ops *ops)
728{
1991722a 729 struct mmu_notifier *subscription;
2c7933f5 730
984cfe4e 731 spin_lock(&mm->notifier_subscriptions->lock);
1991722a 732 hlist_for_each_entry_rcu(subscription,
63886bad
QC
733 &mm->notifier_subscriptions->list, hlist,
734 lockdep_is_held(&mm->notifier_subscriptions->lock)) {
1991722a 735 if (subscription->ops != ops)
2c7933f5
JG
736 continue;
737
1991722a
JG
738 if (likely(subscription->users != UINT_MAX))
739 subscription->users++;
2c7933f5 740 else
1991722a 741 subscription = ERR_PTR(-EOVERFLOW);
984cfe4e 742 spin_unlock(&mm->notifier_subscriptions->lock);
1991722a 743 return subscription;
2c7933f5 744 }
984cfe4e 745 spin_unlock(&mm->notifier_subscriptions->lock);
2c7933f5
JG
746 return NULL;
747}
748
749/**
750 * mmu_notifier_get_locked - Return the single struct mmu_notifier for
751 * the mm & ops
752 * @ops: The operations struct being subscribe with
753 * @mm : The mm to attach notifiers too
754 *
755 * This function either allocates a new mmu_notifier via
756 * ops->alloc_notifier(), or returns an already existing notifier on the
757 * list. The value of the ops pointer is used to determine when two notifiers
758 * are the same.
759 *
760 * Each call to mmu_notifier_get() must be paired with a call to
c1e8d7c6 761 * mmu_notifier_put(). The caller must hold the write side of mm->mmap_lock.
2c7933f5
JG
762 *
763 * While the caller has a mmu_notifier get the mm pointer will remain valid,
764 * and can be converted to an active mm pointer via mmget_not_zero().
765 */
766struct mmu_notifier *mmu_notifier_get_locked(const struct mmu_notifier_ops *ops,
767 struct mm_struct *mm)
768{
1991722a 769 struct mmu_notifier *subscription;
2c7933f5
JG
770 int ret;
771
42fc5414 772 mmap_assert_write_locked(mm);
2c7933f5 773
984cfe4e 774 if (mm->notifier_subscriptions) {
1991722a
JG
775 subscription = find_get_mmu_notifier(mm, ops);
776 if (subscription)
777 return subscription;
2c7933f5
JG
778 }
779
1991722a
JG
780 subscription = ops->alloc_notifier(mm);
781 if (IS_ERR(subscription))
782 return subscription;
783 subscription->ops = ops;
784 ret = __mmu_notifier_register(subscription, mm);
2c7933f5
JG
785 if (ret)
786 goto out_free;
1991722a 787 return subscription;
2c7933f5 788out_free:
1991722a 789 subscription->ops->free_notifier(subscription);
2c7933f5
JG
790 return ERR_PTR(ret);
791}
792EXPORT_SYMBOL_GPL(mmu_notifier_get_locked);
793
cddb8a5c 794/* this is called after the last mmu_notifier_unregister() returned */
984cfe4e 795void __mmu_notifier_subscriptions_destroy(struct mm_struct *mm)
cddb8a5c 796{
984cfe4e
JG
797 BUG_ON(!hlist_empty(&mm->notifier_subscriptions->list));
798 kfree(mm->notifier_subscriptions);
799 mm->notifier_subscriptions = LIST_POISON1; /* debug */
cddb8a5c
AA
800}
801
802/*
803 * This releases the mm_count pin automatically and frees the mm
804 * structure if it was the last user of it. It serializes against
21a92735
SG
805 * running mmu notifiers with SRCU and against mmu_notifier_unregister
806 * with the unregister lock + SRCU. All sptes must be dropped before
cddb8a5c
AA
807 * calling mmu_notifier_unregister. ->release or any other notifier
808 * method may be invoked concurrently with mmu_notifier_unregister,
809 * and only after mmu_notifier_unregister returned we're guaranteed
810 * that ->release or any other method can't run anymore.
811 */
1991722a
JG
812void mmu_notifier_unregister(struct mmu_notifier *subscription,
813 struct mm_struct *mm)
cddb8a5c
AA
814{
815 BUG_ON(atomic_read(&mm->mm_count) <= 0);
816
1991722a 817 if (!hlist_unhashed(&subscription->hlist)) {
d34883d4
XG
818 /*
819 * SRCU here will force exit_mmap to wait for ->release to
820 * finish before freeing the pages.
821 */
21a92735 822 int id;
3ad3d901 823
d34883d4 824 id = srcu_read_lock(&srcu);
cddb8a5c 825 /*
d34883d4
XG
826 * exit_mmap will block in mmu_notifier_release to guarantee
827 * that ->release is called before freeing the pages.
cddb8a5c 828 */
1991722a
JG
829 if (subscription->ops->release)
830 subscription->ops->release(subscription, mm);
d34883d4 831 srcu_read_unlock(&srcu, id);
3ad3d901 832
984cfe4e 833 spin_lock(&mm->notifier_subscriptions->lock);
751efd86 834 /*
d34883d4
XG
835 * Can not use list_del_rcu() since __mmu_notifier_release
836 * can delete it before we hold the lock.
751efd86 837 */
1991722a 838 hlist_del_init_rcu(&subscription->hlist);
984cfe4e 839 spin_unlock(&mm->notifier_subscriptions->lock);
d34883d4 840 }
cddb8a5c
AA
841
842 /*
d34883d4 843 * Wait for any running method to finish, of course including
83a35e36 844 * ->release if it was run by mmu_notifier_release instead of us.
cddb8a5c 845 */
21a92735 846 synchronize_srcu(&srcu);
cddb8a5c
AA
847
848 BUG_ON(atomic_read(&mm->mm_count) <= 0);
849
850 mmdrop(mm);
851}
852EXPORT_SYMBOL_GPL(mmu_notifier_unregister);
21a92735 853
2c7933f5
JG
854static void mmu_notifier_free_rcu(struct rcu_head *rcu)
855{
1991722a
JG
856 struct mmu_notifier *subscription =
857 container_of(rcu, struct mmu_notifier, rcu);
858 struct mm_struct *mm = subscription->mm;
2c7933f5 859
1991722a 860 subscription->ops->free_notifier(subscription);
2c7933f5
JG
861 /* Pairs with the get in __mmu_notifier_register() */
862 mmdrop(mm);
863}
864
865/**
866 * mmu_notifier_put - Release the reference on the notifier
d49653f3 867 * @subscription: The notifier to act on
2c7933f5
JG
868 *
869 * This function must be paired with each mmu_notifier_get(), it releases the
870 * reference obtained by the get. If this is the last reference then process
871 * to free the notifier will be run asynchronously.
872 *
873 * Unlike mmu_notifier_unregister() the get/put flow only calls ops->release
874 * when the mm_struct is destroyed. Instead free_notifier is always called to
875 * release any resources held by the user.
876 *
877 * As ops->release is not guaranteed to be called, the user must ensure that
878 * all sptes are dropped, and no new sptes can be established before
879 * mmu_notifier_put() is called.
880 *
881 * This function can be called from the ops->release callback, however the
882 * caller must still ensure it is called pairwise with mmu_notifier_get().
883 *
884 * Modules calling this function must call mmu_notifier_synchronize() in
885 * their __exit functions to ensure the async work is completed.
886 */
1991722a 887void mmu_notifier_put(struct mmu_notifier *subscription)
2c7933f5 888{
1991722a 889 struct mm_struct *mm = subscription->mm;
2c7933f5 890
984cfe4e 891 spin_lock(&mm->notifier_subscriptions->lock);
1991722a 892 if (WARN_ON(!subscription->users) || --subscription->users)
2c7933f5 893 goto out_unlock;
1991722a 894 hlist_del_init_rcu(&subscription->hlist);
984cfe4e 895 spin_unlock(&mm->notifier_subscriptions->lock);
2c7933f5 896
1991722a 897 call_srcu(&srcu, &subscription->rcu, mmu_notifier_free_rcu);
2c7933f5
JG
898 return;
899
900out_unlock:
984cfe4e 901 spin_unlock(&mm->notifier_subscriptions->lock);
2c7933f5
JG
902}
903EXPORT_SYMBOL_GPL(mmu_notifier_put);
904
99cb252f 905static int __mmu_interval_notifier_insert(
5292e24a 906 struct mmu_interval_notifier *interval_sub, struct mm_struct *mm,
984cfe4e 907 struct mmu_notifier_subscriptions *subscriptions, unsigned long start,
99cb252f
JG
908 unsigned long length, const struct mmu_interval_notifier_ops *ops)
909{
5292e24a
JG
910 interval_sub->mm = mm;
911 interval_sub->ops = ops;
912 RB_CLEAR_NODE(&interval_sub->interval_tree.rb);
913 interval_sub->interval_tree.start = start;
99cb252f
JG
914 /*
915 * Note that the representation of the intervals in the interval tree
916 * considers the ending point as contained in the interval.
917 */
918 if (length == 0 ||
5292e24a
JG
919 check_add_overflow(start, length - 1,
920 &interval_sub->interval_tree.last))
99cb252f
JG
921 return -EOVERFLOW;
922
923 /* Must call with a mmget() held */
c9682d10 924 if (WARN_ON(atomic_read(&mm->mm_users) <= 0))
99cb252f
JG
925 return -EINVAL;
926
927 /* pairs with mmdrop in mmu_interval_notifier_remove() */
928 mmgrab(mm);
929
930 /*
931 * If some invalidate_range_start/end region is going on in parallel
932 * we don't know what VA ranges are affected, so we must assume this
933 * new range is included.
934 *
935 * If the itree is invalidating then we are not allowed to change
936 * it. Retrying until invalidation is done is tricky due to the
937 * possibility for live lock, instead defer the add to
938 * mn_itree_inv_end() so this algorithm is deterministic.
939 *
5292e24a 940 * In all cases the value for the interval_sub->invalidate_seq should be
99cb252f
JG
941 * odd, see mmu_interval_read_begin()
942 */
984cfe4e
JG
943 spin_lock(&subscriptions->lock);
944 if (subscriptions->active_invalidate_ranges) {
945 if (mn_itree_is_invalidating(subscriptions))
5292e24a 946 hlist_add_head(&interval_sub->deferred_item,
984cfe4e 947 &subscriptions->deferred_list);
99cb252f 948 else {
984cfe4e 949 subscriptions->invalidate_seq |= 1;
5292e24a 950 interval_tree_insert(&interval_sub->interval_tree,
984cfe4e 951 &subscriptions->itree);
99cb252f 952 }
5292e24a 953 interval_sub->invalidate_seq = subscriptions->invalidate_seq;
99cb252f 954 } else {
984cfe4e 955 WARN_ON(mn_itree_is_invalidating(subscriptions));
99cb252f 956 /*
5292e24a
JG
957 * The starting seq for a subscription not under invalidation
958 * should be odd, not equal to the current invalidate_seq and
99cb252f
JG
959 * invalidate_seq should not 'wrap' to the new seq any time
960 * soon.
961 */
5292e24a
JG
962 interval_sub->invalidate_seq =
963 subscriptions->invalidate_seq - 1;
964 interval_tree_insert(&interval_sub->interval_tree,
984cfe4e 965 &subscriptions->itree);
99cb252f 966 }
984cfe4e 967 spin_unlock(&subscriptions->lock);
99cb252f
JG
968 return 0;
969}
970
971/**
972 * mmu_interval_notifier_insert - Insert an interval notifier
5292e24a 973 * @interval_sub: Interval subscription to register
99cb252f
JG
974 * @start: Starting virtual address to monitor
975 * @length: Length of the range to monitor
d49653f3
KK
976 * @mm: mm_struct to attach to
977 * @ops: Interval notifier operations to be called on matching events
99cb252f
JG
978 *
979 * This function subscribes the interval notifier for notifications from the
980 * mm. Upon return the ops related to mmu_interval_notifier will be called
981 * whenever an event that intersects with the given range occurs.
982 *
983 * Upon return the range_notifier may not be present in the interval tree yet.
984 * The caller must use the normal interval notifier read flow via
985 * mmu_interval_read_begin() to establish SPTEs for this range.
986 */
5292e24a 987int mmu_interval_notifier_insert(struct mmu_interval_notifier *interval_sub,
99cb252f
JG
988 struct mm_struct *mm, unsigned long start,
989 unsigned long length,
990 const struct mmu_interval_notifier_ops *ops)
991{
984cfe4e 992 struct mmu_notifier_subscriptions *subscriptions;
99cb252f
JG
993 int ret;
994
da1c55f1 995 might_lock(&mm->mmap_lock);
99cb252f 996
984cfe4e
JG
997 subscriptions = smp_load_acquire(&mm->notifier_subscriptions);
998 if (!subscriptions || !subscriptions->has_itree) {
99cb252f
JG
999 ret = mmu_notifier_register(NULL, mm);
1000 if (ret)
1001 return ret;
984cfe4e 1002 subscriptions = mm->notifier_subscriptions;
99cb252f 1003 }
5292e24a
JG
1004 return __mmu_interval_notifier_insert(interval_sub, mm, subscriptions,
1005 start, length, ops);
99cb252f
JG
1006}
1007EXPORT_SYMBOL_GPL(mmu_interval_notifier_insert);
1008
1009int mmu_interval_notifier_insert_locked(
5292e24a 1010 struct mmu_interval_notifier *interval_sub, struct mm_struct *mm,
99cb252f
JG
1011 unsigned long start, unsigned long length,
1012 const struct mmu_interval_notifier_ops *ops)
1013{
984cfe4e
JG
1014 struct mmu_notifier_subscriptions *subscriptions =
1015 mm->notifier_subscriptions;
99cb252f
JG
1016 int ret;
1017
42fc5414 1018 mmap_assert_write_locked(mm);
99cb252f 1019
984cfe4e 1020 if (!subscriptions || !subscriptions->has_itree) {
99cb252f
JG
1021 ret = __mmu_notifier_register(NULL, mm);
1022 if (ret)
1023 return ret;
984cfe4e 1024 subscriptions = mm->notifier_subscriptions;
99cb252f 1025 }
5292e24a
JG
1026 return __mmu_interval_notifier_insert(interval_sub, mm, subscriptions,
1027 start, length, ops);
99cb252f
JG
1028}
1029EXPORT_SYMBOL_GPL(mmu_interval_notifier_insert_locked);
1030
31956166
AP
1031static bool
1032mmu_interval_seq_released(struct mmu_notifier_subscriptions *subscriptions,
1033 unsigned long seq)
1034{
1035 bool ret;
1036
1037 spin_lock(&subscriptions->lock);
1038 ret = subscriptions->invalidate_seq != seq;
1039 spin_unlock(&subscriptions->lock);
1040 return ret;
1041}
1042
99cb252f
JG
1043/**
1044 * mmu_interval_notifier_remove - Remove a interval notifier
5292e24a 1045 * @interval_sub: Interval subscription to unregister
99cb252f
JG
1046 *
1047 * This function must be paired with mmu_interval_notifier_insert(). It cannot
1048 * be called from any ops callback.
1049 *
1050 * Once this returns ops callbacks are no longer running on other CPUs and
1051 * will not be called in future.
1052 */
5292e24a 1053void mmu_interval_notifier_remove(struct mmu_interval_notifier *interval_sub)
99cb252f 1054{
5292e24a 1055 struct mm_struct *mm = interval_sub->mm;
984cfe4e
JG
1056 struct mmu_notifier_subscriptions *subscriptions =
1057 mm->notifier_subscriptions;
99cb252f
JG
1058 unsigned long seq = 0;
1059
1060 might_sleep();
1061
984cfe4e
JG
1062 spin_lock(&subscriptions->lock);
1063 if (mn_itree_is_invalidating(subscriptions)) {
99cb252f
JG
1064 /*
1065 * remove is being called after insert put this on the
1066 * deferred list, but before the deferred list was processed.
1067 */
5292e24a
JG
1068 if (RB_EMPTY_NODE(&interval_sub->interval_tree.rb)) {
1069 hlist_del(&interval_sub->deferred_item);
99cb252f 1070 } else {
5292e24a 1071 hlist_add_head(&interval_sub->deferred_item,
984cfe4e
JG
1072 &subscriptions->deferred_list);
1073 seq = subscriptions->invalidate_seq;
99cb252f
JG
1074 }
1075 } else {
5292e24a
JG
1076 WARN_ON(RB_EMPTY_NODE(&interval_sub->interval_tree.rb));
1077 interval_tree_remove(&interval_sub->interval_tree,
984cfe4e 1078 &subscriptions->itree);
99cb252f 1079 }
984cfe4e 1080 spin_unlock(&subscriptions->lock);
99cb252f
JG
1081
1082 /*
1083 * The possible sleep on progress in the invalidation requires the
1084 * caller not hold any locks held by invalidation callbacks.
1085 */
1086 lock_map_acquire(&__mmu_notifier_invalidate_range_start_map);
1087 lock_map_release(&__mmu_notifier_invalidate_range_start_map);
1088 if (seq)
984cfe4e 1089 wait_event(subscriptions->wq,
31956166 1090 mmu_interval_seq_released(subscriptions, seq));
99cb252f
JG
1091
1092 /* pairs with mmgrab in mmu_interval_notifier_insert() */
1093 mmdrop(mm);
1094}
1095EXPORT_SYMBOL_GPL(mmu_interval_notifier_remove);
1096
2c7933f5
JG
1097/**
1098 * mmu_notifier_synchronize - Ensure all mmu_notifiers are freed
1099 *
1100 * This function ensures that all outstanding async SRU work from
1101 * mmu_notifier_put() is completed. After it returns any mmu_notifier_ops
1102 * associated with an unused mmu_notifier will no longer be called.
1103 *
1104 * Before using the caller must ensure that all of its mmu_notifiers have been
1105 * fully released via mmu_notifier_put().
1106 *
1107 * Modules using the mmu_notifier_put() API should call this in their __exit
1108 * function to avoid module unloading races.
1109 */
1110void mmu_notifier_synchronize(void)
1111{
1112 synchronize_srcu(&srcu);
1113}
1114EXPORT_SYMBOL_GPL(mmu_notifier_synchronize);