]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - arch/x86/mm/gup.c
Merge branch 'WIP.x86/boot' into x86/boot, to pick up ready branch
[mirror_ubuntu-focal-kernel.git] / arch / x86 / mm / gup.c
CommitLineData
8174c430
NP
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>
8ee53820 11#include <linux/swap.h>
3565fce3 12#include <linux/memremap.h>
8174c430 13
33a709b2 14#include <asm/mmu_context.h>
8174c430
NP
15#include <asm/pgtable.h>
16
17static inline pte_t gup_get_pte(pte_t *ptep)
18{
19#ifndef CONFIG_X86_PAE
14cf3d97 20 return READ_ONCE(*ptep);
8174c430
NP
21#else
22 /*
23 * With get_user_pages_fast, we walk down the pagetables without taking
ab09809f 24 * any locks. For this we would like to load the pointers atomically,
8174c430
NP
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
56retry:
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
3565fce3
DW
68static 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
1874f689
DH
78/*
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 */
83static 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
33a709b2
DH
93 /* Check memory protection keys permissions. */
94 if (!__pkru_allows_pkey(pte_flags_pkey(pteval), write))
95 return 0;
96
1874f689
DH
97 return 1;
98}
99
8174c430
NP
100/*
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 */
105static noinline int gup_pte_range(pmd_t pmd, unsigned long addr,
106 unsigned long end, int write, struct page **pages, int *nr)
107{
3565fce3 108 struct dev_pagemap *pgmap = NULL;
3565fce3 109 int nr_start = *nr;
8174c430
NP
110 pte_t *ptep;
111
8174c430
NP
112 ptep = pte_offset_map(&pmd, addr);
113 do {
114 pte_t pte = gup_get_pte(ptep);
115 struct page *page;
116
2b4847e7 117 /* Similar to the PMD case, NUMA hinting must take slow path */
8a0516ed 118 if (pte_protnone(pte)) {
2b4847e7
MG
119 pte_unmap(ptep);
120 return 0;
121 }
122
3565fce3
DW
123 if (pte_devmap(pte)) {
124 pgmap = get_dev_pagemap(pte_pfn(pte), pgmap);
125 if (unlikely(!pgmap)) {
126 undo_dev_pagemap(nr, nr_start, pages);
127 pte_unmap(ptep);
128 return 0;
129 }
1874f689
DH
130 } else if (!pte_allows_gup(pte_val(pte), write) ||
131 pte_special(pte)) {
8174c430
NP
132 pte_unmap(ptep);
133 return 0;
134 }
135 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
457a98b0 136 page = pte_page(pte);
8174c430 137 get_page(page);
3565fce3 138 put_dev_pagemap(pgmap);
8ee53820 139 SetPageReferenced(page);
8174c430
NP
140 pages[*nr] = page;
141 (*nr)++;
142
143 } while (ptep++, addr += PAGE_SIZE, addr != end);
144 pte_unmap(ptep - 1);
145
146 return 1;
147}
148
149static inline void get_head_page_multiple(struct page *page, int nr)
150{
309381fe
SL
151 VM_BUG_ON_PAGE(page != compound_head(page), page);
152 VM_BUG_ON_PAGE(page_count(page) == 0, page);
fe896d18 153 page_ref_add(page, nr);
8ee53820 154 SetPageReferenced(page);
8174c430
NP
155}
156
220ced16 157static int __gup_device_huge(unsigned long pfn, unsigned long addr,
3565fce3
DW
158 unsigned long end, struct page **pages, int *nr)
159{
160 int nr_start = *nr;
3565fce3
DW
161 struct dev_pagemap *pgmap = NULL;
162
3565fce3
DW
163 do {
164 struct page *page = pfn_to_page(pfn);
165
166 pgmap = get_dev_pagemap(pfn, pgmap);
167 if (unlikely(!pgmap)) {
168 undo_dev_pagemap(nr, nr_start, pages);
169 return 0;
170 }
171 SetPageReferenced(page);
172 pages[*nr] = page;
173 get_page(page);
174 put_dev_pagemap(pgmap);
175 (*nr)++;
176 pfn++;
177 } while (addr += PAGE_SIZE, addr != end);
178 return 1;
179}
180
220ced16
DW
181static int __gup_device_huge_pmd(pmd_t pmd, unsigned long addr,
182 unsigned long end, struct page **pages, int *nr)
183{
184 unsigned long fault_pfn;
185
186 fault_pfn = pmd_pfn(pmd) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
187 return __gup_device_huge(fault_pfn, addr, end, pages, nr);
188}
189
190static int __gup_device_huge_pud(pud_t pud, unsigned long addr,
191 unsigned long end, struct page **pages, int *nr)
192{
193 unsigned long fault_pfn;
194
195 fault_pfn = pud_pfn(pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
196 return __gup_device_huge(fault_pfn, addr, end, pages, nr);
197}
198
8174c430
NP
199static noinline int gup_huge_pmd(pmd_t pmd, unsigned long addr,
200 unsigned long end, int write, struct page **pages, int *nr)
201{
8174c430
NP
202 struct page *head, *page;
203 int refs;
204
1874f689 205 if (!pte_allows_gup(pmd_val(pmd), write))
8174c430 206 return 0;
3565fce3
DW
207
208 VM_BUG_ON(!pfn_valid(pmd_pfn(pmd)));
209 if (pmd_devmap(pmd))
210 return __gup_device_huge_pmd(pmd, addr, end, pages, nr);
211
8174c430 212 /* hugepages are never "special" */
daf3e35c 213 VM_BUG_ON(pmd_flags(pmd) & _PAGE_SPECIAL);
8174c430
NP
214
215 refs = 0;
daf3e35c 216 head = pmd_page(pmd);
652ea695 217 page = head + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
8174c430 218 do {
309381fe 219 VM_BUG_ON_PAGE(compound_head(page) != head, page);
8174c430
NP
220 pages[*nr] = page;
221 (*nr)++;
222 page++;
223 refs++;
224 } while (addr += PAGE_SIZE, addr != end);
225 get_head_page_multiple(head, refs);
226
227 return 1;
228}
229
230static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
231 int write, struct page **pages, int *nr)
232{
233 unsigned long next;
234 pmd_t *pmdp;
235
236 pmdp = pmd_offset(&pud, addr);
237 do {
238 pmd_t pmd = *pmdp;
239
240 next = pmd_addr_end(addr, end);
1f19617d 241 if (pmd_none(pmd))
8174c430 242 return 0;
cbef8478 243 if (unlikely(pmd_large(pmd) || !pmd_present(pmd))) {
2b4847e7
MG
244 /*
245 * NUMA hinting faults need to be handled in the GUP
246 * slowpath for accounting purposes and so that they
247 * can be serialised against THP migration.
248 */
8a0516ed 249 if (pmd_protnone(pmd))
2b4847e7 250 return 0;
8174c430
NP
251 if (!gup_huge_pmd(pmd, addr, next, write, pages, nr))
252 return 0;
253 } else {
254 if (!gup_pte_range(pmd, addr, next, write, pages, nr))
255 return 0;
256 }
257 } while (pmdp++, addr = next, addr != end);
258
259 return 1;
260}
261
652ea695
NP
262static noinline int gup_huge_pud(pud_t pud, unsigned long addr,
263 unsigned long end, int write, struct page **pages, int *nr)
264{
652ea695
NP
265 struct page *head, *page;
266 int refs;
267
1874f689 268 if (!pte_allows_gup(pud_val(pud), write))
652ea695 269 return 0;
220ced16
DW
270
271 VM_BUG_ON(!pfn_valid(pud_pfn(pud)));
272 if (pud_devmap(pud))
273 return __gup_device_huge_pud(pud, addr, end, pages, nr);
274
652ea695 275 /* hugepages are never "special" */
daf3e35c 276 VM_BUG_ON(pud_flags(pud) & _PAGE_SPECIAL);
652ea695
NP
277
278 refs = 0;
daf3e35c 279 head = pud_page(pud);
652ea695
NP
280 page = head + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
281 do {
309381fe 282 VM_BUG_ON_PAGE(compound_head(page) != head, page);
652ea695
NP
283 pages[*nr] = page;
284 (*nr)++;
285 page++;
286 refs++;
287 } while (addr += PAGE_SIZE, addr != end);
288 get_head_page_multiple(head, refs);
289
290 return 1;
291}
292
8174c430
NP
293static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end,
294 int write, struct page **pages, int *nr)
295{
296 unsigned long next;
297 pud_t *pudp;
298
299 pudp = pud_offset(&pgd, addr);
300 do {
301 pud_t pud = *pudp;
302
303 next = pud_addr_end(addr, end);
304 if (pud_none(pud))
305 return 0;
652ea695
NP
306 if (unlikely(pud_large(pud))) {
307 if (!gup_huge_pud(pud, addr, next, write, pages, nr))
308 return 0;
309 } else {
310 if (!gup_pmd_range(pud, addr, next, write, pages, nr))
311 return 0;
312 }
8174c430
NP
313 } while (pudp++, addr = next, addr != end);
314
315 return 1;
316}
317
465a454f
PZ
318/*
319 * Like get_user_pages_fast() except its IRQ-safe in that it won't fall
320 * back to the regular GUP.
321 */
322int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
323 struct page **pages)
324{
325 struct mm_struct *mm = current->mm;
326 unsigned long addr, len, end;
327 unsigned long next;
328 unsigned long flags;
329 pgd_t *pgdp;
330 int nr = 0;
331
332 start &= PAGE_MASK;
333 addr = start;
334 len = (unsigned long) nr_pages << PAGE_SHIFT;
335 end = start + len;
336 if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ,
337 (void __user *)start, len)))
338 return 0;
339
340 /*
341 * XXX: batch / limit 'nr', to avoid large irq off latency
342 * needs some instrumenting to determine the common sizes used by
343 * important workloads (eg. DB2), and whether limiting the batch size
344 * will decrease performance.
345 *
346 * It seems like we're in the clear for the moment. Direct-IO is
347 * the main guy that batches up lots of get_user_pages, and even
348 * they are limited to 64-at-a-time which is not so many.
349 */
350 /*
351 * This doesn't prevent pagetable teardown, but does prevent
352 * the pagetables and pages from being freed on x86.
353 *
354 * So long as we atomically load page table pointers versus teardown
355 * (which we do on x86, with the above PAE exception), we can follow the
356 * address down to the the page and take a ref on it.
357 */
358 local_irq_save(flags);
359 pgdp = pgd_offset(mm, addr);
360 do {
361 pgd_t pgd = *pgdp;
362
363 next = pgd_addr_end(addr, end);
364 if (pgd_none(pgd))
365 break;
366 if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
367 break;
368 } while (pgdp++, addr = next, addr != end);
369 local_irq_restore(flags);
370
371 return nr;
372}
373
a0d22f48
AG
374/**
375 * get_user_pages_fast() - pin user pages in memory
376 * @start: starting user address
377 * @nr_pages: number of pages from start to pin
378 * @write: whether pages will be written to
379 * @pages: array that receives pointers to the pages pinned.
380 * Should be at least nr_pages long.
381 *
382 * Attempt to pin user pages in memory without taking mm->mmap_sem.
383 * If not successful, it will fall back to taking the lock and
384 * calling get_user_pages().
385 *
386 * Returns number of pages pinned. This may be fewer than the number
387 * requested. If nr_pages is 0 or negative, returns 0. If no pages
388 * were pinned, returns -errno.
389 */
8174c430
NP
390int get_user_pages_fast(unsigned long start, int nr_pages, int write,
391 struct page **pages)
392{
393 struct mm_struct *mm = current->mm;
9b79022c 394 unsigned long addr, len, end;
8174c430
NP
395 unsigned long next;
396 pgd_t *pgdp;
397 int nr = 0;
398
9b79022c
LT
399 start &= PAGE_MASK;
400 addr = start;
401 len = (unsigned long) nr_pages << PAGE_SHIFT;
7f818906 402
9b79022c 403 end = start + len;
7f818906
LT
404 if (end < start)
405 goto slow_irqon;
406
407#ifdef CONFIG_X86_64
408 if (end >> __VIRTUAL_MASK_SHIFT)
8174c430 409 goto slow_irqon;
7f818906 410#endif
8174c430
NP
411
412 /*
413 * XXX: batch / limit 'nr', to avoid large irq off latency
414 * needs some instrumenting to determine the common sizes used by
415 * important workloads (eg. DB2), and whether limiting the batch size
416 * will decrease performance.
417 *
418 * It seems like we're in the clear for the moment. Direct-IO is
419 * the main guy that batches up lots of get_user_pages, and even
420 * they are limited to 64-at-a-time which is not so many.
421 */
422 /*
423 * This doesn't prevent pagetable teardown, but does prevent
424 * the pagetables and pages from being freed on x86.
425 *
426 * So long as we atomically load page table pointers versus teardown
427 * (which we do on x86, with the above PAE exception), we can follow the
428 * address down to the the page and take a ref on it.
429 */
430 local_irq_disable();
431 pgdp = pgd_offset(mm, addr);
432 do {
433 pgd_t pgd = *pgdp;
434
435 next = pgd_addr_end(addr, end);
436 if (pgd_none(pgd))
437 goto slow;
438 if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
439 goto slow;
440 } while (pgdp++, addr = next, addr != end);
441 local_irq_enable();
442
443 VM_BUG_ON(nr != (end - start) >> PAGE_SHIFT);
444 return nr;
445
446 {
447 int ret;
448
449slow:
450 local_irq_enable();
451slow_irqon:
452 /* Try to get the remaining pages with get_user_pages */
453 start += nr << PAGE_SHIFT;
454 pages += nr;
455
d4edcf0d 456 ret = get_user_pages_unlocked(start,
a7b78075 457 (end - start) >> PAGE_SHIFT,
c164154f 458 pages, write ? FOLL_WRITE : 0);
8174c430
NP
459
460 /* Have to be a bit careful with return values */
461 if (nr > 0) {
462 if (ret < 0)
463 ret = nr;
464 else
465 ret += nr;
466 }
467
468 return ret;
469 }
470}