3 * drivers/gpu/ion/ion.c
5 * Copyright (C) 2011 Google, Inc.
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
18 #include <linux/device.h>
19 #include <linux/file.h>
20 #include <linux/freezer.h>
22 #include <linux/anon_inodes.h>
23 #include <linux/kthread.h>
24 #include <linux/list.h>
25 #include <linux/memblock.h>
26 #include <linux/miscdevice.h>
27 #include <linux/export.h>
29 #include <linux/mm_types.h>
30 #include <linux/rbtree.h>
31 #include <linux/slab.h>
32 #include <linux/seq_file.h>
33 #include <linux/uaccess.h>
34 #include <linux/vmalloc.h>
35 #include <linux/debugfs.h>
36 #include <linux/dma-buf.h>
37 #include <linux/idr.h>
41 #include "compat_ion.h"
44 * struct ion_device - the metadata of the ion device node
45 * @dev: the actual misc device
46 * @buffers: an rb tree of all the existing buffers
47 * @buffer_lock: lock protecting the tree of buffers
48 * @lock: rwsem protecting the tree of heaps and clients
49 * @heaps: list of all the heaps in the system
50 * @user_clients: list of all the clients created from userspace
53 struct miscdevice dev;
54 struct rb_root buffers;
55 struct mutex buffer_lock;
56 struct rw_semaphore lock;
57 struct plist_head heaps;
58 long (*custom_ioctl) (struct ion_client *client, unsigned int cmd,
60 struct rb_root clients;
61 struct dentry *debug_root;
62 struct dentry *heaps_debug_root;
63 struct dentry *clients_debug_root;
67 * struct ion_client - a process/hw block local address space
68 * @node: node in the tree of all clients
69 * @dev: backpointer to ion device
70 * @handles: an rb tree of all the handles in this client
71 * @idr: an idr space for allocating handle ids
72 * @lock: lock protecting the tree of handles
73 * @name: used for debugging
74 * @display_name: used for debugging (unique version of @name)
75 * @display_serial: used for debugging (to make display_name unique)
76 * @task: used for debugging
78 * A client represents a list of buffers this client may access.
79 * The mutex stored here is used to protect both handles tree
80 * as well as the handles themselves, and should be held while modifying either.
84 struct ion_device *dev;
85 struct rb_root handles;
91 struct task_struct *task;
93 struct dentry *debug_root;
97 * ion_handle - a client local reference to a buffer
98 * @ref: reference count
99 * @client: back pointer to the client the buffer resides in
100 * @buffer: pointer to the buffer
101 * @node: node in the client's handle rbtree
102 * @kmap_cnt: count of times this client has mapped to kernel
103 * @id: client-unique id allocated by client->idr
105 * Modifications to node, map_cnt or mapping should be protected by the
106 * lock in the client. Other fields are never changed after initialization.
110 struct ion_client *client;
111 struct ion_buffer *buffer;
113 unsigned int kmap_cnt;
117 bool ion_buffer_fault_user_mappings(struct ion_buffer *buffer)
119 return (buffer->flags & ION_FLAG_CACHED) &&
120 !(buffer->flags & ION_FLAG_CACHED_NEEDS_SYNC);
123 bool ion_buffer_cached(struct ion_buffer *buffer)
125 return !!(buffer->flags & ION_FLAG_CACHED);
128 static inline struct page *ion_buffer_page(struct page *page)
130 return (struct page *)((unsigned long)page & ~(1UL));
133 static inline bool ion_buffer_page_is_dirty(struct page *page)
135 return !!((unsigned long)page & 1UL);
138 static inline void ion_buffer_page_dirty(struct page **page)
140 *page = (struct page *)((unsigned long)(*page) | 1UL);
143 static inline void ion_buffer_page_clean(struct page **page)
145 *page = (struct page *)((unsigned long)(*page) & ~(1UL));
148 /* this function should only be called while dev->lock is held */
149 static void ion_buffer_add(struct ion_device *dev,
150 struct ion_buffer *buffer)
152 struct rb_node **p = &dev->buffers.rb_node;
153 struct rb_node *parent = NULL;
154 struct ion_buffer *entry;
158 entry = rb_entry(parent, struct ion_buffer, node);
160 if (buffer < entry) {
162 } else if (buffer > entry) {
165 pr_err("%s: buffer already found.", __func__);
170 rb_link_node(&buffer->node, parent, p);
171 rb_insert_color(&buffer->node, &dev->buffers);
174 /* this function should only be called while dev->lock is held */
175 static struct ion_buffer *ion_buffer_create(struct ion_heap *heap,
176 struct ion_device *dev,
181 struct ion_buffer *buffer;
182 struct sg_table *table;
183 struct scatterlist *sg;
186 buffer = kzalloc(sizeof(struct ion_buffer), GFP_KERNEL);
188 return ERR_PTR(-ENOMEM);
191 buffer->flags = flags;
192 kref_init(&buffer->ref);
194 ret = heap->ops->allocate(heap, buffer, len, align, flags);
197 if (!(heap->flags & ION_HEAP_FLAG_DEFER_FREE))
200 ion_heap_freelist_drain(heap, 0);
201 ret = heap->ops->allocate(heap, buffer, len, align,
210 table = heap->ops->map_dma(heap, buffer);
211 if (WARN_ONCE(table == NULL,
212 "heap->ops->map_dma should return ERR_PTR on error"))
213 table = ERR_PTR(-EINVAL);
215 heap->ops->free(buffer);
217 return ERR_PTR(PTR_ERR(table));
219 buffer->sg_table = table;
220 if (ion_buffer_fault_user_mappings(buffer)) {
221 int num_pages = PAGE_ALIGN(buffer->size) / PAGE_SIZE;
222 struct scatterlist *sg;
225 buffer->pages = vmalloc(sizeof(struct page *) * num_pages);
226 if (!buffer->pages) {
231 for_each_sg(table->sgl, sg, table->nents, i) {
232 struct page *page = sg_page(sg);
234 for (j = 0; j < sg->length / PAGE_SIZE; j++)
235 buffer->pages[k++] = page++;
244 INIT_LIST_HEAD(&buffer->vmas);
245 mutex_init(&buffer->lock);
246 /* this will set up dma addresses for the sglist -- it is not
247 technically correct as per the dma api -- a specific
248 device isn't really taking ownership here. However, in practice on
249 our systems the only dma_address space is physical addresses.
250 Additionally, we can't afford the overhead of invalidating every
251 allocation via dma_map_sg. The implicit contract here is that
252 memory comming from the heaps is ready for dma, ie if it has a
253 cached mapping that mapping has been invalidated */
254 for_each_sg(buffer->sg_table->sgl, sg, buffer->sg_table->nents, i)
255 sg_dma_address(sg) = sg_phys(sg);
256 mutex_lock(&dev->buffer_lock);
257 ion_buffer_add(dev, buffer);
258 mutex_unlock(&dev->buffer_lock);
262 heap->ops->unmap_dma(heap, buffer);
263 heap->ops->free(buffer);
266 vfree(buffer->pages);
272 void ion_buffer_destroy(struct ion_buffer *buffer)
274 if (WARN_ON(buffer->kmap_cnt > 0))
275 buffer->heap->ops->unmap_kernel(buffer->heap, buffer);
276 buffer->heap->ops->unmap_dma(buffer->heap, buffer);
277 buffer->heap->ops->free(buffer);
279 vfree(buffer->pages);
283 static void _ion_buffer_destroy(struct kref *kref)
285 struct ion_buffer *buffer = container_of(kref, struct ion_buffer, ref);
286 struct ion_heap *heap = buffer->heap;
287 struct ion_device *dev = buffer->dev;
289 mutex_lock(&dev->buffer_lock);
290 rb_erase(&buffer->node, &dev->buffers);
291 mutex_unlock(&dev->buffer_lock);
293 if (heap->flags & ION_HEAP_FLAG_DEFER_FREE)
294 ion_heap_freelist_add(heap, buffer);
296 ion_buffer_destroy(buffer);
299 static void ion_buffer_get(struct ion_buffer *buffer)
301 kref_get(&buffer->ref);
304 static int ion_buffer_put(struct ion_buffer *buffer)
306 return kref_put(&buffer->ref, _ion_buffer_destroy);
309 static void ion_buffer_add_to_handle(struct ion_buffer *buffer)
311 mutex_lock(&buffer->lock);
312 buffer->handle_count++;
313 mutex_unlock(&buffer->lock);
316 static void ion_buffer_remove_from_handle(struct ion_buffer *buffer)
319 * when a buffer is removed from a handle, if it is not in
320 * any other handles, copy the taskcomm and the pid of the
321 * process it's being removed from into the buffer. At this
322 * point there will be no way to track what processes this buffer is
323 * being used by, it only exists as a dma_buf file descriptor.
324 * The taskcomm and pid can provide a debug hint as to where this fd
327 mutex_lock(&buffer->lock);
328 buffer->handle_count--;
329 BUG_ON(buffer->handle_count < 0);
330 if (!buffer->handle_count) {
331 struct task_struct *task;
333 task = current->group_leader;
334 get_task_comm(buffer->task_comm, task);
335 buffer->pid = task_pid_nr(task);
337 mutex_unlock(&buffer->lock);
340 static struct ion_handle *ion_handle_create(struct ion_client *client,
341 struct ion_buffer *buffer)
343 struct ion_handle *handle;
345 handle = kzalloc(sizeof(struct ion_handle), GFP_KERNEL);
347 return ERR_PTR(-ENOMEM);
348 kref_init(&handle->ref);
349 RB_CLEAR_NODE(&handle->node);
350 handle->client = client;
351 ion_buffer_get(buffer);
352 ion_buffer_add_to_handle(buffer);
353 handle->buffer = buffer;
358 static void ion_handle_kmap_put(struct ion_handle *);
360 static void ion_handle_destroy(struct kref *kref)
362 struct ion_handle *handle = container_of(kref, struct ion_handle, ref);
363 struct ion_client *client = handle->client;
364 struct ion_buffer *buffer = handle->buffer;
366 mutex_lock(&buffer->lock);
367 while (handle->kmap_cnt)
368 ion_handle_kmap_put(handle);
369 mutex_unlock(&buffer->lock);
371 idr_remove(&client->idr, handle->id);
372 if (!RB_EMPTY_NODE(&handle->node))
373 rb_erase(&handle->node, &client->handles);
375 ion_buffer_remove_from_handle(buffer);
376 ion_buffer_put(buffer);
381 struct ion_buffer *ion_handle_buffer(struct ion_handle *handle)
383 return handle->buffer;
386 static void ion_handle_get(struct ion_handle *handle)
388 kref_get(&handle->ref);
391 static int ion_handle_put(struct ion_handle *handle)
393 struct ion_client *client = handle->client;
396 mutex_lock(&client->lock);
397 ret = kref_put(&handle->ref, ion_handle_destroy);
398 mutex_unlock(&client->lock);
403 static struct ion_handle *ion_handle_lookup(struct ion_client *client,
404 struct ion_buffer *buffer)
406 struct rb_node *n = client->handles.rb_node;
409 struct ion_handle *entry = rb_entry(n, struct ion_handle, node);
411 if (buffer < entry->buffer)
413 else if (buffer > entry->buffer)
418 return ERR_PTR(-EINVAL);
421 static struct ion_handle *ion_handle_get_by_id(struct ion_client *client,
424 struct ion_handle *handle;
426 mutex_lock(&client->lock);
427 handle = idr_find(&client->idr, id);
429 ion_handle_get(handle);
430 mutex_unlock(&client->lock);
432 return handle ? handle : ERR_PTR(-EINVAL);
435 static bool ion_handle_validate(struct ion_client *client,
436 struct ion_handle *handle)
438 WARN_ON(!mutex_is_locked(&client->lock));
439 return (idr_find(&client->idr, handle->id) == handle);
442 static int ion_handle_add(struct ion_client *client, struct ion_handle *handle)
445 struct rb_node **p = &client->handles.rb_node;
446 struct rb_node *parent = NULL;
447 struct ion_handle *entry;
449 id = idr_alloc(&client->idr, handle, 1, 0, GFP_KERNEL);
457 entry = rb_entry(parent, struct ion_handle, node);
459 if (handle->buffer < entry->buffer)
461 else if (handle->buffer > entry->buffer)
464 WARN(1, "%s: buffer already found.", __func__);
467 rb_link_node(&handle->node, parent, p);
468 rb_insert_color(&handle->node, &client->handles);
473 struct ion_handle *ion_alloc(struct ion_client *client, size_t len,
474 size_t align, unsigned int heap_id_mask,
477 struct ion_handle *handle;
478 struct ion_device *dev = client->dev;
479 struct ion_buffer *buffer = NULL;
480 struct ion_heap *heap;
483 pr_debug("%s: len %zu align %zu heap_id_mask %u flags %x\n", __func__,
484 len, align, heap_id_mask, flags);
486 * traverse the list of heaps available in this system in priority
487 * order. If the heap type is supported by the client, and matches the
488 * request of the caller allocate from it. Repeat until allocate has
489 * succeeded or all heaps have been tried
491 len = PAGE_ALIGN(len);
494 return ERR_PTR(-EINVAL);
496 down_read(&dev->lock);
497 plist_for_each_entry(heap, &dev->heaps, node) {
498 /* if the caller didn't specify this heap id */
499 if (!((1 << heap->id) & heap_id_mask))
501 buffer = ion_buffer_create(heap, dev, len, align, flags);
508 return ERR_PTR(-ENODEV);
511 return ERR_PTR(PTR_ERR(buffer));
513 handle = ion_handle_create(client, buffer);
516 * ion_buffer_create will create a buffer with a ref_cnt of 1,
517 * and ion_handle_create will take a second reference, drop one here
519 ion_buffer_put(buffer);
524 mutex_lock(&client->lock);
525 ret = ion_handle_add(client, handle);
526 mutex_unlock(&client->lock);
528 ion_handle_put(handle);
529 handle = ERR_PTR(ret);
534 EXPORT_SYMBOL(ion_alloc);
536 void ion_free(struct ion_client *client, struct ion_handle *handle)
540 BUG_ON(client != handle->client);
542 mutex_lock(&client->lock);
543 valid_handle = ion_handle_validate(client, handle);
546 WARN(1, "%s: invalid handle passed to free.\n", __func__);
547 mutex_unlock(&client->lock);
550 mutex_unlock(&client->lock);
551 ion_handle_put(handle);
553 EXPORT_SYMBOL(ion_free);
555 int ion_phys(struct ion_client *client, struct ion_handle *handle,
556 ion_phys_addr_t *addr, size_t *len)
558 struct ion_buffer *buffer;
561 mutex_lock(&client->lock);
562 if (!ion_handle_validate(client, handle)) {
563 mutex_unlock(&client->lock);
567 buffer = handle->buffer;
569 if (!buffer->heap->ops->phys) {
570 pr_err("%s: ion_phys is not implemented by this heap.\n",
572 mutex_unlock(&client->lock);
575 mutex_unlock(&client->lock);
576 ret = buffer->heap->ops->phys(buffer->heap, buffer, addr, len);
579 EXPORT_SYMBOL(ion_phys);
581 static void *ion_buffer_kmap_get(struct ion_buffer *buffer)
585 if (buffer->kmap_cnt) {
587 return buffer->vaddr;
589 vaddr = buffer->heap->ops->map_kernel(buffer->heap, buffer);
590 if (WARN_ONCE(vaddr == NULL,
591 "heap->ops->map_kernel should return ERR_PTR on error"))
592 return ERR_PTR(-EINVAL);
595 buffer->vaddr = vaddr;
600 static void *ion_handle_kmap_get(struct ion_handle *handle)
602 struct ion_buffer *buffer = handle->buffer;
605 if (handle->kmap_cnt) {
607 return buffer->vaddr;
609 vaddr = ion_buffer_kmap_get(buffer);
616 static void ion_buffer_kmap_put(struct ion_buffer *buffer)
619 if (!buffer->kmap_cnt) {
620 buffer->heap->ops->unmap_kernel(buffer->heap, buffer);
621 buffer->vaddr = NULL;
625 static void ion_handle_kmap_put(struct ion_handle *handle)
627 struct ion_buffer *buffer = handle->buffer;
630 if (!handle->kmap_cnt)
631 ion_buffer_kmap_put(buffer);
634 void *ion_map_kernel(struct ion_client *client, struct ion_handle *handle)
636 struct ion_buffer *buffer;
639 mutex_lock(&client->lock);
640 if (!ion_handle_validate(client, handle)) {
641 pr_err("%s: invalid handle passed to map_kernel.\n",
643 mutex_unlock(&client->lock);
644 return ERR_PTR(-EINVAL);
647 buffer = handle->buffer;
649 if (!handle->buffer->heap->ops->map_kernel) {
650 pr_err("%s: map_kernel is not implemented by this heap.\n",
652 mutex_unlock(&client->lock);
653 return ERR_PTR(-ENODEV);
656 mutex_lock(&buffer->lock);
657 vaddr = ion_handle_kmap_get(handle);
658 mutex_unlock(&buffer->lock);
659 mutex_unlock(&client->lock);
662 EXPORT_SYMBOL(ion_map_kernel);
664 void ion_unmap_kernel(struct ion_client *client, struct ion_handle *handle)
666 struct ion_buffer *buffer;
668 mutex_lock(&client->lock);
669 buffer = handle->buffer;
670 mutex_lock(&buffer->lock);
671 ion_handle_kmap_put(handle);
672 mutex_unlock(&buffer->lock);
673 mutex_unlock(&client->lock);
675 EXPORT_SYMBOL(ion_unmap_kernel);
677 static int ion_debug_client_show(struct seq_file *s, void *unused)
679 struct ion_client *client = s->private;
681 size_t sizes[ION_NUM_HEAP_IDS] = {0};
682 const char *names[ION_NUM_HEAP_IDS] = {NULL};
685 mutex_lock(&client->lock);
686 for (n = rb_first(&client->handles); n; n = rb_next(n)) {
687 struct ion_handle *handle = rb_entry(n, struct ion_handle,
689 unsigned int id = handle->buffer->heap->id;
692 names[id] = handle->buffer->heap->name;
693 sizes[id] += handle->buffer->size;
695 mutex_unlock(&client->lock);
697 seq_printf(s, "%16.16s: %16.16s\n", "heap_name", "size_in_bytes");
698 for (i = 0; i < ION_NUM_HEAP_IDS; i++) {
701 seq_printf(s, "%16.16s: %16zu\n", names[i], sizes[i]);
706 static int ion_debug_client_open(struct inode *inode, struct file *file)
708 return single_open(file, ion_debug_client_show, inode->i_private);
711 static const struct file_operations debug_client_fops = {
712 .open = ion_debug_client_open,
715 .release = single_release,
718 static int ion_get_client_serial(const struct rb_root *root,
719 const unsigned char *name)
722 struct rb_node *node;
724 for (node = rb_first(root); node; node = rb_next(node)) {
725 struct ion_client *client = rb_entry(node, struct ion_client,
728 if (strcmp(client->name, name))
730 serial = max(serial, client->display_serial);
735 struct ion_client *ion_client_create(struct ion_device *dev,
738 struct ion_client *client;
739 struct task_struct *task;
741 struct rb_node *parent = NULL;
742 struct ion_client *entry;
746 pr_err("%s: Name cannot be null\n", __func__);
747 return ERR_PTR(-EINVAL);
750 get_task_struct(current->group_leader);
751 task_lock(current->group_leader);
752 pid = task_pid_nr(current->group_leader);
753 /* don't bother to store task struct for kernel threads,
754 they can't be killed anyway */
755 if (current->group_leader->flags & PF_KTHREAD) {
756 put_task_struct(current->group_leader);
759 task = current->group_leader;
761 task_unlock(current->group_leader);
763 client = kzalloc(sizeof(struct ion_client), GFP_KERNEL);
765 goto err_put_task_struct;
768 client->handles = RB_ROOT;
769 idr_init(&client->idr);
770 mutex_init(&client->lock);
773 client->name = kstrdup(name, GFP_KERNEL);
775 goto err_free_client;
777 down_write(&dev->lock);
778 client->display_serial = ion_get_client_serial(&dev->clients, name);
779 client->display_name = kasprintf(
780 GFP_KERNEL, "%s-%d", name, client->display_serial);
781 if (!client->display_name) {
782 up_write(&dev->lock);
783 goto err_free_client_name;
785 p = &dev->clients.rb_node;
788 entry = rb_entry(parent, struct ion_client, node);
792 else if (client > entry)
795 rb_link_node(&client->node, parent, p);
796 rb_insert_color(&client->node, &dev->clients);
798 client->debug_root = debugfs_create_file(client->display_name, 0664,
799 dev->clients_debug_root,
800 client, &debug_client_fops);
801 if (!client->debug_root) {
802 char buf[256], *path;
803 path = dentry_path(dev->clients_debug_root, buf, 256);
804 pr_err("Failed to create client debugfs at %s/%s\n",
805 path, client->display_name);
808 up_write(&dev->lock);
812 err_free_client_name:
818 put_task_struct(current->group_leader);
819 return ERR_PTR(-ENOMEM);
821 EXPORT_SYMBOL(ion_client_create);
823 void ion_client_destroy(struct ion_client *client)
825 struct ion_device *dev = client->dev;
828 pr_debug("%s: %d\n", __func__, __LINE__);
829 while ((n = rb_first(&client->handles))) {
830 struct ion_handle *handle = rb_entry(n, struct ion_handle,
832 ion_handle_destroy(&handle->ref);
835 idr_destroy(&client->idr);
837 down_write(&dev->lock);
839 put_task_struct(client->task);
840 rb_erase(&client->node, &dev->clients);
841 debugfs_remove_recursive(client->debug_root);
842 up_write(&dev->lock);
844 kfree(client->display_name);
848 EXPORT_SYMBOL(ion_client_destroy);
850 struct sg_table *ion_sg_table(struct ion_client *client,
851 struct ion_handle *handle)
853 struct ion_buffer *buffer;
854 struct sg_table *table;
856 mutex_lock(&client->lock);
857 if (!ion_handle_validate(client, handle)) {
858 pr_err("%s: invalid handle passed to map_dma.\n",
860 mutex_unlock(&client->lock);
861 return ERR_PTR(-EINVAL);
863 buffer = handle->buffer;
864 table = buffer->sg_table;
865 mutex_unlock(&client->lock);
868 EXPORT_SYMBOL(ion_sg_table);
870 static void ion_buffer_sync_for_device(struct ion_buffer *buffer,
872 enum dma_data_direction direction);
874 static struct sg_table *ion_map_dma_buf(struct dma_buf_attachment *attachment,
875 enum dma_data_direction direction)
877 struct dma_buf *dmabuf = attachment->dmabuf;
878 struct ion_buffer *buffer = dmabuf->priv;
880 ion_buffer_sync_for_device(buffer, attachment->dev, direction);
881 return buffer->sg_table;
884 static void ion_unmap_dma_buf(struct dma_buf_attachment *attachment,
885 struct sg_table *table,
886 enum dma_data_direction direction)
890 void ion_pages_sync_for_device(struct device *dev, struct page *page,
891 size_t size, enum dma_data_direction dir)
893 struct scatterlist sg;
895 sg_init_table(&sg, 1);
896 sg_set_page(&sg, page, size, 0);
898 * This is not correct - sg_dma_address needs a dma_addr_t that is valid
899 * for the the targeted device, but this works on the currently targeted
902 sg_dma_address(&sg) = page_to_phys(page);
903 dma_sync_sg_for_device(dev, &sg, 1, dir);
906 struct ion_vma_list {
907 struct list_head list;
908 struct vm_area_struct *vma;
911 static void ion_buffer_sync_for_device(struct ion_buffer *buffer,
913 enum dma_data_direction dir)
915 struct ion_vma_list *vma_list;
916 int pages = PAGE_ALIGN(buffer->size) / PAGE_SIZE;
919 pr_debug("%s: syncing for device %s\n", __func__,
920 dev ? dev_name(dev) : "null");
922 if (!ion_buffer_fault_user_mappings(buffer))
925 mutex_lock(&buffer->lock);
926 for (i = 0; i < pages; i++) {
927 struct page *page = buffer->pages[i];
929 if (ion_buffer_page_is_dirty(page))
930 ion_pages_sync_for_device(dev, ion_buffer_page(page),
933 ion_buffer_page_clean(buffer->pages + i);
935 list_for_each_entry(vma_list, &buffer->vmas, list) {
936 struct vm_area_struct *vma = vma_list->vma;
938 zap_page_range(vma, vma->vm_start, vma->vm_end - vma->vm_start,
941 mutex_unlock(&buffer->lock);
944 static int ion_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
946 struct ion_buffer *buffer = vma->vm_private_data;
950 mutex_lock(&buffer->lock);
951 ion_buffer_page_dirty(buffer->pages + vmf->pgoff);
952 BUG_ON(!buffer->pages || !buffer->pages[vmf->pgoff]);
954 pfn = page_to_pfn(ion_buffer_page(buffer->pages[vmf->pgoff]));
955 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
956 mutex_unlock(&buffer->lock);
958 return VM_FAULT_ERROR;
960 return VM_FAULT_NOPAGE;
963 static void ion_vm_open(struct vm_area_struct *vma)
965 struct ion_buffer *buffer = vma->vm_private_data;
966 struct ion_vma_list *vma_list;
968 vma_list = kmalloc(sizeof(struct ion_vma_list), GFP_KERNEL);
972 mutex_lock(&buffer->lock);
973 list_add(&vma_list->list, &buffer->vmas);
974 mutex_unlock(&buffer->lock);
975 pr_debug("%s: adding %p\n", __func__, vma);
978 static void ion_vm_close(struct vm_area_struct *vma)
980 struct ion_buffer *buffer = vma->vm_private_data;
981 struct ion_vma_list *vma_list, *tmp;
983 pr_debug("%s\n", __func__);
984 mutex_lock(&buffer->lock);
985 list_for_each_entry_safe(vma_list, tmp, &buffer->vmas, list) {
986 if (vma_list->vma != vma)
988 list_del(&vma_list->list);
990 pr_debug("%s: deleting %p\n", __func__, vma);
993 mutex_unlock(&buffer->lock);
996 static struct vm_operations_struct ion_vma_ops = {
998 .close = ion_vm_close,
999 .fault = ion_vm_fault,
1002 static int ion_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma)
1004 struct ion_buffer *buffer = dmabuf->priv;
1007 if (!buffer->heap->ops->map_user) {
1008 pr_err("%s: this heap does not define a method for mapping "
1009 "to userspace\n", __func__);
1013 if (ion_buffer_fault_user_mappings(buffer)) {
1014 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND |
1016 vma->vm_private_data = buffer;
1017 vma->vm_ops = &ion_vma_ops;
1022 if (!(buffer->flags & ION_FLAG_CACHED))
1023 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
1025 mutex_lock(&buffer->lock);
1026 /* now map it to userspace */
1027 ret = buffer->heap->ops->map_user(buffer->heap, buffer, vma);
1028 mutex_unlock(&buffer->lock);
1031 pr_err("%s: failure mapping buffer to userspace\n",
1037 static void ion_dma_buf_release(struct dma_buf *dmabuf)
1039 struct ion_buffer *buffer = dmabuf->priv;
1041 ion_buffer_put(buffer);
1044 static void *ion_dma_buf_kmap(struct dma_buf *dmabuf, unsigned long offset)
1046 struct ion_buffer *buffer = dmabuf->priv;
1048 return buffer->vaddr + offset * PAGE_SIZE;
1051 static void ion_dma_buf_kunmap(struct dma_buf *dmabuf, unsigned long offset,
1057 static int ion_dma_buf_begin_cpu_access(struct dma_buf *dmabuf, size_t start,
1059 enum dma_data_direction direction)
1061 struct ion_buffer *buffer = dmabuf->priv;
1064 if (!buffer->heap->ops->map_kernel) {
1065 pr_err("%s: map kernel is not implemented by this heap.\n",
1070 mutex_lock(&buffer->lock);
1071 vaddr = ion_buffer_kmap_get(buffer);
1072 mutex_unlock(&buffer->lock);
1074 return PTR_ERR(vaddr);
1078 static void ion_dma_buf_end_cpu_access(struct dma_buf *dmabuf, size_t start,
1080 enum dma_data_direction direction)
1082 struct ion_buffer *buffer = dmabuf->priv;
1084 mutex_lock(&buffer->lock);
1085 ion_buffer_kmap_put(buffer);
1086 mutex_unlock(&buffer->lock);
1089 static struct dma_buf_ops dma_buf_ops = {
1090 .map_dma_buf = ion_map_dma_buf,
1091 .unmap_dma_buf = ion_unmap_dma_buf,
1093 .release = ion_dma_buf_release,
1094 .begin_cpu_access = ion_dma_buf_begin_cpu_access,
1095 .end_cpu_access = ion_dma_buf_end_cpu_access,
1096 .kmap_atomic = ion_dma_buf_kmap,
1097 .kunmap_atomic = ion_dma_buf_kunmap,
1098 .kmap = ion_dma_buf_kmap,
1099 .kunmap = ion_dma_buf_kunmap,
1102 struct dma_buf *ion_share_dma_buf(struct ion_client *client,
1103 struct ion_handle *handle)
1105 struct ion_buffer *buffer;
1106 struct dma_buf *dmabuf;
1109 mutex_lock(&client->lock);
1110 valid_handle = ion_handle_validate(client, handle);
1111 if (!valid_handle) {
1112 WARN(1, "%s: invalid handle passed to share.\n", __func__);
1113 mutex_unlock(&client->lock);
1114 return ERR_PTR(-EINVAL);
1116 buffer = handle->buffer;
1117 ion_buffer_get(buffer);
1118 mutex_unlock(&client->lock);
1120 dmabuf = dma_buf_export(buffer, &dma_buf_ops, buffer->size, O_RDWR);
1121 if (IS_ERR(dmabuf)) {
1122 ion_buffer_put(buffer);
1128 EXPORT_SYMBOL(ion_share_dma_buf);
1130 int ion_share_dma_buf_fd(struct ion_client *client, struct ion_handle *handle)
1132 struct dma_buf *dmabuf;
1135 dmabuf = ion_share_dma_buf(client, handle);
1137 return PTR_ERR(dmabuf);
1139 fd = dma_buf_fd(dmabuf, O_CLOEXEC);
1141 dma_buf_put(dmabuf);
1145 EXPORT_SYMBOL(ion_share_dma_buf_fd);
1147 struct ion_handle *ion_import_dma_buf(struct ion_client *client, int fd)
1149 struct dma_buf *dmabuf;
1150 struct ion_buffer *buffer;
1151 struct ion_handle *handle;
1154 dmabuf = dma_buf_get(fd);
1156 return ERR_PTR(PTR_ERR(dmabuf));
1157 /* if this memory came from ion */
1159 if (dmabuf->ops != &dma_buf_ops) {
1160 pr_err("%s: can not import dmabuf from another exporter\n",
1162 dma_buf_put(dmabuf);
1163 return ERR_PTR(-EINVAL);
1165 buffer = dmabuf->priv;
1167 mutex_lock(&client->lock);
1168 /* if a handle exists for this buffer just take a reference to it */
1169 handle = ion_handle_lookup(client, buffer);
1170 if (!IS_ERR(handle)) {
1171 ion_handle_get(handle);
1172 mutex_unlock(&client->lock);
1175 mutex_unlock(&client->lock);
1177 handle = ion_handle_create(client, buffer);
1181 mutex_lock(&client->lock);
1182 ret = ion_handle_add(client, handle);
1183 mutex_unlock(&client->lock);
1185 ion_handle_put(handle);
1186 handle = ERR_PTR(ret);
1190 dma_buf_put(dmabuf);
1193 EXPORT_SYMBOL(ion_import_dma_buf);
1195 static int ion_sync_for_device(struct ion_client *client, int fd)
1197 struct dma_buf *dmabuf;
1198 struct ion_buffer *buffer;
1200 dmabuf = dma_buf_get(fd);
1202 return PTR_ERR(dmabuf);
1204 /* if this memory came from ion */
1205 if (dmabuf->ops != &dma_buf_ops) {
1206 pr_err("%s: can not sync dmabuf from another exporter\n",
1208 dma_buf_put(dmabuf);
1211 buffer = dmabuf->priv;
1213 dma_sync_sg_for_device(NULL, buffer->sg_table->sgl,
1214 buffer->sg_table->nents, DMA_BIDIRECTIONAL);
1215 dma_buf_put(dmabuf);
1219 /* fix up the cases where the ioctl direction bits are incorrect */
1220 static unsigned int ion_ioctl_dir(unsigned int cmd)
1225 case ION_IOC_CUSTOM:
1228 return _IOC_DIR(cmd);
1232 static long ion_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1234 struct ion_client *client = filp->private_data;
1235 struct ion_device *dev = client->dev;
1236 struct ion_handle *cleanup_handle = NULL;
1241 struct ion_fd_data fd;
1242 struct ion_allocation_data allocation;
1243 struct ion_handle_data handle;
1244 struct ion_custom_data custom;
1247 dir = ion_ioctl_dir(cmd);
1249 if (_IOC_SIZE(cmd) > sizeof(data))
1252 if (dir & _IOC_WRITE)
1253 if (copy_from_user(&data, (void __user *)arg, _IOC_SIZE(cmd)))
1259 struct ion_handle *handle;
1261 handle = ion_alloc(client, data.allocation.len,
1262 data.allocation.align,
1263 data.allocation.heap_id_mask,
1264 data.allocation.flags);
1266 return PTR_ERR(handle);
1268 data.allocation.handle = handle->id;
1270 cleanup_handle = handle;
1275 struct ion_handle *handle;
1277 handle = ion_handle_get_by_id(client, data.handle.handle);
1279 return PTR_ERR(handle);
1280 ion_free(client, handle);
1281 ion_handle_put(handle);
1287 struct ion_handle *handle;
1289 handle = ion_handle_get_by_id(client, data.handle.handle);
1291 return PTR_ERR(handle);
1292 data.fd.fd = ion_share_dma_buf_fd(client, handle);
1293 ion_handle_put(handle);
1298 case ION_IOC_IMPORT:
1300 struct ion_handle *handle;
1302 handle = ion_import_dma_buf(client, data.fd.fd);
1304 ret = PTR_ERR(handle);
1306 data.handle.handle = handle->id;
1311 ret = ion_sync_for_device(client, data.fd.fd);
1314 case ION_IOC_CUSTOM:
1316 if (!dev->custom_ioctl)
1318 ret = dev->custom_ioctl(client, data.custom.cmd,
1326 if (dir & _IOC_READ) {
1327 if (copy_to_user((void __user *)arg, &data, _IOC_SIZE(cmd))) {
1329 ion_free(client, cleanup_handle);
1336 static int ion_release(struct inode *inode, struct file *file)
1338 struct ion_client *client = file->private_data;
1340 pr_debug("%s: %d\n", __func__, __LINE__);
1341 ion_client_destroy(client);
1345 static int ion_open(struct inode *inode, struct file *file)
1347 struct miscdevice *miscdev = file->private_data;
1348 struct ion_device *dev = container_of(miscdev, struct ion_device, dev);
1349 struct ion_client *client;
1350 char debug_name[64];
1352 pr_debug("%s: %d\n", __func__, __LINE__);
1353 snprintf(debug_name, 64, "%u", task_pid_nr(current->group_leader));
1354 client = ion_client_create(dev, debug_name);
1356 return PTR_ERR(client);
1357 file->private_data = client;
1362 static const struct file_operations ion_fops = {
1363 .owner = THIS_MODULE,
1365 .release = ion_release,
1366 .unlocked_ioctl = ion_ioctl,
1367 .compat_ioctl = compat_ion_ioctl,
1370 static size_t ion_debug_heap_total(struct ion_client *client,
1376 mutex_lock(&client->lock);
1377 for (n = rb_first(&client->handles); n; n = rb_next(n)) {
1378 struct ion_handle *handle = rb_entry(n,
1381 if (handle->buffer->heap->id == id)
1382 size += handle->buffer->size;
1384 mutex_unlock(&client->lock);
1388 static int ion_debug_heap_show(struct seq_file *s, void *unused)
1390 struct ion_heap *heap = s->private;
1391 struct ion_device *dev = heap->dev;
1393 size_t total_size = 0;
1394 size_t total_orphaned_size = 0;
1396 seq_printf(s, "%16.s %16.s %16.s\n", "client", "pid", "size");
1397 seq_printf(s, "----------------------------------------------------\n");
1399 for (n = rb_first(&dev->clients); n; n = rb_next(n)) {
1400 struct ion_client *client = rb_entry(n, struct ion_client,
1402 size_t size = ion_debug_heap_total(client, heap->id);
1407 char task_comm[TASK_COMM_LEN];
1409 get_task_comm(task_comm, client->task);
1410 seq_printf(s, "%16.s %16u %16zu\n", task_comm,
1413 seq_printf(s, "%16.s %16u %16zu\n", client->name,
1417 seq_printf(s, "----------------------------------------------------\n");
1418 seq_printf(s, "orphaned allocations (info is from last known client):"
1420 mutex_lock(&dev->buffer_lock);
1421 for (n = rb_first(&dev->buffers); n; n = rb_next(n)) {
1422 struct ion_buffer *buffer = rb_entry(n, struct ion_buffer,
1424 if (buffer->heap->id != heap->id)
1426 total_size += buffer->size;
1427 if (!buffer->handle_count) {
1428 seq_printf(s, "%16.s %16u %16zu %d %d\n",
1429 buffer->task_comm, buffer->pid,
1430 buffer->size, buffer->kmap_cnt,
1431 atomic_read(&buffer->ref.refcount));
1432 total_orphaned_size += buffer->size;
1435 mutex_unlock(&dev->buffer_lock);
1436 seq_printf(s, "----------------------------------------------------\n");
1437 seq_printf(s, "%16.s %16zu\n", "total orphaned",
1438 total_orphaned_size);
1439 seq_printf(s, "%16.s %16zu\n", "total ", total_size);
1440 if (heap->flags & ION_HEAP_FLAG_DEFER_FREE)
1441 seq_printf(s, "%16.s %16zu\n", "deferred free",
1442 heap->free_list_size);
1443 seq_printf(s, "----------------------------------------------------\n");
1445 if (heap->debug_show)
1446 heap->debug_show(heap, s, unused);
1451 static int ion_debug_heap_open(struct inode *inode, struct file *file)
1453 return single_open(file, ion_debug_heap_show, inode->i_private);
1456 static const struct file_operations debug_heap_fops = {
1457 .open = ion_debug_heap_open,
1459 .llseek = seq_lseek,
1460 .release = single_release,
1463 #ifdef DEBUG_HEAP_SHRINKER
1464 static int debug_shrink_set(void *data, u64 val)
1466 struct ion_heap *heap = data;
1467 struct shrink_control sc;
1476 objs = heap->shrinker.shrink(&heap->shrinker, &sc);
1477 sc.nr_to_scan = objs;
1479 heap->shrinker.shrink(&heap->shrinker, &sc);
1483 static int debug_shrink_get(void *data, u64 *val)
1485 struct ion_heap *heap = data;
1486 struct shrink_control sc;
1492 objs = heap->shrinker.shrink(&heap->shrinker, &sc);
1497 DEFINE_SIMPLE_ATTRIBUTE(debug_shrink_fops, debug_shrink_get,
1498 debug_shrink_set, "%llu\n");
1501 void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap)
1503 struct dentry *debug_file;
1505 if (!heap->ops->allocate || !heap->ops->free || !heap->ops->map_dma ||
1506 !heap->ops->unmap_dma)
1507 pr_err("%s: can not add heap with invalid ops struct.\n",
1510 if (heap->flags & ION_HEAP_FLAG_DEFER_FREE)
1511 ion_heap_init_deferred_free(heap);
1513 if ((heap->flags & ION_HEAP_FLAG_DEFER_FREE) || heap->ops->shrink)
1514 ion_heap_init_shrinker(heap);
1517 down_write(&dev->lock);
1518 /* use negative heap->id to reverse the priority -- when traversing
1519 the list later attempt higher id numbers first */
1520 plist_node_init(&heap->node, -heap->id);
1521 plist_add(&heap->node, &dev->heaps);
1522 debug_file = debugfs_create_file(heap->name, 0664,
1523 dev->heaps_debug_root, heap,
1527 char buf[256], *path;
1529 path = dentry_path(dev->heaps_debug_root, buf, 256);
1530 pr_err("Failed to create heap debugfs at %s/%s\n",
1534 #ifdef DEBUG_HEAP_SHRINKER
1535 if (heap->shrinker.shrink) {
1536 char debug_name[64];
1538 snprintf(debug_name, 64, "%s_shrink", heap->name);
1539 debug_file = debugfs_create_file(
1540 debug_name, 0644, dev->heaps_debug_root, heap,
1541 &debug_shrink_fops);
1543 char buf[256], *path;
1545 path = dentry_path(dev->heaps_debug_root, buf, 256);
1546 pr_err("Failed to create heap shrinker debugfs at %s/%s\n",
1551 up_write(&dev->lock);
1554 struct ion_device *ion_device_create(long (*custom_ioctl)
1555 (struct ion_client *client,
1559 struct ion_device *idev;
1562 idev = kzalloc(sizeof(struct ion_device), GFP_KERNEL);
1564 return ERR_PTR(-ENOMEM);
1566 idev->dev.minor = MISC_DYNAMIC_MINOR;
1567 idev->dev.name = "ion";
1568 idev->dev.fops = &ion_fops;
1569 idev->dev.parent = NULL;
1570 ret = misc_register(&idev->dev);
1572 pr_err("ion: failed to register misc device.\n");
1573 return ERR_PTR(ret);
1576 idev->debug_root = debugfs_create_dir("ion", NULL);
1577 if (!idev->debug_root) {
1578 pr_err("ion: failed to create debugfs root directory.\n");
1581 idev->heaps_debug_root = debugfs_create_dir("heaps", idev->debug_root);
1582 if (!idev->heaps_debug_root) {
1583 pr_err("ion: failed to create debugfs heaps directory.\n");
1586 idev->clients_debug_root = debugfs_create_dir("clients",
1588 if (!idev->clients_debug_root)
1589 pr_err("ion: failed to create debugfs clients directory.\n");
1593 idev->custom_ioctl = custom_ioctl;
1594 idev->buffers = RB_ROOT;
1595 mutex_init(&idev->buffer_lock);
1596 init_rwsem(&idev->lock);
1597 plist_head_init(&idev->heaps);
1598 idev->clients = RB_ROOT;
1602 void ion_device_destroy(struct ion_device *dev)
1604 misc_deregister(&dev->dev);
1605 debugfs_remove_recursive(dev->debug_root);
1606 /* XXX need to free the heaps and clients ? */
1610 void __init ion_reserve(struct ion_platform_data *data)
1614 for (i = 0; i < data->nr; i++) {
1615 if (data->heaps[i].size == 0)
1618 if (data->heaps[i].base == 0) {
1621 paddr = memblock_alloc_base(data->heaps[i].size,
1622 data->heaps[i].align,
1623 MEMBLOCK_ALLOC_ANYWHERE);
1625 pr_err("%s: error allocating memblock for "
1630 data->heaps[i].base = paddr;
1632 int ret = memblock_reserve(data->heaps[i].base,
1633 data->heaps[i].size);
1635 pr_err("memblock reserve of %zx@%lx failed\n",
1636 data->heaps[i].size,
1637 data->heaps[i].base);
1639 pr_info("%s: %s reserved base %lx size %zu\n", __func__,
1640 data->heaps[i].name,
1641 data->heaps[i].base,
1642 data->heaps[i].size);