Linux 4.9.243
[linux/fpc-iii.git] / drivers / android / binder.c
blobf4c0b62959450055fdc655e59ebb18bf60d7d88a
1 /* binder.c
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>
24 #include <linux/fs.h>
25 #include <linux/list.h>
26 #include <linux/miscdevice.h>
27 #include <linux/mm.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
44 #endif
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;
63 #define BINDER_DEBUG_ENTRY(name) \
64 static int binder_##name##_open(struct inode *inode, struct file *file) \
65 { \
66 return single_open(file, binder_##name##_show, inode->i_private); \
67 } \
69 static const struct file_operations binder_##name##_fops = { \
70 .owner = THIS_MODULE, \
71 .open = binder_##name##_open, \
72 .read = seq_read, \
73 .llseek = seq_lseek, \
74 .release = single_release, \
77 static int binder_proc_show(struct seq_file *m, void *unused);
78 BINDER_DEBUG_ENTRY(proc);
80 /* This is only defined in include/asm-arm/sizes.h */
81 #ifndef SZ_1K
82 #define SZ_1K 0x400
83 #endif
85 #ifndef SZ_4M
86 #define SZ_4M 0x400000
87 #endif
89 #define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
91 #define BINDER_SMALL_BUF_SIZE (PAGE_SIZE * 64)
93 enum {
94 BINDER_DEBUG_USER_ERROR = 1U << 0,
95 BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1,
96 BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2,
97 BINDER_DEBUG_OPEN_CLOSE = 1U << 3,
98 BINDER_DEBUG_DEAD_BINDER = 1U << 4,
99 BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5,
100 BINDER_DEBUG_READ_WRITE = 1U << 6,
101 BINDER_DEBUG_USER_REFS = 1U << 7,
102 BINDER_DEBUG_THREADS = 1U << 8,
103 BINDER_DEBUG_TRANSACTION = 1U << 9,
104 BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10,
105 BINDER_DEBUG_FREE_BUFFER = 1U << 11,
106 BINDER_DEBUG_INTERNAL_REFS = 1U << 12,
107 BINDER_DEBUG_BUFFER_ALLOC = 1U << 13,
108 BINDER_DEBUG_PRIORITY_CAP = 1U << 14,
109 BINDER_DEBUG_BUFFER_ALLOC_ASYNC = 1U << 15,
111 static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR |
112 BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION;
113 module_param_named(debug_mask, binder_debug_mask, uint, S_IWUSR | S_IRUGO);
115 static bool binder_debug_no_lock;
116 module_param_named(proc_no_lock, binder_debug_no_lock, bool, S_IWUSR | S_IRUGO);
118 static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
119 static int binder_stop_on_user_error;
121 static int binder_set_stop_on_user_error(const char *val,
122 struct kernel_param *kp)
124 int ret;
126 ret = param_set_int(val, kp);
127 if (binder_stop_on_user_error < 2)
128 wake_up(&binder_user_error_wait);
129 return ret;
131 module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
132 param_get_int, &binder_stop_on_user_error, S_IWUSR | S_IRUGO);
134 #define binder_debug(mask, x...) \
135 do { \
136 if (binder_debug_mask & mask) \
137 pr_info(x); \
138 } while (0)
140 #define binder_user_error(x...) \
141 do { \
142 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
143 pr_info(x); \
144 if (binder_stop_on_user_error) \
145 binder_stop_on_user_error = 2; \
146 } while (0)
148 enum binder_stat_types {
149 BINDER_STAT_PROC,
150 BINDER_STAT_THREAD,
151 BINDER_STAT_NODE,
152 BINDER_STAT_REF,
153 BINDER_STAT_DEATH,
154 BINDER_STAT_TRANSACTION,
155 BINDER_STAT_TRANSACTION_COMPLETE,
156 BINDER_STAT_COUNT
159 struct binder_stats {
160 int br[_IOC_NR(BR_FAILED_REPLY) + 1];
161 int bc[_IOC_NR(BC_DEAD_BINDER_DONE) + 1];
162 int obj_created[BINDER_STAT_COUNT];
163 int obj_deleted[BINDER_STAT_COUNT];
166 static struct binder_stats binder_stats;
168 static inline void binder_stats_deleted(enum binder_stat_types type)
170 binder_stats.obj_deleted[type]++;
173 static inline void binder_stats_created(enum binder_stat_types type)
175 binder_stats.obj_created[type]++;
178 struct binder_transaction_log_entry {
179 int debug_id;
180 int call_type;
181 int from_proc;
182 int from_thread;
183 int target_handle;
184 int to_proc;
185 int to_thread;
186 int to_node;
187 int data_size;
188 int offsets_size;
190 struct binder_transaction_log {
191 int next;
192 int full;
193 struct binder_transaction_log_entry entry[32];
195 static struct binder_transaction_log binder_transaction_log;
196 static struct binder_transaction_log binder_transaction_log_failed;
198 static struct binder_transaction_log_entry *binder_transaction_log_add(
199 struct binder_transaction_log *log)
201 struct binder_transaction_log_entry *e;
203 e = &log->entry[log->next];
204 memset(e, 0, sizeof(*e));
205 log->next++;
206 if (log->next == ARRAY_SIZE(log->entry)) {
207 log->next = 0;
208 log->full = 1;
210 return e;
213 struct binder_work {
214 struct list_head entry;
215 enum {
216 BINDER_WORK_TRANSACTION = 1,
217 BINDER_WORK_TRANSACTION_COMPLETE,
218 BINDER_WORK_NODE,
219 BINDER_WORK_DEAD_BINDER,
220 BINDER_WORK_DEAD_BINDER_AND_CLEAR,
221 BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
222 } type;
225 struct binder_node {
226 int debug_id;
227 struct binder_work work;
228 union {
229 struct rb_node rb_node;
230 struct hlist_node dead_node;
232 struct binder_proc *proc;
233 struct hlist_head refs;
234 int internal_strong_refs;
235 int local_weak_refs;
236 int local_strong_refs;
237 binder_uintptr_t ptr;
238 binder_uintptr_t cookie;
239 unsigned has_strong_ref:1;
240 unsigned pending_strong_ref:1;
241 unsigned has_weak_ref:1;
242 unsigned pending_weak_ref:1;
243 unsigned has_async_transaction:1;
244 unsigned accept_fds:1;
245 unsigned min_priority:8;
246 struct list_head async_todo;
249 struct binder_ref_death {
250 struct binder_work work;
251 binder_uintptr_t cookie;
254 struct binder_ref {
255 /* Lookups needed: */
256 /* node + proc => ref (transaction) */
257 /* desc + proc => ref (transaction, inc/dec ref) */
258 /* node => refs + procs (proc exit) */
259 int debug_id;
260 struct rb_node rb_node_desc;
261 struct rb_node rb_node_node;
262 struct hlist_node node_entry;
263 struct binder_proc *proc;
264 struct binder_node *node;
265 uint32_t desc;
266 int strong;
267 int weak;
268 struct binder_ref_death *death;
271 struct binder_buffer {
272 struct list_head entry; /* free and allocated entries by address */
273 struct rb_node rb_node; /* free entry by size or allocated entry */
274 /* by address */
275 unsigned free:1;
276 unsigned allow_user_free:1;
277 unsigned async_transaction:1;
278 unsigned debug_id:29;
280 struct binder_transaction *transaction;
282 struct binder_node *target_node;
283 size_t data_size;
284 size_t offsets_size;
285 uint8_t data[0];
288 enum binder_deferred_state {
289 BINDER_DEFERRED_PUT_FILES = 0x01,
290 BINDER_DEFERRED_FLUSH = 0x02,
291 BINDER_DEFERRED_RELEASE = 0x04,
294 struct binder_proc {
295 struct hlist_node proc_node;
296 struct rb_root threads;
297 struct rb_root nodes;
298 struct rb_root refs_by_desc;
299 struct rb_root refs_by_node;
300 int pid;
301 struct vm_area_struct *vma;
302 struct mm_struct *vma_vm_mm;
303 struct task_struct *tsk;
304 struct files_struct *files;
305 struct mutex files_lock;
306 struct hlist_node deferred_work_node;
307 int deferred_work;
308 void *buffer;
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;
316 struct page **pages;
317 size_t buffer_size;
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;
323 int max_threads;
324 int requested_threads;
325 int requested_threads_started;
326 int ready_threads;
327 long default_priority;
328 struct dentry *debugfs_entry;
331 enum {
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;
344 int pid;
345 int looper;
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 {
357 int debug_id;
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;
368 unsigned int code;
369 unsigned int flags;
370 long priority;
371 long saved_priority;
372 kuid_t sender_euid;
375 static void
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 unsigned long rlim_cur;
381 unsigned long irqs;
382 int ret;
384 mutex_lock(&proc->files_lock);
385 if (proc->files == NULL) {
386 ret = -ESRCH;
387 goto err;
389 if (!lock_task_sighand(proc->tsk, &irqs)) {
390 ret = -EMFILE;
391 goto err;
393 rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
394 unlock_task_sighand(proc->tsk, &irqs);
396 ret = __alloc_fd(proc->files, 0, rlim_cur, flags);
397 err:
398 mutex_unlock(&proc->files_lock);
399 return ret;
403 * copied from fd_install
405 static void task_fd_install(
406 struct binder_proc *proc, unsigned int fd, struct file *file)
408 mutex_lock(&proc->files_lock);
409 if (proc->files)
410 __fd_install(proc->files, fd, file);
411 mutex_unlock(&proc->files_lock);
415 * copied from sys_close
417 static long task_close_fd(struct binder_proc *proc, unsigned int fd)
419 int retval;
421 mutex_lock(&proc->files_lock);
422 if (proc->files == NULL) {
423 retval = -ESRCH;
424 goto err;
426 retval = __close_fd(proc->files, fd);
427 /* can't restart close syscall because file table entry was cleared */
428 if (unlikely(retval == -ERESTARTSYS ||
429 retval == -ERESTARTNOINTR ||
430 retval == -ERESTARTNOHAND ||
431 retval == -ERESTART_RESTARTBLOCK))
432 retval = -EINTR;
433 err:
434 mutex_unlock(&proc->files_lock);
435 return retval;
438 static inline void binder_lock(const char *tag)
440 trace_binder_lock(tag);
441 mutex_lock(&binder_main_lock);
442 trace_binder_locked(tag);
445 static inline void binder_unlock(const char *tag)
447 trace_binder_unlock(tag);
448 mutex_unlock(&binder_main_lock);
451 static void binder_set_nice(long nice)
453 long min_nice;
455 if (can_nice(current, nice)) {
456 set_user_nice(current, nice);
457 return;
459 min_nice = rlimit_to_nice(current->signal->rlim[RLIMIT_NICE].rlim_cur);
460 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
461 "%d: nice value %ld not allowed use %ld instead\n",
462 current->pid, nice, min_nice);
463 set_user_nice(current, min_nice);
464 if (min_nice <= MAX_NICE)
465 return;
466 binder_user_error("%d RLIMIT_NICE not set\n", current->pid);
469 static size_t binder_buffer_size(struct binder_proc *proc,
470 struct binder_buffer *buffer)
472 if (list_is_last(&buffer->entry, &proc->buffers))
473 return proc->buffer + proc->buffer_size - (void *)buffer->data;
474 return (size_t)list_entry(buffer->entry.next,
475 struct binder_buffer, entry) - (size_t)buffer->data;
478 static void binder_insert_free_buffer(struct binder_proc *proc,
479 struct binder_buffer *new_buffer)
481 struct rb_node **p = &proc->free_buffers.rb_node;
482 struct rb_node *parent = NULL;
483 struct binder_buffer *buffer;
484 size_t buffer_size;
485 size_t new_buffer_size;
487 BUG_ON(!new_buffer->free);
489 new_buffer_size = binder_buffer_size(proc, new_buffer);
491 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
492 "%d: add free buffer, size %zd, at %pK\n",
493 proc->pid, new_buffer_size, new_buffer);
495 while (*p) {
496 parent = *p;
497 buffer = rb_entry(parent, struct binder_buffer, rb_node);
498 BUG_ON(!buffer->free);
500 buffer_size = binder_buffer_size(proc, buffer);
502 if (new_buffer_size < buffer_size)
503 p = &parent->rb_left;
504 else
505 p = &parent->rb_right;
507 rb_link_node(&new_buffer->rb_node, parent, p);
508 rb_insert_color(&new_buffer->rb_node, &proc->free_buffers);
511 static void binder_insert_allocated_buffer(struct binder_proc *proc,
512 struct binder_buffer *new_buffer)
514 struct rb_node **p = &proc->allocated_buffers.rb_node;
515 struct rb_node *parent = NULL;
516 struct binder_buffer *buffer;
518 BUG_ON(new_buffer->free);
520 while (*p) {
521 parent = *p;
522 buffer = rb_entry(parent, struct binder_buffer, rb_node);
523 BUG_ON(buffer->free);
525 if (new_buffer < buffer)
526 p = &parent->rb_left;
527 else if (new_buffer > buffer)
528 p = &parent->rb_right;
529 else
530 BUG();
532 rb_link_node(&new_buffer->rb_node, parent, p);
533 rb_insert_color(&new_buffer->rb_node, &proc->allocated_buffers);
536 static struct binder_buffer *binder_buffer_lookup(struct binder_proc *proc,
537 uintptr_t user_ptr)
539 struct rb_node *n = proc->allocated_buffers.rb_node;
540 struct binder_buffer *buffer;
541 struct binder_buffer *kern_ptr;
543 kern_ptr = (struct binder_buffer *)(user_ptr - proc->user_buffer_offset
544 - offsetof(struct binder_buffer, data));
546 while (n) {
547 buffer = rb_entry(n, struct binder_buffer, rb_node);
548 BUG_ON(buffer->free);
550 if (kern_ptr < buffer)
551 n = n->rb_left;
552 else if (kern_ptr > buffer)
553 n = n->rb_right;
554 else
555 return buffer;
557 return NULL;
560 static int binder_update_page_range(struct binder_proc *proc, int allocate,
561 void *start, void *end,
562 struct vm_area_struct *vma)
564 void *page_addr;
565 unsigned long user_page_addr;
566 struct page **page;
567 struct mm_struct *mm;
569 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
570 "%d: %s pages %pK-%pK\n", proc->pid,
571 allocate ? "allocate" : "free", start, end);
573 if (end <= start)
574 return 0;
576 trace_binder_update_page_range(proc, allocate, start, end);
578 if (vma)
579 mm = NULL;
580 else
581 mm = get_task_mm(proc->tsk);
583 if (mm) {
584 down_write(&mm->mmap_sem);
585 if (!mmget_still_valid(mm)) {
586 if (allocate == 0)
587 goto free_range;
588 goto err_no_vma;
591 vma = proc->vma;
592 if (vma && mm != proc->vma_vm_mm) {
593 pr_err("%d: vma mm and task mm mismatch\n",
594 proc->pid);
595 vma = NULL;
599 if (allocate == 0)
600 goto free_range;
602 if (vma == NULL) {
603 pr_err("%d: binder_alloc_buf failed to map pages in userspace, no vma\n",
604 proc->pid);
605 goto err_no_vma;
608 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
609 int ret;
611 page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
613 BUG_ON(*page);
614 *page = alloc_page(GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
615 if (*page == NULL) {
616 pr_err("%d: binder_alloc_buf failed for page at %pK\n",
617 proc->pid, page_addr);
618 goto err_alloc_page_failed;
620 ret = map_kernel_range_noflush((unsigned long)page_addr,
621 PAGE_SIZE, PAGE_KERNEL, page);
622 flush_cache_vmap((unsigned long)page_addr,
623 (unsigned long)page_addr + PAGE_SIZE);
624 if (ret != 1) {
625 pr_err("%d: binder_alloc_buf failed to map page at %pK in kernel\n",
626 proc->pid, page_addr);
627 goto err_map_kernel_failed;
629 user_page_addr =
630 (uintptr_t)page_addr + proc->user_buffer_offset;
631 ret = vm_insert_page(vma, user_page_addr, page[0]);
632 if (ret) {
633 pr_err("%d: binder_alloc_buf failed to map page at %lx in userspace\n",
634 proc->pid, user_page_addr);
635 goto err_vm_insert_page_failed;
637 /* vm_insert_page does not seem to increment the refcount */
639 if (mm) {
640 up_write(&mm->mmap_sem);
641 mmput(mm);
643 return 0;
645 free_range:
646 for (page_addr = end - PAGE_SIZE; page_addr >= start;
647 page_addr -= PAGE_SIZE) {
648 page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
649 if (vma)
650 zap_page_range(vma, (uintptr_t)page_addr +
651 proc->user_buffer_offset, PAGE_SIZE, NULL);
652 err_vm_insert_page_failed:
653 unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
654 err_map_kernel_failed:
655 __free_page(*page);
656 *page = NULL;
657 err_alloc_page_failed:
660 err_no_vma:
661 if (mm) {
662 up_write(&mm->mmap_sem);
663 mmput(mm);
665 return -ENOMEM;
668 static struct binder_buffer *binder_alloc_buf(struct binder_proc *proc,
669 size_t data_size,
670 size_t offsets_size, int is_async)
672 struct rb_node *n = proc->free_buffers.rb_node;
673 struct binder_buffer *buffer;
674 size_t buffer_size;
675 struct rb_node *best_fit = NULL;
676 void *has_page_addr;
677 void *end_page_addr;
678 size_t size;
680 if (proc->vma == NULL) {
681 pr_err("%d: binder_alloc_buf, no vma\n",
682 proc->pid);
683 return NULL;
686 size = ALIGN(data_size, sizeof(void *)) +
687 ALIGN(offsets_size, sizeof(void *));
689 if (size < data_size || size < offsets_size) {
690 binder_user_error("%d: got transaction with invalid size %zd-%zd\n",
691 proc->pid, data_size, offsets_size);
692 return NULL;
695 if (is_async &&
696 proc->free_async_space < size + sizeof(struct binder_buffer)) {
697 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
698 "%d: binder_alloc_buf size %zd failed, no async space left\n",
699 proc->pid, size);
700 return NULL;
703 while (n) {
704 buffer = rb_entry(n, struct binder_buffer, rb_node);
705 BUG_ON(!buffer->free);
706 buffer_size = binder_buffer_size(proc, buffer);
708 if (size < buffer_size) {
709 best_fit = n;
710 n = n->rb_left;
711 } else if (size > buffer_size)
712 n = n->rb_right;
713 else {
714 best_fit = n;
715 break;
718 if (best_fit == NULL) {
719 pr_err("%d: binder_alloc_buf size %zd failed, no address space\n",
720 proc->pid, size);
721 return NULL;
723 if (n == NULL) {
724 buffer = rb_entry(best_fit, struct binder_buffer, rb_node);
725 buffer_size = binder_buffer_size(proc, buffer);
728 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
729 "%d: binder_alloc_buf size %zd got buffer %pK size %zd\n",
730 proc->pid, size, buffer, buffer_size);
732 has_page_addr =
733 (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK);
734 if (n == NULL) {
735 if (size + sizeof(struct binder_buffer) + 4 >= buffer_size)
736 buffer_size = size; /* no room for other buffers */
737 else
738 buffer_size = size + sizeof(struct binder_buffer);
740 end_page_addr =
741 (void *)PAGE_ALIGN((uintptr_t)buffer->data + buffer_size);
742 if (end_page_addr > has_page_addr)
743 end_page_addr = has_page_addr;
744 if (binder_update_page_range(proc, 1,
745 (void *)PAGE_ALIGN((uintptr_t)buffer->data), end_page_addr, NULL))
746 return NULL;
748 rb_erase(best_fit, &proc->free_buffers);
749 buffer->free = 0;
750 binder_insert_allocated_buffer(proc, buffer);
751 if (buffer_size != size) {
752 struct binder_buffer *new_buffer = (void *)buffer->data + size;
754 list_add(&new_buffer->entry, &buffer->entry);
755 new_buffer->free = 1;
756 binder_insert_free_buffer(proc, new_buffer);
758 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
759 "%d: binder_alloc_buf size %zd got %pK\n",
760 proc->pid, size, buffer);
761 buffer->data_size = data_size;
762 buffer->offsets_size = offsets_size;
763 buffer->async_transaction = is_async;
764 if (is_async) {
765 proc->free_async_space -= size + sizeof(struct binder_buffer);
766 binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
767 "%d: binder_alloc_buf size %zd async free %zd\n",
768 proc->pid, size, proc->free_async_space);
771 return buffer;
774 static void *buffer_start_page(struct binder_buffer *buffer)
776 return (void *)((uintptr_t)buffer & PAGE_MASK);
779 static void *buffer_end_page(struct binder_buffer *buffer)
781 return (void *)(((uintptr_t)(buffer + 1) - 1) & PAGE_MASK);
784 static void binder_delete_free_buffer(struct binder_proc *proc,
785 struct binder_buffer *buffer)
787 struct binder_buffer *prev, *next = NULL;
788 int free_page_end = 1;
789 int free_page_start = 1;
791 BUG_ON(proc->buffers.next == &buffer->entry);
792 prev = list_entry(buffer->entry.prev, struct binder_buffer, entry);
793 BUG_ON(!prev->free);
794 if (buffer_end_page(prev) == buffer_start_page(buffer)) {
795 free_page_start = 0;
796 if (buffer_end_page(prev) == buffer_end_page(buffer))
797 free_page_end = 0;
798 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
799 "%d: merge free, buffer %pK share page with %pK\n",
800 proc->pid, buffer, prev);
803 if (!list_is_last(&buffer->entry, &proc->buffers)) {
804 next = list_entry(buffer->entry.next,
805 struct binder_buffer, entry);
806 if (buffer_start_page(next) == buffer_end_page(buffer)) {
807 free_page_end = 0;
808 if (buffer_start_page(next) ==
809 buffer_start_page(buffer))
810 free_page_start = 0;
811 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
812 "%d: merge free, buffer %pK share page with %pK\n",
813 proc->pid, buffer, prev);
816 list_del(&buffer->entry);
817 if (free_page_start || free_page_end) {
818 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
819 "%d: merge free, buffer %pK do not share page%s%s with %pK or %pK\n",
820 proc->pid, buffer, free_page_start ? "" : " end",
821 free_page_end ? "" : " start", prev, next);
822 binder_update_page_range(proc, 0, free_page_start ?
823 buffer_start_page(buffer) : buffer_end_page(buffer),
824 (free_page_end ? buffer_end_page(buffer) :
825 buffer_start_page(buffer)) + PAGE_SIZE, NULL);
829 static void binder_free_buf(struct binder_proc *proc,
830 struct binder_buffer *buffer)
832 size_t size, buffer_size;
834 buffer_size = binder_buffer_size(proc, buffer);
836 size = ALIGN(buffer->data_size, sizeof(void *)) +
837 ALIGN(buffer->offsets_size, sizeof(void *));
839 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
840 "%d: binder_free_buf %pK size %zd buffer_size %zd\n",
841 proc->pid, buffer, size, buffer_size);
843 BUG_ON(buffer->free);
844 BUG_ON(size > buffer_size);
845 BUG_ON(buffer->transaction != NULL);
846 BUG_ON((void *)buffer < proc->buffer);
847 BUG_ON((void *)buffer > proc->buffer + proc->buffer_size);
849 if (buffer->async_transaction) {
850 proc->free_async_space += size + sizeof(struct binder_buffer);
852 binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
853 "%d: binder_free_buf size %zd async free %zd\n",
854 proc->pid, size, proc->free_async_space);
857 binder_update_page_range(proc, 0,
858 (void *)PAGE_ALIGN((uintptr_t)buffer->data),
859 (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK),
860 NULL);
861 rb_erase(&buffer->rb_node, &proc->allocated_buffers);
862 buffer->free = 1;
863 if (!list_is_last(&buffer->entry, &proc->buffers)) {
864 struct binder_buffer *next = list_entry(buffer->entry.next,
865 struct binder_buffer, entry);
867 if (next->free) {
868 rb_erase(&next->rb_node, &proc->free_buffers);
869 binder_delete_free_buffer(proc, next);
872 if (proc->buffers.next != &buffer->entry) {
873 struct binder_buffer *prev = list_entry(buffer->entry.prev,
874 struct binder_buffer, entry);
876 if (prev->free) {
877 binder_delete_free_buffer(proc, buffer);
878 rb_erase(&prev->rb_node, &proc->free_buffers);
879 buffer = prev;
882 binder_insert_free_buffer(proc, buffer);
885 static struct binder_node *binder_get_node(struct binder_proc *proc,
886 binder_uintptr_t ptr)
888 struct rb_node *n = proc->nodes.rb_node;
889 struct binder_node *node;
891 while (n) {
892 node = rb_entry(n, struct binder_node, rb_node);
894 if (ptr < node->ptr)
895 n = n->rb_left;
896 else if (ptr > node->ptr)
897 n = n->rb_right;
898 else
899 return node;
901 return NULL;
904 static struct binder_node *binder_new_node(struct binder_proc *proc,
905 binder_uintptr_t ptr,
906 binder_uintptr_t cookie)
908 struct rb_node **p = &proc->nodes.rb_node;
909 struct rb_node *parent = NULL;
910 struct binder_node *node;
912 while (*p) {
913 parent = *p;
914 node = rb_entry(parent, struct binder_node, rb_node);
916 if (ptr < node->ptr)
917 p = &(*p)->rb_left;
918 else if (ptr > node->ptr)
919 p = &(*p)->rb_right;
920 else
921 return NULL;
924 node = kzalloc(sizeof(*node), GFP_KERNEL);
925 if (node == NULL)
926 return NULL;
927 binder_stats_created(BINDER_STAT_NODE);
928 rb_link_node(&node->rb_node, parent, p);
929 rb_insert_color(&node->rb_node, &proc->nodes);
930 node->debug_id = ++binder_last_id;
931 node->proc = proc;
932 node->ptr = ptr;
933 node->cookie = cookie;
934 node->work.type = BINDER_WORK_NODE;
935 INIT_LIST_HEAD(&node->work.entry);
936 INIT_LIST_HEAD(&node->async_todo);
937 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
938 "%d:%d node %d u%016llx c%016llx created\n",
939 proc->pid, current->pid, node->debug_id,
940 (u64)node->ptr, (u64)node->cookie);
941 return node;
944 static int binder_inc_node(struct binder_node *node, int strong, int internal,
945 struct list_head *target_list)
947 if (strong) {
948 if (internal) {
949 if (target_list == NULL &&
950 node->internal_strong_refs == 0 &&
951 !(node == binder_context_mgr_node &&
952 node->has_strong_ref)) {
953 pr_err("invalid inc strong node for %d\n",
954 node->debug_id);
955 return -EINVAL;
957 node->internal_strong_refs++;
958 } else
959 node->local_strong_refs++;
960 if (!node->has_strong_ref && target_list) {
961 list_del_init(&node->work.entry);
962 list_add_tail(&node->work.entry, target_list);
964 } else {
965 if (!internal)
966 node->local_weak_refs++;
967 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
968 if (target_list == NULL) {
969 pr_err("invalid inc weak node for %d\n",
970 node->debug_id);
971 return -EINVAL;
973 list_add_tail(&node->work.entry, target_list);
976 return 0;
979 static int binder_dec_node(struct binder_node *node, int strong, int internal)
981 if (strong) {
982 if (internal)
983 node->internal_strong_refs--;
984 else
985 node->local_strong_refs--;
986 if (node->local_strong_refs || node->internal_strong_refs)
987 return 0;
988 } else {
989 if (!internal)
990 node->local_weak_refs--;
991 if (node->local_weak_refs || !hlist_empty(&node->refs))
992 return 0;
994 if (node->proc && (node->has_strong_ref || node->has_weak_ref)) {
995 if (list_empty(&node->work.entry)) {
996 list_add_tail(&node->work.entry, &node->proc->todo);
997 wake_up_interruptible(&node->proc->wait);
999 } else {
1000 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
1001 !node->local_weak_refs) {
1002 list_del_init(&node->work.entry);
1003 if (node->proc) {
1004 rb_erase(&node->rb_node, &node->proc->nodes);
1005 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1006 "refless node %d deleted\n",
1007 node->debug_id);
1008 } else {
1009 hlist_del(&node->dead_node);
1010 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1011 "dead node %d deleted\n",
1012 node->debug_id);
1014 kfree(node);
1015 binder_stats_deleted(BINDER_STAT_NODE);
1019 return 0;
1023 static struct binder_ref *binder_get_ref(struct binder_proc *proc,
1024 u32 desc, bool need_strong_ref)
1026 struct rb_node *n = proc->refs_by_desc.rb_node;
1027 struct binder_ref *ref;
1029 while (n) {
1030 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1032 if (desc < ref->desc) {
1033 n = n->rb_left;
1034 } else if (desc > ref->desc) {
1035 n = n->rb_right;
1036 } else if (need_strong_ref && !ref->strong) {
1037 binder_user_error("tried to use weak ref as strong ref\n");
1038 return NULL;
1039 } else {
1040 return ref;
1043 return NULL;
1046 static struct binder_ref *binder_get_ref_for_node(struct binder_proc *proc,
1047 struct binder_node *node)
1049 struct rb_node *n;
1050 struct rb_node **p = &proc->refs_by_node.rb_node;
1051 struct rb_node *parent = NULL;
1052 struct binder_ref *ref, *new_ref;
1054 while (*p) {
1055 parent = *p;
1056 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1058 if (node < ref->node)
1059 p = &(*p)->rb_left;
1060 else if (node > ref->node)
1061 p = &(*p)->rb_right;
1062 else
1063 return ref;
1065 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1066 if (new_ref == NULL)
1067 return NULL;
1068 binder_stats_created(BINDER_STAT_REF);
1069 new_ref->debug_id = ++binder_last_id;
1070 new_ref->proc = proc;
1071 new_ref->node = node;
1072 rb_link_node(&new_ref->rb_node_node, parent, p);
1073 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1075 new_ref->desc = (node == binder_context_mgr_node) ? 0 : 1;
1076 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1077 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1078 if (ref->desc > new_ref->desc)
1079 break;
1080 new_ref->desc = ref->desc + 1;
1083 p = &proc->refs_by_desc.rb_node;
1084 while (*p) {
1085 parent = *p;
1086 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1088 if (new_ref->desc < ref->desc)
1089 p = &(*p)->rb_left;
1090 else if (new_ref->desc > ref->desc)
1091 p = &(*p)->rb_right;
1092 else
1093 BUG();
1095 rb_link_node(&new_ref->rb_node_desc, parent, p);
1096 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
1097 if (node) {
1098 hlist_add_head(&new_ref->node_entry, &node->refs);
1100 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1101 "%d new ref %d desc %d for node %d\n",
1102 proc->pid, new_ref->debug_id, new_ref->desc,
1103 node->debug_id);
1104 } else {
1105 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1106 "%d new ref %d desc %d for dead node\n",
1107 proc->pid, new_ref->debug_id, new_ref->desc);
1109 return new_ref;
1112 static void binder_delete_ref(struct binder_ref *ref)
1114 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1115 "%d delete ref %d desc %d for node %d\n",
1116 ref->proc->pid, ref->debug_id, ref->desc,
1117 ref->node->debug_id);
1119 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1120 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
1121 if (ref->strong)
1122 binder_dec_node(ref->node, 1, 1);
1123 hlist_del(&ref->node_entry);
1124 binder_dec_node(ref->node, 0, 1);
1125 if (ref->death) {
1126 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1127 "%d delete ref %d desc %d has death notification\n",
1128 ref->proc->pid, ref->debug_id, ref->desc);
1129 list_del(&ref->death->work.entry);
1130 kfree(ref->death);
1131 binder_stats_deleted(BINDER_STAT_DEATH);
1133 kfree(ref);
1134 binder_stats_deleted(BINDER_STAT_REF);
1137 static int binder_inc_ref(struct binder_ref *ref, int strong,
1138 struct list_head *target_list)
1140 int ret;
1142 if (strong) {
1143 if (ref->strong == 0) {
1144 ret = binder_inc_node(ref->node, 1, 1, target_list);
1145 if (ret)
1146 return ret;
1148 ref->strong++;
1149 } else {
1150 if (ref->weak == 0) {
1151 ret = binder_inc_node(ref->node, 0, 1, target_list);
1152 if (ret)
1153 return ret;
1155 ref->weak++;
1157 return 0;
1161 static int binder_dec_ref(struct binder_ref *ref, int strong)
1163 if (strong) {
1164 if (ref->strong == 0) {
1165 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
1166 ref->proc->pid, ref->debug_id,
1167 ref->desc, ref->strong, ref->weak);
1168 return -EINVAL;
1170 ref->strong--;
1171 if (ref->strong == 0) {
1172 int ret;
1174 ret = binder_dec_node(ref->node, strong, 1);
1175 if (ret)
1176 return ret;
1178 } else {
1179 if (ref->weak == 0) {
1180 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
1181 ref->proc->pid, ref->debug_id,
1182 ref->desc, ref->strong, ref->weak);
1183 return -EINVAL;
1185 ref->weak--;
1187 if (ref->strong == 0 && ref->weak == 0)
1188 binder_delete_ref(ref);
1189 return 0;
1192 static void binder_pop_transaction(struct binder_thread *target_thread,
1193 struct binder_transaction *t)
1195 if (target_thread) {
1196 BUG_ON(target_thread->transaction_stack != t);
1197 BUG_ON(target_thread->transaction_stack->from != target_thread);
1198 target_thread->transaction_stack =
1199 target_thread->transaction_stack->from_parent;
1200 t->from = NULL;
1202 t->need_reply = 0;
1203 if (t->buffer)
1204 t->buffer->transaction = NULL;
1205 kfree(t);
1206 binder_stats_deleted(BINDER_STAT_TRANSACTION);
1209 static void binder_send_failed_reply(struct binder_transaction *t,
1210 uint32_t error_code)
1212 struct binder_thread *target_thread;
1213 struct binder_transaction *next;
1215 BUG_ON(t->flags & TF_ONE_WAY);
1216 while (1) {
1217 target_thread = t->from;
1218 if (target_thread) {
1219 if (target_thread->return_error != BR_OK &&
1220 target_thread->return_error2 == BR_OK) {
1221 target_thread->return_error2 =
1222 target_thread->return_error;
1223 target_thread->return_error = BR_OK;
1225 if (target_thread->return_error == BR_OK) {
1226 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1227 "send failed reply for transaction %d to %d:%d\n",
1228 t->debug_id,
1229 target_thread->proc->pid,
1230 target_thread->pid);
1232 binder_pop_transaction(target_thread, t);
1233 target_thread->return_error = error_code;
1234 wake_up_interruptible(&target_thread->wait);
1235 } else {
1236 pr_err("reply failed, target thread, %d:%d, has error code %d already\n",
1237 target_thread->proc->pid,
1238 target_thread->pid,
1239 target_thread->return_error);
1241 return;
1243 next = t->from_parent;
1245 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1246 "send failed reply for transaction %d, target dead\n",
1247 t->debug_id);
1249 binder_pop_transaction(target_thread, t);
1250 if (next == NULL) {
1251 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1252 "reply failed, no target thread at root\n");
1253 return;
1255 t = next;
1256 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1257 "reply failed, no target thread -- retry %d\n",
1258 t->debug_id);
1262 static void binder_transaction_buffer_release(struct binder_proc *proc,
1263 struct binder_buffer *buffer,
1264 binder_size_t *failed_at)
1266 binder_size_t *offp, *off_end;
1267 int debug_id = buffer->debug_id;
1269 binder_debug(BINDER_DEBUG_TRANSACTION,
1270 "%d buffer release %d, size %zd-%zd, failed at %pK\n",
1271 proc->pid, buffer->debug_id,
1272 buffer->data_size, buffer->offsets_size, failed_at);
1274 if (buffer->target_node)
1275 binder_dec_node(buffer->target_node, 1, 0);
1277 offp = (binder_size_t *)(buffer->data +
1278 ALIGN(buffer->data_size, sizeof(void *)));
1279 if (failed_at)
1280 off_end = failed_at;
1281 else
1282 off_end = (void *)offp + buffer->offsets_size;
1283 for (; offp < off_end; offp++) {
1284 struct flat_binder_object *fp;
1286 if (*offp > buffer->data_size - sizeof(*fp) ||
1287 buffer->data_size < sizeof(*fp) ||
1288 !IS_ALIGNED(*offp, sizeof(u32))) {
1289 pr_err("transaction release %d bad offset %lld, size %zd\n",
1290 debug_id, (u64)*offp, buffer->data_size);
1291 continue;
1293 fp = (struct flat_binder_object *)(buffer->data + *offp);
1294 switch (fp->type) {
1295 case BINDER_TYPE_BINDER:
1296 case BINDER_TYPE_WEAK_BINDER: {
1297 struct binder_node *node = binder_get_node(proc, fp->binder);
1299 if (node == NULL) {
1300 pr_err("transaction release %d bad node %016llx\n",
1301 debug_id, (u64)fp->binder);
1302 break;
1304 binder_debug(BINDER_DEBUG_TRANSACTION,
1305 " node %d u%016llx\n",
1306 node->debug_id, (u64)node->ptr);
1307 binder_dec_node(node, fp->type == BINDER_TYPE_BINDER, 0);
1308 } break;
1309 case BINDER_TYPE_HANDLE:
1310 case BINDER_TYPE_WEAK_HANDLE: {
1311 struct binder_ref *ref;
1313 ref = binder_get_ref(proc, fp->handle,
1314 fp->type == BINDER_TYPE_HANDLE);
1316 if (ref == NULL) {
1317 pr_err("transaction release %d bad handle %d\n",
1318 debug_id, fp->handle);
1319 break;
1321 binder_debug(BINDER_DEBUG_TRANSACTION,
1322 " ref %d desc %d (node %d)\n",
1323 ref->debug_id, ref->desc, ref->node->debug_id);
1324 binder_dec_ref(ref, fp->type == BINDER_TYPE_HANDLE);
1325 } break;
1327 case BINDER_TYPE_FD:
1328 binder_debug(BINDER_DEBUG_TRANSACTION,
1329 " fd %d\n", fp->handle);
1330 if (failed_at)
1331 task_close_fd(proc, fp->handle);
1332 break;
1334 default:
1335 pr_err("transaction release %d bad object type %x\n",
1336 debug_id, fp->type);
1337 break;
1342 static void binder_transaction(struct binder_proc *proc,
1343 struct binder_thread *thread,
1344 struct binder_transaction_data *tr, int reply)
1346 struct binder_transaction *t;
1347 struct binder_work *tcomplete;
1348 binder_size_t *offp, *off_end;
1349 binder_size_t off_min;
1350 struct binder_proc *target_proc;
1351 struct binder_thread *target_thread = NULL;
1352 struct binder_node *target_node = NULL;
1353 struct list_head *target_list;
1354 wait_queue_head_t *target_wait;
1355 struct binder_transaction *in_reply_to = NULL;
1356 struct binder_transaction_log_entry *e;
1357 uint32_t return_error;
1359 e = binder_transaction_log_add(&binder_transaction_log);
1360 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
1361 e->from_proc = proc->pid;
1362 e->from_thread = thread->pid;
1363 e->target_handle = tr->target.handle;
1364 e->data_size = tr->data_size;
1365 e->offsets_size = tr->offsets_size;
1367 if (reply) {
1368 in_reply_to = thread->transaction_stack;
1369 if (in_reply_to == NULL) {
1370 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
1371 proc->pid, thread->pid);
1372 return_error = BR_FAILED_REPLY;
1373 goto err_empty_call_stack;
1375 binder_set_nice(in_reply_to->saved_priority);
1376 if (in_reply_to->to_thread != thread) {
1377 binder_user_error("%d:%d got reply transaction with bad transaction stack, transaction %d has target %d:%d\n",
1378 proc->pid, thread->pid, in_reply_to->debug_id,
1379 in_reply_to->to_proc ?
1380 in_reply_to->to_proc->pid : 0,
1381 in_reply_to->to_thread ?
1382 in_reply_to->to_thread->pid : 0);
1383 return_error = BR_FAILED_REPLY;
1384 in_reply_to = NULL;
1385 goto err_bad_call_stack;
1387 thread->transaction_stack = in_reply_to->to_parent;
1388 target_thread = in_reply_to->from;
1389 if (target_thread == NULL) {
1390 return_error = BR_DEAD_REPLY;
1391 goto err_dead_binder;
1393 if (target_thread->transaction_stack != in_reply_to) {
1394 binder_user_error("%d:%d got reply transaction with bad target transaction stack %d, expected %d\n",
1395 proc->pid, thread->pid,
1396 target_thread->transaction_stack ?
1397 target_thread->transaction_stack->debug_id : 0,
1398 in_reply_to->debug_id);
1399 return_error = BR_FAILED_REPLY;
1400 in_reply_to = NULL;
1401 target_thread = NULL;
1402 goto err_dead_binder;
1404 target_proc = target_thread->proc;
1405 } else {
1406 if (tr->target.handle) {
1407 struct binder_ref *ref;
1409 ref = binder_get_ref(proc, tr->target.handle, true);
1410 if (ref == NULL) {
1411 binder_user_error("%d:%d got transaction to invalid handle\n",
1412 proc->pid, thread->pid);
1413 return_error = BR_FAILED_REPLY;
1414 goto err_invalid_target_handle;
1416 target_node = ref->node;
1417 } else {
1418 target_node = binder_context_mgr_node;
1419 if (target_node == NULL) {
1420 return_error = BR_DEAD_REPLY;
1421 goto err_no_context_mgr_node;
1424 e->to_node = target_node->debug_id;
1425 target_proc = target_node->proc;
1426 if (target_proc == NULL) {
1427 return_error = BR_DEAD_REPLY;
1428 goto err_dead_binder;
1430 if (WARN_ON(proc == target_proc)) {
1431 return_error = BR_FAILED_REPLY;
1432 goto err_invalid_target_handle;
1434 if (security_binder_transaction(proc->tsk,
1435 target_proc->tsk) < 0) {
1436 return_error = BR_FAILED_REPLY;
1437 goto err_invalid_target_handle;
1439 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
1440 struct binder_transaction *tmp;
1442 tmp = thread->transaction_stack;
1443 if (tmp->to_thread != thread) {
1444 binder_user_error("%d:%d got new transaction with bad transaction stack, transaction %d has target %d:%d\n",
1445 proc->pid, thread->pid, tmp->debug_id,
1446 tmp->to_proc ? tmp->to_proc->pid : 0,
1447 tmp->to_thread ?
1448 tmp->to_thread->pid : 0);
1449 return_error = BR_FAILED_REPLY;
1450 goto err_bad_call_stack;
1452 while (tmp) {
1453 if (tmp->from && tmp->from->proc == target_proc)
1454 target_thread = tmp->from;
1455 tmp = tmp->from_parent;
1459 if (target_thread) {
1460 e->to_thread = target_thread->pid;
1461 target_list = &target_thread->todo;
1462 target_wait = &target_thread->wait;
1463 } else {
1464 target_list = &target_proc->todo;
1465 target_wait = &target_proc->wait;
1467 e->to_proc = target_proc->pid;
1469 /* TODO: reuse incoming transaction for reply */
1470 t = kzalloc(sizeof(*t), GFP_KERNEL);
1471 if (t == NULL) {
1472 return_error = BR_FAILED_REPLY;
1473 goto err_alloc_t_failed;
1475 binder_stats_created(BINDER_STAT_TRANSACTION);
1477 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
1478 if (tcomplete == NULL) {
1479 return_error = BR_FAILED_REPLY;
1480 goto err_alloc_tcomplete_failed;
1482 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
1484 t->debug_id = ++binder_last_id;
1485 e->debug_id = t->debug_id;
1487 if (reply)
1488 binder_debug(BINDER_DEBUG_TRANSACTION,
1489 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld\n",
1490 proc->pid, thread->pid, t->debug_id,
1491 target_proc->pid, target_thread->pid,
1492 (u64)tr->data.ptr.buffer,
1493 (u64)tr->data.ptr.offsets,
1494 (u64)tr->data_size, (u64)tr->offsets_size);
1495 else
1496 binder_debug(BINDER_DEBUG_TRANSACTION,
1497 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld\n",
1498 proc->pid, thread->pid, t->debug_id,
1499 target_proc->pid, target_node->debug_id,
1500 (u64)tr->data.ptr.buffer,
1501 (u64)tr->data.ptr.offsets,
1502 (u64)tr->data_size, (u64)tr->offsets_size);
1504 if (!reply && !(tr->flags & TF_ONE_WAY))
1505 t->from = thread;
1506 else
1507 t->from = NULL;
1508 t->sender_euid = task_euid(proc->tsk);
1509 t->to_proc = target_proc;
1510 t->to_thread = target_thread;
1511 t->code = tr->code;
1512 t->flags = tr->flags;
1513 t->priority = task_nice(current);
1515 trace_binder_transaction(reply, t, target_node);
1517 t->buffer = binder_alloc_buf(target_proc, tr->data_size,
1518 tr->offsets_size, !reply && (t->flags & TF_ONE_WAY));
1519 if (t->buffer == NULL) {
1520 return_error = BR_FAILED_REPLY;
1521 goto err_binder_alloc_buf_failed;
1523 t->buffer->allow_user_free = 0;
1524 t->buffer->debug_id = t->debug_id;
1525 t->buffer->transaction = t;
1526 t->buffer->target_node = target_node;
1527 trace_binder_transaction_alloc_buf(t->buffer);
1528 if (target_node)
1529 binder_inc_node(target_node, 1, 0, NULL);
1531 offp = (binder_size_t *)(t->buffer->data +
1532 ALIGN(tr->data_size, sizeof(void *)));
1534 if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t)
1535 tr->data.ptr.buffer, tr->data_size)) {
1536 binder_user_error("%d:%d got transaction with invalid data ptr\n",
1537 proc->pid, thread->pid);
1538 return_error = BR_FAILED_REPLY;
1539 goto err_copy_data_failed;
1541 if (copy_from_user(offp, (const void __user *)(uintptr_t)
1542 tr->data.ptr.offsets, tr->offsets_size)) {
1543 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
1544 proc->pid, thread->pid);
1545 return_error = BR_FAILED_REPLY;
1546 goto err_copy_data_failed;
1548 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
1549 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
1550 proc->pid, thread->pid, (u64)tr->offsets_size);
1551 return_error = BR_FAILED_REPLY;
1552 goto err_bad_offset;
1554 off_end = (void *)offp + tr->offsets_size;
1555 off_min = 0;
1556 for (; offp < off_end; offp++) {
1557 struct flat_binder_object *fp;
1559 if (*offp > t->buffer->data_size - sizeof(*fp) ||
1560 *offp < off_min ||
1561 t->buffer->data_size < sizeof(*fp) ||
1562 !IS_ALIGNED(*offp, sizeof(u32))) {
1563 binder_user_error("%d:%d got transaction with invalid offset, %lld (min %lld, max %lld)\n",
1564 proc->pid, thread->pid, (u64)*offp,
1565 (u64)off_min,
1566 (u64)(t->buffer->data_size -
1567 sizeof(*fp)));
1568 return_error = BR_FAILED_REPLY;
1569 goto err_bad_offset;
1571 fp = (struct flat_binder_object *)(t->buffer->data + *offp);
1572 off_min = *offp + sizeof(struct flat_binder_object);
1573 switch (fp->type) {
1574 case BINDER_TYPE_BINDER:
1575 case BINDER_TYPE_WEAK_BINDER: {
1576 struct binder_ref *ref;
1577 struct binder_node *node = binder_get_node(proc, fp->binder);
1579 if (node == NULL) {
1580 node = binder_new_node(proc, fp->binder, fp->cookie);
1581 if (node == NULL) {
1582 return_error = BR_FAILED_REPLY;
1583 goto err_binder_new_node_failed;
1585 node->min_priority = fp->flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
1586 node->accept_fds = !!(fp->flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
1588 if (fp->cookie != node->cookie) {
1589 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
1590 proc->pid, thread->pid,
1591 (u64)fp->binder, node->debug_id,
1592 (u64)fp->cookie, (u64)node->cookie);
1593 return_error = BR_FAILED_REPLY;
1594 goto err_binder_get_ref_for_node_failed;
1596 if (security_binder_transfer_binder(proc->tsk,
1597 target_proc->tsk)) {
1598 return_error = BR_FAILED_REPLY;
1599 goto err_binder_get_ref_for_node_failed;
1601 ref = binder_get_ref_for_node(target_proc, node);
1602 if (ref == NULL) {
1603 return_error = BR_FAILED_REPLY;
1604 goto err_binder_get_ref_for_node_failed;
1606 if (fp->type == BINDER_TYPE_BINDER)
1607 fp->type = BINDER_TYPE_HANDLE;
1608 else
1609 fp->type = BINDER_TYPE_WEAK_HANDLE;
1610 fp->binder = 0;
1611 fp->handle = ref->desc;
1612 fp->cookie = 0;
1613 binder_inc_ref(ref, fp->type == BINDER_TYPE_HANDLE,
1614 &thread->todo);
1616 trace_binder_transaction_node_to_ref(t, node, ref);
1617 binder_debug(BINDER_DEBUG_TRANSACTION,
1618 " node %d u%016llx -> ref %d desc %d\n",
1619 node->debug_id, (u64)node->ptr,
1620 ref->debug_id, ref->desc);
1621 } break;
1622 case BINDER_TYPE_HANDLE:
1623 case BINDER_TYPE_WEAK_HANDLE: {
1624 struct binder_ref *ref;
1626 ref = binder_get_ref(proc, fp->handle,
1627 fp->type == BINDER_TYPE_HANDLE);
1629 if (ref == NULL) {
1630 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
1631 proc->pid,
1632 thread->pid, fp->handle);
1633 return_error = BR_FAILED_REPLY;
1634 goto err_binder_get_ref_failed;
1636 if (security_binder_transfer_binder(proc->tsk,
1637 target_proc->tsk)) {
1638 return_error = BR_FAILED_REPLY;
1639 goto err_binder_get_ref_failed;
1641 if (ref->node->proc == target_proc) {
1642 if (fp->type == BINDER_TYPE_HANDLE)
1643 fp->type = BINDER_TYPE_BINDER;
1644 else
1645 fp->type = BINDER_TYPE_WEAK_BINDER;
1646 fp->binder = ref->node->ptr;
1647 fp->cookie = ref->node->cookie;
1648 binder_inc_node(ref->node, fp->type == BINDER_TYPE_BINDER, 0, NULL);
1649 trace_binder_transaction_ref_to_node(t, ref);
1650 binder_debug(BINDER_DEBUG_TRANSACTION,
1651 " ref %d desc %d -> node %d u%016llx\n",
1652 ref->debug_id, ref->desc, ref->node->debug_id,
1653 (u64)ref->node->ptr);
1654 } else {
1655 struct binder_ref *new_ref;
1657 new_ref = binder_get_ref_for_node(target_proc, ref->node);
1658 if (new_ref == NULL) {
1659 return_error = BR_FAILED_REPLY;
1660 goto err_binder_get_ref_for_node_failed;
1662 fp->binder = 0;
1663 fp->handle = new_ref->desc;
1664 fp->cookie = 0;
1665 binder_inc_ref(new_ref, fp->type == BINDER_TYPE_HANDLE, NULL);
1666 trace_binder_transaction_ref_to_ref(t, ref,
1667 new_ref);
1668 binder_debug(BINDER_DEBUG_TRANSACTION,
1669 " ref %d desc %d -> ref %d desc %d (node %d)\n",
1670 ref->debug_id, ref->desc, new_ref->debug_id,
1671 new_ref->desc, ref->node->debug_id);
1673 } break;
1675 case BINDER_TYPE_FD: {
1676 int target_fd;
1677 struct file *file;
1679 if (reply) {
1680 if (!(in_reply_to->flags & TF_ACCEPT_FDS)) {
1681 binder_user_error("%d:%d got reply with fd, %d, but target does not allow fds\n",
1682 proc->pid, thread->pid, fp->handle);
1683 return_error = BR_FAILED_REPLY;
1684 goto err_fd_not_allowed;
1686 } else if (!target_node->accept_fds) {
1687 binder_user_error("%d:%d got transaction with fd, %d, but target does not allow fds\n",
1688 proc->pid, thread->pid, fp->handle);
1689 return_error = BR_FAILED_REPLY;
1690 goto err_fd_not_allowed;
1693 file = fget(fp->handle);
1694 if (file == NULL) {
1695 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
1696 proc->pid, thread->pid, fp->handle);
1697 return_error = BR_FAILED_REPLY;
1698 goto err_fget_failed;
1700 if (security_binder_transfer_file(proc->tsk,
1701 target_proc->tsk,
1702 file) < 0) {
1703 fput(file);
1704 return_error = BR_FAILED_REPLY;
1705 goto err_get_unused_fd_failed;
1707 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
1708 if (target_fd < 0) {
1709 fput(file);
1710 return_error = BR_FAILED_REPLY;
1711 goto err_get_unused_fd_failed;
1713 task_fd_install(target_proc, target_fd, file);
1714 trace_binder_transaction_fd(t, fp->handle, target_fd);
1715 binder_debug(BINDER_DEBUG_TRANSACTION,
1716 " fd %d -> %d\n", fp->handle, target_fd);
1717 /* TODO: fput? */
1718 fp->binder = 0;
1719 fp->handle = target_fd;
1720 } break;
1722 default:
1723 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
1724 proc->pid, thread->pid, fp->type);
1725 return_error = BR_FAILED_REPLY;
1726 goto err_bad_object_type;
1729 if (reply) {
1730 BUG_ON(t->buffer->async_transaction != 0);
1731 binder_pop_transaction(target_thread, in_reply_to);
1732 } else if (!(t->flags & TF_ONE_WAY)) {
1733 BUG_ON(t->buffer->async_transaction != 0);
1734 t->need_reply = 1;
1735 t->from_parent = thread->transaction_stack;
1736 thread->transaction_stack = t;
1737 } else {
1738 BUG_ON(target_node == NULL);
1739 BUG_ON(t->buffer->async_transaction != 1);
1740 if (target_node->has_async_transaction) {
1741 target_list = &target_node->async_todo;
1742 target_wait = NULL;
1743 } else
1744 target_node->has_async_transaction = 1;
1746 t->work.type = BINDER_WORK_TRANSACTION;
1747 list_add_tail(&t->work.entry, target_list);
1748 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
1749 list_add_tail(&tcomplete->entry, &thread->todo);
1750 if (target_wait) {
1751 if (reply || !(t->flags & TF_ONE_WAY))
1752 wake_up_interruptible_sync(target_wait);
1753 else
1754 wake_up_interruptible(target_wait);
1756 return;
1758 err_get_unused_fd_failed:
1759 err_fget_failed:
1760 err_fd_not_allowed:
1761 err_binder_get_ref_for_node_failed:
1762 err_binder_get_ref_failed:
1763 err_binder_new_node_failed:
1764 err_bad_object_type:
1765 err_bad_offset:
1766 err_copy_data_failed:
1767 trace_binder_transaction_failed_buffer_release(t->buffer);
1768 binder_transaction_buffer_release(target_proc, t->buffer, offp);
1769 t->buffer->transaction = NULL;
1770 binder_free_buf(target_proc, t->buffer);
1771 err_binder_alloc_buf_failed:
1772 kfree(tcomplete);
1773 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
1774 err_alloc_tcomplete_failed:
1775 kfree(t);
1776 binder_stats_deleted(BINDER_STAT_TRANSACTION);
1777 err_alloc_t_failed:
1778 err_bad_call_stack:
1779 err_empty_call_stack:
1780 err_dead_binder:
1781 err_invalid_target_handle:
1782 err_no_context_mgr_node:
1783 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1784 "%d:%d transaction failed %d, size %lld-%lld\n",
1785 proc->pid, thread->pid, return_error,
1786 (u64)tr->data_size, (u64)tr->offsets_size);
1789 struct binder_transaction_log_entry *fe;
1791 fe = binder_transaction_log_add(&binder_transaction_log_failed);
1792 *fe = *e;
1795 BUG_ON(thread->return_error != BR_OK);
1796 if (in_reply_to) {
1797 thread->return_error = BR_TRANSACTION_COMPLETE;
1798 binder_send_failed_reply(in_reply_to, return_error);
1799 } else
1800 thread->return_error = return_error;
1803 static int binder_thread_write(struct binder_proc *proc,
1804 struct binder_thread *thread,
1805 binder_uintptr_t binder_buffer, size_t size,
1806 binder_size_t *consumed)
1808 uint32_t cmd;
1809 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
1810 void __user *ptr = buffer + *consumed;
1811 void __user *end = buffer + size;
1813 while (ptr < end && thread->return_error == BR_OK) {
1814 if (get_user(cmd, (uint32_t __user *)ptr))
1815 return -EFAULT;
1816 ptr += sizeof(uint32_t);
1817 trace_binder_command(cmd);
1818 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
1819 binder_stats.bc[_IOC_NR(cmd)]++;
1820 proc->stats.bc[_IOC_NR(cmd)]++;
1821 thread->stats.bc[_IOC_NR(cmd)]++;
1823 switch (cmd) {
1824 case BC_INCREFS:
1825 case BC_ACQUIRE:
1826 case BC_RELEASE:
1827 case BC_DECREFS: {
1828 uint32_t target;
1829 struct binder_ref *ref;
1830 const char *debug_string;
1832 if (get_user(target, (uint32_t __user *)ptr))
1833 return -EFAULT;
1834 ptr += sizeof(uint32_t);
1835 if (target == 0 && binder_context_mgr_node &&
1836 (cmd == BC_INCREFS || cmd == BC_ACQUIRE)) {
1837 if (binder_context_mgr_node->proc == proc) {
1838 binder_user_error("%d:%d context manager tried to acquire desc 0\n",
1839 proc->pid, thread->pid);
1840 return -EINVAL;
1842 ref = binder_get_ref_for_node(proc,
1843 binder_context_mgr_node);
1844 if (ref->desc != target) {
1845 binder_user_error("%d:%d tried to acquire reference to desc 0, got %d instead\n",
1846 proc->pid, thread->pid,
1847 ref->desc);
1849 } else
1850 ref = binder_get_ref(proc, target,
1851 cmd == BC_ACQUIRE ||
1852 cmd == BC_RELEASE);
1853 if (ref == NULL) {
1854 binder_user_error("%d:%d refcount change on invalid ref %d\n",
1855 proc->pid, thread->pid, target);
1856 break;
1858 switch (cmd) {
1859 case BC_INCREFS:
1860 debug_string = "IncRefs";
1861 binder_inc_ref(ref, 0, NULL);
1862 break;
1863 case BC_ACQUIRE:
1864 debug_string = "Acquire";
1865 binder_inc_ref(ref, 1, NULL);
1866 break;
1867 case BC_RELEASE:
1868 debug_string = "Release";
1869 binder_dec_ref(ref, 1);
1870 break;
1871 case BC_DECREFS:
1872 default:
1873 debug_string = "DecRefs";
1874 binder_dec_ref(ref, 0);
1875 break;
1877 binder_debug(BINDER_DEBUG_USER_REFS,
1878 "%d:%d %s ref %d desc %d s %d w %d for node %d\n",
1879 proc->pid, thread->pid, debug_string, ref->debug_id,
1880 ref->desc, ref->strong, ref->weak, ref->node->debug_id);
1881 break;
1883 case BC_INCREFS_DONE:
1884 case BC_ACQUIRE_DONE: {
1885 binder_uintptr_t node_ptr;
1886 binder_uintptr_t cookie;
1887 struct binder_node *node;
1889 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
1890 return -EFAULT;
1891 ptr += sizeof(binder_uintptr_t);
1892 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
1893 return -EFAULT;
1894 ptr += sizeof(binder_uintptr_t);
1895 node = binder_get_node(proc, node_ptr);
1896 if (node == NULL) {
1897 binder_user_error("%d:%d %s u%016llx no match\n",
1898 proc->pid, thread->pid,
1899 cmd == BC_INCREFS_DONE ?
1900 "BC_INCREFS_DONE" :
1901 "BC_ACQUIRE_DONE",
1902 (u64)node_ptr);
1903 break;
1905 if (cookie != node->cookie) {
1906 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
1907 proc->pid, thread->pid,
1908 cmd == BC_INCREFS_DONE ?
1909 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
1910 (u64)node_ptr, node->debug_id,
1911 (u64)cookie, (u64)node->cookie);
1912 break;
1914 if (cmd == BC_ACQUIRE_DONE) {
1915 if (node->pending_strong_ref == 0) {
1916 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
1917 proc->pid, thread->pid,
1918 node->debug_id);
1919 break;
1921 node->pending_strong_ref = 0;
1922 } else {
1923 if (node->pending_weak_ref == 0) {
1924 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
1925 proc->pid, thread->pid,
1926 node->debug_id);
1927 break;
1929 node->pending_weak_ref = 0;
1931 binder_dec_node(node, cmd == BC_ACQUIRE_DONE, 0);
1932 binder_debug(BINDER_DEBUG_USER_REFS,
1933 "%d:%d %s node %d ls %d lw %d\n",
1934 proc->pid, thread->pid,
1935 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
1936 node->debug_id, node->local_strong_refs, node->local_weak_refs);
1937 break;
1939 case BC_ATTEMPT_ACQUIRE:
1940 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
1941 return -EINVAL;
1942 case BC_ACQUIRE_RESULT:
1943 pr_err("BC_ACQUIRE_RESULT not supported\n");
1944 return -EINVAL;
1946 case BC_FREE_BUFFER: {
1947 binder_uintptr_t data_ptr;
1948 struct binder_buffer *buffer;
1950 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
1951 return -EFAULT;
1952 ptr += sizeof(binder_uintptr_t);
1954 buffer = binder_buffer_lookup(proc, data_ptr);
1955 if (buffer == NULL) {
1956 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
1957 proc->pid, thread->pid, (u64)data_ptr);
1958 break;
1960 if (!buffer->allow_user_free) {
1961 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
1962 proc->pid, thread->pid, (u64)data_ptr);
1963 break;
1965 binder_debug(BINDER_DEBUG_FREE_BUFFER,
1966 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
1967 proc->pid, thread->pid, (u64)data_ptr,
1968 buffer->debug_id,
1969 buffer->transaction ? "active" : "finished");
1971 if (buffer->transaction) {
1972 buffer->transaction->buffer = NULL;
1973 buffer->transaction = NULL;
1975 if (buffer->async_transaction && buffer->target_node) {
1976 BUG_ON(!buffer->target_node->has_async_transaction);
1977 if (list_empty(&buffer->target_node->async_todo))
1978 buffer->target_node->has_async_transaction = 0;
1979 else
1980 list_move_tail(buffer->target_node->async_todo.next, &thread->todo);
1982 trace_binder_transaction_buffer_release(buffer);
1983 binder_transaction_buffer_release(proc, buffer, NULL);
1984 binder_free_buf(proc, buffer);
1985 break;
1988 case BC_TRANSACTION:
1989 case BC_REPLY: {
1990 struct binder_transaction_data tr;
1992 if (copy_from_user(&tr, ptr, sizeof(tr)))
1993 return -EFAULT;
1994 ptr += sizeof(tr);
1995 binder_transaction(proc, thread, &tr, cmd == BC_REPLY);
1996 break;
1999 case BC_REGISTER_LOOPER:
2000 binder_debug(BINDER_DEBUG_THREADS,
2001 "%d:%d BC_REGISTER_LOOPER\n",
2002 proc->pid, thread->pid);
2003 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
2004 thread->looper |= BINDER_LOOPER_STATE_INVALID;
2005 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
2006 proc->pid, thread->pid);
2007 } else if (proc->requested_threads == 0) {
2008 thread->looper |= BINDER_LOOPER_STATE_INVALID;
2009 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
2010 proc->pid, thread->pid);
2011 } else {
2012 proc->requested_threads--;
2013 proc->requested_threads_started++;
2015 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
2016 break;
2017 case BC_ENTER_LOOPER:
2018 binder_debug(BINDER_DEBUG_THREADS,
2019 "%d:%d BC_ENTER_LOOPER\n",
2020 proc->pid, thread->pid);
2021 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
2022 thread->looper |= BINDER_LOOPER_STATE_INVALID;
2023 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
2024 proc->pid, thread->pid);
2026 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
2027 break;
2028 case BC_EXIT_LOOPER:
2029 binder_debug(BINDER_DEBUG_THREADS,
2030 "%d:%d BC_EXIT_LOOPER\n",
2031 proc->pid, thread->pid);
2032 thread->looper |= BINDER_LOOPER_STATE_EXITED;
2033 break;
2035 case BC_REQUEST_DEATH_NOTIFICATION:
2036 case BC_CLEAR_DEATH_NOTIFICATION: {
2037 uint32_t target;
2038 binder_uintptr_t cookie;
2039 struct binder_ref *ref;
2040 struct binder_ref_death *death;
2042 if (get_user(target, (uint32_t __user *)ptr))
2043 return -EFAULT;
2044 ptr += sizeof(uint32_t);
2045 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
2046 return -EFAULT;
2047 ptr += sizeof(binder_uintptr_t);
2048 ref = binder_get_ref(proc, target, false);
2049 if (ref == NULL) {
2050 binder_user_error("%d:%d %s invalid ref %d\n",
2051 proc->pid, thread->pid,
2052 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
2053 "BC_REQUEST_DEATH_NOTIFICATION" :
2054 "BC_CLEAR_DEATH_NOTIFICATION",
2055 target);
2056 break;
2059 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
2060 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
2061 proc->pid, thread->pid,
2062 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
2063 "BC_REQUEST_DEATH_NOTIFICATION" :
2064 "BC_CLEAR_DEATH_NOTIFICATION",
2065 (u64)cookie, ref->debug_id, ref->desc,
2066 ref->strong, ref->weak, ref->node->debug_id);
2068 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
2069 if (ref->death) {
2070 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
2071 proc->pid, thread->pid);
2072 break;
2074 death = kzalloc(sizeof(*death), GFP_KERNEL);
2075 if (death == NULL) {
2076 thread->return_error = BR_ERROR;
2077 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2078 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
2079 proc->pid, thread->pid);
2080 break;
2082 binder_stats_created(BINDER_STAT_DEATH);
2083 INIT_LIST_HEAD(&death->work.entry);
2084 death->cookie = cookie;
2085 ref->death = death;
2086 if (ref->node->proc == NULL) {
2087 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
2088 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2089 list_add_tail(&ref->death->work.entry, &thread->todo);
2090 } else {
2091 list_add_tail(&ref->death->work.entry, &proc->todo);
2092 wake_up_interruptible(&proc->wait);
2095 } else {
2096 if (ref->death == NULL) {
2097 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
2098 proc->pid, thread->pid);
2099 break;
2101 death = ref->death;
2102 if (death->cookie != cookie) {
2103 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
2104 proc->pid, thread->pid,
2105 (u64)death->cookie,
2106 (u64)cookie);
2107 break;
2109 ref->death = NULL;
2110 if (list_empty(&death->work.entry)) {
2111 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2112 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2113 list_add_tail(&death->work.entry, &thread->todo);
2114 } else {
2115 list_add_tail(&death->work.entry, &proc->todo);
2116 wake_up_interruptible(&proc->wait);
2118 } else {
2119 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
2120 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
2123 } break;
2124 case BC_DEAD_BINDER_DONE: {
2125 struct binder_work *w;
2126 binder_uintptr_t cookie;
2127 struct binder_ref_death *death = NULL;
2129 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
2130 return -EFAULT;
2132 ptr += sizeof(cookie);
2133 list_for_each_entry(w, &proc->delivered_death, entry) {
2134 struct binder_ref_death *tmp_death = container_of(w, struct binder_ref_death, work);
2136 if (tmp_death->cookie == cookie) {
2137 death = tmp_death;
2138 break;
2141 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2142 "%d:%d BC_DEAD_BINDER_DONE %016llx found %pK\n",
2143 proc->pid, thread->pid, (u64)cookie,
2144 death);
2145 if (death == NULL) {
2146 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
2147 proc->pid, thread->pid, (u64)cookie);
2148 break;
2151 list_del_init(&death->work.entry);
2152 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
2153 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2154 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2155 list_add_tail(&death->work.entry, &thread->todo);
2156 } else {
2157 list_add_tail(&death->work.entry, &proc->todo);
2158 wake_up_interruptible(&proc->wait);
2161 } break;
2163 default:
2164 pr_err("%d:%d unknown command %d\n",
2165 proc->pid, thread->pid, cmd);
2166 return -EINVAL;
2168 *consumed = ptr - buffer;
2170 return 0;
2173 static void binder_stat_br(struct binder_proc *proc,
2174 struct binder_thread *thread, uint32_t cmd)
2176 trace_binder_return(cmd);
2177 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
2178 binder_stats.br[_IOC_NR(cmd)]++;
2179 proc->stats.br[_IOC_NR(cmd)]++;
2180 thread->stats.br[_IOC_NR(cmd)]++;
2184 static int binder_has_proc_work(struct binder_proc *proc,
2185 struct binder_thread *thread)
2187 return !list_empty(&proc->todo) ||
2188 (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2191 static int binder_has_thread_work(struct binder_thread *thread)
2193 return !list_empty(&thread->todo) || thread->return_error != BR_OK ||
2194 (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2197 static int binder_thread_read(struct binder_proc *proc,
2198 struct binder_thread *thread,
2199 binder_uintptr_t binder_buffer, size_t size,
2200 binder_size_t *consumed, int non_block)
2202 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
2203 void __user *ptr = buffer + *consumed;
2204 void __user *end = buffer + size;
2206 int ret = 0;
2207 int wait_for_proc_work;
2209 if (*consumed == 0) {
2210 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
2211 return -EFAULT;
2212 ptr += sizeof(uint32_t);
2215 retry:
2216 wait_for_proc_work = thread->transaction_stack == NULL &&
2217 list_empty(&thread->todo);
2219 if (thread->return_error != BR_OK && ptr < end) {
2220 if (thread->return_error2 != BR_OK) {
2221 if (put_user(thread->return_error2, (uint32_t __user *)ptr))
2222 return -EFAULT;
2223 ptr += sizeof(uint32_t);
2224 binder_stat_br(proc, thread, thread->return_error2);
2225 if (ptr == end)
2226 goto done;
2227 thread->return_error2 = BR_OK;
2229 if (put_user(thread->return_error, (uint32_t __user *)ptr))
2230 return -EFAULT;
2231 ptr += sizeof(uint32_t);
2232 binder_stat_br(proc, thread, thread->return_error);
2233 thread->return_error = BR_OK;
2234 goto done;
2238 thread->looper |= BINDER_LOOPER_STATE_WAITING;
2239 if (wait_for_proc_work)
2240 proc->ready_threads++;
2242 binder_unlock(__func__);
2244 trace_binder_wait_for_work(wait_for_proc_work,
2245 !!thread->transaction_stack,
2246 !list_empty(&thread->todo));
2247 if (wait_for_proc_work) {
2248 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2249 BINDER_LOOPER_STATE_ENTERED))) {
2250 binder_user_error("%d:%d ERROR: Thread waiting for process work before calling BC_REGISTER_LOOPER or BC_ENTER_LOOPER (state %x)\n",
2251 proc->pid, thread->pid, thread->looper);
2252 wait_event_interruptible(binder_user_error_wait,
2253 binder_stop_on_user_error < 2);
2255 binder_set_nice(proc->default_priority);
2256 if (non_block) {
2257 if (!binder_has_proc_work(proc, thread))
2258 ret = -EAGAIN;
2259 } else
2260 ret = wait_event_freezable_exclusive(proc->wait, binder_has_proc_work(proc, thread));
2261 } else {
2262 if (non_block) {
2263 if (!binder_has_thread_work(thread))
2264 ret = -EAGAIN;
2265 } else
2266 ret = wait_event_freezable(thread->wait, binder_has_thread_work(thread));
2269 binder_lock(__func__);
2271 if (wait_for_proc_work)
2272 proc->ready_threads--;
2273 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
2275 if (ret)
2276 return ret;
2278 while (1) {
2279 uint32_t cmd;
2280 struct binder_transaction_data tr;
2281 struct binder_work *w;
2282 struct binder_transaction *t = NULL;
2284 if (!list_empty(&thread->todo)) {
2285 w = list_first_entry(&thread->todo, struct binder_work,
2286 entry);
2287 } else if (!list_empty(&proc->todo) && wait_for_proc_work) {
2288 w = list_first_entry(&proc->todo, struct binder_work,
2289 entry);
2290 } else {
2291 /* no data added */
2292 if (ptr - buffer == 4 &&
2293 !(thread->looper & BINDER_LOOPER_STATE_NEED_RETURN))
2294 goto retry;
2295 break;
2298 if (end - ptr < sizeof(tr) + 4)
2299 break;
2301 switch (w->type) {
2302 case BINDER_WORK_TRANSACTION: {
2303 t = container_of(w, struct binder_transaction, work);
2304 } break;
2305 case BINDER_WORK_TRANSACTION_COMPLETE: {
2306 cmd = BR_TRANSACTION_COMPLETE;
2307 if (put_user(cmd, (uint32_t __user *)ptr))
2308 return -EFAULT;
2309 ptr += sizeof(uint32_t);
2311 binder_stat_br(proc, thread, cmd);
2312 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
2313 "%d:%d BR_TRANSACTION_COMPLETE\n",
2314 proc->pid, thread->pid);
2316 list_del(&w->entry);
2317 kfree(w);
2318 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2319 } break;
2320 case BINDER_WORK_NODE: {
2321 struct binder_node *node = container_of(w, struct binder_node, work);
2322 uint32_t cmd = BR_NOOP;
2323 const char *cmd_name;
2324 int strong = node->internal_strong_refs || node->local_strong_refs;
2325 int weak = !hlist_empty(&node->refs) || node->local_weak_refs || strong;
2327 if (weak && !node->has_weak_ref) {
2328 cmd = BR_INCREFS;
2329 cmd_name = "BR_INCREFS";
2330 node->has_weak_ref = 1;
2331 node->pending_weak_ref = 1;
2332 node->local_weak_refs++;
2333 } else if (strong && !node->has_strong_ref) {
2334 cmd = BR_ACQUIRE;
2335 cmd_name = "BR_ACQUIRE";
2336 node->has_strong_ref = 1;
2337 node->pending_strong_ref = 1;
2338 node->local_strong_refs++;
2339 } else if (!strong && node->has_strong_ref) {
2340 cmd = BR_RELEASE;
2341 cmd_name = "BR_RELEASE";
2342 node->has_strong_ref = 0;
2343 } else if (!weak && node->has_weak_ref) {
2344 cmd = BR_DECREFS;
2345 cmd_name = "BR_DECREFS";
2346 node->has_weak_ref = 0;
2348 if (cmd != BR_NOOP) {
2349 if (put_user(cmd, (uint32_t __user *)ptr))
2350 return -EFAULT;
2351 ptr += sizeof(uint32_t);
2352 if (put_user(node->ptr,
2353 (binder_uintptr_t __user *)ptr))
2354 return -EFAULT;
2355 ptr += sizeof(binder_uintptr_t);
2356 if (put_user(node->cookie,
2357 (binder_uintptr_t __user *)ptr))
2358 return -EFAULT;
2359 ptr += sizeof(binder_uintptr_t);
2361 binder_stat_br(proc, thread, cmd);
2362 binder_debug(BINDER_DEBUG_USER_REFS,
2363 "%d:%d %s %d u%016llx c%016llx\n",
2364 proc->pid, thread->pid, cmd_name,
2365 node->debug_id,
2366 (u64)node->ptr, (u64)node->cookie);
2367 } else {
2368 list_del_init(&w->entry);
2369 if (!weak && !strong) {
2370 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
2371 "%d:%d node %d u%016llx c%016llx deleted\n",
2372 proc->pid, thread->pid,
2373 node->debug_id,
2374 (u64)node->ptr,
2375 (u64)node->cookie);
2376 rb_erase(&node->rb_node, &proc->nodes);
2377 kfree(node);
2378 binder_stats_deleted(BINDER_STAT_NODE);
2379 } else {
2380 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
2381 "%d:%d node %d u%016llx c%016llx state unchanged\n",
2382 proc->pid, thread->pid,
2383 node->debug_id,
2384 (u64)node->ptr,
2385 (u64)node->cookie);
2388 } break;
2389 case BINDER_WORK_DEAD_BINDER:
2390 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2391 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2392 struct binder_ref_death *death;
2393 uint32_t cmd;
2395 death = container_of(w, struct binder_ref_death, work);
2396 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
2397 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
2398 else
2399 cmd = BR_DEAD_BINDER;
2400 if (put_user(cmd, (uint32_t __user *)ptr))
2401 return -EFAULT;
2402 ptr += sizeof(uint32_t);
2403 if (put_user(death->cookie,
2404 (binder_uintptr_t __user *)ptr))
2405 return -EFAULT;
2406 ptr += sizeof(binder_uintptr_t);
2407 binder_stat_br(proc, thread, cmd);
2408 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
2409 "%d:%d %s %016llx\n",
2410 proc->pid, thread->pid,
2411 cmd == BR_DEAD_BINDER ?
2412 "BR_DEAD_BINDER" :
2413 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
2414 (u64)death->cookie);
2416 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
2417 list_del(&w->entry);
2418 kfree(death);
2419 binder_stats_deleted(BINDER_STAT_DEATH);
2420 } else
2421 list_move(&w->entry, &proc->delivered_death);
2422 if (cmd == BR_DEAD_BINDER)
2423 goto done; /* DEAD_BINDER notifications can cause transactions */
2424 } break;
2427 if (!t)
2428 continue;
2430 BUG_ON(t->buffer == NULL);
2431 if (t->buffer->target_node) {
2432 struct binder_node *target_node = t->buffer->target_node;
2434 tr.target.ptr = target_node->ptr;
2435 tr.cookie = target_node->cookie;
2436 t->saved_priority = task_nice(current);
2437 if (t->priority < target_node->min_priority &&
2438 !(t->flags & TF_ONE_WAY))
2439 binder_set_nice(t->priority);
2440 else if (!(t->flags & TF_ONE_WAY) ||
2441 t->saved_priority > target_node->min_priority)
2442 binder_set_nice(target_node->min_priority);
2443 cmd = BR_TRANSACTION;
2444 } else {
2445 tr.target.ptr = 0;
2446 tr.cookie = 0;
2447 cmd = BR_REPLY;
2449 tr.code = t->code;
2450 tr.flags = t->flags;
2451 tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
2453 if (t->from) {
2454 struct task_struct *sender = t->from->proc->tsk;
2456 tr.sender_pid = task_tgid_nr_ns(sender,
2457 task_active_pid_ns(current));
2458 } else {
2459 tr.sender_pid = 0;
2462 tr.data_size = t->buffer->data_size;
2463 tr.offsets_size = t->buffer->offsets_size;
2464 tr.data.ptr.buffer = (binder_uintptr_t)(
2465 (uintptr_t)t->buffer->data +
2466 proc->user_buffer_offset);
2467 tr.data.ptr.offsets = tr.data.ptr.buffer +
2468 ALIGN(t->buffer->data_size,
2469 sizeof(void *));
2471 if (put_user(cmd, (uint32_t __user *)ptr))
2472 return -EFAULT;
2473 ptr += sizeof(uint32_t);
2474 if (copy_to_user(ptr, &tr, sizeof(tr)))
2475 return -EFAULT;
2476 ptr += sizeof(tr);
2478 trace_binder_transaction_received(t);
2479 binder_stat_br(proc, thread, cmd);
2480 binder_debug(BINDER_DEBUG_TRANSACTION,
2481 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
2482 proc->pid, thread->pid,
2483 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
2484 "BR_REPLY",
2485 t->debug_id, t->from ? t->from->proc->pid : 0,
2486 t->from ? t->from->pid : 0, cmd,
2487 t->buffer->data_size, t->buffer->offsets_size,
2488 (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
2490 list_del(&t->work.entry);
2491 t->buffer->allow_user_free = 1;
2492 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
2493 t->to_parent = thread->transaction_stack;
2494 t->to_thread = thread;
2495 thread->transaction_stack = t;
2496 } else {
2497 t->buffer->transaction = NULL;
2498 kfree(t);
2499 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2501 break;
2504 done:
2506 *consumed = ptr - buffer;
2507 if (proc->requested_threads + proc->ready_threads == 0 &&
2508 proc->requested_threads_started < proc->max_threads &&
2509 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2510 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
2511 /*spawn a new thread if we leave this out */) {
2512 proc->requested_threads++;
2513 binder_debug(BINDER_DEBUG_THREADS,
2514 "%d:%d BR_SPAWN_LOOPER\n",
2515 proc->pid, thread->pid);
2516 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
2517 return -EFAULT;
2518 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
2520 return 0;
2523 static void binder_release_work(struct list_head *list)
2525 struct binder_work *w;
2527 while (!list_empty(list)) {
2528 w = list_first_entry(list, struct binder_work, entry);
2529 list_del_init(&w->entry);
2530 switch (w->type) {
2531 case BINDER_WORK_TRANSACTION: {
2532 struct binder_transaction *t;
2534 t = container_of(w, struct binder_transaction, work);
2535 if (t->buffer->target_node &&
2536 !(t->flags & TF_ONE_WAY)) {
2537 binder_send_failed_reply(t, BR_DEAD_REPLY);
2538 } else {
2539 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2540 "undelivered transaction %d\n",
2541 t->debug_id);
2542 t->buffer->transaction = NULL;
2543 kfree(t);
2544 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2546 } break;
2547 case BINDER_WORK_TRANSACTION_COMPLETE: {
2548 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2549 "undelivered TRANSACTION_COMPLETE\n");
2550 kfree(w);
2551 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2552 } break;
2553 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2554 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2555 struct binder_ref_death *death;
2557 death = container_of(w, struct binder_ref_death, work);
2558 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2559 "undelivered death notification, %016llx\n",
2560 (u64)death->cookie);
2561 kfree(death);
2562 binder_stats_deleted(BINDER_STAT_DEATH);
2563 } break;
2564 default:
2565 pr_err("unexpected work type, %d, not freed\n",
2566 w->type);
2567 break;
2573 static struct binder_thread *binder_get_thread(struct binder_proc *proc)
2575 struct binder_thread *thread = NULL;
2576 struct rb_node *parent = NULL;
2577 struct rb_node **p = &proc->threads.rb_node;
2579 while (*p) {
2580 parent = *p;
2581 thread = rb_entry(parent, struct binder_thread, rb_node);
2583 if (current->pid < thread->pid)
2584 p = &(*p)->rb_left;
2585 else if (current->pid > thread->pid)
2586 p = &(*p)->rb_right;
2587 else
2588 break;
2590 if (*p == NULL) {
2591 thread = kzalloc(sizeof(*thread), GFP_KERNEL);
2592 if (thread == NULL)
2593 return NULL;
2594 binder_stats_created(BINDER_STAT_THREAD);
2595 thread->proc = proc;
2596 thread->pid = current->pid;
2597 init_waitqueue_head(&thread->wait);
2598 INIT_LIST_HEAD(&thread->todo);
2599 rb_link_node(&thread->rb_node, parent, p);
2600 rb_insert_color(&thread->rb_node, &proc->threads);
2601 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
2602 thread->return_error = BR_OK;
2603 thread->return_error2 = BR_OK;
2605 return thread;
2608 static int binder_free_thread(struct binder_proc *proc,
2609 struct binder_thread *thread)
2611 struct binder_transaction *t;
2612 struct binder_transaction *send_reply = NULL;
2613 int active_transactions = 0;
2615 rb_erase(&thread->rb_node, &proc->threads);
2616 t = thread->transaction_stack;
2617 if (t && t->to_thread == thread)
2618 send_reply = t;
2619 while (t) {
2620 active_transactions++;
2621 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2622 "release %d:%d transaction %d %s, still active\n",
2623 proc->pid, thread->pid,
2624 t->debug_id,
2625 (t->to_thread == thread) ? "in" : "out");
2627 if (t->to_thread == thread) {
2628 t->to_proc = NULL;
2629 t->to_thread = NULL;
2630 if (t->buffer) {
2631 t->buffer->transaction = NULL;
2632 t->buffer = NULL;
2634 t = t->to_parent;
2635 } else if (t->from == thread) {
2636 t->from = NULL;
2637 t = t->from_parent;
2638 } else
2639 BUG();
2643 * If this thread used poll, make sure we remove the waitqueue
2644 * from any epoll data structures holding it with POLLFREE.
2645 * waitqueue_active() is safe to use here because we're holding
2646 * the global lock.
2648 if ((thread->looper & BINDER_LOOPER_STATE_POLL) &&
2649 waitqueue_active(&thread->wait)) {
2650 wake_up_poll(&thread->wait, POLLHUP | POLLFREE);
2654 * This is needed to avoid races between wake_up_poll() above and
2655 * and ep_remove_waitqueue() called for other reasons (eg the epoll file
2656 * descriptor being closed); ep_remove_waitqueue() holds an RCU read
2657 * lock, so we can be sure it's done after calling synchronize_rcu().
2659 if (thread->looper & BINDER_LOOPER_STATE_POLL)
2660 synchronize_rcu();
2662 if (send_reply)
2663 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
2664 binder_release_work(&thread->todo);
2665 kfree(thread);
2666 binder_stats_deleted(BINDER_STAT_THREAD);
2667 return active_transactions;
2670 static unsigned int binder_poll(struct file *filp,
2671 struct poll_table_struct *wait)
2673 struct binder_proc *proc = filp->private_data;
2674 struct binder_thread *thread = NULL;
2675 int wait_for_proc_work;
2677 binder_lock(__func__);
2679 thread = binder_get_thread(proc);
2680 if (!thread) {
2681 binder_unlock(__func__);
2682 return POLLERR;
2685 thread->looper |= BINDER_LOOPER_STATE_POLL;
2687 wait_for_proc_work = thread->transaction_stack == NULL &&
2688 list_empty(&thread->todo) && thread->return_error == BR_OK;
2690 binder_unlock(__func__);
2692 if (wait_for_proc_work) {
2693 if (binder_has_proc_work(proc, thread))
2694 return POLLIN;
2695 poll_wait(filp, &proc->wait, wait);
2696 if (binder_has_proc_work(proc, thread))
2697 return POLLIN;
2698 } else {
2699 if (binder_has_thread_work(thread))
2700 return POLLIN;
2701 poll_wait(filp, &thread->wait, wait);
2702 if (binder_has_thread_work(thread))
2703 return POLLIN;
2705 return 0;
2708 static int binder_ioctl_write_read(struct file *filp,
2709 unsigned int cmd, unsigned long arg,
2710 struct binder_thread *thread)
2712 int ret = 0;
2713 struct binder_proc *proc = filp->private_data;
2714 unsigned int size = _IOC_SIZE(cmd);
2715 void __user *ubuf = (void __user *)arg;
2716 struct binder_write_read bwr;
2718 if (size != sizeof(struct binder_write_read)) {
2719 ret = -EINVAL;
2720 goto out;
2722 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
2723 ret = -EFAULT;
2724 goto out;
2726 binder_debug(BINDER_DEBUG_READ_WRITE,
2727 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
2728 proc->pid, thread->pid,
2729 (u64)bwr.write_size, (u64)bwr.write_buffer,
2730 (u64)bwr.read_size, (u64)bwr.read_buffer);
2732 if (bwr.write_size > 0) {
2733 ret = binder_thread_write(proc, thread,
2734 bwr.write_buffer,
2735 bwr.write_size,
2736 &bwr.write_consumed);
2737 trace_binder_write_done(ret);
2738 if (ret < 0) {
2739 bwr.read_consumed = 0;
2740 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2741 ret = -EFAULT;
2742 goto out;
2745 if (bwr.read_size > 0) {
2746 ret = binder_thread_read(proc, thread, bwr.read_buffer,
2747 bwr.read_size,
2748 &bwr.read_consumed,
2749 filp->f_flags & O_NONBLOCK);
2750 trace_binder_read_done(ret);
2751 if (!list_empty(&proc->todo))
2752 wake_up_interruptible(&proc->wait);
2753 if (ret < 0) {
2754 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2755 ret = -EFAULT;
2756 goto out;
2759 binder_debug(BINDER_DEBUG_READ_WRITE,
2760 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
2761 proc->pid, thread->pid,
2762 (u64)bwr.write_consumed, (u64)bwr.write_size,
2763 (u64)bwr.read_consumed, (u64)bwr.read_size);
2764 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
2765 ret = -EFAULT;
2766 goto out;
2768 out:
2769 return ret;
2772 static int binder_ioctl_set_ctx_mgr(struct file *filp)
2774 int ret = 0;
2775 struct binder_proc *proc = filp->private_data;
2776 kuid_t curr_euid = current_euid();
2778 if (binder_context_mgr_node != NULL) {
2779 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
2780 ret = -EBUSY;
2781 goto out;
2783 ret = security_binder_set_context_mgr(proc->tsk);
2784 if (ret < 0)
2785 goto out;
2786 if (uid_valid(binder_context_mgr_uid)) {
2787 if (!uid_eq(binder_context_mgr_uid, curr_euid)) {
2788 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
2789 from_kuid(&init_user_ns, curr_euid),
2790 from_kuid(&init_user_ns,
2791 binder_context_mgr_uid));
2792 ret = -EPERM;
2793 goto out;
2795 } else {
2796 binder_context_mgr_uid = curr_euid;
2798 binder_context_mgr_node = binder_new_node(proc, 0, 0);
2799 if (binder_context_mgr_node == NULL) {
2800 ret = -ENOMEM;
2801 goto out;
2803 binder_context_mgr_node->local_weak_refs++;
2804 binder_context_mgr_node->local_strong_refs++;
2805 binder_context_mgr_node->has_strong_ref = 1;
2806 binder_context_mgr_node->has_weak_ref = 1;
2807 out:
2808 return ret;
2811 static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2813 int ret;
2814 struct binder_proc *proc = filp->private_data;
2815 struct binder_thread *thread;
2816 unsigned int size = _IOC_SIZE(cmd);
2817 void __user *ubuf = (void __user *)arg;
2819 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
2820 proc->pid, current->pid, cmd, arg);*/
2822 trace_binder_ioctl(cmd, arg);
2824 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2825 if (ret)
2826 goto err_unlocked;
2828 binder_lock(__func__);
2829 thread = binder_get_thread(proc);
2830 if (thread == NULL) {
2831 ret = -ENOMEM;
2832 goto err;
2835 switch (cmd) {
2836 case BINDER_WRITE_READ:
2837 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
2838 if (ret)
2839 goto err;
2840 break;
2841 case BINDER_SET_MAX_THREADS:
2842 if (copy_from_user(&proc->max_threads, ubuf, sizeof(proc->max_threads))) {
2843 ret = -EINVAL;
2844 goto err;
2846 break;
2847 case BINDER_SET_CONTEXT_MGR:
2848 ret = binder_ioctl_set_ctx_mgr(filp);
2849 if (ret)
2850 goto err;
2851 break;
2852 case BINDER_THREAD_EXIT:
2853 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
2854 proc->pid, thread->pid);
2855 binder_free_thread(proc, thread);
2856 thread = NULL;
2857 break;
2858 case BINDER_VERSION: {
2859 struct binder_version __user *ver = ubuf;
2861 if (size != sizeof(struct binder_version)) {
2862 ret = -EINVAL;
2863 goto err;
2865 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
2866 &ver->protocol_version)) {
2867 ret = -EINVAL;
2868 goto err;
2870 break;
2872 default:
2873 ret = -EINVAL;
2874 goto err;
2876 ret = 0;
2877 err:
2878 if (thread)
2879 thread->looper &= ~BINDER_LOOPER_STATE_NEED_RETURN;
2880 binder_unlock(__func__);
2881 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2882 if (ret && ret != -ERESTARTSYS)
2883 pr_info("%d:%d ioctl %x %lx returned %d\n", proc->pid, current->pid, cmd, arg, ret);
2884 err_unlocked:
2885 trace_binder_ioctl_done(ret);
2886 return ret;
2889 static void binder_vma_open(struct vm_area_struct *vma)
2891 struct binder_proc *proc = vma->vm_private_data;
2893 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
2894 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
2895 proc->pid, vma->vm_start, vma->vm_end,
2896 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2897 (unsigned long)pgprot_val(vma->vm_page_prot));
2900 static void binder_vma_close(struct vm_area_struct *vma)
2902 struct binder_proc *proc = vma->vm_private_data;
2904 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
2905 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
2906 proc->pid, vma->vm_start, vma->vm_end,
2907 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2908 (unsigned long)pgprot_val(vma->vm_page_prot));
2909 proc->vma = NULL;
2910 proc->vma_vm_mm = NULL;
2911 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
2914 static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2916 return VM_FAULT_SIGBUS;
2919 static const struct vm_operations_struct binder_vm_ops = {
2920 .open = binder_vma_open,
2921 .close = binder_vma_close,
2922 .fault = binder_vm_fault,
2925 static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
2927 int ret;
2928 struct vm_struct *area;
2929 struct binder_proc *proc = filp->private_data;
2930 const char *failure_string;
2931 struct binder_buffer *buffer;
2933 if (proc->tsk != current->group_leader)
2934 return -EINVAL;
2936 if ((vma->vm_end - vma->vm_start) > SZ_4M)
2937 vma->vm_end = vma->vm_start + SZ_4M;
2939 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
2940 "binder_mmap: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
2941 proc->pid, vma->vm_start, vma->vm_end,
2942 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2943 (unsigned long)pgprot_val(vma->vm_page_prot));
2945 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
2946 ret = -EPERM;
2947 failure_string = "bad vm_flags";
2948 goto err_bad_arg;
2950 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
2952 mutex_lock(&binder_mmap_lock);
2953 if (proc->buffer) {
2954 ret = -EBUSY;
2955 failure_string = "already mapped";
2956 goto err_already_mapped;
2959 area = get_vm_area(vma->vm_end - vma->vm_start, VM_IOREMAP);
2960 if (area == NULL) {
2961 ret = -ENOMEM;
2962 failure_string = "get_vm_area";
2963 goto err_get_vm_area_failed;
2965 proc->buffer = area->addr;
2966 proc->user_buffer_offset = vma->vm_start - (uintptr_t)proc->buffer;
2967 mutex_unlock(&binder_mmap_lock);
2969 #ifdef CONFIG_CPU_CACHE_VIPT
2970 if (cache_is_vipt_aliasing()) {
2971 while (CACHE_COLOUR((vma->vm_start ^ (uint32_t)proc->buffer))) {
2972 pr_info("binder_mmap: %d %lx-%lx maps %pK bad alignment\n", proc->pid, vma->vm_start, vma->vm_end, proc->buffer);
2973 vma->vm_start += PAGE_SIZE;
2976 #endif
2977 proc->pages = kzalloc(sizeof(proc->pages[0]) * ((vma->vm_end - vma->vm_start) / PAGE_SIZE), GFP_KERNEL);
2978 if (proc->pages == NULL) {
2979 ret = -ENOMEM;
2980 failure_string = "alloc page array";
2981 goto err_alloc_pages_failed;
2983 proc->buffer_size = vma->vm_end - vma->vm_start;
2985 vma->vm_ops = &binder_vm_ops;
2986 vma->vm_private_data = proc;
2988 if (binder_update_page_range(proc, 1, proc->buffer, proc->buffer + PAGE_SIZE, vma)) {
2989 ret = -ENOMEM;
2990 failure_string = "alloc small buf";
2991 goto err_alloc_small_buf_failed;
2993 buffer = proc->buffer;
2994 INIT_LIST_HEAD(&proc->buffers);
2995 list_add(&buffer->entry, &proc->buffers);
2996 buffer->free = 1;
2997 binder_insert_free_buffer(proc, buffer);
2998 proc->free_async_space = proc->buffer_size / 2;
2999 barrier();
3000 mutex_lock(&proc->files_lock);
3001 proc->files = get_files_struct(current);
3002 mutex_unlock(&proc->files_lock);
3003 proc->vma = vma;
3004 proc->vma_vm_mm = vma->vm_mm;
3006 /*pr_info("binder_mmap: %d %lx-%lx maps %p\n",
3007 proc->pid, vma->vm_start, vma->vm_end, proc->buffer);*/
3008 return 0;
3010 err_alloc_small_buf_failed:
3011 kfree(proc->pages);
3012 proc->pages = NULL;
3013 err_alloc_pages_failed:
3014 mutex_lock(&binder_mmap_lock);
3015 vfree(proc->buffer);
3016 proc->buffer = NULL;
3017 err_get_vm_area_failed:
3018 err_already_mapped:
3019 mutex_unlock(&binder_mmap_lock);
3020 err_bad_arg:
3021 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
3022 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
3023 return ret;
3026 static int binder_open(struct inode *nodp, struct file *filp)
3028 struct binder_proc *proc;
3030 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
3031 current->group_leader->pid, current->pid);
3033 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
3034 if (proc == NULL)
3035 return -ENOMEM;
3036 get_task_struct(current->group_leader);
3037 proc->tsk = current->group_leader;
3038 mutex_init(&proc->files_lock);
3039 INIT_LIST_HEAD(&proc->todo);
3040 init_waitqueue_head(&proc->wait);
3041 proc->default_priority = task_nice(current);
3043 binder_lock(__func__);
3045 binder_stats_created(BINDER_STAT_PROC);
3046 hlist_add_head(&proc->proc_node, &binder_procs);
3047 proc->pid = current->group_leader->pid;
3048 INIT_LIST_HEAD(&proc->delivered_death);
3049 filp->private_data = proc;
3051 binder_unlock(__func__);
3053 if (binder_debugfs_dir_entry_proc) {
3054 char strbuf[11];
3056 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
3057 proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
3058 binder_debugfs_dir_entry_proc, proc, &binder_proc_fops);
3061 return 0;
3064 static int binder_flush(struct file *filp, fl_owner_t id)
3066 struct binder_proc *proc = filp->private_data;
3068 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
3070 return 0;
3073 static void binder_deferred_flush(struct binder_proc *proc)
3075 struct rb_node *n;
3076 int wake_count = 0;
3078 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
3079 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
3081 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
3082 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
3083 wake_up_interruptible(&thread->wait);
3084 wake_count++;
3087 wake_up_interruptible_all(&proc->wait);
3089 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
3090 "binder_flush: %d woke %d threads\n", proc->pid,
3091 wake_count);
3094 static int binder_release(struct inode *nodp, struct file *filp)
3096 struct binder_proc *proc = filp->private_data;
3098 debugfs_remove(proc->debugfs_entry);
3099 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
3101 return 0;
3104 static int binder_node_release(struct binder_node *node, int refs)
3106 struct binder_ref *ref;
3107 int death = 0;
3109 list_del_init(&node->work.entry);
3110 binder_release_work(&node->async_todo);
3112 if (hlist_empty(&node->refs)) {
3113 kfree(node);
3114 binder_stats_deleted(BINDER_STAT_NODE);
3116 return refs;
3119 node->proc = NULL;
3120 node->local_strong_refs = 0;
3121 node->local_weak_refs = 0;
3122 hlist_add_head(&node->dead_node, &binder_dead_nodes);
3124 hlist_for_each_entry(ref, &node->refs, node_entry) {
3125 refs++;
3127 if (!ref->death)
3128 continue;
3130 death++;
3132 if (list_empty(&ref->death->work.entry)) {
3133 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
3134 list_add_tail(&ref->death->work.entry,
3135 &ref->proc->todo);
3136 wake_up_interruptible(&ref->proc->wait);
3137 } else
3138 BUG();
3141 binder_debug(BINDER_DEBUG_DEAD_BINDER,
3142 "node %d now dead, refs %d, death %d\n",
3143 node->debug_id, refs, death);
3145 return refs;
3148 static void binder_deferred_release(struct binder_proc *proc)
3150 struct binder_transaction *t;
3151 struct rb_node *n;
3152 int threads, nodes, incoming_refs, outgoing_refs, buffers,
3153 active_transactions, page_count;
3155 BUG_ON(proc->vma);
3156 BUG_ON(proc->files);
3158 hlist_del(&proc->proc_node);
3160 if (binder_context_mgr_node && binder_context_mgr_node->proc == proc) {
3161 binder_debug(BINDER_DEBUG_DEAD_BINDER,
3162 "%s: %d context_mgr_node gone\n",
3163 __func__, proc->pid);
3164 binder_context_mgr_node = NULL;
3167 threads = 0;
3168 active_transactions = 0;
3169 while ((n = rb_first(&proc->threads))) {
3170 struct binder_thread *thread;
3172 thread = rb_entry(n, struct binder_thread, rb_node);
3173 threads++;
3174 active_transactions += binder_free_thread(proc, thread);
3177 nodes = 0;
3178 incoming_refs = 0;
3179 while ((n = rb_first(&proc->nodes))) {
3180 struct binder_node *node;
3182 node = rb_entry(n, struct binder_node, rb_node);
3183 nodes++;
3184 rb_erase(&node->rb_node, &proc->nodes);
3185 incoming_refs = binder_node_release(node, incoming_refs);
3188 outgoing_refs = 0;
3189 while ((n = rb_first(&proc->refs_by_desc))) {
3190 struct binder_ref *ref;
3192 ref = rb_entry(n, struct binder_ref, rb_node_desc);
3193 outgoing_refs++;
3194 binder_delete_ref(ref);
3197 binder_release_work(&proc->todo);
3198 binder_release_work(&proc->delivered_death);
3200 buffers = 0;
3201 while ((n = rb_first(&proc->allocated_buffers))) {
3202 struct binder_buffer *buffer;
3204 buffer = rb_entry(n, struct binder_buffer, rb_node);
3206 t = buffer->transaction;
3207 if (t) {
3208 t->buffer = NULL;
3209 buffer->transaction = NULL;
3210 pr_err("release proc %d, transaction %d, not freed\n",
3211 proc->pid, t->debug_id);
3212 /*BUG();*/
3215 binder_free_buf(proc, buffer);
3216 buffers++;
3219 binder_stats_deleted(BINDER_STAT_PROC);
3221 page_count = 0;
3222 if (proc->pages) {
3223 int i;
3225 for (i = 0; i < proc->buffer_size / PAGE_SIZE; i++) {
3226 void *page_addr;
3228 if (!proc->pages[i])
3229 continue;
3231 page_addr = proc->buffer + i * PAGE_SIZE;
3232 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
3233 "%s: %d: page %d at %pK not freed\n",
3234 __func__, proc->pid, i, page_addr);
3235 unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
3236 __free_page(proc->pages[i]);
3237 page_count++;
3239 kfree(proc->pages);
3240 vfree(proc->buffer);
3243 put_task_struct(proc->tsk);
3245 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
3246 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d, buffers %d, pages %d\n",
3247 __func__, proc->pid, threads, nodes, incoming_refs,
3248 outgoing_refs, active_transactions, buffers, page_count);
3250 kfree(proc);
3253 static void binder_deferred_func(struct work_struct *work)
3255 struct binder_proc *proc;
3256 struct files_struct *files;
3258 int defer;
3260 do {
3261 binder_lock(__func__);
3262 mutex_lock(&binder_deferred_lock);
3263 if (!hlist_empty(&binder_deferred_list)) {
3264 proc = hlist_entry(binder_deferred_list.first,
3265 struct binder_proc, deferred_work_node);
3266 hlist_del_init(&proc->deferred_work_node);
3267 defer = proc->deferred_work;
3268 proc->deferred_work = 0;
3269 } else {
3270 proc = NULL;
3271 defer = 0;
3273 mutex_unlock(&binder_deferred_lock);
3275 files = NULL;
3276 if (defer & BINDER_DEFERRED_PUT_FILES) {
3277 mutex_lock(&proc->files_lock);
3278 files = proc->files;
3279 if (files)
3280 proc->files = NULL;
3281 mutex_unlock(&proc->files_lock);
3284 if (defer & BINDER_DEFERRED_FLUSH)
3285 binder_deferred_flush(proc);
3287 if (defer & BINDER_DEFERRED_RELEASE)
3288 binder_deferred_release(proc); /* frees proc */
3290 binder_unlock(__func__);
3291 if (files)
3292 put_files_struct(files);
3293 } while (proc);
3295 static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
3297 static void
3298 binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
3300 mutex_lock(&binder_deferred_lock);
3301 proc->deferred_work |= defer;
3302 if (hlist_unhashed(&proc->deferred_work_node)) {
3303 hlist_add_head(&proc->deferred_work_node,
3304 &binder_deferred_list);
3305 schedule_work(&binder_deferred_work);
3307 mutex_unlock(&binder_deferred_lock);
3310 static void print_binder_transaction(struct seq_file *m, const char *prefix,
3311 struct binder_transaction *t)
3313 seq_printf(m,
3314 "%s %d: %pK from %d:%d to %d:%d code %x flags %x pri %ld r%d",
3315 prefix, t->debug_id, t,
3316 t->from ? t->from->proc->pid : 0,
3317 t->from ? t->from->pid : 0,
3318 t->to_proc ? t->to_proc->pid : 0,
3319 t->to_thread ? t->to_thread->pid : 0,
3320 t->code, t->flags, t->priority, t->need_reply);
3321 if (t->buffer == NULL) {
3322 seq_puts(m, " buffer free\n");
3323 return;
3325 if (t->buffer->target_node)
3326 seq_printf(m, " node %d",
3327 t->buffer->target_node->debug_id);
3328 seq_printf(m, " size %zd:%zd data %pK\n",
3329 t->buffer->data_size, t->buffer->offsets_size,
3330 t->buffer->data);
3333 static void print_binder_buffer(struct seq_file *m, const char *prefix,
3334 struct binder_buffer *buffer)
3336 seq_printf(m, "%s %d: %pK size %zd:%zd %s\n",
3337 prefix, buffer->debug_id, buffer->data,
3338 buffer->data_size, buffer->offsets_size,
3339 buffer->transaction ? "active" : "delivered");
3342 static void print_binder_work(struct seq_file *m, const char *prefix,
3343 const char *transaction_prefix,
3344 struct binder_work *w)
3346 struct binder_node *node;
3347 struct binder_transaction *t;
3349 switch (w->type) {
3350 case BINDER_WORK_TRANSACTION:
3351 t = container_of(w, struct binder_transaction, work);
3352 print_binder_transaction(m, transaction_prefix, t);
3353 break;
3354 case BINDER_WORK_TRANSACTION_COMPLETE:
3355 seq_printf(m, "%stransaction complete\n", prefix);
3356 break;
3357 case BINDER_WORK_NODE:
3358 node = container_of(w, struct binder_node, work);
3359 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
3360 prefix, node->debug_id,
3361 (u64)node->ptr, (u64)node->cookie);
3362 break;
3363 case BINDER_WORK_DEAD_BINDER:
3364 seq_printf(m, "%shas dead binder\n", prefix);
3365 break;
3366 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
3367 seq_printf(m, "%shas cleared dead binder\n", prefix);
3368 break;
3369 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
3370 seq_printf(m, "%shas cleared death notification\n", prefix);
3371 break;
3372 default:
3373 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
3374 break;
3378 static void print_binder_thread(struct seq_file *m,
3379 struct binder_thread *thread,
3380 int print_always)
3382 struct binder_transaction *t;
3383 struct binder_work *w;
3384 size_t start_pos = m->count;
3385 size_t header_pos;
3387 seq_printf(m, " thread %d: l %02x\n", thread->pid, thread->looper);
3388 header_pos = m->count;
3389 t = thread->transaction_stack;
3390 while (t) {
3391 if (t->from == thread) {
3392 print_binder_transaction(m,
3393 " outgoing transaction", t);
3394 t = t->from_parent;
3395 } else if (t->to_thread == thread) {
3396 print_binder_transaction(m,
3397 " incoming transaction", t);
3398 t = t->to_parent;
3399 } else {
3400 print_binder_transaction(m, " bad transaction", t);
3401 t = NULL;
3404 list_for_each_entry(w, &thread->todo, entry) {
3405 print_binder_work(m, " ", " pending transaction", w);
3407 if (!print_always && m->count == header_pos)
3408 m->count = start_pos;
3411 static void print_binder_node(struct seq_file *m, struct binder_node *node)
3413 struct binder_ref *ref;
3414 struct binder_work *w;
3415 int count;
3417 count = 0;
3418 hlist_for_each_entry(ref, &node->refs, node_entry)
3419 count++;
3421 seq_printf(m, " node %d: u%016llx c%016llx hs %d hw %d ls %d lw %d is %d iw %d",
3422 node->debug_id, (u64)node->ptr, (u64)node->cookie,
3423 node->has_strong_ref, node->has_weak_ref,
3424 node->local_strong_refs, node->local_weak_refs,
3425 node->internal_strong_refs, count);
3426 if (count) {
3427 seq_puts(m, " proc");
3428 hlist_for_each_entry(ref, &node->refs, node_entry)
3429 seq_printf(m, " %d", ref->proc->pid);
3431 seq_puts(m, "\n");
3432 list_for_each_entry(w, &node->async_todo, entry)
3433 print_binder_work(m, " ",
3434 " pending async transaction", w);
3437 static void print_binder_ref(struct seq_file *m, struct binder_ref *ref)
3439 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
3440 ref->debug_id, ref->desc, ref->node->proc ? "" : "dead ",
3441 ref->node->debug_id, ref->strong, ref->weak, ref->death);
3444 static void print_binder_proc(struct seq_file *m,
3445 struct binder_proc *proc, int print_all)
3447 struct binder_work *w;
3448 struct rb_node *n;
3449 size_t start_pos = m->count;
3450 size_t header_pos;
3452 seq_printf(m, "proc %d\n", proc->pid);
3453 header_pos = m->count;
3455 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3456 print_binder_thread(m, rb_entry(n, struct binder_thread,
3457 rb_node), print_all);
3458 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
3459 struct binder_node *node = rb_entry(n, struct binder_node,
3460 rb_node);
3461 if (print_all || node->has_async_transaction)
3462 print_binder_node(m, node);
3464 if (print_all) {
3465 for (n = rb_first(&proc->refs_by_desc);
3466 n != NULL;
3467 n = rb_next(n))
3468 print_binder_ref(m, rb_entry(n, struct binder_ref,
3469 rb_node_desc));
3471 for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
3472 print_binder_buffer(m, " buffer",
3473 rb_entry(n, struct binder_buffer, rb_node));
3474 list_for_each_entry(w, &proc->todo, entry)
3475 print_binder_work(m, " ", " pending transaction", w);
3476 list_for_each_entry(w, &proc->delivered_death, entry) {
3477 seq_puts(m, " has delivered dead binder\n");
3478 break;
3480 if (!print_all && m->count == header_pos)
3481 m->count = start_pos;
3484 static const char * const binder_return_strings[] = {
3485 "BR_ERROR",
3486 "BR_OK",
3487 "BR_TRANSACTION",
3488 "BR_REPLY",
3489 "BR_ACQUIRE_RESULT",
3490 "BR_DEAD_REPLY",
3491 "BR_TRANSACTION_COMPLETE",
3492 "BR_INCREFS",
3493 "BR_ACQUIRE",
3494 "BR_RELEASE",
3495 "BR_DECREFS",
3496 "BR_ATTEMPT_ACQUIRE",
3497 "BR_NOOP",
3498 "BR_SPAWN_LOOPER",
3499 "BR_FINISHED",
3500 "BR_DEAD_BINDER",
3501 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
3502 "BR_FAILED_REPLY"
3505 static const char * const binder_command_strings[] = {
3506 "BC_TRANSACTION",
3507 "BC_REPLY",
3508 "BC_ACQUIRE_RESULT",
3509 "BC_FREE_BUFFER",
3510 "BC_INCREFS",
3511 "BC_ACQUIRE",
3512 "BC_RELEASE",
3513 "BC_DECREFS",
3514 "BC_INCREFS_DONE",
3515 "BC_ACQUIRE_DONE",
3516 "BC_ATTEMPT_ACQUIRE",
3517 "BC_REGISTER_LOOPER",
3518 "BC_ENTER_LOOPER",
3519 "BC_EXIT_LOOPER",
3520 "BC_REQUEST_DEATH_NOTIFICATION",
3521 "BC_CLEAR_DEATH_NOTIFICATION",
3522 "BC_DEAD_BINDER_DONE"
3525 static const char * const binder_objstat_strings[] = {
3526 "proc",
3527 "thread",
3528 "node",
3529 "ref",
3530 "death",
3531 "transaction",
3532 "transaction_complete"
3535 static void print_binder_stats(struct seq_file *m, const char *prefix,
3536 struct binder_stats *stats)
3538 int i;
3540 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
3541 ARRAY_SIZE(binder_command_strings));
3542 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
3543 if (stats->bc[i])
3544 seq_printf(m, "%s%s: %d\n", prefix,
3545 binder_command_strings[i], stats->bc[i]);
3548 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
3549 ARRAY_SIZE(binder_return_strings));
3550 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
3551 if (stats->br[i])
3552 seq_printf(m, "%s%s: %d\n", prefix,
3553 binder_return_strings[i], stats->br[i]);
3556 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
3557 ARRAY_SIZE(binder_objstat_strings));
3558 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
3559 ARRAY_SIZE(stats->obj_deleted));
3560 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
3561 if (stats->obj_created[i] || stats->obj_deleted[i])
3562 seq_printf(m, "%s%s: active %d total %d\n", prefix,
3563 binder_objstat_strings[i],
3564 stats->obj_created[i] - stats->obj_deleted[i],
3565 stats->obj_created[i]);
3569 static void print_binder_proc_stats(struct seq_file *m,
3570 struct binder_proc *proc)
3572 struct binder_work *w;
3573 struct rb_node *n;
3574 int count, strong, weak;
3576 seq_printf(m, "proc %d\n", proc->pid);
3577 count = 0;
3578 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3579 count++;
3580 seq_printf(m, " threads: %d\n", count);
3581 seq_printf(m, " requested threads: %d+%d/%d\n"
3582 " ready threads %d\n"
3583 " free async space %zd\n", proc->requested_threads,
3584 proc->requested_threads_started, proc->max_threads,
3585 proc->ready_threads, proc->free_async_space);
3586 count = 0;
3587 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
3588 count++;
3589 seq_printf(m, " nodes: %d\n", count);
3590 count = 0;
3591 strong = 0;
3592 weak = 0;
3593 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
3594 struct binder_ref *ref = rb_entry(n, struct binder_ref,
3595 rb_node_desc);
3596 count++;
3597 strong += ref->strong;
3598 weak += ref->weak;
3600 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
3602 count = 0;
3603 for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
3604 count++;
3605 seq_printf(m, " buffers: %d\n", count);
3607 count = 0;
3608 list_for_each_entry(w, &proc->todo, entry) {
3609 switch (w->type) {
3610 case BINDER_WORK_TRANSACTION:
3611 count++;
3612 break;
3613 default:
3614 break;
3617 seq_printf(m, " pending transactions: %d\n", count);
3619 print_binder_stats(m, " ", &proc->stats);
3623 static int binder_state_show(struct seq_file *m, void *unused)
3625 struct binder_proc *proc;
3626 struct binder_node *node;
3627 int do_lock = !binder_debug_no_lock;
3629 if (do_lock)
3630 binder_lock(__func__);
3632 seq_puts(m, "binder state:\n");
3634 if (!hlist_empty(&binder_dead_nodes))
3635 seq_puts(m, "dead nodes:\n");
3636 hlist_for_each_entry(node, &binder_dead_nodes, dead_node)
3637 print_binder_node(m, node);
3639 hlist_for_each_entry(proc, &binder_procs, proc_node)
3640 print_binder_proc(m, proc, 1);
3641 if (do_lock)
3642 binder_unlock(__func__);
3643 return 0;
3646 static int binder_stats_show(struct seq_file *m, void *unused)
3648 struct binder_proc *proc;
3649 int do_lock = !binder_debug_no_lock;
3651 if (do_lock)
3652 binder_lock(__func__);
3654 seq_puts(m, "binder stats:\n");
3656 print_binder_stats(m, "", &binder_stats);
3658 hlist_for_each_entry(proc, &binder_procs, proc_node)
3659 print_binder_proc_stats(m, proc);
3660 if (do_lock)
3661 binder_unlock(__func__);
3662 return 0;
3665 static int binder_transactions_show(struct seq_file *m, void *unused)
3667 struct binder_proc *proc;
3668 int do_lock = !binder_debug_no_lock;
3670 if (do_lock)
3671 binder_lock(__func__);
3673 seq_puts(m, "binder transactions:\n");
3674 hlist_for_each_entry(proc, &binder_procs, proc_node)
3675 print_binder_proc(m, proc, 0);
3676 if (do_lock)
3677 binder_unlock(__func__);
3678 return 0;
3681 static int binder_proc_show(struct seq_file *m, void *unused)
3683 struct binder_proc *itr;
3684 struct binder_proc *proc = m->private;
3685 int do_lock = !binder_debug_no_lock;
3686 bool valid_proc = false;
3688 if (do_lock)
3689 binder_lock(__func__);
3691 hlist_for_each_entry(itr, &binder_procs, proc_node) {
3692 if (itr == proc) {
3693 valid_proc = true;
3694 break;
3697 if (valid_proc) {
3698 seq_puts(m, "binder proc state:\n");
3699 print_binder_proc(m, proc, 1);
3701 if (do_lock)
3702 binder_unlock(__func__);
3703 return 0;
3706 static void print_binder_transaction_log_entry(struct seq_file *m,
3707 struct binder_transaction_log_entry *e)
3709 seq_printf(m,
3710 "%d: %s from %d:%d to %d:%d node %d handle %d size %d:%d\n",
3711 e->debug_id, (e->call_type == 2) ? "reply" :
3712 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
3713 e->from_thread, e->to_proc, e->to_thread, e->to_node,
3714 e->target_handle, e->data_size, e->offsets_size);
3717 static int binder_transaction_log_show(struct seq_file *m, void *unused)
3719 struct binder_transaction_log *log = m->private;
3720 int i;
3722 if (log->full) {
3723 for (i = log->next; i < ARRAY_SIZE(log->entry); i++)
3724 print_binder_transaction_log_entry(m, &log->entry[i]);
3726 for (i = 0; i < log->next; i++)
3727 print_binder_transaction_log_entry(m, &log->entry[i]);
3728 return 0;
3731 static const struct file_operations binder_fops = {
3732 .owner = THIS_MODULE,
3733 .poll = binder_poll,
3734 .unlocked_ioctl = binder_ioctl,
3735 .compat_ioctl = binder_ioctl,
3736 .mmap = binder_mmap,
3737 .open = binder_open,
3738 .flush = binder_flush,
3739 .release = binder_release,
3742 static struct miscdevice binder_miscdev = {
3743 .minor = MISC_DYNAMIC_MINOR,
3744 .name = "binder",
3745 .fops = &binder_fops
3748 BINDER_DEBUG_ENTRY(state);
3749 BINDER_DEBUG_ENTRY(stats);
3750 BINDER_DEBUG_ENTRY(transactions);
3751 BINDER_DEBUG_ENTRY(transaction_log);
3753 static int __init binder_init(void)
3755 int ret;
3757 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
3758 if (binder_debugfs_dir_entry_root)
3759 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
3760 binder_debugfs_dir_entry_root);
3761 ret = misc_register(&binder_miscdev);
3762 if (binder_debugfs_dir_entry_root) {
3763 debugfs_create_file("state",
3764 S_IRUGO,
3765 binder_debugfs_dir_entry_root,
3766 NULL,
3767 &binder_state_fops);
3768 debugfs_create_file("stats",
3769 S_IRUGO,
3770 binder_debugfs_dir_entry_root,
3771 NULL,
3772 &binder_stats_fops);
3773 debugfs_create_file("transactions",
3774 S_IRUGO,
3775 binder_debugfs_dir_entry_root,
3776 NULL,
3777 &binder_transactions_fops);
3778 debugfs_create_file("transaction_log",
3779 S_IRUGO,
3780 binder_debugfs_dir_entry_root,
3781 &binder_transaction_log,
3782 &binder_transaction_log_fops);
3783 debugfs_create_file("failed_transaction_log",
3784 S_IRUGO,
3785 binder_debugfs_dir_entry_root,
3786 &binder_transaction_log_failed,
3787 &binder_transaction_log_fops);
3789 return ret;
3792 device_initcall(binder_init);
3794 #define CREATE_TRACE_POINTS
3795 #include "binder_trace.h"
3797 MODULE_LICENSE("GPL v2");