Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 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 | { |
Martin Schwidefsky | 25591b0 | 2010-11-10 10:05:51 +0100 | [diff] [blame] | 23 | unsigned long mask; |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 24 | pte_t *ptep, pte; |
| 25 | struct page *page; |
| 26 | |
Martin Schwidefsky | e509861 | 2013-07-23 20:57:57 +0200 | [diff] [blame] | 27 | mask = (write ? _PAGE_PROTECT : 0) | _PAGE_INVALID | _PAGE_SPECIAL; |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 28 | |
| 29 | ptep = ((pte_t *) pmd_deref(pmd)) + pte_index(addr); |
| 30 | do { |
| 31 | pte = *ptep; |
| 32 | barrier(); |
Gerald Schaefer | ecf46ab | 2015-05-29 15:34:51 +0200 | [diff] [blame] | 33 | /* Similar to the PMD case, NUMA hinting must take slow path */ |
| 34 | if (pte_protnone(pte)) |
| 35 | return 0; |
Martin Schwidefsky | 25591b0 | 2010-11-10 10:05:51 +0100 | [diff] [blame] | 36 | if ((pte_val(pte) & mask) != 0) |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 37 | 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 | |
| 54 | static 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. Shutemov | ddc58f2 | 2016-01-15 16:52:56 -0800 | [diff] [blame^] | 58 | struct page *head, *page; |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 59 | int refs; |
| 60 | |
Martin Schwidefsky | e509861 | 2013-07-23 20:57:57 +0200 | [diff] [blame] | 61 | result = write ? 0 : _SEGMENT_ENTRY_PROTECT; |
| 62 | mask = result | _SEGMENT_ENTRY_INVALID; |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 63 | 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 Arcangeli | 0693bc9 | 2011-11-02 13:37:28 -0700 | [diff] [blame] | 87 | return 0; |
| 88 | } |
| 89 | |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 90 | return 1; |
| 91 | } |
| 92 | |
| 93 | |
| 94 | static 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 Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 101 | 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 Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 104 | do { |
| 105 | pmd = *pmdp; |
| 106 | barrier(); |
| 107 | next = pmd_addr_end(addr, end); |
Gerald Schaefer | 75077af | 2012-10-08 16:30:15 -0700 | [diff] [blame] | 108 | /* |
| 109 | * The pmd_trans_splitting() check below explains why |
| 110 | * pmdp_splitting_flush() has to serialize with |
| 111 | * smp_call_function() against our disabled IRQs, to stop |
| 112 | * this gup-fast code from running while we set the |
| 113 | * splitting bit in the pmd. Returning zero will take |
| 114 | * the slow path that will call wait_split_huge_page() |
| 115 | * if the pmd is still in splitting state. |
| 116 | */ |
| 117 | if (pmd_none(pmd) || pmd_trans_splitting(pmd)) |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 118 | return 0; |
Gerald Schaefer | 156152f | 2012-10-25 17:24:12 +0200 | [diff] [blame] | 119 | if (unlikely(pmd_large(pmd))) { |
Gerald Schaefer | ecf46ab | 2015-05-29 15:34:51 +0200 | [diff] [blame] | 120 | /* |
| 121 | * NUMA hinting faults need to be handled in the GUP |
| 122 | * slowpath for accounting purposes and so that they |
| 123 | * can be serialised against THP migration. |
| 124 | */ |
| 125 | if (pmd_protnone(pmd)) |
| 126 | return 0; |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 127 | if (!gup_huge_pmd(pmdp, pmd, addr, next, |
| 128 | write, pages, nr)) |
| 129 | return 0; |
| 130 | } else if (!gup_pte_range(pmdp, pmd, addr, next, |
| 131 | write, pages, nr)) |
| 132 | return 0; |
| 133 | } while (pmdp++, addr = next, addr != end); |
| 134 | |
| 135 | return 1; |
| 136 | } |
| 137 | |
| 138 | static inline int gup_pud_range(pgd_t *pgdp, pgd_t pgd, unsigned long addr, |
| 139 | unsigned long end, int write, struct page **pages, int *nr) |
| 140 | { |
| 141 | unsigned long next; |
| 142 | pud_t *pudp, pud; |
| 143 | |
| 144 | pudp = (pud_t *) pgdp; |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 145 | if ((pgd_val(pgd) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R2) |
| 146 | pudp = (pud_t *) pgd_deref(pgd); |
| 147 | pudp += pud_index(addr); |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 148 | do { |
| 149 | pud = *pudp; |
| 150 | barrier(); |
| 151 | next = pud_addr_end(addr, end); |
| 152 | if (pud_none(pud)) |
| 153 | return 0; |
| 154 | if (!gup_pmd_range(pudp, pud, addr, next, write, pages, nr)) |
| 155 | return 0; |
| 156 | } while (pudp++, addr = next, addr != end); |
| 157 | |
| 158 | return 1; |
| 159 | } |
| 160 | |
Gerald Schaefer | 34cda99 | 2012-09-04 15:37:55 +0200 | [diff] [blame] | 161 | /* |
| 162 | * Like get_user_pages_fast() except its IRQ-safe in that it won't fall |
| 163 | * back to the regular GUP. |
| 164 | */ |
| 165 | int __get_user_pages_fast(unsigned long start, int nr_pages, int write, |
| 166 | struct page **pages) |
| 167 | { |
| 168 | struct mm_struct *mm = current->mm; |
| 169 | unsigned long addr, len, end; |
| 170 | unsigned long next, flags; |
| 171 | pgd_t *pgdp, pgd; |
| 172 | int nr = 0; |
| 173 | |
| 174 | start &= PAGE_MASK; |
| 175 | addr = start; |
| 176 | len = (unsigned long) nr_pages << PAGE_SHIFT; |
| 177 | end = start + len; |
Heiko Carstens | eb0bf92 | 2013-10-08 09:29:09 +0200 | [diff] [blame] | 178 | if ((end <= start) || (end > TASK_SIZE)) |
Gerald Schaefer | 34cda99 | 2012-09-04 15:37:55 +0200 | [diff] [blame] | 179 | return 0; |
Heiko Carstens | 01997bb | 2013-10-07 16:14:50 +0200 | [diff] [blame] | 180 | /* |
| 181 | * local_irq_save() doesn't prevent pagetable teardown, but does |
| 182 | * prevent the pagetables from being freed on s390. |
| 183 | * |
| 184 | * So long as we atomically load page table pointers versus teardown, |
| 185 | * we can follow the address down to the the page and take a ref on it. |
| 186 | */ |
Gerald Schaefer | 34cda99 | 2012-09-04 15:37:55 +0200 | [diff] [blame] | 187 | local_irq_save(flags); |
| 188 | pgdp = pgd_offset(mm, addr); |
| 189 | do { |
| 190 | pgd = *pgdp; |
| 191 | barrier(); |
| 192 | next = pgd_addr_end(addr, end); |
| 193 | if (pgd_none(pgd)) |
| 194 | break; |
| 195 | if (!gup_pud_range(pgdp, pgd, addr, next, write, pages, &nr)) |
| 196 | break; |
| 197 | } while (pgdp++, addr = next, addr != end); |
| 198 | local_irq_restore(flags); |
| 199 | |
| 200 | return nr; |
| 201 | } |
| 202 | |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 203 | /** |
| 204 | * get_user_pages_fast() - pin user pages in memory |
| 205 | * @start: starting user address |
| 206 | * @nr_pages: number of pages from start to pin |
| 207 | * @write: whether pages will be written to |
| 208 | * @pages: array that receives pointers to the pages pinned. |
| 209 | * Should be at least nr_pages long. |
| 210 | * |
| 211 | * Attempt to pin user pages in memory without taking mm->mmap_sem. |
| 212 | * If not successful, it will fall back to taking the lock and |
| 213 | * calling get_user_pages(). |
| 214 | * |
| 215 | * Returns number of pages pinned. This may be fewer than the number |
| 216 | * requested. If nr_pages is 0 or negative, returns 0. If no pages |
| 217 | * were pinned, returns -errno. |
| 218 | */ |
| 219 | int get_user_pages_fast(unsigned long start, int nr_pages, int write, |
| 220 | struct page **pages) |
| 221 | { |
| 222 | struct mm_struct *mm = current->mm; |
Heiko Carstens | 01997bb | 2013-10-07 16:14:50 +0200 | [diff] [blame] | 223 | int nr, ret; |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 224 | |
David Hildenbrand | 40612351 | 2015-10-15 10:47:18 +0200 | [diff] [blame] | 225 | might_sleep(); |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 226 | start &= PAGE_MASK; |
Heiko Carstens | 01997bb | 2013-10-07 16:14:50 +0200 | [diff] [blame] | 227 | nr = __get_user_pages_fast(start, nr_pages, write, pages); |
| 228 | if (nr == nr_pages) |
| 229 | return nr; |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 230 | |
Heiko Carstens | 01997bb | 2013-10-07 16:14:50 +0200 | [diff] [blame] | 231 | /* Try to get the remaining pages with get_user_pages */ |
| 232 | start += nr << PAGE_SHIFT; |
| 233 | pages += nr; |
Andrea Arcangeli | a7b7807 | 2015-02-11 15:27:23 -0800 | [diff] [blame] | 234 | ret = get_user_pages_unlocked(current, mm, start, |
| 235 | nr_pages - nr, write, 0, pages); |
Heiko Carstens | 01997bb | 2013-10-07 16:14:50 +0200 | [diff] [blame] | 236 | /* Have to be a bit careful with return values */ |
| 237 | if (nr > 0) |
| 238 | ret = (ret < 0) ? nr : ret + nr; |
| 239 | return ret; |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 240 | } |