3 * Android IPC Subsystem
5 * Copyright (C) 2007-2008 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 <asm/cacheflush.h>
19 #include <linux/fdtable.h>
20 #include <linux/file.h>
22 #include <linux/list.h>
23 #include <linux/miscdevice.h>
25 #include <linux/module.h>
26 #include <linux/mutex.h>
27 #include <linux/nsproxy.h>
28 #include <linux/poll.h>
29 #include <linux/proc_fs.h>
30 #include <linux/rbtree.h>
31 #include <linux/sched.h>
32 #include <linux/uaccess.h>
33 #include <linux/vmalloc.h>
36 static DEFINE_MUTEX(binder_lock);
37 static HLIST_HEAD(binder_procs);
38 static struct binder_node *binder_context_mgr_node;
39 static uid_t binder_context_mgr_uid = -1;
40 static int binder_last_id;
41 static struct proc_dir_entry *binder_proc_dir_entry_root;
42 static struct proc_dir_entry *binder_proc_dir_entry_proc;
43 static struct hlist_head binder_dead_nodes;
44 static HLIST_HEAD(binder_deferred_list);
45 static DEFINE_MUTEX(binder_deferred_lock);
47 static int binder_read_proc_proc(char *page, char **start, off_t off,
48 int count, int *eof, void *data);
50 /* This is only defined in include/asm-arm/sizes.h */
56 #define SZ_4M 0x400000
59 #define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
61 #define BINDER_SMALL_BUF_SIZE (PAGE_SIZE * 64)
64 BINDER_DEBUG_USER_ERROR = 1U << 0,
65 BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1,
66 BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2,
67 BINDER_DEBUG_OPEN_CLOSE = 1U << 3,
68 BINDER_DEBUG_DEAD_BINDER = 1U << 4,
69 BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5,
70 BINDER_DEBUG_READ_WRITE = 1U << 6,
71 BINDER_DEBUG_USER_REFS = 1U << 7,
72 BINDER_DEBUG_THREADS = 1U << 8,
73 BINDER_DEBUG_TRANSACTION = 1U << 9,
74 BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10,
75 BINDER_DEBUG_FREE_BUFFER = 1U << 11,
76 BINDER_DEBUG_INTERNAL_REFS = 1U << 12,
77 BINDER_DEBUG_BUFFER_ALLOC = 1U << 13,
78 BINDER_DEBUG_PRIORITY_CAP = 1U << 14,
79 BINDER_DEBUG_BUFFER_ALLOC_ASYNC = 1U << 15,
81 static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR |
82 BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION;
83 module_param_named(debug_mask, binder_debug_mask, uint, S_IWUSR | S_IRUGO);
85 static int binder_debug_no_lock;
86 module_param_named(proc_no_lock, binder_debug_no_lock, bool, S_IWUSR | S_IRUGO);
88 static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
89 static int binder_stop_on_user_error;
91 static int binder_set_stop_on_user_error(const char *val,
92 struct kernel_param *kp)
95 ret = param_set_int(val, kp);
96 if (binder_stop_on_user_error < 2)
97 wake_up(&binder_user_error_wait);
100 module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
101 param_get_int, &binder_stop_on_user_error, S_IWUSR | S_IRUGO);
103 #define binder_user_error(x...) \
105 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
106 printk(KERN_INFO x); \
107 if (binder_stop_on_user_error) \
108 binder_stop_on_user_error = 2; \
117 BINDER_STAT_TRANSACTION,
118 BINDER_STAT_TRANSACTION_COMPLETE,
122 struct binder_stats {
123 int br[_IOC_NR(BR_FAILED_REPLY) + 1];
124 int bc[_IOC_NR(BC_DEAD_BINDER_DONE) + 1];
125 int obj_created[BINDER_STAT_COUNT];
126 int obj_deleted[BINDER_STAT_COUNT];
129 static struct binder_stats binder_stats;
131 struct binder_transaction_log_entry {
143 struct binder_transaction_log {
146 struct binder_transaction_log_entry entry[32];
148 struct binder_transaction_log binder_transaction_log;
149 struct binder_transaction_log binder_transaction_log_failed;
151 static struct binder_transaction_log_entry *binder_transaction_log_add(
152 struct binder_transaction_log *log)
154 struct binder_transaction_log_entry *e;
155 e = &log->entry[log->next];
156 memset(e, 0, sizeof(*e));
158 if (log->next == ARRAY_SIZE(log->entry)) {
166 struct list_head entry;
168 BINDER_WORK_TRANSACTION = 1,
169 BINDER_WORK_TRANSACTION_COMPLETE,
171 BINDER_WORK_DEAD_BINDER,
172 BINDER_WORK_DEAD_BINDER_AND_CLEAR,
173 BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
179 struct binder_work work;
181 struct rb_node rb_node;
182 struct hlist_node dead_node;
184 struct binder_proc *proc;
185 struct hlist_head refs;
186 int internal_strong_refs;
188 int local_strong_refs;
191 unsigned has_strong_ref:1;
192 unsigned pending_strong_ref:1;
193 unsigned has_weak_ref:1;
194 unsigned pending_weak_ref:1;
195 unsigned has_async_transaction:1;
196 unsigned accept_fds:1;
197 unsigned min_priority:8;
198 struct list_head async_todo;
201 struct binder_ref_death {
202 struct binder_work work;
207 /* Lookups needed: */
208 /* node + proc => ref (transaction) */
209 /* desc + proc => ref (transaction, inc/dec ref) */
210 /* node => refs + procs (proc exit) */
212 struct rb_node rb_node_desc;
213 struct rb_node rb_node_node;
214 struct hlist_node node_entry;
215 struct binder_proc *proc;
216 struct binder_node *node;
220 struct binder_ref_death *death;
223 struct binder_buffer {
224 struct list_head entry; /* free and allocated entries by addesss */
225 struct rb_node rb_node; /* free entry by size or allocated entry */
228 unsigned allow_user_free:1;
229 unsigned async_transaction:1;
230 unsigned debug_id:29;
232 struct binder_transaction *transaction;
234 struct binder_node *target_node;
241 BINDER_DEFERRED_PUT_FILES = 0x01,
242 BINDER_DEFERRED_FLUSH = 0x02,
243 BINDER_DEFERRED_RELEASE = 0x04,
247 struct hlist_node proc_node;
248 struct rb_root threads;
249 struct rb_root nodes;
250 struct rb_root refs_by_desc;
251 struct rb_root refs_by_node;
253 struct vm_area_struct *vma;
254 struct task_struct *tsk;
255 struct files_struct *files;
256 struct hlist_node deferred_work_node;
259 ptrdiff_t user_buffer_offset;
261 struct list_head buffers;
262 struct rb_root free_buffers;
263 struct rb_root allocated_buffers;
264 size_t free_async_space;
268 uint32_t buffer_free;
269 struct list_head todo;
270 wait_queue_head_t wait;
271 struct binder_stats stats;
272 struct list_head delivered_death;
274 int requested_threads;
275 int requested_threads_started;
277 long default_priority;
281 BINDER_LOOPER_STATE_REGISTERED = 0x01,
282 BINDER_LOOPER_STATE_ENTERED = 0x02,
283 BINDER_LOOPER_STATE_EXITED = 0x04,
284 BINDER_LOOPER_STATE_INVALID = 0x08,
285 BINDER_LOOPER_STATE_WAITING = 0x10,
286 BINDER_LOOPER_STATE_NEED_RETURN = 0x20
289 struct binder_thread {
290 struct binder_proc *proc;
291 struct rb_node rb_node;
294 struct binder_transaction *transaction_stack;
295 struct list_head todo;
296 uint32_t return_error; /* Write failed, return error code in read buf */
297 uint32_t return_error2; /* Write failed, return error code in read */
298 /* buffer. Used when sending a reply to a dead process that */
299 /* we are also waiting on */
300 wait_queue_head_t wait;
301 struct binder_stats stats;
304 struct binder_transaction {
306 struct binder_work work;
307 struct binder_thread *from;
308 struct binder_transaction *from_parent;
309 struct binder_proc *to_proc;
310 struct binder_thread *to_thread;
311 struct binder_transaction *to_parent;
312 unsigned need_reply:1;
313 /* unsigned is_dead:1; */ /* not used at the moment */
315 struct binder_buffer *buffer;
323 static void binder_defer_work(struct binder_proc *proc, int defer);
326 * copied from get_unused_fd_flags
328 int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
330 struct files_struct *files = proc->files;
333 unsigned long rlim_cur;
340 spin_lock(&files->file_lock);
343 fdt = files_fdtable(files);
344 fd = find_next_zero_bit(fdt->open_fds->fds_bits, fdt->max_fds,
348 * N.B. For clone tasks sharing a files structure, this test
349 * will limit the total number of files that can be opened.
352 if (lock_task_sighand(proc->tsk, &irqs)) {
353 rlim_cur = proc->tsk->signal->rlim[RLIMIT_NOFILE].rlim_cur;
354 unlock_task_sighand(proc->tsk, &irqs);
359 /* Do we need to expand the fd array or fd set? */
360 error = expand_files(files, fd);
366 * If we needed to expand the fs array we
367 * might have blocked - try again.
373 FD_SET(fd, fdt->open_fds);
374 if (flags & O_CLOEXEC)
375 FD_SET(fd, fdt->close_on_exec);
377 FD_CLR(fd, fdt->close_on_exec);
378 files->next_fd = fd + 1;
381 if (fdt->fd[fd] != NULL) {
382 printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
389 spin_unlock(&files->file_lock);
394 * copied from fd_install
396 static void task_fd_install(
397 struct binder_proc *proc, unsigned int fd, struct file *file)
399 struct files_struct *files = proc->files;
405 spin_lock(&files->file_lock);
406 fdt = files_fdtable(files);
407 BUG_ON(fdt->fd[fd] != NULL);
408 rcu_assign_pointer(fdt->fd[fd], file);
409 spin_unlock(&files->file_lock);
413 * copied from __put_unused_fd in open.c
415 static void __put_unused_fd(struct files_struct *files, unsigned int fd)
417 struct fdtable *fdt = files_fdtable(files);
418 __FD_CLR(fd, fdt->open_fds);
419 if (fd < files->next_fd)
424 * copied from sys_close
426 static long task_close_fd(struct binder_proc *proc, unsigned int fd)
429 struct files_struct *files = proc->files;
436 spin_lock(&files->file_lock);
437 fdt = files_fdtable(files);
438 if (fd >= fdt->max_fds)
443 rcu_assign_pointer(fdt->fd[fd], NULL);
444 FD_CLR(fd, fdt->close_on_exec);
445 __put_unused_fd(files, fd);
446 spin_unlock(&files->file_lock);
447 retval = filp_close(filp, files);
449 /* can't restart close syscall because file table entry was cleared */
450 if (unlikely(retval == -ERESTARTSYS ||
451 retval == -ERESTARTNOINTR ||
452 retval == -ERESTARTNOHAND ||
453 retval == -ERESTART_RESTARTBLOCK))
459 spin_unlock(&files->file_lock);
463 static void binder_set_nice(long nice)
466 if (can_nice(current, nice)) {
467 set_user_nice(current, nice);
470 min_nice = 20 - current->signal->rlim[RLIMIT_NICE].rlim_cur;
471 if (binder_debug_mask & BINDER_DEBUG_PRIORITY_CAP)
472 printk(KERN_INFO "binder: %d: nice value %ld not allowed use "
473 "%ld instead\n", current->pid, nice, min_nice);
474 set_user_nice(current, min_nice);
477 binder_user_error("binder: %d RLIMIT_NICE not set\n", current->pid);
480 static size_t binder_buffer_size(struct binder_proc *proc,
481 struct binder_buffer *buffer)
483 if (list_is_last(&buffer->entry, &proc->buffers))
484 return proc->buffer + proc->buffer_size - (void *)buffer->data;
486 return (size_t)list_entry(buffer->entry.next,
487 struct binder_buffer, entry) - (size_t)buffer->data;
490 static void binder_insert_free_buffer(struct binder_proc *proc,
491 struct binder_buffer *new_buffer)
493 struct rb_node **p = &proc->free_buffers.rb_node;
494 struct rb_node *parent = NULL;
495 struct binder_buffer *buffer;
497 size_t new_buffer_size;
499 BUG_ON(!new_buffer->free);
501 new_buffer_size = binder_buffer_size(proc, new_buffer);
503 if (binder_debug_mask & BINDER_DEBUG_BUFFER_ALLOC)
504 printk(KERN_INFO "binder: %d: add free buffer, size %zd, "
505 "at %p\n", proc->pid, new_buffer_size, new_buffer);
509 buffer = rb_entry(parent, struct binder_buffer, rb_node);
510 BUG_ON(!buffer->free);
512 buffer_size = binder_buffer_size(proc, buffer);
514 if (new_buffer_size < buffer_size)
515 p = &parent->rb_left;
517 p = &parent->rb_right;
519 rb_link_node(&new_buffer->rb_node, parent, p);
520 rb_insert_color(&new_buffer->rb_node, &proc->free_buffers);
523 static void binder_insert_allocated_buffer(struct binder_proc *proc,
524 struct binder_buffer *new_buffer)
526 struct rb_node **p = &proc->allocated_buffers.rb_node;
527 struct rb_node *parent = NULL;
528 struct binder_buffer *buffer;
530 BUG_ON(new_buffer->free);
534 buffer = rb_entry(parent, struct binder_buffer, rb_node);
535 BUG_ON(buffer->free);
537 if (new_buffer < buffer)
538 p = &parent->rb_left;
539 else if (new_buffer > buffer)
540 p = &parent->rb_right;
544 rb_link_node(&new_buffer->rb_node, parent, p);
545 rb_insert_color(&new_buffer->rb_node, &proc->allocated_buffers);
548 static struct binder_buffer *binder_buffer_lookup(struct binder_proc *proc,
549 void __user *user_ptr)
551 struct rb_node *n = proc->allocated_buffers.rb_node;
552 struct binder_buffer *buffer;
553 struct binder_buffer *kern_ptr;
555 kern_ptr = user_ptr - proc->user_buffer_offset
556 - offsetof(struct binder_buffer, data);
559 buffer = rb_entry(n, struct binder_buffer, rb_node);
560 BUG_ON(buffer->free);
562 if (kern_ptr < buffer)
564 else if (kern_ptr > buffer)
572 static int binder_update_page_range(struct binder_proc *proc, int allocate,
573 void *start, void *end,
574 struct vm_area_struct *vma)
577 unsigned long user_page_addr;
578 struct vm_struct tmp_area;
580 struct mm_struct *mm;
582 if (binder_debug_mask & BINDER_DEBUG_BUFFER_ALLOC)
583 printk(KERN_INFO "binder: %d: %s pages %p-%p\n",
584 proc->pid, allocate ? "allocate" : "free", start, end);
592 mm = get_task_mm(proc->tsk);
595 down_write(&mm->mmap_sem);
603 printk(KERN_ERR "binder: %d: binder_alloc_buf failed to "
604 "map pages in userspace, no vma\n", proc->pid);
608 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
610 struct page **page_array_ptr;
611 page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
614 *page = alloc_page(GFP_KERNEL | __GFP_ZERO);
616 printk(KERN_ERR "binder: %d: binder_alloc_buf failed "
617 "for page at %p\n", proc->pid, page_addr);
618 goto err_alloc_page_failed;
620 tmp_area.addr = page_addr;
621 tmp_area.size = PAGE_SIZE + PAGE_SIZE /* guard page? */;
622 page_array_ptr = page;
623 ret = map_vm_area(&tmp_area, PAGE_KERNEL, &page_array_ptr);
625 printk(KERN_ERR "binder: %d: binder_alloc_buf failed "
626 "to map page at %p in kernel\n",
627 proc->pid, page_addr);
628 goto err_map_kernel_failed;
631 (uintptr_t)page_addr + proc->user_buffer_offset;
632 ret = vm_insert_page(vma, user_page_addr, page[0]);
634 printk(KERN_ERR "binder: %d: binder_alloc_buf failed "
635 "to map page at %lx in userspace\n",
636 proc->pid, user_page_addr);
637 goto err_vm_insert_page_failed;
639 /* vm_insert_page does not seem to increment the refcount */
642 up_write(&mm->mmap_sem);
648 for (page_addr = end - PAGE_SIZE; page_addr >= start;
649 page_addr -= PAGE_SIZE) {
650 page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
652 zap_page_range(vma, (uintptr_t)page_addr +
653 proc->user_buffer_offset, PAGE_SIZE, NULL);
654 err_vm_insert_page_failed:
655 unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
656 err_map_kernel_failed:
659 err_alloc_page_failed:
664 up_write(&mm->mmap_sem);
670 static struct binder_buffer *binder_alloc_buf(struct binder_proc *proc,
672 size_t offsets_size, int is_async)
674 struct rb_node *n = proc->free_buffers.rb_node;
675 struct binder_buffer *buffer;
677 struct rb_node *best_fit = NULL;
682 if (proc->vma == NULL) {
683 printk(KERN_ERR "binder: %d: binder_alloc_buf, no vma\n",
688 size = ALIGN(data_size, sizeof(void *)) +
689 ALIGN(offsets_size, sizeof(void *));
691 if (size < data_size || size < offsets_size) {
692 binder_user_error("binder: %d: got transaction with invalid "
693 "size %zd-%zd\n", proc->pid, data_size, offsets_size);
698 proc->free_async_space < size + sizeof(struct binder_buffer)) {
699 if (binder_debug_mask & BINDER_DEBUG_BUFFER_ALLOC)
701 "binder: %d: binder_alloc_buf size %zd failed, "
702 "no async space left\n", proc->pid, size);
707 buffer = rb_entry(n, struct binder_buffer, rb_node);
708 BUG_ON(!buffer->free);
709 buffer_size = binder_buffer_size(proc, buffer);
711 if (size < buffer_size) {
714 } else if (size > buffer_size)
721 if (best_fit == NULL) {
722 printk(KERN_ERR "binder: %d: binder_alloc_buf size %zd failed, "
723 "no address space\n", proc->pid, size);
727 buffer = rb_entry(best_fit, struct binder_buffer, rb_node);
728 buffer_size = binder_buffer_size(proc, buffer);
730 if (binder_debug_mask & BINDER_DEBUG_BUFFER_ALLOC)
731 printk(KERN_INFO "binder: %d: binder_alloc_buf size %zd got buff"
732 "er %p size %zd\n", proc->pid, size, buffer, buffer_size);
735 (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK);
737 if (size + sizeof(struct binder_buffer) + 4 >= buffer_size)
738 buffer_size = size; /* no room for other buffers */
740 buffer_size = size + sizeof(struct binder_buffer);
743 (void *)PAGE_ALIGN((uintptr_t)buffer->data + buffer_size);
744 if (end_page_addr > has_page_addr)
745 end_page_addr = has_page_addr;
746 if (binder_update_page_range(proc, 1,
747 (void *)PAGE_ALIGN((uintptr_t)buffer->data), end_page_addr, NULL))
750 rb_erase(best_fit, &proc->free_buffers);
752 binder_insert_allocated_buffer(proc, buffer);
753 if (buffer_size != size) {
754 struct binder_buffer *new_buffer = (void *)buffer->data + size;
755 list_add(&new_buffer->entry, &buffer->entry);
756 new_buffer->free = 1;
757 binder_insert_free_buffer(proc, new_buffer);
759 if (binder_debug_mask & BINDER_DEBUG_BUFFER_ALLOC)
760 printk(KERN_INFO "binder: %d: binder_alloc_buf size %zd got "
761 "%p\n", proc->pid, size, buffer);
762 buffer->data_size = data_size;
763 buffer->offsets_size = offsets_size;
764 buffer->async_transaction = is_async;
766 proc->free_async_space -= size + sizeof(struct binder_buffer);
767 if (binder_debug_mask & BINDER_DEBUG_BUFFER_ALLOC_ASYNC)
768 printk(KERN_INFO "binder: %d: binder_alloc_buf size %zd "
769 "async free %zd\n", proc->pid, size,
770 proc->free_async_space);
776 static void *buffer_start_page(struct binder_buffer *buffer)
778 return (void *)((uintptr_t)buffer & PAGE_MASK);
781 static void *buffer_end_page(struct binder_buffer *buffer)
783 return (void *)(((uintptr_t)(buffer + 1) - 1) & PAGE_MASK);
786 static void binder_delete_free_buffer(struct binder_proc *proc,
787 struct binder_buffer *buffer)
789 struct binder_buffer *prev, *next = NULL;
790 int free_page_end = 1;
791 int free_page_start = 1;
793 BUG_ON(proc->buffers.next == &buffer->entry);
794 prev = list_entry(buffer->entry.prev, struct binder_buffer, entry);
796 if (buffer_end_page(prev) == buffer_start_page(buffer)) {
798 if (buffer_end_page(prev) == buffer_end_page(buffer))
800 if (binder_debug_mask & BINDER_DEBUG_BUFFER_ALLOC)
801 printk(KERN_INFO "binder: %d: merge free, buffer %p "
802 "share page with %p\n", proc->pid, buffer, prev);
805 if (!list_is_last(&buffer->entry, &proc->buffers)) {
806 next = list_entry(buffer->entry.next,
807 struct binder_buffer, entry);
808 if (buffer_start_page(next) == buffer_end_page(buffer)) {
810 if (buffer_start_page(next) ==
811 buffer_start_page(buffer))
813 if (binder_debug_mask & BINDER_DEBUG_BUFFER_ALLOC)
814 printk(KERN_INFO "binder: %d: merge free, "
815 "buffer %p share page with %p\n",
816 proc->pid, buffer, prev);
819 list_del(&buffer->entry);
820 if (free_page_start || free_page_end) {
821 if (binder_debug_mask & BINDER_DEBUG_BUFFER_ALLOC)
822 printk(KERN_INFO "binder: %d: merge free, buffer %p do "
823 "not share page%s%s with with %p or %p\n",
824 proc->pid, buffer, free_page_start ? "" : " end",
825 free_page_end ? "" : " start", prev, next);
826 binder_update_page_range(proc, 0, free_page_start ?
827 buffer_start_page(buffer) : buffer_end_page(buffer),
828 (free_page_end ? buffer_end_page(buffer) :
829 buffer_start_page(buffer)) + PAGE_SIZE, NULL);
833 static void binder_free_buf(struct binder_proc *proc,
834 struct binder_buffer *buffer)
836 size_t size, buffer_size;
838 buffer_size = binder_buffer_size(proc, buffer);
840 size = ALIGN(buffer->data_size, sizeof(void *)) +
841 ALIGN(buffer->offsets_size, sizeof(void *));
842 if (binder_debug_mask & BINDER_DEBUG_BUFFER_ALLOC)
843 printk(KERN_INFO "binder: %d: binder_free_buf %p size %zd buffer"
844 "_size %zd\n", proc->pid, buffer, size, buffer_size);
846 BUG_ON(buffer->free);
847 BUG_ON(size > buffer_size);
848 BUG_ON(buffer->transaction != NULL);
849 BUG_ON((void *)buffer < proc->buffer);
850 BUG_ON((void *)buffer > proc->buffer + proc->buffer_size);
852 if (buffer->async_transaction) {
853 proc->free_async_space += size + sizeof(struct binder_buffer);
854 if (binder_debug_mask & BINDER_DEBUG_BUFFER_ALLOC_ASYNC)
855 printk(KERN_INFO "binder: %d: binder_free_buf size %zd "
856 "async free %zd\n", proc->pid, size,
857 proc->free_async_space);
860 binder_update_page_range(proc, 0,
861 (void *)PAGE_ALIGN((uintptr_t)buffer->data),
862 (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK),
864 rb_erase(&buffer->rb_node, &proc->allocated_buffers);
866 if (!list_is_last(&buffer->entry, &proc->buffers)) {
867 struct binder_buffer *next = list_entry(buffer->entry.next,
868 struct binder_buffer, entry);
870 rb_erase(&next->rb_node, &proc->free_buffers);
871 binder_delete_free_buffer(proc, next);
874 if (proc->buffers.next != &buffer->entry) {
875 struct binder_buffer *prev = list_entry(buffer->entry.prev,
876 struct binder_buffer, entry);
878 binder_delete_free_buffer(proc, buffer);
879 rb_erase(&prev->rb_node, &proc->free_buffers);
883 binder_insert_free_buffer(proc, buffer);
886 static struct binder_node *binder_get_node(struct binder_proc *proc,
889 struct rb_node *n = proc->nodes.rb_node;
890 struct binder_node *node;
893 node = rb_entry(n, struct binder_node, rb_node);
897 else if (ptr > node->ptr)
905 static struct binder_node *binder_new_node(struct binder_proc *proc,
909 struct rb_node **p = &proc->nodes.rb_node;
910 struct rb_node *parent = NULL;
911 struct binder_node *node;
915 node = rb_entry(parent, struct binder_node, rb_node);
919 else if (ptr > node->ptr)
925 node = kzalloc(sizeof(*node), GFP_KERNEL);
928 binder_stats.obj_created[BINDER_STAT_NODE]++;
929 rb_link_node(&node->rb_node, parent, p);
930 rb_insert_color(&node->rb_node, &proc->nodes);
931 node->debug_id = ++binder_last_id;
934 node->cookie = cookie;
935 node->work.type = BINDER_WORK_NODE;
936 INIT_LIST_HEAD(&node->work.entry);
937 INIT_LIST_HEAD(&node->async_todo);
938 if (binder_debug_mask & BINDER_DEBUG_INTERNAL_REFS)
939 printk(KERN_INFO "binder: %d:%d node %d u%p c%p created\n",
940 proc->pid, current->pid, node->debug_id,
941 node->ptr, node->cookie);
945 static int binder_inc_node(struct binder_node *node, int strong, int internal,
946 struct list_head *target_list)
950 if (target_list == NULL &&
951 node->internal_strong_refs == 0 &&
952 !(node == binder_context_mgr_node &&
953 node->has_strong_ref)) {
954 printk(KERN_ERR "binder: invalid inc strong "
955 "node for %d\n", node->debug_id);
958 node->internal_strong_refs++;
960 node->local_strong_refs++;
961 if (!node->has_strong_ref && target_list) {
962 list_del_init(&node->work.entry);
963 list_add_tail(&node->work.entry, target_list);
967 node->local_weak_refs++;
968 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
969 if (target_list == NULL) {
970 printk(KERN_ERR "binder: invalid inc weak node "
971 "for %d\n", node->debug_id);
974 list_add_tail(&node->work.entry, target_list);
980 static int binder_dec_node(struct binder_node *node, int strong, int internal)
984 node->internal_strong_refs--;
986 node->local_strong_refs--;
987 if (node->local_strong_refs || node->internal_strong_refs)
991 node->local_weak_refs--;
992 if (node->local_weak_refs || !hlist_empty(&node->refs))
995 if (node->proc && (node->has_strong_ref || node->has_weak_ref)) {
996 if (list_empty(&node->work.entry)) {
997 list_add_tail(&node->work.entry, &node->proc->todo);
998 wake_up_interruptible(&node->proc->wait);
1001 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
1002 !node->local_weak_refs) {
1003 list_del_init(&node->work.entry);
1005 rb_erase(&node->rb_node, &node->proc->nodes);
1006 if (binder_debug_mask & BINDER_DEBUG_INTERNAL_REFS)
1007 printk(KERN_INFO "binder: refless node %d deleted\n", node->debug_id);
1009 hlist_del(&node->dead_node);
1010 if (binder_debug_mask & BINDER_DEBUG_INTERNAL_REFS)
1011 printk(KERN_INFO "binder: dead node %d deleted\n", node->debug_id);
1014 binder_stats.obj_deleted[BINDER_STAT_NODE]++;
1022 static struct binder_ref *binder_get_ref(struct binder_proc *proc,
1025 struct rb_node *n = proc->refs_by_desc.rb_node;
1026 struct binder_ref *ref;
1029 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1031 if (desc < ref->desc)
1033 else if (desc > ref->desc)
1041 static struct binder_ref *binder_get_ref_for_node(struct binder_proc *proc,
1042 struct binder_node *node)
1045 struct rb_node **p = &proc->refs_by_node.rb_node;
1046 struct rb_node *parent = NULL;
1047 struct binder_ref *ref, *new_ref;
1051 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1053 if (node < ref->node)
1055 else if (node > ref->node)
1056 p = &(*p)->rb_right;
1060 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1061 if (new_ref == NULL)
1063 binder_stats.obj_created[BINDER_STAT_REF]++;
1064 new_ref->debug_id = ++binder_last_id;
1065 new_ref->proc = proc;
1066 new_ref->node = node;
1067 rb_link_node(&new_ref->rb_node_node, parent, p);
1068 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1070 new_ref->desc = (node == binder_context_mgr_node) ? 0 : 1;
1071 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1072 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1073 if (ref->desc > new_ref->desc)
1075 new_ref->desc = ref->desc + 1;
1078 p = &proc->refs_by_desc.rb_node;
1081 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1083 if (new_ref->desc < ref->desc)
1085 else if (new_ref->desc > ref->desc)
1086 p = &(*p)->rb_right;
1090 rb_link_node(&new_ref->rb_node_desc, parent, p);
1091 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
1093 hlist_add_head(&new_ref->node_entry, &node->refs);
1094 if (binder_debug_mask & BINDER_DEBUG_INTERNAL_REFS)
1095 printk(KERN_INFO "binder: %d new ref %d desc %d for "
1096 "node %d\n", proc->pid, new_ref->debug_id,
1097 new_ref->desc, node->debug_id);
1099 if (binder_debug_mask & BINDER_DEBUG_INTERNAL_REFS)
1100 printk(KERN_INFO "binder: %d new ref %d desc %d for "
1101 "dead node\n", proc->pid, new_ref->debug_id,
1107 static void binder_delete_ref(struct binder_ref *ref)
1109 if (binder_debug_mask & BINDER_DEBUG_INTERNAL_REFS)
1110 printk(KERN_INFO "binder: %d delete ref %d desc %d for "
1111 "node %d\n", ref->proc->pid, ref->debug_id,
1112 ref->desc, ref->node->debug_id);
1113 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1114 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
1116 binder_dec_node(ref->node, 1, 1);
1117 hlist_del(&ref->node_entry);
1118 binder_dec_node(ref->node, 0, 1);
1120 if (binder_debug_mask & BINDER_DEBUG_DEAD_BINDER)
1121 printk(KERN_INFO "binder: %d delete ref %d desc %d "
1122 "has death notification\n", ref->proc->pid,
1123 ref->debug_id, ref->desc);
1124 list_del(&ref->death->work.entry);
1126 binder_stats.obj_deleted[BINDER_STAT_DEATH]++;
1129 binder_stats.obj_deleted[BINDER_STAT_REF]++;
1132 static int binder_inc_ref(struct binder_ref *ref, int strong,
1133 struct list_head *target_list)
1137 if (ref->strong == 0) {
1138 ret = binder_inc_node(ref->node, 1, 1, target_list);
1144 if (ref->weak == 0) {
1145 ret = binder_inc_node(ref->node, 0, 1, target_list);
1155 static int binder_dec_ref(struct binder_ref *ref, int strong)
1158 if (ref->strong == 0) {
1159 binder_user_error("binder: %d invalid dec strong, "
1160 "ref %d desc %d s %d w %d\n",
1161 ref->proc->pid, ref->debug_id,
1162 ref->desc, ref->strong, ref->weak);
1166 if (ref->strong == 0) {
1168 ret = binder_dec_node(ref->node, strong, 1);
1173 if (ref->weak == 0) {
1174 binder_user_error("binder: %d invalid dec weak, "
1175 "ref %d desc %d s %d w %d\n",
1176 ref->proc->pid, ref->debug_id,
1177 ref->desc, ref->strong, ref->weak);
1182 if (ref->strong == 0 && ref->weak == 0)
1183 binder_delete_ref(ref);
1187 static void binder_pop_transaction(struct binder_thread *target_thread,
1188 struct binder_transaction *t)
1190 if (target_thread) {
1191 BUG_ON(target_thread->transaction_stack != t);
1192 BUG_ON(target_thread->transaction_stack->from != target_thread);
1193 target_thread->transaction_stack =
1194 target_thread->transaction_stack->from_parent;
1199 t->buffer->transaction = NULL;
1201 binder_stats.obj_deleted[BINDER_STAT_TRANSACTION]++;
1204 static void binder_send_failed_reply(struct binder_transaction *t,
1205 uint32_t error_code)
1207 struct binder_thread *target_thread;
1208 BUG_ON(t->flags & TF_ONE_WAY);
1210 target_thread = t->from;
1211 if (target_thread) {
1212 if (target_thread->return_error != BR_OK &&
1213 target_thread->return_error2 == BR_OK) {
1214 target_thread->return_error2 =
1215 target_thread->return_error;
1216 target_thread->return_error = BR_OK;
1218 if (target_thread->return_error == BR_OK) {
1219 if (binder_debug_mask & BINDER_DEBUG_FAILED_TRANSACTION)
1220 printk(KERN_INFO "binder: send failed reply for transaction %d to %d:%d\n",
1221 t->debug_id, target_thread->proc->pid, target_thread->pid);
1223 binder_pop_transaction(target_thread, t);
1224 target_thread->return_error = error_code;
1225 wake_up_interruptible(&target_thread->wait);
1227 printk(KERN_ERR "binder: reply failed, target "
1228 "thread, %d:%d, has error code %d "
1229 "already\n", target_thread->proc->pid,
1231 target_thread->return_error);
1235 struct binder_transaction *next = t->from_parent;
1237 if (binder_debug_mask & BINDER_DEBUG_FAILED_TRANSACTION)
1238 printk(KERN_INFO "binder: send failed reply "
1239 "for transaction %d, target dead\n",
1242 binder_pop_transaction(target_thread, t);
1244 if (binder_debug_mask & BINDER_DEBUG_DEAD_BINDER)
1245 printk(KERN_INFO "binder: reply failed,"
1246 " no target thread at root\n");
1250 if (binder_debug_mask & BINDER_DEBUG_DEAD_BINDER)
1251 printk(KERN_INFO "binder: reply failed, no targ"
1252 "et thread -- retry %d\n", t->debug_id);
1257 static void binder_transaction_buffer_release(struct binder_proc *proc,
1258 struct binder_buffer *buffer,
1261 static void binder_transaction(struct binder_proc *proc,
1262 struct binder_thread *thread,
1263 struct binder_transaction_data *tr, int reply)
1265 struct binder_transaction *t;
1266 struct binder_work *tcomplete;
1267 size_t *offp, *off_end;
1268 struct binder_proc *target_proc;
1269 struct binder_thread *target_thread = NULL;
1270 struct binder_node *target_node = NULL;
1271 struct list_head *target_list;
1272 wait_queue_head_t *target_wait;
1273 struct binder_transaction *in_reply_to = NULL;
1274 struct binder_transaction_log_entry *e;
1275 uint32_t return_error;
1277 e = binder_transaction_log_add(&binder_transaction_log);
1278 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
1279 e->from_proc = proc->pid;
1280 e->from_thread = thread->pid;
1281 e->target_handle = tr->target.handle;
1282 e->data_size = tr->data_size;
1283 e->offsets_size = tr->offsets_size;
1286 in_reply_to = thread->transaction_stack;
1287 if (in_reply_to == NULL) {
1288 binder_user_error("binder: %d:%d got reply transaction "
1289 "with no transaction stack\n",
1290 proc->pid, thread->pid);
1291 return_error = BR_FAILED_REPLY;
1292 goto err_empty_call_stack;
1294 binder_set_nice(in_reply_to->saved_priority);
1295 if (in_reply_to->to_thread != thread) {
1296 binder_user_error("binder: %d:%d got reply transaction "
1297 "with bad transaction stack,"
1298 " transaction %d has target %d:%d\n",
1299 proc->pid, thread->pid, in_reply_to->debug_id,
1300 in_reply_to->to_proc ?
1301 in_reply_to->to_proc->pid : 0,
1302 in_reply_to->to_thread ?
1303 in_reply_to->to_thread->pid : 0);
1304 return_error = BR_FAILED_REPLY;
1306 goto err_bad_call_stack;
1308 thread->transaction_stack = in_reply_to->to_parent;
1309 target_thread = in_reply_to->from;
1310 if (target_thread == NULL) {
1311 return_error = BR_DEAD_REPLY;
1312 goto err_dead_binder;
1314 if (target_thread->transaction_stack != in_reply_to) {
1315 binder_user_error("binder: %d:%d got reply transaction "
1316 "with bad target transaction stack %d, "
1318 proc->pid, thread->pid,
1319 target_thread->transaction_stack ?
1320 target_thread->transaction_stack->debug_id : 0,
1321 in_reply_to->debug_id);
1322 return_error = BR_FAILED_REPLY;
1324 target_thread = NULL;
1325 goto err_dead_binder;
1327 target_proc = target_thread->proc;
1329 if (tr->target.handle) {
1330 struct binder_ref *ref;
1331 ref = binder_get_ref(proc, tr->target.handle);
1333 binder_user_error("binder: %d:%d got "
1334 "transaction to invalid handle\n",
1335 proc->pid, thread->pid);
1336 return_error = BR_FAILED_REPLY;
1337 goto err_invalid_target_handle;
1339 target_node = ref->node;
1341 target_node = binder_context_mgr_node;
1342 if (target_node == NULL) {
1343 return_error = BR_DEAD_REPLY;
1344 goto err_no_context_mgr_node;
1347 e->to_node = target_node->debug_id;
1348 target_proc = target_node->proc;
1349 if (target_proc == NULL) {
1350 return_error = BR_DEAD_REPLY;
1351 goto err_dead_binder;
1353 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
1354 struct binder_transaction *tmp;
1355 tmp = thread->transaction_stack;
1356 if (tmp->to_thread != thread) {
1357 binder_user_error("binder: %d:%d got new "
1358 "transaction with bad transaction stack"
1359 ", transaction %d has target %d:%d\n",
1360 proc->pid, thread->pid, tmp->debug_id,
1361 tmp->to_proc ? tmp->to_proc->pid : 0,
1363 tmp->to_thread->pid : 0);
1364 return_error = BR_FAILED_REPLY;
1365 goto err_bad_call_stack;
1368 if (tmp->from && tmp->from->proc == target_proc)
1369 target_thread = tmp->from;
1370 tmp = tmp->from_parent;
1374 if (target_thread) {
1375 e->to_thread = target_thread->pid;
1376 target_list = &target_thread->todo;
1377 target_wait = &target_thread->wait;
1379 target_list = &target_proc->todo;
1380 target_wait = &target_proc->wait;
1382 e->to_proc = target_proc->pid;
1384 /* TODO: reuse incoming transaction for reply */
1385 t = kzalloc(sizeof(*t), GFP_KERNEL);
1387 return_error = BR_FAILED_REPLY;
1388 goto err_alloc_t_failed;
1390 binder_stats.obj_created[BINDER_STAT_TRANSACTION]++;
1392 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
1393 if (tcomplete == NULL) {
1394 return_error = BR_FAILED_REPLY;
1395 goto err_alloc_tcomplete_failed;
1397 binder_stats.obj_created[BINDER_STAT_TRANSACTION_COMPLETE]++;
1399 t->debug_id = ++binder_last_id;
1400 e->debug_id = t->debug_id;
1402 if (binder_debug_mask & BINDER_DEBUG_TRANSACTION) {
1404 printk(KERN_INFO "binder: %d:%d BC_REPLY %d -> %d:%d, "
1405 "data %p-%p size %zd-%zd\n",
1406 proc->pid, thread->pid, t->debug_id,
1407 target_proc->pid, target_thread->pid,
1408 tr->data.ptr.buffer, tr->data.ptr.offsets,
1409 tr->data_size, tr->offsets_size);
1411 printk(KERN_INFO "binder: %d:%d BC_TRANSACTION %d -> "
1412 "%d - node %d, data %p-%p size %zd-%zd\n",
1413 proc->pid, thread->pid, t->debug_id,
1414 target_proc->pid, target_node->debug_id,
1415 tr->data.ptr.buffer, tr->data.ptr.offsets,
1416 tr->data_size, tr->offsets_size);
1419 if (!reply && !(tr->flags & TF_ONE_WAY))
1423 t->sender_euid = proc->tsk->cred->euid;
1424 t->to_proc = target_proc;
1425 t->to_thread = target_thread;
1427 t->flags = tr->flags;
1428 t->priority = task_nice(current);
1429 t->buffer = binder_alloc_buf(target_proc, tr->data_size,
1430 tr->offsets_size, !reply && (t->flags & TF_ONE_WAY));
1431 if (t->buffer == NULL) {
1432 return_error = BR_FAILED_REPLY;
1433 goto err_binder_alloc_buf_failed;
1435 t->buffer->allow_user_free = 0;
1436 t->buffer->debug_id = t->debug_id;
1437 t->buffer->transaction = t;
1438 t->buffer->target_node = target_node;
1440 binder_inc_node(target_node, 1, 0, NULL);
1442 offp = (size_t *)(t->buffer->data + ALIGN(tr->data_size, sizeof(void *)));
1444 if (copy_from_user(t->buffer->data, tr->data.ptr.buffer, tr->data_size)) {
1445 binder_user_error("binder: %d:%d got transaction with invalid "
1446 "data ptr\n", proc->pid, thread->pid);
1447 return_error = BR_FAILED_REPLY;
1448 goto err_copy_data_failed;
1450 if (copy_from_user(offp, tr->data.ptr.offsets, tr->offsets_size)) {
1451 binder_user_error("binder: %d:%d got transaction with invalid "
1452 "offsets ptr\n", proc->pid, thread->pid);
1453 return_error = BR_FAILED_REPLY;
1454 goto err_copy_data_failed;
1456 if (!IS_ALIGNED(tr->offsets_size, sizeof(size_t))) {
1457 binder_user_error("binder: %d:%d got transaction with "
1458 "invalid offsets size, %zd\n",
1459 proc->pid, thread->pid, tr->offsets_size);
1460 return_error = BR_FAILED_REPLY;
1461 goto err_bad_offset;
1463 off_end = (void *)offp + tr->offsets_size;
1464 for (; offp < off_end; offp++) {
1465 struct flat_binder_object *fp;
1466 if (*offp > t->buffer->data_size - sizeof(*fp) ||
1467 t->buffer->data_size < sizeof(*fp) ||
1468 !IS_ALIGNED(*offp, sizeof(void *))) {
1469 binder_user_error("binder: %d:%d got transaction with "
1470 "invalid offset, %zd\n",
1471 proc->pid, thread->pid, *offp);
1472 return_error = BR_FAILED_REPLY;
1473 goto err_bad_offset;
1475 fp = (struct flat_binder_object *)(t->buffer->data + *offp);
1477 case BINDER_TYPE_BINDER:
1478 case BINDER_TYPE_WEAK_BINDER: {
1479 struct binder_ref *ref;
1480 struct binder_node *node = binder_get_node(proc, fp->binder);
1482 node = binder_new_node(proc, fp->binder, fp->cookie);
1484 return_error = BR_FAILED_REPLY;
1485 goto err_binder_new_node_failed;
1487 node->min_priority = fp->flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
1488 node->accept_fds = !!(fp->flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
1490 if (fp->cookie != node->cookie) {
1491 binder_user_error("binder: %d:%d sending u%p "
1492 "node %d, cookie mismatch %p != %p\n",
1493 proc->pid, thread->pid,
1494 fp->binder, node->debug_id,
1495 fp->cookie, node->cookie);
1496 goto err_binder_get_ref_for_node_failed;
1498 ref = binder_get_ref_for_node(target_proc, node);
1500 return_error = BR_FAILED_REPLY;
1501 goto err_binder_get_ref_for_node_failed;
1503 if (fp->type == BINDER_TYPE_BINDER)
1504 fp->type = BINDER_TYPE_HANDLE;
1506 fp->type = BINDER_TYPE_WEAK_HANDLE;
1507 fp->handle = ref->desc;
1508 binder_inc_ref(ref, fp->type == BINDER_TYPE_HANDLE, &thread->todo);
1509 if (binder_debug_mask & BINDER_DEBUG_TRANSACTION)
1510 printk(KERN_INFO " node %d u%p -> ref %d desc %d\n",
1511 node->debug_id, node->ptr, ref->debug_id, ref->desc);
1513 case BINDER_TYPE_HANDLE:
1514 case BINDER_TYPE_WEAK_HANDLE: {
1515 struct binder_ref *ref = binder_get_ref(proc, fp->handle);
1517 binder_user_error("binder: %d:%d got "
1518 "transaction with invalid "
1519 "handle, %ld\n", proc->pid,
1520 thread->pid, fp->handle);
1521 return_error = BR_FAILED_REPLY;
1522 goto err_binder_get_ref_failed;
1524 if (ref->node->proc == target_proc) {
1525 if (fp->type == BINDER_TYPE_HANDLE)
1526 fp->type = BINDER_TYPE_BINDER;
1528 fp->type = BINDER_TYPE_WEAK_BINDER;
1529 fp->binder = ref->node->ptr;
1530 fp->cookie = ref->node->cookie;
1531 binder_inc_node(ref->node, fp->type == BINDER_TYPE_BINDER, 0, NULL);
1532 if (binder_debug_mask & BINDER_DEBUG_TRANSACTION)
1533 printk(KERN_INFO " ref %d desc %d -> node %d u%p\n",
1534 ref->debug_id, ref->desc, ref->node->debug_id, ref->node->ptr);
1536 struct binder_ref *new_ref;
1537 new_ref = binder_get_ref_for_node(target_proc, ref->node);
1538 if (new_ref == NULL) {
1539 return_error = BR_FAILED_REPLY;
1540 goto err_binder_get_ref_for_node_failed;
1542 fp->handle = new_ref->desc;
1543 binder_inc_ref(new_ref, fp->type == BINDER_TYPE_HANDLE, NULL);
1544 if (binder_debug_mask & BINDER_DEBUG_TRANSACTION)
1545 printk(KERN_INFO " ref %d desc %d -> ref %d desc %d (node %d)\n",
1546 ref->debug_id, ref->desc, new_ref->debug_id, new_ref->desc, ref->node->debug_id);
1550 case BINDER_TYPE_FD: {
1555 if (!(in_reply_to->flags & TF_ACCEPT_FDS)) {
1556 binder_user_error("binder: %d:%d got reply with fd, %ld, but target does not allow fds\n",
1557 proc->pid, thread->pid, fp->handle);
1558 return_error = BR_FAILED_REPLY;
1559 goto err_fd_not_allowed;
1561 } else if (!target_node->accept_fds) {
1562 binder_user_error("binder: %d:%d got transaction with fd, %ld, but target does not allow fds\n",
1563 proc->pid, thread->pid, fp->handle);
1564 return_error = BR_FAILED_REPLY;
1565 goto err_fd_not_allowed;
1568 file = fget(fp->handle);
1570 binder_user_error("binder: %d:%d got transaction with invalid fd, %ld\n",
1571 proc->pid, thread->pid, fp->handle);
1572 return_error = BR_FAILED_REPLY;
1573 goto err_fget_failed;
1575 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
1576 if (target_fd < 0) {
1578 return_error = BR_FAILED_REPLY;
1579 goto err_get_unused_fd_failed;
1581 task_fd_install(target_proc, target_fd, file);
1582 if (binder_debug_mask & BINDER_DEBUG_TRANSACTION)
1583 printk(KERN_INFO " fd %ld -> %d\n", fp->handle, target_fd);
1585 fp->handle = target_fd;
1589 binder_user_error("binder: %d:%d got transactio"
1590 "n with invalid object type, %lx\n",
1591 proc->pid, thread->pid, fp->type);
1592 return_error = BR_FAILED_REPLY;
1593 goto err_bad_object_type;
1597 BUG_ON(t->buffer->async_transaction != 0);
1598 binder_pop_transaction(target_thread, in_reply_to);
1599 } else if (!(t->flags & TF_ONE_WAY)) {
1600 BUG_ON(t->buffer->async_transaction != 0);
1602 t->from_parent = thread->transaction_stack;
1603 thread->transaction_stack = t;
1605 BUG_ON(target_node == NULL);
1606 BUG_ON(t->buffer->async_transaction != 1);
1607 if (target_node->has_async_transaction) {
1608 target_list = &target_node->async_todo;
1611 target_node->has_async_transaction = 1;
1613 t->work.type = BINDER_WORK_TRANSACTION;
1614 list_add_tail(&t->work.entry, target_list);
1615 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
1616 list_add_tail(&tcomplete->entry, &thread->todo);
1618 wake_up_interruptible(target_wait);
1621 err_get_unused_fd_failed:
1624 err_binder_get_ref_for_node_failed:
1625 err_binder_get_ref_failed:
1626 err_binder_new_node_failed:
1627 err_bad_object_type:
1629 err_copy_data_failed:
1630 binder_transaction_buffer_release(target_proc, t->buffer, offp);
1631 t->buffer->transaction = NULL;
1632 binder_free_buf(target_proc, t->buffer);
1633 err_binder_alloc_buf_failed:
1635 binder_stats.obj_deleted[BINDER_STAT_TRANSACTION_COMPLETE]++;
1636 err_alloc_tcomplete_failed:
1638 binder_stats.obj_deleted[BINDER_STAT_TRANSACTION]++;
1641 err_empty_call_stack:
1643 err_invalid_target_handle:
1644 err_no_context_mgr_node:
1645 if (binder_debug_mask & BINDER_DEBUG_FAILED_TRANSACTION)
1646 printk(KERN_INFO "binder: %d:%d transaction failed %d, size"
1648 proc->pid, thread->pid, return_error,
1649 tr->data_size, tr->offsets_size);
1652 struct binder_transaction_log_entry *fe;
1653 fe = binder_transaction_log_add(&binder_transaction_log_failed);
1657 BUG_ON(thread->return_error != BR_OK);
1659 thread->return_error = BR_TRANSACTION_COMPLETE;
1660 binder_send_failed_reply(in_reply_to, return_error);
1662 thread->return_error = return_error;
1665 static void binder_transaction_buffer_release(struct binder_proc *proc,
1666 struct binder_buffer *buffer,
1669 size_t *offp, *off_end;
1670 int debug_id = buffer->debug_id;
1672 if (binder_debug_mask & BINDER_DEBUG_TRANSACTION)
1673 printk(KERN_INFO "binder: %d buffer release %d, size %zd-%zd, failed at %p\n",
1674 proc->pid, buffer->debug_id,
1675 buffer->data_size, buffer->offsets_size, failed_at);
1677 if (buffer->target_node)
1678 binder_dec_node(buffer->target_node, 1, 0);
1680 offp = (size_t *)(buffer->data + ALIGN(buffer->data_size, sizeof(void *)));
1682 off_end = failed_at;
1684 off_end = (void *)offp + buffer->offsets_size;
1685 for (; offp < off_end; offp++) {
1686 struct flat_binder_object *fp;
1687 if (*offp > buffer->data_size - sizeof(*fp) ||
1688 buffer->data_size < sizeof(*fp) ||
1689 !IS_ALIGNED(*offp, sizeof(void *))) {
1690 printk(KERN_ERR "binder: transaction release %d bad"
1691 "offset %zd, size %zd\n", debug_id, *offp, buffer->data_size);
1694 fp = (struct flat_binder_object *)(buffer->data + *offp);
1696 case BINDER_TYPE_BINDER:
1697 case BINDER_TYPE_WEAK_BINDER: {
1698 struct binder_node *node = binder_get_node(proc, fp->binder);
1700 printk(KERN_ERR "binder: transaction release %d bad node %p\n", debug_id, fp->binder);
1703 if (binder_debug_mask & BINDER_DEBUG_TRANSACTION)
1704 printk(KERN_INFO " node %d u%p\n",
1705 node->debug_id, node->ptr);
1706 binder_dec_node(node, fp->type == BINDER_TYPE_BINDER, 0);
1708 case BINDER_TYPE_HANDLE:
1709 case BINDER_TYPE_WEAK_HANDLE: {
1710 struct binder_ref *ref = binder_get_ref(proc, fp->handle);
1712 printk(KERN_ERR "binder: transaction release %d bad handle %ld\n", debug_id, fp->handle);
1715 if (binder_debug_mask & BINDER_DEBUG_TRANSACTION)
1716 printk(KERN_INFO " ref %d desc %d (node %d)\n",
1717 ref->debug_id, ref->desc, ref->node->debug_id);
1718 binder_dec_ref(ref, fp->type == BINDER_TYPE_HANDLE);
1721 case BINDER_TYPE_FD:
1722 if (binder_debug_mask & BINDER_DEBUG_TRANSACTION)
1723 printk(KERN_INFO " fd %ld\n", fp->handle);
1725 task_close_fd(proc, fp->handle);
1729 printk(KERN_ERR "binder: transaction release %d bad object type %lx\n", debug_id, fp->type);
1735 int binder_thread_write(struct binder_proc *proc, struct binder_thread *thread,
1736 void __user *buffer, int size, signed long *consumed)
1739 void __user *ptr = buffer + *consumed;
1740 void __user *end = buffer + size;
1742 while (ptr < end && thread->return_error == BR_OK) {
1743 if (get_user(cmd, (uint32_t __user *)ptr))
1745 ptr += sizeof(uint32_t);
1746 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
1747 binder_stats.bc[_IOC_NR(cmd)]++;
1748 proc->stats.bc[_IOC_NR(cmd)]++;
1749 thread->stats.bc[_IOC_NR(cmd)]++;
1757 struct binder_ref *ref;
1758 const char *debug_string;
1760 if (get_user(target, (uint32_t __user *)ptr))
1762 ptr += sizeof(uint32_t);
1763 if (target == 0 && binder_context_mgr_node &&
1764 (cmd == BC_INCREFS || cmd == BC_ACQUIRE)) {
1765 ref = binder_get_ref_for_node(proc,
1766 binder_context_mgr_node);
1767 if (ref->desc != target) {
1768 binder_user_error("binder: %d:"
1769 "%d tried to acquire "
1770 "reference to desc 0, "
1772 proc->pid, thread->pid,
1776 ref = binder_get_ref(proc, target);
1778 binder_user_error("binder: %d:%d refcou"
1779 "nt change on invalid ref %d\n",
1780 proc->pid, thread->pid, target);
1785 debug_string = "IncRefs";
1786 binder_inc_ref(ref, 0, NULL);
1789 debug_string = "Acquire";
1790 binder_inc_ref(ref, 1, NULL);
1793 debug_string = "Release";
1794 binder_dec_ref(ref, 1);
1798 debug_string = "DecRefs";
1799 binder_dec_ref(ref, 0);
1802 if (binder_debug_mask & BINDER_DEBUG_USER_REFS)
1803 printk(KERN_INFO "binder: %d:%d %s ref %d desc %d s %d w %d for node %d\n",
1804 proc->pid, thread->pid, debug_string, ref->debug_id, ref->desc, ref->strong, ref->weak, ref->node->debug_id);
1807 case BC_INCREFS_DONE:
1808 case BC_ACQUIRE_DONE: {
1809 void __user *node_ptr;
1811 struct binder_node *node;
1813 if (get_user(node_ptr, (void * __user *)ptr))
1815 ptr += sizeof(void *);
1816 if (get_user(cookie, (void * __user *)ptr))
1818 ptr += sizeof(void *);
1819 node = binder_get_node(proc, node_ptr);
1821 binder_user_error("binder: %d:%d "
1822 "%s u%p no match\n",
1823 proc->pid, thread->pid,
1824 cmd == BC_INCREFS_DONE ?
1830 if (cookie != node->cookie) {
1831 binder_user_error("binder: %d:%d %s u%p node %d"
1832 " cookie mismatch %p != %p\n",
1833 proc->pid, thread->pid,
1834 cmd == BC_INCREFS_DONE ?
1835 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
1836 node_ptr, node->debug_id,
1837 cookie, node->cookie);
1840 if (cmd == BC_ACQUIRE_DONE) {
1841 if (node->pending_strong_ref == 0) {
1842 binder_user_error("binder: %d:%d "
1843 "BC_ACQUIRE_DONE node %d has "
1844 "no pending acquire request\n",
1845 proc->pid, thread->pid,
1849 node->pending_strong_ref = 0;
1851 if (node->pending_weak_ref == 0) {
1852 binder_user_error("binder: %d:%d "
1853 "BC_INCREFS_DONE node %d has "
1854 "no pending increfs request\n",
1855 proc->pid, thread->pid,
1859 node->pending_weak_ref = 0;
1861 binder_dec_node(node, cmd == BC_ACQUIRE_DONE, 0);
1862 if (binder_debug_mask & BINDER_DEBUG_USER_REFS)
1863 printk(KERN_INFO "binder: %d:%d %s node %d ls %d lw %d\n",
1864 proc->pid, thread->pid, cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE", node->debug_id, node->local_strong_refs, node->local_weak_refs);
1867 case BC_ATTEMPT_ACQUIRE:
1868 printk(KERN_ERR "binder: BC_ATTEMPT_ACQUIRE not supported\n");
1870 case BC_ACQUIRE_RESULT:
1871 printk(KERN_ERR "binder: BC_ACQUIRE_RESULT not supported\n");
1874 case BC_FREE_BUFFER: {
1875 void __user *data_ptr;
1876 struct binder_buffer *buffer;
1878 if (get_user(data_ptr, (void * __user *)ptr))
1880 ptr += sizeof(void *);
1882 buffer = binder_buffer_lookup(proc, data_ptr);
1883 if (buffer == NULL) {
1884 binder_user_error("binder: %d:%d "
1885 "BC_FREE_BUFFER u%p no match\n",
1886 proc->pid, thread->pid, data_ptr);
1889 if (!buffer->allow_user_free) {
1890 binder_user_error("binder: %d:%d "
1891 "BC_FREE_BUFFER u%p matched "
1892 "unreturned buffer\n",
1893 proc->pid, thread->pid, data_ptr);
1896 if (binder_debug_mask & BINDER_DEBUG_FREE_BUFFER)
1897 printk(KERN_INFO "binder: %d:%d BC_FREE_BUFFER u%p found buffer %d for %s transaction\n",
1898 proc->pid, thread->pid, data_ptr, buffer->debug_id,
1899 buffer->transaction ? "active" : "finished");
1901 if (buffer->transaction) {
1902 buffer->transaction->buffer = NULL;
1903 buffer->transaction = NULL;
1905 if (buffer->async_transaction && buffer->target_node) {
1906 BUG_ON(!buffer->target_node->has_async_transaction);
1907 if (list_empty(&buffer->target_node->async_todo))
1908 buffer->target_node->has_async_transaction = 0;
1910 list_move_tail(buffer->target_node->async_todo.next, &thread->todo);
1912 binder_transaction_buffer_release(proc, buffer, NULL);
1913 binder_free_buf(proc, buffer);
1917 case BC_TRANSACTION:
1919 struct binder_transaction_data tr;
1921 if (copy_from_user(&tr, ptr, sizeof(tr)))
1924 binder_transaction(proc, thread, &tr, cmd == BC_REPLY);
1928 case BC_REGISTER_LOOPER:
1929 if (binder_debug_mask & BINDER_DEBUG_THREADS)
1930 printk(KERN_INFO "binder: %d:%d BC_REGISTER_LOOPER\n",
1931 proc->pid, thread->pid);
1932 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
1933 thread->looper |= BINDER_LOOPER_STATE_INVALID;
1934 binder_user_error("binder: %d:%d ERROR:"
1935 " BC_REGISTER_LOOPER called "
1936 "after BC_ENTER_LOOPER\n",
1937 proc->pid, thread->pid);
1938 } else if (proc->requested_threads == 0) {
1939 thread->looper |= BINDER_LOOPER_STATE_INVALID;
1940 binder_user_error("binder: %d:%d ERROR:"
1941 " BC_REGISTER_LOOPER called "
1942 "without request\n",
1943 proc->pid, thread->pid);
1945 proc->requested_threads--;
1946 proc->requested_threads_started++;
1948 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
1950 case BC_ENTER_LOOPER:
1951 if (binder_debug_mask & BINDER_DEBUG_THREADS)
1952 printk(KERN_INFO "binder: %d:%d BC_ENTER_LOOPER\n",
1953 proc->pid, thread->pid);
1954 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
1955 thread->looper |= BINDER_LOOPER_STATE_INVALID;
1956 binder_user_error("binder: %d:%d ERROR:"
1957 " BC_ENTER_LOOPER called after "
1958 "BC_REGISTER_LOOPER\n",
1959 proc->pid, thread->pid);
1961 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
1963 case BC_EXIT_LOOPER:
1964 if (binder_debug_mask & BINDER_DEBUG_THREADS)
1965 printk(KERN_INFO "binder: %d:%d BC_EXIT_LOOPER\n",
1966 proc->pid, thread->pid);
1967 thread->looper |= BINDER_LOOPER_STATE_EXITED;
1970 case BC_REQUEST_DEATH_NOTIFICATION:
1971 case BC_CLEAR_DEATH_NOTIFICATION: {
1973 void __user *cookie;
1974 struct binder_ref *ref;
1975 struct binder_ref_death *death;
1977 if (get_user(target, (uint32_t __user *)ptr))
1979 ptr += sizeof(uint32_t);
1980 if (get_user(cookie, (void __user * __user *)ptr))
1982 ptr += sizeof(void *);
1983 ref = binder_get_ref(proc, target);
1985 binder_user_error("binder: %d:%d %s "
1987 proc->pid, thread->pid,
1988 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
1989 "BC_REQUEST_DEATH_NOTIFICATION" :
1990 "BC_CLEAR_DEATH_NOTIFICATION",
1995 if (binder_debug_mask & BINDER_DEBUG_DEATH_NOTIFICATION)
1996 printk(KERN_INFO "binder: %d:%d %s %p ref %d desc %d s %d w %d for node %d\n",
1997 proc->pid, thread->pid,
1998 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
1999 "BC_REQUEST_DEATH_NOTIFICATION" :
2000 "BC_CLEAR_DEATH_NOTIFICATION",
2001 cookie, ref->debug_id, ref->desc,
2002 ref->strong, ref->weak, ref->node->debug_id);
2004 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
2006 binder_user_error("binder: %d:%"
2007 "d BC_REQUEST_DEATH_NOTI"
2008 "FICATION death notific"
2009 "ation already set\n",
2010 proc->pid, thread->pid);
2013 death = kzalloc(sizeof(*death), GFP_KERNEL);
2014 if (death == NULL) {
2015 thread->return_error = BR_ERROR;
2016 if (binder_debug_mask & BINDER_DEBUG_FAILED_TRANSACTION)
2017 printk(KERN_INFO "binder: %d:%d "
2018 "BC_REQUEST_DEATH_NOTIFICATION failed\n",
2019 proc->pid, thread->pid);
2022 binder_stats.obj_created[BINDER_STAT_DEATH]++;
2023 INIT_LIST_HEAD(&death->work.entry);
2024 death->cookie = cookie;
2026 if (ref->node->proc == NULL) {
2027 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
2028 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2029 list_add_tail(&ref->death->work.entry, &thread->todo);
2031 list_add_tail(&ref->death->work.entry, &proc->todo);
2032 wake_up_interruptible(&proc->wait);
2036 if (ref->death == NULL) {
2037 binder_user_error("binder: %d:%"
2038 "d BC_CLEAR_DEATH_NOTIFI"
2039 "CATION death notificat"
2041 proc->pid, thread->pid);
2045 if (death->cookie != cookie) {
2046 binder_user_error("binder: %d:%"
2047 "d BC_CLEAR_DEATH_NOTIFI"
2048 "CATION death notificat"
2049 "ion cookie mismatch "
2051 proc->pid, thread->pid,
2052 death->cookie, cookie);
2056 if (list_empty(&death->work.entry)) {
2057 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2058 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2059 list_add_tail(&death->work.entry, &thread->todo);
2061 list_add_tail(&death->work.entry, &proc->todo);
2062 wake_up_interruptible(&proc->wait);
2065 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
2066 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
2070 case BC_DEAD_BINDER_DONE: {
2071 struct binder_work *w;
2072 void __user *cookie;
2073 struct binder_ref_death *death = NULL;
2074 if (get_user(cookie, (void __user * __user *)ptr))
2077 ptr += sizeof(void *);
2078 list_for_each_entry(w, &proc->delivered_death, entry) {
2079 struct binder_ref_death *tmp_death = container_of(w, struct binder_ref_death, work);
2080 if (tmp_death->cookie == cookie) {
2085 if (binder_debug_mask & BINDER_DEBUG_DEAD_BINDER)
2086 printk(KERN_INFO "binder: %d:%d BC_DEAD_BINDER_DONE %p found %p\n",
2087 proc->pid, thread->pid, cookie, death);
2088 if (death == NULL) {
2089 binder_user_error("binder: %d:%d BC_DEAD"
2090 "_BINDER_DONE %p not found\n",
2091 proc->pid, thread->pid, cookie);
2095 list_del_init(&death->work.entry);
2096 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
2097 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2098 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2099 list_add_tail(&death->work.entry, &thread->todo);
2101 list_add_tail(&death->work.entry, &proc->todo);
2102 wake_up_interruptible(&proc->wait);
2108 printk(KERN_ERR "binder: %d:%d unknown command %d\n",
2109 proc->pid, thread->pid, cmd);
2112 *consumed = ptr - buffer;
2117 void binder_stat_br(struct binder_proc *proc, struct binder_thread *thread,
2120 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
2121 binder_stats.br[_IOC_NR(cmd)]++;
2122 proc->stats.br[_IOC_NR(cmd)]++;
2123 thread->stats.br[_IOC_NR(cmd)]++;
2127 static int binder_has_proc_work(struct binder_proc *proc,
2128 struct binder_thread *thread)
2130 return !list_empty(&proc->todo) ||
2131 (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2134 static int binder_has_thread_work(struct binder_thread *thread)
2136 return !list_empty(&thread->todo) || thread->return_error != BR_OK ||
2137 (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2140 static int binder_thread_read(struct binder_proc *proc,
2141 struct binder_thread *thread,
2142 void __user *buffer, int size,
2143 signed long *consumed, int non_block)
2145 void __user *ptr = buffer + *consumed;
2146 void __user *end = buffer + size;
2149 int wait_for_proc_work;
2151 if (*consumed == 0) {
2152 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
2154 ptr += sizeof(uint32_t);
2158 wait_for_proc_work = thread->transaction_stack == NULL &&
2159 list_empty(&thread->todo);
2161 if (thread->return_error != BR_OK && ptr < end) {
2162 if (thread->return_error2 != BR_OK) {
2163 if (put_user(thread->return_error2, (uint32_t __user *)ptr))
2165 ptr += sizeof(uint32_t);
2168 thread->return_error2 = BR_OK;
2170 if (put_user(thread->return_error, (uint32_t __user *)ptr))
2172 ptr += sizeof(uint32_t);
2173 thread->return_error = BR_OK;
2178 thread->looper |= BINDER_LOOPER_STATE_WAITING;
2179 if (wait_for_proc_work)
2180 proc->ready_threads++;
2181 mutex_unlock(&binder_lock);
2182 if (wait_for_proc_work) {
2183 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2184 BINDER_LOOPER_STATE_ENTERED))) {
2185 binder_user_error("binder: %d:%d ERROR: Thread waiting "
2186 "for process work before calling BC_REGISTER_"
2187 "LOOPER or BC_ENTER_LOOPER (state %x)\n",
2188 proc->pid, thread->pid, thread->looper);
2189 wait_event_interruptible(binder_user_error_wait,
2190 binder_stop_on_user_error < 2);
2192 binder_set_nice(proc->default_priority);
2194 if (!binder_has_proc_work(proc, thread))
2197 ret = wait_event_interruptible_exclusive(proc->wait, binder_has_proc_work(proc, thread));
2200 if (!binder_has_thread_work(thread))
2203 ret = wait_event_interruptible(thread->wait, binder_has_thread_work(thread));
2205 mutex_lock(&binder_lock);
2206 if (wait_for_proc_work)
2207 proc->ready_threads--;
2208 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
2215 struct binder_transaction_data tr;
2216 struct binder_work *w;
2217 struct binder_transaction *t = NULL;
2219 if (!list_empty(&thread->todo))
2220 w = list_first_entry(&thread->todo, struct binder_work, entry);
2221 else if (!list_empty(&proc->todo) && wait_for_proc_work)
2222 w = list_first_entry(&proc->todo, struct binder_work, entry);
2224 if (ptr - buffer == 4 && !(thread->looper & BINDER_LOOPER_STATE_NEED_RETURN)) /* no data added */
2229 if (end - ptr < sizeof(tr) + 4)
2233 case BINDER_WORK_TRANSACTION: {
2234 t = container_of(w, struct binder_transaction, work);
2236 case BINDER_WORK_TRANSACTION_COMPLETE: {
2237 cmd = BR_TRANSACTION_COMPLETE;
2238 if (put_user(cmd, (uint32_t __user *)ptr))
2240 ptr += sizeof(uint32_t);
2242 binder_stat_br(proc, thread, cmd);
2243 if (binder_debug_mask & BINDER_DEBUG_TRANSACTION_COMPLETE)
2244 printk(KERN_INFO "binder: %d:%d BR_TRANSACTION_COMPLETE\n",
2245 proc->pid, thread->pid);
2247 list_del(&w->entry);
2249 binder_stats.obj_deleted[BINDER_STAT_TRANSACTION_COMPLETE]++;
2251 case BINDER_WORK_NODE: {
2252 struct binder_node *node = container_of(w, struct binder_node, work);
2253 uint32_t cmd = BR_NOOP;
2254 const char *cmd_name;
2255 int strong = node->internal_strong_refs || node->local_strong_refs;
2256 int weak = !hlist_empty(&node->refs) || node->local_weak_refs || strong;
2257 if (weak && !node->has_weak_ref) {
2259 cmd_name = "BR_INCREFS";
2260 node->has_weak_ref = 1;
2261 node->pending_weak_ref = 1;
2262 node->local_weak_refs++;
2263 } else if (strong && !node->has_strong_ref) {
2265 cmd_name = "BR_ACQUIRE";
2266 node->has_strong_ref = 1;
2267 node->pending_strong_ref = 1;
2268 node->local_strong_refs++;
2269 } else if (!strong && node->has_strong_ref) {
2271 cmd_name = "BR_RELEASE";
2272 node->has_strong_ref = 0;
2273 } else if (!weak && node->has_weak_ref) {
2275 cmd_name = "BR_DECREFS";
2276 node->has_weak_ref = 0;
2278 if (cmd != BR_NOOP) {
2279 if (put_user(cmd, (uint32_t __user *)ptr))
2281 ptr += sizeof(uint32_t);
2282 if (put_user(node->ptr, (void * __user *)ptr))
2284 ptr += sizeof(void *);
2285 if (put_user(node->cookie, (void * __user *)ptr))
2287 ptr += sizeof(void *);
2289 binder_stat_br(proc, thread, cmd);
2290 if (binder_debug_mask & BINDER_DEBUG_USER_REFS)
2291 printk(KERN_INFO "binder: %d:%d %s %d u%p c%p\n",
2292 proc->pid, thread->pid, cmd_name, node->debug_id, node->ptr, node->cookie);
2294 list_del_init(&w->entry);
2295 if (!weak && !strong) {
2296 if (binder_debug_mask & BINDER_DEBUG_INTERNAL_REFS)
2297 printk(KERN_INFO "binder: %d:%d node %d u%p c%p deleted\n",
2298 proc->pid, thread->pid, node->debug_id, node->ptr, node->cookie);
2299 rb_erase(&node->rb_node, &proc->nodes);
2301 binder_stats.obj_deleted[BINDER_STAT_NODE]++;
2303 if (binder_debug_mask & BINDER_DEBUG_INTERNAL_REFS)
2304 printk(KERN_INFO "binder: %d:%d node %d u%p c%p state unchanged\n",
2305 proc->pid, thread->pid, node->debug_id, node->ptr, node->cookie);
2309 case BINDER_WORK_DEAD_BINDER:
2310 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2311 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2312 struct binder_ref_death *death;
2315 death = container_of(w, struct binder_ref_death, work);
2316 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
2317 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
2319 cmd = BR_DEAD_BINDER;
2320 if (put_user(cmd, (uint32_t __user *)ptr))
2322 ptr += sizeof(uint32_t);
2323 if (put_user(death->cookie, (void * __user *)ptr))
2325 ptr += sizeof(void *);
2326 if (binder_debug_mask & BINDER_DEBUG_DEATH_NOTIFICATION)
2327 printk(KERN_INFO "binder: %d:%d %s %p\n",
2328 proc->pid, thread->pid,
2329 cmd == BR_DEAD_BINDER ?
2331 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
2334 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
2335 list_del(&w->entry);
2337 binder_stats.obj_deleted[BINDER_STAT_DEATH]++;
2339 list_move(&w->entry, &proc->delivered_death);
2340 if (cmd == BR_DEAD_BINDER)
2341 goto done; /* DEAD_BINDER notifications can cause transactions */
2348 BUG_ON(t->buffer == NULL);
2349 if (t->buffer->target_node) {
2350 struct binder_node *target_node = t->buffer->target_node;
2351 tr.target.ptr = target_node->ptr;
2352 tr.cookie = target_node->cookie;
2353 t->saved_priority = task_nice(current);
2354 if (t->priority < target_node->min_priority &&
2355 !(t->flags & TF_ONE_WAY))
2356 binder_set_nice(t->priority);
2357 else if (!(t->flags & TF_ONE_WAY) ||
2358 t->saved_priority > target_node->min_priority)
2359 binder_set_nice(target_node->min_priority);
2360 cmd = BR_TRANSACTION;
2362 tr.target.ptr = NULL;
2367 tr.flags = t->flags;
2368 tr.sender_euid = t->sender_euid;
2371 struct task_struct *sender = t->from->proc->tsk;
2372 tr.sender_pid = task_tgid_nr_ns(sender,
2373 current->nsproxy->pid_ns);
2378 tr.data_size = t->buffer->data_size;
2379 tr.offsets_size = t->buffer->offsets_size;
2380 tr.data.ptr.buffer = (void *)t->buffer->data +
2381 proc->user_buffer_offset;
2382 tr.data.ptr.offsets = tr.data.ptr.buffer +
2383 ALIGN(t->buffer->data_size,
2386 if (put_user(cmd, (uint32_t __user *)ptr))
2388 ptr += sizeof(uint32_t);
2389 if (copy_to_user(ptr, &tr, sizeof(tr)))
2393 binder_stat_br(proc, thread, cmd);
2394 if (binder_debug_mask & BINDER_DEBUG_TRANSACTION)
2395 printk(KERN_INFO "binder: %d:%d %s %d %d:%d, cmd %d"
2396 "size %zd-%zd ptr %p-%p\n",
2397 proc->pid, thread->pid,
2398 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
2400 t->debug_id, t->from ? t->from->proc->pid : 0,
2401 t->from ? t->from->pid : 0, cmd,
2402 t->buffer->data_size, t->buffer->offsets_size,
2403 tr.data.ptr.buffer, tr.data.ptr.offsets);
2405 list_del(&t->work.entry);
2406 t->buffer->allow_user_free = 1;
2407 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
2408 t->to_parent = thread->transaction_stack;
2409 t->to_thread = thread;
2410 thread->transaction_stack = t;
2412 t->buffer->transaction = NULL;
2414 binder_stats.obj_deleted[BINDER_STAT_TRANSACTION]++;
2421 *consumed = ptr - buffer;
2422 if (proc->requested_threads + proc->ready_threads == 0 &&
2423 proc->requested_threads_started < proc->max_threads &&
2424 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2425 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
2426 /*spawn a new thread if we leave this out */) {
2427 proc->requested_threads++;
2428 if (binder_debug_mask & BINDER_DEBUG_THREADS)
2429 printk(KERN_INFO "binder: %d:%d BR_SPAWN_LOOPER\n",
2430 proc->pid, thread->pid);
2431 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
2437 static void binder_release_work(struct list_head *list)
2439 struct binder_work *w;
2440 while (!list_empty(list)) {
2441 w = list_first_entry(list, struct binder_work, entry);
2442 list_del_init(&w->entry);
2444 case BINDER_WORK_TRANSACTION: {
2445 struct binder_transaction *t;
2447 t = container_of(w, struct binder_transaction, work);
2448 if (t->buffer->target_node && !(t->flags & TF_ONE_WAY))
2449 binder_send_failed_reply(t, BR_DEAD_REPLY);
2451 case BINDER_WORK_TRANSACTION_COMPLETE: {
2453 binder_stats.obj_deleted[BINDER_STAT_TRANSACTION_COMPLETE]++;
2462 static struct binder_thread *binder_get_thread(struct binder_proc *proc)
2464 struct binder_thread *thread = NULL;
2465 struct rb_node *parent = NULL;
2466 struct rb_node **p = &proc->threads.rb_node;
2470 thread = rb_entry(parent, struct binder_thread, rb_node);
2472 if (current->pid < thread->pid)
2474 else if (current->pid > thread->pid)
2475 p = &(*p)->rb_right;
2480 thread = kzalloc(sizeof(*thread), GFP_KERNEL);
2483 binder_stats.obj_created[BINDER_STAT_THREAD]++;
2484 thread->proc = proc;
2485 thread->pid = current->pid;
2486 init_waitqueue_head(&thread->wait);
2487 INIT_LIST_HEAD(&thread->todo);
2488 rb_link_node(&thread->rb_node, parent, p);
2489 rb_insert_color(&thread->rb_node, &proc->threads);
2490 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
2491 thread->return_error = BR_OK;
2492 thread->return_error2 = BR_OK;
2497 static int binder_free_thread(struct binder_proc *proc,
2498 struct binder_thread *thread)
2500 struct binder_transaction *t;
2501 struct binder_transaction *send_reply = NULL;
2502 int active_transactions = 0;
2504 rb_erase(&thread->rb_node, &proc->threads);
2505 t = thread->transaction_stack;
2506 if (t && t->to_thread == thread)
2509 active_transactions++;
2510 if (binder_debug_mask & BINDER_DEBUG_DEAD_TRANSACTION)
2511 printk(KERN_INFO "binder: release %d:%d transaction %d "
2512 "%s, still active\n", proc->pid, thread->pid,
2514 (t->to_thread == thread) ? "in" : "out");
2515 if (t->to_thread == thread) {
2517 t->to_thread = NULL;
2519 t->buffer->transaction = NULL;
2523 } else if (t->from == thread) {
2530 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
2531 binder_release_work(&thread->todo);
2533 binder_stats.obj_deleted[BINDER_STAT_THREAD]++;
2534 return active_transactions;
2537 static unsigned int binder_poll(struct file *filp,
2538 struct poll_table_struct *wait)
2540 struct binder_proc *proc = filp->private_data;
2541 struct binder_thread *thread = NULL;
2542 int wait_for_proc_work;
2544 mutex_lock(&binder_lock);
2545 thread = binder_get_thread(proc);
2547 wait_for_proc_work = thread->transaction_stack == NULL &&
2548 list_empty(&thread->todo) && thread->return_error == BR_OK;
2549 mutex_unlock(&binder_lock);
2551 if (wait_for_proc_work) {
2552 if (binder_has_proc_work(proc, thread))
2554 poll_wait(filp, &proc->wait, wait);
2555 if (binder_has_proc_work(proc, thread))
2558 if (binder_has_thread_work(thread))
2560 poll_wait(filp, &thread->wait, wait);
2561 if (binder_has_thread_work(thread))
2567 static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2570 struct binder_proc *proc = filp->private_data;
2571 struct binder_thread *thread;
2572 unsigned int size = _IOC_SIZE(cmd);
2573 void __user *ubuf = (void __user *)arg;
2575 /*printk(KERN_INFO "binder_ioctl: %d:%d %x %lx\n", proc->pid, current->pid, cmd, arg);*/
2577 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2581 mutex_lock(&binder_lock);
2582 thread = binder_get_thread(proc);
2583 if (thread == NULL) {
2589 case BINDER_WRITE_READ: {
2590 struct binder_write_read bwr;
2591 if (size != sizeof(struct binder_write_read)) {
2595 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
2599 if (binder_debug_mask & BINDER_DEBUG_READ_WRITE)
2600 printk(KERN_INFO "binder: %d:%d write %ld at %08lx, read %ld at %08lx\n",
2601 proc->pid, thread->pid, bwr.write_size, bwr.write_buffer, bwr.read_size, bwr.read_buffer);
2602 if (bwr.write_size > 0) {
2603 ret = binder_thread_write(proc, thread, (void __user *)bwr.write_buffer, bwr.write_size, &bwr.write_consumed);
2605 bwr.read_consumed = 0;
2606 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2611 if (bwr.read_size > 0) {
2612 ret = binder_thread_read(proc, thread, (void __user *)bwr.read_buffer, bwr.read_size, &bwr.read_consumed, filp->f_flags & O_NONBLOCK);
2613 if (!list_empty(&proc->todo))
2614 wake_up_interruptible(&proc->wait);
2616 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2621 if (binder_debug_mask & BINDER_DEBUG_READ_WRITE)
2622 printk(KERN_INFO "binder: %d:%d wrote %ld of %ld, read return %ld of %ld\n",
2623 proc->pid, thread->pid, bwr.write_consumed, bwr.write_size, bwr.read_consumed, bwr.read_size);
2624 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
2630 case BINDER_SET_MAX_THREADS:
2631 if (copy_from_user(&proc->max_threads, ubuf, sizeof(proc->max_threads))) {
2636 case BINDER_SET_CONTEXT_MGR:
2637 if (binder_context_mgr_node != NULL) {
2638 printk(KERN_ERR "binder: BINDER_SET_CONTEXT_MGR already set\n");
2642 if (binder_context_mgr_uid != -1) {
2643 if (binder_context_mgr_uid != current->cred->euid) {
2644 printk(KERN_ERR "binder: BINDER_SET_"
2645 "CONTEXT_MGR bad uid %d != %d\n",
2646 current->cred->euid,
2647 binder_context_mgr_uid);
2652 binder_context_mgr_uid = current->cred->euid;
2653 binder_context_mgr_node = binder_new_node(proc, NULL, NULL);
2654 if (binder_context_mgr_node == NULL) {
2658 binder_context_mgr_node->local_weak_refs++;
2659 binder_context_mgr_node->local_strong_refs++;
2660 binder_context_mgr_node->has_strong_ref = 1;
2661 binder_context_mgr_node->has_weak_ref = 1;
2663 case BINDER_THREAD_EXIT:
2664 if (binder_debug_mask & BINDER_DEBUG_THREADS)
2665 printk(KERN_INFO "binder: %d:%d exit\n",
2666 proc->pid, thread->pid);
2667 binder_free_thread(proc, thread);
2670 case BINDER_VERSION:
2671 if (size != sizeof(struct binder_version)) {
2675 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION, &((struct binder_version *)ubuf)->protocol_version)) {
2687 thread->looper &= ~BINDER_LOOPER_STATE_NEED_RETURN;
2688 mutex_unlock(&binder_lock);
2689 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2690 if (ret && ret != -ERESTARTSYS)
2691 printk(KERN_INFO "binder: %d:%d ioctl %x %lx returned %d\n", proc->pid, current->pid, cmd, arg, ret);
2695 static void binder_vma_open(struct vm_area_struct *vma)
2697 struct binder_proc *proc = vma->vm_private_data;
2698 if (binder_debug_mask & BINDER_DEBUG_OPEN_CLOSE)
2700 "binder: %d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
2701 proc->pid, vma->vm_start, vma->vm_end,
2702 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2703 (unsigned long)pgprot_val(vma->vm_page_prot));
2707 static void binder_vma_close(struct vm_area_struct *vma)
2709 struct binder_proc *proc = vma->vm_private_data;
2710 if (binder_debug_mask & BINDER_DEBUG_OPEN_CLOSE)
2712 "binder: %d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
2713 proc->pid, vma->vm_start, vma->vm_end,
2714 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2715 (unsigned long)pgprot_val(vma->vm_page_prot));
2717 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
2720 static struct vm_operations_struct binder_vm_ops = {
2721 .open = binder_vma_open,
2722 .close = binder_vma_close,
2725 static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
2728 struct vm_struct *area;
2729 struct binder_proc *proc = filp->private_data;
2730 const char *failure_string;
2731 struct binder_buffer *buffer;
2733 if ((vma->vm_end - vma->vm_start) > SZ_4M)
2734 vma->vm_end = vma->vm_start + SZ_4M;
2736 if (binder_debug_mask & BINDER_DEBUG_OPEN_CLOSE)
2738 "binder_mmap: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
2739 proc->pid, vma->vm_start, vma->vm_end,
2740 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2741 (unsigned long)pgprot_val(vma->vm_page_prot));
2743 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
2745 failure_string = "bad vm_flags";
2748 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
2752 failure_string = "already mapped";
2753 goto err_already_mapped;
2756 area = get_vm_area(vma->vm_end - vma->vm_start, VM_IOREMAP);
2759 failure_string = "get_vm_area";
2760 goto err_get_vm_area_failed;
2762 proc->buffer = area->addr;
2763 proc->user_buffer_offset = vma->vm_start - (uintptr_t)proc->buffer;
2765 #ifdef CONFIG_CPU_CACHE_VIPT
2766 if (cache_is_vipt_aliasing()) {
2767 while (CACHE_COLOUR((vma->vm_start ^ (uint32_t)proc->buffer))) {
2768 printk(KERN_INFO "binder_mmap: %d %lx-%lx maps %p bad alignment\n", proc->pid, vma->vm_start, vma->vm_end, proc->buffer);
2769 vma->vm_start += PAGE_SIZE;
2773 proc->pages = kzalloc(sizeof(proc->pages[0]) * ((vma->vm_end - vma->vm_start) / PAGE_SIZE), GFP_KERNEL);
2774 if (proc->pages == NULL) {
2776 failure_string = "alloc page array";
2777 goto err_alloc_pages_failed;
2779 proc->buffer_size = vma->vm_end - vma->vm_start;
2781 vma->vm_ops = &binder_vm_ops;
2782 vma->vm_private_data = proc;
2784 if (binder_update_page_range(proc, 1, proc->buffer, proc->buffer + PAGE_SIZE, vma)) {
2786 failure_string = "alloc small buf";
2787 goto err_alloc_small_buf_failed;
2789 buffer = proc->buffer;
2790 INIT_LIST_HEAD(&proc->buffers);
2791 list_add(&buffer->entry, &proc->buffers);
2793 binder_insert_free_buffer(proc, buffer);
2794 proc->free_async_space = proc->buffer_size / 2;
2796 proc->files = get_files_struct(current);
2799 /*printk(KERN_INFO "binder_mmap: %d %lx-%lx maps %p\n",
2800 proc->pid, vma->vm_start, vma->vm_end, proc->buffer);*/
2803 err_alloc_small_buf_failed:
2806 err_alloc_pages_failed:
2807 vfree(proc->buffer);
2808 proc->buffer = NULL;
2809 err_get_vm_area_failed:
2812 printk(KERN_ERR "binder_mmap: %d %lx-%lx %s failed %d\n",
2813 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
2817 static int binder_open(struct inode *nodp, struct file *filp)
2819 struct binder_proc *proc;
2821 if (binder_debug_mask & BINDER_DEBUG_OPEN_CLOSE)
2822 printk(KERN_INFO "binder_open: %d:%d\n",
2823 current->group_leader->pid, current->pid);
2825 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
2828 get_task_struct(current);
2829 proc->tsk = current;
2830 INIT_LIST_HEAD(&proc->todo);
2831 init_waitqueue_head(&proc->wait);
2832 proc->default_priority = task_nice(current);
2833 mutex_lock(&binder_lock);
2834 binder_stats.obj_created[BINDER_STAT_PROC]++;
2835 hlist_add_head(&proc->proc_node, &binder_procs);
2836 proc->pid = current->group_leader->pid;
2837 INIT_LIST_HEAD(&proc->delivered_death);
2838 filp->private_data = proc;
2839 mutex_unlock(&binder_lock);
2841 if (binder_proc_dir_entry_proc) {
2843 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
2844 remove_proc_entry(strbuf, binder_proc_dir_entry_proc);
2845 create_proc_read_entry(strbuf, S_IRUGO,
2846 binder_proc_dir_entry_proc,
2847 binder_read_proc_proc, proc);
2853 static int binder_flush(struct file *filp, fl_owner_t id)
2855 struct binder_proc *proc = filp->private_data;
2857 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
2862 static void binder_deferred_flush(struct binder_proc *proc)
2866 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
2867 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
2868 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
2869 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
2870 wake_up_interruptible(&thread->wait);
2874 wake_up_interruptible_all(&proc->wait);
2876 if (binder_debug_mask & BINDER_DEBUG_OPEN_CLOSE)
2877 printk(KERN_INFO "binder_flush: %d woke %d threads\n", proc->pid, wake_count);
2880 static int binder_release(struct inode *nodp, struct file *filp)
2882 struct binder_proc *proc = filp->private_data;
2883 if (binder_proc_dir_entry_proc) {
2885 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
2886 remove_proc_entry(strbuf, binder_proc_dir_entry_proc);
2889 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
2894 static void binder_deferred_release(struct binder_proc *proc)
2896 struct hlist_node *pos;
2897 struct binder_transaction *t;
2899 int threads, nodes, incoming_refs, outgoing_refs, buffers, active_transactions, page_count;
2902 BUG_ON(proc->files);
2904 hlist_del(&proc->proc_node);
2905 if (binder_context_mgr_node && binder_context_mgr_node->proc == proc) {
2906 if (binder_debug_mask & BINDER_DEBUG_DEAD_BINDER)
2907 printk(KERN_INFO "binder_release: %d context_mgr_node gone\n", proc->pid);
2908 binder_context_mgr_node = NULL;
2912 active_transactions = 0;
2913 while ((n = rb_first(&proc->threads))) {
2914 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
2916 active_transactions += binder_free_thread(proc, thread);
2920 while ((n = rb_first(&proc->nodes))) {
2921 struct binder_node *node = rb_entry(n, struct binder_node, rb_node);
2924 rb_erase(&node->rb_node, &proc->nodes);
2925 list_del_init(&node->work.entry);
2926 if (hlist_empty(&node->refs)) {
2928 binder_stats.obj_deleted[BINDER_STAT_NODE]++;
2930 struct binder_ref *ref;
2934 node->local_strong_refs = 0;
2935 node->local_weak_refs = 0;
2936 hlist_add_head(&node->dead_node, &binder_dead_nodes);
2938 hlist_for_each_entry(ref, pos, &node->refs, node_entry) {
2942 if (list_empty(&ref->death->work.entry)) {
2943 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
2944 list_add_tail(&ref->death->work.entry, &ref->proc->todo);
2945 wake_up_interruptible(&ref->proc->wait);
2950 if (binder_debug_mask & BINDER_DEBUG_DEAD_BINDER)
2951 printk(KERN_INFO "binder: node %d now dead, "
2952 "refs %d, death %d\n", node->debug_id,
2953 incoming_refs, death);
2957 while ((n = rb_first(&proc->refs_by_desc))) {
2958 struct binder_ref *ref = rb_entry(n, struct binder_ref,
2961 binder_delete_ref(ref);
2963 binder_release_work(&proc->todo);
2966 while ((n = rb_first(&proc->allocated_buffers))) {
2967 struct binder_buffer *buffer = rb_entry(n, struct binder_buffer,
2969 t = buffer->transaction;
2972 buffer->transaction = NULL;
2973 printk(KERN_ERR "binder: release proc %d, "
2974 "transaction %d, not freed\n",
2975 proc->pid, t->debug_id);
2978 binder_free_buf(proc, buffer);
2982 binder_stats.obj_deleted[BINDER_STAT_PROC]++;
2987 for (i = 0; i < proc->buffer_size / PAGE_SIZE; i++) {
2988 if (proc->pages[i]) {
2989 if (binder_debug_mask &
2990 BINDER_DEBUG_BUFFER_ALLOC)
2992 "binder_release: %d: "
2993 "page %d at %p not freed\n",
2995 proc->buffer + i * PAGE_SIZE);
2996 __free_page(proc->pages[i]);
3001 vfree(proc->buffer);
3004 put_task_struct(proc->tsk);
3006 if (binder_debug_mask & BINDER_DEBUG_OPEN_CLOSE)
3008 "binder_release: %d threads %d, nodes %d (ref %d), "
3009 "refs %d, active transactions %d, buffers %d, "
3011 proc->pid, threads, nodes, incoming_refs, outgoing_refs,
3012 active_transactions, buffers, page_count);
3017 static void binder_deferred_func(struct work_struct *work)
3019 struct binder_proc *proc;
3020 struct files_struct *files;
3024 mutex_lock(&binder_lock);
3025 mutex_lock(&binder_deferred_lock);
3026 if (!hlist_empty(&binder_deferred_list)) {
3027 proc = hlist_entry(binder_deferred_list.first,
3028 struct binder_proc, deferred_work_node);
3029 hlist_del_init(&proc->deferred_work_node);
3030 defer = proc->deferred_work;
3031 proc->deferred_work = 0;
3036 mutex_unlock(&binder_deferred_lock);
3039 if (defer & BINDER_DEFERRED_PUT_FILES) {
3040 files = proc->files;
3045 if (defer & BINDER_DEFERRED_FLUSH)
3046 binder_deferred_flush(proc);
3048 if (defer & BINDER_DEFERRED_RELEASE)
3049 binder_deferred_release(proc); /* frees proc */
3051 mutex_unlock(&binder_lock);
3053 put_files_struct(files);
3056 static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
3058 static void binder_defer_work(struct binder_proc *proc, int defer)
3060 mutex_lock(&binder_deferred_lock);
3061 proc->deferred_work |= defer;
3062 if (hlist_unhashed(&proc->deferred_work_node)) {
3063 hlist_add_head(&proc->deferred_work_node,
3064 &binder_deferred_list);
3065 schedule_work(&binder_deferred_work);
3067 mutex_unlock(&binder_deferred_lock);
3070 static char *print_binder_transaction(char *buf, char *end, const char *prefix,
3071 struct binder_transaction *t)
3073 buf += snprintf(buf, end - buf,
3074 "%s %d: %p from %d:%d to %d:%d code %x "
3075 "flags %x pri %ld r%d",
3076 prefix, t->debug_id, t,
3077 t->from ? t->from->proc->pid : 0,
3078 t->from ? t->from->pid : 0,
3079 t->to_proc ? t->to_proc->pid : 0,
3080 t->to_thread ? t->to_thread->pid : 0,
3081 t->code, t->flags, t->priority, t->need_reply);
3084 if (t->buffer == NULL) {
3085 buf += snprintf(buf, end - buf, " buffer free\n");
3088 if (t->buffer->target_node) {
3089 buf += snprintf(buf, end - buf, " node %d",
3090 t->buffer->target_node->debug_id);
3094 buf += snprintf(buf, end - buf, " size %zd:%zd data %p\n",
3095 t->buffer->data_size, t->buffer->offsets_size,
3100 static char *print_binder_buffer(char *buf, char *end, const char *prefix,
3101 struct binder_buffer *buffer)
3103 buf += snprintf(buf, end - buf, "%s %d: %p size %zd:%zd %s\n",
3104 prefix, buffer->debug_id, buffer->data,
3105 buffer->data_size, buffer->offsets_size,
3106 buffer->transaction ? "active" : "delivered");
3110 static char *print_binder_work(char *buf, char *end, const char *prefix,
3111 const char *transaction_prefix,
3112 struct binder_work *w)
3114 struct binder_node *node;
3115 struct binder_transaction *t;
3118 case BINDER_WORK_TRANSACTION:
3119 t = container_of(w, struct binder_transaction, work);
3120 buf = print_binder_transaction(buf, end, transaction_prefix, t);
3122 case BINDER_WORK_TRANSACTION_COMPLETE:
3123 buf += snprintf(buf, end - buf,
3124 "%stransaction complete\n", prefix);
3126 case BINDER_WORK_NODE:
3127 node = container_of(w, struct binder_node, work);
3128 buf += snprintf(buf, end - buf, "%snode work %d: u%p c%p\n",
3129 prefix, node->debug_id, node->ptr,
3132 case BINDER_WORK_DEAD_BINDER:
3133 buf += snprintf(buf, end - buf, "%shas dead binder\n", prefix);
3135 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
3136 buf += snprintf(buf, end - buf,
3137 "%shas cleared dead binder\n", prefix);
3139 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
3140 buf += snprintf(buf, end - buf,
3141 "%shas cleared death notification\n", prefix);
3144 buf += snprintf(buf, end - buf, "%sunknown work: type %d\n",
3151 static char *print_binder_thread(char *buf, char *end,
3152 struct binder_thread *thread,
3155 struct binder_transaction *t;
3156 struct binder_work *w;
3157 char *start_buf = buf;
3160 buf += snprintf(buf, end - buf, " thread %d: l %02x\n",
3161 thread->pid, thread->looper);
3163 t = thread->transaction_stack;
3167 if (t->from == thread) {
3168 buf = print_binder_transaction(buf, end,
3169 " outgoing transaction", t);
3171 } else if (t->to_thread == thread) {
3172 buf = print_binder_transaction(buf, end,
3173 " incoming transaction", t);
3176 buf = print_binder_transaction(buf, end,
3177 " bad transaction", t);
3181 list_for_each_entry(w, &thread->todo, entry) {
3184 buf = print_binder_work(buf, end, " ",
3185 " pending transaction", w);
3187 if (!print_always && buf == header_buf)
3192 static char *print_binder_node(char *buf, char *end, struct binder_node *node)
3194 struct binder_ref *ref;
3195 struct hlist_node *pos;
3196 struct binder_work *w;
3200 hlist_for_each_entry(ref, pos, &node->refs, node_entry)
3203 buf += snprintf(buf, end - buf,
3204 " node %d: u%p c%p hs %d hw %d ls %d lw %d "
3206 node->debug_id, node->ptr, node->cookie,
3207 node->has_strong_ref, node->has_weak_ref,
3208 node->local_strong_refs, node->local_weak_refs,
3209 node->internal_strong_refs, count);
3213 buf += snprintf(buf, end - buf, " proc");
3216 hlist_for_each_entry(ref, pos, &node->refs, node_entry) {
3217 buf += snprintf(buf, end - buf, " %d", ref->proc->pid);
3222 buf += snprintf(buf, end - buf, "\n");
3223 list_for_each_entry(w, &node->async_todo, entry) {
3226 buf = print_binder_work(buf, end, " ",
3227 " pending async transaction", w);
3232 static char *print_binder_ref(char *buf, char *end, struct binder_ref *ref)
3234 buf += snprintf(buf, end - buf,
3235 " ref %d: desc %d %snode %d s %d w %d d %p\n",
3236 ref->debug_id, ref->desc,
3237 ref->node->proc ? "" : "dead ", ref->node->debug_id,
3238 ref->strong, ref->weak, ref->death);
3242 static char *print_binder_proc(char *buf, char *end,
3243 struct binder_proc *proc, int print_all)
3245 struct binder_work *w;
3247 char *start_buf = buf;
3250 buf += snprintf(buf, end - buf, "proc %d\n", proc->pid);
3253 for (n = rb_first(&proc->threads);
3254 n != NULL && buf < end;
3256 buf = print_binder_thread(buf, end,
3257 rb_entry(n, struct binder_thread,
3258 rb_node), print_all);
3259 for (n = rb_first(&proc->nodes);
3260 n != NULL && buf < end;
3262 struct binder_node *node = rb_entry(n, struct binder_node,
3264 if (print_all || node->has_async_transaction)
3265 buf = print_binder_node(buf, end, node);
3268 for (n = rb_first(&proc->refs_by_desc);
3269 n != NULL && buf < end;
3271 buf = print_binder_ref(buf, end,
3272 rb_entry(n, struct binder_ref,
3275 for (n = rb_first(&proc->allocated_buffers);
3276 n != NULL && buf < end;
3278 buf = print_binder_buffer(buf, end, " buffer",
3279 rb_entry(n, struct binder_buffer,
3281 list_for_each_entry(w, &proc->todo, entry) {
3284 buf = print_binder_work(buf, end, " ",
3285 " pending transaction", w);
3287 list_for_each_entry(w, &proc->delivered_death, entry) {
3290 buf += snprintf(buf, end - buf,
3291 " has delivered dead binder\n");
3294 if (!print_all && buf == header_buf)
3299 static const char *binder_return_strings[] = {
3304 "BR_ACQUIRE_RESULT",
3306 "BR_TRANSACTION_COMPLETE",
3311 "BR_ATTEMPT_ACQUIRE",
3316 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
3320 static const char *binder_command_strings[] = {
3323 "BC_ACQUIRE_RESULT",
3331 "BC_ATTEMPT_ACQUIRE",
3332 "BC_REGISTER_LOOPER",
3335 "BC_REQUEST_DEATH_NOTIFICATION",
3336 "BC_CLEAR_DEATH_NOTIFICATION",
3337 "BC_DEAD_BINDER_DONE"
3340 static const char *binder_objstat_strings[] = {
3347 "transaction_complete"
3350 static char *print_binder_stats(char *buf, char *end, const char *prefix,
3351 struct binder_stats *stats)
3355 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
3356 ARRAY_SIZE(binder_command_strings));
3357 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
3359 buf += snprintf(buf, end - buf, "%s%s: %d\n", prefix,
3360 binder_command_strings[i],
3366 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
3367 ARRAY_SIZE(binder_return_strings));
3368 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
3370 buf += snprintf(buf, end - buf, "%s%s: %d\n", prefix,
3371 binder_return_strings[i], stats->br[i]);
3376 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
3377 ARRAY_SIZE(binder_objstat_strings));
3378 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
3379 ARRAY_SIZE(stats->obj_deleted));
3380 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
3381 if (stats->obj_created[i] || stats->obj_deleted[i])
3382 buf += snprintf(buf, end - buf,
3383 "%s%s: active %d total %d\n", prefix,
3384 binder_objstat_strings[i],
3385 stats->obj_created[i] -
3386 stats->obj_deleted[i],
3387 stats->obj_created[i]);
3394 static char *print_binder_proc_stats(char *buf, char *end,
3395 struct binder_proc *proc)
3397 struct binder_work *w;
3399 int count, strong, weak;
3401 buf += snprintf(buf, end - buf, "proc %d\n", proc->pid);
3405 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3407 buf += snprintf(buf, end - buf, " threads: %d\n", count);
3410 buf += snprintf(buf, end - buf, " requested threads: %d+%d/%d\n"
3411 " ready threads %d\n"
3412 " free async space %zd\n", proc->requested_threads,
3413 proc->requested_threads_started, proc->max_threads,
3414 proc->ready_threads, proc->free_async_space);
3418 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
3420 buf += snprintf(buf, end - buf, " nodes: %d\n", count);
3426 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
3427 struct binder_ref *ref = rb_entry(n, struct binder_ref,
3430 strong += ref->strong;
3433 buf += snprintf(buf, end - buf, " refs: %d s %d w %d\n",
3434 count, strong, weak);
3439 for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
3441 buf += snprintf(buf, end - buf, " buffers: %d\n", count);
3446 list_for_each_entry(w, &proc->todo, entry) {
3448 case BINDER_WORK_TRANSACTION:
3455 buf += snprintf(buf, end - buf, " pending transactions: %d\n", count);
3459 buf = print_binder_stats(buf, end, " ", &proc->stats);
3465 static int binder_read_proc_state(char *page, char **start, off_t off,
3466 int count, int *eof, void *data)
3468 struct binder_proc *proc;
3469 struct hlist_node *pos;
3470 struct binder_node *node;
3473 char *end = page + PAGE_SIZE;
3474 int do_lock = !binder_debug_no_lock;
3480 mutex_lock(&binder_lock);
3482 buf += snprintf(buf, end - buf, "binder state:\n");
3484 if (!hlist_empty(&binder_dead_nodes))
3485 buf += snprintf(buf, end - buf, "dead nodes:\n");
3486 hlist_for_each_entry(node, pos, &binder_dead_nodes, dead_node) {
3489 buf = print_binder_node(buf, end, node);
3492 hlist_for_each_entry(proc, pos, &binder_procs, proc_node) {
3495 buf = print_binder_proc(buf, end, proc, 1);
3498 mutex_unlock(&binder_lock);
3499 if (buf > page + PAGE_SIZE)
3500 buf = page + PAGE_SIZE;
3502 *start = page + off;
3510 return len < count ? len : count;
3513 static int binder_read_proc_stats(char *page, char **start, off_t off,
3514 int count, int *eof, void *data)
3516 struct binder_proc *proc;
3517 struct hlist_node *pos;
3520 int do_lock = !binder_debug_no_lock;
3526 mutex_lock(&binder_lock);
3528 p += snprintf(p, PAGE_SIZE, "binder stats:\n");
3530 p = print_binder_stats(p, page + PAGE_SIZE, "", &binder_stats);
3532 hlist_for_each_entry(proc, pos, &binder_procs, proc_node) {
3533 if (p >= page + PAGE_SIZE)
3535 p = print_binder_proc_stats(p, page + PAGE_SIZE, proc);
3538 mutex_unlock(&binder_lock);
3539 if (p > page + PAGE_SIZE)
3540 p = page + PAGE_SIZE;
3542 *start = page + off;
3550 return len < count ? len : count;
3553 static int binder_read_proc_transactions(char *page, char **start, off_t off,
3554 int count, int *eof, void *data)
3556 struct binder_proc *proc;
3557 struct hlist_node *pos;
3560 char *end = page + PAGE_SIZE;
3561 int do_lock = !binder_debug_no_lock;
3567 mutex_lock(&binder_lock);
3569 buf += snprintf(buf, end - buf, "binder transactions:\n");
3570 hlist_for_each_entry(proc, pos, &binder_procs, proc_node) {
3573 buf = print_binder_proc(buf, end, proc, 0);
3576 mutex_unlock(&binder_lock);
3577 if (buf > page + PAGE_SIZE)
3578 buf = page + PAGE_SIZE;
3580 *start = page + off;
3588 return len < count ? len : count;
3591 static int binder_read_proc_proc(char *page, char **start, off_t off,
3592 int count, int *eof, void *data)
3594 struct binder_proc *proc = data;
3597 int do_lock = !binder_debug_no_lock;
3603 mutex_lock(&binder_lock);
3604 p += snprintf(p, PAGE_SIZE, "binder proc state:\n");
3605 p = print_binder_proc(p, page + PAGE_SIZE, proc, 1);
3607 mutex_unlock(&binder_lock);
3609 if (p > page + PAGE_SIZE)
3610 p = page + PAGE_SIZE;
3611 *start = page + off;
3619 return len < count ? len : count;
3622 static char *print_binder_transaction_log_entry(char *buf, char *end,
3623 struct binder_transaction_log_entry *e)
3625 buf += snprintf(buf, end - buf,
3626 "%d: %s from %d:%d to %d:%d node %d handle %d "
3628 e->debug_id, (e->call_type == 2) ? "reply" :
3629 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
3630 e->from_thread, e->to_proc, e->to_thread, e->to_node,
3631 e->target_handle, e->data_size, e->offsets_size);
3635 static int binder_read_proc_transaction_log(
3636 char *page, char **start, off_t off, int count, int *eof, void *data)
3638 struct binder_transaction_log *log = data;
3642 char *end = page + PAGE_SIZE;
3648 for (i = log->next; i < ARRAY_SIZE(log->entry); i++) {
3651 buf = print_binder_transaction_log_entry(buf, end,
3655 for (i = 0; i < log->next; i++) {
3658 buf = print_binder_transaction_log_entry(buf, end,
3662 *start = page + off;
3670 return len < count ? len : count;
3673 static const struct file_operations binder_fops = {
3674 .owner = THIS_MODULE,
3675 .poll = binder_poll,
3676 .unlocked_ioctl = binder_ioctl,
3677 .mmap = binder_mmap,
3678 .open = binder_open,
3679 .flush = binder_flush,
3680 .release = binder_release,
3683 static struct miscdevice binder_miscdev = {
3684 .minor = MISC_DYNAMIC_MINOR,
3686 .fops = &binder_fops
3689 static int __init binder_init(void)
3693 binder_proc_dir_entry_root = proc_mkdir("binder", NULL);
3694 if (binder_proc_dir_entry_root)
3695 binder_proc_dir_entry_proc = proc_mkdir("proc",
3696 binder_proc_dir_entry_root);
3697 ret = misc_register(&binder_miscdev);
3698 if (binder_proc_dir_entry_root) {
3699 create_proc_read_entry("state",
3701 binder_proc_dir_entry_root,
3702 binder_read_proc_state,
3704 create_proc_read_entry("stats",
3706 binder_proc_dir_entry_root,
3707 binder_read_proc_stats,
3709 create_proc_read_entry("transactions",
3711 binder_proc_dir_entry_root,
3712 binder_read_proc_transactions,
3714 create_proc_read_entry("transaction_log",
3716 binder_proc_dir_entry_root,
3717 binder_read_proc_transaction_log,
3718 &binder_transaction_log);
3719 create_proc_read_entry("failed_transaction_log",
3721 binder_proc_dir_entry_root,
3722 binder_read_proc_transaction_log,
3723 &binder_transaction_log_failed);
3728 device_initcall(binder_init);
3730 MODULE_LICENSE("GPL v2");