GNU Linux-libre 4.19.268-gnu1
[releases.git] / mm / frame_vector.c
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 /**
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.
18  *
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.
26  *
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.
31  *
32  * This function takes care of grabbing mmap_sem as necessary.
33  */
34 int get_vaddr_frames(unsigned long start, unsigned int nr_frames,
35                      unsigned int gup_flags, struct frame_vector *vec)
36 {
37         struct mm_struct *mm = current->mm;
38         struct vm_area_struct *vma;
39         int ret = 0;
40         int locked;
41
42         if (nr_frames == 0)
43                 return 0;
44
45         if (WARN_ON_ONCE(nr_frames > vec->nr_allocated))
46                 nr_frames = vec->nr_allocated;
47
48         down_read(&mm->mmap_sem);
49         locked = 1;
50         vma = find_vma_intersection(mm, start, start + 1);
51         if (!vma) {
52                 ret = -EFAULT;
53                 goto out;
54         }
55
56         /*
57          * While get_vaddr_frames() could be used for transient (kernel
58          * controlled lifetime) pinning of memory pages all current
59          * users establish long term (userspace controlled lifetime)
60          * page pinning. Treat get_vaddr_frames() like
61          * get_user_pages_longterm() and disallow it for filesystem-dax
62          * mappings.
63          */
64         if (vma_is_fsdax(vma)) {
65                 ret = -EOPNOTSUPP;
66                 goto out;
67         }
68
69         if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) {
70                 vec->got_ref = true;
71                 vec->is_pfns = false;
72                 ret = get_user_pages_locked(start, nr_frames,
73                         gup_flags, (struct page **)(vec->ptrs), &locked);
74                 if (likely(ret > 0))
75                         goto out;
76         }
77
78         /* This used to (racily) return non-refcounted pfns. Let people know */
79         WARN_ONCE(1, "get_vaddr_frames() cannot follow VM_IO mapping");
80         vec->nr_frames = 0;
81
82 out:
83         if (locked)
84                 up_read(&mm->mmap_sem);
85         if (!ret)
86                 ret = -EFAULT;
87         if (ret > 0)
88                 vec->nr_frames = ret;
89         return ret;
90 }
91 EXPORT_SYMBOL(get_vaddr_frames);
92
93 /**
94  * put_vaddr_frames() - drop references to pages if get_vaddr_frames() acquired
95  *                      them
96  * @vec:        frame vector to put
97  *
98  * Drop references to pages if get_vaddr_frames() acquired them. We also
99  * invalidate the frame vector so that it is prepared for the next call into
100  * get_vaddr_frames().
101  */
102 void put_vaddr_frames(struct frame_vector *vec)
103 {
104         int i;
105         struct page **pages;
106
107         if (!vec->got_ref)
108                 goto out;
109         pages = frame_vector_pages(vec);
110         /*
111          * frame_vector_pages() might needed to do a conversion when
112          * get_vaddr_frames() got pages but vec was later converted to pfns.
113          * But it shouldn't really fail to convert pfns back...
114          */
115         if (WARN_ON(IS_ERR(pages)))
116                 goto out;
117         for (i = 0; i < vec->nr_frames; i++)
118                 put_page(pages[i]);
119         vec->got_ref = false;
120 out:
121         vec->nr_frames = 0;
122 }
123 EXPORT_SYMBOL(put_vaddr_frames);
124
125 /**
126  * frame_vector_to_pages - convert frame vector to contain page pointers
127  * @vec:        frame vector to convert
128  *
129  * Convert @vec to contain array of page pointers.  If the conversion is
130  * successful, return 0. Otherwise return an error. Note that we do not grab
131  * page references for the page structures.
132  */
133 int frame_vector_to_pages(struct frame_vector *vec)
134 {
135         int i;
136         unsigned long *nums;
137         struct page **pages;
138
139         if (!vec->is_pfns)
140                 return 0;
141         nums = frame_vector_pfns(vec);
142         for (i = 0; i < vec->nr_frames; i++)
143                 if (!pfn_valid(nums[i]))
144                         return -EINVAL;
145         pages = (struct page **)nums;
146         for (i = 0; i < vec->nr_frames; i++)
147                 pages[i] = pfn_to_page(nums[i]);
148         vec->is_pfns = false;
149         return 0;
150 }
151 EXPORT_SYMBOL(frame_vector_to_pages);
152
153 /**
154  * frame_vector_to_pfns - convert frame vector to contain pfns
155  * @vec:        frame vector to convert
156  *
157  * Convert @vec to contain array of pfns.
158  */
159 void frame_vector_to_pfns(struct frame_vector *vec)
160 {
161         int i;
162         unsigned long *nums;
163         struct page **pages;
164
165         if (vec->is_pfns)
166                 return;
167         pages = (struct page **)(vec->ptrs);
168         nums = (unsigned long *)pages;
169         for (i = 0; i < vec->nr_frames; i++)
170                 nums[i] = page_to_pfn(pages[i]);
171         vec->is_pfns = true;
172 }
173 EXPORT_SYMBOL(frame_vector_to_pfns);
174
175 /**
176  * frame_vector_create() - allocate & initialize structure for pinned pfns
177  * @nr_frames:  number of pfns slots we should reserve
178  *
179  * Allocate and initialize struct pinned_pfns to be able to hold @nr_pfns
180  * pfns.
181  */
182 struct frame_vector *frame_vector_create(unsigned int nr_frames)
183 {
184         struct frame_vector *vec;
185         int size = sizeof(struct frame_vector) + sizeof(void *) * nr_frames;
186
187         if (WARN_ON_ONCE(nr_frames == 0))
188                 return NULL;
189         /*
190          * This is absurdly high. It's here just to avoid strange effects when
191          * arithmetics overflows.
192          */
193         if (WARN_ON_ONCE(nr_frames > INT_MAX / sizeof(void *) / 2))
194                 return NULL;
195         /*
196          * Avoid higher order allocations, use vmalloc instead. It should
197          * be rare anyway.
198          */
199         vec = kvmalloc(size, GFP_KERNEL);
200         if (!vec)
201                 return NULL;
202         vec->nr_allocated = nr_frames;
203         vec->nr_frames = 0;
204         return vec;
205 }
206 EXPORT_SYMBOL(frame_vector_create);
207
208 /**
209  * frame_vector_destroy() - free memory allocated to carry frame vector
210  * @vec:        Frame vector to free
211  *
212  * Free structure allocated by frame_vector_create() to carry frames.
213  */
214 void frame_vector_destroy(struct frame_vector *vec)
215 {
216         /* Make sure put_vaddr_frames() got called properly... */
217         VM_BUG_ON(vec->nr_frames > 0);
218         kvfree(vec);
219 }
220 EXPORT_SYMBOL(frame_vector_destroy);