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>
37 static DEFINE_MUTEX(binder_lock);
38 static DEFINE_MUTEX(binder_deferred_lock);
40 static HLIST_HEAD(binder_procs);
41 static HLIST_HEAD(binder_deferred_list);
42 static HLIST_HEAD(binder_dead_nodes);
44 static struct proc_dir_entry *binder_proc_dir_entry_root;
45 static struct proc_dir_entry *binder_proc_dir_entry_proc;
46 static struct binder_node *binder_context_mgr_node;
47 static uid_t binder_context_mgr_uid = -1;
48 static int binder_last_id;
50 static int binder_read_proc_proc(char *page, char **start, off_t off,
51 int count, int *eof, void *data);
53 /* This is only defined in include/asm-arm/sizes.h */
59 #define SZ_4M 0x400000
62 #define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
64 #define BINDER_SMALL_BUF_SIZE (PAGE_SIZE * 64)
67 BINDER_DEBUG_USER_ERROR = 1U << 0,
68 BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1,
69 BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2,
70 BINDER_DEBUG_OPEN_CLOSE = 1U << 3,
71 BINDER_DEBUG_DEAD_BINDER = 1U << 4,
72 BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5,
73 BINDER_DEBUG_READ_WRITE = 1U << 6,
74 BINDER_DEBUG_USER_REFS = 1U << 7,
75 BINDER_DEBUG_THREADS = 1U << 8,
76 BINDER_DEBUG_TRANSACTION = 1U << 9,
77 BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10,
78 BINDER_DEBUG_FREE_BUFFER = 1U << 11,
79 BINDER_DEBUG_INTERNAL_REFS = 1U << 12,
80 BINDER_DEBUG_BUFFER_ALLOC = 1U << 13,
81 BINDER_DEBUG_PRIORITY_CAP = 1U << 14,
82 BINDER_DEBUG_BUFFER_ALLOC_ASYNC = 1U << 15,
84 static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR |
85 BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION;
86 module_param_named(debug_mask, binder_debug_mask, uint, S_IWUSR | S_IRUGO);
88 static int binder_debug_no_lock;
89 module_param_named(proc_no_lock, binder_debug_no_lock, bool, S_IWUSR | S_IRUGO);
91 static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
92 static int binder_stop_on_user_error;
94 static int binder_set_stop_on_user_error(const char *val,
95 struct kernel_param *kp)
98 ret = param_set_int(val, kp);
99 if (binder_stop_on_user_error < 2)
100 wake_up(&binder_user_error_wait);
103 module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
104 param_get_int, &binder_stop_on_user_error, S_IWUSR | S_IRUGO);
106 #define binder_debug(mask, x...) \
108 if (binder_debug_mask & mask) \
109 printk(KERN_INFO x); \
112 #define binder_user_error(x...) \
114 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
115 printk(KERN_INFO x); \
116 if (binder_stop_on_user_error) \
117 binder_stop_on_user_error = 2; \
126 BINDER_STAT_TRANSACTION,
127 BINDER_STAT_TRANSACTION_COMPLETE,
131 struct binder_stats {
132 int br[_IOC_NR(BR_FAILED_REPLY) + 1];
133 int bc[_IOC_NR(BC_DEAD_BINDER_DONE) + 1];
134 int obj_created[BINDER_STAT_COUNT];
135 int obj_deleted[BINDER_STAT_COUNT];
138 static struct binder_stats binder_stats;
140 struct binder_transaction_log_entry {
152 struct binder_transaction_log {
155 struct binder_transaction_log_entry entry[32];
157 struct binder_transaction_log binder_transaction_log;
158 struct binder_transaction_log binder_transaction_log_failed;
160 static struct binder_transaction_log_entry *binder_transaction_log_add(
161 struct binder_transaction_log *log)
163 struct binder_transaction_log_entry *e;
164 e = &log->entry[log->next];
165 memset(e, 0, sizeof(*e));
167 if (log->next == ARRAY_SIZE(log->entry)) {
175 struct list_head entry;
177 BINDER_WORK_TRANSACTION = 1,
178 BINDER_WORK_TRANSACTION_COMPLETE,
180 BINDER_WORK_DEAD_BINDER,
181 BINDER_WORK_DEAD_BINDER_AND_CLEAR,
182 BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
188 struct binder_work work;
190 struct rb_node rb_node;
191 struct hlist_node dead_node;
193 struct binder_proc *proc;
194 struct hlist_head refs;
195 int internal_strong_refs;
197 int local_strong_refs;
200 unsigned has_strong_ref:1;
201 unsigned pending_strong_ref:1;
202 unsigned has_weak_ref:1;
203 unsigned pending_weak_ref:1;
204 unsigned has_async_transaction:1;
205 unsigned accept_fds:1;
206 unsigned min_priority:8;
207 struct list_head async_todo;
210 struct binder_ref_death {
211 struct binder_work work;
216 /* Lookups needed: */
217 /* node + proc => ref (transaction) */
218 /* desc + proc => ref (transaction, inc/dec ref) */
219 /* node => refs + procs (proc exit) */
221 struct rb_node rb_node_desc;
222 struct rb_node rb_node_node;
223 struct hlist_node node_entry;
224 struct binder_proc *proc;
225 struct binder_node *node;
229 struct binder_ref_death *death;
232 struct binder_buffer {
233 struct list_head entry; /* free and allocated entries by addesss */
234 struct rb_node rb_node; /* free entry by size or allocated entry */
237 unsigned allow_user_free:1;
238 unsigned async_transaction:1;
239 unsigned debug_id:29;
241 struct binder_transaction *transaction;
243 struct binder_node *target_node;
249 enum binder_deferred_state {
250 BINDER_DEFERRED_PUT_FILES = 0x01,
251 BINDER_DEFERRED_FLUSH = 0x02,
252 BINDER_DEFERRED_RELEASE = 0x04,
256 struct hlist_node proc_node;
257 struct rb_root threads;
258 struct rb_root nodes;
259 struct rb_root refs_by_desc;
260 struct rb_root refs_by_node;
262 struct vm_area_struct *vma;
263 struct task_struct *tsk;
264 struct files_struct *files;
265 struct hlist_node deferred_work_node;
268 ptrdiff_t user_buffer_offset;
270 struct list_head buffers;
271 struct rb_root free_buffers;
272 struct rb_root allocated_buffers;
273 size_t free_async_space;
277 uint32_t buffer_free;
278 struct list_head todo;
279 wait_queue_head_t wait;
280 struct binder_stats stats;
281 struct list_head delivered_death;
283 int requested_threads;
284 int requested_threads_started;
286 long default_priority;
290 BINDER_LOOPER_STATE_REGISTERED = 0x01,
291 BINDER_LOOPER_STATE_ENTERED = 0x02,
292 BINDER_LOOPER_STATE_EXITED = 0x04,
293 BINDER_LOOPER_STATE_INVALID = 0x08,
294 BINDER_LOOPER_STATE_WAITING = 0x10,
295 BINDER_LOOPER_STATE_NEED_RETURN = 0x20
298 struct binder_thread {
299 struct binder_proc *proc;
300 struct rb_node rb_node;
303 struct binder_transaction *transaction_stack;
304 struct list_head todo;
305 uint32_t return_error; /* Write failed, return error code in read buf */
306 uint32_t return_error2; /* Write failed, return error code in read */
307 /* buffer. Used when sending a reply to a dead process that */
308 /* we are also waiting on */
309 wait_queue_head_t wait;
310 struct binder_stats stats;
313 struct binder_transaction {
315 struct binder_work work;
316 struct binder_thread *from;
317 struct binder_transaction *from_parent;
318 struct binder_proc *to_proc;
319 struct binder_thread *to_thread;
320 struct binder_transaction *to_parent;
321 unsigned need_reply:1;
322 /* unsigned is_dead:1; */ /* not used at the moment */
324 struct binder_buffer *buffer;
333 binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
336 * copied from get_unused_fd_flags
338 int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
340 struct files_struct *files = proc->files;
343 unsigned long rlim_cur;
350 spin_lock(&files->file_lock);
353 fdt = files_fdtable(files);
354 fd = find_next_zero_bit(fdt->open_fds->fds_bits, fdt->max_fds,
358 * N.B. For clone tasks sharing a files structure, this test
359 * will limit the total number of files that can be opened.
362 if (lock_task_sighand(proc->tsk, &irqs)) {
363 rlim_cur = proc->tsk->signal->rlim[RLIMIT_NOFILE].rlim_cur;
364 unlock_task_sighand(proc->tsk, &irqs);
369 /* Do we need to expand the fd array or fd set? */
370 error = expand_files(files, fd);
376 * If we needed to expand the fs array we
377 * might have blocked - try again.
383 FD_SET(fd, fdt->open_fds);
384 if (flags & O_CLOEXEC)
385 FD_SET(fd, fdt->close_on_exec);
387 FD_CLR(fd, fdt->close_on_exec);
388 files->next_fd = fd + 1;
391 if (fdt->fd[fd] != NULL) {
392 printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
399 spin_unlock(&files->file_lock);
404 * copied from fd_install
406 static void task_fd_install(
407 struct binder_proc *proc, unsigned int fd, struct file *file)
409 struct files_struct *files = proc->files;
415 spin_lock(&files->file_lock);
416 fdt = files_fdtable(files);
417 BUG_ON(fdt->fd[fd] != NULL);
418 rcu_assign_pointer(fdt->fd[fd], file);
419 spin_unlock(&files->file_lock);
423 * copied from __put_unused_fd in open.c
425 static void __put_unused_fd(struct files_struct *files, unsigned int fd)
427 struct fdtable *fdt = files_fdtable(files);
428 __FD_CLR(fd, fdt->open_fds);
429 if (fd < files->next_fd)
434 * copied from sys_close
436 static long task_close_fd(struct binder_proc *proc, unsigned int fd)
439 struct files_struct *files = proc->files;
446 spin_lock(&files->file_lock);
447 fdt = files_fdtable(files);
448 if (fd >= fdt->max_fds)
453 rcu_assign_pointer(fdt->fd[fd], NULL);
454 FD_CLR(fd, fdt->close_on_exec);
455 __put_unused_fd(files, fd);
456 spin_unlock(&files->file_lock);
457 retval = filp_close(filp, files);
459 /* can't restart close syscall because file table entry was cleared */
460 if (unlikely(retval == -ERESTARTSYS ||
461 retval == -ERESTARTNOINTR ||
462 retval == -ERESTARTNOHAND ||
463 retval == -ERESTART_RESTARTBLOCK))
469 spin_unlock(&files->file_lock);
473 static void binder_set_nice(long nice)
476 if (can_nice(current, nice)) {
477 set_user_nice(current, nice);
480 min_nice = 20 - current->signal->rlim[RLIMIT_NICE].rlim_cur;
481 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
482 "binder: %d: nice value %ld not allowed use "
483 "%ld instead\n", current->pid, nice, min_nice);
484 set_user_nice(current, min_nice);
487 binder_user_error("binder: %d RLIMIT_NICE not set\n", current->pid);
490 static size_t binder_buffer_size(struct binder_proc *proc,
491 struct binder_buffer *buffer)
493 if (list_is_last(&buffer->entry, &proc->buffers))
494 return proc->buffer + proc->buffer_size - (void *)buffer->data;
496 return (size_t)list_entry(buffer->entry.next,
497 struct binder_buffer, entry) - (size_t)buffer->data;
500 static void binder_insert_free_buffer(struct binder_proc *proc,
501 struct binder_buffer *new_buffer)
503 struct rb_node **p = &proc->free_buffers.rb_node;
504 struct rb_node *parent = NULL;
505 struct binder_buffer *buffer;
507 size_t new_buffer_size;
509 BUG_ON(!new_buffer->free);
511 new_buffer_size = binder_buffer_size(proc, new_buffer);
513 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
514 "binder: %d: add free buffer, size %zd, "
515 "at %p\n", proc->pid, new_buffer_size, new_buffer);
519 buffer = rb_entry(parent, struct binder_buffer, rb_node);
520 BUG_ON(!buffer->free);
522 buffer_size = binder_buffer_size(proc, buffer);
524 if (new_buffer_size < buffer_size)
525 p = &parent->rb_left;
527 p = &parent->rb_right;
529 rb_link_node(&new_buffer->rb_node, parent, p);
530 rb_insert_color(&new_buffer->rb_node, &proc->free_buffers);
533 static void binder_insert_allocated_buffer(struct binder_proc *proc,
534 struct binder_buffer *new_buffer)
536 struct rb_node **p = &proc->allocated_buffers.rb_node;
537 struct rb_node *parent = NULL;
538 struct binder_buffer *buffer;
540 BUG_ON(new_buffer->free);
544 buffer = rb_entry(parent, struct binder_buffer, rb_node);
545 BUG_ON(buffer->free);
547 if (new_buffer < buffer)
548 p = &parent->rb_left;
549 else if (new_buffer > buffer)
550 p = &parent->rb_right;
554 rb_link_node(&new_buffer->rb_node, parent, p);
555 rb_insert_color(&new_buffer->rb_node, &proc->allocated_buffers);
558 static struct binder_buffer *binder_buffer_lookup(struct binder_proc *proc,
559 void __user *user_ptr)
561 struct rb_node *n = proc->allocated_buffers.rb_node;
562 struct binder_buffer *buffer;
563 struct binder_buffer *kern_ptr;
565 kern_ptr = user_ptr - proc->user_buffer_offset
566 - offsetof(struct binder_buffer, data);
569 buffer = rb_entry(n, struct binder_buffer, rb_node);
570 BUG_ON(buffer->free);
572 if (kern_ptr < buffer)
574 else if (kern_ptr > buffer)
582 static int binder_update_page_range(struct binder_proc *proc, int allocate,
583 void *start, void *end,
584 struct vm_area_struct *vma)
587 unsigned long user_page_addr;
588 struct vm_struct tmp_area;
590 struct mm_struct *mm;
592 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
593 "binder: %d: %s pages %p-%p\n", proc->pid,
594 allocate ? "allocate" : "free", start, end);
602 mm = get_task_mm(proc->tsk);
605 down_write(&mm->mmap_sem);
613 printk(KERN_ERR "binder: %d: binder_alloc_buf failed to "
614 "map pages in userspace, no vma\n", proc->pid);
618 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
620 struct page **page_array_ptr;
621 page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
624 *page = alloc_page(GFP_KERNEL | __GFP_ZERO);
626 printk(KERN_ERR "binder: %d: binder_alloc_buf failed "
627 "for page at %p\n", proc->pid, page_addr);
628 goto err_alloc_page_failed;
630 tmp_area.addr = page_addr;
631 tmp_area.size = PAGE_SIZE + PAGE_SIZE /* guard page? */;
632 page_array_ptr = page;
633 ret = map_vm_area(&tmp_area, PAGE_KERNEL, &page_array_ptr);
635 printk(KERN_ERR "binder: %d: binder_alloc_buf failed "
636 "to map page at %p in kernel\n",
637 proc->pid, page_addr);
638 goto err_map_kernel_failed;
641 (uintptr_t)page_addr + proc->user_buffer_offset;
642 ret = vm_insert_page(vma, user_page_addr, page[0]);
644 printk(KERN_ERR "binder: %d: binder_alloc_buf failed "
645 "to map page at %lx in userspace\n",
646 proc->pid, user_page_addr);
647 goto err_vm_insert_page_failed;
649 /* vm_insert_page does not seem to increment the refcount */
652 up_write(&mm->mmap_sem);
658 for (page_addr = end - PAGE_SIZE; page_addr >= start;
659 page_addr -= PAGE_SIZE) {
660 page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
662 zap_page_range(vma, (uintptr_t)page_addr +
663 proc->user_buffer_offset, PAGE_SIZE, NULL);
664 err_vm_insert_page_failed:
665 unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
666 err_map_kernel_failed:
669 err_alloc_page_failed:
674 up_write(&mm->mmap_sem);
680 static struct binder_buffer *binder_alloc_buf(struct binder_proc *proc,
682 size_t offsets_size, int is_async)
684 struct rb_node *n = proc->free_buffers.rb_node;
685 struct binder_buffer *buffer;
687 struct rb_node *best_fit = NULL;
692 if (proc->vma == NULL) {
693 printk(KERN_ERR "binder: %d: binder_alloc_buf, no vma\n",
698 size = ALIGN(data_size, sizeof(void *)) +
699 ALIGN(offsets_size, sizeof(void *));
701 if (size < data_size || size < offsets_size) {
702 binder_user_error("binder: %d: got transaction with invalid "
703 "size %zd-%zd\n", proc->pid, data_size, offsets_size);
708 proc->free_async_space < size + sizeof(struct binder_buffer)) {
709 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
710 "binder: %d: binder_alloc_buf size %zd"
711 "failed, no async space left\n", proc->pid, size);
716 buffer = rb_entry(n, struct binder_buffer, rb_node);
717 BUG_ON(!buffer->free);
718 buffer_size = binder_buffer_size(proc, buffer);
720 if (size < buffer_size) {
723 } else if (size > buffer_size)
730 if (best_fit == NULL) {
731 printk(KERN_ERR "binder: %d: binder_alloc_buf size %zd failed, "
732 "no address space\n", proc->pid, size);
736 buffer = rb_entry(best_fit, struct binder_buffer, rb_node);
737 buffer_size = binder_buffer_size(proc, buffer);
740 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
741 "binder: %d: binder_alloc_buf size %zd got buff"
742 "er %p size %zd\n", proc->pid, size, buffer, buffer_size);
745 (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK);
747 if (size + sizeof(struct binder_buffer) + 4 >= buffer_size)
748 buffer_size = size; /* no room for other buffers */
750 buffer_size = size + sizeof(struct binder_buffer);
753 (void *)PAGE_ALIGN((uintptr_t)buffer->data + buffer_size);
754 if (end_page_addr > has_page_addr)
755 end_page_addr = has_page_addr;
756 if (binder_update_page_range(proc, 1,
757 (void *)PAGE_ALIGN((uintptr_t)buffer->data), end_page_addr, NULL))
760 rb_erase(best_fit, &proc->free_buffers);
762 binder_insert_allocated_buffer(proc, buffer);
763 if (buffer_size != size) {
764 struct binder_buffer *new_buffer = (void *)buffer->data + size;
765 list_add(&new_buffer->entry, &buffer->entry);
766 new_buffer->free = 1;
767 binder_insert_free_buffer(proc, new_buffer);
769 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
770 "binder: %d: binder_alloc_buf size %zd got "
771 "%p\n", proc->pid, size, buffer);
772 buffer->data_size = data_size;
773 buffer->offsets_size = offsets_size;
774 buffer->async_transaction = is_async;
776 proc->free_async_space -= size + sizeof(struct binder_buffer);
777 binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
778 "binder: %d: binder_alloc_buf size %zd "
779 "async free %zd\n", proc->pid, size,
780 proc->free_async_space);
786 static void *buffer_start_page(struct binder_buffer *buffer)
788 return (void *)((uintptr_t)buffer & PAGE_MASK);
791 static void *buffer_end_page(struct binder_buffer *buffer)
793 return (void *)(((uintptr_t)(buffer + 1) - 1) & PAGE_MASK);
796 static void binder_delete_free_buffer(struct binder_proc *proc,
797 struct binder_buffer *buffer)
799 struct binder_buffer *prev, *next = NULL;
800 int free_page_end = 1;
801 int free_page_start = 1;
803 BUG_ON(proc->buffers.next == &buffer->entry);
804 prev = list_entry(buffer->entry.prev, struct binder_buffer, entry);
806 if (buffer_end_page(prev) == buffer_start_page(buffer)) {
808 if (buffer_end_page(prev) == buffer_end_page(buffer))
810 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
811 "binder: %d: merge free, buffer %p "
812 "share page with %p\n", proc->pid, buffer, prev);
815 if (!list_is_last(&buffer->entry, &proc->buffers)) {
816 next = list_entry(buffer->entry.next,
817 struct binder_buffer, entry);
818 if (buffer_start_page(next) == buffer_end_page(buffer)) {
820 if (buffer_start_page(next) ==
821 buffer_start_page(buffer))
823 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
824 "binder: %d: merge free, buffer"
825 " %p share page with %p\n", proc->pid,
829 list_del(&buffer->entry);
830 if (free_page_start || free_page_end) {
831 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
832 "binder: %d: merge free, buffer %p do "
833 "not share page%s%s with with %p or %p\n",
834 proc->pid, buffer, free_page_start ? "" : " end",
835 free_page_end ? "" : " start", prev, next);
836 binder_update_page_range(proc, 0, free_page_start ?
837 buffer_start_page(buffer) : buffer_end_page(buffer),
838 (free_page_end ? buffer_end_page(buffer) :
839 buffer_start_page(buffer)) + PAGE_SIZE, NULL);
843 static void binder_free_buf(struct binder_proc *proc,
844 struct binder_buffer *buffer)
846 size_t size, buffer_size;
848 buffer_size = binder_buffer_size(proc, buffer);
850 size = ALIGN(buffer->data_size, sizeof(void *)) +
851 ALIGN(buffer->offsets_size, sizeof(void *));
853 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
854 "binder: %d: binder_free_buf %p size %zd buffer"
855 "_size %zd\n", proc->pid, buffer, size, buffer_size);
857 BUG_ON(buffer->free);
858 BUG_ON(size > buffer_size);
859 BUG_ON(buffer->transaction != NULL);
860 BUG_ON((void *)buffer < proc->buffer);
861 BUG_ON((void *)buffer > proc->buffer + proc->buffer_size);
863 if (buffer->async_transaction) {
864 proc->free_async_space += size + sizeof(struct binder_buffer);
866 binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
867 "binder: %d: binder_free_buf size %zd "
868 "async free %zd\n", proc->pid, size,
869 proc->free_async_space);
872 binder_update_page_range(proc, 0,
873 (void *)PAGE_ALIGN((uintptr_t)buffer->data),
874 (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK),
876 rb_erase(&buffer->rb_node, &proc->allocated_buffers);
878 if (!list_is_last(&buffer->entry, &proc->buffers)) {
879 struct binder_buffer *next = list_entry(buffer->entry.next,
880 struct binder_buffer, entry);
882 rb_erase(&next->rb_node, &proc->free_buffers);
883 binder_delete_free_buffer(proc, next);
886 if (proc->buffers.next != &buffer->entry) {
887 struct binder_buffer *prev = list_entry(buffer->entry.prev,
888 struct binder_buffer, entry);
890 binder_delete_free_buffer(proc, buffer);
891 rb_erase(&prev->rb_node, &proc->free_buffers);
895 binder_insert_free_buffer(proc, buffer);
898 static struct binder_node *binder_get_node(struct binder_proc *proc,
901 struct rb_node *n = proc->nodes.rb_node;
902 struct binder_node *node;
905 node = rb_entry(n, struct binder_node, rb_node);
909 else if (ptr > node->ptr)
917 static struct binder_node *binder_new_node(struct binder_proc *proc,
921 struct rb_node **p = &proc->nodes.rb_node;
922 struct rb_node *parent = NULL;
923 struct binder_node *node;
927 node = rb_entry(parent, struct binder_node, rb_node);
931 else if (ptr > node->ptr)
937 node = kzalloc(sizeof(*node), GFP_KERNEL);
940 binder_stats.obj_created[BINDER_STAT_NODE]++;
941 rb_link_node(&node->rb_node, parent, p);
942 rb_insert_color(&node->rb_node, &proc->nodes);
943 node->debug_id = ++binder_last_id;
946 node->cookie = cookie;
947 node->work.type = BINDER_WORK_NODE;
948 INIT_LIST_HEAD(&node->work.entry);
949 INIT_LIST_HEAD(&node->async_todo);
950 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
951 "binder: %d:%d node %d u%p c%p created\n",
952 proc->pid, current->pid, node->debug_id,
953 node->ptr, node->cookie);
957 static int binder_inc_node(struct binder_node *node, int strong, int internal,
958 struct list_head *target_list)
962 if (target_list == NULL &&
963 node->internal_strong_refs == 0 &&
964 !(node == binder_context_mgr_node &&
965 node->has_strong_ref)) {
966 printk(KERN_ERR "binder: invalid inc strong "
967 "node for %d\n", node->debug_id);
970 node->internal_strong_refs++;
972 node->local_strong_refs++;
973 if (!node->has_strong_ref && target_list) {
974 list_del_init(&node->work.entry);
975 list_add_tail(&node->work.entry, target_list);
979 node->local_weak_refs++;
980 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
981 if (target_list == NULL) {
982 printk(KERN_ERR "binder: invalid inc weak node "
983 "for %d\n", node->debug_id);
986 list_add_tail(&node->work.entry, target_list);
992 static int binder_dec_node(struct binder_node *node, int strong, int internal)
996 node->internal_strong_refs--;
998 node->local_strong_refs--;
999 if (node->local_strong_refs || node->internal_strong_refs)
1003 node->local_weak_refs--;
1004 if (node->local_weak_refs || !hlist_empty(&node->refs))
1007 if (node->proc && (node->has_strong_ref || node->has_weak_ref)) {
1008 if (list_empty(&node->work.entry)) {
1009 list_add_tail(&node->work.entry, &node->proc->todo);
1010 wake_up_interruptible(&node->proc->wait);
1013 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
1014 !node->local_weak_refs) {
1015 list_del_init(&node->work.entry);
1017 rb_erase(&node->rb_node, &node->proc->nodes);
1018 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1019 "binder: refless node %d deleted\n",
1022 hlist_del(&node->dead_node);
1023 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1024 "binder: dead node %d deleted\n",
1028 binder_stats.obj_deleted[BINDER_STAT_NODE]++;
1036 static struct binder_ref *binder_get_ref(struct binder_proc *proc,
1039 struct rb_node *n = proc->refs_by_desc.rb_node;
1040 struct binder_ref *ref;
1043 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1045 if (desc < ref->desc)
1047 else if (desc > ref->desc)
1055 static struct binder_ref *binder_get_ref_for_node(struct binder_proc *proc,
1056 struct binder_node *node)
1059 struct rb_node **p = &proc->refs_by_node.rb_node;
1060 struct rb_node *parent = NULL;
1061 struct binder_ref *ref, *new_ref;
1065 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1067 if (node < ref->node)
1069 else if (node > ref->node)
1070 p = &(*p)->rb_right;
1074 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1075 if (new_ref == NULL)
1077 binder_stats.obj_created[BINDER_STAT_REF]++;
1078 new_ref->debug_id = ++binder_last_id;
1079 new_ref->proc = proc;
1080 new_ref->node = node;
1081 rb_link_node(&new_ref->rb_node_node, parent, p);
1082 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1084 new_ref->desc = (node == binder_context_mgr_node) ? 0 : 1;
1085 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1086 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1087 if (ref->desc > new_ref->desc)
1089 new_ref->desc = ref->desc + 1;
1092 p = &proc->refs_by_desc.rb_node;
1095 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1097 if (new_ref->desc < ref->desc)
1099 else if (new_ref->desc > ref->desc)
1100 p = &(*p)->rb_right;
1104 rb_link_node(&new_ref->rb_node_desc, parent, p);
1105 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
1107 hlist_add_head(&new_ref->node_entry, &node->refs);
1109 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1110 "binder: %d new ref %d desc %d for "
1111 "node %d\n", proc->pid, new_ref->debug_id,
1112 new_ref->desc, node->debug_id);
1114 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1115 "binder: %d new ref %d desc %d for "
1116 "dead node\n", proc->pid, new_ref->debug_id,
1122 static void binder_delete_ref(struct binder_ref *ref)
1124 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1125 "binder: %d delete ref %d desc %d for "
1126 "node %d\n", ref->proc->pid, ref->debug_id,
1127 ref->desc, ref->node->debug_id);
1129 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1130 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
1132 binder_dec_node(ref->node, 1, 1);
1133 hlist_del(&ref->node_entry);
1134 binder_dec_node(ref->node, 0, 1);
1136 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1137 "binder: %d delete ref %d desc %d "
1138 "has death notification\n", ref->proc->pid,
1139 ref->debug_id, ref->desc);
1140 list_del(&ref->death->work.entry);
1142 binder_stats.obj_deleted[BINDER_STAT_DEATH]++;
1145 binder_stats.obj_deleted[BINDER_STAT_REF]++;
1148 static int binder_inc_ref(struct binder_ref *ref, int strong,
1149 struct list_head *target_list)
1153 if (ref->strong == 0) {
1154 ret = binder_inc_node(ref->node, 1, 1, target_list);
1160 if (ref->weak == 0) {
1161 ret = binder_inc_node(ref->node, 0, 1, target_list);
1171 static int binder_dec_ref(struct binder_ref *ref, int strong)
1174 if (ref->strong == 0) {
1175 binder_user_error("binder: %d invalid dec strong, "
1176 "ref %d desc %d s %d w %d\n",
1177 ref->proc->pid, ref->debug_id,
1178 ref->desc, ref->strong, ref->weak);
1182 if (ref->strong == 0) {
1184 ret = binder_dec_node(ref->node, strong, 1);
1189 if (ref->weak == 0) {
1190 binder_user_error("binder: %d invalid dec weak, "
1191 "ref %d desc %d s %d w %d\n",
1192 ref->proc->pid, ref->debug_id,
1193 ref->desc, ref->strong, ref->weak);
1198 if (ref->strong == 0 && ref->weak == 0)
1199 binder_delete_ref(ref);
1203 static void binder_pop_transaction(struct binder_thread *target_thread,
1204 struct binder_transaction *t)
1206 if (target_thread) {
1207 BUG_ON(target_thread->transaction_stack != t);
1208 BUG_ON(target_thread->transaction_stack->from != target_thread);
1209 target_thread->transaction_stack =
1210 target_thread->transaction_stack->from_parent;
1215 t->buffer->transaction = NULL;
1217 binder_stats.obj_deleted[BINDER_STAT_TRANSACTION]++;
1220 static void binder_send_failed_reply(struct binder_transaction *t,
1221 uint32_t error_code)
1223 struct binder_thread *target_thread;
1224 BUG_ON(t->flags & TF_ONE_WAY);
1226 target_thread = t->from;
1227 if (target_thread) {
1228 if (target_thread->return_error != BR_OK &&
1229 target_thread->return_error2 == BR_OK) {
1230 target_thread->return_error2 =
1231 target_thread->return_error;
1232 target_thread->return_error = BR_OK;
1234 if (target_thread->return_error == BR_OK) {
1235 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1236 "binder: send failed reply for "
1237 "transaction %d to %d:%d\n",
1238 t->debug_id, target_thread->proc->pid,
1239 target_thread->pid);
1241 binder_pop_transaction(target_thread, t);
1242 target_thread->return_error = error_code;
1243 wake_up_interruptible(&target_thread->wait);
1245 printk(KERN_ERR "binder: reply failed, target "
1246 "thread, %d:%d, has error code %d "
1247 "already\n", target_thread->proc->pid,
1249 target_thread->return_error);
1253 struct binder_transaction *next = t->from_parent;
1255 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1256 "binder: send failed reply "
1257 "for transaction %d, target dead\n",
1260 binder_pop_transaction(target_thread, t);
1262 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1263 "binder: reply failed,"
1264 " no target thread at root\n");
1268 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1269 "binder: reply failed, no target "
1270 "thread -- retry %d\n", t->debug_id);
1275 static void binder_transaction_buffer_release(struct binder_proc *proc,
1276 struct binder_buffer *buffer,
1279 size_t *offp, *off_end;
1280 int debug_id = buffer->debug_id;
1282 binder_debug(BINDER_DEBUG_TRANSACTION,
1283 "binder: %d buffer release %d, size %zd-%zd, failed at %p\n",
1284 proc->pid, buffer->debug_id,
1285 buffer->data_size, buffer->offsets_size, failed_at);
1287 if (buffer->target_node)
1288 binder_dec_node(buffer->target_node, 1, 0);
1290 offp = (size_t *)(buffer->data + ALIGN(buffer->data_size, sizeof(void *)));
1292 off_end = failed_at;
1294 off_end = (void *)offp + buffer->offsets_size;
1295 for (; offp < off_end; offp++) {
1296 struct flat_binder_object *fp;
1297 if (*offp > buffer->data_size - sizeof(*fp) ||
1298 buffer->data_size < sizeof(*fp) ||
1299 !IS_ALIGNED(*offp, sizeof(void *))) {
1300 printk(KERN_ERR "binder: transaction release %d bad"
1301 "offset %zd, size %zd\n", debug_id, *offp, buffer->data_size);
1304 fp = (struct flat_binder_object *)(buffer->data + *offp);
1306 case BINDER_TYPE_BINDER:
1307 case BINDER_TYPE_WEAK_BINDER: {
1308 struct binder_node *node = binder_get_node(proc, fp->binder);
1310 printk(KERN_ERR "binder: transaction release %d bad node %p\n", debug_id, fp->binder);
1313 binder_debug(BINDER_DEBUG_TRANSACTION,
1315 node->debug_id, node->ptr);
1316 binder_dec_node(node, fp->type == BINDER_TYPE_BINDER, 0);
1318 case BINDER_TYPE_HANDLE:
1319 case BINDER_TYPE_WEAK_HANDLE: {
1320 struct binder_ref *ref = binder_get_ref(proc, fp->handle);
1322 printk(KERN_ERR "binder: transaction release %d bad handle %ld\n", debug_id, fp->handle);
1325 binder_debug(BINDER_DEBUG_TRANSACTION,
1326 " ref %d desc %d (node %d)\n",
1327 ref->debug_id, ref->desc, ref->node->debug_id);
1328 binder_dec_ref(ref, fp->type == BINDER_TYPE_HANDLE);
1331 case BINDER_TYPE_FD:
1332 binder_debug(BINDER_DEBUG_TRANSACTION,
1333 " fd %ld\n", fp->handle);
1335 task_close_fd(proc, fp->handle);
1339 printk(KERN_ERR "binder: transaction release %d bad object type %lx\n", debug_id, fp->type);
1345 static void binder_transaction(struct binder_proc *proc,
1346 struct binder_thread *thread,
1347 struct binder_transaction_data *tr, int reply)
1349 struct binder_transaction *t;
1350 struct binder_work *tcomplete;
1351 size_t *offp, *off_end;
1352 struct binder_proc *target_proc;
1353 struct binder_thread *target_thread = NULL;
1354 struct binder_node *target_node = NULL;
1355 struct list_head *target_list;
1356 wait_queue_head_t *target_wait;
1357 struct binder_transaction *in_reply_to = NULL;
1358 struct binder_transaction_log_entry *e;
1359 uint32_t return_error;
1361 e = binder_transaction_log_add(&binder_transaction_log);
1362 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
1363 e->from_proc = proc->pid;
1364 e->from_thread = thread->pid;
1365 e->target_handle = tr->target.handle;
1366 e->data_size = tr->data_size;
1367 e->offsets_size = tr->offsets_size;
1370 in_reply_to = thread->transaction_stack;
1371 if (in_reply_to == NULL) {
1372 binder_user_error("binder: %d:%d got reply transaction "
1373 "with no transaction stack\n",
1374 proc->pid, thread->pid);
1375 return_error = BR_FAILED_REPLY;
1376 goto err_empty_call_stack;
1378 binder_set_nice(in_reply_to->saved_priority);
1379 if (in_reply_to->to_thread != thread) {
1380 binder_user_error("binder: %d:%d got reply transaction "
1381 "with bad transaction stack,"
1382 " transaction %d has target %d:%d\n",
1383 proc->pid, thread->pid, in_reply_to->debug_id,
1384 in_reply_to->to_proc ?
1385 in_reply_to->to_proc->pid : 0,
1386 in_reply_to->to_thread ?
1387 in_reply_to->to_thread->pid : 0);
1388 return_error = BR_FAILED_REPLY;
1390 goto err_bad_call_stack;
1392 thread->transaction_stack = in_reply_to->to_parent;
1393 target_thread = in_reply_to->from;
1394 if (target_thread == NULL) {
1395 return_error = BR_DEAD_REPLY;
1396 goto err_dead_binder;
1398 if (target_thread->transaction_stack != in_reply_to) {
1399 binder_user_error("binder: %d:%d got reply transaction "
1400 "with bad target transaction stack %d, "
1402 proc->pid, thread->pid,
1403 target_thread->transaction_stack ?
1404 target_thread->transaction_stack->debug_id : 0,
1405 in_reply_to->debug_id);
1406 return_error = BR_FAILED_REPLY;
1408 target_thread = NULL;
1409 goto err_dead_binder;
1411 target_proc = target_thread->proc;
1413 if (tr->target.handle) {
1414 struct binder_ref *ref;
1415 ref = binder_get_ref(proc, tr->target.handle);
1417 binder_user_error("binder: %d:%d got "
1418 "transaction to invalid handle\n",
1419 proc->pid, thread->pid);
1420 return_error = BR_FAILED_REPLY;
1421 goto err_invalid_target_handle;
1423 target_node = ref->node;
1425 target_node = binder_context_mgr_node;
1426 if (target_node == NULL) {
1427 return_error = BR_DEAD_REPLY;
1428 goto err_no_context_mgr_node;
1431 e->to_node = target_node->debug_id;
1432 target_proc = target_node->proc;
1433 if (target_proc == NULL) {
1434 return_error = BR_DEAD_REPLY;
1435 goto err_dead_binder;
1437 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
1438 struct binder_transaction *tmp;
1439 tmp = thread->transaction_stack;
1440 if (tmp->to_thread != thread) {
1441 binder_user_error("binder: %d:%d got new "
1442 "transaction with bad transaction stack"
1443 ", transaction %d has target %d:%d\n",
1444 proc->pid, thread->pid, tmp->debug_id,
1445 tmp->to_proc ? tmp->to_proc->pid : 0,
1447 tmp->to_thread->pid : 0);
1448 return_error = BR_FAILED_REPLY;
1449 goto err_bad_call_stack;
1452 if (tmp->from && tmp->from->proc == target_proc)
1453 target_thread = tmp->from;
1454 tmp = tmp->from_parent;
1458 if (target_thread) {
1459 e->to_thread = target_thread->pid;
1460 target_list = &target_thread->todo;
1461 target_wait = &target_thread->wait;
1463 target_list = &target_proc->todo;
1464 target_wait = &target_proc->wait;
1466 e->to_proc = target_proc->pid;
1468 /* TODO: reuse incoming transaction for reply */
1469 t = kzalloc(sizeof(*t), GFP_KERNEL);
1471 return_error = BR_FAILED_REPLY;
1472 goto err_alloc_t_failed;
1474 binder_stats.obj_created[BINDER_STAT_TRANSACTION]++;
1476 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
1477 if (tcomplete == NULL) {
1478 return_error = BR_FAILED_REPLY;
1479 goto err_alloc_tcomplete_failed;
1481 binder_stats.obj_created[BINDER_STAT_TRANSACTION_COMPLETE]++;
1483 t->debug_id = ++binder_last_id;
1484 e->debug_id = t->debug_id;
1487 binder_debug(BINDER_DEBUG_TRANSACTION,
1488 "binder: %d:%d BC_REPLY %d -> %d:%d, "
1489 "data %p-%p size %zd-%zd\n",
1490 proc->pid, thread->pid, t->debug_id,
1491 target_proc->pid, target_thread->pid,
1492 tr->data.ptr.buffer, tr->data.ptr.offsets,
1493 tr->data_size, tr->offsets_size);
1495 binder_debug(BINDER_DEBUG_TRANSACTION,
1496 "binder: %d:%d BC_TRANSACTION %d -> "
1497 "%d - node %d, data %p-%p size %zd-%zd\n",
1498 proc->pid, thread->pid, t->debug_id,
1499 target_proc->pid, target_node->debug_id,
1500 tr->data.ptr.buffer, tr->data.ptr.offsets,
1501 tr->data_size, tr->offsets_size);
1503 if (!reply && !(tr->flags & TF_ONE_WAY))
1507 t->sender_euid = proc->tsk->cred->euid;
1508 t->to_proc = target_proc;
1509 t->to_thread = target_thread;
1511 t->flags = tr->flags;
1512 t->priority = task_nice(current);
1513 t->buffer = binder_alloc_buf(target_proc, tr->data_size,
1514 tr->offsets_size, !reply && (t->flags & TF_ONE_WAY));
1515 if (t->buffer == NULL) {
1516 return_error = BR_FAILED_REPLY;
1517 goto err_binder_alloc_buf_failed;
1519 t->buffer->allow_user_free = 0;
1520 t->buffer->debug_id = t->debug_id;
1521 t->buffer->transaction = t;
1522 t->buffer->target_node = target_node;
1524 binder_inc_node(target_node, 1, 0, NULL);
1526 offp = (size_t *)(t->buffer->data + ALIGN(tr->data_size, sizeof(void *)));
1528 if (copy_from_user(t->buffer->data, tr->data.ptr.buffer, tr->data_size)) {
1529 binder_user_error("binder: %d:%d got transaction with invalid "
1530 "data ptr\n", proc->pid, thread->pid);
1531 return_error = BR_FAILED_REPLY;
1532 goto err_copy_data_failed;
1534 if (copy_from_user(offp, tr->data.ptr.offsets, tr->offsets_size)) {
1535 binder_user_error("binder: %d:%d got transaction with invalid "
1536 "offsets ptr\n", proc->pid, thread->pid);
1537 return_error = BR_FAILED_REPLY;
1538 goto err_copy_data_failed;
1540 if (!IS_ALIGNED(tr->offsets_size, sizeof(size_t))) {
1541 binder_user_error("binder: %d:%d got transaction with "
1542 "invalid offsets size, %zd\n",
1543 proc->pid, thread->pid, tr->offsets_size);
1544 return_error = BR_FAILED_REPLY;
1545 goto err_bad_offset;
1547 off_end = (void *)offp + tr->offsets_size;
1548 for (; offp < off_end; offp++) {
1549 struct flat_binder_object *fp;
1550 if (*offp > t->buffer->data_size - sizeof(*fp) ||
1551 t->buffer->data_size < sizeof(*fp) ||
1552 !IS_ALIGNED(*offp, sizeof(void *))) {
1553 binder_user_error("binder: %d:%d got transaction with "
1554 "invalid offset, %zd\n",
1555 proc->pid, thread->pid, *offp);
1556 return_error = BR_FAILED_REPLY;
1557 goto err_bad_offset;
1559 fp = (struct flat_binder_object *)(t->buffer->data + *offp);
1561 case BINDER_TYPE_BINDER:
1562 case BINDER_TYPE_WEAK_BINDER: {
1563 struct binder_ref *ref;
1564 struct binder_node *node = binder_get_node(proc, fp->binder);
1566 node = binder_new_node(proc, fp->binder, fp->cookie);
1568 return_error = BR_FAILED_REPLY;
1569 goto err_binder_new_node_failed;
1571 node->min_priority = fp->flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
1572 node->accept_fds = !!(fp->flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
1574 if (fp->cookie != node->cookie) {
1575 binder_user_error("binder: %d:%d sending u%p "
1576 "node %d, cookie mismatch %p != %p\n",
1577 proc->pid, thread->pid,
1578 fp->binder, node->debug_id,
1579 fp->cookie, node->cookie);
1580 goto err_binder_get_ref_for_node_failed;
1582 ref = binder_get_ref_for_node(target_proc, node);
1584 return_error = BR_FAILED_REPLY;
1585 goto err_binder_get_ref_for_node_failed;
1587 if (fp->type == BINDER_TYPE_BINDER)
1588 fp->type = BINDER_TYPE_HANDLE;
1590 fp->type = BINDER_TYPE_WEAK_HANDLE;
1591 fp->handle = ref->desc;
1592 binder_inc_ref(ref, fp->type == BINDER_TYPE_HANDLE, &thread->todo);
1594 binder_debug(BINDER_DEBUG_TRANSACTION,
1595 " node %d u%p -> ref %d desc %d\n",
1596 node->debug_id, node->ptr, ref->debug_id,
1599 case BINDER_TYPE_HANDLE:
1600 case BINDER_TYPE_WEAK_HANDLE: {
1601 struct binder_ref *ref = binder_get_ref(proc, fp->handle);
1603 binder_user_error("binder: %d:%d got "
1604 "transaction with invalid "
1605 "handle, %ld\n", proc->pid,
1606 thread->pid, fp->handle);
1607 return_error = BR_FAILED_REPLY;
1608 goto err_binder_get_ref_failed;
1610 if (ref->node->proc == target_proc) {
1611 if (fp->type == BINDER_TYPE_HANDLE)
1612 fp->type = BINDER_TYPE_BINDER;
1614 fp->type = BINDER_TYPE_WEAK_BINDER;
1615 fp->binder = ref->node->ptr;
1616 fp->cookie = ref->node->cookie;
1617 binder_inc_node(ref->node, fp->type == BINDER_TYPE_BINDER, 0, NULL);
1618 binder_debug(BINDER_DEBUG_TRANSACTION,
1619 " ref %d desc %d -> node %d u%p\n",
1620 ref->debug_id, ref->desc, ref->node->debug_id,
1623 struct binder_ref *new_ref;
1624 new_ref = binder_get_ref_for_node(target_proc, ref->node);
1625 if (new_ref == NULL) {
1626 return_error = BR_FAILED_REPLY;
1627 goto err_binder_get_ref_for_node_failed;
1629 fp->handle = new_ref->desc;
1630 binder_inc_ref(new_ref, fp->type == BINDER_TYPE_HANDLE, NULL);
1631 binder_debug(BINDER_DEBUG_TRANSACTION,
1632 " ref %d desc %d -> ref %d desc %d (node %d)\n",
1633 ref->debug_id, ref->desc, new_ref->debug_id,
1634 new_ref->desc, ref->node->debug_id);
1638 case BINDER_TYPE_FD: {
1643 if (!(in_reply_to->flags & TF_ACCEPT_FDS)) {
1644 binder_user_error("binder: %d:%d got reply with fd, %ld, but target does not allow fds\n",
1645 proc->pid, thread->pid, fp->handle);
1646 return_error = BR_FAILED_REPLY;
1647 goto err_fd_not_allowed;
1649 } else if (!target_node->accept_fds) {
1650 binder_user_error("binder: %d:%d got transaction with fd, %ld, but target does not allow fds\n",
1651 proc->pid, thread->pid, fp->handle);
1652 return_error = BR_FAILED_REPLY;
1653 goto err_fd_not_allowed;
1656 file = fget(fp->handle);
1658 binder_user_error("binder: %d:%d got transaction with invalid fd, %ld\n",
1659 proc->pid, thread->pid, fp->handle);
1660 return_error = BR_FAILED_REPLY;
1661 goto err_fget_failed;
1663 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
1664 if (target_fd < 0) {
1666 return_error = BR_FAILED_REPLY;
1667 goto err_get_unused_fd_failed;
1669 task_fd_install(target_proc, target_fd, file);
1670 binder_debug(BINDER_DEBUG_TRANSACTION,
1671 " fd %ld -> %d\n", fp->handle, target_fd);
1673 fp->handle = target_fd;
1677 binder_user_error("binder: %d:%d got transactio"
1678 "n with invalid object type, %lx\n",
1679 proc->pid, thread->pid, fp->type);
1680 return_error = BR_FAILED_REPLY;
1681 goto err_bad_object_type;
1685 BUG_ON(t->buffer->async_transaction != 0);
1686 binder_pop_transaction(target_thread, in_reply_to);
1687 } else if (!(t->flags & TF_ONE_WAY)) {
1688 BUG_ON(t->buffer->async_transaction != 0);
1690 t->from_parent = thread->transaction_stack;
1691 thread->transaction_stack = t;
1693 BUG_ON(target_node == NULL);
1694 BUG_ON(t->buffer->async_transaction != 1);
1695 if (target_node->has_async_transaction) {
1696 target_list = &target_node->async_todo;
1699 target_node->has_async_transaction = 1;
1701 t->work.type = BINDER_WORK_TRANSACTION;
1702 list_add_tail(&t->work.entry, target_list);
1703 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
1704 list_add_tail(&tcomplete->entry, &thread->todo);
1706 wake_up_interruptible(target_wait);
1709 err_get_unused_fd_failed:
1712 err_binder_get_ref_for_node_failed:
1713 err_binder_get_ref_failed:
1714 err_binder_new_node_failed:
1715 err_bad_object_type:
1717 err_copy_data_failed:
1718 binder_transaction_buffer_release(target_proc, t->buffer, offp);
1719 t->buffer->transaction = NULL;
1720 binder_free_buf(target_proc, t->buffer);
1721 err_binder_alloc_buf_failed:
1723 binder_stats.obj_deleted[BINDER_STAT_TRANSACTION_COMPLETE]++;
1724 err_alloc_tcomplete_failed:
1726 binder_stats.obj_deleted[BINDER_STAT_TRANSACTION]++;
1729 err_empty_call_stack:
1731 err_invalid_target_handle:
1732 err_no_context_mgr_node:
1733 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1734 "binder: %d:%d transaction failed %d, size %zd-%zd\n",
1735 proc->pid, thread->pid, return_error,
1736 tr->data_size, tr->offsets_size);
1739 struct binder_transaction_log_entry *fe;
1740 fe = binder_transaction_log_add(&binder_transaction_log_failed);
1744 BUG_ON(thread->return_error != BR_OK);
1746 thread->return_error = BR_TRANSACTION_COMPLETE;
1747 binder_send_failed_reply(in_reply_to, return_error);
1749 thread->return_error = return_error;
1752 int binder_thread_write(struct binder_proc *proc, struct binder_thread *thread,
1753 void __user *buffer, int size, signed long *consumed)
1756 void __user *ptr = buffer + *consumed;
1757 void __user *end = buffer + size;
1759 while (ptr < end && thread->return_error == BR_OK) {
1760 if (get_user(cmd, (uint32_t __user *)ptr))
1762 ptr += sizeof(uint32_t);
1763 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
1764 binder_stats.bc[_IOC_NR(cmd)]++;
1765 proc->stats.bc[_IOC_NR(cmd)]++;
1766 thread->stats.bc[_IOC_NR(cmd)]++;
1774 struct binder_ref *ref;
1775 const char *debug_string;
1777 if (get_user(target, (uint32_t __user *)ptr))
1779 ptr += sizeof(uint32_t);
1780 if (target == 0 && binder_context_mgr_node &&
1781 (cmd == BC_INCREFS || cmd == BC_ACQUIRE)) {
1782 ref = binder_get_ref_for_node(proc,
1783 binder_context_mgr_node);
1784 if (ref->desc != target) {
1785 binder_user_error("binder: %d:"
1786 "%d tried to acquire "
1787 "reference to desc 0, "
1789 proc->pid, thread->pid,
1793 ref = binder_get_ref(proc, target);
1795 binder_user_error("binder: %d:%d refcou"
1796 "nt change on invalid ref %d\n",
1797 proc->pid, thread->pid, target);
1802 debug_string = "IncRefs";
1803 binder_inc_ref(ref, 0, NULL);
1806 debug_string = "Acquire";
1807 binder_inc_ref(ref, 1, NULL);
1810 debug_string = "Release";
1811 binder_dec_ref(ref, 1);
1815 debug_string = "DecRefs";
1816 binder_dec_ref(ref, 0);
1819 binder_debug(BINDER_DEBUG_USER_REFS,
1820 "binder: %d:%d %s ref %d desc %d s %d w %d for node %d\n",
1821 proc->pid, thread->pid, debug_string, ref->debug_id,
1822 ref->desc, ref->strong, ref->weak, ref->node->debug_id);
1825 case BC_INCREFS_DONE:
1826 case BC_ACQUIRE_DONE: {
1827 void __user *node_ptr;
1829 struct binder_node *node;
1831 if (get_user(node_ptr, (void * __user *)ptr))
1833 ptr += sizeof(void *);
1834 if (get_user(cookie, (void * __user *)ptr))
1836 ptr += sizeof(void *);
1837 node = binder_get_node(proc, node_ptr);
1839 binder_user_error("binder: %d:%d "
1840 "%s u%p no match\n",
1841 proc->pid, thread->pid,
1842 cmd == BC_INCREFS_DONE ?
1848 if (cookie != node->cookie) {
1849 binder_user_error("binder: %d:%d %s u%p node %d"
1850 " cookie mismatch %p != %p\n",
1851 proc->pid, thread->pid,
1852 cmd == BC_INCREFS_DONE ?
1853 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
1854 node_ptr, node->debug_id,
1855 cookie, node->cookie);
1858 if (cmd == BC_ACQUIRE_DONE) {
1859 if (node->pending_strong_ref == 0) {
1860 binder_user_error("binder: %d:%d "
1861 "BC_ACQUIRE_DONE node %d has "
1862 "no pending acquire request\n",
1863 proc->pid, thread->pid,
1867 node->pending_strong_ref = 0;
1869 if (node->pending_weak_ref == 0) {
1870 binder_user_error("binder: %d:%d "
1871 "BC_INCREFS_DONE node %d has "
1872 "no pending increfs request\n",
1873 proc->pid, thread->pid,
1877 node->pending_weak_ref = 0;
1879 binder_dec_node(node, cmd == BC_ACQUIRE_DONE, 0);
1880 binder_debug(BINDER_DEBUG_USER_REFS,
1881 "binder: %d:%d %s node %d ls %d lw %d\n",
1882 proc->pid, thread->pid,
1883 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
1884 node->debug_id, node->local_strong_refs, node->local_weak_refs);
1887 case BC_ATTEMPT_ACQUIRE:
1888 printk(KERN_ERR "binder: BC_ATTEMPT_ACQUIRE not supported\n");
1890 case BC_ACQUIRE_RESULT:
1891 printk(KERN_ERR "binder: BC_ACQUIRE_RESULT not supported\n");
1894 case BC_FREE_BUFFER: {
1895 void __user *data_ptr;
1896 struct binder_buffer *buffer;
1898 if (get_user(data_ptr, (void * __user *)ptr))
1900 ptr += sizeof(void *);
1902 buffer = binder_buffer_lookup(proc, data_ptr);
1903 if (buffer == NULL) {
1904 binder_user_error("binder: %d:%d "
1905 "BC_FREE_BUFFER u%p no match\n",
1906 proc->pid, thread->pid, data_ptr);
1909 if (!buffer->allow_user_free) {
1910 binder_user_error("binder: %d:%d "
1911 "BC_FREE_BUFFER u%p matched "
1912 "unreturned buffer\n",
1913 proc->pid, thread->pid, data_ptr);
1916 binder_debug(BINDER_DEBUG_FREE_BUFFER,
1917 "binder: %d:%d BC_FREE_BUFFER u%p found buffer %d for %s transaction\n",
1918 proc->pid, thread->pid, data_ptr, buffer->debug_id,
1919 buffer->transaction ? "active" : "finished");
1921 if (buffer->transaction) {
1922 buffer->transaction->buffer = NULL;
1923 buffer->transaction = NULL;
1925 if (buffer->async_transaction && buffer->target_node) {
1926 BUG_ON(!buffer->target_node->has_async_transaction);
1927 if (list_empty(&buffer->target_node->async_todo))
1928 buffer->target_node->has_async_transaction = 0;
1930 list_move_tail(buffer->target_node->async_todo.next, &thread->todo);
1932 binder_transaction_buffer_release(proc, buffer, NULL);
1933 binder_free_buf(proc, buffer);
1937 case BC_TRANSACTION:
1939 struct binder_transaction_data tr;
1941 if (copy_from_user(&tr, ptr, sizeof(tr)))
1944 binder_transaction(proc, thread, &tr, cmd == BC_REPLY);
1948 case BC_REGISTER_LOOPER:
1949 binder_debug(BINDER_DEBUG_THREADS,
1950 "binder: %d:%d BC_REGISTER_LOOPER\n",
1951 proc->pid, thread->pid);
1952 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
1953 thread->looper |= BINDER_LOOPER_STATE_INVALID;
1954 binder_user_error("binder: %d:%d ERROR:"
1955 " BC_REGISTER_LOOPER called "
1956 "after BC_ENTER_LOOPER\n",
1957 proc->pid, thread->pid);
1958 } else if (proc->requested_threads == 0) {
1959 thread->looper |= BINDER_LOOPER_STATE_INVALID;
1960 binder_user_error("binder: %d:%d ERROR:"
1961 " BC_REGISTER_LOOPER called "
1962 "without request\n",
1963 proc->pid, thread->pid);
1965 proc->requested_threads--;
1966 proc->requested_threads_started++;
1968 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
1970 case BC_ENTER_LOOPER:
1971 binder_debug(BINDER_DEBUG_THREADS,
1972 "binder: %d:%d BC_ENTER_LOOPER\n",
1973 proc->pid, thread->pid);
1974 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
1975 thread->looper |= BINDER_LOOPER_STATE_INVALID;
1976 binder_user_error("binder: %d:%d ERROR:"
1977 " BC_ENTER_LOOPER called after "
1978 "BC_REGISTER_LOOPER\n",
1979 proc->pid, thread->pid);
1981 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
1983 case BC_EXIT_LOOPER:
1984 binder_debug(BINDER_DEBUG_THREADS,
1985 "binder: %d:%d BC_EXIT_LOOPER\n",
1986 proc->pid, thread->pid);
1987 thread->looper |= BINDER_LOOPER_STATE_EXITED;
1990 case BC_REQUEST_DEATH_NOTIFICATION:
1991 case BC_CLEAR_DEATH_NOTIFICATION: {
1993 void __user *cookie;
1994 struct binder_ref *ref;
1995 struct binder_ref_death *death;
1997 if (get_user(target, (uint32_t __user *)ptr))
1999 ptr += sizeof(uint32_t);
2000 if (get_user(cookie, (void __user * __user *)ptr))
2002 ptr += sizeof(void *);
2003 ref = binder_get_ref(proc, target);
2005 binder_user_error("binder: %d:%d %s "
2007 proc->pid, thread->pid,
2008 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
2009 "BC_REQUEST_DEATH_NOTIFICATION" :
2010 "BC_CLEAR_DEATH_NOTIFICATION",
2015 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
2016 "binder: %d:%d %s %p ref %d desc %d s %d w %d for node %d\n",
2017 proc->pid, thread->pid,
2018 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
2019 "BC_REQUEST_DEATH_NOTIFICATION" :
2020 "BC_CLEAR_DEATH_NOTIFICATION",
2021 cookie, ref->debug_id, ref->desc,
2022 ref->strong, ref->weak, ref->node->debug_id);
2024 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
2026 binder_user_error("binder: %d:%"
2027 "d BC_REQUEST_DEATH_NOTI"
2028 "FICATION death notific"
2029 "ation already set\n",
2030 proc->pid, thread->pid);
2033 death = kzalloc(sizeof(*death), GFP_KERNEL);
2034 if (death == NULL) {
2035 thread->return_error = BR_ERROR;
2036 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2038 "BC_REQUEST_DEATH_NOTIFICATION failed\n",
2039 proc->pid, thread->pid);
2042 binder_stats.obj_created[BINDER_STAT_DEATH]++;
2043 INIT_LIST_HEAD(&death->work.entry);
2044 death->cookie = cookie;
2046 if (ref->node->proc == NULL) {
2047 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
2048 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2049 list_add_tail(&ref->death->work.entry, &thread->todo);
2051 list_add_tail(&ref->death->work.entry, &proc->todo);
2052 wake_up_interruptible(&proc->wait);
2056 if (ref->death == NULL) {
2057 binder_user_error("binder: %d:%"
2058 "d BC_CLEAR_DEATH_NOTIFI"
2059 "CATION death notificat"
2061 proc->pid, thread->pid);
2065 if (death->cookie != cookie) {
2066 binder_user_error("binder: %d:%"
2067 "d BC_CLEAR_DEATH_NOTIFI"
2068 "CATION death notificat"
2069 "ion cookie mismatch "
2071 proc->pid, thread->pid,
2072 death->cookie, cookie);
2076 if (list_empty(&death->work.entry)) {
2077 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2078 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2079 list_add_tail(&death->work.entry, &thread->todo);
2081 list_add_tail(&death->work.entry, &proc->todo);
2082 wake_up_interruptible(&proc->wait);
2085 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
2086 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
2090 case BC_DEAD_BINDER_DONE: {
2091 struct binder_work *w;
2092 void __user *cookie;
2093 struct binder_ref_death *death = NULL;
2094 if (get_user(cookie, (void __user * __user *)ptr))
2097 ptr += sizeof(void *);
2098 list_for_each_entry(w, &proc->delivered_death, entry) {
2099 struct binder_ref_death *tmp_death = container_of(w, struct binder_ref_death, work);
2100 if (tmp_death->cookie == cookie) {
2105 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2106 "binder: %d:%d BC_DEAD_BINDER_DONE %p found %p\n",
2107 proc->pid, thread->pid, cookie, death);
2108 if (death == NULL) {
2109 binder_user_error("binder: %d:%d BC_DEAD"
2110 "_BINDER_DONE %p not found\n",
2111 proc->pid, thread->pid, cookie);
2115 list_del_init(&death->work.entry);
2116 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
2117 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2118 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2119 list_add_tail(&death->work.entry, &thread->todo);
2121 list_add_tail(&death->work.entry, &proc->todo);
2122 wake_up_interruptible(&proc->wait);
2128 printk(KERN_ERR "binder: %d:%d unknown command %d\n",
2129 proc->pid, thread->pid, cmd);
2132 *consumed = ptr - buffer;
2137 void binder_stat_br(struct binder_proc *proc, struct binder_thread *thread,
2140 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
2141 binder_stats.br[_IOC_NR(cmd)]++;
2142 proc->stats.br[_IOC_NR(cmd)]++;
2143 thread->stats.br[_IOC_NR(cmd)]++;
2147 static int binder_has_proc_work(struct binder_proc *proc,
2148 struct binder_thread *thread)
2150 return !list_empty(&proc->todo) ||
2151 (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2154 static int binder_has_thread_work(struct binder_thread *thread)
2156 return !list_empty(&thread->todo) || thread->return_error != BR_OK ||
2157 (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2160 static int binder_thread_read(struct binder_proc *proc,
2161 struct binder_thread *thread,
2162 void __user *buffer, int size,
2163 signed long *consumed, int non_block)
2165 void __user *ptr = buffer + *consumed;
2166 void __user *end = buffer + size;
2169 int wait_for_proc_work;
2171 if (*consumed == 0) {
2172 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
2174 ptr += sizeof(uint32_t);
2178 wait_for_proc_work = thread->transaction_stack == NULL &&
2179 list_empty(&thread->todo);
2181 if (thread->return_error != BR_OK && ptr < end) {
2182 if (thread->return_error2 != BR_OK) {
2183 if (put_user(thread->return_error2, (uint32_t __user *)ptr))
2185 ptr += sizeof(uint32_t);
2188 thread->return_error2 = BR_OK;
2190 if (put_user(thread->return_error, (uint32_t __user *)ptr))
2192 ptr += sizeof(uint32_t);
2193 thread->return_error = BR_OK;
2198 thread->looper |= BINDER_LOOPER_STATE_WAITING;
2199 if (wait_for_proc_work)
2200 proc->ready_threads++;
2201 mutex_unlock(&binder_lock);
2202 if (wait_for_proc_work) {
2203 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2204 BINDER_LOOPER_STATE_ENTERED))) {
2205 binder_user_error("binder: %d:%d ERROR: Thread waiting "
2206 "for process work before calling BC_REGISTER_"
2207 "LOOPER or BC_ENTER_LOOPER (state %x)\n",
2208 proc->pid, thread->pid, thread->looper);
2209 wait_event_interruptible(binder_user_error_wait,
2210 binder_stop_on_user_error < 2);
2212 binder_set_nice(proc->default_priority);
2214 if (!binder_has_proc_work(proc, thread))
2217 ret = wait_event_interruptible_exclusive(proc->wait, binder_has_proc_work(proc, thread));
2220 if (!binder_has_thread_work(thread))
2223 ret = wait_event_interruptible(thread->wait, binder_has_thread_work(thread));
2225 mutex_lock(&binder_lock);
2226 if (wait_for_proc_work)
2227 proc->ready_threads--;
2228 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
2235 struct binder_transaction_data tr;
2236 struct binder_work *w;
2237 struct binder_transaction *t = NULL;
2239 if (!list_empty(&thread->todo))
2240 w = list_first_entry(&thread->todo, struct binder_work, entry);
2241 else if (!list_empty(&proc->todo) && wait_for_proc_work)
2242 w = list_first_entry(&proc->todo, struct binder_work, entry);
2244 if (ptr - buffer == 4 && !(thread->looper & BINDER_LOOPER_STATE_NEED_RETURN)) /* no data added */
2249 if (end - ptr < sizeof(tr) + 4)
2253 case BINDER_WORK_TRANSACTION: {
2254 t = container_of(w, struct binder_transaction, work);
2256 case BINDER_WORK_TRANSACTION_COMPLETE: {
2257 cmd = BR_TRANSACTION_COMPLETE;
2258 if (put_user(cmd, (uint32_t __user *)ptr))
2260 ptr += sizeof(uint32_t);
2262 binder_stat_br(proc, thread, cmd);
2263 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
2264 "binder: %d:%d BR_TRANSACTION_COMPLETE\n",
2265 proc->pid, thread->pid);
2267 list_del(&w->entry);
2269 binder_stats.obj_deleted[BINDER_STAT_TRANSACTION_COMPLETE]++;
2271 case BINDER_WORK_NODE: {
2272 struct binder_node *node = container_of(w, struct binder_node, work);
2273 uint32_t cmd = BR_NOOP;
2274 const char *cmd_name;
2275 int strong = node->internal_strong_refs || node->local_strong_refs;
2276 int weak = !hlist_empty(&node->refs) || node->local_weak_refs || strong;
2277 if (weak && !node->has_weak_ref) {
2279 cmd_name = "BR_INCREFS";
2280 node->has_weak_ref = 1;
2281 node->pending_weak_ref = 1;
2282 node->local_weak_refs++;
2283 } else if (strong && !node->has_strong_ref) {
2285 cmd_name = "BR_ACQUIRE";
2286 node->has_strong_ref = 1;
2287 node->pending_strong_ref = 1;
2288 node->local_strong_refs++;
2289 } else if (!strong && node->has_strong_ref) {
2291 cmd_name = "BR_RELEASE";
2292 node->has_strong_ref = 0;
2293 } else if (!weak && node->has_weak_ref) {
2295 cmd_name = "BR_DECREFS";
2296 node->has_weak_ref = 0;
2298 if (cmd != BR_NOOP) {
2299 if (put_user(cmd, (uint32_t __user *)ptr))
2301 ptr += sizeof(uint32_t);
2302 if (put_user(node->ptr, (void * __user *)ptr))
2304 ptr += sizeof(void *);
2305 if (put_user(node->cookie, (void * __user *)ptr))
2307 ptr += sizeof(void *);
2309 binder_stat_br(proc, thread, cmd);
2310 binder_debug(BINDER_DEBUG_USER_REFS,
2311 "binder: %d:%d %s %d u%p c%p\n",
2312 proc->pid, thread->pid, cmd_name, node->debug_id, node->ptr, node->cookie);
2314 list_del_init(&w->entry);
2315 if (!weak && !strong) {
2316 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
2317 "binder: %d:%d node %d u%p c%p deleted\n",
2318 proc->pid, thread->pid, node->debug_id,
2319 node->ptr, node->cookie);
2320 rb_erase(&node->rb_node, &proc->nodes);
2322 binder_stats.obj_deleted[BINDER_STAT_NODE]++;
2324 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
2325 "binder: %d:%d node %d u%p c%p state unchanged\n",
2326 proc->pid, thread->pid, node->debug_id, node->ptr,
2331 case BINDER_WORK_DEAD_BINDER:
2332 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2333 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2334 struct binder_ref_death *death;
2337 death = container_of(w, struct binder_ref_death, work);
2338 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
2339 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
2341 cmd = BR_DEAD_BINDER;
2342 if (put_user(cmd, (uint32_t __user *)ptr))
2344 ptr += sizeof(uint32_t);
2345 if (put_user(death->cookie, (void * __user *)ptr))
2347 ptr += sizeof(void *);
2348 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
2349 "binder: %d:%d %s %p\n",
2350 proc->pid, thread->pid,
2351 cmd == BR_DEAD_BINDER ?
2353 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
2356 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
2357 list_del(&w->entry);
2359 binder_stats.obj_deleted[BINDER_STAT_DEATH]++;
2361 list_move(&w->entry, &proc->delivered_death);
2362 if (cmd == BR_DEAD_BINDER)
2363 goto done; /* DEAD_BINDER notifications can cause transactions */
2370 BUG_ON(t->buffer == NULL);
2371 if (t->buffer->target_node) {
2372 struct binder_node *target_node = t->buffer->target_node;
2373 tr.target.ptr = target_node->ptr;
2374 tr.cookie = target_node->cookie;
2375 t->saved_priority = task_nice(current);
2376 if (t->priority < target_node->min_priority &&
2377 !(t->flags & TF_ONE_WAY))
2378 binder_set_nice(t->priority);
2379 else if (!(t->flags & TF_ONE_WAY) ||
2380 t->saved_priority > target_node->min_priority)
2381 binder_set_nice(target_node->min_priority);
2382 cmd = BR_TRANSACTION;
2384 tr.target.ptr = NULL;
2389 tr.flags = t->flags;
2390 tr.sender_euid = t->sender_euid;
2393 struct task_struct *sender = t->from->proc->tsk;
2394 tr.sender_pid = task_tgid_nr_ns(sender,
2395 current->nsproxy->pid_ns);
2400 tr.data_size = t->buffer->data_size;
2401 tr.offsets_size = t->buffer->offsets_size;
2402 tr.data.ptr.buffer = (void *)t->buffer->data +
2403 proc->user_buffer_offset;
2404 tr.data.ptr.offsets = tr.data.ptr.buffer +
2405 ALIGN(t->buffer->data_size,
2408 if (put_user(cmd, (uint32_t __user *)ptr))
2410 ptr += sizeof(uint32_t);
2411 if (copy_to_user(ptr, &tr, sizeof(tr)))
2415 binder_stat_br(proc, thread, cmd);
2416 binder_debug(BINDER_DEBUG_TRANSACTION,
2417 "binder: %d:%d %s %d %d:%d, cmd %d"
2418 "size %zd-%zd ptr %p-%p\n",
2419 proc->pid, thread->pid,
2420 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
2422 t->debug_id, t->from ? t->from->proc->pid : 0,
2423 t->from ? t->from->pid : 0, cmd,
2424 t->buffer->data_size, t->buffer->offsets_size,
2425 tr.data.ptr.buffer, tr.data.ptr.offsets);
2427 list_del(&t->work.entry);
2428 t->buffer->allow_user_free = 1;
2429 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
2430 t->to_parent = thread->transaction_stack;
2431 t->to_thread = thread;
2432 thread->transaction_stack = t;
2434 t->buffer->transaction = NULL;
2436 binder_stats.obj_deleted[BINDER_STAT_TRANSACTION]++;
2443 *consumed = ptr - buffer;
2444 if (proc->requested_threads + proc->ready_threads == 0 &&
2445 proc->requested_threads_started < proc->max_threads &&
2446 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2447 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
2448 /*spawn a new thread if we leave this out */) {
2449 proc->requested_threads++;
2450 binder_debug(BINDER_DEBUG_THREADS,
2451 "binder: %d:%d BR_SPAWN_LOOPER\n",
2452 proc->pid, thread->pid);
2453 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
2459 static void binder_release_work(struct list_head *list)
2461 struct binder_work *w;
2462 while (!list_empty(list)) {
2463 w = list_first_entry(list, struct binder_work, entry);
2464 list_del_init(&w->entry);
2466 case BINDER_WORK_TRANSACTION: {
2467 struct binder_transaction *t;
2469 t = container_of(w, struct binder_transaction, work);
2470 if (t->buffer->target_node && !(t->flags & TF_ONE_WAY))
2471 binder_send_failed_reply(t, BR_DEAD_REPLY);
2473 case BINDER_WORK_TRANSACTION_COMPLETE: {
2475 binder_stats.obj_deleted[BINDER_STAT_TRANSACTION_COMPLETE]++;
2484 static struct binder_thread *binder_get_thread(struct binder_proc *proc)
2486 struct binder_thread *thread = NULL;
2487 struct rb_node *parent = NULL;
2488 struct rb_node **p = &proc->threads.rb_node;
2492 thread = rb_entry(parent, struct binder_thread, rb_node);
2494 if (current->pid < thread->pid)
2496 else if (current->pid > thread->pid)
2497 p = &(*p)->rb_right;
2502 thread = kzalloc(sizeof(*thread), GFP_KERNEL);
2505 binder_stats.obj_created[BINDER_STAT_THREAD]++;
2506 thread->proc = proc;
2507 thread->pid = current->pid;
2508 init_waitqueue_head(&thread->wait);
2509 INIT_LIST_HEAD(&thread->todo);
2510 rb_link_node(&thread->rb_node, parent, p);
2511 rb_insert_color(&thread->rb_node, &proc->threads);
2512 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
2513 thread->return_error = BR_OK;
2514 thread->return_error2 = BR_OK;
2519 static int binder_free_thread(struct binder_proc *proc,
2520 struct binder_thread *thread)
2522 struct binder_transaction *t;
2523 struct binder_transaction *send_reply = NULL;
2524 int active_transactions = 0;
2526 rb_erase(&thread->rb_node, &proc->threads);
2527 t = thread->transaction_stack;
2528 if (t && t->to_thread == thread)
2531 active_transactions++;
2532 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2533 "binder: release %d:%d transaction %d "
2534 "%s, still active\n", proc->pid, thread->pid,
2536 (t->to_thread == thread) ? "in" : "out");
2538 if (t->to_thread == thread) {
2540 t->to_thread = NULL;
2542 t->buffer->transaction = NULL;
2546 } else if (t->from == thread) {
2553 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
2554 binder_release_work(&thread->todo);
2556 binder_stats.obj_deleted[BINDER_STAT_THREAD]++;
2557 return active_transactions;
2560 static unsigned int binder_poll(struct file *filp,
2561 struct poll_table_struct *wait)
2563 struct binder_proc *proc = filp->private_data;
2564 struct binder_thread *thread = NULL;
2565 int wait_for_proc_work;
2567 mutex_lock(&binder_lock);
2568 thread = binder_get_thread(proc);
2570 wait_for_proc_work = thread->transaction_stack == NULL &&
2571 list_empty(&thread->todo) && thread->return_error == BR_OK;
2572 mutex_unlock(&binder_lock);
2574 if (wait_for_proc_work) {
2575 if (binder_has_proc_work(proc, thread))
2577 poll_wait(filp, &proc->wait, wait);
2578 if (binder_has_proc_work(proc, thread))
2581 if (binder_has_thread_work(thread))
2583 poll_wait(filp, &thread->wait, wait);
2584 if (binder_has_thread_work(thread))
2590 static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2593 struct binder_proc *proc = filp->private_data;
2594 struct binder_thread *thread;
2595 unsigned int size = _IOC_SIZE(cmd);
2596 void __user *ubuf = (void __user *)arg;
2598 /*printk(KERN_INFO "binder_ioctl: %d:%d %x %lx\n", proc->pid, current->pid, cmd, arg);*/
2600 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2604 mutex_lock(&binder_lock);
2605 thread = binder_get_thread(proc);
2606 if (thread == NULL) {
2612 case BINDER_WRITE_READ: {
2613 struct binder_write_read bwr;
2614 if (size != sizeof(struct binder_write_read)) {
2618 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
2622 binder_debug(BINDER_DEBUG_READ_WRITE,
2623 "binder: %d:%d write %ld at %08lx, read %ld at %08lx\n",
2624 proc->pid, thread->pid, bwr.write_size, bwr.write_buffer,
2625 bwr.read_size, bwr.read_buffer);
2627 if (bwr.write_size > 0) {
2628 ret = binder_thread_write(proc, thread, (void __user *)bwr.write_buffer, bwr.write_size, &bwr.write_consumed);
2630 bwr.read_consumed = 0;
2631 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2636 if (bwr.read_size > 0) {
2637 ret = binder_thread_read(proc, thread, (void __user *)bwr.read_buffer, bwr.read_size, &bwr.read_consumed, filp->f_flags & O_NONBLOCK);
2638 if (!list_empty(&proc->todo))
2639 wake_up_interruptible(&proc->wait);
2641 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2646 binder_debug(BINDER_DEBUG_READ_WRITE,
2647 "binder: %d:%d wrote %ld of %ld, read return %ld of %ld\n",
2648 proc->pid, thread->pid, bwr.write_consumed, bwr.write_size,
2649 bwr.read_consumed, bwr.read_size);
2650 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
2656 case BINDER_SET_MAX_THREADS:
2657 if (copy_from_user(&proc->max_threads, ubuf, sizeof(proc->max_threads))) {
2662 case BINDER_SET_CONTEXT_MGR:
2663 if (binder_context_mgr_node != NULL) {
2664 printk(KERN_ERR "binder: BINDER_SET_CONTEXT_MGR already set\n");
2668 if (binder_context_mgr_uid != -1) {
2669 if (binder_context_mgr_uid != current->cred->euid) {
2670 printk(KERN_ERR "binder: BINDER_SET_"
2671 "CONTEXT_MGR bad uid %d != %d\n",
2672 current->cred->euid,
2673 binder_context_mgr_uid);
2678 binder_context_mgr_uid = current->cred->euid;
2679 binder_context_mgr_node = binder_new_node(proc, NULL, NULL);
2680 if (binder_context_mgr_node == NULL) {
2684 binder_context_mgr_node->local_weak_refs++;
2685 binder_context_mgr_node->local_strong_refs++;
2686 binder_context_mgr_node->has_strong_ref = 1;
2687 binder_context_mgr_node->has_weak_ref = 1;
2689 case BINDER_THREAD_EXIT:
2690 binder_debug(BINDER_DEBUG_THREADS, "binder: %d:%d exit\n",
2691 proc->pid, thread->pid);
2692 binder_free_thread(proc, thread);
2695 case BINDER_VERSION:
2696 if (size != sizeof(struct binder_version)) {
2700 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION, &((struct binder_version *)ubuf)->protocol_version)) {
2712 thread->looper &= ~BINDER_LOOPER_STATE_NEED_RETURN;
2713 mutex_unlock(&binder_lock);
2714 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2715 if (ret && ret != -ERESTARTSYS)
2716 printk(KERN_INFO "binder: %d:%d ioctl %x %lx returned %d\n", proc->pid, current->pid, cmd, arg, ret);
2720 static void binder_vma_open(struct vm_area_struct *vma)
2722 struct binder_proc *proc = vma->vm_private_data;
2723 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
2724 "binder: %d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
2725 proc->pid, vma->vm_start, vma->vm_end,
2726 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2727 (unsigned long)pgprot_val(vma->vm_page_prot));
2731 static void binder_vma_close(struct vm_area_struct *vma)
2733 struct binder_proc *proc = vma->vm_private_data;
2734 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
2735 "binder: %d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
2736 proc->pid, vma->vm_start, vma->vm_end,
2737 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2738 (unsigned long)pgprot_val(vma->vm_page_prot));
2740 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
2743 static struct vm_operations_struct binder_vm_ops = {
2744 .open = binder_vma_open,
2745 .close = binder_vma_close,
2748 static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
2751 struct vm_struct *area;
2752 struct binder_proc *proc = filp->private_data;
2753 const char *failure_string;
2754 struct binder_buffer *buffer;
2756 if ((vma->vm_end - vma->vm_start) > SZ_4M)
2757 vma->vm_end = vma->vm_start + SZ_4M;
2759 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
2760 "binder_mmap: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
2761 proc->pid, vma->vm_start, vma->vm_end,
2762 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2763 (unsigned long)pgprot_val(vma->vm_page_prot));
2765 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
2767 failure_string = "bad vm_flags";
2770 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
2774 failure_string = "already mapped";
2775 goto err_already_mapped;
2778 area = get_vm_area(vma->vm_end - vma->vm_start, VM_IOREMAP);
2781 failure_string = "get_vm_area";
2782 goto err_get_vm_area_failed;
2784 proc->buffer = area->addr;
2785 proc->user_buffer_offset = vma->vm_start - (uintptr_t)proc->buffer;
2787 #ifdef CONFIG_CPU_CACHE_VIPT
2788 if (cache_is_vipt_aliasing()) {
2789 while (CACHE_COLOUR((vma->vm_start ^ (uint32_t)proc->buffer))) {
2790 printk(KERN_INFO "binder_mmap: %d %lx-%lx maps %p bad alignment\n", proc->pid, vma->vm_start, vma->vm_end, proc->buffer);
2791 vma->vm_start += PAGE_SIZE;
2795 proc->pages = kzalloc(sizeof(proc->pages[0]) * ((vma->vm_end - vma->vm_start) / PAGE_SIZE), GFP_KERNEL);
2796 if (proc->pages == NULL) {
2798 failure_string = "alloc page array";
2799 goto err_alloc_pages_failed;
2801 proc->buffer_size = vma->vm_end - vma->vm_start;
2803 vma->vm_ops = &binder_vm_ops;
2804 vma->vm_private_data = proc;
2806 if (binder_update_page_range(proc, 1, proc->buffer, proc->buffer + PAGE_SIZE, vma)) {
2808 failure_string = "alloc small buf";
2809 goto err_alloc_small_buf_failed;
2811 buffer = proc->buffer;
2812 INIT_LIST_HEAD(&proc->buffers);
2813 list_add(&buffer->entry, &proc->buffers);
2815 binder_insert_free_buffer(proc, buffer);
2816 proc->free_async_space = proc->buffer_size / 2;
2818 proc->files = get_files_struct(current);
2821 /*printk(KERN_INFO "binder_mmap: %d %lx-%lx maps %p\n",
2822 proc->pid, vma->vm_start, vma->vm_end, proc->buffer);*/
2825 err_alloc_small_buf_failed:
2828 err_alloc_pages_failed:
2829 vfree(proc->buffer);
2830 proc->buffer = NULL;
2831 err_get_vm_area_failed:
2834 printk(KERN_ERR "binder_mmap: %d %lx-%lx %s failed %d\n",
2835 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
2839 static int binder_open(struct inode *nodp, struct file *filp)
2841 struct binder_proc *proc;
2843 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
2844 current->group_leader->pid, current->pid);
2846 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
2849 get_task_struct(current);
2850 proc->tsk = current;
2851 INIT_LIST_HEAD(&proc->todo);
2852 init_waitqueue_head(&proc->wait);
2853 proc->default_priority = task_nice(current);
2854 mutex_lock(&binder_lock);
2855 binder_stats.obj_created[BINDER_STAT_PROC]++;
2856 hlist_add_head(&proc->proc_node, &binder_procs);
2857 proc->pid = current->group_leader->pid;
2858 INIT_LIST_HEAD(&proc->delivered_death);
2859 filp->private_data = proc;
2860 mutex_unlock(&binder_lock);
2862 if (binder_proc_dir_entry_proc) {
2864 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
2865 remove_proc_entry(strbuf, binder_proc_dir_entry_proc);
2866 create_proc_read_entry(strbuf, S_IRUGO,
2867 binder_proc_dir_entry_proc,
2868 binder_read_proc_proc, proc);
2874 static int binder_flush(struct file *filp, fl_owner_t id)
2876 struct binder_proc *proc = filp->private_data;
2878 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
2883 static void binder_deferred_flush(struct binder_proc *proc)
2887 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
2888 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
2889 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
2890 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
2891 wake_up_interruptible(&thread->wait);
2895 wake_up_interruptible_all(&proc->wait);
2897 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
2898 "binder_flush: %d woke %d threads\n", proc->pid,
2902 static int binder_release(struct inode *nodp, struct file *filp)
2904 struct binder_proc *proc = filp->private_data;
2905 if (binder_proc_dir_entry_proc) {
2907 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
2908 remove_proc_entry(strbuf, binder_proc_dir_entry_proc);
2911 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
2916 static void binder_deferred_release(struct binder_proc *proc)
2918 struct hlist_node *pos;
2919 struct binder_transaction *t;
2921 int threads, nodes, incoming_refs, outgoing_refs, buffers, active_transactions, page_count;
2924 BUG_ON(proc->files);
2926 hlist_del(&proc->proc_node);
2927 if (binder_context_mgr_node && binder_context_mgr_node->proc == proc) {
2928 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2929 "binder_release: %d context_mgr_node gone\n",
2931 binder_context_mgr_node = NULL;
2935 active_transactions = 0;
2936 while ((n = rb_first(&proc->threads))) {
2937 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
2939 active_transactions += binder_free_thread(proc, thread);
2943 while ((n = rb_first(&proc->nodes))) {
2944 struct binder_node *node = rb_entry(n, struct binder_node, rb_node);
2947 rb_erase(&node->rb_node, &proc->nodes);
2948 list_del_init(&node->work.entry);
2949 if (hlist_empty(&node->refs)) {
2951 binder_stats.obj_deleted[BINDER_STAT_NODE]++;
2953 struct binder_ref *ref;
2957 node->local_strong_refs = 0;
2958 node->local_weak_refs = 0;
2959 hlist_add_head(&node->dead_node, &binder_dead_nodes);
2961 hlist_for_each_entry(ref, pos, &node->refs, node_entry) {
2965 if (list_empty(&ref->death->work.entry)) {
2966 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
2967 list_add_tail(&ref->death->work.entry, &ref->proc->todo);
2968 wake_up_interruptible(&ref->proc->wait);
2973 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2974 "binder: node %d now dead, "
2975 "refs %d, death %d\n", node->debug_id,
2976 incoming_refs, death);
2980 while ((n = rb_first(&proc->refs_by_desc))) {
2981 struct binder_ref *ref = rb_entry(n, struct binder_ref,
2984 binder_delete_ref(ref);
2986 binder_release_work(&proc->todo);
2989 while ((n = rb_first(&proc->allocated_buffers))) {
2990 struct binder_buffer *buffer = rb_entry(n, struct binder_buffer,
2992 t = buffer->transaction;
2995 buffer->transaction = NULL;
2996 printk(KERN_ERR "binder: release proc %d, "
2997 "transaction %d, not freed\n",
2998 proc->pid, t->debug_id);
3001 binder_free_buf(proc, buffer);
3005 binder_stats.obj_deleted[BINDER_STAT_PROC]++;
3010 for (i = 0; i < proc->buffer_size / PAGE_SIZE; i++) {
3011 if (proc->pages[i]) {
3012 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
3013 "binder_release: %d: "
3014 "page %d at %p not freed\n",
3016 proc->buffer + i * PAGE_SIZE);
3017 __free_page(proc->pages[i]);
3022 vfree(proc->buffer);
3025 put_task_struct(proc->tsk);
3027 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
3028 "binder_release: %d threads %d, nodes %d (ref %d), "
3029 "refs %d, active transactions %d, buffers %d, "
3031 proc->pid, threads, nodes, incoming_refs, outgoing_refs,
3032 active_transactions, buffers, page_count);
3037 static void binder_deferred_func(struct work_struct *work)
3039 struct binder_proc *proc;
3040 struct files_struct *files;
3044 mutex_lock(&binder_lock);
3045 mutex_lock(&binder_deferred_lock);
3046 if (!hlist_empty(&binder_deferred_list)) {
3047 proc = hlist_entry(binder_deferred_list.first,
3048 struct binder_proc, deferred_work_node);
3049 hlist_del_init(&proc->deferred_work_node);
3050 defer = proc->deferred_work;
3051 proc->deferred_work = 0;
3056 mutex_unlock(&binder_deferred_lock);
3059 if (defer & BINDER_DEFERRED_PUT_FILES) {
3060 files = proc->files;
3065 if (defer & BINDER_DEFERRED_FLUSH)
3066 binder_deferred_flush(proc);
3068 if (defer & BINDER_DEFERRED_RELEASE)
3069 binder_deferred_release(proc); /* frees proc */
3071 mutex_unlock(&binder_lock);
3073 put_files_struct(files);
3076 static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
3079 binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
3081 mutex_lock(&binder_deferred_lock);
3082 proc->deferred_work |= defer;
3083 if (hlist_unhashed(&proc->deferred_work_node)) {
3084 hlist_add_head(&proc->deferred_work_node,
3085 &binder_deferred_list);
3086 schedule_work(&binder_deferred_work);
3088 mutex_unlock(&binder_deferred_lock);
3091 static char *print_binder_transaction(char *buf, char *end, const char *prefix,
3092 struct binder_transaction *t)
3094 buf += snprintf(buf, end - buf,
3095 "%s %d: %p from %d:%d to %d:%d code %x "
3096 "flags %x pri %ld r%d",
3097 prefix, t->debug_id, t,
3098 t->from ? t->from->proc->pid : 0,
3099 t->from ? t->from->pid : 0,
3100 t->to_proc ? t->to_proc->pid : 0,
3101 t->to_thread ? t->to_thread->pid : 0,
3102 t->code, t->flags, t->priority, t->need_reply);
3105 if (t->buffer == NULL) {
3106 buf += snprintf(buf, end - buf, " buffer free\n");
3109 if (t->buffer->target_node) {
3110 buf += snprintf(buf, end - buf, " node %d",
3111 t->buffer->target_node->debug_id);
3115 buf += snprintf(buf, end - buf, " size %zd:%zd data %p\n",
3116 t->buffer->data_size, t->buffer->offsets_size,
3121 static char *print_binder_buffer(char *buf, char *end, const char *prefix,
3122 struct binder_buffer *buffer)
3124 buf += snprintf(buf, end - buf, "%s %d: %p size %zd:%zd %s\n",
3125 prefix, buffer->debug_id, buffer->data,
3126 buffer->data_size, buffer->offsets_size,
3127 buffer->transaction ? "active" : "delivered");
3131 static char *print_binder_work(char *buf, char *end, const char *prefix,
3132 const char *transaction_prefix,
3133 struct binder_work *w)
3135 struct binder_node *node;
3136 struct binder_transaction *t;
3139 case BINDER_WORK_TRANSACTION:
3140 t = container_of(w, struct binder_transaction, work);
3141 buf = print_binder_transaction(buf, end, transaction_prefix, t);
3143 case BINDER_WORK_TRANSACTION_COMPLETE:
3144 buf += snprintf(buf, end - buf,
3145 "%stransaction complete\n", prefix);
3147 case BINDER_WORK_NODE:
3148 node = container_of(w, struct binder_node, work);
3149 buf += snprintf(buf, end - buf, "%snode work %d: u%p c%p\n",
3150 prefix, node->debug_id, node->ptr,
3153 case BINDER_WORK_DEAD_BINDER:
3154 buf += snprintf(buf, end - buf, "%shas dead binder\n", prefix);
3156 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
3157 buf += snprintf(buf, end - buf,
3158 "%shas cleared dead binder\n", prefix);
3160 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
3161 buf += snprintf(buf, end - buf,
3162 "%shas cleared death notification\n", prefix);
3165 buf += snprintf(buf, end - buf, "%sunknown work: type %d\n",
3172 static char *print_binder_thread(char *buf, char *end,
3173 struct binder_thread *thread,
3176 struct binder_transaction *t;
3177 struct binder_work *w;
3178 char *start_buf = buf;
3181 buf += snprintf(buf, end - buf, " thread %d: l %02x\n",
3182 thread->pid, thread->looper);
3184 t = thread->transaction_stack;
3188 if (t->from == thread) {
3189 buf = print_binder_transaction(buf, end,
3190 " outgoing transaction", t);
3192 } else if (t->to_thread == thread) {
3193 buf = print_binder_transaction(buf, end,
3194 " incoming transaction", t);
3197 buf = print_binder_transaction(buf, end,
3198 " bad transaction", t);
3202 list_for_each_entry(w, &thread->todo, entry) {
3205 buf = print_binder_work(buf, end, " ",
3206 " pending transaction", w);
3208 if (!print_always && buf == header_buf)
3213 static char *print_binder_node(char *buf, char *end, struct binder_node *node)
3215 struct binder_ref *ref;
3216 struct hlist_node *pos;
3217 struct binder_work *w;
3221 hlist_for_each_entry(ref, pos, &node->refs, node_entry)
3224 buf += snprintf(buf, end - buf,
3225 " node %d: u%p c%p hs %d hw %d ls %d lw %d "
3227 node->debug_id, node->ptr, node->cookie,
3228 node->has_strong_ref, node->has_weak_ref,
3229 node->local_strong_refs, node->local_weak_refs,
3230 node->internal_strong_refs, count);
3234 buf += snprintf(buf, end - buf, " proc");
3237 hlist_for_each_entry(ref, pos, &node->refs, node_entry) {
3238 buf += snprintf(buf, end - buf, " %d", ref->proc->pid);
3243 buf += snprintf(buf, end - buf, "\n");
3244 list_for_each_entry(w, &node->async_todo, entry) {
3247 buf = print_binder_work(buf, end, " ",
3248 " pending async transaction", w);
3253 static char *print_binder_ref(char *buf, char *end, struct binder_ref *ref)
3255 buf += snprintf(buf, end - buf,
3256 " ref %d: desc %d %snode %d s %d w %d d %p\n",
3257 ref->debug_id, ref->desc,
3258 ref->node->proc ? "" : "dead ", ref->node->debug_id,
3259 ref->strong, ref->weak, ref->death);
3263 static char *print_binder_proc(char *buf, char *end,
3264 struct binder_proc *proc, int print_all)
3266 struct binder_work *w;
3268 char *start_buf = buf;
3271 buf += snprintf(buf, end - buf, "proc %d\n", proc->pid);
3274 for (n = rb_first(&proc->threads);
3275 n != NULL && buf < end;
3277 buf = print_binder_thread(buf, end,
3278 rb_entry(n, struct binder_thread,
3279 rb_node), print_all);
3280 for (n = rb_first(&proc->nodes);
3281 n != NULL && buf < end;
3283 struct binder_node *node = rb_entry(n, struct binder_node,
3285 if (print_all || node->has_async_transaction)
3286 buf = print_binder_node(buf, end, node);
3289 for (n = rb_first(&proc->refs_by_desc);
3290 n != NULL && buf < end;
3292 buf = print_binder_ref(buf, end,
3293 rb_entry(n, struct binder_ref,
3296 for (n = rb_first(&proc->allocated_buffers);
3297 n != NULL && buf < end;
3299 buf = print_binder_buffer(buf, end, " buffer",
3300 rb_entry(n, struct binder_buffer,
3302 list_for_each_entry(w, &proc->todo, entry) {
3305 buf = print_binder_work(buf, end, " ",
3306 " pending transaction", w);
3308 list_for_each_entry(w, &proc->delivered_death, entry) {
3311 buf += snprintf(buf, end - buf,
3312 " has delivered dead binder\n");
3315 if (!print_all && buf == header_buf)
3320 static const char *binder_return_strings[] = {
3325 "BR_ACQUIRE_RESULT",
3327 "BR_TRANSACTION_COMPLETE",
3332 "BR_ATTEMPT_ACQUIRE",
3337 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
3341 static const char *binder_command_strings[] = {
3344 "BC_ACQUIRE_RESULT",
3352 "BC_ATTEMPT_ACQUIRE",
3353 "BC_REGISTER_LOOPER",
3356 "BC_REQUEST_DEATH_NOTIFICATION",
3357 "BC_CLEAR_DEATH_NOTIFICATION",
3358 "BC_DEAD_BINDER_DONE"
3361 static const char *binder_objstat_strings[] = {
3368 "transaction_complete"
3371 static char *print_binder_stats(char *buf, char *end, const char *prefix,
3372 struct binder_stats *stats)
3376 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
3377 ARRAY_SIZE(binder_command_strings));
3378 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
3380 buf += snprintf(buf, end - buf, "%s%s: %d\n", prefix,
3381 binder_command_strings[i],
3387 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
3388 ARRAY_SIZE(binder_return_strings));
3389 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
3391 buf += snprintf(buf, end - buf, "%s%s: %d\n", prefix,
3392 binder_return_strings[i], stats->br[i]);
3397 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
3398 ARRAY_SIZE(binder_objstat_strings));
3399 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
3400 ARRAY_SIZE(stats->obj_deleted));
3401 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
3402 if (stats->obj_created[i] || stats->obj_deleted[i])
3403 buf += snprintf(buf, end - buf,
3404 "%s%s: active %d total %d\n", prefix,
3405 binder_objstat_strings[i],
3406 stats->obj_created[i] -
3407 stats->obj_deleted[i],
3408 stats->obj_created[i]);
3415 static char *print_binder_proc_stats(char *buf, char *end,
3416 struct binder_proc *proc)
3418 struct binder_work *w;
3420 int count, strong, weak;
3422 buf += snprintf(buf, end - buf, "proc %d\n", proc->pid);
3426 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3428 buf += snprintf(buf, end - buf, " threads: %d\n", count);
3431 buf += snprintf(buf, end - buf, " requested threads: %d+%d/%d\n"
3432 " ready threads %d\n"
3433 " free async space %zd\n", proc->requested_threads,
3434 proc->requested_threads_started, proc->max_threads,
3435 proc->ready_threads, proc->free_async_space);
3439 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
3441 buf += snprintf(buf, end - buf, " nodes: %d\n", count);
3447 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
3448 struct binder_ref *ref = rb_entry(n, struct binder_ref,
3451 strong += ref->strong;
3454 buf += snprintf(buf, end - buf, " refs: %d s %d w %d\n",
3455 count, strong, weak);
3460 for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
3462 buf += snprintf(buf, end - buf, " buffers: %d\n", count);
3467 list_for_each_entry(w, &proc->todo, entry) {
3469 case BINDER_WORK_TRANSACTION:
3476 buf += snprintf(buf, end - buf, " pending transactions: %d\n", count);
3480 buf = print_binder_stats(buf, end, " ", &proc->stats);
3486 static int binder_read_proc_state(char *page, char **start, off_t off,
3487 int count, int *eof, void *data)
3489 struct binder_proc *proc;
3490 struct hlist_node *pos;
3491 struct binder_node *node;
3494 char *end = page + PAGE_SIZE;
3495 int do_lock = !binder_debug_no_lock;
3501 mutex_lock(&binder_lock);
3503 buf += snprintf(buf, end - buf, "binder state:\n");
3505 if (!hlist_empty(&binder_dead_nodes))
3506 buf += snprintf(buf, end - buf, "dead nodes:\n");
3507 hlist_for_each_entry(node, pos, &binder_dead_nodes, dead_node) {
3510 buf = print_binder_node(buf, end, node);
3513 hlist_for_each_entry(proc, pos, &binder_procs, proc_node) {
3516 buf = print_binder_proc(buf, end, proc, 1);
3519 mutex_unlock(&binder_lock);
3520 if (buf > page + PAGE_SIZE)
3521 buf = page + PAGE_SIZE;
3523 *start = page + off;
3531 return len < count ? len : count;
3534 static int binder_read_proc_stats(char *page, char **start, off_t off,
3535 int count, int *eof, void *data)
3537 struct binder_proc *proc;
3538 struct hlist_node *pos;
3541 int do_lock = !binder_debug_no_lock;
3547 mutex_lock(&binder_lock);
3549 p += snprintf(p, PAGE_SIZE, "binder stats:\n");
3551 p = print_binder_stats(p, page + PAGE_SIZE, "", &binder_stats);
3553 hlist_for_each_entry(proc, pos, &binder_procs, proc_node) {
3554 if (p >= page + PAGE_SIZE)
3556 p = print_binder_proc_stats(p, page + PAGE_SIZE, proc);
3559 mutex_unlock(&binder_lock);
3560 if (p > page + PAGE_SIZE)
3561 p = page + PAGE_SIZE;
3563 *start = page + off;
3571 return len < count ? len : count;
3574 static int binder_read_proc_transactions(char *page, char **start, off_t off,
3575 int count, int *eof, void *data)
3577 struct binder_proc *proc;
3578 struct hlist_node *pos;
3581 char *end = page + PAGE_SIZE;
3582 int do_lock = !binder_debug_no_lock;
3588 mutex_lock(&binder_lock);
3590 buf += snprintf(buf, end - buf, "binder transactions:\n");
3591 hlist_for_each_entry(proc, pos, &binder_procs, proc_node) {
3594 buf = print_binder_proc(buf, end, proc, 0);
3597 mutex_unlock(&binder_lock);
3598 if (buf > page + PAGE_SIZE)
3599 buf = page + PAGE_SIZE;
3601 *start = page + off;
3609 return len < count ? len : count;
3612 static int binder_read_proc_proc(char *page, char **start, off_t off,
3613 int count, int *eof, void *data)
3615 struct binder_proc *proc = data;
3618 int do_lock = !binder_debug_no_lock;
3624 mutex_lock(&binder_lock);
3625 p += snprintf(p, PAGE_SIZE, "binder proc state:\n");
3626 p = print_binder_proc(p, page + PAGE_SIZE, proc, 1);
3628 mutex_unlock(&binder_lock);
3630 if (p > page + PAGE_SIZE)
3631 p = page + PAGE_SIZE;
3632 *start = page + off;
3640 return len < count ? len : count;
3643 static char *print_binder_transaction_log_entry(char *buf, char *end,
3644 struct binder_transaction_log_entry *e)
3646 buf += snprintf(buf, end - buf,
3647 "%d: %s from %d:%d to %d:%d node %d handle %d "
3649 e->debug_id, (e->call_type == 2) ? "reply" :
3650 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
3651 e->from_thread, e->to_proc, e->to_thread, e->to_node,
3652 e->target_handle, e->data_size, e->offsets_size);
3656 static int binder_read_proc_transaction_log(
3657 char *page, char **start, off_t off, int count, int *eof, void *data)
3659 struct binder_transaction_log *log = data;
3663 char *end = page + PAGE_SIZE;
3669 for (i = log->next; i < ARRAY_SIZE(log->entry); i++) {
3672 buf = print_binder_transaction_log_entry(buf, end,
3676 for (i = 0; i < log->next; i++) {
3679 buf = print_binder_transaction_log_entry(buf, end,
3683 *start = page + off;
3691 return len < count ? len : count;
3694 static const struct file_operations binder_fops = {
3695 .owner = THIS_MODULE,
3696 .poll = binder_poll,
3697 .unlocked_ioctl = binder_ioctl,
3698 .mmap = binder_mmap,
3699 .open = binder_open,
3700 .flush = binder_flush,
3701 .release = binder_release,
3704 static struct miscdevice binder_miscdev = {
3705 .minor = MISC_DYNAMIC_MINOR,
3707 .fops = &binder_fops
3710 static int __init binder_init(void)
3714 binder_proc_dir_entry_root = proc_mkdir("binder", NULL);
3715 if (binder_proc_dir_entry_root)
3716 binder_proc_dir_entry_proc = proc_mkdir("proc",
3717 binder_proc_dir_entry_root);
3718 ret = misc_register(&binder_miscdev);
3719 if (binder_proc_dir_entry_root) {
3720 create_proc_read_entry("state",
3722 binder_proc_dir_entry_root,
3723 binder_read_proc_state,
3725 create_proc_read_entry("stats",
3727 binder_proc_dir_entry_root,
3728 binder_read_proc_stats,
3730 create_proc_read_entry("transactions",
3732 binder_proc_dir_entry_root,
3733 binder_read_proc_transactions,
3735 create_proc_read_entry("transaction_log",
3737 binder_proc_dir_entry_root,
3738 binder_read_proc_transaction_log,
3739 &binder_transaction_log);
3740 create_proc_read_entry("failed_transaction_log",
3742 binder_proc_dir_entry_root,
3743 binder_read_proc_transaction_log,
3744 &binder_transaction_log_failed);
3749 device_initcall(binder_init);
3751 MODULE_LICENSE("GPL v2");