blob: 0a610e09ae237e81ec86c247346147fa1f174c80 [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
Gurchetan Singh17a7ce22019-12-02 17:36:26 -080050static struct sg_table *get_sg_table(struct device *dev, struct dma_buf *buf,
51 enum dma_data_direction direction)
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +020052{
Gurchetan Singh17a7ce22019-12-02 17:36:26 -080053 struct udmabuf *ubuf = buf->priv;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +020054 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;
Gurchetan Singh17a7ce22019-12-02 17:36:26 -080065 if (!dma_map_sg(dev, sg->sgl, sg->nents, direction)) {
Dan Carpenter6f19eb22018-09-14 09:56:15 +030066 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
Gurchetan Singh17a7ce22019-12-02 17:36:26 -080077static void put_sg_table(struct device *dev, struct sg_table *sg,
78 enum dma_data_direction direction)
79{
80 dma_unmap_sg(dev, sg->sgl, sg->nents, direction);
81 sg_free_table(sg);
82 kfree(sg);
83}
84
85static struct sg_table *map_udmabuf(struct dma_buf_attachment *at,
86 enum dma_data_direction direction)
87{
88 return get_sg_table(at->dev, at->dmabuf, direction);
89}
90
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +020091static void unmap_udmabuf(struct dma_buf_attachment *at,
92 struct sg_table *sg,
93 enum dma_data_direction direction)
94{
Gurchetan Singh17a7ce22019-12-02 17:36:26 -080095 return put_sg_table(at->dev, sg, direction);
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +020096}
97
98static void release_udmabuf(struct dma_buf *buf)
99{
100 struct udmabuf *ubuf = buf->priv;
101 pgoff_t pg;
102
103 for (pg = 0; pg < ubuf->pagecount; pg++)
104 put_page(ubuf->pages[pg]);
105 kfree(ubuf->pages);
106 kfree(ubuf);
107}
108
Gerd Hoffmanna3485282018-09-11 15:42:07 +0200109static const struct dma_buf_ops udmabuf_ops = {
Gurchetan Singhbc7a71d2019-12-02 17:36:24 -0800110 .cache_sgt_mapping = true,
111 .map_dma_buf = map_udmabuf,
112 .unmap_dma_buf = unmap_udmabuf,
113 .release = release_udmabuf,
114 .mmap = mmap_udmabuf,
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200115};
116
117#define SEALS_WANTED (F_SEAL_SHRINK)
118#define SEALS_DENIED (F_SEAL_WRITE)
119
Gurchetan Singhc1bbed62019-12-02 17:36:25 -0800120static long udmabuf_create(struct miscdevice *device,
121 struct udmabuf_create_list *head,
122 struct udmabuf_create_item *list)
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200123{
124 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
125 struct file *memfd = NULL;
126 struct udmabuf *ubuf;
127 struct dma_buf *buf;
Gerd Hoffmann0d174552018-09-11 15:42:11 +0200128 pgoff_t pgoff, pgcnt, pgidx, pgbuf = 0, pglimit;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200129 struct page *page;
130 int seals, ret = -EINVAL;
131 u32 i, flags;
132
Gerd Hoffmann33f35422018-09-11 15:42:15 +0200133 ubuf = kzalloc(sizeof(*ubuf), GFP_KERNEL);
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200134 if (!ubuf)
135 return -ENOMEM;
136
Gerd Hoffmanndc4716d2018-09-11 15:42:10 +0200137 pglimit = (size_limit_mb * 1024 * 1024) >> PAGE_SHIFT;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200138 for (i = 0; i < head->count; i++) {
139 if (!IS_ALIGNED(list[i].offset, PAGE_SIZE))
Gerd Hoffmann0d174552018-09-11 15:42:11 +0200140 goto err;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200141 if (!IS_ALIGNED(list[i].size, PAGE_SIZE))
Gerd Hoffmann0d174552018-09-11 15:42:11 +0200142 goto err;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200143 ubuf->pagecount += list[i].size >> PAGE_SHIFT;
Gerd Hoffmanndc4716d2018-09-11 15:42:10 +0200144 if (ubuf->pagecount > pglimit)
Gerd Hoffmann0d174552018-09-11 15:42:11 +0200145 goto err;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200146 }
Gerd Hoffmann33f35422018-09-11 15:42:15 +0200147 ubuf->pages = kmalloc_array(ubuf->pagecount, sizeof(*ubuf->pages),
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200148 GFP_KERNEL);
149 if (!ubuf->pages) {
150 ret = -ENOMEM;
Gerd Hoffmann0d174552018-09-11 15:42:11 +0200151 goto err;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200152 }
153
154 pgbuf = 0;
155 for (i = 0; i < head->count; i++) {
Gerd Hoffmann7a1c67d2018-09-11 15:42:12 +0200156 ret = -EBADFD;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200157 memfd = fget(list[i].memfd);
158 if (!memfd)
Gerd Hoffmann0d174552018-09-11 15:42:11 +0200159 goto err;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200160 if (!shmem_mapping(file_inode(memfd)->i_mapping))
Gerd Hoffmann0d174552018-09-11 15:42:11 +0200161 goto err;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200162 seals = memfd_fcntl(memfd, F_GET_SEALS, 0);
Gerd Hoffmann7a1c67d2018-09-11 15:42:12 +0200163 if (seals == -EINVAL)
164 goto err;
165 ret = -EINVAL;
166 if ((seals & SEALS_WANTED) != SEALS_WANTED ||
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200167 (seals & SEALS_DENIED) != 0)
Gerd Hoffmann0d174552018-09-11 15:42:11 +0200168 goto err;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200169 pgoff = list[i].offset >> PAGE_SHIFT;
170 pgcnt = list[i].size >> PAGE_SHIFT;
171 for (pgidx = 0; pgidx < pgcnt; pgidx++) {
172 page = shmem_read_mapping_page(
173 file_inode(memfd)->i_mapping, pgoff + pgidx);
174 if (IS_ERR(page)) {
175 ret = PTR_ERR(page);
Gerd Hoffmann0d174552018-09-11 15:42:11 +0200176 goto err;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200177 }
178 ubuf->pages[pgbuf++] = page;
179 }
180 fput(memfd);
Gerd Hoffmann0d174552018-09-11 15:42:11 +0200181 memfd = NULL;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200182 }
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200183
184 exp_info.ops = &udmabuf_ops;
185 exp_info.size = ubuf->pagecount << PAGE_SHIFT;
186 exp_info.priv = ubuf;
Gerd Hoffmann5c074ee2018-11-14 13:20:29 +0100187 exp_info.flags = O_RDWR;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200188
Gurchetan Singhc1bbed62019-12-02 17:36:25 -0800189 ubuf->device = device;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200190 buf = dma_buf_export(&exp_info);
191 if (IS_ERR(buf)) {
192 ret = PTR_ERR(buf);
Gerd Hoffmann0d174552018-09-11 15:42:11 +0200193 goto err;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200194 }
195
196 flags = 0;
197 if (head->flags & UDMABUF_FLAGS_CLOEXEC)
198 flags |= O_CLOEXEC;
199 return dma_buf_fd(buf, flags);
200
Gerd Hoffmann0d174552018-09-11 15:42:11 +0200201err:
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200202 while (pgbuf > 0)
203 put_page(ubuf->pages[--pgbuf]);
Gustavo A. R. Silva683a0e62018-09-04 14:07:49 -0500204 if (memfd)
205 fput(memfd);
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200206 kfree(ubuf->pages);
207 kfree(ubuf);
208 return ret;
209}
210
211static long udmabuf_ioctl_create(struct file *filp, unsigned long arg)
212{
213 struct udmabuf_create create;
214 struct udmabuf_create_list head;
215 struct udmabuf_create_item list;
216
217 if (copy_from_user(&create, (void __user *)arg,
Gerd Hoffmann33f35422018-09-11 15:42:15 +0200218 sizeof(create)))
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200219 return -EFAULT;
220
221 head.flags = create.flags;
222 head.count = 1;
223 list.memfd = create.memfd;
224 list.offset = create.offset;
225 list.size = create.size;
226
Gurchetan Singhc1bbed62019-12-02 17:36:25 -0800227 return udmabuf_create(filp->private_data, &head, &list);
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200228}
229
230static long udmabuf_ioctl_create_list(struct file *filp, unsigned long arg)
231{
232 struct udmabuf_create_list head;
233 struct udmabuf_create_item *list;
234 int ret = -EINVAL;
235 u32 lsize;
236
237 if (copy_from_user(&head, (void __user *)arg, sizeof(head)))
238 return -EFAULT;
Gerd Hoffmanndc4716d2018-09-11 15:42:10 +0200239 if (head.count > list_limit)
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200240 return -EINVAL;
241 lsize = sizeof(struct udmabuf_create_item) * head.count;
242 list = memdup_user((void __user *)(arg + sizeof(head)), lsize);
243 if (IS_ERR(list))
244 return PTR_ERR(list);
245
Gurchetan Singhc1bbed62019-12-02 17:36:25 -0800246 ret = udmabuf_create(filp->private_data, &head, list);
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200247 kfree(list);
248 return ret;
249}
250
251static long udmabuf_ioctl(struct file *filp, unsigned int ioctl,
252 unsigned long arg)
253{
254 long ret;
255
256 switch (ioctl) {
257 case UDMABUF_CREATE:
258 ret = udmabuf_ioctl_create(filp, arg);
259 break;
260 case UDMABUF_CREATE_LIST:
261 ret = udmabuf_ioctl_create_list(filp, arg);
262 break;
263 default:
Gerd Hoffmann52499d9c2018-09-11 15:42:13 +0200264 ret = -ENOTTY;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200265 break;
266 }
267 return ret;
268}
269
270static const struct file_operations udmabuf_fops = {
271 .owner = THIS_MODULE,
272 .unlocked_ioctl = udmabuf_ioctl,
273};
274
275static struct miscdevice udmabuf_misc = {
276 .minor = MISC_DYNAMIC_MINOR,
277 .name = "udmabuf",
278 .fops = &udmabuf_fops,
279};
280
281static int __init udmabuf_dev_init(void)
282{
283 return misc_register(&udmabuf_misc);
284}
285
286static void __exit udmabuf_dev_exit(void)
287{
288 misc_deregister(&udmabuf_misc);
289}
290
291module_init(udmabuf_dev_init)
292module_exit(udmabuf_dev_exit)
293
294MODULE_AUTHOR("Gerd Hoffmann <kraxel@redhat.com>");
295MODULE_LICENSE("GPL v2");