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 | |
| 23 | static int udmabuf_vm_fault(struct vm_fault *vmf) |
| 24 | { |
| 25 | struct vm_area_struct *vma = vmf->vma; |
| 26 | struct udmabuf *ubuf = vma->vm_private_data; |
| 27 | |
| 28 | if (WARN_ON(vmf->pgoff >= ubuf->pagecount)) |
| 29 | return VM_FAULT_SIGBUS; |
| 30 | |
| 31 | vmf->page = ubuf->pages[vmf->pgoff]; |
| 32 | get_page(vmf->page); |
| 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | static const struct vm_operations_struct udmabuf_vm_ops = { |
| 37 | .fault = udmabuf_vm_fault, |
| 38 | }; |
| 39 | |
| 40 | static int mmap_udmabuf(struct dma_buf *buf, struct vm_area_struct *vma) |
| 41 | { |
| 42 | struct udmabuf *ubuf = buf->priv; |
| 43 | |
| 44 | if ((vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) == 0) |
| 45 | return -EINVAL; |
| 46 | |
| 47 | vma->vm_ops = &udmabuf_vm_ops; |
| 48 | vma->vm_private_data = ubuf; |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | static struct sg_table *map_udmabuf(struct dma_buf_attachment *at, |
| 53 | enum dma_data_direction direction) |
| 54 | { |
| 55 | struct udmabuf *ubuf = at->dmabuf->priv; |
| 56 | struct sg_table *sg; |
Gerd Hoffmann | a3e722d | 2018-09-11 15:42:05 +0200 | [diff] [blame] | 57 | int ret; |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 58 | |
| 59 | sg = kzalloc(sizeof(*sg), GFP_KERNEL); |
| 60 | if (!sg) |
Gerd Hoffmann | a3e722d | 2018-09-11 15:42:05 +0200 | [diff] [blame] | 61 | return ERR_PTR(-ENOMEM); |
| 62 | ret = sg_alloc_table_from_pages(sg, ubuf->pages, ubuf->pagecount, |
| 63 | 0, ubuf->pagecount << PAGE_SHIFT, |
| 64 | GFP_KERNEL); |
| 65 | if (ret < 0) |
| 66 | goto err; |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 67 | if (!dma_map_sg(at->dev, sg->sgl, sg->nents, direction)) |
Gerd Hoffmann | a3e722d | 2018-09-11 15:42:05 +0200 | [diff] [blame] | 68 | goto err; |
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 | { |
| 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 | |
| 96 | static void *kmap_udmabuf(struct dma_buf *buf, unsigned long page_num) |
| 97 | { |
| 98 | struct udmabuf *ubuf = buf->priv; |
| 99 | struct page *page = ubuf->pages[page_num]; |
| 100 | |
| 101 | return kmap(page); |
| 102 | } |
| 103 | |
| 104 | static void kunmap_udmabuf(struct dma_buf *buf, unsigned long page_num, |
| 105 | void *vaddr) |
| 106 | { |
| 107 | kunmap(vaddr); |
| 108 | } |
| 109 | |
Gerd Hoffmann | a348528 | 2018-09-11 15:42:07 +0200 | [diff] [blame] | 110 | static const struct dma_buf_ops udmabuf_ops = { |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 111 | .map_dma_buf = map_udmabuf, |
| 112 | .unmap_dma_buf = unmap_udmabuf, |
| 113 | .release = release_udmabuf, |
| 114 | .map = kmap_udmabuf, |
| 115 | .unmap = kunmap_udmabuf, |
| 116 | .mmap = mmap_udmabuf, |
| 117 | }; |
| 118 | |
| 119 | #define SEALS_WANTED (F_SEAL_SHRINK) |
| 120 | #define SEALS_DENIED (F_SEAL_WRITE) |
| 121 | |
Gerd Hoffmann | dc25455 | 2018-09-11 15:42:08 +0200 | [diff] [blame] | 122 | static long udmabuf_create(const struct udmabuf_create_list *head, |
| 123 | const struct udmabuf_create_item *list) |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 124 | { |
| 125 | DEFINE_DMA_BUF_EXPORT_INFO(exp_info); |
| 126 | struct file *memfd = NULL; |
| 127 | struct udmabuf *ubuf; |
| 128 | struct dma_buf *buf; |
Gerd Hoffmann | 0d17455 | 2018-09-11 15:42:11 +0200 | [diff] [blame^] | 129 | pgoff_t pgoff, pgcnt, pgidx, pgbuf = 0, pglimit; |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 130 | struct page *page; |
| 131 | int seals, ret = -EINVAL; |
| 132 | u32 i, flags; |
| 133 | |
| 134 | ubuf = kzalloc(sizeof(struct udmabuf), GFP_KERNEL); |
| 135 | if (!ubuf) |
| 136 | return -ENOMEM; |
| 137 | |
Gerd Hoffmann | dc4716d | 2018-09-11 15:42:10 +0200 | [diff] [blame] | 138 | pglimit = (size_limit_mb * 1024 * 1024) >> PAGE_SHIFT; |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 139 | for (i = 0; i < head->count; i++) { |
| 140 | if (!IS_ALIGNED(list[i].offset, PAGE_SIZE)) |
Gerd Hoffmann | 0d17455 | 2018-09-11 15:42:11 +0200 | [diff] [blame^] | 141 | goto err; |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 142 | if (!IS_ALIGNED(list[i].size, PAGE_SIZE)) |
Gerd Hoffmann | 0d17455 | 2018-09-11 15:42:11 +0200 | [diff] [blame^] | 143 | goto err; |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 144 | ubuf->pagecount += list[i].size >> PAGE_SHIFT; |
Gerd Hoffmann | dc4716d | 2018-09-11 15:42:10 +0200 | [diff] [blame] | 145 | if (ubuf->pagecount > pglimit) |
Gerd Hoffmann | 0d17455 | 2018-09-11 15:42:11 +0200 | [diff] [blame^] | 146 | goto err; |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 147 | } |
| 148 | ubuf->pages = kmalloc_array(ubuf->pagecount, sizeof(struct page *), |
| 149 | GFP_KERNEL); |
| 150 | if (!ubuf->pages) { |
| 151 | ret = -ENOMEM; |
Gerd Hoffmann | 0d17455 | 2018-09-11 15:42:11 +0200 | [diff] [blame^] | 152 | goto err; |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | pgbuf = 0; |
| 156 | for (i = 0; i < head->count; i++) { |
| 157 | memfd = fget(list[i].memfd); |
| 158 | if (!memfd) |
Gerd Hoffmann | 0d17455 | 2018-09-11 15:42:11 +0200 | [diff] [blame^] | 159 | goto err; |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 160 | if (!shmem_mapping(file_inode(memfd)->i_mapping)) |
Gerd Hoffmann | 0d17455 | 2018-09-11 15:42:11 +0200 | [diff] [blame^] | 161 | goto err; |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 162 | seals = memfd_fcntl(memfd, F_GET_SEALS, 0); |
| 163 | if (seals == -EINVAL || |
| 164 | (seals & SEALS_WANTED) != SEALS_WANTED || |
| 165 | (seals & SEALS_DENIED) != 0) |
Gerd Hoffmann | 0d17455 | 2018-09-11 15:42:11 +0200 | [diff] [blame^] | 166 | goto err; |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 167 | pgoff = list[i].offset >> PAGE_SHIFT; |
| 168 | pgcnt = list[i].size >> PAGE_SHIFT; |
| 169 | for (pgidx = 0; pgidx < pgcnt; pgidx++) { |
| 170 | page = shmem_read_mapping_page( |
| 171 | file_inode(memfd)->i_mapping, pgoff + pgidx); |
| 172 | if (IS_ERR(page)) { |
| 173 | ret = PTR_ERR(page); |
Gerd Hoffmann | 0d17455 | 2018-09-11 15:42:11 +0200 | [diff] [blame^] | 174 | goto err; |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 175 | } |
| 176 | ubuf->pages[pgbuf++] = page; |
| 177 | } |
| 178 | fput(memfd); |
Gerd Hoffmann | 0d17455 | 2018-09-11 15:42:11 +0200 | [diff] [blame^] | 179 | memfd = NULL; |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 180 | } |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 181 | |
| 182 | exp_info.ops = &udmabuf_ops; |
| 183 | exp_info.size = ubuf->pagecount << PAGE_SHIFT; |
| 184 | exp_info.priv = ubuf; |
| 185 | |
| 186 | buf = dma_buf_export(&exp_info); |
| 187 | if (IS_ERR(buf)) { |
| 188 | ret = PTR_ERR(buf); |
Gerd Hoffmann | 0d17455 | 2018-09-11 15:42:11 +0200 | [diff] [blame^] | 189 | goto err; |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | flags = 0; |
| 193 | if (head->flags & UDMABUF_FLAGS_CLOEXEC) |
| 194 | flags |= O_CLOEXEC; |
| 195 | return dma_buf_fd(buf, flags); |
| 196 | |
Gerd Hoffmann | 0d17455 | 2018-09-11 15:42:11 +0200 | [diff] [blame^] | 197 | err: |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 198 | while (pgbuf > 0) |
| 199 | put_page(ubuf->pages[--pgbuf]); |
Gustavo A. R. Silva | 683a0e6 | 2018-09-04 14:07:49 -0500 | [diff] [blame] | 200 | if (memfd) |
| 201 | fput(memfd); |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 202 | kfree(ubuf->pages); |
| 203 | kfree(ubuf); |
| 204 | return ret; |
| 205 | } |
| 206 | |
| 207 | static long udmabuf_ioctl_create(struct file *filp, unsigned long arg) |
| 208 | { |
| 209 | struct udmabuf_create create; |
| 210 | struct udmabuf_create_list head; |
| 211 | struct udmabuf_create_item list; |
| 212 | |
| 213 | if (copy_from_user(&create, (void __user *)arg, |
| 214 | sizeof(struct udmabuf_create))) |
| 215 | return -EFAULT; |
| 216 | |
| 217 | head.flags = create.flags; |
| 218 | head.count = 1; |
| 219 | list.memfd = create.memfd; |
| 220 | list.offset = create.offset; |
| 221 | list.size = create.size; |
| 222 | |
| 223 | return udmabuf_create(&head, &list); |
| 224 | } |
| 225 | |
| 226 | static long udmabuf_ioctl_create_list(struct file *filp, unsigned long arg) |
| 227 | { |
| 228 | struct udmabuf_create_list head; |
| 229 | struct udmabuf_create_item *list; |
| 230 | int ret = -EINVAL; |
| 231 | u32 lsize; |
| 232 | |
| 233 | if (copy_from_user(&head, (void __user *)arg, sizeof(head))) |
| 234 | return -EFAULT; |
Gerd Hoffmann | dc4716d | 2018-09-11 15:42:10 +0200 | [diff] [blame] | 235 | if (head.count > list_limit) |
Gerd Hoffmann | fbb0de7 | 2018-08-27 11:34:44 +0200 | [diff] [blame] | 236 | return -EINVAL; |
| 237 | lsize = sizeof(struct udmabuf_create_item) * head.count; |
| 238 | list = memdup_user((void __user *)(arg + sizeof(head)), lsize); |
| 239 | if (IS_ERR(list)) |
| 240 | return PTR_ERR(list); |
| 241 | |
| 242 | ret = udmabuf_create(&head, list); |
| 243 | kfree(list); |
| 244 | return ret; |
| 245 | } |
| 246 | |
| 247 | static long udmabuf_ioctl(struct file *filp, unsigned int ioctl, |
| 248 | unsigned long arg) |
| 249 | { |
| 250 | long ret; |
| 251 | |
| 252 | switch (ioctl) { |
| 253 | case UDMABUF_CREATE: |
| 254 | ret = udmabuf_ioctl_create(filp, arg); |
| 255 | break; |
| 256 | case UDMABUF_CREATE_LIST: |
| 257 | ret = udmabuf_ioctl_create_list(filp, arg); |
| 258 | break; |
| 259 | default: |
| 260 | ret = -EINVAL; |
| 261 | break; |
| 262 | } |
| 263 | return ret; |
| 264 | } |
| 265 | |
| 266 | static const struct file_operations udmabuf_fops = { |
| 267 | .owner = THIS_MODULE, |
| 268 | .unlocked_ioctl = udmabuf_ioctl, |
| 269 | }; |
| 270 | |
| 271 | static struct miscdevice udmabuf_misc = { |
| 272 | .minor = MISC_DYNAMIC_MINOR, |
| 273 | .name = "udmabuf", |
| 274 | .fops = &udmabuf_fops, |
| 275 | }; |
| 276 | |
| 277 | static int __init udmabuf_dev_init(void) |
| 278 | { |
| 279 | return misc_register(&udmabuf_misc); |
| 280 | } |
| 281 | |
| 282 | static void __exit udmabuf_dev_exit(void) |
| 283 | { |
| 284 | misc_deregister(&udmabuf_misc); |
| 285 | } |
| 286 | |
| 287 | module_init(udmabuf_dev_init) |
| 288 | module_exit(udmabuf_dev_exit) |
| 289 | |
| 290 | MODULE_AUTHOR("Gerd Hoffmann <kraxel@redhat.com>"); |
| 291 | MODULE_LICENSE("GPL v2"); |