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