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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 #include <asm/cacheflush.h>
21 #include <linux/fdtable.h>
22 #include <linux/file.h>
23 #include <linux/freezer.h>
25 #include <linux/list.h>
26 #include <linux/miscdevice.h>
28 #include <linux/module.h>
29 #include <linux/rtmutex.h>
30 #include <linux/mutex.h>
31 #include <linux/nsproxy.h>
32 #include <linux/poll.h>
33 #include <linux/debugfs.h>
34 #include <linux/rbtree.h>
35 #include <linux/sched.h>
36 #include <linux/seq_file.h>
37 #include <linux/uaccess.h>
38 #include <linux/vmalloc.h>
39 #include <linux/slab.h>
40 #include <linux/pid_namespace.h>
41 #include <linux/security.h>
44 #include "binder_trace.h"
46 static DEFINE_RT_MUTEX(binder_main_lock);
47 static DEFINE_MUTEX(binder_deferred_lock);
48 static DEFINE_MUTEX(binder_mmap_lock);
50 static HLIST_HEAD(binder_procs);
51 static HLIST_HEAD(binder_deferred_list);
52 static HLIST_HEAD(binder_dead_nodes);
54 static struct dentry *binder_debugfs_dir_entry_root;
55 static struct dentry *binder_debugfs_dir_entry_proc;
56 static struct binder_node *binder_context_mgr_node;
57 static kuid_t binder_context_mgr_uid = INVALID_UID;
58 static int binder_last_id;
59 static struct workqueue_struct *binder_deferred_workqueue;
61 #define BINDER_DEBUG_ENTRY(name) \
62 static int binder_##name##_open(struct inode *inode, struct file *file) \
64 return single_open(file, binder_##name##_show, inode->i_private); \
67 static const struct file_operations binder_##name##_fops = { \
68 .owner = THIS_MODULE, \
69 .open = binder_##name##_open, \
71 .llseek = seq_lseek, \
72 .release = single_release, \
75 static int binder_proc_show(struct seq_file *m, void *unused);
76 BINDER_DEBUG_ENTRY(proc);
78 /* This is only defined in include/asm-arm/sizes.h */
84 #define SZ_4M 0x400000
87 #define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
89 #define BINDER_SMALL_BUF_SIZE (PAGE_SIZE * 64)
92 BINDER_DEBUG_USER_ERROR = 1U << 0,
93 BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1,
94 BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2,
95 BINDER_DEBUG_OPEN_CLOSE = 1U << 3,
96 BINDER_DEBUG_DEAD_BINDER = 1U << 4,
97 BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5,
98 BINDER_DEBUG_READ_WRITE = 1U << 6,
99 BINDER_DEBUG_USER_REFS = 1U << 7,
100 BINDER_DEBUG_THREADS = 1U << 8,
101 BINDER_DEBUG_TRANSACTION = 1U << 9,
102 BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10,
103 BINDER_DEBUG_FREE_BUFFER = 1U << 11,
104 BINDER_DEBUG_INTERNAL_REFS = 1U << 12,
105 BINDER_DEBUG_BUFFER_ALLOC = 1U << 13,
106 BINDER_DEBUG_PRIORITY_CAP = 1U << 14,
107 BINDER_DEBUG_BUFFER_ALLOC_ASYNC = 1U << 15,
109 static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR |
110 BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION;
111 module_param_named(debug_mask, binder_debug_mask, uint, S_IWUSR | S_IRUGO);
113 static bool binder_debug_no_lock;
114 module_param_named(proc_no_lock, binder_debug_no_lock, bool, S_IWUSR | S_IRUGO);
116 static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
117 static int binder_stop_on_user_error;
119 static int binder_set_stop_on_user_error(const char *val,
120 struct kernel_param *kp)
123 ret = param_set_int(val, kp);
124 if (binder_stop_on_user_error < 2)
125 wake_up(&binder_user_error_wait);
128 module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
129 param_get_int, &binder_stop_on_user_error, S_IWUSR | S_IRUGO);
131 #define binder_debug(mask, x...) \
133 if (binder_debug_mask & mask) \
137 #define binder_user_error(x...) \
139 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
141 if (binder_stop_on_user_error) \
142 binder_stop_on_user_error = 2; \
145 enum binder_stat_types {
151 BINDER_STAT_TRANSACTION,
152 BINDER_STAT_TRANSACTION_COMPLETE,
156 struct binder_stats {
157 int br[_IOC_NR(BR_FAILED_REPLY) + 1];
158 int bc[_IOC_NR(BC_DEAD_BINDER_DONE) + 1];
159 int obj_created[BINDER_STAT_COUNT];
160 int obj_deleted[BINDER_STAT_COUNT];
163 static struct binder_stats binder_stats;
165 static inline void binder_stats_deleted(enum binder_stat_types type)
167 binder_stats.obj_deleted[type]++;
170 static inline void binder_stats_created(enum binder_stat_types type)
172 binder_stats.obj_created[type]++;
175 struct binder_transaction_log_entry {
187 struct binder_transaction_log {
190 struct binder_transaction_log_entry entry[32];
192 static struct binder_transaction_log binder_transaction_log;
193 static struct binder_transaction_log binder_transaction_log_failed;
195 static struct binder_transaction_log_entry *binder_transaction_log_add(
196 struct binder_transaction_log *log)
198 struct binder_transaction_log_entry *e;
199 e = &log->entry[log->next];
200 memset(e, 0, sizeof(*e));
202 if (log->next == ARRAY_SIZE(log->entry)) {
210 struct list_head entry;
212 BINDER_WORK_TRANSACTION = 1,
213 BINDER_WORK_TRANSACTION_COMPLETE,
215 BINDER_WORK_DEAD_BINDER,
216 BINDER_WORK_DEAD_BINDER_AND_CLEAR,
217 BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
223 struct binder_work work;
225 struct rb_node rb_node;
226 struct hlist_node dead_node;
228 struct binder_proc *proc;
229 struct hlist_head refs;
230 int internal_strong_refs;
232 int local_strong_refs;
233 binder_uintptr_t ptr;
234 binder_uintptr_t cookie;
235 unsigned has_strong_ref:1;
236 unsigned pending_strong_ref:1;
237 unsigned has_weak_ref:1;
238 unsigned pending_weak_ref:1;
239 unsigned has_async_transaction:1;
240 unsigned accept_fds:1;
241 unsigned min_priority:8;
242 struct list_head async_todo;
245 struct binder_ref_death {
246 struct binder_work work;
247 binder_uintptr_t cookie;
251 /* Lookups needed: */
252 /* node + proc => ref (transaction) */
253 /* desc + proc => ref (transaction, inc/dec ref) */
254 /* node => refs + procs (proc exit) */
256 struct rb_node rb_node_desc;
257 struct rb_node rb_node_node;
258 struct hlist_node node_entry;
259 struct binder_proc *proc;
260 struct binder_node *node;
264 struct binder_ref_death *death;
267 struct binder_buffer {
268 struct list_head entry; /* free and allocated entries by address */
269 struct rb_node rb_node; /* free entry by size or allocated entry */
272 unsigned allow_user_free:1;
273 unsigned async_transaction:1;
274 unsigned debug_id:29;
276 struct binder_transaction *transaction;
278 struct binder_node *target_node;
284 enum binder_deferred_state {
285 BINDER_DEFERRED_PUT_FILES = 0x01,
286 BINDER_DEFERRED_FLUSH = 0x02,
287 BINDER_DEFERRED_RELEASE = 0x04,
291 struct hlist_node proc_node;
292 struct rb_root threads;
293 struct rb_root nodes;
294 struct rb_root refs_by_desc;
295 struct rb_root refs_by_node;
297 struct vm_area_struct *vma;
298 struct mm_struct *vma_vm_mm;
299 struct task_struct *tsk;
300 struct files_struct *files;
301 struct hlist_node deferred_work_node;
304 ptrdiff_t user_buffer_offset;
306 struct list_head buffers;
307 struct rb_root free_buffers;
308 struct rb_root allocated_buffers;
309 size_t free_async_space;
313 uint32_t buffer_free;
314 struct list_head todo;
315 wait_queue_head_t wait;
316 struct binder_stats stats;
317 struct list_head delivered_death;
319 int requested_threads;
320 int requested_threads_started;
322 long default_priority;
323 struct dentry *debugfs_entry;
327 BINDER_LOOPER_STATE_REGISTERED = 0x01,
328 BINDER_LOOPER_STATE_ENTERED = 0x02,
329 BINDER_LOOPER_STATE_EXITED = 0x04,
330 BINDER_LOOPER_STATE_INVALID = 0x08,
331 BINDER_LOOPER_STATE_WAITING = 0x10,
332 BINDER_LOOPER_STATE_NEED_RETURN = 0x20
335 struct binder_thread {
336 struct binder_proc *proc;
337 struct rb_node rb_node;
340 struct binder_transaction *transaction_stack;
341 struct list_head todo;
342 uint32_t return_error; /* Write failed, return error code in read buf */
343 uint32_t return_error2; /* Write failed, return error code in read */
344 /* buffer. Used when sending a reply to a dead process that */
345 /* we are also waiting on */
346 wait_queue_head_t wait;
347 struct binder_stats stats;
350 struct binder_transaction {
352 struct binder_work work;
353 struct binder_thread *from;
354 struct binder_transaction *from_parent;
355 struct binder_proc *to_proc;
356 struct binder_thread *to_thread;
357 struct binder_transaction *to_parent;
358 unsigned need_reply:1;
359 /* unsigned is_dead:1; */ /* not used at the moment */
361 struct binder_buffer *buffer;
370 binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
372 static int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
374 struct files_struct *files = proc->files;
375 unsigned long rlim_cur;
381 if (!lock_task_sighand(proc->tsk, &irqs))
384 rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
385 unlock_task_sighand(proc->tsk, &irqs);
387 return __alloc_fd(files, 0, rlim_cur, flags);
391 * copied from fd_install
393 static void task_fd_install(
394 struct binder_proc *proc, unsigned int fd, struct file *file)
397 __fd_install(proc->files, fd, file);
401 * copied from sys_close
403 static long task_close_fd(struct binder_proc *proc, unsigned int fd)
407 if (proc->files == NULL)
410 retval = __close_fd(proc->files, fd);
411 /* can't restart close syscall because file table entry was cleared */
412 if (unlikely(retval == -ERESTARTSYS ||
413 retval == -ERESTARTNOINTR ||
414 retval == -ERESTARTNOHAND ||
415 retval == -ERESTART_RESTARTBLOCK))
421 static inline void binder_lock(const char *tag)
423 trace_binder_lock(tag);
424 rt_mutex_lock(&binder_main_lock);
425 trace_binder_locked(tag);
428 static inline void binder_unlock(const char *tag)
430 trace_binder_unlock(tag);
431 rt_mutex_unlock(&binder_main_lock);
434 static void binder_set_nice(long nice)
437 if (can_nice(current, nice)) {
438 set_user_nice(current, nice);
441 min_nice = 20 - current->signal->rlim[RLIMIT_NICE].rlim_cur;
442 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
443 "%d: nice value %ld not allowed use %ld instead\n",
444 current->pid, nice, min_nice);
445 set_user_nice(current, min_nice);
448 binder_user_error("%d RLIMIT_NICE not set\n", current->pid);
451 static size_t binder_buffer_size(struct binder_proc *proc,
452 struct binder_buffer *buffer)
454 if (list_is_last(&buffer->entry, &proc->buffers))
455 return proc->buffer + proc->buffer_size - (void *)buffer->data;
457 return (size_t)list_entry(buffer->entry.next,
458 struct binder_buffer, entry) - (size_t)buffer->data;
461 static void binder_insert_free_buffer(struct binder_proc *proc,
462 struct binder_buffer *new_buffer)
464 struct rb_node **p = &proc->free_buffers.rb_node;
465 struct rb_node *parent = NULL;
466 struct binder_buffer *buffer;
468 size_t new_buffer_size;
470 BUG_ON(!new_buffer->free);
472 new_buffer_size = binder_buffer_size(proc, new_buffer);
474 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
475 "%d: add free buffer, size %zd, at %p\n",
476 proc->pid, new_buffer_size, new_buffer);
480 buffer = rb_entry(parent, struct binder_buffer, rb_node);
481 BUG_ON(!buffer->free);
483 buffer_size = binder_buffer_size(proc, buffer);
485 if (new_buffer_size < buffer_size)
486 p = &parent->rb_left;
488 p = &parent->rb_right;
490 rb_link_node(&new_buffer->rb_node, parent, p);
491 rb_insert_color(&new_buffer->rb_node, &proc->free_buffers);
494 static void binder_insert_allocated_buffer(struct binder_proc *proc,
495 struct binder_buffer *new_buffer)
497 struct rb_node **p = &proc->allocated_buffers.rb_node;
498 struct rb_node *parent = NULL;
499 struct binder_buffer *buffer;
501 BUG_ON(new_buffer->free);
505 buffer = rb_entry(parent, struct binder_buffer, rb_node);
506 BUG_ON(buffer->free);
508 if (new_buffer < buffer)
509 p = &parent->rb_left;
510 else if (new_buffer > buffer)
511 p = &parent->rb_right;
515 rb_link_node(&new_buffer->rb_node, parent, p);
516 rb_insert_color(&new_buffer->rb_node, &proc->allocated_buffers);
519 static struct binder_buffer *binder_buffer_lookup(struct binder_proc *proc,
522 struct rb_node *n = proc->allocated_buffers.rb_node;
523 struct binder_buffer *buffer;
524 struct binder_buffer *kern_ptr;
526 kern_ptr = (struct binder_buffer *)(user_ptr - proc->user_buffer_offset
527 - offsetof(struct binder_buffer, data));
530 buffer = rb_entry(n, struct binder_buffer, rb_node);
531 BUG_ON(buffer->free);
533 if (kern_ptr < buffer)
535 else if (kern_ptr > buffer)
543 static int binder_update_page_range(struct binder_proc *proc, int allocate,
544 void *start, void *end,
545 struct vm_area_struct *vma)
548 unsigned long user_page_addr;
549 struct vm_struct tmp_area;
551 struct mm_struct *mm;
553 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
554 "%d: %s pages %p-%p\n", proc->pid,
555 allocate ? "allocate" : "free", start, end);
560 trace_binder_update_page_range(proc, allocate, start, end);
565 mm = get_task_mm(proc->tsk);
568 down_write(&mm->mmap_sem);
570 if (vma && mm != proc->vma_vm_mm) {
571 pr_err("%d: vma mm and task mm mismatch\n",
581 pr_err("%d: binder_alloc_buf failed to map pages in userspace, no vma\n",
586 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
588 struct page **page_array_ptr;
589 page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
592 *page = alloc_page(GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
594 pr_err("%d: binder_alloc_buf failed for page at %p\n",
595 proc->pid, page_addr);
596 goto err_alloc_page_failed;
598 tmp_area.addr = page_addr;
599 tmp_area.size = PAGE_SIZE + PAGE_SIZE /* guard page? */;
600 page_array_ptr = page;
601 ret = map_vm_area(&tmp_area, PAGE_KERNEL, &page_array_ptr);
603 pr_err("%d: binder_alloc_buf failed to map page at %p in kernel\n",
604 proc->pid, page_addr);
605 goto err_map_kernel_failed;
608 (uintptr_t)page_addr + proc->user_buffer_offset;
609 ret = vm_insert_page(vma, user_page_addr, page[0]);
611 pr_err("%d: binder_alloc_buf failed to map page at %lx in userspace\n",
612 proc->pid, user_page_addr);
613 goto err_vm_insert_page_failed;
615 /* vm_insert_page does not seem to increment the refcount */
618 up_write(&mm->mmap_sem);
624 for (page_addr = end - PAGE_SIZE; page_addr >= start;
625 page_addr -= PAGE_SIZE) {
626 page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
628 zap_page_range(vma, (uintptr_t)page_addr +
629 proc->user_buffer_offset, PAGE_SIZE, NULL);
630 err_vm_insert_page_failed:
631 unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
632 err_map_kernel_failed:
635 err_alloc_page_failed:
640 up_write(&mm->mmap_sem);
646 static struct binder_buffer *binder_alloc_buf(struct binder_proc *proc,
648 size_t offsets_size, int is_async)
650 struct rb_node *n = proc->free_buffers.rb_node;
651 struct binder_buffer *buffer;
653 struct rb_node *best_fit = NULL;
658 if (proc->vma == NULL) {
659 pr_err("%d: binder_alloc_buf, no vma\n",
664 size = ALIGN(data_size, sizeof(void *)) +
665 ALIGN(offsets_size, sizeof(void *));
667 if (size < data_size || size < offsets_size) {
668 binder_user_error("%d: got transaction with invalid size %zd-%zd\n",
669 proc->pid, data_size, offsets_size);
674 proc->free_async_space < size + sizeof(struct binder_buffer)) {
675 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
676 "%d: binder_alloc_buf size %zd failed, no async space left\n",
682 buffer = rb_entry(n, struct binder_buffer, rb_node);
683 BUG_ON(!buffer->free);
684 buffer_size = binder_buffer_size(proc, buffer);
686 if (size < buffer_size) {
689 } else if (size > buffer_size)
696 if (best_fit == NULL) {
697 pr_err("%d: binder_alloc_buf size %zd failed, no address space\n",
702 buffer = rb_entry(best_fit, struct binder_buffer, rb_node);
703 buffer_size = binder_buffer_size(proc, buffer);
706 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
707 "%d: binder_alloc_buf size %zd got buffer %p size %zd\n",
708 proc->pid, size, buffer, buffer_size);
711 (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK);
713 if (size + sizeof(struct binder_buffer) + 4 >= buffer_size)
714 buffer_size = size; /* no room for other buffers */
716 buffer_size = size + sizeof(struct binder_buffer);
719 (void *)PAGE_ALIGN((uintptr_t)buffer->data + buffer_size);
720 if (end_page_addr > has_page_addr)
721 end_page_addr = has_page_addr;
722 if (binder_update_page_range(proc, 1,
723 (void *)PAGE_ALIGN((uintptr_t)buffer->data), end_page_addr, NULL))
726 rb_erase(best_fit, &proc->free_buffers);
728 binder_insert_allocated_buffer(proc, buffer);
729 if (buffer_size != size) {
730 struct binder_buffer *new_buffer = (void *)buffer->data + size;
731 list_add(&new_buffer->entry, &buffer->entry);
732 new_buffer->free = 1;
733 binder_insert_free_buffer(proc, new_buffer);
735 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
736 "%d: binder_alloc_buf size %zd got %p\n",
737 proc->pid, size, buffer);
738 buffer->data_size = data_size;
739 buffer->offsets_size = offsets_size;
740 buffer->async_transaction = is_async;
742 proc->free_async_space -= size + sizeof(struct binder_buffer);
743 binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
744 "%d: binder_alloc_buf size %zd async free %zd\n",
745 proc->pid, size, proc->free_async_space);
751 static void *buffer_start_page(struct binder_buffer *buffer)
753 return (void *)((uintptr_t)buffer & PAGE_MASK);
756 static void *buffer_end_page(struct binder_buffer *buffer)
758 return (void *)(((uintptr_t)(buffer + 1) - 1) & PAGE_MASK);
761 static void binder_delete_free_buffer(struct binder_proc *proc,
762 struct binder_buffer *buffer)
764 struct binder_buffer *prev, *next = NULL;
765 int free_page_end = 1;
766 int free_page_start = 1;
768 BUG_ON(proc->buffers.next == &buffer->entry);
769 prev = list_entry(buffer->entry.prev, struct binder_buffer, entry);
771 if (buffer_end_page(prev) == buffer_start_page(buffer)) {
773 if (buffer_end_page(prev) == buffer_end_page(buffer))
775 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
776 "%d: merge free, buffer %p share page with %p\n",
777 proc->pid, buffer, prev);
780 if (!list_is_last(&buffer->entry, &proc->buffers)) {
781 next = list_entry(buffer->entry.next,
782 struct binder_buffer, entry);
783 if (buffer_start_page(next) == buffer_end_page(buffer)) {
785 if (buffer_start_page(next) ==
786 buffer_start_page(buffer))
788 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
789 "%d: merge free, buffer %p share page with %p\n",
790 proc->pid, buffer, prev);
793 list_del(&buffer->entry);
794 if (free_page_start || free_page_end) {
795 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
796 "%d: merge free, buffer %p do not share page%s%s with %p or %p\n",
797 proc->pid, buffer, free_page_start ? "" : " end",
798 free_page_end ? "" : " start", prev, next);
799 binder_update_page_range(proc, 0, free_page_start ?
800 buffer_start_page(buffer) : buffer_end_page(buffer),
801 (free_page_end ? buffer_end_page(buffer) :
802 buffer_start_page(buffer)) + PAGE_SIZE, NULL);
806 static void binder_free_buf(struct binder_proc *proc,
807 struct binder_buffer *buffer)
809 size_t size, buffer_size;
811 buffer_size = binder_buffer_size(proc, buffer);
813 size = ALIGN(buffer->data_size, sizeof(void *)) +
814 ALIGN(buffer->offsets_size, sizeof(void *));
816 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
817 "%d: binder_free_buf %p size %zd buffer_size %zd\n",
818 proc->pid, buffer, size, buffer_size);
820 BUG_ON(buffer->free);
821 BUG_ON(size > buffer_size);
822 BUG_ON(buffer->transaction != NULL);
823 BUG_ON((void *)buffer < proc->buffer);
824 BUG_ON((void *)buffer > proc->buffer + proc->buffer_size);
826 if (buffer->async_transaction) {
827 proc->free_async_space += size + sizeof(struct binder_buffer);
829 binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
830 "%d: binder_free_buf size %zd async free %zd\n",
831 proc->pid, size, proc->free_async_space);
834 binder_update_page_range(proc, 0,
835 (void *)PAGE_ALIGN((uintptr_t)buffer->data),
836 (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK),
838 rb_erase(&buffer->rb_node, &proc->allocated_buffers);
840 if (!list_is_last(&buffer->entry, &proc->buffers)) {
841 struct binder_buffer *next = list_entry(buffer->entry.next,
842 struct binder_buffer, entry);
844 rb_erase(&next->rb_node, &proc->free_buffers);
845 binder_delete_free_buffer(proc, next);
848 if (proc->buffers.next != &buffer->entry) {
849 struct binder_buffer *prev = list_entry(buffer->entry.prev,
850 struct binder_buffer, entry);
852 binder_delete_free_buffer(proc, buffer);
853 rb_erase(&prev->rb_node, &proc->free_buffers);
857 binder_insert_free_buffer(proc, buffer);
860 static struct binder_node *binder_get_node(struct binder_proc *proc,
861 binder_uintptr_t ptr)
863 struct rb_node *n = proc->nodes.rb_node;
864 struct binder_node *node;
867 node = rb_entry(n, struct binder_node, rb_node);
871 else if (ptr > node->ptr)
879 static struct binder_node *binder_new_node(struct binder_proc *proc,
880 binder_uintptr_t ptr,
881 binder_uintptr_t cookie)
883 struct rb_node **p = &proc->nodes.rb_node;
884 struct rb_node *parent = NULL;
885 struct binder_node *node;
889 node = rb_entry(parent, struct binder_node, rb_node);
893 else if (ptr > node->ptr)
899 node = kzalloc(sizeof(*node), GFP_KERNEL);
902 binder_stats_created(BINDER_STAT_NODE);
903 rb_link_node(&node->rb_node, parent, p);
904 rb_insert_color(&node->rb_node, &proc->nodes);
905 node->debug_id = ++binder_last_id;
908 node->cookie = cookie;
909 node->work.type = BINDER_WORK_NODE;
910 INIT_LIST_HEAD(&node->work.entry);
911 INIT_LIST_HEAD(&node->async_todo);
912 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
913 "%d:%d node %d u%016llx c%016llx created\n",
914 proc->pid, current->pid, node->debug_id,
915 (u64)node->ptr, (u64)node->cookie);
919 static int binder_inc_node(struct binder_node *node, int strong, int internal,
920 struct list_head *target_list)
924 if (target_list == NULL &&
925 node->internal_strong_refs == 0 &&
926 !(node == binder_context_mgr_node &&
927 node->has_strong_ref)) {
928 pr_err("invalid inc strong node for %d\n",
932 node->internal_strong_refs++;
934 node->local_strong_refs++;
935 if (!node->has_strong_ref && target_list) {
936 list_del_init(&node->work.entry);
937 list_add_tail(&node->work.entry, target_list);
941 node->local_weak_refs++;
942 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
943 if (target_list == NULL) {
944 pr_err("invalid inc weak node for %d\n",
948 list_add_tail(&node->work.entry, target_list);
954 static int binder_dec_node(struct binder_node *node, int strong, int internal)
958 node->internal_strong_refs--;
960 node->local_strong_refs--;
961 if (node->local_strong_refs || node->internal_strong_refs)
965 node->local_weak_refs--;
966 if (node->local_weak_refs || !hlist_empty(&node->refs))
969 if (node->proc && (node->has_strong_ref || node->has_weak_ref)) {
970 if (list_empty(&node->work.entry)) {
971 list_add_tail(&node->work.entry, &node->proc->todo);
972 wake_up_interruptible(&node->proc->wait);
975 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
976 !node->local_weak_refs) {
977 list_del_init(&node->work.entry);
979 rb_erase(&node->rb_node, &node->proc->nodes);
980 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
981 "refless node %d deleted\n",
984 hlist_del(&node->dead_node);
985 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
986 "dead node %d deleted\n",
990 binder_stats_deleted(BINDER_STAT_NODE);
998 static struct binder_ref *binder_get_ref(struct binder_proc *proc,
1001 struct rb_node *n = proc->refs_by_desc.rb_node;
1002 struct binder_ref *ref;
1005 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1007 if (desc < ref->desc)
1009 else if (desc > ref->desc)
1017 static struct binder_ref *binder_get_ref_for_node(struct binder_proc *proc,
1018 struct binder_node *node)
1021 struct rb_node **p = &proc->refs_by_node.rb_node;
1022 struct rb_node *parent = NULL;
1023 struct binder_ref *ref, *new_ref;
1027 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1029 if (node < ref->node)
1031 else if (node > ref->node)
1032 p = &(*p)->rb_right;
1036 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1037 if (new_ref == NULL)
1039 binder_stats_created(BINDER_STAT_REF);
1040 new_ref->debug_id = ++binder_last_id;
1041 new_ref->proc = proc;
1042 new_ref->node = node;
1043 rb_link_node(&new_ref->rb_node_node, parent, p);
1044 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1046 new_ref->desc = (node == binder_context_mgr_node) ? 0 : 1;
1047 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1048 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1049 if (ref->desc > new_ref->desc)
1051 new_ref->desc = ref->desc + 1;
1054 p = &proc->refs_by_desc.rb_node;
1057 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1059 if (new_ref->desc < ref->desc)
1061 else if (new_ref->desc > ref->desc)
1062 p = &(*p)->rb_right;
1066 rb_link_node(&new_ref->rb_node_desc, parent, p);
1067 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
1069 hlist_add_head(&new_ref->node_entry, &node->refs);
1071 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1072 "%d new ref %d desc %d for node %d\n",
1073 proc->pid, new_ref->debug_id, new_ref->desc,
1076 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1077 "%d new ref %d desc %d for dead node\n",
1078 proc->pid, new_ref->debug_id, new_ref->desc);
1083 static void binder_delete_ref(struct binder_ref *ref)
1085 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1086 "%d delete ref %d desc %d for node %d\n",
1087 ref->proc->pid, ref->debug_id, ref->desc,
1088 ref->node->debug_id);
1090 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1091 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
1093 binder_dec_node(ref->node, 1, 1);
1094 hlist_del(&ref->node_entry);
1095 binder_dec_node(ref->node, 0, 1);
1097 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1098 "%d delete ref %d desc %d has death notification\n",
1099 ref->proc->pid, ref->debug_id, ref->desc);
1100 list_del(&ref->death->work.entry);
1102 binder_stats_deleted(BINDER_STAT_DEATH);
1105 binder_stats_deleted(BINDER_STAT_REF);
1108 static int binder_inc_ref(struct binder_ref *ref, int strong,
1109 struct list_head *target_list)
1113 if (ref->strong == 0) {
1114 ret = binder_inc_node(ref->node, 1, 1, target_list);
1120 if (ref->weak == 0) {
1121 ret = binder_inc_node(ref->node, 0, 1, target_list);
1131 static int binder_dec_ref(struct binder_ref *ref, int strong)
1134 if (ref->strong == 0) {
1135 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
1136 ref->proc->pid, ref->debug_id,
1137 ref->desc, ref->strong, ref->weak);
1141 if (ref->strong == 0) {
1143 ret = binder_dec_node(ref->node, strong, 1);
1148 if (ref->weak == 0) {
1149 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
1150 ref->proc->pid, ref->debug_id,
1151 ref->desc, ref->strong, ref->weak);
1156 if (ref->strong == 0 && ref->weak == 0)
1157 binder_delete_ref(ref);
1161 static void binder_pop_transaction(struct binder_thread *target_thread,
1162 struct binder_transaction *t)
1164 if (target_thread) {
1165 BUG_ON(target_thread->transaction_stack != t);
1166 BUG_ON(target_thread->transaction_stack->from != target_thread);
1167 target_thread->transaction_stack =
1168 target_thread->transaction_stack->from_parent;
1173 t->buffer->transaction = NULL;
1175 binder_stats_deleted(BINDER_STAT_TRANSACTION);
1178 static void binder_send_failed_reply(struct binder_transaction *t,
1179 uint32_t error_code)
1181 struct binder_thread *target_thread;
1182 BUG_ON(t->flags & TF_ONE_WAY);
1184 target_thread = t->from;
1185 if (target_thread) {
1186 if (target_thread->return_error != BR_OK &&
1187 target_thread->return_error2 == BR_OK) {
1188 target_thread->return_error2 =
1189 target_thread->return_error;
1190 target_thread->return_error = BR_OK;
1192 if (target_thread->return_error == BR_OK) {
1193 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1194 "send failed reply for transaction %d to %d:%d\n",
1195 t->debug_id, target_thread->proc->pid,
1196 target_thread->pid);
1198 binder_pop_transaction(target_thread, t);
1199 target_thread->return_error = error_code;
1200 wake_up_interruptible(&target_thread->wait);
1202 pr_err("reply failed, target thread, %d:%d, has error code %d already\n",
1203 target_thread->proc->pid,
1205 target_thread->return_error);
1209 struct binder_transaction *next = t->from_parent;
1211 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1212 "send failed reply for transaction %d, target dead\n",
1215 binder_pop_transaction(target_thread, t);
1217 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1218 "reply failed, no target thread at root\n");
1222 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1223 "reply failed, no target thread -- retry %d\n",
1229 static void binder_transaction_buffer_release(struct binder_proc *proc,
1230 struct binder_buffer *buffer,
1231 binder_size_t *failed_at)
1233 binder_size_t *offp, *off_end;
1234 int debug_id = buffer->debug_id;
1236 binder_debug(BINDER_DEBUG_TRANSACTION,
1237 "%d buffer release %d, size %zd-%zd, failed at %p\n",
1238 proc->pid, buffer->debug_id,
1239 buffer->data_size, buffer->offsets_size, failed_at);
1241 if (buffer->target_node)
1242 binder_dec_node(buffer->target_node, 1, 0);
1244 offp = (binder_size_t *)(buffer->data +
1245 ALIGN(buffer->data_size, sizeof(void *)));
1247 off_end = failed_at;
1249 off_end = (void *)offp + buffer->offsets_size;
1250 for (; offp < off_end; offp++) {
1251 struct flat_binder_object *fp;
1252 if (*offp > buffer->data_size - sizeof(*fp) ||
1253 buffer->data_size < sizeof(*fp) ||
1254 !IS_ALIGNED(*offp, sizeof(u32))) {
1255 pr_err("transaction release %d bad offset %lld, size %zd\n",
1256 debug_id, (u64)*offp, buffer->data_size);
1259 fp = (struct flat_binder_object *)(buffer->data + *offp);
1261 case BINDER_TYPE_BINDER:
1262 case BINDER_TYPE_WEAK_BINDER: {
1263 struct binder_node *node = binder_get_node(proc, fp->binder);
1265 pr_err("transaction release %d bad node %016llx\n",
1266 debug_id, (u64)fp->binder);
1269 binder_debug(BINDER_DEBUG_TRANSACTION,
1270 " node %d u%016llx\n",
1271 node->debug_id, (u64)node->ptr);
1272 binder_dec_node(node, fp->type == BINDER_TYPE_BINDER, 0);
1274 case BINDER_TYPE_HANDLE:
1275 case BINDER_TYPE_WEAK_HANDLE: {
1276 struct binder_ref *ref = binder_get_ref(proc, fp->handle);
1278 pr_err("transaction release %d bad handle %d\n",
1279 debug_id, fp->handle);
1282 binder_debug(BINDER_DEBUG_TRANSACTION,
1283 " ref %d desc %d (node %d)\n",
1284 ref->debug_id, ref->desc, ref->node->debug_id);
1285 binder_dec_ref(ref, fp->type == BINDER_TYPE_HANDLE);
1288 case BINDER_TYPE_FD:
1289 binder_debug(BINDER_DEBUG_TRANSACTION,
1290 " fd %d\n", fp->handle);
1292 task_close_fd(proc, fp->handle);
1296 pr_err("transaction release %d bad object type %x\n",
1297 debug_id, fp->type);
1303 static void binder_transaction(struct binder_proc *proc,
1304 struct binder_thread *thread,
1305 struct binder_transaction_data *tr, int reply)
1307 struct binder_transaction *t;
1308 struct binder_work *tcomplete;
1309 binder_size_t *offp, *off_end;
1310 binder_size_t off_min;
1311 struct binder_proc *target_proc;
1312 struct binder_thread *target_thread = NULL;
1313 struct binder_node *target_node = NULL;
1314 struct list_head *target_list;
1315 wait_queue_head_t *target_wait;
1316 struct binder_transaction *in_reply_to = NULL;
1317 struct binder_transaction_log_entry *e;
1318 uint32_t return_error;
1320 e = binder_transaction_log_add(&binder_transaction_log);
1321 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
1322 e->from_proc = proc->pid;
1323 e->from_thread = thread->pid;
1324 e->target_handle = tr->target.handle;
1325 e->data_size = tr->data_size;
1326 e->offsets_size = tr->offsets_size;
1329 in_reply_to = thread->transaction_stack;
1330 if (in_reply_to == NULL) {
1331 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
1332 proc->pid, thread->pid);
1333 return_error = BR_FAILED_REPLY;
1334 goto err_empty_call_stack;
1336 binder_set_nice(in_reply_to->saved_priority);
1337 if (in_reply_to->to_thread != thread) {
1338 binder_user_error("%d:%d got reply transaction with bad transaction stack, transaction %d has target %d:%d\n",
1339 proc->pid, thread->pid, in_reply_to->debug_id,
1340 in_reply_to->to_proc ?
1341 in_reply_to->to_proc->pid : 0,
1342 in_reply_to->to_thread ?
1343 in_reply_to->to_thread->pid : 0);
1344 return_error = BR_FAILED_REPLY;
1346 goto err_bad_call_stack;
1348 thread->transaction_stack = in_reply_to->to_parent;
1349 target_thread = in_reply_to->from;
1350 if (target_thread == NULL) {
1351 return_error = BR_DEAD_REPLY;
1352 goto err_dead_binder;
1354 if (target_thread->transaction_stack != in_reply_to) {
1355 binder_user_error("%d:%d got reply transaction with bad target transaction stack %d, expected %d\n",
1356 proc->pid, thread->pid,
1357 target_thread->transaction_stack ?
1358 target_thread->transaction_stack->debug_id : 0,
1359 in_reply_to->debug_id);
1360 return_error = BR_FAILED_REPLY;
1362 target_thread = NULL;
1363 goto err_dead_binder;
1365 target_proc = target_thread->proc;
1367 if (tr->target.handle) {
1368 struct binder_ref *ref;
1369 ref = binder_get_ref(proc, tr->target.handle);
1371 binder_user_error("%d:%d got transaction to invalid handle\n",
1372 proc->pid, thread->pid);
1373 return_error = BR_FAILED_REPLY;
1374 goto err_invalid_target_handle;
1376 target_node = ref->node;
1378 target_node = binder_context_mgr_node;
1379 if (target_node == NULL) {
1380 return_error = BR_DEAD_REPLY;
1381 goto err_no_context_mgr_node;
1384 e->to_node = target_node->debug_id;
1385 target_proc = target_node->proc;
1386 if (target_proc == NULL) {
1387 return_error = BR_DEAD_REPLY;
1388 goto err_dead_binder;
1390 if (security_binder_transaction(proc->tsk, target_proc->tsk) < 0) {
1391 return_error = BR_FAILED_REPLY;
1392 goto err_invalid_target_handle;
1394 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
1395 struct binder_transaction *tmp;
1396 tmp = thread->transaction_stack;
1397 if (tmp->to_thread != thread) {
1398 binder_user_error("%d:%d got new transaction with bad transaction stack, transaction %d has target %d:%d\n",
1399 proc->pid, thread->pid, tmp->debug_id,
1400 tmp->to_proc ? tmp->to_proc->pid : 0,
1402 tmp->to_thread->pid : 0);
1403 return_error = BR_FAILED_REPLY;
1404 goto err_bad_call_stack;
1407 if (tmp->from && tmp->from->proc == target_proc)
1408 target_thread = tmp->from;
1409 tmp = tmp->from_parent;
1413 if (target_thread) {
1414 e->to_thread = target_thread->pid;
1415 target_list = &target_thread->todo;
1416 target_wait = &target_thread->wait;
1418 target_list = &target_proc->todo;
1419 target_wait = &target_proc->wait;
1421 e->to_proc = target_proc->pid;
1423 /* TODO: reuse incoming transaction for reply */
1424 t = kzalloc(sizeof(*t), GFP_KERNEL);
1426 return_error = BR_FAILED_REPLY;
1427 goto err_alloc_t_failed;
1429 binder_stats_created(BINDER_STAT_TRANSACTION);
1431 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
1432 if (tcomplete == NULL) {
1433 return_error = BR_FAILED_REPLY;
1434 goto err_alloc_tcomplete_failed;
1436 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
1438 t->debug_id = ++binder_last_id;
1439 e->debug_id = t->debug_id;
1442 binder_debug(BINDER_DEBUG_TRANSACTION,
1443 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld\n",
1444 proc->pid, thread->pid, t->debug_id,
1445 target_proc->pid, target_thread->pid,
1446 (u64)tr->data.ptr.buffer,
1447 (u64)tr->data.ptr.offsets,
1448 (u64)tr->data_size, (u64)tr->offsets_size);
1450 binder_debug(BINDER_DEBUG_TRANSACTION,
1451 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld\n",
1452 proc->pid, thread->pid, t->debug_id,
1453 target_proc->pid, target_node->debug_id,
1454 (u64)tr->data.ptr.buffer,
1455 (u64)tr->data.ptr.offsets,
1456 (u64)tr->data_size, (u64)tr->offsets_size);
1458 if (!reply && !(tr->flags & TF_ONE_WAY))
1462 t->sender_euid = proc->tsk->cred->euid;
1463 t->to_proc = target_proc;
1464 t->to_thread = target_thread;
1466 t->flags = tr->flags;
1467 t->priority = task_nice(current);
1469 trace_binder_transaction(reply, t, target_node);
1471 t->buffer = binder_alloc_buf(target_proc, tr->data_size,
1472 tr->offsets_size, !reply && (t->flags & TF_ONE_WAY));
1473 if (t->buffer == NULL) {
1474 return_error = BR_FAILED_REPLY;
1475 goto err_binder_alloc_buf_failed;
1477 t->buffer->allow_user_free = 0;
1478 t->buffer->debug_id = t->debug_id;
1479 t->buffer->transaction = t;
1480 t->buffer->target_node = target_node;
1481 trace_binder_transaction_alloc_buf(t->buffer);
1483 binder_inc_node(target_node, 1, 0, NULL);
1485 offp = (binder_size_t *)(t->buffer->data +
1486 ALIGN(tr->data_size, sizeof(void *)));
1488 if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t)
1489 tr->data.ptr.buffer, tr->data_size)) {
1490 binder_user_error("%d:%d got transaction with invalid data ptr\n",
1491 proc->pid, thread->pid);
1492 return_error = BR_FAILED_REPLY;
1493 goto err_copy_data_failed;
1495 if (copy_from_user(offp, (const void __user *)(uintptr_t)
1496 tr->data.ptr.offsets, tr->offsets_size)) {
1497 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
1498 proc->pid, thread->pid);
1499 return_error = BR_FAILED_REPLY;
1500 goto err_copy_data_failed;
1502 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
1503 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
1504 proc->pid, thread->pid, (u64)tr->offsets_size);
1505 return_error = BR_FAILED_REPLY;
1506 goto err_bad_offset;
1508 off_end = (void *)offp + tr->offsets_size;
1510 for (; offp < off_end; offp++) {
1511 struct flat_binder_object *fp;
1512 if (*offp > t->buffer->data_size - sizeof(*fp) ||
1514 t->buffer->data_size < sizeof(*fp) ||
1515 !IS_ALIGNED(*offp, sizeof(u32))) {
1516 binder_user_error("%d:%d got transaction with invalid offset, %lld (min %lld, max %lld)\n",
1517 proc->pid, thread->pid, (u64)*offp,
1519 (u64)(t->buffer->data_size -
1521 return_error = BR_FAILED_REPLY;
1522 goto err_bad_offset;
1524 fp = (struct flat_binder_object *)(t->buffer->data + *offp);
1525 off_min = *offp + sizeof(struct flat_binder_object);
1527 case BINDER_TYPE_BINDER:
1528 case BINDER_TYPE_WEAK_BINDER: {
1529 struct binder_ref *ref;
1530 struct binder_node *node = binder_get_node(proc, fp->binder);
1532 node = binder_new_node(proc, fp->binder, fp->cookie);
1534 return_error = BR_FAILED_REPLY;
1535 goto err_binder_new_node_failed;
1537 node->min_priority = fp->flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
1538 node->accept_fds = !!(fp->flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
1540 if (fp->cookie != node->cookie) {
1541 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
1542 proc->pid, thread->pid,
1543 (u64)fp->binder, node->debug_id,
1544 (u64)fp->cookie, (u64)node->cookie);
1545 goto err_binder_get_ref_for_node_failed;
1547 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
1548 return_error = BR_FAILED_REPLY;
1549 goto err_binder_get_ref_for_node_failed;
1551 ref = binder_get_ref_for_node(target_proc, node);
1553 return_error = BR_FAILED_REPLY;
1554 goto err_binder_get_ref_for_node_failed;
1556 if (fp->type == BINDER_TYPE_BINDER)
1557 fp->type = BINDER_TYPE_HANDLE;
1559 fp->type = BINDER_TYPE_WEAK_HANDLE;
1560 fp->handle = ref->desc;
1561 binder_inc_ref(ref, fp->type == BINDER_TYPE_HANDLE,
1564 trace_binder_transaction_node_to_ref(t, node, ref);
1565 binder_debug(BINDER_DEBUG_TRANSACTION,
1566 " node %d u%016llx -> ref %d desc %d\n",
1567 node->debug_id, (u64)node->ptr,
1568 ref->debug_id, ref->desc);
1570 case BINDER_TYPE_HANDLE:
1571 case BINDER_TYPE_WEAK_HANDLE: {
1572 struct binder_ref *ref = binder_get_ref(proc, fp->handle);
1574 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
1576 thread->pid, fp->handle);
1577 return_error = BR_FAILED_REPLY;
1578 goto err_binder_get_ref_failed;
1580 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
1581 return_error = BR_FAILED_REPLY;
1582 goto err_binder_get_ref_failed;
1584 if (ref->node->proc == target_proc) {
1585 if (fp->type == BINDER_TYPE_HANDLE)
1586 fp->type = BINDER_TYPE_BINDER;
1588 fp->type = BINDER_TYPE_WEAK_BINDER;
1589 fp->binder = ref->node->ptr;
1590 fp->cookie = ref->node->cookie;
1591 binder_inc_node(ref->node, fp->type == BINDER_TYPE_BINDER, 0, NULL);
1592 trace_binder_transaction_ref_to_node(t, ref);
1593 binder_debug(BINDER_DEBUG_TRANSACTION,
1594 " ref %d desc %d -> node %d u%016llx\n",
1595 ref->debug_id, ref->desc, ref->node->debug_id,
1596 (u64)ref->node->ptr);
1598 struct binder_ref *new_ref;
1599 new_ref = binder_get_ref_for_node(target_proc, ref->node);
1600 if (new_ref == NULL) {
1601 return_error = BR_FAILED_REPLY;
1602 goto err_binder_get_ref_for_node_failed;
1604 fp->handle = new_ref->desc;
1605 binder_inc_ref(new_ref, fp->type == BINDER_TYPE_HANDLE, NULL);
1606 trace_binder_transaction_ref_to_ref(t, ref,
1608 binder_debug(BINDER_DEBUG_TRANSACTION,
1609 " ref %d desc %d -> ref %d desc %d (node %d)\n",
1610 ref->debug_id, ref->desc, new_ref->debug_id,
1611 new_ref->desc, ref->node->debug_id);
1615 case BINDER_TYPE_FD: {
1620 if (!(in_reply_to->flags & TF_ACCEPT_FDS)) {
1621 binder_user_error("%d:%d got reply with fd, %d, but target does not allow fds\n",
1622 proc->pid, thread->pid, fp->handle);
1623 return_error = BR_FAILED_REPLY;
1624 goto err_fd_not_allowed;
1626 } else if (!target_node->accept_fds) {
1627 binder_user_error("%d:%d got transaction with fd, %d, but target does not allow fds\n",
1628 proc->pid, thread->pid, fp->handle);
1629 return_error = BR_FAILED_REPLY;
1630 goto err_fd_not_allowed;
1633 file = fget(fp->handle);
1635 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
1636 proc->pid, thread->pid, fp->handle);
1637 return_error = BR_FAILED_REPLY;
1638 goto err_fget_failed;
1640 if (security_binder_transfer_file(proc->tsk, target_proc->tsk, file) < 0) {
1642 return_error = BR_FAILED_REPLY;
1643 goto err_get_unused_fd_failed;
1645 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
1646 if (target_fd < 0) {
1648 return_error = BR_FAILED_REPLY;
1649 goto err_get_unused_fd_failed;
1651 task_fd_install(target_proc, target_fd, file);
1652 trace_binder_transaction_fd(t, fp->handle, target_fd);
1653 binder_debug(BINDER_DEBUG_TRANSACTION,
1654 " fd %d -> %d\n", fp->handle, target_fd);
1656 fp->handle = target_fd;
1660 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
1661 proc->pid, thread->pid, fp->type);
1662 return_error = BR_FAILED_REPLY;
1663 goto err_bad_object_type;
1667 BUG_ON(t->buffer->async_transaction != 0);
1668 binder_pop_transaction(target_thread, in_reply_to);
1669 } else if (!(t->flags & TF_ONE_WAY)) {
1670 BUG_ON(t->buffer->async_transaction != 0);
1672 t->from_parent = thread->transaction_stack;
1673 thread->transaction_stack = t;
1675 BUG_ON(target_node == NULL);
1676 BUG_ON(t->buffer->async_transaction != 1);
1677 if (target_node->has_async_transaction) {
1678 target_list = &target_node->async_todo;
1681 target_node->has_async_transaction = 1;
1683 t->work.type = BINDER_WORK_TRANSACTION;
1684 list_add_tail(&t->work.entry, target_list);
1685 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
1686 list_add_tail(&tcomplete->entry, &thread->todo);
1688 wake_up_interruptible(target_wait);
1691 err_get_unused_fd_failed:
1694 err_binder_get_ref_for_node_failed:
1695 err_binder_get_ref_failed:
1696 err_binder_new_node_failed:
1697 err_bad_object_type:
1699 err_copy_data_failed:
1700 trace_binder_transaction_failed_buffer_release(t->buffer);
1701 binder_transaction_buffer_release(target_proc, t->buffer, offp);
1702 t->buffer->transaction = NULL;
1703 binder_free_buf(target_proc, t->buffer);
1704 err_binder_alloc_buf_failed:
1706 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
1707 err_alloc_tcomplete_failed:
1709 binder_stats_deleted(BINDER_STAT_TRANSACTION);
1712 err_empty_call_stack:
1714 err_invalid_target_handle:
1715 err_no_context_mgr_node:
1716 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1717 "%d:%d transaction failed %d, size %lld-%lld\n",
1718 proc->pid, thread->pid, return_error,
1719 (u64)tr->data_size, (u64)tr->offsets_size);
1722 struct binder_transaction_log_entry *fe;
1723 fe = binder_transaction_log_add(&binder_transaction_log_failed);
1727 BUG_ON(thread->return_error != BR_OK);
1729 thread->return_error = BR_TRANSACTION_COMPLETE;
1730 binder_send_failed_reply(in_reply_to, return_error);
1732 thread->return_error = return_error;
1735 int binder_thread_write(struct binder_proc *proc, struct binder_thread *thread,
1736 binder_uintptr_t binder_buffer, size_t size,
1737 binder_size_t *consumed)
1740 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
1741 void __user *ptr = buffer + *consumed;
1742 void __user *end = buffer + size;
1744 while (ptr < end && thread->return_error == BR_OK) {
1745 if (get_user(cmd, (uint32_t __user *)ptr))
1747 ptr += sizeof(uint32_t);
1748 trace_binder_command(cmd);
1749 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
1750 binder_stats.bc[_IOC_NR(cmd)]++;
1751 proc->stats.bc[_IOC_NR(cmd)]++;
1752 thread->stats.bc[_IOC_NR(cmd)]++;
1760 struct binder_ref *ref;
1761 const char *debug_string;
1763 if (get_user(target, (uint32_t __user *)ptr))
1765 ptr += sizeof(uint32_t);
1766 if (target == 0 && binder_context_mgr_node &&
1767 (cmd == BC_INCREFS || cmd == BC_ACQUIRE)) {
1768 ref = binder_get_ref_for_node(proc,
1769 binder_context_mgr_node);
1770 if (ref->desc != target) {
1771 binder_user_error("%d:%d tried to acquire reference to desc 0, got %d instead\n",
1772 proc->pid, thread->pid,
1776 ref = binder_get_ref(proc, target);
1778 binder_user_error("%d:%d refcount change on invalid ref %d\n",
1779 proc->pid, thread->pid, target);
1784 debug_string = "IncRefs";
1785 binder_inc_ref(ref, 0, NULL);
1788 debug_string = "Acquire";
1789 binder_inc_ref(ref, 1, NULL);
1792 debug_string = "Release";
1793 binder_dec_ref(ref, 1);
1797 debug_string = "DecRefs";
1798 binder_dec_ref(ref, 0);
1801 binder_debug(BINDER_DEBUG_USER_REFS,
1802 "%d:%d %s ref %d desc %d s %d w %d for node %d\n",
1803 proc->pid, thread->pid, debug_string, ref->debug_id,
1804 ref->desc, ref->strong, ref->weak, ref->node->debug_id);
1807 case BC_INCREFS_DONE:
1808 case BC_ACQUIRE_DONE: {
1809 binder_uintptr_t node_ptr;
1810 binder_uintptr_t cookie;
1811 struct binder_node *node;
1813 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
1815 ptr += sizeof(binder_uintptr_t);
1816 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
1818 ptr += sizeof(binder_uintptr_t);
1819 node = binder_get_node(proc, node_ptr);
1821 binder_user_error("%d:%d %s u%016llx no match\n",
1822 proc->pid, thread->pid,
1823 cmd == BC_INCREFS_DONE ?
1829 if (cookie != node->cookie) {
1830 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
1831 proc->pid, thread->pid,
1832 cmd == BC_INCREFS_DONE ?
1833 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
1834 (u64)node_ptr, node->debug_id,
1835 (u64)cookie, (u64)node->cookie);
1838 if (cmd == BC_ACQUIRE_DONE) {
1839 if (node->pending_strong_ref == 0) {
1840 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
1841 proc->pid, thread->pid,
1845 node->pending_strong_ref = 0;
1847 if (node->pending_weak_ref == 0) {
1848 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
1849 proc->pid, thread->pid,
1853 node->pending_weak_ref = 0;
1855 binder_dec_node(node, cmd == BC_ACQUIRE_DONE, 0);
1856 binder_debug(BINDER_DEBUG_USER_REFS,
1857 "%d:%d %s node %d ls %d lw %d\n",
1858 proc->pid, thread->pid,
1859 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
1860 node->debug_id, node->local_strong_refs, node->local_weak_refs);
1863 case BC_ATTEMPT_ACQUIRE:
1864 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
1866 case BC_ACQUIRE_RESULT:
1867 pr_err("BC_ACQUIRE_RESULT not supported\n");
1870 case BC_FREE_BUFFER: {
1871 binder_uintptr_t data_ptr;
1872 struct binder_buffer *buffer;
1874 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
1876 ptr += sizeof(binder_uintptr_t);
1878 buffer = binder_buffer_lookup(proc, data_ptr);
1879 if (buffer == NULL) {
1880 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
1881 proc->pid, thread->pid, (u64)data_ptr);
1884 if (!buffer->allow_user_free) {
1885 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
1886 proc->pid, thread->pid, (u64)data_ptr);
1889 binder_debug(BINDER_DEBUG_FREE_BUFFER,
1890 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
1891 proc->pid, thread->pid, (u64)data_ptr, buffer->debug_id,
1892 buffer->transaction ? "active" : "finished");
1894 if (buffer->transaction) {
1895 buffer->transaction->buffer = NULL;
1896 buffer->transaction = NULL;
1898 if (buffer->async_transaction && buffer->target_node) {
1899 BUG_ON(!buffer->target_node->has_async_transaction);
1900 if (list_empty(&buffer->target_node->async_todo))
1901 buffer->target_node->has_async_transaction = 0;
1903 list_move_tail(buffer->target_node->async_todo.next, &thread->todo);
1905 trace_binder_transaction_buffer_release(buffer);
1906 binder_transaction_buffer_release(proc, buffer, NULL);
1907 binder_free_buf(proc, buffer);
1911 case BC_TRANSACTION:
1913 struct binder_transaction_data tr;
1915 if (copy_from_user(&tr, ptr, sizeof(tr)))
1918 binder_transaction(proc, thread, &tr, cmd == BC_REPLY);
1922 case BC_REGISTER_LOOPER:
1923 binder_debug(BINDER_DEBUG_THREADS,
1924 "%d:%d BC_REGISTER_LOOPER\n",
1925 proc->pid, thread->pid);
1926 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
1927 thread->looper |= BINDER_LOOPER_STATE_INVALID;
1928 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
1929 proc->pid, thread->pid);
1930 } else if (proc->requested_threads == 0) {
1931 thread->looper |= BINDER_LOOPER_STATE_INVALID;
1932 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
1933 proc->pid, thread->pid);
1935 proc->requested_threads--;
1936 proc->requested_threads_started++;
1938 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
1940 case BC_ENTER_LOOPER:
1941 binder_debug(BINDER_DEBUG_THREADS,
1942 "%d:%d BC_ENTER_LOOPER\n",
1943 proc->pid, thread->pid);
1944 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
1945 thread->looper |= BINDER_LOOPER_STATE_INVALID;
1946 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
1947 proc->pid, thread->pid);
1949 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
1951 case BC_EXIT_LOOPER:
1952 binder_debug(BINDER_DEBUG_THREADS,
1953 "%d:%d BC_EXIT_LOOPER\n",
1954 proc->pid, thread->pid);
1955 thread->looper |= BINDER_LOOPER_STATE_EXITED;
1958 case BC_REQUEST_DEATH_NOTIFICATION:
1959 case BC_CLEAR_DEATH_NOTIFICATION: {
1961 binder_uintptr_t cookie;
1962 struct binder_ref *ref;
1963 struct binder_ref_death *death;
1965 if (get_user(target, (uint32_t __user *)ptr))
1967 ptr += sizeof(uint32_t);
1968 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
1970 ptr += sizeof(binder_uintptr_t);
1971 ref = binder_get_ref(proc, target);
1973 binder_user_error("%d:%d %s invalid ref %d\n",
1974 proc->pid, thread->pid,
1975 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
1976 "BC_REQUEST_DEATH_NOTIFICATION" :
1977 "BC_CLEAR_DEATH_NOTIFICATION",
1982 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
1983 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
1984 proc->pid, thread->pid,
1985 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
1986 "BC_REQUEST_DEATH_NOTIFICATION" :
1987 "BC_CLEAR_DEATH_NOTIFICATION",
1988 (u64)cookie, ref->debug_id, ref->desc,
1989 ref->strong, ref->weak, ref->node->debug_id);
1991 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
1993 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
1994 proc->pid, thread->pid);
1997 death = kzalloc(sizeof(*death), GFP_KERNEL);
1998 if (death == NULL) {
1999 thread->return_error = BR_ERROR;
2000 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2001 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
2002 proc->pid, thread->pid);
2005 binder_stats_created(BINDER_STAT_DEATH);
2006 INIT_LIST_HEAD(&death->work.entry);
2007 death->cookie = cookie;
2009 if (ref->node->proc == NULL) {
2010 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
2011 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2012 list_add_tail(&ref->death->work.entry, &thread->todo);
2014 list_add_tail(&ref->death->work.entry, &proc->todo);
2015 wake_up_interruptible(&proc->wait);
2019 if (ref->death == NULL) {
2020 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
2021 proc->pid, thread->pid);
2025 if (death->cookie != cookie) {
2026 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
2027 proc->pid, thread->pid,
2028 (u64)death->cookie, (u64)cookie);
2032 if (list_empty(&death->work.entry)) {
2033 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2034 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2035 list_add_tail(&death->work.entry, &thread->todo);
2037 list_add_tail(&death->work.entry, &proc->todo);
2038 wake_up_interruptible(&proc->wait);
2041 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
2042 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
2046 case BC_DEAD_BINDER_DONE: {
2047 struct binder_work *w;
2048 binder_uintptr_t cookie;
2049 struct binder_ref_death *death = NULL;
2050 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
2053 ptr += sizeof(void *);
2054 list_for_each_entry(w, &proc->delivered_death, entry) {
2055 struct binder_ref_death *tmp_death = container_of(w, struct binder_ref_death, work);
2056 if (tmp_death->cookie == cookie) {
2061 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2062 "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n",
2063 proc->pid, thread->pid, (u64)cookie, death);
2064 if (death == NULL) {
2065 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
2066 proc->pid, thread->pid, (u64)cookie);
2070 list_del_init(&death->work.entry);
2071 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
2072 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2073 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2074 list_add_tail(&death->work.entry, &thread->todo);
2076 list_add_tail(&death->work.entry, &proc->todo);
2077 wake_up_interruptible(&proc->wait);
2083 pr_err("%d:%d unknown command %d\n",
2084 proc->pid, thread->pid, cmd);
2087 *consumed = ptr - buffer;
2092 void binder_stat_br(struct binder_proc *proc, struct binder_thread *thread,
2095 trace_binder_return(cmd);
2096 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
2097 binder_stats.br[_IOC_NR(cmd)]++;
2098 proc->stats.br[_IOC_NR(cmd)]++;
2099 thread->stats.br[_IOC_NR(cmd)]++;
2103 static int binder_has_proc_work(struct binder_proc *proc,
2104 struct binder_thread *thread)
2106 return !list_empty(&proc->todo) ||
2107 (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2110 static int binder_has_thread_work(struct binder_thread *thread)
2112 return !list_empty(&thread->todo) || thread->return_error != BR_OK ||
2113 (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2116 static int binder_thread_read(struct binder_proc *proc,
2117 struct binder_thread *thread,
2118 binder_uintptr_t binder_buffer, size_t size,
2119 binder_size_t *consumed, int non_block)
2121 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
2122 void __user *ptr = buffer + *consumed;
2123 void __user *end = buffer + size;
2126 int wait_for_proc_work;
2128 if (*consumed == 0) {
2129 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
2131 ptr += sizeof(uint32_t);
2135 wait_for_proc_work = thread->transaction_stack == NULL &&
2136 list_empty(&thread->todo);
2138 if (thread->return_error != BR_OK && ptr < end) {
2139 if (thread->return_error2 != BR_OK) {
2140 if (put_user(thread->return_error2, (uint32_t __user *)ptr))
2142 ptr += sizeof(uint32_t);
2143 binder_stat_br(proc, thread, thread->return_error2);
2146 thread->return_error2 = BR_OK;
2148 if (put_user(thread->return_error, (uint32_t __user *)ptr))
2150 ptr += sizeof(uint32_t);
2151 binder_stat_br(proc, thread, thread->return_error);
2152 thread->return_error = BR_OK;
2157 thread->looper |= BINDER_LOOPER_STATE_WAITING;
2158 if (wait_for_proc_work)
2159 proc->ready_threads++;
2161 binder_unlock(__func__);
2163 trace_binder_wait_for_work(wait_for_proc_work,
2164 !!thread->transaction_stack,
2165 !list_empty(&thread->todo));
2166 if (wait_for_proc_work) {
2167 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2168 BINDER_LOOPER_STATE_ENTERED))) {
2169 binder_user_error("%d:%d ERROR: Thread waiting for process work before calling BC_REGISTER_LOOPER or BC_ENTER_LOOPER (state %x)\n",
2170 proc->pid, thread->pid, thread->looper);
2171 wait_event_interruptible(binder_user_error_wait,
2172 binder_stop_on_user_error < 2);
2174 binder_set_nice(proc->default_priority);
2176 if (!binder_has_proc_work(proc, thread))
2179 ret = wait_event_freezable_exclusive(proc->wait, binder_has_proc_work(proc, thread));
2182 if (!binder_has_thread_work(thread))
2185 ret = wait_event_freezable(thread->wait, binder_has_thread_work(thread));
2188 binder_lock(__func__);
2190 if (wait_for_proc_work)
2191 proc->ready_threads--;
2192 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
2199 struct binder_transaction_data tr;
2200 struct binder_work *w;
2201 struct binder_transaction *t = NULL;
2203 if (!list_empty(&thread->todo))
2204 w = list_first_entry(&thread->todo, struct binder_work, entry);
2205 else if (!list_empty(&proc->todo) && wait_for_proc_work)
2206 w = list_first_entry(&proc->todo, struct binder_work, entry);
2208 if (ptr - buffer == 4 && !(thread->looper & BINDER_LOOPER_STATE_NEED_RETURN)) /* no data added */
2213 if (end - ptr < sizeof(tr) + 4)
2217 case BINDER_WORK_TRANSACTION: {
2218 t = container_of(w, struct binder_transaction, work);
2220 case BINDER_WORK_TRANSACTION_COMPLETE: {
2221 cmd = BR_TRANSACTION_COMPLETE;
2222 if (put_user(cmd, (uint32_t __user *)ptr))
2224 ptr += sizeof(uint32_t);
2226 binder_stat_br(proc, thread, cmd);
2227 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
2228 "%d:%d BR_TRANSACTION_COMPLETE\n",
2229 proc->pid, thread->pid);
2231 list_del(&w->entry);
2233 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2235 case BINDER_WORK_NODE: {
2236 struct binder_node *node = container_of(w, struct binder_node, work);
2237 uint32_t cmd = BR_NOOP;
2238 const char *cmd_name;
2239 int strong = node->internal_strong_refs || node->local_strong_refs;
2240 int weak = !hlist_empty(&node->refs) || node->local_weak_refs || strong;
2241 if (weak && !node->has_weak_ref) {
2243 cmd_name = "BR_INCREFS";
2244 node->has_weak_ref = 1;
2245 node->pending_weak_ref = 1;
2246 node->local_weak_refs++;
2247 } else if (strong && !node->has_strong_ref) {
2249 cmd_name = "BR_ACQUIRE";
2250 node->has_strong_ref = 1;
2251 node->pending_strong_ref = 1;
2252 node->local_strong_refs++;
2253 } else if (!strong && node->has_strong_ref) {
2255 cmd_name = "BR_RELEASE";
2256 node->has_strong_ref = 0;
2257 } else if (!weak && node->has_weak_ref) {
2259 cmd_name = "BR_DECREFS";
2260 node->has_weak_ref = 0;
2262 if (cmd != BR_NOOP) {
2263 if (put_user(cmd, (uint32_t __user *)ptr))
2265 ptr += sizeof(uint32_t);
2266 if (put_user(node->ptr,
2267 (binder_uintptr_t __user *)ptr))
2269 ptr += sizeof(binder_uintptr_t);
2270 if (put_user(node->cookie,
2271 (binder_uintptr_t __user *)ptr))
2273 ptr += sizeof(binder_uintptr_t);
2275 binder_stat_br(proc, thread, cmd);
2276 binder_debug(BINDER_DEBUG_USER_REFS,
2277 "%d:%d %s %d u%016llx c%016llx\n",
2278 proc->pid, thread->pid, cmd_name,
2280 (u64)node->ptr, (u64)node->cookie);
2282 list_del_init(&w->entry);
2283 if (!weak && !strong) {
2284 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
2285 "%d:%d node %d u%016llx c%016llx deleted\n",
2286 proc->pid, thread->pid, node->debug_id,
2287 (u64)node->ptr, (u64)node->cookie);
2288 rb_erase(&node->rb_node, &proc->nodes);
2290 binder_stats_deleted(BINDER_STAT_NODE);
2292 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
2293 "%d:%d node %d u%016llx c%016llx state unchanged\n",
2294 proc->pid, thread->pid, node->debug_id,
2295 (u64)node->ptr, (u64)node->cookie);
2299 case BINDER_WORK_DEAD_BINDER:
2300 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2301 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2302 struct binder_ref_death *death;
2305 death = container_of(w, struct binder_ref_death, work);
2306 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
2307 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
2309 cmd = BR_DEAD_BINDER;
2310 if (put_user(cmd, (uint32_t __user *)ptr))
2312 ptr += sizeof(uint32_t);
2313 if (put_user(death->cookie,
2314 (binder_uintptr_t __user *)ptr))
2316 ptr += sizeof(binder_uintptr_t);
2317 binder_stat_br(proc, thread, cmd);
2318 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
2319 "%d:%d %s %016llx\n",
2320 proc->pid, thread->pid,
2321 cmd == BR_DEAD_BINDER ?
2323 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
2324 (u64)death->cookie);
2326 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
2327 list_del(&w->entry);
2329 binder_stats_deleted(BINDER_STAT_DEATH);
2331 list_move(&w->entry, &proc->delivered_death);
2332 if (cmd == BR_DEAD_BINDER)
2333 goto done; /* DEAD_BINDER notifications can cause transactions */
2340 BUG_ON(t->buffer == NULL);
2341 if (t->buffer->target_node) {
2342 struct binder_node *target_node = t->buffer->target_node;
2343 tr.target.ptr = target_node->ptr;
2344 tr.cookie = target_node->cookie;
2345 t->saved_priority = task_nice(current);
2346 if (t->priority < target_node->min_priority &&
2347 !(t->flags & TF_ONE_WAY))
2348 binder_set_nice(t->priority);
2349 else if (!(t->flags & TF_ONE_WAY) ||
2350 t->saved_priority > target_node->min_priority)
2351 binder_set_nice(target_node->min_priority);
2352 cmd = BR_TRANSACTION;
2359 tr.flags = t->flags;
2360 tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
2363 struct task_struct *sender = t->from->proc->tsk;
2364 tr.sender_pid = task_tgid_nr_ns(sender,
2365 task_active_pid_ns(current));
2370 tr.data_size = t->buffer->data_size;
2371 tr.offsets_size = t->buffer->offsets_size;
2372 tr.data.ptr.buffer = (binder_uintptr_t)(
2373 (uintptr_t)t->buffer->data +
2374 proc->user_buffer_offset);
2375 tr.data.ptr.offsets = tr.data.ptr.buffer +
2376 ALIGN(t->buffer->data_size,
2379 if (put_user(cmd, (uint32_t __user *)ptr))
2381 ptr += sizeof(uint32_t);
2382 if (copy_to_user(ptr, &tr, sizeof(tr)))
2386 trace_binder_transaction_received(t);
2387 binder_stat_br(proc, thread, cmd);
2388 binder_debug(BINDER_DEBUG_TRANSACTION,
2389 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
2390 proc->pid, thread->pid,
2391 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
2393 t->debug_id, t->from ? t->from->proc->pid : 0,
2394 t->from ? t->from->pid : 0, cmd,
2395 t->buffer->data_size, t->buffer->offsets_size,
2396 (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
2398 list_del(&t->work.entry);
2399 t->buffer->allow_user_free = 1;
2400 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
2401 t->to_parent = thread->transaction_stack;
2402 t->to_thread = thread;
2403 thread->transaction_stack = t;
2405 t->buffer->transaction = NULL;
2407 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2414 *consumed = ptr - buffer;
2415 if (proc->requested_threads + proc->ready_threads == 0 &&
2416 proc->requested_threads_started < proc->max_threads &&
2417 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2418 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
2419 /*spawn a new thread if we leave this out */) {
2420 proc->requested_threads++;
2421 binder_debug(BINDER_DEBUG_THREADS,
2422 "%d:%d BR_SPAWN_LOOPER\n",
2423 proc->pid, thread->pid);
2424 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
2426 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
2431 static void binder_release_work(struct list_head *list)
2433 struct binder_work *w;
2434 while (!list_empty(list)) {
2435 w = list_first_entry(list, struct binder_work, entry);
2436 list_del_init(&w->entry);
2438 case BINDER_WORK_TRANSACTION: {
2439 struct binder_transaction *t;
2441 t = container_of(w, struct binder_transaction, work);
2442 if (t->buffer->target_node &&
2443 !(t->flags & TF_ONE_WAY)) {
2444 binder_send_failed_reply(t, BR_DEAD_REPLY);
2446 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2447 "undelivered transaction %d\n",
2449 t->buffer->transaction = NULL;
2451 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2454 case BINDER_WORK_TRANSACTION_COMPLETE: {
2455 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2456 "undelivered TRANSACTION_COMPLETE\n");
2458 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2460 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2461 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2462 struct binder_ref_death *death;
2464 death = container_of(w, struct binder_ref_death, work);
2465 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2466 "undelivered death notification, %016llx\n",
2467 (u64)death->cookie);
2469 binder_stats_deleted(BINDER_STAT_DEATH);
2472 pr_err("unexpected work type, %d, not freed\n",
2480 static struct binder_thread *binder_get_thread(struct binder_proc *proc)
2482 struct binder_thread *thread = NULL;
2483 struct rb_node *parent = NULL;
2484 struct rb_node **p = &proc->threads.rb_node;
2488 thread = rb_entry(parent, struct binder_thread, rb_node);
2490 if (current->pid < thread->pid)
2492 else if (current->pid > thread->pid)
2493 p = &(*p)->rb_right;
2498 thread = kzalloc(sizeof(*thread), GFP_KERNEL);
2501 binder_stats_created(BINDER_STAT_THREAD);
2502 thread->proc = proc;
2503 thread->pid = current->pid;
2504 init_waitqueue_head(&thread->wait);
2505 INIT_LIST_HEAD(&thread->todo);
2506 rb_link_node(&thread->rb_node, parent, p);
2507 rb_insert_color(&thread->rb_node, &proc->threads);
2508 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
2509 thread->return_error = BR_OK;
2510 thread->return_error2 = BR_OK;
2515 static int binder_free_thread(struct binder_proc *proc,
2516 struct binder_thread *thread)
2518 struct binder_transaction *t;
2519 struct binder_transaction *send_reply = NULL;
2520 int active_transactions = 0;
2522 rb_erase(&thread->rb_node, &proc->threads);
2523 t = thread->transaction_stack;
2524 if (t && t->to_thread == thread)
2527 active_transactions++;
2528 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2529 "release %d:%d transaction %d %s, still active\n",
2530 proc->pid, thread->pid,
2532 (t->to_thread == thread) ? "in" : "out");
2534 if (t->to_thread == thread) {
2536 t->to_thread = NULL;
2538 t->buffer->transaction = NULL;
2542 } else if (t->from == thread) {
2549 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
2550 binder_release_work(&thread->todo);
2552 binder_stats_deleted(BINDER_STAT_THREAD);
2553 return active_transactions;
2556 static unsigned int binder_poll(struct file *filp,
2557 struct poll_table_struct *wait)
2559 struct binder_proc *proc = filp->private_data;
2560 struct binder_thread *thread = NULL;
2561 int wait_for_proc_work;
2563 binder_lock(__func__);
2565 thread = binder_get_thread(proc);
2567 wait_for_proc_work = thread->transaction_stack == NULL &&
2568 list_empty(&thread->todo) && thread->return_error == BR_OK;
2570 binder_unlock(__func__);
2572 if (wait_for_proc_work) {
2573 if (binder_has_proc_work(proc, thread))
2575 poll_wait(filp, &proc->wait, wait);
2576 if (binder_has_proc_work(proc, thread))
2579 if (binder_has_thread_work(thread))
2581 poll_wait(filp, &thread->wait, wait);
2582 if (binder_has_thread_work(thread))
2588 static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2591 struct binder_proc *proc = filp->private_data;
2592 struct binder_thread *thread;
2593 unsigned int size = _IOC_SIZE(cmd);
2594 void __user *ubuf = (void __user *)arg;
2596 /*pr_info("binder_ioctl: %d:%d %x %lx\n", proc->pid, current->pid, cmd, arg);*/
2598 trace_binder_ioctl(cmd, arg);
2600 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2604 binder_lock(__func__);
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 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
2624 proc->pid, thread->pid,
2625 (u64)bwr.write_size, (u64)bwr.write_buffer,
2626 (u64)bwr.read_size, (u64)bwr.read_buffer);
2628 if (bwr.write_size > 0) {
2629 ret = binder_thread_write(proc, thread, bwr.write_buffer, bwr.write_size, &bwr.write_consumed);
2630 trace_binder_write_done(ret);
2632 bwr.read_consumed = 0;
2633 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2638 if (bwr.read_size > 0) {
2639 ret = binder_thread_read(proc, thread, bwr.read_buffer, bwr.read_size, &bwr.read_consumed, filp->f_flags & O_NONBLOCK);
2640 trace_binder_read_done(ret);
2641 if (!list_empty(&proc->todo))
2642 wake_up_interruptible(&proc->wait);
2644 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2649 binder_debug(BINDER_DEBUG_READ_WRITE,
2650 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
2651 proc->pid, thread->pid,
2652 (u64)bwr.write_consumed, (u64)bwr.write_size,
2653 (u64)bwr.read_consumed, (u64)bwr.read_size);
2654 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
2660 case BINDER_SET_MAX_THREADS:
2661 if (copy_from_user(&proc->max_threads, ubuf, sizeof(proc->max_threads))) {
2666 case BINDER_SET_CONTEXT_MGR:
2667 if (binder_context_mgr_node != NULL) {
2668 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
2672 ret = security_binder_set_context_mgr(proc->tsk);
2675 if (uid_valid(binder_context_mgr_uid)) {
2676 if (!uid_eq(binder_context_mgr_uid, current->cred->euid)) {
2677 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
2678 from_kuid(&init_user_ns, current->cred->euid),
2679 from_kuid(&init_user_ns, binder_context_mgr_uid));
2684 binder_context_mgr_uid = current->cred->euid;
2685 binder_context_mgr_node = binder_new_node(proc, 0, 0);
2686 if (binder_context_mgr_node == NULL) {
2690 binder_context_mgr_node->local_weak_refs++;
2691 binder_context_mgr_node->local_strong_refs++;
2692 binder_context_mgr_node->has_strong_ref = 1;
2693 binder_context_mgr_node->has_weak_ref = 1;
2695 case BINDER_THREAD_EXIT:
2696 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
2697 proc->pid, thread->pid);
2698 binder_free_thread(proc, thread);
2701 case BINDER_VERSION:
2702 if (size != sizeof(struct binder_version)) {
2706 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION, &((struct binder_version *)ubuf)->protocol_version)) {
2718 thread->looper &= ~BINDER_LOOPER_STATE_NEED_RETURN;
2719 binder_unlock(__func__);
2720 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2721 if (ret && ret != -ERESTARTSYS)
2722 pr_info("%d:%d ioctl %x %lx returned %d\n", proc->pid, current->pid, cmd, arg, ret);
2724 trace_binder_ioctl_done(ret);
2728 static void binder_vma_open(struct vm_area_struct *vma)
2730 struct binder_proc *proc = vma->vm_private_data;
2731 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
2732 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
2733 proc->pid, vma->vm_start, vma->vm_end,
2734 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2735 (unsigned long)pgprot_val(vma->vm_page_prot));
2738 static void binder_vma_close(struct vm_area_struct *vma)
2740 struct binder_proc *proc = vma->vm_private_data;
2741 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
2742 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
2743 proc->pid, vma->vm_start, vma->vm_end,
2744 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2745 (unsigned long)pgprot_val(vma->vm_page_prot));
2747 proc->vma_vm_mm = NULL;
2748 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
2751 static struct vm_operations_struct binder_vm_ops = {
2752 .open = binder_vma_open,
2753 .close = binder_vma_close,
2756 static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
2759 struct vm_struct *area;
2760 struct binder_proc *proc = filp->private_data;
2761 const char *failure_string;
2762 struct binder_buffer *buffer;
2764 if (proc->tsk != current)
2767 if ((vma->vm_end - vma->vm_start) > SZ_4M)
2768 vma->vm_end = vma->vm_start + SZ_4M;
2770 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
2771 "binder_mmap: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
2772 proc->pid, vma->vm_start, vma->vm_end,
2773 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2774 (unsigned long)pgprot_val(vma->vm_page_prot));
2776 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
2778 failure_string = "bad vm_flags";
2781 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
2783 mutex_lock(&binder_mmap_lock);
2786 failure_string = "already mapped";
2787 goto err_already_mapped;
2790 area = get_vm_area(vma->vm_end - vma->vm_start, VM_IOREMAP);
2793 failure_string = "get_vm_area";
2794 goto err_get_vm_area_failed;
2796 proc->buffer = area->addr;
2797 proc->user_buffer_offset = vma->vm_start - (uintptr_t)proc->buffer;
2798 mutex_unlock(&binder_mmap_lock);
2800 #ifdef CONFIG_CPU_CACHE_VIPT
2801 if (cache_is_vipt_aliasing()) {
2802 while (CACHE_COLOUR((vma->vm_start ^ (uint32_t)proc->buffer))) {
2803 pr_info("binder_mmap: %d %lx-%lx maps %p bad alignment\n", proc->pid, vma->vm_start, vma->vm_end, proc->buffer);
2804 vma->vm_start += PAGE_SIZE;
2808 proc->pages = kzalloc(sizeof(proc->pages[0]) * ((vma->vm_end - vma->vm_start) / PAGE_SIZE), GFP_KERNEL);
2809 if (proc->pages == NULL) {
2811 failure_string = "alloc page array";
2812 goto err_alloc_pages_failed;
2814 proc->buffer_size = vma->vm_end - vma->vm_start;
2816 vma->vm_ops = &binder_vm_ops;
2817 vma->vm_private_data = proc;
2819 if (binder_update_page_range(proc, 1, proc->buffer, proc->buffer + PAGE_SIZE, vma)) {
2821 failure_string = "alloc small buf";
2822 goto err_alloc_small_buf_failed;
2824 buffer = proc->buffer;
2825 INIT_LIST_HEAD(&proc->buffers);
2826 list_add(&buffer->entry, &proc->buffers);
2828 binder_insert_free_buffer(proc, buffer);
2829 proc->free_async_space = proc->buffer_size / 2;
2831 proc->files = get_files_struct(current);
2833 proc->vma_vm_mm = vma->vm_mm;
2835 /*pr_info("binder_mmap: %d %lx-%lx maps %p\n",
2836 proc->pid, vma->vm_start, vma->vm_end, proc->buffer);*/
2839 err_alloc_small_buf_failed:
2842 err_alloc_pages_failed:
2843 mutex_lock(&binder_mmap_lock);
2844 vfree(proc->buffer);
2845 proc->buffer = NULL;
2846 err_get_vm_area_failed:
2848 mutex_unlock(&binder_mmap_lock);
2850 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
2851 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
2855 static int binder_open(struct inode *nodp, struct file *filp)
2857 struct binder_proc *proc;
2859 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
2860 current->group_leader->pid, current->pid);
2862 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
2865 get_task_struct(current);
2866 proc->tsk = current;
2867 INIT_LIST_HEAD(&proc->todo);
2868 init_waitqueue_head(&proc->wait);
2869 proc->default_priority = task_nice(current);
2871 binder_lock(__func__);
2873 binder_stats_created(BINDER_STAT_PROC);
2874 hlist_add_head(&proc->proc_node, &binder_procs);
2875 proc->pid = current->group_leader->pid;
2876 INIT_LIST_HEAD(&proc->delivered_death);
2877 filp->private_data = proc;
2879 binder_unlock(__func__);
2881 if (binder_debugfs_dir_entry_proc) {
2883 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
2884 proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
2885 binder_debugfs_dir_entry_proc, proc, &binder_proc_fops);
2891 static int binder_flush(struct file *filp, fl_owner_t id)
2893 struct binder_proc *proc = filp->private_data;
2895 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
2900 static void binder_deferred_flush(struct binder_proc *proc)
2904 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
2905 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
2906 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
2907 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
2908 wake_up_interruptible(&thread->wait);
2912 wake_up_interruptible_all(&proc->wait);
2914 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
2915 "binder_flush: %d woke %d threads\n", proc->pid,
2919 static int binder_release(struct inode *nodp, struct file *filp)
2921 struct binder_proc *proc = filp->private_data;
2922 debugfs_remove(proc->debugfs_entry);
2923 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
2928 static int binder_node_release(struct binder_node *node, int refs)
2930 struct binder_ref *ref;
2933 list_del_init(&node->work.entry);
2934 binder_release_work(&node->async_todo);
2936 if (hlist_empty(&node->refs)) {
2938 binder_stats_deleted(BINDER_STAT_NODE);
2944 node->local_strong_refs = 0;
2945 node->local_weak_refs = 0;
2946 hlist_add_head(&node->dead_node, &binder_dead_nodes);
2948 hlist_for_each_entry(ref, &node->refs, node_entry) {
2956 if (list_empty(&ref->death->work.entry)) {
2957 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
2958 list_add_tail(&ref->death->work.entry,
2960 wake_up_interruptible(&ref->proc->wait);
2965 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2966 "node %d now dead, refs %d, death %d\n",
2967 node->debug_id, refs, death);
2972 static void binder_deferred_release(struct binder_proc *proc)
2974 struct binder_transaction *t;
2976 int threads, nodes, incoming_refs, outgoing_refs, buffers,
2977 active_transactions, page_count;
2980 BUG_ON(proc->files);
2982 hlist_del(&proc->proc_node);
2984 if (binder_context_mgr_node && binder_context_mgr_node->proc == proc) {
2985 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2986 "%s: %d context_mgr_node gone\n",
2987 __func__, proc->pid);
2988 binder_context_mgr_node = NULL;
2992 active_transactions = 0;
2993 while ((n = rb_first(&proc->threads))) {
2994 struct binder_thread *thread;
2996 thread = rb_entry(n, struct binder_thread, rb_node);
2998 active_transactions += binder_free_thread(proc, thread);
3003 while ((n = rb_first(&proc->nodes))) {
3004 struct binder_node *node;
3006 node = rb_entry(n, struct binder_node, rb_node);
3008 rb_erase(&node->rb_node, &proc->nodes);
3009 incoming_refs = binder_node_release(node, incoming_refs);
3013 while ((n = rb_first(&proc->refs_by_desc))) {
3014 struct binder_ref *ref;
3016 ref = rb_entry(n, struct binder_ref, rb_node_desc);
3018 binder_delete_ref(ref);
3021 binder_release_work(&proc->todo);
3022 binder_release_work(&proc->delivered_death);
3025 while ((n = rb_first(&proc->allocated_buffers))) {
3026 struct binder_buffer *buffer;
3028 buffer = rb_entry(n, struct binder_buffer, rb_node);
3030 t = buffer->transaction;
3033 buffer->transaction = NULL;
3034 pr_err("release proc %d, transaction %d, not freed\n",
3035 proc->pid, t->debug_id);
3039 binder_free_buf(proc, buffer);
3043 binder_stats_deleted(BINDER_STAT_PROC);
3049 for (i = 0; i < proc->buffer_size / PAGE_SIZE; i++) {
3052 if (!proc->pages[i])
3055 page_addr = proc->buffer + i * PAGE_SIZE;
3056 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
3057 "%s: %d: page %d at %p not freed\n",
3058 __func__, proc->pid, i, page_addr);
3059 unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
3060 __free_page(proc->pages[i]);
3064 vfree(proc->buffer);
3067 put_task_struct(proc->tsk);
3069 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
3070 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d, buffers %d, pages %d\n",
3071 __func__, proc->pid, threads, nodes, incoming_refs,
3072 outgoing_refs, active_transactions, buffers, page_count);
3077 static void binder_deferred_func(struct work_struct *work)
3079 struct binder_proc *proc;
3080 struct files_struct *files;
3084 binder_lock(__func__);
3085 mutex_lock(&binder_deferred_lock);
3086 if (!hlist_empty(&binder_deferred_list)) {
3087 proc = hlist_entry(binder_deferred_list.first,
3088 struct binder_proc, deferred_work_node);
3089 hlist_del_init(&proc->deferred_work_node);
3090 defer = proc->deferred_work;
3091 proc->deferred_work = 0;
3096 mutex_unlock(&binder_deferred_lock);
3099 if (defer & BINDER_DEFERRED_PUT_FILES) {
3100 files = proc->files;
3105 if (defer & BINDER_DEFERRED_FLUSH)
3106 binder_deferred_flush(proc);
3108 if (defer & BINDER_DEFERRED_RELEASE)
3109 binder_deferred_release(proc); /* frees proc */
3111 binder_unlock(__func__);
3113 put_files_struct(files);
3116 static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
3119 binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
3121 mutex_lock(&binder_deferred_lock);
3122 proc->deferred_work |= defer;
3123 if (hlist_unhashed(&proc->deferred_work_node)) {
3124 hlist_add_head(&proc->deferred_work_node,
3125 &binder_deferred_list);
3126 queue_work(binder_deferred_workqueue, &binder_deferred_work);
3128 mutex_unlock(&binder_deferred_lock);
3131 static void print_binder_transaction(struct seq_file *m, const char *prefix,
3132 struct binder_transaction *t)
3135 "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %ld r%d",
3136 prefix, t->debug_id, t,
3137 t->from ? t->from->proc->pid : 0,
3138 t->from ? t->from->pid : 0,
3139 t->to_proc ? t->to_proc->pid : 0,
3140 t->to_thread ? t->to_thread->pid : 0,
3141 t->code, t->flags, t->priority, t->need_reply);
3142 if (t->buffer == NULL) {
3143 seq_puts(m, " buffer free\n");
3146 if (t->buffer->target_node)
3147 seq_printf(m, " node %d",
3148 t->buffer->target_node->debug_id);
3149 seq_printf(m, " size %zd:%zd data %p\n",
3150 t->buffer->data_size, t->buffer->offsets_size,
3154 static void print_binder_buffer(struct seq_file *m, const char *prefix,
3155 struct binder_buffer *buffer)
3157 seq_printf(m, "%s %d: %p size %zd:%zd %s\n",
3158 prefix, buffer->debug_id, buffer->data,
3159 buffer->data_size, buffer->offsets_size,
3160 buffer->transaction ? "active" : "delivered");
3163 static void print_binder_work(struct seq_file *m, const char *prefix,
3164 const char *transaction_prefix,
3165 struct binder_work *w)
3167 struct binder_node *node;
3168 struct binder_transaction *t;
3171 case BINDER_WORK_TRANSACTION:
3172 t = container_of(w, struct binder_transaction, work);
3173 print_binder_transaction(m, transaction_prefix, t);
3175 case BINDER_WORK_TRANSACTION_COMPLETE:
3176 seq_printf(m, "%stransaction complete\n", prefix);
3178 case BINDER_WORK_NODE:
3179 node = container_of(w, struct binder_node, work);
3180 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
3181 prefix, node->debug_id,
3182 (u64)node->ptr, (u64)node->cookie);
3184 case BINDER_WORK_DEAD_BINDER:
3185 seq_printf(m, "%shas dead binder\n", prefix);
3187 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
3188 seq_printf(m, "%shas cleared dead binder\n", prefix);
3190 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
3191 seq_printf(m, "%shas cleared death notification\n", prefix);
3194 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
3199 static void print_binder_thread(struct seq_file *m,
3200 struct binder_thread *thread,
3203 struct binder_transaction *t;
3204 struct binder_work *w;
3205 size_t start_pos = m->count;
3208 seq_printf(m, " thread %d: l %02x\n", thread->pid, thread->looper);
3209 header_pos = m->count;
3210 t = thread->transaction_stack;
3212 if (t->from == thread) {
3213 print_binder_transaction(m,
3214 " outgoing transaction", t);
3216 } else if (t->to_thread == thread) {
3217 print_binder_transaction(m,
3218 " incoming transaction", t);
3221 print_binder_transaction(m, " bad transaction", t);
3225 list_for_each_entry(w, &thread->todo, entry) {
3226 print_binder_work(m, " ", " pending transaction", w);
3228 if (!print_always && m->count == header_pos)
3229 m->count = start_pos;
3232 static void print_binder_node(struct seq_file *m, struct binder_node *node)
3234 struct binder_ref *ref;
3235 struct binder_work *w;
3239 hlist_for_each_entry(ref, &node->refs, node_entry)
3242 seq_printf(m, " node %d: u%016llx c%016llx hs %d hw %d ls %d lw %d is %d iw %d",
3243 node->debug_id, (u64)node->ptr, (u64)node->cookie,
3244 node->has_strong_ref, node->has_weak_ref,
3245 node->local_strong_refs, node->local_weak_refs,
3246 node->internal_strong_refs, count);
3248 seq_puts(m, " proc");
3249 hlist_for_each_entry(ref, &node->refs, node_entry)
3250 seq_printf(m, " %d", ref->proc->pid);
3253 list_for_each_entry(w, &node->async_todo, entry)
3254 print_binder_work(m, " ",
3255 " pending async transaction", w);
3258 static void print_binder_ref(struct seq_file *m, struct binder_ref *ref)
3260 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %p\n",
3261 ref->debug_id, ref->desc, ref->node->proc ? "" : "dead ",
3262 ref->node->debug_id, ref->strong, ref->weak, ref->death);
3265 static void print_binder_proc(struct seq_file *m,
3266 struct binder_proc *proc, int print_all)
3268 struct binder_work *w;
3270 size_t start_pos = m->count;
3273 seq_printf(m, "proc %d\n", proc->pid);
3274 header_pos = m->count;
3276 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3277 print_binder_thread(m, rb_entry(n, struct binder_thread,
3278 rb_node), print_all);
3279 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
3280 struct binder_node *node = rb_entry(n, struct binder_node,
3282 if (print_all || node->has_async_transaction)
3283 print_binder_node(m, node);
3286 for (n = rb_first(&proc->refs_by_desc);
3289 print_binder_ref(m, rb_entry(n, struct binder_ref,
3292 for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
3293 print_binder_buffer(m, " buffer",
3294 rb_entry(n, struct binder_buffer, rb_node));
3295 list_for_each_entry(w, &proc->todo, entry)
3296 print_binder_work(m, " ", " pending transaction", w);
3297 list_for_each_entry(w, &proc->delivered_death, entry) {
3298 seq_puts(m, " has delivered dead binder\n");
3301 if (!print_all && m->count == header_pos)
3302 m->count = start_pos;
3305 static const char * const binder_return_strings[] = {
3310 "BR_ACQUIRE_RESULT",
3312 "BR_TRANSACTION_COMPLETE",
3317 "BR_ATTEMPT_ACQUIRE",
3322 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
3326 static const char * const binder_command_strings[] = {
3329 "BC_ACQUIRE_RESULT",
3337 "BC_ATTEMPT_ACQUIRE",
3338 "BC_REGISTER_LOOPER",
3341 "BC_REQUEST_DEATH_NOTIFICATION",
3342 "BC_CLEAR_DEATH_NOTIFICATION",
3343 "BC_DEAD_BINDER_DONE"
3346 static const char * const binder_objstat_strings[] = {
3353 "transaction_complete"
3356 static void print_binder_stats(struct seq_file *m, const char *prefix,
3357 struct binder_stats *stats)
3361 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
3362 ARRAY_SIZE(binder_command_strings));
3363 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
3365 seq_printf(m, "%s%s: %d\n", prefix,
3366 binder_command_strings[i], stats->bc[i]);
3369 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
3370 ARRAY_SIZE(binder_return_strings));
3371 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
3373 seq_printf(m, "%s%s: %d\n", prefix,
3374 binder_return_strings[i], stats->br[i]);
3377 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
3378 ARRAY_SIZE(binder_objstat_strings));
3379 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
3380 ARRAY_SIZE(stats->obj_deleted));
3381 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
3382 if (stats->obj_created[i] || stats->obj_deleted[i])
3383 seq_printf(m, "%s%s: active %d total %d\n", prefix,
3384 binder_objstat_strings[i],
3385 stats->obj_created[i] - stats->obj_deleted[i],
3386 stats->obj_created[i]);
3390 static void print_binder_proc_stats(struct seq_file *m,
3391 struct binder_proc *proc)
3393 struct binder_work *w;
3395 int count, strong, weak;
3397 seq_printf(m, "proc %d\n", proc->pid);
3399 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3401 seq_printf(m, " threads: %d\n", count);
3402 seq_printf(m, " requested threads: %d+%d/%d\n"
3403 " ready threads %d\n"
3404 " free async space %zd\n", proc->requested_threads,
3405 proc->requested_threads_started, proc->max_threads,
3406 proc->ready_threads, proc->free_async_space);
3408 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
3410 seq_printf(m, " nodes: %d\n", count);
3414 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
3415 struct binder_ref *ref = rb_entry(n, struct binder_ref,
3418 strong += ref->strong;
3421 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
3424 for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
3426 seq_printf(m, " buffers: %d\n", count);
3429 list_for_each_entry(w, &proc->todo, entry) {
3431 case BINDER_WORK_TRANSACTION:
3438 seq_printf(m, " pending transactions: %d\n", count);
3440 print_binder_stats(m, " ", &proc->stats);
3444 static int binder_state_show(struct seq_file *m, void *unused)
3446 struct binder_proc *proc;
3447 struct binder_node *node;
3448 int do_lock = !binder_debug_no_lock;
3451 binder_lock(__func__);
3453 seq_puts(m, "binder state:\n");
3455 if (!hlist_empty(&binder_dead_nodes))
3456 seq_puts(m, "dead nodes:\n");
3457 hlist_for_each_entry(node, &binder_dead_nodes, dead_node)
3458 print_binder_node(m, node);
3460 hlist_for_each_entry(proc, &binder_procs, proc_node)
3461 print_binder_proc(m, proc, 1);
3463 binder_unlock(__func__);
3467 static int binder_stats_show(struct seq_file *m, void *unused)
3469 struct binder_proc *proc;
3470 int do_lock = !binder_debug_no_lock;
3473 binder_lock(__func__);
3475 seq_puts(m, "binder stats:\n");
3477 print_binder_stats(m, "", &binder_stats);
3479 hlist_for_each_entry(proc, &binder_procs, proc_node)
3480 print_binder_proc_stats(m, proc);
3482 binder_unlock(__func__);
3486 static int binder_transactions_show(struct seq_file *m, void *unused)
3488 struct binder_proc *proc;
3489 int do_lock = !binder_debug_no_lock;
3492 binder_lock(__func__);
3494 seq_puts(m, "binder transactions:\n");
3495 hlist_for_each_entry(proc, &binder_procs, proc_node)
3496 print_binder_proc(m, proc, 0);
3498 binder_unlock(__func__);
3502 static int binder_proc_show(struct seq_file *m, void *unused)
3504 struct binder_proc *proc = m->private;
3505 int do_lock = !binder_debug_no_lock;
3508 binder_lock(__func__);
3509 seq_puts(m, "binder proc state:\n");
3510 print_binder_proc(m, proc, 1);
3512 binder_unlock(__func__);
3516 static void print_binder_transaction_log_entry(struct seq_file *m,
3517 struct binder_transaction_log_entry *e)
3520 "%d: %s from %d:%d to %d:%d node %d handle %d size %d:%d\n",
3521 e->debug_id, (e->call_type == 2) ? "reply" :
3522 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
3523 e->from_thread, e->to_proc, e->to_thread, e->to_node,
3524 e->target_handle, e->data_size, e->offsets_size);
3527 static int binder_transaction_log_show(struct seq_file *m, void *unused)
3529 struct binder_transaction_log *log = m->private;
3533 for (i = log->next; i < ARRAY_SIZE(log->entry); i++)
3534 print_binder_transaction_log_entry(m, &log->entry[i]);
3536 for (i = 0; i < log->next; i++)
3537 print_binder_transaction_log_entry(m, &log->entry[i]);
3541 static const struct file_operations binder_fops = {
3542 .owner = THIS_MODULE,
3543 .poll = binder_poll,
3544 .unlocked_ioctl = binder_ioctl,
3545 .compat_ioctl = binder_ioctl,
3546 .mmap = binder_mmap,
3547 .open = binder_open,
3548 .flush = binder_flush,
3549 .release = binder_release,
3552 static struct miscdevice binder_miscdev = {
3553 .minor = MISC_DYNAMIC_MINOR,
3555 .fops = &binder_fops
3558 BINDER_DEBUG_ENTRY(state);
3559 BINDER_DEBUG_ENTRY(stats);
3560 BINDER_DEBUG_ENTRY(transactions);
3561 BINDER_DEBUG_ENTRY(transaction_log);
3563 static int __init binder_init(void)
3567 binder_deferred_workqueue = create_singlethread_workqueue("binder");
3568 if (!binder_deferred_workqueue)
3571 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
3572 if (binder_debugfs_dir_entry_root)
3573 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
3574 binder_debugfs_dir_entry_root);
3575 ret = misc_register(&binder_miscdev);
3576 if (binder_debugfs_dir_entry_root) {
3577 debugfs_create_file("state",
3579 binder_debugfs_dir_entry_root,
3581 &binder_state_fops);
3582 debugfs_create_file("stats",
3584 binder_debugfs_dir_entry_root,
3586 &binder_stats_fops);
3587 debugfs_create_file("transactions",
3589 binder_debugfs_dir_entry_root,
3591 &binder_transactions_fops);
3592 debugfs_create_file("transaction_log",
3594 binder_debugfs_dir_entry_root,
3595 &binder_transaction_log,
3596 &binder_transaction_log_fops);
3597 debugfs_create_file("failed_transaction_log",
3599 binder_debugfs_dir_entry_root,
3600 &binder_transaction_log_failed,
3601 &binder_transaction_log_fops);
3606 device_initcall(binder_init);
3608 #define CREATE_TRACE_POINTS
3609 #include "binder_trace.h"
3611 MODULE_LICENSE("GPL v2");