xref: /linux/drivers/media/common/videobuf2/frame_vector.c (revision 172cdcaefea5c297fdb3d20b7d5aff60ae4fbce6)
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  * This function takes care of grabbing mmap_lock as necessary.
34  */
35 int get_vaddr_frames(unsigned long start, unsigned int nr_frames,
36 		     struct frame_vector *vec)
37 {
38 	struct mm_struct *mm = current->mm;
39 	struct vm_area_struct *vma;
40 	int ret = 0;
41 	int err;
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 	ret = pin_user_pages_fast(start, nr_frames,
52 				  FOLL_FORCE | FOLL_WRITE | FOLL_LONGTERM,
53 				  (struct page **)(vec->ptrs));
54 	if (ret > 0) {
55 		vec->got_ref = true;
56 		vec->is_pfns = false;
57 		goto out_unlocked;
58 	}
59 
60 	mmap_read_lock(mm);
61 	vec->got_ref = false;
62 	vec->is_pfns = true;
63 	ret = 0;
64 	do {
65 		unsigned long *nums = frame_vector_pfns(vec);
66 
67 		vma = find_vma_intersection(mm, start, start + 1);
68 		if (!vma)
69 			break;
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 		/* Bail out if VMA doesn't completely cover the tail page. */
82 		if (start < vma->vm_end)
83 			break;
84 	} while (ret < nr_frames);
85 out:
86 	mmap_read_unlock(mm);
87 out_unlocked:
88 	if (!ret)
89 		ret = -EFAULT;
90 	if (ret > 0)
91 		vec->nr_frames = ret;
92 	return ret;
93 }
94 EXPORT_SYMBOL(get_vaddr_frames);
95 
96 /**
97  * put_vaddr_frames() - drop references to pages if get_vaddr_frames() acquired
98  *			them
99  * @vec:	frame vector to put
100  *
101  * Drop references to pages if get_vaddr_frames() acquired them. We also
102  * invalidate the frame vector so that it is prepared for the next call into
103  * get_vaddr_frames().
104  */
105 void put_vaddr_frames(struct frame_vector *vec)
106 {
107 	struct page **pages;
108 
109 	if (!vec->got_ref)
110 		goto out;
111 	pages = frame_vector_pages(vec);
112 	/*
113 	 * frame_vector_pages() might needed to do a conversion when
114 	 * get_vaddr_frames() got pages but vec was later converted to pfns.
115 	 * But it shouldn't really fail to convert pfns back...
116 	 */
117 	if (WARN_ON(IS_ERR(pages)))
118 		goto out;
119 
120 	unpin_user_pages(pages, vec->nr_frames);
121 	vec->got_ref = false;
122 out:
123 	vec->nr_frames = 0;
124 }
125 EXPORT_SYMBOL(put_vaddr_frames);
126 
127 /**
128  * frame_vector_to_pages - convert frame vector to contain page pointers
129  * @vec:	frame vector to convert
130  *
131  * Convert @vec to contain array of page pointers.  If the conversion is
132  * successful, return 0. Otherwise return an error. Note that we do not grab
133  * page references for the page structures.
134  */
135 int frame_vector_to_pages(struct frame_vector *vec)
136 {
137 	int i;
138 	unsigned long *nums;
139 	struct page **pages;
140 
141 	if (!vec->is_pfns)
142 		return 0;
143 	nums = frame_vector_pfns(vec);
144 	for (i = 0; i < vec->nr_frames; i++)
145 		if (!pfn_valid(nums[i]))
146 			return -EINVAL;
147 	pages = (struct page **)nums;
148 	for (i = 0; i < vec->nr_frames; i++)
149 		pages[i] = pfn_to_page(nums[i]);
150 	vec->is_pfns = false;
151 	return 0;
152 }
153 EXPORT_SYMBOL(frame_vector_to_pages);
154 
155 /**
156  * frame_vector_to_pfns - convert frame vector to contain pfns
157  * @vec:	frame vector to convert
158  *
159  * Convert @vec to contain array of pfns.
160  */
161 void frame_vector_to_pfns(struct frame_vector *vec)
162 {
163 	int i;
164 	unsigned long *nums;
165 	struct page **pages;
166 
167 	if (vec->is_pfns)
168 		return;
169 	pages = (struct page **)(vec->ptrs);
170 	nums = (unsigned long *)pages;
171 	for (i = 0; i < vec->nr_frames; i++)
172 		nums[i] = page_to_pfn(pages[i]);
173 	vec->is_pfns = true;
174 }
175 EXPORT_SYMBOL(frame_vector_to_pfns);
176 
177 /**
178  * frame_vector_create() - allocate & initialize structure for pinned pfns
179  * @nr_frames:	number of pfns slots we should reserve
180  *
181  * Allocate and initialize struct pinned_pfns to be able to hold @nr_pfns
182  * pfns.
183  */
184 struct frame_vector *frame_vector_create(unsigned int nr_frames)
185 {
186 	struct frame_vector *vec;
187 	int size = sizeof(struct frame_vector) + sizeof(void *) * nr_frames;
188 
189 	if (WARN_ON_ONCE(nr_frames == 0))
190 		return NULL;
191 	/*
192 	 * This is absurdly high. It's here just to avoid strange effects when
193 	 * arithmetics overflows.
194 	 */
195 	if (WARN_ON_ONCE(nr_frames > INT_MAX / sizeof(void *) / 2))
196 		return NULL;
197 	/*
198 	 * Avoid higher order allocations, use vmalloc instead. It should
199 	 * be rare anyway.
200 	 */
201 	vec = kvmalloc(size, GFP_KERNEL);
202 	if (!vec)
203 		return NULL;
204 	vec->nr_allocated = nr_frames;
205 	vec->nr_frames = 0;
206 	return vec;
207 }
208 EXPORT_SYMBOL(frame_vector_create);
209 
210 /**
211  * frame_vector_destroy() - free memory allocated to carry frame vector
212  * @vec:	Frame vector to free
213  *
214  * Free structure allocated by frame_vector_create() to carry frames.
215  */
216 void frame_vector_destroy(struct frame_vector *vec)
217 {
218 	/* Make sure put_vaddr_frames() got called properly... */
219 	VM_BUG_ON(vec->nr_frames > 0);
220 	kvfree(vec);
221 }
222 EXPORT_SYMBOL(frame_vector_destroy);
223