1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kernel.h>
3 #include <linux/errno.h>
6 #include <linux/slab.h>
7 #include <linux/vmalloc.h>
8 #include <linux/pagemap.h>
9 #include <linux/sched.h>
12 * get_vaddr_frames() - map virtual addresses to pfns
13 * @start: starting user address
14 * @nr_frames: number of pages / pfns from start to map
15 * @gup_flags: flags modifying lookup behaviour
16 * @vec: structure which receives pages / pfns of the addresses mapped.
17 * It should have space for at least nr_frames entries.
19 * This function maps virtual addresses from @start and fills @vec structure
20 * with page frame numbers or page pointers to corresponding pages (choice
21 * depends on the type of the vma underlying the virtual address). If @start
22 * belongs to a normal vma, the function grabs reference to each of the pages
23 * to pin them in memory. If @start belongs to VM_IO | VM_PFNMAP vma, we don't
24 * touch page structures and the caller must make sure pfns aren't reused for
25 * anything else while he is using them.
27 * The function returns number of pages mapped which may be less than
28 * @nr_frames. In particular we stop mapping if there are more vmas of
29 * different type underlying the specified range of virtual addresses.
30 * When the function isn't able to map a single page, it returns error.
32 * Note that get_vaddr_frames() cannot follow VM_IO mappings. It used
33 * to be able to do that, but that could (racily) return non-refcounted
36 * This function takes care of grabbing mmap_lock as necessary.
38 int get_vaddr_frames(unsigned long start, unsigned int nr_frames,
39 unsigned int gup_flags, struct frame_vector *vec)
41 struct mm_struct *mm = current->mm;
42 struct vm_area_struct *vma;
49 if (WARN_ON_ONCE(nr_frames > vec->nr_allocated))
50 nr_frames = vec->nr_allocated;
52 start = untagged_addr(start);
56 vma = find_vma_intersection(mm, start, start + 1);
63 * While get_vaddr_frames() could be used for transient (kernel
64 * controlled lifetime) pinning of memory pages all current
65 * users establish long term (userspace controlled lifetime)
66 * page pinning. Treat get_vaddr_frames() like
67 * get_user_pages_longterm() and disallow it for filesystem-dax
70 if (vma_is_fsdax(vma)) {
75 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) {
78 ret = pin_user_pages_locked(start, nr_frames,
79 gup_flags, (struct page **)(vec->ptrs), &locked);
95 EXPORT_SYMBOL(get_vaddr_frames);
98 * put_vaddr_frames() - drop references to pages if get_vaddr_frames() acquired
100 * @vec: frame vector to put
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().
106 void put_vaddr_frames(struct frame_vector *vec)
112 pages = frame_vector_pages(vec);
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...
118 if (WARN_ON(IS_ERR(pages)))
121 unpin_user_pages(pages, vec->nr_frames);
122 vec->got_ref = false;
126 EXPORT_SYMBOL(put_vaddr_frames);
129 * frame_vector_to_pages - convert frame vector to contain page pointers
130 * @vec: frame vector to convert
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.
136 int frame_vector_to_pages(struct frame_vector *vec)
144 nums = frame_vector_pfns(vec);
145 for (i = 0; i < vec->nr_frames; i++)
146 if (!pfn_valid(nums[i]))
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;
154 EXPORT_SYMBOL(frame_vector_to_pages);
157 * frame_vector_to_pfns - convert frame vector to contain pfns
158 * @vec: frame vector to convert
160 * Convert @vec to contain array of pfns.
162 void frame_vector_to_pfns(struct frame_vector *vec)
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]);
176 EXPORT_SYMBOL(frame_vector_to_pfns);
179 * frame_vector_create() - allocate & initialize structure for pinned pfns
180 * @nr_frames: number of pfns slots we should reserve
182 * Allocate and initialize struct pinned_pfns to be able to hold @nr_pfns
185 struct frame_vector *frame_vector_create(unsigned int nr_frames)
187 struct frame_vector *vec;
188 int size = sizeof(struct frame_vector) + sizeof(void *) * nr_frames;
190 if (WARN_ON_ONCE(nr_frames == 0))
193 * This is absurdly high. It's here just to avoid strange effects when
194 * arithmetics overflows.
196 if (WARN_ON_ONCE(nr_frames > INT_MAX / sizeof(void *) / 2))
199 * Avoid higher order allocations, use vmalloc instead. It should
202 vec = kvmalloc(size, GFP_KERNEL);
205 vec->nr_allocated = nr_frames;
209 EXPORT_SYMBOL(frame_vector_create);
212 * frame_vector_destroy() - free memory allocated to carry frame vector
213 * @vec: Frame vector to free
215 * Free structure allocated by frame_vector_create() to carry frames.
217 void frame_vector_destroy(struct frame_vector *vec)
219 /* Make sure put_vaddr_frames() got called properly... */
220 VM_BUG_ON(vec->nr_frames > 0);
223 EXPORT_SYMBOL(frame_vector_destroy);