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