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> |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 12 | |
| 13 | #include <asm/pgtable.h> |
| 14 | |
| 15 | static inline pte_t gup_get_pte(pte_t *ptep) |
| 16 | { |
| 17 | #ifndef CONFIG_X86_PAE |
Ingo Molnar | 0c87197 | 2009-06-15 11:35:01 +0200 | [diff] [blame] | 18 | return ACCESS_ONCE(*ptep); |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 19 | #else |
| 20 | /* |
| 21 | * With get_user_pages_fast, we walk down the pagetables without taking |
Andy Shevchenko | ab09809 | 2010-02-02 14:38:12 -0800 | [diff] [blame] | 22 | * any locks. For this we would like to load the pointers atomically, |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 23 | * but that is not possible (without expensive cmpxchg8b) on PAE. What |
| 24 | * we do have is the guarantee that a pte will only either go from not |
| 25 | * present to present, or present to not present or both -- it will not |
| 26 | * switch to a completely different present page without a TLB flush in |
| 27 | * between; something that we are blocking by holding interrupts off. |
| 28 | * |
| 29 | * Setting ptes from not present to present goes: |
| 30 | * ptep->pte_high = h; |
| 31 | * smp_wmb(); |
| 32 | * ptep->pte_low = l; |
| 33 | * |
| 34 | * And present to not present goes: |
| 35 | * ptep->pte_low = 0; |
| 36 | * smp_wmb(); |
| 37 | * ptep->pte_high = 0; |
| 38 | * |
| 39 | * We must ensure here that the load of pte_low sees l iff pte_high |
| 40 | * sees h. We load pte_high *after* loading pte_low, which ensures we |
| 41 | * don't see an older value of pte_high. *Then* we recheck pte_low, |
| 42 | * which ensures that we haven't picked up a changed pte high. We might |
| 43 | * have got rubbish values from pte_low and pte_high, but we are |
| 44 | * guaranteed that pte_low will not have the present bit set *unless* |
| 45 | * it is 'l'. And get_user_pages_fast only operates on present ptes, so |
| 46 | * we're safe. |
| 47 | * |
| 48 | * gup_get_pte should not be used or copied outside gup.c without being |
| 49 | * very careful -- it does not atomically load the pte or anything that |
| 50 | * is likely to be useful for you. |
| 51 | */ |
| 52 | pte_t pte; |
| 53 | |
| 54 | retry: |
| 55 | pte.pte_low = ptep->pte_low; |
| 56 | smp_rmb(); |
| 57 | pte.pte_high = ptep->pte_high; |
| 58 | smp_rmb(); |
| 59 | if (unlikely(pte.pte_low != ptep->pte_low)) |
| 60 | goto retry; |
| 61 | |
| 62 | return pte; |
| 63 | #endif |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | * The performance critical leaf functions are made noinline otherwise gcc |
| 68 | * inlines everything into a single function which results in too much |
| 69 | * register pressure. |
| 70 | */ |
| 71 | static noinline int gup_pte_range(pmd_t pmd, unsigned long addr, |
| 72 | unsigned long end, int write, struct page **pages, int *nr) |
| 73 | { |
| 74 | unsigned long mask; |
| 75 | pte_t *ptep; |
| 76 | |
| 77 | mask = _PAGE_PRESENT|_PAGE_USER; |
| 78 | if (write) |
| 79 | mask |= _PAGE_RW; |
| 80 | |
| 81 | ptep = pte_offset_map(&pmd, addr); |
| 82 | do { |
| 83 | pte_t pte = gup_get_pte(ptep); |
| 84 | struct page *page; |
| 85 | |
Jan Beulich | 606ee44 | 2008-09-17 16:48:17 +0100 | [diff] [blame] | 86 | if ((pte_flags(pte) & (mask | _PAGE_SPECIAL)) != mask) { |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 87 | pte_unmap(ptep); |
| 88 | return 0; |
| 89 | } |
| 90 | VM_BUG_ON(!pfn_valid(pte_pfn(pte))); |
| 91 | page = pte_page(pte); |
| 92 | get_page(page); |
Andrea Arcangeli | 8ee5382 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 93 | SetPageReferenced(page); |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 94 | pages[*nr] = page; |
| 95 | (*nr)++; |
| 96 | |
| 97 | } while (ptep++, addr += PAGE_SIZE, addr != end); |
| 98 | pte_unmap(ptep - 1); |
| 99 | |
| 100 | return 1; |
| 101 | } |
| 102 | |
| 103 | static inline void get_head_page_multiple(struct page *page, int nr) |
| 104 | { |
| 105 | VM_BUG_ON(page != compound_head(page)); |
| 106 | VM_BUG_ON(page_count(page) == 0); |
| 107 | atomic_add(nr, &page->_count); |
Andrea Arcangeli | 8ee5382 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 108 | SetPageReferenced(page); |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | static noinline int gup_huge_pmd(pmd_t pmd, unsigned long addr, |
| 112 | unsigned long end, int write, struct page **pages, int *nr) |
| 113 | { |
| 114 | unsigned long mask; |
| 115 | pte_t pte = *(pte_t *)&pmd; |
| 116 | struct page *head, *page; |
| 117 | int refs; |
| 118 | |
| 119 | mask = _PAGE_PRESENT|_PAGE_USER; |
| 120 | if (write) |
| 121 | mask |= _PAGE_RW; |
Jan Beulich | 606ee44 | 2008-09-17 16:48:17 +0100 | [diff] [blame] | 122 | if ((pte_flags(pte) & mask) != mask) |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 123 | return 0; |
| 124 | /* hugepages are never "special" */ |
Jan Beulich | 606ee44 | 2008-09-17 16:48:17 +0100 | [diff] [blame] | 125 | VM_BUG_ON(pte_flags(pte) & _PAGE_SPECIAL); |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 126 | VM_BUG_ON(!pfn_valid(pte_pfn(pte))); |
| 127 | |
| 128 | refs = 0; |
| 129 | head = pte_page(pte); |
Nick Piggin | 652ea69 | 2008-07-25 19:45:27 -0700 | [diff] [blame] | 130 | page = head + ((addr & ~PMD_MASK) >> PAGE_SHIFT); |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 131 | do { |
| 132 | VM_BUG_ON(compound_head(page) != head); |
| 133 | pages[*nr] = page; |
Andrea Arcangeli | 9180706 | 2011-01-13 15:46:32 -0800 | [diff] [blame] | 134 | if (PageTail(page)) |
| 135 | get_huge_page_tail(page); |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 136 | (*nr)++; |
| 137 | page++; |
| 138 | refs++; |
| 139 | } while (addr += PAGE_SIZE, addr != end); |
| 140 | get_head_page_multiple(head, refs); |
| 141 | |
| 142 | return 1; |
| 143 | } |
| 144 | |
| 145 | static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end, |
| 146 | int write, struct page **pages, int *nr) |
| 147 | { |
| 148 | unsigned long next; |
| 149 | pmd_t *pmdp; |
| 150 | |
| 151 | pmdp = pmd_offset(&pud, addr); |
| 152 | do { |
| 153 | pmd_t pmd = *pmdp; |
| 154 | |
| 155 | next = pmd_addr_end(addr, end); |
Andrea Arcangeli | 64cc6ae | 2011-01-13 15:46:42 -0800 | [diff] [blame] | 156 | /* |
| 157 | * The pmd_trans_splitting() check below explains why |
| 158 | * pmdp_splitting_flush has to flush the tlb, to stop |
| 159 | * this gup-fast code from running while we set the |
| 160 | * splitting bit in the pmd. Returning zero will take |
| 161 | * the slow path that will call wait_split_huge_page() |
| 162 | * if the pmd is still in splitting state. gup-fast |
| 163 | * can't because it has irq disabled and |
| 164 | * wait_split_huge_page() would never return as the |
| 165 | * tlb flush IPI wouldn't run. |
| 166 | */ |
| 167 | if (pmd_none(pmd) || pmd_trans_splitting(pmd)) |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 168 | return 0; |
| 169 | if (unlikely(pmd_large(pmd))) { |
| 170 | if (!gup_huge_pmd(pmd, addr, next, write, pages, nr)) |
| 171 | return 0; |
| 172 | } else { |
| 173 | if (!gup_pte_range(pmd, addr, next, write, pages, nr)) |
| 174 | return 0; |
| 175 | } |
| 176 | } while (pmdp++, addr = next, addr != end); |
| 177 | |
| 178 | return 1; |
| 179 | } |
| 180 | |
Nick Piggin | 652ea69 | 2008-07-25 19:45:27 -0700 | [diff] [blame] | 181 | static noinline int gup_huge_pud(pud_t pud, unsigned long addr, |
| 182 | unsigned long end, int write, struct page **pages, int *nr) |
| 183 | { |
| 184 | unsigned long mask; |
| 185 | pte_t pte = *(pte_t *)&pud; |
| 186 | struct page *head, *page; |
| 187 | int refs; |
| 188 | |
| 189 | mask = _PAGE_PRESENT|_PAGE_USER; |
| 190 | if (write) |
| 191 | mask |= _PAGE_RW; |
Jan Beulich | 606ee44 | 2008-09-17 16:48:17 +0100 | [diff] [blame] | 192 | if ((pte_flags(pte) & mask) != mask) |
Nick Piggin | 652ea69 | 2008-07-25 19:45:27 -0700 | [diff] [blame] | 193 | return 0; |
| 194 | /* hugepages are never "special" */ |
Jan Beulich | 606ee44 | 2008-09-17 16:48:17 +0100 | [diff] [blame] | 195 | VM_BUG_ON(pte_flags(pte) & _PAGE_SPECIAL); |
Nick Piggin | 652ea69 | 2008-07-25 19:45:27 -0700 | [diff] [blame] | 196 | VM_BUG_ON(!pfn_valid(pte_pfn(pte))); |
| 197 | |
| 198 | refs = 0; |
| 199 | head = pte_page(pte); |
| 200 | page = head + ((addr & ~PUD_MASK) >> PAGE_SHIFT); |
| 201 | do { |
| 202 | VM_BUG_ON(compound_head(page) != head); |
| 203 | pages[*nr] = page; |
| 204 | (*nr)++; |
| 205 | page++; |
| 206 | refs++; |
| 207 | } while (addr += PAGE_SIZE, addr != end); |
| 208 | get_head_page_multiple(head, refs); |
| 209 | |
| 210 | return 1; |
| 211 | } |
| 212 | |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 213 | static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end, |
| 214 | int write, struct page **pages, int *nr) |
| 215 | { |
| 216 | unsigned long next; |
| 217 | pud_t *pudp; |
| 218 | |
| 219 | pudp = pud_offset(&pgd, addr); |
| 220 | do { |
| 221 | pud_t pud = *pudp; |
| 222 | |
| 223 | next = pud_addr_end(addr, end); |
| 224 | if (pud_none(pud)) |
| 225 | return 0; |
Nick Piggin | 652ea69 | 2008-07-25 19:45:27 -0700 | [diff] [blame] | 226 | if (unlikely(pud_large(pud))) { |
| 227 | if (!gup_huge_pud(pud, addr, next, write, pages, nr)) |
| 228 | return 0; |
| 229 | } else { |
| 230 | if (!gup_pmd_range(pud, addr, next, write, pages, nr)) |
| 231 | return 0; |
| 232 | } |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 233 | } while (pudp++, addr = next, addr != end); |
| 234 | |
| 235 | return 1; |
| 236 | } |
| 237 | |
Peter Zijlstra | 465a454 | 2009-06-15 12:31:37 +0200 | [diff] [blame] | 238 | /* |
| 239 | * Like get_user_pages_fast() except its IRQ-safe in that it won't fall |
| 240 | * back to the regular GUP. |
| 241 | */ |
| 242 | int __get_user_pages_fast(unsigned long start, int nr_pages, int write, |
| 243 | struct page **pages) |
| 244 | { |
| 245 | struct mm_struct *mm = current->mm; |
| 246 | unsigned long addr, len, end; |
| 247 | unsigned long next; |
| 248 | unsigned long flags; |
| 249 | pgd_t *pgdp; |
| 250 | int nr = 0; |
| 251 | |
| 252 | start &= PAGE_MASK; |
| 253 | addr = start; |
| 254 | len = (unsigned long) nr_pages << PAGE_SHIFT; |
| 255 | end = start + len; |
| 256 | if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ, |
| 257 | (void __user *)start, len))) |
| 258 | return 0; |
| 259 | |
| 260 | /* |
| 261 | * XXX: batch / limit 'nr', to avoid large irq off latency |
| 262 | * needs some instrumenting to determine the common sizes used by |
| 263 | * important workloads (eg. DB2), and whether limiting the batch size |
| 264 | * will decrease performance. |
| 265 | * |
| 266 | * It seems like we're in the clear for the moment. Direct-IO is |
| 267 | * the main guy that batches up lots of get_user_pages, and even |
| 268 | * they are limited to 64-at-a-time which is not so many. |
| 269 | */ |
| 270 | /* |
| 271 | * This doesn't prevent pagetable teardown, but does prevent |
| 272 | * the pagetables and pages from being freed on x86. |
| 273 | * |
| 274 | * So long as we atomically load page table pointers versus teardown |
| 275 | * (which we do on x86, with the above PAE exception), we can follow the |
| 276 | * address down to the the page and take a ref on it. |
| 277 | */ |
| 278 | local_irq_save(flags); |
| 279 | pgdp = pgd_offset(mm, addr); |
| 280 | do { |
| 281 | pgd_t pgd = *pgdp; |
| 282 | |
| 283 | next = pgd_addr_end(addr, end); |
| 284 | if (pgd_none(pgd)) |
| 285 | break; |
| 286 | if (!gup_pud_range(pgd, addr, next, write, pages, &nr)) |
| 287 | break; |
| 288 | } while (pgdp++, addr = next, addr != end); |
| 289 | local_irq_restore(flags); |
| 290 | |
| 291 | return nr; |
| 292 | } |
| 293 | |
Andy Grover | a0d22f4 | 2009-04-09 16:45:29 -0700 | [diff] [blame] | 294 | /** |
| 295 | * get_user_pages_fast() - pin user pages in memory |
| 296 | * @start: starting user address |
| 297 | * @nr_pages: number of pages from start to pin |
| 298 | * @write: whether pages will be written to |
| 299 | * @pages: array that receives pointers to the pages pinned. |
| 300 | * Should be at least nr_pages long. |
| 301 | * |
| 302 | * Attempt to pin user pages in memory without taking mm->mmap_sem. |
| 303 | * If not successful, it will fall back to taking the lock and |
| 304 | * calling get_user_pages(). |
| 305 | * |
| 306 | * Returns number of pages pinned. This may be fewer than the number |
| 307 | * requested. If nr_pages is 0 or negative, returns 0. If no pages |
| 308 | * were pinned, returns -errno. |
| 309 | */ |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 310 | int get_user_pages_fast(unsigned long start, int nr_pages, int write, |
| 311 | struct page **pages) |
| 312 | { |
| 313 | struct mm_struct *mm = current->mm; |
Linus Torvalds | 9b79022 | 2008-07-28 17:54:21 -0700 | [diff] [blame] | 314 | unsigned long addr, len, end; |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 315 | unsigned long next; |
| 316 | pgd_t *pgdp; |
| 317 | int nr = 0; |
| 318 | |
Linus Torvalds | 9b79022 | 2008-07-28 17:54:21 -0700 | [diff] [blame] | 319 | start &= PAGE_MASK; |
| 320 | addr = start; |
| 321 | len = (unsigned long) nr_pages << PAGE_SHIFT; |
Linus Torvalds | 7f81890 | 2009-06-20 09:52:27 -0700 | [diff] [blame] | 322 | |
Linus Torvalds | 9b79022 | 2008-07-28 17:54:21 -0700 | [diff] [blame] | 323 | end = start + len; |
Linus Torvalds | 7f81890 | 2009-06-20 09:52:27 -0700 | [diff] [blame] | 324 | if (end < start) |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 325 | goto slow_irqon; |
| 326 | |
Linus Torvalds | 7f81890 | 2009-06-20 09:52:27 -0700 | [diff] [blame] | 327 | #ifdef CONFIG_X86_64 |
| 328 | if (end >> __VIRTUAL_MASK_SHIFT) |
| 329 | goto slow_irqon; |
| 330 | #endif |
| 331 | |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 332 | /* |
| 333 | * XXX: batch / limit 'nr', to avoid large irq off latency |
| 334 | * needs some instrumenting to determine the common sizes used by |
| 335 | * important workloads (eg. DB2), and whether limiting the batch size |
| 336 | * will decrease performance. |
| 337 | * |
| 338 | * It seems like we're in the clear for the moment. Direct-IO is |
| 339 | * the main guy that batches up lots of get_user_pages, and even |
| 340 | * they are limited to 64-at-a-time which is not so many. |
| 341 | */ |
| 342 | /* |
| 343 | * This doesn't prevent pagetable teardown, but does prevent |
| 344 | * the pagetables and pages from being freed on x86. |
| 345 | * |
| 346 | * So long as we atomically load page table pointers versus teardown |
| 347 | * (which we do on x86, with the above PAE exception), we can follow the |
| 348 | * address down to the the page and take a ref on it. |
| 349 | */ |
| 350 | local_irq_disable(); |
| 351 | pgdp = pgd_offset(mm, addr); |
| 352 | do { |
| 353 | pgd_t pgd = *pgdp; |
| 354 | |
| 355 | next = pgd_addr_end(addr, end); |
| 356 | if (pgd_none(pgd)) |
| 357 | goto slow; |
| 358 | if (!gup_pud_range(pgd, addr, next, write, pages, &nr)) |
| 359 | goto slow; |
| 360 | } while (pgdp++, addr = next, addr != end); |
| 361 | local_irq_enable(); |
| 362 | |
| 363 | VM_BUG_ON(nr != (end - start) >> PAGE_SHIFT); |
| 364 | return nr; |
| 365 | |
| 366 | { |
| 367 | int ret; |
| 368 | |
| 369 | slow: |
| 370 | local_irq_enable(); |
| 371 | slow_irqon: |
| 372 | /* Try to get the remaining pages with get_user_pages */ |
| 373 | start += nr << PAGE_SHIFT; |
| 374 | pages += nr; |
| 375 | |
| 376 | down_read(&mm->mmap_sem); |
| 377 | ret = get_user_pages(current, mm, start, |
| 378 | (end - start) >> PAGE_SHIFT, write, 0, pages, NULL); |
| 379 | up_read(&mm->mmap_sem); |
| 380 | |
| 381 | /* Have to be a bit careful with return values */ |
| 382 | if (nr > 0) { |
| 383 | if (ret < 0) |
| 384 | ret = nr; |
| 385 | else |
| 386 | ret += nr; |
| 387 | } |
| 388 | |
| 389 | return ret; |
| 390 | } |
| 391 | } |