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> |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 15 | #include <linux/srcu.h> |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 16 | #include <linux/rcupdate.h> |
| 17 | #include <linux/sched.h> |
Ingo Molnar | 6e84f31 | 2017-02-08 18:51:29 +0100 | [diff] [blame] | 18 | #include <linux/sched/mm.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 20 | |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 21 | /* global SRCU for all MMs */ |
Paul E. McKenney | dde8da6 | 2017-03-25 10:42:07 -0700 | [diff] [blame] | 22 | DEFINE_STATIC_SRCU(srcu); |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 23 | |
Daniel Vetter | 23b6839 | 2019-08-26 22:14:21 +0200 | [diff] [blame] | 24 | #ifdef CONFIG_LOCKDEP |
| 25 | struct lockdep_map __mmu_notifier_invalidate_range_start_map = { |
| 26 | .name = "mmu_notifier_invalidate_range_start" |
| 27 | }; |
| 28 | #endif |
| 29 | |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 30 | /* |
Jason Gunthorpe | 56f434f | 2019-11-12 16:22:18 -0400 | [diff] [blame^] | 31 | * The mmu notifier_mm structure is allocated and installed in |
| 32 | * mm->mmu_notifier_mm inside the mm_take_all_locks() protected |
| 33 | * critical section and it's released only when mm_count reaches zero |
| 34 | * in mmdrop(). |
| 35 | */ |
| 36 | struct mmu_notifier_mm { |
| 37 | /* all mmu notifiers registered in this mm are queued in this list */ |
| 38 | struct hlist_head list; |
| 39 | /* to serialize the list modifications and hlist_unhashed */ |
| 40 | spinlock_t lock; |
| 41 | }; |
| 42 | |
| 43 | /* |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 44 | * This function can't run concurrently against mmu_notifier_register |
| 45 | * because mm->mm_users > 0 during mmu_notifier_register and exit_mmap |
| 46 | * runs with mm_users == 0. Other tasks may still invoke mmu notifiers |
| 47 | * in parallel despite there being no task using this mm any more, |
| 48 | * through the vmas outside of the exit_mmap context, such as with |
| 49 | * vmtruncate. This serializes against mmu_notifier_unregister with |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 50 | * the mmu_notifier_mm->lock in addition to SRCU and it serializes |
| 51 | * against the other mmu notifiers with SRCU. struct mmu_notifier_mm |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 52 | * can't go away from under us as exit_mmap holds an mm_count pin |
| 53 | * itself. |
| 54 | */ |
| 55 | void __mmu_notifier_release(struct mm_struct *mm) |
| 56 | { |
| 57 | struct mmu_notifier *mn; |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 58 | int id; |
Xiao Guangrong | 3ad3d90 | 2012-07-31 16:45:52 -0700 | [diff] [blame] | 59 | |
| 60 | /* |
Xiao Guangrong | d34883d | 2013-05-24 15:55:11 -0700 | [diff] [blame] | 61 | * SRCU here will block mmu_notifier_unregister until |
| 62 | * ->release returns. |
Xiao Guangrong | 3ad3d90 | 2012-07-31 16:45:52 -0700 | [diff] [blame] | 63 | */ |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 64 | id = srcu_read_lock(&srcu); |
Xiao Guangrong | d34883d | 2013-05-24 15:55:11 -0700 | [diff] [blame] | 65 | hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist) |
| 66 | /* |
| 67 | * If ->release runs before mmu_notifier_unregister it must be |
| 68 | * handled, as it's the only way for the driver to flush all |
| 69 | * existing sptes and stop the driver from establishing any more |
| 70 | * sptes before all the pages in the mm are freed. |
| 71 | */ |
| 72 | if (mn->ops->release) |
| 73 | mn->ops->release(mn, mm); |
Xiao Guangrong | d34883d | 2013-05-24 15:55:11 -0700 | [diff] [blame] | 74 | |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 75 | spin_lock(&mm->mmu_notifier_mm->lock); |
| 76 | while (unlikely(!hlist_empty(&mm->mmu_notifier_mm->list))) { |
| 77 | mn = hlist_entry(mm->mmu_notifier_mm->list.first, |
| 78 | struct mmu_notifier, |
| 79 | hlist); |
| 80 | /* |
Xiao Guangrong | d34883d | 2013-05-24 15:55:11 -0700 | [diff] [blame] | 81 | * We arrived before mmu_notifier_unregister so |
| 82 | * mmu_notifier_unregister will do nothing other than to wait |
| 83 | * for ->release to finish and for mmu_notifier_unregister to |
| 84 | * return. |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 85 | */ |
| 86 | hlist_del_init_rcu(&mn->hlist); |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 87 | } |
| 88 | spin_unlock(&mm->mmu_notifier_mm->lock); |
Peter Zijlstra | b972216 | 2014-08-06 16:08:20 -0700 | [diff] [blame] | 89 | srcu_read_unlock(&srcu, id); |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 90 | |
| 91 | /* |
Xiao Guangrong | d34883d | 2013-05-24 15:55:11 -0700 | [diff] [blame] | 92 | * synchronize_srcu here prevents mmu_notifier_release from returning to |
| 93 | * exit_mmap (which would proceed with freeing all pages in the mm) |
| 94 | * until the ->release method returns, if it was invoked by |
| 95 | * mmu_notifier_unregister. |
| 96 | * |
| 97 | * The mmu_notifier_mm can't go away from under us because one mm_count |
| 98 | * is held by exit_mmap. |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 99 | */ |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 100 | synchronize_srcu(&srcu); |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /* |
| 104 | * If no young bitflag is supported by the hardware, ->clear_flush_young can |
| 105 | * unmap the address and return 1 or 0 depending if the mapping previously |
| 106 | * existed or not. |
| 107 | */ |
| 108 | int __mmu_notifier_clear_flush_young(struct mm_struct *mm, |
Andres Lagar-Cavilla | 5712846 | 2014-09-22 14:54:42 -0700 | [diff] [blame] | 109 | unsigned long start, |
| 110 | unsigned long end) |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 111 | { |
| 112 | struct mmu_notifier *mn; |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 113 | int young = 0, id; |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 114 | |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 115 | id = srcu_read_lock(&srcu); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 116 | hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist) { |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 117 | if (mn->ops->clear_flush_young) |
Andres Lagar-Cavilla | 5712846 | 2014-09-22 14:54:42 -0700 | [diff] [blame] | 118 | young |= mn->ops->clear_flush_young(mn, mm, start, end); |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 119 | } |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 120 | srcu_read_unlock(&srcu, id); |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 121 | |
| 122 | return young; |
| 123 | } |
| 124 | |
Vladimir Davydov | 1d7715c | 2015-09-09 15:35:41 -0700 | [diff] [blame] | 125 | int __mmu_notifier_clear_young(struct mm_struct *mm, |
| 126 | unsigned long start, |
| 127 | unsigned long end) |
| 128 | { |
| 129 | struct mmu_notifier *mn; |
| 130 | int young = 0, id; |
| 131 | |
| 132 | id = srcu_read_lock(&srcu); |
| 133 | hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist) { |
| 134 | if (mn->ops->clear_young) |
| 135 | young |= mn->ops->clear_young(mn, mm, start, end); |
| 136 | } |
| 137 | srcu_read_unlock(&srcu, id); |
| 138 | |
| 139 | return young; |
| 140 | } |
| 141 | |
Andrea Arcangeli | 8ee5382 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 142 | int __mmu_notifier_test_young(struct mm_struct *mm, |
| 143 | unsigned long address) |
| 144 | { |
| 145 | struct mmu_notifier *mn; |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 146 | int young = 0, id; |
Andrea Arcangeli | 8ee5382 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 147 | |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 148 | id = srcu_read_lock(&srcu); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 149 | hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist) { |
Andrea Arcangeli | 8ee5382 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 150 | if (mn->ops->test_young) { |
| 151 | young = mn->ops->test_young(mn, mm, address); |
| 152 | if (young) |
| 153 | break; |
| 154 | } |
| 155 | } |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 156 | srcu_read_unlock(&srcu, id); |
Andrea Arcangeli | 8ee5382 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 157 | |
| 158 | return young; |
| 159 | } |
| 160 | |
Izik Eidus | 828502d | 2009-09-21 17:01:51 -0700 | [diff] [blame] | 161 | void __mmu_notifier_change_pte(struct mm_struct *mm, unsigned long address, |
| 162 | pte_t pte) |
| 163 | { |
| 164 | struct mmu_notifier *mn; |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 165 | int id; |
Izik Eidus | 828502d | 2009-09-21 17:01:51 -0700 | [diff] [blame] | 166 | |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 167 | id = srcu_read_lock(&srcu); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 168 | hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist) { |
Izik Eidus | 828502d | 2009-09-21 17:01:51 -0700 | [diff] [blame] | 169 | if (mn->ops->change_pte) |
| 170 | mn->ops->change_pte(mn, mm, address, pte); |
Izik Eidus | 828502d | 2009-09-21 17:01:51 -0700 | [diff] [blame] | 171 | } |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 172 | srcu_read_unlock(&srcu, id); |
Izik Eidus | 828502d | 2009-09-21 17:01:51 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 175 | int __mmu_notifier_invalidate_range_start(struct mmu_notifier_range *range) |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 176 | { |
| 177 | struct mmu_notifier *mn; |
Michal Hocko | 93065ac | 2018-08-21 21:52:33 -0700 | [diff] [blame] | 178 | int ret = 0; |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 179 | int id; |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 180 | |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 181 | id = srcu_read_lock(&srcu); |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 182 | hlist_for_each_entry_rcu(mn, &range->mm->mmu_notifier_mm->list, hlist) { |
Michal Hocko | 93065ac | 2018-08-21 21:52:33 -0700 | [diff] [blame] | 183 | if (mn->ops->invalidate_range_start) { |
Daniel Vetter | ba170f7 | 2019-08-26 22:14:24 +0200 | [diff] [blame] | 184 | int _ret; |
| 185 | |
| 186 | if (!mmu_notifier_range_blockable(range)) |
| 187 | non_block_start(); |
| 188 | _ret = mn->ops->invalidate_range_start(mn, range); |
| 189 | if (!mmu_notifier_range_blockable(range)) |
| 190 | non_block_end(); |
Michal Hocko | 93065ac | 2018-08-21 21:52:33 -0700 | [diff] [blame] | 191 | if (_ret) { |
| 192 | pr_info("%pS callback failed with %d in %sblockable context.\n", |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 193 | mn->ops->invalidate_range_start, _ret, |
Jérôme Glisse | dfcd666 | 2019-05-13 17:20:38 -0700 | [diff] [blame] | 194 | !mmu_notifier_range_blockable(range) ? "non-" : ""); |
Daniel Vetter | 8402ce6 | 2019-08-14 22:20:23 +0200 | [diff] [blame] | 195 | WARN_ON(mmu_notifier_range_blockable(range) || |
| 196 | ret != -EAGAIN); |
Michal Hocko | 93065ac | 2018-08-21 21:52:33 -0700 | [diff] [blame] | 197 | ret = _ret; |
| 198 | } |
| 199 | } |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 200 | } |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 201 | srcu_read_unlock(&srcu, id); |
Michal Hocko | 93065ac | 2018-08-21 21:52:33 -0700 | [diff] [blame] | 202 | |
| 203 | return ret; |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 206 | void __mmu_notifier_invalidate_range_end(struct mmu_notifier_range *range, |
Jérôme Glisse | 4645b9f | 2017-11-15 17:34:11 -0800 | [diff] [blame] | 207 | bool only_end) |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 208 | { |
| 209 | struct mmu_notifier *mn; |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 210 | int id; |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 211 | |
Daniel Vetter | 23b6839 | 2019-08-26 22:14:21 +0200 | [diff] [blame] | 212 | lock_map_acquire(&__mmu_notifier_invalidate_range_start_map); |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 213 | id = srcu_read_lock(&srcu); |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 214 | hlist_for_each_entry_rcu(mn, &range->mm->mmu_notifier_mm->list, hlist) { |
Joerg Roedel | 0f0a327 | 2014-11-13 13:46:09 +1100 | [diff] [blame] | 215 | /* |
| 216 | * Call invalidate_range here too to avoid the need for the |
| 217 | * subsystem of having to register an invalidate_range_end |
| 218 | * call-back when there is invalidate_range already. Usually a |
| 219 | * subsystem registers either invalidate_range_start()/end() or |
| 220 | * invalidate_range(), so this will be no additional overhead |
| 221 | * (besides the pointer check). |
Jérôme Glisse | 4645b9f | 2017-11-15 17:34:11 -0800 | [diff] [blame] | 222 | * |
| 223 | * We skip call to invalidate_range() if we know it is safe ie |
| 224 | * call site use mmu_notifier_invalidate_range_only_end() which |
| 225 | * is safe to do when we know that a call to invalidate_range() |
| 226 | * already happen under page table lock. |
Joerg Roedel | 0f0a327 | 2014-11-13 13:46:09 +1100 | [diff] [blame] | 227 | */ |
Jérôme Glisse | 4645b9f | 2017-11-15 17:34:11 -0800 | [diff] [blame] | 228 | if (!only_end && mn->ops->invalidate_range) |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 229 | mn->ops->invalidate_range(mn, range->mm, |
| 230 | range->start, |
| 231 | range->end); |
Daniel Vetter | ba170f7 | 2019-08-26 22:14:24 +0200 | [diff] [blame] | 232 | if (mn->ops->invalidate_range_end) { |
| 233 | if (!mmu_notifier_range_blockable(range)) |
| 234 | non_block_start(); |
Jérôme Glisse | 5d6527a | 2018-12-28 00:38:05 -0800 | [diff] [blame] | 235 | mn->ops->invalidate_range_end(mn, range); |
Daniel Vetter | ba170f7 | 2019-08-26 22:14:24 +0200 | [diff] [blame] | 236 | if (!mmu_notifier_range_blockable(range)) |
| 237 | non_block_end(); |
| 238 | } |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 239 | } |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 240 | srcu_read_unlock(&srcu, id); |
Daniel Vetter | 23b6839 | 2019-08-26 22:14:21 +0200 | [diff] [blame] | 241 | lock_map_release(&__mmu_notifier_invalidate_range_start_map); |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Joerg Roedel | 0f0a327 | 2014-11-13 13:46:09 +1100 | [diff] [blame] | 244 | void __mmu_notifier_invalidate_range(struct mm_struct *mm, |
| 245 | unsigned long start, unsigned long end) |
| 246 | { |
| 247 | struct mmu_notifier *mn; |
| 248 | int id; |
| 249 | |
| 250 | id = srcu_read_lock(&srcu); |
| 251 | hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist) { |
| 252 | if (mn->ops->invalidate_range) |
| 253 | mn->ops->invalidate_range(mn, mm, start, end); |
| 254 | } |
| 255 | srcu_read_unlock(&srcu, id); |
| 256 | } |
Joerg Roedel | 0f0a327 | 2014-11-13 13:46:09 +1100 | [diff] [blame] | 257 | |
Jason Gunthorpe | 56c57103 | 2019-08-06 20:15:38 -0300 | [diff] [blame] | 258 | /* |
| 259 | * Same as mmu_notifier_register but here the caller must hold the |
| 260 | * mmap_sem in write mode. |
| 261 | */ |
| 262 | int __mmu_notifier_register(struct mmu_notifier *mn, struct mm_struct *mm) |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 263 | { |
Jason Gunthorpe | 70df291 | 2019-08-06 20:15:39 -0300 | [diff] [blame] | 264 | struct mmu_notifier_mm *mmu_notifier_mm = NULL; |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 265 | int ret; |
| 266 | |
Jason Gunthorpe | 56c57103 | 2019-08-06 20:15:38 -0300 | [diff] [blame] | 267 | lockdep_assert_held_write(&mm->mmap_sem); |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 268 | BUG_ON(atomic_read(&mm->mm_users) <= 0); |
| 269 | |
Daniel Vetter | 66204f1 | 2019-08-26 22:14:22 +0200 | [diff] [blame] | 270 | if (IS_ENABLED(CONFIG_LOCKDEP)) { |
| 271 | fs_reclaim_acquire(GFP_KERNEL); |
| 272 | lock_map_acquire(&__mmu_notifier_invalidate_range_start_map); |
| 273 | lock_map_release(&__mmu_notifier_invalidate_range_start_map); |
| 274 | fs_reclaim_release(GFP_KERNEL); |
| 275 | } |
| 276 | |
Jason Gunthorpe | 2c7933f | 2019-08-06 20:15:40 -0300 | [diff] [blame] | 277 | mn->mm = mm; |
| 278 | mn->users = 1; |
| 279 | |
Jason Gunthorpe | 70df291 | 2019-08-06 20:15:39 -0300 | [diff] [blame] | 280 | if (!mm->mmu_notifier_mm) { |
| 281 | /* |
| 282 | * kmalloc cannot be called under mm_take_all_locks(), but we |
| 283 | * know that mm->mmu_notifier_mm can't change while we hold |
| 284 | * the write side of the mmap_sem. |
| 285 | */ |
| 286 | mmu_notifier_mm = |
| 287 | kmalloc(sizeof(struct mmu_notifier_mm), GFP_KERNEL); |
| 288 | if (!mmu_notifier_mm) |
| 289 | return -ENOMEM; |
| 290 | |
| 291 | INIT_HLIST_HEAD(&mmu_notifier_mm->list); |
| 292 | spin_lock_init(&mmu_notifier_mm->lock); |
| 293 | } |
Gavin Shan | 35cfa2b | 2012-10-25 13:38:01 -0700 | [diff] [blame] | 294 | |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 295 | ret = mm_take_all_locks(mm); |
| 296 | if (unlikely(ret)) |
Gavin Shan | 35cfa2b | 2012-10-25 13:38:01 -0700 | [diff] [blame] | 297 | goto out_clean; |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 298 | |
Jason Gunthorpe | 70df291 | 2019-08-06 20:15:39 -0300 | [diff] [blame] | 299 | /* Pairs with the mmdrop in mmu_notifier_unregister_* */ |
Vegard Nossum | f1f1007 | 2017-02-27 14:30:07 -0800 | [diff] [blame] | 300 | mmgrab(mm); |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 301 | |
| 302 | /* |
| 303 | * Serialize the update against mmu_notifier_unregister. A |
| 304 | * side note: mmu_notifier_release can't run concurrently with |
| 305 | * us because we hold the mm_users pin (either implicitly as |
| 306 | * current->mm or explicitly with get_task_mm() or similar). |
| 307 | * We can't race against any other mmu notifier method either |
| 308 | * thanks to mm_take_all_locks(). |
| 309 | */ |
Jason Gunthorpe | 70df291 | 2019-08-06 20:15:39 -0300 | [diff] [blame] | 310 | if (mmu_notifier_mm) |
| 311 | mm->mmu_notifier_mm = mmu_notifier_mm; |
| 312 | |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 313 | spin_lock(&mm->mmu_notifier_mm->lock); |
Jean-Philippe Brucker | 543bdb2 | 2019-07-11 20:58:50 -0700 | [diff] [blame] | 314 | hlist_add_head_rcu(&mn->hlist, &mm->mmu_notifier_mm->list); |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 315 | spin_unlock(&mm->mmu_notifier_mm->lock); |
| 316 | |
| 317 | mm_drop_all_locks(mm); |
Jason Gunthorpe | 70df291 | 2019-08-06 20:15:39 -0300 | [diff] [blame] | 318 | BUG_ON(atomic_read(&mm->mm_users) <= 0); |
| 319 | return 0; |
| 320 | |
Gavin Shan | 35cfa2b | 2012-10-25 13:38:01 -0700 | [diff] [blame] | 321 | out_clean: |
Gavin Shan | 35cfa2b | 2012-10-25 13:38:01 -0700 | [diff] [blame] | 322 | kfree(mmu_notifier_mm); |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 323 | return ret; |
| 324 | } |
Jason Gunthorpe | 56c57103 | 2019-08-06 20:15:38 -0300 | [diff] [blame] | 325 | EXPORT_SYMBOL_GPL(__mmu_notifier_register); |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 326 | |
Jason Gunthorpe | 2c7933f | 2019-08-06 20:15:40 -0300 | [diff] [blame] | 327 | /** |
| 328 | * mmu_notifier_register - Register a notifier on a mm |
| 329 | * @mn: The notifier to attach |
| 330 | * @mm: The mm to attach the notifier to |
| 331 | * |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 332 | * Must not hold mmap_sem nor any other VM related lock when calling |
| 333 | * this registration function. Must also ensure mm_users can't go down |
| 334 | * to zero while this runs to avoid races with mmu_notifier_release, |
| 335 | * so mm has to be current->mm or the mm should be pinned safely such |
| 336 | * as with get_task_mm(). If the mm is not current->mm, the mm_users |
| 337 | * pin should be released by calling mmput after mmu_notifier_register |
Jason Gunthorpe | 2c7933f | 2019-08-06 20:15:40 -0300 | [diff] [blame] | 338 | * returns. |
| 339 | * |
| 340 | * mmu_notifier_unregister() or mmu_notifier_put() must be always called to |
| 341 | * unregister the notifier. |
| 342 | * |
| 343 | * While the caller has a mmu_notifier get the mn->mm pointer will remain |
| 344 | * 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] | 345 | */ |
| 346 | int mmu_notifier_register(struct mmu_notifier *mn, struct mm_struct *mm) |
| 347 | { |
Jason Gunthorpe | 56c57103 | 2019-08-06 20:15:38 -0300 | [diff] [blame] | 348 | int ret; |
| 349 | |
| 350 | down_write(&mm->mmap_sem); |
| 351 | ret = __mmu_notifier_register(mn, mm); |
| 352 | up_write(&mm->mmap_sem); |
| 353 | return ret; |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 354 | } |
| 355 | EXPORT_SYMBOL_GPL(mmu_notifier_register); |
| 356 | |
Jason Gunthorpe | 2c7933f | 2019-08-06 20:15:40 -0300 | [diff] [blame] | 357 | static struct mmu_notifier * |
| 358 | find_get_mmu_notifier(struct mm_struct *mm, const struct mmu_notifier_ops *ops) |
| 359 | { |
| 360 | struct mmu_notifier *mn; |
| 361 | |
| 362 | spin_lock(&mm->mmu_notifier_mm->lock); |
| 363 | hlist_for_each_entry_rcu (mn, &mm->mmu_notifier_mm->list, hlist) { |
| 364 | if (mn->ops != ops) |
| 365 | continue; |
| 366 | |
| 367 | if (likely(mn->users != UINT_MAX)) |
| 368 | mn->users++; |
| 369 | else |
| 370 | mn = ERR_PTR(-EOVERFLOW); |
| 371 | spin_unlock(&mm->mmu_notifier_mm->lock); |
| 372 | return mn; |
| 373 | } |
| 374 | spin_unlock(&mm->mmu_notifier_mm->lock); |
| 375 | return NULL; |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * mmu_notifier_get_locked - Return the single struct mmu_notifier for |
| 380 | * the mm & ops |
| 381 | * @ops: The operations struct being subscribe with |
| 382 | * @mm : The mm to attach notifiers too |
| 383 | * |
| 384 | * This function either allocates a new mmu_notifier via |
| 385 | * ops->alloc_notifier(), or returns an already existing notifier on the |
| 386 | * list. The value of the ops pointer is used to determine when two notifiers |
| 387 | * are the same. |
| 388 | * |
| 389 | * Each call to mmu_notifier_get() must be paired with a call to |
| 390 | * mmu_notifier_put(). The caller must hold the write side of mm->mmap_sem. |
| 391 | * |
| 392 | * While the caller has a mmu_notifier get the mm pointer will remain valid, |
| 393 | * and can be converted to an active mm pointer via mmget_not_zero(). |
| 394 | */ |
| 395 | struct mmu_notifier *mmu_notifier_get_locked(const struct mmu_notifier_ops *ops, |
| 396 | struct mm_struct *mm) |
| 397 | { |
| 398 | struct mmu_notifier *mn; |
| 399 | int ret; |
| 400 | |
| 401 | lockdep_assert_held_write(&mm->mmap_sem); |
| 402 | |
| 403 | if (mm->mmu_notifier_mm) { |
| 404 | mn = find_get_mmu_notifier(mm, ops); |
| 405 | if (mn) |
| 406 | return mn; |
| 407 | } |
| 408 | |
| 409 | mn = ops->alloc_notifier(mm); |
| 410 | if (IS_ERR(mn)) |
| 411 | return mn; |
| 412 | mn->ops = ops; |
| 413 | ret = __mmu_notifier_register(mn, mm); |
| 414 | if (ret) |
| 415 | goto out_free; |
| 416 | return mn; |
| 417 | out_free: |
| 418 | mn->ops->free_notifier(mn); |
| 419 | return ERR_PTR(ret); |
| 420 | } |
| 421 | EXPORT_SYMBOL_GPL(mmu_notifier_get_locked); |
| 422 | |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 423 | /* this is called after the last mmu_notifier_unregister() returned */ |
| 424 | void __mmu_notifier_mm_destroy(struct mm_struct *mm) |
| 425 | { |
| 426 | BUG_ON(!hlist_empty(&mm->mmu_notifier_mm->list)); |
| 427 | kfree(mm->mmu_notifier_mm); |
| 428 | mm->mmu_notifier_mm = LIST_POISON1; /* debug */ |
| 429 | } |
| 430 | |
| 431 | /* |
| 432 | * This releases the mm_count pin automatically and frees the mm |
| 433 | * structure if it was the last user of it. It serializes against |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 434 | * running mmu notifiers with SRCU and against mmu_notifier_unregister |
| 435 | * with the unregister lock + SRCU. All sptes must be dropped before |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 436 | * calling mmu_notifier_unregister. ->release or any other notifier |
| 437 | * method may be invoked concurrently with mmu_notifier_unregister, |
| 438 | * and only after mmu_notifier_unregister returned we're guaranteed |
| 439 | * that ->release or any other method can't run anymore. |
| 440 | */ |
| 441 | void mmu_notifier_unregister(struct mmu_notifier *mn, struct mm_struct *mm) |
| 442 | { |
| 443 | BUG_ON(atomic_read(&mm->mm_count) <= 0); |
| 444 | |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 445 | if (!hlist_unhashed(&mn->hlist)) { |
Xiao Guangrong | d34883d | 2013-05-24 15:55:11 -0700 | [diff] [blame] | 446 | /* |
| 447 | * SRCU here will force exit_mmap to wait for ->release to |
| 448 | * finish before freeing the pages. |
| 449 | */ |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 450 | int id; |
Xiao Guangrong | 3ad3d90 | 2012-07-31 16:45:52 -0700 | [diff] [blame] | 451 | |
Robin Holt | 751efd8 | 2013-02-22 16:35:34 -0800 | [diff] [blame] | 452 | id = srcu_read_lock(&srcu); |
Xiao Guangrong | d34883d | 2013-05-24 15:55:11 -0700 | [diff] [blame] | 453 | /* |
| 454 | * exit_mmap will block in mmu_notifier_release to guarantee |
| 455 | * that ->release is called before freeing the pages. |
| 456 | */ |
Robin Holt | 751efd8 | 2013-02-22 16:35:34 -0800 | [diff] [blame] | 457 | if (mn->ops->release) |
| 458 | mn->ops->release(mn, mm); |
Robin Holt | 751efd8 | 2013-02-22 16:35:34 -0800 | [diff] [blame] | 459 | srcu_read_unlock(&srcu, id); |
Xiao Guangrong | d34883d | 2013-05-24 15:55:11 -0700 | [diff] [blame] | 460 | |
| 461 | spin_lock(&mm->mmu_notifier_mm->lock); |
| 462 | /* |
| 463 | * Can not use list_del_rcu() since __mmu_notifier_release |
| 464 | * can delete it before we hold the lock. |
| 465 | */ |
| 466 | hlist_del_init_rcu(&mn->hlist); |
Robin Holt | 751efd8 | 2013-02-22 16:35:34 -0800 | [diff] [blame] | 467 | spin_unlock(&mm->mmu_notifier_mm->lock); |
Xiao Guangrong | d34883d | 2013-05-24 15:55:11 -0700 | [diff] [blame] | 468 | } |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 469 | |
| 470 | /* |
Xiao Guangrong | d34883d | 2013-05-24 15:55:11 -0700 | [diff] [blame] | 471 | * Wait for any running method to finish, of course including |
Geert Uytterhoeven | 83a35e3 | 2013-06-28 11:27:31 +0200 | [diff] [blame] | 472 | * ->release if it was run by mmu_notifier_release instead of us. |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 473 | */ |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 474 | synchronize_srcu(&srcu); |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 475 | |
| 476 | BUG_ON(atomic_read(&mm->mm_count) <= 0); |
| 477 | |
| 478 | mmdrop(mm); |
| 479 | } |
| 480 | EXPORT_SYMBOL_GPL(mmu_notifier_unregister); |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 481 | |
Jason Gunthorpe | 2c7933f | 2019-08-06 20:15:40 -0300 | [diff] [blame] | 482 | static void mmu_notifier_free_rcu(struct rcu_head *rcu) |
| 483 | { |
| 484 | struct mmu_notifier *mn = container_of(rcu, struct mmu_notifier, rcu); |
| 485 | struct mm_struct *mm = mn->mm; |
| 486 | |
| 487 | mn->ops->free_notifier(mn); |
| 488 | /* Pairs with the get in __mmu_notifier_register() */ |
| 489 | mmdrop(mm); |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * mmu_notifier_put - Release the reference on the notifier |
| 494 | * @mn: The notifier to act on |
| 495 | * |
| 496 | * This function must be paired with each mmu_notifier_get(), it releases the |
| 497 | * reference obtained by the get. If this is the last reference then process |
| 498 | * to free the notifier will be run asynchronously. |
| 499 | * |
| 500 | * Unlike mmu_notifier_unregister() the get/put flow only calls ops->release |
| 501 | * when the mm_struct is destroyed. Instead free_notifier is always called to |
| 502 | * release any resources held by the user. |
| 503 | * |
| 504 | * As ops->release is not guaranteed to be called, the user must ensure that |
| 505 | * all sptes are dropped, and no new sptes can be established before |
| 506 | * mmu_notifier_put() is called. |
| 507 | * |
| 508 | * This function can be called from the ops->release callback, however the |
| 509 | * caller must still ensure it is called pairwise with mmu_notifier_get(). |
| 510 | * |
| 511 | * Modules calling this function must call mmu_notifier_synchronize() in |
| 512 | * their __exit functions to ensure the async work is completed. |
| 513 | */ |
| 514 | void mmu_notifier_put(struct mmu_notifier *mn) |
| 515 | { |
| 516 | struct mm_struct *mm = mn->mm; |
| 517 | |
| 518 | spin_lock(&mm->mmu_notifier_mm->lock); |
| 519 | if (WARN_ON(!mn->users) || --mn->users) |
| 520 | goto out_unlock; |
| 521 | hlist_del_init_rcu(&mn->hlist); |
| 522 | spin_unlock(&mm->mmu_notifier_mm->lock); |
| 523 | |
| 524 | call_srcu(&srcu, &mn->rcu, mmu_notifier_free_rcu); |
| 525 | return; |
| 526 | |
| 527 | out_unlock: |
| 528 | spin_unlock(&mm->mmu_notifier_mm->lock); |
| 529 | } |
| 530 | EXPORT_SYMBOL_GPL(mmu_notifier_put); |
| 531 | |
| 532 | /** |
| 533 | * mmu_notifier_synchronize - Ensure all mmu_notifiers are freed |
| 534 | * |
| 535 | * This function ensures that all outstanding async SRU work from |
| 536 | * mmu_notifier_put() is completed. After it returns any mmu_notifier_ops |
| 537 | * associated with an unused mmu_notifier will no longer be called. |
| 538 | * |
| 539 | * Before using the caller must ensure that all of its mmu_notifiers have been |
| 540 | * fully released via mmu_notifier_put(). |
| 541 | * |
| 542 | * Modules using the mmu_notifier_put() API should call this in their __exit |
| 543 | * function to avoid module unloading races. |
| 544 | */ |
| 545 | void mmu_notifier_synchronize(void) |
| 546 | { |
| 547 | synchronize_srcu(&srcu); |
| 548 | } |
| 549 | EXPORT_SYMBOL_GPL(mmu_notifier_synchronize); |
| 550 | |
Jérôme Glisse | c6d2341 | 2019-05-13 17:21:00 -0700 | [diff] [blame] | 551 | bool |
| 552 | mmu_notifier_range_update_to_read_only(const struct mmu_notifier_range *range) |
| 553 | { |
| 554 | if (!range->vma || range->event != MMU_NOTIFY_PROTECTION_VMA) |
| 555 | return false; |
| 556 | /* Return true if the vma still have the read flag set. */ |
| 557 | return range->vma->vm_flags & VM_READ; |
| 558 | } |
| 559 | EXPORT_SYMBOL_GPL(mmu_notifier_range_update_to_read_only); |