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