Linux Audio

Check our new training course

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