blob: 367670cfd02b7bc10b0648fb38ca789c3b87d173 [file] [log] [blame]
Thomas Gleixner20c8ccb2019-06-04 10:11:32 +02001// SPDX-License-Identifier: GPL-2.0-only
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07002/*
3 * linux/mm/mmu_notifier.c
4 *
5 * Copyright (C) 2008 Qumranet, Inc.
6 * Copyright (C) 2008 SGI
Christoph Lameter93e205a2016-03-17 14:21:15 -07007 * Christoph Lameter <cl@linux.com>
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07008 */
9
10#include <linux/rculist.h>
11#include <linux/mmu_notifier.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040012#include <linux/export.h>
Andrea Arcangelicddb8a52008-07-28 15:46:29 -070013#include <linux/mm.h>
14#include <linux/err.h>
Sagi Grimberg21a92732012-10-08 16:29:24 -070015#include <linux/srcu.h>
Andrea Arcangelicddb8a52008-07-28 15:46:29 -070016#include <linux/rcupdate.h>
17#include <linux/sched.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010018#include <linux/sched/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Andrea Arcangelicddb8a52008-07-28 15:46:29 -070020
Sagi Grimberg21a92732012-10-08 16:29:24 -070021/* global SRCU for all MMs */
Paul E. McKenneydde8da62017-03-25 10:42:07 -070022DEFINE_STATIC_SRCU(srcu);
Sagi Grimberg21a92732012-10-08 16:29:24 -070023
Daniel Vetter23b68392019-08-26 22:14:21 +020024#ifdef CONFIG_LOCKDEP
25struct lockdep_map __mmu_notifier_invalidate_range_start_map = {
26 .name = "mmu_notifier_invalidate_range_start"
27};
28#endif
29
Andrea Arcangelicddb8a52008-07-28 15:46:29 -070030/*
Jason Gunthorpe56f434f2019-11-12 16:22:18 -040031 * 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 */
36struct 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 Arcangelicddb8a52008-07-28 15:46:29 -070044 * 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 Grimberg21a92732012-10-08 16:29:24 -070050 * 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 Arcangelicddb8a52008-07-28 15:46:29 -070052 * can't go away from under us as exit_mmap holds an mm_count pin
53 * itself.
54 */
55void __mmu_notifier_release(struct mm_struct *mm)
56{
57 struct mmu_notifier *mn;
Sagi Grimberg21a92732012-10-08 16:29:24 -070058 int id;
Xiao Guangrong3ad3d902012-07-31 16:45:52 -070059
60 /*
Xiao Guangrongd34883d2013-05-24 15:55:11 -070061 * SRCU here will block mmu_notifier_unregister until
62 * ->release returns.
Xiao Guangrong3ad3d902012-07-31 16:45:52 -070063 */
Sagi Grimberg21a92732012-10-08 16:29:24 -070064 id = srcu_read_lock(&srcu);
Xiao Guangrongd34883d2013-05-24 15:55:11 -070065 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 Guangrongd34883d2013-05-24 15:55:11 -070074
Andrea Arcangelicddb8a52008-07-28 15:46:29 -070075 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 Guangrongd34883d2013-05-24 15:55:11 -070081 * 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 Arcangelicddb8a52008-07-28 15:46:29 -070085 */
86 hlist_del_init_rcu(&mn->hlist);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -070087 }
88 spin_unlock(&mm->mmu_notifier_mm->lock);
Peter Zijlstrab9722162014-08-06 16:08:20 -070089 srcu_read_unlock(&srcu, id);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -070090
91 /*
Xiao Guangrongd34883d2013-05-24 15:55:11 -070092 * 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 Arcangelicddb8a52008-07-28 15:46:29 -070099 */
Sagi Grimberg21a92732012-10-08 16:29:24 -0700100 synchronize_srcu(&srcu);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700101}
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 */
108int __mmu_notifier_clear_flush_young(struct mm_struct *mm,
Andres Lagar-Cavilla57128462014-09-22 14:54:42 -0700109 unsigned long start,
110 unsigned long end)
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700111{
112 struct mmu_notifier *mn;
Sagi Grimberg21a92732012-10-08 16:29:24 -0700113 int young = 0, id;
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700114
Sagi Grimberg21a92732012-10-08 16:29:24 -0700115 id = srcu_read_lock(&srcu);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800116 hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist) {
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700117 if (mn->ops->clear_flush_young)
Andres Lagar-Cavilla57128462014-09-22 14:54:42 -0700118 young |= mn->ops->clear_flush_young(mn, mm, start, end);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700119 }
Sagi Grimberg21a92732012-10-08 16:29:24 -0700120 srcu_read_unlock(&srcu, id);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700121
122 return young;
123}
124
Vladimir Davydov1d7715c2015-09-09 15:35:41 -0700125int __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 Arcangeli8ee53822011-01-13 15:47:10 -0800142int __mmu_notifier_test_young(struct mm_struct *mm,
143 unsigned long address)
144{
145 struct mmu_notifier *mn;
Sagi Grimberg21a92732012-10-08 16:29:24 -0700146 int young = 0, id;
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800147
Sagi Grimberg21a92732012-10-08 16:29:24 -0700148 id = srcu_read_lock(&srcu);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800149 hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist) {
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800150 if (mn->ops->test_young) {
151 young = mn->ops->test_young(mn, mm, address);
152 if (young)
153 break;
154 }
155 }
Sagi Grimberg21a92732012-10-08 16:29:24 -0700156 srcu_read_unlock(&srcu, id);
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800157
158 return young;
159}
160
Izik Eidus828502d2009-09-21 17:01:51 -0700161void __mmu_notifier_change_pte(struct mm_struct *mm, unsigned long address,
162 pte_t pte)
163{
164 struct mmu_notifier *mn;
Sagi Grimberg21a92732012-10-08 16:29:24 -0700165 int id;
Izik Eidus828502d2009-09-21 17:01:51 -0700166
Sagi Grimberg21a92732012-10-08 16:29:24 -0700167 id = srcu_read_lock(&srcu);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800168 hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist) {
Izik Eidus828502d2009-09-21 17:01:51 -0700169 if (mn->ops->change_pte)
170 mn->ops->change_pte(mn, mm, address, pte);
Izik Eidus828502d2009-09-21 17:01:51 -0700171 }
Sagi Grimberg21a92732012-10-08 16:29:24 -0700172 srcu_read_unlock(&srcu, id);
Izik Eidus828502d2009-09-21 17:01:51 -0700173}
174
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800175int __mmu_notifier_invalidate_range_start(struct mmu_notifier_range *range)
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700176{
177 struct mmu_notifier *mn;
Michal Hocko93065ac2018-08-21 21:52:33 -0700178 int ret = 0;
Sagi Grimberg21a92732012-10-08 16:29:24 -0700179 int id;
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700180
Sagi Grimberg21a92732012-10-08 16:29:24 -0700181 id = srcu_read_lock(&srcu);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800182 hlist_for_each_entry_rcu(mn, &range->mm->mmu_notifier_mm->list, hlist) {
Michal Hocko93065ac2018-08-21 21:52:33 -0700183 if (mn->ops->invalidate_range_start) {
Daniel Vetterba170f72019-08-26 22:14:24 +0200184 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 Hocko93065ac2018-08-21 21:52:33 -0700191 if (_ret) {
192 pr_info("%pS callback failed with %d in %sblockable context.\n",
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800193 mn->ops->invalidate_range_start, _ret,
Jérôme Glissedfcd6662019-05-13 17:20:38 -0700194 !mmu_notifier_range_blockable(range) ? "non-" : "");
Daniel Vetter8402ce62019-08-14 22:20:23 +0200195 WARN_ON(mmu_notifier_range_blockable(range) ||
196 ret != -EAGAIN);
Michal Hocko93065ac2018-08-21 21:52:33 -0700197 ret = _ret;
198 }
199 }
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700200 }
Sagi Grimberg21a92732012-10-08 16:29:24 -0700201 srcu_read_unlock(&srcu, id);
Michal Hocko93065ac2018-08-21 21:52:33 -0700202
203 return ret;
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700204}
205
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800206void __mmu_notifier_invalidate_range_end(struct mmu_notifier_range *range,
Jérôme Glisse4645b9f2017-11-15 17:34:11 -0800207 bool only_end)
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700208{
209 struct mmu_notifier *mn;
Sagi Grimberg21a92732012-10-08 16:29:24 -0700210 int id;
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700211
Daniel Vetter23b68392019-08-26 22:14:21 +0200212 lock_map_acquire(&__mmu_notifier_invalidate_range_start_map);
Sagi Grimberg21a92732012-10-08 16:29:24 -0700213 id = srcu_read_lock(&srcu);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800214 hlist_for_each_entry_rcu(mn, &range->mm->mmu_notifier_mm->list, hlist) {
Joerg Roedel0f0a3272014-11-13 13:46:09 +1100215 /*
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 Glisse4645b9f2017-11-15 17:34:11 -0800222 *
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 Roedel0f0a3272014-11-13 13:46:09 +1100227 */
Jérôme Glisse4645b9f2017-11-15 17:34:11 -0800228 if (!only_end && mn->ops->invalidate_range)
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800229 mn->ops->invalidate_range(mn, range->mm,
230 range->start,
231 range->end);
Daniel Vetterba170f72019-08-26 22:14:24 +0200232 if (mn->ops->invalidate_range_end) {
233 if (!mmu_notifier_range_blockable(range))
234 non_block_start();
Jérôme Glisse5d6527a2018-12-28 00:38:05 -0800235 mn->ops->invalidate_range_end(mn, range);
Daniel Vetterba170f72019-08-26 22:14:24 +0200236 if (!mmu_notifier_range_blockable(range))
237 non_block_end();
238 }
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700239 }
Sagi Grimberg21a92732012-10-08 16:29:24 -0700240 srcu_read_unlock(&srcu, id);
Daniel Vetter23b68392019-08-26 22:14:21 +0200241 lock_map_release(&__mmu_notifier_invalidate_range_start_map);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700242}
243
Joerg Roedel0f0a3272014-11-13 13:46:09 +1100244void __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 Roedel0f0a3272014-11-13 13:46:09 +1100257
Jason Gunthorpe56c571032019-08-06 20:15:38 -0300258/*
259 * Same as mmu_notifier_register but here the caller must hold the
260 * mmap_sem in write mode.
261 */
262int __mmu_notifier_register(struct mmu_notifier *mn, struct mm_struct *mm)
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700263{
Jason Gunthorpe70df2912019-08-06 20:15:39 -0300264 struct mmu_notifier_mm *mmu_notifier_mm = NULL;
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700265 int ret;
266
Jason Gunthorpe56c571032019-08-06 20:15:38 -0300267 lockdep_assert_held_write(&mm->mmap_sem);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700268 BUG_ON(atomic_read(&mm->mm_users) <= 0);
269
Daniel Vetter66204f12019-08-26 22:14:22 +0200270 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 Gunthorpe2c7933f2019-08-06 20:15:40 -0300277 mn->mm = mm;
278 mn->users = 1;
279
Jason Gunthorpe70df2912019-08-06 20:15:39 -0300280 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 Shan35cfa2b2012-10-25 13:38:01 -0700294
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700295 ret = mm_take_all_locks(mm);
296 if (unlikely(ret))
Gavin Shan35cfa2b2012-10-25 13:38:01 -0700297 goto out_clean;
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700298
Jason Gunthorpe70df2912019-08-06 20:15:39 -0300299 /* Pairs with the mmdrop in mmu_notifier_unregister_* */
Vegard Nossumf1f10072017-02-27 14:30:07 -0800300 mmgrab(mm);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700301
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 Gunthorpe70df2912019-08-06 20:15:39 -0300310 if (mmu_notifier_mm)
311 mm->mmu_notifier_mm = mmu_notifier_mm;
312
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700313 spin_lock(&mm->mmu_notifier_mm->lock);
Jean-Philippe Brucker543bdb22019-07-11 20:58:50 -0700314 hlist_add_head_rcu(&mn->hlist, &mm->mmu_notifier_mm->list);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700315 spin_unlock(&mm->mmu_notifier_mm->lock);
316
317 mm_drop_all_locks(mm);
Jason Gunthorpe70df2912019-08-06 20:15:39 -0300318 BUG_ON(atomic_read(&mm->mm_users) <= 0);
319 return 0;
320
Gavin Shan35cfa2b2012-10-25 13:38:01 -0700321out_clean:
Gavin Shan35cfa2b2012-10-25 13:38:01 -0700322 kfree(mmu_notifier_mm);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700323 return ret;
324}
Jason Gunthorpe56c571032019-08-06 20:15:38 -0300325EXPORT_SYMBOL_GPL(__mmu_notifier_register);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700326
Jason Gunthorpe2c7933f2019-08-06 20:15:40 -0300327/**
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 Arcangelicddb8a52008-07-28 15:46:29 -0700332 * 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 Gunthorpe2c7933f2019-08-06 20:15:40 -0300338 * 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 Arcangelicddb8a52008-07-28 15:46:29 -0700345 */
346int mmu_notifier_register(struct mmu_notifier *mn, struct mm_struct *mm)
347{
Jason Gunthorpe56c571032019-08-06 20:15:38 -0300348 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 Arcangelicddb8a52008-07-28 15:46:29 -0700354}
355EXPORT_SYMBOL_GPL(mmu_notifier_register);
356
Jason Gunthorpe2c7933f2019-08-06 20:15:40 -0300357static struct mmu_notifier *
358find_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 */
395struct 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;
417out_free:
418 mn->ops->free_notifier(mn);
419 return ERR_PTR(ret);
420}
421EXPORT_SYMBOL_GPL(mmu_notifier_get_locked);
422
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700423/* this is called after the last mmu_notifier_unregister() returned */
424void __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 Grimberg21a92732012-10-08 16:29:24 -0700434 * running mmu notifiers with SRCU and against mmu_notifier_unregister
435 * with the unregister lock + SRCU. All sptes must be dropped before
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700436 * 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 */
441void mmu_notifier_unregister(struct mmu_notifier *mn, struct mm_struct *mm)
442{
443 BUG_ON(atomic_read(&mm->mm_count) <= 0);
444
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700445 if (!hlist_unhashed(&mn->hlist)) {
Xiao Guangrongd34883d2013-05-24 15:55:11 -0700446 /*
447 * SRCU here will force exit_mmap to wait for ->release to
448 * finish before freeing the pages.
449 */
Sagi Grimberg21a92732012-10-08 16:29:24 -0700450 int id;
Xiao Guangrong3ad3d902012-07-31 16:45:52 -0700451
Robin Holt751efd82013-02-22 16:35:34 -0800452 id = srcu_read_lock(&srcu);
Xiao Guangrongd34883d2013-05-24 15:55:11 -0700453 /*
454 * exit_mmap will block in mmu_notifier_release to guarantee
455 * that ->release is called before freeing the pages.
456 */
Robin Holt751efd82013-02-22 16:35:34 -0800457 if (mn->ops->release)
458 mn->ops->release(mn, mm);
Robin Holt751efd82013-02-22 16:35:34 -0800459 srcu_read_unlock(&srcu, id);
Xiao Guangrongd34883d2013-05-24 15:55:11 -0700460
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 Holt751efd82013-02-22 16:35:34 -0800467 spin_unlock(&mm->mmu_notifier_mm->lock);
Xiao Guangrongd34883d2013-05-24 15:55:11 -0700468 }
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700469
470 /*
Xiao Guangrongd34883d2013-05-24 15:55:11 -0700471 * Wait for any running method to finish, of course including
Geert Uytterhoeven83a35e32013-06-28 11:27:31 +0200472 * ->release if it was run by mmu_notifier_release instead of us.
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700473 */
Sagi Grimberg21a92732012-10-08 16:29:24 -0700474 synchronize_srcu(&srcu);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700475
476 BUG_ON(atomic_read(&mm->mm_count) <= 0);
477
478 mmdrop(mm);
479}
480EXPORT_SYMBOL_GPL(mmu_notifier_unregister);
Sagi Grimberg21a92732012-10-08 16:29:24 -0700481
Jason Gunthorpe2c7933f2019-08-06 20:15:40 -0300482static 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 */
514void 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
527out_unlock:
528 spin_unlock(&mm->mmu_notifier_mm->lock);
529}
530EXPORT_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 */
545void mmu_notifier_synchronize(void)
546{
547 synchronize_srcu(&srcu);
548}
549EXPORT_SYMBOL_GPL(mmu_notifier_synchronize);
550
Jérôme Glissec6d23412019-05-13 17:21:00 -0700551bool
552mmu_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}
559EXPORT_SYMBOL_GPL(mmu_notifier_range_update_to_read_only);