blob: 31aa971315a142f3720d2fe90d5260269c9e70d8 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07002#ifndef _LINUX_MMU_NOTIFIER_H
3#define _LINUX_MMU_NOTIFIER_H
4
5#include <linux/list.h>
6#include <linux/spinlock.h>
7#include <linux/mm_types.h>
Sagi Grimberg21a92732012-10-08 16:29:24 -07008#include <linux/srcu.h>
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07009
10struct mmu_notifier;
11struct mmu_notifier_ops;
12
Jérôme Glissed87f0552019-05-13 17:20:45 -070013/**
14 * enum mmu_notifier_event - reason for the mmu notifier callback
15 * @MMU_NOTIFY_UNMAP: either munmap() that unmap the range or a mremap() that
16 * move the range
17 *
18 * @MMU_NOTIFY_CLEAR: clear page table entry (many reasons for this like
19 * madvise() or replacing a page by another one, ...).
20 *
21 * @MMU_NOTIFY_PROTECTION_VMA: update is due to protection change for the range
22 * ie using the vma access permission (vm_page_prot) to update the whole range
23 * is enough no need to inspect changes to the CPU page table (mprotect()
24 * syscall)
25 *
26 * @MMU_NOTIFY_PROTECTION_PAGE: update is due to change in read/write flag for
27 * pages in the range so to mirror those changes the user must inspect the CPU
28 * page table (from the end callback).
29 *
30 * @MMU_NOTIFY_SOFT_DIRTY: soft dirty accounting (still same page and same
31 * access flags). User should soft dirty the page in the end callback to make
32 * sure that anyone relying on soft dirtyness catch pages that might be written
33 * through non CPU mappings.
34 */
35enum mmu_notifier_event {
36 MMU_NOTIFY_UNMAP = 0,
37 MMU_NOTIFY_CLEAR,
38 MMU_NOTIFY_PROTECTION_VMA,
39 MMU_NOTIFY_PROTECTION_PAGE,
40 MMU_NOTIFY_SOFT_DIRTY,
41};
42
Andrea Arcangelicddb8a52008-07-28 15:46:29 -070043#ifdef CONFIG_MMU_NOTIFIER
44
45/*
46 * The mmu notifier_mm structure is allocated and installed in
47 * mm->mmu_notifier_mm inside the mm_take_all_locks() protected
48 * critical section and it's released only when mm_count reaches zero
49 * in mmdrop().
50 */
51struct mmu_notifier_mm {
52 /* all mmu notifiers registerd in this mm are queued in this list */
53 struct hlist_head list;
54 /* to serialize the list modifications and hlist_unhashed */
55 spinlock_t lock;
56};
57
Jérôme Glisse27560ee2019-05-13 17:20:42 -070058#define MMU_NOTIFIER_RANGE_BLOCKABLE (1 << 0)
59
Jérôme Glisse5d6527a2018-12-28 00:38:05 -080060struct mmu_notifier_range {
Jérôme Glissebf198b22019-05-13 17:20:57 -070061 struct vm_area_struct *vma;
Jérôme Glisse5d6527a2018-12-28 00:38:05 -080062 struct mm_struct *mm;
63 unsigned long start;
64 unsigned long end;
Jérôme Glisse27560ee2019-05-13 17:20:42 -070065 unsigned flags;
Jérôme Glissebf198b22019-05-13 17:20:57 -070066 enum mmu_notifier_event event;
Jérôme Glisse5d6527a2018-12-28 00:38:05 -080067};
68
Andrea Arcangelicddb8a52008-07-28 15:46:29 -070069struct mmu_notifier_ops {
70 /*
71 * Called either by mmu_notifier_unregister or when the mm is
72 * being destroyed by exit_mmap, always before all pages are
73 * freed. This can run concurrently with other mmu notifier
74 * methods (the ones invoked outside the mm context) and it
75 * should tear down all secondary mmu mappings and freeze the
76 * secondary mmu. If this method isn't implemented you've to
77 * be sure that nothing could possibly write to the pages
78 * through the secondary mmu by the time the last thread with
79 * tsk->mm == mm exits.
80 *
81 * As side note: the pages freed after ->release returns could
82 * be immediately reallocated by the gart at an alias physical
83 * address with a different cache model, so if ->release isn't
84 * implemented because all _software_ driven memory accesses
85 * through the secondary mmu are terminated by the time the
86 * last thread of this mm quits, you've also to be sure that
87 * speculative _hardware_ operations can't allocate dirty
88 * cachelines in the cpu that could not be snooped and made
89 * coherent with the other read and write operations happening
90 * through the gart alias address, so leading to memory
91 * corruption.
92 */
93 void (*release)(struct mmu_notifier *mn,
94 struct mm_struct *mm);
95
96 /*
97 * clear_flush_young is called after the VM is
98 * test-and-clearing the young/accessed bitflag in the
99 * pte. This way the VM will provide proper aging to the
100 * accesses to the page through the secondary MMUs and not
101 * only to the ones through the Linux pte.
Andres Lagar-Cavilla57128462014-09-22 14:54:42 -0700102 * Start-end is necessary in case the secondary MMU is mapping the page
103 * at a smaller granularity than the primary MMU.
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700104 */
105 int (*clear_flush_young)(struct mmu_notifier *mn,
106 struct mm_struct *mm,
Andres Lagar-Cavilla57128462014-09-22 14:54:42 -0700107 unsigned long start,
108 unsigned long end);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700109
110 /*
Vladimir Davydov1d7715c2015-09-09 15:35:41 -0700111 * clear_young is a lightweight version of clear_flush_young. Like the
112 * latter, it is supposed to test-and-clear the young/accessed bitflag
113 * in the secondary pte, but it may omit flushing the secondary tlb.
114 */
115 int (*clear_young)(struct mmu_notifier *mn,
116 struct mm_struct *mm,
117 unsigned long start,
118 unsigned long end);
119
120 /*
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800121 * test_young is called to check the young/accessed bitflag in
122 * the secondary pte. This is used to know if the page is
123 * frequently used without actually clearing the flag or tearing
124 * down the secondary mapping on the page.
125 */
126 int (*test_young)(struct mmu_notifier *mn,
127 struct mm_struct *mm,
128 unsigned long address);
129
130 /*
Izik Eidus828502d2009-09-21 17:01:51 -0700131 * change_pte is called in cases that pte mapping to page is changed:
132 * for example, when ksm remaps pte to point to a new shared page.
133 */
134 void (*change_pte)(struct mmu_notifier *mn,
135 struct mm_struct *mm,
136 unsigned long address,
137 pte_t pte);
138
139 /*
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700140 * invalidate_range_start() and invalidate_range_end() must be
141 * paired and are called only when the mmap_sem and/or the
Joerg Roedel0f0a3272014-11-13 13:46:09 +1100142 * locks protecting the reverse maps are held. If the subsystem
143 * can't guarantee that no additional references are taken to
144 * the pages in the range, it has to implement the
145 * invalidate_range() notifier to remove any references taken
146 * after invalidate_range_start().
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700147 *
148 * Invalidation of multiple concurrent ranges may be
149 * optionally permitted by the driver. Either way the
150 * establishment of sptes is forbidden in the range passed to
151 * invalidate_range_begin/end for the whole duration of the
152 * invalidate_range_begin/end critical section.
153 *
154 * invalidate_range_start() is called when all pages in the
155 * range are still mapped and have at least a refcount of one.
156 *
157 * invalidate_range_end() is called when all pages in the
158 * range have been unmapped and the pages have been freed by
159 * the VM.
160 *
161 * The VM will remove the page table entries and potentially
162 * the page between invalidate_range_start() and
163 * invalidate_range_end(). If the page must not be freed
164 * because of pending I/O or other circumstances then the
165 * invalidate_range_start() callback (or the initial mapping
166 * by the driver) must make sure that the refcount is kept
167 * elevated.
168 *
169 * If the driver increases the refcount when the pages are
170 * initially mapped into an address space then either
171 * invalidate_range_start() or invalidate_range_end() may
172 * decrease the refcount. If the refcount is decreased on
173 * invalidate_range_start() then the VM can free pages as page
174 * table entries are removed. If the refcount is only
175 * droppped on invalidate_range_end() then the driver itself
176 * will drop the last refcount but it must take care to flush
177 * any secondary tlb before doing the final free on the
178 * page. Pages will no longer be referenced by the linux
179 * address space but may still be referenced by sptes until
180 * the last refcount is dropped.
David Rientjes5ff70912018-01-31 16:18:32 -0800181 *
Michal Hocko93065ac2018-08-21 21:52:33 -0700182 * If blockable argument is set to false then the callback cannot
183 * sleep and has to return with -EAGAIN. 0 should be returned
Michal Hocko33490af2018-10-26 15:03:35 -0700184 * otherwise. Please note that if invalidate_range_start approves
185 * a non-blocking behavior then the same applies to
186 * invalidate_range_end.
Michal Hocko93065ac2018-08-21 21:52:33 -0700187 *
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700188 */
Michal Hocko93065ac2018-08-21 21:52:33 -0700189 int (*invalidate_range_start)(struct mmu_notifier *mn,
Jérôme Glisse5d6527a2018-12-28 00:38:05 -0800190 const struct mmu_notifier_range *range);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700191 void (*invalidate_range_end)(struct mmu_notifier *mn,
Jérôme Glisse5d6527a2018-12-28 00:38:05 -0800192 const struct mmu_notifier_range *range);
Joerg Roedel0f0a3272014-11-13 13:46:09 +1100193
194 /*
195 * invalidate_range() is either called between
196 * invalidate_range_start() and invalidate_range_end() when the
197 * VM has to free pages that where unmapped, but before the
198 * pages are actually freed, or outside of _start()/_end() when
199 * a (remote) TLB is necessary.
200 *
201 * If invalidate_range() is used to manage a non-CPU TLB with
202 * shared page-tables, it not necessary to implement the
203 * invalidate_range_start()/end() notifiers, as
204 * invalidate_range() alread catches the points in time when an
Jérôme Glisse0f108512017-11-15 17:34:07 -0800205 * external TLB range needs to be flushed. For more in depth
Mike Rapoportad56b732018-03-21 21:22:47 +0200206 * discussion on this see Documentation/vm/mmu_notifier.rst
Joerg Roedel0f0a3272014-11-13 13:46:09 +1100207 *
Joerg Roedel0f0a3272014-11-13 13:46:09 +1100208 * Note that this function might be called with just a sub-range
209 * of what was passed to invalidate_range_start()/end(), if
210 * called between those functions.
211 */
212 void (*invalidate_range)(struct mmu_notifier *mn, struct mm_struct *mm,
213 unsigned long start, unsigned long end);
Jason Gunthorpe2c7933f2019-08-06 20:15:40 -0300214
215 /*
216 * These callbacks are used with the get/put interface to manage the
217 * lifetime of the mmu_notifier memory. alloc_notifier() returns a new
218 * notifier for use with the mm.
219 *
220 * free_notifier() is only called after the mmu_notifier has been
221 * fully put, calls to any ops callback are prevented and no ops
222 * callbacks are currently running. It is called from a SRCU callback
223 * and cannot sleep.
224 */
225 struct mmu_notifier *(*alloc_notifier)(struct mm_struct *mm);
226 void (*free_notifier)(struct mmu_notifier *mn);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700227};
228
229/*
230 * The notifier chains are protected by mmap_sem and/or the reverse map
231 * semaphores. Notifier chains are only changed when all reverse maps and
232 * the mmap_sem locks are taken.
233 *
234 * Therefore notifier chains can only be traversed when either
235 *
236 * 1. mmap_sem is held.
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -0800237 * 2. One of the reverse map locks is held (i_mmap_rwsem or anon_vma->rwsem).
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700238 * 3. No other concurrent thread can access the list (release)
239 */
240struct mmu_notifier {
241 struct hlist_node hlist;
242 const struct mmu_notifier_ops *ops;
Jason Gunthorpe2c7933f2019-08-06 20:15:40 -0300243 struct mm_struct *mm;
244 struct rcu_head rcu;
245 unsigned int users;
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700246};
247
248static inline int mm_has_notifiers(struct mm_struct *mm)
249{
250 return unlikely(mm->mmu_notifier_mm);
251}
252
Jason Gunthorpe2c7933f2019-08-06 20:15:40 -0300253struct mmu_notifier *mmu_notifier_get_locked(const struct mmu_notifier_ops *ops,
254 struct mm_struct *mm);
255static inline struct mmu_notifier *
256mmu_notifier_get(const struct mmu_notifier_ops *ops, struct mm_struct *mm)
257{
258 struct mmu_notifier *ret;
259
260 down_write(&mm->mmap_sem);
261 ret = mmu_notifier_get_locked(ops, mm);
262 up_write(&mm->mmap_sem);
263 return ret;
264}
265void mmu_notifier_put(struct mmu_notifier *mn);
266void mmu_notifier_synchronize(void);
267
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700268extern int mmu_notifier_register(struct mmu_notifier *mn,
269 struct mm_struct *mm);
270extern int __mmu_notifier_register(struct mmu_notifier *mn,
271 struct mm_struct *mm);
272extern void mmu_notifier_unregister(struct mmu_notifier *mn,
273 struct mm_struct *mm);
Peter Zijlstrab9722162014-08-06 16:08:20 -0700274extern void mmu_notifier_unregister_no_release(struct mmu_notifier *mn,
275 struct mm_struct *mm);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700276extern void __mmu_notifier_mm_destroy(struct mm_struct *mm);
277extern void __mmu_notifier_release(struct mm_struct *mm);
278extern int __mmu_notifier_clear_flush_young(struct mm_struct *mm,
Andres Lagar-Cavilla57128462014-09-22 14:54:42 -0700279 unsigned long start,
280 unsigned long end);
Vladimir Davydov1d7715c2015-09-09 15:35:41 -0700281extern int __mmu_notifier_clear_young(struct mm_struct *mm,
282 unsigned long start,
283 unsigned long end);
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800284extern int __mmu_notifier_test_young(struct mm_struct *mm,
285 unsigned long address);
Izik Eidus828502d2009-09-21 17:01:51 -0700286extern void __mmu_notifier_change_pte(struct mm_struct *mm,
287 unsigned long address, pte_t pte);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800288extern int __mmu_notifier_invalidate_range_start(struct mmu_notifier_range *r);
289extern void __mmu_notifier_invalidate_range_end(struct mmu_notifier_range *r,
Jérôme Glisse4645b9f2017-11-15 17:34:11 -0800290 bool only_end);
Joerg Roedel0f0a3272014-11-13 13:46:09 +1100291extern void __mmu_notifier_invalidate_range(struct mm_struct *mm,
292 unsigned long start, unsigned long end);
Jérôme Glissec6d23412019-05-13 17:21:00 -0700293extern bool
294mmu_notifier_range_update_to_read_only(const struct mmu_notifier_range *range);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700295
Jérôme Glisse4a83bfe2019-05-13 17:20:34 -0700296static inline bool
297mmu_notifier_range_blockable(const struct mmu_notifier_range *range)
298{
Jérôme Glisse27560ee2019-05-13 17:20:42 -0700299 return (range->flags & MMU_NOTIFIER_RANGE_BLOCKABLE);
Jérôme Glisse4a83bfe2019-05-13 17:20:34 -0700300}
301
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700302static inline void mmu_notifier_release(struct mm_struct *mm)
303{
304 if (mm_has_notifiers(mm))
305 __mmu_notifier_release(mm);
306}
307
308static inline int mmu_notifier_clear_flush_young(struct mm_struct *mm,
Andres Lagar-Cavilla57128462014-09-22 14:54:42 -0700309 unsigned long start,
310 unsigned long end)
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700311{
312 if (mm_has_notifiers(mm))
Andres Lagar-Cavilla57128462014-09-22 14:54:42 -0700313 return __mmu_notifier_clear_flush_young(mm, start, end);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700314 return 0;
315}
316
Vladimir Davydov1d7715c2015-09-09 15:35:41 -0700317static inline int mmu_notifier_clear_young(struct mm_struct *mm,
318 unsigned long start,
319 unsigned long end)
320{
321 if (mm_has_notifiers(mm))
322 return __mmu_notifier_clear_young(mm, start, end);
323 return 0;
324}
325
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800326static inline int mmu_notifier_test_young(struct mm_struct *mm,
327 unsigned long address)
328{
329 if (mm_has_notifiers(mm))
330 return __mmu_notifier_test_young(mm, address);
331 return 0;
332}
333
Izik Eidus828502d2009-09-21 17:01:51 -0700334static inline void mmu_notifier_change_pte(struct mm_struct *mm,
335 unsigned long address, pte_t pte)
336{
337 if (mm_has_notifiers(mm))
338 __mmu_notifier_change_pte(mm, address, pte);
339}
340
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800341static inline void
342mmu_notifier_invalidate_range_start(struct mmu_notifier_range *range)
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700343{
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800344 if (mm_has_notifiers(range->mm)) {
Jérôme Glisse27560ee2019-05-13 17:20:42 -0700345 range->flags |= MMU_NOTIFIER_RANGE_BLOCKABLE;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800346 __mmu_notifier_invalidate_range_start(range);
347 }
Michal Hocko93065ac2018-08-21 21:52:33 -0700348}
349
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800350static inline int
351mmu_notifier_invalidate_range_start_nonblock(struct mmu_notifier_range *range)
Michal Hocko93065ac2018-08-21 21:52:33 -0700352{
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800353 if (mm_has_notifiers(range->mm)) {
Jérôme Glisse27560ee2019-05-13 17:20:42 -0700354 range->flags &= ~MMU_NOTIFIER_RANGE_BLOCKABLE;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800355 return __mmu_notifier_invalidate_range_start(range);
356 }
Michal Hocko93065ac2018-08-21 21:52:33 -0700357 return 0;
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700358}
359
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800360static inline void
361mmu_notifier_invalidate_range_end(struct mmu_notifier_range *range)
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700362{
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800363 if (mm_has_notifiers(range->mm))
364 __mmu_notifier_invalidate_range_end(range, false);
Jérôme Glisse4645b9f2017-11-15 17:34:11 -0800365}
366
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800367static inline void
368mmu_notifier_invalidate_range_only_end(struct mmu_notifier_range *range)
Jérôme Glisse4645b9f2017-11-15 17:34:11 -0800369{
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800370 if (mm_has_notifiers(range->mm))
371 __mmu_notifier_invalidate_range_end(range, true);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700372}
373
Joerg Roedel1897bdc2014-11-13 13:46:09 +1100374static inline void mmu_notifier_invalidate_range(struct mm_struct *mm,
375 unsigned long start, unsigned long end)
376{
Joerg Roedel0f0a3272014-11-13 13:46:09 +1100377 if (mm_has_notifiers(mm))
378 __mmu_notifier_invalidate_range(mm, start, end);
Joerg Roedel1897bdc2014-11-13 13:46:09 +1100379}
380
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700381static inline void mmu_notifier_mm_init(struct mm_struct *mm)
382{
383 mm->mmu_notifier_mm = NULL;
384}
385
386static inline void mmu_notifier_mm_destroy(struct mm_struct *mm)
387{
388 if (mm_has_notifiers(mm))
389 __mmu_notifier_mm_destroy(mm);
390}
391
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800392
393static inline void mmu_notifier_range_init(struct mmu_notifier_range *range,
Jérôme Glisse6f4f13e2019-05-13 17:20:49 -0700394 enum mmu_notifier_event event,
395 unsigned flags,
396 struct vm_area_struct *vma,
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800397 struct mm_struct *mm,
398 unsigned long start,
399 unsigned long end)
400{
Jérôme Glissebf198b22019-05-13 17:20:57 -0700401 range->vma = vma;
402 range->event = event;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800403 range->mm = mm;
404 range->start = start;
405 range->end = end;
Jérôme Glissebf198b22019-05-13 17:20:57 -0700406 range->flags = flags;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800407}
408
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700409#define ptep_clear_flush_young_notify(__vma, __address, __ptep) \
410({ \
411 int __young; \
412 struct vm_area_struct *___vma = __vma; \
413 unsigned long ___address = __address; \
414 __young = ptep_clear_flush_young(___vma, ___address, __ptep); \
415 __young |= mmu_notifier_clear_flush_young(___vma->vm_mm, \
Andres Lagar-Cavilla57128462014-09-22 14:54:42 -0700416 ___address, \
417 ___address + \
418 PAGE_SIZE); \
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700419 __young; \
420})
421
Andrea Arcangeli91a4ee22011-01-13 15:46:44 -0800422#define pmdp_clear_flush_young_notify(__vma, __address, __pmdp) \
423({ \
424 int __young; \
425 struct vm_area_struct *___vma = __vma; \
426 unsigned long ___address = __address; \
427 __young = pmdp_clear_flush_young(___vma, ___address, __pmdp); \
428 __young |= mmu_notifier_clear_flush_young(___vma->vm_mm, \
Andres Lagar-Cavilla57128462014-09-22 14:54:42 -0700429 ___address, \
430 ___address + \
431 PMD_SIZE); \
Andrea Arcangeli91a4ee22011-01-13 15:46:44 -0800432 __young; \
433})
434
Vladimir Davydov1d7715c2015-09-09 15:35:41 -0700435#define ptep_clear_young_notify(__vma, __address, __ptep) \
436({ \
437 int __young; \
438 struct vm_area_struct *___vma = __vma; \
439 unsigned long ___address = __address; \
440 __young = ptep_test_and_clear_young(___vma, ___address, __ptep);\
441 __young |= mmu_notifier_clear_young(___vma->vm_mm, ___address, \
442 ___address + PAGE_SIZE); \
443 __young; \
444})
445
446#define pmdp_clear_young_notify(__vma, __address, __pmdp) \
447({ \
448 int __young; \
449 struct vm_area_struct *___vma = __vma; \
450 unsigned long ___address = __address; \
451 __young = pmdp_test_and_clear_young(___vma, ___address, __pmdp);\
452 __young |= mmu_notifier_clear_young(___vma->vm_mm, ___address, \
453 ___address + PMD_SIZE); \
454 __young; \
455})
456
Joerg Roedel34ee6452014-11-13 13:46:09 +1100457#define ptep_clear_flush_notify(__vma, __address, __ptep) \
458({ \
459 unsigned long ___addr = __address & PAGE_MASK; \
460 struct mm_struct *___mm = (__vma)->vm_mm; \
461 pte_t ___pte; \
462 \
463 ___pte = ptep_clear_flush(__vma, __address, __ptep); \
464 mmu_notifier_invalidate_range(___mm, ___addr, \
465 ___addr + PAGE_SIZE); \
466 \
467 ___pte; \
468})
469
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -0700470#define pmdp_huge_clear_flush_notify(__vma, __haddr, __pmd) \
Joerg Roedel34ee6452014-11-13 13:46:09 +1100471({ \
472 unsigned long ___haddr = __haddr & HPAGE_PMD_MASK; \
473 struct mm_struct *___mm = (__vma)->vm_mm; \
474 pmd_t ___pmd; \
475 \
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -0700476 ___pmd = pmdp_huge_clear_flush(__vma, __haddr, __pmd); \
Joerg Roedel34ee6452014-11-13 13:46:09 +1100477 mmu_notifier_invalidate_range(___mm, ___haddr, \
478 ___haddr + HPAGE_PMD_SIZE); \
479 \
480 ___pmd; \
481})
482
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800483#define pudp_huge_clear_flush_notify(__vma, __haddr, __pud) \
484({ \
485 unsigned long ___haddr = __haddr & HPAGE_PUD_MASK; \
486 struct mm_struct *___mm = (__vma)->vm_mm; \
487 pud_t ___pud; \
488 \
489 ___pud = pudp_huge_clear_flush(__vma, __haddr, __pud); \
490 mmu_notifier_invalidate_range(___mm, ___haddr, \
491 ___haddr + HPAGE_PUD_SIZE); \
492 \
493 ___pud; \
494})
495
Xiao Guangrong48af0d72012-10-08 16:29:23 -0700496/*
497 * set_pte_at_notify() sets the pte _after_ running the notifier.
498 * This is safe to start by updating the secondary MMUs, because the primary MMU
499 * pte invalidate must have already happened with a ptep_clear_flush() before
500 * set_pte_at_notify() has been invoked. Updating the secondary MMUs first is
501 * required when we change both the protection of the mapping from read-only to
502 * read-write and the pfn (like during copy on write page faults). Otherwise the
503 * old page would remain mapped readonly in the secondary MMUs after the new
504 * page is already writable by some CPU through the primary MMU.
505 */
Izik Eidus828502d2009-09-21 17:01:51 -0700506#define set_pte_at_notify(__mm, __address, __ptep, __pte) \
507({ \
508 struct mm_struct *___mm = __mm; \
509 unsigned long ___address = __address; \
510 pte_t ___pte = __pte; \
511 \
Izik Eidus828502d2009-09-21 17:01:51 -0700512 mmu_notifier_change_pte(___mm, ___address, ___pte); \
Xiao Guangrong48af0d72012-10-08 16:29:23 -0700513 set_pte_at(___mm, ___address, __ptep, ___pte); \
Izik Eidus828502d2009-09-21 17:01:51 -0700514})
515
Peter Zijlstrab9722162014-08-06 16:08:20 -0700516extern void mmu_notifier_call_srcu(struct rcu_head *rcu,
517 void (*func)(struct rcu_head *rcu));
Peter Zijlstrab9722162014-08-06 16:08:20 -0700518
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700519#else /* CONFIG_MMU_NOTIFIER */
520
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800521struct mmu_notifier_range {
522 unsigned long start;
523 unsigned long end;
524};
525
526static inline void _mmu_notifier_range_init(struct mmu_notifier_range *range,
527 unsigned long start,
528 unsigned long end)
529{
530 range->start = start;
531 range->end = end;
532}
533
Jérôme Glisse6f4f13e2019-05-13 17:20:49 -0700534#define mmu_notifier_range_init(range,event,flags,vma,mm,start,end) \
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800535 _mmu_notifier_range_init(range, start, end)
536
Jérôme Glisse4a83bfe2019-05-13 17:20:34 -0700537static inline bool
538mmu_notifier_range_blockable(const struct mmu_notifier_range *range)
539{
540 return true;
541}
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800542
Michal Hocko4d4bbd82017-10-03 16:14:50 -0700543static inline int mm_has_notifiers(struct mm_struct *mm)
544{
545 return 0;
546}
547
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700548static inline void mmu_notifier_release(struct mm_struct *mm)
549{
550}
551
552static inline int mmu_notifier_clear_flush_young(struct mm_struct *mm,
Andres Lagar-Cavilla57128462014-09-22 14:54:42 -0700553 unsigned long start,
554 unsigned long end)
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700555{
556 return 0;
557}
558
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800559static inline int mmu_notifier_test_young(struct mm_struct *mm,
560 unsigned long address)
561{
562 return 0;
563}
564
Izik Eidus828502d2009-09-21 17:01:51 -0700565static inline void mmu_notifier_change_pte(struct mm_struct *mm,
566 unsigned long address, pte_t pte)
567{
568}
569
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800570static inline void
571mmu_notifier_invalidate_range_start(struct mmu_notifier_range *range)
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700572{
573}
574
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800575static inline int
576mmu_notifier_invalidate_range_start_nonblock(struct mmu_notifier_range *range)
Michal Hocko93065ac2018-08-21 21:52:33 -0700577{
578 return 0;
579}
580
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800581static inline
582void mmu_notifier_invalidate_range_end(struct mmu_notifier_range *range)
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700583{
584}
585
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800586static inline void
587mmu_notifier_invalidate_range_only_end(struct mmu_notifier_range *range)
Jérôme Glisse4645b9f2017-11-15 17:34:11 -0800588{
589}
590
Joerg Roedel1897bdc2014-11-13 13:46:09 +1100591static inline void mmu_notifier_invalidate_range(struct mm_struct *mm,
592 unsigned long start, unsigned long end)
593{
594}
595
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700596static inline void mmu_notifier_mm_init(struct mm_struct *mm)
597{
598}
599
600static inline void mmu_notifier_mm_destroy(struct mm_struct *mm)
601{
602}
603
Jérôme Glissec6d23412019-05-13 17:21:00 -0700604#define mmu_notifier_range_update_to_read_only(r) false
605
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700606#define ptep_clear_flush_young_notify ptep_clear_flush_young
Andrea Arcangeli91a4ee22011-01-13 15:46:44 -0800607#define pmdp_clear_flush_young_notify pmdp_clear_flush_young
Vladimir Davydov33c3fc72015-09-09 15:35:45 -0700608#define ptep_clear_young_notify ptep_test_and_clear_young
609#define pmdp_clear_young_notify pmdp_test_and_clear_young
Joerg Roedel34ee6452014-11-13 13:46:09 +1100610#define ptep_clear_flush_notify ptep_clear_flush
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -0700611#define pmdp_huge_clear_flush_notify pmdp_huge_clear_flush
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800612#define pudp_huge_clear_flush_notify pudp_huge_clear_flush
Izik Eidus828502d2009-09-21 17:01:51 -0700613#define set_pte_at_notify set_pte_at
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700614
Jason Gunthorpe2c7933f2019-08-06 20:15:40 -0300615static inline void mmu_notifier_synchronize(void)
616{
617}
618
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700619#endif /* CONFIG_MMU_NOTIFIER */
620
621#endif /* _LINUX_MMU_NOTIFIER_H */