1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2011 Google, Inc.
8 #include <linux/debugfs.h>
9 #include <linux/device.h>
10 #include <linux/dma-buf.h>
11 #include <linux/err.h>
12 #include <linux/export.h>
13 #include <linux/file.h>
14 #include <linux/freezer.h>
16 #include <linux/kthread.h>
17 #include <linux/list.h>
18 #include <linux/miscdevice.h>
20 #include <linux/mm_types.h>
21 #include <linux/rbtree.h>
22 #include <linux/sched/task.h>
23 #include <linux/slab.h>
24 #include <linux/uaccess.h>
25 #include <linux/vmalloc.h>
29 static struct ion_device *internal_dev;
32 /* this function should only be called while dev->lock is held */
33 static void ion_buffer_add(struct ion_device *dev,
34 struct ion_buffer *buffer)
36 struct rb_node **p = &dev->buffers.rb_node;
37 struct rb_node *parent = NULL;
38 struct ion_buffer *entry;
42 entry = rb_entry(parent, struct ion_buffer, node);
46 } else if (buffer > entry) {
49 pr_err("%s: buffer already found.", __func__);
54 rb_link_node(&buffer->node, parent, p);
55 rb_insert_color(&buffer->node, &dev->buffers);
58 /* this function should only be called while dev->lock is held */
59 static struct ion_buffer *ion_buffer_create(struct ion_heap *heap,
60 struct ion_device *dev,
64 struct ion_buffer *buffer;
67 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
69 return ERR_PTR(-ENOMEM);
72 buffer->flags = flags;
76 ret = heap->ops->allocate(heap, buffer, len, flags);
79 if (!(heap->flags & ION_HEAP_FLAG_DEFER_FREE))
82 ion_heap_freelist_drain(heap, 0);
83 ret = heap->ops->allocate(heap, buffer, len, flags);
88 if (!buffer->sg_table) {
89 WARN_ONCE(1, "This heap needs to set the sgtable");
94 spin_lock(&heap->stat_lock);
95 heap->num_of_buffers++;
96 heap->num_of_alloc_bytes += len;
97 if (heap->num_of_alloc_bytes > heap->alloc_bytes_wm)
98 heap->alloc_bytes_wm = heap->num_of_alloc_bytes;
99 spin_unlock(&heap->stat_lock);
101 INIT_LIST_HEAD(&buffer->attachments);
102 mutex_init(&buffer->lock);
103 mutex_lock(&dev->buffer_lock);
104 ion_buffer_add(dev, buffer);
105 mutex_unlock(&dev->buffer_lock);
109 heap->ops->free(buffer);
115 void ion_buffer_destroy(struct ion_buffer *buffer)
117 if (buffer->kmap_cnt > 0) {
118 pr_warn_once("%s: buffer still mapped in the kernel\n",
120 buffer->heap->ops->unmap_kernel(buffer->heap, buffer);
122 buffer->heap->ops->free(buffer);
123 spin_lock(&buffer->heap->stat_lock);
124 buffer->heap->num_of_buffers--;
125 buffer->heap->num_of_alloc_bytes -= buffer->size;
126 spin_unlock(&buffer->heap->stat_lock);
131 static void _ion_buffer_destroy(struct ion_buffer *buffer)
133 struct ion_heap *heap = buffer->heap;
134 struct ion_device *dev = buffer->dev;
136 mutex_lock(&dev->buffer_lock);
137 rb_erase(&buffer->node, &dev->buffers);
138 mutex_unlock(&dev->buffer_lock);
140 if (heap->flags & ION_HEAP_FLAG_DEFER_FREE)
141 ion_heap_freelist_add(heap, buffer);
143 ion_buffer_destroy(buffer);
146 static void *ion_buffer_kmap_get(struct ion_buffer *buffer)
150 if (buffer->kmap_cnt) {
152 return buffer->vaddr;
154 vaddr = buffer->heap->ops->map_kernel(buffer->heap, buffer);
155 if (WARN_ONCE(!vaddr,
156 "heap->ops->map_kernel should return ERR_PTR on error"))
157 return ERR_PTR(-EINVAL);
160 buffer->vaddr = vaddr;
165 static void ion_buffer_kmap_put(struct ion_buffer *buffer)
168 if (!buffer->kmap_cnt) {
169 buffer->heap->ops->unmap_kernel(buffer->heap, buffer);
170 buffer->vaddr = NULL;
174 static struct sg_table *dup_sg_table(struct sg_table *table)
176 struct sg_table *new_table;
178 struct scatterlist *sg, *new_sg;
180 new_table = kzalloc(sizeof(*new_table), GFP_KERNEL);
182 return ERR_PTR(-ENOMEM);
184 ret = sg_alloc_table(new_table, table->nents, GFP_KERNEL);
187 return ERR_PTR(-ENOMEM);
190 new_sg = new_table->sgl;
191 for_each_sg(table->sgl, sg, table->nents, i) {
192 memcpy(new_sg, sg, sizeof(*sg));
193 new_sg->dma_address = 0;
194 new_sg = sg_next(new_sg);
200 static void free_duped_table(struct sg_table *table)
202 sg_free_table(table);
206 struct ion_dma_buf_attachment {
208 struct sg_table *table;
209 struct list_head list;
212 static int ion_dma_buf_attach(struct dma_buf *dmabuf,
213 struct dma_buf_attachment *attachment)
215 struct ion_dma_buf_attachment *a;
216 struct sg_table *table;
217 struct ion_buffer *buffer = dmabuf->priv;
219 a = kzalloc(sizeof(*a), GFP_KERNEL);
223 table = dup_sg_table(buffer->sg_table);
230 a->dev = attachment->dev;
231 INIT_LIST_HEAD(&a->list);
233 attachment->priv = a;
235 mutex_lock(&buffer->lock);
236 list_add(&a->list, &buffer->attachments);
237 mutex_unlock(&buffer->lock);
242 static void ion_dma_buf_detatch(struct dma_buf *dmabuf,
243 struct dma_buf_attachment *attachment)
245 struct ion_dma_buf_attachment *a = attachment->priv;
246 struct ion_buffer *buffer = dmabuf->priv;
248 mutex_lock(&buffer->lock);
250 mutex_unlock(&buffer->lock);
251 free_duped_table(a->table);
256 static struct sg_table *ion_map_dma_buf(struct dma_buf_attachment *attachment,
257 enum dma_data_direction direction)
259 struct ion_dma_buf_attachment *a = attachment->priv;
260 struct sg_table *table;
264 if (!dma_map_sg(attachment->dev, table->sgl, table->nents,
266 return ERR_PTR(-ENOMEM);
271 static void ion_unmap_dma_buf(struct dma_buf_attachment *attachment,
272 struct sg_table *table,
273 enum dma_data_direction direction)
275 dma_unmap_sg(attachment->dev, table->sgl, table->nents, direction);
278 static int ion_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma)
280 struct ion_buffer *buffer = dmabuf->priv;
283 if (!buffer->heap->ops->map_user) {
284 pr_err("%s: this heap does not define a method for mapping to userspace\n",
289 if (!(buffer->flags & ION_FLAG_CACHED))
290 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
292 mutex_lock(&buffer->lock);
293 /* now map it to userspace */
294 ret = buffer->heap->ops->map_user(buffer->heap, buffer, vma);
295 mutex_unlock(&buffer->lock);
298 pr_err("%s: failure mapping buffer to userspace\n",
304 static void ion_dma_buf_release(struct dma_buf *dmabuf)
306 struct ion_buffer *buffer = dmabuf->priv;
308 _ion_buffer_destroy(buffer);
311 static void *ion_dma_buf_kmap(struct dma_buf *dmabuf, unsigned long offset)
313 struct ion_buffer *buffer = dmabuf->priv;
315 return buffer->vaddr + offset * PAGE_SIZE;
318 static void ion_dma_buf_kunmap(struct dma_buf *dmabuf, unsigned long offset,
323 static int ion_dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
324 enum dma_data_direction direction)
326 struct ion_buffer *buffer = dmabuf->priv;
328 struct ion_dma_buf_attachment *a;
332 * TODO: Move this elsewhere because we don't always need a vaddr
334 if (buffer->heap->ops->map_kernel) {
335 mutex_lock(&buffer->lock);
336 vaddr = ion_buffer_kmap_get(buffer);
338 ret = PTR_ERR(vaddr);
341 mutex_unlock(&buffer->lock);
344 mutex_lock(&buffer->lock);
345 list_for_each_entry(a, &buffer->attachments, list) {
346 dma_sync_sg_for_cpu(a->dev, a->table->sgl, a->table->nents,
351 mutex_unlock(&buffer->lock);
355 static int ion_dma_buf_end_cpu_access(struct dma_buf *dmabuf,
356 enum dma_data_direction direction)
358 struct ion_buffer *buffer = dmabuf->priv;
359 struct ion_dma_buf_attachment *a;
361 if (buffer->heap->ops->map_kernel) {
362 mutex_lock(&buffer->lock);
363 ion_buffer_kmap_put(buffer);
364 mutex_unlock(&buffer->lock);
367 mutex_lock(&buffer->lock);
368 list_for_each_entry(a, &buffer->attachments, list) {
369 dma_sync_sg_for_device(a->dev, a->table->sgl, a->table->nents,
372 mutex_unlock(&buffer->lock);
377 static const struct dma_buf_ops dma_buf_ops = {
378 .map_dma_buf = ion_map_dma_buf,
379 .unmap_dma_buf = ion_unmap_dma_buf,
381 .release = ion_dma_buf_release,
382 .attach = ion_dma_buf_attach,
383 .detach = ion_dma_buf_detatch,
384 .begin_cpu_access = ion_dma_buf_begin_cpu_access,
385 .end_cpu_access = ion_dma_buf_end_cpu_access,
386 .map = ion_dma_buf_kmap,
387 .unmap = ion_dma_buf_kunmap,
390 static int ion_alloc(size_t len, unsigned int heap_id_mask, unsigned int flags)
392 struct ion_device *dev = internal_dev;
393 struct ion_buffer *buffer = NULL;
394 struct ion_heap *heap;
395 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
397 struct dma_buf *dmabuf;
399 pr_debug("%s: len %zu heap_id_mask %u flags %x\n", __func__,
400 len, heap_id_mask, flags);
402 * traverse the list of heaps available in this system in priority
403 * order. If the heap type is supported by the client, and matches the
404 * request of the caller allocate from it. Repeat until allocate has
405 * succeeded or all heaps have been tried
407 len = PAGE_ALIGN(len);
412 down_read(&dev->lock);
413 plist_for_each_entry(heap, &dev->heaps, node) {
414 /* if the caller didn't specify this heap id */
415 if (!((1 << heap->id) & heap_id_mask))
417 buffer = ion_buffer_create(heap, dev, len, flags);
427 return PTR_ERR(buffer);
429 exp_info.ops = &dma_buf_ops;
430 exp_info.size = buffer->size;
431 exp_info.flags = O_RDWR;
432 exp_info.priv = buffer;
434 dmabuf = dma_buf_export(&exp_info);
435 if (IS_ERR(dmabuf)) {
436 _ion_buffer_destroy(buffer);
437 return PTR_ERR(dmabuf);
440 fd = dma_buf_fd(dmabuf, O_CLOEXEC);
447 static int ion_query_heaps(struct ion_heap_query *query)
449 struct ion_device *dev = internal_dev;
450 struct ion_heap_data __user *buffer = u64_to_user_ptr(query->heaps);
451 int ret = -EINVAL, cnt = 0, max_cnt;
452 struct ion_heap *heap;
453 struct ion_heap_data hdata;
455 memset(&hdata, 0, sizeof(hdata));
457 down_read(&dev->lock);
459 query->cnt = dev->heap_cnt;
467 max_cnt = query->cnt;
469 plist_for_each_entry(heap, &dev->heaps, node) {
470 strncpy(hdata.name, heap->name, MAX_HEAP_NAME);
471 hdata.name[sizeof(hdata.name) - 1] = '\0';
472 hdata.type = heap->type;
473 hdata.heap_id = heap->id;
475 if (copy_to_user(&buffer[cnt], &hdata, sizeof(hdata))) {
492 union ion_ioctl_arg {
493 struct ion_allocation_data allocation;
494 struct ion_heap_query query;
497 static int validate_ioctl_arg(unsigned int cmd, union ion_ioctl_arg *arg)
500 case ION_IOC_HEAP_QUERY:
501 if (arg->query.reserved0 ||
502 arg->query.reserved1 ||
503 arg->query.reserved2)
513 static long ion_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
516 union ion_ioctl_arg data;
518 if (_IOC_SIZE(cmd) > sizeof(data))
522 * The copy_from_user is unconditional here for both read and write
523 * to do the validate. If there is no write for the ioctl, the
526 if (copy_from_user(&data, (void __user *)arg, _IOC_SIZE(cmd)))
529 ret = validate_ioctl_arg(cmd, &data);
531 pr_warn_once("%s: ioctl validate failed\n", __func__);
535 if (!(_IOC_DIR(cmd) & _IOC_WRITE))
536 memset(&data, 0, sizeof(data));
543 fd = ion_alloc(data.allocation.len,
544 data.allocation.heap_id_mask,
545 data.allocation.flags);
549 data.allocation.fd = fd;
553 case ION_IOC_HEAP_QUERY:
554 ret = ion_query_heaps(&data.query);
560 if (_IOC_DIR(cmd) & _IOC_READ) {
561 if (copy_to_user((void __user *)arg, &data, _IOC_SIZE(cmd)))
567 static const struct file_operations ion_fops = {
568 .owner = THIS_MODULE,
569 .unlocked_ioctl = ion_ioctl,
571 .compat_ioctl = ion_ioctl,
575 static int debug_shrink_set(void *data, u64 val)
577 struct ion_heap *heap = data;
578 struct shrink_control sc;
581 sc.gfp_mask = GFP_HIGHUSER;
585 objs = heap->shrinker.count_objects(&heap->shrinker, &sc);
586 sc.nr_to_scan = objs;
589 heap->shrinker.scan_objects(&heap->shrinker, &sc);
593 static int debug_shrink_get(void *data, u64 *val)
595 struct ion_heap *heap = data;
596 struct shrink_control sc;
599 sc.gfp_mask = GFP_HIGHUSER;
602 objs = heap->shrinker.count_objects(&heap->shrinker, &sc);
607 DEFINE_SIMPLE_ATTRIBUTE(debug_shrink_fops, debug_shrink_get,
608 debug_shrink_set, "%llu\n");
610 void ion_device_add_heap(struct ion_heap *heap)
612 struct ion_device *dev = internal_dev;
614 struct dentry *heap_root;
617 if (!heap->ops->allocate || !heap->ops->free)
618 pr_err("%s: can not add heap with invalid ops struct.\n",
621 spin_lock_init(&heap->free_lock);
622 spin_lock_init(&heap->stat_lock);
623 heap->free_list_size = 0;
625 if (heap->flags & ION_HEAP_FLAG_DEFER_FREE)
626 ion_heap_init_deferred_free(heap);
628 if ((heap->flags & ION_HEAP_FLAG_DEFER_FREE) || heap->ops->shrink) {
629 ret = ion_heap_init_shrinker(heap);
631 pr_err("%s: Failed to register shrinker\n", __func__);
635 heap->num_of_buffers = 0;
636 heap->num_of_alloc_bytes = 0;
637 heap->alloc_bytes_wm = 0;
639 heap_root = debugfs_create_dir(heap->name, dev->debug_root);
640 debugfs_create_u64("num_of_buffers",
642 &heap->num_of_buffers);
643 debugfs_create_u64("num_of_alloc_bytes",
646 &heap->num_of_alloc_bytes);
647 debugfs_create_u64("alloc_bytes_wm",
650 &heap->alloc_bytes_wm);
652 if (heap->shrinker.count_objects &&
653 heap->shrinker.scan_objects) {
654 snprintf(debug_name, 64, "%s_shrink", heap->name);
655 debugfs_create_file(debug_name,
662 down_write(&dev->lock);
663 heap->id = heap_id++;
665 * use negative heap->id to reverse the priority -- when traversing
666 * the list later attempt higher id numbers first
668 plist_node_init(&heap->node, -heap->id);
669 plist_add(&heap->node, &dev->heaps);
672 up_write(&dev->lock);
674 EXPORT_SYMBOL(ion_device_add_heap);
676 static int ion_device_create(void)
678 struct ion_device *idev;
681 idev = kzalloc(sizeof(*idev), GFP_KERNEL);
685 idev->dev.minor = MISC_DYNAMIC_MINOR;
686 idev->dev.name = "ion";
687 idev->dev.fops = &ion_fops;
688 idev->dev.parent = NULL;
689 ret = misc_register(&idev->dev);
691 pr_err("ion: failed to register misc device.\n");
696 idev->debug_root = debugfs_create_dir("ion", NULL);
697 idev->buffers = RB_ROOT;
698 mutex_init(&idev->buffer_lock);
699 init_rwsem(&idev->lock);
700 plist_head_init(&idev->heaps);
704 subsys_initcall(ion_device_create);