Linux 4.13.16
[linux/fpc-iii.git] / arch / s390 / mm / gup.c
blob98ffe3ee9411ad23798c7ef896783ebd4d6edcfc
1 /*
2 * Lockless get_user_pages_fast for s390
4 * Copyright IBM Corp. 2010
5 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
6 */
7 #include <linux/sched.h>
8 #include <linux/mm.h>
9 #include <linux/hugetlb.h>
10 #include <linux/vmstat.h>
11 #include <linux/pagemap.h>
12 #include <linux/rwsem.h>
13 #include <asm/pgtable.h>
16 * The performance critical leaf functions are made noinline otherwise gcc
17 * inlines everything into a single function which results in too much
18 * register pressure.
20 static inline int gup_pte_range(pmd_t *pmdp, pmd_t pmd, unsigned long addr,
21 unsigned long end, int write, struct page **pages, int *nr)
23 struct page *head, *page;
24 unsigned long mask;
25 pte_t *ptep, pte;
27 mask = (write ? _PAGE_PROTECT : 0) | _PAGE_INVALID | _PAGE_SPECIAL;
29 ptep = ((pte_t *) pmd_deref(pmd)) + pte_index(addr);
30 do {
31 pte = *ptep;
32 barrier();
33 /* Similar to the PMD case, NUMA hinting must take slow path */
34 if (pte_protnone(pte))
35 return 0;
36 if ((pte_val(pte) & mask) != 0)
37 return 0;
38 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
39 page = pte_page(pte);
40 head = compound_head(page);
41 if (!page_cache_get_speculative(head))
42 return 0;
43 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
44 put_page(head);
45 return 0;
47 VM_BUG_ON_PAGE(compound_head(page) != head, page);
48 pages[*nr] = page;
49 (*nr)++;
51 } while (ptep++, addr += PAGE_SIZE, addr != end);
53 return 1;
56 static inline int gup_huge_pmd(pmd_t *pmdp, pmd_t pmd, unsigned long addr,
57 unsigned long end, int write, struct page **pages, int *nr)
59 struct page *head, *page;
60 unsigned long mask;
61 int refs;
63 mask = (write ? _SEGMENT_ENTRY_PROTECT : 0) | _SEGMENT_ENTRY_INVALID;
64 if ((pmd_val(pmd) & mask) != 0)
65 return 0;
66 VM_BUG_ON(!pfn_valid(pmd_val(pmd) >> PAGE_SHIFT));
68 refs = 0;
69 head = pmd_page(pmd);
70 page = head + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
71 do {
72 VM_BUG_ON(compound_head(page) != head);
73 pages[*nr] = page;
74 (*nr)++;
75 page++;
76 refs++;
77 } while (addr += PAGE_SIZE, addr != end);
79 if (!page_cache_add_speculative(head, refs)) {
80 *nr -= refs;
81 return 0;
84 if (unlikely(pmd_val(pmd) != pmd_val(*pmdp))) {
85 *nr -= refs;
86 while (refs--)
87 put_page(head);
88 return 0;
91 return 1;
95 static inline int gup_pmd_range(pud_t *pudp, pud_t pud, unsigned long addr,
96 unsigned long end, int write, struct page **pages, int *nr)
98 unsigned long next;
99 pmd_t *pmdp, pmd;
101 pmdp = (pmd_t *) pudp;
102 if ((pud_val(pud) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R3)
103 pmdp = (pmd_t *) pud_deref(pud);
104 pmdp += pmd_index(addr);
105 do {
106 pmd = *pmdp;
107 barrier();
108 next = pmd_addr_end(addr, end);
109 if (pmd_none(pmd))
110 return 0;
111 if (unlikely(pmd_large(pmd))) {
113 * NUMA hinting faults need to be handled in the GUP
114 * slowpath for accounting purposes and so that they
115 * can be serialised against THP migration.
117 if (pmd_protnone(pmd))
118 return 0;
119 if (!gup_huge_pmd(pmdp, pmd, addr, next,
120 write, pages, nr))
121 return 0;
122 } else if (!gup_pte_range(pmdp, pmd, addr, next,
123 write, pages, nr))
124 return 0;
125 } while (pmdp++, addr = next, addr != end);
127 return 1;
130 static int gup_huge_pud(pud_t *pudp, pud_t pud, unsigned long addr,
131 unsigned long end, int write, struct page **pages, int *nr)
133 struct page *head, *page;
134 unsigned long mask;
135 int refs;
137 mask = (write ? _REGION_ENTRY_PROTECT : 0) | _REGION_ENTRY_INVALID;
138 if ((pud_val(pud) & mask) != 0)
139 return 0;
140 VM_BUG_ON(!pfn_valid(pud_pfn(pud)));
142 refs = 0;
143 head = pud_page(pud);
144 page = head + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
145 do {
146 VM_BUG_ON_PAGE(compound_head(page) != head, page);
147 pages[*nr] = page;
148 (*nr)++;
149 page++;
150 refs++;
151 } while (addr += PAGE_SIZE, addr != end);
153 if (!page_cache_add_speculative(head, refs)) {
154 *nr -= refs;
155 return 0;
158 if (unlikely(pud_val(pud) != pud_val(*pudp))) {
159 *nr -= refs;
160 while (refs--)
161 put_page(head);
162 return 0;
165 return 1;
168 static inline int gup_pud_range(p4d_t *p4dp, p4d_t p4d, unsigned long addr,
169 unsigned long end, int write, struct page **pages, int *nr)
171 unsigned long next;
172 pud_t *pudp, pud;
174 pudp = (pud_t *) p4dp;
175 if ((p4d_val(p4d) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R2)
176 pudp = (pud_t *) p4d_deref(p4d);
177 pudp += pud_index(addr);
178 do {
179 pud = *pudp;
180 barrier();
181 next = pud_addr_end(addr, end);
182 if (pud_none(pud))
183 return 0;
184 if (unlikely(pud_large(pud))) {
185 if (!gup_huge_pud(pudp, pud, addr, next, write, pages,
186 nr))
187 return 0;
188 } else if (!gup_pmd_range(pudp, pud, addr, next, write, pages,
189 nr))
190 return 0;
191 } while (pudp++, addr = next, addr != end);
193 return 1;
196 static inline int gup_p4d_range(pgd_t *pgdp, pgd_t pgd, unsigned long addr,
197 unsigned long end, int write, struct page **pages, int *nr)
199 unsigned long next;
200 p4d_t *p4dp, p4d;
202 p4dp = (p4d_t *) pgdp;
203 if ((pgd_val(pgd) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R1)
204 p4dp = (p4d_t *) pgd_deref(pgd);
205 p4dp += p4d_index(addr);
206 do {
207 p4d = *p4dp;
208 barrier();
209 next = p4d_addr_end(addr, end);
210 if (p4d_none(p4d))
211 return 0;
212 if (!gup_pud_range(p4dp, p4d, addr, next, write, pages, nr))
213 return 0;
214 } while (p4dp++, addr = next, addr != end);
216 return 1;
220 * Like get_user_pages_fast() except its IRQ-safe in that it won't fall
221 * back to the regular GUP.
223 int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
224 struct page **pages)
226 struct mm_struct *mm = current->mm;
227 unsigned long addr, len, end;
228 unsigned long next, flags;
229 pgd_t *pgdp, pgd;
230 int nr = 0;
232 start &= PAGE_MASK;
233 addr = start;
234 len = (unsigned long) nr_pages << PAGE_SHIFT;
235 end = start + len;
236 if ((end <= start) || (end > mm->context.asce_limit))
237 return 0;
239 * local_irq_save() doesn't prevent pagetable teardown, but does
240 * prevent the pagetables from being freed on s390.
242 * So long as we atomically load page table pointers versus teardown,
243 * we can follow the address down to the the page and take a ref on it.
245 local_irq_save(flags);
246 pgdp = pgd_offset(mm, addr);
247 do {
248 pgd = *pgdp;
249 barrier();
250 next = pgd_addr_end(addr, end);
251 if (pgd_none(pgd))
252 break;
253 if (!gup_p4d_range(pgdp, pgd, addr, next, write, pages, &nr))
254 break;
255 } while (pgdp++, addr = next, addr != end);
256 local_irq_restore(flags);
258 return nr;
262 * get_user_pages_fast() - pin user pages in memory
263 * @start: starting user address
264 * @nr_pages: number of pages from start to pin
265 * @write: whether pages will be written to
266 * @pages: array that receives pointers to the pages pinned.
267 * Should be at least nr_pages long.
269 * Attempt to pin user pages in memory without taking mm->mmap_sem.
270 * If not successful, it will fall back to taking the lock and
271 * calling get_user_pages().
273 * Returns number of pages pinned. This may be fewer than the number
274 * requested. If nr_pages is 0 or negative, returns 0. If no pages
275 * were pinned, returns -errno.
277 int get_user_pages_fast(unsigned long start, int nr_pages, int write,
278 struct page **pages)
280 int nr, ret;
282 might_sleep();
283 start &= PAGE_MASK;
284 nr = __get_user_pages_fast(start, nr_pages, write, pages);
285 if (nr == nr_pages)
286 return nr;
288 /* Try to get the remaining pages with get_user_pages */
289 start += nr << PAGE_SHIFT;
290 pages += nr;
291 ret = get_user_pages_unlocked(start, nr_pages - nr, pages,
292 write ? FOLL_WRITE : 0);
293 /* Have to be a bit careful with return values */
294 if (nr > 0)
295 ret = (ret < 0) ? nr : ret + nr;
296 return ret;