18 #include <asm/cacheflush.h>
22 #include <linux/list.h>
25 #include <linux/module.h>
28 #include <linux/poll.h>
30 #include <linux/rbtree.h>
31 #include <linux/sched.h>
35 #include <linux/slab.h>
47 static struct dentry *binder_debugfs_dir_entry_root;
48 static struct dentry *binder_debugfs_dir_entry_proc;
51 static int binder_last_id;
54 #define BINDER_DEBUG_ENTRY(name) \
55 static int binder_##name##_open(struct inode *inode, struct file *file) \
57 return single_open(file, binder_##name##_show, inode->i_private); \
60 static const struct file_operations binder_##name##_fops = { \
61 .owner = THIS_MODULE, \
62 .open = binder_##name##_open, \
64 .llseek = seq_lseek, \
65 .release = single_release, \
77 #define SZ_4M 0x400000
80 #define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
82 #define BINDER_SMALL_BUF_SIZE (PAGE_SIZE * 64)
106 static bool binder_debug_no_lock;
110 static int binder_stop_on_user_error;
112 static int binder_set_stop_on_user_error(
const char *
val,
117 if (binder_stop_on_user_error < 2)
118 wake_up(&binder_user_error_wait);
124 #define binder_debug(mask, x...) \
126 if (binder_debug_mask & mask) \
130 #define binder_user_error(x...) \
132 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
134 if (binder_stop_on_user_error) \
135 binder_stop_on_user_error = 2; \
368 unsigned long rlim_cur;
374 if (!lock_task_sighand(proc->
tsk, &irqs))
378 unlock_task_sighand(proc->
tsk, &irqs);
386 static void task_fd_install(
396 static long task_close_fd(
struct binder_proc *proc,
unsigned int fd)
414 static void binder_set_nice(
long nice)
423 "binder: %d: nice value %ld not allowed use "
424 "%ld instead\n",
current->pid, nice, min_nice);
431 static size_t binder_buffer_size(
struct binder_proc *proc,
441 static void binder_insert_free_buffer(
struct binder_proc *proc,
448 size_t new_buffer_size;
452 new_buffer_size = binder_buffer_size(proc, new_buffer);
455 "binder: %d: add free buffer, size %zd, "
456 "at %p\n", proc->
pid, new_buffer_size, new_buffer);
463 buffer_size = binder_buffer_size(proc, buffer);
465 if (new_buffer_size < buffer_size)
470 rb_link_node(&new_buffer->
rb_node, parent, p);
474 static void binder_insert_allocated_buffer(
struct binder_proc *proc,
488 if (new_buffer < buffer)
490 else if (new_buffer > buffer)
495 rb_link_node(&new_buffer->
rb_node, parent, p);
500 void __user *user_ptr)
513 if (kern_ptr < buffer)
515 else if (kern_ptr > buffer)
523 static int binder_update_page_range(
struct binder_proc *proc,
int allocate,
528 unsigned long user_page_addr;
534 "binder: %d: %s pages %p-%p\n", proc->
pid,
535 allocate ?
"allocate" :
"free", start, end);
549 pr_err(
"binder: %d: vma mm and task mm mismatch\n",
559 pr_err(
"binder: %d: binder_alloc_buf failed to "
560 "map pages in userspace, no vma\n", proc->
pid);
564 for (page_addr = start; page_addr <
end; page_addr +=
PAGE_SIZE) {
566 struct page **page_array_ptr;
572 pr_err(
"binder: %d: binder_alloc_buf failed "
573 "for page at %p\n", proc->
pid, page_addr);
574 goto err_alloc_page_failed;
576 tmp_area.addr = page_addr;
578 page_array_ptr =
page;
579 ret =
map_vm_area(&tmp_area, PAGE_KERNEL, &page_array_ptr);
581 pr_err(
"binder: %d: binder_alloc_buf failed "
582 "to map page at %p in kernel\n",
583 proc->
pid, page_addr);
584 goto err_map_kernel_failed;
590 pr_err(
"binder: %d: binder_alloc_buf failed "
591 "to map page at %lx in userspace\n",
592 proc->
pid, user_page_addr);
593 goto err_vm_insert_page_failed;
604 for (page_addr = end - PAGE_SIZE; page_addr >=
start;
606 page = &proc->
pages[(page_addr - proc->
buffer) / PAGE_SIZE];
610 err_vm_insert_page_failed:
612 err_map_kernel_failed:
615 err_alloc_page_failed:
639 pr_err(
"binder: %d: binder_alloc_buf, no vma\n",
644 size =
ALIGN(data_size,
sizeof(
void *)) +
645 ALIGN(offsets_size,
sizeof(
void *));
647 if (size < data_size || size < offsets_size) {
649 "size %zd-%zd\n", proc->
pid, data_size, offsets_size);
656 "binder: %d: binder_alloc_buf size %zd"
657 "failed, no async space left\n", proc->
pid, size);
664 buffer_size = binder_buffer_size(proc, buffer);
666 if (size < buffer_size) {
669 }
else if (size > buffer_size)
676 if (best_fit ==
NULL) {
677 pr_err(
"binder: %d: binder_alloc_buf size %zd failed, "
678 "no address space\n", proc->
pid, size);
683 buffer_size = binder_buffer_size(proc, buffer);
687 "binder: %d: binder_alloc_buf size %zd got buff"
688 "er %p size %zd\n", proc->
pid, size, buffer, buffer_size);
700 if (end_page_addr > has_page_addr)
701 end_page_addr = has_page_addr;
702 if (binder_update_page_range(proc, 1,
708 binder_insert_allocated_buffer(proc, buffer);
709 if (buffer_size != size) {
712 new_buffer->
free = 1;
713 binder_insert_free_buffer(proc, new_buffer);
716 "binder: %d: binder_alloc_buf size %zd got "
717 "%p\n", proc->
pid, size, buffer);
724 "binder: %d: binder_alloc_buf size %zd "
725 "async free %zd\n", proc->
pid, size,
742 static void binder_delete_free_buffer(
struct binder_proc *proc,
746 int free_page_end = 1;
747 int free_page_start = 1;
752 if (buffer_end_page(prev) == buffer_start_page(buffer)) {
754 if (buffer_end_page(prev) == buffer_end_page(buffer))
757 "binder: %d: merge free, buffer %p "
758 "share page with %p\n", proc->
pid, buffer, prev);
764 if (buffer_start_page(next) == buffer_end_page(buffer)) {
766 if (buffer_start_page(next) ==
767 buffer_start_page(buffer))
770 "binder: %d: merge free, buffer"
771 " %p share page with %p\n", proc->
pid,
776 if (free_page_start || free_page_end) {
778 "binder: %d: merge free, buffer %p do "
779 "not share page%s%s with with %p or %p\n",
780 proc->
pid, buffer, free_page_start ?
"" :
" end",
781 free_page_end ?
"" :
" start", prev, next);
782 binder_update_page_range(proc, 0, free_page_start ?
783 buffer_start_page(buffer) : buffer_end_page(buffer),
784 (free_page_end ? buffer_end_page(buffer) :
785 buffer_start_page(buffer)) + PAGE_SIZE,
NULL);
789 static void binder_free_buf(
struct binder_proc *proc,
792 size_t size, buffer_size;
794 buffer_size = binder_buffer_size(proc, buffer);
800 "binder: %d: binder_free_buf %p size %zd buffer"
801 "_size %zd\n", proc->
pid, buffer, size, buffer_size);
804 BUG_ON(size > buffer_size);
813 "binder: %d: binder_free_buf size %zd "
814 "async free %zd\n", proc->
pid, size,
818 binder_update_page_range(proc, 0,
829 binder_delete_free_buffer(proc, next);
836 binder_delete_free_buffer(proc, buffer);
841 binder_insert_free_buffer(proc, buffer);
855 else if (ptr > node->
ptr)
877 else if (ptr > node->
ptr)
887 rb_link_node(&node->
rb_node, parent, p);
893 node->
work.type = BINDER_WORK_NODE;
894 INIT_LIST_HEAD(&node->
work.entry);
897 "binder: %d:%d node %d u%p c%p created\n",
903 static int binder_inc_node(
struct binder_node *node,
int strong,
int internal,
908 if (target_list ==
NULL &&
910 !(node == binder_context_mgr_node &&
912 pr_err(
"binder: invalid inc strong "
920 list_del_init(&node->
work.entry);
927 if (target_list ==
NULL) {
928 pr_err(
"binder: invalid inc weak node "
938 static int binder_dec_node(
struct binder_node *node,
int strong,
int internal)
954 if (list_empty(&node->
work.entry)) {
961 list_del_init(&node->
work.entry);
965 "binder: refless node %d deleted\n",
970 "binder: dead node %d deleted\n",
993 else if (desc > ref->
desc)
1015 else if (node > ref->
node)
1021 if (new_ref ==
NULL)
1024 new_ref->
debug_id = ++binder_last_id;
1030 new_ref->
desc = (node == binder_context_mgr_node) ? 0 : 1;
1045 else if (new_ref->
desc > ref->
desc)
1053 hlist_add_head(&new_ref->
node_entry, &node->refs);
1056 "binder: %d new ref %d desc %d for "
1058 new_ref->
desc, node->debug_id);
1061 "binder: %d new ref %d desc %d for "
1068 static void binder_delete_ref(
struct binder_ref *ref)
1071 "binder: %d delete ref %d desc %d for "
1078 binder_dec_node(ref->
node, 1, 1);
1080 binder_dec_node(ref->
node, 0, 1);
1083 "binder: %d delete ref %d desc %d "
1084 "has death notification\n", ref->
proc->pid,
1094 static int binder_inc_ref(
struct binder_ref *ref,
int strong,
1100 ret = binder_inc_node(ref->
node, 1, 1, target_list);
1106 if (ref->
weak == 0) {
1107 ret = binder_inc_node(ref->
node, 0, 1, target_list);
1117 static int binder_dec_ref(
struct binder_ref *ref,
int strong)
1122 "ref %d desc %d s %d w %d\n",
1130 ret = binder_dec_node(ref->
node, strong, 1);
1135 if (ref->
weak == 0) {
1137 "ref %d desc %d s %d w %d\n",
1145 binder_delete_ref(ref);
1149 static void binder_pop_transaction(
struct binder_thread *target_thread,
1152 if (target_thread) {
1172 target_thread = t->
from;
1173 if (target_thread) {
1182 "binder: send failed reply for "
1183 "transaction %d to %d:%d\n",
1185 target_thread->
pid);
1187 binder_pop_transaction(target_thread, t);
1191 pr_err(
"binder: reply failed, target "
1192 "thread, %d:%d, has error code %d "
1193 "already\n", target_thread->
proc->pid,
1202 "binder: send failed reply "
1203 "for transaction %d, target dead\n",
1206 binder_pop_transaction(target_thread, t);
1209 "binder: reply failed,"
1210 " no target thread at root\n");
1215 "binder: reply failed, no target "
1216 "thread -- retry %d\n", t->
debug_id);
1221 static void binder_transaction_buffer_release(
struct binder_proc *proc,
1225 size_t *offp, *off_end;
1229 "binder: %d buffer release %d, size %zd-%zd, failed at %p\n",
1238 off_end = failed_at;
1241 for (; offp < off_end; offp++) {
1243 if (*offp > buffer->
data_size -
sizeof(*fp) ||
1246 pr_err(
"binder: transaction release %d bad"
1247 "offset %zd, size %zd\n", debug_id,
1257 pr_err(
"binder: transaction release %d"
1258 " bad node %p\n", debug_id, fp->
binder);
1270 pr_err(
"binder: transaction release %d"
1271 " bad handle %ld\n", debug_id,
1276 " ref %d desc %d (node %d)\n",
1283 " fd %ld\n", fp->
handle);
1285 task_close_fd(proc, fp->
handle);
1289 pr_err(
"binder: transaction release %d bad "
1290 "object type %lx\n", debug_id, fp->
type);
1302 size_t *offp, *off_end;
1322 if (in_reply_to ==
NULL) {
1324 "with no transaction stack\n",
1327 goto err_empty_call_stack;
1332 "with bad transaction stack,"
1333 " transaction %d has target %d:%d\n",
1336 in_reply_to->
to_proc->pid : 0,
1341 goto err_bad_call_stack;
1344 target_thread = in_reply_to->
from;
1345 if (target_thread ==
NULL) {
1347 goto err_dead_binder;
1351 "with bad target transaction stack %d, "
1359 target_thread =
NULL;
1360 goto err_dead_binder;
1362 target_proc = target_thread->
proc;
1369 "transaction to invalid handle\n",
1372 goto err_invalid_target_handle;
1374 target_node = ref->
node;
1376 target_node = binder_context_mgr_node;
1377 if (target_node ==
NULL) {
1379 goto err_no_context_mgr_node;
1383 target_proc = target_node->
proc;
1384 if (target_proc ==
NULL) {
1386 goto err_dead_binder;
1393 "transaction with bad transaction stack"
1394 ", transaction %d has target %d:%d\n",
1400 goto err_bad_call_stack;
1403 if (tmp->
from && tmp->
from->proc == target_proc)
1404 target_thread = tmp->
from;
1409 if (target_thread) {
1411 target_list = &target_thread->
todo;
1412 target_wait = &target_thread->
wait;
1414 target_list = &target_proc->
todo;
1415 target_wait = &target_proc->
wait;
1423 goto err_alloc_t_failed;
1427 tcomplete = kzalloc(
sizeof(*tcomplete),
GFP_KERNEL);
1428 if (tcomplete ==
NULL) {
1430 goto err_alloc_tcomplete_failed;
1439 "binder: %d:%d BC_REPLY %d -> %d:%d, "
1440 "data %p-%p size %zd-%zd\n",
1442 target_proc->
pid, target_thread->
pid,
1447 "binder: %d:%d BC_TRANSACTION %d -> "
1448 "%d - node %d, data %p-%p size %zd-%zd\n",
1468 goto err_binder_alloc_buf_failed;
1470 t->
buffer->allow_user_free = 0;
1473 t->
buffer->target_node = target_node;
1475 binder_inc_node(target_node, 1, 0,
NULL);
1481 "data ptr\n", proc->
pid, thread->
pid);
1483 goto err_copy_data_failed;
1487 "offsets ptr\n", proc->
pid, thread->
pid);
1489 goto err_copy_data_failed;
1493 "invalid offsets size, %zd\n",
1496 goto err_bad_offset;
1499 for (; offp < off_end; offp++) {
1501 if (*offp > t->
buffer->data_size -
sizeof(*fp) ||
1502 t->
buffer->data_size <
sizeof(*fp) ||
1505 "invalid offset, %zd\n",
1506 proc->
pid, thread->
pid, *offp);
1508 goto err_bad_offset;
1520 goto err_binder_new_node_failed;
1527 "node %d, cookie mismatch %p != %p\n",
1531 goto err_binder_get_ref_for_node_failed;
1533 ref = binder_get_ref_for_node(target_proc, node);
1536 goto err_binder_get_ref_for_node_failed;
1547 " node %d u%p -> ref %d desc %d\n",
1556 "transaction with invalid "
1557 "handle, %ld\n", proc->
pid,
1560 goto err_binder_get_ref_failed;
1562 if (ref->
node->proc == target_proc) {
1571 " ref %d desc %d -> node %d u%p\n",
1576 new_ref = binder_get_ref_for_node(target_proc, ref->
node);
1577 if (new_ref ==
NULL) {
1579 goto err_binder_get_ref_for_node_failed;
1584 " ref %d desc %d -> ref %d desc %d (node %d)\n",
1586 new_ref->
desc, ref->
node->debug_id);
1596 binder_user_error(
"binder: %d:%d got reply with fd, %ld, but target does not allow fds\n",
1599 goto err_fd_not_allowed;
1602 binder_user_error(
"binder: %d:%d got transaction with fd, %ld, but target does not allow fds\n",
1605 goto err_fd_not_allowed;
1613 goto err_fget_failed;
1615 target_fd = task_get_unused_fd_flags(target_proc,
O_CLOEXEC);
1616 if (target_fd < 0) {
1619 goto err_get_unused_fd_failed;
1621 task_fd_install(target_proc, target_fd, file);
1623 " fd %ld -> %d\n", fp->
handle, target_fd);
1630 "n with invalid object type, %lx\n",
1633 goto err_bad_object_type;
1638 binder_pop_transaction(target_thread, in_reply_to);
1653 t->
work.type = BINDER_WORK_TRANSACTION;
1655 tcomplete->
type = BINDER_WORK_TRANSACTION_COMPLETE;
1661 err_get_unused_fd_failed:
1664 err_binder_get_ref_for_node_failed:
1665 err_binder_get_ref_failed:
1666 err_binder_new_node_failed:
1667 err_bad_object_type:
1669 err_copy_data_failed:
1670 binder_transaction_buffer_release(target_proc, t->
buffer, offp);
1672 binder_free_buf(target_proc, t->
buffer);
1673 err_binder_alloc_buf_failed:
1676 err_alloc_tcomplete_failed:
1681 err_empty_call_stack:
1683 err_invalid_target_handle:
1684 err_no_context_mgr_node:
1686 "binder: %d:%d transaction failed %d, size %zd-%zd\n",
1687 proc->
pid, thread->
pid, return_error,
1692 fe = binder_transaction_log_add(&binder_transaction_log_failed);
1699 binder_send_failed_reply(in_reply_to, return_error);
1705 void __user *buffer,
int size,
signed long *consumed)
1708 void __user *ptr = buffer + *consumed;
1711 while (ptr < end && thread->return_error ==
BR_OK) {
1727 const char *debug_string;
1732 if (target == 0 && binder_context_mgr_node &&
1734 ref = binder_get_ref_for_node(proc,
1735 binder_context_mgr_node);
1736 if (ref->
desc != target) {
1738 "%d tried to acquire "
1739 "reference to desc 0, "
1745 ref = binder_get_ref(proc, target);
1748 "nt change on invalid ref %d\n",
1749 proc->
pid, thread->
pid, target);
1754 debug_string =
"IncRefs";
1755 binder_inc_ref(ref, 0,
NULL);
1758 debug_string =
"Acquire";
1759 binder_inc_ref(ref, 1,
NULL);
1762 debug_string =
"Release";
1763 binder_dec_ref(ref, 1);
1767 debug_string =
"DecRefs";
1768 binder_dec_ref(ref, 0);
1772 "binder: %d:%d %s ref %d desc %d s %d w %d for node %d\n",
1785 ptr +=
sizeof(
void *);
1788 ptr +=
sizeof(
void *);
1789 node = binder_get_node(proc, node_ptr);
1792 "%s u%p no match\n",
1800 if (cookie != node->
cookie) {
1802 " cookie mismatch %p != %p\n",
1805 "BC_INCREFS_DONE" :
"BC_ACQUIRE_DONE",
1813 "BC_ACQUIRE_DONE node %d has "
1814 "no pending acquire request\n",
1823 "BC_INCREFS_DONE node %d has "
1824 "no pending increfs request\n",
1833 "binder: %d:%d %s node %d ls %d lw %d\n",
1840 pr_err(
"binder: BC_ATTEMPT_ACQUIRE not supported\n");
1843 pr_err(
"binder: BC_ACQUIRE_RESULT not supported\n");
1852 ptr +=
sizeof(
void *);
1854 buffer = binder_buffer_lookup(proc, data_ptr);
1855 if (buffer ==
NULL) {
1857 "BC_FREE_BUFFER u%p no match\n",
1858 proc->
pid, thread->
pid, data_ptr);
1863 "BC_FREE_BUFFER u%p matched "
1864 "unreturned buffer\n",
1865 proc->
pid, thread->
pid, data_ptr);
1869 "binder: %d:%d BC_FREE_BUFFER u%p found buffer %d for %s transaction\n",
1884 binder_transaction_buffer_release(proc, buffer,
NULL);
1885 binder_free_buf(proc, buffer);
1902 "binder: %d:%d BC_REGISTER_LOOPER\n",
1907 " BC_REGISTER_LOOPER called "
1908 "after BC_ENTER_LOOPER\n",
1913 " BC_REGISTER_LOOPER called "
1914 "without request\n",
1924 "binder: %d:%d BC_ENTER_LOOPER\n",
1929 " BC_ENTER_LOOPER called after "
1930 "BC_REGISTER_LOOPER\n",
1937 "binder: %d:%d BC_EXIT_LOOPER\n",
1954 ptr +=
sizeof(
void *);
1955 ref = binder_get_ref(proc, target);
1961 "BC_REQUEST_DEATH_NOTIFICATION" :
1962 "BC_CLEAR_DEATH_NOTIFICATION",
1968 "binder: %d:%d %s %p ref %d desc %d s %d w %d for node %d\n",
1971 "BC_REQUEST_DEATH_NOTIFICATION" :
1972 "BC_CLEAR_DEATH_NOTIFICATION",
1979 "d BC_REQUEST_DEATH_NOTI"
1980 "FICATION death notific"
1981 "ation already set\n",
1986 if (death ==
NULL) {
1990 "BC_REQUEST_DEATH_NOTIFICATION failed\n",
1995 INIT_LIST_HEAD(&death->
work.entry);
1999 ref->
death->work.type = BINDER_WORK_DEAD_BINDER;
2010 "d BC_CLEAR_DEATH_NOTIFI"
2011 "CATION death notificat"
2017 if (death->
cookie != cookie) {
2019 "d BC_CLEAR_DEATH_NOTIFI"
2020 "CATION death notificat"
2021 "ion cookie mismatch "
2028 if (list_empty(&death->
work.entry)) {
2029 death->
work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2037 BUG_ON(death->
work.type != BINDER_WORK_DEAD_BINDER);
2038 death->
work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
2049 ptr +=
sizeof(
void *);
2052 if (tmp_death->
cookie == cookie) {
2058 "binder: %d:%d BC_DEAD_BINDER_DONE %p found %p\n",
2059 proc->
pid, thread->
pid, cookie, death);
2060 if (death ==
NULL) {
2062 "_BINDER_DONE %p not found\n",
2063 proc->
pid, thread->
pid, cookie);
2067 list_del_init(&death->
work.entry);
2068 if (death->
work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
2069 death->
work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2080 pr_err(
"binder: %d:%d unknown command %d\n",
2081 proc->
pid, thread->
pid, cmd);
2084 *consumed = ptr -
buffer;
2099 static int binder_has_proc_work(
struct binder_proc *proc,
2102 return !list_empty(&proc->
todo) ||
2106 static int binder_has_thread_work(
struct binder_thread *thread)
2112 static int binder_thread_read(
struct binder_proc *proc,
2114 void __user *buffer,
int size,
2115 signed long *consumed,
int non_block)
2117 void __user *ptr = buffer + *consumed;
2121 int wait_for_proc_work;
2123 if (*consumed == 0) {
2131 list_empty(&thread->
todo);
2151 if (wait_for_proc_work)
2154 if (wait_for_proc_work) {
2158 "for process work before calling BC_REGISTER_"
2159 "LOOPER or BC_ENTER_LOOPER (state %x)\n",
2162 binder_stop_on_user_error < 2);
2166 if (!binder_has_proc_work(proc, thread))
2172 if (!binder_has_thread_work(thread))
2178 if (wait_for_proc_work)
2191 if (!list_empty(&thread->
todo))
2193 else if (!list_empty(&proc->
todo) && wait_for_proc_work)
2201 if (end - ptr <
sizeof(tr) + 4)
2205 case BINDER_WORK_TRANSACTION: {
2208 case BINDER_WORK_TRANSACTION_COMPLETE: {
2216 "binder: %d:%d BR_TRANSACTION_COMPLETE\n",
2223 case BINDER_WORK_NODE: {
2226 const char *cmd_name;
2231 cmd_name =
"BR_INCREFS";
2237 cmd_name =
"BR_ACQUIRE";
2243 cmd_name =
"BR_RELEASE";
2247 cmd_name =
"BR_DECREFS";
2256 ptr +=
sizeof(
void *);
2259 ptr +=
sizeof(
void *);
2263 "binder: %d:%d %s %d u%p c%p\n",
2266 list_del_init(&w->
entry);
2267 if (!weak && !strong) {
2269 "binder: %d:%d node %d u%p c%p deleted\n",
2277 "binder: %d:%d node %d u%p c%p state unchanged\n",
2283 case BINDER_WORK_DEAD_BINDER:
2284 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2285 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2290 if (w->
type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
2299 ptr +=
sizeof(
void *);
2301 "binder: %d:%d %s %p\n",
2305 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
2308 if (w->
type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
2323 if (t->
buffer->target_node) {
2369 "binder: %d:%d %s %d %d:%d, cmd %d"
2370 "size %zd-%zd ptr %p-%p\n",
2380 t->
buffer->allow_user_free = 1;
2395 *consumed = ptr -
buffer;
2403 "binder: %d:%d BR_SPAWN_LOOPER\n",
2414 while (!list_empty(list)) {
2416 list_del_init(&w->
entry);
2422 if (t->
buffer->target_node &&
2427 "binder: undelivered transaction %d\n",
2434 case BINDER_WORK_TRANSACTION_COMPLETE: {
2436 "binder: undelivered TRANSACTION_COMPLETE\n");
2440 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2441 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2446 "binder: undelivered death notification, %p\n",
2452 pr_err(
"binder: unexpected work type, %d, not freed\n",
2478 thread = kzalloc(
sizeof(*thread),
GFP_KERNEL);
2482 thread->
proc = proc;
2485 INIT_LIST_HEAD(&thread->
todo);
2486 rb_link_node(&thread->
rb_node, parent, p);
2495 static int binder_free_thread(
struct binder_proc *proc,
2500 int active_transactions = 0;
2507 active_transactions++;
2509 "binder: release %d:%d transaction %d "
2510 "%s, still active\n", proc->
pid, thread->
pid,
2512 (t->
to_thread == thread) ?
"in" :
"out");
2522 }
else if (t->
from == thread) {
2530 binder_release_work(&thread->
todo);
2533 return active_transactions;
2536 static unsigned int binder_poll(
struct file *filp,
2541 int wait_for_proc_work;
2544 thread = binder_get_thread(proc);
2550 if (wait_for_proc_work) {
2551 if (binder_has_proc_work(proc, thread))
2553 poll_wait(filp, &proc->
wait, wait);
2554 if (binder_has_proc_work(proc, thread))
2557 if (binder_has_thread_work(thread))
2559 poll_wait(filp, &thread->
wait, wait);
2560 if (binder_has_thread_work(thread))
2566 static long binder_ioctl(
struct file *filp,
unsigned int cmd,
unsigned long arg)
2581 thread = binder_get_thread(proc);
2582 if (thread ==
NULL) {
2599 "binder: %d:%d write %ld at %08lx, read %ld at %08lx\n",
2600 proc->
pid, thread->
pid, bwr.write_size, bwr.write_buffer,
2601 bwr.read_size, bwr.read_buffer);
2603 if (bwr.write_size > 0) {
2604 ret =
binder_thread_write(proc, thread, (
void __user *)bwr.write_buffer, bwr.write_size, &bwr.write_consumed);
2606 bwr.read_consumed = 0;
2612 if (bwr.read_size > 0) {
2613 ret = binder_thread_read(proc, thread, (
void __user *)bwr.read_buffer, bwr.read_size, &bwr.read_consumed, filp->
f_flags &
O_NONBLOCK);
2614 if (!list_empty(&proc->
todo))
2623 "binder: %d:%d wrote %ld of %ld, read return %ld of %ld\n",
2624 proc->
pid, thread->
pid, bwr.write_consumed, bwr.write_size,
2625 bwr.read_consumed, bwr.read_size);
2639 if (binder_context_mgr_node !=
NULL) {
2640 pr_err(
"binder: BINDER_SET_CONTEXT_MGR already set\n");
2644 if (uid_valid(binder_context_mgr_uid)) {
2645 if (!uid_eq(binder_context_mgr_uid,
current->cred->euid)) {
2646 pr_err(
"binder: BINDER_SET_"
2647 "CONTEXT_MGR bad uid %d != %d\n",
2654 binder_context_mgr_uid =
current->cred->euid;
2655 binder_context_mgr_node = binder_new_node(proc,
NULL,
NULL);
2656 if (binder_context_mgr_node ==
NULL) {
2668 binder_free_thread(proc, thread);
2692 pr_info(
"binder: %d:%d ioctl %x %lx returned %d\n", proc->
pid,
current->pid, cmd, arg, ret);
2700 "binder: %d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
2710 "binder: %d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
2719 static struct vm_operations_struct binder_vm_ops = {
2720 .open = binder_vma_open,
2721 .close = binder_vma_close,
2724 static int binder_mmap(
struct file *filp,
struct vm_area_struct *vma)
2729 const char *failure_string;
2739 "binder_mmap: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
2746 failure_string =
"bad vm_flags";
2754 failure_string =
"already mapped";
2755 goto err_already_mapped;
2761 failure_string =
"get_vm_area";
2762 goto err_get_vm_area_failed;
2768 #ifdef CONFIG_CPU_CACHE_VIPT
2779 failure_string =
"alloc page array";
2780 goto err_alloc_pages_failed;
2784 vma->
vm_ops = &binder_vm_ops;
2787 if (binder_update_page_range(proc, 1, proc->
buffer, proc->
buffer + PAGE_SIZE, vma)) {
2789 failure_string =
"alloc small buf";
2790 goto err_alloc_small_buf_failed;
2793 INIT_LIST_HEAD(&proc->
buffers);
2796 binder_insert_free_buffer(proc, buffer);
2807 err_alloc_small_buf_failed:
2810 err_alloc_pages_failed:
2814 err_get_vm_area_failed:
2818 pr_err(
"binder_mmap: %d %lx-%lx %s failed %d\n",
2823 static int binder_open(
struct inode *nodp,
struct file *filp)
2835 INIT_LIST_HEAD(&proc->
todo);
2840 hlist_add_head(&proc->
proc_node, &binder_procs);
2846 if (binder_debugfs_dir_entry_proc) {
2848 snprintf(strbuf,
sizeof(strbuf),
"%u", proc->
pid);
2850 binder_debugfs_dir_entry_proc, proc, &binder_proc_fops);
2856 static int binder_flush(
struct file *filp,
fl_owner_t id)
2865 static void binder_deferred_flush(
struct binder_proc *proc)
2880 "binder_flush: %d woke %d threads\n", proc->
pid,
2884 static int binder_release(
struct inode *nodp,
struct file *filp)
2893 static void binder_deferred_release(
struct binder_proc *proc)
2898 int threads, nodes, incoming_refs, outgoing_refs, buffers, active_transactions, page_count;
2904 if (binder_context_mgr_node && binder_context_mgr_node->
proc == proc) {
2906 "binder_release: %d context_mgr_node gone\n",
2908 binder_context_mgr_node =
NULL;
2912 active_transactions = 0;
2916 active_transactions += binder_free_thread(proc, thread);
2925 list_del_init(&node->
work.entry);
2927 if (hlist_empty(&node->
refs)) {
2937 hlist_add_head(&node->
dead_node, &binder_dead_nodes);
2943 if (list_empty(&ref->
death->work.entry)) {
2944 ref->
death->work.type = BINDER_WORK_DEAD_BINDER;
2952 "binder: node %d now dead, "
2953 "refs %d, death %d\n", node->
debug_id,
2954 incoming_refs, death);
2962 binder_delete_ref(ref);
2964 binder_release_work(&proc->
todo);
2975 pr_err(
"binder: release proc %d, "
2976 "transaction %d, not freed\n",
2980 binder_free_buf(proc, buffer);
2990 if (proc->
pages[i]) {
2993 "binder_release: %d: "
2994 "page %d at %p not freed\n",
3007 put_task_struct(proc->
tsk);
3010 "binder_release: %d threads %d, nodes %d (ref %d), "
3011 "refs %d, active transactions %d, buffers %d, "
3013 proc->
pid, threads, nodes, incoming_refs, outgoing_refs,
3014 active_transactions, buffers, page_count);
3028 if (!hlist_empty(&binder_deferred_list)) {
3042 files = proc->
files;
3048 binder_deferred_flush(proc);
3051 binder_deferred_release(proc);
3058 static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
3067 &binder_deferred_list);
3068 queue_work(binder_deferred_workqueue, &binder_deferred_work);
3073 static void print_binder_transaction(
struct seq_file *
m,
const char *
prefix,
3077 "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %ld r%d",
3088 if (t->
buffer->target_node)
3090 t->
buffer->target_node->debug_id);
3096 static void print_binder_buffer(
struct seq_file *m,
const char *prefix,
3105 static void print_binder_work(
struct seq_file *m,
const char *prefix,
3106 const char *transaction_prefix,
3113 case BINDER_WORK_TRANSACTION:
3115 print_binder_transaction(m, transaction_prefix, t);
3117 case BINDER_WORK_TRANSACTION_COMPLETE:
3118 seq_printf(m,
"%stransaction complete\n", prefix);
3120 case BINDER_WORK_NODE:
3125 case BINDER_WORK_DEAD_BINDER:
3126 seq_printf(m,
"%shas dead binder\n", prefix);
3128 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
3129 seq_printf(m,
"%shas cleared dead binder\n", prefix);
3131 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
3132 seq_printf(m,
"%shas cleared death notification\n", prefix);
3140 static void print_binder_thread(
struct seq_file *m,
3146 size_t start_pos = m->
count;
3150 header_pos = m->
count;
3153 if (t->
from == thread) {
3154 print_binder_transaction(m,
3155 " outgoing transaction", t);
3158 print_binder_transaction(m,
3159 " incoming transaction", t);
3162 print_binder_transaction(m,
" bad transaction", t);
3167 print_binder_work(m,
" ",
" pending transaction", w);
3169 if (!print_always && m->
count == header_pos)
3170 m->
count = start_pos;
3185 node->debug_id, node->ptr, node->cookie,
3186 node->has_strong_ref, node->has_weak_ref,
3187 node->local_strong_refs, node->local_weak_refs,
3188 node->internal_strong_refs, count);
3196 print_binder_work(m, " ",
3202 seq_printf(m,
" ref %d: desc %d %snode %d s %d w %d d %p\n",
3203 ref->debug_id, ref->desc, ref->node->proc ?
"" :
"dead ",
3204 ref->node->debug_id, ref->strong, ref->weak, ref->death);
3207 static void print_binder_proc(
struct seq_file *m,
3212 size_t start_pos = m->
count;
3216 header_pos = m->
count;
3225 print_binder_node(m, node);
3235 print_binder_buffer(m,
" buffer",
3238 print_binder_work(m, " ", " pending
transaction", w);
3240 seq_puts(m,
" has delivered dead binder\n");
3243 if (!print_all && m->
count == header_pos)
3244 m->
count = start_pos;
3247 static const char *binder_return_strings[] = {
3252 "BR_ACQUIRE_RESULT",
3254 "BR_TRANSACTION_COMPLETE",
3259 "BR_ATTEMPT_ACQUIRE",
3264 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
3268 static const char *binder_command_strings[] = {
3271 "BC_ACQUIRE_RESULT",
3279 "BC_ATTEMPT_ACQUIRE",
3280 "BC_REGISTER_LOOPER",
3283 "BC_REQUEST_DEATH_NOTIFICATION",
3284 "BC_CLEAR_DEATH_NOTIFICATION",
3285 "BC_DEAD_BINDER_DONE"
3288 static const char *binder_objstat_strings[] = {
3295 "transaction_complete"
3298 static void print_binder_stats(
struct seq_file *m,
const char *prefix,
3308 binder_command_strings[i], stats->
bc[i]);
3316 binder_return_strings[i], stats->
br[i]);
3325 seq_printf(m,
"%s%s: active %d total %d\n", prefix,
3326 binder_objstat_strings[i],
3332 static void print_binder_proc_stats(
struct seq_file *m,
3337 int count, strong, weak;
3344 seq_printf(m,
" requested threads: %d+%d/%d\n"
3345 " ready threads %d\n"
3363 seq_printf(m,
" refs: %d s %d w %d\n", count, strong, weak);
3373 case BINDER_WORK_TRANSACTION:
3380 seq_printf(m,
" pending transactions: %d\n", count);
3382 print_binder_stats(m,
" ", &proc->
stats);
3391 int do_lock = !binder_debug_no_lock;
3398 if (!hlist_empty(&binder_dead_nodes))
3401 print_binder_node(m, node);
3404 print_binder_proc(m, proc, 1);
3414 int do_lock = !binder_debug_no_lock;
3424 print_binder_proc_stats(m, proc);
3430 static
int binder_transactions_show(
struct seq_file *m,
void *unused)
3434 int do_lock = !binder_debug_no_lock;
3439 seq_puts(m,
"binder transactions:\n");
3441 print_binder_proc(m, proc, 0);
3450 int do_lock = !binder_debug_no_lock;
3454 seq_puts(m,
"binder proc state:\n");
3455 print_binder_proc(m, proc, 1);
3461 static void print_binder_transaction_log_entry(
struct seq_file *m,
3465 "%d: %s from %d:%d to %d:%d node %d handle %d size %d:%d\n",
3472 static int binder_transaction_log_show(
struct seq_file *m,
void *unused)
3479 print_binder_transaction_log_entry(m, &log->
entry[i]);
3481 for (i = 0; i < log->
next; i++)
3482 print_binder_transaction_log_entry(m, &log->
entry[i]);
3488 .poll = binder_poll,
3489 .unlocked_ioctl = binder_ioctl,
3490 .mmap = binder_mmap,
3491 .open = binder_open,
3492 .flush = binder_flush,
3493 .release = binder_release,
3499 .fops = &binder_fops
3507 static int __init binder_init(
void)
3512 if (!binder_deferred_workqueue)
3516 if (binder_debugfs_dir_entry_root)
3518 binder_debugfs_dir_entry_root);
3520 if (binder_debugfs_dir_entry_root) {
3523 binder_debugfs_dir_entry_root,
3525 &binder_state_fops);
3528 binder_debugfs_dir_entry_root,
3530 &binder_stats_fops);
3533 binder_debugfs_dir_entry_root,
3535 &binder_transactions_fops);
3538 binder_debugfs_dir_entry_root,
3540 &binder_transaction_log_fops);
3543 binder_debugfs_dir_entry_root,
3544 &binder_transaction_log_failed,
3545 &binder_transaction_log_fops);