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