Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Lockless get_user_pages_fast for x86 |
| 3 | * |
| 4 | * Copyright (C) 2008 Nick Piggin |
| 5 | * Copyright (C) 2008 Novell Inc. |
| 6 | */ |
| 7 | #include <linux/sched.h> |
| 8 | #include <linux/mm.h> |
| 9 | #include <linux/vmstat.h> |
| 10 | #include <linux/highmem.h> |
Andrea Arcangeli | 8ee5382 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 11 | #include <linux/swap.h> |
Dan Williams | 3565fce | 2016-01-15 16:56:55 -0800 | [diff] [blame] | 12 | #include <linux/memremap.h> |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 13 | |
Dave Hansen | 33a709b | 2016-02-12 13:02:19 -0800 | [diff] [blame] | 14 | #include <asm/mmu_context.h> |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 15 | #include <asm/pgtable.h> |
| 16 | |
| 17 | static inline pte_t gup_get_pte(pte_t *ptep) |
| 18 | { |
| 19 | #ifndef CONFIG_X86_PAE |
Christian Borntraeger | 14cf3d9 | 2014-11-21 16:29:40 +0100 | [diff] [blame] | 20 | return READ_ONCE(*ptep); |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 21 | #else |
| 22 | /* |
| 23 | * With get_user_pages_fast, we walk down the pagetables without taking |
Andy Shevchenko | ab09809 | 2010-02-02 14:38:12 -0800 | [diff] [blame] | 24 | * any locks. For this we would like to load the pointers atomically, |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 25 | * but that is not possible (without expensive cmpxchg8b) on PAE. What |
| 26 | * we do have is the guarantee that a pte will only either go from not |
| 27 | * present to present, or present to not present or both -- it will not |
| 28 | * switch to a completely different present page without a TLB flush in |
| 29 | * between; something that we are blocking by holding interrupts off. |
| 30 | * |
| 31 | * Setting ptes from not present to present goes: |
| 32 | * ptep->pte_high = h; |
| 33 | * smp_wmb(); |
| 34 | * ptep->pte_low = l; |
| 35 | * |
| 36 | * And present to not present goes: |
| 37 | * ptep->pte_low = 0; |
| 38 | * smp_wmb(); |
| 39 | * ptep->pte_high = 0; |
| 40 | * |
| 41 | * We must ensure here that the load of pte_low sees l iff pte_high |
| 42 | * sees h. We load pte_high *after* loading pte_low, which ensures we |
| 43 | * don't see an older value of pte_high. *Then* we recheck pte_low, |
| 44 | * which ensures that we haven't picked up a changed pte high. We might |
| 45 | * have got rubbish values from pte_low and pte_high, but we are |
| 46 | * guaranteed that pte_low will not have the present bit set *unless* |
| 47 | * it is 'l'. And get_user_pages_fast only operates on present ptes, so |
| 48 | * we're safe. |
| 49 | * |
| 50 | * gup_get_pte should not be used or copied outside gup.c without being |
| 51 | * very careful -- it does not atomically load the pte or anything that |
| 52 | * is likely to be useful for you. |
| 53 | */ |
| 54 | pte_t pte; |
| 55 | |
| 56 | retry: |
| 57 | pte.pte_low = ptep->pte_low; |
| 58 | smp_rmb(); |
| 59 | pte.pte_high = ptep->pte_high; |
| 60 | smp_rmb(); |
| 61 | if (unlikely(pte.pte_low != ptep->pte_low)) |
| 62 | goto retry; |
| 63 | |
| 64 | return pte; |
| 65 | #endif |
| 66 | } |
| 67 | |
Dan Williams | 3565fce | 2016-01-15 16:56:55 -0800 | [diff] [blame] | 68 | static void undo_dev_pagemap(int *nr, int nr_start, struct page **pages) |
| 69 | { |
| 70 | while ((*nr) - nr_start) { |
| 71 | struct page *page = pages[--(*nr)]; |
| 72 | |
| 73 | ClearPageReferenced(page); |
| 74 | put_page(page); |
| 75 | } |
| 76 | } |
| 77 | |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 78 | /* |
Dave Hansen | 1874f68 | 2016-02-12 13:02:18 -0800 | [diff] [blame] | 79 | * 'pteval' can come from a pte, pmd or pud. We only check |
| 80 | * _PAGE_PRESENT, _PAGE_USER, and _PAGE_RW in here which are the |
| 81 | * same value on all 3 types. |
| 82 | */ |
| 83 | static inline int pte_allows_gup(unsigned long pteval, int write) |
| 84 | { |
| 85 | unsigned long need_pte_bits = _PAGE_PRESENT|_PAGE_USER; |
| 86 | |
| 87 | if (write) |
| 88 | need_pte_bits |= _PAGE_RW; |
| 89 | |
| 90 | if ((pteval & need_pte_bits) != need_pte_bits) |
| 91 | return 0; |
| 92 | |
Dave Hansen | 33a709b | 2016-02-12 13:02:19 -0800 | [diff] [blame] | 93 | /* Check memory protection keys permissions. */ |
| 94 | if (!__pkru_allows_pkey(pte_flags_pkey(pteval), write)) |
| 95 | return 0; |
| 96 | |
Dave Hansen | 1874f68 | 2016-02-12 13:02:18 -0800 | [diff] [blame] | 97 | return 1; |
| 98 | } |
| 99 | |
| 100 | /* |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 101 | * The performance critical leaf functions are made noinline otherwise gcc |
| 102 | * inlines everything into a single function which results in too much |
| 103 | * register pressure. |
| 104 | */ |
| 105 | static noinline int gup_pte_range(pmd_t pmd, unsigned long addr, |
| 106 | unsigned long end, int write, struct page **pages, int *nr) |
| 107 | { |
Dan Williams | 3565fce | 2016-01-15 16:56:55 -0800 | [diff] [blame] | 108 | struct dev_pagemap *pgmap = NULL; |
Dan Williams | b2e593e | 2017-03-09 16:16:45 -0800 | [diff] [blame^] | 109 | int nr_start = *nr, ret = 0; |
| 110 | pte_t *ptep, *ptem; |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 111 | |
Dan Williams | b2e593e | 2017-03-09 16:16:45 -0800 | [diff] [blame^] | 112 | /* |
| 113 | * Keep the original mapped PTE value (ptem) around since we |
| 114 | * might increment ptep off the end of the page when finishing |
| 115 | * our loop iteration. |
| 116 | */ |
| 117 | ptem = ptep = pte_offset_map(&pmd, addr); |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 118 | do { |
| 119 | pte_t pte = gup_get_pte(ptep); |
| 120 | struct page *page; |
| 121 | |
Mel Gorman | 2b4847e | 2013-12-18 17:08:32 -0800 | [diff] [blame] | 122 | /* Similar to the PMD case, NUMA hinting must take slow path */ |
Dan Williams | b2e593e | 2017-03-09 16:16:45 -0800 | [diff] [blame^] | 123 | if (pte_protnone(pte)) |
| 124 | break; |
Mel Gorman | 2b4847e | 2013-12-18 17:08:32 -0800 | [diff] [blame] | 125 | |
Dan Williams | b2e593e | 2017-03-09 16:16:45 -0800 | [diff] [blame^] | 126 | if (!pte_allows_gup(pte_val(pte), write)) |
| 127 | break; |
Dan Williams | ef947b2 | 2017-03-09 16:16:42 -0800 | [diff] [blame] | 128 | |
Dan Williams | 3565fce | 2016-01-15 16:56:55 -0800 | [diff] [blame] | 129 | if (pte_devmap(pte)) { |
| 130 | pgmap = get_dev_pagemap(pte_pfn(pte), pgmap); |
| 131 | if (unlikely(!pgmap)) { |
| 132 | undo_dev_pagemap(nr, nr_start, pages); |
Dan Williams | b2e593e | 2017-03-09 16:16:45 -0800 | [diff] [blame^] | 133 | break; |
Dan Williams | 3565fce | 2016-01-15 16:56:55 -0800 | [diff] [blame] | 134 | } |
Dan Williams | b2e593e | 2017-03-09 16:16:45 -0800 | [diff] [blame^] | 135 | } else if (pte_special(pte)) |
| 136 | break; |
| 137 | |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 138 | VM_BUG_ON(!pfn_valid(pte_pfn(pte))); |
Hugh Dickins | 457a98b | 2016-02-17 13:11:23 -0800 | [diff] [blame] | 139 | page = pte_page(pte); |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 140 | get_page(page); |
Dan Williams | 3565fce | 2016-01-15 16:56:55 -0800 | [diff] [blame] | 141 | put_dev_pagemap(pgmap); |
Andrea Arcangeli | 8ee5382 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 142 | SetPageReferenced(page); |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 143 | pages[*nr] = page; |
| 144 | (*nr)++; |
| 145 | |
| 146 | } while (ptep++, addr += PAGE_SIZE, addr != end); |
Dan Williams | b2e593e | 2017-03-09 16:16:45 -0800 | [diff] [blame^] | 147 | if (addr == end) |
| 148 | ret = 1; |
| 149 | pte_unmap(ptem); |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 150 | |
Dan Williams | b2e593e | 2017-03-09 16:16:45 -0800 | [diff] [blame^] | 151 | return ret; |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | static inline void get_head_page_multiple(struct page *page, int nr) |
| 155 | { |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 156 | VM_BUG_ON_PAGE(page != compound_head(page), page); |
| 157 | VM_BUG_ON_PAGE(page_count(page) == 0, page); |
Joonsoo Kim | fe896d1 | 2016-03-17 14:19:26 -0700 | [diff] [blame] | 158 | page_ref_add(page, nr); |
Andrea Arcangeli | 8ee5382 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 159 | SetPageReferenced(page); |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Dan Williams | 220ced1 | 2017-02-24 14:57:12 -0800 | [diff] [blame] | 162 | static int __gup_device_huge(unsigned long pfn, unsigned long addr, |
Dan Williams | 3565fce | 2016-01-15 16:56:55 -0800 | [diff] [blame] | 163 | unsigned long end, struct page **pages, int *nr) |
| 164 | { |
| 165 | int nr_start = *nr; |
Dan Williams | 3565fce | 2016-01-15 16:56:55 -0800 | [diff] [blame] | 166 | struct dev_pagemap *pgmap = NULL; |
| 167 | |
Dan Williams | 3565fce | 2016-01-15 16:56:55 -0800 | [diff] [blame] | 168 | do { |
| 169 | struct page *page = pfn_to_page(pfn); |
| 170 | |
| 171 | pgmap = get_dev_pagemap(pfn, pgmap); |
| 172 | if (unlikely(!pgmap)) { |
| 173 | undo_dev_pagemap(nr, nr_start, pages); |
| 174 | return 0; |
| 175 | } |
| 176 | SetPageReferenced(page); |
| 177 | pages[*nr] = page; |
| 178 | get_page(page); |
| 179 | put_dev_pagemap(pgmap); |
| 180 | (*nr)++; |
| 181 | pfn++; |
| 182 | } while (addr += PAGE_SIZE, addr != end); |
| 183 | return 1; |
| 184 | } |
| 185 | |
Dan Williams | 220ced1 | 2017-02-24 14:57:12 -0800 | [diff] [blame] | 186 | static int __gup_device_huge_pmd(pmd_t pmd, unsigned long addr, |
| 187 | unsigned long end, struct page **pages, int *nr) |
| 188 | { |
| 189 | unsigned long fault_pfn; |
| 190 | |
| 191 | fault_pfn = pmd_pfn(pmd) + ((addr & ~PMD_MASK) >> PAGE_SHIFT); |
| 192 | return __gup_device_huge(fault_pfn, addr, end, pages, nr); |
| 193 | } |
| 194 | |
| 195 | static int __gup_device_huge_pud(pud_t pud, unsigned long addr, |
| 196 | unsigned long end, struct page **pages, int *nr) |
| 197 | { |
| 198 | unsigned long fault_pfn; |
| 199 | |
| 200 | fault_pfn = pud_pfn(pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT); |
| 201 | return __gup_device_huge(fault_pfn, addr, end, pages, nr); |
| 202 | } |
| 203 | |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 204 | static noinline int gup_huge_pmd(pmd_t pmd, unsigned long addr, |
| 205 | unsigned long end, int write, struct page **pages, int *nr) |
| 206 | { |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 207 | struct page *head, *page; |
| 208 | int refs; |
| 209 | |
Dave Hansen | 1874f68 | 2016-02-12 13:02:18 -0800 | [diff] [blame] | 210 | if (!pte_allows_gup(pmd_val(pmd), write)) |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 211 | return 0; |
Dan Williams | 3565fce | 2016-01-15 16:56:55 -0800 | [diff] [blame] | 212 | |
| 213 | VM_BUG_ON(!pfn_valid(pmd_pfn(pmd))); |
| 214 | if (pmd_devmap(pmd)) |
| 215 | return __gup_device_huge_pmd(pmd, addr, end, pages, nr); |
| 216 | |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 217 | /* hugepages are never "special" */ |
Toshi Kani | daf3e35 | 2015-09-17 12:24:21 -0600 | [diff] [blame] | 218 | VM_BUG_ON(pmd_flags(pmd) & _PAGE_SPECIAL); |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 219 | |
| 220 | refs = 0; |
Toshi Kani | daf3e35 | 2015-09-17 12:24:21 -0600 | [diff] [blame] | 221 | head = pmd_page(pmd); |
Nick Piggin | 652ea69 | 2008-07-25 19:45:27 -0700 | [diff] [blame] | 222 | page = head + ((addr & ~PMD_MASK) >> PAGE_SHIFT); |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 223 | do { |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 224 | VM_BUG_ON_PAGE(compound_head(page) != head, page); |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 225 | pages[*nr] = page; |
| 226 | (*nr)++; |
| 227 | page++; |
| 228 | refs++; |
| 229 | } while (addr += PAGE_SIZE, addr != end); |
| 230 | get_head_page_multiple(head, refs); |
| 231 | |
| 232 | return 1; |
| 233 | } |
| 234 | |
| 235 | static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end, |
| 236 | int write, struct page **pages, int *nr) |
| 237 | { |
| 238 | unsigned long next; |
| 239 | pmd_t *pmdp; |
| 240 | |
| 241 | pmdp = pmd_offset(&pud, addr); |
| 242 | do { |
| 243 | pmd_t pmd = *pmdp; |
| 244 | |
| 245 | next = pmd_addr_end(addr, end); |
Kirill A. Shutemov | 1f19617 | 2016-01-15 16:53:35 -0800 | [diff] [blame] | 246 | if (pmd_none(pmd)) |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 247 | return 0; |
Naoya Horiguchi | cbef847 | 2015-02-11 15:25:19 -0800 | [diff] [blame] | 248 | if (unlikely(pmd_large(pmd) || !pmd_present(pmd))) { |
Mel Gorman | 2b4847e | 2013-12-18 17:08:32 -0800 | [diff] [blame] | 249 | /* |
| 250 | * NUMA hinting faults need to be handled in the GUP |
| 251 | * slowpath for accounting purposes and so that they |
| 252 | * can be serialised against THP migration. |
| 253 | */ |
Mel Gorman | 8a0516e | 2015-02-12 14:58:22 -0800 | [diff] [blame] | 254 | if (pmd_protnone(pmd)) |
Mel Gorman | 2b4847e | 2013-12-18 17:08:32 -0800 | [diff] [blame] | 255 | return 0; |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 256 | if (!gup_huge_pmd(pmd, addr, next, write, pages, nr)) |
| 257 | return 0; |
| 258 | } else { |
| 259 | if (!gup_pte_range(pmd, addr, next, write, pages, nr)) |
| 260 | return 0; |
| 261 | } |
| 262 | } while (pmdp++, addr = next, addr != end); |
| 263 | |
| 264 | return 1; |
| 265 | } |
| 266 | |
Nick Piggin | 652ea69 | 2008-07-25 19:45:27 -0700 | [diff] [blame] | 267 | static noinline int gup_huge_pud(pud_t pud, unsigned long addr, |
| 268 | unsigned long end, int write, struct page **pages, int *nr) |
| 269 | { |
Nick Piggin | 652ea69 | 2008-07-25 19:45:27 -0700 | [diff] [blame] | 270 | struct page *head, *page; |
| 271 | int refs; |
| 272 | |
Dave Hansen | 1874f68 | 2016-02-12 13:02:18 -0800 | [diff] [blame] | 273 | if (!pte_allows_gup(pud_val(pud), write)) |
Nick Piggin | 652ea69 | 2008-07-25 19:45:27 -0700 | [diff] [blame] | 274 | return 0; |
Dan Williams | 220ced1 | 2017-02-24 14:57:12 -0800 | [diff] [blame] | 275 | |
| 276 | VM_BUG_ON(!pfn_valid(pud_pfn(pud))); |
| 277 | if (pud_devmap(pud)) |
| 278 | return __gup_device_huge_pud(pud, addr, end, pages, nr); |
| 279 | |
Nick Piggin | 652ea69 | 2008-07-25 19:45:27 -0700 | [diff] [blame] | 280 | /* hugepages are never "special" */ |
Toshi Kani | daf3e35 | 2015-09-17 12:24:21 -0600 | [diff] [blame] | 281 | VM_BUG_ON(pud_flags(pud) & _PAGE_SPECIAL); |
Nick Piggin | 652ea69 | 2008-07-25 19:45:27 -0700 | [diff] [blame] | 282 | |
| 283 | refs = 0; |
Toshi Kani | daf3e35 | 2015-09-17 12:24:21 -0600 | [diff] [blame] | 284 | head = pud_page(pud); |
Nick Piggin | 652ea69 | 2008-07-25 19:45:27 -0700 | [diff] [blame] | 285 | page = head + ((addr & ~PUD_MASK) >> PAGE_SHIFT); |
| 286 | do { |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 287 | VM_BUG_ON_PAGE(compound_head(page) != head, page); |
Nick Piggin | 652ea69 | 2008-07-25 19:45:27 -0700 | [diff] [blame] | 288 | pages[*nr] = page; |
| 289 | (*nr)++; |
| 290 | page++; |
| 291 | refs++; |
| 292 | } while (addr += PAGE_SIZE, addr != end); |
| 293 | get_head_page_multiple(head, refs); |
| 294 | |
| 295 | return 1; |
| 296 | } |
| 297 | |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 298 | static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end, |
| 299 | int write, struct page **pages, int *nr) |
| 300 | { |
| 301 | unsigned long next; |
| 302 | pud_t *pudp; |
| 303 | |
| 304 | pudp = pud_offset(&pgd, addr); |
| 305 | do { |
| 306 | pud_t pud = *pudp; |
| 307 | |
| 308 | next = pud_addr_end(addr, end); |
| 309 | if (pud_none(pud)) |
| 310 | return 0; |
Nick Piggin | 652ea69 | 2008-07-25 19:45:27 -0700 | [diff] [blame] | 311 | if (unlikely(pud_large(pud))) { |
| 312 | if (!gup_huge_pud(pud, addr, next, write, pages, nr)) |
| 313 | return 0; |
| 314 | } else { |
| 315 | if (!gup_pmd_range(pud, addr, next, write, pages, nr)) |
| 316 | return 0; |
| 317 | } |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 318 | } while (pudp++, addr = next, addr != end); |
| 319 | |
| 320 | return 1; |
| 321 | } |
| 322 | |
Peter Zijlstra | 465a454 | 2009-06-15 12:31:37 +0200 | [diff] [blame] | 323 | /* |
| 324 | * Like get_user_pages_fast() except its IRQ-safe in that it won't fall |
| 325 | * back to the regular GUP. |
| 326 | */ |
| 327 | int __get_user_pages_fast(unsigned long start, int nr_pages, int write, |
| 328 | struct page **pages) |
| 329 | { |
| 330 | struct mm_struct *mm = current->mm; |
| 331 | unsigned long addr, len, end; |
| 332 | unsigned long next; |
| 333 | unsigned long flags; |
| 334 | pgd_t *pgdp; |
| 335 | int nr = 0; |
| 336 | |
| 337 | start &= PAGE_MASK; |
| 338 | addr = start; |
| 339 | len = (unsigned long) nr_pages << PAGE_SHIFT; |
| 340 | end = start + len; |
| 341 | if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ, |
| 342 | (void __user *)start, len))) |
| 343 | return 0; |
| 344 | |
| 345 | /* |
| 346 | * XXX: batch / limit 'nr', to avoid large irq off latency |
| 347 | * needs some instrumenting to determine the common sizes used by |
| 348 | * important workloads (eg. DB2), and whether limiting the batch size |
| 349 | * will decrease performance. |
| 350 | * |
| 351 | * It seems like we're in the clear for the moment. Direct-IO is |
| 352 | * the main guy that batches up lots of get_user_pages, and even |
| 353 | * they are limited to 64-at-a-time which is not so many. |
| 354 | */ |
| 355 | /* |
| 356 | * This doesn't prevent pagetable teardown, but does prevent |
| 357 | * the pagetables and pages from being freed on x86. |
| 358 | * |
| 359 | * So long as we atomically load page table pointers versus teardown |
| 360 | * (which we do on x86, with the above PAE exception), we can follow the |
| 361 | * address down to the the page and take a ref on it. |
| 362 | */ |
| 363 | local_irq_save(flags); |
| 364 | pgdp = pgd_offset(mm, addr); |
| 365 | do { |
| 366 | pgd_t pgd = *pgdp; |
| 367 | |
| 368 | next = pgd_addr_end(addr, end); |
| 369 | if (pgd_none(pgd)) |
| 370 | break; |
| 371 | if (!gup_pud_range(pgd, addr, next, write, pages, &nr)) |
| 372 | break; |
| 373 | } while (pgdp++, addr = next, addr != end); |
| 374 | local_irq_restore(flags); |
| 375 | |
| 376 | return nr; |
| 377 | } |
| 378 | |
Andy Grover | a0d22f4 | 2009-04-09 16:45:29 -0700 | [diff] [blame] | 379 | /** |
| 380 | * get_user_pages_fast() - pin user pages in memory |
| 381 | * @start: starting user address |
| 382 | * @nr_pages: number of pages from start to pin |
| 383 | * @write: whether pages will be written to |
| 384 | * @pages: array that receives pointers to the pages pinned. |
| 385 | * Should be at least nr_pages long. |
| 386 | * |
| 387 | * Attempt to pin user pages in memory without taking mm->mmap_sem. |
| 388 | * If not successful, it will fall back to taking the lock and |
| 389 | * calling get_user_pages(). |
| 390 | * |
| 391 | * Returns number of pages pinned. This may be fewer than the number |
| 392 | * requested. If nr_pages is 0 or negative, returns 0. If no pages |
| 393 | * were pinned, returns -errno. |
| 394 | */ |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 395 | int get_user_pages_fast(unsigned long start, int nr_pages, int write, |
| 396 | struct page **pages) |
| 397 | { |
| 398 | struct mm_struct *mm = current->mm; |
Linus Torvalds | 9b79022 | 2008-07-28 17:54:21 -0700 | [diff] [blame] | 399 | unsigned long addr, len, end; |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 400 | unsigned long next; |
| 401 | pgd_t *pgdp; |
| 402 | int nr = 0; |
| 403 | |
Linus Torvalds | 9b79022 | 2008-07-28 17:54:21 -0700 | [diff] [blame] | 404 | start &= PAGE_MASK; |
| 405 | addr = start; |
| 406 | len = (unsigned long) nr_pages << PAGE_SHIFT; |
Linus Torvalds | 7f81890 | 2009-06-20 09:52:27 -0700 | [diff] [blame] | 407 | |
Linus Torvalds | 9b79022 | 2008-07-28 17:54:21 -0700 | [diff] [blame] | 408 | end = start + len; |
Linus Torvalds | 7f81890 | 2009-06-20 09:52:27 -0700 | [diff] [blame] | 409 | if (end < start) |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 410 | goto slow_irqon; |
| 411 | |
Linus Torvalds | 7f81890 | 2009-06-20 09:52:27 -0700 | [diff] [blame] | 412 | #ifdef CONFIG_X86_64 |
| 413 | if (end >> __VIRTUAL_MASK_SHIFT) |
| 414 | goto slow_irqon; |
| 415 | #endif |
| 416 | |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 417 | /* |
| 418 | * XXX: batch / limit 'nr', to avoid large irq off latency |
| 419 | * needs some instrumenting to determine the common sizes used by |
| 420 | * important workloads (eg. DB2), and whether limiting the batch size |
| 421 | * will decrease performance. |
| 422 | * |
| 423 | * It seems like we're in the clear for the moment. Direct-IO is |
| 424 | * the main guy that batches up lots of get_user_pages, and even |
| 425 | * they are limited to 64-at-a-time which is not so many. |
| 426 | */ |
| 427 | /* |
| 428 | * This doesn't prevent pagetable teardown, but does prevent |
| 429 | * the pagetables and pages from being freed on x86. |
| 430 | * |
| 431 | * So long as we atomically load page table pointers versus teardown |
| 432 | * (which we do on x86, with the above PAE exception), we can follow the |
| 433 | * address down to the the page and take a ref on it. |
| 434 | */ |
| 435 | local_irq_disable(); |
| 436 | pgdp = pgd_offset(mm, addr); |
| 437 | do { |
| 438 | pgd_t pgd = *pgdp; |
| 439 | |
| 440 | next = pgd_addr_end(addr, end); |
| 441 | if (pgd_none(pgd)) |
| 442 | goto slow; |
| 443 | if (!gup_pud_range(pgd, addr, next, write, pages, &nr)) |
| 444 | goto slow; |
| 445 | } while (pgdp++, addr = next, addr != end); |
| 446 | local_irq_enable(); |
| 447 | |
| 448 | VM_BUG_ON(nr != (end - start) >> PAGE_SHIFT); |
| 449 | return nr; |
| 450 | |
| 451 | { |
| 452 | int ret; |
| 453 | |
| 454 | slow: |
| 455 | local_irq_enable(); |
| 456 | slow_irqon: |
| 457 | /* Try to get the remaining pages with get_user_pages */ |
| 458 | start += nr << PAGE_SHIFT; |
| 459 | pages += nr; |
| 460 | |
Dave Hansen | d4edcf0 | 2016-02-12 13:01:56 -0800 | [diff] [blame] | 461 | ret = get_user_pages_unlocked(start, |
Andrea Arcangeli | a7b7807 | 2015-02-11 15:27:23 -0800 | [diff] [blame] | 462 | (end - start) >> PAGE_SHIFT, |
Lorenzo Stoakes | c164154 | 2016-10-13 01:20:13 +0100 | [diff] [blame] | 463 | pages, write ? FOLL_WRITE : 0); |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 464 | |
| 465 | /* Have to be a bit careful with return values */ |
| 466 | if (nr > 0) { |
| 467 | if (ret < 0) |
| 468 | ret = nr; |
| 469 | else |
| 470 | ret += nr; |
| 471 | } |
| 472 | |
| 473 | return ret; |
| 474 | } |
| 475 | } |