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 * @write: the mapped address has write permission
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_lock as necessary.
35 */
get_vaddr_frames(unsigned long start,unsigned int nr_frames,bool write,struct frame_vector * vec)36 int get_vaddr_frames(unsigned long start, unsigned int nr_frames, bool write,
37 struct frame_vector *vec)
38 {
39 int ret;
40 unsigned int gup_flags = FOLL_LONGTERM;
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 start = untagged_addr(start);
49
50 if (write)
51 gup_flags |= FOLL_WRITE;
52
53 ret = pin_user_pages_fast(start, nr_frames, gup_flags,
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 /* This used to (racily) return non-refcounted pfns. Let people know */
63 WARN_ONCE(1, "get_vaddr_frames() cannot follow VM_IO mapping");
64 vec->nr_frames = 0;
65 return ret ? ret : -EFAULT;
66 }
67 EXPORT_SYMBOL(get_vaddr_frames);
68
69 /**
70 * put_vaddr_frames() - drop references to pages if get_vaddr_frames() acquired
71 * them
72 * @vec: frame vector to put
73 *
74 * Drop references to pages if get_vaddr_frames() acquired them. We also
75 * invalidate the frame vector so that it is prepared for the next call into
76 * get_vaddr_frames().
77 */
put_vaddr_frames(struct frame_vector * vec)78 void put_vaddr_frames(struct frame_vector *vec)
79 {
80 struct page **pages;
81
82 if (!vec->got_ref)
83 goto out;
84 pages = frame_vector_pages(vec);
85 /*
86 * frame_vector_pages() might needed to do a conversion when
87 * get_vaddr_frames() got pages but vec was later converted to pfns.
88 * But it shouldn't really fail to convert pfns back...
89 */
90 if (WARN_ON(IS_ERR(pages)))
91 goto out;
92
93 unpin_user_pages(pages, vec->nr_frames);
94 vec->got_ref = false;
95 out:
96 vec->nr_frames = 0;
97 }
98 EXPORT_SYMBOL(put_vaddr_frames);
99
100 /**
101 * frame_vector_to_pages - convert frame vector to contain page pointers
102 * @vec: frame vector to convert
103 *
104 * Convert @vec to contain array of page pointers. If the conversion is
105 * successful, return 0. Otherwise return an error. Note that we do not grab
106 * page references for the page structures.
107 */
frame_vector_to_pages(struct frame_vector * vec)108 int frame_vector_to_pages(struct frame_vector *vec)
109 {
110 int i;
111 unsigned long *nums;
112 struct page **pages;
113
114 if (!vec->is_pfns)
115 return 0;
116 nums = frame_vector_pfns(vec);
117 for (i = 0; i < vec->nr_frames; i++)
118 if (!pfn_valid(nums[i]))
119 return -EINVAL;
120 pages = (struct page **)nums;
121 for (i = 0; i < vec->nr_frames; i++)
122 pages[i] = pfn_to_page(nums[i]);
123 vec->is_pfns = false;
124 return 0;
125 }
126 EXPORT_SYMBOL(frame_vector_to_pages);
127
128 /**
129 * frame_vector_to_pfns - convert frame vector to contain pfns
130 * @vec: frame vector to convert
131 *
132 * Convert @vec to contain array of pfns.
133 */
frame_vector_to_pfns(struct frame_vector * vec)134 void frame_vector_to_pfns(struct frame_vector *vec)
135 {
136 int i;
137 unsigned long *nums;
138 struct page **pages;
139
140 if (vec->is_pfns)
141 return;
142 pages = (struct page **)(vec->ptrs);
143 nums = (unsigned long *)pages;
144 for (i = 0; i < vec->nr_frames; i++)
145 nums[i] = page_to_pfn(pages[i]);
146 vec->is_pfns = true;
147 }
148 EXPORT_SYMBOL(frame_vector_to_pfns);
149
150 /**
151 * frame_vector_create() - allocate & initialize structure for pinned pfns
152 * @nr_frames: number of pfns slots we should reserve
153 *
154 * Allocate and initialize struct pinned_pfns to be able to hold @nr_pfns
155 * pfns.
156 */
frame_vector_create(unsigned int nr_frames)157 struct frame_vector *frame_vector_create(unsigned int nr_frames)
158 {
159 struct frame_vector *vec;
160 int size = sizeof(struct frame_vector) + sizeof(void *) * nr_frames;
161
162 if (WARN_ON_ONCE(nr_frames == 0))
163 return NULL;
164 /*
165 * This is absurdly high. It's here just to avoid strange effects when
166 * arithmetics overflows.
167 */
168 if (WARN_ON_ONCE(nr_frames > INT_MAX / sizeof(void *) / 2))
169 return NULL;
170 /*
171 * Avoid higher order allocations, use vmalloc instead. It should
172 * be rare anyway.
173 */
174 vec = kvmalloc(size, GFP_KERNEL);
175 if (!vec)
176 return NULL;
177 vec->nr_allocated = nr_frames;
178 vec->nr_frames = 0;
179 return vec;
180 }
181 EXPORT_SYMBOL(frame_vector_create);
182
183 /**
184 * frame_vector_destroy() - free memory allocated to carry frame vector
185 * @vec: Frame vector to free
186 *
187 * Free structure allocated by frame_vector_create() to carry frames.
188 */
frame_vector_destroy(struct frame_vector * vec)189 void frame_vector_destroy(struct frame_vector *vec)
190 {
191 /* Make sure put_vaddr_frames() got called properly... */
192 VM_BUG_ON(vec->nr_frames > 0);
193 kvfree(vec);
194 }
195 EXPORT_SYMBOL(frame_vector_destroy);
196