GNU Linux-libre 4.4.288-gnu1
[releases.git] / arch / s390 / mm / gup.c
1 /*
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  */
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)
22 {
23         unsigned long mask;
24         pte_t *ptep, pte;
25         struct page *page;
26
27         mask = (write ? _PAGE_PROTECT : 0) | _PAGE_INVALID | _PAGE_SPECIAL;
28
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                 if (WARN_ON_ONCE(page_ref_count(page) < 0)
41                     || !page_cache_get_speculative(page))
42                         return 0;
43                 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
44                         put_page(page);
45                         return 0;
46                 }
47                 pages[*nr] = page;
48                 (*nr)++;
49
50         } while (ptep++, addr += PAGE_SIZE, addr != end);
51
52         return 1;
53 }
54
55 static inline int gup_huge_pmd(pmd_t *pmdp, pmd_t pmd, unsigned long addr,
56                 unsigned long end, int write, struct page **pages, int *nr)
57 {
58         unsigned long mask, result;
59         struct page *head, *page, *tail;
60         int refs;
61
62         result = write ? 0 : _SEGMENT_ENTRY_PROTECT;
63         mask = result | _SEGMENT_ENTRY_INVALID;
64         if ((pmd_val(pmd) & mask) != result)
65                 return 0;
66         VM_BUG_ON(!pfn_valid(pmd_val(pmd) >> PAGE_SHIFT));
67
68         refs = 0;
69         head = pmd_page(pmd);
70         page = head + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
71         tail = page;
72         do {
73                 VM_BUG_ON(compound_head(page) != head);
74                 pages[*nr] = page;
75                 (*nr)++;
76                 page++;
77                 refs++;
78         } while (addr += PAGE_SIZE, addr != end);
79
80         if (WARN_ON_ONCE(page_ref_count(head) < 0)
81             || !page_cache_add_speculative(head, refs)) {
82                 *nr -= refs;
83                 return 0;
84         }
85
86         if (unlikely(pmd_val(pmd) != pmd_val(*pmdp))) {
87                 *nr -= refs;
88                 while (refs--)
89                         put_page(head);
90                 return 0;
91         }
92
93         /*
94          * Any tail page need their mapcount reference taken before we
95          * return.
96          */
97         while (refs--) {
98                 if (PageTail(tail))
99                         get_huge_page_tail(tail);
100                 tail++;
101         }
102
103         return 1;
104 }
105
106
107 static inline int gup_pmd_range(pud_t *pudp, pud_t pud, unsigned long addr,
108                 unsigned long end, int write, struct page **pages, int *nr)
109 {
110         unsigned long next;
111         pmd_t *pmdp, pmd;
112
113         pmdp = (pmd_t *) pudp;
114         if ((pud_val(pud) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R3)
115                 pmdp = (pmd_t *) pud_deref(pud);
116         pmdp += pmd_index(addr);
117         do {
118                 pmd = *pmdp;
119                 barrier();
120                 next = pmd_addr_end(addr, end);
121                 /*
122                  * The pmd_trans_splitting() check below explains why
123                  * pmdp_splitting_flush() has to serialize with
124                  * smp_call_function() against our disabled IRQs, to stop
125                  * this gup-fast code from running while we set the
126                  * splitting bit in the pmd. Returning zero will take
127                  * the slow path that will call wait_split_huge_page()
128                  * if the pmd is still in splitting state.
129                  */
130                 if (pmd_none(pmd) || pmd_trans_splitting(pmd))
131                         return 0;
132                 if (unlikely(pmd_large(pmd))) {
133                         /*
134                          * NUMA hinting faults need to be handled in the GUP
135                          * slowpath for accounting purposes and so that they
136                          * can be serialised against THP migration.
137                          */
138                         if (pmd_protnone(pmd))
139                                 return 0;
140                         if (!gup_huge_pmd(pmdp, pmd, addr, next,
141                                           write, pages, nr))
142                                 return 0;
143                 } else if (!gup_pte_range(pmdp, pmd, addr, next,
144                                           write, pages, nr))
145                         return 0;
146         } while (pmdp++, addr = next, addr != end);
147
148         return 1;
149 }
150
151 static inline int gup_pud_range(pgd_t *pgdp, pgd_t pgd, unsigned long addr,
152                 unsigned long end, int write, struct page **pages, int *nr)
153 {
154         unsigned long next;
155         pud_t *pudp, pud;
156
157         pudp = (pud_t *) pgdp;
158         if ((pgd_val(pgd) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R2)
159                 pudp = (pud_t *) pgd_deref(pgd);
160         pudp += pud_index(addr);
161         do {
162                 pud = *pudp;
163                 barrier();
164                 next = pud_addr_end(addr, end);
165                 if (pud_none(pud))
166                         return 0;
167                 if (!gup_pmd_range(pudp, pud, addr, next, write, pages, nr))
168                         return 0;
169         } while (pudp++, addr = next, addr != end);
170
171         return 1;
172 }
173
174 /*
175  * Like get_user_pages_fast() except its IRQ-safe in that it won't fall
176  * back to the regular GUP.
177  */
178 int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
179                           struct page **pages)
180 {
181         struct mm_struct *mm = current->mm;
182         unsigned long addr, len, end;
183         unsigned long next, flags;
184         pgd_t *pgdp, pgd;
185         int nr = 0;
186
187         start &= PAGE_MASK;
188         addr = start;
189         len = (unsigned long) nr_pages << PAGE_SHIFT;
190         end = start + len;
191         if ((end <= start) || (end > TASK_SIZE))
192                 return 0;
193         /*
194          * local_irq_save() doesn't prevent pagetable teardown, but does
195          * prevent the pagetables from being freed on s390.
196          *
197          * So long as we atomically load page table pointers versus teardown,
198          * we can follow the address down to the the page and take a ref on it.
199          */
200         local_irq_save(flags);
201         pgdp = pgd_offset(mm, addr);
202         do {
203                 pgd = *pgdp;
204                 barrier();
205                 next = pgd_addr_end(addr, end);
206                 if (pgd_none(pgd))
207                         break;
208                 if (!gup_pud_range(pgdp, pgd, addr, next, write, pages, &nr))
209                         break;
210         } while (pgdp++, addr = next, addr != end);
211         local_irq_restore(flags);
212
213         return nr;
214 }
215
216 /**
217  * get_user_pages_fast() - pin user pages in memory
218  * @start:      starting user address
219  * @nr_pages:   number of pages from start to pin
220  * @write:      whether pages will be written to
221  * @pages:      array that receives pointers to the pages pinned.
222  *              Should be at least nr_pages long.
223  *
224  * Attempt to pin user pages in memory without taking mm->mmap_sem.
225  * If not successful, it will fall back to taking the lock and
226  * calling get_user_pages().
227  *
228  * Returns number of pages pinned. This may be fewer than the number
229  * requested. If nr_pages is 0 or negative, returns 0. If no pages
230  * were pinned, returns -errno.
231  */
232 int get_user_pages_fast(unsigned long start, int nr_pages, int write,
233                         struct page **pages)
234 {
235         struct mm_struct *mm = current->mm;
236         int nr, ret;
237
238         start &= PAGE_MASK;
239         nr = __get_user_pages_fast(start, nr_pages, write, pages);
240         if (nr == nr_pages)
241                 return nr;
242
243         /* Try to get the remaining pages with get_user_pages */
244         start += nr << PAGE_SHIFT;
245         pages += nr;
246         ret = get_user_pages_unlocked(current, mm, start,
247                              nr_pages - nr, pages, write ? FOLL_WRITE : 0);
248         /* Have to be a bit careful with return values */
249         if (nr > 0)
250                 ret = (ret < 0) ? nr : ret + nr;
251         return ret;
252 }