1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/cred.h>
3 #include <linux/device.h>
4 #include <linux/dma-buf.h>
5 #include <linux/highmem.h>
6 #include <linux/init.h>
7 #include <linux/kernel.h>
8 #include <linux/memfd.h>
9 #include <linux/miscdevice.h>
10 #include <linux/module.h>
11 #include <linux/shmem_fs.h>
12 #include <linux/slab.h>
13 #include <linux/udmabuf.h>
14
15 static const u32 list_limit = 1024; /* udmabuf_create_list->count limit */
16 static const size_t size_limit_mb = 64; /* total dmabuf size, in megabytes */
17
18 struct udmabuf {
19 pgoff_t pagecount;
20 struct page **pages;
21 struct sg_table *sg;
22 struct miscdevice *device;
23 };
24
udmabuf_vm_fault(struct vm_fault * vmf)25 static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf)
26 {
27 struct vm_area_struct *vma = vmf->vma;
28 struct udmabuf *ubuf = vma->vm_private_data;
29 pgoff_t pgoff = vmf->pgoff;
30
31 if (pgoff >= ubuf->pagecount)
32 return VM_FAULT_SIGBUS;
33 vmf->page = ubuf->pages[pgoff];
34 get_page(vmf->page);
35 return 0;
36 }
37
38 static const struct vm_operations_struct udmabuf_vm_ops = {
39 .fault = udmabuf_vm_fault,
40 };
41
mmap_udmabuf(struct dma_buf * buf,struct vm_area_struct * vma)42 static int mmap_udmabuf(struct dma_buf *buf, struct vm_area_struct *vma)
43 {
44 struct udmabuf *ubuf = buf->priv;
45
46 if ((vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) == 0)
47 return -EINVAL;
48
49 vma->vm_ops = &udmabuf_vm_ops;
50 vma->vm_private_data = ubuf;
51 return 0;
52 }
53
get_sg_table(struct device * dev,struct dma_buf * buf,enum dma_data_direction direction)54 static struct sg_table *get_sg_table(struct device *dev, struct dma_buf *buf,
55 enum dma_data_direction direction)
56 {
57 struct udmabuf *ubuf = buf->priv;
58 struct sg_table *sg;
59 int ret;
60
61 sg = kzalloc(sizeof(*sg), GFP_KERNEL);
62 if (!sg)
63 return ERR_PTR(-ENOMEM);
64 ret = sg_alloc_table_from_pages(sg, ubuf->pages, ubuf->pagecount,
65 0, ubuf->pagecount << PAGE_SHIFT,
66 GFP_KERNEL);
67 if (ret < 0)
68 goto err;
69 ret = dma_map_sgtable(dev, sg, direction, 0);
70 if (ret < 0)
71 goto err;
72 return sg;
73
74 err:
75 sg_free_table(sg);
76 kfree(sg);
77 return ERR_PTR(ret);
78 }
79
put_sg_table(struct device * dev,struct sg_table * sg,enum dma_data_direction direction)80 static void put_sg_table(struct device *dev, struct sg_table *sg,
81 enum dma_data_direction direction)
82 {
83 dma_unmap_sgtable(dev, sg, direction, 0);
84 sg_free_table(sg);
85 kfree(sg);
86 }
87
map_udmabuf(struct dma_buf_attachment * at,enum dma_data_direction direction)88 static struct sg_table *map_udmabuf(struct dma_buf_attachment *at,
89 enum dma_data_direction direction)
90 {
91 return get_sg_table(at->dev, at->dmabuf, direction);
92 }
93
unmap_udmabuf(struct dma_buf_attachment * at,struct sg_table * sg,enum dma_data_direction direction)94 static void unmap_udmabuf(struct dma_buf_attachment *at,
95 struct sg_table *sg,
96 enum dma_data_direction direction)
97 {
98 return put_sg_table(at->dev, sg, direction);
99 }
100
release_udmabuf(struct dma_buf * buf)101 static void release_udmabuf(struct dma_buf *buf)
102 {
103 struct udmabuf *ubuf = buf->priv;
104 struct device *dev = ubuf->device->this_device;
105 pgoff_t pg;
106
107 if (ubuf->sg)
108 put_sg_table(dev, ubuf->sg, DMA_BIDIRECTIONAL);
109
110 for (pg = 0; pg < ubuf->pagecount; pg++)
111 put_page(ubuf->pages[pg]);
112 kfree(ubuf->pages);
113 kfree(ubuf);
114 }
115
begin_cpu_udmabuf(struct dma_buf * buf,enum dma_data_direction direction)116 static int begin_cpu_udmabuf(struct dma_buf *buf,
117 enum dma_data_direction direction)
118 {
119 struct udmabuf *ubuf = buf->priv;
120 struct device *dev = ubuf->device->this_device;
121
122 if (!ubuf->sg) {
123 ubuf->sg = get_sg_table(dev, buf, direction);
124 if (IS_ERR(ubuf->sg))
125 return PTR_ERR(ubuf->sg);
126 } else {
127 dma_sync_sg_for_cpu(dev, ubuf->sg->sgl, ubuf->sg->nents,
128 direction);
129 }
130
131 return 0;
132 }
133
end_cpu_udmabuf(struct dma_buf * buf,enum dma_data_direction direction)134 static int end_cpu_udmabuf(struct dma_buf *buf,
135 enum dma_data_direction direction)
136 {
137 struct udmabuf *ubuf = buf->priv;
138 struct device *dev = ubuf->device->this_device;
139
140 if (!ubuf->sg)
141 return -EINVAL;
142
143 dma_sync_sg_for_device(dev, ubuf->sg->sgl, ubuf->sg->nents, direction);
144 return 0;
145 }
146
147 static const struct dma_buf_ops udmabuf_ops = {
148 .cache_sgt_mapping = true,
149 .map_dma_buf = map_udmabuf,
150 .unmap_dma_buf = unmap_udmabuf,
151 .release = release_udmabuf,
152 .mmap = mmap_udmabuf,
153 .begin_cpu_access = begin_cpu_udmabuf,
154 .end_cpu_access = end_cpu_udmabuf,
155 };
156
157 #define SEALS_WANTED (F_SEAL_SHRINK)
158 #define SEALS_DENIED (F_SEAL_WRITE)
159
udmabuf_create(struct miscdevice * device,struct udmabuf_create_list * head,struct udmabuf_create_item * list)160 static long udmabuf_create(struct miscdevice *device,
161 struct udmabuf_create_list *head,
162 struct udmabuf_create_item *list)
163 {
164 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
165 struct file *memfd = NULL;
166 struct udmabuf *ubuf;
167 struct dma_buf *buf;
168 pgoff_t pgoff, pgcnt, pgidx, pgbuf = 0, pglimit;
169 struct page *page;
170 int seals, ret = -EINVAL;
171 u32 i, flags;
172
173 ubuf = kzalloc(sizeof(*ubuf), GFP_KERNEL);
174 if (!ubuf)
175 return -ENOMEM;
176
177 pglimit = (size_limit_mb * 1024 * 1024) >> PAGE_SHIFT;
178 for (i = 0; i < head->count; i++) {
179 if (!IS_ALIGNED(list[i].offset, PAGE_SIZE))
180 goto err;
181 if (!IS_ALIGNED(list[i].size, PAGE_SIZE))
182 goto err;
183 ubuf->pagecount += list[i].size >> PAGE_SHIFT;
184 if (ubuf->pagecount > pglimit)
185 goto err;
186 }
187 ubuf->pages = kmalloc_array(ubuf->pagecount, sizeof(*ubuf->pages),
188 GFP_KERNEL);
189 if (!ubuf->pages) {
190 ret = -ENOMEM;
191 goto err;
192 }
193
194 pgbuf = 0;
195 for (i = 0; i < head->count; i++) {
196 ret = -EBADFD;
197 memfd = fget(list[i].memfd);
198 if (!memfd)
199 goto err;
200 if (!shmem_mapping(file_inode(memfd)->i_mapping))
201 goto err;
202 seals = memfd_fcntl(memfd, F_GET_SEALS, 0);
203 if (seals == -EINVAL)
204 goto err;
205 ret = -EINVAL;
206 if ((seals & SEALS_WANTED) != SEALS_WANTED ||
207 (seals & SEALS_DENIED) != 0)
208 goto err;
209 pgoff = list[i].offset >> PAGE_SHIFT;
210 pgcnt = list[i].size >> PAGE_SHIFT;
211 for (pgidx = 0; pgidx < pgcnt; pgidx++) {
212 page = shmem_read_mapping_page(
213 file_inode(memfd)->i_mapping, pgoff + pgidx);
214 if (IS_ERR(page)) {
215 ret = PTR_ERR(page);
216 goto err;
217 }
218 ubuf->pages[pgbuf++] = page;
219 }
220 fput(memfd);
221 memfd = NULL;
222 }
223
224 exp_info.ops = &udmabuf_ops;
225 exp_info.size = ubuf->pagecount << PAGE_SHIFT;
226 exp_info.priv = ubuf;
227 exp_info.flags = O_RDWR;
228
229 ubuf->device = device;
230 buf = dma_buf_export(&exp_info);
231 if (IS_ERR(buf)) {
232 ret = PTR_ERR(buf);
233 goto err;
234 }
235
236 flags = 0;
237 if (head->flags & UDMABUF_FLAGS_CLOEXEC)
238 flags |= O_CLOEXEC;
239 return dma_buf_fd(buf, flags);
240
241 err:
242 while (pgbuf > 0)
243 put_page(ubuf->pages[--pgbuf]);
244 if (memfd)
245 fput(memfd);
246 kfree(ubuf->pages);
247 kfree(ubuf);
248 return ret;
249 }
250
udmabuf_ioctl_create(struct file * filp,unsigned long arg)251 static long udmabuf_ioctl_create(struct file *filp, unsigned long arg)
252 {
253 struct udmabuf_create create;
254 struct udmabuf_create_list head;
255 struct udmabuf_create_item list;
256
257 if (copy_from_user(&create, (void __user *)arg,
258 sizeof(create)))
259 return -EFAULT;
260
261 head.flags = create.flags;
262 head.count = 1;
263 list.memfd = create.memfd;
264 list.offset = create.offset;
265 list.size = create.size;
266
267 return udmabuf_create(filp->private_data, &head, &list);
268 }
269
udmabuf_ioctl_create_list(struct file * filp,unsigned long arg)270 static long udmabuf_ioctl_create_list(struct file *filp, unsigned long arg)
271 {
272 struct udmabuf_create_list head;
273 struct udmabuf_create_item *list;
274 int ret = -EINVAL;
275 u32 lsize;
276
277 if (copy_from_user(&head, (void __user *)arg, sizeof(head)))
278 return -EFAULT;
279 if (head.count > list_limit)
280 return -EINVAL;
281 lsize = sizeof(struct udmabuf_create_item) * head.count;
282 list = memdup_user((void __user *)(arg + sizeof(head)), lsize);
283 if (IS_ERR(list))
284 return PTR_ERR(list);
285
286 ret = udmabuf_create(filp->private_data, &head, list);
287 kfree(list);
288 return ret;
289 }
290
udmabuf_ioctl(struct file * filp,unsigned int ioctl,unsigned long arg)291 static long udmabuf_ioctl(struct file *filp, unsigned int ioctl,
292 unsigned long arg)
293 {
294 long ret;
295
296 switch (ioctl) {
297 case UDMABUF_CREATE:
298 ret = udmabuf_ioctl_create(filp, arg);
299 break;
300 case UDMABUF_CREATE_LIST:
301 ret = udmabuf_ioctl_create_list(filp, arg);
302 break;
303 default:
304 ret = -ENOTTY;
305 break;
306 }
307 return ret;
308 }
309
310 static const struct file_operations udmabuf_fops = {
311 .owner = THIS_MODULE,
312 .unlocked_ioctl = udmabuf_ioctl,
313 #ifdef CONFIG_COMPAT
314 .compat_ioctl = udmabuf_ioctl,
315 #endif
316 };
317
318 static struct miscdevice udmabuf_misc = {
319 .minor = MISC_DYNAMIC_MINOR,
320 .name = "udmabuf",
321 .fops = &udmabuf_fops,
322 };
323
udmabuf_dev_init(void)324 static int __init udmabuf_dev_init(void)
325 {
326 return misc_register(&udmabuf_misc);
327 }
328
udmabuf_dev_exit(void)329 static void __exit udmabuf_dev_exit(void)
330 {
331 misc_deregister(&udmabuf_misc);
332 }
333
334 module_init(udmabuf_dev_init)
335 module_exit(udmabuf_dev_exit)
336
337 MODULE_AUTHOR("Gerd Hoffmann <kraxel@redhat.com>");
338 MODULE_LICENSE("GPL v2");
339