Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Christoph Hellwig | a520110 | 2019-08-28 16:19:53 +0200 | [diff] [blame] | 2 | #include <linux/pagewalk.h> |
Matt Mackall | e647309 | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 3 | #include <linux/highmem.h> |
| 4 | #include <linux/sched.h> |
Naoya Horiguchi | d33b9f4 | 2009-12-14 17:59:59 -0800 | [diff] [blame] | 5 | #include <linux/hugetlb.h> |
Matt Mackall | e647309 | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 6 | |
| 7 | static int walk_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, |
Dave Hansen | 2165009 | 2008-06-12 15:21:47 -0700 | [diff] [blame] | 8 | struct mm_walk *walk) |
Matt Mackall | e647309 | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 9 | { |
| 10 | pte_t *pte; |
| 11 | int err = 0; |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 12 | const struct mm_walk_ops *ops = walk->ops; |
Thomas Hellstrom | ace88f1 | 2019-10-04 11:04:43 +0200 | [diff] [blame] | 13 | spinlock_t *ptl; |
Matt Mackall | e647309 | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 14 | |
Thomas Hellstrom | ace88f1 | 2019-10-04 11:04:43 +0200 | [diff] [blame] | 15 | pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl); |
Johannes Weiner | 556637c | 2008-04-28 02:11:47 -0700 | [diff] [blame] | 16 | for (;;) { |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 17 | err = ops->pte_entry(pte, addr, addr + PAGE_SIZE, walk); |
Matt Mackall | e647309 | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 18 | if (err) |
| 19 | break; |
Johannes Weiner | 556637c | 2008-04-28 02:11:47 -0700 | [diff] [blame] | 20 | addr += PAGE_SIZE; |
| 21 | if (addr == end) |
| 22 | break; |
| 23 | pte++; |
| 24 | } |
Matt Mackall | e647309 | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 25 | |
Thomas Hellstrom | ace88f1 | 2019-10-04 11:04:43 +0200 | [diff] [blame] | 26 | pte_unmap_unlock(pte, ptl); |
Matt Mackall | e647309 | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 27 | return err; |
| 28 | } |
| 29 | |
| 30 | static int walk_pmd_range(pud_t *pud, unsigned long addr, unsigned long end, |
Dave Hansen | 2165009 | 2008-06-12 15:21:47 -0700 | [diff] [blame] | 31 | struct mm_walk *walk) |
Matt Mackall | e647309 | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 32 | { |
| 33 | pmd_t *pmd; |
| 34 | unsigned long next; |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 35 | const struct mm_walk_ops *ops = walk->ops; |
Matt Mackall | e647309 | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 36 | int err = 0; |
| 37 | |
| 38 | pmd = pmd_offset(pud, addr); |
| 39 | do { |
Dave Hansen | 0331932 | 2011-03-22 16:32:56 -0700 | [diff] [blame] | 40 | again: |
Matt Mackall | e647309 | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 41 | next = pmd_addr_end(addr, end); |
Naoya Horiguchi | 48684a6 | 2015-02-11 15:28:06 -0800 | [diff] [blame] | 42 | if (pmd_none(*pmd) || !walk->vma) { |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 43 | if (ops->pte_hole) |
| 44 | err = ops->pte_hole(addr, next, walk); |
Matt Mackall | e647309 | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 45 | if (err) |
| 46 | break; |
| 47 | continue; |
| 48 | } |
Steven Price | 3afc423 | 2020-02-03 17:35:45 -0800 | [diff] [blame^] | 49 | |
| 50 | walk->action = ACTION_SUBTREE; |
| 51 | |
Dave Hansen | 0331932 | 2011-03-22 16:32:56 -0700 | [diff] [blame] | 52 | /* |
| 53 | * This implies that each ->pmd_entry() handler |
| 54 | * needs to know about pmd_trans_huge() pmds |
| 55 | */ |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 56 | if (ops->pmd_entry) |
| 57 | err = ops->pmd_entry(pmd, addr, next, walk); |
Dave Hansen | 0331932 | 2011-03-22 16:32:56 -0700 | [diff] [blame] | 58 | if (err) |
| 59 | break; |
| 60 | |
Steven Price | 3afc423 | 2020-02-03 17:35:45 -0800 | [diff] [blame^] | 61 | if (walk->action == ACTION_AGAIN) |
| 62 | goto again; |
| 63 | |
Dave Hansen | 0331932 | 2011-03-22 16:32:56 -0700 | [diff] [blame] | 64 | /* |
| 65 | * Check this here so we only break down trans_huge |
| 66 | * pages when we _need_ to |
| 67 | */ |
Steven Price | 3afc423 | 2020-02-03 17:35:45 -0800 | [diff] [blame^] | 68 | if (walk->action == ACTION_CONTINUE || |
| 69 | !(ops->pte_entry)) |
Dave Hansen | 0331932 | 2011-03-22 16:32:56 -0700 | [diff] [blame] | 70 | continue; |
| 71 | |
Kirill A. Shutemov | 78ddc53 | 2016-01-15 16:52:42 -0800 | [diff] [blame] | 72 | split_huge_pmd(walk->vma, pmd, addr); |
Naoya Horiguchi | fafaa42 | 2015-02-11 15:27:37 -0800 | [diff] [blame] | 73 | if (pmd_trans_unstable(pmd)) |
Dave Hansen | 0331932 | 2011-03-22 16:32:56 -0700 | [diff] [blame] | 74 | goto again; |
Steven Price | 3afc423 | 2020-02-03 17:35:45 -0800 | [diff] [blame^] | 75 | |
Dave Hansen | 0331932 | 2011-03-22 16:32:56 -0700 | [diff] [blame] | 76 | err = walk_pte_range(pmd, addr, next, walk); |
Matt Mackall | e647309 | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 77 | if (err) |
| 78 | break; |
| 79 | } while (pmd++, addr = next, addr != end); |
| 80 | |
| 81 | return err; |
| 82 | } |
| 83 | |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 84 | static int walk_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end, |
Dave Hansen | 2165009 | 2008-06-12 15:21:47 -0700 | [diff] [blame] | 85 | struct mm_walk *walk) |
Matt Mackall | e647309 | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 86 | { |
| 87 | pud_t *pud; |
| 88 | unsigned long next; |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 89 | const struct mm_walk_ops *ops = walk->ops; |
Matt Mackall | e647309 | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 90 | int err = 0; |
| 91 | |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 92 | pud = pud_offset(p4d, addr); |
Matt Mackall | e647309 | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 93 | do { |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 94 | again: |
Matt Mackall | e647309 | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 95 | next = pud_addr_end(addr, end); |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 96 | if (pud_none(*pud) || !walk->vma) { |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 97 | if (ops->pte_hole) |
| 98 | err = ops->pte_hole(addr, next, walk); |
Matt Mackall | e647309 | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 99 | if (err) |
| 100 | break; |
| 101 | continue; |
| 102 | } |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 103 | |
Steven Price | 3afc423 | 2020-02-03 17:35:45 -0800 | [diff] [blame^] | 104 | walk->action = ACTION_SUBTREE; |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 105 | |
Steven Price | 3afc423 | 2020-02-03 17:35:45 -0800 | [diff] [blame^] | 106 | if (ops->pud_entry) |
| 107 | err = ops->pud_entry(pud, addr, next, walk); |
| 108 | if (err) |
| 109 | break; |
| 110 | |
| 111 | if (walk->action == ACTION_AGAIN) |
| 112 | goto again; |
| 113 | |
| 114 | if (walk->action == ACTION_CONTINUE || |
| 115 | !(ops->pmd_entry || ops->pte_entry)) |
| 116 | continue; |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 117 | |
| 118 | split_huge_pud(walk->vma, pud, addr); |
| 119 | if (pud_none(*pud)) |
| 120 | goto again; |
| 121 | |
Steven Price | 3afc423 | 2020-02-03 17:35:45 -0800 | [diff] [blame^] | 122 | err = walk_pmd_range(pud, addr, next, walk); |
Matt Mackall | e647309 | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 123 | if (err) |
| 124 | break; |
| 125 | } while (pud++, addr = next, addr != end); |
| 126 | |
| 127 | return err; |
| 128 | } |
| 129 | |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 130 | static int walk_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end, |
| 131 | struct mm_walk *walk) |
| 132 | { |
| 133 | p4d_t *p4d; |
| 134 | unsigned long next; |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 135 | const struct mm_walk_ops *ops = walk->ops; |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 136 | int err = 0; |
| 137 | |
| 138 | p4d = p4d_offset(pgd, addr); |
| 139 | do { |
| 140 | next = p4d_addr_end(addr, end); |
| 141 | if (p4d_none_or_clear_bad(p4d)) { |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 142 | if (ops->pte_hole) |
| 143 | err = ops->pte_hole(addr, next, walk); |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 144 | if (err) |
| 145 | break; |
| 146 | continue; |
| 147 | } |
Steven Price | 3afc423 | 2020-02-03 17:35:45 -0800 | [diff] [blame^] | 148 | if (ops->p4d_entry) { |
| 149 | err = ops->p4d_entry(p4d, addr, next, walk); |
| 150 | if (err) |
| 151 | break; |
| 152 | } |
| 153 | if (ops->pud_entry || ops->pmd_entry || ops->pte_entry) |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 154 | err = walk_pud_range(p4d, addr, next, walk); |
| 155 | if (err) |
| 156 | break; |
| 157 | } while (p4d++, addr = next, addr != end); |
| 158 | |
| 159 | return err; |
| 160 | } |
| 161 | |
Naoya Horiguchi | fafaa42 | 2015-02-11 15:27:37 -0800 | [diff] [blame] | 162 | static int walk_pgd_range(unsigned long addr, unsigned long end, |
| 163 | struct mm_walk *walk) |
| 164 | { |
| 165 | pgd_t *pgd; |
| 166 | unsigned long next; |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 167 | const struct mm_walk_ops *ops = walk->ops; |
Naoya Horiguchi | fafaa42 | 2015-02-11 15:27:37 -0800 | [diff] [blame] | 168 | int err = 0; |
| 169 | |
| 170 | pgd = pgd_offset(walk->mm, addr); |
| 171 | do { |
| 172 | next = pgd_addr_end(addr, end); |
| 173 | if (pgd_none_or_clear_bad(pgd)) { |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 174 | if (ops->pte_hole) |
| 175 | err = ops->pte_hole(addr, next, walk); |
Naoya Horiguchi | fafaa42 | 2015-02-11 15:27:37 -0800 | [diff] [blame] | 176 | if (err) |
| 177 | break; |
| 178 | continue; |
| 179 | } |
Steven Price | 3afc423 | 2020-02-03 17:35:45 -0800 | [diff] [blame^] | 180 | if (ops->pgd_entry) { |
| 181 | err = ops->pgd_entry(pgd, addr, next, walk); |
| 182 | if (err) |
| 183 | break; |
| 184 | } |
| 185 | if (ops->p4d_entry || ops->pud_entry || ops->pmd_entry || |
| 186 | ops->pte_entry) |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 187 | err = walk_p4d_range(pgd, addr, next, walk); |
Naoya Horiguchi | fafaa42 | 2015-02-11 15:27:37 -0800 | [diff] [blame] | 188 | if (err) |
| 189 | break; |
| 190 | } while (pgd++, addr = next, addr != end); |
| 191 | |
| 192 | return err; |
| 193 | } |
| 194 | |
Naoya Horiguchi | 116354d | 2010-04-06 14:35:04 -0700 | [diff] [blame] | 195 | #ifdef CONFIG_HUGETLB_PAGE |
| 196 | static unsigned long hugetlb_entry_end(struct hstate *h, unsigned long addr, |
| 197 | unsigned long end) |
| 198 | { |
| 199 | unsigned long boundary = (addr & huge_page_mask(h)) + huge_page_size(h); |
| 200 | return boundary < end ? boundary : end; |
| 201 | } |
| 202 | |
Naoya Horiguchi | fafaa42 | 2015-02-11 15:27:37 -0800 | [diff] [blame] | 203 | static int walk_hugetlb_range(unsigned long addr, unsigned long end, |
Naoya Horiguchi | 116354d | 2010-04-06 14:35:04 -0700 | [diff] [blame] | 204 | struct mm_walk *walk) |
| 205 | { |
Naoya Horiguchi | fafaa42 | 2015-02-11 15:27:37 -0800 | [diff] [blame] | 206 | struct vm_area_struct *vma = walk->vma; |
Naoya Horiguchi | 116354d | 2010-04-06 14:35:04 -0700 | [diff] [blame] | 207 | struct hstate *h = hstate_vma(vma); |
| 208 | unsigned long next; |
| 209 | unsigned long hmask = huge_page_mask(h); |
Punit Agrawal | 7868a20 | 2017-07-06 15:39:42 -0700 | [diff] [blame] | 210 | unsigned long sz = huge_page_size(h); |
Naoya Horiguchi | 116354d | 2010-04-06 14:35:04 -0700 | [diff] [blame] | 211 | pte_t *pte; |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 212 | const struct mm_walk_ops *ops = walk->ops; |
Naoya Horiguchi | 116354d | 2010-04-06 14:35:04 -0700 | [diff] [blame] | 213 | int err = 0; |
| 214 | |
| 215 | do { |
| 216 | next = hugetlb_entry_end(h, addr, end); |
Punit Agrawal | 7868a20 | 2017-07-06 15:39:42 -0700 | [diff] [blame] | 217 | pte = huge_pte_offset(walk->mm, addr & hmask, sz); |
Jann Horn | 373c455 | 2017-11-14 01:03:44 +0100 | [diff] [blame] | 218 | |
| 219 | if (pte) |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 220 | err = ops->hugetlb_entry(pte, hmask, addr, next, walk); |
| 221 | else if (ops->pte_hole) |
| 222 | err = ops->pte_hole(addr, next, walk); |
Jann Horn | 373c455 | 2017-11-14 01:03:44 +0100 | [diff] [blame] | 223 | |
Naoya Horiguchi | 116354d | 2010-04-06 14:35:04 -0700 | [diff] [blame] | 224 | if (err) |
Naoya Horiguchi | fafaa42 | 2015-02-11 15:27:37 -0800 | [diff] [blame] | 225 | break; |
Naoya Horiguchi | 116354d | 2010-04-06 14:35:04 -0700 | [diff] [blame] | 226 | } while (addr = next, addr != end); |
| 227 | |
Naoya Horiguchi | fafaa42 | 2015-02-11 15:27:37 -0800 | [diff] [blame] | 228 | return err; |
Naoya Horiguchi | 116354d | 2010-04-06 14:35:04 -0700 | [diff] [blame] | 229 | } |
KOSAKI Motohiro | 6c6d528 | 2011-07-25 17:12:09 -0700 | [diff] [blame] | 230 | |
KOSAKI Motohiro | 6c6d528 | 2011-07-25 17:12:09 -0700 | [diff] [blame] | 231 | #else /* CONFIG_HUGETLB_PAGE */ |
Naoya Horiguchi | fafaa42 | 2015-02-11 15:27:37 -0800 | [diff] [blame] | 232 | static int walk_hugetlb_range(unsigned long addr, unsigned long end, |
KOSAKI Motohiro | 6c6d528 | 2011-07-25 17:12:09 -0700 | [diff] [blame] | 233 | struct mm_walk *walk) |
| 234 | { |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | #endif /* CONFIG_HUGETLB_PAGE */ |
| 239 | |
Naoya Horiguchi | fafaa42 | 2015-02-11 15:27:37 -0800 | [diff] [blame] | 240 | /* |
| 241 | * Decide whether we really walk over the current vma on [@start, @end) |
| 242 | * or skip it via the returned value. Return 0 if we do walk over the |
| 243 | * current vma, and return 1 if we skip the vma. Negative values means |
| 244 | * error, where we abort the current walk. |
Naoya Horiguchi | fafaa42 | 2015-02-11 15:27:37 -0800 | [diff] [blame] | 245 | */ |
| 246 | static int walk_page_test(unsigned long start, unsigned long end, |
| 247 | struct mm_walk *walk) |
| 248 | { |
| 249 | struct vm_area_struct *vma = walk->vma; |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 250 | const struct mm_walk_ops *ops = walk->ops; |
KOSAKI Motohiro | 6c6d528 | 2011-07-25 17:12:09 -0700 | [diff] [blame] | 251 | |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 252 | if (ops->test_walk) |
| 253 | return ops->test_walk(start, end, walk); |
Naoya Horiguchi | fafaa42 | 2015-02-11 15:27:37 -0800 | [diff] [blame] | 254 | |
| 255 | /* |
Naoya Horiguchi | 48684a6 | 2015-02-11 15:28:06 -0800 | [diff] [blame] | 256 | * vma(VM_PFNMAP) doesn't have any valid struct pages behind VM_PFNMAP |
| 257 | * range, so we don't walk over it as we do for normal vmas. However, |
| 258 | * Some callers are interested in handling hole range and they don't |
| 259 | * want to just ignore any single address range. Such users certainly |
| 260 | * define their ->pte_hole() callbacks, so let's delegate them to handle |
| 261 | * vma(VM_PFNMAP). |
Naoya Horiguchi | fafaa42 | 2015-02-11 15:27:37 -0800 | [diff] [blame] | 262 | */ |
Naoya Horiguchi | 48684a6 | 2015-02-11 15:28:06 -0800 | [diff] [blame] | 263 | if (vma->vm_flags & VM_PFNMAP) { |
| 264 | int err = 1; |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 265 | if (ops->pte_hole) |
| 266 | err = ops->pte_hole(start, end, walk); |
Naoya Horiguchi | 48684a6 | 2015-02-11 15:28:06 -0800 | [diff] [blame] | 267 | return err ? err : 1; |
| 268 | } |
Naoya Horiguchi | fafaa42 | 2015-02-11 15:27:37 -0800 | [diff] [blame] | 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | static int __walk_page_range(unsigned long start, unsigned long end, |
| 273 | struct mm_walk *walk) |
| 274 | { |
| 275 | int err = 0; |
| 276 | struct vm_area_struct *vma = walk->vma; |
Thomas Hellstrom | ecaad8a | 2019-10-01 11:17:34 +0200 | [diff] [blame] | 277 | const struct mm_walk_ops *ops = walk->ops; |
| 278 | |
| 279 | if (vma && ops->pre_vma) { |
| 280 | err = ops->pre_vma(start, end, walk); |
| 281 | if (err) |
| 282 | return err; |
| 283 | } |
Naoya Horiguchi | fafaa42 | 2015-02-11 15:27:37 -0800 | [diff] [blame] | 284 | |
| 285 | if (vma && is_vm_hugetlb_page(vma)) { |
Thomas Hellstrom | ecaad8a | 2019-10-01 11:17:34 +0200 | [diff] [blame] | 286 | if (ops->hugetlb_entry) |
Naoya Horiguchi | fafaa42 | 2015-02-11 15:27:37 -0800 | [diff] [blame] | 287 | err = walk_hugetlb_range(start, end, walk); |
| 288 | } else |
| 289 | err = walk_pgd_range(start, end, walk); |
| 290 | |
Thomas Hellstrom | ecaad8a | 2019-10-01 11:17:34 +0200 | [diff] [blame] | 291 | if (vma && ops->post_vma) |
| 292 | ops->post_vma(walk); |
| 293 | |
Naoya Horiguchi | fafaa42 | 2015-02-11 15:27:37 -0800 | [diff] [blame] | 294 | return err; |
| 295 | } |
Naoya Horiguchi | 116354d | 2010-04-06 14:35:04 -0700 | [diff] [blame] | 296 | |
Matt Mackall | e647309 | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 297 | /** |
Naoya Horiguchi | fafaa42 | 2015-02-11 15:27:37 -0800 | [diff] [blame] | 298 | * walk_page_range - walk page table with caller specific callbacks |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 299 | * @mm: mm_struct representing the target process of page table walk |
| 300 | * @start: start address of the virtual address range |
| 301 | * @end: end address of the virtual address range |
| 302 | * @ops: operation to call during the walk |
| 303 | * @private: private data for callbacks' usage |
Matt Mackall | e647309 | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 304 | * |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 305 | * Recursively walk the page table tree of the process represented by @mm |
Naoya Horiguchi | fafaa42 | 2015-02-11 15:27:37 -0800 | [diff] [blame] | 306 | * within the virtual address range [@start, @end). During walking, we can do |
| 307 | * some caller-specific works for each entry, by setting up pmd_entry(), |
| 308 | * pte_entry(), and/or hugetlb_entry(). If you don't set up for some of these |
| 309 | * callbacks, the associated entries/pages are just ignored. |
| 310 | * The return values of these callbacks are commonly defined like below: |
Mike Rapoport | a5d09be | 2018-02-06 15:42:19 -0800 | [diff] [blame] | 311 | * |
Naoya Horiguchi | fafaa42 | 2015-02-11 15:27:37 -0800 | [diff] [blame] | 312 | * - 0 : succeeded to handle the current entry, and if you don't reach the |
| 313 | * end address yet, continue to walk. |
| 314 | * - >0 : succeeded to handle the current entry, and return to the caller |
| 315 | * with caller specific value. |
| 316 | * - <0 : failed to handle the current entry, and return to the caller |
| 317 | * with error code. |
Matt Mackall | e647309 | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 318 | * |
Naoya Horiguchi | fafaa42 | 2015-02-11 15:27:37 -0800 | [diff] [blame] | 319 | * Before starting to walk page table, some callers want to check whether |
| 320 | * they really want to walk over the current vma, typically by checking |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 321 | * its vm_flags. walk_page_test() and @ops->test_walk() are used for this |
Naoya Horiguchi | fafaa42 | 2015-02-11 15:27:37 -0800 | [diff] [blame] | 322 | * purpose. |
Matt Mackall | e647309 | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 323 | * |
Thomas Hellstrom | ecaad8a | 2019-10-01 11:17:34 +0200 | [diff] [blame] | 324 | * If operations need to be staged before and committed after a vma is walked, |
| 325 | * there are two callbacks, pre_vma() and post_vma(). Note that post_vma(), |
| 326 | * since it is intended to handle commit-type operations, can't return any |
| 327 | * errors. |
| 328 | * |
Naoya Horiguchi | fafaa42 | 2015-02-11 15:27:37 -0800 | [diff] [blame] | 329 | * struct mm_walk keeps current values of some common data like vma and pmd, |
| 330 | * which are useful for the access from callbacks. If you want to pass some |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 331 | * caller-specific data to callbacks, @private should be helpful. |
Matt Mackall | e647309 | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 332 | * |
Naoya Horiguchi | fafaa42 | 2015-02-11 15:27:37 -0800 | [diff] [blame] | 333 | * Locking: |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 334 | * Callers of walk_page_range() and walk_page_vma() should hold @mm->mmap_sem, |
| 335 | * because these function traverse vma list and/or access to vma's data. |
Matt Mackall | e647309 | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 336 | */ |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 337 | int walk_page_range(struct mm_struct *mm, unsigned long start, |
| 338 | unsigned long end, const struct mm_walk_ops *ops, |
| 339 | void *private) |
Matt Mackall | e647309 | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 340 | { |
Matt Mackall | e647309 | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 341 | int err = 0; |
Naoya Horiguchi | fafaa42 | 2015-02-11 15:27:37 -0800 | [diff] [blame] | 342 | unsigned long next; |
| 343 | struct vm_area_struct *vma; |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 344 | struct mm_walk walk = { |
| 345 | .ops = ops, |
| 346 | .mm = mm, |
| 347 | .private = private, |
| 348 | }; |
Matt Mackall | e647309 | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 349 | |
Naoya Horiguchi | fafaa42 | 2015-02-11 15:27:37 -0800 | [diff] [blame] | 350 | if (start >= end) |
| 351 | return -EINVAL; |
Matt Mackall | e647309 | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 352 | |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 353 | if (!walk.mm) |
Dave Hansen | 2165009 | 2008-06-12 15:21:47 -0700 | [diff] [blame] | 354 | return -EINVAL; |
| 355 | |
Christoph Hellwig | b4bc781 | 2019-08-28 16:19:55 +0200 | [diff] [blame] | 356 | lockdep_assert_held(&walk.mm->mmap_sem); |
Cliff Wickman | a9ff785 | 2013-05-24 15:55:36 -0700 | [diff] [blame] | 357 | |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 358 | vma = find_vma(walk.mm, start); |
Matt Mackall | e647309 | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 359 | do { |
Naoya Horiguchi | fafaa42 | 2015-02-11 15:27:37 -0800 | [diff] [blame] | 360 | if (!vma) { /* after the last vma */ |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 361 | walk.vma = NULL; |
Naoya Horiguchi | fafaa42 | 2015-02-11 15:27:37 -0800 | [diff] [blame] | 362 | next = end; |
| 363 | } else if (start < vma->vm_start) { /* outside vma */ |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 364 | walk.vma = NULL; |
Naoya Horiguchi | fafaa42 | 2015-02-11 15:27:37 -0800 | [diff] [blame] | 365 | next = min(end, vma->vm_start); |
| 366 | } else { /* inside vma */ |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 367 | walk.vma = vma; |
Naoya Horiguchi | fafaa42 | 2015-02-11 15:27:37 -0800 | [diff] [blame] | 368 | next = min(end, vma->vm_end); |
| 369 | vma = vma->vm_next; |
David Sterba | 5f0af70 | 2010-11-24 12:57:10 -0800 | [diff] [blame] | 370 | |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 371 | err = walk_page_test(start, next, &walk); |
Naoya Horiguchi | f683739 | 2015-03-25 15:55:14 -0700 | [diff] [blame] | 372 | if (err > 0) { |
| 373 | /* |
| 374 | * positive return values are purely for |
| 375 | * controlling the pagewalk, so should never |
| 376 | * be passed to the callers. |
| 377 | */ |
| 378 | err = 0; |
Cliff Wickman | a9ff785 | 2013-05-24 15:55:36 -0700 | [diff] [blame] | 379 | continue; |
Naoya Horiguchi | f683739 | 2015-03-25 15:55:14 -0700 | [diff] [blame] | 380 | } |
Naoya Horiguchi | fafaa42 | 2015-02-11 15:27:37 -0800 | [diff] [blame] | 381 | if (err < 0) |
Matt Mackall | e647309 | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 382 | break; |
Matt Mackall | e647309 | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 383 | } |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 384 | if (walk.vma || walk.ops->pte_hole) |
| 385 | err = __walk_page_range(start, next, &walk); |
Matt Mackall | e647309 | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 386 | if (err) |
| 387 | break; |
Naoya Horiguchi | fafaa42 | 2015-02-11 15:27:37 -0800 | [diff] [blame] | 388 | } while (start = next, start < end); |
Matt Mackall | e647309 | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 389 | return err; |
| 390 | } |
Naoya Horiguchi | 900fc5f | 2015-02-11 15:27:40 -0800 | [diff] [blame] | 391 | |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 392 | int walk_page_vma(struct vm_area_struct *vma, const struct mm_walk_ops *ops, |
| 393 | void *private) |
Naoya Horiguchi | 900fc5f | 2015-02-11 15:27:40 -0800 | [diff] [blame] | 394 | { |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 395 | struct mm_walk walk = { |
| 396 | .ops = ops, |
| 397 | .mm = vma->vm_mm, |
| 398 | .vma = vma, |
| 399 | .private = private, |
| 400 | }; |
Naoya Horiguchi | 900fc5f | 2015-02-11 15:27:40 -0800 | [diff] [blame] | 401 | int err; |
| 402 | |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 403 | if (!walk.mm) |
Naoya Horiguchi | 900fc5f | 2015-02-11 15:27:40 -0800 | [diff] [blame] | 404 | return -EINVAL; |
| 405 | |
Christoph Hellwig | b4bc781 | 2019-08-28 16:19:55 +0200 | [diff] [blame] | 406 | lockdep_assert_held(&walk.mm->mmap_sem); |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 407 | |
| 408 | err = walk_page_test(vma->vm_start, vma->vm_end, &walk); |
Naoya Horiguchi | 900fc5f | 2015-02-11 15:27:40 -0800 | [diff] [blame] | 409 | if (err > 0) |
| 410 | return 0; |
| 411 | if (err < 0) |
| 412 | return err; |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 413 | return __walk_page_range(vma->vm_start, vma->vm_end, &walk); |
Naoya Horiguchi | 900fc5f | 2015-02-11 15:27:40 -0800 | [diff] [blame] | 414 | } |
Thomas Hellstrom | ecaad8a | 2019-10-01 11:17:34 +0200 | [diff] [blame] | 415 | |
| 416 | /** |
| 417 | * walk_page_mapping - walk all memory areas mapped into a struct address_space. |
| 418 | * @mapping: Pointer to the struct address_space |
| 419 | * @first_index: First page offset in the address_space |
| 420 | * @nr: Number of incremental page offsets to cover |
| 421 | * @ops: operation to call during the walk |
| 422 | * @private: private data for callbacks' usage |
| 423 | * |
| 424 | * This function walks all memory areas mapped into a struct address_space. |
| 425 | * The walk is limited to only the given page-size index range, but if |
| 426 | * the index boundaries cross a huge page-table entry, that entry will be |
| 427 | * included. |
| 428 | * |
| 429 | * Also see walk_page_range() for additional information. |
| 430 | * |
| 431 | * Locking: |
| 432 | * This function can't require that the struct mm_struct::mmap_sem is held, |
| 433 | * since @mapping may be mapped by multiple processes. Instead |
| 434 | * @mapping->i_mmap_rwsem must be held. This might have implications in the |
| 435 | * callbacks, and it's up tho the caller to ensure that the |
| 436 | * struct mm_struct::mmap_sem is not needed. |
| 437 | * |
| 438 | * Also this means that a caller can't rely on the struct |
| 439 | * vm_area_struct::vm_flags to be constant across a call, |
| 440 | * except for immutable flags. Callers requiring this shouldn't use |
| 441 | * this function. |
| 442 | * |
| 443 | * Return: 0 on success, negative error code on failure, positive number on |
| 444 | * caller defined premature termination. |
| 445 | */ |
| 446 | int walk_page_mapping(struct address_space *mapping, pgoff_t first_index, |
| 447 | pgoff_t nr, const struct mm_walk_ops *ops, |
| 448 | void *private) |
| 449 | { |
| 450 | struct mm_walk walk = { |
| 451 | .ops = ops, |
| 452 | .private = private, |
| 453 | }; |
| 454 | struct vm_area_struct *vma; |
| 455 | pgoff_t vba, vea, cba, cea; |
| 456 | unsigned long start_addr, end_addr; |
| 457 | int err = 0; |
| 458 | |
| 459 | lockdep_assert_held(&mapping->i_mmap_rwsem); |
| 460 | vma_interval_tree_foreach(vma, &mapping->i_mmap, first_index, |
| 461 | first_index + nr - 1) { |
| 462 | /* Clip to the vma */ |
| 463 | vba = vma->vm_pgoff; |
| 464 | vea = vba + vma_pages(vma); |
| 465 | cba = first_index; |
| 466 | cba = max(cba, vba); |
| 467 | cea = first_index + nr; |
| 468 | cea = min(cea, vea); |
| 469 | |
| 470 | start_addr = ((cba - vba) << PAGE_SHIFT) + vma->vm_start; |
| 471 | end_addr = ((cea - vba) << PAGE_SHIFT) + vma->vm_start; |
| 472 | if (start_addr >= end_addr) |
| 473 | continue; |
| 474 | |
| 475 | walk.vma = vma; |
| 476 | walk.mm = vma->vm_mm; |
| 477 | |
| 478 | err = walk_page_test(vma->vm_start, vma->vm_end, &walk); |
| 479 | if (err > 0) { |
| 480 | err = 0; |
| 481 | break; |
| 482 | } else if (err < 0) |
| 483 | break; |
| 484 | |
| 485 | err = __walk_page_range(start_addr, end_addr, &walk); |
| 486 | if (err) |
| 487 | break; |
| 488 | } |
| 489 | |
| 490 | return err; |
| 491 | } |