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/dma-resv.h>
6 #include <linux/highmem.h>
7 #include <linux/init.h>
8 #include <linux/kernel.h>
9 #include <linux/memfd.h>
10 #include <linux/miscdevice.h>
11 #include <linux/module.h>
12 #include <linux/shmem_fs.h>
13 #include <linux/hugetlb.h>
14 #include <linux/slab.h>
15 #include <linux/udmabuf.h>
16 #include <linux/vmalloc.h>
17 #include <linux/iosys-map.h>
19 static int list_limit = 1024;
20 module_param(list_limit, int, 0644);
21 MODULE_PARM_DESC(list_limit, "udmabuf_create_list->count limit. Default is 1024.");
23 static int size_limit_mb = 64;
24 module_param(size_limit_mb, int, 0644);
25 MODULE_PARM_DESC(size_limit_mb, "Max size of a dmabuf, in megabytes. Default is 64.");
29 struct folio **folios;
31 struct miscdevice *device;
33 struct list_head unpin_list;
36 struct udmabuf_folio {
38 struct list_head list;
41 static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf)
43 struct vm_area_struct *vma = vmf->vma;
44 struct udmabuf *ubuf = vma->vm_private_data;
45 pgoff_t pgoff = vmf->pgoff;
48 if (pgoff >= ubuf->pagecount)
49 return VM_FAULT_SIGBUS;
51 pfn = folio_pfn(ubuf->folios[pgoff]);
52 pfn += ubuf->offsets[pgoff] >> PAGE_SHIFT;
54 return vmf_insert_pfn(vma, vmf->address, pfn);
57 static const struct vm_operations_struct udmabuf_vm_ops = {
58 .fault = udmabuf_vm_fault,
61 static int mmap_udmabuf(struct dma_buf *buf, struct vm_area_struct *vma)
63 struct udmabuf *ubuf = buf->priv;
65 if ((vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) == 0)
68 vma->vm_ops = &udmabuf_vm_ops;
69 vma->vm_private_data = ubuf;
70 vm_flags_set(vma, VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP);
74 static int vmap_udmabuf(struct dma_buf *buf, struct iosys_map *map)
76 struct udmabuf *ubuf = buf->priv;
81 dma_resv_assert_held(buf->resv);
83 pages = kmalloc_array(ubuf->pagecount, sizeof(*pages), GFP_KERNEL);
87 for (pg = 0; pg < ubuf->pagecount; pg++)
88 pages[pg] = &ubuf->folios[pg]->page;
90 vaddr = vm_map_ram(pages, ubuf->pagecount, -1);
95 iosys_map_set_vaddr(map, vaddr);
99 static void vunmap_udmabuf(struct dma_buf *buf, struct iosys_map *map)
101 struct udmabuf *ubuf = buf->priv;
103 dma_resv_assert_held(buf->resv);
105 vm_unmap_ram(map->vaddr, ubuf->pagecount);
108 static struct sg_table *get_sg_table(struct device *dev, struct dma_buf *buf,
109 enum dma_data_direction direction)
111 struct udmabuf *ubuf = buf->priv;
113 struct scatterlist *sgl;
117 sg = kzalloc(sizeof(*sg), GFP_KERNEL);
119 return ERR_PTR(-ENOMEM);
121 ret = sg_alloc_table(sg, ubuf->pagecount, GFP_KERNEL);
125 for_each_sg(sg->sgl, sgl, ubuf->pagecount, i)
126 sg_set_folio(sgl, ubuf->folios[i], PAGE_SIZE,
129 ret = dma_map_sgtable(dev, sg, direction, 0);
141 static void put_sg_table(struct device *dev, struct sg_table *sg,
142 enum dma_data_direction direction)
144 dma_unmap_sgtable(dev, sg, direction, 0);
149 static struct sg_table *map_udmabuf(struct dma_buf_attachment *at,
150 enum dma_data_direction direction)
152 return get_sg_table(at->dev, at->dmabuf, direction);
155 static void unmap_udmabuf(struct dma_buf_attachment *at,
157 enum dma_data_direction direction)
159 return put_sg_table(at->dev, sg, direction);
162 static void unpin_all_folios(struct list_head *unpin_list)
164 struct udmabuf_folio *ubuf_folio;
166 while (!list_empty(unpin_list)) {
167 ubuf_folio = list_first_entry(unpin_list,
168 struct udmabuf_folio, list);
169 unpin_folio(ubuf_folio->folio);
171 list_del(&ubuf_folio->list);
176 static int add_to_unpin_list(struct list_head *unpin_list,
179 struct udmabuf_folio *ubuf_folio;
181 ubuf_folio = kzalloc(sizeof(*ubuf_folio), GFP_KERNEL);
185 ubuf_folio->folio = folio;
186 list_add_tail(&ubuf_folio->list, unpin_list);
190 static void release_udmabuf(struct dma_buf *buf)
192 struct udmabuf *ubuf = buf->priv;
193 struct device *dev = ubuf->device->this_device;
196 put_sg_table(dev, ubuf->sg, DMA_BIDIRECTIONAL);
198 unpin_all_folios(&ubuf->unpin_list);
199 kfree(ubuf->offsets);
204 static int begin_cpu_udmabuf(struct dma_buf *buf,
205 enum dma_data_direction direction)
207 struct udmabuf *ubuf = buf->priv;
208 struct device *dev = ubuf->device->this_device;
212 ubuf->sg = get_sg_table(dev, buf, direction);
213 if (IS_ERR(ubuf->sg)) {
214 ret = PTR_ERR(ubuf->sg);
218 dma_sync_sg_for_cpu(dev, ubuf->sg->sgl, ubuf->sg->nents,
225 static int end_cpu_udmabuf(struct dma_buf *buf,
226 enum dma_data_direction direction)
228 struct udmabuf *ubuf = buf->priv;
229 struct device *dev = ubuf->device->this_device;
234 dma_sync_sg_for_device(dev, ubuf->sg->sgl, ubuf->sg->nents, direction);
238 static const struct dma_buf_ops udmabuf_ops = {
239 .cache_sgt_mapping = true,
240 .map_dma_buf = map_udmabuf,
241 .unmap_dma_buf = unmap_udmabuf,
242 .release = release_udmabuf,
243 .mmap = mmap_udmabuf,
244 .vmap = vmap_udmabuf,
245 .vunmap = vunmap_udmabuf,
246 .begin_cpu_access = begin_cpu_udmabuf,
247 .end_cpu_access = end_cpu_udmabuf,
250 #define SEALS_WANTED (F_SEAL_SHRINK)
251 #define SEALS_DENIED (F_SEAL_WRITE)
253 static int check_memfd_seals(struct file *memfd)
260 if (!shmem_file(memfd) && !is_file_hugepages(memfd))
263 seals = memfd_fcntl(memfd, F_GET_SEALS, 0);
264 if (seals == -EINVAL)
267 if ((seals & SEALS_WANTED) != SEALS_WANTED ||
268 (seals & SEALS_DENIED) != 0)
274 static int export_udmabuf(struct udmabuf *ubuf,
275 struct miscdevice *device,
278 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
281 ubuf->device = device;
282 exp_info.ops = &udmabuf_ops;
283 exp_info.size = ubuf->pagecount << PAGE_SHIFT;
284 exp_info.priv = ubuf;
285 exp_info.flags = O_RDWR;
287 buf = dma_buf_export(&exp_info);
291 return dma_buf_fd(buf, flags);
294 static long udmabuf_create(struct miscdevice *device,
295 struct udmabuf_create_list *head,
296 struct udmabuf_create_item *list)
298 pgoff_t pgoff, pgcnt, pglimit, pgbuf = 0;
299 long nr_folios, ret = -EINVAL;
300 struct file *memfd = NULL;
301 struct folio **folios;
302 struct udmabuf *ubuf;
306 ubuf = kzalloc(sizeof(*ubuf), GFP_KERNEL);
310 INIT_LIST_HEAD(&ubuf->unpin_list);
311 pglimit = (size_limit_mb * 1024 * 1024) >> PAGE_SHIFT;
312 for (i = 0; i < head->count; i++) {
313 if (!IS_ALIGNED(list[i].offset, PAGE_SIZE))
315 if (!IS_ALIGNED(list[i].size, PAGE_SIZE))
317 ubuf->pagecount += list[i].size >> PAGE_SHIFT;
318 if (ubuf->pagecount > pglimit)
322 if (!ubuf->pagecount)
325 ubuf->folios = kmalloc_array(ubuf->pagecount, sizeof(*ubuf->folios),
331 ubuf->offsets = kcalloc(ubuf->pagecount, sizeof(*ubuf->offsets),
333 if (!ubuf->offsets) {
339 for (i = 0; i < head->count; i++) {
340 memfd = fget(list[i].memfd);
341 ret = check_memfd_seals(memfd);
345 pgcnt = list[i].size >> PAGE_SHIFT;
346 folios = kmalloc_array(pgcnt, sizeof(*folios), GFP_KERNEL);
352 end = list[i].offset + (pgcnt << PAGE_SHIFT) - 1;
353 ret = memfd_pin_folios(memfd, list[i].offset, end,
354 folios, pgcnt, &pgoff);
363 pgoff >>= PAGE_SHIFT;
364 for (j = 0, k = 0; j < pgcnt; j++) {
365 ubuf->folios[pgbuf] = folios[k];
366 ubuf->offsets[pgbuf] = pgoff << PAGE_SHIFT;
368 if (j == 0 || ubuf->folios[pgbuf-1] != folios[k]) {
369 ret = add_to_unpin_list(&ubuf->unpin_list,
378 if (++pgoff == folio_nr_pages(folios[k])) {
380 if (++k == nr_folios)
390 flags = head->flags & UDMABUF_FLAGS_CLOEXEC ? O_CLOEXEC : 0;
391 ret = export_udmabuf(ubuf, device, flags);
400 unpin_all_folios(&ubuf->unpin_list);
401 kfree(ubuf->offsets);
407 static long udmabuf_ioctl_create(struct file *filp, unsigned long arg)
409 struct udmabuf_create create;
410 struct udmabuf_create_list head;
411 struct udmabuf_create_item list;
413 if (copy_from_user(&create, (void __user *)arg,
417 head.flags = create.flags;
419 list.memfd = create.memfd;
420 list.offset = create.offset;
421 list.size = create.size;
423 return udmabuf_create(filp->private_data, &head, &list);
426 static long udmabuf_ioctl_create_list(struct file *filp, unsigned long arg)
428 struct udmabuf_create_list head;
429 struct udmabuf_create_item *list;
433 if (copy_from_user(&head, (void __user *)arg, sizeof(head)))
435 if (head.count > list_limit)
437 lsize = sizeof(struct udmabuf_create_item) * head.count;
438 list = memdup_user((void __user *)(arg + sizeof(head)), lsize);
440 return PTR_ERR(list);
442 ret = udmabuf_create(filp->private_data, &head, list);
447 static long udmabuf_ioctl(struct file *filp, unsigned int ioctl,
454 ret = udmabuf_ioctl_create(filp, arg);
456 case UDMABUF_CREATE_LIST:
457 ret = udmabuf_ioctl_create_list(filp, arg);
466 static const struct file_operations udmabuf_fops = {
467 .owner = THIS_MODULE,
468 .unlocked_ioctl = udmabuf_ioctl,
470 .compat_ioctl = udmabuf_ioctl,
474 static struct miscdevice udmabuf_misc = {
475 .minor = MISC_DYNAMIC_MINOR,
477 .fops = &udmabuf_fops,
480 static int __init udmabuf_dev_init(void)
484 ret = misc_register(&udmabuf_misc);
486 pr_err("Could not initialize udmabuf device\n");
490 ret = dma_coerce_mask_and_coherent(udmabuf_misc.this_device,
493 pr_err("Could not setup DMA mask for udmabuf device\n");
494 misc_deregister(&udmabuf_misc);
501 static void __exit udmabuf_dev_exit(void)
503 misc_deregister(&udmabuf_misc);
506 module_init(udmabuf_dev_init)
507 module_exit(udmabuf_dev_exit)