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;
45 static int binder_read_proc_proc(
46 char *page, char **start, off_t off, int count, int *eof, void *data);
48 /* This is only defined in include/asm-arm/sizes.h */
54 #define SZ_4M 0x400000
58 #define FORBIDDEN_MMAP_FLAGS (VM_WRITE | VM_EXEC)
60 #define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
63 #define BINDER_SMALL_BUF_SIZE (PAGE_SIZE * 64)
66 BINDER_DEBUG_USER_ERROR = 1U << 0,
67 BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1,
68 BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2,
69 BINDER_DEBUG_OPEN_CLOSE = 1U << 3,
70 BINDER_DEBUG_DEAD_BINDER = 1U << 4,
71 BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5,
72 BINDER_DEBUG_READ_WRITE = 1U << 6,
73 BINDER_DEBUG_USER_REFS = 1U << 7,
74 BINDER_DEBUG_THREADS = 1U << 8,
75 BINDER_DEBUG_TRANSACTION = 1U << 9,
76 BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10,
77 BINDER_DEBUG_FREE_BUFFER = 1U << 11,
78 BINDER_DEBUG_INTERNAL_REFS = 1U << 12,
79 BINDER_DEBUG_BUFFER_ALLOC = 1U << 13,
80 BINDER_DEBUG_PRIORITY_CAP = 1U << 14,
81 BINDER_DEBUG_BUFFER_ALLOC_ASYNC = 1U << 15,
83 static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR |
84 BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION;
85 module_param_named(debug_mask, binder_debug_mask, uint, S_IWUSR | S_IRUGO);
86 static int binder_debug_no_lock;
87 module_param_named(proc_no_lock, binder_debug_no_lock, bool, S_IWUSR | S_IRUGO);
88 static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
89 static int binder_stop_on_user_error;
90 static int binder_set_stop_on_user_error(
91 const char *val, struct kernel_param *kp)
94 ret = param_set_int(val, kp);
95 if (binder_stop_on_user_error < 2)
96 wake_up(&binder_user_error_wait);
99 module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
100 param_get_int, &binder_stop_on_user_error, S_IWUSR | S_IRUGO);
102 #define binder_user_error(x...) \
104 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
105 printk(KERN_INFO x); \
106 if (binder_stop_on_user_error) \
107 binder_stop_on_user_error = 2; \
116 BINDER_STAT_TRANSACTION,
117 BINDER_STAT_TRANSACTION_COMPLETE,
121 struct binder_stats {
122 int br[_IOC_NR(BR_FAILED_REPLY) + 1];
123 int bc[_IOC_NR(BC_DEAD_BINDER_DONE) + 1];
124 int obj_created[BINDER_STAT_COUNT];
125 int obj_deleted[BINDER_STAT_COUNT];
128 static struct binder_stats binder_stats;
130 struct binder_transaction_log_entry {
142 struct binder_transaction_log {
145 struct binder_transaction_log_entry entry[32];
147 struct binder_transaction_log binder_transaction_log;
148 struct binder_transaction_log binder_transaction_log_failed;
150 static struct binder_transaction_log_entry *binder_transaction_log_add(
151 struct binder_transaction_log *log)
153 struct binder_transaction_log_entry *e;
154 e = &log->entry[log->next];
155 memset(e, 0, sizeof(*e));
157 if (log->next == ARRAY_SIZE(log->entry)) {
165 struct list_head entry;
167 BINDER_WORK_TRANSACTION = 1,
168 BINDER_WORK_TRANSACTION_COMPLETE,
170 BINDER_WORK_DEAD_BINDER,
171 BINDER_WORK_DEAD_BINDER_AND_CLEAR,
172 BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
178 struct binder_work work;
180 struct rb_node rb_node;
181 struct hlist_node dead_node;
183 struct binder_proc *proc;
184 struct hlist_head refs;
185 int internal_strong_refs;
187 int local_strong_refs;
190 unsigned has_strong_ref : 1;
191 unsigned pending_strong_ref : 1;
192 unsigned has_weak_ref : 1;
193 unsigned pending_weak_ref : 1;
194 unsigned has_async_transaction : 1;
195 unsigned accept_fds : 1;
196 int min_priority : 8;
197 struct list_head async_todo;
200 struct binder_ref_death {
201 struct binder_work work;
206 /* Lookups needed: */
207 /* node + proc => ref (transaction) */
208 /* desc + proc => ref (transaction, inc/dec ref) */
209 /* node => refs + procs (proc exit) */
211 struct rb_node rb_node_desc;
212 struct rb_node rb_node_node;
213 struct hlist_node node_entry;
214 struct binder_proc *proc;
215 struct binder_node *node;
219 struct binder_ref_death *death;
222 struct binder_buffer {
223 struct list_head entry; /* free and allocated entries by addesss */
224 struct rb_node rb_node; /* free entry by size or allocated entry */
227 unsigned allow_user_free : 1;
228 unsigned async_transaction : 1;
229 unsigned debug_id : 29;
231 struct binder_transaction *transaction;
233 struct binder_node *target_node;
240 struct hlist_node proc_node;
241 struct rb_root threads;
242 struct rb_root nodes;
243 struct rb_root refs_by_desc;
244 struct rb_root refs_by_node;
246 struct vm_area_struct *vma;
247 struct task_struct *tsk;
249 size_t user_buffer_offset;
251 struct list_head buffers;
252 struct rb_root free_buffers;
253 struct rb_root allocated_buffers;
254 size_t free_async_space;
258 uint32_t buffer_free;
259 struct list_head todo;
260 wait_queue_head_t wait;
261 struct binder_stats stats;
262 struct list_head delivered_death;
264 int requested_threads;
265 int requested_threads_started;
267 long default_priority;
271 BINDER_LOOPER_STATE_REGISTERED = 0x01,
272 BINDER_LOOPER_STATE_ENTERED = 0x02,
273 BINDER_LOOPER_STATE_EXITED = 0x04,
274 BINDER_LOOPER_STATE_INVALID = 0x08,
275 BINDER_LOOPER_STATE_WAITING = 0x10,
276 BINDER_LOOPER_STATE_NEED_RETURN = 0x20
279 struct binder_thread {
280 struct binder_proc *proc;
281 struct rb_node rb_node;
284 struct binder_transaction *transaction_stack;
285 struct list_head todo;
286 uint32_t return_error; /* Write failed, return error code in read buf */
287 uint32_t return_error2; /* Write failed, return error code in read */
288 /* buffer. Used when sending a reply to a dead process that */
289 /* we are also waiting on */
290 wait_queue_head_t wait;
291 struct binder_stats stats;
294 struct binder_transaction {
296 struct binder_work work;
297 struct binder_thread *from;
298 struct binder_transaction *from_parent;
299 struct binder_proc *to_proc;
300 struct binder_thread *to_thread;
301 struct binder_transaction *to_parent;
302 unsigned need_reply : 1;
303 /*unsigned is_dead : 1;*/ /* not used at the moment */
305 struct binder_buffer *buffer;
314 * copied from get_unused_fd_flags
316 int task_get_unused_fd_flags(struct task_struct *tsk, int flags)
318 struct files_struct *files = get_files_struct(tsk);
321 unsigned long rlim_cur;
327 spin_lock(&files->file_lock);
330 fdt = files_fdtable(files);
331 fd = find_next_zero_bit(fdt->open_fds->fds_bits, fdt->max_fds,
335 * N.B. For clone tasks sharing a files structure, this test
336 * will limit the total number of files that can be opened.
340 rlim_cur = tsk->signal->rlim[RLIMIT_NOFILE].rlim_cur;
347 /* Do we need to expand the fd array or fd set? */
348 error = expand_files(files, fd);
354 * If we needed to expand the fs array we
355 * might have blocked - try again.
361 FD_SET(fd, fdt->open_fds);
362 if (flags & O_CLOEXEC)
363 FD_SET(fd, fdt->close_on_exec);
365 FD_CLR(fd, fdt->close_on_exec);
366 files->next_fd = fd + 1;
369 if (fdt->fd[fd] != NULL) {
370 printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
377 spin_unlock(&files->file_lock);
378 put_files_struct(files);
383 * copied from fd_install
385 static void task_fd_install(
386 struct task_struct *tsk, unsigned int fd, struct file *file)
388 struct files_struct *files = get_files_struct(tsk);
394 spin_lock(&files->file_lock);
395 fdt = files_fdtable(files);
396 BUG_ON(fdt->fd[fd] != NULL);
397 rcu_assign_pointer(fdt->fd[fd], file);
398 spin_unlock(&files->file_lock);
399 put_files_struct(files);
403 * copied from __put_unused_fd in open.c
405 static void __put_unused_fd(struct files_struct *files, unsigned int fd)
407 struct fdtable *fdt = files_fdtable(files);
408 __FD_CLR(fd, fdt->open_fds);
409 if (fd < files->next_fd)
414 * copied from sys_close
416 static long task_close_fd(struct task_struct *tsk, unsigned int fd)
419 struct files_struct *files = get_files_struct(tsk);
426 spin_lock(&files->file_lock);
427 fdt = files_fdtable(files);
428 if (fd >= fdt->max_fds)
433 rcu_assign_pointer(fdt->fd[fd], NULL);
434 FD_CLR(fd, fdt->close_on_exec);
435 __put_unused_fd(files, fd);
436 spin_unlock(&files->file_lock);
437 retval = filp_close(filp, files);
439 /* can't restart close syscall because file table entry was cleared */
440 if (unlikely(retval == -ERESTARTSYS ||
441 retval == -ERESTARTNOINTR ||
442 retval == -ERESTARTNOHAND ||
443 retval == -ERESTART_RESTARTBLOCK))
446 put_files_struct(files);
450 spin_unlock(&files->file_lock);
451 put_files_struct(files);
455 static void binder_set_nice(long nice)
458 if (can_nice(current, nice)) {
459 set_user_nice(current, nice);
462 min_nice = 20 - current->signal->rlim[RLIMIT_NICE].rlim_cur;
463 if (binder_debug_mask & BINDER_DEBUG_PRIORITY_CAP)
464 printk(KERN_INFO "binder: %d: nice value %ld not allowed use "
465 "%ld instead\n", current->pid, nice, min_nice);
466 set_user_nice(current, min_nice);
469 binder_user_error("binder: %d RLIMIT_NICE not set\n", current->pid);
472 static size_t binder_buffer_size(
473 struct binder_proc *proc, struct binder_buffer *buffer)
475 if (list_is_last(&buffer->entry, &proc->buffers))
476 return proc->buffer + proc->buffer_size - (void *)buffer->data;
478 return (size_t)list_entry(buffer->entry.next,
479 struct binder_buffer, entry) - (size_t)buffer->data;
482 static void binder_insert_free_buffer(
483 struct binder_proc *proc, struct binder_buffer *new_buffer)
485 struct rb_node **p = &proc->free_buffers.rb_node;
486 struct rb_node *parent = NULL;
487 struct binder_buffer *buffer;
489 size_t new_buffer_size;
491 BUG_ON(!new_buffer->free);
493 new_buffer_size = binder_buffer_size(proc, new_buffer);
495 if (binder_debug_mask & BINDER_DEBUG_BUFFER_ALLOC)
496 printk(KERN_INFO "binder: %d: add free buffer, size %zd, "
497 "at %p\n", proc->pid, new_buffer_size, new_buffer);
501 buffer = rb_entry(parent, struct binder_buffer, rb_node);
502 BUG_ON(!buffer->free);
504 buffer_size = binder_buffer_size(proc, buffer);
506 if (new_buffer_size < buffer_size)
507 p = &parent->rb_left;
509 p = &parent->rb_right;
511 rb_link_node(&new_buffer->rb_node, parent, p);
512 rb_insert_color(&new_buffer->rb_node, &proc->free_buffers);
515 static void binder_insert_allocated_buffer(
516 struct binder_proc *proc, struct binder_buffer *new_buffer)
518 struct rb_node **p = &proc->allocated_buffers.rb_node;
519 struct rb_node *parent = NULL;
520 struct binder_buffer *buffer;
522 BUG_ON(new_buffer->free);
526 buffer = rb_entry(parent, struct binder_buffer, rb_node);
527 BUG_ON(buffer->free);
529 if (new_buffer < buffer)
530 p = &parent->rb_left;
531 else if (new_buffer > buffer)
532 p = &parent->rb_right;
536 rb_link_node(&new_buffer->rb_node, parent, p);
537 rb_insert_color(&new_buffer->rb_node, &proc->allocated_buffers);
540 static struct binder_buffer *binder_buffer_lookup(
541 struct binder_proc *proc, void __user *user_ptr)
543 struct rb_node *n = proc->allocated_buffers.rb_node;
544 struct binder_buffer *buffer;
545 struct binder_buffer *kern_ptr;
547 kern_ptr = user_ptr - proc->user_buffer_offset
548 - offsetof(struct binder_buffer, data);
551 buffer = rb_entry(n, struct binder_buffer, rb_node);
552 BUG_ON(buffer->free);
554 if (kern_ptr < buffer)
556 else if (kern_ptr > buffer)
564 static int binder_update_page_range(struct binder_proc *proc, int allocate,
565 void *start, void *end, struct vm_area_struct *vma)
568 unsigned long user_page_addr;
569 struct vm_struct tmp_area;
571 struct mm_struct *mm;
573 if (binder_debug_mask & BINDER_DEBUG_BUFFER_ALLOC)
574 printk(KERN_INFO "binder: %d: %s pages %p-%p\n",
575 proc->pid, allocate ? "allocate" : "free", start, end);
583 mm = get_task_mm(proc->tsk);
586 down_write(&mm->mmap_sem);
594 printk(KERN_ERR "binder: %d: binder_alloc_buf failed to "
595 "map pages in userspace, no vma\n", proc->pid);
599 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
601 struct page **page_array_ptr;
602 page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
605 *page = alloc_page(GFP_KERNEL | __GFP_ZERO);
607 printk(KERN_ERR "binder: %d: binder_alloc_buf failed "
608 "for page at %p\n", proc->pid, page_addr);
609 goto err_alloc_page_failed;
611 tmp_area.addr = page_addr;
612 tmp_area.size = PAGE_SIZE + PAGE_SIZE /* guard page? */;
613 page_array_ptr = page;
614 ret = map_vm_area(&tmp_area, PAGE_KERNEL, &page_array_ptr);
616 printk(KERN_ERR "binder: %d: binder_alloc_buf failed "
617 "to map page at %p in kernel\n",
618 proc->pid, page_addr);
619 goto err_map_kernel_failed;
621 user_page_addr = (size_t)page_addr + proc->user_buffer_offset;
622 ret = vm_insert_page(vma, user_page_addr, page[0]);
624 printk(KERN_ERR "binder: %d: binder_alloc_buf failed "
625 "to map page at %lx in userspace\n",
626 proc->pid, user_page_addr);
627 goto err_vm_insert_page_failed;
629 /* vm_insert_page does not seem to increment the refcount */
632 up_write(&mm->mmap_sem);
638 for (page_addr = end - PAGE_SIZE; page_addr >= start;
639 page_addr -= PAGE_SIZE) {
640 page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
642 zap_page_range(vma, (size_t)page_addr +
643 proc->user_buffer_offset, PAGE_SIZE, NULL);
644 err_vm_insert_page_failed:
645 unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
646 err_map_kernel_failed:
649 err_alloc_page_failed:
654 up_write(&mm->mmap_sem);
660 static struct binder_buffer *binder_alloc_buf(struct binder_proc *proc,
661 size_t data_size, size_t offsets_size, int is_async)
663 struct rb_node *n = proc->free_buffers.rb_node;
664 struct binder_buffer *buffer;
666 struct rb_node *best_fit = NULL;
671 if (proc->vma == NULL) {
672 printk(KERN_ERR "binder: %d: binder_alloc_buf, no vma\n",
677 size = ALIGN(data_size, sizeof(void *)) +
678 ALIGN(offsets_size, sizeof(void *));
680 if (size < data_size || size < offsets_size) {
681 binder_user_error("binder: %d: got transaction with invalid "
682 "size %zd-%zd\n", proc->pid, data_size, offsets_size);
687 proc->free_async_space < size + sizeof(struct binder_buffer)) {
688 if (binder_debug_mask & BINDER_DEBUG_BUFFER_ALLOC)
689 printk(KERN_ERR "binder: %d: binder_alloc_buf size %zd f"
690 "ailed, no async space left\n", proc->pid, size);
695 buffer = rb_entry(n, struct binder_buffer, rb_node);
696 BUG_ON(!buffer->free);
697 buffer_size = binder_buffer_size(proc, buffer);
699 if (size < buffer_size) {
702 } else if (size > buffer_size)
709 if (best_fit == NULL) {
710 printk(KERN_ERR "binder: %d: binder_alloc_buf size %zd failed, "
711 "no address space\n", proc->pid, size);
715 buffer = rb_entry(best_fit, struct binder_buffer, rb_node);
716 buffer_size = binder_buffer_size(proc, buffer);
718 if (binder_debug_mask & BINDER_DEBUG_BUFFER_ALLOC)
719 printk(KERN_INFO "binder: %d: binder_alloc_buf size %zd got buff"
720 "er %p size %zd\n", proc->pid, size, buffer, buffer_size);
723 (void *)(((size_t)buffer->data + buffer_size) & PAGE_MASK);
725 if (size + sizeof(struct binder_buffer) + 4 >= buffer_size)
726 buffer_size = size; /* no room for other buffers */
728 buffer_size = size + sizeof(struct binder_buffer);
730 end_page_addr = (void *)PAGE_ALIGN((size_t)buffer->data + buffer_size);
731 if (end_page_addr > has_page_addr)
732 end_page_addr = has_page_addr;
733 if (binder_update_page_range(proc, 1,
734 (void *)PAGE_ALIGN((size_t)buffer->data), end_page_addr, NULL))
737 rb_erase(best_fit, &proc->free_buffers);
739 binder_insert_allocated_buffer(proc, buffer);
740 if (buffer_size != size) {
741 struct binder_buffer *new_buffer = (void *)buffer->data + size;
742 list_add(&new_buffer->entry, &buffer->entry);
743 new_buffer->free = 1;
744 binder_insert_free_buffer(proc, new_buffer);
746 if (binder_debug_mask & BINDER_DEBUG_BUFFER_ALLOC)
747 printk(KERN_INFO "binder: %d: binder_alloc_buf size %zd got "
748 "%p\n", proc->pid, size, buffer);
749 buffer->data_size = data_size;
750 buffer->offsets_size = offsets_size;
751 buffer->async_transaction = is_async;
753 proc->free_async_space -= size + sizeof(struct binder_buffer);
754 if (binder_debug_mask & BINDER_DEBUG_BUFFER_ALLOC_ASYNC)
755 printk(KERN_INFO "binder: %d: binder_alloc_buf size %zd "
756 "async free %zd\n", proc->pid, size,
757 proc->free_async_space);
763 static void *buffer_start_page(struct binder_buffer *buffer)
765 return (void *)((size_t)buffer & PAGE_MASK);
768 static void *buffer_end_page(struct binder_buffer *buffer)
770 return (void *)(((size_t)(buffer + 1) - 1) & PAGE_MASK);
773 static void binder_delete_free_buffer(
774 struct binder_proc *proc, struct binder_buffer *buffer)
776 struct binder_buffer *prev, *next = NULL;
777 int free_page_end = 1;
778 int free_page_start = 1;
780 BUG_ON(proc->buffers.next == &buffer->entry);
781 prev = list_entry(buffer->entry.prev, struct binder_buffer, entry);
783 if (buffer_end_page(prev) == buffer_start_page(buffer)) {
785 if (buffer_end_page(prev) == buffer_end_page(buffer))
787 if (binder_debug_mask & BINDER_DEBUG_BUFFER_ALLOC)
788 printk(KERN_INFO "binder: %d: merge free, buffer %p "
789 "share page with %p\n", proc->pid, buffer, prev);
792 if (!list_is_last(&buffer->entry, &proc->buffers)) {
793 next = list_entry(buffer->entry.next,
794 struct binder_buffer, entry);
795 if (buffer_start_page(next) == buffer_end_page(buffer)) {
797 if (buffer_start_page(next) ==
798 buffer_start_page(buffer))
800 if (binder_debug_mask & BINDER_DEBUG_BUFFER_ALLOC)
801 printk(KERN_INFO "binder: %d: merge free, "
802 "buffer %p share page with %p\n",
803 proc->pid, buffer, prev);
806 list_del(&buffer->entry);
807 if (free_page_start || free_page_end) {
808 if (binder_debug_mask & BINDER_DEBUG_BUFFER_ALLOC)
809 printk(KERN_INFO "binder: %d: merge free, buffer %p do "
810 "not share page%s%s with with %p or %p\n",
811 proc->pid, buffer, free_page_start ? "" : " end",
812 free_page_end ? "" : " start", prev, next);
813 binder_update_page_range(proc, 0, free_page_start ?
814 buffer_start_page(buffer) : buffer_end_page(buffer),
815 (free_page_end ? buffer_end_page(buffer) :
816 buffer_start_page(buffer)) + PAGE_SIZE, NULL);
820 static void binder_free_buf(
821 struct binder_proc *proc, struct binder_buffer *buffer)
823 size_t size, buffer_size;
825 buffer_size = binder_buffer_size(proc, buffer);
827 size = ALIGN(buffer->data_size, sizeof(void *)) +
828 ALIGN(buffer->offsets_size, sizeof(void *));
829 if (binder_debug_mask & BINDER_DEBUG_BUFFER_ALLOC)
830 printk(KERN_INFO "binder: %d: binder_free_buf %p size %zd buffer"
831 "_size %zd\n", proc->pid, buffer, size, buffer_size);
833 BUG_ON(buffer->free);
834 BUG_ON(size > buffer_size);
835 BUG_ON(buffer->transaction != NULL);
836 BUG_ON((void *)buffer < proc->buffer);
837 BUG_ON((void *)buffer > proc->buffer + proc->buffer_size);
839 if (buffer->async_transaction) {
840 proc->free_async_space += size + sizeof(struct binder_buffer);
841 if (binder_debug_mask & BINDER_DEBUG_BUFFER_ALLOC_ASYNC)
842 printk(KERN_INFO "binder: %d: binder_free_buf size %zd "
843 "async free %zd\n", proc->pid, size,
844 proc->free_async_space);
847 binder_update_page_range(proc, 0,
848 (void *)PAGE_ALIGN((size_t)buffer->data),
849 (void *)(((size_t)buffer->data + buffer_size) & PAGE_MASK),
851 rb_erase(&buffer->rb_node, &proc->allocated_buffers);
853 if (!list_is_last(&buffer->entry, &proc->buffers)) {
854 struct binder_buffer *next = list_entry(buffer->entry.next,
855 struct binder_buffer, entry);
857 rb_erase(&next->rb_node, &proc->free_buffers);
858 binder_delete_free_buffer(proc, next);
861 if (proc->buffers.next != &buffer->entry) {
862 struct binder_buffer *prev = list_entry(buffer->entry.prev,
863 struct binder_buffer, entry);
865 binder_delete_free_buffer(proc, buffer);
866 rb_erase(&prev->rb_node, &proc->free_buffers);
870 binder_insert_free_buffer(proc, buffer);
873 static struct binder_node *
874 binder_get_node(struct binder_proc *proc, void __user *ptr)
876 struct rb_node *n = proc->nodes.rb_node;
877 struct binder_node *node;
880 node = rb_entry(n, struct binder_node, rb_node);
884 else if (ptr > node->ptr)
892 static struct binder_node *
893 binder_new_node(struct binder_proc *proc, void __user *ptr, void __user *cookie)
895 struct rb_node **p = &proc->nodes.rb_node;
896 struct rb_node *parent = NULL;
897 struct binder_node *node;
901 node = rb_entry(parent, struct binder_node, rb_node);
905 else if (ptr > node->ptr)
911 node = kzalloc(sizeof(*node), GFP_KERNEL);
914 binder_stats.obj_created[BINDER_STAT_NODE]++;
915 rb_link_node(&node->rb_node, parent, p);
916 rb_insert_color(&node->rb_node, &proc->nodes);
917 node->debug_id = ++binder_last_id;
920 node->cookie = cookie;
921 node->work.type = BINDER_WORK_NODE;
922 INIT_LIST_HEAD(&node->work.entry);
923 INIT_LIST_HEAD(&node->async_todo);
924 if (binder_debug_mask & BINDER_DEBUG_INTERNAL_REFS)
925 printk(KERN_INFO "binder: %d:%d node %d u%p c%p created\n",
926 proc->pid, current->pid, node->debug_id,
927 node->ptr, node->cookie);
932 binder_inc_node(struct binder_node *node, int strong, int internal,
933 struct list_head *target_list)
937 if (target_list == NULL &&
938 node->internal_strong_refs == 0 &&
939 !(node == binder_context_mgr_node &&
940 node->has_strong_ref)) {
941 printk(KERN_ERR "binder: invalid inc strong "
942 "node for %d\n", node->debug_id);
945 node->internal_strong_refs++;
947 node->local_strong_refs++;
948 if (!node->has_strong_ref && target_list) {
949 list_del_init(&node->work.entry);
950 list_add_tail(&node->work.entry, target_list);
954 node->local_weak_refs++;
955 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
956 if (target_list == NULL) {
957 printk(KERN_ERR "binder: invalid inc weak node "
958 "for %d\n", node->debug_id);
961 list_add_tail(&node->work.entry, target_list);
968 binder_dec_node(struct binder_node *node, int strong, int internal)
972 node->internal_strong_refs--;
974 node->local_strong_refs--;
975 if (node->local_strong_refs || node->internal_strong_refs)
979 node->local_weak_refs--;
980 if (node->local_weak_refs || !hlist_empty(&node->refs))
983 if (node->proc && (node->has_strong_ref || node->has_weak_ref)) {
984 if (list_empty(&node->work.entry)) {
985 list_add_tail(&node->work.entry, &node->proc->todo);
986 wake_up_interruptible(&node->proc->wait);
989 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
990 !node->local_weak_refs) {
991 list_del_init(&node->work.entry);
993 rb_erase(&node->rb_node, &node->proc->nodes);
994 if (binder_debug_mask & BINDER_DEBUG_INTERNAL_REFS)
995 printk(KERN_INFO "binder: refless node %d deleted\n", node->debug_id);
997 hlist_del(&node->dead_node);
998 if (binder_debug_mask & BINDER_DEBUG_INTERNAL_REFS)
999 printk(KERN_INFO "binder: dead node %d deleted\n", node->debug_id);
1002 binder_stats.obj_deleted[BINDER_STAT_NODE]++;
1010 static struct binder_ref *
1011 binder_get_ref(struct binder_proc *proc, uint32_t desc)
1013 struct rb_node *n = proc->refs_by_desc.rb_node;
1014 struct binder_ref *ref;
1017 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1019 if (desc < ref->desc)
1021 else if (desc > ref->desc)
1029 static struct binder_ref *
1030 binder_get_ref_for_node(struct binder_proc *proc, struct binder_node *node)
1033 struct rb_node **p = &proc->refs_by_node.rb_node;
1034 struct rb_node *parent = NULL;
1035 struct binder_ref *ref, *new_ref;
1039 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1041 if (node < ref->node)
1043 else if (node > ref->node)
1044 p = &(*p)->rb_right;
1048 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1049 if (new_ref == NULL)
1051 binder_stats.obj_created[BINDER_STAT_REF]++;
1052 new_ref->debug_id = ++binder_last_id;
1053 new_ref->proc = proc;
1054 new_ref->node = node;
1055 rb_link_node(&new_ref->rb_node_node, parent, p);
1056 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1058 new_ref->desc = (node == binder_context_mgr_node) ? 0 : 1;
1059 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1060 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1061 if (ref->desc > new_ref->desc)
1063 new_ref->desc = ref->desc + 1;
1066 p = &proc->refs_by_desc.rb_node;
1069 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1071 if (new_ref->desc < ref->desc)
1073 else if (new_ref->desc > ref->desc)
1074 p = &(*p)->rb_right;
1078 rb_link_node(&new_ref->rb_node_desc, parent, p);
1079 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
1081 hlist_add_head(&new_ref->node_entry, &node->refs);
1082 if (binder_debug_mask & BINDER_DEBUG_INTERNAL_REFS)
1083 printk(KERN_INFO "binder: %d new ref %d desc %d for "
1084 "node %d\n", proc->pid, new_ref->debug_id,
1085 new_ref->desc, node->debug_id);
1087 if (binder_debug_mask & BINDER_DEBUG_INTERNAL_REFS)
1088 printk(KERN_INFO "binder: %d new ref %d desc %d for "
1089 "dead node\n", proc->pid, new_ref->debug_id,
1096 binder_delete_ref(struct binder_ref *ref)
1098 if (binder_debug_mask & BINDER_DEBUG_INTERNAL_REFS)
1099 printk(KERN_INFO "binder: %d delete ref %d desc %d for "
1100 "node %d\n", ref->proc->pid, ref->debug_id,
1101 ref->desc, ref->node->debug_id);
1102 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1103 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
1105 binder_dec_node(ref->node, 1, 1);
1106 hlist_del(&ref->node_entry);
1107 binder_dec_node(ref->node, 0, 1);
1109 if (binder_debug_mask & BINDER_DEBUG_DEAD_BINDER)
1110 printk(KERN_INFO "binder: %d delete ref %d desc %d "
1111 "has death notification\n", ref->proc->pid,
1112 ref->debug_id, ref->desc);
1113 list_del(&ref->death->work.entry);
1115 binder_stats.obj_deleted[BINDER_STAT_DEATH]++;
1118 binder_stats.obj_deleted[BINDER_STAT_REF]++;
1123 struct binder_ref *ref, int strong, struct list_head *target_list)
1127 if (ref->strong == 0) {
1128 ret = binder_inc_node(ref->node, 1, 1, target_list);
1134 if (ref->weak == 0) {
1135 ret = binder_inc_node(ref->node, 0, 1, target_list);
1146 binder_dec_ref(struct binder_ref *ref, int strong)
1149 if (ref->strong == 0) {
1150 binder_user_error("binder: %d invalid dec strong, "
1151 "ref %d desc %d s %d w %d\n",
1152 ref->proc->pid, ref->debug_id,
1153 ref->desc, ref->strong, ref->weak);
1157 if (ref->strong == 0) {
1159 ret = binder_dec_node(ref->node, strong, 1);
1164 if (ref->weak == 0) {
1165 binder_user_error("binder: %d invalid dec weak, "
1166 "ref %d desc %d s %d w %d\n",
1167 ref->proc->pid, ref->debug_id,
1168 ref->desc, ref->strong, ref->weak);
1173 if (ref->strong == 0 && ref->weak == 0)
1174 binder_delete_ref(ref);
1179 binder_pop_transaction(
1180 struct binder_thread *target_thread, struct binder_transaction *t)
1182 if (target_thread) {
1183 BUG_ON(target_thread->transaction_stack != t);
1184 BUG_ON(target_thread->transaction_stack->from != target_thread);
1185 target_thread->transaction_stack =
1186 target_thread->transaction_stack->from_parent;
1191 t->buffer->transaction = NULL;
1193 binder_stats.obj_deleted[BINDER_STAT_TRANSACTION]++;
1197 binder_send_failed_reply(struct binder_transaction *t, uint32_t error_code)
1199 struct binder_thread *target_thread;
1200 BUG_ON(t->flags & TF_ONE_WAY);
1202 target_thread = t->from;
1203 if (target_thread) {
1204 if (target_thread->return_error != BR_OK &&
1205 target_thread->return_error2 == BR_OK) {
1206 target_thread->return_error2 =
1207 target_thread->return_error;
1208 target_thread->return_error = BR_OK;
1210 if (target_thread->return_error == BR_OK) {
1211 if (binder_debug_mask & BINDER_DEBUG_FAILED_TRANSACTION)
1212 printk(KERN_INFO "binder: send failed reply for transaction %d to %d:%d\n",
1213 t->debug_id, target_thread->proc->pid, target_thread->pid);
1215 binder_pop_transaction(target_thread, t);
1216 target_thread->return_error = error_code;
1217 wake_up_interruptible(&target_thread->wait);
1219 printk(KERN_ERR "binder: reply failed, target "
1220 "thread, %d:%d, has error code %d "
1221 "already\n", target_thread->proc->pid,
1223 target_thread->return_error);
1227 struct binder_transaction *next = t->from_parent;
1229 if (binder_debug_mask & BINDER_DEBUG_FAILED_TRANSACTION)
1230 printk(KERN_INFO "binder: send failed reply "
1231 "for transaction %d, target dead\n",
1234 binder_pop_transaction(target_thread, t);
1236 if (binder_debug_mask & BINDER_DEBUG_DEAD_BINDER)
1237 printk(KERN_INFO "binder: reply failed,"
1238 " no target thread at root\n");
1242 if (binder_debug_mask & BINDER_DEBUG_DEAD_BINDER)
1243 printk(KERN_INFO "binder: reply failed, no targ"
1244 "et thread -- retry %d\n", t->debug_id);
1250 binder_transaction_buffer_release(struct binder_proc *proc,
1251 struct binder_buffer *buffer, size_t *failed_at);
1254 binder_transaction(struct binder_proc *proc, struct binder_thread *thread,
1255 struct binder_transaction_data *tr, int reply)
1257 struct binder_transaction *t;
1258 struct binder_work *tcomplete;
1259 size_t *offp, *off_end;
1260 struct binder_proc *target_proc;
1261 struct binder_thread *target_thread = NULL;
1262 struct binder_node *target_node = NULL;
1263 struct list_head *target_list;
1264 wait_queue_head_t *target_wait;
1265 struct binder_transaction *in_reply_to = NULL;
1266 struct binder_transaction_log_entry *e;
1267 uint32_t return_error;
1269 e = binder_transaction_log_add(&binder_transaction_log);
1270 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
1271 e->from_proc = proc->pid;
1272 e->from_thread = thread->pid;
1273 e->target_handle = tr->target.handle;
1274 e->data_size = tr->data_size;
1275 e->offsets_size = tr->offsets_size;
1278 in_reply_to = thread->transaction_stack;
1279 if (in_reply_to == NULL) {
1280 binder_user_error("binder: %d:%d got reply transaction "
1281 "with no transaction stack\n",
1282 proc->pid, thread->pid);
1283 return_error = BR_FAILED_REPLY;
1284 goto err_empty_call_stack;
1286 binder_set_nice(in_reply_to->saved_priority);
1287 if (in_reply_to->to_thread != thread) {
1288 binder_user_error("binder: %d:%d got reply transaction "
1289 "with bad transaction stack,"
1290 " transaction %d has target %d:%d\n",
1291 proc->pid, thread->pid, in_reply_to->debug_id,
1292 in_reply_to->to_proc ?
1293 in_reply_to->to_proc->pid : 0,
1294 in_reply_to->to_thread ?
1295 in_reply_to->to_thread->pid : 0);
1296 return_error = BR_FAILED_REPLY;
1298 goto err_bad_call_stack;
1300 thread->transaction_stack = in_reply_to->to_parent;
1301 target_thread = in_reply_to->from;
1302 if (target_thread == NULL) {
1303 return_error = BR_DEAD_REPLY;
1304 goto err_dead_binder;
1306 if (target_thread->transaction_stack != in_reply_to) {
1307 binder_user_error("binder: %d:%d got reply transaction "
1308 "with bad target transaction stack %d, "
1310 proc->pid, thread->pid,
1311 target_thread->transaction_stack ?
1312 target_thread->transaction_stack->debug_id : 0,
1313 in_reply_to->debug_id);
1314 return_error = BR_FAILED_REPLY;
1316 target_thread = NULL;
1317 goto err_dead_binder;
1319 target_proc = target_thread->proc;
1321 if (tr->target.handle) {
1322 struct binder_ref *ref;
1323 ref = binder_get_ref(proc, tr->target.handle);
1325 binder_user_error("binder: %d:%d got "
1326 "transaction to invalid handle\n",
1327 proc->pid, thread->pid);
1328 return_error = BR_FAILED_REPLY;
1329 goto err_invalid_target_handle;
1331 target_node = ref->node;
1333 target_node = binder_context_mgr_node;
1334 if (target_node == NULL) {
1335 return_error = BR_DEAD_REPLY;
1336 goto err_no_context_mgr_node;
1339 e->to_node = target_node->debug_id;
1340 target_proc = target_node->proc;
1341 if (target_proc == NULL) {
1342 return_error = BR_DEAD_REPLY;
1343 goto err_dead_binder;
1345 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
1346 struct binder_transaction *tmp;
1347 tmp = thread->transaction_stack;
1349 if (tmp->from && tmp->from->proc == target_proc)
1350 target_thread = tmp->from;
1351 tmp = tmp->from_parent;
1355 if (target_thread) {
1356 e->to_thread = target_thread->pid;
1357 target_list = &target_thread->todo;
1358 target_wait = &target_thread->wait;
1360 target_list = &target_proc->todo;
1361 target_wait = &target_proc->wait;
1363 e->to_proc = target_proc->pid;
1365 /* TODO: reuse incoming transaction for reply */
1366 t = kzalloc(sizeof(*t), GFP_KERNEL);
1368 return_error = BR_FAILED_REPLY;
1369 goto err_alloc_t_failed;
1371 binder_stats.obj_created[BINDER_STAT_TRANSACTION]++;
1373 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
1374 if (tcomplete == NULL) {
1375 return_error = BR_FAILED_REPLY;
1376 goto err_alloc_tcomplete_failed;
1378 binder_stats.obj_created[BINDER_STAT_TRANSACTION_COMPLETE]++;
1380 t->debug_id = ++binder_last_id;
1381 e->debug_id = t->debug_id;
1383 if (binder_debug_mask & BINDER_DEBUG_TRANSACTION) {
1385 printk(KERN_INFO "binder: %d:%d BC_REPLY %d -> %d:%d, "
1386 "data %p-%p size %zd-%zd\n",
1387 proc->pid, thread->pid, t->debug_id,
1388 target_proc->pid, target_thread->pid,
1389 tr->data.ptr.buffer, tr->data.ptr.offsets,
1390 tr->data_size, tr->offsets_size);
1392 printk(KERN_INFO "binder: %d:%d BC_TRANSACTION %d -> "
1393 "%d - node %d, data %p-%p size %zd-%zd\n",
1394 proc->pid, thread->pid, t->debug_id,
1395 target_proc->pid, target_node->debug_id,
1396 tr->data.ptr.buffer, tr->data.ptr.offsets,
1397 tr->data_size, tr->offsets_size);
1400 if (!reply && !(tr->flags & TF_ONE_WAY))
1404 t->sender_euid = proc->tsk->cred->euid;
1405 t->to_proc = target_proc;
1406 t->to_thread = target_thread;
1408 t->flags = tr->flags;
1409 t->priority = task_nice(current);
1410 t->buffer = binder_alloc_buf(target_proc, tr->data_size,
1411 tr->offsets_size, !reply && (t->flags & TF_ONE_WAY));
1412 if (t->buffer == NULL) {
1413 return_error = BR_FAILED_REPLY;
1414 goto err_binder_alloc_buf_failed;
1416 t->buffer->allow_user_free = 0;
1417 t->buffer->debug_id = t->debug_id;
1418 t->buffer->transaction = t;
1419 t->buffer->target_node = target_node;
1421 binder_inc_node(target_node, 1, 0, NULL);
1423 offp = (size_t *)(t->buffer->data + ALIGN(tr->data_size, sizeof(void *)));
1425 if (copy_from_user(t->buffer->data, tr->data.ptr.buffer, tr->data_size)) {
1426 binder_user_error("binder: %d:%d got transaction with invalid "
1427 "data ptr\n", proc->pid, thread->pid);
1428 return_error = BR_FAILED_REPLY;
1429 goto err_copy_data_failed;
1431 if (copy_from_user(offp, tr->data.ptr.offsets, tr->offsets_size)) {
1432 binder_user_error("binder: %d:%d got transaction with invalid "
1433 "offsets ptr\n", proc->pid, thread->pid);
1434 return_error = BR_FAILED_REPLY;
1435 goto err_copy_data_failed;
1437 off_end = (void *)offp + tr->offsets_size;
1438 for (; offp < off_end; offp++) {
1439 struct flat_binder_object *fp;
1440 if (*offp > t->buffer->data_size - sizeof(*fp)) {
1441 binder_user_error("binder: %d:%d got transaction with "
1442 "invalid offset, %zd\n",
1443 proc->pid, thread->pid, *offp);
1444 return_error = BR_FAILED_REPLY;
1445 goto err_bad_offset;
1447 fp = (struct flat_binder_object *)(t->buffer->data + *offp);
1449 case BINDER_TYPE_BINDER:
1450 case BINDER_TYPE_WEAK_BINDER: {
1451 struct binder_ref *ref;
1452 struct binder_node *node = binder_get_node(proc, fp->binder);
1454 node = binder_new_node(proc, fp->binder, fp->cookie);
1456 return_error = BR_FAILED_REPLY;
1457 goto err_binder_new_node_failed;
1459 node->min_priority = fp->flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
1460 node->accept_fds = !!(fp->flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
1462 if (fp->cookie != node->cookie) {
1463 binder_user_error("binder: %d:%d sending u%p "
1464 "node %d, cookie mismatch %p != %p\n",
1465 proc->pid, thread->pid,
1466 fp->binder, node->debug_id,
1467 fp->cookie, node->cookie);
1468 goto err_binder_get_ref_for_node_failed;
1470 ref = binder_get_ref_for_node(target_proc, node);
1472 return_error = BR_FAILED_REPLY;
1473 goto err_binder_get_ref_for_node_failed;
1475 if (fp->type == BINDER_TYPE_BINDER)
1476 fp->type = BINDER_TYPE_HANDLE;
1478 fp->type = BINDER_TYPE_WEAK_HANDLE;
1479 fp->handle = ref->desc;
1480 binder_inc_ref(ref, fp->type == BINDER_TYPE_HANDLE, &thread->todo);
1481 if (binder_debug_mask & BINDER_DEBUG_TRANSACTION)
1482 printk(KERN_INFO " node %d u%p -> ref %d desc %d\n",
1483 node->debug_id, node->ptr, ref->debug_id, ref->desc);
1485 case BINDER_TYPE_HANDLE:
1486 case BINDER_TYPE_WEAK_HANDLE: {
1487 struct binder_ref *ref = binder_get_ref(proc, fp->handle);
1489 binder_user_error("binder: %d:%d got "
1490 "transaction with invalid "
1491 "handle, %ld\n", proc->pid,
1492 thread->pid, fp->handle);
1493 return_error = BR_FAILED_REPLY;
1494 goto err_binder_get_ref_failed;
1496 if (ref->node->proc == target_proc) {
1497 if (fp->type == BINDER_TYPE_HANDLE)
1498 fp->type = BINDER_TYPE_BINDER;
1500 fp->type = BINDER_TYPE_WEAK_BINDER;
1501 fp->binder = ref->node->ptr;
1502 fp->cookie = ref->node->cookie;
1503 binder_inc_node(ref->node, fp->type == BINDER_TYPE_BINDER, 0, NULL);
1504 if (binder_debug_mask & BINDER_DEBUG_TRANSACTION)
1505 printk(KERN_INFO " ref %d desc %d -> node %d u%p\n",
1506 ref->debug_id, ref->desc, ref->node->debug_id, ref->node->ptr);
1508 struct binder_ref *new_ref;
1509 new_ref = binder_get_ref_for_node(target_proc, ref->node);
1510 if (new_ref == NULL) {
1511 return_error = BR_FAILED_REPLY;
1512 goto err_binder_get_ref_for_node_failed;
1514 fp->handle = new_ref->desc;
1515 binder_inc_ref(new_ref, fp->type == BINDER_TYPE_HANDLE, NULL);
1516 if (binder_debug_mask & BINDER_DEBUG_TRANSACTION)
1517 printk(KERN_INFO " ref %d desc %d -> ref %d desc %d (node %d)\n",
1518 ref->debug_id, ref->desc, new_ref->debug_id, new_ref->desc, ref->node->debug_id);
1522 case BINDER_TYPE_FD: {
1527 if (!(in_reply_to->flags & TF_ACCEPT_FDS)) {
1528 binder_user_error("binder: %d:%d got reply with fd, %ld, but target does not allow fds\n",
1529 proc->pid, thread->pid, fp->handle);
1530 return_error = BR_FAILED_REPLY;
1531 goto err_fd_not_allowed;
1533 } else if (!target_node->accept_fds) {
1534 binder_user_error("binder: %d:%d got transaction with fd, %ld, but target does not allow fds\n",
1535 proc->pid, thread->pid, fp->handle);
1536 return_error = BR_FAILED_REPLY;
1537 goto err_fd_not_allowed;
1540 file = fget(fp->handle);
1542 binder_user_error("binder: %d:%d got transaction with invalid fd, %ld\n",
1543 proc->pid, thread->pid, fp->handle);
1544 return_error = BR_FAILED_REPLY;
1545 goto err_fget_failed;
1547 target_fd = task_get_unused_fd_flags(target_proc->tsk, O_CLOEXEC);
1548 if (target_fd < 0) {
1550 return_error = BR_FAILED_REPLY;
1551 goto err_get_unused_fd_failed;
1553 task_fd_install(target_proc->tsk, target_fd, file);
1554 if (binder_debug_mask & BINDER_DEBUG_TRANSACTION)
1555 printk(KERN_INFO " fd %ld -> %d\n", fp->handle, target_fd);
1557 fp->handle = target_fd;
1561 binder_user_error("binder: %d:%d got transactio"
1562 "n with invalid object type, %lx\n",
1563 proc->pid, thread->pid, fp->type);
1564 return_error = BR_FAILED_REPLY;
1565 goto err_bad_object_type;
1569 BUG_ON(t->buffer->async_transaction != 0);
1570 binder_pop_transaction(target_thread, in_reply_to);
1571 } else if (!(t->flags & TF_ONE_WAY)) {
1572 BUG_ON(t->buffer->async_transaction != 0);
1574 t->from_parent = thread->transaction_stack;
1575 thread->transaction_stack = t;
1577 BUG_ON(target_node == NULL);
1578 BUG_ON(t->buffer->async_transaction != 1);
1579 if (target_node->has_async_transaction) {
1580 target_list = &target_node->async_todo;
1583 target_node->has_async_transaction = 1;
1585 t->work.type = BINDER_WORK_TRANSACTION;
1586 list_add_tail(&t->work.entry, target_list);
1587 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
1588 list_add_tail(&tcomplete->entry, &thread->todo);
1590 wake_up_interruptible(target_wait);
1593 err_get_unused_fd_failed:
1596 err_binder_get_ref_for_node_failed:
1597 err_binder_get_ref_failed:
1598 err_binder_new_node_failed:
1599 err_bad_object_type:
1601 err_copy_data_failed:
1602 binder_transaction_buffer_release(target_proc, t->buffer, offp);
1603 t->buffer->transaction = NULL;
1604 binder_free_buf(target_proc, t->buffer);
1605 err_binder_alloc_buf_failed:
1607 binder_stats.obj_deleted[BINDER_STAT_TRANSACTION_COMPLETE]++;
1608 err_alloc_tcomplete_failed:
1610 binder_stats.obj_deleted[BINDER_STAT_TRANSACTION]++;
1613 err_empty_call_stack:
1615 err_invalid_target_handle:
1616 err_no_context_mgr_node:
1617 if (binder_debug_mask & BINDER_DEBUG_FAILED_TRANSACTION)
1618 printk(KERN_INFO "binder: %d:%d transaction failed %d, size"
1620 proc->pid, thread->pid, return_error,
1621 tr->data_size, tr->offsets_size);
1624 struct binder_transaction_log_entry *fe;
1625 fe = binder_transaction_log_add(&binder_transaction_log_failed);
1629 BUG_ON(thread->return_error != BR_OK);
1631 thread->return_error = BR_TRANSACTION_COMPLETE;
1632 binder_send_failed_reply(in_reply_to, return_error);
1634 thread->return_error = return_error;
1638 binder_transaction_buffer_release(struct binder_proc *proc, struct binder_buffer *buffer, size_t *failed_at)
1640 size_t *offp, *off_end;
1641 int debug_id = buffer->debug_id;
1643 if (binder_debug_mask & BINDER_DEBUG_TRANSACTION)
1644 printk(KERN_INFO "binder: %d buffer release %d, size %zd-%zd, failed at %p\n",
1645 proc->pid, buffer->debug_id,
1646 buffer->data_size, buffer->offsets_size, failed_at);
1648 if (buffer->target_node)
1649 binder_dec_node(buffer->target_node, 1, 0);
1651 offp = (size_t *)(buffer->data + ALIGN(buffer->data_size, sizeof(void *)));
1653 off_end = failed_at;
1655 off_end = (void *)offp + buffer->offsets_size;
1656 for (; offp < off_end; offp++) {
1657 struct flat_binder_object *fp;
1658 if (*offp > buffer->data_size - sizeof(*fp)) {
1659 printk(KERN_ERR "binder: transaction release %d bad"
1660 "offset %zd, size %zd\n", debug_id, *offp, buffer->data_size);
1663 fp = (struct flat_binder_object *)(buffer->data + *offp);
1665 case BINDER_TYPE_BINDER:
1666 case BINDER_TYPE_WEAK_BINDER: {
1667 struct binder_node *node = binder_get_node(proc, fp->binder);
1669 printk(KERN_ERR "binder: transaction release %d bad node %p\n", debug_id, fp->binder);
1672 if (binder_debug_mask & BINDER_DEBUG_TRANSACTION)
1673 printk(KERN_INFO " node %d u%p\n",
1674 node->debug_id, node->ptr);
1675 binder_dec_node(node, fp->type == BINDER_TYPE_BINDER, 0);
1677 case BINDER_TYPE_HANDLE:
1678 case BINDER_TYPE_WEAK_HANDLE: {
1679 struct binder_ref *ref = binder_get_ref(proc, fp->handle);
1681 printk(KERN_ERR "binder: transaction release %d bad handle %ld\n", debug_id, fp->handle);
1684 if (binder_debug_mask & BINDER_DEBUG_TRANSACTION)
1685 printk(KERN_INFO " ref %d desc %d (node %d)\n",
1686 ref->debug_id, ref->desc, ref->node->debug_id);
1687 binder_dec_ref(ref, fp->type == BINDER_TYPE_HANDLE);
1690 case BINDER_TYPE_FD:
1691 if (binder_debug_mask & BINDER_DEBUG_TRANSACTION)
1692 printk(KERN_INFO " fd %ld\n", fp->handle);
1694 task_close_fd(proc->tsk, fp->handle);
1698 printk(KERN_ERR "binder: transaction release %d bad object type %lx\n", debug_id, fp->type);
1705 binder_thread_write(struct binder_proc *proc, struct binder_thread *thread,
1706 void __user *buffer, int size, signed long *consumed)
1709 void __user *ptr = buffer + *consumed;
1710 void __user *end = buffer + size;
1712 while (ptr < end && thread->return_error == BR_OK) {
1713 if (get_user(cmd, (uint32_t __user *)ptr))
1715 ptr += sizeof(uint32_t);
1716 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
1717 binder_stats.bc[_IOC_NR(cmd)]++;
1718 proc->stats.bc[_IOC_NR(cmd)]++;
1719 thread->stats.bc[_IOC_NR(cmd)]++;
1727 struct binder_ref *ref;
1728 const char *debug_string;
1730 if (get_user(target, (uint32_t __user *)ptr))
1732 ptr += sizeof(uint32_t);
1733 if (target == 0 && binder_context_mgr_node &&
1734 (cmd == BC_INCREFS || cmd == BC_ACQUIRE)) {
1735 ref = binder_get_ref_for_node(proc,
1736 binder_context_mgr_node);
1737 if (ref->desc != target) {
1738 binder_user_error("binder: %d:"
1739 "%d tried to acquire "
1740 "reference to desc 0, "
1742 proc->pid, thread->pid,
1746 ref = binder_get_ref(proc, target);
1748 binder_user_error("binder: %d:%d refcou"
1749 "nt change on invalid ref %d\n",
1750 proc->pid, thread->pid, target);
1755 debug_string = "IncRefs";
1756 binder_inc_ref(ref, 0, NULL);
1759 debug_string = "Acquire";
1760 binder_inc_ref(ref, 1, NULL);
1763 debug_string = "Release";
1764 binder_dec_ref(ref, 1);
1768 debug_string = "DecRefs";
1769 binder_dec_ref(ref, 0);
1772 if (binder_debug_mask & BINDER_DEBUG_USER_REFS)
1773 printk(KERN_INFO "binder: %d:%d %s ref %d desc %d s %d w %d for node %d\n",
1774 proc->pid, thread->pid, debug_string, ref->debug_id, ref->desc, ref->strong, ref->weak, ref->node->debug_id);
1777 case BC_INCREFS_DONE:
1778 case BC_ACQUIRE_DONE: {
1779 void __user *node_ptr;
1781 struct binder_node *node;
1783 if (get_user(node_ptr, (void * __user *)ptr))
1785 ptr += sizeof(void *);
1786 if (get_user(cookie, (void * __user *)ptr))
1788 ptr += sizeof(void *);
1789 node = binder_get_node(proc, node_ptr);
1791 binder_user_error("binder: %d:%d "
1792 "%s u%p no match\n",
1793 proc->pid, thread->pid,
1794 cmd == BC_INCREFS_DONE ?
1800 if (cookie != node->cookie) {
1801 binder_user_error("binder: %d:%d %s u%p node %d"
1802 " cookie mismatch %p != %p\n",
1803 proc->pid, thread->pid,
1804 cmd == BC_INCREFS_DONE ?
1805 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
1806 node_ptr, node->debug_id,
1807 cookie, node->cookie);
1810 if (cmd == BC_ACQUIRE_DONE) {
1811 if (node->pending_strong_ref == 0) {
1812 binder_user_error("binder: %d:%d "
1813 "BC_ACQUIRE_DONE node %d has "
1814 "no pending acquire request\n",
1815 proc->pid, thread->pid,
1819 node->pending_strong_ref = 0;
1821 if (node->pending_weak_ref == 0) {
1822 binder_user_error("binder: %d:%d "
1823 "BC_INCREFS_DONE node %d has "
1824 "no pending increfs request\n",
1825 proc->pid, thread->pid,
1829 node->pending_weak_ref = 0;
1831 binder_dec_node(node, cmd == BC_ACQUIRE_DONE, 0);
1832 if (binder_debug_mask & BINDER_DEBUG_USER_REFS)
1833 printk(KERN_INFO "binder: %d:%d %s node %d ls %d lw %d\n",
1834 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);
1837 case BC_ATTEMPT_ACQUIRE:
1838 printk(KERN_ERR "binder: BC_ATTEMPT_ACQUIRE not supported\n");
1840 case BC_ACQUIRE_RESULT:
1841 printk(KERN_ERR "binder: BC_ACQUIRE_RESULT not supported\n");
1844 case BC_FREE_BUFFER: {
1845 void __user *data_ptr;
1846 struct binder_buffer *buffer;
1848 if (get_user(data_ptr, (void * __user *)ptr))
1850 ptr += sizeof(void *);
1852 buffer = binder_buffer_lookup(proc, data_ptr);
1853 if (buffer == NULL) {
1854 binder_user_error("binder: %d:%d "
1855 "BC_FREE_BUFFER u%p no match\n",
1856 proc->pid, thread->pid, data_ptr);
1859 if (!buffer->allow_user_free) {
1860 binder_user_error("binder: %d:%d "
1861 "BC_FREE_BUFFER u%p matched "
1862 "unreturned buffer\n",
1863 proc->pid, thread->pid, data_ptr);
1866 if (binder_debug_mask & BINDER_DEBUG_FREE_BUFFER)
1867 printk(KERN_INFO "binder: %d:%d BC_FREE_BUFFER u%p found buffer %d for %s transaction\n",
1868 proc->pid, thread->pid, data_ptr, buffer->debug_id,
1869 buffer->transaction ? "active" : "finished");
1871 if (buffer->transaction) {
1872 buffer->transaction->buffer = NULL;
1873 buffer->transaction = NULL;
1875 if (buffer->async_transaction && buffer->target_node) {
1876 BUG_ON(!buffer->target_node->has_async_transaction);
1877 if (list_empty(&buffer->target_node->async_todo))
1878 buffer->target_node->has_async_transaction = 0;
1880 list_move_tail(buffer->target_node->async_todo.next, &thread->todo);
1882 binder_transaction_buffer_release(proc, buffer, NULL);
1883 binder_free_buf(proc, buffer);
1887 case BC_TRANSACTION:
1889 struct binder_transaction_data tr;
1891 if (copy_from_user(&tr, ptr, sizeof(tr)))
1894 binder_transaction(proc, thread, &tr, cmd == BC_REPLY);
1898 case BC_REGISTER_LOOPER:
1899 if (binder_debug_mask & BINDER_DEBUG_THREADS)
1900 printk(KERN_INFO "binder: %d:%d BC_REGISTER_LOOPER\n",
1901 proc->pid, thread->pid);
1902 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
1903 thread->looper |= BINDER_LOOPER_STATE_INVALID;
1904 binder_user_error("binder: %d:%d ERROR:"
1905 " BC_REGISTER_LOOPER called "
1906 "after BC_ENTER_LOOPER\n",
1907 proc->pid, thread->pid);
1908 } else if (proc->requested_threads == 0) {
1909 thread->looper |= BINDER_LOOPER_STATE_INVALID;
1910 binder_user_error("binder: %d:%d ERROR:"
1911 " BC_REGISTER_LOOPER called "
1912 "without request\n",
1913 proc->pid, thread->pid);
1915 proc->requested_threads--;
1916 proc->requested_threads_started++;
1918 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
1920 case BC_ENTER_LOOPER:
1921 if (binder_debug_mask & BINDER_DEBUG_THREADS)
1922 printk(KERN_INFO "binder: %d:%d BC_ENTER_LOOPER\n",
1923 proc->pid, thread->pid);
1924 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
1925 thread->looper |= BINDER_LOOPER_STATE_INVALID;
1926 binder_user_error("binder: %d:%d ERROR:"
1927 " BC_ENTER_LOOPER called after "
1928 "BC_REGISTER_LOOPER\n",
1929 proc->pid, thread->pid);
1931 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
1933 case BC_EXIT_LOOPER:
1934 if (binder_debug_mask & BINDER_DEBUG_THREADS)
1935 printk(KERN_INFO "binder: %d:%d BC_EXIT_LOOPER\n",
1936 proc->pid, thread->pid);
1937 thread->looper |= BINDER_LOOPER_STATE_EXITED;
1940 case BC_REQUEST_DEATH_NOTIFICATION:
1941 case BC_CLEAR_DEATH_NOTIFICATION: {
1943 void __user *cookie;
1944 struct binder_ref *ref;
1945 struct binder_ref_death *death;
1947 if (get_user(target, (uint32_t __user *)ptr))
1949 ptr += sizeof(uint32_t);
1950 if (get_user(cookie, (void __user * __user *)ptr))
1952 ptr += sizeof(void *);
1953 ref = binder_get_ref(proc, target);
1955 binder_user_error("binder: %d:%d %s "
1957 proc->pid, thread->pid,
1958 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
1959 "BC_REQUEST_DEATH_NOTIFICATION" :
1960 "BC_CLEAR_DEATH_NOTIFICATION",
1965 if (binder_debug_mask & BINDER_DEBUG_DEATH_NOTIFICATION)
1966 printk(KERN_INFO "binder: %d:%d %s %p ref %d desc %d s %d w %d for node %d\n",
1967 proc->pid, thread->pid,
1968 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
1969 "BC_REQUEST_DEATH_NOTIFICATION" :
1970 "BC_CLEAR_DEATH_NOTIFICATION",
1971 cookie, ref->debug_id, ref->desc,
1972 ref->strong, ref->weak, ref->node->debug_id);
1974 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
1976 binder_user_error("binder: %d:%"
1977 "d BC_REQUEST_DEATH_NOTI"
1978 "FICATION death notific"
1979 "ation already set\n",
1980 proc->pid, thread->pid);
1983 death = kzalloc(sizeof(*death), GFP_KERNEL);
1984 if (death == NULL) {
1985 thread->return_error = BR_ERROR;
1986 if (binder_debug_mask & BINDER_DEBUG_FAILED_TRANSACTION)
1987 printk(KERN_INFO "binder: %d:%d "
1988 "BC_REQUEST_DEATH_NOTIFICATION failed\n",
1989 proc->pid, thread->pid);
1992 binder_stats.obj_created[BINDER_STAT_DEATH]++;
1993 INIT_LIST_HEAD(&death->work.entry);
1994 death->cookie = cookie;
1996 if (ref->node->proc == NULL) {
1997 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
1998 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
1999 list_add_tail(&ref->death->work.entry, &thread->todo);
2001 list_add_tail(&ref->death->work.entry, &proc->todo);
2002 wake_up_interruptible(&proc->wait);
2006 if (ref->death == NULL) {
2007 binder_user_error("binder: %d:%"
2008 "d BC_CLEAR_DEATH_NOTIFI"
2009 "CATION death notificat"
2011 proc->pid, thread->pid);
2015 if (death->cookie != cookie) {
2016 binder_user_error("binder: %d:%"
2017 "d BC_CLEAR_DEATH_NOTIFI"
2018 "CATION death notificat"
2019 "ion cookie mismatch "
2021 proc->pid, thread->pid,
2022 death->cookie, cookie);
2026 if (list_empty(&death->work.entry)) {
2027 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2028 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2029 list_add_tail(&death->work.entry, &thread->todo);
2031 list_add_tail(&death->work.entry, &proc->todo);
2032 wake_up_interruptible(&proc->wait);
2035 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
2036 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
2040 case BC_DEAD_BINDER_DONE: {
2041 struct binder_work *w;
2042 void __user *cookie;
2043 struct binder_ref_death *death = NULL;
2044 if (get_user(cookie, (void __user * __user *)ptr))
2047 ptr += sizeof(void *);
2048 list_for_each_entry(w, &proc->delivered_death, entry) {
2049 struct binder_ref_death *tmp_death = container_of(w, struct binder_ref_death, work);
2050 if (tmp_death->cookie == cookie) {
2055 if (binder_debug_mask & BINDER_DEBUG_DEAD_BINDER)
2056 printk(KERN_INFO "binder: %d:%d BC_DEAD_BINDER_DONE %p found %p\n",
2057 proc->pid, thread->pid, cookie, death);
2058 if (death == NULL) {
2059 binder_user_error("binder: %d:%d BC_DEAD"
2060 "_BINDER_DONE %p not found\n",
2061 proc->pid, thread->pid, cookie);
2065 list_del_init(&death->work.entry);
2066 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
2067 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2068 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2069 list_add_tail(&death->work.entry, &thread->todo);
2071 list_add_tail(&death->work.entry, &proc->todo);
2072 wake_up_interruptible(&proc->wait);
2078 printk(KERN_ERR "binder: %d:%d unknown command %d\n", proc->pid, thread->pid, cmd);
2081 *consumed = ptr - buffer;
2087 binder_stat_br(struct binder_proc *proc, struct binder_thread *thread, uint32_t cmd)
2089 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
2090 binder_stats.br[_IOC_NR(cmd)]++;
2091 proc->stats.br[_IOC_NR(cmd)]++;
2092 thread->stats.br[_IOC_NR(cmd)]++;
2097 binder_has_proc_work(struct binder_proc *proc, struct binder_thread *thread)
2099 return !list_empty(&proc->todo) || (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2103 binder_has_thread_work(struct binder_thread *thread)
2105 return !list_empty(&thread->todo) || thread->return_error != BR_OK ||
2106 (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2110 binder_thread_read(struct binder_proc *proc, struct binder_thread *thread,
2111 void __user *buffer, int size, signed long *consumed, int non_block)
2113 void __user *ptr = buffer + *consumed;
2114 void __user *end = buffer + size;
2117 int wait_for_proc_work;
2119 if (*consumed == 0) {
2120 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
2122 ptr += sizeof(uint32_t);
2126 wait_for_proc_work = thread->transaction_stack == NULL && list_empty(&thread->todo);
2128 if (thread->return_error != BR_OK && ptr < end) {
2129 if (thread->return_error2 != BR_OK) {
2130 if (put_user(thread->return_error2, (uint32_t __user *)ptr))
2132 ptr += sizeof(uint32_t);
2135 thread->return_error2 = BR_OK;
2137 if (put_user(thread->return_error, (uint32_t __user *)ptr))
2139 ptr += sizeof(uint32_t);
2140 thread->return_error = BR_OK;
2145 thread->looper |= BINDER_LOOPER_STATE_WAITING;
2146 if (wait_for_proc_work)
2147 proc->ready_threads++;
2148 mutex_unlock(&binder_lock);
2149 if (wait_for_proc_work) {
2150 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2151 BINDER_LOOPER_STATE_ENTERED))) {
2152 binder_user_error("binder: %d:%d ERROR: Thread waiting "
2153 "for process work before calling BC_REGISTER_"
2154 "LOOPER or BC_ENTER_LOOPER (state %x)\n",
2155 proc->pid, thread->pid, thread->looper);
2156 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2158 binder_set_nice(proc->default_priority);
2160 if (!binder_has_proc_work(proc, thread))
2163 ret = wait_event_interruptible_exclusive(proc->wait, binder_has_proc_work(proc, thread));
2166 if (!binder_has_thread_work(thread))
2169 ret = wait_event_interruptible(thread->wait, binder_has_thread_work(thread));
2171 mutex_lock(&binder_lock);
2172 if (wait_for_proc_work)
2173 proc->ready_threads--;
2174 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
2181 struct binder_transaction_data tr;
2182 struct binder_work *w;
2183 struct binder_transaction *t = NULL;
2185 if (!list_empty(&thread->todo))
2186 w = list_first_entry(&thread->todo, struct binder_work, entry);
2187 else if (!list_empty(&proc->todo) && wait_for_proc_work)
2188 w = list_first_entry(&proc->todo, struct binder_work, entry);
2190 if (ptr - buffer == 4 && !(thread->looper & BINDER_LOOPER_STATE_NEED_RETURN)) /* no data added */
2195 if (end - ptr < sizeof(tr) + 4)
2199 case BINDER_WORK_TRANSACTION: {
2200 t = container_of(w, struct binder_transaction, work);
2202 case BINDER_WORK_TRANSACTION_COMPLETE: {
2203 cmd = BR_TRANSACTION_COMPLETE;
2204 if (put_user(cmd, (uint32_t __user *)ptr))
2206 ptr += sizeof(uint32_t);
2208 binder_stat_br(proc, thread, cmd);
2209 if (binder_debug_mask & BINDER_DEBUG_TRANSACTION_COMPLETE)
2210 printk(KERN_INFO "binder: %d:%d BR_TRANSACTION_COMPLETE\n",
2211 proc->pid, thread->pid);
2213 list_del(&w->entry);
2215 binder_stats.obj_deleted[BINDER_STAT_TRANSACTION_COMPLETE]++;
2217 case BINDER_WORK_NODE: {
2218 struct binder_node *node = container_of(w, struct binder_node, work);
2219 uint32_t cmd = BR_NOOP;
2220 const char *cmd_name;
2221 int strong = node->internal_strong_refs || node->local_strong_refs;
2222 int weak = !hlist_empty(&node->refs) || node->local_weak_refs || strong;
2223 if (weak && !node->has_weak_ref) {
2225 cmd_name = "BR_INCREFS";
2226 node->has_weak_ref = 1;
2227 node->pending_weak_ref = 1;
2228 node->local_weak_refs++;
2229 } else if (strong && !node->has_strong_ref) {
2231 cmd_name = "BR_ACQUIRE";
2232 node->has_strong_ref = 1;
2233 node->pending_strong_ref = 1;
2234 node->local_strong_refs++;
2235 } else if (!strong && node->has_strong_ref) {
2237 cmd_name = "BR_RELEASE";
2238 node->has_strong_ref = 0;
2239 } else if (!weak && node->has_weak_ref) {
2241 cmd_name = "BR_DECREFS";
2242 node->has_weak_ref = 0;
2244 if (cmd != BR_NOOP) {
2245 if (put_user(cmd, (uint32_t __user *)ptr))
2247 ptr += sizeof(uint32_t);
2248 if (put_user(node->ptr, (void * __user *)ptr))
2250 ptr += sizeof(void *);
2251 if (put_user(node->cookie, (void * __user *)ptr))
2253 ptr += sizeof(void *);
2255 binder_stat_br(proc, thread, cmd);
2256 if (binder_debug_mask & BINDER_DEBUG_USER_REFS)
2257 printk(KERN_INFO "binder: %d:%d %s %d u%p c%p\n",
2258 proc->pid, thread->pid, cmd_name, node->debug_id, node->ptr, node->cookie);
2260 list_del_init(&w->entry);
2261 if (!weak && !strong) {
2262 if (binder_debug_mask & BINDER_DEBUG_INTERNAL_REFS)
2263 printk(KERN_INFO "binder: %d:%d node %d u%p c%p deleted\n",
2264 proc->pid, thread->pid, node->debug_id, node->ptr, node->cookie);
2265 rb_erase(&node->rb_node, &proc->nodes);
2267 binder_stats.obj_deleted[BINDER_STAT_NODE]++;
2269 if (binder_debug_mask & BINDER_DEBUG_INTERNAL_REFS)
2270 printk(KERN_INFO "binder: %d:%d node %d u%p c%p state unchanged\n",
2271 proc->pid, thread->pid, node->debug_id, node->ptr, node->cookie);
2275 case BINDER_WORK_DEAD_BINDER:
2276 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2277 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2278 struct binder_ref_death *death = container_of(w, struct binder_ref_death, work);
2280 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
2281 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
2283 cmd = BR_DEAD_BINDER;
2284 if (put_user(cmd, (uint32_t __user *)ptr))
2286 ptr += sizeof(uint32_t);
2287 if (put_user(death->cookie, (void * __user *)ptr))
2289 ptr += sizeof(void *);
2290 if (binder_debug_mask & BINDER_DEBUG_DEATH_NOTIFICATION)
2291 printk(KERN_INFO "binder: %d:%d %s %p\n",
2292 proc->pid, thread->pid,
2293 cmd == BR_DEAD_BINDER ?
2295 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
2298 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
2299 list_del(&w->entry);
2301 binder_stats.obj_deleted[BINDER_STAT_DEATH]++;
2303 list_move(&w->entry, &proc->delivered_death);
2304 if (cmd == BR_DEAD_BINDER)
2305 goto done; /* DEAD_BINDER notifications can cause transactions */
2312 BUG_ON(t->buffer == NULL);
2313 if (t->buffer->target_node) {
2314 struct binder_node *target_node = t->buffer->target_node;
2315 tr.target.ptr = target_node->ptr;
2316 tr.cookie = target_node->cookie;
2317 t->saved_priority = task_nice(current);
2318 if (t->priority < target_node->min_priority &&
2319 !(t->flags & TF_ONE_WAY))
2320 binder_set_nice(t->priority);
2321 else if (!(t->flags & TF_ONE_WAY) ||
2322 t->saved_priority > target_node->min_priority)
2323 binder_set_nice(target_node->min_priority);
2324 cmd = BR_TRANSACTION;
2326 tr.target.ptr = NULL;
2331 tr.flags = t->flags;
2332 tr.sender_euid = t->sender_euid;
2335 struct task_struct *sender = t->from->proc->tsk;
2336 tr.sender_pid = task_tgid_nr_ns(sender, current->nsproxy->pid_ns);
2341 tr.data_size = t->buffer->data_size;
2342 tr.offsets_size = t->buffer->offsets_size;
2343 tr.data.ptr.buffer = (void *)((void *)t->buffer->data + proc->user_buffer_offset);
2344 tr.data.ptr.offsets = tr.data.ptr.buffer + ALIGN(t->buffer->data_size, sizeof(void *));
2346 if (put_user(cmd, (uint32_t __user *)ptr))
2348 ptr += sizeof(uint32_t);
2349 if (copy_to_user(ptr, &tr, sizeof(tr)))
2353 binder_stat_br(proc, thread, cmd);
2354 if (binder_debug_mask & BINDER_DEBUG_TRANSACTION)
2355 printk(KERN_INFO "binder: %d:%d %s %d %d:%d, cmd %d"
2356 "size %zd-%zd ptr %p-%p\n",
2357 proc->pid, thread->pid,
2358 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" : "BR_REPLY",
2359 t->debug_id, t->from ? t->from->proc->pid : 0,
2360 t->from ? t->from->pid : 0, cmd,
2361 t->buffer->data_size, t->buffer->offsets_size,
2362 tr.data.ptr.buffer, tr.data.ptr.offsets);
2364 list_del(&t->work.entry);
2365 t->buffer->allow_user_free = 1;
2366 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
2367 t->to_parent = thread->transaction_stack;
2368 t->to_thread = thread;
2369 thread->transaction_stack = t;
2371 t->buffer->transaction = NULL;
2373 binder_stats.obj_deleted[BINDER_STAT_TRANSACTION]++;
2380 *consumed = ptr - buffer;
2381 if (proc->requested_threads + proc->ready_threads == 0 &&
2382 proc->requested_threads_started < proc->max_threads &&
2383 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2384 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
2385 /*spawn a new thread if we leave this out */) {
2386 proc->requested_threads++;
2387 if (binder_debug_mask & BINDER_DEBUG_THREADS)
2388 printk(KERN_INFO "binder: %d:%d BR_SPAWN_LOOPER\n",
2389 proc->pid, thread->pid);
2390 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
2396 static void binder_release_work(struct list_head *list)
2398 struct binder_work *w;
2399 while (!list_empty(list)) {
2400 w = list_first_entry(list, struct binder_work, entry);
2401 list_del_init(&w->entry);
2403 case BINDER_WORK_TRANSACTION: {
2404 struct binder_transaction *t = container_of(w, struct binder_transaction, work);
2405 if (t->buffer->target_node && !(t->flags & TF_ONE_WAY))
2406 binder_send_failed_reply(t, BR_DEAD_REPLY);
2408 case BINDER_WORK_TRANSACTION_COMPLETE: {
2410 binder_stats.obj_deleted[BINDER_STAT_TRANSACTION_COMPLETE]++;
2419 static struct binder_thread *binder_get_thread(struct binder_proc *proc)
2421 struct binder_thread *thread = NULL;
2422 struct rb_node *parent = NULL;
2423 struct rb_node **p = &proc->threads.rb_node;
2427 thread = rb_entry(parent, struct binder_thread, rb_node);
2429 if (current->pid < thread->pid)
2431 else if (current->pid > thread->pid)
2432 p = &(*p)->rb_right;
2437 thread = kzalloc(sizeof(*thread), GFP_KERNEL);
2440 binder_stats.obj_created[BINDER_STAT_THREAD]++;
2441 thread->proc = proc;
2442 thread->pid = current->pid;
2443 init_waitqueue_head(&thread->wait);
2444 INIT_LIST_HEAD(&thread->todo);
2445 rb_link_node(&thread->rb_node, parent, p);
2446 rb_insert_color(&thread->rb_node, &proc->threads);
2447 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
2448 thread->return_error = BR_OK;
2449 thread->return_error2 = BR_OK;
2454 static int binder_free_thread(struct binder_proc *proc, struct binder_thread *thread)
2456 struct binder_transaction *t;
2457 struct binder_transaction *send_reply = NULL;
2458 int active_transactions = 0;
2460 rb_erase(&thread->rb_node, &proc->threads);
2461 t = thread->transaction_stack;
2462 if (t && t->to_thread == thread)
2465 active_transactions++;
2466 if (binder_debug_mask & BINDER_DEBUG_DEAD_TRANSACTION)
2467 printk(KERN_INFO "binder: release %d:%d transaction %d %s, still active\n",
2468 proc->pid, thread->pid, t->debug_id, (t->to_thread == thread) ? "in" : "out");
2469 if (t->to_thread == thread) {
2471 t->to_thread = NULL;
2473 t->buffer->transaction = NULL;
2477 } else if (t->from == thread) {
2484 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
2485 binder_release_work(&thread->todo);
2487 binder_stats.obj_deleted[BINDER_STAT_THREAD]++;
2488 return active_transactions;
2491 static unsigned int binder_poll(struct file *filp, struct poll_table_struct *wait)
2493 struct binder_proc *proc = filp->private_data;
2494 struct binder_thread *thread = NULL;
2495 int wait_for_proc_work;
2497 mutex_lock(&binder_lock);
2498 thread = binder_get_thread(proc);
2500 wait_for_proc_work = thread->transaction_stack == NULL &&
2501 list_empty(&thread->todo) && thread->return_error == BR_OK;
2502 mutex_unlock(&binder_lock);
2504 if (wait_for_proc_work) {
2505 if (binder_has_proc_work(proc, thread))
2507 poll_wait(filp, &proc->wait, wait);
2508 if (binder_has_proc_work(proc, thread))
2511 if (binder_has_thread_work(thread))
2513 poll_wait(filp, &thread->wait, wait);
2514 if (binder_has_thread_work(thread))
2520 static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2523 struct binder_proc *proc = filp->private_data;
2524 struct binder_thread *thread;
2525 unsigned int size = _IOC_SIZE(cmd);
2526 void __user *ubuf = (void __user *)arg;
2528 /*printk(KERN_INFO "binder_ioctl: %d:%d %x %lx\n", proc->pid, current->pid, cmd, arg);*/
2530 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2534 mutex_lock(&binder_lock);
2535 thread = binder_get_thread(proc);
2536 if (thread == NULL) {
2542 case BINDER_WRITE_READ: {
2543 struct binder_write_read bwr;
2544 if (size != sizeof(struct binder_write_read)) {
2548 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
2552 if (binder_debug_mask & BINDER_DEBUG_READ_WRITE)
2553 printk(KERN_INFO "binder: %d:%d write %ld at %08lx, read %ld at %08lx\n",
2554 proc->pid, thread->pid, bwr.write_size, bwr.write_buffer, bwr.read_size, bwr.read_buffer);
2555 if (bwr.write_size > 0) {
2556 ret = binder_thread_write(proc, thread, (void __user *)bwr.write_buffer, bwr.write_size, &bwr.write_consumed);
2558 bwr.read_consumed = 0;
2559 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2564 if (bwr.read_size > 0) {
2565 ret = binder_thread_read(proc, thread, (void __user *)bwr.read_buffer, bwr.read_size, &bwr.read_consumed, filp->f_flags & O_NONBLOCK);
2566 if (!list_empty(&proc->todo))
2567 wake_up_interruptible(&proc->wait);
2569 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2574 if (binder_debug_mask & BINDER_DEBUG_READ_WRITE)
2575 printk(KERN_INFO "binder: %d:%d wrote %ld of %ld, read return %ld of %ld\n",
2576 proc->pid, thread->pid, bwr.write_consumed, bwr.write_size, bwr.read_consumed, bwr.read_size);
2577 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
2583 case BINDER_SET_MAX_THREADS:
2584 if (copy_from_user(&proc->max_threads, ubuf, sizeof(proc->max_threads))) {
2589 case BINDER_SET_CONTEXT_MGR:
2590 if (binder_context_mgr_node != NULL) {
2591 printk(KERN_ERR "binder: BINDER_SET_CONTEXT_MGR already set\n");
2595 if (binder_context_mgr_uid != -1) {
2596 if (binder_context_mgr_uid != current->cred->euid) {
2597 printk(KERN_ERR "binder: BINDER_SET_"
2598 "CONTEXT_MGR bad uid %d != %d\n",
2599 current->cred->euid,
2600 binder_context_mgr_uid);
2605 binder_context_mgr_uid = current->cred->euid;
2606 binder_context_mgr_node = binder_new_node(proc, NULL, NULL);
2607 if (binder_context_mgr_node == NULL) {
2611 binder_context_mgr_node->local_weak_refs++;
2612 binder_context_mgr_node->local_strong_refs++;
2613 binder_context_mgr_node->has_strong_ref = 1;
2614 binder_context_mgr_node->has_weak_ref = 1;
2616 case BINDER_THREAD_EXIT:
2617 if (binder_debug_mask & BINDER_DEBUG_THREADS)
2618 printk(KERN_INFO "binder: %d:%d exit\n",
2619 proc->pid, thread->pid);
2620 binder_free_thread(proc, thread);
2623 case BINDER_VERSION:
2624 if (size != sizeof(struct binder_version)) {
2628 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION, &((struct binder_version *)ubuf)->protocol_version)) {
2640 thread->looper &= ~BINDER_LOOPER_STATE_NEED_RETURN;
2641 mutex_unlock(&binder_lock);
2642 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2643 if (ret && ret != -ERESTARTSYS)
2644 printk(KERN_INFO "binder: %d:%d ioctl %x %lx returned %d\n", proc->pid, current->pid, cmd, arg, ret);
2648 static void binder_vma_open(struct vm_area_struct *vma)
2650 struct binder_proc *proc = vma->vm_private_data;
2651 if (binder_debug_mask & BINDER_DEBUG_OPEN_CLOSE)
2652 printk(KERN_INFO "binder: %d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n", proc->pid, vma->vm_start, vma->vm_end, (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, vma->vm_page_prot.pgprot);
2655 static void binder_vma_close(struct vm_area_struct *vma)
2657 struct binder_proc *proc = vma->vm_private_data;
2658 if (binder_debug_mask & BINDER_DEBUG_OPEN_CLOSE)
2659 printk(KERN_INFO "binder: %d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n", proc->pid, vma->vm_start, vma->vm_end, (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, vma->vm_page_prot.pgprot);
2663 static struct vm_operations_struct binder_vm_ops = {
2664 .open = binder_vma_open,
2665 .close = binder_vma_close,
2668 static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
2671 struct vm_struct *area;
2672 struct binder_proc *proc = filp->private_data;
2673 const char *failure_string;
2674 struct binder_buffer *buffer;
2676 if ((vma->vm_end - vma->vm_start) > SZ_4M)
2677 vma->vm_end = vma->vm_start + SZ_4M;
2679 if (binder_debug_mask & BINDER_DEBUG_OPEN_CLOSE)
2680 printk(KERN_INFO "binder_mmap: %d %lx-%lx (%ld K) vma %lx pagep %lx\n", proc->pid, vma->vm_start, vma->vm_end, (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, vma->vm_page_prot.pgprot);
2682 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
2684 failure_string = "bad vm_flags";
2687 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
2689 area = get_vm_area(vma->vm_end - vma->vm_start, VM_IOREMAP);
2692 failure_string = "get_vm_area";
2693 goto err_get_vm_area_failed;
2695 proc->buffer = area->addr;
2696 proc->user_buffer_offset = vma->vm_start - (size_t)proc->buffer;
2698 #ifdef CONFIG_CPU_CACHE_VIPT
2699 if (cache_is_vipt_aliasing()) {
2700 while (CACHE_COLOUR((vma->vm_start ^ (uint32_t)proc->buffer))) {
2701 printk(KERN_INFO "binder_mmap: %d %lx-%lx maps %p bad alignment\n", proc->pid, vma->vm_start, vma->vm_end, proc->buffer);
2702 vma->vm_start += PAGE_SIZE;
2706 proc->pages = kzalloc(sizeof(proc->pages[0]) * ((vma->vm_end - vma->vm_start) / PAGE_SIZE), GFP_KERNEL);
2707 if (proc->pages == NULL) {
2709 failure_string = "alloc page array";
2710 goto err_alloc_pages_failed;
2712 proc->buffer_size = vma->vm_end - vma->vm_start;
2714 vma->vm_ops = &binder_vm_ops;
2715 vma->vm_private_data = proc;
2717 if (binder_update_page_range(proc, 1, proc->buffer, proc->buffer + PAGE_SIZE, vma)) {
2719 failure_string = "alloc small buf";
2720 goto err_alloc_small_buf_failed;
2722 buffer = proc->buffer;
2723 INIT_LIST_HEAD(&proc->buffers);
2724 list_add(&buffer->entry, &proc->buffers);
2726 binder_insert_free_buffer(proc, buffer);
2727 proc->free_async_space = proc->buffer_size / 2;
2731 /*printk(KERN_INFO "binder_mmap: %d %lx-%lx maps %p\n", proc->pid, vma->vm_start, vma->vm_end, proc->buffer);*/
2734 err_alloc_small_buf_failed:
2736 err_alloc_pages_failed:
2737 vfree(proc->buffer);
2738 err_get_vm_area_failed:
2739 mutex_unlock(&binder_lock);
2741 printk(KERN_ERR "binder_mmap: %d %lx-%lx %s failed %d\n", proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
2745 static int binder_open(struct inode *nodp, struct file *filp)
2747 struct binder_proc *proc;
2749 if (binder_debug_mask & BINDER_DEBUG_OPEN_CLOSE)
2750 printk(KERN_INFO "binder_open: %d:%d\n", current->group_leader->pid, current->pid);
2752 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
2755 get_task_struct(current);
2756 proc->tsk = current;
2757 INIT_LIST_HEAD(&proc->todo);
2758 init_waitqueue_head(&proc->wait);
2759 proc->default_priority = task_nice(current);
2760 mutex_lock(&binder_lock);
2761 binder_stats.obj_created[BINDER_STAT_PROC]++;
2762 hlist_add_head(&proc->proc_node, &binder_procs);
2763 proc->pid = current->group_leader->pid;
2764 INIT_LIST_HEAD(&proc->delivered_death);
2765 filp->private_data = proc;
2766 mutex_unlock(&binder_lock);
2768 if (binder_proc_dir_entry_proc) {
2770 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
2771 create_proc_read_entry(strbuf, S_IRUGO, binder_proc_dir_entry_proc, binder_read_proc_proc, proc);
2777 static int binder_flush(struct file *filp, fl_owner_t id)
2780 struct binder_proc *proc = filp->private_data;
2783 mutex_lock(&binder_lock);
2784 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
2785 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
2786 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
2787 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
2788 wake_up_interruptible(&thread->wait);
2792 wake_up_interruptible_all(&proc->wait);
2793 mutex_unlock(&binder_lock);
2795 if (binder_debug_mask & BINDER_DEBUG_OPEN_CLOSE)
2796 printk(KERN_INFO "binder_flush: %d woke %d threads\n", proc->pid, wake_count);
2801 static int binder_release(struct inode *nodp, struct file *filp)
2803 struct hlist_node *pos;
2804 struct binder_transaction *t;
2806 struct binder_proc *proc = filp->private_data;
2807 int threads, nodes, incoming_refs, outgoing_refs, buffers, active_transactions, page_count;
2809 if (binder_proc_dir_entry_proc) {
2811 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
2812 remove_proc_entry(strbuf, binder_proc_dir_entry_proc);
2814 mutex_lock(&binder_lock);
2815 hlist_del(&proc->proc_node);
2816 if (binder_context_mgr_node && binder_context_mgr_node->proc == proc) {
2817 if (binder_debug_mask & BINDER_DEBUG_DEAD_BINDER)
2818 printk(KERN_INFO "binder_release: %d context_mgr_node gone\n", proc->pid);
2819 binder_context_mgr_node = NULL;
2823 active_transactions = 0;
2824 while ((n = rb_first(&proc->threads))) {
2825 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
2827 active_transactions += binder_free_thread(proc, thread);
2831 while ((n = rb_first(&proc->nodes))) {
2832 struct binder_node *node = rb_entry(n, struct binder_node, rb_node);
2835 rb_erase(&node->rb_node, &proc->nodes);
2836 list_del_init(&node->work.entry);
2837 if (hlist_empty(&node->refs)) {
2839 binder_stats.obj_deleted[BINDER_STAT_NODE]++;
2841 struct binder_ref *ref;
2845 node->local_strong_refs = 0;
2846 node->local_weak_refs = 0;
2847 hlist_add_head(&node->dead_node, &binder_dead_nodes);
2849 hlist_for_each_entry(ref, pos, &node->refs, node_entry) {
2853 if (list_empty(&ref->death->work.entry)) {
2854 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
2855 list_add_tail(&ref->death->work.entry, &ref->proc->todo);
2856 wake_up_interruptible(&ref->proc->wait);
2861 if (binder_debug_mask & BINDER_DEBUG_DEAD_BINDER)
2862 printk(KERN_INFO "binder: node %d now dead, refs %d, death %d\n", node->debug_id, incoming_refs, death);
2866 while ((n = rb_first(&proc->refs_by_desc))) {
2867 struct binder_ref *ref = rb_entry(n, struct binder_ref, rb_node_desc);
2869 binder_delete_ref(ref);
2871 binder_release_work(&proc->todo);
2874 while ((n = rb_first(&proc->allocated_buffers))) {
2875 struct binder_buffer *buffer = rb_entry(n, struct binder_buffer, rb_node);
2876 t = buffer->transaction;
2879 buffer->transaction = NULL;
2880 printk(KERN_ERR "binder: release proc %d, transaction %d, not freed\n", proc->pid, t->debug_id);
2883 binder_free_buf(proc, buffer);
2887 binder_stats.obj_deleted[BINDER_STAT_PROC]++;
2888 mutex_unlock(&binder_lock);
2893 for (i = 0; i < proc->buffer_size / PAGE_SIZE; i++) {
2894 if (proc->pages[i]) {
2895 if (binder_debug_mask & BINDER_DEBUG_BUFFER_ALLOC)
2896 printk(KERN_INFO "binder_release: %d: page %d at %p not freed\n", proc->pid, i, proc->buffer + i * PAGE_SIZE);
2897 __free_page(proc->pages[i]);
2902 vfree(proc->buffer);
2905 put_task_struct(proc->tsk);
2907 if (binder_debug_mask & BINDER_DEBUG_OPEN_CLOSE)
2908 printk(KERN_INFO "binder_release: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d, buffers %d, pages %d\n",
2909 proc->pid, threads, nodes, incoming_refs, outgoing_refs, active_transactions, buffers, page_count);
2915 static char *print_binder_transaction(char *buf, char *end, const char *prefix, struct binder_transaction *t)
2917 buf += snprintf(buf, end - buf, "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %ld r%d",
2918 prefix, t->debug_id, t, t->from ? t->from->proc->pid : 0,
2919 t->from ? t->from->pid : 0,
2920 t->to_proc ? t->to_proc->pid : 0,
2921 t->to_thread ? t->to_thread->pid : 0,
2922 t->code, t->flags, t->priority, t->need_reply);
2925 if (t->buffer == NULL) {
2926 buf += snprintf(buf, end - buf, " buffer free\n");
2929 if (t->buffer->target_node) {
2930 buf += snprintf(buf, end - buf, " node %d",
2931 t->buffer->target_node->debug_id);
2935 buf += snprintf(buf, end - buf, " size %zd:%zd data %p\n",
2936 t->buffer->data_size, t->buffer->offsets_size,
2941 static char *print_binder_buffer(char *buf, char *end, const char *prefix, struct binder_buffer *buffer)
2943 buf += snprintf(buf, end - buf, "%s %d: %p size %zd:%zd %s\n",
2944 prefix, buffer->debug_id, buffer->data,
2945 buffer->data_size, buffer->offsets_size,
2946 buffer->transaction ? "active" : "delivered");
2950 static char *print_binder_work(char *buf, char *end, const char *prefix,
2951 const char *transaction_prefix, struct binder_work *w)
2953 struct binder_node *node;
2954 struct binder_transaction *t;
2957 case BINDER_WORK_TRANSACTION:
2958 t = container_of(w, struct binder_transaction, work);
2959 buf = print_binder_transaction(buf, end, transaction_prefix, t);
2961 case BINDER_WORK_TRANSACTION_COMPLETE:
2962 buf += snprintf(buf, end - buf,
2963 "%stransaction complete\n", prefix);
2965 case BINDER_WORK_NODE:
2966 node = container_of(w, struct binder_node, work);
2967 buf += snprintf(buf, end - buf, "%snode work %d: u%p c%p\n",
2968 prefix, node->debug_id, node->ptr, node->cookie);
2970 case BINDER_WORK_DEAD_BINDER:
2971 buf += snprintf(buf, end - buf, "%shas dead binder\n", prefix);
2973 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2974 buf += snprintf(buf, end - buf,
2975 "%shas cleared dead binder\n", prefix);
2977 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
2978 buf += snprintf(buf, end - buf,
2979 "%shas cleared death notification\n", prefix);
2982 buf += snprintf(buf, end - buf, "%sunknown work: type %d\n",
2989 static char *print_binder_thread(char *buf, char *end, struct binder_thread *thread, int print_always)
2991 struct binder_transaction *t;
2992 struct binder_work *w;
2993 char *start_buf = buf;
2996 buf += snprintf(buf, end - buf, " thread %d: l %02x\n", thread->pid, thread->looper);
2998 t = thread->transaction_stack;
3002 if (t->from == thread) {
3003 buf = print_binder_transaction(buf, end, " outgoing transaction", t);
3005 } else if (t->to_thread == thread) {
3006 buf = print_binder_transaction(buf, end, " incoming transaction", t);
3009 buf = print_binder_transaction(buf, end, " bad transaction", t);
3013 list_for_each_entry(w, &thread->todo, entry) {
3016 buf = print_binder_work(buf, end, " ",
3017 " pending transaction", w);
3019 if (!print_always && buf == header_buf)
3024 static char *print_binder_node(char *buf, char *end, struct binder_node *node)
3026 struct binder_ref *ref;
3027 struct hlist_node *pos;
3028 struct binder_work *w;
3031 hlist_for_each_entry(ref, pos, &node->refs, node_entry)
3034 buf += snprintf(buf, end - buf, " node %d: u%p c%p hs %d hw %d ls %d lw %d is %d iw %d",
3035 node->debug_id, node->ptr, node->cookie,
3036 node->has_strong_ref, node->has_weak_ref,
3037 node->local_strong_refs, node->local_weak_refs,
3038 node->internal_strong_refs, count);
3042 buf += snprintf(buf, end - buf, " proc");
3045 hlist_for_each_entry(ref, pos, &node->refs, node_entry) {
3046 buf += snprintf(buf, end - buf, " %d", ref->proc->pid);
3051 buf += snprintf(buf, end - buf, "\n");
3052 list_for_each_entry(w, &node->async_todo, entry) {
3055 buf = print_binder_work(buf, end, " ",
3056 " pending async transaction", w);
3061 static char *print_binder_ref(char *buf, char *end, struct binder_ref *ref)
3063 buf += snprintf(buf, end - buf, " ref %d: desc %d %snode %d s %d w %d d %p\n",
3064 ref->debug_id, ref->desc, ref->node->proc ? "" : "dead ",
3065 ref->node->debug_id, ref->strong, ref->weak, ref->death);
3069 static char *print_binder_proc(char *buf, char *end, struct binder_proc *proc, int print_all)
3071 struct binder_work *w;
3073 char *start_buf = buf;
3076 buf += snprintf(buf, end - buf, "proc %d\n", proc->pid);
3079 for (n = rb_first(&proc->threads); n != NULL && buf < end; n = rb_next(n))
3080 buf = print_binder_thread(buf, end, rb_entry(n, struct binder_thread, rb_node), print_all);
3081 for (n = rb_first(&proc->nodes); n != NULL && buf < end; n = rb_next(n)) {
3082 struct binder_node *node = rb_entry(n, struct binder_node, rb_node);
3083 if (print_all || node->has_async_transaction)
3084 buf = print_binder_node(buf, end, node);
3087 for (n = rb_first(&proc->refs_by_desc); n != NULL && buf < end; n = rb_next(n))
3088 buf = print_binder_ref(buf, end, rb_entry(n, struct binder_ref, rb_node_desc));
3090 for (n = rb_first(&proc->allocated_buffers); n != NULL && buf < end; n = rb_next(n))
3091 buf = print_binder_buffer(buf, end, " buffer", rb_entry(n, struct binder_buffer, rb_node));
3092 list_for_each_entry(w, &proc->todo, entry) {
3095 buf = print_binder_work(buf, end, " ",
3096 " pending transaction", w);
3098 list_for_each_entry(w, &proc->delivered_death, entry) {
3101 buf += snprintf(buf, end - buf, " has delivered dead binder\n");
3104 if (!print_all && buf == header_buf)
3109 static const char *binder_return_strings[] = {
3114 "BR_ACQUIRE_RESULT",
3116 "BR_TRANSACTION_COMPLETE",
3121 "BR_ATTEMPT_ACQUIRE",
3126 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
3130 static const char *binder_command_strings[] = {
3133 "BC_ACQUIRE_RESULT",
3141 "BC_ATTEMPT_ACQUIRE",
3142 "BC_REGISTER_LOOPER",
3145 "BC_REQUEST_DEATH_NOTIFICATION",
3146 "BC_CLEAR_DEATH_NOTIFICATION",
3147 "BC_DEAD_BINDER_DONE"
3150 static const char *binder_objstat_strings[] = {
3157 "transaction_complete"
3160 static char *print_binder_stats(char *buf, char *end, const char *prefix, struct binder_stats *stats)
3164 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) != ARRAY_SIZE(binder_command_strings));
3165 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
3167 buf += snprintf(buf, end - buf, "%s%s: %d\n", prefix,
3168 binder_command_strings[i], stats->bc[i]);
3173 BUILD_BUG_ON(ARRAY_SIZE(stats->br) != ARRAY_SIZE(binder_return_strings));
3174 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
3176 buf += snprintf(buf, end - buf, "%s%s: %d\n", prefix,
3177 binder_return_strings[i], stats->br[i]);
3182 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) != ARRAY_SIZE(binder_objstat_strings));
3183 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) != ARRAY_SIZE(stats->obj_deleted));
3184 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
3185 if (stats->obj_created[i] || stats->obj_deleted[i])
3186 buf += snprintf(buf, end - buf, "%s%s: active %d total %d\n", prefix,
3187 binder_objstat_strings[i],
3188 stats->obj_created[i] - stats->obj_deleted[i],
3189 stats->obj_created[i]);
3196 static char *print_binder_proc_stats(char *buf, char *end, struct binder_proc *proc)
3198 struct binder_work *w;
3200 int count, strong, weak;
3202 buf += snprintf(buf, end - buf, "proc %d\n", proc->pid);
3206 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3208 buf += snprintf(buf, end - buf, " threads: %d\n", count);
3211 buf += snprintf(buf, end - buf, " requested threads: %d+%d/%d\n"
3212 " ready threads %d\n"
3213 " free async space %zd\n", proc->requested_threads,
3214 proc->requested_threads_started, proc->max_threads,
3215 proc->ready_threads, proc->free_async_space);
3219 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
3221 buf += snprintf(buf, end - buf, " nodes: %d\n", count);
3227 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
3228 struct binder_ref *ref = rb_entry(n, struct binder_ref, rb_node_desc);
3230 strong += ref->strong;
3233 buf += snprintf(buf, end - buf, " refs: %d s %d w %d\n", count, strong, weak);
3238 for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
3240 buf += snprintf(buf, end - buf, " buffers: %d\n", count);
3245 list_for_each_entry(w, &proc->todo, entry) {
3247 case BINDER_WORK_TRANSACTION:
3254 buf += snprintf(buf, end - buf, " pending transactions: %d\n", count);
3258 buf = print_binder_stats(buf, end, " ", &proc->stats);
3264 static int binder_read_proc_state(
3265 char *page, char **start, off_t off, int count, int *eof, void *data)
3267 struct binder_proc *proc;
3268 struct hlist_node *pos;
3269 struct binder_node *node;
3272 char *end = page + PAGE_SIZE;
3273 int do_lock = !binder_debug_no_lock;
3279 mutex_lock(&binder_lock);
3281 buf += snprintf(buf, end - buf, "binder state:\n");
3283 if (!hlist_empty(&binder_dead_nodes))
3284 buf += snprintf(buf, end - buf, "dead nodes:\n");
3285 hlist_for_each_entry(node, pos, &binder_dead_nodes, dead_node) {
3288 buf = print_binder_node(buf, end, node);
3291 hlist_for_each_entry(proc, pos, &binder_procs, proc_node) {
3294 buf = print_binder_proc(buf, end, proc, 1);
3297 mutex_unlock(&binder_lock);
3298 if (buf > page + PAGE_SIZE)
3299 buf = page + PAGE_SIZE;
3301 *start = page + off;
3309 return len < count ? len : count;
3312 static int binder_read_proc_stats(
3313 char *page, char **start, off_t off, int count, int *eof, void *data)
3315 struct binder_proc *proc;
3316 struct hlist_node *pos;
3319 int do_lock = !binder_debug_no_lock;
3325 mutex_lock(&binder_lock);
3327 p += snprintf(p, PAGE_SIZE, "binder stats:\n");
3329 p = print_binder_stats(p, page + PAGE_SIZE, "", &binder_stats);
3331 hlist_for_each_entry(proc, pos, &binder_procs, proc_node) {
3332 if (p >= page + PAGE_SIZE)
3334 p = print_binder_proc_stats(p, page + PAGE_SIZE, proc);
3337 mutex_unlock(&binder_lock);
3338 if (p > page + PAGE_SIZE)
3339 p = page + PAGE_SIZE;
3341 *start = page + off;
3349 return len < count ? len : count;
3352 static int binder_read_proc_transactions(
3353 char *page, char **start, off_t off, int count, int *eof, void *data)
3355 struct binder_proc *proc;
3356 struct hlist_node *pos;
3359 char *end = page + PAGE_SIZE;
3360 int do_lock = !binder_debug_no_lock;
3366 mutex_lock(&binder_lock);
3368 buf += snprintf(buf, end - buf, "binder transactions:\n");
3369 hlist_for_each_entry(proc, pos, &binder_procs, proc_node) {
3372 buf = print_binder_proc(buf, end, proc, 0);
3375 mutex_unlock(&binder_lock);
3376 if (buf > page + PAGE_SIZE)
3377 buf = page + PAGE_SIZE;
3379 *start = page + off;
3387 return len < count ? len : count;
3390 static int binder_read_proc_proc(
3391 char *page, char **start, off_t off, int count, int *eof, void *data)
3393 struct binder_proc *proc = data;
3396 int do_lock = !binder_debug_no_lock;
3402 mutex_lock(&binder_lock);
3403 p += snprintf(p, PAGE_SIZE, "binder proc state:\n");
3404 p = print_binder_proc(p, page + PAGE_SIZE, proc, 1);
3406 mutex_unlock(&binder_lock);
3408 if (p > page + PAGE_SIZE)
3409 p = page + PAGE_SIZE;
3410 *start = page + off;
3418 return len < count ? len : count;
3421 static char *print_binder_transaction_log_entry(char *buf, char *end, struct binder_transaction_log_entry *e)
3423 buf += snprintf(buf, end - buf, "%d: %s from %d:%d to %d:%d node %d handle %d size %d:%d\n",
3424 e->debug_id, (e->call_type == 2) ? "reply" :
3425 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
3426 e->from_thread, e->to_proc, e->to_thread, e->to_node,
3427 e->target_handle, e->data_size, e->offsets_size);
3431 static int binder_read_proc_transaction_log(
3432 char *page, char **start, off_t off, int count, int *eof, void *data)
3434 struct binder_transaction_log *log = data;
3438 char *end = page + PAGE_SIZE;
3444 for (i = log->next; i < ARRAY_SIZE(log->entry); i++) {
3447 buf = print_binder_transaction_log_entry(buf, end, &log->entry[i]);
3450 for (i = 0; i < log->next; i++) {
3453 buf = print_binder_transaction_log_entry(buf, end, &log->entry[i]);
3456 *start = page + off;
3464 return len < count ? len : count;
3467 static struct file_operations binder_fops = {
3468 .owner = THIS_MODULE,
3469 .poll = binder_poll,
3470 .unlocked_ioctl = binder_ioctl,
3471 .mmap = binder_mmap,
3472 .open = binder_open,
3473 .flush = binder_flush,
3474 .release = binder_release,
3477 static struct miscdevice binder_miscdev = {
3478 .minor = MISC_DYNAMIC_MINOR,
3480 .fops = &binder_fops
3483 static int __init binder_init(void)
3487 binder_proc_dir_entry_root = proc_mkdir("binder", NULL);
3488 if (binder_proc_dir_entry_root)
3489 binder_proc_dir_entry_proc = proc_mkdir("proc", binder_proc_dir_entry_root);
3490 ret = misc_register(&binder_miscdev);
3491 if (binder_proc_dir_entry_root) {
3492 create_proc_read_entry("state", S_IRUGO, binder_proc_dir_entry_root, binder_read_proc_state, NULL);
3493 create_proc_read_entry("stats", S_IRUGO, binder_proc_dir_entry_root, binder_read_proc_stats, NULL);
3494 create_proc_read_entry("transactions", S_IRUGO, binder_proc_dir_entry_root, binder_read_proc_transactions, NULL);
3495 create_proc_read_entry("transaction_log", S_IRUGO, binder_proc_dir_entry_root, binder_read_proc_transaction_log, &binder_transaction_log);
3496 create_proc_read_entry("failed_transaction_log", S_IRUGO, binder_proc_dir_entry_root, binder_read_proc_transaction_log, &binder_transaction_log_failed);
3501 device_initcall(binder_init);
3503 MODULE_LICENSE("GPL v2");