Thomas Gleixner | 20c8ccb | 2019-06-04 10:11:32 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 2 | /* |
| 3 | * linux/mm/mmu_notifier.c |
| 4 | * |
| 5 | * Copyright (C) 2008 Qumranet, Inc. |
| 6 | * Copyright (C) 2008 SGI |
Christoph Lameter | 93e205a | 2016-03-17 14:21:15 -0700 | [diff] [blame] | 7 | * Christoph Lameter <cl@linux.com> |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/rculist.h> |
| 11 | #include <linux/mmu_notifier.h> |
Paul Gortmaker | b95f1b31 | 2011-10-16 02:01:52 -0400 | [diff] [blame] | 12 | #include <linux/export.h> |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 13 | #include <linux/mm.h> |
| 14 | #include <linux/err.h> |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 15 | #include <linux/interval_tree.h> |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 16 | #include <linux/srcu.h> |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 17 | #include <linux/rcupdate.h> |
| 18 | #include <linux/sched.h> |
Ingo Molnar | 6e84f31 | 2017-02-08 18:51:29 +0100 | [diff] [blame] | 19 | #include <linux/sched/mm.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 21 | |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 22 | /* global SRCU for all MMs */ |
Paul E. McKenney | dde8da6 | 2017-03-25 10:42:07 -0700 | [diff] [blame] | 23 | DEFINE_STATIC_SRCU(srcu); |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 24 | |
Daniel Vetter | 23b6839 | 2019-08-26 22:14:21 +0200 | [diff] [blame] | 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 | |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 31 | /* |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 32 | * The mmu_notifier_subscriptions structure is allocated and installed in |
| 33 | * mm->notifier_subscriptions inside the mm_take_all_locks() protected |
Jason Gunthorpe | 56f434f | 2019-11-12 16:22:18 -0400 | [diff] [blame] | 34 | * critical section and it's released only when mm_count reaches zero |
| 35 | * in mmdrop(). |
| 36 | */ |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 37 | struct mmu_notifier_subscriptions { |
Jason Gunthorpe | 56f434f | 2019-11-12 16:22:18 -0400 | [diff] [blame] | 38 | /* all mmu notifiers registered in this mm are queued in this list */ |
| 39 | struct hlist_head list; |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 40 | bool has_itree; |
Jason Gunthorpe | 56f434f | 2019-11-12 16:22:18 -0400 | [diff] [blame] | 41 | /* to serialize the list modifications and hlist_unhashed */ |
| 42 | spinlock_t lock; |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 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; |
Jason Gunthorpe | 56f434f | 2019-11-12 16:22:18 -0400 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | /* |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 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 |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 68 | * - subscriptions->invalidate_seq & 1 == True (odd) |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 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 |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 74 | * - subscriptions->invalidate_seq & 1 == False (even) |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 75 | * - some range on the mm_struct is being invalidated |
| 76 | * - the itree is allowed to change |
| 77 | * |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 78 | * Operations on notifier_subscriptions->invalidate_seq (under spinlock): |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 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 |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 84 | * no mmu_interval_notifier monitoring the VA. |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 85 | */ |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 86 | static bool |
| 87 | mn_itree_is_invalidating(struct mmu_notifier_subscriptions *subscriptions) |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 88 | { |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 89 | lockdep_assert_held(&subscriptions->lock); |
| 90 | return subscriptions->invalidate_seq & 1; |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | static struct mmu_interval_notifier * |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 94 | mn_itree_inv_start_range(struct mmu_notifier_subscriptions *subscriptions, |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 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 | |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 101 | spin_lock(&subscriptions->lock); |
| 102 | subscriptions->active_invalidate_ranges++; |
| 103 | node = interval_tree_iter_first(&subscriptions->itree, range->start, |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 104 | range->end - 1); |
| 105 | if (node) { |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 106 | subscriptions->invalidate_seq |= 1; |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 107 | res = container_of(node, struct mmu_interval_notifier, |
| 108 | interval_tree); |
| 109 | } |
| 110 | |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 111 | *seq = subscriptions->invalidate_seq; |
| 112 | spin_unlock(&subscriptions->lock); |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 113 | return res; |
| 114 | } |
| 115 | |
| 116 | static struct mmu_interval_notifier * |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 117 | mn_itree_inv_next(struct mmu_interval_notifier *interval_sub, |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 118 | const struct mmu_notifier_range *range) |
| 119 | { |
| 120 | struct interval_tree_node *node; |
| 121 | |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 122 | node = interval_tree_iter_next(&interval_sub->interval_tree, |
| 123 | range->start, range->end - 1); |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 124 | if (!node) |
| 125 | return NULL; |
| 126 | return container_of(node, struct mmu_interval_notifier, interval_tree); |
| 127 | } |
| 128 | |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 129 | static void mn_itree_inv_end(struct mmu_notifier_subscriptions *subscriptions) |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 130 | { |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 131 | struct mmu_interval_notifier *interval_sub; |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 132 | struct hlist_node *next; |
| 133 | |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 134 | spin_lock(&subscriptions->lock); |
| 135 | if (--subscriptions->active_invalidate_ranges || |
| 136 | !mn_itree_is_invalidating(subscriptions)) { |
| 137 | spin_unlock(&subscriptions->lock); |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 138 | return; |
| 139 | } |
| 140 | |
| 141 | /* Make invalidate_seq even */ |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 142 | subscriptions->invalidate_seq++; |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 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 | */ |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 150 | hlist_for_each_entry_safe(interval_sub, next, |
| 151 | &subscriptions->deferred_list, |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 152 | deferred_item) { |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 153 | if (RB_EMPTY_NODE(&interval_sub->interval_tree.rb)) |
| 154 | interval_tree_insert(&interval_sub->interval_tree, |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 155 | &subscriptions->itree); |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 156 | else |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 157 | interval_tree_remove(&interval_sub->interval_tree, |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 158 | &subscriptions->itree); |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 159 | hlist_del(&interval_sub->deferred_item); |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 160 | } |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 161 | spin_unlock(&subscriptions->lock); |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 162 | |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 163 | wake_up_all(&subscriptions->wq); |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /** |
| 167 | * mmu_interval_read_begin - Begin a read side critical section against a VA |
| 168 | * range |
Krzysztof Kozlowski | d49653f | 2020-08-11 18:32:09 -0700 | [diff] [blame] | 169 | * @interval_sub: The interval subscription |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 170 | * |
| 171 | * mmu_iterval_read_begin()/mmu_iterval_read_retry() implement a |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 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. |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 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 | */ |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 185 | unsigned long |
| 186 | mmu_interval_read_begin(struct mmu_interval_notifier *interval_sub) |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 187 | { |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 188 | struct mmu_notifier_subscriptions *subscriptions = |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 189 | interval_sub->mm->notifier_subscriptions; |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 190 | unsigned long seq; |
| 191 | bool is_invalidating; |
| 192 | |
| 193 | /* |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 194 | * If the subscription has a different seq value under the user_lock |
| 195 | * than we started with then it has collided. |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 196 | * |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 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. |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 200 | * |
| 201 | * The locking looks broadly like this: |
| 202 | * mn_tree_invalidate_start(): mmu_interval_read_begin(): |
| 203 | * spin_lock |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 204 | * seq = READ_ONCE(interval_sub->invalidate_seq); |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 205 | * seq == subs->invalidate_seq |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 206 | * spin_unlock |
| 207 | * spin_lock |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 208 | * seq = ++subscriptions->invalidate_seq |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 209 | * spin_unlock |
| 210 | * op->invalidate_range(): |
| 211 | * user_lock |
| 212 | * mmu_interval_set_seq() |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 213 | * interval_sub->invalidate_seq = seq |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 214 | * user_unlock |
| 215 | * |
| 216 | * [Required: mmu_interval_read_retry() == true] |
| 217 | * |
| 218 | * mn_itree_inv_end(): |
| 219 | * spin_lock |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 220 | * seq = ++subscriptions->invalidate_seq |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 221 | * spin_unlock |
| 222 | * |
| 223 | * user_lock |
| 224 | * mmu_interval_read_retry(): |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 225 | * interval_sub->invalidate_seq != seq |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 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 | */ |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 232 | spin_lock(&subscriptions->lock); |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 233 | /* Pairs with the WRITE_ONCE in mmu_interval_set_seq() */ |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 234 | seq = READ_ONCE(interval_sub->invalidate_seq); |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 235 | is_invalidating = seq == subscriptions->invalidate_seq; |
| 236 | spin_unlock(&subscriptions->lock); |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 237 | |
| 238 | /* |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 239 | * interval_sub->invalidate_seq must always be set to an odd value via |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 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 |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 243 | * subscriptions->invalidate_seq is even in the idle state. |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 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) |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 248 | wait_event(subscriptions->wq, |
| 249 | READ_ONCE(subscriptions->invalidate_seq) != seq); |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 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 | |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 261 | static void mn_itree_release(struct mmu_notifier_subscriptions *subscriptions, |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 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 | }; |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 271 | struct mmu_interval_notifier *interval_sub; |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 272 | unsigned long cur_seq; |
| 273 | bool ret; |
| 274 | |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 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); |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 281 | WARN_ON(!ret); |
| 282 | } |
| 283 | |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 284 | mn_itree_inv_end(subscriptions); |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | /* |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 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 |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 294 | * the notifier_subscriptions->lock in addition to SRCU and it serializes |
| 295 | * against the other mmu notifiers with SRCU. struct mmu_notifier_subscriptions |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 296 | * can't go away from under us as exit_mmap holds an mm_count pin |
| 297 | * itself. |
| 298 | */ |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 299 | static void mn_hlist_release(struct mmu_notifier_subscriptions *subscriptions, |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 300 | struct mm_struct *mm) |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 301 | { |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 302 | struct mmu_notifier *subscription; |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 303 | int id; |
Xiao Guangrong | 3ad3d90 | 2012-07-31 16:45:52 -0700 | [diff] [blame] | 304 | |
| 305 | /* |
Xiao Guangrong | d34883d | 2013-05-24 15:55:11 -0700 | [diff] [blame] | 306 | * SRCU here will block mmu_notifier_unregister until |
| 307 | * ->release returns. |
Xiao Guangrong | 3ad3d90 | 2012-07-31 16:45:52 -0700 | [diff] [blame] | 308 | */ |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 309 | id = srcu_read_lock(&srcu); |
Qian Cai | 63886ba | 2020-03-21 18:22:34 -0700 | [diff] [blame] | 310 | hlist_for_each_entry_rcu(subscription, &subscriptions->list, hlist, |
| 311 | srcu_read_lock_held(&srcu)) |
Xiao Guangrong | d34883d | 2013-05-24 15:55:11 -0700 | [diff] [blame] | 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 | */ |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 318 | if (subscription->ops->release) |
| 319 | subscription->ops->release(subscription, mm); |
Xiao Guangrong | d34883d | 2013-05-24 15:55:11 -0700 | [diff] [blame] | 320 | |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 321 | spin_lock(&subscriptions->lock); |
| 322 | while (unlikely(!hlist_empty(&subscriptions->list))) { |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 323 | subscription = hlist_entry(subscriptions->list.first, |
| 324 | struct mmu_notifier, hlist); |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 325 | /* |
Xiao Guangrong | d34883d | 2013-05-24 15:55:11 -0700 | [diff] [blame] | 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. |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 330 | */ |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 331 | hlist_del_init_rcu(&subscription->hlist); |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 332 | } |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 333 | spin_unlock(&subscriptions->lock); |
Peter Zijlstra | b972216 | 2014-08-06 16:08:20 -0700 | [diff] [blame] | 334 | srcu_read_unlock(&srcu, id); |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 335 | |
| 336 | /* |
Xiao Guangrong | d34883d | 2013-05-24 15:55:11 -0700 | [diff] [blame] | 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 | * |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 342 | * The notifier_subscriptions can't go away from under us because |
| 343 | * one mm_count is held by exit_mmap. |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 344 | */ |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 345 | synchronize_srcu(&srcu); |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 348 | void __mmu_notifier_release(struct mm_struct *mm) |
| 349 | { |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 350 | struct mmu_notifier_subscriptions *subscriptions = |
| 351 | mm->notifier_subscriptions; |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 352 | |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 353 | if (subscriptions->has_itree) |
| 354 | mn_itree_release(subscriptions, mm); |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 355 | |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 356 | if (!hlist_empty(&subscriptions->list)) |
| 357 | mn_hlist_release(subscriptions, mm); |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 358 | } |
| 359 | |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 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 | */ |
| 365 | int __mmu_notifier_clear_flush_young(struct mm_struct *mm, |
Andres Lagar-Cavilla | 5712846 | 2014-09-22 14:54:42 -0700 | [diff] [blame] | 366 | unsigned long start, |
| 367 | unsigned long end) |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 368 | { |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 369 | struct mmu_notifier *subscription; |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 370 | int young = 0, id; |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 371 | |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 372 | id = srcu_read_lock(&srcu); |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 373 | hlist_for_each_entry_rcu(subscription, |
Qian Cai | 63886ba | 2020-03-21 18:22:34 -0700 | [diff] [blame] | 374 | &mm->notifier_subscriptions->list, hlist, |
| 375 | srcu_read_lock_held(&srcu)) { |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 376 | if (subscription->ops->clear_flush_young) |
| 377 | young |= subscription->ops->clear_flush_young( |
| 378 | subscription, mm, start, end); |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 379 | } |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 380 | srcu_read_unlock(&srcu, id); |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 381 | |
| 382 | return young; |
| 383 | } |
| 384 | |
Vladimir Davydov | 1d7715c | 2015-09-09 15:35:41 -0700 | [diff] [blame] | 385 | int __mmu_notifier_clear_young(struct mm_struct *mm, |
| 386 | unsigned long start, |
| 387 | unsigned long end) |
| 388 | { |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 389 | struct mmu_notifier *subscription; |
Vladimir Davydov | 1d7715c | 2015-09-09 15:35:41 -0700 | [diff] [blame] | 390 | int young = 0, id; |
| 391 | |
| 392 | id = srcu_read_lock(&srcu); |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 393 | hlist_for_each_entry_rcu(subscription, |
Qian Cai | 63886ba | 2020-03-21 18:22:34 -0700 | [diff] [blame] | 394 | &mm->notifier_subscriptions->list, hlist, |
| 395 | srcu_read_lock_held(&srcu)) { |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 396 | if (subscription->ops->clear_young) |
| 397 | young |= subscription->ops->clear_young(subscription, |
| 398 | mm, start, end); |
Vladimir Davydov | 1d7715c | 2015-09-09 15:35:41 -0700 | [diff] [blame] | 399 | } |
| 400 | srcu_read_unlock(&srcu, id); |
| 401 | |
| 402 | return young; |
| 403 | } |
| 404 | |
Andrea Arcangeli | 8ee5382 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 405 | int __mmu_notifier_test_young(struct mm_struct *mm, |
| 406 | unsigned long address) |
| 407 | { |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 408 | struct mmu_notifier *subscription; |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 409 | int young = 0, id; |
Andrea Arcangeli | 8ee5382 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 410 | |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 411 | id = srcu_read_lock(&srcu); |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 412 | hlist_for_each_entry_rcu(subscription, |
Qian Cai | 63886ba | 2020-03-21 18:22:34 -0700 | [diff] [blame] | 413 | &mm->notifier_subscriptions->list, hlist, |
| 414 | srcu_read_lock_held(&srcu)) { |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 415 | if (subscription->ops->test_young) { |
| 416 | young = subscription->ops->test_young(subscription, mm, |
| 417 | address); |
Andrea Arcangeli | 8ee5382 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 418 | if (young) |
| 419 | break; |
| 420 | } |
| 421 | } |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 422 | srcu_read_unlock(&srcu, id); |
Andrea Arcangeli | 8ee5382 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 423 | |
| 424 | return young; |
| 425 | } |
| 426 | |
Izik Eidus | 828502d | 2009-09-21 17:01:51 -0700 | [diff] [blame] | 427 | void __mmu_notifier_change_pte(struct mm_struct *mm, unsigned long address, |
| 428 | pte_t pte) |
| 429 | { |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 430 | struct mmu_notifier *subscription; |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 431 | int id; |
Izik Eidus | 828502d | 2009-09-21 17:01:51 -0700 | [diff] [blame] | 432 | |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 433 | id = srcu_read_lock(&srcu); |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 434 | hlist_for_each_entry_rcu(subscription, |
Qian Cai | 63886ba | 2020-03-21 18:22:34 -0700 | [diff] [blame] | 435 | &mm->notifier_subscriptions->list, hlist, |
| 436 | srcu_read_lock_held(&srcu)) { |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 437 | if (subscription->ops->change_pte) |
| 438 | subscription->ops->change_pte(subscription, mm, address, |
| 439 | pte); |
Izik Eidus | 828502d | 2009-09-21 17:01:51 -0700 | [diff] [blame] | 440 | } |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 441 | srcu_read_unlock(&srcu, id); |
Izik Eidus | 828502d | 2009-09-21 17:01:51 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 444 | static int mn_itree_invalidate(struct mmu_notifier_subscriptions *subscriptions, |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 445 | const struct mmu_notifier_range *range) |
| 446 | { |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 447 | struct mmu_interval_notifier *interval_sub; |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 448 | unsigned long cur_seq; |
| 449 | |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 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)) { |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 454 | bool ret; |
| 455 | |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 456 | ret = interval_sub->ops->invalidate(interval_sub, range, |
| 457 | cur_seq); |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 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 | |
| 466 | out_would_block: |
| 467 | /* |
| 468 | * On -EAGAIN the non-blocking caller is not allowed to call |
| 469 | * invalidate_range_end() |
| 470 | */ |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 471 | mn_itree_inv_end(subscriptions); |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 472 | return -EAGAIN; |
| 473 | } |
| 474 | |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 475 | static int mn_hlist_invalidate_range_start( |
| 476 | struct mmu_notifier_subscriptions *subscriptions, |
| 477 | struct mmu_notifier_range *range) |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 478 | { |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 479 | struct mmu_notifier *subscription; |
Michal Hocko | 93065ac | 2018-08-21 21:52:33 -0700 | [diff] [blame] | 480 | int ret = 0; |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 481 | int id; |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 482 | |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 483 | id = srcu_read_lock(&srcu); |
Qian Cai | 63886ba | 2020-03-21 18:22:34 -0700 | [diff] [blame] | 484 | hlist_for_each_entry_rcu(subscription, &subscriptions->list, hlist, |
| 485 | srcu_read_lock_held(&srcu)) { |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 486 | const struct mmu_notifier_ops *ops = subscription->ops; |
| 487 | |
| 488 | if (ops->invalidate_range_start) { |
Daniel Vetter | ba170f7 | 2019-08-26 22:14:24 +0200 | [diff] [blame] | 489 | int _ret; |
| 490 | |
| 491 | if (!mmu_notifier_range_blockable(range)) |
| 492 | non_block_start(); |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 493 | _ret = ops->invalidate_range_start(subscription, range); |
Daniel Vetter | ba170f7 | 2019-08-26 22:14:24 +0200 | [diff] [blame] | 494 | if (!mmu_notifier_range_blockable(range)) |
| 495 | non_block_end(); |
Michal Hocko | 93065ac | 2018-08-21 21:52:33 -0700 | [diff] [blame] | 496 | if (_ret) { |
| 497 | pr_info("%pS callback failed with %d in %sblockable context.\n", |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 498 | ops->invalidate_range_start, _ret, |
| 499 | !mmu_notifier_range_blockable(range) ? |
| 500 | "non-" : |
| 501 | ""); |
Daniel Vetter | 8402ce6 | 2019-08-14 22:20:23 +0200 | [diff] [blame] | 502 | WARN_ON(mmu_notifier_range_blockable(range) || |
Jason Gunthorpe | df2ec76 | 2019-11-05 21:16:37 -0800 | [diff] [blame] | 503 | _ret != -EAGAIN); |
Sean Christopherson | de2e6b4 | 2021-03-24 21:37:23 -0700 | [diff] [blame] | 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); |
Michal Hocko | 93065ac | 2018-08-21 21:52:33 -0700 | [diff] [blame] | 511 | ret = _ret; |
| 512 | } |
| 513 | } |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 514 | } |
Sean Christopherson | de2e6b4 | 2021-03-24 21:37:23 -0700 | [diff] [blame] | 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 | } |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 531 | srcu_read_unlock(&srcu, id); |
Michal Hocko | 93065ac | 2018-08-21 21:52:33 -0700 | [diff] [blame] | 532 | |
| 533 | return ret; |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 536 | int __mmu_notifier_invalidate_range_start(struct mmu_notifier_range *range) |
| 537 | { |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 538 | struct mmu_notifier_subscriptions *subscriptions = |
| 539 | range->mm->notifier_subscriptions; |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 540 | int ret; |
| 541 | |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 542 | if (subscriptions->has_itree) { |
| 543 | ret = mn_itree_invalidate(subscriptions, range); |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 544 | if (ret) |
| 545 | return ret; |
| 546 | } |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 547 | if (!hlist_empty(&subscriptions->list)) |
| 548 | return mn_hlist_invalidate_range_start(subscriptions, range); |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 549 | return 0; |
| 550 | } |
| 551 | |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 552 | static void |
| 553 | mn_hlist_invalidate_end(struct mmu_notifier_subscriptions *subscriptions, |
| 554 | struct mmu_notifier_range *range, bool only_end) |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 555 | { |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 556 | struct mmu_notifier *subscription; |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 557 | int id; |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 558 | |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 559 | id = srcu_read_lock(&srcu); |
Qian Cai | 63886ba | 2020-03-21 18:22:34 -0700 | [diff] [blame] | 560 | hlist_for_each_entry_rcu(subscription, &subscriptions->list, hlist, |
| 561 | srcu_read_lock_held(&srcu)) { |
Joerg Roedel | 0f0a327 | 2014-11-13 13:46:09 +1100 | [diff] [blame] | 562 | /* |
| 563 | * Call invalidate_range here too to avoid the need for the |
| 564 | * subsystem of having to register an invalidate_range_end |
| 565 | * call-back when there is invalidate_range already. Usually a |
| 566 | * subsystem registers either invalidate_range_start()/end() or |
| 567 | * invalidate_range(), so this will be no additional overhead |
| 568 | * (besides the pointer check). |
Jérôme Glisse | 4645b9f | 2017-11-15 17:34:11 -0800 | [diff] [blame] | 569 | * |
| 570 | * We skip call to invalidate_range() if we know it is safe ie |
| 571 | * call site use mmu_notifier_invalidate_range_only_end() which |
| 572 | * is safe to do when we know that a call to invalidate_range() |
| 573 | * already happen under page table lock. |
Joerg Roedel | 0f0a327 | 2014-11-13 13:46:09 +1100 | [diff] [blame] | 574 | */ |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 575 | if (!only_end && subscription->ops->invalidate_range) |
| 576 | subscription->ops->invalidate_range(subscription, |
| 577 | range->mm, |
| 578 | range->start, |
| 579 | range->end); |
| 580 | if (subscription->ops->invalidate_range_end) { |
Daniel Vetter | ba170f7 | 2019-08-26 22:14:24 +0200 | [diff] [blame] | 581 | if (!mmu_notifier_range_blockable(range)) |
| 582 | non_block_start(); |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 583 | subscription->ops->invalidate_range_end(subscription, |
| 584 | range); |
Daniel Vetter | ba170f7 | 2019-08-26 22:14:24 +0200 | [diff] [blame] | 585 | if (!mmu_notifier_range_blockable(range)) |
| 586 | non_block_end(); |
| 587 | } |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 588 | } |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 589 | srcu_read_unlock(&srcu, id); |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | void __mmu_notifier_invalidate_range_end(struct mmu_notifier_range *range, |
| 593 | bool only_end) |
| 594 | { |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 595 | struct mmu_notifier_subscriptions *subscriptions = |
| 596 | range->mm->notifier_subscriptions; |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 597 | |
| 598 | lock_map_acquire(&__mmu_notifier_invalidate_range_start_map); |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 599 | if (subscriptions->has_itree) |
| 600 | mn_itree_inv_end(subscriptions); |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 601 | |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 602 | if (!hlist_empty(&subscriptions->list)) |
| 603 | mn_hlist_invalidate_end(subscriptions, range, only_end); |
Daniel Vetter | 23b6839 | 2019-08-26 22:14:21 +0200 | [diff] [blame] | 604 | lock_map_release(&__mmu_notifier_invalidate_range_start_map); |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 605 | } |
| 606 | |
Joerg Roedel | 0f0a327 | 2014-11-13 13:46:09 +1100 | [diff] [blame] | 607 | void __mmu_notifier_invalidate_range(struct mm_struct *mm, |
| 608 | unsigned long start, unsigned long end) |
| 609 | { |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 610 | struct mmu_notifier *subscription; |
Joerg Roedel | 0f0a327 | 2014-11-13 13:46:09 +1100 | [diff] [blame] | 611 | int id; |
| 612 | |
| 613 | id = srcu_read_lock(&srcu); |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 614 | hlist_for_each_entry_rcu(subscription, |
Qian Cai | 63886ba | 2020-03-21 18:22:34 -0700 | [diff] [blame] | 615 | &mm->notifier_subscriptions->list, hlist, |
| 616 | srcu_read_lock_held(&srcu)) { |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 617 | if (subscription->ops->invalidate_range) |
| 618 | subscription->ops->invalidate_range(subscription, mm, |
| 619 | start, end); |
Joerg Roedel | 0f0a327 | 2014-11-13 13:46:09 +1100 | [diff] [blame] | 620 | } |
| 621 | srcu_read_unlock(&srcu, id); |
| 622 | } |
Joerg Roedel | 0f0a327 | 2014-11-13 13:46:09 +1100 | [diff] [blame] | 623 | |
Jason Gunthorpe | 56c57103 | 2019-08-06 20:15:38 -0300 | [diff] [blame] | 624 | /* |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 625 | * Same as mmu_notifier_register but here the caller must hold the mmap_lock in |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 626 | * write mode. A NULL mn signals the notifier is being registered for itree |
| 627 | * mode. |
Jason Gunthorpe | 56c57103 | 2019-08-06 20:15:38 -0300 | [diff] [blame] | 628 | */ |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 629 | int __mmu_notifier_register(struct mmu_notifier *subscription, |
| 630 | struct mm_struct *mm) |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 631 | { |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 632 | struct mmu_notifier_subscriptions *subscriptions = NULL; |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 633 | int ret; |
| 634 | |
Michel Lespinasse | 42fc541 | 2020-06-08 21:33:44 -0700 | [diff] [blame] | 635 | mmap_assert_write_locked(mm); |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 636 | BUG_ON(atomic_read(&mm->mm_users) <= 0); |
| 637 | |
Daniel Vetter | 66204f1 | 2019-08-26 22:14:22 +0200 | [diff] [blame] | 638 | if (IS_ENABLED(CONFIG_LOCKDEP)) { |
| 639 | fs_reclaim_acquire(GFP_KERNEL); |
| 640 | lock_map_acquire(&__mmu_notifier_invalidate_range_start_map); |
| 641 | lock_map_release(&__mmu_notifier_invalidate_range_start_map); |
| 642 | fs_reclaim_release(GFP_KERNEL); |
| 643 | } |
| 644 | |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 645 | if (!mm->notifier_subscriptions) { |
Jason Gunthorpe | 70df291 | 2019-08-06 20:15:39 -0300 | [diff] [blame] | 646 | /* |
| 647 | * kmalloc cannot be called under mm_take_all_locks(), but we |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 648 | * know that mm->notifier_subscriptions can't change while we |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 649 | * hold the write side of the mmap_lock. |
Jason Gunthorpe | 70df291 | 2019-08-06 20:15:39 -0300 | [diff] [blame] | 650 | */ |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 651 | subscriptions = kzalloc( |
| 652 | sizeof(struct mmu_notifier_subscriptions), GFP_KERNEL); |
| 653 | if (!subscriptions) |
Jason Gunthorpe | 70df291 | 2019-08-06 20:15:39 -0300 | [diff] [blame] | 654 | return -ENOMEM; |
| 655 | |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 656 | INIT_HLIST_HEAD(&subscriptions->list); |
| 657 | spin_lock_init(&subscriptions->lock); |
| 658 | subscriptions->invalidate_seq = 2; |
| 659 | subscriptions->itree = RB_ROOT_CACHED; |
| 660 | init_waitqueue_head(&subscriptions->wq); |
| 661 | INIT_HLIST_HEAD(&subscriptions->deferred_list); |
Jason Gunthorpe | 70df291 | 2019-08-06 20:15:39 -0300 | [diff] [blame] | 662 | } |
Gavin Shan | 35cfa2b | 2012-10-25 13:38:01 -0700 | [diff] [blame] | 663 | |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 664 | ret = mm_take_all_locks(mm); |
| 665 | if (unlikely(ret)) |
Gavin Shan | 35cfa2b | 2012-10-25 13:38:01 -0700 | [diff] [blame] | 666 | goto out_clean; |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 667 | |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 668 | /* |
| 669 | * Serialize the update against mmu_notifier_unregister. A |
| 670 | * side note: mmu_notifier_release can't run concurrently with |
| 671 | * us because we hold the mm_users pin (either implicitly as |
| 672 | * current->mm or explicitly with get_task_mm() or similar). |
| 673 | * We can't race against any other mmu notifier method either |
| 674 | * thanks to mm_take_all_locks(). |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 675 | * |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 676 | * release semantics on the initialization of the |
| 677 | * mmu_notifier_subscriptions's contents are provided for unlocked |
| 678 | * readers. acquire can only be used while holding the mmgrab or |
| 679 | * mmget, and is safe because once created the |
| 680 | * mmu_notifier_subscriptions is not freed until the mm is destroyed. |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 681 | * As above, users holding the mmap_lock or one of the |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 682 | * mm_take_all_locks() do not need to use acquire semantics. |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 683 | */ |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 684 | if (subscriptions) |
| 685 | smp_store_release(&mm->notifier_subscriptions, subscriptions); |
Jason Gunthorpe | 70df291 | 2019-08-06 20:15:39 -0300 | [diff] [blame] | 686 | |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 687 | if (subscription) { |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 688 | /* Pairs with the mmdrop in mmu_notifier_unregister_* */ |
| 689 | mmgrab(mm); |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 690 | subscription->mm = mm; |
| 691 | subscription->users = 1; |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 692 | |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 693 | spin_lock(&mm->notifier_subscriptions->lock); |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 694 | hlist_add_head_rcu(&subscription->hlist, |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 695 | &mm->notifier_subscriptions->list); |
| 696 | spin_unlock(&mm->notifier_subscriptions->lock); |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 697 | } else |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 698 | mm->notifier_subscriptions->has_itree = true; |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 699 | |
| 700 | mm_drop_all_locks(mm); |
Jason Gunthorpe | 70df291 | 2019-08-06 20:15:39 -0300 | [diff] [blame] | 701 | BUG_ON(atomic_read(&mm->mm_users) <= 0); |
| 702 | return 0; |
| 703 | |
Gavin Shan | 35cfa2b | 2012-10-25 13:38:01 -0700 | [diff] [blame] | 704 | out_clean: |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 705 | kfree(subscriptions); |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 706 | return ret; |
| 707 | } |
Jason Gunthorpe | 56c57103 | 2019-08-06 20:15:38 -0300 | [diff] [blame] | 708 | EXPORT_SYMBOL_GPL(__mmu_notifier_register); |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 709 | |
Jason Gunthorpe | 2c7933f | 2019-08-06 20:15:40 -0300 | [diff] [blame] | 710 | /** |
| 711 | * mmu_notifier_register - Register a notifier on a mm |
Krzysztof Kozlowski | d49653f | 2020-08-11 18:32:09 -0700 | [diff] [blame] | 712 | * @subscription: The notifier to attach |
Jason Gunthorpe | 2c7933f | 2019-08-06 20:15:40 -0300 | [diff] [blame] | 713 | * @mm: The mm to attach the notifier to |
| 714 | * |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 715 | * Must not hold mmap_lock nor any other VM related lock when calling |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 716 | * this registration function. Must also ensure mm_users can't go down |
| 717 | * to zero while this runs to avoid races with mmu_notifier_release, |
| 718 | * so mm has to be current->mm or the mm should be pinned safely such |
| 719 | * as with get_task_mm(). If the mm is not current->mm, the mm_users |
| 720 | * pin should be released by calling mmput after mmu_notifier_register |
Jason Gunthorpe | 2c7933f | 2019-08-06 20:15:40 -0300 | [diff] [blame] | 721 | * returns. |
| 722 | * |
| 723 | * mmu_notifier_unregister() or mmu_notifier_put() must be always called to |
| 724 | * unregister the notifier. |
| 725 | * |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 726 | * While the caller has a mmu_notifier get the subscription->mm pointer will remain |
Jason Gunthorpe | 2c7933f | 2019-08-06 20:15:40 -0300 | [diff] [blame] | 727 | * valid, and can be converted to an active mm pointer via mmget_not_zero(). |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 728 | */ |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 729 | int mmu_notifier_register(struct mmu_notifier *subscription, |
| 730 | struct mm_struct *mm) |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 731 | { |
Jason Gunthorpe | 56c57103 | 2019-08-06 20:15:38 -0300 | [diff] [blame] | 732 | int ret; |
| 733 | |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 734 | mmap_write_lock(mm); |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 735 | ret = __mmu_notifier_register(subscription, mm); |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 736 | mmap_write_unlock(mm); |
Jason Gunthorpe | 56c57103 | 2019-08-06 20:15:38 -0300 | [diff] [blame] | 737 | return ret; |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 738 | } |
| 739 | EXPORT_SYMBOL_GPL(mmu_notifier_register); |
| 740 | |
Jason Gunthorpe | 2c7933f | 2019-08-06 20:15:40 -0300 | [diff] [blame] | 741 | static struct mmu_notifier * |
| 742 | find_get_mmu_notifier(struct mm_struct *mm, const struct mmu_notifier_ops *ops) |
| 743 | { |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 744 | struct mmu_notifier *subscription; |
Jason Gunthorpe | 2c7933f | 2019-08-06 20:15:40 -0300 | [diff] [blame] | 745 | |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 746 | spin_lock(&mm->notifier_subscriptions->lock); |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 747 | hlist_for_each_entry_rcu(subscription, |
Qian Cai | 63886ba | 2020-03-21 18:22:34 -0700 | [diff] [blame] | 748 | &mm->notifier_subscriptions->list, hlist, |
| 749 | lockdep_is_held(&mm->notifier_subscriptions->lock)) { |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 750 | if (subscription->ops != ops) |
Jason Gunthorpe | 2c7933f | 2019-08-06 20:15:40 -0300 | [diff] [blame] | 751 | continue; |
| 752 | |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 753 | if (likely(subscription->users != UINT_MAX)) |
| 754 | subscription->users++; |
Jason Gunthorpe | 2c7933f | 2019-08-06 20:15:40 -0300 | [diff] [blame] | 755 | else |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 756 | subscription = ERR_PTR(-EOVERFLOW); |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 757 | spin_unlock(&mm->notifier_subscriptions->lock); |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 758 | return subscription; |
Jason Gunthorpe | 2c7933f | 2019-08-06 20:15:40 -0300 | [diff] [blame] | 759 | } |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 760 | spin_unlock(&mm->notifier_subscriptions->lock); |
Jason Gunthorpe | 2c7933f | 2019-08-06 20:15:40 -0300 | [diff] [blame] | 761 | return NULL; |
| 762 | } |
| 763 | |
| 764 | /** |
| 765 | * mmu_notifier_get_locked - Return the single struct mmu_notifier for |
| 766 | * the mm & ops |
| 767 | * @ops: The operations struct being subscribe with |
| 768 | * @mm : The mm to attach notifiers too |
| 769 | * |
| 770 | * This function either allocates a new mmu_notifier via |
| 771 | * ops->alloc_notifier(), or returns an already existing notifier on the |
| 772 | * list. The value of the ops pointer is used to determine when two notifiers |
| 773 | * are the same. |
| 774 | * |
| 775 | * Each call to mmu_notifier_get() must be paired with a call to |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 776 | * mmu_notifier_put(). The caller must hold the write side of mm->mmap_lock. |
Jason Gunthorpe | 2c7933f | 2019-08-06 20:15:40 -0300 | [diff] [blame] | 777 | * |
| 778 | * While the caller has a mmu_notifier get the mm pointer will remain valid, |
| 779 | * and can be converted to an active mm pointer via mmget_not_zero(). |
| 780 | */ |
| 781 | struct mmu_notifier *mmu_notifier_get_locked(const struct mmu_notifier_ops *ops, |
| 782 | struct mm_struct *mm) |
| 783 | { |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 784 | struct mmu_notifier *subscription; |
Jason Gunthorpe | 2c7933f | 2019-08-06 20:15:40 -0300 | [diff] [blame] | 785 | int ret; |
| 786 | |
Michel Lespinasse | 42fc541 | 2020-06-08 21:33:44 -0700 | [diff] [blame] | 787 | mmap_assert_write_locked(mm); |
Jason Gunthorpe | 2c7933f | 2019-08-06 20:15:40 -0300 | [diff] [blame] | 788 | |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 789 | if (mm->notifier_subscriptions) { |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 790 | subscription = find_get_mmu_notifier(mm, ops); |
| 791 | if (subscription) |
| 792 | return subscription; |
Jason Gunthorpe | 2c7933f | 2019-08-06 20:15:40 -0300 | [diff] [blame] | 793 | } |
| 794 | |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 795 | subscription = ops->alloc_notifier(mm); |
| 796 | if (IS_ERR(subscription)) |
| 797 | return subscription; |
| 798 | subscription->ops = ops; |
| 799 | ret = __mmu_notifier_register(subscription, mm); |
Jason Gunthorpe | 2c7933f | 2019-08-06 20:15:40 -0300 | [diff] [blame] | 800 | if (ret) |
| 801 | goto out_free; |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 802 | return subscription; |
Jason Gunthorpe | 2c7933f | 2019-08-06 20:15:40 -0300 | [diff] [blame] | 803 | out_free: |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 804 | subscription->ops->free_notifier(subscription); |
Jason Gunthorpe | 2c7933f | 2019-08-06 20:15:40 -0300 | [diff] [blame] | 805 | return ERR_PTR(ret); |
| 806 | } |
| 807 | EXPORT_SYMBOL_GPL(mmu_notifier_get_locked); |
| 808 | |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 809 | /* this is called after the last mmu_notifier_unregister() returned */ |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 810 | void __mmu_notifier_subscriptions_destroy(struct mm_struct *mm) |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 811 | { |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 812 | BUG_ON(!hlist_empty(&mm->notifier_subscriptions->list)); |
| 813 | kfree(mm->notifier_subscriptions); |
| 814 | mm->notifier_subscriptions = LIST_POISON1; /* debug */ |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | /* |
| 818 | * This releases the mm_count pin automatically and frees the mm |
| 819 | * structure if it was the last user of it. It serializes against |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 820 | * running mmu notifiers with SRCU and against mmu_notifier_unregister |
| 821 | * with the unregister lock + SRCU. All sptes must be dropped before |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 822 | * calling mmu_notifier_unregister. ->release or any other notifier |
| 823 | * method may be invoked concurrently with mmu_notifier_unregister, |
| 824 | * and only after mmu_notifier_unregister returned we're guaranteed |
| 825 | * that ->release or any other method can't run anymore. |
| 826 | */ |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 827 | void mmu_notifier_unregister(struct mmu_notifier *subscription, |
| 828 | struct mm_struct *mm) |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 829 | { |
| 830 | BUG_ON(atomic_read(&mm->mm_count) <= 0); |
| 831 | |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 832 | if (!hlist_unhashed(&subscription->hlist)) { |
Xiao Guangrong | d34883d | 2013-05-24 15:55:11 -0700 | [diff] [blame] | 833 | /* |
| 834 | * SRCU here will force exit_mmap to wait for ->release to |
| 835 | * finish before freeing the pages. |
| 836 | */ |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 837 | int id; |
Xiao Guangrong | 3ad3d90 | 2012-07-31 16:45:52 -0700 | [diff] [blame] | 838 | |
Robin Holt | 751efd8 | 2013-02-22 16:35:34 -0800 | [diff] [blame] | 839 | id = srcu_read_lock(&srcu); |
Xiao Guangrong | d34883d | 2013-05-24 15:55:11 -0700 | [diff] [blame] | 840 | /* |
| 841 | * exit_mmap will block in mmu_notifier_release to guarantee |
| 842 | * that ->release is called before freeing the pages. |
| 843 | */ |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 844 | if (subscription->ops->release) |
| 845 | subscription->ops->release(subscription, mm); |
Robin Holt | 751efd8 | 2013-02-22 16:35:34 -0800 | [diff] [blame] | 846 | srcu_read_unlock(&srcu, id); |
Xiao Guangrong | d34883d | 2013-05-24 15:55:11 -0700 | [diff] [blame] | 847 | |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 848 | spin_lock(&mm->notifier_subscriptions->lock); |
Xiao Guangrong | d34883d | 2013-05-24 15:55:11 -0700 | [diff] [blame] | 849 | /* |
| 850 | * Can not use list_del_rcu() since __mmu_notifier_release |
| 851 | * can delete it before we hold the lock. |
| 852 | */ |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 853 | hlist_del_init_rcu(&subscription->hlist); |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 854 | spin_unlock(&mm->notifier_subscriptions->lock); |
Xiao Guangrong | d34883d | 2013-05-24 15:55:11 -0700 | [diff] [blame] | 855 | } |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 856 | |
| 857 | /* |
Xiao Guangrong | d34883d | 2013-05-24 15:55:11 -0700 | [diff] [blame] | 858 | * Wait for any running method to finish, of course including |
Geert Uytterhoeven | 83a35e3 | 2013-06-28 11:27:31 +0200 | [diff] [blame] | 859 | * ->release if it was run by mmu_notifier_release instead of us. |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 860 | */ |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 861 | synchronize_srcu(&srcu); |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 862 | |
| 863 | BUG_ON(atomic_read(&mm->mm_count) <= 0); |
| 864 | |
| 865 | mmdrop(mm); |
| 866 | } |
| 867 | EXPORT_SYMBOL_GPL(mmu_notifier_unregister); |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 868 | |
Jason Gunthorpe | 2c7933f | 2019-08-06 20:15:40 -0300 | [diff] [blame] | 869 | static void mmu_notifier_free_rcu(struct rcu_head *rcu) |
| 870 | { |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 871 | struct mmu_notifier *subscription = |
| 872 | container_of(rcu, struct mmu_notifier, rcu); |
| 873 | struct mm_struct *mm = subscription->mm; |
Jason Gunthorpe | 2c7933f | 2019-08-06 20:15:40 -0300 | [diff] [blame] | 874 | |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 875 | subscription->ops->free_notifier(subscription); |
Jason Gunthorpe | 2c7933f | 2019-08-06 20:15:40 -0300 | [diff] [blame] | 876 | /* Pairs with the get in __mmu_notifier_register() */ |
| 877 | mmdrop(mm); |
| 878 | } |
| 879 | |
| 880 | /** |
| 881 | * mmu_notifier_put - Release the reference on the notifier |
Krzysztof Kozlowski | d49653f | 2020-08-11 18:32:09 -0700 | [diff] [blame] | 882 | * @subscription: The notifier to act on |
Jason Gunthorpe | 2c7933f | 2019-08-06 20:15:40 -0300 | [diff] [blame] | 883 | * |
| 884 | * This function must be paired with each mmu_notifier_get(), it releases the |
| 885 | * reference obtained by the get. If this is the last reference then process |
| 886 | * to free the notifier will be run asynchronously. |
| 887 | * |
| 888 | * Unlike mmu_notifier_unregister() the get/put flow only calls ops->release |
| 889 | * when the mm_struct is destroyed. Instead free_notifier is always called to |
| 890 | * release any resources held by the user. |
| 891 | * |
| 892 | * As ops->release is not guaranteed to be called, the user must ensure that |
| 893 | * all sptes are dropped, and no new sptes can be established before |
| 894 | * mmu_notifier_put() is called. |
| 895 | * |
| 896 | * This function can be called from the ops->release callback, however the |
| 897 | * caller must still ensure it is called pairwise with mmu_notifier_get(). |
| 898 | * |
| 899 | * Modules calling this function must call mmu_notifier_synchronize() in |
| 900 | * their __exit functions to ensure the async work is completed. |
| 901 | */ |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 902 | void mmu_notifier_put(struct mmu_notifier *subscription) |
Jason Gunthorpe | 2c7933f | 2019-08-06 20:15:40 -0300 | [diff] [blame] | 903 | { |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 904 | struct mm_struct *mm = subscription->mm; |
Jason Gunthorpe | 2c7933f | 2019-08-06 20:15:40 -0300 | [diff] [blame] | 905 | |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 906 | spin_lock(&mm->notifier_subscriptions->lock); |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 907 | if (WARN_ON(!subscription->users) || --subscription->users) |
Jason Gunthorpe | 2c7933f | 2019-08-06 20:15:40 -0300 | [diff] [blame] | 908 | goto out_unlock; |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 909 | hlist_del_init_rcu(&subscription->hlist); |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 910 | spin_unlock(&mm->notifier_subscriptions->lock); |
Jason Gunthorpe | 2c7933f | 2019-08-06 20:15:40 -0300 | [diff] [blame] | 911 | |
Jason Gunthorpe | 1991722 | 2020-01-14 11:11:17 -0400 | [diff] [blame] | 912 | call_srcu(&srcu, &subscription->rcu, mmu_notifier_free_rcu); |
Jason Gunthorpe | 2c7933f | 2019-08-06 20:15:40 -0300 | [diff] [blame] | 913 | return; |
| 914 | |
| 915 | out_unlock: |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 916 | spin_unlock(&mm->notifier_subscriptions->lock); |
Jason Gunthorpe | 2c7933f | 2019-08-06 20:15:40 -0300 | [diff] [blame] | 917 | } |
| 918 | EXPORT_SYMBOL_GPL(mmu_notifier_put); |
| 919 | |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 920 | static int __mmu_interval_notifier_insert( |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 921 | struct mmu_interval_notifier *interval_sub, struct mm_struct *mm, |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 922 | struct mmu_notifier_subscriptions *subscriptions, unsigned long start, |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 923 | unsigned long length, const struct mmu_interval_notifier_ops *ops) |
| 924 | { |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 925 | interval_sub->mm = mm; |
| 926 | interval_sub->ops = ops; |
| 927 | RB_CLEAR_NODE(&interval_sub->interval_tree.rb); |
| 928 | interval_sub->interval_tree.start = start; |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 929 | /* |
| 930 | * Note that the representation of the intervals in the interval tree |
| 931 | * considers the ending point as contained in the interval. |
| 932 | */ |
| 933 | if (length == 0 || |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 934 | check_add_overflow(start, length - 1, |
| 935 | &interval_sub->interval_tree.last)) |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 936 | return -EOVERFLOW; |
| 937 | |
| 938 | /* Must call with a mmget() held */ |
Jann Horn | c9682d1 | 2020-10-15 20:07:43 -0700 | [diff] [blame] | 939 | if (WARN_ON(atomic_read(&mm->mm_users) <= 0)) |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 940 | return -EINVAL; |
| 941 | |
| 942 | /* pairs with mmdrop in mmu_interval_notifier_remove() */ |
| 943 | mmgrab(mm); |
| 944 | |
| 945 | /* |
| 946 | * If some invalidate_range_start/end region is going on in parallel |
| 947 | * we don't know what VA ranges are affected, so we must assume this |
| 948 | * new range is included. |
| 949 | * |
| 950 | * If the itree is invalidating then we are not allowed to change |
| 951 | * it. Retrying until invalidation is done is tricky due to the |
| 952 | * possibility for live lock, instead defer the add to |
| 953 | * mn_itree_inv_end() so this algorithm is deterministic. |
| 954 | * |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 955 | * In all cases the value for the interval_sub->invalidate_seq should be |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 956 | * odd, see mmu_interval_read_begin() |
| 957 | */ |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 958 | spin_lock(&subscriptions->lock); |
| 959 | if (subscriptions->active_invalidate_ranges) { |
| 960 | if (mn_itree_is_invalidating(subscriptions)) |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 961 | hlist_add_head(&interval_sub->deferred_item, |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 962 | &subscriptions->deferred_list); |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 963 | else { |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 964 | subscriptions->invalidate_seq |= 1; |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 965 | interval_tree_insert(&interval_sub->interval_tree, |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 966 | &subscriptions->itree); |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 967 | } |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 968 | interval_sub->invalidate_seq = subscriptions->invalidate_seq; |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 969 | } else { |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 970 | WARN_ON(mn_itree_is_invalidating(subscriptions)); |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 971 | /* |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 972 | * The starting seq for a subscription not under invalidation |
| 973 | * should be odd, not equal to the current invalidate_seq and |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 974 | * invalidate_seq should not 'wrap' to the new seq any time |
| 975 | * soon. |
| 976 | */ |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 977 | interval_sub->invalidate_seq = |
| 978 | subscriptions->invalidate_seq - 1; |
| 979 | interval_tree_insert(&interval_sub->interval_tree, |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 980 | &subscriptions->itree); |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 981 | } |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 982 | spin_unlock(&subscriptions->lock); |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 983 | return 0; |
| 984 | } |
| 985 | |
| 986 | /** |
| 987 | * mmu_interval_notifier_insert - Insert an interval notifier |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 988 | * @interval_sub: Interval subscription to register |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 989 | * @start: Starting virtual address to monitor |
| 990 | * @length: Length of the range to monitor |
Krzysztof Kozlowski | d49653f | 2020-08-11 18:32:09 -0700 | [diff] [blame] | 991 | * @mm: mm_struct to attach to |
| 992 | * @ops: Interval notifier operations to be called on matching events |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 993 | * |
| 994 | * This function subscribes the interval notifier for notifications from the |
| 995 | * mm. Upon return the ops related to mmu_interval_notifier will be called |
| 996 | * whenever an event that intersects with the given range occurs. |
| 997 | * |
| 998 | * Upon return the range_notifier may not be present in the interval tree yet. |
| 999 | * The caller must use the normal interval notifier read flow via |
| 1000 | * mmu_interval_read_begin() to establish SPTEs for this range. |
| 1001 | */ |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 1002 | int mmu_interval_notifier_insert(struct mmu_interval_notifier *interval_sub, |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 1003 | struct mm_struct *mm, unsigned long start, |
| 1004 | unsigned long length, |
| 1005 | const struct mmu_interval_notifier_ops *ops) |
| 1006 | { |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 1007 | struct mmu_notifier_subscriptions *subscriptions; |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 1008 | int ret; |
| 1009 | |
Michel Lespinasse | da1c55f | 2020-06-08 21:33:47 -0700 | [diff] [blame] | 1010 | might_lock(&mm->mmap_lock); |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 1011 | |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 1012 | subscriptions = smp_load_acquire(&mm->notifier_subscriptions); |
| 1013 | if (!subscriptions || !subscriptions->has_itree) { |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 1014 | ret = mmu_notifier_register(NULL, mm); |
| 1015 | if (ret) |
| 1016 | return ret; |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 1017 | subscriptions = mm->notifier_subscriptions; |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 1018 | } |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 1019 | return __mmu_interval_notifier_insert(interval_sub, mm, subscriptions, |
| 1020 | start, length, ops); |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 1021 | } |
| 1022 | EXPORT_SYMBOL_GPL(mmu_interval_notifier_insert); |
| 1023 | |
| 1024 | int mmu_interval_notifier_insert_locked( |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 1025 | struct mmu_interval_notifier *interval_sub, struct mm_struct *mm, |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 1026 | unsigned long start, unsigned long length, |
| 1027 | const struct mmu_interval_notifier_ops *ops) |
| 1028 | { |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 1029 | struct mmu_notifier_subscriptions *subscriptions = |
| 1030 | mm->notifier_subscriptions; |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 1031 | int ret; |
| 1032 | |
Michel Lespinasse | 42fc541 | 2020-06-08 21:33:44 -0700 | [diff] [blame] | 1033 | mmap_assert_write_locked(mm); |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 1034 | |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 1035 | if (!subscriptions || !subscriptions->has_itree) { |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 1036 | ret = __mmu_notifier_register(NULL, mm); |
| 1037 | if (ret) |
| 1038 | return ret; |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 1039 | subscriptions = mm->notifier_subscriptions; |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 1040 | } |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 1041 | return __mmu_interval_notifier_insert(interval_sub, mm, subscriptions, |
| 1042 | start, length, ops); |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 1043 | } |
| 1044 | EXPORT_SYMBOL_GPL(mmu_interval_notifier_insert_locked); |
| 1045 | |
| 1046 | /** |
| 1047 | * mmu_interval_notifier_remove - Remove a interval notifier |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 1048 | * @interval_sub: Interval subscription to unregister |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 1049 | * |
| 1050 | * This function must be paired with mmu_interval_notifier_insert(). It cannot |
| 1051 | * be called from any ops callback. |
| 1052 | * |
| 1053 | * Once this returns ops callbacks are no longer running on other CPUs and |
| 1054 | * will not be called in future. |
| 1055 | */ |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 1056 | void mmu_interval_notifier_remove(struct mmu_interval_notifier *interval_sub) |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 1057 | { |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 1058 | struct mm_struct *mm = interval_sub->mm; |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 1059 | struct mmu_notifier_subscriptions *subscriptions = |
| 1060 | mm->notifier_subscriptions; |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 1061 | unsigned long seq = 0; |
| 1062 | |
| 1063 | might_sleep(); |
| 1064 | |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 1065 | spin_lock(&subscriptions->lock); |
| 1066 | if (mn_itree_is_invalidating(subscriptions)) { |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 1067 | /* |
| 1068 | * remove is being called after insert put this on the |
| 1069 | * deferred list, but before the deferred list was processed. |
| 1070 | */ |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 1071 | if (RB_EMPTY_NODE(&interval_sub->interval_tree.rb)) { |
| 1072 | hlist_del(&interval_sub->deferred_item); |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 1073 | } else { |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 1074 | hlist_add_head(&interval_sub->deferred_item, |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 1075 | &subscriptions->deferred_list); |
| 1076 | seq = subscriptions->invalidate_seq; |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 1077 | } |
| 1078 | } else { |
Jason Gunthorpe | 5292e24 | 2020-01-14 11:29:52 -0400 | [diff] [blame] | 1079 | WARN_ON(RB_EMPTY_NODE(&interval_sub->interval_tree.rb)); |
| 1080 | interval_tree_remove(&interval_sub->interval_tree, |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 1081 | &subscriptions->itree); |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 1082 | } |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 1083 | spin_unlock(&subscriptions->lock); |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 1084 | |
| 1085 | /* |
| 1086 | * The possible sleep on progress in the invalidation requires the |
| 1087 | * caller not hold any locks held by invalidation callbacks. |
| 1088 | */ |
| 1089 | lock_map_acquire(&__mmu_notifier_invalidate_range_start_map); |
| 1090 | lock_map_release(&__mmu_notifier_invalidate_range_start_map); |
| 1091 | if (seq) |
Jason Gunthorpe | 984cfe4 | 2019-12-18 13:40:35 -0400 | [diff] [blame] | 1092 | wait_event(subscriptions->wq, |
| 1093 | READ_ONCE(subscriptions->invalidate_seq) != seq); |
Jason Gunthorpe | 99cb252 | 2019-11-12 16:22:19 -0400 | [diff] [blame] | 1094 | |
| 1095 | /* pairs with mmgrab in mmu_interval_notifier_insert() */ |
| 1096 | mmdrop(mm); |
| 1097 | } |
| 1098 | EXPORT_SYMBOL_GPL(mmu_interval_notifier_remove); |
| 1099 | |
Jason Gunthorpe | 2c7933f | 2019-08-06 20:15:40 -0300 | [diff] [blame] | 1100 | /** |
| 1101 | * mmu_notifier_synchronize - Ensure all mmu_notifiers are freed |
| 1102 | * |
| 1103 | * This function ensures that all outstanding async SRU work from |
| 1104 | * mmu_notifier_put() is completed. After it returns any mmu_notifier_ops |
| 1105 | * associated with an unused mmu_notifier will no longer be called. |
| 1106 | * |
| 1107 | * Before using the caller must ensure that all of its mmu_notifiers have been |
| 1108 | * fully released via mmu_notifier_put(). |
| 1109 | * |
| 1110 | * Modules using the mmu_notifier_put() API should call this in their __exit |
| 1111 | * function to avoid module unloading races. |
| 1112 | */ |
| 1113 | void mmu_notifier_synchronize(void) |
| 1114 | { |
| 1115 | synchronize_srcu(&srcu); |
| 1116 | } |
| 1117 | EXPORT_SYMBOL_GPL(mmu_notifier_synchronize); |
| 1118 | |
Jérôme Glisse | c6d2341 | 2019-05-13 17:21:00 -0700 | [diff] [blame] | 1119 | bool |
| 1120 | mmu_notifier_range_update_to_read_only(const struct mmu_notifier_range *range) |
| 1121 | { |
| 1122 | if (!range->vma || range->event != MMU_NOTIFY_PROTECTION_VMA) |
| 1123 | return false; |
| 1124 | /* Return true if the vma still have the read flag set. */ |
| 1125 | return range->vma->vm_flags & VM_READ; |
| 1126 | } |
| 1127 | EXPORT_SYMBOL_GPL(mmu_notifier_range_update_to_read_only); |