1 // SPDX-License-Identifier: GPL-2.0
3 * Lockless get_user_pages_fast for s390
5 * Copyright IBM Corp. 2010
6 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
8 #include <linux/sched.h>
10 #include <linux/hugetlb.h>
11 #include <linux/vmstat.h>
12 #include <linux/pagemap.h>
13 #include <linux/rwsem.h>
14 #include <asm/pgtable.h>
17 * The performance critical leaf functions are made noinline otherwise gcc
18 * inlines everything into a single function which results in too much
21 static inline int gup_pte_range(pmd_t
*pmdp
, pmd_t pmd
, unsigned long addr
,
22 unsigned long end
, int write
, struct page
**pages
, int *nr
)
24 struct page
*head
, *page
;
28 mask
= (write
? _PAGE_PROTECT
: 0) | _PAGE_INVALID
| _PAGE_SPECIAL
;
30 ptep
= ((pte_t
*) pmd_deref(pmd
)) + pte_index(addr
);
34 /* Similar to the PMD case, NUMA hinting must take slow path */
35 if (pte_protnone(pte
))
37 if ((pte_val(pte
) & mask
) != 0)
39 VM_BUG_ON(!pfn_valid(pte_pfn(pte
)));
41 head
= compound_head(page
);
42 if (!page_cache_get_speculative(head
))
44 if (unlikely(pte_val(pte
) != pte_val(*ptep
))) {
48 VM_BUG_ON_PAGE(compound_head(page
) != head
, page
);
52 } while (ptep
++, addr
+= PAGE_SIZE
, addr
!= end
);
57 static inline int gup_huge_pmd(pmd_t
*pmdp
, pmd_t pmd
, unsigned long addr
,
58 unsigned long end
, int write
, struct page
**pages
, int *nr
)
60 struct page
*head
, *page
;
64 mask
= (write
? _SEGMENT_ENTRY_PROTECT
: 0) | _SEGMENT_ENTRY_INVALID
;
65 if ((pmd_val(pmd
) & mask
) != 0)
67 VM_BUG_ON(!pfn_valid(pmd_val(pmd
) >> PAGE_SHIFT
));
71 page
= head
+ ((addr
& ~PMD_MASK
) >> PAGE_SHIFT
);
73 VM_BUG_ON(compound_head(page
) != head
);
78 } while (addr
+= PAGE_SIZE
, addr
!= end
);
80 if (!page_cache_add_speculative(head
, refs
)) {
85 if (unlikely(pmd_val(pmd
) != pmd_val(*pmdp
))) {
96 static inline int gup_pmd_range(pud_t
*pudp
, pud_t pud
, unsigned long addr
,
97 unsigned long end
, int write
, struct page
**pages
, int *nr
)
102 pmdp
= (pmd_t
*) pudp
;
103 if ((pud_val(pud
) & _REGION_ENTRY_TYPE_MASK
) == _REGION_ENTRY_TYPE_R3
)
104 pmdp
= (pmd_t
*) pud_deref(pud
);
105 pmdp
+= pmd_index(addr
);
109 next
= pmd_addr_end(addr
, end
);
112 if (unlikely(pmd_large(pmd
))) {
114 * NUMA hinting faults need to be handled in the GUP
115 * slowpath for accounting purposes and so that they
116 * can be serialised against THP migration.
118 if (pmd_protnone(pmd
))
120 if (!gup_huge_pmd(pmdp
, pmd
, addr
, next
,
123 } else if (!gup_pte_range(pmdp
, pmd
, addr
, next
,
126 } while (pmdp
++, addr
= next
, addr
!= end
);
131 static int gup_huge_pud(pud_t
*pudp
, pud_t pud
, unsigned long addr
,
132 unsigned long end
, int write
, struct page
**pages
, int *nr
)
134 struct page
*head
, *page
;
138 mask
= (write
? _REGION_ENTRY_PROTECT
: 0) | _REGION_ENTRY_INVALID
;
139 if ((pud_val(pud
) & mask
) != 0)
141 VM_BUG_ON(!pfn_valid(pud_pfn(pud
)));
144 head
= pud_page(pud
);
145 page
= head
+ ((addr
& ~PUD_MASK
) >> PAGE_SHIFT
);
147 VM_BUG_ON_PAGE(compound_head(page
) != head
, page
);
152 } while (addr
+= PAGE_SIZE
, addr
!= end
);
154 if (!page_cache_add_speculative(head
, refs
)) {
159 if (unlikely(pud_val(pud
) != pud_val(*pudp
))) {
169 static inline int gup_pud_range(p4d_t
*p4dp
, p4d_t p4d
, unsigned long addr
,
170 unsigned long end
, int write
, struct page
**pages
, int *nr
)
175 pudp
= (pud_t
*) p4dp
;
176 if ((p4d_val(p4d
) & _REGION_ENTRY_TYPE_MASK
) == _REGION_ENTRY_TYPE_R2
)
177 pudp
= (pud_t
*) p4d_deref(p4d
);
178 pudp
+= pud_index(addr
);
182 next
= pud_addr_end(addr
, end
);
185 if (unlikely(pud_large(pud
))) {
186 if (!gup_huge_pud(pudp
, pud
, addr
, next
, write
, pages
,
189 } else if (!gup_pmd_range(pudp
, pud
, addr
, next
, write
, pages
,
192 } while (pudp
++, addr
= next
, addr
!= end
);
197 static inline int gup_p4d_range(pgd_t
*pgdp
, pgd_t pgd
, unsigned long addr
,
198 unsigned long end
, int write
, struct page
**pages
, int *nr
)
203 p4dp
= (p4d_t
*) pgdp
;
204 if ((pgd_val(pgd
) & _REGION_ENTRY_TYPE_MASK
) == _REGION_ENTRY_TYPE_R1
)
205 p4dp
= (p4d_t
*) pgd_deref(pgd
);
206 p4dp
+= p4d_index(addr
);
210 next
= p4d_addr_end(addr
, end
);
213 if (!gup_pud_range(p4dp
, p4d
, addr
, next
, write
, pages
, nr
))
215 } while (p4dp
++, addr
= next
, addr
!= end
);
221 * Like get_user_pages_fast() except its IRQ-safe in that it won't fall
222 * back to the regular GUP.
224 int __get_user_pages_fast(unsigned long start
, int nr_pages
, int write
,
227 struct mm_struct
*mm
= current
->mm
;
228 unsigned long addr
, len
, end
;
229 unsigned long next
, flags
;
235 len
= (unsigned long) nr_pages
<< PAGE_SHIFT
;
237 if ((end
<= start
) || (end
> mm
->context
.asce_limit
))
240 * local_irq_save() doesn't prevent pagetable teardown, but does
241 * prevent the pagetables from being freed on s390.
243 * So long as we atomically load page table pointers versus teardown,
244 * we can follow the address down to the the page and take a ref on it.
246 local_irq_save(flags
);
247 pgdp
= pgd_offset(mm
, addr
);
251 next
= pgd_addr_end(addr
, end
);
254 if (!gup_p4d_range(pgdp
, pgd
, addr
, next
, write
, pages
, &nr
))
256 } while (pgdp
++, addr
= next
, addr
!= end
);
257 local_irq_restore(flags
);
263 * get_user_pages_fast() - pin user pages in memory
264 * @start: starting user address
265 * @nr_pages: number of pages from start to pin
266 * @write: whether pages will be written to
267 * @pages: array that receives pointers to the pages pinned.
268 * Should be at least nr_pages long.
270 * Attempt to pin user pages in memory without taking mm->mmap_sem.
271 * If not successful, it will fall back to taking the lock and
272 * calling get_user_pages().
274 * Returns number of pages pinned. This may be fewer than the number
275 * requested. If nr_pages is 0 or negative, returns 0. If no pages
276 * were pinned, returns -errno.
278 int get_user_pages_fast(unsigned long start
, int nr_pages
, int write
,
285 nr
= __get_user_pages_fast(start
, nr_pages
, write
, pages
);
289 /* Try to get the remaining pages with get_user_pages */
290 start
+= nr
<< PAGE_SHIFT
;
292 ret
= get_user_pages_unlocked(start
, nr_pages
- nr
, pages
,
293 write
? FOLL_WRITE
: 0);
294 /* Have to be a bit careful with return values */
296 ret
= (ret
< 0) ? nr
: ret
+ nr
;