Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jan Kara | 8025e5d | 2015-07-13 11:55:44 -0300 | [diff] [blame] | 2 | #include <linux/kernel.h> |
| 3 | #include <linux/errno.h> |
| 4 | #include <linux/err.h> |
| 5 | #include <linux/mm.h> |
| 6 | #include <linux/slab.h> |
| 7 | #include <linux/vmalloc.h> |
| 8 | #include <linux/pagemap.h> |
| 9 | #include <linux/sched.h> |
| 10 | |
Jonathan Corbet | 61f9ec1 | 2015-11-05 18:46:23 -0800 | [diff] [blame] | 11 | /** |
Jan Kara | 8025e5d | 2015-07-13 11:55:44 -0300 | [diff] [blame] | 12 | * get_vaddr_frames() - map virtual addresses to pfns |
| 13 | * @start: starting user address |
| 14 | * @nr_frames: number of pages / pfns from start to map |
Lorenzo Stoakes | 7f23b35 | 2016-10-13 01:20:15 +0100 | [diff] [blame] | 15 | * @gup_flags: flags modifying lookup behaviour |
Jan Kara | 8025e5d | 2015-07-13 11:55:44 -0300 | [diff] [blame] | 16 | * @vec: structure which receives pages / pfns of the addresses mapped. |
| 17 | * It should have space for at least nr_frames entries. |
| 18 | * |
| 19 | * This function maps virtual addresses from @start and fills @vec structure |
| 20 | * with page frame numbers or page pointers to corresponding pages (choice |
| 21 | * depends on the type of the vma underlying the virtual address). If @start |
| 22 | * belongs to a normal vma, the function grabs reference to each of the pages |
| 23 | * to pin them in memory. If @start belongs to VM_IO | VM_PFNMAP vma, we don't |
| 24 | * touch page structures and the caller must make sure pfns aren't reused for |
| 25 | * anything else while he is using them. |
| 26 | * |
| 27 | * The function returns number of pages mapped which may be less than |
| 28 | * @nr_frames. In particular we stop mapping if there are more vmas of |
| 29 | * different type underlying the specified range of virtual addresses. |
| 30 | * When the function isn't able to map a single page, it returns error. |
| 31 | * |
| 32 | * This function takes care of grabbing mmap_sem as necessary. |
| 33 | */ |
| 34 | int get_vaddr_frames(unsigned long start, unsigned int nr_frames, |
Lorenzo Stoakes | 7f23b35 | 2016-10-13 01:20:15 +0100 | [diff] [blame] | 35 | unsigned int gup_flags, struct frame_vector *vec) |
Jan Kara | 8025e5d | 2015-07-13 11:55:44 -0300 | [diff] [blame] | 36 | { |
| 37 | struct mm_struct *mm = current->mm; |
| 38 | struct vm_area_struct *vma; |
| 39 | int ret = 0; |
| 40 | int err; |
| 41 | int locked; |
| 42 | |
| 43 | if (nr_frames == 0) |
| 44 | return 0; |
| 45 | |
| 46 | if (WARN_ON_ONCE(nr_frames > vec->nr_allocated)) |
| 47 | nr_frames = vec->nr_allocated; |
| 48 | |
Andrey Konovalov | 5d65e7a | 2019-09-25 16:48:37 -0700 | [diff] [blame] | 49 | start = untagged_addr(start); |
| 50 | |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame^] | 51 | mmap_read_lock(mm); |
Jan Kara | 8025e5d | 2015-07-13 11:55:44 -0300 | [diff] [blame] | 52 | locked = 1; |
| 53 | vma = find_vma_intersection(mm, start, start + 1); |
| 54 | if (!vma) { |
| 55 | ret = -EFAULT; |
| 56 | goto out; |
| 57 | } |
Dan Williams | b7f0554 | 2017-11-29 16:10:39 -0800 | [diff] [blame] | 58 | |
| 59 | /* |
| 60 | * While get_vaddr_frames() could be used for transient (kernel |
| 61 | * controlled lifetime) pinning of memory pages all current |
| 62 | * users establish long term (userspace controlled lifetime) |
| 63 | * page pinning. Treat get_vaddr_frames() like |
| 64 | * get_user_pages_longterm() and disallow it for filesystem-dax |
| 65 | * mappings. |
| 66 | */ |
Christophe JAILLET | 1f704fd | 2017-12-14 15:33:08 -0800 | [diff] [blame] | 67 | if (vma_is_fsdax(vma)) { |
| 68 | ret = -EOPNOTSUPP; |
| 69 | goto out; |
| 70 | } |
Dan Williams | b7f0554 | 2017-11-29 16:10:39 -0800 | [diff] [blame] | 71 | |
Jan Kara | 8025e5d | 2015-07-13 11:55:44 -0300 | [diff] [blame] | 72 | if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) { |
| 73 | vec->got_ref = true; |
| 74 | vec->is_pfns = false; |
John Hubbard | 55a650c | 2020-06-07 21:41:05 -0700 | [diff] [blame] | 75 | ret = pin_user_pages_locked(start, nr_frames, |
Lorenzo Stoakes | 3b91317 | 2016-10-13 01:20:14 +0100 | [diff] [blame] | 76 | gup_flags, (struct page **)(vec->ptrs), &locked); |
Jan Kara | 8025e5d | 2015-07-13 11:55:44 -0300 | [diff] [blame] | 77 | goto out; |
| 78 | } |
| 79 | |
| 80 | vec->got_ref = false; |
| 81 | vec->is_pfns = true; |
| 82 | do { |
| 83 | unsigned long *nums = frame_vector_pfns(vec); |
| 84 | |
| 85 | while (ret < nr_frames && start + PAGE_SIZE <= vma->vm_end) { |
| 86 | err = follow_pfn(vma, start, &nums[ret]); |
| 87 | if (err) { |
| 88 | if (ret == 0) |
| 89 | ret = err; |
| 90 | goto out; |
| 91 | } |
| 92 | start += PAGE_SIZE; |
| 93 | ret++; |
| 94 | } |
| 95 | /* |
| 96 | * We stop if we have enough pages or if VMA doesn't completely |
| 97 | * cover the tail page. |
| 98 | */ |
| 99 | if (ret >= nr_frames || start < vma->vm_end) |
| 100 | break; |
| 101 | vma = find_vma_intersection(mm, start, start + 1); |
| 102 | } while (vma && vma->vm_flags & (VM_IO | VM_PFNMAP)); |
| 103 | out: |
| 104 | if (locked) |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame^] | 105 | mmap_read_unlock(mm); |
Jan Kara | 8025e5d | 2015-07-13 11:55:44 -0300 | [diff] [blame] | 106 | if (!ret) |
| 107 | ret = -EFAULT; |
| 108 | if (ret > 0) |
| 109 | vec->nr_frames = ret; |
| 110 | return ret; |
| 111 | } |
| 112 | EXPORT_SYMBOL(get_vaddr_frames); |
| 113 | |
| 114 | /** |
| 115 | * put_vaddr_frames() - drop references to pages if get_vaddr_frames() acquired |
| 116 | * them |
| 117 | * @vec: frame vector to put |
| 118 | * |
| 119 | * Drop references to pages if get_vaddr_frames() acquired them. We also |
| 120 | * invalidate the frame vector so that it is prepared for the next call into |
| 121 | * get_vaddr_frames(). |
| 122 | */ |
| 123 | void put_vaddr_frames(struct frame_vector *vec) |
| 124 | { |
Jan Kara | 8025e5d | 2015-07-13 11:55:44 -0300 | [diff] [blame] | 125 | struct page **pages; |
| 126 | |
| 127 | if (!vec->got_ref) |
| 128 | goto out; |
| 129 | pages = frame_vector_pages(vec); |
| 130 | /* |
| 131 | * frame_vector_pages() might needed to do a conversion when |
| 132 | * get_vaddr_frames() got pages but vec was later converted to pfns. |
| 133 | * But it shouldn't really fail to convert pfns back... |
| 134 | */ |
| 135 | if (WARN_ON(IS_ERR(pages))) |
| 136 | goto out; |
John Hubbard | 55a650c | 2020-06-07 21:41:05 -0700 | [diff] [blame] | 137 | |
| 138 | unpin_user_pages(pages, vec->nr_frames); |
Jan Kara | 8025e5d | 2015-07-13 11:55:44 -0300 | [diff] [blame] | 139 | vec->got_ref = false; |
| 140 | out: |
| 141 | vec->nr_frames = 0; |
| 142 | } |
| 143 | EXPORT_SYMBOL(put_vaddr_frames); |
| 144 | |
| 145 | /** |
| 146 | * frame_vector_to_pages - convert frame vector to contain page pointers |
| 147 | * @vec: frame vector to convert |
| 148 | * |
| 149 | * Convert @vec to contain array of page pointers. If the conversion is |
| 150 | * successful, return 0. Otherwise return an error. Note that we do not grab |
| 151 | * page references for the page structures. |
| 152 | */ |
| 153 | int frame_vector_to_pages(struct frame_vector *vec) |
| 154 | { |
| 155 | int i; |
| 156 | unsigned long *nums; |
| 157 | struct page **pages; |
| 158 | |
| 159 | if (!vec->is_pfns) |
| 160 | return 0; |
| 161 | nums = frame_vector_pfns(vec); |
| 162 | for (i = 0; i < vec->nr_frames; i++) |
| 163 | if (!pfn_valid(nums[i])) |
| 164 | return -EINVAL; |
| 165 | pages = (struct page **)nums; |
| 166 | for (i = 0; i < vec->nr_frames; i++) |
| 167 | pages[i] = pfn_to_page(nums[i]); |
| 168 | vec->is_pfns = false; |
| 169 | return 0; |
| 170 | } |
| 171 | EXPORT_SYMBOL(frame_vector_to_pages); |
| 172 | |
| 173 | /** |
| 174 | * frame_vector_to_pfns - convert frame vector to contain pfns |
| 175 | * @vec: frame vector to convert |
| 176 | * |
| 177 | * Convert @vec to contain array of pfns. |
| 178 | */ |
| 179 | void frame_vector_to_pfns(struct frame_vector *vec) |
| 180 | { |
| 181 | int i; |
| 182 | unsigned long *nums; |
| 183 | struct page **pages; |
| 184 | |
| 185 | if (vec->is_pfns) |
| 186 | return; |
| 187 | pages = (struct page **)(vec->ptrs); |
| 188 | nums = (unsigned long *)pages; |
| 189 | for (i = 0; i < vec->nr_frames; i++) |
| 190 | nums[i] = page_to_pfn(pages[i]); |
| 191 | vec->is_pfns = true; |
| 192 | } |
| 193 | EXPORT_SYMBOL(frame_vector_to_pfns); |
| 194 | |
| 195 | /** |
| 196 | * frame_vector_create() - allocate & initialize structure for pinned pfns |
| 197 | * @nr_frames: number of pfns slots we should reserve |
| 198 | * |
| 199 | * Allocate and initialize struct pinned_pfns to be able to hold @nr_pfns |
| 200 | * pfns. |
| 201 | */ |
| 202 | struct frame_vector *frame_vector_create(unsigned int nr_frames) |
| 203 | { |
| 204 | struct frame_vector *vec; |
| 205 | int size = sizeof(struct frame_vector) + sizeof(void *) * nr_frames; |
| 206 | |
| 207 | if (WARN_ON_ONCE(nr_frames == 0)) |
| 208 | return NULL; |
| 209 | /* |
| 210 | * This is absurdly high. It's here just to avoid strange effects when |
| 211 | * arithmetics overflows. |
| 212 | */ |
| 213 | if (WARN_ON_ONCE(nr_frames > INT_MAX / sizeof(void *) / 2)) |
| 214 | return NULL; |
| 215 | /* |
| 216 | * Avoid higher order allocations, use vmalloc instead. It should |
| 217 | * be rare anyway. |
| 218 | */ |
Michal Hocko | 752ade6 | 2017-05-08 15:57:27 -0700 | [diff] [blame] | 219 | vec = kvmalloc(size, GFP_KERNEL); |
Jan Kara | 8025e5d | 2015-07-13 11:55:44 -0300 | [diff] [blame] | 220 | if (!vec) |
| 221 | return NULL; |
| 222 | vec->nr_allocated = nr_frames; |
| 223 | vec->nr_frames = 0; |
| 224 | return vec; |
| 225 | } |
| 226 | EXPORT_SYMBOL(frame_vector_create); |
| 227 | |
| 228 | /** |
| 229 | * frame_vector_destroy() - free memory allocated to carry frame vector |
| 230 | * @vec: Frame vector to free |
| 231 | * |
| 232 | * Free structure allocated by frame_vector_create() to carry frames. |
| 233 | */ |
| 234 | void frame_vector_destroy(struct frame_vector *vec) |
| 235 | { |
| 236 | /* Make sure put_vaddr_frames() got called properly... */ |
| 237 | VM_BUG_ON(vec->nr_frames > 0); |
| 238 | kvfree(vec); |
| 239 | } |
| 240 | EXPORT_SYMBOL(frame_vector_destroy); |