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 #include <media/frame_vector.h>
12
13 /**
14 * get_vaddr_frames() - map virtual addresses to pfns
15 * @start: starting user address
16 * @nr_frames: number of pages / pfns from start to map
17 * @vec: structure which receives pages / pfns of the addresses mapped.
18 * It should have space for at least nr_frames entries.
19 *
20 * This function maps virtual addresses from @start and fills @vec structure
21 * with page frame numbers or page pointers to corresponding pages (choice
22 * depends on the type of the vma underlying the virtual address). If @start
23 * belongs to a normal vma, the function grabs reference to each of the pages
24 * to pin them in memory. If @start belongs to VM_IO | VM_PFNMAP vma, we don't
25 * touch page structures and the caller must make sure pfns aren't reused for
26 * anything else while he is using them.
27 *
28 * The function returns number of pages mapped which may be less than
29 * @nr_frames. In particular we stop mapping if there are more vmas of
30 * different type underlying the specified range of virtual addresses.
31 * When the function isn't able to map a single page, it returns error.
32 *
33 * Note that get_vaddr_frames() cannot follow VM_IO mappings. It used
34 * to be able to do that, but that could (racily) return non-refcounted
35 * pfns.
36 *
37 * This function takes care of grabbing mmap_lock as necessary.
38 */
get_vaddr_frames(unsigned long start,unsigned int nr_frames,struct frame_vector * vec)39 int get_vaddr_frames(unsigned long start, unsigned int nr_frames,
40 struct frame_vector *vec)
41 {
42 int ret;
43
44 if (nr_frames == 0)
45 return 0;
46
47 if (WARN_ON_ONCE(nr_frames > vec->nr_allocated))
48 nr_frames = vec->nr_allocated;
49
50 start = untagged_addr(start);
51
52 ret = pin_user_pages_fast(start, nr_frames,
53 FOLL_FORCE | FOLL_WRITE | FOLL_LONGTERM,
54 (struct page **)(vec->ptrs));
55 vec->got_ref = true;
56 vec->is_pfns = false;
57 vec->nr_frames = ret;
58
59 if (likely(ret > 0))
60 return ret;
61
62 vec->nr_frames = 0;
63 return ret ? ret : -EFAULT;
64 }
65 EXPORT_SYMBOL(get_vaddr_frames);
66
67 /**
68 * put_vaddr_frames() - drop references to pages if get_vaddr_frames() acquired
69 * them
70 * @vec: frame vector to put
71 *
72 * Drop references to pages if get_vaddr_frames() acquired them. We also
73 * invalidate the frame vector so that it is prepared for the next call into
74 * get_vaddr_frames().
75 */
put_vaddr_frames(struct frame_vector * vec)76 void put_vaddr_frames(struct frame_vector *vec)
77 {
78 struct page **pages;
79
80 if (!vec->got_ref)
81 goto out;
82 pages = frame_vector_pages(vec);
83 /*
84 * frame_vector_pages() might needed to do a conversion when
85 * get_vaddr_frames() got pages but vec was later converted to pfns.
86 * But it shouldn't really fail to convert pfns back...
87 */
88 if (WARN_ON(IS_ERR(pages)))
89 goto out;
90
91 unpin_user_pages(pages, vec->nr_frames);
92 vec->got_ref = false;
93 out:
94 vec->nr_frames = 0;
95 }
96 EXPORT_SYMBOL(put_vaddr_frames);
97
98 /**
99 * frame_vector_to_pages - convert frame vector to contain page pointers
100 * @vec: frame vector to convert
101 *
102 * Convert @vec to contain array of page pointers. If the conversion is
103 * successful, return 0. Otherwise return an error. Note that we do not grab
104 * page references for the page structures.
105 */
frame_vector_to_pages(struct frame_vector * vec)106 int frame_vector_to_pages(struct frame_vector *vec)
107 {
108 int i;
109 unsigned long *nums;
110 struct page **pages;
111
112 if (!vec->is_pfns)
113 return 0;
114 nums = frame_vector_pfns(vec);
115 for (i = 0; i < vec->nr_frames; i++)
116 if (!pfn_valid(nums[i]))
117 return -EINVAL;
118 pages = (struct page **)nums;
119 for (i = 0; i < vec->nr_frames; i++)
120 pages[i] = pfn_to_page(nums[i]);
121 vec->is_pfns = false;
122 return 0;
123 }
124 EXPORT_SYMBOL(frame_vector_to_pages);
125
126 /**
127 * frame_vector_to_pfns - convert frame vector to contain pfns
128 * @vec: frame vector to convert
129 *
130 * Convert @vec to contain array of pfns.
131 */
frame_vector_to_pfns(struct frame_vector * vec)132 void frame_vector_to_pfns(struct frame_vector *vec)
133 {
134 int i;
135 unsigned long *nums;
136 struct page **pages;
137
138 if (vec->is_pfns)
139 return;
140 pages = (struct page **)(vec->ptrs);
141 nums = (unsigned long *)pages;
142 for (i = 0; i < vec->nr_frames; i++)
143 nums[i] = page_to_pfn(pages[i]);
144 vec->is_pfns = true;
145 }
146 EXPORT_SYMBOL(frame_vector_to_pfns);
147
148 /**
149 * frame_vector_create() - allocate & initialize structure for pinned pfns
150 * @nr_frames: number of pfns slots we should reserve
151 *
152 * Allocate and initialize struct pinned_pfns to be able to hold @nr_pfns
153 * pfns.
154 */
frame_vector_create(unsigned int nr_frames)155 struct frame_vector *frame_vector_create(unsigned int nr_frames)
156 {
157 struct frame_vector *vec;
158 int size = sizeof(struct frame_vector) + sizeof(void *) * nr_frames;
159
160 if (WARN_ON_ONCE(nr_frames == 0))
161 return NULL;
162 /*
163 * This is absurdly high. It's here just to avoid strange effects when
164 * arithmetics overflows.
165 */
166 if (WARN_ON_ONCE(nr_frames > INT_MAX / sizeof(void *) / 2))
167 return NULL;
168 /*
169 * Avoid higher order allocations, use vmalloc instead. It should
170 * be rare anyway.
171 */
172 vec = kvmalloc(size, GFP_KERNEL);
173 if (!vec)
174 return NULL;
175 vec->nr_allocated = nr_frames;
176 vec->nr_frames = 0;
177 return vec;
178 }
179 EXPORT_SYMBOL(frame_vector_create);
180
181 /**
182 * frame_vector_destroy() - free memory allocated to carry frame vector
183 * @vec: Frame vector to free
184 *
185 * Free structure allocated by frame_vector_create() to carry frames.
186 */
frame_vector_destroy(struct frame_vector * vec)187 void frame_vector_destroy(struct frame_vector *vec)
188 {
189 /* Make sure put_vaddr_frames() got called properly... */
190 VM_BUG_ON(vec->nr_frames > 0);
191 kvfree(vec);
192 }
193 EXPORT_SYMBOL(frame_vector_destroy);
194