Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 1 | #include <linux/kernel.h> |
| 2 | #include <linux/errno.h> |
| 3 | #include <linux/err.h> |
| 4 | #include <linux/spinlock.h> |
| 5 | |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 6 | #include <linux/mm.h> |
| 7 | #include <linux/pagemap.h> |
| 8 | #include <linux/rmap.h> |
| 9 | #include <linux/swap.h> |
| 10 | #include <linux/swapops.h> |
| 11 | |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 12 | #include <linux/sched.h> |
| 13 | #include <linux/rwsem.h> |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 14 | #include <linux/hugetlb.h> |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 15 | #include <asm/pgtable.h> |
| 16 | |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 17 | #include "internal.h" |
| 18 | |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 19 | static struct page *no_page_table(struct vm_area_struct *vma, |
| 20 | unsigned int flags) |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 21 | { |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 22 | /* |
| 23 | * When core dumping an enormous anonymous area that nobody |
| 24 | * has touched so far, we don't want to allocate unnecessary pages or |
| 25 | * page tables. Return error instead of NULL to skip handle_mm_fault, |
| 26 | * then get_dump_page() will return NULL to leave a hole in the dump. |
| 27 | * But we can only make this optimization where a hole would surely |
| 28 | * be zero-filled if handle_mm_fault() actually did handle it. |
| 29 | */ |
| 30 | if ((flags & FOLL_DUMP) && (!vma->vm_ops || !vma->vm_ops->fault)) |
| 31 | return ERR_PTR(-EFAULT); |
| 32 | return NULL; |
| 33 | } |
| 34 | |
Linus Torvalds | c865f98 | 2016-10-13 13:07:36 -0700 | [diff] [blame^] | 35 | /* |
| 36 | * FOLL_FORCE can write to even unwritable pte's, but only |
| 37 | * after we've gone through a COW cycle and they are dirty. |
| 38 | */ |
| 39 | static inline bool can_follow_write_pte(pte_t pte, unsigned int flags) |
| 40 | { |
| 41 | return pte_write(pte) || |
| 42 | ((flags & FOLL_FORCE) && (flags & FOLL_COW) && pte_dirty(pte)); |
| 43 | } |
| 44 | |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 45 | static struct page *follow_page_pte(struct vm_area_struct *vma, |
| 46 | unsigned long address, pmd_t *pmd, unsigned int flags) |
| 47 | { |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 48 | struct mm_struct *mm = vma->vm_mm; |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 49 | struct page *page; |
| 50 | spinlock_t *ptl; |
| 51 | pte_t *ptep, pte; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 52 | |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 53 | retry: |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 54 | if (unlikely(pmd_bad(*pmd))) |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 55 | return no_page_table(vma, flags); |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 56 | |
| 57 | ptep = pte_offset_map_lock(mm, pmd, address, &ptl); |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 58 | pte = *ptep; |
| 59 | if (!pte_present(pte)) { |
| 60 | swp_entry_t entry; |
| 61 | /* |
| 62 | * KSM's break_ksm() relies upon recognizing a ksm page |
| 63 | * even while it is being migrated, so for that case we |
| 64 | * need migration_entry_wait(). |
| 65 | */ |
| 66 | if (likely(!(flags & FOLL_MIGRATION))) |
| 67 | goto no_page; |
Kirill A. Shutemov | 0661a33 | 2015-02-10 14:10:04 -0800 | [diff] [blame] | 68 | if (pte_none(pte)) |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 69 | goto no_page; |
| 70 | entry = pte_to_swp_entry(pte); |
| 71 | if (!is_migration_entry(entry)) |
| 72 | goto no_page; |
| 73 | pte_unmap_unlock(ptep, ptl); |
| 74 | migration_entry_wait(mm, pmd, address); |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 75 | goto retry; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 76 | } |
Mel Gorman | 8a0516e | 2015-02-12 14:58:22 -0800 | [diff] [blame] | 77 | if ((flags & FOLL_NUMA) && pte_protnone(pte)) |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 78 | goto no_page; |
Linus Torvalds | c865f98 | 2016-10-13 13:07:36 -0700 | [diff] [blame^] | 79 | if ((flags & FOLL_WRITE) && !can_follow_write_pte(pte, flags)) { |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 80 | pte_unmap_unlock(ptep, ptl); |
| 81 | return NULL; |
| 82 | } |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 83 | |
| 84 | page = vm_normal_page(vma, address, pte); |
| 85 | if (unlikely(!page)) { |
| 86 | if ((flags & FOLL_DUMP) || |
| 87 | !is_zero_pfn(pte_pfn(pte))) |
| 88 | goto bad_page; |
| 89 | page = pte_page(pte); |
| 90 | } |
| 91 | |
| 92 | if (flags & FOLL_GET) |
| 93 | get_page_foll(page); |
| 94 | if (flags & FOLL_TOUCH) { |
| 95 | if ((flags & FOLL_WRITE) && |
| 96 | !pte_dirty(pte) && !PageDirty(page)) |
| 97 | set_page_dirty(page); |
| 98 | /* |
| 99 | * pte_mkyoung() would be more correct here, but atomic care |
| 100 | * is needed to avoid losing the dirty bit: it is easier to use |
| 101 | * mark_page_accessed(). |
| 102 | */ |
| 103 | mark_page_accessed(page); |
| 104 | } |
Kirill A. Shutemov | 84d33df | 2015-04-14 15:44:37 -0700 | [diff] [blame] | 105 | if ((flags & FOLL_POPULATE) && (vma->vm_flags & VM_LOCKED)) { |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 106 | /* |
| 107 | * The preliminary mapping check is mainly to avoid the |
| 108 | * pointless overhead of lock_page on the ZERO_PAGE |
| 109 | * which might bounce very badly if there is contention. |
| 110 | * |
| 111 | * If the page is already locked, we don't need to |
| 112 | * handle it now - vmscan will handle it later if and |
| 113 | * when it attempts to reclaim the page. |
| 114 | */ |
| 115 | if (page->mapping && trylock_page(page)) { |
| 116 | lru_add_drain(); /* push cached pages to LRU */ |
| 117 | /* |
| 118 | * Because we lock page here, and migration is |
| 119 | * blocked by the pte's page reference, and we |
| 120 | * know the page is still mapped, we don't even |
| 121 | * need to check for file-cache page truncation. |
| 122 | */ |
| 123 | mlock_vma_page(page); |
| 124 | unlock_page(page); |
| 125 | } |
| 126 | } |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 127 | pte_unmap_unlock(ptep, ptl); |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 128 | return page; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 129 | bad_page: |
| 130 | pte_unmap_unlock(ptep, ptl); |
| 131 | return ERR_PTR(-EFAULT); |
| 132 | |
| 133 | no_page: |
| 134 | pte_unmap_unlock(ptep, ptl); |
| 135 | if (!pte_none(pte)) |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 136 | return NULL; |
| 137 | return no_page_table(vma, flags); |
| 138 | } |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 139 | |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 140 | /** |
| 141 | * follow_page_mask - look up a page descriptor from a user-virtual address |
| 142 | * @vma: vm_area_struct mapping @address |
| 143 | * @address: virtual address to look up |
| 144 | * @flags: flags modifying lookup behaviour |
| 145 | * @page_mask: on output, *page_mask is set according to the size of the page |
| 146 | * |
| 147 | * @flags can have FOLL_ flags set, defined in <linux/mm.h> |
| 148 | * |
| 149 | * Returns the mapped (struct page *), %NULL if no mapping exists, or |
| 150 | * an error pointer if there is a mapping to something not represented |
| 151 | * by a page descriptor (see also vm_normal_page()). |
| 152 | */ |
| 153 | struct page *follow_page_mask(struct vm_area_struct *vma, |
| 154 | unsigned long address, unsigned int flags, |
| 155 | unsigned int *page_mask) |
| 156 | { |
| 157 | pgd_t *pgd; |
| 158 | pud_t *pud; |
| 159 | pmd_t *pmd; |
| 160 | spinlock_t *ptl; |
| 161 | struct page *page; |
| 162 | struct mm_struct *mm = vma->vm_mm; |
| 163 | |
| 164 | *page_mask = 0; |
| 165 | |
| 166 | page = follow_huge_addr(mm, address, flags & FOLL_WRITE); |
| 167 | if (!IS_ERR(page)) { |
| 168 | BUG_ON(flags & FOLL_GET); |
| 169 | return page; |
| 170 | } |
| 171 | |
| 172 | pgd = pgd_offset(mm, address); |
| 173 | if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) |
| 174 | return no_page_table(vma, flags); |
| 175 | |
| 176 | pud = pud_offset(pgd, address); |
| 177 | if (pud_none(*pud)) |
| 178 | return no_page_table(vma, flags); |
| 179 | if (pud_huge(*pud) && vma->vm_flags & VM_HUGETLB) { |
Naoya Horiguchi | e66f17f | 2015-02-11 15:25:22 -0800 | [diff] [blame] | 180 | page = follow_huge_pud(mm, address, pud, flags); |
| 181 | if (page) |
| 182 | return page; |
| 183 | return no_page_table(vma, flags); |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 184 | } |
| 185 | if (unlikely(pud_bad(*pud))) |
| 186 | return no_page_table(vma, flags); |
| 187 | |
| 188 | pmd = pmd_offset(pud, address); |
| 189 | if (pmd_none(*pmd)) |
| 190 | return no_page_table(vma, flags); |
| 191 | if (pmd_huge(*pmd) && vma->vm_flags & VM_HUGETLB) { |
Naoya Horiguchi | e66f17f | 2015-02-11 15:25:22 -0800 | [diff] [blame] | 192 | page = follow_huge_pmd(mm, address, pmd, flags); |
| 193 | if (page) |
| 194 | return page; |
| 195 | return no_page_table(vma, flags); |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 196 | } |
Mel Gorman | 8a0516e | 2015-02-12 14:58:22 -0800 | [diff] [blame] | 197 | if ((flags & FOLL_NUMA) && pmd_protnone(*pmd)) |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 198 | return no_page_table(vma, flags); |
| 199 | if (pmd_trans_huge(*pmd)) { |
| 200 | if (flags & FOLL_SPLIT) { |
| 201 | split_huge_page_pmd(vma, address, pmd); |
| 202 | return follow_page_pte(vma, address, pmd, flags); |
| 203 | } |
| 204 | ptl = pmd_lock(mm, pmd); |
| 205 | if (likely(pmd_trans_huge(*pmd))) { |
| 206 | if (unlikely(pmd_trans_splitting(*pmd))) { |
| 207 | spin_unlock(ptl); |
| 208 | wait_split_huge_page(vma->anon_vma, pmd); |
| 209 | } else { |
| 210 | page = follow_trans_huge_pmd(vma, address, |
| 211 | pmd, flags); |
| 212 | spin_unlock(ptl); |
| 213 | *page_mask = HPAGE_PMD_NR - 1; |
| 214 | return page; |
| 215 | } |
| 216 | } else |
| 217 | spin_unlock(ptl); |
| 218 | } |
| 219 | return follow_page_pte(vma, address, pmd, flags); |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Kirill A. Shutemov | f2b495c | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 222 | static int get_gate_page(struct mm_struct *mm, unsigned long address, |
| 223 | unsigned int gup_flags, struct vm_area_struct **vma, |
| 224 | struct page **page) |
| 225 | { |
| 226 | pgd_t *pgd; |
| 227 | pud_t *pud; |
| 228 | pmd_t *pmd; |
| 229 | pte_t *pte; |
| 230 | int ret = -EFAULT; |
| 231 | |
| 232 | /* user gate pages are read-only */ |
| 233 | if (gup_flags & FOLL_WRITE) |
| 234 | return -EFAULT; |
| 235 | if (address > TASK_SIZE) |
| 236 | pgd = pgd_offset_k(address); |
| 237 | else |
| 238 | pgd = pgd_offset_gate(mm, address); |
| 239 | BUG_ON(pgd_none(*pgd)); |
| 240 | pud = pud_offset(pgd, address); |
| 241 | BUG_ON(pud_none(*pud)); |
| 242 | pmd = pmd_offset(pud, address); |
| 243 | if (pmd_none(*pmd)) |
| 244 | return -EFAULT; |
| 245 | VM_BUG_ON(pmd_trans_huge(*pmd)); |
| 246 | pte = pte_offset_map(pmd, address); |
| 247 | if (pte_none(*pte)) |
| 248 | goto unmap; |
| 249 | *vma = get_gate_vma(mm); |
| 250 | if (!page) |
| 251 | goto out; |
| 252 | *page = vm_normal_page(*vma, address, *pte); |
| 253 | if (!*page) { |
| 254 | if ((gup_flags & FOLL_DUMP) || !is_zero_pfn(pte_pfn(*pte))) |
| 255 | goto unmap; |
| 256 | *page = pte_page(*pte); |
| 257 | } |
| 258 | get_page(*page); |
| 259 | out: |
| 260 | ret = 0; |
| 261 | unmap: |
| 262 | pte_unmap(pte); |
| 263 | return ret; |
| 264 | } |
| 265 | |
Paul Cassella | 9a95f3c | 2014-08-06 16:07:24 -0700 | [diff] [blame] | 266 | /* |
| 267 | * mmap_sem must be held on entry. If @nonblocking != NULL and |
| 268 | * *@flags does not include FOLL_NOWAIT, the mmap_sem may be released. |
| 269 | * If it is, *@nonblocking will be set to 0 and -EBUSY returned. |
| 270 | */ |
Kirill A. Shutemov | 1674448 | 2014-06-04 16:08:12 -0700 | [diff] [blame] | 271 | static int faultin_page(struct task_struct *tsk, struct vm_area_struct *vma, |
| 272 | unsigned long address, unsigned int *flags, int *nonblocking) |
| 273 | { |
| 274 | struct mm_struct *mm = vma->vm_mm; |
| 275 | unsigned int fault_flags = 0; |
| 276 | int ret; |
| 277 | |
Kirill A. Shutemov | 84d33df | 2015-04-14 15:44:37 -0700 | [diff] [blame] | 278 | /* For mm_populate(), just skip the stack guard page. */ |
| 279 | if ((*flags & FOLL_POPULATE) && |
Kirill A. Shutemov | 1674448 | 2014-06-04 16:08:12 -0700 | [diff] [blame] | 280 | (stack_guard_page_start(vma, address) || |
| 281 | stack_guard_page_end(vma, address + PAGE_SIZE))) |
| 282 | return -ENOENT; |
| 283 | if (*flags & FOLL_WRITE) |
| 284 | fault_flags |= FAULT_FLAG_WRITE; |
| 285 | if (nonblocking) |
| 286 | fault_flags |= FAULT_FLAG_ALLOW_RETRY; |
| 287 | if (*flags & FOLL_NOWAIT) |
| 288 | fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT; |
Andres Lagar-Cavilla | 234b239 | 2014-09-17 10:51:48 -0700 | [diff] [blame] | 289 | if (*flags & FOLL_TRIED) { |
| 290 | VM_WARN_ON_ONCE(fault_flags & FAULT_FLAG_ALLOW_RETRY); |
| 291 | fault_flags |= FAULT_FLAG_TRIED; |
| 292 | } |
Kirill A. Shutemov | 1674448 | 2014-06-04 16:08:12 -0700 | [diff] [blame] | 293 | |
| 294 | ret = handle_mm_fault(mm, vma, address, fault_flags); |
| 295 | if (ret & VM_FAULT_ERROR) { |
| 296 | if (ret & VM_FAULT_OOM) |
| 297 | return -ENOMEM; |
| 298 | if (ret & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE)) |
| 299 | return *flags & FOLL_HWPOISON ? -EHWPOISON : -EFAULT; |
Linus Torvalds | 33692f2 | 2015-01-29 10:51:32 -0800 | [diff] [blame] | 300 | if (ret & (VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV)) |
Kirill A. Shutemov | 1674448 | 2014-06-04 16:08:12 -0700 | [diff] [blame] | 301 | return -EFAULT; |
| 302 | BUG(); |
| 303 | } |
| 304 | |
| 305 | if (tsk) { |
| 306 | if (ret & VM_FAULT_MAJOR) |
| 307 | tsk->maj_flt++; |
| 308 | else |
| 309 | tsk->min_flt++; |
| 310 | } |
| 311 | |
| 312 | if (ret & VM_FAULT_RETRY) { |
| 313 | if (nonblocking) |
| 314 | *nonblocking = 0; |
| 315 | return -EBUSY; |
| 316 | } |
| 317 | |
| 318 | /* |
| 319 | * The VM_FAULT_WRITE bit tells us that do_wp_page has broken COW when |
| 320 | * necessary, even if maybe_mkwrite decided not to set pte_write. We |
| 321 | * can thus safely do subsequent page lookups as if they were reads. |
| 322 | * But only do so when looping for pte_write is futile: in some cases |
| 323 | * userspace may also be wanting to write to the gotten user page, |
| 324 | * which a read fault here might prevent (a readonly page might get |
| 325 | * reCOWed by userspace write). |
| 326 | */ |
| 327 | if ((ret & VM_FAULT_WRITE) && !(vma->vm_flags & VM_WRITE)) |
Linus Torvalds | c865f98 | 2016-10-13 13:07:36 -0700 | [diff] [blame^] | 328 | *flags |= FOLL_COW; |
Kirill A. Shutemov | 1674448 | 2014-06-04 16:08:12 -0700 | [diff] [blame] | 329 | return 0; |
| 330 | } |
| 331 | |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 332 | static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags) |
| 333 | { |
| 334 | vm_flags_t vm_flags = vma->vm_flags; |
| 335 | |
| 336 | if (vm_flags & (VM_IO | VM_PFNMAP)) |
| 337 | return -EFAULT; |
| 338 | |
| 339 | if (gup_flags & FOLL_WRITE) { |
| 340 | if (!(vm_flags & VM_WRITE)) { |
| 341 | if (!(gup_flags & FOLL_FORCE)) |
| 342 | return -EFAULT; |
| 343 | /* |
| 344 | * We used to let the write,force case do COW in a |
| 345 | * VM_MAYWRITE VM_SHARED !VM_WRITE vma, so ptrace could |
| 346 | * set a breakpoint in a read-only mapping of an |
| 347 | * executable, without corrupting the file (yet only |
| 348 | * when that file had been opened for writing!). |
| 349 | * Anon pages in shared mappings are surprising: now |
| 350 | * just reject it. |
| 351 | */ |
| 352 | if (!is_cow_mapping(vm_flags)) { |
| 353 | WARN_ON_ONCE(vm_flags & VM_MAYWRITE); |
| 354 | return -EFAULT; |
| 355 | } |
| 356 | } |
| 357 | } else if (!(vm_flags & VM_READ)) { |
| 358 | if (!(gup_flags & FOLL_FORCE)) |
| 359 | return -EFAULT; |
| 360 | /* |
| 361 | * Is there actually any vma we can reach here which does not |
| 362 | * have VM_MAYREAD set? |
| 363 | */ |
| 364 | if (!(vm_flags & VM_MAYREAD)) |
| 365 | return -EFAULT; |
| 366 | } |
| 367 | return 0; |
| 368 | } |
| 369 | |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 370 | /** |
| 371 | * __get_user_pages() - pin user pages in memory |
| 372 | * @tsk: task_struct of target task |
| 373 | * @mm: mm_struct of target mm |
| 374 | * @start: starting user address |
| 375 | * @nr_pages: number of pages from start to pin |
| 376 | * @gup_flags: flags modifying pin behaviour |
| 377 | * @pages: array that receives pointers to the pages pinned. |
| 378 | * Should be at least nr_pages long. Or NULL, if caller |
| 379 | * only intends to ensure the pages are faulted in. |
| 380 | * @vmas: array of pointers to vmas corresponding to each page. |
| 381 | * Or NULL if the caller does not require them. |
| 382 | * @nonblocking: whether waiting for disk IO or mmap_sem contention |
| 383 | * |
| 384 | * Returns number of pages pinned. This may be fewer than the number |
| 385 | * requested. If nr_pages is 0 or negative, returns 0. If no pages |
| 386 | * were pinned, returns -errno. Each page returned must be released |
| 387 | * with a put_page() call when it is finished with. vmas will only |
| 388 | * remain valid while mmap_sem is held. |
| 389 | * |
Paul Cassella | 9a95f3c | 2014-08-06 16:07:24 -0700 | [diff] [blame] | 390 | * Must be called with mmap_sem held. It may be released. See below. |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 391 | * |
| 392 | * __get_user_pages walks a process's page tables and takes a reference to |
| 393 | * each struct page that each user address corresponds to at a given |
| 394 | * instant. That is, it takes the page that would be accessed if a user |
| 395 | * thread accesses the given user virtual address at that instant. |
| 396 | * |
| 397 | * This does not guarantee that the page exists in the user mappings when |
| 398 | * __get_user_pages returns, and there may even be a completely different |
| 399 | * page there in some cases (eg. if mmapped pagecache has been invalidated |
| 400 | * and subsequently re faulted). However it does guarantee that the page |
| 401 | * won't be freed completely. And mostly callers simply care that the page |
| 402 | * contains data that was valid *at some point in time*. Typically, an IO |
| 403 | * or similar operation cannot guarantee anything stronger anyway because |
| 404 | * locks can't be held over the syscall boundary. |
| 405 | * |
| 406 | * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If |
| 407 | * the page is written to, set_page_dirty (or set_page_dirty_lock, as |
| 408 | * appropriate) must be called after the page is finished with, and |
| 409 | * before put_page is called. |
| 410 | * |
| 411 | * If @nonblocking != NULL, __get_user_pages will not wait for disk IO |
| 412 | * or mmap_sem contention, and if waiting is needed to pin all pages, |
Paul Cassella | 9a95f3c | 2014-08-06 16:07:24 -0700 | [diff] [blame] | 413 | * *@nonblocking will be set to 0. Further, if @gup_flags does not |
| 414 | * include FOLL_NOWAIT, the mmap_sem will be released via up_read() in |
| 415 | * this case. |
| 416 | * |
| 417 | * A caller using such a combination of @nonblocking and @gup_flags |
| 418 | * must therefore hold the mmap_sem for reading only, and recognize |
| 419 | * when it's been released. Otherwise, it must be held for either |
| 420 | * reading or writing and will not be released. |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 421 | * |
| 422 | * In most cases, get_user_pages or get_user_pages_fast should be used |
| 423 | * instead of __get_user_pages. __get_user_pages should be used only if |
| 424 | * you need some special @gup_flags. |
| 425 | */ |
| 426 | long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm, |
| 427 | unsigned long start, unsigned long nr_pages, |
| 428 | unsigned int gup_flags, struct page **pages, |
| 429 | struct vm_area_struct **vmas, int *nonblocking) |
| 430 | { |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 431 | long i = 0; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 432 | unsigned int page_mask; |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 433 | struct vm_area_struct *vma = NULL; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 434 | |
| 435 | if (!nr_pages) |
| 436 | return 0; |
| 437 | |
| 438 | VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET)); |
| 439 | |
| 440 | /* |
| 441 | * If FOLL_FORCE is set then do not force a full fault as the hinting |
| 442 | * fault information is unrelated to the reference behaviour of a task |
| 443 | * using the address space |
| 444 | */ |
| 445 | if (!(gup_flags & FOLL_FORCE)) |
| 446 | gup_flags |= FOLL_NUMA; |
| 447 | |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 448 | do { |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 449 | struct page *page; |
| 450 | unsigned int foll_flags = gup_flags; |
| 451 | unsigned int page_increm; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 452 | |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 453 | /* first iteration or cross vma bound */ |
| 454 | if (!vma || start >= vma->vm_end) { |
| 455 | vma = find_extend_vma(mm, start); |
| 456 | if (!vma && in_gate_area(mm, start)) { |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 457 | int ret; |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 458 | ret = get_gate_page(mm, start & PAGE_MASK, |
| 459 | gup_flags, &vma, |
| 460 | pages ? &pages[i] : NULL); |
| 461 | if (ret) |
| 462 | return i ? : ret; |
| 463 | page_mask = 0; |
| 464 | goto next_page; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 465 | } |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 466 | |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 467 | if (!vma || check_vma_flags(vma, gup_flags)) |
| 468 | return i ? : -EFAULT; |
| 469 | if (is_vm_hugetlb_page(vma)) { |
| 470 | i = follow_hugetlb_page(mm, vma, pages, vmas, |
| 471 | &start, &nr_pages, i, |
| 472 | gup_flags); |
| 473 | continue; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 474 | } |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 475 | } |
| 476 | retry: |
| 477 | /* |
| 478 | * If we have a pending SIGKILL, don't keep faulting pages and |
| 479 | * potentially allocating memory. |
| 480 | */ |
| 481 | if (unlikely(fatal_signal_pending(current))) |
| 482 | return i ? i : -ERESTARTSYS; |
| 483 | cond_resched(); |
| 484 | page = follow_page_mask(vma, start, foll_flags, &page_mask); |
| 485 | if (!page) { |
| 486 | int ret; |
| 487 | ret = faultin_page(tsk, vma, start, &foll_flags, |
| 488 | nonblocking); |
| 489 | switch (ret) { |
| 490 | case 0: |
| 491 | goto retry; |
| 492 | case -EFAULT: |
| 493 | case -ENOMEM: |
| 494 | case -EHWPOISON: |
| 495 | return i ? i : ret; |
| 496 | case -EBUSY: |
| 497 | return i; |
| 498 | case -ENOENT: |
| 499 | goto next_page; |
| 500 | } |
| 501 | BUG(); |
| 502 | } |
| 503 | if (IS_ERR(page)) |
| 504 | return i ? i : PTR_ERR(page); |
| 505 | if (pages) { |
| 506 | pages[i] = page; |
| 507 | flush_anon_page(vma, page, start); |
| 508 | flush_dcache_page(page); |
| 509 | page_mask = 0; |
| 510 | } |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 511 | next_page: |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 512 | if (vmas) { |
| 513 | vmas[i] = vma; |
| 514 | page_mask = 0; |
| 515 | } |
| 516 | page_increm = 1 + (~(start >> PAGE_SHIFT) & page_mask); |
| 517 | if (page_increm > nr_pages) |
| 518 | page_increm = nr_pages; |
| 519 | i += page_increm; |
| 520 | start += page_increm * PAGE_SIZE; |
| 521 | nr_pages -= page_increm; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 522 | } while (nr_pages); |
| 523 | return i; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 524 | } |
| 525 | EXPORT_SYMBOL(__get_user_pages); |
| 526 | |
| 527 | /* |
| 528 | * fixup_user_fault() - manually resolve a user page fault |
| 529 | * @tsk: the task_struct to use for page fault accounting, or |
| 530 | * NULL if faults are not to be recorded. |
| 531 | * @mm: mm_struct of target mm |
| 532 | * @address: user address |
| 533 | * @fault_flags:flags to pass down to handle_mm_fault() |
| 534 | * |
| 535 | * This is meant to be called in the specific scenario where for locking reasons |
| 536 | * we try to access user memory in atomic context (within a pagefault_disable() |
| 537 | * section), this returns -EFAULT, and we want to resolve the user fault before |
| 538 | * trying again. |
| 539 | * |
| 540 | * Typically this is meant to be used by the futex code. |
| 541 | * |
| 542 | * The main difference with get_user_pages() is that this function will |
| 543 | * unconditionally call handle_mm_fault() which will in turn perform all the |
| 544 | * necessary SW fixup of the dirty and young bits in the PTE, while |
| 545 | * handle_mm_fault() only guarantees to update these in the struct page. |
| 546 | * |
| 547 | * This is important for some architectures where those bits also gate the |
| 548 | * access permission to the page because they are maintained in software. On |
| 549 | * such architectures, gup() will not be enough to make a subsequent access |
| 550 | * succeed. |
| 551 | * |
Paul Cassella | 9a95f3c | 2014-08-06 16:07:24 -0700 | [diff] [blame] | 552 | * This has the same semantics wrt the @mm->mmap_sem as does filemap_fault(). |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 553 | */ |
| 554 | int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm, |
| 555 | unsigned long address, unsigned int fault_flags) |
| 556 | { |
| 557 | struct vm_area_struct *vma; |
| 558 | vm_flags_t vm_flags; |
| 559 | int ret; |
| 560 | |
| 561 | vma = find_extend_vma(mm, address); |
| 562 | if (!vma || address < vma->vm_start) |
| 563 | return -EFAULT; |
| 564 | |
| 565 | vm_flags = (fault_flags & FAULT_FLAG_WRITE) ? VM_WRITE : VM_READ; |
| 566 | if (!(vm_flags & vma->vm_flags)) |
| 567 | return -EFAULT; |
| 568 | |
| 569 | ret = handle_mm_fault(mm, vma, address, fault_flags); |
| 570 | if (ret & VM_FAULT_ERROR) { |
| 571 | if (ret & VM_FAULT_OOM) |
| 572 | return -ENOMEM; |
| 573 | if (ret & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE)) |
| 574 | return -EHWPOISON; |
Linus Torvalds | 33692f2 | 2015-01-29 10:51:32 -0800 | [diff] [blame] | 575 | if (ret & (VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV)) |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 576 | return -EFAULT; |
| 577 | BUG(); |
| 578 | } |
| 579 | if (tsk) { |
| 580 | if (ret & VM_FAULT_MAJOR) |
| 581 | tsk->maj_flt++; |
| 582 | else |
| 583 | tsk->min_flt++; |
| 584 | } |
| 585 | return 0; |
| 586 | } |
| 587 | |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 588 | static __always_inline long __get_user_pages_locked(struct task_struct *tsk, |
| 589 | struct mm_struct *mm, |
| 590 | unsigned long start, |
| 591 | unsigned long nr_pages, |
| 592 | int write, int force, |
| 593 | struct page **pages, |
| 594 | struct vm_area_struct **vmas, |
Andrea Arcangeli | 0fd71a5 | 2015-02-11 15:27:20 -0800 | [diff] [blame] | 595 | int *locked, bool notify_drop, |
| 596 | unsigned int flags) |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 597 | { |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 598 | long ret, pages_done; |
| 599 | bool lock_dropped; |
| 600 | |
| 601 | if (locked) { |
| 602 | /* if VM_FAULT_RETRY can be returned, vmas become invalid */ |
| 603 | BUG_ON(vmas); |
| 604 | /* check caller initialized locked */ |
| 605 | BUG_ON(*locked != 1); |
| 606 | } |
| 607 | |
| 608 | if (pages) |
| 609 | flags |= FOLL_GET; |
| 610 | if (write) |
| 611 | flags |= FOLL_WRITE; |
| 612 | if (force) |
| 613 | flags |= FOLL_FORCE; |
| 614 | |
| 615 | pages_done = 0; |
| 616 | lock_dropped = false; |
| 617 | for (;;) { |
| 618 | ret = __get_user_pages(tsk, mm, start, nr_pages, flags, pages, |
| 619 | vmas, locked); |
| 620 | if (!locked) |
| 621 | /* VM_FAULT_RETRY couldn't trigger, bypass */ |
| 622 | return ret; |
| 623 | |
| 624 | /* VM_FAULT_RETRY cannot return errors */ |
| 625 | if (!*locked) { |
| 626 | BUG_ON(ret < 0); |
| 627 | BUG_ON(ret >= nr_pages); |
| 628 | } |
| 629 | |
| 630 | if (!pages) |
| 631 | /* If it's a prefault don't insist harder */ |
| 632 | return ret; |
| 633 | |
| 634 | if (ret > 0) { |
| 635 | nr_pages -= ret; |
| 636 | pages_done += ret; |
| 637 | if (!nr_pages) |
| 638 | break; |
| 639 | } |
| 640 | if (*locked) { |
| 641 | /* VM_FAULT_RETRY didn't trigger */ |
| 642 | if (!pages_done) |
| 643 | pages_done = ret; |
| 644 | break; |
| 645 | } |
| 646 | /* VM_FAULT_RETRY triggered, so seek to the faulting offset */ |
| 647 | pages += ret; |
| 648 | start += ret << PAGE_SHIFT; |
| 649 | |
| 650 | /* |
| 651 | * Repeat on the address that fired VM_FAULT_RETRY |
| 652 | * without FAULT_FLAG_ALLOW_RETRY but with |
| 653 | * FAULT_FLAG_TRIED. |
| 654 | */ |
| 655 | *locked = 1; |
| 656 | lock_dropped = true; |
| 657 | down_read(&mm->mmap_sem); |
| 658 | ret = __get_user_pages(tsk, mm, start, 1, flags | FOLL_TRIED, |
| 659 | pages, NULL, NULL); |
| 660 | if (ret != 1) { |
| 661 | BUG_ON(ret > 1); |
| 662 | if (!pages_done) |
| 663 | pages_done = ret; |
| 664 | break; |
| 665 | } |
| 666 | nr_pages--; |
| 667 | pages_done++; |
| 668 | if (!nr_pages) |
| 669 | break; |
| 670 | pages++; |
| 671 | start += PAGE_SIZE; |
| 672 | } |
| 673 | if (notify_drop && lock_dropped && *locked) { |
| 674 | /* |
| 675 | * We must let the caller know we temporarily dropped the lock |
| 676 | * and so the critical section protected by it was lost. |
| 677 | */ |
| 678 | up_read(&mm->mmap_sem); |
| 679 | *locked = 0; |
| 680 | } |
| 681 | return pages_done; |
| 682 | } |
| 683 | |
| 684 | /* |
| 685 | * We can leverage the VM_FAULT_RETRY functionality in the page fault |
| 686 | * paths better by using either get_user_pages_locked() or |
| 687 | * get_user_pages_unlocked(). |
| 688 | * |
| 689 | * get_user_pages_locked() is suitable to replace the form: |
| 690 | * |
| 691 | * down_read(&mm->mmap_sem); |
| 692 | * do_something() |
| 693 | * get_user_pages(tsk, mm, ..., pages, NULL); |
| 694 | * up_read(&mm->mmap_sem); |
| 695 | * |
| 696 | * to: |
| 697 | * |
| 698 | * int locked = 1; |
| 699 | * down_read(&mm->mmap_sem); |
| 700 | * do_something() |
| 701 | * get_user_pages_locked(tsk, mm, ..., pages, &locked); |
| 702 | * if (locked) |
| 703 | * up_read(&mm->mmap_sem); |
| 704 | */ |
| 705 | long get_user_pages_locked(struct task_struct *tsk, struct mm_struct *mm, |
| 706 | unsigned long start, unsigned long nr_pages, |
| 707 | int write, int force, struct page **pages, |
| 708 | int *locked) |
| 709 | { |
| 710 | return __get_user_pages_locked(tsk, mm, start, nr_pages, write, force, |
Andrea Arcangeli | 0fd71a5 | 2015-02-11 15:27:20 -0800 | [diff] [blame] | 711 | pages, NULL, locked, true, FOLL_TOUCH); |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 712 | } |
| 713 | EXPORT_SYMBOL(get_user_pages_locked); |
| 714 | |
| 715 | /* |
Andrea Arcangeli | 0fd71a5 | 2015-02-11 15:27:20 -0800 | [diff] [blame] | 716 | * Same as get_user_pages_unlocked(...., FOLL_TOUCH) but it allows to |
| 717 | * pass additional gup_flags as last parameter (like FOLL_HWPOISON). |
| 718 | * |
| 719 | * NOTE: here FOLL_TOUCH is not set implicitly and must be set by the |
| 720 | * caller if required (just like with __get_user_pages). "FOLL_GET", |
| 721 | * "FOLL_WRITE" and "FOLL_FORCE" are set implicitly as needed |
| 722 | * according to the parameters "pages", "write", "force" |
| 723 | * respectively. |
| 724 | */ |
| 725 | __always_inline long __get_user_pages_unlocked(struct task_struct *tsk, struct mm_struct *mm, |
| 726 | unsigned long start, unsigned long nr_pages, |
| 727 | int write, int force, struct page **pages, |
| 728 | unsigned int gup_flags) |
| 729 | { |
| 730 | long ret; |
| 731 | int locked = 1; |
| 732 | down_read(&mm->mmap_sem); |
| 733 | ret = __get_user_pages_locked(tsk, mm, start, nr_pages, write, force, |
| 734 | pages, NULL, &locked, false, gup_flags); |
| 735 | if (locked) |
| 736 | up_read(&mm->mmap_sem); |
| 737 | return ret; |
| 738 | } |
| 739 | EXPORT_SYMBOL(__get_user_pages_unlocked); |
| 740 | |
| 741 | /* |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 742 | * get_user_pages_unlocked() is suitable to replace the form: |
| 743 | * |
| 744 | * down_read(&mm->mmap_sem); |
| 745 | * get_user_pages(tsk, mm, ..., pages, NULL); |
| 746 | * up_read(&mm->mmap_sem); |
| 747 | * |
| 748 | * with: |
| 749 | * |
| 750 | * get_user_pages_unlocked(tsk, mm, ..., pages); |
| 751 | * |
| 752 | * It is functionally equivalent to get_user_pages_fast so |
| 753 | * get_user_pages_fast should be used instead, if the two parameters |
| 754 | * "tsk" and "mm" are respectively equal to current and current->mm, |
| 755 | * or if "force" shall be set to 1 (get_user_pages_fast misses the |
| 756 | * "force" parameter). |
| 757 | */ |
| 758 | long get_user_pages_unlocked(struct task_struct *tsk, struct mm_struct *mm, |
| 759 | unsigned long start, unsigned long nr_pages, |
| 760 | int write, int force, struct page **pages) |
| 761 | { |
Andrea Arcangeli | 0fd71a5 | 2015-02-11 15:27:20 -0800 | [diff] [blame] | 762 | return __get_user_pages_unlocked(tsk, mm, start, nr_pages, write, |
| 763 | force, pages, FOLL_TOUCH); |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 764 | } |
| 765 | EXPORT_SYMBOL(get_user_pages_unlocked); |
| 766 | |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 767 | /* |
| 768 | * get_user_pages() - pin user pages in memory |
| 769 | * @tsk: the task_struct to use for page fault accounting, or |
| 770 | * NULL if faults are not to be recorded. |
| 771 | * @mm: mm_struct of target mm |
| 772 | * @start: starting user address |
| 773 | * @nr_pages: number of pages from start to pin |
| 774 | * @write: whether pages will be written to by the caller |
| 775 | * @force: whether to force access even when user mapping is currently |
| 776 | * protected (but never forces write access to shared mapping). |
| 777 | * @pages: array that receives pointers to the pages pinned. |
| 778 | * Should be at least nr_pages long. Or NULL, if caller |
| 779 | * only intends to ensure the pages are faulted in. |
| 780 | * @vmas: array of pointers to vmas corresponding to each page. |
| 781 | * Or NULL if the caller does not require them. |
| 782 | * |
| 783 | * Returns number of pages pinned. This may be fewer than the number |
| 784 | * requested. If nr_pages is 0 or negative, returns 0. If no pages |
| 785 | * were pinned, returns -errno. Each page returned must be released |
| 786 | * with a put_page() call when it is finished with. vmas will only |
| 787 | * remain valid while mmap_sem is held. |
| 788 | * |
| 789 | * Must be called with mmap_sem held for read or write. |
| 790 | * |
| 791 | * get_user_pages walks a process's page tables and takes a reference to |
| 792 | * each struct page that each user address corresponds to at a given |
| 793 | * instant. That is, it takes the page that would be accessed if a user |
| 794 | * thread accesses the given user virtual address at that instant. |
| 795 | * |
| 796 | * This does not guarantee that the page exists in the user mappings when |
| 797 | * get_user_pages returns, and there may even be a completely different |
| 798 | * page there in some cases (eg. if mmapped pagecache has been invalidated |
| 799 | * and subsequently re faulted). However it does guarantee that the page |
| 800 | * won't be freed completely. And mostly callers simply care that the page |
| 801 | * contains data that was valid *at some point in time*. Typically, an IO |
| 802 | * or similar operation cannot guarantee anything stronger anyway because |
| 803 | * locks can't be held over the syscall boundary. |
| 804 | * |
| 805 | * If write=0, the page must not be written to. If the page is written to, |
| 806 | * set_page_dirty (or set_page_dirty_lock, as appropriate) must be called |
| 807 | * after the page is finished with, and before put_page is called. |
| 808 | * |
| 809 | * get_user_pages is typically used for fewer-copy IO operations, to get a |
| 810 | * handle on the memory by some means other than accesses via the user virtual |
| 811 | * addresses. The pages may be submitted for DMA to devices or accessed via |
| 812 | * their kernel linear mapping (via the kmap APIs). Care should be taken to |
| 813 | * use the correct cache flushing APIs. |
| 814 | * |
| 815 | * See also get_user_pages_fast, for performance critical applications. |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 816 | * |
| 817 | * get_user_pages should be phased out in favor of |
| 818 | * get_user_pages_locked|unlocked or get_user_pages_fast. Nothing |
| 819 | * should use get_user_pages because it cannot pass |
| 820 | * FAULT_FLAG_ALLOW_RETRY to handle_mm_fault. |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 821 | */ |
| 822 | long get_user_pages(struct task_struct *tsk, struct mm_struct *mm, |
| 823 | unsigned long start, unsigned long nr_pages, int write, |
| 824 | int force, struct page **pages, struct vm_area_struct **vmas) |
| 825 | { |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 826 | return __get_user_pages_locked(tsk, mm, start, nr_pages, write, force, |
Andrea Arcangeli | 0fd71a5 | 2015-02-11 15:27:20 -0800 | [diff] [blame] | 827 | pages, vmas, NULL, false, FOLL_TOUCH); |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 828 | } |
| 829 | EXPORT_SYMBOL(get_user_pages); |
| 830 | |
| 831 | /** |
Kirill A. Shutemov | acc3c8d | 2015-04-14 15:44:45 -0700 | [diff] [blame] | 832 | * populate_vma_page_range() - populate a range of pages in the vma. |
| 833 | * @vma: target vma |
| 834 | * @start: start address |
| 835 | * @end: end address |
| 836 | * @nonblocking: |
| 837 | * |
| 838 | * This takes care of mlocking the pages too if VM_LOCKED is set. |
| 839 | * |
| 840 | * return 0 on success, negative error code on error. |
| 841 | * |
| 842 | * vma->vm_mm->mmap_sem must be held. |
| 843 | * |
| 844 | * If @nonblocking is NULL, it may be held for read or write and will |
| 845 | * be unperturbed. |
| 846 | * |
| 847 | * If @nonblocking is non-NULL, it must held for read only and may be |
| 848 | * released. If it's released, *@nonblocking will be set to 0. |
| 849 | */ |
| 850 | long populate_vma_page_range(struct vm_area_struct *vma, |
| 851 | unsigned long start, unsigned long end, int *nonblocking) |
| 852 | { |
| 853 | struct mm_struct *mm = vma->vm_mm; |
| 854 | unsigned long nr_pages = (end - start) / PAGE_SIZE; |
| 855 | int gup_flags; |
| 856 | |
| 857 | VM_BUG_ON(start & ~PAGE_MASK); |
| 858 | VM_BUG_ON(end & ~PAGE_MASK); |
| 859 | VM_BUG_ON_VMA(start < vma->vm_start, vma); |
| 860 | VM_BUG_ON_VMA(end > vma->vm_end, vma); |
| 861 | VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_sem), mm); |
| 862 | |
| 863 | gup_flags = FOLL_TOUCH | FOLL_POPULATE; |
| 864 | /* |
| 865 | * We want to touch writable mappings with a write fault in order |
| 866 | * to break COW, except for shared mappings because these don't COW |
| 867 | * and we would not want to dirty them for nothing. |
| 868 | */ |
| 869 | if ((vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE) |
| 870 | gup_flags |= FOLL_WRITE; |
| 871 | |
| 872 | /* |
| 873 | * We want mlock to succeed for regions that have any permissions |
| 874 | * other than PROT_NONE. |
| 875 | */ |
| 876 | if (vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)) |
| 877 | gup_flags |= FOLL_FORCE; |
| 878 | |
| 879 | /* |
| 880 | * We made sure addr is within a VMA, so the following will |
| 881 | * not result in a stack expansion that recurses back here. |
| 882 | */ |
| 883 | return __get_user_pages(current, mm, start, nr_pages, gup_flags, |
| 884 | NULL, NULL, nonblocking); |
| 885 | } |
| 886 | |
| 887 | /* |
| 888 | * __mm_populate - populate and/or mlock pages within a range of address space. |
| 889 | * |
| 890 | * This is used to implement mlock() and the MAP_POPULATE / MAP_LOCKED mmap |
| 891 | * flags. VMAs must be already marked with the desired vm_flags, and |
| 892 | * mmap_sem must not be held. |
| 893 | */ |
| 894 | int __mm_populate(unsigned long start, unsigned long len, int ignore_errors) |
| 895 | { |
| 896 | struct mm_struct *mm = current->mm; |
| 897 | unsigned long end, nstart, nend; |
| 898 | struct vm_area_struct *vma = NULL; |
| 899 | int locked = 0; |
| 900 | long ret = 0; |
| 901 | |
| 902 | VM_BUG_ON(start & ~PAGE_MASK); |
| 903 | VM_BUG_ON(len != PAGE_ALIGN(len)); |
| 904 | end = start + len; |
| 905 | |
| 906 | for (nstart = start; nstart < end; nstart = nend) { |
| 907 | /* |
| 908 | * We want to fault in pages for [nstart; end) address range. |
| 909 | * Find first corresponding VMA. |
| 910 | */ |
| 911 | if (!locked) { |
| 912 | locked = 1; |
| 913 | down_read(&mm->mmap_sem); |
| 914 | vma = find_vma(mm, nstart); |
| 915 | } else if (nstart >= vma->vm_end) |
| 916 | vma = vma->vm_next; |
| 917 | if (!vma || vma->vm_start >= end) |
| 918 | break; |
| 919 | /* |
| 920 | * Set [nstart; nend) to intersection of desired address |
| 921 | * range with the first VMA. Also, skip undesirable VMA types. |
| 922 | */ |
| 923 | nend = min(end, vma->vm_end); |
| 924 | if (vma->vm_flags & (VM_IO | VM_PFNMAP)) |
| 925 | continue; |
| 926 | if (nstart < vma->vm_start) |
| 927 | nstart = vma->vm_start; |
| 928 | /* |
| 929 | * Now fault in a range of pages. populate_vma_page_range() |
| 930 | * double checks the vma flags, so that it won't mlock pages |
| 931 | * if the vma was already munlocked. |
| 932 | */ |
| 933 | ret = populate_vma_page_range(vma, nstart, nend, &locked); |
| 934 | if (ret < 0) { |
| 935 | if (ignore_errors) { |
| 936 | ret = 0; |
| 937 | continue; /* continue at next VMA */ |
| 938 | } |
| 939 | break; |
| 940 | } |
| 941 | nend = nstart + ret * PAGE_SIZE; |
| 942 | ret = 0; |
| 943 | } |
| 944 | if (locked) |
| 945 | up_read(&mm->mmap_sem); |
| 946 | return ret; /* 0 or negative error code */ |
| 947 | } |
| 948 | |
| 949 | /** |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 950 | * get_dump_page() - pin user page in memory while writing it to core dump |
| 951 | * @addr: user address |
| 952 | * |
| 953 | * Returns struct page pointer of user page pinned for dump, |
| 954 | * to be freed afterwards by page_cache_release() or put_page(). |
| 955 | * |
| 956 | * Returns NULL on any kind of failure - a hole must then be inserted into |
| 957 | * the corefile, to preserve alignment with its headers; and also returns |
| 958 | * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found - |
| 959 | * allowing a hole to be left in the corefile to save diskspace. |
| 960 | * |
| 961 | * Called without mmap_sem, but after all other threads have been killed. |
| 962 | */ |
| 963 | #ifdef CONFIG_ELF_CORE |
| 964 | struct page *get_dump_page(unsigned long addr) |
| 965 | { |
| 966 | struct vm_area_struct *vma; |
| 967 | struct page *page; |
| 968 | |
| 969 | if (__get_user_pages(current, current->mm, addr, 1, |
| 970 | FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma, |
| 971 | NULL) < 1) |
| 972 | return NULL; |
| 973 | flush_cache_page(vma, addr, page_to_pfn(page)); |
| 974 | return page; |
| 975 | } |
| 976 | #endif /* CONFIG_ELF_CORE */ |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 977 | |
| 978 | /* |
| 979 | * Generic RCU Fast GUP |
| 980 | * |
| 981 | * get_user_pages_fast attempts to pin user pages by walking the page |
| 982 | * tables directly and avoids taking locks. Thus the walker needs to be |
| 983 | * protected from page table pages being freed from under it, and should |
| 984 | * block any THP splits. |
| 985 | * |
| 986 | * One way to achieve this is to have the walker disable interrupts, and |
| 987 | * rely on IPIs from the TLB flushing code blocking before the page table |
| 988 | * pages are freed. This is unsuitable for architectures that do not need |
| 989 | * to broadcast an IPI when invalidating TLBs. |
| 990 | * |
| 991 | * Another way to achieve this is to batch up page table containing pages |
| 992 | * belonging to more than one mm_user, then rcu_sched a callback to free those |
| 993 | * pages. Disabling interrupts will allow the fast_gup walker to both block |
| 994 | * the rcu_sched callback, and an IPI that we broadcast for splitting THPs |
| 995 | * (which is a relatively rare event). The code below adopts this strategy. |
| 996 | * |
| 997 | * Before activating this code, please be aware that the following assumptions |
| 998 | * are currently made: |
| 999 | * |
| 1000 | * *) HAVE_RCU_TABLE_FREE is enabled, and tlb_remove_table is used to free |
| 1001 | * pages containing page tables. |
| 1002 | * |
| 1003 | * *) THP splits will broadcast an IPI, this can be achieved by overriding |
| 1004 | * pmdp_splitting_flush. |
| 1005 | * |
| 1006 | * *) ptes can be read atomically by the architecture. |
| 1007 | * |
| 1008 | * *) access_ok is sufficient to validate userspace address ranges. |
| 1009 | * |
| 1010 | * The last two assumptions can be relaxed by the addition of helper functions. |
| 1011 | * |
| 1012 | * This code is based heavily on the PowerPC implementation by Nick Piggin. |
| 1013 | */ |
| 1014 | #ifdef CONFIG_HAVE_GENERIC_RCU_GUP |
| 1015 | |
| 1016 | #ifdef __HAVE_ARCH_PTE_SPECIAL |
| 1017 | static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end, |
| 1018 | int write, struct page **pages, int *nr) |
| 1019 | { |
| 1020 | pte_t *ptep, *ptem; |
| 1021 | int ret = 0; |
| 1022 | |
| 1023 | ptem = ptep = pte_offset_map(&pmd, addr); |
| 1024 | do { |
| 1025 | /* |
| 1026 | * In the line below we are assuming that the pte can be read |
| 1027 | * atomically. If this is not the case for your architecture, |
| 1028 | * please wrap this in a helper function! |
| 1029 | * |
| 1030 | * for an example see gup_get_pte in arch/x86/mm/gup.c |
| 1031 | */ |
Jason Low | 9d8c47e | 2015-04-15 16:14:05 -0700 | [diff] [blame] | 1032 | pte_t pte = READ_ONCE(*ptep); |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1033 | struct page *page; |
| 1034 | |
| 1035 | /* |
| 1036 | * Similar to the PMD case below, NUMA hinting must take slow |
Mel Gorman | 8a0516e | 2015-02-12 14:58:22 -0800 | [diff] [blame] | 1037 | * path using the pte_protnone check. |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1038 | */ |
| 1039 | if (!pte_present(pte) || pte_special(pte) || |
Mel Gorman | 8a0516e | 2015-02-12 14:58:22 -0800 | [diff] [blame] | 1040 | pte_protnone(pte) || (write && !pte_write(pte))) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1041 | goto pte_unmap; |
| 1042 | |
| 1043 | VM_BUG_ON(!pfn_valid(pte_pfn(pte))); |
| 1044 | page = pte_page(pte); |
| 1045 | |
| 1046 | if (!page_cache_get_speculative(page)) |
| 1047 | goto pte_unmap; |
| 1048 | |
| 1049 | if (unlikely(pte_val(pte) != pte_val(*ptep))) { |
| 1050 | put_page(page); |
| 1051 | goto pte_unmap; |
| 1052 | } |
| 1053 | |
| 1054 | pages[*nr] = page; |
| 1055 | (*nr)++; |
| 1056 | |
| 1057 | } while (ptep++, addr += PAGE_SIZE, addr != end); |
| 1058 | |
| 1059 | ret = 1; |
| 1060 | |
| 1061 | pte_unmap: |
| 1062 | pte_unmap(ptem); |
| 1063 | return ret; |
| 1064 | } |
| 1065 | #else |
| 1066 | |
| 1067 | /* |
| 1068 | * If we can't determine whether or not a pte is special, then fail immediately |
| 1069 | * for ptes. Note, we can still pin HugeTLB and THP as these are guaranteed not |
| 1070 | * to be special. |
| 1071 | * |
| 1072 | * For a futex to be placed on a THP tail page, get_futex_key requires a |
| 1073 | * __get_user_pages_fast implementation that can pin pages. Thus it's still |
| 1074 | * useful to have gup_huge_pmd even if we can't operate on ptes. |
| 1075 | */ |
| 1076 | static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end, |
| 1077 | int write, struct page **pages, int *nr) |
| 1078 | { |
| 1079 | return 0; |
| 1080 | } |
| 1081 | #endif /* __HAVE_ARCH_PTE_SPECIAL */ |
| 1082 | |
| 1083 | static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr, |
| 1084 | unsigned long end, int write, struct page **pages, int *nr) |
| 1085 | { |
| 1086 | struct page *head, *page, *tail; |
| 1087 | int refs; |
| 1088 | |
| 1089 | if (write && !pmd_write(orig)) |
| 1090 | return 0; |
| 1091 | |
| 1092 | refs = 0; |
| 1093 | head = pmd_page(orig); |
| 1094 | page = head + ((addr & ~PMD_MASK) >> PAGE_SHIFT); |
| 1095 | tail = page; |
| 1096 | do { |
| 1097 | VM_BUG_ON_PAGE(compound_head(page) != head, page); |
| 1098 | pages[*nr] = page; |
| 1099 | (*nr)++; |
| 1100 | page++; |
| 1101 | refs++; |
| 1102 | } while (addr += PAGE_SIZE, addr != end); |
| 1103 | |
| 1104 | if (!page_cache_add_speculative(head, refs)) { |
| 1105 | *nr -= refs; |
| 1106 | return 0; |
| 1107 | } |
| 1108 | |
| 1109 | if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) { |
| 1110 | *nr -= refs; |
| 1111 | while (refs--) |
| 1112 | put_page(head); |
| 1113 | return 0; |
| 1114 | } |
| 1115 | |
| 1116 | /* |
| 1117 | * Any tail pages need their mapcount reference taken before we |
| 1118 | * return. (This allows the THP code to bump their ref count when |
| 1119 | * they are split into base pages). |
| 1120 | */ |
| 1121 | while (refs--) { |
| 1122 | if (PageTail(tail)) |
| 1123 | get_huge_page_tail(tail); |
| 1124 | tail++; |
| 1125 | } |
| 1126 | |
| 1127 | return 1; |
| 1128 | } |
| 1129 | |
| 1130 | static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr, |
| 1131 | unsigned long end, int write, struct page **pages, int *nr) |
| 1132 | { |
| 1133 | struct page *head, *page, *tail; |
| 1134 | int refs; |
| 1135 | |
| 1136 | if (write && !pud_write(orig)) |
| 1137 | return 0; |
| 1138 | |
| 1139 | refs = 0; |
| 1140 | head = pud_page(orig); |
| 1141 | page = head + ((addr & ~PUD_MASK) >> PAGE_SHIFT); |
| 1142 | tail = page; |
| 1143 | do { |
| 1144 | VM_BUG_ON_PAGE(compound_head(page) != head, page); |
| 1145 | pages[*nr] = page; |
| 1146 | (*nr)++; |
| 1147 | page++; |
| 1148 | refs++; |
| 1149 | } while (addr += PAGE_SIZE, addr != end); |
| 1150 | |
| 1151 | if (!page_cache_add_speculative(head, refs)) { |
| 1152 | *nr -= refs; |
| 1153 | return 0; |
| 1154 | } |
| 1155 | |
| 1156 | if (unlikely(pud_val(orig) != pud_val(*pudp))) { |
| 1157 | *nr -= refs; |
| 1158 | while (refs--) |
| 1159 | put_page(head); |
| 1160 | return 0; |
| 1161 | } |
| 1162 | |
| 1163 | while (refs--) { |
| 1164 | if (PageTail(tail)) |
| 1165 | get_huge_page_tail(tail); |
| 1166 | tail++; |
| 1167 | } |
| 1168 | |
| 1169 | return 1; |
| 1170 | } |
| 1171 | |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 1172 | static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsigned long addr, |
| 1173 | unsigned long end, int write, |
| 1174 | struct page **pages, int *nr) |
| 1175 | { |
| 1176 | int refs; |
| 1177 | struct page *head, *page, *tail; |
| 1178 | |
| 1179 | if (write && !pgd_write(orig)) |
| 1180 | return 0; |
| 1181 | |
| 1182 | refs = 0; |
| 1183 | head = pgd_page(orig); |
| 1184 | page = head + ((addr & ~PGDIR_MASK) >> PAGE_SHIFT); |
| 1185 | tail = page; |
| 1186 | do { |
| 1187 | VM_BUG_ON_PAGE(compound_head(page) != head, page); |
| 1188 | pages[*nr] = page; |
| 1189 | (*nr)++; |
| 1190 | page++; |
| 1191 | refs++; |
| 1192 | } while (addr += PAGE_SIZE, addr != end); |
| 1193 | |
| 1194 | if (!page_cache_add_speculative(head, refs)) { |
| 1195 | *nr -= refs; |
| 1196 | return 0; |
| 1197 | } |
| 1198 | |
| 1199 | if (unlikely(pgd_val(orig) != pgd_val(*pgdp))) { |
| 1200 | *nr -= refs; |
| 1201 | while (refs--) |
| 1202 | put_page(head); |
| 1203 | return 0; |
| 1204 | } |
| 1205 | |
| 1206 | while (refs--) { |
| 1207 | if (PageTail(tail)) |
| 1208 | get_huge_page_tail(tail); |
| 1209 | tail++; |
| 1210 | } |
| 1211 | |
| 1212 | return 1; |
| 1213 | } |
| 1214 | |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1215 | static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end, |
| 1216 | int write, struct page **pages, int *nr) |
| 1217 | { |
| 1218 | unsigned long next; |
| 1219 | pmd_t *pmdp; |
| 1220 | |
| 1221 | pmdp = pmd_offset(&pud, addr); |
| 1222 | do { |
Christian Borntraeger | 38c5ce9 | 2015-01-06 22:54:46 +0100 | [diff] [blame] | 1223 | pmd_t pmd = READ_ONCE(*pmdp); |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1224 | |
| 1225 | next = pmd_addr_end(addr, end); |
| 1226 | if (pmd_none(pmd) || pmd_trans_splitting(pmd)) |
| 1227 | return 0; |
| 1228 | |
| 1229 | if (unlikely(pmd_trans_huge(pmd) || pmd_huge(pmd))) { |
| 1230 | /* |
| 1231 | * NUMA hinting faults need to be handled in the GUP |
| 1232 | * slowpath for accounting purposes and so that they |
| 1233 | * can be serialised against THP migration. |
| 1234 | */ |
Mel Gorman | 8a0516e | 2015-02-12 14:58:22 -0800 | [diff] [blame] | 1235 | if (pmd_protnone(pmd)) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1236 | return 0; |
| 1237 | |
| 1238 | if (!gup_huge_pmd(pmd, pmdp, addr, next, write, |
| 1239 | pages, nr)) |
| 1240 | return 0; |
| 1241 | |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 1242 | } else if (unlikely(is_hugepd(__hugepd(pmd_val(pmd))))) { |
| 1243 | /* |
| 1244 | * architecture have different format for hugetlbfs |
| 1245 | * pmd format and THP pmd format |
| 1246 | */ |
| 1247 | if (!gup_huge_pd(__hugepd(pmd_val(pmd)), addr, |
| 1248 | PMD_SHIFT, next, write, pages, nr)) |
| 1249 | return 0; |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1250 | } else if (!gup_pte_range(pmd, addr, next, write, pages, nr)) |
| 1251 | return 0; |
| 1252 | } while (pmdp++, addr = next, addr != end); |
| 1253 | |
| 1254 | return 1; |
| 1255 | } |
| 1256 | |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 1257 | static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end, |
| 1258 | int write, struct page **pages, int *nr) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1259 | { |
| 1260 | unsigned long next; |
| 1261 | pud_t *pudp; |
| 1262 | |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 1263 | pudp = pud_offset(&pgd, addr); |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1264 | do { |
Christian Borntraeger | e37c698 | 2014-12-07 21:41:33 +0100 | [diff] [blame] | 1265 | pud_t pud = READ_ONCE(*pudp); |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1266 | |
| 1267 | next = pud_addr_end(addr, end); |
| 1268 | if (pud_none(pud)) |
| 1269 | return 0; |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 1270 | if (unlikely(pud_huge(pud))) { |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1271 | if (!gup_huge_pud(pud, pudp, addr, next, write, |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 1272 | pages, nr)) |
| 1273 | return 0; |
| 1274 | } else if (unlikely(is_hugepd(__hugepd(pud_val(pud))))) { |
| 1275 | if (!gup_huge_pd(__hugepd(pud_val(pud)), addr, |
| 1276 | PUD_SHIFT, next, write, pages, nr)) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1277 | return 0; |
| 1278 | } else if (!gup_pmd_range(pud, addr, next, write, pages, nr)) |
| 1279 | return 0; |
| 1280 | } while (pudp++, addr = next, addr != end); |
| 1281 | |
| 1282 | return 1; |
| 1283 | } |
| 1284 | |
| 1285 | /* |
| 1286 | * Like get_user_pages_fast() except it's IRQ-safe in that it won't fall back to |
| 1287 | * the regular GUP. It will only return non-negative values. |
| 1288 | */ |
| 1289 | int __get_user_pages_fast(unsigned long start, int nr_pages, int write, |
| 1290 | struct page **pages) |
| 1291 | { |
| 1292 | struct mm_struct *mm = current->mm; |
| 1293 | unsigned long addr, len, end; |
| 1294 | unsigned long next, flags; |
| 1295 | pgd_t *pgdp; |
| 1296 | int nr = 0; |
| 1297 | |
| 1298 | start &= PAGE_MASK; |
| 1299 | addr = start; |
| 1300 | len = (unsigned long) nr_pages << PAGE_SHIFT; |
| 1301 | end = start + len; |
| 1302 | |
| 1303 | if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ, |
| 1304 | start, len))) |
| 1305 | return 0; |
| 1306 | |
| 1307 | /* |
| 1308 | * Disable interrupts. We use the nested form as we can already have |
| 1309 | * interrupts disabled by get_futex_key. |
| 1310 | * |
| 1311 | * With interrupts disabled, we block page table pages from being |
| 1312 | * freed from under us. See mmu_gather_tlb in asm-generic/tlb.h |
| 1313 | * for more details. |
| 1314 | * |
| 1315 | * We do not adopt an rcu_read_lock(.) here as we also want to |
| 1316 | * block IPIs that come from THPs splitting. |
| 1317 | */ |
| 1318 | |
| 1319 | local_irq_save(flags); |
| 1320 | pgdp = pgd_offset(mm, addr); |
| 1321 | do { |
Jason Low | 9d8c47e | 2015-04-15 16:14:05 -0700 | [diff] [blame] | 1322 | pgd_t pgd = READ_ONCE(*pgdp); |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 1323 | |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1324 | next = pgd_addr_end(addr, end); |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 1325 | if (pgd_none(pgd)) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1326 | break; |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 1327 | if (unlikely(pgd_huge(pgd))) { |
| 1328 | if (!gup_huge_pgd(pgd, pgdp, addr, next, write, |
| 1329 | pages, &nr)) |
| 1330 | break; |
| 1331 | } else if (unlikely(is_hugepd(__hugepd(pgd_val(pgd))))) { |
| 1332 | if (!gup_huge_pd(__hugepd(pgd_val(pgd)), addr, |
| 1333 | PGDIR_SHIFT, next, write, pages, &nr)) |
| 1334 | break; |
| 1335 | } else if (!gup_pud_range(pgd, addr, next, write, pages, &nr)) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1336 | break; |
| 1337 | } while (pgdp++, addr = next, addr != end); |
| 1338 | local_irq_restore(flags); |
| 1339 | |
| 1340 | return nr; |
| 1341 | } |
| 1342 | |
| 1343 | /** |
| 1344 | * get_user_pages_fast() - pin user pages in memory |
| 1345 | * @start: starting user address |
| 1346 | * @nr_pages: number of pages from start to pin |
| 1347 | * @write: whether pages will be written to |
| 1348 | * @pages: array that receives pointers to the pages pinned. |
| 1349 | * Should be at least nr_pages long. |
| 1350 | * |
| 1351 | * Attempt to pin user pages in memory without taking mm->mmap_sem. |
| 1352 | * If not successful, it will fall back to taking the lock and |
| 1353 | * calling get_user_pages(). |
| 1354 | * |
| 1355 | * Returns number of pages pinned. This may be fewer than the number |
| 1356 | * requested. If nr_pages is 0 or negative, returns 0. If no pages |
| 1357 | * were pinned, returns -errno. |
| 1358 | */ |
| 1359 | int get_user_pages_fast(unsigned long start, int nr_pages, int write, |
| 1360 | struct page **pages) |
| 1361 | { |
| 1362 | struct mm_struct *mm = current->mm; |
| 1363 | int nr, ret; |
| 1364 | |
| 1365 | start &= PAGE_MASK; |
| 1366 | nr = __get_user_pages_fast(start, nr_pages, write, pages); |
| 1367 | ret = nr; |
| 1368 | |
| 1369 | if (nr < nr_pages) { |
| 1370 | /* Try to get the remaining pages with get_user_pages */ |
| 1371 | start += nr << PAGE_SHIFT; |
| 1372 | pages += nr; |
| 1373 | |
Andrea Arcangeli | a7b7807 | 2015-02-11 15:27:23 -0800 | [diff] [blame] | 1374 | ret = get_user_pages_unlocked(current, mm, start, |
| 1375 | nr_pages - nr, write, 0, pages); |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1376 | |
| 1377 | /* Have to be a bit careful with return values */ |
| 1378 | if (nr > 0) { |
| 1379 | if (ret < 0) |
| 1380 | ret = nr; |
| 1381 | else |
| 1382 | ret += nr; |
| 1383 | } |
| 1384 | } |
| 1385 | |
| 1386 | return ret; |
| 1387 | } |
| 1388 | |
| 1389 | #endif /* CONFIG_HAVE_GENERIC_RCU_GUP */ |