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>
36 #include <linux/pid_namespace.h>
40 static DEFINE_MUTEX(binder_lock);
41 static DEFINE_MUTEX(binder_deferred_lock);
42 static DEFINE_MUTEX(binder_mmap_lock);
44 static HLIST_HEAD(binder_procs);
45 static HLIST_HEAD(binder_deferred_list);
46 static HLIST_HEAD(binder_dead_nodes);
48 static struct dentry *binder_debugfs_dir_entry_root;
49 static struct dentry *binder_debugfs_dir_entry_proc;
50 static struct binder_node *binder_context_mgr_node;
51 static kuid_t binder_context_mgr_uid = INVALID_UID;
52 static int binder_last_id;
53 static struct workqueue_struct *binder_deferred_workqueue;
55 #define BINDER_DEBUG_ENTRY(name) \
56 static int binder_##name##_open(struct inode *inode, struct file *file) \
58 return single_open(file, binder_##name##_show, inode->i_private); \
61 static const struct file_operations binder_##name##_fops = { \
62 .owner = THIS_MODULE, \
63 .open = binder_##name##_open, \
65 .llseek = seq_lseek, \
66 .release = single_release, \
69 static int binder_proc_show(struct seq_file *m, void *unused);
70 BINDER_DEBUG_ENTRY(proc);
72 /* This is only defined in include/asm-arm/sizes.h */
78 #define SZ_4M 0x400000
81 #define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
83 #define BINDER_SMALL_BUF_SIZE (PAGE_SIZE * 64)
86 BINDER_DEBUG_USER_ERROR = 1U << 0,
87 BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1,
88 BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2,
89 BINDER_DEBUG_OPEN_CLOSE = 1U << 3,
90 BINDER_DEBUG_DEAD_BINDER = 1U << 4,
91 BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5,
92 BINDER_DEBUG_READ_WRITE = 1U << 6,
93 BINDER_DEBUG_USER_REFS = 1U << 7,
94 BINDER_DEBUG_THREADS = 1U << 8,
95 BINDER_DEBUG_TRANSACTION = 1U << 9,
96 BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10,
97 BINDER_DEBUG_FREE_BUFFER = 1U << 11,
98 BINDER_DEBUG_INTERNAL_REFS = 1U << 12,
99 BINDER_DEBUG_BUFFER_ALLOC = 1U << 13,
100 BINDER_DEBUG_PRIORITY_CAP = 1U << 14,
101 BINDER_DEBUG_BUFFER_ALLOC_ASYNC = 1U << 15,
103 static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR |
104 BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION;
105 module_param_named(debug_mask, binder_debug_mask, uint, S_IWUSR | S_IRUGO);
107 static bool binder_debug_no_lock;
108 module_param_named(proc_no_lock, binder_debug_no_lock, bool, S_IWUSR | S_IRUGO);
110 static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
111 static int binder_stop_on_user_error;
113 static int binder_set_stop_on_user_error(const char *val,
114 struct kernel_param *kp)
117 ret = param_set_int(val, kp);
118 if (binder_stop_on_user_error < 2)
119 wake_up(&binder_user_error_wait);
122 module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
123 param_get_int, &binder_stop_on_user_error, S_IWUSR | S_IRUGO);
125 #define binder_debug(mask, x...) \
127 if (binder_debug_mask & mask) \
131 #define binder_user_error(x...) \
133 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
135 if (binder_stop_on_user_error) \
136 binder_stop_on_user_error = 2; \
139 enum binder_stat_types {
145 BINDER_STAT_TRANSACTION,
146 BINDER_STAT_TRANSACTION_COMPLETE,
150 struct binder_stats {
151 int br[_IOC_NR(BR_FAILED_REPLY) + 1];
152 int bc[_IOC_NR(BC_DEAD_BINDER_DONE) + 1];
153 int obj_created[BINDER_STAT_COUNT];
154 int obj_deleted[BINDER_STAT_COUNT];
157 static struct binder_stats binder_stats;
159 static inline void binder_stats_deleted(enum binder_stat_types type)
161 binder_stats.obj_deleted[type]++;
164 static inline void binder_stats_created(enum binder_stat_types type)
166 binder_stats.obj_created[type]++;
169 struct binder_transaction_log_entry {
181 struct binder_transaction_log {
184 struct binder_transaction_log_entry entry[32];
186 static struct binder_transaction_log binder_transaction_log;
187 static struct binder_transaction_log binder_transaction_log_failed;
189 static struct binder_transaction_log_entry *binder_transaction_log_add(
190 struct binder_transaction_log *log)
192 struct binder_transaction_log_entry *e;
193 e = &log->entry[log->next];
194 memset(e, 0, sizeof(*e));
196 if (log->next == ARRAY_SIZE(log->entry)) {
204 struct list_head entry;
206 BINDER_WORK_TRANSACTION = 1,
207 BINDER_WORK_TRANSACTION_COMPLETE,
209 BINDER_WORK_DEAD_BINDER,
210 BINDER_WORK_DEAD_BINDER_AND_CLEAR,
211 BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
217 struct binder_work work;
219 struct rb_node rb_node;
220 struct hlist_node dead_node;
222 struct binder_proc *proc;
223 struct hlist_head refs;
224 int internal_strong_refs;
226 int local_strong_refs;
229 unsigned has_strong_ref:1;
230 unsigned pending_strong_ref:1;
231 unsigned has_weak_ref:1;
232 unsigned pending_weak_ref:1;
233 unsigned has_async_transaction:1;
234 unsigned accept_fds:1;
235 unsigned min_priority:8;
236 struct list_head async_todo;
239 struct binder_ref_death {
240 struct binder_work work;
245 /* Lookups needed: */
246 /* node + proc => ref (transaction) */
247 /* desc + proc => ref (transaction, inc/dec ref) */
248 /* node => refs + procs (proc exit) */
250 struct rb_node rb_node_desc;
251 struct rb_node rb_node_node;
252 struct hlist_node node_entry;
253 struct binder_proc *proc;
254 struct binder_node *node;
258 struct binder_ref_death *death;
261 struct binder_buffer {
262 struct list_head entry; /* free and allocated entries by address */
263 struct rb_node rb_node; /* free entry by size or allocated entry */
266 unsigned allow_user_free:1;
267 unsigned async_transaction:1;
268 unsigned debug_id:29;
270 struct binder_transaction *transaction;
272 struct binder_node *target_node;
278 enum binder_deferred_state {
279 BINDER_DEFERRED_PUT_FILES = 0x01,
280 BINDER_DEFERRED_FLUSH = 0x02,
281 BINDER_DEFERRED_RELEASE = 0x04,
285 struct hlist_node proc_node;
286 struct rb_root threads;
287 struct rb_root nodes;
288 struct rb_root refs_by_desc;
289 struct rb_root refs_by_node;
291 struct vm_area_struct *vma;
292 struct mm_struct *vma_vm_mm;
293 struct task_struct *tsk;
294 struct files_struct *files;
295 struct hlist_node deferred_work_node;
298 ptrdiff_t user_buffer_offset;
300 struct list_head buffers;
301 struct rb_root free_buffers;
302 struct rb_root allocated_buffers;
303 size_t free_async_space;
307 uint32_t buffer_free;
308 struct list_head todo;
309 wait_queue_head_t wait;
310 struct binder_stats stats;
311 struct list_head delivered_death;
313 int requested_threads;
314 int requested_threads_started;
316 long default_priority;
317 struct dentry *debugfs_entry;
321 BINDER_LOOPER_STATE_REGISTERED = 0x01,
322 BINDER_LOOPER_STATE_ENTERED = 0x02,
323 BINDER_LOOPER_STATE_EXITED = 0x04,
324 BINDER_LOOPER_STATE_INVALID = 0x08,
325 BINDER_LOOPER_STATE_WAITING = 0x10,
326 BINDER_LOOPER_STATE_NEED_RETURN = 0x20
329 struct binder_thread {
330 struct binder_proc *proc;
331 struct rb_node rb_node;
334 struct binder_transaction *transaction_stack;
335 struct list_head todo;
336 uint32_t return_error; /* Write failed, return error code in read buf */
337 uint32_t return_error2; /* Write failed, return error code in read */
338 /* buffer. Used when sending a reply to a dead process that */
339 /* we are also waiting on */
340 wait_queue_head_t wait;
341 struct binder_stats stats;
344 struct binder_transaction {
346 struct binder_work work;
347 struct binder_thread *from;
348 struct binder_transaction *from_parent;
349 struct binder_proc *to_proc;
350 struct binder_thread *to_thread;
351 struct binder_transaction *to_parent;
352 unsigned need_reply:1;
353 /* unsigned is_dead:1; */ /* not used at the moment */
355 struct binder_buffer *buffer;
364 binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
366 static int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
368 struct files_struct *files = proc->files;
369 unsigned long rlim_cur;
375 if (!lock_task_sighand(proc->tsk, &irqs))
378 rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
379 unlock_task_sighand(proc->tsk, &irqs);
381 return __alloc_fd(files, 0, rlim_cur, flags);
385 * copied from fd_install
387 static void task_fd_install(
388 struct binder_proc *proc, unsigned int fd, struct file *file)
391 __fd_install(proc->files, fd, file);
395 * copied from sys_close
397 static long task_close_fd(struct binder_proc *proc, unsigned int fd)
401 if (proc->files == NULL)
404 retval = __close_fd(proc->files, fd);
405 /* can't restart close syscall because file table entry was cleared */
406 if (unlikely(retval == -ERESTARTSYS ||
407 retval == -ERESTARTNOINTR ||
408 retval == -ERESTARTNOHAND ||
409 retval == -ERESTART_RESTARTBLOCK))
415 static void binder_set_nice(long nice)
418 if (can_nice(current, nice)) {
419 set_user_nice(current, nice);
422 min_nice = 20 - current->signal->rlim[RLIMIT_NICE].rlim_cur;
423 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
424 "binder: %d: nice value %ld not allowed use "
425 "%ld instead\n", current->pid, nice, min_nice);
426 set_user_nice(current, min_nice);
429 binder_user_error("binder: %d RLIMIT_NICE not set\n", current->pid);
432 static size_t binder_buffer_size(struct binder_proc *proc,
433 struct binder_buffer *buffer)
435 if (list_is_last(&buffer->entry, &proc->buffers))
436 return proc->buffer + proc->buffer_size - (void *)buffer->data;
438 return (size_t)list_entry(buffer->entry.next,
439 struct binder_buffer, entry) - (size_t)buffer->data;
442 static void binder_insert_free_buffer(struct binder_proc *proc,
443 struct binder_buffer *new_buffer)
445 struct rb_node **p = &proc->free_buffers.rb_node;
446 struct rb_node *parent = NULL;
447 struct binder_buffer *buffer;
449 size_t new_buffer_size;
451 BUG_ON(!new_buffer->free);
453 new_buffer_size = binder_buffer_size(proc, new_buffer);
455 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
456 "binder: %d: add free buffer, size %zd, "
457 "at %p\n", proc->pid, new_buffer_size, new_buffer);
461 buffer = rb_entry(parent, struct binder_buffer, rb_node);
462 BUG_ON(!buffer->free);
464 buffer_size = binder_buffer_size(proc, buffer);
466 if (new_buffer_size < buffer_size)
467 p = &parent->rb_left;
469 p = &parent->rb_right;
471 rb_link_node(&new_buffer->rb_node, parent, p);
472 rb_insert_color(&new_buffer->rb_node, &proc->free_buffers);
475 static void binder_insert_allocated_buffer(struct binder_proc *proc,
476 struct binder_buffer *new_buffer)
478 struct rb_node **p = &proc->allocated_buffers.rb_node;
479 struct rb_node *parent = NULL;
480 struct binder_buffer *buffer;
482 BUG_ON(new_buffer->free);
486 buffer = rb_entry(parent, struct binder_buffer, rb_node);
487 BUG_ON(buffer->free);
489 if (new_buffer < buffer)
490 p = &parent->rb_left;
491 else if (new_buffer > buffer)
492 p = &parent->rb_right;
496 rb_link_node(&new_buffer->rb_node, parent, p);
497 rb_insert_color(&new_buffer->rb_node, &proc->allocated_buffers);
500 static struct binder_buffer *binder_buffer_lookup(struct binder_proc *proc,
501 void __user *user_ptr)
503 struct rb_node *n = proc->allocated_buffers.rb_node;
504 struct binder_buffer *buffer;
505 struct binder_buffer *kern_ptr;
507 kern_ptr = user_ptr - proc->user_buffer_offset
508 - offsetof(struct binder_buffer, data);
511 buffer = rb_entry(n, struct binder_buffer, rb_node);
512 BUG_ON(buffer->free);
514 if (kern_ptr < buffer)
516 else if (kern_ptr > buffer)
524 static int binder_update_page_range(struct binder_proc *proc, int allocate,
525 void *start, void *end,
526 struct vm_area_struct *vma)
529 unsigned long user_page_addr;
530 struct vm_struct tmp_area;
532 struct mm_struct *mm;
534 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
535 "binder: %d: %s pages %p-%p\n", proc->pid,
536 allocate ? "allocate" : "free", start, end);
544 mm = get_task_mm(proc->tsk);
547 down_write(&mm->mmap_sem);
549 if (vma && mm != proc->vma_vm_mm) {
550 pr_err("binder: %d: vma mm and task mm mismatch\n",
560 pr_err("binder: %d: binder_alloc_buf failed to "
561 "map pages in userspace, no vma\n", proc->pid);
565 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
567 struct page **page_array_ptr;
568 page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
571 *page = alloc_page(GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
573 pr_err("binder: %d: binder_alloc_buf failed "
574 "for page at %p\n", proc->pid, page_addr);
575 goto err_alloc_page_failed;
577 tmp_area.addr = page_addr;
578 tmp_area.size = PAGE_SIZE + PAGE_SIZE /* guard page? */;
579 page_array_ptr = page;
580 ret = map_vm_area(&tmp_area, PAGE_KERNEL, &page_array_ptr);
582 pr_err("binder: %d: binder_alloc_buf failed "
583 "to map page at %p in kernel\n",
584 proc->pid, page_addr);
585 goto err_map_kernel_failed;
588 (uintptr_t)page_addr + proc->user_buffer_offset;
589 ret = vm_insert_page(vma, user_page_addr, page[0]);
591 pr_err("binder: %d: binder_alloc_buf failed "
592 "to map page at %lx in userspace\n",
593 proc->pid, user_page_addr);
594 goto err_vm_insert_page_failed;
596 /* vm_insert_page does not seem to increment the refcount */
599 up_write(&mm->mmap_sem);
605 for (page_addr = end - PAGE_SIZE; page_addr >= start;
606 page_addr -= PAGE_SIZE) {
607 page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
609 zap_page_range(vma, (uintptr_t)page_addr +
610 proc->user_buffer_offset, PAGE_SIZE, NULL);
611 err_vm_insert_page_failed:
612 unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
613 err_map_kernel_failed:
616 err_alloc_page_failed:
621 up_write(&mm->mmap_sem);
627 static struct binder_buffer *binder_alloc_buf(struct binder_proc *proc,
629 size_t offsets_size, int is_async)
631 struct rb_node *n = proc->free_buffers.rb_node;
632 struct binder_buffer *buffer;
634 struct rb_node *best_fit = NULL;
639 if (proc->vma == NULL) {
640 pr_err("binder: %d: binder_alloc_buf, no vma\n",
645 size = ALIGN(data_size, sizeof(void *)) +
646 ALIGN(offsets_size, sizeof(void *));
648 if (size < data_size || size < offsets_size) {
649 binder_user_error("binder: %d: got transaction with invalid "
650 "size %zd-%zd\n", proc->pid, data_size, offsets_size);
655 proc->free_async_space < size + sizeof(struct binder_buffer)) {
656 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
657 "binder: %d: binder_alloc_buf size %zd"
658 "failed, no async space left\n", proc->pid, size);
663 buffer = rb_entry(n, struct binder_buffer, rb_node);
664 BUG_ON(!buffer->free);
665 buffer_size = binder_buffer_size(proc, buffer);
667 if (size < buffer_size) {
670 } else if (size > buffer_size)
677 if (best_fit == NULL) {
678 pr_err("binder: %d: binder_alloc_buf size %zd failed, "
679 "no address space\n", proc->pid, size);
683 buffer = rb_entry(best_fit, struct binder_buffer, rb_node);
684 buffer_size = binder_buffer_size(proc, buffer);
687 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
688 "binder: %d: binder_alloc_buf size %zd got buff"
689 "er %p size %zd\n", proc->pid, size, buffer, buffer_size);
692 (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK);
694 if (size + sizeof(struct binder_buffer) + 4 >= buffer_size)
695 buffer_size = size; /* no room for other buffers */
697 buffer_size = size + sizeof(struct binder_buffer);
700 (void *)PAGE_ALIGN((uintptr_t)buffer->data + buffer_size);
701 if (end_page_addr > has_page_addr)
702 end_page_addr = has_page_addr;
703 if (binder_update_page_range(proc, 1,
704 (void *)PAGE_ALIGN((uintptr_t)buffer->data), end_page_addr, NULL))
707 rb_erase(best_fit, &proc->free_buffers);
709 binder_insert_allocated_buffer(proc, buffer);
710 if (buffer_size != size) {
711 struct binder_buffer *new_buffer = (void *)buffer->data + size;
712 list_add(&new_buffer->entry, &buffer->entry);
713 new_buffer->free = 1;
714 binder_insert_free_buffer(proc, new_buffer);
716 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
717 "binder: %d: binder_alloc_buf size %zd got "
718 "%p\n", proc->pid, size, buffer);
719 buffer->data_size = data_size;
720 buffer->offsets_size = offsets_size;
721 buffer->async_transaction = is_async;
723 proc->free_async_space -= size + sizeof(struct binder_buffer);
724 binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
725 "binder: %d: binder_alloc_buf size %zd "
726 "async free %zd\n", proc->pid, size,
727 proc->free_async_space);
733 static void *buffer_start_page(struct binder_buffer *buffer)
735 return (void *)((uintptr_t)buffer & PAGE_MASK);
738 static void *buffer_end_page(struct binder_buffer *buffer)
740 return (void *)(((uintptr_t)(buffer + 1) - 1) & PAGE_MASK);
743 static void binder_delete_free_buffer(struct binder_proc *proc,
744 struct binder_buffer *buffer)
746 struct binder_buffer *prev, *next = NULL;
747 int free_page_end = 1;
748 int free_page_start = 1;
750 BUG_ON(proc->buffers.next == &buffer->entry);
751 prev = list_entry(buffer->entry.prev, struct binder_buffer, entry);
753 if (buffer_end_page(prev) == buffer_start_page(buffer)) {
755 if (buffer_end_page(prev) == buffer_end_page(buffer))
757 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
758 "binder: %d: merge free, buffer %p "
759 "share page with %p\n", proc->pid, buffer, prev);
762 if (!list_is_last(&buffer->entry, &proc->buffers)) {
763 next = list_entry(buffer->entry.next,
764 struct binder_buffer, entry);
765 if (buffer_start_page(next) == buffer_end_page(buffer)) {
767 if (buffer_start_page(next) ==
768 buffer_start_page(buffer))
770 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
771 "binder: %d: merge free, buffer"
772 " %p share page with %p\n", proc->pid,
776 list_del(&buffer->entry);
777 if (free_page_start || free_page_end) {
778 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
779 "binder: %d: merge free, buffer %p do "
780 "not share page%s%s with with %p or %p\n",
781 proc->pid, buffer, free_page_start ? "" : " end",
782 free_page_end ? "" : " start", prev, next);
783 binder_update_page_range(proc, 0, free_page_start ?
784 buffer_start_page(buffer) : buffer_end_page(buffer),
785 (free_page_end ? buffer_end_page(buffer) :
786 buffer_start_page(buffer)) + PAGE_SIZE, NULL);
790 static void binder_free_buf(struct binder_proc *proc,
791 struct binder_buffer *buffer)
793 size_t size, buffer_size;
795 buffer_size = binder_buffer_size(proc, buffer);
797 size = ALIGN(buffer->data_size, sizeof(void *)) +
798 ALIGN(buffer->offsets_size, sizeof(void *));
800 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
801 "binder: %d: binder_free_buf %p size %zd buffer"
802 "_size %zd\n", proc->pid, buffer, size, buffer_size);
804 BUG_ON(buffer->free);
805 BUG_ON(size > buffer_size);
806 BUG_ON(buffer->transaction != NULL);
807 BUG_ON((void *)buffer < proc->buffer);
808 BUG_ON((void *)buffer > proc->buffer + proc->buffer_size);
810 if (buffer->async_transaction) {
811 proc->free_async_space += size + sizeof(struct binder_buffer);
813 binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
814 "binder: %d: binder_free_buf size %zd "
815 "async free %zd\n", proc->pid, size,
816 proc->free_async_space);
819 binder_update_page_range(proc, 0,
820 (void *)PAGE_ALIGN((uintptr_t)buffer->data),
821 (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK),
823 rb_erase(&buffer->rb_node, &proc->allocated_buffers);
825 if (!list_is_last(&buffer->entry, &proc->buffers)) {
826 struct binder_buffer *next = list_entry(buffer->entry.next,
827 struct binder_buffer, entry);
829 rb_erase(&next->rb_node, &proc->free_buffers);
830 binder_delete_free_buffer(proc, next);
833 if (proc->buffers.next != &buffer->entry) {
834 struct binder_buffer *prev = list_entry(buffer->entry.prev,
835 struct binder_buffer, entry);
837 binder_delete_free_buffer(proc, buffer);
838 rb_erase(&prev->rb_node, &proc->free_buffers);
842 binder_insert_free_buffer(proc, buffer);
845 static struct binder_node *binder_get_node(struct binder_proc *proc,
848 struct rb_node *n = proc->nodes.rb_node;
849 struct binder_node *node;
852 node = rb_entry(n, struct binder_node, rb_node);
856 else if (ptr > node->ptr)
864 static struct binder_node *binder_new_node(struct binder_proc *proc,
868 struct rb_node **p = &proc->nodes.rb_node;
869 struct rb_node *parent = NULL;
870 struct binder_node *node;
874 node = rb_entry(parent, struct binder_node, rb_node);
878 else if (ptr > node->ptr)
884 node = kzalloc(sizeof(*node), GFP_KERNEL);
887 binder_stats_created(BINDER_STAT_NODE);
888 rb_link_node(&node->rb_node, parent, p);
889 rb_insert_color(&node->rb_node, &proc->nodes);
890 node->debug_id = ++binder_last_id;
893 node->cookie = cookie;
894 node->work.type = BINDER_WORK_NODE;
895 INIT_LIST_HEAD(&node->work.entry);
896 INIT_LIST_HEAD(&node->async_todo);
897 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
898 "binder: %d:%d node %d u%p c%p created\n",
899 proc->pid, current->pid, node->debug_id,
900 node->ptr, node->cookie);
904 static int binder_inc_node(struct binder_node *node, int strong, int internal,
905 struct list_head *target_list)
909 if (target_list == NULL &&
910 node->internal_strong_refs == 0 &&
911 !(node == binder_context_mgr_node &&
912 node->has_strong_ref)) {
913 pr_err("binder: invalid inc strong "
914 "node for %d\n", node->debug_id);
917 node->internal_strong_refs++;
919 node->local_strong_refs++;
920 if (!node->has_strong_ref && target_list) {
921 list_del_init(&node->work.entry);
922 list_add_tail(&node->work.entry, target_list);
926 node->local_weak_refs++;
927 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
928 if (target_list == NULL) {
929 pr_err("binder: invalid inc weak node "
930 "for %d\n", node->debug_id);
933 list_add_tail(&node->work.entry, target_list);
939 static int binder_dec_node(struct binder_node *node, int strong, int internal)
943 node->internal_strong_refs--;
945 node->local_strong_refs--;
946 if (node->local_strong_refs || node->internal_strong_refs)
950 node->local_weak_refs--;
951 if (node->local_weak_refs || !hlist_empty(&node->refs))
954 if (node->proc && (node->has_strong_ref || node->has_weak_ref)) {
955 if (list_empty(&node->work.entry)) {
956 list_add_tail(&node->work.entry, &node->proc->todo);
957 wake_up_interruptible(&node->proc->wait);
960 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
961 !node->local_weak_refs) {
962 list_del_init(&node->work.entry);
964 rb_erase(&node->rb_node, &node->proc->nodes);
965 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
966 "binder: refless node %d deleted\n",
969 hlist_del(&node->dead_node);
970 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
971 "binder: dead node %d deleted\n",
975 binder_stats_deleted(BINDER_STAT_NODE);
983 static struct binder_ref *binder_get_ref(struct binder_proc *proc,
986 struct rb_node *n = proc->refs_by_desc.rb_node;
987 struct binder_ref *ref;
990 ref = rb_entry(n, struct binder_ref, rb_node_desc);
992 if (desc < ref->desc)
994 else if (desc > ref->desc)
1002 static struct binder_ref *binder_get_ref_for_node(struct binder_proc *proc,
1003 struct binder_node *node)
1006 struct rb_node **p = &proc->refs_by_node.rb_node;
1007 struct rb_node *parent = NULL;
1008 struct binder_ref *ref, *new_ref;
1012 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1014 if (node < ref->node)
1016 else if (node > ref->node)
1017 p = &(*p)->rb_right;
1021 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1022 if (new_ref == NULL)
1024 binder_stats_created(BINDER_STAT_REF);
1025 new_ref->debug_id = ++binder_last_id;
1026 new_ref->proc = proc;
1027 new_ref->node = node;
1028 rb_link_node(&new_ref->rb_node_node, parent, p);
1029 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1031 new_ref->desc = (node == binder_context_mgr_node) ? 0 : 1;
1032 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1033 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1034 if (ref->desc > new_ref->desc)
1036 new_ref->desc = ref->desc + 1;
1039 p = &proc->refs_by_desc.rb_node;
1042 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1044 if (new_ref->desc < ref->desc)
1046 else if (new_ref->desc > ref->desc)
1047 p = &(*p)->rb_right;
1051 rb_link_node(&new_ref->rb_node_desc, parent, p);
1052 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
1054 hlist_add_head(&new_ref->node_entry, &node->refs);
1056 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1057 "binder: %d new ref %d desc %d for "
1058 "node %d\n", proc->pid, new_ref->debug_id,
1059 new_ref->desc, node->debug_id);
1061 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1062 "binder: %d new ref %d desc %d for "
1063 "dead node\n", proc->pid, new_ref->debug_id,
1069 static void binder_delete_ref(struct binder_ref *ref)
1071 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1072 "binder: %d delete ref %d desc %d for "
1073 "node %d\n", ref->proc->pid, ref->debug_id,
1074 ref->desc, ref->node->debug_id);
1076 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1077 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
1079 binder_dec_node(ref->node, 1, 1);
1080 hlist_del(&ref->node_entry);
1081 binder_dec_node(ref->node, 0, 1);
1083 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1084 "binder: %d delete ref %d desc %d "
1085 "has death notification\n", ref->proc->pid,
1086 ref->debug_id, ref->desc);
1087 list_del(&ref->death->work.entry);
1089 binder_stats_deleted(BINDER_STAT_DEATH);
1092 binder_stats_deleted(BINDER_STAT_REF);
1095 static int binder_inc_ref(struct binder_ref *ref, int strong,
1096 struct list_head *target_list)
1100 if (ref->strong == 0) {
1101 ret = binder_inc_node(ref->node, 1, 1, target_list);
1107 if (ref->weak == 0) {
1108 ret = binder_inc_node(ref->node, 0, 1, target_list);
1118 static int binder_dec_ref(struct binder_ref *ref, int strong)
1121 if (ref->strong == 0) {
1122 binder_user_error("binder: %d invalid dec strong, "
1123 "ref %d desc %d s %d w %d\n",
1124 ref->proc->pid, ref->debug_id,
1125 ref->desc, ref->strong, ref->weak);
1129 if (ref->strong == 0) {
1131 ret = binder_dec_node(ref->node, strong, 1);
1136 if (ref->weak == 0) {
1137 binder_user_error("binder: %d invalid dec weak, "
1138 "ref %d desc %d s %d w %d\n",
1139 ref->proc->pid, ref->debug_id,
1140 ref->desc, ref->strong, ref->weak);
1145 if (ref->strong == 0 && ref->weak == 0)
1146 binder_delete_ref(ref);
1150 static void binder_pop_transaction(struct binder_thread *target_thread,
1151 struct binder_transaction *t)
1153 if (target_thread) {
1154 BUG_ON(target_thread->transaction_stack != t);
1155 BUG_ON(target_thread->transaction_stack->from != target_thread);
1156 target_thread->transaction_stack =
1157 target_thread->transaction_stack->from_parent;
1162 t->buffer->transaction = NULL;
1164 binder_stats_deleted(BINDER_STAT_TRANSACTION);
1167 static void binder_send_failed_reply(struct binder_transaction *t,
1168 uint32_t error_code)
1170 struct binder_thread *target_thread;
1171 BUG_ON(t->flags & TF_ONE_WAY);
1173 target_thread = t->from;
1174 if (target_thread) {
1175 if (target_thread->return_error != BR_OK &&
1176 target_thread->return_error2 == BR_OK) {
1177 target_thread->return_error2 =
1178 target_thread->return_error;
1179 target_thread->return_error = BR_OK;
1181 if (target_thread->return_error == BR_OK) {
1182 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1183 "binder: send failed reply for "
1184 "transaction %d to %d:%d\n",
1185 t->debug_id, target_thread->proc->pid,
1186 target_thread->pid);
1188 binder_pop_transaction(target_thread, t);
1189 target_thread->return_error = error_code;
1190 wake_up_interruptible(&target_thread->wait);
1192 pr_err("binder: reply failed, target "
1193 "thread, %d:%d, has error code %d "
1194 "already\n", target_thread->proc->pid,
1196 target_thread->return_error);
1200 struct binder_transaction *next = t->from_parent;
1202 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1203 "binder: send failed reply "
1204 "for transaction %d, target dead\n",
1207 binder_pop_transaction(target_thread, t);
1209 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1210 "binder: reply failed,"
1211 " no target thread at root\n");
1215 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1216 "binder: reply failed, no target "
1217 "thread -- retry %d\n", t->debug_id);
1222 static void binder_transaction_buffer_release(struct binder_proc *proc,
1223 struct binder_buffer *buffer,
1226 size_t *offp, *off_end;
1227 int debug_id = buffer->debug_id;
1229 binder_debug(BINDER_DEBUG_TRANSACTION,
1230 "binder: %d buffer release %d, size %zd-%zd, failed at %p\n",
1231 proc->pid, buffer->debug_id,
1232 buffer->data_size, buffer->offsets_size, failed_at);
1234 if (buffer->target_node)
1235 binder_dec_node(buffer->target_node, 1, 0);
1237 offp = (size_t *)(buffer->data + ALIGN(buffer->data_size, sizeof(void *)));
1239 off_end = failed_at;
1241 off_end = (void *)offp + buffer->offsets_size;
1242 for (; offp < off_end; offp++) {
1243 struct flat_binder_object *fp;
1244 if (*offp > buffer->data_size - sizeof(*fp) ||
1245 buffer->data_size < sizeof(*fp) ||
1246 !IS_ALIGNED(*offp, sizeof(void *))) {
1247 pr_err("binder: transaction release %d bad"
1248 "offset %zd, size %zd\n", debug_id,
1249 *offp, buffer->data_size);
1252 fp = (struct flat_binder_object *)(buffer->data + *offp);
1254 case BINDER_TYPE_BINDER:
1255 case BINDER_TYPE_WEAK_BINDER: {
1256 struct binder_node *node = binder_get_node(proc, fp->binder);
1258 pr_err("binder: transaction release %d"
1259 " bad node %p\n", debug_id, fp->binder);
1262 binder_debug(BINDER_DEBUG_TRANSACTION,
1264 node->debug_id, node->ptr);
1265 binder_dec_node(node, fp->type == BINDER_TYPE_BINDER, 0);
1267 case BINDER_TYPE_HANDLE:
1268 case BINDER_TYPE_WEAK_HANDLE: {
1269 struct binder_ref *ref = binder_get_ref(proc, fp->handle);
1271 pr_err("binder: transaction release %d"
1272 " bad handle %ld\n", debug_id,
1276 binder_debug(BINDER_DEBUG_TRANSACTION,
1277 " ref %d desc %d (node %d)\n",
1278 ref->debug_id, ref->desc, ref->node->debug_id);
1279 binder_dec_ref(ref, fp->type == BINDER_TYPE_HANDLE);
1282 case BINDER_TYPE_FD:
1283 binder_debug(BINDER_DEBUG_TRANSACTION,
1284 " fd %ld\n", fp->handle);
1286 task_close_fd(proc, fp->handle);
1290 pr_err("binder: transaction release %d bad "
1291 "object type %lx\n", debug_id, fp->type);
1297 static void binder_transaction(struct binder_proc *proc,
1298 struct binder_thread *thread,
1299 struct binder_transaction_data *tr, int reply)
1301 struct binder_transaction *t;
1302 struct binder_work *tcomplete;
1303 size_t *offp, *off_end;
1304 struct binder_proc *target_proc;
1305 struct binder_thread *target_thread = NULL;
1306 struct binder_node *target_node = NULL;
1307 struct list_head *target_list;
1308 wait_queue_head_t *target_wait;
1309 struct binder_transaction *in_reply_to = NULL;
1310 struct binder_transaction_log_entry *e;
1311 uint32_t return_error;
1313 e = binder_transaction_log_add(&binder_transaction_log);
1314 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
1315 e->from_proc = proc->pid;
1316 e->from_thread = thread->pid;
1317 e->target_handle = tr->target.handle;
1318 e->data_size = tr->data_size;
1319 e->offsets_size = tr->offsets_size;
1322 in_reply_to = thread->transaction_stack;
1323 if (in_reply_to == NULL) {
1324 binder_user_error("binder: %d:%d got reply transaction "
1325 "with no transaction stack\n",
1326 proc->pid, thread->pid);
1327 return_error = BR_FAILED_REPLY;
1328 goto err_empty_call_stack;
1330 binder_set_nice(in_reply_to->saved_priority);
1331 if (in_reply_to->to_thread != thread) {
1332 binder_user_error("binder: %d:%d got reply transaction "
1333 "with bad transaction stack,"
1334 " transaction %d has target %d:%d\n",
1335 proc->pid, thread->pid, in_reply_to->debug_id,
1336 in_reply_to->to_proc ?
1337 in_reply_to->to_proc->pid : 0,
1338 in_reply_to->to_thread ?
1339 in_reply_to->to_thread->pid : 0);
1340 return_error = BR_FAILED_REPLY;
1342 goto err_bad_call_stack;
1344 thread->transaction_stack = in_reply_to->to_parent;
1345 target_thread = in_reply_to->from;
1346 if (target_thread == NULL) {
1347 return_error = BR_DEAD_REPLY;
1348 goto err_dead_binder;
1350 if (target_thread->transaction_stack != in_reply_to) {
1351 binder_user_error("binder: %d:%d got reply transaction "
1352 "with bad target transaction stack %d, "
1354 proc->pid, thread->pid,
1355 target_thread->transaction_stack ?
1356 target_thread->transaction_stack->debug_id : 0,
1357 in_reply_to->debug_id);
1358 return_error = BR_FAILED_REPLY;
1360 target_thread = NULL;
1361 goto err_dead_binder;
1363 target_proc = target_thread->proc;
1365 if (tr->target.handle) {
1366 struct binder_ref *ref;
1367 ref = binder_get_ref(proc, tr->target.handle);
1369 binder_user_error("binder: %d:%d got "
1370 "transaction to invalid handle\n",
1371 proc->pid, thread->pid);
1372 return_error = BR_FAILED_REPLY;
1373 goto err_invalid_target_handle;
1375 target_node = ref->node;
1377 target_node = binder_context_mgr_node;
1378 if (target_node == NULL) {
1379 return_error = BR_DEAD_REPLY;
1380 goto err_no_context_mgr_node;
1383 e->to_node = target_node->debug_id;
1384 target_proc = target_node->proc;
1385 if (target_proc == NULL) {
1386 return_error = BR_DEAD_REPLY;
1387 goto err_dead_binder;
1389 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
1390 struct binder_transaction *tmp;
1391 tmp = thread->transaction_stack;
1392 if (tmp->to_thread != thread) {
1393 binder_user_error("binder: %d:%d got new "
1394 "transaction with bad transaction stack"
1395 ", transaction %d has target %d:%d\n",
1396 proc->pid, thread->pid, tmp->debug_id,
1397 tmp->to_proc ? tmp->to_proc->pid : 0,
1399 tmp->to_thread->pid : 0);
1400 return_error = BR_FAILED_REPLY;
1401 goto err_bad_call_stack;
1404 if (tmp->from && tmp->from->proc == target_proc)
1405 target_thread = tmp->from;
1406 tmp = tmp->from_parent;
1410 if (target_thread) {
1411 e->to_thread = target_thread->pid;
1412 target_list = &target_thread->todo;
1413 target_wait = &target_thread->wait;
1415 target_list = &target_proc->todo;
1416 target_wait = &target_proc->wait;
1418 e->to_proc = target_proc->pid;
1420 /* TODO: reuse incoming transaction for reply */
1421 t = kzalloc(sizeof(*t), GFP_KERNEL);
1423 return_error = BR_FAILED_REPLY;
1424 goto err_alloc_t_failed;
1426 binder_stats_created(BINDER_STAT_TRANSACTION);
1428 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
1429 if (tcomplete == NULL) {
1430 return_error = BR_FAILED_REPLY;
1431 goto err_alloc_tcomplete_failed;
1433 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
1435 t->debug_id = ++binder_last_id;
1436 e->debug_id = t->debug_id;
1439 binder_debug(BINDER_DEBUG_TRANSACTION,
1440 "binder: %d:%d BC_REPLY %d -> %d:%d, "
1441 "data %p-%p size %zd-%zd\n",
1442 proc->pid, thread->pid, t->debug_id,
1443 target_proc->pid, target_thread->pid,
1444 tr->data.ptr.buffer, tr->data.ptr.offsets,
1445 tr->data_size, tr->offsets_size);
1447 binder_debug(BINDER_DEBUG_TRANSACTION,
1448 "binder: %d:%d BC_TRANSACTION %d -> "
1449 "%d - node %d, data %p-%p size %zd-%zd\n",
1450 proc->pid, thread->pid, t->debug_id,
1451 target_proc->pid, target_node->debug_id,
1452 tr->data.ptr.buffer, tr->data.ptr.offsets,
1453 tr->data_size, tr->offsets_size);
1455 if (!reply && !(tr->flags & TF_ONE_WAY))
1459 t->sender_euid = proc->tsk->cred->euid;
1460 t->to_proc = target_proc;
1461 t->to_thread = target_thread;
1463 t->flags = tr->flags;
1464 t->priority = task_nice(current);
1465 t->buffer = binder_alloc_buf(target_proc, tr->data_size,
1466 tr->offsets_size, !reply && (t->flags & TF_ONE_WAY));
1467 if (t->buffer == NULL) {
1468 return_error = BR_FAILED_REPLY;
1469 goto err_binder_alloc_buf_failed;
1471 t->buffer->allow_user_free = 0;
1472 t->buffer->debug_id = t->debug_id;
1473 t->buffer->transaction = t;
1474 t->buffer->target_node = target_node;
1476 binder_inc_node(target_node, 1, 0, NULL);
1478 offp = (size_t *)(t->buffer->data + ALIGN(tr->data_size, sizeof(void *)));
1480 if (copy_from_user(t->buffer->data, tr->data.ptr.buffer, tr->data_size)) {
1481 binder_user_error("binder: %d:%d got transaction with invalid "
1482 "data ptr\n", proc->pid, thread->pid);
1483 return_error = BR_FAILED_REPLY;
1484 goto err_copy_data_failed;
1486 if (copy_from_user(offp, tr->data.ptr.offsets, tr->offsets_size)) {
1487 binder_user_error("binder: %d:%d got transaction with invalid "
1488 "offsets ptr\n", proc->pid, thread->pid);
1489 return_error = BR_FAILED_REPLY;
1490 goto err_copy_data_failed;
1492 if (!IS_ALIGNED(tr->offsets_size, sizeof(size_t))) {
1493 binder_user_error("binder: %d:%d got transaction with "
1494 "invalid offsets size, %zd\n",
1495 proc->pid, thread->pid, tr->offsets_size);
1496 return_error = BR_FAILED_REPLY;
1497 goto err_bad_offset;
1499 off_end = (void *)offp + tr->offsets_size;
1500 for (; offp < off_end; offp++) {
1501 struct flat_binder_object *fp;
1502 if (*offp > t->buffer->data_size - sizeof(*fp) ||
1503 t->buffer->data_size < sizeof(*fp) ||
1504 !IS_ALIGNED(*offp, sizeof(void *))) {
1505 binder_user_error("binder: %d:%d got transaction with "
1506 "invalid offset, %zd\n",
1507 proc->pid, thread->pid, *offp);
1508 return_error = BR_FAILED_REPLY;
1509 goto err_bad_offset;
1511 fp = (struct flat_binder_object *)(t->buffer->data + *offp);
1513 case BINDER_TYPE_BINDER:
1514 case BINDER_TYPE_WEAK_BINDER: {
1515 struct binder_ref *ref;
1516 struct binder_node *node = binder_get_node(proc, fp->binder);
1518 node = binder_new_node(proc, fp->binder, fp->cookie);
1520 return_error = BR_FAILED_REPLY;
1521 goto err_binder_new_node_failed;
1523 node->min_priority = fp->flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
1524 node->accept_fds = !!(fp->flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
1526 if (fp->cookie != node->cookie) {
1527 binder_user_error("binder: %d:%d sending u%p "
1528 "node %d, cookie mismatch %p != %p\n",
1529 proc->pid, thread->pid,
1530 fp->binder, node->debug_id,
1531 fp->cookie, node->cookie);
1532 goto err_binder_get_ref_for_node_failed;
1534 ref = binder_get_ref_for_node(target_proc, node);
1536 return_error = BR_FAILED_REPLY;
1537 goto err_binder_get_ref_for_node_failed;
1539 if (fp->type == BINDER_TYPE_BINDER)
1540 fp->type = BINDER_TYPE_HANDLE;
1542 fp->type = BINDER_TYPE_WEAK_HANDLE;
1543 fp->handle = ref->desc;
1544 binder_inc_ref(ref, fp->type == BINDER_TYPE_HANDLE,
1547 binder_debug(BINDER_DEBUG_TRANSACTION,
1548 " node %d u%p -> ref %d desc %d\n",
1549 node->debug_id, node->ptr, ref->debug_id,
1552 case BINDER_TYPE_HANDLE:
1553 case BINDER_TYPE_WEAK_HANDLE: {
1554 struct binder_ref *ref = binder_get_ref(proc, fp->handle);
1556 binder_user_error("binder: %d:%d got "
1557 "transaction with invalid "
1558 "handle, %ld\n", proc->pid,
1559 thread->pid, fp->handle);
1560 return_error = BR_FAILED_REPLY;
1561 goto err_binder_get_ref_failed;
1563 if (ref->node->proc == target_proc) {
1564 if (fp->type == BINDER_TYPE_HANDLE)
1565 fp->type = BINDER_TYPE_BINDER;
1567 fp->type = BINDER_TYPE_WEAK_BINDER;
1568 fp->binder = ref->node->ptr;
1569 fp->cookie = ref->node->cookie;
1570 binder_inc_node(ref->node, fp->type == BINDER_TYPE_BINDER, 0, NULL);
1571 binder_debug(BINDER_DEBUG_TRANSACTION,
1572 " ref %d desc %d -> node %d u%p\n",
1573 ref->debug_id, ref->desc, ref->node->debug_id,
1576 struct binder_ref *new_ref;
1577 new_ref = binder_get_ref_for_node(target_proc, ref->node);
1578 if (new_ref == NULL) {
1579 return_error = BR_FAILED_REPLY;
1580 goto err_binder_get_ref_for_node_failed;
1582 fp->handle = new_ref->desc;
1583 binder_inc_ref(new_ref, fp->type == BINDER_TYPE_HANDLE, NULL);
1584 binder_debug(BINDER_DEBUG_TRANSACTION,
1585 " ref %d desc %d -> ref %d desc %d (node %d)\n",
1586 ref->debug_id, ref->desc, new_ref->debug_id,
1587 new_ref->desc, ref->node->debug_id);
1591 case BINDER_TYPE_FD: {
1596 if (!(in_reply_to->flags & TF_ACCEPT_FDS)) {
1597 binder_user_error("binder: %d:%d got reply with fd, %ld, but target does not allow fds\n",
1598 proc->pid, thread->pid, fp->handle);
1599 return_error = BR_FAILED_REPLY;
1600 goto err_fd_not_allowed;
1602 } else if (!target_node->accept_fds) {
1603 binder_user_error("binder: %d:%d got transaction with fd, %ld, but target does not allow fds\n",
1604 proc->pid, thread->pid, fp->handle);
1605 return_error = BR_FAILED_REPLY;
1606 goto err_fd_not_allowed;
1609 file = fget(fp->handle);
1611 binder_user_error("binder: %d:%d got transaction with invalid fd, %ld\n",
1612 proc->pid, thread->pid, fp->handle);
1613 return_error = BR_FAILED_REPLY;
1614 goto err_fget_failed;
1616 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
1617 if (target_fd < 0) {
1619 return_error = BR_FAILED_REPLY;
1620 goto err_get_unused_fd_failed;
1622 task_fd_install(target_proc, target_fd, file);
1623 binder_debug(BINDER_DEBUG_TRANSACTION,
1624 " fd %ld -> %d\n", fp->handle, target_fd);
1626 fp->handle = target_fd;
1630 binder_user_error("binder: %d:%d got transactio"
1631 "n with invalid object type, %lx\n",
1632 proc->pid, thread->pid, fp->type);
1633 return_error = BR_FAILED_REPLY;
1634 goto err_bad_object_type;
1638 BUG_ON(t->buffer->async_transaction != 0);
1639 binder_pop_transaction(target_thread, in_reply_to);
1640 } else if (!(t->flags & TF_ONE_WAY)) {
1641 BUG_ON(t->buffer->async_transaction != 0);
1643 t->from_parent = thread->transaction_stack;
1644 thread->transaction_stack = t;
1646 BUG_ON(target_node == NULL);
1647 BUG_ON(t->buffer->async_transaction != 1);
1648 if (target_node->has_async_transaction) {
1649 target_list = &target_node->async_todo;
1652 target_node->has_async_transaction = 1;
1654 t->work.type = BINDER_WORK_TRANSACTION;
1655 list_add_tail(&t->work.entry, target_list);
1656 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
1657 list_add_tail(&tcomplete->entry, &thread->todo);
1659 wake_up_interruptible(target_wait);
1662 err_get_unused_fd_failed:
1665 err_binder_get_ref_for_node_failed:
1666 err_binder_get_ref_failed:
1667 err_binder_new_node_failed:
1668 err_bad_object_type:
1670 err_copy_data_failed:
1671 binder_transaction_buffer_release(target_proc, t->buffer, offp);
1672 t->buffer->transaction = NULL;
1673 binder_free_buf(target_proc, t->buffer);
1674 err_binder_alloc_buf_failed:
1676 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
1677 err_alloc_tcomplete_failed:
1679 binder_stats_deleted(BINDER_STAT_TRANSACTION);
1682 err_empty_call_stack:
1684 err_invalid_target_handle:
1685 err_no_context_mgr_node:
1686 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1687 "binder: %d:%d transaction failed %d, size %zd-%zd\n",
1688 proc->pid, thread->pid, return_error,
1689 tr->data_size, tr->offsets_size);
1692 struct binder_transaction_log_entry *fe;
1693 fe = binder_transaction_log_add(&binder_transaction_log_failed);
1697 BUG_ON(thread->return_error != BR_OK);
1699 thread->return_error = BR_TRANSACTION_COMPLETE;
1700 binder_send_failed_reply(in_reply_to, return_error);
1702 thread->return_error = return_error;
1705 int binder_thread_write(struct binder_proc *proc, struct binder_thread *thread,
1706 void __user *buffer, int size, signed long *consumed)
1709 void __user *ptr = buffer + *consumed;
1710 void __user *end = buffer + size;
1712 while (ptr < end && thread->return_error == BR_OK) {
1713 if (get_user(cmd, (uint32_t __user *)ptr))
1715 ptr += sizeof(uint32_t);
1716 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
1717 binder_stats.bc[_IOC_NR(cmd)]++;
1718 proc->stats.bc[_IOC_NR(cmd)]++;
1719 thread->stats.bc[_IOC_NR(cmd)]++;
1727 struct binder_ref *ref;
1728 const char *debug_string;
1730 if (get_user(target, (uint32_t __user *)ptr))
1732 ptr += sizeof(uint32_t);
1733 if (target == 0 && binder_context_mgr_node &&
1734 (cmd == BC_INCREFS || cmd == BC_ACQUIRE)) {
1735 ref = binder_get_ref_for_node(proc,
1736 binder_context_mgr_node);
1737 if (ref->desc != target) {
1738 binder_user_error("binder: %d:"
1739 "%d tried to acquire "
1740 "reference to desc 0, "
1742 proc->pid, thread->pid,
1746 ref = binder_get_ref(proc, target);
1748 binder_user_error("binder: %d:%d refcou"
1749 "nt change on invalid ref %d\n",
1750 proc->pid, thread->pid, target);
1755 debug_string = "IncRefs";
1756 binder_inc_ref(ref, 0, NULL);
1759 debug_string = "Acquire";
1760 binder_inc_ref(ref, 1, NULL);
1763 debug_string = "Release";
1764 binder_dec_ref(ref, 1);
1768 debug_string = "DecRefs";
1769 binder_dec_ref(ref, 0);
1772 binder_debug(BINDER_DEBUG_USER_REFS,
1773 "binder: %d:%d %s ref %d desc %d s %d w %d for node %d\n",
1774 proc->pid, thread->pid, debug_string, ref->debug_id,
1775 ref->desc, ref->strong, ref->weak, ref->node->debug_id);
1778 case BC_INCREFS_DONE:
1779 case BC_ACQUIRE_DONE: {
1780 void __user *node_ptr;
1782 struct binder_node *node;
1784 if (get_user(node_ptr, (void * __user *)ptr))
1786 ptr += sizeof(void *);
1787 if (get_user(cookie, (void * __user *)ptr))
1789 ptr += sizeof(void *);
1790 node = binder_get_node(proc, node_ptr);
1792 binder_user_error("binder: %d:%d "
1793 "%s u%p no match\n",
1794 proc->pid, thread->pid,
1795 cmd == BC_INCREFS_DONE ?
1801 if (cookie != node->cookie) {
1802 binder_user_error("binder: %d:%d %s u%p node %d"
1803 " cookie mismatch %p != %p\n",
1804 proc->pid, thread->pid,
1805 cmd == BC_INCREFS_DONE ?
1806 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
1807 node_ptr, node->debug_id,
1808 cookie, node->cookie);
1811 if (cmd == BC_ACQUIRE_DONE) {
1812 if (node->pending_strong_ref == 0) {
1813 binder_user_error("binder: %d:%d "
1814 "BC_ACQUIRE_DONE node %d has "
1815 "no pending acquire request\n",
1816 proc->pid, thread->pid,
1820 node->pending_strong_ref = 0;
1822 if (node->pending_weak_ref == 0) {
1823 binder_user_error("binder: %d:%d "
1824 "BC_INCREFS_DONE node %d has "
1825 "no pending increfs request\n",
1826 proc->pid, thread->pid,
1830 node->pending_weak_ref = 0;
1832 binder_dec_node(node, cmd == BC_ACQUIRE_DONE, 0);
1833 binder_debug(BINDER_DEBUG_USER_REFS,
1834 "binder: %d:%d %s node %d ls %d lw %d\n",
1835 proc->pid, thread->pid,
1836 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
1837 node->debug_id, node->local_strong_refs, node->local_weak_refs);
1840 case BC_ATTEMPT_ACQUIRE:
1841 pr_err("binder: BC_ATTEMPT_ACQUIRE not supported\n");
1843 case BC_ACQUIRE_RESULT:
1844 pr_err("binder: BC_ACQUIRE_RESULT not supported\n");
1847 case BC_FREE_BUFFER: {
1848 void __user *data_ptr;
1849 struct binder_buffer *buffer;
1851 if (get_user(data_ptr, (void * __user *)ptr))
1853 ptr += sizeof(void *);
1855 buffer = binder_buffer_lookup(proc, data_ptr);
1856 if (buffer == NULL) {
1857 binder_user_error("binder: %d:%d "
1858 "BC_FREE_BUFFER u%p no match\n",
1859 proc->pid, thread->pid, data_ptr);
1862 if (!buffer->allow_user_free) {
1863 binder_user_error("binder: %d:%d "
1864 "BC_FREE_BUFFER u%p matched "
1865 "unreturned buffer\n",
1866 proc->pid, thread->pid, data_ptr);
1869 binder_debug(BINDER_DEBUG_FREE_BUFFER,
1870 "binder: %d:%d BC_FREE_BUFFER u%p found buffer %d for %s transaction\n",
1871 proc->pid, thread->pid, data_ptr, buffer->debug_id,
1872 buffer->transaction ? "active" : "finished");
1874 if (buffer->transaction) {
1875 buffer->transaction->buffer = NULL;
1876 buffer->transaction = NULL;
1878 if (buffer->async_transaction && buffer->target_node) {
1879 BUG_ON(!buffer->target_node->has_async_transaction);
1880 if (list_empty(&buffer->target_node->async_todo))
1881 buffer->target_node->has_async_transaction = 0;
1883 list_move_tail(buffer->target_node->async_todo.next, &thread->todo);
1885 binder_transaction_buffer_release(proc, buffer, NULL);
1886 binder_free_buf(proc, buffer);
1890 case BC_TRANSACTION:
1892 struct binder_transaction_data tr;
1894 if (copy_from_user(&tr, ptr, sizeof(tr)))
1897 binder_transaction(proc, thread, &tr, cmd == BC_REPLY);
1901 case BC_REGISTER_LOOPER:
1902 binder_debug(BINDER_DEBUG_THREADS,
1903 "binder: %d:%d BC_REGISTER_LOOPER\n",
1904 proc->pid, thread->pid);
1905 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
1906 thread->looper |= BINDER_LOOPER_STATE_INVALID;
1907 binder_user_error("binder: %d:%d ERROR:"
1908 " BC_REGISTER_LOOPER called "
1909 "after BC_ENTER_LOOPER\n",
1910 proc->pid, thread->pid);
1911 } else if (proc->requested_threads == 0) {
1912 thread->looper |= BINDER_LOOPER_STATE_INVALID;
1913 binder_user_error("binder: %d:%d ERROR:"
1914 " BC_REGISTER_LOOPER called "
1915 "without request\n",
1916 proc->pid, thread->pid);
1918 proc->requested_threads--;
1919 proc->requested_threads_started++;
1921 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
1923 case BC_ENTER_LOOPER:
1924 binder_debug(BINDER_DEBUG_THREADS,
1925 "binder: %d:%d BC_ENTER_LOOPER\n",
1926 proc->pid, thread->pid);
1927 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
1928 thread->looper |= BINDER_LOOPER_STATE_INVALID;
1929 binder_user_error("binder: %d:%d ERROR:"
1930 " BC_ENTER_LOOPER called after "
1931 "BC_REGISTER_LOOPER\n",
1932 proc->pid, thread->pid);
1934 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
1936 case BC_EXIT_LOOPER:
1937 binder_debug(BINDER_DEBUG_THREADS,
1938 "binder: %d:%d BC_EXIT_LOOPER\n",
1939 proc->pid, thread->pid);
1940 thread->looper |= BINDER_LOOPER_STATE_EXITED;
1943 case BC_REQUEST_DEATH_NOTIFICATION:
1944 case BC_CLEAR_DEATH_NOTIFICATION: {
1946 void __user *cookie;
1947 struct binder_ref *ref;
1948 struct binder_ref_death *death;
1950 if (get_user(target, (uint32_t __user *)ptr))
1952 ptr += sizeof(uint32_t);
1953 if (get_user(cookie, (void __user * __user *)ptr))
1955 ptr += sizeof(void *);
1956 ref = binder_get_ref(proc, target);
1958 binder_user_error("binder: %d:%d %s "
1960 proc->pid, thread->pid,
1961 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
1962 "BC_REQUEST_DEATH_NOTIFICATION" :
1963 "BC_CLEAR_DEATH_NOTIFICATION",
1968 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
1969 "binder: %d:%d %s %p ref %d desc %d s %d w %d for node %d\n",
1970 proc->pid, thread->pid,
1971 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
1972 "BC_REQUEST_DEATH_NOTIFICATION" :
1973 "BC_CLEAR_DEATH_NOTIFICATION",
1974 cookie, ref->debug_id, ref->desc,
1975 ref->strong, ref->weak, ref->node->debug_id);
1977 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
1979 binder_user_error("binder: %d:%"
1980 "d BC_REQUEST_DEATH_NOTI"
1981 "FICATION death notific"
1982 "ation already set\n",
1983 proc->pid, thread->pid);
1986 death = kzalloc(sizeof(*death), GFP_KERNEL);
1987 if (death == NULL) {
1988 thread->return_error = BR_ERROR;
1989 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1991 "BC_REQUEST_DEATH_NOTIFICATION failed\n",
1992 proc->pid, thread->pid);
1995 binder_stats_created(BINDER_STAT_DEATH);
1996 INIT_LIST_HEAD(&death->work.entry);
1997 death->cookie = cookie;
1999 if (ref->node->proc == NULL) {
2000 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
2001 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2002 list_add_tail(&ref->death->work.entry, &thread->todo);
2004 list_add_tail(&ref->death->work.entry, &proc->todo);
2005 wake_up_interruptible(&proc->wait);
2009 if (ref->death == NULL) {
2010 binder_user_error("binder: %d:%"
2011 "d BC_CLEAR_DEATH_NOTIFI"
2012 "CATION death notificat"
2014 proc->pid, thread->pid);
2018 if (death->cookie != cookie) {
2019 binder_user_error("binder: %d:%"
2020 "d BC_CLEAR_DEATH_NOTIFI"
2021 "CATION death notificat"
2022 "ion cookie mismatch "
2024 proc->pid, thread->pid,
2025 death->cookie, cookie);
2029 if (list_empty(&death->work.entry)) {
2030 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2031 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2032 list_add_tail(&death->work.entry, &thread->todo);
2034 list_add_tail(&death->work.entry, &proc->todo);
2035 wake_up_interruptible(&proc->wait);
2038 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
2039 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
2043 case BC_DEAD_BINDER_DONE: {
2044 struct binder_work *w;
2045 void __user *cookie;
2046 struct binder_ref_death *death = NULL;
2047 if (get_user(cookie, (void __user * __user *)ptr))
2050 ptr += sizeof(void *);
2051 list_for_each_entry(w, &proc->delivered_death, entry) {
2052 struct binder_ref_death *tmp_death = container_of(w, struct binder_ref_death, work);
2053 if (tmp_death->cookie == cookie) {
2058 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2059 "binder: %d:%d BC_DEAD_BINDER_DONE %p found %p\n",
2060 proc->pid, thread->pid, cookie, death);
2061 if (death == NULL) {
2062 binder_user_error("binder: %d:%d BC_DEAD"
2063 "_BINDER_DONE %p not found\n",
2064 proc->pid, thread->pid, cookie);
2068 list_del_init(&death->work.entry);
2069 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
2070 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2071 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2072 list_add_tail(&death->work.entry, &thread->todo);
2074 list_add_tail(&death->work.entry, &proc->todo);
2075 wake_up_interruptible(&proc->wait);
2081 pr_err("binder: %d:%d unknown command %d\n",
2082 proc->pid, thread->pid, cmd);
2085 *consumed = ptr - buffer;
2090 void binder_stat_br(struct binder_proc *proc, struct binder_thread *thread,
2093 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
2094 binder_stats.br[_IOC_NR(cmd)]++;
2095 proc->stats.br[_IOC_NR(cmd)]++;
2096 thread->stats.br[_IOC_NR(cmd)]++;
2100 static int binder_has_proc_work(struct binder_proc *proc,
2101 struct binder_thread *thread)
2103 return !list_empty(&proc->todo) ||
2104 (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2107 static int binder_has_thread_work(struct binder_thread *thread)
2109 return !list_empty(&thread->todo) || thread->return_error != BR_OK ||
2110 (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2113 static int binder_thread_read(struct binder_proc *proc,
2114 struct binder_thread *thread,
2115 void __user *buffer, int size,
2116 signed long *consumed, int non_block)
2118 void __user *ptr = buffer + *consumed;
2119 void __user *end = buffer + size;
2122 int wait_for_proc_work;
2124 if (*consumed == 0) {
2125 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
2127 ptr += sizeof(uint32_t);
2131 wait_for_proc_work = thread->transaction_stack == NULL &&
2132 list_empty(&thread->todo);
2134 if (thread->return_error != BR_OK && ptr < end) {
2135 if (thread->return_error2 != BR_OK) {
2136 if (put_user(thread->return_error2, (uint32_t __user *)ptr))
2138 ptr += sizeof(uint32_t);
2141 thread->return_error2 = BR_OK;
2143 if (put_user(thread->return_error, (uint32_t __user *)ptr))
2145 ptr += sizeof(uint32_t);
2146 thread->return_error = BR_OK;
2151 thread->looper |= BINDER_LOOPER_STATE_WAITING;
2152 if (wait_for_proc_work)
2153 proc->ready_threads++;
2154 mutex_unlock(&binder_lock);
2155 if (wait_for_proc_work) {
2156 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2157 BINDER_LOOPER_STATE_ENTERED))) {
2158 binder_user_error("binder: %d:%d ERROR: Thread waiting "
2159 "for process work before calling BC_REGISTER_"
2160 "LOOPER or BC_ENTER_LOOPER (state %x)\n",
2161 proc->pid, thread->pid, thread->looper);
2162 wait_event_interruptible(binder_user_error_wait,
2163 binder_stop_on_user_error < 2);
2165 binder_set_nice(proc->default_priority);
2167 if (!binder_has_proc_work(proc, thread))
2170 ret = wait_event_interruptible_exclusive(proc->wait, binder_has_proc_work(proc, thread));
2173 if (!binder_has_thread_work(thread))
2176 ret = wait_event_interruptible(thread->wait, binder_has_thread_work(thread));
2178 mutex_lock(&binder_lock);
2179 if (wait_for_proc_work)
2180 proc->ready_threads--;
2181 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
2188 struct binder_transaction_data tr;
2189 struct binder_work *w;
2190 struct binder_transaction *t = NULL;
2192 if (!list_empty(&thread->todo))
2193 w = list_first_entry(&thread->todo, struct binder_work, entry);
2194 else if (!list_empty(&proc->todo) && wait_for_proc_work)
2195 w = list_first_entry(&proc->todo, struct binder_work, entry);
2197 if (ptr - buffer == 4 && !(thread->looper & BINDER_LOOPER_STATE_NEED_RETURN)) /* no data added */
2202 if (end - ptr < sizeof(tr) + 4)
2206 case BINDER_WORK_TRANSACTION: {
2207 t = container_of(w, struct binder_transaction, work);
2209 case BINDER_WORK_TRANSACTION_COMPLETE: {
2210 cmd = BR_TRANSACTION_COMPLETE;
2211 if (put_user(cmd, (uint32_t __user *)ptr))
2213 ptr += sizeof(uint32_t);
2215 binder_stat_br(proc, thread, cmd);
2216 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
2217 "binder: %d:%d BR_TRANSACTION_COMPLETE\n",
2218 proc->pid, thread->pid);
2220 list_del(&w->entry);
2222 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2224 case BINDER_WORK_NODE: {
2225 struct binder_node *node = container_of(w, struct binder_node, work);
2226 uint32_t cmd = BR_NOOP;
2227 const char *cmd_name;
2228 int strong = node->internal_strong_refs || node->local_strong_refs;
2229 int weak = !hlist_empty(&node->refs) || node->local_weak_refs || strong;
2230 if (weak && !node->has_weak_ref) {
2232 cmd_name = "BR_INCREFS";
2233 node->has_weak_ref = 1;
2234 node->pending_weak_ref = 1;
2235 node->local_weak_refs++;
2236 } else if (strong && !node->has_strong_ref) {
2238 cmd_name = "BR_ACQUIRE";
2239 node->has_strong_ref = 1;
2240 node->pending_strong_ref = 1;
2241 node->local_strong_refs++;
2242 } else if (!strong && node->has_strong_ref) {
2244 cmd_name = "BR_RELEASE";
2245 node->has_strong_ref = 0;
2246 } else if (!weak && node->has_weak_ref) {
2248 cmd_name = "BR_DECREFS";
2249 node->has_weak_ref = 0;
2251 if (cmd != BR_NOOP) {
2252 if (put_user(cmd, (uint32_t __user *)ptr))
2254 ptr += sizeof(uint32_t);
2255 if (put_user(node->ptr, (void * __user *)ptr))
2257 ptr += sizeof(void *);
2258 if (put_user(node->cookie, (void * __user *)ptr))
2260 ptr += sizeof(void *);
2262 binder_stat_br(proc, thread, cmd);
2263 binder_debug(BINDER_DEBUG_USER_REFS,
2264 "binder: %d:%d %s %d u%p c%p\n",
2265 proc->pid, thread->pid, cmd_name, node->debug_id, node->ptr, node->cookie);
2267 list_del_init(&w->entry);
2268 if (!weak && !strong) {
2269 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
2270 "binder: %d:%d node %d u%p c%p deleted\n",
2271 proc->pid, thread->pid, node->debug_id,
2272 node->ptr, node->cookie);
2273 rb_erase(&node->rb_node, &proc->nodes);
2275 binder_stats_deleted(BINDER_STAT_NODE);
2277 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
2278 "binder: %d:%d node %d u%p c%p state unchanged\n",
2279 proc->pid, thread->pid, node->debug_id, node->ptr,
2284 case BINDER_WORK_DEAD_BINDER:
2285 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2286 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2287 struct binder_ref_death *death;
2290 death = container_of(w, struct binder_ref_death, work);
2291 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
2292 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
2294 cmd = BR_DEAD_BINDER;
2295 if (put_user(cmd, (uint32_t __user *)ptr))
2297 ptr += sizeof(uint32_t);
2298 if (put_user(death->cookie, (void * __user *)ptr))
2300 ptr += sizeof(void *);
2301 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
2302 "binder: %d:%d %s %p\n",
2303 proc->pid, thread->pid,
2304 cmd == BR_DEAD_BINDER ?
2306 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
2309 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
2310 list_del(&w->entry);
2312 binder_stats_deleted(BINDER_STAT_DEATH);
2314 list_move(&w->entry, &proc->delivered_death);
2315 if (cmd == BR_DEAD_BINDER)
2316 goto done; /* DEAD_BINDER notifications can cause transactions */
2323 BUG_ON(t->buffer == NULL);
2324 if (t->buffer->target_node) {
2325 struct binder_node *target_node = t->buffer->target_node;
2326 tr.target.ptr = target_node->ptr;
2327 tr.cookie = target_node->cookie;
2328 t->saved_priority = task_nice(current);
2329 if (t->priority < target_node->min_priority &&
2330 !(t->flags & TF_ONE_WAY))
2331 binder_set_nice(t->priority);
2332 else if (!(t->flags & TF_ONE_WAY) ||
2333 t->saved_priority > target_node->min_priority)
2334 binder_set_nice(target_node->min_priority);
2335 cmd = BR_TRANSACTION;
2337 tr.target.ptr = NULL;
2342 tr.flags = t->flags;
2343 tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
2346 struct task_struct *sender = t->from->proc->tsk;
2347 tr.sender_pid = task_tgid_nr_ns(sender,
2348 task_active_pid_ns(current));
2353 tr.data_size = t->buffer->data_size;
2354 tr.offsets_size = t->buffer->offsets_size;
2355 tr.data.ptr.buffer = (void *)t->buffer->data +
2356 proc->user_buffer_offset;
2357 tr.data.ptr.offsets = tr.data.ptr.buffer +
2358 ALIGN(t->buffer->data_size,
2361 if (put_user(cmd, (uint32_t __user *)ptr))
2363 ptr += sizeof(uint32_t);
2364 if (copy_to_user(ptr, &tr, sizeof(tr)))
2368 binder_stat_br(proc, thread, cmd);
2369 binder_debug(BINDER_DEBUG_TRANSACTION,
2370 "binder: %d:%d %s %d %d:%d, cmd %d"
2371 "size %zd-%zd ptr %p-%p\n",
2372 proc->pid, thread->pid,
2373 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
2375 t->debug_id, t->from ? t->from->proc->pid : 0,
2376 t->from ? t->from->pid : 0, cmd,
2377 t->buffer->data_size, t->buffer->offsets_size,
2378 tr.data.ptr.buffer, tr.data.ptr.offsets);
2380 list_del(&t->work.entry);
2381 t->buffer->allow_user_free = 1;
2382 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
2383 t->to_parent = thread->transaction_stack;
2384 t->to_thread = thread;
2385 thread->transaction_stack = t;
2387 t->buffer->transaction = NULL;
2389 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2396 *consumed = ptr - buffer;
2397 if (proc->requested_threads + proc->ready_threads == 0 &&
2398 proc->requested_threads_started < proc->max_threads &&
2399 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2400 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
2401 /*spawn a new thread if we leave this out */) {
2402 proc->requested_threads++;
2403 binder_debug(BINDER_DEBUG_THREADS,
2404 "binder: %d:%d BR_SPAWN_LOOPER\n",
2405 proc->pid, thread->pid);
2406 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
2412 static void binder_release_work(struct list_head *list)
2414 struct binder_work *w;
2415 while (!list_empty(list)) {
2416 w = list_first_entry(list, struct binder_work, entry);
2417 list_del_init(&w->entry);
2419 case BINDER_WORK_TRANSACTION: {
2420 struct binder_transaction *t;
2422 t = container_of(w, struct binder_transaction, work);
2423 if (t->buffer->target_node &&
2424 !(t->flags & TF_ONE_WAY)) {
2425 binder_send_failed_reply(t, BR_DEAD_REPLY);
2427 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2428 "binder: undelivered transaction %d\n",
2430 t->buffer->transaction = NULL;
2432 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2435 case BINDER_WORK_TRANSACTION_COMPLETE: {
2436 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2437 "binder: undelivered TRANSACTION_COMPLETE\n");
2439 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2441 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2442 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2443 struct binder_ref_death *death;
2445 death = container_of(w, struct binder_ref_death, work);
2446 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2447 "binder: undelivered death notification, %p\n",
2450 binder_stats_deleted(BINDER_STAT_DEATH);
2453 pr_err("binder: unexpected work type, %d, not freed\n",
2461 static struct binder_thread *binder_get_thread(struct binder_proc *proc)
2463 struct binder_thread *thread = NULL;
2464 struct rb_node *parent = NULL;
2465 struct rb_node **p = &proc->threads.rb_node;
2469 thread = rb_entry(parent, struct binder_thread, rb_node);
2471 if (current->pid < thread->pid)
2473 else if (current->pid > thread->pid)
2474 p = &(*p)->rb_right;
2479 thread = kzalloc(sizeof(*thread), GFP_KERNEL);
2482 binder_stats_created(BINDER_STAT_THREAD);
2483 thread->proc = proc;
2484 thread->pid = current->pid;
2485 init_waitqueue_head(&thread->wait);
2486 INIT_LIST_HEAD(&thread->todo);
2487 rb_link_node(&thread->rb_node, parent, p);
2488 rb_insert_color(&thread->rb_node, &proc->threads);
2489 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
2490 thread->return_error = BR_OK;
2491 thread->return_error2 = BR_OK;
2496 static int binder_free_thread(struct binder_proc *proc,
2497 struct binder_thread *thread)
2499 struct binder_transaction *t;
2500 struct binder_transaction *send_reply = NULL;
2501 int active_transactions = 0;
2503 rb_erase(&thread->rb_node, &proc->threads);
2504 t = thread->transaction_stack;
2505 if (t && t->to_thread == thread)
2508 active_transactions++;
2509 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2510 "binder: release %d:%d transaction %d "
2511 "%s, still active\n", proc->pid, thread->pid,
2513 (t->to_thread == thread) ? "in" : "out");
2515 if (t->to_thread == thread) {
2517 t->to_thread = NULL;
2519 t->buffer->transaction = NULL;
2523 } else if (t->from == thread) {
2530 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
2531 binder_release_work(&thread->todo);
2533 binder_stats_deleted(BINDER_STAT_THREAD);
2534 return active_transactions;
2537 static unsigned int binder_poll(struct file *filp,
2538 struct poll_table_struct *wait)
2540 struct binder_proc *proc = filp->private_data;
2541 struct binder_thread *thread = NULL;
2542 int wait_for_proc_work;
2544 mutex_lock(&binder_lock);
2545 thread = binder_get_thread(proc);
2547 wait_for_proc_work = thread->transaction_stack == NULL &&
2548 list_empty(&thread->todo) && thread->return_error == BR_OK;
2549 mutex_unlock(&binder_lock);
2551 if (wait_for_proc_work) {
2552 if (binder_has_proc_work(proc, thread))
2554 poll_wait(filp, &proc->wait, wait);
2555 if (binder_has_proc_work(proc, thread))
2558 if (binder_has_thread_work(thread))
2560 poll_wait(filp, &thread->wait, wait);
2561 if (binder_has_thread_work(thread))
2567 static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2570 struct binder_proc *proc = filp->private_data;
2571 struct binder_thread *thread;
2572 unsigned int size = _IOC_SIZE(cmd);
2573 void __user *ubuf = (void __user *)arg;
2575 /*pr_info("binder_ioctl: %d:%d %x %lx\n", proc->pid, current->pid, cmd, arg);*/
2577 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2581 mutex_lock(&binder_lock);
2582 thread = binder_get_thread(proc);
2583 if (thread == NULL) {
2589 case BINDER_WRITE_READ: {
2590 struct binder_write_read bwr;
2591 if (size != sizeof(struct binder_write_read)) {
2595 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
2599 binder_debug(BINDER_DEBUG_READ_WRITE,
2600 "binder: %d:%d write %ld at %08lx, read %ld at %08lx\n",
2601 proc->pid, thread->pid, bwr.write_size, bwr.write_buffer,
2602 bwr.read_size, bwr.read_buffer);
2604 if (bwr.write_size > 0) {
2605 ret = binder_thread_write(proc, thread, (void __user *)bwr.write_buffer, bwr.write_size, &bwr.write_consumed);
2607 bwr.read_consumed = 0;
2608 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2613 if (bwr.read_size > 0) {
2614 ret = binder_thread_read(proc, thread, (void __user *)bwr.read_buffer, bwr.read_size, &bwr.read_consumed, filp->f_flags & O_NONBLOCK);
2615 if (!list_empty(&proc->todo))
2616 wake_up_interruptible(&proc->wait);
2618 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2623 binder_debug(BINDER_DEBUG_READ_WRITE,
2624 "binder: %d:%d wrote %ld of %ld, read return %ld of %ld\n",
2625 proc->pid, thread->pid, bwr.write_consumed, bwr.write_size,
2626 bwr.read_consumed, bwr.read_size);
2627 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
2633 case BINDER_SET_MAX_THREADS:
2634 if (copy_from_user(&proc->max_threads, ubuf, sizeof(proc->max_threads))) {
2639 case BINDER_SET_CONTEXT_MGR:
2640 if (binder_context_mgr_node != NULL) {
2641 pr_err("binder: BINDER_SET_CONTEXT_MGR already set\n");
2645 if (uid_valid(binder_context_mgr_uid)) {
2646 if (!uid_eq(binder_context_mgr_uid, current->cred->euid)) {
2647 pr_err("binder: BINDER_SET_"
2648 "CONTEXT_MGR bad uid %d != %d\n",
2649 from_kuid(&init_user_ns, current->cred->euid),
2650 from_kuid(&init_user_ns, binder_context_mgr_uid));
2655 binder_context_mgr_uid = current->cred->euid;
2656 binder_context_mgr_node = binder_new_node(proc, NULL, NULL);
2657 if (binder_context_mgr_node == NULL) {
2661 binder_context_mgr_node->local_weak_refs++;
2662 binder_context_mgr_node->local_strong_refs++;
2663 binder_context_mgr_node->has_strong_ref = 1;
2664 binder_context_mgr_node->has_weak_ref = 1;
2666 case BINDER_THREAD_EXIT:
2667 binder_debug(BINDER_DEBUG_THREADS, "binder: %d:%d exit\n",
2668 proc->pid, thread->pid);
2669 binder_free_thread(proc, thread);
2672 case BINDER_VERSION:
2673 if (size != sizeof(struct binder_version)) {
2677 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION, &((struct binder_version *)ubuf)->protocol_version)) {
2689 thread->looper &= ~BINDER_LOOPER_STATE_NEED_RETURN;
2690 mutex_unlock(&binder_lock);
2691 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2692 if (ret && ret != -ERESTARTSYS)
2693 pr_info("binder: %d:%d ioctl %x %lx returned %d\n", proc->pid, current->pid, cmd, arg, ret);
2697 static void binder_vma_open(struct vm_area_struct *vma)
2699 struct binder_proc *proc = vma->vm_private_data;
2700 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
2701 "binder: %d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
2702 proc->pid, vma->vm_start, vma->vm_end,
2703 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2704 (unsigned long)pgprot_val(vma->vm_page_prot));
2707 static void binder_vma_close(struct vm_area_struct *vma)
2709 struct binder_proc *proc = vma->vm_private_data;
2710 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
2711 "binder: %d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
2712 proc->pid, vma->vm_start, vma->vm_end,
2713 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2714 (unsigned long)pgprot_val(vma->vm_page_prot));
2716 proc->vma_vm_mm = NULL;
2717 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
2720 static struct vm_operations_struct binder_vm_ops = {
2721 .open = binder_vma_open,
2722 .close = binder_vma_close,
2725 static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
2728 struct vm_struct *area;
2729 struct binder_proc *proc = filp->private_data;
2730 const char *failure_string;
2731 struct binder_buffer *buffer;
2733 if (proc->tsk != current)
2736 if ((vma->vm_end - vma->vm_start) > SZ_4M)
2737 vma->vm_end = vma->vm_start + SZ_4M;
2739 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
2740 "binder_mmap: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
2741 proc->pid, vma->vm_start, vma->vm_end,
2742 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2743 (unsigned long)pgprot_val(vma->vm_page_prot));
2745 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
2747 failure_string = "bad vm_flags";
2750 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
2752 mutex_lock(&binder_mmap_lock);
2755 failure_string = "already mapped";
2756 goto err_already_mapped;
2759 area = get_vm_area(vma->vm_end - vma->vm_start, VM_IOREMAP);
2762 failure_string = "get_vm_area";
2763 goto err_get_vm_area_failed;
2765 proc->buffer = area->addr;
2766 proc->user_buffer_offset = vma->vm_start - (uintptr_t)proc->buffer;
2767 mutex_unlock(&binder_mmap_lock);
2769 #ifdef CONFIG_CPU_CACHE_VIPT
2770 if (cache_is_vipt_aliasing()) {
2771 while (CACHE_COLOUR((vma->vm_start ^ (uint32_t)proc->buffer))) {
2772 pr_info("binder_mmap: %d %lx-%lx maps %p bad alignment\n", proc->pid, vma->vm_start, vma->vm_end, proc->buffer);
2773 vma->vm_start += PAGE_SIZE;
2777 proc->pages = kzalloc(sizeof(proc->pages[0]) * ((vma->vm_end - vma->vm_start) / PAGE_SIZE), GFP_KERNEL);
2778 if (proc->pages == NULL) {
2780 failure_string = "alloc page array";
2781 goto err_alloc_pages_failed;
2783 proc->buffer_size = vma->vm_end - vma->vm_start;
2785 vma->vm_ops = &binder_vm_ops;
2786 vma->vm_private_data = proc;
2788 if (binder_update_page_range(proc, 1, proc->buffer, proc->buffer + PAGE_SIZE, vma)) {
2790 failure_string = "alloc small buf";
2791 goto err_alloc_small_buf_failed;
2793 buffer = proc->buffer;
2794 INIT_LIST_HEAD(&proc->buffers);
2795 list_add(&buffer->entry, &proc->buffers);
2797 binder_insert_free_buffer(proc, buffer);
2798 proc->free_async_space = proc->buffer_size / 2;
2800 proc->files = get_files_struct(current);
2802 proc->vma_vm_mm = vma->vm_mm;
2804 /*pr_info("binder_mmap: %d %lx-%lx maps %p\n",
2805 proc->pid, vma->vm_start, vma->vm_end, proc->buffer);*/
2808 err_alloc_small_buf_failed:
2811 err_alloc_pages_failed:
2812 mutex_lock(&binder_mmap_lock);
2813 vfree(proc->buffer);
2814 proc->buffer = NULL;
2815 err_get_vm_area_failed:
2817 mutex_unlock(&binder_mmap_lock);
2819 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
2820 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
2824 static int binder_open(struct inode *nodp, struct file *filp)
2826 struct binder_proc *proc;
2828 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
2829 current->group_leader->pid, current->pid);
2831 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
2834 get_task_struct(current);
2835 proc->tsk = current;
2836 INIT_LIST_HEAD(&proc->todo);
2837 init_waitqueue_head(&proc->wait);
2838 proc->default_priority = task_nice(current);
2839 mutex_lock(&binder_lock);
2840 binder_stats_created(BINDER_STAT_PROC);
2841 hlist_add_head(&proc->proc_node, &binder_procs);
2842 proc->pid = current->group_leader->pid;
2843 INIT_LIST_HEAD(&proc->delivered_death);
2844 filp->private_data = proc;
2845 mutex_unlock(&binder_lock);
2847 if (binder_debugfs_dir_entry_proc) {
2849 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
2850 proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
2851 binder_debugfs_dir_entry_proc, proc, &binder_proc_fops);
2857 static int binder_flush(struct file *filp, fl_owner_t id)
2859 struct binder_proc *proc = filp->private_data;
2861 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
2866 static void binder_deferred_flush(struct binder_proc *proc)
2870 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
2871 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
2872 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
2873 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
2874 wake_up_interruptible(&thread->wait);
2878 wake_up_interruptible_all(&proc->wait);
2880 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
2881 "binder_flush: %d woke %d threads\n", proc->pid,
2885 static int binder_release(struct inode *nodp, struct file *filp)
2887 struct binder_proc *proc = filp->private_data;
2888 debugfs_remove(proc->debugfs_entry);
2889 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
2894 static void binder_deferred_release(struct binder_proc *proc)
2896 struct hlist_node *pos;
2897 struct binder_transaction *t;
2899 int threads, nodes, incoming_refs, outgoing_refs, buffers, active_transactions, page_count;
2902 BUG_ON(proc->files);
2904 hlist_del(&proc->proc_node);
2905 if (binder_context_mgr_node && binder_context_mgr_node->proc == proc) {
2906 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2907 "binder_release: %d context_mgr_node gone\n",
2909 binder_context_mgr_node = NULL;
2913 active_transactions = 0;
2914 while ((n = rb_first(&proc->threads))) {
2915 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
2917 active_transactions += binder_free_thread(proc, thread);
2921 while ((n = rb_first(&proc->nodes))) {
2922 struct binder_node *node = rb_entry(n, struct binder_node, rb_node);
2925 rb_erase(&node->rb_node, &proc->nodes);
2926 list_del_init(&node->work.entry);
2927 binder_release_work(&node->async_todo);
2928 if (hlist_empty(&node->refs)) {
2930 binder_stats_deleted(BINDER_STAT_NODE);
2932 struct binder_ref *ref;
2936 node->local_strong_refs = 0;
2937 node->local_weak_refs = 0;
2938 hlist_add_head(&node->dead_node, &binder_dead_nodes);
2940 hlist_for_each_entry(ref, pos, &node->refs, node_entry) {
2944 if (list_empty(&ref->death->work.entry)) {
2945 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
2946 list_add_tail(&ref->death->work.entry, &ref->proc->todo);
2947 wake_up_interruptible(&ref->proc->wait);
2952 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2953 "binder: node %d now dead, "
2954 "refs %d, death %d\n", node->debug_id,
2955 incoming_refs, death);
2959 while ((n = rb_first(&proc->refs_by_desc))) {
2960 struct binder_ref *ref = rb_entry(n, struct binder_ref,
2963 binder_delete_ref(ref);
2965 binder_release_work(&proc->todo);
2966 binder_release_work(&proc->delivered_death);
2969 while ((n = rb_first(&proc->allocated_buffers))) {
2970 struct binder_buffer *buffer = rb_entry(n, struct binder_buffer,
2972 t = buffer->transaction;
2975 buffer->transaction = NULL;
2976 pr_err("binder: release proc %d, "
2977 "transaction %d, not freed\n",
2978 proc->pid, t->debug_id);
2981 binder_free_buf(proc, buffer);
2985 binder_stats_deleted(BINDER_STAT_PROC);
2990 for (i = 0; i < proc->buffer_size / PAGE_SIZE; i++) {
2991 if (proc->pages[i]) {
2992 void *page_addr = proc->buffer + i * PAGE_SIZE;
2993 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
2994 "binder_release: %d: "
2995 "page %d at %p not freed\n",
2998 unmap_kernel_range((unsigned long)page_addr,
3000 __free_page(proc->pages[i]);
3005 vfree(proc->buffer);
3008 put_task_struct(proc->tsk);
3010 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
3011 "binder_release: %d threads %d, nodes %d (ref %d), "
3012 "refs %d, active transactions %d, buffers %d, "
3014 proc->pid, threads, nodes, incoming_refs, outgoing_refs,
3015 active_transactions, buffers, page_count);
3020 static void binder_deferred_func(struct work_struct *work)
3022 struct binder_proc *proc;
3023 struct files_struct *files;
3027 mutex_lock(&binder_lock);
3028 mutex_lock(&binder_deferred_lock);
3029 if (!hlist_empty(&binder_deferred_list)) {
3030 proc = hlist_entry(binder_deferred_list.first,
3031 struct binder_proc, deferred_work_node);
3032 hlist_del_init(&proc->deferred_work_node);
3033 defer = proc->deferred_work;
3034 proc->deferred_work = 0;
3039 mutex_unlock(&binder_deferred_lock);
3042 if (defer & BINDER_DEFERRED_PUT_FILES) {
3043 files = proc->files;
3048 if (defer & BINDER_DEFERRED_FLUSH)
3049 binder_deferred_flush(proc);
3051 if (defer & BINDER_DEFERRED_RELEASE)
3052 binder_deferred_release(proc); /* frees proc */
3054 mutex_unlock(&binder_lock);
3056 put_files_struct(files);
3059 static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
3062 binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
3064 mutex_lock(&binder_deferred_lock);
3065 proc->deferred_work |= defer;
3066 if (hlist_unhashed(&proc->deferred_work_node)) {
3067 hlist_add_head(&proc->deferred_work_node,
3068 &binder_deferred_list);
3069 queue_work(binder_deferred_workqueue, &binder_deferred_work);
3071 mutex_unlock(&binder_deferred_lock);
3074 static void print_binder_transaction(struct seq_file *m, const char *prefix,
3075 struct binder_transaction *t)
3078 "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %ld r%d",
3079 prefix, t->debug_id, t,
3080 t->from ? t->from->proc->pid : 0,
3081 t->from ? t->from->pid : 0,
3082 t->to_proc ? t->to_proc->pid : 0,
3083 t->to_thread ? t->to_thread->pid : 0,
3084 t->code, t->flags, t->priority, t->need_reply);
3085 if (t->buffer == NULL) {
3086 seq_puts(m, " buffer free\n");
3089 if (t->buffer->target_node)
3090 seq_printf(m, " node %d",
3091 t->buffer->target_node->debug_id);
3092 seq_printf(m, " size %zd:%zd data %p\n",
3093 t->buffer->data_size, t->buffer->offsets_size,
3097 static void print_binder_buffer(struct seq_file *m, const char *prefix,
3098 struct binder_buffer *buffer)
3100 seq_printf(m, "%s %d: %p size %zd:%zd %s\n",
3101 prefix, buffer->debug_id, buffer->data,
3102 buffer->data_size, buffer->offsets_size,
3103 buffer->transaction ? "active" : "delivered");
3106 static void print_binder_work(struct seq_file *m, const char *prefix,
3107 const char *transaction_prefix,
3108 struct binder_work *w)
3110 struct binder_node *node;
3111 struct binder_transaction *t;
3114 case BINDER_WORK_TRANSACTION:
3115 t = container_of(w, struct binder_transaction, work);
3116 print_binder_transaction(m, transaction_prefix, t);
3118 case BINDER_WORK_TRANSACTION_COMPLETE:
3119 seq_printf(m, "%stransaction complete\n", prefix);
3121 case BINDER_WORK_NODE:
3122 node = container_of(w, struct binder_node, work);
3123 seq_printf(m, "%snode work %d: u%p c%p\n",
3124 prefix, node->debug_id, node->ptr, node->cookie);
3126 case BINDER_WORK_DEAD_BINDER:
3127 seq_printf(m, "%shas dead binder\n", prefix);
3129 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
3130 seq_printf(m, "%shas cleared dead binder\n", prefix);
3132 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
3133 seq_printf(m, "%shas cleared death notification\n", prefix);
3136 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
3141 static void print_binder_thread(struct seq_file *m,
3142 struct binder_thread *thread,
3145 struct binder_transaction *t;
3146 struct binder_work *w;
3147 size_t start_pos = m->count;
3150 seq_printf(m, " thread %d: l %02x\n", thread->pid, thread->looper);
3151 header_pos = m->count;
3152 t = thread->transaction_stack;
3154 if (t->from == thread) {
3155 print_binder_transaction(m,
3156 " outgoing transaction", t);
3158 } else if (t->to_thread == thread) {
3159 print_binder_transaction(m,
3160 " incoming transaction", t);
3163 print_binder_transaction(m, " bad transaction", t);
3167 list_for_each_entry(w, &thread->todo, entry) {
3168 print_binder_work(m, " ", " pending transaction", w);
3170 if (!print_always && m->count == header_pos)
3171 m->count = start_pos;
3174 static void print_binder_node(struct seq_file *m, struct binder_node *node)
3176 struct binder_ref *ref;
3177 struct hlist_node *pos;
3178 struct binder_work *w;
3182 hlist_for_each_entry(ref, pos, &node->refs, node_entry)
3185 seq_printf(m, " node %d: u%p c%p hs %d hw %d ls %d lw %d is %d iw %d",
3186 node->debug_id, node->ptr, node->cookie,
3187 node->has_strong_ref, node->has_weak_ref,
3188 node->local_strong_refs, node->local_weak_refs,
3189 node->internal_strong_refs, count);
3191 seq_puts(m, " proc");
3192 hlist_for_each_entry(ref, pos, &node->refs, node_entry)
3193 seq_printf(m, " %d", ref->proc->pid);
3196 list_for_each_entry(w, &node->async_todo, entry)
3197 print_binder_work(m, " ",
3198 " pending async transaction", w);
3201 static void print_binder_ref(struct seq_file *m, struct binder_ref *ref)
3203 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %p\n",
3204 ref->debug_id, ref->desc, ref->node->proc ? "" : "dead ",
3205 ref->node->debug_id, ref->strong, ref->weak, ref->death);
3208 static void print_binder_proc(struct seq_file *m,
3209 struct binder_proc *proc, int print_all)
3211 struct binder_work *w;
3213 size_t start_pos = m->count;
3216 seq_printf(m, "proc %d\n", proc->pid);
3217 header_pos = m->count;
3219 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3220 print_binder_thread(m, rb_entry(n, struct binder_thread,
3221 rb_node), print_all);
3222 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
3223 struct binder_node *node = rb_entry(n, struct binder_node,
3225 if (print_all || node->has_async_transaction)
3226 print_binder_node(m, node);
3229 for (n = rb_first(&proc->refs_by_desc);
3232 print_binder_ref(m, rb_entry(n, struct binder_ref,
3235 for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
3236 print_binder_buffer(m, " buffer",
3237 rb_entry(n, struct binder_buffer, rb_node));
3238 list_for_each_entry(w, &proc->todo, entry)
3239 print_binder_work(m, " ", " pending transaction", w);
3240 list_for_each_entry(w, &proc->delivered_death, entry) {
3241 seq_puts(m, " has delivered dead binder\n");
3244 if (!print_all && m->count == header_pos)
3245 m->count = start_pos;
3248 static const char *binder_return_strings[] = {
3253 "BR_ACQUIRE_RESULT",
3255 "BR_TRANSACTION_COMPLETE",
3260 "BR_ATTEMPT_ACQUIRE",
3265 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
3269 static const char *binder_command_strings[] = {
3272 "BC_ACQUIRE_RESULT",
3280 "BC_ATTEMPT_ACQUIRE",
3281 "BC_REGISTER_LOOPER",
3284 "BC_REQUEST_DEATH_NOTIFICATION",
3285 "BC_CLEAR_DEATH_NOTIFICATION",
3286 "BC_DEAD_BINDER_DONE"
3289 static const char *binder_objstat_strings[] = {
3296 "transaction_complete"
3299 static void print_binder_stats(struct seq_file *m, const char *prefix,
3300 struct binder_stats *stats)
3304 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
3305 ARRAY_SIZE(binder_command_strings));
3306 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
3308 seq_printf(m, "%s%s: %d\n", prefix,
3309 binder_command_strings[i], stats->bc[i]);
3312 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
3313 ARRAY_SIZE(binder_return_strings));
3314 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
3316 seq_printf(m, "%s%s: %d\n", prefix,
3317 binder_return_strings[i], stats->br[i]);
3320 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
3321 ARRAY_SIZE(binder_objstat_strings));
3322 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
3323 ARRAY_SIZE(stats->obj_deleted));
3324 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
3325 if (stats->obj_created[i] || stats->obj_deleted[i])
3326 seq_printf(m, "%s%s: active %d total %d\n", prefix,
3327 binder_objstat_strings[i],
3328 stats->obj_created[i] - stats->obj_deleted[i],
3329 stats->obj_created[i]);
3333 static void print_binder_proc_stats(struct seq_file *m,
3334 struct binder_proc *proc)
3336 struct binder_work *w;
3338 int count, strong, weak;
3340 seq_printf(m, "proc %d\n", proc->pid);
3342 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3344 seq_printf(m, " threads: %d\n", count);
3345 seq_printf(m, " requested threads: %d+%d/%d\n"
3346 " ready threads %d\n"
3347 " free async space %zd\n", proc->requested_threads,
3348 proc->requested_threads_started, proc->max_threads,
3349 proc->ready_threads, proc->free_async_space);
3351 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
3353 seq_printf(m, " nodes: %d\n", count);
3357 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
3358 struct binder_ref *ref = rb_entry(n, struct binder_ref,
3361 strong += ref->strong;
3364 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
3367 for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
3369 seq_printf(m, " buffers: %d\n", count);
3372 list_for_each_entry(w, &proc->todo, entry) {
3374 case BINDER_WORK_TRANSACTION:
3381 seq_printf(m, " pending transactions: %d\n", count);
3383 print_binder_stats(m, " ", &proc->stats);
3387 static int binder_state_show(struct seq_file *m, void *unused)
3389 struct binder_proc *proc;
3390 struct hlist_node *pos;
3391 struct binder_node *node;
3392 int do_lock = !binder_debug_no_lock;
3395 mutex_lock(&binder_lock);
3397 seq_puts(m, "binder state:\n");
3399 if (!hlist_empty(&binder_dead_nodes))
3400 seq_puts(m, "dead nodes:\n");
3401 hlist_for_each_entry(node, pos, &binder_dead_nodes, dead_node)
3402 print_binder_node(m, node);
3404 hlist_for_each_entry(proc, pos, &binder_procs, proc_node)
3405 print_binder_proc(m, proc, 1);
3407 mutex_unlock(&binder_lock);
3411 static int binder_stats_show(struct seq_file *m, void *unused)
3413 struct binder_proc *proc;
3414 struct hlist_node *pos;
3415 int do_lock = !binder_debug_no_lock;
3418 mutex_lock(&binder_lock);
3420 seq_puts(m, "binder stats:\n");
3422 print_binder_stats(m, "", &binder_stats);
3424 hlist_for_each_entry(proc, pos, &binder_procs, proc_node)
3425 print_binder_proc_stats(m, proc);
3427 mutex_unlock(&binder_lock);
3431 static int binder_transactions_show(struct seq_file *m, void *unused)
3433 struct binder_proc *proc;
3434 struct hlist_node *pos;
3435 int do_lock = !binder_debug_no_lock;
3438 mutex_lock(&binder_lock);
3440 seq_puts(m, "binder transactions:\n");
3441 hlist_for_each_entry(proc, pos, &binder_procs, proc_node)
3442 print_binder_proc(m, proc, 0);
3444 mutex_unlock(&binder_lock);
3448 static int binder_proc_show(struct seq_file *m, void *unused)
3450 struct binder_proc *proc = m->private;
3451 int do_lock = !binder_debug_no_lock;
3454 mutex_lock(&binder_lock);
3455 seq_puts(m, "binder proc state:\n");
3456 print_binder_proc(m, proc, 1);
3458 mutex_unlock(&binder_lock);
3462 static void print_binder_transaction_log_entry(struct seq_file *m,
3463 struct binder_transaction_log_entry *e)
3466 "%d: %s from %d:%d to %d:%d node %d handle %d size %d:%d\n",
3467 e->debug_id, (e->call_type == 2) ? "reply" :
3468 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
3469 e->from_thread, e->to_proc, e->to_thread, e->to_node,
3470 e->target_handle, e->data_size, e->offsets_size);
3473 static int binder_transaction_log_show(struct seq_file *m, void *unused)
3475 struct binder_transaction_log *log = m->private;
3479 for (i = log->next; i < ARRAY_SIZE(log->entry); i++)
3480 print_binder_transaction_log_entry(m, &log->entry[i]);
3482 for (i = 0; i < log->next; i++)
3483 print_binder_transaction_log_entry(m, &log->entry[i]);
3487 static const struct file_operations binder_fops = {
3488 .owner = THIS_MODULE,
3489 .poll = binder_poll,
3490 .unlocked_ioctl = binder_ioctl,
3491 .mmap = binder_mmap,
3492 .open = binder_open,
3493 .flush = binder_flush,
3494 .release = binder_release,
3497 static struct miscdevice binder_miscdev = {
3498 .minor = MISC_DYNAMIC_MINOR,
3500 .fops = &binder_fops
3503 BINDER_DEBUG_ENTRY(state);
3504 BINDER_DEBUG_ENTRY(stats);
3505 BINDER_DEBUG_ENTRY(transactions);
3506 BINDER_DEBUG_ENTRY(transaction_log);
3508 static int __init binder_init(void)
3512 binder_deferred_workqueue = create_singlethread_workqueue("binder");
3513 if (!binder_deferred_workqueue)
3516 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
3517 if (binder_debugfs_dir_entry_root)
3518 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
3519 binder_debugfs_dir_entry_root);
3520 ret = misc_register(&binder_miscdev);
3521 if (binder_debugfs_dir_entry_root) {
3522 debugfs_create_file("state",
3524 binder_debugfs_dir_entry_root,
3526 &binder_state_fops);
3527 debugfs_create_file("stats",
3529 binder_debugfs_dir_entry_root,
3531 &binder_stats_fops);
3532 debugfs_create_file("transactions",
3534 binder_debugfs_dir_entry_root,
3536 &binder_transactions_fops);
3537 debugfs_create_file("transaction_log",
3539 binder_debugfs_dir_entry_root,
3540 &binder_transaction_log,
3541 &binder_transaction_log_fops);
3542 debugfs_create_file("failed_transaction_log",
3544 binder_debugfs_dir_entry_root,
3545 &binder_transaction_log_failed,
3546 &binder_transaction_log_fops);
3551 device_initcall(binder_init);
3553 MODULE_LICENSE("GPL v2");