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 #include <linux/hugetlb.h>
16 static int list_limit = 1024;
17 module_param(list_limit, int, 0644);
18 MODULE_PARM_DESC(list_limit, "udmabuf_create_list->count limit. Default is 1024.");
20 static int size_limit_mb = 64;
21 module_param(size_limit_mb, int, 0644);
22 MODULE_PARM_DESC(size_limit_mb, "Max size of a dmabuf, in megabytes. Default is 64.");
28 struct miscdevice *device;
31 static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf)
33 struct vm_area_struct *vma = vmf->vma;
34 struct udmabuf *ubuf = vma->vm_private_data;
36 vmf->page = ubuf->pages[vmf->pgoff];
41 static const struct vm_operations_struct udmabuf_vm_ops = {
42 .fault = udmabuf_vm_fault,
45 static int mmap_udmabuf(struct dma_buf *buf, struct vm_area_struct *vma)
47 struct udmabuf *ubuf = buf->priv;
49 if ((vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) == 0)
52 vma->vm_ops = &udmabuf_vm_ops;
53 vma->vm_private_data = ubuf;
57 static struct sg_table *get_sg_table(struct device *dev, struct dma_buf *buf,
58 enum dma_data_direction direction)
60 struct udmabuf *ubuf = buf->priv;
64 sg = kzalloc(sizeof(*sg), GFP_KERNEL);
66 return ERR_PTR(-ENOMEM);
67 ret = sg_alloc_table_from_pages(sg, ubuf->pages, ubuf->pagecount,
68 0, ubuf->pagecount << PAGE_SHIFT,
72 ret = dma_map_sgtable(dev, sg, direction, 0);
83 static void put_sg_table(struct device *dev, struct sg_table *sg,
84 enum dma_data_direction direction)
86 dma_unmap_sgtable(dev, sg, direction, 0);
91 static struct sg_table *map_udmabuf(struct dma_buf_attachment *at,
92 enum dma_data_direction direction)
94 return get_sg_table(at->dev, at->dmabuf, direction);
97 static void unmap_udmabuf(struct dma_buf_attachment *at,
99 enum dma_data_direction direction)
101 return put_sg_table(at->dev, sg, direction);
104 static void release_udmabuf(struct dma_buf *buf)
106 struct udmabuf *ubuf = buf->priv;
107 struct device *dev = ubuf->device->this_device;
111 put_sg_table(dev, ubuf->sg, DMA_BIDIRECTIONAL);
113 for (pg = 0; pg < ubuf->pagecount; pg++)
114 put_page(ubuf->pages[pg]);
119 static int begin_cpu_udmabuf(struct dma_buf *buf,
120 enum dma_data_direction direction)
122 struct udmabuf *ubuf = buf->priv;
123 struct device *dev = ubuf->device->this_device;
126 ubuf->sg = get_sg_table(dev, buf, direction);
127 if (IS_ERR(ubuf->sg))
128 return PTR_ERR(ubuf->sg);
130 dma_sync_sg_for_cpu(dev, ubuf->sg->sgl, ubuf->sg->nents,
137 static int end_cpu_udmabuf(struct dma_buf *buf,
138 enum dma_data_direction direction)
140 struct udmabuf *ubuf = buf->priv;
141 struct device *dev = ubuf->device->this_device;
146 dma_sync_sg_for_device(dev, ubuf->sg->sgl, ubuf->sg->nents, direction);
150 static const struct dma_buf_ops udmabuf_ops = {
151 .cache_sgt_mapping = true,
152 .map_dma_buf = map_udmabuf,
153 .unmap_dma_buf = unmap_udmabuf,
154 .release = release_udmabuf,
155 .mmap = mmap_udmabuf,
156 .begin_cpu_access = begin_cpu_udmabuf,
157 .end_cpu_access = end_cpu_udmabuf,
160 #define SEALS_WANTED (F_SEAL_SHRINK)
161 #define SEALS_DENIED (F_SEAL_WRITE)
163 static long udmabuf_create(struct miscdevice *device,
164 struct udmabuf_create_list *head,
165 struct udmabuf_create_item *list)
167 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
168 struct file *memfd = NULL;
169 struct address_space *mapping = NULL;
170 struct udmabuf *ubuf;
172 pgoff_t pgoff, pgcnt, pgidx, pgbuf = 0, pglimit;
173 struct page *page, *hpage = NULL;
174 pgoff_t subpgoff, maxsubpgs;
175 struct hstate *hpstate;
176 int seals, ret = -EINVAL;
179 ubuf = kzalloc(sizeof(*ubuf), GFP_KERNEL);
183 pglimit = (size_limit_mb * 1024 * 1024) >> PAGE_SHIFT;
184 for (i = 0; i < head->count; i++) {
185 if (!IS_ALIGNED(list[i].offset, PAGE_SIZE))
187 if (!IS_ALIGNED(list[i].size, PAGE_SIZE))
189 ubuf->pagecount += list[i].size >> PAGE_SHIFT;
190 if (ubuf->pagecount > pglimit)
193 ubuf->pages = kmalloc_array(ubuf->pagecount, sizeof(*ubuf->pages),
201 for (i = 0; i < head->count; i++) {
203 memfd = fget(list[i].memfd);
206 mapping = file_inode(memfd)->i_mapping;
207 if (!shmem_mapping(mapping) && !is_file_hugepages(memfd))
209 seals = memfd_fcntl(memfd, F_GET_SEALS, 0);
210 if (seals == -EINVAL)
213 if ((seals & SEALS_WANTED) != SEALS_WANTED ||
214 (seals & SEALS_DENIED) != 0)
216 pgoff = list[i].offset >> PAGE_SHIFT;
217 pgcnt = list[i].size >> PAGE_SHIFT;
218 if (is_file_hugepages(memfd)) {
219 hpstate = hstate_file(memfd);
220 pgoff = list[i].offset >> huge_page_shift(hpstate);
221 subpgoff = (list[i].offset &
222 ~huge_page_mask(hpstate)) >> PAGE_SHIFT;
223 maxsubpgs = huge_page_size(hpstate) >> PAGE_SHIFT;
225 for (pgidx = 0; pgidx < pgcnt; pgidx++) {
226 if (is_file_hugepages(memfd)) {
228 hpage = find_get_page_flags(mapping, pgoff,
235 page = hpage + subpgoff;
238 if (subpgoff == maxsubpgs) {
245 page = shmem_read_mapping_page(mapping,
252 ubuf->pages[pgbuf++] = page;
262 exp_info.ops = &udmabuf_ops;
263 exp_info.size = ubuf->pagecount << PAGE_SHIFT;
264 exp_info.priv = ubuf;
265 exp_info.flags = O_RDWR;
267 ubuf->device = device;
268 buf = dma_buf_export(&exp_info);
275 if (head->flags & UDMABUF_FLAGS_CLOEXEC)
277 return dma_buf_fd(buf, flags);
281 put_page(ubuf->pages[--pgbuf]);
289 static long udmabuf_ioctl_create(struct file *filp, unsigned long arg)
291 struct udmabuf_create create;
292 struct udmabuf_create_list head;
293 struct udmabuf_create_item list;
295 if (copy_from_user(&create, (void __user *)arg,
299 head.flags = create.flags;
301 list.memfd = create.memfd;
302 list.offset = create.offset;
303 list.size = create.size;
305 return udmabuf_create(filp->private_data, &head, &list);
308 static long udmabuf_ioctl_create_list(struct file *filp, unsigned long arg)
310 struct udmabuf_create_list head;
311 struct udmabuf_create_item *list;
315 if (copy_from_user(&head, (void __user *)arg, sizeof(head)))
317 if (head.count > list_limit)
319 lsize = sizeof(struct udmabuf_create_item) * head.count;
320 list = memdup_user((void __user *)(arg + sizeof(head)), lsize);
322 return PTR_ERR(list);
324 ret = udmabuf_create(filp->private_data, &head, list);
329 static long udmabuf_ioctl(struct file *filp, unsigned int ioctl,
336 ret = udmabuf_ioctl_create(filp, arg);
338 case UDMABUF_CREATE_LIST:
339 ret = udmabuf_ioctl_create_list(filp, arg);
348 static const struct file_operations udmabuf_fops = {
349 .owner = THIS_MODULE,
350 .unlocked_ioctl = udmabuf_ioctl,
352 .compat_ioctl = udmabuf_ioctl,
356 static struct miscdevice udmabuf_misc = {
357 .minor = MISC_DYNAMIC_MINOR,
359 .fops = &udmabuf_fops,
362 static int __init udmabuf_dev_init(void)
364 return misc_register(&udmabuf_misc);
367 static void __exit udmabuf_dev_exit(void)
369 misc_deregister(&udmabuf_misc);
372 module_init(udmabuf_dev_init)
373 module_exit(udmabuf_dev_exit)
375 MODULE_AUTHOR("Gerd Hoffmann <kraxel@redhat.com>");
376 MODULE_LICENSE("GPL v2");