3 * Android IPC Subsystem
5 * Copyright (C) 2007-2008 Google, Inc.
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 #include <asm/cacheflush.h>
21 #include <linux/fdtable.h>
22 #include <linux/file.h>
23 #include <linux/freezer.h>
25 #include <linux/list.h>
26 #include <linux/miscdevice.h>
28 #include <linux/module.h>
29 #include <linux/mutex.h>
30 #include <linux/nsproxy.h>
31 #include <linux/poll.h>
32 #include <linux/debugfs.h>
33 #include <linux/rbtree.h>
34 #include <linux/sched.h>
35 #include <linux/seq_file.h>
36 #include <linux/uaccess.h>
37 #include <linux/vmalloc.h>
38 #include <linux/slab.h>
39 #include <linux/pid_namespace.h>
40 #include <linux/security.h>
42 #ifdef CONFIG_ANDROID_BINDER_IPC_32BIT
43 #define BINDER_IPC_32BIT 1
46 #include <uapi/linux/android/binder.h>
47 #include "binder_trace.h"
49 static DEFINE_MUTEX(binder_main_lock
);
50 static DEFINE_MUTEX(binder_deferred_lock
);
51 static DEFINE_MUTEX(binder_mmap_lock
);
53 static HLIST_HEAD(binder_procs
);
54 static HLIST_HEAD(binder_deferred_list
);
55 static HLIST_HEAD(binder_dead_nodes
);
57 static struct dentry
*binder_debugfs_dir_entry_root
;
58 static struct dentry
*binder_debugfs_dir_entry_proc
;
59 static struct binder_node
*binder_context_mgr_node
;
60 static kuid_t binder_context_mgr_uid
= INVALID_UID
;
61 static int binder_last_id
;
62 static struct workqueue_struct
*binder_deferred_workqueue
;
64 #define BINDER_DEBUG_ENTRY(name) \
65 static int binder_##name##_open(struct inode *inode, struct file *file) \
67 return single_open(file, binder_##name##_show, inode->i_private); \
70 static const struct file_operations binder_##name##_fops = { \
71 .owner = THIS_MODULE, \
72 .open = binder_##name##_open, \
74 .llseek = seq_lseek, \
75 .release = single_release, \
78 static int binder_proc_show(struct seq_file
*m
, void *unused
);
79 BINDER_DEBUG_ENTRY(proc
);
81 /* This is only defined in include/asm-arm/sizes.h */
87 #define SZ_4M 0x400000
90 #define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
92 #define BINDER_SMALL_BUF_SIZE (PAGE_SIZE * 64)
95 BINDER_DEBUG_USER_ERROR
= 1U << 0,
96 BINDER_DEBUG_FAILED_TRANSACTION
= 1U << 1,
97 BINDER_DEBUG_DEAD_TRANSACTION
= 1U << 2,
98 BINDER_DEBUG_OPEN_CLOSE
= 1U << 3,
99 BINDER_DEBUG_DEAD_BINDER
= 1U << 4,
100 BINDER_DEBUG_DEATH_NOTIFICATION
= 1U << 5,
101 BINDER_DEBUG_READ_WRITE
= 1U << 6,
102 BINDER_DEBUG_USER_REFS
= 1U << 7,
103 BINDER_DEBUG_THREADS
= 1U << 8,
104 BINDER_DEBUG_TRANSACTION
= 1U << 9,
105 BINDER_DEBUG_TRANSACTION_COMPLETE
= 1U << 10,
106 BINDER_DEBUG_FREE_BUFFER
= 1U << 11,
107 BINDER_DEBUG_INTERNAL_REFS
= 1U << 12,
108 BINDER_DEBUG_BUFFER_ALLOC
= 1U << 13,
109 BINDER_DEBUG_PRIORITY_CAP
= 1U << 14,
110 BINDER_DEBUG_BUFFER_ALLOC_ASYNC
= 1U << 15,
112 static uint32_t binder_debug_mask
= BINDER_DEBUG_USER_ERROR
|
113 BINDER_DEBUG_FAILED_TRANSACTION
| BINDER_DEBUG_DEAD_TRANSACTION
;
114 module_param_named(debug_mask
, binder_debug_mask
, uint
, S_IWUSR
| S_IRUGO
);
116 static bool binder_debug_no_lock
;
117 module_param_named(proc_no_lock
, binder_debug_no_lock
, bool, S_IWUSR
| S_IRUGO
);
119 static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait
);
120 static int binder_stop_on_user_error
;
122 static int binder_set_stop_on_user_error(const char *val
,
123 struct kernel_param
*kp
)
127 ret
= param_set_int(val
, kp
);
128 if (binder_stop_on_user_error
< 2)
129 wake_up(&binder_user_error_wait
);
132 module_param_call(stop_on_user_error
, binder_set_stop_on_user_error
,
133 param_get_int
, &binder_stop_on_user_error
, S_IWUSR
| S_IRUGO
);
135 #define binder_debug(mask, x...) \
137 if (binder_debug_mask & mask) \
141 #define binder_user_error(x...) \
143 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
145 if (binder_stop_on_user_error) \
146 binder_stop_on_user_error = 2; \
149 enum binder_stat_types
{
155 BINDER_STAT_TRANSACTION
,
156 BINDER_STAT_TRANSACTION_COMPLETE
,
160 struct binder_stats
{
161 int br
[_IOC_NR(BR_FAILED_REPLY
) + 1];
162 int bc
[_IOC_NR(BC_DEAD_BINDER_DONE
) + 1];
163 int obj_created
[BINDER_STAT_COUNT
];
164 int obj_deleted
[BINDER_STAT_COUNT
];
167 static struct binder_stats binder_stats
;
169 static inline void binder_stats_deleted(enum binder_stat_types type
)
171 binder_stats
.obj_deleted
[type
]++;
174 static inline void binder_stats_created(enum binder_stat_types type
)
176 binder_stats
.obj_created
[type
]++;
179 struct binder_transaction_log_entry
{
191 struct binder_transaction_log
{
194 struct binder_transaction_log_entry entry
[32];
196 static struct binder_transaction_log binder_transaction_log
;
197 static struct binder_transaction_log binder_transaction_log_failed
;
199 static struct binder_transaction_log_entry
*binder_transaction_log_add(
200 struct binder_transaction_log
*log
)
202 struct binder_transaction_log_entry
*e
;
204 e
= &log
->entry
[log
->next
];
205 memset(e
, 0, sizeof(*e
));
207 if (log
->next
== ARRAY_SIZE(log
->entry
)) {
215 struct list_head entry
;
217 BINDER_WORK_TRANSACTION
= 1,
218 BINDER_WORK_TRANSACTION_COMPLETE
,
220 BINDER_WORK_DEAD_BINDER
,
221 BINDER_WORK_DEAD_BINDER_AND_CLEAR
,
222 BINDER_WORK_CLEAR_DEATH_NOTIFICATION
,
228 struct binder_work work
;
230 struct rb_node rb_node
;
231 struct hlist_node dead_node
;
233 struct binder_proc
*proc
;
234 struct hlist_head refs
;
235 int internal_strong_refs
;
237 int local_strong_refs
;
238 binder_uintptr_t ptr
;
239 binder_uintptr_t cookie
;
240 unsigned has_strong_ref
:1;
241 unsigned pending_strong_ref
:1;
242 unsigned has_weak_ref
:1;
243 unsigned pending_weak_ref
:1;
244 unsigned has_async_transaction
:1;
245 unsigned accept_fds
:1;
246 unsigned min_priority
:8;
247 struct list_head async_todo
;
250 struct binder_ref_death
{
251 struct binder_work work
;
252 binder_uintptr_t cookie
;
256 /* Lookups needed: */
257 /* node + proc => ref (transaction) */
258 /* desc + proc => ref (transaction, inc/dec ref) */
259 /* node => refs + procs (proc exit) */
261 struct rb_node rb_node_desc
;
262 struct rb_node rb_node_node
;
263 struct hlist_node node_entry
;
264 struct binder_proc
*proc
;
265 struct binder_node
*node
;
269 struct binder_ref_death
*death
;
272 struct binder_buffer
{
273 struct list_head entry
; /* free and allocated entries by address */
274 struct rb_node rb_node
; /* free entry by size or allocated entry */
277 unsigned allow_user_free
:1;
278 unsigned async_transaction
:1;
279 unsigned debug_id
:29;
281 struct binder_transaction
*transaction
;
283 struct binder_node
*target_node
;
289 enum binder_deferred_state
{
290 BINDER_DEFERRED_PUT_FILES
= 0x01,
291 BINDER_DEFERRED_FLUSH
= 0x02,
292 BINDER_DEFERRED_RELEASE
= 0x04,
296 struct hlist_node proc_node
;
297 struct rb_root threads
;
298 struct rb_root nodes
;
299 struct rb_root refs_by_desc
;
300 struct rb_root refs_by_node
;
302 struct vm_area_struct
*vma
;
303 struct mm_struct
*vma_vm_mm
;
304 struct task_struct
*tsk
;
305 struct files_struct
*files
;
306 struct hlist_node deferred_work_node
;
309 ptrdiff_t user_buffer_offset
;
311 struct list_head buffers
;
312 struct rb_root free_buffers
;
313 struct rb_root allocated_buffers
;
314 size_t free_async_space
;
318 uint32_t buffer_free
;
319 struct list_head todo
;
320 wait_queue_head_t wait
;
321 struct binder_stats stats
;
322 struct list_head delivered_death
;
324 int requested_threads
;
325 int requested_threads_started
;
327 long default_priority
;
328 struct dentry
*debugfs_entry
;
332 BINDER_LOOPER_STATE_REGISTERED
= 0x01,
333 BINDER_LOOPER_STATE_ENTERED
= 0x02,
334 BINDER_LOOPER_STATE_EXITED
= 0x04,
335 BINDER_LOOPER_STATE_INVALID
= 0x08,
336 BINDER_LOOPER_STATE_WAITING
= 0x10,
337 BINDER_LOOPER_STATE_NEED_RETURN
= 0x20
340 struct binder_thread
{
341 struct binder_proc
*proc
;
342 struct rb_node rb_node
;
345 struct binder_transaction
*transaction_stack
;
346 struct list_head todo
;
347 uint32_t return_error
; /* Write failed, return error code in read buf */
348 uint32_t return_error2
; /* Write failed, return error code in read */
349 /* buffer. Used when sending a reply to a dead process that */
350 /* we are also waiting on */
351 wait_queue_head_t wait
;
352 struct binder_stats stats
;
355 struct binder_transaction
{
357 struct binder_work work
;
358 struct binder_thread
*from
;
359 struct binder_transaction
*from_parent
;
360 struct binder_proc
*to_proc
;
361 struct binder_thread
*to_thread
;
362 struct binder_transaction
*to_parent
;
363 unsigned need_reply
:1;
364 /* unsigned is_dead:1; */ /* not used at the moment */
366 struct binder_buffer
*buffer
;
375 binder_defer_work(struct binder_proc
*proc
, enum binder_deferred_state defer
);
377 static int task_get_unused_fd_flags(struct binder_proc
*proc
, int flags
)
379 struct files_struct
*files
= proc
->files
;
380 unsigned long rlim_cur
;
386 if (!lock_task_sighand(proc
->tsk
, &irqs
))
389 rlim_cur
= task_rlimit(proc
->tsk
, RLIMIT_NOFILE
);
390 unlock_task_sighand(proc
->tsk
, &irqs
);
392 return __alloc_fd(files
, 0, rlim_cur
, flags
);
396 * copied from fd_install
398 static void task_fd_install(
399 struct binder_proc
*proc
, unsigned int fd
, struct file
*file
)
402 __fd_install(proc
->files
, fd
, file
);
406 * copied from sys_close
408 static long task_close_fd(struct binder_proc
*proc
, unsigned int fd
)
412 if (proc
->files
== NULL
)
415 retval
= __close_fd(proc
->files
, fd
);
416 /* can't restart close syscall because file table entry was cleared */
417 if (unlikely(retval
== -ERESTARTSYS
||
418 retval
== -ERESTARTNOINTR
||
419 retval
== -ERESTARTNOHAND
||
420 retval
== -ERESTART_RESTARTBLOCK
))
426 static inline void binder_lock(const char *tag
)
428 trace_binder_lock(tag
);
429 mutex_lock(&binder_main_lock
);
430 trace_binder_locked(tag
);
433 static inline void binder_unlock(const char *tag
)
435 trace_binder_unlock(tag
);
436 mutex_unlock(&binder_main_lock
);
439 static void binder_set_nice(long nice
)
443 if (can_nice(current
, nice
)) {
444 set_user_nice(current
, nice
);
447 min_nice
= rlimit_to_nice(current
->signal
->rlim
[RLIMIT_NICE
].rlim_cur
);
448 binder_debug(BINDER_DEBUG_PRIORITY_CAP
,
449 "%d: nice value %ld not allowed use %ld instead\n",
450 current
->pid
, nice
, min_nice
);
451 set_user_nice(current
, min_nice
);
452 if (min_nice
<= MAX_NICE
)
454 binder_user_error("%d RLIMIT_NICE not set\n", current
->pid
);
457 static size_t binder_buffer_size(struct binder_proc
*proc
,
458 struct binder_buffer
*buffer
)
460 if (list_is_last(&buffer
->entry
, &proc
->buffers
))
461 return proc
->buffer
+ proc
->buffer_size
- (void *)buffer
->data
;
462 return (size_t)list_entry(buffer
->entry
.next
,
463 struct binder_buffer
, entry
) - (size_t)buffer
->data
;
466 static void binder_insert_free_buffer(struct binder_proc
*proc
,
467 struct binder_buffer
*new_buffer
)
469 struct rb_node
**p
= &proc
->free_buffers
.rb_node
;
470 struct rb_node
*parent
= NULL
;
471 struct binder_buffer
*buffer
;
473 size_t new_buffer_size
;
475 BUG_ON(!new_buffer
->free
);
477 new_buffer_size
= binder_buffer_size(proc
, new_buffer
);
479 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
480 "%d: add free buffer, size %zd, at %p\n",
481 proc
->pid
, new_buffer_size
, new_buffer
);
485 buffer
= rb_entry(parent
, struct binder_buffer
, rb_node
);
486 BUG_ON(!buffer
->free
);
488 buffer_size
= binder_buffer_size(proc
, buffer
);
490 if (new_buffer_size
< buffer_size
)
491 p
= &parent
->rb_left
;
493 p
= &parent
->rb_right
;
495 rb_link_node(&new_buffer
->rb_node
, parent
, p
);
496 rb_insert_color(&new_buffer
->rb_node
, &proc
->free_buffers
);
499 static void binder_insert_allocated_buffer(struct binder_proc
*proc
,
500 struct binder_buffer
*new_buffer
)
502 struct rb_node
**p
= &proc
->allocated_buffers
.rb_node
;
503 struct rb_node
*parent
= NULL
;
504 struct binder_buffer
*buffer
;
506 BUG_ON(new_buffer
->free
);
510 buffer
= rb_entry(parent
, struct binder_buffer
, rb_node
);
511 BUG_ON(buffer
->free
);
513 if (new_buffer
< buffer
)
514 p
= &parent
->rb_left
;
515 else if (new_buffer
> buffer
)
516 p
= &parent
->rb_right
;
520 rb_link_node(&new_buffer
->rb_node
, parent
, p
);
521 rb_insert_color(&new_buffer
->rb_node
, &proc
->allocated_buffers
);
524 static struct binder_buffer
*binder_buffer_lookup(struct binder_proc
*proc
,
527 struct rb_node
*n
= proc
->allocated_buffers
.rb_node
;
528 struct binder_buffer
*buffer
;
529 struct binder_buffer
*kern_ptr
;
531 kern_ptr
= (struct binder_buffer
*)(user_ptr
- proc
->user_buffer_offset
532 - offsetof(struct binder_buffer
, data
));
535 buffer
= rb_entry(n
, struct binder_buffer
, rb_node
);
536 BUG_ON(buffer
->free
);
538 if (kern_ptr
< buffer
)
540 else if (kern_ptr
> buffer
)
548 static int binder_update_page_range(struct binder_proc
*proc
, int allocate
,
549 void *start
, void *end
,
550 struct vm_area_struct
*vma
)
553 unsigned long user_page_addr
;
555 struct mm_struct
*mm
;
557 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
558 "%d: %s pages %p-%p\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 %p\n",
599 proc
->pid
, page_addr
);
600 goto err_alloc_page_failed
;
602 ret
= map_kernel_range_noflush((unsigned long)page_addr
,
603 PAGE_SIZE
, PAGE_KERNEL
, page
);
604 flush_cache_vmap((unsigned long)page_addr
,
605 (unsigned long)page_addr
+ PAGE_SIZE
);
607 pr_err("%d: binder_alloc_buf failed to map page at %p in kernel\n",
608 proc
->pid
, page_addr
);
609 goto err_map_kernel_failed
;
612 (uintptr_t)page_addr
+ proc
->user_buffer_offset
;
613 ret
= vm_insert_page(vma
, user_page_addr
, page
[0]);
615 pr_err("%d: binder_alloc_buf failed to map page at %lx in userspace\n",
616 proc
->pid
, user_page_addr
);
617 goto err_vm_insert_page_failed
;
619 /* vm_insert_page does not seem to increment the refcount */
622 up_write(&mm
->mmap_sem
);
628 for (page_addr
= end
- PAGE_SIZE
; page_addr
>= start
;
629 page_addr
-= PAGE_SIZE
) {
630 page
= &proc
->pages
[(page_addr
- proc
->buffer
) / PAGE_SIZE
];
632 zap_page_range(vma
, (uintptr_t)page_addr
+
633 proc
->user_buffer_offset
, PAGE_SIZE
, NULL
);
634 err_vm_insert_page_failed
:
635 unmap_kernel_range((unsigned long)page_addr
, PAGE_SIZE
);
636 err_map_kernel_failed
:
639 err_alloc_page_failed
:
644 up_write(&mm
->mmap_sem
);
650 static struct binder_buffer
*binder_alloc_buf(struct binder_proc
*proc
,
652 size_t offsets_size
, int is_async
)
654 struct rb_node
*n
= proc
->free_buffers
.rb_node
;
655 struct binder_buffer
*buffer
;
657 struct rb_node
*best_fit
= NULL
;
662 if (proc
->vma
== NULL
) {
663 pr_err("%d: binder_alloc_buf, no vma\n",
668 size
= ALIGN(data_size
, sizeof(void *)) +
669 ALIGN(offsets_size
, sizeof(void *));
671 if (size
< data_size
|| size
< offsets_size
) {
672 binder_user_error("%d: got transaction with invalid size %zd-%zd\n",
673 proc
->pid
, data_size
, offsets_size
);
678 proc
->free_async_space
< size
+ sizeof(struct binder_buffer
)) {
679 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
680 "%d: binder_alloc_buf size %zd failed, no async space left\n",
686 buffer
= rb_entry(n
, struct binder_buffer
, rb_node
);
687 BUG_ON(!buffer
->free
);
688 buffer_size
= binder_buffer_size(proc
, buffer
);
690 if (size
< buffer_size
) {
693 } else if (size
> buffer_size
)
700 if (best_fit
== NULL
) {
701 pr_err("%d: binder_alloc_buf size %zd failed, no address space\n",
706 buffer
= rb_entry(best_fit
, struct binder_buffer
, rb_node
);
707 buffer_size
= binder_buffer_size(proc
, buffer
);
710 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
711 "%d: binder_alloc_buf size %zd got buffer %p size %zd\n",
712 proc
->pid
, size
, buffer
, buffer_size
);
715 (void *)(((uintptr_t)buffer
->data
+ buffer_size
) & PAGE_MASK
);
717 if (size
+ sizeof(struct binder_buffer
) + 4 >= buffer_size
)
718 buffer_size
= size
; /* no room for other buffers */
720 buffer_size
= size
+ sizeof(struct binder_buffer
);
723 (void *)PAGE_ALIGN((uintptr_t)buffer
->data
+ buffer_size
);
724 if (end_page_addr
> has_page_addr
)
725 end_page_addr
= has_page_addr
;
726 if (binder_update_page_range(proc
, 1,
727 (void *)PAGE_ALIGN((uintptr_t)buffer
->data
), end_page_addr
, NULL
))
730 rb_erase(best_fit
, &proc
->free_buffers
);
732 binder_insert_allocated_buffer(proc
, buffer
);
733 if (buffer_size
!= size
) {
734 struct binder_buffer
*new_buffer
= (void *)buffer
->data
+ size
;
736 list_add(&new_buffer
->entry
, &buffer
->entry
);
737 new_buffer
->free
= 1;
738 binder_insert_free_buffer(proc
, new_buffer
);
740 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
741 "%d: binder_alloc_buf size %zd got %p\n",
742 proc
->pid
, size
, buffer
);
743 buffer
->data_size
= data_size
;
744 buffer
->offsets_size
= offsets_size
;
745 buffer
->async_transaction
= is_async
;
747 proc
->free_async_space
-= size
+ sizeof(struct binder_buffer
);
748 binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC
,
749 "%d: binder_alloc_buf size %zd async free %zd\n",
750 proc
->pid
, size
, proc
->free_async_space
);
756 static void *buffer_start_page(struct binder_buffer
*buffer
)
758 return (void *)((uintptr_t)buffer
& PAGE_MASK
);
761 static void *buffer_end_page(struct binder_buffer
*buffer
)
763 return (void *)(((uintptr_t)(buffer
+ 1) - 1) & PAGE_MASK
);
766 static void binder_delete_free_buffer(struct binder_proc
*proc
,
767 struct binder_buffer
*buffer
)
769 struct binder_buffer
*prev
, *next
= NULL
;
770 int free_page_end
= 1;
771 int free_page_start
= 1;
773 BUG_ON(proc
->buffers
.next
== &buffer
->entry
);
774 prev
= list_entry(buffer
->entry
.prev
, struct binder_buffer
, entry
);
776 if (buffer_end_page(prev
) == buffer_start_page(buffer
)) {
778 if (buffer_end_page(prev
) == buffer_end_page(buffer
))
780 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
781 "%d: merge free, buffer %p share page with %p\n",
782 proc
->pid
, buffer
, prev
);
785 if (!list_is_last(&buffer
->entry
, &proc
->buffers
)) {
786 next
= list_entry(buffer
->entry
.next
,
787 struct binder_buffer
, entry
);
788 if (buffer_start_page(next
) == buffer_end_page(buffer
)) {
790 if (buffer_start_page(next
) ==
791 buffer_start_page(buffer
))
793 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
794 "%d: merge free, buffer %p share page with %p\n",
795 proc
->pid
, buffer
, prev
);
798 list_del(&buffer
->entry
);
799 if (free_page_start
|| free_page_end
) {
800 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
801 "%d: merge free, buffer %p do not share page%s%s with %p or %p\n",
802 proc
->pid
, buffer
, free_page_start
? "" : " end",
803 free_page_end
? "" : " start", prev
, next
);
804 binder_update_page_range(proc
, 0, free_page_start
?
805 buffer_start_page(buffer
) : buffer_end_page(buffer
),
806 (free_page_end
? buffer_end_page(buffer
) :
807 buffer_start_page(buffer
)) + PAGE_SIZE
, NULL
);
811 static void binder_free_buf(struct binder_proc
*proc
,
812 struct binder_buffer
*buffer
)
814 size_t size
, buffer_size
;
816 buffer_size
= binder_buffer_size(proc
, buffer
);
818 size
= ALIGN(buffer
->data_size
, sizeof(void *)) +
819 ALIGN(buffer
->offsets_size
, sizeof(void *));
821 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
822 "%d: binder_free_buf %p size %zd buffer_size %zd\n",
823 proc
->pid
, buffer
, size
, buffer_size
);
825 BUG_ON(buffer
->free
);
826 BUG_ON(size
> buffer_size
);
827 BUG_ON(buffer
->transaction
!= NULL
);
828 BUG_ON((void *)buffer
< proc
->buffer
);
829 BUG_ON((void *)buffer
> proc
->buffer
+ proc
->buffer_size
);
831 if (buffer
->async_transaction
) {
832 proc
->free_async_space
+= size
+ sizeof(struct binder_buffer
);
834 binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC
,
835 "%d: binder_free_buf size %zd async free %zd\n",
836 proc
->pid
, size
, proc
->free_async_space
);
839 binder_update_page_range(proc
, 0,
840 (void *)PAGE_ALIGN((uintptr_t)buffer
->data
),
841 (void *)(((uintptr_t)buffer
->data
+ buffer_size
) & PAGE_MASK
),
843 rb_erase(&buffer
->rb_node
, &proc
->allocated_buffers
);
845 if (!list_is_last(&buffer
->entry
, &proc
->buffers
)) {
846 struct binder_buffer
*next
= list_entry(buffer
->entry
.next
,
847 struct binder_buffer
, entry
);
850 rb_erase(&next
->rb_node
, &proc
->free_buffers
);
851 binder_delete_free_buffer(proc
, next
);
854 if (proc
->buffers
.next
!= &buffer
->entry
) {
855 struct binder_buffer
*prev
= list_entry(buffer
->entry
.prev
,
856 struct binder_buffer
, entry
);
859 binder_delete_free_buffer(proc
, buffer
);
860 rb_erase(&prev
->rb_node
, &proc
->free_buffers
);
864 binder_insert_free_buffer(proc
, buffer
);
867 static struct binder_node
*binder_get_node(struct binder_proc
*proc
,
868 binder_uintptr_t ptr
)
870 struct rb_node
*n
= proc
->nodes
.rb_node
;
871 struct binder_node
*node
;
874 node
= rb_entry(n
, struct binder_node
, rb_node
);
878 else if (ptr
> node
->ptr
)
886 static struct binder_node
*binder_new_node(struct binder_proc
*proc
,
887 binder_uintptr_t ptr
,
888 binder_uintptr_t cookie
)
890 struct rb_node
**p
= &proc
->nodes
.rb_node
;
891 struct rb_node
*parent
= NULL
;
892 struct binder_node
*node
;
896 node
= rb_entry(parent
, struct binder_node
, rb_node
);
900 else if (ptr
> node
->ptr
)
906 node
= kzalloc(sizeof(*node
), GFP_KERNEL
);
909 binder_stats_created(BINDER_STAT_NODE
);
910 rb_link_node(&node
->rb_node
, parent
, p
);
911 rb_insert_color(&node
->rb_node
, &proc
->nodes
);
912 node
->debug_id
= ++binder_last_id
;
915 node
->cookie
= cookie
;
916 node
->work
.type
= BINDER_WORK_NODE
;
917 INIT_LIST_HEAD(&node
->work
.entry
);
918 INIT_LIST_HEAD(&node
->async_todo
);
919 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
920 "%d:%d node %d u%016llx c%016llx created\n",
921 proc
->pid
, current
->pid
, node
->debug_id
,
922 (u64
)node
->ptr
, (u64
)node
->cookie
);
926 static int binder_inc_node(struct binder_node
*node
, int strong
, int internal
,
927 struct list_head
*target_list
)
931 if (target_list
== NULL
&&
932 node
->internal_strong_refs
== 0 &&
933 !(node
== binder_context_mgr_node
&&
934 node
->has_strong_ref
)) {
935 pr_err("invalid inc strong node for %d\n",
939 node
->internal_strong_refs
++;
941 node
->local_strong_refs
++;
942 if (!node
->has_strong_ref
&& target_list
) {
943 list_del_init(&node
->work
.entry
);
944 list_add_tail(&node
->work
.entry
, target_list
);
948 node
->local_weak_refs
++;
949 if (!node
->has_weak_ref
&& list_empty(&node
->work
.entry
)) {
950 if (target_list
== NULL
) {
951 pr_err("invalid inc weak node for %d\n",
955 list_add_tail(&node
->work
.entry
, target_list
);
961 static int binder_dec_node(struct binder_node
*node
, int strong
, int internal
)
965 node
->internal_strong_refs
--;
967 node
->local_strong_refs
--;
968 if (node
->local_strong_refs
|| node
->internal_strong_refs
)
972 node
->local_weak_refs
--;
973 if (node
->local_weak_refs
|| !hlist_empty(&node
->refs
))
976 if (node
->proc
&& (node
->has_strong_ref
|| node
->has_weak_ref
)) {
977 if (list_empty(&node
->work
.entry
)) {
978 list_add_tail(&node
->work
.entry
, &node
->proc
->todo
);
979 wake_up_interruptible(&node
->proc
->wait
);
982 if (hlist_empty(&node
->refs
) && !node
->local_strong_refs
&&
983 !node
->local_weak_refs
) {
984 list_del_init(&node
->work
.entry
);
986 rb_erase(&node
->rb_node
, &node
->proc
->nodes
);
987 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
988 "refless node %d deleted\n",
991 hlist_del(&node
->dead_node
);
992 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
993 "dead node %d deleted\n",
997 binder_stats_deleted(BINDER_STAT_NODE
);
1005 static struct binder_ref
*binder_get_ref(struct binder_proc
*proc
,
1008 struct rb_node
*n
= proc
->refs_by_desc
.rb_node
;
1009 struct binder_ref
*ref
;
1012 ref
= rb_entry(n
, struct binder_ref
, rb_node_desc
);
1014 if (desc
< ref
->desc
)
1016 else if (desc
> ref
->desc
)
1024 static struct binder_ref
*binder_get_ref_for_node(struct binder_proc
*proc
,
1025 struct binder_node
*node
)
1028 struct rb_node
**p
= &proc
->refs_by_node
.rb_node
;
1029 struct rb_node
*parent
= NULL
;
1030 struct binder_ref
*ref
, *new_ref
;
1034 ref
= rb_entry(parent
, struct binder_ref
, rb_node_node
);
1036 if (node
< ref
->node
)
1038 else if (node
> ref
->node
)
1039 p
= &(*p
)->rb_right
;
1043 new_ref
= kzalloc(sizeof(*ref
), GFP_KERNEL
);
1044 if (new_ref
== NULL
)
1046 binder_stats_created(BINDER_STAT_REF
);
1047 new_ref
->debug_id
= ++binder_last_id
;
1048 new_ref
->proc
= proc
;
1049 new_ref
->node
= node
;
1050 rb_link_node(&new_ref
->rb_node_node
, parent
, p
);
1051 rb_insert_color(&new_ref
->rb_node_node
, &proc
->refs_by_node
);
1053 new_ref
->desc
= (node
== binder_context_mgr_node
) ? 0 : 1;
1054 for (n
= rb_first(&proc
->refs_by_desc
); n
!= NULL
; n
= rb_next(n
)) {
1055 ref
= rb_entry(n
, struct binder_ref
, rb_node_desc
);
1056 if (ref
->desc
> new_ref
->desc
)
1058 new_ref
->desc
= ref
->desc
+ 1;
1061 p
= &proc
->refs_by_desc
.rb_node
;
1064 ref
= rb_entry(parent
, struct binder_ref
, rb_node_desc
);
1066 if (new_ref
->desc
< ref
->desc
)
1068 else if (new_ref
->desc
> ref
->desc
)
1069 p
= &(*p
)->rb_right
;
1073 rb_link_node(&new_ref
->rb_node_desc
, parent
, p
);
1074 rb_insert_color(&new_ref
->rb_node_desc
, &proc
->refs_by_desc
);
1076 hlist_add_head(&new_ref
->node_entry
, &node
->refs
);
1078 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1079 "%d new ref %d desc %d for node %d\n",
1080 proc
->pid
, new_ref
->debug_id
, new_ref
->desc
,
1083 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1084 "%d new ref %d desc %d for dead node\n",
1085 proc
->pid
, new_ref
->debug_id
, new_ref
->desc
);
1090 static void binder_delete_ref(struct binder_ref
*ref
)
1092 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1093 "%d delete ref %d desc %d for node %d\n",
1094 ref
->proc
->pid
, ref
->debug_id
, ref
->desc
,
1095 ref
->node
->debug_id
);
1097 rb_erase(&ref
->rb_node_desc
, &ref
->proc
->refs_by_desc
);
1098 rb_erase(&ref
->rb_node_node
, &ref
->proc
->refs_by_node
);
1100 binder_dec_node(ref
->node
, 1, 1);
1101 hlist_del(&ref
->node_entry
);
1102 binder_dec_node(ref
->node
, 0, 1);
1104 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
1105 "%d delete ref %d desc %d has death notification\n",
1106 ref
->proc
->pid
, ref
->debug_id
, ref
->desc
);
1107 list_del(&ref
->death
->work
.entry
);
1109 binder_stats_deleted(BINDER_STAT_DEATH
);
1112 binder_stats_deleted(BINDER_STAT_REF
);
1115 static int binder_inc_ref(struct binder_ref
*ref
, int strong
,
1116 struct list_head
*target_list
)
1121 if (ref
->strong
== 0) {
1122 ret
= binder_inc_node(ref
->node
, 1, 1, target_list
);
1128 if (ref
->weak
== 0) {
1129 ret
= binder_inc_node(ref
->node
, 0, 1, target_list
);
1139 static int binder_dec_ref(struct binder_ref
*ref
, int strong
)
1142 if (ref
->strong
== 0) {
1143 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
1144 ref
->proc
->pid
, ref
->debug_id
,
1145 ref
->desc
, ref
->strong
, ref
->weak
);
1149 if (ref
->strong
== 0) {
1152 ret
= binder_dec_node(ref
->node
, strong
, 1);
1157 if (ref
->weak
== 0) {
1158 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
1159 ref
->proc
->pid
, ref
->debug_id
,
1160 ref
->desc
, ref
->strong
, ref
->weak
);
1165 if (ref
->strong
== 0 && ref
->weak
== 0)
1166 binder_delete_ref(ref
);
1170 static void binder_pop_transaction(struct binder_thread
*target_thread
,
1171 struct binder_transaction
*t
)
1173 if (target_thread
) {
1174 BUG_ON(target_thread
->transaction_stack
!= t
);
1175 BUG_ON(target_thread
->transaction_stack
->from
!= target_thread
);
1176 target_thread
->transaction_stack
=
1177 target_thread
->transaction_stack
->from_parent
;
1182 t
->buffer
->transaction
= NULL
;
1184 binder_stats_deleted(BINDER_STAT_TRANSACTION
);
1187 static void binder_send_failed_reply(struct binder_transaction
*t
,
1188 uint32_t error_code
)
1190 struct binder_thread
*target_thread
;
1191 struct binder_transaction
*next
;
1193 BUG_ON(t
->flags
& TF_ONE_WAY
);
1195 target_thread
= t
->from
;
1196 if (target_thread
) {
1197 if (target_thread
->return_error
!= BR_OK
&&
1198 target_thread
->return_error2
== BR_OK
) {
1199 target_thread
->return_error2
=
1200 target_thread
->return_error
;
1201 target_thread
->return_error
= BR_OK
;
1203 if (target_thread
->return_error
== BR_OK
) {
1204 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
1205 "send failed reply for transaction %d to %d:%d\n",
1207 target_thread
->proc
->pid
,
1208 target_thread
->pid
);
1210 binder_pop_transaction(target_thread
, t
);
1211 target_thread
->return_error
= error_code
;
1212 wake_up_interruptible(&target_thread
->wait
);
1214 pr_err("reply failed, target thread, %d:%d, has error code %d already\n",
1215 target_thread
->proc
->pid
,
1217 target_thread
->return_error
);
1221 next
= t
->from_parent
;
1223 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
1224 "send failed reply for transaction %d, target dead\n",
1227 binder_pop_transaction(target_thread
, t
);
1229 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
1230 "reply failed, no target thread at root\n");
1234 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
1235 "reply failed, no target thread -- retry %d\n",
1240 static void binder_transaction_buffer_release(struct binder_proc
*proc
,
1241 struct binder_buffer
*buffer
,
1242 binder_size_t
*failed_at
)
1244 binder_size_t
*offp
, *off_end
;
1245 int debug_id
= buffer
->debug_id
;
1247 binder_debug(BINDER_DEBUG_TRANSACTION
,
1248 "%d buffer release %d, size %zd-%zd, failed at %p\n",
1249 proc
->pid
, buffer
->debug_id
,
1250 buffer
->data_size
, buffer
->offsets_size
, failed_at
);
1252 if (buffer
->target_node
)
1253 binder_dec_node(buffer
->target_node
, 1, 0);
1255 offp
= (binder_size_t
*)(buffer
->data
+
1256 ALIGN(buffer
->data_size
, sizeof(void *)));
1258 off_end
= failed_at
;
1260 off_end
= (void *)offp
+ buffer
->offsets_size
;
1261 for (; offp
< off_end
; offp
++) {
1262 struct flat_binder_object
*fp
;
1264 if (*offp
> buffer
->data_size
- sizeof(*fp
) ||
1265 buffer
->data_size
< sizeof(*fp
) ||
1266 !IS_ALIGNED(*offp
, sizeof(u32
))) {
1267 pr_err("transaction release %d bad offset %lld, size %zd\n",
1268 debug_id
, (u64
)*offp
, buffer
->data_size
);
1271 fp
= (struct flat_binder_object
*)(buffer
->data
+ *offp
);
1273 case BINDER_TYPE_BINDER
:
1274 case BINDER_TYPE_WEAK_BINDER
: {
1275 struct binder_node
*node
= binder_get_node(proc
, fp
->binder
);
1278 pr_err("transaction release %d bad node %016llx\n",
1279 debug_id
, (u64
)fp
->binder
);
1282 binder_debug(BINDER_DEBUG_TRANSACTION
,
1283 " node %d u%016llx\n",
1284 node
->debug_id
, (u64
)node
->ptr
);
1285 binder_dec_node(node
, fp
->type
== BINDER_TYPE_BINDER
, 0);
1287 case BINDER_TYPE_HANDLE
:
1288 case BINDER_TYPE_WEAK_HANDLE
: {
1289 struct binder_ref
*ref
= binder_get_ref(proc
, fp
->handle
);
1292 pr_err("transaction release %d bad handle %d\n",
1293 debug_id
, fp
->handle
);
1296 binder_debug(BINDER_DEBUG_TRANSACTION
,
1297 " ref %d desc %d (node %d)\n",
1298 ref
->debug_id
, ref
->desc
, ref
->node
->debug_id
);
1299 binder_dec_ref(ref
, fp
->type
== BINDER_TYPE_HANDLE
);
1302 case BINDER_TYPE_FD
:
1303 binder_debug(BINDER_DEBUG_TRANSACTION
,
1304 " fd %d\n", fp
->handle
);
1306 task_close_fd(proc
, fp
->handle
);
1310 pr_err("transaction release %d bad object type %x\n",
1311 debug_id
, fp
->type
);
1317 static void binder_transaction(struct binder_proc
*proc
,
1318 struct binder_thread
*thread
,
1319 struct binder_transaction_data
*tr
, int reply
)
1321 struct binder_transaction
*t
;
1322 struct binder_work
*tcomplete
;
1323 binder_size_t
*offp
, *off_end
;
1324 binder_size_t off_min
;
1325 struct binder_proc
*target_proc
;
1326 struct binder_thread
*target_thread
= NULL
;
1327 struct binder_node
*target_node
= NULL
;
1328 struct list_head
*target_list
;
1329 wait_queue_head_t
*target_wait
;
1330 struct binder_transaction
*in_reply_to
= NULL
;
1331 struct binder_transaction_log_entry
*e
;
1332 uint32_t return_error
;
1334 e
= binder_transaction_log_add(&binder_transaction_log
);
1335 e
->call_type
= reply
? 2 : !!(tr
->flags
& TF_ONE_WAY
);
1336 e
->from_proc
= proc
->pid
;
1337 e
->from_thread
= thread
->pid
;
1338 e
->target_handle
= tr
->target
.handle
;
1339 e
->data_size
= tr
->data_size
;
1340 e
->offsets_size
= tr
->offsets_size
;
1343 in_reply_to
= thread
->transaction_stack
;
1344 if (in_reply_to
== NULL
) {
1345 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
1346 proc
->pid
, thread
->pid
);
1347 return_error
= BR_FAILED_REPLY
;
1348 goto err_empty_call_stack
;
1350 binder_set_nice(in_reply_to
->saved_priority
);
1351 if (in_reply_to
->to_thread
!= thread
) {
1352 binder_user_error("%d:%d got reply transaction with bad transaction stack, transaction %d has target %d:%d\n",
1353 proc
->pid
, thread
->pid
, in_reply_to
->debug_id
,
1354 in_reply_to
->to_proc
?
1355 in_reply_to
->to_proc
->pid
: 0,
1356 in_reply_to
->to_thread
?
1357 in_reply_to
->to_thread
->pid
: 0);
1358 return_error
= BR_FAILED_REPLY
;
1360 goto err_bad_call_stack
;
1362 thread
->transaction_stack
= in_reply_to
->to_parent
;
1363 target_thread
= in_reply_to
->from
;
1364 if (target_thread
== NULL
) {
1365 return_error
= BR_DEAD_REPLY
;
1366 goto err_dead_binder
;
1368 if (target_thread
->transaction_stack
!= in_reply_to
) {
1369 binder_user_error("%d:%d got reply transaction with bad target transaction stack %d, expected %d\n",
1370 proc
->pid
, thread
->pid
,
1371 target_thread
->transaction_stack
?
1372 target_thread
->transaction_stack
->debug_id
: 0,
1373 in_reply_to
->debug_id
);
1374 return_error
= BR_FAILED_REPLY
;
1376 target_thread
= NULL
;
1377 goto err_dead_binder
;
1379 target_proc
= target_thread
->proc
;
1381 if (tr
->target
.handle
) {
1382 struct binder_ref
*ref
;
1384 ref
= binder_get_ref(proc
, tr
->target
.handle
);
1386 binder_user_error("%d:%d got transaction to invalid handle\n",
1387 proc
->pid
, thread
->pid
);
1388 return_error
= BR_FAILED_REPLY
;
1389 goto err_invalid_target_handle
;
1391 target_node
= ref
->node
;
1393 target_node
= binder_context_mgr_node
;
1394 if (target_node
== NULL
) {
1395 return_error
= BR_DEAD_REPLY
;
1396 goto err_no_context_mgr_node
;
1399 e
->to_node
= target_node
->debug_id
;
1400 target_proc
= target_node
->proc
;
1401 if (target_proc
== NULL
) {
1402 return_error
= BR_DEAD_REPLY
;
1403 goto err_dead_binder
;
1405 if (security_binder_transaction(proc
->tsk
,
1406 target_proc
->tsk
) < 0) {
1407 return_error
= BR_FAILED_REPLY
;
1408 goto err_invalid_target_handle
;
1410 if (!(tr
->flags
& TF_ONE_WAY
) && thread
->transaction_stack
) {
1411 struct binder_transaction
*tmp
;
1413 tmp
= thread
->transaction_stack
;
1414 if (tmp
->to_thread
!= thread
) {
1415 binder_user_error("%d:%d got new transaction with bad transaction stack, transaction %d has target %d:%d\n",
1416 proc
->pid
, thread
->pid
, tmp
->debug_id
,
1417 tmp
->to_proc
? tmp
->to_proc
->pid
: 0,
1419 tmp
->to_thread
->pid
: 0);
1420 return_error
= BR_FAILED_REPLY
;
1421 goto err_bad_call_stack
;
1424 if (tmp
->from
&& tmp
->from
->proc
== target_proc
)
1425 target_thread
= tmp
->from
;
1426 tmp
= tmp
->from_parent
;
1430 if (target_thread
) {
1431 e
->to_thread
= target_thread
->pid
;
1432 target_list
= &target_thread
->todo
;
1433 target_wait
= &target_thread
->wait
;
1435 target_list
= &target_proc
->todo
;
1436 target_wait
= &target_proc
->wait
;
1438 e
->to_proc
= target_proc
->pid
;
1440 /* TODO: reuse incoming transaction for reply */
1441 t
= kzalloc(sizeof(*t
), GFP_KERNEL
);
1443 return_error
= BR_FAILED_REPLY
;
1444 goto err_alloc_t_failed
;
1446 binder_stats_created(BINDER_STAT_TRANSACTION
);
1448 tcomplete
= kzalloc(sizeof(*tcomplete
), GFP_KERNEL
);
1449 if (tcomplete
== NULL
) {
1450 return_error
= BR_FAILED_REPLY
;
1451 goto err_alloc_tcomplete_failed
;
1453 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE
);
1455 t
->debug_id
= ++binder_last_id
;
1456 e
->debug_id
= t
->debug_id
;
1459 binder_debug(BINDER_DEBUG_TRANSACTION
,
1460 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld\n",
1461 proc
->pid
, thread
->pid
, t
->debug_id
,
1462 target_proc
->pid
, target_thread
->pid
,
1463 (u64
)tr
->data
.ptr
.buffer
,
1464 (u64
)tr
->data
.ptr
.offsets
,
1465 (u64
)tr
->data_size
, (u64
)tr
->offsets_size
);
1467 binder_debug(BINDER_DEBUG_TRANSACTION
,
1468 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld\n",
1469 proc
->pid
, thread
->pid
, t
->debug_id
,
1470 target_proc
->pid
, target_node
->debug_id
,
1471 (u64
)tr
->data
.ptr
.buffer
,
1472 (u64
)tr
->data
.ptr
.offsets
,
1473 (u64
)tr
->data_size
, (u64
)tr
->offsets_size
);
1475 if (!reply
&& !(tr
->flags
& TF_ONE_WAY
))
1479 t
->sender_euid
= task_euid(proc
->tsk
);
1480 t
->to_proc
= target_proc
;
1481 t
->to_thread
= target_thread
;
1483 t
->flags
= tr
->flags
;
1484 t
->priority
= task_nice(current
);
1486 trace_binder_transaction(reply
, t
, target_node
);
1488 t
->buffer
= binder_alloc_buf(target_proc
, tr
->data_size
,
1489 tr
->offsets_size
, !reply
&& (t
->flags
& TF_ONE_WAY
));
1490 if (t
->buffer
== NULL
) {
1491 return_error
= BR_FAILED_REPLY
;
1492 goto err_binder_alloc_buf_failed
;
1494 t
->buffer
->allow_user_free
= 0;
1495 t
->buffer
->debug_id
= t
->debug_id
;
1496 t
->buffer
->transaction
= t
;
1497 t
->buffer
->target_node
= target_node
;
1498 trace_binder_transaction_alloc_buf(t
->buffer
);
1500 binder_inc_node(target_node
, 1, 0, NULL
);
1502 offp
= (binder_size_t
*)(t
->buffer
->data
+
1503 ALIGN(tr
->data_size
, sizeof(void *)));
1505 if (copy_from_user(t
->buffer
->data
, (const void __user
*)(uintptr_t)
1506 tr
->data
.ptr
.buffer
, tr
->data_size
)) {
1507 binder_user_error("%d:%d got transaction with invalid data ptr\n",
1508 proc
->pid
, thread
->pid
);
1509 return_error
= BR_FAILED_REPLY
;
1510 goto err_copy_data_failed
;
1512 if (copy_from_user(offp
, (const void __user
*)(uintptr_t)
1513 tr
->data
.ptr
.offsets
, tr
->offsets_size
)) {
1514 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
1515 proc
->pid
, thread
->pid
);
1516 return_error
= BR_FAILED_REPLY
;
1517 goto err_copy_data_failed
;
1519 if (!IS_ALIGNED(tr
->offsets_size
, sizeof(binder_size_t
))) {
1520 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
1521 proc
->pid
, thread
->pid
, (u64
)tr
->offsets_size
);
1522 return_error
= BR_FAILED_REPLY
;
1523 goto err_bad_offset
;
1525 off_end
= (void *)offp
+ tr
->offsets_size
;
1527 for (; offp
< off_end
; offp
++) {
1528 struct flat_binder_object
*fp
;
1530 if (*offp
> t
->buffer
->data_size
- sizeof(*fp
) ||
1532 t
->buffer
->data_size
< sizeof(*fp
) ||
1533 !IS_ALIGNED(*offp
, sizeof(u32
))) {
1534 binder_user_error("%d:%d got transaction with invalid offset, %lld (min %lld, max %lld)\n",
1535 proc
->pid
, thread
->pid
, (u64
)*offp
,
1537 (u64
)(t
->buffer
->data_size
-
1539 return_error
= BR_FAILED_REPLY
;
1540 goto err_bad_offset
;
1542 fp
= (struct flat_binder_object
*)(t
->buffer
->data
+ *offp
);
1543 off_min
= *offp
+ sizeof(struct flat_binder_object
);
1545 case BINDER_TYPE_BINDER
:
1546 case BINDER_TYPE_WEAK_BINDER
: {
1547 struct binder_ref
*ref
;
1548 struct binder_node
*node
= binder_get_node(proc
, fp
->binder
);
1551 node
= binder_new_node(proc
, fp
->binder
, fp
->cookie
);
1553 return_error
= BR_FAILED_REPLY
;
1554 goto err_binder_new_node_failed
;
1556 node
->min_priority
= fp
->flags
& FLAT_BINDER_FLAG_PRIORITY_MASK
;
1557 node
->accept_fds
= !!(fp
->flags
& FLAT_BINDER_FLAG_ACCEPTS_FDS
);
1559 if (fp
->cookie
!= node
->cookie
) {
1560 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
1561 proc
->pid
, thread
->pid
,
1562 (u64
)fp
->binder
, node
->debug_id
,
1563 (u64
)fp
->cookie
, (u64
)node
->cookie
);
1564 return_error
= BR_FAILED_REPLY
;
1565 goto err_binder_get_ref_for_node_failed
;
1567 if (security_binder_transfer_binder(proc
->tsk
,
1568 target_proc
->tsk
)) {
1569 return_error
= BR_FAILED_REPLY
;
1570 goto err_binder_get_ref_for_node_failed
;
1572 ref
= binder_get_ref_for_node(target_proc
, node
);
1574 return_error
= BR_FAILED_REPLY
;
1575 goto err_binder_get_ref_for_node_failed
;
1577 if (fp
->type
== BINDER_TYPE_BINDER
)
1578 fp
->type
= BINDER_TYPE_HANDLE
;
1580 fp
->type
= BINDER_TYPE_WEAK_HANDLE
;
1581 fp
->handle
= ref
->desc
;
1582 binder_inc_ref(ref
, fp
->type
== BINDER_TYPE_HANDLE
,
1585 trace_binder_transaction_node_to_ref(t
, node
, ref
);
1586 binder_debug(BINDER_DEBUG_TRANSACTION
,
1587 " node %d u%016llx -> ref %d desc %d\n",
1588 node
->debug_id
, (u64
)node
->ptr
,
1589 ref
->debug_id
, ref
->desc
);
1591 case BINDER_TYPE_HANDLE
:
1592 case BINDER_TYPE_WEAK_HANDLE
: {
1593 struct binder_ref
*ref
= binder_get_ref(proc
, fp
->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 (security_binder_transfer_binder(proc
->tsk
,
1603 target_proc
->tsk
)) {
1604 return_error
= BR_FAILED_REPLY
;
1605 goto err_binder_get_ref_failed
;
1607 if (ref
->node
->proc
== target_proc
) {
1608 if (fp
->type
== BINDER_TYPE_HANDLE
)
1609 fp
->type
= BINDER_TYPE_BINDER
;
1611 fp
->type
= BINDER_TYPE_WEAK_BINDER
;
1612 fp
->binder
= ref
->node
->ptr
;
1613 fp
->cookie
= ref
->node
->cookie
;
1614 binder_inc_node(ref
->node
, fp
->type
== BINDER_TYPE_BINDER
, 0, NULL
);
1615 trace_binder_transaction_ref_to_node(t
, ref
);
1616 binder_debug(BINDER_DEBUG_TRANSACTION
,
1617 " ref %d desc %d -> node %d u%016llx\n",
1618 ref
->debug_id
, ref
->desc
, ref
->node
->debug_id
,
1619 (u64
)ref
->node
->ptr
);
1621 struct binder_ref
*new_ref
;
1623 new_ref
= binder_get_ref_for_node(target_proc
, ref
->node
);
1624 if (new_ref
== NULL
) {
1625 return_error
= BR_FAILED_REPLY
;
1626 goto err_binder_get_ref_for_node_failed
;
1628 fp
->handle
= new_ref
->desc
;
1629 binder_inc_ref(new_ref
, fp
->type
== BINDER_TYPE_HANDLE
, NULL
);
1630 trace_binder_transaction_ref_to_ref(t
, ref
,
1632 binder_debug(BINDER_DEBUG_TRANSACTION
,
1633 " ref %d desc %d -> ref %d desc %d (node %d)\n",
1634 ref
->debug_id
, ref
->desc
, new_ref
->debug_id
,
1635 new_ref
->desc
, ref
->node
->debug_id
);
1639 case BINDER_TYPE_FD
: {
1644 if (!(in_reply_to
->flags
& TF_ACCEPT_FDS
)) {
1645 binder_user_error("%d:%d got reply with fd, %d, but target does not allow fds\n",
1646 proc
->pid
, thread
->pid
, fp
->handle
);
1647 return_error
= BR_FAILED_REPLY
;
1648 goto err_fd_not_allowed
;
1650 } else if (!target_node
->accept_fds
) {
1651 binder_user_error("%d:%d got transaction with fd, %d, but target does not allow fds\n",
1652 proc
->pid
, thread
->pid
, fp
->handle
);
1653 return_error
= BR_FAILED_REPLY
;
1654 goto err_fd_not_allowed
;
1657 file
= fget(fp
->handle
);
1659 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
1660 proc
->pid
, thread
->pid
, fp
->handle
);
1661 return_error
= BR_FAILED_REPLY
;
1662 goto err_fget_failed
;
1664 if (security_binder_transfer_file(proc
->tsk
,
1668 return_error
= BR_FAILED_REPLY
;
1669 goto err_get_unused_fd_failed
;
1671 target_fd
= task_get_unused_fd_flags(target_proc
, O_CLOEXEC
);
1672 if (target_fd
< 0) {
1674 return_error
= BR_FAILED_REPLY
;
1675 goto err_get_unused_fd_failed
;
1677 task_fd_install(target_proc
, target_fd
, file
);
1678 trace_binder_transaction_fd(t
, fp
->handle
, target_fd
);
1679 binder_debug(BINDER_DEBUG_TRANSACTION
,
1680 " fd %d -> %d\n", fp
->handle
, target_fd
);
1682 fp
->handle
= target_fd
;
1686 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
1687 proc
->pid
, thread
->pid
, fp
->type
);
1688 return_error
= BR_FAILED_REPLY
;
1689 goto err_bad_object_type
;
1693 BUG_ON(t
->buffer
->async_transaction
!= 0);
1694 binder_pop_transaction(target_thread
, in_reply_to
);
1695 } else if (!(t
->flags
& TF_ONE_WAY
)) {
1696 BUG_ON(t
->buffer
->async_transaction
!= 0);
1698 t
->from_parent
= thread
->transaction_stack
;
1699 thread
->transaction_stack
= t
;
1701 BUG_ON(target_node
== NULL
);
1702 BUG_ON(t
->buffer
->async_transaction
!= 1);
1703 if (target_node
->has_async_transaction
) {
1704 target_list
= &target_node
->async_todo
;
1707 target_node
->has_async_transaction
= 1;
1709 t
->work
.type
= BINDER_WORK_TRANSACTION
;
1710 list_add_tail(&t
->work
.entry
, target_list
);
1711 tcomplete
->type
= BINDER_WORK_TRANSACTION_COMPLETE
;
1712 list_add_tail(&tcomplete
->entry
, &thread
->todo
);
1714 wake_up_interruptible(target_wait
);
1717 err_get_unused_fd_failed
:
1720 err_binder_get_ref_for_node_failed
:
1721 err_binder_get_ref_failed
:
1722 err_binder_new_node_failed
:
1723 err_bad_object_type
:
1725 err_copy_data_failed
:
1726 trace_binder_transaction_failed_buffer_release(t
->buffer
);
1727 binder_transaction_buffer_release(target_proc
, t
->buffer
, offp
);
1728 t
->buffer
->transaction
= NULL
;
1729 binder_free_buf(target_proc
, t
->buffer
);
1730 err_binder_alloc_buf_failed
:
1732 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE
);
1733 err_alloc_tcomplete_failed
:
1735 binder_stats_deleted(BINDER_STAT_TRANSACTION
);
1738 err_empty_call_stack
:
1740 err_invalid_target_handle
:
1741 err_no_context_mgr_node
:
1742 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
1743 "%d:%d transaction failed %d, size %lld-%lld\n",
1744 proc
->pid
, thread
->pid
, return_error
,
1745 (u64
)tr
->data_size
, (u64
)tr
->offsets_size
);
1748 struct binder_transaction_log_entry
*fe
;
1750 fe
= binder_transaction_log_add(&binder_transaction_log_failed
);
1754 BUG_ON(thread
->return_error
!= BR_OK
);
1756 thread
->return_error
= BR_TRANSACTION_COMPLETE
;
1757 binder_send_failed_reply(in_reply_to
, return_error
);
1759 thread
->return_error
= return_error
;
1762 static int binder_thread_write(struct binder_proc
*proc
,
1763 struct binder_thread
*thread
,
1764 binder_uintptr_t binder_buffer
, size_t size
,
1765 binder_size_t
*consumed
)
1768 void __user
*buffer
= (void __user
*)(uintptr_t)binder_buffer
;
1769 void __user
*ptr
= buffer
+ *consumed
;
1770 void __user
*end
= buffer
+ size
;
1772 while (ptr
< end
&& thread
->return_error
== BR_OK
) {
1773 if (get_user(cmd
, (uint32_t __user
*)ptr
))
1775 ptr
+= sizeof(uint32_t);
1776 trace_binder_command(cmd
);
1777 if (_IOC_NR(cmd
) < ARRAY_SIZE(binder_stats
.bc
)) {
1778 binder_stats
.bc
[_IOC_NR(cmd
)]++;
1779 proc
->stats
.bc
[_IOC_NR(cmd
)]++;
1780 thread
->stats
.bc
[_IOC_NR(cmd
)]++;
1788 struct binder_ref
*ref
;
1789 const char *debug_string
;
1791 if (get_user(target
, (uint32_t __user
*)ptr
))
1793 ptr
+= sizeof(uint32_t);
1794 if (target
== 0 && binder_context_mgr_node
&&
1795 (cmd
== BC_INCREFS
|| cmd
== BC_ACQUIRE
)) {
1796 ref
= binder_get_ref_for_node(proc
,
1797 binder_context_mgr_node
);
1798 if (ref
->desc
!= target
) {
1799 binder_user_error("%d:%d tried to acquire reference to desc 0, got %d instead\n",
1800 proc
->pid
, thread
->pid
,
1804 ref
= binder_get_ref(proc
, target
);
1806 binder_user_error("%d:%d refcount change on invalid ref %d\n",
1807 proc
->pid
, thread
->pid
, target
);
1812 debug_string
= "IncRefs";
1813 binder_inc_ref(ref
, 0, NULL
);
1816 debug_string
= "Acquire";
1817 binder_inc_ref(ref
, 1, NULL
);
1820 debug_string
= "Release";
1821 binder_dec_ref(ref
, 1);
1825 debug_string
= "DecRefs";
1826 binder_dec_ref(ref
, 0);
1829 binder_debug(BINDER_DEBUG_USER_REFS
,
1830 "%d:%d %s ref %d desc %d s %d w %d for node %d\n",
1831 proc
->pid
, thread
->pid
, debug_string
, ref
->debug_id
,
1832 ref
->desc
, ref
->strong
, 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
);
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(cookie
);
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 %p\n",
2095 proc
->pid
, thread
->pid
, (u64
)cookie
,
2097 if (death
== NULL
) {
2098 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
2099 proc
->pid
, thread
->pid
, (u64
)cookie
);
2103 list_del_init(&death
->work
.entry
);
2104 if (death
->work
.type
== BINDER_WORK_DEAD_BINDER_AND_CLEAR
) {
2105 death
->work
.type
= BINDER_WORK_CLEAR_DEATH_NOTIFICATION
;
2106 if (thread
->looper
& (BINDER_LOOPER_STATE_REGISTERED
| BINDER_LOOPER_STATE_ENTERED
)) {
2107 list_add_tail(&death
->work
.entry
, &thread
->todo
);
2109 list_add_tail(&death
->work
.entry
, &proc
->todo
);
2110 wake_up_interruptible(&proc
->wait
);
2116 pr_err("%d:%d unknown command %d\n",
2117 proc
->pid
, thread
->pid
, cmd
);
2120 *consumed
= ptr
- buffer
;
2125 static void binder_stat_br(struct binder_proc
*proc
,
2126 struct binder_thread
*thread
, uint32_t cmd
)
2128 trace_binder_return(cmd
);
2129 if (_IOC_NR(cmd
) < ARRAY_SIZE(binder_stats
.br
)) {
2130 binder_stats
.br
[_IOC_NR(cmd
)]++;
2131 proc
->stats
.br
[_IOC_NR(cmd
)]++;
2132 thread
->stats
.br
[_IOC_NR(cmd
)]++;
2136 static int binder_has_proc_work(struct binder_proc
*proc
,
2137 struct binder_thread
*thread
)
2139 return !list_empty(&proc
->todo
) ||
2140 (thread
->looper
& BINDER_LOOPER_STATE_NEED_RETURN
);
2143 static int binder_has_thread_work(struct binder_thread
*thread
)
2145 return !list_empty(&thread
->todo
) || thread
->return_error
!= BR_OK
||
2146 (thread
->looper
& BINDER_LOOPER_STATE_NEED_RETURN
);
2149 static int binder_thread_read(struct binder_proc
*proc
,
2150 struct binder_thread
*thread
,
2151 binder_uintptr_t binder_buffer
, size_t size
,
2152 binder_size_t
*consumed
, int non_block
)
2154 void __user
*buffer
= (void __user
*)(uintptr_t)binder_buffer
;
2155 void __user
*ptr
= buffer
+ *consumed
;
2156 void __user
*end
= buffer
+ size
;
2159 int wait_for_proc_work
;
2161 if (*consumed
== 0) {
2162 if (put_user(BR_NOOP
, (uint32_t __user
*)ptr
))
2164 ptr
+= sizeof(uint32_t);
2168 wait_for_proc_work
= thread
->transaction_stack
== NULL
&&
2169 list_empty(&thread
->todo
);
2171 if (thread
->return_error
!= BR_OK
&& ptr
< end
) {
2172 if (thread
->return_error2
!= BR_OK
) {
2173 if (put_user(thread
->return_error2
, (uint32_t __user
*)ptr
))
2175 ptr
+= sizeof(uint32_t);
2176 binder_stat_br(proc
, thread
, thread
->return_error2
);
2179 thread
->return_error2
= BR_OK
;
2181 if (put_user(thread
->return_error
, (uint32_t __user
*)ptr
))
2183 ptr
+= sizeof(uint32_t);
2184 binder_stat_br(proc
, thread
, thread
->return_error
);
2185 thread
->return_error
= BR_OK
;
2190 thread
->looper
|= BINDER_LOOPER_STATE_WAITING
;
2191 if (wait_for_proc_work
)
2192 proc
->ready_threads
++;
2194 binder_unlock(__func__
);
2196 trace_binder_wait_for_work(wait_for_proc_work
,
2197 !!thread
->transaction_stack
,
2198 !list_empty(&thread
->todo
));
2199 if (wait_for_proc_work
) {
2200 if (!(thread
->looper
& (BINDER_LOOPER_STATE_REGISTERED
|
2201 BINDER_LOOPER_STATE_ENTERED
))) {
2202 binder_user_error("%d:%d ERROR: Thread waiting for process work before calling BC_REGISTER_LOOPER or BC_ENTER_LOOPER (state %x)\n",
2203 proc
->pid
, thread
->pid
, thread
->looper
);
2204 wait_event_interruptible(binder_user_error_wait
,
2205 binder_stop_on_user_error
< 2);
2207 binder_set_nice(proc
->default_priority
);
2209 if (!binder_has_proc_work(proc
, thread
))
2212 ret
= wait_event_freezable_exclusive(proc
->wait
, binder_has_proc_work(proc
, thread
));
2215 if (!binder_has_thread_work(thread
))
2218 ret
= wait_event_freezable(thread
->wait
, binder_has_thread_work(thread
));
2221 binder_lock(__func__
);
2223 if (wait_for_proc_work
)
2224 proc
->ready_threads
--;
2225 thread
->looper
&= ~BINDER_LOOPER_STATE_WAITING
;
2232 struct binder_transaction_data tr
;
2233 struct binder_work
*w
;
2234 struct binder_transaction
*t
= NULL
;
2236 if (!list_empty(&thread
->todo
)) {
2237 w
= list_first_entry(&thread
->todo
, struct binder_work
,
2239 } else if (!list_empty(&proc
->todo
) && wait_for_proc_work
) {
2240 w
= list_first_entry(&proc
->todo
, struct binder_work
,
2244 if (ptr
- buffer
== 4 &&
2245 !(thread
->looper
& BINDER_LOOPER_STATE_NEED_RETURN
))
2250 if (end
- ptr
< sizeof(tr
) + 4)
2254 case BINDER_WORK_TRANSACTION
: {
2255 t
= container_of(w
, struct binder_transaction
, work
);
2257 case BINDER_WORK_TRANSACTION_COMPLETE
: {
2258 cmd
= BR_TRANSACTION_COMPLETE
;
2259 if (put_user(cmd
, (uint32_t __user
*)ptr
))
2261 ptr
+= sizeof(uint32_t);
2263 binder_stat_br(proc
, thread
, cmd
);
2264 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE
,
2265 "%d:%d BR_TRANSACTION_COMPLETE\n",
2266 proc
->pid
, thread
->pid
);
2268 list_del(&w
->entry
);
2270 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE
);
2272 case BINDER_WORK_NODE
: {
2273 struct binder_node
*node
= container_of(w
, struct binder_node
, work
);
2274 uint32_t cmd
= BR_NOOP
;
2275 const char *cmd_name
;
2276 int strong
= node
->internal_strong_refs
|| node
->local_strong_refs
;
2277 int weak
= !hlist_empty(&node
->refs
) || node
->local_weak_refs
|| strong
;
2279 if (weak
&& !node
->has_weak_ref
) {
2281 cmd_name
= "BR_INCREFS";
2282 node
->has_weak_ref
= 1;
2283 node
->pending_weak_ref
= 1;
2284 node
->local_weak_refs
++;
2285 } else if (strong
&& !node
->has_strong_ref
) {
2287 cmd_name
= "BR_ACQUIRE";
2288 node
->has_strong_ref
= 1;
2289 node
->pending_strong_ref
= 1;
2290 node
->local_strong_refs
++;
2291 } else if (!strong
&& node
->has_strong_ref
) {
2293 cmd_name
= "BR_RELEASE";
2294 node
->has_strong_ref
= 0;
2295 } else if (!weak
&& node
->has_weak_ref
) {
2297 cmd_name
= "BR_DECREFS";
2298 node
->has_weak_ref
= 0;
2300 if (cmd
!= BR_NOOP
) {
2301 if (put_user(cmd
, (uint32_t __user
*)ptr
))
2303 ptr
+= sizeof(uint32_t);
2304 if (put_user(node
->ptr
,
2305 (binder_uintptr_t __user
*)ptr
))
2307 ptr
+= sizeof(binder_uintptr_t
);
2308 if (put_user(node
->cookie
,
2309 (binder_uintptr_t __user
*)ptr
))
2311 ptr
+= sizeof(binder_uintptr_t
);
2313 binder_stat_br(proc
, thread
, cmd
);
2314 binder_debug(BINDER_DEBUG_USER_REFS
,
2315 "%d:%d %s %d u%016llx c%016llx\n",
2316 proc
->pid
, thread
->pid
, cmd_name
,
2318 (u64
)node
->ptr
, (u64
)node
->cookie
);
2320 list_del_init(&w
->entry
);
2321 if (!weak
&& !strong
) {
2322 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
2323 "%d:%d node %d u%016llx c%016llx deleted\n",
2324 proc
->pid
, thread
->pid
,
2328 rb_erase(&node
->rb_node
, &proc
->nodes
);
2330 binder_stats_deleted(BINDER_STAT_NODE
);
2332 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
2333 "%d:%d node %d u%016llx c%016llx state unchanged\n",
2334 proc
->pid
, thread
->pid
,
2341 case BINDER_WORK_DEAD_BINDER
:
2342 case BINDER_WORK_DEAD_BINDER_AND_CLEAR
:
2343 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION
: {
2344 struct binder_ref_death
*death
;
2347 death
= container_of(w
, struct binder_ref_death
, work
);
2348 if (w
->type
== BINDER_WORK_CLEAR_DEATH_NOTIFICATION
)
2349 cmd
= BR_CLEAR_DEATH_NOTIFICATION_DONE
;
2351 cmd
= BR_DEAD_BINDER
;
2352 if (put_user(cmd
, (uint32_t __user
*)ptr
))
2354 ptr
+= sizeof(uint32_t);
2355 if (put_user(death
->cookie
,
2356 (binder_uintptr_t __user
*)ptr
))
2358 ptr
+= sizeof(binder_uintptr_t
);
2359 binder_stat_br(proc
, thread
, cmd
);
2360 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION
,
2361 "%d:%d %s %016llx\n",
2362 proc
->pid
, thread
->pid
,
2363 cmd
== BR_DEAD_BINDER
?
2365 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
2366 (u64
)death
->cookie
);
2368 if (w
->type
== BINDER_WORK_CLEAR_DEATH_NOTIFICATION
) {
2369 list_del(&w
->entry
);
2371 binder_stats_deleted(BINDER_STAT_DEATH
);
2373 list_move(&w
->entry
, &proc
->delivered_death
);
2374 if (cmd
== BR_DEAD_BINDER
)
2375 goto done
; /* DEAD_BINDER notifications can cause transactions */
2382 BUG_ON(t
->buffer
== NULL
);
2383 if (t
->buffer
->target_node
) {
2384 struct binder_node
*target_node
= t
->buffer
->target_node
;
2386 tr
.target
.ptr
= target_node
->ptr
;
2387 tr
.cookie
= target_node
->cookie
;
2388 t
->saved_priority
= task_nice(current
);
2389 if (t
->priority
< target_node
->min_priority
&&
2390 !(t
->flags
& TF_ONE_WAY
))
2391 binder_set_nice(t
->priority
);
2392 else if (!(t
->flags
& TF_ONE_WAY
) ||
2393 t
->saved_priority
> target_node
->min_priority
)
2394 binder_set_nice(target_node
->min_priority
);
2395 cmd
= BR_TRANSACTION
;
2402 tr
.flags
= t
->flags
;
2403 tr
.sender_euid
= from_kuid(current_user_ns(), t
->sender_euid
);
2406 struct task_struct
*sender
= t
->from
->proc
->tsk
;
2408 tr
.sender_pid
= task_tgid_nr_ns(sender
,
2409 task_active_pid_ns(current
));
2414 tr
.data_size
= t
->buffer
->data_size
;
2415 tr
.offsets_size
= t
->buffer
->offsets_size
;
2416 tr
.data
.ptr
.buffer
= (binder_uintptr_t
)(
2417 (uintptr_t)t
->buffer
->data
+
2418 proc
->user_buffer_offset
);
2419 tr
.data
.ptr
.offsets
= tr
.data
.ptr
.buffer
+
2420 ALIGN(t
->buffer
->data_size
,
2423 if (put_user(cmd
, (uint32_t __user
*)ptr
))
2425 ptr
+= sizeof(uint32_t);
2426 if (copy_to_user(ptr
, &tr
, sizeof(tr
)))
2430 trace_binder_transaction_received(t
);
2431 binder_stat_br(proc
, thread
, cmd
);
2432 binder_debug(BINDER_DEBUG_TRANSACTION
,
2433 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
2434 proc
->pid
, thread
->pid
,
2435 (cmd
== BR_TRANSACTION
) ? "BR_TRANSACTION" :
2437 t
->debug_id
, t
->from
? t
->from
->proc
->pid
: 0,
2438 t
->from
? t
->from
->pid
: 0, cmd
,
2439 t
->buffer
->data_size
, t
->buffer
->offsets_size
,
2440 (u64
)tr
.data
.ptr
.buffer
, (u64
)tr
.data
.ptr
.offsets
);
2442 list_del(&t
->work
.entry
);
2443 t
->buffer
->allow_user_free
= 1;
2444 if (cmd
== BR_TRANSACTION
&& !(t
->flags
& TF_ONE_WAY
)) {
2445 t
->to_parent
= thread
->transaction_stack
;
2446 t
->to_thread
= thread
;
2447 thread
->transaction_stack
= t
;
2449 t
->buffer
->transaction
= NULL
;
2451 binder_stats_deleted(BINDER_STAT_TRANSACTION
);
2458 *consumed
= ptr
- buffer
;
2459 if (proc
->requested_threads
+ proc
->ready_threads
== 0 &&
2460 proc
->requested_threads_started
< proc
->max_threads
&&
2461 (thread
->looper
& (BINDER_LOOPER_STATE_REGISTERED
|
2462 BINDER_LOOPER_STATE_ENTERED
)) /* the user-space code fails to */
2463 /*spawn a new thread if we leave this out */) {
2464 proc
->requested_threads
++;
2465 binder_debug(BINDER_DEBUG_THREADS
,
2466 "%d:%d BR_SPAWN_LOOPER\n",
2467 proc
->pid
, thread
->pid
);
2468 if (put_user(BR_SPAWN_LOOPER
, (uint32_t __user
*)buffer
))
2470 binder_stat_br(proc
, thread
, BR_SPAWN_LOOPER
);
2475 static void binder_release_work(struct list_head
*list
)
2477 struct binder_work
*w
;
2479 while (!list_empty(list
)) {
2480 w
= list_first_entry(list
, struct binder_work
, entry
);
2481 list_del_init(&w
->entry
);
2483 case BINDER_WORK_TRANSACTION
: {
2484 struct binder_transaction
*t
;
2486 t
= container_of(w
, struct binder_transaction
, work
);
2487 if (t
->buffer
->target_node
&&
2488 !(t
->flags
& TF_ONE_WAY
)) {
2489 binder_send_failed_reply(t
, BR_DEAD_REPLY
);
2491 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
2492 "undelivered transaction %d\n",
2494 t
->buffer
->transaction
= NULL
;
2496 binder_stats_deleted(BINDER_STAT_TRANSACTION
);
2499 case BINDER_WORK_TRANSACTION_COMPLETE
: {
2500 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
2501 "undelivered TRANSACTION_COMPLETE\n");
2503 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE
);
2505 case BINDER_WORK_DEAD_BINDER_AND_CLEAR
:
2506 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION
: {
2507 struct binder_ref_death
*death
;
2509 death
= container_of(w
, struct binder_ref_death
, work
);
2510 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
2511 "undelivered death notification, %016llx\n",
2512 (u64
)death
->cookie
);
2514 binder_stats_deleted(BINDER_STAT_DEATH
);
2517 pr_err("unexpected work type, %d, not freed\n",
2525 static struct binder_thread
*binder_get_thread(struct binder_proc
*proc
)
2527 struct binder_thread
*thread
= NULL
;
2528 struct rb_node
*parent
= NULL
;
2529 struct rb_node
**p
= &proc
->threads
.rb_node
;
2533 thread
= rb_entry(parent
, struct binder_thread
, rb_node
);
2535 if (current
->pid
< thread
->pid
)
2537 else if (current
->pid
> thread
->pid
)
2538 p
= &(*p
)->rb_right
;
2543 thread
= kzalloc(sizeof(*thread
), GFP_KERNEL
);
2546 binder_stats_created(BINDER_STAT_THREAD
);
2547 thread
->proc
= proc
;
2548 thread
->pid
= current
->pid
;
2549 init_waitqueue_head(&thread
->wait
);
2550 INIT_LIST_HEAD(&thread
->todo
);
2551 rb_link_node(&thread
->rb_node
, parent
, p
);
2552 rb_insert_color(&thread
->rb_node
, &proc
->threads
);
2553 thread
->looper
|= BINDER_LOOPER_STATE_NEED_RETURN
;
2554 thread
->return_error
= BR_OK
;
2555 thread
->return_error2
= BR_OK
;
2560 static int binder_free_thread(struct binder_proc
*proc
,
2561 struct binder_thread
*thread
)
2563 struct binder_transaction
*t
;
2564 struct binder_transaction
*send_reply
= NULL
;
2565 int active_transactions
= 0;
2567 rb_erase(&thread
->rb_node
, &proc
->threads
);
2568 t
= thread
->transaction_stack
;
2569 if (t
&& t
->to_thread
== thread
)
2572 active_transactions
++;
2573 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
2574 "release %d:%d transaction %d %s, still active\n",
2575 proc
->pid
, thread
->pid
,
2577 (t
->to_thread
== thread
) ? "in" : "out");
2579 if (t
->to_thread
== thread
) {
2581 t
->to_thread
= NULL
;
2583 t
->buffer
->transaction
= NULL
;
2587 } else if (t
->from
== thread
) {
2594 binder_send_failed_reply(send_reply
, BR_DEAD_REPLY
);
2595 binder_release_work(&thread
->todo
);
2597 binder_stats_deleted(BINDER_STAT_THREAD
);
2598 return active_transactions
;
2601 static unsigned int binder_poll(struct file
*filp
,
2602 struct poll_table_struct
*wait
)
2604 struct binder_proc
*proc
= filp
->private_data
;
2605 struct binder_thread
*thread
= NULL
;
2606 int wait_for_proc_work
;
2608 binder_lock(__func__
);
2610 thread
= binder_get_thread(proc
);
2612 wait_for_proc_work
= thread
->transaction_stack
== NULL
&&
2613 list_empty(&thread
->todo
) && thread
->return_error
== BR_OK
;
2615 binder_unlock(__func__
);
2617 if (wait_for_proc_work
) {
2618 if (binder_has_proc_work(proc
, thread
))
2620 poll_wait(filp
, &proc
->wait
, wait
);
2621 if (binder_has_proc_work(proc
, thread
))
2624 if (binder_has_thread_work(thread
))
2626 poll_wait(filp
, &thread
->wait
, wait
);
2627 if (binder_has_thread_work(thread
))
2633 static int binder_ioctl_write_read(struct file
*filp
,
2634 unsigned int cmd
, unsigned long arg
,
2635 struct binder_thread
*thread
)
2638 struct binder_proc
*proc
= filp
->private_data
;
2639 unsigned int size
= _IOC_SIZE(cmd
);
2640 void __user
*ubuf
= (void __user
*)arg
;
2641 struct binder_write_read bwr
;
2643 if (size
!= sizeof(struct binder_write_read
)) {
2647 if (copy_from_user(&bwr
, ubuf
, sizeof(bwr
))) {
2651 binder_debug(BINDER_DEBUG_READ_WRITE
,
2652 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
2653 proc
->pid
, thread
->pid
,
2654 (u64
)bwr
.write_size
, (u64
)bwr
.write_buffer
,
2655 (u64
)bwr
.read_size
, (u64
)bwr
.read_buffer
);
2657 if (bwr
.write_size
> 0) {
2658 ret
= binder_thread_write(proc
, thread
,
2661 &bwr
.write_consumed
);
2662 trace_binder_write_done(ret
);
2664 bwr
.read_consumed
= 0;
2665 if (copy_to_user(ubuf
, &bwr
, sizeof(bwr
)))
2670 if (bwr
.read_size
> 0) {
2671 ret
= binder_thread_read(proc
, thread
, bwr
.read_buffer
,
2674 filp
->f_flags
& O_NONBLOCK
);
2675 trace_binder_read_done(ret
);
2676 if (!list_empty(&proc
->todo
))
2677 wake_up_interruptible(&proc
->wait
);
2679 if (copy_to_user(ubuf
, &bwr
, sizeof(bwr
)))
2684 binder_debug(BINDER_DEBUG_READ_WRITE
,
2685 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
2686 proc
->pid
, thread
->pid
,
2687 (u64
)bwr
.write_consumed
, (u64
)bwr
.write_size
,
2688 (u64
)bwr
.read_consumed
, (u64
)bwr
.read_size
);
2689 if (copy_to_user(ubuf
, &bwr
, sizeof(bwr
))) {
2697 static int binder_ioctl_set_ctx_mgr(struct file
*filp
)
2700 struct binder_proc
*proc
= filp
->private_data
;
2701 kuid_t curr_euid
= current_euid();
2703 if (binder_context_mgr_node
!= NULL
) {
2704 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
2708 ret
= security_binder_set_context_mgr(proc
->tsk
);
2711 if (uid_valid(binder_context_mgr_uid
)) {
2712 if (!uid_eq(binder_context_mgr_uid
, curr_euid
)) {
2713 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
2714 from_kuid(&init_user_ns
, curr_euid
),
2715 from_kuid(&init_user_ns
,
2716 binder_context_mgr_uid
));
2721 binder_context_mgr_uid
= curr_euid
;
2723 binder_context_mgr_node
= binder_new_node(proc
, 0, 0);
2724 if (binder_context_mgr_node
== NULL
) {
2728 binder_context_mgr_node
->local_weak_refs
++;
2729 binder_context_mgr_node
->local_strong_refs
++;
2730 binder_context_mgr_node
->has_strong_ref
= 1;
2731 binder_context_mgr_node
->has_weak_ref
= 1;
2736 static long binder_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
2739 struct binder_proc
*proc
= filp
->private_data
;
2740 struct binder_thread
*thread
;
2741 unsigned int size
= _IOC_SIZE(cmd
);
2742 void __user
*ubuf
= (void __user
*)arg
;
2744 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
2745 proc->pid, current->pid, cmd, arg);*/
2747 if (unlikely(current
->mm
!= proc
->vma_vm_mm
)) {
2748 pr_err("current mm mismatch proc mm\n");
2751 trace_binder_ioctl(cmd
, arg
);
2753 ret
= wait_event_interruptible(binder_user_error_wait
, binder_stop_on_user_error
< 2);
2757 binder_lock(__func__
);
2758 thread
= binder_get_thread(proc
);
2759 if (thread
== NULL
) {
2765 case BINDER_WRITE_READ
:
2766 ret
= binder_ioctl_write_read(filp
, cmd
, arg
, thread
);
2770 case BINDER_SET_MAX_THREADS
:
2771 if (copy_from_user(&proc
->max_threads
, ubuf
, sizeof(proc
->max_threads
))) {
2776 case BINDER_SET_CONTEXT_MGR
:
2777 ret
= binder_ioctl_set_ctx_mgr(filp
);
2781 case BINDER_THREAD_EXIT
:
2782 binder_debug(BINDER_DEBUG_THREADS
, "%d:%d exit\n",
2783 proc
->pid
, thread
->pid
);
2784 binder_free_thread(proc
, thread
);
2787 case BINDER_VERSION
: {
2788 struct binder_version __user
*ver
= ubuf
;
2790 if (size
!= sizeof(struct binder_version
)) {
2794 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION
,
2795 &ver
->protocol_version
)) {
2808 thread
->looper
&= ~BINDER_LOOPER_STATE_NEED_RETURN
;
2809 binder_unlock(__func__
);
2810 wait_event_interruptible(binder_user_error_wait
, binder_stop_on_user_error
< 2);
2811 if (ret
&& ret
!= -ERESTARTSYS
)
2812 pr_info("%d:%d ioctl %x %lx returned %d\n", proc
->pid
, current
->pid
, cmd
, arg
, ret
);
2814 trace_binder_ioctl_done(ret
);
2818 static void binder_vma_open(struct vm_area_struct
*vma
)
2820 struct binder_proc
*proc
= vma
->vm_private_data
;
2822 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
2823 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
2824 proc
->pid
, vma
->vm_start
, vma
->vm_end
,
2825 (vma
->vm_end
- vma
->vm_start
) / SZ_1K
, vma
->vm_flags
,
2826 (unsigned long)pgprot_val(vma
->vm_page_prot
));
2829 static void binder_vma_close(struct vm_area_struct
*vma
)
2831 struct binder_proc
*proc
= vma
->vm_private_data
;
2833 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
2834 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
2835 proc
->pid
, vma
->vm_start
, vma
->vm_end
,
2836 (vma
->vm_end
- vma
->vm_start
) / SZ_1K
, vma
->vm_flags
,
2837 (unsigned long)pgprot_val(vma
->vm_page_prot
));
2839 proc
->vma_vm_mm
= NULL
;
2840 binder_defer_work(proc
, BINDER_DEFERRED_PUT_FILES
);
2843 static int binder_vm_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
2845 return VM_FAULT_SIGBUS
;
2848 static const struct vm_operations_struct binder_vm_ops
= {
2849 .open
= binder_vma_open
,
2850 .close
= binder_vma_close
,
2851 .fault
= binder_vm_fault
,
2854 static int binder_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
2857 struct vm_struct
*area
;
2858 struct binder_proc
*proc
= filp
->private_data
;
2859 const char *failure_string
;
2860 struct binder_buffer
*buffer
;
2862 if (proc
->tsk
!= current
)
2865 if ((vma
->vm_end
- vma
->vm_start
) > SZ_4M
)
2866 vma
->vm_end
= vma
->vm_start
+ SZ_4M
;
2868 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
2869 "binder_mmap: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
2870 proc
->pid
, vma
->vm_start
, vma
->vm_end
,
2871 (vma
->vm_end
- vma
->vm_start
) / SZ_1K
, vma
->vm_flags
,
2872 (unsigned long)pgprot_val(vma
->vm_page_prot
));
2874 if (vma
->vm_flags
& FORBIDDEN_MMAP_FLAGS
) {
2876 failure_string
= "bad vm_flags";
2879 vma
->vm_flags
= (vma
->vm_flags
| VM_DONTCOPY
) & ~VM_MAYWRITE
;
2881 mutex_lock(&binder_mmap_lock
);
2884 failure_string
= "already mapped";
2885 goto err_already_mapped
;
2888 area
= get_vm_area(vma
->vm_end
- vma
->vm_start
, VM_IOREMAP
);
2891 failure_string
= "get_vm_area";
2892 goto err_get_vm_area_failed
;
2894 proc
->buffer
= area
->addr
;
2895 proc
->user_buffer_offset
= vma
->vm_start
- (uintptr_t)proc
->buffer
;
2896 mutex_unlock(&binder_mmap_lock
);
2898 #ifdef CONFIG_CPU_CACHE_VIPT
2899 if (cache_is_vipt_aliasing()) {
2900 while (CACHE_COLOUR((vma
->vm_start
^ (uint32_t)proc
->buffer
))) {
2901 pr_info("binder_mmap: %d %lx-%lx maps %p bad alignment\n", proc
->pid
, vma
->vm_start
, vma
->vm_end
, proc
->buffer
);
2902 vma
->vm_start
+= PAGE_SIZE
;
2906 proc
->pages
= kzalloc(sizeof(proc
->pages
[0]) * ((vma
->vm_end
- vma
->vm_start
) / PAGE_SIZE
), GFP_KERNEL
);
2907 if (proc
->pages
== NULL
) {
2909 failure_string
= "alloc page array";
2910 goto err_alloc_pages_failed
;
2912 proc
->buffer_size
= vma
->vm_end
- vma
->vm_start
;
2914 vma
->vm_ops
= &binder_vm_ops
;
2915 vma
->vm_private_data
= proc
;
2917 if (binder_update_page_range(proc
, 1, proc
->buffer
, proc
->buffer
+ PAGE_SIZE
, vma
)) {
2919 failure_string
= "alloc small buf";
2920 goto err_alloc_small_buf_failed
;
2922 buffer
= proc
->buffer
;
2923 INIT_LIST_HEAD(&proc
->buffers
);
2924 list_add(&buffer
->entry
, &proc
->buffers
);
2926 binder_insert_free_buffer(proc
, buffer
);
2927 proc
->free_async_space
= proc
->buffer_size
/ 2;
2929 proc
->files
= get_files_struct(current
);
2931 proc
->vma_vm_mm
= vma
->vm_mm
;
2933 /*pr_info("binder_mmap: %d %lx-%lx maps %p\n",
2934 proc->pid, vma->vm_start, vma->vm_end, proc->buffer);*/
2937 err_alloc_small_buf_failed
:
2940 err_alloc_pages_failed
:
2941 mutex_lock(&binder_mmap_lock
);
2942 vfree(proc
->buffer
);
2943 proc
->buffer
= NULL
;
2944 err_get_vm_area_failed
:
2946 mutex_unlock(&binder_mmap_lock
);
2948 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
2949 proc
->pid
, vma
->vm_start
, vma
->vm_end
, failure_string
, ret
);
2953 static int binder_open(struct inode
*nodp
, struct file
*filp
)
2955 struct binder_proc
*proc
;
2957 binder_debug(BINDER_DEBUG_OPEN_CLOSE
, "binder_open: %d:%d\n",
2958 current
->group_leader
->pid
, current
->pid
);
2960 proc
= kzalloc(sizeof(*proc
), GFP_KERNEL
);
2963 get_task_struct(current
);
2964 proc
->tsk
= current
;
2965 proc
->vma_vm_mm
= current
->mm
;
2966 INIT_LIST_HEAD(&proc
->todo
);
2967 init_waitqueue_head(&proc
->wait
);
2968 proc
->default_priority
= task_nice(current
);
2970 binder_lock(__func__
);
2972 binder_stats_created(BINDER_STAT_PROC
);
2973 hlist_add_head(&proc
->proc_node
, &binder_procs
);
2974 proc
->pid
= current
->group_leader
->pid
;
2975 INIT_LIST_HEAD(&proc
->delivered_death
);
2976 filp
->private_data
= proc
;
2978 binder_unlock(__func__
);
2980 if (binder_debugfs_dir_entry_proc
) {
2983 snprintf(strbuf
, sizeof(strbuf
), "%u", proc
->pid
);
2984 proc
->debugfs_entry
= debugfs_create_file(strbuf
, S_IRUGO
,
2985 binder_debugfs_dir_entry_proc
, proc
, &binder_proc_fops
);
2991 static int binder_flush(struct file
*filp
, fl_owner_t id
)
2993 struct binder_proc
*proc
= filp
->private_data
;
2995 binder_defer_work(proc
, BINDER_DEFERRED_FLUSH
);
3000 static void binder_deferred_flush(struct binder_proc
*proc
)
3005 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
)) {
3006 struct binder_thread
*thread
= rb_entry(n
, struct binder_thread
, rb_node
);
3008 thread
->looper
|= BINDER_LOOPER_STATE_NEED_RETURN
;
3009 if (thread
->looper
& BINDER_LOOPER_STATE_WAITING
) {
3010 wake_up_interruptible(&thread
->wait
);
3014 wake_up_interruptible_all(&proc
->wait
);
3016 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
3017 "binder_flush: %d woke %d threads\n", proc
->pid
,
3021 static int binder_release(struct inode
*nodp
, struct file
*filp
)
3023 struct binder_proc
*proc
= filp
->private_data
;
3025 debugfs_remove(proc
->debugfs_entry
);
3026 binder_defer_work(proc
, BINDER_DEFERRED_RELEASE
);
3031 static int binder_node_release(struct binder_node
*node
, int refs
)
3033 struct binder_ref
*ref
;
3036 list_del_init(&node
->work
.entry
);
3037 binder_release_work(&node
->async_todo
);
3039 if (hlist_empty(&node
->refs
)) {
3041 binder_stats_deleted(BINDER_STAT_NODE
);
3047 node
->local_strong_refs
= 0;
3048 node
->local_weak_refs
= 0;
3049 hlist_add_head(&node
->dead_node
, &binder_dead_nodes
);
3051 hlist_for_each_entry(ref
, &node
->refs
, node_entry
) {
3059 if (list_empty(&ref
->death
->work
.entry
)) {
3060 ref
->death
->work
.type
= BINDER_WORK_DEAD_BINDER
;
3061 list_add_tail(&ref
->death
->work
.entry
,
3063 wake_up_interruptible(&ref
->proc
->wait
);
3068 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
3069 "node %d now dead, refs %d, death %d\n",
3070 node
->debug_id
, refs
, death
);
3075 static void binder_deferred_release(struct binder_proc
*proc
)
3077 struct binder_transaction
*t
;
3079 int threads
, nodes
, incoming_refs
, outgoing_refs
, buffers
,
3080 active_transactions
, page_count
;
3083 BUG_ON(proc
->files
);
3085 hlist_del(&proc
->proc_node
);
3087 if (binder_context_mgr_node
&& binder_context_mgr_node
->proc
== proc
) {
3088 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
3089 "%s: %d context_mgr_node gone\n",
3090 __func__
, proc
->pid
);
3091 binder_context_mgr_node
= NULL
;
3095 active_transactions
= 0;
3096 while ((n
= rb_first(&proc
->threads
))) {
3097 struct binder_thread
*thread
;
3099 thread
= rb_entry(n
, struct binder_thread
, rb_node
);
3101 active_transactions
+= binder_free_thread(proc
, thread
);
3106 while ((n
= rb_first(&proc
->nodes
))) {
3107 struct binder_node
*node
;
3109 node
= rb_entry(n
, struct binder_node
, rb_node
);
3111 rb_erase(&node
->rb_node
, &proc
->nodes
);
3112 incoming_refs
= binder_node_release(node
, incoming_refs
);
3116 while ((n
= rb_first(&proc
->refs_by_desc
))) {
3117 struct binder_ref
*ref
;
3119 ref
= rb_entry(n
, struct binder_ref
, rb_node_desc
);
3121 binder_delete_ref(ref
);
3124 binder_release_work(&proc
->todo
);
3125 binder_release_work(&proc
->delivered_death
);
3128 while ((n
= rb_first(&proc
->allocated_buffers
))) {
3129 struct binder_buffer
*buffer
;
3131 buffer
= rb_entry(n
, struct binder_buffer
, rb_node
);
3133 t
= buffer
->transaction
;
3136 buffer
->transaction
= NULL
;
3137 pr_err("release proc %d, transaction %d, not freed\n",
3138 proc
->pid
, t
->debug_id
);
3142 binder_free_buf(proc
, buffer
);
3146 binder_stats_deleted(BINDER_STAT_PROC
);
3152 for (i
= 0; i
< proc
->buffer_size
/ PAGE_SIZE
; i
++) {
3155 if (!proc
->pages
[i
])
3158 page_addr
= proc
->buffer
+ i
* PAGE_SIZE
;
3159 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
3160 "%s: %d: page %d at %p not freed\n",
3161 __func__
, proc
->pid
, i
, page_addr
);
3162 unmap_kernel_range((unsigned long)page_addr
, PAGE_SIZE
);
3163 __free_page(proc
->pages
[i
]);
3167 vfree(proc
->buffer
);
3170 put_task_struct(proc
->tsk
);
3172 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
3173 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d, buffers %d, pages %d\n",
3174 __func__
, proc
->pid
, threads
, nodes
, incoming_refs
,
3175 outgoing_refs
, active_transactions
, buffers
, page_count
);
3180 static void binder_deferred_func(struct work_struct
*work
)
3182 struct binder_proc
*proc
;
3183 struct files_struct
*files
;
3188 binder_lock(__func__
);
3189 mutex_lock(&binder_deferred_lock
);
3190 if (!hlist_empty(&binder_deferred_list
)) {
3191 proc
= hlist_entry(binder_deferred_list
.first
,
3192 struct binder_proc
, deferred_work_node
);
3193 hlist_del_init(&proc
->deferred_work_node
);
3194 defer
= proc
->deferred_work
;
3195 proc
->deferred_work
= 0;
3200 mutex_unlock(&binder_deferred_lock
);
3203 if (defer
& BINDER_DEFERRED_PUT_FILES
) {
3204 files
= proc
->files
;
3209 if (defer
& BINDER_DEFERRED_FLUSH
)
3210 binder_deferred_flush(proc
);
3212 if (defer
& BINDER_DEFERRED_RELEASE
)
3213 binder_deferred_release(proc
); /* frees proc */
3215 binder_unlock(__func__
);
3217 put_files_struct(files
);
3220 static DECLARE_WORK(binder_deferred_work
, binder_deferred_func
);
3223 binder_defer_work(struct binder_proc
*proc
, enum binder_deferred_state defer
)
3225 mutex_lock(&binder_deferred_lock
);
3226 proc
->deferred_work
|= defer
;
3227 if (hlist_unhashed(&proc
->deferred_work_node
)) {
3228 hlist_add_head(&proc
->deferred_work_node
,
3229 &binder_deferred_list
);
3230 queue_work(binder_deferred_workqueue
, &binder_deferred_work
);
3232 mutex_unlock(&binder_deferred_lock
);
3235 static void print_binder_transaction(struct seq_file
*m
, const char *prefix
,
3236 struct binder_transaction
*t
)
3239 "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %ld r%d",
3240 prefix
, t
->debug_id
, t
,
3241 t
->from
? t
->from
->proc
->pid
: 0,
3242 t
->from
? t
->from
->pid
: 0,
3243 t
->to_proc
? t
->to_proc
->pid
: 0,
3244 t
->to_thread
? t
->to_thread
->pid
: 0,
3245 t
->code
, t
->flags
, t
->priority
, t
->need_reply
);
3246 if (t
->buffer
== NULL
) {
3247 seq_puts(m
, " buffer free\n");
3250 if (t
->buffer
->target_node
)
3251 seq_printf(m
, " node %d",
3252 t
->buffer
->target_node
->debug_id
);
3253 seq_printf(m
, " size %zd:%zd data %p\n",
3254 t
->buffer
->data_size
, t
->buffer
->offsets_size
,
3258 static void print_binder_buffer(struct seq_file
*m
, const char *prefix
,
3259 struct binder_buffer
*buffer
)
3261 seq_printf(m
, "%s %d: %p size %zd:%zd %s\n",
3262 prefix
, buffer
->debug_id
, buffer
->data
,
3263 buffer
->data_size
, buffer
->offsets_size
,
3264 buffer
->transaction
? "active" : "delivered");
3267 static void print_binder_work(struct seq_file
*m
, const char *prefix
,
3268 const char *transaction_prefix
,
3269 struct binder_work
*w
)
3271 struct binder_node
*node
;
3272 struct binder_transaction
*t
;
3275 case BINDER_WORK_TRANSACTION
:
3276 t
= container_of(w
, struct binder_transaction
, work
);
3277 print_binder_transaction(m
, transaction_prefix
, t
);
3279 case BINDER_WORK_TRANSACTION_COMPLETE
:
3280 seq_printf(m
, "%stransaction complete\n", prefix
);
3282 case BINDER_WORK_NODE
:
3283 node
= container_of(w
, struct binder_node
, work
);
3284 seq_printf(m
, "%snode work %d: u%016llx c%016llx\n",
3285 prefix
, node
->debug_id
,
3286 (u64
)node
->ptr
, (u64
)node
->cookie
);
3288 case BINDER_WORK_DEAD_BINDER
:
3289 seq_printf(m
, "%shas dead binder\n", prefix
);
3291 case BINDER_WORK_DEAD_BINDER_AND_CLEAR
:
3292 seq_printf(m
, "%shas cleared dead binder\n", prefix
);
3294 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION
:
3295 seq_printf(m
, "%shas cleared death notification\n", prefix
);
3298 seq_printf(m
, "%sunknown work: type %d\n", prefix
, w
->type
);
3303 static void print_binder_thread(struct seq_file
*m
,
3304 struct binder_thread
*thread
,
3307 struct binder_transaction
*t
;
3308 struct binder_work
*w
;
3309 size_t start_pos
= m
->count
;
3312 seq_printf(m
, " thread %d: l %02x\n", thread
->pid
, thread
->looper
);
3313 header_pos
= m
->count
;
3314 t
= thread
->transaction_stack
;
3316 if (t
->from
== thread
) {
3317 print_binder_transaction(m
,
3318 " outgoing transaction", t
);
3320 } else if (t
->to_thread
== thread
) {
3321 print_binder_transaction(m
,
3322 " incoming transaction", t
);
3325 print_binder_transaction(m
, " bad transaction", t
);
3329 list_for_each_entry(w
, &thread
->todo
, entry
) {
3330 print_binder_work(m
, " ", " pending transaction", w
);
3332 if (!print_always
&& m
->count
== header_pos
)
3333 m
->count
= start_pos
;
3336 static void print_binder_node(struct seq_file
*m
, struct binder_node
*node
)
3338 struct binder_ref
*ref
;
3339 struct binder_work
*w
;
3343 hlist_for_each_entry(ref
, &node
->refs
, node_entry
)
3346 seq_printf(m
, " node %d: u%016llx c%016llx hs %d hw %d ls %d lw %d is %d iw %d",
3347 node
->debug_id
, (u64
)node
->ptr
, (u64
)node
->cookie
,
3348 node
->has_strong_ref
, node
->has_weak_ref
,
3349 node
->local_strong_refs
, node
->local_weak_refs
,
3350 node
->internal_strong_refs
, count
);
3352 seq_puts(m
, " proc");
3353 hlist_for_each_entry(ref
, &node
->refs
, node_entry
)
3354 seq_printf(m
, " %d", ref
->proc
->pid
);
3357 list_for_each_entry(w
, &node
->async_todo
, entry
)
3358 print_binder_work(m
, " ",
3359 " pending async transaction", w
);
3362 static void print_binder_ref(struct seq_file
*m
, struct binder_ref
*ref
)
3364 seq_printf(m
, " ref %d: desc %d %snode %d s %d w %d d %p\n",
3365 ref
->debug_id
, ref
->desc
, ref
->node
->proc
? "" : "dead ",
3366 ref
->node
->debug_id
, ref
->strong
, ref
->weak
, ref
->death
);
3369 static void print_binder_proc(struct seq_file
*m
,
3370 struct binder_proc
*proc
, int print_all
)
3372 struct binder_work
*w
;
3374 size_t start_pos
= m
->count
;
3377 seq_printf(m
, "proc %d\n", proc
->pid
);
3378 header_pos
= m
->count
;
3380 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
))
3381 print_binder_thread(m
, rb_entry(n
, struct binder_thread
,
3382 rb_node
), print_all
);
3383 for (n
= rb_first(&proc
->nodes
); n
!= NULL
; n
= rb_next(n
)) {
3384 struct binder_node
*node
= rb_entry(n
, struct binder_node
,
3386 if (print_all
|| node
->has_async_transaction
)
3387 print_binder_node(m
, node
);
3390 for (n
= rb_first(&proc
->refs_by_desc
);
3393 print_binder_ref(m
, rb_entry(n
, struct binder_ref
,
3396 for (n
= rb_first(&proc
->allocated_buffers
); n
!= NULL
; n
= rb_next(n
))
3397 print_binder_buffer(m
, " buffer",
3398 rb_entry(n
, struct binder_buffer
, rb_node
));
3399 list_for_each_entry(w
, &proc
->todo
, entry
)
3400 print_binder_work(m
, " ", " pending transaction", w
);
3401 list_for_each_entry(w
, &proc
->delivered_death
, entry
) {
3402 seq_puts(m
, " has delivered dead binder\n");
3405 if (!print_all
&& m
->count
== header_pos
)
3406 m
->count
= start_pos
;
3409 static const char * const binder_return_strings
[] = {
3414 "BR_ACQUIRE_RESULT",
3416 "BR_TRANSACTION_COMPLETE",
3421 "BR_ATTEMPT_ACQUIRE",
3426 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
3430 static const char * const binder_command_strings
[] = {
3433 "BC_ACQUIRE_RESULT",
3441 "BC_ATTEMPT_ACQUIRE",
3442 "BC_REGISTER_LOOPER",
3445 "BC_REQUEST_DEATH_NOTIFICATION",
3446 "BC_CLEAR_DEATH_NOTIFICATION",
3447 "BC_DEAD_BINDER_DONE"
3450 static const char * const binder_objstat_strings
[] = {
3457 "transaction_complete"
3460 static void print_binder_stats(struct seq_file
*m
, const char *prefix
,
3461 struct binder_stats
*stats
)
3465 BUILD_BUG_ON(ARRAY_SIZE(stats
->bc
) !=
3466 ARRAY_SIZE(binder_command_strings
));
3467 for (i
= 0; i
< ARRAY_SIZE(stats
->bc
); i
++) {
3469 seq_printf(m
, "%s%s: %d\n", prefix
,
3470 binder_command_strings
[i
], stats
->bc
[i
]);
3473 BUILD_BUG_ON(ARRAY_SIZE(stats
->br
) !=
3474 ARRAY_SIZE(binder_return_strings
));
3475 for (i
= 0; i
< ARRAY_SIZE(stats
->br
); i
++) {
3477 seq_printf(m
, "%s%s: %d\n", prefix
,
3478 binder_return_strings
[i
], stats
->br
[i
]);
3481 BUILD_BUG_ON(ARRAY_SIZE(stats
->obj_created
) !=
3482 ARRAY_SIZE(binder_objstat_strings
));
3483 BUILD_BUG_ON(ARRAY_SIZE(stats
->obj_created
) !=
3484 ARRAY_SIZE(stats
->obj_deleted
));
3485 for (i
= 0; i
< ARRAY_SIZE(stats
->obj_created
); i
++) {
3486 if (stats
->obj_created
[i
] || stats
->obj_deleted
[i
])
3487 seq_printf(m
, "%s%s: active %d total %d\n", prefix
,
3488 binder_objstat_strings
[i
],
3489 stats
->obj_created
[i
] - stats
->obj_deleted
[i
],
3490 stats
->obj_created
[i
]);
3494 static void print_binder_proc_stats(struct seq_file
*m
,
3495 struct binder_proc
*proc
)
3497 struct binder_work
*w
;
3499 int count
, strong
, weak
;
3501 seq_printf(m
, "proc %d\n", proc
->pid
);
3503 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
))
3505 seq_printf(m
, " threads: %d\n", count
);
3506 seq_printf(m
, " requested threads: %d+%d/%d\n"
3507 " ready threads %d\n"
3508 " free async space %zd\n", proc
->requested_threads
,
3509 proc
->requested_threads_started
, proc
->max_threads
,
3510 proc
->ready_threads
, proc
->free_async_space
);
3512 for (n
= rb_first(&proc
->nodes
); n
!= NULL
; n
= rb_next(n
))
3514 seq_printf(m
, " nodes: %d\n", count
);
3518 for (n
= rb_first(&proc
->refs_by_desc
); n
!= NULL
; n
= rb_next(n
)) {
3519 struct binder_ref
*ref
= rb_entry(n
, struct binder_ref
,
3522 strong
+= ref
->strong
;
3525 seq_printf(m
, " refs: %d s %d w %d\n", count
, strong
, weak
);
3528 for (n
= rb_first(&proc
->allocated_buffers
); n
!= NULL
; n
= rb_next(n
))
3530 seq_printf(m
, " buffers: %d\n", count
);
3533 list_for_each_entry(w
, &proc
->todo
, entry
) {
3535 case BINDER_WORK_TRANSACTION
:
3542 seq_printf(m
, " pending transactions: %d\n", count
);
3544 print_binder_stats(m
, " ", &proc
->stats
);
3548 static int binder_state_show(struct seq_file
*m
, void *unused
)
3550 struct binder_proc
*proc
;
3551 struct binder_node
*node
;
3552 int do_lock
= !binder_debug_no_lock
;
3555 binder_lock(__func__
);
3557 seq_puts(m
, "binder state:\n");
3559 if (!hlist_empty(&binder_dead_nodes
))
3560 seq_puts(m
, "dead nodes:\n");
3561 hlist_for_each_entry(node
, &binder_dead_nodes
, dead_node
)
3562 print_binder_node(m
, node
);
3564 hlist_for_each_entry(proc
, &binder_procs
, proc_node
)
3565 print_binder_proc(m
, proc
, 1);
3567 binder_unlock(__func__
);
3571 static int binder_stats_show(struct seq_file
*m
, void *unused
)
3573 struct binder_proc
*proc
;
3574 int do_lock
= !binder_debug_no_lock
;
3577 binder_lock(__func__
);
3579 seq_puts(m
, "binder stats:\n");
3581 print_binder_stats(m
, "", &binder_stats
);
3583 hlist_for_each_entry(proc
, &binder_procs
, proc_node
)
3584 print_binder_proc_stats(m
, proc
);
3586 binder_unlock(__func__
);
3590 static int binder_transactions_show(struct seq_file
*m
, void *unused
)
3592 struct binder_proc
*proc
;
3593 int do_lock
= !binder_debug_no_lock
;
3596 binder_lock(__func__
);
3598 seq_puts(m
, "binder transactions:\n");
3599 hlist_for_each_entry(proc
, &binder_procs
, proc_node
)
3600 print_binder_proc(m
, proc
, 0);
3602 binder_unlock(__func__
);
3606 static int binder_proc_show(struct seq_file
*m
, void *unused
)
3608 struct binder_proc
*itr
;
3609 struct binder_proc
*proc
= m
->private;
3610 int do_lock
= !binder_debug_no_lock
;
3611 bool valid_proc
= false;
3614 binder_lock(__func__
);
3616 hlist_for_each_entry(itr
, &binder_procs
, proc_node
) {
3623 seq_puts(m
, "binder proc state:\n");
3624 print_binder_proc(m
, proc
, 1);
3627 binder_unlock(__func__
);
3631 static void print_binder_transaction_log_entry(struct seq_file
*m
,
3632 struct binder_transaction_log_entry
*e
)
3635 "%d: %s from %d:%d to %d:%d node %d handle %d size %d:%d\n",
3636 e
->debug_id
, (e
->call_type
== 2) ? "reply" :
3637 ((e
->call_type
== 1) ? "async" : "call "), e
->from_proc
,
3638 e
->from_thread
, e
->to_proc
, e
->to_thread
, e
->to_node
,
3639 e
->target_handle
, e
->data_size
, e
->offsets_size
);
3642 static int binder_transaction_log_show(struct seq_file
*m
, void *unused
)
3644 struct binder_transaction_log
*log
= m
->private;
3648 for (i
= log
->next
; i
< ARRAY_SIZE(log
->entry
); i
++)
3649 print_binder_transaction_log_entry(m
, &log
->entry
[i
]);
3651 for (i
= 0; i
< log
->next
; i
++)
3652 print_binder_transaction_log_entry(m
, &log
->entry
[i
]);
3656 static const struct file_operations binder_fops
= {
3657 .owner
= THIS_MODULE
,
3658 .poll
= binder_poll
,
3659 .unlocked_ioctl
= binder_ioctl
,
3660 .compat_ioctl
= binder_ioctl
,
3661 .mmap
= binder_mmap
,
3662 .open
= binder_open
,
3663 .flush
= binder_flush
,
3664 .release
= binder_release
,
3667 static struct miscdevice binder_miscdev
= {
3668 .minor
= MISC_DYNAMIC_MINOR
,
3670 .fops
= &binder_fops
3673 BINDER_DEBUG_ENTRY(state
);
3674 BINDER_DEBUG_ENTRY(stats
);
3675 BINDER_DEBUG_ENTRY(transactions
);
3676 BINDER_DEBUG_ENTRY(transaction_log
);
3678 static int __init
binder_init(void)
3682 binder_deferred_workqueue
= create_singlethread_workqueue("binder");
3683 if (!binder_deferred_workqueue
)
3686 binder_debugfs_dir_entry_root
= debugfs_create_dir("binder", NULL
);
3687 if (binder_debugfs_dir_entry_root
)
3688 binder_debugfs_dir_entry_proc
= debugfs_create_dir("proc",
3689 binder_debugfs_dir_entry_root
);
3690 ret
= misc_register(&binder_miscdev
);
3691 if (binder_debugfs_dir_entry_root
) {
3692 debugfs_create_file("state",
3694 binder_debugfs_dir_entry_root
,
3696 &binder_state_fops
);
3697 debugfs_create_file("stats",
3699 binder_debugfs_dir_entry_root
,
3701 &binder_stats_fops
);
3702 debugfs_create_file("transactions",
3704 binder_debugfs_dir_entry_root
,
3706 &binder_transactions_fops
);
3707 debugfs_create_file("transaction_log",
3709 binder_debugfs_dir_entry_root
,
3710 &binder_transaction_log
,
3711 &binder_transaction_log_fops
);
3712 debugfs_create_file("failed_transaction_log",
3714 binder_debugfs_dir_entry_root
,
3715 &binder_transaction_log_failed
,
3716 &binder_transaction_log_fops
);
3721 device_initcall(binder_init
);
3723 #define CREATE_TRACE_POINTS
3724 #include "binder_trace.h"
3726 MODULE_LICENSE("GPL v2");