blob: f0bf3ba7441e728bd25afd023a0fc5687c1dcdc3 [file] [log] [blame]
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +02001// SPDX-License-Identifier: GPL-2.0
Gerd Hoffmann913965c2018-09-11 15:42:04 +02002#include <linux/cred.h>
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +02003#include <linux/device.h>
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +02004#include <linux/dma-buf.h>
5#include <linux/highmem.h>
Gerd Hoffmann913965c2018-09-11 15:42:04 +02006#include <linux/init.h>
7#include <linux/kernel.h>
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +02008#include <linux/memfd.h>
Gerd Hoffmann913965c2018-09-11 15:42:04 +02009#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>
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +020014
Gerd Hoffmanndc4716d2018-09-11 15:42:10 +020015static const u32 list_limit = 1024; /* udmabuf_create_list->count limit */
16static const size_t size_limit_mb = 64; /* total dmabuf size, in megabytes */
17
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +020018struct udmabuf {
Gerd Hoffmannb35f57c2018-09-11 15:42:06 +020019 pgoff_t pagecount;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +020020 struct page **pages;
Gurchetan Singhc1bbed62019-12-02 17:36:25 -080021 struct miscdevice *device;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +020022};
23
Souptick Joarder300133d2019-01-03 15:26:34 -080024static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf)
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +020025{
26 struct vm_area_struct *vma = vmf->vma;
27 struct udmabuf *ubuf = vma->vm_private_data;
28
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +020029 vmf->page = ubuf->pages[vmf->pgoff];
30 get_page(vmf->page);
31 return 0;
32}
33
34static const struct vm_operations_struct udmabuf_vm_ops = {
35 .fault = udmabuf_vm_fault,
36};
37
38static int mmap_udmabuf(struct dma_buf *buf, struct vm_area_struct *vma)
39{
40 struct udmabuf *ubuf = buf->priv;
41
42 if ((vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) == 0)
43 return -EINVAL;
44
45 vma->vm_ops = &udmabuf_vm_ops;
46 vma->vm_private_data = ubuf;
47 return 0;
48}
49
50static struct sg_table *map_udmabuf(struct dma_buf_attachment *at,
51 enum dma_data_direction direction)
52{
53 struct udmabuf *ubuf = at->dmabuf->priv;
54 struct sg_table *sg;
Gerd Hoffmanna3e722d2018-09-11 15:42:05 +020055 int ret;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +020056
57 sg = kzalloc(sizeof(*sg), GFP_KERNEL);
58 if (!sg)
Gerd Hoffmanna3e722d2018-09-11 15:42:05 +020059 return ERR_PTR(-ENOMEM);
60 ret = sg_alloc_table_from_pages(sg, ubuf->pages, ubuf->pagecount,
61 0, ubuf->pagecount << PAGE_SHIFT,
62 GFP_KERNEL);
63 if (ret < 0)
64 goto err;
Dan Carpenter6f19eb22018-09-14 09:56:15 +030065 if (!dma_map_sg(at->dev, sg->sgl, sg->nents, direction)) {
66 ret = -EINVAL;
Gerd Hoffmanna3e722d2018-09-11 15:42:05 +020067 goto err;
Dan Carpenter6f19eb22018-09-14 09:56:15 +030068 }
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +020069 return sg;
70
Gerd Hoffmanna3e722d2018-09-11 15:42:05 +020071err:
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +020072 sg_free_table(sg);
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +020073 kfree(sg);
Gerd Hoffmanna3e722d2018-09-11 15:42:05 +020074 return ERR_PTR(ret);
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +020075}
76
77static void unmap_udmabuf(struct dma_buf_attachment *at,
78 struct sg_table *sg,
79 enum dma_data_direction direction)
80{
Lucas Stach283f1e32019-06-04 22:23:31 +020081 dma_unmap_sg(at->dev, sg->sgl, sg->nents, direction);
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +020082 sg_free_table(sg);
83 kfree(sg);
84}
85
86static void release_udmabuf(struct dma_buf *buf)
87{
88 struct udmabuf *ubuf = buf->priv;
89 pgoff_t pg;
90
91 for (pg = 0; pg < ubuf->pagecount; pg++)
92 put_page(ubuf->pages[pg]);
93 kfree(ubuf->pages);
94 kfree(ubuf);
95}
96
Gerd Hoffmanna3485282018-09-11 15:42:07 +020097static const struct dma_buf_ops udmabuf_ops = {
Gurchetan Singhbc7a71d2019-12-02 17:36:24 -080098 .cache_sgt_mapping = true,
99 .map_dma_buf = map_udmabuf,
100 .unmap_dma_buf = unmap_udmabuf,
101 .release = release_udmabuf,
102 .mmap = mmap_udmabuf,
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200103};
104
105#define SEALS_WANTED (F_SEAL_SHRINK)
106#define SEALS_DENIED (F_SEAL_WRITE)
107
Gurchetan Singhc1bbed62019-12-02 17:36:25 -0800108static long udmabuf_create(struct miscdevice *device,
109 struct udmabuf_create_list *head,
110 struct udmabuf_create_item *list)
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200111{
112 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
113 struct file *memfd = NULL;
114 struct udmabuf *ubuf;
115 struct dma_buf *buf;
Gerd Hoffmann0d174552018-09-11 15:42:11 +0200116 pgoff_t pgoff, pgcnt, pgidx, pgbuf = 0, pglimit;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200117 struct page *page;
118 int seals, ret = -EINVAL;
119 u32 i, flags;
120
Gerd Hoffmann33f35422018-09-11 15:42:15 +0200121 ubuf = kzalloc(sizeof(*ubuf), GFP_KERNEL);
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200122 if (!ubuf)
123 return -ENOMEM;
124
Gerd Hoffmanndc4716d2018-09-11 15:42:10 +0200125 pglimit = (size_limit_mb * 1024 * 1024) >> PAGE_SHIFT;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200126 for (i = 0; i < head->count; i++) {
127 if (!IS_ALIGNED(list[i].offset, PAGE_SIZE))
Gerd Hoffmann0d174552018-09-11 15:42:11 +0200128 goto err;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200129 if (!IS_ALIGNED(list[i].size, PAGE_SIZE))
Gerd Hoffmann0d174552018-09-11 15:42:11 +0200130 goto err;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200131 ubuf->pagecount += list[i].size >> PAGE_SHIFT;
Gerd Hoffmanndc4716d2018-09-11 15:42:10 +0200132 if (ubuf->pagecount > pglimit)
Gerd Hoffmann0d174552018-09-11 15:42:11 +0200133 goto err;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200134 }
Gerd Hoffmann33f35422018-09-11 15:42:15 +0200135 ubuf->pages = kmalloc_array(ubuf->pagecount, sizeof(*ubuf->pages),
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200136 GFP_KERNEL);
137 if (!ubuf->pages) {
138 ret = -ENOMEM;
Gerd Hoffmann0d174552018-09-11 15:42:11 +0200139 goto err;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200140 }
141
142 pgbuf = 0;
143 for (i = 0; i < head->count; i++) {
Gerd Hoffmann7a1c67d2018-09-11 15:42:12 +0200144 ret = -EBADFD;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200145 memfd = fget(list[i].memfd);
146 if (!memfd)
Gerd Hoffmann0d174552018-09-11 15:42:11 +0200147 goto err;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200148 if (!shmem_mapping(file_inode(memfd)->i_mapping))
Gerd Hoffmann0d174552018-09-11 15:42:11 +0200149 goto err;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200150 seals = memfd_fcntl(memfd, F_GET_SEALS, 0);
Gerd Hoffmann7a1c67d2018-09-11 15:42:12 +0200151 if (seals == -EINVAL)
152 goto err;
153 ret = -EINVAL;
154 if ((seals & SEALS_WANTED) != SEALS_WANTED ||
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200155 (seals & SEALS_DENIED) != 0)
Gerd Hoffmann0d174552018-09-11 15:42:11 +0200156 goto err;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200157 pgoff = list[i].offset >> PAGE_SHIFT;
158 pgcnt = list[i].size >> PAGE_SHIFT;
159 for (pgidx = 0; pgidx < pgcnt; pgidx++) {
160 page = shmem_read_mapping_page(
161 file_inode(memfd)->i_mapping, pgoff + pgidx);
162 if (IS_ERR(page)) {
163 ret = PTR_ERR(page);
Gerd Hoffmann0d174552018-09-11 15:42:11 +0200164 goto err;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200165 }
166 ubuf->pages[pgbuf++] = page;
167 }
168 fput(memfd);
Gerd Hoffmann0d174552018-09-11 15:42:11 +0200169 memfd = NULL;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200170 }
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200171
172 exp_info.ops = &udmabuf_ops;
173 exp_info.size = ubuf->pagecount << PAGE_SHIFT;
174 exp_info.priv = ubuf;
Gerd Hoffmann5c074ee2018-11-14 13:20:29 +0100175 exp_info.flags = O_RDWR;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200176
Gurchetan Singhc1bbed62019-12-02 17:36:25 -0800177 ubuf->device = device;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200178 buf = dma_buf_export(&exp_info);
179 if (IS_ERR(buf)) {
180 ret = PTR_ERR(buf);
Gerd Hoffmann0d174552018-09-11 15:42:11 +0200181 goto err;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200182 }
183
184 flags = 0;
185 if (head->flags & UDMABUF_FLAGS_CLOEXEC)
186 flags |= O_CLOEXEC;
187 return dma_buf_fd(buf, flags);
188
Gerd Hoffmann0d174552018-09-11 15:42:11 +0200189err:
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200190 while (pgbuf > 0)
191 put_page(ubuf->pages[--pgbuf]);
Gustavo A. R. Silva683a0e62018-09-04 14:07:49 -0500192 if (memfd)
193 fput(memfd);
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200194 kfree(ubuf->pages);
195 kfree(ubuf);
196 return ret;
197}
198
199static long udmabuf_ioctl_create(struct file *filp, unsigned long arg)
200{
201 struct udmabuf_create create;
202 struct udmabuf_create_list head;
203 struct udmabuf_create_item list;
204
205 if (copy_from_user(&create, (void __user *)arg,
Gerd Hoffmann33f35422018-09-11 15:42:15 +0200206 sizeof(create)))
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200207 return -EFAULT;
208
209 head.flags = create.flags;
210 head.count = 1;
211 list.memfd = create.memfd;
212 list.offset = create.offset;
213 list.size = create.size;
214
Gurchetan Singhc1bbed62019-12-02 17:36:25 -0800215 return udmabuf_create(filp->private_data, &head, &list);
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200216}
217
218static long udmabuf_ioctl_create_list(struct file *filp, unsigned long arg)
219{
220 struct udmabuf_create_list head;
221 struct udmabuf_create_item *list;
222 int ret = -EINVAL;
223 u32 lsize;
224
225 if (copy_from_user(&head, (void __user *)arg, sizeof(head)))
226 return -EFAULT;
Gerd Hoffmanndc4716d2018-09-11 15:42:10 +0200227 if (head.count > list_limit)
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200228 return -EINVAL;
229 lsize = sizeof(struct udmabuf_create_item) * head.count;
230 list = memdup_user((void __user *)(arg + sizeof(head)), lsize);
231 if (IS_ERR(list))
232 return PTR_ERR(list);
233
Gurchetan Singhc1bbed62019-12-02 17:36:25 -0800234 ret = udmabuf_create(filp->private_data, &head, list);
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200235 kfree(list);
236 return ret;
237}
238
239static long udmabuf_ioctl(struct file *filp, unsigned int ioctl,
240 unsigned long arg)
241{
242 long ret;
243
244 switch (ioctl) {
245 case UDMABUF_CREATE:
246 ret = udmabuf_ioctl_create(filp, arg);
247 break;
248 case UDMABUF_CREATE_LIST:
249 ret = udmabuf_ioctl_create_list(filp, arg);
250 break;
251 default:
Gerd Hoffmann52499d9c2018-09-11 15:42:13 +0200252 ret = -ENOTTY;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200253 break;
254 }
255 return ret;
256}
257
258static const struct file_operations udmabuf_fops = {
259 .owner = THIS_MODULE,
260 .unlocked_ioctl = udmabuf_ioctl,
261};
262
263static struct miscdevice udmabuf_misc = {
264 .minor = MISC_DYNAMIC_MINOR,
265 .name = "udmabuf",
266 .fops = &udmabuf_fops,
267};
268
269static int __init udmabuf_dev_init(void)
270{
271 return misc_register(&udmabuf_misc);
272}
273
274static void __exit udmabuf_dev_exit(void)
275{
276 misc_deregister(&udmabuf_misc);
277}
278
279module_init(udmabuf_dev_init)
280module_exit(udmabuf_dev_exit)
281
282MODULE_AUTHOR("Gerd Hoffmann <kraxel@redhat.com>");
283MODULE_LICENSE("GPL v2");