1 // SPDX-License-Identifier: GPL-2.0
3 * Lockless get_user_pages_fast for SuperH
5 * Copyright (C) 2009 - 2010 Paul Mundt
7 * Cloned from the x86 and PowerPC versions, by:
9 * Copyright (C) 2008 Nick Piggin
10 * Copyright (C) 2008 Novell Inc.
12 #include <linux/sched.h>
14 #include <linux/vmstat.h>
15 #include <linux/highmem.h>
16 #include <asm/pgtable.h>
18 static inline pte_t
gup_get_pte(pte_t
*ptep
)
21 return READ_ONCE(*ptep
);
24 * With get_user_pages_fast, we walk down the pagetables without
25 * taking any locks. For this we would like to load the pointers
26 * atomically, but that is not possible with 64-bit PTEs. What
27 * we do have is the guarantee that a pte will only either go
28 * from not present to present, or present to not present or both
29 * -- it will not switch to a completely different present page
30 * without a TLB flush in between; something that we are blocking
31 * by holding interrupts off.
33 * Setting ptes from not present to present goes:
38 * And present to not present goes:
43 * We must ensure here that the load of pte_low sees l iff pte_high
44 * sees h. We load pte_high *after* loading pte_low, which ensures we
45 * don't see an older value of pte_high. *Then* we recheck pte_low,
46 * which ensures that we haven't picked up a changed pte high. We might
47 * have got rubbish values from pte_low and pte_high, but we are
48 * guaranteed that pte_low will not have the present bit set *unless*
49 * it is 'l'. And get_user_pages_fast only operates on present ptes, so
52 * gup_get_pte should not be used or copied outside gup.c without being
53 * very careful -- it does not atomically load the pte or anything that
54 * is likely to be useful for you.
59 pte
.pte_low
= ptep
->pte_low
;
61 pte
.pte_high
= ptep
->pte_high
;
63 if (unlikely(pte
.pte_low
!= ptep
->pte_low
))
71 * The performance critical leaf functions are made noinline otherwise gcc
72 * inlines everything into a single function which results in too much
75 static noinline
int gup_pte_range(pmd_t pmd
, unsigned long addr
,
76 unsigned long end
, int write
, struct page
**pages
, int *nr
)
82 result
= _PAGE_PRESENT
| _PAGE_EXT(_PAGE_EXT_KERN_READ
| _PAGE_EXT_USER_READ
);
84 result
|= _PAGE_EXT(_PAGE_EXT_KERN_WRITE
| _PAGE_EXT_USER_WRITE
);
85 #elif defined(CONFIG_SUPERH64)
86 result
= _PAGE_PRESENT
| _PAGE_USER
| _PAGE_READ
;
88 result
|= _PAGE_WRITE
;
90 result
= _PAGE_PRESENT
| _PAGE_USER
;
95 mask
= result
| _PAGE_SPECIAL
;
97 ptep
= pte_offset_map(&pmd
, addr
);
99 pte_t pte
= gup_get_pte(ptep
);
102 if ((pte_val(pte
) & mask
) != result
) {
106 VM_BUG_ON(!pfn_valid(pte_pfn(pte
)));
107 page
= pte_page(pte
);
109 __flush_anon_page(page
, addr
);
110 flush_dcache_page(page
);
114 } while (ptep
++, addr
+= PAGE_SIZE
, addr
!= end
);
120 static int gup_pmd_range(pud_t pud
, unsigned long addr
, unsigned long end
,
121 int write
, struct page
**pages
, int *nr
)
126 pmdp
= pmd_offset(&pud
, addr
);
130 next
= pmd_addr_end(addr
, end
);
133 if (!gup_pte_range(pmd
, addr
, next
, write
, pages
, nr
))
135 } while (pmdp
++, addr
= next
, addr
!= end
);
140 static int gup_pud_range(pgd_t pgd
, unsigned long addr
, unsigned long end
,
141 int write
, struct page
**pages
, int *nr
)
146 pudp
= pud_offset(&pgd
, addr
);
150 next
= pud_addr_end(addr
, end
);
153 if (!gup_pmd_range(pud
, addr
, next
, write
, pages
, nr
))
155 } while (pudp
++, addr
= next
, addr
!= end
);
161 * Like get_user_pages_fast() except its IRQ-safe in that it won't fall
162 * back to the regular GUP.
163 * Note a difference with get_user_pages_fast: this always returns the
164 * number of pages pinned, 0 if no pages were pinned.
166 int __get_user_pages_fast(unsigned long start
, int nr_pages
, int write
,
169 struct mm_struct
*mm
= current
->mm
;
170 unsigned long addr
, len
, end
;
178 len
= (unsigned long) nr_pages
<< PAGE_SHIFT
;
180 if (unlikely(!access_ok((void __user
*)start
, len
)))
184 * This doesn't prevent pagetable teardown, but does prevent
185 * the pagetables and pages from being freed.
187 local_irq_save(flags
);
188 pgdp
= pgd_offset(mm
, addr
);
192 next
= pgd_addr_end(addr
, end
);
195 if (!gup_pud_range(pgd
, addr
, next
, write
, pages
, &nr
))
197 } while (pgdp
++, addr
= next
, addr
!= end
);
198 local_irq_restore(flags
);
204 * get_user_pages_fast() - pin user pages in memory
205 * @start: starting user address
206 * @nr_pages: number of pages from start to pin
207 * @gup_flags: flags modifying pin behaviour
208 * @pages: array that receives pointers to the pages pinned.
209 * Should be at least nr_pages long.
211 * Attempt to pin user pages in memory without taking mm->mmap_sem.
212 * If not successful, it will fall back to taking the lock and
213 * calling get_user_pages().
215 * Returns number of pages pinned. This may be fewer than the number
216 * requested. If nr_pages is 0 or negative, returns 0. If no pages
217 * were pinned, returns -errno.
219 int get_user_pages_fast(unsigned long start
, int nr_pages
,
220 unsigned int gup_flags
, struct page
**pages
)
222 struct mm_struct
*mm
= current
->mm
;
223 unsigned long addr
, len
, end
;
230 len
= (unsigned long) nr_pages
<< PAGE_SHIFT
;
237 pgdp
= pgd_offset(mm
, addr
);
241 next
= pgd_addr_end(addr
, end
);
244 if (!gup_pud_range(pgd
, addr
, next
, gup_flags
& FOLL_WRITE
,
247 } while (pgdp
++, addr
= next
, addr
!= end
);
250 VM_BUG_ON(nr
!= (end
- start
) >> PAGE_SHIFT
);
259 /* Try to get the remaining pages with get_user_pages */
260 start
+= nr
<< PAGE_SHIFT
;
263 ret
= get_user_pages_unlocked(start
,
264 (end
- start
) >> PAGE_SHIFT
, pages
,
267 /* Have to be a bit careful with return values */