blob: be15eb6b058651897efb46fc99c12d6ffa4dea6d [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;
21};
22
Souptick Joarder300133d2019-01-03 15:26:34 -080023static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf)
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +020024{
25 struct vm_area_struct *vma = vmf->vma;
26 struct udmabuf *ubuf = vma->vm_private_data;
27
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +020028 vmf->page = ubuf->pages[vmf->pgoff];
29 get_page(vmf->page);
30 return 0;
31}
32
33static const struct vm_operations_struct udmabuf_vm_ops = {
34 .fault = udmabuf_vm_fault,
35};
36
37static int mmap_udmabuf(struct dma_buf *buf, struct vm_area_struct *vma)
38{
39 struct udmabuf *ubuf = buf->priv;
40
41 if ((vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) == 0)
42 return -EINVAL;
43
44 vma->vm_ops = &udmabuf_vm_ops;
45 vma->vm_private_data = ubuf;
46 return 0;
47}
48
49static struct sg_table *map_udmabuf(struct dma_buf_attachment *at,
50 enum dma_data_direction direction)
51{
52 struct udmabuf *ubuf = at->dmabuf->priv;
53 struct sg_table *sg;
Gerd Hoffmanna3e722d2018-09-11 15:42:05 +020054 int ret;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +020055
56 sg = kzalloc(sizeof(*sg), GFP_KERNEL);
57 if (!sg)
Gerd Hoffmanna3e722d2018-09-11 15:42:05 +020058 return ERR_PTR(-ENOMEM);
59 ret = sg_alloc_table_from_pages(sg, ubuf->pages, ubuf->pagecount,
60 0, ubuf->pagecount << PAGE_SHIFT,
61 GFP_KERNEL);
62 if (ret < 0)
63 goto err;
Dan Carpenter6f19eb22018-09-14 09:56:15 +030064 if (!dma_map_sg(at->dev, sg->sgl, sg->nents, direction)) {
65 ret = -EINVAL;
Gerd Hoffmanna3e722d2018-09-11 15:42:05 +020066 goto err;
Dan Carpenter6f19eb22018-09-14 09:56:15 +030067 }
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +020068 return sg;
69
Gerd Hoffmanna3e722d2018-09-11 15:42:05 +020070err:
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +020071 sg_free_table(sg);
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +020072 kfree(sg);
Gerd Hoffmanna3e722d2018-09-11 15:42:05 +020073 return ERR_PTR(ret);
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +020074}
75
76static void unmap_udmabuf(struct dma_buf_attachment *at,
77 struct sg_table *sg,
78 enum dma_data_direction direction)
79{
Lucas Stach283f1e32019-06-04 22:23:31 +020080 dma_unmap_sg(at->dev, sg->sgl, sg->nents, direction);
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +020081 sg_free_table(sg);
82 kfree(sg);
83}
84
85static void release_udmabuf(struct dma_buf *buf)
86{
87 struct udmabuf *ubuf = buf->priv;
88 pgoff_t pg;
89
90 for (pg = 0; pg < ubuf->pagecount; pg++)
91 put_page(ubuf->pages[pg]);
92 kfree(ubuf->pages);
93 kfree(ubuf);
94}
95
Gerd Hoffmanna3485282018-09-11 15:42:07 +020096static const struct dma_buf_ops udmabuf_ops = {
Gurchetan Singhbc7a71d2019-12-02 17:36:24 -080097 .cache_sgt_mapping = true,
98 .map_dma_buf = map_udmabuf,
99 .unmap_dma_buf = unmap_udmabuf,
100 .release = release_udmabuf,
101 .mmap = mmap_udmabuf,
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200102};
103
104#define SEALS_WANTED (F_SEAL_SHRINK)
105#define SEALS_DENIED (F_SEAL_WRITE)
106
Gerd Hoffmanndc254552018-09-11 15:42:08 +0200107static long udmabuf_create(const struct udmabuf_create_list *head,
108 const struct udmabuf_create_item *list)
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200109{
110 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
111 struct file *memfd = NULL;
112 struct udmabuf *ubuf;
113 struct dma_buf *buf;
Gerd Hoffmann0d174552018-09-11 15:42:11 +0200114 pgoff_t pgoff, pgcnt, pgidx, pgbuf = 0, pglimit;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200115 struct page *page;
116 int seals, ret = -EINVAL;
117 u32 i, flags;
118
Gerd Hoffmann33f35422018-09-11 15:42:15 +0200119 ubuf = kzalloc(sizeof(*ubuf), GFP_KERNEL);
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200120 if (!ubuf)
121 return -ENOMEM;
122
Gerd Hoffmanndc4716d2018-09-11 15:42:10 +0200123 pglimit = (size_limit_mb * 1024 * 1024) >> PAGE_SHIFT;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200124 for (i = 0; i < head->count; i++) {
125 if (!IS_ALIGNED(list[i].offset, PAGE_SIZE))
Gerd Hoffmann0d174552018-09-11 15:42:11 +0200126 goto err;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200127 if (!IS_ALIGNED(list[i].size, PAGE_SIZE))
Gerd Hoffmann0d174552018-09-11 15:42:11 +0200128 goto err;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200129 ubuf->pagecount += list[i].size >> PAGE_SHIFT;
Gerd Hoffmanndc4716d2018-09-11 15:42:10 +0200130 if (ubuf->pagecount > pglimit)
Gerd Hoffmann0d174552018-09-11 15:42:11 +0200131 goto err;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200132 }
Gerd Hoffmann33f35422018-09-11 15:42:15 +0200133 ubuf->pages = kmalloc_array(ubuf->pagecount, sizeof(*ubuf->pages),
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200134 GFP_KERNEL);
135 if (!ubuf->pages) {
136 ret = -ENOMEM;
Gerd Hoffmann0d174552018-09-11 15:42:11 +0200137 goto err;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200138 }
139
140 pgbuf = 0;
141 for (i = 0; i < head->count; i++) {
Gerd Hoffmann7a1c67d2018-09-11 15:42:12 +0200142 ret = -EBADFD;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200143 memfd = fget(list[i].memfd);
144 if (!memfd)
Gerd Hoffmann0d174552018-09-11 15:42:11 +0200145 goto err;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200146 if (!shmem_mapping(file_inode(memfd)->i_mapping))
Gerd Hoffmann0d174552018-09-11 15:42:11 +0200147 goto err;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200148 seals = memfd_fcntl(memfd, F_GET_SEALS, 0);
Gerd Hoffmann7a1c67d2018-09-11 15:42:12 +0200149 if (seals == -EINVAL)
150 goto err;
151 ret = -EINVAL;
152 if ((seals & SEALS_WANTED) != SEALS_WANTED ||
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200153 (seals & SEALS_DENIED) != 0)
Gerd Hoffmann0d174552018-09-11 15:42:11 +0200154 goto err;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200155 pgoff = list[i].offset >> PAGE_SHIFT;
156 pgcnt = list[i].size >> PAGE_SHIFT;
157 for (pgidx = 0; pgidx < pgcnt; pgidx++) {
158 page = shmem_read_mapping_page(
159 file_inode(memfd)->i_mapping, pgoff + pgidx);
160 if (IS_ERR(page)) {
161 ret = PTR_ERR(page);
Gerd Hoffmann0d174552018-09-11 15:42:11 +0200162 goto err;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200163 }
164 ubuf->pages[pgbuf++] = page;
165 }
166 fput(memfd);
Gerd Hoffmann0d174552018-09-11 15:42:11 +0200167 memfd = NULL;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200168 }
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200169
170 exp_info.ops = &udmabuf_ops;
171 exp_info.size = ubuf->pagecount << PAGE_SHIFT;
172 exp_info.priv = ubuf;
Gerd Hoffmann5c074ee2018-11-14 13:20:29 +0100173 exp_info.flags = O_RDWR;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200174
175 buf = dma_buf_export(&exp_info);
176 if (IS_ERR(buf)) {
177 ret = PTR_ERR(buf);
Gerd Hoffmann0d174552018-09-11 15:42:11 +0200178 goto err;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200179 }
180
181 flags = 0;
182 if (head->flags & UDMABUF_FLAGS_CLOEXEC)
183 flags |= O_CLOEXEC;
184 return dma_buf_fd(buf, flags);
185
Gerd Hoffmann0d174552018-09-11 15:42:11 +0200186err:
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200187 while (pgbuf > 0)
188 put_page(ubuf->pages[--pgbuf]);
Gustavo A. R. Silva683a0e62018-09-04 14:07:49 -0500189 if (memfd)
190 fput(memfd);
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200191 kfree(ubuf->pages);
192 kfree(ubuf);
193 return ret;
194}
195
196static long udmabuf_ioctl_create(struct file *filp, unsigned long arg)
197{
198 struct udmabuf_create create;
199 struct udmabuf_create_list head;
200 struct udmabuf_create_item list;
201
202 if (copy_from_user(&create, (void __user *)arg,
Gerd Hoffmann33f35422018-09-11 15:42:15 +0200203 sizeof(create)))
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200204 return -EFAULT;
205
206 head.flags = create.flags;
207 head.count = 1;
208 list.memfd = create.memfd;
209 list.offset = create.offset;
210 list.size = create.size;
211
212 return udmabuf_create(&head, &list);
213}
214
215static long udmabuf_ioctl_create_list(struct file *filp, unsigned long arg)
216{
217 struct udmabuf_create_list head;
218 struct udmabuf_create_item *list;
219 int ret = -EINVAL;
220 u32 lsize;
221
222 if (copy_from_user(&head, (void __user *)arg, sizeof(head)))
223 return -EFAULT;
Gerd Hoffmanndc4716d2018-09-11 15:42:10 +0200224 if (head.count > list_limit)
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200225 return -EINVAL;
226 lsize = sizeof(struct udmabuf_create_item) * head.count;
227 list = memdup_user((void __user *)(arg + sizeof(head)), lsize);
228 if (IS_ERR(list))
229 return PTR_ERR(list);
230
231 ret = udmabuf_create(&head, list);
232 kfree(list);
233 return ret;
234}
235
236static long udmabuf_ioctl(struct file *filp, unsigned int ioctl,
237 unsigned long arg)
238{
239 long ret;
240
241 switch (ioctl) {
242 case UDMABUF_CREATE:
243 ret = udmabuf_ioctl_create(filp, arg);
244 break;
245 case UDMABUF_CREATE_LIST:
246 ret = udmabuf_ioctl_create_list(filp, arg);
247 break;
248 default:
Gerd Hoffmann52499d9c2018-09-11 15:42:13 +0200249 ret = -ENOTTY;
Gerd Hoffmannfbb0de72018-08-27 11:34:44 +0200250 break;
251 }
252 return ret;
253}
254
255static const struct file_operations udmabuf_fops = {
256 .owner = THIS_MODULE,
257 .unlocked_ioctl = udmabuf_ioctl,
258};
259
260static struct miscdevice udmabuf_misc = {
261 .minor = MISC_DYNAMIC_MINOR,
262 .name = "udmabuf",
263 .fops = &udmabuf_fops,
264};
265
266static int __init udmabuf_dev_init(void)
267{
268 return misc_register(&udmabuf_misc);
269}
270
271static void __exit udmabuf_dev_exit(void)
272{
273 misc_deregister(&udmabuf_misc);
274}
275
276module_init(udmabuf_dev_init)
277module_exit(udmabuf_dev_exit)
278
279MODULE_AUTHOR("Gerd Hoffmann <kraxel@redhat.com>");
280MODULE_LICENSE("GPL v2");