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
);
42 static HLIST_HEAD(binder_procs
);
43 static HLIST_HEAD(binder_deferred_list
);
44 static HLIST_HEAD(binder_dead_nodes
);
46 static struct dentry
*binder_debugfs_dir_entry_root
;
47 static struct dentry
*binder_debugfs_dir_entry_proc
;
48 static struct binder_node
*binder_context_mgr_node
;
49 static uid_t binder_context_mgr_uid
= -1;
50 static int binder_last_id
;
51 static struct workqueue_struct
*binder_deferred_workqueue
;
53 #define BINDER_DEBUG_ENTRY(name) \
54 static int binder_##name##_open(struct inode *inode, struct file *file) \
56 return single_open(file, binder_##name##_show, inode->i_private); \
59 static const struct file_operations binder_##name##_fops = { \
60 .owner = THIS_MODULE, \
61 .open = binder_##name##_open, \
63 .llseek = seq_lseek, \
64 .release = single_release, \
67 static int binder_proc_show(struct seq_file
*m
, void *unused
);
68 BINDER_DEBUG_ENTRY(proc
);
70 /* This is only defined in include/asm-arm/sizes.h */
76 #define SZ_4M 0x400000
79 #define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
81 #define BINDER_SMALL_BUF_SIZE (PAGE_SIZE * 64)
84 BINDER_DEBUG_USER_ERROR
= 1U << 0,
85 BINDER_DEBUG_FAILED_TRANSACTION
= 1U << 1,
86 BINDER_DEBUG_DEAD_TRANSACTION
= 1U << 2,
87 BINDER_DEBUG_OPEN_CLOSE
= 1U << 3,
88 BINDER_DEBUG_DEAD_BINDER
= 1U << 4,
89 BINDER_DEBUG_DEATH_NOTIFICATION
= 1U << 5,
90 BINDER_DEBUG_READ_WRITE
= 1U << 6,
91 BINDER_DEBUG_USER_REFS
= 1U << 7,
92 BINDER_DEBUG_THREADS
= 1U << 8,
93 BINDER_DEBUG_TRANSACTION
= 1U << 9,
94 BINDER_DEBUG_TRANSACTION_COMPLETE
= 1U << 10,
95 BINDER_DEBUG_FREE_BUFFER
= 1U << 11,
96 BINDER_DEBUG_INTERNAL_REFS
= 1U << 12,
97 BINDER_DEBUG_BUFFER_ALLOC
= 1U << 13,
98 BINDER_DEBUG_PRIORITY_CAP
= 1U << 14,
99 BINDER_DEBUG_BUFFER_ALLOC_ASYNC
= 1U << 15,
101 static uint32_t binder_debug_mask
= BINDER_DEBUG_USER_ERROR
|
102 BINDER_DEBUG_FAILED_TRANSACTION
| BINDER_DEBUG_DEAD_TRANSACTION
;
103 module_param_named(debug_mask
, binder_debug_mask
, uint
, S_IWUSR
| S_IRUGO
);
105 static int binder_debug_no_lock
;
106 module_param_named(proc_no_lock
, binder_debug_no_lock
, bool, S_IWUSR
| S_IRUGO
);
108 static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait
);
109 static int binder_stop_on_user_error
;
111 static int binder_set_stop_on_user_error(const char *val
,
112 struct kernel_param
*kp
)
115 ret
= param_set_int(val
, kp
);
116 if (binder_stop_on_user_error
< 2)
117 wake_up(&binder_user_error_wait
);
120 module_param_call(stop_on_user_error
, binder_set_stop_on_user_error
,
121 param_get_int
, &binder_stop_on_user_error
, S_IWUSR
| S_IRUGO
);
123 #define binder_debug(mask, x...) \
125 if (binder_debug_mask & mask) \
126 printk(KERN_INFO x); \
129 #define binder_user_error(x...) \
131 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
132 printk(KERN_INFO x); \
133 if (binder_stop_on_user_error) \
134 binder_stop_on_user_error = 2; \
137 enum binder_stat_types
{
143 BINDER_STAT_TRANSACTION
,
144 BINDER_STAT_TRANSACTION_COMPLETE
,
148 struct binder_stats
{
149 int br
[_IOC_NR(BR_FAILED_REPLY
) + 1];
150 int bc
[_IOC_NR(BC_DEAD_BINDER_DONE
) + 1];
151 int obj_created
[BINDER_STAT_COUNT
];
152 int obj_deleted
[BINDER_STAT_COUNT
];
155 static struct binder_stats binder_stats
;
157 static inline void binder_stats_deleted(enum binder_stat_types type
)
159 binder_stats
.obj_deleted
[type
]++;
162 static inline void binder_stats_created(enum binder_stat_types type
)
164 binder_stats
.obj_created
[type
]++;
167 struct binder_transaction_log_entry
{
179 struct binder_transaction_log
{
182 struct binder_transaction_log_entry entry
[32];
184 static struct binder_transaction_log binder_transaction_log
;
185 static struct binder_transaction_log binder_transaction_log_failed
;
187 static struct binder_transaction_log_entry
*binder_transaction_log_add(
188 struct binder_transaction_log
*log
)
190 struct binder_transaction_log_entry
*e
;
191 e
= &log
->entry
[log
->next
];
192 memset(e
, 0, sizeof(*e
));
194 if (log
->next
== ARRAY_SIZE(log
->entry
)) {
202 struct list_head entry
;
204 BINDER_WORK_TRANSACTION
= 1,
205 BINDER_WORK_TRANSACTION_COMPLETE
,
207 BINDER_WORK_DEAD_BINDER
,
208 BINDER_WORK_DEAD_BINDER_AND_CLEAR
,
209 BINDER_WORK_CLEAR_DEATH_NOTIFICATION
,
215 struct binder_work work
;
217 struct rb_node rb_node
;
218 struct hlist_node dead_node
;
220 struct binder_proc
*proc
;
221 struct hlist_head refs
;
222 int internal_strong_refs
;
224 int local_strong_refs
;
227 unsigned has_strong_ref
:1;
228 unsigned pending_strong_ref
:1;
229 unsigned has_weak_ref
:1;
230 unsigned pending_weak_ref
:1;
231 unsigned has_async_transaction
:1;
232 unsigned accept_fds
:1;
233 unsigned min_priority
:8;
234 struct list_head async_todo
;
237 struct binder_ref_death
{
238 struct binder_work work
;
243 /* Lookups needed: */
244 /* node + proc => ref (transaction) */
245 /* desc + proc => ref (transaction, inc/dec ref) */
246 /* node => refs + procs (proc exit) */
248 struct rb_node rb_node_desc
;
249 struct rb_node rb_node_node
;
250 struct hlist_node node_entry
;
251 struct binder_proc
*proc
;
252 struct binder_node
*node
;
256 struct binder_ref_death
*death
;
259 struct binder_buffer
{
260 struct list_head entry
; /* free and allocated entries by addesss */
261 struct rb_node rb_node
; /* free entry by size or allocated entry */
264 unsigned allow_user_free
:1;
265 unsigned async_transaction
:1;
266 unsigned debug_id
:29;
268 struct binder_transaction
*transaction
;
270 struct binder_node
*target_node
;
276 enum binder_deferred_state
{
277 BINDER_DEFERRED_PUT_FILES
= 0x01,
278 BINDER_DEFERRED_FLUSH
= 0x02,
279 BINDER_DEFERRED_RELEASE
= 0x04,
283 struct hlist_node proc_node
;
284 struct rb_root threads
;
285 struct rb_root nodes
;
286 struct rb_root refs_by_desc
;
287 struct rb_root refs_by_node
;
289 struct vm_area_struct
*vma
;
290 struct task_struct
*tsk
;
291 struct files_struct
*files
;
292 struct hlist_node deferred_work_node
;
295 ptrdiff_t user_buffer_offset
;
297 struct list_head buffers
;
298 struct rb_root free_buffers
;
299 struct rb_root allocated_buffers
;
300 size_t free_async_space
;
304 uint32_t buffer_free
;
305 struct list_head todo
;
306 wait_queue_head_t wait
;
307 struct binder_stats stats
;
308 struct list_head delivered_death
;
310 int requested_threads
;
311 int requested_threads_started
;
313 long default_priority
;
314 struct dentry
*debugfs_entry
;
318 BINDER_LOOPER_STATE_REGISTERED
= 0x01,
319 BINDER_LOOPER_STATE_ENTERED
= 0x02,
320 BINDER_LOOPER_STATE_EXITED
= 0x04,
321 BINDER_LOOPER_STATE_INVALID
= 0x08,
322 BINDER_LOOPER_STATE_WAITING
= 0x10,
323 BINDER_LOOPER_STATE_NEED_RETURN
= 0x20
326 struct binder_thread
{
327 struct binder_proc
*proc
;
328 struct rb_node rb_node
;
331 struct binder_transaction
*transaction_stack
;
332 struct list_head todo
;
333 uint32_t return_error
; /* Write failed, return error code in read buf */
334 uint32_t return_error2
; /* Write failed, return error code in read */
335 /* buffer. Used when sending a reply to a dead process that */
336 /* we are also waiting on */
337 wait_queue_head_t wait
;
338 struct binder_stats stats
;
341 struct binder_transaction
{
343 struct binder_work work
;
344 struct binder_thread
*from
;
345 struct binder_transaction
*from_parent
;
346 struct binder_proc
*to_proc
;
347 struct binder_thread
*to_thread
;
348 struct binder_transaction
*to_parent
;
349 unsigned need_reply
:1;
350 /* unsigned is_dead:1; */ /* not used at the moment */
352 struct binder_buffer
*buffer
;
361 binder_defer_work(struct binder_proc
*proc
, enum binder_deferred_state defer
);
364 * copied from get_unused_fd_flags
366 int task_get_unused_fd_flags(struct binder_proc
*proc
, int flags
)
368 struct files_struct
*files
= proc
->files
;
371 unsigned long rlim_cur
;
378 spin_lock(&files
->file_lock
);
381 fdt
= files_fdtable(files
);
382 fd
= find_next_zero_bit(fdt
->open_fds
->fds_bits
, fdt
->max_fds
,
386 * N.B. For clone tasks sharing a files structure, this test
387 * will limit the total number of files that can be opened.
390 if (lock_task_sighand(proc
->tsk
, &irqs
)) {
391 rlim_cur
= proc
->tsk
->signal
->rlim
[RLIMIT_NOFILE
].rlim_cur
;
392 unlock_task_sighand(proc
->tsk
, &irqs
);
397 /* Do we need to expand the fd array or fd set? */
398 error
= expand_files(files
, fd
);
404 * If we needed to expand the fs array we
405 * might have blocked - try again.
411 FD_SET(fd
, fdt
->open_fds
);
412 if (flags
& O_CLOEXEC
)
413 FD_SET(fd
, fdt
->close_on_exec
);
415 FD_CLR(fd
, fdt
->close_on_exec
);
416 files
->next_fd
= fd
+ 1;
419 if (fdt
->fd
[fd
] != NULL
) {
420 printk(KERN_WARNING
"get_unused_fd: slot %d not NULL!\n", fd
);
427 spin_unlock(&files
->file_lock
);
432 * copied from fd_install
434 static void task_fd_install(
435 struct binder_proc
*proc
, unsigned int fd
, struct file
*file
)
437 struct files_struct
*files
= proc
->files
;
443 spin_lock(&files
->file_lock
);
444 fdt
= files_fdtable(files
);
445 BUG_ON(fdt
->fd
[fd
] != NULL
);
446 rcu_assign_pointer(fdt
->fd
[fd
], file
);
447 spin_unlock(&files
->file_lock
);
451 * copied from __put_unused_fd in open.c
453 static void __put_unused_fd(struct files_struct
*files
, unsigned int fd
)
455 struct fdtable
*fdt
= files_fdtable(files
);
456 __FD_CLR(fd
, fdt
->open_fds
);
457 if (fd
< files
->next_fd
)
462 * copied from sys_close
464 static long task_close_fd(struct binder_proc
*proc
, unsigned int fd
)
467 struct files_struct
*files
= proc
->files
;
474 spin_lock(&files
->file_lock
);
475 fdt
= files_fdtable(files
);
476 if (fd
>= fdt
->max_fds
)
481 rcu_assign_pointer(fdt
->fd
[fd
], NULL
);
482 FD_CLR(fd
, fdt
->close_on_exec
);
483 __put_unused_fd(files
, fd
);
484 spin_unlock(&files
->file_lock
);
485 retval
= filp_close(filp
, files
);
487 /* can't restart close syscall because file table entry was cleared */
488 if (unlikely(retval
== -ERESTARTSYS
||
489 retval
== -ERESTARTNOINTR
||
490 retval
== -ERESTARTNOHAND
||
491 retval
== -ERESTART_RESTARTBLOCK
))
497 spin_unlock(&files
->file_lock
);
501 static void binder_set_nice(long nice
)
504 if (can_nice(current
, nice
)) {
505 set_user_nice(current
, nice
);
508 min_nice
= 20 - current
->signal
->rlim
[RLIMIT_NICE
].rlim_cur
;
509 binder_debug(BINDER_DEBUG_PRIORITY_CAP
,
510 "binder: %d: nice value %ld not allowed use "
511 "%ld instead\n", current
->pid
, nice
, min_nice
);
512 set_user_nice(current
, min_nice
);
515 binder_user_error("binder: %d RLIMIT_NICE not set\n", current
->pid
);
518 static size_t binder_buffer_size(struct binder_proc
*proc
,
519 struct binder_buffer
*buffer
)
521 if (list_is_last(&buffer
->entry
, &proc
->buffers
))
522 return proc
->buffer
+ proc
->buffer_size
- (void *)buffer
->data
;
524 return (size_t)list_entry(buffer
->entry
.next
,
525 struct binder_buffer
, entry
) - (size_t)buffer
->data
;
528 static void binder_insert_free_buffer(struct binder_proc
*proc
,
529 struct binder_buffer
*new_buffer
)
531 struct rb_node
**p
= &proc
->free_buffers
.rb_node
;
532 struct rb_node
*parent
= NULL
;
533 struct binder_buffer
*buffer
;
535 size_t new_buffer_size
;
537 BUG_ON(!new_buffer
->free
);
539 new_buffer_size
= binder_buffer_size(proc
, new_buffer
);
541 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
542 "binder: %d: add free buffer, size %zd, "
543 "at %p\n", proc
->pid
, new_buffer_size
, new_buffer
);
547 buffer
= rb_entry(parent
, struct binder_buffer
, rb_node
);
548 BUG_ON(!buffer
->free
);
550 buffer_size
= binder_buffer_size(proc
, buffer
);
552 if (new_buffer_size
< buffer_size
)
553 p
= &parent
->rb_left
;
555 p
= &parent
->rb_right
;
557 rb_link_node(&new_buffer
->rb_node
, parent
, p
);
558 rb_insert_color(&new_buffer
->rb_node
, &proc
->free_buffers
);
561 static void binder_insert_allocated_buffer(struct binder_proc
*proc
,
562 struct binder_buffer
*new_buffer
)
564 struct rb_node
**p
= &proc
->allocated_buffers
.rb_node
;
565 struct rb_node
*parent
= NULL
;
566 struct binder_buffer
*buffer
;
568 BUG_ON(new_buffer
->free
);
572 buffer
= rb_entry(parent
, struct binder_buffer
, rb_node
);
573 BUG_ON(buffer
->free
);
575 if (new_buffer
< buffer
)
576 p
= &parent
->rb_left
;
577 else if (new_buffer
> buffer
)
578 p
= &parent
->rb_right
;
582 rb_link_node(&new_buffer
->rb_node
, parent
, p
);
583 rb_insert_color(&new_buffer
->rb_node
, &proc
->allocated_buffers
);
586 static struct binder_buffer
*binder_buffer_lookup(struct binder_proc
*proc
,
587 void __user
*user_ptr
)
589 struct rb_node
*n
= proc
->allocated_buffers
.rb_node
;
590 struct binder_buffer
*buffer
;
591 struct binder_buffer
*kern_ptr
;
593 kern_ptr
= user_ptr
- proc
->user_buffer_offset
594 - offsetof(struct binder_buffer
, data
);
597 buffer
= rb_entry(n
, struct binder_buffer
, rb_node
);
598 BUG_ON(buffer
->free
);
600 if (kern_ptr
< buffer
)
602 else if (kern_ptr
> buffer
)
610 static int binder_update_page_range(struct binder_proc
*proc
, int allocate
,
611 void *start
, void *end
,
612 struct vm_area_struct
*vma
)
615 unsigned long user_page_addr
;
616 struct vm_struct tmp_area
;
618 struct mm_struct
*mm
;
620 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
621 "binder: %d: %s pages %p-%p\n", proc
->pid
,
622 allocate
? "allocate" : "free", start
, end
);
630 mm
= get_task_mm(proc
->tsk
);
633 down_write(&mm
->mmap_sem
);
641 printk(KERN_ERR
"binder: %d: binder_alloc_buf failed to "
642 "map pages in userspace, no vma\n", proc
->pid
);
646 for (page_addr
= start
; page_addr
< end
; page_addr
+= PAGE_SIZE
) {
648 struct page
**page_array_ptr
;
649 page
= &proc
->pages
[(page_addr
- proc
->buffer
) / PAGE_SIZE
];
652 *page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
654 printk(KERN_ERR
"binder: %d: binder_alloc_buf failed "
655 "for page at %p\n", proc
->pid
, page_addr
);
656 goto err_alloc_page_failed
;
658 tmp_area
.addr
= page_addr
;
659 tmp_area
.size
= PAGE_SIZE
+ PAGE_SIZE
/* guard page? */;
660 page_array_ptr
= page
;
661 ret
= map_vm_area(&tmp_area
, PAGE_KERNEL
, &page_array_ptr
);
663 printk(KERN_ERR
"binder: %d: binder_alloc_buf failed "
664 "to map page at %p in kernel\n",
665 proc
->pid
, page_addr
);
666 goto err_map_kernel_failed
;
669 (uintptr_t)page_addr
+ proc
->user_buffer_offset
;
670 ret
= vm_insert_page(vma
, user_page_addr
, page
[0]);
672 printk(KERN_ERR
"binder: %d: binder_alloc_buf failed "
673 "to map page at %lx in userspace\n",
674 proc
->pid
, user_page_addr
);
675 goto err_vm_insert_page_failed
;
677 /* vm_insert_page does not seem to increment the refcount */
680 up_write(&mm
->mmap_sem
);
686 for (page_addr
= end
- PAGE_SIZE
; page_addr
>= start
;
687 page_addr
-= PAGE_SIZE
) {
688 page
= &proc
->pages
[(page_addr
- proc
->buffer
) / PAGE_SIZE
];
690 zap_page_range(vma
, (uintptr_t)page_addr
+
691 proc
->user_buffer_offset
, PAGE_SIZE
, NULL
);
692 err_vm_insert_page_failed
:
693 unmap_kernel_range((unsigned long)page_addr
, PAGE_SIZE
);
694 err_map_kernel_failed
:
697 err_alloc_page_failed
:
702 up_write(&mm
->mmap_sem
);
708 static struct binder_buffer
*binder_alloc_buf(struct binder_proc
*proc
,
710 size_t offsets_size
, int is_async
)
712 struct rb_node
*n
= proc
->free_buffers
.rb_node
;
713 struct binder_buffer
*buffer
;
715 struct rb_node
*best_fit
= NULL
;
720 if (proc
->vma
== NULL
) {
721 printk(KERN_ERR
"binder: %d: binder_alloc_buf, no vma\n",
726 size
= ALIGN(data_size
, sizeof(void *)) +
727 ALIGN(offsets_size
, sizeof(void *));
729 if (size
< data_size
|| size
< offsets_size
) {
730 binder_user_error("binder: %d: got transaction with invalid "
731 "size %zd-%zd\n", proc
->pid
, data_size
, offsets_size
);
736 proc
->free_async_space
< size
+ sizeof(struct binder_buffer
)) {
737 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
738 "binder: %d: binder_alloc_buf size %zd"
739 "failed, no async space left\n", proc
->pid
, size
);
744 buffer
= rb_entry(n
, struct binder_buffer
, rb_node
);
745 BUG_ON(!buffer
->free
);
746 buffer_size
= binder_buffer_size(proc
, buffer
);
748 if (size
< buffer_size
) {
751 } else if (size
> buffer_size
)
758 if (best_fit
== NULL
) {
759 printk(KERN_ERR
"binder: %d: binder_alloc_buf size %zd failed, "
760 "no address space\n", proc
->pid
, size
);
764 buffer
= rb_entry(best_fit
, struct binder_buffer
, rb_node
);
765 buffer_size
= binder_buffer_size(proc
, buffer
);
768 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
769 "binder: %d: binder_alloc_buf size %zd got buff"
770 "er %p size %zd\n", proc
->pid
, size
, buffer
, buffer_size
);
773 (void *)(((uintptr_t)buffer
->data
+ buffer_size
) & PAGE_MASK
);
775 if (size
+ sizeof(struct binder_buffer
) + 4 >= buffer_size
)
776 buffer_size
= size
; /* no room for other buffers */
778 buffer_size
= size
+ sizeof(struct binder_buffer
);
781 (void *)PAGE_ALIGN((uintptr_t)buffer
->data
+ buffer_size
);
782 if (end_page_addr
> has_page_addr
)
783 end_page_addr
= has_page_addr
;
784 if (binder_update_page_range(proc
, 1,
785 (void *)PAGE_ALIGN((uintptr_t)buffer
->data
), end_page_addr
, NULL
))
788 rb_erase(best_fit
, &proc
->free_buffers
);
790 binder_insert_allocated_buffer(proc
, buffer
);
791 if (buffer_size
!= size
) {
792 struct binder_buffer
*new_buffer
= (void *)buffer
->data
+ size
;
793 list_add(&new_buffer
->entry
, &buffer
->entry
);
794 new_buffer
->free
= 1;
795 binder_insert_free_buffer(proc
, new_buffer
);
797 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
798 "binder: %d: binder_alloc_buf size %zd got "
799 "%p\n", proc
->pid
, size
, buffer
);
800 buffer
->data_size
= data_size
;
801 buffer
->offsets_size
= offsets_size
;
802 buffer
->async_transaction
= is_async
;
804 proc
->free_async_space
-= size
+ sizeof(struct binder_buffer
);
805 binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC
,
806 "binder: %d: binder_alloc_buf size %zd "
807 "async free %zd\n", proc
->pid
, size
,
808 proc
->free_async_space
);
814 static void *buffer_start_page(struct binder_buffer
*buffer
)
816 return (void *)((uintptr_t)buffer
& PAGE_MASK
);
819 static void *buffer_end_page(struct binder_buffer
*buffer
)
821 return (void *)(((uintptr_t)(buffer
+ 1) - 1) & PAGE_MASK
);
824 static void binder_delete_free_buffer(struct binder_proc
*proc
,
825 struct binder_buffer
*buffer
)
827 struct binder_buffer
*prev
, *next
= NULL
;
828 int free_page_end
= 1;
829 int free_page_start
= 1;
831 BUG_ON(proc
->buffers
.next
== &buffer
->entry
);
832 prev
= list_entry(buffer
->entry
.prev
, struct binder_buffer
, entry
);
834 if (buffer_end_page(prev
) == buffer_start_page(buffer
)) {
836 if (buffer_end_page(prev
) == buffer_end_page(buffer
))
838 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
839 "binder: %d: merge free, buffer %p "
840 "share page with %p\n", proc
->pid
, buffer
, prev
);
843 if (!list_is_last(&buffer
->entry
, &proc
->buffers
)) {
844 next
= list_entry(buffer
->entry
.next
,
845 struct binder_buffer
, entry
);
846 if (buffer_start_page(next
) == buffer_end_page(buffer
)) {
848 if (buffer_start_page(next
) ==
849 buffer_start_page(buffer
))
851 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
852 "binder: %d: merge free, buffer"
853 " %p share page with %p\n", proc
->pid
,
857 list_del(&buffer
->entry
);
858 if (free_page_start
|| free_page_end
) {
859 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
860 "binder: %d: merge free, buffer %p do "
861 "not share page%s%s with with %p or %p\n",
862 proc
->pid
, buffer
, free_page_start
? "" : " end",
863 free_page_end
? "" : " start", prev
, next
);
864 binder_update_page_range(proc
, 0, free_page_start
?
865 buffer_start_page(buffer
) : buffer_end_page(buffer
),
866 (free_page_end
? buffer_end_page(buffer
) :
867 buffer_start_page(buffer
)) + PAGE_SIZE
, NULL
);
871 static void binder_free_buf(struct binder_proc
*proc
,
872 struct binder_buffer
*buffer
)
874 size_t size
, buffer_size
;
876 buffer_size
= binder_buffer_size(proc
, buffer
);
878 size
= ALIGN(buffer
->data_size
, sizeof(void *)) +
879 ALIGN(buffer
->offsets_size
, sizeof(void *));
881 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
882 "binder: %d: binder_free_buf %p size %zd buffer"
883 "_size %zd\n", proc
->pid
, buffer
, size
, buffer_size
);
885 BUG_ON(buffer
->free
);
886 BUG_ON(size
> buffer_size
);
887 BUG_ON(buffer
->transaction
!= NULL
);
888 BUG_ON((void *)buffer
< proc
->buffer
);
889 BUG_ON((void *)buffer
> proc
->buffer
+ proc
->buffer_size
);
891 if (buffer
->async_transaction
) {
892 proc
->free_async_space
+= size
+ sizeof(struct binder_buffer
);
894 binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC
,
895 "binder: %d: binder_free_buf size %zd "
896 "async free %zd\n", proc
->pid
, size
,
897 proc
->free_async_space
);
900 binder_update_page_range(proc
, 0,
901 (void *)PAGE_ALIGN((uintptr_t)buffer
->data
),
902 (void *)(((uintptr_t)buffer
->data
+ buffer_size
) & PAGE_MASK
),
904 rb_erase(&buffer
->rb_node
, &proc
->allocated_buffers
);
906 if (!list_is_last(&buffer
->entry
, &proc
->buffers
)) {
907 struct binder_buffer
*next
= list_entry(buffer
->entry
.next
,
908 struct binder_buffer
, entry
);
910 rb_erase(&next
->rb_node
, &proc
->free_buffers
);
911 binder_delete_free_buffer(proc
, next
);
914 if (proc
->buffers
.next
!= &buffer
->entry
) {
915 struct binder_buffer
*prev
= list_entry(buffer
->entry
.prev
,
916 struct binder_buffer
, entry
);
918 binder_delete_free_buffer(proc
, buffer
);
919 rb_erase(&prev
->rb_node
, &proc
->free_buffers
);
923 binder_insert_free_buffer(proc
, buffer
);
926 static struct binder_node
*binder_get_node(struct binder_proc
*proc
,
929 struct rb_node
*n
= proc
->nodes
.rb_node
;
930 struct binder_node
*node
;
933 node
= rb_entry(n
, struct binder_node
, rb_node
);
937 else if (ptr
> node
->ptr
)
945 static struct binder_node
*binder_new_node(struct binder_proc
*proc
,
949 struct rb_node
**p
= &proc
->nodes
.rb_node
;
950 struct rb_node
*parent
= NULL
;
951 struct binder_node
*node
;
955 node
= rb_entry(parent
, struct binder_node
, rb_node
);
959 else if (ptr
> node
->ptr
)
965 node
= kzalloc(sizeof(*node
), GFP_KERNEL
);
968 binder_stats_created(BINDER_STAT_NODE
);
969 rb_link_node(&node
->rb_node
, parent
, p
);
970 rb_insert_color(&node
->rb_node
, &proc
->nodes
);
971 node
->debug_id
= ++binder_last_id
;
974 node
->cookie
= cookie
;
975 node
->work
.type
= BINDER_WORK_NODE
;
976 INIT_LIST_HEAD(&node
->work
.entry
);
977 INIT_LIST_HEAD(&node
->async_todo
);
978 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
979 "binder: %d:%d node %d u%p c%p created\n",
980 proc
->pid
, current
->pid
, node
->debug_id
,
981 node
->ptr
, node
->cookie
);
985 static int binder_inc_node(struct binder_node
*node
, int strong
, int internal
,
986 struct list_head
*target_list
)
990 if (target_list
== NULL
&&
991 node
->internal_strong_refs
== 0 &&
992 !(node
== binder_context_mgr_node
&&
993 node
->has_strong_ref
)) {
994 printk(KERN_ERR
"binder: invalid inc strong "
995 "node for %d\n", node
->debug_id
);
998 node
->internal_strong_refs
++;
1000 node
->local_strong_refs
++;
1001 if (!node
->has_strong_ref
&& target_list
) {
1002 list_del_init(&node
->work
.entry
);
1003 list_add_tail(&node
->work
.entry
, target_list
);
1007 node
->local_weak_refs
++;
1008 if (!node
->has_weak_ref
&& list_empty(&node
->work
.entry
)) {
1009 if (target_list
== NULL
) {
1010 printk(KERN_ERR
"binder: invalid inc weak node "
1011 "for %d\n", node
->debug_id
);
1014 list_add_tail(&node
->work
.entry
, target_list
);
1020 static int binder_dec_node(struct binder_node
*node
, int strong
, int internal
)
1024 node
->internal_strong_refs
--;
1026 node
->local_strong_refs
--;
1027 if (node
->local_strong_refs
|| node
->internal_strong_refs
)
1031 node
->local_weak_refs
--;
1032 if (node
->local_weak_refs
|| !hlist_empty(&node
->refs
))
1035 if (node
->proc
&& (node
->has_strong_ref
|| node
->has_weak_ref
)) {
1036 if (list_empty(&node
->work
.entry
)) {
1037 list_add_tail(&node
->work
.entry
, &node
->proc
->todo
);
1038 wake_up_interruptible(&node
->proc
->wait
);
1041 if (hlist_empty(&node
->refs
) && !node
->local_strong_refs
&&
1042 !node
->local_weak_refs
) {
1043 list_del_init(&node
->work
.entry
);
1045 rb_erase(&node
->rb_node
, &node
->proc
->nodes
);
1046 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1047 "binder: refless node %d deleted\n",
1050 hlist_del(&node
->dead_node
);
1051 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1052 "binder: dead node %d deleted\n",
1056 binder_stats_deleted(BINDER_STAT_NODE
);
1064 static struct binder_ref
*binder_get_ref(struct binder_proc
*proc
,
1067 struct rb_node
*n
= proc
->refs_by_desc
.rb_node
;
1068 struct binder_ref
*ref
;
1071 ref
= rb_entry(n
, struct binder_ref
, rb_node_desc
);
1073 if (desc
< ref
->desc
)
1075 else if (desc
> ref
->desc
)
1083 static struct binder_ref
*binder_get_ref_for_node(struct binder_proc
*proc
,
1084 struct binder_node
*node
)
1087 struct rb_node
**p
= &proc
->refs_by_node
.rb_node
;
1088 struct rb_node
*parent
= NULL
;
1089 struct binder_ref
*ref
, *new_ref
;
1093 ref
= rb_entry(parent
, struct binder_ref
, rb_node_node
);
1095 if (node
< ref
->node
)
1097 else if (node
> ref
->node
)
1098 p
= &(*p
)->rb_right
;
1102 new_ref
= kzalloc(sizeof(*ref
), GFP_KERNEL
);
1103 if (new_ref
== NULL
)
1105 binder_stats_created(BINDER_STAT_REF
);
1106 new_ref
->debug_id
= ++binder_last_id
;
1107 new_ref
->proc
= proc
;
1108 new_ref
->node
= node
;
1109 rb_link_node(&new_ref
->rb_node_node
, parent
, p
);
1110 rb_insert_color(&new_ref
->rb_node_node
, &proc
->refs_by_node
);
1112 new_ref
->desc
= (node
== binder_context_mgr_node
) ? 0 : 1;
1113 for (n
= rb_first(&proc
->refs_by_desc
); n
!= NULL
; n
= rb_next(n
)) {
1114 ref
= rb_entry(n
, struct binder_ref
, rb_node_desc
);
1115 if (ref
->desc
> new_ref
->desc
)
1117 new_ref
->desc
= ref
->desc
+ 1;
1120 p
= &proc
->refs_by_desc
.rb_node
;
1123 ref
= rb_entry(parent
, struct binder_ref
, rb_node_desc
);
1125 if (new_ref
->desc
< ref
->desc
)
1127 else if (new_ref
->desc
> ref
->desc
)
1128 p
= &(*p
)->rb_right
;
1132 rb_link_node(&new_ref
->rb_node_desc
, parent
, p
);
1133 rb_insert_color(&new_ref
->rb_node_desc
, &proc
->refs_by_desc
);
1135 hlist_add_head(&new_ref
->node_entry
, &node
->refs
);
1137 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1138 "binder: %d new ref %d desc %d for "
1139 "node %d\n", proc
->pid
, new_ref
->debug_id
,
1140 new_ref
->desc
, node
->debug_id
);
1142 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1143 "binder: %d new ref %d desc %d for "
1144 "dead node\n", proc
->pid
, new_ref
->debug_id
,
1150 static void binder_delete_ref(struct binder_ref
*ref
)
1152 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1153 "binder: %d delete ref %d desc %d for "
1154 "node %d\n", ref
->proc
->pid
, ref
->debug_id
,
1155 ref
->desc
, ref
->node
->debug_id
);
1157 rb_erase(&ref
->rb_node_desc
, &ref
->proc
->refs_by_desc
);
1158 rb_erase(&ref
->rb_node_node
, &ref
->proc
->refs_by_node
);
1160 binder_dec_node(ref
->node
, 1, 1);
1161 hlist_del(&ref
->node_entry
);
1162 binder_dec_node(ref
->node
, 0, 1);
1164 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
1165 "binder: %d delete ref %d desc %d "
1166 "has death notification\n", ref
->proc
->pid
,
1167 ref
->debug_id
, ref
->desc
);
1168 list_del(&ref
->death
->work
.entry
);
1170 binder_stats_deleted(BINDER_STAT_DEATH
);
1173 binder_stats_deleted(BINDER_STAT_REF
);
1176 static int binder_inc_ref(struct binder_ref
*ref
, int strong
,
1177 struct list_head
*target_list
)
1181 if (ref
->strong
== 0) {
1182 ret
= binder_inc_node(ref
->node
, 1, 1, target_list
);
1188 if (ref
->weak
== 0) {
1189 ret
= binder_inc_node(ref
->node
, 0, 1, target_list
);
1199 static int binder_dec_ref(struct binder_ref
*ref
, int strong
)
1202 if (ref
->strong
== 0) {
1203 binder_user_error("binder: %d invalid dec strong, "
1204 "ref %d desc %d s %d w %d\n",
1205 ref
->proc
->pid
, ref
->debug_id
,
1206 ref
->desc
, ref
->strong
, ref
->weak
);
1210 if (ref
->strong
== 0) {
1212 ret
= binder_dec_node(ref
->node
, strong
, 1);
1217 if (ref
->weak
== 0) {
1218 binder_user_error("binder: %d invalid dec weak, "
1219 "ref %d desc %d s %d w %d\n",
1220 ref
->proc
->pid
, ref
->debug_id
,
1221 ref
->desc
, ref
->strong
, ref
->weak
);
1226 if (ref
->strong
== 0 && ref
->weak
== 0)
1227 binder_delete_ref(ref
);
1231 static void binder_pop_transaction(struct binder_thread
*target_thread
,
1232 struct binder_transaction
*t
)
1234 if (target_thread
) {
1235 BUG_ON(target_thread
->transaction_stack
!= t
);
1236 BUG_ON(target_thread
->transaction_stack
->from
!= target_thread
);
1237 target_thread
->transaction_stack
=
1238 target_thread
->transaction_stack
->from_parent
;
1243 t
->buffer
->transaction
= NULL
;
1245 binder_stats_deleted(BINDER_STAT_TRANSACTION
);
1248 static void binder_send_failed_reply(struct binder_transaction
*t
,
1249 uint32_t error_code
)
1251 struct binder_thread
*target_thread
;
1252 BUG_ON(t
->flags
& TF_ONE_WAY
);
1254 target_thread
= t
->from
;
1255 if (target_thread
) {
1256 if (target_thread
->return_error
!= BR_OK
&&
1257 target_thread
->return_error2
== BR_OK
) {
1258 target_thread
->return_error2
=
1259 target_thread
->return_error
;
1260 target_thread
->return_error
= BR_OK
;
1262 if (target_thread
->return_error
== BR_OK
) {
1263 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
1264 "binder: send failed reply for "
1265 "transaction %d to %d:%d\n",
1266 t
->debug_id
, target_thread
->proc
->pid
,
1267 target_thread
->pid
);
1269 binder_pop_transaction(target_thread
, t
);
1270 target_thread
->return_error
= error_code
;
1271 wake_up_interruptible(&target_thread
->wait
);
1273 printk(KERN_ERR
"binder: reply failed, target "
1274 "thread, %d:%d, has error code %d "
1275 "already\n", target_thread
->proc
->pid
,
1277 target_thread
->return_error
);
1281 struct binder_transaction
*next
= t
->from_parent
;
1283 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
1284 "binder: send failed reply "
1285 "for transaction %d, target dead\n",
1288 binder_pop_transaction(target_thread
, t
);
1290 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
1291 "binder: reply failed,"
1292 " no target thread at root\n");
1296 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
1297 "binder: reply failed, no target "
1298 "thread -- retry %d\n", t
->debug_id
);
1303 static void binder_transaction_buffer_release(struct binder_proc
*proc
,
1304 struct binder_buffer
*buffer
,
1307 size_t *offp
, *off_end
;
1308 int debug_id
= buffer
->debug_id
;
1310 binder_debug(BINDER_DEBUG_TRANSACTION
,
1311 "binder: %d buffer release %d, size %zd-%zd, failed at %p\n",
1312 proc
->pid
, buffer
->debug_id
,
1313 buffer
->data_size
, buffer
->offsets_size
, failed_at
);
1315 if (buffer
->target_node
)
1316 binder_dec_node(buffer
->target_node
, 1, 0);
1318 offp
= (size_t *)(buffer
->data
+ ALIGN(buffer
->data_size
, sizeof(void *)));
1320 off_end
= failed_at
;
1322 off_end
= (void *)offp
+ buffer
->offsets_size
;
1323 for (; offp
< off_end
; offp
++) {
1324 struct flat_binder_object
*fp
;
1325 if (*offp
> buffer
->data_size
- sizeof(*fp
) ||
1326 buffer
->data_size
< sizeof(*fp
) ||
1327 !IS_ALIGNED(*offp
, sizeof(void *))) {
1328 printk(KERN_ERR
"binder: transaction release %d bad"
1329 "offset %zd, size %zd\n", debug_id
,
1330 *offp
, buffer
->data_size
);
1333 fp
= (struct flat_binder_object
*)(buffer
->data
+ *offp
);
1335 case BINDER_TYPE_BINDER
:
1336 case BINDER_TYPE_WEAK_BINDER
: {
1337 struct binder_node
*node
= binder_get_node(proc
, fp
->binder
);
1339 printk(KERN_ERR
"binder: transaction release %d"
1340 " bad node %p\n", debug_id
, fp
->binder
);
1343 binder_debug(BINDER_DEBUG_TRANSACTION
,
1345 node
->debug_id
, node
->ptr
);
1346 binder_dec_node(node
, fp
->type
== BINDER_TYPE_BINDER
, 0);
1348 case BINDER_TYPE_HANDLE
:
1349 case BINDER_TYPE_WEAK_HANDLE
: {
1350 struct binder_ref
*ref
= binder_get_ref(proc
, fp
->handle
);
1352 printk(KERN_ERR
"binder: transaction release %d"
1353 " bad handle %ld\n", debug_id
,
1357 binder_debug(BINDER_DEBUG_TRANSACTION
,
1358 " ref %d desc %d (node %d)\n",
1359 ref
->debug_id
, ref
->desc
, ref
->node
->debug_id
);
1360 binder_dec_ref(ref
, fp
->type
== BINDER_TYPE_HANDLE
);
1363 case BINDER_TYPE_FD
:
1364 binder_debug(BINDER_DEBUG_TRANSACTION
,
1365 " fd %ld\n", fp
->handle
);
1367 task_close_fd(proc
, fp
->handle
);
1371 printk(KERN_ERR
"binder: transaction release %d bad "
1372 "object type %lx\n", debug_id
, fp
->type
);
1378 static void binder_transaction(struct binder_proc
*proc
,
1379 struct binder_thread
*thread
,
1380 struct binder_transaction_data
*tr
, int reply
)
1382 struct binder_transaction
*t
;
1383 struct binder_work
*tcomplete
;
1384 size_t *offp
, *off_end
;
1385 struct binder_proc
*target_proc
;
1386 struct binder_thread
*target_thread
= NULL
;
1387 struct binder_node
*target_node
= NULL
;
1388 struct list_head
*target_list
;
1389 wait_queue_head_t
*target_wait
;
1390 struct binder_transaction
*in_reply_to
= NULL
;
1391 struct binder_transaction_log_entry
*e
;
1392 uint32_t return_error
;
1394 e
= binder_transaction_log_add(&binder_transaction_log
);
1395 e
->call_type
= reply
? 2 : !!(tr
->flags
& TF_ONE_WAY
);
1396 e
->from_proc
= proc
->pid
;
1397 e
->from_thread
= thread
->pid
;
1398 e
->target_handle
= tr
->target
.handle
;
1399 e
->data_size
= tr
->data_size
;
1400 e
->offsets_size
= tr
->offsets_size
;
1403 in_reply_to
= thread
->transaction_stack
;
1404 if (in_reply_to
== NULL
) {
1405 binder_user_error("binder: %d:%d got reply transaction "
1406 "with no transaction stack\n",
1407 proc
->pid
, thread
->pid
);
1408 return_error
= BR_FAILED_REPLY
;
1409 goto err_empty_call_stack
;
1411 binder_set_nice(in_reply_to
->saved_priority
);
1412 if (in_reply_to
->to_thread
!= thread
) {
1413 binder_user_error("binder: %d:%d got reply transaction "
1414 "with bad transaction stack,"
1415 " transaction %d has target %d:%d\n",
1416 proc
->pid
, thread
->pid
, in_reply_to
->debug_id
,
1417 in_reply_to
->to_proc
?
1418 in_reply_to
->to_proc
->pid
: 0,
1419 in_reply_to
->to_thread
?
1420 in_reply_to
->to_thread
->pid
: 0);
1421 return_error
= BR_FAILED_REPLY
;
1423 goto err_bad_call_stack
;
1425 thread
->transaction_stack
= in_reply_to
->to_parent
;
1426 target_thread
= in_reply_to
->from
;
1427 if (target_thread
== NULL
) {
1428 return_error
= BR_DEAD_REPLY
;
1429 goto err_dead_binder
;
1431 if (target_thread
->transaction_stack
!= in_reply_to
) {
1432 binder_user_error("binder: %d:%d got reply transaction "
1433 "with bad target transaction stack %d, "
1435 proc
->pid
, thread
->pid
,
1436 target_thread
->transaction_stack
?
1437 target_thread
->transaction_stack
->debug_id
: 0,
1438 in_reply_to
->debug_id
);
1439 return_error
= BR_FAILED_REPLY
;
1441 target_thread
= NULL
;
1442 goto err_dead_binder
;
1444 target_proc
= target_thread
->proc
;
1446 if (tr
->target
.handle
) {
1447 struct binder_ref
*ref
;
1448 ref
= binder_get_ref(proc
, tr
->target
.handle
);
1450 binder_user_error("binder: %d:%d got "
1451 "transaction to invalid handle\n",
1452 proc
->pid
, thread
->pid
);
1453 return_error
= BR_FAILED_REPLY
;
1454 goto err_invalid_target_handle
;
1456 target_node
= ref
->node
;
1458 target_node
= binder_context_mgr_node
;
1459 if (target_node
== NULL
) {
1460 return_error
= BR_DEAD_REPLY
;
1461 goto err_no_context_mgr_node
;
1464 e
->to_node
= target_node
->debug_id
;
1465 target_proc
= target_node
->proc
;
1466 if (target_proc
== NULL
) {
1467 return_error
= BR_DEAD_REPLY
;
1468 goto err_dead_binder
;
1470 if (!(tr
->flags
& TF_ONE_WAY
) && thread
->transaction_stack
) {
1471 struct binder_transaction
*tmp
;
1472 tmp
= thread
->transaction_stack
;
1473 if (tmp
->to_thread
!= thread
) {
1474 binder_user_error("binder: %d:%d got new "
1475 "transaction with bad transaction stack"
1476 ", transaction %d has target %d:%d\n",
1477 proc
->pid
, thread
->pid
, tmp
->debug_id
,
1478 tmp
->to_proc
? tmp
->to_proc
->pid
: 0,
1480 tmp
->to_thread
->pid
: 0);
1481 return_error
= BR_FAILED_REPLY
;
1482 goto err_bad_call_stack
;
1485 if (tmp
->from
&& tmp
->from
->proc
== target_proc
)
1486 target_thread
= tmp
->from
;
1487 tmp
= tmp
->from_parent
;
1491 if (target_thread
) {
1492 e
->to_thread
= target_thread
->pid
;
1493 target_list
= &target_thread
->todo
;
1494 target_wait
= &target_thread
->wait
;
1496 target_list
= &target_proc
->todo
;
1497 target_wait
= &target_proc
->wait
;
1499 e
->to_proc
= target_proc
->pid
;
1501 /* TODO: reuse incoming transaction for reply */
1502 t
= kzalloc(sizeof(*t
), GFP_KERNEL
);
1504 return_error
= BR_FAILED_REPLY
;
1505 goto err_alloc_t_failed
;
1507 binder_stats_created(BINDER_STAT_TRANSACTION
);
1509 tcomplete
= kzalloc(sizeof(*tcomplete
), GFP_KERNEL
);
1510 if (tcomplete
== NULL
) {
1511 return_error
= BR_FAILED_REPLY
;
1512 goto err_alloc_tcomplete_failed
;
1514 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE
);
1516 t
->debug_id
= ++binder_last_id
;
1517 e
->debug_id
= t
->debug_id
;
1520 binder_debug(BINDER_DEBUG_TRANSACTION
,
1521 "binder: %d:%d BC_REPLY %d -> %d:%d, "
1522 "data %p-%p size %zd-%zd\n",
1523 proc
->pid
, thread
->pid
, t
->debug_id
,
1524 target_proc
->pid
, target_thread
->pid
,
1525 tr
->data
.ptr
.buffer
, tr
->data
.ptr
.offsets
,
1526 tr
->data_size
, tr
->offsets_size
);
1528 binder_debug(BINDER_DEBUG_TRANSACTION
,
1529 "binder: %d:%d BC_TRANSACTION %d -> "
1530 "%d - node %d, data %p-%p size %zd-%zd\n",
1531 proc
->pid
, thread
->pid
, t
->debug_id
,
1532 target_proc
->pid
, target_node
->debug_id
,
1533 tr
->data
.ptr
.buffer
, tr
->data
.ptr
.offsets
,
1534 tr
->data_size
, tr
->offsets_size
);
1536 if (!reply
&& !(tr
->flags
& TF_ONE_WAY
))
1540 t
->sender_euid
= proc
->tsk
->cred
->euid
;
1541 t
->to_proc
= target_proc
;
1542 t
->to_thread
= target_thread
;
1544 t
->flags
= tr
->flags
;
1545 t
->priority
= task_nice(current
);
1546 t
->buffer
= binder_alloc_buf(target_proc
, tr
->data_size
,
1547 tr
->offsets_size
, !reply
&& (t
->flags
& TF_ONE_WAY
));
1548 if (t
->buffer
== NULL
) {
1549 return_error
= BR_FAILED_REPLY
;
1550 goto err_binder_alloc_buf_failed
;
1552 t
->buffer
->allow_user_free
= 0;
1553 t
->buffer
->debug_id
= t
->debug_id
;
1554 t
->buffer
->transaction
= t
;
1555 t
->buffer
->target_node
= target_node
;
1557 binder_inc_node(target_node
, 1, 0, NULL
);
1559 offp
= (size_t *)(t
->buffer
->data
+ ALIGN(tr
->data_size
, sizeof(void *)));
1561 if (copy_from_user(t
->buffer
->data
, tr
->data
.ptr
.buffer
, tr
->data_size
)) {
1562 binder_user_error("binder: %d:%d got transaction with invalid "
1563 "data ptr\n", proc
->pid
, thread
->pid
);
1564 return_error
= BR_FAILED_REPLY
;
1565 goto err_copy_data_failed
;
1567 if (copy_from_user(offp
, tr
->data
.ptr
.offsets
, tr
->offsets_size
)) {
1568 binder_user_error("binder: %d:%d got transaction with invalid "
1569 "offsets ptr\n", proc
->pid
, thread
->pid
);
1570 return_error
= BR_FAILED_REPLY
;
1571 goto err_copy_data_failed
;
1573 if (!IS_ALIGNED(tr
->offsets_size
, sizeof(size_t))) {
1574 binder_user_error("binder: %d:%d got transaction with "
1575 "invalid offsets size, %zd\n",
1576 proc
->pid
, thread
->pid
, tr
->offsets_size
);
1577 return_error
= BR_FAILED_REPLY
;
1578 goto err_bad_offset
;
1580 off_end
= (void *)offp
+ tr
->offsets_size
;
1581 for (; offp
< off_end
; offp
++) {
1582 struct flat_binder_object
*fp
;
1583 if (*offp
> t
->buffer
->data_size
- sizeof(*fp
) ||
1584 t
->buffer
->data_size
< sizeof(*fp
) ||
1585 !IS_ALIGNED(*offp
, sizeof(void *))) {
1586 binder_user_error("binder: %d:%d got transaction with "
1587 "invalid offset, %zd\n",
1588 proc
->pid
, thread
->pid
, *offp
);
1589 return_error
= BR_FAILED_REPLY
;
1590 goto err_bad_offset
;
1592 fp
= (struct flat_binder_object
*)(t
->buffer
->data
+ *offp
);
1594 case BINDER_TYPE_BINDER
:
1595 case BINDER_TYPE_WEAK_BINDER
: {
1596 struct binder_ref
*ref
;
1597 struct binder_node
*node
= binder_get_node(proc
, fp
->binder
);
1599 node
= binder_new_node(proc
, fp
->binder
, fp
->cookie
);
1601 return_error
= BR_FAILED_REPLY
;
1602 goto err_binder_new_node_failed
;
1604 node
->min_priority
= fp
->flags
& FLAT_BINDER_FLAG_PRIORITY_MASK
;
1605 node
->accept_fds
= !!(fp
->flags
& FLAT_BINDER_FLAG_ACCEPTS_FDS
);
1607 if (fp
->cookie
!= node
->cookie
) {
1608 binder_user_error("binder: %d:%d sending u%p "
1609 "node %d, cookie mismatch %p != %p\n",
1610 proc
->pid
, thread
->pid
,
1611 fp
->binder
, node
->debug_id
,
1612 fp
->cookie
, node
->cookie
);
1613 goto err_binder_get_ref_for_node_failed
;
1615 ref
= binder_get_ref_for_node(target_proc
, node
);
1617 return_error
= BR_FAILED_REPLY
;
1618 goto err_binder_get_ref_for_node_failed
;
1620 if (fp
->type
== BINDER_TYPE_BINDER
)
1621 fp
->type
= BINDER_TYPE_HANDLE
;
1623 fp
->type
= BINDER_TYPE_WEAK_HANDLE
;
1624 fp
->handle
= ref
->desc
;
1625 binder_inc_ref(ref
, fp
->type
== BINDER_TYPE_HANDLE
,
1628 binder_debug(BINDER_DEBUG_TRANSACTION
,
1629 " node %d u%p -> ref %d desc %d\n",
1630 node
->debug_id
, node
->ptr
, ref
->debug_id
,
1633 case BINDER_TYPE_HANDLE
:
1634 case BINDER_TYPE_WEAK_HANDLE
: {
1635 struct binder_ref
*ref
= binder_get_ref(proc
, fp
->handle
);
1637 binder_user_error("binder: %d:%d got "
1638 "transaction with invalid "
1639 "handle, %ld\n", proc
->pid
,
1640 thread
->pid
, fp
->handle
);
1641 return_error
= BR_FAILED_REPLY
;
1642 goto err_binder_get_ref_failed
;
1644 if (ref
->node
->proc
== target_proc
) {
1645 if (fp
->type
== BINDER_TYPE_HANDLE
)
1646 fp
->type
= BINDER_TYPE_BINDER
;
1648 fp
->type
= BINDER_TYPE_WEAK_BINDER
;
1649 fp
->binder
= ref
->node
->ptr
;
1650 fp
->cookie
= ref
->node
->cookie
;
1651 binder_inc_node(ref
->node
, fp
->type
== BINDER_TYPE_BINDER
, 0, NULL
);
1652 binder_debug(BINDER_DEBUG_TRANSACTION
,
1653 " ref %d desc %d -> node %d u%p\n",
1654 ref
->debug_id
, ref
->desc
, ref
->node
->debug_id
,
1657 struct binder_ref
*new_ref
;
1658 new_ref
= binder_get_ref_for_node(target_proc
, ref
->node
);
1659 if (new_ref
== NULL
) {
1660 return_error
= BR_FAILED_REPLY
;
1661 goto err_binder_get_ref_for_node_failed
;
1663 fp
->handle
= new_ref
->desc
;
1664 binder_inc_ref(new_ref
, fp
->type
== BINDER_TYPE_HANDLE
, NULL
);
1665 binder_debug(BINDER_DEBUG_TRANSACTION
,
1666 " ref %d desc %d -> ref %d desc %d (node %d)\n",
1667 ref
->debug_id
, ref
->desc
, new_ref
->debug_id
,
1668 new_ref
->desc
, ref
->node
->debug_id
);
1672 case BINDER_TYPE_FD
: {
1677 if (!(in_reply_to
->flags
& TF_ACCEPT_FDS
)) {
1678 binder_user_error("binder: %d:%d got reply with fd, %ld, but target does not allow fds\n",
1679 proc
->pid
, thread
->pid
, fp
->handle
);
1680 return_error
= BR_FAILED_REPLY
;
1681 goto err_fd_not_allowed
;
1683 } else if (!target_node
->accept_fds
) {
1684 binder_user_error("binder: %d:%d got transaction with fd, %ld, but target does not allow fds\n",
1685 proc
->pid
, thread
->pid
, fp
->handle
);
1686 return_error
= BR_FAILED_REPLY
;
1687 goto err_fd_not_allowed
;
1690 file
= fget(fp
->handle
);
1692 binder_user_error("binder: %d:%d got transaction with invalid fd, %ld\n",
1693 proc
->pid
, thread
->pid
, fp
->handle
);
1694 return_error
= BR_FAILED_REPLY
;
1695 goto err_fget_failed
;
1697 target_fd
= task_get_unused_fd_flags(target_proc
, O_CLOEXEC
);
1698 if (target_fd
< 0) {
1700 return_error
= BR_FAILED_REPLY
;
1701 goto err_get_unused_fd_failed
;
1703 task_fd_install(target_proc
, target_fd
, file
);
1704 binder_debug(BINDER_DEBUG_TRANSACTION
,
1705 " fd %ld -> %d\n", fp
->handle
, target_fd
);
1707 fp
->handle
= target_fd
;
1711 binder_user_error("binder: %d:%d got transactio"
1712 "n with invalid object type, %lx\n",
1713 proc
->pid
, thread
->pid
, fp
->type
);
1714 return_error
= BR_FAILED_REPLY
;
1715 goto err_bad_object_type
;
1719 BUG_ON(t
->buffer
->async_transaction
!= 0);
1720 binder_pop_transaction(target_thread
, in_reply_to
);
1721 } else if (!(t
->flags
& TF_ONE_WAY
)) {
1722 BUG_ON(t
->buffer
->async_transaction
!= 0);
1724 t
->from_parent
= thread
->transaction_stack
;
1725 thread
->transaction_stack
= t
;
1727 BUG_ON(target_node
== NULL
);
1728 BUG_ON(t
->buffer
->async_transaction
!= 1);
1729 if (target_node
->has_async_transaction
) {
1730 target_list
= &target_node
->async_todo
;
1733 target_node
->has_async_transaction
= 1;
1735 t
->work
.type
= BINDER_WORK_TRANSACTION
;
1736 list_add_tail(&t
->work
.entry
, target_list
);
1737 tcomplete
->type
= BINDER_WORK_TRANSACTION_COMPLETE
;
1738 list_add_tail(&tcomplete
->entry
, &thread
->todo
);
1740 wake_up_interruptible(target_wait
);
1743 err_get_unused_fd_failed
:
1746 err_binder_get_ref_for_node_failed
:
1747 err_binder_get_ref_failed
:
1748 err_binder_new_node_failed
:
1749 err_bad_object_type
:
1751 err_copy_data_failed
:
1752 binder_transaction_buffer_release(target_proc
, t
->buffer
, offp
);
1753 t
->buffer
->transaction
= NULL
;
1754 binder_free_buf(target_proc
, t
->buffer
);
1755 err_binder_alloc_buf_failed
:
1757 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE
);
1758 err_alloc_tcomplete_failed
:
1760 binder_stats_deleted(BINDER_STAT_TRANSACTION
);
1763 err_empty_call_stack
:
1765 err_invalid_target_handle
:
1766 err_no_context_mgr_node
:
1767 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
1768 "binder: %d:%d transaction failed %d, size %zd-%zd\n",
1769 proc
->pid
, thread
->pid
, return_error
,
1770 tr
->data_size
, tr
->offsets_size
);
1773 struct binder_transaction_log_entry
*fe
;
1774 fe
= binder_transaction_log_add(&binder_transaction_log_failed
);
1778 BUG_ON(thread
->return_error
!= BR_OK
);
1780 thread
->return_error
= BR_TRANSACTION_COMPLETE
;
1781 binder_send_failed_reply(in_reply_to
, return_error
);
1783 thread
->return_error
= return_error
;
1786 int binder_thread_write(struct binder_proc
*proc
, struct binder_thread
*thread
,
1787 void __user
*buffer
, int size
, signed long *consumed
)
1790 void __user
*ptr
= buffer
+ *consumed
;
1791 void __user
*end
= buffer
+ size
;
1793 while (ptr
< end
&& thread
->return_error
== BR_OK
) {
1794 if (get_user(cmd
, (uint32_t __user
*)ptr
))
1796 ptr
+= sizeof(uint32_t);
1797 if (_IOC_NR(cmd
) < ARRAY_SIZE(binder_stats
.bc
)) {
1798 binder_stats
.bc
[_IOC_NR(cmd
)]++;
1799 proc
->stats
.bc
[_IOC_NR(cmd
)]++;
1800 thread
->stats
.bc
[_IOC_NR(cmd
)]++;
1808 struct binder_ref
*ref
;
1809 const char *debug_string
;
1811 if (get_user(target
, (uint32_t __user
*)ptr
))
1813 ptr
+= sizeof(uint32_t);
1814 if (target
== 0 && binder_context_mgr_node
&&
1815 (cmd
== BC_INCREFS
|| cmd
== BC_ACQUIRE
)) {
1816 ref
= binder_get_ref_for_node(proc
,
1817 binder_context_mgr_node
);
1818 if (ref
->desc
!= target
) {
1819 binder_user_error("binder: %d:"
1820 "%d tried to acquire "
1821 "reference to desc 0, "
1823 proc
->pid
, thread
->pid
,
1827 ref
= binder_get_ref(proc
, target
);
1829 binder_user_error("binder: %d:%d refcou"
1830 "nt change on invalid ref %d\n",
1831 proc
->pid
, thread
->pid
, target
);
1836 debug_string
= "IncRefs";
1837 binder_inc_ref(ref
, 0, NULL
);
1840 debug_string
= "Acquire";
1841 binder_inc_ref(ref
, 1, NULL
);
1844 debug_string
= "Release";
1845 binder_dec_ref(ref
, 1);
1849 debug_string
= "DecRefs";
1850 binder_dec_ref(ref
, 0);
1853 binder_debug(BINDER_DEBUG_USER_REFS
,
1854 "binder: %d:%d %s ref %d desc %d s %d w %d for node %d\n",
1855 proc
->pid
, thread
->pid
, debug_string
, ref
->debug_id
,
1856 ref
->desc
, ref
->strong
, ref
->weak
, ref
->node
->debug_id
);
1859 case BC_INCREFS_DONE
:
1860 case BC_ACQUIRE_DONE
: {
1861 void __user
*node_ptr
;
1863 struct binder_node
*node
;
1865 if (get_user(node_ptr
, (void * __user
*)ptr
))
1867 ptr
+= sizeof(void *);
1868 if (get_user(cookie
, (void * __user
*)ptr
))
1870 ptr
+= sizeof(void *);
1871 node
= binder_get_node(proc
, node_ptr
);
1873 binder_user_error("binder: %d:%d "
1874 "%s u%p no match\n",
1875 proc
->pid
, thread
->pid
,
1876 cmd
== BC_INCREFS_DONE
?
1882 if (cookie
!= node
->cookie
) {
1883 binder_user_error("binder: %d:%d %s u%p node %d"
1884 " cookie mismatch %p != %p\n",
1885 proc
->pid
, thread
->pid
,
1886 cmd
== BC_INCREFS_DONE
?
1887 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
1888 node_ptr
, node
->debug_id
,
1889 cookie
, node
->cookie
);
1892 if (cmd
== BC_ACQUIRE_DONE
) {
1893 if (node
->pending_strong_ref
== 0) {
1894 binder_user_error("binder: %d:%d "
1895 "BC_ACQUIRE_DONE node %d has "
1896 "no pending acquire request\n",
1897 proc
->pid
, thread
->pid
,
1901 node
->pending_strong_ref
= 0;
1903 if (node
->pending_weak_ref
== 0) {
1904 binder_user_error("binder: %d:%d "
1905 "BC_INCREFS_DONE node %d has "
1906 "no pending increfs request\n",
1907 proc
->pid
, thread
->pid
,
1911 node
->pending_weak_ref
= 0;
1913 binder_dec_node(node
, cmd
== BC_ACQUIRE_DONE
, 0);
1914 binder_debug(BINDER_DEBUG_USER_REFS
,
1915 "binder: %d:%d %s node %d ls %d lw %d\n",
1916 proc
->pid
, thread
->pid
,
1917 cmd
== BC_INCREFS_DONE
? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
1918 node
->debug_id
, node
->local_strong_refs
, node
->local_weak_refs
);
1921 case BC_ATTEMPT_ACQUIRE
:
1922 printk(KERN_ERR
"binder: BC_ATTEMPT_ACQUIRE not supported\n");
1924 case BC_ACQUIRE_RESULT
:
1925 printk(KERN_ERR
"binder: BC_ACQUIRE_RESULT not supported\n");
1928 case BC_FREE_BUFFER
: {
1929 void __user
*data_ptr
;
1930 struct binder_buffer
*buffer
;
1932 if (get_user(data_ptr
, (void * __user
*)ptr
))
1934 ptr
+= sizeof(void *);
1936 buffer
= binder_buffer_lookup(proc
, data_ptr
);
1937 if (buffer
== NULL
) {
1938 binder_user_error("binder: %d:%d "
1939 "BC_FREE_BUFFER u%p no match\n",
1940 proc
->pid
, thread
->pid
, data_ptr
);
1943 if (!buffer
->allow_user_free
) {
1944 binder_user_error("binder: %d:%d "
1945 "BC_FREE_BUFFER u%p matched "
1946 "unreturned buffer\n",
1947 proc
->pid
, thread
->pid
, data_ptr
);
1950 binder_debug(BINDER_DEBUG_FREE_BUFFER
,
1951 "binder: %d:%d BC_FREE_BUFFER u%p found buffer %d for %s transaction\n",
1952 proc
->pid
, thread
->pid
, data_ptr
, buffer
->debug_id
,
1953 buffer
->transaction
? "active" : "finished");
1955 if (buffer
->transaction
) {
1956 buffer
->transaction
->buffer
= NULL
;
1957 buffer
->transaction
= NULL
;
1959 if (buffer
->async_transaction
&& buffer
->target_node
) {
1960 BUG_ON(!buffer
->target_node
->has_async_transaction
);
1961 if (list_empty(&buffer
->target_node
->async_todo
))
1962 buffer
->target_node
->has_async_transaction
= 0;
1964 list_move_tail(buffer
->target_node
->async_todo
.next
, &thread
->todo
);
1966 binder_transaction_buffer_release(proc
, buffer
, NULL
);
1967 binder_free_buf(proc
, buffer
);
1971 case BC_TRANSACTION
:
1973 struct binder_transaction_data tr
;
1975 if (copy_from_user(&tr
, ptr
, sizeof(tr
)))
1978 binder_transaction(proc
, thread
, &tr
, cmd
== BC_REPLY
);
1982 case BC_REGISTER_LOOPER
:
1983 binder_debug(BINDER_DEBUG_THREADS
,
1984 "binder: %d:%d BC_REGISTER_LOOPER\n",
1985 proc
->pid
, thread
->pid
);
1986 if (thread
->looper
& BINDER_LOOPER_STATE_ENTERED
) {
1987 thread
->looper
|= BINDER_LOOPER_STATE_INVALID
;
1988 binder_user_error("binder: %d:%d ERROR:"
1989 " BC_REGISTER_LOOPER called "
1990 "after BC_ENTER_LOOPER\n",
1991 proc
->pid
, thread
->pid
);
1992 } else if (proc
->requested_threads
== 0) {
1993 thread
->looper
|= BINDER_LOOPER_STATE_INVALID
;
1994 binder_user_error("binder: %d:%d ERROR:"
1995 " BC_REGISTER_LOOPER called "
1996 "without request\n",
1997 proc
->pid
, thread
->pid
);
1999 proc
->requested_threads
--;
2000 proc
->requested_threads_started
++;
2002 thread
->looper
|= BINDER_LOOPER_STATE_REGISTERED
;
2004 case BC_ENTER_LOOPER
:
2005 binder_debug(BINDER_DEBUG_THREADS
,
2006 "binder: %d:%d BC_ENTER_LOOPER\n",
2007 proc
->pid
, thread
->pid
);
2008 if (thread
->looper
& BINDER_LOOPER_STATE_REGISTERED
) {
2009 thread
->looper
|= BINDER_LOOPER_STATE_INVALID
;
2010 binder_user_error("binder: %d:%d ERROR:"
2011 " BC_ENTER_LOOPER called after "
2012 "BC_REGISTER_LOOPER\n",
2013 proc
->pid
, thread
->pid
);
2015 thread
->looper
|= BINDER_LOOPER_STATE_ENTERED
;
2017 case BC_EXIT_LOOPER
:
2018 binder_debug(BINDER_DEBUG_THREADS
,
2019 "binder: %d:%d BC_EXIT_LOOPER\n",
2020 proc
->pid
, thread
->pid
);
2021 thread
->looper
|= BINDER_LOOPER_STATE_EXITED
;
2024 case BC_REQUEST_DEATH_NOTIFICATION
:
2025 case BC_CLEAR_DEATH_NOTIFICATION
: {
2027 void __user
*cookie
;
2028 struct binder_ref
*ref
;
2029 struct binder_ref_death
*death
;
2031 if (get_user(target
, (uint32_t __user
*)ptr
))
2033 ptr
+= sizeof(uint32_t);
2034 if (get_user(cookie
, (void __user
* __user
*)ptr
))
2036 ptr
+= sizeof(void *);
2037 ref
= binder_get_ref(proc
, target
);
2039 binder_user_error("binder: %d:%d %s "
2041 proc
->pid
, thread
->pid
,
2042 cmd
== BC_REQUEST_DEATH_NOTIFICATION
?
2043 "BC_REQUEST_DEATH_NOTIFICATION" :
2044 "BC_CLEAR_DEATH_NOTIFICATION",
2049 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION
,
2050 "binder: %d:%d %s %p ref %d desc %d s %d w %d for node %d\n",
2051 proc
->pid
, thread
->pid
,
2052 cmd
== BC_REQUEST_DEATH_NOTIFICATION
?
2053 "BC_REQUEST_DEATH_NOTIFICATION" :
2054 "BC_CLEAR_DEATH_NOTIFICATION",
2055 cookie
, ref
->debug_id
, ref
->desc
,
2056 ref
->strong
, ref
->weak
, ref
->node
->debug_id
);
2058 if (cmd
== BC_REQUEST_DEATH_NOTIFICATION
) {
2060 binder_user_error("binder: %d:%"
2061 "d BC_REQUEST_DEATH_NOTI"
2062 "FICATION death notific"
2063 "ation already set\n",
2064 proc
->pid
, thread
->pid
);
2067 death
= kzalloc(sizeof(*death
), GFP_KERNEL
);
2068 if (death
== NULL
) {
2069 thread
->return_error
= BR_ERROR
;
2070 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
2072 "BC_REQUEST_DEATH_NOTIFICATION failed\n",
2073 proc
->pid
, thread
->pid
);
2076 binder_stats_created(BINDER_STAT_DEATH
);
2077 INIT_LIST_HEAD(&death
->work
.entry
);
2078 death
->cookie
= cookie
;
2080 if (ref
->node
->proc
== NULL
) {
2081 ref
->death
->work
.type
= BINDER_WORK_DEAD_BINDER
;
2082 if (thread
->looper
& (BINDER_LOOPER_STATE_REGISTERED
| BINDER_LOOPER_STATE_ENTERED
)) {
2083 list_add_tail(&ref
->death
->work
.entry
, &thread
->todo
);
2085 list_add_tail(&ref
->death
->work
.entry
, &proc
->todo
);
2086 wake_up_interruptible(&proc
->wait
);
2090 if (ref
->death
== NULL
) {
2091 binder_user_error("binder: %d:%"
2092 "d BC_CLEAR_DEATH_NOTIFI"
2093 "CATION death notificat"
2095 proc
->pid
, thread
->pid
);
2099 if (death
->cookie
!= cookie
) {
2100 binder_user_error("binder: %d:%"
2101 "d BC_CLEAR_DEATH_NOTIFI"
2102 "CATION death notificat"
2103 "ion cookie mismatch "
2105 proc
->pid
, thread
->pid
,
2106 death
->cookie
, cookie
);
2110 if (list_empty(&death
->work
.entry
)) {
2111 death
->work
.type
= BINDER_WORK_CLEAR_DEATH_NOTIFICATION
;
2112 if (thread
->looper
& (BINDER_LOOPER_STATE_REGISTERED
| BINDER_LOOPER_STATE_ENTERED
)) {
2113 list_add_tail(&death
->work
.entry
, &thread
->todo
);
2115 list_add_tail(&death
->work
.entry
, &proc
->todo
);
2116 wake_up_interruptible(&proc
->wait
);
2119 BUG_ON(death
->work
.type
!= BINDER_WORK_DEAD_BINDER
);
2120 death
->work
.type
= BINDER_WORK_DEAD_BINDER_AND_CLEAR
;
2124 case BC_DEAD_BINDER_DONE
: {
2125 struct binder_work
*w
;
2126 void __user
*cookie
;
2127 struct binder_ref_death
*death
= NULL
;
2128 if (get_user(cookie
, (void __user
* __user
*)ptr
))
2131 ptr
+= sizeof(void *);
2132 list_for_each_entry(w
, &proc
->delivered_death
, entry
) {
2133 struct binder_ref_death
*tmp_death
= container_of(w
, struct binder_ref_death
, work
);
2134 if (tmp_death
->cookie
== cookie
) {
2139 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
2140 "binder: %d:%d BC_DEAD_BINDER_DONE %p found %p\n",
2141 proc
->pid
, thread
->pid
, cookie
, death
);
2142 if (death
== NULL
) {
2143 binder_user_error("binder: %d:%d BC_DEAD"
2144 "_BINDER_DONE %p not found\n",
2145 proc
->pid
, thread
->pid
, cookie
);
2149 list_del_init(&death
->work
.entry
);
2150 if (death
->work
.type
== BINDER_WORK_DEAD_BINDER_AND_CLEAR
) {
2151 death
->work
.type
= BINDER_WORK_CLEAR_DEATH_NOTIFICATION
;
2152 if (thread
->looper
& (BINDER_LOOPER_STATE_REGISTERED
| BINDER_LOOPER_STATE_ENTERED
)) {
2153 list_add_tail(&death
->work
.entry
, &thread
->todo
);
2155 list_add_tail(&death
->work
.entry
, &proc
->todo
);
2156 wake_up_interruptible(&proc
->wait
);
2162 printk(KERN_ERR
"binder: %d:%d unknown command %d\n",
2163 proc
->pid
, thread
->pid
, cmd
);
2166 *consumed
= ptr
- buffer
;
2171 void binder_stat_br(struct binder_proc
*proc
, struct binder_thread
*thread
,
2174 if (_IOC_NR(cmd
) < ARRAY_SIZE(binder_stats
.br
)) {
2175 binder_stats
.br
[_IOC_NR(cmd
)]++;
2176 proc
->stats
.br
[_IOC_NR(cmd
)]++;
2177 thread
->stats
.br
[_IOC_NR(cmd
)]++;
2181 static int binder_has_proc_work(struct binder_proc
*proc
,
2182 struct binder_thread
*thread
)
2184 return !list_empty(&proc
->todo
) ||
2185 (thread
->looper
& BINDER_LOOPER_STATE_NEED_RETURN
);
2188 static int binder_has_thread_work(struct binder_thread
*thread
)
2190 return !list_empty(&thread
->todo
) || thread
->return_error
!= BR_OK
||
2191 (thread
->looper
& BINDER_LOOPER_STATE_NEED_RETURN
);
2194 static int binder_thread_read(struct binder_proc
*proc
,
2195 struct binder_thread
*thread
,
2196 void __user
*buffer
, int size
,
2197 signed long *consumed
, int non_block
)
2199 void __user
*ptr
= buffer
+ *consumed
;
2200 void __user
*end
= buffer
+ size
;
2203 int wait_for_proc_work
;
2205 if (*consumed
== 0) {
2206 if (put_user(BR_NOOP
, (uint32_t __user
*)ptr
))
2208 ptr
+= sizeof(uint32_t);
2212 wait_for_proc_work
= thread
->transaction_stack
== NULL
&&
2213 list_empty(&thread
->todo
);
2215 if (thread
->return_error
!= BR_OK
&& ptr
< end
) {
2216 if (thread
->return_error2
!= BR_OK
) {
2217 if (put_user(thread
->return_error2
, (uint32_t __user
*)ptr
))
2219 ptr
+= sizeof(uint32_t);
2222 thread
->return_error2
= BR_OK
;
2224 if (put_user(thread
->return_error
, (uint32_t __user
*)ptr
))
2226 ptr
+= sizeof(uint32_t);
2227 thread
->return_error
= BR_OK
;
2232 thread
->looper
|= BINDER_LOOPER_STATE_WAITING
;
2233 if (wait_for_proc_work
)
2234 proc
->ready_threads
++;
2235 mutex_unlock(&binder_lock
);
2236 if (wait_for_proc_work
) {
2237 if (!(thread
->looper
& (BINDER_LOOPER_STATE_REGISTERED
|
2238 BINDER_LOOPER_STATE_ENTERED
))) {
2239 binder_user_error("binder: %d:%d ERROR: Thread waiting "
2240 "for process work before calling BC_REGISTER_"
2241 "LOOPER or BC_ENTER_LOOPER (state %x)\n",
2242 proc
->pid
, thread
->pid
, thread
->looper
);
2243 wait_event_interruptible(binder_user_error_wait
,
2244 binder_stop_on_user_error
< 2);
2246 binder_set_nice(proc
->default_priority
);
2248 if (!binder_has_proc_work(proc
, thread
))
2251 ret
= wait_event_interruptible_exclusive(proc
->wait
, binder_has_proc_work(proc
, thread
));
2254 if (!binder_has_thread_work(thread
))
2257 ret
= wait_event_interruptible(thread
->wait
, binder_has_thread_work(thread
));
2259 mutex_lock(&binder_lock
);
2260 if (wait_for_proc_work
)
2261 proc
->ready_threads
--;
2262 thread
->looper
&= ~BINDER_LOOPER_STATE_WAITING
;
2269 struct binder_transaction_data tr
;
2270 struct binder_work
*w
;
2271 struct binder_transaction
*t
= NULL
;
2273 if (!list_empty(&thread
->todo
))
2274 w
= list_first_entry(&thread
->todo
, struct binder_work
, entry
);
2275 else if (!list_empty(&proc
->todo
) && wait_for_proc_work
)
2276 w
= list_first_entry(&proc
->todo
, struct binder_work
, entry
);
2278 if (ptr
- buffer
== 4 && !(thread
->looper
& BINDER_LOOPER_STATE_NEED_RETURN
)) /* no data added */
2283 if (end
- ptr
< sizeof(tr
) + 4)
2287 case BINDER_WORK_TRANSACTION
: {
2288 t
= container_of(w
, struct binder_transaction
, work
);
2290 case BINDER_WORK_TRANSACTION_COMPLETE
: {
2291 cmd
= BR_TRANSACTION_COMPLETE
;
2292 if (put_user(cmd
, (uint32_t __user
*)ptr
))
2294 ptr
+= sizeof(uint32_t);
2296 binder_stat_br(proc
, thread
, cmd
);
2297 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE
,
2298 "binder: %d:%d BR_TRANSACTION_COMPLETE\n",
2299 proc
->pid
, thread
->pid
);
2301 list_del(&w
->entry
);
2303 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE
);
2305 case BINDER_WORK_NODE
: {
2306 struct binder_node
*node
= container_of(w
, struct binder_node
, work
);
2307 uint32_t cmd
= BR_NOOP
;
2308 const char *cmd_name
;
2309 int strong
= node
->internal_strong_refs
|| node
->local_strong_refs
;
2310 int weak
= !hlist_empty(&node
->refs
) || node
->local_weak_refs
|| strong
;
2311 if (weak
&& !node
->has_weak_ref
) {
2313 cmd_name
= "BR_INCREFS";
2314 node
->has_weak_ref
= 1;
2315 node
->pending_weak_ref
= 1;
2316 node
->local_weak_refs
++;
2317 } else if (strong
&& !node
->has_strong_ref
) {
2319 cmd_name
= "BR_ACQUIRE";
2320 node
->has_strong_ref
= 1;
2321 node
->pending_strong_ref
= 1;
2322 node
->local_strong_refs
++;
2323 } else if (!strong
&& node
->has_strong_ref
) {
2325 cmd_name
= "BR_RELEASE";
2326 node
->has_strong_ref
= 0;
2327 } else if (!weak
&& node
->has_weak_ref
) {
2329 cmd_name
= "BR_DECREFS";
2330 node
->has_weak_ref
= 0;
2332 if (cmd
!= BR_NOOP
) {
2333 if (put_user(cmd
, (uint32_t __user
*)ptr
))
2335 ptr
+= sizeof(uint32_t);
2336 if (put_user(node
->ptr
, (void * __user
*)ptr
))
2338 ptr
+= sizeof(void *);
2339 if (put_user(node
->cookie
, (void * __user
*)ptr
))
2341 ptr
+= sizeof(void *);
2343 binder_stat_br(proc
, thread
, cmd
);
2344 binder_debug(BINDER_DEBUG_USER_REFS
,
2345 "binder: %d:%d %s %d u%p c%p\n",
2346 proc
->pid
, thread
->pid
, cmd_name
, node
->debug_id
, node
->ptr
, node
->cookie
);
2348 list_del_init(&w
->entry
);
2349 if (!weak
&& !strong
) {
2350 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
2351 "binder: %d:%d node %d u%p c%p deleted\n",
2352 proc
->pid
, thread
->pid
, node
->debug_id
,
2353 node
->ptr
, node
->cookie
);
2354 rb_erase(&node
->rb_node
, &proc
->nodes
);
2356 binder_stats_deleted(BINDER_STAT_NODE
);
2358 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
2359 "binder: %d:%d node %d u%p c%p state unchanged\n",
2360 proc
->pid
, thread
->pid
, node
->debug_id
, node
->ptr
,
2365 case BINDER_WORK_DEAD_BINDER
:
2366 case BINDER_WORK_DEAD_BINDER_AND_CLEAR
:
2367 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION
: {
2368 struct binder_ref_death
*death
;
2371 death
= container_of(w
, struct binder_ref_death
, work
);
2372 if (w
->type
== BINDER_WORK_CLEAR_DEATH_NOTIFICATION
)
2373 cmd
= BR_CLEAR_DEATH_NOTIFICATION_DONE
;
2375 cmd
= BR_DEAD_BINDER
;
2376 if (put_user(cmd
, (uint32_t __user
*)ptr
))
2378 ptr
+= sizeof(uint32_t);
2379 if (put_user(death
->cookie
, (void * __user
*)ptr
))
2381 ptr
+= sizeof(void *);
2382 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION
,
2383 "binder: %d:%d %s %p\n",
2384 proc
->pid
, thread
->pid
,
2385 cmd
== BR_DEAD_BINDER
?
2387 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
2390 if (w
->type
== BINDER_WORK_CLEAR_DEATH_NOTIFICATION
) {
2391 list_del(&w
->entry
);
2393 binder_stats_deleted(BINDER_STAT_DEATH
);
2395 list_move(&w
->entry
, &proc
->delivered_death
);
2396 if (cmd
== BR_DEAD_BINDER
)
2397 goto done
; /* DEAD_BINDER notifications can cause transactions */
2404 BUG_ON(t
->buffer
== NULL
);
2405 if (t
->buffer
->target_node
) {
2406 struct binder_node
*target_node
= t
->buffer
->target_node
;
2407 tr
.target
.ptr
= target_node
->ptr
;
2408 tr
.cookie
= target_node
->cookie
;
2409 t
->saved_priority
= task_nice(current
);
2410 if (t
->priority
< target_node
->min_priority
&&
2411 !(t
->flags
& TF_ONE_WAY
))
2412 binder_set_nice(t
->priority
);
2413 else if (!(t
->flags
& TF_ONE_WAY
) ||
2414 t
->saved_priority
> target_node
->min_priority
)
2415 binder_set_nice(target_node
->min_priority
);
2416 cmd
= BR_TRANSACTION
;
2418 tr
.target
.ptr
= NULL
;
2423 tr
.flags
= t
->flags
;
2424 tr
.sender_euid
= t
->sender_euid
;
2427 struct task_struct
*sender
= t
->from
->proc
->tsk
;
2428 tr
.sender_pid
= task_tgid_nr_ns(sender
,
2429 current
->nsproxy
->pid_ns
);
2434 tr
.data_size
= t
->buffer
->data_size
;
2435 tr
.offsets_size
= t
->buffer
->offsets_size
;
2436 tr
.data
.ptr
.buffer
= (void *)t
->buffer
->data
+
2437 proc
->user_buffer_offset
;
2438 tr
.data
.ptr
.offsets
= tr
.data
.ptr
.buffer
+
2439 ALIGN(t
->buffer
->data_size
,
2442 if (put_user(cmd
, (uint32_t __user
*)ptr
))
2444 ptr
+= sizeof(uint32_t);
2445 if (copy_to_user(ptr
, &tr
, sizeof(tr
)))
2449 binder_stat_br(proc
, thread
, cmd
);
2450 binder_debug(BINDER_DEBUG_TRANSACTION
,
2451 "binder: %d:%d %s %d %d:%d, cmd %d"
2452 "size %zd-%zd ptr %p-%p\n",
2453 proc
->pid
, thread
->pid
,
2454 (cmd
== BR_TRANSACTION
) ? "BR_TRANSACTION" :
2456 t
->debug_id
, t
->from
? t
->from
->proc
->pid
: 0,
2457 t
->from
? t
->from
->pid
: 0, cmd
,
2458 t
->buffer
->data_size
, t
->buffer
->offsets_size
,
2459 tr
.data
.ptr
.buffer
, tr
.data
.ptr
.offsets
);
2461 list_del(&t
->work
.entry
);
2462 t
->buffer
->allow_user_free
= 1;
2463 if (cmd
== BR_TRANSACTION
&& !(t
->flags
& TF_ONE_WAY
)) {
2464 t
->to_parent
= thread
->transaction_stack
;
2465 t
->to_thread
= thread
;
2466 thread
->transaction_stack
= t
;
2468 t
->buffer
->transaction
= NULL
;
2470 binder_stats_deleted(BINDER_STAT_TRANSACTION
);
2477 *consumed
= ptr
- buffer
;
2478 if (proc
->requested_threads
+ proc
->ready_threads
== 0 &&
2479 proc
->requested_threads_started
< proc
->max_threads
&&
2480 (thread
->looper
& (BINDER_LOOPER_STATE_REGISTERED
|
2481 BINDER_LOOPER_STATE_ENTERED
)) /* the user-space code fails to */
2482 /*spawn a new thread if we leave this out */) {
2483 proc
->requested_threads
++;
2484 binder_debug(BINDER_DEBUG_THREADS
,
2485 "binder: %d:%d BR_SPAWN_LOOPER\n",
2486 proc
->pid
, thread
->pid
);
2487 if (put_user(BR_SPAWN_LOOPER
, (uint32_t __user
*)buffer
))
2493 static void binder_release_work(struct list_head
*list
)
2495 struct binder_work
*w
;
2496 while (!list_empty(list
)) {
2497 w
= list_first_entry(list
, struct binder_work
, entry
);
2498 list_del_init(&w
->entry
);
2500 case BINDER_WORK_TRANSACTION
: {
2501 struct binder_transaction
*t
;
2503 t
= container_of(w
, struct binder_transaction
, work
);
2504 if (t
->buffer
->target_node
&& !(t
->flags
& TF_ONE_WAY
))
2505 binder_send_failed_reply(t
, BR_DEAD_REPLY
);
2507 case BINDER_WORK_TRANSACTION_COMPLETE
: {
2509 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE
);
2518 static struct binder_thread
*binder_get_thread(struct binder_proc
*proc
)
2520 struct binder_thread
*thread
= NULL
;
2521 struct rb_node
*parent
= NULL
;
2522 struct rb_node
**p
= &proc
->threads
.rb_node
;
2526 thread
= rb_entry(parent
, struct binder_thread
, rb_node
);
2528 if (current
->pid
< thread
->pid
)
2530 else if (current
->pid
> thread
->pid
)
2531 p
= &(*p
)->rb_right
;
2536 thread
= kzalloc(sizeof(*thread
), GFP_KERNEL
);
2539 binder_stats_created(BINDER_STAT_THREAD
);
2540 thread
->proc
= proc
;
2541 thread
->pid
= current
->pid
;
2542 init_waitqueue_head(&thread
->wait
);
2543 INIT_LIST_HEAD(&thread
->todo
);
2544 rb_link_node(&thread
->rb_node
, parent
, p
);
2545 rb_insert_color(&thread
->rb_node
, &proc
->threads
);
2546 thread
->looper
|= BINDER_LOOPER_STATE_NEED_RETURN
;
2547 thread
->return_error
= BR_OK
;
2548 thread
->return_error2
= BR_OK
;
2553 static int binder_free_thread(struct binder_proc
*proc
,
2554 struct binder_thread
*thread
)
2556 struct binder_transaction
*t
;
2557 struct binder_transaction
*send_reply
= NULL
;
2558 int active_transactions
= 0;
2560 rb_erase(&thread
->rb_node
, &proc
->threads
);
2561 t
= thread
->transaction_stack
;
2562 if (t
&& t
->to_thread
== thread
)
2565 active_transactions
++;
2566 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
2567 "binder: release %d:%d transaction %d "
2568 "%s, still active\n", proc
->pid
, thread
->pid
,
2570 (t
->to_thread
== thread
) ? "in" : "out");
2572 if (t
->to_thread
== thread
) {
2574 t
->to_thread
= NULL
;
2576 t
->buffer
->transaction
= NULL
;
2580 } else if (t
->from
== thread
) {
2587 binder_send_failed_reply(send_reply
, BR_DEAD_REPLY
);
2588 binder_release_work(&thread
->todo
);
2590 binder_stats_deleted(BINDER_STAT_THREAD
);
2591 return active_transactions
;
2594 static unsigned int binder_poll(struct file
*filp
,
2595 struct poll_table_struct
*wait
)
2597 struct binder_proc
*proc
= filp
->private_data
;
2598 struct binder_thread
*thread
= NULL
;
2599 int wait_for_proc_work
;
2601 mutex_lock(&binder_lock
);
2602 thread
= binder_get_thread(proc
);
2604 wait_for_proc_work
= thread
->transaction_stack
== NULL
&&
2605 list_empty(&thread
->todo
) && thread
->return_error
== BR_OK
;
2606 mutex_unlock(&binder_lock
);
2608 if (wait_for_proc_work
) {
2609 if (binder_has_proc_work(proc
, thread
))
2611 poll_wait(filp
, &proc
->wait
, wait
);
2612 if (binder_has_proc_work(proc
, thread
))
2615 if (binder_has_thread_work(thread
))
2617 poll_wait(filp
, &thread
->wait
, wait
);
2618 if (binder_has_thread_work(thread
))
2624 static long binder_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
2627 struct binder_proc
*proc
= filp
->private_data
;
2628 struct binder_thread
*thread
;
2629 unsigned int size
= _IOC_SIZE(cmd
);
2630 void __user
*ubuf
= (void __user
*)arg
;
2632 /*printk(KERN_INFO "binder_ioctl: %d:%d %x %lx\n", proc->pid, current->pid, cmd, arg);*/
2634 ret
= wait_event_interruptible(binder_user_error_wait
, binder_stop_on_user_error
< 2);
2638 mutex_lock(&binder_lock
);
2639 thread
= binder_get_thread(proc
);
2640 if (thread
== NULL
) {
2646 case BINDER_WRITE_READ
: {
2647 struct binder_write_read bwr
;
2648 if (size
!= sizeof(struct binder_write_read
)) {
2652 if (copy_from_user(&bwr
, ubuf
, sizeof(bwr
))) {
2656 binder_debug(BINDER_DEBUG_READ_WRITE
,
2657 "binder: %d:%d write %ld at %08lx, read %ld at %08lx\n",
2658 proc
->pid
, thread
->pid
, bwr
.write_size
, bwr
.write_buffer
,
2659 bwr
.read_size
, bwr
.read_buffer
);
2661 if (bwr
.write_size
> 0) {
2662 ret
= binder_thread_write(proc
, thread
, (void __user
*)bwr
.write_buffer
, bwr
.write_size
, &bwr
.write_consumed
);
2664 bwr
.read_consumed
= 0;
2665 if (copy_to_user(ubuf
, &bwr
, sizeof(bwr
)))
2670 if (bwr
.read_size
> 0) {
2671 ret
= binder_thread_read(proc
, thread
, (void __user
*)bwr
.read_buffer
, bwr
.read_size
, &bwr
.read_consumed
, filp
->f_flags
& O_NONBLOCK
);
2672 if (!list_empty(&proc
->todo
))
2673 wake_up_interruptible(&proc
->wait
);
2675 if (copy_to_user(ubuf
, &bwr
, sizeof(bwr
)))
2680 binder_debug(BINDER_DEBUG_READ_WRITE
,
2681 "binder: %d:%d wrote %ld of %ld, read return %ld of %ld\n",
2682 proc
->pid
, thread
->pid
, bwr
.write_consumed
, bwr
.write_size
,
2683 bwr
.read_consumed
, bwr
.read_size
);
2684 if (copy_to_user(ubuf
, &bwr
, sizeof(bwr
))) {
2690 case BINDER_SET_MAX_THREADS
:
2691 if (copy_from_user(&proc
->max_threads
, ubuf
, sizeof(proc
->max_threads
))) {
2696 case BINDER_SET_CONTEXT_MGR
:
2697 if (binder_context_mgr_node
!= NULL
) {
2698 printk(KERN_ERR
"binder: BINDER_SET_CONTEXT_MGR already set\n");
2702 if (binder_context_mgr_uid
!= -1) {
2703 if (binder_context_mgr_uid
!= current
->cred
->euid
) {
2704 printk(KERN_ERR
"binder: BINDER_SET_"
2705 "CONTEXT_MGR bad uid %d != %d\n",
2706 current
->cred
->euid
,
2707 binder_context_mgr_uid
);
2712 binder_context_mgr_uid
= current
->cred
->euid
;
2713 binder_context_mgr_node
= binder_new_node(proc
, NULL
, NULL
);
2714 if (binder_context_mgr_node
== NULL
) {
2718 binder_context_mgr_node
->local_weak_refs
++;
2719 binder_context_mgr_node
->local_strong_refs
++;
2720 binder_context_mgr_node
->has_strong_ref
= 1;
2721 binder_context_mgr_node
->has_weak_ref
= 1;
2723 case BINDER_THREAD_EXIT
:
2724 binder_debug(BINDER_DEBUG_THREADS
, "binder: %d:%d exit\n",
2725 proc
->pid
, thread
->pid
);
2726 binder_free_thread(proc
, thread
);
2729 case BINDER_VERSION
:
2730 if (size
!= sizeof(struct binder_version
)) {
2734 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION
, &((struct binder_version
*)ubuf
)->protocol_version
)) {
2746 thread
->looper
&= ~BINDER_LOOPER_STATE_NEED_RETURN
;
2747 mutex_unlock(&binder_lock
);
2748 wait_event_interruptible(binder_user_error_wait
, binder_stop_on_user_error
< 2);
2749 if (ret
&& ret
!= -ERESTARTSYS
)
2750 printk(KERN_INFO
"binder: %d:%d ioctl %x %lx returned %d\n", proc
->pid
, current
->pid
, cmd
, arg
, ret
);
2754 static void binder_vma_open(struct vm_area_struct
*vma
)
2756 struct binder_proc
*proc
= vma
->vm_private_data
;
2757 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
2758 "binder: %d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
2759 proc
->pid
, vma
->vm_start
, vma
->vm_end
,
2760 (vma
->vm_end
- vma
->vm_start
) / SZ_1K
, vma
->vm_flags
,
2761 (unsigned long)pgprot_val(vma
->vm_page_prot
));
2765 static void binder_vma_close(struct vm_area_struct
*vma
)
2767 struct binder_proc
*proc
= vma
->vm_private_data
;
2768 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
2769 "binder: %d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
2770 proc
->pid
, vma
->vm_start
, vma
->vm_end
,
2771 (vma
->vm_end
- vma
->vm_start
) / SZ_1K
, vma
->vm_flags
,
2772 (unsigned long)pgprot_val(vma
->vm_page_prot
));
2774 binder_defer_work(proc
, BINDER_DEFERRED_PUT_FILES
);
2777 static struct vm_operations_struct binder_vm_ops
= {
2778 .open
= binder_vma_open
,
2779 .close
= binder_vma_close
,
2782 static int binder_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
2785 struct vm_struct
*area
;
2786 struct binder_proc
*proc
= filp
->private_data
;
2787 const char *failure_string
;
2788 struct binder_buffer
*buffer
;
2790 if ((vma
->vm_end
- vma
->vm_start
) > SZ_4M
)
2791 vma
->vm_end
= vma
->vm_start
+ SZ_4M
;
2793 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
2794 "binder_mmap: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
2795 proc
->pid
, vma
->vm_start
, vma
->vm_end
,
2796 (vma
->vm_end
- vma
->vm_start
) / SZ_1K
, vma
->vm_flags
,
2797 (unsigned long)pgprot_val(vma
->vm_page_prot
));
2799 if (vma
->vm_flags
& FORBIDDEN_MMAP_FLAGS
) {
2801 failure_string
= "bad vm_flags";
2804 vma
->vm_flags
= (vma
->vm_flags
| VM_DONTCOPY
) & ~VM_MAYWRITE
;
2808 failure_string
= "already mapped";
2809 goto err_already_mapped
;
2812 area
= get_vm_area(vma
->vm_end
- vma
->vm_start
, VM_IOREMAP
);
2815 failure_string
= "get_vm_area";
2816 goto err_get_vm_area_failed
;
2818 proc
->buffer
= area
->addr
;
2819 proc
->user_buffer_offset
= vma
->vm_start
- (uintptr_t)proc
->buffer
;
2821 #ifdef CONFIG_CPU_CACHE_VIPT
2822 if (cache_is_vipt_aliasing()) {
2823 while (CACHE_COLOUR((vma
->vm_start
^ (uint32_t)proc
->buffer
))) {
2824 printk(KERN_INFO
"binder_mmap: %d %lx-%lx maps %p bad alignment\n", proc
->pid
, vma
->vm_start
, vma
->vm_end
, proc
->buffer
);
2825 vma
->vm_start
+= PAGE_SIZE
;
2829 proc
->pages
= kzalloc(sizeof(proc
->pages
[0]) * ((vma
->vm_end
- vma
->vm_start
) / PAGE_SIZE
), GFP_KERNEL
);
2830 if (proc
->pages
== NULL
) {
2832 failure_string
= "alloc page array";
2833 goto err_alloc_pages_failed
;
2835 proc
->buffer_size
= vma
->vm_end
- vma
->vm_start
;
2837 vma
->vm_ops
= &binder_vm_ops
;
2838 vma
->vm_private_data
= proc
;
2840 if (binder_update_page_range(proc
, 1, proc
->buffer
, proc
->buffer
+ PAGE_SIZE
, vma
)) {
2842 failure_string
= "alloc small buf";
2843 goto err_alloc_small_buf_failed
;
2845 buffer
= proc
->buffer
;
2846 INIT_LIST_HEAD(&proc
->buffers
);
2847 list_add(&buffer
->entry
, &proc
->buffers
);
2849 binder_insert_free_buffer(proc
, buffer
);
2850 proc
->free_async_space
= proc
->buffer_size
/ 2;
2852 proc
->files
= get_files_struct(current
);
2855 /*printk(KERN_INFO "binder_mmap: %d %lx-%lx maps %p\n",
2856 proc->pid, vma->vm_start, vma->vm_end, proc->buffer);*/
2859 err_alloc_small_buf_failed
:
2862 err_alloc_pages_failed
:
2863 vfree(proc
->buffer
);
2864 proc
->buffer
= NULL
;
2865 err_get_vm_area_failed
:
2868 printk(KERN_ERR
"binder_mmap: %d %lx-%lx %s failed %d\n",
2869 proc
->pid
, vma
->vm_start
, vma
->vm_end
, failure_string
, ret
);
2873 static int binder_open(struct inode
*nodp
, struct file
*filp
)
2875 struct binder_proc
*proc
;
2877 binder_debug(BINDER_DEBUG_OPEN_CLOSE
, "binder_open: %d:%d\n",
2878 current
->group_leader
->pid
, current
->pid
);
2880 proc
= kzalloc(sizeof(*proc
), GFP_KERNEL
);
2883 get_task_struct(current
);
2884 proc
->tsk
= current
;
2885 INIT_LIST_HEAD(&proc
->todo
);
2886 init_waitqueue_head(&proc
->wait
);
2887 proc
->default_priority
= task_nice(current
);
2888 mutex_lock(&binder_lock
);
2889 binder_stats_created(BINDER_STAT_PROC
);
2890 hlist_add_head(&proc
->proc_node
, &binder_procs
);
2891 proc
->pid
= current
->group_leader
->pid
;
2892 INIT_LIST_HEAD(&proc
->delivered_death
);
2893 filp
->private_data
= proc
;
2894 mutex_unlock(&binder_lock
);
2896 if (binder_debugfs_dir_entry_proc
) {
2898 snprintf(strbuf
, sizeof(strbuf
), "%u", proc
->pid
);
2899 proc
->debugfs_entry
= debugfs_create_file(strbuf
, S_IRUGO
,
2900 binder_debugfs_dir_entry_proc
, proc
, &binder_proc_fops
);
2906 static int binder_flush(struct file
*filp
, fl_owner_t id
)
2908 struct binder_proc
*proc
= filp
->private_data
;
2910 binder_defer_work(proc
, BINDER_DEFERRED_FLUSH
);
2915 static void binder_deferred_flush(struct binder_proc
*proc
)
2919 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
)) {
2920 struct binder_thread
*thread
= rb_entry(n
, struct binder_thread
, rb_node
);
2921 thread
->looper
|= BINDER_LOOPER_STATE_NEED_RETURN
;
2922 if (thread
->looper
& BINDER_LOOPER_STATE_WAITING
) {
2923 wake_up_interruptible(&thread
->wait
);
2927 wake_up_interruptible_all(&proc
->wait
);
2929 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
2930 "binder_flush: %d woke %d threads\n", proc
->pid
,
2934 static int binder_release(struct inode
*nodp
, struct file
*filp
)
2936 struct binder_proc
*proc
= filp
->private_data
;
2937 debugfs_remove(proc
->debugfs_entry
);
2938 binder_defer_work(proc
, BINDER_DEFERRED_RELEASE
);
2943 static void binder_deferred_release(struct binder_proc
*proc
)
2945 struct hlist_node
*pos
;
2946 struct binder_transaction
*t
;
2948 int threads
, nodes
, incoming_refs
, outgoing_refs
, buffers
, active_transactions
, page_count
;
2951 BUG_ON(proc
->files
);
2953 hlist_del(&proc
->proc_node
);
2954 if (binder_context_mgr_node
&& binder_context_mgr_node
->proc
== proc
) {
2955 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
2956 "binder_release: %d context_mgr_node gone\n",
2958 binder_context_mgr_node
= NULL
;
2962 active_transactions
= 0;
2963 while ((n
= rb_first(&proc
->threads
))) {
2964 struct binder_thread
*thread
= rb_entry(n
, struct binder_thread
, rb_node
);
2966 active_transactions
+= binder_free_thread(proc
, thread
);
2970 while ((n
= rb_first(&proc
->nodes
))) {
2971 struct binder_node
*node
= rb_entry(n
, struct binder_node
, rb_node
);
2974 rb_erase(&node
->rb_node
, &proc
->nodes
);
2975 list_del_init(&node
->work
.entry
);
2976 if (hlist_empty(&node
->refs
)) {
2978 binder_stats_deleted(BINDER_STAT_NODE
);
2980 struct binder_ref
*ref
;
2984 node
->local_strong_refs
= 0;
2985 node
->local_weak_refs
= 0;
2986 hlist_add_head(&node
->dead_node
, &binder_dead_nodes
);
2988 hlist_for_each_entry(ref
, pos
, &node
->refs
, node_entry
) {
2992 if (list_empty(&ref
->death
->work
.entry
)) {
2993 ref
->death
->work
.type
= BINDER_WORK_DEAD_BINDER
;
2994 list_add_tail(&ref
->death
->work
.entry
, &ref
->proc
->todo
);
2995 wake_up_interruptible(&ref
->proc
->wait
);
3000 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
3001 "binder: node %d now dead, "
3002 "refs %d, death %d\n", node
->debug_id
,
3003 incoming_refs
, death
);
3007 while ((n
= rb_first(&proc
->refs_by_desc
))) {
3008 struct binder_ref
*ref
= rb_entry(n
, struct binder_ref
,
3011 binder_delete_ref(ref
);
3013 binder_release_work(&proc
->todo
);
3016 while ((n
= rb_first(&proc
->allocated_buffers
))) {
3017 struct binder_buffer
*buffer
= rb_entry(n
, struct binder_buffer
,
3019 t
= buffer
->transaction
;
3022 buffer
->transaction
= NULL
;
3023 printk(KERN_ERR
"binder: release proc %d, "
3024 "transaction %d, not freed\n",
3025 proc
->pid
, t
->debug_id
);
3028 binder_free_buf(proc
, buffer
);
3032 binder_stats_deleted(BINDER_STAT_PROC
);
3037 for (i
= 0; i
< proc
->buffer_size
/ PAGE_SIZE
; i
++) {
3038 if (proc
->pages
[i
]) {
3039 void *page_addr
= proc
->buffer
+ i
* PAGE_SIZE
;
3040 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
3041 "binder_release: %d: "
3042 "page %d at %p not freed\n",
3045 unmap_kernel_range((unsigned long)page_addr
,
3047 __free_page(proc
->pages
[i
]);
3052 vfree(proc
->buffer
);
3055 put_task_struct(proc
->tsk
);
3057 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
3058 "binder_release: %d threads %d, nodes %d (ref %d), "
3059 "refs %d, active transactions %d, buffers %d, "
3061 proc
->pid
, threads
, nodes
, incoming_refs
, outgoing_refs
,
3062 active_transactions
, buffers
, page_count
);
3067 static void binder_deferred_func(struct work_struct
*work
)
3069 struct binder_proc
*proc
;
3070 struct files_struct
*files
;
3074 mutex_lock(&binder_lock
);
3075 mutex_lock(&binder_deferred_lock
);
3076 if (!hlist_empty(&binder_deferred_list
)) {
3077 proc
= hlist_entry(binder_deferred_list
.first
,
3078 struct binder_proc
, deferred_work_node
);
3079 hlist_del_init(&proc
->deferred_work_node
);
3080 defer
= proc
->deferred_work
;
3081 proc
->deferred_work
= 0;
3086 mutex_unlock(&binder_deferred_lock
);
3089 if (defer
& BINDER_DEFERRED_PUT_FILES
) {
3090 files
= proc
->files
;
3095 if (defer
& BINDER_DEFERRED_FLUSH
)
3096 binder_deferred_flush(proc
);
3098 if (defer
& BINDER_DEFERRED_RELEASE
)
3099 binder_deferred_release(proc
); /* frees proc */
3101 mutex_unlock(&binder_lock
);
3103 put_files_struct(files
);
3106 static DECLARE_WORK(binder_deferred_work
, binder_deferred_func
);
3109 binder_defer_work(struct binder_proc
*proc
, enum binder_deferred_state defer
)
3111 mutex_lock(&binder_deferred_lock
);
3112 proc
->deferred_work
|= defer
;
3113 if (hlist_unhashed(&proc
->deferred_work_node
)) {
3114 hlist_add_head(&proc
->deferred_work_node
,
3115 &binder_deferred_list
);
3116 queue_work(binder_deferred_workqueue
, &binder_deferred_work
);
3118 mutex_unlock(&binder_deferred_lock
);
3121 static void print_binder_transaction(struct seq_file
*m
, const char *prefix
,
3122 struct binder_transaction
*t
)
3125 "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %ld r%d",
3126 prefix
, t
->debug_id
, t
,
3127 t
->from
? t
->from
->proc
->pid
: 0,
3128 t
->from
? t
->from
->pid
: 0,
3129 t
->to_proc
? t
->to_proc
->pid
: 0,
3130 t
->to_thread
? t
->to_thread
->pid
: 0,
3131 t
->code
, t
->flags
, t
->priority
, t
->need_reply
);
3132 if (t
->buffer
== NULL
) {
3133 seq_puts(m
, " buffer free\n");
3136 if (t
->buffer
->target_node
)
3137 seq_printf(m
, " node %d",
3138 t
->buffer
->target_node
->debug_id
);
3139 seq_printf(m
, " size %zd:%zd data %p\n",
3140 t
->buffer
->data_size
, t
->buffer
->offsets_size
,
3144 static void print_binder_buffer(struct seq_file
*m
, const char *prefix
,
3145 struct binder_buffer
*buffer
)
3147 seq_printf(m
, "%s %d: %p size %zd:%zd %s\n",
3148 prefix
, buffer
->debug_id
, buffer
->data
,
3149 buffer
->data_size
, buffer
->offsets_size
,
3150 buffer
->transaction
? "active" : "delivered");
3153 static void print_binder_work(struct seq_file
*m
, const char *prefix
,
3154 const char *transaction_prefix
,
3155 struct binder_work
*w
)
3157 struct binder_node
*node
;
3158 struct binder_transaction
*t
;
3161 case BINDER_WORK_TRANSACTION
:
3162 t
= container_of(w
, struct binder_transaction
, work
);
3163 print_binder_transaction(m
, transaction_prefix
, t
);
3165 case BINDER_WORK_TRANSACTION_COMPLETE
:
3166 seq_printf(m
, "%stransaction complete\n", prefix
);
3168 case BINDER_WORK_NODE
:
3169 node
= container_of(w
, struct binder_node
, work
);
3170 seq_printf(m
, "%snode work %d: u%p c%p\n",
3171 prefix
, node
->debug_id
, node
->ptr
, node
->cookie
);
3173 case BINDER_WORK_DEAD_BINDER
:
3174 seq_printf(m
, "%shas dead binder\n", prefix
);
3176 case BINDER_WORK_DEAD_BINDER_AND_CLEAR
:
3177 seq_printf(m
, "%shas cleared dead binder\n", prefix
);
3179 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION
:
3180 seq_printf(m
, "%shas cleared death notification\n", prefix
);
3183 seq_printf(m
, "%sunknown work: type %d\n", prefix
, w
->type
);
3188 static void print_binder_thread(struct seq_file
*m
,
3189 struct binder_thread
*thread
,
3192 struct binder_transaction
*t
;
3193 struct binder_work
*w
;
3194 size_t start_pos
= m
->count
;
3197 seq_printf(m
, " thread %d: l %02x\n", thread
->pid
, thread
->looper
);
3198 header_pos
= m
->count
;
3199 t
= thread
->transaction_stack
;
3201 if (t
->from
== thread
) {
3202 print_binder_transaction(m
,
3203 " outgoing transaction", t
);
3205 } else if (t
->to_thread
== thread
) {
3206 print_binder_transaction(m
,
3207 " incoming transaction", t
);
3210 print_binder_transaction(m
, " bad transaction", t
);
3214 list_for_each_entry(w
, &thread
->todo
, entry
) {
3215 print_binder_work(m
, " ", " pending transaction", w
);
3217 if (!print_always
&& m
->count
== header_pos
)
3218 m
->count
= start_pos
;
3221 static void print_binder_node(struct seq_file
*m
, struct binder_node
*node
)
3223 struct binder_ref
*ref
;
3224 struct hlist_node
*pos
;
3225 struct binder_work
*w
;
3229 hlist_for_each_entry(ref
, pos
, &node
->refs
, node_entry
)
3232 seq_printf(m
, " node %d: u%p c%p hs %d hw %d ls %d lw %d is %d iw %d",
3233 node
->debug_id
, node
->ptr
, node
->cookie
,
3234 node
->has_strong_ref
, node
->has_weak_ref
,
3235 node
->local_strong_refs
, node
->local_weak_refs
,
3236 node
->internal_strong_refs
, count
);
3238 seq_puts(m
, " proc");
3239 hlist_for_each_entry(ref
, pos
, &node
->refs
, node_entry
)
3240 seq_printf(m
, " %d", ref
->proc
->pid
);
3243 list_for_each_entry(w
, &node
->async_todo
, entry
)
3244 print_binder_work(m
, " ",
3245 " pending async transaction", w
);
3248 static void print_binder_ref(struct seq_file
*m
, struct binder_ref
*ref
)
3250 seq_printf(m
, " ref %d: desc %d %snode %d s %d w %d d %p\n",
3251 ref
->debug_id
, ref
->desc
, ref
->node
->proc
? "" : "dead ",
3252 ref
->node
->debug_id
, ref
->strong
, ref
->weak
, ref
->death
);
3255 static void print_binder_proc(struct seq_file
*m
,
3256 struct binder_proc
*proc
, int print_all
)
3258 struct binder_work
*w
;
3260 size_t start_pos
= m
->count
;
3263 seq_printf(m
, "proc %d\n", proc
->pid
);
3264 header_pos
= m
->count
;
3266 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
))
3267 print_binder_thread(m
, rb_entry(n
, struct binder_thread
,
3268 rb_node
), print_all
);
3269 for (n
= rb_first(&proc
->nodes
); n
!= NULL
; n
= rb_next(n
)) {
3270 struct binder_node
*node
= rb_entry(n
, struct binder_node
,
3272 if (print_all
|| node
->has_async_transaction
)
3273 print_binder_node(m
, node
);
3276 for (n
= rb_first(&proc
->refs_by_desc
);
3279 print_binder_ref(m
, rb_entry(n
, struct binder_ref
,
3282 for (n
= rb_first(&proc
->allocated_buffers
); n
!= NULL
; n
= rb_next(n
))
3283 print_binder_buffer(m
, " buffer",
3284 rb_entry(n
, struct binder_buffer
, rb_node
));
3285 list_for_each_entry(w
, &proc
->todo
, entry
)
3286 print_binder_work(m
, " ", " pending transaction", w
);
3287 list_for_each_entry(w
, &proc
->delivered_death
, entry
) {
3288 seq_puts(m
, " has delivered dead binder\n");
3291 if (!print_all
&& m
->count
== header_pos
)
3292 m
->count
= start_pos
;
3295 static const char *binder_return_strings
[] = {
3300 "BR_ACQUIRE_RESULT",
3302 "BR_TRANSACTION_COMPLETE",
3307 "BR_ATTEMPT_ACQUIRE",
3312 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
3316 static const char *binder_command_strings
[] = {
3319 "BC_ACQUIRE_RESULT",
3327 "BC_ATTEMPT_ACQUIRE",
3328 "BC_REGISTER_LOOPER",
3331 "BC_REQUEST_DEATH_NOTIFICATION",
3332 "BC_CLEAR_DEATH_NOTIFICATION",
3333 "BC_DEAD_BINDER_DONE"
3336 static const char *binder_objstat_strings
[] = {
3343 "transaction_complete"
3346 static void print_binder_stats(struct seq_file
*m
, const char *prefix
,
3347 struct binder_stats
*stats
)
3351 BUILD_BUG_ON(ARRAY_SIZE(stats
->bc
) !=
3352 ARRAY_SIZE(binder_command_strings
));
3353 for (i
= 0; i
< ARRAY_SIZE(stats
->bc
); i
++) {
3355 seq_printf(m
, "%s%s: %d\n", prefix
,
3356 binder_command_strings
[i
], stats
->bc
[i
]);
3359 BUILD_BUG_ON(ARRAY_SIZE(stats
->br
) !=
3360 ARRAY_SIZE(binder_return_strings
));
3361 for (i
= 0; i
< ARRAY_SIZE(stats
->br
); i
++) {
3363 seq_printf(m
, "%s%s: %d\n", prefix
,
3364 binder_return_strings
[i
], stats
->br
[i
]);
3367 BUILD_BUG_ON(ARRAY_SIZE(stats
->obj_created
) !=
3368 ARRAY_SIZE(binder_objstat_strings
));
3369 BUILD_BUG_ON(ARRAY_SIZE(stats
->obj_created
) !=
3370 ARRAY_SIZE(stats
->obj_deleted
));
3371 for (i
= 0; i
< ARRAY_SIZE(stats
->obj_created
); i
++) {
3372 if (stats
->obj_created
[i
] || stats
->obj_deleted
[i
])
3373 seq_printf(m
, "%s%s: active %d total %d\n", prefix
,
3374 binder_objstat_strings
[i
],
3375 stats
->obj_created
[i
] - stats
->obj_deleted
[i
],
3376 stats
->obj_created
[i
]);
3380 static void print_binder_proc_stats(struct seq_file
*m
,
3381 struct binder_proc
*proc
)
3383 struct binder_work
*w
;
3385 int count
, strong
, weak
;
3387 seq_printf(m
, "proc %d\n", proc
->pid
);
3389 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
))
3391 seq_printf(m
, " threads: %d\n", count
);
3392 seq_printf(m
, " requested threads: %d+%d/%d\n"
3393 " ready threads %d\n"
3394 " free async space %zd\n", proc
->requested_threads
,
3395 proc
->requested_threads_started
, proc
->max_threads
,
3396 proc
->ready_threads
, proc
->free_async_space
);
3398 for (n
= rb_first(&proc
->nodes
); n
!= NULL
; n
= rb_next(n
))
3400 seq_printf(m
, " nodes: %d\n", count
);
3404 for (n
= rb_first(&proc
->refs_by_desc
); n
!= NULL
; n
= rb_next(n
)) {
3405 struct binder_ref
*ref
= rb_entry(n
, struct binder_ref
,
3408 strong
+= ref
->strong
;
3411 seq_printf(m
, " refs: %d s %d w %d\n", count
, strong
, weak
);
3414 for (n
= rb_first(&proc
->allocated_buffers
); n
!= NULL
; n
= rb_next(n
))
3416 seq_printf(m
, " buffers: %d\n", count
);
3419 list_for_each_entry(w
, &proc
->todo
, entry
) {
3421 case BINDER_WORK_TRANSACTION
:
3428 seq_printf(m
, " pending transactions: %d\n", count
);
3430 print_binder_stats(m
, " ", &proc
->stats
);
3434 static int binder_state_show(struct seq_file
*m
, void *unused
)
3436 struct binder_proc
*proc
;
3437 struct hlist_node
*pos
;
3438 struct binder_node
*node
;
3439 int do_lock
= !binder_debug_no_lock
;
3442 mutex_lock(&binder_lock
);
3444 seq_puts(m
, "binder state:\n");
3446 if (!hlist_empty(&binder_dead_nodes
))
3447 seq_puts(m
, "dead nodes:\n");
3448 hlist_for_each_entry(node
, pos
, &binder_dead_nodes
, dead_node
)
3449 print_binder_node(m
, node
);
3451 hlist_for_each_entry(proc
, pos
, &binder_procs
, proc_node
)
3452 print_binder_proc(m
, proc
, 1);
3454 mutex_unlock(&binder_lock
);
3458 static int binder_stats_show(struct seq_file
*m
, void *unused
)
3460 struct binder_proc
*proc
;
3461 struct hlist_node
*pos
;
3462 int do_lock
= !binder_debug_no_lock
;
3465 mutex_lock(&binder_lock
);
3467 seq_puts(m
, "binder stats:\n");
3469 print_binder_stats(m
, "", &binder_stats
);
3471 hlist_for_each_entry(proc
, pos
, &binder_procs
, proc_node
)
3472 print_binder_proc_stats(m
, proc
);
3474 mutex_unlock(&binder_lock
);
3478 static int binder_transactions_show(struct seq_file
*m
, void *unused
)
3480 struct binder_proc
*proc
;
3481 struct hlist_node
*pos
;
3482 int do_lock
= !binder_debug_no_lock
;
3485 mutex_lock(&binder_lock
);
3487 seq_puts(m
, "binder transactions:\n");
3488 hlist_for_each_entry(proc
, pos
, &binder_procs
, proc_node
)
3489 print_binder_proc(m
, proc
, 0);
3491 mutex_unlock(&binder_lock
);
3495 static int binder_proc_show(struct seq_file
*m
, void *unused
)
3497 struct binder_proc
*proc
= m
->private;
3498 int do_lock
= !binder_debug_no_lock
;
3501 mutex_lock(&binder_lock
);
3502 seq_puts(m
, "binder proc state:\n");
3503 print_binder_proc(m
, proc
, 1);
3505 mutex_unlock(&binder_lock
);
3509 static void print_binder_transaction_log_entry(struct seq_file
*m
,
3510 struct binder_transaction_log_entry
*e
)
3513 "%d: %s from %d:%d to %d:%d node %d handle %d size %d:%d\n",
3514 e
->debug_id
, (e
->call_type
== 2) ? "reply" :
3515 ((e
->call_type
== 1) ? "async" : "call "), e
->from_proc
,
3516 e
->from_thread
, e
->to_proc
, e
->to_thread
, e
->to_node
,
3517 e
->target_handle
, e
->data_size
, e
->offsets_size
);
3520 static int binder_transaction_log_show(struct seq_file
*m
, void *unused
)
3522 struct binder_transaction_log
*log
= m
->private;
3526 for (i
= log
->next
; i
< ARRAY_SIZE(log
->entry
); i
++)
3527 print_binder_transaction_log_entry(m
, &log
->entry
[i
]);
3529 for (i
= 0; i
< log
->next
; i
++)
3530 print_binder_transaction_log_entry(m
, &log
->entry
[i
]);
3534 static const struct file_operations binder_fops
= {
3535 .owner
= THIS_MODULE
,
3536 .poll
= binder_poll
,
3537 .unlocked_ioctl
= binder_ioctl
,
3538 .mmap
= binder_mmap
,
3539 .open
= binder_open
,
3540 .flush
= binder_flush
,
3541 .release
= binder_release
,
3544 static struct miscdevice binder_miscdev
= {
3545 .minor
= MISC_DYNAMIC_MINOR
,
3547 .fops
= &binder_fops
3550 BINDER_DEBUG_ENTRY(state
);
3551 BINDER_DEBUG_ENTRY(stats
);
3552 BINDER_DEBUG_ENTRY(transactions
);
3553 BINDER_DEBUG_ENTRY(transaction_log
);
3555 static int __init
binder_init(void)
3559 binder_deferred_workqueue
= create_singlethread_workqueue("binder");
3560 if (!binder_deferred_workqueue
)
3563 binder_debugfs_dir_entry_root
= debugfs_create_dir("binder", NULL
);
3564 if (binder_debugfs_dir_entry_root
)
3565 binder_debugfs_dir_entry_proc
= debugfs_create_dir("proc",
3566 binder_debugfs_dir_entry_root
);
3567 ret
= misc_register(&binder_miscdev
);
3568 if (binder_debugfs_dir_entry_root
) {
3569 debugfs_create_file("state",
3571 binder_debugfs_dir_entry_root
,
3573 &binder_state_fops
);
3574 debugfs_create_file("stats",
3576 binder_debugfs_dir_entry_root
,
3578 &binder_stats_fops
);
3579 debugfs_create_file("transactions",
3581 binder_debugfs_dir_entry_root
,
3583 &binder_transactions_fops
);
3584 debugfs_create_file("transaction_log",
3586 binder_debugfs_dir_entry_root
,
3587 &binder_transaction_log
,
3588 &binder_transaction_log_fops
);
3589 debugfs_create_file("failed_transaction_log",
3591 binder_debugfs_dir_entry_root
,
3592 &binder_transaction_log_failed
,
3593 &binder_transaction_log_fops
);
3598 device_initcall(binder_init
);
3600 MODULE_LICENSE("GPL v2");