3 * Android IPC Subsystem
5 * Copyright (C) 2007-2008 Google, Inc.
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 #include <asm/cacheflush.h>
21 #include <linux/fdtable.h>
22 #include <linux/file.h>
23 #include <linux/freezer.h>
25 #include <linux/list.h>
26 #include <linux/miscdevice.h>
28 #include <linux/module.h>
29 #include <linux/mutex.h>
30 #include <linux/nsproxy.h>
31 #include <linux/poll.h>
32 #include <linux/debugfs.h>
33 #include <linux/rbtree.h>
34 #include <linux/sched.h>
35 #include <linux/seq_file.h>
36 #include <linux/uaccess.h>
37 #include <linux/vmalloc.h>
38 #include <linux/slab.h>
39 #include <linux/pid_namespace.h>
41 #ifdef CONFIG_ANDROID_BINDER_IPC_32BIT
42 #define BINDER_IPC_32BIT 1
45 #include <uapi/linux/android/binder.h>
46 #include "binder_trace.h"
48 static DEFINE_MUTEX(binder_main_lock
);
49 static DEFINE_MUTEX(binder_deferred_lock
);
50 static DEFINE_MUTEX(binder_mmap_lock
);
52 static HLIST_HEAD(binder_procs
);
53 static HLIST_HEAD(binder_deferred_list
);
54 static HLIST_HEAD(binder_dead_nodes
);
56 static struct dentry
*binder_debugfs_dir_entry_root
;
57 static struct dentry
*binder_debugfs_dir_entry_proc
;
58 static struct binder_node
*binder_context_mgr_node
;
59 static kuid_t binder_context_mgr_uid
= INVALID_UID
;
60 static int binder_last_id
;
61 static struct workqueue_struct
*binder_deferred_workqueue
;
63 #define BINDER_DEBUG_ENTRY(name) \
64 static int binder_##name##_open(struct inode *inode, struct file *file) \
66 return single_open(file, binder_##name##_show, inode->i_private); \
69 static const struct file_operations binder_##name##_fops = { \
70 .owner = THIS_MODULE, \
71 .open = binder_##name##_open, \
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 */
86 #define SZ_4M 0x400000
89 #define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
91 #define BINDER_SMALL_BUF_SIZE (PAGE_SIZE * 64)
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
;
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
)
126 ret
= param_set_int(val
, kp
);
127 if (binder_stop_on_user_error
< 2)
128 wake_up(&binder_user_error_wait
);
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...) \
136 if (binder_debug_mask & mask) \
140 #define binder_user_error(x...) \
142 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
144 if (binder_stop_on_user_error) \
145 binder_stop_on_user_error = 2; \
148 enum binder_stat_types
{
154 BINDER_STAT_TRANSACTION
,
155 BINDER_STAT_TRANSACTION_COMPLETE
,
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
{
190 struct binder_transaction_log
{
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
));
206 if (log
->next
== ARRAY_SIZE(log
->entry
)) {
214 struct list_head entry
;
216 BINDER_WORK_TRANSACTION
= 1,
217 BINDER_WORK_TRANSACTION_COMPLETE
,
219 BINDER_WORK_DEAD_BINDER
,
220 BINDER_WORK_DEAD_BINDER_AND_CLEAR
,
221 BINDER_WORK_CLEAR_DEATH_NOTIFICATION
,
227 struct binder_work work
;
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
;
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
;
255 /* Lookups needed: */
256 /* node + proc => ref (transaction) */
257 /* desc + proc => ref (transaction, inc/dec ref) */
258 /* node => refs + procs (proc exit) */
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
;
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 */
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
;
288 enum binder_deferred_state
{
289 BINDER_DEFERRED_PUT_FILES
= 0x01,
290 BINDER_DEFERRED_FLUSH
= 0x02,
291 BINDER_DEFERRED_RELEASE
= 0x04,
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
;
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 hlist_node deferred_work_node
;
308 ptrdiff_t user_buffer_offset
;
310 struct list_head buffers
;
311 struct rb_root free_buffers
;
312 struct rb_root allocated_buffers
;
313 size_t free_async_space
;
317 uint32_t buffer_free
;
318 struct list_head todo
;
319 wait_queue_head_t wait
;
320 struct binder_stats stats
;
321 struct list_head delivered_death
;
323 int requested_threads
;
324 int requested_threads_started
;
326 long default_priority
;
327 struct dentry
*debugfs_entry
;
331 BINDER_LOOPER_STATE_REGISTERED
= 0x01,
332 BINDER_LOOPER_STATE_ENTERED
= 0x02,
333 BINDER_LOOPER_STATE_EXITED
= 0x04,
334 BINDER_LOOPER_STATE_INVALID
= 0x08,
335 BINDER_LOOPER_STATE_WAITING
= 0x10,
336 BINDER_LOOPER_STATE_NEED_RETURN
= 0x20
339 struct binder_thread
{
340 struct binder_proc
*proc
;
341 struct rb_node rb_node
;
344 struct binder_transaction
*transaction_stack
;
345 struct list_head todo
;
346 uint32_t return_error
; /* Write failed, return error code in read buf */
347 uint32_t return_error2
; /* Write failed, return error code in read */
348 /* buffer. Used when sending a reply to a dead process that */
349 /* we are also waiting on */
350 wait_queue_head_t wait
;
351 struct binder_stats stats
;
354 struct binder_transaction
{
356 struct binder_work work
;
357 struct binder_thread
*from
;
358 struct binder_transaction
*from_parent
;
359 struct binder_proc
*to_proc
;
360 struct binder_thread
*to_thread
;
361 struct binder_transaction
*to_parent
;
362 unsigned need_reply
:1;
363 /* unsigned is_dead:1; */ /* not used at the moment */
365 struct binder_buffer
*buffer
;
374 binder_defer_work(struct binder_proc
*proc
, enum binder_deferred_state defer
);
376 static int task_get_unused_fd_flags(struct binder_proc
*proc
, int flags
)
378 struct files_struct
*files
= proc
->files
;
379 unsigned long rlim_cur
;
385 if (!lock_task_sighand(proc
->tsk
, &irqs
))
388 rlim_cur
= task_rlimit(proc
->tsk
, RLIMIT_NOFILE
);
389 unlock_task_sighand(proc
->tsk
, &irqs
);
391 return __alloc_fd(files
, 0, rlim_cur
, flags
);
395 * copied from fd_install
397 static void task_fd_install(
398 struct binder_proc
*proc
, unsigned int fd
, struct file
*file
)
401 __fd_install(proc
->files
, fd
, file
);
405 * copied from sys_close
407 static long task_close_fd(struct binder_proc
*proc
, unsigned int fd
)
411 if (proc
->files
== NULL
)
414 retval
= __close_fd(proc
->files
, fd
);
415 /* can't restart close syscall because file table entry was cleared */
416 if (unlikely(retval
== -ERESTARTSYS
||
417 retval
== -ERESTARTNOINTR
||
418 retval
== -ERESTARTNOHAND
||
419 retval
== -ERESTART_RESTARTBLOCK
))
425 static inline void binder_lock(const char *tag
)
427 trace_binder_lock(tag
);
428 mutex_lock(&binder_main_lock
);
429 trace_binder_locked(tag
);
432 static inline void binder_unlock(const char *tag
)
434 trace_binder_unlock(tag
);
435 mutex_unlock(&binder_main_lock
);
438 static void binder_set_nice(long nice
)
442 if (can_nice(current
, nice
)) {
443 set_user_nice(current
, nice
);
446 min_nice
= 20 - current
->signal
->rlim
[RLIMIT_NICE
].rlim_cur
;
447 binder_debug(BINDER_DEBUG_PRIORITY_CAP
,
448 "%d: nice value %ld not allowed use %ld instead\n",
449 current
->pid
, nice
, min_nice
);
450 set_user_nice(current
, min_nice
);
453 binder_user_error("%d RLIMIT_NICE not set\n", current
->pid
);
456 static size_t binder_buffer_size(struct binder_proc
*proc
,
457 struct binder_buffer
*buffer
)
459 if (list_is_last(&buffer
->entry
, &proc
->buffers
))
460 return proc
->buffer
+ proc
->buffer_size
- (void *)buffer
->data
;
461 return (size_t)list_entry(buffer
->entry
.next
,
462 struct binder_buffer
, entry
) - (size_t)buffer
->data
;
465 static void binder_insert_free_buffer(struct binder_proc
*proc
,
466 struct binder_buffer
*new_buffer
)
468 struct rb_node
**p
= &proc
->free_buffers
.rb_node
;
469 struct rb_node
*parent
= NULL
;
470 struct binder_buffer
*buffer
;
472 size_t new_buffer_size
;
474 BUG_ON(!new_buffer
->free
);
476 new_buffer_size
= binder_buffer_size(proc
, new_buffer
);
478 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
479 "%d: add free buffer, size %zd, at %pK\n",
480 proc
->pid
, new_buffer_size
, new_buffer
);
484 buffer
= rb_entry(parent
, struct binder_buffer
, rb_node
);
485 BUG_ON(!buffer
->free
);
487 buffer_size
= binder_buffer_size(proc
, buffer
);
489 if (new_buffer_size
< buffer_size
)
490 p
= &parent
->rb_left
;
492 p
= &parent
->rb_right
;
494 rb_link_node(&new_buffer
->rb_node
, parent
, p
);
495 rb_insert_color(&new_buffer
->rb_node
, &proc
->free_buffers
);
498 static void binder_insert_allocated_buffer(struct binder_proc
*proc
,
499 struct binder_buffer
*new_buffer
)
501 struct rb_node
**p
= &proc
->allocated_buffers
.rb_node
;
502 struct rb_node
*parent
= NULL
;
503 struct binder_buffer
*buffer
;
505 BUG_ON(new_buffer
->free
);
509 buffer
= rb_entry(parent
, struct binder_buffer
, rb_node
);
510 BUG_ON(buffer
->free
);
512 if (new_buffer
< buffer
)
513 p
= &parent
->rb_left
;
514 else if (new_buffer
> buffer
)
515 p
= &parent
->rb_right
;
519 rb_link_node(&new_buffer
->rb_node
, parent
, p
);
520 rb_insert_color(&new_buffer
->rb_node
, &proc
->allocated_buffers
);
523 static struct binder_buffer
*binder_buffer_lookup(struct binder_proc
*proc
,
526 struct rb_node
*n
= proc
->allocated_buffers
.rb_node
;
527 struct binder_buffer
*buffer
;
528 struct binder_buffer
*kern_ptr
;
530 kern_ptr
= (struct binder_buffer
*)(user_ptr
- proc
->user_buffer_offset
531 - offsetof(struct binder_buffer
, data
));
534 buffer
= rb_entry(n
, struct binder_buffer
, rb_node
);
535 BUG_ON(buffer
->free
);
537 if (kern_ptr
< buffer
)
539 else if (kern_ptr
> buffer
)
547 static int binder_update_page_range(struct binder_proc
*proc
, int allocate
,
548 void *start
, void *end
,
549 struct vm_area_struct
*vma
)
552 unsigned long user_page_addr
;
553 struct vm_struct tmp_area
;
555 struct mm_struct
*mm
;
557 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
558 "%d: %s pages %pK-%pK\n", proc
->pid
,
559 allocate
? "allocate" : "free", start
, end
);
564 trace_binder_update_page_range(proc
, allocate
, start
, end
);
569 mm
= get_task_mm(proc
->tsk
);
572 down_write(&mm
->mmap_sem
);
574 if (vma
&& mm
!= proc
->vma_vm_mm
) {
575 pr_err("%d: vma mm and task mm mismatch\n",
585 pr_err("%d: binder_alloc_buf failed to map pages in userspace, no vma\n",
590 for (page_addr
= start
; page_addr
< end
; page_addr
+= PAGE_SIZE
) {
593 page
= &proc
->pages
[(page_addr
- proc
->buffer
) / PAGE_SIZE
];
596 *page
= alloc_page(GFP_KERNEL
| __GFP_HIGHMEM
| __GFP_ZERO
);
598 pr_err("%d: binder_alloc_buf failed for page at %pK\n",
599 proc
->pid
, page_addr
);
600 goto err_alloc_page_failed
;
602 tmp_area
.addr
= page_addr
;
603 tmp_area
.size
= PAGE_SIZE
+ PAGE_SIZE
/* guard page? */;
604 ret
= map_vm_area(&tmp_area
, PAGE_KERNEL
, page
);
606 pr_err("%d: binder_alloc_buf failed to map page at %pK in kernel\n",
607 proc
->pid
, page_addr
);
608 goto err_map_kernel_failed
;
611 (uintptr_t)page_addr
+ proc
->user_buffer_offset
;
612 ret
= vm_insert_page(vma
, user_page_addr
, page
[0]);
614 pr_err("%d: binder_alloc_buf failed to map page at %lx in userspace\n",
615 proc
->pid
, user_page_addr
);
616 goto err_vm_insert_page_failed
;
618 /* vm_insert_page does not seem to increment the refcount */
621 up_write(&mm
->mmap_sem
);
627 for (page_addr
= end
- PAGE_SIZE
; page_addr
>= start
;
628 page_addr
-= PAGE_SIZE
) {
629 page
= &proc
->pages
[(page_addr
- proc
->buffer
) / PAGE_SIZE
];
631 zap_page_range(vma
, (uintptr_t)page_addr
+
632 proc
->user_buffer_offset
, PAGE_SIZE
, NULL
);
633 err_vm_insert_page_failed
:
634 unmap_kernel_range((unsigned long)page_addr
, PAGE_SIZE
);
635 err_map_kernel_failed
:
638 err_alloc_page_failed
:
643 up_write(&mm
->mmap_sem
);
649 static struct binder_buffer
*binder_alloc_buf(struct binder_proc
*proc
,
651 size_t offsets_size
, int is_async
)
653 struct rb_node
*n
= proc
->free_buffers
.rb_node
;
654 struct binder_buffer
*buffer
;
656 struct rb_node
*best_fit
= NULL
;
661 if (proc
->vma
== NULL
) {
662 pr_err("%d: binder_alloc_buf, no vma\n",
667 size
= ALIGN(data_size
, sizeof(void *)) +
668 ALIGN(offsets_size
, sizeof(void *));
670 if (size
< data_size
|| size
< offsets_size
) {
671 binder_user_error("%d: got transaction with invalid size %zd-%zd\n",
672 proc
->pid
, data_size
, offsets_size
);
677 proc
->free_async_space
< size
+ sizeof(struct binder_buffer
)) {
678 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
679 "%d: binder_alloc_buf size %zd failed, no async space left\n",
685 buffer
= rb_entry(n
, struct binder_buffer
, rb_node
);
686 BUG_ON(!buffer
->free
);
687 buffer_size
= binder_buffer_size(proc
, buffer
);
689 if (size
< buffer_size
) {
692 } else if (size
> buffer_size
)
699 if (best_fit
== NULL
) {
700 pr_err("%d: binder_alloc_buf size %zd failed, no address space\n",
705 buffer
= rb_entry(best_fit
, struct binder_buffer
, rb_node
);
706 buffer_size
= binder_buffer_size(proc
, buffer
);
709 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
710 "%d: binder_alloc_buf size %zd got buffer %pK size %zd\n",
711 proc
->pid
, size
, buffer
, buffer_size
);
714 (void *)(((uintptr_t)buffer
->data
+ buffer_size
) & PAGE_MASK
);
716 if (size
+ sizeof(struct binder_buffer
) + 4 >= buffer_size
)
717 buffer_size
= size
; /* no room for other buffers */
719 buffer_size
= size
+ sizeof(struct binder_buffer
);
722 (void *)PAGE_ALIGN((uintptr_t)buffer
->data
+ buffer_size
);
723 if (end_page_addr
> has_page_addr
)
724 end_page_addr
= has_page_addr
;
725 if (binder_update_page_range(proc
, 1,
726 (void *)PAGE_ALIGN((uintptr_t)buffer
->data
), end_page_addr
, NULL
))
729 rb_erase(best_fit
, &proc
->free_buffers
);
731 binder_insert_allocated_buffer(proc
, buffer
);
732 if (buffer_size
!= size
) {
733 struct binder_buffer
*new_buffer
= (void *)buffer
->data
+ size
;
735 list_add(&new_buffer
->entry
, &buffer
->entry
);
736 new_buffer
->free
= 1;
737 binder_insert_free_buffer(proc
, new_buffer
);
739 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
740 "%d: binder_alloc_buf size %zd got %pK\n",
741 proc
->pid
, size
, buffer
);
742 buffer
->data_size
= data_size
;
743 buffer
->offsets_size
= offsets_size
;
744 buffer
->async_transaction
= is_async
;
746 proc
->free_async_space
-= size
+ sizeof(struct binder_buffer
);
747 binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC
,
748 "%d: binder_alloc_buf size %zd async free %zd\n",
749 proc
->pid
, size
, proc
->free_async_space
);
755 static void *buffer_start_page(struct binder_buffer
*buffer
)
757 return (void *)((uintptr_t)buffer
& PAGE_MASK
);
760 static void *buffer_end_page(struct binder_buffer
*buffer
)
762 return (void *)(((uintptr_t)(buffer
+ 1) - 1) & PAGE_MASK
);
765 static void binder_delete_free_buffer(struct binder_proc
*proc
,
766 struct binder_buffer
*buffer
)
768 struct binder_buffer
*prev
, *next
= NULL
;
769 int free_page_end
= 1;
770 int free_page_start
= 1;
772 BUG_ON(proc
->buffers
.next
== &buffer
->entry
);
773 prev
= list_entry(buffer
->entry
.prev
, struct binder_buffer
, entry
);
775 if (buffer_end_page(prev
) == buffer_start_page(buffer
)) {
777 if (buffer_end_page(prev
) == buffer_end_page(buffer
))
779 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
780 "%d: merge free, buffer %pK share page with %pK\n",
781 proc
->pid
, buffer
, prev
);
784 if (!list_is_last(&buffer
->entry
, &proc
->buffers
)) {
785 next
= list_entry(buffer
->entry
.next
,
786 struct binder_buffer
, entry
);
787 if (buffer_start_page(next
) == buffer_end_page(buffer
)) {
789 if (buffer_start_page(next
) ==
790 buffer_start_page(buffer
))
792 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
793 "%d: merge free, buffer %pK share page with %pK\n",
794 proc
->pid
, buffer
, prev
);
797 list_del(&buffer
->entry
);
798 if (free_page_start
|| free_page_end
) {
799 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
800 "%d: merge free, buffer %pK do not share page%s%s with %pK or %pK\n",
801 proc
->pid
, buffer
, free_page_start
? "" : " end",
802 free_page_end
? "" : " start", prev
, next
);
803 binder_update_page_range(proc
, 0, free_page_start
?
804 buffer_start_page(buffer
) : buffer_end_page(buffer
),
805 (free_page_end
? buffer_end_page(buffer
) :
806 buffer_start_page(buffer
)) + PAGE_SIZE
, NULL
);
810 static void binder_free_buf(struct binder_proc
*proc
,
811 struct binder_buffer
*buffer
)
813 size_t size
, buffer_size
;
815 buffer_size
= binder_buffer_size(proc
, buffer
);
817 size
= ALIGN(buffer
->data_size
, sizeof(void *)) +
818 ALIGN(buffer
->offsets_size
, sizeof(void *));
820 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
821 "%d: binder_free_buf %pK size %zd buffer_size %zd\n",
822 proc
->pid
, buffer
, size
, buffer_size
);
824 BUG_ON(buffer
->free
);
825 BUG_ON(size
> buffer_size
);
826 BUG_ON(buffer
->transaction
!= NULL
);
827 BUG_ON((void *)buffer
< proc
->buffer
);
828 BUG_ON((void *)buffer
> proc
->buffer
+ proc
->buffer_size
);
830 if (buffer
->async_transaction
) {
831 proc
->free_async_space
+= size
+ sizeof(struct binder_buffer
);
833 binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC
,
834 "%d: binder_free_buf size %zd async free %zd\n",
835 proc
->pid
, size
, proc
->free_async_space
);
838 binder_update_page_range(proc
, 0,
839 (void *)PAGE_ALIGN((uintptr_t)buffer
->data
),
840 (void *)(((uintptr_t)buffer
->data
+ buffer_size
) & PAGE_MASK
),
842 rb_erase(&buffer
->rb_node
, &proc
->allocated_buffers
);
844 if (!list_is_last(&buffer
->entry
, &proc
->buffers
)) {
845 struct binder_buffer
*next
= list_entry(buffer
->entry
.next
,
846 struct binder_buffer
, entry
);
849 rb_erase(&next
->rb_node
, &proc
->free_buffers
);
850 binder_delete_free_buffer(proc
, next
);
853 if (proc
->buffers
.next
!= &buffer
->entry
) {
854 struct binder_buffer
*prev
= list_entry(buffer
->entry
.prev
,
855 struct binder_buffer
, entry
);
858 binder_delete_free_buffer(proc
, buffer
);
859 rb_erase(&prev
->rb_node
, &proc
->free_buffers
);
863 binder_insert_free_buffer(proc
, buffer
);
866 static struct binder_node
*binder_get_node(struct binder_proc
*proc
,
867 binder_uintptr_t ptr
)
869 struct rb_node
*n
= proc
->nodes
.rb_node
;
870 struct binder_node
*node
;
873 node
= rb_entry(n
, struct binder_node
, rb_node
);
877 else if (ptr
> node
->ptr
)
885 static struct binder_node
*binder_new_node(struct binder_proc
*proc
,
886 binder_uintptr_t ptr
,
887 binder_uintptr_t cookie
)
889 struct rb_node
**p
= &proc
->nodes
.rb_node
;
890 struct rb_node
*parent
= NULL
;
891 struct binder_node
*node
;
895 node
= rb_entry(parent
, struct binder_node
, rb_node
);
899 else if (ptr
> node
->ptr
)
905 node
= kzalloc(sizeof(*node
), GFP_KERNEL
);
908 binder_stats_created(BINDER_STAT_NODE
);
909 rb_link_node(&node
->rb_node
, parent
, p
);
910 rb_insert_color(&node
->rb_node
, &proc
->nodes
);
911 node
->debug_id
= ++binder_last_id
;
914 node
->cookie
= cookie
;
915 node
->work
.type
= BINDER_WORK_NODE
;
916 INIT_LIST_HEAD(&node
->work
.entry
);
917 INIT_LIST_HEAD(&node
->async_todo
);
918 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
919 "%d:%d node %d u%016llx c%016llx created\n",
920 proc
->pid
, current
->pid
, node
->debug_id
,
921 (u64
)node
->ptr
, (u64
)node
->cookie
);
925 static int binder_inc_node(struct binder_node
*node
, int strong
, int internal
,
926 struct list_head
*target_list
)
930 if (target_list
== NULL
&&
931 node
->internal_strong_refs
== 0 &&
932 !(node
== binder_context_mgr_node
&&
933 node
->has_strong_ref
)) {
934 pr_err("invalid inc strong node for %d\n",
938 node
->internal_strong_refs
++;
940 node
->local_strong_refs
++;
941 if (!node
->has_strong_ref
&& target_list
) {
942 list_del_init(&node
->work
.entry
);
943 list_add_tail(&node
->work
.entry
, target_list
);
947 node
->local_weak_refs
++;
948 if (!node
->has_weak_ref
&& list_empty(&node
->work
.entry
)) {
949 if (target_list
== NULL
) {
950 pr_err("invalid inc weak node for %d\n",
954 list_add_tail(&node
->work
.entry
, target_list
);
960 static int binder_dec_node(struct binder_node
*node
, int strong
, int internal
)
964 node
->internal_strong_refs
--;
966 node
->local_strong_refs
--;
967 if (node
->local_strong_refs
|| node
->internal_strong_refs
)
971 node
->local_weak_refs
--;
972 if (node
->local_weak_refs
|| !hlist_empty(&node
->refs
))
975 if (node
->proc
&& (node
->has_strong_ref
|| node
->has_weak_ref
)) {
976 if (list_empty(&node
->work
.entry
)) {
977 list_add_tail(&node
->work
.entry
, &node
->proc
->todo
);
978 wake_up_interruptible(&node
->proc
->wait
);
981 if (hlist_empty(&node
->refs
) && !node
->local_strong_refs
&&
982 !node
->local_weak_refs
) {
983 list_del_init(&node
->work
.entry
);
985 rb_erase(&node
->rb_node
, &node
->proc
->nodes
);
986 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
987 "refless node %d deleted\n",
990 hlist_del(&node
->dead_node
);
991 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
992 "dead node %d deleted\n",
996 binder_stats_deleted(BINDER_STAT_NODE
);
1004 static struct binder_ref
*binder_get_ref(struct binder_proc
*proc
,
1005 uint32_t desc
, bool need_strong_ref
)
1007 struct rb_node
*n
= proc
->refs_by_desc
.rb_node
;
1008 struct binder_ref
*ref
;
1011 ref
= rb_entry(n
, struct binder_ref
, rb_node_desc
);
1013 if (desc
< ref
->desc
) {
1015 } else if (desc
> ref
->desc
) {
1017 } else if (need_strong_ref
&& !ref
->strong
) {
1018 binder_user_error("tried to use weak ref as strong ref\n");
1027 static struct binder_ref
*binder_get_ref_for_node(struct binder_proc
*proc
,
1028 struct binder_node
*node
)
1031 struct rb_node
**p
= &proc
->refs_by_node
.rb_node
;
1032 struct rb_node
*parent
= NULL
;
1033 struct binder_ref
*ref
, *new_ref
;
1037 ref
= rb_entry(parent
, struct binder_ref
, rb_node_node
);
1039 if (node
< ref
->node
)
1041 else if (node
> ref
->node
)
1042 p
= &(*p
)->rb_right
;
1046 new_ref
= kzalloc(sizeof(*ref
), GFP_KERNEL
);
1047 if (new_ref
== NULL
)
1049 binder_stats_created(BINDER_STAT_REF
);
1050 new_ref
->debug_id
= ++binder_last_id
;
1051 new_ref
->proc
= proc
;
1052 new_ref
->node
= node
;
1053 rb_link_node(&new_ref
->rb_node_node
, parent
, p
);
1054 rb_insert_color(&new_ref
->rb_node_node
, &proc
->refs_by_node
);
1056 new_ref
->desc
= (node
== binder_context_mgr_node
) ? 0 : 1;
1057 for (n
= rb_first(&proc
->refs_by_desc
); n
!= NULL
; n
= rb_next(n
)) {
1058 ref
= rb_entry(n
, struct binder_ref
, rb_node_desc
);
1059 if (ref
->desc
> new_ref
->desc
)
1061 new_ref
->desc
= ref
->desc
+ 1;
1064 p
= &proc
->refs_by_desc
.rb_node
;
1067 ref
= rb_entry(parent
, struct binder_ref
, rb_node_desc
);
1069 if (new_ref
->desc
< ref
->desc
)
1071 else if (new_ref
->desc
> ref
->desc
)
1072 p
= &(*p
)->rb_right
;
1076 rb_link_node(&new_ref
->rb_node_desc
, parent
, p
);
1077 rb_insert_color(&new_ref
->rb_node_desc
, &proc
->refs_by_desc
);
1079 hlist_add_head(&new_ref
->node_entry
, &node
->refs
);
1081 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1082 "%d new ref %d desc %d for node %d\n",
1083 proc
->pid
, new_ref
->debug_id
, new_ref
->desc
,
1086 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1087 "%d new ref %d desc %d for dead node\n",
1088 proc
->pid
, new_ref
->debug_id
, new_ref
->desc
);
1093 static void binder_delete_ref(struct binder_ref
*ref
)
1095 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1096 "%d delete ref %d desc %d for node %d\n",
1097 ref
->proc
->pid
, ref
->debug_id
, ref
->desc
,
1098 ref
->node
->debug_id
);
1100 rb_erase(&ref
->rb_node_desc
, &ref
->proc
->refs_by_desc
);
1101 rb_erase(&ref
->rb_node_node
, &ref
->proc
->refs_by_node
);
1103 binder_dec_node(ref
->node
, 1, 1);
1104 hlist_del(&ref
->node_entry
);
1105 binder_dec_node(ref
->node
, 0, 1);
1107 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
1108 "%d delete ref %d desc %d has death notification\n",
1109 ref
->proc
->pid
, ref
->debug_id
, ref
->desc
);
1110 list_del(&ref
->death
->work
.entry
);
1112 binder_stats_deleted(BINDER_STAT_DEATH
);
1115 binder_stats_deleted(BINDER_STAT_REF
);
1118 static int binder_inc_ref(struct binder_ref
*ref
, int strong
,
1119 struct list_head
*target_list
)
1124 if (ref
->strong
== 0) {
1125 ret
= binder_inc_node(ref
->node
, 1, 1, target_list
);
1131 if (ref
->weak
== 0) {
1132 ret
= binder_inc_node(ref
->node
, 0, 1, target_list
);
1142 static int binder_dec_ref(struct binder_ref
**ptr_to_ref
, int strong
)
1144 struct binder_ref
*ref
= *ptr_to_ref
;
1146 if (ref
->strong
== 0) {
1147 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
1148 ref
->proc
->pid
, ref
->debug_id
,
1149 ref
->desc
, ref
->strong
, ref
->weak
);
1153 if (ref
->strong
== 0) {
1156 ret
= binder_dec_node(ref
->node
, strong
, 1);
1161 if (ref
->weak
== 0) {
1162 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
1163 ref
->proc
->pid
, ref
->debug_id
,
1164 ref
->desc
, ref
->strong
, ref
->weak
);
1169 if (ref
->strong
== 0 && ref
->weak
== 0) {
1170 binder_delete_ref(ref
);
1176 static void binder_pop_transaction(struct binder_thread
*target_thread
,
1177 struct binder_transaction
*t
)
1179 if (target_thread
) {
1180 BUG_ON(target_thread
->transaction_stack
!= t
);
1181 BUG_ON(target_thread
->transaction_stack
->from
!= target_thread
);
1182 target_thread
->transaction_stack
=
1183 target_thread
->transaction_stack
->from_parent
;
1188 t
->buffer
->transaction
= NULL
;
1190 binder_stats_deleted(BINDER_STAT_TRANSACTION
);
1193 static void binder_send_failed_reply(struct binder_transaction
*t
,
1194 uint32_t error_code
)
1196 struct binder_thread
*target_thread
;
1197 struct binder_transaction
*next
;
1199 BUG_ON(t
->flags
& TF_ONE_WAY
);
1201 target_thread
= t
->from
;
1202 if (target_thread
) {
1203 if (target_thread
->return_error
!= BR_OK
&&
1204 target_thread
->return_error2
== BR_OK
) {
1205 target_thread
->return_error2
=
1206 target_thread
->return_error
;
1207 target_thread
->return_error
= BR_OK
;
1209 if (target_thread
->return_error
== BR_OK
) {
1210 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
1211 "send failed reply for transaction %d to %d:%d\n",
1213 target_thread
->proc
->pid
,
1214 target_thread
->pid
);
1216 binder_pop_transaction(target_thread
, t
);
1217 target_thread
->return_error
= error_code
;
1218 wake_up_interruptible(&target_thread
->wait
);
1220 pr_err("reply failed, target thread, %d:%d, has error code %d already\n",
1221 target_thread
->proc
->pid
,
1223 target_thread
->return_error
);
1227 next
= t
->from_parent
;
1229 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
1230 "send failed reply for transaction %d, target dead\n",
1233 binder_pop_transaction(target_thread
, t
);
1235 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
1236 "reply failed, no target thread at root\n");
1240 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
1241 "reply failed, no target thread -- retry %d\n",
1246 static void binder_transaction_buffer_release(struct binder_proc
*proc
,
1247 struct binder_buffer
*buffer
,
1248 binder_size_t
*failed_at
)
1250 binder_size_t
*offp
, *off_end
;
1251 int debug_id
= buffer
->debug_id
;
1253 binder_debug(BINDER_DEBUG_TRANSACTION
,
1254 "%d buffer release %d, size %zd-%zd, failed at %pK\n",
1255 proc
->pid
, buffer
->debug_id
,
1256 buffer
->data_size
, buffer
->offsets_size
, failed_at
);
1258 if (buffer
->target_node
)
1259 binder_dec_node(buffer
->target_node
, 1, 0);
1261 offp
= (binder_size_t
*)(buffer
->data
+
1262 ALIGN(buffer
->data_size
, sizeof(void *)));
1264 off_end
= failed_at
;
1266 off_end
= (void *)offp
+ buffer
->offsets_size
;
1267 for (; offp
< off_end
; offp
++) {
1268 struct flat_binder_object
*fp
;
1270 if (*offp
> buffer
->data_size
- sizeof(*fp
) ||
1271 buffer
->data_size
< sizeof(*fp
) ||
1272 !IS_ALIGNED(*offp
, sizeof(u32
))) {
1273 pr_err("transaction release %d bad offset %lld, size %zd\n",
1274 debug_id
, (u64
)*offp
, buffer
->data_size
);
1277 fp
= (struct flat_binder_object
*)(buffer
->data
+ *offp
);
1279 case BINDER_TYPE_BINDER
:
1280 case BINDER_TYPE_WEAK_BINDER
: {
1281 struct binder_node
*node
= binder_get_node(proc
, fp
->binder
);
1284 pr_err("transaction release %d bad node %016llx\n",
1285 debug_id
, (u64
)fp
->binder
);
1288 binder_debug(BINDER_DEBUG_TRANSACTION
,
1289 " node %d u%016llx\n",
1290 node
->debug_id
, (u64
)node
->ptr
);
1291 binder_dec_node(node
, fp
->type
== BINDER_TYPE_BINDER
, 0);
1293 case BINDER_TYPE_HANDLE
:
1294 case BINDER_TYPE_WEAK_HANDLE
: {
1295 struct binder_ref
*ref
= binder_get_ref(proc
, fp
->handle
,
1296 fp
->type
== BINDER_TYPE_HANDLE
);
1299 pr_err("transaction release %d bad handle %d\n",
1300 debug_id
, fp
->handle
);
1303 binder_debug(BINDER_DEBUG_TRANSACTION
,
1304 " ref %d desc %d (node %d)\n",
1305 ref
->debug_id
, ref
->desc
, ref
->node
->debug_id
);
1306 binder_dec_ref(&ref
, fp
->type
== BINDER_TYPE_HANDLE
);
1309 case BINDER_TYPE_FD
:
1310 binder_debug(BINDER_DEBUG_TRANSACTION
,
1311 " fd %d\n", fp
->handle
);
1313 task_close_fd(proc
, fp
->handle
);
1317 pr_err("transaction release %d bad object type %x\n",
1318 debug_id
, fp
->type
);
1324 static void binder_transaction(struct binder_proc
*proc
,
1325 struct binder_thread
*thread
,
1326 struct binder_transaction_data
*tr
, int reply
)
1328 struct binder_transaction
*t
;
1329 struct binder_work
*tcomplete
;
1330 binder_size_t
*offp
, *off_end
;
1331 binder_size_t off_min
;
1332 struct binder_proc
*target_proc
;
1333 struct binder_thread
*target_thread
= NULL
;
1334 struct binder_node
*target_node
= NULL
;
1335 struct list_head
*target_list
;
1336 wait_queue_head_t
*target_wait
;
1337 struct binder_transaction
*in_reply_to
= NULL
;
1338 struct binder_transaction_log_entry
*e
;
1339 uint32_t return_error
;
1341 e
= binder_transaction_log_add(&binder_transaction_log
);
1342 e
->call_type
= reply
? 2 : !!(tr
->flags
& TF_ONE_WAY
);
1343 e
->from_proc
= proc
->pid
;
1344 e
->from_thread
= thread
->pid
;
1345 e
->target_handle
= tr
->target
.handle
;
1346 e
->data_size
= tr
->data_size
;
1347 e
->offsets_size
= tr
->offsets_size
;
1350 in_reply_to
= thread
->transaction_stack
;
1351 if (in_reply_to
== NULL
) {
1352 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
1353 proc
->pid
, thread
->pid
);
1354 return_error
= BR_FAILED_REPLY
;
1355 goto err_empty_call_stack
;
1357 binder_set_nice(in_reply_to
->saved_priority
);
1358 if (in_reply_to
->to_thread
!= thread
) {
1359 binder_user_error("%d:%d got reply transaction with bad transaction stack, transaction %d has target %d:%d\n",
1360 proc
->pid
, thread
->pid
, in_reply_to
->debug_id
,
1361 in_reply_to
->to_proc
?
1362 in_reply_to
->to_proc
->pid
: 0,
1363 in_reply_to
->to_thread
?
1364 in_reply_to
->to_thread
->pid
: 0);
1365 return_error
= BR_FAILED_REPLY
;
1367 goto err_bad_call_stack
;
1369 thread
->transaction_stack
= in_reply_to
->to_parent
;
1370 target_thread
= in_reply_to
->from
;
1371 if (target_thread
== NULL
) {
1372 return_error
= BR_DEAD_REPLY
;
1373 goto err_dead_binder
;
1375 if (target_thread
->transaction_stack
!= in_reply_to
) {
1376 binder_user_error("%d:%d got reply transaction with bad target transaction stack %d, expected %d\n",
1377 proc
->pid
, thread
->pid
,
1378 target_thread
->transaction_stack
?
1379 target_thread
->transaction_stack
->debug_id
: 0,
1380 in_reply_to
->debug_id
);
1381 return_error
= BR_FAILED_REPLY
;
1383 target_thread
= NULL
;
1384 goto err_dead_binder
;
1386 target_proc
= target_thread
->proc
;
1388 if (tr
->target
.handle
) {
1389 struct binder_ref
*ref
;
1391 ref
= binder_get_ref(proc
, tr
->target
.handle
, true);
1393 binder_user_error("%d:%d got transaction to invalid handle\n",
1394 proc
->pid
, thread
->pid
);
1395 return_error
= BR_FAILED_REPLY
;
1396 goto err_invalid_target_handle
;
1398 target_node
= ref
->node
;
1400 target_node
= binder_context_mgr_node
;
1401 if (target_node
== NULL
) {
1402 return_error
= BR_DEAD_REPLY
;
1403 goto err_no_context_mgr_node
;
1406 e
->to_node
= target_node
->debug_id
;
1407 target_proc
= target_node
->proc
;
1408 if (target_proc
== NULL
) {
1409 return_error
= BR_DEAD_REPLY
;
1410 goto err_dead_binder
;
1412 if (!(tr
->flags
& TF_ONE_WAY
) && thread
->transaction_stack
) {
1413 struct binder_transaction
*tmp
;
1415 tmp
= thread
->transaction_stack
;
1416 if (tmp
->to_thread
!= thread
) {
1417 binder_user_error("%d:%d got new transaction with bad transaction stack, transaction %d has target %d:%d\n",
1418 proc
->pid
, thread
->pid
, tmp
->debug_id
,
1419 tmp
->to_proc
? tmp
->to_proc
->pid
: 0,
1421 tmp
->to_thread
->pid
: 0);
1422 return_error
= BR_FAILED_REPLY
;
1423 goto err_bad_call_stack
;
1426 if (tmp
->from
&& tmp
->from
->proc
== target_proc
)
1427 target_thread
= tmp
->from
;
1428 tmp
= tmp
->from_parent
;
1432 if (target_thread
) {
1433 e
->to_thread
= target_thread
->pid
;
1434 target_list
= &target_thread
->todo
;
1435 target_wait
= &target_thread
->wait
;
1437 target_list
= &target_proc
->todo
;
1438 target_wait
= &target_proc
->wait
;
1440 e
->to_proc
= target_proc
->pid
;
1442 /* TODO: reuse incoming transaction for reply */
1443 t
= kzalloc(sizeof(*t
), GFP_KERNEL
);
1445 return_error
= BR_FAILED_REPLY
;
1446 goto err_alloc_t_failed
;
1448 binder_stats_created(BINDER_STAT_TRANSACTION
);
1450 tcomplete
= kzalloc(sizeof(*tcomplete
), GFP_KERNEL
);
1451 if (tcomplete
== NULL
) {
1452 return_error
= BR_FAILED_REPLY
;
1453 goto err_alloc_tcomplete_failed
;
1455 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE
);
1457 t
->debug_id
= ++binder_last_id
;
1458 e
->debug_id
= t
->debug_id
;
1461 binder_debug(BINDER_DEBUG_TRANSACTION
,
1462 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld\n",
1463 proc
->pid
, thread
->pid
, t
->debug_id
,
1464 target_proc
->pid
, target_thread
->pid
,
1465 (u64
)tr
->data
.ptr
.buffer
,
1466 (u64
)tr
->data
.ptr
.offsets
,
1467 (u64
)tr
->data_size
, (u64
)tr
->offsets_size
);
1469 binder_debug(BINDER_DEBUG_TRANSACTION
,
1470 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld\n",
1471 proc
->pid
, thread
->pid
, t
->debug_id
,
1472 target_proc
->pid
, target_node
->debug_id
,
1473 (u64
)tr
->data
.ptr
.buffer
,
1474 (u64
)tr
->data
.ptr
.offsets
,
1475 (u64
)tr
->data_size
, (u64
)tr
->offsets_size
);
1477 if (!reply
&& !(tr
->flags
& TF_ONE_WAY
))
1481 t
->sender_euid
= task_euid(proc
->tsk
);
1482 t
->to_proc
= target_proc
;
1483 t
->to_thread
= target_thread
;
1485 t
->flags
= tr
->flags
;
1486 t
->priority
= task_nice(current
);
1488 trace_binder_transaction(reply
, t
, target_node
);
1490 t
->buffer
= binder_alloc_buf(target_proc
, tr
->data_size
,
1491 tr
->offsets_size
, !reply
&& (t
->flags
& TF_ONE_WAY
));
1492 if (t
->buffer
== NULL
) {
1493 return_error
= BR_FAILED_REPLY
;
1494 goto err_binder_alloc_buf_failed
;
1496 t
->buffer
->allow_user_free
= 0;
1497 t
->buffer
->debug_id
= t
->debug_id
;
1498 t
->buffer
->transaction
= t
;
1499 t
->buffer
->target_node
= target_node
;
1500 trace_binder_transaction_alloc_buf(t
->buffer
);
1502 binder_inc_node(target_node
, 1, 0, NULL
);
1504 offp
= (binder_size_t
*)(t
->buffer
->data
+
1505 ALIGN(tr
->data_size
, sizeof(void *)));
1507 if (copy_from_user(t
->buffer
->data
, (const void __user
*)(uintptr_t)
1508 tr
->data
.ptr
.buffer
, tr
->data_size
)) {
1509 binder_user_error("%d:%d got transaction with invalid data ptr\n",
1510 proc
->pid
, thread
->pid
);
1511 return_error
= BR_FAILED_REPLY
;
1512 goto err_copy_data_failed
;
1514 if (copy_from_user(offp
, (const void __user
*)(uintptr_t)
1515 tr
->data
.ptr
.offsets
, tr
->offsets_size
)) {
1516 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
1517 proc
->pid
, thread
->pid
);
1518 return_error
= BR_FAILED_REPLY
;
1519 goto err_copy_data_failed
;
1521 if (!IS_ALIGNED(tr
->offsets_size
, sizeof(binder_size_t
))) {
1522 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
1523 proc
->pid
, thread
->pid
, (u64
)tr
->offsets_size
);
1524 return_error
= BR_FAILED_REPLY
;
1525 goto err_bad_offset
;
1527 off_end
= (void *)offp
+ tr
->offsets_size
;
1529 for (; offp
< off_end
; offp
++) {
1530 struct flat_binder_object
*fp
;
1532 if (*offp
> t
->buffer
->data_size
- sizeof(*fp
) ||
1534 t
->buffer
->data_size
< sizeof(*fp
) ||
1535 !IS_ALIGNED(*offp
, sizeof(u32
))) {
1536 binder_user_error("%d:%d got transaction with invalid offset, %lld (min %lld, max %lld)\n",
1537 proc
->pid
, thread
->pid
, (u64
)*offp
,
1539 (u64
)(t
->buffer
->data_size
-
1541 return_error
= BR_FAILED_REPLY
;
1542 goto err_bad_offset
;
1544 fp
= (struct flat_binder_object
*)(t
->buffer
->data
+ *offp
);
1545 off_min
= *offp
+ sizeof(struct flat_binder_object
);
1547 case BINDER_TYPE_BINDER
:
1548 case BINDER_TYPE_WEAK_BINDER
: {
1549 struct binder_ref
*ref
;
1550 struct binder_node
*node
= binder_get_node(proc
, fp
->binder
);
1553 node
= binder_new_node(proc
, fp
->binder
, fp
->cookie
);
1555 return_error
= BR_FAILED_REPLY
;
1556 goto err_binder_new_node_failed
;
1558 node
->min_priority
= fp
->flags
& FLAT_BINDER_FLAG_PRIORITY_MASK
;
1559 node
->accept_fds
= !!(fp
->flags
& FLAT_BINDER_FLAG_ACCEPTS_FDS
);
1561 if (fp
->cookie
!= node
->cookie
) {
1562 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
1563 proc
->pid
, thread
->pid
,
1564 (u64
)fp
->binder
, node
->debug_id
,
1565 (u64
)fp
->cookie
, (u64
)node
->cookie
);
1566 return_error
= BR_FAILED_REPLY
;
1567 goto err_binder_get_ref_for_node_failed
;
1569 ref
= binder_get_ref_for_node(target_proc
, node
);
1571 return_error
= BR_FAILED_REPLY
;
1572 goto err_binder_get_ref_for_node_failed
;
1574 if (fp
->type
== BINDER_TYPE_BINDER
)
1575 fp
->type
= BINDER_TYPE_HANDLE
;
1577 fp
->type
= BINDER_TYPE_WEAK_HANDLE
;
1579 fp
->handle
= ref
->desc
;
1581 binder_inc_ref(ref
, fp
->type
== BINDER_TYPE_HANDLE
,
1584 trace_binder_transaction_node_to_ref(t
, node
, ref
);
1585 binder_debug(BINDER_DEBUG_TRANSACTION
,
1586 " node %d u%016llx -> ref %d desc %d\n",
1587 node
->debug_id
, (u64
)node
->ptr
,
1588 ref
->debug_id
, ref
->desc
);
1590 case BINDER_TYPE_HANDLE
:
1591 case BINDER_TYPE_WEAK_HANDLE
: {
1592 struct binder_ref
*ref
= binder_get_ref(proc
, fp
->handle
,
1593 fp
->type
== BINDER_TYPE_HANDLE
);
1596 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
1598 thread
->pid
, fp
->handle
);
1599 return_error
= BR_FAILED_REPLY
;
1600 goto err_binder_get_ref_failed
;
1602 if (ref
->node
->proc
== target_proc
) {
1603 if (fp
->type
== BINDER_TYPE_HANDLE
)
1604 fp
->type
= BINDER_TYPE_BINDER
;
1606 fp
->type
= BINDER_TYPE_WEAK_BINDER
;
1607 fp
->binder
= ref
->node
->ptr
;
1608 fp
->cookie
= ref
->node
->cookie
;
1609 binder_inc_node(ref
->node
, fp
->type
== BINDER_TYPE_BINDER
, 0, NULL
);
1610 trace_binder_transaction_ref_to_node(t
, ref
);
1611 binder_debug(BINDER_DEBUG_TRANSACTION
,
1612 " ref %d desc %d -> node %d u%016llx\n",
1613 ref
->debug_id
, ref
->desc
, ref
->node
->debug_id
,
1614 (u64
)ref
->node
->ptr
);
1616 struct binder_ref
*new_ref
;
1618 new_ref
= binder_get_ref_for_node(target_proc
, ref
->node
);
1619 if (new_ref
== NULL
) {
1620 return_error
= BR_FAILED_REPLY
;
1621 goto err_binder_get_ref_for_node_failed
;
1624 fp
->handle
= new_ref
->desc
;
1626 binder_inc_ref(new_ref
, fp
->type
== BINDER_TYPE_HANDLE
, NULL
);
1627 trace_binder_transaction_ref_to_ref(t
, ref
,
1629 binder_debug(BINDER_DEBUG_TRANSACTION
,
1630 " ref %d desc %d -> ref %d desc %d (node %d)\n",
1631 ref
->debug_id
, ref
->desc
, new_ref
->debug_id
,
1632 new_ref
->desc
, ref
->node
->debug_id
);
1636 case BINDER_TYPE_FD
: {
1641 if (!(in_reply_to
->flags
& TF_ACCEPT_FDS
)) {
1642 binder_user_error("%d:%d got reply with fd, %d, but target does not allow fds\n",
1643 proc
->pid
, thread
->pid
, fp
->handle
);
1644 return_error
= BR_FAILED_REPLY
;
1645 goto err_fd_not_allowed
;
1647 } else if (!target_node
->accept_fds
) {
1648 binder_user_error("%d:%d got transaction with fd, %d, but target does not allow fds\n",
1649 proc
->pid
, thread
->pid
, fp
->handle
);
1650 return_error
= BR_FAILED_REPLY
;
1651 goto err_fd_not_allowed
;
1654 file
= fget(fp
->handle
);
1656 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
1657 proc
->pid
, thread
->pid
, fp
->handle
);
1658 return_error
= BR_FAILED_REPLY
;
1659 goto err_fget_failed
;
1661 target_fd
= task_get_unused_fd_flags(target_proc
, O_CLOEXEC
);
1662 if (target_fd
< 0) {
1664 return_error
= BR_FAILED_REPLY
;
1665 goto err_get_unused_fd_failed
;
1667 task_fd_install(target_proc
, target_fd
, file
);
1668 trace_binder_transaction_fd(t
, fp
->handle
, target_fd
);
1669 binder_debug(BINDER_DEBUG_TRANSACTION
,
1670 " fd %d -> %d\n", fp
->handle
, target_fd
);
1673 fp
->handle
= target_fd
;
1677 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
1678 proc
->pid
, thread
->pid
, fp
->type
);
1679 return_error
= BR_FAILED_REPLY
;
1680 goto err_bad_object_type
;
1684 BUG_ON(t
->buffer
->async_transaction
!= 0);
1685 binder_pop_transaction(target_thread
, in_reply_to
);
1686 } else if (!(t
->flags
& TF_ONE_WAY
)) {
1687 BUG_ON(t
->buffer
->async_transaction
!= 0);
1689 t
->from_parent
= thread
->transaction_stack
;
1690 thread
->transaction_stack
= t
;
1692 BUG_ON(target_node
== NULL
);
1693 BUG_ON(t
->buffer
->async_transaction
!= 1);
1694 if (target_node
->has_async_transaction
) {
1695 target_list
= &target_node
->async_todo
;
1698 target_node
->has_async_transaction
= 1;
1700 t
->work
.type
= BINDER_WORK_TRANSACTION
;
1701 list_add_tail(&t
->work
.entry
, target_list
);
1702 tcomplete
->type
= BINDER_WORK_TRANSACTION_COMPLETE
;
1703 list_add_tail(&tcomplete
->entry
, &thread
->todo
);
1705 wake_up_interruptible(target_wait
);
1708 err_get_unused_fd_failed
:
1711 err_binder_get_ref_for_node_failed
:
1712 err_binder_get_ref_failed
:
1713 err_binder_new_node_failed
:
1714 err_bad_object_type
:
1716 err_copy_data_failed
:
1717 trace_binder_transaction_failed_buffer_release(t
->buffer
);
1718 binder_transaction_buffer_release(target_proc
, t
->buffer
, offp
);
1719 t
->buffer
->transaction
= NULL
;
1720 binder_free_buf(target_proc
, t
->buffer
);
1721 err_binder_alloc_buf_failed
:
1723 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE
);
1724 err_alloc_tcomplete_failed
:
1726 binder_stats_deleted(BINDER_STAT_TRANSACTION
);
1729 err_empty_call_stack
:
1731 err_invalid_target_handle
:
1732 err_no_context_mgr_node
:
1733 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
1734 "%d:%d transaction failed %d, size %lld-%lld\n",
1735 proc
->pid
, thread
->pid
, return_error
,
1736 (u64
)tr
->data_size
, (u64
)tr
->offsets_size
);
1739 struct binder_transaction_log_entry
*fe
;
1741 fe
= binder_transaction_log_add(&binder_transaction_log_failed
);
1745 BUG_ON(thread
->return_error
!= BR_OK
);
1747 thread
->return_error
= BR_TRANSACTION_COMPLETE
;
1748 binder_send_failed_reply(in_reply_to
, return_error
);
1750 thread
->return_error
= return_error
;
1753 int binder_thread_write(struct binder_proc
*proc
,
1754 struct binder_thread
*thread
,
1755 binder_uintptr_t binder_buffer
, size_t size
,
1756 binder_size_t
*consumed
)
1759 void __user
*buffer
= (void __user
*)(uintptr_t)binder_buffer
;
1760 void __user
*ptr
= buffer
+ *consumed
;
1761 void __user
*end
= buffer
+ size
;
1763 while (ptr
< end
&& thread
->return_error
== BR_OK
) {
1764 if (get_user(cmd
, (uint32_t __user
*)ptr
))
1766 ptr
+= sizeof(uint32_t);
1767 trace_binder_command(cmd
);
1768 if (_IOC_NR(cmd
) < ARRAY_SIZE(binder_stats
.bc
)) {
1769 binder_stats
.bc
[_IOC_NR(cmd
)]++;
1770 proc
->stats
.bc
[_IOC_NR(cmd
)]++;
1771 thread
->stats
.bc
[_IOC_NR(cmd
)]++;
1779 struct binder_ref
*ref
;
1780 const char *debug_string
;
1782 if (get_user(target
, (uint32_t __user
*)ptr
))
1784 ptr
+= sizeof(uint32_t);
1785 if (target
== 0 && binder_context_mgr_node
&&
1786 (cmd
== BC_INCREFS
|| cmd
== BC_ACQUIRE
)) {
1787 ref
= binder_get_ref_for_node(proc
,
1788 binder_context_mgr_node
);
1789 if (ref
->desc
!= target
) {
1790 binder_user_error("%d:%d tried to acquire reference to desc 0, got %d instead\n",
1791 proc
->pid
, thread
->pid
,
1795 ref
= binder_get_ref(proc
, target
,
1796 cmd
== BC_ACQUIRE
||
1799 binder_user_error("%d:%d refcount change on invalid ref %d\n",
1800 proc
->pid
, thread
->pid
, target
);
1805 debug_string
= "IncRefs";
1806 binder_inc_ref(ref
, 0, NULL
);
1809 debug_string
= "Acquire";
1810 binder_inc_ref(ref
, 1, NULL
);
1813 debug_string
= "Release";
1814 binder_dec_ref(&ref
, 1);
1818 debug_string
= "DecRefs";
1819 binder_dec_ref(&ref
, 0);
1823 binder_debug(BINDER_DEBUG_USER_REFS
,
1824 "binder: %d:%d %s ref deleted",
1825 proc
->pid
, thread
->pid
, debug_string
);
1827 binder_debug(BINDER_DEBUG_USER_REFS
,
1828 "binder: %d:%d %s ref %d desc %d s %d w %d for node %d\n",
1829 proc
->pid
, thread
->pid
, debug_string
,
1830 ref
->debug_id
, ref
->desc
, ref
->strong
,
1831 ref
->weak
, ref
->node
->debug_id
);
1835 case BC_INCREFS_DONE
:
1836 case BC_ACQUIRE_DONE
: {
1837 binder_uintptr_t node_ptr
;
1838 binder_uintptr_t cookie
;
1839 struct binder_node
*node
;
1841 if (get_user(node_ptr
, (binder_uintptr_t __user
*)ptr
))
1843 ptr
+= sizeof(binder_uintptr_t
);
1844 if (get_user(cookie
, (binder_uintptr_t __user
*)ptr
))
1846 ptr
+= sizeof(binder_uintptr_t
);
1847 node
= binder_get_node(proc
, node_ptr
);
1849 binder_user_error("%d:%d %s u%016llx no match\n",
1850 proc
->pid
, thread
->pid
,
1851 cmd
== BC_INCREFS_DONE
?
1857 if (cookie
!= node
->cookie
) {
1858 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
1859 proc
->pid
, thread
->pid
,
1860 cmd
== BC_INCREFS_DONE
?
1861 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
1862 (u64
)node_ptr
, node
->debug_id
,
1863 (u64
)cookie
, (u64
)node
->cookie
);
1866 if (cmd
== BC_ACQUIRE_DONE
) {
1867 if (node
->pending_strong_ref
== 0) {
1868 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
1869 proc
->pid
, thread
->pid
,
1873 node
->pending_strong_ref
= 0;
1875 if (node
->pending_weak_ref
== 0) {
1876 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
1877 proc
->pid
, thread
->pid
,
1881 node
->pending_weak_ref
= 0;
1883 binder_dec_node(node
, cmd
== BC_ACQUIRE_DONE
, 0);
1884 binder_debug(BINDER_DEBUG_USER_REFS
,
1885 "%d:%d %s node %d ls %d lw %d\n",
1886 proc
->pid
, thread
->pid
,
1887 cmd
== BC_INCREFS_DONE
? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
1888 node
->debug_id
, node
->local_strong_refs
, node
->local_weak_refs
);
1891 case BC_ATTEMPT_ACQUIRE
:
1892 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
1894 case BC_ACQUIRE_RESULT
:
1895 pr_err("BC_ACQUIRE_RESULT not supported\n");
1898 case BC_FREE_BUFFER
: {
1899 binder_uintptr_t data_ptr
;
1900 struct binder_buffer
*buffer
;
1902 if (get_user(data_ptr
, (binder_uintptr_t __user
*)ptr
))
1904 ptr
+= sizeof(binder_uintptr_t
);
1906 buffer
= binder_buffer_lookup(proc
, data_ptr
);
1907 if (buffer
== NULL
) {
1908 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
1909 proc
->pid
, thread
->pid
, (u64
)data_ptr
);
1912 if (!buffer
->allow_user_free
) {
1913 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
1914 proc
->pid
, thread
->pid
, (u64
)data_ptr
);
1917 binder_debug(BINDER_DEBUG_FREE_BUFFER
,
1918 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
1919 proc
->pid
, thread
->pid
, (u64
)data_ptr
,
1921 buffer
->transaction
? "active" : "finished");
1923 if (buffer
->transaction
) {
1924 buffer
->transaction
->buffer
= NULL
;
1925 buffer
->transaction
= NULL
;
1927 if (buffer
->async_transaction
&& buffer
->target_node
) {
1928 BUG_ON(!buffer
->target_node
->has_async_transaction
);
1929 if (list_empty(&buffer
->target_node
->async_todo
))
1930 buffer
->target_node
->has_async_transaction
= 0;
1932 list_move_tail(buffer
->target_node
->async_todo
.next
, &thread
->todo
);
1934 trace_binder_transaction_buffer_release(buffer
);
1935 binder_transaction_buffer_release(proc
, buffer
, NULL
);
1936 binder_free_buf(proc
, buffer
);
1940 case BC_TRANSACTION
:
1942 struct binder_transaction_data tr
;
1944 if (copy_from_user(&tr
, ptr
, sizeof(tr
)))
1947 binder_transaction(proc
, thread
, &tr
, cmd
== BC_REPLY
);
1951 case BC_REGISTER_LOOPER
:
1952 binder_debug(BINDER_DEBUG_THREADS
,
1953 "%d:%d BC_REGISTER_LOOPER\n",
1954 proc
->pid
, thread
->pid
);
1955 if (thread
->looper
& BINDER_LOOPER_STATE_ENTERED
) {
1956 thread
->looper
|= BINDER_LOOPER_STATE_INVALID
;
1957 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
1958 proc
->pid
, thread
->pid
);
1959 } else if (proc
->requested_threads
== 0) {
1960 thread
->looper
|= BINDER_LOOPER_STATE_INVALID
;
1961 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
1962 proc
->pid
, thread
->pid
);
1964 proc
->requested_threads
--;
1965 proc
->requested_threads_started
++;
1967 thread
->looper
|= BINDER_LOOPER_STATE_REGISTERED
;
1969 case BC_ENTER_LOOPER
:
1970 binder_debug(BINDER_DEBUG_THREADS
,
1971 "%d:%d BC_ENTER_LOOPER\n",
1972 proc
->pid
, thread
->pid
);
1973 if (thread
->looper
& BINDER_LOOPER_STATE_REGISTERED
) {
1974 thread
->looper
|= BINDER_LOOPER_STATE_INVALID
;
1975 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
1976 proc
->pid
, thread
->pid
);
1978 thread
->looper
|= BINDER_LOOPER_STATE_ENTERED
;
1980 case BC_EXIT_LOOPER
:
1981 binder_debug(BINDER_DEBUG_THREADS
,
1982 "%d:%d BC_EXIT_LOOPER\n",
1983 proc
->pid
, thread
->pid
);
1984 thread
->looper
|= BINDER_LOOPER_STATE_EXITED
;
1987 case BC_REQUEST_DEATH_NOTIFICATION
:
1988 case BC_CLEAR_DEATH_NOTIFICATION
: {
1990 binder_uintptr_t cookie
;
1991 struct binder_ref
*ref
;
1992 struct binder_ref_death
*death
;
1994 if (get_user(target
, (uint32_t __user
*)ptr
))
1996 ptr
+= sizeof(uint32_t);
1997 if (get_user(cookie
, (binder_uintptr_t __user
*)ptr
))
1999 ptr
+= sizeof(binder_uintptr_t
);
2000 ref
= binder_get_ref(proc
, target
, false);
2002 binder_user_error("%d:%d %s invalid ref %d\n",
2003 proc
->pid
, thread
->pid
,
2004 cmd
== BC_REQUEST_DEATH_NOTIFICATION
?
2005 "BC_REQUEST_DEATH_NOTIFICATION" :
2006 "BC_CLEAR_DEATH_NOTIFICATION",
2011 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION
,
2012 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
2013 proc
->pid
, thread
->pid
,
2014 cmd
== BC_REQUEST_DEATH_NOTIFICATION
?
2015 "BC_REQUEST_DEATH_NOTIFICATION" :
2016 "BC_CLEAR_DEATH_NOTIFICATION",
2017 (u64
)cookie
, ref
->debug_id
, ref
->desc
,
2018 ref
->strong
, ref
->weak
, ref
->node
->debug_id
);
2020 if (cmd
== BC_REQUEST_DEATH_NOTIFICATION
) {
2022 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
2023 proc
->pid
, thread
->pid
);
2026 death
= kzalloc(sizeof(*death
), GFP_KERNEL
);
2027 if (death
== NULL
) {
2028 thread
->return_error
= BR_ERROR
;
2029 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
2030 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
2031 proc
->pid
, thread
->pid
);
2034 binder_stats_created(BINDER_STAT_DEATH
);
2035 INIT_LIST_HEAD(&death
->work
.entry
);
2036 death
->cookie
= cookie
;
2038 if (ref
->node
->proc
== NULL
) {
2039 ref
->death
->work
.type
= BINDER_WORK_DEAD_BINDER
;
2040 if (thread
->looper
& (BINDER_LOOPER_STATE_REGISTERED
| BINDER_LOOPER_STATE_ENTERED
)) {
2041 list_add_tail(&ref
->death
->work
.entry
, &thread
->todo
);
2043 list_add_tail(&ref
->death
->work
.entry
, &proc
->todo
);
2044 wake_up_interruptible(&proc
->wait
);
2048 if (ref
->death
== NULL
) {
2049 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
2050 proc
->pid
, thread
->pid
);
2054 if (death
->cookie
!= cookie
) {
2055 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
2056 proc
->pid
, thread
->pid
,
2062 if (list_empty(&death
->work
.entry
)) {
2063 death
->work
.type
= BINDER_WORK_CLEAR_DEATH_NOTIFICATION
;
2064 if (thread
->looper
& (BINDER_LOOPER_STATE_REGISTERED
| BINDER_LOOPER_STATE_ENTERED
)) {
2065 list_add_tail(&death
->work
.entry
, &thread
->todo
);
2067 list_add_tail(&death
->work
.entry
, &proc
->todo
);
2068 wake_up_interruptible(&proc
->wait
);
2071 BUG_ON(death
->work
.type
!= BINDER_WORK_DEAD_BINDER
);
2072 death
->work
.type
= BINDER_WORK_DEAD_BINDER_AND_CLEAR
;
2076 case BC_DEAD_BINDER_DONE
: {
2077 struct binder_work
*w
;
2078 binder_uintptr_t cookie
;
2079 struct binder_ref_death
*death
= NULL
;
2081 if (get_user(cookie
, (binder_uintptr_t __user
*)ptr
))
2084 ptr
+= sizeof(void *);
2085 list_for_each_entry(w
, &proc
->delivered_death
, entry
) {
2086 struct binder_ref_death
*tmp_death
= container_of(w
, struct binder_ref_death
, work
);
2088 if (tmp_death
->cookie
== cookie
) {
2093 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
2094 "%d:%d BC_DEAD_BINDER_DONE %016llx found %pK\n",
2095 proc
->pid
, thread
->pid
, (u64
)cookie
, death
);
2096 if (death
== NULL
) {
2097 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
2098 proc
->pid
, thread
->pid
, (u64
)cookie
);
2102 list_del_init(&death
->work
.entry
);
2103 if (death
->work
.type
== BINDER_WORK_DEAD_BINDER_AND_CLEAR
) {
2104 death
->work
.type
= BINDER_WORK_CLEAR_DEATH_NOTIFICATION
;
2105 if (thread
->looper
& (BINDER_LOOPER_STATE_REGISTERED
| BINDER_LOOPER_STATE_ENTERED
)) {
2106 list_add_tail(&death
->work
.entry
, &thread
->todo
);
2108 list_add_tail(&death
->work
.entry
, &proc
->todo
);
2109 wake_up_interruptible(&proc
->wait
);
2115 pr_err("%d:%d unknown command %d\n",
2116 proc
->pid
, thread
->pid
, cmd
);
2119 *consumed
= ptr
- buffer
;
2124 static void binder_stat_br(struct binder_proc
*proc
,
2125 struct binder_thread
*thread
, uint32_t cmd
)
2127 trace_binder_return(cmd
);
2128 if (_IOC_NR(cmd
) < ARRAY_SIZE(binder_stats
.br
)) {
2129 binder_stats
.br
[_IOC_NR(cmd
)]++;
2130 proc
->stats
.br
[_IOC_NR(cmd
)]++;
2131 thread
->stats
.br
[_IOC_NR(cmd
)]++;
2135 static int binder_has_proc_work(struct binder_proc
*proc
,
2136 struct binder_thread
*thread
)
2138 return !list_empty(&proc
->todo
) ||
2139 (thread
->looper
& BINDER_LOOPER_STATE_NEED_RETURN
);
2142 static int binder_has_thread_work(struct binder_thread
*thread
)
2144 return !list_empty(&thread
->todo
) || thread
->return_error
!= BR_OK
||
2145 (thread
->looper
& BINDER_LOOPER_STATE_NEED_RETURN
);
2148 static int binder_thread_read(struct binder_proc
*proc
,
2149 struct binder_thread
*thread
,
2150 binder_uintptr_t binder_buffer
, size_t size
,
2151 binder_size_t
*consumed
, int non_block
)
2153 void __user
*buffer
= (void __user
*)(uintptr_t)binder_buffer
;
2154 void __user
*ptr
= buffer
+ *consumed
;
2155 void __user
*end
= buffer
+ size
;
2158 int wait_for_proc_work
;
2160 if (*consumed
== 0) {
2161 if (put_user(BR_NOOP
, (uint32_t __user
*)ptr
))
2163 ptr
+= sizeof(uint32_t);
2167 wait_for_proc_work
= thread
->transaction_stack
== NULL
&&
2168 list_empty(&thread
->todo
);
2170 if (thread
->return_error
!= BR_OK
&& ptr
< end
) {
2171 if (thread
->return_error2
!= BR_OK
) {
2172 if (put_user(thread
->return_error2
, (uint32_t __user
*)ptr
))
2174 ptr
+= sizeof(uint32_t);
2175 binder_stat_br(proc
, thread
, thread
->return_error2
);
2178 thread
->return_error2
= BR_OK
;
2180 if (put_user(thread
->return_error
, (uint32_t __user
*)ptr
))
2182 ptr
+= sizeof(uint32_t);
2183 binder_stat_br(proc
, thread
, thread
->return_error
);
2184 thread
->return_error
= BR_OK
;
2189 thread
->looper
|= BINDER_LOOPER_STATE_WAITING
;
2190 if (wait_for_proc_work
)
2191 proc
->ready_threads
++;
2193 binder_unlock(__func__
);
2195 trace_binder_wait_for_work(wait_for_proc_work
,
2196 !!thread
->transaction_stack
,
2197 !list_empty(&thread
->todo
));
2198 if (wait_for_proc_work
) {
2199 if (!(thread
->looper
& (BINDER_LOOPER_STATE_REGISTERED
|
2200 BINDER_LOOPER_STATE_ENTERED
))) {
2201 binder_user_error("%d:%d ERROR: Thread waiting for process work before calling BC_REGISTER_LOOPER or BC_ENTER_LOOPER (state %x)\n",
2202 proc
->pid
, thread
->pid
, thread
->looper
);
2203 wait_event_interruptible(binder_user_error_wait
,
2204 binder_stop_on_user_error
< 2);
2206 binder_set_nice(proc
->default_priority
);
2208 if (!binder_has_proc_work(proc
, thread
))
2211 ret
= wait_event_freezable_exclusive(proc
->wait
, binder_has_proc_work(proc
, thread
));
2214 if (!binder_has_thread_work(thread
))
2217 ret
= wait_event_freezable(thread
->wait
, binder_has_thread_work(thread
));
2220 binder_lock(__func__
);
2222 if (wait_for_proc_work
)
2223 proc
->ready_threads
--;
2224 thread
->looper
&= ~BINDER_LOOPER_STATE_WAITING
;
2231 struct binder_transaction_data tr
;
2232 struct binder_work
*w
;
2233 struct binder_transaction
*t
= NULL
;
2235 if (!list_empty(&thread
->todo
)) {
2236 w
= list_first_entry(&thread
->todo
, struct binder_work
,
2238 } else if (!list_empty(&proc
->todo
) && wait_for_proc_work
) {
2239 w
= list_first_entry(&proc
->todo
, struct binder_work
,
2243 if (ptr
- buffer
== 4 &&
2244 !(thread
->looper
& BINDER_LOOPER_STATE_NEED_RETURN
))
2249 if (end
- ptr
< sizeof(tr
) + 4)
2253 case BINDER_WORK_TRANSACTION
: {
2254 t
= container_of(w
, struct binder_transaction
, work
);
2256 case BINDER_WORK_TRANSACTION_COMPLETE
: {
2257 cmd
= BR_TRANSACTION_COMPLETE
;
2258 if (put_user(cmd
, (uint32_t __user
*)ptr
))
2260 ptr
+= sizeof(uint32_t);
2262 binder_stat_br(proc
, thread
, cmd
);
2263 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE
,
2264 "%d:%d BR_TRANSACTION_COMPLETE\n",
2265 proc
->pid
, thread
->pid
);
2267 list_del(&w
->entry
);
2269 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE
);
2271 case BINDER_WORK_NODE
: {
2272 struct binder_node
*node
= container_of(w
, struct binder_node
, work
);
2273 uint32_t cmd
= BR_NOOP
;
2274 const char *cmd_name
;
2275 int strong
= node
->internal_strong_refs
|| node
->local_strong_refs
;
2276 int weak
= !hlist_empty(&node
->refs
) || node
->local_weak_refs
|| strong
;
2278 if (weak
&& !node
->has_weak_ref
) {
2280 cmd_name
= "BR_INCREFS";
2281 node
->has_weak_ref
= 1;
2282 node
->pending_weak_ref
= 1;
2283 node
->local_weak_refs
++;
2284 } else if (strong
&& !node
->has_strong_ref
) {
2286 cmd_name
= "BR_ACQUIRE";
2287 node
->has_strong_ref
= 1;
2288 node
->pending_strong_ref
= 1;
2289 node
->local_strong_refs
++;
2290 } else if (!strong
&& node
->has_strong_ref
) {
2292 cmd_name
= "BR_RELEASE";
2293 node
->has_strong_ref
= 0;
2294 } else if (!weak
&& node
->has_weak_ref
) {
2296 cmd_name
= "BR_DECREFS";
2297 node
->has_weak_ref
= 0;
2299 if (cmd
!= BR_NOOP
) {
2300 if (put_user(cmd
, (uint32_t __user
*)ptr
))
2302 ptr
+= sizeof(uint32_t);
2303 if (put_user(node
->ptr
,
2304 (binder_uintptr_t __user
*)ptr
))
2306 ptr
+= sizeof(binder_uintptr_t
);
2307 if (put_user(node
->cookie
,
2308 (binder_uintptr_t __user
*)ptr
))
2310 ptr
+= sizeof(binder_uintptr_t
);
2312 binder_stat_br(proc
, thread
, cmd
);
2313 binder_debug(BINDER_DEBUG_USER_REFS
,
2314 "%d:%d %s %d u%016llx c%016llx\n",
2315 proc
->pid
, thread
->pid
, cmd_name
,
2317 (u64
)node
->ptr
, (u64
)node
->cookie
);
2319 list_del_init(&w
->entry
);
2320 if (!weak
&& !strong
) {
2321 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
2322 "%d:%d node %d u%016llx c%016llx deleted\n",
2323 proc
->pid
, thread
->pid
,
2327 rb_erase(&node
->rb_node
, &proc
->nodes
);
2329 binder_stats_deleted(BINDER_STAT_NODE
);
2331 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
2332 "%d:%d node %d u%016llx c%016llx state unchanged\n",
2333 proc
->pid
, thread
->pid
,
2340 case BINDER_WORK_DEAD_BINDER
:
2341 case BINDER_WORK_DEAD_BINDER_AND_CLEAR
:
2342 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION
: {
2343 struct binder_ref_death
*death
;
2346 death
= container_of(w
, struct binder_ref_death
, work
);
2347 if (w
->type
== BINDER_WORK_CLEAR_DEATH_NOTIFICATION
)
2348 cmd
= BR_CLEAR_DEATH_NOTIFICATION_DONE
;
2350 cmd
= BR_DEAD_BINDER
;
2351 if (put_user(cmd
, (uint32_t __user
*)ptr
))
2353 ptr
+= sizeof(uint32_t);
2354 if (put_user(death
->cookie
,
2355 (binder_uintptr_t __user
*)ptr
))
2357 ptr
+= sizeof(binder_uintptr_t
);
2358 binder_stat_br(proc
, thread
, cmd
);
2359 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION
,
2360 "%d:%d %s %016llx\n",
2361 proc
->pid
, thread
->pid
,
2362 cmd
== BR_DEAD_BINDER
?
2364 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
2365 (u64
)death
->cookie
);
2367 if (w
->type
== BINDER_WORK_CLEAR_DEATH_NOTIFICATION
) {
2368 list_del(&w
->entry
);
2370 binder_stats_deleted(BINDER_STAT_DEATH
);
2372 list_move(&w
->entry
, &proc
->delivered_death
);
2373 if (cmd
== BR_DEAD_BINDER
)
2374 goto done
; /* DEAD_BINDER notifications can cause transactions */
2381 BUG_ON(t
->buffer
== NULL
);
2382 if (t
->buffer
->target_node
) {
2383 struct binder_node
*target_node
= t
->buffer
->target_node
;
2385 tr
.target
.ptr
= target_node
->ptr
;
2386 tr
.cookie
= target_node
->cookie
;
2387 t
->saved_priority
= task_nice(current
);
2388 if (t
->priority
< target_node
->min_priority
&&
2389 !(t
->flags
& TF_ONE_WAY
))
2390 binder_set_nice(t
->priority
);
2391 else if (!(t
->flags
& TF_ONE_WAY
) ||
2392 t
->saved_priority
> target_node
->min_priority
)
2393 binder_set_nice(target_node
->min_priority
);
2394 cmd
= BR_TRANSACTION
;
2401 tr
.flags
= t
->flags
;
2402 tr
.sender_euid
= from_kuid(current_user_ns(), t
->sender_euid
);
2405 struct task_struct
*sender
= t
->from
->proc
->tsk
;
2407 tr
.sender_pid
= task_tgid_nr_ns(sender
,
2408 task_active_pid_ns(current
));
2413 tr
.data_size
= t
->buffer
->data_size
;
2414 tr
.offsets_size
= t
->buffer
->offsets_size
;
2415 tr
.data
.ptr
.buffer
= (binder_uintptr_t
)(
2416 (uintptr_t)t
->buffer
->data
+
2417 proc
->user_buffer_offset
);
2418 tr
.data
.ptr
.offsets
= tr
.data
.ptr
.buffer
+
2419 ALIGN(t
->buffer
->data_size
,
2422 if (put_user(cmd
, (uint32_t __user
*)ptr
))
2424 ptr
+= sizeof(uint32_t);
2425 if (copy_to_user(ptr
, &tr
, sizeof(tr
)))
2429 trace_binder_transaction_received(t
);
2430 binder_stat_br(proc
, thread
, cmd
);
2431 binder_debug(BINDER_DEBUG_TRANSACTION
,
2432 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
2433 proc
->pid
, thread
->pid
,
2434 (cmd
== BR_TRANSACTION
) ? "BR_TRANSACTION" :
2436 t
->debug_id
, t
->from
? t
->from
->proc
->pid
: 0,
2437 t
->from
? t
->from
->pid
: 0, cmd
,
2438 t
->buffer
->data_size
, t
->buffer
->offsets_size
,
2439 (u64
)tr
.data
.ptr
.buffer
, (u64
)tr
.data
.ptr
.offsets
);
2441 list_del(&t
->work
.entry
);
2442 t
->buffer
->allow_user_free
= 1;
2443 if (cmd
== BR_TRANSACTION
&& !(t
->flags
& TF_ONE_WAY
)) {
2444 t
->to_parent
= thread
->transaction_stack
;
2445 t
->to_thread
= thread
;
2446 thread
->transaction_stack
= t
;
2448 t
->buffer
->transaction
= NULL
;
2450 binder_stats_deleted(BINDER_STAT_TRANSACTION
);
2457 *consumed
= ptr
- buffer
;
2458 if (proc
->requested_threads
+ proc
->ready_threads
== 0 &&
2459 proc
->requested_threads_started
< proc
->max_threads
&&
2460 (thread
->looper
& (BINDER_LOOPER_STATE_REGISTERED
|
2461 BINDER_LOOPER_STATE_ENTERED
)) /* the user-space code fails to */
2462 /*spawn a new thread if we leave this out */) {
2463 proc
->requested_threads
++;
2464 binder_debug(BINDER_DEBUG_THREADS
,
2465 "%d:%d BR_SPAWN_LOOPER\n",
2466 proc
->pid
, thread
->pid
);
2467 if (put_user(BR_SPAWN_LOOPER
, (uint32_t __user
*)buffer
))
2469 binder_stat_br(proc
, thread
, BR_SPAWN_LOOPER
);
2474 static void binder_release_work(struct list_head
*list
)
2476 struct binder_work
*w
;
2478 while (!list_empty(list
)) {
2479 w
= list_first_entry(list
, struct binder_work
, entry
);
2480 list_del_init(&w
->entry
);
2482 case BINDER_WORK_TRANSACTION
: {
2483 struct binder_transaction
*t
;
2485 t
= container_of(w
, struct binder_transaction
, work
);
2486 if (t
->buffer
->target_node
&&
2487 !(t
->flags
& TF_ONE_WAY
)) {
2488 binder_send_failed_reply(t
, BR_DEAD_REPLY
);
2490 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
2491 "undelivered transaction %d\n",
2493 t
->buffer
->transaction
= NULL
;
2495 binder_stats_deleted(BINDER_STAT_TRANSACTION
);
2498 case BINDER_WORK_TRANSACTION_COMPLETE
: {
2499 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
2500 "undelivered TRANSACTION_COMPLETE\n");
2502 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE
);
2504 case BINDER_WORK_DEAD_BINDER_AND_CLEAR
:
2505 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION
: {
2506 struct binder_ref_death
*death
;
2508 death
= container_of(w
, struct binder_ref_death
, work
);
2509 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
2510 "undelivered death notification, %016llx\n",
2511 (u64
)death
->cookie
);
2513 binder_stats_deleted(BINDER_STAT_DEATH
);
2516 pr_err("unexpected work type, %d, not freed\n",
2524 static struct binder_thread
*binder_get_thread(struct binder_proc
*proc
)
2526 struct binder_thread
*thread
= NULL
;
2527 struct rb_node
*parent
= NULL
;
2528 struct rb_node
**p
= &proc
->threads
.rb_node
;
2532 thread
= rb_entry(parent
, struct binder_thread
, rb_node
);
2534 if (current
->pid
< thread
->pid
)
2536 else if (current
->pid
> thread
->pid
)
2537 p
= &(*p
)->rb_right
;
2542 thread
= kzalloc(sizeof(*thread
), GFP_KERNEL
);
2545 binder_stats_created(BINDER_STAT_THREAD
);
2546 thread
->proc
= proc
;
2547 thread
->pid
= current
->pid
;
2548 init_waitqueue_head(&thread
->wait
);
2549 INIT_LIST_HEAD(&thread
->todo
);
2550 rb_link_node(&thread
->rb_node
, parent
, p
);
2551 rb_insert_color(&thread
->rb_node
, &proc
->threads
);
2552 thread
->looper
|= BINDER_LOOPER_STATE_NEED_RETURN
;
2553 thread
->return_error
= BR_OK
;
2554 thread
->return_error2
= BR_OK
;
2559 static int binder_free_thread(struct binder_proc
*proc
,
2560 struct binder_thread
*thread
)
2562 struct binder_transaction
*t
;
2563 struct binder_transaction
*send_reply
= NULL
;
2564 int active_transactions
= 0;
2566 rb_erase(&thread
->rb_node
, &proc
->threads
);
2567 t
= thread
->transaction_stack
;
2568 if (t
&& t
->to_thread
== thread
)
2571 active_transactions
++;
2572 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
2573 "release %d:%d transaction %d %s, still active\n",
2574 proc
->pid
, thread
->pid
,
2576 (t
->to_thread
== thread
) ? "in" : "out");
2578 if (t
->to_thread
== thread
) {
2580 t
->to_thread
= NULL
;
2582 t
->buffer
->transaction
= NULL
;
2586 } else if (t
->from
== thread
) {
2593 binder_send_failed_reply(send_reply
, BR_DEAD_REPLY
);
2594 binder_release_work(&thread
->todo
);
2596 binder_stats_deleted(BINDER_STAT_THREAD
);
2597 return active_transactions
;
2600 static unsigned int binder_poll(struct file
*filp
,
2601 struct poll_table_struct
*wait
)
2603 struct binder_proc
*proc
= filp
->private_data
;
2604 struct binder_thread
*thread
= NULL
;
2605 int wait_for_proc_work
;
2607 binder_lock(__func__
);
2609 thread
= binder_get_thread(proc
);
2611 wait_for_proc_work
= thread
->transaction_stack
== NULL
&&
2612 list_empty(&thread
->todo
) && thread
->return_error
== BR_OK
;
2614 binder_unlock(__func__
);
2616 if (wait_for_proc_work
) {
2617 if (binder_has_proc_work(proc
, thread
))
2619 poll_wait(filp
, &proc
->wait
, wait
);
2620 if (binder_has_proc_work(proc
, thread
))
2623 if (binder_has_thread_work(thread
))
2625 poll_wait(filp
, &thread
->wait
, wait
);
2626 if (binder_has_thread_work(thread
))
2632 static int binder_ioctl_write_read(struct file
*filp
,
2633 unsigned int cmd
, unsigned long arg
,
2634 struct binder_thread
*thread
)
2637 struct binder_proc
*proc
= filp
->private_data
;
2638 unsigned int size
= _IOC_SIZE(cmd
);
2639 void __user
*ubuf
= (void __user
*)arg
;
2640 struct binder_write_read bwr
;
2642 if (size
!= sizeof(struct binder_write_read
)) {
2646 if (copy_from_user(&bwr
, ubuf
, sizeof(bwr
))) {
2650 binder_debug(BINDER_DEBUG_READ_WRITE
,
2651 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
2652 proc
->pid
, thread
->pid
,
2653 (u64
)bwr
.write_size
, (u64
)bwr
.write_buffer
,
2654 (u64
)bwr
.read_size
, (u64
)bwr
.read_buffer
);
2656 if (bwr
.write_size
> 0) {
2657 ret
= binder_thread_write(proc
, thread
,
2660 &bwr
.write_consumed
);
2661 trace_binder_write_done(ret
);
2663 bwr
.read_consumed
= 0;
2664 if (copy_to_user(ubuf
, &bwr
, sizeof(bwr
)))
2669 if (bwr
.read_size
> 0) {
2670 ret
= binder_thread_read(proc
, thread
, bwr
.read_buffer
,
2673 filp
->f_flags
& O_NONBLOCK
);
2674 trace_binder_read_done(ret
);
2675 if (!list_empty(&proc
->todo
))
2676 wake_up_interruptible(&proc
->wait
);
2678 if (copy_to_user(ubuf
, &bwr
, sizeof(bwr
)))
2683 binder_debug(BINDER_DEBUG_READ_WRITE
,
2684 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
2685 proc
->pid
, thread
->pid
,
2686 (u64
)bwr
.write_consumed
, (u64
)bwr
.write_size
,
2687 (u64
)bwr
.read_consumed
, (u64
)bwr
.read_size
);
2688 if (copy_to_user(ubuf
, &bwr
, sizeof(bwr
))) {
2696 static int binder_ioctl_set_ctx_mgr(struct file
*filp
)
2699 struct binder_proc
*proc
= filp
->private_data
;
2700 kuid_t curr_euid
= current_euid();
2702 if (binder_context_mgr_node
!= NULL
) {
2703 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
2707 if (uid_valid(binder_context_mgr_uid
)) {
2708 if (!uid_eq(binder_context_mgr_uid
, curr_euid
)) {
2709 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
2710 from_kuid(&init_user_ns
, curr_euid
),
2711 from_kuid(&init_user_ns
,
2712 binder_context_mgr_uid
));
2717 binder_context_mgr_uid
= curr_euid
;
2719 binder_context_mgr_node
= binder_new_node(proc
, 0, 0);
2720 if (binder_context_mgr_node
== NULL
) {
2724 binder_context_mgr_node
->local_weak_refs
++;
2725 binder_context_mgr_node
->local_strong_refs
++;
2726 binder_context_mgr_node
->has_strong_ref
= 1;
2727 binder_context_mgr_node
->has_weak_ref
= 1;
2732 static long binder_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
2735 struct binder_proc
*proc
= filp
->private_data
;
2736 struct binder_thread
*thread
;
2737 unsigned int size
= _IOC_SIZE(cmd
);
2738 void __user
*ubuf
= (void __user
*)arg
;
2740 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
2741 proc->pid, current->pid, cmd, arg);*/
2743 trace_binder_ioctl(cmd
, arg
);
2745 ret
= wait_event_interruptible(binder_user_error_wait
, binder_stop_on_user_error
< 2);
2749 binder_lock(__func__
);
2750 thread
= binder_get_thread(proc
);
2751 if (thread
== NULL
) {
2757 case BINDER_WRITE_READ
:
2758 ret
= binder_ioctl_write_read(filp
, cmd
, arg
, thread
);
2762 case BINDER_SET_MAX_THREADS
:
2763 if (copy_from_user(&proc
->max_threads
, ubuf
, sizeof(proc
->max_threads
))) {
2768 case BINDER_SET_CONTEXT_MGR
:
2769 ret
= binder_ioctl_set_ctx_mgr(filp
);
2773 case BINDER_THREAD_EXIT
:
2774 binder_debug(BINDER_DEBUG_THREADS
, "%d:%d exit\n",
2775 proc
->pid
, thread
->pid
);
2776 binder_free_thread(proc
, thread
);
2779 case BINDER_VERSION
: {
2780 struct binder_version __user
*ver
= ubuf
;
2782 if (size
!= sizeof(struct binder_version
)) {
2786 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION
,
2787 &ver
->protocol_version
)) {
2800 thread
->looper
&= ~BINDER_LOOPER_STATE_NEED_RETURN
;
2801 binder_unlock(__func__
);
2802 wait_event_interruptible(binder_user_error_wait
, binder_stop_on_user_error
< 2);
2803 if (ret
&& ret
!= -ERESTARTSYS
)
2804 pr_info("%d:%d ioctl %x %lx returned %d\n", proc
->pid
, current
->pid
, cmd
, arg
, ret
);
2806 trace_binder_ioctl_done(ret
);
2810 static void binder_vma_open(struct vm_area_struct
*vma
)
2812 struct binder_proc
*proc
= vma
->vm_private_data
;
2814 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
2815 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
2816 proc
->pid
, vma
->vm_start
, vma
->vm_end
,
2817 (vma
->vm_end
- vma
->vm_start
) / SZ_1K
, vma
->vm_flags
,
2818 (unsigned long)pgprot_val(vma
->vm_page_prot
));
2821 static void binder_vma_close(struct vm_area_struct
*vma
)
2823 struct binder_proc
*proc
= vma
->vm_private_data
;
2825 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
2826 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
2827 proc
->pid
, vma
->vm_start
, vma
->vm_end
,
2828 (vma
->vm_end
- vma
->vm_start
) / SZ_1K
, vma
->vm_flags
,
2829 (unsigned long)pgprot_val(vma
->vm_page_prot
));
2831 proc
->vma_vm_mm
= NULL
;
2832 binder_defer_work(proc
, BINDER_DEFERRED_PUT_FILES
);
2835 static int binder_vm_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
2837 return VM_FAULT_SIGBUS
;
2840 static struct vm_operations_struct binder_vm_ops
= {
2841 .open
= binder_vma_open
,
2842 .close
= binder_vma_close
,
2843 .fault
= binder_vm_fault
,
2846 static int binder_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
2849 struct vm_struct
*area
;
2850 struct binder_proc
*proc
= filp
->private_data
;
2851 const char *failure_string
;
2852 struct binder_buffer
*buffer
;
2854 if (proc
->tsk
!= current
)
2857 if ((vma
->vm_end
- vma
->vm_start
) > SZ_4M
)
2858 vma
->vm_end
= vma
->vm_start
+ SZ_4M
;
2860 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
2861 "binder_mmap: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
2862 proc
->pid
, vma
->vm_start
, vma
->vm_end
,
2863 (vma
->vm_end
- vma
->vm_start
) / SZ_1K
, vma
->vm_flags
,
2864 (unsigned long)pgprot_val(vma
->vm_page_prot
));
2866 if (vma
->vm_flags
& FORBIDDEN_MMAP_FLAGS
) {
2868 failure_string
= "bad vm_flags";
2871 vma
->vm_flags
= (vma
->vm_flags
| VM_DONTCOPY
) & ~VM_MAYWRITE
;
2873 mutex_lock(&binder_mmap_lock
);
2876 failure_string
= "already mapped";
2877 goto err_already_mapped
;
2880 area
= get_vm_area(vma
->vm_end
- vma
->vm_start
, VM_IOREMAP
);
2883 failure_string
= "get_vm_area";
2884 goto err_get_vm_area_failed
;
2886 proc
->buffer
= area
->addr
;
2887 proc
->user_buffer_offset
= vma
->vm_start
- (uintptr_t)proc
->buffer
;
2888 mutex_unlock(&binder_mmap_lock
);
2890 #ifdef CONFIG_CPU_CACHE_VIPT
2891 if (cache_is_vipt_aliasing()) {
2892 while (CACHE_COLOUR((vma
->vm_start
^ (uint32_t)proc
->buffer
))) {
2893 pr_info("binder_mmap: %d %lx-%lx maps %pK bad alignment\n", proc
->pid
, vma
->vm_start
, vma
->vm_end
, proc
->buffer
);
2894 vma
->vm_start
+= PAGE_SIZE
;
2898 proc
->pages
= kzalloc(sizeof(proc
->pages
[0]) * ((vma
->vm_end
- vma
->vm_start
) / PAGE_SIZE
), GFP_KERNEL
);
2899 if (proc
->pages
== NULL
) {
2901 failure_string
= "alloc page array";
2902 goto err_alloc_pages_failed
;
2904 proc
->buffer_size
= vma
->vm_end
- vma
->vm_start
;
2906 vma
->vm_ops
= &binder_vm_ops
;
2907 vma
->vm_private_data
= proc
;
2909 if (binder_update_page_range(proc
, 1, proc
->buffer
, proc
->buffer
+ PAGE_SIZE
, vma
)) {
2911 failure_string
= "alloc small buf";
2912 goto err_alloc_small_buf_failed
;
2914 buffer
= proc
->buffer
;
2915 INIT_LIST_HEAD(&proc
->buffers
);
2916 list_add(&buffer
->entry
, &proc
->buffers
);
2918 binder_insert_free_buffer(proc
, buffer
);
2919 proc
->free_async_space
= proc
->buffer_size
/ 2;
2921 proc
->files
= get_files_struct(current
);
2923 proc
->vma_vm_mm
= vma
->vm_mm
;
2925 /*pr_info("binder_mmap: %d %lx-%lx maps %pK\n",
2926 proc->pid, vma->vm_start, vma->vm_end, proc->buffer);*/
2929 err_alloc_small_buf_failed
:
2932 err_alloc_pages_failed
:
2933 mutex_lock(&binder_mmap_lock
);
2934 vfree(proc
->buffer
);
2935 proc
->buffer
= NULL
;
2936 err_get_vm_area_failed
:
2938 mutex_unlock(&binder_mmap_lock
);
2940 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
2941 proc
->pid
, vma
->vm_start
, vma
->vm_end
, failure_string
, ret
);
2945 static int binder_open(struct inode
*nodp
, struct file
*filp
)
2947 struct binder_proc
*proc
;
2949 binder_debug(BINDER_DEBUG_OPEN_CLOSE
, "binder_open: %d:%d\n",
2950 current
->group_leader
->pid
, current
->pid
);
2952 proc
= kzalloc(sizeof(*proc
), GFP_KERNEL
);
2955 get_task_struct(current
);
2956 proc
->tsk
= current
;
2957 INIT_LIST_HEAD(&proc
->todo
);
2958 init_waitqueue_head(&proc
->wait
);
2959 proc
->default_priority
= task_nice(current
);
2961 binder_lock(__func__
);
2963 binder_stats_created(BINDER_STAT_PROC
);
2964 hlist_add_head(&proc
->proc_node
, &binder_procs
);
2965 proc
->pid
= current
->group_leader
->pid
;
2966 INIT_LIST_HEAD(&proc
->delivered_death
);
2967 filp
->private_data
= proc
;
2969 binder_unlock(__func__
);
2971 if (binder_debugfs_dir_entry_proc
) {
2974 snprintf(strbuf
, sizeof(strbuf
), "%u", proc
->pid
);
2975 proc
->debugfs_entry
= debugfs_create_file(strbuf
, S_IRUGO
,
2976 binder_debugfs_dir_entry_proc
, proc
, &binder_proc_fops
);
2982 static int binder_flush(struct file
*filp
, fl_owner_t id
)
2984 struct binder_proc
*proc
= filp
->private_data
;
2986 binder_defer_work(proc
, BINDER_DEFERRED_FLUSH
);
2991 static void binder_deferred_flush(struct binder_proc
*proc
)
2996 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
)) {
2997 struct binder_thread
*thread
= rb_entry(n
, struct binder_thread
, rb_node
);
2999 thread
->looper
|= BINDER_LOOPER_STATE_NEED_RETURN
;
3000 if (thread
->looper
& BINDER_LOOPER_STATE_WAITING
) {
3001 wake_up_interruptible(&thread
->wait
);
3005 wake_up_interruptible_all(&proc
->wait
);
3007 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
3008 "binder_flush: %d woke %d threads\n", proc
->pid
,
3012 static int binder_release(struct inode
*nodp
, struct file
*filp
)
3014 struct binder_proc
*proc
= filp
->private_data
;
3016 debugfs_remove(proc
->debugfs_entry
);
3017 binder_defer_work(proc
, BINDER_DEFERRED_RELEASE
);
3022 static int binder_node_release(struct binder_node
*node
, int refs
)
3024 struct binder_ref
*ref
;
3027 list_del_init(&node
->work
.entry
);
3028 binder_release_work(&node
->async_todo
);
3030 if (hlist_empty(&node
->refs
)) {
3032 binder_stats_deleted(BINDER_STAT_NODE
);
3038 node
->local_strong_refs
= 0;
3039 node
->local_weak_refs
= 0;
3040 hlist_add_head(&node
->dead_node
, &binder_dead_nodes
);
3042 hlist_for_each_entry(ref
, &node
->refs
, node_entry
) {
3050 if (list_empty(&ref
->death
->work
.entry
)) {
3051 ref
->death
->work
.type
= BINDER_WORK_DEAD_BINDER
;
3052 list_add_tail(&ref
->death
->work
.entry
,
3054 wake_up_interruptible(&ref
->proc
->wait
);
3059 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
3060 "node %d now dead, refs %d, death %d\n",
3061 node
->debug_id
, refs
, death
);
3066 static void binder_deferred_release(struct binder_proc
*proc
)
3068 struct binder_transaction
*t
;
3070 int threads
, nodes
, incoming_refs
, outgoing_refs
, buffers
,
3071 active_transactions
, page_count
;
3074 BUG_ON(proc
->files
);
3076 hlist_del(&proc
->proc_node
);
3078 if (binder_context_mgr_node
&& binder_context_mgr_node
->proc
== proc
) {
3079 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
3080 "%s: %d context_mgr_node gone\n",
3081 __func__
, proc
->pid
);
3082 binder_context_mgr_node
= NULL
;
3086 active_transactions
= 0;
3087 while ((n
= rb_first(&proc
->threads
))) {
3088 struct binder_thread
*thread
;
3090 thread
= rb_entry(n
, struct binder_thread
, rb_node
);
3092 active_transactions
+= binder_free_thread(proc
, thread
);
3097 while ((n
= rb_first(&proc
->nodes
))) {
3098 struct binder_node
*node
;
3100 node
= rb_entry(n
, struct binder_node
, rb_node
);
3102 rb_erase(&node
->rb_node
, &proc
->nodes
);
3103 incoming_refs
= binder_node_release(node
, incoming_refs
);
3107 while ((n
= rb_first(&proc
->refs_by_desc
))) {
3108 struct binder_ref
*ref
;
3110 ref
= rb_entry(n
, struct binder_ref
, rb_node_desc
);
3112 binder_delete_ref(ref
);
3115 binder_release_work(&proc
->todo
);
3116 binder_release_work(&proc
->delivered_death
);
3119 while ((n
= rb_first(&proc
->allocated_buffers
))) {
3120 struct binder_buffer
*buffer
;
3122 buffer
= rb_entry(n
, struct binder_buffer
, rb_node
);
3124 t
= buffer
->transaction
;
3127 buffer
->transaction
= NULL
;
3128 pr_err("release proc %d, transaction %d, not freed\n",
3129 proc
->pid
, t
->debug_id
);
3133 binder_free_buf(proc
, buffer
);
3137 binder_stats_deleted(BINDER_STAT_PROC
);
3143 for (i
= 0; i
< proc
->buffer_size
/ PAGE_SIZE
; i
++) {
3146 if (!proc
->pages
[i
])
3149 page_addr
= proc
->buffer
+ i
* PAGE_SIZE
;
3150 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
3151 "%s: %d: page %d at %pK not freed\n",
3152 __func__
, proc
->pid
, i
, page_addr
);
3153 unmap_kernel_range((unsigned long)page_addr
, PAGE_SIZE
);
3154 __free_page(proc
->pages
[i
]);
3158 vfree(proc
->buffer
);
3161 put_task_struct(proc
->tsk
);
3163 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
3164 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d, buffers %d, pages %d\n",
3165 __func__
, proc
->pid
, threads
, nodes
, incoming_refs
,
3166 outgoing_refs
, active_transactions
, buffers
, page_count
);
3171 static void binder_deferred_func(struct work_struct
*work
)
3173 struct binder_proc
*proc
;
3174 struct files_struct
*files
;
3179 binder_lock(__func__
);
3180 mutex_lock(&binder_deferred_lock
);
3181 if (!hlist_empty(&binder_deferred_list
)) {
3182 proc
= hlist_entry(binder_deferred_list
.first
,
3183 struct binder_proc
, deferred_work_node
);
3184 hlist_del_init(&proc
->deferred_work_node
);
3185 defer
= proc
->deferred_work
;
3186 proc
->deferred_work
= 0;
3191 mutex_unlock(&binder_deferred_lock
);
3194 if (defer
& BINDER_DEFERRED_PUT_FILES
) {
3195 files
= proc
->files
;
3200 if (defer
& BINDER_DEFERRED_FLUSH
)
3201 binder_deferred_flush(proc
);
3203 if (defer
& BINDER_DEFERRED_RELEASE
)
3204 binder_deferred_release(proc
); /* frees proc */
3206 binder_unlock(__func__
);
3208 put_files_struct(files
);
3211 static DECLARE_WORK(binder_deferred_work
, binder_deferred_func
);
3214 binder_defer_work(struct binder_proc
*proc
, enum binder_deferred_state defer
)
3216 mutex_lock(&binder_deferred_lock
);
3217 proc
->deferred_work
|= defer
;
3218 if (hlist_unhashed(&proc
->deferred_work_node
)) {
3219 hlist_add_head(&proc
->deferred_work_node
,
3220 &binder_deferred_list
);
3221 queue_work(binder_deferred_workqueue
, &binder_deferred_work
);
3223 mutex_unlock(&binder_deferred_lock
);
3226 static void print_binder_transaction(struct seq_file
*m
, const char *prefix
,
3227 struct binder_transaction
*t
)
3230 "%s %d: %pK from %d:%d to %d:%d code %x flags %x pri %ld r%d",
3231 prefix
, t
->debug_id
, t
,
3232 t
->from
? t
->from
->proc
->pid
: 0,
3233 t
->from
? t
->from
->pid
: 0,
3234 t
->to_proc
? t
->to_proc
->pid
: 0,
3235 t
->to_thread
? t
->to_thread
->pid
: 0,
3236 t
->code
, t
->flags
, t
->priority
, t
->need_reply
);
3237 if (t
->buffer
== NULL
) {
3238 seq_puts(m
, " buffer free\n");
3241 if (t
->buffer
->target_node
)
3242 seq_printf(m
, " node %d",
3243 t
->buffer
->target_node
->debug_id
);
3244 seq_printf(m
, " size %zd:%zd data %pK\n",
3245 t
->buffer
->data_size
, t
->buffer
->offsets_size
,
3249 static void print_binder_buffer(struct seq_file
*m
, const char *prefix
,
3250 struct binder_buffer
*buffer
)
3252 seq_printf(m
, "%s %d: %pK size %zd:%zd %s\n",
3253 prefix
, buffer
->debug_id
, buffer
->data
,
3254 buffer
->data_size
, buffer
->offsets_size
,
3255 buffer
->transaction
? "active" : "delivered");
3258 static void print_binder_work(struct seq_file
*m
, const char *prefix
,
3259 const char *transaction_prefix
,
3260 struct binder_work
*w
)
3262 struct binder_node
*node
;
3263 struct binder_transaction
*t
;
3266 case BINDER_WORK_TRANSACTION
:
3267 t
= container_of(w
, struct binder_transaction
, work
);
3268 print_binder_transaction(m
, transaction_prefix
, t
);
3270 case BINDER_WORK_TRANSACTION_COMPLETE
:
3271 seq_printf(m
, "%stransaction complete\n", prefix
);
3273 case BINDER_WORK_NODE
:
3274 node
= container_of(w
, struct binder_node
, work
);
3275 seq_printf(m
, "%snode work %d: u%016llx c%016llx\n",
3276 prefix
, node
->debug_id
,
3277 (u64
)node
->ptr
, (u64
)node
->cookie
);
3279 case BINDER_WORK_DEAD_BINDER
:
3280 seq_printf(m
, "%shas dead binder\n", prefix
);
3282 case BINDER_WORK_DEAD_BINDER_AND_CLEAR
:
3283 seq_printf(m
, "%shas cleared dead binder\n", prefix
);
3285 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION
:
3286 seq_printf(m
, "%shas cleared death notification\n", prefix
);
3289 seq_printf(m
, "%sunknown work: type %d\n", prefix
, w
->type
);
3294 static void print_binder_thread(struct seq_file
*m
,
3295 struct binder_thread
*thread
,
3298 struct binder_transaction
*t
;
3299 struct binder_work
*w
;
3300 size_t start_pos
= m
->count
;
3303 seq_printf(m
, " thread %d: l %02x\n", thread
->pid
, thread
->looper
);
3304 header_pos
= m
->count
;
3305 t
= thread
->transaction_stack
;
3307 if (t
->from
== thread
) {
3308 print_binder_transaction(m
,
3309 " outgoing transaction", t
);
3311 } else if (t
->to_thread
== thread
) {
3312 print_binder_transaction(m
,
3313 " incoming transaction", t
);
3316 print_binder_transaction(m
, " bad transaction", t
);
3320 list_for_each_entry(w
, &thread
->todo
, entry
) {
3321 print_binder_work(m
, " ", " pending transaction", w
);
3323 if (!print_always
&& m
->count
== header_pos
)
3324 m
->count
= start_pos
;
3327 static void print_binder_node(struct seq_file
*m
, struct binder_node
*node
)
3329 struct binder_ref
*ref
;
3330 struct binder_work
*w
;
3334 hlist_for_each_entry(ref
, &node
->refs
, node_entry
)
3337 seq_printf(m
, " node %d: u%016llx c%016llx hs %d hw %d ls %d lw %d is %d iw %d",
3338 node
->debug_id
, (u64
)node
->ptr
, (u64
)node
->cookie
,
3339 node
->has_strong_ref
, node
->has_weak_ref
,
3340 node
->local_strong_refs
, node
->local_weak_refs
,
3341 node
->internal_strong_refs
, count
);
3343 seq_puts(m
, " proc");
3344 hlist_for_each_entry(ref
, &node
->refs
, node_entry
)
3345 seq_printf(m
, " %d", ref
->proc
->pid
);
3348 list_for_each_entry(w
, &node
->async_todo
, entry
)
3349 print_binder_work(m
, " ",
3350 " pending async transaction", w
);
3353 static void print_binder_ref(struct seq_file
*m
, struct binder_ref
*ref
)
3355 seq_printf(m
, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
3356 ref
->debug_id
, ref
->desc
, ref
->node
->proc
? "" : "dead ",
3357 ref
->node
->debug_id
, ref
->strong
, ref
->weak
, ref
->death
);
3360 static void print_binder_proc(struct seq_file
*m
,
3361 struct binder_proc
*proc
, int print_all
)
3363 struct binder_work
*w
;
3365 size_t start_pos
= m
->count
;
3368 seq_printf(m
, "proc %d\n", proc
->pid
);
3369 header_pos
= m
->count
;
3371 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
))
3372 print_binder_thread(m
, rb_entry(n
, struct binder_thread
,
3373 rb_node
), print_all
);
3374 for (n
= rb_first(&proc
->nodes
); n
!= NULL
; n
= rb_next(n
)) {
3375 struct binder_node
*node
= rb_entry(n
, struct binder_node
,
3377 if (print_all
|| node
->has_async_transaction
)
3378 print_binder_node(m
, node
);
3381 for (n
= rb_first(&proc
->refs_by_desc
);
3384 print_binder_ref(m
, rb_entry(n
, struct binder_ref
,
3387 for (n
= rb_first(&proc
->allocated_buffers
); n
!= NULL
; n
= rb_next(n
))
3388 print_binder_buffer(m
, " buffer",
3389 rb_entry(n
, struct binder_buffer
, rb_node
));
3390 list_for_each_entry(w
, &proc
->todo
, entry
)
3391 print_binder_work(m
, " ", " pending transaction", w
);
3392 list_for_each_entry(w
, &proc
->delivered_death
, entry
) {
3393 seq_puts(m
, " has delivered dead binder\n");
3396 if (!print_all
&& m
->count
== header_pos
)
3397 m
->count
= start_pos
;
3400 static const char * const binder_return_strings
[] = {
3405 "BR_ACQUIRE_RESULT",
3407 "BR_TRANSACTION_COMPLETE",
3412 "BR_ATTEMPT_ACQUIRE",
3417 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
3421 static const char * const binder_command_strings
[] = {
3424 "BC_ACQUIRE_RESULT",
3432 "BC_ATTEMPT_ACQUIRE",
3433 "BC_REGISTER_LOOPER",
3436 "BC_REQUEST_DEATH_NOTIFICATION",
3437 "BC_CLEAR_DEATH_NOTIFICATION",
3438 "BC_DEAD_BINDER_DONE"
3441 static const char * const binder_objstat_strings
[] = {
3448 "transaction_complete"
3451 static void print_binder_stats(struct seq_file
*m
, const char *prefix
,
3452 struct binder_stats
*stats
)
3456 BUILD_BUG_ON(ARRAY_SIZE(stats
->bc
) !=
3457 ARRAY_SIZE(binder_command_strings
));
3458 for (i
= 0; i
< ARRAY_SIZE(stats
->bc
); i
++) {
3460 seq_printf(m
, "%s%s: %d\n", prefix
,
3461 binder_command_strings
[i
], stats
->bc
[i
]);
3464 BUILD_BUG_ON(ARRAY_SIZE(stats
->br
) !=
3465 ARRAY_SIZE(binder_return_strings
));
3466 for (i
= 0; i
< ARRAY_SIZE(stats
->br
); i
++) {
3468 seq_printf(m
, "%s%s: %d\n", prefix
,
3469 binder_return_strings
[i
], stats
->br
[i
]);
3472 BUILD_BUG_ON(ARRAY_SIZE(stats
->obj_created
) !=
3473 ARRAY_SIZE(binder_objstat_strings
));
3474 BUILD_BUG_ON(ARRAY_SIZE(stats
->obj_created
) !=
3475 ARRAY_SIZE(stats
->obj_deleted
));
3476 for (i
= 0; i
< ARRAY_SIZE(stats
->obj_created
); i
++) {
3477 if (stats
->obj_created
[i
] || stats
->obj_deleted
[i
])
3478 seq_printf(m
, "%s%s: active %d total %d\n", prefix
,
3479 binder_objstat_strings
[i
],
3480 stats
->obj_created
[i
] - stats
->obj_deleted
[i
],
3481 stats
->obj_created
[i
]);
3485 static void print_binder_proc_stats(struct seq_file
*m
,
3486 struct binder_proc
*proc
)
3488 struct binder_work
*w
;
3490 int count
, strong
, weak
;
3492 seq_printf(m
, "proc %d\n", proc
->pid
);
3494 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
))
3496 seq_printf(m
, " threads: %d\n", count
);
3497 seq_printf(m
, " requested threads: %d+%d/%d\n"
3498 " ready threads %d\n"
3499 " free async space %zd\n", proc
->requested_threads
,
3500 proc
->requested_threads_started
, proc
->max_threads
,
3501 proc
->ready_threads
, proc
->free_async_space
);
3503 for (n
= rb_first(&proc
->nodes
); n
!= NULL
; n
= rb_next(n
))
3505 seq_printf(m
, " nodes: %d\n", count
);
3509 for (n
= rb_first(&proc
->refs_by_desc
); n
!= NULL
; n
= rb_next(n
)) {
3510 struct binder_ref
*ref
= rb_entry(n
, struct binder_ref
,
3513 strong
+= ref
->strong
;
3516 seq_printf(m
, " refs: %d s %d w %d\n", count
, strong
, weak
);
3519 for (n
= rb_first(&proc
->allocated_buffers
); n
!= NULL
; n
= rb_next(n
))
3521 seq_printf(m
, " buffers: %d\n", count
);
3524 list_for_each_entry(w
, &proc
->todo
, entry
) {
3526 case BINDER_WORK_TRANSACTION
:
3533 seq_printf(m
, " pending transactions: %d\n", count
);
3535 print_binder_stats(m
, " ", &proc
->stats
);
3539 static int binder_state_show(struct seq_file
*m
, void *unused
)
3541 struct binder_proc
*proc
;
3542 struct binder_node
*node
;
3543 int do_lock
= !binder_debug_no_lock
;
3546 binder_lock(__func__
);
3548 seq_puts(m
, "binder state:\n");
3550 if (!hlist_empty(&binder_dead_nodes
))
3551 seq_puts(m
, "dead nodes:\n");
3552 hlist_for_each_entry(node
, &binder_dead_nodes
, dead_node
)
3553 print_binder_node(m
, node
);
3555 hlist_for_each_entry(proc
, &binder_procs
, proc_node
)
3556 print_binder_proc(m
, proc
, 1);
3558 binder_unlock(__func__
);
3562 static int binder_stats_show(struct seq_file
*m
, void *unused
)
3564 struct binder_proc
*proc
;
3565 int do_lock
= !binder_debug_no_lock
;
3568 binder_lock(__func__
);
3570 seq_puts(m
, "binder stats:\n");
3572 print_binder_stats(m
, "", &binder_stats
);
3574 hlist_for_each_entry(proc
, &binder_procs
, proc_node
)
3575 print_binder_proc_stats(m
, proc
);
3577 binder_unlock(__func__
);
3581 static int binder_transactions_show(struct seq_file
*m
, void *unused
)
3583 struct binder_proc
*proc
;
3584 int do_lock
= !binder_debug_no_lock
;
3587 binder_lock(__func__
);
3589 seq_puts(m
, "binder transactions:\n");
3590 hlist_for_each_entry(proc
, &binder_procs
, proc_node
)
3591 print_binder_proc(m
, proc
, 0);
3593 binder_unlock(__func__
);
3597 static int binder_proc_show(struct seq_file
*m
, void *unused
)
3599 struct binder_proc
*itr
;
3600 struct binder_proc
*proc
= m
->private;
3601 int do_lock
= !binder_debug_no_lock
;
3602 bool valid_proc
= false;
3605 binder_lock(__func__
);
3607 hlist_for_each_entry(itr
, &binder_procs
, proc_node
) {
3614 seq_puts(m
, "binder proc state:\n");
3615 print_binder_proc(m
, proc
, 1);
3618 binder_unlock(__func__
);
3622 static void print_binder_transaction_log_entry(struct seq_file
*m
,
3623 struct binder_transaction_log_entry
*e
)
3626 "%d: %s from %d:%d to %d:%d node %d handle %d size %d:%d\n",
3627 e
->debug_id
, (e
->call_type
== 2) ? "reply" :
3628 ((e
->call_type
== 1) ? "async" : "call "), e
->from_proc
,
3629 e
->from_thread
, e
->to_proc
, e
->to_thread
, e
->to_node
,
3630 e
->target_handle
, e
->data_size
, e
->offsets_size
);
3633 static int binder_transaction_log_show(struct seq_file
*m
, void *unused
)
3635 struct binder_transaction_log
*log
= m
->private;
3639 for (i
= log
->next
; i
< ARRAY_SIZE(log
->entry
); i
++)
3640 print_binder_transaction_log_entry(m
, &log
->entry
[i
]);
3642 for (i
= 0; i
< log
->next
; i
++)
3643 print_binder_transaction_log_entry(m
, &log
->entry
[i
]);
3647 static const struct file_operations binder_fops
= {
3648 .owner
= THIS_MODULE
,
3649 .poll
= binder_poll
,
3650 .unlocked_ioctl
= binder_ioctl
,
3651 .compat_ioctl
= binder_ioctl
,
3652 .mmap
= binder_mmap
,
3653 .open
= binder_open
,
3654 .flush
= binder_flush
,
3655 .release
= binder_release
,
3658 static struct miscdevice binder_miscdev
= {
3659 .minor
= MISC_DYNAMIC_MINOR
,
3661 .fops
= &binder_fops
3664 BINDER_DEBUG_ENTRY(state
);
3665 BINDER_DEBUG_ENTRY(stats
);
3666 BINDER_DEBUG_ENTRY(transactions
);
3667 BINDER_DEBUG_ENTRY(transaction_log
);
3669 static int __init
binder_init(void)
3673 binder_deferred_workqueue
= create_singlethread_workqueue("binder");
3674 if (!binder_deferred_workqueue
)
3677 binder_debugfs_dir_entry_root
= debugfs_create_dir("binder", NULL
);
3678 if (binder_debugfs_dir_entry_root
)
3679 binder_debugfs_dir_entry_proc
= debugfs_create_dir("proc",
3680 binder_debugfs_dir_entry_root
);
3681 ret
= misc_register(&binder_miscdev
);
3682 if (binder_debugfs_dir_entry_root
) {
3683 debugfs_create_file("state",
3685 binder_debugfs_dir_entry_root
,
3687 &binder_state_fops
);
3688 debugfs_create_file("stats",
3690 binder_debugfs_dir_entry_root
,
3692 &binder_stats_fops
);
3693 debugfs_create_file("transactions",
3695 binder_debugfs_dir_entry_root
,
3697 &binder_transactions_fops
);
3698 debugfs_create_file("transaction_log",
3700 binder_debugfs_dir_entry_root
,
3701 &binder_transaction_log
,
3702 &binder_transaction_log_fops
);
3703 debugfs_create_file("failed_transaction_log",
3705 binder_debugfs_dir_entry_root
,
3706 &binder_transaction_log_failed
,
3707 &binder_transaction_log_fops
);
3712 device_initcall(binder_init
);
3714 #define CREATE_TRACE_POINTS
3715 #include "binder_trace.h"
3717 MODULE_LICENSE("GPL v2");