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;
48 if (WARN_ON_ONCE(nr_frames > vec->nr_allocated))
49 nr_frames = vec->nr_allocated;
51 down_read(&mm->mmap_sem);
53 vma = find_vma_intersection(mm, start, start + 1);
58 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) {
61 ret = get_user_pages_locked(start, nr_frames,
62 write, force, (struct page **)(vec->ptrs), &locked);
69 unsigned long *nums = frame_vector_pfns(vec);
71 while (ret < nr_frames && start + PAGE_SIZE <= vma->vm_end) {
72 err = follow_pfn(vma, start, &nums[ret]);
82 * We stop if we have enough pages or if VMA doesn't completely
83 * cover the tail page.
85 if (ret >= nr_frames || start < vma->vm_end)
87 vma = find_vma_intersection(mm, start, start + 1);
88 } while (vma && vma->vm_flags & (VM_IO | VM_PFNMAP));
91 up_read(&mm->mmap_sem);
98 EXPORT_SYMBOL(get_vaddr_frames);
101 * put_vaddr_frames() - drop references to pages if get_vaddr_frames() acquired
103 * @vec: frame vector to put
105 * Drop references to pages if get_vaddr_frames() acquired them. We also
106 * invalidate the frame vector so that it is prepared for the next call into
107 * get_vaddr_frames().
109 void put_vaddr_frames(struct frame_vector *vec)
116 pages = frame_vector_pages(vec);
118 * frame_vector_pages() might needed to do a conversion when
119 * get_vaddr_frames() got pages but vec was later converted to pfns.
120 * But it shouldn't really fail to convert pfns back...
122 if (WARN_ON(IS_ERR(pages)))
124 for (i = 0; i < vec->nr_frames; i++)
126 vec->got_ref = false;
130 EXPORT_SYMBOL(put_vaddr_frames);
133 * frame_vector_to_pages - convert frame vector to contain page pointers
134 * @vec: frame vector to convert
136 * Convert @vec to contain array of page pointers. If the conversion is
137 * successful, return 0. Otherwise return an error. Note that we do not grab
138 * page references for the page structures.
140 int frame_vector_to_pages(struct frame_vector *vec)
148 nums = frame_vector_pfns(vec);
149 for (i = 0; i < vec->nr_frames; i++)
150 if (!pfn_valid(nums[i]))
152 pages = (struct page **)nums;
153 for (i = 0; i < vec->nr_frames; i++)
154 pages[i] = pfn_to_page(nums[i]);
155 vec->is_pfns = false;
158 EXPORT_SYMBOL(frame_vector_to_pages);
161 * frame_vector_to_pfns - convert frame vector to contain pfns
162 * @vec: frame vector to convert
164 * Convert @vec to contain array of pfns.
166 void frame_vector_to_pfns(struct frame_vector *vec)
174 pages = (struct page **)(vec->ptrs);
175 nums = (unsigned long *)pages;
176 for (i = 0; i < vec->nr_frames; i++)
177 nums[i] = page_to_pfn(pages[i]);
180 EXPORT_SYMBOL(frame_vector_to_pfns);
183 * frame_vector_create() - allocate & initialize structure for pinned pfns
184 * @nr_frames: number of pfns slots we should reserve
186 * Allocate and initialize struct pinned_pfns to be able to hold @nr_pfns
189 struct frame_vector *frame_vector_create(unsigned int nr_frames)
191 struct frame_vector *vec;
192 int size = sizeof(struct frame_vector) + sizeof(void *) * nr_frames;
194 if (WARN_ON_ONCE(nr_frames == 0))
197 * This is absurdly high. It's here just to avoid strange effects when
198 * arithmetics overflows.
200 if (WARN_ON_ONCE(nr_frames > INT_MAX / sizeof(void *) / 2))
203 * Avoid higher order allocations, use vmalloc instead. It should
206 if (size <= PAGE_SIZE)
207 vec = kmalloc(size, GFP_KERNEL);
212 vec->nr_allocated = nr_frames;
216 EXPORT_SYMBOL(frame_vector_create);
219 * frame_vector_destroy() - free memory allocated to carry frame vector
220 * @vec: Frame vector to free
222 * Free structure allocated by frame_vector_create() to carry frames.
224 void frame_vector_destroy(struct frame_vector *vec)
226 /* Make sure put_vaddr_frames() got called properly... */
227 VM_BUG_ON(vec->nr_frames > 0);
230 EXPORT_SYMBOL(frame_vector_destroy);