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>
42 #ifdef CONFIG_ANDROID_BINDER_IPC_32BIT
43 #define BINDER_IPC_32BIT 1
46 #include <uapi/linux/android/binder.h>
47 #include "binder_trace.h"
49 static DEFINE_MUTEX(binder_main_lock);
50 static DEFINE_MUTEX(binder_deferred_lock);
51 static DEFINE_MUTEX(binder_mmap_lock);
53 static HLIST_HEAD(binder_procs);
54 static HLIST_HEAD(binder_deferred_list);
55 static HLIST_HEAD(binder_dead_nodes);
57 static struct dentry *binder_debugfs_dir_entry_root;
58 static struct dentry *binder_debugfs_dir_entry_proc;
59 static struct binder_node *binder_context_mgr_node;
60 static kuid_t binder_context_mgr_uid = INVALID_UID;
61 static int binder_last_id;
62 static struct workqueue_struct *binder_deferred_workqueue;
64 #define BINDER_DEBUG_ENTRY(name) \
65 static int binder_##name##_open(struct inode *inode, struct file *file) \
67 return single_open(file, binder_##name##_show, inode->i_private); \
70 static const struct file_operations binder_##name##_fops = { \
71 .owner = THIS_MODULE, \
72 .open = binder_##name##_open, \
74 .llseek = seq_lseek, \
75 .release = single_release, \
78 static int binder_proc_show(struct seq_file *m, void *unused);
79 BINDER_DEBUG_ENTRY(proc);
81 /* This is only defined in include/asm-arm/sizes.h */
87 #define SZ_4M 0x400000
90 #define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
92 #define BINDER_SMALL_BUF_SIZE (PAGE_SIZE * 64)
95 BINDER_DEBUG_USER_ERROR = 1U << 0,
96 BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1,
97 BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2,
98 BINDER_DEBUG_OPEN_CLOSE = 1U << 3,
99 BINDER_DEBUG_DEAD_BINDER = 1U << 4,
100 BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5,
101 BINDER_DEBUG_READ_WRITE = 1U << 6,
102 BINDER_DEBUG_USER_REFS = 1U << 7,
103 BINDER_DEBUG_THREADS = 1U << 8,
104 BINDER_DEBUG_TRANSACTION = 1U << 9,
105 BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10,
106 BINDER_DEBUG_FREE_BUFFER = 1U << 11,
107 BINDER_DEBUG_INTERNAL_REFS = 1U << 12,
108 BINDER_DEBUG_BUFFER_ALLOC = 1U << 13,
109 BINDER_DEBUG_PRIORITY_CAP = 1U << 14,
110 BINDER_DEBUG_BUFFER_ALLOC_ASYNC = 1U << 15,
112 static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR |
113 BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION;
114 module_param_named(debug_mask, binder_debug_mask, uint, S_IWUSR | S_IRUGO);
116 static bool binder_debug_no_lock;
117 module_param_named(proc_no_lock, binder_debug_no_lock, bool, S_IWUSR | S_IRUGO);
119 static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
120 static int binder_stop_on_user_error;
122 static int binder_set_stop_on_user_error(const char *val,
123 struct kernel_param *kp)
127 ret = param_set_int(val, kp);
128 if (binder_stop_on_user_error < 2)
129 wake_up(&binder_user_error_wait);
132 module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
133 param_get_int, &binder_stop_on_user_error, S_IWUSR | S_IRUGO);
135 #define binder_debug(mask, x...) \
137 if (binder_debug_mask & mask) \
141 #define binder_user_error(x...) \
143 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
145 if (binder_stop_on_user_error) \
146 binder_stop_on_user_error = 2; \
149 enum binder_stat_types {
155 BINDER_STAT_TRANSACTION,
156 BINDER_STAT_TRANSACTION_COMPLETE,
160 struct binder_stats {
161 int br[_IOC_NR(BR_FAILED_REPLY) + 1];
162 int bc[_IOC_NR(BC_DEAD_BINDER_DONE) + 1];
163 int obj_created[BINDER_STAT_COUNT];
164 int obj_deleted[BINDER_STAT_COUNT];
167 static struct binder_stats binder_stats;
169 static inline void binder_stats_deleted(enum binder_stat_types type)
171 binder_stats.obj_deleted[type]++;
174 static inline void binder_stats_created(enum binder_stat_types type)
176 binder_stats.obj_created[type]++;
179 struct binder_transaction_log_entry {
191 struct binder_transaction_log {
194 struct binder_transaction_log_entry entry[32];
196 static struct binder_transaction_log binder_transaction_log;
197 static struct binder_transaction_log binder_transaction_log_failed;
199 static struct binder_transaction_log_entry *binder_transaction_log_add(
200 struct binder_transaction_log *log)
202 struct binder_transaction_log_entry *e;
204 e = &log->entry[log->next];
205 memset(e, 0, sizeof(*e));
207 if (log->next == ARRAY_SIZE(log->entry)) {
215 struct list_head entry;
217 BINDER_WORK_TRANSACTION = 1,
218 BINDER_WORK_TRANSACTION_COMPLETE,
220 BINDER_WORK_DEAD_BINDER,
221 BINDER_WORK_DEAD_BINDER_AND_CLEAR,
222 BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
228 struct binder_work work;
230 struct rb_node rb_node;
231 struct hlist_node dead_node;
233 struct binder_proc *proc;
234 struct hlist_head refs;
235 int internal_strong_refs;
237 int local_strong_refs;
238 binder_uintptr_t ptr;
239 binder_uintptr_t cookie;
240 unsigned has_strong_ref:1;
241 unsigned pending_strong_ref:1;
242 unsigned has_weak_ref:1;
243 unsigned pending_weak_ref:1;
244 unsigned has_async_transaction:1;
245 unsigned accept_fds:1;
246 unsigned min_priority:8;
247 struct list_head async_todo;
250 struct binder_ref_death {
251 struct binder_work work;
252 binder_uintptr_t cookie;
256 /* Lookups needed: */
257 /* node + proc => ref (transaction) */
258 /* desc + proc => ref (transaction, inc/dec ref) */
259 /* node => refs + procs (proc exit) */
261 struct rb_node rb_node_desc;
262 struct rb_node rb_node_node;
263 struct hlist_node node_entry;
264 struct binder_proc *proc;
265 struct binder_node *node;
269 struct binder_ref_death *death;
272 struct binder_buffer {
273 struct list_head entry; /* free and allocated entries by address */
274 struct rb_node rb_node; /* free entry by size or allocated entry */
277 unsigned allow_user_free:1;
278 unsigned async_transaction:1;
279 unsigned debug_id:29;
281 struct binder_transaction *transaction;
283 struct binder_node *target_node;
289 enum binder_deferred_state {
290 BINDER_DEFERRED_PUT_FILES = 0x01,
291 BINDER_DEFERRED_FLUSH = 0x02,
292 BINDER_DEFERRED_RELEASE = 0x04,
296 struct hlist_node proc_node;
297 struct rb_root threads;
298 struct rb_root nodes;
299 struct rb_root refs_by_desc;
300 struct rb_root refs_by_node;
302 struct vm_area_struct *vma;
303 struct mm_struct *vma_vm_mm;
304 struct task_struct *tsk;
305 struct files_struct *files;
306 struct hlist_node deferred_work_node;
309 ptrdiff_t user_buffer_offset;
311 struct list_head buffers;
312 struct rb_root free_buffers;
313 struct rb_root allocated_buffers;
314 size_t free_async_space;
318 uint32_t buffer_free;
319 struct list_head todo;
320 wait_queue_head_t wait;
321 struct binder_stats stats;
322 struct list_head delivered_death;
324 int requested_threads;
325 int requested_threads_started;
327 long default_priority;
328 struct dentry *debugfs_entry;
332 BINDER_LOOPER_STATE_REGISTERED = 0x01,
333 BINDER_LOOPER_STATE_ENTERED = 0x02,
334 BINDER_LOOPER_STATE_EXITED = 0x04,
335 BINDER_LOOPER_STATE_INVALID = 0x08,
336 BINDER_LOOPER_STATE_WAITING = 0x10,
337 BINDER_LOOPER_STATE_NEED_RETURN = 0x20,
338 BINDER_LOOPER_STATE_POLL = 0x40,
341 struct binder_thread {
342 struct binder_proc *proc;
343 struct rb_node rb_node;
346 struct binder_transaction *transaction_stack;
347 struct list_head todo;
348 uint32_t return_error; /* Write failed, return error code in read buf */
349 uint32_t return_error2; /* Write failed, return error code in read */
350 /* buffer. Used when sending a reply to a dead process that */
351 /* we are also waiting on */
352 wait_queue_head_t wait;
353 struct binder_stats stats;
356 struct binder_transaction {
358 struct binder_work work;
359 struct binder_thread *from;
360 struct binder_transaction *from_parent;
361 struct binder_proc *to_proc;
362 struct binder_thread *to_thread;
363 struct binder_transaction *to_parent;
364 unsigned need_reply:1;
365 /* unsigned is_dead:1; */ /* not used at the moment */
367 struct binder_buffer *buffer;
376 binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
378 static int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
380 struct files_struct *files = proc->files;
381 unsigned long rlim_cur;
387 if (!lock_task_sighand(proc->tsk, &irqs))
390 rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
391 unlock_task_sighand(proc->tsk, &irqs);
393 return __alloc_fd(files, 0, rlim_cur, flags);
397 * copied from fd_install
399 static void task_fd_install(
400 struct binder_proc *proc, unsigned int fd, struct file *file)
403 __fd_install(proc->files, fd, file);
407 * copied from sys_close
409 static long task_close_fd(struct binder_proc *proc, unsigned int fd)
413 if (proc->files == NULL)
416 retval = __close_fd(proc->files, fd);
417 /* can't restart close syscall because file table entry was cleared */
418 if (unlikely(retval == -ERESTARTSYS ||
419 retval == -ERESTARTNOINTR ||
420 retval == -ERESTARTNOHAND ||
421 retval == -ERESTART_RESTARTBLOCK))
427 static inline void binder_lock(const char *tag)
429 trace_binder_lock(tag);
430 mutex_lock(&binder_main_lock);
431 trace_binder_locked(tag);
434 static inline void binder_unlock(const char *tag)
436 trace_binder_unlock(tag);
437 mutex_unlock(&binder_main_lock);
440 static void binder_set_nice(long nice)
444 if (can_nice(current, nice)) {
445 set_user_nice(current, nice);
448 min_nice = rlimit_to_nice(current->signal->rlim[RLIMIT_NICE].rlim_cur);
449 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
450 "%d: nice value %ld not allowed use %ld instead\n",
451 current->pid, nice, min_nice);
452 set_user_nice(current, min_nice);
453 if (min_nice <= MAX_NICE)
455 binder_user_error("%d RLIMIT_NICE not set\n", current->pid);
458 static size_t binder_buffer_size(struct binder_proc *proc,
459 struct binder_buffer *buffer)
461 if (list_is_last(&buffer->entry, &proc->buffers))
462 return proc->buffer + proc->buffer_size - (void *)buffer->data;
463 return (size_t)list_entry(buffer->entry.next,
464 struct binder_buffer, entry) - (size_t)buffer->data;
467 static void binder_insert_free_buffer(struct binder_proc *proc,
468 struct binder_buffer *new_buffer)
470 struct rb_node **p = &proc->free_buffers.rb_node;
471 struct rb_node *parent = NULL;
472 struct binder_buffer *buffer;
474 size_t new_buffer_size;
476 BUG_ON(!new_buffer->free);
478 new_buffer_size = binder_buffer_size(proc, new_buffer);
480 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
481 "%d: add free buffer, size %zd, at %pK\n",
482 proc->pid, new_buffer_size, new_buffer);
486 buffer = rb_entry(parent, struct binder_buffer, rb_node);
487 BUG_ON(!buffer->free);
489 buffer_size = binder_buffer_size(proc, buffer);
491 if (new_buffer_size < buffer_size)
492 p = &parent->rb_left;
494 p = &parent->rb_right;
496 rb_link_node(&new_buffer->rb_node, parent, p);
497 rb_insert_color(&new_buffer->rb_node, &proc->free_buffers);
500 static void binder_insert_allocated_buffer(struct binder_proc *proc,
501 struct binder_buffer *new_buffer)
503 struct rb_node **p = &proc->allocated_buffers.rb_node;
504 struct rb_node *parent = NULL;
505 struct binder_buffer *buffer;
507 BUG_ON(new_buffer->free);
511 buffer = rb_entry(parent, struct binder_buffer, rb_node);
512 BUG_ON(buffer->free);
514 if (new_buffer < buffer)
515 p = &parent->rb_left;
516 else if (new_buffer > buffer)
517 p = &parent->rb_right;
521 rb_link_node(&new_buffer->rb_node, parent, p);
522 rb_insert_color(&new_buffer->rb_node, &proc->allocated_buffers);
525 static struct binder_buffer *binder_buffer_lookup(struct binder_proc *proc,
528 struct rb_node *n = proc->allocated_buffers.rb_node;
529 struct binder_buffer *buffer;
530 struct binder_buffer *kern_ptr;
532 kern_ptr = (struct binder_buffer *)(user_ptr - proc->user_buffer_offset
533 - offsetof(struct binder_buffer, data));
536 buffer = rb_entry(n, struct binder_buffer, rb_node);
537 BUG_ON(buffer->free);
539 if (kern_ptr < buffer)
541 else if (kern_ptr > buffer)
549 static int binder_update_page_range(struct binder_proc *proc, int allocate,
550 void *start, void *end,
551 struct vm_area_struct *vma)
554 unsigned long user_page_addr;
556 struct mm_struct *mm;
558 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
559 "%d: %s pages %pK-%pK\n", proc->pid,
560 allocate ? "allocate" : "free", start, end);
565 trace_binder_update_page_range(proc, allocate, start, end);
570 mm = get_task_mm(proc->tsk);
573 down_write(&mm->mmap_sem);
574 if (!mmget_still_valid(mm)) {
581 if (vma && mm != proc->vma_vm_mm) {
582 pr_err("%d: vma mm and task mm mismatch\n",
592 pr_err("%d: binder_alloc_buf failed to map pages in userspace, no vma\n",
597 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
600 page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
603 *page = alloc_page(GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
605 pr_err("%d: binder_alloc_buf failed for page at %pK\n",
606 proc->pid, page_addr);
607 goto err_alloc_page_failed;
609 ret = map_kernel_range_noflush((unsigned long)page_addr,
610 PAGE_SIZE, PAGE_KERNEL, page);
611 flush_cache_vmap((unsigned long)page_addr,
612 (unsigned long)page_addr + PAGE_SIZE);
614 pr_err("%d: binder_alloc_buf failed to map page at %pK in kernel\n",
615 proc->pid, page_addr);
616 goto err_map_kernel_failed;
619 (uintptr_t)page_addr + proc->user_buffer_offset;
620 ret = vm_insert_page(vma, user_page_addr, page[0]);
622 pr_err("%d: binder_alloc_buf failed to map page at %lx in userspace\n",
623 proc->pid, user_page_addr);
624 goto err_vm_insert_page_failed;
626 /* vm_insert_page does not seem to increment the refcount */
629 up_write(&mm->mmap_sem);
635 for (page_addr = end - PAGE_SIZE; page_addr >= start;
636 page_addr -= PAGE_SIZE) {
637 page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
639 zap_page_range(vma, (uintptr_t)page_addr +
640 proc->user_buffer_offset, PAGE_SIZE, NULL);
641 err_vm_insert_page_failed:
642 unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
643 err_map_kernel_failed:
646 err_alloc_page_failed:
651 up_write(&mm->mmap_sem);
657 static struct binder_buffer *binder_alloc_buf(struct binder_proc *proc,
659 size_t offsets_size, int is_async)
661 struct rb_node *n = proc->free_buffers.rb_node;
662 struct binder_buffer *buffer;
664 struct rb_node *best_fit = NULL;
669 if (proc->vma == NULL) {
670 pr_err("%d: binder_alloc_buf, no vma\n",
675 size = ALIGN(data_size, sizeof(void *)) +
676 ALIGN(offsets_size, sizeof(void *));
678 if (size < data_size || size < offsets_size) {
679 binder_user_error("%d: got transaction with invalid size %zd-%zd\n",
680 proc->pid, data_size, offsets_size);
685 proc->free_async_space < size + sizeof(struct binder_buffer)) {
686 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
687 "%d: binder_alloc_buf size %zd failed, no async space left\n",
693 buffer = rb_entry(n, struct binder_buffer, rb_node);
694 BUG_ON(!buffer->free);
695 buffer_size = binder_buffer_size(proc, buffer);
697 if (size < buffer_size) {
700 } else if (size > buffer_size)
707 if (best_fit == NULL) {
708 pr_err("%d: binder_alloc_buf size %zd failed, no address space\n",
713 buffer = rb_entry(best_fit, struct binder_buffer, rb_node);
714 buffer_size = binder_buffer_size(proc, buffer);
717 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
718 "%d: binder_alloc_buf size %zd got buffer %pK size %zd\n",
719 proc->pid, size, buffer, buffer_size);
722 (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK);
724 if (size + sizeof(struct binder_buffer) + 4 >= buffer_size)
725 buffer_size = size; /* no room for other buffers */
727 buffer_size = size + sizeof(struct binder_buffer);
730 (void *)PAGE_ALIGN((uintptr_t)buffer->data + buffer_size);
731 if (end_page_addr > has_page_addr)
732 end_page_addr = has_page_addr;
733 if (binder_update_page_range(proc, 1,
734 (void *)PAGE_ALIGN((uintptr_t)buffer->data), end_page_addr, NULL))
737 rb_erase(best_fit, &proc->free_buffers);
739 binder_insert_allocated_buffer(proc, buffer);
740 if (buffer_size != size) {
741 struct binder_buffer *new_buffer = (void *)buffer->data + size;
743 list_add(&new_buffer->entry, &buffer->entry);
744 new_buffer->free = 1;
745 binder_insert_free_buffer(proc, new_buffer);
747 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
748 "%d: binder_alloc_buf size %zd got %pK\n",
749 proc->pid, size, buffer);
750 buffer->data_size = data_size;
751 buffer->offsets_size = offsets_size;
752 buffer->async_transaction = is_async;
754 proc->free_async_space -= size + sizeof(struct binder_buffer);
755 binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
756 "%d: binder_alloc_buf size %zd async free %zd\n",
757 proc->pid, size, proc->free_async_space);
763 static void *buffer_start_page(struct binder_buffer *buffer)
765 return (void *)((uintptr_t)buffer & PAGE_MASK);
768 static void *buffer_end_page(struct binder_buffer *buffer)
770 return (void *)(((uintptr_t)(buffer + 1) - 1) & PAGE_MASK);
773 static void binder_delete_free_buffer(struct binder_proc *proc,
774 struct binder_buffer *buffer)
776 struct binder_buffer *prev, *next = NULL;
777 int free_page_end = 1;
778 int free_page_start = 1;
780 BUG_ON(proc->buffers.next == &buffer->entry);
781 prev = list_entry(buffer->entry.prev, struct binder_buffer, entry);
783 if (buffer_end_page(prev) == buffer_start_page(buffer)) {
785 if (buffer_end_page(prev) == buffer_end_page(buffer))
787 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
788 "%d: merge free, buffer %pK share page with %pK\n",
789 proc->pid, buffer, prev);
792 if (!list_is_last(&buffer->entry, &proc->buffers)) {
793 next = list_entry(buffer->entry.next,
794 struct binder_buffer, entry);
795 if (buffer_start_page(next) == buffer_end_page(buffer)) {
797 if (buffer_start_page(next) ==
798 buffer_start_page(buffer))
800 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
801 "%d: merge free, buffer %pK share page with %pK\n",
802 proc->pid, buffer, prev);
805 list_del(&buffer->entry);
806 if (free_page_start || free_page_end) {
807 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
808 "%d: merge free, buffer %pK do not share page%s%s with %pK or %pK\n",
809 proc->pid, buffer, free_page_start ? "" : " end",
810 free_page_end ? "" : " start", prev, next);
811 binder_update_page_range(proc, 0, free_page_start ?
812 buffer_start_page(buffer) : buffer_end_page(buffer),
813 (free_page_end ? buffer_end_page(buffer) :
814 buffer_start_page(buffer)) + PAGE_SIZE, NULL);
818 static void binder_free_buf(struct binder_proc *proc,
819 struct binder_buffer *buffer)
821 size_t size, buffer_size;
823 buffer_size = binder_buffer_size(proc, buffer);
825 size = ALIGN(buffer->data_size, sizeof(void *)) +
826 ALIGN(buffer->offsets_size, sizeof(void *));
828 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
829 "%d: binder_free_buf %pK size %zd buffer_size %zd\n",
830 proc->pid, buffer, size, buffer_size);
832 BUG_ON(buffer->free);
833 BUG_ON(size > buffer_size);
834 BUG_ON(buffer->transaction != NULL);
835 BUG_ON((void *)buffer < proc->buffer);
836 BUG_ON((void *)buffer > proc->buffer + proc->buffer_size);
838 if (buffer->async_transaction) {
839 proc->free_async_space += size + sizeof(struct binder_buffer);
841 binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
842 "%d: binder_free_buf size %zd async free %zd\n",
843 proc->pid, size, proc->free_async_space);
846 binder_update_page_range(proc, 0,
847 (void *)PAGE_ALIGN((uintptr_t)buffer->data),
848 (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK),
850 rb_erase(&buffer->rb_node, &proc->allocated_buffers);
852 if (!list_is_last(&buffer->entry, &proc->buffers)) {
853 struct binder_buffer *next = list_entry(buffer->entry.next,
854 struct binder_buffer, entry);
857 rb_erase(&next->rb_node, &proc->free_buffers);
858 binder_delete_free_buffer(proc, next);
861 if (proc->buffers.next != &buffer->entry) {
862 struct binder_buffer *prev = list_entry(buffer->entry.prev,
863 struct binder_buffer, entry);
866 binder_delete_free_buffer(proc, buffer);
867 rb_erase(&prev->rb_node, &proc->free_buffers);
871 binder_insert_free_buffer(proc, buffer);
874 static struct binder_node *binder_get_node(struct binder_proc *proc,
875 binder_uintptr_t ptr)
877 struct rb_node *n = proc->nodes.rb_node;
878 struct binder_node *node;
881 node = rb_entry(n, struct binder_node, rb_node);
885 else if (ptr > node->ptr)
893 static struct binder_node *binder_new_node(struct binder_proc *proc,
894 binder_uintptr_t ptr,
895 binder_uintptr_t cookie)
897 struct rb_node **p = &proc->nodes.rb_node;
898 struct rb_node *parent = NULL;
899 struct binder_node *node;
903 node = rb_entry(parent, struct binder_node, rb_node);
907 else if (ptr > node->ptr)
913 node = kzalloc(sizeof(*node), GFP_KERNEL);
916 binder_stats_created(BINDER_STAT_NODE);
917 rb_link_node(&node->rb_node, parent, p);
918 rb_insert_color(&node->rb_node, &proc->nodes);
919 node->debug_id = ++binder_last_id;
922 node->cookie = cookie;
923 node->work.type = BINDER_WORK_NODE;
924 INIT_LIST_HEAD(&node->work.entry);
925 INIT_LIST_HEAD(&node->async_todo);
926 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
927 "%d:%d node %d u%016llx c%016llx created\n",
928 proc->pid, current->pid, node->debug_id,
929 (u64)node->ptr, (u64)node->cookie);
933 static int binder_inc_node(struct binder_node *node, int strong, int internal,
934 struct list_head *target_list)
938 if (target_list == NULL &&
939 node->internal_strong_refs == 0 &&
940 !(node == binder_context_mgr_node &&
941 node->has_strong_ref)) {
942 pr_err("invalid inc strong node for %d\n",
946 node->internal_strong_refs++;
948 node->local_strong_refs++;
949 if (!node->has_strong_ref && target_list) {
950 list_del_init(&node->work.entry);
951 list_add_tail(&node->work.entry, target_list);
955 node->local_weak_refs++;
956 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
957 if (target_list == NULL) {
958 pr_err("invalid inc weak node for %d\n",
962 list_add_tail(&node->work.entry, target_list);
968 static int binder_dec_node(struct binder_node *node, int strong, int internal)
972 node->internal_strong_refs--;
974 node->local_strong_refs--;
975 if (node->local_strong_refs || node->internal_strong_refs)
979 node->local_weak_refs--;
980 if (node->local_weak_refs || !hlist_empty(&node->refs))
983 if (node->proc && (node->has_strong_ref || node->has_weak_ref)) {
984 if (list_empty(&node->work.entry)) {
985 list_add_tail(&node->work.entry, &node->proc->todo);
986 wake_up_interruptible(&node->proc->wait);
989 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
990 !node->local_weak_refs) {
991 list_del_init(&node->work.entry);
993 rb_erase(&node->rb_node, &node->proc->nodes);
994 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
995 "refless node %d deleted\n",
998 hlist_del(&node->dead_node);
999 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1000 "dead node %d deleted\n",
1004 binder_stats_deleted(BINDER_STAT_NODE);
1012 static struct binder_ref *binder_get_ref(struct binder_proc *proc,
1013 u32 desc, bool need_strong_ref)
1015 struct rb_node *n = proc->refs_by_desc.rb_node;
1016 struct binder_ref *ref;
1019 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1021 if (desc < ref->desc) {
1023 } else if (desc > ref->desc) {
1025 } else if (need_strong_ref && !ref->strong) {
1026 binder_user_error("tried to use weak ref as strong ref\n");
1035 static struct binder_ref *binder_get_ref_for_node(struct binder_proc *proc,
1036 struct binder_node *node)
1039 struct rb_node **p = &proc->refs_by_node.rb_node;
1040 struct rb_node *parent = NULL;
1041 struct binder_ref *ref, *new_ref;
1045 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1047 if (node < ref->node)
1049 else if (node > ref->node)
1050 p = &(*p)->rb_right;
1054 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1055 if (new_ref == NULL)
1057 binder_stats_created(BINDER_STAT_REF);
1058 new_ref->debug_id = ++binder_last_id;
1059 new_ref->proc = proc;
1060 new_ref->node = node;
1061 rb_link_node(&new_ref->rb_node_node, parent, p);
1062 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1064 new_ref->desc = (node == binder_context_mgr_node) ? 0 : 1;
1065 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1066 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1067 if (ref->desc > new_ref->desc)
1069 new_ref->desc = ref->desc + 1;
1072 p = &proc->refs_by_desc.rb_node;
1075 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1077 if (new_ref->desc < ref->desc)
1079 else if (new_ref->desc > ref->desc)
1080 p = &(*p)->rb_right;
1084 rb_link_node(&new_ref->rb_node_desc, parent, p);
1085 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
1087 hlist_add_head(&new_ref->node_entry, &node->refs);
1089 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1090 "%d new ref %d desc %d for node %d\n",
1091 proc->pid, new_ref->debug_id, new_ref->desc,
1094 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1095 "%d new ref %d desc %d for dead node\n",
1096 proc->pid, new_ref->debug_id, new_ref->desc);
1101 static void binder_delete_ref(struct binder_ref *ref)
1103 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1104 "%d delete ref %d desc %d for node %d\n",
1105 ref->proc->pid, ref->debug_id, ref->desc,
1106 ref->node->debug_id);
1108 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1109 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
1111 binder_dec_node(ref->node, 1, 1);
1112 hlist_del(&ref->node_entry);
1113 binder_dec_node(ref->node, 0, 1);
1115 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1116 "%d delete ref %d desc %d has death notification\n",
1117 ref->proc->pid, ref->debug_id, ref->desc);
1118 list_del(&ref->death->work.entry);
1120 binder_stats_deleted(BINDER_STAT_DEATH);
1123 binder_stats_deleted(BINDER_STAT_REF);
1126 static int binder_inc_ref(struct binder_ref *ref, int strong,
1127 struct list_head *target_list)
1132 if (ref->strong == 0) {
1133 ret = binder_inc_node(ref->node, 1, 1, target_list);
1139 if (ref->weak == 0) {
1140 ret = binder_inc_node(ref->node, 0, 1, target_list);
1150 static int binder_dec_ref(struct binder_ref *ref, int strong)
1153 if (ref->strong == 0) {
1154 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
1155 ref->proc->pid, ref->debug_id,
1156 ref->desc, ref->strong, ref->weak);
1160 if (ref->strong == 0) {
1163 ret = binder_dec_node(ref->node, strong, 1);
1168 if (ref->weak == 0) {
1169 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
1170 ref->proc->pid, ref->debug_id,
1171 ref->desc, ref->strong, ref->weak);
1176 if (ref->strong == 0 && ref->weak == 0)
1177 binder_delete_ref(ref);
1181 static void binder_pop_transaction(struct binder_thread *target_thread,
1182 struct binder_transaction *t)
1184 if (target_thread) {
1185 BUG_ON(target_thread->transaction_stack != t);
1186 BUG_ON(target_thread->transaction_stack->from != target_thread);
1187 target_thread->transaction_stack =
1188 target_thread->transaction_stack->from_parent;
1193 t->buffer->transaction = NULL;
1195 binder_stats_deleted(BINDER_STAT_TRANSACTION);
1198 static void binder_send_failed_reply(struct binder_transaction *t,
1199 uint32_t error_code)
1201 struct binder_thread *target_thread;
1202 struct binder_transaction *next;
1204 BUG_ON(t->flags & TF_ONE_WAY);
1206 target_thread = t->from;
1207 if (target_thread) {
1208 if (target_thread->return_error != BR_OK &&
1209 target_thread->return_error2 == BR_OK) {
1210 target_thread->return_error2 =
1211 target_thread->return_error;
1212 target_thread->return_error = BR_OK;
1214 if (target_thread->return_error == BR_OK) {
1215 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1216 "send failed reply for transaction %d to %d:%d\n",
1218 target_thread->proc->pid,
1219 target_thread->pid);
1221 binder_pop_transaction(target_thread, t);
1222 target_thread->return_error = error_code;
1223 wake_up_interruptible(&target_thread->wait);
1225 pr_err("reply failed, target thread, %d:%d, has error code %d already\n",
1226 target_thread->proc->pid,
1228 target_thread->return_error);
1232 next = t->from_parent;
1234 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1235 "send failed reply for transaction %d, target dead\n",
1238 binder_pop_transaction(target_thread, t);
1240 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1241 "reply failed, no target thread at root\n");
1245 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1246 "reply failed, no target thread -- retry %d\n",
1251 static void binder_transaction_buffer_release(struct binder_proc *proc,
1252 struct binder_buffer *buffer,
1253 binder_size_t *failed_at)
1255 binder_size_t *offp, *off_end;
1256 int debug_id = buffer->debug_id;
1258 binder_debug(BINDER_DEBUG_TRANSACTION,
1259 "%d buffer release %d, size %zd-%zd, failed at %pK\n",
1260 proc->pid, buffer->debug_id,
1261 buffer->data_size, buffer->offsets_size, failed_at);
1263 if (buffer->target_node)
1264 binder_dec_node(buffer->target_node, 1, 0);
1266 offp = (binder_size_t *)(buffer->data +
1267 ALIGN(buffer->data_size, sizeof(void *)));
1269 off_end = failed_at;
1271 off_end = (void *)offp + buffer->offsets_size;
1272 for (; offp < off_end; offp++) {
1273 struct flat_binder_object *fp;
1275 if (*offp > buffer->data_size - sizeof(*fp) ||
1276 buffer->data_size < sizeof(*fp) ||
1277 !IS_ALIGNED(*offp, sizeof(u32))) {
1278 pr_err("transaction release %d bad offset %lld, size %zd\n",
1279 debug_id, (u64)*offp, buffer->data_size);
1282 fp = (struct flat_binder_object *)(buffer->data + *offp);
1284 case BINDER_TYPE_BINDER:
1285 case BINDER_TYPE_WEAK_BINDER: {
1286 struct binder_node *node = binder_get_node(proc, fp->binder);
1289 pr_err("transaction release %d bad node %016llx\n",
1290 debug_id, (u64)fp->binder);
1293 binder_debug(BINDER_DEBUG_TRANSACTION,
1294 " node %d u%016llx\n",
1295 node->debug_id, (u64)node->ptr);
1296 binder_dec_node(node, fp->type == BINDER_TYPE_BINDER, 0);
1298 case BINDER_TYPE_HANDLE:
1299 case BINDER_TYPE_WEAK_HANDLE: {
1300 struct binder_ref *ref;
1302 ref = binder_get_ref(proc, fp->handle,
1303 fp->type == BINDER_TYPE_HANDLE);
1306 pr_err("transaction release %d bad handle %d\n",
1307 debug_id, fp->handle);
1310 binder_debug(BINDER_DEBUG_TRANSACTION,
1311 " ref %d desc %d (node %d)\n",
1312 ref->debug_id, ref->desc, ref->node->debug_id);
1313 binder_dec_ref(ref, fp->type == BINDER_TYPE_HANDLE);
1316 case BINDER_TYPE_FD:
1317 binder_debug(BINDER_DEBUG_TRANSACTION,
1318 " fd %d\n", fp->handle);
1320 task_close_fd(proc, fp->handle);
1324 pr_err("transaction release %d bad object type %x\n",
1325 debug_id, fp->type);
1331 static void binder_transaction(struct binder_proc *proc,
1332 struct binder_thread *thread,
1333 struct binder_transaction_data *tr, int reply)
1335 struct binder_transaction *t;
1336 struct binder_work *tcomplete;
1337 binder_size_t *offp, *off_end;
1338 struct binder_proc *target_proc;
1339 struct binder_thread *target_thread = NULL;
1340 struct binder_node *target_node = NULL;
1341 struct list_head *target_list;
1342 wait_queue_head_t *target_wait;
1343 struct binder_transaction *in_reply_to = NULL;
1344 struct binder_transaction_log_entry *e;
1345 uint32_t return_error;
1347 e = binder_transaction_log_add(&binder_transaction_log);
1348 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
1349 e->from_proc = proc->pid;
1350 e->from_thread = thread->pid;
1351 e->target_handle = tr->target.handle;
1352 e->data_size = tr->data_size;
1353 e->offsets_size = tr->offsets_size;
1356 in_reply_to = thread->transaction_stack;
1357 if (in_reply_to == NULL) {
1358 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
1359 proc->pid, thread->pid);
1360 return_error = BR_FAILED_REPLY;
1361 goto err_empty_call_stack;
1363 binder_set_nice(in_reply_to->saved_priority);
1364 if (in_reply_to->to_thread != thread) {
1365 binder_user_error("%d:%d got reply transaction with bad transaction stack, transaction %d has target %d:%d\n",
1366 proc->pid, thread->pid, in_reply_to->debug_id,
1367 in_reply_to->to_proc ?
1368 in_reply_to->to_proc->pid : 0,
1369 in_reply_to->to_thread ?
1370 in_reply_to->to_thread->pid : 0);
1371 return_error = BR_FAILED_REPLY;
1373 goto err_bad_call_stack;
1375 thread->transaction_stack = in_reply_to->to_parent;
1376 target_thread = in_reply_to->from;
1377 if (target_thread == NULL) {
1378 return_error = BR_DEAD_REPLY;
1379 goto err_dead_binder;
1381 if (target_thread->transaction_stack != in_reply_to) {
1382 binder_user_error("%d:%d got reply transaction with bad target transaction stack %d, expected %d\n",
1383 proc->pid, thread->pid,
1384 target_thread->transaction_stack ?
1385 target_thread->transaction_stack->debug_id : 0,
1386 in_reply_to->debug_id);
1387 return_error = BR_FAILED_REPLY;
1389 target_thread = NULL;
1390 goto err_dead_binder;
1392 target_proc = target_thread->proc;
1394 if (tr->target.handle) {
1395 struct binder_ref *ref;
1397 ref = binder_get_ref(proc, tr->target.handle, true);
1399 binder_user_error("%d:%d got transaction to invalid handle\n",
1400 proc->pid, thread->pid);
1401 return_error = BR_FAILED_REPLY;
1402 goto err_invalid_target_handle;
1404 target_node = ref->node;
1406 target_node = binder_context_mgr_node;
1407 if (target_node == NULL) {
1408 return_error = BR_DEAD_REPLY;
1409 goto err_no_context_mgr_node;
1412 e->to_node = target_node->debug_id;
1413 target_proc = target_node->proc;
1414 if (target_proc == NULL) {
1415 return_error = BR_DEAD_REPLY;
1416 goto err_dead_binder;
1418 if (WARN_ON(proc == target_proc)) {
1419 return_error = BR_FAILED_REPLY;
1420 goto err_invalid_target_handle;
1422 if (security_binder_transaction(proc->tsk,
1423 target_proc->tsk) < 0) {
1424 return_error = BR_FAILED_REPLY;
1425 goto err_invalid_target_handle;
1427 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
1428 struct binder_transaction *tmp;
1430 tmp = thread->transaction_stack;
1431 if (tmp->to_thread != thread) {
1432 binder_user_error("%d:%d got new transaction with bad transaction stack, transaction %d has target %d:%d\n",
1433 proc->pid, thread->pid, tmp->debug_id,
1434 tmp->to_proc ? tmp->to_proc->pid : 0,
1436 tmp->to_thread->pid : 0);
1437 return_error = BR_FAILED_REPLY;
1438 goto err_bad_call_stack;
1441 if (tmp->from && tmp->from->proc == target_proc)
1442 target_thread = tmp->from;
1443 tmp = tmp->from_parent;
1447 if (target_thread) {
1448 e->to_thread = target_thread->pid;
1449 target_list = &target_thread->todo;
1450 target_wait = &target_thread->wait;
1452 target_list = &target_proc->todo;
1453 target_wait = &target_proc->wait;
1455 e->to_proc = target_proc->pid;
1457 /* TODO: reuse incoming transaction for reply */
1458 t = kzalloc(sizeof(*t), GFP_KERNEL);
1460 return_error = BR_FAILED_REPLY;
1461 goto err_alloc_t_failed;
1463 binder_stats_created(BINDER_STAT_TRANSACTION);
1465 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
1466 if (tcomplete == NULL) {
1467 return_error = BR_FAILED_REPLY;
1468 goto err_alloc_tcomplete_failed;
1470 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
1472 t->debug_id = ++binder_last_id;
1473 e->debug_id = t->debug_id;
1476 binder_debug(BINDER_DEBUG_TRANSACTION,
1477 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld\n",
1478 proc->pid, thread->pid, t->debug_id,
1479 target_proc->pid, target_thread->pid,
1480 (u64)tr->data.ptr.buffer,
1481 (u64)tr->data.ptr.offsets,
1482 (u64)tr->data_size, (u64)tr->offsets_size);
1484 binder_debug(BINDER_DEBUG_TRANSACTION,
1485 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld\n",
1486 proc->pid, thread->pid, t->debug_id,
1487 target_proc->pid, target_node->debug_id,
1488 (u64)tr->data.ptr.buffer,
1489 (u64)tr->data.ptr.offsets,
1490 (u64)tr->data_size, (u64)tr->offsets_size);
1492 if (!reply && !(tr->flags & TF_ONE_WAY))
1496 t->sender_euid = task_euid(proc->tsk);
1497 t->to_proc = target_proc;
1498 t->to_thread = target_thread;
1500 t->flags = tr->flags;
1501 t->priority = task_nice(current);
1503 trace_binder_transaction(reply, t, target_node);
1505 t->buffer = binder_alloc_buf(target_proc, tr->data_size,
1506 tr->offsets_size, !reply && (t->flags & TF_ONE_WAY));
1507 if (t->buffer == NULL) {
1508 return_error = BR_FAILED_REPLY;
1509 goto err_binder_alloc_buf_failed;
1511 t->buffer->allow_user_free = 0;
1512 t->buffer->debug_id = t->debug_id;
1513 t->buffer->transaction = t;
1514 t->buffer->target_node = target_node;
1515 trace_binder_transaction_alloc_buf(t->buffer);
1517 binder_inc_node(target_node, 1, 0, NULL);
1519 offp = (binder_size_t *)(t->buffer->data +
1520 ALIGN(tr->data_size, sizeof(void *)));
1522 if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t)
1523 tr->data.ptr.buffer, tr->data_size)) {
1524 binder_user_error("%d:%d got transaction with invalid data ptr\n",
1525 proc->pid, thread->pid);
1526 return_error = BR_FAILED_REPLY;
1527 goto err_copy_data_failed;
1529 if (copy_from_user(offp, (const void __user *)(uintptr_t)
1530 tr->data.ptr.offsets, tr->offsets_size)) {
1531 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
1532 proc->pid, thread->pid);
1533 return_error = BR_FAILED_REPLY;
1534 goto err_copy_data_failed;
1536 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
1537 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
1538 proc->pid, thread->pid, (u64)tr->offsets_size);
1539 return_error = BR_FAILED_REPLY;
1540 goto err_bad_offset;
1542 off_end = (void *)offp + tr->offsets_size;
1543 for (; offp < off_end; offp++) {
1544 struct flat_binder_object *fp;
1546 if (*offp > t->buffer->data_size - sizeof(*fp) ||
1547 t->buffer->data_size < sizeof(*fp) ||
1548 !IS_ALIGNED(*offp, sizeof(u32))) {
1549 binder_user_error("%d:%d got transaction with invalid offset, %lld\n",
1550 proc->pid, thread->pid, (u64)*offp);
1551 return_error = BR_FAILED_REPLY;
1552 goto err_bad_offset;
1554 fp = (struct flat_binder_object *)(t->buffer->data + *offp);
1556 case BINDER_TYPE_BINDER:
1557 case BINDER_TYPE_WEAK_BINDER: {
1558 struct binder_ref *ref;
1559 struct binder_node *node = binder_get_node(proc, fp->binder);
1562 node = binder_new_node(proc, fp->binder, fp->cookie);
1564 return_error = BR_FAILED_REPLY;
1565 goto err_binder_new_node_failed;
1567 node->min_priority = fp->flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
1568 node->accept_fds = !!(fp->flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
1570 if (fp->cookie != node->cookie) {
1571 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
1572 proc->pid, thread->pid,
1573 (u64)fp->binder, node->debug_id,
1574 (u64)fp->cookie, (u64)node->cookie);
1575 return_error = BR_FAILED_REPLY;
1576 goto err_binder_get_ref_for_node_failed;
1578 if (security_binder_transfer_binder(proc->tsk,
1579 target_proc->tsk)) {
1580 return_error = BR_FAILED_REPLY;
1581 goto err_binder_get_ref_for_node_failed;
1583 ref = binder_get_ref_for_node(target_proc, node);
1585 return_error = BR_FAILED_REPLY;
1586 goto err_binder_get_ref_for_node_failed;
1588 if (fp->type == BINDER_TYPE_BINDER)
1589 fp->type = BINDER_TYPE_HANDLE;
1591 fp->type = BINDER_TYPE_WEAK_HANDLE;
1593 fp->handle = ref->desc;
1595 binder_inc_ref(ref, fp->type == BINDER_TYPE_HANDLE,
1598 trace_binder_transaction_node_to_ref(t, node, ref);
1599 binder_debug(BINDER_DEBUG_TRANSACTION,
1600 " node %d u%016llx -> ref %d desc %d\n",
1601 node->debug_id, (u64)node->ptr,
1602 ref->debug_id, ref->desc);
1604 case BINDER_TYPE_HANDLE:
1605 case BINDER_TYPE_WEAK_HANDLE: {
1606 struct binder_ref *ref;
1608 ref = binder_get_ref(proc, fp->handle,
1609 fp->type == BINDER_TYPE_HANDLE);
1612 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
1614 thread->pid, fp->handle);
1615 return_error = BR_FAILED_REPLY;
1616 goto err_binder_get_ref_failed;
1618 if (security_binder_transfer_binder(proc->tsk,
1619 target_proc->tsk)) {
1620 return_error = BR_FAILED_REPLY;
1621 goto err_binder_get_ref_failed;
1623 if (ref->node->proc == target_proc) {
1624 if (fp->type == BINDER_TYPE_HANDLE)
1625 fp->type = BINDER_TYPE_BINDER;
1627 fp->type = BINDER_TYPE_WEAK_BINDER;
1628 fp->binder = ref->node->ptr;
1629 fp->cookie = ref->node->cookie;
1630 binder_inc_node(ref->node, fp->type == BINDER_TYPE_BINDER, 0, NULL);
1631 trace_binder_transaction_ref_to_node(t, ref);
1632 binder_debug(BINDER_DEBUG_TRANSACTION,
1633 " ref %d desc %d -> node %d u%016llx\n",
1634 ref->debug_id, ref->desc, ref->node->debug_id,
1635 (u64)ref->node->ptr);
1637 struct binder_ref *new_ref;
1639 new_ref = binder_get_ref_for_node(target_proc, ref->node);
1640 if (new_ref == NULL) {
1641 return_error = BR_FAILED_REPLY;
1642 goto err_binder_get_ref_for_node_failed;
1645 fp->handle = new_ref->desc;
1647 binder_inc_ref(new_ref, fp->type == BINDER_TYPE_HANDLE, NULL);
1648 trace_binder_transaction_ref_to_ref(t, ref,
1650 binder_debug(BINDER_DEBUG_TRANSACTION,
1651 " ref %d desc %d -> ref %d desc %d (node %d)\n",
1652 ref->debug_id, ref->desc, new_ref->debug_id,
1653 new_ref->desc, ref->node->debug_id);
1657 case BINDER_TYPE_FD: {
1662 if (!(in_reply_to->flags & TF_ACCEPT_FDS)) {
1663 binder_user_error("%d:%d got reply with fd, %d, but target does not allow fds\n",
1664 proc->pid, thread->pid, fp->handle);
1665 return_error = BR_FAILED_REPLY;
1666 goto err_fd_not_allowed;
1668 } else if (!target_node->accept_fds) {
1669 binder_user_error("%d:%d got transaction with fd, %d, but target does not allow fds\n",
1670 proc->pid, thread->pid, fp->handle);
1671 return_error = BR_FAILED_REPLY;
1672 goto err_fd_not_allowed;
1675 file = fget(fp->handle);
1677 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
1678 proc->pid, thread->pid, fp->handle);
1679 return_error = BR_FAILED_REPLY;
1680 goto err_fget_failed;
1682 if (security_binder_transfer_file(proc->tsk,
1686 return_error = BR_FAILED_REPLY;
1687 goto err_get_unused_fd_failed;
1689 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
1690 if (target_fd < 0) {
1692 return_error = BR_FAILED_REPLY;
1693 goto err_get_unused_fd_failed;
1695 task_fd_install(target_proc, target_fd, file);
1696 trace_binder_transaction_fd(t, fp->handle, target_fd);
1697 binder_debug(BINDER_DEBUG_TRANSACTION,
1698 " fd %d -> %d\n", fp->handle, target_fd);
1701 fp->handle = target_fd;
1705 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
1706 proc->pid, thread->pid, fp->type);
1707 return_error = BR_FAILED_REPLY;
1708 goto err_bad_object_type;
1712 BUG_ON(t->buffer->async_transaction != 0);
1713 binder_pop_transaction(target_thread, in_reply_to);
1714 } else if (!(t->flags & TF_ONE_WAY)) {
1715 BUG_ON(t->buffer->async_transaction != 0);
1717 t->from_parent = thread->transaction_stack;
1718 thread->transaction_stack = t;
1720 BUG_ON(target_node == NULL);
1721 BUG_ON(t->buffer->async_transaction != 1);
1722 if (target_node->has_async_transaction) {
1723 target_list = &target_node->async_todo;
1726 target_node->has_async_transaction = 1;
1728 t->work.type = BINDER_WORK_TRANSACTION;
1729 list_add_tail(&t->work.entry, target_list);
1730 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
1731 list_add_tail(&tcomplete->entry, &thread->todo);
1733 if (reply || !(t->flags & TF_ONE_WAY))
1734 wake_up_interruptible_sync(target_wait);
1736 wake_up_interruptible(target_wait);
1740 err_get_unused_fd_failed:
1743 err_binder_get_ref_for_node_failed:
1744 err_binder_get_ref_failed:
1745 err_binder_new_node_failed:
1746 err_bad_object_type:
1748 err_copy_data_failed:
1749 trace_binder_transaction_failed_buffer_release(t->buffer);
1750 binder_transaction_buffer_release(target_proc, t->buffer, offp);
1751 t->buffer->transaction = NULL;
1752 binder_free_buf(target_proc, t->buffer);
1753 err_binder_alloc_buf_failed:
1755 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
1756 err_alloc_tcomplete_failed:
1758 binder_stats_deleted(BINDER_STAT_TRANSACTION);
1761 err_empty_call_stack:
1763 err_invalid_target_handle:
1764 err_no_context_mgr_node:
1765 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1766 "%d:%d transaction failed %d, size %lld-%lld\n",
1767 proc->pid, thread->pid, return_error,
1768 (u64)tr->data_size, (u64)tr->offsets_size);
1771 struct binder_transaction_log_entry *fe;
1773 fe = binder_transaction_log_add(&binder_transaction_log_failed);
1777 BUG_ON(thread->return_error != BR_OK);
1779 thread->return_error = BR_TRANSACTION_COMPLETE;
1780 binder_send_failed_reply(in_reply_to, return_error);
1782 thread->return_error = return_error;
1785 static int binder_thread_write(struct binder_proc *proc,
1786 struct binder_thread *thread,
1787 binder_uintptr_t binder_buffer, size_t size,
1788 binder_size_t *consumed)
1791 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
1792 void __user *ptr = buffer + *consumed;
1793 void __user *end = buffer + size;
1795 while (ptr < end && thread->return_error == BR_OK) {
1796 if (get_user(cmd, (uint32_t __user *)ptr))
1798 ptr += sizeof(uint32_t);
1799 trace_binder_command(cmd);
1800 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
1801 binder_stats.bc[_IOC_NR(cmd)]++;
1802 proc->stats.bc[_IOC_NR(cmd)]++;
1803 thread->stats.bc[_IOC_NR(cmd)]++;
1811 struct binder_ref *ref;
1812 const char *debug_string;
1814 if (get_user(target, (uint32_t __user *)ptr))
1816 ptr += sizeof(uint32_t);
1817 if (target == 0 && binder_context_mgr_node &&
1818 (cmd == BC_INCREFS || cmd == BC_ACQUIRE)) {
1819 if (binder_context_mgr_node->proc == proc) {
1820 binder_user_error("%d:%d context manager tried to acquire desc 0\n",
1821 proc->pid, thread->pid);
1824 ref = binder_get_ref_for_node(proc,
1825 binder_context_mgr_node);
1826 if (ref->desc != target) {
1827 binder_user_error("%d:%d tried to acquire reference to desc 0, got %d instead\n",
1828 proc->pid, thread->pid,
1832 ref = binder_get_ref(proc, target,
1833 cmd == BC_ACQUIRE ||
1836 binder_user_error("%d:%d refcount change on invalid ref %d\n",
1837 proc->pid, thread->pid, target);
1842 debug_string = "IncRefs";
1843 binder_inc_ref(ref, 0, NULL);
1846 debug_string = "Acquire";
1847 binder_inc_ref(ref, 1, NULL);
1850 debug_string = "Release";
1851 binder_dec_ref(ref, 1);
1855 debug_string = "DecRefs";
1856 binder_dec_ref(ref, 0);
1859 binder_debug(BINDER_DEBUG_USER_REFS,
1860 "%d:%d %s ref %d desc %d s %d w %d for node %d\n",
1861 proc->pid, thread->pid, debug_string, ref->debug_id,
1862 ref->desc, ref->strong, ref->weak, ref->node->debug_id);
1865 case BC_INCREFS_DONE:
1866 case BC_ACQUIRE_DONE: {
1867 binder_uintptr_t node_ptr;
1868 binder_uintptr_t cookie;
1869 struct binder_node *node;
1871 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
1873 ptr += sizeof(binder_uintptr_t);
1874 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
1876 ptr += sizeof(binder_uintptr_t);
1877 node = binder_get_node(proc, node_ptr);
1879 binder_user_error("%d:%d %s u%016llx no match\n",
1880 proc->pid, thread->pid,
1881 cmd == BC_INCREFS_DONE ?
1887 if (cookie != node->cookie) {
1888 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
1889 proc->pid, thread->pid,
1890 cmd == BC_INCREFS_DONE ?
1891 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
1892 (u64)node_ptr, node->debug_id,
1893 (u64)cookie, (u64)node->cookie);
1896 if (cmd == BC_ACQUIRE_DONE) {
1897 if (node->pending_strong_ref == 0) {
1898 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
1899 proc->pid, thread->pid,
1903 node->pending_strong_ref = 0;
1905 if (node->pending_weak_ref == 0) {
1906 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
1907 proc->pid, thread->pid,
1911 node->pending_weak_ref = 0;
1913 binder_dec_node(node, cmd == BC_ACQUIRE_DONE, 0);
1914 binder_debug(BINDER_DEBUG_USER_REFS,
1915 "%d:%d %s node %d ls %d lw %d\n",
1916 proc->pid, thread->pid,
1917 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
1918 node->debug_id, node->local_strong_refs, node->local_weak_refs);
1921 case BC_ATTEMPT_ACQUIRE:
1922 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
1924 case BC_ACQUIRE_RESULT:
1925 pr_err("BC_ACQUIRE_RESULT not supported\n");
1928 case BC_FREE_BUFFER: {
1929 binder_uintptr_t data_ptr;
1930 struct binder_buffer *buffer;
1932 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
1934 ptr += sizeof(binder_uintptr_t);
1936 buffer = binder_buffer_lookup(proc, data_ptr);
1937 if (buffer == NULL) {
1938 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
1939 proc->pid, thread->pid, (u64)data_ptr);
1942 if (!buffer->allow_user_free) {
1943 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
1944 proc->pid, thread->pid, (u64)data_ptr);
1947 binder_debug(BINDER_DEBUG_FREE_BUFFER,
1948 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
1949 proc->pid, thread->pid, (u64)data_ptr,
1951 buffer->transaction ? "active" : "finished");
1953 if (buffer->transaction) {
1954 buffer->transaction->buffer = NULL;
1955 buffer->transaction = NULL;
1957 if (buffer->async_transaction && buffer->target_node) {
1958 BUG_ON(!buffer->target_node->has_async_transaction);
1959 if (list_empty(&buffer->target_node->async_todo))
1960 buffer->target_node->has_async_transaction = 0;
1962 list_move_tail(buffer->target_node->async_todo.next, &thread->todo);
1964 trace_binder_transaction_buffer_release(buffer);
1965 binder_transaction_buffer_release(proc, buffer, NULL);
1966 binder_free_buf(proc, buffer);
1970 case BC_TRANSACTION:
1972 struct binder_transaction_data tr;
1974 if (copy_from_user(&tr, ptr, sizeof(tr)))
1977 binder_transaction(proc, thread, &tr, cmd == BC_REPLY);
1981 case BC_REGISTER_LOOPER:
1982 binder_debug(BINDER_DEBUG_THREADS,
1983 "%d:%d BC_REGISTER_LOOPER\n",
1984 proc->pid, thread->pid);
1985 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
1986 thread->looper |= BINDER_LOOPER_STATE_INVALID;
1987 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
1988 proc->pid, thread->pid);
1989 } else if (proc->requested_threads == 0) {
1990 thread->looper |= BINDER_LOOPER_STATE_INVALID;
1991 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
1992 proc->pid, thread->pid);
1994 proc->requested_threads--;
1995 proc->requested_threads_started++;
1997 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
1999 case BC_ENTER_LOOPER:
2000 binder_debug(BINDER_DEBUG_THREADS,
2001 "%d:%d BC_ENTER_LOOPER\n",
2002 proc->pid, thread->pid);
2003 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
2004 thread->looper |= BINDER_LOOPER_STATE_INVALID;
2005 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
2006 proc->pid, thread->pid);
2008 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
2010 case BC_EXIT_LOOPER:
2011 binder_debug(BINDER_DEBUG_THREADS,
2012 "%d:%d BC_EXIT_LOOPER\n",
2013 proc->pid, thread->pid);
2014 thread->looper |= BINDER_LOOPER_STATE_EXITED;
2017 case BC_REQUEST_DEATH_NOTIFICATION:
2018 case BC_CLEAR_DEATH_NOTIFICATION: {
2020 binder_uintptr_t cookie;
2021 struct binder_ref *ref;
2022 struct binder_ref_death *death;
2024 if (get_user(target, (uint32_t __user *)ptr))
2026 ptr += sizeof(uint32_t);
2027 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
2029 ptr += sizeof(binder_uintptr_t);
2030 ref = binder_get_ref(proc, target, false);
2032 binder_user_error("%d:%d %s invalid ref %d\n",
2033 proc->pid, thread->pid,
2034 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
2035 "BC_REQUEST_DEATH_NOTIFICATION" :
2036 "BC_CLEAR_DEATH_NOTIFICATION",
2041 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
2042 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
2043 proc->pid, thread->pid,
2044 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
2045 "BC_REQUEST_DEATH_NOTIFICATION" :
2046 "BC_CLEAR_DEATH_NOTIFICATION",
2047 (u64)cookie, ref->debug_id, ref->desc,
2048 ref->strong, ref->weak, ref->node->debug_id);
2050 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
2052 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
2053 proc->pid, thread->pid);
2056 death = kzalloc(sizeof(*death), GFP_KERNEL);
2057 if (death == NULL) {
2058 thread->return_error = BR_ERROR;
2059 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2060 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
2061 proc->pid, thread->pid);
2064 binder_stats_created(BINDER_STAT_DEATH);
2065 INIT_LIST_HEAD(&death->work.entry);
2066 death->cookie = cookie;
2068 if (ref->node->proc == NULL) {
2069 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
2070 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2071 list_add_tail(&ref->death->work.entry, &thread->todo);
2073 list_add_tail(&ref->death->work.entry, &proc->todo);
2074 wake_up_interruptible(&proc->wait);
2078 if (ref->death == NULL) {
2079 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
2080 proc->pid, thread->pid);
2084 if (death->cookie != cookie) {
2085 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
2086 proc->pid, thread->pid,
2092 if (list_empty(&death->work.entry)) {
2093 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2094 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2095 list_add_tail(&death->work.entry, &thread->todo);
2097 list_add_tail(&death->work.entry, &proc->todo);
2098 wake_up_interruptible(&proc->wait);
2101 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
2102 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
2106 case BC_DEAD_BINDER_DONE: {
2107 struct binder_work *w;
2108 binder_uintptr_t cookie;
2109 struct binder_ref_death *death = NULL;
2111 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
2114 ptr += sizeof(cookie);
2115 list_for_each_entry(w, &proc->delivered_death, entry) {
2116 struct binder_ref_death *tmp_death = container_of(w, struct binder_ref_death, work);
2118 if (tmp_death->cookie == cookie) {
2123 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2124 "%d:%d BC_DEAD_BINDER_DONE %016llx found %pK\n",
2125 proc->pid, thread->pid, (u64)cookie,
2127 if (death == NULL) {
2128 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
2129 proc->pid, thread->pid, (u64)cookie);
2133 list_del_init(&death->work.entry);
2134 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
2135 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2136 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2137 list_add_tail(&death->work.entry, &thread->todo);
2139 list_add_tail(&death->work.entry, &proc->todo);
2140 wake_up_interruptible(&proc->wait);
2146 pr_err("%d:%d unknown command %d\n",
2147 proc->pid, thread->pid, cmd);
2150 *consumed = ptr - buffer;
2155 static void binder_stat_br(struct binder_proc *proc,
2156 struct binder_thread *thread, uint32_t cmd)
2158 trace_binder_return(cmd);
2159 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
2160 binder_stats.br[_IOC_NR(cmd)]++;
2161 proc->stats.br[_IOC_NR(cmd)]++;
2162 thread->stats.br[_IOC_NR(cmd)]++;
2166 static int binder_has_proc_work(struct binder_proc *proc,
2167 struct binder_thread *thread)
2169 return !list_empty(&proc->todo) ||
2170 (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2173 static int binder_has_thread_work(struct binder_thread *thread)
2175 return !list_empty(&thread->todo) || thread->return_error != BR_OK ||
2176 (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2179 static int binder_thread_read(struct binder_proc *proc,
2180 struct binder_thread *thread,
2181 binder_uintptr_t binder_buffer, size_t size,
2182 binder_size_t *consumed, int non_block)
2184 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
2185 void __user *ptr = buffer + *consumed;
2186 void __user *end = buffer + size;
2189 int wait_for_proc_work;
2191 if (*consumed == 0) {
2192 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
2194 ptr += sizeof(uint32_t);
2198 wait_for_proc_work = thread->transaction_stack == NULL &&
2199 list_empty(&thread->todo);
2201 if (thread->return_error != BR_OK && ptr < end) {
2202 if (thread->return_error2 != BR_OK) {
2203 if (put_user(thread->return_error2, (uint32_t __user *)ptr))
2205 ptr += sizeof(uint32_t);
2206 binder_stat_br(proc, thread, thread->return_error2);
2209 thread->return_error2 = BR_OK;
2211 if (put_user(thread->return_error, (uint32_t __user *)ptr))
2213 ptr += sizeof(uint32_t);
2214 binder_stat_br(proc, thread, thread->return_error);
2215 thread->return_error = BR_OK;
2220 thread->looper |= BINDER_LOOPER_STATE_WAITING;
2221 if (wait_for_proc_work)
2222 proc->ready_threads++;
2224 binder_unlock(__func__);
2226 trace_binder_wait_for_work(wait_for_proc_work,
2227 !!thread->transaction_stack,
2228 !list_empty(&thread->todo));
2229 if (wait_for_proc_work) {
2230 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2231 BINDER_LOOPER_STATE_ENTERED))) {
2232 binder_user_error("%d:%d ERROR: Thread waiting for process work before calling BC_REGISTER_LOOPER or BC_ENTER_LOOPER (state %x)\n",
2233 proc->pid, thread->pid, thread->looper);
2234 wait_event_interruptible(binder_user_error_wait,
2235 binder_stop_on_user_error < 2);
2237 binder_set_nice(proc->default_priority);
2239 if (!binder_has_proc_work(proc, thread))
2242 ret = wait_event_freezable_exclusive(proc->wait, binder_has_proc_work(proc, thread));
2245 if (!binder_has_thread_work(thread))
2248 ret = wait_event_freezable(thread->wait, binder_has_thread_work(thread));
2251 binder_lock(__func__);
2253 if (wait_for_proc_work)
2254 proc->ready_threads--;
2255 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
2262 struct binder_transaction_data tr;
2263 struct binder_work *w;
2264 struct binder_transaction *t = NULL;
2266 if (!list_empty(&thread->todo)) {
2267 w = list_first_entry(&thread->todo, struct binder_work,
2269 } else if (!list_empty(&proc->todo) && wait_for_proc_work) {
2270 w = list_first_entry(&proc->todo, struct binder_work,
2274 if (ptr - buffer == 4 &&
2275 !(thread->looper & BINDER_LOOPER_STATE_NEED_RETURN))
2280 if (end - ptr < sizeof(tr) + 4)
2284 case BINDER_WORK_TRANSACTION: {
2285 t = container_of(w, struct binder_transaction, work);
2287 case BINDER_WORK_TRANSACTION_COMPLETE: {
2288 cmd = BR_TRANSACTION_COMPLETE;
2289 if (put_user(cmd, (uint32_t __user *)ptr))
2291 ptr += sizeof(uint32_t);
2293 binder_stat_br(proc, thread, cmd);
2294 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
2295 "%d:%d BR_TRANSACTION_COMPLETE\n",
2296 proc->pid, thread->pid);
2298 list_del(&w->entry);
2300 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2302 case BINDER_WORK_NODE: {
2303 struct binder_node *node = container_of(w, struct binder_node, work);
2304 uint32_t cmd = BR_NOOP;
2305 const char *cmd_name;
2306 int strong = node->internal_strong_refs || node->local_strong_refs;
2307 int weak = !hlist_empty(&node->refs) || node->local_weak_refs || strong;
2309 if (weak && !node->has_weak_ref) {
2311 cmd_name = "BR_INCREFS";
2312 node->has_weak_ref = 1;
2313 node->pending_weak_ref = 1;
2314 node->local_weak_refs++;
2315 } else if (strong && !node->has_strong_ref) {
2317 cmd_name = "BR_ACQUIRE";
2318 node->has_strong_ref = 1;
2319 node->pending_strong_ref = 1;
2320 node->local_strong_refs++;
2321 } else if (!strong && node->has_strong_ref) {
2323 cmd_name = "BR_RELEASE";
2324 node->has_strong_ref = 0;
2325 } else if (!weak && node->has_weak_ref) {
2327 cmd_name = "BR_DECREFS";
2328 node->has_weak_ref = 0;
2330 if (cmd != BR_NOOP) {
2331 if (put_user(cmd, (uint32_t __user *)ptr))
2333 ptr += sizeof(uint32_t);
2334 if (put_user(node->ptr,
2335 (binder_uintptr_t __user *)ptr))
2337 ptr += sizeof(binder_uintptr_t);
2338 if (put_user(node->cookie,
2339 (binder_uintptr_t __user *)ptr))
2341 ptr += sizeof(binder_uintptr_t);
2343 binder_stat_br(proc, thread, cmd);
2344 binder_debug(BINDER_DEBUG_USER_REFS,
2345 "%d:%d %s %d u%016llx c%016llx\n",
2346 proc->pid, thread->pid, cmd_name,
2348 (u64)node->ptr, (u64)node->cookie);
2350 list_del_init(&w->entry);
2351 if (!weak && !strong) {
2352 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
2353 "%d:%d node %d u%016llx c%016llx deleted\n",
2354 proc->pid, thread->pid,
2358 rb_erase(&node->rb_node, &proc->nodes);
2360 binder_stats_deleted(BINDER_STAT_NODE);
2362 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
2363 "%d:%d node %d u%016llx c%016llx state unchanged\n",
2364 proc->pid, thread->pid,
2371 case BINDER_WORK_DEAD_BINDER:
2372 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2373 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2374 struct binder_ref_death *death;
2377 death = container_of(w, struct binder_ref_death, work);
2378 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
2379 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
2381 cmd = BR_DEAD_BINDER;
2382 if (put_user(cmd, (uint32_t __user *)ptr))
2384 ptr += sizeof(uint32_t);
2385 if (put_user(death->cookie,
2386 (binder_uintptr_t __user *)ptr))
2388 ptr += sizeof(binder_uintptr_t);
2389 binder_stat_br(proc, thread, cmd);
2390 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
2391 "%d:%d %s %016llx\n",
2392 proc->pid, thread->pid,
2393 cmd == BR_DEAD_BINDER ?
2395 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
2396 (u64)death->cookie);
2398 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
2399 list_del(&w->entry);
2401 binder_stats_deleted(BINDER_STAT_DEATH);
2403 list_move(&w->entry, &proc->delivered_death);
2404 if (cmd == BR_DEAD_BINDER)
2405 goto done; /* DEAD_BINDER notifications can cause transactions */
2412 BUG_ON(t->buffer == NULL);
2413 if (t->buffer->target_node) {
2414 struct binder_node *target_node = t->buffer->target_node;
2416 tr.target.ptr = target_node->ptr;
2417 tr.cookie = target_node->cookie;
2418 t->saved_priority = task_nice(current);
2419 if (t->priority < target_node->min_priority &&
2420 !(t->flags & TF_ONE_WAY))
2421 binder_set_nice(t->priority);
2422 else if (!(t->flags & TF_ONE_WAY) ||
2423 t->saved_priority > target_node->min_priority)
2424 binder_set_nice(target_node->min_priority);
2425 cmd = BR_TRANSACTION;
2432 tr.flags = t->flags;
2433 tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
2436 struct task_struct *sender = t->from->proc->tsk;
2438 tr.sender_pid = task_tgid_nr_ns(sender,
2439 task_active_pid_ns(current));
2444 tr.data_size = t->buffer->data_size;
2445 tr.offsets_size = t->buffer->offsets_size;
2446 tr.data.ptr.buffer = (binder_uintptr_t)(
2447 (uintptr_t)t->buffer->data +
2448 proc->user_buffer_offset);
2449 tr.data.ptr.offsets = tr.data.ptr.buffer +
2450 ALIGN(t->buffer->data_size,
2453 if (put_user(cmd, (uint32_t __user *)ptr))
2455 ptr += sizeof(uint32_t);
2456 if (copy_to_user(ptr, &tr, sizeof(tr)))
2460 trace_binder_transaction_received(t);
2461 binder_stat_br(proc, thread, cmd);
2462 binder_debug(BINDER_DEBUG_TRANSACTION,
2463 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
2464 proc->pid, thread->pid,
2465 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
2467 t->debug_id, t->from ? t->from->proc->pid : 0,
2468 t->from ? t->from->pid : 0, cmd,
2469 t->buffer->data_size, t->buffer->offsets_size,
2470 (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
2472 list_del(&t->work.entry);
2473 t->buffer->allow_user_free = 1;
2474 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
2475 t->to_parent = thread->transaction_stack;
2476 t->to_thread = thread;
2477 thread->transaction_stack = t;
2479 t->buffer->transaction = NULL;
2481 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2488 *consumed = ptr - buffer;
2489 if (proc->requested_threads + proc->ready_threads == 0 &&
2490 proc->requested_threads_started < proc->max_threads &&
2491 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2492 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
2493 /*spawn a new thread if we leave this out */) {
2494 proc->requested_threads++;
2495 binder_debug(BINDER_DEBUG_THREADS,
2496 "%d:%d BR_SPAWN_LOOPER\n",
2497 proc->pid, thread->pid);
2498 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
2500 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
2505 static void binder_release_work(struct list_head *list)
2507 struct binder_work *w;
2509 while (!list_empty(list)) {
2510 w = list_first_entry(list, struct binder_work, entry);
2511 list_del_init(&w->entry);
2513 case BINDER_WORK_TRANSACTION: {
2514 struct binder_transaction *t;
2516 t = container_of(w, struct binder_transaction, work);
2517 if (t->buffer->target_node &&
2518 !(t->flags & TF_ONE_WAY)) {
2519 binder_send_failed_reply(t, BR_DEAD_REPLY);
2521 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2522 "undelivered transaction %d\n",
2524 t->buffer->transaction = NULL;
2526 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2529 case BINDER_WORK_TRANSACTION_COMPLETE: {
2530 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2531 "undelivered TRANSACTION_COMPLETE\n");
2533 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2535 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2536 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2537 struct binder_ref_death *death;
2539 death = container_of(w, struct binder_ref_death, work);
2540 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2541 "undelivered death notification, %016llx\n",
2542 (u64)death->cookie);
2544 binder_stats_deleted(BINDER_STAT_DEATH);
2547 pr_err("unexpected work type, %d, not freed\n",
2555 static struct binder_thread *binder_get_thread(struct binder_proc *proc)
2557 struct binder_thread *thread = NULL;
2558 struct rb_node *parent = NULL;
2559 struct rb_node **p = &proc->threads.rb_node;
2563 thread = rb_entry(parent, struct binder_thread, rb_node);
2565 if (current->pid < thread->pid)
2567 else if (current->pid > thread->pid)
2568 p = &(*p)->rb_right;
2573 thread = kzalloc(sizeof(*thread), GFP_KERNEL);
2576 binder_stats_created(BINDER_STAT_THREAD);
2577 thread->proc = proc;
2578 thread->pid = current->pid;
2579 init_waitqueue_head(&thread->wait);
2580 INIT_LIST_HEAD(&thread->todo);
2581 rb_link_node(&thread->rb_node, parent, p);
2582 rb_insert_color(&thread->rb_node, &proc->threads);
2583 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
2584 thread->return_error = BR_OK;
2585 thread->return_error2 = BR_OK;
2590 static int binder_free_thread(struct binder_proc *proc,
2591 struct binder_thread *thread)
2593 struct binder_transaction *t;
2594 struct binder_transaction *send_reply = NULL;
2595 int active_transactions = 0;
2597 rb_erase(&thread->rb_node, &proc->threads);
2598 t = thread->transaction_stack;
2599 if (t && t->to_thread == thread)
2602 active_transactions++;
2603 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2604 "release %d:%d transaction %d %s, still active\n",
2605 proc->pid, thread->pid,
2607 (t->to_thread == thread) ? "in" : "out");
2609 if (t->to_thread == thread) {
2611 t->to_thread = NULL;
2613 t->buffer->transaction = NULL;
2617 } else if (t->from == thread) {
2625 * If this thread used poll, make sure we remove the waitqueue
2626 * from any epoll data structures holding it with POLLFREE.
2627 * waitqueue_active() is safe to use here because we're holding
2630 if ((thread->looper & BINDER_LOOPER_STATE_POLL) &&
2631 waitqueue_active(&thread->wait)) {
2632 wake_up_poll(&thread->wait, POLLHUP | POLLFREE);
2636 * This is needed to avoid races between wake_up_poll() above and
2637 * and ep_remove_waitqueue() called for other reasons (eg the epoll file
2638 * descriptor being closed); ep_remove_waitqueue() holds an RCU read
2639 * lock, so we can be sure it's done after calling synchronize_rcu().
2641 if (thread->looper & BINDER_LOOPER_STATE_POLL)
2645 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
2646 binder_release_work(&thread->todo);
2648 binder_stats_deleted(BINDER_STAT_THREAD);
2649 return active_transactions;
2652 static unsigned int binder_poll(struct file *filp,
2653 struct poll_table_struct *wait)
2655 struct binder_proc *proc = filp->private_data;
2656 struct binder_thread *thread = NULL;
2657 int wait_for_proc_work;
2659 binder_lock(__func__);
2661 thread = binder_get_thread(proc);
2663 binder_unlock(__func__);
2667 thread->looper |= BINDER_LOOPER_STATE_POLL;
2669 wait_for_proc_work = thread->transaction_stack == NULL &&
2670 list_empty(&thread->todo) && thread->return_error == BR_OK;
2672 binder_unlock(__func__);
2674 if (wait_for_proc_work) {
2675 if (binder_has_proc_work(proc, thread))
2677 poll_wait(filp, &proc->wait, wait);
2678 if (binder_has_proc_work(proc, thread))
2681 if (binder_has_thread_work(thread))
2683 poll_wait(filp, &thread->wait, wait);
2684 if (binder_has_thread_work(thread))
2690 static int binder_ioctl_write_read(struct file *filp,
2691 unsigned int cmd, unsigned long arg,
2692 struct binder_thread *thread)
2695 struct binder_proc *proc = filp->private_data;
2696 unsigned int size = _IOC_SIZE(cmd);
2697 void __user *ubuf = (void __user *)arg;
2698 struct binder_write_read bwr;
2700 if (size != sizeof(struct binder_write_read)) {
2704 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
2708 binder_debug(BINDER_DEBUG_READ_WRITE,
2709 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
2710 proc->pid, thread->pid,
2711 (u64)bwr.write_size, (u64)bwr.write_buffer,
2712 (u64)bwr.read_size, (u64)bwr.read_buffer);
2714 if (bwr.write_size > 0) {
2715 ret = binder_thread_write(proc, thread,
2718 &bwr.write_consumed);
2719 trace_binder_write_done(ret);
2721 bwr.read_consumed = 0;
2722 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2727 if (bwr.read_size > 0) {
2728 ret = binder_thread_read(proc, thread, bwr.read_buffer,
2731 filp->f_flags & O_NONBLOCK);
2732 trace_binder_read_done(ret);
2733 if (!list_empty(&proc->todo))
2734 wake_up_interruptible(&proc->wait);
2736 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2741 binder_debug(BINDER_DEBUG_READ_WRITE,
2742 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
2743 proc->pid, thread->pid,
2744 (u64)bwr.write_consumed, (u64)bwr.write_size,
2745 (u64)bwr.read_consumed, (u64)bwr.read_size);
2746 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
2754 static int binder_ioctl_set_ctx_mgr(struct file *filp)
2757 struct binder_proc *proc = filp->private_data;
2758 kuid_t curr_euid = current_euid();
2760 if (binder_context_mgr_node != NULL) {
2761 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
2765 ret = security_binder_set_context_mgr(proc->tsk);
2768 if (uid_valid(binder_context_mgr_uid)) {
2769 if (!uid_eq(binder_context_mgr_uid, curr_euid)) {
2770 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
2771 from_kuid(&init_user_ns, curr_euid),
2772 from_kuid(&init_user_ns,
2773 binder_context_mgr_uid));
2778 binder_context_mgr_uid = curr_euid;
2780 binder_context_mgr_node = binder_new_node(proc, 0, 0);
2781 if (binder_context_mgr_node == NULL) {
2785 binder_context_mgr_node->local_weak_refs++;
2786 binder_context_mgr_node->local_strong_refs++;
2787 binder_context_mgr_node->has_strong_ref = 1;
2788 binder_context_mgr_node->has_weak_ref = 1;
2793 static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2796 struct binder_proc *proc = filp->private_data;
2797 struct binder_thread *thread;
2798 unsigned int size = _IOC_SIZE(cmd);
2799 void __user *ubuf = (void __user *)arg;
2801 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
2802 proc->pid, current->pid, cmd, arg);*/
2804 trace_binder_ioctl(cmd, arg);
2806 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2810 binder_lock(__func__);
2811 thread = binder_get_thread(proc);
2812 if (thread == NULL) {
2818 case BINDER_WRITE_READ:
2819 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
2823 case BINDER_SET_MAX_THREADS:
2824 if (copy_from_user(&proc->max_threads, ubuf, sizeof(proc->max_threads))) {
2829 case BINDER_SET_CONTEXT_MGR:
2830 ret = binder_ioctl_set_ctx_mgr(filp);
2834 case BINDER_THREAD_EXIT:
2835 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
2836 proc->pid, thread->pid);
2837 binder_free_thread(proc, thread);
2840 case BINDER_VERSION: {
2841 struct binder_version __user *ver = ubuf;
2843 if (size != sizeof(struct binder_version)) {
2847 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
2848 &ver->protocol_version)) {
2861 thread->looper &= ~BINDER_LOOPER_STATE_NEED_RETURN;
2862 binder_unlock(__func__);
2863 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2864 if (ret && ret != -ERESTARTSYS)
2865 pr_info("%d:%d ioctl %x %lx returned %d\n", proc->pid, current->pid, cmd, arg, ret);
2867 trace_binder_ioctl_done(ret);
2871 static void binder_vma_open(struct vm_area_struct *vma)
2873 struct binder_proc *proc = vma->vm_private_data;
2875 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
2876 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
2877 proc->pid, vma->vm_start, vma->vm_end,
2878 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2879 (unsigned long)pgprot_val(vma->vm_page_prot));
2882 static void binder_vma_close(struct vm_area_struct *vma)
2884 struct binder_proc *proc = vma->vm_private_data;
2886 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
2887 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
2888 proc->pid, vma->vm_start, vma->vm_end,
2889 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2890 (unsigned long)pgprot_val(vma->vm_page_prot));
2892 proc->vma_vm_mm = NULL;
2893 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
2896 static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2898 return VM_FAULT_SIGBUS;
2901 static const struct vm_operations_struct binder_vm_ops = {
2902 .open = binder_vma_open,
2903 .close = binder_vma_close,
2904 .fault = binder_vm_fault,
2907 static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
2910 struct vm_struct *area;
2911 struct binder_proc *proc = filp->private_data;
2912 const char *failure_string;
2913 struct binder_buffer *buffer;
2915 if (proc->tsk != current->group_leader)
2918 if ((vma->vm_end - vma->vm_start) > SZ_4M)
2919 vma->vm_end = vma->vm_start + SZ_4M;
2921 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
2922 "binder_mmap: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
2923 proc->pid, vma->vm_start, vma->vm_end,
2924 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2925 (unsigned long)pgprot_val(vma->vm_page_prot));
2927 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
2929 failure_string = "bad vm_flags";
2932 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
2934 mutex_lock(&binder_mmap_lock);
2937 failure_string = "already mapped";
2938 goto err_already_mapped;
2941 area = get_vm_area(vma->vm_end - vma->vm_start, VM_IOREMAP);
2944 failure_string = "get_vm_area";
2945 goto err_get_vm_area_failed;
2947 proc->buffer = area->addr;
2948 proc->user_buffer_offset = vma->vm_start - (uintptr_t)proc->buffer;
2949 mutex_unlock(&binder_mmap_lock);
2951 #ifdef CONFIG_CPU_CACHE_VIPT
2952 if (cache_is_vipt_aliasing()) {
2953 while (CACHE_COLOUR((vma->vm_start ^ (uint32_t)proc->buffer))) {
2954 pr_info("binder_mmap: %d %lx-%lx maps %pK bad alignment\n", proc->pid, vma->vm_start, vma->vm_end, proc->buffer);
2955 vma->vm_start += PAGE_SIZE;
2959 proc->pages = kzalloc(sizeof(proc->pages[0]) * ((vma->vm_end - vma->vm_start) / PAGE_SIZE), GFP_KERNEL);
2960 if (proc->pages == NULL) {
2962 failure_string = "alloc page array";
2963 goto err_alloc_pages_failed;
2965 proc->buffer_size = vma->vm_end - vma->vm_start;
2967 vma->vm_ops = &binder_vm_ops;
2968 vma->vm_private_data = proc;
2970 if (binder_update_page_range(proc, 1, proc->buffer, proc->buffer + PAGE_SIZE, vma)) {
2972 failure_string = "alloc small buf";
2973 goto err_alloc_small_buf_failed;
2975 buffer = proc->buffer;
2976 INIT_LIST_HEAD(&proc->buffers);
2977 list_add(&buffer->entry, &proc->buffers);
2979 binder_insert_free_buffer(proc, buffer);
2980 proc->free_async_space = proc->buffer_size / 2;
2982 proc->files = get_files_struct(current);
2984 proc->vma_vm_mm = vma->vm_mm;
2986 /*pr_info("binder_mmap: %d %lx-%lx maps %p\n",
2987 proc->pid, vma->vm_start, vma->vm_end, proc->buffer);*/
2990 err_alloc_small_buf_failed:
2993 err_alloc_pages_failed:
2994 mutex_lock(&binder_mmap_lock);
2995 vfree(proc->buffer);
2996 proc->buffer = NULL;
2997 err_get_vm_area_failed:
2999 mutex_unlock(&binder_mmap_lock);
3001 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
3002 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
3006 static int binder_open(struct inode *nodp, struct file *filp)
3008 struct binder_proc *proc;
3010 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
3011 current->group_leader->pid, current->pid);
3013 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
3016 get_task_struct(current->group_leader);
3017 proc->tsk = current->group_leader;
3018 INIT_LIST_HEAD(&proc->todo);
3019 init_waitqueue_head(&proc->wait);
3020 proc->default_priority = task_nice(current);
3022 binder_lock(__func__);
3024 binder_stats_created(BINDER_STAT_PROC);
3025 hlist_add_head(&proc->proc_node, &binder_procs);
3026 proc->pid = current->group_leader->pid;
3027 INIT_LIST_HEAD(&proc->delivered_death);
3028 filp->private_data = proc;
3030 binder_unlock(__func__);
3032 if (binder_debugfs_dir_entry_proc) {
3035 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
3036 proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
3037 binder_debugfs_dir_entry_proc, proc, &binder_proc_fops);
3043 static int binder_flush(struct file *filp, fl_owner_t id)
3045 struct binder_proc *proc = filp->private_data;
3047 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
3052 static void binder_deferred_flush(struct binder_proc *proc)
3057 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
3058 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
3060 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
3061 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
3062 wake_up_interruptible(&thread->wait);
3066 wake_up_interruptible_all(&proc->wait);
3068 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
3069 "binder_flush: %d woke %d threads\n", proc->pid,
3073 static int binder_release(struct inode *nodp, struct file *filp)
3075 struct binder_proc *proc = filp->private_data;
3077 debugfs_remove(proc->debugfs_entry);
3078 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
3083 static int binder_node_release(struct binder_node *node, int refs)
3085 struct binder_ref *ref;
3088 list_del_init(&node->work.entry);
3089 binder_release_work(&node->async_todo);
3091 if (hlist_empty(&node->refs)) {
3093 binder_stats_deleted(BINDER_STAT_NODE);
3099 node->local_strong_refs = 0;
3100 node->local_weak_refs = 0;
3101 hlist_add_head(&node->dead_node, &binder_dead_nodes);
3103 hlist_for_each_entry(ref, &node->refs, node_entry) {
3111 if (list_empty(&ref->death->work.entry)) {
3112 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
3113 list_add_tail(&ref->death->work.entry,
3115 wake_up_interruptible(&ref->proc->wait);
3120 binder_debug(BINDER_DEBUG_DEAD_BINDER,
3121 "node %d now dead, refs %d, death %d\n",
3122 node->debug_id, refs, death);
3127 static void binder_deferred_release(struct binder_proc *proc)
3129 struct binder_transaction *t;
3131 int threads, nodes, incoming_refs, outgoing_refs, buffers,
3132 active_transactions, page_count;
3135 BUG_ON(proc->files);
3137 hlist_del(&proc->proc_node);
3139 if (binder_context_mgr_node && binder_context_mgr_node->proc == proc) {
3140 binder_debug(BINDER_DEBUG_DEAD_BINDER,
3141 "%s: %d context_mgr_node gone\n",
3142 __func__, proc->pid);
3143 binder_context_mgr_node = NULL;
3147 active_transactions = 0;
3148 while ((n = rb_first(&proc->threads))) {
3149 struct binder_thread *thread;
3151 thread = rb_entry(n, struct binder_thread, rb_node);
3153 active_transactions += binder_free_thread(proc, thread);
3158 while ((n = rb_first(&proc->nodes))) {
3159 struct binder_node *node;
3161 node = rb_entry(n, struct binder_node, rb_node);
3163 rb_erase(&node->rb_node, &proc->nodes);
3164 incoming_refs = binder_node_release(node, incoming_refs);
3168 while ((n = rb_first(&proc->refs_by_desc))) {
3169 struct binder_ref *ref;
3171 ref = rb_entry(n, struct binder_ref, rb_node_desc);
3173 binder_delete_ref(ref);
3176 binder_release_work(&proc->todo);
3177 binder_release_work(&proc->delivered_death);
3180 while ((n = rb_first(&proc->allocated_buffers))) {
3181 struct binder_buffer *buffer;
3183 buffer = rb_entry(n, struct binder_buffer, rb_node);
3185 t = buffer->transaction;
3188 buffer->transaction = NULL;
3189 pr_err("release proc %d, transaction %d, not freed\n",
3190 proc->pid, t->debug_id);
3194 binder_free_buf(proc, buffer);
3198 binder_stats_deleted(BINDER_STAT_PROC);
3204 for (i = 0; i < proc->buffer_size / PAGE_SIZE; i++) {
3207 if (!proc->pages[i])
3210 page_addr = proc->buffer + i * PAGE_SIZE;
3211 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
3212 "%s: %d: page %d at %pK not freed\n",
3213 __func__, proc->pid, i, page_addr);
3214 unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
3215 __free_page(proc->pages[i]);
3219 vfree(proc->buffer);
3222 put_task_struct(proc->tsk);
3224 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
3225 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d, buffers %d, pages %d\n",
3226 __func__, proc->pid, threads, nodes, incoming_refs,
3227 outgoing_refs, active_transactions, buffers, page_count);
3232 static void binder_deferred_func(struct work_struct *work)
3234 struct binder_proc *proc;
3235 struct files_struct *files;
3240 binder_lock(__func__);
3241 mutex_lock(&binder_deferred_lock);
3242 if (!hlist_empty(&binder_deferred_list)) {
3243 proc = hlist_entry(binder_deferred_list.first,
3244 struct binder_proc, deferred_work_node);
3245 hlist_del_init(&proc->deferred_work_node);
3246 defer = proc->deferred_work;
3247 proc->deferred_work = 0;
3252 mutex_unlock(&binder_deferred_lock);
3255 if (defer & BINDER_DEFERRED_PUT_FILES) {
3256 files = proc->files;
3261 if (defer & BINDER_DEFERRED_FLUSH)
3262 binder_deferred_flush(proc);
3264 if (defer & BINDER_DEFERRED_RELEASE)
3265 binder_deferred_release(proc); /* frees proc */
3267 binder_unlock(__func__);
3269 put_files_struct(files);
3272 static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
3275 binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
3277 mutex_lock(&binder_deferred_lock);
3278 proc->deferred_work |= defer;
3279 if (hlist_unhashed(&proc->deferred_work_node)) {
3280 hlist_add_head(&proc->deferred_work_node,
3281 &binder_deferred_list);
3282 queue_work(binder_deferred_workqueue, &binder_deferred_work);
3284 mutex_unlock(&binder_deferred_lock);
3287 static void print_binder_transaction(struct seq_file *m, const char *prefix,
3288 struct binder_transaction *t)
3291 "%s %d: %pK from %d:%d to %d:%d code %x flags %x pri %ld r%d",
3292 prefix, t->debug_id, t,
3293 t->from ? t->from->proc->pid : 0,
3294 t->from ? t->from->pid : 0,
3295 t->to_proc ? t->to_proc->pid : 0,
3296 t->to_thread ? t->to_thread->pid : 0,
3297 t->code, t->flags, t->priority, t->need_reply);
3298 if (t->buffer == NULL) {
3299 seq_puts(m, " buffer free\n");
3302 if (t->buffer->target_node)
3303 seq_printf(m, " node %d",
3304 t->buffer->target_node->debug_id);
3305 seq_printf(m, " size %zd:%zd data %pK\n",
3306 t->buffer->data_size, t->buffer->offsets_size,
3310 static void print_binder_buffer(struct seq_file *m, const char *prefix,
3311 struct binder_buffer *buffer)
3313 seq_printf(m, "%s %d: %pK size %zd:%zd %s\n",
3314 prefix, buffer->debug_id, buffer->data,
3315 buffer->data_size, buffer->offsets_size,
3316 buffer->transaction ? "active" : "delivered");
3319 static void print_binder_work(struct seq_file *m, const char *prefix,
3320 const char *transaction_prefix,
3321 struct binder_work *w)
3323 struct binder_node *node;
3324 struct binder_transaction *t;
3327 case BINDER_WORK_TRANSACTION:
3328 t = container_of(w, struct binder_transaction, work);
3329 print_binder_transaction(m, transaction_prefix, t);
3331 case BINDER_WORK_TRANSACTION_COMPLETE:
3332 seq_printf(m, "%stransaction complete\n", prefix);
3334 case BINDER_WORK_NODE:
3335 node = container_of(w, struct binder_node, work);
3336 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
3337 prefix, node->debug_id,
3338 (u64)node->ptr, (u64)node->cookie);
3340 case BINDER_WORK_DEAD_BINDER:
3341 seq_printf(m, "%shas dead binder\n", prefix);
3343 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
3344 seq_printf(m, "%shas cleared dead binder\n", prefix);
3346 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
3347 seq_printf(m, "%shas cleared death notification\n", prefix);
3350 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
3355 static void print_binder_thread(struct seq_file *m,
3356 struct binder_thread *thread,
3359 struct binder_transaction *t;
3360 struct binder_work *w;
3361 size_t start_pos = m->count;
3364 seq_printf(m, " thread %d: l %02x\n", thread->pid, thread->looper);
3365 header_pos = m->count;
3366 t = thread->transaction_stack;
3368 if (t->from == thread) {
3369 print_binder_transaction(m,
3370 " outgoing transaction", t);
3372 } else if (t->to_thread == thread) {
3373 print_binder_transaction(m,
3374 " incoming transaction", t);
3377 print_binder_transaction(m, " bad transaction", t);
3381 list_for_each_entry(w, &thread->todo, entry) {
3382 print_binder_work(m, " ", " pending transaction", w);
3384 if (!print_always && m->count == header_pos)
3385 m->count = start_pos;
3388 static void print_binder_node(struct seq_file *m, struct binder_node *node)
3390 struct binder_ref *ref;
3391 struct binder_work *w;
3395 hlist_for_each_entry(ref, &node->refs, node_entry)
3398 seq_printf(m, " node %d: u%016llx c%016llx hs %d hw %d ls %d lw %d is %d iw %d",
3399 node->debug_id, (u64)node->ptr, (u64)node->cookie,
3400 node->has_strong_ref, node->has_weak_ref,
3401 node->local_strong_refs, node->local_weak_refs,
3402 node->internal_strong_refs, count);
3404 seq_puts(m, " proc");
3405 hlist_for_each_entry(ref, &node->refs, node_entry)
3406 seq_printf(m, " %d", ref->proc->pid);
3409 list_for_each_entry(w, &node->async_todo, entry)
3410 print_binder_work(m, " ",
3411 " pending async transaction", w);
3414 static void print_binder_ref(struct seq_file *m, struct binder_ref *ref)
3416 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
3417 ref->debug_id, ref->desc, ref->node->proc ? "" : "dead ",
3418 ref->node->debug_id, ref->strong, ref->weak, ref->death);
3421 static void print_binder_proc(struct seq_file *m,
3422 struct binder_proc *proc, int print_all)
3424 struct binder_work *w;
3426 size_t start_pos = m->count;
3429 seq_printf(m, "proc %d\n", proc->pid);
3430 header_pos = m->count;
3432 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3433 print_binder_thread(m, rb_entry(n, struct binder_thread,
3434 rb_node), print_all);
3435 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
3436 struct binder_node *node = rb_entry(n, struct binder_node,
3438 if (print_all || node->has_async_transaction)
3439 print_binder_node(m, node);
3442 for (n = rb_first(&proc->refs_by_desc);
3445 print_binder_ref(m, rb_entry(n, struct binder_ref,
3448 for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
3449 print_binder_buffer(m, " buffer",
3450 rb_entry(n, struct binder_buffer, rb_node));
3451 list_for_each_entry(w, &proc->todo, entry)
3452 print_binder_work(m, " ", " pending transaction", w);
3453 list_for_each_entry(w, &proc->delivered_death, entry) {
3454 seq_puts(m, " has delivered dead binder\n");
3457 if (!print_all && m->count == header_pos)
3458 m->count = start_pos;
3461 static const char * const binder_return_strings[] = {
3466 "BR_ACQUIRE_RESULT",
3468 "BR_TRANSACTION_COMPLETE",
3473 "BR_ATTEMPT_ACQUIRE",
3478 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
3482 static const char * const binder_command_strings[] = {
3485 "BC_ACQUIRE_RESULT",
3493 "BC_ATTEMPT_ACQUIRE",
3494 "BC_REGISTER_LOOPER",
3497 "BC_REQUEST_DEATH_NOTIFICATION",
3498 "BC_CLEAR_DEATH_NOTIFICATION",
3499 "BC_DEAD_BINDER_DONE"
3502 static const char * const binder_objstat_strings[] = {
3509 "transaction_complete"
3512 static void print_binder_stats(struct seq_file *m, const char *prefix,
3513 struct binder_stats *stats)
3517 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
3518 ARRAY_SIZE(binder_command_strings));
3519 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
3521 seq_printf(m, "%s%s: %d\n", prefix,
3522 binder_command_strings[i], stats->bc[i]);
3525 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
3526 ARRAY_SIZE(binder_return_strings));
3527 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
3529 seq_printf(m, "%s%s: %d\n", prefix,
3530 binder_return_strings[i], stats->br[i]);
3533 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
3534 ARRAY_SIZE(binder_objstat_strings));
3535 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
3536 ARRAY_SIZE(stats->obj_deleted));
3537 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
3538 if (stats->obj_created[i] || stats->obj_deleted[i])
3539 seq_printf(m, "%s%s: active %d total %d\n", prefix,
3540 binder_objstat_strings[i],
3541 stats->obj_created[i] - stats->obj_deleted[i],
3542 stats->obj_created[i]);
3546 static void print_binder_proc_stats(struct seq_file *m,
3547 struct binder_proc *proc)
3549 struct binder_work *w;
3551 int count, strong, weak;
3553 seq_printf(m, "proc %d\n", proc->pid);
3555 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3557 seq_printf(m, " threads: %d\n", count);
3558 seq_printf(m, " requested threads: %d+%d/%d\n"
3559 " ready threads %d\n"
3560 " free async space %zd\n", proc->requested_threads,
3561 proc->requested_threads_started, proc->max_threads,
3562 proc->ready_threads, proc->free_async_space);
3564 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
3566 seq_printf(m, " nodes: %d\n", count);
3570 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
3571 struct binder_ref *ref = rb_entry(n, struct binder_ref,
3574 strong += ref->strong;
3577 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
3580 for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
3582 seq_printf(m, " buffers: %d\n", count);
3585 list_for_each_entry(w, &proc->todo, entry) {
3587 case BINDER_WORK_TRANSACTION:
3594 seq_printf(m, " pending transactions: %d\n", count);
3596 print_binder_stats(m, " ", &proc->stats);
3600 static int binder_state_show(struct seq_file *m, void *unused)
3602 struct binder_proc *proc;
3603 struct binder_node *node;
3604 int do_lock = !binder_debug_no_lock;
3607 binder_lock(__func__);
3609 seq_puts(m, "binder state:\n");
3611 if (!hlist_empty(&binder_dead_nodes))
3612 seq_puts(m, "dead nodes:\n");
3613 hlist_for_each_entry(node, &binder_dead_nodes, dead_node)
3614 print_binder_node(m, node);
3616 hlist_for_each_entry(proc, &binder_procs, proc_node)
3617 print_binder_proc(m, proc, 1);
3619 binder_unlock(__func__);
3623 static int binder_stats_show(struct seq_file *m, void *unused)
3625 struct binder_proc *proc;
3626 int do_lock = !binder_debug_no_lock;
3629 binder_lock(__func__);
3631 seq_puts(m, "binder stats:\n");
3633 print_binder_stats(m, "", &binder_stats);
3635 hlist_for_each_entry(proc, &binder_procs, proc_node)
3636 print_binder_proc_stats(m, proc);
3638 binder_unlock(__func__);
3642 static int binder_transactions_show(struct seq_file *m, void *unused)
3644 struct binder_proc *proc;
3645 int do_lock = !binder_debug_no_lock;
3648 binder_lock(__func__);
3650 seq_puts(m, "binder transactions:\n");
3651 hlist_for_each_entry(proc, &binder_procs, proc_node)
3652 print_binder_proc(m, proc, 0);
3654 binder_unlock(__func__);
3658 static int binder_proc_show(struct seq_file *m, void *unused)
3660 struct binder_proc *proc = m->private;
3661 int do_lock = !binder_debug_no_lock;
3664 binder_lock(__func__);
3665 seq_puts(m, "binder proc state:\n");
3666 print_binder_proc(m, proc, 1);
3668 binder_unlock(__func__);
3672 static void print_binder_transaction_log_entry(struct seq_file *m,
3673 struct binder_transaction_log_entry *e)
3676 "%d: %s from %d:%d to %d:%d node %d handle %d size %d:%d\n",
3677 e->debug_id, (e->call_type == 2) ? "reply" :
3678 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
3679 e->from_thread, e->to_proc, e->to_thread, e->to_node,
3680 e->target_handle, e->data_size, e->offsets_size);
3683 static int binder_transaction_log_show(struct seq_file *m, void *unused)
3685 struct binder_transaction_log *log = m->private;
3689 for (i = log->next; i < ARRAY_SIZE(log->entry); i++)
3690 print_binder_transaction_log_entry(m, &log->entry[i]);
3692 for (i = 0; i < log->next; i++)
3693 print_binder_transaction_log_entry(m, &log->entry[i]);
3697 static const struct file_operations binder_fops = {
3698 .owner = THIS_MODULE,
3699 .poll = binder_poll,
3700 .unlocked_ioctl = binder_ioctl,
3701 .compat_ioctl = binder_ioctl,
3702 .mmap = binder_mmap,
3703 .open = binder_open,
3704 .flush = binder_flush,
3705 .release = binder_release,
3708 static struct miscdevice binder_miscdev = {
3709 .minor = MISC_DYNAMIC_MINOR,
3711 .fops = &binder_fops
3714 BINDER_DEBUG_ENTRY(state);
3715 BINDER_DEBUG_ENTRY(stats);
3716 BINDER_DEBUG_ENTRY(transactions);
3717 BINDER_DEBUG_ENTRY(transaction_log);
3719 static int __init binder_init(void)
3723 binder_deferred_workqueue = create_singlethread_workqueue("binder");
3724 if (!binder_deferred_workqueue)
3727 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
3728 if (binder_debugfs_dir_entry_root)
3729 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
3730 binder_debugfs_dir_entry_root);
3731 ret = misc_register(&binder_miscdev);
3732 if (binder_debugfs_dir_entry_root) {
3733 debugfs_create_file("state",
3735 binder_debugfs_dir_entry_root,
3737 &binder_state_fops);
3738 debugfs_create_file("stats",
3740 binder_debugfs_dir_entry_root,
3742 &binder_stats_fops);
3743 debugfs_create_file("transactions",
3745 binder_debugfs_dir_entry_root,
3747 &binder_transactions_fops);
3748 debugfs_create_file("transaction_log",
3750 binder_debugfs_dir_entry_root,
3751 &binder_transaction_log,
3752 &binder_transaction_log_fops);
3753 debugfs_create_file("failed_transaction_log",
3755 binder_debugfs_dir_entry_root,
3756 &binder_transaction_log_failed,
3757 &binder_transaction_log_fops);
3762 device_initcall(binder_init);
3764 #define CREATE_TRACE_POINTS
3765 #include "binder_trace.h"
3767 MODULE_LICENSE("GPL v2");