2 * Lockless get_user_pages_fast for SuperH
4 * Copyright (C) 2009 - 2010 Paul Mundt
6 * Cloned from the x86 and PowerPC versions, by:
8 * Copyright (C) 2008 Nick Piggin
9 * Copyright (C) 2008 Novell Inc.
11 #include <linux/sched.h>
13 #include <linux/vmstat.h>
14 #include <linux/highmem.h>
15 #include <asm/pgtable.h>
17 static inline pte_t
gup_get_pte(pte_t
*ptep
)
20 return ACCESS_ONCE(*ptep
);
23 * With get_user_pages_fast, we walk down the pagetables without
24 * taking any locks. For this we would like to load the pointers
25 * atomically, but that is not possible with 64-bit PTEs. What
26 * we do have is the guarantee that a pte will only either go
27 * from not present to present, or present to not present or both
28 * -- it will not switch to a completely different present page
29 * without a TLB flush in between; something that we are blocking
30 * by holding interrupts off.
32 * Setting ptes from not present to present goes:
37 * And present to not present goes:
42 * We must ensure here that the load of pte_low sees l iff pte_high
43 * sees h. We load pte_high *after* loading pte_low, which ensures we
44 * don't see an older value of pte_high. *Then* we recheck pte_low,
45 * which ensures that we haven't picked up a changed pte high. We might
46 * have got rubbish values from pte_low and pte_high, but we are
47 * guaranteed that pte_low will not have the present bit set *unless*
48 * it is 'l'. And get_user_pages_fast only operates on present ptes, so
51 * gup_get_pte should not be used or copied outside gup.c without being
52 * very careful -- it does not atomically load the pte or anything that
53 * is likely to be useful for you.
58 pte
.pte_low
= ptep
->pte_low
;
60 pte
.pte_high
= ptep
->pte_high
;
62 if (unlikely(pte
.pte_low
!= ptep
->pte_low
))
70 * The performance critical leaf functions are made noinline otherwise gcc
71 * inlines everything into a single function which results in too much
74 static noinline
int gup_pte_range(pmd_t pmd
, unsigned long addr
,
75 unsigned long end
, int write
, struct page
**pages
, int *nr
)
81 result
= _PAGE_PRESENT
| _PAGE_EXT(_PAGE_EXT_KERN_READ
| _PAGE_EXT_USER_READ
);
83 result
|= _PAGE_EXT(_PAGE_EXT_KERN_WRITE
| _PAGE_EXT_USER_WRITE
);
84 #elif defined(CONFIG_SUPERH64)
85 result
= _PAGE_PRESENT
| _PAGE_USER
| _PAGE_READ
;
87 result
|= _PAGE_WRITE
;
89 result
= _PAGE_PRESENT
| _PAGE_USER
;
94 mask
= result
| _PAGE_SPECIAL
;
96 ptep
= pte_offset_map(&pmd
, addr
);
98 pte_t pte
= gup_get_pte(ptep
);
101 if ((pte_val(pte
) & mask
) != result
) {
105 VM_BUG_ON(!pfn_valid(pte_pfn(pte
)));
106 page
= pte_page(pte
);
111 } while (ptep
++, addr
+= PAGE_SIZE
, addr
!= end
);
117 static int gup_pmd_range(pud_t pud
, unsigned long addr
, unsigned long end
,
118 int write
, struct page
**pages
, int *nr
)
123 pmdp
= pmd_offset(&pud
, addr
);
127 next
= pmd_addr_end(addr
, end
);
130 if (!gup_pte_range(pmd
, addr
, next
, write
, pages
, nr
))
132 } while (pmdp
++, addr
= next
, addr
!= end
);
137 static int gup_pud_range(pgd_t pgd
, unsigned long addr
, unsigned long end
,
138 int write
, struct page
**pages
, int *nr
)
143 pudp
= pud_offset(&pgd
, addr
);
147 next
= pud_addr_end(addr
, end
);
150 if (!gup_pmd_range(pud
, addr
, next
, write
, pages
, nr
))
152 } while (pudp
++, addr
= next
, addr
!= end
);
158 * Like get_user_pages_fast() except its IRQ-safe in that it won't fall
159 * back to the regular GUP.
161 int __get_user_pages_fast(unsigned long start
, int nr_pages
, int write
,
164 struct mm_struct
*mm
= current
->mm
;
165 unsigned long addr
, len
, end
;
173 len
= (unsigned long) nr_pages
<< PAGE_SHIFT
;
175 if (unlikely(!access_ok(write
? VERIFY_WRITE
: VERIFY_READ
,
176 (void __user
*)start
, len
)))
180 * This doesn't prevent pagetable teardown, but does prevent
181 * the pagetables and pages from being freed.
183 local_irq_save(flags
);
184 pgdp
= pgd_offset(mm
, addr
);
188 next
= pgd_addr_end(addr
, end
);
191 if (!gup_pud_range(pgd
, addr
, next
, write
, pages
, &nr
))
193 } while (pgdp
++, addr
= next
, addr
!= end
);
194 local_irq_restore(flags
);
200 * get_user_pages_fast() - pin user pages in memory
201 * @start: starting user address
202 * @nr_pages: number of pages from start to pin
203 * @write: whether pages will be written to
204 * @pages: array that receives pointers to the pages pinned.
205 * Should be at least nr_pages long.
207 * Attempt to pin user pages in memory without taking mm->mmap_sem.
208 * If not successful, it will fall back to taking the lock and
209 * calling get_user_pages().
211 * Returns number of pages pinned. This may be fewer than the number
212 * requested. If nr_pages is 0 or negative, returns 0. If no pages
213 * were pinned, returns -errno.
215 int get_user_pages_fast(unsigned long start
, int nr_pages
, int write
,
218 struct mm_struct
*mm
= current
->mm
;
219 unsigned long addr
, len
, end
;
226 len
= (unsigned long) nr_pages
<< PAGE_SHIFT
;
233 pgdp
= pgd_offset(mm
, addr
);
237 next
= pgd_addr_end(addr
, end
);
240 if (!gup_pud_range(pgd
, addr
, next
, write
, pages
, &nr
))
242 } while (pgdp
++, addr
= next
, addr
!= end
);
245 VM_BUG_ON(nr
!= (end
- start
) >> PAGE_SHIFT
);
254 /* Try to get the remaining pages with get_user_pages */
255 start
+= nr
<< PAGE_SHIFT
;
258 down_read(&mm
->mmap_sem
);
259 ret
= get_user_pages(current
, mm
, start
,
260 (end
- start
) >> PAGE_SHIFT
, write
, 0, pages
, NULL
);
261 up_read(&mm
->mmap_sem
);
263 /* Have to be a bit careful with return values */