Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Gerd Hoffmann | 913965c | 2018-09-11 15:42:04 +0200 | [diff] [blame] | 2 | #include <linux/cred.h> |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 3 | #include <linux/device.h> |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 4 | #include <linux/dma-buf.h> |
| 5 | #include <linux/highmem.h> |
Gerd Hoffmann | 913965c | 2018-09-11 15:42:04 +0200 | [diff] [blame] | 6 | #include <linux/init.h> |
| 7 | #include <linux/kernel.h> |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 8 | #include <linux/memfd.h> |
Gerd Hoffmann | 913965c | 2018-09-11 15:42:04 +0200 | [diff] [blame] | 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> |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 14 | |
Gerd Hoffmann | dc4716d | 2018-09-11 15:42:10 +0200 | [diff] [blame] | 15 | static const u32 list_limit = 1024; /* udmabuf_create_list->count limit */ |
| 16 | static const size_t size_limit_mb = 64; /* total dmabuf size, in megabytes */ |
| 17 | |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 18 | struct udmabuf { |
Gerd Hoffmann | b35f57c | 2018-09-11 15:42:06 +0200 | [diff] [blame] | 19 | pgoff_t pagecount; |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 20 | struct page **pages; |
Gurchetan Singh | c1bbed6 | 2019-12-02 17:36:25 -0800 | [diff] [blame^] | 21 | struct miscdevice *device; |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 22 | }; |
| 23 | |
Souptick Joarder | 300133d | 2019-01-03 15:26:34 -0800 | [diff] [blame] | 24 | static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf) |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 25 | { |
| 26 | struct vm_area_struct *vma = vmf->vma; |
| 27 | struct udmabuf *ubuf = vma->vm_private_data; |
| 28 | |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 29 | vmf->page = ubuf->pages[vmf->pgoff]; |
| 30 | get_page(vmf->page); |
| 31 | return 0; |
| 32 | } |
| 33 | |
| 34 | static const struct vm_operations_struct udmabuf_vm_ops = { |
| 35 | .fault = udmabuf_vm_fault, |
| 36 | }; |
| 37 | |
| 38 | static 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 | |
| 50 | static 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 Hoffmann | a3e722d | 2018-09-11 15:42:05 +0200 | [diff] [blame] | 55 | int ret; |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 56 | |
| 57 | sg = kzalloc(sizeof(*sg), GFP_KERNEL); |
| 58 | if (!sg) |
Gerd Hoffmann | a3e722d | 2018-09-11 15:42:05 +0200 | [diff] [blame] | 59 | 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 Carpenter | 6f19eb2 | 2018-09-14 09:56:15 +0300 | [diff] [blame] | 65 | if (!dma_map_sg(at->dev, sg->sgl, sg->nents, direction)) { |
| 66 | ret = -EINVAL; |
Gerd Hoffmann | a3e722d | 2018-09-11 15:42:05 +0200 | [diff] [blame] | 67 | goto err; |
Dan Carpenter | 6f19eb2 | 2018-09-14 09:56:15 +0300 | [diff] [blame] | 68 | } |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 69 | return sg; |
| 70 | |
Gerd Hoffmann | a3e722d | 2018-09-11 15:42:05 +0200 | [diff] [blame] | 71 | err: |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 72 | sg_free_table(sg); |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 73 | kfree(sg); |
Gerd Hoffmann | a3e722d | 2018-09-11 15:42:05 +0200 | [diff] [blame] | 74 | return ERR_PTR(ret); |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | static void unmap_udmabuf(struct dma_buf_attachment *at, |
| 78 | struct sg_table *sg, |
| 79 | enum dma_data_direction direction) |
| 80 | { |
Lucas Stach | 283f1e3 | 2019-06-04 22:23:31 +0200 | [diff] [blame] | 81 | dma_unmap_sg(at->dev, sg->sgl, sg->nents, direction); |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 82 | sg_free_table(sg); |
| 83 | kfree(sg); |
| 84 | } |
| 85 | |
| 86 | static 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 Hoffmann | a348528 | 2018-09-11 15:42:07 +0200 | [diff] [blame] | 97 | static const struct dma_buf_ops udmabuf_ops = { |
Gurchetan Singh | bc7a71d | 2019-12-02 17:36:24 -0800 | [diff] [blame] | 98 | .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 Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | #define SEALS_WANTED (F_SEAL_SHRINK) |
| 106 | #define SEALS_DENIED (F_SEAL_WRITE) |
| 107 | |
Gurchetan Singh | c1bbed6 | 2019-12-02 17:36:25 -0800 | [diff] [blame^] | 108 | static long udmabuf_create(struct miscdevice *device, |
| 109 | struct udmabuf_create_list *head, |
| 110 | struct udmabuf_create_item *list) |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 111 | { |
| 112 | DEFINE_DMA_BUF_EXPORT_INFO(exp_info); |
| 113 | struct file *memfd = NULL; |
| 114 | struct udmabuf *ubuf; |
| 115 | struct dma_buf *buf; |
Gerd Hoffmann | 0d17455 | 2018-09-11 15:42:11 +0200 | [diff] [blame] | 116 | pgoff_t pgoff, pgcnt, pgidx, pgbuf = 0, pglimit; |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 117 | struct page *page; |
| 118 | int seals, ret = -EINVAL; |
| 119 | u32 i, flags; |
| 120 | |
Gerd Hoffmann | 33f3542 | 2018-09-11 15:42:15 +0200 | [diff] [blame] | 121 | ubuf = kzalloc(sizeof(*ubuf), GFP_KERNEL); |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 122 | if (!ubuf) |
| 123 | return -ENOMEM; |
| 124 | |
Gerd Hoffmann | dc4716d | 2018-09-11 15:42:10 +0200 | [diff] [blame] | 125 | pglimit = (size_limit_mb * 1024 * 1024) >> PAGE_SHIFT; |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 126 | for (i = 0; i < head->count; i++) { |
| 127 | if (!IS_ALIGNED(list[i].offset, PAGE_SIZE)) |
Gerd Hoffmann | 0d17455 | 2018-09-11 15:42:11 +0200 | [diff] [blame] | 128 | goto err; |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 129 | if (!IS_ALIGNED(list[i].size, PAGE_SIZE)) |
Gerd Hoffmann | 0d17455 | 2018-09-11 15:42:11 +0200 | [diff] [blame] | 130 | goto err; |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 131 | ubuf->pagecount += list[i].size >> PAGE_SHIFT; |
Gerd Hoffmann | dc4716d | 2018-09-11 15:42:10 +0200 | [diff] [blame] | 132 | if (ubuf->pagecount > pglimit) |
Gerd Hoffmann | 0d17455 | 2018-09-11 15:42:11 +0200 | [diff] [blame] | 133 | goto err; |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 134 | } |
Gerd Hoffmann | 33f3542 | 2018-09-11 15:42:15 +0200 | [diff] [blame] | 135 | ubuf->pages = kmalloc_array(ubuf->pagecount, sizeof(*ubuf->pages), |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 136 | GFP_KERNEL); |
| 137 | if (!ubuf->pages) { |
| 138 | ret = -ENOMEM; |
Gerd Hoffmann | 0d17455 | 2018-09-11 15:42:11 +0200 | [diff] [blame] | 139 | goto err; |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | pgbuf = 0; |
| 143 | for (i = 0; i < head->count; i++) { |
Gerd Hoffmann | 7a1c67d | 2018-09-11 15:42:12 +0200 | [diff] [blame] | 144 | ret = -EBADFD; |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 145 | memfd = fget(list[i].memfd); |
| 146 | if (!memfd) |
Gerd Hoffmann | 0d17455 | 2018-09-11 15:42:11 +0200 | [diff] [blame] | 147 | goto err; |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 148 | if (!shmem_mapping(file_inode(memfd)->i_mapping)) |
Gerd Hoffmann | 0d17455 | 2018-09-11 15:42:11 +0200 | [diff] [blame] | 149 | goto err; |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 150 | seals = memfd_fcntl(memfd, F_GET_SEALS, 0); |
Gerd Hoffmann | 7a1c67d | 2018-09-11 15:42:12 +0200 | [diff] [blame] | 151 | if (seals == -EINVAL) |
| 152 | goto err; |
| 153 | ret = -EINVAL; |
| 154 | if ((seals & SEALS_WANTED) != SEALS_WANTED || |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 155 | (seals & SEALS_DENIED) != 0) |
Gerd Hoffmann | 0d17455 | 2018-09-11 15:42:11 +0200 | [diff] [blame] | 156 | goto err; |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 157 | 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 Hoffmann | 0d17455 | 2018-09-11 15:42:11 +0200 | [diff] [blame] | 164 | goto err; |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 165 | } |
| 166 | ubuf->pages[pgbuf++] = page; |
| 167 | } |
| 168 | fput(memfd); |
Gerd Hoffmann | 0d17455 | 2018-09-11 15:42:11 +0200 | [diff] [blame] | 169 | memfd = NULL; |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 170 | } |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 171 | |
| 172 | exp_info.ops = &udmabuf_ops; |
| 173 | exp_info.size = ubuf->pagecount << PAGE_SHIFT; |
| 174 | exp_info.priv = ubuf; |
Gerd Hoffmann | 5c074ee | 2018-11-14 13:20:29 +0100 | [diff] [blame] | 175 | exp_info.flags = O_RDWR; |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 176 | |
Gurchetan Singh | c1bbed6 | 2019-12-02 17:36:25 -0800 | [diff] [blame^] | 177 | ubuf->device = device; |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 178 | buf = dma_buf_export(&exp_info); |
| 179 | if (IS_ERR(buf)) { |
| 180 | ret = PTR_ERR(buf); |
Gerd Hoffmann | 0d17455 | 2018-09-11 15:42:11 +0200 | [diff] [blame] | 181 | goto err; |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | flags = 0; |
| 185 | if (head->flags & UDMABUF_FLAGS_CLOEXEC) |
| 186 | flags |= O_CLOEXEC; |
| 187 | return dma_buf_fd(buf, flags); |
| 188 | |
Gerd Hoffmann | 0d17455 | 2018-09-11 15:42:11 +0200 | [diff] [blame] | 189 | err: |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 190 | while (pgbuf > 0) |
| 191 | put_page(ubuf->pages[--pgbuf]); |
Gustavo A. R. Silva | 683a0e6 | 2018-09-04 14:07:49 -0500 | [diff] [blame] | 192 | if (memfd) |
| 193 | fput(memfd); |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 194 | kfree(ubuf->pages); |
| 195 | kfree(ubuf); |
| 196 | return ret; |
| 197 | } |
| 198 | |
| 199 | static 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 Hoffmann | 33f3542 | 2018-09-11 15:42:15 +0200 | [diff] [blame] | 206 | sizeof(create))) |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 207 | 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 Singh | c1bbed6 | 2019-12-02 17:36:25 -0800 | [diff] [blame^] | 215 | return udmabuf_create(filp->private_data, &head, &list); |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | static 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 Hoffmann | dc4716d | 2018-09-11 15:42:10 +0200 | [diff] [blame] | 227 | if (head.count > list_limit) |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 228 | 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 Singh | c1bbed6 | 2019-12-02 17:36:25 -0800 | [diff] [blame^] | 234 | ret = udmabuf_create(filp->private_data, &head, list); |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 235 | kfree(list); |
| 236 | return ret; |
| 237 | } |
| 238 | |
| 239 | static 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 Hoffmann | 52499d9c | 2018-09-11 15:42:13 +0200 | [diff] [blame] | 252 | ret = -ENOTTY; |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 253 | break; |
| 254 | } |
| 255 | return ret; |
| 256 | } |
| 257 | |
| 258 | static const struct file_operations udmabuf_fops = { |
| 259 | .owner = THIS_MODULE, |
| 260 | .unlocked_ioctl = udmabuf_ioctl, |
| 261 | }; |
| 262 | |
| 263 | static struct miscdevice udmabuf_misc = { |
| 264 | .minor = MISC_DYNAMIC_MINOR, |
| 265 | .name = "udmabuf", |
| 266 | .fops = &udmabuf_fops, |
| 267 | }; |
| 268 | |
| 269 | static int __init udmabuf_dev_init(void) |
| 270 | { |
| 271 | return misc_register(&udmabuf_misc); |
| 272 | } |
| 273 | |
| 274 | static void __exit udmabuf_dev_exit(void) |
| 275 | { |
| 276 | misc_deregister(&udmabuf_misc); |
| 277 | } |
| 278 | |
| 279 | module_init(udmabuf_dev_init) |
| 280 | module_exit(udmabuf_dev_exit) |
| 281 | |
| 282 | MODULE_AUTHOR("Gerd Hoffmann <kraxel@redhat.com>"); |
| 283 | MODULE_LICENSE("GPL v2"); |