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 #include <asm/cacheflush.h>
19 #include <linux/fdtable.h>
20 #include <linux/file.h>
22 #include <linux/list.h>
23 #include <linux/miscdevice.h>
25 #include <linux/module.h>
26 #include <linux/mutex.h>
27 #include <linux/nsproxy.h>
28 #include <linux/poll.h>
29 #include <linux/debugfs.h>
30 #include <linux/rbtree.h>
31 #include <linux/sched.h>
32 #include <linux/seq_file.h>
33 #include <linux/uaccess.h>
34 #include <linux/vmalloc.h>
35 #include <linux/slab.h>
39 static DEFINE_MUTEX(binder_lock
);
40 static DEFINE_MUTEX(binder_deferred_lock
);
41 static DEFINE_MUTEX(binder_mmap_lock
);
43 static HLIST_HEAD(binder_procs
);
44 static HLIST_HEAD(binder_deferred_list
);
45 static HLIST_HEAD(binder_dead_nodes
);
47 static struct dentry
*binder_debugfs_dir_entry_root
;
48 static struct dentry
*binder_debugfs_dir_entry_proc
;
49 static struct binder_node
*binder_context_mgr_node
;
50 static kuid_t binder_context_mgr_uid
= INVALID_UID
;
51 static int binder_last_id
;
52 static struct workqueue_struct
*binder_deferred_workqueue
;
54 #define BINDER_DEBUG_ENTRY(name) \
55 static int binder_##name##_open(struct inode *inode, struct file *file) \
57 return single_open(file, binder_##name##_show, inode->i_private); \
60 static const struct file_operations binder_##name##_fops = { \
61 .owner = THIS_MODULE, \
62 .open = binder_##name##_open, \
64 .llseek = seq_lseek, \
65 .release = single_release, \
68 static int binder_proc_show(struct seq_file
*m
, void *unused
);
69 BINDER_DEBUG_ENTRY(proc
);
71 /* This is only defined in include/asm-arm/sizes.h */
77 #define SZ_4M 0x400000
80 #define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
82 #define BINDER_SMALL_BUF_SIZE (PAGE_SIZE * 64)
85 BINDER_DEBUG_USER_ERROR
= 1U << 0,
86 BINDER_DEBUG_FAILED_TRANSACTION
= 1U << 1,
87 BINDER_DEBUG_DEAD_TRANSACTION
= 1U << 2,
88 BINDER_DEBUG_OPEN_CLOSE
= 1U << 3,
89 BINDER_DEBUG_DEAD_BINDER
= 1U << 4,
90 BINDER_DEBUG_DEATH_NOTIFICATION
= 1U << 5,
91 BINDER_DEBUG_READ_WRITE
= 1U << 6,
92 BINDER_DEBUG_USER_REFS
= 1U << 7,
93 BINDER_DEBUG_THREADS
= 1U << 8,
94 BINDER_DEBUG_TRANSACTION
= 1U << 9,
95 BINDER_DEBUG_TRANSACTION_COMPLETE
= 1U << 10,
96 BINDER_DEBUG_FREE_BUFFER
= 1U << 11,
97 BINDER_DEBUG_INTERNAL_REFS
= 1U << 12,
98 BINDER_DEBUG_BUFFER_ALLOC
= 1U << 13,
99 BINDER_DEBUG_PRIORITY_CAP
= 1U << 14,
100 BINDER_DEBUG_BUFFER_ALLOC_ASYNC
= 1U << 15,
102 static uint32_t binder_debug_mask
= BINDER_DEBUG_USER_ERROR
|
103 BINDER_DEBUG_FAILED_TRANSACTION
| BINDER_DEBUG_DEAD_TRANSACTION
;
104 module_param_named(debug_mask
, binder_debug_mask
, uint
, S_IWUSR
| S_IRUGO
);
106 static bool binder_debug_no_lock
;
107 module_param_named(proc_no_lock
, binder_debug_no_lock
, bool, S_IWUSR
| S_IRUGO
);
109 static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait
);
110 static int binder_stop_on_user_error
;
112 static int binder_set_stop_on_user_error(const char *val
,
113 struct kernel_param
*kp
)
116 ret
= param_set_int(val
, kp
);
117 if (binder_stop_on_user_error
< 2)
118 wake_up(&binder_user_error_wait
);
121 module_param_call(stop_on_user_error
, binder_set_stop_on_user_error
,
122 param_get_int
, &binder_stop_on_user_error
, S_IWUSR
| S_IRUGO
);
124 #define binder_debug(mask, x...) \
126 if (binder_debug_mask & mask) \
130 #define binder_user_error(x...) \
132 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
134 if (binder_stop_on_user_error) \
135 binder_stop_on_user_error = 2; \
138 enum binder_stat_types
{
144 BINDER_STAT_TRANSACTION
,
145 BINDER_STAT_TRANSACTION_COMPLETE
,
149 struct binder_stats
{
150 int br
[_IOC_NR(BR_FAILED_REPLY
) + 1];
151 int bc
[_IOC_NR(BC_DEAD_BINDER_DONE
) + 1];
152 int obj_created
[BINDER_STAT_COUNT
];
153 int obj_deleted
[BINDER_STAT_COUNT
];
156 static struct binder_stats binder_stats
;
158 static inline void binder_stats_deleted(enum binder_stat_types type
)
160 binder_stats
.obj_deleted
[type
]++;
163 static inline void binder_stats_created(enum binder_stat_types type
)
165 binder_stats
.obj_created
[type
]++;
168 struct binder_transaction_log_entry
{
180 struct binder_transaction_log
{
183 struct binder_transaction_log_entry entry
[32];
185 static struct binder_transaction_log binder_transaction_log
;
186 static struct binder_transaction_log binder_transaction_log_failed
;
188 static struct binder_transaction_log_entry
*binder_transaction_log_add(
189 struct binder_transaction_log
*log
)
191 struct binder_transaction_log_entry
*e
;
192 e
= &log
->entry
[log
->next
];
193 memset(e
, 0, sizeof(*e
));
195 if (log
->next
== ARRAY_SIZE(log
->entry
)) {
203 struct list_head entry
;
205 BINDER_WORK_TRANSACTION
= 1,
206 BINDER_WORK_TRANSACTION_COMPLETE
,
208 BINDER_WORK_DEAD_BINDER
,
209 BINDER_WORK_DEAD_BINDER_AND_CLEAR
,
210 BINDER_WORK_CLEAR_DEATH_NOTIFICATION
,
216 struct binder_work work
;
218 struct rb_node rb_node
;
219 struct hlist_node dead_node
;
221 struct binder_proc
*proc
;
222 struct hlist_head refs
;
223 int internal_strong_refs
;
225 int local_strong_refs
;
228 unsigned has_strong_ref
:1;
229 unsigned pending_strong_ref
:1;
230 unsigned has_weak_ref
:1;
231 unsigned pending_weak_ref
:1;
232 unsigned has_async_transaction
:1;
233 unsigned accept_fds
:1;
234 unsigned min_priority
:8;
235 struct list_head async_todo
;
238 struct binder_ref_death
{
239 struct binder_work work
;
244 /* Lookups needed: */
245 /* node + proc => ref (transaction) */
246 /* desc + proc => ref (transaction, inc/dec ref) */
247 /* node => refs + procs (proc exit) */
249 struct rb_node rb_node_desc
;
250 struct rb_node rb_node_node
;
251 struct hlist_node node_entry
;
252 struct binder_proc
*proc
;
253 struct binder_node
*node
;
257 struct binder_ref_death
*death
;
260 struct binder_buffer
{
261 struct list_head entry
; /* free and allocated entries by address */
262 struct rb_node rb_node
; /* free entry by size or allocated entry */
265 unsigned allow_user_free
:1;
266 unsigned async_transaction
:1;
267 unsigned debug_id
:29;
269 struct binder_transaction
*transaction
;
271 struct binder_node
*target_node
;
277 enum binder_deferred_state
{
278 BINDER_DEFERRED_PUT_FILES
= 0x01,
279 BINDER_DEFERRED_FLUSH
= 0x02,
280 BINDER_DEFERRED_RELEASE
= 0x04,
284 struct hlist_node proc_node
;
285 struct rb_root threads
;
286 struct rb_root nodes
;
287 struct rb_root refs_by_desc
;
288 struct rb_root refs_by_node
;
290 struct vm_area_struct
*vma
;
291 struct mm_struct
*vma_vm_mm
;
292 struct task_struct
*tsk
;
293 struct files_struct
*files
;
294 struct hlist_node deferred_work_node
;
297 ptrdiff_t user_buffer_offset
;
299 struct list_head buffers
;
300 struct rb_root free_buffers
;
301 struct rb_root allocated_buffers
;
302 size_t free_async_space
;
306 uint32_t buffer_free
;
307 struct list_head todo
;
308 wait_queue_head_t wait
;
309 struct binder_stats stats
;
310 struct list_head delivered_death
;
312 int requested_threads
;
313 int requested_threads_started
;
315 long default_priority
;
316 struct dentry
*debugfs_entry
;
320 BINDER_LOOPER_STATE_REGISTERED
= 0x01,
321 BINDER_LOOPER_STATE_ENTERED
= 0x02,
322 BINDER_LOOPER_STATE_EXITED
= 0x04,
323 BINDER_LOOPER_STATE_INVALID
= 0x08,
324 BINDER_LOOPER_STATE_WAITING
= 0x10,
325 BINDER_LOOPER_STATE_NEED_RETURN
= 0x20
328 struct binder_thread
{
329 struct binder_proc
*proc
;
330 struct rb_node rb_node
;
333 struct binder_transaction
*transaction_stack
;
334 struct list_head todo
;
335 uint32_t return_error
; /* Write failed, return error code in read buf */
336 uint32_t return_error2
; /* Write failed, return error code in read */
337 /* buffer. Used when sending a reply to a dead process that */
338 /* we are also waiting on */
339 wait_queue_head_t wait
;
340 struct binder_stats stats
;
343 struct binder_transaction
{
345 struct binder_work work
;
346 struct binder_thread
*from
;
347 struct binder_transaction
*from_parent
;
348 struct binder_proc
*to_proc
;
349 struct binder_thread
*to_thread
;
350 struct binder_transaction
*to_parent
;
351 unsigned need_reply
:1;
352 /* unsigned is_dead:1; */ /* not used at the moment */
354 struct binder_buffer
*buffer
;
363 binder_defer_work(struct binder_proc
*proc
, enum binder_deferred_state defer
);
365 static int task_get_unused_fd_flags(struct binder_proc
*proc
, int flags
)
367 struct files_struct
*files
= proc
->files
;
368 unsigned long rlim_cur
;
374 if (!lock_task_sighand(proc
->tsk
, &irqs
))
377 rlim_cur
= task_rlimit(proc
->tsk
, RLIMIT_NOFILE
);
378 unlock_task_sighand(proc
->tsk
, &irqs
);
380 return __alloc_fd(files
, 0, rlim_cur
, flags
);
384 * copied from fd_install
386 static void task_fd_install(
387 struct binder_proc
*proc
, unsigned int fd
, struct file
*file
)
390 __fd_install(proc
->files
, fd
, file
);
394 * copied from sys_close
396 static long task_close_fd(struct binder_proc
*proc
, unsigned int fd
)
400 if (proc
->files
== NULL
)
403 retval
= __close_fd(proc
->files
, fd
);
404 /* can't restart close syscall because file table entry was cleared */
405 if (unlikely(retval
== -ERESTARTSYS
||
406 retval
== -ERESTARTNOINTR
||
407 retval
== -ERESTARTNOHAND
||
408 retval
== -ERESTART_RESTARTBLOCK
))
414 static void binder_set_nice(long nice
)
417 if (can_nice(current
, nice
)) {
418 set_user_nice(current
, nice
);
421 min_nice
= 20 - current
->signal
->rlim
[RLIMIT_NICE
].rlim_cur
;
422 binder_debug(BINDER_DEBUG_PRIORITY_CAP
,
423 "binder: %d: nice value %ld not allowed use "
424 "%ld instead\n", current
->pid
, nice
, min_nice
);
425 set_user_nice(current
, min_nice
);
428 binder_user_error("binder: %d RLIMIT_NICE not set\n", current
->pid
);
431 static size_t binder_buffer_size(struct binder_proc
*proc
,
432 struct binder_buffer
*buffer
)
434 if (list_is_last(&buffer
->entry
, &proc
->buffers
))
435 return proc
->buffer
+ proc
->buffer_size
- (void *)buffer
->data
;
437 return (size_t)list_entry(buffer
->entry
.next
,
438 struct binder_buffer
, entry
) - (size_t)buffer
->data
;
441 static void binder_insert_free_buffer(struct binder_proc
*proc
,
442 struct binder_buffer
*new_buffer
)
444 struct rb_node
**p
= &proc
->free_buffers
.rb_node
;
445 struct rb_node
*parent
= NULL
;
446 struct binder_buffer
*buffer
;
448 size_t new_buffer_size
;
450 BUG_ON(!new_buffer
->free
);
452 new_buffer_size
= binder_buffer_size(proc
, new_buffer
);
454 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
455 "binder: %d: add free buffer, size %zd, "
456 "at %p\n", proc
->pid
, new_buffer_size
, new_buffer
);
460 buffer
= rb_entry(parent
, struct binder_buffer
, rb_node
);
461 BUG_ON(!buffer
->free
);
463 buffer_size
= binder_buffer_size(proc
, buffer
);
465 if (new_buffer_size
< buffer_size
)
466 p
= &parent
->rb_left
;
468 p
= &parent
->rb_right
;
470 rb_link_node(&new_buffer
->rb_node
, parent
, p
);
471 rb_insert_color(&new_buffer
->rb_node
, &proc
->free_buffers
);
474 static void binder_insert_allocated_buffer(struct binder_proc
*proc
,
475 struct binder_buffer
*new_buffer
)
477 struct rb_node
**p
= &proc
->allocated_buffers
.rb_node
;
478 struct rb_node
*parent
= NULL
;
479 struct binder_buffer
*buffer
;
481 BUG_ON(new_buffer
->free
);
485 buffer
= rb_entry(parent
, struct binder_buffer
, rb_node
);
486 BUG_ON(buffer
->free
);
488 if (new_buffer
< buffer
)
489 p
= &parent
->rb_left
;
490 else if (new_buffer
> buffer
)
491 p
= &parent
->rb_right
;
495 rb_link_node(&new_buffer
->rb_node
, parent
, p
);
496 rb_insert_color(&new_buffer
->rb_node
, &proc
->allocated_buffers
);
499 static struct binder_buffer
*binder_buffer_lookup(struct binder_proc
*proc
,
500 void __user
*user_ptr
)
502 struct rb_node
*n
= proc
->allocated_buffers
.rb_node
;
503 struct binder_buffer
*buffer
;
504 struct binder_buffer
*kern_ptr
;
506 kern_ptr
= user_ptr
- proc
->user_buffer_offset
507 - offsetof(struct binder_buffer
, data
);
510 buffer
= rb_entry(n
, struct binder_buffer
, rb_node
);
511 BUG_ON(buffer
->free
);
513 if (kern_ptr
< buffer
)
515 else if (kern_ptr
> buffer
)
523 static int binder_update_page_range(struct binder_proc
*proc
, int allocate
,
524 void *start
, void *end
,
525 struct vm_area_struct
*vma
)
528 unsigned long user_page_addr
;
529 struct vm_struct tmp_area
;
531 struct mm_struct
*mm
;
533 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
534 "binder: %d: %s pages %p-%p\n", proc
->pid
,
535 allocate
? "allocate" : "free", start
, end
);
543 mm
= get_task_mm(proc
->tsk
);
546 down_write(&mm
->mmap_sem
);
548 if (vma
&& mm
!= proc
->vma_vm_mm
) {
549 pr_err("binder: %d: vma mm and task mm mismatch\n",
559 pr_err("binder: %d: binder_alloc_buf failed to "
560 "map pages in userspace, no vma\n", proc
->pid
);
564 for (page_addr
= start
; page_addr
< end
; page_addr
+= PAGE_SIZE
) {
566 struct page
**page_array_ptr
;
567 page
= &proc
->pages
[(page_addr
- proc
->buffer
) / PAGE_SIZE
];
570 *page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
572 pr_err("binder: %d: binder_alloc_buf failed "
573 "for page at %p\n", proc
->pid
, page_addr
);
574 goto err_alloc_page_failed
;
576 tmp_area
.addr
= page_addr
;
577 tmp_area
.size
= PAGE_SIZE
+ PAGE_SIZE
/* guard page? */;
578 page_array_ptr
= page
;
579 ret
= map_vm_area(&tmp_area
, PAGE_KERNEL
, &page_array_ptr
);
581 pr_err("binder: %d: binder_alloc_buf failed "
582 "to map page at %p in kernel\n",
583 proc
->pid
, page_addr
);
584 goto err_map_kernel_failed
;
587 (uintptr_t)page_addr
+ proc
->user_buffer_offset
;
588 ret
= vm_insert_page(vma
, user_page_addr
, page
[0]);
590 pr_err("binder: %d: binder_alloc_buf failed "
591 "to map page at %lx in userspace\n",
592 proc
->pid
, user_page_addr
);
593 goto err_vm_insert_page_failed
;
595 /* vm_insert_page does not seem to increment the refcount */
598 up_write(&mm
->mmap_sem
);
604 for (page_addr
= end
- PAGE_SIZE
; page_addr
>= start
;
605 page_addr
-= PAGE_SIZE
) {
606 page
= &proc
->pages
[(page_addr
- proc
->buffer
) / PAGE_SIZE
];
608 zap_page_range(vma
, (uintptr_t)page_addr
+
609 proc
->user_buffer_offset
, PAGE_SIZE
, NULL
);
610 err_vm_insert_page_failed
:
611 unmap_kernel_range((unsigned long)page_addr
, PAGE_SIZE
);
612 err_map_kernel_failed
:
615 err_alloc_page_failed
:
620 up_write(&mm
->mmap_sem
);
626 static struct binder_buffer
*binder_alloc_buf(struct binder_proc
*proc
,
628 size_t offsets_size
, int is_async
)
630 struct rb_node
*n
= proc
->free_buffers
.rb_node
;
631 struct binder_buffer
*buffer
;
633 struct rb_node
*best_fit
= NULL
;
638 if (proc
->vma
== NULL
) {
639 pr_err("binder: %d: binder_alloc_buf, no vma\n",
644 size
= ALIGN(data_size
, sizeof(void *)) +
645 ALIGN(offsets_size
, sizeof(void *));
647 if (size
< data_size
|| size
< offsets_size
) {
648 binder_user_error("binder: %d: got transaction with invalid "
649 "size %zd-%zd\n", proc
->pid
, data_size
, offsets_size
);
654 proc
->free_async_space
< size
+ sizeof(struct binder_buffer
)) {
655 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
656 "binder: %d: binder_alloc_buf size %zd"
657 "failed, no async space left\n", proc
->pid
, size
);
662 buffer
= rb_entry(n
, struct binder_buffer
, rb_node
);
663 BUG_ON(!buffer
->free
);
664 buffer_size
= binder_buffer_size(proc
, buffer
);
666 if (size
< buffer_size
) {
669 } else if (size
> buffer_size
)
676 if (best_fit
== NULL
) {
677 pr_err("binder: %d: binder_alloc_buf size %zd failed, "
678 "no address space\n", proc
->pid
, size
);
682 buffer
= rb_entry(best_fit
, struct binder_buffer
, rb_node
);
683 buffer_size
= binder_buffer_size(proc
, buffer
);
686 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
687 "binder: %d: binder_alloc_buf size %zd got buff"
688 "er %p size %zd\n", proc
->pid
, size
, buffer
, buffer_size
);
691 (void *)(((uintptr_t)buffer
->data
+ buffer_size
) & PAGE_MASK
);
693 if (size
+ sizeof(struct binder_buffer
) + 4 >= buffer_size
)
694 buffer_size
= size
; /* no room for other buffers */
696 buffer_size
= size
+ sizeof(struct binder_buffer
);
699 (void *)PAGE_ALIGN((uintptr_t)buffer
->data
+ buffer_size
);
700 if (end_page_addr
> has_page_addr
)
701 end_page_addr
= has_page_addr
;
702 if (binder_update_page_range(proc
, 1,
703 (void *)PAGE_ALIGN((uintptr_t)buffer
->data
), end_page_addr
, NULL
))
706 rb_erase(best_fit
, &proc
->free_buffers
);
708 binder_insert_allocated_buffer(proc
, buffer
);
709 if (buffer_size
!= size
) {
710 struct binder_buffer
*new_buffer
= (void *)buffer
->data
+ size
;
711 list_add(&new_buffer
->entry
, &buffer
->entry
);
712 new_buffer
->free
= 1;
713 binder_insert_free_buffer(proc
, new_buffer
);
715 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
716 "binder: %d: binder_alloc_buf size %zd got "
717 "%p\n", proc
->pid
, size
, buffer
);
718 buffer
->data_size
= data_size
;
719 buffer
->offsets_size
= offsets_size
;
720 buffer
->async_transaction
= is_async
;
722 proc
->free_async_space
-= size
+ sizeof(struct binder_buffer
);
723 binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC
,
724 "binder: %d: binder_alloc_buf size %zd "
725 "async free %zd\n", proc
->pid
, size
,
726 proc
->free_async_space
);
732 static void *buffer_start_page(struct binder_buffer
*buffer
)
734 return (void *)((uintptr_t)buffer
& PAGE_MASK
);
737 static void *buffer_end_page(struct binder_buffer
*buffer
)
739 return (void *)(((uintptr_t)(buffer
+ 1) - 1) & PAGE_MASK
);
742 static void binder_delete_free_buffer(struct binder_proc
*proc
,
743 struct binder_buffer
*buffer
)
745 struct binder_buffer
*prev
, *next
= NULL
;
746 int free_page_end
= 1;
747 int free_page_start
= 1;
749 BUG_ON(proc
->buffers
.next
== &buffer
->entry
);
750 prev
= list_entry(buffer
->entry
.prev
, struct binder_buffer
, entry
);
752 if (buffer_end_page(prev
) == buffer_start_page(buffer
)) {
754 if (buffer_end_page(prev
) == buffer_end_page(buffer
))
756 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
757 "binder: %d: merge free, buffer %p "
758 "share page with %p\n", proc
->pid
, buffer
, prev
);
761 if (!list_is_last(&buffer
->entry
, &proc
->buffers
)) {
762 next
= list_entry(buffer
->entry
.next
,
763 struct binder_buffer
, entry
);
764 if (buffer_start_page(next
) == buffer_end_page(buffer
)) {
766 if (buffer_start_page(next
) ==
767 buffer_start_page(buffer
))
769 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
770 "binder: %d: merge free, buffer"
771 " %p share page with %p\n", proc
->pid
,
775 list_del(&buffer
->entry
);
776 if (free_page_start
|| free_page_end
) {
777 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
778 "binder: %d: merge free, buffer %p do "
779 "not share page%s%s with with %p or %p\n",
780 proc
->pid
, buffer
, free_page_start
? "" : " end",
781 free_page_end
? "" : " start", prev
, next
);
782 binder_update_page_range(proc
, 0, free_page_start
?
783 buffer_start_page(buffer
) : buffer_end_page(buffer
),
784 (free_page_end
? buffer_end_page(buffer
) :
785 buffer_start_page(buffer
)) + PAGE_SIZE
, NULL
);
789 static void binder_free_buf(struct binder_proc
*proc
,
790 struct binder_buffer
*buffer
)
792 size_t size
, buffer_size
;
794 buffer_size
= binder_buffer_size(proc
, buffer
);
796 size
= ALIGN(buffer
->data_size
, sizeof(void *)) +
797 ALIGN(buffer
->offsets_size
, sizeof(void *));
799 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
800 "binder: %d: binder_free_buf %p size %zd buffer"
801 "_size %zd\n", proc
->pid
, buffer
, size
, buffer_size
);
803 BUG_ON(buffer
->free
);
804 BUG_ON(size
> buffer_size
);
805 BUG_ON(buffer
->transaction
!= NULL
);
806 BUG_ON((void *)buffer
< proc
->buffer
);
807 BUG_ON((void *)buffer
> proc
->buffer
+ proc
->buffer_size
);
809 if (buffer
->async_transaction
) {
810 proc
->free_async_space
+= size
+ sizeof(struct binder_buffer
);
812 binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC
,
813 "binder: %d: binder_free_buf size %zd "
814 "async free %zd\n", proc
->pid
, size
,
815 proc
->free_async_space
);
818 binder_update_page_range(proc
, 0,
819 (void *)PAGE_ALIGN((uintptr_t)buffer
->data
),
820 (void *)(((uintptr_t)buffer
->data
+ buffer_size
) & PAGE_MASK
),
822 rb_erase(&buffer
->rb_node
, &proc
->allocated_buffers
);
824 if (!list_is_last(&buffer
->entry
, &proc
->buffers
)) {
825 struct binder_buffer
*next
= list_entry(buffer
->entry
.next
,
826 struct binder_buffer
, entry
);
828 rb_erase(&next
->rb_node
, &proc
->free_buffers
);
829 binder_delete_free_buffer(proc
, next
);
832 if (proc
->buffers
.next
!= &buffer
->entry
) {
833 struct binder_buffer
*prev
= list_entry(buffer
->entry
.prev
,
834 struct binder_buffer
, entry
);
836 binder_delete_free_buffer(proc
, buffer
);
837 rb_erase(&prev
->rb_node
, &proc
->free_buffers
);
841 binder_insert_free_buffer(proc
, buffer
);
844 static struct binder_node
*binder_get_node(struct binder_proc
*proc
,
847 struct rb_node
*n
= proc
->nodes
.rb_node
;
848 struct binder_node
*node
;
851 node
= rb_entry(n
, struct binder_node
, rb_node
);
855 else if (ptr
> node
->ptr
)
863 static struct binder_node
*binder_new_node(struct binder_proc
*proc
,
867 struct rb_node
**p
= &proc
->nodes
.rb_node
;
868 struct rb_node
*parent
= NULL
;
869 struct binder_node
*node
;
873 node
= rb_entry(parent
, struct binder_node
, rb_node
);
877 else if (ptr
> node
->ptr
)
883 node
= kzalloc(sizeof(*node
), GFP_KERNEL
);
886 binder_stats_created(BINDER_STAT_NODE
);
887 rb_link_node(&node
->rb_node
, parent
, p
);
888 rb_insert_color(&node
->rb_node
, &proc
->nodes
);
889 node
->debug_id
= ++binder_last_id
;
892 node
->cookie
= cookie
;
893 node
->work
.type
= BINDER_WORK_NODE
;
894 INIT_LIST_HEAD(&node
->work
.entry
);
895 INIT_LIST_HEAD(&node
->async_todo
);
896 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
897 "binder: %d:%d node %d u%p c%p created\n",
898 proc
->pid
, current
->pid
, node
->debug_id
,
899 node
->ptr
, node
->cookie
);
903 static int binder_inc_node(struct binder_node
*node
, int strong
, int internal
,
904 struct list_head
*target_list
)
908 if (target_list
== NULL
&&
909 node
->internal_strong_refs
== 0 &&
910 !(node
== binder_context_mgr_node
&&
911 node
->has_strong_ref
)) {
912 pr_err("binder: invalid inc strong "
913 "node for %d\n", node
->debug_id
);
916 node
->internal_strong_refs
++;
918 node
->local_strong_refs
++;
919 if (!node
->has_strong_ref
&& target_list
) {
920 list_del_init(&node
->work
.entry
);
921 list_add_tail(&node
->work
.entry
, target_list
);
925 node
->local_weak_refs
++;
926 if (!node
->has_weak_ref
&& list_empty(&node
->work
.entry
)) {
927 if (target_list
== NULL
) {
928 pr_err("binder: invalid inc weak node "
929 "for %d\n", node
->debug_id
);
932 list_add_tail(&node
->work
.entry
, target_list
);
938 static int binder_dec_node(struct binder_node
*node
, int strong
, int internal
)
942 node
->internal_strong_refs
--;
944 node
->local_strong_refs
--;
945 if (node
->local_strong_refs
|| node
->internal_strong_refs
)
949 node
->local_weak_refs
--;
950 if (node
->local_weak_refs
|| !hlist_empty(&node
->refs
))
953 if (node
->proc
&& (node
->has_strong_ref
|| node
->has_weak_ref
)) {
954 if (list_empty(&node
->work
.entry
)) {
955 list_add_tail(&node
->work
.entry
, &node
->proc
->todo
);
956 wake_up_interruptible(&node
->proc
->wait
);
959 if (hlist_empty(&node
->refs
) && !node
->local_strong_refs
&&
960 !node
->local_weak_refs
) {
961 list_del_init(&node
->work
.entry
);
963 rb_erase(&node
->rb_node
, &node
->proc
->nodes
);
964 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
965 "binder: refless node %d deleted\n",
968 hlist_del(&node
->dead_node
);
969 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
970 "binder: dead node %d deleted\n",
974 binder_stats_deleted(BINDER_STAT_NODE
);
982 static struct binder_ref
*binder_get_ref(struct binder_proc
*proc
,
985 struct rb_node
*n
= proc
->refs_by_desc
.rb_node
;
986 struct binder_ref
*ref
;
989 ref
= rb_entry(n
, struct binder_ref
, rb_node_desc
);
991 if (desc
< ref
->desc
)
993 else if (desc
> ref
->desc
)
1001 static struct binder_ref
*binder_get_ref_for_node(struct binder_proc
*proc
,
1002 struct binder_node
*node
)
1005 struct rb_node
**p
= &proc
->refs_by_node
.rb_node
;
1006 struct rb_node
*parent
= NULL
;
1007 struct binder_ref
*ref
, *new_ref
;
1011 ref
= rb_entry(parent
, struct binder_ref
, rb_node_node
);
1013 if (node
< ref
->node
)
1015 else if (node
> ref
->node
)
1016 p
= &(*p
)->rb_right
;
1020 new_ref
= kzalloc(sizeof(*ref
), GFP_KERNEL
);
1021 if (new_ref
== NULL
)
1023 binder_stats_created(BINDER_STAT_REF
);
1024 new_ref
->debug_id
= ++binder_last_id
;
1025 new_ref
->proc
= proc
;
1026 new_ref
->node
= node
;
1027 rb_link_node(&new_ref
->rb_node_node
, parent
, p
);
1028 rb_insert_color(&new_ref
->rb_node_node
, &proc
->refs_by_node
);
1030 new_ref
->desc
= (node
== binder_context_mgr_node
) ? 0 : 1;
1031 for (n
= rb_first(&proc
->refs_by_desc
); n
!= NULL
; n
= rb_next(n
)) {
1032 ref
= rb_entry(n
, struct binder_ref
, rb_node_desc
);
1033 if (ref
->desc
> new_ref
->desc
)
1035 new_ref
->desc
= ref
->desc
+ 1;
1038 p
= &proc
->refs_by_desc
.rb_node
;
1041 ref
= rb_entry(parent
, struct binder_ref
, rb_node_desc
);
1043 if (new_ref
->desc
< ref
->desc
)
1045 else if (new_ref
->desc
> ref
->desc
)
1046 p
= &(*p
)->rb_right
;
1050 rb_link_node(&new_ref
->rb_node_desc
, parent
, p
);
1051 rb_insert_color(&new_ref
->rb_node_desc
, &proc
->refs_by_desc
);
1053 hlist_add_head(&new_ref
->node_entry
, &node
->refs
);
1055 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1056 "binder: %d new ref %d desc %d for "
1057 "node %d\n", proc
->pid
, new_ref
->debug_id
,
1058 new_ref
->desc
, node
->debug_id
);
1060 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1061 "binder: %d new ref %d desc %d for "
1062 "dead node\n", proc
->pid
, new_ref
->debug_id
,
1068 static void binder_delete_ref(struct binder_ref
*ref
)
1070 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1071 "binder: %d delete ref %d desc %d for "
1072 "node %d\n", ref
->proc
->pid
, ref
->debug_id
,
1073 ref
->desc
, ref
->node
->debug_id
);
1075 rb_erase(&ref
->rb_node_desc
, &ref
->proc
->refs_by_desc
);
1076 rb_erase(&ref
->rb_node_node
, &ref
->proc
->refs_by_node
);
1078 binder_dec_node(ref
->node
, 1, 1);
1079 hlist_del(&ref
->node_entry
);
1080 binder_dec_node(ref
->node
, 0, 1);
1082 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
1083 "binder: %d delete ref %d desc %d "
1084 "has death notification\n", ref
->proc
->pid
,
1085 ref
->debug_id
, ref
->desc
);
1086 list_del(&ref
->death
->work
.entry
);
1088 binder_stats_deleted(BINDER_STAT_DEATH
);
1091 binder_stats_deleted(BINDER_STAT_REF
);
1094 static int binder_inc_ref(struct binder_ref
*ref
, int strong
,
1095 struct list_head
*target_list
)
1099 if (ref
->strong
== 0) {
1100 ret
= binder_inc_node(ref
->node
, 1, 1, target_list
);
1106 if (ref
->weak
== 0) {
1107 ret
= binder_inc_node(ref
->node
, 0, 1, target_list
);
1117 static int binder_dec_ref(struct binder_ref
*ref
, int strong
)
1120 if (ref
->strong
== 0) {
1121 binder_user_error("binder: %d invalid dec strong, "
1122 "ref %d desc %d s %d w %d\n",
1123 ref
->proc
->pid
, ref
->debug_id
,
1124 ref
->desc
, ref
->strong
, ref
->weak
);
1128 if (ref
->strong
== 0) {
1130 ret
= binder_dec_node(ref
->node
, strong
, 1);
1135 if (ref
->weak
== 0) {
1136 binder_user_error("binder: %d invalid dec weak, "
1137 "ref %d desc %d s %d w %d\n",
1138 ref
->proc
->pid
, ref
->debug_id
,
1139 ref
->desc
, ref
->strong
, ref
->weak
);
1144 if (ref
->strong
== 0 && ref
->weak
== 0)
1145 binder_delete_ref(ref
);
1149 static void binder_pop_transaction(struct binder_thread
*target_thread
,
1150 struct binder_transaction
*t
)
1152 if (target_thread
) {
1153 BUG_ON(target_thread
->transaction_stack
!= t
);
1154 BUG_ON(target_thread
->transaction_stack
->from
!= target_thread
);
1155 target_thread
->transaction_stack
=
1156 target_thread
->transaction_stack
->from_parent
;
1161 t
->buffer
->transaction
= NULL
;
1163 binder_stats_deleted(BINDER_STAT_TRANSACTION
);
1166 static void binder_send_failed_reply(struct binder_transaction
*t
,
1167 uint32_t error_code
)
1169 struct binder_thread
*target_thread
;
1170 BUG_ON(t
->flags
& TF_ONE_WAY
);
1172 target_thread
= t
->from
;
1173 if (target_thread
) {
1174 if (target_thread
->return_error
!= BR_OK
&&
1175 target_thread
->return_error2
== BR_OK
) {
1176 target_thread
->return_error2
=
1177 target_thread
->return_error
;
1178 target_thread
->return_error
= BR_OK
;
1180 if (target_thread
->return_error
== BR_OK
) {
1181 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
1182 "binder: send failed reply for "
1183 "transaction %d to %d:%d\n",
1184 t
->debug_id
, target_thread
->proc
->pid
,
1185 target_thread
->pid
);
1187 binder_pop_transaction(target_thread
, t
);
1188 target_thread
->return_error
= error_code
;
1189 wake_up_interruptible(&target_thread
->wait
);
1191 pr_err("binder: reply failed, target "
1192 "thread, %d:%d, has error code %d "
1193 "already\n", target_thread
->proc
->pid
,
1195 target_thread
->return_error
);
1199 struct binder_transaction
*next
= t
->from_parent
;
1201 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
1202 "binder: send failed reply "
1203 "for transaction %d, target dead\n",
1206 binder_pop_transaction(target_thread
, t
);
1208 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
1209 "binder: reply failed,"
1210 " no target thread at root\n");
1214 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
1215 "binder: reply failed, no target "
1216 "thread -- retry %d\n", t
->debug_id
);
1221 static void binder_transaction_buffer_release(struct binder_proc
*proc
,
1222 struct binder_buffer
*buffer
,
1225 size_t *offp
, *off_end
;
1226 int debug_id
= buffer
->debug_id
;
1228 binder_debug(BINDER_DEBUG_TRANSACTION
,
1229 "binder: %d buffer release %d, size %zd-%zd, failed at %p\n",
1230 proc
->pid
, buffer
->debug_id
,
1231 buffer
->data_size
, buffer
->offsets_size
, failed_at
);
1233 if (buffer
->target_node
)
1234 binder_dec_node(buffer
->target_node
, 1, 0);
1236 offp
= (size_t *)(buffer
->data
+ ALIGN(buffer
->data_size
, sizeof(void *)));
1238 off_end
= failed_at
;
1240 off_end
= (void *)offp
+ buffer
->offsets_size
;
1241 for (; offp
< off_end
; offp
++) {
1242 struct flat_binder_object
*fp
;
1243 if (*offp
> buffer
->data_size
- sizeof(*fp
) ||
1244 buffer
->data_size
< sizeof(*fp
) ||
1245 !IS_ALIGNED(*offp
, sizeof(void *))) {
1246 pr_err("binder: transaction release %d bad"
1247 "offset %zd, size %zd\n", debug_id
,
1248 *offp
, buffer
->data_size
);
1251 fp
= (struct flat_binder_object
*)(buffer
->data
+ *offp
);
1253 case BINDER_TYPE_BINDER
:
1254 case BINDER_TYPE_WEAK_BINDER
: {
1255 struct binder_node
*node
= binder_get_node(proc
, fp
->binder
);
1257 pr_err("binder: transaction release %d"
1258 " bad node %p\n", debug_id
, fp
->binder
);
1261 binder_debug(BINDER_DEBUG_TRANSACTION
,
1263 node
->debug_id
, node
->ptr
);
1264 binder_dec_node(node
, fp
->type
== BINDER_TYPE_BINDER
, 0);
1266 case BINDER_TYPE_HANDLE
:
1267 case BINDER_TYPE_WEAK_HANDLE
: {
1268 struct binder_ref
*ref
= binder_get_ref(proc
, fp
->handle
);
1270 pr_err("binder: transaction release %d"
1271 " bad handle %ld\n", debug_id
,
1275 binder_debug(BINDER_DEBUG_TRANSACTION
,
1276 " ref %d desc %d (node %d)\n",
1277 ref
->debug_id
, ref
->desc
, ref
->node
->debug_id
);
1278 binder_dec_ref(ref
, fp
->type
== BINDER_TYPE_HANDLE
);
1281 case BINDER_TYPE_FD
:
1282 binder_debug(BINDER_DEBUG_TRANSACTION
,
1283 " fd %ld\n", fp
->handle
);
1285 task_close_fd(proc
, fp
->handle
);
1289 pr_err("binder: transaction release %d bad "
1290 "object type %lx\n", debug_id
, fp
->type
);
1296 static void binder_transaction(struct binder_proc
*proc
,
1297 struct binder_thread
*thread
,
1298 struct binder_transaction_data
*tr
, int reply
)
1300 struct binder_transaction
*t
;
1301 struct binder_work
*tcomplete
;
1302 size_t *offp
, *off_end
;
1303 struct binder_proc
*target_proc
;
1304 struct binder_thread
*target_thread
= NULL
;
1305 struct binder_node
*target_node
= NULL
;
1306 struct list_head
*target_list
;
1307 wait_queue_head_t
*target_wait
;
1308 struct binder_transaction
*in_reply_to
= NULL
;
1309 struct binder_transaction_log_entry
*e
;
1310 uint32_t return_error
;
1312 e
= binder_transaction_log_add(&binder_transaction_log
);
1313 e
->call_type
= reply
? 2 : !!(tr
->flags
& TF_ONE_WAY
);
1314 e
->from_proc
= proc
->pid
;
1315 e
->from_thread
= thread
->pid
;
1316 e
->target_handle
= tr
->target
.handle
;
1317 e
->data_size
= tr
->data_size
;
1318 e
->offsets_size
= tr
->offsets_size
;
1321 in_reply_to
= thread
->transaction_stack
;
1322 if (in_reply_to
== NULL
) {
1323 binder_user_error("binder: %d:%d got reply transaction "
1324 "with no transaction stack\n",
1325 proc
->pid
, thread
->pid
);
1326 return_error
= BR_FAILED_REPLY
;
1327 goto err_empty_call_stack
;
1329 binder_set_nice(in_reply_to
->saved_priority
);
1330 if (in_reply_to
->to_thread
!= thread
) {
1331 binder_user_error("binder: %d:%d got reply transaction "
1332 "with bad transaction stack,"
1333 " transaction %d has target %d:%d\n",
1334 proc
->pid
, thread
->pid
, in_reply_to
->debug_id
,
1335 in_reply_to
->to_proc
?
1336 in_reply_to
->to_proc
->pid
: 0,
1337 in_reply_to
->to_thread
?
1338 in_reply_to
->to_thread
->pid
: 0);
1339 return_error
= BR_FAILED_REPLY
;
1341 goto err_bad_call_stack
;
1343 thread
->transaction_stack
= in_reply_to
->to_parent
;
1344 target_thread
= in_reply_to
->from
;
1345 if (target_thread
== NULL
) {
1346 return_error
= BR_DEAD_REPLY
;
1347 goto err_dead_binder
;
1349 if (target_thread
->transaction_stack
!= in_reply_to
) {
1350 binder_user_error("binder: %d:%d got reply transaction "
1351 "with bad target transaction stack %d, "
1353 proc
->pid
, thread
->pid
,
1354 target_thread
->transaction_stack
?
1355 target_thread
->transaction_stack
->debug_id
: 0,
1356 in_reply_to
->debug_id
);
1357 return_error
= BR_FAILED_REPLY
;
1359 target_thread
= NULL
;
1360 goto err_dead_binder
;
1362 target_proc
= target_thread
->proc
;
1364 if (tr
->target
.handle
) {
1365 struct binder_ref
*ref
;
1366 ref
= binder_get_ref(proc
, tr
->target
.handle
);
1368 binder_user_error("binder: %d:%d got "
1369 "transaction to invalid handle\n",
1370 proc
->pid
, thread
->pid
);
1371 return_error
= BR_FAILED_REPLY
;
1372 goto err_invalid_target_handle
;
1374 target_node
= ref
->node
;
1376 target_node
= binder_context_mgr_node
;
1377 if (target_node
== NULL
) {
1378 return_error
= BR_DEAD_REPLY
;
1379 goto err_no_context_mgr_node
;
1382 e
->to_node
= target_node
->debug_id
;
1383 target_proc
= target_node
->proc
;
1384 if (target_proc
== NULL
) {
1385 return_error
= BR_DEAD_REPLY
;
1386 goto err_dead_binder
;
1388 if (!(tr
->flags
& TF_ONE_WAY
) && thread
->transaction_stack
) {
1389 struct binder_transaction
*tmp
;
1390 tmp
= thread
->transaction_stack
;
1391 if (tmp
->to_thread
!= thread
) {
1392 binder_user_error("binder: %d:%d got new "
1393 "transaction with bad transaction stack"
1394 ", transaction %d has target %d:%d\n",
1395 proc
->pid
, thread
->pid
, tmp
->debug_id
,
1396 tmp
->to_proc
? tmp
->to_proc
->pid
: 0,
1398 tmp
->to_thread
->pid
: 0);
1399 return_error
= BR_FAILED_REPLY
;
1400 goto err_bad_call_stack
;
1403 if (tmp
->from
&& tmp
->from
->proc
== target_proc
)
1404 target_thread
= tmp
->from
;
1405 tmp
= tmp
->from_parent
;
1409 if (target_thread
) {
1410 e
->to_thread
= target_thread
->pid
;
1411 target_list
= &target_thread
->todo
;
1412 target_wait
= &target_thread
->wait
;
1414 target_list
= &target_proc
->todo
;
1415 target_wait
= &target_proc
->wait
;
1417 e
->to_proc
= target_proc
->pid
;
1419 /* TODO: reuse incoming transaction for reply */
1420 t
= kzalloc(sizeof(*t
), GFP_KERNEL
);
1422 return_error
= BR_FAILED_REPLY
;
1423 goto err_alloc_t_failed
;
1425 binder_stats_created(BINDER_STAT_TRANSACTION
);
1427 tcomplete
= kzalloc(sizeof(*tcomplete
), GFP_KERNEL
);
1428 if (tcomplete
== NULL
) {
1429 return_error
= BR_FAILED_REPLY
;
1430 goto err_alloc_tcomplete_failed
;
1432 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE
);
1434 t
->debug_id
= ++binder_last_id
;
1435 e
->debug_id
= t
->debug_id
;
1438 binder_debug(BINDER_DEBUG_TRANSACTION
,
1439 "binder: %d:%d BC_REPLY %d -> %d:%d, "
1440 "data %p-%p size %zd-%zd\n",
1441 proc
->pid
, thread
->pid
, t
->debug_id
,
1442 target_proc
->pid
, target_thread
->pid
,
1443 tr
->data
.ptr
.buffer
, tr
->data
.ptr
.offsets
,
1444 tr
->data_size
, tr
->offsets_size
);
1446 binder_debug(BINDER_DEBUG_TRANSACTION
,
1447 "binder: %d:%d BC_TRANSACTION %d -> "
1448 "%d - node %d, data %p-%p size %zd-%zd\n",
1449 proc
->pid
, thread
->pid
, t
->debug_id
,
1450 target_proc
->pid
, target_node
->debug_id
,
1451 tr
->data
.ptr
.buffer
, tr
->data
.ptr
.offsets
,
1452 tr
->data_size
, tr
->offsets_size
);
1454 if (!reply
&& !(tr
->flags
& TF_ONE_WAY
))
1458 t
->sender_euid
= proc
->tsk
->cred
->euid
;
1459 t
->to_proc
= target_proc
;
1460 t
->to_thread
= target_thread
;
1462 t
->flags
= tr
->flags
;
1463 t
->priority
= task_nice(current
);
1464 t
->buffer
= binder_alloc_buf(target_proc
, tr
->data_size
,
1465 tr
->offsets_size
, !reply
&& (t
->flags
& TF_ONE_WAY
));
1466 if (t
->buffer
== NULL
) {
1467 return_error
= BR_FAILED_REPLY
;
1468 goto err_binder_alloc_buf_failed
;
1470 t
->buffer
->allow_user_free
= 0;
1471 t
->buffer
->debug_id
= t
->debug_id
;
1472 t
->buffer
->transaction
= t
;
1473 t
->buffer
->target_node
= target_node
;
1475 binder_inc_node(target_node
, 1, 0, NULL
);
1477 offp
= (size_t *)(t
->buffer
->data
+ ALIGN(tr
->data_size
, sizeof(void *)));
1479 if (copy_from_user(t
->buffer
->data
, tr
->data
.ptr
.buffer
, tr
->data_size
)) {
1480 binder_user_error("binder: %d:%d got transaction with invalid "
1481 "data ptr\n", proc
->pid
, thread
->pid
);
1482 return_error
= BR_FAILED_REPLY
;
1483 goto err_copy_data_failed
;
1485 if (copy_from_user(offp
, tr
->data
.ptr
.offsets
, tr
->offsets_size
)) {
1486 binder_user_error("binder: %d:%d got transaction with invalid "
1487 "offsets ptr\n", proc
->pid
, thread
->pid
);
1488 return_error
= BR_FAILED_REPLY
;
1489 goto err_copy_data_failed
;
1491 if (!IS_ALIGNED(tr
->offsets_size
, sizeof(size_t))) {
1492 binder_user_error("binder: %d:%d got transaction with "
1493 "invalid offsets size, %zd\n",
1494 proc
->pid
, thread
->pid
, tr
->offsets_size
);
1495 return_error
= BR_FAILED_REPLY
;
1496 goto err_bad_offset
;
1498 off_end
= (void *)offp
+ tr
->offsets_size
;
1499 for (; offp
< off_end
; offp
++) {
1500 struct flat_binder_object
*fp
;
1501 if (*offp
> t
->buffer
->data_size
- sizeof(*fp
) ||
1502 t
->buffer
->data_size
< sizeof(*fp
) ||
1503 !IS_ALIGNED(*offp
, sizeof(void *))) {
1504 binder_user_error("binder: %d:%d got transaction with "
1505 "invalid offset, %zd\n",
1506 proc
->pid
, thread
->pid
, *offp
);
1507 return_error
= BR_FAILED_REPLY
;
1508 goto err_bad_offset
;
1510 fp
= (struct flat_binder_object
*)(t
->buffer
->data
+ *offp
);
1512 case BINDER_TYPE_BINDER
:
1513 case BINDER_TYPE_WEAK_BINDER
: {
1514 struct binder_ref
*ref
;
1515 struct binder_node
*node
= binder_get_node(proc
, fp
->binder
);
1517 node
= binder_new_node(proc
, fp
->binder
, fp
->cookie
);
1519 return_error
= BR_FAILED_REPLY
;
1520 goto err_binder_new_node_failed
;
1522 node
->min_priority
= fp
->flags
& FLAT_BINDER_FLAG_PRIORITY_MASK
;
1523 node
->accept_fds
= !!(fp
->flags
& FLAT_BINDER_FLAG_ACCEPTS_FDS
);
1525 if (fp
->cookie
!= node
->cookie
) {
1526 binder_user_error("binder: %d:%d sending u%p "
1527 "node %d, cookie mismatch %p != %p\n",
1528 proc
->pid
, thread
->pid
,
1529 fp
->binder
, node
->debug_id
,
1530 fp
->cookie
, node
->cookie
);
1531 goto err_binder_get_ref_for_node_failed
;
1533 ref
= binder_get_ref_for_node(target_proc
, node
);
1535 return_error
= BR_FAILED_REPLY
;
1536 goto err_binder_get_ref_for_node_failed
;
1538 if (fp
->type
== BINDER_TYPE_BINDER
)
1539 fp
->type
= BINDER_TYPE_HANDLE
;
1541 fp
->type
= BINDER_TYPE_WEAK_HANDLE
;
1542 fp
->handle
= ref
->desc
;
1543 binder_inc_ref(ref
, fp
->type
== BINDER_TYPE_HANDLE
,
1546 binder_debug(BINDER_DEBUG_TRANSACTION
,
1547 " node %d u%p -> ref %d desc %d\n",
1548 node
->debug_id
, node
->ptr
, ref
->debug_id
,
1551 case BINDER_TYPE_HANDLE
:
1552 case BINDER_TYPE_WEAK_HANDLE
: {
1553 struct binder_ref
*ref
= binder_get_ref(proc
, fp
->handle
);
1555 binder_user_error("binder: %d:%d got "
1556 "transaction with invalid "
1557 "handle, %ld\n", proc
->pid
,
1558 thread
->pid
, fp
->handle
);
1559 return_error
= BR_FAILED_REPLY
;
1560 goto err_binder_get_ref_failed
;
1562 if (ref
->node
->proc
== target_proc
) {
1563 if (fp
->type
== BINDER_TYPE_HANDLE
)
1564 fp
->type
= BINDER_TYPE_BINDER
;
1566 fp
->type
= BINDER_TYPE_WEAK_BINDER
;
1567 fp
->binder
= ref
->node
->ptr
;
1568 fp
->cookie
= ref
->node
->cookie
;
1569 binder_inc_node(ref
->node
, fp
->type
== BINDER_TYPE_BINDER
, 0, NULL
);
1570 binder_debug(BINDER_DEBUG_TRANSACTION
,
1571 " ref %d desc %d -> node %d u%p\n",
1572 ref
->debug_id
, ref
->desc
, ref
->node
->debug_id
,
1575 struct binder_ref
*new_ref
;
1576 new_ref
= binder_get_ref_for_node(target_proc
, ref
->node
);
1577 if (new_ref
== NULL
) {
1578 return_error
= BR_FAILED_REPLY
;
1579 goto err_binder_get_ref_for_node_failed
;
1581 fp
->handle
= new_ref
->desc
;
1582 binder_inc_ref(new_ref
, fp
->type
== BINDER_TYPE_HANDLE
, NULL
);
1583 binder_debug(BINDER_DEBUG_TRANSACTION
,
1584 " ref %d desc %d -> ref %d desc %d (node %d)\n",
1585 ref
->debug_id
, ref
->desc
, new_ref
->debug_id
,
1586 new_ref
->desc
, ref
->node
->debug_id
);
1590 case BINDER_TYPE_FD
: {
1595 if (!(in_reply_to
->flags
& TF_ACCEPT_FDS
)) {
1596 binder_user_error("binder: %d:%d got reply with fd, %ld, but target does not allow fds\n",
1597 proc
->pid
, thread
->pid
, fp
->handle
);
1598 return_error
= BR_FAILED_REPLY
;
1599 goto err_fd_not_allowed
;
1601 } else if (!target_node
->accept_fds
) {
1602 binder_user_error("binder: %d:%d got transaction with fd, %ld, but target does not allow fds\n",
1603 proc
->pid
, thread
->pid
, fp
->handle
);
1604 return_error
= BR_FAILED_REPLY
;
1605 goto err_fd_not_allowed
;
1608 file
= fget(fp
->handle
);
1610 binder_user_error("binder: %d:%d got transaction with invalid fd, %ld\n",
1611 proc
->pid
, thread
->pid
, fp
->handle
);
1612 return_error
= BR_FAILED_REPLY
;
1613 goto err_fget_failed
;
1615 target_fd
= task_get_unused_fd_flags(target_proc
, O_CLOEXEC
);
1616 if (target_fd
< 0) {
1618 return_error
= BR_FAILED_REPLY
;
1619 goto err_get_unused_fd_failed
;
1621 task_fd_install(target_proc
, target_fd
, file
);
1622 binder_debug(BINDER_DEBUG_TRANSACTION
,
1623 " fd %ld -> %d\n", fp
->handle
, target_fd
);
1625 fp
->handle
= target_fd
;
1629 binder_user_error("binder: %d:%d got transactio"
1630 "n with invalid object type, %lx\n",
1631 proc
->pid
, thread
->pid
, fp
->type
);
1632 return_error
= BR_FAILED_REPLY
;
1633 goto err_bad_object_type
;
1637 BUG_ON(t
->buffer
->async_transaction
!= 0);
1638 binder_pop_transaction(target_thread
, in_reply_to
);
1639 } else if (!(t
->flags
& TF_ONE_WAY
)) {
1640 BUG_ON(t
->buffer
->async_transaction
!= 0);
1642 t
->from_parent
= thread
->transaction_stack
;
1643 thread
->transaction_stack
= t
;
1645 BUG_ON(target_node
== NULL
);
1646 BUG_ON(t
->buffer
->async_transaction
!= 1);
1647 if (target_node
->has_async_transaction
) {
1648 target_list
= &target_node
->async_todo
;
1651 target_node
->has_async_transaction
= 1;
1653 t
->work
.type
= BINDER_WORK_TRANSACTION
;
1654 list_add_tail(&t
->work
.entry
, target_list
);
1655 tcomplete
->type
= BINDER_WORK_TRANSACTION_COMPLETE
;
1656 list_add_tail(&tcomplete
->entry
, &thread
->todo
);
1658 wake_up_interruptible(target_wait
);
1661 err_get_unused_fd_failed
:
1664 err_binder_get_ref_for_node_failed
:
1665 err_binder_get_ref_failed
:
1666 err_binder_new_node_failed
:
1667 err_bad_object_type
:
1669 err_copy_data_failed
:
1670 binder_transaction_buffer_release(target_proc
, t
->buffer
, offp
);
1671 t
->buffer
->transaction
= NULL
;
1672 binder_free_buf(target_proc
, t
->buffer
);
1673 err_binder_alloc_buf_failed
:
1675 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE
);
1676 err_alloc_tcomplete_failed
:
1678 binder_stats_deleted(BINDER_STAT_TRANSACTION
);
1681 err_empty_call_stack
:
1683 err_invalid_target_handle
:
1684 err_no_context_mgr_node
:
1685 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
1686 "binder: %d:%d transaction failed %d, size %zd-%zd\n",
1687 proc
->pid
, thread
->pid
, return_error
,
1688 tr
->data_size
, tr
->offsets_size
);
1691 struct binder_transaction_log_entry
*fe
;
1692 fe
= binder_transaction_log_add(&binder_transaction_log_failed
);
1696 BUG_ON(thread
->return_error
!= BR_OK
);
1698 thread
->return_error
= BR_TRANSACTION_COMPLETE
;
1699 binder_send_failed_reply(in_reply_to
, return_error
);
1701 thread
->return_error
= return_error
;
1704 int binder_thread_write(struct binder_proc
*proc
, struct binder_thread
*thread
,
1705 void __user
*buffer
, int size
, signed long *consumed
)
1708 void __user
*ptr
= buffer
+ *consumed
;
1709 void __user
*end
= buffer
+ size
;
1711 while (ptr
< end
&& thread
->return_error
== BR_OK
) {
1712 if (get_user(cmd
, (uint32_t __user
*)ptr
))
1714 ptr
+= sizeof(uint32_t);
1715 if (_IOC_NR(cmd
) < ARRAY_SIZE(binder_stats
.bc
)) {
1716 binder_stats
.bc
[_IOC_NR(cmd
)]++;
1717 proc
->stats
.bc
[_IOC_NR(cmd
)]++;
1718 thread
->stats
.bc
[_IOC_NR(cmd
)]++;
1726 struct binder_ref
*ref
;
1727 const char *debug_string
;
1729 if (get_user(target
, (uint32_t __user
*)ptr
))
1731 ptr
+= sizeof(uint32_t);
1732 if (target
== 0 && binder_context_mgr_node
&&
1733 (cmd
== BC_INCREFS
|| cmd
== BC_ACQUIRE
)) {
1734 ref
= binder_get_ref_for_node(proc
,
1735 binder_context_mgr_node
);
1736 if (ref
->desc
!= target
) {
1737 binder_user_error("binder: %d:"
1738 "%d tried to acquire "
1739 "reference to desc 0, "
1741 proc
->pid
, thread
->pid
,
1745 ref
= binder_get_ref(proc
, target
);
1747 binder_user_error("binder: %d:%d refcou"
1748 "nt change on invalid ref %d\n",
1749 proc
->pid
, thread
->pid
, target
);
1754 debug_string
= "IncRefs";
1755 binder_inc_ref(ref
, 0, NULL
);
1758 debug_string
= "Acquire";
1759 binder_inc_ref(ref
, 1, NULL
);
1762 debug_string
= "Release";
1763 binder_dec_ref(ref
, 1);
1767 debug_string
= "DecRefs";
1768 binder_dec_ref(ref
, 0);
1771 binder_debug(BINDER_DEBUG_USER_REFS
,
1772 "binder: %d:%d %s ref %d desc %d s %d w %d for node %d\n",
1773 proc
->pid
, thread
->pid
, debug_string
, ref
->debug_id
,
1774 ref
->desc
, ref
->strong
, ref
->weak
, ref
->node
->debug_id
);
1777 case BC_INCREFS_DONE
:
1778 case BC_ACQUIRE_DONE
: {
1779 void __user
*node_ptr
;
1781 struct binder_node
*node
;
1783 if (get_user(node_ptr
, (void * __user
*)ptr
))
1785 ptr
+= sizeof(void *);
1786 if (get_user(cookie
, (void * __user
*)ptr
))
1788 ptr
+= sizeof(void *);
1789 node
= binder_get_node(proc
, node_ptr
);
1791 binder_user_error("binder: %d:%d "
1792 "%s u%p no match\n",
1793 proc
->pid
, thread
->pid
,
1794 cmd
== BC_INCREFS_DONE
?
1800 if (cookie
!= node
->cookie
) {
1801 binder_user_error("binder: %d:%d %s u%p node %d"
1802 " cookie mismatch %p != %p\n",
1803 proc
->pid
, thread
->pid
,
1804 cmd
== BC_INCREFS_DONE
?
1805 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
1806 node_ptr
, node
->debug_id
,
1807 cookie
, node
->cookie
);
1810 if (cmd
== BC_ACQUIRE_DONE
) {
1811 if (node
->pending_strong_ref
== 0) {
1812 binder_user_error("binder: %d:%d "
1813 "BC_ACQUIRE_DONE node %d has "
1814 "no pending acquire request\n",
1815 proc
->pid
, thread
->pid
,
1819 node
->pending_strong_ref
= 0;
1821 if (node
->pending_weak_ref
== 0) {
1822 binder_user_error("binder: %d:%d "
1823 "BC_INCREFS_DONE node %d has "
1824 "no pending increfs request\n",
1825 proc
->pid
, thread
->pid
,
1829 node
->pending_weak_ref
= 0;
1831 binder_dec_node(node
, cmd
== BC_ACQUIRE_DONE
, 0);
1832 binder_debug(BINDER_DEBUG_USER_REFS
,
1833 "binder: %d:%d %s node %d ls %d lw %d\n",
1834 proc
->pid
, thread
->pid
,
1835 cmd
== BC_INCREFS_DONE
? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
1836 node
->debug_id
, node
->local_strong_refs
, node
->local_weak_refs
);
1839 case BC_ATTEMPT_ACQUIRE
:
1840 pr_err("binder: BC_ATTEMPT_ACQUIRE not supported\n");
1842 case BC_ACQUIRE_RESULT
:
1843 pr_err("binder: BC_ACQUIRE_RESULT not supported\n");
1846 case BC_FREE_BUFFER
: {
1847 void __user
*data_ptr
;
1848 struct binder_buffer
*buffer
;
1850 if (get_user(data_ptr
, (void * __user
*)ptr
))
1852 ptr
+= sizeof(void *);
1854 buffer
= binder_buffer_lookup(proc
, data_ptr
);
1855 if (buffer
== NULL
) {
1856 binder_user_error("binder: %d:%d "
1857 "BC_FREE_BUFFER u%p no match\n",
1858 proc
->pid
, thread
->pid
, data_ptr
);
1861 if (!buffer
->allow_user_free
) {
1862 binder_user_error("binder: %d:%d "
1863 "BC_FREE_BUFFER u%p matched "
1864 "unreturned buffer\n",
1865 proc
->pid
, thread
->pid
, data_ptr
);
1868 binder_debug(BINDER_DEBUG_FREE_BUFFER
,
1869 "binder: %d:%d BC_FREE_BUFFER u%p found buffer %d for %s transaction\n",
1870 proc
->pid
, thread
->pid
, data_ptr
, buffer
->debug_id
,
1871 buffer
->transaction
? "active" : "finished");
1873 if (buffer
->transaction
) {
1874 buffer
->transaction
->buffer
= NULL
;
1875 buffer
->transaction
= NULL
;
1877 if (buffer
->async_transaction
&& buffer
->target_node
) {
1878 BUG_ON(!buffer
->target_node
->has_async_transaction
);
1879 if (list_empty(&buffer
->target_node
->async_todo
))
1880 buffer
->target_node
->has_async_transaction
= 0;
1882 list_move_tail(buffer
->target_node
->async_todo
.next
, &thread
->todo
);
1884 binder_transaction_buffer_release(proc
, buffer
, NULL
);
1885 binder_free_buf(proc
, buffer
);
1889 case BC_TRANSACTION
:
1891 struct binder_transaction_data tr
;
1893 if (copy_from_user(&tr
, ptr
, sizeof(tr
)))
1896 binder_transaction(proc
, thread
, &tr
, cmd
== BC_REPLY
);
1900 case BC_REGISTER_LOOPER
:
1901 binder_debug(BINDER_DEBUG_THREADS
,
1902 "binder: %d:%d BC_REGISTER_LOOPER\n",
1903 proc
->pid
, thread
->pid
);
1904 if (thread
->looper
& BINDER_LOOPER_STATE_ENTERED
) {
1905 thread
->looper
|= BINDER_LOOPER_STATE_INVALID
;
1906 binder_user_error("binder: %d:%d ERROR:"
1907 " BC_REGISTER_LOOPER called "
1908 "after BC_ENTER_LOOPER\n",
1909 proc
->pid
, thread
->pid
);
1910 } else if (proc
->requested_threads
== 0) {
1911 thread
->looper
|= BINDER_LOOPER_STATE_INVALID
;
1912 binder_user_error("binder: %d:%d ERROR:"
1913 " BC_REGISTER_LOOPER called "
1914 "without request\n",
1915 proc
->pid
, thread
->pid
);
1917 proc
->requested_threads
--;
1918 proc
->requested_threads_started
++;
1920 thread
->looper
|= BINDER_LOOPER_STATE_REGISTERED
;
1922 case BC_ENTER_LOOPER
:
1923 binder_debug(BINDER_DEBUG_THREADS
,
1924 "binder: %d:%d BC_ENTER_LOOPER\n",
1925 proc
->pid
, thread
->pid
);
1926 if (thread
->looper
& BINDER_LOOPER_STATE_REGISTERED
) {
1927 thread
->looper
|= BINDER_LOOPER_STATE_INVALID
;
1928 binder_user_error("binder: %d:%d ERROR:"
1929 " BC_ENTER_LOOPER called after "
1930 "BC_REGISTER_LOOPER\n",
1931 proc
->pid
, thread
->pid
);
1933 thread
->looper
|= BINDER_LOOPER_STATE_ENTERED
;
1935 case BC_EXIT_LOOPER
:
1936 binder_debug(BINDER_DEBUG_THREADS
,
1937 "binder: %d:%d BC_EXIT_LOOPER\n",
1938 proc
->pid
, thread
->pid
);
1939 thread
->looper
|= BINDER_LOOPER_STATE_EXITED
;
1942 case BC_REQUEST_DEATH_NOTIFICATION
:
1943 case BC_CLEAR_DEATH_NOTIFICATION
: {
1945 void __user
*cookie
;
1946 struct binder_ref
*ref
;
1947 struct binder_ref_death
*death
;
1949 if (get_user(target
, (uint32_t __user
*)ptr
))
1951 ptr
+= sizeof(uint32_t);
1952 if (get_user(cookie
, (void __user
* __user
*)ptr
))
1954 ptr
+= sizeof(void *);
1955 ref
= binder_get_ref(proc
, target
);
1957 binder_user_error("binder: %d:%d %s "
1959 proc
->pid
, thread
->pid
,
1960 cmd
== BC_REQUEST_DEATH_NOTIFICATION
?
1961 "BC_REQUEST_DEATH_NOTIFICATION" :
1962 "BC_CLEAR_DEATH_NOTIFICATION",
1967 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION
,
1968 "binder: %d:%d %s %p ref %d desc %d s %d w %d for node %d\n",
1969 proc
->pid
, thread
->pid
,
1970 cmd
== BC_REQUEST_DEATH_NOTIFICATION
?
1971 "BC_REQUEST_DEATH_NOTIFICATION" :
1972 "BC_CLEAR_DEATH_NOTIFICATION",
1973 cookie
, ref
->debug_id
, ref
->desc
,
1974 ref
->strong
, ref
->weak
, ref
->node
->debug_id
);
1976 if (cmd
== BC_REQUEST_DEATH_NOTIFICATION
) {
1978 binder_user_error("binder: %d:%"
1979 "d BC_REQUEST_DEATH_NOTI"
1980 "FICATION death notific"
1981 "ation already set\n",
1982 proc
->pid
, thread
->pid
);
1985 death
= kzalloc(sizeof(*death
), GFP_KERNEL
);
1986 if (death
== NULL
) {
1987 thread
->return_error
= BR_ERROR
;
1988 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
1990 "BC_REQUEST_DEATH_NOTIFICATION failed\n",
1991 proc
->pid
, thread
->pid
);
1994 binder_stats_created(BINDER_STAT_DEATH
);
1995 INIT_LIST_HEAD(&death
->work
.entry
);
1996 death
->cookie
= cookie
;
1998 if (ref
->node
->proc
== NULL
) {
1999 ref
->death
->work
.type
= BINDER_WORK_DEAD_BINDER
;
2000 if (thread
->looper
& (BINDER_LOOPER_STATE_REGISTERED
| BINDER_LOOPER_STATE_ENTERED
)) {
2001 list_add_tail(&ref
->death
->work
.entry
, &thread
->todo
);
2003 list_add_tail(&ref
->death
->work
.entry
, &proc
->todo
);
2004 wake_up_interruptible(&proc
->wait
);
2008 if (ref
->death
== NULL
) {
2009 binder_user_error("binder: %d:%"
2010 "d BC_CLEAR_DEATH_NOTIFI"
2011 "CATION death notificat"
2013 proc
->pid
, thread
->pid
);
2017 if (death
->cookie
!= cookie
) {
2018 binder_user_error("binder: %d:%"
2019 "d BC_CLEAR_DEATH_NOTIFI"
2020 "CATION death notificat"
2021 "ion cookie mismatch "
2023 proc
->pid
, thread
->pid
,
2024 death
->cookie
, cookie
);
2028 if (list_empty(&death
->work
.entry
)) {
2029 death
->work
.type
= BINDER_WORK_CLEAR_DEATH_NOTIFICATION
;
2030 if (thread
->looper
& (BINDER_LOOPER_STATE_REGISTERED
| BINDER_LOOPER_STATE_ENTERED
)) {
2031 list_add_tail(&death
->work
.entry
, &thread
->todo
);
2033 list_add_tail(&death
->work
.entry
, &proc
->todo
);
2034 wake_up_interruptible(&proc
->wait
);
2037 BUG_ON(death
->work
.type
!= BINDER_WORK_DEAD_BINDER
);
2038 death
->work
.type
= BINDER_WORK_DEAD_BINDER_AND_CLEAR
;
2042 case BC_DEAD_BINDER_DONE
: {
2043 struct binder_work
*w
;
2044 void __user
*cookie
;
2045 struct binder_ref_death
*death
= NULL
;
2046 if (get_user(cookie
, (void __user
* __user
*)ptr
))
2049 ptr
+= sizeof(void *);
2050 list_for_each_entry(w
, &proc
->delivered_death
, entry
) {
2051 struct binder_ref_death
*tmp_death
= container_of(w
, struct binder_ref_death
, work
);
2052 if (tmp_death
->cookie
== cookie
) {
2057 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
2058 "binder: %d:%d BC_DEAD_BINDER_DONE %p found %p\n",
2059 proc
->pid
, thread
->pid
, cookie
, death
);
2060 if (death
== NULL
) {
2061 binder_user_error("binder: %d:%d BC_DEAD"
2062 "_BINDER_DONE %p not found\n",
2063 proc
->pid
, thread
->pid
, cookie
);
2067 list_del_init(&death
->work
.entry
);
2068 if (death
->work
.type
== BINDER_WORK_DEAD_BINDER_AND_CLEAR
) {
2069 death
->work
.type
= BINDER_WORK_CLEAR_DEATH_NOTIFICATION
;
2070 if (thread
->looper
& (BINDER_LOOPER_STATE_REGISTERED
| BINDER_LOOPER_STATE_ENTERED
)) {
2071 list_add_tail(&death
->work
.entry
, &thread
->todo
);
2073 list_add_tail(&death
->work
.entry
, &proc
->todo
);
2074 wake_up_interruptible(&proc
->wait
);
2080 pr_err("binder: %d:%d unknown command %d\n",
2081 proc
->pid
, thread
->pid
, cmd
);
2084 *consumed
= ptr
- buffer
;
2089 void binder_stat_br(struct binder_proc
*proc
, struct binder_thread
*thread
,
2092 if (_IOC_NR(cmd
) < ARRAY_SIZE(binder_stats
.br
)) {
2093 binder_stats
.br
[_IOC_NR(cmd
)]++;
2094 proc
->stats
.br
[_IOC_NR(cmd
)]++;
2095 thread
->stats
.br
[_IOC_NR(cmd
)]++;
2099 static int binder_has_proc_work(struct binder_proc
*proc
,
2100 struct binder_thread
*thread
)
2102 return !list_empty(&proc
->todo
) ||
2103 (thread
->looper
& BINDER_LOOPER_STATE_NEED_RETURN
);
2106 static int binder_has_thread_work(struct binder_thread
*thread
)
2108 return !list_empty(&thread
->todo
) || thread
->return_error
!= BR_OK
||
2109 (thread
->looper
& BINDER_LOOPER_STATE_NEED_RETURN
);
2112 static int binder_thread_read(struct binder_proc
*proc
,
2113 struct binder_thread
*thread
,
2114 void __user
*buffer
, int size
,
2115 signed long *consumed
, int non_block
)
2117 void __user
*ptr
= buffer
+ *consumed
;
2118 void __user
*end
= buffer
+ size
;
2121 int wait_for_proc_work
;
2123 if (*consumed
== 0) {
2124 if (put_user(BR_NOOP
, (uint32_t __user
*)ptr
))
2126 ptr
+= sizeof(uint32_t);
2130 wait_for_proc_work
= thread
->transaction_stack
== NULL
&&
2131 list_empty(&thread
->todo
);
2133 if (thread
->return_error
!= BR_OK
&& ptr
< end
) {
2134 if (thread
->return_error2
!= BR_OK
) {
2135 if (put_user(thread
->return_error2
, (uint32_t __user
*)ptr
))
2137 ptr
+= sizeof(uint32_t);
2140 thread
->return_error2
= BR_OK
;
2142 if (put_user(thread
->return_error
, (uint32_t __user
*)ptr
))
2144 ptr
+= sizeof(uint32_t);
2145 thread
->return_error
= BR_OK
;
2150 thread
->looper
|= BINDER_LOOPER_STATE_WAITING
;
2151 if (wait_for_proc_work
)
2152 proc
->ready_threads
++;
2153 mutex_unlock(&binder_lock
);
2154 if (wait_for_proc_work
) {
2155 if (!(thread
->looper
& (BINDER_LOOPER_STATE_REGISTERED
|
2156 BINDER_LOOPER_STATE_ENTERED
))) {
2157 binder_user_error("binder: %d:%d ERROR: Thread waiting "
2158 "for process work before calling BC_REGISTER_"
2159 "LOOPER or BC_ENTER_LOOPER (state %x)\n",
2160 proc
->pid
, thread
->pid
, thread
->looper
);
2161 wait_event_interruptible(binder_user_error_wait
,
2162 binder_stop_on_user_error
< 2);
2164 binder_set_nice(proc
->default_priority
);
2166 if (!binder_has_proc_work(proc
, thread
))
2169 ret
= wait_event_interruptible_exclusive(proc
->wait
, binder_has_proc_work(proc
, thread
));
2172 if (!binder_has_thread_work(thread
))
2175 ret
= wait_event_interruptible(thread
->wait
, binder_has_thread_work(thread
));
2177 mutex_lock(&binder_lock
);
2178 if (wait_for_proc_work
)
2179 proc
->ready_threads
--;
2180 thread
->looper
&= ~BINDER_LOOPER_STATE_WAITING
;
2187 struct binder_transaction_data tr
;
2188 struct binder_work
*w
;
2189 struct binder_transaction
*t
= NULL
;
2191 if (!list_empty(&thread
->todo
))
2192 w
= list_first_entry(&thread
->todo
, struct binder_work
, entry
);
2193 else if (!list_empty(&proc
->todo
) && wait_for_proc_work
)
2194 w
= list_first_entry(&proc
->todo
, struct binder_work
, entry
);
2196 if (ptr
- buffer
== 4 && !(thread
->looper
& BINDER_LOOPER_STATE_NEED_RETURN
)) /* no data added */
2201 if (end
- ptr
< sizeof(tr
) + 4)
2205 case BINDER_WORK_TRANSACTION
: {
2206 t
= container_of(w
, struct binder_transaction
, work
);
2208 case BINDER_WORK_TRANSACTION_COMPLETE
: {
2209 cmd
= BR_TRANSACTION_COMPLETE
;
2210 if (put_user(cmd
, (uint32_t __user
*)ptr
))
2212 ptr
+= sizeof(uint32_t);
2214 binder_stat_br(proc
, thread
, cmd
);
2215 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE
,
2216 "binder: %d:%d BR_TRANSACTION_COMPLETE\n",
2217 proc
->pid
, thread
->pid
);
2219 list_del(&w
->entry
);
2221 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE
);
2223 case BINDER_WORK_NODE
: {
2224 struct binder_node
*node
= container_of(w
, struct binder_node
, work
);
2225 uint32_t cmd
= BR_NOOP
;
2226 const char *cmd_name
;
2227 int strong
= node
->internal_strong_refs
|| node
->local_strong_refs
;
2228 int weak
= !hlist_empty(&node
->refs
) || node
->local_weak_refs
|| strong
;
2229 if (weak
&& !node
->has_weak_ref
) {
2231 cmd_name
= "BR_INCREFS";
2232 node
->has_weak_ref
= 1;
2233 node
->pending_weak_ref
= 1;
2234 node
->local_weak_refs
++;
2235 } else if (strong
&& !node
->has_strong_ref
) {
2237 cmd_name
= "BR_ACQUIRE";
2238 node
->has_strong_ref
= 1;
2239 node
->pending_strong_ref
= 1;
2240 node
->local_strong_refs
++;
2241 } else if (!strong
&& node
->has_strong_ref
) {
2243 cmd_name
= "BR_RELEASE";
2244 node
->has_strong_ref
= 0;
2245 } else if (!weak
&& node
->has_weak_ref
) {
2247 cmd_name
= "BR_DECREFS";
2248 node
->has_weak_ref
= 0;
2250 if (cmd
!= BR_NOOP
) {
2251 if (put_user(cmd
, (uint32_t __user
*)ptr
))
2253 ptr
+= sizeof(uint32_t);
2254 if (put_user(node
->ptr
, (void * __user
*)ptr
))
2256 ptr
+= sizeof(void *);
2257 if (put_user(node
->cookie
, (void * __user
*)ptr
))
2259 ptr
+= sizeof(void *);
2261 binder_stat_br(proc
, thread
, cmd
);
2262 binder_debug(BINDER_DEBUG_USER_REFS
,
2263 "binder: %d:%d %s %d u%p c%p\n",
2264 proc
->pid
, thread
->pid
, cmd_name
, node
->debug_id
, node
->ptr
, node
->cookie
);
2266 list_del_init(&w
->entry
);
2267 if (!weak
&& !strong
) {
2268 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
2269 "binder: %d:%d node %d u%p c%p deleted\n",
2270 proc
->pid
, thread
->pid
, node
->debug_id
,
2271 node
->ptr
, node
->cookie
);
2272 rb_erase(&node
->rb_node
, &proc
->nodes
);
2274 binder_stats_deleted(BINDER_STAT_NODE
);
2276 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
2277 "binder: %d:%d node %d u%p c%p state unchanged\n",
2278 proc
->pid
, thread
->pid
, node
->debug_id
, node
->ptr
,
2283 case BINDER_WORK_DEAD_BINDER
:
2284 case BINDER_WORK_DEAD_BINDER_AND_CLEAR
:
2285 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION
: {
2286 struct binder_ref_death
*death
;
2289 death
= container_of(w
, struct binder_ref_death
, work
);
2290 if (w
->type
== BINDER_WORK_CLEAR_DEATH_NOTIFICATION
)
2291 cmd
= BR_CLEAR_DEATH_NOTIFICATION_DONE
;
2293 cmd
= BR_DEAD_BINDER
;
2294 if (put_user(cmd
, (uint32_t __user
*)ptr
))
2296 ptr
+= sizeof(uint32_t);
2297 if (put_user(death
->cookie
, (void * __user
*)ptr
))
2299 ptr
+= sizeof(void *);
2300 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION
,
2301 "binder: %d:%d %s %p\n",
2302 proc
->pid
, thread
->pid
,
2303 cmd
== BR_DEAD_BINDER
?
2305 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
2308 if (w
->type
== BINDER_WORK_CLEAR_DEATH_NOTIFICATION
) {
2309 list_del(&w
->entry
);
2311 binder_stats_deleted(BINDER_STAT_DEATH
);
2313 list_move(&w
->entry
, &proc
->delivered_death
);
2314 if (cmd
== BR_DEAD_BINDER
)
2315 goto done
; /* DEAD_BINDER notifications can cause transactions */
2322 BUG_ON(t
->buffer
== NULL
);
2323 if (t
->buffer
->target_node
) {
2324 struct binder_node
*target_node
= t
->buffer
->target_node
;
2325 tr
.target
.ptr
= target_node
->ptr
;
2326 tr
.cookie
= target_node
->cookie
;
2327 t
->saved_priority
= task_nice(current
);
2328 if (t
->priority
< target_node
->min_priority
&&
2329 !(t
->flags
& TF_ONE_WAY
))
2330 binder_set_nice(t
->priority
);
2331 else if (!(t
->flags
& TF_ONE_WAY
) ||
2332 t
->saved_priority
> target_node
->min_priority
)
2333 binder_set_nice(target_node
->min_priority
);
2334 cmd
= BR_TRANSACTION
;
2336 tr
.target
.ptr
= NULL
;
2341 tr
.flags
= t
->flags
;
2342 tr
.sender_euid
= from_kuid(current_user_ns(), t
->sender_euid
);
2345 struct task_struct
*sender
= t
->from
->proc
->tsk
;
2346 tr
.sender_pid
= task_tgid_nr_ns(sender
,
2347 current
->nsproxy
->pid_ns
);
2352 tr
.data_size
= t
->buffer
->data_size
;
2353 tr
.offsets_size
= t
->buffer
->offsets_size
;
2354 tr
.data
.ptr
.buffer
= (void *)t
->buffer
->data
+
2355 proc
->user_buffer_offset
;
2356 tr
.data
.ptr
.offsets
= tr
.data
.ptr
.buffer
+
2357 ALIGN(t
->buffer
->data_size
,
2360 if (put_user(cmd
, (uint32_t __user
*)ptr
))
2362 ptr
+= sizeof(uint32_t);
2363 if (copy_to_user(ptr
, &tr
, sizeof(tr
)))
2367 binder_stat_br(proc
, thread
, cmd
);
2368 binder_debug(BINDER_DEBUG_TRANSACTION
,
2369 "binder: %d:%d %s %d %d:%d, cmd %d"
2370 "size %zd-%zd ptr %p-%p\n",
2371 proc
->pid
, thread
->pid
,
2372 (cmd
== BR_TRANSACTION
) ? "BR_TRANSACTION" :
2374 t
->debug_id
, t
->from
? t
->from
->proc
->pid
: 0,
2375 t
->from
? t
->from
->pid
: 0, cmd
,
2376 t
->buffer
->data_size
, t
->buffer
->offsets_size
,
2377 tr
.data
.ptr
.buffer
, tr
.data
.ptr
.offsets
);
2379 list_del(&t
->work
.entry
);
2380 t
->buffer
->allow_user_free
= 1;
2381 if (cmd
== BR_TRANSACTION
&& !(t
->flags
& TF_ONE_WAY
)) {
2382 t
->to_parent
= thread
->transaction_stack
;
2383 t
->to_thread
= thread
;
2384 thread
->transaction_stack
= t
;
2386 t
->buffer
->transaction
= NULL
;
2388 binder_stats_deleted(BINDER_STAT_TRANSACTION
);
2395 *consumed
= ptr
- buffer
;
2396 if (proc
->requested_threads
+ proc
->ready_threads
== 0 &&
2397 proc
->requested_threads_started
< proc
->max_threads
&&
2398 (thread
->looper
& (BINDER_LOOPER_STATE_REGISTERED
|
2399 BINDER_LOOPER_STATE_ENTERED
)) /* the user-space code fails to */
2400 /*spawn a new thread if we leave this out */) {
2401 proc
->requested_threads
++;
2402 binder_debug(BINDER_DEBUG_THREADS
,
2403 "binder: %d:%d BR_SPAWN_LOOPER\n",
2404 proc
->pid
, thread
->pid
);
2405 if (put_user(BR_SPAWN_LOOPER
, (uint32_t __user
*)buffer
))
2411 static void binder_release_work(struct list_head
*list
)
2413 struct binder_work
*w
;
2414 while (!list_empty(list
)) {
2415 w
= list_first_entry(list
, struct binder_work
, entry
);
2416 list_del_init(&w
->entry
);
2418 case BINDER_WORK_TRANSACTION
: {
2419 struct binder_transaction
*t
;
2421 t
= container_of(w
, struct binder_transaction
, work
);
2422 if (t
->buffer
->target_node
&& !(t
->flags
& TF_ONE_WAY
))
2423 binder_send_failed_reply(t
, BR_DEAD_REPLY
);
2425 case BINDER_WORK_TRANSACTION_COMPLETE
: {
2427 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE
);
2436 static struct binder_thread
*binder_get_thread(struct binder_proc
*proc
)
2438 struct binder_thread
*thread
= NULL
;
2439 struct rb_node
*parent
= NULL
;
2440 struct rb_node
**p
= &proc
->threads
.rb_node
;
2444 thread
= rb_entry(parent
, struct binder_thread
, rb_node
);
2446 if (current
->pid
< thread
->pid
)
2448 else if (current
->pid
> thread
->pid
)
2449 p
= &(*p
)->rb_right
;
2454 thread
= kzalloc(sizeof(*thread
), GFP_KERNEL
);
2457 binder_stats_created(BINDER_STAT_THREAD
);
2458 thread
->proc
= proc
;
2459 thread
->pid
= current
->pid
;
2460 init_waitqueue_head(&thread
->wait
);
2461 INIT_LIST_HEAD(&thread
->todo
);
2462 rb_link_node(&thread
->rb_node
, parent
, p
);
2463 rb_insert_color(&thread
->rb_node
, &proc
->threads
);
2464 thread
->looper
|= BINDER_LOOPER_STATE_NEED_RETURN
;
2465 thread
->return_error
= BR_OK
;
2466 thread
->return_error2
= BR_OK
;
2471 static int binder_free_thread(struct binder_proc
*proc
,
2472 struct binder_thread
*thread
)
2474 struct binder_transaction
*t
;
2475 struct binder_transaction
*send_reply
= NULL
;
2476 int active_transactions
= 0;
2478 rb_erase(&thread
->rb_node
, &proc
->threads
);
2479 t
= thread
->transaction_stack
;
2480 if (t
&& t
->to_thread
== thread
)
2483 active_transactions
++;
2484 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
2485 "binder: release %d:%d transaction %d "
2486 "%s, still active\n", proc
->pid
, thread
->pid
,
2488 (t
->to_thread
== thread
) ? "in" : "out");
2490 if (t
->to_thread
== thread
) {
2492 t
->to_thread
= NULL
;
2494 t
->buffer
->transaction
= NULL
;
2498 } else if (t
->from
== thread
) {
2505 binder_send_failed_reply(send_reply
, BR_DEAD_REPLY
);
2506 binder_release_work(&thread
->todo
);
2508 binder_stats_deleted(BINDER_STAT_THREAD
);
2509 return active_transactions
;
2512 static unsigned int binder_poll(struct file
*filp
,
2513 struct poll_table_struct
*wait
)
2515 struct binder_proc
*proc
= filp
->private_data
;
2516 struct binder_thread
*thread
= NULL
;
2517 int wait_for_proc_work
;
2519 mutex_lock(&binder_lock
);
2520 thread
= binder_get_thread(proc
);
2522 wait_for_proc_work
= thread
->transaction_stack
== NULL
&&
2523 list_empty(&thread
->todo
) && thread
->return_error
== BR_OK
;
2524 mutex_unlock(&binder_lock
);
2526 if (wait_for_proc_work
) {
2527 if (binder_has_proc_work(proc
, thread
))
2529 poll_wait(filp
, &proc
->wait
, wait
);
2530 if (binder_has_proc_work(proc
, thread
))
2533 if (binder_has_thread_work(thread
))
2535 poll_wait(filp
, &thread
->wait
, wait
);
2536 if (binder_has_thread_work(thread
))
2542 static long binder_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
2545 struct binder_proc
*proc
= filp
->private_data
;
2546 struct binder_thread
*thread
;
2547 unsigned int size
= _IOC_SIZE(cmd
);
2548 void __user
*ubuf
= (void __user
*)arg
;
2550 /*pr_info("binder_ioctl: %d:%d %x %lx\n", proc->pid, current->pid, cmd, arg);*/
2552 ret
= wait_event_interruptible(binder_user_error_wait
, binder_stop_on_user_error
< 2);
2556 mutex_lock(&binder_lock
);
2557 thread
= binder_get_thread(proc
);
2558 if (thread
== NULL
) {
2564 case BINDER_WRITE_READ
: {
2565 struct binder_write_read bwr
;
2566 if (size
!= sizeof(struct binder_write_read
)) {
2570 if (copy_from_user(&bwr
, ubuf
, sizeof(bwr
))) {
2574 binder_debug(BINDER_DEBUG_READ_WRITE
,
2575 "binder: %d:%d write %ld at %08lx, read %ld at %08lx\n",
2576 proc
->pid
, thread
->pid
, bwr
.write_size
, bwr
.write_buffer
,
2577 bwr
.read_size
, bwr
.read_buffer
);
2579 if (bwr
.write_size
> 0) {
2580 ret
= binder_thread_write(proc
, thread
, (void __user
*)bwr
.write_buffer
, bwr
.write_size
, &bwr
.write_consumed
);
2582 bwr
.read_consumed
= 0;
2583 if (copy_to_user(ubuf
, &bwr
, sizeof(bwr
)))
2588 if (bwr
.read_size
> 0) {
2589 ret
= binder_thread_read(proc
, thread
, (void __user
*)bwr
.read_buffer
, bwr
.read_size
, &bwr
.read_consumed
, filp
->f_flags
& O_NONBLOCK
);
2590 if (!list_empty(&proc
->todo
))
2591 wake_up_interruptible(&proc
->wait
);
2593 if (copy_to_user(ubuf
, &bwr
, sizeof(bwr
)))
2598 binder_debug(BINDER_DEBUG_READ_WRITE
,
2599 "binder: %d:%d wrote %ld of %ld, read return %ld of %ld\n",
2600 proc
->pid
, thread
->pid
, bwr
.write_consumed
, bwr
.write_size
,
2601 bwr
.read_consumed
, bwr
.read_size
);
2602 if (copy_to_user(ubuf
, &bwr
, sizeof(bwr
))) {
2608 case BINDER_SET_MAX_THREADS
:
2609 if (copy_from_user(&proc
->max_threads
, ubuf
, sizeof(proc
->max_threads
))) {
2614 case BINDER_SET_CONTEXT_MGR
:
2615 if (binder_context_mgr_node
!= NULL
) {
2616 pr_err("binder: BINDER_SET_CONTEXT_MGR already set\n");
2620 if (uid_valid(binder_context_mgr_uid
)) {
2621 if (!uid_eq(binder_context_mgr_uid
, current
->cred
->euid
)) {
2622 pr_err("binder: BINDER_SET_"
2623 "CONTEXT_MGR bad uid %d != %d\n",
2624 from_kuid(&init_user_ns
, current
->cred
->euid
),
2625 from_kuid(&init_user_ns
, binder_context_mgr_uid
));
2630 binder_context_mgr_uid
= current
->cred
->euid
;
2631 binder_context_mgr_node
= binder_new_node(proc
, NULL
, NULL
);
2632 if (binder_context_mgr_node
== NULL
) {
2636 binder_context_mgr_node
->local_weak_refs
++;
2637 binder_context_mgr_node
->local_strong_refs
++;
2638 binder_context_mgr_node
->has_strong_ref
= 1;
2639 binder_context_mgr_node
->has_weak_ref
= 1;
2641 case BINDER_THREAD_EXIT
:
2642 binder_debug(BINDER_DEBUG_THREADS
, "binder: %d:%d exit\n",
2643 proc
->pid
, thread
->pid
);
2644 binder_free_thread(proc
, thread
);
2647 case BINDER_VERSION
:
2648 if (size
!= sizeof(struct binder_version
)) {
2652 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION
, &((struct binder_version
*)ubuf
)->protocol_version
)) {
2664 thread
->looper
&= ~BINDER_LOOPER_STATE_NEED_RETURN
;
2665 mutex_unlock(&binder_lock
);
2666 wait_event_interruptible(binder_user_error_wait
, binder_stop_on_user_error
< 2);
2667 if (ret
&& ret
!= -ERESTARTSYS
)
2668 pr_info("binder: %d:%d ioctl %x %lx returned %d\n", proc
->pid
, current
->pid
, cmd
, arg
, ret
);
2672 static void binder_vma_open(struct vm_area_struct
*vma
)
2674 struct binder_proc
*proc
= vma
->vm_private_data
;
2675 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
2676 "binder: %d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
2677 proc
->pid
, vma
->vm_start
, vma
->vm_end
,
2678 (vma
->vm_end
- vma
->vm_start
) / SZ_1K
, vma
->vm_flags
,
2679 (unsigned long)pgprot_val(vma
->vm_page_prot
));
2682 static void binder_vma_close(struct vm_area_struct
*vma
)
2684 struct binder_proc
*proc
= vma
->vm_private_data
;
2685 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
2686 "binder: %d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
2687 proc
->pid
, vma
->vm_start
, vma
->vm_end
,
2688 (vma
->vm_end
- vma
->vm_start
) / SZ_1K
, vma
->vm_flags
,
2689 (unsigned long)pgprot_val(vma
->vm_page_prot
));
2691 proc
->vma_vm_mm
= NULL
;
2692 binder_defer_work(proc
, BINDER_DEFERRED_PUT_FILES
);
2695 static struct vm_operations_struct binder_vm_ops
= {
2696 .open
= binder_vma_open
,
2697 .close
= binder_vma_close
,
2700 static int binder_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
2703 struct vm_struct
*area
;
2704 struct binder_proc
*proc
= filp
->private_data
;
2705 const char *failure_string
;
2706 struct binder_buffer
*buffer
;
2708 if (proc
->tsk
!= current
)
2711 if ((vma
->vm_end
- vma
->vm_start
) > SZ_4M
)
2712 vma
->vm_end
= vma
->vm_start
+ SZ_4M
;
2714 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
2715 "binder_mmap: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
2716 proc
->pid
, vma
->vm_start
, vma
->vm_end
,
2717 (vma
->vm_end
- vma
->vm_start
) / SZ_1K
, vma
->vm_flags
,
2718 (unsigned long)pgprot_val(vma
->vm_page_prot
));
2720 if (vma
->vm_flags
& FORBIDDEN_MMAP_FLAGS
) {
2722 failure_string
= "bad vm_flags";
2725 vma
->vm_flags
= (vma
->vm_flags
| VM_DONTCOPY
) & ~VM_MAYWRITE
;
2727 mutex_lock(&binder_mmap_lock
);
2730 failure_string
= "already mapped";
2731 goto err_already_mapped
;
2734 area
= get_vm_area(vma
->vm_end
- vma
->vm_start
, VM_IOREMAP
);
2737 failure_string
= "get_vm_area";
2738 goto err_get_vm_area_failed
;
2740 proc
->buffer
= area
->addr
;
2741 proc
->user_buffer_offset
= vma
->vm_start
- (uintptr_t)proc
->buffer
;
2742 mutex_unlock(&binder_mmap_lock
);
2744 #ifdef CONFIG_CPU_CACHE_VIPT
2745 if (cache_is_vipt_aliasing()) {
2746 while (CACHE_COLOUR((vma
->vm_start
^ (uint32_t)proc
->buffer
))) {
2747 pr_info("binder_mmap: %d %lx-%lx maps %p bad alignment\n", proc
->pid
, vma
->vm_start
, vma
->vm_end
, proc
->buffer
);
2748 vma
->vm_start
+= PAGE_SIZE
;
2752 proc
->pages
= kzalloc(sizeof(proc
->pages
[0]) * ((vma
->vm_end
- vma
->vm_start
) / PAGE_SIZE
), GFP_KERNEL
);
2753 if (proc
->pages
== NULL
) {
2755 failure_string
= "alloc page array";
2756 goto err_alloc_pages_failed
;
2758 proc
->buffer_size
= vma
->vm_end
- vma
->vm_start
;
2760 vma
->vm_ops
= &binder_vm_ops
;
2761 vma
->vm_private_data
= proc
;
2763 if (binder_update_page_range(proc
, 1, proc
->buffer
, proc
->buffer
+ PAGE_SIZE
, vma
)) {
2765 failure_string
= "alloc small buf";
2766 goto err_alloc_small_buf_failed
;
2768 buffer
= proc
->buffer
;
2769 INIT_LIST_HEAD(&proc
->buffers
);
2770 list_add(&buffer
->entry
, &proc
->buffers
);
2772 binder_insert_free_buffer(proc
, buffer
);
2773 proc
->free_async_space
= proc
->buffer_size
/ 2;
2775 proc
->files
= get_files_struct(current
);
2777 proc
->vma_vm_mm
= vma
->vm_mm
;
2779 /*pr_info("binder_mmap: %d %lx-%lx maps %p\n",
2780 proc->pid, vma->vm_start, vma->vm_end, proc->buffer);*/
2783 err_alloc_small_buf_failed
:
2786 err_alloc_pages_failed
:
2787 mutex_lock(&binder_mmap_lock
);
2788 vfree(proc
->buffer
);
2789 proc
->buffer
= NULL
;
2790 err_get_vm_area_failed
:
2792 mutex_unlock(&binder_mmap_lock
);
2794 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
2795 proc
->pid
, vma
->vm_start
, vma
->vm_end
, failure_string
, ret
);
2799 static int binder_open(struct inode
*nodp
, struct file
*filp
)
2801 struct binder_proc
*proc
;
2803 binder_debug(BINDER_DEBUG_OPEN_CLOSE
, "binder_open: %d:%d\n",
2804 current
->group_leader
->pid
, current
->pid
);
2806 proc
= kzalloc(sizeof(*proc
), GFP_KERNEL
);
2809 get_task_struct(current
);
2810 proc
->tsk
= current
;
2811 INIT_LIST_HEAD(&proc
->todo
);
2812 init_waitqueue_head(&proc
->wait
);
2813 proc
->default_priority
= task_nice(current
);
2814 mutex_lock(&binder_lock
);
2815 binder_stats_created(BINDER_STAT_PROC
);
2816 hlist_add_head(&proc
->proc_node
, &binder_procs
);
2817 proc
->pid
= current
->group_leader
->pid
;
2818 INIT_LIST_HEAD(&proc
->delivered_death
);
2819 filp
->private_data
= proc
;
2820 mutex_unlock(&binder_lock
);
2822 if (binder_debugfs_dir_entry_proc
) {
2824 snprintf(strbuf
, sizeof(strbuf
), "%u", proc
->pid
);
2825 proc
->debugfs_entry
= debugfs_create_file(strbuf
, S_IRUGO
,
2826 binder_debugfs_dir_entry_proc
, proc
, &binder_proc_fops
);
2832 static int binder_flush(struct file
*filp
, fl_owner_t id
)
2834 struct binder_proc
*proc
= filp
->private_data
;
2836 binder_defer_work(proc
, BINDER_DEFERRED_FLUSH
);
2841 static void binder_deferred_flush(struct binder_proc
*proc
)
2845 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
)) {
2846 struct binder_thread
*thread
= rb_entry(n
, struct binder_thread
, rb_node
);
2847 thread
->looper
|= BINDER_LOOPER_STATE_NEED_RETURN
;
2848 if (thread
->looper
& BINDER_LOOPER_STATE_WAITING
) {
2849 wake_up_interruptible(&thread
->wait
);
2853 wake_up_interruptible_all(&proc
->wait
);
2855 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
2856 "binder_flush: %d woke %d threads\n", proc
->pid
,
2860 static int binder_release(struct inode
*nodp
, struct file
*filp
)
2862 struct binder_proc
*proc
= filp
->private_data
;
2863 debugfs_remove(proc
->debugfs_entry
);
2864 binder_defer_work(proc
, BINDER_DEFERRED_RELEASE
);
2869 static void binder_deferred_release(struct binder_proc
*proc
)
2871 struct hlist_node
*pos
;
2872 struct binder_transaction
*t
;
2874 int threads
, nodes
, incoming_refs
, outgoing_refs
, buffers
, active_transactions
, page_count
;
2877 BUG_ON(proc
->files
);
2879 hlist_del(&proc
->proc_node
);
2880 if (binder_context_mgr_node
&& binder_context_mgr_node
->proc
== proc
) {
2881 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
2882 "binder_release: %d context_mgr_node gone\n",
2884 binder_context_mgr_node
= NULL
;
2888 active_transactions
= 0;
2889 while ((n
= rb_first(&proc
->threads
))) {
2890 struct binder_thread
*thread
= rb_entry(n
, struct binder_thread
, rb_node
);
2892 active_transactions
+= binder_free_thread(proc
, thread
);
2896 while ((n
= rb_first(&proc
->nodes
))) {
2897 struct binder_node
*node
= rb_entry(n
, struct binder_node
, rb_node
);
2900 rb_erase(&node
->rb_node
, &proc
->nodes
);
2901 list_del_init(&node
->work
.entry
);
2902 if (hlist_empty(&node
->refs
)) {
2904 binder_stats_deleted(BINDER_STAT_NODE
);
2906 struct binder_ref
*ref
;
2910 node
->local_strong_refs
= 0;
2911 node
->local_weak_refs
= 0;
2912 hlist_add_head(&node
->dead_node
, &binder_dead_nodes
);
2914 hlist_for_each_entry(ref
, pos
, &node
->refs
, node_entry
) {
2918 if (list_empty(&ref
->death
->work
.entry
)) {
2919 ref
->death
->work
.type
= BINDER_WORK_DEAD_BINDER
;
2920 list_add_tail(&ref
->death
->work
.entry
, &ref
->proc
->todo
);
2921 wake_up_interruptible(&ref
->proc
->wait
);
2926 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
2927 "binder: node %d now dead, "
2928 "refs %d, death %d\n", node
->debug_id
,
2929 incoming_refs
, death
);
2933 while ((n
= rb_first(&proc
->refs_by_desc
))) {
2934 struct binder_ref
*ref
= rb_entry(n
, struct binder_ref
,
2937 binder_delete_ref(ref
);
2939 binder_release_work(&proc
->todo
);
2942 while ((n
= rb_first(&proc
->allocated_buffers
))) {
2943 struct binder_buffer
*buffer
= rb_entry(n
, struct binder_buffer
,
2945 t
= buffer
->transaction
;
2948 buffer
->transaction
= NULL
;
2949 pr_err("binder: release proc %d, "
2950 "transaction %d, not freed\n",
2951 proc
->pid
, t
->debug_id
);
2954 binder_free_buf(proc
, buffer
);
2958 binder_stats_deleted(BINDER_STAT_PROC
);
2963 for (i
= 0; i
< proc
->buffer_size
/ PAGE_SIZE
; i
++) {
2964 if (proc
->pages
[i
]) {
2965 void *page_addr
= proc
->buffer
+ i
* PAGE_SIZE
;
2966 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
2967 "binder_release: %d: "
2968 "page %d at %p not freed\n",
2971 unmap_kernel_range((unsigned long)page_addr
,
2973 __free_page(proc
->pages
[i
]);
2978 vfree(proc
->buffer
);
2981 put_task_struct(proc
->tsk
);
2983 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
2984 "binder_release: %d threads %d, nodes %d (ref %d), "
2985 "refs %d, active transactions %d, buffers %d, "
2987 proc
->pid
, threads
, nodes
, incoming_refs
, outgoing_refs
,
2988 active_transactions
, buffers
, page_count
);
2993 static void binder_deferred_func(struct work_struct
*work
)
2995 struct binder_proc
*proc
;
2996 struct files_struct
*files
;
3000 mutex_lock(&binder_lock
);
3001 mutex_lock(&binder_deferred_lock
);
3002 if (!hlist_empty(&binder_deferred_list
)) {
3003 proc
= hlist_entry(binder_deferred_list
.first
,
3004 struct binder_proc
, deferred_work_node
);
3005 hlist_del_init(&proc
->deferred_work_node
);
3006 defer
= proc
->deferred_work
;
3007 proc
->deferred_work
= 0;
3012 mutex_unlock(&binder_deferred_lock
);
3015 if (defer
& BINDER_DEFERRED_PUT_FILES
) {
3016 files
= proc
->files
;
3021 if (defer
& BINDER_DEFERRED_FLUSH
)
3022 binder_deferred_flush(proc
);
3024 if (defer
& BINDER_DEFERRED_RELEASE
)
3025 binder_deferred_release(proc
); /* frees proc */
3027 mutex_unlock(&binder_lock
);
3029 put_files_struct(files
);
3032 static DECLARE_WORK(binder_deferred_work
, binder_deferred_func
);
3035 binder_defer_work(struct binder_proc
*proc
, enum binder_deferred_state defer
)
3037 mutex_lock(&binder_deferred_lock
);
3038 proc
->deferred_work
|= defer
;
3039 if (hlist_unhashed(&proc
->deferred_work_node
)) {
3040 hlist_add_head(&proc
->deferred_work_node
,
3041 &binder_deferred_list
);
3042 queue_work(binder_deferred_workqueue
, &binder_deferred_work
);
3044 mutex_unlock(&binder_deferred_lock
);
3047 static void print_binder_transaction(struct seq_file
*m
, const char *prefix
,
3048 struct binder_transaction
*t
)
3051 "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %ld r%d",
3052 prefix
, t
->debug_id
, t
,
3053 t
->from
? t
->from
->proc
->pid
: 0,
3054 t
->from
? t
->from
->pid
: 0,
3055 t
->to_proc
? t
->to_proc
->pid
: 0,
3056 t
->to_thread
? t
->to_thread
->pid
: 0,
3057 t
->code
, t
->flags
, t
->priority
, t
->need_reply
);
3058 if (t
->buffer
== NULL
) {
3059 seq_puts(m
, " buffer free\n");
3062 if (t
->buffer
->target_node
)
3063 seq_printf(m
, " node %d",
3064 t
->buffer
->target_node
->debug_id
);
3065 seq_printf(m
, " size %zd:%zd data %p\n",
3066 t
->buffer
->data_size
, t
->buffer
->offsets_size
,
3070 static void print_binder_buffer(struct seq_file
*m
, const char *prefix
,
3071 struct binder_buffer
*buffer
)
3073 seq_printf(m
, "%s %d: %p size %zd:%zd %s\n",
3074 prefix
, buffer
->debug_id
, buffer
->data
,
3075 buffer
->data_size
, buffer
->offsets_size
,
3076 buffer
->transaction
? "active" : "delivered");
3079 static void print_binder_work(struct seq_file
*m
, const char *prefix
,
3080 const char *transaction_prefix
,
3081 struct binder_work
*w
)
3083 struct binder_node
*node
;
3084 struct binder_transaction
*t
;
3087 case BINDER_WORK_TRANSACTION
:
3088 t
= container_of(w
, struct binder_transaction
, work
);
3089 print_binder_transaction(m
, transaction_prefix
, t
);
3091 case BINDER_WORK_TRANSACTION_COMPLETE
:
3092 seq_printf(m
, "%stransaction complete\n", prefix
);
3094 case BINDER_WORK_NODE
:
3095 node
= container_of(w
, struct binder_node
, work
);
3096 seq_printf(m
, "%snode work %d: u%p c%p\n",
3097 prefix
, node
->debug_id
, node
->ptr
, node
->cookie
);
3099 case BINDER_WORK_DEAD_BINDER
:
3100 seq_printf(m
, "%shas dead binder\n", prefix
);
3102 case BINDER_WORK_DEAD_BINDER_AND_CLEAR
:
3103 seq_printf(m
, "%shas cleared dead binder\n", prefix
);
3105 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION
:
3106 seq_printf(m
, "%shas cleared death notification\n", prefix
);
3109 seq_printf(m
, "%sunknown work: type %d\n", prefix
, w
->type
);
3114 static void print_binder_thread(struct seq_file
*m
,
3115 struct binder_thread
*thread
,
3118 struct binder_transaction
*t
;
3119 struct binder_work
*w
;
3120 size_t start_pos
= m
->count
;
3123 seq_printf(m
, " thread %d: l %02x\n", thread
->pid
, thread
->looper
);
3124 header_pos
= m
->count
;
3125 t
= thread
->transaction_stack
;
3127 if (t
->from
== thread
) {
3128 print_binder_transaction(m
,
3129 " outgoing transaction", t
);
3131 } else if (t
->to_thread
== thread
) {
3132 print_binder_transaction(m
,
3133 " incoming transaction", t
);
3136 print_binder_transaction(m
, " bad transaction", t
);
3140 list_for_each_entry(w
, &thread
->todo
, entry
) {
3141 print_binder_work(m
, " ", " pending transaction", w
);
3143 if (!print_always
&& m
->count
== header_pos
)
3144 m
->count
= start_pos
;
3147 static void print_binder_node(struct seq_file
*m
, struct binder_node
*node
)
3149 struct binder_ref
*ref
;
3150 struct hlist_node
*pos
;
3151 struct binder_work
*w
;
3155 hlist_for_each_entry(ref
, pos
, &node
->refs
, node_entry
)
3158 seq_printf(m
, " node %d: u%p c%p hs %d hw %d ls %d lw %d is %d iw %d",
3159 node
->debug_id
, node
->ptr
, node
->cookie
,
3160 node
->has_strong_ref
, node
->has_weak_ref
,
3161 node
->local_strong_refs
, node
->local_weak_refs
,
3162 node
->internal_strong_refs
, count
);
3164 seq_puts(m
, " proc");
3165 hlist_for_each_entry(ref
, pos
, &node
->refs
, node_entry
)
3166 seq_printf(m
, " %d", ref
->proc
->pid
);
3169 list_for_each_entry(w
, &node
->async_todo
, entry
)
3170 print_binder_work(m
, " ",
3171 " pending async transaction", w
);
3174 static void print_binder_ref(struct seq_file
*m
, struct binder_ref
*ref
)
3176 seq_printf(m
, " ref %d: desc %d %snode %d s %d w %d d %p\n",
3177 ref
->debug_id
, ref
->desc
, ref
->node
->proc
? "" : "dead ",
3178 ref
->node
->debug_id
, ref
->strong
, ref
->weak
, ref
->death
);
3181 static void print_binder_proc(struct seq_file
*m
,
3182 struct binder_proc
*proc
, int print_all
)
3184 struct binder_work
*w
;
3186 size_t start_pos
= m
->count
;
3189 seq_printf(m
, "proc %d\n", proc
->pid
);
3190 header_pos
= m
->count
;
3192 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
))
3193 print_binder_thread(m
, rb_entry(n
, struct binder_thread
,
3194 rb_node
), print_all
);
3195 for (n
= rb_first(&proc
->nodes
); n
!= NULL
; n
= rb_next(n
)) {
3196 struct binder_node
*node
= rb_entry(n
, struct binder_node
,
3198 if (print_all
|| node
->has_async_transaction
)
3199 print_binder_node(m
, node
);
3202 for (n
= rb_first(&proc
->refs_by_desc
);
3205 print_binder_ref(m
, rb_entry(n
, struct binder_ref
,
3208 for (n
= rb_first(&proc
->allocated_buffers
); n
!= NULL
; n
= rb_next(n
))
3209 print_binder_buffer(m
, " buffer",
3210 rb_entry(n
, struct binder_buffer
, rb_node
));
3211 list_for_each_entry(w
, &proc
->todo
, entry
)
3212 print_binder_work(m
, " ", " pending transaction", w
);
3213 list_for_each_entry(w
, &proc
->delivered_death
, entry
) {
3214 seq_puts(m
, " has delivered dead binder\n");
3217 if (!print_all
&& m
->count
== header_pos
)
3218 m
->count
= start_pos
;
3221 static const char *binder_return_strings
[] = {
3226 "BR_ACQUIRE_RESULT",
3228 "BR_TRANSACTION_COMPLETE",
3233 "BR_ATTEMPT_ACQUIRE",
3238 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
3242 static const char *binder_command_strings
[] = {
3245 "BC_ACQUIRE_RESULT",
3253 "BC_ATTEMPT_ACQUIRE",
3254 "BC_REGISTER_LOOPER",
3257 "BC_REQUEST_DEATH_NOTIFICATION",
3258 "BC_CLEAR_DEATH_NOTIFICATION",
3259 "BC_DEAD_BINDER_DONE"
3262 static const char *binder_objstat_strings
[] = {
3269 "transaction_complete"
3272 static void print_binder_stats(struct seq_file
*m
, const char *prefix
,
3273 struct binder_stats
*stats
)
3277 BUILD_BUG_ON(ARRAY_SIZE(stats
->bc
) !=
3278 ARRAY_SIZE(binder_command_strings
));
3279 for (i
= 0; i
< ARRAY_SIZE(stats
->bc
); i
++) {
3281 seq_printf(m
, "%s%s: %d\n", prefix
,
3282 binder_command_strings
[i
], stats
->bc
[i
]);
3285 BUILD_BUG_ON(ARRAY_SIZE(stats
->br
) !=
3286 ARRAY_SIZE(binder_return_strings
));
3287 for (i
= 0; i
< ARRAY_SIZE(stats
->br
); i
++) {
3289 seq_printf(m
, "%s%s: %d\n", prefix
,
3290 binder_return_strings
[i
], stats
->br
[i
]);
3293 BUILD_BUG_ON(ARRAY_SIZE(stats
->obj_created
) !=
3294 ARRAY_SIZE(binder_objstat_strings
));
3295 BUILD_BUG_ON(ARRAY_SIZE(stats
->obj_created
) !=
3296 ARRAY_SIZE(stats
->obj_deleted
));
3297 for (i
= 0; i
< ARRAY_SIZE(stats
->obj_created
); i
++) {
3298 if (stats
->obj_created
[i
] || stats
->obj_deleted
[i
])
3299 seq_printf(m
, "%s%s: active %d total %d\n", prefix
,
3300 binder_objstat_strings
[i
],
3301 stats
->obj_created
[i
] - stats
->obj_deleted
[i
],
3302 stats
->obj_created
[i
]);
3306 static void print_binder_proc_stats(struct seq_file
*m
,
3307 struct binder_proc
*proc
)
3309 struct binder_work
*w
;
3311 int count
, strong
, weak
;
3313 seq_printf(m
, "proc %d\n", proc
->pid
);
3315 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
))
3317 seq_printf(m
, " threads: %d\n", count
);
3318 seq_printf(m
, " requested threads: %d+%d/%d\n"
3319 " ready threads %d\n"
3320 " free async space %zd\n", proc
->requested_threads
,
3321 proc
->requested_threads_started
, proc
->max_threads
,
3322 proc
->ready_threads
, proc
->free_async_space
);
3324 for (n
= rb_first(&proc
->nodes
); n
!= NULL
; n
= rb_next(n
))
3326 seq_printf(m
, " nodes: %d\n", count
);
3330 for (n
= rb_first(&proc
->refs_by_desc
); n
!= NULL
; n
= rb_next(n
)) {
3331 struct binder_ref
*ref
= rb_entry(n
, struct binder_ref
,
3334 strong
+= ref
->strong
;
3337 seq_printf(m
, " refs: %d s %d w %d\n", count
, strong
, weak
);
3340 for (n
= rb_first(&proc
->allocated_buffers
); n
!= NULL
; n
= rb_next(n
))
3342 seq_printf(m
, " buffers: %d\n", count
);
3345 list_for_each_entry(w
, &proc
->todo
, entry
) {
3347 case BINDER_WORK_TRANSACTION
:
3354 seq_printf(m
, " pending transactions: %d\n", count
);
3356 print_binder_stats(m
, " ", &proc
->stats
);
3360 static int binder_state_show(struct seq_file
*m
, void *unused
)
3362 struct binder_proc
*proc
;
3363 struct hlist_node
*pos
;
3364 struct binder_node
*node
;
3365 int do_lock
= !binder_debug_no_lock
;
3368 mutex_lock(&binder_lock
);
3370 seq_puts(m
, "binder state:\n");
3372 if (!hlist_empty(&binder_dead_nodes
))
3373 seq_puts(m
, "dead nodes:\n");
3374 hlist_for_each_entry(node
, pos
, &binder_dead_nodes
, dead_node
)
3375 print_binder_node(m
, node
);
3377 hlist_for_each_entry(proc
, pos
, &binder_procs
, proc_node
)
3378 print_binder_proc(m
, proc
, 1);
3380 mutex_unlock(&binder_lock
);
3384 static int binder_stats_show(struct seq_file
*m
, void *unused
)
3386 struct binder_proc
*proc
;
3387 struct hlist_node
*pos
;
3388 int do_lock
= !binder_debug_no_lock
;
3391 mutex_lock(&binder_lock
);
3393 seq_puts(m
, "binder stats:\n");
3395 print_binder_stats(m
, "", &binder_stats
);
3397 hlist_for_each_entry(proc
, pos
, &binder_procs
, proc_node
)
3398 print_binder_proc_stats(m
, proc
);
3400 mutex_unlock(&binder_lock
);
3404 static int binder_transactions_show(struct seq_file
*m
, void *unused
)
3406 struct binder_proc
*proc
;
3407 struct hlist_node
*pos
;
3408 int do_lock
= !binder_debug_no_lock
;
3411 mutex_lock(&binder_lock
);
3413 seq_puts(m
, "binder transactions:\n");
3414 hlist_for_each_entry(proc
, pos
, &binder_procs
, proc_node
)
3415 print_binder_proc(m
, proc
, 0);
3417 mutex_unlock(&binder_lock
);
3421 static int binder_proc_show(struct seq_file
*m
, void *unused
)
3423 struct binder_proc
*proc
= m
->private;
3424 int do_lock
= !binder_debug_no_lock
;
3427 mutex_lock(&binder_lock
);
3428 seq_puts(m
, "binder proc state:\n");
3429 print_binder_proc(m
, proc
, 1);
3431 mutex_unlock(&binder_lock
);
3435 static void print_binder_transaction_log_entry(struct seq_file
*m
,
3436 struct binder_transaction_log_entry
*e
)
3439 "%d: %s from %d:%d to %d:%d node %d handle %d size %d:%d\n",
3440 e
->debug_id
, (e
->call_type
== 2) ? "reply" :
3441 ((e
->call_type
== 1) ? "async" : "call "), e
->from_proc
,
3442 e
->from_thread
, e
->to_proc
, e
->to_thread
, e
->to_node
,
3443 e
->target_handle
, e
->data_size
, e
->offsets_size
);
3446 static int binder_transaction_log_show(struct seq_file
*m
, void *unused
)
3448 struct binder_transaction_log
*log
= m
->private;
3452 for (i
= log
->next
; i
< ARRAY_SIZE(log
->entry
); i
++)
3453 print_binder_transaction_log_entry(m
, &log
->entry
[i
]);
3455 for (i
= 0; i
< log
->next
; i
++)
3456 print_binder_transaction_log_entry(m
, &log
->entry
[i
]);
3460 static const struct file_operations binder_fops
= {
3461 .owner
= THIS_MODULE
,
3462 .poll
= binder_poll
,
3463 .unlocked_ioctl
= binder_ioctl
,
3464 .mmap
= binder_mmap
,
3465 .open
= binder_open
,
3466 .flush
= binder_flush
,
3467 .release
= binder_release
,
3470 static struct miscdevice binder_miscdev
= {
3471 .minor
= MISC_DYNAMIC_MINOR
,
3473 .fops
= &binder_fops
3476 BINDER_DEBUG_ENTRY(state
);
3477 BINDER_DEBUG_ENTRY(stats
);
3478 BINDER_DEBUG_ENTRY(transactions
);
3479 BINDER_DEBUG_ENTRY(transaction_log
);
3481 static int __init
binder_init(void)
3485 binder_deferred_workqueue
= create_singlethread_workqueue("binder");
3486 if (!binder_deferred_workqueue
)
3489 binder_debugfs_dir_entry_root
= debugfs_create_dir("binder", NULL
);
3490 if (binder_debugfs_dir_entry_root
)
3491 binder_debugfs_dir_entry_proc
= debugfs_create_dir("proc",
3492 binder_debugfs_dir_entry_root
);
3493 ret
= misc_register(&binder_miscdev
);
3494 if (binder_debugfs_dir_entry_root
) {
3495 debugfs_create_file("state",
3497 binder_debugfs_dir_entry_root
,
3499 &binder_state_fops
);
3500 debugfs_create_file("stats",
3502 binder_debugfs_dir_entry_root
,
3504 &binder_stats_fops
);
3505 debugfs_create_file("transactions",
3507 binder_debugfs_dir_entry_root
,
3509 &binder_transactions_fops
);
3510 debugfs_create_file("transaction_log",
3512 binder_debugfs_dir_entry_root
,
3513 &binder_transaction_log
,
3514 &binder_transaction_log_fops
);
3515 debugfs_create_file("failed_transaction_log",
3517 binder_debugfs_dir_entry_root
,
3518 &binder_transaction_log_failed
,
3519 &binder_transaction_log_fops
);
3524 device_initcall(binder_init
);
3526 MODULE_LICENSE("GPL v2");