source: src/linux/universal/linux-4.9/arch/x86/mm/gup.c @ 31662

Last change on this file since 31662 was 31662, checked in by brainslayer, 2 months ago

use new squashfs in all kernels

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