Commit | Line | Data |
---|---|---|
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 | 23 | DEFINE_STATIC_SRCU(srcu); |
21a92735 | 24 | |
23b68395 DV |
25 | #ifdef CONFIG_LOCKDEP |
26 | struct 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 | 37 | struct 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 |
86 | static bool |
87 | mn_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 | ||
93 | static struct mmu_interval_notifier * | |
984cfe4e | 94 | mn_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 | ||
116 | static struct mmu_interval_notifier * | |
5292e24a | 117 | mn_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 | 129 | static 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 | |
5292e24a | 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 |
185 | unsigned long |
186 | mmu_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: | |
202 | * mn_tree_invalidate_start(): mmu_interval_read_begin(): | |
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 JG |
209 | * spin_unlock |
210 | * op->invalidate_range(): | |
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 | } | |
259 | EXPORT_SYMBOL_GPL(mmu_interval_read_begin); | |
260 | ||
984cfe4e | 261 | static 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 | 299 | static 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); |
1991722a | 310 | hlist_for_each_entry_rcu(subscription, &subscriptions->list, hlist) |
d34883d4 XG |
311 | /* |
312 | * If ->release runs before mmu_notifier_unregister it must be | |
313 | * handled, as it's the only way for the driver to flush all | |
314 | * existing sptes and stop the driver from establishing any more | |
315 | * sptes before all the pages in the mm are freed. | |
316 | */ | |
1991722a JG |
317 | if (subscription->ops->release) |
318 | subscription->ops->release(subscription, mm); | |
d34883d4 | 319 | |
984cfe4e JG |
320 | spin_lock(&subscriptions->lock); |
321 | while (unlikely(!hlist_empty(&subscriptions->list))) { | |
1991722a JG |
322 | subscription = hlist_entry(subscriptions->list.first, |
323 | struct mmu_notifier, hlist); | |
cddb8a5c | 324 | /* |
d34883d4 XG |
325 | * We arrived before mmu_notifier_unregister so |
326 | * mmu_notifier_unregister will do nothing other than to wait | |
327 | * for ->release to finish and for mmu_notifier_unregister to | |
328 | * return. | |
cddb8a5c | 329 | */ |
1991722a | 330 | hlist_del_init_rcu(&subscription->hlist); |
cddb8a5c | 331 | } |
984cfe4e | 332 | spin_unlock(&subscriptions->lock); |
b972216e | 333 | srcu_read_unlock(&srcu, id); |
cddb8a5c AA |
334 | |
335 | /* | |
d34883d4 XG |
336 | * synchronize_srcu here prevents mmu_notifier_release from returning to |
337 | * exit_mmap (which would proceed with freeing all pages in the mm) | |
338 | * until the ->release method returns, if it was invoked by | |
339 | * mmu_notifier_unregister. | |
340 | * | |
984cfe4e JG |
341 | * The notifier_subscriptions can't go away from under us because |
342 | * one mm_count is held by exit_mmap. | |
cddb8a5c | 343 | */ |
21a92735 | 344 | synchronize_srcu(&srcu); |
cddb8a5c AA |
345 | } |
346 | ||
99cb252f JG |
347 | void __mmu_notifier_release(struct mm_struct *mm) |
348 | { | |
984cfe4e JG |
349 | struct mmu_notifier_subscriptions *subscriptions = |
350 | mm->notifier_subscriptions; | |
99cb252f | 351 | |
984cfe4e JG |
352 | if (subscriptions->has_itree) |
353 | mn_itree_release(subscriptions, mm); | |
99cb252f | 354 | |
984cfe4e JG |
355 | if (!hlist_empty(&subscriptions->list)) |
356 | mn_hlist_release(subscriptions, mm); | |
99cb252f JG |
357 | } |
358 | ||
cddb8a5c AA |
359 | /* |
360 | * If no young bitflag is supported by the hardware, ->clear_flush_young can | |
361 | * unmap the address and return 1 or 0 depending if the mapping previously | |
362 | * existed or not. | |
363 | */ | |
364 | int __mmu_notifier_clear_flush_young(struct mm_struct *mm, | |
57128468 ALC |
365 | unsigned long start, |
366 | unsigned long end) | |
cddb8a5c | 367 | { |
1991722a | 368 | struct mmu_notifier *subscription; |
21a92735 | 369 | int young = 0, id; |
cddb8a5c | 370 | |
21a92735 | 371 | id = srcu_read_lock(&srcu); |
1991722a JG |
372 | hlist_for_each_entry_rcu(subscription, |
373 | &mm->notifier_subscriptions->list, hlist) { | |
374 | if (subscription->ops->clear_flush_young) | |
375 | young |= subscription->ops->clear_flush_young( | |
376 | subscription, mm, start, end); | |
cddb8a5c | 377 | } |
21a92735 | 378 | srcu_read_unlock(&srcu, id); |
cddb8a5c AA |
379 | |
380 | return young; | |
381 | } | |
382 | ||
1d7715c6 VD |
383 | int __mmu_notifier_clear_young(struct mm_struct *mm, |
384 | unsigned long start, | |
385 | unsigned long end) | |
386 | { | |
1991722a | 387 | struct mmu_notifier *subscription; |
1d7715c6 VD |
388 | int young = 0, id; |
389 | ||
390 | id = srcu_read_lock(&srcu); | |
1991722a JG |
391 | hlist_for_each_entry_rcu(subscription, |
392 | &mm->notifier_subscriptions->list, hlist) { | |
393 | if (subscription->ops->clear_young) | |
394 | young |= subscription->ops->clear_young(subscription, | |
395 | mm, start, end); | |
1d7715c6 VD |
396 | } |
397 | srcu_read_unlock(&srcu, id); | |
398 | ||
399 | return young; | |
400 | } | |
401 | ||
8ee53820 AA |
402 | int __mmu_notifier_test_young(struct mm_struct *mm, |
403 | unsigned long address) | |
404 | { | |
1991722a | 405 | struct mmu_notifier *subscription; |
21a92735 | 406 | int young = 0, id; |
8ee53820 | 407 | |
21a92735 | 408 | id = srcu_read_lock(&srcu); |
1991722a JG |
409 | hlist_for_each_entry_rcu(subscription, |
410 | &mm->notifier_subscriptions->list, hlist) { | |
411 | if (subscription->ops->test_young) { | |
412 | young = subscription->ops->test_young(subscription, mm, | |
413 | address); | |
8ee53820 AA |
414 | if (young) |
415 | break; | |
416 | } | |
417 | } | |
21a92735 | 418 | srcu_read_unlock(&srcu, id); |
8ee53820 AA |
419 | |
420 | return young; | |
421 | } | |
422 | ||
828502d3 IE |
423 | void __mmu_notifier_change_pte(struct mm_struct *mm, unsigned long address, |
424 | pte_t pte) | |
425 | { | |
1991722a | 426 | struct mmu_notifier *subscription; |
21a92735 | 427 | int id; |
828502d3 | 428 | |
21a92735 | 429 | id = srcu_read_lock(&srcu); |
1991722a JG |
430 | hlist_for_each_entry_rcu(subscription, |
431 | &mm->notifier_subscriptions->list, hlist) { | |
432 | if (subscription->ops->change_pte) | |
433 | subscription->ops->change_pte(subscription, mm, address, | |
434 | pte); | |
828502d3 | 435 | } |
21a92735 | 436 | srcu_read_unlock(&srcu, id); |
828502d3 IE |
437 | } |
438 | ||
984cfe4e | 439 | static int mn_itree_invalidate(struct mmu_notifier_subscriptions *subscriptions, |
99cb252f JG |
440 | const struct mmu_notifier_range *range) |
441 | { | |
5292e24a | 442 | struct mmu_interval_notifier *interval_sub; |
99cb252f JG |
443 | unsigned long cur_seq; |
444 | ||
5292e24a JG |
445 | for (interval_sub = |
446 | mn_itree_inv_start_range(subscriptions, range, &cur_seq); | |
447 | interval_sub; | |
448 | interval_sub = mn_itree_inv_next(interval_sub, range)) { | |
99cb252f JG |
449 | bool ret; |
450 | ||
5292e24a JG |
451 | ret = interval_sub->ops->invalidate(interval_sub, range, |
452 | cur_seq); | |
99cb252f JG |
453 | if (!ret) { |
454 | if (WARN_ON(mmu_notifier_range_blockable(range))) | |
455 | continue; | |
456 | goto out_would_block; | |
457 | } | |
458 | } | |
459 | return 0; | |
460 | ||
461 | out_would_block: | |
462 | /* | |
463 | * On -EAGAIN the non-blocking caller is not allowed to call | |
464 | * invalidate_range_end() | |
465 | */ | |
984cfe4e | 466 | mn_itree_inv_end(subscriptions); |
99cb252f JG |
467 | return -EAGAIN; |
468 | } | |
469 | ||
984cfe4e JG |
470 | static int mn_hlist_invalidate_range_start( |
471 | struct mmu_notifier_subscriptions *subscriptions, | |
472 | struct mmu_notifier_range *range) | |
cddb8a5c | 473 | { |
1991722a | 474 | struct mmu_notifier *subscription; |
93065ac7 | 475 | int ret = 0; |
21a92735 | 476 | int id; |
cddb8a5c | 477 | |
21a92735 | 478 | id = srcu_read_lock(&srcu); |
1991722a JG |
479 | hlist_for_each_entry_rcu(subscription, &subscriptions->list, hlist) { |
480 | const struct mmu_notifier_ops *ops = subscription->ops; | |
481 | ||
482 | if (ops->invalidate_range_start) { | |
ba170f76 DV |
483 | int _ret; |
484 | ||
485 | if (!mmu_notifier_range_blockable(range)) | |
486 | non_block_start(); | |
1991722a | 487 | _ret = ops->invalidate_range_start(subscription, range); |
ba170f76 DV |
488 | if (!mmu_notifier_range_blockable(range)) |
489 | non_block_end(); | |
93065ac7 MH |
490 | if (_ret) { |
491 | pr_info("%pS callback failed with %d in %sblockable context.\n", | |
1991722a JG |
492 | ops->invalidate_range_start, _ret, |
493 | !mmu_notifier_range_blockable(range) ? | |
494 | "non-" : | |
495 | ""); | |
8402ce61 | 496 | WARN_ON(mmu_notifier_range_blockable(range) || |
df2ec764 | 497 | _ret != -EAGAIN); |
93065ac7 MH |
498 | ret = _ret; |
499 | } | |
500 | } | |
cddb8a5c | 501 | } |
21a92735 | 502 | srcu_read_unlock(&srcu, id); |
93065ac7 MH |
503 | |
504 | return ret; | |
cddb8a5c AA |
505 | } |
506 | ||
99cb252f JG |
507 | int __mmu_notifier_invalidate_range_start(struct mmu_notifier_range *range) |
508 | { | |
984cfe4e JG |
509 | struct mmu_notifier_subscriptions *subscriptions = |
510 | range->mm->notifier_subscriptions; | |
99cb252f JG |
511 | int ret; |
512 | ||
984cfe4e JG |
513 | if (subscriptions->has_itree) { |
514 | ret = mn_itree_invalidate(subscriptions, range); | |
99cb252f JG |
515 | if (ret) |
516 | return ret; | |
517 | } | |
984cfe4e JG |
518 | if (!hlist_empty(&subscriptions->list)) |
519 | return mn_hlist_invalidate_range_start(subscriptions, range); | |
99cb252f JG |
520 | return 0; |
521 | } | |
522 | ||
984cfe4e JG |
523 | static void |
524 | mn_hlist_invalidate_end(struct mmu_notifier_subscriptions *subscriptions, | |
525 | struct mmu_notifier_range *range, bool only_end) | |
cddb8a5c | 526 | { |
1991722a | 527 | struct mmu_notifier *subscription; |
21a92735 | 528 | int id; |
cddb8a5c | 529 | |
21a92735 | 530 | id = srcu_read_lock(&srcu); |
1991722a | 531 | hlist_for_each_entry_rcu(subscription, &subscriptions->list, hlist) { |
0f0a327f JR |
532 | /* |
533 | * Call invalidate_range here too to avoid the need for the | |
534 | * subsystem of having to register an invalidate_range_end | |
535 | * call-back when there is invalidate_range already. Usually a | |
536 | * subsystem registers either invalidate_range_start()/end() or | |
537 | * invalidate_range(), so this will be no additional overhead | |
538 | * (besides the pointer check). | |
4645b9fe JG |
539 | * |
540 | * We skip call to invalidate_range() if we know it is safe ie | |
541 | * call site use mmu_notifier_invalidate_range_only_end() which | |
542 | * is safe to do when we know that a call to invalidate_range() | |
543 | * already happen under page table lock. | |
0f0a327f | 544 | */ |
1991722a JG |
545 | if (!only_end && subscription->ops->invalidate_range) |
546 | subscription->ops->invalidate_range(subscription, | |
547 | range->mm, | |
548 | range->start, | |
549 | range->end); | |
550 | if (subscription->ops->invalidate_range_end) { | |
ba170f76 DV |
551 | if (!mmu_notifier_range_blockable(range)) |
552 | non_block_start(); | |
1991722a JG |
553 | subscription->ops->invalidate_range_end(subscription, |
554 | range); | |
ba170f76 DV |
555 | if (!mmu_notifier_range_blockable(range)) |
556 | non_block_end(); | |
557 | } | |
cddb8a5c | 558 | } |
21a92735 | 559 | srcu_read_unlock(&srcu, id); |
99cb252f JG |
560 | } |
561 | ||
562 | void __mmu_notifier_invalidate_range_end(struct mmu_notifier_range *range, | |
563 | bool only_end) | |
564 | { | |
984cfe4e JG |
565 | struct mmu_notifier_subscriptions *subscriptions = |
566 | range->mm->notifier_subscriptions; | |
99cb252f JG |
567 | |
568 | lock_map_acquire(&__mmu_notifier_invalidate_range_start_map); | |
984cfe4e JG |
569 | if (subscriptions->has_itree) |
570 | mn_itree_inv_end(subscriptions); | |
99cb252f | 571 | |
984cfe4e JG |
572 | if (!hlist_empty(&subscriptions->list)) |
573 | mn_hlist_invalidate_end(subscriptions, range, only_end); | |
23b68395 | 574 | lock_map_release(&__mmu_notifier_invalidate_range_start_map); |
cddb8a5c AA |
575 | } |
576 | ||
0f0a327f JR |
577 | void __mmu_notifier_invalidate_range(struct mm_struct *mm, |
578 | unsigned long start, unsigned long end) | |
579 | { | |
1991722a | 580 | struct mmu_notifier *subscription; |
0f0a327f JR |
581 | int id; |
582 | ||
583 | id = srcu_read_lock(&srcu); | |
1991722a JG |
584 | hlist_for_each_entry_rcu(subscription, |
585 | &mm->notifier_subscriptions->list, hlist) { | |
586 | if (subscription->ops->invalidate_range) | |
587 | subscription->ops->invalidate_range(subscription, mm, | |
588 | start, end); | |
0f0a327f JR |
589 | } |
590 | srcu_read_unlock(&srcu, id); | |
591 | } | |
0f0a327f | 592 | |
56c57103 | 593 | /* |
99cb252f JG |
594 | * Same as mmu_notifier_register but here the caller must hold the mmap_sem in |
595 | * write mode. A NULL mn signals the notifier is being registered for itree | |
596 | * mode. | |
56c57103 | 597 | */ |
1991722a JG |
598 | int __mmu_notifier_register(struct mmu_notifier *subscription, |
599 | struct mm_struct *mm) | |
cddb8a5c | 600 | { |
984cfe4e | 601 | struct mmu_notifier_subscriptions *subscriptions = NULL; |
cddb8a5c AA |
602 | int ret; |
603 | ||
56c57103 | 604 | lockdep_assert_held_write(&mm->mmap_sem); |
cddb8a5c AA |
605 | BUG_ON(atomic_read(&mm->mm_users) <= 0); |
606 | ||
66204f1d DV |
607 | if (IS_ENABLED(CONFIG_LOCKDEP)) { |
608 | fs_reclaim_acquire(GFP_KERNEL); | |
609 | lock_map_acquire(&__mmu_notifier_invalidate_range_start_map); | |
610 | lock_map_release(&__mmu_notifier_invalidate_range_start_map); | |
611 | fs_reclaim_release(GFP_KERNEL); | |
612 | } | |
613 | ||
984cfe4e | 614 | if (!mm->notifier_subscriptions) { |
70df291b JG |
615 | /* |
616 | * kmalloc cannot be called under mm_take_all_locks(), but we | |
984cfe4e JG |
617 | * know that mm->notifier_subscriptions can't change while we |
618 | * hold the write side of the mmap_sem. | |
70df291b | 619 | */ |
984cfe4e JG |
620 | subscriptions = kzalloc( |
621 | sizeof(struct mmu_notifier_subscriptions), GFP_KERNEL); | |
622 | if (!subscriptions) | |
70df291b JG |
623 | return -ENOMEM; |
624 | ||
984cfe4e JG |
625 | INIT_HLIST_HEAD(&subscriptions->list); |
626 | spin_lock_init(&subscriptions->lock); | |
627 | subscriptions->invalidate_seq = 2; | |
628 | subscriptions->itree = RB_ROOT_CACHED; | |
629 | init_waitqueue_head(&subscriptions->wq); | |
630 | INIT_HLIST_HEAD(&subscriptions->deferred_list); | |
70df291b | 631 | } |
35cfa2b0 | 632 | |
cddb8a5c AA |
633 | ret = mm_take_all_locks(mm); |
634 | if (unlikely(ret)) | |
35cfa2b0 | 635 | goto out_clean; |
cddb8a5c | 636 | |
cddb8a5c AA |
637 | /* |
638 | * Serialize the update against mmu_notifier_unregister. A | |
639 | * side note: mmu_notifier_release can't run concurrently with | |
640 | * us because we hold the mm_users pin (either implicitly as | |
641 | * current->mm or explicitly with get_task_mm() or similar). | |
642 | * We can't race against any other mmu notifier method either | |
643 | * thanks to mm_take_all_locks(). | |
99cb252f | 644 | * |
984cfe4e JG |
645 | * release semantics on the initialization of the |
646 | * mmu_notifier_subscriptions's contents are provided for unlocked | |
647 | * readers. acquire can only be used while holding the mmgrab or | |
648 | * mmget, and is safe because once created the | |
649 | * mmu_notifier_subscriptions is not freed until the mm is destroyed. | |
650 | * As above, users holding the mmap_sem or one of the | |
99cb252f | 651 | * mm_take_all_locks() do not need to use acquire semantics. |
cddb8a5c | 652 | */ |
984cfe4e JG |
653 | if (subscriptions) |
654 | smp_store_release(&mm->notifier_subscriptions, subscriptions); | |
70df291b | 655 | |
1991722a | 656 | if (subscription) { |
99cb252f JG |
657 | /* Pairs with the mmdrop in mmu_notifier_unregister_* */ |
658 | mmgrab(mm); | |
1991722a JG |
659 | subscription->mm = mm; |
660 | subscription->users = 1; | |
99cb252f | 661 | |
984cfe4e | 662 | spin_lock(&mm->notifier_subscriptions->lock); |
1991722a | 663 | hlist_add_head_rcu(&subscription->hlist, |
984cfe4e JG |
664 | &mm->notifier_subscriptions->list); |
665 | spin_unlock(&mm->notifier_subscriptions->lock); | |
99cb252f | 666 | } else |
984cfe4e | 667 | mm->notifier_subscriptions->has_itree = true; |
cddb8a5c AA |
668 | |
669 | mm_drop_all_locks(mm); | |
70df291b JG |
670 | BUG_ON(atomic_read(&mm->mm_users) <= 0); |
671 | return 0; | |
672 | ||
35cfa2b0 | 673 | out_clean: |
984cfe4e | 674 | kfree(subscriptions); |
cddb8a5c AA |
675 | return ret; |
676 | } | |
56c57103 | 677 | EXPORT_SYMBOL_GPL(__mmu_notifier_register); |
cddb8a5c | 678 | |
2c7933f5 JG |
679 | /** |
680 | * mmu_notifier_register - Register a notifier on a mm | |
681 | * @mn: The notifier to attach | |
682 | * @mm: The mm to attach the notifier to | |
683 | * | |
cddb8a5c AA |
684 | * Must not hold mmap_sem nor any other VM related lock when calling |
685 | * this registration function. Must also ensure mm_users can't go down | |
686 | * to zero while this runs to avoid races with mmu_notifier_release, | |
687 | * so mm has to be current->mm or the mm should be pinned safely such | |
688 | * as with get_task_mm(). If the mm is not current->mm, the mm_users | |
689 | * pin should be released by calling mmput after mmu_notifier_register | |
2c7933f5 JG |
690 | * returns. |
691 | * | |
692 | * mmu_notifier_unregister() or mmu_notifier_put() must be always called to | |
693 | * unregister the notifier. | |
694 | * | |
1991722a | 695 | * While the caller has a mmu_notifier get the subscription->mm pointer will remain |
2c7933f5 | 696 | * valid, and can be converted to an active mm pointer via mmget_not_zero(). |
cddb8a5c | 697 | */ |
1991722a JG |
698 | int mmu_notifier_register(struct mmu_notifier *subscription, |
699 | struct mm_struct *mm) | |
cddb8a5c | 700 | { |
56c57103 | 701 | int ret; |
cddb8a5c | 702 | |
56c57103 | 703 | down_write(&mm->mmap_sem); |
1991722a | 704 | ret = __mmu_notifier_register(subscription, mm); |
56c57103 JG |
705 | up_write(&mm->mmap_sem); |
706 | return ret; | |
cddb8a5c | 707 | } |
56c57103 | 708 | EXPORT_SYMBOL_GPL(mmu_notifier_register); |
cddb8a5c | 709 | |
2c7933f5 JG |
710 | static struct mmu_notifier * |
711 | find_get_mmu_notifier(struct mm_struct *mm, const struct mmu_notifier_ops *ops) | |
712 | { | |
1991722a | 713 | struct mmu_notifier *subscription; |
2c7933f5 | 714 | |
984cfe4e | 715 | spin_lock(&mm->notifier_subscriptions->lock); |
1991722a JG |
716 | hlist_for_each_entry_rcu(subscription, |
717 | &mm->notifier_subscriptions->list, hlist) { | |
718 | if (subscription->ops != ops) | |
2c7933f5 JG |
719 | continue; |
720 | ||
1991722a JG |
721 | if (likely(subscription->users != UINT_MAX)) |
722 | subscription->users++; | |
2c7933f5 | 723 | else |
1991722a | 724 | subscription = ERR_PTR(-EOVERFLOW); |
984cfe4e | 725 | spin_unlock(&mm->notifier_subscriptions->lock); |
1991722a | 726 | return subscription; |
2c7933f5 | 727 | } |
984cfe4e | 728 | spin_unlock(&mm->notifier_subscriptions->lock); |
2c7933f5 JG |
729 | return NULL; |
730 | } | |
731 | ||
732 | /** | |
733 | * mmu_notifier_get_locked - Return the single struct mmu_notifier for | |
734 | * the mm & ops | |
735 | * @ops: The operations struct being subscribe with | |
736 | * @mm : The mm to attach notifiers too | |
737 | * | |
738 | * This function either allocates a new mmu_notifier via | |
739 | * ops->alloc_notifier(), or returns an already existing notifier on the | |
740 | * list. The value of the ops pointer is used to determine when two notifiers | |
741 | * are the same. | |
742 | * | |
743 | * Each call to mmu_notifier_get() must be paired with a call to | |
744 | * mmu_notifier_put(). The caller must hold the write side of mm->mmap_sem. | |
745 | * | |
746 | * While the caller has a mmu_notifier get the mm pointer will remain valid, | |
747 | * and can be converted to an active mm pointer via mmget_not_zero(). | |
748 | */ | |
749 | struct mmu_notifier *mmu_notifier_get_locked(const struct mmu_notifier_ops *ops, | |
750 | struct mm_struct *mm) | |
751 | { | |
1991722a | 752 | struct mmu_notifier *subscription; |
2c7933f5 JG |
753 | int ret; |
754 | ||
755 | lockdep_assert_held_write(&mm->mmap_sem); | |
756 | ||
984cfe4e | 757 | if (mm->notifier_subscriptions) { |
1991722a JG |
758 | subscription = find_get_mmu_notifier(mm, ops); |
759 | if (subscription) | |
760 | return subscription; | |
2c7933f5 JG |
761 | } |
762 | ||
1991722a JG |
763 | subscription = ops->alloc_notifier(mm); |
764 | if (IS_ERR(subscription)) | |
765 | return subscription; | |
766 | subscription->ops = ops; | |
767 | ret = __mmu_notifier_register(subscription, mm); | |
2c7933f5 JG |
768 | if (ret) |
769 | goto out_free; | |
1991722a | 770 | return subscription; |
2c7933f5 | 771 | out_free: |
1991722a | 772 | subscription->ops->free_notifier(subscription); |
2c7933f5 JG |
773 | return ERR_PTR(ret); |
774 | } | |
775 | EXPORT_SYMBOL_GPL(mmu_notifier_get_locked); | |
776 | ||
cddb8a5c | 777 | /* this is called after the last mmu_notifier_unregister() returned */ |
984cfe4e | 778 | void __mmu_notifier_subscriptions_destroy(struct mm_struct *mm) |
cddb8a5c | 779 | { |
984cfe4e JG |
780 | BUG_ON(!hlist_empty(&mm->notifier_subscriptions->list)); |
781 | kfree(mm->notifier_subscriptions); | |
782 | mm->notifier_subscriptions = LIST_POISON1; /* debug */ | |
cddb8a5c AA |
783 | } |
784 | ||
785 | /* | |
786 | * This releases the mm_count pin automatically and frees the mm | |
787 | * structure if it was the last user of it. It serializes against | |
21a92735 SG |
788 | * running mmu notifiers with SRCU and against mmu_notifier_unregister |
789 | * with the unregister lock + SRCU. All sptes must be dropped before | |
cddb8a5c AA |
790 | * calling mmu_notifier_unregister. ->release or any other notifier |
791 | * method may be invoked concurrently with mmu_notifier_unregister, | |
792 | * and only after mmu_notifier_unregister returned we're guaranteed | |
793 | * that ->release or any other method can't run anymore. | |
794 | */ | |
1991722a JG |
795 | void mmu_notifier_unregister(struct mmu_notifier *subscription, |
796 | struct mm_struct *mm) | |
cddb8a5c AA |
797 | { |
798 | BUG_ON(atomic_read(&mm->mm_count) <= 0); | |
799 | ||
1991722a | 800 | if (!hlist_unhashed(&subscription->hlist)) { |
d34883d4 XG |
801 | /* |
802 | * SRCU here will force exit_mmap to wait for ->release to | |
803 | * finish before freeing the pages. | |
804 | */ | |
21a92735 | 805 | int id; |
3ad3d901 | 806 | |
d34883d4 | 807 | id = srcu_read_lock(&srcu); |
cddb8a5c | 808 | /* |
d34883d4 XG |
809 | * exit_mmap will block in mmu_notifier_release to guarantee |
810 | * that ->release is called before freeing the pages. | |
cddb8a5c | 811 | */ |
1991722a JG |
812 | if (subscription->ops->release) |
813 | subscription->ops->release(subscription, mm); | |
d34883d4 | 814 | srcu_read_unlock(&srcu, id); |
3ad3d901 | 815 | |
984cfe4e | 816 | spin_lock(&mm->notifier_subscriptions->lock); |
751efd86 | 817 | /* |
d34883d4 XG |
818 | * Can not use list_del_rcu() since __mmu_notifier_release |
819 | * can delete it before we hold the lock. | |
751efd86 | 820 | */ |
1991722a | 821 | hlist_del_init_rcu(&subscription->hlist); |
984cfe4e | 822 | spin_unlock(&mm->notifier_subscriptions->lock); |
d34883d4 | 823 | } |
cddb8a5c AA |
824 | |
825 | /* | |
d34883d4 | 826 | * Wait for any running method to finish, of course including |
83a35e36 | 827 | * ->release if it was run by mmu_notifier_release instead of us. |
cddb8a5c | 828 | */ |
21a92735 | 829 | synchronize_srcu(&srcu); |
cddb8a5c AA |
830 | |
831 | BUG_ON(atomic_read(&mm->mm_count) <= 0); | |
832 | ||
833 | mmdrop(mm); | |
834 | } | |
835 | EXPORT_SYMBOL_GPL(mmu_notifier_unregister); | |
21a92735 | 836 | |
2c7933f5 JG |
837 | static void mmu_notifier_free_rcu(struct rcu_head *rcu) |
838 | { | |
1991722a JG |
839 | struct mmu_notifier *subscription = |
840 | container_of(rcu, struct mmu_notifier, rcu); | |
841 | struct mm_struct *mm = subscription->mm; | |
2c7933f5 | 842 | |
1991722a | 843 | subscription->ops->free_notifier(subscription); |
2c7933f5 JG |
844 | /* Pairs with the get in __mmu_notifier_register() */ |
845 | mmdrop(mm); | |
846 | } | |
847 | ||
848 | /** | |
849 | * mmu_notifier_put - Release the reference on the notifier | |
850 | * @mn: The notifier to act on | |
851 | * | |
852 | * This function must be paired with each mmu_notifier_get(), it releases the | |
853 | * reference obtained by the get. If this is the last reference then process | |
854 | * to free the notifier will be run asynchronously. | |
855 | * | |
856 | * Unlike mmu_notifier_unregister() the get/put flow only calls ops->release | |
857 | * when the mm_struct is destroyed. Instead free_notifier is always called to | |
858 | * release any resources held by the user. | |
859 | * | |
860 | * As ops->release is not guaranteed to be called, the user must ensure that | |
861 | * all sptes are dropped, and no new sptes can be established before | |
862 | * mmu_notifier_put() is called. | |
863 | * | |
864 | * This function can be called from the ops->release callback, however the | |
865 | * caller must still ensure it is called pairwise with mmu_notifier_get(). | |
866 | * | |
867 | * Modules calling this function must call mmu_notifier_synchronize() in | |
868 | * their __exit functions to ensure the async work is completed. | |
869 | */ | |
1991722a | 870 | void mmu_notifier_put(struct mmu_notifier *subscription) |
2c7933f5 | 871 | { |
1991722a | 872 | struct mm_struct *mm = subscription->mm; |
2c7933f5 | 873 | |
984cfe4e | 874 | spin_lock(&mm->notifier_subscriptions->lock); |
1991722a | 875 | if (WARN_ON(!subscription->users) || --subscription->users) |
2c7933f5 | 876 | goto out_unlock; |
1991722a | 877 | hlist_del_init_rcu(&subscription->hlist); |
984cfe4e | 878 | spin_unlock(&mm->notifier_subscriptions->lock); |
2c7933f5 | 879 | |
1991722a | 880 | call_srcu(&srcu, &subscription->rcu, mmu_notifier_free_rcu); |
2c7933f5 JG |
881 | return; |
882 | ||
883 | out_unlock: | |
984cfe4e | 884 | spin_unlock(&mm->notifier_subscriptions->lock); |
2c7933f5 JG |
885 | } |
886 | EXPORT_SYMBOL_GPL(mmu_notifier_put); | |
887 | ||
99cb252f | 888 | static int __mmu_interval_notifier_insert( |
5292e24a | 889 | struct mmu_interval_notifier *interval_sub, struct mm_struct *mm, |
984cfe4e | 890 | struct mmu_notifier_subscriptions *subscriptions, unsigned long start, |
99cb252f JG |
891 | unsigned long length, const struct mmu_interval_notifier_ops *ops) |
892 | { | |
5292e24a JG |
893 | interval_sub->mm = mm; |
894 | interval_sub->ops = ops; | |
895 | RB_CLEAR_NODE(&interval_sub->interval_tree.rb); | |
896 | interval_sub->interval_tree.start = start; | |
99cb252f JG |
897 | /* |
898 | * Note that the representation of the intervals in the interval tree | |
899 | * considers the ending point as contained in the interval. | |
900 | */ | |
901 | if (length == 0 || | |
5292e24a JG |
902 | check_add_overflow(start, length - 1, |
903 | &interval_sub->interval_tree.last)) | |
99cb252f JG |
904 | return -EOVERFLOW; |
905 | ||
906 | /* Must call with a mmget() held */ | |
907 | if (WARN_ON(atomic_read(&mm->mm_count) <= 0)) | |
908 | return -EINVAL; | |
909 | ||
910 | /* pairs with mmdrop in mmu_interval_notifier_remove() */ | |
911 | mmgrab(mm); | |
912 | ||
913 | /* | |
914 | * If some invalidate_range_start/end region is going on in parallel | |
915 | * we don't know what VA ranges are affected, so we must assume this | |
916 | * new range is included. | |
917 | * | |
918 | * If the itree is invalidating then we are not allowed to change | |
919 | * it. Retrying until invalidation is done is tricky due to the | |
920 | * possibility for live lock, instead defer the add to | |
921 | * mn_itree_inv_end() so this algorithm is deterministic. | |
922 | * | |
5292e24a | 923 | * In all cases the value for the interval_sub->invalidate_seq should be |
99cb252f JG |
924 | * odd, see mmu_interval_read_begin() |
925 | */ | |
984cfe4e JG |
926 | spin_lock(&subscriptions->lock); |
927 | if (subscriptions->active_invalidate_ranges) { | |
928 | if (mn_itree_is_invalidating(subscriptions)) | |
5292e24a | 929 | hlist_add_head(&interval_sub->deferred_item, |
984cfe4e | 930 | &subscriptions->deferred_list); |
99cb252f | 931 | else { |
984cfe4e | 932 | subscriptions->invalidate_seq |= 1; |
5292e24a | 933 | interval_tree_insert(&interval_sub->interval_tree, |
984cfe4e | 934 | &subscriptions->itree); |
99cb252f | 935 | } |
5292e24a | 936 | interval_sub->invalidate_seq = subscriptions->invalidate_seq; |
99cb252f | 937 | } else { |
984cfe4e | 938 | WARN_ON(mn_itree_is_invalidating(subscriptions)); |
99cb252f | 939 | /* |
5292e24a JG |
940 | * The starting seq for a subscription not under invalidation |
941 | * should be odd, not equal to the current invalidate_seq and | |
99cb252f JG |
942 | * invalidate_seq should not 'wrap' to the new seq any time |
943 | * soon. | |
944 | */ | |
5292e24a JG |
945 | interval_sub->invalidate_seq = |
946 | subscriptions->invalidate_seq - 1; | |
947 | interval_tree_insert(&interval_sub->interval_tree, | |
984cfe4e | 948 | &subscriptions->itree); |
99cb252f | 949 | } |
984cfe4e | 950 | spin_unlock(&subscriptions->lock); |
99cb252f JG |
951 | return 0; |
952 | } | |
953 | ||
954 | /** | |
955 | * mmu_interval_notifier_insert - Insert an interval notifier | |
5292e24a | 956 | * @interval_sub: Interval subscription to register |
99cb252f JG |
957 | * @start: Starting virtual address to monitor |
958 | * @length: Length of the range to monitor | |
959 | * @mm : mm_struct to attach to | |
960 | * | |
961 | * This function subscribes the interval notifier for notifications from the | |
962 | * mm. Upon return the ops related to mmu_interval_notifier will be called | |
963 | * whenever an event that intersects with the given range occurs. | |
964 | * | |
965 | * Upon return the range_notifier may not be present in the interval tree yet. | |
966 | * The caller must use the normal interval notifier read flow via | |
967 | * mmu_interval_read_begin() to establish SPTEs for this range. | |
968 | */ | |
5292e24a | 969 | int mmu_interval_notifier_insert(struct mmu_interval_notifier *interval_sub, |
99cb252f JG |
970 | struct mm_struct *mm, unsigned long start, |
971 | unsigned long length, | |
972 | const struct mmu_interval_notifier_ops *ops) | |
973 | { | |
984cfe4e | 974 | struct mmu_notifier_subscriptions *subscriptions; |
99cb252f JG |
975 | int ret; |
976 | ||
977 | might_lock(&mm->mmap_sem); | |
978 | ||
984cfe4e JG |
979 | subscriptions = smp_load_acquire(&mm->notifier_subscriptions); |
980 | if (!subscriptions || !subscriptions->has_itree) { | |
99cb252f JG |
981 | ret = mmu_notifier_register(NULL, mm); |
982 | if (ret) | |
983 | return ret; | |
984cfe4e | 984 | subscriptions = mm->notifier_subscriptions; |
99cb252f | 985 | } |
5292e24a JG |
986 | return __mmu_interval_notifier_insert(interval_sub, mm, subscriptions, |
987 | start, length, ops); | |
99cb252f JG |
988 | } |
989 | EXPORT_SYMBOL_GPL(mmu_interval_notifier_insert); | |
990 | ||
991 | int mmu_interval_notifier_insert_locked( | |
5292e24a | 992 | struct mmu_interval_notifier *interval_sub, struct mm_struct *mm, |
99cb252f JG |
993 | unsigned long start, unsigned long length, |
994 | const struct mmu_interval_notifier_ops *ops) | |
995 | { | |
984cfe4e JG |
996 | struct mmu_notifier_subscriptions *subscriptions = |
997 | mm->notifier_subscriptions; | |
99cb252f JG |
998 | int ret; |
999 | ||
1000 | lockdep_assert_held_write(&mm->mmap_sem); | |
1001 | ||
984cfe4e | 1002 | if (!subscriptions || !subscriptions->has_itree) { |
99cb252f JG |
1003 | ret = __mmu_notifier_register(NULL, mm); |
1004 | if (ret) | |
1005 | return ret; | |
984cfe4e | 1006 | subscriptions = mm->notifier_subscriptions; |
99cb252f | 1007 | } |
5292e24a JG |
1008 | return __mmu_interval_notifier_insert(interval_sub, mm, subscriptions, |
1009 | start, length, ops); | |
99cb252f JG |
1010 | } |
1011 | EXPORT_SYMBOL_GPL(mmu_interval_notifier_insert_locked); | |
1012 | ||
1013 | /** | |
1014 | * mmu_interval_notifier_remove - Remove a interval notifier | |
5292e24a | 1015 | * @interval_sub: Interval subscription to unregister |
99cb252f JG |
1016 | * |
1017 | * This function must be paired with mmu_interval_notifier_insert(). It cannot | |
1018 | * be called from any ops callback. | |
1019 | * | |
1020 | * Once this returns ops callbacks are no longer running on other CPUs and | |
1021 | * will not be called in future. | |
1022 | */ | |
5292e24a | 1023 | void mmu_interval_notifier_remove(struct mmu_interval_notifier *interval_sub) |
99cb252f | 1024 | { |
5292e24a | 1025 | struct mm_struct *mm = interval_sub->mm; |
984cfe4e JG |
1026 | struct mmu_notifier_subscriptions *subscriptions = |
1027 | mm->notifier_subscriptions; | |
99cb252f JG |
1028 | unsigned long seq = 0; |
1029 | ||
1030 | might_sleep(); | |
1031 | ||
984cfe4e JG |
1032 | spin_lock(&subscriptions->lock); |
1033 | if (mn_itree_is_invalidating(subscriptions)) { | |
99cb252f JG |
1034 | /* |
1035 | * remove is being called after insert put this on the | |
1036 | * deferred list, but before the deferred list was processed. | |
1037 | */ | |
5292e24a JG |
1038 | if (RB_EMPTY_NODE(&interval_sub->interval_tree.rb)) { |
1039 | hlist_del(&interval_sub->deferred_item); | |
99cb252f | 1040 | } else { |
5292e24a | 1041 | hlist_add_head(&interval_sub->deferred_item, |
984cfe4e JG |
1042 | &subscriptions->deferred_list); |
1043 | seq = subscriptions->invalidate_seq; | |
99cb252f JG |
1044 | } |
1045 | } else { | |
5292e24a JG |
1046 | WARN_ON(RB_EMPTY_NODE(&interval_sub->interval_tree.rb)); |
1047 | interval_tree_remove(&interval_sub->interval_tree, | |
984cfe4e | 1048 | &subscriptions->itree); |
99cb252f | 1049 | } |
984cfe4e | 1050 | spin_unlock(&subscriptions->lock); |
99cb252f JG |
1051 | |
1052 | /* | |
1053 | * The possible sleep on progress in the invalidation requires the | |
1054 | * caller not hold any locks held by invalidation callbacks. | |
1055 | */ | |
1056 | lock_map_acquire(&__mmu_notifier_invalidate_range_start_map); | |
1057 | lock_map_release(&__mmu_notifier_invalidate_range_start_map); | |
1058 | if (seq) | |
984cfe4e JG |
1059 | wait_event(subscriptions->wq, |
1060 | READ_ONCE(subscriptions->invalidate_seq) != seq); | |
99cb252f JG |
1061 | |
1062 | /* pairs with mmgrab in mmu_interval_notifier_insert() */ | |
1063 | mmdrop(mm); | |
1064 | } | |
1065 | EXPORT_SYMBOL_GPL(mmu_interval_notifier_remove); | |
1066 | ||
2c7933f5 JG |
1067 | /** |
1068 | * mmu_notifier_synchronize - Ensure all mmu_notifiers are freed | |
1069 | * | |
1070 | * This function ensures that all outstanding async SRU work from | |
1071 | * mmu_notifier_put() is completed. After it returns any mmu_notifier_ops | |
1072 | * associated with an unused mmu_notifier will no longer be called. | |
1073 | * | |
1074 | * Before using the caller must ensure that all of its mmu_notifiers have been | |
1075 | * fully released via mmu_notifier_put(). | |
1076 | * | |
1077 | * Modules using the mmu_notifier_put() API should call this in their __exit | |
1078 | * function to avoid module unloading races. | |
1079 | */ | |
1080 | void mmu_notifier_synchronize(void) | |
1081 | { | |
1082 | synchronize_srcu(&srcu); | |
1083 | } | |
1084 | EXPORT_SYMBOL_GPL(mmu_notifier_synchronize); | |
1085 | ||
c6d23413 JG |
1086 | bool |
1087 | mmu_notifier_range_update_to_read_only(const struct mmu_notifier_range *range) | |
1088 | { | |
1089 | if (!range->vma || range->event != MMU_NOTIFY_PROTECTION_VMA) | |
1090 | return false; | |
1091 | /* Return true if the vma still have the read flag set. */ | |
1092 | return range->vma->vm_flags & VM_READ; | |
1093 | } | |
1094 | EXPORT_SYMBOL_GPL(mmu_notifier_range_update_to_read_only); |