Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
cddb8a5c AA |
2 | #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> | |
b72327fc | 8 | #include <linux/mmap_lock.h> |
21a92735 | 9 | #include <linux/srcu.h> |
99cb252f | 10 | #include <linux/interval_tree.h> |
cddb8a5c | 11 | |
984cfe4e | 12 | struct mmu_notifier_subscriptions; |
cddb8a5c | 13 | struct mmu_notifier; |
56f434f4 | 14 | struct mmu_notifier_range; |
99cb252f | 15 | struct mmu_interval_notifier; |
cddb8a5c | 16 | |
d87f055b JG |
17 | /** |
18 | * enum mmu_notifier_event - reason for the mmu notifier callback | |
19 | * @MMU_NOTIFY_UNMAP: either munmap() that unmap the range or a mremap() that | |
20 | * move the range | |
21 | * | |
22 | * @MMU_NOTIFY_CLEAR: clear page table entry (many reasons for this like | |
23 | * madvise() or replacing a page by another one, ...). | |
24 | * | |
25 | * @MMU_NOTIFY_PROTECTION_VMA: update is due to protection change for the range | |
26 | * ie using the vma access permission (vm_page_prot) to update the whole range | |
27 | * is enough no need to inspect changes to the CPU page table (mprotect() | |
28 | * syscall) | |
29 | * | |
30 | * @MMU_NOTIFY_PROTECTION_PAGE: update is due to change in read/write flag for | |
31 | * pages in the range so to mirror those changes the user must inspect the CPU | |
32 | * page table (from the end callback). | |
33 | * | |
34 | * @MMU_NOTIFY_SOFT_DIRTY: soft dirty accounting (still same page and same | |
35 | * access flags). User should soft dirty the page in the end callback to make | |
06c88398 | 36 | * sure that anyone relying on soft dirtiness catch pages that might be written |
d87f055b | 37 | * through non CPU mappings. |
99cb252f JG |
38 | * |
39 | * @MMU_NOTIFY_RELEASE: used during mmu_interval_notifier invalidate to signal | |
40 | * that the mm refcount is zero and the range is no longer accessible. | |
998427b3 RC |
41 | * |
42 | * @MMU_NOTIFY_MIGRATE: used during migrate_vma_collect() invalidate to signal | |
43 | * a device driver to possibly ignore the invalidation if the | |
6b49bf6d | 44 | * owner field matches the driver's device private pgmap owner. |
b756a3b5 AP |
45 | * |
46 | * @MMU_NOTIFY_EXCLUSIVE: to signal a device driver that the device will no | |
47 | * longer have exclusive access to the page. When sent during creation of an | |
48 | * exclusive range the owner will be initialised to the value provided by the | |
49 | * caller of make_device_exclusive_range(), otherwise the owner will be NULL. | |
d87f055b JG |
50 | */ |
51 | enum mmu_notifier_event { | |
52 | MMU_NOTIFY_UNMAP = 0, | |
53 | MMU_NOTIFY_CLEAR, | |
54 | MMU_NOTIFY_PROTECTION_VMA, | |
55 | MMU_NOTIFY_PROTECTION_PAGE, | |
56 | MMU_NOTIFY_SOFT_DIRTY, | |
99cb252f | 57 | MMU_NOTIFY_RELEASE, |
998427b3 | 58 | MMU_NOTIFY_MIGRATE, |
b756a3b5 | 59 | MMU_NOTIFY_EXCLUSIVE, |
d87f055b JG |
60 | }; |
61 | ||
27560ee9 JG |
62 | #define MMU_NOTIFIER_RANGE_BLOCKABLE (1 << 0) |
63 | ||
cddb8a5c AA |
64 | struct mmu_notifier_ops { |
65 | /* | |
66 | * Called either by mmu_notifier_unregister or when the mm is | |
67 | * being destroyed by exit_mmap, always before all pages are | |
68 | * freed. This can run concurrently with other mmu notifier | |
69 | * methods (the ones invoked outside the mm context) and it | |
70 | * should tear down all secondary mmu mappings and freeze the | |
71 | * secondary mmu. If this method isn't implemented you've to | |
72 | * be sure that nothing could possibly write to the pages | |
73 | * through the secondary mmu by the time the last thread with | |
74 | * tsk->mm == mm exits. | |
75 | * | |
76 | * As side note: the pages freed after ->release returns could | |
77 | * be immediately reallocated by the gart at an alias physical | |
78 | * address with a different cache model, so if ->release isn't | |
79 | * implemented because all _software_ driven memory accesses | |
80 | * through the secondary mmu are terminated by the time the | |
81 | * last thread of this mm quits, you've also to be sure that | |
82 | * speculative _hardware_ operations can't allocate dirty | |
83 | * cachelines in the cpu that could not be snooped and made | |
84 | * coherent with the other read and write operations happening | |
85 | * through the gart alias address, so leading to memory | |
86 | * corruption. | |
87 | */ | |
1991722a | 88 | void (*release)(struct mmu_notifier *subscription, |
cddb8a5c AA |
89 | struct mm_struct *mm); |
90 | ||
91 | /* | |
92 | * clear_flush_young is called after the VM is | |
93 | * test-and-clearing the young/accessed bitflag in the | |
94 | * pte. This way the VM will provide proper aging to the | |
95 | * accesses to the page through the secondary MMUs and not | |
96 | * only to the ones through the Linux pte. | |
57128468 ALC |
97 | * Start-end is necessary in case the secondary MMU is mapping the page |
98 | * at a smaller granularity than the primary MMU. | |
cddb8a5c | 99 | */ |
1991722a | 100 | int (*clear_flush_young)(struct mmu_notifier *subscription, |
cddb8a5c | 101 | struct mm_struct *mm, |
57128468 ALC |
102 | unsigned long start, |
103 | unsigned long end); | |
cddb8a5c | 104 | |
1d7715c6 VD |
105 | /* |
106 | * clear_young is a lightweight version of clear_flush_young. Like the | |
107 | * latter, it is supposed to test-and-clear the young/accessed bitflag | |
108 | * in the secondary pte, but it may omit flushing the secondary tlb. | |
109 | */ | |
1991722a | 110 | int (*clear_young)(struct mmu_notifier *subscription, |
1d7715c6 VD |
111 | struct mm_struct *mm, |
112 | unsigned long start, | |
113 | unsigned long end); | |
114 | ||
8ee53820 AA |
115 | /* |
116 | * test_young is called to check the young/accessed bitflag in | |
117 | * the secondary pte. This is used to know if the page is | |
118 | * frequently used without actually clearing the flag or tearing | |
119 | * down the secondary mapping on the page. | |
120 | */ | |
1991722a | 121 | int (*test_young)(struct mmu_notifier *subscription, |
8ee53820 AA |
122 | struct mm_struct *mm, |
123 | unsigned long address); | |
124 | ||
828502d3 IE |
125 | /* |
126 | * change_pte is called in cases that pte mapping to page is changed: | |
127 | * for example, when ksm remaps pte to point to a new shared page. | |
128 | */ | |
1991722a | 129 | void (*change_pte)(struct mmu_notifier *subscription, |
828502d3 IE |
130 | struct mm_struct *mm, |
131 | unsigned long address, | |
132 | pte_t pte); | |
133 | ||
cddb8a5c AA |
134 | /* |
135 | * invalidate_range_start() and invalidate_range_end() must be | |
c1e8d7c6 | 136 | * paired and are called only when the mmap_lock and/or the |
0f0a327f JR |
137 | * locks protecting the reverse maps are held. If the subsystem |
138 | * can't guarantee that no additional references are taken to | |
139 | * the pages in the range, it has to implement the | |
140 | * invalidate_range() notifier to remove any references taken | |
141 | * after invalidate_range_start(). | |
cddb8a5c AA |
142 | * |
143 | * Invalidation of multiple concurrent ranges may be | |
144 | * optionally permitted by the driver. Either way the | |
145 | * establishment of sptes is forbidden in the range passed to | |
146 | * invalidate_range_begin/end for the whole duration of the | |
147 | * invalidate_range_begin/end critical section. | |
148 | * | |
149 | * invalidate_range_start() is called when all pages in the | |
150 | * range are still mapped and have at least a refcount of one. | |
151 | * | |
152 | * invalidate_range_end() is called when all pages in the | |
153 | * range have been unmapped and the pages have been freed by | |
154 | * the VM. | |
155 | * | |
156 | * The VM will remove the page table entries and potentially | |
157 | * the page between invalidate_range_start() and | |
158 | * invalidate_range_end(). If the page must not be freed | |
159 | * because of pending I/O or other circumstances then the | |
160 | * invalidate_range_start() callback (or the initial mapping | |
161 | * by the driver) must make sure that the refcount is kept | |
162 | * elevated. | |
163 | * | |
164 | * If the driver increases the refcount when the pages are | |
165 | * initially mapped into an address space then either | |
166 | * invalidate_range_start() or invalidate_range_end() may | |
167 | * decrease the refcount. If the refcount is decreased on | |
168 | * invalidate_range_start() then the VM can free pages as page | |
169 | * table entries are removed. If the refcount is only | |
06c88398 | 170 | * dropped on invalidate_range_end() then the driver itself |
cddb8a5c AA |
171 | * will drop the last refcount but it must take care to flush |
172 | * any secondary tlb before doing the final free on the | |
173 | * page. Pages will no longer be referenced by the linux | |
174 | * address space but may still be referenced by sptes until | |
175 | * the last refcount is dropped. | |
5ff7091f | 176 | * |
93065ac7 | 177 | * If blockable argument is set to false then the callback cannot |
c2655835 SC |
178 | * sleep and has to return with -EAGAIN if sleeping would be required. |
179 | * 0 should be returned otherwise. Please note that notifiers that can | |
180 | * fail invalidate_range_start are not allowed to implement | |
181 | * invalidate_range_end, as there is no mechanism for informing the | |
182 | * notifier that its start failed. | |
cddb8a5c | 183 | */ |
1991722a | 184 | int (*invalidate_range_start)(struct mmu_notifier *subscription, |
5d6527a7 | 185 | const struct mmu_notifier_range *range); |
1991722a | 186 | void (*invalidate_range_end)(struct mmu_notifier *subscription, |
5d6527a7 | 187 | const struct mmu_notifier_range *range); |
0f0a327f JR |
188 | |
189 | /* | |
190 | * invalidate_range() is either called between | |
191 | * invalidate_range_start() and invalidate_range_end() when the | |
192 | * VM has to free pages that where unmapped, but before the | |
193 | * pages are actually freed, or outside of _start()/_end() when | |
194 | * a (remote) TLB is necessary. | |
195 | * | |
196 | * If invalidate_range() is used to manage a non-CPU TLB with | |
197 | * shared page-tables, it not necessary to implement the | |
198 | * invalidate_range_start()/end() notifiers, as | |
06c88398 | 199 | * invalidate_range() already catches the points in time when an |
0f10851e | 200 | * external TLB range needs to be flushed. For more in depth |
ee65728e | 201 | * discussion on this see Documentation/mm/mmu_notifier.rst |
0f0a327f | 202 | * |
0f0a327f JR |
203 | * Note that this function might be called with just a sub-range |
204 | * of what was passed to invalidate_range_start()/end(), if | |
205 | * called between those functions. | |
206 | */ | |
1991722a JG |
207 | void (*invalidate_range)(struct mmu_notifier *subscription, |
208 | struct mm_struct *mm, | |
209 | unsigned long start, | |
210 | unsigned long end); | |
2c7933f5 JG |
211 | |
212 | /* | |
213 | * These callbacks are used with the get/put interface to manage the | |
214 | * lifetime of the mmu_notifier memory. alloc_notifier() returns a new | |
215 | * notifier for use with the mm. | |
216 | * | |
217 | * free_notifier() is only called after the mmu_notifier has been | |
218 | * fully put, calls to any ops callback are prevented and no ops | |
219 | * callbacks are currently running. It is called from a SRCU callback | |
220 | * and cannot sleep. | |
221 | */ | |
222 | struct mmu_notifier *(*alloc_notifier)(struct mm_struct *mm); | |
1991722a | 223 | void (*free_notifier)(struct mmu_notifier *subscription); |
cddb8a5c AA |
224 | }; |
225 | ||
226 | /* | |
c1e8d7c6 | 227 | * The notifier chains are protected by mmap_lock and/or the reverse map |
cddb8a5c | 228 | * semaphores. Notifier chains are only changed when all reverse maps and |
c1e8d7c6 | 229 | * the mmap_lock locks are taken. |
cddb8a5c AA |
230 | * |
231 | * Therefore notifier chains can only be traversed when either | |
232 | * | |
c1e8d7c6 | 233 | * 1. mmap_lock is held. |
c8c06efa | 234 | * 2. One of the reverse map locks is held (i_mmap_rwsem or anon_vma->rwsem). |
cddb8a5c AA |
235 | * 3. No other concurrent thread can access the list (release) |
236 | */ | |
237 | struct mmu_notifier { | |
238 | struct hlist_node hlist; | |
239 | const struct mmu_notifier_ops *ops; | |
2c7933f5 JG |
240 | struct mm_struct *mm; |
241 | struct rcu_head rcu; | |
242 | unsigned int users; | |
cddb8a5c AA |
243 | }; |
244 | ||
99cb252f JG |
245 | /** |
246 | * struct mmu_interval_notifier_ops | |
247 | * @invalidate: Upon return the caller must stop using any SPTEs within this | |
248 | * range. This function can sleep. Return false only if sleeping | |
249 | * was required but mmu_notifier_range_blockable(range) is false. | |
250 | */ | |
251 | struct mmu_interval_notifier_ops { | |
5292e24a | 252 | bool (*invalidate)(struct mmu_interval_notifier *interval_sub, |
99cb252f JG |
253 | const struct mmu_notifier_range *range, |
254 | unsigned long cur_seq); | |
255 | }; | |
256 | ||
257 | struct mmu_interval_notifier { | |
258 | struct interval_tree_node interval_tree; | |
259 | const struct mmu_interval_notifier_ops *ops; | |
260 | struct mm_struct *mm; | |
261 | struct hlist_node deferred_item; | |
262 | unsigned long invalidate_seq; | |
263 | }; | |
264 | ||
56f434f4 JG |
265 | #ifdef CONFIG_MMU_NOTIFIER |
266 | ||
267 | #ifdef CONFIG_LOCKDEP | |
268 | extern struct lockdep_map __mmu_notifier_invalidate_range_start_map; | |
269 | #endif | |
270 | ||
271 | struct mmu_notifier_range { | |
56f434f4 JG |
272 | struct mm_struct *mm; |
273 | unsigned long start; | |
274 | unsigned long end; | |
275 | unsigned flags; | |
276 | enum mmu_notifier_event event; | |
6b49bf6d | 277 | void *owner; |
56f434f4 JG |
278 | }; |
279 | ||
cddb8a5c AA |
280 | static inline int mm_has_notifiers(struct mm_struct *mm) |
281 | { | |
984cfe4e | 282 | return unlikely(mm->notifier_subscriptions); |
cddb8a5c AA |
283 | } |
284 | ||
2c7933f5 JG |
285 | struct mmu_notifier *mmu_notifier_get_locked(const struct mmu_notifier_ops *ops, |
286 | struct mm_struct *mm); | |
287 | static inline struct mmu_notifier * | |
288 | mmu_notifier_get(const struct mmu_notifier_ops *ops, struct mm_struct *mm) | |
289 | { | |
290 | struct mmu_notifier *ret; | |
291 | ||
b72327fc | 292 | mmap_write_lock(mm); |
2c7933f5 | 293 | ret = mmu_notifier_get_locked(ops, mm); |
b72327fc | 294 | mmap_write_unlock(mm); |
2c7933f5 JG |
295 | return ret; |
296 | } | |
1991722a | 297 | void mmu_notifier_put(struct mmu_notifier *subscription); |
2c7933f5 JG |
298 | void mmu_notifier_synchronize(void); |
299 | ||
1991722a | 300 | extern int mmu_notifier_register(struct mmu_notifier *subscription, |
cddb8a5c | 301 | struct mm_struct *mm); |
1991722a | 302 | extern int __mmu_notifier_register(struct mmu_notifier *subscription, |
cddb8a5c | 303 | struct mm_struct *mm); |
1991722a | 304 | extern void mmu_notifier_unregister(struct mmu_notifier *subscription, |
cddb8a5c | 305 | struct mm_struct *mm); |
99cb252f | 306 | |
5292e24a JG |
307 | unsigned long |
308 | mmu_interval_read_begin(struct mmu_interval_notifier *interval_sub); | |
309 | int mmu_interval_notifier_insert(struct mmu_interval_notifier *interval_sub, | |
99cb252f JG |
310 | struct mm_struct *mm, unsigned long start, |
311 | unsigned long length, | |
312 | const struct mmu_interval_notifier_ops *ops); | |
313 | int mmu_interval_notifier_insert_locked( | |
5292e24a | 314 | struct mmu_interval_notifier *interval_sub, struct mm_struct *mm, |
99cb252f JG |
315 | unsigned long start, unsigned long length, |
316 | const struct mmu_interval_notifier_ops *ops); | |
5292e24a | 317 | void mmu_interval_notifier_remove(struct mmu_interval_notifier *interval_sub); |
99cb252f JG |
318 | |
319 | /** | |
320 | * mmu_interval_set_seq - Save the invalidation sequence | |
5292e24a | 321 | * @interval_sub - The subscription passed to invalidate |
99cb252f JG |
322 | * @cur_seq - The cur_seq passed to the invalidate() callback |
323 | * | |
324 | * This must be called unconditionally from the invalidate callback of a | |
325 | * struct mmu_interval_notifier_ops under the same lock that is used to call | |
326 | * mmu_interval_read_retry(). It updates the sequence number for later use by | |
327 | * mmu_interval_read_retry(). The provided cur_seq will always be odd. | |
328 | * | |
329 | * If the caller does not call mmu_interval_read_begin() or | |
330 | * mmu_interval_read_retry() then this call is not required. | |
331 | */ | |
5292e24a JG |
332 | static inline void |
333 | mmu_interval_set_seq(struct mmu_interval_notifier *interval_sub, | |
334 | unsigned long cur_seq) | |
99cb252f | 335 | { |
5292e24a | 336 | WRITE_ONCE(interval_sub->invalidate_seq, cur_seq); |
99cb252f JG |
337 | } |
338 | ||
339 | /** | |
340 | * mmu_interval_read_retry - End a read side critical section against a VA range | |
5292e24a | 341 | * interval_sub: The subscription |
99cb252f JG |
342 | * seq: The return of the paired mmu_interval_read_begin() |
343 | * | |
344 | * This MUST be called under a user provided lock that is also held | |
345 | * unconditionally by op->invalidate() when it calls mmu_interval_set_seq(). | |
346 | * | |
347 | * Each call should be paired with a single mmu_interval_read_begin() and | |
348 | * should be used to conclude the read side. | |
349 | * | |
350 | * Returns true if an invalidation collided with this critical section, and | |
351 | * the caller should retry. | |
352 | */ | |
5292e24a JG |
353 | static inline bool |
354 | mmu_interval_read_retry(struct mmu_interval_notifier *interval_sub, | |
355 | unsigned long seq) | |
99cb252f | 356 | { |
5292e24a | 357 | return interval_sub->invalidate_seq != seq; |
99cb252f JG |
358 | } |
359 | ||
360 | /** | |
361 | * mmu_interval_check_retry - Test if a collision has occurred | |
5292e24a | 362 | * interval_sub: The subscription |
99cb252f JG |
363 | * seq: The return of the matching mmu_interval_read_begin() |
364 | * | |
365 | * This can be used in the critical section between mmu_interval_read_begin() | |
366 | * and mmu_interval_read_retry(). A return of true indicates an invalidation | |
367 | * has collided with this critical region and a future | |
368 | * mmu_interval_read_retry() will return true. | |
369 | * | |
370 | * False is not reliable and only suggests a collision may not have | |
06c88398 | 371 | * occurred. It can be called many times and does not have to hold the user |
99cb252f JG |
372 | * provided lock. |
373 | * | |
374 | * This call can be used as part of loops and other expensive operations to | |
375 | * expedite a retry. | |
376 | */ | |
5292e24a JG |
377 | static inline bool |
378 | mmu_interval_check_retry(struct mmu_interval_notifier *interval_sub, | |
379 | unsigned long seq) | |
99cb252f JG |
380 | { |
381 | /* Pairs with the WRITE_ONCE in mmu_interval_set_seq() */ | |
5292e24a | 382 | return READ_ONCE(interval_sub->invalidate_seq) != seq; |
99cb252f JG |
383 | } |
384 | ||
984cfe4e | 385 | extern void __mmu_notifier_subscriptions_destroy(struct mm_struct *mm); |
cddb8a5c AA |
386 | extern void __mmu_notifier_release(struct mm_struct *mm); |
387 | extern int __mmu_notifier_clear_flush_young(struct mm_struct *mm, | |
57128468 ALC |
388 | unsigned long start, |
389 | unsigned long end); | |
1d7715c6 VD |
390 | extern int __mmu_notifier_clear_young(struct mm_struct *mm, |
391 | unsigned long start, | |
392 | unsigned long end); | |
8ee53820 AA |
393 | extern int __mmu_notifier_test_young(struct mm_struct *mm, |
394 | unsigned long address); | |
828502d3 IE |
395 | extern void __mmu_notifier_change_pte(struct mm_struct *mm, |
396 | unsigned long address, pte_t pte); | |
ac46d4f3 JG |
397 | extern int __mmu_notifier_invalidate_range_start(struct mmu_notifier_range *r); |
398 | extern void __mmu_notifier_invalidate_range_end(struct mmu_notifier_range *r, | |
4645b9fe | 399 | bool only_end); |
0f0a327f JR |
400 | extern void __mmu_notifier_invalidate_range(struct mm_struct *mm, |
401 | unsigned long start, unsigned long end); | |
c6d23413 JG |
402 | extern bool |
403 | mmu_notifier_range_update_to_read_only(const struct mmu_notifier_range *range); | |
cddb8a5c | 404 | |
4a83bfe9 JG |
405 | static inline bool |
406 | mmu_notifier_range_blockable(const struct mmu_notifier_range *range) | |
407 | { | |
27560ee9 | 408 | return (range->flags & MMU_NOTIFIER_RANGE_BLOCKABLE); |
4a83bfe9 JG |
409 | } |
410 | ||
cddb8a5c AA |
411 | static inline void mmu_notifier_release(struct mm_struct *mm) |
412 | { | |
413 | if (mm_has_notifiers(mm)) | |
414 | __mmu_notifier_release(mm); | |
415 | } | |
416 | ||
417 | static inline int mmu_notifier_clear_flush_young(struct mm_struct *mm, | |
57128468 ALC |
418 | unsigned long start, |
419 | unsigned long end) | |
cddb8a5c AA |
420 | { |
421 | if (mm_has_notifiers(mm)) | |
57128468 | 422 | return __mmu_notifier_clear_flush_young(mm, start, end); |
cddb8a5c AA |
423 | return 0; |
424 | } | |
425 | ||
1d7715c6 VD |
426 | static inline int mmu_notifier_clear_young(struct mm_struct *mm, |
427 | unsigned long start, | |
428 | unsigned long end) | |
429 | { | |
430 | if (mm_has_notifiers(mm)) | |
431 | return __mmu_notifier_clear_young(mm, start, end); | |
432 | return 0; | |
433 | } | |
434 | ||
8ee53820 AA |
435 | static inline int mmu_notifier_test_young(struct mm_struct *mm, |
436 | unsigned long address) | |
437 | { | |
438 | if (mm_has_notifiers(mm)) | |
439 | return __mmu_notifier_test_young(mm, address); | |
440 | return 0; | |
441 | } | |
442 | ||
828502d3 IE |
443 | static inline void mmu_notifier_change_pte(struct mm_struct *mm, |
444 | unsigned long address, pte_t pte) | |
445 | { | |
446 | if (mm_has_notifiers(mm)) | |
447 | __mmu_notifier_change_pte(mm, address, pte); | |
448 | } | |
449 | ||
ac46d4f3 JG |
450 | static inline void |
451 | mmu_notifier_invalidate_range_start(struct mmu_notifier_range *range) | |
cddb8a5c | 452 | { |
810e24e0 DV |
453 | might_sleep(); |
454 | ||
23b68395 | 455 | lock_map_acquire(&__mmu_notifier_invalidate_range_start_map); |
ac46d4f3 | 456 | if (mm_has_notifiers(range->mm)) { |
27560ee9 | 457 | range->flags |= MMU_NOTIFIER_RANGE_BLOCKABLE; |
ac46d4f3 JG |
458 | __mmu_notifier_invalidate_range_start(range); |
459 | } | |
23b68395 | 460 | lock_map_release(&__mmu_notifier_invalidate_range_start_map); |
93065ac7 MH |
461 | } |
462 | ||
ac46d4f3 JG |
463 | static inline int |
464 | mmu_notifier_invalidate_range_start_nonblock(struct mmu_notifier_range *range) | |
93065ac7 | 465 | { |
23b68395 DV |
466 | int ret = 0; |
467 | ||
468 | lock_map_acquire(&__mmu_notifier_invalidate_range_start_map); | |
ac46d4f3 | 469 | if (mm_has_notifiers(range->mm)) { |
27560ee9 | 470 | range->flags &= ~MMU_NOTIFIER_RANGE_BLOCKABLE; |
23b68395 | 471 | ret = __mmu_notifier_invalidate_range_start(range); |
ac46d4f3 | 472 | } |
23b68395 DV |
473 | lock_map_release(&__mmu_notifier_invalidate_range_start_map); |
474 | return ret; | |
cddb8a5c AA |
475 | } |
476 | ||
ac46d4f3 JG |
477 | static inline void |
478 | mmu_notifier_invalidate_range_end(struct mmu_notifier_range *range) | |
cddb8a5c | 479 | { |
810e24e0 DV |
480 | if (mmu_notifier_range_blockable(range)) |
481 | might_sleep(); | |
482 | ||
ac46d4f3 JG |
483 | if (mm_has_notifiers(range->mm)) |
484 | __mmu_notifier_invalidate_range_end(range, false); | |
4645b9fe JG |
485 | } |
486 | ||
ac46d4f3 JG |
487 | static inline void |
488 | mmu_notifier_invalidate_range_only_end(struct mmu_notifier_range *range) | |
4645b9fe | 489 | { |
ac46d4f3 JG |
490 | if (mm_has_notifiers(range->mm)) |
491 | __mmu_notifier_invalidate_range_end(range, true); | |
cddb8a5c AA |
492 | } |
493 | ||
1897bdc4 JR |
494 | static inline void mmu_notifier_invalidate_range(struct mm_struct *mm, |
495 | unsigned long start, unsigned long end) | |
496 | { | |
0f0a327f JR |
497 | if (mm_has_notifiers(mm)) |
498 | __mmu_notifier_invalidate_range(mm, start, end); | |
1897bdc4 JR |
499 | } |
500 | ||
984cfe4e | 501 | static inline void mmu_notifier_subscriptions_init(struct mm_struct *mm) |
cddb8a5c | 502 | { |
984cfe4e | 503 | mm->notifier_subscriptions = NULL; |
cddb8a5c AA |
504 | } |
505 | ||
984cfe4e | 506 | static inline void mmu_notifier_subscriptions_destroy(struct mm_struct *mm) |
cddb8a5c AA |
507 | { |
508 | if (mm_has_notifiers(mm)) | |
984cfe4e | 509 | __mmu_notifier_subscriptions_destroy(mm); |
cddb8a5c AA |
510 | } |
511 | ||
ac46d4f3 JG |
512 | |
513 | static inline void mmu_notifier_range_init(struct mmu_notifier_range *range, | |
6f4f13e8 JG |
514 | enum mmu_notifier_event event, |
515 | unsigned flags, | |
ac46d4f3 JG |
516 | struct mm_struct *mm, |
517 | unsigned long start, | |
518 | unsigned long end) | |
519 | { | |
bf198b2b | 520 | range->event = event; |
ac46d4f3 JG |
521 | range->mm = mm; |
522 | range->start = start; | |
523 | range->end = end; | |
bf198b2b | 524 | range->flags = flags; |
ac46d4f3 JG |
525 | } |
526 | ||
6b49bf6d AP |
527 | static inline void mmu_notifier_range_init_owner( |
528 | struct mmu_notifier_range *range, | |
529 | enum mmu_notifier_event event, unsigned int flags, | |
7d4a8be0 AP |
530 | struct mm_struct *mm, unsigned long start, |
531 | unsigned long end, void *owner) | |
c1a06df6 | 532 | { |
7d4a8be0 | 533 | mmu_notifier_range_init(range, event, flags, mm, start, end); |
6b49bf6d | 534 | range->owner = owner; |
c1a06df6 RC |
535 | } |
536 | ||
cddb8a5c AA |
537 | #define ptep_clear_flush_young_notify(__vma, __address, __ptep) \ |
538 | ({ \ | |
539 | int __young; \ | |
540 | struct vm_area_struct *___vma = __vma; \ | |
541 | unsigned long ___address = __address; \ | |
542 | __young = ptep_clear_flush_young(___vma, ___address, __ptep); \ | |
543 | __young |= mmu_notifier_clear_flush_young(___vma->vm_mm, \ | |
57128468 ALC |
544 | ___address, \ |
545 | ___address + \ | |
546 | PAGE_SIZE); \ | |
cddb8a5c AA |
547 | __young; \ |
548 | }) | |
549 | ||
91a4ee26 AA |
550 | #define pmdp_clear_flush_young_notify(__vma, __address, __pmdp) \ |
551 | ({ \ | |
552 | int __young; \ | |
553 | struct vm_area_struct *___vma = __vma; \ | |
554 | unsigned long ___address = __address; \ | |
555 | __young = pmdp_clear_flush_young(___vma, ___address, __pmdp); \ | |
556 | __young |= mmu_notifier_clear_flush_young(___vma->vm_mm, \ | |
57128468 ALC |
557 | ___address, \ |
558 | ___address + \ | |
559 | PMD_SIZE); \ | |
91a4ee26 AA |
560 | __young; \ |
561 | }) | |
562 | ||
1d7715c6 VD |
563 | #define ptep_clear_young_notify(__vma, __address, __ptep) \ |
564 | ({ \ | |
565 | int __young; \ | |
566 | struct vm_area_struct *___vma = __vma; \ | |
567 | unsigned long ___address = __address; \ | |
568 | __young = ptep_test_and_clear_young(___vma, ___address, __ptep);\ | |
569 | __young |= mmu_notifier_clear_young(___vma->vm_mm, ___address, \ | |
570 | ___address + PAGE_SIZE); \ | |
571 | __young; \ | |
572 | }) | |
573 | ||
574 | #define pmdp_clear_young_notify(__vma, __address, __pmdp) \ | |
575 | ({ \ | |
576 | int __young; \ | |
577 | struct vm_area_struct *___vma = __vma; \ | |
578 | unsigned long ___address = __address; \ | |
579 | __young = pmdp_test_and_clear_young(___vma, ___address, __pmdp);\ | |
580 | __young |= mmu_notifier_clear_young(___vma->vm_mm, ___address, \ | |
581 | ___address + PMD_SIZE); \ | |
582 | __young; \ | |
583 | }) | |
584 | ||
34ee645e JR |
585 | #define ptep_clear_flush_notify(__vma, __address, __ptep) \ |
586 | ({ \ | |
587 | unsigned long ___addr = __address & PAGE_MASK; \ | |
588 | struct mm_struct *___mm = (__vma)->vm_mm; \ | |
589 | pte_t ___pte; \ | |
590 | \ | |
591 | ___pte = ptep_clear_flush(__vma, __address, __ptep); \ | |
592 | mmu_notifier_invalidate_range(___mm, ___addr, \ | |
593 | ___addr + PAGE_SIZE); \ | |
594 | \ | |
595 | ___pte; \ | |
596 | }) | |
597 | ||
8809aa2d | 598 | #define pmdp_huge_clear_flush_notify(__vma, __haddr, __pmd) \ |
34ee645e JR |
599 | ({ \ |
600 | unsigned long ___haddr = __haddr & HPAGE_PMD_MASK; \ | |
601 | struct mm_struct *___mm = (__vma)->vm_mm; \ | |
602 | pmd_t ___pmd; \ | |
603 | \ | |
8809aa2d | 604 | ___pmd = pmdp_huge_clear_flush(__vma, __haddr, __pmd); \ |
34ee645e JR |
605 | mmu_notifier_invalidate_range(___mm, ___haddr, \ |
606 | ___haddr + HPAGE_PMD_SIZE); \ | |
607 | \ | |
608 | ___pmd; \ | |
609 | }) | |
610 | ||
a00cc7d9 MW |
611 | #define pudp_huge_clear_flush_notify(__vma, __haddr, __pud) \ |
612 | ({ \ | |
613 | unsigned long ___haddr = __haddr & HPAGE_PUD_MASK; \ | |
614 | struct mm_struct *___mm = (__vma)->vm_mm; \ | |
615 | pud_t ___pud; \ | |
616 | \ | |
617 | ___pud = pudp_huge_clear_flush(__vma, __haddr, __pud); \ | |
618 | mmu_notifier_invalidate_range(___mm, ___haddr, \ | |
619 | ___haddr + HPAGE_PUD_SIZE); \ | |
620 | \ | |
621 | ___pud; \ | |
622 | }) | |
623 | ||
48af0d7c XG |
624 | /* |
625 | * set_pte_at_notify() sets the pte _after_ running the notifier. | |
626 | * This is safe to start by updating the secondary MMUs, because the primary MMU | |
627 | * pte invalidate must have already happened with a ptep_clear_flush() before | |
628 | * set_pte_at_notify() has been invoked. Updating the secondary MMUs first is | |
629 | * required when we change both the protection of the mapping from read-only to | |
630 | * read-write and the pfn (like during copy on write page faults). Otherwise the | |
631 | * old page would remain mapped readonly in the secondary MMUs after the new | |
632 | * page is already writable by some CPU through the primary MMU. | |
633 | */ | |
828502d3 IE |
634 | #define set_pte_at_notify(__mm, __address, __ptep, __pte) \ |
635 | ({ \ | |
636 | struct mm_struct *___mm = __mm; \ | |
637 | unsigned long ___address = __address; \ | |
638 | pte_t ___pte = __pte; \ | |
639 | \ | |
828502d3 | 640 | mmu_notifier_change_pte(___mm, ___address, ___pte); \ |
48af0d7c | 641 | set_pte_at(___mm, ___address, __ptep, ___pte); \ |
828502d3 IE |
642 | }) |
643 | ||
cddb8a5c AA |
644 | #else /* CONFIG_MMU_NOTIFIER */ |
645 | ||
ac46d4f3 JG |
646 | struct mmu_notifier_range { |
647 | unsigned long start; | |
648 | unsigned long end; | |
649 | }; | |
650 | ||
651 | static inline void _mmu_notifier_range_init(struct mmu_notifier_range *range, | |
652 | unsigned long start, | |
653 | unsigned long end) | |
654 | { | |
655 | range->start = start; | |
656 | range->end = end; | |
657 | } | |
658 | ||
7d4a8be0 | 659 | #define mmu_notifier_range_init(range,event,flags,mm,start,end) \ |
ac46d4f3 | 660 | _mmu_notifier_range_init(range, start, end) |
7d4a8be0 | 661 | #define mmu_notifier_range_init_owner(range, event, flags, mm, start, \ |
6b49bf6d | 662 | end, owner) \ |
c1a06df6 | 663 | _mmu_notifier_range_init(range, start, end) |
ac46d4f3 | 664 | |
4a83bfe9 JG |
665 | static inline bool |
666 | mmu_notifier_range_blockable(const struct mmu_notifier_range *range) | |
667 | { | |
668 | return true; | |
669 | } | |
ac46d4f3 | 670 | |
4d4bbd85 MH |
671 | static inline int mm_has_notifiers(struct mm_struct *mm) |
672 | { | |
673 | return 0; | |
674 | } | |
675 | ||
cddb8a5c AA |
676 | static inline void mmu_notifier_release(struct mm_struct *mm) |
677 | { | |
678 | } | |
679 | ||
680 | static inline int mmu_notifier_clear_flush_young(struct mm_struct *mm, | |
57128468 ALC |
681 | unsigned long start, |
682 | unsigned long end) | |
8ee53820 AA |
683 | { |
684 | return 0; | |
685 | } | |
686 | ||
687 | static inline int mmu_notifier_test_young(struct mm_struct *mm, | |
688 | unsigned long address) | |
cddb8a5c AA |
689 | { |
690 | return 0; | |
691 | } | |
692 | ||
828502d3 IE |
693 | static inline void mmu_notifier_change_pte(struct mm_struct *mm, |
694 | unsigned long address, pte_t pte) | |
695 | { | |
696 | } | |
697 | ||
ac46d4f3 JG |
698 | static inline void |
699 | mmu_notifier_invalidate_range_start(struct mmu_notifier_range *range) | |
cddb8a5c AA |
700 | { |
701 | } | |
702 | ||
ac46d4f3 JG |
703 | static inline int |
704 | mmu_notifier_invalidate_range_start_nonblock(struct mmu_notifier_range *range) | |
93065ac7 MH |
705 | { |
706 | return 0; | |
707 | } | |
708 | ||
ac46d4f3 JG |
709 | static inline |
710 | void mmu_notifier_invalidate_range_end(struct mmu_notifier_range *range) | |
cddb8a5c AA |
711 | { |
712 | } | |
713 | ||
ac46d4f3 JG |
714 | static inline void |
715 | mmu_notifier_invalidate_range_only_end(struct mmu_notifier_range *range) | |
4645b9fe JG |
716 | { |
717 | } | |
718 | ||
1897bdc4 JR |
719 | static inline void mmu_notifier_invalidate_range(struct mm_struct *mm, |
720 | unsigned long start, unsigned long end) | |
721 | { | |
722 | } | |
723 | ||
984cfe4e | 724 | static inline void mmu_notifier_subscriptions_init(struct mm_struct *mm) |
cddb8a5c AA |
725 | { |
726 | } | |
727 | ||
984cfe4e | 728 | static inline void mmu_notifier_subscriptions_destroy(struct mm_struct *mm) |
cddb8a5c AA |
729 | { |
730 | } | |
731 | ||
c6d23413 JG |
732 | #define mmu_notifier_range_update_to_read_only(r) false |
733 | ||
cddb8a5c | 734 | #define ptep_clear_flush_young_notify ptep_clear_flush_young |
91a4ee26 | 735 | #define pmdp_clear_flush_young_notify pmdp_clear_flush_young |
33c3fc71 VD |
736 | #define ptep_clear_young_notify ptep_test_and_clear_young |
737 | #define pmdp_clear_young_notify pmdp_test_and_clear_young | |
34ee645e | 738 | #define ptep_clear_flush_notify ptep_clear_flush |
8809aa2d | 739 | #define pmdp_huge_clear_flush_notify pmdp_huge_clear_flush |
a00cc7d9 | 740 | #define pudp_huge_clear_flush_notify pudp_huge_clear_flush |
828502d3 | 741 | #define set_pte_at_notify set_pte_at |
cddb8a5c | 742 | |
2c7933f5 JG |
743 | static inline void mmu_notifier_synchronize(void) |
744 | { | |
745 | } | |
746 | ||
cddb8a5c AA |
747 | #endif /* CONFIG_MMU_NOTIFIER */ |
748 | ||
749 | #endif /* _LINUX_MMU_NOTIFIER_H */ |