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