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