Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 2 | #ifndef _LINUX_MMU_NOTIFIER_H |
| 3 | #define _LINUX_MMU_NOTIFIER_H |
| 4 | |
David Rientjes | 5ff7091 | 2018-01-31 16:18:32 -0800 | [diff] [blame] | 5 | #include <linux/types.h> |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 6 | #include <linux/list.h> |
| 7 | #include <linux/spinlock.h> |
| 8 | #include <linux/mm_types.h> |
Sagi Grimberg | 21a9273 | 2012-10-08 16:29:24 -0700 | [diff] [blame] | 9 | #include <linux/srcu.h> |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 10 | |
| 11 | struct mmu_notifier; |
| 12 | struct mmu_notifier_ops; |
| 13 | |
David Rientjes | 5ff7091 | 2018-01-31 16:18:32 -0800 | [diff] [blame] | 14 | /* mmu_notifier_ops flags */ |
| 15 | #define MMU_INVALIDATE_DOES_NOT_BLOCK (0x01) |
| 16 | |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 17 | #ifdef CONFIG_MMU_NOTIFIER |
| 18 | |
| 19 | /* |
| 20 | * The mmu notifier_mm structure is allocated and installed in |
| 21 | * mm->mmu_notifier_mm inside the mm_take_all_locks() protected |
| 22 | * critical section and it's released only when mm_count reaches zero |
| 23 | * in mmdrop(). |
| 24 | */ |
| 25 | struct mmu_notifier_mm { |
| 26 | /* all mmu notifiers registerd in this mm are queued in this list */ |
| 27 | struct hlist_head list; |
| 28 | /* to serialize the list modifications and hlist_unhashed */ |
| 29 | spinlock_t lock; |
| 30 | }; |
| 31 | |
| 32 | struct mmu_notifier_ops { |
| 33 | /* |
David Rientjes | 5ff7091 | 2018-01-31 16:18:32 -0800 | [diff] [blame] | 34 | * Flags to specify behavior of callbacks for this MMU notifier. |
| 35 | * Used to determine which context an operation may be called. |
| 36 | * |
| 37 | * MMU_INVALIDATE_DOES_NOT_BLOCK: invalidate_range_* callbacks do not |
| 38 | * block |
| 39 | */ |
| 40 | int flags; |
| 41 | |
| 42 | /* |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 43 | * Called either by mmu_notifier_unregister or when the mm is |
| 44 | * being destroyed by exit_mmap, always before all pages are |
| 45 | * freed. This can run concurrently with other mmu notifier |
| 46 | * methods (the ones invoked outside the mm context) and it |
| 47 | * should tear down all secondary mmu mappings and freeze the |
| 48 | * secondary mmu. If this method isn't implemented you've to |
| 49 | * be sure that nothing could possibly write to the pages |
| 50 | * through the secondary mmu by the time the last thread with |
| 51 | * tsk->mm == mm exits. |
| 52 | * |
| 53 | * As side note: the pages freed after ->release returns could |
| 54 | * be immediately reallocated by the gart at an alias physical |
| 55 | * address with a different cache model, so if ->release isn't |
| 56 | * implemented because all _software_ driven memory accesses |
| 57 | * through the secondary mmu are terminated by the time the |
| 58 | * last thread of this mm quits, you've also to be sure that |
| 59 | * speculative _hardware_ operations can't allocate dirty |
| 60 | * cachelines in the cpu that could not be snooped and made |
| 61 | * coherent with the other read and write operations happening |
| 62 | * through the gart alias address, so leading to memory |
| 63 | * corruption. |
| 64 | */ |
| 65 | void (*release)(struct mmu_notifier *mn, |
| 66 | struct mm_struct *mm); |
| 67 | |
| 68 | /* |
| 69 | * clear_flush_young is called after the VM is |
| 70 | * test-and-clearing the young/accessed bitflag in the |
| 71 | * pte. This way the VM will provide proper aging to the |
| 72 | * accesses to the page through the secondary MMUs and not |
| 73 | * only to the ones through the Linux pte. |
Andres Lagar-Cavilla | 5712846 | 2014-09-22 14:54:42 -0700 | [diff] [blame] | 74 | * Start-end is necessary in case the secondary MMU is mapping the page |
| 75 | * at a smaller granularity than the primary MMU. |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 76 | */ |
| 77 | int (*clear_flush_young)(struct mmu_notifier *mn, |
| 78 | struct mm_struct *mm, |
Andres Lagar-Cavilla | 5712846 | 2014-09-22 14:54:42 -0700 | [diff] [blame] | 79 | unsigned long start, |
| 80 | unsigned long end); |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 81 | |
| 82 | /* |
Vladimir Davydov | 1d7715c | 2015-09-09 15:35:41 -0700 | [diff] [blame] | 83 | * clear_young is a lightweight version of clear_flush_young. Like the |
| 84 | * latter, it is supposed to test-and-clear the young/accessed bitflag |
| 85 | * in the secondary pte, but it may omit flushing the secondary tlb. |
| 86 | */ |
| 87 | int (*clear_young)(struct mmu_notifier *mn, |
| 88 | struct mm_struct *mm, |
| 89 | unsigned long start, |
| 90 | unsigned long end); |
| 91 | |
| 92 | /* |
Andrea Arcangeli | 8ee5382 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 93 | * test_young is called to check the young/accessed bitflag in |
| 94 | * the secondary pte. This is used to know if the page is |
| 95 | * frequently used without actually clearing the flag or tearing |
| 96 | * down the secondary mapping on the page. |
| 97 | */ |
| 98 | int (*test_young)(struct mmu_notifier *mn, |
| 99 | struct mm_struct *mm, |
| 100 | unsigned long address); |
| 101 | |
| 102 | /* |
Izik Eidus | 828502d | 2009-09-21 17:01:51 -0700 | [diff] [blame] | 103 | * change_pte is called in cases that pte mapping to page is changed: |
| 104 | * for example, when ksm remaps pte to point to a new shared page. |
| 105 | */ |
| 106 | void (*change_pte)(struct mmu_notifier *mn, |
| 107 | struct mm_struct *mm, |
| 108 | unsigned long address, |
| 109 | pte_t pte); |
| 110 | |
| 111 | /* |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 112 | * invalidate_range_start() and invalidate_range_end() must be |
| 113 | * paired and are called only when the mmap_sem and/or the |
Joerg Roedel | 0f0a327 | 2014-11-13 13:46:09 +1100 | [diff] [blame] | 114 | * locks protecting the reverse maps are held. If the subsystem |
| 115 | * can't guarantee that no additional references are taken to |
| 116 | * the pages in the range, it has to implement the |
| 117 | * invalidate_range() notifier to remove any references taken |
| 118 | * after invalidate_range_start(). |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 119 | * |
| 120 | * Invalidation of multiple concurrent ranges may be |
| 121 | * optionally permitted by the driver. Either way the |
| 122 | * establishment of sptes is forbidden in the range passed to |
| 123 | * invalidate_range_begin/end for the whole duration of the |
| 124 | * invalidate_range_begin/end critical section. |
| 125 | * |
| 126 | * invalidate_range_start() is called when all pages in the |
| 127 | * range are still mapped and have at least a refcount of one. |
| 128 | * |
| 129 | * invalidate_range_end() is called when all pages in the |
| 130 | * range have been unmapped and the pages have been freed by |
| 131 | * the VM. |
| 132 | * |
| 133 | * The VM will remove the page table entries and potentially |
| 134 | * the page between invalidate_range_start() and |
| 135 | * invalidate_range_end(). If the page must not be freed |
| 136 | * because of pending I/O or other circumstances then the |
| 137 | * invalidate_range_start() callback (or the initial mapping |
| 138 | * by the driver) must make sure that the refcount is kept |
| 139 | * elevated. |
| 140 | * |
| 141 | * If the driver increases the refcount when the pages are |
| 142 | * initially mapped into an address space then either |
| 143 | * invalidate_range_start() or invalidate_range_end() may |
| 144 | * decrease the refcount. If the refcount is decreased on |
| 145 | * invalidate_range_start() then the VM can free pages as page |
| 146 | * table entries are removed. If the refcount is only |
| 147 | * droppped on invalidate_range_end() then the driver itself |
| 148 | * will drop the last refcount but it must take care to flush |
| 149 | * any secondary tlb before doing the final free on the |
| 150 | * page. Pages will no longer be referenced by the linux |
| 151 | * address space but may still be referenced by sptes until |
| 152 | * the last refcount is dropped. |
David Rientjes | 5ff7091 | 2018-01-31 16:18:32 -0800 | [diff] [blame] | 153 | * |
Michal Hocko | 93065ac | 2018-08-21 21:52:33 -0700 | [diff] [blame] | 154 | * If blockable argument is set to false then the callback cannot |
| 155 | * sleep and has to return with -EAGAIN. 0 should be returned |
| 156 | * otherwise. |
| 157 | * |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 158 | */ |
Michal Hocko | 93065ac | 2018-08-21 21:52:33 -0700 | [diff] [blame] | 159 | int (*invalidate_range_start)(struct mmu_notifier *mn, |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 160 | struct mm_struct *mm, |
Michal Hocko | 93065ac | 2018-08-21 21:52:33 -0700 | [diff] [blame] | 161 | unsigned long start, unsigned long end, |
| 162 | bool blockable); |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 163 | void (*invalidate_range_end)(struct mmu_notifier *mn, |
| 164 | struct mm_struct *mm, |
| 165 | unsigned long start, unsigned long end); |
Joerg Roedel | 0f0a327 | 2014-11-13 13:46:09 +1100 | [diff] [blame] | 166 | |
| 167 | /* |
| 168 | * invalidate_range() is either called between |
| 169 | * invalidate_range_start() and invalidate_range_end() when the |
| 170 | * VM has to free pages that where unmapped, but before the |
| 171 | * pages are actually freed, or outside of _start()/_end() when |
| 172 | * a (remote) TLB is necessary. |
| 173 | * |
| 174 | * If invalidate_range() is used to manage a non-CPU TLB with |
| 175 | * shared page-tables, it not necessary to implement the |
| 176 | * invalidate_range_start()/end() notifiers, as |
| 177 | * invalidate_range() alread catches the points in time when an |
Jérôme Glisse | 0f10851 | 2017-11-15 17:34:07 -0800 | [diff] [blame] | 178 | * external TLB range needs to be flushed. For more in depth |
Mike Rapoport | ad56b73 | 2018-03-21 21:22:47 +0200 | [diff] [blame] | 179 | * discussion on this see Documentation/vm/mmu_notifier.rst |
Joerg Roedel | 0f0a327 | 2014-11-13 13:46:09 +1100 | [diff] [blame] | 180 | * |
Joerg Roedel | 0f0a327 | 2014-11-13 13:46:09 +1100 | [diff] [blame] | 181 | * Note that this function might be called with just a sub-range |
| 182 | * of what was passed to invalidate_range_start()/end(), if |
| 183 | * called between those functions. |
David Rientjes | 5ff7091 | 2018-01-31 16:18:32 -0800 | [diff] [blame] | 184 | * |
| 185 | * If this callback cannot block, and invalidate_range_{start,end} |
| 186 | * cannot block, mmu_notifier_ops.flags should have |
| 187 | * MMU_INVALIDATE_DOES_NOT_BLOCK set. |
Joerg Roedel | 0f0a327 | 2014-11-13 13:46:09 +1100 | [diff] [blame] | 188 | */ |
| 189 | void (*invalidate_range)(struct mmu_notifier *mn, struct mm_struct *mm, |
| 190 | unsigned long start, unsigned long end); |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | /* |
| 194 | * The notifier chains are protected by mmap_sem and/or the reverse map |
| 195 | * semaphores. Notifier chains are only changed when all reverse maps and |
| 196 | * the mmap_sem locks are taken. |
| 197 | * |
| 198 | * Therefore notifier chains can only be traversed when either |
| 199 | * |
| 200 | * 1. mmap_sem is held. |
Davidlohr Bueso | c8c06ef | 2014-12-12 16:54:24 -0800 | [diff] [blame] | 201 | * 2. One of the reverse map locks is held (i_mmap_rwsem or anon_vma->rwsem). |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 202 | * 3. No other concurrent thread can access the list (release) |
| 203 | */ |
| 204 | struct mmu_notifier { |
| 205 | struct hlist_node hlist; |
| 206 | const struct mmu_notifier_ops *ops; |
| 207 | }; |
| 208 | |
| 209 | static inline int mm_has_notifiers(struct mm_struct *mm) |
| 210 | { |
| 211 | return unlikely(mm->mmu_notifier_mm); |
| 212 | } |
| 213 | |
| 214 | extern int mmu_notifier_register(struct mmu_notifier *mn, |
| 215 | struct mm_struct *mm); |
| 216 | extern int __mmu_notifier_register(struct mmu_notifier *mn, |
| 217 | struct mm_struct *mm); |
| 218 | extern void mmu_notifier_unregister(struct mmu_notifier *mn, |
| 219 | struct mm_struct *mm); |
Peter Zijlstra | b972216 | 2014-08-06 16:08:20 -0700 | [diff] [blame] | 220 | extern void mmu_notifier_unregister_no_release(struct mmu_notifier *mn, |
| 221 | struct mm_struct *mm); |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 222 | extern void __mmu_notifier_mm_destroy(struct mm_struct *mm); |
| 223 | extern void __mmu_notifier_release(struct mm_struct *mm); |
| 224 | extern int __mmu_notifier_clear_flush_young(struct mm_struct *mm, |
Andres Lagar-Cavilla | 5712846 | 2014-09-22 14:54:42 -0700 | [diff] [blame] | 225 | unsigned long start, |
| 226 | unsigned long end); |
Vladimir Davydov | 1d7715c | 2015-09-09 15:35:41 -0700 | [diff] [blame] | 227 | extern int __mmu_notifier_clear_young(struct mm_struct *mm, |
| 228 | unsigned long start, |
| 229 | unsigned long end); |
Andrea Arcangeli | 8ee5382 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 230 | extern int __mmu_notifier_test_young(struct mm_struct *mm, |
| 231 | unsigned long address); |
Izik Eidus | 828502d | 2009-09-21 17:01:51 -0700 | [diff] [blame] | 232 | extern void __mmu_notifier_change_pte(struct mm_struct *mm, |
| 233 | unsigned long address, pte_t pte); |
Michal Hocko | 93065ac | 2018-08-21 21:52:33 -0700 | [diff] [blame] | 234 | extern int __mmu_notifier_invalidate_range_start(struct mm_struct *mm, |
| 235 | unsigned long start, unsigned long end, |
| 236 | bool blockable); |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 237 | extern void __mmu_notifier_invalidate_range_end(struct mm_struct *mm, |
Jérôme Glisse | 4645b9f | 2017-11-15 17:34:11 -0800 | [diff] [blame] | 238 | unsigned long start, unsigned long end, |
| 239 | bool only_end); |
Joerg Roedel | 0f0a327 | 2014-11-13 13:46:09 +1100 | [diff] [blame] | 240 | extern void __mmu_notifier_invalidate_range(struct mm_struct *mm, |
| 241 | unsigned long start, unsigned long end); |
David Rientjes | 5ff7091 | 2018-01-31 16:18:32 -0800 | [diff] [blame] | 242 | extern bool mm_has_blockable_invalidate_notifiers(struct mm_struct *mm); |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 243 | |
| 244 | static inline void mmu_notifier_release(struct mm_struct *mm) |
| 245 | { |
| 246 | if (mm_has_notifiers(mm)) |
| 247 | __mmu_notifier_release(mm); |
| 248 | } |
| 249 | |
| 250 | static inline int mmu_notifier_clear_flush_young(struct mm_struct *mm, |
Andres Lagar-Cavilla | 5712846 | 2014-09-22 14:54:42 -0700 | [diff] [blame] | 251 | unsigned long start, |
| 252 | unsigned long end) |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 253 | { |
| 254 | if (mm_has_notifiers(mm)) |
Andres Lagar-Cavilla | 5712846 | 2014-09-22 14:54:42 -0700 | [diff] [blame] | 255 | return __mmu_notifier_clear_flush_young(mm, start, end); |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 256 | return 0; |
| 257 | } |
| 258 | |
Vladimir Davydov | 1d7715c | 2015-09-09 15:35:41 -0700 | [diff] [blame] | 259 | static inline int mmu_notifier_clear_young(struct mm_struct *mm, |
| 260 | unsigned long start, |
| 261 | unsigned long end) |
| 262 | { |
| 263 | if (mm_has_notifiers(mm)) |
| 264 | return __mmu_notifier_clear_young(mm, start, end); |
| 265 | return 0; |
| 266 | } |
| 267 | |
Andrea Arcangeli | 8ee5382 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 268 | static inline int mmu_notifier_test_young(struct mm_struct *mm, |
| 269 | unsigned long address) |
| 270 | { |
| 271 | if (mm_has_notifiers(mm)) |
| 272 | return __mmu_notifier_test_young(mm, address); |
| 273 | return 0; |
| 274 | } |
| 275 | |
Izik Eidus | 828502d | 2009-09-21 17:01:51 -0700 | [diff] [blame] | 276 | static inline void mmu_notifier_change_pte(struct mm_struct *mm, |
| 277 | unsigned long address, pte_t pte) |
| 278 | { |
| 279 | if (mm_has_notifiers(mm)) |
| 280 | __mmu_notifier_change_pte(mm, address, pte); |
| 281 | } |
| 282 | |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 283 | static inline void mmu_notifier_invalidate_range_start(struct mm_struct *mm, |
| 284 | unsigned long start, unsigned long end) |
| 285 | { |
| 286 | if (mm_has_notifiers(mm)) |
Michal Hocko | 93065ac | 2018-08-21 21:52:33 -0700 | [diff] [blame] | 287 | __mmu_notifier_invalidate_range_start(mm, start, end, true); |
| 288 | } |
| 289 | |
| 290 | static inline int mmu_notifier_invalidate_range_start_nonblock(struct mm_struct *mm, |
| 291 | unsigned long start, unsigned long end) |
| 292 | { |
| 293 | if (mm_has_notifiers(mm)) |
| 294 | return __mmu_notifier_invalidate_range_start(mm, start, end, false); |
| 295 | return 0; |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | static inline void mmu_notifier_invalidate_range_end(struct mm_struct *mm, |
| 299 | unsigned long start, unsigned long end) |
| 300 | { |
| 301 | if (mm_has_notifiers(mm)) |
Jérôme Glisse | 4645b9f | 2017-11-15 17:34:11 -0800 | [diff] [blame] | 302 | __mmu_notifier_invalidate_range_end(mm, start, end, false); |
| 303 | } |
| 304 | |
| 305 | static inline void mmu_notifier_invalidate_range_only_end(struct mm_struct *mm, |
| 306 | unsigned long start, unsigned long end) |
| 307 | { |
| 308 | if (mm_has_notifiers(mm)) |
| 309 | __mmu_notifier_invalidate_range_end(mm, start, end, true); |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Joerg Roedel | 1897bdc | 2014-11-13 13:46:09 +1100 | [diff] [blame] | 312 | static inline void mmu_notifier_invalidate_range(struct mm_struct *mm, |
| 313 | unsigned long start, unsigned long end) |
| 314 | { |
Joerg Roedel | 0f0a327 | 2014-11-13 13:46:09 +1100 | [diff] [blame] | 315 | if (mm_has_notifiers(mm)) |
| 316 | __mmu_notifier_invalidate_range(mm, start, end); |
Joerg Roedel | 1897bdc | 2014-11-13 13:46:09 +1100 | [diff] [blame] | 317 | } |
| 318 | |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 319 | static inline void mmu_notifier_mm_init(struct mm_struct *mm) |
| 320 | { |
| 321 | mm->mmu_notifier_mm = NULL; |
| 322 | } |
| 323 | |
| 324 | static inline void mmu_notifier_mm_destroy(struct mm_struct *mm) |
| 325 | { |
| 326 | if (mm_has_notifiers(mm)) |
| 327 | __mmu_notifier_mm_destroy(mm); |
| 328 | } |
| 329 | |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 330 | #define ptep_clear_flush_young_notify(__vma, __address, __ptep) \ |
| 331 | ({ \ |
| 332 | int __young; \ |
| 333 | struct vm_area_struct *___vma = __vma; \ |
| 334 | unsigned long ___address = __address; \ |
| 335 | __young = ptep_clear_flush_young(___vma, ___address, __ptep); \ |
| 336 | __young |= mmu_notifier_clear_flush_young(___vma->vm_mm, \ |
Andres Lagar-Cavilla | 5712846 | 2014-09-22 14:54:42 -0700 | [diff] [blame] | 337 | ___address, \ |
| 338 | ___address + \ |
| 339 | PAGE_SIZE); \ |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 340 | __young; \ |
| 341 | }) |
| 342 | |
Andrea Arcangeli | 91a4ee2 | 2011-01-13 15:46:44 -0800 | [diff] [blame] | 343 | #define pmdp_clear_flush_young_notify(__vma, __address, __pmdp) \ |
| 344 | ({ \ |
| 345 | int __young; \ |
| 346 | struct vm_area_struct *___vma = __vma; \ |
| 347 | unsigned long ___address = __address; \ |
| 348 | __young = pmdp_clear_flush_young(___vma, ___address, __pmdp); \ |
| 349 | __young |= mmu_notifier_clear_flush_young(___vma->vm_mm, \ |
Andres Lagar-Cavilla | 5712846 | 2014-09-22 14:54:42 -0700 | [diff] [blame] | 350 | ___address, \ |
| 351 | ___address + \ |
| 352 | PMD_SIZE); \ |
Andrea Arcangeli | 91a4ee2 | 2011-01-13 15:46:44 -0800 | [diff] [blame] | 353 | __young; \ |
| 354 | }) |
| 355 | |
Vladimir Davydov | 1d7715c | 2015-09-09 15:35:41 -0700 | [diff] [blame] | 356 | #define ptep_clear_young_notify(__vma, __address, __ptep) \ |
| 357 | ({ \ |
| 358 | int __young; \ |
| 359 | struct vm_area_struct *___vma = __vma; \ |
| 360 | unsigned long ___address = __address; \ |
| 361 | __young = ptep_test_and_clear_young(___vma, ___address, __ptep);\ |
| 362 | __young |= mmu_notifier_clear_young(___vma->vm_mm, ___address, \ |
| 363 | ___address + PAGE_SIZE); \ |
| 364 | __young; \ |
| 365 | }) |
| 366 | |
| 367 | #define pmdp_clear_young_notify(__vma, __address, __pmdp) \ |
| 368 | ({ \ |
| 369 | int __young; \ |
| 370 | struct vm_area_struct *___vma = __vma; \ |
| 371 | unsigned long ___address = __address; \ |
| 372 | __young = pmdp_test_and_clear_young(___vma, ___address, __pmdp);\ |
| 373 | __young |= mmu_notifier_clear_young(___vma->vm_mm, ___address, \ |
| 374 | ___address + PMD_SIZE); \ |
| 375 | __young; \ |
| 376 | }) |
| 377 | |
Joerg Roedel | 34ee645 | 2014-11-13 13:46:09 +1100 | [diff] [blame] | 378 | #define ptep_clear_flush_notify(__vma, __address, __ptep) \ |
| 379 | ({ \ |
| 380 | unsigned long ___addr = __address & PAGE_MASK; \ |
| 381 | struct mm_struct *___mm = (__vma)->vm_mm; \ |
| 382 | pte_t ___pte; \ |
| 383 | \ |
| 384 | ___pte = ptep_clear_flush(__vma, __address, __ptep); \ |
| 385 | mmu_notifier_invalidate_range(___mm, ___addr, \ |
| 386 | ___addr + PAGE_SIZE); \ |
| 387 | \ |
| 388 | ___pte; \ |
| 389 | }) |
| 390 | |
Aneesh Kumar K.V | 8809aa2 | 2015-06-24 16:57:44 -0700 | [diff] [blame] | 391 | #define pmdp_huge_clear_flush_notify(__vma, __haddr, __pmd) \ |
Joerg Roedel | 34ee645 | 2014-11-13 13:46:09 +1100 | [diff] [blame] | 392 | ({ \ |
| 393 | unsigned long ___haddr = __haddr & HPAGE_PMD_MASK; \ |
| 394 | struct mm_struct *___mm = (__vma)->vm_mm; \ |
| 395 | pmd_t ___pmd; \ |
| 396 | \ |
Aneesh Kumar K.V | 8809aa2 | 2015-06-24 16:57:44 -0700 | [diff] [blame] | 397 | ___pmd = pmdp_huge_clear_flush(__vma, __haddr, __pmd); \ |
Joerg Roedel | 34ee645 | 2014-11-13 13:46:09 +1100 | [diff] [blame] | 398 | mmu_notifier_invalidate_range(___mm, ___haddr, \ |
| 399 | ___haddr + HPAGE_PMD_SIZE); \ |
| 400 | \ |
| 401 | ___pmd; \ |
| 402 | }) |
| 403 | |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 404 | #define pudp_huge_clear_flush_notify(__vma, __haddr, __pud) \ |
| 405 | ({ \ |
| 406 | unsigned long ___haddr = __haddr & HPAGE_PUD_MASK; \ |
| 407 | struct mm_struct *___mm = (__vma)->vm_mm; \ |
| 408 | pud_t ___pud; \ |
| 409 | \ |
| 410 | ___pud = pudp_huge_clear_flush(__vma, __haddr, __pud); \ |
| 411 | mmu_notifier_invalidate_range(___mm, ___haddr, \ |
| 412 | ___haddr + HPAGE_PUD_SIZE); \ |
| 413 | \ |
| 414 | ___pud; \ |
| 415 | }) |
| 416 | |
Xiao Guangrong | 48af0d7 | 2012-10-08 16:29:23 -0700 | [diff] [blame] | 417 | /* |
| 418 | * set_pte_at_notify() sets the pte _after_ running the notifier. |
| 419 | * This is safe to start by updating the secondary MMUs, because the primary MMU |
| 420 | * pte invalidate must have already happened with a ptep_clear_flush() before |
| 421 | * set_pte_at_notify() has been invoked. Updating the secondary MMUs first is |
| 422 | * required when we change both the protection of the mapping from read-only to |
| 423 | * read-write and the pfn (like during copy on write page faults). Otherwise the |
| 424 | * old page would remain mapped readonly in the secondary MMUs after the new |
| 425 | * page is already writable by some CPU through the primary MMU. |
| 426 | */ |
Izik Eidus | 828502d | 2009-09-21 17:01:51 -0700 | [diff] [blame] | 427 | #define set_pte_at_notify(__mm, __address, __ptep, __pte) \ |
| 428 | ({ \ |
| 429 | struct mm_struct *___mm = __mm; \ |
| 430 | unsigned long ___address = __address; \ |
| 431 | pte_t ___pte = __pte; \ |
| 432 | \ |
Izik Eidus | 828502d | 2009-09-21 17:01:51 -0700 | [diff] [blame] | 433 | mmu_notifier_change_pte(___mm, ___address, ___pte); \ |
Xiao Guangrong | 48af0d7 | 2012-10-08 16:29:23 -0700 | [diff] [blame] | 434 | set_pte_at(___mm, ___address, __ptep, ___pte); \ |
Izik Eidus | 828502d | 2009-09-21 17:01:51 -0700 | [diff] [blame] | 435 | }) |
| 436 | |
Peter Zijlstra | b972216 | 2014-08-06 16:08:20 -0700 | [diff] [blame] | 437 | extern void mmu_notifier_call_srcu(struct rcu_head *rcu, |
| 438 | void (*func)(struct rcu_head *rcu)); |
| 439 | extern void mmu_notifier_synchronize(void); |
| 440 | |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 441 | #else /* CONFIG_MMU_NOTIFIER */ |
| 442 | |
Michal Hocko | 4d4bbd8 | 2017-10-03 16:14:50 -0700 | [diff] [blame] | 443 | static inline int mm_has_notifiers(struct mm_struct *mm) |
| 444 | { |
| 445 | return 0; |
| 446 | } |
| 447 | |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 448 | static inline void mmu_notifier_release(struct mm_struct *mm) |
| 449 | { |
| 450 | } |
| 451 | |
| 452 | static inline int mmu_notifier_clear_flush_young(struct mm_struct *mm, |
Andres Lagar-Cavilla | 5712846 | 2014-09-22 14:54:42 -0700 | [diff] [blame] | 453 | unsigned long start, |
| 454 | unsigned long end) |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 455 | { |
| 456 | return 0; |
| 457 | } |
| 458 | |
Andrea Arcangeli | 8ee5382 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 459 | static inline int mmu_notifier_test_young(struct mm_struct *mm, |
| 460 | unsigned long address) |
| 461 | { |
| 462 | return 0; |
| 463 | } |
| 464 | |
Izik Eidus | 828502d | 2009-09-21 17:01:51 -0700 | [diff] [blame] | 465 | static inline void mmu_notifier_change_pte(struct mm_struct *mm, |
| 466 | unsigned long address, pte_t pte) |
| 467 | { |
| 468 | } |
| 469 | |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 470 | static inline void mmu_notifier_invalidate_range_start(struct mm_struct *mm, |
| 471 | unsigned long start, unsigned long end) |
| 472 | { |
| 473 | } |
| 474 | |
Michal Hocko | 93065ac | 2018-08-21 21:52:33 -0700 | [diff] [blame] | 475 | static inline int mmu_notifier_invalidate_range_start_nonblock(struct mm_struct *mm, |
| 476 | unsigned long start, unsigned long end) |
| 477 | { |
| 478 | return 0; |
| 479 | } |
| 480 | |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 481 | static inline void mmu_notifier_invalidate_range_end(struct mm_struct *mm, |
| 482 | unsigned long start, unsigned long end) |
| 483 | { |
| 484 | } |
| 485 | |
Jérôme Glisse | 4645b9f | 2017-11-15 17:34:11 -0800 | [diff] [blame] | 486 | static inline void mmu_notifier_invalidate_range_only_end(struct mm_struct *mm, |
| 487 | unsigned long start, unsigned long end) |
| 488 | { |
| 489 | } |
| 490 | |
Joerg Roedel | 1897bdc | 2014-11-13 13:46:09 +1100 | [diff] [blame] | 491 | static inline void mmu_notifier_invalidate_range(struct mm_struct *mm, |
| 492 | unsigned long start, unsigned long end) |
| 493 | { |
| 494 | } |
| 495 | |
David Rientjes | 5ff7091 | 2018-01-31 16:18:32 -0800 | [diff] [blame] | 496 | static inline bool mm_has_blockable_invalidate_notifiers(struct mm_struct *mm) |
| 497 | { |
| 498 | return false; |
| 499 | } |
| 500 | |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 501 | static inline void mmu_notifier_mm_init(struct mm_struct *mm) |
| 502 | { |
| 503 | } |
| 504 | |
| 505 | static inline void mmu_notifier_mm_destroy(struct mm_struct *mm) |
| 506 | { |
| 507 | } |
| 508 | |
| 509 | #define ptep_clear_flush_young_notify ptep_clear_flush_young |
Andrea Arcangeli | 91a4ee2 | 2011-01-13 15:46:44 -0800 | [diff] [blame] | 510 | #define pmdp_clear_flush_young_notify pmdp_clear_flush_young |
Vladimir Davydov | 33c3fc7 | 2015-09-09 15:35:45 -0700 | [diff] [blame] | 511 | #define ptep_clear_young_notify ptep_test_and_clear_young |
| 512 | #define pmdp_clear_young_notify pmdp_test_and_clear_young |
Joerg Roedel | 34ee645 | 2014-11-13 13:46:09 +1100 | [diff] [blame] | 513 | #define ptep_clear_flush_notify ptep_clear_flush |
Aneesh Kumar K.V | 8809aa2 | 2015-06-24 16:57:44 -0700 | [diff] [blame] | 514 | #define pmdp_huge_clear_flush_notify pmdp_huge_clear_flush |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 515 | #define pudp_huge_clear_flush_notify pudp_huge_clear_flush |
Izik Eidus | 828502d | 2009-09-21 17:01:51 -0700 | [diff] [blame] | 516 | #define set_pte_at_notify set_pte_at |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 517 | |
| 518 | #endif /* CONFIG_MMU_NOTIFIER */ |
| 519 | |
| 520 | #endif /* _LINUX_MMU_NOTIFIER_H */ |