blob: 0e589a9a8801230c3d16a5fcc0045b4162e52e4a [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Jan Kara8025e5d2015-07-13 11:55:44 -03002#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 Corbet61f9ec12015-11-05 18:46:23 -080011/**
Jan Kara8025e5d2015-07-13 11:55:44 -030012 * 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 Stoakes7f23b352016-10-13 01:20:15 +010015 * @gup_flags: flags modifying lookup behaviour
Jan Kara8025e5d2015-07-13 11:55:44 -030016 * @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 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -070032 * This function takes care of grabbing mmap_lock as necessary.
Jan Kara8025e5d2015-07-13 11:55:44 -030033 */
34int get_vaddr_frames(unsigned long start, unsigned int nr_frames,
Lorenzo Stoakes7f23b352016-10-13 01:20:15 +010035 unsigned int gup_flags, struct frame_vector *vec)
Jan Kara8025e5d2015-07-13 11:55:44 -030036{
37 struct mm_struct *mm = current->mm;
38 struct vm_area_struct *vma;
39 int ret = 0;
Jan Kara8025e5d2015-07-13 11:55:44 -030040 int locked;
41
42 if (nr_frames == 0)
43 return 0;
44
45 if (WARN_ON_ONCE(nr_frames > vec->nr_allocated))
46 nr_frames = vec->nr_allocated;
47
Andrey Konovalov5d65e7a2019-09-25 16:48:37 -070048 start = untagged_addr(start);
49
Michel Lespinassed8ed45c2020-06-08 21:33:25 -070050 mmap_read_lock(mm);
Jan Kara8025e5d2015-07-13 11:55:44 -030051 locked = 1;
52 vma = find_vma_intersection(mm, start, start + 1);
53 if (!vma) {
54 ret = -EFAULT;
55 goto out;
56 }
Dan Williamsb7f05542017-11-29 16:10:39 -080057
58 /*
59 * While get_vaddr_frames() could be used for transient (kernel
60 * controlled lifetime) pinning of memory pages all current
61 * users establish long term (userspace controlled lifetime)
62 * page pinning. Treat get_vaddr_frames() like
63 * get_user_pages_longterm() and disallow it for filesystem-dax
64 * mappings.
65 */
Christophe JAILLET1f704fd2017-12-14 15:33:08 -080066 if (vma_is_fsdax(vma)) {
67 ret = -EOPNOTSUPP;
68 goto out;
69 }
Dan Williamsb7f05542017-11-29 16:10:39 -080070
Jan Kara8025e5d2015-07-13 11:55:44 -030071 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) {
72 vec->got_ref = true;
73 vec->is_pfns = false;
John Hubbard55a650c2020-06-07 21:41:05 -070074 ret = pin_user_pages_locked(start, nr_frames,
Lorenzo Stoakes3b913172016-10-13 01:20:14 +010075 gup_flags, (struct page **)(vec->ptrs), &locked);
Linus Torvaldsd072a102022-11-30 16:10:52 -080076 if (likely(ret > 0))
77 goto out;
Jan Kara8025e5d2015-07-13 11:55:44 -030078 }
79
Linus Torvaldsd072a102022-11-30 16:10:52 -080080 /* This used to (racily) return non-refcounted pfns. Let people know */
81 WARN_ONCE(1, "get_vaddr_frames() cannot follow VM_IO mapping");
82 vec->nr_frames = 0;
Jan Kara8025e5d2015-07-13 11:55:44 -030083
Jan Kara8025e5d2015-07-13 11:55:44 -030084out:
85 if (locked)
Michel Lespinassed8ed45c2020-06-08 21:33:25 -070086 mmap_read_unlock(mm);
Jan Kara8025e5d2015-07-13 11:55:44 -030087 if (!ret)
88 ret = -EFAULT;
89 if (ret > 0)
90 vec->nr_frames = ret;
91 return ret;
92}
93EXPORT_SYMBOL(get_vaddr_frames);
94
95/**
96 * put_vaddr_frames() - drop references to pages if get_vaddr_frames() acquired
97 * them
98 * @vec: frame vector to put
99 *
100 * Drop references to pages if get_vaddr_frames() acquired them. We also
101 * invalidate the frame vector so that it is prepared for the next call into
102 * get_vaddr_frames().
103 */
104void put_vaddr_frames(struct frame_vector *vec)
105{
Jan Kara8025e5d2015-07-13 11:55:44 -0300106 struct page **pages;
107
108 if (!vec->got_ref)
109 goto out;
110 pages = frame_vector_pages(vec);
111 /*
112 * frame_vector_pages() might needed to do a conversion when
113 * get_vaddr_frames() got pages but vec was later converted to pfns.
114 * But it shouldn't really fail to convert pfns back...
115 */
116 if (WARN_ON(IS_ERR(pages)))
117 goto out;
John Hubbard55a650c2020-06-07 21:41:05 -0700118
119 unpin_user_pages(pages, vec->nr_frames);
Jan Kara8025e5d2015-07-13 11:55:44 -0300120 vec->got_ref = false;
121out:
122 vec->nr_frames = 0;
123}
124EXPORT_SYMBOL(put_vaddr_frames);
125
126/**
127 * frame_vector_to_pages - convert frame vector to contain page pointers
128 * @vec: frame vector to convert
129 *
130 * Convert @vec to contain array of page pointers. If the conversion is
131 * successful, return 0. Otherwise return an error. Note that we do not grab
132 * page references for the page structures.
133 */
134int frame_vector_to_pages(struct frame_vector *vec)
135{
136 int i;
137 unsigned long *nums;
138 struct page **pages;
139
140 if (!vec->is_pfns)
141 return 0;
142 nums = frame_vector_pfns(vec);
143 for (i = 0; i < vec->nr_frames; i++)
144 if (!pfn_valid(nums[i]))
145 return -EINVAL;
146 pages = (struct page **)nums;
147 for (i = 0; i < vec->nr_frames; i++)
148 pages[i] = pfn_to_page(nums[i]);
149 vec->is_pfns = false;
150 return 0;
151}
152EXPORT_SYMBOL(frame_vector_to_pages);
153
154/**
155 * frame_vector_to_pfns - convert frame vector to contain pfns
156 * @vec: frame vector to convert
157 *
158 * Convert @vec to contain array of pfns.
159 */
160void frame_vector_to_pfns(struct frame_vector *vec)
161{
162 int i;
163 unsigned long *nums;
164 struct page **pages;
165
166 if (vec->is_pfns)
167 return;
168 pages = (struct page **)(vec->ptrs);
169 nums = (unsigned long *)pages;
170 for (i = 0; i < vec->nr_frames; i++)
171 nums[i] = page_to_pfn(pages[i]);
172 vec->is_pfns = true;
173}
174EXPORT_SYMBOL(frame_vector_to_pfns);
175
176/**
177 * frame_vector_create() - allocate & initialize structure for pinned pfns
178 * @nr_frames: number of pfns slots we should reserve
179 *
180 * Allocate and initialize struct pinned_pfns to be able to hold @nr_pfns
181 * pfns.
182 */
183struct frame_vector *frame_vector_create(unsigned int nr_frames)
184{
185 struct frame_vector *vec;
186 int size = sizeof(struct frame_vector) + sizeof(void *) * nr_frames;
187
188 if (WARN_ON_ONCE(nr_frames == 0))
189 return NULL;
190 /*
191 * This is absurdly high. It's here just to avoid strange effects when
192 * arithmetics overflows.
193 */
194 if (WARN_ON_ONCE(nr_frames > INT_MAX / sizeof(void *) / 2))
195 return NULL;
196 /*
197 * Avoid higher order allocations, use vmalloc instead. It should
198 * be rare anyway.
199 */
Michal Hocko752ade62017-05-08 15:57:27 -0700200 vec = kvmalloc(size, GFP_KERNEL);
Jan Kara8025e5d2015-07-13 11:55:44 -0300201 if (!vec)
202 return NULL;
203 vec->nr_allocated = nr_frames;
204 vec->nr_frames = 0;
205 return vec;
206}
207EXPORT_SYMBOL(frame_vector_create);
208
209/**
210 * frame_vector_destroy() - free memory allocated to carry frame vector
211 * @vec: Frame vector to free
212 *
213 * Free structure allocated by frame_vector_create() to carry frames.
214 */
215void frame_vector_destroy(struct frame_vector *vec)
216{
217 /* Make sure put_vaddr_frames() got called properly... */
218 VM_BUG_ON(vec->nr_frames > 0);
219 kvfree(vec);
220}
221EXPORT_SYMBOL(frame_vector_destroy);