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/debugfs.h>
30 #include <linux/rbtree.h>
31 #include <linux/sched.h>
32 #include <linux/seq_file.h>
33 #include <linux/uaccess.h>
34 #include <linux/vmalloc.h>
35 #include <linux/slab.h>
39 static DEFINE_MUTEX(binder_lock);
40 static DEFINE_MUTEX(binder_deferred_lock);
41 static DEFINE_MUTEX(binder_mmap_lock);
43 static HLIST_HEAD(binder_procs);
44 static HLIST_HEAD(binder_deferred_list);
45 static HLIST_HEAD(binder_dead_nodes);
47 static struct dentry *binder_debugfs_dir_entry_root;
48 static struct dentry *binder_debugfs_dir_entry_proc;
49 static struct binder_node *binder_context_mgr_node;
50 static uid_t binder_context_mgr_uid = -1;
51 static int binder_last_id;
52 static struct workqueue_struct *binder_deferred_workqueue;
54 #define BINDER_DEBUG_ENTRY(name) \
55 static int binder_##name##_open(struct inode *inode, struct file *file) \
57 return single_open(file, binder_##name##_show, inode->i_private); \
60 static const struct file_operations binder_##name##_fops = { \
61 .owner = THIS_MODULE, \
62 .open = binder_##name##_open, \
64 .llseek = seq_lseek, \
65 .release = single_release, \
68 static int binder_proc_show(struct seq_file *m, void *unused);
69 BINDER_DEBUG_ENTRY(proc);
71 /* This is only defined in include/asm-arm/sizes.h */
77 #define SZ_4M 0x400000
80 #define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
82 #define BINDER_SMALL_BUF_SIZE (PAGE_SIZE * 64)
85 BINDER_DEBUG_USER_ERROR = 1U << 0,
86 BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1,
87 BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2,
88 BINDER_DEBUG_OPEN_CLOSE = 1U << 3,
89 BINDER_DEBUG_DEAD_BINDER = 1U << 4,
90 BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5,
91 BINDER_DEBUG_READ_WRITE = 1U << 6,
92 BINDER_DEBUG_USER_REFS = 1U << 7,
93 BINDER_DEBUG_THREADS = 1U << 8,
94 BINDER_DEBUG_TRANSACTION = 1U << 9,
95 BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10,
96 BINDER_DEBUG_FREE_BUFFER = 1U << 11,
97 BINDER_DEBUG_INTERNAL_REFS = 1U << 12,
98 BINDER_DEBUG_BUFFER_ALLOC = 1U << 13,
99 BINDER_DEBUG_PRIORITY_CAP = 1U << 14,
100 BINDER_DEBUG_BUFFER_ALLOC_ASYNC = 1U << 15,
102 static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR |
103 BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION;
104 module_param_named(debug_mask, binder_debug_mask, uint, S_IWUSR | S_IRUGO);
106 static int binder_debug_no_lock;
107 module_param_named(proc_no_lock, binder_debug_no_lock, bool, S_IWUSR | S_IRUGO);
109 static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
110 static int binder_stop_on_user_error;
112 static int binder_set_stop_on_user_error(const char *val,
113 struct kernel_param *kp)
116 ret = param_set_int(val, kp);
117 if (binder_stop_on_user_error < 2)
118 wake_up(&binder_user_error_wait);
121 module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
122 param_get_int, &binder_stop_on_user_error, S_IWUSR | S_IRUGO);
124 #define binder_debug(mask, x...) \
126 if (binder_debug_mask & mask) \
127 printk(KERN_INFO x); \
130 #define binder_user_error(x...) \
132 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
133 printk(KERN_INFO x); \
134 if (binder_stop_on_user_error) \
135 binder_stop_on_user_error = 2; \
138 enum binder_stat_types {
144 BINDER_STAT_TRANSACTION,
145 BINDER_STAT_TRANSACTION_COMPLETE,
149 struct binder_stats {
150 int br[_IOC_NR(BR_FAILED_REPLY) + 1];
151 int bc[_IOC_NR(BC_DEAD_BINDER_DONE) + 1];
152 int obj_created[BINDER_STAT_COUNT];
153 int obj_deleted[BINDER_STAT_COUNT];
156 static struct binder_stats binder_stats;
158 static inline void binder_stats_deleted(enum binder_stat_types type)
160 binder_stats.obj_deleted[type]++;
163 static inline void binder_stats_created(enum binder_stat_types type)
165 binder_stats.obj_created[type]++;
168 struct binder_transaction_log_entry {
180 struct binder_transaction_log {
183 struct binder_transaction_log_entry entry[32];
185 static struct binder_transaction_log binder_transaction_log;
186 static struct binder_transaction_log binder_transaction_log_failed;
188 static struct binder_transaction_log_entry *binder_transaction_log_add(
189 struct binder_transaction_log *log)
191 struct binder_transaction_log_entry *e;
192 e = &log->entry[log->next];
193 memset(e, 0, sizeof(*e));
195 if (log->next == ARRAY_SIZE(log->entry)) {
203 struct list_head entry;
205 BINDER_WORK_TRANSACTION = 1,
206 BINDER_WORK_TRANSACTION_COMPLETE,
208 BINDER_WORK_DEAD_BINDER,
209 BINDER_WORK_DEAD_BINDER_AND_CLEAR,
210 BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
216 struct binder_work work;
218 struct rb_node rb_node;
219 struct hlist_node dead_node;
221 struct binder_proc *proc;
222 struct hlist_head refs;
223 int internal_strong_refs;
225 int local_strong_refs;
228 unsigned has_strong_ref:1;
229 unsigned pending_strong_ref:1;
230 unsigned has_weak_ref:1;
231 unsigned pending_weak_ref:1;
232 unsigned has_async_transaction:1;
233 unsigned accept_fds:1;
234 unsigned min_priority:8;
235 struct list_head async_todo;
238 struct binder_ref_death {
239 struct binder_work work;
244 /* Lookups needed: */
245 /* node + proc => ref (transaction) */
246 /* desc + proc => ref (transaction, inc/dec ref) */
247 /* node => refs + procs (proc exit) */
249 struct rb_node rb_node_desc;
250 struct rb_node rb_node_node;
251 struct hlist_node node_entry;
252 struct binder_proc *proc;
253 struct binder_node *node;
257 struct binder_ref_death *death;
260 struct binder_buffer {
261 struct list_head entry; /* free and allocated entries by addesss */
262 struct rb_node rb_node; /* free entry by size or allocated entry */
265 unsigned allow_user_free:1;
266 unsigned async_transaction:1;
267 unsigned debug_id:29;
269 struct binder_transaction *transaction;
271 struct binder_node *target_node;
277 enum binder_deferred_state {
278 BINDER_DEFERRED_PUT_FILES = 0x01,
279 BINDER_DEFERRED_FLUSH = 0x02,
280 BINDER_DEFERRED_RELEASE = 0x04,
284 struct hlist_node proc_node;
285 struct rb_root threads;
286 struct rb_root nodes;
287 struct rb_root refs_by_desc;
288 struct rb_root refs_by_node;
290 struct vm_area_struct *vma;
291 struct task_struct *tsk;
292 struct files_struct *files;
293 struct hlist_node deferred_work_node;
296 ptrdiff_t user_buffer_offset;
298 struct list_head buffers;
299 struct rb_root free_buffers;
300 struct rb_root allocated_buffers;
301 size_t free_async_space;
305 uint32_t buffer_free;
306 struct list_head todo;
307 wait_queue_head_t wait;
308 struct binder_stats stats;
309 struct list_head delivered_death;
311 int requested_threads;
312 int requested_threads_started;
314 long default_priority;
315 struct dentry *debugfs_entry;
319 BINDER_LOOPER_STATE_REGISTERED = 0x01,
320 BINDER_LOOPER_STATE_ENTERED = 0x02,
321 BINDER_LOOPER_STATE_EXITED = 0x04,
322 BINDER_LOOPER_STATE_INVALID = 0x08,
323 BINDER_LOOPER_STATE_WAITING = 0x10,
324 BINDER_LOOPER_STATE_NEED_RETURN = 0x20
327 struct binder_thread {
328 struct binder_proc *proc;
329 struct rb_node rb_node;
332 struct binder_transaction *transaction_stack;
333 struct list_head todo;
334 uint32_t return_error; /* Write failed, return error code in read buf */
335 uint32_t return_error2; /* Write failed, return error code in read */
336 /* buffer. Used when sending a reply to a dead process that */
337 /* we are also waiting on */
338 wait_queue_head_t wait;
339 struct binder_stats stats;
342 struct binder_transaction {
344 struct binder_work work;
345 struct binder_thread *from;
346 struct binder_transaction *from_parent;
347 struct binder_proc *to_proc;
348 struct binder_thread *to_thread;
349 struct binder_transaction *to_parent;
350 unsigned need_reply:1;
351 /* unsigned is_dead:1; */ /* not used at the moment */
353 struct binder_buffer *buffer;
362 binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
365 * copied from get_unused_fd_flags
367 int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
369 struct files_struct *files = proc->files;
372 unsigned long rlim_cur;
379 spin_lock(&files->file_lock);
382 fdt = files_fdtable(files);
383 fd = find_next_zero_bit(fdt->open_fds, fdt->max_fds, files->next_fd);
386 * N.B. For clone tasks sharing a files structure, this test
387 * will limit the total number of files that can be opened.
390 if (lock_task_sighand(proc->tsk, &irqs)) {
391 rlim_cur = proc->tsk->signal->rlim[RLIMIT_NOFILE].rlim_cur;
392 unlock_task_sighand(proc->tsk, &irqs);
397 /* Do we need to expand the fd array or fd set? */
398 error = expand_files(files, fd);
404 * If we needed to expand the fs array we
405 * might have blocked - try again.
411 __set_open_fd(fd, fdt);
412 if (flags & O_CLOEXEC)
413 __set_close_on_exec(fd, fdt);
415 __clear_close_on_exec(fd, fdt);
416 files->next_fd = fd + 1;
419 if (fdt->fd[fd] != NULL) {
420 printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
427 spin_unlock(&files->file_lock);
432 * copied from fd_install
434 static void task_fd_install(
435 struct binder_proc *proc, unsigned int fd, struct file *file)
437 struct files_struct *files = proc->files;
443 spin_lock(&files->file_lock);
444 fdt = files_fdtable(files);
445 BUG_ON(fdt->fd[fd] != NULL);
446 rcu_assign_pointer(fdt->fd[fd], file);
447 spin_unlock(&files->file_lock);
451 * copied from __put_unused_fd in open.c
453 static void __put_unused_fd(struct files_struct *files, unsigned int fd)
455 struct fdtable *fdt = files_fdtable(files);
456 __clear_open_fd(fd, fdt);
457 if (fd < files->next_fd)
462 * copied from sys_close
464 static long task_close_fd(struct binder_proc *proc, unsigned int fd)
467 struct files_struct *files = proc->files;
474 spin_lock(&files->file_lock);
475 fdt = files_fdtable(files);
476 if (fd >= fdt->max_fds)
481 rcu_assign_pointer(fdt->fd[fd], NULL);
482 __clear_close_on_exec(fd, fdt);
483 __put_unused_fd(files, fd);
484 spin_unlock(&files->file_lock);
485 retval = filp_close(filp, files);
487 /* can't restart close syscall because file table entry was cleared */
488 if (unlikely(retval == -ERESTARTSYS ||
489 retval == -ERESTARTNOINTR ||
490 retval == -ERESTARTNOHAND ||
491 retval == -ERESTART_RESTARTBLOCK))
497 spin_unlock(&files->file_lock);
501 static void binder_set_nice(long nice)
504 if (can_nice(current, nice)) {
505 set_user_nice(current, nice);
508 min_nice = 20 - current->signal->rlim[RLIMIT_NICE].rlim_cur;
509 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
510 "binder: %d: nice value %ld not allowed use "
511 "%ld instead\n", current->pid, nice, min_nice);
512 set_user_nice(current, min_nice);
515 binder_user_error("binder: %d RLIMIT_NICE not set\n", current->pid);
518 static size_t binder_buffer_size(struct binder_proc *proc,
519 struct binder_buffer *buffer)
521 if (list_is_last(&buffer->entry, &proc->buffers))
522 return proc->buffer + proc->buffer_size - (void *)buffer->data;
524 return (size_t)list_entry(buffer->entry.next,
525 struct binder_buffer, entry) - (size_t)buffer->data;
528 static void binder_insert_free_buffer(struct binder_proc *proc,
529 struct binder_buffer *new_buffer)
531 struct rb_node **p = &proc->free_buffers.rb_node;
532 struct rb_node *parent = NULL;
533 struct binder_buffer *buffer;
535 size_t new_buffer_size;
537 BUG_ON(!new_buffer->free);
539 new_buffer_size = binder_buffer_size(proc, new_buffer);
541 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
542 "binder: %d: add free buffer, size %zd, "
543 "at %p\n", proc->pid, new_buffer_size, new_buffer);
547 buffer = rb_entry(parent, struct binder_buffer, rb_node);
548 BUG_ON(!buffer->free);
550 buffer_size = binder_buffer_size(proc, buffer);
552 if (new_buffer_size < buffer_size)
553 p = &parent->rb_left;
555 p = &parent->rb_right;
557 rb_link_node(&new_buffer->rb_node, parent, p);
558 rb_insert_color(&new_buffer->rb_node, &proc->free_buffers);
561 static void binder_insert_allocated_buffer(struct binder_proc *proc,
562 struct binder_buffer *new_buffer)
564 struct rb_node **p = &proc->allocated_buffers.rb_node;
565 struct rb_node *parent = NULL;
566 struct binder_buffer *buffer;
568 BUG_ON(new_buffer->free);
572 buffer = rb_entry(parent, struct binder_buffer, rb_node);
573 BUG_ON(buffer->free);
575 if (new_buffer < buffer)
576 p = &parent->rb_left;
577 else if (new_buffer > buffer)
578 p = &parent->rb_right;
582 rb_link_node(&new_buffer->rb_node, parent, p);
583 rb_insert_color(&new_buffer->rb_node, &proc->allocated_buffers);
586 static struct binder_buffer *binder_buffer_lookup(struct binder_proc *proc,
587 void __user *user_ptr)
589 struct rb_node *n = proc->allocated_buffers.rb_node;
590 struct binder_buffer *buffer;
591 struct binder_buffer *kern_ptr;
593 kern_ptr = user_ptr - proc->user_buffer_offset
594 - offsetof(struct binder_buffer, data);
597 buffer = rb_entry(n, struct binder_buffer, rb_node);
598 BUG_ON(buffer->free);
600 if (kern_ptr < buffer)
602 else if (kern_ptr > buffer)
610 static int binder_update_page_range(struct binder_proc *proc, int allocate,
611 void *start, void *end,
612 struct vm_area_struct *vma)
615 unsigned long user_page_addr;
616 struct vm_struct tmp_area;
618 struct mm_struct *mm;
620 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
621 "binder: %d: %s pages %p-%p\n", proc->pid,
622 allocate ? "allocate" : "free", start, end);
630 mm = get_task_mm(proc->tsk);
633 down_write(&mm->mmap_sem);
635 if (vma && mm != vma->vm_mm) {
636 pr_err("binder: %d: vma mm and task mm mismatch\n",
646 printk(KERN_ERR "binder: %d: binder_alloc_buf failed to "
647 "map pages in userspace, no vma\n", proc->pid);
651 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
653 struct page **page_array_ptr;
654 page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
657 *page = alloc_page(GFP_KERNEL | __GFP_ZERO);
659 printk(KERN_ERR "binder: %d: binder_alloc_buf failed "
660 "for page at %p\n", proc->pid, page_addr);
661 goto err_alloc_page_failed;
663 tmp_area.addr = page_addr;
664 tmp_area.size = PAGE_SIZE + PAGE_SIZE /* guard page? */;
665 page_array_ptr = page;
666 ret = map_vm_area(&tmp_area, PAGE_KERNEL, &page_array_ptr);
668 printk(KERN_ERR "binder: %d: binder_alloc_buf failed "
669 "to map page at %p in kernel\n",
670 proc->pid, page_addr);
671 goto err_map_kernel_failed;
674 (uintptr_t)page_addr + proc->user_buffer_offset;
675 ret = vm_insert_page(vma, user_page_addr, page[0]);
677 printk(KERN_ERR "binder: %d: binder_alloc_buf failed "
678 "to map page at %lx in userspace\n",
679 proc->pid, user_page_addr);
680 goto err_vm_insert_page_failed;
682 /* vm_insert_page does not seem to increment the refcount */
685 up_write(&mm->mmap_sem);
691 for (page_addr = end - PAGE_SIZE; page_addr >= start;
692 page_addr -= PAGE_SIZE) {
693 page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
695 zap_page_range(vma, (uintptr_t)page_addr +
696 proc->user_buffer_offset, PAGE_SIZE, NULL);
697 err_vm_insert_page_failed:
698 unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
699 err_map_kernel_failed:
702 err_alloc_page_failed:
707 up_write(&mm->mmap_sem);
713 static struct binder_buffer *binder_alloc_buf(struct binder_proc *proc,
715 size_t offsets_size, int is_async)
717 struct rb_node *n = proc->free_buffers.rb_node;
718 struct binder_buffer *buffer;
720 struct rb_node *best_fit = NULL;
725 if (proc->vma == NULL) {
726 printk(KERN_ERR "binder: %d: binder_alloc_buf, no vma\n",
731 size = ALIGN(data_size, sizeof(void *)) +
732 ALIGN(offsets_size, sizeof(void *));
734 if (size < data_size || size < offsets_size) {
735 binder_user_error("binder: %d: got transaction with invalid "
736 "size %zd-%zd\n", proc->pid, data_size, offsets_size);
741 proc->free_async_space < size + sizeof(struct binder_buffer)) {
742 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
743 "binder: %d: binder_alloc_buf size %zd"
744 "failed, no async space left\n", proc->pid, size);
749 buffer = rb_entry(n, struct binder_buffer, rb_node);
750 BUG_ON(!buffer->free);
751 buffer_size = binder_buffer_size(proc, buffer);
753 if (size < buffer_size) {
756 } else if (size > buffer_size)
763 if (best_fit == NULL) {
764 printk(KERN_ERR "binder: %d: binder_alloc_buf size %zd failed, "
765 "no address space\n", proc->pid, size);
769 buffer = rb_entry(best_fit, struct binder_buffer, rb_node);
770 buffer_size = binder_buffer_size(proc, buffer);
773 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
774 "binder: %d: binder_alloc_buf size %zd got buff"
775 "er %p size %zd\n", proc->pid, size, buffer, buffer_size);
778 (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK);
780 if (size + sizeof(struct binder_buffer) + 4 >= buffer_size)
781 buffer_size = size; /* no room for other buffers */
783 buffer_size = size + sizeof(struct binder_buffer);
786 (void *)PAGE_ALIGN((uintptr_t)buffer->data + buffer_size);
787 if (end_page_addr > has_page_addr)
788 end_page_addr = has_page_addr;
789 if (binder_update_page_range(proc, 1,
790 (void *)PAGE_ALIGN((uintptr_t)buffer->data), end_page_addr, NULL))
793 rb_erase(best_fit, &proc->free_buffers);
795 binder_insert_allocated_buffer(proc, buffer);
796 if (buffer_size != size) {
797 struct binder_buffer *new_buffer = (void *)buffer->data + size;
798 list_add(&new_buffer->entry, &buffer->entry);
799 new_buffer->free = 1;
800 binder_insert_free_buffer(proc, new_buffer);
802 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
803 "binder: %d: binder_alloc_buf size %zd got "
804 "%p\n", proc->pid, size, buffer);
805 buffer->data_size = data_size;
806 buffer->offsets_size = offsets_size;
807 buffer->async_transaction = is_async;
809 proc->free_async_space -= size + sizeof(struct binder_buffer);
810 binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
811 "binder: %d: binder_alloc_buf size %zd "
812 "async free %zd\n", proc->pid, size,
813 proc->free_async_space);
819 static void *buffer_start_page(struct binder_buffer *buffer)
821 return (void *)((uintptr_t)buffer & PAGE_MASK);
824 static void *buffer_end_page(struct binder_buffer *buffer)
826 return (void *)(((uintptr_t)(buffer + 1) - 1) & PAGE_MASK);
829 static void binder_delete_free_buffer(struct binder_proc *proc,
830 struct binder_buffer *buffer)
832 struct binder_buffer *prev, *next = NULL;
833 int free_page_end = 1;
834 int free_page_start = 1;
836 BUG_ON(proc->buffers.next == &buffer->entry);
837 prev = list_entry(buffer->entry.prev, struct binder_buffer, entry);
839 if (buffer_end_page(prev) == buffer_start_page(buffer)) {
841 if (buffer_end_page(prev) == buffer_end_page(buffer))
843 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
844 "binder: %d: merge free, buffer %p "
845 "share page with %p\n", proc->pid, buffer, prev);
848 if (!list_is_last(&buffer->entry, &proc->buffers)) {
849 next = list_entry(buffer->entry.next,
850 struct binder_buffer, entry);
851 if (buffer_start_page(next) == buffer_end_page(buffer)) {
853 if (buffer_start_page(next) ==
854 buffer_start_page(buffer))
856 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
857 "binder: %d: merge free, buffer"
858 " %p share page with %p\n", proc->pid,
862 list_del(&buffer->entry);
863 if (free_page_start || free_page_end) {
864 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
865 "binder: %d: merge free, buffer %p do "
866 "not share page%s%s with with %p or %p\n",
867 proc->pid, buffer, free_page_start ? "" : " end",
868 free_page_end ? "" : " start", prev, next);
869 binder_update_page_range(proc, 0, free_page_start ?
870 buffer_start_page(buffer) : buffer_end_page(buffer),
871 (free_page_end ? buffer_end_page(buffer) :
872 buffer_start_page(buffer)) + PAGE_SIZE, NULL);
876 static void binder_free_buf(struct binder_proc *proc,
877 struct binder_buffer *buffer)
879 size_t size, buffer_size;
881 buffer_size = binder_buffer_size(proc, buffer);
883 size = ALIGN(buffer->data_size, sizeof(void *)) +
884 ALIGN(buffer->offsets_size, sizeof(void *));
886 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
887 "binder: %d: binder_free_buf %p size %zd buffer"
888 "_size %zd\n", proc->pid, buffer, size, buffer_size);
890 BUG_ON(buffer->free);
891 BUG_ON(size > buffer_size);
892 BUG_ON(buffer->transaction != NULL);
893 BUG_ON((void *)buffer < proc->buffer);
894 BUG_ON((void *)buffer > proc->buffer + proc->buffer_size);
896 if (buffer->async_transaction) {
897 proc->free_async_space += size + sizeof(struct binder_buffer);
899 binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
900 "binder: %d: binder_free_buf size %zd "
901 "async free %zd\n", proc->pid, size,
902 proc->free_async_space);
905 binder_update_page_range(proc, 0,
906 (void *)PAGE_ALIGN((uintptr_t)buffer->data),
907 (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK),
909 rb_erase(&buffer->rb_node, &proc->allocated_buffers);
911 if (!list_is_last(&buffer->entry, &proc->buffers)) {
912 struct binder_buffer *next = list_entry(buffer->entry.next,
913 struct binder_buffer, entry);
915 rb_erase(&next->rb_node, &proc->free_buffers);
916 binder_delete_free_buffer(proc, next);
919 if (proc->buffers.next != &buffer->entry) {
920 struct binder_buffer *prev = list_entry(buffer->entry.prev,
921 struct binder_buffer, entry);
923 binder_delete_free_buffer(proc, buffer);
924 rb_erase(&prev->rb_node, &proc->free_buffers);
928 binder_insert_free_buffer(proc, buffer);
931 static struct binder_node *binder_get_node(struct binder_proc *proc,
934 struct rb_node *n = proc->nodes.rb_node;
935 struct binder_node *node;
938 node = rb_entry(n, struct binder_node, rb_node);
942 else if (ptr > node->ptr)
950 static struct binder_node *binder_new_node(struct binder_proc *proc,
954 struct rb_node **p = &proc->nodes.rb_node;
955 struct rb_node *parent = NULL;
956 struct binder_node *node;
960 node = rb_entry(parent, struct binder_node, rb_node);
964 else if (ptr > node->ptr)
970 node = kzalloc(sizeof(*node), GFP_KERNEL);
973 binder_stats_created(BINDER_STAT_NODE);
974 rb_link_node(&node->rb_node, parent, p);
975 rb_insert_color(&node->rb_node, &proc->nodes);
976 node->debug_id = ++binder_last_id;
979 node->cookie = cookie;
980 node->work.type = BINDER_WORK_NODE;
981 INIT_LIST_HEAD(&node->work.entry);
982 INIT_LIST_HEAD(&node->async_todo);
983 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
984 "binder: %d:%d node %d u%p c%p created\n",
985 proc->pid, current->pid, node->debug_id,
986 node->ptr, node->cookie);
990 static int binder_inc_node(struct binder_node *node, int strong, int internal,
991 struct list_head *target_list)
995 if (target_list == NULL &&
996 node->internal_strong_refs == 0 &&
997 !(node == binder_context_mgr_node &&
998 node->has_strong_ref)) {
999 printk(KERN_ERR "binder: invalid inc strong "
1000 "node for %d\n", node->debug_id);
1003 node->internal_strong_refs++;
1005 node->local_strong_refs++;
1006 if (!node->has_strong_ref && target_list) {
1007 list_del_init(&node->work.entry);
1008 list_add_tail(&node->work.entry, target_list);
1012 node->local_weak_refs++;
1013 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
1014 if (target_list == NULL) {
1015 printk(KERN_ERR "binder: invalid inc weak node "
1016 "for %d\n", node->debug_id);
1019 list_add_tail(&node->work.entry, target_list);
1025 static int binder_dec_node(struct binder_node *node, int strong, int internal)
1029 node->internal_strong_refs--;
1031 node->local_strong_refs--;
1032 if (node->local_strong_refs || node->internal_strong_refs)
1036 node->local_weak_refs--;
1037 if (node->local_weak_refs || !hlist_empty(&node->refs))
1040 if (node->proc && (node->has_strong_ref || node->has_weak_ref)) {
1041 if (list_empty(&node->work.entry)) {
1042 list_add_tail(&node->work.entry, &node->proc->todo);
1043 wake_up_interruptible(&node->proc->wait);
1046 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
1047 !node->local_weak_refs) {
1048 list_del_init(&node->work.entry);
1050 rb_erase(&node->rb_node, &node->proc->nodes);
1051 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1052 "binder: refless node %d deleted\n",
1055 hlist_del(&node->dead_node);
1056 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1057 "binder: dead node %d deleted\n",
1061 binder_stats_deleted(BINDER_STAT_NODE);
1069 static struct binder_ref *binder_get_ref(struct binder_proc *proc,
1072 struct rb_node *n = proc->refs_by_desc.rb_node;
1073 struct binder_ref *ref;
1076 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1078 if (desc < ref->desc)
1080 else if (desc > ref->desc)
1088 static struct binder_ref *binder_get_ref_for_node(struct binder_proc *proc,
1089 struct binder_node *node)
1092 struct rb_node **p = &proc->refs_by_node.rb_node;
1093 struct rb_node *parent = NULL;
1094 struct binder_ref *ref, *new_ref;
1098 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1100 if (node < ref->node)
1102 else if (node > ref->node)
1103 p = &(*p)->rb_right;
1107 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1108 if (new_ref == NULL)
1110 binder_stats_created(BINDER_STAT_REF);
1111 new_ref->debug_id = ++binder_last_id;
1112 new_ref->proc = proc;
1113 new_ref->node = node;
1114 rb_link_node(&new_ref->rb_node_node, parent, p);
1115 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1117 new_ref->desc = (node == binder_context_mgr_node) ? 0 : 1;
1118 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1119 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1120 if (ref->desc > new_ref->desc)
1122 new_ref->desc = ref->desc + 1;
1125 p = &proc->refs_by_desc.rb_node;
1128 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1130 if (new_ref->desc < ref->desc)
1132 else if (new_ref->desc > ref->desc)
1133 p = &(*p)->rb_right;
1137 rb_link_node(&new_ref->rb_node_desc, parent, p);
1138 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
1140 hlist_add_head(&new_ref->node_entry, &node->refs);
1142 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1143 "binder: %d new ref %d desc %d for "
1144 "node %d\n", proc->pid, new_ref->debug_id,
1145 new_ref->desc, node->debug_id);
1147 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1148 "binder: %d new ref %d desc %d for "
1149 "dead node\n", proc->pid, new_ref->debug_id,
1155 static void binder_delete_ref(struct binder_ref *ref)
1157 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1158 "binder: %d delete ref %d desc %d for "
1159 "node %d\n", ref->proc->pid, ref->debug_id,
1160 ref->desc, ref->node->debug_id);
1162 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1163 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
1165 binder_dec_node(ref->node, 1, 1);
1166 hlist_del(&ref->node_entry);
1167 binder_dec_node(ref->node, 0, 1);
1169 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1170 "binder: %d delete ref %d desc %d "
1171 "has death notification\n", ref->proc->pid,
1172 ref->debug_id, ref->desc);
1173 list_del(&ref->death->work.entry);
1175 binder_stats_deleted(BINDER_STAT_DEATH);
1178 binder_stats_deleted(BINDER_STAT_REF);
1181 static int binder_inc_ref(struct binder_ref *ref, int strong,
1182 struct list_head *target_list)
1186 if (ref->strong == 0) {
1187 ret = binder_inc_node(ref->node, 1, 1, target_list);
1193 if (ref->weak == 0) {
1194 ret = binder_inc_node(ref->node, 0, 1, target_list);
1204 static int binder_dec_ref(struct binder_ref *ref, int strong)
1207 if (ref->strong == 0) {
1208 binder_user_error("binder: %d invalid dec strong, "
1209 "ref %d desc %d s %d w %d\n",
1210 ref->proc->pid, ref->debug_id,
1211 ref->desc, ref->strong, ref->weak);
1215 if (ref->strong == 0) {
1217 ret = binder_dec_node(ref->node, strong, 1);
1222 if (ref->weak == 0) {
1223 binder_user_error("binder: %d invalid dec weak, "
1224 "ref %d desc %d s %d w %d\n",
1225 ref->proc->pid, ref->debug_id,
1226 ref->desc, ref->strong, ref->weak);
1231 if (ref->strong == 0 && ref->weak == 0)
1232 binder_delete_ref(ref);
1236 static void binder_pop_transaction(struct binder_thread *target_thread,
1237 struct binder_transaction *t)
1239 if (target_thread) {
1240 BUG_ON(target_thread->transaction_stack != t);
1241 BUG_ON(target_thread->transaction_stack->from != target_thread);
1242 target_thread->transaction_stack =
1243 target_thread->transaction_stack->from_parent;
1248 t->buffer->transaction = NULL;
1250 binder_stats_deleted(BINDER_STAT_TRANSACTION);
1253 static void binder_send_failed_reply(struct binder_transaction *t,
1254 uint32_t error_code)
1256 struct binder_thread *target_thread;
1257 BUG_ON(t->flags & TF_ONE_WAY);
1259 target_thread = t->from;
1260 if (target_thread) {
1261 if (target_thread->return_error != BR_OK &&
1262 target_thread->return_error2 == BR_OK) {
1263 target_thread->return_error2 =
1264 target_thread->return_error;
1265 target_thread->return_error = BR_OK;
1267 if (target_thread->return_error == BR_OK) {
1268 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1269 "binder: send failed reply for "
1270 "transaction %d to %d:%d\n",
1271 t->debug_id, target_thread->proc->pid,
1272 target_thread->pid);
1274 binder_pop_transaction(target_thread, t);
1275 target_thread->return_error = error_code;
1276 wake_up_interruptible(&target_thread->wait);
1278 printk(KERN_ERR "binder: reply failed, target "
1279 "thread, %d:%d, has error code %d "
1280 "already\n", target_thread->proc->pid,
1282 target_thread->return_error);
1286 struct binder_transaction *next = t->from_parent;
1288 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1289 "binder: send failed reply "
1290 "for transaction %d, target dead\n",
1293 binder_pop_transaction(target_thread, t);
1295 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1296 "binder: reply failed,"
1297 " no target thread at root\n");
1301 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1302 "binder: reply failed, no target "
1303 "thread -- retry %d\n", t->debug_id);
1308 static void binder_transaction_buffer_release(struct binder_proc *proc,
1309 struct binder_buffer *buffer,
1312 size_t *offp, *off_end;
1313 int debug_id = buffer->debug_id;
1315 binder_debug(BINDER_DEBUG_TRANSACTION,
1316 "binder: %d buffer release %d, size %zd-%zd, failed at %p\n",
1317 proc->pid, buffer->debug_id,
1318 buffer->data_size, buffer->offsets_size, failed_at);
1320 if (buffer->target_node)
1321 binder_dec_node(buffer->target_node, 1, 0);
1323 offp = (size_t *)(buffer->data + ALIGN(buffer->data_size, sizeof(void *)));
1325 off_end = failed_at;
1327 off_end = (void *)offp + buffer->offsets_size;
1328 for (; offp < off_end; offp++) {
1329 struct flat_binder_object *fp;
1330 if (*offp > buffer->data_size - sizeof(*fp) ||
1331 buffer->data_size < sizeof(*fp) ||
1332 !IS_ALIGNED(*offp, sizeof(void *))) {
1333 printk(KERN_ERR "binder: transaction release %d bad"
1334 "offset %zd, size %zd\n", debug_id,
1335 *offp, buffer->data_size);
1338 fp = (struct flat_binder_object *)(buffer->data + *offp);
1340 case BINDER_TYPE_BINDER:
1341 case BINDER_TYPE_WEAK_BINDER: {
1342 struct binder_node *node = binder_get_node(proc, fp->binder);
1344 printk(KERN_ERR "binder: transaction release %d"
1345 " bad node %p\n", debug_id, fp->binder);
1348 binder_debug(BINDER_DEBUG_TRANSACTION,
1350 node->debug_id, node->ptr);
1351 binder_dec_node(node, fp->type == BINDER_TYPE_BINDER, 0);
1353 case BINDER_TYPE_HANDLE:
1354 case BINDER_TYPE_WEAK_HANDLE: {
1355 struct binder_ref *ref = binder_get_ref(proc, fp->handle);
1357 printk(KERN_ERR "binder: transaction release %d"
1358 " bad handle %ld\n", debug_id,
1362 binder_debug(BINDER_DEBUG_TRANSACTION,
1363 " ref %d desc %d (node %d)\n",
1364 ref->debug_id, ref->desc, ref->node->debug_id);
1365 binder_dec_ref(ref, fp->type == BINDER_TYPE_HANDLE);
1368 case BINDER_TYPE_FD:
1369 binder_debug(BINDER_DEBUG_TRANSACTION,
1370 " fd %ld\n", fp->handle);
1372 task_close_fd(proc, fp->handle);
1376 printk(KERN_ERR "binder: transaction release %d bad "
1377 "object type %lx\n", debug_id, fp->type);
1383 static void binder_transaction(struct binder_proc *proc,
1384 struct binder_thread *thread,
1385 struct binder_transaction_data *tr, int reply)
1387 struct binder_transaction *t;
1388 struct binder_work *tcomplete;
1389 size_t *offp, *off_end;
1390 struct binder_proc *target_proc;
1391 struct binder_thread *target_thread = NULL;
1392 struct binder_node *target_node = NULL;
1393 struct list_head *target_list;
1394 wait_queue_head_t *target_wait;
1395 struct binder_transaction *in_reply_to = NULL;
1396 struct binder_transaction_log_entry *e;
1397 uint32_t return_error;
1399 e = binder_transaction_log_add(&binder_transaction_log);
1400 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
1401 e->from_proc = proc->pid;
1402 e->from_thread = thread->pid;
1403 e->target_handle = tr->target.handle;
1404 e->data_size = tr->data_size;
1405 e->offsets_size = tr->offsets_size;
1408 in_reply_to = thread->transaction_stack;
1409 if (in_reply_to == NULL) {
1410 binder_user_error("binder: %d:%d got reply transaction "
1411 "with no transaction stack\n",
1412 proc->pid, thread->pid);
1413 return_error = BR_FAILED_REPLY;
1414 goto err_empty_call_stack;
1416 binder_set_nice(in_reply_to->saved_priority);
1417 if (in_reply_to->to_thread != thread) {
1418 binder_user_error("binder: %d:%d got reply transaction "
1419 "with bad transaction stack,"
1420 " transaction %d has target %d:%d\n",
1421 proc->pid, thread->pid, in_reply_to->debug_id,
1422 in_reply_to->to_proc ?
1423 in_reply_to->to_proc->pid : 0,
1424 in_reply_to->to_thread ?
1425 in_reply_to->to_thread->pid : 0);
1426 return_error = BR_FAILED_REPLY;
1428 goto err_bad_call_stack;
1430 thread->transaction_stack = in_reply_to->to_parent;
1431 target_thread = in_reply_to->from;
1432 if (target_thread == NULL) {
1433 return_error = BR_DEAD_REPLY;
1434 goto err_dead_binder;
1436 if (target_thread->transaction_stack != in_reply_to) {
1437 binder_user_error("binder: %d:%d got reply transaction "
1438 "with bad target transaction stack %d, "
1440 proc->pid, thread->pid,
1441 target_thread->transaction_stack ?
1442 target_thread->transaction_stack->debug_id : 0,
1443 in_reply_to->debug_id);
1444 return_error = BR_FAILED_REPLY;
1446 target_thread = NULL;
1447 goto err_dead_binder;
1449 target_proc = target_thread->proc;
1451 if (tr->target.handle) {
1452 struct binder_ref *ref;
1453 ref = binder_get_ref(proc, tr->target.handle);
1455 binder_user_error("binder: %d:%d got "
1456 "transaction to invalid handle\n",
1457 proc->pid, thread->pid);
1458 return_error = BR_FAILED_REPLY;
1459 goto err_invalid_target_handle;
1461 target_node = ref->node;
1463 target_node = binder_context_mgr_node;
1464 if (target_node == NULL) {
1465 return_error = BR_DEAD_REPLY;
1466 goto err_no_context_mgr_node;
1469 e->to_node = target_node->debug_id;
1470 target_proc = target_node->proc;
1471 if (target_proc == NULL) {
1472 return_error = BR_DEAD_REPLY;
1473 goto err_dead_binder;
1475 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
1476 struct binder_transaction *tmp;
1477 tmp = thread->transaction_stack;
1478 if (tmp->to_thread != thread) {
1479 binder_user_error("binder: %d:%d got new "
1480 "transaction with bad transaction stack"
1481 ", transaction %d has target %d:%d\n",
1482 proc->pid, thread->pid, tmp->debug_id,
1483 tmp->to_proc ? tmp->to_proc->pid : 0,
1485 tmp->to_thread->pid : 0);
1486 return_error = BR_FAILED_REPLY;
1487 goto err_bad_call_stack;
1490 if (tmp->from && tmp->from->proc == target_proc)
1491 target_thread = tmp->from;
1492 tmp = tmp->from_parent;
1496 if (target_thread) {
1497 e->to_thread = target_thread->pid;
1498 target_list = &target_thread->todo;
1499 target_wait = &target_thread->wait;
1501 target_list = &target_proc->todo;
1502 target_wait = &target_proc->wait;
1504 e->to_proc = target_proc->pid;
1506 /* TODO: reuse incoming transaction for reply */
1507 t = kzalloc(sizeof(*t), GFP_KERNEL);
1509 return_error = BR_FAILED_REPLY;
1510 goto err_alloc_t_failed;
1512 binder_stats_created(BINDER_STAT_TRANSACTION);
1514 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
1515 if (tcomplete == NULL) {
1516 return_error = BR_FAILED_REPLY;
1517 goto err_alloc_tcomplete_failed;
1519 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
1521 t->debug_id = ++binder_last_id;
1522 e->debug_id = t->debug_id;
1525 binder_debug(BINDER_DEBUG_TRANSACTION,
1526 "binder: %d:%d BC_REPLY %d -> %d:%d, "
1527 "data %p-%p size %zd-%zd\n",
1528 proc->pid, thread->pid, t->debug_id,
1529 target_proc->pid, target_thread->pid,
1530 tr->data.ptr.buffer, tr->data.ptr.offsets,
1531 tr->data_size, tr->offsets_size);
1533 binder_debug(BINDER_DEBUG_TRANSACTION,
1534 "binder: %d:%d BC_TRANSACTION %d -> "
1535 "%d - node %d, data %p-%p size %zd-%zd\n",
1536 proc->pid, thread->pid, t->debug_id,
1537 target_proc->pid, target_node->debug_id,
1538 tr->data.ptr.buffer, tr->data.ptr.offsets,
1539 tr->data_size, tr->offsets_size);
1541 if (!reply && !(tr->flags & TF_ONE_WAY))
1545 t->sender_euid = proc->tsk->cred->euid;
1546 t->to_proc = target_proc;
1547 t->to_thread = target_thread;
1549 t->flags = tr->flags;
1550 t->priority = task_nice(current);
1551 t->buffer = binder_alloc_buf(target_proc, tr->data_size,
1552 tr->offsets_size, !reply && (t->flags & TF_ONE_WAY));
1553 if (t->buffer == NULL) {
1554 return_error = BR_FAILED_REPLY;
1555 goto err_binder_alloc_buf_failed;
1557 t->buffer->allow_user_free = 0;
1558 t->buffer->debug_id = t->debug_id;
1559 t->buffer->transaction = t;
1560 t->buffer->target_node = target_node;
1562 binder_inc_node(target_node, 1, 0, NULL);
1564 offp = (size_t *)(t->buffer->data + ALIGN(tr->data_size, sizeof(void *)));
1566 if (copy_from_user(t->buffer->data, tr->data.ptr.buffer, tr->data_size)) {
1567 binder_user_error("binder: %d:%d got transaction with invalid "
1568 "data ptr\n", proc->pid, thread->pid);
1569 return_error = BR_FAILED_REPLY;
1570 goto err_copy_data_failed;
1572 if (copy_from_user(offp, tr->data.ptr.offsets, tr->offsets_size)) {
1573 binder_user_error("binder: %d:%d got transaction with invalid "
1574 "offsets ptr\n", proc->pid, thread->pid);
1575 return_error = BR_FAILED_REPLY;
1576 goto err_copy_data_failed;
1578 if (!IS_ALIGNED(tr->offsets_size, sizeof(size_t))) {
1579 binder_user_error("binder: %d:%d got transaction with "
1580 "invalid offsets size, %zd\n",
1581 proc->pid, thread->pid, tr->offsets_size);
1582 return_error = BR_FAILED_REPLY;
1583 goto err_bad_offset;
1585 off_end = (void *)offp + tr->offsets_size;
1586 for (; offp < off_end; offp++) {
1587 struct flat_binder_object *fp;
1588 if (*offp > t->buffer->data_size - sizeof(*fp) ||
1589 t->buffer->data_size < sizeof(*fp) ||
1590 !IS_ALIGNED(*offp, sizeof(void *))) {
1591 binder_user_error("binder: %d:%d got transaction with "
1592 "invalid offset, %zd\n",
1593 proc->pid, thread->pid, *offp);
1594 return_error = BR_FAILED_REPLY;
1595 goto err_bad_offset;
1597 fp = (struct flat_binder_object *)(t->buffer->data + *offp);
1599 case BINDER_TYPE_BINDER:
1600 case BINDER_TYPE_WEAK_BINDER: {
1601 struct binder_ref *ref;
1602 struct binder_node *node = binder_get_node(proc, fp->binder);
1604 node = binder_new_node(proc, fp->binder, fp->cookie);
1606 return_error = BR_FAILED_REPLY;
1607 goto err_binder_new_node_failed;
1609 node->min_priority = fp->flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
1610 node->accept_fds = !!(fp->flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
1612 if (fp->cookie != node->cookie) {
1613 binder_user_error("binder: %d:%d sending u%p "
1614 "node %d, cookie mismatch %p != %p\n",
1615 proc->pid, thread->pid,
1616 fp->binder, node->debug_id,
1617 fp->cookie, node->cookie);
1618 goto err_binder_get_ref_for_node_failed;
1620 ref = binder_get_ref_for_node(target_proc, node);
1622 return_error = BR_FAILED_REPLY;
1623 goto err_binder_get_ref_for_node_failed;
1625 if (fp->type == BINDER_TYPE_BINDER)
1626 fp->type = BINDER_TYPE_HANDLE;
1628 fp->type = BINDER_TYPE_WEAK_HANDLE;
1629 fp->handle = ref->desc;
1630 binder_inc_ref(ref, fp->type == BINDER_TYPE_HANDLE,
1633 binder_debug(BINDER_DEBUG_TRANSACTION,
1634 " node %d u%p -> ref %d desc %d\n",
1635 node->debug_id, node->ptr, ref->debug_id,
1638 case BINDER_TYPE_HANDLE:
1639 case BINDER_TYPE_WEAK_HANDLE: {
1640 struct binder_ref *ref = binder_get_ref(proc, fp->handle);
1642 binder_user_error("binder: %d:%d got "
1643 "transaction with invalid "
1644 "handle, %ld\n", proc->pid,
1645 thread->pid, fp->handle);
1646 return_error = BR_FAILED_REPLY;
1647 goto err_binder_get_ref_failed;
1649 if (ref->node->proc == target_proc) {
1650 if (fp->type == BINDER_TYPE_HANDLE)
1651 fp->type = BINDER_TYPE_BINDER;
1653 fp->type = BINDER_TYPE_WEAK_BINDER;
1654 fp->binder = ref->node->ptr;
1655 fp->cookie = ref->node->cookie;
1656 binder_inc_node(ref->node, fp->type == BINDER_TYPE_BINDER, 0, NULL);
1657 binder_debug(BINDER_DEBUG_TRANSACTION,
1658 " ref %d desc %d -> node %d u%p\n",
1659 ref->debug_id, ref->desc, ref->node->debug_id,
1662 struct binder_ref *new_ref;
1663 new_ref = binder_get_ref_for_node(target_proc, ref->node);
1664 if (new_ref == NULL) {
1665 return_error = BR_FAILED_REPLY;
1666 goto err_binder_get_ref_for_node_failed;
1668 fp->handle = new_ref->desc;
1669 binder_inc_ref(new_ref, fp->type == BINDER_TYPE_HANDLE, NULL);
1670 binder_debug(BINDER_DEBUG_TRANSACTION,
1671 " ref %d desc %d -> ref %d desc %d (node %d)\n",
1672 ref->debug_id, ref->desc, new_ref->debug_id,
1673 new_ref->desc, ref->node->debug_id);
1677 case BINDER_TYPE_FD: {
1682 if (!(in_reply_to->flags & TF_ACCEPT_FDS)) {
1683 binder_user_error("binder: %d:%d got reply with fd, %ld, but target does not allow fds\n",
1684 proc->pid, thread->pid, fp->handle);
1685 return_error = BR_FAILED_REPLY;
1686 goto err_fd_not_allowed;
1688 } else if (!target_node->accept_fds) {
1689 binder_user_error("binder: %d:%d got transaction with fd, %ld, but target does not allow fds\n",
1690 proc->pid, thread->pid, fp->handle);
1691 return_error = BR_FAILED_REPLY;
1692 goto err_fd_not_allowed;
1695 file = fget(fp->handle);
1697 binder_user_error("binder: %d:%d got transaction with invalid fd, %ld\n",
1698 proc->pid, thread->pid, fp->handle);
1699 return_error = BR_FAILED_REPLY;
1700 goto err_fget_failed;
1702 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
1703 if (target_fd < 0) {
1705 return_error = BR_FAILED_REPLY;
1706 goto err_get_unused_fd_failed;
1708 task_fd_install(target_proc, target_fd, file);
1709 binder_debug(BINDER_DEBUG_TRANSACTION,
1710 " fd %ld -> %d\n", fp->handle, target_fd);
1712 fp->handle = target_fd;
1716 binder_user_error("binder: %d:%d got transactio"
1717 "n with invalid object type, %lx\n",
1718 proc->pid, thread->pid, fp->type);
1719 return_error = BR_FAILED_REPLY;
1720 goto err_bad_object_type;
1724 BUG_ON(t->buffer->async_transaction != 0);
1725 binder_pop_transaction(target_thread, in_reply_to);
1726 } else if (!(t->flags & TF_ONE_WAY)) {
1727 BUG_ON(t->buffer->async_transaction != 0);
1729 t->from_parent = thread->transaction_stack;
1730 thread->transaction_stack = t;
1732 BUG_ON(target_node == NULL);
1733 BUG_ON(t->buffer->async_transaction != 1);
1734 if (target_node->has_async_transaction) {
1735 target_list = &target_node->async_todo;
1738 target_node->has_async_transaction = 1;
1740 t->work.type = BINDER_WORK_TRANSACTION;
1741 list_add_tail(&t->work.entry, target_list);
1742 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
1743 list_add_tail(&tcomplete->entry, &thread->todo);
1745 wake_up_interruptible(target_wait);
1748 err_get_unused_fd_failed:
1751 err_binder_get_ref_for_node_failed:
1752 err_binder_get_ref_failed:
1753 err_binder_new_node_failed:
1754 err_bad_object_type:
1756 err_copy_data_failed:
1757 binder_transaction_buffer_release(target_proc, t->buffer, offp);
1758 t->buffer->transaction = NULL;
1759 binder_free_buf(target_proc, t->buffer);
1760 err_binder_alloc_buf_failed:
1762 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
1763 err_alloc_tcomplete_failed:
1765 binder_stats_deleted(BINDER_STAT_TRANSACTION);
1768 err_empty_call_stack:
1770 err_invalid_target_handle:
1771 err_no_context_mgr_node:
1772 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1773 "binder: %d:%d transaction failed %d, size %zd-%zd\n",
1774 proc->pid, thread->pid, return_error,
1775 tr->data_size, tr->offsets_size);
1778 struct binder_transaction_log_entry *fe;
1779 fe = binder_transaction_log_add(&binder_transaction_log_failed);
1783 BUG_ON(thread->return_error != BR_OK);
1785 thread->return_error = BR_TRANSACTION_COMPLETE;
1786 binder_send_failed_reply(in_reply_to, return_error);
1788 thread->return_error = return_error;
1791 int binder_thread_write(struct binder_proc *proc, struct binder_thread *thread,
1792 void __user *buffer, int size, signed long *consumed)
1795 void __user *ptr = buffer + *consumed;
1796 void __user *end = buffer + size;
1798 while (ptr < end && thread->return_error == BR_OK) {
1799 if (get_user(cmd, (uint32_t __user *)ptr))
1801 ptr += sizeof(uint32_t);
1802 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
1803 binder_stats.bc[_IOC_NR(cmd)]++;
1804 proc->stats.bc[_IOC_NR(cmd)]++;
1805 thread->stats.bc[_IOC_NR(cmd)]++;
1813 struct binder_ref *ref;
1814 const char *debug_string;
1816 if (get_user(target, (uint32_t __user *)ptr))
1818 ptr += sizeof(uint32_t);
1819 if (target == 0 && binder_context_mgr_node &&
1820 (cmd == BC_INCREFS || cmd == BC_ACQUIRE)) {
1821 ref = binder_get_ref_for_node(proc,
1822 binder_context_mgr_node);
1823 if (ref->desc != target) {
1824 binder_user_error("binder: %d:"
1825 "%d tried to acquire "
1826 "reference to desc 0, "
1828 proc->pid, thread->pid,
1832 ref = binder_get_ref(proc, target);
1834 binder_user_error("binder: %d:%d refcou"
1835 "nt change on invalid ref %d\n",
1836 proc->pid, thread->pid, target);
1841 debug_string = "IncRefs";
1842 binder_inc_ref(ref, 0, NULL);
1845 debug_string = "Acquire";
1846 binder_inc_ref(ref, 1, NULL);
1849 debug_string = "Release";
1850 binder_dec_ref(ref, 1);
1854 debug_string = "DecRefs";
1855 binder_dec_ref(ref, 0);
1858 binder_debug(BINDER_DEBUG_USER_REFS,
1859 "binder: %d:%d %s ref %d desc %d s %d w %d for node %d\n",
1860 proc->pid, thread->pid, debug_string, ref->debug_id,
1861 ref->desc, ref->strong, ref->weak, ref->node->debug_id);
1864 case BC_INCREFS_DONE:
1865 case BC_ACQUIRE_DONE: {
1866 void __user *node_ptr;
1868 struct binder_node *node;
1870 if (get_user(node_ptr, (void * __user *)ptr))
1872 ptr += sizeof(void *);
1873 if (get_user(cookie, (void * __user *)ptr))
1875 ptr += sizeof(void *);
1876 node = binder_get_node(proc, node_ptr);
1878 binder_user_error("binder: %d:%d "
1879 "%s u%p no match\n",
1880 proc->pid, thread->pid,
1881 cmd == BC_INCREFS_DONE ?
1887 if (cookie != node->cookie) {
1888 binder_user_error("binder: %d:%d %s u%p node %d"
1889 " cookie mismatch %p != %p\n",
1890 proc->pid, thread->pid,
1891 cmd == BC_INCREFS_DONE ?
1892 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
1893 node_ptr, node->debug_id,
1894 cookie, node->cookie);
1897 if (cmd == BC_ACQUIRE_DONE) {
1898 if (node->pending_strong_ref == 0) {
1899 binder_user_error("binder: %d:%d "
1900 "BC_ACQUIRE_DONE node %d has "
1901 "no pending acquire request\n",
1902 proc->pid, thread->pid,
1906 node->pending_strong_ref = 0;
1908 if (node->pending_weak_ref == 0) {
1909 binder_user_error("binder: %d:%d "
1910 "BC_INCREFS_DONE node %d has "
1911 "no pending increfs request\n",
1912 proc->pid, thread->pid,
1916 node->pending_weak_ref = 0;
1918 binder_dec_node(node, cmd == BC_ACQUIRE_DONE, 0);
1919 binder_debug(BINDER_DEBUG_USER_REFS,
1920 "binder: %d:%d %s node %d ls %d lw %d\n",
1921 proc->pid, thread->pid,
1922 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
1923 node->debug_id, node->local_strong_refs, node->local_weak_refs);
1926 case BC_ATTEMPT_ACQUIRE:
1927 printk(KERN_ERR "binder: BC_ATTEMPT_ACQUIRE not supported\n");
1929 case BC_ACQUIRE_RESULT:
1930 printk(KERN_ERR "binder: BC_ACQUIRE_RESULT not supported\n");
1933 case BC_FREE_BUFFER: {
1934 void __user *data_ptr;
1935 struct binder_buffer *buffer;
1937 if (get_user(data_ptr, (void * __user *)ptr))
1939 ptr += sizeof(void *);
1941 buffer = binder_buffer_lookup(proc, data_ptr);
1942 if (buffer == NULL) {
1943 binder_user_error("binder: %d:%d "
1944 "BC_FREE_BUFFER u%p no match\n",
1945 proc->pid, thread->pid, data_ptr);
1948 if (!buffer->allow_user_free) {
1949 binder_user_error("binder: %d:%d "
1950 "BC_FREE_BUFFER u%p matched "
1951 "unreturned buffer\n",
1952 proc->pid, thread->pid, data_ptr);
1955 binder_debug(BINDER_DEBUG_FREE_BUFFER,
1956 "binder: %d:%d BC_FREE_BUFFER u%p found buffer %d for %s transaction\n",
1957 proc->pid, thread->pid, data_ptr, buffer->debug_id,
1958 buffer->transaction ? "active" : "finished");
1960 if (buffer->transaction) {
1961 buffer->transaction->buffer = NULL;
1962 buffer->transaction = NULL;
1964 if (buffer->async_transaction && buffer->target_node) {
1965 BUG_ON(!buffer->target_node->has_async_transaction);
1966 if (list_empty(&buffer->target_node->async_todo))
1967 buffer->target_node->has_async_transaction = 0;
1969 list_move_tail(buffer->target_node->async_todo.next, &thread->todo);
1971 binder_transaction_buffer_release(proc, buffer, NULL);
1972 binder_free_buf(proc, buffer);
1976 case BC_TRANSACTION:
1978 struct binder_transaction_data tr;
1980 if (copy_from_user(&tr, ptr, sizeof(tr)))
1983 binder_transaction(proc, thread, &tr, cmd == BC_REPLY);
1987 case BC_REGISTER_LOOPER:
1988 binder_debug(BINDER_DEBUG_THREADS,
1989 "binder: %d:%d BC_REGISTER_LOOPER\n",
1990 proc->pid, thread->pid);
1991 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
1992 thread->looper |= BINDER_LOOPER_STATE_INVALID;
1993 binder_user_error("binder: %d:%d ERROR:"
1994 " BC_REGISTER_LOOPER called "
1995 "after BC_ENTER_LOOPER\n",
1996 proc->pid, thread->pid);
1997 } else if (proc->requested_threads == 0) {
1998 thread->looper |= BINDER_LOOPER_STATE_INVALID;
1999 binder_user_error("binder: %d:%d ERROR:"
2000 " BC_REGISTER_LOOPER called "
2001 "without request\n",
2002 proc->pid, thread->pid);
2004 proc->requested_threads--;
2005 proc->requested_threads_started++;
2007 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
2009 case BC_ENTER_LOOPER:
2010 binder_debug(BINDER_DEBUG_THREADS,
2011 "binder: %d:%d BC_ENTER_LOOPER\n",
2012 proc->pid, thread->pid);
2013 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
2014 thread->looper |= BINDER_LOOPER_STATE_INVALID;
2015 binder_user_error("binder: %d:%d ERROR:"
2016 " BC_ENTER_LOOPER called after "
2017 "BC_REGISTER_LOOPER\n",
2018 proc->pid, thread->pid);
2020 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
2022 case BC_EXIT_LOOPER:
2023 binder_debug(BINDER_DEBUG_THREADS,
2024 "binder: %d:%d BC_EXIT_LOOPER\n",
2025 proc->pid, thread->pid);
2026 thread->looper |= BINDER_LOOPER_STATE_EXITED;
2029 case BC_REQUEST_DEATH_NOTIFICATION:
2030 case BC_CLEAR_DEATH_NOTIFICATION: {
2032 void __user *cookie;
2033 struct binder_ref *ref;
2034 struct binder_ref_death *death;
2036 if (get_user(target, (uint32_t __user *)ptr))
2038 ptr += sizeof(uint32_t);
2039 if (get_user(cookie, (void __user * __user *)ptr))
2041 ptr += sizeof(void *);
2042 ref = binder_get_ref(proc, target);
2044 binder_user_error("binder: %d:%d %s "
2046 proc->pid, thread->pid,
2047 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
2048 "BC_REQUEST_DEATH_NOTIFICATION" :
2049 "BC_CLEAR_DEATH_NOTIFICATION",
2054 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
2055 "binder: %d:%d %s %p ref %d desc %d s %d w %d for node %d\n",
2056 proc->pid, thread->pid,
2057 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
2058 "BC_REQUEST_DEATH_NOTIFICATION" :
2059 "BC_CLEAR_DEATH_NOTIFICATION",
2060 cookie, ref->debug_id, ref->desc,
2061 ref->strong, ref->weak, ref->node->debug_id);
2063 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
2065 binder_user_error("binder: %d:%"
2066 "d BC_REQUEST_DEATH_NOTI"
2067 "FICATION death notific"
2068 "ation already set\n",
2069 proc->pid, thread->pid);
2072 death = kzalloc(sizeof(*death), GFP_KERNEL);
2073 if (death == NULL) {
2074 thread->return_error = BR_ERROR;
2075 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2077 "BC_REQUEST_DEATH_NOTIFICATION failed\n",
2078 proc->pid, thread->pid);
2081 binder_stats_created(BINDER_STAT_DEATH);
2082 INIT_LIST_HEAD(&death->work.entry);
2083 death->cookie = cookie;
2085 if (ref->node->proc == NULL) {
2086 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
2087 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2088 list_add_tail(&ref->death->work.entry, &thread->todo);
2090 list_add_tail(&ref->death->work.entry, &proc->todo);
2091 wake_up_interruptible(&proc->wait);
2095 if (ref->death == NULL) {
2096 binder_user_error("binder: %d:%"
2097 "d BC_CLEAR_DEATH_NOTIFI"
2098 "CATION death notificat"
2100 proc->pid, thread->pid);
2104 if (death->cookie != cookie) {
2105 binder_user_error("binder: %d:%"
2106 "d BC_CLEAR_DEATH_NOTIFI"
2107 "CATION death notificat"
2108 "ion cookie mismatch "
2110 proc->pid, thread->pid,
2111 death->cookie, cookie);
2115 if (list_empty(&death->work.entry)) {
2116 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2117 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2118 list_add_tail(&death->work.entry, &thread->todo);
2120 list_add_tail(&death->work.entry, &proc->todo);
2121 wake_up_interruptible(&proc->wait);
2124 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
2125 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
2129 case BC_DEAD_BINDER_DONE: {
2130 struct binder_work *w;
2131 void __user *cookie;
2132 struct binder_ref_death *death = NULL;
2133 if (get_user(cookie, (void __user * __user *)ptr))
2136 ptr += sizeof(void *);
2137 list_for_each_entry(w, &proc->delivered_death, entry) {
2138 struct binder_ref_death *tmp_death = container_of(w, struct binder_ref_death, work);
2139 if (tmp_death->cookie == cookie) {
2144 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2145 "binder: %d:%d BC_DEAD_BINDER_DONE %p found %p\n",
2146 proc->pid, thread->pid, cookie, death);
2147 if (death == NULL) {
2148 binder_user_error("binder: %d:%d BC_DEAD"
2149 "_BINDER_DONE %p not found\n",
2150 proc->pid, thread->pid, cookie);
2154 list_del_init(&death->work.entry);
2155 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
2156 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2157 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2158 list_add_tail(&death->work.entry, &thread->todo);
2160 list_add_tail(&death->work.entry, &proc->todo);
2161 wake_up_interruptible(&proc->wait);
2167 printk(KERN_ERR "binder: %d:%d unknown command %d\n",
2168 proc->pid, thread->pid, cmd);
2171 *consumed = ptr - buffer;
2176 void binder_stat_br(struct binder_proc *proc, struct binder_thread *thread,
2179 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
2180 binder_stats.br[_IOC_NR(cmd)]++;
2181 proc->stats.br[_IOC_NR(cmd)]++;
2182 thread->stats.br[_IOC_NR(cmd)]++;
2186 static int binder_has_proc_work(struct binder_proc *proc,
2187 struct binder_thread *thread)
2189 return !list_empty(&proc->todo) ||
2190 (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2193 static int binder_has_thread_work(struct binder_thread *thread)
2195 return !list_empty(&thread->todo) || thread->return_error != BR_OK ||
2196 (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2199 static int binder_thread_read(struct binder_proc *proc,
2200 struct binder_thread *thread,
2201 void __user *buffer, int size,
2202 signed long *consumed, int non_block)
2204 void __user *ptr = buffer + *consumed;
2205 void __user *end = buffer + size;
2208 int wait_for_proc_work;
2210 if (*consumed == 0) {
2211 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
2213 ptr += sizeof(uint32_t);
2217 wait_for_proc_work = thread->transaction_stack == NULL &&
2218 list_empty(&thread->todo);
2220 if (thread->return_error != BR_OK && ptr < end) {
2221 if (thread->return_error2 != BR_OK) {
2222 if (put_user(thread->return_error2, (uint32_t __user *)ptr))
2224 ptr += sizeof(uint32_t);
2227 thread->return_error2 = BR_OK;
2229 if (put_user(thread->return_error, (uint32_t __user *)ptr))
2231 ptr += sizeof(uint32_t);
2232 thread->return_error = BR_OK;
2237 thread->looper |= BINDER_LOOPER_STATE_WAITING;
2238 if (wait_for_proc_work)
2239 proc->ready_threads++;
2240 mutex_unlock(&binder_lock);
2241 if (wait_for_proc_work) {
2242 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2243 BINDER_LOOPER_STATE_ENTERED))) {
2244 binder_user_error("binder: %d:%d ERROR: Thread waiting "
2245 "for process work before calling BC_REGISTER_"
2246 "LOOPER or BC_ENTER_LOOPER (state %x)\n",
2247 proc->pid, thread->pid, thread->looper);
2248 wait_event_interruptible(binder_user_error_wait,
2249 binder_stop_on_user_error < 2);
2251 binder_set_nice(proc->default_priority);
2253 if (!binder_has_proc_work(proc, thread))
2256 ret = wait_event_interruptible_exclusive(proc->wait, binder_has_proc_work(proc, thread));
2259 if (!binder_has_thread_work(thread))
2262 ret = wait_event_interruptible(thread->wait, binder_has_thread_work(thread));
2264 mutex_lock(&binder_lock);
2265 if (wait_for_proc_work)
2266 proc->ready_threads--;
2267 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
2274 struct binder_transaction_data tr;
2275 struct binder_work *w;
2276 struct binder_transaction *t = NULL;
2278 if (!list_empty(&thread->todo))
2279 w = list_first_entry(&thread->todo, struct binder_work, entry);
2280 else if (!list_empty(&proc->todo) && wait_for_proc_work)
2281 w = list_first_entry(&proc->todo, struct binder_work, entry);
2283 if (ptr - buffer == 4 && !(thread->looper & BINDER_LOOPER_STATE_NEED_RETURN)) /* no data added */
2288 if (end - ptr < sizeof(tr) + 4)
2292 case BINDER_WORK_TRANSACTION: {
2293 t = container_of(w, struct binder_transaction, work);
2295 case BINDER_WORK_TRANSACTION_COMPLETE: {
2296 cmd = BR_TRANSACTION_COMPLETE;
2297 if (put_user(cmd, (uint32_t __user *)ptr))
2299 ptr += sizeof(uint32_t);
2301 binder_stat_br(proc, thread, cmd);
2302 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
2303 "binder: %d:%d BR_TRANSACTION_COMPLETE\n",
2304 proc->pid, thread->pid);
2306 list_del(&w->entry);
2308 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2310 case BINDER_WORK_NODE: {
2311 struct binder_node *node = container_of(w, struct binder_node, work);
2312 uint32_t cmd = BR_NOOP;
2313 const char *cmd_name;
2314 int strong = node->internal_strong_refs || node->local_strong_refs;
2315 int weak = !hlist_empty(&node->refs) || node->local_weak_refs || strong;
2316 if (weak && !node->has_weak_ref) {
2318 cmd_name = "BR_INCREFS";
2319 node->has_weak_ref = 1;
2320 node->pending_weak_ref = 1;
2321 node->local_weak_refs++;
2322 } else if (strong && !node->has_strong_ref) {
2324 cmd_name = "BR_ACQUIRE";
2325 node->has_strong_ref = 1;
2326 node->pending_strong_ref = 1;
2327 node->local_strong_refs++;
2328 } else if (!strong && node->has_strong_ref) {
2330 cmd_name = "BR_RELEASE";
2331 node->has_strong_ref = 0;
2332 } else if (!weak && node->has_weak_ref) {
2334 cmd_name = "BR_DECREFS";
2335 node->has_weak_ref = 0;
2337 if (cmd != BR_NOOP) {
2338 if (put_user(cmd, (uint32_t __user *)ptr))
2340 ptr += sizeof(uint32_t);
2341 if (put_user(node->ptr, (void * __user *)ptr))
2343 ptr += sizeof(void *);
2344 if (put_user(node->cookie, (void * __user *)ptr))
2346 ptr += sizeof(void *);
2348 binder_stat_br(proc, thread, cmd);
2349 binder_debug(BINDER_DEBUG_USER_REFS,
2350 "binder: %d:%d %s %d u%p c%p\n",
2351 proc->pid, thread->pid, cmd_name, node->debug_id, node->ptr, node->cookie);
2353 list_del_init(&w->entry);
2354 if (!weak && !strong) {
2355 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
2356 "binder: %d:%d node %d u%p c%p deleted\n",
2357 proc->pid, thread->pid, node->debug_id,
2358 node->ptr, node->cookie);
2359 rb_erase(&node->rb_node, &proc->nodes);
2361 binder_stats_deleted(BINDER_STAT_NODE);
2363 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
2364 "binder: %d:%d node %d u%p c%p state unchanged\n",
2365 proc->pid, thread->pid, node->debug_id, node->ptr,
2370 case BINDER_WORK_DEAD_BINDER:
2371 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2372 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2373 struct binder_ref_death *death;
2376 death = container_of(w, struct binder_ref_death, work);
2377 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
2378 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
2380 cmd = BR_DEAD_BINDER;
2381 if (put_user(cmd, (uint32_t __user *)ptr))
2383 ptr += sizeof(uint32_t);
2384 if (put_user(death->cookie, (void * __user *)ptr))
2386 ptr += sizeof(void *);
2387 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
2388 "binder: %d:%d %s %p\n",
2389 proc->pid, thread->pid,
2390 cmd == BR_DEAD_BINDER ?
2392 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
2395 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
2396 list_del(&w->entry);
2398 binder_stats_deleted(BINDER_STAT_DEATH);
2400 list_move(&w->entry, &proc->delivered_death);
2401 if (cmd == BR_DEAD_BINDER)
2402 goto done; /* DEAD_BINDER notifications can cause transactions */
2409 BUG_ON(t->buffer == NULL);
2410 if (t->buffer->target_node) {
2411 struct binder_node *target_node = t->buffer->target_node;
2412 tr.target.ptr = target_node->ptr;
2413 tr.cookie = target_node->cookie;
2414 t->saved_priority = task_nice(current);
2415 if (t->priority < target_node->min_priority &&
2416 !(t->flags & TF_ONE_WAY))
2417 binder_set_nice(t->priority);
2418 else if (!(t->flags & TF_ONE_WAY) ||
2419 t->saved_priority > target_node->min_priority)
2420 binder_set_nice(target_node->min_priority);
2421 cmd = BR_TRANSACTION;
2423 tr.target.ptr = NULL;
2428 tr.flags = t->flags;
2429 tr.sender_euid = t->sender_euid;
2432 struct task_struct *sender = t->from->proc->tsk;
2433 tr.sender_pid = task_tgid_nr_ns(sender,
2434 current->nsproxy->pid_ns);
2439 tr.data_size = t->buffer->data_size;
2440 tr.offsets_size = t->buffer->offsets_size;
2441 tr.data.ptr.buffer = (void *)t->buffer->data +
2442 proc->user_buffer_offset;
2443 tr.data.ptr.offsets = tr.data.ptr.buffer +
2444 ALIGN(t->buffer->data_size,
2447 if (put_user(cmd, (uint32_t __user *)ptr))
2449 ptr += sizeof(uint32_t);
2450 if (copy_to_user(ptr, &tr, sizeof(tr)))
2454 binder_stat_br(proc, thread, cmd);
2455 binder_debug(BINDER_DEBUG_TRANSACTION,
2456 "binder: %d:%d %s %d %d:%d, cmd %d"
2457 "size %zd-%zd ptr %p-%p\n",
2458 proc->pid, thread->pid,
2459 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
2461 t->debug_id, t->from ? t->from->proc->pid : 0,
2462 t->from ? t->from->pid : 0, cmd,
2463 t->buffer->data_size, t->buffer->offsets_size,
2464 tr.data.ptr.buffer, tr.data.ptr.offsets);
2466 list_del(&t->work.entry);
2467 t->buffer->allow_user_free = 1;
2468 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
2469 t->to_parent = thread->transaction_stack;
2470 t->to_thread = thread;
2471 thread->transaction_stack = t;
2473 t->buffer->transaction = NULL;
2475 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2482 *consumed = ptr - buffer;
2483 if (proc->requested_threads + proc->ready_threads == 0 &&
2484 proc->requested_threads_started < proc->max_threads &&
2485 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2486 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
2487 /*spawn a new thread if we leave this out */) {
2488 proc->requested_threads++;
2489 binder_debug(BINDER_DEBUG_THREADS,
2490 "binder: %d:%d BR_SPAWN_LOOPER\n",
2491 proc->pid, thread->pid);
2492 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
2498 static void binder_release_work(struct list_head *list)
2500 struct binder_work *w;
2501 while (!list_empty(list)) {
2502 w = list_first_entry(list, struct binder_work, entry);
2503 list_del_init(&w->entry);
2505 case BINDER_WORK_TRANSACTION: {
2506 struct binder_transaction *t;
2508 t = container_of(w, struct binder_transaction, work);
2509 if (t->buffer->target_node && !(t->flags & TF_ONE_WAY))
2510 binder_send_failed_reply(t, BR_DEAD_REPLY);
2512 case BINDER_WORK_TRANSACTION_COMPLETE: {
2514 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2523 static struct binder_thread *binder_get_thread(struct binder_proc *proc)
2525 struct binder_thread *thread = NULL;
2526 struct rb_node *parent = NULL;
2527 struct rb_node **p = &proc->threads.rb_node;
2531 thread = rb_entry(parent, struct binder_thread, rb_node);
2533 if (current->pid < thread->pid)
2535 else if (current->pid > thread->pid)
2536 p = &(*p)->rb_right;
2541 thread = kzalloc(sizeof(*thread), GFP_KERNEL);
2544 binder_stats_created(BINDER_STAT_THREAD);
2545 thread->proc = proc;
2546 thread->pid = current->pid;
2547 init_waitqueue_head(&thread->wait);
2548 INIT_LIST_HEAD(&thread->todo);
2549 rb_link_node(&thread->rb_node, parent, p);
2550 rb_insert_color(&thread->rb_node, &proc->threads);
2551 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
2552 thread->return_error = BR_OK;
2553 thread->return_error2 = BR_OK;
2558 static int binder_free_thread(struct binder_proc *proc,
2559 struct binder_thread *thread)
2561 struct binder_transaction *t;
2562 struct binder_transaction *send_reply = NULL;
2563 int active_transactions = 0;
2565 rb_erase(&thread->rb_node, &proc->threads);
2566 t = thread->transaction_stack;
2567 if (t && t->to_thread == thread)
2570 active_transactions++;
2571 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2572 "binder: release %d:%d transaction %d "
2573 "%s, still active\n", proc->pid, thread->pid,
2575 (t->to_thread == thread) ? "in" : "out");
2577 if (t->to_thread == thread) {
2579 t->to_thread = NULL;
2581 t->buffer->transaction = NULL;
2585 } else if (t->from == thread) {
2592 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
2593 binder_release_work(&thread->todo);
2595 binder_stats_deleted(BINDER_STAT_THREAD);
2596 return active_transactions;
2599 static unsigned int binder_poll(struct file *filp,
2600 struct poll_table_struct *wait)
2602 struct binder_proc *proc = filp->private_data;
2603 struct binder_thread *thread = NULL;
2604 int wait_for_proc_work;
2606 mutex_lock(&binder_lock);
2607 thread = binder_get_thread(proc);
2609 wait_for_proc_work = thread->transaction_stack == NULL &&
2610 list_empty(&thread->todo) && thread->return_error == BR_OK;
2611 mutex_unlock(&binder_lock);
2613 if (wait_for_proc_work) {
2614 if (binder_has_proc_work(proc, thread))
2616 poll_wait(filp, &proc->wait, wait);
2617 if (binder_has_proc_work(proc, thread))
2620 if (binder_has_thread_work(thread))
2622 poll_wait(filp, &thread->wait, wait);
2623 if (binder_has_thread_work(thread))
2629 static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2632 struct binder_proc *proc = filp->private_data;
2633 struct binder_thread *thread;
2634 unsigned int size = _IOC_SIZE(cmd);
2635 void __user *ubuf = (void __user *)arg;
2637 /*printk(KERN_INFO "binder_ioctl: %d:%d %x %lx\n", proc->pid, current->pid, cmd, arg);*/
2639 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2643 mutex_lock(&binder_lock);
2644 thread = binder_get_thread(proc);
2645 if (thread == NULL) {
2651 case BINDER_WRITE_READ: {
2652 struct binder_write_read bwr;
2653 if (size != sizeof(struct binder_write_read)) {
2657 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
2661 binder_debug(BINDER_DEBUG_READ_WRITE,
2662 "binder: %d:%d write %ld at %08lx, read %ld at %08lx\n",
2663 proc->pid, thread->pid, bwr.write_size, bwr.write_buffer,
2664 bwr.read_size, bwr.read_buffer);
2666 if (bwr.write_size > 0) {
2667 ret = binder_thread_write(proc, thread, (void __user *)bwr.write_buffer, bwr.write_size, &bwr.write_consumed);
2669 bwr.read_consumed = 0;
2670 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2675 if (bwr.read_size > 0) {
2676 ret = binder_thread_read(proc, thread, (void __user *)bwr.read_buffer, bwr.read_size, &bwr.read_consumed, filp->f_flags & O_NONBLOCK);
2677 if (!list_empty(&proc->todo))
2678 wake_up_interruptible(&proc->wait);
2680 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2685 binder_debug(BINDER_DEBUG_READ_WRITE,
2686 "binder: %d:%d wrote %ld of %ld, read return %ld of %ld\n",
2687 proc->pid, thread->pid, bwr.write_consumed, bwr.write_size,
2688 bwr.read_consumed, bwr.read_size);
2689 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
2695 case BINDER_SET_MAX_THREADS:
2696 if (copy_from_user(&proc->max_threads, ubuf, sizeof(proc->max_threads))) {
2701 case BINDER_SET_CONTEXT_MGR:
2702 if (binder_context_mgr_node != NULL) {
2703 printk(KERN_ERR "binder: BINDER_SET_CONTEXT_MGR already set\n");
2707 if (binder_context_mgr_uid != -1) {
2708 if (binder_context_mgr_uid != current->cred->euid) {
2709 printk(KERN_ERR "binder: BINDER_SET_"
2710 "CONTEXT_MGR bad uid %d != %d\n",
2711 current->cred->euid,
2712 binder_context_mgr_uid);
2717 binder_context_mgr_uid = current->cred->euid;
2718 binder_context_mgr_node = binder_new_node(proc, NULL, NULL);
2719 if (binder_context_mgr_node == NULL) {
2723 binder_context_mgr_node->local_weak_refs++;
2724 binder_context_mgr_node->local_strong_refs++;
2725 binder_context_mgr_node->has_strong_ref = 1;
2726 binder_context_mgr_node->has_weak_ref = 1;
2728 case BINDER_THREAD_EXIT:
2729 binder_debug(BINDER_DEBUG_THREADS, "binder: %d:%d exit\n",
2730 proc->pid, thread->pid);
2731 binder_free_thread(proc, thread);
2734 case BINDER_VERSION:
2735 if (size != sizeof(struct binder_version)) {
2739 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION, &((struct binder_version *)ubuf)->protocol_version)) {
2751 thread->looper &= ~BINDER_LOOPER_STATE_NEED_RETURN;
2752 mutex_unlock(&binder_lock);
2753 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2754 if (ret && ret != -ERESTARTSYS)
2755 printk(KERN_INFO "binder: %d:%d ioctl %x %lx returned %d\n", proc->pid, current->pid, cmd, arg, ret);
2759 static void binder_vma_open(struct vm_area_struct *vma)
2761 struct binder_proc *proc = vma->vm_private_data;
2762 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
2763 "binder: %d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
2764 proc->pid, vma->vm_start, vma->vm_end,
2765 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2766 (unsigned long)pgprot_val(vma->vm_page_prot));
2769 static void binder_vma_close(struct vm_area_struct *vma)
2771 struct binder_proc *proc = vma->vm_private_data;
2772 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
2773 "binder: %d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
2774 proc->pid, vma->vm_start, vma->vm_end,
2775 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2776 (unsigned long)pgprot_val(vma->vm_page_prot));
2778 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
2781 static struct vm_operations_struct binder_vm_ops = {
2782 .open = binder_vma_open,
2783 .close = binder_vma_close,
2786 static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
2789 struct vm_struct *area;
2790 struct binder_proc *proc = filp->private_data;
2791 const char *failure_string;
2792 struct binder_buffer *buffer;
2794 if ((vma->vm_end - vma->vm_start) > SZ_4M)
2795 vma->vm_end = vma->vm_start + SZ_4M;
2797 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
2798 "binder_mmap: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
2799 proc->pid, vma->vm_start, vma->vm_end,
2800 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2801 (unsigned long)pgprot_val(vma->vm_page_prot));
2803 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
2805 failure_string = "bad vm_flags";
2808 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
2810 mutex_lock(&binder_mmap_lock);
2813 failure_string = "already mapped";
2814 goto err_already_mapped;
2817 area = get_vm_area(vma->vm_end - vma->vm_start, VM_IOREMAP);
2820 failure_string = "get_vm_area";
2821 goto err_get_vm_area_failed;
2823 proc->buffer = area->addr;
2824 proc->user_buffer_offset = vma->vm_start - (uintptr_t)proc->buffer;
2825 mutex_unlock(&binder_mmap_lock);
2827 #ifdef CONFIG_CPU_CACHE_VIPT
2828 if (cache_is_vipt_aliasing()) {
2829 while (CACHE_COLOUR((vma->vm_start ^ (uint32_t)proc->buffer))) {
2830 printk(KERN_INFO "binder_mmap: %d %lx-%lx maps %p bad alignment\n", proc->pid, vma->vm_start, vma->vm_end, proc->buffer);
2831 vma->vm_start += PAGE_SIZE;
2835 proc->pages = kzalloc(sizeof(proc->pages[0]) * ((vma->vm_end - vma->vm_start) / PAGE_SIZE), GFP_KERNEL);
2836 if (proc->pages == NULL) {
2838 failure_string = "alloc page array";
2839 goto err_alloc_pages_failed;
2841 proc->buffer_size = vma->vm_end - vma->vm_start;
2843 vma->vm_ops = &binder_vm_ops;
2844 vma->vm_private_data = proc;
2846 if (binder_update_page_range(proc, 1, proc->buffer, proc->buffer + PAGE_SIZE, vma)) {
2848 failure_string = "alloc small buf";
2849 goto err_alloc_small_buf_failed;
2851 buffer = proc->buffer;
2852 INIT_LIST_HEAD(&proc->buffers);
2853 list_add(&buffer->entry, &proc->buffers);
2855 binder_insert_free_buffer(proc, buffer);
2856 proc->free_async_space = proc->buffer_size / 2;
2858 proc->files = get_files_struct(proc->tsk);
2861 /*printk(KERN_INFO "binder_mmap: %d %lx-%lx maps %p\n",
2862 proc->pid, vma->vm_start, vma->vm_end, proc->buffer);*/
2865 err_alloc_small_buf_failed:
2868 err_alloc_pages_failed:
2869 mutex_lock(&binder_mmap_lock);
2870 vfree(proc->buffer);
2871 proc->buffer = NULL;
2872 err_get_vm_area_failed:
2874 mutex_unlock(&binder_mmap_lock);
2876 printk(KERN_ERR "binder_mmap: %d %lx-%lx %s failed %d\n",
2877 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
2881 static int binder_open(struct inode *nodp, struct file *filp)
2883 struct binder_proc *proc;
2885 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
2886 current->group_leader->pid, current->pid);
2888 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
2891 get_task_struct(current);
2892 proc->tsk = current;
2893 INIT_LIST_HEAD(&proc->todo);
2894 init_waitqueue_head(&proc->wait);
2895 proc->default_priority = task_nice(current);
2896 mutex_lock(&binder_lock);
2897 binder_stats_created(BINDER_STAT_PROC);
2898 hlist_add_head(&proc->proc_node, &binder_procs);
2899 proc->pid = current->group_leader->pid;
2900 INIT_LIST_HEAD(&proc->delivered_death);
2901 filp->private_data = proc;
2902 mutex_unlock(&binder_lock);
2904 if (binder_debugfs_dir_entry_proc) {
2906 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
2907 proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
2908 binder_debugfs_dir_entry_proc, proc, &binder_proc_fops);
2914 static int binder_flush(struct file *filp, fl_owner_t id)
2916 struct binder_proc *proc = filp->private_data;
2918 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
2923 static void binder_deferred_flush(struct binder_proc *proc)
2927 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
2928 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
2929 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
2930 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
2931 wake_up_interruptible(&thread->wait);
2935 wake_up_interruptible_all(&proc->wait);
2937 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
2938 "binder_flush: %d woke %d threads\n", proc->pid,
2942 static int binder_release(struct inode *nodp, struct file *filp)
2944 struct binder_proc *proc = filp->private_data;
2945 debugfs_remove(proc->debugfs_entry);
2946 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
2951 static void binder_deferred_release(struct binder_proc *proc)
2953 struct hlist_node *pos;
2954 struct binder_transaction *t;
2956 int threads, nodes, incoming_refs, outgoing_refs, buffers, active_transactions, page_count;
2959 BUG_ON(proc->files);
2961 hlist_del(&proc->proc_node);
2962 if (binder_context_mgr_node && binder_context_mgr_node->proc == proc) {
2963 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2964 "binder_release: %d context_mgr_node gone\n",
2966 binder_context_mgr_node = NULL;
2970 active_transactions = 0;
2971 while ((n = rb_first(&proc->threads))) {
2972 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
2974 active_transactions += binder_free_thread(proc, thread);
2978 while ((n = rb_first(&proc->nodes))) {
2979 struct binder_node *node = rb_entry(n, struct binder_node, rb_node);
2982 rb_erase(&node->rb_node, &proc->nodes);
2983 list_del_init(&node->work.entry);
2984 if (hlist_empty(&node->refs)) {
2986 binder_stats_deleted(BINDER_STAT_NODE);
2988 struct binder_ref *ref;
2992 node->local_strong_refs = 0;
2993 node->local_weak_refs = 0;
2994 hlist_add_head(&node->dead_node, &binder_dead_nodes);
2996 hlist_for_each_entry(ref, pos, &node->refs, node_entry) {
3000 if (list_empty(&ref->death->work.entry)) {
3001 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
3002 list_add_tail(&ref->death->work.entry, &ref->proc->todo);
3003 wake_up_interruptible(&ref->proc->wait);
3008 binder_debug(BINDER_DEBUG_DEAD_BINDER,
3009 "binder: node %d now dead, "
3010 "refs %d, death %d\n", node->debug_id,
3011 incoming_refs, death);
3015 while ((n = rb_first(&proc->refs_by_desc))) {
3016 struct binder_ref *ref = rb_entry(n, struct binder_ref,
3019 binder_delete_ref(ref);
3021 binder_release_work(&proc->todo);
3024 while ((n = rb_first(&proc->allocated_buffers))) {
3025 struct binder_buffer *buffer = rb_entry(n, struct binder_buffer,
3027 t = buffer->transaction;
3030 buffer->transaction = NULL;
3031 printk(KERN_ERR "binder: release proc %d, "
3032 "transaction %d, not freed\n",
3033 proc->pid, t->debug_id);
3036 binder_free_buf(proc, buffer);
3040 binder_stats_deleted(BINDER_STAT_PROC);
3045 for (i = 0; i < proc->buffer_size / PAGE_SIZE; i++) {
3046 if (proc->pages[i]) {
3047 void *page_addr = proc->buffer + i * PAGE_SIZE;
3048 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
3049 "binder_release: %d: "
3050 "page %d at %p not freed\n",
3053 unmap_kernel_range((unsigned long)page_addr,
3055 __free_page(proc->pages[i]);
3060 vfree(proc->buffer);
3063 put_task_struct(proc->tsk);
3065 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
3066 "binder_release: %d threads %d, nodes %d (ref %d), "
3067 "refs %d, active transactions %d, buffers %d, "
3069 proc->pid, threads, nodes, incoming_refs, outgoing_refs,
3070 active_transactions, buffers, page_count);
3075 static void binder_deferred_func(struct work_struct *work)
3077 struct binder_proc *proc;
3078 struct files_struct *files;
3082 mutex_lock(&binder_lock);
3083 mutex_lock(&binder_deferred_lock);
3084 if (!hlist_empty(&binder_deferred_list)) {
3085 proc = hlist_entry(binder_deferred_list.first,
3086 struct binder_proc, deferred_work_node);
3087 hlist_del_init(&proc->deferred_work_node);
3088 defer = proc->deferred_work;
3089 proc->deferred_work = 0;
3094 mutex_unlock(&binder_deferred_lock);
3097 if (defer & BINDER_DEFERRED_PUT_FILES) {
3098 files = proc->files;
3103 if (defer & BINDER_DEFERRED_FLUSH)
3104 binder_deferred_flush(proc);
3106 if (defer & BINDER_DEFERRED_RELEASE)
3107 binder_deferred_release(proc); /* frees proc */
3109 mutex_unlock(&binder_lock);
3111 put_files_struct(files);
3114 static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
3117 binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
3119 mutex_lock(&binder_deferred_lock);
3120 proc->deferred_work |= defer;
3121 if (hlist_unhashed(&proc->deferred_work_node)) {
3122 hlist_add_head(&proc->deferred_work_node,
3123 &binder_deferred_list);
3124 queue_work(binder_deferred_workqueue, &binder_deferred_work);
3126 mutex_unlock(&binder_deferred_lock);
3129 static void print_binder_transaction(struct seq_file *m, const char *prefix,
3130 struct binder_transaction *t)
3133 "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %ld r%d",
3134 prefix, t->debug_id, t,
3135 t->from ? t->from->proc->pid : 0,
3136 t->from ? t->from->pid : 0,
3137 t->to_proc ? t->to_proc->pid : 0,
3138 t->to_thread ? t->to_thread->pid : 0,
3139 t->code, t->flags, t->priority, t->need_reply);
3140 if (t->buffer == NULL) {
3141 seq_puts(m, " buffer free\n");
3144 if (t->buffer->target_node)
3145 seq_printf(m, " node %d",
3146 t->buffer->target_node->debug_id);
3147 seq_printf(m, " size %zd:%zd data %p\n",
3148 t->buffer->data_size, t->buffer->offsets_size,
3152 static void print_binder_buffer(struct seq_file *m, const char *prefix,
3153 struct binder_buffer *buffer)
3155 seq_printf(m, "%s %d: %p size %zd:%zd %s\n",
3156 prefix, buffer->debug_id, buffer->data,
3157 buffer->data_size, buffer->offsets_size,
3158 buffer->transaction ? "active" : "delivered");
3161 static void print_binder_work(struct seq_file *m, const char *prefix,
3162 const char *transaction_prefix,
3163 struct binder_work *w)
3165 struct binder_node *node;
3166 struct binder_transaction *t;
3169 case BINDER_WORK_TRANSACTION:
3170 t = container_of(w, struct binder_transaction, work);
3171 print_binder_transaction(m, transaction_prefix, t);
3173 case BINDER_WORK_TRANSACTION_COMPLETE:
3174 seq_printf(m, "%stransaction complete\n", prefix);
3176 case BINDER_WORK_NODE:
3177 node = container_of(w, struct binder_node, work);
3178 seq_printf(m, "%snode work %d: u%p c%p\n",
3179 prefix, node->debug_id, node->ptr, node->cookie);
3181 case BINDER_WORK_DEAD_BINDER:
3182 seq_printf(m, "%shas dead binder\n", prefix);
3184 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
3185 seq_printf(m, "%shas cleared dead binder\n", prefix);
3187 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
3188 seq_printf(m, "%shas cleared death notification\n", prefix);
3191 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
3196 static void print_binder_thread(struct seq_file *m,
3197 struct binder_thread *thread,
3200 struct binder_transaction *t;
3201 struct binder_work *w;
3202 size_t start_pos = m->count;
3205 seq_printf(m, " thread %d: l %02x\n", thread->pid, thread->looper);
3206 header_pos = m->count;
3207 t = thread->transaction_stack;
3209 if (t->from == thread) {
3210 print_binder_transaction(m,
3211 " outgoing transaction", t);
3213 } else if (t->to_thread == thread) {
3214 print_binder_transaction(m,
3215 " incoming transaction", t);
3218 print_binder_transaction(m, " bad transaction", t);
3222 list_for_each_entry(w, &thread->todo, entry) {
3223 print_binder_work(m, " ", " pending transaction", w);
3225 if (!print_always && m->count == header_pos)
3226 m->count = start_pos;
3229 static void print_binder_node(struct seq_file *m, struct binder_node *node)
3231 struct binder_ref *ref;
3232 struct hlist_node *pos;
3233 struct binder_work *w;
3237 hlist_for_each_entry(ref, pos, &node->refs, node_entry)
3240 seq_printf(m, " node %d: u%p c%p hs %d hw %d ls %d lw %d is %d iw %d",
3241 node->debug_id, node->ptr, node->cookie,
3242 node->has_strong_ref, node->has_weak_ref,
3243 node->local_strong_refs, node->local_weak_refs,
3244 node->internal_strong_refs, count);
3246 seq_puts(m, " proc");
3247 hlist_for_each_entry(ref, pos, &node->refs, node_entry)
3248 seq_printf(m, " %d", ref->proc->pid);
3251 list_for_each_entry(w, &node->async_todo, entry)
3252 print_binder_work(m, " ",
3253 " pending async transaction", w);
3256 static void print_binder_ref(struct seq_file *m, struct binder_ref *ref)
3258 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %p\n",
3259 ref->debug_id, ref->desc, ref->node->proc ? "" : "dead ",
3260 ref->node->debug_id, ref->strong, ref->weak, ref->death);
3263 static void print_binder_proc(struct seq_file *m,
3264 struct binder_proc *proc, int print_all)
3266 struct binder_work *w;
3268 size_t start_pos = m->count;
3271 seq_printf(m, "proc %d\n", proc->pid);
3272 header_pos = m->count;
3274 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3275 print_binder_thread(m, rb_entry(n, struct binder_thread,
3276 rb_node), print_all);
3277 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
3278 struct binder_node *node = rb_entry(n, struct binder_node,
3280 if (print_all || node->has_async_transaction)
3281 print_binder_node(m, node);
3284 for (n = rb_first(&proc->refs_by_desc);
3287 print_binder_ref(m, rb_entry(n, struct binder_ref,
3290 for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
3291 print_binder_buffer(m, " buffer",
3292 rb_entry(n, struct binder_buffer, rb_node));
3293 list_for_each_entry(w, &proc->todo, entry)
3294 print_binder_work(m, " ", " pending transaction", w);
3295 list_for_each_entry(w, &proc->delivered_death, entry) {
3296 seq_puts(m, " has delivered dead binder\n");
3299 if (!print_all && m->count == header_pos)
3300 m->count = start_pos;
3303 static const char *binder_return_strings[] = {
3308 "BR_ACQUIRE_RESULT",
3310 "BR_TRANSACTION_COMPLETE",
3315 "BR_ATTEMPT_ACQUIRE",
3320 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
3324 static const char *binder_command_strings[] = {
3327 "BC_ACQUIRE_RESULT",
3335 "BC_ATTEMPT_ACQUIRE",
3336 "BC_REGISTER_LOOPER",
3339 "BC_REQUEST_DEATH_NOTIFICATION",
3340 "BC_CLEAR_DEATH_NOTIFICATION",
3341 "BC_DEAD_BINDER_DONE"
3344 static const char *binder_objstat_strings[] = {
3351 "transaction_complete"
3354 static void print_binder_stats(struct seq_file *m, const char *prefix,
3355 struct binder_stats *stats)
3359 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
3360 ARRAY_SIZE(binder_command_strings));
3361 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
3363 seq_printf(m, "%s%s: %d\n", prefix,
3364 binder_command_strings[i], stats->bc[i]);
3367 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
3368 ARRAY_SIZE(binder_return_strings));
3369 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
3371 seq_printf(m, "%s%s: %d\n", prefix,
3372 binder_return_strings[i], stats->br[i]);
3375 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
3376 ARRAY_SIZE(binder_objstat_strings));
3377 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
3378 ARRAY_SIZE(stats->obj_deleted));
3379 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
3380 if (stats->obj_created[i] || stats->obj_deleted[i])
3381 seq_printf(m, "%s%s: active %d total %d\n", prefix,
3382 binder_objstat_strings[i],
3383 stats->obj_created[i] - stats->obj_deleted[i],
3384 stats->obj_created[i]);
3388 static void print_binder_proc_stats(struct seq_file *m,
3389 struct binder_proc *proc)
3391 struct binder_work *w;
3393 int count, strong, weak;
3395 seq_printf(m, "proc %d\n", proc->pid);
3397 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3399 seq_printf(m, " threads: %d\n", count);
3400 seq_printf(m, " requested threads: %d+%d/%d\n"
3401 " ready threads %d\n"
3402 " free async space %zd\n", proc->requested_threads,
3403 proc->requested_threads_started, proc->max_threads,
3404 proc->ready_threads, proc->free_async_space);
3406 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
3408 seq_printf(m, " nodes: %d\n", count);
3412 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
3413 struct binder_ref *ref = rb_entry(n, struct binder_ref,
3416 strong += ref->strong;
3419 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
3422 for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
3424 seq_printf(m, " buffers: %d\n", count);
3427 list_for_each_entry(w, &proc->todo, entry) {
3429 case BINDER_WORK_TRANSACTION:
3436 seq_printf(m, " pending transactions: %d\n", count);
3438 print_binder_stats(m, " ", &proc->stats);
3442 static int binder_state_show(struct seq_file *m, void *unused)
3444 struct binder_proc *proc;
3445 struct hlist_node *pos;
3446 struct binder_node *node;
3447 int do_lock = !binder_debug_no_lock;
3450 mutex_lock(&binder_lock);
3452 seq_puts(m, "binder state:\n");
3454 if (!hlist_empty(&binder_dead_nodes))
3455 seq_puts(m, "dead nodes:\n");
3456 hlist_for_each_entry(node, pos, &binder_dead_nodes, dead_node)
3457 print_binder_node(m, node);
3459 hlist_for_each_entry(proc, pos, &binder_procs, proc_node)
3460 print_binder_proc(m, proc, 1);
3462 mutex_unlock(&binder_lock);
3466 static int binder_stats_show(struct seq_file *m, void *unused)
3468 struct binder_proc *proc;
3469 struct hlist_node *pos;
3470 int do_lock = !binder_debug_no_lock;
3473 mutex_lock(&binder_lock);
3475 seq_puts(m, "binder stats:\n");
3477 print_binder_stats(m, "", &binder_stats);
3479 hlist_for_each_entry(proc, pos, &binder_procs, proc_node)
3480 print_binder_proc_stats(m, proc);
3482 mutex_unlock(&binder_lock);
3486 static int binder_transactions_show(struct seq_file *m, void *unused)
3488 struct binder_proc *proc;
3489 struct hlist_node *pos;
3490 int do_lock = !binder_debug_no_lock;
3493 mutex_lock(&binder_lock);
3495 seq_puts(m, "binder transactions:\n");
3496 hlist_for_each_entry(proc, pos, &binder_procs, proc_node)
3497 print_binder_proc(m, proc, 0);
3499 mutex_unlock(&binder_lock);
3503 static int binder_proc_show(struct seq_file *m, void *unused)
3505 struct binder_proc *proc = m->private;
3506 int do_lock = !binder_debug_no_lock;
3509 mutex_lock(&binder_lock);
3510 seq_puts(m, "binder proc state:\n");
3511 print_binder_proc(m, proc, 1);
3513 mutex_unlock(&binder_lock);
3517 static void print_binder_transaction_log_entry(struct seq_file *m,
3518 struct binder_transaction_log_entry *e)
3521 "%d: %s from %d:%d to %d:%d node %d handle %d size %d:%d\n",
3522 e->debug_id, (e->call_type == 2) ? "reply" :
3523 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
3524 e->from_thread, e->to_proc, e->to_thread, e->to_node,
3525 e->target_handle, e->data_size, e->offsets_size);
3528 static int binder_transaction_log_show(struct seq_file *m, void *unused)
3530 struct binder_transaction_log *log = m->private;
3534 for (i = log->next; i < ARRAY_SIZE(log->entry); i++)
3535 print_binder_transaction_log_entry(m, &log->entry[i]);
3537 for (i = 0; i < log->next; i++)
3538 print_binder_transaction_log_entry(m, &log->entry[i]);
3542 static const struct file_operations binder_fops = {
3543 .owner = THIS_MODULE,
3544 .poll = binder_poll,
3545 .unlocked_ioctl = binder_ioctl,
3546 .mmap = binder_mmap,
3547 .open = binder_open,
3548 .flush = binder_flush,
3549 .release = binder_release,
3552 static struct miscdevice binder_miscdev = {
3553 .minor = MISC_DYNAMIC_MINOR,
3555 .fops = &binder_fops
3558 BINDER_DEBUG_ENTRY(state);
3559 BINDER_DEBUG_ENTRY(stats);
3560 BINDER_DEBUG_ENTRY(transactions);
3561 BINDER_DEBUG_ENTRY(transaction_log);
3563 static int __init binder_init(void)
3567 binder_deferred_workqueue = create_singlethread_workqueue("binder");
3568 if (!binder_deferred_workqueue)
3571 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
3572 if (binder_debugfs_dir_entry_root)
3573 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
3574 binder_debugfs_dir_entry_root);
3575 ret = misc_register(&binder_miscdev);
3576 if (binder_debugfs_dir_entry_root) {
3577 debugfs_create_file("state",
3579 binder_debugfs_dir_entry_root,
3581 &binder_state_fops);
3582 debugfs_create_file("stats",
3584 binder_debugfs_dir_entry_root,
3586 &binder_stats_fops);
3587 debugfs_create_file("transactions",
3589 binder_debugfs_dir_entry_root,
3591 &binder_transactions_fops);
3592 debugfs_create_file("transaction_log",
3594 binder_debugfs_dir_entry_root,
3595 &binder_transaction_log,
3596 &binder_transaction_log_fops);
3597 debugfs_create_file("failed_transaction_log",
3599 binder_debugfs_dir_entry_root,
3600 &binder_transaction_log_failed,
3601 &binder_transaction_log_fops);
3606 device_initcall(binder_init);
3608 MODULE_LICENSE("GPL v2");