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