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.
21 * There are 3 main spinlocks which must be acquired in the
24 * 1) proc->outer_lock : protects binder_ref
25 * binder_proc_lock() and binder_proc_unlock() are
27 * 2) node->lock : protects most fields of binder_node.
28 * binder_node_lock() and binder_node_unlock() are
30 * 3) proc->inner_lock : protects the thread and node lists
31 * (proc->threads, proc->waiting_threads, proc->nodes)
32 * and all todo lists associated with the binder_proc
33 * (proc->todo, thread->todo, proc->delivered_death and
34 * node->async_todo), as well as thread->transaction_stack
35 * binder_inner_proc_lock() and binder_inner_proc_unlock()
38 * Any lock under procA must never be nested under any lock at the same
39 * level or below on procB.
41 * Functions that require a lock held on entry indicate which lock
42 * in the suffix of the function name:
44 * foo_olocked() : requires node->outer_lock
45 * foo_nlocked() : requires node->lock
46 * foo_ilocked() : requires proc->inner_lock
47 * foo_oilocked(): requires proc->outer_lock and proc->inner_lock
48 * foo_nilocked(): requires node->lock and proc->inner_lock
52 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
54 #include <asm/cacheflush.h>
55 #include <linux/fdtable.h>
56 #include <linux/file.h>
57 #include <linux/freezer.h>
59 #include <linux/list.h>
60 #include <linux/miscdevice.h>
61 #include <linux/module.h>
62 #include <linux/mutex.h>
63 #include <linux/nsproxy.h>
64 #include <linux/poll.h>
65 #include <linux/debugfs.h>
66 #include <linux/rbtree.h>
67 #include <linux/sched/signal.h>
68 #include <linux/sched/mm.h>
69 #include <linux/seq_file.h>
70 #include <linux/uaccess.h>
71 #include <linux/pid_namespace.h>
72 #include <linux/security.h>
73 #include <linux/spinlock.h>
75 #include <uapi/linux/android/binder.h>
76 #include <uapi/linux/sched/types.h>
77 #include "binder_alloc.h"
78 #include "binder_trace.h"
80 static HLIST_HEAD(binder_deferred_list
);
81 static DEFINE_MUTEX(binder_deferred_lock
);
83 static HLIST_HEAD(binder_devices
);
84 static HLIST_HEAD(binder_procs
);
85 static DEFINE_MUTEX(binder_procs_lock
);
87 static HLIST_HEAD(binder_dead_nodes
);
88 static DEFINE_SPINLOCK(binder_dead_nodes_lock
);
90 static struct dentry
*binder_debugfs_dir_entry_root
;
91 static struct dentry
*binder_debugfs_dir_entry_proc
;
92 static atomic_t binder_last_id
;
94 #define BINDER_DEBUG_ENTRY(name) \
95 static int binder_##name##_open(struct inode *inode, struct file *file) \
97 return single_open(file, binder_##name##_show, inode->i_private); \
100 static const struct file_operations binder_##name##_fops = { \
101 .owner = THIS_MODULE, \
102 .open = binder_##name##_open, \
104 .llseek = seq_lseek, \
105 .release = single_release, \
108 static int binder_proc_show(struct seq_file
*m
, void *unused
);
109 BINDER_DEBUG_ENTRY(proc
);
111 /* This is only defined in include/asm-arm/sizes.h */
117 #define SZ_4M 0x400000
120 #define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
123 BINDER_DEBUG_USER_ERROR
= 1U << 0,
124 BINDER_DEBUG_FAILED_TRANSACTION
= 1U << 1,
125 BINDER_DEBUG_DEAD_TRANSACTION
= 1U << 2,
126 BINDER_DEBUG_OPEN_CLOSE
= 1U << 3,
127 BINDER_DEBUG_DEAD_BINDER
= 1U << 4,
128 BINDER_DEBUG_DEATH_NOTIFICATION
= 1U << 5,
129 BINDER_DEBUG_READ_WRITE
= 1U << 6,
130 BINDER_DEBUG_USER_REFS
= 1U << 7,
131 BINDER_DEBUG_THREADS
= 1U << 8,
132 BINDER_DEBUG_TRANSACTION
= 1U << 9,
133 BINDER_DEBUG_TRANSACTION_COMPLETE
= 1U << 10,
134 BINDER_DEBUG_FREE_BUFFER
= 1U << 11,
135 BINDER_DEBUG_INTERNAL_REFS
= 1U << 12,
136 BINDER_DEBUG_PRIORITY_CAP
= 1U << 13,
137 BINDER_DEBUG_SPINLOCKS
= 1U << 14,
139 static uint32_t binder_debug_mask
= BINDER_DEBUG_USER_ERROR
|
140 BINDER_DEBUG_FAILED_TRANSACTION
| BINDER_DEBUG_DEAD_TRANSACTION
;
141 module_param_named(debug_mask
, binder_debug_mask
, uint
, 0644);
143 static char *binder_devices_param
= CONFIG_ANDROID_BINDER_DEVICES
;
144 module_param_named(devices
, binder_devices_param
, charp
, 0444);
146 static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait
);
147 static int binder_stop_on_user_error
;
149 static int binder_set_stop_on_user_error(const char *val
,
150 const struct kernel_param
*kp
)
154 ret
= param_set_int(val
, kp
);
155 if (binder_stop_on_user_error
< 2)
156 wake_up(&binder_user_error_wait
);
159 module_param_call(stop_on_user_error
, binder_set_stop_on_user_error
,
160 param_get_int
, &binder_stop_on_user_error
, 0644);
162 #define binder_debug(mask, x...) \
164 if (binder_debug_mask & mask) \
168 #define binder_user_error(x...) \
170 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
172 if (binder_stop_on_user_error) \
173 binder_stop_on_user_error = 2; \
176 #define to_flat_binder_object(hdr) \
177 container_of(hdr, struct flat_binder_object, hdr)
179 #define to_binder_fd_object(hdr) container_of(hdr, struct binder_fd_object, hdr)
181 #define to_binder_buffer_object(hdr) \
182 container_of(hdr, struct binder_buffer_object, hdr)
184 #define to_binder_fd_array_object(hdr) \
185 container_of(hdr, struct binder_fd_array_object, hdr)
187 enum binder_stat_types
{
193 BINDER_STAT_TRANSACTION
,
194 BINDER_STAT_TRANSACTION_COMPLETE
,
198 struct binder_stats
{
199 atomic_t br
[_IOC_NR(BR_FAILED_REPLY
) + 1];
200 atomic_t bc
[_IOC_NR(BC_REPLY_SG
) + 1];
201 atomic_t obj_created
[BINDER_STAT_COUNT
];
202 atomic_t obj_deleted
[BINDER_STAT_COUNT
];
205 static struct binder_stats binder_stats
;
207 static inline void binder_stats_deleted(enum binder_stat_types type
)
209 atomic_inc(&binder_stats
.obj_deleted
[type
]);
212 static inline void binder_stats_created(enum binder_stat_types type
)
214 atomic_inc(&binder_stats
.obj_created
[type
]);
217 struct binder_transaction_log_entry
{
229 int return_error_line
;
230 uint32_t return_error
;
231 uint32_t return_error_param
;
232 const char *context_name
;
234 struct binder_transaction_log
{
237 struct binder_transaction_log_entry entry
[32];
239 static struct binder_transaction_log binder_transaction_log
;
240 static struct binder_transaction_log binder_transaction_log_failed
;
242 static struct binder_transaction_log_entry
*binder_transaction_log_add(
243 struct binder_transaction_log
*log
)
245 struct binder_transaction_log_entry
*e
;
246 unsigned int cur
= atomic_inc_return(&log
->cur
);
248 if (cur
>= ARRAY_SIZE(log
->entry
))
250 e
= &log
->entry
[cur
% ARRAY_SIZE(log
->entry
)];
251 WRITE_ONCE(e
->debug_id_done
, 0);
253 * write-barrier to synchronize access to e->debug_id_done.
254 * We make sure the initialized 0 value is seen before
255 * memset() other fields are zeroed by memset.
258 memset(e
, 0, sizeof(*e
));
262 struct binder_context
{
263 struct binder_node
*binder_context_mgr_node
;
264 struct mutex context_mgr_node_lock
;
266 kuid_t binder_context_mgr_uid
;
270 struct binder_device
{
271 struct hlist_node hlist
;
272 struct miscdevice miscdev
;
273 struct binder_context context
;
277 * struct binder_work - work enqueued on a worklist
278 * @entry: node enqueued on list
279 * @type: type of work to be performed
281 * There are separate work lists for proc, thread, and node (async).
284 struct list_head entry
;
286 enum binder_work_type
{
287 BINDER_WORK_TRANSACTION
= 1,
288 BINDER_WORK_TRANSACTION_COMPLETE
,
289 BINDER_WORK_RETURN_ERROR
,
291 BINDER_WORK_DEAD_BINDER
,
292 BINDER_WORK_DEAD_BINDER_AND_CLEAR
,
293 BINDER_WORK_CLEAR_DEATH_NOTIFICATION
,
297 struct binder_error
{
298 struct binder_work work
;
303 * struct binder_node - binder node bookkeeping
304 * @debug_id: unique ID for debugging
305 * (invariant after initialized)
306 * @lock: lock for node fields
307 * @work: worklist element for node work
308 * (protected by @proc->inner_lock)
309 * @rb_node: element for proc->nodes tree
310 * (protected by @proc->inner_lock)
311 * @dead_node: element for binder_dead_nodes list
312 * (protected by binder_dead_nodes_lock)
313 * @proc: binder_proc that owns this node
314 * (invariant after initialized)
315 * @refs: list of references on this node
316 * (protected by @lock)
317 * @internal_strong_refs: used to take strong references when
318 * initiating a transaction
319 * (protected by @proc->inner_lock if @proc
321 * @local_weak_refs: weak user refs from local process
322 * (protected by @proc->inner_lock if @proc
324 * @local_strong_refs: strong user refs from local process
325 * (protected by @proc->inner_lock if @proc
327 * @tmp_refs: temporary kernel refs
328 * (protected by @proc->inner_lock while @proc
329 * is valid, and by binder_dead_nodes_lock
330 * if @proc is NULL. During inc/dec and node release
331 * it is also protected by @lock to provide safety
332 * as the node dies and @proc becomes NULL)
333 * @ptr: userspace pointer for node
334 * (invariant, no lock needed)
335 * @cookie: userspace cookie for node
336 * (invariant, no lock needed)
337 * @has_strong_ref: userspace notified of strong ref
338 * (protected by @proc->inner_lock if @proc
340 * @pending_strong_ref: userspace has acked notification of strong ref
341 * (protected by @proc->inner_lock if @proc
343 * @has_weak_ref: userspace notified of weak ref
344 * (protected by @proc->inner_lock if @proc
346 * @pending_weak_ref: userspace has acked notification of weak ref
347 * (protected by @proc->inner_lock if @proc
349 * @has_async_transaction: async transaction to node in progress
350 * (protected by @lock)
351 * @sched_policy: minimum scheduling policy for node
352 * (invariant after initialized)
353 * @accept_fds: file descriptor operations supported for node
354 * (invariant after initialized)
355 * @min_priority: minimum scheduling priority
356 * (invariant after initialized)
357 * @inherit_rt: inherit RT scheduling policy from caller
358 * @txn_security_ctx: require sender's security context
359 * (invariant after initialized)
360 * @async_todo: list of async work items
361 * (protected by @proc->inner_lock)
363 * Bookkeeping structure for binder nodes.
368 struct binder_work work
;
370 struct rb_node rb_node
;
371 struct hlist_node dead_node
;
373 struct binder_proc
*proc
;
374 struct hlist_head refs
;
375 int internal_strong_refs
;
377 int local_strong_refs
;
379 binder_uintptr_t ptr
;
380 binder_uintptr_t cookie
;
383 * bitfield elements protected by
387 u8 pending_strong_ref
:1;
389 u8 pending_weak_ref
:1;
393 * invariant after initialization
398 u8 txn_security_ctx
:1;
401 bool has_async_transaction
;
402 struct list_head async_todo
;
405 struct binder_ref_death
{
407 * @work: worklist element for death notifications
408 * (protected by inner_lock of the proc that
409 * this ref belongs to)
411 struct binder_work work
;
412 binder_uintptr_t cookie
;
416 * struct binder_ref_data - binder_ref counts and id
417 * @debug_id: unique ID for the ref
418 * @desc: unique userspace handle for ref
419 * @strong: strong ref count (debugging only if not locked)
420 * @weak: weak ref count (debugging only if not locked)
422 * Structure to hold ref count and ref id information. Since
423 * the actual ref can only be accessed with a lock, this structure
424 * is used to return information about the ref to callers of
425 * ref inc/dec functions.
427 struct binder_ref_data
{
435 * struct binder_ref - struct to track references on nodes
436 * @data: binder_ref_data containing id, handle, and current refcounts
437 * @rb_node_desc: node for lookup by @data.desc in proc's rb_tree
438 * @rb_node_node: node for lookup by @node in proc's rb_tree
439 * @node_entry: list entry for node->refs list in target node
440 * (protected by @node->lock)
441 * @proc: binder_proc containing ref
442 * @node: binder_node of target node. When cleaning up a
443 * ref for deletion in binder_cleanup_ref, a non-NULL
444 * @node indicates the node must be freed
445 * @death: pointer to death notification (ref_death) if requested
446 * (protected by @node->lock)
448 * Structure to track references from procA to target node (on procB). This
449 * structure is unsafe to access without holding @proc->outer_lock.
452 /* Lookups needed: */
453 /* node + proc => ref (transaction) */
454 /* desc + proc => ref (transaction, inc/dec ref) */
455 /* node => refs + procs (proc exit) */
456 struct binder_ref_data data
;
457 struct rb_node rb_node_desc
;
458 struct rb_node rb_node_node
;
459 struct hlist_node node_entry
;
460 struct binder_proc
*proc
;
461 struct binder_node
*node
;
462 struct binder_ref_death
*death
;
465 enum binder_deferred_state
{
466 BINDER_DEFERRED_PUT_FILES
= 0x01,
467 BINDER_DEFERRED_FLUSH
= 0x02,
468 BINDER_DEFERRED_RELEASE
= 0x04,
472 * struct binder_priority - scheduler policy and priority
473 * @sched_policy scheduler policy
474 * @prio [100..139] for SCHED_NORMAL, [0..99] for FIFO/RT
476 * The binder driver supports inheriting the following scheduler policies:
482 struct binder_priority
{
483 unsigned int sched_policy
;
488 * struct binder_proc - binder process bookkeeping
489 * @proc_node: element for binder_procs list
490 * @threads: rbtree of binder_threads in this proc
491 * (protected by @inner_lock)
492 * @nodes: rbtree of binder nodes associated with
493 * this proc ordered by node->ptr
494 * (protected by @inner_lock)
495 * @refs_by_desc: rbtree of refs ordered by ref->desc
496 * (protected by @outer_lock)
497 * @refs_by_node: rbtree of refs ordered by ref->node
498 * (protected by @outer_lock)
499 * @waiting_threads: threads currently waiting for proc work
500 * (protected by @inner_lock)
501 * @pid PID of group_leader of process
502 * (invariant after initialized)
503 * @tsk task_struct for group_leader of process
504 * (invariant after initialized)
505 * @files files_struct for process
506 * (protected by @files_lock)
507 * @files_lock mutex to protect @files
508 * @deferred_work_node: element for binder_deferred_list
509 * (protected by binder_deferred_lock)
510 * @deferred_work: bitmap of deferred work to perform
511 * (protected by binder_deferred_lock)
512 * @is_dead: process is dead and awaiting free
513 * when outstanding transactions are cleaned up
514 * (protected by @inner_lock)
515 * @todo: list of work for this process
516 * (protected by @inner_lock)
517 * @stats: per-process binder statistics
518 * (atomics, no lock needed)
519 * @delivered_death: list of delivered death notification
520 * (protected by @inner_lock)
521 * @max_threads: cap on number of binder threads
522 * (protected by @inner_lock)
523 * @requested_threads: number of binder threads requested but not
524 * yet started. In current implementation, can
526 * (protected by @inner_lock)
527 * @requested_threads_started: number binder threads started
528 * (protected by @inner_lock)
529 * @tmp_ref: temporary reference to indicate proc is in use
530 * (protected by @inner_lock)
531 * @default_priority: default scheduler priority
532 * (invariant after initialized)
533 * @debugfs_entry: debugfs node
534 * @alloc: binder allocator bookkeeping
535 * @context: binder_context for this proc
536 * (invariant after initialized)
537 * @inner_lock: can nest under outer_lock and/or node lock
538 * @outer_lock: no nesting under innor or node lock
539 * Lock order: 1) outer, 2) node, 3) inner
541 * Bookkeeping structure for binder processes
544 struct hlist_node proc_node
;
545 struct rb_root threads
;
546 struct rb_root nodes
;
547 struct rb_root refs_by_desc
;
548 struct rb_root refs_by_node
;
549 struct list_head waiting_threads
;
551 struct task_struct
*tsk
;
552 struct files_struct
*files
;
553 struct mutex files_lock
;
554 struct hlist_node deferred_work_node
;
558 struct list_head todo
;
559 struct binder_stats stats
;
560 struct list_head delivered_death
;
562 int requested_threads
;
563 int requested_threads_started
;
565 struct binder_priority default_priority
;
566 struct dentry
*debugfs_entry
;
567 struct binder_alloc alloc
;
568 struct binder_context
*context
;
569 spinlock_t inner_lock
;
570 spinlock_t outer_lock
;
574 BINDER_LOOPER_STATE_REGISTERED
= 0x01,
575 BINDER_LOOPER_STATE_ENTERED
= 0x02,
576 BINDER_LOOPER_STATE_EXITED
= 0x04,
577 BINDER_LOOPER_STATE_INVALID
= 0x08,
578 BINDER_LOOPER_STATE_WAITING
= 0x10,
579 BINDER_LOOPER_STATE_POLL
= 0x20,
583 * struct binder_thread - binder thread bookkeeping
584 * @proc: binder process for this thread
585 * (invariant after initialization)
586 * @rb_node: element for proc->threads rbtree
587 * (protected by @proc->inner_lock)
588 * @waiting_thread_node: element for @proc->waiting_threads list
589 * (protected by @proc->inner_lock)
590 * @pid: PID for this thread
591 * (invariant after initialization)
592 * @looper: bitmap of looping state
593 * (only accessed by this thread)
594 * @looper_needs_return: looping thread needs to exit driver
596 * @transaction_stack: stack of in-progress transactions for this thread
597 * (protected by @proc->inner_lock)
598 * @todo: list of work to do for this thread
599 * (protected by @proc->inner_lock)
600 * @process_todo: whether work in @todo should be processed
601 * (protected by @proc->inner_lock)
602 * @return_error: transaction errors reported by this thread
603 * (only accessed by this thread)
604 * @reply_error: transaction errors reported by target thread
605 * (protected by @proc->inner_lock)
606 * @wait: wait queue for thread work
607 * @stats: per-thread statistics
608 * (atomics, no lock needed)
609 * @tmp_ref: temporary reference to indicate thread is in use
610 * (atomic since @proc->inner_lock cannot
611 * always be acquired)
612 * @is_dead: thread is dead and awaiting free
613 * when outstanding transactions are cleaned up
614 * (protected by @proc->inner_lock)
615 * @task: struct task_struct for this thread
617 * Bookkeeping structure for binder threads.
619 struct binder_thread
{
620 struct binder_proc
*proc
;
621 struct rb_node rb_node
;
622 struct list_head waiting_thread_node
;
624 int looper
; /* only modified by this thread */
625 bool looper_need_return
; /* can be written by other thread */
626 struct binder_transaction
*transaction_stack
;
627 struct list_head todo
;
629 struct binder_error return_error
;
630 struct binder_error reply_error
;
631 wait_queue_head_t wait
;
632 struct binder_stats stats
;
635 struct task_struct
*task
;
638 struct binder_transaction
{
640 struct binder_work work
;
641 struct binder_thread
*from
;
642 struct binder_transaction
*from_parent
;
643 struct binder_proc
*to_proc
;
644 struct binder_thread
*to_thread
;
645 struct binder_transaction
*to_parent
;
646 unsigned need_reply
:1;
647 /* unsigned is_dead:1; */ /* not used at the moment */
649 struct binder_buffer
*buffer
;
652 struct binder_priority priority
;
653 struct binder_priority saved_priority
;
654 bool set_priority_called
;
656 binder_uintptr_t security_ctx
;
658 * @lock: protects @from, @to_proc, and @to_thread
660 * @from, @to_proc, and @to_thread can be set to NULL
661 * during thread teardown
667 * binder_proc_lock() - Acquire outer lock for given binder_proc
668 * @proc: struct binder_proc to acquire
670 * Acquires proc->outer_lock. Used to protect binder_ref
671 * structures associated with the given proc.
673 #define binder_proc_lock(proc) _binder_proc_lock(proc, __LINE__)
675 _binder_proc_lock(struct binder_proc
*proc
, int line
)
677 binder_debug(BINDER_DEBUG_SPINLOCKS
,
678 "%s: line=%d\n", __func__
, line
);
679 spin_lock(&proc
->outer_lock
);
683 * binder_proc_unlock() - Release spinlock for given binder_proc
684 * @proc: struct binder_proc to acquire
686 * Release lock acquired via binder_proc_lock()
688 #define binder_proc_unlock(_proc) _binder_proc_unlock(_proc, __LINE__)
690 _binder_proc_unlock(struct binder_proc
*proc
, int line
)
692 binder_debug(BINDER_DEBUG_SPINLOCKS
,
693 "%s: line=%d\n", __func__
, line
);
694 spin_unlock(&proc
->outer_lock
);
698 * binder_inner_proc_lock() - Acquire inner lock for given binder_proc
699 * @proc: struct binder_proc to acquire
701 * Acquires proc->inner_lock. Used to protect todo lists
703 #define binder_inner_proc_lock(proc) _binder_inner_proc_lock(proc, __LINE__)
705 _binder_inner_proc_lock(struct binder_proc
*proc
, int line
)
707 binder_debug(BINDER_DEBUG_SPINLOCKS
,
708 "%s: line=%d\n", __func__
, line
);
709 spin_lock(&proc
->inner_lock
);
713 * binder_inner_proc_unlock() - Release inner lock for given binder_proc
714 * @proc: struct binder_proc to acquire
716 * Release lock acquired via binder_inner_proc_lock()
718 #define binder_inner_proc_unlock(proc) _binder_inner_proc_unlock(proc, __LINE__)
720 _binder_inner_proc_unlock(struct binder_proc
*proc
, int line
)
722 binder_debug(BINDER_DEBUG_SPINLOCKS
,
723 "%s: line=%d\n", __func__
, line
);
724 spin_unlock(&proc
->inner_lock
);
728 * binder_node_lock() - Acquire spinlock for given binder_node
729 * @node: struct binder_node to acquire
731 * Acquires node->lock. Used to protect binder_node fields
733 #define binder_node_lock(node) _binder_node_lock(node, __LINE__)
735 _binder_node_lock(struct binder_node
*node
, int line
)
737 binder_debug(BINDER_DEBUG_SPINLOCKS
,
738 "%s: line=%d\n", __func__
, line
);
739 spin_lock(&node
->lock
);
743 * binder_node_unlock() - Release spinlock for given binder_proc
744 * @node: struct binder_node to acquire
746 * Release lock acquired via binder_node_lock()
748 #define binder_node_unlock(node) _binder_node_unlock(node, __LINE__)
750 _binder_node_unlock(struct binder_node
*node
, int line
)
752 binder_debug(BINDER_DEBUG_SPINLOCKS
,
753 "%s: line=%d\n", __func__
, line
);
754 spin_unlock(&node
->lock
);
758 * binder_node_inner_lock() - Acquire node and inner locks
759 * @node: struct binder_node to acquire
761 * Acquires node->lock. If node->proc also acquires
762 * proc->inner_lock. Used to protect binder_node fields
764 #define binder_node_inner_lock(node) _binder_node_inner_lock(node, __LINE__)
766 _binder_node_inner_lock(struct binder_node
*node
, int line
)
768 binder_debug(BINDER_DEBUG_SPINLOCKS
,
769 "%s: line=%d\n", __func__
, line
);
770 spin_lock(&node
->lock
);
772 binder_inner_proc_lock(node
->proc
);
776 * binder_node_unlock() - Release node and inner locks
777 * @node: struct binder_node to acquire
779 * Release lock acquired via binder_node_lock()
781 #define binder_node_inner_unlock(node) _binder_node_inner_unlock(node, __LINE__)
783 _binder_node_inner_unlock(struct binder_node
*node
, int line
)
785 struct binder_proc
*proc
= node
->proc
;
787 binder_debug(BINDER_DEBUG_SPINLOCKS
,
788 "%s: line=%d\n", __func__
, line
);
790 binder_inner_proc_unlock(proc
);
791 spin_unlock(&node
->lock
);
794 #ifdef CONFIG_DEBUG_SNAPSHOT_BINDER
796 * Binder Debug Snapshot
798 static void init_binder_transaction_base(int type
, struct trace_binder_transaction_base
*base
,
799 struct binder_transaction
*t
, struct binder_thread
*from
,
800 struct binder_thread
*to
)
802 struct binder_thread
*t_from
;
803 struct binder_thread
*t_to
;
808 t_from
= t
->from
? t
->from
: (from
? from
: NULL
);
809 t_to
= t
->to_thread
? t
->to_thread
: (to
? to
: NULL
);
810 base
->trace_type
= type
;
811 base
->transaction_id
= t
->debug_id
;
812 base
->from_pid
= t_from
? t_from
->proc
->pid
: 0;
813 base
->from_tid
= t_from
? t_from
->pid
: 0;
814 base
->to_pid
= t
->to_proc
? t
->to_proc
->pid
: 0;
815 base
->to_tid
= t_to
? t_to
->pid
: 0;
817 strncpy(base
->from_pid_comm
, t_from
->proc
->tsk
->comm
, TASK_COMM_LEN
);
818 strncpy(base
->from_tid_comm
, t_from
->task
->comm
, TASK_COMM_LEN
);
820 base
->from_pid_comm
[0] = '\0';
821 base
->from_tid_comm
[0] = '\0';
824 strncpy(base
->to_pid_comm
, t
->to_proc
->tsk
->comm
, TASK_COMM_LEN
);
826 base
->to_pid_comm
[0] = '\0';
828 strncpy(base
->to_tid_comm
, t_to
->task
->comm
, TASK_COMM_LEN
);
830 base
->to_tid_comm
[0] = '\0';
833 static void dss_binder_transaction(int reply
, struct binder_transaction
*t
, struct binder_thread
*from
, int to_node_id
)
835 struct trace_binder_transaction_base base
;
836 struct trace_binder_transaction transaction
;
838 init_binder_transaction_base(TRANSACTION
, &base
, t
, from
, NULL
);
839 transaction
.to_node_id
= to_node_id
;
840 transaction
.reply
= reply
;
841 transaction
.flags
= t
->flags
;
842 transaction
.code
= t
->code
;
844 dbg_snapshot_binder(&base
, &transaction
, NULL
);
847 static void dss_binder_transaction_received(struct binder_transaction
*t
, struct binder_thread
*to
)
849 struct trace_binder_transaction_base base
;
851 init_binder_transaction_base(TRANSACTION_DONE
, &base
, t
, NULL
, to
);
853 dbg_snapshot_binder(&base
, NULL
, NULL
);
856 static void dss_binder_transaction_failed(int reply
, struct binder_transaction_log_entry
*e
,
857 char *from_pid_comm
, char *from_tid_comm
,
858 unsigned int flags
, unsigned int code
)
860 struct trace_binder_transaction_base base
;
861 struct trace_binder_transaction transaction
;
862 struct trace_binder_transaction_error error
;
864 base
.trace_type
= TRANSACTION_ERROR
;
865 base
.transaction_id
= e
->debug_id
;
866 base
.from_pid
= e
->from_proc
;
867 base
.from_tid
= e
->from_thread
;
868 base
.to_pid
= e
->to_proc
;
869 base
.to_tid
= e
->to_thread
;
870 strncpy(base
.from_pid_comm
, from_pid_comm
, TASK_COMM_LEN
);
871 strncpy(base
.from_tid_comm
, from_tid_comm
, TASK_COMM_LEN
);
872 base
.to_pid_comm
[0] = '\0';
873 base
.to_tid_comm
[0] = '\0';
874 transaction
.to_node_id
= e
->to_node
;
875 transaction
.reply
= reply
;
876 transaction
.flags
= flags
;
877 transaction
.code
= code
;
878 error
.return_error
= e
->return_error
;
879 error
.return_error_param
= e
->return_error_param
;
880 error
.return_error_line
= e
->return_error_line
;
882 dbg_snapshot_binder(&base
, &transaction
, &error
);
884 #endif /* CONFIG_DEBUG_SNAPSHOT_BINDER */
886 static bool binder_worklist_empty_ilocked(struct list_head
*list
)
888 return list_empty(list
);
892 * binder_worklist_empty() - Check if no items on the work list
893 * @proc: binder_proc associated with list
894 * @list: list to check
896 * Return: true if there are no items on list, else false
898 static bool binder_worklist_empty(struct binder_proc
*proc
,
899 struct list_head
*list
)
903 binder_inner_proc_lock(proc
);
904 ret
= binder_worklist_empty_ilocked(list
);
905 binder_inner_proc_unlock(proc
);
910 * binder_enqueue_work_ilocked() - Add an item to the work list
911 * @work: struct binder_work to add to list
912 * @target_list: list to add work to
914 * Adds the work to the specified list. Asserts that work
915 * is not already on a list.
917 * Requires the proc->inner_lock to be held.
920 binder_enqueue_work_ilocked(struct binder_work
*work
,
921 struct list_head
*target_list
)
923 BUG_ON(target_list
== NULL
);
924 BUG_ON(work
->entry
.next
&& !list_empty(&work
->entry
));
925 list_add_tail(&work
->entry
, target_list
);
929 * binder_enqueue_deferred_thread_work_ilocked() - Add deferred thread work
930 * @thread: thread to queue work to
931 * @work: struct binder_work to add to list
933 * Adds the work to the todo list of the thread. Doesn't set the process_todo
934 * flag, which means that (if it wasn't already set) the thread will go to
935 * sleep without handling this work when it calls read.
937 * Requires the proc->inner_lock to be held.
940 binder_enqueue_deferred_thread_work_ilocked(struct binder_thread
*thread
,
941 struct binder_work
*work
)
943 binder_enqueue_work_ilocked(work
, &thread
->todo
);
947 * binder_enqueue_thread_work_ilocked() - Add an item to the thread work list
948 * @thread: thread to queue work to
949 * @work: struct binder_work to add to list
951 * Adds the work to the todo list of the thread, and enables processing
954 * Requires the proc->inner_lock to be held.
957 binder_enqueue_thread_work_ilocked(struct binder_thread
*thread
,
958 struct binder_work
*work
)
960 binder_enqueue_work_ilocked(work
, &thread
->todo
);
961 thread
->process_todo
= true;
965 * binder_enqueue_thread_work() - Add an item to the thread work list
966 * @thread: thread to queue work to
967 * @work: struct binder_work to add to list
969 * Adds the work to the todo list of the thread, and enables processing
973 binder_enqueue_thread_work(struct binder_thread
*thread
,
974 struct binder_work
*work
)
976 binder_inner_proc_lock(thread
->proc
);
977 binder_enqueue_thread_work_ilocked(thread
, work
);
978 binder_inner_proc_unlock(thread
->proc
);
982 binder_dequeue_work_ilocked(struct binder_work
*work
)
984 list_del_init(&work
->entry
);
988 * binder_dequeue_work() - Removes an item from the work list
989 * @proc: binder_proc associated with list
990 * @work: struct binder_work to remove from list
992 * Removes the specified work item from whatever list it is on.
993 * Can safely be called if work is not on any list.
996 binder_dequeue_work(struct binder_proc
*proc
, struct binder_work
*work
)
998 binder_inner_proc_lock(proc
);
999 binder_dequeue_work_ilocked(work
);
1000 binder_inner_proc_unlock(proc
);
1003 static struct binder_work
*binder_dequeue_work_head_ilocked(
1004 struct list_head
*list
)
1006 struct binder_work
*w
;
1008 w
= list_first_entry_or_null(list
, struct binder_work
, entry
);
1010 list_del_init(&w
->entry
);
1015 binder_defer_work(struct binder_proc
*proc
, enum binder_deferred_state defer
);
1016 static void binder_free_thread(struct binder_thread
*thread
);
1017 static void binder_free_proc(struct binder_proc
*proc
);
1018 static void binder_inc_node_tmpref_ilocked(struct binder_node
*node
);
1020 static int task_get_unused_fd_flags(struct binder_proc
*proc
, int flags
)
1022 unsigned long rlim_cur
;
1026 mutex_lock(&proc
->files_lock
);
1027 if (proc
->files
== NULL
) {
1031 if (!lock_task_sighand(proc
->tsk
, &irqs
)) {
1035 rlim_cur
= task_rlimit(proc
->tsk
, RLIMIT_NOFILE
);
1036 unlock_task_sighand(proc
->tsk
, &irqs
);
1038 ret
= __alloc_fd(proc
->files
, 0, rlim_cur
, flags
);
1040 mutex_unlock(&proc
->files_lock
);
1045 * copied from fd_install
1047 static void task_fd_install(
1048 struct binder_proc
*proc
, unsigned int fd
, struct file
*file
)
1050 mutex_lock(&proc
->files_lock
);
1052 __fd_install(proc
->files
, fd
, file
);
1053 mutex_unlock(&proc
->files_lock
);
1057 * copied from sys_close
1059 static long task_close_fd(struct binder_proc
*proc
, unsigned int fd
)
1063 mutex_lock(&proc
->files_lock
);
1064 if (proc
->files
== NULL
) {
1068 retval
= __close_fd(proc
->files
, fd
);
1069 /* can't restart close syscall because file table entry was cleared */
1070 if (unlikely(retval
== -ERESTARTSYS
||
1071 retval
== -ERESTARTNOINTR
||
1072 retval
== -ERESTARTNOHAND
||
1073 retval
== -ERESTART_RESTARTBLOCK
))
1076 mutex_unlock(&proc
->files_lock
);
1080 static bool binder_has_work_ilocked(struct binder_thread
*thread
,
1083 return thread
->process_todo
||
1084 thread
->looper_need_return
||
1086 !binder_worklist_empty_ilocked(&thread
->proc
->todo
));
1089 static bool binder_has_work(struct binder_thread
*thread
, bool do_proc_work
)
1093 binder_inner_proc_lock(thread
->proc
);
1094 has_work
= binder_has_work_ilocked(thread
, do_proc_work
);
1095 binder_inner_proc_unlock(thread
->proc
);
1100 static bool binder_available_for_proc_work_ilocked(struct binder_thread
*thread
)
1102 return !thread
->transaction_stack
&&
1103 binder_worklist_empty_ilocked(&thread
->todo
) &&
1104 (thread
->looper
& (BINDER_LOOPER_STATE_ENTERED
|
1105 BINDER_LOOPER_STATE_REGISTERED
));
1108 static void binder_wakeup_poll_threads_ilocked(struct binder_proc
*proc
,
1112 struct binder_thread
*thread
;
1114 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
)) {
1115 thread
= rb_entry(n
, struct binder_thread
, rb_node
);
1116 if (thread
->looper
& BINDER_LOOPER_STATE_POLL
&&
1117 binder_available_for_proc_work_ilocked(thread
)) {
1119 wake_up_interruptible_sync(&thread
->wait
);
1121 wake_up_interruptible(&thread
->wait
);
1127 * binder_select_thread_ilocked() - selects a thread for doing proc work.
1128 * @proc: process to select a thread from
1130 * Note that calling this function moves the thread off the waiting_threads
1131 * list, so it can only be woken up by the caller of this function, or a
1132 * signal. Therefore, callers *should* always wake up the thread this function
1135 * Return: If there's a thread currently waiting for process work,
1136 * returns that thread. Otherwise returns NULL.
1138 static struct binder_thread
*
1139 binder_select_thread_ilocked(struct binder_proc
*proc
)
1141 struct binder_thread
*thread
;
1143 assert_spin_locked(&proc
->inner_lock
);
1144 thread
= list_first_entry_or_null(&proc
->waiting_threads
,
1145 struct binder_thread
,
1146 waiting_thread_node
);
1149 list_del_init(&thread
->waiting_thread_node
);
1155 * binder_wakeup_thread_ilocked() - wakes up a thread for doing proc work.
1156 * @proc: process to wake up a thread in
1157 * @thread: specific thread to wake-up (may be NULL)
1158 * @sync: whether to do a synchronous wake-up
1160 * This function wakes up a thread in the @proc process.
1161 * The caller may provide a specific thread to wake-up in
1162 * the @thread parameter. If @thread is NULL, this function
1163 * will wake up threads that have called poll().
1165 * Note that for this function to work as expected, callers
1166 * should first call binder_select_thread() to find a thread
1167 * to handle the work (if they don't have a thread already),
1168 * and pass the result into the @thread parameter.
1170 static void binder_wakeup_thread_ilocked(struct binder_proc
*proc
,
1171 struct binder_thread
*thread
,
1174 assert_spin_locked(&proc
->inner_lock
);
1178 wake_up_interruptible_sync(&thread
->wait
);
1180 wake_up_interruptible(&thread
->wait
);
1184 /* Didn't find a thread waiting for proc work; this can happen
1186 * 1. All threads are busy handling transactions
1187 * In that case, one of those threads should call back into
1188 * the kernel driver soon and pick up this work.
1189 * 2. Threads are using the (e)poll interface, in which case
1190 * they may be blocked on the waitqueue without having been
1191 * added to waiting_threads. For this case, we just iterate
1192 * over all threads not handling transaction work, and
1193 * wake them all up. We wake all because we don't know whether
1194 * a thread that called into (e)poll is handling non-binder
1197 binder_wakeup_poll_threads_ilocked(proc
, sync
);
1200 static void binder_wakeup_proc_ilocked(struct binder_proc
*proc
)
1202 struct binder_thread
*thread
= binder_select_thread_ilocked(proc
);
1204 binder_wakeup_thread_ilocked(proc
, thread
, /* sync = */false);
1207 static bool is_rt_policy(int policy
)
1209 return policy
== SCHED_FIFO
|| policy
== SCHED_RR
;
1212 static bool is_fair_policy(int policy
)
1214 return policy
== SCHED_NORMAL
|| policy
== SCHED_BATCH
;
1217 static bool binder_supported_policy(int policy
)
1219 return is_fair_policy(policy
) || is_rt_policy(policy
);
1222 static int to_userspace_prio(int policy
, int kernel_priority
)
1224 if (is_fair_policy(policy
))
1225 return PRIO_TO_NICE(kernel_priority
);
1227 return MAX_USER_RT_PRIO
- 1 - kernel_priority
;
1230 static int to_kernel_prio(int policy
, int user_priority
)
1232 if (is_fair_policy(policy
))
1233 return NICE_TO_PRIO(user_priority
);
1235 return MAX_USER_RT_PRIO
- 1 - user_priority
;
1238 static void binder_do_set_priority(struct task_struct
*task
,
1239 struct binder_priority desired
,
1242 int priority
; /* user-space prio value */
1244 unsigned int policy
= desired
.sched_policy
;
1246 if (task
->policy
== policy
&& task
->normal_prio
== desired
.prio
)
1249 has_cap_nice
= has_capability_noaudit(task
, CAP_SYS_NICE
);
1251 priority
= to_userspace_prio(policy
, desired
.prio
);
1253 if (verify
&& is_rt_policy(policy
) && !has_cap_nice
) {
1254 long max_rtprio
= task_rlimit(task
, RLIMIT_RTPRIO
);
1256 if (max_rtprio
== 0) {
1257 policy
= SCHED_NORMAL
;
1258 priority
= MIN_NICE
;
1259 } else if (priority
> max_rtprio
) {
1260 priority
= max_rtprio
;
1264 if (verify
&& is_fair_policy(policy
) && !has_cap_nice
) {
1265 long min_nice
= rlimit_to_nice(task_rlimit(task
, RLIMIT_NICE
));
1267 if (min_nice
> MAX_NICE
) {
1268 binder_user_error("%d(%s) RLIMIT_NICE not set\n",
1269 task
->pid
, task
->comm
);
1271 } else if (priority
< min_nice
) {
1272 priority
= min_nice
;
1276 if (policy
!= desired
.sched_policy
||
1277 to_kernel_prio(policy
, priority
) != desired
.prio
)
1278 binder_debug(BINDER_DEBUG_PRIORITY_CAP
,
1279 "%d(%s): priority %d not allowed, using %d instead\n",
1280 task
->pid
, task
->comm
, desired
.prio
,
1281 to_kernel_prio(policy
, priority
));
1283 trace_binder_set_priority(task
->tgid
, task
->pid
, task
->normal_prio
,
1284 to_kernel_prio(policy
, priority
),
1287 /* Set the actual priority */
1288 if (task
->policy
!= policy
|| is_rt_policy(policy
)) {
1289 struct sched_param params
;
1291 params
.sched_priority
= is_rt_policy(policy
) ? priority
: 0;
1293 sched_setscheduler_nocheck(task
,
1294 policy
| SCHED_RESET_ON_FORK
,
1297 if (is_fair_policy(policy
))
1298 set_user_nice(task
, priority
);
1301 static void binder_set_priority(struct task_struct
*task
,
1302 struct binder_priority desired
)
1304 binder_do_set_priority(task
, desired
, /* verify = */ true);
1307 static void binder_restore_priority(struct task_struct
*task
,
1308 struct binder_priority desired
)
1310 binder_do_set_priority(task
, desired
, /* verify = */ false);
1313 static void binder_transaction_priority(struct task_struct
*task
,
1314 struct binder_transaction
*t
,
1315 struct binder_priority node_prio
,
1318 struct binder_priority desired_prio
= t
->priority
;
1320 if (t
->set_priority_called
)
1323 t
->set_priority_called
= true;
1324 t
->saved_priority
.sched_policy
= task
->policy
;
1325 t
->saved_priority
.prio
= task
->normal_prio
;
1327 if (!inherit_rt
&& is_rt_policy(desired_prio
.sched_policy
)) {
1328 desired_prio
.prio
= NICE_TO_PRIO(0);
1329 desired_prio
.sched_policy
= SCHED_NORMAL
;
1332 if (node_prio
.prio
< t
->priority
.prio
||
1333 (node_prio
.prio
== t
->priority
.prio
&&
1334 node_prio
.sched_policy
== SCHED_FIFO
)) {
1336 * In case the minimum priority on the node is
1337 * higher (lower value), use that priority. If
1338 * the priority is the same, but the node uses
1339 * SCHED_FIFO, prefer SCHED_FIFO, since it can
1340 * run unbounded, unlike SCHED_RR.
1342 desired_prio
= node_prio
;
1345 binder_set_priority(task
, desired_prio
);
1348 static struct binder_node
*binder_get_node_ilocked(struct binder_proc
*proc
,
1349 binder_uintptr_t ptr
)
1351 struct rb_node
*n
= proc
->nodes
.rb_node
;
1352 struct binder_node
*node
;
1354 assert_spin_locked(&proc
->inner_lock
);
1357 node
= rb_entry(n
, struct binder_node
, rb_node
);
1359 if (ptr
< node
->ptr
)
1361 else if (ptr
> node
->ptr
)
1365 * take an implicit weak reference
1366 * to ensure node stays alive until
1367 * call to binder_put_node()
1369 binder_inc_node_tmpref_ilocked(node
);
1376 static struct binder_node
*binder_get_node(struct binder_proc
*proc
,
1377 binder_uintptr_t ptr
)
1379 struct binder_node
*node
;
1381 binder_inner_proc_lock(proc
);
1382 node
= binder_get_node_ilocked(proc
, ptr
);
1383 binder_inner_proc_unlock(proc
);
1387 static struct binder_node
*binder_init_node_ilocked(
1388 struct binder_proc
*proc
,
1389 struct binder_node
*new_node
,
1390 struct flat_binder_object
*fp
)
1392 struct rb_node
**p
= &proc
->nodes
.rb_node
;
1393 struct rb_node
*parent
= NULL
;
1394 struct binder_node
*node
;
1395 binder_uintptr_t ptr
= fp
? fp
->binder
: 0;
1396 binder_uintptr_t cookie
= fp
? fp
->cookie
: 0;
1397 __u32 flags
= fp
? fp
->flags
: 0;
1400 assert_spin_locked(&proc
->inner_lock
);
1405 node
= rb_entry(parent
, struct binder_node
, rb_node
);
1407 if (ptr
< node
->ptr
)
1409 else if (ptr
> node
->ptr
)
1410 p
= &(*p
)->rb_right
;
1413 * A matching node is already in
1414 * the rb tree. Abandon the init
1417 binder_inc_node_tmpref_ilocked(node
);
1422 binder_stats_created(BINDER_STAT_NODE
);
1424 rb_link_node(&node
->rb_node
, parent
, p
);
1425 rb_insert_color(&node
->rb_node
, &proc
->nodes
);
1426 node
->debug_id
= atomic_inc_return(&binder_last_id
);
1429 node
->cookie
= cookie
;
1430 node
->work
.type
= BINDER_WORK_NODE
;
1431 priority
= flags
& FLAT_BINDER_FLAG_PRIORITY_MASK
;
1432 node
->sched_policy
= (flags
& FLAT_BINDER_FLAG_SCHED_POLICY_MASK
) >>
1433 FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT
;
1434 node
->min_priority
= to_kernel_prio(node
->sched_policy
, priority
);
1435 node
->accept_fds
= !!(flags
& FLAT_BINDER_FLAG_ACCEPTS_FDS
);
1436 node
->inherit_rt
= !!(flags
& FLAT_BINDER_FLAG_INHERIT_RT
);
1437 node
->txn_security_ctx
= !!(flags
& FLAT_BINDER_FLAG_TXN_SECURITY_CTX
);
1438 spin_lock_init(&node
->lock
);
1439 INIT_LIST_HEAD(&node
->work
.entry
);
1440 INIT_LIST_HEAD(&node
->async_todo
);
1441 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1442 "%d:%d(%s:%s) node %d u%016llx c%016llx created\n",
1443 proc
->pid
, current
->pid
, proc
->tsk
->comm
, current
->comm
, node
->debug_id
,
1444 (u64
)node
->ptr
, (u64
)node
->cookie
);
1449 static struct binder_node
*binder_new_node(struct binder_proc
*proc
,
1450 struct flat_binder_object
*fp
)
1452 struct binder_node
*node
;
1453 struct binder_node
*new_node
= kzalloc(sizeof(*node
), GFP_KERNEL
);
1457 binder_inner_proc_lock(proc
);
1458 node
= binder_init_node_ilocked(proc
, new_node
, fp
);
1459 binder_inner_proc_unlock(proc
);
1460 if (node
!= new_node
)
1462 * The node was already added by another thread
1469 static void binder_free_node(struct binder_node
*node
)
1472 binder_stats_deleted(BINDER_STAT_NODE
);
1475 static int binder_inc_node_nilocked(struct binder_node
*node
, int strong
,
1477 struct list_head
*target_list
)
1479 struct binder_proc
*proc
= node
->proc
;
1481 assert_spin_locked(&node
->lock
);
1483 assert_spin_locked(&proc
->inner_lock
);
1486 if (target_list
== NULL
&&
1487 node
->internal_strong_refs
== 0 &&
1489 node
== node
->proc
->context
->binder_context_mgr_node
&&
1490 node
->has_strong_ref
)) {
1491 pr_err("invalid inc strong node for %d\n",
1495 node
->internal_strong_refs
++;
1497 node
->local_strong_refs
++;
1498 if (!node
->has_strong_ref
&& target_list
) {
1499 binder_dequeue_work_ilocked(&node
->work
);
1501 * Note: this function is the only place where we queue
1502 * directly to a thread->todo without using the
1503 * corresponding binder_enqueue_thread_work() helper
1504 * functions; in this case it's ok to not set the
1505 * process_todo flag, since we know this node work will
1506 * always be followed by other work that starts queue
1507 * processing: in case of synchronous transactions, a
1508 * BR_REPLY or BR_ERROR; in case of oneway
1509 * transactions, a BR_TRANSACTION_COMPLETE.
1511 binder_enqueue_work_ilocked(&node
->work
, target_list
);
1515 node
->local_weak_refs
++;
1516 if (!node
->has_weak_ref
&& list_empty(&node
->work
.entry
)) {
1517 if (target_list
== NULL
) {
1518 pr_err("invalid inc weak node for %d\n",
1525 binder_enqueue_work_ilocked(&node
->work
, target_list
);
1531 static int binder_inc_node(struct binder_node
*node
, int strong
, int internal
,
1532 struct list_head
*target_list
)
1536 binder_node_inner_lock(node
);
1537 ret
= binder_inc_node_nilocked(node
, strong
, internal
, target_list
);
1538 binder_node_inner_unlock(node
);
1543 static bool binder_dec_node_nilocked(struct binder_node
*node
,
1544 int strong
, int internal
)
1546 struct binder_proc
*proc
= node
->proc
;
1548 assert_spin_locked(&node
->lock
);
1550 assert_spin_locked(&proc
->inner_lock
);
1553 node
->internal_strong_refs
--;
1555 node
->local_strong_refs
--;
1556 if (node
->local_strong_refs
|| node
->internal_strong_refs
)
1560 node
->local_weak_refs
--;
1561 if (node
->local_weak_refs
|| node
->tmp_refs
||
1562 !hlist_empty(&node
->refs
))
1566 if (proc
&& (node
->has_strong_ref
|| node
->has_weak_ref
)) {
1567 if (list_empty(&node
->work
.entry
)) {
1568 binder_enqueue_work_ilocked(&node
->work
, &proc
->todo
);
1569 binder_wakeup_proc_ilocked(proc
);
1572 if (hlist_empty(&node
->refs
) && !node
->local_strong_refs
&&
1573 !node
->local_weak_refs
&& !node
->tmp_refs
) {
1575 binder_dequeue_work_ilocked(&node
->work
);
1576 rb_erase(&node
->rb_node
, &proc
->nodes
);
1577 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1578 "refless node %d deleted\n",
1581 BUG_ON(!list_empty(&node
->work
.entry
));
1582 spin_lock(&binder_dead_nodes_lock
);
1584 * tmp_refs could have changed so
1587 if (node
->tmp_refs
) {
1588 spin_unlock(&binder_dead_nodes_lock
);
1591 hlist_del(&node
->dead_node
);
1592 spin_unlock(&binder_dead_nodes_lock
);
1593 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1594 "dead node %d deleted\n",
1603 static void binder_dec_node(struct binder_node
*node
, int strong
, int internal
)
1607 binder_node_inner_lock(node
);
1608 free_node
= binder_dec_node_nilocked(node
, strong
, internal
);
1609 binder_node_inner_unlock(node
);
1611 binder_free_node(node
);
1614 static void binder_inc_node_tmpref_ilocked(struct binder_node
*node
)
1617 * No call to binder_inc_node() is needed since we
1618 * don't need to inform userspace of any changes to
1625 * binder_inc_node_tmpref() - take a temporary reference on node
1626 * @node: node to reference
1628 * Take reference on node to prevent the node from being freed
1629 * while referenced only by a local variable. The inner lock is
1630 * needed to serialize with the node work on the queue (which
1631 * isn't needed after the node is dead). If the node is dead
1632 * (node->proc is NULL), use binder_dead_nodes_lock to protect
1633 * node->tmp_refs against dead-node-only cases where the node
1634 * lock cannot be acquired (eg traversing the dead node list to
1637 static void binder_inc_node_tmpref(struct binder_node
*node
)
1639 binder_node_lock(node
);
1641 binder_inner_proc_lock(node
->proc
);
1643 spin_lock(&binder_dead_nodes_lock
);
1644 binder_inc_node_tmpref_ilocked(node
);
1646 binder_inner_proc_unlock(node
->proc
);
1648 spin_unlock(&binder_dead_nodes_lock
);
1649 binder_node_unlock(node
);
1653 * binder_dec_node_tmpref() - remove a temporary reference on node
1654 * @node: node to reference
1656 * Release temporary reference on node taken via binder_inc_node_tmpref()
1658 static void binder_dec_node_tmpref(struct binder_node
*node
)
1662 binder_node_inner_lock(node
);
1664 spin_lock(&binder_dead_nodes_lock
);
1666 BUG_ON(node
->tmp_refs
< 0);
1668 spin_unlock(&binder_dead_nodes_lock
);
1670 * Call binder_dec_node() to check if all refcounts are 0
1671 * and cleanup is needed. Calling with strong=0 and internal=1
1672 * causes no actual reference to be released in binder_dec_node().
1673 * If that changes, a change is needed here too.
1675 free_node
= binder_dec_node_nilocked(node
, 0, 1);
1676 binder_node_inner_unlock(node
);
1678 binder_free_node(node
);
1681 static void binder_put_node(struct binder_node
*node
)
1683 binder_dec_node_tmpref(node
);
1686 static struct binder_ref
*binder_get_ref_olocked(struct binder_proc
*proc
,
1687 u32 desc
, bool need_strong_ref
)
1689 struct rb_node
*n
= proc
->refs_by_desc
.rb_node
;
1690 struct binder_ref
*ref
;
1693 ref
= rb_entry(n
, struct binder_ref
, rb_node_desc
);
1695 if (desc
< ref
->data
.desc
) {
1697 } else if (desc
> ref
->data
.desc
) {
1699 } else if (need_strong_ref
&& !ref
->data
.strong
) {
1700 binder_user_error("tried to use weak ref as strong ref\n");
1710 * binder_get_ref_for_node_olocked() - get the ref associated with given node
1711 * @proc: binder_proc that owns the ref
1712 * @node: binder_node of target
1713 * @new_ref: newly allocated binder_ref to be initialized or %NULL
1715 * Look up the ref for the given node and return it if it exists
1717 * If it doesn't exist and the caller provides a newly allocated
1718 * ref, initialize the fields of the newly allocated ref and insert
1719 * into the given proc rb_trees and node refs list.
1721 * Return: the ref for node. It is possible that another thread
1722 * allocated/initialized the ref first in which case the
1723 * returned ref would be different than the passed-in
1724 * new_ref. new_ref must be kfree'd by the caller in
1727 static struct binder_ref
*binder_get_ref_for_node_olocked(
1728 struct binder_proc
*proc
,
1729 struct binder_node
*node
,
1730 struct binder_ref
*new_ref
)
1732 struct binder_context
*context
= proc
->context
;
1733 struct rb_node
**p
= &proc
->refs_by_node
.rb_node
;
1734 struct rb_node
*parent
= NULL
;
1735 struct binder_ref
*ref
;
1740 ref
= rb_entry(parent
, struct binder_ref
, rb_node_node
);
1742 if (node
< ref
->node
)
1744 else if (node
> ref
->node
)
1745 p
= &(*p
)->rb_right
;
1752 binder_stats_created(BINDER_STAT_REF
);
1753 new_ref
->data
.debug_id
= atomic_inc_return(&binder_last_id
);
1754 new_ref
->proc
= proc
;
1755 new_ref
->node
= node
;
1756 rb_link_node(&new_ref
->rb_node_node
, parent
, p
);
1757 rb_insert_color(&new_ref
->rb_node_node
, &proc
->refs_by_node
);
1759 new_ref
->data
.desc
= (node
== context
->binder_context_mgr_node
) ? 0 : 1;
1760 for (n
= rb_first(&proc
->refs_by_desc
); n
!= NULL
; n
= rb_next(n
)) {
1761 ref
= rb_entry(n
, struct binder_ref
, rb_node_desc
);
1762 if (ref
->data
.desc
> new_ref
->data
.desc
)
1764 new_ref
->data
.desc
= ref
->data
.desc
+ 1;
1767 p
= &proc
->refs_by_desc
.rb_node
;
1770 ref
= rb_entry(parent
, struct binder_ref
, rb_node_desc
);
1772 if (new_ref
->data
.desc
< ref
->data
.desc
)
1774 else if (new_ref
->data
.desc
> ref
->data
.desc
)
1775 p
= &(*p
)->rb_right
;
1779 rb_link_node(&new_ref
->rb_node_desc
, parent
, p
);
1780 rb_insert_color(&new_ref
->rb_node_desc
, &proc
->refs_by_desc
);
1782 binder_node_lock(node
);
1783 hlist_add_head(&new_ref
->node_entry
, &node
->refs
);
1785 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1786 "%d(%s) new ref %d desc %d for node %d\n",
1787 proc
->pid
, proc
->tsk
->comm
, new_ref
->data
.debug_id
, new_ref
->data
.desc
,
1789 binder_node_unlock(node
);
1793 static void binder_cleanup_ref_olocked(struct binder_ref
*ref
)
1795 bool delete_node
= false;
1797 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1798 "%d(%s) delete ref %d desc %d for node %d\n",
1799 ref
->proc
->pid
, ref
->proc
->tsk
->comm
, ref
->data
.debug_id
, ref
->data
.desc
,
1800 ref
->node
->debug_id
);
1802 rb_erase(&ref
->rb_node_desc
, &ref
->proc
->refs_by_desc
);
1803 rb_erase(&ref
->rb_node_node
, &ref
->proc
->refs_by_node
);
1805 binder_node_inner_lock(ref
->node
);
1806 if (ref
->data
.strong
)
1807 binder_dec_node_nilocked(ref
->node
, 1, 1);
1809 hlist_del(&ref
->node_entry
);
1810 delete_node
= binder_dec_node_nilocked(ref
->node
, 0, 1);
1811 binder_node_inner_unlock(ref
->node
);
1813 * Clear ref->node unless we want the caller to free the node
1817 * The caller uses ref->node to determine
1818 * whether the node needs to be freed. Clear
1819 * it since the node is still alive.
1825 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
1826 "%d(%s) delete ref %d desc %d has death notification\n",
1827 ref
->proc
->pid
, ref
->proc
->tsk
->comm
, ref
->data
.debug_id
,
1829 binder_dequeue_work(ref
->proc
, &ref
->death
->work
);
1830 binder_stats_deleted(BINDER_STAT_DEATH
);
1832 binder_stats_deleted(BINDER_STAT_REF
);
1836 * binder_inc_ref_olocked() - increment the ref for given handle
1837 * @ref: ref to be incremented
1838 * @strong: if true, strong increment, else weak
1839 * @target_list: list to queue node work on
1841 * Increment the ref. @ref->proc->outer_lock must be held on entry
1843 * Return: 0, if successful, else errno
1845 static int binder_inc_ref_olocked(struct binder_ref
*ref
, int strong
,
1846 struct list_head
*target_list
)
1851 if (ref
->data
.strong
== 0) {
1852 ret
= binder_inc_node(ref
->node
, 1, 1, target_list
);
1858 if (ref
->data
.weak
== 0) {
1859 ret
= binder_inc_node(ref
->node
, 0, 1, target_list
);
1869 * binder_dec_ref() - dec the ref for given handle
1870 * @ref: ref to be decremented
1871 * @strong: if true, strong decrement, else weak
1873 * Decrement the ref.
1875 * Return: true if ref is cleaned up and ready to be freed
1877 static bool binder_dec_ref_olocked(struct binder_ref
*ref
, int strong
)
1880 if (ref
->data
.strong
== 0) {
1881 binder_user_error("%d(%s) invalid dec strong, ref %d desc %d s %d w %d\n",
1882 ref
->proc
->pid
, ref
->proc
->tsk
->comm
, ref
->data
.debug_id
,
1883 ref
->data
.desc
, ref
->data
.strong
,
1888 if (ref
->data
.strong
== 0)
1889 binder_dec_node(ref
->node
, strong
, 1);
1891 if (ref
->data
.weak
== 0) {
1892 binder_user_error("%d(%s) invalid dec weak, ref %d desc %d s %d w %d\n",
1893 ref
->proc
->pid
, ref
->proc
->tsk
->comm
, ref
->data
.debug_id
,
1894 ref
->data
.desc
, ref
->data
.strong
,
1900 if (ref
->data
.strong
== 0 && ref
->data
.weak
== 0) {
1901 binder_cleanup_ref_olocked(ref
);
1908 * binder_get_node_from_ref() - get the node from the given proc/desc
1909 * @proc: proc containing the ref
1910 * @desc: the handle associated with the ref
1911 * @need_strong_ref: if true, only return node if ref is strong
1912 * @rdata: the id/refcount data for the ref
1914 * Given a proc and ref handle, return the associated binder_node
1916 * Return: a binder_node or NULL if not found or not strong when strong required
1918 static struct binder_node
*binder_get_node_from_ref(
1919 struct binder_proc
*proc
,
1920 u32 desc
, bool need_strong_ref
,
1921 struct binder_ref_data
*rdata
)
1923 struct binder_node
*node
;
1924 struct binder_ref
*ref
;
1926 binder_proc_lock(proc
);
1927 ref
= binder_get_ref_olocked(proc
, desc
, need_strong_ref
);
1932 * Take an implicit reference on the node to ensure
1933 * it stays alive until the call to binder_put_node()
1935 binder_inc_node_tmpref(node
);
1938 binder_proc_unlock(proc
);
1943 binder_proc_unlock(proc
);
1948 * binder_free_ref() - free the binder_ref
1951 * Free the binder_ref. Free the binder_node indicated by ref->node
1952 * (if non-NULL) and the binder_ref_death indicated by ref->death.
1954 static void binder_free_ref(struct binder_ref
*ref
)
1957 binder_free_node(ref
->node
);
1963 * binder_update_ref_for_handle() - inc/dec the ref for given handle
1964 * @proc: proc containing the ref
1965 * @desc: the handle associated with the ref
1966 * @increment: true=inc reference, false=dec reference
1967 * @strong: true=strong reference, false=weak reference
1968 * @rdata: the id/refcount data for the ref
1970 * Given a proc and ref handle, increment or decrement the ref
1971 * according to "increment" arg.
1973 * Return: 0 if successful, else errno
1975 static int binder_update_ref_for_handle(struct binder_proc
*proc
,
1976 uint32_t desc
, bool increment
, bool strong
,
1977 struct binder_ref_data
*rdata
)
1980 struct binder_ref
*ref
;
1981 bool delete_ref
= false;
1983 binder_proc_lock(proc
);
1984 ref
= binder_get_ref_olocked(proc
, desc
, strong
);
1990 ret
= binder_inc_ref_olocked(ref
, strong
, NULL
);
1992 delete_ref
= binder_dec_ref_olocked(ref
, strong
);
1996 binder_proc_unlock(proc
);
1999 binder_free_ref(ref
);
2003 binder_proc_unlock(proc
);
2008 * binder_dec_ref_for_handle() - dec the ref for given handle
2009 * @proc: proc containing the ref
2010 * @desc: the handle associated with the ref
2011 * @strong: true=strong reference, false=weak reference
2012 * @rdata: the id/refcount data for the ref
2014 * Just calls binder_update_ref_for_handle() to decrement the ref.
2016 * Return: 0 if successful, else errno
2018 static int binder_dec_ref_for_handle(struct binder_proc
*proc
,
2019 uint32_t desc
, bool strong
, struct binder_ref_data
*rdata
)
2021 return binder_update_ref_for_handle(proc
, desc
, false, strong
, rdata
);
2026 * binder_inc_ref_for_node() - increment the ref for given proc/node
2027 * @proc: proc containing the ref
2028 * @node: target node
2029 * @strong: true=strong reference, false=weak reference
2030 * @target_list: worklist to use if node is incremented
2031 * @rdata: the id/refcount data for the ref
2033 * Given a proc and node, increment the ref. Create the ref if it
2034 * doesn't already exist
2036 * Return: 0 if successful, else errno
2038 static int binder_inc_ref_for_node(struct binder_proc
*proc
,
2039 struct binder_node
*node
,
2041 struct list_head
*target_list
,
2042 struct binder_ref_data
*rdata
)
2044 struct binder_ref
*ref
;
2045 struct binder_ref
*new_ref
= NULL
;
2048 binder_proc_lock(proc
);
2049 ref
= binder_get_ref_for_node_olocked(proc
, node
, NULL
);
2051 binder_proc_unlock(proc
);
2052 new_ref
= kzalloc(sizeof(*ref
), GFP_KERNEL
);
2055 binder_proc_lock(proc
);
2056 ref
= binder_get_ref_for_node_olocked(proc
, node
, new_ref
);
2058 ret
= binder_inc_ref_olocked(ref
, strong
, target_list
);
2060 binder_proc_unlock(proc
);
2061 if (new_ref
&& ref
!= new_ref
)
2063 * Another thread created the ref first so
2064 * free the one we allocated
2070 static void binder_pop_transaction_ilocked(struct binder_thread
*target_thread
,
2071 struct binder_transaction
*t
)
2073 BUG_ON(!target_thread
);
2074 assert_spin_locked(&target_thread
->proc
->inner_lock
);
2075 BUG_ON(target_thread
->transaction_stack
!= t
);
2076 BUG_ON(target_thread
->transaction_stack
->from
!= target_thread
);
2077 target_thread
->transaction_stack
=
2078 target_thread
->transaction_stack
->from_parent
;
2083 * binder_thread_dec_tmpref() - decrement thread->tmp_ref
2084 * @thread: thread to decrement
2086 * A thread needs to be kept alive while being used to create or
2087 * handle a transaction. binder_get_txn_from() is used to safely
2088 * extract t->from from a binder_transaction and keep the thread
2089 * indicated by t->from from being freed. When done with that
2090 * binder_thread, this function is called to decrement the
2091 * tmp_ref and free if appropriate (thread has been released
2092 * and no transaction being processed by the driver)
2094 static void binder_thread_dec_tmpref(struct binder_thread
*thread
)
2097 * atomic is used to protect the counter value while
2098 * it cannot reach zero or thread->is_dead is false
2100 binder_inner_proc_lock(thread
->proc
);
2101 atomic_dec(&thread
->tmp_ref
);
2102 if (thread
->is_dead
&& !atomic_read(&thread
->tmp_ref
)) {
2103 binder_inner_proc_unlock(thread
->proc
);
2104 binder_free_thread(thread
);
2107 binder_inner_proc_unlock(thread
->proc
);
2111 * binder_proc_dec_tmpref() - decrement proc->tmp_ref
2112 * @proc: proc to decrement
2114 * A binder_proc needs to be kept alive while being used to create or
2115 * handle a transaction. proc->tmp_ref is incremented when
2116 * creating a new transaction or the binder_proc is currently in-use
2117 * by threads that are being released. When done with the binder_proc,
2118 * this function is called to decrement the counter and free the
2119 * proc if appropriate (proc has been released, all threads have
2120 * been released and not currenly in-use to process a transaction).
2122 static void binder_proc_dec_tmpref(struct binder_proc
*proc
)
2124 binder_inner_proc_lock(proc
);
2126 if (proc
->is_dead
&& RB_EMPTY_ROOT(&proc
->threads
) &&
2128 binder_inner_proc_unlock(proc
);
2129 binder_free_proc(proc
);
2132 binder_inner_proc_unlock(proc
);
2136 * binder_get_txn_from() - safely extract the "from" thread in transaction
2137 * @t: binder transaction for t->from
2139 * Atomically return the "from" thread and increment the tmp_ref
2140 * count for the thread to ensure it stays alive until
2141 * binder_thread_dec_tmpref() is called.
2143 * Return: the value of t->from
2145 static struct binder_thread
*binder_get_txn_from(
2146 struct binder_transaction
*t
)
2148 struct binder_thread
*from
;
2150 spin_lock(&t
->lock
);
2153 atomic_inc(&from
->tmp_ref
);
2154 spin_unlock(&t
->lock
);
2159 * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock
2160 * @t: binder transaction for t->from
2162 * Same as binder_get_txn_from() except it also acquires the proc->inner_lock
2163 * to guarantee that the thread cannot be released while operating on it.
2164 * The caller must call binder_inner_proc_unlock() to release the inner lock
2165 * as well as call binder_dec_thread_txn() to release the reference.
2167 * Return: the value of t->from
2169 static struct binder_thread
*binder_get_txn_from_and_acq_inner(
2170 struct binder_transaction
*t
)
2172 struct binder_thread
*from
;
2174 from
= binder_get_txn_from(t
);
2177 binder_inner_proc_lock(from
->proc
);
2179 BUG_ON(from
!= t
->from
);
2182 binder_inner_proc_unlock(from
->proc
);
2183 binder_thread_dec_tmpref(from
);
2187 static void binder_free_transaction(struct binder_transaction
*t
)
2189 struct binder_proc
*target_proc
= t
->to_proc
;
2192 binder_inner_proc_lock(target_proc
);
2194 t
->buffer
->transaction
= NULL
;
2195 binder_inner_proc_unlock(target_proc
);
2198 * If the transaction has no target_proc, then
2199 * t->buffer->transaction has already been cleared.
2202 binder_stats_deleted(BINDER_STAT_TRANSACTION
);
2205 static void binder_send_failed_reply(struct binder_transaction
*t
,
2206 uint32_t error_code
)
2208 struct binder_thread
*target_thread
;
2209 struct binder_transaction
*next
;
2211 BUG_ON(t
->flags
& TF_ONE_WAY
);
2213 target_thread
= binder_get_txn_from_and_acq_inner(t
);
2214 if (target_thread
) {
2215 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
2216 "send failed reply for transaction %d to %d:%d(%s:%s)\n",
2218 target_thread
->proc
->pid
,
2220 target_thread
->proc
->tsk
->comm
,
2221 target_thread
->task
->comm
);
2223 binder_pop_transaction_ilocked(target_thread
, t
);
2224 if (target_thread
->reply_error
.cmd
== BR_OK
) {
2225 target_thread
->reply_error
.cmd
= error_code
;
2226 binder_enqueue_thread_work_ilocked(
2228 &target_thread
->reply_error
.work
);
2229 wake_up_interruptible(&target_thread
->wait
);
2232 * Cannot get here for normal operation, but
2233 * we can if multiple synchronous transactions
2234 * are sent without blocking for responses.
2235 * Just ignore the 2nd error in this case.
2237 pr_warn("Unexpected reply error: %u\n",
2238 target_thread
->reply_error
.cmd
);
2240 binder_inner_proc_unlock(target_thread
->proc
);
2241 binder_thread_dec_tmpref(target_thread
);
2242 binder_free_transaction(t
);
2245 next
= t
->from_parent
;
2247 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
2248 "send failed reply for transaction %d, target dead\n",
2251 binder_free_transaction(t
);
2253 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
2254 "reply failed, no target thread at root\n");
2258 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
2259 "reply failed, no target thread -- retry %d\n",
2265 * binder_cleanup_transaction() - cleans up undelivered transaction
2266 * @t: transaction that needs to be cleaned up
2267 * @reason: reason the transaction wasn't delivered
2268 * @error_code: error to return to caller (if synchronous call)
2270 static void binder_cleanup_transaction(struct binder_transaction
*t
,
2272 uint32_t error_code
)
2274 if (t
->buffer
->target_node
&& !(t
->flags
& TF_ONE_WAY
)) {
2275 binder_send_failed_reply(t
, error_code
);
2277 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
2278 "undelivered transaction %d, %s\n",
2279 t
->debug_id
, reason
);
2280 binder_free_transaction(t
);
2285 * binder_validate_object() - checks for a valid metadata object in a buffer.
2286 * @buffer: binder_buffer that we're parsing.
2287 * @offset: offset in the buffer at which to validate an object.
2289 * Return: If there's a valid metadata object at @offset in @buffer, the
2290 * size of that object. Otherwise, it returns zero.
2292 static size_t binder_validate_object(struct binder_buffer
*buffer
, u64 offset
)
2294 /* Check if we can read a header first */
2295 struct binder_object_header
*hdr
;
2296 size_t object_size
= 0;
2298 if (buffer
->data_size
< sizeof(*hdr
) ||
2299 offset
> buffer
->data_size
- sizeof(*hdr
) ||
2300 !IS_ALIGNED(offset
, sizeof(u32
)))
2303 /* Ok, now see if we can read a complete object. */
2304 hdr
= (struct binder_object_header
*)(buffer
->data
+ offset
);
2305 switch (hdr
->type
) {
2306 case BINDER_TYPE_BINDER
:
2307 case BINDER_TYPE_WEAK_BINDER
:
2308 case BINDER_TYPE_HANDLE
:
2309 case BINDER_TYPE_WEAK_HANDLE
:
2310 object_size
= sizeof(struct flat_binder_object
);
2312 case BINDER_TYPE_FD
:
2313 object_size
= sizeof(struct binder_fd_object
);
2315 case BINDER_TYPE_PTR
:
2316 object_size
= sizeof(struct binder_buffer_object
);
2318 case BINDER_TYPE_FDA
:
2319 object_size
= sizeof(struct binder_fd_array_object
);
2324 if (offset
<= buffer
->data_size
- object_size
&&
2325 buffer
->data_size
>= object_size
)
2332 * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
2333 * @b: binder_buffer containing the object
2334 * @index: index in offset array at which the binder_buffer_object is
2336 * @start: points to the start of the offset array
2337 * @num_valid: the number of valid offsets in the offset array
2339 * Return: If @index is within the valid range of the offset array
2340 * described by @start and @num_valid, and if there's a valid
2341 * binder_buffer_object at the offset found in index @index
2342 * of the offset array, that object is returned. Otherwise,
2343 * %NULL is returned.
2344 * Note that the offset found in index @index itself is not
2345 * verified; this function assumes that @num_valid elements
2346 * from @start were previously verified to have valid offsets.
2348 static struct binder_buffer_object
*binder_validate_ptr(struct binder_buffer
*b
,
2349 binder_size_t index
,
2350 binder_size_t
*start
,
2351 binder_size_t num_valid
)
2353 struct binder_buffer_object
*buffer_obj
;
2354 binder_size_t
*offp
;
2356 if (index
>= num_valid
)
2359 offp
= start
+ index
;
2360 buffer_obj
= (struct binder_buffer_object
*)(b
->data
+ *offp
);
2361 if (buffer_obj
->hdr
.type
!= BINDER_TYPE_PTR
)
2368 * binder_validate_fixup() - validates pointer/fd fixups happen in order.
2369 * @b: transaction buffer
2370 * @objects_start start of objects buffer
2371 * @buffer: binder_buffer_object in which to fix up
2372 * @offset: start offset in @buffer to fix up
2373 * @last_obj: last binder_buffer_object that we fixed up in
2374 * @last_min_offset: minimum fixup offset in @last_obj
2376 * Return: %true if a fixup in buffer @buffer at offset @offset is
2379 * For safety reasons, we only allow fixups inside a buffer to happen
2380 * at increasing offsets; additionally, we only allow fixup on the last
2381 * buffer object that was verified, or one of its parents.
2383 * Example of what is allowed:
2386 * B (parent = A, offset = 0)
2387 * C (parent = A, offset = 16)
2388 * D (parent = C, offset = 0)
2389 * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
2391 * Examples of what is not allowed:
2393 * Decreasing offsets within the same parent:
2395 * C (parent = A, offset = 16)
2396 * B (parent = A, offset = 0) // decreasing offset within A
2398 * Referring to a parent that wasn't the last object or any of its parents:
2400 * B (parent = A, offset = 0)
2401 * C (parent = A, offset = 0)
2402 * C (parent = A, offset = 16)
2403 * D (parent = B, offset = 0) // B is not A or any of A's parents
2405 static bool binder_validate_fixup(struct binder_buffer
*b
,
2406 binder_size_t
*objects_start
,
2407 struct binder_buffer_object
*buffer
,
2408 binder_size_t fixup_offset
,
2409 struct binder_buffer_object
*last_obj
,
2410 binder_size_t last_min_offset
)
2413 /* Nothing to fix up in */
2417 while (last_obj
!= buffer
) {
2419 * Safe to retrieve the parent of last_obj, since it
2420 * was already previously verified by the driver.
2422 if ((last_obj
->flags
& BINDER_BUFFER_FLAG_HAS_PARENT
) == 0)
2424 last_min_offset
= last_obj
->parent_offset
+ sizeof(uintptr_t);
2425 last_obj
= (struct binder_buffer_object
*)
2426 (b
->data
+ *(objects_start
+ last_obj
->parent
));
2428 return (fixup_offset
>= last_min_offset
);
2431 static void binder_transaction_buffer_release(struct binder_proc
*proc
,
2432 struct binder_buffer
*buffer
,
2433 binder_size_t
*failed_at
)
2435 binder_size_t
*offp
, *off_start
, *off_end
;
2436 int debug_id
= buffer
->debug_id
;
2438 binder_debug(BINDER_DEBUG_TRANSACTION
,
2439 "%d(%s) buffer release %d, size %zd-%zd, failed at %pK\n",
2440 proc
->pid
, proc
->tsk
->comm
, buffer
->debug_id
,
2441 buffer
->data_size
, buffer
->offsets_size
, failed_at
);
2443 if (buffer
->target_node
)
2444 binder_dec_node(buffer
->target_node
, 1, 0);
2446 off_start
= (binder_size_t
*)(buffer
->data
+
2447 ALIGN(buffer
->data_size
, sizeof(void *)));
2449 off_end
= failed_at
;
2451 off_end
= (void *)off_start
+ buffer
->offsets_size
;
2452 for (offp
= off_start
; offp
< off_end
; offp
++) {
2453 struct binder_object_header
*hdr
;
2454 size_t object_size
= binder_validate_object(buffer
, *offp
);
2456 if (object_size
== 0) {
2457 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
2458 debug_id
, (u64
)*offp
, buffer
->data_size
);
2461 hdr
= (struct binder_object_header
*)(buffer
->data
+ *offp
);
2462 switch (hdr
->type
) {
2463 case BINDER_TYPE_BINDER
:
2464 case BINDER_TYPE_WEAK_BINDER
: {
2465 struct flat_binder_object
*fp
;
2466 struct binder_node
*node
;
2468 fp
= to_flat_binder_object(hdr
);
2469 node
= binder_get_node(proc
, fp
->binder
);
2471 pr_err("transaction release %d bad node %016llx\n",
2472 debug_id
, (u64
)fp
->binder
);
2475 binder_debug(BINDER_DEBUG_TRANSACTION
,
2476 " node %d u%016llx\n",
2477 node
->debug_id
, (u64
)node
->ptr
);
2478 binder_dec_node(node
, hdr
->type
== BINDER_TYPE_BINDER
,
2480 binder_put_node(node
);
2482 case BINDER_TYPE_HANDLE
:
2483 case BINDER_TYPE_WEAK_HANDLE
: {
2484 struct flat_binder_object
*fp
;
2485 struct binder_ref_data rdata
;
2488 fp
= to_flat_binder_object(hdr
);
2489 ret
= binder_dec_ref_for_handle(proc
, fp
->handle
,
2490 hdr
->type
== BINDER_TYPE_HANDLE
, &rdata
);
2493 pr_err("transaction release %d bad handle %d, ret = %d\n",
2494 debug_id
, fp
->handle
, ret
);
2497 binder_debug(BINDER_DEBUG_TRANSACTION
,
2498 " ref %d desc %d\n",
2499 rdata
.debug_id
, rdata
.desc
);
2502 case BINDER_TYPE_FD
: {
2503 struct binder_fd_object
*fp
= to_binder_fd_object(hdr
);
2505 binder_debug(BINDER_DEBUG_TRANSACTION
,
2506 " fd %d\n", fp
->fd
);
2508 task_close_fd(proc
, fp
->fd
);
2510 case BINDER_TYPE_PTR
:
2512 * Nothing to do here, this will get cleaned up when the
2513 * transaction buffer gets freed
2516 case BINDER_TYPE_FDA
: {
2517 struct binder_fd_array_object
*fda
;
2518 struct binder_buffer_object
*parent
;
2519 uintptr_t parent_buffer
;
2522 binder_size_t fd_buf_size
;
2524 fda
= to_binder_fd_array_object(hdr
);
2525 parent
= binder_validate_ptr(buffer
, fda
->parent
,
2529 pr_err("transaction release %d bad parent offset",
2534 * Since the parent was already fixed up, convert it
2535 * back to kernel address space to access it
2537 parent_buffer
= parent
->buffer
-
2538 binder_alloc_get_user_buffer_offset(
2541 fd_buf_size
= sizeof(u32
) * fda
->num_fds
;
2542 if (fda
->num_fds
>= SIZE_MAX
/ sizeof(u32
)) {
2543 pr_err("transaction release %d invalid number of fds (%lld)\n",
2544 debug_id
, (u64
)fda
->num_fds
);
2547 if (fd_buf_size
> parent
->length
||
2548 fda
->parent_offset
> parent
->length
- fd_buf_size
) {
2549 /* No space for all file descriptors here. */
2550 pr_err("transaction release %d not enough space for %lld fds in buffer\n",
2551 debug_id
, (u64
)fda
->num_fds
);
2554 fd_array
= (u32
*)(parent_buffer
+ (uintptr_t)fda
->parent_offset
);
2555 for (fd_index
= 0; fd_index
< fda
->num_fds
; fd_index
++)
2556 task_close_fd(proc
, fd_array
[fd_index
]);
2559 pr_err("transaction release %d bad object type %x\n",
2560 debug_id
, hdr
->type
);
2566 static int binder_translate_binder(struct flat_binder_object
*fp
,
2567 struct binder_transaction
*t
,
2568 struct binder_thread
*thread
)
2570 struct binder_node
*node
;
2571 struct binder_proc
*proc
= thread
->proc
;
2572 struct binder_proc
*target_proc
= t
->to_proc
;
2573 struct binder_ref_data rdata
;
2576 node
= binder_get_node(proc
, fp
->binder
);
2578 node
= binder_new_node(proc
, fp
);
2582 if (fp
->cookie
!= node
->cookie
) {
2583 binder_user_error("%d:%d(%s:%s) sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
2584 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, (u64
)fp
->binder
,
2585 node
->debug_id
, (u64
)fp
->cookie
,
2590 if (security_binder_transfer_binder(proc
->tsk
, target_proc
->tsk
)) {
2595 ret
= binder_inc_ref_for_node(target_proc
, node
,
2596 fp
->hdr
.type
== BINDER_TYPE_BINDER
,
2597 &thread
->todo
, &rdata
);
2601 if (fp
->hdr
.type
== BINDER_TYPE_BINDER
)
2602 fp
->hdr
.type
= BINDER_TYPE_HANDLE
;
2604 fp
->hdr
.type
= BINDER_TYPE_WEAK_HANDLE
;
2606 fp
->handle
= rdata
.desc
;
2609 trace_binder_transaction_node_to_ref(t
, node
, &rdata
);
2610 binder_debug(BINDER_DEBUG_TRANSACTION
,
2611 " node %d u%016llx -> ref %d desc %d\n",
2612 node
->debug_id
, (u64
)node
->ptr
,
2613 rdata
.debug_id
, rdata
.desc
);
2615 binder_put_node(node
);
2619 static int binder_translate_handle(struct flat_binder_object
*fp
,
2620 struct binder_transaction
*t
,
2621 struct binder_thread
*thread
)
2623 struct binder_proc
*proc
= thread
->proc
;
2624 struct binder_proc
*target_proc
= t
->to_proc
;
2625 struct binder_node
*node
;
2626 struct binder_ref_data src_rdata
;
2629 node
= binder_get_node_from_ref(proc
, fp
->handle
,
2630 fp
->hdr
.type
== BINDER_TYPE_HANDLE
, &src_rdata
);
2632 binder_user_error("%d:%d(%s:%s) got transaction with invalid handle, %d\n",
2633 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, fp
->handle
);
2636 if (security_binder_transfer_binder(proc
->tsk
, target_proc
->tsk
)) {
2641 binder_node_lock(node
);
2642 if (node
->proc
== target_proc
) {
2643 if (fp
->hdr
.type
== BINDER_TYPE_HANDLE
)
2644 fp
->hdr
.type
= BINDER_TYPE_BINDER
;
2646 fp
->hdr
.type
= BINDER_TYPE_WEAK_BINDER
;
2647 fp
->binder
= node
->ptr
;
2648 fp
->cookie
= node
->cookie
;
2650 binder_inner_proc_lock(node
->proc
);
2651 binder_inc_node_nilocked(node
,
2652 fp
->hdr
.type
== BINDER_TYPE_BINDER
,
2655 binder_inner_proc_unlock(node
->proc
);
2656 trace_binder_transaction_ref_to_node(t
, node
, &src_rdata
);
2657 binder_debug(BINDER_DEBUG_TRANSACTION
,
2658 " ref %d desc %d -> node %d u%016llx\n",
2659 src_rdata
.debug_id
, src_rdata
.desc
, node
->debug_id
,
2661 binder_node_unlock(node
);
2663 struct binder_ref_data dest_rdata
;
2665 binder_node_unlock(node
);
2666 ret
= binder_inc_ref_for_node(target_proc
, node
,
2667 fp
->hdr
.type
== BINDER_TYPE_HANDLE
,
2673 fp
->handle
= dest_rdata
.desc
;
2675 trace_binder_transaction_ref_to_ref(t
, node
, &src_rdata
,
2677 binder_debug(BINDER_DEBUG_TRANSACTION
,
2678 " ref %d desc %d -> ref %d desc %d (node %d)\n",
2679 src_rdata
.debug_id
, src_rdata
.desc
,
2680 dest_rdata
.debug_id
, dest_rdata
.desc
,
2684 binder_put_node(node
);
2688 static int binder_translate_fd(int fd
,
2689 struct binder_transaction
*t
,
2690 struct binder_thread
*thread
,
2691 struct binder_transaction
*in_reply_to
)
2693 struct binder_proc
*proc
= thread
->proc
;
2694 struct binder_proc
*target_proc
= t
->to_proc
;
2698 bool target_allows_fd
;
2701 target_allows_fd
= !!(in_reply_to
->flags
& TF_ACCEPT_FDS
);
2703 target_allows_fd
= t
->buffer
->target_node
->accept_fds
;
2704 if (!target_allows_fd
) {
2705 binder_user_error("%d:%d(%s:%s) got %s with fd, %d, but target does not allow fds\n",
2706 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
2707 in_reply_to
? "reply" : "transaction",
2710 goto err_fd_not_accepted
;
2715 binder_user_error("%d:%d(%s:%s) got transaction with invalid fd, %d\n",
2716 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, fd
);
2720 ret
= security_binder_transfer_file(proc
->tsk
, target_proc
->tsk
, file
);
2726 target_fd
= task_get_unused_fd_flags(target_proc
, O_CLOEXEC
);
2727 if (target_fd
< 0) {
2729 goto err_get_unused_fd
;
2731 task_fd_install(target_proc
, target_fd
, file
);
2732 trace_binder_transaction_fd(t
, fd
, target_fd
);
2733 binder_debug(BINDER_DEBUG_TRANSACTION
, " fd %d -> %d\n",
2742 err_fd_not_accepted
:
2746 static int binder_translate_fd_array(struct binder_fd_array_object
*fda
,
2747 struct binder_buffer_object
*parent
,
2748 struct binder_transaction
*t
,
2749 struct binder_thread
*thread
,
2750 struct binder_transaction
*in_reply_to
)
2752 binder_size_t fdi
, fd_buf_size
, num_installed_fds
;
2754 uintptr_t parent_buffer
;
2756 struct binder_proc
*proc
= thread
->proc
;
2757 struct binder_proc
*target_proc
= t
->to_proc
;
2759 fd_buf_size
= sizeof(u32
) * fda
->num_fds
;
2760 if (fda
->num_fds
>= SIZE_MAX
/ sizeof(u32
)) {
2761 binder_user_error("%d:%d(%s:%s) got transaction with invalid number of fds (%lld)\n",
2762 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, (u64
)fda
->num_fds
);
2765 if (fd_buf_size
> parent
->length
||
2766 fda
->parent_offset
> parent
->length
- fd_buf_size
) {
2767 /* No space for all file descriptors here. */
2768 binder_user_error("%d:%d(%s:%s) not enough space to store %lld fds in buffer\n",
2769 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, (u64
)fda
->num_fds
);
2773 * Since the parent was already fixed up, convert it
2774 * back to the kernel address space to access it
2776 parent_buffer
= parent
->buffer
-
2777 binder_alloc_get_user_buffer_offset(&target_proc
->alloc
);
2778 fd_array
= (u32
*)(parent_buffer
+ (uintptr_t)fda
->parent_offset
);
2779 if (!IS_ALIGNED((unsigned long)fd_array
, sizeof(u32
))) {
2780 binder_user_error("%d:%d(%s:%s) parent offset not aligned correctly.\n",
2781 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
2784 for (fdi
= 0; fdi
< fda
->num_fds
; fdi
++) {
2785 target_fd
= binder_translate_fd(fd_array
[fdi
], t
, thread
,
2788 goto err_translate_fd_failed
;
2789 fd_array
[fdi
] = target_fd
;
2793 err_translate_fd_failed
:
2795 * Failed to allocate fd or security error, free fds
2798 num_installed_fds
= fdi
;
2799 for (fdi
= 0; fdi
< num_installed_fds
; fdi
++)
2800 task_close_fd(target_proc
, fd_array
[fdi
]);
2804 static int binder_fixup_parent(struct binder_transaction
*t
,
2805 struct binder_thread
*thread
,
2806 struct binder_buffer_object
*bp
,
2807 binder_size_t
*off_start
,
2808 binder_size_t num_valid
,
2809 struct binder_buffer_object
*last_fixup_obj
,
2810 binder_size_t last_fixup_min_off
)
2812 struct binder_buffer_object
*parent
;
2814 struct binder_buffer
*b
= t
->buffer
;
2815 struct binder_proc
*proc
= thread
->proc
;
2816 struct binder_proc
*target_proc
= t
->to_proc
;
2818 if (!(bp
->flags
& BINDER_BUFFER_FLAG_HAS_PARENT
))
2821 parent
= binder_validate_ptr(b
, bp
->parent
, off_start
, num_valid
);
2823 binder_user_error("%d:%d(%s:%s) got transaction with invalid parent offset or type\n",
2824 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
2828 if (!binder_validate_fixup(b
, off_start
,
2829 parent
, bp
->parent_offset
,
2831 last_fixup_min_off
)) {
2832 binder_user_error("%d:%d(%s:%s) got transaction with out-of-order buffer fixup\n",
2833 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
2837 if (parent
->length
< sizeof(binder_uintptr_t
) ||
2838 bp
->parent_offset
> parent
->length
- sizeof(binder_uintptr_t
)) {
2839 /* No space for a pointer here! */
2840 binder_user_error("%d:%d(%s:%s) got transaction with invalid parent offset\n",
2841 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
2844 parent_buffer
= (u8
*)((uintptr_t)parent
->buffer
-
2845 binder_alloc_get_user_buffer_offset(
2846 &target_proc
->alloc
));
2847 *(binder_uintptr_t
*)(parent_buffer
+ bp
->parent_offset
) = bp
->buffer
;
2853 * binder_proc_transaction() - sends a transaction to a process and wakes it up
2854 * @t: transaction to send
2855 * @proc: process to send the transaction to
2856 * @thread: thread in @proc to send the transaction to (may be NULL)
2858 * This function queues a transaction to the specified process. It will try
2859 * to find a thread in the target process to handle the transaction and
2860 * wake it up. If no thread is found, the work is queued to the proc
2863 * If the @thread parameter is not NULL, the transaction is always queued
2864 * to the waitlist of that specific thread.
2866 * Return: true if the transactions was successfully queued
2867 * false if the target process or thread is dead
2869 static bool binder_proc_transaction(struct binder_transaction
*t
,
2870 struct binder_proc
*proc
,
2871 struct binder_thread
*thread
)
2873 struct binder_node
*node
= t
->buffer
->target_node
;
2874 struct binder_priority node_prio
;
2875 bool oneway
= !!(t
->flags
& TF_ONE_WAY
);
2876 bool pending_async
= false;
2879 binder_node_lock(node
);
2880 node_prio
.prio
= node
->min_priority
;
2881 node_prio
.sched_policy
= node
->sched_policy
;
2885 if (node
->has_async_transaction
) {
2886 pending_async
= true;
2888 node
->has_async_transaction
= true;
2892 binder_inner_proc_lock(proc
);
2894 if (proc
->is_dead
|| (thread
&& thread
->is_dead
)) {
2895 binder_inner_proc_unlock(proc
);
2896 binder_node_unlock(node
);
2900 if (!thread
&& !pending_async
)
2901 thread
= binder_select_thread_ilocked(proc
);
2904 binder_transaction_priority(thread
->task
, t
, node_prio
,
2906 binder_enqueue_thread_work_ilocked(thread
, &t
->work
);
2907 } else if (!pending_async
) {
2908 binder_enqueue_work_ilocked(&t
->work
, &proc
->todo
);
2910 binder_enqueue_work_ilocked(&t
->work
, &node
->async_todo
);
2914 binder_wakeup_thread_ilocked(proc
, thread
, !oneway
/* sync */);
2916 binder_inner_proc_unlock(proc
);
2917 binder_node_unlock(node
);
2923 * binder_get_node_refs_for_txn() - Get required refs on node for txn
2924 * @node: struct binder_node for which to get refs
2925 * @proc: returns @node->proc if valid
2926 * @error: if no @proc then returns BR_DEAD_REPLY
2928 * User-space normally keeps the node alive when creating a transaction
2929 * since it has a reference to the target. The local strong ref keeps it
2930 * alive if the sending process dies before the target process processes
2931 * the transaction. If the source process is malicious or has a reference
2932 * counting bug, relying on the local strong ref can fail.
2934 * Since user-space can cause the local strong ref to go away, we also take
2935 * a tmpref on the node to ensure it survives while we are constructing
2936 * the transaction. We also need a tmpref on the proc while we are
2937 * constructing the transaction, so we take that here as well.
2939 * Return: The target_node with refs taken or NULL if no @node->proc is NULL.
2940 * Also sets @proc if valid. If the @node->proc is NULL indicating that the
2941 * target proc has died, @error is set to BR_DEAD_REPLY
2943 static struct binder_node
*binder_get_node_refs_for_txn(
2944 struct binder_node
*node
,
2945 struct binder_proc
**procp
,
2948 struct binder_node
*target_node
= NULL
;
2950 binder_node_inner_lock(node
);
2953 binder_inc_node_nilocked(node
, 1, 0, NULL
);
2954 binder_inc_node_tmpref_ilocked(node
);
2955 node
->proc
->tmp_ref
++;
2956 *procp
= node
->proc
;
2958 *error
= BR_DEAD_REPLY
;
2959 binder_node_inner_unlock(node
);
2964 static void binder_transaction(struct binder_proc
*proc
,
2965 struct binder_thread
*thread
,
2966 struct binder_transaction_data
*tr
, int reply
,
2967 binder_size_t extra_buffers_size
)
2970 struct binder_transaction
*t
;
2971 struct binder_work
*tcomplete
;
2972 binder_size_t
*offp
, *off_end
, *off_start
;
2973 binder_size_t off_min
;
2974 u8
*sg_bufp
, *sg_buf_end
;
2975 struct binder_proc
*target_proc
= NULL
;
2976 struct binder_thread
*target_thread
= NULL
;
2977 struct binder_node
*target_node
= NULL
;
2978 struct binder_transaction
*in_reply_to
= NULL
;
2979 struct binder_transaction_log_entry
*e
;
2980 uint32_t return_error
= 0;
2981 uint32_t return_error_param
= 0;
2982 uint32_t return_error_line
= 0;
2983 struct binder_buffer_object
*last_fixup_obj
= NULL
;
2984 binder_size_t last_fixup_min_off
= 0;
2985 struct binder_context
*context
= proc
->context
;
2986 int t_debug_id
= atomic_inc_return(&binder_last_id
);
2987 char *secctx
= NULL
;
2990 e
= binder_transaction_log_add(&binder_transaction_log
);
2991 e
->debug_id
= t_debug_id
;
2992 e
->call_type
= reply
? 2 : !!(tr
->flags
& TF_ONE_WAY
);
2993 e
->from_proc
= proc
->pid
;
2994 e
->from_thread
= thread
->pid
;
2995 e
->target_handle
= tr
->target
.handle
;
2996 e
->data_size
= tr
->data_size
;
2997 e
->offsets_size
= tr
->offsets_size
;
2998 e
->context_name
= proc
->context
->name
;
3001 binder_inner_proc_lock(proc
);
3002 in_reply_to
= thread
->transaction_stack
;
3003 if (in_reply_to
== NULL
) {
3004 binder_inner_proc_unlock(proc
);
3005 binder_user_error("%d:%d(%s:%s) got reply transaction with no transaction stack\n",
3006 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3007 return_error
= BR_FAILED_REPLY
;
3008 return_error_param
= -EPROTO
;
3009 return_error_line
= __LINE__
;
3010 goto err_empty_call_stack
;
3012 if (in_reply_to
->to_thread
!= thread
) {
3013 spin_lock(&in_reply_to
->lock
);
3014 binder_user_error("%d:%d(%s:%s) got reply transaction with bad transaction stack, "\
3015 "transaction %d has target %d:%d(%s:%s)\n",
3016 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, in_reply_to
->debug_id
,
3017 in_reply_to
->to_proc
?
3018 in_reply_to
->to_proc
->pid
: 0,
3019 in_reply_to
->to_thread
?
3020 in_reply_to
->to_thread
->pid
: 0,
3021 in_reply_to
->to_proc
?
3022 in_reply_to
->to_proc
->tsk
->comm
: "",
3023 in_reply_to
->to_thread
?
3024 in_reply_to
->to_thread
->task
->comm
: "");
3025 spin_unlock(&in_reply_to
->lock
);
3026 binder_inner_proc_unlock(proc
);
3027 return_error
= BR_FAILED_REPLY
;
3028 return_error_param
= -EPROTO
;
3029 return_error_line
= __LINE__
;
3031 goto err_bad_call_stack
;
3033 thread
->transaction_stack
= in_reply_to
->to_parent
;
3034 binder_inner_proc_unlock(proc
);
3035 target_thread
= binder_get_txn_from_and_acq_inner(in_reply_to
);
3036 if (target_thread
== NULL
) {
3037 return_error
= BR_DEAD_REPLY
;
3038 return_error_line
= __LINE__
;
3039 goto err_dead_binder
;
3041 if (target_thread
->transaction_stack
!= in_reply_to
) {
3042 binder_user_error("%d:%d(%s:%s) got reply transaction with bad target transaction "\
3043 "stack %d, expected %d\n",
3044 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3045 target_thread
->transaction_stack
?
3046 target_thread
->transaction_stack
->debug_id
: 0,
3047 in_reply_to
->debug_id
);
3048 binder_inner_proc_unlock(target_thread
->proc
);
3049 return_error
= BR_FAILED_REPLY
;
3050 return_error_param
= -EPROTO
;
3051 return_error_line
= __LINE__
;
3053 target_thread
= NULL
;
3054 goto err_dead_binder
;
3056 target_proc
= target_thread
->proc
;
3057 target_proc
->tmp_ref
++;
3058 binder_inner_proc_unlock(target_thread
->proc
);
3060 if (tr
->target
.handle
) {
3061 struct binder_ref
*ref
;
3064 * There must already be a strong ref
3065 * on this node. If so, do a strong
3066 * increment on the node to ensure it
3067 * stays alive until the transaction is
3070 binder_proc_lock(proc
);
3071 ref
= binder_get_ref_olocked(proc
, tr
->target
.handle
,
3074 target_node
= binder_get_node_refs_for_txn(
3075 ref
->node
, &target_proc
,
3078 binder_user_error("%d:%d(%s:%s) got transaction to invalid handle\n",
3079 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3080 return_error
= BR_FAILED_REPLY
;
3082 binder_proc_unlock(proc
);
3084 mutex_lock(&context
->context_mgr_node_lock
);
3085 target_node
= context
->binder_context_mgr_node
;
3087 target_node
= binder_get_node_refs_for_txn(
3088 target_node
, &target_proc
,
3091 return_error
= BR_DEAD_REPLY
;
3092 mutex_unlock(&context
->context_mgr_node_lock
);
3093 if (target_node
&& target_proc
== proc
) {
3094 binder_user_error("%d:%d got transaction to context manager from process owning it\n",
3095 proc
->pid
, thread
->pid
);
3096 return_error
= BR_FAILED_REPLY
;
3097 return_error_param
= -EINVAL
;
3098 return_error_line
= __LINE__
;
3099 goto err_invalid_target_handle
;
3104 * return_error is set above
3106 return_error_param
= -EINVAL
;
3107 return_error_line
= __LINE__
;
3108 goto err_dead_binder
;
3110 e
->to_node
= target_node
->debug_id
;
3111 if (security_binder_transaction(proc
->tsk
,
3112 target_proc
->tsk
) < 0) {
3113 return_error
= BR_FAILED_REPLY
;
3114 return_error_param
= -EPERM
;
3115 return_error_line
= __LINE__
;
3116 goto err_invalid_target_handle
;
3118 binder_inner_proc_lock(proc
);
3119 if (!(tr
->flags
& TF_ONE_WAY
) && thread
->transaction_stack
) {
3120 struct binder_transaction
*tmp
;
3122 tmp
= thread
->transaction_stack
;
3123 if (tmp
->to_thread
!= thread
) {
3124 spin_lock(&tmp
->lock
);
3125 binder_user_error("%d:%d(%s:%s) got new transaction with bad transaction stack, "\
3126 "transaction %d has target %d:%d(%s:%s)\n",
3127 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, tmp
->debug_id
,
3128 tmp
->to_proc
? tmp
->to_proc
->pid
: 0,
3130 tmp
->to_thread
->pid
: 0,
3131 tmp
->to_proc
? tmp
->to_proc
->tsk
->comm
: "",
3132 tmp
->to_thread
? tmp
->to_thread
->task
->comm
: "");
3133 spin_unlock(&tmp
->lock
);
3134 binder_inner_proc_unlock(proc
);
3135 return_error
= BR_FAILED_REPLY
;
3136 return_error_param
= -EPROTO
;
3137 return_error_line
= __LINE__
;
3138 goto err_bad_call_stack
;
3141 struct binder_thread
*from
;
3143 spin_lock(&tmp
->lock
);
3145 if (from
&& from
->proc
== target_proc
) {
3146 atomic_inc(&from
->tmp_ref
);
3147 target_thread
= from
;
3148 spin_unlock(&tmp
->lock
);
3151 spin_unlock(&tmp
->lock
);
3152 tmp
= tmp
->from_parent
;
3155 binder_inner_proc_unlock(proc
);
3157 if (target_thread
) {
3158 e
->to_thread
= target_thread
->pid
;
3160 e
->to_proc
= target_proc
->pid
;
3162 /* TODO: reuse incoming transaction for reply */
3163 t
= kzalloc(sizeof(*t
), GFP_KERNEL
);
3165 return_error
= BR_FAILED_REPLY
;
3166 return_error_param
= -ENOMEM
;
3167 return_error_line
= __LINE__
;
3168 goto err_alloc_t_failed
;
3170 binder_stats_created(BINDER_STAT_TRANSACTION
);
3171 spin_lock_init(&t
->lock
);
3173 tcomplete
= kzalloc(sizeof(*tcomplete
), GFP_KERNEL
);
3174 if (tcomplete
== NULL
) {
3175 return_error
= BR_FAILED_REPLY
;
3176 return_error_param
= -ENOMEM
;
3177 return_error_line
= __LINE__
;
3178 goto err_alloc_tcomplete_failed
;
3180 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE
);
3182 t
->debug_id
= t_debug_id
;
3185 binder_debug(BINDER_DEBUG_TRANSACTION
,
3186 "%d:%d(%s:%s) BC_REPLY %d -> %d:%d (%s:%s), data %016llx-%016llx size %lld-%lld-%lld\n",
3187 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, t
->debug_id
,
3188 target_proc
->pid
, target_thread
->pid
, target_proc
->tsk
->comm
, target_thread
->task
->comm
,
3189 (u64
)tr
->data
.ptr
.buffer
,
3190 (u64
)tr
->data
.ptr
.offsets
,
3191 (u64
)tr
->data_size
, (u64
)tr
->offsets_size
,
3192 (u64
)extra_buffers_size
);
3194 binder_debug(BINDER_DEBUG_TRANSACTION
,
3195 "%d:%d(%s:%s) BC_TRANSACTION %d -> %d (%s) - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
3196 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, t
->debug_id
,
3197 target_proc
->pid
, target_proc
->tsk
->comm
, target_node
->debug_id
,
3198 (u64
)tr
->data
.ptr
.buffer
,
3199 (u64
)tr
->data
.ptr
.offsets
,
3200 (u64
)tr
->data_size
, (u64
)tr
->offsets_size
,
3201 (u64
)extra_buffers_size
);
3203 if (!reply
&& !(tr
->flags
& TF_ONE_WAY
))
3207 t
->sender_euid
= task_euid(proc
->tsk
);
3208 t
->to_proc
= target_proc
;
3209 t
->to_thread
= target_thread
;
3211 t
->flags
= tr
->flags
;
3212 if (!(t
->flags
& TF_ONE_WAY
) &&
3213 binder_supported_policy(current
->policy
)) {
3214 /* Inherit supported policies for synchronous transactions */
3215 t
->priority
.sched_policy
= current
->policy
;
3216 t
->priority
.prio
= current
->normal_prio
;
3218 /* Otherwise, fall back to the default priority */
3219 t
->priority
= target_proc
->default_priority
;
3222 if (target_node
&& target_node
->txn_security_ctx
) {
3226 security_task_getsecid(proc
->tsk
, &secid
);
3227 ret
= security_secid_to_secctx(secid
, &secctx
, &secctx_sz
);
3229 return_error
= BR_FAILED_REPLY
;
3230 return_error_param
= ret
;
3231 return_error_line
= __LINE__
;
3232 goto err_get_secctx_failed
;
3234 added_size
= ALIGN(secctx_sz
, sizeof(u64
));
3235 extra_buffers_size
+= added_size
;
3236 if (extra_buffers_size
< added_size
) {
3237 /* integer overflow of extra_buffers_size */
3238 return_error
= BR_FAILED_REPLY
;
3239 return_error_param
= EINVAL
;
3240 return_error_line
= __LINE__
;
3241 goto err_bad_extra_size
;
3245 #ifdef CONFIG_DEBUG_SNAPSHOT_BINDER
3246 dss_binder_transaction(reply
, t
, t
->from
? t
->from
: thread
, target_node
? target_node
->debug_id
: 0);
3248 trace_binder_transaction(reply
, t
, target_node
);
3250 t
->buffer
= binder_alloc_new_buf(&target_proc
->alloc
, tr
->data_size
,
3251 tr
->offsets_size
, extra_buffers_size
,
3252 !reply
&& (t
->flags
& TF_ONE_WAY
));
3253 if (IS_ERR(t
->buffer
)) {
3255 * -ESRCH indicates VMA cleared. The target is dying.
3257 return_error_param
= PTR_ERR(t
->buffer
);
3258 return_error
= return_error_param
== -ESRCH
?
3259 BR_DEAD_REPLY
: BR_FAILED_REPLY
;
3260 return_error_line
= __LINE__
;
3262 goto err_binder_alloc_buf_failed
;
3265 size_t buf_offset
= ALIGN(tr
->data_size
, sizeof(void *)) +
3266 ALIGN(tr
->offsets_size
, sizeof(void *)) +
3267 ALIGN(extra_buffers_size
, sizeof(void *)) -
3268 ALIGN(secctx_sz
, sizeof(u64
));
3269 char *kptr
= t
->buffer
->data
+ buf_offset
;
3271 t
->security_ctx
= (uintptr_t)kptr
+
3272 binder_alloc_get_user_buffer_offset(&target_proc
->alloc
);
3273 memcpy(kptr
, secctx
, secctx_sz
);
3274 security_release_secctx(secctx
, secctx_sz
);
3277 t
->buffer
->debug_id
= t
->debug_id
;
3278 t
->buffer
->transaction
= t
;
3279 t
->buffer
->target_node
= target_node
;
3280 trace_binder_transaction_alloc_buf(t
->buffer
);
3281 off_start
= (binder_size_t
*)(t
->buffer
->data
+
3282 ALIGN(tr
->data_size
, sizeof(void *)));
3285 if (copy_from_user(t
->buffer
->data
, (const void __user
*)(uintptr_t)
3286 tr
->data
.ptr
.buffer
, tr
->data_size
)) {
3287 binder_user_error("%d:%d(%s:%s) got transaction with invalid data ptr\n",
3288 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3289 return_error
= BR_FAILED_REPLY
;
3290 return_error_param
= -EFAULT
;
3291 return_error_line
= __LINE__
;
3292 goto err_copy_data_failed
;
3294 if (copy_from_user(offp
, (const void __user
*)(uintptr_t)
3295 tr
->data
.ptr
.offsets
, tr
->offsets_size
)) {
3296 binder_user_error("%d:%d(%s:%s) got transaction with invalid offsets ptr\n",
3297 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3298 return_error
= BR_FAILED_REPLY
;
3299 return_error_param
= -EFAULT
;
3300 return_error_line
= __LINE__
;
3301 goto err_copy_data_failed
;
3303 if (!IS_ALIGNED(tr
->offsets_size
, sizeof(binder_size_t
))) {
3304 binder_user_error("%d:%d(%s:%s) got transaction with invalid offsets size, %lld\n",
3305 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, (u64
)tr
->offsets_size
);
3306 return_error
= BR_FAILED_REPLY
;
3307 return_error_param
= -EINVAL
;
3308 return_error_line
= __LINE__
;
3309 goto err_bad_offset
;
3311 if (!IS_ALIGNED(extra_buffers_size
, sizeof(u64
))) {
3312 binder_user_error("%d:%d(%s:%s) got transaction with unaligned buffers size, %lld\n",
3313 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3314 (u64
)extra_buffers_size
);
3315 return_error
= BR_FAILED_REPLY
;
3316 return_error_param
= -EINVAL
;
3317 return_error_line
= __LINE__
;
3318 goto err_bad_offset
;
3320 off_end
= (void *)off_start
+ tr
->offsets_size
;
3321 sg_bufp
= (u8
*)(PTR_ALIGN(off_end
, sizeof(void *)));
3322 sg_buf_end
= sg_bufp
+ extra_buffers_size
-
3323 ALIGN(secctx_sz
, sizeof(u64
));
3325 for (; offp
< off_end
; offp
++) {
3326 struct binder_object_header
*hdr
;
3327 size_t object_size
= binder_validate_object(t
->buffer
, *offp
);
3329 if (object_size
== 0 || *offp
< off_min
) {
3330 binder_user_error("%d:%d(%s:%s) got transaction with invalid offset (%lld, min %lld max %lld)"\
3332 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3335 (u64
)t
->buffer
->data_size
);
3336 return_error
= BR_FAILED_REPLY
;
3337 return_error_param
= -EINVAL
;
3338 return_error_line
= __LINE__
;
3339 goto err_bad_offset
;
3342 hdr
= (struct binder_object_header
*)(t
->buffer
->data
+ *offp
);
3343 off_min
= *offp
+ object_size
;
3344 switch (hdr
->type
) {
3345 case BINDER_TYPE_BINDER
:
3346 case BINDER_TYPE_WEAK_BINDER
: {
3347 struct flat_binder_object
*fp
;
3349 fp
= to_flat_binder_object(hdr
);
3350 ret
= binder_translate_binder(fp
, t
, thread
);
3352 return_error
= BR_FAILED_REPLY
;
3353 return_error_param
= ret
;
3354 return_error_line
= __LINE__
;
3355 goto err_translate_failed
;
3358 case BINDER_TYPE_HANDLE
:
3359 case BINDER_TYPE_WEAK_HANDLE
: {
3360 struct flat_binder_object
*fp
;
3362 fp
= to_flat_binder_object(hdr
);
3363 ret
= binder_translate_handle(fp
, t
, thread
);
3365 return_error
= BR_FAILED_REPLY
;
3366 return_error_param
= ret
;
3367 return_error_line
= __LINE__
;
3368 goto err_translate_failed
;
3372 case BINDER_TYPE_FD
: {
3373 struct binder_fd_object
*fp
= to_binder_fd_object(hdr
);
3374 int target_fd
= binder_translate_fd(fp
->fd
, t
, thread
,
3377 if (target_fd
< 0) {
3378 return_error
= BR_FAILED_REPLY
;
3379 return_error_param
= target_fd
;
3380 return_error_line
= __LINE__
;
3381 goto err_translate_failed
;
3386 case BINDER_TYPE_FDA
: {
3387 struct binder_fd_array_object
*fda
=
3388 to_binder_fd_array_object(hdr
);
3389 struct binder_buffer_object
*parent
=
3390 binder_validate_ptr(t
->buffer
, fda
->parent
,
3394 binder_user_error("%d:%d(%s:%s) got transaction with invalid parent offset or type\n",
3395 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3396 return_error
= BR_FAILED_REPLY
;
3397 return_error_param
= -EINVAL
;
3398 return_error_line
= __LINE__
;
3399 goto err_bad_parent
;
3401 if (!binder_validate_fixup(t
->buffer
, off_start
,
3402 parent
, fda
->parent_offset
,
3404 last_fixup_min_off
)) {
3405 binder_user_error("%d:%d(%s:%s) got transaction with out-of-order buffer fixup\n",
3406 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3407 return_error
= BR_FAILED_REPLY
;
3408 return_error_param
= -EINVAL
;
3409 return_error_line
= __LINE__
;
3410 goto err_bad_parent
;
3412 ret
= binder_translate_fd_array(fda
, parent
, t
, thread
,
3415 return_error
= BR_FAILED_REPLY
;
3416 return_error_param
= ret
;
3417 return_error_line
= __LINE__
;
3418 goto err_translate_failed
;
3420 last_fixup_obj
= parent
;
3421 last_fixup_min_off
=
3422 fda
->parent_offset
+ sizeof(u32
) * fda
->num_fds
;
3424 case BINDER_TYPE_PTR
: {
3425 struct binder_buffer_object
*bp
=
3426 to_binder_buffer_object(hdr
);
3427 size_t buf_left
= sg_buf_end
- sg_bufp
;
3429 if (bp
->length
> buf_left
) {
3430 binder_user_error("%d:%d(%s:%s) got transaction with too large buffer\n",
3431 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3432 return_error
= BR_FAILED_REPLY
;
3433 return_error_param
= -EINVAL
;
3434 return_error_line
= __LINE__
;
3435 goto err_bad_offset
;
3437 if (copy_from_user(sg_bufp
,
3438 (const void __user
*)(uintptr_t)
3439 bp
->buffer
, bp
->length
)) {
3440 binder_user_error("%d:%d(%s:%s) got transaction with invalid offsets ptr\n",
3441 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3442 return_error_param
= -EFAULT
;
3443 return_error
= BR_FAILED_REPLY
;
3444 return_error_line
= __LINE__
;
3445 goto err_copy_data_failed
;
3447 /* Fixup buffer pointer to target proc address space */
3448 bp
->buffer
= (uintptr_t)sg_bufp
+
3449 binder_alloc_get_user_buffer_offset(
3450 &target_proc
->alloc
);
3451 sg_bufp
+= ALIGN(bp
->length
, sizeof(u64
));
3453 ret
= binder_fixup_parent(t
, thread
, bp
, off_start
,
3456 last_fixup_min_off
);
3458 return_error
= BR_FAILED_REPLY
;
3459 return_error_param
= ret
;
3460 return_error_line
= __LINE__
;
3461 goto err_translate_failed
;
3463 last_fixup_obj
= bp
;
3464 last_fixup_min_off
= 0;
3467 binder_user_error("%d:%d(%s:%s) got transaction with invalid object type, %x\n",
3468 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, hdr
->type
);
3469 return_error
= BR_FAILED_REPLY
;
3470 return_error_param
= -EINVAL
;
3471 return_error_line
= __LINE__
;
3472 goto err_bad_object_type
;
3475 tcomplete
->type
= BINDER_WORK_TRANSACTION_COMPLETE
;
3476 t
->work
.type
= BINDER_WORK_TRANSACTION
;
3479 binder_enqueue_thread_work(thread
, tcomplete
);
3480 binder_inner_proc_lock(target_proc
);
3481 if (target_thread
->is_dead
) {
3482 binder_inner_proc_unlock(target_proc
);
3483 goto err_dead_proc_or_thread
;
3485 BUG_ON(t
->buffer
->async_transaction
!= 0);
3486 binder_pop_transaction_ilocked(target_thread
, in_reply_to
);
3487 binder_enqueue_thread_work_ilocked(target_thread
, &t
->work
);
3488 binder_inner_proc_unlock(target_proc
);
3489 wake_up_interruptible_sync(&target_thread
->wait
);
3490 binder_restore_priority(current
, in_reply_to
->saved_priority
);
3491 binder_free_transaction(in_reply_to
);
3492 } else if (!(t
->flags
& TF_ONE_WAY
)) {
3493 BUG_ON(t
->buffer
->async_transaction
!= 0);
3494 binder_inner_proc_lock(proc
);
3496 * Defer the TRANSACTION_COMPLETE, so we don't return to
3497 * userspace immediately; this allows the target process to
3498 * immediately start processing this transaction, reducing
3499 * latency. We will then return the TRANSACTION_COMPLETE when
3500 * the target replies (or there is an error).
3502 binder_enqueue_deferred_thread_work_ilocked(thread
, tcomplete
);
3504 t
->from_parent
= thread
->transaction_stack
;
3505 thread
->transaction_stack
= t
;
3506 binder_inner_proc_unlock(proc
);
3507 if (!binder_proc_transaction(t
, target_proc
, target_thread
)) {
3508 binder_inner_proc_lock(proc
);
3509 binder_pop_transaction_ilocked(thread
, t
);
3510 binder_inner_proc_unlock(proc
);
3511 goto err_dead_proc_or_thread
;
3514 BUG_ON(target_node
== NULL
);
3515 BUG_ON(t
->buffer
->async_transaction
!= 1);
3516 binder_enqueue_thread_work(thread
, tcomplete
);
3517 if (!binder_proc_transaction(t
, target_proc
, NULL
))
3518 goto err_dead_proc_or_thread
;
3521 binder_thread_dec_tmpref(target_thread
);
3522 binder_proc_dec_tmpref(target_proc
);
3524 binder_dec_node_tmpref(target_node
);
3526 * write barrier to synchronize with initialization
3530 WRITE_ONCE(e
->debug_id_done
, t_debug_id
);
3533 err_dead_proc_or_thread
:
3534 return_error
= BR_DEAD_REPLY
;
3535 return_error_line
= __LINE__
;
3536 binder_dequeue_work(proc
, tcomplete
);
3537 err_translate_failed
:
3538 err_bad_object_type
:
3541 err_copy_data_failed
:
3542 trace_binder_transaction_failed_buffer_release(t
->buffer
);
3543 binder_transaction_buffer_release(target_proc
, t
->buffer
, offp
);
3545 binder_dec_node_tmpref(target_node
);
3547 t
->buffer
->transaction
= NULL
;
3548 binder_alloc_free_buf(&target_proc
->alloc
, t
->buffer
);
3549 err_binder_alloc_buf_failed
:
3552 security_release_secctx(secctx
, secctx_sz
);
3553 err_get_secctx_failed
:
3555 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE
);
3556 err_alloc_tcomplete_failed
:
3558 binder_stats_deleted(BINDER_STAT_TRANSACTION
);
3561 err_empty_call_stack
:
3563 err_invalid_target_handle
:
3565 binder_thread_dec_tmpref(target_thread
);
3567 binder_proc_dec_tmpref(target_proc
);
3569 binder_dec_node(target_node
, 1, 0);
3570 binder_dec_node_tmpref(target_node
);
3573 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
3574 "%d:%d(%s:%s) transaction failed %d/%d, size %lld-%lld line %d\n",
3575 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3576 return_error
, return_error_param
,
3577 (u64
)tr
->data_size
, (u64
)tr
->offsets_size
,
3581 struct binder_transaction_log_entry
*fe
;
3583 e
->return_error
= return_error
;
3584 e
->return_error_param
= return_error_param
;
3585 e
->return_error_line
= return_error_line
;
3586 #ifdef CONFIG_DEBUG_SNAPSHOT_BINDER
3587 dss_binder_transaction_failed(reply
, e
, proc
->tsk
->comm
, thread
->task
->comm
, tr
->flags
, tr
->code
);
3589 fe
= binder_transaction_log_add(&binder_transaction_log_failed
);
3592 * write barrier to synchronize with initialization
3596 WRITE_ONCE(e
->debug_id_done
, t_debug_id
);
3597 WRITE_ONCE(fe
->debug_id_done
, t_debug_id
);
3600 BUG_ON(thread
->return_error
.cmd
!= BR_OK
);
3602 binder_restore_priority(current
, in_reply_to
->saved_priority
);
3603 thread
->return_error
.cmd
= BR_TRANSACTION_COMPLETE
;
3604 binder_enqueue_thread_work(thread
, &thread
->return_error
.work
);
3605 binder_send_failed_reply(in_reply_to
, return_error
);
3607 thread
->return_error
.cmd
= return_error
;
3608 binder_enqueue_thread_work(thread
, &thread
->return_error
.work
);
3612 static int binder_thread_write(struct binder_proc
*proc
,
3613 struct binder_thread
*thread
,
3614 binder_uintptr_t binder_buffer
, size_t size
,
3615 binder_size_t
*consumed
)
3618 struct binder_context
*context
= proc
->context
;
3619 void __user
*buffer
= (void __user
*)(uintptr_t)binder_buffer
;
3620 void __user
*ptr
= buffer
+ *consumed
;
3621 void __user
*end
= buffer
+ size
;
3623 while (ptr
< end
&& thread
->return_error
.cmd
== BR_OK
) {
3626 if (get_user(cmd
, (uint32_t __user
*)ptr
))
3628 ptr
+= sizeof(uint32_t);
3629 trace_binder_command(cmd
);
3630 if (_IOC_NR(cmd
) < ARRAY_SIZE(binder_stats
.bc
)) {
3631 atomic_inc(&binder_stats
.bc
[_IOC_NR(cmd
)]);
3632 atomic_inc(&proc
->stats
.bc
[_IOC_NR(cmd
)]);
3633 atomic_inc(&thread
->stats
.bc
[_IOC_NR(cmd
)]);
3641 const char *debug_string
;
3642 bool strong
= cmd
== BC_ACQUIRE
|| cmd
== BC_RELEASE
;
3643 bool increment
= cmd
== BC_INCREFS
|| cmd
== BC_ACQUIRE
;
3644 struct binder_ref_data rdata
;
3646 if (get_user(target
, (uint32_t __user
*)ptr
))
3649 ptr
+= sizeof(uint32_t);
3651 if (increment
&& !target
) {
3652 struct binder_node
*ctx_mgr_node
;
3653 mutex_lock(&context
->context_mgr_node_lock
);
3654 ctx_mgr_node
= context
->binder_context_mgr_node
;
3656 ret
= binder_inc_ref_for_node(
3658 strong
, NULL
, &rdata
);
3659 mutex_unlock(&context
->context_mgr_node_lock
);
3662 ret
= binder_update_ref_for_handle(
3663 proc
, target
, increment
, strong
,
3665 if (!ret
&& rdata
.desc
!= target
) {
3666 binder_user_error("%d:%d(%s:%s) tried to acquire reference to"\
3667 " desc %d, got %d instead\n",
3668 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3669 target
, rdata
.desc
);
3673 debug_string
= "IncRefs";
3676 debug_string
= "Acquire";
3679 debug_string
= "Release";
3683 debug_string
= "DecRefs";
3687 binder_user_error("%d:%d(%s:%s) %s %d refcount change on invalid ref %d ret %d\n",
3688 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3689 debug_string
, strong
, target
, ret
);
3692 binder_debug(BINDER_DEBUG_USER_REFS
,
3693 "%d:%d(%s:%s) %s ref %d desc %d s %d w %d\n",
3694 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3695 debug_string
, rdata
.debug_id
, rdata
.desc
, rdata
.strong
,
3699 case BC_INCREFS_DONE
:
3700 case BC_ACQUIRE_DONE
: {
3701 binder_uintptr_t node_ptr
;
3702 binder_uintptr_t cookie
;
3703 struct binder_node
*node
;
3706 if (get_user(node_ptr
, (binder_uintptr_t __user
*)ptr
))
3708 ptr
+= sizeof(binder_uintptr_t
);
3709 if (get_user(cookie
, (binder_uintptr_t __user
*)ptr
))
3711 ptr
+= sizeof(binder_uintptr_t
);
3712 node
= binder_get_node(proc
, node_ptr
);
3714 binder_user_error("%d:%d(%s:%s) %s u%016llx no match\n",
3715 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3716 cmd
== BC_INCREFS_DONE
?
3722 if (cookie
!= node
->cookie
) {
3723 binder_user_error("%d:%d(%s:%s) %s u%016llx node %d cookie mismatch"\
3724 " %016llx != %016llx\n",
3725 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3726 cmd
== BC_INCREFS_DONE
?
3727 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
3728 (u64
)node_ptr
, node
->debug_id
,
3729 (u64
)cookie
, (u64
)node
->cookie
);
3730 binder_put_node(node
);
3733 binder_node_inner_lock(node
);
3734 if (cmd
== BC_ACQUIRE_DONE
) {
3735 if (node
->pending_strong_ref
== 0) {
3736 binder_user_error("%d:%d(%s:%s) BC_ACQUIRE_DONE node %d"\
3737 " has no pending acquire request\n",
3738 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3740 binder_node_inner_unlock(node
);
3741 binder_put_node(node
);
3744 node
->pending_strong_ref
= 0;
3746 if (node
->pending_weak_ref
== 0) {
3747 binder_user_error("%d:%d(%s:%s) BC_INCREFS_DONE node %d"\
3748 " has no pending increfs request\n",
3749 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3751 binder_node_inner_unlock(node
);
3752 binder_put_node(node
);
3755 node
->pending_weak_ref
= 0;
3757 free_node
= binder_dec_node_nilocked(node
,
3758 cmd
== BC_ACQUIRE_DONE
, 0);
3760 binder_debug(BINDER_DEBUG_USER_REFS
,
3761 "%d:%d(%s:%s) %s node %d ls %d lw %d tr %d\n",
3762 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3763 cmd
== BC_INCREFS_DONE
? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
3764 node
->debug_id
, node
->local_strong_refs
,
3765 node
->local_weak_refs
, node
->tmp_refs
);
3766 binder_node_inner_unlock(node
);
3767 binder_put_node(node
);
3770 case BC_ATTEMPT_ACQUIRE
:
3771 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
3773 case BC_ACQUIRE_RESULT
:
3774 pr_err("BC_ACQUIRE_RESULT not supported\n");
3777 case BC_FREE_BUFFER
: {
3778 binder_uintptr_t data_ptr
;
3779 struct binder_buffer
*buffer
;
3781 if (get_user(data_ptr
, (binder_uintptr_t __user
*)ptr
))
3783 ptr
+= sizeof(binder_uintptr_t
);
3785 buffer
= binder_alloc_prepare_to_free(&proc
->alloc
,
3787 if (IS_ERR_OR_NULL(buffer
)) {
3788 if (PTR_ERR(buffer
) == -EPERM
) {
3790 "%d:%d(%s:%s) BC_FREE_BUFFER u%016llx matched unreturned or currently freeing buffer\n",
3791 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3795 "%d:%d(%s:%s) BC_FREE_BUFFER u%016llx no match\n",
3796 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3801 binder_debug(BINDER_DEBUG_FREE_BUFFER
,
3802 "%d:%d(%s:%s) BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
3803 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, (u64
)data_ptr
,
3805 buffer
->transaction
? "active" : "finished");
3807 binder_inner_proc_lock(proc
);
3808 if (buffer
->transaction
) {
3809 buffer
->transaction
->buffer
= NULL
;
3810 buffer
->transaction
= NULL
;
3812 binder_inner_proc_unlock(proc
);
3813 if (buffer
->async_transaction
&& buffer
->target_node
) {
3814 struct binder_node
*buf_node
;
3815 struct binder_work
*w
;
3817 buf_node
= buffer
->target_node
;
3818 binder_node_inner_lock(buf_node
);
3819 BUG_ON(!buf_node
->has_async_transaction
);
3820 BUG_ON(buf_node
->proc
!= proc
);
3821 w
= binder_dequeue_work_head_ilocked(
3822 &buf_node
->async_todo
);
3824 buf_node
->has_async_transaction
= false;
3826 binder_enqueue_work_ilocked(
3828 binder_wakeup_proc_ilocked(proc
);
3830 binder_node_inner_unlock(buf_node
);
3832 trace_binder_transaction_buffer_release(buffer
);
3833 binder_transaction_buffer_release(proc
, buffer
, NULL
);
3834 binder_alloc_free_buf(&proc
->alloc
, buffer
);
3838 case BC_TRANSACTION_SG
:
3840 struct binder_transaction_data_sg tr
;
3842 if (copy_from_user(&tr
, ptr
, sizeof(tr
)))
3845 binder_transaction(proc
, thread
, &tr
.transaction_data
,
3846 cmd
== BC_REPLY_SG
, tr
.buffers_size
);
3849 case BC_TRANSACTION
:
3851 struct binder_transaction_data tr
;
3853 if (copy_from_user(&tr
, ptr
, sizeof(tr
)))
3856 binder_transaction(proc
, thread
, &tr
,
3857 cmd
== BC_REPLY
, 0);
3861 case BC_REGISTER_LOOPER
:
3862 binder_debug(BINDER_DEBUG_THREADS
,
3863 "%d:%d(%s:%s) BC_REGISTER_LOOPER\n",
3864 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3865 binder_inner_proc_lock(proc
);
3866 if (thread
->looper
& BINDER_LOOPER_STATE_ENTERED
) {
3867 thread
->looper
|= BINDER_LOOPER_STATE_INVALID
;
3868 binder_user_error("%d:%d(%s:%s) ERROR: BC_REGISTER_LOOPER"\
3869 " called after BC_ENTER_LOOPER\n",
3870 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3871 } else if (proc
->requested_threads
== 0) {
3872 thread
->looper
|= BINDER_LOOPER_STATE_INVALID
;
3873 binder_user_error("%d:%d(%s:%s) ERROR: BC_REGISTER_LOOPER called without request\n",
3874 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3876 proc
->requested_threads
--;
3877 proc
->requested_threads_started
++;
3879 thread
->looper
|= BINDER_LOOPER_STATE_REGISTERED
;
3880 binder_inner_proc_unlock(proc
);
3882 case BC_ENTER_LOOPER
:
3883 binder_debug(BINDER_DEBUG_THREADS
,
3884 "%d:%d(%s:%s) BC_ENTER_LOOPER\n",
3885 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3886 if (thread
->looper
& BINDER_LOOPER_STATE_REGISTERED
) {
3887 thread
->looper
|= BINDER_LOOPER_STATE_INVALID
;
3888 binder_user_error("%d:%d(%s:%s) ERROR: BC_ENTER_LOOPER"\
3889 " called after BC_REGISTER_LOOPER\n",
3890 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3892 thread
->looper
|= BINDER_LOOPER_STATE_ENTERED
;
3894 case BC_EXIT_LOOPER
:
3895 binder_debug(BINDER_DEBUG_THREADS
,
3896 "%d:%d(%s:%s) BC_EXIT_LOOPER\n",
3897 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3898 thread
->looper
|= BINDER_LOOPER_STATE_EXITED
;
3901 case BC_REQUEST_DEATH_NOTIFICATION
:
3902 case BC_CLEAR_DEATH_NOTIFICATION
: {
3904 binder_uintptr_t cookie
;
3905 struct binder_ref
*ref
;
3906 struct binder_ref_death
*death
= NULL
;
3908 if (get_user(target
, (uint32_t __user
*)ptr
))
3910 ptr
+= sizeof(uint32_t);
3911 if (get_user(cookie
, (binder_uintptr_t __user
*)ptr
))
3913 ptr
+= sizeof(binder_uintptr_t
);
3914 if (cmd
== BC_REQUEST_DEATH_NOTIFICATION
) {
3916 * Allocate memory for death notification
3917 * before taking lock
3919 death
= kzalloc(sizeof(*death
), GFP_KERNEL
);
3920 if (death
== NULL
) {
3921 WARN_ON(thread
->return_error
.cmd
!=
3923 thread
->return_error
.cmd
= BR_ERROR
;
3924 binder_enqueue_thread_work(
3926 &thread
->return_error
.work
);
3928 BINDER_DEBUG_FAILED_TRANSACTION
,
3929 "%d:%d(%s:%s) BC_REQUEST_DEATH_NOTIFICATION failed\n",
3930 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3934 binder_proc_lock(proc
);
3935 ref
= binder_get_ref_olocked(proc
, target
, false);
3937 binder_user_error("%d:%d(%s:%s) %s invalid ref %d\n",
3938 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3939 cmd
== BC_REQUEST_DEATH_NOTIFICATION
?
3940 "BC_REQUEST_DEATH_NOTIFICATION" :
3941 "BC_CLEAR_DEATH_NOTIFICATION",
3943 binder_proc_unlock(proc
);
3948 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION
,
3949 "%d:%d(%s:%s) %s %016llx ref %d desc %d s %d w %d for node %d\n",
3950 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3951 cmd
== BC_REQUEST_DEATH_NOTIFICATION
?
3952 "BC_REQUEST_DEATH_NOTIFICATION" :
3953 "BC_CLEAR_DEATH_NOTIFICATION",
3954 (u64
)cookie
, ref
->data
.debug_id
,
3955 ref
->data
.desc
, ref
->data
.strong
,
3956 ref
->data
.weak
, ref
->node
->debug_id
);
3958 binder_node_lock(ref
->node
);
3959 if (cmd
== BC_REQUEST_DEATH_NOTIFICATION
) {
3961 binder_user_error("%d:%d(%s:%s) BC_REQUEST_DEATH_NOTIFICATION"\
3962 " death notification already set\n",
3963 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3964 binder_node_unlock(ref
->node
);
3965 binder_proc_unlock(proc
);
3969 binder_stats_created(BINDER_STAT_DEATH
);
3970 INIT_LIST_HEAD(&death
->work
.entry
);
3971 death
->cookie
= cookie
;
3973 if (ref
->node
->proc
== NULL
) {
3974 ref
->death
->work
.type
= BINDER_WORK_DEAD_BINDER
;
3976 binder_inner_proc_lock(proc
);
3977 binder_enqueue_work_ilocked(
3978 &ref
->death
->work
, &proc
->todo
);
3979 binder_wakeup_proc_ilocked(proc
);
3980 binder_inner_proc_unlock(proc
);
3983 if (ref
->death
== NULL
) {
3984 binder_user_error("%d:%d(%s:%s) BC_CLEAR_DEATH_NOTIFICATION"\
3985 " death notification not active\n",
3986 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3987 binder_node_unlock(ref
->node
);
3988 binder_proc_unlock(proc
);
3992 if (death
->cookie
!= cookie
) {
3993 binder_user_error("%d:%d(%s:%s) BC_CLEAR_DEATH_NOTIFICATION"\
3994 " death notification cookie mismatch %016llx != %016llx\n",
3995 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3998 binder_node_unlock(ref
->node
);
3999 binder_proc_unlock(proc
);
4003 binder_inner_proc_lock(proc
);
4004 if (list_empty(&death
->work
.entry
)) {
4005 death
->work
.type
= BINDER_WORK_CLEAR_DEATH_NOTIFICATION
;
4006 if (thread
->looper
&
4007 (BINDER_LOOPER_STATE_REGISTERED
|
4008 BINDER_LOOPER_STATE_ENTERED
))
4009 binder_enqueue_thread_work_ilocked(
4013 binder_enqueue_work_ilocked(
4016 binder_wakeup_proc_ilocked(
4020 BUG_ON(death
->work
.type
!= BINDER_WORK_DEAD_BINDER
);
4021 death
->work
.type
= BINDER_WORK_DEAD_BINDER_AND_CLEAR
;
4023 binder_inner_proc_unlock(proc
);
4025 binder_node_unlock(ref
->node
);
4026 binder_proc_unlock(proc
);
4028 case BC_DEAD_BINDER_DONE
: {
4029 struct binder_work
*w
;
4030 binder_uintptr_t cookie
;
4031 struct binder_ref_death
*death
= NULL
;
4033 if (get_user(cookie
, (binder_uintptr_t __user
*)ptr
))
4036 ptr
+= sizeof(cookie
);
4037 binder_inner_proc_lock(proc
);
4038 list_for_each_entry(w
, &proc
->delivered_death
,
4040 struct binder_ref_death
*tmp_death
=
4042 struct binder_ref_death
,
4045 if (tmp_death
->cookie
== cookie
) {
4050 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
4051 "%d:%d(%s:%s) BC_DEAD_BINDER_DONE %016llx found %pK\n",
4052 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, (u64
)cookie
,
4054 if (death
== NULL
) {
4055 binder_user_error("%d:%d(%s:%s) BC_DEAD_BINDER_DONE %016llx not found\n",
4056 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, (u64
)cookie
);
4057 binder_inner_proc_unlock(proc
);
4060 binder_dequeue_work_ilocked(&death
->work
);
4061 if (death
->work
.type
== BINDER_WORK_DEAD_BINDER_AND_CLEAR
) {
4062 death
->work
.type
= BINDER_WORK_CLEAR_DEATH_NOTIFICATION
;
4063 if (thread
->looper
&
4064 (BINDER_LOOPER_STATE_REGISTERED
|
4065 BINDER_LOOPER_STATE_ENTERED
))
4066 binder_enqueue_thread_work_ilocked(
4067 thread
, &death
->work
);
4069 binder_enqueue_work_ilocked(
4072 binder_wakeup_proc_ilocked(proc
);
4075 binder_inner_proc_unlock(proc
);
4079 pr_err("%d:%d(%s:%s) unknown command %d\n",
4080 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, cmd
);
4083 *consumed
= ptr
- buffer
;
4088 static void binder_stat_br(struct binder_proc
*proc
,
4089 struct binder_thread
*thread
, uint32_t cmd
)
4091 trace_binder_return(cmd
);
4092 if (_IOC_NR(cmd
) < ARRAY_SIZE(binder_stats
.br
)) {
4093 atomic_inc(&binder_stats
.br
[_IOC_NR(cmd
)]);
4094 atomic_inc(&proc
->stats
.br
[_IOC_NR(cmd
)]);
4095 atomic_inc(&thread
->stats
.br
[_IOC_NR(cmd
)]);
4099 static int binder_put_node_cmd(struct binder_proc
*proc
,
4100 struct binder_thread
*thread
,
4102 binder_uintptr_t node_ptr
,
4103 binder_uintptr_t node_cookie
,
4105 uint32_t cmd
, const char *cmd_name
)
4107 void __user
*ptr
= *ptrp
;
4109 if (put_user(cmd
, (uint32_t __user
*)ptr
))
4111 ptr
+= sizeof(uint32_t);
4113 if (put_user(node_ptr
, (binder_uintptr_t __user
*)ptr
))
4115 ptr
+= sizeof(binder_uintptr_t
);
4117 if (put_user(node_cookie
, (binder_uintptr_t __user
*)ptr
))
4119 ptr
+= sizeof(binder_uintptr_t
);
4121 binder_stat_br(proc
, thread
, cmd
);
4122 binder_debug(BINDER_DEBUG_USER_REFS
, "%d:%d(%s:%s) %s %d u%016llx c%016llx\n",
4123 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
4124 cmd_name
, node_debug_id
,
4125 (u64
)node_ptr
, (u64
)node_cookie
);
4131 static int binder_wait_for_work(struct binder_thread
*thread
,
4135 struct binder_proc
*proc
= thread
->proc
;
4138 freezer_do_not_count();
4139 binder_inner_proc_lock(proc
);
4141 prepare_to_wait(&thread
->wait
, &wait
, TASK_INTERRUPTIBLE
);
4142 if (binder_has_work_ilocked(thread
, do_proc_work
))
4145 list_add(&thread
->waiting_thread_node
,
4146 &proc
->waiting_threads
);
4147 binder_inner_proc_unlock(proc
);
4149 binder_inner_proc_lock(proc
);
4150 list_del_init(&thread
->waiting_thread_node
);
4151 if (signal_pending(current
)) {
4156 finish_wait(&thread
->wait
, &wait
);
4157 binder_inner_proc_unlock(proc
);
4163 static int binder_thread_read(struct binder_proc
*proc
,
4164 struct binder_thread
*thread
,
4165 binder_uintptr_t binder_buffer
, size_t size
,
4166 binder_size_t
*consumed
, int non_block
)
4168 void __user
*buffer
= (void __user
*)(uintptr_t)binder_buffer
;
4169 void __user
*ptr
= buffer
+ *consumed
;
4170 void __user
*end
= buffer
+ size
;
4173 int wait_for_proc_work
;
4175 if (*consumed
== 0) {
4176 if (put_user(BR_NOOP
, (uint32_t __user
*)ptr
))
4178 ptr
+= sizeof(uint32_t);
4182 binder_inner_proc_lock(proc
);
4183 wait_for_proc_work
= binder_available_for_proc_work_ilocked(thread
);
4184 binder_inner_proc_unlock(proc
);
4186 thread
->looper
|= BINDER_LOOPER_STATE_WAITING
;
4188 trace_binder_wait_for_work(wait_for_proc_work
,
4189 !!thread
->transaction_stack
,
4190 !binder_worklist_empty(proc
, &thread
->todo
));
4191 if (wait_for_proc_work
) {
4192 if (!(thread
->looper
& (BINDER_LOOPER_STATE_REGISTERED
|
4193 BINDER_LOOPER_STATE_ENTERED
))) {
4194 binder_user_error("%d:%d(%s:%s) ERROR: Thread waiting for process work"\
4195 " before calling BC_REGISTER_LOOPER or BC_ENTER_LOOPER (state %x)\n",
4196 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, thread
->looper
);
4197 wait_event_interruptible(binder_user_error_wait
,
4198 binder_stop_on_user_error
< 2);
4200 binder_restore_priority(current
, proc
->default_priority
);
4204 if (!binder_has_work(thread
, wait_for_proc_work
))
4207 ret
= binder_wait_for_work(thread
, wait_for_proc_work
);
4210 thread
->looper
&= ~BINDER_LOOPER_STATE_WAITING
;
4217 struct binder_transaction_data_secctx tr
;
4218 struct binder_transaction_data
*trd
= &tr
.transaction_data
;
4219 struct binder_work
*w
= NULL
;
4220 struct list_head
*list
= NULL
;
4221 struct binder_transaction
*t
= NULL
;
4222 struct binder_thread
*t_from
;
4223 size_t trsize
= sizeof(*trd
);
4225 binder_inner_proc_lock(proc
);
4226 if (!binder_worklist_empty_ilocked(&thread
->todo
))
4227 list
= &thread
->todo
;
4228 else if (!binder_worklist_empty_ilocked(&proc
->todo
) &&
4232 binder_inner_proc_unlock(proc
);
4235 if (ptr
- buffer
== 4 && !thread
->looper_need_return
)
4240 if (end
- ptr
< sizeof(tr
) + 4) {
4241 binder_inner_proc_unlock(proc
);
4244 w
= binder_dequeue_work_head_ilocked(list
);
4245 if (binder_worklist_empty_ilocked(&thread
->todo
))
4246 thread
->process_todo
= false;
4249 case BINDER_WORK_TRANSACTION
: {
4250 binder_inner_proc_unlock(proc
);
4251 t
= container_of(w
, struct binder_transaction
, work
);
4253 case BINDER_WORK_RETURN_ERROR
: {
4254 struct binder_error
*e
= container_of(
4255 w
, struct binder_error
, work
);
4257 WARN_ON(e
->cmd
== BR_OK
);
4258 binder_inner_proc_unlock(proc
);
4259 if (put_user(e
->cmd
, (uint32_t __user
*)ptr
))
4263 ptr
+= sizeof(uint32_t);
4265 binder_stat_br(proc
, thread
, e
->cmd
);
4267 case BINDER_WORK_TRANSACTION_COMPLETE
: {
4268 binder_inner_proc_unlock(proc
);
4269 cmd
= BR_TRANSACTION_COMPLETE
;
4270 if (put_user(cmd
, (uint32_t __user
*)ptr
))
4272 ptr
+= sizeof(uint32_t);
4274 binder_stat_br(proc
, thread
, cmd
);
4275 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE
,
4276 "%d:%d(%s:%s) BR_TRANSACTION_COMPLETE\n",
4277 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
4279 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE
);
4281 case BINDER_WORK_NODE
: {
4282 struct binder_node
*node
= container_of(w
, struct binder_node
, work
);
4284 binder_uintptr_t node_ptr
= node
->ptr
;
4285 binder_uintptr_t node_cookie
= node
->cookie
;
4286 int node_debug_id
= node
->debug_id
;
4289 void __user
*orig_ptr
= ptr
;
4291 BUG_ON(proc
!= node
->proc
);
4292 strong
= node
->internal_strong_refs
||
4293 node
->local_strong_refs
;
4294 weak
= !hlist_empty(&node
->refs
) ||
4295 node
->local_weak_refs
||
4296 node
->tmp_refs
|| strong
;
4297 has_strong_ref
= node
->has_strong_ref
;
4298 has_weak_ref
= node
->has_weak_ref
;
4300 if (weak
&& !has_weak_ref
) {
4301 node
->has_weak_ref
= 1;
4302 node
->pending_weak_ref
= 1;
4303 node
->local_weak_refs
++;
4305 if (strong
&& !has_strong_ref
) {
4306 node
->has_strong_ref
= 1;
4307 node
->pending_strong_ref
= 1;
4308 node
->local_strong_refs
++;
4310 if (!strong
&& has_strong_ref
)
4311 node
->has_strong_ref
= 0;
4312 if (!weak
&& has_weak_ref
)
4313 node
->has_weak_ref
= 0;
4314 if (!weak
&& !strong
) {
4315 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
4316 "%d:%d(%s:%s) node %d u%016llx c%016llx deleted\n",
4317 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
4321 rb_erase(&node
->rb_node
, &proc
->nodes
);
4322 binder_inner_proc_unlock(proc
);
4323 binder_node_lock(node
);
4325 * Acquire the node lock before freeing the
4326 * node to serialize with other threads that
4327 * may have been holding the node lock while
4328 * decrementing this node (avoids race where
4329 * this thread frees while the other thread
4330 * is unlocking the node after the final
4333 binder_node_unlock(node
);
4334 binder_free_node(node
);
4336 binder_inner_proc_unlock(proc
);
4338 if (weak
&& !has_weak_ref
)
4339 ret
= binder_put_node_cmd(
4340 proc
, thread
, &ptr
, node_ptr
,
4341 node_cookie
, node_debug_id
,
4342 BR_INCREFS
, "BR_INCREFS");
4343 if (!ret
&& strong
&& !has_strong_ref
)
4344 ret
= binder_put_node_cmd(
4345 proc
, thread
, &ptr
, node_ptr
,
4346 node_cookie
, node_debug_id
,
4347 BR_ACQUIRE
, "BR_ACQUIRE");
4348 if (!ret
&& !strong
&& has_strong_ref
)
4349 ret
= binder_put_node_cmd(
4350 proc
, thread
, &ptr
, node_ptr
,
4351 node_cookie
, node_debug_id
,
4352 BR_RELEASE
, "BR_RELEASE");
4353 if (!ret
&& !weak
&& has_weak_ref
)
4354 ret
= binder_put_node_cmd(
4355 proc
, thread
, &ptr
, node_ptr
,
4356 node_cookie
, node_debug_id
,
4357 BR_DECREFS
, "BR_DECREFS");
4358 if (orig_ptr
== ptr
)
4359 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
4360 "%d:%d(%s:%s) node %d u%016llx c%016llx state unchanged\n",
4361 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
4368 case BINDER_WORK_DEAD_BINDER
:
4369 case BINDER_WORK_DEAD_BINDER_AND_CLEAR
:
4370 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION
: {
4371 struct binder_ref_death
*death
;
4373 binder_uintptr_t cookie
;
4375 death
= container_of(w
, struct binder_ref_death
, work
);
4376 if (w
->type
== BINDER_WORK_CLEAR_DEATH_NOTIFICATION
)
4377 cmd
= BR_CLEAR_DEATH_NOTIFICATION_DONE
;
4379 cmd
= BR_DEAD_BINDER
;
4380 cookie
= death
->cookie
;
4382 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION
,
4383 "%d:%d(%s:%s) %s %016llx\n",
4384 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
4385 cmd
== BR_DEAD_BINDER
?
4387 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
4389 if (w
->type
== BINDER_WORK_CLEAR_DEATH_NOTIFICATION
) {
4390 binder_inner_proc_unlock(proc
);
4392 binder_stats_deleted(BINDER_STAT_DEATH
);
4394 binder_enqueue_work_ilocked(
4395 w
, &proc
->delivered_death
);
4396 binder_inner_proc_unlock(proc
);
4398 if (put_user(cmd
, (uint32_t __user
*)ptr
))
4400 ptr
+= sizeof(uint32_t);
4401 if (put_user(cookie
,
4402 (binder_uintptr_t __user
*)ptr
))
4404 ptr
+= sizeof(binder_uintptr_t
);
4405 binder_stat_br(proc
, thread
, cmd
);
4406 if (cmd
== BR_DEAD_BINDER
)
4407 goto done
; /* DEAD_BINDER notifications can cause transactions */
4414 BUG_ON(t
->buffer
== NULL
);
4415 if (t
->buffer
->target_node
) {
4416 struct binder_node
*target_node
= t
->buffer
->target_node
;
4417 struct binder_priority node_prio
;
4419 trd
->target
.ptr
= target_node
->ptr
;
4420 trd
->cookie
= target_node
->cookie
;
4421 node_prio
.sched_policy
= target_node
->sched_policy
;
4422 node_prio
.prio
= target_node
->min_priority
;
4423 binder_transaction_priority(current
, t
, node_prio
,
4424 target_node
->inherit_rt
);
4425 cmd
= BR_TRANSACTION
;
4427 trd
->target
.ptr
= 0;
4431 trd
->code
= t
->code
;
4432 trd
->flags
= t
->flags
;
4433 trd
->sender_euid
= from_kuid(current_user_ns(), t
->sender_euid
);
4435 t_from
= binder_get_txn_from(t
);
4437 struct task_struct
*sender
= t_from
->proc
->tsk
;
4440 task_tgid_nr_ns(sender
,
4441 task_active_pid_ns(current
));
4443 trd
->sender_pid
= 0;
4446 trd
->data_size
= t
->buffer
->data_size
;
4447 trd
->offsets_size
= t
->buffer
->offsets_size
;
4448 trd
->data
.ptr
.buffer
= (binder_uintptr_t
)
4449 ((uintptr_t)t
->buffer
->data
+
4450 binder_alloc_get_user_buffer_offset(&proc
->alloc
));
4451 trd
->data
.ptr
.offsets
= trd
->data
.ptr
.buffer
+
4452 ALIGN(t
->buffer
->data_size
,
4455 tr
.secctx
= t
->security_ctx
;
4456 if (t
->security_ctx
) {
4457 cmd
= BR_TRANSACTION_SEC_CTX
;
4458 trsize
= sizeof(tr
);
4460 if (put_user(cmd
, (uint32_t __user
*)ptr
)) {
4462 binder_thread_dec_tmpref(t_from
);
4464 binder_cleanup_transaction(t
, "put_user failed",
4469 ptr
+= sizeof(uint32_t);
4470 if (copy_to_user(ptr
, &tr
, trsize
)) {
4472 binder_thread_dec_tmpref(t_from
);
4474 binder_cleanup_transaction(t
, "copy_to_user failed",
4481 #ifdef CONFIG_DEBUG_SNAPSHOT_BINDER
4482 dss_binder_transaction_received(t
, thread
);
4484 trace_binder_transaction_received(t
);
4485 binder_stat_br(proc
, thread
, cmd
);
4486 binder_debug(BINDER_DEBUG_TRANSACTION
,
4487 "%d:%d(%s:%s) %s %d %d:%d(%s:%s), cmd %d size %zd-%zd ptr %016llx-%016llx\n",
4488 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
4489 (cmd
== BR_TRANSACTION
) ? "BR_TRANSACTION" :
4490 (cmd
== BR_TRANSACTION_SEC_CTX
) ?
4491 "BR_TRANSACTION_SEC_CTX" : "BR_REPLY",
4492 t
->debug_id
, t_from
? t_from
->proc
->pid
: 0,
4493 t_from
? t_from
->pid
: 0,
4494 t_from
? t_from
->proc
->tsk
->comm
: "",
4495 t_from
? t_from
->task
->comm
: "",
4497 t
->buffer
->data_size
, t
->buffer
->offsets_size
,
4498 (u64
)trd
->data
.ptr
.buffer
,
4499 (u64
)trd
->data
.ptr
.offsets
);
4502 binder_thread_dec_tmpref(t_from
);
4503 t
->buffer
->allow_user_free
= 1;
4504 if (cmd
!= BR_REPLY
&& !(t
->flags
& TF_ONE_WAY
)) {
4505 binder_inner_proc_lock(thread
->proc
);
4506 t
->to_parent
= thread
->transaction_stack
;
4507 t
->to_thread
= thread
;
4508 thread
->transaction_stack
= t
;
4509 binder_inner_proc_unlock(thread
->proc
);
4511 binder_free_transaction(t
);
4518 *consumed
= ptr
- buffer
;
4519 binder_inner_proc_lock(proc
);
4520 if (proc
->requested_threads
== 0 &&
4521 list_empty(&thread
->proc
->waiting_threads
) &&
4522 proc
->requested_threads_started
< proc
->max_threads
&&
4523 (thread
->looper
& (BINDER_LOOPER_STATE_REGISTERED
|
4524 BINDER_LOOPER_STATE_ENTERED
)) /* the user-space code fails to */
4525 /*spawn a new thread if we leave this out */) {
4526 proc
->requested_threads
++;
4527 binder_inner_proc_unlock(proc
);
4528 binder_debug(BINDER_DEBUG_THREADS
,
4529 "%d:%d(%s:%s) BR_SPAWN_LOOPER\n",
4530 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
4531 if (put_user(BR_SPAWN_LOOPER
, (uint32_t __user
*)buffer
))
4533 binder_stat_br(proc
, thread
, BR_SPAWN_LOOPER
);
4535 binder_inner_proc_unlock(proc
);
4539 static void binder_release_work(struct binder_proc
*proc
,
4540 struct list_head
*list
)
4542 struct binder_work
*w
;
4543 enum binder_work_type wtype
;
4546 binder_inner_proc_lock(proc
);
4547 w
= binder_dequeue_work_head_ilocked(list
);
4548 wtype
= w
? w
->type
: 0;
4549 binder_inner_proc_unlock(proc
);
4554 case BINDER_WORK_TRANSACTION
: {
4555 struct binder_transaction
*t
;
4557 t
= container_of(w
, struct binder_transaction
, work
);
4559 binder_cleanup_transaction(t
, "process died.",
4562 case BINDER_WORK_RETURN_ERROR
: {
4563 struct binder_error
*e
= container_of(
4564 w
, struct binder_error
, work
);
4566 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
4567 "undelivered TRANSACTION_ERROR: %u\n",
4570 case BINDER_WORK_TRANSACTION_COMPLETE
: {
4571 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
4572 "undelivered TRANSACTION_COMPLETE\n");
4574 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE
);
4576 case BINDER_WORK_DEAD_BINDER_AND_CLEAR
:
4577 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION
: {
4578 struct binder_ref_death
*death
;
4580 death
= container_of(w
, struct binder_ref_death
, work
);
4581 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
4582 "undelivered death notification, %016llx\n",
4583 (u64
)death
->cookie
);
4585 binder_stats_deleted(BINDER_STAT_DEATH
);
4587 case BINDER_WORK_NODE
:
4590 pr_err("unexpected work type, %d, not freed\n",
4598 static struct binder_thread
*binder_get_thread_ilocked(
4599 struct binder_proc
*proc
, struct binder_thread
*new_thread
)
4601 struct binder_thread
*thread
= NULL
;
4602 struct rb_node
*parent
= NULL
;
4603 struct rb_node
**p
= &proc
->threads
.rb_node
;
4607 thread
= rb_entry(parent
, struct binder_thread
, rb_node
);
4609 if (current
->pid
< thread
->pid
)
4611 else if (current
->pid
> thread
->pid
)
4612 p
= &(*p
)->rb_right
;
4618 thread
= new_thread
;
4619 binder_stats_created(BINDER_STAT_THREAD
);
4620 thread
->proc
= proc
;
4621 thread
->pid
= current
->pid
;
4622 get_task_struct(current
);
4623 thread
->task
= current
;
4624 atomic_set(&thread
->tmp_ref
, 0);
4625 init_waitqueue_head(&thread
->wait
);
4626 INIT_LIST_HEAD(&thread
->todo
);
4627 rb_link_node(&thread
->rb_node
, parent
, p
);
4628 rb_insert_color(&thread
->rb_node
, &proc
->threads
);
4629 thread
->looper_need_return
= true;
4630 thread
->return_error
.work
.type
= BINDER_WORK_RETURN_ERROR
;
4631 thread
->return_error
.cmd
= BR_OK
;
4632 thread
->reply_error
.work
.type
= BINDER_WORK_RETURN_ERROR
;
4633 thread
->reply_error
.cmd
= BR_OK
;
4634 INIT_LIST_HEAD(&new_thread
->waiting_thread_node
);
4638 static struct binder_thread
*binder_get_thread(struct binder_proc
*proc
)
4640 struct binder_thread
*thread
;
4641 struct binder_thread
*new_thread
;
4643 binder_inner_proc_lock(proc
);
4644 thread
= binder_get_thread_ilocked(proc
, NULL
);
4645 binder_inner_proc_unlock(proc
);
4647 new_thread
= kzalloc(sizeof(*thread
), GFP_KERNEL
);
4648 if (new_thread
== NULL
)
4650 binder_inner_proc_lock(proc
);
4651 thread
= binder_get_thread_ilocked(proc
, new_thread
);
4652 binder_inner_proc_unlock(proc
);
4653 if (thread
!= new_thread
)
4659 static void binder_free_proc(struct binder_proc
*proc
)
4661 BUG_ON(!list_empty(&proc
->todo
));
4662 BUG_ON(!list_empty(&proc
->delivered_death
));
4663 binder_alloc_deferred_release(&proc
->alloc
);
4664 put_task_struct(proc
->tsk
);
4665 binder_stats_deleted(BINDER_STAT_PROC
);
4669 static void binder_free_thread(struct binder_thread
*thread
)
4671 BUG_ON(!list_empty(&thread
->todo
));
4672 binder_stats_deleted(BINDER_STAT_THREAD
);
4673 binder_proc_dec_tmpref(thread
->proc
);
4674 put_task_struct(thread
->task
);
4678 static int binder_thread_release(struct binder_proc
*proc
,
4679 struct binder_thread
*thread
)
4681 struct binder_transaction
*t
;
4682 struct binder_transaction
*send_reply
= NULL
;
4683 int active_transactions
= 0;
4684 struct binder_transaction
*last_t
= NULL
;
4686 binder_inner_proc_lock(thread
->proc
);
4688 * take a ref on the proc so it survives
4689 * after we remove this thread from proc->threads.
4690 * The corresponding dec is when we actually
4691 * free the thread in binder_free_thread()
4695 * take a ref on this thread to ensure it
4696 * survives while we are releasing it
4698 atomic_inc(&thread
->tmp_ref
);
4699 rb_erase(&thread
->rb_node
, &proc
->threads
);
4700 t
= thread
->transaction_stack
;
4702 spin_lock(&t
->lock
);
4703 if (t
->to_thread
== thread
)
4706 thread
->is_dead
= true;
4710 active_transactions
++;
4711 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
4712 "release %d:%d(%s:%s) transaction %d %s, still active\n",
4713 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
4715 (t
->to_thread
== thread
) ? "in" : "out");
4717 if (t
->to_thread
== thread
) {
4719 t
->to_thread
= NULL
;
4721 t
->buffer
->transaction
= NULL
;
4725 } else if (t
->from
== thread
) {
4730 spin_unlock(&last_t
->lock
);
4732 spin_lock(&t
->lock
);
4736 * If this thread used poll, make sure we remove the waitqueue
4737 * from any epoll data structures holding it with POLLFREE.
4738 * waitqueue_active() is safe to use here because we're holding
4741 if ((thread
->looper
& BINDER_LOOPER_STATE_POLL
) &&
4742 waitqueue_active(&thread
->wait
)) {
4743 wake_up_poll(&thread
->wait
, POLLHUP
| POLLFREE
);
4746 binder_inner_proc_unlock(thread
->proc
);
4749 * This is needed to avoid races between wake_up_poll() above and
4750 * and ep_remove_waitqueue() called for other reasons (eg the epoll file
4751 * descriptor being closed); ep_remove_waitqueue() holds an RCU read
4752 * lock, so we can be sure it's done after calling synchronize_rcu().
4754 if (thread
->looper
& BINDER_LOOPER_STATE_POLL
)
4758 binder_send_failed_reply(send_reply
, BR_DEAD_REPLY
);
4759 binder_release_work(proc
, &thread
->todo
);
4760 binder_thread_dec_tmpref(thread
);
4761 return active_transactions
;
4764 static unsigned int binder_poll(struct file
*filp
,
4765 struct poll_table_struct
*wait
)
4767 struct binder_proc
*proc
= filp
->private_data
;
4768 struct binder_thread
*thread
= NULL
;
4769 bool wait_for_proc_work
;
4771 thread
= binder_get_thread(proc
);
4775 binder_inner_proc_lock(thread
->proc
);
4776 thread
->looper
|= BINDER_LOOPER_STATE_POLL
;
4777 wait_for_proc_work
= binder_available_for_proc_work_ilocked(thread
);
4779 binder_inner_proc_unlock(thread
->proc
);
4781 poll_wait(filp
, &thread
->wait
, wait
);
4783 if (binder_has_work(thread
, wait_for_proc_work
))
4789 static int binder_ioctl_write_read(struct file
*filp
,
4790 unsigned int cmd
, unsigned long arg
,
4791 struct binder_thread
*thread
)
4794 struct binder_proc
*proc
= filp
->private_data
;
4795 unsigned int size
= _IOC_SIZE(cmd
);
4796 void __user
*ubuf
= (void __user
*)arg
;
4797 struct binder_write_read bwr
;
4799 if (size
!= sizeof(struct binder_write_read
)) {
4803 if (copy_from_user(&bwr
, ubuf
, sizeof(bwr
))) {
4807 binder_debug(BINDER_DEBUG_READ_WRITE
,
4808 "%d:%d(%s:%s) write %lld at %016llx, read %lld at %016llx\n",
4809 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
4810 (u64
)bwr
.write_size
, (u64
)bwr
.write_buffer
,
4811 (u64
)bwr
.read_size
, (u64
)bwr
.read_buffer
);
4813 if (bwr
.write_size
> 0) {
4814 ret
= binder_thread_write(proc
, thread
,
4817 &bwr
.write_consumed
);
4818 trace_binder_write_done(ret
);
4820 bwr
.read_consumed
= 0;
4821 if (copy_to_user(ubuf
, &bwr
, sizeof(bwr
)))
4826 if (bwr
.read_size
> 0) {
4827 ret
= binder_thread_read(proc
, thread
, bwr
.read_buffer
,
4830 filp
->f_flags
& O_NONBLOCK
);
4831 trace_binder_read_done(ret
);
4832 binder_inner_proc_lock(proc
);
4833 if (!binder_worklist_empty_ilocked(&proc
->todo
))
4834 binder_wakeup_proc_ilocked(proc
);
4835 binder_inner_proc_unlock(proc
);
4837 if (copy_to_user(ubuf
, &bwr
, sizeof(bwr
)))
4842 binder_debug(BINDER_DEBUG_READ_WRITE
,
4843 "%d:%d(%s:%s) wrote %lld of %lld, read return %lld of %lld\n",
4844 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
4845 (u64
)bwr
.write_consumed
, (u64
)bwr
.write_size
,
4846 (u64
)bwr
.read_consumed
, (u64
)bwr
.read_size
);
4847 if (copy_to_user(ubuf
, &bwr
, sizeof(bwr
))) {
4855 static int binder_ioctl_set_ctx_mgr(struct file
*filp
,
4856 struct flat_binder_object
*fbo
)
4859 struct binder_proc
*proc
= filp
->private_data
;
4860 struct binder_context
*context
= proc
->context
;
4861 struct binder_node
*new_node
;
4862 kuid_t curr_euid
= current_euid();
4864 mutex_lock(&context
->context_mgr_node_lock
);
4865 if (context
->binder_context_mgr_node
) {
4866 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
4870 ret
= security_binder_set_context_mgr(proc
->tsk
);
4873 if (uid_valid(context
->binder_context_mgr_uid
)) {
4874 if (!uid_eq(context
->binder_context_mgr_uid
, curr_euid
)) {
4875 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
4876 from_kuid(&init_user_ns
, curr_euid
),
4877 from_kuid(&init_user_ns
,
4878 context
->binder_context_mgr_uid
));
4883 context
->binder_context_mgr_uid
= curr_euid
;
4885 new_node
= binder_new_node(proc
, fbo
);
4890 binder_node_lock(new_node
);
4891 new_node
->local_weak_refs
++;
4892 new_node
->local_strong_refs
++;
4893 new_node
->has_strong_ref
= 1;
4894 new_node
->has_weak_ref
= 1;
4895 context
->binder_context_mgr_node
= new_node
;
4896 binder_node_unlock(new_node
);
4897 binder_put_node(new_node
);
4899 mutex_unlock(&context
->context_mgr_node_lock
);
4903 static int binder_ioctl_get_node_debug_info(struct binder_proc
*proc
,
4904 struct binder_node_debug_info
*info
)
4907 binder_uintptr_t ptr
= info
->ptr
;
4909 memset(info
, 0, sizeof(*info
));
4911 binder_inner_proc_lock(proc
);
4912 for (n
= rb_first(&proc
->nodes
); n
!= NULL
; n
= rb_next(n
)) {
4913 struct binder_node
*node
= rb_entry(n
, struct binder_node
,
4915 if (node
->ptr
> ptr
) {
4916 info
->ptr
= node
->ptr
;
4917 info
->cookie
= node
->cookie
;
4918 info
->has_strong_ref
= node
->has_strong_ref
;
4919 info
->has_weak_ref
= node
->has_weak_ref
;
4923 binder_inner_proc_unlock(proc
);
4928 static long binder_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
4931 struct binder_proc
*proc
= filp
->private_data
;
4932 struct binder_thread
*thread
;
4933 unsigned int size
= _IOC_SIZE(cmd
);
4934 void __user
*ubuf
= (void __user
*)arg
;
4936 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
4937 proc->pid, current->pid, cmd, arg);*/
4939 binder_selftest_alloc(&proc
->alloc
);
4941 trace_binder_ioctl(cmd
, arg
);
4943 ret
= wait_event_interruptible(binder_user_error_wait
, binder_stop_on_user_error
< 2);
4947 thread
= binder_get_thread(proc
);
4948 if (thread
== NULL
) {
4954 case BINDER_WRITE_READ
:
4955 ret
= binder_ioctl_write_read(filp
, cmd
, arg
, thread
);
4959 case BINDER_SET_MAX_THREADS
: {
4962 if (copy_from_user(&max_threads
, ubuf
,
4963 sizeof(max_threads
))) {
4967 binder_inner_proc_lock(proc
);
4968 proc
->max_threads
= max_threads
;
4969 binder_inner_proc_unlock(proc
);
4972 case BINDER_SET_CONTEXT_MGR_EXT
: {
4973 struct flat_binder_object fbo
;
4975 if (copy_from_user(&fbo
, ubuf
, sizeof(fbo
))) {
4979 ret
= binder_ioctl_set_ctx_mgr(filp
, &fbo
);
4984 case BINDER_SET_CONTEXT_MGR
:
4985 ret
= binder_ioctl_set_ctx_mgr(filp
, NULL
);
4989 case BINDER_THREAD_EXIT
:
4990 binder_debug(BINDER_DEBUG_THREADS
, "%d:%d(%s:%s) exit\n",
4991 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
4992 binder_thread_release(proc
, thread
);
4995 case BINDER_VERSION
: {
4996 struct binder_version __user
*ver
= ubuf
;
4998 if (size
!= sizeof(struct binder_version
)) {
5002 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION
,
5003 &ver
->protocol_version
)) {
5009 case BINDER_GET_NODE_DEBUG_INFO
: {
5010 struct binder_node_debug_info info
;
5012 if (copy_from_user(&info
, ubuf
, sizeof(info
))) {
5017 ret
= binder_ioctl_get_node_debug_info(proc
, &info
);
5021 if (copy_to_user(ubuf
, &info
, sizeof(info
))) {
5034 thread
->looper_need_return
= false;
5035 wait_event_interruptible(binder_user_error_wait
, binder_stop_on_user_error
< 2);
5036 if (ret
&& ret
!= -ERESTARTSYS
)
5037 pr_info("%d:%d(%s:%s) ioctl %x %lx returned %d\n", proc
->pid
, current
->pid
,
5038 proc
->tsk
->comm
, current
->comm
, cmd
, arg
, ret
);
5040 trace_binder_ioctl_done(ret
);
5044 static void binder_vma_open(struct vm_area_struct
*vma
)
5046 struct binder_proc
*proc
= vma
->vm_private_data
;
5048 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
5049 "%d(%s) open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
5050 proc
->pid
, proc
->tsk
->comm
, vma
->vm_start
, vma
->vm_end
,
5051 (vma
->vm_end
- vma
->vm_start
) / SZ_1K
, vma
->vm_flags
,
5052 (unsigned long)pgprot_val(vma
->vm_page_prot
));
5055 static void binder_vma_close(struct vm_area_struct
*vma
)
5057 struct binder_proc
*proc
= vma
->vm_private_data
;
5059 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
5060 "%d(%s) close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
5061 proc
->pid
, proc
->tsk
->comm
, vma
->vm_start
, vma
->vm_end
,
5062 (vma
->vm_end
- vma
->vm_start
) / SZ_1K
, vma
->vm_flags
,
5063 (unsigned long)pgprot_val(vma
->vm_page_prot
));
5064 binder_alloc_vma_close(&proc
->alloc
);
5065 binder_defer_work(proc
, BINDER_DEFERRED_PUT_FILES
);
5068 static int binder_vm_fault(struct vm_fault
*vmf
)
5070 return VM_FAULT_SIGBUS
;
5073 static const struct vm_operations_struct binder_vm_ops
= {
5074 .open
= binder_vma_open
,
5075 .close
= binder_vma_close
,
5076 .fault
= binder_vm_fault
,
5079 static int binder_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
5082 struct binder_proc
*proc
= filp
->private_data
;
5083 const char *failure_string
;
5085 if (proc
->tsk
!= current
->group_leader
)
5088 if ((vma
->vm_end
- vma
->vm_start
) > SZ_4M
)
5089 vma
->vm_end
= vma
->vm_start
+ SZ_4M
;
5091 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
5092 "%s: %d(%s) %lx-%lx (%ld K) vma %lx pagep %lx\n",
5093 __func__
, proc
->pid
, proc
->tsk
->comm
, vma
->vm_start
, vma
->vm_end
,
5094 (vma
->vm_end
- vma
->vm_start
) / SZ_1K
, vma
->vm_flags
,
5095 (unsigned long)pgprot_val(vma
->vm_page_prot
));
5097 if (vma
->vm_flags
& FORBIDDEN_MMAP_FLAGS
) {
5099 failure_string
= "bad vm_flags";
5102 vma
->vm_flags
|= VM_DONTCOPY
| VM_MIXEDMAP
;
5103 vma
->vm_flags
&= ~VM_MAYWRITE
;
5105 vma
->vm_ops
= &binder_vm_ops
;
5106 vma
->vm_private_data
= proc
;
5108 ret
= binder_alloc_mmap_handler(&proc
->alloc
, vma
);
5110 failure_string
= "";
5113 mutex_lock(&proc
->files_lock
);
5114 proc
->files
= get_files_struct(current
);
5115 mutex_unlock(&proc
->files_lock
);
5119 pr_err("%s: %d(%s) %lx-%lx %s failed %d\n", __func__
,
5120 proc
->pid
, proc
->tsk
->comm
, vma
->vm_start
, vma
->vm_end
, failure_string
, ret
);
5124 static int binder_open(struct inode
*nodp
, struct file
*filp
)
5126 struct binder_proc
*proc
;
5127 struct binder_device
*binder_dev
;
5129 binder_debug(BINDER_DEBUG_OPEN_CLOSE
, "%s: %d:%d (%s:%s)\n", __func__
,
5130 current
->group_leader
->pid
, current
->pid
,
5131 current
->group_leader
->comm
, current
->comm
);
5133 proc
= kzalloc(sizeof(*proc
), GFP_KERNEL
);
5136 spin_lock_init(&proc
->inner_lock
);
5137 spin_lock_init(&proc
->outer_lock
);
5138 get_task_struct(current
->group_leader
);
5139 proc
->tsk
= current
->group_leader
;
5140 mutex_init(&proc
->files_lock
);
5141 INIT_LIST_HEAD(&proc
->todo
);
5142 if (binder_supported_policy(current
->policy
)) {
5143 proc
->default_priority
.sched_policy
= current
->policy
;
5144 proc
->default_priority
.prio
= current
->normal_prio
;
5146 proc
->default_priority
.sched_policy
= SCHED_NORMAL
;
5147 proc
->default_priority
.prio
= NICE_TO_PRIO(0);
5150 binder_dev
= container_of(filp
->private_data
, struct binder_device
,
5152 proc
->context
= &binder_dev
->context
;
5153 binder_alloc_init(&proc
->alloc
);
5155 binder_stats_created(BINDER_STAT_PROC
);
5156 proc
->pid
= current
->group_leader
->pid
;
5157 INIT_LIST_HEAD(&proc
->delivered_death
);
5158 INIT_LIST_HEAD(&proc
->waiting_threads
);
5159 filp
->private_data
= proc
;
5161 mutex_lock(&binder_procs_lock
);
5162 hlist_add_head(&proc
->proc_node
, &binder_procs
);
5163 mutex_unlock(&binder_procs_lock
);
5165 if (binder_debugfs_dir_entry_proc
) {
5168 snprintf(strbuf
, sizeof(strbuf
), "%u", proc
->pid
);
5170 * proc debug entries are shared between contexts, so
5171 * this will fail if the process tries to open the driver
5172 * again with a different context. The priting code will
5173 * anyway print all contexts that a given PID has, so this
5176 proc
->debugfs_entry
= debugfs_create_file(strbuf
, 0444,
5177 binder_debugfs_dir_entry_proc
,
5178 (void *)(unsigned long)proc
->pid
,
5185 static int binder_flush(struct file
*filp
, fl_owner_t id
)
5187 struct binder_proc
*proc
= filp
->private_data
;
5189 binder_defer_work(proc
, BINDER_DEFERRED_FLUSH
);
5194 static void binder_deferred_flush(struct binder_proc
*proc
)
5199 binder_inner_proc_lock(proc
);
5200 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
)) {
5201 struct binder_thread
*thread
= rb_entry(n
, struct binder_thread
, rb_node
);
5203 thread
->looper_need_return
= true;
5204 if (thread
->looper
& BINDER_LOOPER_STATE_WAITING
) {
5205 wake_up_interruptible(&thread
->wait
);
5209 binder_inner_proc_unlock(proc
);
5211 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
5212 "binder_flush: %d(%s) woke %d threads\n", proc
->pid
, proc
->tsk
->comm
,
5216 static int binder_release(struct inode
*nodp
, struct file
*filp
)
5218 struct binder_proc
*proc
= filp
->private_data
;
5220 debugfs_remove(proc
->debugfs_entry
);
5221 binder_defer_work(proc
, BINDER_DEFERRED_RELEASE
);
5226 static int binder_node_release(struct binder_node
*node
, int refs
)
5228 struct binder_ref
*ref
;
5230 struct binder_proc
*proc
= node
->proc
;
5232 binder_release_work(proc
, &node
->async_todo
);
5234 binder_node_lock(node
);
5235 binder_inner_proc_lock(proc
);
5236 binder_dequeue_work_ilocked(&node
->work
);
5238 * The caller must have taken a temporary ref on the node,
5240 BUG_ON(!node
->tmp_refs
);
5241 if (hlist_empty(&node
->refs
) && node
->tmp_refs
== 1) {
5242 binder_inner_proc_unlock(proc
);
5243 binder_node_unlock(node
);
5244 binder_free_node(node
);
5250 node
->local_strong_refs
= 0;
5251 node
->local_weak_refs
= 0;
5252 binder_inner_proc_unlock(proc
);
5254 spin_lock(&binder_dead_nodes_lock
);
5255 hlist_add_head(&node
->dead_node
, &binder_dead_nodes
);
5256 spin_unlock(&binder_dead_nodes_lock
);
5258 hlist_for_each_entry(ref
, &node
->refs
, node_entry
) {
5261 * Need the node lock to synchronize
5262 * with new notification requests and the
5263 * inner lock to synchronize with queued
5264 * death notifications.
5266 binder_inner_proc_lock(ref
->proc
);
5268 binder_inner_proc_unlock(ref
->proc
);
5274 BUG_ON(!list_empty(&ref
->death
->work
.entry
));
5275 ref
->death
->work
.type
= BINDER_WORK_DEAD_BINDER
;
5276 binder_enqueue_work_ilocked(&ref
->death
->work
,
5278 binder_wakeup_proc_ilocked(ref
->proc
);
5279 binder_inner_proc_unlock(ref
->proc
);
5282 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
5283 "node %d now dead, refs %d, death %d\n",
5284 node
->debug_id
, refs
, death
);
5285 binder_node_unlock(node
);
5286 binder_put_node(node
);
5291 static void binder_deferred_release(struct binder_proc
*proc
)
5293 struct binder_context
*context
= proc
->context
;
5295 int threads
, nodes
, incoming_refs
, outgoing_refs
, active_transactions
;
5297 BUG_ON(proc
->files
);
5299 mutex_lock(&binder_procs_lock
);
5300 hlist_del(&proc
->proc_node
);
5301 mutex_unlock(&binder_procs_lock
);
5303 mutex_lock(&context
->context_mgr_node_lock
);
5304 if (context
->binder_context_mgr_node
&&
5305 context
->binder_context_mgr_node
->proc
== proc
) {
5306 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
5307 "%s: %d(%s) context_mgr_node gone\n",
5308 __func__
, proc
->pid
, proc
->tsk
->comm
);
5309 context
->binder_context_mgr_node
= NULL
;
5311 mutex_unlock(&context
->context_mgr_node_lock
);
5312 binder_inner_proc_lock(proc
);
5314 * Make sure proc stays alive after we
5315 * remove all the threads
5319 proc
->is_dead
= true;
5321 active_transactions
= 0;
5322 while ((n
= rb_first(&proc
->threads
))) {
5323 struct binder_thread
*thread
;
5325 thread
= rb_entry(n
, struct binder_thread
, rb_node
);
5326 binder_inner_proc_unlock(proc
);
5328 active_transactions
+= binder_thread_release(proc
, thread
);
5329 binder_inner_proc_lock(proc
);
5334 while ((n
= rb_first(&proc
->nodes
))) {
5335 struct binder_node
*node
;
5337 node
= rb_entry(n
, struct binder_node
, rb_node
);
5340 * take a temporary ref on the node before
5341 * calling binder_node_release() which will either
5342 * kfree() the node or call binder_put_node()
5344 binder_inc_node_tmpref_ilocked(node
);
5345 rb_erase(&node
->rb_node
, &proc
->nodes
);
5346 binder_inner_proc_unlock(proc
);
5347 incoming_refs
= binder_node_release(node
, incoming_refs
);
5348 binder_inner_proc_lock(proc
);
5350 binder_inner_proc_unlock(proc
);
5353 binder_proc_lock(proc
);
5354 while ((n
= rb_first(&proc
->refs_by_desc
))) {
5355 struct binder_ref
*ref
;
5357 ref
= rb_entry(n
, struct binder_ref
, rb_node_desc
);
5359 binder_cleanup_ref_olocked(ref
);
5360 binder_proc_unlock(proc
);
5361 binder_free_ref(ref
);
5362 binder_proc_lock(proc
);
5364 binder_proc_unlock(proc
);
5366 binder_release_work(proc
, &proc
->todo
);
5367 binder_release_work(proc
, &proc
->delivered_death
);
5369 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
5370 "%s: %d(%s) threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
5371 __func__
, proc
->pid
, proc
->tsk
->comm
, threads
, nodes
, incoming_refs
,
5372 outgoing_refs
, active_transactions
);
5374 binder_proc_dec_tmpref(proc
);
5377 static void binder_deferred_func(struct work_struct
*work
)
5379 struct binder_proc
*proc
;
5380 struct files_struct
*files
;
5385 mutex_lock(&binder_deferred_lock
);
5386 if (!hlist_empty(&binder_deferred_list
)) {
5387 proc
= hlist_entry(binder_deferred_list
.first
,
5388 struct binder_proc
, deferred_work_node
);
5389 hlist_del_init(&proc
->deferred_work_node
);
5390 defer
= proc
->deferred_work
;
5391 proc
->deferred_work
= 0;
5396 mutex_unlock(&binder_deferred_lock
);
5399 if (defer
& BINDER_DEFERRED_PUT_FILES
) {
5400 mutex_lock(&proc
->files_lock
);
5401 files
= proc
->files
;
5404 mutex_unlock(&proc
->files_lock
);
5407 if (defer
& BINDER_DEFERRED_FLUSH
)
5408 binder_deferred_flush(proc
);
5410 if (defer
& BINDER_DEFERRED_RELEASE
)
5411 binder_deferred_release(proc
); /* frees proc */
5414 put_files_struct(files
);
5417 static DECLARE_WORK(binder_deferred_work
, binder_deferred_func
);
5420 binder_defer_work(struct binder_proc
*proc
, enum binder_deferred_state defer
)
5422 mutex_lock(&binder_deferred_lock
);
5423 proc
->deferred_work
|= defer
;
5424 if (hlist_unhashed(&proc
->deferred_work_node
)) {
5425 hlist_add_head(&proc
->deferred_work_node
,
5426 &binder_deferred_list
);
5427 schedule_work(&binder_deferred_work
);
5429 mutex_unlock(&binder_deferred_lock
);
5432 static void print_binder_transaction_ilocked(struct seq_file
*m
,
5433 struct binder_proc
*proc
,
5435 struct binder_transaction
*t
)
5437 struct binder_proc
*to_proc
;
5438 struct binder_buffer
*buffer
= t
->buffer
;
5440 spin_lock(&t
->lock
);
5441 to_proc
= t
->to_proc
;
5443 "%s %d: %pK from %d:%d(%s:%s) to %d:%d(%s:%s) code %x flags %x pri %d:%d r%d",
5444 prefix
, t
->debug_id
, t
,
5445 t
->from
? t
->from
->proc
->pid
: 0,
5446 t
->from
? t
->from
->pid
: 0,
5447 t
->from
? t
->from
->proc
->tsk
->comm
: "",
5448 t
->from
? t
->from
->task
->comm
: "",
5449 to_proc
? to_proc
->pid
: 0,
5450 t
->to_thread
? t
->to_thread
->pid
: 0,
5451 to_proc
? to_proc
->tsk
->comm
: "",
5452 t
->to_thread
? t
->to_thread
->task
->comm
: "",
5453 t
->code
, t
->flags
, t
->priority
.sched_policy
,
5454 t
->priority
.prio
, t
->need_reply
);
5455 spin_unlock(&t
->lock
);
5457 if (proc
!= to_proc
) {
5459 * Can only safely deref buffer if we are holding the
5460 * correct proc inner lock for this node
5466 if (buffer
== NULL
) {
5467 seq_puts(m
, " buffer free\n");
5470 if (buffer
->target_node
)
5471 seq_printf(m
, " node %d", buffer
->target_node
->debug_id
);
5472 seq_printf(m
, " size %zd:%zd data %pK\n",
5473 buffer
->data_size
, buffer
->offsets_size
,
5477 static void print_binder_work_ilocked(struct seq_file
*m
,
5478 struct binder_proc
*proc
,
5480 const char *transaction_prefix
,
5481 struct binder_work
*w
)
5483 struct binder_node
*node
;
5484 struct binder_transaction
*t
;
5487 case BINDER_WORK_TRANSACTION
:
5488 t
= container_of(w
, struct binder_transaction
, work
);
5489 print_binder_transaction_ilocked(
5490 m
, proc
, transaction_prefix
, t
);
5492 case BINDER_WORK_RETURN_ERROR
: {
5493 struct binder_error
*e
= container_of(
5494 w
, struct binder_error
, work
);
5496 seq_printf(m
, "%stransaction error: %u\n",
5499 case BINDER_WORK_TRANSACTION_COMPLETE
:
5500 seq_printf(m
, "%stransaction complete\n", prefix
);
5502 case BINDER_WORK_NODE
:
5503 node
= container_of(w
, struct binder_node
, work
);
5504 seq_printf(m
, "%snode work %d: u%016llx c%016llx\n",
5505 prefix
, node
->debug_id
,
5506 (u64
)node
->ptr
, (u64
)node
->cookie
);
5508 case BINDER_WORK_DEAD_BINDER
:
5509 seq_printf(m
, "%shas dead binder\n", prefix
);
5511 case BINDER_WORK_DEAD_BINDER_AND_CLEAR
:
5512 seq_printf(m
, "%shas cleared dead binder\n", prefix
);
5514 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION
:
5515 seq_printf(m
, "%shas cleared death notification\n", prefix
);
5518 seq_printf(m
, "%sunknown work: type %d\n", prefix
, w
->type
);
5523 static void print_binder_thread_ilocked(struct seq_file
*m
,
5524 struct binder_thread
*thread
,
5527 struct binder_transaction
*t
;
5528 struct binder_work
*w
;
5529 size_t start_pos
= m
->count
;
5532 seq_printf(m
, " thread %d %s: l %02x need_return %d tr %d\n",
5533 thread
->pid
, thread
->task
->comm
, thread
->looper
,
5534 thread
->looper_need_return
,
5535 atomic_read(&thread
->tmp_ref
));
5536 header_pos
= m
->count
;
5537 t
= thread
->transaction_stack
;
5539 if (t
->from
== thread
) {
5540 print_binder_transaction_ilocked(m
, thread
->proc
,
5541 " outgoing transaction", t
);
5543 } else if (t
->to_thread
== thread
) {
5544 print_binder_transaction_ilocked(m
, thread
->proc
,
5545 " incoming transaction", t
);
5548 print_binder_transaction_ilocked(m
, thread
->proc
,
5549 " bad transaction", t
);
5553 list_for_each_entry(w
, &thread
->todo
, entry
) {
5554 print_binder_work_ilocked(m
, thread
->proc
, " ",
5555 " pending transaction", w
);
5557 if (!print_always
&& m
->count
== header_pos
)
5558 m
->count
= start_pos
;
5561 static void print_binder_node_nilocked(struct seq_file
*m
,
5562 struct binder_node
*node
)
5564 struct binder_ref
*ref
;
5565 struct binder_work
*w
;
5569 hlist_for_each_entry(ref
, &node
->refs
, node_entry
)
5572 seq_printf(m
, " node %d: u%016llx c%016llx pri %d:%d hs %d hw %d ls %d lw %d is %d iw %d tr %d",
5573 node
->debug_id
, (u64
)node
->ptr
, (u64
)node
->cookie
,
5574 node
->sched_policy
, node
->min_priority
,
5575 node
->has_strong_ref
, node
->has_weak_ref
,
5576 node
->local_strong_refs
, node
->local_weak_refs
,
5577 node
->internal_strong_refs
, count
, node
->tmp_refs
);
5579 seq_puts(m
, " proc");
5580 hlist_for_each_entry(ref
, &node
->refs
, node_entry
)
5581 seq_printf(m
, " %d %s", ref
->proc
->pid
, ref
->proc
->tsk
->comm
);
5585 list_for_each_entry(w
, &node
->async_todo
, entry
)
5586 print_binder_work_ilocked(m
, node
->proc
, " ",
5587 " pending async transaction", w
);
5591 static void print_binder_ref_olocked(struct seq_file
*m
,
5592 struct binder_ref
*ref
)
5594 binder_node_lock(ref
->node
);
5595 seq_printf(m
, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
5596 ref
->data
.debug_id
, ref
->data
.desc
,
5597 ref
->node
->proc
? "" : "dead ",
5598 ref
->node
->debug_id
, ref
->data
.strong
,
5599 ref
->data
.weak
, ref
->death
);
5600 binder_node_unlock(ref
->node
);
5603 static void print_binder_proc(struct seq_file
*m
,
5604 struct binder_proc
*proc
, int print_all
)
5606 struct binder_work
*w
;
5608 size_t start_pos
= m
->count
;
5610 struct binder_node
*last_node
= NULL
;
5612 seq_printf(m
, "proc %d %s\n", proc
->pid
, proc
->tsk
->comm
);
5613 seq_printf(m
, "context %s\n", proc
->context
->name
);
5614 header_pos
= m
->count
;
5616 binder_inner_proc_lock(proc
);
5617 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
))
5618 print_binder_thread_ilocked(m
, rb_entry(n
, struct binder_thread
,
5619 rb_node
), print_all
);
5621 for (n
= rb_first(&proc
->nodes
); n
!= NULL
; n
= rb_next(n
)) {
5622 struct binder_node
*node
= rb_entry(n
, struct binder_node
,
5625 * take a temporary reference on the node so it
5626 * survives and isn't removed from the tree
5627 * while we print it.
5629 binder_inc_node_tmpref_ilocked(node
);
5630 /* Need to drop inner lock to take node lock */
5631 binder_inner_proc_unlock(proc
);
5633 binder_put_node(last_node
);
5634 binder_node_inner_lock(node
);
5635 print_binder_node_nilocked(m
, node
);
5636 binder_node_inner_unlock(node
);
5638 binder_inner_proc_lock(proc
);
5640 binder_inner_proc_unlock(proc
);
5642 binder_put_node(last_node
);
5645 binder_proc_lock(proc
);
5646 for (n
= rb_first(&proc
->refs_by_desc
);
5649 print_binder_ref_olocked(m
, rb_entry(n
,
5652 binder_proc_unlock(proc
);
5654 binder_alloc_print_allocated(m
, &proc
->alloc
);
5655 binder_inner_proc_lock(proc
);
5656 list_for_each_entry(w
, &proc
->todo
, entry
)
5657 print_binder_work_ilocked(m
, proc
, " ",
5658 " pending transaction", w
);
5659 list_for_each_entry(w
, &proc
->delivered_death
, entry
) {
5660 seq_puts(m
, " has delivered dead binder\n");
5663 binder_inner_proc_unlock(proc
);
5664 if (!print_all
&& m
->count
== header_pos
)
5665 m
->count
= start_pos
;
5668 static const char * const binder_return_strings
[] = {
5673 "BR_ACQUIRE_RESULT",
5675 "BR_TRANSACTION_COMPLETE",
5680 "BR_ATTEMPT_ACQUIRE",
5685 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
5689 static const char * const binder_command_strings
[] = {
5692 "BC_ACQUIRE_RESULT",
5700 "BC_ATTEMPT_ACQUIRE",
5701 "BC_REGISTER_LOOPER",
5704 "BC_REQUEST_DEATH_NOTIFICATION",
5705 "BC_CLEAR_DEATH_NOTIFICATION",
5706 "BC_DEAD_BINDER_DONE",
5707 "BC_TRANSACTION_SG",
5711 static const char * const binder_objstat_strings
[] = {
5718 "transaction_complete"
5721 static void print_binder_stats(struct seq_file
*m
, const char *prefix
,
5722 struct binder_stats
*stats
)
5726 BUILD_BUG_ON(ARRAY_SIZE(stats
->bc
) !=
5727 ARRAY_SIZE(binder_command_strings
));
5728 for (i
= 0; i
< ARRAY_SIZE(stats
->bc
); i
++) {
5729 int temp
= atomic_read(&stats
->bc
[i
]);
5732 seq_printf(m
, "%s%s: %d\n", prefix
,
5733 binder_command_strings
[i
], temp
);
5736 BUILD_BUG_ON(ARRAY_SIZE(stats
->br
) !=
5737 ARRAY_SIZE(binder_return_strings
));
5738 for (i
= 0; i
< ARRAY_SIZE(stats
->br
); i
++) {
5739 int temp
= atomic_read(&stats
->br
[i
]);
5742 seq_printf(m
, "%s%s: %d\n", prefix
,
5743 binder_return_strings
[i
], temp
);
5746 BUILD_BUG_ON(ARRAY_SIZE(stats
->obj_created
) !=
5747 ARRAY_SIZE(binder_objstat_strings
));
5748 BUILD_BUG_ON(ARRAY_SIZE(stats
->obj_created
) !=
5749 ARRAY_SIZE(stats
->obj_deleted
));
5750 for (i
= 0; i
< ARRAY_SIZE(stats
->obj_created
); i
++) {
5751 int created
= atomic_read(&stats
->obj_created
[i
]);
5752 int deleted
= atomic_read(&stats
->obj_deleted
[i
]);
5754 if (created
|| deleted
)
5755 seq_printf(m
, "%s%s: active %d total %d\n",
5757 binder_objstat_strings
[i
],
5763 static void print_binder_proc_stats(struct seq_file
*m
,
5764 struct binder_proc
*proc
)
5766 struct binder_work
*w
;
5767 struct binder_thread
*thread
;
5769 int count
, strong
, weak
, ready_threads
;
5770 size_t free_async_space
=
5771 binder_alloc_get_free_async_space(&proc
->alloc
);
5773 seq_printf(m
, "proc %d %s\n", proc
->pid
, proc
->tsk
->comm
);
5774 seq_printf(m
, "context %s\n", proc
->context
->name
);
5777 binder_inner_proc_lock(proc
);
5778 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
))
5781 list_for_each_entry(thread
, &proc
->waiting_threads
, waiting_thread_node
)
5784 seq_printf(m
, " threads: %d\n", count
);
5785 seq_printf(m
, " requested threads: %d+%d/%d\n"
5786 " ready threads %d\n"
5787 " free async space %zd\n", proc
->requested_threads
,
5788 proc
->requested_threads_started
, proc
->max_threads
,
5792 for (n
= rb_first(&proc
->nodes
); n
!= NULL
; n
= rb_next(n
))
5794 binder_inner_proc_unlock(proc
);
5795 seq_printf(m
, " nodes: %d\n", count
);
5799 binder_proc_lock(proc
);
5800 for (n
= rb_first(&proc
->refs_by_desc
); n
!= NULL
; n
= rb_next(n
)) {
5801 struct binder_ref
*ref
= rb_entry(n
, struct binder_ref
,
5804 strong
+= ref
->data
.strong
;
5805 weak
+= ref
->data
.weak
;
5807 binder_proc_unlock(proc
);
5808 seq_printf(m
, " refs: %d s %d w %d\n", count
, strong
, weak
);
5810 count
= binder_alloc_get_allocated_count(&proc
->alloc
);
5811 seq_printf(m
, " buffers: %d\n", count
);
5813 binder_alloc_print_pages(m
, &proc
->alloc
);
5816 binder_inner_proc_lock(proc
);
5817 list_for_each_entry(w
, &proc
->todo
, entry
) {
5818 if (w
->type
== BINDER_WORK_TRANSACTION
)
5821 binder_inner_proc_unlock(proc
);
5822 seq_printf(m
, " pending transactions: %d\n", count
);
5824 print_binder_stats(m
, " ", &proc
->stats
);
5828 static int binder_state_show(struct seq_file
*m
, void *unused
)
5830 struct binder_proc
*proc
;
5831 struct binder_node
*node
;
5832 struct binder_node
*last_node
= NULL
;
5834 seq_puts(m
, "binder state:\n");
5836 spin_lock(&binder_dead_nodes_lock
);
5837 if (!hlist_empty(&binder_dead_nodes
))
5838 seq_puts(m
, "dead nodes:\n");
5839 hlist_for_each_entry(node
, &binder_dead_nodes
, dead_node
) {
5841 * take a temporary reference on the node so it
5842 * survives and isn't removed from the list
5843 * while we print it.
5846 spin_unlock(&binder_dead_nodes_lock
);
5848 binder_put_node(last_node
);
5849 binder_node_lock(node
);
5850 print_binder_node_nilocked(m
, node
);
5851 binder_node_unlock(node
);
5853 spin_lock(&binder_dead_nodes_lock
);
5855 spin_unlock(&binder_dead_nodes_lock
);
5857 binder_put_node(last_node
);
5859 mutex_lock(&binder_procs_lock
);
5860 hlist_for_each_entry(proc
, &binder_procs
, proc_node
)
5861 print_binder_proc(m
, proc
, 1);
5862 mutex_unlock(&binder_procs_lock
);
5867 static int binder_stats_show(struct seq_file
*m
, void *unused
)
5869 struct binder_proc
*proc
;
5871 seq_puts(m
, "binder stats:\n");
5873 print_binder_stats(m
, "", &binder_stats
);
5875 mutex_lock(&binder_procs_lock
);
5876 hlist_for_each_entry(proc
, &binder_procs
, proc_node
)
5877 print_binder_proc_stats(m
, proc
);
5878 mutex_unlock(&binder_procs_lock
);
5883 static int binder_transactions_show(struct seq_file
*m
, void *unused
)
5885 struct binder_proc
*proc
;
5887 seq_puts(m
, "binder transactions:\n");
5888 mutex_lock(&binder_procs_lock
);
5889 hlist_for_each_entry(proc
, &binder_procs
, proc_node
)
5890 print_binder_proc(m
, proc
, 0);
5891 mutex_unlock(&binder_procs_lock
);
5896 static int binder_proc_show(struct seq_file
*m
, void *unused
)
5898 struct binder_proc
*itr
;
5899 int pid
= (unsigned long)m
->private;
5901 mutex_lock(&binder_procs_lock
);
5902 hlist_for_each_entry(itr
, &binder_procs
, proc_node
) {
5903 if (itr
->pid
== pid
) {
5904 seq_puts(m
, "binder proc state:\n");
5905 print_binder_proc(m
, itr
, 1);
5908 mutex_unlock(&binder_procs_lock
);
5913 static void print_binder_transaction_log_entry(struct seq_file
*m
,
5914 struct binder_transaction_log_entry
*e
)
5916 int debug_id
= READ_ONCE(e
->debug_id_done
);
5918 * read barrier to guarantee debug_id_done read before
5919 * we print the log values
5923 "%d: %s from %d:%d to %d:%d context %s node %d handle %d size %d:%d ret %d/%d l=%d",
5924 e
->debug_id
, (e
->call_type
== 2) ? "reply" :
5925 ((e
->call_type
== 1) ? "async" : "call "), e
->from_proc
,
5926 e
->from_thread
, e
->to_proc
, e
->to_thread
, e
->context_name
,
5927 e
->to_node
, e
->target_handle
, e
->data_size
, e
->offsets_size
,
5928 e
->return_error
, e
->return_error_param
,
5929 e
->return_error_line
);
5931 * read-barrier to guarantee read of debug_id_done after
5932 * done printing the fields of the entry
5935 seq_printf(m
, debug_id
&& debug_id
== READ_ONCE(e
->debug_id_done
) ?
5936 "\n" : " (incomplete)\n");
5939 static int binder_transaction_log_show(struct seq_file
*m
, void *unused
)
5941 struct binder_transaction_log
*log
= m
->private;
5942 unsigned int log_cur
= atomic_read(&log
->cur
);
5947 count
= log_cur
+ 1;
5948 cur
= count
< ARRAY_SIZE(log
->entry
) && !log
->full
?
5949 0 : count
% ARRAY_SIZE(log
->entry
);
5950 if (count
> ARRAY_SIZE(log
->entry
) || log
->full
)
5951 count
= ARRAY_SIZE(log
->entry
);
5952 for (i
= 0; i
< count
; i
++) {
5953 unsigned int index
= cur
++ % ARRAY_SIZE(log
->entry
);
5955 print_binder_transaction_log_entry(m
, &log
->entry
[index
]);
5960 static const struct file_operations binder_fops
= {
5961 .owner
= THIS_MODULE
,
5962 .poll
= binder_poll
,
5963 .unlocked_ioctl
= binder_ioctl
,
5964 .compat_ioctl
= binder_ioctl
,
5965 .mmap
= binder_mmap
,
5966 .open
= binder_open
,
5967 .flush
= binder_flush
,
5968 .release
= binder_release
,
5971 BINDER_DEBUG_ENTRY(state
);
5972 BINDER_DEBUG_ENTRY(stats
);
5973 BINDER_DEBUG_ENTRY(transactions
);
5974 BINDER_DEBUG_ENTRY(transaction_log
);
5976 static int __init
init_binder_device(const char *name
)
5979 struct binder_device
*binder_device
;
5981 binder_device
= kzalloc(sizeof(*binder_device
), GFP_KERNEL
);
5985 binder_device
->miscdev
.fops
= &binder_fops
;
5986 binder_device
->miscdev
.minor
= MISC_DYNAMIC_MINOR
;
5987 binder_device
->miscdev
.name
= name
;
5989 binder_device
->context
.binder_context_mgr_uid
= INVALID_UID
;
5990 binder_device
->context
.name
= name
;
5991 mutex_init(&binder_device
->context
.context_mgr_node_lock
);
5993 ret
= misc_register(&binder_device
->miscdev
);
5995 kfree(binder_device
);
5999 hlist_add_head(&binder_device
->hlist
, &binder_devices
);
6004 static int __init
binder_init(void)
6007 char *device_name
, *device_names
, *device_tmp
;
6008 struct binder_device
*device
;
6009 struct hlist_node
*tmp
;
6011 ret
= binder_alloc_shrinker_init();
6015 atomic_set(&binder_transaction_log
.cur
, ~0U);
6016 atomic_set(&binder_transaction_log_failed
.cur
, ~0U);
6018 binder_debugfs_dir_entry_root
= debugfs_create_dir("binder", NULL
);
6019 if (binder_debugfs_dir_entry_root
)
6020 binder_debugfs_dir_entry_proc
= debugfs_create_dir("proc",
6021 binder_debugfs_dir_entry_root
);
6023 if (binder_debugfs_dir_entry_root
) {
6024 debugfs_create_file("state",
6026 binder_debugfs_dir_entry_root
,
6028 &binder_state_fops
);
6029 debugfs_create_file("stats",
6031 binder_debugfs_dir_entry_root
,
6033 &binder_stats_fops
);
6034 debugfs_create_file("transactions",
6036 binder_debugfs_dir_entry_root
,
6038 &binder_transactions_fops
);
6039 debugfs_create_file("transaction_log",
6041 binder_debugfs_dir_entry_root
,
6042 &binder_transaction_log
,
6043 &binder_transaction_log_fops
);
6044 debugfs_create_file("failed_transaction_log",
6046 binder_debugfs_dir_entry_root
,
6047 &binder_transaction_log_failed
,
6048 &binder_transaction_log_fops
);
6052 * Copy the module_parameter string, because we don't want to
6053 * tokenize it in-place.
6055 device_names
= kzalloc(strlen(binder_devices_param
) + 1, GFP_KERNEL
);
6056 if (!device_names
) {
6058 goto err_alloc_device_names_failed
;
6060 strcpy(device_names
, binder_devices_param
);
6062 device_tmp
= device_names
;
6063 while ((device_name
= strsep(&device_tmp
, ","))) {
6064 ret
= init_binder_device(device_name
);
6066 goto err_init_binder_device_failed
;
6071 err_init_binder_device_failed
:
6072 hlist_for_each_entry_safe(device
, tmp
, &binder_devices
, hlist
) {
6073 misc_deregister(&device
->miscdev
);
6074 hlist_del(&device
->hlist
);
6078 kfree(device_names
);
6080 err_alloc_device_names_failed
:
6081 debugfs_remove_recursive(binder_debugfs_dir_entry_root
);
6086 device_initcall(binder_init
);
6088 #define CREATE_TRACE_POINTS
6089 #include "binder_trace.h"
6091 MODULE_LICENSE("GPL v2");