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/proc_fs.h>
30 #include <linux/rbtree.h>
31 #include <linux/sched.h>
32 #include <linux/uaccess.h>
33 #include <linux/vmalloc.h>
37 static DEFINE_MUTEX(binder_lock
);
38 static DEFINE_MUTEX(binder_deferred_lock
);
40 static HLIST_HEAD(binder_procs
);
41 static HLIST_HEAD(binder_deferred_list
);
42 static HLIST_HEAD(binder_dead_nodes
);
44 static struct proc_dir_entry
*binder_proc_dir_entry_root
;
45 static struct proc_dir_entry
*binder_proc_dir_entry_proc
;
46 static struct binder_node
*binder_context_mgr_node
;
47 static uid_t binder_context_mgr_uid
= -1;
48 static int binder_last_id
;
50 static int binder_read_proc_proc(char *page
, char **start
, off_t off
,
51 int count
, int *eof
, void *data
);
53 /* This is only defined in include/asm-arm/sizes.h */
59 #define SZ_4M 0x400000
62 #define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
64 #define BINDER_SMALL_BUF_SIZE (PAGE_SIZE * 64)
67 BINDER_DEBUG_USER_ERROR
= 1U << 0,
68 BINDER_DEBUG_FAILED_TRANSACTION
= 1U << 1,
69 BINDER_DEBUG_DEAD_TRANSACTION
= 1U << 2,
70 BINDER_DEBUG_OPEN_CLOSE
= 1U << 3,
71 BINDER_DEBUG_DEAD_BINDER
= 1U << 4,
72 BINDER_DEBUG_DEATH_NOTIFICATION
= 1U << 5,
73 BINDER_DEBUG_READ_WRITE
= 1U << 6,
74 BINDER_DEBUG_USER_REFS
= 1U << 7,
75 BINDER_DEBUG_THREADS
= 1U << 8,
76 BINDER_DEBUG_TRANSACTION
= 1U << 9,
77 BINDER_DEBUG_TRANSACTION_COMPLETE
= 1U << 10,
78 BINDER_DEBUG_FREE_BUFFER
= 1U << 11,
79 BINDER_DEBUG_INTERNAL_REFS
= 1U << 12,
80 BINDER_DEBUG_BUFFER_ALLOC
= 1U << 13,
81 BINDER_DEBUG_PRIORITY_CAP
= 1U << 14,
82 BINDER_DEBUG_BUFFER_ALLOC_ASYNC
= 1U << 15,
84 static uint32_t binder_debug_mask
= BINDER_DEBUG_USER_ERROR
|
85 BINDER_DEBUG_FAILED_TRANSACTION
| BINDER_DEBUG_DEAD_TRANSACTION
;
86 module_param_named(debug_mask
, binder_debug_mask
, uint
, S_IWUSR
| S_IRUGO
);
88 static int binder_debug_no_lock
;
89 module_param_named(proc_no_lock
, binder_debug_no_lock
, bool, S_IWUSR
| S_IRUGO
);
91 static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait
);
92 static int binder_stop_on_user_error
;
94 static int binder_set_stop_on_user_error(const char *val
,
95 struct kernel_param
*kp
)
98 ret
= param_set_int(val
, kp
);
99 if (binder_stop_on_user_error
< 2)
100 wake_up(&binder_user_error_wait
);
103 module_param_call(stop_on_user_error
, binder_set_stop_on_user_error
,
104 param_get_int
, &binder_stop_on_user_error
, S_IWUSR
| S_IRUGO
);
106 #define binder_debug(mask, x...) \
108 if (binder_debug_mask & mask) \
109 printk(KERN_INFO x); \
112 #define binder_user_error(x...) \
114 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
115 printk(KERN_INFO x); \
116 if (binder_stop_on_user_error) \
117 binder_stop_on_user_error = 2; \
120 enum binder_stat_types
{
126 BINDER_STAT_TRANSACTION
,
127 BINDER_STAT_TRANSACTION_COMPLETE
,
131 struct binder_stats
{
132 int br
[_IOC_NR(BR_FAILED_REPLY
) + 1];
133 int bc
[_IOC_NR(BC_DEAD_BINDER_DONE
) + 1];
134 int obj_created
[BINDER_STAT_COUNT
];
135 int obj_deleted
[BINDER_STAT_COUNT
];
138 static struct binder_stats binder_stats
;
140 static inline void binder_stats_deleted(enum binder_stat_types type
)
142 binder_stats
.obj_deleted
[type
]++;
145 static inline void binder_stats_created(enum binder_stat_types type
)
147 binder_stats
.obj_created
[type
]++;
150 struct binder_transaction_log_entry
{
162 struct binder_transaction_log
{
165 struct binder_transaction_log_entry entry
[32];
167 static struct binder_transaction_log binder_transaction_log
;
168 static struct binder_transaction_log binder_transaction_log_failed
;
170 static struct binder_transaction_log_entry
*binder_transaction_log_add(
171 struct binder_transaction_log
*log
)
173 struct binder_transaction_log_entry
*e
;
174 e
= &log
->entry
[log
->next
];
175 memset(e
, 0, sizeof(*e
));
177 if (log
->next
== ARRAY_SIZE(log
->entry
)) {
185 struct list_head entry
;
187 BINDER_WORK_TRANSACTION
= 1,
188 BINDER_WORK_TRANSACTION_COMPLETE
,
190 BINDER_WORK_DEAD_BINDER
,
191 BINDER_WORK_DEAD_BINDER_AND_CLEAR
,
192 BINDER_WORK_CLEAR_DEATH_NOTIFICATION
,
198 struct binder_work work
;
200 struct rb_node rb_node
;
201 struct hlist_node dead_node
;
203 struct binder_proc
*proc
;
204 struct hlist_head refs
;
205 int internal_strong_refs
;
207 int local_strong_refs
;
210 unsigned has_strong_ref
:1;
211 unsigned pending_strong_ref
:1;
212 unsigned has_weak_ref
:1;
213 unsigned pending_weak_ref
:1;
214 unsigned has_async_transaction
:1;
215 unsigned accept_fds
:1;
216 unsigned min_priority
:8;
217 struct list_head async_todo
;
220 struct binder_ref_death
{
221 struct binder_work work
;
226 /* Lookups needed: */
227 /* node + proc => ref (transaction) */
228 /* desc + proc => ref (transaction, inc/dec ref) */
229 /* node => refs + procs (proc exit) */
231 struct rb_node rb_node_desc
;
232 struct rb_node rb_node_node
;
233 struct hlist_node node_entry
;
234 struct binder_proc
*proc
;
235 struct binder_node
*node
;
239 struct binder_ref_death
*death
;
242 struct binder_buffer
{
243 struct list_head entry
; /* free and allocated entries by addesss */
244 struct rb_node rb_node
; /* free entry by size or allocated entry */
247 unsigned allow_user_free
:1;
248 unsigned async_transaction
:1;
249 unsigned debug_id
:29;
251 struct binder_transaction
*transaction
;
253 struct binder_node
*target_node
;
259 enum binder_deferred_state
{
260 BINDER_DEFERRED_PUT_FILES
= 0x01,
261 BINDER_DEFERRED_FLUSH
= 0x02,
262 BINDER_DEFERRED_RELEASE
= 0x04,
266 struct hlist_node proc_node
;
267 struct rb_root threads
;
268 struct rb_root nodes
;
269 struct rb_root refs_by_desc
;
270 struct rb_root refs_by_node
;
272 struct vm_area_struct
*vma
;
273 struct task_struct
*tsk
;
274 struct files_struct
*files
;
275 struct hlist_node deferred_work_node
;
278 ptrdiff_t user_buffer_offset
;
280 struct list_head buffers
;
281 struct rb_root free_buffers
;
282 struct rb_root allocated_buffers
;
283 size_t free_async_space
;
287 uint32_t buffer_free
;
288 struct list_head todo
;
289 wait_queue_head_t wait
;
290 struct binder_stats stats
;
291 struct list_head delivered_death
;
293 int requested_threads
;
294 int requested_threads_started
;
296 long default_priority
;
300 BINDER_LOOPER_STATE_REGISTERED
= 0x01,
301 BINDER_LOOPER_STATE_ENTERED
= 0x02,
302 BINDER_LOOPER_STATE_EXITED
= 0x04,
303 BINDER_LOOPER_STATE_INVALID
= 0x08,
304 BINDER_LOOPER_STATE_WAITING
= 0x10,
305 BINDER_LOOPER_STATE_NEED_RETURN
= 0x20
308 struct binder_thread
{
309 struct binder_proc
*proc
;
310 struct rb_node rb_node
;
313 struct binder_transaction
*transaction_stack
;
314 struct list_head todo
;
315 uint32_t return_error
; /* Write failed, return error code in read buf */
316 uint32_t return_error2
; /* Write failed, return error code in read */
317 /* buffer. Used when sending a reply to a dead process that */
318 /* we are also waiting on */
319 wait_queue_head_t wait
;
320 struct binder_stats stats
;
323 struct binder_transaction
{
325 struct binder_work work
;
326 struct binder_thread
*from
;
327 struct binder_transaction
*from_parent
;
328 struct binder_proc
*to_proc
;
329 struct binder_thread
*to_thread
;
330 struct binder_transaction
*to_parent
;
331 unsigned need_reply
:1;
332 /* unsigned is_dead:1; */ /* not used at the moment */
334 struct binder_buffer
*buffer
;
343 binder_defer_work(struct binder_proc
*proc
, enum binder_deferred_state defer
);
346 * copied from get_unused_fd_flags
348 int task_get_unused_fd_flags(struct binder_proc
*proc
, int flags
)
350 struct files_struct
*files
= proc
->files
;
353 unsigned long rlim_cur
;
360 spin_lock(&files
->file_lock
);
363 fdt
= files_fdtable(files
);
364 fd
= find_next_zero_bit(fdt
->open_fds
->fds_bits
, fdt
->max_fds
,
368 * N.B. For clone tasks sharing a files structure, this test
369 * will limit the total number of files that can be opened.
372 if (lock_task_sighand(proc
->tsk
, &irqs
)) {
373 rlim_cur
= proc
->tsk
->signal
->rlim
[RLIMIT_NOFILE
].rlim_cur
;
374 unlock_task_sighand(proc
->tsk
, &irqs
);
379 /* Do we need to expand the fd array or fd set? */
380 error
= expand_files(files
, fd
);
386 * If we needed to expand the fs array we
387 * might have blocked - try again.
393 FD_SET(fd
, fdt
->open_fds
);
394 if (flags
& O_CLOEXEC
)
395 FD_SET(fd
, fdt
->close_on_exec
);
397 FD_CLR(fd
, fdt
->close_on_exec
);
398 files
->next_fd
= fd
+ 1;
401 if (fdt
->fd
[fd
] != NULL
) {
402 printk(KERN_WARNING
"get_unused_fd: slot %d not NULL!\n", fd
);
409 spin_unlock(&files
->file_lock
);
414 * copied from fd_install
416 static void task_fd_install(
417 struct binder_proc
*proc
, unsigned int fd
, struct file
*file
)
419 struct files_struct
*files
= proc
->files
;
425 spin_lock(&files
->file_lock
);
426 fdt
= files_fdtable(files
);
427 BUG_ON(fdt
->fd
[fd
] != NULL
);
428 rcu_assign_pointer(fdt
->fd
[fd
], file
);
429 spin_unlock(&files
->file_lock
);
433 * copied from __put_unused_fd in open.c
435 static void __put_unused_fd(struct files_struct
*files
, unsigned int fd
)
437 struct fdtable
*fdt
= files_fdtable(files
);
438 __FD_CLR(fd
, fdt
->open_fds
);
439 if (fd
< files
->next_fd
)
444 * copied from sys_close
446 static long task_close_fd(struct binder_proc
*proc
, unsigned int fd
)
449 struct files_struct
*files
= proc
->files
;
456 spin_lock(&files
->file_lock
);
457 fdt
= files_fdtable(files
);
458 if (fd
>= fdt
->max_fds
)
463 rcu_assign_pointer(fdt
->fd
[fd
], NULL
);
464 FD_CLR(fd
, fdt
->close_on_exec
);
465 __put_unused_fd(files
, fd
);
466 spin_unlock(&files
->file_lock
);
467 retval
= filp_close(filp
, files
);
469 /* can't restart close syscall because file table entry was cleared */
470 if (unlikely(retval
== -ERESTARTSYS
||
471 retval
== -ERESTARTNOINTR
||
472 retval
== -ERESTARTNOHAND
||
473 retval
== -ERESTART_RESTARTBLOCK
))
479 spin_unlock(&files
->file_lock
);
483 static void binder_set_nice(long nice
)
486 if (can_nice(current
, nice
)) {
487 set_user_nice(current
, nice
);
490 min_nice
= 20 - current
->signal
->rlim
[RLIMIT_NICE
].rlim_cur
;
491 binder_debug(BINDER_DEBUG_PRIORITY_CAP
,
492 "binder: %d: nice value %ld not allowed use "
493 "%ld instead\n", current
->pid
, nice
, min_nice
);
494 set_user_nice(current
, min_nice
);
497 binder_user_error("binder: %d RLIMIT_NICE not set\n", current
->pid
);
500 static size_t binder_buffer_size(struct binder_proc
*proc
,
501 struct binder_buffer
*buffer
)
503 if (list_is_last(&buffer
->entry
, &proc
->buffers
))
504 return proc
->buffer
+ proc
->buffer_size
- (void *)buffer
->data
;
506 return (size_t)list_entry(buffer
->entry
.next
,
507 struct binder_buffer
, entry
) - (size_t)buffer
->data
;
510 static void binder_insert_free_buffer(struct binder_proc
*proc
,
511 struct binder_buffer
*new_buffer
)
513 struct rb_node
**p
= &proc
->free_buffers
.rb_node
;
514 struct rb_node
*parent
= NULL
;
515 struct binder_buffer
*buffer
;
517 size_t new_buffer_size
;
519 BUG_ON(!new_buffer
->free
);
521 new_buffer_size
= binder_buffer_size(proc
, new_buffer
);
523 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
524 "binder: %d: add free buffer, size %zd, "
525 "at %p\n", proc
->pid
, new_buffer_size
, new_buffer
);
529 buffer
= rb_entry(parent
, struct binder_buffer
, rb_node
);
530 BUG_ON(!buffer
->free
);
532 buffer_size
= binder_buffer_size(proc
, buffer
);
534 if (new_buffer_size
< buffer_size
)
535 p
= &parent
->rb_left
;
537 p
= &parent
->rb_right
;
539 rb_link_node(&new_buffer
->rb_node
, parent
, p
);
540 rb_insert_color(&new_buffer
->rb_node
, &proc
->free_buffers
);
543 static void binder_insert_allocated_buffer(struct binder_proc
*proc
,
544 struct binder_buffer
*new_buffer
)
546 struct rb_node
**p
= &proc
->allocated_buffers
.rb_node
;
547 struct rb_node
*parent
= NULL
;
548 struct binder_buffer
*buffer
;
550 BUG_ON(new_buffer
->free
);
554 buffer
= rb_entry(parent
, struct binder_buffer
, rb_node
);
555 BUG_ON(buffer
->free
);
557 if (new_buffer
< buffer
)
558 p
= &parent
->rb_left
;
559 else if (new_buffer
> buffer
)
560 p
= &parent
->rb_right
;
564 rb_link_node(&new_buffer
->rb_node
, parent
, p
);
565 rb_insert_color(&new_buffer
->rb_node
, &proc
->allocated_buffers
);
568 static struct binder_buffer
*binder_buffer_lookup(struct binder_proc
*proc
,
569 void __user
*user_ptr
)
571 struct rb_node
*n
= proc
->allocated_buffers
.rb_node
;
572 struct binder_buffer
*buffer
;
573 struct binder_buffer
*kern_ptr
;
575 kern_ptr
= user_ptr
- proc
->user_buffer_offset
576 - offsetof(struct binder_buffer
, data
);
579 buffer
= rb_entry(n
, struct binder_buffer
, rb_node
);
580 BUG_ON(buffer
->free
);
582 if (kern_ptr
< buffer
)
584 else if (kern_ptr
> buffer
)
592 static int binder_update_page_range(struct binder_proc
*proc
, int allocate
,
593 void *start
, void *end
,
594 struct vm_area_struct
*vma
)
597 unsigned long user_page_addr
;
598 struct vm_struct tmp_area
;
600 struct mm_struct
*mm
;
602 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
603 "binder: %d: %s pages %p-%p\n", proc
->pid
,
604 allocate
? "allocate" : "free", start
, end
);
612 mm
= get_task_mm(proc
->tsk
);
615 down_write(&mm
->mmap_sem
);
623 printk(KERN_ERR
"binder: %d: binder_alloc_buf failed to "
624 "map pages in userspace, no vma\n", proc
->pid
);
628 for (page_addr
= start
; page_addr
< end
; page_addr
+= PAGE_SIZE
) {
630 struct page
**page_array_ptr
;
631 page
= &proc
->pages
[(page_addr
- proc
->buffer
) / PAGE_SIZE
];
634 *page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
636 printk(KERN_ERR
"binder: %d: binder_alloc_buf failed "
637 "for page at %p\n", proc
->pid
, page_addr
);
638 goto err_alloc_page_failed
;
640 tmp_area
.addr
= page_addr
;
641 tmp_area
.size
= PAGE_SIZE
+ PAGE_SIZE
/* guard page? */;
642 page_array_ptr
= page
;
643 ret
= map_vm_area(&tmp_area
, PAGE_KERNEL
, &page_array_ptr
);
645 printk(KERN_ERR
"binder: %d: binder_alloc_buf failed "
646 "to map page at %p in kernel\n",
647 proc
->pid
, page_addr
);
648 goto err_map_kernel_failed
;
651 (uintptr_t)page_addr
+ proc
->user_buffer_offset
;
652 ret
= vm_insert_page(vma
, user_page_addr
, page
[0]);
654 printk(KERN_ERR
"binder: %d: binder_alloc_buf failed "
655 "to map page at %lx in userspace\n",
656 proc
->pid
, user_page_addr
);
657 goto err_vm_insert_page_failed
;
659 /* vm_insert_page does not seem to increment the refcount */
662 up_write(&mm
->mmap_sem
);
668 for (page_addr
= end
- PAGE_SIZE
; page_addr
>= start
;
669 page_addr
-= PAGE_SIZE
) {
670 page
= &proc
->pages
[(page_addr
- proc
->buffer
) / PAGE_SIZE
];
672 zap_page_range(vma
, (uintptr_t)page_addr
+
673 proc
->user_buffer_offset
, PAGE_SIZE
, NULL
);
674 err_vm_insert_page_failed
:
675 unmap_kernel_range((unsigned long)page_addr
, PAGE_SIZE
);
676 err_map_kernel_failed
:
679 err_alloc_page_failed
:
684 up_write(&mm
->mmap_sem
);
690 static struct binder_buffer
*binder_alloc_buf(struct binder_proc
*proc
,
692 size_t offsets_size
, int is_async
)
694 struct rb_node
*n
= proc
->free_buffers
.rb_node
;
695 struct binder_buffer
*buffer
;
697 struct rb_node
*best_fit
= NULL
;
702 if (proc
->vma
== NULL
) {
703 printk(KERN_ERR
"binder: %d: binder_alloc_buf, no vma\n",
708 size
= ALIGN(data_size
, sizeof(void *)) +
709 ALIGN(offsets_size
, sizeof(void *));
711 if (size
< data_size
|| size
< offsets_size
) {
712 binder_user_error("binder: %d: got transaction with invalid "
713 "size %zd-%zd\n", proc
->pid
, data_size
, offsets_size
);
718 proc
->free_async_space
< size
+ sizeof(struct binder_buffer
)) {
719 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
720 "binder: %d: binder_alloc_buf size %zd"
721 "failed, no async space left\n", proc
->pid
, size
);
726 buffer
= rb_entry(n
, struct binder_buffer
, rb_node
);
727 BUG_ON(!buffer
->free
);
728 buffer_size
= binder_buffer_size(proc
, buffer
);
730 if (size
< buffer_size
) {
733 } else if (size
> buffer_size
)
740 if (best_fit
== NULL
) {
741 printk(KERN_ERR
"binder: %d: binder_alloc_buf size %zd failed, "
742 "no address space\n", proc
->pid
, size
);
746 buffer
= rb_entry(best_fit
, struct binder_buffer
, rb_node
);
747 buffer_size
= binder_buffer_size(proc
, buffer
);
750 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
751 "binder: %d: binder_alloc_buf size %zd got buff"
752 "er %p size %zd\n", proc
->pid
, size
, buffer
, buffer_size
);
755 (void *)(((uintptr_t)buffer
->data
+ buffer_size
) & PAGE_MASK
);
757 if (size
+ sizeof(struct binder_buffer
) + 4 >= buffer_size
)
758 buffer_size
= size
; /* no room for other buffers */
760 buffer_size
= size
+ sizeof(struct binder_buffer
);
763 (void *)PAGE_ALIGN((uintptr_t)buffer
->data
+ buffer_size
);
764 if (end_page_addr
> has_page_addr
)
765 end_page_addr
= has_page_addr
;
766 if (binder_update_page_range(proc
, 1,
767 (void *)PAGE_ALIGN((uintptr_t)buffer
->data
), end_page_addr
, NULL
))
770 rb_erase(best_fit
, &proc
->free_buffers
);
772 binder_insert_allocated_buffer(proc
, buffer
);
773 if (buffer_size
!= size
) {
774 struct binder_buffer
*new_buffer
= (void *)buffer
->data
+ size
;
775 list_add(&new_buffer
->entry
, &buffer
->entry
);
776 new_buffer
->free
= 1;
777 binder_insert_free_buffer(proc
, new_buffer
);
779 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
780 "binder: %d: binder_alloc_buf size %zd got "
781 "%p\n", proc
->pid
, size
, buffer
);
782 buffer
->data_size
= data_size
;
783 buffer
->offsets_size
= offsets_size
;
784 buffer
->async_transaction
= is_async
;
786 proc
->free_async_space
-= size
+ sizeof(struct binder_buffer
);
787 binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC
,
788 "binder: %d: binder_alloc_buf size %zd "
789 "async free %zd\n", proc
->pid
, size
,
790 proc
->free_async_space
);
796 static void *buffer_start_page(struct binder_buffer
*buffer
)
798 return (void *)((uintptr_t)buffer
& PAGE_MASK
);
801 static void *buffer_end_page(struct binder_buffer
*buffer
)
803 return (void *)(((uintptr_t)(buffer
+ 1) - 1) & PAGE_MASK
);
806 static void binder_delete_free_buffer(struct binder_proc
*proc
,
807 struct binder_buffer
*buffer
)
809 struct binder_buffer
*prev
, *next
= NULL
;
810 int free_page_end
= 1;
811 int free_page_start
= 1;
813 BUG_ON(proc
->buffers
.next
== &buffer
->entry
);
814 prev
= list_entry(buffer
->entry
.prev
, struct binder_buffer
, entry
);
816 if (buffer_end_page(prev
) == buffer_start_page(buffer
)) {
818 if (buffer_end_page(prev
) == buffer_end_page(buffer
))
820 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
821 "binder: %d: merge free, buffer %p "
822 "share page with %p\n", proc
->pid
, buffer
, prev
);
825 if (!list_is_last(&buffer
->entry
, &proc
->buffers
)) {
826 next
= list_entry(buffer
->entry
.next
,
827 struct binder_buffer
, entry
);
828 if (buffer_start_page(next
) == buffer_end_page(buffer
)) {
830 if (buffer_start_page(next
) ==
831 buffer_start_page(buffer
))
833 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
834 "binder: %d: merge free, buffer"
835 " %p share page with %p\n", proc
->pid
,
839 list_del(&buffer
->entry
);
840 if (free_page_start
|| free_page_end
) {
841 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
842 "binder: %d: merge free, buffer %p do "
843 "not share page%s%s with with %p or %p\n",
844 proc
->pid
, buffer
, free_page_start
? "" : " end",
845 free_page_end
? "" : " start", prev
, next
);
846 binder_update_page_range(proc
, 0, free_page_start
?
847 buffer_start_page(buffer
) : buffer_end_page(buffer
),
848 (free_page_end
? buffer_end_page(buffer
) :
849 buffer_start_page(buffer
)) + PAGE_SIZE
, NULL
);
853 static void binder_free_buf(struct binder_proc
*proc
,
854 struct binder_buffer
*buffer
)
856 size_t size
, buffer_size
;
858 buffer_size
= binder_buffer_size(proc
, buffer
);
860 size
= ALIGN(buffer
->data_size
, sizeof(void *)) +
861 ALIGN(buffer
->offsets_size
, sizeof(void *));
863 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
864 "binder: %d: binder_free_buf %p size %zd buffer"
865 "_size %zd\n", proc
->pid
, buffer
, size
, buffer_size
);
867 BUG_ON(buffer
->free
);
868 BUG_ON(size
> buffer_size
);
869 BUG_ON(buffer
->transaction
!= NULL
);
870 BUG_ON((void *)buffer
< proc
->buffer
);
871 BUG_ON((void *)buffer
> proc
->buffer
+ proc
->buffer_size
);
873 if (buffer
->async_transaction
) {
874 proc
->free_async_space
+= size
+ sizeof(struct binder_buffer
);
876 binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC
,
877 "binder: %d: binder_free_buf size %zd "
878 "async free %zd\n", proc
->pid
, size
,
879 proc
->free_async_space
);
882 binder_update_page_range(proc
, 0,
883 (void *)PAGE_ALIGN((uintptr_t)buffer
->data
),
884 (void *)(((uintptr_t)buffer
->data
+ buffer_size
) & PAGE_MASK
),
886 rb_erase(&buffer
->rb_node
, &proc
->allocated_buffers
);
888 if (!list_is_last(&buffer
->entry
, &proc
->buffers
)) {
889 struct binder_buffer
*next
= list_entry(buffer
->entry
.next
,
890 struct binder_buffer
, entry
);
892 rb_erase(&next
->rb_node
, &proc
->free_buffers
);
893 binder_delete_free_buffer(proc
, next
);
896 if (proc
->buffers
.next
!= &buffer
->entry
) {
897 struct binder_buffer
*prev
= list_entry(buffer
->entry
.prev
,
898 struct binder_buffer
, entry
);
900 binder_delete_free_buffer(proc
, buffer
);
901 rb_erase(&prev
->rb_node
, &proc
->free_buffers
);
905 binder_insert_free_buffer(proc
, buffer
);
908 static struct binder_node
*binder_get_node(struct binder_proc
*proc
,
911 struct rb_node
*n
= proc
->nodes
.rb_node
;
912 struct binder_node
*node
;
915 node
= rb_entry(n
, struct binder_node
, rb_node
);
919 else if (ptr
> node
->ptr
)
927 static struct binder_node
*binder_new_node(struct binder_proc
*proc
,
931 struct rb_node
**p
= &proc
->nodes
.rb_node
;
932 struct rb_node
*parent
= NULL
;
933 struct binder_node
*node
;
937 node
= rb_entry(parent
, struct binder_node
, rb_node
);
941 else if (ptr
> node
->ptr
)
947 node
= kzalloc(sizeof(*node
), GFP_KERNEL
);
950 binder_stats_created(BINDER_STAT_NODE
);
951 rb_link_node(&node
->rb_node
, parent
, p
);
952 rb_insert_color(&node
->rb_node
, &proc
->nodes
);
953 node
->debug_id
= ++binder_last_id
;
956 node
->cookie
= cookie
;
957 node
->work
.type
= BINDER_WORK_NODE
;
958 INIT_LIST_HEAD(&node
->work
.entry
);
959 INIT_LIST_HEAD(&node
->async_todo
);
960 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
961 "binder: %d:%d node %d u%p c%p created\n",
962 proc
->pid
, current
->pid
, node
->debug_id
,
963 node
->ptr
, node
->cookie
);
967 static int binder_inc_node(struct binder_node
*node
, int strong
, int internal
,
968 struct list_head
*target_list
)
972 if (target_list
== NULL
&&
973 node
->internal_strong_refs
== 0 &&
974 !(node
== binder_context_mgr_node
&&
975 node
->has_strong_ref
)) {
976 printk(KERN_ERR
"binder: invalid inc strong "
977 "node for %d\n", node
->debug_id
);
980 node
->internal_strong_refs
++;
982 node
->local_strong_refs
++;
983 if (!node
->has_strong_ref
&& target_list
) {
984 list_del_init(&node
->work
.entry
);
985 list_add_tail(&node
->work
.entry
, target_list
);
989 node
->local_weak_refs
++;
990 if (!node
->has_weak_ref
&& list_empty(&node
->work
.entry
)) {
991 if (target_list
== NULL
) {
992 printk(KERN_ERR
"binder: invalid inc weak node "
993 "for %d\n", node
->debug_id
);
996 list_add_tail(&node
->work
.entry
, target_list
);
1002 static int binder_dec_node(struct binder_node
*node
, int strong
, int internal
)
1006 node
->internal_strong_refs
--;
1008 node
->local_strong_refs
--;
1009 if (node
->local_strong_refs
|| node
->internal_strong_refs
)
1013 node
->local_weak_refs
--;
1014 if (node
->local_weak_refs
|| !hlist_empty(&node
->refs
))
1017 if (node
->proc
&& (node
->has_strong_ref
|| node
->has_weak_ref
)) {
1018 if (list_empty(&node
->work
.entry
)) {
1019 list_add_tail(&node
->work
.entry
, &node
->proc
->todo
);
1020 wake_up_interruptible(&node
->proc
->wait
);
1023 if (hlist_empty(&node
->refs
) && !node
->local_strong_refs
&&
1024 !node
->local_weak_refs
) {
1025 list_del_init(&node
->work
.entry
);
1027 rb_erase(&node
->rb_node
, &node
->proc
->nodes
);
1028 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1029 "binder: refless node %d deleted\n",
1032 hlist_del(&node
->dead_node
);
1033 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1034 "binder: dead node %d deleted\n",
1038 binder_stats_deleted(BINDER_STAT_NODE
);
1046 static struct binder_ref
*binder_get_ref(struct binder_proc
*proc
,
1049 struct rb_node
*n
= proc
->refs_by_desc
.rb_node
;
1050 struct binder_ref
*ref
;
1053 ref
= rb_entry(n
, struct binder_ref
, rb_node_desc
);
1055 if (desc
< ref
->desc
)
1057 else if (desc
> ref
->desc
)
1065 static struct binder_ref
*binder_get_ref_for_node(struct binder_proc
*proc
,
1066 struct binder_node
*node
)
1069 struct rb_node
**p
= &proc
->refs_by_node
.rb_node
;
1070 struct rb_node
*parent
= NULL
;
1071 struct binder_ref
*ref
, *new_ref
;
1075 ref
= rb_entry(parent
, struct binder_ref
, rb_node_node
);
1077 if (node
< ref
->node
)
1079 else if (node
> ref
->node
)
1080 p
= &(*p
)->rb_right
;
1084 new_ref
= kzalloc(sizeof(*ref
), GFP_KERNEL
);
1085 if (new_ref
== NULL
)
1087 binder_stats_created(BINDER_STAT_REF
);
1088 new_ref
->debug_id
= ++binder_last_id
;
1089 new_ref
->proc
= proc
;
1090 new_ref
->node
= node
;
1091 rb_link_node(&new_ref
->rb_node_node
, parent
, p
);
1092 rb_insert_color(&new_ref
->rb_node_node
, &proc
->refs_by_node
);
1094 new_ref
->desc
= (node
== binder_context_mgr_node
) ? 0 : 1;
1095 for (n
= rb_first(&proc
->refs_by_desc
); n
!= NULL
; n
= rb_next(n
)) {
1096 ref
= rb_entry(n
, struct binder_ref
, rb_node_desc
);
1097 if (ref
->desc
> new_ref
->desc
)
1099 new_ref
->desc
= ref
->desc
+ 1;
1102 p
= &proc
->refs_by_desc
.rb_node
;
1105 ref
= rb_entry(parent
, struct binder_ref
, rb_node_desc
);
1107 if (new_ref
->desc
< ref
->desc
)
1109 else if (new_ref
->desc
> ref
->desc
)
1110 p
= &(*p
)->rb_right
;
1114 rb_link_node(&new_ref
->rb_node_desc
, parent
, p
);
1115 rb_insert_color(&new_ref
->rb_node_desc
, &proc
->refs_by_desc
);
1117 hlist_add_head(&new_ref
->node_entry
, &node
->refs
);
1119 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1120 "binder: %d new ref %d desc %d for "
1121 "node %d\n", proc
->pid
, new_ref
->debug_id
,
1122 new_ref
->desc
, node
->debug_id
);
1124 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1125 "binder: %d new ref %d desc %d for "
1126 "dead node\n", proc
->pid
, new_ref
->debug_id
,
1132 static void binder_delete_ref(struct binder_ref
*ref
)
1134 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1135 "binder: %d delete ref %d desc %d for "
1136 "node %d\n", ref
->proc
->pid
, ref
->debug_id
,
1137 ref
->desc
, ref
->node
->debug_id
);
1139 rb_erase(&ref
->rb_node_desc
, &ref
->proc
->refs_by_desc
);
1140 rb_erase(&ref
->rb_node_node
, &ref
->proc
->refs_by_node
);
1142 binder_dec_node(ref
->node
, 1, 1);
1143 hlist_del(&ref
->node_entry
);
1144 binder_dec_node(ref
->node
, 0, 1);
1146 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
1147 "binder: %d delete ref %d desc %d "
1148 "has death notification\n", ref
->proc
->pid
,
1149 ref
->debug_id
, ref
->desc
);
1150 list_del(&ref
->death
->work
.entry
);
1152 binder_stats_deleted(BINDER_STAT_DEATH
);
1155 binder_stats_deleted(BINDER_STAT_REF
);
1158 static int binder_inc_ref(struct binder_ref
*ref
, int strong
,
1159 struct list_head
*target_list
)
1163 if (ref
->strong
== 0) {
1164 ret
= binder_inc_node(ref
->node
, 1, 1, target_list
);
1170 if (ref
->weak
== 0) {
1171 ret
= binder_inc_node(ref
->node
, 0, 1, target_list
);
1181 static int binder_dec_ref(struct binder_ref
*ref
, int strong
)
1184 if (ref
->strong
== 0) {
1185 binder_user_error("binder: %d invalid dec strong, "
1186 "ref %d desc %d s %d w %d\n",
1187 ref
->proc
->pid
, ref
->debug_id
,
1188 ref
->desc
, ref
->strong
, ref
->weak
);
1192 if (ref
->strong
== 0) {
1194 ret
= binder_dec_node(ref
->node
, strong
, 1);
1199 if (ref
->weak
== 0) {
1200 binder_user_error("binder: %d invalid dec weak, "
1201 "ref %d desc %d s %d w %d\n",
1202 ref
->proc
->pid
, ref
->debug_id
,
1203 ref
->desc
, ref
->strong
, ref
->weak
);
1208 if (ref
->strong
== 0 && ref
->weak
== 0)
1209 binder_delete_ref(ref
);
1213 static void binder_pop_transaction(struct binder_thread
*target_thread
,
1214 struct binder_transaction
*t
)
1216 if (target_thread
) {
1217 BUG_ON(target_thread
->transaction_stack
!= t
);
1218 BUG_ON(target_thread
->transaction_stack
->from
!= target_thread
);
1219 target_thread
->transaction_stack
=
1220 target_thread
->transaction_stack
->from_parent
;
1225 t
->buffer
->transaction
= NULL
;
1227 binder_stats_deleted(BINDER_STAT_TRANSACTION
);
1230 static void binder_send_failed_reply(struct binder_transaction
*t
,
1231 uint32_t error_code
)
1233 struct binder_thread
*target_thread
;
1234 BUG_ON(t
->flags
& TF_ONE_WAY
);
1236 target_thread
= t
->from
;
1237 if (target_thread
) {
1238 if (target_thread
->return_error
!= BR_OK
&&
1239 target_thread
->return_error2
== BR_OK
) {
1240 target_thread
->return_error2
=
1241 target_thread
->return_error
;
1242 target_thread
->return_error
= BR_OK
;
1244 if (target_thread
->return_error
== BR_OK
) {
1245 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
1246 "binder: send failed reply for "
1247 "transaction %d to %d:%d\n",
1248 t
->debug_id
, target_thread
->proc
->pid
,
1249 target_thread
->pid
);
1251 binder_pop_transaction(target_thread
, t
);
1252 target_thread
->return_error
= error_code
;
1253 wake_up_interruptible(&target_thread
->wait
);
1255 printk(KERN_ERR
"binder: reply failed, target "
1256 "thread, %d:%d, has error code %d "
1257 "already\n", target_thread
->proc
->pid
,
1259 target_thread
->return_error
);
1263 struct binder_transaction
*next
= t
->from_parent
;
1265 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
1266 "binder: send failed reply "
1267 "for transaction %d, target dead\n",
1270 binder_pop_transaction(target_thread
, t
);
1272 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
1273 "binder: reply failed,"
1274 " no target thread at root\n");
1278 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
1279 "binder: reply failed, no target "
1280 "thread -- retry %d\n", t
->debug_id
);
1285 static void binder_transaction_buffer_release(struct binder_proc
*proc
,
1286 struct binder_buffer
*buffer
,
1289 size_t *offp
, *off_end
;
1290 int debug_id
= buffer
->debug_id
;
1292 binder_debug(BINDER_DEBUG_TRANSACTION
,
1293 "binder: %d buffer release %d, size %zd-%zd, failed at %p\n",
1294 proc
->pid
, buffer
->debug_id
,
1295 buffer
->data_size
, buffer
->offsets_size
, failed_at
);
1297 if (buffer
->target_node
)
1298 binder_dec_node(buffer
->target_node
, 1, 0);
1300 offp
= (size_t *)(buffer
->data
+ ALIGN(buffer
->data_size
, sizeof(void *)));
1302 off_end
= failed_at
;
1304 off_end
= (void *)offp
+ buffer
->offsets_size
;
1305 for (; offp
< off_end
; offp
++) {
1306 struct flat_binder_object
*fp
;
1307 if (*offp
> buffer
->data_size
- sizeof(*fp
) ||
1308 buffer
->data_size
< sizeof(*fp
) ||
1309 !IS_ALIGNED(*offp
, sizeof(void *))) {
1310 printk(KERN_ERR
"binder: transaction release %d bad"
1311 "offset %zd, size %zd\n", debug_id
,
1312 *offp
, buffer
->data_size
);
1315 fp
= (struct flat_binder_object
*)(buffer
->data
+ *offp
);
1317 case BINDER_TYPE_BINDER
:
1318 case BINDER_TYPE_WEAK_BINDER
: {
1319 struct binder_node
*node
= binder_get_node(proc
, fp
->binder
);
1321 printk(KERN_ERR
"binder: transaction release %d"
1322 " bad node %p\n", debug_id
, fp
->binder
);
1325 binder_debug(BINDER_DEBUG_TRANSACTION
,
1327 node
->debug_id
, node
->ptr
);
1328 binder_dec_node(node
, fp
->type
== BINDER_TYPE_BINDER
, 0);
1330 case BINDER_TYPE_HANDLE
:
1331 case BINDER_TYPE_WEAK_HANDLE
: {
1332 struct binder_ref
*ref
= binder_get_ref(proc
, fp
->handle
);
1334 printk(KERN_ERR
"binder: transaction release %d"
1335 " bad handle %ld\n", debug_id
,
1339 binder_debug(BINDER_DEBUG_TRANSACTION
,
1340 " ref %d desc %d (node %d)\n",
1341 ref
->debug_id
, ref
->desc
, ref
->node
->debug_id
);
1342 binder_dec_ref(ref
, fp
->type
== BINDER_TYPE_HANDLE
);
1345 case BINDER_TYPE_FD
:
1346 binder_debug(BINDER_DEBUG_TRANSACTION
,
1347 " fd %ld\n", fp
->handle
);
1349 task_close_fd(proc
, fp
->handle
);
1353 printk(KERN_ERR
"binder: transaction release %d bad "
1354 "object type %lx\n", debug_id
, fp
->type
);
1360 static void binder_transaction(struct binder_proc
*proc
,
1361 struct binder_thread
*thread
,
1362 struct binder_transaction_data
*tr
, int reply
)
1364 struct binder_transaction
*t
;
1365 struct binder_work
*tcomplete
;
1366 size_t *offp
, *off_end
;
1367 struct binder_proc
*target_proc
;
1368 struct binder_thread
*target_thread
= NULL
;
1369 struct binder_node
*target_node
= NULL
;
1370 struct list_head
*target_list
;
1371 wait_queue_head_t
*target_wait
;
1372 struct binder_transaction
*in_reply_to
= NULL
;
1373 struct binder_transaction_log_entry
*e
;
1374 uint32_t return_error
;
1376 e
= binder_transaction_log_add(&binder_transaction_log
);
1377 e
->call_type
= reply
? 2 : !!(tr
->flags
& TF_ONE_WAY
);
1378 e
->from_proc
= proc
->pid
;
1379 e
->from_thread
= thread
->pid
;
1380 e
->target_handle
= tr
->target
.handle
;
1381 e
->data_size
= tr
->data_size
;
1382 e
->offsets_size
= tr
->offsets_size
;
1385 in_reply_to
= thread
->transaction_stack
;
1386 if (in_reply_to
== NULL
) {
1387 binder_user_error("binder: %d:%d got reply transaction "
1388 "with no transaction stack\n",
1389 proc
->pid
, thread
->pid
);
1390 return_error
= BR_FAILED_REPLY
;
1391 goto err_empty_call_stack
;
1393 binder_set_nice(in_reply_to
->saved_priority
);
1394 if (in_reply_to
->to_thread
!= thread
) {
1395 binder_user_error("binder: %d:%d got reply transaction "
1396 "with bad transaction stack,"
1397 " transaction %d has target %d:%d\n",
1398 proc
->pid
, thread
->pid
, in_reply_to
->debug_id
,
1399 in_reply_to
->to_proc
?
1400 in_reply_to
->to_proc
->pid
: 0,
1401 in_reply_to
->to_thread
?
1402 in_reply_to
->to_thread
->pid
: 0);
1403 return_error
= BR_FAILED_REPLY
;
1405 goto err_bad_call_stack
;
1407 thread
->transaction_stack
= in_reply_to
->to_parent
;
1408 target_thread
= in_reply_to
->from
;
1409 if (target_thread
== NULL
) {
1410 return_error
= BR_DEAD_REPLY
;
1411 goto err_dead_binder
;
1413 if (target_thread
->transaction_stack
!= in_reply_to
) {
1414 binder_user_error("binder: %d:%d got reply transaction "
1415 "with bad target transaction stack %d, "
1417 proc
->pid
, thread
->pid
,
1418 target_thread
->transaction_stack
?
1419 target_thread
->transaction_stack
->debug_id
: 0,
1420 in_reply_to
->debug_id
);
1421 return_error
= BR_FAILED_REPLY
;
1423 target_thread
= NULL
;
1424 goto err_dead_binder
;
1426 target_proc
= target_thread
->proc
;
1428 if (tr
->target
.handle
) {
1429 struct binder_ref
*ref
;
1430 ref
= binder_get_ref(proc
, tr
->target
.handle
);
1432 binder_user_error("binder: %d:%d got "
1433 "transaction to invalid handle\n",
1434 proc
->pid
, thread
->pid
);
1435 return_error
= BR_FAILED_REPLY
;
1436 goto err_invalid_target_handle
;
1438 target_node
= ref
->node
;
1440 target_node
= binder_context_mgr_node
;
1441 if (target_node
== NULL
) {
1442 return_error
= BR_DEAD_REPLY
;
1443 goto err_no_context_mgr_node
;
1446 e
->to_node
= target_node
->debug_id
;
1447 target_proc
= target_node
->proc
;
1448 if (target_proc
== NULL
) {
1449 return_error
= BR_DEAD_REPLY
;
1450 goto err_dead_binder
;
1452 if (!(tr
->flags
& TF_ONE_WAY
) && thread
->transaction_stack
) {
1453 struct binder_transaction
*tmp
;
1454 tmp
= thread
->transaction_stack
;
1455 if (tmp
->to_thread
!= thread
) {
1456 binder_user_error("binder: %d:%d got new "
1457 "transaction with bad transaction stack"
1458 ", transaction %d has target %d:%d\n",
1459 proc
->pid
, thread
->pid
, tmp
->debug_id
,
1460 tmp
->to_proc
? tmp
->to_proc
->pid
: 0,
1462 tmp
->to_thread
->pid
: 0);
1463 return_error
= BR_FAILED_REPLY
;
1464 goto err_bad_call_stack
;
1467 if (tmp
->from
&& tmp
->from
->proc
== target_proc
)
1468 target_thread
= tmp
->from
;
1469 tmp
= tmp
->from_parent
;
1473 if (target_thread
) {
1474 e
->to_thread
= target_thread
->pid
;
1475 target_list
= &target_thread
->todo
;
1476 target_wait
= &target_thread
->wait
;
1478 target_list
= &target_proc
->todo
;
1479 target_wait
= &target_proc
->wait
;
1481 e
->to_proc
= target_proc
->pid
;
1483 /* TODO: reuse incoming transaction for reply */
1484 t
= kzalloc(sizeof(*t
), GFP_KERNEL
);
1486 return_error
= BR_FAILED_REPLY
;
1487 goto err_alloc_t_failed
;
1489 binder_stats_created(BINDER_STAT_TRANSACTION
);
1491 tcomplete
= kzalloc(sizeof(*tcomplete
), GFP_KERNEL
);
1492 if (tcomplete
== NULL
) {
1493 return_error
= BR_FAILED_REPLY
;
1494 goto err_alloc_tcomplete_failed
;
1496 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE
);
1498 t
->debug_id
= ++binder_last_id
;
1499 e
->debug_id
= t
->debug_id
;
1502 binder_debug(BINDER_DEBUG_TRANSACTION
,
1503 "binder: %d:%d BC_REPLY %d -> %d:%d, "
1504 "data %p-%p size %zd-%zd\n",
1505 proc
->pid
, thread
->pid
, t
->debug_id
,
1506 target_proc
->pid
, target_thread
->pid
,
1507 tr
->data
.ptr
.buffer
, tr
->data
.ptr
.offsets
,
1508 tr
->data_size
, tr
->offsets_size
);
1510 binder_debug(BINDER_DEBUG_TRANSACTION
,
1511 "binder: %d:%d BC_TRANSACTION %d -> "
1512 "%d - node %d, data %p-%p size %zd-%zd\n",
1513 proc
->pid
, thread
->pid
, t
->debug_id
,
1514 target_proc
->pid
, target_node
->debug_id
,
1515 tr
->data
.ptr
.buffer
, tr
->data
.ptr
.offsets
,
1516 tr
->data_size
, tr
->offsets_size
);
1518 if (!reply
&& !(tr
->flags
& TF_ONE_WAY
))
1522 t
->sender_euid
= proc
->tsk
->cred
->euid
;
1523 t
->to_proc
= target_proc
;
1524 t
->to_thread
= target_thread
;
1526 t
->flags
= tr
->flags
;
1527 t
->priority
= task_nice(current
);
1528 t
->buffer
= binder_alloc_buf(target_proc
, tr
->data_size
,
1529 tr
->offsets_size
, !reply
&& (t
->flags
& TF_ONE_WAY
));
1530 if (t
->buffer
== NULL
) {
1531 return_error
= BR_FAILED_REPLY
;
1532 goto err_binder_alloc_buf_failed
;
1534 t
->buffer
->allow_user_free
= 0;
1535 t
->buffer
->debug_id
= t
->debug_id
;
1536 t
->buffer
->transaction
= t
;
1537 t
->buffer
->target_node
= target_node
;
1539 binder_inc_node(target_node
, 1, 0, NULL
);
1541 offp
= (size_t *)(t
->buffer
->data
+ ALIGN(tr
->data_size
, sizeof(void *)));
1543 if (copy_from_user(t
->buffer
->data
, tr
->data
.ptr
.buffer
, tr
->data_size
)) {
1544 binder_user_error("binder: %d:%d got transaction with invalid "
1545 "data ptr\n", proc
->pid
, thread
->pid
);
1546 return_error
= BR_FAILED_REPLY
;
1547 goto err_copy_data_failed
;
1549 if (copy_from_user(offp
, tr
->data
.ptr
.offsets
, tr
->offsets_size
)) {
1550 binder_user_error("binder: %d:%d got transaction with invalid "
1551 "offsets ptr\n", proc
->pid
, thread
->pid
);
1552 return_error
= BR_FAILED_REPLY
;
1553 goto err_copy_data_failed
;
1555 if (!IS_ALIGNED(tr
->offsets_size
, sizeof(size_t))) {
1556 binder_user_error("binder: %d:%d got transaction with "
1557 "invalid offsets size, %zd\n",
1558 proc
->pid
, thread
->pid
, tr
->offsets_size
);
1559 return_error
= BR_FAILED_REPLY
;
1560 goto err_bad_offset
;
1562 off_end
= (void *)offp
+ tr
->offsets_size
;
1563 for (; offp
< off_end
; offp
++) {
1564 struct flat_binder_object
*fp
;
1565 if (*offp
> t
->buffer
->data_size
- sizeof(*fp
) ||
1566 t
->buffer
->data_size
< sizeof(*fp
) ||
1567 !IS_ALIGNED(*offp
, sizeof(void *))) {
1568 binder_user_error("binder: %d:%d got transaction with "
1569 "invalid offset, %zd\n",
1570 proc
->pid
, thread
->pid
, *offp
);
1571 return_error
= BR_FAILED_REPLY
;
1572 goto err_bad_offset
;
1574 fp
= (struct flat_binder_object
*)(t
->buffer
->data
+ *offp
);
1576 case BINDER_TYPE_BINDER
:
1577 case BINDER_TYPE_WEAK_BINDER
: {
1578 struct binder_ref
*ref
;
1579 struct binder_node
*node
= binder_get_node(proc
, fp
->binder
);
1581 node
= binder_new_node(proc
, fp
->binder
, fp
->cookie
);
1583 return_error
= BR_FAILED_REPLY
;
1584 goto err_binder_new_node_failed
;
1586 node
->min_priority
= fp
->flags
& FLAT_BINDER_FLAG_PRIORITY_MASK
;
1587 node
->accept_fds
= !!(fp
->flags
& FLAT_BINDER_FLAG_ACCEPTS_FDS
);
1589 if (fp
->cookie
!= node
->cookie
) {
1590 binder_user_error("binder: %d:%d sending u%p "
1591 "node %d, cookie mismatch %p != %p\n",
1592 proc
->pid
, thread
->pid
,
1593 fp
->binder
, node
->debug_id
,
1594 fp
->cookie
, node
->cookie
);
1595 goto err_binder_get_ref_for_node_failed
;
1597 ref
= binder_get_ref_for_node(target_proc
, node
);
1599 return_error
= BR_FAILED_REPLY
;
1600 goto err_binder_get_ref_for_node_failed
;
1602 if (fp
->type
== BINDER_TYPE_BINDER
)
1603 fp
->type
= BINDER_TYPE_HANDLE
;
1605 fp
->type
= BINDER_TYPE_WEAK_HANDLE
;
1606 fp
->handle
= ref
->desc
;
1607 binder_inc_ref(ref
, fp
->type
== BINDER_TYPE_HANDLE
,
1610 binder_debug(BINDER_DEBUG_TRANSACTION
,
1611 " node %d u%p -> ref %d desc %d\n",
1612 node
->debug_id
, node
->ptr
, ref
->debug_id
,
1615 case BINDER_TYPE_HANDLE
:
1616 case BINDER_TYPE_WEAK_HANDLE
: {
1617 struct binder_ref
*ref
= binder_get_ref(proc
, fp
->handle
);
1619 binder_user_error("binder: %d:%d got "
1620 "transaction with invalid "
1621 "handle, %ld\n", proc
->pid
,
1622 thread
->pid
, fp
->handle
);
1623 return_error
= BR_FAILED_REPLY
;
1624 goto err_binder_get_ref_failed
;
1626 if (ref
->node
->proc
== target_proc
) {
1627 if (fp
->type
== BINDER_TYPE_HANDLE
)
1628 fp
->type
= BINDER_TYPE_BINDER
;
1630 fp
->type
= BINDER_TYPE_WEAK_BINDER
;
1631 fp
->binder
= ref
->node
->ptr
;
1632 fp
->cookie
= ref
->node
->cookie
;
1633 binder_inc_node(ref
->node
, fp
->type
== BINDER_TYPE_BINDER
, 0, NULL
);
1634 binder_debug(BINDER_DEBUG_TRANSACTION
,
1635 " ref %d desc %d -> node %d u%p\n",
1636 ref
->debug_id
, ref
->desc
, ref
->node
->debug_id
,
1639 struct binder_ref
*new_ref
;
1640 new_ref
= binder_get_ref_for_node(target_proc
, ref
->node
);
1641 if (new_ref
== NULL
) {
1642 return_error
= BR_FAILED_REPLY
;
1643 goto err_binder_get_ref_for_node_failed
;
1645 fp
->handle
= new_ref
->desc
;
1646 binder_inc_ref(new_ref
, fp
->type
== BINDER_TYPE_HANDLE
, NULL
);
1647 binder_debug(BINDER_DEBUG_TRANSACTION
,
1648 " ref %d desc %d -> ref %d desc %d (node %d)\n",
1649 ref
->debug_id
, ref
->desc
, new_ref
->debug_id
,
1650 new_ref
->desc
, ref
->node
->debug_id
);
1654 case BINDER_TYPE_FD
: {
1659 if (!(in_reply_to
->flags
& TF_ACCEPT_FDS
)) {
1660 binder_user_error("binder: %d:%d got reply with fd, %ld, but target does not allow fds\n",
1661 proc
->pid
, thread
->pid
, fp
->handle
);
1662 return_error
= BR_FAILED_REPLY
;
1663 goto err_fd_not_allowed
;
1665 } else if (!target_node
->accept_fds
) {
1666 binder_user_error("binder: %d:%d got transaction with fd, %ld, but target does not allow fds\n",
1667 proc
->pid
, thread
->pid
, fp
->handle
);
1668 return_error
= BR_FAILED_REPLY
;
1669 goto err_fd_not_allowed
;
1672 file
= fget(fp
->handle
);
1674 binder_user_error("binder: %d:%d got transaction with invalid fd, %ld\n",
1675 proc
->pid
, thread
->pid
, fp
->handle
);
1676 return_error
= BR_FAILED_REPLY
;
1677 goto err_fget_failed
;
1679 target_fd
= task_get_unused_fd_flags(target_proc
, O_CLOEXEC
);
1680 if (target_fd
< 0) {
1682 return_error
= BR_FAILED_REPLY
;
1683 goto err_get_unused_fd_failed
;
1685 task_fd_install(target_proc
, target_fd
, file
);
1686 binder_debug(BINDER_DEBUG_TRANSACTION
,
1687 " fd %ld -> %d\n", fp
->handle
, target_fd
);
1689 fp
->handle
= target_fd
;
1693 binder_user_error("binder: %d:%d got transactio"
1694 "n with invalid object type, %lx\n",
1695 proc
->pid
, thread
->pid
, fp
->type
);
1696 return_error
= BR_FAILED_REPLY
;
1697 goto err_bad_object_type
;
1701 BUG_ON(t
->buffer
->async_transaction
!= 0);
1702 binder_pop_transaction(target_thread
, in_reply_to
);
1703 } else if (!(t
->flags
& TF_ONE_WAY
)) {
1704 BUG_ON(t
->buffer
->async_transaction
!= 0);
1706 t
->from_parent
= thread
->transaction_stack
;
1707 thread
->transaction_stack
= t
;
1709 BUG_ON(target_node
== NULL
);
1710 BUG_ON(t
->buffer
->async_transaction
!= 1);
1711 if (target_node
->has_async_transaction
) {
1712 target_list
= &target_node
->async_todo
;
1715 target_node
->has_async_transaction
= 1;
1717 t
->work
.type
= BINDER_WORK_TRANSACTION
;
1718 list_add_tail(&t
->work
.entry
, target_list
);
1719 tcomplete
->type
= BINDER_WORK_TRANSACTION_COMPLETE
;
1720 list_add_tail(&tcomplete
->entry
, &thread
->todo
);
1722 wake_up_interruptible(target_wait
);
1725 err_get_unused_fd_failed
:
1728 err_binder_get_ref_for_node_failed
:
1729 err_binder_get_ref_failed
:
1730 err_binder_new_node_failed
:
1731 err_bad_object_type
:
1733 err_copy_data_failed
:
1734 binder_transaction_buffer_release(target_proc
, t
->buffer
, offp
);
1735 t
->buffer
->transaction
= NULL
;
1736 binder_free_buf(target_proc
, t
->buffer
);
1737 err_binder_alloc_buf_failed
:
1739 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE
);
1740 err_alloc_tcomplete_failed
:
1742 binder_stats_deleted(BINDER_STAT_TRANSACTION
);
1745 err_empty_call_stack
:
1747 err_invalid_target_handle
:
1748 err_no_context_mgr_node
:
1749 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
1750 "binder: %d:%d transaction failed %d, size %zd-%zd\n",
1751 proc
->pid
, thread
->pid
, return_error
,
1752 tr
->data_size
, tr
->offsets_size
);
1755 struct binder_transaction_log_entry
*fe
;
1756 fe
= binder_transaction_log_add(&binder_transaction_log_failed
);
1760 BUG_ON(thread
->return_error
!= BR_OK
);
1762 thread
->return_error
= BR_TRANSACTION_COMPLETE
;
1763 binder_send_failed_reply(in_reply_to
, return_error
);
1765 thread
->return_error
= return_error
;
1768 int binder_thread_write(struct binder_proc
*proc
, struct binder_thread
*thread
,
1769 void __user
*buffer
, int size
, signed long *consumed
)
1772 void __user
*ptr
= buffer
+ *consumed
;
1773 void __user
*end
= buffer
+ size
;
1775 while (ptr
< end
&& thread
->return_error
== BR_OK
) {
1776 if (get_user(cmd
, (uint32_t __user
*)ptr
))
1778 ptr
+= sizeof(uint32_t);
1779 if (_IOC_NR(cmd
) < ARRAY_SIZE(binder_stats
.bc
)) {
1780 binder_stats
.bc
[_IOC_NR(cmd
)]++;
1781 proc
->stats
.bc
[_IOC_NR(cmd
)]++;
1782 thread
->stats
.bc
[_IOC_NR(cmd
)]++;
1790 struct binder_ref
*ref
;
1791 const char *debug_string
;
1793 if (get_user(target
, (uint32_t __user
*)ptr
))
1795 ptr
+= sizeof(uint32_t);
1796 if (target
== 0 && binder_context_mgr_node
&&
1797 (cmd
== BC_INCREFS
|| cmd
== BC_ACQUIRE
)) {
1798 ref
= binder_get_ref_for_node(proc
,
1799 binder_context_mgr_node
);
1800 if (ref
->desc
!= target
) {
1801 binder_user_error("binder: %d:"
1802 "%d tried to acquire "
1803 "reference to desc 0, "
1805 proc
->pid
, thread
->pid
,
1809 ref
= binder_get_ref(proc
, target
);
1811 binder_user_error("binder: %d:%d refcou"
1812 "nt change on invalid ref %d\n",
1813 proc
->pid
, thread
->pid
, target
);
1818 debug_string
= "IncRefs";
1819 binder_inc_ref(ref
, 0, NULL
);
1822 debug_string
= "Acquire";
1823 binder_inc_ref(ref
, 1, NULL
);
1826 debug_string
= "Release";
1827 binder_dec_ref(ref
, 1);
1831 debug_string
= "DecRefs";
1832 binder_dec_ref(ref
, 0);
1835 binder_debug(BINDER_DEBUG_USER_REFS
,
1836 "binder: %d:%d %s ref %d desc %d s %d w %d for node %d\n",
1837 proc
->pid
, thread
->pid
, debug_string
, ref
->debug_id
,
1838 ref
->desc
, ref
->strong
, ref
->weak
, ref
->node
->debug_id
);
1841 case BC_INCREFS_DONE
:
1842 case BC_ACQUIRE_DONE
: {
1843 void __user
*node_ptr
;
1845 struct binder_node
*node
;
1847 if (get_user(node_ptr
, (void * __user
*)ptr
))
1849 ptr
+= sizeof(void *);
1850 if (get_user(cookie
, (void * __user
*)ptr
))
1852 ptr
+= sizeof(void *);
1853 node
= binder_get_node(proc
, node_ptr
);
1855 binder_user_error("binder: %d:%d "
1856 "%s u%p no match\n",
1857 proc
->pid
, thread
->pid
,
1858 cmd
== BC_INCREFS_DONE
?
1864 if (cookie
!= node
->cookie
) {
1865 binder_user_error("binder: %d:%d %s u%p node %d"
1866 " cookie mismatch %p != %p\n",
1867 proc
->pid
, thread
->pid
,
1868 cmd
== BC_INCREFS_DONE
?
1869 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
1870 node_ptr
, node
->debug_id
,
1871 cookie
, node
->cookie
);
1874 if (cmd
== BC_ACQUIRE_DONE
) {
1875 if (node
->pending_strong_ref
== 0) {
1876 binder_user_error("binder: %d:%d "
1877 "BC_ACQUIRE_DONE node %d has "
1878 "no pending acquire request\n",
1879 proc
->pid
, thread
->pid
,
1883 node
->pending_strong_ref
= 0;
1885 if (node
->pending_weak_ref
== 0) {
1886 binder_user_error("binder: %d:%d "
1887 "BC_INCREFS_DONE node %d has "
1888 "no pending increfs request\n",
1889 proc
->pid
, thread
->pid
,
1893 node
->pending_weak_ref
= 0;
1895 binder_dec_node(node
, cmd
== BC_ACQUIRE_DONE
, 0);
1896 binder_debug(BINDER_DEBUG_USER_REFS
,
1897 "binder: %d:%d %s node %d ls %d lw %d\n",
1898 proc
->pid
, thread
->pid
,
1899 cmd
== BC_INCREFS_DONE
? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
1900 node
->debug_id
, node
->local_strong_refs
, node
->local_weak_refs
);
1903 case BC_ATTEMPT_ACQUIRE
:
1904 printk(KERN_ERR
"binder: BC_ATTEMPT_ACQUIRE not supported\n");
1906 case BC_ACQUIRE_RESULT
:
1907 printk(KERN_ERR
"binder: BC_ACQUIRE_RESULT not supported\n");
1910 case BC_FREE_BUFFER
: {
1911 void __user
*data_ptr
;
1912 struct binder_buffer
*buffer
;
1914 if (get_user(data_ptr
, (void * __user
*)ptr
))
1916 ptr
+= sizeof(void *);
1918 buffer
= binder_buffer_lookup(proc
, data_ptr
);
1919 if (buffer
== NULL
) {
1920 binder_user_error("binder: %d:%d "
1921 "BC_FREE_BUFFER u%p no match\n",
1922 proc
->pid
, thread
->pid
, data_ptr
);
1925 if (!buffer
->allow_user_free
) {
1926 binder_user_error("binder: %d:%d "
1927 "BC_FREE_BUFFER u%p matched "
1928 "unreturned buffer\n",
1929 proc
->pid
, thread
->pid
, data_ptr
);
1932 binder_debug(BINDER_DEBUG_FREE_BUFFER
,
1933 "binder: %d:%d BC_FREE_BUFFER u%p found buffer %d for %s transaction\n",
1934 proc
->pid
, thread
->pid
, data_ptr
, buffer
->debug_id
,
1935 buffer
->transaction
? "active" : "finished");
1937 if (buffer
->transaction
) {
1938 buffer
->transaction
->buffer
= NULL
;
1939 buffer
->transaction
= NULL
;
1941 if (buffer
->async_transaction
&& buffer
->target_node
) {
1942 BUG_ON(!buffer
->target_node
->has_async_transaction
);
1943 if (list_empty(&buffer
->target_node
->async_todo
))
1944 buffer
->target_node
->has_async_transaction
= 0;
1946 list_move_tail(buffer
->target_node
->async_todo
.next
, &thread
->todo
);
1948 binder_transaction_buffer_release(proc
, buffer
, NULL
);
1949 binder_free_buf(proc
, buffer
);
1953 case BC_TRANSACTION
:
1955 struct binder_transaction_data tr
;
1957 if (copy_from_user(&tr
, ptr
, sizeof(tr
)))
1960 binder_transaction(proc
, thread
, &tr
, cmd
== BC_REPLY
);
1964 case BC_REGISTER_LOOPER
:
1965 binder_debug(BINDER_DEBUG_THREADS
,
1966 "binder: %d:%d BC_REGISTER_LOOPER\n",
1967 proc
->pid
, thread
->pid
);
1968 if (thread
->looper
& BINDER_LOOPER_STATE_ENTERED
) {
1969 thread
->looper
|= BINDER_LOOPER_STATE_INVALID
;
1970 binder_user_error("binder: %d:%d ERROR:"
1971 " BC_REGISTER_LOOPER called "
1972 "after BC_ENTER_LOOPER\n",
1973 proc
->pid
, thread
->pid
);
1974 } else if (proc
->requested_threads
== 0) {
1975 thread
->looper
|= BINDER_LOOPER_STATE_INVALID
;
1976 binder_user_error("binder: %d:%d ERROR:"
1977 " BC_REGISTER_LOOPER called "
1978 "without request\n",
1979 proc
->pid
, thread
->pid
);
1981 proc
->requested_threads
--;
1982 proc
->requested_threads_started
++;
1984 thread
->looper
|= BINDER_LOOPER_STATE_REGISTERED
;
1986 case BC_ENTER_LOOPER
:
1987 binder_debug(BINDER_DEBUG_THREADS
,
1988 "binder: %d:%d BC_ENTER_LOOPER\n",
1989 proc
->pid
, thread
->pid
);
1990 if (thread
->looper
& BINDER_LOOPER_STATE_REGISTERED
) {
1991 thread
->looper
|= BINDER_LOOPER_STATE_INVALID
;
1992 binder_user_error("binder: %d:%d ERROR:"
1993 " BC_ENTER_LOOPER called after "
1994 "BC_REGISTER_LOOPER\n",
1995 proc
->pid
, thread
->pid
);
1997 thread
->looper
|= BINDER_LOOPER_STATE_ENTERED
;
1999 case BC_EXIT_LOOPER
:
2000 binder_debug(BINDER_DEBUG_THREADS
,
2001 "binder: %d:%d BC_EXIT_LOOPER\n",
2002 proc
->pid
, thread
->pid
);
2003 thread
->looper
|= BINDER_LOOPER_STATE_EXITED
;
2006 case BC_REQUEST_DEATH_NOTIFICATION
:
2007 case BC_CLEAR_DEATH_NOTIFICATION
: {
2009 void __user
*cookie
;
2010 struct binder_ref
*ref
;
2011 struct binder_ref_death
*death
;
2013 if (get_user(target
, (uint32_t __user
*)ptr
))
2015 ptr
+= sizeof(uint32_t);
2016 if (get_user(cookie
, (void __user
* __user
*)ptr
))
2018 ptr
+= sizeof(void *);
2019 ref
= binder_get_ref(proc
, target
);
2021 binder_user_error("binder: %d:%d %s "
2023 proc
->pid
, thread
->pid
,
2024 cmd
== BC_REQUEST_DEATH_NOTIFICATION
?
2025 "BC_REQUEST_DEATH_NOTIFICATION" :
2026 "BC_CLEAR_DEATH_NOTIFICATION",
2031 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION
,
2032 "binder: %d:%d %s %p ref %d desc %d s %d w %d for node %d\n",
2033 proc
->pid
, thread
->pid
,
2034 cmd
== BC_REQUEST_DEATH_NOTIFICATION
?
2035 "BC_REQUEST_DEATH_NOTIFICATION" :
2036 "BC_CLEAR_DEATH_NOTIFICATION",
2037 cookie
, ref
->debug_id
, ref
->desc
,
2038 ref
->strong
, ref
->weak
, ref
->node
->debug_id
);
2040 if (cmd
== BC_REQUEST_DEATH_NOTIFICATION
) {
2042 binder_user_error("binder: %d:%"
2043 "d BC_REQUEST_DEATH_NOTI"
2044 "FICATION death notific"
2045 "ation already set\n",
2046 proc
->pid
, thread
->pid
);
2049 death
= kzalloc(sizeof(*death
), GFP_KERNEL
);
2050 if (death
== NULL
) {
2051 thread
->return_error
= BR_ERROR
;
2052 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
2054 "BC_REQUEST_DEATH_NOTIFICATION failed\n",
2055 proc
->pid
, thread
->pid
);
2058 binder_stats_created(BINDER_STAT_DEATH
);
2059 INIT_LIST_HEAD(&death
->work
.entry
);
2060 death
->cookie
= cookie
;
2062 if (ref
->node
->proc
== NULL
) {
2063 ref
->death
->work
.type
= BINDER_WORK_DEAD_BINDER
;
2064 if (thread
->looper
& (BINDER_LOOPER_STATE_REGISTERED
| BINDER_LOOPER_STATE_ENTERED
)) {
2065 list_add_tail(&ref
->death
->work
.entry
, &thread
->todo
);
2067 list_add_tail(&ref
->death
->work
.entry
, &proc
->todo
);
2068 wake_up_interruptible(&proc
->wait
);
2072 if (ref
->death
== NULL
) {
2073 binder_user_error("binder: %d:%"
2074 "d BC_CLEAR_DEATH_NOTIFI"
2075 "CATION death notificat"
2077 proc
->pid
, thread
->pid
);
2081 if (death
->cookie
!= cookie
) {
2082 binder_user_error("binder: %d:%"
2083 "d BC_CLEAR_DEATH_NOTIFI"
2084 "CATION death notificat"
2085 "ion cookie mismatch "
2087 proc
->pid
, thread
->pid
,
2088 death
->cookie
, cookie
);
2092 if (list_empty(&death
->work
.entry
)) {
2093 death
->work
.type
= BINDER_WORK_CLEAR_DEATH_NOTIFICATION
;
2094 if (thread
->looper
& (BINDER_LOOPER_STATE_REGISTERED
| BINDER_LOOPER_STATE_ENTERED
)) {
2095 list_add_tail(&death
->work
.entry
, &thread
->todo
);
2097 list_add_tail(&death
->work
.entry
, &proc
->todo
);
2098 wake_up_interruptible(&proc
->wait
);
2101 BUG_ON(death
->work
.type
!= BINDER_WORK_DEAD_BINDER
);
2102 death
->work
.type
= BINDER_WORK_DEAD_BINDER_AND_CLEAR
;
2106 case BC_DEAD_BINDER_DONE
: {
2107 struct binder_work
*w
;
2108 void __user
*cookie
;
2109 struct binder_ref_death
*death
= NULL
;
2110 if (get_user(cookie
, (void __user
* __user
*)ptr
))
2113 ptr
+= sizeof(void *);
2114 list_for_each_entry(w
, &proc
->delivered_death
, entry
) {
2115 struct binder_ref_death
*tmp_death
= container_of(w
, struct binder_ref_death
, work
);
2116 if (tmp_death
->cookie
== cookie
) {
2121 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
2122 "binder: %d:%d BC_DEAD_BINDER_DONE %p found %p\n",
2123 proc
->pid
, thread
->pid
, cookie
, death
);
2124 if (death
== NULL
) {
2125 binder_user_error("binder: %d:%d BC_DEAD"
2126 "_BINDER_DONE %p not found\n",
2127 proc
->pid
, thread
->pid
, cookie
);
2131 list_del_init(&death
->work
.entry
);
2132 if (death
->work
.type
== BINDER_WORK_DEAD_BINDER_AND_CLEAR
) {
2133 death
->work
.type
= BINDER_WORK_CLEAR_DEATH_NOTIFICATION
;
2134 if (thread
->looper
& (BINDER_LOOPER_STATE_REGISTERED
| BINDER_LOOPER_STATE_ENTERED
)) {
2135 list_add_tail(&death
->work
.entry
, &thread
->todo
);
2137 list_add_tail(&death
->work
.entry
, &proc
->todo
);
2138 wake_up_interruptible(&proc
->wait
);
2144 printk(KERN_ERR
"binder: %d:%d unknown command %d\n",
2145 proc
->pid
, thread
->pid
, cmd
);
2148 *consumed
= ptr
- buffer
;
2153 void binder_stat_br(struct binder_proc
*proc
, struct binder_thread
*thread
,
2156 if (_IOC_NR(cmd
) < ARRAY_SIZE(binder_stats
.br
)) {
2157 binder_stats
.br
[_IOC_NR(cmd
)]++;
2158 proc
->stats
.br
[_IOC_NR(cmd
)]++;
2159 thread
->stats
.br
[_IOC_NR(cmd
)]++;
2163 static int binder_has_proc_work(struct binder_proc
*proc
,
2164 struct binder_thread
*thread
)
2166 return !list_empty(&proc
->todo
) ||
2167 (thread
->looper
& BINDER_LOOPER_STATE_NEED_RETURN
);
2170 static int binder_has_thread_work(struct binder_thread
*thread
)
2172 return !list_empty(&thread
->todo
) || thread
->return_error
!= BR_OK
||
2173 (thread
->looper
& BINDER_LOOPER_STATE_NEED_RETURN
);
2176 static int binder_thread_read(struct binder_proc
*proc
,
2177 struct binder_thread
*thread
,
2178 void __user
*buffer
, int size
,
2179 signed long *consumed
, int non_block
)
2181 void __user
*ptr
= buffer
+ *consumed
;
2182 void __user
*end
= buffer
+ size
;
2185 int wait_for_proc_work
;
2187 if (*consumed
== 0) {
2188 if (put_user(BR_NOOP
, (uint32_t __user
*)ptr
))
2190 ptr
+= sizeof(uint32_t);
2194 wait_for_proc_work
= thread
->transaction_stack
== NULL
&&
2195 list_empty(&thread
->todo
);
2197 if (thread
->return_error
!= BR_OK
&& ptr
< end
) {
2198 if (thread
->return_error2
!= BR_OK
) {
2199 if (put_user(thread
->return_error2
, (uint32_t __user
*)ptr
))
2201 ptr
+= sizeof(uint32_t);
2204 thread
->return_error2
= BR_OK
;
2206 if (put_user(thread
->return_error
, (uint32_t __user
*)ptr
))
2208 ptr
+= sizeof(uint32_t);
2209 thread
->return_error
= BR_OK
;
2214 thread
->looper
|= BINDER_LOOPER_STATE_WAITING
;
2215 if (wait_for_proc_work
)
2216 proc
->ready_threads
++;
2217 mutex_unlock(&binder_lock
);
2218 if (wait_for_proc_work
) {
2219 if (!(thread
->looper
& (BINDER_LOOPER_STATE_REGISTERED
|
2220 BINDER_LOOPER_STATE_ENTERED
))) {
2221 binder_user_error("binder: %d:%d ERROR: Thread waiting "
2222 "for process work before calling BC_REGISTER_"
2223 "LOOPER or BC_ENTER_LOOPER (state %x)\n",
2224 proc
->pid
, thread
->pid
, thread
->looper
);
2225 wait_event_interruptible(binder_user_error_wait
,
2226 binder_stop_on_user_error
< 2);
2228 binder_set_nice(proc
->default_priority
);
2230 if (!binder_has_proc_work(proc
, thread
))
2233 ret
= wait_event_interruptible_exclusive(proc
->wait
, binder_has_proc_work(proc
, thread
));
2236 if (!binder_has_thread_work(thread
))
2239 ret
= wait_event_interruptible(thread
->wait
, binder_has_thread_work(thread
));
2241 mutex_lock(&binder_lock
);
2242 if (wait_for_proc_work
)
2243 proc
->ready_threads
--;
2244 thread
->looper
&= ~BINDER_LOOPER_STATE_WAITING
;
2251 struct binder_transaction_data tr
;
2252 struct binder_work
*w
;
2253 struct binder_transaction
*t
= NULL
;
2255 if (!list_empty(&thread
->todo
))
2256 w
= list_first_entry(&thread
->todo
, struct binder_work
, entry
);
2257 else if (!list_empty(&proc
->todo
) && wait_for_proc_work
)
2258 w
= list_first_entry(&proc
->todo
, struct binder_work
, entry
);
2260 if (ptr
- buffer
== 4 && !(thread
->looper
& BINDER_LOOPER_STATE_NEED_RETURN
)) /* no data added */
2265 if (end
- ptr
< sizeof(tr
) + 4)
2269 case BINDER_WORK_TRANSACTION
: {
2270 t
= container_of(w
, struct binder_transaction
, work
);
2272 case BINDER_WORK_TRANSACTION_COMPLETE
: {
2273 cmd
= BR_TRANSACTION_COMPLETE
;
2274 if (put_user(cmd
, (uint32_t __user
*)ptr
))
2276 ptr
+= sizeof(uint32_t);
2278 binder_stat_br(proc
, thread
, cmd
);
2279 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE
,
2280 "binder: %d:%d BR_TRANSACTION_COMPLETE\n",
2281 proc
->pid
, thread
->pid
);
2283 list_del(&w
->entry
);
2285 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE
);
2287 case BINDER_WORK_NODE
: {
2288 struct binder_node
*node
= container_of(w
, struct binder_node
, work
);
2289 uint32_t cmd
= BR_NOOP
;
2290 const char *cmd_name
;
2291 int strong
= node
->internal_strong_refs
|| node
->local_strong_refs
;
2292 int weak
= !hlist_empty(&node
->refs
) || node
->local_weak_refs
|| strong
;
2293 if (weak
&& !node
->has_weak_ref
) {
2295 cmd_name
= "BR_INCREFS";
2296 node
->has_weak_ref
= 1;
2297 node
->pending_weak_ref
= 1;
2298 node
->local_weak_refs
++;
2299 } else if (strong
&& !node
->has_strong_ref
) {
2301 cmd_name
= "BR_ACQUIRE";
2302 node
->has_strong_ref
= 1;
2303 node
->pending_strong_ref
= 1;
2304 node
->local_strong_refs
++;
2305 } else if (!strong
&& node
->has_strong_ref
) {
2307 cmd_name
= "BR_RELEASE";
2308 node
->has_strong_ref
= 0;
2309 } else if (!weak
&& node
->has_weak_ref
) {
2311 cmd_name
= "BR_DECREFS";
2312 node
->has_weak_ref
= 0;
2314 if (cmd
!= BR_NOOP
) {
2315 if (put_user(cmd
, (uint32_t __user
*)ptr
))
2317 ptr
+= sizeof(uint32_t);
2318 if (put_user(node
->ptr
, (void * __user
*)ptr
))
2320 ptr
+= sizeof(void *);
2321 if (put_user(node
->cookie
, (void * __user
*)ptr
))
2323 ptr
+= sizeof(void *);
2325 binder_stat_br(proc
, thread
, cmd
);
2326 binder_debug(BINDER_DEBUG_USER_REFS
,
2327 "binder: %d:%d %s %d u%p c%p\n",
2328 proc
->pid
, thread
->pid
, cmd_name
, node
->debug_id
, node
->ptr
, node
->cookie
);
2330 list_del_init(&w
->entry
);
2331 if (!weak
&& !strong
) {
2332 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
2333 "binder: %d:%d node %d u%p c%p deleted\n",
2334 proc
->pid
, thread
->pid
, node
->debug_id
,
2335 node
->ptr
, node
->cookie
);
2336 rb_erase(&node
->rb_node
, &proc
->nodes
);
2338 binder_stats_deleted(BINDER_STAT_NODE
);
2340 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
2341 "binder: %d:%d node %d u%p c%p state unchanged\n",
2342 proc
->pid
, thread
->pid
, node
->debug_id
, node
->ptr
,
2347 case BINDER_WORK_DEAD_BINDER
:
2348 case BINDER_WORK_DEAD_BINDER_AND_CLEAR
:
2349 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION
: {
2350 struct binder_ref_death
*death
;
2353 death
= container_of(w
, struct binder_ref_death
, work
);
2354 if (w
->type
== BINDER_WORK_CLEAR_DEATH_NOTIFICATION
)
2355 cmd
= BR_CLEAR_DEATH_NOTIFICATION_DONE
;
2357 cmd
= BR_DEAD_BINDER
;
2358 if (put_user(cmd
, (uint32_t __user
*)ptr
))
2360 ptr
+= sizeof(uint32_t);
2361 if (put_user(death
->cookie
, (void * __user
*)ptr
))
2363 ptr
+= sizeof(void *);
2364 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION
,
2365 "binder: %d:%d %s %p\n",
2366 proc
->pid
, thread
->pid
,
2367 cmd
== BR_DEAD_BINDER
?
2369 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
2372 if (w
->type
== BINDER_WORK_CLEAR_DEATH_NOTIFICATION
) {
2373 list_del(&w
->entry
);
2375 binder_stats_deleted(BINDER_STAT_DEATH
);
2377 list_move(&w
->entry
, &proc
->delivered_death
);
2378 if (cmd
== BR_DEAD_BINDER
)
2379 goto done
; /* DEAD_BINDER notifications can cause transactions */
2386 BUG_ON(t
->buffer
== NULL
);
2387 if (t
->buffer
->target_node
) {
2388 struct binder_node
*target_node
= t
->buffer
->target_node
;
2389 tr
.target
.ptr
= target_node
->ptr
;
2390 tr
.cookie
= target_node
->cookie
;
2391 t
->saved_priority
= task_nice(current
);
2392 if (t
->priority
< target_node
->min_priority
&&
2393 !(t
->flags
& TF_ONE_WAY
))
2394 binder_set_nice(t
->priority
);
2395 else if (!(t
->flags
& TF_ONE_WAY
) ||
2396 t
->saved_priority
> target_node
->min_priority
)
2397 binder_set_nice(target_node
->min_priority
);
2398 cmd
= BR_TRANSACTION
;
2400 tr
.target
.ptr
= NULL
;
2405 tr
.flags
= t
->flags
;
2406 tr
.sender_euid
= t
->sender_euid
;
2409 struct task_struct
*sender
= t
->from
->proc
->tsk
;
2410 tr
.sender_pid
= task_tgid_nr_ns(sender
,
2411 current
->nsproxy
->pid_ns
);
2416 tr
.data_size
= t
->buffer
->data_size
;
2417 tr
.offsets_size
= t
->buffer
->offsets_size
;
2418 tr
.data
.ptr
.buffer
= (void *)t
->buffer
->data
+
2419 proc
->user_buffer_offset
;
2420 tr
.data
.ptr
.offsets
= tr
.data
.ptr
.buffer
+
2421 ALIGN(t
->buffer
->data_size
,
2424 if (put_user(cmd
, (uint32_t __user
*)ptr
))
2426 ptr
+= sizeof(uint32_t);
2427 if (copy_to_user(ptr
, &tr
, sizeof(tr
)))
2431 binder_stat_br(proc
, thread
, cmd
);
2432 binder_debug(BINDER_DEBUG_TRANSACTION
,
2433 "binder: %d:%d %s %d %d:%d, cmd %d"
2434 "size %zd-%zd ptr %p-%p\n",
2435 proc
->pid
, thread
->pid
,
2436 (cmd
== BR_TRANSACTION
) ? "BR_TRANSACTION" :
2438 t
->debug_id
, t
->from
? t
->from
->proc
->pid
: 0,
2439 t
->from
? t
->from
->pid
: 0, cmd
,
2440 t
->buffer
->data_size
, t
->buffer
->offsets_size
,
2441 tr
.data
.ptr
.buffer
, tr
.data
.ptr
.offsets
);
2443 list_del(&t
->work
.entry
);
2444 t
->buffer
->allow_user_free
= 1;
2445 if (cmd
== BR_TRANSACTION
&& !(t
->flags
& TF_ONE_WAY
)) {
2446 t
->to_parent
= thread
->transaction_stack
;
2447 t
->to_thread
= thread
;
2448 thread
->transaction_stack
= t
;
2450 t
->buffer
->transaction
= NULL
;
2452 binder_stats_deleted(BINDER_STAT_TRANSACTION
);
2459 *consumed
= ptr
- buffer
;
2460 if (proc
->requested_threads
+ proc
->ready_threads
== 0 &&
2461 proc
->requested_threads_started
< proc
->max_threads
&&
2462 (thread
->looper
& (BINDER_LOOPER_STATE_REGISTERED
|
2463 BINDER_LOOPER_STATE_ENTERED
)) /* the user-space code fails to */
2464 /*spawn a new thread if we leave this out */) {
2465 proc
->requested_threads
++;
2466 binder_debug(BINDER_DEBUG_THREADS
,
2467 "binder: %d:%d BR_SPAWN_LOOPER\n",
2468 proc
->pid
, thread
->pid
);
2469 if (put_user(BR_SPAWN_LOOPER
, (uint32_t __user
*)buffer
))
2475 static void binder_release_work(struct list_head
*list
)
2477 struct binder_work
*w
;
2478 while (!list_empty(list
)) {
2479 w
= list_first_entry(list
, struct binder_work
, entry
);
2480 list_del_init(&w
->entry
);
2482 case BINDER_WORK_TRANSACTION
: {
2483 struct binder_transaction
*t
;
2485 t
= container_of(w
, struct binder_transaction
, work
);
2486 if (t
->buffer
->target_node
&& !(t
->flags
& TF_ONE_WAY
))
2487 binder_send_failed_reply(t
, BR_DEAD_REPLY
);
2489 case BINDER_WORK_TRANSACTION_COMPLETE
: {
2491 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE
);
2500 static struct binder_thread
*binder_get_thread(struct binder_proc
*proc
)
2502 struct binder_thread
*thread
= NULL
;
2503 struct rb_node
*parent
= NULL
;
2504 struct rb_node
**p
= &proc
->threads
.rb_node
;
2508 thread
= rb_entry(parent
, struct binder_thread
, rb_node
);
2510 if (current
->pid
< thread
->pid
)
2512 else if (current
->pid
> thread
->pid
)
2513 p
= &(*p
)->rb_right
;
2518 thread
= kzalloc(sizeof(*thread
), GFP_KERNEL
);
2521 binder_stats_created(BINDER_STAT_THREAD
);
2522 thread
->proc
= proc
;
2523 thread
->pid
= current
->pid
;
2524 init_waitqueue_head(&thread
->wait
);
2525 INIT_LIST_HEAD(&thread
->todo
);
2526 rb_link_node(&thread
->rb_node
, parent
, p
);
2527 rb_insert_color(&thread
->rb_node
, &proc
->threads
);
2528 thread
->looper
|= BINDER_LOOPER_STATE_NEED_RETURN
;
2529 thread
->return_error
= BR_OK
;
2530 thread
->return_error2
= BR_OK
;
2535 static int binder_free_thread(struct binder_proc
*proc
,
2536 struct binder_thread
*thread
)
2538 struct binder_transaction
*t
;
2539 struct binder_transaction
*send_reply
= NULL
;
2540 int active_transactions
= 0;
2542 rb_erase(&thread
->rb_node
, &proc
->threads
);
2543 t
= thread
->transaction_stack
;
2544 if (t
&& t
->to_thread
== thread
)
2547 active_transactions
++;
2548 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
2549 "binder: release %d:%d transaction %d "
2550 "%s, still active\n", proc
->pid
, thread
->pid
,
2552 (t
->to_thread
== thread
) ? "in" : "out");
2554 if (t
->to_thread
== thread
) {
2556 t
->to_thread
= NULL
;
2558 t
->buffer
->transaction
= NULL
;
2562 } else if (t
->from
== thread
) {
2569 binder_send_failed_reply(send_reply
, BR_DEAD_REPLY
);
2570 binder_release_work(&thread
->todo
);
2572 binder_stats_deleted(BINDER_STAT_THREAD
);
2573 return active_transactions
;
2576 static unsigned int binder_poll(struct file
*filp
,
2577 struct poll_table_struct
*wait
)
2579 struct binder_proc
*proc
= filp
->private_data
;
2580 struct binder_thread
*thread
= NULL
;
2581 int wait_for_proc_work
;
2583 mutex_lock(&binder_lock
);
2584 thread
= binder_get_thread(proc
);
2586 wait_for_proc_work
= thread
->transaction_stack
== NULL
&&
2587 list_empty(&thread
->todo
) && thread
->return_error
== BR_OK
;
2588 mutex_unlock(&binder_lock
);
2590 if (wait_for_proc_work
) {
2591 if (binder_has_proc_work(proc
, thread
))
2593 poll_wait(filp
, &proc
->wait
, wait
);
2594 if (binder_has_proc_work(proc
, thread
))
2597 if (binder_has_thread_work(thread
))
2599 poll_wait(filp
, &thread
->wait
, wait
);
2600 if (binder_has_thread_work(thread
))
2606 static long binder_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
2609 struct binder_proc
*proc
= filp
->private_data
;
2610 struct binder_thread
*thread
;
2611 unsigned int size
= _IOC_SIZE(cmd
);
2612 void __user
*ubuf
= (void __user
*)arg
;
2614 /*printk(KERN_INFO "binder_ioctl: %d:%d %x %lx\n", proc->pid, current->pid, cmd, arg);*/
2616 ret
= wait_event_interruptible(binder_user_error_wait
, binder_stop_on_user_error
< 2);
2620 mutex_lock(&binder_lock
);
2621 thread
= binder_get_thread(proc
);
2622 if (thread
== NULL
) {
2628 case BINDER_WRITE_READ
: {
2629 struct binder_write_read bwr
;
2630 if (size
!= sizeof(struct binder_write_read
)) {
2634 if (copy_from_user(&bwr
, ubuf
, sizeof(bwr
))) {
2638 binder_debug(BINDER_DEBUG_READ_WRITE
,
2639 "binder: %d:%d write %ld at %08lx, read %ld at %08lx\n",
2640 proc
->pid
, thread
->pid
, bwr
.write_size
, bwr
.write_buffer
,
2641 bwr
.read_size
, bwr
.read_buffer
);
2643 if (bwr
.write_size
> 0) {
2644 ret
= binder_thread_write(proc
, thread
, (void __user
*)bwr
.write_buffer
, bwr
.write_size
, &bwr
.write_consumed
);
2646 bwr
.read_consumed
= 0;
2647 if (copy_to_user(ubuf
, &bwr
, sizeof(bwr
)))
2652 if (bwr
.read_size
> 0) {
2653 ret
= binder_thread_read(proc
, thread
, (void __user
*)bwr
.read_buffer
, bwr
.read_size
, &bwr
.read_consumed
, filp
->f_flags
& O_NONBLOCK
);
2654 if (!list_empty(&proc
->todo
))
2655 wake_up_interruptible(&proc
->wait
);
2657 if (copy_to_user(ubuf
, &bwr
, sizeof(bwr
)))
2662 binder_debug(BINDER_DEBUG_READ_WRITE
,
2663 "binder: %d:%d wrote %ld of %ld, read return %ld of %ld\n",
2664 proc
->pid
, thread
->pid
, bwr
.write_consumed
, bwr
.write_size
,
2665 bwr
.read_consumed
, bwr
.read_size
);
2666 if (copy_to_user(ubuf
, &bwr
, sizeof(bwr
))) {
2672 case BINDER_SET_MAX_THREADS
:
2673 if (copy_from_user(&proc
->max_threads
, ubuf
, sizeof(proc
->max_threads
))) {
2678 case BINDER_SET_CONTEXT_MGR
:
2679 if (binder_context_mgr_node
!= NULL
) {
2680 printk(KERN_ERR
"binder: BINDER_SET_CONTEXT_MGR already set\n");
2684 if (binder_context_mgr_uid
!= -1) {
2685 if (binder_context_mgr_uid
!= current
->cred
->euid
) {
2686 printk(KERN_ERR
"binder: BINDER_SET_"
2687 "CONTEXT_MGR bad uid %d != %d\n",
2688 current
->cred
->euid
,
2689 binder_context_mgr_uid
);
2694 binder_context_mgr_uid
= current
->cred
->euid
;
2695 binder_context_mgr_node
= binder_new_node(proc
, NULL
, NULL
);
2696 if (binder_context_mgr_node
== NULL
) {
2700 binder_context_mgr_node
->local_weak_refs
++;
2701 binder_context_mgr_node
->local_strong_refs
++;
2702 binder_context_mgr_node
->has_strong_ref
= 1;
2703 binder_context_mgr_node
->has_weak_ref
= 1;
2705 case BINDER_THREAD_EXIT
:
2706 binder_debug(BINDER_DEBUG_THREADS
, "binder: %d:%d exit\n",
2707 proc
->pid
, thread
->pid
);
2708 binder_free_thread(proc
, thread
);
2711 case BINDER_VERSION
:
2712 if (size
!= sizeof(struct binder_version
)) {
2716 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION
, &((struct binder_version
*)ubuf
)->protocol_version
)) {
2728 thread
->looper
&= ~BINDER_LOOPER_STATE_NEED_RETURN
;
2729 mutex_unlock(&binder_lock
);
2730 wait_event_interruptible(binder_user_error_wait
, binder_stop_on_user_error
< 2);
2731 if (ret
&& ret
!= -ERESTARTSYS
)
2732 printk(KERN_INFO
"binder: %d:%d ioctl %x %lx returned %d\n", proc
->pid
, current
->pid
, cmd
, arg
, ret
);
2736 static void binder_vma_open(struct vm_area_struct
*vma
)
2738 struct binder_proc
*proc
= vma
->vm_private_data
;
2739 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
2740 "binder: %d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
2741 proc
->pid
, vma
->vm_start
, vma
->vm_end
,
2742 (vma
->vm_end
- vma
->vm_start
) / SZ_1K
, vma
->vm_flags
,
2743 (unsigned long)pgprot_val(vma
->vm_page_prot
));
2747 static void binder_vma_close(struct vm_area_struct
*vma
)
2749 struct binder_proc
*proc
= vma
->vm_private_data
;
2750 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
2751 "binder: %d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
2752 proc
->pid
, vma
->vm_start
, vma
->vm_end
,
2753 (vma
->vm_end
- vma
->vm_start
) / SZ_1K
, vma
->vm_flags
,
2754 (unsigned long)pgprot_val(vma
->vm_page_prot
));
2756 binder_defer_work(proc
, BINDER_DEFERRED_PUT_FILES
);
2759 static struct vm_operations_struct binder_vm_ops
= {
2760 .open
= binder_vma_open
,
2761 .close
= binder_vma_close
,
2764 static int binder_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
2767 struct vm_struct
*area
;
2768 struct binder_proc
*proc
= filp
->private_data
;
2769 const char *failure_string
;
2770 struct binder_buffer
*buffer
;
2772 if ((vma
->vm_end
- vma
->vm_start
) > SZ_4M
)
2773 vma
->vm_end
= vma
->vm_start
+ SZ_4M
;
2775 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
2776 "binder_mmap: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
2777 proc
->pid
, vma
->vm_start
, vma
->vm_end
,
2778 (vma
->vm_end
- vma
->vm_start
) / SZ_1K
, vma
->vm_flags
,
2779 (unsigned long)pgprot_val(vma
->vm_page_prot
));
2781 if (vma
->vm_flags
& FORBIDDEN_MMAP_FLAGS
) {
2783 failure_string
= "bad vm_flags";
2786 vma
->vm_flags
= (vma
->vm_flags
| VM_DONTCOPY
) & ~VM_MAYWRITE
;
2790 failure_string
= "already mapped";
2791 goto err_already_mapped
;
2794 area
= get_vm_area(vma
->vm_end
- vma
->vm_start
, VM_IOREMAP
);
2797 failure_string
= "get_vm_area";
2798 goto err_get_vm_area_failed
;
2800 proc
->buffer
= area
->addr
;
2801 proc
->user_buffer_offset
= vma
->vm_start
- (uintptr_t)proc
->buffer
;
2803 #ifdef CONFIG_CPU_CACHE_VIPT
2804 if (cache_is_vipt_aliasing()) {
2805 while (CACHE_COLOUR((vma
->vm_start
^ (uint32_t)proc
->buffer
))) {
2806 printk(KERN_INFO
"binder_mmap: %d %lx-%lx maps %p bad alignment\n", proc
->pid
, vma
->vm_start
, vma
->vm_end
, proc
->buffer
);
2807 vma
->vm_start
+= PAGE_SIZE
;
2811 proc
->pages
= kzalloc(sizeof(proc
->pages
[0]) * ((vma
->vm_end
- vma
->vm_start
) / PAGE_SIZE
), GFP_KERNEL
);
2812 if (proc
->pages
== NULL
) {
2814 failure_string
= "alloc page array";
2815 goto err_alloc_pages_failed
;
2817 proc
->buffer_size
= vma
->vm_end
- vma
->vm_start
;
2819 vma
->vm_ops
= &binder_vm_ops
;
2820 vma
->vm_private_data
= proc
;
2822 if (binder_update_page_range(proc
, 1, proc
->buffer
, proc
->buffer
+ PAGE_SIZE
, vma
)) {
2824 failure_string
= "alloc small buf";
2825 goto err_alloc_small_buf_failed
;
2827 buffer
= proc
->buffer
;
2828 INIT_LIST_HEAD(&proc
->buffers
);
2829 list_add(&buffer
->entry
, &proc
->buffers
);
2831 binder_insert_free_buffer(proc
, buffer
);
2832 proc
->free_async_space
= proc
->buffer_size
/ 2;
2834 proc
->files
= get_files_struct(current
);
2837 /*printk(KERN_INFO "binder_mmap: %d %lx-%lx maps %p\n",
2838 proc->pid, vma->vm_start, vma->vm_end, proc->buffer);*/
2841 err_alloc_small_buf_failed
:
2844 err_alloc_pages_failed
:
2845 vfree(proc
->buffer
);
2846 proc
->buffer
= NULL
;
2847 err_get_vm_area_failed
:
2850 printk(KERN_ERR
"binder_mmap: %d %lx-%lx %s failed %d\n",
2851 proc
->pid
, vma
->vm_start
, vma
->vm_end
, failure_string
, ret
);
2855 static int binder_open(struct inode
*nodp
, struct file
*filp
)
2857 struct binder_proc
*proc
;
2859 binder_debug(BINDER_DEBUG_OPEN_CLOSE
, "binder_open: %d:%d\n",
2860 current
->group_leader
->pid
, current
->pid
);
2862 proc
= kzalloc(sizeof(*proc
), GFP_KERNEL
);
2865 get_task_struct(current
);
2866 proc
->tsk
= current
;
2867 INIT_LIST_HEAD(&proc
->todo
);
2868 init_waitqueue_head(&proc
->wait
);
2869 proc
->default_priority
= task_nice(current
);
2870 mutex_lock(&binder_lock
);
2871 binder_stats_created(BINDER_STAT_PROC
);
2872 hlist_add_head(&proc
->proc_node
, &binder_procs
);
2873 proc
->pid
= current
->group_leader
->pid
;
2874 INIT_LIST_HEAD(&proc
->delivered_death
);
2875 filp
->private_data
= proc
;
2876 mutex_unlock(&binder_lock
);
2878 if (binder_proc_dir_entry_proc
) {
2880 snprintf(strbuf
, sizeof(strbuf
), "%u", proc
->pid
);
2881 remove_proc_entry(strbuf
, binder_proc_dir_entry_proc
);
2882 create_proc_read_entry(strbuf
, S_IRUGO
,
2883 binder_proc_dir_entry_proc
,
2884 binder_read_proc_proc
, proc
);
2890 static int binder_flush(struct file
*filp
, fl_owner_t id
)
2892 struct binder_proc
*proc
= filp
->private_data
;
2894 binder_defer_work(proc
, BINDER_DEFERRED_FLUSH
);
2899 static void binder_deferred_flush(struct binder_proc
*proc
)
2903 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
)) {
2904 struct binder_thread
*thread
= rb_entry(n
, struct binder_thread
, rb_node
);
2905 thread
->looper
|= BINDER_LOOPER_STATE_NEED_RETURN
;
2906 if (thread
->looper
& BINDER_LOOPER_STATE_WAITING
) {
2907 wake_up_interruptible(&thread
->wait
);
2911 wake_up_interruptible_all(&proc
->wait
);
2913 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
2914 "binder_flush: %d woke %d threads\n", proc
->pid
,
2918 static int binder_release(struct inode
*nodp
, struct file
*filp
)
2920 struct binder_proc
*proc
= filp
->private_data
;
2921 if (binder_proc_dir_entry_proc
) {
2923 snprintf(strbuf
, sizeof(strbuf
), "%u", proc
->pid
);
2924 remove_proc_entry(strbuf
, binder_proc_dir_entry_proc
);
2927 binder_defer_work(proc
, BINDER_DEFERRED_RELEASE
);
2932 static void binder_deferred_release(struct binder_proc
*proc
)
2934 struct hlist_node
*pos
;
2935 struct binder_transaction
*t
;
2937 int threads
, nodes
, incoming_refs
, outgoing_refs
, buffers
, active_transactions
, page_count
;
2940 BUG_ON(proc
->files
);
2942 hlist_del(&proc
->proc_node
);
2943 if (binder_context_mgr_node
&& binder_context_mgr_node
->proc
== proc
) {
2944 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
2945 "binder_release: %d context_mgr_node gone\n",
2947 binder_context_mgr_node
= NULL
;
2951 active_transactions
= 0;
2952 while ((n
= rb_first(&proc
->threads
))) {
2953 struct binder_thread
*thread
= rb_entry(n
, struct binder_thread
, rb_node
);
2955 active_transactions
+= binder_free_thread(proc
, thread
);
2959 while ((n
= rb_first(&proc
->nodes
))) {
2960 struct binder_node
*node
= rb_entry(n
, struct binder_node
, rb_node
);
2963 rb_erase(&node
->rb_node
, &proc
->nodes
);
2964 list_del_init(&node
->work
.entry
);
2965 if (hlist_empty(&node
->refs
)) {
2967 binder_stats_deleted(BINDER_STAT_NODE
);
2969 struct binder_ref
*ref
;
2973 node
->local_strong_refs
= 0;
2974 node
->local_weak_refs
= 0;
2975 hlist_add_head(&node
->dead_node
, &binder_dead_nodes
);
2977 hlist_for_each_entry(ref
, pos
, &node
->refs
, node_entry
) {
2981 if (list_empty(&ref
->death
->work
.entry
)) {
2982 ref
->death
->work
.type
= BINDER_WORK_DEAD_BINDER
;
2983 list_add_tail(&ref
->death
->work
.entry
, &ref
->proc
->todo
);
2984 wake_up_interruptible(&ref
->proc
->wait
);
2989 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
2990 "binder: node %d now dead, "
2991 "refs %d, death %d\n", node
->debug_id
,
2992 incoming_refs
, death
);
2996 while ((n
= rb_first(&proc
->refs_by_desc
))) {
2997 struct binder_ref
*ref
= rb_entry(n
, struct binder_ref
,
3000 binder_delete_ref(ref
);
3002 binder_release_work(&proc
->todo
);
3005 while ((n
= rb_first(&proc
->allocated_buffers
))) {
3006 struct binder_buffer
*buffer
= rb_entry(n
, struct binder_buffer
,
3008 t
= buffer
->transaction
;
3011 buffer
->transaction
= NULL
;
3012 printk(KERN_ERR
"binder: release proc %d, "
3013 "transaction %d, not freed\n",
3014 proc
->pid
, t
->debug_id
);
3017 binder_free_buf(proc
, buffer
);
3021 binder_stats_deleted(BINDER_STAT_PROC
);
3026 for (i
= 0; i
< proc
->buffer_size
/ PAGE_SIZE
; i
++) {
3027 if (proc
->pages
[i
]) {
3028 binder_debug(BINDER_DEBUG_BUFFER_ALLOC
,
3029 "binder_release: %d: "
3030 "page %d at %p not freed\n",
3032 proc
->buffer
+ i
* PAGE_SIZE
);
3033 __free_page(proc
->pages
[i
]);
3038 vfree(proc
->buffer
);
3041 put_task_struct(proc
->tsk
);
3043 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
3044 "binder_release: %d threads %d, nodes %d (ref %d), "
3045 "refs %d, active transactions %d, buffers %d, "
3047 proc
->pid
, threads
, nodes
, incoming_refs
, outgoing_refs
,
3048 active_transactions
, buffers
, page_count
);
3053 static void binder_deferred_func(struct work_struct
*work
)
3055 struct binder_proc
*proc
;
3056 struct files_struct
*files
;
3060 mutex_lock(&binder_lock
);
3061 mutex_lock(&binder_deferred_lock
);
3062 if (!hlist_empty(&binder_deferred_list
)) {
3063 proc
= hlist_entry(binder_deferred_list
.first
,
3064 struct binder_proc
, deferred_work_node
);
3065 hlist_del_init(&proc
->deferred_work_node
);
3066 defer
= proc
->deferred_work
;
3067 proc
->deferred_work
= 0;
3072 mutex_unlock(&binder_deferred_lock
);
3075 if (defer
& BINDER_DEFERRED_PUT_FILES
) {
3076 files
= proc
->files
;
3081 if (defer
& BINDER_DEFERRED_FLUSH
)
3082 binder_deferred_flush(proc
);
3084 if (defer
& BINDER_DEFERRED_RELEASE
)
3085 binder_deferred_release(proc
); /* frees proc */
3087 mutex_unlock(&binder_lock
);
3089 put_files_struct(files
);
3092 static DECLARE_WORK(binder_deferred_work
, binder_deferred_func
);
3095 binder_defer_work(struct binder_proc
*proc
, enum binder_deferred_state defer
)
3097 mutex_lock(&binder_deferred_lock
);
3098 proc
->deferred_work
|= defer
;
3099 if (hlist_unhashed(&proc
->deferred_work_node
)) {
3100 hlist_add_head(&proc
->deferred_work_node
,
3101 &binder_deferred_list
);
3102 schedule_work(&binder_deferred_work
);
3104 mutex_unlock(&binder_deferred_lock
);
3107 static char *print_binder_transaction(char *buf
, char *end
, const char *prefix
,
3108 struct binder_transaction
*t
)
3110 buf
+= snprintf(buf
, end
- buf
,
3111 "%s %d: %p from %d:%d to %d:%d code %x "
3112 "flags %x pri %ld r%d",
3113 prefix
, t
->debug_id
, t
,
3114 t
->from
? t
->from
->proc
->pid
: 0,
3115 t
->from
? t
->from
->pid
: 0,
3116 t
->to_proc
? t
->to_proc
->pid
: 0,
3117 t
->to_thread
? t
->to_thread
->pid
: 0,
3118 t
->code
, t
->flags
, t
->priority
, t
->need_reply
);
3121 if (t
->buffer
== NULL
) {
3122 buf
+= snprintf(buf
, end
- buf
, " buffer free\n");
3125 if (t
->buffer
->target_node
) {
3126 buf
+= snprintf(buf
, end
- buf
, " node %d",
3127 t
->buffer
->target_node
->debug_id
);
3131 buf
+= snprintf(buf
, end
- buf
, " size %zd:%zd data %p\n",
3132 t
->buffer
->data_size
, t
->buffer
->offsets_size
,
3137 static char *print_binder_buffer(char *buf
, char *end
, const char *prefix
,
3138 struct binder_buffer
*buffer
)
3140 buf
+= snprintf(buf
, end
- buf
, "%s %d: %p size %zd:%zd %s\n",
3141 prefix
, buffer
->debug_id
, buffer
->data
,
3142 buffer
->data_size
, buffer
->offsets_size
,
3143 buffer
->transaction
? "active" : "delivered");
3147 static char *print_binder_work(char *buf
, char *end
, const char *prefix
,
3148 const char *transaction_prefix
,
3149 struct binder_work
*w
)
3151 struct binder_node
*node
;
3152 struct binder_transaction
*t
;
3155 case BINDER_WORK_TRANSACTION
:
3156 t
= container_of(w
, struct binder_transaction
, work
);
3157 buf
= print_binder_transaction(buf
, end
, transaction_prefix
, t
);
3159 case BINDER_WORK_TRANSACTION_COMPLETE
:
3160 buf
+= snprintf(buf
, end
- buf
,
3161 "%stransaction complete\n", prefix
);
3163 case BINDER_WORK_NODE
:
3164 node
= container_of(w
, struct binder_node
, work
);
3165 buf
+= snprintf(buf
, end
- buf
, "%snode work %d: u%p c%p\n",
3166 prefix
, node
->debug_id
, node
->ptr
,
3169 case BINDER_WORK_DEAD_BINDER
:
3170 buf
+= snprintf(buf
, end
- buf
, "%shas dead binder\n", prefix
);
3172 case BINDER_WORK_DEAD_BINDER_AND_CLEAR
:
3173 buf
+= snprintf(buf
, end
- buf
,
3174 "%shas cleared dead binder\n", prefix
);
3176 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION
:
3177 buf
+= snprintf(buf
, end
- buf
,
3178 "%shas cleared death notification\n", prefix
);
3181 buf
+= snprintf(buf
, end
- buf
, "%sunknown work: type %d\n",
3188 static char *print_binder_thread(char *buf
, char *end
,
3189 struct binder_thread
*thread
,
3192 struct binder_transaction
*t
;
3193 struct binder_work
*w
;
3194 char *start_buf
= buf
;
3197 buf
+= snprintf(buf
, end
- buf
, " thread %d: l %02x\n",
3198 thread
->pid
, thread
->looper
);
3200 t
= thread
->transaction_stack
;
3204 if (t
->from
== thread
) {
3205 buf
= print_binder_transaction(buf
, end
,
3206 " outgoing transaction", t
);
3208 } else if (t
->to_thread
== thread
) {
3209 buf
= print_binder_transaction(buf
, end
,
3210 " incoming transaction", t
);
3213 buf
= print_binder_transaction(buf
, end
,
3214 " bad transaction", t
);
3218 list_for_each_entry(w
, &thread
->todo
, entry
) {
3221 buf
= print_binder_work(buf
, end
, " ",
3222 " pending transaction", w
);
3224 if (!print_always
&& buf
== header_buf
)
3229 static char *print_binder_node(char *buf
, char *end
, struct binder_node
*node
)
3231 struct binder_ref
*ref
;
3232 struct hlist_node
*pos
;
3233 struct binder_work
*w
;
3237 hlist_for_each_entry(ref
, pos
, &node
->refs
, node_entry
)
3240 buf
+= snprintf(buf
, end
- buf
,
3241 " node %d: u%p c%p hs %d hw %d ls %d lw %d "
3243 node
->debug_id
, node
->ptr
, node
->cookie
,
3244 node
->has_strong_ref
, node
->has_weak_ref
,
3245 node
->local_strong_refs
, node
->local_weak_refs
,
3246 node
->internal_strong_refs
, count
);
3250 buf
+= snprintf(buf
, end
- buf
, " proc");
3253 hlist_for_each_entry(ref
, pos
, &node
->refs
, node_entry
) {
3254 buf
+= snprintf(buf
, end
- buf
, " %d", ref
->proc
->pid
);
3259 buf
+= snprintf(buf
, end
- buf
, "\n");
3260 list_for_each_entry(w
, &node
->async_todo
, entry
) {
3263 buf
= print_binder_work(buf
, end
, " ",
3264 " pending async transaction", w
);
3269 static char *print_binder_ref(char *buf
, char *end
, struct binder_ref
*ref
)
3271 buf
+= snprintf(buf
, end
- buf
,
3272 " ref %d: desc %d %snode %d s %d w %d d %p\n",
3273 ref
->debug_id
, ref
->desc
,
3274 ref
->node
->proc
? "" : "dead ", ref
->node
->debug_id
,
3275 ref
->strong
, ref
->weak
, ref
->death
);
3279 static char *print_binder_proc(char *buf
, char *end
,
3280 struct binder_proc
*proc
, int print_all
)
3282 struct binder_work
*w
;
3284 char *start_buf
= buf
;
3287 buf
+= snprintf(buf
, end
- buf
, "proc %d\n", proc
->pid
);
3290 for (n
= rb_first(&proc
->threads
);
3291 n
!= NULL
&& buf
< end
;
3293 buf
= print_binder_thread(buf
, end
,
3294 rb_entry(n
, struct binder_thread
,
3295 rb_node
), print_all
);
3296 for (n
= rb_first(&proc
->nodes
);
3297 n
!= NULL
&& buf
< end
;
3299 struct binder_node
*node
= rb_entry(n
, struct binder_node
,
3301 if (print_all
|| node
->has_async_transaction
)
3302 buf
= print_binder_node(buf
, end
, node
);
3305 for (n
= rb_first(&proc
->refs_by_desc
);
3306 n
!= NULL
&& buf
< end
;
3308 buf
= print_binder_ref(buf
, end
,
3309 rb_entry(n
, struct binder_ref
,
3312 for (n
= rb_first(&proc
->allocated_buffers
);
3313 n
!= NULL
&& buf
< end
;
3315 buf
= print_binder_buffer(buf
, end
, " buffer",
3316 rb_entry(n
, struct binder_buffer
,
3318 list_for_each_entry(w
, &proc
->todo
, entry
) {
3321 buf
= print_binder_work(buf
, end
, " ",
3322 " pending transaction", w
);
3324 list_for_each_entry(w
, &proc
->delivered_death
, entry
) {
3327 buf
+= snprintf(buf
, end
- buf
,
3328 " has delivered dead binder\n");
3331 if (!print_all
&& buf
== header_buf
)
3336 static const char *binder_return_strings
[] = {
3341 "BR_ACQUIRE_RESULT",
3343 "BR_TRANSACTION_COMPLETE",
3348 "BR_ATTEMPT_ACQUIRE",
3353 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
3357 static const char *binder_command_strings
[] = {
3360 "BC_ACQUIRE_RESULT",
3368 "BC_ATTEMPT_ACQUIRE",
3369 "BC_REGISTER_LOOPER",
3372 "BC_REQUEST_DEATH_NOTIFICATION",
3373 "BC_CLEAR_DEATH_NOTIFICATION",
3374 "BC_DEAD_BINDER_DONE"
3377 static const char *binder_objstat_strings
[] = {
3384 "transaction_complete"
3387 static char *print_binder_stats(char *buf
, char *end
, const char *prefix
,
3388 struct binder_stats
*stats
)
3392 BUILD_BUG_ON(ARRAY_SIZE(stats
->bc
) !=
3393 ARRAY_SIZE(binder_command_strings
));
3394 for (i
= 0; i
< ARRAY_SIZE(stats
->bc
); i
++) {
3396 buf
+= snprintf(buf
, end
- buf
, "%s%s: %d\n", prefix
,
3397 binder_command_strings
[i
],
3403 BUILD_BUG_ON(ARRAY_SIZE(stats
->br
) !=
3404 ARRAY_SIZE(binder_return_strings
));
3405 for (i
= 0; i
< ARRAY_SIZE(stats
->br
); i
++) {
3407 buf
+= snprintf(buf
, end
- buf
, "%s%s: %d\n", prefix
,
3408 binder_return_strings
[i
], stats
->br
[i
]);
3413 BUILD_BUG_ON(ARRAY_SIZE(stats
->obj_created
) !=
3414 ARRAY_SIZE(binder_objstat_strings
));
3415 BUILD_BUG_ON(ARRAY_SIZE(stats
->obj_created
) !=
3416 ARRAY_SIZE(stats
->obj_deleted
));
3417 for (i
= 0; i
< ARRAY_SIZE(stats
->obj_created
); i
++) {
3418 if (stats
->obj_created
[i
] || stats
->obj_deleted
[i
])
3419 buf
+= snprintf(buf
, end
- buf
,
3420 "%s%s: active %d total %d\n", prefix
,
3421 binder_objstat_strings
[i
],
3422 stats
->obj_created
[i
] -
3423 stats
->obj_deleted
[i
],
3424 stats
->obj_created
[i
]);
3431 static char *print_binder_proc_stats(char *buf
, char *end
,
3432 struct binder_proc
*proc
)
3434 struct binder_work
*w
;
3436 int count
, strong
, weak
;
3438 buf
+= snprintf(buf
, end
- buf
, "proc %d\n", proc
->pid
);
3442 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
))
3444 buf
+= snprintf(buf
, end
- buf
, " threads: %d\n", count
);
3447 buf
+= snprintf(buf
, end
- buf
, " requested threads: %d+%d/%d\n"
3448 " ready threads %d\n"
3449 " free async space %zd\n", proc
->requested_threads
,
3450 proc
->requested_threads_started
, proc
->max_threads
,
3451 proc
->ready_threads
, proc
->free_async_space
);
3455 for (n
= rb_first(&proc
->nodes
); n
!= NULL
; n
= rb_next(n
))
3457 buf
+= snprintf(buf
, end
- buf
, " nodes: %d\n", count
);
3463 for (n
= rb_first(&proc
->refs_by_desc
); n
!= NULL
; n
= rb_next(n
)) {
3464 struct binder_ref
*ref
= rb_entry(n
, struct binder_ref
,
3467 strong
+= ref
->strong
;
3470 buf
+= snprintf(buf
, end
- buf
, " refs: %d s %d w %d\n",
3471 count
, strong
, weak
);
3476 for (n
= rb_first(&proc
->allocated_buffers
); n
!= NULL
; n
= rb_next(n
))
3478 buf
+= snprintf(buf
, end
- buf
, " buffers: %d\n", count
);
3483 list_for_each_entry(w
, &proc
->todo
, entry
) {
3485 case BINDER_WORK_TRANSACTION
:
3492 buf
+= snprintf(buf
, end
- buf
, " pending transactions: %d\n", count
);
3496 buf
= print_binder_stats(buf
, end
, " ", &proc
->stats
);
3502 static int binder_read_proc_state(char *page
, char **start
, off_t off
,
3503 int count
, int *eof
, void *data
)
3505 struct binder_proc
*proc
;
3506 struct hlist_node
*pos
;
3507 struct binder_node
*node
;
3510 char *end
= page
+ PAGE_SIZE
;
3511 int do_lock
= !binder_debug_no_lock
;
3517 mutex_lock(&binder_lock
);
3519 buf
+= snprintf(buf
, end
- buf
, "binder state:\n");
3521 if (!hlist_empty(&binder_dead_nodes
))
3522 buf
+= snprintf(buf
, end
- buf
, "dead nodes:\n");
3523 hlist_for_each_entry(node
, pos
, &binder_dead_nodes
, dead_node
) {
3526 buf
= print_binder_node(buf
, end
, node
);
3529 hlist_for_each_entry(proc
, pos
, &binder_procs
, proc_node
) {
3532 buf
= print_binder_proc(buf
, end
, proc
, 1);
3535 mutex_unlock(&binder_lock
);
3536 if (buf
> page
+ PAGE_SIZE
)
3537 buf
= page
+ PAGE_SIZE
;
3539 *start
= page
+ off
;
3547 return len
< count
? len
: count
;
3550 static int binder_read_proc_stats(char *page
, char **start
, off_t off
,
3551 int count
, int *eof
, void *data
)
3553 struct binder_proc
*proc
;
3554 struct hlist_node
*pos
;
3557 int do_lock
= !binder_debug_no_lock
;
3563 mutex_lock(&binder_lock
);
3565 p
+= snprintf(p
, PAGE_SIZE
, "binder stats:\n");
3567 p
= print_binder_stats(p
, page
+ PAGE_SIZE
, "", &binder_stats
);
3569 hlist_for_each_entry(proc
, pos
, &binder_procs
, proc_node
) {
3570 if (p
>= page
+ PAGE_SIZE
)
3572 p
= print_binder_proc_stats(p
, page
+ PAGE_SIZE
, proc
);
3575 mutex_unlock(&binder_lock
);
3576 if (p
> page
+ PAGE_SIZE
)
3577 p
= page
+ PAGE_SIZE
;
3579 *start
= page
+ off
;
3587 return len
< count
? len
: count
;
3590 static int binder_read_proc_transactions(char *page
, char **start
, off_t off
,
3591 int count
, int *eof
, void *data
)
3593 struct binder_proc
*proc
;
3594 struct hlist_node
*pos
;
3597 char *end
= page
+ PAGE_SIZE
;
3598 int do_lock
= !binder_debug_no_lock
;
3604 mutex_lock(&binder_lock
);
3606 buf
+= snprintf(buf
, end
- buf
, "binder transactions:\n");
3607 hlist_for_each_entry(proc
, pos
, &binder_procs
, proc_node
) {
3610 buf
= print_binder_proc(buf
, end
, proc
, 0);
3613 mutex_unlock(&binder_lock
);
3614 if (buf
> page
+ PAGE_SIZE
)
3615 buf
= page
+ PAGE_SIZE
;
3617 *start
= page
+ off
;
3625 return len
< count
? len
: count
;
3628 static int binder_read_proc_proc(char *page
, char **start
, off_t off
,
3629 int count
, int *eof
, void *data
)
3631 struct binder_proc
*proc
= data
;
3634 int do_lock
= !binder_debug_no_lock
;
3640 mutex_lock(&binder_lock
);
3641 p
+= snprintf(p
, PAGE_SIZE
, "binder proc state:\n");
3642 p
= print_binder_proc(p
, page
+ PAGE_SIZE
, proc
, 1);
3644 mutex_unlock(&binder_lock
);
3646 if (p
> page
+ PAGE_SIZE
)
3647 p
= page
+ PAGE_SIZE
;
3648 *start
= page
+ off
;
3656 return len
< count
? len
: count
;
3659 static char *print_binder_transaction_log_entry(char *buf
, char *end
,
3660 struct binder_transaction_log_entry
*e
)
3662 buf
+= snprintf(buf
, end
- buf
,
3663 "%d: %s from %d:%d to %d:%d node %d handle %d "
3665 e
->debug_id
, (e
->call_type
== 2) ? "reply" :
3666 ((e
->call_type
== 1) ? "async" : "call "), e
->from_proc
,
3667 e
->from_thread
, e
->to_proc
, e
->to_thread
, e
->to_node
,
3668 e
->target_handle
, e
->data_size
, e
->offsets_size
);
3672 static int binder_read_proc_transaction_log(
3673 char *page
, char **start
, off_t off
, int count
, int *eof
, void *data
)
3675 struct binder_transaction_log
*log
= data
;
3679 char *end
= page
+ PAGE_SIZE
;
3685 for (i
= log
->next
; i
< ARRAY_SIZE(log
->entry
); i
++) {
3688 buf
= print_binder_transaction_log_entry(buf
, end
,
3692 for (i
= 0; i
< log
->next
; i
++) {
3695 buf
= print_binder_transaction_log_entry(buf
, end
,
3699 *start
= page
+ off
;
3707 return len
< count
? len
: count
;
3710 static const struct file_operations binder_fops
= {
3711 .owner
= THIS_MODULE
,
3712 .poll
= binder_poll
,
3713 .unlocked_ioctl
= binder_ioctl
,
3714 .mmap
= binder_mmap
,
3715 .open
= binder_open
,
3716 .flush
= binder_flush
,
3717 .release
= binder_release
,
3720 static struct miscdevice binder_miscdev
= {
3721 .minor
= MISC_DYNAMIC_MINOR
,
3723 .fops
= &binder_fops
3726 static int __init
binder_init(void)
3730 binder_proc_dir_entry_root
= proc_mkdir("binder", NULL
);
3731 if (binder_proc_dir_entry_root
)
3732 binder_proc_dir_entry_proc
= proc_mkdir("proc",
3733 binder_proc_dir_entry_root
);
3734 ret
= misc_register(&binder_miscdev
);
3735 if (binder_proc_dir_entry_root
) {
3736 create_proc_read_entry("state",
3738 binder_proc_dir_entry_root
,
3739 binder_read_proc_state
,
3741 create_proc_read_entry("stats",
3743 binder_proc_dir_entry_root
,
3744 binder_read_proc_stats
,
3746 create_proc_read_entry("transactions",
3748 binder_proc_dir_entry_root
,
3749 binder_read_proc_transactions
,
3751 create_proc_read_entry("transaction_log",
3753 binder_proc_dir_entry_root
,
3754 binder_read_proc_transaction_log
,
3755 &binder_transaction_log
);
3756 create_proc_read_entry("failed_transaction_log",
3758 binder_proc_dir_entry_root
,
3759 binder_read_proc_transaction_log
,
3760 &binder_transaction_log_failed
);
3765 device_initcall(binder_init
);
3767 MODULE_LICENSE("GPL v2");