blob: 49a1c84ed2663fadeadb2bc1b8548814d9cab012 [file] [log] [blame]
Martin Schwidefsky80217142010-10-25 16:10:11 +02001/*
2 * Lockless get_user_pages_fast for s390
3 *
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>
14
15/*
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.
19 */
20static 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)
22{
Martin Schwidefsky25591b02010-11-10 10:05:51 +010023 unsigned long mask;
Martin Schwidefsky80217142010-10-25 16:10:11 +020024 pte_t *ptep, pte;
25 struct page *page;
26
Martin Schwidefskye5098612013-07-23 20:57:57 +020027 mask = (write ? _PAGE_PROTECT : 0) | _PAGE_INVALID | _PAGE_SPECIAL;
Martin Schwidefsky80217142010-10-25 16:10:11 +020028
29 ptep = ((pte_t *) pmd_deref(pmd)) + pte_index(addr);
30 do {
31 pte = *ptep;
32 barrier();
Gerald Schaeferecf46ab2015-05-29 15:34:51 +020033 /* Similar to the PMD case, NUMA hinting must take slow path */
34 if (pte_protnone(pte))
35 return 0;
Martin Schwidefsky25591b02010-11-10 10:05:51 +010036 if ((pte_val(pte) & mask) != 0)
Martin Schwidefsky80217142010-10-25 16:10:11 +020037 return 0;
38 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
39 page = pte_page(pte);
40 if (!page_cache_get_speculative(page))
41 return 0;
42 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
43 put_page(page);
44 return 0;
45 }
46 pages[*nr] = page;
47 (*nr)++;
48
49 } while (ptep++, addr += PAGE_SIZE, addr != end);
50
51 return 1;
52}
53
54static inline int gup_huge_pmd(pmd_t *pmdp, pmd_t pmd, unsigned long addr,
55 unsigned long end, int write, struct page **pages, int *nr)
56{
57 unsigned long mask, result;
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -080058 struct page *head, *page;
Martin Schwidefsky80217142010-10-25 16:10:11 +020059 int refs;
60
Martin Schwidefskye5098612013-07-23 20:57:57 +020061 result = write ? 0 : _SEGMENT_ENTRY_PROTECT;
62 mask = result | _SEGMENT_ENTRY_INVALID;
Martin Schwidefsky80217142010-10-25 16:10:11 +020063 if ((pmd_val(pmd) & mask) != result)
64 return 0;
65 VM_BUG_ON(!pfn_valid(pmd_val(pmd) >> PAGE_SHIFT));
66
67 refs = 0;
68 head = pmd_page(pmd);
69 page = head + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
70 do {
71 VM_BUG_ON(compound_head(page) != head);
72 pages[*nr] = page;
73 (*nr)++;
74 page++;
75 refs++;
76 } while (addr += PAGE_SIZE, addr != end);
77
78 if (!page_cache_add_speculative(head, refs)) {
79 *nr -= refs;
80 return 0;
81 }
82
83 if (unlikely(pmd_val(pmd) != pmd_val(*pmdp))) {
84 *nr -= refs;
85 while (refs--)
86 put_page(head);
Andrea Arcangeli0693bc92011-11-02 13:37:28 -070087 return 0;
88 }
89
Martin Schwidefsky80217142010-10-25 16:10:11 +020090 return 1;
91}
92
93
94static inline int gup_pmd_range(pud_t *pudp, pud_t pud, unsigned long addr,
95 unsigned long end, int write, struct page **pages, int *nr)
96{
97 unsigned long next;
98 pmd_t *pmdp, pmd;
99
100 pmdp = (pmd_t *) pudp;
Martin Schwidefsky80217142010-10-25 16:10:11 +0200101 if ((pud_val(pud) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R3)
102 pmdp = (pmd_t *) pud_deref(pud);
103 pmdp += pmd_index(addr);
Martin Schwidefsky80217142010-10-25 16:10:11 +0200104 do {
105 pmd = *pmdp;
106 barrier();
107 next = pmd_addr_end(addr, end);
Kirill A. Shutemovfecffad2016-01-15 16:53:24 -0800108 if (pmd_none(pmd))
Martin Schwidefsky80217142010-10-25 16:10:11 +0200109 return 0;
Gerald Schaefer156152f2012-10-25 17:24:12 +0200110 if (unlikely(pmd_large(pmd))) {
Gerald Schaeferecf46ab2015-05-29 15:34:51 +0200111 /*
112 * NUMA hinting faults need to be handled in the GUP
113 * slowpath for accounting purposes and so that they
114 * can be serialised against THP migration.
115 */
116 if (pmd_protnone(pmd))
117 return 0;
Martin Schwidefsky80217142010-10-25 16:10:11 +0200118 if (!gup_huge_pmd(pmdp, pmd, addr, next,
119 write, pages, nr))
120 return 0;
121 } else if (!gup_pte_range(pmdp, pmd, addr, next,
122 write, pages, nr))
123 return 0;
124 } while (pmdp++, addr = next, addr != end);
125
126 return 1;
127}
128
129static inline int gup_pud_range(pgd_t *pgdp, pgd_t pgd, unsigned long addr,
130 unsigned long end, int write, struct page **pages, int *nr)
131{
132 unsigned long next;
133 pud_t *pudp, pud;
134
135 pudp = (pud_t *) pgdp;
Martin Schwidefsky80217142010-10-25 16:10:11 +0200136 if ((pgd_val(pgd) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R2)
137 pudp = (pud_t *) pgd_deref(pgd);
138 pudp += pud_index(addr);
Martin Schwidefsky80217142010-10-25 16:10:11 +0200139 do {
140 pud = *pudp;
141 barrier();
142 next = pud_addr_end(addr, end);
143 if (pud_none(pud))
144 return 0;
145 if (!gup_pmd_range(pudp, pud, addr, next, write, pages, nr))
146 return 0;
147 } while (pudp++, addr = next, addr != end);
148
149 return 1;
150}
151
Gerald Schaefer34cda992012-09-04 15:37:55 +0200152/*
153 * Like get_user_pages_fast() except its IRQ-safe in that it won't fall
154 * back to the regular GUP.
155 */
156int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
157 struct page **pages)
158{
159 struct mm_struct *mm = current->mm;
160 unsigned long addr, len, end;
161 unsigned long next, flags;
162 pgd_t *pgdp, pgd;
163 int nr = 0;
164
165 start &= PAGE_MASK;
166 addr = start;
167 len = (unsigned long) nr_pages << PAGE_SHIFT;
168 end = start + len;
Heiko Carstenseb0bf922013-10-08 09:29:09 +0200169 if ((end <= start) || (end > TASK_SIZE))
Gerald Schaefer34cda992012-09-04 15:37:55 +0200170 return 0;
Heiko Carstens01997bb2013-10-07 16:14:50 +0200171 /*
172 * local_irq_save() doesn't prevent pagetable teardown, but does
173 * prevent the pagetables from being freed on s390.
174 *
175 * So long as we atomically load page table pointers versus teardown,
176 * we can follow the address down to the the page and take a ref on it.
177 */
Gerald Schaefer34cda992012-09-04 15:37:55 +0200178 local_irq_save(flags);
179 pgdp = pgd_offset(mm, addr);
180 do {
181 pgd = *pgdp;
182 barrier();
183 next = pgd_addr_end(addr, end);
184 if (pgd_none(pgd))
185 break;
186 if (!gup_pud_range(pgdp, pgd, addr, next, write, pages, &nr))
187 break;
188 } while (pgdp++, addr = next, addr != end);
189 local_irq_restore(flags);
190
191 return nr;
192}
193
Martin Schwidefsky80217142010-10-25 16:10:11 +0200194/**
195 * get_user_pages_fast() - pin user pages in memory
196 * @start: starting user address
197 * @nr_pages: number of pages from start to pin
198 * @write: whether pages will be written to
199 * @pages: array that receives pointers to the pages pinned.
200 * Should be at least nr_pages long.
201 *
202 * Attempt to pin user pages in memory without taking mm->mmap_sem.
203 * If not successful, it will fall back to taking the lock and
204 * calling get_user_pages().
205 *
206 * Returns number of pages pinned. This may be fewer than the number
207 * requested. If nr_pages is 0 or negative, returns 0. If no pages
208 * were pinned, returns -errno.
209 */
210int get_user_pages_fast(unsigned long start, int nr_pages, int write,
211 struct page **pages)
212{
Heiko Carstens01997bb2013-10-07 16:14:50 +0200213 int nr, ret;
Martin Schwidefsky80217142010-10-25 16:10:11 +0200214
David Hildenbrand406123512015-10-15 10:47:18 +0200215 might_sleep();
Martin Schwidefsky80217142010-10-25 16:10:11 +0200216 start &= PAGE_MASK;
Heiko Carstens01997bb2013-10-07 16:14:50 +0200217 nr = __get_user_pages_fast(start, nr_pages, write, pages);
218 if (nr == nr_pages)
219 return nr;
Martin Schwidefsky80217142010-10-25 16:10:11 +0200220
Heiko Carstens01997bb2013-10-07 16:14:50 +0200221 /* Try to get the remaining pages with get_user_pages */
222 start += nr << PAGE_SHIFT;
223 pages += nr;
Dave Hansend4edcf02016-02-12 13:01:56 -0800224 ret = get_user_pages_unlocked(start, nr_pages - nr, write, 0, pages);
Heiko Carstens01997bb2013-10-07 16:14:50 +0200225 /* Have to be a bit careful with return values */
226 if (nr > 0)
227 ret = (ret < 0) ? nr : ret + nr;
228 return ret;
Martin Schwidefsky80217142010-10-25 16:10:11 +0200229}