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> |
Dan Williams | 3565fce | 2016-01-15 16:56:55 -0800 | [diff] [blame] | 7 | #include <linux/memremap.h> |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 8 | #include <linux/pagemap.h> |
| 9 | #include <linux/rmap.h> |
| 10 | #include <linux/swap.h> |
| 11 | #include <linux/swapops.h> |
| 12 | |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 13 | #include <linux/sched/signal.h> |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 14 | #include <linux/rwsem.h> |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 15 | #include <linux/hugetlb.h> |
Aneesh Kumar K.V | 9a4e9f3 | 2019-03-05 15:47:44 -0800 | [diff] [blame] | 16 | #include <linux/migrate.h> |
| 17 | #include <linux/mm_inline.h> |
| 18 | #include <linux/sched/mm.h> |
Kirill A. Shutemov | 1027e44 | 2015-09-04 15:47:55 -0700 | [diff] [blame] | 19 | |
Dave Hansen | 33a709b | 2016-02-12 13:02:19 -0800 | [diff] [blame] | 20 | #include <asm/mmu_context.h> |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 21 | #include <asm/pgtable.h> |
Kirill A. Shutemov | 1027e44 | 2015-09-04 15:47:55 -0700 | [diff] [blame] | 22 | #include <asm/tlbflush.h> |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 23 | |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 24 | #include "internal.h" |
| 25 | |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 26 | struct follow_page_context { |
| 27 | struct dev_pagemap *pgmap; |
| 28 | unsigned int page_mask; |
| 29 | }; |
| 30 | |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 31 | static struct page *no_page_table(struct vm_area_struct *vma, |
| 32 | unsigned int flags) |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 33 | { |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 34 | /* |
| 35 | * When core dumping an enormous anonymous area that nobody |
| 36 | * has touched so far, we don't want to allocate unnecessary pages or |
| 37 | * page tables. Return error instead of NULL to skip handle_mm_fault, |
| 38 | * then get_dump_page() will return NULL to leave a hole in the dump. |
| 39 | * But we can only make this optimization where a hole would surely |
| 40 | * be zero-filled if handle_mm_fault() actually did handle it. |
| 41 | */ |
| 42 | if ((flags & FOLL_DUMP) && (!vma->vm_ops || !vma->vm_ops->fault)) |
| 43 | return ERR_PTR(-EFAULT); |
| 44 | return NULL; |
| 45 | } |
| 46 | |
Kirill A. Shutemov | 1027e44 | 2015-09-04 15:47:55 -0700 | [diff] [blame] | 47 | static int follow_pfn_pte(struct vm_area_struct *vma, unsigned long address, |
| 48 | pte_t *pte, unsigned int flags) |
| 49 | { |
| 50 | /* No page to get reference */ |
| 51 | if (flags & FOLL_GET) |
| 52 | return -EFAULT; |
| 53 | |
| 54 | if (flags & FOLL_TOUCH) { |
| 55 | pte_t entry = *pte; |
| 56 | |
| 57 | if (flags & FOLL_WRITE) |
| 58 | entry = pte_mkdirty(entry); |
| 59 | entry = pte_mkyoung(entry); |
| 60 | |
| 61 | if (!pte_same(*pte, entry)) { |
| 62 | set_pte_at(vma->vm_mm, address, pte, entry); |
| 63 | update_mmu_cache(vma, address, pte); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /* Proper page table entry exists, but no corresponding struct page */ |
| 68 | return -EEXIST; |
| 69 | } |
| 70 | |
Linus Torvalds | 19be0ea | 2016-10-13 13:07:36 -0700 | [diff] [blame] | 71 | /* |
| 72 | * FOLL_FORCE can write to even unwritable pte's, but only |
| 73 | * after we've gone through a COW cycle and they are dirty. |
| 74 | */ |
| 75 | static inline bool can_follow_write_pte(pte_t pte, unsigned int flags) |
| 76 | { |
Linus Torvalds | f6f3732 | 2017-12-15 18:53:22 -0800 | [diff] [blame] | 77 | return pte_write(pte) || |
Linus Torvalds | 19be0ea | 2016-10-13 13:07:36 -0700 | [diff] [blame] | 78 | ((flags & FOLL_FORCE) && (flags & FOLL_COW) && pte_dirty(pte)); |
| 79 | } |
| 80 | |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 81 | static struct page *follow_page_pte(struct vm_area_struct *vma, |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 82 | unsigned long address, pmd_t *pmd, unsigned int flags, |
| 83 | struct dev_pagemap **pgmap) |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 84 | { |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 85 | struct mm_struct *mm = vma->vm_mm; |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 86 | struct page *page; |
| 87 | spinlock_t *ptl; |
| 88 | pte_t *ptep, pte; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 89 | |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 90 | retry: |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 91 | if (unlikely(pmd_bad(*pmd))) |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 92 | return no_page_table(vma, flags); |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 93 | |
| 94 | ptep = pte_offset_map_lock(mm, pmd, address, &ptl); |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 95 | pte = *ptep; |
| 96 | if (!pte_present(pte)) { |
| 97 | swp_entry_t entry; |
| 98 | /* |
| 99 | * KSM's break_ksm() relies upon recognizing a ksm page |
| 100 | * even while it is being migrated, so for that case we |
| 101 | * need migration_entry_wait(). |
| 102 | */ |
| 103 | if (likely(!(flags & FOLL_MIGRATION))) |
| 104 | goto no_page; |
Kirill A. Shutemov | 0661a33 | 2015-02-10 14:10:04 -0800 | [diff] [blame] | 105 | if (pte_none(pte)) |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 106 | goto no_page; |
| 107 | entry = pte_to_swp_entry(pte); |
| 108 | if (!is_migration_entry(entry)) |
| 109 | goto no_page; |
| 110 | pte_unmap_unlock(ptep, ptl); |
| 111 | migration_entry_wait(mm, pmd, address); |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 112 | goto retry; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 113 | } |
Mel Gorman | 8a0516e | 2015-02-12 14:58:22 -0800 | [diff] [blame] | 114 | if ((flags & FOLL_NUMA) && pte_protnone(pte)) |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 115 | goto no_page; |
Linus Torvalds | 19be0ea | 2016-10-13 13:07:36 -0700 | [diff] [blame] | 116 | if ((flags & FOLL_WRITE) && !can_follow_write_pte(pte, flags)) { |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 117 | pte_unmap_unlock(ptep, ptl); |
| 118 | return NULL; |
| 119 | } |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 120 | |
| 121 | page = vm_normal_page(vma, address, pte); |
Dan Williams | 3565fce | 2016-01-15 16:56:55 -0800 | [diff] [blame] | 122 | if (!page && pte_devmap(pte) && (flags & FOLL_GET)) { |
| 123 | /* |
| 124 | * Only return device mapping pages in the FOLL_GET case since |
| 125 | * they are only valid while holding the pgmap reference. |
| 126 | */ |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 127 | *pgmap = get_dev_pagemap(pte_pfn(pte), *pgmap); |
| 128 | if (*pgmap) |
Dan Williams | 3565fce | 2016-01-15 16:56:55 -0800 | [diff] [blame] | 129 | page = pte_page(pte); |
| 130 | else |
| 131 | goto no_page; |
| 132 | } else if (unlikely(!page)) { |
Kirill A. Shutemov | 1027e44 | 2015-09-04 15:47:55 -0700 | [diff] [blame] | 133 | if (flags & FOLL_DUMP) { |
| 134 | /* Avoid special (like zero) pages in core dumps */ |
| 135 | page = ERR_PTR(-EFAULT); |
| 136 | goto out; |
| 137 | } |
| 138 | |
| 139 | if (is_zero_pfn(pte_pfn(pte))) { |
| 140 | page = pte_page(pte); |
| 141 | } else { |
| 142 | int ret; |
| 143 | |
| 144 | ret = follow_pfn_pte(vma, address, ptep, flags); |
| 145 | page = ERR_PTR(ret); |
| 146 | goto out; |
| 147 | } |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Kirill A. Shutemov | 6742d29 | 2016-01-15 16:52:28 -0800 | [diff] [blame] | 150 | if (flags & FOLL_SPLIT && PageTransCompound(page)) { |
| 151 | int ret; |
| 152 | get_page(page); |
| 153 | pte_unmap_unlock(ptep, ptl); |
| 154 | lock_page(page); |
| 155 | ret = split_huge_page(page); |
| 156 | unlock_page(page); |
| 157 | put_page(page); |
| 158 | if (ret) |
| 159 | return ERR_PTR(ret); |
| 160 | goto retry; |
| 161 | } |
| 162 | |
Linus Torvalds | 8fde12c | 2019-04-11 10:49:19 -0700 | [diff] [blame] | 163 | if (flags & FOLL_GET) { |
| 164 | if (unlikely(!try_get_page(page))) { |
| 165 | page = ERR_PTR(-ENOMEM); |
| 166 | goto out; |
| 167 | } |
| 168 | } |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 169 | if (flags & FOLL_TOUCH) { |
| 170 | if ((flags & FOLL_WRITE) && |
| 171 | !pte_dirty(pte) && !PageDirty(page)) |
| 172 | set_page_dirty(page); |
| 173 | /* |
| 174 | * pte_mkyoung() would be more correct here, but atomic care |
| 175 | * is needed to avoid losing the dirty bit: it is easier to use |
| 176 | * mark_page_accessed(). |
| 177 | */ |
| 178 | mark_page_accessed(page); |
| 179 | } |
Eric B Munson | de60f5f | 2015-11-05 18:51:36 -0800 | [diff] [blame] | 180 | if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) { |
Kirill A. Shutemov | e90309c | 2016-01-15 16:54:33 -0800 | [diff] [blame] | 181 | /* Do not mlock pte-mapped THP */ |
| 182 | if (PageTransCompound(page)) |
| 183 | goto out; |
| 184 | |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 185 | /* |
| 186 | * The preliminary mapping check is mainly to avoid the |
| 187 | * pointless overhead of lock_page on the ZERO_PAGE |
| 188 | * which might bounce very badly if there is contention. |
| 189 | * |
| 190 | * If the page is already locked, we don't need to |
| 191 | * handle it now - vmscan will handle it later if and |
| 192 | * when it attempts to reclaim the page. |
| 193 | */ |
| 194 | if (page->mapping && trylock_page(page)) { |
| 195 | lru_add_drain(); /* push cached pages to LRU */ |
| 196 | /* |
| 197 | * Because we lock page here, and migration is |
| 198 | * blocked by the pte's page reference, and we |
| 199 | * know the page is still mapped, we don't even |
| 200 | * need to check for file-cache page truncation. |
| 201 | */ |
| 202 | mlock_vma_page(page); |
| 203 | unlock_page(page); |
| 204 | } |
| 205 | } |
Kirill A. Shutemov | 1027e44 | 2015-09-04 15:47:55 -0700 | [diff] [blame] | 206 | out: |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 207 | pte_unmap_unlock(ptep, ptl); |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 208 | return page; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 209 | no_page: |
| 210 | pte_unmap_unlock(ptep, ptl); |
| 211 | if (!pte_none(pte)) |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 212 | return NULL; |
| 213 | return no_page_table(vma, flags); |
| 214 | } |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 215 | |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 216 | static struct page *follow_pmd_mask(struct vm_area_struct *vma, |
| 217 | unsigned long address, pud_t *pudp, |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 218 | unsigned int flags, |
| 219 | struct follow_page_context *ctx) |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 220 | { |
Huang Ying | 6882728 | 2018-06-07 17:06:34 -0700 | [diff] [blame] | 221 | pmd_t *pmd, pmdval; |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 222 | spinlock_t *ptl; |
| 223 | struct page *page; |
| 224 | struct mm_struct *mm = vma->vm_mm; |
| 225 | |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 226 | pmd = pmd_offset(pudp, address); |
Huang Ying | 6882728 | 2018-06-07 17:06:34 -0700 | [diff] [blame] | 227 | /* |
| 228 | * The READ_ONCE() will stabilize the pmdval in a register or |
| 229 | * on the stack so that it will stop changing under the code. |
| 230 | */ |
| 231 | pmdval = READ_ONCE(*pmd); |
| 232 | if (pmd_none(pmdval)) |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 233 | return no_page_table(vma, flags); |
Huang Ying | 6882728 | 2018-06-07 17:06:34 -0700 | [diff] [blame] | 234 | if (pmd_huge(pmdval) && vma->vm_flags & VM_HUGETLB) { |
Naoya Horiguchi | e66f17f | 2015-02-11 15:25:22 -0800 | [diff] [blame] | 235 | page = follow_huge_pmd(mm, address, pmd, flags); |
| 236 | if (page) |
| 237 | return page; |
| 238 | return no_page_table(vma, flags); |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 239 | } |
Huang Ying | 6882728 | 2018-06-07 17:06:34 -0700 | [diff] [blame] | 240 | if (is_hugepd(__hugepd(pmd_val(pmdval)))) { |
Aneesh Kumar K.V | 4dc7145 | 2017-07-06 15:38:56 -0700 | [diff] [blame] | 241 | page = follow_huge_pd(vma, address, |
Huang Ying | 6882728 | 2018-06-07 17:06:34 -0700 | [diff] [blame] | 242 | __hugepd(pmd_val(pmdval)), flags, |
Aneesh Kumar K.V | 4dc7145 | 2017-07-06 15:38:56 -0700 | [diff] [blame] | 243 | PMD_SHIFT); |
| 244 | if (page) |
| 245 | return page; |
| 246 | return no_page_table(vma, flags); |
| 247 | } |
Zi Yan | 84c3fc4 | 2017-09-08 16:11:01 -0700 | [diff] [blame] | 248 | retry: |
Huang Ying | 6882728 | 2018-06-07 17:06:34 -0700 | [diff] [blame] | 249 | if (!pmd_present(pmdval)) { |
Zi Yan | 84c3fc4 | 2017-09-08 16:11:01 -0700 | [diff] [blame] | 250 | if (likely(!(flags & FOLL_MIGRATION))) |
| 251 | return no_page_table(vma, flags); |
| 252 | VM_BUG_ON(thp_migration_supported() && |
Huang Ying | 6882728 | 2018-06-07 17:06:34 -0700 | [diff] [blame] | 253 | !is_pmd_migration_entry(pmdval)); |
| 254 | if (is_pmd_migration_entry(pmdval)) |
Zi Yan | 84c3fc4 | 2017-09-08 16:11:01 -0700 | [diff] [blame] | 255 | pmd_migration_entry_wait(mm, pmd); |
Huang Ying | 6882728 | 2018-06-07 17:06:34 -0700 | [diff] [blame] | 256 | pmdval = READ_ONCE(*pmd); |
| 257 | /* |
| 258 | * MADV_DONTNEED may convert the pmd to null because |
| 259 | * mmap_sem is held in read mode |
| 260 | */ |
| 261 | if (pmd_none(pmdval)) |
| 262 | return no_page_table(vma, flags); |
Zi Yan | 84c3fc4 | 2017-09-08 16:11:01 -0700 | [diff] [blame] | 263 | goto retry; |
| 264 | } |
Huang Ying | 6882728 | 2018-06-07 17:06:34 -0700 | [diff] [blame] | 265 | if (pmd_devmap(pmdval)) { |
Dan Williams | 3565fce | 2016-01-15 16:56:55 -0800 | [diff] [blame] | 266 | ptl = pmd_lock(mm, pmd); |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 267 | page = follow_devmap_pmd(vma, address, pmd, flags, &ctx->pgmap); |
Dan Williams | 3565fce | 2016-01-15 16:56:55 -0800 | [diff] [blame] | 268 | spin_unlock(ptl); |
| 269 | if (page) |
| 270 | return page; |
| 271 | } |
Huang Ying | 6882728 | 2018-06-07 17:06:34 -0700 | [diff] [blame] | 272 | if (likely(!pmd_trans_huge(pmdval))) |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 273 | return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap); |
Kirill A. Shutemov | 6742d29 | 2016-01-15 16:52:28 -0800 | [diff] [blame] | 274 | |
Huang Ying | 6882728 | 2018-06-07 17:06:34 -0700 | [diff] [blame] | 275 | if ((flags & FOLL_NUMA) && pmd_protnone(pmdval)) |
Aneesh Kumar K.V | db08f20 | 2017-02-24 14:59:53 -0800 | [diff] [blame] | 276 | return no_page_table(vma, flags); |
| 277 | |
Zi Yan | 84c3fc4 | 2017-09-08 16:11:01 -0700 | [diff] [blame] | 278 | retry_locked: |
Kirill A. Shutemov | 6742d29 | 2016-01-15 16:52:28 -0800 | [diff] [blame] | 279 | ptl = pmd_lock(mm, pmd); |
Huang Ying | 6882728 | 2018-06-07 17:06:34 -0700 | [diff] [blame] | 280 | if (unlikely(pmd_none(*pmd))) { |
| 281 | spin_unlock(ptl); |
| 282 | return no_page_table(vma, flags); |
| 283 | } |
Zi Yan | 84c3fc4 | 2017-09-08 16:11:01 -0700 | [diff] [blame] | 284 | if (unlikely(!pmd_present(*pmd))) { |
| 285 | spin_unlock(ptl); |
| 286 | if (likely(!(flags & FOLL_MIGRATION))) |
| 287 | return no_page_table(vma, flags); |
| 288 | pmd_migration_entry_wait(mm, pmd); |
| 289 | goto retry_locked; |
| 290 | } |
Kirill A. Shutemov | 6742d29 | 2016-01-15 16:52:28 -0800 | [diff] [blame] | 291 | if (unlikely(!pmd_trans_huge(*pmd))) { |
| 292 | spin_unlock(ptl); |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 293 | return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap); |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 294 | } |
Kirill A. Shutemov | 6742d29 | 2016-01-15 16:52:28 -0800 | [diff] [blame] | 295 | if (flags & FOLL_SPLIT) { |
| 296 | int ret; |
| 297 | page = pmd_page(*pmd); |
| 298 | if (is_huge_zero_page(page)) { |
| 299 | spin_unlock(ptl); |
| 300 | ret = 0; |
Kirill A. Shutemov | 78ddc53 | 2016-01-15 16:52:42 -0800 | [diff] [blame] | 301 | split_huge_pmd(vma, pmd, address); |
Naoya Horiguchi | 337d9ab | 2016-07-26 15:24:03 -0700 | [diff] [blame] | 302 | if (pmd_trans_unstable(pmd)) |
| 303 | ret = -EBUSY; |
Kirill A. Shutemov | 6742d29 | 2016-01-15 16:52:28 -0800 | [diff] [blame] | 304 | } else { |
Linus Torvalds | 8fde12c | 2019-04-11 10:49:19 -0700 | [diff] [blame] | 305 | if (unlikely(!try_get_page(page))) { |
| 306 | spin_unlock(ptl); |
| 307 | return ERR_PTR(-ENOMEM); |
| 308 | } |
Kirill A. Shutemov | 6742d29 | 2016-01-15 16:52:28 -0800 | [diff] [blame] | 309 | spin_unlock(ptl); |
| 310 | lock_page(page); |
| 311 | ret = split_huge_page(page); |
| 312 | unlock_page(page); |
| 313 | put_page(page); |
Kirill A. Shutemov | baa355f | 2016-07-26 15:25:51 -0700 | [diff] [blame] | 314 | if (pmd_none(*pmd)) |
| 315 | return no_page_table(vma, flags); |
Kirill A. Shutemov | 6742d29 | 2016-01-15 16:52:28 -0800 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | return ret ? ERR_PTR(ret) : |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 319 | follow_page_pte(vma, address, pmd, flags, &ctx->pgmap); |
Kirill A. Shutemov | 6742d29 | 2016-01-15 16:52:28 -0800 | [diff] [blame] | 320 | } |
Kirill A. Shutemov | 6742d29 | 2016-01-15 16:52:28 -0800 | [diff] [blame] | 321 | page = follow_trans_huge_pmd(vma, address, pmd, flags); |
| 322 | spin_unlock(ptl); |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 323 | ctx->page_mask = HPAGE_PMD_NR - 1; |
Kirill A. Shutemov | 6742d29 | 2016-01-15 16:52:28 -0800 | [diff] [blame] | 324 | return page; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 327 | static struct page *follow_pud_mask(struct vm_area_struct *vma, |
| 328 | unsigned long address, p4d_t *p4dp, |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 329 | unsigned int flags, |
| 330 | struct follow_page_context *ctx) |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 331 | { |
| 332 | pud_t *pud; |
| 333 | spinlock_t *ptl; |
| 334 | struct page *page; |
| 335 | struct mm_struct *mm = vma->vm_mm; |
| 336 | |
| 337 | pud = pud_offset(p4dp, address); |
| 338 | if (pud_none(*pud)) |
| 339 | return no_page_table(vma, flags); |
| 340 | if (pud_huge(*pud) && vma->vm_flags & VM_HUGETLB) { |
| 341 | page = follow_huge_pud(mm, address, pud, flags); |
| 342 | if (page) |
| 343 | return page; |
| 344 | return no_page_table(vma, flags); |
| 345 | } |
Aneesh Kumar K.V | 4dc7145 | 2017-07-06 15:38:56 -0700 | [diff] [blame] | 346 | if (is_hugepd(__hugepd(pud_val(*pud)))) { |
| 347 | page = follow_huge_pd(vma, address, |
| 348 | __hugepd(pud_val(*pud)), flags, |
| 349 | PUD_SHIFT); |
| 350 | if (page) |
| 351 | return page; |
| 352 | return no_page_table(vma, flags); |
| 353 | } |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 354 | if (pud_devmap(*pud)) { |
| 355 | ptl = pud_lock(mm, pud); |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 356 | page = follow_devmap_pud(vma, address, pud, flags, &ctx->pgmap); |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 357 | spin_unlock(ptl); |
| 358 | if (page) |
| 359 | return page; |
| 360 | } |
| 361 | if (unlikely(pud_bad(*pud))) |
| 362 | return no_page_table(vma, flags); |
| 363 | |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 364 | return follow_pmd_mask(vma, address, pud, flags, ctx); |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 367 | static struct page *follow_p4d_mask(struct vm_area_struct *vma, |
| 368 | unsigned long address, pgd_t *pgdp, |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 369 | unsigned int flags, |
| 370 | struct follow_page_context *ctx) |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 371 | { |
| 372 | p4d_t *p4d; |
Aneesh Kumar K.V | 4dc7145 | 2017-07-06 15:38:56 -0700 | [diff] [blame] | 373 | struct page *page; |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 374 | |
| 375 | p4d = p4d_offset(pgdp, address); |
| 376 | if (p4d_none(*p4d)) |
| 377 | return no_page_table(vma, flags); |
| 378 | BUILD_BUG_ON(p4d_huge(*p4d)); |
| 379 | if (unlikely(p4d_bad(*p4d))) |
| 380 | return no_page_table(vma, flags); |
| 381 | |
Aneesh Kumar K.V | 4dc7145 | 2017-07-06 15:38:56 -0700 | [diff] [blame] | 382 | if (is_hugepd(__hugepd(p4d_val(*p4d)))) { |
| 383 | page = follow_huge_pd(vma, address, |
| 384 | __hugepd(p4d_val(*p4d)), flags, |
| 385 | P4D_SHIFT); |
| 386 | if (page) |
| 387 | return page; |
| 388 | return no_page_table(vma, flags); |
| 389 | } |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 390 | return follow_pud_mask(vma, address, p4d, flags, ctx); |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | /** |
| 394 | * follow_page_mask - look up a page descriptor from a user-virtual address |
| 395 | * @vma: vm_area_struct mapping @address |
| 396 | * @address: virtual address to look up |
| 397 | * @flags: flags modifying lookup behaviour |
Mike Rapoport | 7817955 | 2018-11-16 15:08:29 -0800 | [diff] [blame] | 398 | * @ctx: contains dev_pagemap for %ZONE_DEVICE memory pinning and a |
| 399 | * pointer to output page_mask |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 400 | * |
| 401 | * @flags can have FOLL_ flags set, defined in <linux/mm.h> |
| 402 | * |
Mike Rapoport | 7817955 | 2018-11-16 15:08:29 -0800 | [diff] [blame] | 403 | * When getting pages from ZONE_DEVICE memory, the @ctx->pgmap caches |
| 404 | * the device's dev_pagemap metadata to avoid repeating expensive lookups. |
| 405 | * |
| 406 | * On output, the @ctx->page_mask is set according to the size of the page. |
| 407 | * |
| 408 | * Return: the mapped (struct page *), %NULL if no mapping exists, or |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 409 | * an error pointer if there is a mapping to something not represented |
| 410 | * by a page descriptor (see also vm_normal_page()). |
| 411 | */ |
| 412 | struct page *follow_page_mask(struct vm_area_struct *vma, |
| 413 | unsigned long address, unsigned int flags, |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 414 | struct follow_page_context *ctx) |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 415 | { |
| 416 | pgd_t *pgd; |
| 417 | struct page *page; |
| 418 | struct mm_struct *mm = vma->vm_mm; |
| 419 | |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 420 | ctx->page_mask = 0; |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 421 | |
| 422 | /* make this handle hugepd */ |
| 423 | page = follow_huge_addr(mm, address, flags & FOLL_WRITE); |
| 424 | if (!IS_ERR(page)) { |
| 425 | BUG_ON(flags & FOLL_GET); |
| 426 | return page; |
| 427 | } |
| 428 | |
| 429 | pgd = pgd_offset(mm, address); |
| 430 | |
| 431 | if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) |
| 432 | return no_page_table(vma, flags); |
| 433 | |
Anshuman Khandual | faaa5b6 | 2017-07-06 15:38:50 -0700 | [diff] [blame] | 434 | if (pgd_huge(*pgd)) { |
| 435 | page = follow_huge_pgd(mm, address, pgd, flags); |
| 436 | if (page) |
| 437 | return page; |
| 438 | return no_page_table(vma, flags); |
| 439 | } |
Aneesh Kumar K.V | 4dc7145 | 2017-07-06 15:38:56 -0700 | [diff] [blame] | 440 | if (is_hugepd(__hugepd(pgd_val(*pgd)))) { |
| 441 | page = follow_huge_pd(vma, address, |
| 442 | __hugepd(pgd_val(*pgd)), flags, |
| 443 | PGDIR_SHIFT); |
| 444 | if (page) |
| 445 | return page; |
| 446 | return no_page_table(vma, flags); |
| 447 | } |
Anshuman Khandual | faaa5b6 | 2017-07-06 15:38:50 -0700 | [diff] [blame] | 448 | |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 449 | return follow_p4d_mask(vma, address, pgd, flags, ctx); |
| 450 | } |
| 451 | |
| 452 | struct page *follow_page(struct vm_area_struct *vma, unsigned long address, |
| 453 | unsigned int foll_flags) |
| 454 | { |
| 455 | struct follow_page_context ctx = { NULL }; |
| 456 | struct page *page; |
| 457 | |
| 458 | page = follow_page_mask(vma, address, foll_flags, &ctx); |
| 459 | if (ctx.pgmap) |
| 460 | put_dev_pagemap(ctx.pgmap); |
| 461 | return page; |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 462 | } |
| 463 | |
Kirill A. Shutemov | f2b495c | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 464 | static int get_gate_page(struct mm_struct *mm, unsigned long address, |
| 465 | unsigned int gup_flags, struct vm_area_struct **vma, |
| 466 | struct page **page) |
| 467 | { |
| 468 | pgd_t *pgd; |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 469 | p4d_t *p4d; |
Kirill A. Shutemov | f2b495c | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 470 | pud_t *pud; |
| 471 | pmd_t *pmd; |
| 472 | pte_t *pte; |
| 473 | int ret = -EFAULT; |
| 474 | |
| 475 | /* user gate pages are read-only */ |
| 476 | if (gup_flags & FOLL_WRITE) |
| 477 | return -EFAULT; |
| 478 | if (address > TASK_SIZE) |
| 479 | pgd = pgd_offset_k(address); |
| 480 | else |
| 481 | pgd = pgd_offset_gate(mm, address); |
| 482 | BUG_ON(pgd_none(*pgd)); |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 483 | p4d = p4d_offset(pgd, address); |
| 484 | BUG_ON(p4d_none(*p4d)); |
| 485 | pud = pud_offset(p4d, address); |
Kirill A. Shutemov | f2b495c | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 486 | BUG_ON(pud_none(*pud)); |
| 487 | pmd = pmd_offset(pud, address); |
Zi Yan | 84c3fc4 | 2017-09-08 16:11:01 -0700 | [diff] [blame] | 488 | if (!pmd_present(*pmd)) |
Kirill A. Shutemov | f2b495c | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 489 | return -EFAULT; |
| 490 | VM_BUG_ON(pmd_trans_huge(*pmd)); |
| 491 | pte = pte_offset_map(pmd, address); |
| 492 | if (pte_none(*pte)) |
| 493 | goto unmap; |
| 494 | *vma = get_gate_vma(mm); |
| 495 | if (!page) |
| 496 | goto out; |
| 497 | *page = vm_normal_page(*vma, address, *pte); |
| 498 | if (!*page) { |
| 499 | if ((gup_flags & FOLL_DUMP) || !is_zero_pfn(pte_pfn(*pte))) |
| 500 | goto unmap; |
| 501 | *page = pte_page(*pte); |
Jérôme Glisse | df6ad69 | 2017-09-08 16:12:24 -0700 | [diff] [blame] | 502 | |
| 503 | /* |
| 504 | * This should never happen (a device public page in the gate |
| 505 | * area). |
| 506 | */ |
| 507 | if (is_device_public_page(*page)) |
| 508 | goto unmap; |
Kirill A. Shutemov | f2b495c | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 509 | } |
Linus Torvalds | 8fde12c | 2019-04-11 10:49:19 -0700 | [diff] [blame] | 510 | if (unlikely(!try_get_page(*page))) { |
| 511 | ret = -ENOMEM; |
| 512 | goto unmap; |
| 513 | } |
Kirill A. Shutemov | f2b495c | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 514 | out: |
| 515 | ret = 0; |
| 516 | unmap: |
| 517 | pte_unmap(pte); |
| 518 | return ret; |
| 519 | } |
| 520 | |
Paul Cassella | 9a95f3c | 2014-08-06 16:07:24 -0700 | [diff] [blame] | 521 | /* |
| 522 | * mmap_sem must be held on entry. If @nonblocking != NULL and |
| 523 | * *@flags does not include FOLL_NOWAIT, the mmap_sem may be released. |
| 524 | * If it is, *@nonblocking will be set to 0 and -EBUSY returned. |
| 525 | */ |
Kirill A. Shutemov | 1674448 | 2014-06-04 16:08:12 -0700 | [diff] [blame] | 526 | static int faultin_page(struct task_struct *tsk, struct vm_area_struct *vma, |
| 527 | unsigned long address, unsigned int *flags, int *nonblocking) |
| 528 | { |
Kirill A. Shutemov | 1674448 | 2014-06-04 16:08:12 -0700 | [diff] [blame] | 529 | unsigned int fault_flags = 0; |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 530 | vm_fault_t ret; |
Kirill A. Shutemov | 1674448 | 2014-06-04 16:08:12 -0700 | [diff] [blame] | 531 | |
Eric B Munson | de60f5f | 2015-11-05 18:51:36 -0800 | [diff] [blame] | 532 | /* mlock all present pages, but do not fault in new pages */ |
| 533 | if ((*flags & (FOLL_POPULATE | FOLL_MLOCK)) == FOLL_MLOCK) |
| 534 | return -ENOENT; |
Kirill A. Shutemov | 1674448 | 2014-06-04 16:08:12 -0700 | [diff] [blame] | 535 | if (*flags & FOLL_WRITE) |
| 536 | fault_flags |= FAULT_FLAG_WRITE; |
Dave Hansen | 1b2ee12 | 2016-02-12 13:02:21 -0800 | [diff] [blame] | 537 | if (*flags & FOLL_REMOTE) |
| 538 | fault_flags |= FAULT_FLAG_REMOTE; |
Kirill A. Shutemov | 1674448 | 2014-06-04 16:08:12 -0700 | [diff] [blame] | 539 | if (nonblocking) |
| 540 | fault_flags |= FAULT_FLAG_ALLOW_RETRY; |
| 541 | if (*flags & FOLL_NOWAIT) |
| 542 | fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT; |
Andres Lagar-Cavilla | 234b239 | 2014-09-17 10:51:48 -0700 | [diff] [blame] | 543 | if (*flags & FOLL_TRIED) { |
| 544 | VM_WARN_ON_ONCE(fault_flags & FAULT_FLAG_ALLOW_RETRY); |
| 545 | fault_flags |= FAULT_FLAG_TRIED; |
| 546 | } |
Kirill A. Shutemov | 1674448 | 2014-06-04 16:08:12 -0700 | [diff] [blame] | 547 | |
Kirill A. Shutemov | dcddffd | 2016-07-26 15:25:18 -0700 | [diff] [blame] | 548 | ret = handle_mm_fault(vma, address, fault_flags); |
Kirill A. Shutemov | 1674448 | 2014-06-04 16:08:12 -0700 | [diff] [blame] | 549 | if (ret & VM_FAULT_ERROR) { |
James Morse | 9a291a7 | 2017-06-02 14:46:46 -0700 | [diff] [blame] | 550 | int err = vm_fault_to_errno(ret, *flags); |
| 551 | |
| 552 | if (err) |
| 553 | return err; |
Kirill A. Shutemov | 1674448 | 2014-06-04 16:08:12 -0700 | [diff] [blame] | 554 | BUG(); |
| 555 | } |
| 556 | |
| 557 | if (tsk) { |
| 558 | if (ret & VM_FAULT_MAJOR) |
| 559 | tsk->maj_flt++; |
| 560 | else |
| 561 | tsk->min_flt++; |
| 562 | } |
| 563 | |
| 564 | if (ret & VM_FAULT_RETRY) { |
Andrea Arcangeli | 96312e6 | 2018-03-09 15:51:06 -0800 | [diff] [blame] | 565 | if (nonblocking && !(fault_flags & FAULT_FLAG_RETRY_NOWAIT)) |
Kirill A. Shutemov | 1674448 | 2014-06-04 16:08:12 -0700 | [diff] [blame] | 566 | *nonblocking = 0; |
| 567 | return -EBUSY; |
| 568 | } |
| 569 | |
| 570 | /* |
| 571 | * The VM_FAULT_WRITE bit tells us that do_wp_page has broken COW when |
| 572 | * necessary, even if maybe_mkwrite decided not to set pte_write. We |
| 573 | * can thus safely do subsequent page lookups as if they were reads. |
| 574 | * But only do so when looping for pte_write is futile: in some cases |
| 575 | * userspace may also be wanting to write to the gotten user page, |
| 576 | * which a read fault here might prevent (a readonly page might get |
| 577 | * reCOWed by userspace write). |
| 578 | */ |
| 579 | if ((ret & VM_FAULT_WRITE) && !(vma->vm_flags & VM_WRITE)) |
Mario Leinweber | 2923117 | 2018-04-05 16:24:18 -0700 | [diff] [blame] | 580 | *flags |= FOLL_COW; |
Kirill A. Shutemov | 1674448 | 2014-06-04 16:08:12 -0700 | [diff] [blame] | 581 | return 0; |
| 582 | } |
| 583 | |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 584 | static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags) |
| 585 | { |
| 586 | vm_flags_t vm_flags = vma->vm_flags; |
Dave Hansen | 1b2ee12 | 2016-02-12 13:02:21 -0800 | [diff] [blame] | 587 | int write = (gup_flags & FOLL_WRITE); |
| 588 | int foreign = (gup_flags & FOLL_REMOTE); |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 589 | |
| 590 | if (vm_flags & (VM_IO | VM_PFNMAP)) |
| 591 | return -EFAULT; |
| 592 | |
Willy Tarreau | 7f7ccc2 | 2018-05-11 08:11:44 +0200 | [diff] [blame] | 593 | if (gup_flags & FOLL_ANON && !vma_is_anonymous(vma)) |
| 594 | return -EFAULT; |
| 595 | |
Dave Hansen | 1b2ee12 | 2016-02-12 13:02:21 -0800 | [diff] [blame] | 596 | if (write) { |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 597 | if (!(vm_flags & VM_WRITE)) { |
| 598 | if (!(gup_flags & FOLL_FORCE)) |
| 599 | return -EFAULT; |
| 600 | /* |
| 601 | * We used to let the write,force case do COW in a |
| 602 | * VM_MAYWRITE VM_SHARED !VM_WRITE vma, so ptrace could |
| 603 | * set a breakpoint in a read-only mapping of an |
| 604 | * executable, without corrupting the file (yet only |
| 605 | * when that file had been opened for writing!). |
| 606 | * Anon pages in shared mappings are surprising: now |
| 607 | * just reject it. |
| 608 | */ |
Hugh Dickins | 4643536 | 2016-01-30 18:03:16 -0800 | [diff] [blame] | 609 | if (!is_cow_mapping(vm_flags)) |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 610 | return -EFAULT; |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 611 | } |
| 612 | } else if (!(vm_flags & VM_READ)) { |
| 613 | if (!(gup_flags & FOLL_FORCE)) |
| 614 | return -EFAULT; |
| 615 | /* |
| 616 | * Is there actually any vma we can reach here which does not |
| 617 | * have VM_MAYREAD set? |
| 618 | */ |
| 619 | if (!(vm_flags & VM_MAYREAD)) |
| 620 | return -EFAULT; |
| 621 | } |
Dave Hansen | d61172b | 2016-02-12 13:02:24 -0800 | [diff] [blame] | 622 | /* |
| 623 | * gups are always data accesses, not instruction |
| 624 | * fetches, so execute=false here |
| 625 | */ |
| 626 | if (!arch_vma_access_permitted(vma, write, false, foreign)) |
Dave Hansen | 33a709b | 2016-02-12 13:02:19 -0800 | [diff] [blame] | 627 | return -EFAULT; |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 628 | return 0; |
| 629 | } |
| 630 | |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 631 | /** |
| 632 | * __get_user_pages() - pin user pages in memory |
| 633 | * @tsk: task_struct of target task |
| 634 | * @mm: mm_struct of target mm |
| 635 | * @start: starting user address |
| 636 | * @nr_pages: number of pages from start to pin |
| 637 | * @gup_flags: flags modifying pin behaviour |
| 638 | * @pages: array that receives pointers to the pages pinned. |
| 639 | * Should be at least nr_pages long. Or NULL, if caller |
| 640 | * only intends to ensure the pages are faulted in. |
| 641 | * @vmas: array of pointers to vmas corresponding to each page. |
| 642 | * Or NULL if the caller does not require them. |
| 643 | * @nonblocking: whether waiting for disk IO or mmap_sem contention |
| 644 | * |
| 645 | * Returns number of pages pinned. This may be fewer than the number |
| 646 | * requested. If nr_pages is 0 or negative, returns 0. If no pages |
| 647 | * were pinned, returns -errno. Each page returned must be released |
| 648 | * with a put_page() call when it is finished with. vmas will only |
| 649 | * remain valid while mmap_sem is held. |
| 650 | * |
Paul Cassella | 9a95f3c | 2014-08-06 16:07:24 -0700 | [diff] [blame] | 651 | * 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] | 652 | * |
| 653 | * __get_user_pages walks a process's page tables and takes a reference to |
| 654 | * each struct page that each user address corresponds to at a given |
| 655 | * instant. That is, it takes the page that would be accessed if a user |
| 656 | * thread accesses the given user virtual address at that instant. |
| 657 | * |
| 658 | * This does not guarantee that the page exists in the user mappings when |
| 659 | * __get_user_pages returns, and there may even be a completely different |
| 660 | * page there in some cases (eg. if mmapped pagecache has been invalidated |
| 661 | * and subsequently re faulted). However it does guarantee that the page |
| 662 | * won't be freed completely. And mostly callers simply care that the page |
| 663 | * contains data that was valid *at some point in time*. Typically, an IO |
| 664 | * or similar operation cannot guarantee anything stronger anyway because |
| 665 | * locks can't be held over the syscall boundary. |
| 666 | * |
| 667 | * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If |
| 668 | * the page is written to, set_page_dirty (or set_page_dirty_lock, as |
| 669 | * appropriate) must be called after the page is finished with, and |
| 670 | * before put_page is called. |
| 671 | * |
| 672 | * If @nonblocking != NULL, __get_user_pages will not wait for disk IO |
| 673 | * 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] | 674 | * *@nonblocking will be set to 0. Further, if @gup_flags does not |
| 675 | * include FOLL_NOWAIT, the mmap_sem will be released via up_read() in |
| 676 | * this case. |
| 677 | * |
| 678 | * A caller using such a combination of @nonblocking and @gup_flags |
| 679 | * must therefore hold the mmap_sem for reading only, and recognize |
| 680 | * when it's been released. Otherwise, it must be held for either |
| 681 | * reading or writing and will not be released. |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 682 | * |
| 683 | * In most cases, get_user_pages or get_user_pages_fast should be used |
| 684 | * instead of __get_user_pages. __get_user_pages should be used only if |
| 685 | * you need some special @gup_flags. |
| 686 | */ |
Lorenzo Stoakes | 0d73175 | 2016-10-24 10:57:25 +0100 | [diff] [blame] | 687 | static long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm, |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 688 | unsigned long start, unsigned long nr_pages, |
| 689 | unsigned int gup_flags, struct page **pages, |
| 690 | struct vm_area_struct **vmas, int *nonblocking) |
| 691 | { |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 692 | long ret = 0, i = 0; |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 693 | struct vm_area_struct *vma = NULL; |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 694 | struct follow_page_context ctx = { NULL }; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 695 | |
| 696 | if (!nr_pages) |
| 697 | return 0; |
| 698 | |
| 699 | VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET)); |
| 700 | |
| 701 | /* |
| 702 | * If FOLL_FORCE is set then do not force a full fault as the hinting |
| 703 | * fault information is unrelated to the reference behaviour of a task |
| 704 | * using the address space |
| 705 | */ |
| 706 | if (!(gup_flags & FOLL_FORCE)) |
| 707 | gup_flags |= FOLL_NUMA; |
| 708 | |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 709 | do { |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 710 | struct page *page; |
| 711 | unsigned int foll_flags = gup_flags; |
| 712 | unsigned int page_increm; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 713 | |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 714 | /* first iteration or cross vma bound */ |
| 715 | if (!vma || start >= vma->vm_end) { |
| 716 | vma = find_extend_vma(mm, start); |
| 717 | if (!vma && in_gate_area(mm, start)) { |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 718 | ret = get_gate_page(mm, start & PAGE_MASK, |
| 719 | gup_flags, &vma, |
| 720 | pages ? &pages[i] : NULL); |
| 721 | if (ret) |
John Hubbard | 08be37b | 2018-11-30 14:08:53 -0800 | [diff] [blame] | 722 | goto out; |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 723 | ctx.page_mask = 0; |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 724 | goto next_page; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 725 | } |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 726 | |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 727 | if (!vma || check_vma_flags(vma, gup_flags)) { |
| 728 | ret = -EFAULT; |
| 729 | goto out; |
| 730 | } |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 731 | if (is_vm_hugetlb_page(vma)) { |
| 732 | i = follow_hugetlb_page(mm, vma, pages, vmas, |
| 733 | &start, &nr_pages, i, |
Andrea Arcangeli | 87ffc11 | 2017-02-22 15:43:13 -0800 | [diff] [blame] | 734 | gup_flags, nonblocking); |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 735 | continue; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 736 | } |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 737 | } |
| 738 | retry: |
| 739 | /* |
| 740 | * If we have a pending SIGKILL, don't keep faulting pages and |
| 741 | * potentially allocating memory. |
| 742 | */ |
Davidlohr Bueso | fa45f11 | 2019-01-03 15:28:55 -0800 | [diff] [blame] | 743 | if (fatal_signal_pending(current)) { |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 744 | ret = -ERESTARTSYS; |
| 745 | goto out; |
| 746 | } |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 747 | cond_resched(); |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 748 | |
| 749 | page = follow_page_mask(vma, start, foll_flags, &ctx); |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 750 | if (!page) { |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 751 | ret = faultin_page(tsk, vma, start, &foll_flags, |
| 752 | nonblocking); |
| 753 | switch (ret) { |
| 754 | case 0: |
| 755 | goto retry; |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 756 | case -EBUSY: |
| 757 | ret = 0; |
| 758 | /* FALLTHRU */ |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 759 | case -EFAULT: |
| 760 | case -ENOMEM: |
| 761 | case -EHWPOISON: |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 762 | goto out; |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 763 | case -ENOENT: |
| 764 | goto next_page; |
| 765 | } |
| 766 | BUG(); |
Kirill A. Shutemov | 1027e44 | 2015-09-04 15:47:55 -0700 | [diff] [blame] | 767 | } else if (PTR_ERR(page) == -EEXIST) { |
| 768 | /* |
| 769 | * Proper page table entry exists, but no corresponding |
| 770 | * struct page. |
| 771 | */ |
| 772 | goto next_page; |
| 773 | } else if (IS_ERR(page)) { |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 774 | ret = PTR_ERR(page); |
| 775 | goto out; |
Kirill A. Shutemov | 1027e44 | 2015-09-04 15:47:55 -0700 | [diff] [blame] | 776 | } |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 777 | if (pages) { |
| 778 | pages[i] = page; |
| 779 | flush_anon_page(vma, page, start); |
| 780 | flush_dcache_page(page); |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 781 | ctx.page_mask = 0; |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 782 | } |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 783 | next_page: |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 784 | if (vmas) { |
| 785 | vmas[i] = vma; |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 786 | ctx.page_mask = 0; |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 787 | } |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 788 | page_increm = 1 + (~(start >> PAGE_SHIFT) & ctx.page_mask); |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 789 | if (page_increm > nr_pages) |
| 790 | page_increm = nr_pages; |
| 791 | i += page_increm; |
| 792 | start += page_increm * PAGE_SIZE; |
| 793 | nr_pages -= page_increm; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 794 | } while (nr_pages); |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 795 | out: |
| 796 | if (ctx.pgmap) |
| 797 | put_dev_pagemap(ctx.pgmap); |
| 798 | return i ? i : ret; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 799 | } |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 800 | |
Tobias Klauser | 771ab43 | 2016-12-12 16:41:53 -0800 | [diff] [blame] | 801 | static bool vma_permits_fault(struct vm_area_struct *vma, |
| 802 | unsigned int fault_flags) |
Dave Hansen | d4925e0 | 2016-02-12 13:02:16 -0800 | [diff] [blame] | 803 | { |
Dave Hansen | 1b2ee12 | 2016-02-12 13:02:21 -0800 | [diff] [blame] | 804 | bool write = !!(fault_flags & FAULT_FLAG_WRITE); |
| 805 | bool foreign = !!(fault_flags & FAULT_FLAG_REMOTE); |
Dave Hansen | 33a709b | 2016-02-12 13:02:19 -0800 | [diff] [blame] | 806 | vm_flags_t vm_flags = write ? VM_WRITE : VM_READ; |
Dave Hansen | d4925e0 | 2016-02-12 13:02:16 -0800 | [diff] [blame] | 807 | |
| 808 | if (!(vm_flags & vma->vm_flags)) |
| 809 | return false; |
| 810 | |
Dave Hansen | 33a709b | 2016-02-12 13:02:19 -0800 | [diff] [blame] | 811 | /* |
| 812 | * The architecture might have a hardware protection |
Dave Hansen | 1b2ee12 | 2016-02-12 13:02:21 -0800 | [diff] [blame] | 813 | * mechanism other than read/write that can deny access. |
Dave Hansen | d61172b | 2016-02-12 13:02:24 -0800 | [diff] [blame] | 814 | * |
| 815 | * gup always represents data access, not instruction |
| 816 | * fetches, so execute=false here: |
Dave Hansen | 33a709b | 2016-02-12 13:02:19 -0800 | [diff] [blame] | 817 | */ |
Dave Hansen | d61172b | 2016-02-12 13:02:24 -0800 | [diff] [blame] | 818 | if (!arch_vma_access_permitted(vma, write, false, foreign)) |
Dave Hansen | 33a709b | 2016-02-12 13:02:19 -0800 | [diff] [blame] | 819 | return false; |
| 820 | |
Dave Hansen | d4925e0 | 2016-02-12 13:02:16 -0800 | [diff] [blame] | 821 | return true; |
| 822 | } |
| 823 | |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 824 | /* |
| 825 | * fixup_user_fault() - manually resolve a user page fault |
| 826 | * @tsk: the task_struct to use for page fault accounting, or |
| 827 | * NULL if faults are not to be recorded. |
| 828 | * @mm: mm_struct of target mm |
| 829 | * @address: user address |
| 830 | * @fault_flags:flags to pass down to handle_mm_fault() |
Dominik Dingel | 4a9e1cd | 2016-01-15 16:57:04 -0800 | [diff] [blame] | 831 | * @unlocked: did we unlock the mmap_sem while retrying, maybe NULL if caller |
| 832 | * does not allow retry |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 833 | * |
| 834 | * This is meant to be called in the specific scenario where for locking reasons |
| 835 | * we try to access user memory in atomic context (within a pagefault_disable() |
| 836 | * section), this returns -EFAULT, and we want to resolve the user fault before |
| 837 | * trying again. |
| 838 | * |
| 839 | * Typically this is meant to be used by the futex code. |
| 840 | * |
| 841 | * The main difference with get_user_pages() is that this function will |
| 842 | * unconditionally call handle_mm_fault() which will in turn perform all the |
| 843 | * necessary SW fixup of the dirty and young bits in the PTE, while |
Dominik Dingel | 4a9e1cd | 2016-01-15 16:57:04 -0800 | [diff] [blame] | 844 | * get_user_pages() only guarantees to update these in the struct page. |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 845 | * |
| 846 | * This is important for some architectures where those bits also gate the |
| 847 | * access permission to the page because they are maintained in software. On |
| 848 | * such architectures, gup() will not be enough to make a subsequent access |
| 849 | * succeed. |
| 850 | * |
Dominik Dingel | 4a9e1cd | 2016-01-15 16:57:04 -0800 | [diff] [blame] | 851 | * This function will not return with an unlocked mmap_sem. So it has not the |
| 852 | * same semantics wrt the @mm->mmap_sem as does filemap_fault(). |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 853 | */ |
| 854 | int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm, |
Dominik Dingel | 4a9e1cd | 2016-01-15 16:57:04 -0800 | [diff] [blame] | 855 | unsigned long address, unsigned int fault_flags, |
| 856 | bool *unlocked) |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 857 | { |
| 858 | struct vm_area_struct *vma; |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 859 | vm_fault_t ret, major = 0; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 860 | |
Dominik Dingel | 4a9e1cd | 2016-01-15 16:57:04 -0800 | [diff] [blame] | 861 | if (unlocked) |
| 862 | fault_flags |= FAULT_FLAG_ALLOW_RETRY; |
| 863 | |
| 864 | retry: |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 865 | vma = find_extend_vma(mm, address); |
| 866 | if (!vma || address < vma->vm_start) |
| 867 | return -EFAULT; |
| 868 | |
Dave Hansen | d4925e0 | 2016-02-12 13:02:16 -0800 | [diff] [blame] | 869 | if (!vma_permits_fault(vma, fault_flags)) |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 870 | return -EFAULT; |
| 871 | |
Kirill A. Shutemov | dcddffd | 2016-07-26 15:25:18 -0700 | [diff] [blame] | 872 | ret = handle_mm_fault(vma, address, fault_flags); |
Dominik Dingel | 4a9e1cd | 2016-01-15 16:57:04 -0800 | [diff] [blame] | 873 | major |= ret & VM_FAULT_MAJOR; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 874 | if (ret & VM_FAULT_ERROR) { |
James Morse | 9a291a7 | 2017-06-02 14:46:46 -0700 | [diff] [blame] | 875 | int err = vm_fault_to_errno(ret, 0); |
| 876 | |
| 877 | if (err) |
| 878 | return err; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 879 | BUG(); |
| 880 | } |
Dominik Dingel | 4a9e1cd | 2016-01-15 16:57:04 -0800 | [diff] [blame] | 881 | |
| 882 | if (ret & VM_FAULT_RETRY) { |
| 883 | down_read(&mm->mmap_sem); |
| 884 | if (!(fault_flags & FAULT_FLAG_TRIED)) { |
| 885 | *unlocked = true; |
| 886 | fault_flags &= ~FAULT_FLAG_ALLOW_RETRY; |
| 887 | fault_flags |= FAULT_FLAG_TRIED; |
| 888 | goto retry; |
| 889 | } |
| 890 | } |
| 891 | |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 892 | if (tsk) { |
Dominik Dingel | 4a9e1cd | 2016-01-15 16:57:04 -0800 | [diff] [blame] | 893 | if (major) |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 894 | tsk->maj_flt++; |
| 895 | else |
| 896 | tsk->min_flt++; |
| 897 | } |
| 898 | return 0; |
| 899 | } |
Paolo Bonzini | add6a0c | 2016-06-07 17:51:18 +0200 | [diff] [blame] | 900 | EXPORT_SYMBOL_GPL(fixup_user_fault); |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 901 | |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 902 | static __always_inline long __get_user_pages_locked(struct task_struct *tsk, |
| 903 | struct mm_struct *mm, |
| 904 | unsigned long start, |
| 905 | unsigned long nr_pages, |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 906 | struct page **pages, |
| 907 | struct vm_area_struct **vmas, |
Al Viro | e716712 | 2017-11-19 11:32:05 -0500 | [diff] [blame] | 908 | int *locked, |
Andrea Arcangeli | 0fd71a5 | 2015-02-11 15:27:20 -0800 | [diff] [blame] | 909 | unsigned int flags) |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 910 | { |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 911 | long ret, pages_done; |
| 912 | bool lock_dropped; |
| 913 | |
| 914 | if (locked) { |
| 915 | /* if VM_FAULT_RETRY can be returned, vmas become invalid */ |
| 916 | BUG_ON(vmas); |
| 917 | /* check caller initialized locked */ |
| 918 | BUG_ON(*locked != 1); |
| 919 | } |
| 920 | |
| 921 | if (pages) |
| 922 | flags |= FOLL_GET; |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 923 | |
| 924 | pages_done = 0; |
| 925 | lock_dropped = false; |
| 926 | for (;;) { |
| 927 | ret = __get_user_pages(tsk, mm, start, nr_pages, flags, pages, |
| 928 | vmas, locked); |
| 929 | if (!locked) |
| 930 | /* VM_FAULT_RETRY couldn't trigger, bypass */ |
| 931 | return ret; |
| 932 | |
| 933 | /* VM_FAULT_RETRY cannot return errors */ |
| 934 | if (!*locked) { |
| 935 | BUG_ON(ret < 0); |
| 936 | BUG_ON(ret >= nr_pages); |
| 937 | } |
| 938 | |
| 939 | if (!pages) |
| 940 | /* If it's a prefault don't insist harder */ |
| 941 | return ret; |
| 942 | |
| 943 | if (ret > 0) { |
| 944 | nr_pages -= ret; |
| 945 | pages_done += ret; |
| 946 | if (!nr_pages) |
| 947 | break; |
| 948 | } |
| 949 | if (*locked) { |
Andrea Arcangeli | 96312e6 | 2018-03-09 15:51:06 -0800 | [diff] [blame] | 950 | /* |
| 951 | * VM_FAULT_RETRY didn't trigger or it was a |
| 952 | * FOLL_NOWAIT. |
| 953 | */ |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 954 | if (!pages_done) |
| 955 | pages_done = ret; |
| 956 | break; |
| 957 | } |
| 958 | /* VM_FAULT_RETRY triggered, so seek to the faulting offset */ |
| 959 | pages += ret; |
| 960 | start += ret << PAGE_SHIFT; |
| 961 | |
| 962 | /* |
| 963 | * Repeat on the address that fired VM_FAULT_RETRY |
| 964 | * without FAULT_FLAG_ALLOW_RETRY but with |
| 965 | * FAULT_FLAG_TRIED. |
| 966 | */ |
| 967 | *locked = 1; |
| 968 | lock_dropped = true; |
| 969 | down_read(&mm->mmap_sem); |
| 970 | ret = __get_user_pages(tsk, mm, start, 1, flags | FOLL_TRIED, |
| 971 | pages, NULL, NULL); |
| 972 | if (ret != 1) { |
| 973 | BUG_ON(ret > 1); |
| 974 | if (!pages_done) |
| 975 | pages_done = ret; |
| 976 | break; |
| 977 | } |
| 978 | nr_pages--; |
| 979 | pages_done++; |
| 980 | if (!nr_pages) |
| 981 | break; |
| 982 | pages++; |
| 983 | start += PAGE_SIZE; |
| 984 | } |
Al Viro | e716712 | 2017-11-19 11:32:05 -0500 | [diff] [blame] | 985 | if (lock_dropped && *locked) { |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 986 | /* |
| 987 | * We must let the caller know we temporarily dropped the lock |
| 988 | * and so the critical section protected by it was lost. |
| 989 | */ |
| 990 | up_read(&mm->mmap_sem); |
| 991 | *locked = 0; |
| 992 | } |
| 993 | return pages_done; |
| 994 | } |
| 995 | |
| 996 | /* |
| 997 | * We can leverage the VM_FAULT_RETRY functionality in the page fault |
| 998 | * paths better by using either get_user_pages_locked() or |
| 999 | * get_user_pages_unlocked(). |
| 1000 | * |
| 1001 | * get_user_pages_locked() is suitable to replace the form: |
| 1002 | * |
| 1003 | * down_read(&mm->mmap_sem); |
| 1004 | * do_something() |
| 1005 | * get_user_pages(tsk, mm, ..., pages, NULL); |
| 1006 | * up_read(&mm->mmap_sem); |
| 1007 | * |
| 1008 | * to: |
| 1009 | * |
| 1010 | * int locked = 1; |
| 1011 | * down_read(&mm->mmap_sem); |
| 1012 | * do_something() |
| 1013 | * get_user_pages_locked(tsk, mm, ..., pages, &locked); |
| 1014 | * if (locked) |
| 1015 | * up_read(&mm->mmap_sem); |
| 1016 | */ |
Ingo Molnar | c12d2da | 2016-04-04 10:24:58 +0200 | [diff] [blame] | 1017 | long get_user_pages_locked(unsigned long start, unsigned long nr_pages, |
Lorenzo Stoakes | 3b91317 | 2016-10-13 01:20:14 +0100 | [diff] [blame] | 1018 | unsigned int gup_flags, struct page **pages, |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 1019 | int *locked) |
| 1020 | { |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1021 | /* |
| 1022 | * FIXME: Current FOLL_LONGTERM behavior is incompatible with |
| 1023 | * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on |
| 1024 | * vmas. As there are no users of this flag in this call we simply |
| 1025 | * disallow this option for now. |
| 1026 | */ |
| 1027 | if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM)) |
| 1028 | return -EINVAL; |
| 1029 | |
Dave Hansen | cde7014 | 2016-02-12 13:01:55 -0800 | [diff] [blame] | 1030 | return __get_user_pages_locked(current, current->mm, start, nr_pages, |
Al Viro | e716712 | 2017-11-19 11:32:05 -0500 | [diff] [blame] | 1031 | pages, NULL, locked, |
Lorenzo Stoakes | 3b91317 | 2016-10-13 01:20:14 +0100 | [diff] [blame] | 1032 | gup_flags | FOLL_TOUCH); |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 1033 | } |
Ingo Molnar | c12d2da | 2016-04-04 10:24:58 +0200 | [diff] [blame] | 1034 | EXPORT_SYMBOL(get_user_pages_locked); |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 1035 | |
| 1036 | /* |
| 1037 | * get_user_pages_unlocked() is suitable to replace the form: |
| 1038 | * |
| 1039 | * down_read(&mm->mmap_sem); |
| 1040 | * get_user_pages(tsk, mm, ..., pages, NULL); |
| 1041 | * up_read(&mm->mmap_sem); |
| 1042 | * |
| 1043 | * with: |
| 1044 | * |
| 1045 | * get_user_pages_unlocked(tsk, mm, ..., pages); |
| 1046 | * |
| 1047 | * It is functionally equivalent to get_user_pages_fast so |
Lorenzo Stoakes | 80a7951 | 2016-12-12 16:42:46 -0800 | [diff] [blame] | 1048 | * get_user_pages_fast should be used instead if specific gup_flags |
| 1049 | * (e.g. FOLL_FORCE) are not required. |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 1050 | */ |
Ingo Molnar | c12d2da | 2016-04-04 10:24:58 +0200 | [diff] [blame] | 1051 | long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages, |
Lorenzo Stoakes | c164154 | 2016-10-13 01:20:13 +0100 | [diff] [blame] | 1052 | struct page **pages, unsigned int gup_flags) |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 1053 | { |
Al Viro | c803c9c | 2017-11-18 14:17:46 -0500 | [diff] [blame] | 1054 | struct mm_struct *mm = current->mm; |
| 1055 | int locked = 1; |
| 1056 | long ret; |
| 1057 | |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1058 | /* |
| 1059 | * FIXME: Current FOLL_LONGTERM behavior is incompatible with |
| 1060 | * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on |
| 1061 | * vmas. As there are no users of this flag in this call we simply |
| 1062 | * disallow this option for now. |
| 1063 | */ |
| 1064 | if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM)) |
| 1065 | return -EINVAL; |
| 1066 | |
Al Viro | c803c9c | 2017-11-18 14:17:46 -0500 | [diff] [blame] | 1067 | down_read(&mm->mmap_sem); |
| 1068 | ret = __get_user_pages_locked(current, mm, start, nr_pages, pages, NULL, |
Al Viro | e716712 | 2017-11-19 11:32:05 -0500 | [diff] [blame] | 1069 | &locked, gup_flags | FOLL_TOUCH); |
Al Viro | c803c9c | 2017-11-18 14:17:46 -0500 | [diff] [blame] | 1070 | if (locked) |
| 1071 | up_read(&mm->mmap_sem); |
| 1072 | return ret; |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 1073 | } |
Ingo Molnar | c12d2da | 2016-04-04 10:24:58 +0200 | [diff] [blame] | 1074 | EXPORT_SYMBOL(get_user_pages_unlocked); |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 1075 | |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 1076 | /* |
Dave Hansen | 1e98779 | 2016-02-12 13:01:54 -0800 | [diff] [blame] | 1077 | * get_user_pages_remote() - pin user pages in memory |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 1078 | * @tsk: the task_struct to use for page fault accounting, or |
| 1079 | * NULL if faults are not to be recorded. |
| 1080 | * @mm: mm_struct of target mm |
| 1081 | * @start: starting user address |
| 1082 | * @nr_pages: number of pages from start to pin |
Lorenzo Stoakes | 9beae1e | 2016-10-13 01:20:17 +0100 | [diff] [blame] | 1083 | * @gup_flags: flags modifying lookup behaviour |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 1084 | * @pages: array that receives pointers to the pages pinned. |
| 1085 | * Should be at least nr_pages long. Or NULL, if caller |
| 1086 | * only intends to ensure the pages are faulted in. |
| 1087 | * @vmas: array of pointers to vmas corresponding to each page. |
| 1088 | * Or NULL if the caller does not require them. |
Lorenzo Stoakes | 5b56d49 | 2016-12-14 15:06:52 -0800 | [diff] [blame] | 1089 | * @locked: pointer to lock flag indicating whether lock is held and |
| 1090 | * subsequently whether VM_FAULT_RETRY functionality can be |
| 1091 | * utilised. Lock must initially be held. |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 1092 | * |
| 1093 | * Returns number of pages pinned. This may be fewer than the number |
| 1094 | * requested. If nr_pages is 0 or negative, returns 0. If no pages |
| 1095 | * were pinned, returns -errno. Each page returned must be released |
| 1096 | * with a put_page() call when it is finished with. vmas will only |
| 1097 | * remain valid while mmap_sem is held. |
| 1098 | * |
| 1099 | * Must be called with mmap_sem held for read or write. |
| 1100 | * |
| 1101 | * get_user_pages walks a process's page tables and takes a reference to |
| 1102 | * each struct page that each user address corresponds to at a given |
| 1103 | * instant. That is, it takes the page that would be accessed if a user |
| 1104 | * thread accesses the given user virtual address at that instant. |
| 1105 | * |
| 1106 | * This does not guarantee that the page exists in the user mappings when |
| 1107 | * get_user_pages returns, and there may even be a completely different |
| 1108 | * page there in some cases (eg. if mmapped pagecache has been invalidated |
| 1109 | * and subsequently re faulted). However it does guarantee that the page |
| 1110 | * won't be freed completely. And mostly callers simply care that the page |
| 1111 | * contains data that was valid *at some point in time*. Typically, an IO |
| 1112 | * or similar operation cannot guarantee anything stronger anyway because |
| 1113 | * locks can't be held over the syscall boundary. |
| 1114 | * |
Lorenzo Stoakes | 9beae1e | 2016-10-13 01:20:17 +0100 | [diff] [blame] | 1115 | * If gup_flags & FOLL_WRITE == 0, the page must not be written to. If the page |
| 1116 | * is written to, set_page_dirty (or set_page_dirty_lock, as appropriate) must |
| 1117 | * be called after the page is finished with, and before put_page is called. |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 1118 | * |
| 1119 | * get_user_pages is typically used for fewer-copy IO operations, to get a |
| 1120 | * handle on the memory by some means other than accesses via the user virtual |
| 1121 | * addresses. The pages may be submitted for DMA to devices or accessed via |
| 1122 | * their kernel linear mapping (via the kmap APIs). Care should be taken to |
| 1123 | * use the correct cache flushing APIs. |
| 1124 | * |
| 1125 | * See also get_user_pages_fast, for performance critical applications. |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 1126 | * |
| 1127 | * get_user_pages should be phased out in favor of |
| 1128 | * get_user_pages_locked|unlocked or get_user_pages_fast. Nothing |
| 1129 | * should use get_user_pages because it cannot pass |
| 1130 | * FAULT_FLAG_ALLOW_RETRY to handle_mm_fault. |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 1131 | */ |
Dave Hansen | 1e98779 | 2016-02-12 13:01:54 -0800 | [diff] [blame] | 1132 | long get_user_pages_remote(struct task_struct *tsk, struct mm_struct *mm, |
| 1133 | unsigned long start, unsigned long nr_pages, |
Lorenzo Stoakes | 9beae1e | 2016-10-13 01:20:17 +0100 | [diff] [blame] | 1134 | unsigned int gup_flags, struct page **pages, |
Lorenzo Stoakes | 5b56d49 | 2016-12-14 15:06:52 -0800 | [diff] [blame] | 1135 | struct vm_area_struct **vmas, int *locked) |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 1136 | { |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1137 | /* |
| 1138 | * FIXME: Current FOLL_LONGTERM behavior is incompatible with |
| 1139 | * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on |
| 1140 | * vmas. As there are no users of this flag in this call we simply |
| 1141 | * disallow this option for now. |
| 1142 | */ |
| 1143 | if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM)) |
| 1144 | return -EINVAL; |
| 1145 | |
Lorenzo Stoakes | 859110d | 2016-10-13 01:20:11 +0100 | [diff] [blame] | 1146 | return __get_user_pages_locked(tsk, mm, start, nr_pages, pages, vmas, |
Al Viro | e716712 | 2017-11-19 11:32:05 -0500 | [diff] [blame] | 1147 | locked, |
Lorenzo Stoakes | 9beae1e | 2016-10-13 01:20:17 +0100 | [diff] [blame] | 1148 | gup_flags | FOLL_TOUCH | FOLL_REMOTE); |
Dave Hansen | 1e98779 | 2016-02-12 13:01:54 -0800 | [diff] [blame] | 1149 | } |
| 1150 | EXPORT_SYMBOL(get_user_pages_remote); |
| 1151 | |
Aneesh Kumar K.V | 9a4e9f3 | 2019-03-05 15:47:44 -0800 | [diff] [blame] | 1152 | #if defined(CONFIG_FS_DAX) || defined (CONFIG_CMA) |
Aneesh Kumar K.V | 9a4e9f3 | 2019-03-05 15:47:44 -0800 | [diff] [blame] | 1153 | static bool check_dax_vmas(struct vm_area_struct **vmas, long nr_pages) |
| 1154 | { |
| 1155 | long i; |
| 1156 | struct vm_area_struct *vma_prev = NULL; |
| 1157 | |
| 1158 | for (i = 0; i < nr_pages; i++) { |
| 1159 | struct vm_area_struct *vma = vmas[i]; |
| 1160 | |
| 1161 | if (vma == vma_prev) |
| 1162 | continue; |
| 1163 | |
| 1164 | vma_prev = vma; |
| 1165 | |
| 1166 | if (vma_is_fsdax(vma)) |
| 1167 | return true; |
| 1168 | } |
| 1169 | return false; |
| 1170 | } |
Aneesh Kumar K.V | 9a4e9f3 | 2019-03-05 15:47:44 -0800 | [diff] [blame] | 1171 | |
| 1172 | #ifdef CONFIG_CMA |
| 1173 | static struct page *new_non_cma_page(struct page *page, unsigned long private) |
| 1174 | { |
| 1175 | /* |
| 1176 | * We want to make sure we allocate the new page from the same node |
| 1177 | * as the source page. |
| 1178 | */ |
| 1179 | int nid = page_to_nid(page); |
| 1180 | /* |
| 1181 | * Trying to allocate a page for migration. Ignore allocation |
| 1182 | * failure warnings. We don't force __GFP_THISNODE here because |
| 1183 | * this node here is the node where we have CMA reservation and |
| 1184 | * in some case these nodes will have really less non movable |
| 1185 | * allocation memory. |
| 1186 | */ |
| 1187 | gfp_t gfp_mask = GFP_USER | __GFP_NOWARN; |
| 1188 | |
| 1189 | if (PageHighMem(page)) |
| 1190 | gfp_mask |= __GFP_HIGHMEM; |
| 1191 | |
| 1192 | #ifdef CONFIG_HUGETLB_PAGE |
| 1193 | if (PageHuge(page)) { |
| 1194 | struct hstate *h = page_hstate(page); |
| 1195 | /* |
| 1196 | * We don't want to dequeue from the pool because pool pages will |
| 1197 | * mostly be from the CMA region. |
| 1198 | */ |
| 1199 | return alloc_migrate_huge_page(h, gfp_mask, nid, NULL); |
| 1200 | } |
| 1201 | #endif |
| 1202 | if (PageTransHuge(page)) { |
| 1203 | struct page *thp; |
| 1204 | /* |
| 1205 | * ignore allocation failure warnings |
| 1206 | */ |
| 1207 | gfp_t thp_gfpmask = GFP_TRANSHUGE | __GFP_NOWARN; |
| 1208 | |
| 1209 | /* |
| 1210 | * Remove the movable mask so that we don't allocate from |
| 1211 | * CMA area again. |
| 1212 | */ |
| 1213 | thp_gfpmask &= ~__GFP_MOVABLE; |
| 1214 | thp = __alloc_pages_node(nid, thp_gfpmask, HPAGE_PMD_ORDER); |
| 1215 | if (!thp) |
| 1216 | return NULL; |
| 1217 | prep_transhuge_page(thp); |
| 1218 | return thp; |
| 1219 | } |
| 1220 | |
| 1221 | return __alloc_pages_node(nid, gfp_mask, 0); |
| 1222 | } |
| 1223 | |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1224 | static long check_and_migrate_cma_pages(struct task_struct *tsk, |
| 1225 | struct mm_struct *mm, |
| 1226 | unsigned long start, |
| 1227 | unsigned long nr_pages, |
Aneesh Kumar K.V | 9a4e9f3 | 2019-03-05 15:47:44 -0800 | [diff] [blame] | 1228 | struct page **pages, |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1229 | struct vm_area_struct **vmas, |
| 1230 | unsigned int gup_flags) |
Aneesh Kumar K.V | 9a4e9f3 | 2019-03-05 15:47:44 -0800 | [diff] [blame] | 1231 | { |
| 1232 | long i; |
| 1233 | bool drain_allow = true; |
| 1234 | bool migrate_allow = true; |
| 1235 | LIST_HEAD(cma_page_list); |
| 1236 | |
| 1237 | check_again: |
| 1238 | for (i = 0; i < nr_pages; i++) { |
| 1239 | /* |
| 1240 | * If we get a page from the CMA zone, since we are going to |
| 1241 | * be pinning these entries, we might as well move them out |
| 1242 | * of the CMA zone if possible. |
| 1243 | */ |
| 1244 | if (is_migrate_cma_page(pages[i])) { |
| 1245 | |
| 1246 | struct page *head = compound_head(pages[i]); |
| 1247 | |
| 1248 | if (PageHuge(head)) { |
| 1249 | isolate_huge_page(head, &cma_page_list); |
| 1250 | } else { |
| 1251 | if (!PageLRU(head) && drain_allow) { |
| 1252 | lru_add_drain_all(); |
| 1253 | drain_allow = false; |
| 1254 | } |
| 1255 | |
| 1256 | if (!isolate_lru_page(head)) { |
| 1257 | list_add_tail(&head->lru, &cma_page_list); |
| 1258 | mod_node_page_state(page_pgdat(head), |
| 1259 | NR_ISOLATED_ANON + |
| 1260 | page_is_file_cache(head), |
| 1261 | hpage_nr_pages(head)); |
| 1262 | } |
| 1263 | } |
| 1264 | } |
| 1265 | } |
| 1266 | |
| 1267 | if (!list_empty(&cma_page_list)) { |
| 1268 | /* |
| 1269 | * drop the above get_user_pages reference. |
| 1270 | */ |
| 1271 | for (i = 0; i < nr_pages; i++) |
| 1272 | put_page(pages[i]); |
| 1273 | |
| 1274 | if (migrate_pages(&cma_page_list, new_non_cma_page, |
| 1275 | NULL, 0, MIGRATE_SYNC, MR_CONTIG_RANGE)) { |
| 1276 | /* |
| 1277 | * some of the pages failed migration. Do get_user_pages |
| 1278 | * without migration. |
| 1279 | */ |
| 1280 | migrate_allow = false; |
| 1281 | |
| 1282 | if (!list_empty(&cma_page_list)) |
| 1283 | putback_movable_pages(&cma_page_list); |
| 1284 | } |
| 1285 | /* |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1286 | * We did migrate all the pages, Try to get the page references |
| 1287 | * again migrating any new CMA pages which we failed to isolate |
| 1288 | * earlier. |
Aneesh Kumar K.V | 9a4e9f3 | 2019-03-05 15:47:44 -0800 | [diff] [blame] | 1289 | */ |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1290 | nr_pages = __get_user_pages_locked(tsk, mm, start, nr_pages, |
| 1291 | pages, vmas, NULL, |
| 1292 | gup_flags); |
| 1293 | |
Aneesh Kumar K.V | 9a4e9f3 | 2019-03-05 15:47:44 -0800 | [diff] [blame] | 1294 | if ((nr_pages > 0) && migrate_allow) { |
| 1295 | drain_allow = true; |
| 1296 | goto check_again; |
| 1297 | } |
| 1298 | } |
| 1299 | |
| 1300 | return nr_pages; |
| 1301 | } |
| 1302 | #else |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1303 | static long check_and_migrate_cma_pages(struct task_struct *tsk, |
| 1304 | struct mm_struct *mm, |
| 1305 | unsigned long start, |
| 1306 | unsigned long nr_pages, |
| 1307 | struct page **pages, |
| 1308 | struct vm_area_struct **vmas, |
| 1309 | unsigned int gup_flags) |
Aneesh Kumar K.V | 9a4e9f3 | 2019-03-05 15:47:44 -0800 | [diff] [blame] | 1310 | { |
| 1311 | return nr_pages; |
| 1312 | } |
| 1313 | #endif |
| 1314 | |
Dan Williams | 2bb6d28 | 2017-11-29 16:10:35 -0800 | [diff] [blame] | 1315 | /* |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1316 | * __gup_longterm_locked() is a wrapper for __get_user_pages_locked which |
| 1317 | * allows us to process the FOLL_LONGTERM flag. |
Dan Williams | 2bb6d28 | 2017-11-29 16:10:35 -0800 | [diff] [blame] | 1318 | */ |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1319 | static long __gup_longterm_locked(struct task_struct *tsk, |
| 1320 | struct mm_struct *mm, |
| 1321 | unsigned long start, |
| 1322 | unsigned long nr_pages, |
| 1323 | struct page **pages, |
| 1324 | struct vm_area_struct **vmas, |
| 1325 | unsigned int gup_flags) |
Dan Williams | 2bb6d28 | 2017-11-29 16:10:35 -0800 | [diff] [blame] | 1326 | { |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1327 | struct vm_area_struct **vmas_tmp = vmas; |
| 1328 | unsigned long flags = 0; |
Dan Williams | 2bb6d28 | 2017-11-29 16:10:35 -0800 | [diff] [blame] | 1329 | long rc, i; |
| 1330 | |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1331 | if (gup_flags & FOLL_LONGTERM) { |
| 1332 | if (!pages) |
| 1333 | return -EINVAL; |
Dan Williams | 2bb6d28 | 2017-11-29 16:10:35 -0800 | [diff] [blame] | 1334 | |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1335 | if (!vmas_tmp) { |
| 1336 | vmas_tmp = kcalloc(nr_pages, |
| 1337 | sizeof(struct vm_area_struct *), |
| 1338 | GFP_KERNEL); |
| 1339 | if (!vmas_tmp) |
| 1340 | return -ENOMEM; |
| 1341 | } |
| 1342 | flags = memalloc_nocma_save(); |
Dan Williams | 2bb6d28 | 2017-11-29 16:10:35 -0800 | [diff] [blame] | 1343 | } |
| 1344 | |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1345 | rc = __get_user_pages_locked(tsk, mm, start, nr_pages, pages, |
| 1346 | vmas_tmp, NULL, gup_flags); |
Dan Williams | 2bb6d28 | 2017-11-29 16:10:35 -0800 | [diff] [blame] | 1347 | |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1348 | if (gup_flags & FOLL_LONGTERM) { |
| 1349 | memalloc_nocma_restore(flags); |
| 1350 | if (rc < 0) |
| 1351 | goto out; |
| 1352 | |
| 1353 | if (check_dax_vmas(vmas_tmp, rc)) { |
| 1354 | for (i = 0; i < rc; i++) |
| 1355 | put_page(pages[i]); |
| 1356 | rc = -EOPNOTSUPP; |
| 1357 | goto out; |
| 1358 | } |
| 1359 | |
| 1360 | rc = check_and_migrate_cma_pages(tsk, mm, start, rc, pages, |
| 1361 | vmas_tmp, gup_flags); |
Aneesh Kumar K.V | 9a4e9f3 | 2019-03-05 15:47:44 -0800 | [diff] [blame] | 1362 | } |
| 1363 | |
Dan Williams | 2bb6d28 | 2017-11-29 16:10:35 -0800 | [diff] [blame] | 1364 | out: |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1365 | if (vmas_tmp != vmas) |
| 1366 | kfree(vmas_tmp); |
Dan Williams | 2bb6d28 | 2017-11-29 16:10:35 -0800 | [diff] [blame] | 1367 | return rc; |
| 1368 | } |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1369 | #else /* !CONFIG_FS_DAX && !CONFIG_CMA */ |
| 1370 | static __always_inline long __gup_longterm_locked(struct task_struct *tsk, |
| 1371 | struct mm_struct *mm, |
| 1372 | unsigned long start, |
| 1373 | unsigned long nr_pages, |
| 1374 | struct page **pages, |
| 1375 | struct vm_area_struct **vmas, |
| 1376 | unsigned int flags) |
| 1377 | { |
| 1378 | return __get_user_pages_locked(tsk, mm, start, nr_pages, pages, vmas, |
| 1379 | NULL, flags); |
| 1380 | } |
| 1381 | #endif /* CONFIG_FS_DAX || CONFIG_CMA */ |
| 1382 | |
| 1383 | /* |
| 1384 | * This is the same as get_user_pages_remote(), just with a |
| 1385 | * less-flexible calling convention where we assume that the task |
| 1386 | * and mm being operated on are the current task's and don't allow |
| 1387 | * passing of a locked parameter. We also obviously don't pass |
| 1388 | * FOLL_REMOTE in here. |
| 1389 | */ |
| 1390 | long get_user_pages(unsigned long start, unsigned long nr_pages, |
| 1391 | unsigned int gup_flags, struct page **pages, |
| 1392 | struct vm_area_struct **vmas) |
| 1393 | { |
| 1394 | return __gup_longterm_locked(current, current->mm, start, nr_pages, |
| 1395 | pages, vmas, gup_flags | FOLL_TOUCH); |
| 1396 | } |
| 1397 | EXPORT_SYMBOL(get_user_pages); |
Dan Williams | 2bb6d28 | 2017-11-29 16:10:35 -0800 | [diff] [blame] | 1398 | |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 1399 | /** |
Kirill A. Shutemov | acc3c8d | 2015-04-14 15:44:45 -0700 | [diff] [blame] | 1400 | * populate_vma_page_range() - populate a range of pages in the vma. |
| 1401 | * @vma: target vma |
| 1402 | * @start: start address |
| 1403 | * @end: end address |
| 1404 | * @nonblocking: |
| 1405 | * |
| 1406 | * This takes care of mlocking the pages too if VM_LOCKED is set. |
| 1407 | * |
| 1408 | * return 0 on success, negative error code on error. |
| 1409 | * |
| 1410 | * vma->vm_mm->mmap_sem must be held. |
| 1411 | * |
| 1412 | * If @nonblocking is NULL, it may be held for read or write and will |
| 1413 | * be unperturbed. |
| 1414 | * |
| 1415 | * If @nonblocking is non-NULL, it must held for read only and may be |
| 1416 | * released. If it's released, *@nonblocking will be set to 0. |
| 1417 | */ |
| 1418 | long populate_vma_page_range(struct vm_area_struct *vma, |
| 1419 | unsigned long start, unsigned long end, int *nonblocking) |
| 1420 | { |
| 1421 | struct mm_struct *mm = vma->vm_mm; |
| 1422 | unsigned long nr_pages = (end - start) / PAGE_SIZE; |
| 1423 | int gup_flags; |
| 1424 | |
| 1425 | VM_BUG_ON(start & ~PAGE_MASK); |
| 1426 | VM_BUG_ON(end & ~PAGE_MASK); |
| 1427 | VM_BUG_ON_VMA(start < vma->vm_start, vma); |
| 1428 | VM_BUG_ON_VMA(end > vma->vm_end, vma); |
| 1429 | VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_sem), mm); |
| 1430 | |
Eric B Munson | de60f5f | 2015-11-05 18:51:36 -0800 | [diff] [blame] | 1431 | gup_flags = FOLL_TOUCH | FOLL_POPULATE | FOLL_MLOCK; |
| 1432 | if (vma->vm_flags & VM_LOCKONFAULT) |
| 1433 | gup_flags &= ~FOLL_POPULATE; |
Kirill A. Shutemov | acc3c8d | 2015-04-14 15:44:45 -0700 | [diff] [blame] | 1434 | /* |
| 1435 | * We want to touch writable mappings with a write fault in order |
| 1436 | * to break COW, except for shared mappings because these don't COW |
| 1437 | * and we would not want to dirty them for nothing. |
| 1438 | */ |
| 1439 | if ((vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE) |
| 1440 | gup_flags |= FOLL_WRITE; |
| 1441 | |
| 1442 | /* |
| 1443 | * We want mlock to succeed for regions that have any permissions |
| 1444 | * other than PROT_NONE. |
| 1445 | */ |
| 1446 | if (vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)) |
| 1447 | gup_flags |= FOLL_FORCE; |
| 1448 | |
| 1449 | /* |
| 1450 | * We made sure addr is within a VMA, so the following will |
| 1451 | * not result in a stack expansion that recurses back here. |
| 1452 | */ |
| 1453 | return __get_user_pages(current, mm, start, nr_pages, gup_flags, |
| 1454 | NULL, NULL, nonblocking); |
| 1455 | } |
| 1456 | |
| 1457 | /* |
| 1458 | * __mm_populate - populate and/or mlock pages within a range of address space. |
| 1459 | * |
| 1460 | * This is used to implement mlock() and the MAP_POPULATE / MAP_LOCKED mmap |
| 1461 | * flags. VMAs must be already marked with the desired vm_flags, and |
| 1462 | * mmap_sem must not be held. |
| 1463 | */ |
| 1464 | int __mm_populate(unsigned long start, unsigned long len, int ignore_errors) |
| 1465 | { |
| 1466 | struct mm_struct *mm = current->mm; |
| 1467 | unsigned long end, nstart, nend; |
| 1468 | struct vm_area_struct *vma = NULL; |
| 1469 | int locked = 0; |
| 1470 | long ret = 0; |
| 1471 | |
Kirill A. Shutemov | acc3c8d | 2015-04-14 15:44:45 -0700 | [diff] [blame] | 1472 | end = start + len; |
| 1473 | |
| 1474 | for (nstart = start; nstart < end; nstart = nend) { |
| 1475 | /* |
| 1476 | * We want to fault in pages for [nstart; end) address range. |
| 1477 | * Find first corresponding VMA. |
| 1478 | */ |
| 1479 | if (!locked) { |
| 1480 | locked = 1; |
| 1481 | down_read(&mm->mmap_sem); |
| 1482 | vma = find_vma(mm, nstart); |
| 1483 | } else if (nstart >= vma->vm_end) |
| 1484 | vma = vma->vm_next; |
| 1485 | if (!vma || vma->vm_start >= end) |
| 1486 | break; |
| 1487 | /* |
| 1488 | * Set [nstart; nend) to intersection of desired address |
| 1489 | * range with the first VMA. Also, skip undesirable VMA types. |
| 1490 | */ |
| 1491 | nend = min(end, vma->vm_end); |
| 1492 | if (vma->vm_flags & (VM_IO | VM_PFNMAP)) |
| 1493 | continue; |
| 1494 | if (nstart < vma->vm_start) |
| 1495 | nstart = vma->vm_start; |
| 1496 | /* |
| 1497 | * Now fault in a range of pages. populate_vma_page_range() |
| 1498 | * double checks the vma flags, so that it won't mlock pages |
| 1499 | * if the vma was already munlocked. |
| 1500 | */ |
| 1501 | ret = populate_vma_page_range(vma, nstart, nend, &locked); |
| 1502 | if (ret < 0) { |
| 1503 | if (ignore_errors) { |
| 1504 | ret = 0; |
| 1505 | continue; /* continue at next VMA */ |
| 1506 | } |
| 1507 | break; |
| 1508 | } |
| 1509 | nend = nstart + ret * PAGE_SIZE; |
| 1510 | ret = 0; |
| 1511 | } |
| 1512 | if (locked) |
| 1513 | up_read(&mm->mmap_sem); |
| 1514 | return ret; /* 0 or negative error code */ |
| 1515 | } |
| 1516 | |
| 1517 | /** |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 1518 | * get_dump_page() - pin user page in memory while writing it to core dump |
| 1519 | * @addr: user address |
| 1520 | * |
| 1521 | * Returns struct page pointer of user page pinned for dump, |
Kirill A. Shutemov | ea1754a | 2016-04-01 15:29:48 +0300 | [diff] [blame] | 1522 | * to be freed afterwards by put_page(). |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 1523 | * |
| 1524 | * Returns NULL on any kind of failure - a hole must then be inserted into |
| 1525 | * the corefile, to preserve alignment with its headers; and also returns |
| 1526 | * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found - |
| 1527 | * allowing a hole to be left in the corefile to save diskspace. |
| 1528 | * |
| 1529 | * Called without mmap_sem, but after all other threads have been killed. |
| 1530 | */ |
| 1531 | #ifdef CONFIG_ELF_CORE |
| 1532 | struct page *get_dump_page(unsigned long addr) |
| 1533 | { |
| 1534 | struct vm_area_struct *vma; |
| 1535 | struct page *page; |
| 1536 | |
| 1537 | if (__get_user_pages(current, current->mm, addr, 1, |
| 1538 | FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma, |
| 1539 | NULL) < 1) |
| 1540 | return NULL; |
| 1541 | flush_cache_page(vma, addr, page_to_pfn(page)); |
| 1542 | return page; |
| 1543 | } |
| 1544 | #endif /* CONFIG_ELF_CORE */ |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1545 | |
| 1546 | /* |
Kirill A. Shutemov | e585513 | 2017-06-06 14:31:20 +0300 | [diff] [blame] | 1547 | * Generic Fast GUP |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1548 | * |
| 1549 | * get_user_pages_fast attempts to pin user pages by walking the page |
| 1550 | * tables directly and avoids taking locks. Thus the walker needs to be |
| 1551 | * protected from page table pages being freed from under it, and should |
| 1552 | * block any THP splits. |
| 1553 | * |
| 1554 | * One way to achieve this is to have the walker disable interrupts, and |
| 1555 | * rely on IPIs from the TLB flushing code blocking before the page table |
| 1556 | * pages are freed. This is unsuitable for architectures that do not need |
| 1557 | * to broadcast an IPI when invalidating TLBs. |
| 1558 | * |
| 1559 | * Another way to achieve this is to batch up page table containing pages |
| 1560 | * belonging to more than one mm_user, then rcu_sched a callback to free those |
| 1561 | * pages. Disabling interrupts will allow the fast_gup walker to both block |
| 1562 | * the rcu_sched callback, and an IPI that we broadcast for splitting THPs |
| 1563 | * (which is a relatively rare event). The code below adopts this strategy. |
| 1564 | * |
| 1565 | * Before activating this code, please be aware that the following assumptions |
| 1566 | * are currently made: |
| 1567 | * |
Kirill A. Shutemov | e585513 | 2017-06-06 14:31:20 +0300 | [diff] [blame] | 1568 | * *) Either HAVE_RCU_TABLE_FREE is enabled, and tlb_remove_table() is used to |
| 1569 | * free pages containing page tables or TLB flushing requires IPI broadcast. |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1570 | * |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1571 | * *) ptes can be read atomically by the architecture. |
| 1572 | * |
| 1573 | * *) access_ok is sufficient to validate userspace address ranges. |
| 1574 | * |
| 1575 | * The last two assumptions can be relaxed by the addition of helper functions. |
| 1576 | * |
| 1577 | * This code is based heavily on the PowerPC implementation by Nick Piggin. |
| 1578 | */ |
Kirill A. Shutemov | e585513 | 2017-06-06 14:31:20 +0300 | [diff] [blame] | 1579 | #ifdef CONFIG_HAVE_GENERIC_GUP |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1580 | |
Kirill A. Shutemov | 0005d20 | 2017-03-16 18:26:51 +0300 | [diff] [blame] | 1581 | #ifndef gup_get_pte |
| 1582 | /* |
| 1583 | * We assume that the PTE can be read atomically. If this is not the case for |
| 1584 | * your architecture, please provide the helper. |
| 1585 | */ |
| 1586 | static inline pte_t gup_get_pte(pte_t *ptep) |
| 1587 | { |
| 1588 | return READ_ONCE(*ptep); |
| 1589 | } |
| 1590 | #endif |
| 1591 | |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1592 | static void undo_dev_pagemap(int *nr, int nr_start, struct page **pages) |
| 1593 | { |
| 1594 | while ((*nr) - nr_start) { |
| 1595 | struct page *page = pages[--(*nr)]; |
| 1596 | |
| 1597 | ClearPageReferenced(page); |
| 1598 | put_page(page); |
| 1599 | } |
| 1600 | } |
| 1601 | |
Linus Torvalds | 8fde12c | 2019-04-11 10:49:19 -0700 | [diff] [blame] | 1602 | /* |
| 1603 | * Return the compund head page with ref appropriately incremented, |
| 1604 | * or NULL if that failed. |
| 1605 | */ |
| 1606 | static inline struct page *try_get_compound_head(struct page *page, int refs) |
| 1607 | { |
| 1608 | struct page *head = compound_head(page); |
| 1609 | if (WARN_ON_ONCE(page_ref_count(head) < 0)) |
| 1610 | return NULL; |
| 1611 | if (unlikely(!page_cache_add_speculative(head, refs))) |
| 1612 | return NULL; |
| 1613 | return head; |
| 1614 | } |
| 1615 | |
Laurent Dufour | 3010a5e | 2018-06-07 17:06:08 -0700 | [diff] [blame] | 1616 | #ifdef CONFIG_ARCH_HAS_PTE_SPECIAL |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1617 | static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end, |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame^] | 1618 | unsigned int flags, struct page **pages, int *nr) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1619 | { |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1620 | struct dev_pagemap *pgmap = NULL; |
| 1621 | int nr_start = *nr, ret = 0; |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1622 | pte_t *ptep, *ptem; |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1623 | |
| 1624 | ptem = ptep = pte_offset_map(&pmd, addr); |
| 1625 | do { |
Kirill A. Shutemov | 0005d20 | 2017-03-16 18:26:51 +0300 | [diff] [blame] | 1626 | pte_t pte = gup_get_pte(ptep); |
Kirill A. Shutemov | 7aef417 | 2016-01-15 16:52:32 -0800 | [diff] [blame] | 1627 | struct page *head, *page; |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1628 | |
| 1629 | /* |
| 1630 | * Similar to the PMD case below, NUMA hinting must take slow |
Mel Gorman | 8a0516e | 2015-02-12 14:58:22 -0800 | [diff] [blame] | 1631 | * path using the pte_protnone check. |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1632 | */ |
Kirill A. Shutemov | e7884f8 | 2017-03-16 18:26:50 +0300 | [diff] [blame] | 1633 | if (pte_protnone(pte)) |
| 1634 | goto pte_unmap; |
| 1635 | |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame^] | 1636 | if (!pte_access_permitted(pte, flags & FOLL_WRITE)) |
Kirill A. Shutemov | e7884f8 | 2017-03-16 18:26:50 +0300 | [diff] [blame] | 1637 | goto pte_unmap; |
| 1638 | |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1639 | if (pte_devmap(pte)) { |
| 1640 | pgmap = get_dev_pagemap(pte_pfn(pte), pgmap); |
| 1641 | if (unlikely(!pgmap)) { |
| 1642 | undo_dev_pagemap(nr, nr_start, pages); |
| 1643 | goto pte_unmap; |
| 1644 | } |
| 1645 | } else if (pte_special(pte)) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1646 | goto pte_unmap; |
| 1647 | |
| 1648 | VM_BUG_ON(!pfn_valid(pte_pfn(pte))); |
| 1649 | page = pte_page(pte); |
| 1650 | |
Linus Torvalds | 8fde12c | 2019-04-11 10:49:19 -0700 | [diff] [blame] | 1651 | head = try_get_compound_head(page, 1); |
| 1652 | if (!head) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1653 | goto pte_unmap; |
| 1654 | |
| 1655 | if (unlikely(pte_val(pte) != pte_val(*ptep))) { |
Kirill A. Shutemov | 7aef417 | 2016-01-15 16:52:32 -0800 | [diff] [blame] | 1656 | put_page(head); |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1657 | goto pte_unmap; |
| 1658 | } |
| 1659 | |
Kirill A. Shutemov | 7aef417 | 2016-01-15 16:52:32 -0800 | [diff] [blame] | 1660 | VM_BUG_ON_PAGE(compound_head(page) != head, page); |
Kirill A. Shutemov | e934805 | 2017-03-16 18:26:52 +0300 | [diff] [blame] | 1661 | |
| 1662 | SetPageReferenced(page); |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1663 | pages[*nr] = page; |
| 1664 | (*nr)++; |
| 1665 | |
| 1666 | } while (ptep++, addr += PAGE_SIZE, addr != end); |
| 1667 | |
| 1668 | ret = 1; |
| 1669 | |
| 1670 | pte_unmap: |
Christoph Hellwig | 832d7aa | 2017-12-29 08:54:01 +0100 | [diff] [blame] | 1671 | if (pgmap) |
| 1672 | put_dev_pagemap(pgmap); |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1673 | pte_unmap(ptem); |
| 1674 | return ret; |
| 1675 | } |
| 1676 | #else |
| 1677 | |
| 1678 | /* |
| 1679 | * If we can't determine whether or not a pte is special, then fail immediately |
| 1680 | * for ptes. Note, we can still pin HugeTLB and THP as these are guaranteed not |
| 1681 | * to be special. |
| 1682 | * |
| 1683 | * For a futex to be placed on a THP tail page, get_futex_key requires a |
| 1684 | * __get_user_pages_fast implementation that can pin pages. Thus it's still |
| 1685 | * useful to have gup_huge_pmd even if we can't operate on ptes. |
| 1686 | */ |
| 1687 | static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end, |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame^] | 1688 | unsigned int flags, struct page **pages, int *nr) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1689 | { |
| 1690 | return 0; |
| 1691 | } |
Laurent Dufour | 3010a5e | 2018-06-07 17:06:08 -0700 | [diff] [blame] | 1692 | #endif /* CONFIG_ARCH_HAS_PTE_SPECIAL */ |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1693 | |
Oliver O'Halloran | 09180ca | 2017-09-06 16:20:58 -0700 | [diff] [blame] | 1694 | #if defined(__HAVE_ARCH_PTE_DEVMAP) && defined(CONFIG_TRANSPARENT_HUGEPAGE) |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1695 | static int __gup_device_huge(unsigned long pfn, unsigned long addr, |
| 1696 | unsigned long end, struct page **pages, int *nr) |
| 1697 | { |
| 1698 | int nr_start = *nr; |
| 1699 | struct dev_pagemap *pgmap = NULL; |
| 1700 | |
| 1701 | do { |
| 1702 | struct page *page = pfn_to_page(pfn); |
| 1703 | |
| 1704 | pgmap = get_dev_pagemap(pfn, pgmap); |
| 1705 | if (unlikely(!pgmap)) { |
| 1706 | undo_dev_pagemap(nr, nr_start, pages); |
| 1707 | return 0; |
| 1708 | } |
| 1709 | SetPageReferenced(page); |
| 1710 | pages[*nr] = page; |
| 1711 | get_page(page); |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1712 | (*nr)++; |
| 1713 | pfn++; |
| 1714 | } while (addr += PAGE_SIZE, addr != end); |
Christoph Hellwig | 832d7aa | 2017-12-29 08:54:01 +0100 | [diff] [blame] | 1715 | |
| 1716 | if (pgmap) |
| 1717 | put_dev_pagemap(pgmap); |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1718 | return 1; |
| 1719 | } |
| 1720 | |
Dan Williams | a9b6de7 | 2018-04-19 21:32:19 -0700 | [diff] [blame] | 1721 | static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr, |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1722 | unsigned long end, struct page **pages, int *nr) |
| 1723 | { |
| 1724 | unsigned long fault_pfn; |
Dan Williams | a9b6de7 | 2018-04-19 21:32:19 -0700 | [diff] [blame] | 1725 | int nr_start = *nr; |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1726 | |
Dan Williams | a9b6de7 | 2018-04-19 21:32:19 -0700 | [diff] [blame] | 1727 | fault_pfn = pmd_pfn(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT); |
| 1728 | if (!__gup_device_huge(fault_pfn, addr, end, pages, nr)) |
| 1729 | return 0; |
| 1730 | |
| 1731 | if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) { |
| 1732 | undo_dev_pagemap(nr, nr_start, pages); |
| 1733 | return 0; |
| 1734 | } |
| 1735 | return 1; |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1736 | } |
| 1737 | |
Dan Williams | a9b6de7 | 2018-04-19 21:32:19 -0700 | [diff] [blame] | 1738 | static int __gup_device_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr, |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1739 | unsigned long end, struct page **pages, int *nr) |
| 1740 | { |
| 1741 | unsigned long fault_pfn; |
Dan Williams | a9b6de7 | 2018-04-19 21:32:19 -0700 | [diff] [blame] | 1742 | int nr_start = *nr; |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1743 | |
Dan Williams | a9b6de7 | 2018-04-19 21:32:19 -0700 | [diff] [blame] | 1744 | fault_pfn = pud_pfn(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT); |
| 1745 | if (!__gup_device_huge(fault_pfn, addr, end, pages, nr)) |
| 1746 | return 0; |
| 1747 | |
| 1748 | if (unlikely(pud_val(orig) != pud_val(*pudp))) { |
| 1749 | undo_dev_pagemap(nr, nr_start, pages); |
| 1750 | return 0; |
| 1751 | } |
| 1752 | return 1; |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1753 | } |
| 1754 | #else |
Dan Williams | a9b6de7 | 2018-04-19 21:32:19 -0700 | [diff] [blame] | 1755 | static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr, |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1756 | unsigned long end, struct page **pages, int *nr) |
| 1757 | { |
| 1758 | BUILD_BUG(); |
| 1759 | return 0; |
| 1760 | } |
| 1761 | |
Dan Williams | a9b6de7 | 2018-04-19 21:32:19 -0700 | [diff] [blame] | 1762 | static int __gup_device_huge_pud(pud_t pud, pud_t *pudp, unsigned long addr, |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1763 | unsigned long end, struct page **pages, int *nr) |
| 1764 | { |
| 1765 | BUILD_BUG(); |
| 1766 | return 0; |
| 1767 | } |
| 1768 | #endif |
| 1769 | |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1770 | static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr, |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame^] | 1771 | unsigned long end, unsigned int flags, struct page **pages, int *nr) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1772 | { |
Kirill A. Shutemov | ddc58f2 | 2016-01-15 16:52:56 -0800 | [diff] [blame] | 1773 | struct page *head, *page; |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1774 | int refs; |
| 1775 | |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame^] | 1776 | if (!pmd_access_permitted(orig, flags & FOLL_WRITE)) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1777 | return 0; |
| 1778 | |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1779 | if (pmd_devmap(orig)) |
Dan Williams | a9b6de7 | 2018-04-19 21:32:19 -0700 | [diff] [blame] | 1780 | return __gup_device_huge_pmd(orig, pmdp, addr, end, pages, nr); |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1781 | |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1782 | refs = 0; |
Punit Agrawal | d63206e | 2017-07-06 15:39:39 -0700 | [diff] [blame] | 1783 | page = pmd_page(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT); |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1784 | do { |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1785 | pages[*nr] = page; |
| 1786 | (*nr)++; |
| 1787 | page++; |
| 1788 | refs++; |
| 1789 | } while (addr += PAGE_SIZE, addr != end); |
| 1790 | |
Linus Torvalds | 8fde12c | 2019-04-11 10:49:19 -0700 | [diff] [blame] | 1791 | head = try_get_compound_head(pmd_page(orig), refs); |
| 1792 | if (!head) { |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1793 | *nr -= refs; |
| 1794 | return 0; |
| 1795 | } |
| 1796 | |
| 1797 | if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) { |
| 1798 | *nr -= refs; |
| 1799 | while (refs--) |
| 1800 | put_page(head); |
| 1801 | return 0; |
| 1802 | } |
| 1803 | |
Kirill A. Shutemov | e934805 | 2017-03-16 18:26:52 +0300 | [diff] [blame] | 1804 | SetPageReferenced(head); |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1805 | return 1; |
| 1806 | } |
| 1807 | |
| 1808 | static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr, |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame^] | 1809 | unsigned long end, unsigned int flags, struct page **pages, int *nr) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1810 | { |
Kirill A. Shutemov | ddc58f2 | 2016-01-15 16:52:56 -0800 | [diff] [blame] | 1811 | struct page *head, *page; |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1812 | int refs; |
| 1813 | |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame^] | 1814 | if (!pud_access_permitted(orig, flags & FOLL_WRITE)) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1815 | return 0; |
| 1816 | |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1817 | if (pud_devmap(orig)) |
Dan Williams | a9b6de7 | 2018-04-19 21:32:19 -0700 | [diff] [blame] | 1818 | return __gup_device_huge_pud(orig, pudp, addr, end, pages, nr); |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1819 | |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1820 | refs = 0; |
Punit Agrawal | d63206e | 2017-07-06 15:39:39 -0700 | [diff] [blame] | 1821 | page = pud_page(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT); |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1822 | do { |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1823 | pages[*nr] = page; |
| 1824 | (*nr)++; |
| 1825 | page++; |
| 1826 | refs++; |
| 1827 | } while (addr += PAGE_SIZE, addr != end); |
| 1828 | |
Linus Torvalds | 8fde12c | 2019-04-11 10:49:19 -0700 | [diff] [blame] | 1829 | head = try_get_compound_head(pud_page(orig), refs); |
| 1830 | if (!head) { |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1831 | *nr -= refs; |
| 1832 | return 0; |
| 1833 | } |
| 1834 | |
| 1835 | if (unlikely(pud_val(orig) != pud_val(*pudp))) { |
| 1836 | *nr -= refs; |
| 1837 | while (refs--) |
| 1838 | put_page(head); |
| 1839 | return 0; |
| 1840 | } |
| 1841 | |
Kirill A. Shutemov | e934805 | 2017-03-16 18:26:52 +0300 | [diff] [blame] | 1842 | SetPageReferenced(head); |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1843 | return 1; |
| 1844 | } |
| 1845 | |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 1846 | static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsigned long addr, |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame^] | 1847 | unsigned long end, unsigned int flags, |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 1848 | struct page **pages, int *nr) |
| 1849 | { |
| 1850 | int refs; |
Kirill A. Shutemov | ddc58f2 | 2016-01-15 16:52:56 -0800 | [diff] [blame] | 1851 | struct page *head, *page; |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 1852 | |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame^] | 1853 | if (!pgd_access_permitted(orig, flags & FOLL_WRITE)) |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 1854 | return 0; |
| 1855 | |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1856 | BUILD_BUG_ON(pgd_devmap(orig)); |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 1857 | refs = 0; |
Punit Agrawal | d63206e | 2017-07-06 15:39:39 -0700 | [diff] [blame] | 1858 | page = pgd_page(orig) + ((addr & ~PGDIR_MASK) >> PAGE_SHIFT); |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 1859 | do { |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 1860 | pages[*nr] = page; |
| 1861 | (*nr)++; |
| 1862 | page++; |
| 1863 | refs++; |
| 1864 | } while (addr += PAGE_SIZE, addr != end); |
| 1865 | |
Linus Torvalds | 8fde12c | 2019-04-11 10:49:19 -0700 | [diff] [blame] | 1866 | head = try_get_compound_head(pgd_page(orig), refs); |
| 1867 | if (!head) { |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 1868 | *nr -= refs; |
| 1869 | return 0; |
| 1870 | } |
| 1871 | |
| 1872 | if (unlikely(pgd_val(orig) != pgd_val(*pgdp))) { |
| 1873 | *nr -= refs; |
| 1874 | while (refs--) |
| 1875 | put_page(head); |
| 1876 | return 0; |
| 1877 | } |
| 1878 | |
Kirill A. Shutemov | e934805 | 2017-03-16 18:26:52 +0300 | [diff] [blame] | 1879 | SetPageReferenced(head); |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 1880 | return 1; |
| 1881 | } |
| 1882 | |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1883 | static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end, |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame^] | 1884 | unsigned int flags, struct page **pages, int *nr) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1885 | { |
| 1886 | unsigned long next; |
| 1887 | pmd_t *pmdp; |
| 1888 | |
| 1889 | pmdp = pmd_offset(&pud, addr); |
| 1890 | do { |
Christian Borntraeger | 38c5ce9 | 2015-01-06 22:54:46 +0100 | [diff] [blame] | 1891 | pmd_t pmd = READ_ONCE(*pmdp); |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1892 | |
| 1893 | next = pmd_addr_end(addr, end); |
Zi Yan | 84c3fc4 | 2017-09-08 16:11:01 -0700 | [diff] [blame] | 1894 | if (!pmd_present(pmd)) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1895 | return 0; |
| 1896 | |
Yu Zhao | 414fd08 | 2019-02-12 15:35:58 -0800 | [diff] [blame] | 1897 | if (unlikely(pmd_trans_huge(pmd) || pmd_huge(pmd) || |
| 1898 | pmd_devmap(pmd))) { |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1899 | /* |
| 1900 | * NUMA hinting faults need to be handled in the GUP |
| 1901 | * slowpath for accounting purposes and so that they |
| 1902 | * can be serialised against THP migration. |
| 1903 | */ |
Mel Gorman | 8a0516e | 2015-02-12 14:58:22 -0800 | [diff] [blame] | 1904 | if (pmd_protnone(pmd)) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1905 | return 0; |
| 1906 | |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame^] | 1907 | if (!gup_huge_pmd(pmd, pmdp, addr, next, flags, |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1908 | pages, nr)) |
| 1909 | return 0; |
| 1910 | |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 1911 | } else if (unlikely(is_hugepd(__hugepd(pmd_val(pmd))))) { |
| 1912 | /* |
| 1913 | * architecture have different format for hugetlbfs |
| 1914 | * pmd format and THP pmd format |
| 1915 | */ |
| 1916 | if (!gup_huge_pd(__hugepd(pmd_val(pmd)), addr, |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame^] | 1917 | PMD_SHIFT, next, flags, pages, nr)) |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 1918 | return 0; |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame^] | 1919 | } else if (!gup_pte_range(pmd, addr, next, flags, pages, nr)) |
Mario Leinweber | 2923117 | 2018-04-05 16:24:18 -0700 | [diff] [blame] | 1920 | return 0; |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1921 | } while (pmdp++, addr = next, addr != end); |
| 1922 | |
| 1923 | return 1; |
| 1924 | } |
| 1925 | |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 1926 | static int gup_pud_range(p4d_t p4d, unsigned long addr, unsigned long end, |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame^] | 1927 | unsigned int flags, struct page **pages, int *nr) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1928 | { |
| 1929 | unsigned long next; |
| 1930 | pud_t *pudp; |
| 1931 | |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 1932 | pudp = pud_offset(&p4d, addr); |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1933 | do { |
Christian Borntraeger | e37c698 | 2014-12-07 21:41:33 +0100 | [diff] [blame] | 1934 | pud_t pud = READ_ONCE(*pudp); |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1935 | |
| 1936 | next = pud_addr_end(addr, end); |
| 1937 | if (pud_none(pud)) |
| 1938 | return 0; |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 1939 | if (unlikely(pud_huge(pud))) { |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame^] | 1940 | if (!gup_huge_pud(pud, pudp, addr, next, flags, |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 1941 | pages, nr)) |
| 1942 | return 0; |
| 1943 | } else if (unlikely(is_hugepd(__hugepd(pud_val(pud))))) { |
| 1944 | if (!gup_huge_pd(__hugepd(pud_val(pud)), addr, |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame^] | 1945 | PUD_SHIFT, next, flags, pages, nr)) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1946 | return 0; |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame^] | 1947 | } else if (!gup_pmd_range(pud, addr, next, flags, pages, nr)) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1948 | return 0; |
| 1949 | } while (pudp++, addr = next, addr != end); |
| 1950 | |
| 1951 | return 1; |
| 1952 | } |
| 1953 | |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 1954 | static int gup_p4d_range(pgd_t pgd, unsigned long addr, unsigned long end, |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame^] | 1955 | unsigned int flags, struct page **pages, int *nr) |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 1956 | { |
| 1957 | unsigned long next; |
| 1958 | p4d_t *p4dp; |
| 1959 | |
| 1960 | p4dp = p4d_offset(&pgd, addr); |
| 1961 | do { |
| 1962 | p4d_t p4d = READ_ONCE(*p4dp); |
| 1963 | |
| 1964 | next = p4d_addr_end(addr, end); |
| 1965 | if (p4d_none(p4d)) |
| 1966 | return 0; |
| 1967 | BUILD_BUG_ON(p4d_huge(p4d)); |
| 1968 | if (unlikely(is_hugepd(__hugepd(p4d_val(p4d))))) { |
| 1969 | if (!gup_huge_pd(__hugepd(p4d_val(p4d)), addr, |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame^] | 1970 | P4D_SHIFT, next, flags, pages, nr)) |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 1971 | return 0; |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame^] | 1972 | } else if (!gup_pud_range(p4d, addr, next, flags, pages, nr)) |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 1973 | return 0; |
| 1974 | } while (p4dp++, addr = next, addr != end); |
| 1975 | |
| 1976 | return 1; |
| 1977 | } |
| 1978 | |
Kirill A. Shutemov | 5b65c467 | 2017-09-09 00:56:03 +0300 | [diff] [blame] | 1979 | static void gup_pgd_range(unsigned long addr, unsigned long end, |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame^] | 1980 | unsigned int flags, struct page **pages, int *nr) |
Kirill A. Shutemov | 5b65c467 | 2017-09-09 00:56:03 +0300 | [diff] [blame] | 1981 | { |
| 1982 | unsigned long next; |
| 1983 | pgd_t *pgdp; |
| 1984 | |
| 1985 | pgdp = pgd_offset(current->mm, addr); |
| 1986 | do { |
| 1987 | pgd_t pgd = READ_ONCE(*pgdp); |
| 1988 | |
| 1989 | next = pgd_addr_end(addr, end); |
| 1990 | if (pgd_none(pgd)) |
| 1991 | return; |
| 1992 | if (unlikely(pgd_huge(pgd))) { |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame^] | 1993 | if (!gup_huge_pgd(pgd, pgdp, addr, next, flags, |
Kirill A. Shutemov | 5b65c467 | 2017-09-09 00:56:03 +0300 | [diff] [blame] | 1994 | pages, nr)) |
| 1995 | return; |
| 1996 | } else if (unlikely(is_hugepd(__hugepd(pgd_val(pgd))))) { |
| 1997 | if (!gup_huge_pd(__hugepd(pgd_val(pgd)), addr, |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame^] | 1998 | PGDIR_SHIFT, next, flags, pages, nr)) |
Kirill A. Shutemov | 5b65c467 | 2017-09-09 00:56:03 +0300 | [diff] [blame] | 1999 | return; |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame^] | 2000 | } else if (!gup_p4d_range(pgd, addr, next, flags, pages, nr)) |
Kirill A. Shutemov | 5b65c467 | 2017-09-09 00:56:03 +0300 | [diff] [blame] | 2001 | return; |
| 2002 | } while (pgdp++, addr = next, addr != end); |
| 2003 | } |
| 2004 | |
| 2005 | #ifndef gup_fast_permitted |
| 2006 | /* |
| 2007 | * Check if it's allowed to use __get_user_pages_fast() for the range, or |
| 2008 | * we need to fall back to the slow version: |
| 2009 | */ |
Ira Weiny | ad8cfb9 | 2019-02-10 14:34:24 -0800 | [diff] [blame] | 2010 | bool gup_fast_permitted(unsigned long start, int nr_pages) |
Kirill A. Shutemov | 5b65c467 | 2017-09-09 00:56:03 +0300 | [diff] [blame] | 2011 | { |
| 2012 | unsigned long len, end; |
| 2013 | |
| 2014 | len = (unsigned long) nr_pages << PAGE_SHIFT; |
| 2015 | end = start + len; |
| 2016 | return end >= start; |
| 2017 | } |
| 2018 | #endif |
| 2019 | |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2020 | /* |
| 2021 | * Like get_user_pages_fast() except it's IRQ-safe in that it won't fall back to |
Michael S. Tsirkin | d081107 | 2018-04-13 15:35:23 -0700 | [diff] [blame] | 2022 | * the regular GUP. |
| 2023 | * Note a difference with get_user_pages_fast: this always returns the |
| 2024 | * number of pages pinned, 0 if no pages were pinned. |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2025 | */ |
| 2026 | int __get_user_pages_fast(unsigned long start, int nr_pages, int write, |
| 2027 | struct page **pages) |
| 2028 | { |
Wei Yang | d4faa40 | 2018-10-26 15:07:55 -0700 | [diff] [blame] | 2029 | unsigned long len, end; |
Kirill A. Shutemov | 5b65c467 | 2017-09-09 00:56:03 +0300 | [diff] [blame] | 2030 | unsigned long flags; |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2031 | int nr = 0; |
| 2032 | |
| 2033 | start &= PAGE_MASK; |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2034 | len = (unsigned long) nr_pages << PAGE_SHIFT; |
| 2035 | end = start + len; |
| 2036 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 2037 | if (unlikely(!access_ok((void __user *)start, len))) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2038 | return 0; |
| 2039 | |
| 2040 | /* |
| 2041 | * Disable interrupts. We use the nested form as we can already have |
| 2042 | * interrupts disabled by get_futex_key. |
| 2043 | * |
| 2044 | * With interrupts disabled, we block page table pages from being |
Fengguang Wu | 2ebe822 | 2018-10-30 15:10:51 -0700 | [diff] [blame] | 2045 | * freed from under us. See struct mmu_table_batch comments in |
| 2046 | * include/asm-generic/tlb.h for more details. |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2047 | * |
| 2048 | * We do not adopt an rcu_read_lock(.) here as we also want to |
| 2049 | * block IPIs that come from THPs splitting. |
| 2050 | */ |
| 2051 | |
Ira Weiny | ad8cfb9 | 2019-02-10 14:34:24 -0800 | [diff] [blame] | 2052 | if (gup_fast_permitted(start, nr_pages)) { |
Kirill A. Shutemov | 5b65c467 | 2017-09-09 00:56:03 +0300 | [diff] [blame] | 2053 | local_irq_save(flags); |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame^] | 2054 | gup_pgd_range(start, end, write ? FOLL_WRITE : 0, pages, &nr); |
Kirill A. Shutemov | 5b65c467 | 2017-09-09 00:56:03 +0300 | [diff] [blame] | 2055 | local_irq_restore(flags); |
| 2056 | } |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2057 | |
| 2058 | return nr; |
| 2059 | } |
| 2060 | |
| 2061 | /** |
| 2062 | * get_user_pages_fast() - pin user pages in memory |
| 2063 | * @start: starting user address |
| 2064 | * @nr_pages: number of pages from start to pin |
| 2065 | * @write: whether pages will be written to |
| 2066 | * @pages: array that receives pointers to the pages pinned. |
| 2067 | * Should be at least nr_pages long. |
| 2068 | * |
| 2069 | * Attempt to pin user pages in memory without taking mm->mmap_sem. |
| 2070 | * If not successful, it will fall back to taking the lock and |
| 2071 | * calling get_user_pages(). |
| 2072 | * |
| 2073 | * Returns number of pages pinned. This may be fewer than the number |
| 2074 | * requested. If nr_pages is 0 or negative, returns 0. If no pages |
| 2075 | * were pinned, returns -errno. |
| 2076 | */ |
| 2077 | int get_user_pages_fast(unsigned long start, int nr_pages, int write, |
| 2078 | struct page **pages) |
| 2079 | { |
Kirill A. Shutemov | 5b65c467 | 2017-09-09 00:56:03 +0300 | [diff] [blame] | 2080 | unsigned long addr, len, end; |
Kirill A. Shutemov | 73e10a6 | 2017-03-16 18:26:54 +0300 | [diff] [blame] | 2081 | int nr = 0, ret = 0; |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2082 | |
| 2083 | start &= PAGE_MASK; |
Kirill A. Shutemov | 5b65c467 | 2017-09-09 00:56:03 +0300 | [diff] [blame] | 2084 | addr = start; |
| 2085 | len = (unsigned long) nr_pages << PAGE_SHIFT; |
| 2086 | end = start + len; |
| 2087 | |
Michael S. Tsirkin | c61611f | 2018-04-13 15:35:20 -0700 | [diff] [blame] | 2088 | if (nr_pages <= 0) |
| 2089 | return 0; |
| 2090 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 2091 | if (unlikely(!access_ok((void __user *)start, len))) |
Michael S. Tsirkin | c61611f | 2018-04-13 15:35:20 -0700 | [diff] [blame] | 2092 | return -EFAULT; |
Kirill A. Shutemov | 73e10a6 | 2017-03-16 18:26:54 +0300 | [diff] [blame] | 2093 | |
Ira Weiny | ad8cfb9 | 2019-02-10 14:34:24 -0800 | [diff] [blame] | 2094 | if (gup_fast_permitted(start, nr_pages)) { |
Kirill A. Shutemov | 5b65c467 | 2017-09-09 00:56:03 +0300 | [diff] [blame] | 2095 | local_irq_disable(); |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame^] | 2096 | gup_pgd_range(addr, end, write ? FOLL_WRITE : 0, pages, &nr); |
Kirill A. Shutemov | 5b65c467 | 2017-09-09 00:56:03 +0300 | [diff] [blame] | 2097 | local_irq_enable(); |
Kirill A. Shutemov | 73e10a6 | 2017-03-16 18:26:54 +0300 | [diff] [blame] | 2098 | ret = nr; |
| 2099 | } |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2100 | |
| 2101 | if (nr < nr_pages) { |
| 2102 | /* Try to get the remaining pages with get_user_pages */ |
| 2103 | start += nr << PAGE_SHIFT; |
| 2104 | pages += nr; |
| 2105 | |
Lorenzo Stoakes | c164154 | 2016-10-13 01:20:13 +0100 | [diff] [blame] | 2106 | ret = get_user_pages_unlocked(start, nr_pages - nr, pages, |
| 2107 | write ? FOLL_WRITE : 0); |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2108 | |
| 2109 | /* Have to be a bit careful with return values */ |
| 2110 | if (nr > 0) { |
| 2111 | if (ret < 0) |
| 2112 | ret = nr; |
| 2113 | else |
| 2114 | ret += nr; |
| 2115 | } |
| 2116 | } |
| 2117 | |
| 2118 | return ret; |
| 2119 | } |
| 2120 | |
Kirill A. Shutemov | e585513 | 2017-06-06 14:31:20 +0300 | [diff] [blame] | 2121 | #endif /* CONFIG_HAVE_GENERIC_GUP */ |