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(
48 char *page, char **start, off_t off, 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);
84 static int binder_debug_no_lock;
85 module_param_named(proc_no_lock, binder_debug_no_lock, bool, S_IWUSR | S_IRUGO);
86 static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
87 static int binder_stop_on_user_error;
88 static int binder_set_stop_on_user_error(
89 const char *val, struct kernel_param *kp)
92 ret = param_set_int(val, kp);
93 if (binder_stop_on_user_error < 2)
94 wake_up(&binder_user_error_wait);
97 module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
98 param_get_int, &binder_stop_on_user_error, S_IWUSR | S_IRUGO);
100 #define binder_user_error(x...) \
102 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
103 printk(KERN_INFO x); \
104 if (binder_stop_on_user_error) \
105 binder_stop_on_user_error = 2; \
114 BINDER_STAT_TRANSACTION,
115 BINDER_STAT_TRANSACTION_COMPLETE,
119 struct binder_stats {
120 int br[_IOC_NR(BR_FAILED_REPLY) + 1];
121 int bc[_IOC_NR(BC_DEAD_BINDER_DONE) + 1];
122 int obj_created[BINDER_STAT_COUNT];
123 int obj_deleted[BINDER_STAT_COUNT];
126 static struct binder_stats binder_stats;
128 struct binder_transaction_log_entry {
140 struct binder_transaction_log {
143 struct binder_transaction_log_entry entry[32];
145 struct binder_transaction_log binder_transaction_log;
146 struct binder_transaction_log binder_transaction_log_failed;
148 static struct binder_transaction_log_entry *binder_transaction_log_add(
149 struct binder_transaction_log *log)
151 struct binder_transaction_log_entry *e;
152 e = &log->entry[log->next];
153 memset(e, 0, sizeof(*e));
155 if (log->next == ARRAY_SIZE(log->entry)) {
163 struct list_head entry;
165 BINDER_WORK_TRANSACTION = 1,
166 BINDER_WORK_TRANSACTION_COMPLETE,
168 BINDER_WORK_DEAD_BINDER,
169 BINDER_WORK_DEAD_BINDER_AND_CLEAR,
170 BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
176 struct binder_work work;
178 struct rb_node rb_node;
179 struct hlist_node dead_node;
181 struct binder_proc *proc;
182 struct hlist_head refs;
183 int internal_strong_refs;
185 int local_strong_refs;
188 unsigned has_strong_ref : 1;
189 unsigned pending_strong_ref : 1;
190 unsigned has_weak_ref : 1;
191 unsigned pending_weak_ref : 1;
192 unsigned has_async_transaction : 1;
193 unsigned accept_fds : 1;
194 int min_priority : 8;
195 struct list_head async_todo;
198 struct binder_ref_death {
199 struct binder_work work;
204 /* Lookups needed: */
205 /* node + proc => ref (transaction) */
206 /* desc + proc => ref (transaction, inc/dec ref) */
207 /* node => refs + procs (proc exit) */
209 struct rb_node rb_node_desc;
210 struct rb_node rb_node_node;
211 struct hlist_node node_entry;
212 struct binder_proc *proc;
213 struct binder_node *node;
217 struct binder_ref_death *death;
220 struct binder_buffer {
221 struct list_head entry; /* free and allocated entries by addesss */
222 struct rb_node rb_node; /* free entry by size or allocated entry */
225 unsigned allow_user_free : 1;
226 unsigned async_transaction : 1;
227 unsigned debug_id : 29;
229 struct binder_transaction *transaction;
231 struct binder_node *target_node;
238 BINDER_DEFERRED_PUT_FILES = 0x01,
239 BINDER_DEFERRED_FLUSH = 0x02,
240 BINDER_DEFERRED_RELEASE = 0x04,
244 struct hlist_node proc_node;
245 struct rb_root threads;
246 struct rb_root nodes;
247 struct rb_root refs_by_desc;
248 struct rb_root refs_by_node;
250 struct vm_area_struct *vma;
251 struct task_struct *tsk;
252 struct files_struct *files;
253 struct hlist_node deferred_work_node;
256 ptrdiff_t user_buffer_offset;
258 struct list_head buffers;
259 struct rb_root free_buffers;
260 struct rb_root allocated_buffers;
261 size_t free_async_space;
265 uint32_t buffer_free;
266 struct list_head todo;
267 wait_queue_head_t wait;
268 struct binder_stats stats;
269 struct list_head delivered_death;
271 int requested_threads;
272 int requested_threads_started;
274 long default_priority;
278 BINDER_LOOPER_STATE_REGISTERED = 0x01,
279 BINDER_LOOPER_STATE_ENTERED = 0x02,
280 BINDER_LOOPER_STATE_EXITED = 0x04,
281 BINDER_LOOPER_STATE_INVALID = 0x08,
282 BINDER_LOOPER_STATE_WAITING = 0x10,
283 BINDER_LOOPER_STATE_NEED_RETURN = 0x20
286 struct binder_thread {
287 struct binder_proc *proc;
288 struct rb_node rb_node;
291 struct binder_transaction *transaction_stack;
292 struct list_head todo;
293 uint32_t return_error; /* Write failed, return error code in read buf */
294 uint32_t return_error2; /* Write failed, return error code in read */
295 /* buffer. Used when sending a reply to a dead process that */
296 /* we are also waiting on */
297 wait_queue_head_t wait;
298 struct binder_stats stats;
301 struct binder_transaction {
303 struct binder_work work;
304 struct binder_thread *from;
305 struct binder_transaction *from_parent;
306 struct binder_proc *to_proc;
307 struct binder_thread *to_thread;
308 struct binder_transaction *to_parent;
309 unsigned need_reply : 1;
310 /*unsigned is_dead : 1;*/ /* not used at the moment */
312 struct binder_buffer *buffer;
320 static void binder_defer_work(struct binder_proc *proc, int defer);
323 * copied from get_unused_fd_flags
325 int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
327 struct files_struct *files = proc->files;
330 unsigned long rlim_cur;
337 spin_lock(&files->file_lock);
340 fdt = files_fdtable(files);
341 fd = find_next_zero_bit(fdt->open_fds->fds_bits, fdt->max_fds,
345 * N.B. For clone tasks sharing a files structure, this test
346 * will limit the total number of files that can be opened.
349 if (lock_task_sighand(proc->tsk, &irqs)) {
350 rlim_cur = proc->tsk->signal->rlim[RLIMIT_NOFILE].rlim_cur;
351 unlock_task_sighand(proc->tsk, &irqs);
356 /* Do we need to expand the fd array or fd set? */
357 error = expand_files(files, fd);
363 * If we needed to expand the fs array we
364 * might have blocked - try again.
370 FD_SET(fd, fdt->open_fds);
371 if (flags & O_CLOEXEC)
372 FD_SET(fd, fdt->close_on_exec);
374 FD_CLR(fd, fdt->close_on_exec);
375 files->next_fd = fd + 1;
378 if (fdt->fd[fd] != NULL) {
379 printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
386 spin_unlock(&files->file_lock);
391 * copied from fd_install
393 static void task_fd_install(
394 struct binder_proc *proc, unsigned int fd, struct file *file)
396 struct files_struct *files = proc->files;
402 spin_lock(&files->file_lock);
403 fdt = files_fdtable(files);
404 BUG_ON(fdt->fd[fd] != NULL);
405 rcu_assign_pointer(fdt->fd[fd], file);
406 spin_unlock(&files->file_lock);
410 * copied from __put_unused_fd in open.c
412 static void __put_unused_fd(struct files_struct *files, unsigned int fd)
414 struct fdtable *fdt = files_fdtable(files);
415 __FD_CLR(fd, fdt->open_fds);
416 if (fd < files->next_fd)
421 * copied from sys_close
423 static long task_close_fd(struct binder_proc *proc, unsigned int fd)
426 struct files_struct *files = proc->files;
433 spin_lock(&files->file_lock);
434 fdt = files_fdtable(files);
435 if (fd >= fdt->max_fds)
440 rcu_assign_pointer(fdt->fd[fd], NULL);
441 FD_CLR(fd, fdt->close_on_exec);
442 __put_unused_fd(files, fd);
443 spin_unlock(&files->file_lock);
444 retval = filp_close(filp, files);
446 /* can't restart close syscall because file table entry was cleared */
447 if (unlikely(retval == -ERESTARTSYS ||
448 retval == -ERESTARTNOINTR ||
449 retval == -ERESTARTNOHAND ||
450 retval == -ERESTART_RESTARTBLOCK))
456 spin_unlock(&files->file_lock);
460 static void binder_set_nice(long nice)
463 if (can_nice(current, nice)) {
464 set_user_nice(current, nice);
467 min_nice = 20 - current->signal->rlim[RLIMIT_NICE].rlim_cur;
468 if (binder_debug_mask & BINDER_DEBUG_PRIORITY_CAP)
469 printk(KERN_INFO "binder: %d: nice value %ld not allowed use "
470 "%ld instead\n", current->pid, nice, min_nice);
471 set_user_nice(current, min_nice);
474 binder_user_error("binder: %d RLIMIT_NICE not set\n", current->pid);
477 static size_t binder_buffer_size(
478 struct binder_proc *proc, struct binder_buffer *buffer)
480 if (list_is_last(&buffer->entry, &proc->buffers))
481 return proc->buffer + proc->buffer_size - (void *)buffer->data;
483 return (size_t)list_entry(buffer->entry.next,
484 struct binder_buffer, entry) - (size_t)buffer->data;
487 static void binder_insert_free_buffer(
488 struct binder_proc *proc, struct binder_buffer *new_buffer)
490 struct rb_node **p = &proc->free_buffers.rb_node;
491 struct rb_node *parent = NULL;
492 struct binder_buffer *buffer;
494 size_t new_buffer_size;
496 BUG_ON(!new_buffer->free);
498 new_buffer_size = binder_buffer_size(proc, new_buffer);
500 if (binder_debug_mask & BINDER_DEBUG_BUFFER_ALLOC)
501 printk(KERN_INFO "binder: %d: add free buffer, size %zd, "
502 "at %p\n", proc->pid, new_buffer_size, new_buffer);
506 buffer = rb_entry(parent, struct binder_buffer, rb_node);
507 BUG_ON(!buffer->free);
509 buffer_size = binder_buffer_size(proc, buffer);
511 if (new_buffer_size < buffer_size)
512 p = &parent->rb_left;
514 p = &parent->rb_right;
516 rb_link_node(&new_buffer->rb_node, parent, p);
517 rb_insert_color(&new_buffer->rb_node, &proc->free_buffers);
520 static void binder_insert_allocated_buffer(
521 struct binder_proc *proc, struct binder_buffer *new_buffer)
523 struct rb_node **p = &proc->allocated_buffers.rb_node;
524 struct rb_node *parent = NULL;
525 struct binder_buffer *buffer;
527 BUG_ON(new_buffer->free);
531 buffer = rb_entry(parent, struct binder_buffer, rb_node);
532 BUG_ON(buffer->free);
534 if (new_buffer < buffer)
535 p = &parent->rb_left;
536 else if (new_buffer > buffer)
537 p = &parent->rb_right;
541 rb_link_node(&new_buffer->rb_node, parent, p);
542 rb_insert_color(&new_buffer->rb_node, &proc->allocated_buffers);
545 static struct binder_buffer *binder_buffer_lookup(
546 struct binder_proc *proc, void __user *user_ptr)
548 struct rb_node *n = proc->allocated_buffers.rb_node;
549 struct binder_buffer *buffer;
550 struct binder_buffer *kern_ptr;
552 kern_ptr = user_ptr - proc->user_buffer_offset
553 - offsetof(struct binder_buffer, data);
556 buffer = rb_entry(n, struct binder_buffer, rb_node);
557 BUG_ON(buffer->free);
559 if (kern_ptr < buffer)
561 else if (kern_ptr > buffer)
569 static int binder_update_page_range(struct binder_proc *proc, int allocate,
570 void *start, void *end, struct vm_area_struct *vma)
573 unsigned long user_page_addr;
574 struct vm_struct tmp_area;
576 struct mm_struct *mm;
578 if (binder_debug_mask & BINDER_DEBUG_BUFFER_ALLOC)
579 printk(KERN_INFO "binder: %d: %s pages %p-%p\n",
580 proc->pid, allocate ? "allocate" : "free", start, end);
588 mm = get_task_mm(proc->tsk);
591 down_write(&mm->mmap_sem);
599 printk(KERN_ERR "binder: %d: binder_alloc_buf failed to "
600 "map pages in userspace, no vma\n", proc->pid);
604 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
606 struct page **page_array_ptr;
607 page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
610 *page = alloc_page(GFP_KERNEL | __GFP_ZERO);
612 printk(KERN_ERR "binder: %d: binder_alloc_buf failed "
613 "for page at %p\n", proc->pid, page_addr);
614 goto err_alloc_page_failed;
616 tmp_area.addr = page_addr;
617 tmp_area.size = PAGE_SIZE + PAGE_SIZE /* guard page? */;
618 page_array_ptr = page;
619 ret = map_vm_area(&tmp_area, PAGE_KERNEL, &page_array_ptr);
621 printk(KERN_ERR "binder: %d: binder_alloc_buf failed "
622 "to map page at %p in kernel\n",
623 proc->pid, page_addr);
624 goto err_map_kernel_failed;
627 (uintptr_t)page_addr + proc->user_buffer_offset;
628 ret = vm_insert_page(vma, user_page_addr, page[0]);
630 printk(KERN_ERR "binder: %d: binder_alloc_buf failed "
631 "to map page at %lx in userspace\n",
632 proc->pid, user_page_addr);
633 goto err_vm_insert_page_failed;
635 /* vm_insert_page does not seem to increment the refcount */
638 up_write(&mm->mmap_sem);
644 for (page_addr = end - PAGE_SIZE; page_addr >= start;
645 page_addr -= PAGE_SIZE) {
646 page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
648 zap_page_range(vma, (uintptr_t)page_addr +
649 proc->user_buffer_offset, PAGE_SIZE, NULL);
650 err_vm_insert_page_failed:
651 unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
652 err_map_kernel_failed:
655 err_alloc_page_failed:
660 up_write(&mm->mmap_sem);
666 static struct binder_buffer *binder_alloc_buf(struct binder_proc *proc,
667 size_t data_size, size_t offsets_size, int is_async)
669 struct rb_node *n = proc->free_buffers.rb_node;
670 struct binder_buffer *buffer;
672 struct rb_node *best_fit = NULL;
677 if (proc->vma == NULL) {
678 printk(KERN_ERR "binder: %d: binder_alloc_buf, no vma\n",
683 size = ALIGN(data_size, sizeof(void *)) +
684 ALIGN(offsets_size, sizeof(void *));
686 if (size < data_size || size < offsets_size) {
687 binder_user_error("binder: %d: got transaction with invalid "
688 "size %zd-%zd\n", proc->pid, data_size, offsets_size);
693 proc->free_async_space < size + sizeof(struct binder_buffer)) {
694 if (binder_debug_mask & BINDER_DEBUG_BUFFER_ALLOC)
695 printk(KERN_ERR "binder: %d: binder_alloc_buf size %zd f"
696 "ailed, no async space left\n", proc->pid, size);
701 buffer = rb_entry(n, struct binder_buffer, rb_node);
702 BUG_ON(!buffer->free);
703 buffer_size = binder_buffer_size(proc, buffer);
705 if (size < buffer_size) {
708 } else if (size > buffer_size)
715 if (best_fit == NULL) {
716 printk(KERN_ERR "binder: %d: binder_alloc_buf size %zd failed, "
717 "no address space\n", proc->pid, size);
721 buffer = rb_entry(best_fit, struct binder_buffer, rb_node);
722 buffer_size = binder_buffer_size(proc, buffer);
724 if (binder_debug_mask & BINDER_DEBUG_BUFFER_ALLOC)
725 printk(KERN_INFO "binder: %d: binder_alloc_buf size %zd got buff"
726 "er %p size %zd\n", proc->pid, size, buffer, buffer_size);
729 (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK);
731 if (size + sizeof(struct binder_buffer) + 4 >= buffer_size)
732 buffer_size = size; /* no room for other buffers */
734 buffer_size = size + sizeof(struct binder_buffer);
737 (void *)PAGE_ALIGN((uintptr_t)buffer->data + buffer_size);
738 if (end_page_addr > has_page_addr)
739 end_page_addr = has_page_addr;
740 if (binder_update_page_range(proc, 1,
741 (void *)PAGE_ALIGN((uintptr_t)buffer->data), end_page_addr, NULL))
744 rb_erase(best_fit, &proc->free_buffers);
746 binder_insert_allocated_buffer(proc, buffer);
747 if (buffer_size != size) {
748 struct binder_buffer *new_buffer = (void *)buffer->data + size;
749 list_add(&new_buffer->entry, &buffer->entry);
750 new_buffer->free = 1;
751 binder_insert_free_buffer(proc, new_buffer);
753 if (binder_debug_mask & BINDER_DEBUG_BUFFER_ALLOC)
754 printk(KERN_INFO "binder: %d: binder_alloc_buf size %zd got "
755 "%p\n", proc->pid, size, buffer);
756 buffer->data_size = data_size;
757 buffer->offsets_size = offsets_size;
758 buffer->async_transaction = is_async;
760 proc->free_async_space -= size + sizeof(struct binder_buffer);
761 if (binder_debug_mask & BINDER_DEBUG_BUFFER_ALLOC_ASYNC)
762 printk(KERN_INFO "binder: %d: binder_alloc_buf size %zd "
763 "async free %zd\n", proc->pid, size,
764 proc->free_async_space);
770 static void *buffer_start_page(struct binder_buffer *buffer)
772 return (void *)((uintptr_t)buffer & PAGE_MASK);
775 static void *buffer_end_page(struct binder_buffer *buffer)
777 return (void *)(((uintptr_t)(buffer + 1) - 1) & PAGE_MASK);
780 static void binder_delete_free_buffer(
781 struct binder_proc *proc, struct binder_buffer *buffer)
783 struct binder_buffer *prev, *next = NULL;
784 int free_page_end = 1;
785 int free_page_start = 1;
787 BUG_ON(proc->buffers.next == &buffer->entry);
788 prev = list_entry(buffer->entry.prev, struct binder_buffer, entry);
790 if (buffer_end_page(prev) == buffer_start_page(buffer)) {
792 if (buffer_end_page(prev) == buffer_end_page(buffer))
794 if (binder_debug_mask & BINDER_DEBUG_BUFFER_ALLOC)
795 printk(KERN_INFO "binder: %d: merge free, buffer %p "
796 "share page with %p\n", proc->pid, buffer, prev);
799 if (!list_is_last(&buffer->entry, &proc->buffers)) {
800 next = list_entry(buffer->entry.next,
801 struct binder_buffer, entry);
802 if (buffer_start_page(next) == buffer_end_page(buffer)) {
804 if (buffer_start_page(next) ==
805 buffer_start_page(buffer))
807 if (binder_debug_mask & BINDER_DEBUG_BUFFER_ALLOC)
808 printk(KERN_INFO "binder: %d: merge free, "
809 "buffer %p share page with %p\n",
810 proc->pid, buffer, prev);
813 list_del(&buffer->entry);
814 if (free_page_start || free_page_end) {
815 if (binder_debug_mask & BINDER_DEBUG_BUFFER_ALLOC)
816 printk(KERN_INFO "binder: %d: merge free, buffer %p do "
817 "not share page%s%s with with %p or %p\n",
818 proc->pid, buffer, free_page_start ? "" : " end",
819 free_page_end ? "" : " start", prev, next);
820 binder_update_page_range(proc, 0, free_page_start ?
821 buffer_start_page(buffer) : buffer_end_page(buffer),
822 (free_page_end ? buffer_end_page(buffer) :
823 buffer_start_page(buffer)) + PAGE_SIZE, NULL);
827 static void binder_free_buf(
828 struct binder_proc *proc, struct binder_buffer *buffer)
830 size_t size, buffer_size;
832 buffer_size = binder_buffer_size(proc, buffer);
834 size = ALIGN(buffer->data_size, sizeof(void *)) +
835 ALIGN(buffer->offsets_size, sizeof(void *));
836 if (binder_debug_mask & BINDER_DEBUG_BUFFER_ALLOC)
837 printk(KERN_INFO "binder: %d: binder_free_buf %p size %zd buffer"
838 "_size %zd\n", proc->pid, buffer, size, buffer_size);
840 BUG_ON(buffer->free);
841 BUG_ON(size > buffer_size);
842 BUG_ON(buffer->transaction != NULL);
843 BUG_ON((void *)buffer < proc->buffer);
844 BUG_ON((void *)buffer > proc->buffer + proc->buffer_size);
846 if (buffer->async_transaction) {
847 proc->free_async_space += size + sizeof(struct binder_buffer);
848 if (binder_debug_mask & BINDER_DEBUG_BUFFER_ALLOC_ASYNC)
849 printk(KERN_INFO "binder: %d: binder_free_buf size %zd "
850 "async free %zd\n", proc->pid, size,
851 proc->free_async_space);
854 binder_update_page_range(proc, 0,
855 (void *)PAGE_ALIGN((uintptr_t)buffer->data),
856 (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK),
858 rb_erase(&buffer->rb_node, &proc->allocated_buffers);
860 if (!list_is_last(&buffer->entry, &proc->buffers)) {
861 struct binder_buffer *next = list_entry(buffer->entry.next,
862 struct binder_buffer, entry);
864 rb_erase(&next->rb_node, &proc->free_buffers);
865 binder_delete_free_buffer(proc, next);
868 if (proc->buffers.next != &buffer->entry) {
869 struct binder_buffer *prev = list_entry(buffer->entry.prev,
870 struct binder_buffer, entry);
872 binder_delete_free_buffer(proc, buffer);
873 rb_erase(&prev->rb_node, &proc->free_buffers);
877 binder_insert_free_buffer(proc, buffer);
880 static struct binder_node *
881 binder_get_node(struct binder_proc *proc, void __user *ptr)
883 struct rb_node *n = proc->nodes.rb_node;
884 struct binder_node *node;
887 node = rb_entry(n, struct binder_node, rb_node);
891 else if (ptr > node->ptr)
899 static struct binder_node *
900 binder_new_node(struct binder_proc *proc, void __user *ptr, void __user *cookie)
902 struct rb_node **p = &proc->nodes.rb_node;
903 struct rb_node *parent = NULL;
904 struct binder_node *node;
908 node = rb_entry(parent, struct binder_node, rb_node);
912 else if (ptr > node->ptr)
918 node = kzalloc(sizeof(*node), GFP_KERNEL);
921 binder_stats.obj_created[BINDER_STAT_NODE]++;
922 rb_link_node(&node->rb_node, parent, p);
923 rb_insert_color(&node->rb_node, &proc->nodes);
924 node->debug_id = ++binder_last_id;
927 node->cookie = cookie;
928 node->work.type = BINDER_WORK_NODE;
929 INIT_LIST_HEAD(&node->work.entry);
930 INIT_LIST_HEAD(&node->async_todo);
931 if (binder_debug_mask & BINDER_DEBUG_INTERNAL_REFS)
932 printk(KERN_INFO "binder: %d:%d node %d u%p c%p created\n",
933 proc->pid, current->pid, node->debug_id,
934 node->ptr, node->cookie);
939 binder_inc_node(struct binder_node *node, int strong, int internal,
940 struct list_head *target_list)
944 if (target_list == NULL &&
945 node->internal_strong_refs == 0 &&
946 !(node == binder_context_mgr_node &&
947 node->has_strong_ref)) {
948 printk(KERN_ERR "binder: invalid inc strong "
949 "node for %d\n", node->debug_id);
952 node->internal_strong_refs++;
954 node->local_strong_refs++;
955 if (!node->has_strong_ref && target_list) {
956 list_del_init(&node->work.entry);
957 list_add_tail(&node->work.entry, target_list);
961 node->local_weak_refs++;
962 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
963 if (target_list == NULL) {
964 printk(KERN_ERR "binder: invalid inc weak node "
965 "for %d\n", node->debug_id);
968 list_add_tail(&node->work.entry, target_list);
975 binder_dec_node(struct binder_node *node, int strong, int internal)
979 node->internal_strong_refs--;
981 node->local_strong_refs--;
982 if (node->local_strong_refs || node->internal_strong_refs)
986 node->local_weak_refs--;
987 if (node->local_weak_refs || !hlist_empty(&node->refs))
990 if (node->proc && (node->has_strong_ref || node->has_weak_ref)) {
991 if (list_empty(&node->work.entry)) {
992 list_add_tail(&node->work.entry, &node->proc->todo);
993 wake_up_interruptible(&node->proc->wait);
996 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
997 !node->local_weak_refs) {
998 list_del_init(&node->work.entry);
1000 rb_erase(&node->rb_node, &node->proc->nodes);
1001 if (binder_debug_mask & BINDER_DEBUG_INTERNAL_REFS)
1002 printk(KERN_INFO "binder: refless node %d deleted\n", node->debug_id);
1004 hlist_del(&node->dead_node);
1005 if (binder_debug_mask & BINDER_DEBUG_INTERNAL_REFS)
1006 printk(KERN_INFO "binder: dead node %d deleted\n", node->debug_id);
1009 binder_stats.obj_deleted[BINDER_STAT_NODE]++;
1017 static struct binder_ref *
1018 binder_get_ref(struct binder_proc *proc, uint32_t desc)
1020 struct rb_node *n = proc->refs_by_desc.rb_node;
1021 struct binder_ref *ref;
1024 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1026 if (desc < ref->desc)
1028 else if (desc > ref->desc)
1036 static struct binder_ref *
1037 binder_get_ref_for_node(struct binder_proc *proc, struct binder_node *node)
1040 struct rb_node **p = &proc->refs_by_node.rb_node;
1041 struct rb_node *parent = NULL;
1042 struct binder_ref *ref, *new_ref;
1046 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1048 if (node < ref->node)
1050 else if (node > ref->node)
1051 p = &(*p)->rb_right;
1055 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1056 if (new_ref == NULL)
1058 binder_stats.obj_created[BINDER_STAT_REF]++;
1059 new_ref->debug_id = ++binder_last_id;
1060 new_ref->proc = proc;
1061 new_ref->node = node;
1062 rb_link_node(&new_ref->rb_node_node, parent, p);
1063 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1065 new_ref->desc = (node == binder_context_mgr_node) ? 0 : 1;
1066 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1067 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1068 if (ref->desc > new_ref->desc)
1070 new_ref->desc = ref->desc + 1;
1073 p = &proc->refs_by_desc.rb_node;
1076 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1078 if (new_ref->desc < ref->desc)
1080 else if (new_ref->desc > ref->desc)
1081 p = &(*p)->rb_right;
1085 rb_link_node(&new_ref->rb_node_desc, parent, p);
1086 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
1088 hlist_add_head(&new_ref->node_entry, &node->refs);
1089 if (binder_debug_mask & BINDER_DEBUG_INTERNAL_REFS)
1090 printk(KERN_INFO "binder: %d new ref %d desc %d for "
1091 "node %d\n", proc->pid, new_ref->debug_id,
1092 new_ref->desc, node->debug_id);
1094 if (binder_debug_mask & BINDER_DEBUG_INTERNAL_REFS)
1095 printk(KERN_INFO "binder: %d new ref %d desc %d for "
1096 "dead node\n", proc->pid, new_ref->debug_id,
1103 binder_delete_ref(struct binder_ref *ref)
1105 if (binder_debug_mask & BINDER_DEBUG_INTERNAL_REFS)
1106 printk(KERN_INFO "binder: %d delete ref %d desc %d for "
1107 "node %d\n", ref->proc->pid, ref->debug_id,
1108 ref->desc, ref->node->debug_id);
1109 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1110 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
1112 binder_dec_node(ref->node, 1, 1);
1113 hlist_del(&ref->node_entry);
1114 binder_dec_node(ref->node, 0, 1);
1116 if (binder_debug_mask & BINDER_DEBUG_DEAD_BINDER)
1117 printk(KERN_INFO "binder: %d delete ref %d desc %d "
1118 "has death notification\n", ref->proc->pid,
1119 ref->debug_id, ref->desc);
1120 list_del(&ref->death->work.entry);
1122 binder_stats.obj_deleted[BINDER_STAT_DEATH]++;
1125 binder_stats.obj_deleted[BINDER_STAT_REF]++;
1130 struct binder_ref *ref, int strong, struct list_head *target_list)
1134 if (ref->strong == 0) {
1135 ret = binder_inc_node(ref->node, 1, 1, target_list);
1141 if (ref->weak == 0) {
1142 ret = binder_inc_node(ref->node, 0, 1, target_list);
1153 binder_dec_ref(struct binder_ref *ref, int strong)
1156 if (ref->strong == 0) {
1157 binder_user_error("binder: %d invalid dec strong, "
1158 "ref %d desc %d s %d w %d\n",
1159 ref->proc->pid, ref->debug_id,
1160 ref->desc, ref->strong, ref->weak);
1164 if (ref->strong == 0) {
1166 ret = binder_dec_node(ref->node, strong, 1);
1171 if (ref->weak == 0) {
1172 binder_user_error("binder: %d invalid dec weak, "
1173 "ref %d desc %d s %d w %d\n",
1174 ref->proc->pid, ref->debug_id,
1175 ref->desc, ref->strong, ref->weak);
1180 if (ref->strong == 0 && ref->weak == 0)
1181 binder_delete_ref(ref);
1186 binder_pop_transaction(
1187 struct binder_thread *target_thread, struct binder_transaction *t)
1189 if (target_thread) {
1190 BUG_ON(target_thread->transaction_stack != t);
1191 BUG_ON(target_thread->transaction_stack->from != target_thread);
1192 target_thread->transaction_stack =
1193 target_thread->transaction_stack->from_parent;
1198 t->buffer->transaction = NULL;
1200 binder_stats.obj_deleted[BINDER_STAT_TRANSACTION]++;
1204 binder_send_failed_reply(struct binder_transaction *t, uint32_t error_code)
1206 struct binder_thread *target_thread;
1207 BUG_ON(t->flags & TF_ONE_WAY);
1209 target_thread = t->from;
1210 if (target_thread) {
1211 if (target_thread->return_error != BR_OK &&
1212 target_thread->return_error2 == BR_OK) {
1213 target_thread->return_error2 =
1214 target_thread->return_error;
1215 target_thread->return_error = BR_OK;
1217 if (target_thread->return_error == BR_OK) {
1218 if (binder_debug_mask & BINDER_DEBUG_FAILED_TRANSACTION)
1219 printk(KERN_INFO "binder: send failed reply for transaction %d to %d:%d\n",
1220 t->debug_id, target_thread->proc->pid, target_thread->pid);
1222 binder_pop_transaction(target_thread, t);
1223 target_thread->return_error = error_code;
1224 wake_up_interruptible(&target_thread->wait);
1226 printk(KERN_ERR "binder: reply failed, target "
1227 "thread, %d:%d, has error code %d "
1228 "already\n", target_thread->proc->pid,
1230 target_thread->return_error);
1234 struct binder_transaction *next = t->from_parent;
1236 if (binder_debug_mask & BINDER_DEBUG_FAILED_TRANSACTION)
1237 printk(KERN_INFO "binder: send failed reply "
1238 "for transaction %d, target dead\n",
1241 binder_pop_transaction(target_thread, t);
1243 if (binder_debug_mask & BINDER_DEBUG_DEAD_BINDER)
1244 printk(KERN_INFO "binder: reply failed,"
1245 " no target thread at root\n");
1249 if (binder_debug_mask & BINDER_DEBUG_DEAD_BINDER)
1250 printk(KERN_INFO "binder: reply failed, no targ"
1251 "et thread -- retry %d\n", t->debug_id);
1257 binder_transaction_buffer_release(struct binder_proc *proc,
1258 struct binder_buffer *buffer, size_t *failed_at);
1261 binder_transaction(struct binder_proc *proc, struct binder_thread *thread,
1262 struct binder_transaction_data *tr, int reply)
1264 struct binder_transaction *t;
1265 struct binder_work *tcomplete;
1266 size_t *offp, *off_end;
1267 struct binder_proc *target_proc;
1268 struct binder_thread *target_thread = NULL;
1269 struct binder_node *target_node = NULL;
1270 struct list_head *target_list;
1271 wait_queue_head_t *target_wait;
1272 struct binder_transaction *in_reply_to = NULL;
1273 struct binder_transaction_log_entry *e;
1274 uint32_t return_error;
1276 e = binder_transaction_log_add(&binder_transaction_log);
1277 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
1278 e->from_proc = proc->pid;
1279 e->from_thread = thread->pid;
1280 e->target_handle = tr->target.handle;
1281 e->data_size = tr->data_size;
1282 e->offsets_size = tr->offsets_size;
1285 in_reply_to = thread->transaction_stack;
1286 if (in_reply_to == NULL) {
1287 binder_user_error("binder: %d:%d got reply transaction "
1288 "with no transaction stack\n",
1289 proc->pid, thread->pid);
1290 return_error = BR_FAILED_REPLY;
1291 goto err_empty_call_stack;
1293 binder_set_nice(in_reply_to->saved_priority);
1294 if (in_reply_to->to_thread != thread) {
1295 binder_user_error("binder: %d:%d got reply transaction "
1296 "with bad transaction stack,"
1297 " transaction %d has target %d:%d\n",
1298 proc->pid, thread->pid, in_reply_to->debug_id,
1299 in_reply_to->to_proc ?
1300 in_reply_to->to_proc->pid : 0,
1301 in_reply_to->to_thread ?
1302 in_reply_to->to_thread->pid : 0);
1303 return_error = BR_FAILED_REPLY;
1305 goto err_bad_call_stack;
1307 thread->transaction_stack = in_reply_to->to_parent;
1308 target_thread = in_reply_to->from;
1309 if (target_thread == NULL) {
1310 return_error = BR_DEAD_REPLY;
1311 goto err_dead_binder;
1313 if (target_thread->transaction_stack != in_reply_to) {
1314 binder_user_error("binder: %d:%d got reply transaction "
1315 "with bad target transaction stack %d, "
1317 proc->pid, thread->pid,
1318 target_thread->transaction_stack ?
1319 target_thread->transaction_stack->debug_id : 0,
1320 in_reply_to->debug_id);
1321 return_error = BR_FAILED_REPLY;
1323 target_thread = NULL;
1324 goto err_dead_binder;
1326 target_proc = target_thread->proc;
1328 if (tr->target.handle) {
1329 struct binder_ref *ref;
1330 ref = binder_get_ref(proc, tr->target.handle);
1332 binder_user_error("binder: %d:%d got "
1333 "transaction to invalid handle\n",
1334 proc->pid, thread->pid);
1335 return_error = BR_FAILED_REPLY;
1336 goto err_invalid_target_handle;
1338 target_node = ref->node;
1340 target_node = binder_context_mgr_node;
1341 if (target_node == NULL) {
1342 return_error = BR_DEAD_REPLY;
1343 goto err_no_context_mgr_node;
1346 e->to_node = target_node->debug_id;
1347 target_proc = target_node->proc;
1348 if (target_proc == NULL) {
1349 return_error = BR_DEAD_REPLY;
1350 goto err_dead_binder;
1352 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
1353 struct binder_transaction *tmp;
1354 tmp = thread->transaction_stack;
1355 if (tmp->to_thread != thread) {
1356 binder_user_error("binder: %d:%d got new "
1357 "transaction with bad transaction stack"
1358 ", transaction %d has target %d:%d\n",
1359 proc->pid, thread->pid, tmp->debug_id,
1360 tmp->to_proc ? tmp->to_proc->pid : 0,
1362 tmp->to_thread->pid : 0);
1363 return_error = BR_FAILED_REPLY;
1364 goto err_bad_call_stack;
1367 if (tmp->from && tmp->from->proc == target_proc)
1368 target_thread = tmp->from;
1369 tmp = tmp->from_parent;
1373 if (target_thread) {
1374 e->to_thread = target_thread->pid;
1375 target_list = &target_thread->todo;
1376 target_wait = &target_thread->wait;
1378 target_list = &target_proc->todo;
1379 target_wait = &target_proc->wait;
1381 e->to_proc = target_proc->pid;
1383 /* TODO: reuse incoming transaction for reply */
1384 t = kzalloc(sizeof(*t), GFP_KERNEL);
1386 return_error = BR_FAILED_REPLY;
1387 goto err_alloc_t_failed;
1389 binder_stats.obj_created[BINDER_STAT_TRANSACTION]++;
1391 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
1392 if (tcomplete == NULL) {
1393 return_error = BR_FAILED_REPLY;
1394 goto err_alloc_tcomplete_failed;
1396 binder_stats.obj_created[BINDER_STAT_TRANSACTION_COMPLETE]++;
1398 t->debug_id = ++binder_last_id;
1399 e->debug_id = t->debug_id;
1401 if (binder_debug_mask & BINDER_DEBUG_TRANSACTION) {
1403 printk(KERN_INFO "binder: %d:%d BC_REPLY %d -> %d:%d, "
1404 "data %p-%p size %zd-%zd\n",
1405 proc->pid, thread->pid, t->debug_id,
1406 target_proc->pid, target_thread->pid,
1407 tr->data.ptr.buffer, tr->data.ptr.offsets,
1408 tr->data_size, tr->offsets_size);
1410 printk(KERN_INFO "binder: %d:%d BC_TRANSACTION %d -> "
1411 "%d - node %d, data %p-%p size %zd-%zd\n",
1412 proc->pid, thread->pid, t->debug_id,
1413 target_proc->pid, target_node->debug_id,
1414 tr->data.ptr.buffer, tr->data.ptr.offsets,
1415 tr->data_size, tr->offsets_size);
1418 if (!reply && !(tr->flags & TF_ONE_WAY))
1422 t->sender_euid = proc->tsk->cred->euid;
1423 t->to_proc = target_proc;
1424 t->to_thread = target_thread;
1426 t->flags = tr->flags;
1427 t->priority = task_nice(current);
1428 t->buffer = binder_alloc_buf(target_proc, tr->data_size,
1429 tr->offsets_size, !reply && (t->flags & TF_ONE_WAY));
1430 if (t->buffer == NULL) {
1431 return_error = BR_FAILED_REPLY;
1432 goto err_binder_alloc_buf_failed;
1434 t->buffer->allow_user_free = 0;
1435 t->buffer->debug_id = t->debug_id;
1436 t->buffer->transaction = t;
1437 t->buffer->target_node = target_node;
1439 binder_inc_node(target_node, 1, 0, NULL);
1441 offp = (size_t *)(t->buffer->data + ALIGN(tr->data_size, sizeof(void *)));
1443 if (copy_from_user(t->buffer->data, tr->data.ptr.buffer, tr->data_size)) {
1444 binder_user_error("binder: %d:%d got transaction with invalid "
1445 "data ptr\n", proc->pid, thread->pid);
1446 return_error = BR_FAILED_REPLY;
1447 goto err_copy_data_failed;
1449 if (copy_from_user(offp, tr->data.ptr.offsets, tr->offsets_size)) {
1450 binder_user_error("binder: %d:%d got transaction with invalid "
1451 "offsets ptr\n", proc->pid, thread->pid);
1452 return_error = BR_FAILED_REPLY;
1453 goto err_copy_data_failed;
1455 if (!IS_ALIGNED(tr->offsets_size, sizeof(size_t))) {
1456 binder_user_error("binder: %d:%d got transaction with "
1457 "invalid offsets size, %zd\n",
1458 proc->pid, thread->pid, tr->offsets_size);
1459 return_error = BR_FAILED_REPLY;
1460 goto err_bad_offset;
1462 off_end = (void *)offp + tr->offsets_size;
1463 for (; offp < off_end; offp++) {
1464 struct flat_binder_object *fp;
1465 if (*offp > t->buffer->data_size - sizeof(*fp) ||
1466 t->buffer->data_size < sizeof(*fp) ||
1467 !IS_ALIGNED(*offp, sizeof(void *))) {
1468 binder_user_error("binder: %d:%d got transaction with "
1469 "invalid offset, %zd\n",
1470 proc->pid, thread->pid, *offp);
1471 return_error = BR_FAILED_REPLY;
1472 goto err_bad_offset;
1474 fp = (struct flat_binder_object *)(t->buffer->data + *offp);
1476 case BINDER_TYPE_BINDER:
1477 case BINDER_TYPE_WEAK_BINDER: {
1478 struct binder_ref *ref;
1479 struct binder_node *node = binder_get_node(proc, fp->binder);
1481 node = binder_new_node(proc, fp->binder, fp->cookie);
1483 return_error = BR_FAILED_REPLY;
1484 goto err_binder_new_node_failed;
1486 node->min_priority = fp->flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
1487 node->accept_fds = !!(fp->flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
1489 if (fp->cookie != node->cookie) {
1490 binder_user_error("binder: %d:%d sending u%p "
1491 "node %d, cookie mismatch %p != %p\n",
1492 proc->pid, thread->pid,
1493 fp->binder, node->debug_id,
1494 fp->cookie, node->cookie);
1495 goto err_binder_get_ref_for_node_failed;
1497 ref = binder_get_ref_for_node(target_proc, node);
1499 return_error = BR_FAILED_REPLY;
1500 goto err_binder_get_ref_for_node_failed;
1502 if (fp->type == BINDER_TYPE_BINDER)
1503 fp->type = BINDER_TYPE_HANDLE;
1505 fp->type = BINDER_TYPE_WEAK_HANDLE;
1506 fp->handle = ref->desc;
1507 binder_inc_ref(ref, fp->type == BINDER_TYPE_HANDLE, &thread->todo);
1508 if (binder_debug_mask & BINDER_DEBUG_TRANSACTION)
1509 printk(KERN_INFO " node %d u%p -> ref %d desc %d\n",
1510 node->debug_id, node->ptr, ref->debug_id, ref->desc);
1512 case BINDER_TYPE_HANDLE:
1513 case BINDER_TYPE_WEAK_HANDLE: {
1514 struct binder_ref *ref = binder_get_ref(proc, fp->handle);
1516 binder_user_error("binder: %d:%d got "
1517 "transaction with invalid "
1518 "handle, %ld\n", proc->pid,
1519 thread->pid, fp->handle);
1520 return_error = BR_FAILED_REPLY;
1521 goto err_binder_get_ref_failed;
1523 if (ref->node->proc == target_proc) {
1524 if (fp->type == BINDER_TYPE_HANDLE)
1525 fp->type = BINDER_TYPE_BINDER;
1527 fp->type = BINDER_TYPE_WEAK_BINDER;
1528 fp->binder = ref->node->ptr;
1529 fp->cookie = ref->node->cookie;
1530 binder_inc_node(ref->node, fp->type == BINDER_TYPE_BINDER, 0, NULL);
1531 if (binder_debug_mask & BINDER_DEBUG_TRANSACTION)
1532 printk(KERN_INFO " ref %d desc %d -> node %d u%p\n",
1533 ref->debug_id, ref->desc, ref->node->debug_id, ref->node->ptr);
1535 struct binder_ref *new_ref;
1536 new_ref = binder_get_ref_for_node(target_proc, ref->node);
1537 if (new_ref == NULL) {
1538 return_error = BR_FAILED_REPLY;
1539 goto err_binder_get_ref_for_node_failed;
1541 fp->handle = new_ref->desc;
1542 binder_inc_ref(new_ref, fp->type == BINDER_TYPE_HANDLE, NULL);
1543 if (binder_debug_mask & BINDER_DEBUG_TRANSACTION)
1544 printk(KERN_INFO " ref %d desc %d -> ref %d desc %d (node %d)\n",
1545 ref->debug_id, ref->desc, new_ref->debug_id, new_ref->desc, ref->node->debug_id);
1549 case BINDER_TYPE_FD: {
1554 if (!(in_reply_to->flags & TF_ACCEPT_FDS)) {
1555 binder_user_error("binder: %d:%d got reply with fd, %ld, but target does not allow fds\n",
1556 proc->pid, thread->pid, fp->handle);
1557 return_error = BR_FAILED_REPLY;
1558 goto err_fd_not_allowed;
1560 } else if (!target_node->accept_fds) {
1561 binder_user_error("binder: %d:%d got transaction with fd, %ld, but target does not allow fds\n",
1562 proc->pid, thread->pid, fp->handle);
1563 return_error = BR_FAILED_REPLY;
1564 goto err_fd_not_allowed;
1567 file = fget(fp->handle);
1569 binder_user_error("binder: %d:%d got transaction with invalid fd, %ld\n",
1570 proc->pid, thread->pid, fp->handle);
1571 return_error = BR_FAILED_REPLY;
1572 goto err_fget_failed;
1574 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
1575 if (target_fd < 0) {
1577 return_error = BR_FAILED_REPLY;
1578 goto err_get_unused_fd_failed;
1580 task_fd_install(target_proc, target_fd, file);
1581 if (binder_debug_mask & BINDER_DEBUG_TRANSACTION)
1582 printk(KERN_INFO " fd %ld -> %d\n", fp->handle, target_fd);
1584 fp->handle = target_fd;
1588 binder_user_error("binder: %d:%d got transactio"
1589 "n with invalid object type, %lx\n",
1590 proc->pid, thread->pid, fp->type);
1591 return_error = BR_FAILED_REPLY;
1592 goto err_bad_object_type;
1596 BUG_ON(t->buffer->async_transaction != 0);
1597 binder_pop_transaction(target_thread, in_reply_to);
1598 } else if (!(t->flags & TF_ONE_WAY)) {
1599 BUG_ON(t->buffer->async_transaction != 0);
1601 t->from_parent = thread->transaction_stack;
1602 thread->transaction_stack = t;
1604 BUG_ON(target_node == NULL);
1605 BUG_ON(t->buffer->async_transaction != 1);
1606 if (target_node->has_async_transaction) {
1607 target_list = &target_node->async_todo;
1610 target_node->has_async_transaction = 1;
1612 t->work.type = BINDER_WORK_TRANSACTION;
1613 list_add_tail(&t->work.entry, target_list);
1614 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
1615 list_add_tail(&tcomplete->entry, &thread->todo);
1617 wake_up_interruptible(target_wait);
1620 err_get_unused_fd_failed:
1623 err_binder_get_ref_for_node_failed:
1624 err_binder_get_ref_failed:
1625 err_binder_new_node_failed:
1626 err_bad_object_type:
1628 err_copy_data_failed:
1629 binder_transaction_buffer_release(target_proc, t->buffer, offp);
1630 t->buffer->transaction = NULL;
1631 binder_free_buf(target_proc, t->buffer);
1632 err_binder_alloc_buf_failed:
1634 binder_stats.obj_deleted[BINDER_STAT_TRANSACTION_COMPLETE]++;
1635 err_alloc_tcomplete_failed:
1637 binder_stats.obj_deleted[BINDER_STAT_TRANSACTION]++;
1640 err_empty_call_stack:
1642 err_invalid_target_handle:
1643 err_no_context_mgr_node:
1644 if (binder_debug_mask & BINDER_DEBUG_FAILED_TRANSACTION)
1645 printk(KERN_INFO "binder: %d:%d transaction failed %d, size"
1647 proc->pid, thread->pid, return_error,
1648 tr->data_size, tr->offsets_size);
1651 struct binder_transaction_log_entry *fe;
1652 fe = binder_transaction_log_add(&binder_transaction_log_failed);
1656 BUG_ON(thread->return_error != BR_OK);
1658 thread->return_error = BR_TRANSACTION_COMPLETE;
1659 binder_send_failed_reply(in_reply_to, return_error);
1661 thread->return_error = return_error;
1665 binder_transaction_buffer_release(struct binder_proc *proc, struct binder_buffer *buffer, size_t *failed_at)
1667 size_t *offp, *off_end;
1668 int debug_id = buffer->debug_id;
1670 if (binder_debug_mask & BINDER_DEBUG_TRANSACTION)
1671 printk(KERN_INFO "binder: %d buffer release %d, size %zd-%zd, failed at %p\n",
1672 proc->pid, buffer->debug_id,
1673 buffer->data_size, buffer->offsets_size, failed_at);
1675 if (buffer->target_node)
1676 binder_dec_node(buffer->target_node, 1, 0);
1678 offp = (size_t *)(buffer->data + ALIGN(buffer->data_size, sizeof(void *)));
1680 off_end = failed_at;
1682 off_end = (void *)offp + buffer->offsets_size;
1683 for (; offp < off_end; offp++) {
1684 struct flat_binder_object *fp;
1685 if (*offp > buffer->data_size - sizeof(*fp) ||
1686 buffer->data_size < sizeof(*fp) ||
1687 !IS_ALIGNED(*offp, sizeof(void *))) {
1688 printk(KERN_ERR "binder: transaction release %d bad"
1689 "offset %zd, size %zd\n", debug_id, *offp, buffer->data_size);
1692 fp = (struct flat_binder_object *)(buffer->data + *offp);
1694 case BINDER_TYPE_BINDER:
1695 case BINDER_TYPE_WEAK_BINDER: {
1696 struct binder_node *node = binder_get_node(proc, fp->binder);
1698 printk(KERN_ERR "binder: transaction release %d bad node %p\n", debug_id, fp->binder);
1701 if (binder_debug_mask & BINDER_DEBUG_TRANSACTION)
1702 printk(KERN_INFO " node %d u%p\n",
1703 node->debug_id, node->ptr);
1704 binder_dec_node(node, fp->type == BINDER_TYPE_BINDER, 0);
1706 case BINDER_TYPE_HANDLE:
1707 case BINDER_TYPE_WEAK_HANDLE: {
1708 struct binder_ref *ref = binder_get_ref(proc, fp->handle);
1710 printk(KERN_ERR "binder: transaction release %d bad handle %ld\n", debug_id, fp->handle);
1713 if (binder_debug_mask & BINDER_DEBUG_TRANSACTION)
1714 printk(KERN_INFO " ref %d desc %d (node %d)\n",
1715 ref->debug_id, ref->desc, ref->node->debug_id);
1716 binder_dec_ref(ref, fp->type == BINDER_TYPE_HANDLE);
1719 case BINDER_TYPE_FD:
1720 if (binder_debug_mask & BINDER_DEBUG_TRANSACTION)
1721 printk(KERN_INFO " fd %ld\n", fp->handle);
1723 task_close_fd(proc, fp->handle);
1727 printk(KERN_ERR "binder: transaction release %d bad object type %lx\n", debug_id, fp->type);
1734 binder_thread_write(struct binder_proc *proc, struct binder_thread *thread,
1735 void __user *buffer, int size, signed long *consumed)
1738 void __user *ptr = buffer + *consumed;
1739 void __user *end = buffer + size;
1741 while (ptr < end && thread->return_error == BR_OK) {
1742 if (get_user(cmd, (uint32_t __user *)ptr))
1744 ptr += sizeof(uint32_t);
1745 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
1746 binder_stats.bc[_IOC_NR(cmd)]++;
1747 proc->stats.bc[_IOC_NR(cmd)]++;
1748 thread->stats.bc[_IOC_NR(cmd)]++;
1756 struct binder_ref *ref;
1757 const char *debug_string;
1759 if (get_user(target, (uint32_t __user *)ptr))
1761 ptr += sizeof(uint32_t);
1762 if (target == 0 && binder_context_mgr_node &&
1763 (cmd == BC_INCREFS || cmd == BC_ACQUIRE)) {
1764 ref = binder_get_ref_for_node(proc,
1765 binder_context_mgr_node);
1766 if (ref->desc != target) {
1767 binder_user_error("binder: %d:"
1768 "%d tried to acquire "
1769 "reference to desc 0, "
1771 proc->pid, thread->pid,
1775 ref = binder_get_ref(proc, target);
1777 binder_user_error("binder: %d:%d refcou"
1778 "nt change on invalid ref %d\n",
1779 proc->pid, thread->pid, target);
1784 debug_string = "IncRefs";
1785 binder_inc_ref(ref, 0, NULL);
1788 debug_string = "Acquire";
1789 binder_inc_ref(ref, 1, NULL);
1792 debug_string = "Release";
1793 binder_dec_ref(ref, 1);
1797 debug_string = "DecRefs";
1798 binder_dec_ref(ref, 0);
1801 if (binder_debug_mask & BINDER_DEBUG_USER_REFS)
1802 printk(KERN_INFO "binder: %d:%d %s ref %d desc %d s %d w %d for node %d\n",
1803 proc->pid, thread->pid, debug_string, ref->debug_id, ref->desc, ref->strong, ref->weak, ref->node->debug_id);
1806 case BC_INCREFS_DONE:
1807 case BC_ACQUIRE_DONE: {
1808 void __user *node_ptr;
1810 struct binder_node *node;
1812 if (get_user(node_ptr, (void * __user *)ptr))
1814 ptr += sizeof(void *);
1815 if (get_user(cookie, (void * __user *)ptr))
1817 ptr += sizeof(void *);
1818 node = binder_get_node(proc, node_ptr);
1820 binder_user_error("binder: %d:%d "
1821 "%s u%p no match\n",
1822 proc->pid, thread->pid,
1823 cmd == BC_INCREFS_DONE ?
1829 if (cookie != node->cookie) {
1830 binder_user_error("binder: %d:%d %s u%p node %d"
1831 " cookie mismatch %p != %p\n",
1832 proc->pid, thread->pid,
1833 cmd == BC_INCREFS_DONE ?
1834 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
1835 node_ptr, node->debug_id,
1836 cookie, node->cookie);
1839 if (cmd == BC_ACQUIRE_DONE) {
1840 if (node->pending_strong_ref == 0) {
1841 binder_user_error("binder: %d:%d "
1842 "BC_ACQUIRE_DONE node %d has "
1843 "no pending acquire request\n",
1844 proc->pid, thread->pid,
1848 node->pending_strong_ref = 0;
1850 if (node->pending_weak_ref == 0) {
1851 binder_user_error("binder: %d:%d "
1852 "BC_INCREFS_DONE node %d has "
1853 "no pending increfs request\n",
1854 proc->pid, thread->pid,
1858 node->pending_weak_ref = 0;
1860 binder_dec_node(node, cmd == BC_ACQUIRE_DONE, 0);
1861 if (binder_debug_mask & BINDER_DEBUG_USER_REFS)
1862 printk(KERN_INFO "binder: %d:%d %s node %d ls %d lw %d\n",
1863 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);
1866 case BC_ATTEMPT_ACQUIRE:
1867 printk(KERN_ERR "binder: BC_ATTEMPT_ACQUIRE not supported\n");
1869 case BC_ACQUIRE_RESULT:
1870 printk(KERN_ERR "binder: BC_ACQUIRE_RESULT not supported\n");
1873 case BC_FREE_BUFFER: {
1874 void __user *data_ptr;
1875 struct binder_buffer *buffer;
1877 if (get_user(data_ptr, (void * __user *)ptr))
1879 ptr += sizeof(void *);
1881 buffer = binder_buffer_lookup(proc, data_ptr);
1882 if (buffer == NULL) {
1883 binder_user_error("binder: %d:%d "
1884 "BC_FREE_BUFFER u%p no match\n",
1885 proc->pid, thread->pid, data_ptr);
1888 if (!buffer->allow_user_free) {
1889 binder_user_error("binder: %d:%d "
1890 "BC_FREE_BUFFER u%p matched "
1891 "unreturned buffer\n",
1892 proc->pid, thread->pid, data_ptr);
1895 if (binder_debug_mask & BINDER_DEBUG_FREE_BUFFER)
1896 printk(KERN_INFO "binder: %d:%d BC_FREE_BUFFER u%p found buffer %d for %s transaction\n",
1897 proc->pid, thread->pid, data_ptr, buffer->debug_id,
1898 buffer->transaction ? "active" : "finished");
1900 if (buffer->transaction) {
1901 buffer->transaction->buffer = NULL;
1902 buffer->transaction = NULL;
1904 if (buffer->async_transaction && buffer->target_node) {
1905 BUG_ON(!buffer->target_node->has_async_transaction);
1906 if (list_empty(&buffer->target_node->async_todo))
1907 buffer->target_node->has_async_transaction = 0;
1909 list_move_tail(buffer->target_node->async_todo.next, &thread->todo);
1911 binder_transaction_buffer_release(proc, buffer, NULL);
1912 binder_free_buf(proc, buffer);
1916 case BC_TRANSACTION:
1918 struct binder_transaction_data tr;
1920 if (copy_from_user(&tr, ptr, sizeof(tr)))
1923 binder_transaction(proc, thread, &tr, cmd == BC_REPLY);
1927 case BC_REGISTER_LOOPER:
1928 if (binder_debug_mask & BINDER_DEBUG_THREADS)
1929 printk(KERN_INFO "binder: %d:%d BC_REGISTER_LOOPER\n",
1930 proc->pid, thread->pid);
1931 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
1932 thread->looper |= BINDER_LOOPER_STATE_INVALID;
1933 binder_user_error("binder: %d:%d ERROR:"
1934 " BC_REGISTER_LOOPER called "
1935 "after BC_ENTER_LOOPER\n",
1936 proc->pid, thread->pid);
1937 } else if (proc->requested_threads == 0) {
1938 thread->looper |= BINDER_LOOPER_STATE_INVALID;
1939 binder_user_error("binder: %d:%d ERROR:"
1940 " BC_REGISTER_LOOPER called "
1941 "without request\n",
1942 proc->pid, thread->pid);
1944 proc->requested_threads--;
1945 proc->requested_threads_started++;
1947 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
1949 case BC_ENTER_LOOPER:
1950 if (binder_debug_mask & BINDER_DEBUG_THREADS)
1951 printk(KERN_INFO "binder: %d:%d BC_ENTER_LOOPER\n",
1952 proc->pid, thread->pid);
1953 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
1954 thread->looper |= BINDER_LOOPER_STATE_INVALID;
1955 binder_user_error("binder: %d:%d ERROR:"
1956 " BC_ENTER_LOOPER called after "
1957 "BC_REGISTER_LOOPER\n",
1958 proc->pid, thread->pid);
1960 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
1962 case BC_EXIT_LOOPER:
1963 if (binder_debug_mask & BINDER_DEBUG_THREADS)
1964 printk(KERN_INFO "binder: %d:%d BC_EXIT_LOOPER\n",
1965 proc->pid, thread->pid);
1966 thread->looper |= BINDER_LOOPER_STATE_EXITED;
1969 case BC_REQUEST_DEATH_NOTIFICATION:
1970 case BC_CLEAR_DEATH_NOTIFICATION: {
1972 void __user *cookie;
1973 struct binder_ref *ref;
1974 struct binder_ref_death *death;
1976 if (get_user(target, (uint32_t __user *)ptr))
1978 ptr += sizeof(uint32_t);
1979 if (get_user(cookie, (void __user * __user *)ptr))
1981 ptr += sizeof(void *);
1982 ref = binder_get_ref(proc, target);
1984 binder_user_error("binder: %d:%d %s "
1986 proc->pid, thread->pid,
1987 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
1988 "BC_REQUEST_DEATH_NOTIFICATION" :
1989 "BC_CLEAR_DEATH_NOTIFICATION",
1994 if (binder_debug_mask & BINDER_DEBUG_DEATH_NOTIFICATION)
1995 printk(KERN_INFO "binder: %d:%d %s %p ref %d desc %d s %d w %d for node %d\n",
1996 proc->pid, thread->pid,
1997 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
1998 "BC_REQUEST_DEATH_NOTIFICATION" :
1999 "BC_CLEAR_DEATH_NOTIFICATION",
2000 cookie, ref->debug_id, ref->desc,
2001 ref->strong, ref->weak, ref->node->debug_id);
2003 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
2005 binder_user_error("binder: %d:%"
2006 "d BC_REQUEST_DEATH_NOTI"
2007 "FICATION death notific"
2008 "ation already set\n",
2009 proc->pid, thread->pid);
2012 death = kzalloc(sizeof(*death), GFP_KERNEL);
2013 if (death == NULL) {
2014 thread->return_error = BR_ERROR;
2015 if (binder_debug_mask & BINDER_DEBUG_FAILED_TRANSACTION)
2016 printk(KERN_INFO "binder: %d:%d "
2017 "BC_REQUEST_DEATH_NOTIFICATION failed\n",
2018 proc->pid, thread->pid);
2021 binder_stats.obj_created[BINDER_STAT_DEATH]++;
2022 INIT_LIST_HEAD(&death->work.entry);
2023 death->cookie = cookie;
2025 if (ref->node->proc == NULL) {
2026 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
2027 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2028 list_add_tail(&ref->death->work.entry, &thread->todo);
2030 list_add_tail(&ref->death->work.entry, &proc->todo);
2031 wake_up_interruptible(&proc->wait);
2035 if (ref->death == NULL) {
2036 binder_user_error("binder: %d:%"
2037 "d BC_CLEAR_DEATH_NOTIFI"
2038 "CATION death notificat"
2040 proc->pid, thread->pid);
2044 if (death->cookie != cookie) {
2045 binder_user_error("binder: %d:%"
2046 "d BC_CLEAR_DEATH_NOTIFI"
2047 "CATION death notificat"
2048 "ion cookie mismatch "
2050 proc->pid, thread->pid,
2051 death->cookie, cookie);
2055 if (list_empty(&death->work.entry)) {
2056 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2057 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2058 list_add_tail(&death->work.entry, &thread->todo);
2060 list_add_tail(&death->work.entry, &proc->todo);
2061 wake_up_interruptible(&proc->wait);
2064 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
2065 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
2069 case BC_DEAD_BINDER_DONE: {
2070 struct binder_work *w;
2071 void __user *cookie;
2072 struct binder_ref_death *death = NULL;
2073 if (get_user(cookie, (void __user * __user *)ptr))
2076 ptr += sizeof(void *);
2077 list_for_each_entry(w, &proc->delivered_death, entry) {
2078 struct binder_ref_death *tmp_death = container_of(w, struct binder_ref_death, work);
2079 if (tmp_death->cookie == cookie) {
2084 if (binder_debug_mask & BINDER_DEBUG_DEAD_BINDER)
2085 printk(KERN_INFO "binder: %d:%d BC_DEAD_BINDER_DONE %p found %p\n",
2086 proc->pid, thread->pid, cookie, death);
2087 if (death == NULL) {
2088 binder_user_error("binder: %d:%d BC_DEAD"
2089 "_BINDER_DONE %p not found\n",
2090 proc->pid, thread->pid, cookie);
2094 list_del_init(&death->work.entry);
2095 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
2096 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2097 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2098 list_add_tail(&death->work.entry, &thread->todo);
2100 list_add_tail(&death->work.entry, &proc->todo);
2101 wake_up_interruptible(&proc->wait);
2107 printk(KERN_ERR "binder: %d:%d unknown command %d\n", proc->pid, thread->pid, cmd);
2110 *consumed = ptr - buffer;
2116 binder_stat_br(struct binder_proc *proc, struct binder_thread *thread, uint32_t cmd)
2118 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
2119 binder_stats.br[_IOC_NR(cmd)]++;
2120 proc->stats.br[_IOC_NR(cmd)]++;
2121 thread->stats.br[_IOC_NR(cmd)]++;
2126 binder_has_proc_work(struct binder_proc *proc, struct binder_thread *thread)
2128 return !list_empty(&proc->todo) || (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2132 binder_has_thread_work(struct binder_thread *thread)
2134 return !list_empty(&thread->todo) || thread->return_error != BR_OK ||
2135 (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2139 binder_thread_read(struct binder_proc *proc, struct binder_thread *thread,
2140 void __user *buffer, int size, signed long *consumed, int non_block)
2142 void __user *ptr = buffer + *consumed;
2143 void __user *end = buffer + size;
2146 int wait_for_proc_work;
2148 if (*consumed == 0) {
2149 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
2151 ptr += sizeof(uint32_t);
2155 wait_for_proc_work = thread->transaction_stack == NULL && list_empty(&thread->todo);
2157 if (thread->return_error != BR_OK && ptr < end) {
2158 if (thread->return_error2 != BR_OK) {
2159 if (put_user(thread->return_error2, (uint32_t __user *)ptr))
2161 ptr += sizeof(uint32_t);
2164 thread->return_error2 = BR_OK;
2166 if (put_user(thread->return_error, (uint32_t __user *)ptr))
2168 ptr += sizeof(uint32_t);
2169 thread->return_error = BR_OK;
2174 thread->looper |= BINDER_LOOPER_STATE_WAITING;
2175 if (wait_for_proc_work)
2176 proc->ready_threads++;
2177 mutex_unlock(&binder_lock);
2178 if (wait_for_proc_work) {
2179 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2180 BINDER_LOOPER_STATE_ENTERED))) {
2181 binder_user_error("binder: %d:%d ERROR: Thread waiting "
2182 "for process work before calling BC_REGISTER_"
2183 "LOOPER or BC_ENTER_LOOPER (state %x)\n",
2184 proc->pid, thread->pid, thread->looper);
2185 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2187 binder_set_nice(proc->default_priority);
2189 if (!binder_has_proc_work(proc, thread))
2192 ret = wait_event_interruptible_exclusive(proc->wait, binder_has_proc_work(proc, thread));
2195 if (!binder_has_thread_work(thread))
2198 ret = wait_event_interruptible(thread->wait, binder_has_thread_work(thread));
2200 mutex_lock(&binder_lock);
2201 if (wait_for_proc_work)
2202 proc->ready_threads--;
2203 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
2210 struct binder_transaction_data tr;
2211 struct binder_work *w;
2212 struct binder_transaction *t = NULL;
2214 if (!list_empty(&thread->todo))
2215 w = list_first_entry(&thread->todo, struct binder_work, entry);
2216 else if (!list_empty(&proc->todo) && wait_for_proc_work)
2217 w = list_first_entry(&proc->todo, struct binder_work, entry);
2219 if (ptr - buffer == 4 && !(thread->looper & BINDER_LOOPER_STATE_NEED_RETURN)) /* no data added */
2224 if (end - ptr < sizeof(tr) + 4)
2228 case BINDER_WORK_TRANSACTION: {
2229 t = container_of(w, struct binder_transaction, work);
2231 case BINDER_WORK_TRANSACTION_COMPLETE: {
2232 cmd = BR_TRANSACTION_COMPLETE;
2233 if (put_user(cmd, (uint32_t __user *)ptr))
2235 ptr += sizeof(uint32_t);
2237 binder_stat_br(proc, thread, cmd);
2238 if (binder_debug_mask & BINDER_DEBUG_TRANSACTION_COMPLETE)
2239 printk(KERN_INFO "binder: %d:%d BR_TRANSACTION_COMPLETE\n",
2240 proc->pid, thread->pid);
2242 list_del(&w->entry);
2244 binder_stats.obj_deleted[BINDER_STAT_TRANSACTION_COMPLETE]++;
2246 case BINDER_WORK_NODE: {
2247 struct binder_node *node = container_of(w, struct binder_node, work);
2248 uint32_t cmd = BR_NOOP;
2249 const char *cmd_name;
2250 int strong = node->internal_strong_refs || node->local_strong_refs;
2251 int weak = !hlist_empty(&node->refs) || node->local_weak_refs || strong;
2252 if (weak && !node->has_weak_ref) {
2254 cmd_name = "BR_INCREFS";
2255 node->has_weak_ref = 1;
2256 node->pending_weak_ref = 1;
2257 node->local_weak_refs++;
2258 } else if (strong && !node->has_strong_ref) {
2260 cmd_name = "BR_ACQUIRE";
2261 node->has_strong_ref = 1;
2262 node->pending_strong_ref = 1;
2263 node->local_strong_refs++;
2264 } else if (!strong && node->has_strong_ref) {
2266 cmd_name = "BR_RELEASE";
2267 node->has_strong_ref = 0;
2268 } else if (!weak && node->has_weak_ref) {
2270 cmd_name = "BR_DECREFS";
2271 node->has_weak_ref = 0;
2273 if (cmd != BR_NOOP) {
2274 if (put_user(cmd, (uint32_t __user *)ptr))
2276 ptr += sizeof(uint32_t);
2277 if (put_user(node->ptr, (void * __user *)ptr))
2279 ptr += sizeof(void *);
2280 if (put_user(node->cookie, (void * __user *)ptr))
2282 ptr += sizeof(void *);
2284 binder_stat_br(proc, thread, cmd);
2285 if (binder_debug_mask & BINDER_DEBUG_USER_REFS)
2286 printk(KERN_INFO "binder: %d:%d %s %d u%p c%p\n",
2287 proc->pid, thread->pid, cmd_name, node->debug_id, node->ptr, node->cookie);
2289 list_del_init(&w->entry);
2290 if (!weak && !strong) {
2291 if (binder_debug_mask & BINDER_DEBUG_INTERNAL_REFS)
2292 printk(KERN_INFO "binder: %d:%d node %d u%p c%p deleted\n",
2293 proc->pid, thread->pid, node->debug_id, node->ptr, node->cookie);
2294 rb_erase(&node->rb_node, &proc->nodes);
2296 binder_stats.obj_deleted[BINDER_STAT_NODE]++;
2298 if (binder_debug_mask & BINDER_DEBUG_INTERNAL_REFS)
2299 printk(KERN_INFO "binder: %d:%d node %d u%p c%p state unchanged\n",
2300 proc->pid, thread->pid, node->debug_id, node->ptr, node->cookie);
2304 case BINDER_WORK_DEAD_BINDER:
2305 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2306 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2307 struct binder_ref_death *death = container_of(w, struct binder_ref_death, work);
2309 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
2310 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
2312 cmd = BR_DEAD_BINDER;
2313 if (put_user(cmd, (uint32_t __user *)ptr))
2315 ptr += sizeof(uint32_t);
2316 if (put_user(death->cookie, (void * __user *)ptr))
2318 ptr += sizeof(void *);
2319 if (binder_debug_mask & BINDER_DEBUG_DEATH_NOTIFICATION)
2320 printk(KERN_INFO "binder: %d:%d %s %p\n",
2321 proc->pid, thread->pid,
2322 cmd == BR_DEAD_BINDER ?
2324 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
2327 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
2328 list_del(&w->entry);
2330 binder_stats.obj_deleted[BINDER_STAT_DEATH]++;
2332 list_move(&w->entry, &proc->delivered_death);
2333 if (cmd == BR_DEAD_BINDER)
2334 goto done; /* DEAD_BINDER notifications can cause transactions */
2341 BUG_ON(t->buffer == NULL);
2342 if (t->buffer->target_node) {
2343 struct binder_node *target_node = t->buffer->target_node;
2344 tr.target.ptr = target_node->ptr;
2345 tr.cookie = target_node->cookie;
2346 t->saved_priority = task_nice(current);
2347 if (t->priority < target_node->min_priority &&
2348 !(t->flags & TF_ONE_WAY))
2349 binder_set_nice(t->priority);
2350 else if (!(t->flags & TF_ONE_WAY) ||
2351 t->saved_priority > target_node->min_priority)
2352 binder_set_nice(target_node->min_priority);
2353 cmd = BR_TRANSACTION;
2355 tr.target.ptr = NULL;
2360 tr.flags = t->flags;
2361 tr.sender_euid = t->sender_euid;
2364 struct task_struct *sender = t->from->proc->tsk;
2365 tr.sender_pid = task_tgid_nr_ns(sender, current->nsproxy->pid_ns);
2370 tr.data_size = t->buffer->data_size;
2371 tr.offsets_size = t->buffer->offsets_size;
2372 tr.data.ptr.buffer = (void *)t->buffer->data + proc->user_buffer_offset;
2373 tr.data.ptr.offsets = tr.data.ptr.buffer + ALIGN(t->buffer->data_size, sizeof(void *));
2375 if (put_user(cmd, (uint32_t __user *)ptr))
2377 ptr += sizeof(uint32_t);
2378 if (copy_to_user(ptr, &tr, sizeof(tr)))
2382 binder_stat_br(proc, thread, cmd);
2383 if (binder_debug_mask & BINDER_DEBUG_TRANSACTION)
2384 printk(KERN_INFO "binder: %d:%d %s %d %d:%d, cmd %d"
2385 "size %zd-%zd ptr %p-%p\n",
2386 proc->pid, thread->pid,
2387 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" : "BR_REPLY",
2388 t->debug_id, t->from ? t->from->proc->pid : 0,
2389 t->from ? t->from->pid : 0, cmd,
2390 t->buffer->data_size, t->buffer->offsets_size,
2391 tr.data.ptr.buffer, tr.data.ptr.offsets);
2393 list_del(&t->work.entry);
2394 t->buffer->allow_user_free = 1;
2395 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
2396 t->to_parent = thread->transaction_stack;
2397 t->to_thread = thread;
2398 thread->transaction_stack = t;
2400 t->buffer->transaction = NULL;
2402 binder_stats.obj_deleted[BINDER_STAT_TRANSACTION]++;
2409 *consumed = ptr - buffer;
2410 if (proc->requested_threads + proc->ready_threads == 0 &&
2411 proc->requested_threads_started < proc->max_threads &&
2412 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2413 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
2414 /*spawn a new thread if we leave this out */) {
2415 proc->requested_threads++;
2416 if (binder_debug_mask & BINDER_DEBUG_THREADS)
2417 printk(KERN_INFO "binder: %d:%d BR_SPAWN_LOOPER\n",
2418 proc->pid, thread->pid);
2419 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
2425 static void binder_release_work(struct list_head *list)
2427 struct binder_work *w;
2428 while (!list_empty(list)) {
2429 w = list_first_entry(list, struct binder_work, entry);
2430 list_del_init(&w->entry);
2432 case BINDER_WORK_TRANSACTION: {
2433 struct binder_transaction *t = container_of(w, struct binder_transaction, work);
2434 if (t->buffer->target_node && !(t->flags & TF_ONE_WAY))
2435 binder_send_failed_reply(t, BR_DEAD_REPLY);
2437 case BINDER_WORK_TRANSACTION_COMPLETE: {
2439 binder_stats.obj_deleted[BINDER_STAT_TRANSACTION_COMPLETE]++;
2448 static struct binder_thread *binder_get_thread(struct binder_proc *proc)
2450 struct binder_thread *thread = NULL;
2451 struct rb_node *parent = NULL;
2452 struct rb_node **p = &proc->threads.rb_node;
2456 thread = rb_entry(parent, struct binder_thread, rb_node);
2458 if (current->pid < thread->pid)
2460 else if (current->pid > thread->pid)
2461 p = &(*p)->rb_right;
2466 thread = kzalloc(sizeof(*thread), GFP_KERNEL);
2469 binder_stats.obj_created[BINDER_STAT_THREAD]++;
2470 thread->proc = proc;
2471 thread->pid = current->pid;
2472 init_waitqueue_head(&thread->wait);
2473 INIT_LIST_HEAD(&thread->todo);
2474 rb_link_node(&thread->rb_node, parent, p);
2475 rb_insert_color(&thread->rb_node, &proc->threads);
2476 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
2477 thread->return_error = BR_OK;
2478 thread->return_error2 = BR_OK;
2483 static int binder_free_thread(struct binder_proc *proc, struct binder_thread *thread)
2485 struct binder_transaction *t;
2486 struct binder_transaction *send_reply = NULL;
2487 int active_transactions = 0;
2489 rb_erase(&thread->rb_node, &proc->threads);
2490 t = thread->transaction_stack;
2491 if (t && t->to_thread == thread)
2494 active_transactions++;
2495 if (binder_debug_mask & BINDER_DEBUG_DEAD_TRANSACTION)
2496 printk(KERN_INFO "binder: release %d:%d transaction %d %s, still active\n",
2497 proc->pid, thread->pid, t->debug_id, (t->to_thread == thread) ? "in" : "out");
2498 if (t->to_thread == thread) {
2500 t->to_thread = NULL;
2502 t->buffer->transaction = NULL;
2506 } else if (t->from == thread) {
2513 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
2514 binder_release_work(&thread->todo);
2516 binder_stats.obj_deleted[BINDER_STAT_THREAD]++;
2517 return active_transactions;
2520 static unsigned int binder_poll(struct file *filp, struct poll_table_struct *wait)
2522 struct binder_proc *proc = filp->private_data;
2523 struct binder_thread *thread = NULL;
2524 int wait_for_proc_work;
2526 mutex_lock(&binder_lock);
2527 thread = binder_get_thread(proc);
2529 wait_for_proc_work = thread->transaction_stack == NULL &&
2530 list_empty(&thread->todo) && thread->return_error == BR_OK;
2531 mutex_unlock(&binder_lock);
2533 if (wait_for_proc_work) {
2534 if (binder_has_proc_work(proc, thread))
2536 poll_wait(filp, &proc->wait, wait);
2537 if (binder_has_proc_work(proc, thread))
2540 if (binder_has_thread_work(thread))
2542 poll_wait(filp, &thread->wait, wait);
2543 if (binder_has_thread_work(thread))
2549 static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2552 struct binder_proc *proc = filp->private_data;
2553 struct binder_thread *thread;
2554 unsigned int size = _IOC_SIZE(cmd);
2555 void __user *ubuf = (void __user *)arg;
2557 /*printk(KERN_INFO "binder_ioctl: %d:%d %x %lx\n", proc->pid, current->pid, cmd, arg);*/
2559 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2563 mutex_lock(&binder_lock);
2564 thread = binder_get_thread(proc);
2565 if (thread == NULL) {
2571 case BINDER_WRITE_READ: {
2572 struct binder_write_read bwr;
2573 if (size != sizeof(struct binder_write_read)) {
2577 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
2581 if (binder_debug_mask & BINDER_DEBUG_READ_WRITE)
2582 printk(KERN_INFO "binder: %d:%d write %ld at %08lx, read %ld at %08lx\n",
2583 proc->pid, thread->pid, bwr.write_size, bwr.write_buffer, bwr.read_size, bwr.read_buffer);
2584 if (bwr.write_size > 0) {
2585 ret = binder_thread_write(proc, thread, (void __user *)bwr.write_buffer, bwr.write_size, &bwr.write_consumed);
2587 bwr.read_consumed = 0;
2588 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2593 if (bwr.read_size > 0) {
2594 ret = binder_thread_read(proc, thread, (void __user *)bwr.read_buffer, bwr.read_size, &bwr.read_consumed, filp->f_flags & O_NONBLOCK);
2595 if (!list_empty(&proc->todo))
2596 wake_up_interruptible(&proc->wait);
2598 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2603 if (binder_debug_mask & BINDER_DEBUG_READ_WRITE)
2604 printk(KERN_INFO "binder: %d:%d wrote %ld of %ld, read return %ld of %ld\n",
2605 proc->pid, thread->pid, bwr.write_consumed, bwr.write_size, bwr.read_consumed, bwr.read_size);
2606 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
2612 case BINDER_SET_MAX_THREADS:
2613 if (copy_from_user(&proc->max_threads, ubuf, sizeof(proc->max_threads))) {
2618 case BINDER_SET_CONTEXT_MGR:
2619 if (binder_context_mgr_node != NULL) {
2620 printk(KERN_ERR "binder: BINDER_SET_CONTEXT_MGR already set\n");
2624 if (binder_context_mgr_uid != -1) {
2625 if (binder_context_mgr_uid != current->cred->euid) {
2626 printk(KERN_ERR "binder: BINDER_SET_"
2627 "CONTEXT_MGR bad uid %d != %d\n",
2628 current->cred->euid,
2629 binder_context_mgr_uid);
2634 binder_context_mgr_uid = current->cred->euid;
2635 binder_context_mgr_node = binder_new_node(proc, NULL, NULL);
2636 if (binder_context_mgr_node == NULL) {
2640 binder_context_mgr_node->local_weak_refs++;
2641 binder_context_mgr_node->local_strong_refs++;
2642 binder_context_mgr_node->has_strong_ref = 1;
2643 binder_context_mgr_node->has_weak_ref = 1;
2645 case BINDER_THREAD_EXIT:
2646 if (binder_debug_mask & BINDER_DEBUG_THREADS)
2647 printk(KERN_INFO "binder: %d:%d exit\n",
2648 proc->pid, thread->pid);
2649 binder_free_thread(proc, thread);
2652 case BINDER_VERSION:
2653 if (size != sizeof(struct binder_version)) {
2657 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION, &((struct binder_version *)ubuf)->protocol_version)) {
2669 thread->looper &= ~BINDER_LOOPER_STATE_NEED_RETURN;
2670 mutex_unlock(&binder_lock);
2671 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2672 if (ret && ret != -ERESTARTSYS)
2673 printk(KERN_INFO "binder: %d:%d ioctl %x %lx returned %d\n", proc->pid, current->pid, cmd, arg, ret);
2677 static void binder_vma_open(struct vm_area_struct *vma)
2679 struct binder_proc *proc = vma->vm_private_data;
2680 if (binder_debug_mask & BINDER_DEBUG_OPEN_CLOSE)
2682 "binder: %d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
2683 proc->pid, vma->vm_start, vma->vm_end,
2684 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2685 (unsigned long)pgprot_val(vma->vm_page_prot));
2689 static void binder_vma_close(struct vm_area_struct *vma)
2691 struct binder_proc *proc = vma->vm_private_data;
2692 if (binder_debug_mask & BINDER_DEBUG_OPEN_CLOSE)
2694 "binder: %d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
2695 proc->pid, vma->vm_start, vma->vm_end,
2696 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2697 (unsigned long)pgprot_val(vma->vm_page_prot));
2699 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
2702 static struct vm_operations_struct binder_vm_ops = {
2703 .open = binder_vma_open,
2704 .close = binder_vma_close,
2707 static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
2710 struct vm_struct *area;
2711 struct binder_proc *proc = filp->private_data;
2712 const char *failure_string;
2713 struct binder_buffer *buffer;
2715 if ((vma->vm_end - vma->vm_start) > SZ_4M)
2716 vma->vm_end = vma->vm_start + SZ_4M;
2718 if (binder_debug_mask & BINDER_DEBUG_OPEN_CLOSE)
2720 "binder_mmap: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
2721 proc->pid, vma->vm_start, vma->vm_end,
2722 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2723 (unsigned long)pgprot_val(vma->vm_page_prot));
2725 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
2727 failure_string = "bad vm_flags";
2730 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
2734 failure_string = "already mapped";
2735 goto err_already_mapped;
2738 area = get_vm_area(vma->vm_end - vma->vm_start, VM_IOREMAP);
2741 failure_string = "get_vm_area";
2742 goto err_get_vm_area_failed;
2744 proc->buffer = area->addr;
2745 proc->user_buffer_offset = vma->vm_start - (uintptr_t)proc->buffer;
2747 #ifdef CONFIG_CPU_CACHE_VIPT
2748 if (cache_is_vipt_aliasing()) {
2749 while (CACHE_COLOUR((vma->vm_start ^ (uint32_t)proc->buffer))) {
2750 printk(KERN_INFO "binder_mmap: %d %lx-%lx maps %p bad alignment\n", proc->pid, vma->vm_start, vma->vm_end, proc->buffer);
2751 vma->vm_start += PAGE_SIZE;
2755 proc->pages = kzalloc(sizeof(proc->pages[0]) * ((vma->vm_end - vma->vm_start) / PAGE_SIZE), GFP_KERNEL);
2756 if (proc->pages == NULL) {
2758 failure_string = "alloc page array";
2759 goto err_alloc_pages_failed;
2761 proc->buffer_size = vma->vm_end - vma->vm_start;
2763 vma->vm_ops = &binder_vm_ops;
2764 vma->vm_private_data = proc;
2766 if (binder_update_page_range(proc, 1, proc->buffer, proc->buffer + PAGE_SIZE, vma)) {
2768 failure_string = "alloc small buf";
2769 goto err_alloc_small_buf_failed;
2771 buffer = proc->buffer;
2772 INIT_LIST_HEAD(&proc->buffers);
2773 list_add(&buffer->entry, &proc->buffers);
2775 binder_insert_free_buffer(proc, buffer);
2776 proc->free_async_space = proc->buffer_size / 2;
2778 proc->files = get_files_struct(current);
2781 /*printk(KERN_INFO "binder_mmap: %d %lx-%lx maps %p\n", proc->pid, vma->vm_start, vma->vm_end, proc->buffer);*/
2784 err_alloc_small_buf_failed:
2787 err_alloc_pages_failed:
2788 vfree(proc->buffer);
2789 proc->buffer = NULL;
2790 err_get_vm_area_failed:
2793 printk(KERN_ERR "binder_mmap: %d %lx-%lx %s failed %d\n", proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
2797 static int binder_open(struct inode *nodp, struct file *filp)
2799 struct binder_proc *proc;
2801 if (binder_debug_mask & BINDER_DEBUG_OPEN_CLOSE)
2802 printk(KERN_INFO "binder_open: %d:%d\n", current->group_leader->pid, current->pid);
2804 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
2807 get_task_struct(current);
2808 proc->tsk = current;
2809 INIT_LIST_HEAD(&proc->todo);
2810 init_waitqueue_head(&proc->wait);
2811 proc->default_priority = task_nice(current);
2812 mutex_lock(&binder_lock);
2813 binder_stats.obj_created[BINDER_STAT_PROC]++;
2814 hlist_add_head(&proc->proc_node, &binder_procs);
2815 proc->pid = current->group_leader->pid;
2816 INIT_LIST_HEAD(&proc->delivered_death);
2817 filp->private_data = proc;
2818 mutex_unlock(&binder_lock);
2820 if (binder_proc_dir_entry_proc) {
2822 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
2823 remove_proc_entry(strbuf, binder_proc_dir_entry_proc);
2824 create_proc_read_entry(strbuf, S_IRUGO, binder_proc_dir_entry_proc, binder_read_proc_proc, proc);
2830 static int binder_flush(struct file *filp, fl_owner_t id)
2832 struct binder_proc *proc = filp->private_data;
2834 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
2839 static void binder_deferred_flush(struct binder_proc *proc)
2843 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
2844 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
2845 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
2846 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
2847 wake_up_interruptible(&thread->wait);
2851 wake_up_interruptible_all(&proc->wait);
2853 if (binder_debug_mask & BINDER_DEBUG_OPEN_CLOSE)
2854 printk(KERN_INFO "binder_flush: %d woke %d threads\n", proc->pid, wake_count);
2857 static int binder_release(struct inode *nodp, struct file *filp)
2859 struct binder_proc *proc = filp->private_data;
2860 if (binder_proc_dir_entry_proc) {
2862 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
2863 remove_proc_entry(strbuf, binder_proc_dir_entry_proc);
2866 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
2871 static void binder_deferred_release(struct binder_proc *proc)
2873 struct hlist_node *pos;
2874 struct binder_transaction *t;
2876 int threads, nodes, incoming_refs, outgoing_refs, buffers, active_transactions, page_count;
2879 BUG_ON(proc->files);
2881 hlist_del(&proc->proc_node);
2882 if (binder_context_mgr_node && binder_context_mgr_node->proc == proc) {
2883 if (binder_debug_mask & BINDER_DEBUG_DEAD_BINDER)
2884 printk(KERN_INFO "binder_release: %d context_mgr_node gone\n", proc->pid);
2885 binder_context_mgr_node = NULL;
2889 active_transactions = 0;
2890 while ((n = rb_first(&proc->threads))) {
2891 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
2893 active_transactions += binder_free_thread(proc, thread);
2897 while ((n = rb_first(&proc->nodes))) {
2898 struct binder_node *node = rb_entry(n, struct binder_node, rb_node);
2901 rb_erase(&node->rb_node, &proc->nodes);
2902 list_del_init(&node->work.entry);
2903 if (hlist_empty(&node->refs)) {
2905 binder_stats.obj_deleted[BINDER_STAT_NODE]++;
2907 struct binder_ref *ref;
2911 node->local_strong_refs = 0;
2912 node->local_weak_refs = 0;
2913 hlist_add_head(&node->dead_node, &binder_dead_nodes);
2915 hlist_for_each_entry(ref, pos, &node->refs, node_entry) {
2919 if (list_empty(&ref->death->work.entry)) {
2920 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
2921 list_add_tail(&ref->death->work.entry, &ref->proc->todo);
2922 wake_up_interruptible(&ref->proc->wait);
2927 if (binder_debug_mask & BINDER_DEBUG_DEAD_BINDER)
2928 printk(KERN_INFO "binder: node %d now dead, refs %d, death %d\n", node->debug_id, incoming_refs, death);
2932 while ((n = rb_first(&proc->refs_by_desc))) {
2933 struct binder_ref *ref = rb_entry(n, struct binder_ref, rb_node_desc);
2935 binder_delete_ref(ref);
2937 binder_release_work(&proc->todo);
2940 while ((n = rb_first(&proc->allocated_buffers))) {
2941 struct binder_buffer *buffer = rb_entry(n, struct binder_buffer, rb_node);
2942 t = buffer->transaction;
2945 buffer->transaction = NULL;
2946 printk(KERN_ERR "binder: release proc %d, transaction %d, not freed\n", proc->pid, t->debug_id);
2949 binder_free_buf(proc, buffer);
2953 binder_stats.obj_deleted[BINDER_STAT_PROC]++;
2958 for (i = 0; i < proc->buffer_size / PAGE_SIZE; i++) {
2959 if (proc->pages[i]) {
2960 if (binder_debug_mask & BINDER_DEBUG_BUFFER_ALLOC)
2961 printk(KERN_INFO "binder_release: %d: page %d at %p not freed\n", proc->pid, i, proc->buffer + i * PAGE_SIZE);
2962 __free_page(proc->pages[i]);
2967 vfree(proc->buffer);
2970 put_task_struct(proc->tsk);
2972 if (binder_debug_mask & BINDER_DEBUG_OPEN_CLOSE)
2973 printk(KERN_INFO "binder_release: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d, buffers %d, pages %d\n",
2974 proc->pid, threads, nodes, incoming_refs, outgoing_refs, active_transactions, buffers, page_count);
2979 static void binder_deferred_func(struct work_struct *work)
2981 struct binder_proc *proc;
2982 struct files_struct *files;
2986 mutex_lock(&binder_lock);
2987 mutex_lock(&binder_deferred_lock);
2988 if (!hlist_empty(&binder_deferred_list)) {
2989 proc = hlist_entry(binder_deferred_list.first,
2990 struct binder_proc, deferred_work_node);
2991 hlist_del_init(&proc->deferred_work_node);
2992 defer = proc->deferred_work;
2993 proc->deferred_work = 0;
2998 mutex_unlock(&binder_deferred_lock);
3001 if (defer & BINDER_DEFERRED_PUT_FILES)
3002 if ((files = proc->files))
3005 if (defer & BINDER_DEFERRED_FLUSH)
3006 binder_deferred_flush(proc);
3008 if (defer & BINDER_DEFERRED_RELEASE)
3009 binder_deferred_release(proc); /* frees proc */
3011 mutex_unlock(&binder_lock);
3013 put_files_struct(files);
3016 static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
3018 static void binder_defer_work(struct binder_proc *proc, int defer)
3020 mutex_lock(&binder_deferred_lock);
3021 proc->deferred_work |= defer;
3022 if (hlist_unhashed(&proc->deferred_work_node)) {
3023 hlist_add_head(&proc->deferred_work_node,
3024 &binder_deferred_list);
3025 schedule_work(&binder_deferred_work);
3027 mutex_unlock(&binder_deferred_lock);
3030 static char *print_binder_transaction(char *buf, char *end, const char *prefix, struct binder_transaction *t)
3032 buf += snprintf(buf, end - buf, "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %ld r%d",
3033 prefix, t->debug_id, t, t->from ? t->from->proc->pid : 0,
3034 t->from ? t->from->pid : 0,
3035 t->to_proc ? t->to_proc->pid : 0,
3036 t->to_thread ? t->to_thread->pid : 0,
3037 t->code, t->flags, t->priority, t->need_reply);
3040 if (t->buffer == NULL) {
3041 buf += snprintf(buf, end - buf, " buffer free\n");
3044 if (t->buffer->target_node) {
3045 buf += snprintf(buf, end - buf, " node %d",
3046 t->buffer->target_node->debug_id);
3050 buf += snprintf(buf, end - buf, " size %zd:%zd data %p\n",
3051 t->buffer->data_size, t->buffer->offsets_size,
3056 static char *print_binder_buffer(char *buf, char *end, const char *prefix, struct binder_buffer *buffer)
3058 buf += snprintf(buf, end - buf, "%s %d: %p size %zd:%zd %s\n",
3059 prefix, buffer->debug_id, buffer->data,
3060 buffer->data_size, buffer->offsets_size,
3061 buffer->transaction ? "active" : "delivered");
3065 static char *print_binder_work(char *buf, char *end, const char *prefix,
3066 const char *transaction_prefix, struct binder_work *w)
3068 struct binder_node *node;
3069 struct binder_transaction *t;
3072 case BINDER_WORK_TRANSACTION:
3073 t = container_of(w, struct binder_transaction, work);
3074 buf = print_binder_transaction(buf, end, transaction_prefix, t);
3076 case BINDER_WORK_TRANSACTION_COMPLETE:
3077 buf += snprintf(buf, end - buf,
3078 "%stransaction complete\n", prefix);
3080 case BINDER_WORK_NODE:
3081 node = container_of(w, struct binder_node, work);
3082 buf += snprintf(buf, end - buf, "%snode work %d: u%p c%p\n",
3083 prefix, node->debug_id, node->ptr, node->cookie);
3085 case BINDER_WORK_DEAD_BINDER:
3086 buf += snprintf(buf, end - buf, "%shas dead binder\n", prefix);
3088 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
3089 buf += snprintf(buf, end - buf,
3090 "%shas cleared dead binder\n", prefix);
3092 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
3093 buf += snprintf(buf, end - buf,
3094 "%shas cleared death notification\n", prefix);
3097 buf += snprintf(buf, end - buf, "%sunknown work: type %d\n",
3104 static char *print_binder_thread(char *buf, char *end, struct binder_thread *thread, int print_always)
3106 struct binder_transaction *t;
3107 struct binder_work *w;
3108 char *start_buf = buf;
3111 buf += snprintf(buf, end - buf, " thread %d: l %02x\n", thread->pid, thread->looper);
3113 t = thread->transaction_stack;
3117 if (t->from == thread) {
3118 buf = print_binder_transaction(buf, end, " outgoing transaction", t);
3120 } else if (t->to_thread == thread) {
3121 buf = print_binder_transaction(buf, end, " incoming transaction", t);
3124 buf = print_binder_transaction(buf, end, " bad transaction", t);
3128 list_for_each_entry(w, &thread->todo, entry) {
3131 buf = print_binder_work(buf, end, " ",
3132 " pending transaction", w);
3134 if (!print_always && buf == header_buf)
3139 static char *print_binder_node(char *buf, char *end, struct binder_node *node)
3141 struct binder_ref *ref;
3142 struct hlist_node *pos;
3143 struct binder_work *w;
3146 hlist_for_each_entry(ref, pos, &node->refs, node_entry)
3149 buf += snprintf(buf, end - buf, " node %d: u%p c%p hs %d hw %d ls %d lw %d is %d iw %d",
3150 node->debug_id, node->ptr, node->cookie,
3151 node->has_strong_ref, node->has_weak_ref,
3152 node->local_strong_refs, node->local_weak_refs,
3153 node->internal_strong_refs, count);
3157 buf += snprintf(buf, end - buf, " proc");
3160 hlist_for_each_entry(ref, pos, &node->refs, node_entry) {
3161 buf += snprintf(buf, end - buf, " %d", ref->proc->pid);
3166 buf += snprintf(buf, end - buf, "\n");
3167 list_for_each_entry(w, &node->async_todo, entry) {
3170 buf = print_binder_work(buf, end, " ",
3171 " pending async transaction", w);
3176 static char *print_binder_ref(char *buf, char *end, struct binder_ref *ref)
3178 buf += snprintf(buf, end - buf, " ref %d: desc %d %snode %d s %d w %d d %p\n",
3179 ref->debug_id, ref->desc, ref->node->proc ? "" : "dead ",
3180 ref->node->debug_id, ref->strong, ref->weak, ref->death);
3184 static char *print_binder_proc(char *buf, char *end, struct binder_proc *proc, int print_all)
3186 struct binder_work *w;
3188 char *start_buf = buf;
3191 buf += snprintf(buf, end - buf, "proc %d\n", proc->pid);
3194 for (n = rb_first(&proc->threads); n != NULL && buf < end; n = rb_next(n))
3195 buf = print_binder_thread(buf, end, rb_entry(n, struct binder_thread, rb_node), print_all);
3196 for (n = rb_first(&proc->nodes); n != NULL && buf < end; n = rb_next(n)) {
3197 struct binder_node *node = rb_entry(n, struct binder_node, rb_node);
3198 if (print_all || node->has_async_transaction)
3199 buf = print_binder_node(buf, end, node);
3202 for (n = rb_first(&proc->refs_by_desc); n != NULL && buf < end; n = rb_next(n))
3203 buf = print_binder_ref(buf, end, rb_entry(n, struct binder_ref, rb_node_desc));
3205 for (n = rb_first(&proc->allocated_buffers); n != NULL && buf < end; n = rb_next(n))
3206 buf = print_binder_buffer(buf, end, " buffer", rb_entry(n, struct binder_buffer, rb_node));
3207 list_for_each_entry(w, &proc->todo, entry) {
3210 buf = print_binder_work(buf, end, " ",
3211 " pending transaction", w);
3213 list_for_each_entry(w, &proc->delivered_death, entry) {
3216 buf += snprintf(buf, end - buf, " has delivered dead binder\n");
3219 if (!print_all && buf == header_buf)
3224 static const char *binder_return_strings[] = {
3229 "BR_ACQUIRE_RESULT",
3231 "BR_TRANSACTION_COMPLETE",
3236 "BR_ATTEMPT_ACQUIRE",
3241 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
3245 static const char *binder_command_strings[] = {
3248 "BC_ACQUIRE_RESULT",
3256 "BC_ATTEMPT_ACQUIRE",
3257 "BC_REGISTER_LOOPER",
3260 "BC_REQUEST_DEATH_NOTIFICATION",
3261 "BC_CLEAR_DEATH_NOTIFICATION",
3262 "BC_DEAD_BINDER_DONE"
3265 static const char *binder_objstat_strings[] = {
3272 "transaction_complete"
3275 static char *print_binder_stats(char *buf, char *end, const char *prefix, struct binder_stats *stats)
3279 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) != ARRAY_SIZE(binder_command_strings));
3280 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
3282 buf += snprintf(buf, end - buf, "%s%s: %d\n", prefix,
3283 binder_command_strings[i], stats->bc[i]);
3288 BUILD_BUG_ON(ARRAY_SIZE(stats->br) != ARRAY_SIZE(binder_return_strings));
3289 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
3291 buf += snprintf(buf, end - buf, "%s%s: %d\n", prefix,
3292 binder_return_strings[i], stats->br[i]);
3297 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) != ARRAY_SIZE(binder_objstat_strings));
3298 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) != ARRAY_SIZE(stats->obj_deleted));
3299 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
3300 if (stats->obj_created[i] || stats->obj_deleted[i])
3301 buf += snprintf(buf, end - buf, "%s%s: active %d total %d\n", prefix,
3302 binder_objstat_strings[i],
3303 stats->obj_created[i] - stats->obj_deleted[i],
3304 stats->obj_created[i]);
3311 static char *print_binder_proc_stats(char *buf, char *end, struct binder_proc *proc)
3313 struct binder_work *w;
3315 int count, strong, weak;
3317 buf += snprintf(buf, end - buf, "proc %d\n", proc->pid);
3321 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3323 buf += snprintf(buf, end - buf, " threads: %d\n", count);
3326 buf += snprintf(buf, end - buf, " requested threads: %d+%d/%d\n"
3327 " ready threads %d\n"
3328 " free async space %zd\n", proc->requested_threads,
3329 proc->requested_threads_started, proc->max_threads,
3330 proc->ready_threads, proc->free_async_space);
3334 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
3336 buf += snprintf(buf, end - buf, " nodes: %d\n", count);
3342 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
3343 struct binder_ref *ref = rb_entry(n, struct binder_ref, rb_node_desc);
3345 strong += ref->strong;
3348 buf += snprintf(buf, end - buf, " refs: %d s %d w %d\n", count, strong, weak);
3353 for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
3355 buf += snprintf(buf, end - buf, " buffers: %d\n", count);
3360 list_for_each_entry(w, &proc->todo, entry) {
3362 case BINDER_WORK_TRANSACTION:
3369 buf += snprintf(buf, end - buf, " pending transactions: %d\n", count);
3373 buf = print_binder_stats(buf, end, " ", &proc->stats);
3379 static int binder_read_proc_state(
3380 char *page, char **start, off_t off, int count, int *eof, void *data)
3382 struct binder_proc *proc;
3383 struct hlist_node *pos;
3384 struct binder_node *node;
3387 char *end = page + PAGE_SIZE;
3388 int do_lock = !binder_debug_no_lock;
3394 mutex_lock(&binder_lock);
3396 buf += snprintf(buf, end - buf, "binder state:\n");
3398 if (!hlist_empty(&binder_dead_nodes))
3399 buf += snprintf(buf, end - buf, "dead nodes:\n");
3400 hlist_for_each_entry(node, pos, &binder_dead_nodes, dead_node) {
3403 buf = print_binder_node(buf, end, node);
3406 hlist_for_each_entry(proc, pos, &binder_procs, proc_node) {
3409 buf = print_binder_proc(buf, end, proc, 1);
3412 mutex_unlock(&binder_lock);
3413 if (buf > page + PAGE_SIZE)
3414 buf = page + PAGE_SIZE;
3416 *start = page + off;
3424 return len < count ? len : count;
3427 static int binder_read_proc_stats(
3428 char *page, char **start, off_t off, int count, int *eof, void *data)
3430 struct binder_proc *proc;
3431 struct hlist_node *pos;
3434 int do_lock = !binder_debug_no_lock;
3440 mutex_lock(&binder_lock);
3442 p += snprintf(p, PAGE_SIZE, "binder stats:\n");
3444 p = print_binder_stats(p, page + PAGE_SIZE, "", &binder_stats);
3446 hlist_for_each_entry(proc, pos, &binder_procs, proc_node) {
3447 if (p >= page + PAGE_SIZE)
3449 p = print_binder_proc_stats(p, page + PAGE_SIZE, proc);
3452 mutex_unlock(&binder_lock);
3453 if (p > page + PAGE_SIZE)
3454 p = page + PAGE_SIZE;
3456 *start = page + off;
3464 return len < count ? len : count;
3467 static int binder_read_proc_transactions(
3468 char *page, char **start, off_t off, int count, int *eof, void *data)
3470 struct binder_proc *proc;
3471 struct hlist_node *pos;
3474 char *end = page + PAGE_SIZE;
3475 int do_lock = !binder_debug_no_lock;
3481 mutex_lock(&binder_lock);
3483 buf += snprintf(buf, end - buf, "binder transactions:\n");
3484 hlist_for_each_entry(proc, pos, &binder_procs, proc_node) {
3487 buf = print_binder_proc(buf, end, proc, 0);
3490 mutex_unlock(&binder_lock);
3491 if (buf > page + PAGE_SIZE)
3492 buf = page + PAGE_SIZE;
3494 *start = page + off;
3502 return len < count ? len : count;
3505 static int binder_read_proc_proc(
3506 char *page, char **start, off_t off, int count, int *eof, void *data)
3508 struct binder_proc *proc = data;
3511 int do_lock = !binder_debug_no_lock;
3517 mutex_lock(&binder_lock);
3518 p += snprintf(p, PAGE_SIZE, "binder proc state:\n");
3519 p = print_binder_proc(p, page + PAGE_SIZE, proc, 1);
3521 mutex_unlock(&binder_lock);
3523 if (p > page + PAGE_SIZE)
3524 p = page + PAGE_SIZE;
3525 *start = page + off;
3533 return len < count ? len : count;
3536 static char *print_binder_transaction_log_entry(char *buf, char *end, struct binder_transaction_log_entry *e)
3538 buf += snprintf(buf, end - buf, "%d: %s from %d:%d to %d:%d node %d handle %d size %d:%d\n",
3539 e->debug_id, (e->call_type == 2) ? "reply" :
3540 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
3541 e->from_thread, e->to_proc, e->to_thread, e->to_node,
3542 e->target_handle, e->data_size, e->offsets_size);
3546 static int binder_read_proc_transaction_log(
3547 char *page, char **start, off_t off, int count, int *eof, void *data)
3549 struct binder_transaction_log *log = data;
3553 char *end = page + PAGE_SIZE;
3559 for (i = log->next; i < ARRAY_SIZE(log->entry); i++) {
3562 buf = print_binder_transaction_log_entry(buf, end, &log->entry[i]);
3565 for (i = 0; i < log->next; i++) {
3568 buf = print_binder_transaction_log_entry(buf, end, &log->entry[i]);
3571 *start = page + off;
3579 return len < count ? len : count;
3582 static struct file_operations binder_fops = {
3583 .owner = THIS_MODULE,
3584 .poll = binder_poll,
3585 .unlocked_ioctl = binder_ioctl,
3586 .mmap = binder_mmap,
3587 .open = binder_open,
3588 .flush = binder_flush,
3589 .release = binder_release,
3592 static struct miscdevice binder_miscdev = {
3593 .minor = MISC_DYNAMIC_MINOR,
3595 .fops = &binder_fops
3598 static int __init binder_init(void)
3602 binder_proc_dir_entry_root = proc_mkdir("binder", NULL);
3603 if (binder_proc_dir_entry_root)
3604 binder_proc_dir_entry_proc = proc_mkdir("proc", binder_proc_dir_entry_root);
3605 ret = misc_register(&binder_miscdev);
3606 if (binder_proc_dir_entry_root) {
3607 create_proc_read_entry("state", S_IRUGO, binder_proc_dir_entry_root, binder_read_proc_state, NULL);
3608 create_proc_read_entry("stats", S_IRUGO, binder_proc_dir_entry_root, binder_read_proc_stats, NULL);
3609 create_proc_read_entry("transactions", S_IRUGO, binder_proc_dir_entry_root, binder_read_proc_transactions, NULL);
3610 create_proc_read_entry("transaction_log", S_IRUGO, binder_proc_dir_entry_root, binder_read_proc_transaction_log, &binder_transaction_log);
3611 create_proc_read_entry("failed_transaction_log", S_IRUGO, binder_proc_dir_entry_root, binder_read_proc_transaction_log, &binder_transaction_log_failed);
3616 device_initcall(binder_init);
3618 MODULE_LICENSE("GPL v2");