1 #include <linux/kernel.h>
2 #include <linux/errno.h>
5 #include <linux/slab.h>
6 #include <linux/vmalloc.h>
7 #include <linux/pagemap.h>
8 #include <linux/sched.h>
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 * @write: whether pages will be written to by the caller
15 * @force: whether to force write access even if user mapping is
16 * readonly. See description of the same argument of
18 * @vec: structure which receives pages / pfns of the addresses mapped.
19 * It should have space for at least nr_frames entries.
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.
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.
34 * This function takes care of grabbing mmap_sem as necessary.
36 int get_vaddr_frames(unsigned long start, unsigned int nr_frames,
37 bool write, bool force, struct frame_vector *vec)
39 struct mm_struct *mm = current->mm;
40 struct vm_area_struct *vma;
44 unsigned int gup_flags = 0;
50 gup_flags |= FOLL_WRITE;
52 gup_flags |= FOLL_FORCE;
54 if (WARN_ON_ONCE(nr_frames > vec->nr_allocated))
55 nr_frames = vec->nr_allocated;
57 down_read(&mm->mmap_sem);
59 vma = find_vma_intersection(mm, start, start + 1);
64 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) {
67 ret = get_user_pages_locked(start, nr_frames,
68 gup_flags, (struct page **)(vec->ptrs), &locked);
75 unsigned long *nums = frame_vector_pfns(vec);
77 while (ret < nr_frames && start + PAGE_SIZE <= vma->vm_end) {
78 err = follow_pfn(vma, start, &nums[ret]);
88 * We stop if we have enough pages or if VMA doesn't completely
89 * cover the tail page.
91 if (ret >= nr_frames || start < vma->vm_end)
93 vma = find_vma_intersection(mm, start, start + 1);
94 } while (vma && vma->vm_flags & (VM_IO | VM_PFNMAP));
97 up_read(&mm->mmap_sem);
101 vec->nr_frames = ret;
104 EXPORT_SYMBOL(get_vaddr_frames);
107 * put_vaddr_frames() - drop references to pages if get_vaddr_frames() acquired
109 * @vec: frame vector to put
111 * Drop references to pages if get_vaddr_frames() acquired them. We also
112 * invalidate the frame vector so that it is prepared for the next call into
113 * get_vaddr_frames().
115 void put_vaddr_frames(struct frame_vector *vec)
122 pages = frame_vector_pages(vec);
124 * frame_vector_pages() might needed to do a conversion when
125 * get_vaddr_frames() got pages but vec was later converted to pfns.
126 * But it shouldn't really fail to convert pfns back...
128 if (WARN_ON(IS_ERR(pages)))
130 for (i = 0; i < vec->nr_frames; i++)
132 vec->got_ref = false;
136 EXPORT_SYMBOL(put_vaddr_frames);
139 * frame_vector_to_pages - convert frame vector to contain page pointers
140 * @vec: frame vector to convert
142 * Convert @vec to contain array of page pointers. If the conversion is
143 * successful, return 0. Otherwise return an error. Note that we do not grab
144 * page references for the page structures.
146 int frame_vector_to_pages(struct frame_vector *vec)
154 nums = frame_vector_pfns(vec);
155 for (i = 0; i < vec->nr_frames; i++)
156 if (!pfn_valid(nums[i]))
158 pages = (struct page **)nums;
159 for (i = 0; i < vec->nr_frames; i++)
160 pages[i] = pfn_to_page(nums[i]);
161 vec->is_pfns = false;
164 EXPORT_SYMBOL(frame_vector_to_pages);
167 * frame_vector_to_pfns - convert frame vector to contain pfns
168 * @vec: frame vector to convert
170 * Convert @vec to contain array of pfns.
172 void frame_vector_to_pfns(struct frame_vector *vec)
180 pages = (struct page **)(vec->ptrs);
181 nums = (unsigned long *)pages;
182 for (i = 0; i < vec->nr_frames; i++)
183 nums[i] = page_to_pfn(pages[i]);
186 EXPORT_SYMBOL(frame_vector_to_pfns);
189 * frame_vector_create() - allocate & initialize structure for pinned pfns
190 * @nr_frames: number of pfns slots we should reserve
192 * Allocate and initialize struct pinned_pfns to be able to hold @nr_pfns
195 struct frame_vector *frame_vector_create(unsigned int nr_frames)
197 struct frame_vector *vec;
198 int size = sizeof(struct frame_vector) + sizeof(void *) * nr_frames;
200 if (WARN_ON_ONCE(nr_frames == 0))
203 * This is absurdly high. It's here just to avoid strange effects when
204 * arithmetics overflows.
206 if (WARN_ON_ONCE(nr_frames > INT_MAX / sizeof(void *) / 2))
209 * Avoid higher order allocations, use vmalloc instead. It should
212 if (size <= PAGE_SIZE)
213 vec = kmalloc(size, GFP_KERNEL);
218 vec->nr_allocated = nr_frames;
222 EXPORT_SYMBOL(frame_vector_create);
225 * frame_vector_destroy() - free memory allocated to carry frame vector
226 * @vec: Frame vector to free
228 * Free structure allocated by frame_vector_create() to carry frames.
230 void frame_vector_destroy(struct frame_vector *vec)
232 /* Make sure put_vaddr_frames() got called properly... */
233 VM_BUG_ON(vec->nr_frames > 0);
236 EXPORT_SYMBOL(frame_vector_destroy);