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/mutex.h>
30 #include <linux/nsproxy.h>
31 #include <linux/poll.h>
32 #include <linux/debugfs.h>
33 #include <linux/rbtree.h>
34 #include <linux/sched.h>
35 #include <linux/seq_file.h>
36 #include <linux/uaccess.h>
37 #include <linux/vmalloc.h>
38 #include <linux/slab.h>
39 #include <linux/pid_namespace.h>
40 #include <linux/security.h>
43 #include "binder_trace.h"
45 static DEFINE_MUTEX(binder_main_lock);
46 static DEFINE_MUTEX(binder_deferred_lock);
47 static DEFINE_MUTEX(binder_mmap_lock);
49 static HLIST_HEAD(binder_procs);
50 static HLIST_HEAD(binder_deferred_list);
51 static HLIST_HEAD(binder_dead_nodes);
53 static struct dentry *binder_debugfs_dir_entry_root;
54 static struct dentry *binder_debugfs_dir_entry_proc;
55 static struct binder_node *binder_context_mgr_node;
56 static kuid_t binder_context_mgr_uid = INVALID_UID;
57 static int binder_last_id;
58 static struct workqueue_struct *binder_deferred_workqueue;
60 #define BINDER_DEBUG_ENTRY(name) \
61 static int binder_##name##_open(struct inode *inode, struct file *file) \
63 return single_open(file, binder_##name##_show, inode->i_private); \
66 static const struct file_operations binder_##name##_fops = { \
67 .owner = THIS_MODULE, \
68 .open = binder_##name##_open, \
70 .llseek = seq_lseek, \
71 .release = single_release, \
74 static int binder_proc_show(struct seq_file *m, void *unused);
75 BINDER_DEBUG_ENTRY(proc);
77 /* This is only defined in include/asm-arm/sizes.h */
83 #define SZ_4M 0x400000
86 #define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
88 #define BINDER_SMALL_BUF_SIZE (PAGE_SIZE * 64)
91 BINDER_DEBUG_USER_ERROR = 1U << 0,
92 BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1,
93 BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2,
94 BINDER_DEBUG_OPEN_CLOSE = 1U << 3,
95 BINDER_DEBUG_DEAD_BINDER = 1U << 4,
96 BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5,
97 BINDER_DEBUG_READ_WRITE = 1U << 6,
98 BINDER_DEBUG_USER_REFS = 1U << 7,
99 BINDER_DEBUG_THREADS = 1U << 8,
100 BINDER_DEBUG_TRANSACTION = 1U << 9,
101 BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10,
102 BINDER_DEBUG_FREE_BUFFER = 1U << 11,
103 BINDER_DEBUG_INTERNAL_REFS = 1U << 12,
104 BINDER_DEBUG_BUFFER_ALLOC = 1U << 13,
105 BINDER_DEBUG_PRIORITY_CAP = 1U << 14,
106 BINDER_DEBUG_BUFFER_ALLOC_ASYNC = 1U << 15,
108 static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR |
109 BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION;
110 module_param_named(debug_mask, binder_debug_mask, uint, S_IWUSR | S_IRUGO);
112 static bool binder_debug_no_lock;
113 module_param_named(proc_no_lock, binder_debug_no_lock, bool, S_IWUSR | S_IRUGO);
115 static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
116 static int binder_stop_on_user_error;
118 static int binder_set_stop_on_user_error(const char *val,
119 struct kernel_param *kp)
122 ret = param_set_int(val, kp);
123 if (binder_stop_on_user_error < 2)
124 wake_up(&binder_user_error_wait);
127 module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
128 param_get_int, &binder_stop_on_user_error, S_IWUSR | S_IRUGO);
130 #define binder_debug(mask, x...) \
132 if (binder_debug_mask & mask) \
136 #define binder_user_error(x...) \
138 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
140 if (binder_stop_on_user_error) \
141 binder_stop_on_user_error = 2; \
144 enum binder_stat_types {
150 BINDER_STAT_TRANSACTION,
151 BINDER_STAT_TRANSACTION_COMPLETE,
155 struct binder_stats {
156 int br[_IOC_NR(BR_FAILED_REPLY) + 1];
157 int bc[_IOC_NR(BC_DEAD_BINDER_DONE) + 1];
158 int obj_created[BINDER_STAT_COUNT];
159 int obj_deleted[BINDER_STAT_COUNT];
162 static struct binder_stats binder_stats;
164 static inline void binder_stats_deleted(enum binder_stat_types type)
166 binder_stats.obj_deleted[type]++;
169 static inline void binder_stats_created(enum binder_stat_types type)
171 binder_stats.obj_created[type]++;
174 struct binder_transaction_log_entry {
186 struct binder_transaction_log {
189 struct binder_transaction_log_entry entry[32];
191 static struct binder_transaction_log binder_transaction_log;
192 static struct binder_transaction_log binder_transaction_log_failed;
194 static struct binder_transaction_log_entry *binder_transaction_log_add(
195 struct binder_transaction_log *log)
197 struct binder_transaction_log_entry *e;
198 e = &log->entry[log->next];
199 memset(e, 0, sizeof(*e));
201 if (log->next == ARRAY_SIZE(log->entry)) {
209 struct list_head entry;
211 BINDER_WORK_TRANSACTION = 1,
212 BINDER_WORK_TRANSACTION_COMPLETE,
214 BINDER_WORK_DEAD_BINDER,
215 BINDER_WORK_DEAD_BINDER_AND_CLEAR,
216 BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
222 struct binder_work work;
224 struct rb_node rb_node;
225 struct hlist_node dead_node;
227 struct binder_proc *proc;
228 struct hlist_head refs;
229 int internal_strong_refs;
231 int local_strong_refs;
232 binder_uintptr_t ptr;
233 binder_uintptr_t cookie;
234 unsigned has_strong_ref:1;
235 unsigned pending_strong_ref:1;
236 unsigned has_weak_ref:1;
237 unsigned pending_weak_ref:1;
238 unsigned has_async_transaction:1;
239 unsigned accept_fds:1;
240 unsigned min_priority:8;
241 struct list_head async_todo;
244 struct binder_ref_death {
245 struct binder_work work;
246 binder_uintptr_t cookie;
250 /* Lookups needed: */
251 /* node + proc => ref (transaction) */
252 /* desc + proc => ref (transaction, inc/dec ref) */
253 /* node => refs + procs (proc exit) */
255 struct rb_node rb_node_desc;
256 struct rb_node rb_node_node;
257 struct hlist_node node_entry;
258 struct binder_proc *proc;
259 struct binder_node *node;
263 struct binder_ref_death *death;
266 struct binder_buffer {
267 struct list_head entry; /* free and allocated entries by address */
268 struct rb_node rb_node; /* free entry by size or allocated entry */
271 unsigned allow_user_free:1;
272 unsigned async_transaction:1;
273 unsigned debug_id:29;
275 struct binder_transaction *transaction;
277 struct binder_node *target_node;
283 enum binder_deferred_state {
284 BINDER_DEFERRED_PUT_FILES = 0x01,
285 BINDER_DEFERRED_FLUSH = 0x02,
286 BINDER_DEFERRED_RELEASE = 0x04,
290 struct hlist_node proc_node;
291 struct rb_root threads;
292 struct rb_root nodes;
293 struct rb_root refs_by_desc;
294 struct rb_root refs_by_node;
296 struct vm_area_struct *vma;
297 struct mm_struct *vma_vm_mm;
298 struct task_struct *tsk;
299 struct files_struct *files;
300 struct hlist_node deferred_work_node;
303 ptrdiff_t user_buffer_offset;
305 struct list_head buffers;
306 struct rb_root free_buffers;
307 struct rb_root allocated_buffers;
308 size_t free_async_space;
312 uint32_t buffer_free;
313 struct list_head todo;
314 wait_queue_head_t wait;
315 struct binder_stats stats;
316 struct list_head delivered_death;
318 int requested_threads;
319 int requested_threads_started;
321 long default_priority;
322 struct dentry *debugfs_entry;
326 BINDER_LOOPER_STATE_REGISTERED = 0x01,
327 BINDER_LOOPER_STATE_ENTERED = 0x02,
328 BINDER_LOOPER_STATE_EXITED = 0x04,
329 BINDER_LOOPER_STATE_INVALID = 0x08,
330 BINDER_LOOPER_STATE_WAITING = 0x10,
331 BINDER_LOOPER_STATE_NEED_RETURN = 0x20
334 struct binder_thread {
335 struct binder_proc *proc;
336 struct rb_node rb_node;
339 struct binder_transaction *transaction_stack;
340 struct list_head todo;
341 uint32_t return_error; /* Write failed, return error code in read buf */
342 uint32_t return_error2; /* Write failed, return error code in read */
343 /* buffer. Used when sending a reply to a dead process that */
344 /* we are also waiting on */
345 wait_queue_head_t wait;
346 struct binder_stats stats;
349 struct binder_transaction {
351 struct binder_work work;
352 struct binder_thread *from;
353 struct binder_transaction *from_parent;
354 struct binder_proc *to_proc;
355 struct binder_thread *to_thread;
356 struct binder_transaction *to_parent;
357 unsigned need_reply:1;
358 /* unsigned is_dead:1; */ /* not used at the moment */
360 struct binder_buffer *buffer;
369 binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
371 static int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
373 struct files_struct *files = proc->files;
374 unsigned long rlim_cur;
380 if (!lock_task_sighand(proc->tsk, &irqs))
383 rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
384 unlock_task_sighand(proc->tsk, &irqs);
386 return __alloc_fd(files, 0, rlim_cur, flags);
390 * copied from fd_install
392 static void task_fd_install(
393 struct binder_proc *proc, unsigned int fd, struct file *file)
396 __fd_install(proc->files, fd, file);
400 * copied from sys_close
402 static long task_close_fd(struct binder_proc *proc, unsigned int fd)
406 if (proc->files == NULL)
409 retval = __close_fd(proc->files, fd);
410 /* can't restart close syscall because file table entry was cleared */
411 if (unlikely(retval == -ERESTARTSYS ||
412 retval == -ERESTARTNOINTR ||
413 retval == -ERESTARTNOHAND ||
414 retval == -ERESTART_RESTARTBLOCK))
420 static inline void binder_lock(const char *tag)
422 trace_binder_lock(tag);
423 mutex_lock(&binder_main_lock);
424 trace_binder_locked(tag);
427 static inline void binder_unlock(const char *tag)
429 trace_binder_unlock(tag);
430 mutex_unlock(&binder_main_lock);
433 static void binder_set_nice(long nice)
436 if (can_nice(current, nice)) {
437 set_user_nice(current, nice);
440 min_nice = 20 - current->signal->rlim[RLIMIT_NICE].rlim_cur;
441 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
442 "%d: nice value %ld not allowed use %ld instead\n",
443 current->pid, nice, min_nice);
444 set_user_nice(current, min_nice);
447 binder_user_error("%d RLIMIT_NICE not set\n", current->pid);
450 static size_t binder_buffer_size(struct binder_proc *proc,
451 struct binder_buffer *buffer)
453 if (list_is_last(&buffer->entry, &proc->buffers))
454 return proc->buffer + proc->buffer_size - (void *)buffer->data;
456 return (size_t)list_entry(buffer->entry.next,
457 struct binder_buffer, entry) - (size_t)buffer->data;
460 static void binder_insert_free_buffer(struct binder_proc *proc,
461 struct binder_buffer *new_buffer)
463 struct rb_node **p = &proc->free_buffers.rb_node;
464 struct rb_node *parent = NULL;
465 struct binder_buffer *buffer;
467 size_t new_buffer_size;
469 BUG_ON(!new_buffer->free);
471 new_buffer_size = binder_buffer_size(proc, new_buffer);
473 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
474 "%d: add free buffer, size %zd, at %p\n",
475 proc->pid, new_buffer_size, new_buffer);
479 buffer = rb_entry(parent, struct binder_buffer, rb_node);
480 BUG_ON(!buffer->free);
482 buffer_size = binder_buffer_size(proc, buffer);
484 if (new_buffer_size < buffer_size)
485 p = &parent->rb_left;
487 p = &parent->rb_right;
489 rb_link_node(&new_buffer->rb_node, parent, p);
490 rb_insert_color(&new_buffer->rb_node, &proc->free_buffers);
493 static void binder_insert_allocated_buffer(struct binder_proc *proc,
494 struct binder_buffer *new_buffer)
496 struct rb_node **p = &proc->allocated_buffers.rb_node;
497 struct rb_node *parent = NULL;
498 struct binder_buffer *buffer;
500 BUG_ON(new_buffer->free);
504 buffer = rb_entry(parent, struct binder_buffer, rb_node);
505 BUG_ON(buffer->free);
507 if (new_buffer < buffer)
508 p = &parent->rb_left;
509 else if (new_buffer > buffer)
510 p = &parent->rb_right;
514 rb_link_node(&new_buffer->rb_node, parent, p);
515 rb_insert_color(&new_buffer->rb_node, &proc->allocated_buffers);
518 static struct binder_buffer *binder_buffer_lookup(struct binder_proc *proc,
521 struct rb_node *n = proc->allocated_buffers.rb_node;
522 struct binder_buffer *buffer;
523 struct binder_buffer *kern_ptr;
525 kern_ptr = (struct binder_buffer *)(user_ptr - proc->user_buffer_offset
526 - offsetof(struct binder_buffer, data));
529 buffer = rb_entry(n, struct binder_buffer, rb_node);
530 BUG_ON(buffer->free);
532 if (kern_ptr < buffer)
534 else if (kern_ptr > buffer)
542 static int binder_update_page_range(struct binder_proc *proc, int allocate,
543 void *start, void *end,
544 struct vm_area_struct *vma)
547 unsigned long user_page_addr;
548 struct vm_struct tmp_area;
550 struct mm_struct *mm;
552 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
553 "%d: %s pages %p-%p\n", proc->pid,
554 allocate ? "allocate" : "free", start, end);
559 trace_binder_update_page_range(proc, allocate, start, end);
564 mm = get_task_mm(proc->tsk);
567 down_write(&mm->mmap_sem);
569 if (vma && mm != proc->vma_vm_mm) {
570 pr_err("%d: vma mm and task mm mismatch\n",
580 pr_err("%d: binder_alloc_buf failed to map pages in userspace, no vma\n",
585 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
587 struct page **page_array_ptr;
588 page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
591 *page = alloc_page(GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
593 pr_err("%d: binder_alloc_buf failed for page at %p\n",
594 proc->pid, page_addr);
595 goto err_alloc_page_failed;
597 tmp_area.addr = page_addr;
598 tmp_area.size = PAGE_SIZE + PAGE_SIZE /* guard page? */;
599 page_array_ptr = page;
600 ret = map_vm_area(&tmp_area, PAGE_KERNEL, &page_array_ptr);
602 pr_err("%d: binder_alloc_buf failed to map page at %p in kernel\n",
603 proc->pid, page_addr);
604 goto err_map_kernel_failed;
607 (uintptr_t)page_addr + proc->user_buffer_offset;
608 ret = vm_insert_page(vma, user_page_addr, page[0]);
610 pr_err("%d: binder_alloc_buf failed to map page at %lx in userspace\n",
611 proc->pid, user_page_addr);
612 goto err_vm_insert_page_failed;
614 /* vm_insert_page does not seem to increment the refcount */
617 up_write(&mm->mmap_sem);
623 for (page_addr = end - PAGE_SIZE; page_addr >= start;
624 page_addr -= PAGE_SIZE) {
625 page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
627 zap_page_range(vma, (uintptr_t)page_addr +
628 proc->user_buffer_offset, PAGE_SIZE, NULL);
629 err_vm_insert_page_failed:
630 unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
631 err_map_kernel_failed:
634 err_alloc_page_failed:
639 up_write(&mm->mmap_sem);
645 static struct binder_buffer *binder_alloc_buf(struct binder_proc *proc,
647 size_t offsets_size, int is_async)
649 struct rb_node *n = proc->free_buffers.rb_node;
650 struct binder_buffer *buffer;
652 struct rb_node *best_fit = NULL;
657 if (proc->vma == NULL) {
658 pr_err("%d: binder_alloc_buf, no vma\n",
663 size = ALIGN(data_size, sizeof(void *)) +
664 ALIGN(offsets_size, sizeof(void *));
666 if (size < data_size || size < offsets_size) {
667 binder_user_error("%d: got transaction with invalid size %zd-%zd\n",
668 proc->pid, data_size, offsets_size);
673 proc->free_async_space < size + sizeof(struct binder_buffer)) {
674 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
675 "%d: binder_alloc_buf size %zd failed, no async space left\n",
681 buffer = rb_entry(n, struct binder_buffer, rb_node);
682 BUG_ON(!buffer->free);
683 buffer_size = binder_buffer_size(proc, buffer);
685 if (size < buffer_size) {
688 } else if (size > buffer_size)
695 if (best_fit == NULL) {
696 pr_err("%d: binder_alloc_buf size %zd failed, no address space\n",
701 buffer = rb_entry(best_fit, struct binder_buffer, rb_node);
702 buffer_size = binder_buffer_size(proc, buffer);
705 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
706 "%d: binder_alloc_buf size %zd got buffer %p size %zd\n",
707 proc->pid, size, buffer, buffer_size);
710 (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK);
712 if (size + sizeof(struct binder_buffer) + 4 >= buffer_size)
713 buffer_size = size; /* no room for other buffers */
715 buffer_size = size + sizeof(struct binder_buffer);
718 (void *)PAGE_ALIGN((uintptr_t)buffer->data + buffer_size);
719 if (end_page_addr > has_page_addr)
720 end_page_addr = has_page_addr;
721 if (binder_update_page_range(proc, 1,
722 (void *)PAGE_ALIGN((uintptr_t)buffer->data), end_page_addr, NULL))
725 rb_erase(best_fit, &proc->free_buffers);
727 binder_insert_allocated_buffer(proc, buffer);
728 if (buffer_size != size) {
729 struct binder_buffer *new_buffer = (void *)buffer->data + size;
730 list_add(&new_buffer->entry, &buffer->entry);
731 new_buffer->free = 1;
732 binder_insert_free_buffer(proc, new_buffer);
734 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
735 "%d: binder_alloc_buf size %zd got %p\n",
736 proc->pid, size, buffer);
737 buffer->data_size = data_size;
738 buffer->offsets_size = offsets_size;
739 buffer->async_transaction = is_async;
741 proc->free_async_space -= size + sizeof(struct binder_buffer);
742 binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
743 "%d: binder_alloc_buf size %zd async free %zd\n",
744 proc->pid, size, proc->free_async_space);
750 static void *buffer_start_page(struct binder_buffer *buffer)
752 return (void *)((uintptr_t)buffer & PAGE_MASK);
755 static void *buffer_end_page(struct binder_buffer *buffer)
757 return (void *)(((uintptr_t)(buffer + 1) - 1) & PAGE_MASK);
760 static void binder_delete_free_buffer(struct binder_proc *proc,
761 struct binder_buffer *buffer)
763 struct binder_buffer *prev, *next = NULL;
764 int free_page_end = 1;
765 int free_page_start = 1;
767 BUG_ON(proc->buffers.next == &buffer->entry);
768 prev = list_entry(buffer->entry.prev, struct binder_buffer, entry);
770 if (buffer_end_page(prev) == buffer_start_page(buffer)) {
772 if (buffer_end_page(prev) == buffer_end_page(buffer))
774 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
775 "%d: merge free, buffer %p share page with %p\n",
776 proc->pid, buffer, prev);
779 if (!list_is_last(&buffer->entry, &proc->buffers)) {
780 next = list_entry(buffer->entry.next,
781 struct binder_buffer, entry);
782 if (buffer_start_page(next) == buffer_end_page(buffer)) {
784 if (buffer_start_page(next) ==
785 buffer_start_page(buffer))
787 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
788 "%d: merge free, buffer %p share page with %p\n",
789 proc->pid, buffer, prev);
792 list_del(&buffer->entry);
793 if (free_page_start || free_page_end) {
794 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
795 "%d: merge free, buffer %p do not share page%s%s with %p or %p\n",
796 proc->pid, buffer, free_page_start ? "" : " end",
797 free_page_end ? "" : " start", prev, next);
798 binder_update_page_range(proc, 0, free_page_start ?
799 buffer_start_page(buffer) : buffer_end_page(buffer),
800 (free_page_end ? buffer_end_page(buffer) :
801 buffer_start_page(buffer)) + PAGE_SIZE, NULL);
805 static void binder_free_buf(struct binder_proc *proc,
806 struct binder_buffer *buffer)
808 size_t size, buffer_size;
810 buffer_size = binder_buffer_size(proc, buffer);
812 size = ALIGN(buffer->data_size, sizeof(void *)) +
813 ALIGN(buffer->offsets_size, sizeof(void *));
815 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
816 "%d: binder_free_buf %p size %zd buffer_size %zd\n",
817 proc->pid, buffer, size, buffer_size);
819 BUG_ON(buffer->free);
820 BUG_ON(size > buffer_size);
821 BUG_ON(buffer->transaction != NULL);
822 BUG_ON((void *)buffer < proc->buffer);
823 BUG_ON((void *)buffer > proc->buffer + proc->buffer_size);
825 if (buffer->async_transaction) {
826 proc->free_async_space += size + sizeof(struct binder_buffer);
828 binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
829 "%d: binder_free_buf size %zd async free %zd\n",
830 proc->pid, size, proc->free_async_space);
833 binder_update_page_range(proc, 0,
834 (void *)PAGE_ALIGN((uintptr_t)buffer->data),
835 (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK),
837 rb_erase(&buffer->rb_node, &proc->allocated_buffers);
839 if (!list_is_last(&buffer->entry, &proc->buffers)) {
840 struct binder_buffer *next = list_entry(buffer->entry.next,
841 struct binder_buffer, entry);
843 rb_erase(&next->rb_node, &proc->free_buffers);
844 binder_delete_free_buffer(proc, next);
847 if (proc->buffers.next != &buffer->entry) {
848 struct binder_buffer *prev = list_entry(buffer->entry.prev,
849 struct binder_buffer, entry);
851 binder_delete_free_buffer(proc, buffer);
852 rb_erase(&prev->rb_node, &proc->free_buffers);
856 binder_insert_free_buffer(proc, buffer);
859 static struct binder_node *binder_get_node(struct binder_proc *proc,
860 binder_uintptr_t ptr)
862 struct rb_node *n = proc->nodes.rb_node;
863 struct binder_node *node;
866 node = rb_entry(n, struct binder_node, rb_node);
870 else if (ptr > node->ptr)
878 static struct binder_node *binder_new_node(struct binder_proc *proc,
879 binder_uintptr_t ptr,
880 binder_uintptr_t cookie)
882 struct rb_node **p = &proc->nodes.rb_node;
883 struct rb_node *parent = NULL;
884 struct binder_node *node;
888 node = rb_entry(parent, struct binder_node, rb_node);
892 else if (ptr > node->ptr)
898 node = kzalloc(sizeof(*node), GFP_KERNEL);
901 binder_stats_created(BINDER_STAT_NODE);
902 rb_link_node(&node->rb_node, parent, p);
903 rb_insert_color(&node->rb_node, &proc->nodes);
904 node->debug_id = ++binder_last_id;
907 node->cookie = cookie;
908 node->work.type = BINDER_WORK_NODE;
909 INIT_LIST_HEAD(&node->work.entry);
910 INIT_LIST_HEAD(&node->async_todo);
911 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
912 "%d:%d node %d u%016llx c%016llx created\n",
913 proc->pid, current->pid, node->debug_id,
914 (u64)node->ptr, (u64)node->cookie);
918 static int binder_inc_node(struct binder_node *node, int strong, int internal,
919 struct list_head *target_list)
923 if (target_list == NULL &&
924 node->internal_strong_refs == 0 &&
925 !(node == binder_context_mgr_node &&
926 node->has_strong_ref)) {
927 pr_err("invalid inc strong node for %d\n",
931 node->internal_strong_refs++;
933 node->local_strong_refs++;
934 if (!node->has_strong_ref && target_list) {
935 list_del_init(&node->work.entry);
936 list_add_tail(&node->work.entry, target_list);
940 node->local_weak_refs++;
941 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
942 if (target_list == NULL) {
943 pr_err("invalid inc weak node for %d\n",
947 list_add_tail(&node->work.entry, target_list);
953 static int binder_dec_node(struct binder_node *node, int strong, int internal)
957 node->internal_strong_refs--;
959 node->local_strong_refs--;
960 if (node->local_strong_refs || node->internal_strong_refs)
964 node->local_weak_refs--;
965 if (node->local_weak_refs || !hlist_empty(&node->refs))
968 if (node->proc && (node->has_strong_ref || node->has_weak_ref)) {
969 if (list_empty(&node->work.entry)) {
970 list_add_tail(&node->work.entry, &node->proc->todo);
971 wake_up_interruptible(&node->proc->wait);
974 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
975 !node->local_weak_refs) {
976 list_del_init(&node->work.entry);
978 rb_erase(&node->rb_node, &node->proc->nodes);
979 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
980 "refless node %d deleted\n",
983 hlist_del(&node->dead_node);
984 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
985 "dead node %d deleted\n",
989 binder_stats_deleted(BINDER_STAT_NODE);
997 static struct binder_ref *binder_get_ref(struct binder_proc *proc,
1000 struct rb_node *n = proc->refs_by_desc.rb_node;
1001 struct binder_ref *ref;
1004 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1006 if (desc < ref->desc)
1008 else if (desc > ref->desc)
1016 static struct binder_ref *binder_get_ref_for_node(struct binder_proc *proc,
1017 struct binder_node *node)
1020 struct rb_node **p = &proc->refs_by_node.rb_node;
1021 struct rb_node *parent = NULL;
1022 struct binder_ref *ref, *new_ref;
1026 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1028 if (node < ref->node)
1030 else if (node > ref->node)
1031 p = &(*p)->rb_right;
1035 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1036 if (new_ref == NULL)
1038 binder_stats_created(BINDER_STAT_REF);
1039 new_ref->debug_id = ++binder_last_id;
1040 new_ref->proc = proc;
1041 new_ref->node = node;
1042 rb_link_node(&new_ref->rb_node_node, parent, p);
1043 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1045 new_ref->desc = (node == binder_context_mgr_node) ? 0 : 1;
1046 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1047 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1048 if (ref->desc > new_ref->desc)
1050 new_ref->desc = ref->desc + 1;
1053 p = &proc->refs_by_desc.rb_node;
1056 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1058 if (new_ref->desc < ref->desc)
1060 else if (new_ref->desc > ref->desc)
1061 p = &(*p)->rb_right;
1065 rb_link_node(&new_ref->rb_node_desc, parent, p);
1066 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
1068 hlist_add_head(&new_ref->node_entry, &node->refs);
1070 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1071 "%d new ref %d desc %d for node %d\n",
1072 proc->pid, new_ref->debug_id, new_ref->desc,
1075 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1076 "%d new ref %d desc %d for dead node\n",
1077 proc->pid, new_ref->debug_id, new_ref->desc);
1082 static void binder_delete_ref(struct binder_ref *ref)
1084 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1085 "%d delete ref %d desc %d for node %d\n",
1086 ref->proc->pid, ref->debug_id, ref->desc,
1087 ref->node->debug_id);
1089 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1090 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
1092 binder_dec_node(ref->node, 1, 1);
1093 hlist_del(&ref->node_entry);
1094 binder_dec_node(ref->node, 0, 1);
1096 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1097 "%d delete ref %d desc %d has death notification\n",
1098 ref->proc->pid, ref->debug_id, ref->desc);
1099 list_del(&ref->death->work.entry);
1101 binder_stats_deleted(BINDER_STAT_DEATH);
1104 binder_stats_deleted(BINDER_STAT_REF);
1107 static int binder_inc_ref(struct binder_ref *ref, int strong,
1108 struct list_head *target_list)
1112 if (ref->strong == 0) {
1113 ret = binder_inc_node(ref->node, 1, 1, target_list);
1119 if (ref->weak == 0) {
1120 ret = binder_inc_node(ref->node, 0, 1, target_list);
1130 static int binder_dec_ref(struct binder_ref *ref, int strong)
1133 if (ref->strong == 0) {
1134 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
1135 ref->proc->pid, ref->debug_id,
1136 ref->desc, ref->strong, ref->weak);
1140 if (ref->strong == 0) {
1142 ret = binder_dec_node(ref->node, strong, 1);
1147 if (ref->weak == 0) {
1148 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
1149 ref->proc->pid, ref->debug_id,
1150 ref->desc, ref->strong, ref->weak);
1155 if (ref->strong == 0 && ref->weak == 0)
1156 binder_delete_ref(ref);
1160 static void binder_pop_transaction(struct binder_thread *target_thread,
1161 struct binder_transaction *t)
1163 if (target_thread) {
1164 BUG_ON(target_thread->transaction_stack != t);
1165 BUG_ON(target_thread->transaction_stack->from != target_thread);
1166 target_thread->transaction_stack =
1167 target_thread->transaction_stack->from_parent;
1172 t->buffer->transaction = NULL;
1174 binder_stats_deleted(BINDER_STAT_TRANSACTION);
1177 static void binder_send_failed_reply(struct binder_transaction *t,
1178 uint32_t error_code)
1180 struct binder_thread *target_thread;
1181 BUG_ON(t->flags & TF_ONE_WAY);
1183 target_thread = t->from;
1184 if (target_thread) {
1185 if (target_thread->return_error != BR_OK &&
1186 target_thread->return_error2 == BR_OK) {
1187 target_thread->return_error2 =
1188 target_thread->return_error;
1189 target_thread->return_error = BR_OK;
1191 if (target_thread->return_error == BR_OK) {
1192 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1193 "send failed reply for transaction %d to %d:%d\n",
1194 t->debug_id, target_thread->proc->pid,
1195 target_thread->pid);
1197 binder_pop_transaction(target_thread, t);
1198 target_thread->return_error = error_code;
1199 wake_up_interruptible(&target_thread->wait);
1201 pr_err("reply failed, target thread, %d:%d, has error code %d already\n",
1202 target_thread->proc->pid,
1204 target_thread->return_error);
1208 struct binder_transaction *next = t->from_parent;
1210 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1211 "send failed reply for transaction %d, target dead\n",
1214 binder_pop_transaction(target_thread, t);
1216 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1217 "reply failed, no target thread at root\n");
1221 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1222 "reply failed, no target thread -- retry %d\n",
1228 static void binder_transaction_buffer_release(struct binder_proc *proc,
1229 struct binder_buffer *buffer,
1230 binder_size_t *failed_at)
1232 binder_size_t *offp, *off_end;
1233 int debug_id = buffer->debug_id;
1235 binder_debug(BINDER_DEBUG_TRANSACTION,
1236 "%d buffer release %d, size %zd-%zd, failed at %p\n",
1237 proc->pid, buffer->debug_id,
1238 buffer->data_size, buffer->offsets_size, failed_at);
1240 if (buffer->target_node)
1241 binder_dec_node(buffer->target_node, 1, 0);
1243 offp = (binder_size_t *)(buffer->data +
1244 ALIGN(buffer->data_size, sizeof(void *)));
1246 off_end = failed_at;
1248 off_end = (void *)offp + buffer->offsets_size;
1249 for (; offp < off_end; offp++) {
1250 struct flat_binder_object *fp;
1251 if (*offp > buffer->data_size - sizeof(*fp) ||
1252 buffer->data_size < sizeof(*fp) ||
1253 !IS_ALIGNED(*offp, sizeof(u32))) {
1254 pr_err("transaction release %d bad offset %lld, size %zd\n",
1255 debug_id, (u64)*offp, buffer->data_size);
1258 fp = (struct flat_binder_object *)(buffer->data + *offp);
1260 case BINDER_TYPE_BINDER:
1261 case BINDER_TYPE_WEAK_BINDER: {
1262 struct binder_node *node = binder_get_node(proc, fp->binder);
1264 pr_err("transaction release %d bad node %016llx\n",
1265 debug_id, (u64)fp->binder);
1268 binder_debug(BINDER_DEBUG_TRANSACTION,
1269 " node %d u%016llx\n",
1270 node->debug_id, (u64)node->ptr);
1271 binder_dec_node(node, fp->type == BINDER_TYPE_BINDER, 0);
1273 case BINDER_TYPE_HANDLE:
1274 case BINDER_TYPE_WEAK_HANDLE: {
1275 struct binder_ref *ref = binder_get_ref(proc, fp->handle);
1277 pr_err("transaction release %d bad handle %d\n",
1278 debug_id, fp->handle);
1281 binder_debug(BINDER_DEBUG_TRANSACTION,
1282 " ref %d desc %d (node %d)\n",
1283 ref->debug_id, ref->desc, ref->node->debug_id);
1284 binder_dec_ref(ref, fp->type == BINDER_TYPE_HANDLE);
1287 case BINDER_TYPE_FD:
1288 binder_debug(BINDER_DEBUG_TRANSACTION,
1289 " fd %d\n", fp->handle);
1291 task_close_fd(proc, fp->handle);
1295 pr_err("transaction release %d bad object type %x\n",
1296 debug_id, fp->type);
1302 static void binder_transaction(struct binder_proc *proc,
1303 struct binder_thread *thread,
1304 struct binder_transaction_data *tr, int reply)
1306 struct binder_transaction *t;
1307 struct binder_work *tcomplete;
1308 binder_size_t *offp, *off_end;
1309 binder_size_t off_min;
1310 struct binder_proc *target_proc;
1311 struct binder_thread *target_thread = NULL;
1312 struct binder_node *target_node = NULL;
1313 struct list_head *target_list;
1314 wait_queue_head_t *target_wait;
1315 struct binder_transaction *in_reply_to = NULL;
1316 struct binder_transaction_log_entry *e;
1317 uint32_t return_error;
1319 e = binder_transaction_log_add(&binder_transaction_log);
1320 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
1321 e->from_proc = proc->pid;
1322 e->from_thread = thread->pid;
1323 e->target_handle = tr->target.handle;
1324 e->data_size = tr->data_size;
1325 e->offsets_size = tr->offsets_size;
1328 in_reply_to = thread->transaction_stack;
1329 if (in_reply_to == NULL) {
1330 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
1331 proc->pid, thread->pid);
1332 return_error = BR_FAILED_REPLY;
1333 goto err_empty_call_stack;
1335 binder_set_nice(in_reply_to->saved_priority);
1336 if (in_reply_to->to_thread != thread) {
1337 binder_user_error("%d:%d got reply transaction with bad transaction stack, transaction %d has target %d:%d\n",
1338 proc->pid, thread->pid, in_reply_to->debug_id,
1339 in_reply_to->to_proc ?
1340 in_reply_to->to_proc->pid : 0,
1341 in_reply_to->to_thread ?
1342 in_reply_to->to_thread->pid : 0);
1343 return_error = BR_FAILED_REPLY;
1345 goto err_bad_call_stack;
1347 thread->transaction_stack = in_reply_to->to_parent;
1348 target_thread = in_reply_to->from;
1349 if (target_thread == NULL) {
1350 return_error = BR_DEAD_REPLY;
1351 goto err_dead_binder;
1353 if (target_thread->transaction_stack != in_reply_to) {
1354 binder_user_error("%d:%d got reply transaction with bad target transaction stack %d, expected %d\n",
1355 proc->pid, thread->pid,
1356 target_thread->transaction_stack ?
1357 target_thread->transaction_stack->debug_id : 0,
1358 in_reply_to->debug_id);
1359 return_error = BR_FAILED_REPLY;
1361 target_thread = NULL;
1362 goto err_dead_binder;
1364 target_proc = target_thread->proc;
1366 if (tr->target.handle) {
1367 struct binder_ref *ref;
1368 ref = binder_get_ref(proc, tr->target.handle);
1370 binder_user_error("%d:%d got transaction to invalid handle\n",
1371 proc->pid, thread->pid);
1372 return_error = BR_FAILED_REPLY;
1373 goto err_invalid_target_handle;
1375 target_node = ref->node;
1377 target_node = binder_context_mgr_node;
1378 if (target_node == NULL) {
1379 return_error = BR_DEAD_REPLY;
1380 goto err_no_context_mgr_node;
1383 e->to_node = target_node->debug_id;
1384 target_proc = target_node->proc;
1385 if (target_proc == NULL) {
1386 return_error = BR_DEAD_REPLY;
1387 goto err_dead_binder;
1389 if (security_binder_transaction(proc->tsk, target_proc->tsk) < 0) {
1390 return_error = BR_FAILED_REPLY;
1391 goto err_invalid_target_handle;
1393 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
1394 struct binder_transaction *tmp;
1395 tmp = thread->transaction_stack;
1396 if (tmp->to_thread != thread) {
1397 binder_user_error("%d:%d got new transaction with bad transaction stack, transaction %d has target %d:%d\n",
1398 proc->pid, thread->pid, tmp->debug_id,
1399 tmp->to_proc ? tmp->to_proc->pid : 0,
1401 tmp->to_thread->pid : 0);
1402 return_error = BR_FAILED_REPLY;
1403 goto err_bad_call_stack;
1406 if (tmp->from && tmp->from->proc == target_proc)
1407 target_thread = tmp->from;
1408 tmp = tmp->from_parent;
1412 if (target_thread) {
1413 e->to_thread = target_thread->pid;
1414 target_list = &target_thread->todo;
1415 target_wait = &target_thread->wait;
1417 target_list = &target_proc->todo;
1418 target_wait = &target_proc->wait;
1420 e->to_proc = target_proc->pid;
1422 /* TODO: reuse incoming transaction for reply */
1423 t = kzalloc(sizeof(*t), GFP_KERNEL);
1425 return_error = BR_FAILED_REPLY;
1426 goto err_alloc_t_failed;
1428 binder_stats_created(BINDER_STAT_TRANSACTION);
1430 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
1431 if (tcomplete == NULL) {
1432 return_error = BR_FAILED_REPLY;
1433 goto err_alloc_tcomplete_failed;
1435 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
1437 t->debug_id = ++binder_last_id;
1438 e->debug_id = t->debug_id;
1441 binder_debug(BINDER_DEBUG_TRANSACTION,
1442 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld\n",
1443 proc->pid, thread->pid, t->debug_id,
1444 target_proc->pid, target_thread->pid,
1445 (u64)tr->data.ptr.buffer,
1446 (u64)tr->data.ptr.offsets,
1447 (u64)tr->data_size, (u64)tr->offsets_size);
1449 binder_debug(BINDER_DEBUG_TRANSACTION,
1450 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld\n",
1451 proc->pid, thread->pid, t->debug_id,
1452 target_proc->pid, target_node->debug_id,
1453 (u64)tr->data.ptr.buffer,
1454 (u64)tr->data.ptr.offsets,
1455 (u64)tr->data_size, (u64)tr->offsets_size);
1457 if (!reply && !(tr->flags & TF_ONE_WAY))
1461 t->sender_euid = proc->tsk->cred->euid;
1462 t->to_proc = target_proc;
1463 t->to_thread = target_thread;
1465 t->flags = tr->flags;
1466 t->priority = task_nice(current);
1468 trace_binder_transaction(reply, t, target_node);
1470 t->buffer = binder_alloc_buf(target_proc, tr->data_size,
1471 tr->offsets_size, !reply && (t->flags & TF_ONE_WAY));
1472 if (t->buffer == NULL) {
1473 return_error = BR_FAILED_REPLY;
1474 goto err_binder_alloc_buf_failed;
1476 t->buffer->allow_user_free = 0;
1477 t->buffer->debug_id = t->debug_id;
1478 t->buffer->transaction = t;
1479 t->buffer->target_node = target_node;
1480 trace_binder_transaction_alloc_buf(t->buffer);
1482 binder_inc_node(target_node, 1, 0, NULL);
1484 offp = (binder_size_t *)(t->buffer->data +
1485 ALIGN(tr->data_size, sizeof(void *)));
1487 if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t)
1488 tr->data.ptr.buffer, tr->data_size)) {
1489 binder_user_error("%d:%d got transaction with invalid data ptr\n",
1490 proc->pid, thread->pid);
1491 return_error = BR_FAILED_REPLY;
1492 goto err_copy_data_failed;
1494 if (copy_from_user(offp, (const void __user *)(uintptr_t)
1495 tr->data.ptr.offsets, tr->offsets_size)) {
1496 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
1497 proc->pid, thread->pid);
1498 return_error = BR_FAILED_REPLY;
1499 goto err_copy_data_failed;
1501 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
1502 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
1503 proc->pid, thread->pid, (u64)tr->offsets_size);
1504 return_error = BR_FAILED_REPLY;
1505 goto err_bad_offset;
1507 off_end = (void *)offp + tr->offsets_size;
1509 for (; offp < off_end; offp++) {
1510 struct flat_binder_object *fp;
1511 if (*offp > t->buffer->data_size - sizeof(*fp) ||
1513 t->buffer->data_size < sizeof(*fp) ||
1514 !IS_ALIGNED(*offp, sizeof(u32))) {
1515 binder_user_error("%d:%d got transaction with invalid offset, %lld (min %lld, max %lld)\n",
1516 proc->pid, thread->pid, (u64)*offp,
1518 (u64)(t->buffer->data_size -
1520 return_error = BR_FAILED_REPLY;
1521 goto err_bad_offset;
1523 fp = (struct flat_binder_object *)(t->buffer->data + *offp);
1524 off_min = *offp + sizeof(struct flat_binder_object);
1526 case BINDER_TYPE_BINDER:
1527 case BINDER_TYPE_WEAK_BINDER: {
1528 struct binder_ref *ref;
1529 struct binder_node *node = binder_get_node(proc, fp->binder);
1531 node = binder_new_node(proc, fp->binder, fp->cookie);
1533 return_error = BR_FAILED_REPLY;
1534 goto err_binder_new_node_failed;
1536 node->min_priority = fp->flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
1537 node->accept_fds = !!(fp->flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
1539 if (fp->cookie != node->cookie) {
1540 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
1541 proc->pid, thread->pid,
1542 (u64)fp->binder, node->debug_id,
1543 (u64)fp->cookie, (u64)node->cookie);
1544 goto err_binder_get_ref_for_node_failed;
1546 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
1547 return_error = BR_FAILED_REPLY;
1548 goto err_binder_get_ref_for_node_failed;
1550 ref = binder_get_ref_for_node(target_proc, node);
1552 return_error = BR_FAILED_REPLY;
1553 goto err_binder_get_ref_for_node_failed;
1555 if (fp->type == BINDER_TYPE_BINDER)
1556 fp->type = BINDER_TYPE_HANDLE;
1558 fp->type = BINDER_TYPE_WEAK_HANDLE;
1559 fp->handle = ref->desc;
1560 binder_inc_ref(ref, fp->type == BINDER_TYPE_HANDLE,
1563 trace_binder_transaction_node_to_ref(t, node, ref);
1564 binder_debug(BINDER_DEBUG_TRANSACTION,
1565 " node %d u%016llx -> ref %d desc %d\n",
1566 node->debug_id, (u64)node->ptr,
1567 ref->debug_id, ref->desc);
1569 case BINDER_TYPE_HANDLE:
1570 case BINDER_TYPE_WEAK_HANDLE: {
1571 struct binder_ref *ref = binder_get_ref(proc, fp->handle);
1573 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
1575 thread->pid, fp->handle);
1576 return_error = BR_FAILED_REPLY;
1577 goto err_binder_get_ref_failed;
1579 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
1580 return_error = BR_FAILED_REPLY;
1581 goto err_binder_get_ref_failed;
1583 if (ref->node->proc == target_proc) {
1584 if (fp->type == BINDER_TYPE_HANDLE)
1585 fp->type = BINDER_TYPE_BINDER;
1587 fp->type = BINDER_TYPE_WEAK_BINDER;
1588 fp->binder = ref->node->ptr;
1589 fp->cookie = ref->node->cookie;
1590 binder_inc_node(ref->node, fp->type == BINDER_TYPE_BINDER, 0, NULL);
1591 trace_binder_transaction_ref_to_node(t, ref);
1592 binder_debug(BINDER_DEBUG_TRANSACTION,
1593 " ref %d desc %d -> node %d u%016llx\n",
1594 ref->debug_id, ref->desc, ref->node->debug_id,
1595 (u64)ref->node->ptr);
1597 struct binder_ref *new_ref;
1598 new_ref = binder_get_ref_for_node(target_proc, ref->node);
1599 if (new_ref == NULL) {
1600 return_error = BR_FAILED_REPLY;
1601 goto err_binder_get_ref_for_node_failed;
1603 fp->handle = new_ref->desc;
1604 binder_inc_ref(new_ref, fp->type == BINDER_TYPE_HANDLE, NULL);
1605 trace_binder_transaction_ref_to_ref(t, ref,
1607 binder_debug(BINDER_DEBUG_TRANSACTION,
1608 " ref %d desc %d -> ref %d desc %d (node %d)\n",
1609 ref->debug_id, ref->desc, new_ref->debug_id,
1610 new_ref->desc, ref->node->debug_id);
1614 case BINDER_TYPE_FD: {
1619 if (!(in_reply_to->flags & TF_ACCEPT_FDS)) {
1620 binder_user_error("%d:%d got reply with fd, %d, but target does not allow fds\n",
1621 proc->pid, thread->pid, fp->handle);
1622 return_error = BR_FAILED_REPLY;
1623 goto err_fd_not_allowed;
1625 } else if (!target_node->accept_fds) {
1626 binder_user_error("%d:%d got transaction with fd, %d, but target does not allow fds\n",
1627 proc->pid, thread->pid, fp->handle);
1628 return_error = BR_FAILED_REPLY;
1629 goto err_fd_not_allowed;
1632 file = fget(fp->handle);
1634 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
1635 proc->pid, thread->pid, fp->handle);
1636 return_error = BR_FAILED_REPLY;
1637 goto err_fget_failed;
1639 if (security_binder_transfer_file(proc->tsk, target_proc->tsk, file) < 0) {
1641 return_error = BR_FAILED_REPLY;
1642 goto err_get_unused_fd_failed;
1644 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
1645 if (target_fd < 0) {
1647 return_error = BR_FAILED_REPLY;
1648 goto err_get_unused_fd_failed;
1650 task_fd_install(target_proc, target_fd, file);
1651 trace_binder_transaction_fd(t, fp->handle, target_fd);
1652 binder_debug(BINDER_DEBUG_TRANSACTION,
1653 " fd %d -> %d\n", fp->handle, target_fd);
1655 fp->handle = target_fd;
1659 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
1660 proc->pid, thread->pid, fp->type);
1661 return_error = BR_FAILED_REPLY;
1662 goto err_bad_object_type;
1666 BUG_ON(t->buffer->async_transaction != 0);
1667 binder_pop_transaction(target_thread, in_reply_to);
1668 } else if (!(t->flags & TF_ONE_WAY)) {
1669 BUG_ON(t->buffer->async_transaction != 0);
1671 t->from_parent = thread->transaction_stack;
1672 thread->transaction_stack = t;
1674 BUG_ON(target_node == NULL);
1675 BUG_ON(t->buffer->async_transaction != 1);
1676 if (target_node->has_async_transaction) {
1677 target_list = &target_node->async_todo;
1680 target_node->has_async_transaction = 1;
1682 t->work.type = BINDER_WORK_TRANSACTION;
1683 list_add_tail(&t->work.entry, target_list);
1684 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
1685 list_add_tail(&tcomplete->entry, &thread->todo);
1687 wake_up_interruptible(target_wait);
1690 err_get_unused_fd_failed:
1693 err_binder_get_ref_for_node_failed:
1694 err_binder_get_ref_failed:
1695 err_binder_new_node_failed:
1696 err_bad_object_type:
1698 err_copy_data_failed:
1699 trace_binder_transaction_failed_buffer_release(t->buffer);
1700 binder_transaction_buffer_release(target_proc, t->buffer, offp);
1701 t->buffer->transaction = NULL;
1702 binder_free_buf(target_proc, t->buffer);
1703 err_binder_alloc_buf_failed:
1705 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
1706 err_alloc_tcomplete_failed:
1708 binder_stats_deleted(BINDER_STAT_TRANSACTION);
1711 err_empty_call_stack:
1713 err_invalid_target_handle:
1714 err_no_context_mgr_node:
1715 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1716 "%d:%d transaction failed %d, size %lld-%lld\n",
1717 proc->pid, thread->pid, return_error,
1718 (u64)tr->data_size, (u64)tr->offsets_size);
1721 struct binder_transaction_log_entry *fe;
1722 fe = binder_transaction_log_add(&binder_transaction_log_failed);
1726 BUG_ON(thread->return_error != BR_OK);
1728 thread->return_error = BR_TRANSACTION_COMPLETE;
1729 binder_send_failed_reply(in_reply_to, return_error);
1731 thread->return_error = return_error;
1734 int binder_thread_write(struct binder_proc *proc, struct binder_thread *thread,
1735 binder_uintptr_t binder_buffer, size_t size,
1736 binder_size_t *consumed)
1739 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
1740 void __user *ptr = buffer + *consumed;
1741 void __user *end = buffer + size;
1743 while (ptr < end && thread->return_error == BR_OK) {
1744 if (get_user(cmd, (uint32_t __user *)ptr))
1746 ptr += sizeof(uint32_t);
1747 trace_binder_command(cmd);
1748 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
1749 binder_stats.bc[_IOC_NR(cmd)]++;
1750 proc->stats.bc[_IOC_NR(cmd)]++;
1751 thread->stats.bc[_IOC_NR(cmd)]++;
1759 struct binder_ref *ref;
1760 const char *debug_string;
1762 if (get_user(target, (uint32_t __user *)ptr))
1764 ptr += sizeof(uint32_t);
1765 if (target == 0 && binder_context_mgr_node &&
1766 (cmd == BC_INCREFS || cmd == BC_ACQUIRE)) {
1767 ref = binder_get_ref_for_node(proc,
1768 binder_context_mgr_node);
1769 if (ref->desc != target) {
1770 binder_user_error("%d:%d tried to acquire reference to desc 0, got %d instead\n",
1771 proc->pid, thread->pid,
1775 ref = binder_get_ref(proc, target);
1777 binder_user_error("%d:%d refcount change on invalid ref %d\n",
1778 proc->pid, thread->pid, target);
1783 debug_string = "IncRefs";
1784 binder_inc_ref(ref, 0, NULL);
1787 debug_string = "Acquire";
1788 binder_inc_ref(ref, 1, NULL);
1791 debug_string = "Release";
1792 binder_dec_ref(ref, 1);
1796 debug_string = "DecRefs";
1797 binder_dec_ref(ref, 0);
1800 binder_debug(BINDER_DEBUG_USER_REFS,
1801 "%d:%d %s ref %d desc %d s %d w %d for node %d\n",
1802 proc->pid, thread->pid, debug_string, ref->debug_id,
1803 ref->desc, ref->strong, ref->weak, ref->node->debug_id);
1806 case BC_INCREFS_DONE:
1807 case BC_ACQUIRE_DONE: {
1808 binder_uintptr_t node_ptr;
1809 binder_uintptr_t cookie;
1810 struct binder_node *node;
1812 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
1814 ptr += sizeof(binder_uintptr_t);
1815 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
1817 ptr += sizeof(binder_uintptr_t);
1818 node = binder_get_node(proc, node_ptr);
1820 binder_user_error("%d:%d %s u%016llx no match\n",
1821 proc->pid, thread->pid,
1822 cmd == BC_INCREFS_DONE ?
1828 if (cookie != node->cookie) {
1829 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
1830 proc->pid, thread->pid,
1831 cmd == BC_INCREFS_DONE ?
1832 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
1833 (u64)node_ptr, node->debug_id,
1834 (u64)cookie, (u64)node->cookie);
1837 if (cmd == BC_ACQUIRE_DONE) {
1838 if (node->pending_strong_ref == 0) {
1839 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
1840 proc->pid, thread->pid,
1844 node->pending_strong_ref = 0;
1846 if (node->pending_weak_ref == 0) {
1847 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
1848 proc->pid, thread->pid,
1852 node->pending_weak_ref = 0;
1854 binder_dec_node(node, cmd == BC_ACQUIRE_DONE, 0);
1855 binder_debug(BINDER_DEBUG_USER_REFS,
1856 "%d:%d %s node %d ls %d lw %d\n",
1857 proc->pid, thread->pid,
1858 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
1859 node->debug_id, node->local_strong_refs, node->local_weak_refs);
1862 case BC_ATTEMPT_ACQUIRE:
1863 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
1865 case BC_ACQUIRE_RESULT:
1866 pr_err("BC_ACQUIRE_RESULT not supported\n");
1869 case BC_FREE_BUFFER: {
1870 binder_uintptr_t data_ptr;
1871 struct binder_buffer *buffer;
1873 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
1875 ptr += sizeof(binder_uintptr_t);
1877 buffer = binder_buffer_lookup(proc, data_ptr);
1878 if (buffer == NULL) {
1879 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
1880 proc->pid, thread->pid, (u64)data_ptr);
1883 if (!buffer->allow_user_free) {
1884 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
1885 proc->pid, thread->pid, (u64)data_ptr);
1888 binder_debug(BINDER_DEBUG_FREE_BUFFER,
1889 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
1890 proc->pid, thread->pid, (u64)data_ptr, buffer->debug_id,
1891 buffer->transaction ? "active" : "finished");
1893 if (buffer->transaction) {
1894 buffer->transaction->buffer = NULL;
1895 buffer->transaction = NULL;
1897 if (buffer->async_transaction && buffer->target_node) {
1898 BUG_ON(!buffer->target_node->has_async_transaction);
1899 if (list_empty(&buffer->target_node->async_todo))
1900 buffer->target_node->has_async_transaction = 0;
1902 list_move_tail(buffer->target_node->async_todo.next, &thread->todo);
1904 trace_binder_transaction_buffer_release(buffer);
1905 binder_transaction_buffer_release(proc, buffer, NULL);
1906 binder_free_buf(proc, buffer);
1910 case BC_TRANSACTION:
1912 struct binder_transaction_data tr;
1914 if (copy_from_user(&tr, ptr, sizeof(tr)))
1917 binder_transaction(proc, thread, &tr, cmd == BC_REPLY);
1921 case BC_REGISTER_LOOPER:
1922 binder_debug(BINDER_DEBUG_THREADS,
1923 "%d:%d BC_REGISTER_LOOPER\n",
1924 proc->pid, thread->pid);
1925 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
1926 thread->looper |= BINDER_LOOPER_STATE_INVALID;
1927 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
1928 proc->pid, thread->pid);
1929 } else if (proc->requested_threads == 0) {
1930 thread->looper |= BINDER_LOOPER_STATE_INVALID;
1931 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
1932 proc->pid, thread->pid);
1934 proc->requested_threads--;
1935 proc->requested_threads_started++;
1937 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
1939 case BC_ENTER_LOOPER:
1940 binder_debug(BINDER_DEBUG_THREADS,
1941 "%d:%d BC_ENTER_LOOPER\n",
1942 proc->pid, thread->pid);
1943 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
1944 thread->looper |= BINDER_LOOPER_STATE_INVALID;
1945 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
1946 proc->pid, thread->pid);
1948 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
1950 case BC_EXIT_LOOPER:
1951 binder_debug(BINDER_DEBUG_THREADS,
1952 "%d:%d BC_EXIT_LOOPER\n",
1953 proc->pid, thread->pid);
1954 thread->looper |= BINDER_LOOPER_STATE_EXITED;
1957 case BC_REQUEST_DEATH_NOTIFICATION:
1958 case BC_CLEAR_DEATH_NOTIFICATION: {
1960 binder_uintptr_t cookie;
1961 struct binder_ref *ref;
1962 struct binder_ref_death *death;
1964 if (get_user(target, (uint32_t __user *)ptr))
1966 ptr += sizeof(uint32_t);
1967 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
1969 ptr += sizeof(binder_uintptr_t);
1970 ref = binder_get_ref(proc, target);
1972 binder_user_error("%d:%d %s invalid ref %d\n",
1973 proc->pid, thread->pid,
1974 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
1975 "BC_REQUEST_DEATH_NOTIFICATION" :
1976 "BC_CLEAR_DEATH_NOTIFICATION",
1981 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
1982 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
1983 proc->pid, thread->pid,
1984 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
1985 "BC_REQUEST_DEATH_NOTIFICATION" :
1986 "BC_CLEAR_DEATH_NOTIFICATION",
1987 (u64)cookie, ref->debug_id, ref->desc,
1988 ref->strong, ref->weak, ref->node->debug_id);
1990 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
1992 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
1993 proc->pid, thread->pid);
1996 death = kzalloc(sizeof(*death), GFP_KERNEL);
1997 if (death == NULL) {
1998 thread->return_error = BR_ERROR;
1999 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2000 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
2001 proc->pid, thread->pid);
2004 binder_stats_created(BINDER_STAT_DEATH);
2005 INIT_LIST_HEAD(&death->work.entry);
2006 death->cookie = cookie;
2008 if (ref->node->proc == NULL) {
2009 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
2010 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2011 list_add_tail(&ref->death->work.entry, &thread->todo);
2013 list_add_tail(&ref->death->work.entry, &proc->todo);
2014 wake_up_interruptible(&proc->wait);
2018 if (ref->death == NULL) {
2019 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
2020 proc->pid, thread->pid);
2024 if (death->cookie != cookie) {
2025 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
2026 proc->pid, thread->pid,
2027 (u64)death->cookie, (u64)cookie);
2031 if (list_empty(&death->work.entry)) {
2032 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2033 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2034 list_add_tail(&death->work.entry, &thread->todo);
2036 list_add_tail(&death->work.entry, &proc->todo);
2037 wake_up_interruptible(&proc->wait);
2040 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
2041 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
2045 case BC_DEAD_BINDER_DONE: {
2046 struct binder_work *w;
2047 binder_uintptr_t cookie;
2048 struct binder_ref_death *death = NULL;
2049 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
2052 ptr += sizeof(void *);
2053 list_for_each_entry(w, &proc->delivered_death, entry) {
2054 struct binder_ref_death *tmp_death = container_of(w, struct binder_ref_death, work);
2055 if (tmp_death->cookie == cookie) {
2060 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2061 "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n",
2062 proc->pid, thread->pid, (u64)cookie, death);
2063 if (death == NULL) {
2064 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
2065 proc->pid, thread->pid, (u64)cookie);
2069 list_del_init(&death->work.entry);
2070 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
2071 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2072 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2073 list_add_tail(&death->work.entry, &thread->todo);
2075 list_add_tail(&death->work.entry, &proc->todo);
2076 wake_up_interruptible(&proc->wait);
2082 pr_err("%d:%d unknown command %d\n",
2083 proc->pid, thread->pid, cmd);
2086 *consumed = ptr - buffer;
2091 void binder_stat_br(struct binder_proc *proc, struct binder_thread *thread,
2094 trace_binder_return(cmd);
2095 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
2096 binder_stats.br[_IOC_NR(cmd)]++;
2097 proc->stats.br[_IOC_NR(cmd)]++;
2098 thread->stats.br[_IOC_NR(cmd)]++;
2102 static int binder_has_proc_work(struct binder_proc *proc,
2103 struct binder_thread *thread)
2105 return !list_empty(&proc->todo) ||
2106 (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2109 static int binder_has_thread_work(struct binder_thread *thread)
2111 return !list_empty(&thread->todo) || thread->return_error != BR_OK ||
2112 (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2115 static int binder_thread_read(struct binder_proc *proc,
2116 struct binder_thread *thread,
2117 binder_uintptr_t binder_buffer, size_t size,
2118 binder_size_t *consumed, int non_block)
2120 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
2121 void __user *ptr = buffer + *consumed;
2122 void __user *end = buffer + size;
2125 int wait_for_proc_work;
2127 if (*consumed == 0) {
2128 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
2130 ptr += sizeof(uint32_t);
2134 wait_for_proc_work = thread->transaction_stack == NULL &&
2135 list_empty(&thread->todo);
2137 if (thread->return_error != BR_OK && ptr < end) {
2138 if (thread->return_error2 != BR_OK) {
2139 if (put_user(thread->return_error2, (uint32_t __user *)ptr))
2141 ptr += sizeof(uint32_t);
2142 binder_stat_br(proc, thread, thread->return_error2);
2145 thread->return_error2 = BR_OK;
2147 if (put_user(thread->return_error, (uint32_t __user *)ptr))
2149 ptr += sizeof(uint32_t);
2150 binder_stat_br(proc, thread, thread->return_error);
2151 thread->return_error = BR_OK;
2156 thread->looper |= BINDER_LOOPER_STATE_WAITING;
2157 if (wait_for_proc_work)
2158 proc->ready_threads++;
2160 binder_unlock(__func__);
2162 trace_binder_wait_for_work(wait_for_proc_work,
2163 !!thread->transaction_stack,
2164 !list_empty(&thread->todo));
2165 if (wait_for_proc_work) {
2166 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2167 BINDER_LOOPER_STATE_ENTERED))) {
2168 binder_user_error("%d:%d ERROR: Thread waiting for process work before calling BC_REGISTER_LOOPER or BC_ENTER_LOOPER (state %x)\n",
2169 proc->pid, thread->pid, thread->looper);
2170 wait_event_interruptible(binder_user_error_wait,
2171 binder_stop_on_user_error < 2);
2173 binder_set_nice(proc->default_priority);
2175 if (!binder_has_proc_work(proc, thread))
2178 ret = wait_event_freezable_exclusive(proc->wait, binder_has_proc_work(proc, thread));
2181 if (!binder_has_thread_work(thread))
2184 ret = wait_event_freezable(thread->wait, binder_has_thread_work(thread));
2187 binder_lock(__func__);
2189 if (wait_for_proc_work)
2190 proc->ready_threads--;
2191 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
2198 struct binder_transaction_data tr;
2199 struct binder_work *w;
2200 struct binder_transaction *t = NULL;
2202 if (!list_empty(&thread->todo))
2203 w = list_first_entry(&thread->todo, struct binder_work, entry);
2204 else if (!list_empty(&proc->todo) && wait_for_proc_work)
2205 w = list_first_entry(&proc->todo, struct binder_work, entry);
2207 if (ptr - buffer == 4 && !(thread->looper & BINDER_LOOPER_STATE_NEED_RETURN)) /* no data added */
2212 if (end - ptr < sizeof(tr) + 4)
2216 case BINDER_WORK_TRANSACTION: {
2217 t = container_of(w, struct binder_transaction, work);
2219 case BINDER_WORK_TRANSACTION_COMPLETE: {
2220 cmd = BR_TRANSACTION_COMPLETE;
2221 if (put_user(cmd, (uint32_t __user *)ptr))
2223 ptr += sizeof(uint32_t);
2225 binder_stat_br(proc, thread, cmd);
2226 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
2227 "%d:%d BR_TRANSACTION_COMPLETE\n",
2228 proc->pid, thread->pid);
2230 list_del(&w->entry);
2232 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2234 case BINDER_WORK_NODE: {
2235 struct binder_node *node = container_of(w, struct binder_node, work);
2236 uint32_t cmd = BR_NOOP;
2237 const char *cmd_name;
2238 int strong = node->internal_strong_refs || node->local_strong_refs;
2239 int weak = !hlist_empty(&node->refs) || node->local_weak_refs || strong;
2240 if (weak && !node->has_weak_ref) {
2242 cmd_name = "BR_INCREFS";
2243 node->has_weak_ref = 1;
2244 node->pending_weak_ref = 1;
2245 node->local_weak_refs++;
2246 } else if (strong && !node->has_strong_ref) {
2248 cmd_name = "BR_ACQUIRE";
2249 node->has_strong_ref = 1;
2250 node->pending_strong_ref = 1;
2251 node->local_strong_refs++;
2252 } else if (!strong && node->has_strong_ref) {
2254 cmd_name = "BR_RELEASE";
2255 node->has_strong_ref = 0;
2256 } else if (!weak && node->has_weak_ref) {
2258 cmd_name = "BR_DECREFS";
2259 node->has_weak_ref = 0;
2261 if (cmd != BR_NOOP) {
2262 if (put_user(cmd, (uint32_t __user *)ptr))
2264 ptr += sizeof(uint32_t);
2265 if (put_user(node->ptr,
2266 (binder_uintptr_t __user *)ptr))
2268 ptr += sizeof(binder_uintptr_t);
2269 if (put_user(node->cookie,
2270 (binder_uintptr_t __user *)ptr))
2272 ptr += sizeof(binder_uintptr_t);
2274 binder_stat_br(proc, thread, cmd);
2275 binder_debug(BINDER_DEBUG_USER_REFS,
2276 "%d:%d %s %d u%016llx c%016llx\n",
2277 proc->pid, thread->pid, cmd_name,
2279 (u64)node->ptr, (u64)node->cookie);
2281 list_del_init(&w->entry);
2282 if (!weak && !strong) {
2283 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
2284 "%d:%d node %d u%016llx c%016llx deleted\n",
2285 proc->pid, thread->pid, node->debug_id,
2286 (u64)node->ptr, (u64)node->cookie);
2287 rb_erase(&node->rb_node, &proc->nodes);
2289 binder_stats_deleted(BINDER_STAT_NODE);
2291 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
2292 "%d:%d node %d u%016llx c%016llx state unchanged\n",
2293 proc->pid, thread->pid, node->debug_id,
2294 (u64)node->ptr, (u64)node->cookie);
2298 case BINDER_WORK_DEAD_BINDER:
2299 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2300 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2301 struct binder_ref_death *death;
2304 death = container_of(w, struct binder_ref_death, work);
2305 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
2306 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
2308 cmd = BR_DEAD_BINDER;
2309 if (put_user(cmd, (uint32_t __user *)ptr))
2311 ptr += sizeof(uint32_t);
2312 if (put_user(death->cookie,
2313 (binder_uintptr_t __user *)ptr))
2315 ptr += sizeof(binder_uintptr_t);
2316 binder_stat_br(proc, thread, cmd);
2317 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
2318 "%d:%d %s %016llx\n",
2319 proc->pid, thread->pid,
2320 cmd == BR_DEAD_BINDER ?
2322 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
2323 (u64)death->cookie);
2325 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
2326 list_del(&w->entry);
2328 binder_stats_deleted(BINDER_STAT_DEATH);
2330 list_move(&w->entry, &proc->delivered_death);
2331 if (cmd == BR_DEAD_BINDER)
2332 goto done; /* DEAD_BINDER notifications can cause transactions */
2339 BUG_ON(t->buffer == NULL);
2340 if (t->buffer->target_node) {
2341 struct binder_node *target_node = t->buffer->target_node;
2342 tr.target.ptr = target_node->ptr;
2343 tr.cookie = target_node->cookie;
2344 t->saved_priority = task_nice(current);
2345 if (t->priority < target_node->min_priority &&
2346 !(t->flags & TF_ONE_WAY))
2347 binder_set_nice(t->priority);
2348 else if (!(t->flags & TF_ONE_WAY) ||
2349 t->saved_priority > target_node->min_priority)
2350 binder_set_nice(target_node->min_priority);
2351 cmd = BR_TRANSACTION;
2358 tr.flags = t->flags;
2359 tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
2362 struct task_struct *sender = t->from->proc->tsk;
2363 tr.sender_pid = task_tgid_nr_ns(sender,
2364 task_active_pid_ns(current));
2369 tr.data_size = t->buffer->data_size;
2370 tr.offsets_size = t->buffer->offsets_size;
2371 tr.data.ptr.buffer = (binder_uintptr_t)(
2372 (uintptr_t)t->buffer->data +
2373 proc->user_buffer_offset);
2374 tr.data.ptr.offsets = tr.data.ptr.buffer +
2375 ALIGN(t->buffer->data_size,
2378 if (put_user(cmd, (uint32_t __user *)ptr))
2380 ptr += sizeof(uint32_t);
2381 if (copy_to_user(ptr, &tr, sizeof(tr)))
2385 trace_binder_transaction_received(t);
2386 binder_stat_br(proc, thread, cmd);
2387 binder_debug(BINDER_DEBUG_TRANSACTION,
2388 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
2389 proc->pid, thread->pid,
2390 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
2392 t->debug_id, t->from ? t->from->proc->pid : 0,
2393 t->from ? t->from->pid : 0, cmd,
2394 t->buffer->data_size, t->buffer->offsets_size,
2395 (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
2397 list_del(&t->work.entry);
2398 t->buffer->allow_user_free = 1;
2399 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
2400 t->to_parent = thread->transaction_stack;
2401 t->to_thread = thread;
2402 thread->transaction_stack = t;
2404 t->buffer->transaction = NULL;
2406 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2413 *consumed = ptr - buffer;
2414 if (proc->requested_threads + proc->ready_threads == 0 &&
2415 proc->requested_threads_started < proc->max_threads &&
2416 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2417 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
2418 /*spawn a new thread if we leave this out */) {
2419 proc->requested_threads++;
2420 binder_debug(BINDER_DEBUG_THREADS,
2421 "%d:%d BR_SPAWN_LOOPER\n",
2422 proc->pid, thread->pid);
2423 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
2425 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
2430 static void binder_release_work(struct list_head *list)
2432 struct binder_work *w;
2433 while (!list_empty(list)) {
2434 w = list_first_entry(list, struct binder_work, entry);
2435 list_del_init(&w->entry);
2437 case BINDER_WORK_TRANSACTION: {
2438 struct binder_transaction *t;
2440 t = container_of(w, struct binder_transaction, work);
2441 if (t->buffer->target_node &&
2442 !(t->flags & TF_ONE_WAY)) {
2443 binder_send_failed_reply(t, BR_DEAD_REPLY);
2445 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2446 "undelivered transaction %d\n",
2448 t->buffer->transaction = NULL;
2450 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2453 case BINDER_WORK_TRANSACTION_COMPLETE: {
2454 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2455 "undelivered TRANSACTION_COMPLETE\n");
2457 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2459 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2460 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2461 struct binder_ref_death *death;
2463 death = container_of(w, struct binder_ref_death, work);
2464 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2465 "undelivered death notification, %016llx\n",
2466 (u64)death->cookie);
2468 binder_stats_deleted(BINDER_STAT_DEATH);
2471 pr_err("unexpected work type, %d, not freed\n",
2479 static struct binder_thread *binder_get_thread(struct binder_proc *proc)
2481 struct binder_thread *thread = NULL;
2482 struct rb_node *parent = NULL;
2483 struct rb_node **p = &proc->threads.rb_node;
2487 thread = rb_entry(parent, struct binder_thread, rb_node);
2489 if (current->pid < thread->pid)
2491 else if (current->pid > thread->pid)
2492 p = &(*p)->rb_right;
2497 thread = kzalloc(sizeof(*thread), GFP_KERNEL);
2500 binder_stats_created(BINDER_STAT_THREAD);
2501 thread->proc = proc;
2502 thread->pid = current->pid;
2503 init_waitqueue_head(&thread->wait);
2504 INIT_LIST_HEAD(&thread->todo);
2505 rb_link_node(&thread->rb_node, parent, p);
2506 rb_insert_color(&thread->rb_node, &proc->threads);
2507 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
2508 thread->return_error = BR_OK;
2509 thread->return_error2 = BR_OK;
2514 static int binder_free_thread(struct binder_proc *proc,
2515 struct binder_thread *thread)
2517 struct binder_transaction *t;
2518 struct binder_transaction *send_reply = NULL;
2519 int active_transactions = 0;
2521 rb_erase(&thread->rb_node, &proc->threads);
2522 t = thread->transaction_stack;
2523 if (t && t->to_thread == thread)
2526 active_transactions++;
2527 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2528 "release %d:%d transaction %d %s, still active\n",
2529 proc->pid, thread->pid,
2531 (t->to_thread == thread) ? "in" : "out");
2533 if (t->to_thread == thread) {
2535 t->to_thread = NULL;
2537 t->buffer->transaction = NULL;
2541 } else if (t->from == thread) {
2548 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
2549 binder_release_work(&thread->todo);
2551 binder_stats_deleted(BINDER_STAT_THREAD);
2552 return active_transactions;
2555 static unsigned int binder_poll(struct file *filp,
2556 struct poll_table_struct *wait)
2558 struct binder_proc *proc = filp->private_data;
2559 struct binder_thread *thread = NULL;
2560 int wait_for_proc_work;
2562 binder_lock(__func__);
2564 thread = binder_get_thread(proc);
2566 wait_for_proc_work = thread->transaction_stack == NULL &&
2567 list_empty(&thread->todo) && thread->return_error == BR_OK;
2569 binder_unlock(__func__);
2571 if (wait_for_proc_work) {
2572 if (binder_has_proc_work(proc, thread))
2574 poll_wait(filp, &proc->wait, wait);
2575 if (binder_has_proc_work(proc, thread))
2578 if (binder_has_thread_work(thread))
2580 poll_wait(filp, &thread->wait, wait);
2581 if (binder_has_thread_work(thread))
2587 static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2590 struct binder_proc *proc = filp->private_data;
2591 struct binder_thread *thread;
2592 unsigned int size = _IOC_SIZE(cmd);
2593 void __user *ubuf = (void __user *)arg;
2595 /*pr_info("binder_ioctl: %d:%d %x %lx\n", proc->pid, current->pid, cmd, arg);*/
2597 trace_binder_ioctl(cmd, arg);
2599 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2603 binder_lock(__func__);
2604 thread = binder_get_thread(proc);
2605 if (thread == NULL) {
2611 case BINDER_WRITE_READ: {
2612 struct binder_write_read bwr;
2613 if (size != sizeof(struct binder_write_read)) {
2617 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
2621 binder_debug(BINDER_DEBUG_READ_WRITE,
2622 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
2623 proc->pid, thread->pid,
2624 (u64)bwr.write_size, (u64)bwr.write_buffer,
2625 (u64)bwr.read_size, (u64)bwr.read_buffer);
2627 if (bwr.write_size > 0) {
2628 ret = binder_thread_write(proc, thread, bwr.write_buffer, bwr.write_size, &bwr.write_consumed);
2629 trace_binder_write_done(ret);
2631 bwr.read_consumed = 0;
2632 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2637 if (bwr.read_size > 0) {
2638 ret = binder_thread_read(proc, thread, bwr.read_buffer, bwr.read_size, &bwr.read_consumed, filp->f_flags & O_NONBLOCK);
2639 trace_binder_read_done(ret);
2640 if (!list_empty(&proc->todo))
2641 wake_up_interruptible(&proc->wait);
2643 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2648 binder_debug(BINDER_DEBUG_READ_WRITE,
2649 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
2650 proc->pid, thread->pid,
2651 (u64)bwr.write_consumed, (u64)bwr.write_size,
2652 (u64)bwr.read_consumed, (u64)bwr.read_size);
2653 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
2659 case BINDER_SET_MAX_THREADS:
2660 if (copy_from_user(&proc->max_threads, ubuf, sizeof(proc->max_threads))) {
2665 case BINDER_SET_CONTEXT_MGR:
2666 if (binder_context_mgr_node != NULL) {
2667 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
2671 ret = security_binder_set_context_mgr(proc->tsk);
2674 if (uid_valid(binder_context_mgr_uid)) {
2675 if (!uid_eq(binder_context_mgr_uid, current->cred->euid)) {
2676 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
2677 from_kuid(&init_user_ns, current->cred->euid),
2678 from_kuid(&init_user_ns, binder_context_mgr_uid));
2683 binder_context_mgr_uid = current->cred->euid;
2684 binder_context_mgr_node = binder_new_node(proc, 0, 0);
2685 if (binder_context_mgr_node == NULL) {
2689 binder_context_mgr_node->local_weak_refs++;
2690 binder_context_mgr_node->local_strong_refs++;
2691 binder_context_mgr_node->has_strong_ref = 1;
2692 binder_context_mgr_node->has_weak_ref = 1;
2694 case BINDER_THREAD_EXIT:
2695 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
2696 proc->pid, thread->pid);
2697 binder_free_thread(proc, thread);
2700 case BINDER_VERSION:
2701 if (size != sizeof(struct binder_version)) {
2705 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION, &((struct binder_version *)ubuf)->protocol_version)) {
2717 thread->looper &= ~BINDER_LOOPER_STATE_NEED_RETURN;
2718 binder_unlock(__func__);
2719 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2720 if (ret && ret != -ERESTARTSYS)
2721 pr_info("%d:%d ioctl %x %lx returned %d\n", proc->pid, current->pid, cmd, arg, ret);
2723 trace_binder_ioctl_done(ret);
2727 static void binder_vma_open(struct vm_area_struct *vma)
2729 struct binder_proc *proc = vma->vm_private_data;
2730 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
2731 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
2732 proc->pid, vma->vm_start, vma->vm_end,
2733 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2734 (unsigned long)pgprot_val(vma->vm_page_prot));
2737 static void binder_vma_close(struct vm_area_struct *vma)
2739 struct binder_proc *proc = vma->vm_private_data;
2740 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
2741 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
2742 proc->pid, vma->vm_start, vma->vm_end,
2743 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2744 (unsigned long)pgprot_val(vma->vm_page_prot));
2746 proc->vma_vm_mm = NULL;
2747 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
2750 static struct vm_operations_struct binder_vm_ops = {
2751 .open = binder_vma_open,
2752 .close = binder_vma_close,
2755 static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
2758 struct vm_struct *area;
2759 struct binder_proc *proc = filp->private_data;
2760 const char *failure_string;
2761 struct binder_buffer *buffer;
2763 if (proc->tsk != current)
2766 if ((vma->vm_end - vma->vm_start) > SZ_4M)
2767 vma->vm_end = vma->vm_start + SZ_4M;
2769 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
2770 "binder_mmap: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
2771 proc->pid, vma->vm_start, vma->vm_end,
2772 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2773 (unsigned long)pgprot_val(vma->vm_page_prot));
2775 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
2777 failure_string = "bad vm_flags";
2780 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
2782 mutex_lock(&binder_mmap_lock);
2785 failure_string = "already mapped";
2786 goto err_already_mapped;
2789 area = get_vm_area(vma->vm_end - vma->vm_start, VM_IOREMAP);
2792 failure_string = "get_vm_area";
2793 goto err_get_vm_area_failed;
2795 proc->buffer = area->addr;
2796 proc->user_buffer_offset = vma->vm_start - (uintptr_t)proc->buffer;
2797 mutex_unlock(&binder_mmap_lock);
2799 #ifdef CONFIG_CPU_CACHE_VIPT
2800 if (cache_is_vipt_aliasing()) {
2801 while (CACHE_COLOUR((vma->vm_start ^ (uint32_t)proc->buffer))) {
2802 pr_info("binder_mmap: %d %lx-%lx maps %p bad alignment\n", proc->pid, vma->vm_start, vma->vm_end, proc->buffer);
2803 vma->vm_start += PAGE_SIZE;
2807 proc->pages = kzalloc(sizeof(proc->pages[0]) * ((vma->vm_end - vma->vm_start) / PAGE_SIZE), GFP_KERNEL);
2808 if (proc->pages == NULL) {
2810 failure_string = "alloc page array";
2811 goto err_alloc_pages_failed;
2813 proc->buffer_size = vma->vm_end - vma->vm_start;
2815 vma->vm_ops = &binder_vm_ops;
2816 vma->vm_private_data = proc;
2818 if (binder_update_page_range(proc, 1, proc->buffer, proc->buffer + PAGE_SIZE, vma)) {
2820 failure_string = "alloc small buf";
2821 goto err_alloc_small_buf_failed;
2823 buffer = proc->buffer;
2824 INIT_LIST_HEAD(&proc->buffers);
2825 list_add(&buffer->entry, &proc->buffers);
2827 binder_insert_free_buffer(proc, buffer);
2828 proc->free_async_space = proc->buffer_size / 2;
2830 proc->files = get_files_struct(current);
2832 proc->vma_vm_mm = vma->vm_mm;
2834 /*pr_info("binder_mmap: %d %lx-%lx maps %p\n",
2835 proc->pid, vma->vm_start, vma->vm_end, proc->buffer);*/
2838 err_alloc_small_buf_failed:
2841 err_alloc_pages_failed:
2842 mutex_lock(&binder_mmap_lock);
2843 vfree(proc->buffer);
2844 proc->buffer = NULL;
2845 err_get_vm_area_failed:
2847 mutex_unlock(&binder_mmap_lock);
2849 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
2850 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
2854 static int binder_open(struct inode *nodp, struct file *filp)
2856 struct binder_proc *proc;
2858 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
2859 current->group_leader->pid, current->pid);
2861 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
2864 get_task_struct(current);
2865 proc->tsk = current;
2866 INIT_LIST_HEAD(&proc->todo);
2867 init_waitqueue_head(&proc->wait);
2868 proc->default_priority = task_nice(current);
2870 binder_lock(__func__);
2872 binder_stats_created(BINDER_STAT_PROC);
2873 hlist_add_head(&proc->proc_node, &binder_procs);
2874 proc->pid = current->group_leader->pid;
2875 INIT_LIST_HEAD(&proc->delivered_death);
2876 filp->private_data = proc;
2878 binder_unlock(__func__);
2880 if (binder_debugfs_dir_entry_proc) {
2882 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
2883 proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
2884 binder_debugfs_dir_entry_proc, proc, &binder_proc_fops);
2890 static int binder_flush(struct file *filp, fl_owner_t id)
2892 struct binder_proc *proc = filp->private_data;
2894 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
2899 static void binder_deferred_flush(struct binder_proc *proc)
2903 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
2904 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
2905 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
2906 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
2907 wake_up_interruptible(&thread->wait);
2911 wake_up_interruptible_all(&proc->wait);
2913 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
2914 "binder_flush: %d woke %d threads\n", proc->pid,
2918 static int binder_release(struct inode *nodp, struct file *filp)
2920 struct binder_proc *proc = filp->private_data;
2921 debugfs_remove(proc->debugfs_entry);
2922 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
2927 static int binder_node_release(struct binder_node *node, int refs)
2929 struct binder_ref *ref;
2932 list_del_init(&node->work.entry);
2933 binder_release_work(&node->async_todo);
2935 if (hlist_empty(&node->refs)) {
2937 binder_stats_deleted(BINDER_STAT_NODE);
2943 node->local_strong_refs = 0;
2944 node->local_weak_refs = 0;
2945 hlist_add_head(&node->dead_node, &binder_dead_nodes);
2947 hlist_for_each_entry(ref, &node->refs, node_entry) {
2955 if (list_empty(&ref->death->work.entry)) {
2956 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
2957 list_add_tail(&ref->death->work.entry,
2959 wake_up_interruptible(&ref->proc->wait);
2964 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2965 "node %d now dead, refs %d, death %d\n",
2966 node->debug_id, refs, death);
2971 static void binder_deferred_release(struct binder_proc *proc)
2973 struct binder_transaction *t;
2975 int threads, nodes, incoming_refs, outgoing_refs, buffers,
2976 active_transactions, page_count;
2979 BUG_ON(proc->files);
2981 hlist_del(&proc->proc_node);
2983 if (binder_context_mgr_node && binder_context_mgr_node->proc == proc) {
2984 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2985 "%s: %d context_mgr_node gone\n",
2986 __func__, proc->pid);
2987 binder_context_mgr_node = NULL;
2991 active_transactions = 0;
2992 while ((n = rb_first(&proc->threads))) {
2993 struct binder_thread *thread;
2995 thread = rb_entry(n, struct binder_thread, rb_node);
2997 active_transactions += binder_free_thread(proc, thread);
3002 while ((n = rb_first(&proc->nodes))) {
3003 struct binder_node *node;
3005 node = rb_entry(n, struct binder_node, rb_node);
3007 rb_erase(&node->rb_node, &proc->nodes);
3008 incoming_refs = binder_node_release(node, incoming_refs);
3012 while ((n = rb_first(&proc->refs_by_desc))) {
3013 struct binder_ref *ref;
3015 ref = rb_entry(n, struct binder_ref, rb_node_desc);
3017 binder_delete_ref(ref);
3020 binder_release_work(&proc->todo);
3021 binder_release_work(&proc->delivered_death);
3024 while ((n = rb_first(&proc->allocated_buffers))) {
3025 struct binder_buffer *buffer;
3027 buffer = rb_entry(n, struct binder_buffer, rb_node);
3029 t = buffer->transaction;
3032 buffer->transaction = NULL;
3033 pr_err("release proc %d, transaction %d, not freed\n",
3034 proc->pid, t->debug_id);
3038 binder_free_buf(proc, buffer);
3042 binder_stats_deleted(BINDER_STAT_PROC);
3048 for (i = 0; i < proc->buffer_size / PAGE_SIZE; i++) {
3051 if (!proc->pages[i])
3054 page_addr = proc->buffer + i * PAGE_SIZE;
3055 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
3056 "%s: %d: page %d at %p not freed\n",
3057 __func__, proc->pid, i, page_addr);
3058 unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
3059 __free_page(proc->pages[i]);
3063 vfree(proc->buffer);
3066 put_task_struct(proc->tsk);
3068 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
3069 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d, buffers %d, pages %d\n",
3070 __func__, proc->pid, threads, nodes, incoming_refs,
3071 outgoing_refs, active_transactions, buffers, page_count);
3076 static void binder_deferred_func(struct work_struct *work)
3078 struct binder_proc *proc;
3079 struct files_struct *files;
3083 binder_lock(__func__);
3084 mutex_lock(&binder_deferred_lock);
3085 if (!hlist_empty(&binder_deferred_list)) {
3086 proc = hlist_entry(binder_deferred_list.first,
3087 struct binder_proc, deferred_work_node);
3088 hlist_del_init(&proc->deferred_work_node);
3089 defer = proc->deferred_work;
3090 proc->deferred_work = 0;
3095 mutex_unlock(&binder_deferred_lock);
3098 if (defer & BINDER_DEFERRED_PUT_FILES) {
3099 files = proc->files;
3104 if (defer & BINDER_DEFERRED_FLUSH)
3105 binder_deferred_flush(proc);
3107 if (defer & BINDER_DEFERRED_RELEASE)
3108 binder_deferred_release(proc); /* frees proc */
3110 binder_unlock(__func__);
3112 put_files_struct(files);
3115 static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
3118 binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
3120 mutex_lock(&binder_deferred_lock);
3121 proc->deferred_work |= defer;
3122 if (hlist_unhashed(&proc->deferred_work_node)) {
3123 hlist_add_head(&proc->deferred_work_node,
3124 &binder_deferred_list);
3125 queue_work(binder_deferred_workqueue, &binder_deferred_work);
3127 mutex_unlock(&binder_deferred_lock);
3130 static void print_binder_transaction(struct seq_file *m, const char *prefix,
3131 struct binder_transaction *t)
3134 "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %ld r%d",
3135 prefix, t->debug_id, t,
3136 t->from ? t->from->proc->pid : 0,
3137 t->from ? t->from->pid : 0,
3138 t->to_proc ? t->to_proc->pid : 0,
3139 t->to_thread ? t->to_thread->pid : 0,
3140 t->code, t->flags, t->priority, t->need_reply);
3141 if (t->buffer == NULL) {
3142 seq_puts(m, " buffer free\n");
3145 if (t->buffer->target_node)
3146 seq_printf(m, " node %d",
3147 t->buffer->target_node->debug_id);
3148 seq_printf(m, " size %zd:%zd data %p\n",
3149 t->buffer->data_size, t->buffer->offsets_size,
3153 static void print_binder_buffer(struct seq_file *m, const char *prefix,
3154 struct binder_buffer *buffer)
3156 seq_printf(m, "%s %d: %p size %zd:%zd %s\n",
3157 prefix, buffer->debug_id, buffer->data,
3158 buffer->data_size, buffer->offsets_size,
3159 buffer->transaction ? "active" : "delivered");
3162 static void print_binder_work(struct seq_file *m, const char *prefix,
3163 const char *transaction_prefix,
3164 struct binder_work *w)
3166 struct binder_node *node;
3167 struct binder_transaction *t;
3170 case BINDER_WORK_TRANSACTION:
3171 t = container_of(w, struct binder_transaction, work);
3172 print_binder_transaction(m, transaction_prefix, t);
3174 case BINDER_WORK_TRANSACTION_COMPLETE:
3175 seq_printf(m, "%stransaction complete\n", prefix);
3177 case BINDER_WORK_NODE:
3178 node = container_of(w, struct binder_node, work);
3179 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
3180 prefix, node->debug_id,
3181 (u64)node->ptr, (u64)node->cookie);
3183 case BINDER_WORK_DEAD_BINDER:
3184 seq_printf(m, "%shas dead binder\n", prefix);
3186 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
3187 seq_printf(m, "%shas cleared dead binder\n", prefix);
3189 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
3190 seq_printf(m, "%shas cleared death notification\n", prefix);
3193 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
3198 static void print_binder_thread(struct seq_file *m,
3199 struct binder_thread *thread,
3202 struct binder_transaction *t;
3203 struct binder_work *w;
3204 size_t start_pos = m->count;
3207 seq_printf(m, " thread %d: l %02x\n", thread->pid, thread->looper);
3208 header_pos = m->count;
3209 t = thread->transaction_stack;
3211 if (t->from == thread) {
3212 print_binder_transaction(m,
3213 " outgoing transaction", t);
3215 } else if (t->to_thread == thread) {
3216 print_binder_transaction(m,
3217 " incoming transaction", t);
3220 print_binder_transaction(m, " bad transaction", t);
3224 list_for_each_entry(w, &thread->todo, entry) {
3225 print_binder_work(m, " ", " pending transaction", w);
3227 if (!print_always && m->count == header_pos)
3228 m->count = start_pos;
3231 static void print_binder_node(struct seq_file *m, struct binder_node *node)
3233 struct binder_ref *ref;
3234 struct binder_work *w;
3238 hlist_for_each_entry(ref, &node->refs, node_entry)
3241 seq_printf(m, " node %d: u%016llx c%016llx hs %d hw %d ls %d lw %d is %d iw %d",
3242 node->debug_id, (u64)node->ptr, (u64)node->cookie,
3243 node->has_strong_ref, node->has_weak_ref,
3244 node->local_strong_refs, node->local_weak_refs,
3245 node->internal_strong_refs, count);
3247 seq_puts(m, " proc");
3248 hlist_for_each_entry(ref, &node->refs, node_entry)
3249 seq_printf(m, " %d", ref->proc->pid);
3252 list_for_each_entry(w, &node->async_todo, entry)
3253 print_binder_work(m, " ",
3254 " pending async transaction", w);
3257 static void print_binder_ref(struct seq_file *m, struct binder_ref *ref)
3259 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %p\n",
3260 ref->debug_id, ref->desc, ref->node->proc ? "" : "dead ",
3261 ref->node->debug_id, ref->strong, ref->weak, ref->death);
3264 static void print_binder_proc(struct seq_file *m,
3265 struct binder_proc *proc, int print_all)
3267 struct binder_work *w;
3269 size_t start_pos = m->count;
3272 seq_printf(m, "proc %d\n", proc->pid);
3273 header_pos = m->count;
3275 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3276 print_binder_thread(m, rb_entry(n, struct binder_thread,
3277 rb_node), print_all);
3278 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
3279 struct binder_node *node = rb_entry(n, struct binder_node,
3281 if (print_all || node->has_async_transaction)
3282 print_binder_node(m, node);
3285 for (n = rb_first(&proc->refs_by_desc);
3288 print_binder_ref(m, rb_entry(n, struct binder_ref,
3291 for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
3292 print_binder_buffer(m, " buffer",
3293 rb_entry(n, struct binder_buffer, rb_node));
3294 list_for_each_entry(w, &proc->todo, entry)
3295 print_binder_work(m, " ", " pending transaction", w);
3296 list_for_each_entry(w, &proc->delivered_death, entry) {
3297 seq_puts(m, " has delivered dead binder\n");
3300 if (!print_all && m->count == header_pos)
3301 m->count = start_pos;
3304 static const char * const binder_return_strings[] = {
3309 "BR_ACQUIRE_RESULT",
3311 "BR_TRANSACTION_COMPLETE",
3316 "BR_ATTEMPT_ACQUIRE",
3321 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
3325 static const char * const binder_command_strings[] = {
3328 "BC_ACQUIRE_RESULT",
3336 "BC_ATTEMPT_ACQUIRE",
3337 "BC_REGISTER_LOOPER",
3340 "BC_REQUEST_DEATH_NOTIFICATION",
3341 "BC_CLEAR_DEATH_NOTIFICATION",
3342 "BC_DEAD_BINDER_DONE"
3345 static const char * const binder_objstat_strings[] = {
3352 "transaction_complete"
3355 static void print_binder_stats(struct seq_file *m, const char *prefix,
3356 struct binder_stats *stats)
3360 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
3361 ARRAY_SIZE(binder_command_strings));
3362 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
3364 seq_printf(m, "%s%s: %d\n", prefix,
3365 binder_command_strings[i], stats->bc[i]);
3368 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
3369 ARRAY_SIZE(binder_return_strings));
3370 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
3372 seq_printf(m, "%s%s: %d\n", prefix,
3373 binder_return_strings[i], stats->br[i]);
3376 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
3377 ARRAY_SIZE(binder_objstat_strings));
3378 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
3379 ARRAY_SIZE(stats->obj_deleted));
3380 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
3381 if (stats->obj_created[i] || stats->obj_deleted[i])
3382 seq_printf(m, "%s%s: active %d total %d\n", prefix,
3383 binder_objstat_strings[i],
3384 stats->obj_created[i] - stats->obj_deleted[i],
3385 stats->obj_created[i]);
3389 static void print_binder_proc_stats(struct seq_file *m,
3390 struct binder_proc *proc)
3392 struct binder_work *w;
3394 int count, strong, weak;
3396 seq_printf(m, "proc %d\n", proc->pid);
3398 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3400 seq_printf(m, " threads: %d\n", count);
3401 seq_printf(m, " requested threads: %d+%d/%d\n"
3402 " ready threads %d\n"
3403 " free async space %zd\n", proc->requested_threads,
3404 proc->requested_threads_started, proc->max_threads,
3405 proc->ready_threads, proc->free_async_space);
3407 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
3409 seq_printf(m, " nodes: %d\n", count);
3413 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
3414 struct binder_ref *ref = rb_entry(n, struct binder_ref,
3417 strong += ref->strong;
3420 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
3423 for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
3425 seq_printf(m, " buffers: %d\n", count);
3428 list_for_each_entry(w, &proc->todo, entry) {
3430 case BINDER_WORK_TRANSACTION:
3437 seq_printf(m, " pending transactions: %d\n", count);
3439 print_binder_stats(m, " ", &proc->stats);
3443 static int binder_state_show(struct seq_file *m, void *unused)
3445 struct binder_proc *proc;
3446 struct binder_node *node;
3447 int do_lock = !binder_debug_no_lock;
3450 binder_lock(__func__);
3452 seq_puts(m, "binder state:\n");
3454 if (!hlist_empty(&binder_dead_nodes))
3455 seq_puts(m, "dead nodes:\n");
3456 hlist_for_each_entry(node, &binder_dead_nodes, dead_node)
3457 print_binder_node(m, node);
3459 hlist_for_each_entry(proc, &binder_procs, proc_node)
3460 print_binder_proc(m, proc, 1);
3462 binder_unlock(__func__);
3466 static int binder_stats_show(struct seq_file *m, void *unused)
3468 struct binder_proc *proc;
3469 int do_lock = !binder_debug_no_lock;
3472 binder_lock(__func__);
3474 seq_puts(m, "binder stats:\n");
3476 print_binder_stats(m, "", &binder_stats);
3478 hlist_for_each_entry(proc, &binder_procs, proc_node)
3479 print_binder_proc_stats(m, proc);
3481 binder_unlock(__func__);
3485 static int binder_transactions_show(struct seq_file *m, void *unused)
3487 struct binder_proc *proc;
3488 int do_lock = !binder_debug_no_lock;
3491 binder_lock(__func__);
3493 seq_puts(m, "binder transactions:\n");
3494 hlist_for_each_entry(proc, &binder_procs, proc_node)
3495 print_binder_proc(m, proc, 0);
3497 binder_unlock(__func__);
3501 static int binder_proc_show(struct seq_file *m, void *unused)
3503 struct binder_proc *proc = m->private;
3504 int do_lock = !binder_debug_no_lock;
3507 binder_lock(__func__);
3508 seq_puts(m, "binder proc state:\n");
3509 print_binder_proc(m, proc, 1);
3511 binder_unlock(__func__);
3515 static void print_binder_transaction_log_entry(struct seq_file *m,
3516 struct binder_transaction_log_entry *e)
3519 "%d: %s from %d:%d to %d:%d node %d handle %d size %d:%d\n",
3520 e->debug_id, (e->call_type == 2) ? "reply" :
3521 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
3522 e->from_thread, e->to_proc, e->to_thread, e->to_node,
3523 e->target_handle, e->data_size, e->offsets_size);
3526 static int binder_transaction_log_show(struct seq_file *m, void *unused)
3528 struct binder_transaction_log *log = m->private;
3532 for (i = log->next; i < ARRAY_SIZE(log->entry); i++)
3533 print_binder_transaction_log_entry(m, &log->entry[i]);
3535 for (i = 0; i < log->next; i++)
3536 print_binder_transaction_log_entry(m, &log->entry[i]);
3540 static const struct file_operations binder_fops = {
3541 .owner = THIS_MODULE,
3542 .poll = binder_poll,
3543 .unlocked_ioctl = binder_ioctl,
3544 .compat_ioctl = binder_ioctl,
3545 .mmap = binder_mmap,
3546 .open = binder_open,
3547 .flush = binder_flush,
3548 .release = binder_release,
3551 static struct miscdevice binder_miscdev = {
3552 .minor = MISC_DYNAMIC_MINOR,
3554 .fops = &binder_fops
3557 BINDER_DEBUG_ENTRY(state);
3558 BINDER_DEBUG_ENTRY(stats);
3559 BINDER_DEBUG_ENTRY(transactions);
3560 BINDER_DEBUG_ENTRY(transaction_log);
3562 static int __init binder_init(void)
3566 binder_deferred_workqueue = create_singlethread_workqueue("binder");
3567 if (!binder_deferred_workqueue)
3570 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
3571 if (binder_debugfs_dir_entry_root)
3572 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
3573 binder_debugfs_dir_entry_root);
3574 ret = misc_register(&binder_miscdev);
3575 if (binder_debugfs_dir_entry_root) {
3576 debugfs_create_file("state",
3578 binder_debugfs_dir_entry_root,
3580 &binder_state_fops);
3581 debugfs_create_file("stats",
3583 binder_debugfs_dir_entry_root,
3585 &binder_stats_fops);
3586 debugfs_create_file("transactions",
3588 binder_debugfs_dir_entry_root,
3590 &binder_transactions_fops);
3591 debugfs_create_file("transaction_log",
3593 binder_debugfs_dir_entry_root,
3594 &binder_transaction_log,
3595 &binder_transaction_log_fops);
3596 debugfs_create_file("failed_transaction_log",
3598 binder_debugfs_dir_entry_root,
3599 &binder_transaction_log_failed,
3600 &binder_transaction_log_fops);
3605 device_initcall(binder_init);
3607 #define CREATE_TRACE_POINTS
3608 #include "binder_trace.h"
3610 MODULE_LICENSE("GPL v2");