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