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
;
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_dequeue_work_head() - Dequeues the item at head of list
1016 * @proc: binder_proc associated with list
1017 * @list: list to dequeue head
1019 * Removes the head of the list if there are items on the list
1021 * Return: pointer dequeued binder_work, NULL if list was empty
1023 static struct binder_work
*binder_dequeue_work_head(
1024 struct binder_proc
*proc
,
1025 struct list_head
*list
)
1027 struct binder_work
*w
;
1029 binder_inner_proc_lock(proc
);
1030 w
= binder_dequeue_work_head_ilocked(list
);
1031 binder_inner_proc_unlock(proc
);
1036 binder_defer_work(struct binder_proc
*proc
, enum binder_deferred_state defer
);
1037 static void binder_free_thread(struct binder_thread
*thread
);
1038 static void binder_free_proc(struct binder_proc
*proc
);
1039 static void binder_inc_node_tmpref_ilocked(struct binder_node
*node
);
1041 static int task_get_unused_fd_flags(struct binder_proc
*proc
, int flags
)
1043 unsigned long rlim_cur
;
1047 mutex_lock(&proc
->files_lock
);
1048 if (proc
->files
== NULL
) {
1052 if (!lock_task_sighand(proc
->tsk
, &irqs
)) {
1056 rlim_cur
= task_rlimit(proc
->tsk
, RLIMIT_NOFILE
);
1057 unlock_task_sighand(proc
->tsk
, &irqs
);
1059 ret
= __alloc_fd(proc
->files
, 0, rlim_cur
, flags
);
1061 mutex_unlock(&proc
->files_lock
);
1066 * copied from fd_install
1068 static void task_fd_install(
1069 struct binder_proc
*proc
, unsigned int fd
, struct file
*file
)
1071 mutex_lock(&proc
->files_lock
);
1073 __fd_install(proc
->files
, fd
, file
);
1074 mutex_unlock(&proc
->files_lock
);
1078 * copied from sys_close
1080 static long task_close_fd(struct binder_proc
*proc
, unsigned int fd
)
1084 mutex_lock(&proc
->files_lock
);
1085 if (proc
->files
== NULL
) {
1089 retval
= __close_fd(proc
->files
, fd
);
1090 /* can't restart close syscall because file table entry was cleared */
1091 if (unlikely(retval
== -ERESTARTSYS
||
1092 retval
== -ERESTARTNOINTR
||
1093 retval
== -ERESTARTNOHAND
||
1094 retval
== -ERESTART_RESTARTBLOCK
))
1097 mutex_unlock(&proc
->files_lock
);
1101 static bool binder_has_work_ilocked(struct binder_thread
*thread
,
1104 return thread
->process_todo
||
1105 thread
->looper_need_return
||
1107 !binder_worklist_empty_ilocked(&thread
->proc
->todo
));
1110 static bool binder_has_work(struct binder_thread
*thread
, bool do_proc_work
)
1114 binder_inner_proc_lock(thread
->proc
);
1115 has_work
= binder_has_work_ilocked(thread
, do_proc_work
);
1116 binder_inner_proc_unlock(thread
->proc
);
1121 static bool binder_available_for_proc_work_ilocked(struct binder_thread
*thread
)
1123 return !thread
->transaction_stack
&&
1124 binder_worklist_empty_ilocked(&thread
->todo
) &&
1125 (thread
->looper
& (BINDER_LOOPER_STATE_ENTERED
|
1126 BINDER_LOOPER_STATE_REGISTERED
));
1129 static void binder_wakeup_poll_threads_ilocked(struct binder_proc
*proc
,
1133 struct binder_thread
*thread
;
1135 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
)) {
1136 thread
= rb_entry(n
, struct binder_thread
, rb_node
);
1137 if (thread
->looper
& BINDER_LOOPER_STATE_POLL
&&
1138 binder_available_for_proc_work_ilocked(thread
)) {
1140 wake_up_interruptible_sync(&thread
->wait
);
1142 wake_up_interruptible(&thread
->wait
);
1148 * binder_select_thread_ilocked() - selects a thread for doing proc work.
1149 * @proc: process to select a thread from
1151 * Note that calling this function moves the thread off the waiting_threads
1152 * list, so it can only be woken up by the caller of this function, or a
1153 * signal. Therefore, callers *should* always wake up the thread this function
1156 * Return: If there's a thread currently waiting for process work,
1157 * returns that thread. Otherwise returns NULL.
1159 static struct binder_thread
*
1160 binder_select_thread_ilocked(struct binder_proc
*proc
)
1162 struct binder_thread
*thread
;
1164 assert_spin_locked(&proc
->inner_lock
);
1165 thread
= list_first_entry_or_null(&proc
->waiting_threads
,
1166 struct binder_thread
,
1167 waiting_thread_node
);
1170 list_del_init(&thread
->waiting_thread_node
);
1176 * binder_wakeup_thread_ilocked() - wakes up a thread for doing proc work.
1177 * @proc: process to wake up a thread in
1178 * @thread: specific thread to wake-up (may be NULL)
1179 * @sync: whether to do a synchronous wake-up
1181 * This function wakes up a thread in the @proc process.
1182 * The caller may provide a specific thread to wake-up in
1183 * the @thread parameter. If @thread is NULL, this function
1184 * will wake up threads that have called poll().
1186 * Note that for this function to work as expected, callers
1187 * should first call binder_select_thread() to find a thread
1188 * to handle the work (if they don't have a thread already),
1189 * and pass the result into the @thread parameter.
1191 static void binder_wakeup_thread_ilocked(struct binder_proc
*proc
,
1192 struct binder_thread
*thread
,
1195 assert_spin_locked(&proc
->inner_lock
);
1199 wake_up_interruptible_sync(&thread
->wait
);
1201 wake_up_interruptible(&thread
->wait
);
1205 /* Didn't find a thread waiting for proc work; this can happen
1207 * 1. All threads are busy handling transactions
1208 * In that case, one of those threads should call back into
1209 * the kernel driver soon and pick up this work.
1210 * 2. Threads are using the (e)poll interface, in which case
1211 * they may be blocked on the waitqueue without having been
1212 * added to waiting_threads. For this case, we just iterate
1213 * over all threads not handling transaction work, and
1214 * wake them all up. We wake all because we don't know whether
1215 * a thread that called into (e)poll is handling non-binder
1218 binder_wakeup_poll_threads_ilocked(proc
, sync
);
1221 static void binder_wakeup_proc_ilocked(struct binder_proc
*proc
)
1223 struct binder_thread
*thread
= binder_select_thread_ilocked(proc
);
1225 binder_wakeup_thread_ilocked(proc
, thread
, /* sync = */false);
1228 static bool is_rt_policy(int policy
)
1230 return policy
== SCHED_FIFO
|| policy
== SCHED_RR
;
1233 static bool is_fair_policy(int policy
)
1235 return policy
== SCHED_NORMAL
|| policy
== SCHED_BATCH
;
1238 static bool binder_supported_policy(int policy
)
1240 return is_fair_policy(policy
) || is_rt_policy(policy
);
1243 static int to_userspace_prio(int policy
, int kernel_priority
)
1245 if (is_fair_policy(policy
))
1246 return PRIO_TO_NICE(kernel_priority
);
1248 return MAX_USER_RT_PRIO
- 1 - kernel_priority
;
1251 static int to_kernel_prio(int policy
, int user_priority
)
1253 if (is_fair_policy(policy
))
1254 return NICE_TO_PRIO(user_priority
);
1256 return MAX_USER_RT_PRIO
- 1 - user_priority
;
1259 static void binder_do_set_priority(struct task_struct
*task
,
1260 struct binder_priority desired
,
1263 int priority
; /* user-space prio value */
1265 unsigned int policy
= desired
.sched_policy
;
1267 if (task
->policy
== policy
&& task
->normal_prio
== desired
.prio
)
1270 has_cap_nice
= has_capability_noaudit(task
, CAP_SYS_NICE
);
1272 priority
= to_userspace_prio(policy
, desired
.prio
);
1274 if (verify
&& is_rt_policy(policy
) && !has_cap_nice
) {
1275 long max_rtprio
= task_rlimit(task
, RLIMIT_RTPRIO
);
1277 if (max_rtprio
== 0) {
1278 policy
= SCHED_NORMAL
;
1279 priority
= MIN_NICE
;
1280 } else if (priority
> max_rtprio
) {
1281 priority
= max_rtprio
;
1285 if (verify
&& is_fair_policy(policy
) && !has_cap_nice
) {
1286 long min_nice
= rlimit_to_nice(task_rlimit(task
, RLIMIT_NICE
));
1288 if (min_nice
> MAX_NICE
) {
1289 binder_user_error("%d(%s) RLIMIT_NICE not set\n",
1290 task
->pid
, task
->comm
);
1292 } else if (priority
< min_nice
) {
1293 priority
= min_nice
;
1297 if (policy
!= desired
.sched_policy
||
1298 to_kernel_prio(policy
, priority
) != desired
.prio
)
1299 binder_debug(BINDER_DEBUG_PRIORITY_CAP
,
1300 "%d(%s): priority %d not allowed, using %d instead\n",
1301 task
->pid
, task
->comm
, desired
.prio
,
1302 to_kernel_prio(policy
, priority
));
1304 trace_binder_set_priority(task
->tgid
, task
->pid
, task
->normal_prio
,
1305 to_kernel_prio(policy
, priority
),
1308 /* Set the actual priority */
1309 if (task
->policy
!= policy
|| is_rt_policy(policy
)) {
1310 struct sched_param params
;
1312 params
.sched_priority
= is_rt_policy(policy
) ? priority
: 0;
1314 sched_setscheduler_nocheck(task
,
1315 policy
| SCHED_RESET_ON_FORK
,
1318 if (is_fair_policy(policy
))
1319 set_user_nice(task
, priority
);
1322 static void binder_set_priority(struct task_struct
*task
,
1323 struct binder_priority desired
)
1325 binder_do_set_priority(task
, desired
, /* verify = */ true);
1328 static void binder_restore_priority(struct task_struct
*task
,
1329 struct binder_priority desired
)
1331 binder_do_set_priority(task
, desired
, /* verify = */ false);
1334 static void binder_transaction_priority(struct task_struct
*task
,
1335 struct binder_transaction
*t
,
1336 struct binder_priority node_prio
,
1339 struct binder_priority desired_prio
= t
->priority
;
1341 if (t
->set_priority_called
)
1344 t
->set_priority_called
= true;
1345 t
->saved_priority
.sched_policy
= task
->policy
;
1346 t
->saved_priority
.prio
= task
->normal_prio
;
1348 if (!inherit_rt
&& is_rt_policy(desired_prio
.sched_policy
)) {
1349 desired_prio
.prio
= NICE_TO_PRIO(0);
1350 desired_prio
.sched_policy
= SCHED_NORMAL
;
1353 if (node_prio
.prio
< t
->priority
.prio
||
1354 (node_prio
.prio
== t
->priority
.prio
&&
1355 node_prio
.sched_policy
== SCHED_FIFO
)) {
1357 * In case the minimum priority on the node is
1358 * higher (lower value), use that priority. If
1359 * the priority is the same, but the node uses
1360 * SCHED_FIFO, prefer SCHED_FIFO, since it can
1361 * run unbounded, unlike SCHED_RR.
1363 desired_prio
= node_prio
;
1366 binder_set_priority(task
, desired_prio
);
1369 static struct binder_node
*binder_get_node_ilocked(struct binder_proc
*proc
,
1370 binder_uintptr_t ptr
)
1372 struct rb_node
*n
= proc
->nodes
.rb_node
;
1373 struct binder_node
*node
;
1375 assert_spin_locked(&proc
->inner_lock
);
1378 node
= rb_entry(n
, struct binder_node
, rb_node
);
1380 if (ptr
< node
->ptr
)
1382 else if (ptr
> node
->ptr
)
1386 * take an implicit weak reference
1387 * to ensure node stays alive until
1388 * call to binder_put_node()
1390 binder_inc_node_tmpref_ilocked(node
);
1397 static struct binder_node
*binder_get_node(struct binder_proc
*proc
,
1398 binder_uintptr_t ptr
)
1400 struct binder_node
*node
;
1402 binder_inner_proc_lock(proc
);
1403 node
= binder_get_node_ilocked(proc
, ptr
);
1404 binder_inner_proc_unlock(proc
);
1408 static struct binder_node
*binder_init_node_ilocked(
1409 struct binder_proc
*proc
,
1410 struct binder_node
*new_node
,
1411 struct flat_binder_object
*fp
)
1413 struct rb_node
**p
= &proc
->nodes
.rb_node
;
1414 struct rb_node
*parent
= NULL
;
1415 struct binder_node
*node
;
1416 binder_uintptr_t ptr
= fp
? fp
->binder
: 0;
1417 binder_uintptr_t cookie
= fp
? fp
->cookie
: 0;
1418 __u32 flags
= fp
? fp
->flags
: 0;
1421 assert_spin_locked(&proc
->inner_lock
);
1426 node
= rb_entry(parent
, struct binder_node
, rb_node
);
1428 if (ptr
< node
->ptr
)
1430 else if (ptr
> node
->ptr
)
1431 p
= &(*p
)->rb_right
;
1434 * A matching node is already in
1435 * the rb tree. Abandon the init
1438 binder_inc_node_tmpref_ilocked(node
);
1443 binder_stats_created(BINDER_STAT_NODE
);
1445 rb_link_node(&node
->rb_node
, parent
, p
);
1446 rb_insert_color(&node
->rb_node
, &proc
->nodes
);
1447 node
->debug_id
= atomic_inc_return(&binder_last_id
);
1450 node
->cookie
= cookie
;
1451 node
->work
.type
= BINDER_WORK_NODE
;
1452 priority
= flags
& FLAT_BINDER_FLAG_PRIORITY_MASK
;
1453 node
->sched_policy
= (flags
& FLAT_BINDER_FLAG_SCHED_POLICY_MASK
) >>
1454 FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT
;
1455 node
->min_priority
= to_kernel_prio(node
->sched_policy
, priority
);
1456 node
->accept_fds
= !!(flags
& FLAT_BINDER_FLAG_ACCEPTS_FDS
);
1457 node
->inherit_rt
= !!(flags
& FLAT_BINDER_FLAG_INHERIT_RT
);
1458 node
->txn_security_ctx
= !!(flags
& FLAT_BINDER_FLAG_TXN_SECURITY_CTX
);
1459 spin_lock_init(&node
->lock
);
1460 INIT_LIST_HEAD(&node
->work
.entry
);
1461 INIT_LIST_HEAD(&node
->async_todo
);
1462 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1463 "%d:%d(%s:%s) node %d u%016llx c%016llx created\n",
1464 proc
->pid
, current
->pid
, proc
->tsk
->comm
, current
->comm
, node
->debug_id
,
1465 (u64
)node
->ptr
, (u64
)node
->cookie
);
1470 static struct binder_node
*binder_new_node(struct binder_proc
*proc
,
1471 struct flat_binder_object
*fp
)
1473 struct binder_node
*node
;
1474 struct binder_node
*new_node
= kzalloc(sizeof(*node
), GFP_KERNEL
);
1478 binder_inner_proc_lock(proc
);
1479 node
= binder_init_node_ilocked(proc
, new_node
, fp
);
1480 binder_inner_proc_unlock(proc
);
1481 if (node
!= new_node
)
1483 * The node was already added by another thread
1490 static void binder_free_node(struct binder_node
*node
)
1493 binder_stats_deleted(BINDER_STAT_NODE
);
1496 static int binder_inc_node_nilocked(struct binder_node
*node
, int strong
,
1498 struct list_head
*target_list
)
1500 struct binder_proc
*proc
= node
->proc
;
1502 assert_spin_locked(&node
->lock
);
1504 assert_spin_locked(&proc
->inner_lock
);
1507 if (target_list
== NULL
&&
1508 node
->internal_strong_refs
== 0 &&
1510 node
== node
->proc
->context
->binder_context_mgr_node
&&
1511 node
->has_strong_ref
)) {
1512 pr_err("invalid inc strong node for %d\n",
1516 node
->internal_strong_refs
++;
1518 node
->local_strong_refs
++;
1519 if (!node
->has_strong_ref
&& target_list
) {
1520 binder_dequeue_work_ilocked(&node
->work
);
1522 * Note: this function is the only place where we queue
1523 * directly to a thread->todo without using the
1524 * corresponding binder_enqueue_thread_work() helper
1525 * functions; in this case it's ok to not set the
1526 * process_todo flag, since we know this node work will
1527 * always be followed by other work that starts queue
1528 * processing: in case of synchronous transactions, a
1529 * BR_REPLY or BR_ERROR; in case of oneway
1530 * transactions, a BR_TRANSACTION_COMPLETE.
1532 binder_enqueue_work_ilocked(&node
->work
, target_list
);
1536 node
->local_weak_refs
++;
1537 if (!node
->has_weak_ref
&& list_empty(&node
->work
.entry
)) {
1538 if (target_list
== NULL
) {
1539 pr_err("invalid inc weak node for %d\n",
1546 binder_enqueue_work_ilocked(&node
->work
, target_list
);
1552 static int binder_inc_node(struct binder_node
*node
, int strong
, int internal
,
1553 struct list_head
*target_list
)
1557 binder_node_inner_lock(node
);
1558 ret
= binder_inc_node_nilocked(node
, strong
, internal
, target_list
);
1559 binder_node_inner_unlock(node
);
1564 static bool binder_dec_node_nilocked(struct binder_node
*node
,
1565 int strong
, int internal
)
1567 struct binder_proc
*proc
= node
->proc
;
1569 assert_spin_locked(&node
->lock
);
1571 assert_spin_locked(&proc
->inner_lock
);
1574 node
->internal_strong_refs
--;
1576 node
->local_strong_refs
--;
1577 if (node
->local_strong_refs
|| node
->internal_strong_refs
)
1581 node
->local_weak_refs
--;
1582 if (node
->local_weak_refs
|| node
->tmp_refs
||
1583 !hlist_empty(&node
->refs
))
1587 if (proc
&& (node
->has_strong_ref
|| node
->has_weak_ref
)) {
1588 if (list_empty(&node
->work
.entry
)) {
1589 binder_enqueue_work_ilocked(&node
->work
, &proc
->todo
);
1590 binder_wakeup_proc_ilocked(proc
);
1593 if (hlist_empty(&node
->refs
) && !node
->local_strong_refs
&&
1594 !node
->local_weak_refs
&& !node
->tmp_refs
) {
1596 binder_dequeue_work_ilocked(&node
->work
);
1597 rb_erase(&node
->rb_node
, &proc
->nodes
);
1598 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1599 "refless node %d deleted\n",
1602 BUG_ON(!list_empty(&node
->work
.entry
));
1603 spin_lock(&binder_dead_nodes_lock
);
1605 * tmp_refs could have changed so
1608 if (node
->tmp_refs
) {
1609 spin_unlock(&binder_dead_nodes_lock
);
1612 hlist_del(&node
->dead_node
);
1613 spin_unlock(&binder_dead_nodes_lock
);
1614 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1615 "dead node %d deleted\n",
1624 static void binder_dec_node(struct binder_node
*node
, int strong
, int internal
)
1628 binder_node_inner_lock(node
);
1629 free_node
= binder_dec_node_nilocked(node
, strong
, internal
);
1630 binder_node_inner_unlock(node
);
1632 binder_free_node(node
);
1635 static void binder_inc_node_tmpref_ilocked(struct binder_node
*node
)
1638 * No call to binder_inc_node() is needed since we
1639 * don't need to inform userspace of any changes to
1646 * binder_inc_node_tmpref() - take a temporary reference on node
1647 * @node: node to reference
1649 * Take reference on node to prevent the node from being freed
1650 * while referenced only by a local variable. The inner lock is
1651 * needed to serialize with the node work on the queue (which
1652 * isn't needed after the node is dead). If the node is dead
1653 * (node->proc is NULL), use binder_dead_nodes_lock to protect
1654 * node->tmp_refs against dead-node-only cases where the node
1655 * lock cannot be acquired (eg traversing the dead node list to
1658 static void binder_inc_node_tmpref(struct binder_node
*node
)
1660 binder_node_lock(node
);
1662 binder_inner_proc_lock(node
->proc
);
1664 spin_lock(&binder_dead_nodes_lock
);
1665 binder_inc_node_tmpref_ilocked(node
);
1667 binder_inner_proc_unlock(node
->proc
);
1669 spin_unlock(&binder_dead_nodes_lock
);
1670 binder_node_unlock(node
);
1674 * binder_dec_node_tmpref() - remove a temporary reference on node
1675 * @node: node to reference
1677 * Release temporary reference on node taken via binder_inc_node_tmpref()
1679 static void binder_dec_node_tmpref(struct binder_node
*node
)
1683 binder_node_inner_lock(node
);
1685 spin_lock(&binder_dead_nodes_lock
);
1687 BUG_ON(node
->tmp_refs
< 0);
1689 spin_unlock(&binder_dead_nodes_lock
);
1691 * Call binder_dec_node() to check if all refcounts are 0
1692 * and cleanup is needed. Calling with strong=0 and internal=1
1693 * causes no actual reference to be released in binder_dec_node().
1694 * If that changes, a change is needed here too.
1696 free_node
= binder_dec_node_nilocked(node
, 0, 1);
1697 binder_node_inner_unlock(node
);
1699 binder_free_node(node
);
1702 static void binder_put_node(struct binder_node
*node
)
1704 binder_dec_node_tmpref(node
);
1707 static struct binder_ref
*binder_get_ref_olocked(struct binder_proc
*proc
,
1708 u32 desc
, bool need_strong_ref
)
1710 struct rb_node
*n
= proc
->refs_by_desc
.rb_node
;
1711 struct binder_ref
*ref
;
1714 ref
= rb_entry(n
, struct binder_ref
, rb_node_desc
);
1716 if (desc
< ref
->data
.desc
) {
1718 } else if (desc
> ref
->data
.desc
) {
1720 } else if (need_strong_ref
&& !ref
->data
.strong
) {
1721 binder_user_error("tried to use weak ref as strong ref\n");
1731 * binder_get_ref_for_node_olocked() - get the ref associated with given node
1732 * @proc: binder_proc that owns the ref
1733 * @node: binder_node of target
1734 * @new_ref: newly allocated binder_ref to be initialized or %NULL
1736 * Look up the ref for the given node and return it if it exists
1738 * If it doesn't exist and the caller provides a newly allocated
1739 * ref, initialize the fields of the newly allocated ref and insert
1740 * into the given proc rb_trees and node refs list.
1742 * Return: the ref for node. It is possible that another thread
1743 * allocated/initialized the ref first in which case the
1744 * returned ref would be different than the passed-in
1745 * new_ref. new_ref must be kfree'd by the caller in
1748 static struct binder_ref
*binder_get_ref_for_node_olocked(
1749 struct binder_proc
*proc
,
1750 struct binder_node
*node
,
1751 struct binder_ref
*new_ref
)
1753 struct binder_context
*context
= proc
->context
;
1754 struct rb_node
**p
= &proc
->refs_by_node
.rb_node
;
1755 struct rb_node
*parent
= NULL
;
1756 struct binder_ref
*ref
;
1761 ref
= rb_entry(parent
, struct binder_ref
, rb_node_node
);
1763 if (node
< ref
->node
)
1765 else if (node
> ref
->node
)
1766 p
= &(*p
)->rb_right
;
1773 binder_stats_created(BINDER_STAT_REF
);
1774 new_ref
->data
.debug_id
= atomic_inc_return(&binder_last_id
);
1775 new_ref
->proc
= proc
;
1776 new_ref
->node
= node
;
1777 rb_link_node(&new_ref
->rb_node_node
, parent
, p
);
1778 rb_insert_color(&new_ref
->rb_node_node
, &proc
->refs_by_node
);
1780 new_ref
->data
.desc
= (node
== context
->binder_context_mgr_node
) ? 0 : 1;
1781 for (n
= rb_first(&proc
->refs_by_desc
); n
!= NULL
; n
= rb_next(n
)) {
1782 ref
= rb_entry(n
, struct binder_ref
, rb_node_desc
);
1783 if (ref
->data
.desc
> new_ref
->data
.desc
)
1785 new_ref
->data
.desc
= ref
->data
.desc
+ 1;
1788 p
= &proc
->refs_by_desc
.rb_node
;
1791 ref
= rb_entry(parent
, struct binder_ref
, rb_node_desc
);
1793 if (new_ref
->data
.desc
< ref
->data
.desc
)
1795 else if (new_ref
->data
.desc
> ref
->data
.desc
)
1796 p
= &(*p
)->rb_right
;
1800 rb_link_node(&new_ref
->rb_node_desc
, parent
, p
);
1801 rb_insert_color(&new_ref
->rb_node_desc
, &proc
->refs_by_desc
);
1803 binder_node_lock(node
);
1804 hlist_add_head(&new_ref
->node_entry
, &node
->refs
);
1806 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1807 "%d(%s) new ref %d desc %d for node %d\n",
1808 proc
->pid
, proc
->tsk
->comm
, new_ref
->data
.debug_id
, new_ref
->data
.desc
,
1810 binder_node_unlock(node
);
1814 static void binder_cleanup_ref_olocked(struct binder_ref
*ref
)
1816 bool delete_node
= false;
1818 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1819 "%d(%s) delete ref %d desc %d for node %d\n",
1820 ref
->proc
->pid
, ref
->proc
->tsk
->comm
, ref
->data
.debug_id
, ref
->data
.desc
,
1821 ref
->node
->debug_id
);
1823 rb_erase(&ref
->rb_node_desc
, &ref
->proc
->refs_by_desc
);
1824 rb_erase(&ref
->rb_node_node
, &ref
->proc
->refs_by_node
);
1826 binder_node_inner_lock(ref
->node
);
1827 if (ref
->data
.strong
)
1828 binder_dec_node_nilocked(ref
->node
, 1, 1);
1830 hlist_del(&ref
->node_entry
);
1831 delete_node
= binder_dec_node_nilocked(ref
->node
, 0, 1);
1832 binder_node_inner_unlock(ref
->node
);
1834 * Clear ref->node unless we want the caller to free the node
1838 * The caller uses ref->node to determine
1839 * whether the node needs to be freed. Clear
1840 * it since the node is still alive.
1846 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
1847 "%d(%s) delete ref %d desc %d has death notification\n",
1848 ref
->proc
->pid
, ref
->proc
->tsk
->comm
, ref
->data
.debug_id
,
1850 binder_dequeue_work(ref
->proc
, &ref
->death
->work
);
1851 binder_stats_deleted(BINDER_STAT_DEATH
);
1853 binder_stats_deleted(BINDER_STAT_REF
);
1857 * binder_inc_ref_olocked() - increment the ref for given handle
1858 * @ref: ref to be incremented
1859 * @strong: if true, strong increment, else weak
1860 * @target_list: list to queue node work on
1862 * Increment the ref. @ref->proc->outer_lock must be held on entry
1864 * Return: 0, if successful, else errno
1866 static int binder_inc_ref_olocked(struct binder_ref
*ref
, int strong
,
1867 struct list_head
*target_list
)
1872 if (ref
->data
.strong
== 0) {
1873 ret
= binder_inc_node(ref
->node
, 1, 1, target_list
);
1879 if (ref
->data
.weak
== 0) {
1880 ret
= binder_inc_node(ref
->node
, 0, 1, target_list
);
1890 * binder_dec_ref() - dec the ref for given handle
1891 * @ref: ref to be decremented
1892 * @strong: if true, strong decrement, else weak
1894 * Decrement the ref.
1896 * Return: true if ref is cleaned up and ready to be freed
1898 static bool binder_dec_ref_olocked(struct binder_ref
*ref
, int strong
)
1901 if (ref
->data
.strong
== 0) {
1902 binder_user_error("%d(%s) invalid dec strong, ref %d desc %d s %d w %d\n",
1903 ref
->proc
->pid
, ref
->proc
->tsk
->comm
, ref
->data
.debug_id
,
1904 ref
->data
.desc
, ref
->data
.strong
,
1909 if (ref
->data
.strong
== 0)
1910 binder_dec_node(ref
->node
, strong
, 1);
1912 if (ref
->data
.weak
== 0) {
1913 binder_user_error("%d(%s) invalid dec weak, ref %d desc %d s %d w %d\n",
1914 ref
->proc
->pid
, ref
->proc
->tsk
->comm
, ref
->data
.debug_id
,
1915 ref
->data
.desc
, ref
->data
.strong
,
1921 if (ref
->data
.strong
== 0 && ref
->data
.weak
== 0) {
1922 binder_cleanup_ref_olocked(ref
);
1929 * binder_get_node_from_ref() - get the node from the given proc/desc
1930 * @proc: proc containing the ref
1931 * @desc: the handle associated with the ref
1932 * @need_strong_ref: if true, only return node if ref is strong
1933 * @rdata: the id/refcount data for the ref
1935 * Given a proc and ref handle, return the associated binder_node
1937 * Return: a binder_node or NULL if not found or not strong when strong required
1939 static struct binder_node
*binder_get_node_from_ref(
1940 struct binder_proc
*proc
,
1941 u32 desc
, bool need_strong_ref
,
1942 struct binder_ref_data
*rdata
)
1944 struct binder_node
*node
;
1945 struct binder_ref
*ref
;
1947 binder_proc_lock(proc
);
1948 ref
= binder_get_ref_olocked(proc
, desc
, need_strong_ref
);
1953 * Take an implicit reference on the node to ensure
1954 * it stays alive until the call to binder_put_node()
1956 binder_inc_node_tmpref(node
);
1959 binder_proc_unlock(proc
);
1964 binder_proc_unlock(proc
);
1969 * binder_free_ref() - free the binder_ref
1972 * Free the binder_ref. Free the binder_node indicated by ref->node
1973 * (if non-NULL) and the binder_ref_death indicated by ref->death.
1975 static void binder_free_ref(struct binder_ref
*ref
)
1978 binder_free_node(ref
->node
);
1984 * binder_update_ref_for_handle() - inc/dec the ref for given handle
1985 * @proc: proc containing the ref
1986 * @desc: the handle associated with the ref
1987 * @increment: true=inc reference, false=dec reference
1988 * @strong: true=strong reference, false=weak reference
1989 * @rdata: the id/refcount data for the ref
1991 * Given a proc and ref handle, increment or decrement the ref
1992 * according to "increment" arg.
1994 * Return: 0 if successful, else errno
1996 static int binder_update_ref_for_handle(struct binder_proc
*proc
,
1997 uint32_t desc
, bool increment
, bool strong
,
1998 struct binder_ref_data
*rdata
)
2001 struct binder_ref
*ref
;
2002 bool delete_ref
= false;
2004 binder_proc_lock(proc
);
2005 ref
= binder_get_ref_olocked(proc
, desc
, strong
);
2011 ret
= binder_inc_ref_olocked(ref
, strong
, NULL
);
2013 delete_ref
= binder_dec_ref_olocked(ref
, strong
);
2017 binder_proc_unlock(proc
);
2020 binder_free_ref(ref
);
2024 binder_proc_unlock(proc
);
2029 * binder_dec_ref_for_handle() - dec the ref for given handle
2030 * @proc: proc containing the ref
2031 * @desc: the handle associated with the ref
2032 * @strong: true=strong reference, false=weak reference
2033 * @rdata: the id/refcount data for the ref
2035 * Just calls binder_update_ref_for_handle() to decrement the ref.
2037 * Return: 0 if successful, else errno
2039 static int binder_dec_ref_for_handle(struct binder_proc
*proc
,
2040 uint32_t desc
, bool strong
, struct binder_ref_data
*rdata
)
2042 return binder_update_ref_for_handle(proc
, desc
, false, strong
, rdata
);
2047 * binder_inc_ref_for_node() - increment the ref for given proc/node
2048 * @proc: proc containing the ref
2049 * @node: target node
2050 * @strong: true=strong reference, false=weak reference
2051 * @target_list: worklist to use if node is incremented
2052 * @rdata: the id/refcount data for the ref
2054 * Given a proc and node, increment the ref. Create the ref if it
2055 * doesn't already exist
2057 * Return: 0 if successful, else errno
2059 static int binder_inc_ref_for_node(struct binder_proc
*proc
,
2060 struct binder_node
*node
,
2062 struct list_head
*target_list
,
2063 struct binder_ref_data
*rdata
)
2065 struct binder_ref
*ref
;
2066 struct binder_ref
*new_ref
= NULL
;
2069 binder_proc_lock(proc
);
2070 ref
= binder_get_ref_for_node_olocked(proc
, node
, NULL
);
2072 binder_proc_unlock(proc
);
2073 new_ref
= kzalloc(sizeof(*ref
), GFP_KERNEL
);
2076 binder_proc_lock(proc
);
2077 ref
= binder_get_ref_for_node_olocked(proc
, node
, new_ref
);
2079 ret
= binder_inc_ref_olocked(ref
, strong
, target_list
);
2081 binder_proc_unlock(proc
);
2082 if (new_ref
&& ref
!= new_ref
)
2084 * Another thread created the ref first so
2085 * free the one we allocated
2091 static void binder_pop_transaction_ilocked(struct binder_thread
*target_thread
,
2092 struct binder_transaction
*t
)
2094 BUG_ON(!target_thread
);
2095 assert_spin_locked(&target_thread
->proc
->inner_lock
);
2096 BUG_ON(target_thread
->transaction_stack
!= t
);
2097 BUG_ON(target_thread
->transaction_stack
->from
!= target_thread
);
2098 target_thread
->transaction_stack
=
2099 target_thread
->transaction_stack
->from_parent
;
2104 * binder_thread_dec_tmpref() - decrement thread->tmp_ref
2105 * @thread: thread to decrement
2107 * A thread needs to be kept alive while being used to create or
2108 * handle a transaction. binder_get_txn_from() is used to safely
2109 * extract t->from from a binder_transaction and keep the thread
2110 * indicated by t->from from being freed. When done with that
2111 * binder_thread, this function is called to decrement the
2112 * tmp_ref and free if appropriate (thread has been released
2113 * and no transaction being processed by the driver)
2115 static void binder_thread_dec_tmpref(struct binder_thread
*thread
)
2118 * atomic is used to protect the counter value while
2119 * it cannot reach zero or thread->is_dead is false
2121 binder_inner_proc_lock(thread
->proc
);
2122 atomic_dec(&thread
->tmp_ref
);
2123 if (thread
->is_dead
&& !atomic_read(&thread
->tmp_ref
)) {
2124 binder_inner_proc_unlock(thread
->proc
);
2125 binder_free_thread(thread
);
2128 binder_inner_proc_unlock(thread
->proc
);
2132 * binder_proc_dec_tmpref() - decrement proc->tmp_ref
2133 * @proc: proc to decrement
2135 * A binder_proc needs to be kept alive while being used to create or
2136 * handle a transaction. proc->tmp_ref is incremented when
2137 * creating a new transaction or the binder_proc is currently in-use
2138 * by threads that are being released. When done with the binder_proc,
2139 * this function is called to decrement the counter and free the
2140 * proc if appropriate (proc has been released, all threads have
2141 * been released and not currenly in-use to process a transaction).
2143 static void binder_proc_dec_tmpref(struct binder_proc
*proc
)
2145 binder_inner_proc_lock(proc
);
2147 if (proc
->is_dead
&& RB_EMPTY_ROOT(&proc
->threads
) &&
2149 binder_inner_proc_unlock(proc
);
2150 binder_free_proc(proc
);
2153 binder_inner_proc_unlock(proc
);
2157 * binder_get_txn_from() - safely extract the "from" thread in transaction
2158 * @t: binder transaction for t->from
2160 * Atomically return the "from" thread and increment the tmp_ref
2161 * count for the thread to ensure it stays alive until
2162 * binder_thread_dec_tmpref() is called.
2164 * Return: the value of t->from
2166 static struct binder_thread
*binder_get_txn_from(
2167 struct binder_transaction
*t
)
2169 struct binder_thread
*from
;
2171 spin_lock(&t
->lock
);
2174 atomic_inc(&from
->tmp_ref
);
2175 spin_unlock(&t
->lock
);
2180 * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock
2181 * @t: binder transaction for t->from
2183 * Same as binder_get_txn_from() except it also acquires the proc->inner_lock
2184 * to guarantee that the thread cannot be released while operating on it.
2185 * The caller must call binder_inner_proc_unlock() to release the inner lock
2186 * as well as call binder_dec_thread_txn() to release the reference.
2188 * Return: the value of t->from
2190 static struct binder_thread
*binder_get_txn_from_and_acq_inner(
2191 struct binder_transaction
*t
)
2193 struct binder_thread
*from
;
2195 from
= binder_get_txn_from(t
);
2198 binder_inner_proc_lock(from
->proc
);
2200 BUG_ON(from
!= t
->from
);
2203 binder_inner_proc_unlock(from
->proc
);
2204 binder_thread_dec_tmpref(from
);
2208 static void binder_free_transaction(struct binder_transaction
*t
)
2210 struct binder_proc
*target_proc
= t
->to_proc
;
2213 binder_inner_proc_lock(target_proc
);
2215 t
->buffer
->transaction
= NULL
;
2216 binder_inner_proc_unlock(target_proc
);
2219 * If the transaction has no target_proc, then
2220 * t->buffer->transaction has already been cleared.
2223 binder_stats_deleted(BINDER_STAT_TRANSACTION
);
2226 static void binder_send_failed_reply(struct binder_transaction
*t
,
2227 uint32_t error_code
)
2229 struct binder_thread
*target_thread
;
2230 struct binder_transaction
*next
;
2232 BUG_ON(t
->flags
& TF_ONE_WAY
);
2234 target_thread
= binder_get_txn_from_and_acq_inner(t
);
2235 if (target_thread
) {
2236 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
2237 "send failed reply for transaction %d to %d:%d(%s:%s)\n",
2239 target_thread
->proc
->pid
,
2241 target_thread
->proc
->tsk
->comm
,
2242 target_thread
->task
->comm
);
2244 binder_pop_transaction_ilocked(target_thread
, t
);
2245 if (target_thread
->reply_error
.cmd
== BR_OK
) {
2246 target_thread
->reply_error
.cmd
= error_code
;
2247 binder_enqueue_thread_work_ilocked(
2249 &target_thread
->reply_error
.work
);
2250 wake_up_interruptible(&target_thread
->wait
);
2253 * Cannot get here for normal operation, but
2254 * we can if multiple synchronous transactions
2255 * are sent without blocking for responses.
2256 * Just ignore the 2nd error in this case.
2258 pr_warn("Unexpected reply error: %u\n",
2259 target_thread
->reply_error
.cmd
);
2261 binder_inner_proc_unlock(target_thread
->proc
);
2262 binder_thread_dec_tmpref(target_thread
);
2263 binder_free_transaction(t
);
2266 next
= t
->from_parent
;
2268 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
2269 "send failed reply for transaction %d, target dead\n",
2272 binder_free_transaction(t
);
2274 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
2275 "reply failed, no target thread at root\n");
2279 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
2280 "reply failed, no target thread -- retry %d\n",
2286 * binder_cleanup_transaction() - cleans up undelivered transaction
2287 * @t: transaction that needs to be cleaned up
2288 * @reason: reason the transaction wasn't delivered
2289 * @error_code: error to return to caller (if synchronous call)
2291 static void binder_cleanup_transaction(struct binder_transaction
*t
,
2293 uint32_t error_code
)
2295 if (t
->buffer
->target_node
&& !(t
->flags
& TF_ONE_WAY
)) {
2296 binder_send_failed_reply(t
, error_code
);
2298 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
2299 "undelivered transaction %d, %s\n",
2300 t
->debug_id
, reason
);
2301 binder_free_transaction(t
);
2306 * binder_validate_object() - checks for a valid metadata object in a buffer.
2307 * @buffer: binder_buffer that we're parsing.
2308 * @offset: offset in the buffer at which to validate an object.
2310 * Return: If there's a valid metadata object at @offset in @buffer, the
2311 * size of that object. Otherwise, it returns zero.
2313 static size_t binder_validate_object(struct binder_buffer
*buffer
, u64 offset
)
2315 /* Check if we can read a header first */
2316 struct binder_object_header
*hdr
;
2317 size_t object_size
= 0;
2319 if (buffer
->data_size
< sizeof(*hdr
) ||
2320 offset
> buffer
->data_size
- sizeof(*hdr
) ||
2321 !IS_ALIGNED(offset
, sizeof(u32
)))
2324 /* Ok, now see if we can read a complete object. */
2325 hdr
= (struct binder_object_header
*)(buffer
->data
+ offset
);
2326 switch (hdr
->type
) {
2327 case BINDER_TYPE_BINDER
:
2328 case BINDER_TYPE_WEAK_BINDER
:
2329 case BINDER_TYPE_HANDLE
:
2330 case BINDER_TYPE_WEAK_HANDLE
:
2331 object_size
= sizeof(struct flat_binder_object
);
2333 case BINDER_TYPE_FD
:
2334 object_size
= sizeof(struct binder_fd_object
);
2336 case BINDER_TYPE_PTR
:
2337 object_size
= sizeof(struct binder_buffer_object
);
2339 case BINDER_TYPE_FDA
:
2340 object_size
= sizeof(struct binder_fd_array_object
);
2345 if (offset
<= buffer
->data_size
- object_size
&&
2346 buffer
->data_size
>= object_size
)
2353 * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
2354 * @b: binder_buffer containing the object
2355 * @index: index in offset array at which the binder_buffer_object is
2357 * @start: points to the start of the offset array
2358 * @num_valid: the number of valid offsets in the offset array
2360 * Return: If @index is within the valid range of the offset array
2361 * described by @start and @num_valid, and if there's a valid
2362 * binder_buffer_object at the offset found in index @index
2363 * of the offset array, that object is returned. Otherwise,
2364 * %NULL is returned.
2365 * Note that the offset found in index @index itself is not
2366 * verified; this function assumes that @num_valid elements
2367 * from @start were previously verified to have valid offsets.
2369 static struct binder_buffer_object
*binder_validate_ptr(struct binder_buffer
*b
,
2370 binder_size_t index
,
2371 binder_size_t
*start
,
2372 binder_size_t num_valid
)
2374 struct binder_buffer_object
*buffer_obj
;
2375 binder_size_t
*offp
;
2377 if (index
>= num_valid
)
2380 offp
= start
+ index
;
2381 buffer_obj
= (struct binder_buffer_object
*)(b
->data
+ *offp
);
2382 if (buffer_obj
->hdr
.type
!= BINDER_TYPE_PTR
)
2389 * binder_validate_fixup() - validates pointer/fd fixups happen in order.
2390 * @b: transaction buffer
2391 * @objects_start start of objects buffer
2392 * @buffer: binder_buffer_object in which to fix up
2393 * @offset: start offset in @buffer to fix up
2394 * @last_obj: last binder_buffer_object that we fixed up in
2395 * @last_min_offset: minimum fixup offset in @last_obj
2397 * Return: %true if a fixup in buffer @buffer at offset @offset is
2400 * For safety reasons, we only allow fixups inside a buffer to happen
2401 * at increasing offsets; additionally, we only allow fixup on the last
2402 * buffer object that was verified, or one of its parents.
2404 * Example of what is allowed:
2407 * B (parent = A, offset = 0)
2408 * C (parent = A, offset = 16)
2409 * D (parent = C, offset = 0)
2410 * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
2412 * Examples of what is not allowed:
2414 * Decreasing offsets within the same parent:
2416 * C (parent = A, offset = 16)
2417 * B (parent = A, offset = 0) // decreasing offset within A
2419 * Referring to a parent that wasn't the last object or any of its parents:
2421 * B (parent = A, offset = 0)
2422 * C (parent = A, offset = 0)
2423 * C (parent = A, offset = 16)
2424 * D (parent = B, offset = 0) // B is not A or any of A's parents
2426 static bool binder_validate_fixup(struct binder_buffer
*b
,
2427 binder_size_t
*objects_start
,
2428 struct binder_buffer_object
*buffer
,
2429 binder_size_t fixup_offset
,
2430 struct binder_buffer_object
*last_obj
,
2431 binder_size_t last_min_offset
)
2434 /* Nothing to fix up in */
2438 while (last_obj
!= buffer
) {
2440 * Safe to retrieve the parent of last_obj, since it
2441 * was already previously verified by the driver.
2443 if ((last_obj
->flags
& BINDER_BUFFER_FLAG_HAS_PARENT
) == 0)
2445 last_min_offset
= last_obj
->parent_offset
+ sizeof(uintptr_t);
2446 last_obj
= (struct binder_buffer_object
*)
2447 (b
->data
+ *(objects_start
+ last_obj
->parent
));
2449 return (fixup_offset
>= last_min_offset
);
2452 static void binder_transaction_buffer_release(struct binder_proc
*proc
,
2453 struct binder_buffer
*buffer
,
2454 binder_size_t
*failed_at
)
2456 binder_size_t
*offp
, *off_start
, *off_end
;
2457 int debug_id
= buffer
->debug_id
;
2459 binder_debug(BINDER_DEBUG_TRANSACTION
,
2460 "%d(%s) buffer release %d, size %zd-%zd, failed at %pK\n",
2461 proc
->pid
, proc
->tsk
->comm
, buffer
->debug_id
,
2462 buffer
->data_size
, buffer
->offsets_size
, failed_at
);
2464 if (buffer
->target_node
)
2465 binder_dec_node(buffer
->target_node
, 1, 0);
2467 off_start
= (binder_size_t
*)(buffer
->data
+
2468 ALIGN(buffer
->data_size
, sizeof(void *)));
2470 off_end
= failed_at
;
2472 off_end
= (void *)off_start
+ buffer
->offsets_size
;
2473 for (offp
= off_start
; offp
< off_end
; offp
++) {
2474 struct binder_object_header
*hdr
;
2475 size_t object_size
= binder_validate_object(buffer
, *offp
);
2477 if (object_size
== 0) {
2478 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
2479 debug_id
, (u64
)*offp
, buffer
->data_size
);
2482 hdr
= (struct binder_object_header
*)(buffer
->data
+ *offp
);
2483 switch (hdr
->type
) {
2484 case BINDER_TYPE_BINDER
:
2485 case BINDER_TYPE_WEAK_BINDER
: {
2486 struct flat_binder_object
*fp
;
2487 struct binder_node
*node
;
2489 fp
= to_flat_binder_object(hdr
);
2490 node
= binder_get_node(proc
, fp
->binder
);
2492 pr_err("transaction release %d bad node %016llx\n",
2493 debug_id
, (u64
)fp
->binder
);
2496 binder_debug(BINDER_DEBUG_TRANSACTION
,
2497 " node %d u%016llx\n",
2498 node
->debug_id
, (u64
)node
->ptr
);
2499 binder_dec_node(node
, hdr
->type
== BINDER_TYPE_BINDER
,
2501 binder_put_node(node
);
2503 case BINDER_TYPE_HANDLE
:
2504 case BINDER_TYPE_WEAK_HANDLE
: {
2505 struct flat_binder_object
*fp
;
2506 struct binder_ref_data rdata
;
2509 fp
= to_flat_binder_object(hdr
);
2510 ret
= binder_dec_ref_for_handle(proc
, fp
->handle
,
2511 hdr
->type
== BINDER_TYPE_HANDLE
, &rdata
);
2514 pr_err("transaction release %d bad handle %d, ret = %d\n",
2515 debug_id
, fp
->handle
, ret
);
2518 binder_debug(BINDER_DEBUG_TRANSACTION
,
2519 " ref %d desc %d\n",
2520 rdata
.debug_id
, rdata
.desc
);
2523 case BINDER_TYPE_FD
: {
2524 struct binder_fd_object
*fp
= to_binder_fd_object(hdr
);
2526 binder_debug(BINDER_DEBUG_TRANSACTION
,
2527 " fd %d\n", fp
->fd
);
2529 task_close_fd(proc
, fp
->fd
);
2531 case BINDER_TYPE_PTR
:
2533 * Nothing to do here, this will get cleaned up when the
2534 * transaction buffer gets freed
2537 case BINDER_TYPE_FDA
: {
2538 struct binder_fd_array_object
*fda
;
2539 struct binder_buffer_object
*parent
;
2540 uintptr_t parent_buffer
;
2543 binder_size_t fd_buf_size
;
2545 fda
= to_binder_fd_array_object(hdr
);
2546 parent
= binder_validate_ptr(buffer
, fda
->parent
,
2550 pr_err("transaction release %d bad parent offset",
2555 * Since the parent was already fixed up, convert it
2556 * back to kernel address space to access it
2558 parent_buffer
= parent
->buffer
-
2559 binder_alloc_get_user_buffer_offset(
2562 fd_buf_size
= sizeof(u32
) * fda
->num_fds
;
2563 if (fda
->num_fds
>= SIZE_MAX
/ sizeof(u32
)) {
2564 pr_err("transaction release %d invalid number of fds (%lld)\n",
2565 debug_id
, (u64
)fda
->num_fds
);
2568 if (fd_buf_size
> parent
->length
||
2569 fda
->parent_offset
> parent
->length
- fd_buf_size
) {
2570 /* No space for all file descriptors here. */
2571 pr_err("transaction release %d not enough space for %lld fds in buffer\n",
2572 debug_id
, (u64
)fda
->num_fds
);
2575 fd_array
= (u32
*)(parent_buffer
+ (uintptr_t)fda
->parent_offset
);
2576 for (fd_index
= 0; fd_index
< fda
->num_fds
; fd_index
++)
2577 task_close_fd(proc
, fd_array
[fd_index
]);
2580 pr_err("transaction release %d bad object type %x\n",
2581 debug_id
, hdr
->type
);
2587 static int binder_translate_binder(struct flat_binder_object
*fp
,
2588 struct binder_transaction
*t
,
2589 struct binder_thread
*thread
)
2591 struct binder_node
*node
;
2592 struct binder_proc
*proc
= thread
->proc
;
2593 struct binder_proc
*target_proc
= t
->to_proc
;
2594 struct binder_ref_data rdata
;
2597 node
= binder_get_node(proc
, fp
->binder
);
2599 node
= binder_new_node(proc
, fp
);
2603 if (fp
->cookie
!= node
->cookie
) {
2604 binder_user_error("%d:%d(%s:%s) sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
2605 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, (u64
)fp
->binder
,
2606 node
->debug_id
, (u64
)fp
->cookie
,
2611 if (security_binder_transfer_binder(proc
->tsk
, target_proc
->tsk
)) {
2616 ret
= binder_inc_ref_for_node(target_proc
, node
,
2617 fp
->hdr
.type
== BINDER_TYPE_BINDER
,
2618 &thread
->todo
, &rdata
);
2622 if (fp
->hdr
.type
== BINDER_TYPE_BINDER
)
2623 fp
->hdr
.type
= BINDER_TYPE_HANDLE
;
2625 fp
->hdr
.type
= BINDER_TYPE_WEAK_HANDLE
;
2627 fp
->handle
= rdata
.desc
;
2630 trace_binder_transaction_node_to_ref(t
, node
, &rdata
);
2631 binder_debug(BINDER_DEBUG_TRANSACTION
,
2632 " node %d u%016llx -> ref %d desc %d\n",
2633 node
->debug_id
, (u64
)node
->ptr
,
2634 rdata
.debug_id
, rdata
.desc
);
2636 binder_put_node(node
);
2640 static int binder_translate_handle(struct flat_binder_object
*fp
,
2641 struct binder_transaction
*t
,
2642 struct binder_thread
*thread
)
2644 struct binder_proc
*proc
= thread
->proc
;
2645 struct binder_proc
*target_proc
= t
->to_proc
;
2646 struct binder_node
*node
;
2647 struct binder_ref_data src_rdata
;
2650 node
= binder_get_node_from_ref(proc
, fp
->handle
,
2651 fp
->hdr
.type
== BINDER_TYPE_HANDLE
, &src_rdata
);
2653 binder_user_error("%d:%d(%s:%s) got transaction with invalid handle, %d\n",
2654 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, fp
->handle
);
2657 if (security_binder_transfer_binder(proc
->tsk
, target_proc
->tsk
)) {
2662 binder_node_lock(node
);
2663 if (node
->proc
== target_proc
) {
2664 if (fp
->hdr
.type
== BINDER_TYPE_HANDLE
)
2665 fp
->hdr
.type
= BINDER_TYPE_BINDER
;
2667 fp
->hdr
.type
= BINDER_TYPE_WEAK_BINDER
;
2668 fp
->binder
= node
->ptr
;
2669 fp
->cookie
= node
->cookie
;
2671 binder_inner_proc_lock(node
->proc
);
2672 binder_inc_node_nilocked(node
,
2673 fp
->hdr
.type
== BINDER_TYPE_BINDER
,
2676 binder_inner_proc_unlock(node
->proc
);
2677 trace_binder_transaction_ref_to_node(t
, node
, &src_rdata
);
2678 binder_debug(BINDER_DEBUG_TRANSACTION
,
2679 " ref %d desc %d -> node %d u%016llx\n",
2680 src_rdata
.debug_id
, src_rdata
.desc
, node
->debug_id
,
2682 binder_node_unlock(node
);
2684 struct binder_ref_data dest_rdata
;
2686 binder_node_unlock(node
);
2687 ret
= binder_inc_ref_for_node(target_proc
, node
,
2688 fp
->hdr
.type
== BINDER_TYPE_HANDLE
,
2694 fp
->handle
= dest_rdata
.desc
;
2696 trace_binder_transaction_ref_to_ref(t
, node
, &src_rdata
,
2698 binder_debug(BINDER_DEBUG_TRANSACTION
,
2699 " ref %d desc %d -> ref %d desc %d (node %d)\n",
2700 src_rdata
.debug_id
, src_rdata
.desc
,
2701 dest_rdata
.debug_id
, dest_rdata
.desc
,
2705 binder_put_node(node
);
2709 static int binder_translate_fd(int fd
,
2710 struct binder_transaction
*t
,
2711 struct binder_thread
*thread
,
2712 struct binder_transaction
*in_reply_to
)
2714 struct binder_proc
*proc
= thread
->proc
;
2715 struct binder_proc
*target_proc
= t
->to_proc
;
2719 bool target_allows_fd
;
2722 target_allows_fd
= !!(in_reply_to
->flags
& TF_ACCEPT_FDS
);
2724 target_allows_fd
= t
->buffer
->target_node
->accept_fds
;
2725 if (!target_allows_fd
) {
2726 binder_user_error("%d:%d(%s:%s) got %s with fd, %d, but target does not allow fds\n",
2727 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
2728 in_reply_to
? "reply" : "transaction",
2731 goto err_fd_not_accepted
;
2736 binder_user_error("%d:%d(%s:%s) got transaction with invalid fd, %d\n",
2737 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, fd
);
2741 ret
= security_binder_transfer_file(proc
->tsk
, target_proc
->tsk
, file
);
2747 target_fd
= task_get_unused_fd_flags(target_proc
, O_CLOEXEC
);
2748 if (target_fd
< 0) {
2750 goto err_get_unused_fd
;
2752 task_fd_install(target_proc
, target_fd
, file
);
2753 trace_binder_transaction_fd(t
, fd
, target_fd
);
2754 binder_debug(BINDER_DEBUG_TRANSACTION
, " fd %d -> %d\n",
2763 err_fd_not_accepted
:
2767 static int binder_translate_fd_array(struct binder_fd_array_object
*fda
,
2768 struct binder_buffer_object
*parent
,
2769 struct binder_transaction
*t
,
2770 struct binder_thread
*thread
,
2771 struct binder_transaction
*in_reply_to
)
2773 binder_size_t fdi
, fd_buf_size
, num_installed_fds
;
2775 uintptr_t parent_buffer
;
2777 struct binder_proc
*proc
= thread
->proc
;
2778 struct binder_proc
*target_proc
= t
->to_proc
;
2780 fd_buf_size
= sizeof(u32
) * fda
->num_fds
;
2781 if (fda
->num_fds
>= SIZE_MAX
/ sizeof(u32
)) {
2782 binder_user_error("%d:%d(%s:%s) got transaction with invalid number of fds (%lld)\n",
2783 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, (u64
)fda
->num_fds
);
2786 if (fd_buf_size
> parent
->length
||
2787 fda
->parent_offset
> parent
->length
- fd_buf_size
) {
2788 /* No space for all file descriptors here. */
2789 binder_user_error("%d:%d(%s:%s) not enough space to store %lld fds in buffer\n",
2790 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, (u64
)fda
->num_fds
);
2794 * Since the parent was already fixed up, convert it
2795 * back to the kernel address space to access it
2797 parent_buffer
= parent
->buffer
-
2798 binder_alloc_get_user_buffer_offset(&target_proc
->alloc
);
2799 fd_array
= (u32
*)(parent_buffer
+ (uintptr_t)fda
->parent_offset
);
2800 if (!IS_ALIGNED((unsigned long)fd_array
, sizeof(u32
))) {
2801 binder_user_error("%d:%d(%s:%s) parent offset not aligned correctly.\n",
2802 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
2805 for (fdi
= 0; fdi
< fda
->num_fds
; fdi
++) {
2806 target_fd
= binder_translate_fd(fd_array
[fdi
], t
, thread
,
2809 goto err_translate_fd_failed
;
2810 fd_array
[fdi
] = target_fd
;
2814 err_translate_fd_failed
:
2816 * Failed to allocate fd or security error, free fds
2819 num_installed_fds
= fdi
;
2820 for (fdi
= 0; fdi
< num_installed_fds
; fdi
++)
2821 task_close_fd(target_proc
, fd_array
[fdi
]);
2825 static int binder_fixup_parent(struct binder_transaction
*t
,
2826 struct binder_thread
*thread
,
2827 struct binder_buffer_object
*bp
,
2828 binder_size_t
*off_start
,
2829 binder_size_t num_valid
,
2830 struct binder_buffer_object
*last_fixup_obj
,
2831 binder_size_t last_fixup_min_off
)
2833 struct binder_buffer_object
*parent
;
2835 struct binder_buffer
*b
= t
->buffer
;
2836 struct binder_proc
*proc
= thread
->proc
;
2837 struct binder_proc
*target_proc
= t
->to_proc
;
2839 if (!(bp
->flags
& BINDER_BUFFER_FLAG_HAS_PARENT
))
2842 parent
= binder_validate_ptr(b
, bp
->parent
, off_start
, num_valid
);
2844 binder_user_error("%d:%d(%s:%s) got transaction with invalid parent offset or type\n",
2845 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
2849 if (!binder_validate_fixup(b
, off_start
,
2850 parent
, bp
->parent_offset
,
2852 last_fixup_min_off
)) {
2853 binder_user_error("%d:%d(%s:%s) got transaction with out-of-order buffer fixup\n",
2854 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
2858 if (parent
->length
< sizeof(binder_uintptr_t
) ||
2859 bp
->parent_offset
> parent
->length
- sizeof(binder_uintptr_t
)) {
2860 /* No space for a pointer here! */
2861 binder_user_error("%d:%d(%s:%s) got transaction with invalid parent offset\n",
2862 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
2865 parent_buffer
= (u8
*)((uintptr_t)parent
->buffer
-
2866 binder_alloc_get_user_buffer_offset(
2867 &target_proc
->alloc
));
2868 *(binder_uintptr_t
*)(parent_buffer
+ bp
->parent_offset
) = bp
->buffer
;
2874 * binder_proc_transaction() - sends a transaction to a process and wakes it up
2875 * @t: transaction to send
2876 * @proc: process to send the transaction to
2877 * @thread: thread in @proc to send the transaction to (may be NULL)
2879 * This function queues a transaction to the specified process. It will try
2880 * to find a thread in the target process to handle the transaction and
2881 * wake it up. If no thread is found, the work is queued to the proc
2884 * If the @thread parameter is not NULL, the transaction is always queued
2885 * to the waitlist of that specific thread.
2887 * Return: true if the transactions was successfully queued
2888 * false if the target process or thread is dead
2890 static bool binder_proc_transaction(struct binder_transaction
*t
,
2891 struct binder_proc
*proc
,
2892 struct binder_thread
*thread
)
2894 struct binder_node
*node
= t
->buffer
->target_node
;
2895 struct binder_priority node_prio
;
2896 bool oneway
= !!(t
->flags
& TF_ONE_WAY
);
2897 bool pending_async
= false;
2900 binder_node_lock(node
);
2901 node_prio
.prio
= node
->min_priority
;
2902 node_prio
.sched_policy
= node
->sched_policy
;
2906 if (node
->has_async_transaction
) {
2907 pending_async
= true;
2909 node
->has_async_transaction
= true;
2913 binder_inner_proc_lock(proc
);
2915 if (proc
->is_dead
|| (thread
&& thread
->is_dead
)) {
2916 binder_inner_proc_unlock(proc
);
2917 binder_node_unlock(node
);
2921 if (!thread
&& !pending_async
)
2922 thread
= binder_select_thread_ilocked(proc
);
2925 binder_transaction_priority(thread
->task
, t
, node_prio
,
2927 binder_enqueue_thread_work_ilocked(thread
, &t
->work
);
2928 } else if (!pending_async
) {
2929 binder_enqueue_work_ilocked(&t
->work
, &proc
->todo
);
2931 binder_enqueue_work_ilocked(&t
->work
, &node
->async_todo
);
2935 binder_wakeup_thread_ilocked(proc
, thread
, !oneway
/* sync */);
2937 binder_inner_proc_unlock(proc
);
2938 binder_node_unlock(node
);
2944 * binder_get_node_refs_for_txn() - Get required refs on node for txn
2945 * @node: struct binder_node for which to get refs
2946 * @proc: returns @node->proc if valid
2947 * @error: if no @proc then returns BR_DEAD_REPLY
2949 * User-space normally keeps the node alive when creating a transaction
2950 * since it has a reference to the target. The local strong ref keeps it
2951 * alive if the sending process dies before the target process processes
2952 * the transaction. If the source process is malicious or has a reference
2953 * counting bug, relying on the local strong ref can fail.
2955 * Since user-space can cause the local strong ref to go away, we also take
2956 * a tmpref on the node to ensure it survives while we are constructing
2957 * the transaction. We also need a tmpref on the proc while we are
2958 * constructing the transaction, so we take that here as well.
2960 * Return: The target_node with refs taken or NULL if no @node->proc is NULL.
2961 * Also sets @proc if valid. If the @node->proc is NULL indicating that the
2962 * target proc has died, @error is set to BR_DEAD_REPLY
2964 static struct binder_node
*binder_get_node_refs_for_txn(
2965 struct binder_node
*node
,
2966 struct binder_proc
**procp
,
2969 struct binder_node
*target_node
= NULL
;
2971 binder_node_inner_lock(node
);
2974 binder_inc_node_nilocked(node
, 1, 0, NULL
);
2975 binder_inc_node_tmpref_ilocked(node
);
2976 node
->proc
->tmp_ref
++;
2977 *procp
= node
->proc
;
2979 *error
= BR_DEAD_REPLY
;
2980 binder_node_inner_unlock(node
);
2985 static void binder_transaction(struct binder_proc
*proc
,
2986 struct binder_thread
*thread
,
2987 struct binder_transaction_data
*tr
, int reply
,
2988 binder_size_t extra_buffers_size
)
2991 struct binder_transaction
*t
;
2992 struct binder_work
*tcomplete
;
2993 binder_size_t
*offp
, *off_end
, *off_start
;
2994 binder_size_t off_min
;
2995 u8
*sg_bufp
, *sg_buf_end
;
2996 struct binder_proc
*target_proc
= NULL
;
2997 struct binder_thread
*target_thread
= NULL
;
2998 struct binder_node
*target_node
= NULL
;
2999 struct binder_transaction
*in_reply_to
= NULL
;
3000 struct binder_transaction_log_entry
*e
;
3001 uint32_t return_error
= 0;
3002 uint32_t return_error_param
= 0;
3003 uint32_t return_error_line
= 0;
3004 struct binder_buffer_object
*last_fixup_obj
= NULL
;
3005 binder_size_t last_fixup_min_off
= 0;
3006 struct binder_context
*context
= proc
->context
;
3007 int t_debug_id
= atomic_inc_return(&binder_last_id
);
3008 char *secctx
= NULL
;
3011 e
= binder_transaction_log_add(&binder_transaction_log
);
3012 e
->debug_id
= t_debug_id
;
3013 e
->call_type
= reply
? 2 : !!(tr
->flags
& TF_ONE_WAY
);
3014 e
->from_proc
= proc
->pid
;
3015 e
->from_thread
= thread
->pid
;
3016 e
->target_handle
= tr
->target
.handle
;
3017 e
->data_size
= tr
->data_size
;
3018 e
->offsets_size
= tr
->offsets_size
;
3019 e
->context_name
= proc
->context
->name
;
3022 binder_inner_proc_lock(proc
);
3023 in_reply_to
= thread
->transaction_stack
;
3024 if (in_reply_to
== NULL
) {
3025 binder_inner_proc_unlock(proc
);
3026 binder_user_error("%d:%d(%s:%s) got reply transaction with no transaction stack\n",
3027 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3028 return_error
= BR_FAILED_REPLY
;
3029 return_error_param
= -EPROTO
;
3030 return_error_line
= __LINE__
;
3031 goto err_empty_call_stack
;
3033 if (in_reply_to
->to_thread
!= thread
) {
3034 spin_lock(&in_reply_to
->lock
);
3035 binder_user_error("%d:%d(%s:%s) got reply transaction with bad transaction stack, "\
3036 "transaction %d has target %d:%d(%s:%s)\n",
3037 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, in_reply_to
->debug_id
,
3038 in_reply_to
->to_proc
?
3039 in_reply_to
->to_proc
->pid
: 0,
3040 in_reply_to
->to_thread
?
3041 in_reply_to
->to_thread
->pid
: 0,
3042 in_reply_to
->to_proc
?
3043 in_reply_to
->to_proc
->tsk
->comm
: "",
3044 in_reply_to
->to_thread
?
3045 in_reply_to
->to_thread
->task
->comm
: "");
3046 spin_unlock(&in_reply_to
->lock
);
3047 binder_inner_proc_unlock(proc
);
3048 return_error
= BR_FAILED_REPLY
;
3049 return_error_param
= -EPROTO
;
3050 return_error_line
= __LINE__
;
3052 goto err_bad_call_stack
;
3054 thread
->transaction_stack
= in_reply_to
->to_parent
;
3055 binder_inner_proc_unlock(proc
);
3056 target_thread
= binder_get_txn_from_and_acq_inner(in_reply_to
);
3057 if (target_thread
== NULL
) {
3058 return_error
= BR_DEAD_REPLY
;
3059 return_error_line
= __LINE__
;
3060 goto err_dead_binder
;
3062 if (target_thread
->transaction_stack
!= in_reply_to
) {
3063 binder_user_error("%d:%d(%s:%s) got reply transaction with bad target transaction "\
3064 "stack %d, expected %d\n",
3065 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3066 target_thread
->transaction_stack
?
3067 target_thread
->transaction_stack
->debug_id
: 0,
3068 in_reply_to
->debug_id
);
3069 binder_inner_proc_unlock(target_thread
->proc
);
3070 return_error
= BR_FAILED_REPLY
;
3071 return_error_param
= -EPROTO
;
3072 return_error_line
= __LINE__
;
3074 target_thread
= NULL
;
3075 goto err_dead_binder
;
3077 target_proc
= target_thread
->proc
;
3078 target_proc
->tmp_ref
++;
3079 binder_inner_proc_unlock(target_thread
->proc
);
3081 if (tr
->target
.handle
) {
3082 struct binder_ref
*ref
;
3085 * There must already be a strong ref
3086 * on this node. If so, do a strong
3087 * increment on the node to ensure it
3088 * stays alive until the transaction is
3091 binder_proc_lock(proc
);
3092 ref
= binder_get_ref_olocked(proc
, tr
->target
.handle
,
3095 target_node
= binder_get_node_refs_for_txn(
3096 ref
->node
, &target_proc
,
3099 binder_user_error("%d:%d(%s:%s) got transaction to invalid handle\n",
3100 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3101 return_error
= BR_FAILED_REPLY
;
3103 binder_proc_unlock(proc
);
3105 mutex_lock(&context
->context_mgr_node_lock
);
3106 target_node
= context
->binder_context_mgr_node
;
3108 target_node
= binder_get_node_refs_for_txn(
3109 target_node
, &target_proc
,
3112 return_error
= BR_DEAD_REPLY
;
3113 mutex_unlock(&context
->context_mgr_node_lock
);
3114 if (target_node
&& target_proc
== proc
) {
3115 binder_user_error("%d:%d got transaction to context manager from process owning it\n",
3116 proc
->pid
, thread
->pid
);
3117 return_error
= BR_FAILED_REPLY
;
3118 return_error_param
= -EINVAL
;
3119 return_error_line
= __LINE__
;
3120 goto err_invalid_target_handle
;
3125 * return_error is set above
3127 return_error_param
= -EINVAL
;
3128 return_error_line
= __LINE__
;
3129 goto err_dead_binder
;
3131 e
->to_node
= target_node
->debug_id
;
3132 if (security_binder_transaction(proc
->tsk
,
3133 target_proc
->tsk
) < 0) {
3134 return_error
= BR_FAILED_REPLY
;
3135 return_error_param
= -EPERM
;
3136 return_error_line
= __LINE__
;
3137 goto err_invalid_target_handle
;
3139 binder_inner_proc_lock(proc
);
3140 if (!(tr
->flags
& TF_ONE_WAY
) && thread
->transaction_stack
) {
3141 struct binder_transaction
*tmp
;
3143 tmp
= thread
->transaction_stack
;
3144 if (tmp
->to_thread
!= thread
) {
3145 spin_lock(&tmp
->lock
);
3146 binder_user_error("%d:%d(%s:%s) got new transaction with bad transaction stack, "\
3147 "transaction %d has target %d:%d(%s:%s)\n",
3148 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, tmp
->debug_id
,
3149 tmp
->to_proc
? tmp
->to_proc
->pid
: 0,
3151 tmp
->to_thread
->pid
: 0,
3152 tmp
->to_proc
? tmp
->to_proc
->tsk
->comm
: "",
3153 tmp
->to_thread
? tmp
->to_thread
->task
->comm
: "");
3154 spin_unlock(&tmp
->lock
);
3155 binder_inner_proc_unlock(proc
);
3156 return_error
= BR_FAILED_REPLY
;
3157 return_error_param
= -EPROTO
;
3158 return_error_line
= __LINE__
;
3159 goto err_bad_call_stack
;
3162 struct binder_thread
*from
;
3164 spin_lock(&tmp
->lock
);
3166 if (from
&& from
->proc
== target_proc
) {
3167 atomic_inc(&from
->tmp_ref
);
3168 target_thread
= from
;
3169 spin_unlock(&tmp
->lock
);
3172 spin_unlock(&tmp
->lock
);
3173 tmp
= tmp
->from_parent
;
3176 binder_inner_proc_unlock(proc
);
3178 if (target_thread
) {
3179 e
->to_thread
= target_thread
->pid
;
3181 e
->to_proc
= target_proc
->pid
;
3183 /* TODO: reuse incoming transaction for reply */
3184 t
= kzalloc(sizeof(*t
), GFP_KERNEL
);
3186 return_error
= BR_FAILED_REPLY
;
3187 return_error_param
= -ENOMEM
;
3188 return_error_line
= __LINE__
;
3189 goto err_alloc_t_failed
;
3191 binder_stats_created(BINDER_STAT_TRANSACTION
);
3192 spin_lock_init(&t
->lock
);
3194 tcomplete
= kzalloc(sizeof(*tcomplete
), GFP_KERNEL
);
3195 if (tcomplete
== NULL
) {
3196 return_error
= BR_FAILED_REPLY
;
3197 return_error_param
= -ENOMEM
;
3198 return_error_line
= __LINE__
;
3199 goto err_alloc_tcomplete_failed
;
3201 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE
);
3203 t
->debug_id
= t_debug_id
;
3206 binder_debug(BINDER_DEBUG_TRANSACTION
,
3207 "%d:%d(%s:%s) BC_REPLY %d -> %d:%d (%s:%s), data %016llx-%016llx size %lld-%lld-%lld\n",
3208 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, t
->debug_id
,
3209 target_proc
->pid
, target_thread
->pid
, target_proc
->tsk
->comm
, target_thread
->task
->comm
,
3210 (u64
)tr
->data
.ptr
.buffer
,
3211 (u64
)tr
->data
.ptr
.offsets
,
3212 (u64
)tr
->data_size
, (u64
)tr
->offsets_size
,
3213 (u64
)extra_buffers_size
);
3215 binder_debug(BINDER_DEBUG_TRANSACTION
,
3216 "%d:%d(%s:%s) BC_TRANSACTION %d -> %d (%s) - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
3217 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, t
->debug_id
,
3218 target_proc
->pid
, target_proc
->tsk
->comm
, target_node
->debug_id
,
3219 (u64
)tr
->data
.ptr
.buffer
,
3220 (u64
)tr
->data
.ptr
.offsets
,
3221 (u64
)tr
->data_size
, (u64
)tr
->offsets_size
,
3222 (u64
)extra_buffers_size
);
3224 if (!reply
&& !(tr
->flags
& TF_ONE_WAY
))
3228 t
->sender_euid
= task_euid(proc
->tsk
);
3229 t
->to_proc
= target_proc
;
3230 t
->to_thread
= target_thread
;
3232 t
->flags
= tr
->flags
;
3233 if (!(t
->flags
& TF_ONE_WAY
) &&
3234 binder_supported_policy(current
->policy
)) {
3235 /* Inherit supported policies for synchronous transactions */
3236 t
->priority
.sched_policy
= current
->policy
;
3237 t
->priority
.prio
= current
->normal_prio
;
3239 /* Otherwise, fall back to the default priority */
3240 t
->priority
= target_proc
->default_priority
;
3243 if (target_node
&& target_node
->txn_security_ctx
) {
3247 security_task_getsecid(proc
->tsk
, &secid
);
3248 ret
= security_secid_to_secctx(secid
, &secctx
, &secctx_sz
);
3250 return_error
= BR_FAILED_REPLY
;
3251 return_error_param
= ret
;
3252 return_error_line
= __LINE__
;
3253 goto err_get_secctx_failed
;
3255 added_size
= ALIGN(secctx_sz
, sizeof(u64
));
3256 extra_buffers_size
+= added_size
;
3257 if (extra_buffers_size
< added_size
) {
3258 /* integer overflow of extra_buffers_size */
3259 return_error
= BR_FAILED_REPLY
;
3260 return_error_param
= EINVAL
;
3261 return_error_line
= __LINE__
;
3262 goto err_bad_extra_size
;
3266 #ifdef CONFIG_DEBUG_SNAPSHOT_BINDER
3267 dss_binder_transaction(reply
, t
, t
->from
? t
->from
: thread
, target_node
? target_node
->debug_id
: 0);
3269 trace_binder_transaction(reply
, t
, target_node
);
3271 t
->buffer
= binder_alloc_new_buf(&target_proc
->alloc
, tr
->data_size
,
3272 tr
->offsets_size
, extra_buffers_size
,
3273 !reply
&& (t
->flags
& TF_ONE_WAY
));
3274 if (IS_ERR(t
->buffer
)) {
3276 * -ESRCH indicates VMA cleared. The target is dying.
3278 return_error_param
= PTR_ERR(t
->buffer
);
3279 return_error
= return_error_param
== -ESRCH
?
3280 BR_DEAD_REPLY
: BR_FAILED_REPLY
;
3281 return_error_line
= __LINE__
;
3283 goto err_binder_alloc_buf_failed
;
3286 size_t buf_offset
= ALIGN(tr
->data_size
, sizeof(void *)) +
3287 ALIGN(tr
->offsets_size
, sizeof(void *)) +
3288 ALIGN(extra_buffers_size
, sizeof(void *)) -
3289 ALIGN(secctx_sz
, sizeof(u64
));
3290 char *kptr
= t
->buffer
->data
+ buf_offset
;
3292 t
->security_ctx
= (uintptr_t)kptr
+
3293 binder_alloc_get_user_buffer_offset(&target_proc
->alloc
);
3294 memcpy(kptr
, secctx
, secctx_sz
);
3295 security_release_secctx(secctx
, secctx_sz
);
3298 t
->buffer
->debug_id
= t
->debug_id
;
3299 t
->buffer
->transaction
= t
;
3300 t
->buffer
->target_node
= target_node
;
3301 trace_binder_transaction_alloc_buf(t
->buffer
);
3302 off_start
= (binder_size_t
*)(t
->buffer
->data
+
3303 ALIGN(tr
->data_size
, sizeof(void *)));
3306 if (copy_from_user(t
->buffer
->data
, (const void __user
*)(uintptr_t)
3307 tr
->data
.ptr
.buffer
, tr
->data_size
)) {
3308 binder_user_error("%d:%d(%s:%s) got transaction with invalid data ptr\n",
3309 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3310 return_error
= BR_FAILED_REPLY
;
3311 return_error_param
= -EFAULT
;
3312 return_error_line
= __LINE__
;
3313 goto err_copy_data_failed
;
3315 if (copy_from_user(offp
, (const void __user
*)(uintptr_t)
3316 tr
->data
.ptr
.offsets
, tr
->offsets_size
)) {
3317 binder_user_error("%d:%d(%s:%s) got transaction with invalid offsets ptr\n",
3318 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3319 return_error
= BR_FAILED_REPLY
;
3320 return_error_param
= -EFAULT
;
3321 return_error_line
= __LINE__
;
3322 goto err_copy_data_failed
;
3324 if (!IS_ALIGNED(tr
->offsets_size
, sizeof(binder_size_t
))) {
3325 binder_user_error("%d:%d(%s:%s) got transaction with invalid offsets size, %lld\n",
3326 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, (u64
)tr
->offsets_size
);
3327 return_error
= BR_FAILED_REPLY
;
3328 return_error_param
= -EINVAL
;
3329 return_error_line
= __LINE__
;
3330 goto err_bad_offset
;
3332 if (!IS_ALIGNED(extra_buffers_size
, sizeof(u64
))) {
3333 binder_user_error("%d:%d(%s:%s) got transaction with unaligned buffers size, %lld\n",
3334 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3335 (u64
)extra_buffers_size
);
3336 return_error
= BR_FAILED_REPLY
;
3337 return_error_param
= -EINVAL
;
3338 return_error_line
= __LINE__
;
3339 goto err_bad_offset
;
3341 off_end
= (void *)off_start
+ tr
->offsets_size
;
3342 sg_bufp
= (u8
*)(PTR_ALIGN(off_end
, sizeof(void *)));
3343 sg_buf_end
= sg_bufp
+ extra_buffers_size
-
3344 ALIGN(secctx_sz
, sizeof(u64
));
3346 for (; offp
< off_end
; offp
++) {
3347 struct binder_object_header
*hdr
;
3348 size_t object_size
= binder_validate_object(t
->buffer
, *offp
);
3350 if (object_size
== 0 || *offp
< off_min
) {
3351 binder_user_error("%d:%d(%s:%s) got transaction with invalid offset (%lld, min %lld max %lld)"\
3353 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3356 (u64
)t
->buffer
->data_size
);
3357 return_error
= BR_FAILED_REPLY
;
3358 return_error_param
= -EINVAL
;
3359 return_error_line
= __LINE__
;
3360 goto err_bad_offset
;
3363 hdr
= (struct binder_object_header
*)(t
->buffer
->data
+ *offp
);
3364 off_min
= *offp
+ object_size
;
3365 switch (hdr
->type
) {
3366 case BINDER_TYPE_BINDER
:
3367 case BINDER_TYPE_WEAK_BINDER
: {
3368 struct flat_binder_object
*fp
;
3370 fp
= to_flat_binder_object(hdr
);
3371 ret
= binder_translate_binder(fp
, t
, thread
);
3373 return_error
= BR_FAILED_REPLY
;
3374 return_error_param
= ret
;
3375 return_error_line
= __LINE__
;
3376 goto err_translate_failed
;
3379 case BINDER_TYPE_HANDLE
:
3380 case BINDER_TYPE_WEAK_HANDLE
: {
3381 struct flat_binder_object
*fp
;
3383 fp
= to_flat_binder_object(hdr
);
3384 ret
= binder_translate_handle(fp
, t
, thread
);
3386 return_error
= BR_FAILED_REPLY
;
3387 return_error_param
= ret
;
3388 return_error_line
= __LINE__
;
3389 goto err_translate_failed
;
3393 case BINDER_TYPE_FD
: {
3394 struct binder_fd_object
*fp
= to_binder_fd_object(hdr
);
3395 int target_fd
= binder_translate_fd(fp
->fd
, t
, thread
,
3398 if (target_fd
< 0) {
3399 return_error
= BR_FAILED_REPLY
;
3400 return_error_param
= target_fd
;
3401 return_error_line
= __LINE__
;
3402 goto err_translate_failed
;
3407 case BINDER_TYPE_FDA
: {
3408 struct binder_fd_array_object
*fda
=
3409 to_binder_fd_array_object(hdr
);
3410 struct binder_buffer_object
*parent
=
3411 binder_validate_ptr(t
->buffer
, fda
->parent
,
3415 binder_user_error("%d:%d(%s:%s) got transaction with invalid parent offset or type\n",
3416 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3417 return_error
= BR_FAILED_REPLY
;
3418 return_error_param
= -EINVAL
;
3419 return_error_line
= __LINE__
;
3420 goto err_bad_parent
;
3422 if (!binder_validate_fixup(t
->buffer
, off_start
,
3423 parent
, fda
->parent_offset
,
3425 last_fixup_min_off
)) {
3426 binder_user_error("%d:%d(%s:%s) got transaction with out-of-order buffer fixup\n",
3427 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3428 return_error
= BR_FAILED_REPLY
;
3429 return_error_param
= -EINVAL
;
3430 return_error_line
= __LINE__
;
3431 goto err_bad_parent
;
3433 ret
= binder_translate_fd_array(fda
, parent
, t
, thread
,
3436 return_error
= BR_FAILED_REPLY
;
3437 return_error_param
= ret
;
3438 return_error_line
= __LINE__
;
3439 goto err_translate_failed
;
3441 last_fixup_obj
= parent
;
3442 last_fixup_min_off
=
3443 fda
->parent_offset
+ sizeof(u32
) * fda
->num_fds
;
3445 case BINDER_TYPE_PTR
: {
3446 struct binder_buffer_object
*bp
=
3447 to_binder_buffer_object(hdr
);
3448 size_t buf_left
= sg_buf_end
- sg_bufp
;
3450 if (bp
->length
> buf_left
) {
3451 binder_user_error("%d:%d(%s:%s) got transaction with too large buffer\n",
3452 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3453 return_error
= BR_FAILED_REPLY
;
3454 return_error_param
= -EINVAL
;
3455 return_error_line
= __LINE__
;
3456 goto err_bad_offset
;
3458 if (copy_from_user(sg_bufp
,
3459 (const void __user
*)(uintptr_t)
3460 bp
->buffer
, bp
->length
)) {
3461 binder_user_error("%d:%d(%s:%s) got transaction with invalid offsets ptr\n",
3462 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3463 return_error_param
= -EFAULT
;
3464 return_error
= BR_FAILED_REPLY
;
3465 return_error_line
= __LINE__
;
3466 goto err_copy_data_failed
;
3468 /* Fixup buffer pointer to target proc address space */
3469 bp
->buffer
= (uintptr_t)sg_bufp
+
3470 binder_alloc_get_user_buffer_offset(
3471 &target_proc
->alloc
);
3472 sg_bufp
+= ALIGN(bp
->length
, sizeof(u64
));
3474 ret
= binder_fixup_parent(t
, thread
, bp
, off_start
,
3477 last_fixup_min_off
);
3479 return_error
= BR_FAILED_REPLY
;
3480 return_error_param
= ret
;
3481 return_error_line
= __LINE__
;
3482 goto err_translate_failed
;
3484 last_fixup_obj
= bp
;
3485 last_fixup_min_off
= 0;
3488 binder_user_error("%d:%d(%s:%s) got transaction with invalid object type, %x\n",
3489 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, hdr
->type
);
3490 return_error
= BR_FAILED_REPLY
;
3491 return_error_param
= -EINVAL
;
3492 return_error_line
= __LINE__
;
3493 goto err_bad_object_type
;
3496 tcomplete
->type
= BINDER_WORK_TRANSACTION_COMPLETE
;
3497 t
->work
.type
= BINDER_WORK_TRANSACTION
;
3500 binder_enqueue_thread_work(thread
, tcomplete
);
3501 binder_inner_proc_lock(target_proc
);
3502 if (target_thread
->is_dead
) {
3503 binder_inner_proc_unlock(target_proc
);
3504 goto err_dead_proc_or_thread
;
3506 BUG_ON(t
->buffer
->async_transaction
!= 0);
3507 binder_pop_transaction_ilocked(target_thread
, in_reply_to
);
3508 binder_enqueue_thread_work_ilocked(target_thread
, &t
->work
);
3509 binder_inner_proc_unlock(target_proc
);
3510 wake_up_interruptible_sync(&target_thread
->wait
);
3511 binder_restore_priority(current
, in_reply_to
->saved_priority
);
3512 binder_free_transaction(in_reply_to
);
3513 } else if (!(t
->flags
& TF_ONE_WAY
)) {
3514 BUG_ON(t
->buffer
->async_transaction
!= 0);
3515 binder_inner_proc_lock(proc
);
3517 * Defer the TRANSACTION_COMPLETE, so we don't return to
3518 * userspace immediately; this allows the target process to
3519 * immediately start processing this transaction, reducing
3520 * latency. We will then return the TRANSACTION_COMPLETE when
3521 * the target replies (or there is an error).
3523 binder_enqueue_deferred_thread_work_ilocked(thread
, tcomplete
);
3525 t
->from_parent
= thread
->transaction_stack
;
3526 thread
->transaction_stack
= t
;
3527 binder_inner_proc_unlock(proc
);
3528 if (!binder_proc_transaction(t
, target_proc
, target_thread
)) {
3529 binder_inner_proc_lock(proc
);
3530 binder_pop_transaction_ilocked(thread
, t
);
3531 binder_inner_proc_unlock(proc
);
3532 goto err_dead_proc_or_thread
;
3535 BUG_ON(target_node
== NULL
);
3536 BUG_ON(t
->buffer
->async_transaction
!= 1);
3537 binder_enqueue_thread_work(thread
, tcomplete
);
3538 if (!binder_proc_transaction(t
, target_proc
, NULL
))
3539 goto err_dead_proc_or_thread
;
3542 binder_thread_dec_tmpref(target_thread
);
3543 binder_proc_dec_tmpref(target_proc
);
3545 binder_dec_node_tmpref(target_node
);
3547 * write barrier to synchronize with initialization
3551 WRITE_ONCE(e
->debug_id_done
, t_debug_id
);
3554 err_dead_proc_or_thread
:
3555 return_error
= BR_DEAD_REPLY
;
3556 return_error_line
= __LINE__
;
3557 binder_dequeue_work(proc
, tcomplete
);
3558 err_translate_failed
:
3559 err_bad_object_type
:
3562 err_copy_data_failed
:
3563 trace_binder_transaction_failed_buffer_release(t
->buffer
);
3564 binder_transaction_buffer_release(target_proc
, t
->buffer
, offp
);
3566 binder_dec_node_tmpref(target_node
);
3568 t
->buffer
->transaction
= NULL
;
3569 binder_alloc_free_buf(&target_proc
->alloc
, t
->buffer
);
3570 err_binder_alloc_buf_failed
:
3573 security_release_secctx(secctx
, secctx_sz
);
3574 err_get_secctx_failed
:
3576 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE
);
3577 err_alloc_tcomplete_failed
:
3579 binder_stats_deleted(BINDER_STAT_TRANSACTION
);
3582 err_empty_call_stack
:
3584 err_invalid_target_handle
:
3586 binder_thread_dec_tmpref(target_thread
);
3588 binder_proc_dec_tmpref(target_proc
);
3590 binder_dec_node(target_node
, 1, 0);
3591 binder_dec_node_tmpref(target_node
);
3594 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
3595 "%d:%d(%s:%s) transaction failed %d/%d, size %lld-%lld line %d\n",
3596 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3597 return_error
, return_error_param
,
3598 (u64
)tr
->data_size
, (u64
)tr
->offsets_size
,
3602 struct binder_transaction_log_entry
*fe
;
3604 e
->return_error
= return_error
;
3605 e
->return_error_param
= return_error_param
;
3606 e
->return_error_line
= return_error_line
;
3607 #ifdef CONFIG_DEBUG_SNAPSHOT_BINDER
3608 dss_binder_transaction_failed(reply
, e
, proc
->tsk
->comm
, thread
->task
->comm
, tr
->flags
, tr
->code
);
3610 fe
= binder_transaction_log_add(&binder_transaction_log_failed
);
3613 * write barrier to synchronize with initialization
3617 WRITE_ONCE(e
->debug_id_done
, t_debug_id
);
3618 WRITE_ONCE(fe
->debug_id_done
, t_debug_id
);
3621 BUG_ON(thread
->return_error
.cmd
!= BR_OK
);
3623 binder_restore_priority(current
, in_reply_to
->saved_priority
);
3624 thread
->return_error
.cmd
= BR_TRANSACTION_COMPLETE
;
3625 binder_enqueue_thread_work(thread
, &thread
->return_error
.work
);
3626 binder_send_failed_reply(in_reply_to
, return_error
);
3628 thread
->return_error
.cmd
= return_error
;
3629 binder_enqueue_thread_work(thread
, &thread
->return_error
.work
);
3633 static int binder_thread_write(struct binder_proc
*proc
,
3634 struct binder_thread
*thread
,
3635 binder_uintptr_t binder_buffer
, size_t size
,
3636 binder_size_t
*consumed
)
3639 struct binder_context
*context
= proc
->context
;
3640 void __user
*buffer
= (void __user
*)(uintptr_t)binder_buffer
;
3641 void __user
*ptr
= buffer
+ *consumed
;
3642 void __user
*end
= buffer
+ size
;
3644 while (ptr
< end
&& thread
->return_error
.cmd
== BR_OK
) {
3647 if (get_user(cmd
, (uint32_t __user
*)ptr
))
3649 ptr
+= sizeof(uint32_t);
3650 trace_binder_command(cmd
);
3651 if (_IOC_NR(cmd
) < ARRAY_SIZE(binder_stats
.bc
)) {
3652 atomic_inc(&binder_stats
.bc
[_IOC_NR(cmd
)]);
3653 atomic_inc(&proc
->stats
.bc
[_IOC_NR(cmd
)]);
3654 atomic_inc(&thread
->stats
.bc
[_IOC_NR(cmd
)]);
3662 const char *debug_string
;
3663 bool strong
= cmd
== BC_ACQUIRE
|| cmd
== BC_RELEASE
;
3664 bool increment
= cmd
== BC_INCREFS
|| cmd
== BC_ACQUIRE
;
3665 struct binder_ref_data rdata
;
3667 if (get_user(target
, (uint32_t __user
*)ptr
))
3670 ptr
+= sizeof(uint32_t);
3672 if (increment
&& !target
) {
3673 struct binder_node
*ctx_mgr_node
;
3674 mutex_lock(&context
->context_mgr_node_lock
);
3675 ctx_mgr_node
= context
->binder_context_mgr_node
;
3677 ret
= binder_inc_ref_for_node(
3679 strong
, NULL
, &rdata
);
3680 mutex_unlock(&context
->context_mgr_node_lock
);
3683 ret
= binder_update_ref_for_handle(
3684 proc
, target
, increment
, strong
,
3686 if (!ret
&& rdata
.desc
!= target
) {
3687 binder_user_error("%d:%d(%s:%s) tried to acquire reference to"\
3688 " desc %d, got %d instead\n",
3689 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3690 target
, rdata
.desc
);
3694 debug_string
= "IncRefs";
3697 debug_string
= "Acquire";
3700 debug_string
= "Release";
3704 debug_string
= "DecRefs";
3708 binder_user_error("%d:%d(%s:%s) %s %d refcount change on invalid ref %d ret %d\n",
3709 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3710 debug_string
, strong
, target
, ret
);
3713 binder_debug(BINDER_DEBUG_USER_REFS
,
3714 "%d:%d(%s:%s) %s ref %d desc %d s %d w %d\n",
3715 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3716 debug_string
, rdata
.debug_id
, rdata
.desc
, rdata
.strong
,
3720 case BC_INCREFS_DONE
:
3721 case BC_ACQUIRE_DONE
: {
3722 binder_uintptr_t node_ptr
;
3723 binder_uintptr_t cookie
;
3724 struct binder_node
*node
;
3727 if (get_user(node_ptr
, (binder_uintptr_t __user
*)ptr
))
3729 ptr
+= sizeof(binder_uintptr_t
);
3730 if (get_user(cookie
, (binder_uintptr_t __user
*)ptr
))
3732 ptr
+= sizeof(binder_uintptr_t
);
3733 node
= binder_get_node(proc
, node_ptr
);
3735 binder_user_error("%d:%d(%s:%s) %s u%016llx no match\n",
3736 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3737 cmd
== BC_INCREFS_DONE
?
3743 if (cookie
!= node
->cookie
) {
3744 binder_user_error("%d:%d(%s:%s) %s u%016llx node %d cookie mismatch"\
3745 " %016llx != %016llx\n",
3746 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3747 cmd
== BC_INCREFS_DONE
?
3748 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
3749 (u64
)node_ptr
, node
->debug_id
,
3750 (u64
)cookie
, (u64
)node
->cookie
);
3751 binder_put_node(node
);
3754 binder_node_inner_lock(node
);
3755 if (cmd
== BC_ACQUIRE_DONE
) {
3756 if (node
->pending_strong_ref
== 0) {
3757 binder_user_error("%d:%d(%s:%s) BC_ACQUIRE_DONE node %d"\
3758 " has no pending acquire request\n",
3759 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3761 binder_node_inner_unlock(node
);
3762 binder_put_node(node
);
3765 node
->pending_strong_ref
= 0;
3767 if (node
->pending_weak_ref
== 0) {
3768 binder_user_error("%d:%d(%s:%s) BC_INCREFS_DONE node %d"\
3769 " has no pending increfs request\n",
3770 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3772 binder_node_inner_unlock(node
);
3773 binder_put_node(node
);
3776 node
->pending_weak_ref
= 0;
3778 free_node
= binder_dec_node_nilocked(node
,
3779 cmd
== BC_ACQUIRE_DONE
, 0);
3781 binder_debug(BINDER_DEBUG_USER_REFS
,
3782 "%d:%d(%s:%s) %s node %d ls %d lw %d tr %d\n",
3783 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3784 cmd
== BC_INCREFS_DONE
? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
3785 node
->debug_id
, node
->local_strong_refs
,
3786 node
->local_weak_refs
, node
->tmp_refs
);
3787 binder_node_inner_unlock(node
);
3788 binder_put_node(node
);
3791 case BC_ATTEMPT_ACQUIRE
:
3792 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
3794 case BC_ACQUIRE_RESULT
:
3795 pr_err("BC_ACQUIRE_RESULT not supported\n");
3798 case BC_FREE_BUFFER
: {
3799 binder_uintptr_t data_ptr
;
3800 struct binder_buffer
*buffer
;
3802 if (get_user(data_ptr
, (binder_uintptr_t __user
*)ptr
))
3804 ptr
+= sizeof(binder_uintptr_t
);
3806 buffer
= binder_alloc_prepare_to_free(&proc
->alloc
,
3808 if (IS_ERR_OR_NULL(buffer
)) {
3809 if (PTR_ERR(buffer
) == -EPERM
) {
3811 "%d:%d(%s:%s) BC_FREE_BUFFER u%016llx matched unreturned or currently freeing buffer\n",
3812 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3816 "%d:%d(%s:%s) BC_FREE_BUFFER u%016llx no match\n",
3817 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3822 binder_debug(BINDER_DEBUG_FREE_BUFFER
,
3823 "%d:%d(%s:%s) BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
3824 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, (u64
)data_ptr
,
3826 buffer
->transaction
? "active" : "finished");
3828 binder_inner_proc_lock(proc
);
3829 if (buffer
->transaction
) {
3830 buffer
->transaction
->buffer
= NULL
;
3831 buffer
->transaction
= NULL
;
3833 binder_inner_proc_unlock(proc
);
3834 if (buffer
->async_transaction
&& buffer
->target_node
) {
3835 struct binder_node
*buf_node
;
3836 struct binder_work
*w
;
3838 buf_node
= buffer
->target_node
;
3839 binder_node_inner_lock(buf_node
);
3840 BUG_ON(!buf_node
->has_async_transaction
);
3841 BUG_ON(buf_node
->proc
!= proc
);
3842 w
= binder_dequeue_work_head_ilocked(
3843 &buf_node
->async_todo
);
3845 buf_node
->has_async_transaction
= false;
3847 binder_enqueue_work_ilocked(
3849 binder_wakeup_proc_ilocked(proc
);
3851 binder_node_inner_unlock(buf_node
);
3853 trace_binder_transaction_buffer_release(buffer
);
3854 binder_transaction_buffer_release(proc
, buffer
, NULL
);
3855 binder_alloc_free_buf(&proc
->alloc
, buffer
);
3859 case BC_TRANSACTION_SG
:
3861 struct binder_transaction_data_sg tr
;
3863 if (copy_from_user(&tr
, ptr
, sizeof(tr
)))
3866 binder_transaction(proc
, thread
, &tr
.transaction_data
,
3867 cmd
== BC_REPLY_SG
, tr
.buffers_size
);
3870 case BC_TRANSACTION
:
3872 struct binder_transaction_data tr
;
3874 if (copy_from_user(&tr
, ptr
, sizeof(tr
)))
3877 binder_transaction(proc
, thread
, &tr
,
3878 cmd
== BC_REPLY
, 0);
3882 case BC_REGISTER_LOOPER
:
3883 binder_debug(BINDER_DEBUG_THREADS
,
3884 "%d:%d(%s:%s) BC_REGISTER_LOOPER\n",
3885 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3886 binder_inner_proc_lock(proc
);
3887 if (thread
->looper
& BINDER_LOOPER_STATE_ENTERED
) {
3888 thread
->looper
|= BINDER_LOOPER_STATE_INVALID
;
3889 binder_user_error("%d:%d(%s:%s) ERROR: BC_REGISTER_LOOPER"\
3890 " called after BC_ENTER_LOOPER\n",
3891 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3892 } else if (proc
->requested_threads
== 0) {
3893 thread
->looper
|= BINDER_LOOPER_STATE_INVALID
;
3894 binder_user_error("%d:%d(%s:%s) ERROR: BC_REGISTER_LOOPER called without request\n",
3895 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3897 proc
->requested_threads
--;
3898 proc
->requested_threads_started
++;
3900 thread
->looper
|= BINDER_LOOPER_STATE_REGISTERED
;
3901 binder_inner_proc_unlock(proc
);
3903 case BC_ENTER_LOOPER
:
3904 binder_debug(BINDER_DEBUG_THREADS
,
3905 "%d:%d(%s:%s) BC_ENTER_LOOPER\n",
3906 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3907 if (thread
->looper
& BINDER_LOOPER_STATE_REGISTERED
) {
3908 thread
->looper
|= BINDER_LOOPER_STATE_INVALID
;
3909 binder_user_error("%d:%d(%s:%s) ERROR: BC_ENTER_LOOPER"\
3910 " called after BC_REGISTER_LOOPER\n",
3911 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3913 thread
->looper
|= BINDER_LOOPER_STATE_ENTERED
;
3915 case BC_EXIT_LOOPER
:
3916 binder_debug(BINDER_DEBUG_THREADS
,
3917 "%d:%d(%s:%s) BC_EXIT_LOOPER\n",
3918 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3919 thread
->looper
|= BINDER_LOOPER_STATE_EXITED
;
3922 case BC_REQUEST_DEATH_NOTIFICATION
:
3923 case BC_CLEAR_DEATH_NOTIFICATION
: {
3925 binder_uintptr_t cookie
;
3926 struct binder_ref
*ref
;
3927 struct binder_ref_death
*death
= NULL
;
3929 if (get_user(target
, (uint32_t __user
*)ptr
))
3931 ptr
+= sizeof(uint32_t);
3932 if (get_user(cookie
, (binder_uintptr_t __user
*)ptr
))
3934 ptr
+= sizeof(binder_uintptr_t
);
3935 if (cmd
== BC_REQUEST_DEATH_NOTIFICATION
) {
3937 * Allocate memory for death notification
3938 * before taking lock
3940 death
= kzalloc(sizeof(*death
), GFP_KERNEL
);
3941 if (death
== NULL
) {
3942 WARN_ON(thread
->return_error
.cmd
!=
3944 thread
->return_error
.cmd
= BR_ERROR
;
3945 binder_enqueue_thread_work(
3947 &thread
->return_error
.work
);
3949 BINDER_DEBUG_FAILED_TRANSACTION
,
3950 "%d:%d(%s:%s) BC_REQUEST_DEATH_NOTIFICATION failed\n",
3951 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3955 binder_proc_lock(proc
);
3956 ref
= binder_get_ref_olocked(proc
, target
, false);
3958 binder_user_error("%d:%d(%s:%s) %s invalid ref %d\n",
3959 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3960 cmd
== BC_REQUEST_DEATH_NOTIFICATION
?
3961 "BC_REQUEST_DEATH_NOTIFICATION" :
3962 "BC_CLEAR_DEATH_NOTIFICATION",
3964 binder_proc_unlock(proc
);
3969 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION
,
3970 "%d:%d(%s:%s) %s %016llx ref %d desc %d s %d w %d for node %d\n",
3971 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3972 cmd
== BC_REQUEST_DEATH_NOTIFICATION
?
3973 "BC_REQUEST_DEATH_NOTIFICATION" :
3974 "BC_CLEAR_DEATH_NOTIFICATION",
3975 (u64
)cookie
, ref
->data
.debug_id
,
3976 ref
->data
.desc
, ref
->data
.strong
,
3977 ref
->data
.weak
, ref
->node
->debug_id
);
3979 binder_node_lock(ref
->node
);
3980 if (cmd
== BC_REQUEST_DEATH_NOTIFICATION
) {
3982 binder_user_error("%d:%d(%s:%s) BC_REQUEST_DEATH_NOTIFICATION"\
3983 " death notification already set\n",
3984 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3985 binder_node_unlock(ref
->node
);
3986 binder_proc_unlock(proc
);
3990 binder_stats_created(BINDER_STAT_DEATH
);
3991 INIT_LIST_HEAD(&death
->work
.entry
);
3992 death
->cookie
= cookie
;
3994 if (ref
->node
->proc
== NULL
) {
3995 ref
->death
->work
.type
= BINDER_WORK_DEAD_BINDER
;
3997 binder_inner_proc_lock(proc
);
3998 binder_enqueue_work_ilocked(
3999 &ref
->death
->work
, &proc
->todo
);
4000 binder_wakeup_proc_ilocked(proc
);
4001 binder_inner_proc_unlock(proc
);
4004 if (ref
->death
== NULL
) {
4005 binder_user_error("%d:%d(%s:%s) BC_CLEAR_DEATH_NOTIFICATION"\
4006 " death notification not active\n",
4007 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
4008 binder_node_unlock(ref
->node
);
4009 binder_proc_unlock(proc
);
4013 if (death
->cookie
!= cookie
) {
4014 binder_user_error("%d:%d(%s:%s) BC_CLEAR_DEATH_NOTIFICATION"\
4015 " death notification cookie mismatch %016llx != %016llx\n",
4016 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
4019 binder_node_unlock(ref
->node
);
4020 binder_proc_unlock(proc
);
4024 binder_inner_proc_lock(proc
);
4025 if (list_empty(&death
->work
.entry
)) {
4026 death
->work
.type
= BINDER_WORK_CLEAR_DEATH_NOTIFICATION
;
4027 if (thread
->looper
&
4028 (BINDER_LOOPER_STATE_REGISTERED
|
4029 BINDER_LOOPER_STATE_ENTERED
))
4030 binder_enqueue_thread_work_ilocked(
4034 binder_enqueue_work_ilocked(
4037 binder_wakeup_proc_ilocked(
4041 BUG_ON(death
->work
.type
!= BINDER_WORK_DEAD_BINDER
);
4042 death
->work
.type
= BINDER_WORK_DEAD_BINDER_AND_CLEAR
;
4044 binder_inner_proc_unlock(proc
);
4046 binder_node_unlock(ref
->node
);
4047 binder_proc_unlock(proc
);
4049 case BC_DEAD_BINDER_DONE
: {
4050 struct binder_work
*w
;
4051 binder_uintptr_t cookie
;
4052 struct binder_ref_death
*death
= NULL
;
4054 if (get_user(cookie
, (binder_uintptr_t __user
*)ptr
))
4057 ptr
+= sizeof(cookie
);
4058 binder_inner_proc_lock(proc
);
4059 list_for_each_entry(w
, &proc
->delivered_death
,
4061 struct binder_ref_death
*tmp_death
=
4063 struct binder_ref_death
,
4066 if (tmp_death
->cookie
== cookie
) {
4071 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
4072 "%d:%d(%s:%s) BC_DEAD_BINDER_DONE %016llx found %pK\n",
4073 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, (u64
)cookie
,
4075 if (death
== NULL
) {
4076 binder_user_error("%d:%d(%s:%s) BC_DEAD_BINDER_DONE %016llx not found\n",
4077 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, (u64
)cookie
);
4078 binder_inner_proc_unlock(proc
);
4081 binder_dequeue_work_ilocked(&death
->work
);
4082 if (death
->work
.type
== BINDER_WORK_DEAD_BINDER_AND_CLEAR
) {
4083 death
->work
.type
= BINDER_WORK_CLEAR_DEATH_NOTIFICATION
;
4084 if (thread
->looper
&
4085 (BINDER_LOOPER_STATE_REGISTERED
|
4086 BINDER_LOOPER_STATE_ENTERED
))
4087 binder_enqueue_thread_work_ilocked(
4088 thread
, &death
->work
);
4090 binder_enqueue_work_ilocked(
4093 binder_wakeup_proc_ilocked(proc
);
4096 binder_inner_proc_unlock(proc
);
4100 pr_err("%d:%d(%s:%s) unknown command %d\n",
4101 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, cmd
);
4104 *consumed
= ptr
- buffer
;
4109 static void binder_stat_br(struct binder_proc
*proc
,
4110 struct binder_thread
*thread
, uint32_t cmd
)
4112 trace_binder_return(cmd
);
4113 if (_IOC_NR(cmd
) < ARRAY_SIZE(binder_stats
.br
)) {
4114 atomic_inc(&binder_stats
.br
[_IOC_NR(cmd
)]);
4115 atomic_inc(&proc
->stats
.br
[_IOC_NR(cmd
)]);
4116 atomic_inc(&thread
->stats
.br
[_IOC_NR(cmd
)]);
4120 static int binder_put_node_cmd(struct binder_proc
*proc
,
4121 struct binder_thread
*thread
,
4123 binder_uintptr_t node_ptr
,
4124 binder_uintptr_t node_cookie
,
4126 uint32_t cmd
, const char *cmd_name
)
4128 void __user
*ptr
= *ptrp
;
4130 if (put_user(cmd
, (uint32_t __user
*)ptr
))
4132 ptr
+= sizeof(uint32_t);
4134 if (put_user(node_ptr
, (binder_uintptr_t __user
*)ptr
))
4136 ptr
+= sizeof(binder_uintptr_t
);
4138 if (put_user(node_cookie
, (binder_uintptr_t __user
*)ptr
))
4140 ptr
+= sizeof(binder_uintptr_t
);
4142 binder_stat_br(proc
, thread
, cmd
);
4143 binder_debug(BINDER_DEBUG_USER_REFS
, "%d:%d(%s:%s) %s %d u%016llx c%016llx\n",
4144 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
4145 cmd_name
, node_debug_id
,
4146 (u64
)node_ptr
, (u64
)node_cookie
);
4152 static int binder_wait_for_work(struct binder_thread
*thread
,
4156 struct binder_proc
*proc
= thread
->proc
;
4159 freezer_do_not_count();
4160 binder_inner_proc_lock(proc
);
4162 prepare_to_wait(&thread
->wait
, &wait
, TASK_INTERRUPTIBLE
);
4163 if (binder_has_work_ilocked(thread
, do_proc_work
))
4166 list_add(&thread
->waiting_thread_node
,
4167 &proc
->waiting_threads
);
4168 binder_inner_proc_unlock(proc
);
4170 binder_inner_proc_lock(proc
);
4171 list_del_init(&thread
->waiting_thread_node
);
4172 if (signal_pending(current
)) {
4177 finish_wait(&thread
->wait
, &wait
);
4178 binder_inner_proc_unlock(proc
);
4184 static int binder_thread_read(struct binder_proc
*proc
,
4185 struct binder_thread
*thread
,
4186 binder_uintptr_t binder_buffer
, size_t size
,
4187 binder_size_t
*consumed
, int non_block
)
4189 void __user
*buffer
= (void __user
*)(uintptr_t)binder_buffer
;
4190 void __user
*ptr
= buffer
+ *consumed
;
4191 void __user
*end
= buffer
+ size
;
4194 int wait_for_proc_work
;
4196 if (*consumed
== 0) {
4197 if (put_user(BR_NOOP
, (uint32_t __user
*)ptr
))
4199 ptr
+= sizeof(uint32_t);
4203 binder_inner_proc_lock(proc
);
4204 wait_for_proc_work
= binder_available_for_proc_work_ilocked(thread
);
4205 binder_inner_proc_unlock(proc
);
4207 thread
->looper
|= BINDER_LOOPER_STATE_WAITING
;
4209 trace_binder_wait_for_work(wait_for_proc_work
,
4210 !!thread
->transaction_stack
,
4211 !binder_worklist_empty(proc
, &thread
->todo
));
4212 if (wait_for_proc_work
) {
4213 if (!(thread
->looper
& (BINDER_LOOPER_STATE_REGISTERED
|
4214 BINDER_LOOPER_STATE_ENTERED
))) {
4215 binder_user_error("%d:%d(%s:%s) ERROR: Thread waiting for process work"\
4216 " before calling BC_REGISTER_LOOPER or BC_ENTER_LOOPER (state %x)\n",
4217 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, thread
->looper
);
4218 wait_event_interruptible(binder_user_error_wait
,
4219 binder_stop_on_user_error
< 2);
4221 binder_restore_priority(current
, proc
->default_priority
);
4225 if (!binder_has_work(thread
, wait_for_proc_work
))
4228 ret
= binder_wait_for_work(thread
, wait_for_proc_work
);
4231 thread
->looper
&= ~BINDER_LOOPER_STATE_WAITING
;
4238 struct binder_transaction_data_secctx tr
;
4239 struct binder_transaction_data
*trd
= &tr
.transaction_data
;
4240 struct binder_work
*w
= NULL
;
4241 struct list_head
*list
= NULL
;
4242 struct binder_transaction
*t
= NULL
;
4243 struct binder_thread
*t_from
;
4244 size_t trsize
= sizeof(*trd
);
4246 binder_inner_proc_lock(proc
);
4247 if (!binder_worklist_empty_ilocked(&thread
->todo
))
4248 list
= &thread
->todo
;
4249 else if (!binder_worklist_empty_ilocked(&proc
->todo
) &&
4253 binder_inner_proc_unlock(proc
);
4256 if (ptr
- buffer
== 4 && !thread
->looper_need_return
)
4261 if (end
- ptr
< sizeof(tr
) + 4) {
4262 binder_inner_proc_unlock(proc
);
4265 w
= binder_dequeue_work_head_ilocked(list
);
4266 if (binder_worklist_empty_ilocked(&thread
->todo
))
4267 thread
->process_todo
= false;
4270 case BINDER_WORK_TRANSACTION
: {
4271 binder_inner_proc_unlock(proc
);
4272 t
= container_of(w
, struct binder_transaction
, work
);
4274 case BINDER_WORK_RETURN_ERROR
: {
4275 struct binder_error
*e
= container_of(
4276 w
, struct binder_error
, work
);
4278 WARN_ON(e
->cmd
== BR_OK
);
4279 binder_inner_proc_unlock(proc
);
4280 if (put_user(e
->cmd
, (uint32_t __user
*)ptr
))
4284 ptr
+= sizeof(uint32_t);
4286 binder_stat_br(proc
, thread
, e
->cmd
);
4288 case BINDER_WORK_TRANSACTION_COMPLETE
: {
4289 binder_inner_proc_unlock(proc
);
4290 cmd
= BR_TRANSACTION_COMPLETE
;
4291 if (put_user(cmd
, (uint32_t __user
*)ptr
))
4293 ptr
+= sizeof(uint32_t);
4295 binder_stat_br(proc
, thread
, cmd
);
4296 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE
,
4297 "%d:%d(%s:%s) BR_TRANSACTION_COMPLETE\n",
4298 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
4300 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE
);
4302 case BINDER_WORK_NODE
: {
4303 struct binder_node
*node
= container_of(w
, struct binder_node
, work
);
4305 binder_uintptr_t node_ptr
= node
->ptr
;
4306 binder_uintptr_t node_cookie
= node
->cookie
;
4307 int node_debug_id
= node
->debug_id
;
4310 void __user
*orig_ptr
= ptr
;
4312 BUG_ON(proc
!= node
->proc
);
4313 strong
= node
->internal_strong_refs
||
4314 node
->local_strong_refs
;
4315 weak
= !hlist_empty(&node
->refs
) ||
4316 node
->local_weak_refs
||
4317 node
->tmp_refs
|| strong
;
4318 has_strong_ref
= node
->has_strong_ref
;
4319 has_weak_ref
= node
->has_weak_ref
;
4321 if (weak
&& !has_weak_ref
) {
4322 node
->has_weak_ref
= 1;
4323 node
->pending_weak_ref
= 1;
4324 node
->local_weak_refs
++;
4326 if (strong
&& !has_strong_ref
) {
4327 node
->has_strong_ref
= 1;
4328 node
->pending_strong_ref
= 1;
4329 node
->local_strong_refs
++;
4331 if (!strong
&& has_strong_ref
)
4332 node
->has_strong_ref
= 0;
4333 if (!weak
&& has_weak_ref
)
4334 node
->has_weak_ref
= 0;
4335 if (!weak
&& !strong
) {
4336 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
4337 "%d:%d(%s:%s) node %d u%016llx c%016llx deleted\n",
4338 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
4342 rb_erase(&node
->rb_node
, &proc
->nodes
);
4343 binder_inner_proc_unlock(proc
);
4344 binder_node_lock(node
);
4346 * Acquire the node lock before freeing the
4347 * node to serialize with other threads that
4348 * may have been holding the node lock while
4349 * decrementing this node (avoids race where
4350 * this thread frees while the other thread
4351 * is unlocking the node after the final
4354 binder_node_unlock(node
);
4355 binder_free_node(node
);
4357 binder_inner_proc_unlock(proc
);
4359 if (weak
&& !has_weak_ref
)
4360 ret
= binder_put_node_cmd(
4361 proc
, thread
, &ptr
, node_ptr
,
4362 node_cookie
, node_debug_id
,
4363 BR_INCREFS
, "BR_INCREFS");
4364 if (!ret
&& strong
&& !has_strong_ref
)
4365 ret
= binder_put_node_cmd(
4366 proc
, thread
, &ptr
, node_ptr
,
4367 node_cookie
, node_debug_id
,
4368 BR_ACQUIRE
, "BR_ACQUIRE");
4369 if (!ret
&& !strong
&& has_strong_ref
)
4370 ret
= binder_put_node_cmd(
4371 proc
, thread
, &ptr
, node_ptr
,
4372 node_cookie
, node_debug_id
,
4373 BR_RELEASE
, "BR_RELEASE");
4374 if (!ret
&& !weak
&& has_weak_ref
)
4375 ret
= binder_put_node_cmd(
4376 proc
, thread
, &ptr
, node_ptr
,
4377 node_cookie
, node_debug_id
,
4378 BR_DECREFS
, "BR_DECREFS");
4379 if (orig_ptr
== ptr
)
4380 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
4381 "%d:%d(%s:%s) node %d u%016llx c%016llx state unchanged\n",
4382 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
4389 case BINDER_WORK_DEAD_BINDER
:
4390 case BINDER_WORK_DEAD_BINDER_AND_CLEAR
:
4391 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION
: {
4392 struct binder_ref_death
*death
;
4394 binder_uintptr_t cookie
;
4396 death
= container_of(w
, struct binder_ref_death
, work
);
4397 if (w
->type
== BINDER_WORK_CLEAR_DEATH_NOTIFICATION
)
4398 cmd
= BR_CLEAR_DEATH_NOTIFICATION_DONE
;
4400 cmd
= BR_DEAD_BINDER
;
4401 cookie
= death
->cookie
;
4403 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION
,
4404 "%d:%d(%s:%s) %s %016llx\n",
4405 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
4406 cmd
== BR_DEAD_BINDER
?
4408 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
4410 if (w
->type
== BINDER_WORK_CLEAR_DEATH_NOTIFICATION
) {
4411 binder_inner_proc_unlock(proc
);
4413 binder_stats_deleted(BINDER_STAT_DEATH
);
4415 binder_enqueue_work_ilocked(
4416 w
, &proc
->delivered_death
);
4417 binder_inner_proc_unlock(proc
);
4419 if (put_user(cmd
, (uint32_t __user
*)ptr
))
4421 ptr
+= sizeof(uint32_t);
4422 if (put_user(cookie
,
4423 (binder_uintptr_t __user
*)ptr
))
4425 ptr
+= sizeof(binder_uintptr_t
);
4426 binder_stat_br(proc
, thread
, cmd
);
4427 if (cmd
== BR_DEAD_BINDER
)
4428 goto done
; /* DEAD_BINDER notifications can cause transactions */
4435 BUG_ON(t
->buffer
== NULL
);
4436 if (t
->buffer
->target_node
) {
4437 struct binder_node
*target_node
= t
->buffer
->target_node
;
4438 struct binder_priority node_prio
;
4440 trd
->target
.ptr
= target_node
->ptr
;
4441 trd
->cookie
= target_node
->cookie
;
4442 node_prio
.sched_policy
= target_node
->sched_policy
;
4443 node_prio
.prio
= target_node
->min_priority
;
4444 binder_transaction_priority(current
, t
, node_prio
,
4445 target_node
->inherit_rt
);
4446 cmd
= BR_TRANSACTION
;
4448 trd
->target
.ptr
= 0;
4452 trd
->code
= t
->code
;
4453 trd
->flags
= t
->flags
;
4454 trd
->sender_euid
= from_kuid(current_user_ns(), t
->sender_euid
);
4456 t_from
= binder_get_txn_from(t
);
4458 struct task_struct
*sender
= t_from
->proc
->tsk
;
4461 task_tgid_nr_ns(sender
,
4462 task_active_pid_ns(current
));
4464 trd
->sender_pid
= 0;
4467 trd
->data_size
= t
->buffer
->data_size
;
4468 trd
->offsets_size
= t
->buffer
->offsets_size
;
4469 trd
->data
.ptr
.buffer
= (binder_uintptr_t
)
4470 ((uintptr_t)t
->buffer
->data
+
4471 binder_alloc_get_user_buffer_offset(&proc
->alloc
));
4472 trd
->data
.ptr
.offsets
= trd
->data
.ptr
.buffer
+
4473 ALIGN(t
->buffer
->data_size
,
4476 tr
.secctx
= t
->security_ctx
;
4477 if (t
->security_ctx
) {
4478 cmd
= BR_TRANSACTION_SEC_CTX
;
4479 trsize
= sizeof(tr
);
4481 if (put_user(cmd
, (uint32_t __user
*)ptr
)) {
4483 binder_thread_dec_tmpref(t_from
);
4485 binder_cleanup_transaction(t
, "put_user failed",
4490 ptr
+= sizeof(uint32_t);
4491 if (copy_to_user(ptr
, &tr
, trsize
)) {
4493 binder_thread_dec_tmpref(t_from
);
4495 binder_cleanup_transaction(t
, "copy_to_user failed",
4502 #ifdef CONFIG_DEBUG_SNAPSHOT_BINDER
4503 dss_binder_transaction_received(t
, thread
);
4505 trace_binder_transaction_received(t
);
4506 binder_stat_br(proc
, thread
, cmd
);
4507 binder_debug(BINDER_DEBUG_TRANSACTION
,
4508 "%d:%d(%s:%s) %s %d %d:%d(%s:%s), cmd %d size %zd-%zd ptr %016llx-%016llx\n",
4509 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
4510 (cmd
== BR_TRANSACTION
) ? "BR_TRANSACTION" :
4511 (cmd
== BR_TRANSACTION_SEC_CTX
) ?
4512 "BR_TRANSACTION_SEC_CTX" : "BR_REPLY",
4513 t
->debug_id
, t_from
? t_from
->proc
->pid
: 0,
4514 t_from
? t_from
->pid
: 0,
4515 t_from
? t_from
->proc
->tsk
->comm
: "",
4516 t_from
? t_from
->task
->comm
: "",
4518 t
->buffer
->data_size
, t
->buffer
->offsets_size
,
4519 (u64
)trd
->data
.ptr
.buffer
,
4520 (u64
)trd
->data
.ptr
.offsets
);
4523 binder_thread_dec_tmpref(t_from
);
4524 t
->buffer
->allow_user_free
= 1;
4525 if (cmd
!= BR_REPLY
&& !(t
->flags
& TF_ONE_WAY
)) {
4526 binder_inner_proc_lock(thread
->proc
);
4527 t
->to_parent
= thread
->transaction_stack
;
4528 t
->to_thread
= thread
;
4529 thread
->transaction_stack
= t
;
4530 binder_inner_proc_unlock(thread
->proc
);
4532 binder_free_transaction(t
);
4539 *consumed
= ptr
- buffer
;
4540 binder_inner_proc_lock(proc
);
4541 if (proc
->requested_threads
== 0 &&
4542 list_empty(&thread
->proc
->waiting_threads
) &&
4543 proc
->requested_threads_started
< proc
->max_threads
&&
4544 (thread
->looper
& (BINDER_LOOPER_STATE_REGISTERED
|
4545 BINDER_LOOPER_STATE_ENTERED
)) /* the user-space code fails to */
4546 /*spawn a new thread if we leave this out */) {
4547 proc
->requested_threads
++;
4548 binder_inner_proc_unlock(proc
);
4549 binder_debug(BINDER_DEBUG_THREADS
,
4550 "%d:%d(%s:%s) BR_SPAWN_LOOPER\n",
4551 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
4552 if (put_user(BR_SPAWN_LOOPER
, (uint32_t __user
*)buffer
))
4554 binder_stat_br(proc
, thread
, BR_SPAWN_LOOPER
);
4556 binder_inner_proc_unlock(proc
);
4560 static void binder_release_work(struct binder_proc
*proc
,
4561 struct list_head
*list
)
4563 struct binder_work
*w
;
4566 w
= binder_dequeue_work_head(proc
, list
);
4571 case BINDER_WORK_TRANSACTION
: {
4572 struct binder_transaction
*t
;
4574 t
= container_of(w
, struct binder_transaction
, work
);
4576 binder_cleanup_transaction(t
, "process died.",
4579 case BINDER_WORK_RETURN_ERROR
: {
4580 struct binder_error
*e
= container_of(
4581 w
, struct binder_error
, work
);
4583 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
4584 "undelivered TRANSACTION_ERROR: %u\n",
4587 case BINDER_WORK_TRANSACTION_COMPLETE
: {
4588 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
4589 "undelivered TRANSACTION_COMPLETE\n");
4591 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE
);
4593 case BINDER_WORK_DEAD_BINDER_AND_CLEAR
:
4594 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION
: {
4595 struct binder_ref_death
*death
;
4597 death
= container_of(w
, struct binder_ref_death
, work
);
4598 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
4599 "undelivered death notification, %016llx\n",
4600 (u64
)death
->cookie
);
4602 binder_stats_deleted(BINDER_STAT_DEATH
);
4605 pr_err("unexpected work type, %d, not freed\n",
4613 static struct binder_thread
*binder_get_thread_ilocked(
4614 struct binder_proc
*proc
, struct binder_thread
*new_thread
)
4616 struct binder_thread
*thread
= NULL
;
4617 struct rb_node
*parent
= NULL
;
4618 struct rb_node
**p
= &proc
->threads
.rb_node
;
4622 thread
= rb_entry(parent
, struct binder_thread
, rb_node
);
4624 if (current
->pid
< thread
->pid
)
4626 else if (current
->pid
> thread
->pid
)
4627 p
= &(*p
)->rb_right
;
4633 thread
= new_thread
;
4634 binder_stats_created(BINDER_STAT_THREAD
);
4635 thread
->proc
= proc
;
4636 thread
->pid
= current
->pid
;
4637 get_task_struct(current
);
4638 thread
->task
= current
;
4639 atomic_set(&thread
->tmp_ref
, 0);
4640 init_waitqueue_head(&thread
->wait
);
4641 INIT_LIST_HEAD(&thread
->todo
);
4642 rb_link_node(&thread
->rb_node
, parent
, p
);
4643 rb_insert_color(&thread
->rb_node
, &proc
->threads
);
4644 thread
->looper_need_return
= true;
4645 thread
->return_error
.work
.type
= BINDER_WORK_RETURN_ERROR
;
4646 thread
->return_error
.cmd
= BR_OK
;
4647 thread
->reply_error
.work
.type
= BINDER_WORK_RETURN_ERROR
;
4648 thread
->reply_error
.cmd
= BR_OK
;
4649 INIT_LIST_HEAD(&new_thread
->waiting_thread_node
);
4653 static struct binder_thread
*binder_get_thread(struct binder_proc
*proc
)
4655 struct binder_thread
*thread
;
4656 struct binder_thread
*new_thread
;
4658 binder_inner_proc_lock(proc
);
4659 thread
= binder_get_thread_ilocked(proc
, NULL
);
4660 binder_inner_proc_unlock(proc
);
4662 new_thread
= kzalloc(sizeof(*thread
), GFP_KERNEL
);
4663 if (new_thread
== NULL
)
4665 binder_inner_proc_lock(proc
);
4666 thread
= binder_get_thread_ilocked(proc
, new_thread
);
4667 binder_inner_proc_unlock(proc
);
4668 if (thread
!= new_thread
)
4674 static void binder_free_proc(struct binder_proc
*proc
)
4676 BUG_ON(!list_empty(&proc
->todo
));
4677 BUG_ON(!list_empty(&proc
->delivered_death
));
4678 binder_alloc_deferred_release(&proc
->alloc
);
4679 put_task_struct(proc
->tsk
);
4680 binder_stats_deleted(BINDER_STAT_PROC
);
4684 static void binder_free_thread(struct binder_thread
*thread
)
4686 BUG_ON(!list_empty(&thread
->todo
));
4687 binder_stats_deleted(BINDER_STAT_THREAD
);
4688 binder_proc_dec_tmpref(thread
->proc
);
4689 put_task_struct(thread
->task
);
4693 static int binder_thread_release(struct binder_proc
*proc
,
4694 struct binder_thread
*thread
)
4696 struct binder_transaction
*t
;
4697 struct binder_transaction
*send_reply
= NULL
;
4698 int active_transactions
= 0;
4699 struct binder_transaction
*last_t
= NULL
;
4701 binder_inner_proc_lock(thread
->proc
);
4703 * take a ref on the proc so it survives
4704 * after we remove this thread from proc->threads.
4705 * The corresponding dec is when we actually
4706 * free the thread in binder_free_thread()
4710 * take a ref on this thread to ensure it
4711 * survives while we are releasing it
4713 atomic_inc(&thread
->tmp_ref
);
4714 rb_erase(&thread
->rb_node
, &proc
->threads
);
4715 t
= thread
->transaction_stack
;
4717 spin_lock(&t
->lock
);
4718 if (t
->to_thread
== thread
)
4721 thread
->is_dead
= true;
4725 active_transactions
++;
4726 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
4727 "release %d:%d(%s:%s) transaction %d %s, still active\n",
4728 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
4730 (t
->to_thread
== thread
) ? "in" : "out");
4732 if (t
->to_thread
== thread
) {
4734 t
->to_thread
= NULL
;
4736 t
->buffer
->transaction
= NULL
;
4740 } else if (t
->from
== thread
) {
4745 spin_unlock(&last_t
->lock
);
4747 spin_lock(&t
->lock
);
4751 * If this thread used poll, make sure we remove the waitqueue
4752 * from any epoll data structures holding it with POLLFREE.
4753 * waitqueue_active() is safe to use here because we're holding
4756 if ((thread
->looper
& BINDER_LOOPER_STATE_POLL
) &&
4757 waitqueue_active(&thread
->wait
)) {
4758 wake_up_poll(&thread
->wait
, POLLHUP
| POLLFREE
);
4761 binder_inner_proc_unlock(thread
->proc
);
4764 * This is needed to avoid races between wake_up_poll() above and
4765 * and ep_remove_waitqueue() called for other reasons (eg the epoll file
4766 * descriptor being closed); ep_remove_waitqueue() holds an RCU read
4767 * lock, so we can be sure it's done after calling synchronize_rcu().
4769 if (thread
->looper
& BINDER_LOOPER_STATE_POLL
)
4773 binder_send_failed_reply(send_reply
, BR_DEAD_REPLY
);
4774 binder_release_work(proc
, &thread
->todo
);
4775 binder_thread_dec_tmpref(thread
);
4776 return active_transactions
;
4779 static unsigned int binder_poll(struct file
*filp
,
4780 struct poll_table_struct
*wait
)
4782 struct binder_proc
*proc
= filp
->private_data
;
4783 struct binder_thread
*thread
= NULL
;
4784 bool wait_for_proc_work
;
4786 thread
= binder_get_thread(proc
);
4790 binder_inner_proc_lock(thread
->proc
);
4791 thread
->looper
|= BINDER_LOOPER_STATE_POLL
;
4792 wait_for_proc_work
= binder_available_for_proc_work_ilocked(thread
);
4794 binder_inner_proc_unlock(thread
->proc
);
4796 poll_wait(filp
, &thread
->wait
, wait
);
4798 if (binder_has_work(thread
, wait_for_proc_work
))
4804 static int binder_ioctl_write_read(struct file
*filp
,
4805 unsigned int cmd
, unsigned long arg
,
4806 struct binder_thread
*thread
)
4809 struct binder_proc
*proc
= filp
->private_data
;
4810 unsigned int size
= _IOC_SIZE(cmd
);
4811 void __user
*ubuf
= (void __user
*)arg
;
4812 struct binder_write_read bwr
;
4814 if (size
!= sizeof(struct binder_write_read
)) {
4818 if (copy_from_user(&bwr
, ubuf
, sizeof(bwr
))) {
4822 binder_debug(BINDER_DEBUG_READ_WRITE
,
4823 "%d:%d(%s:%s) write %lld at %016llx, read %lld at %016llx\n",
4824 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
4825 (u64
)bwr
.write_size
, (u64
)bwr
.write_buffer
,
4826 (u64
)bwr
.read_size
, (u64
)bwr
.read_buffer
);
4828 if (bwr
.write_size
> 0) {
4829 ret
= binder_thread_write(proc
, thread
,
4832 &bwr
.write_consumed
);
4833 trace_binder_write_done(ret
);
4835 bwr
.read_consumed
= 0;
4836 if (copy_to_user(ubuf
, &bwr
, sizeof(bwr
)))
4841 if (bwr
.read_size
> 0) {
4842 ret
= binder_thread_read(proc
, thread
, bwr
.read_buffer
,
4845 filp
->f_flags
& O_NONBLOCK
);
4846 trace_binder_read_done(ret
);
4847 binder_inner_proc_lock(proc
);
4848 if (!binder_worklist_empty_ilocked(&proc
->todo
))
4849 binder_wakeup_proc_ilocked(proc
);
4850 binder_inner_proc_unlock(proc
);
4852 if (copy_to_user(ubuf
, &bwr
, sizeof(bwr
)))
4857 binder_debug(BINDER_DEBUG_READ_WRITE
,
4858 "%d:%d(%s:%s) wrote %lld of %lld, read return %lld of %lld\n",
4859 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
4860 (u64
)bwr
.write_consumed
, (u64
)bwr
.write_size
,
4861 (u64
)bwr
.read_consumed
, (u64
)bwr
.read_size
);
4862 if (copy_to_user(ubuf
, &bwr
, sizeof(bwr
))) {
4870 static int binder_ioctl_set_ctx_mgr(struct file
*filp
,
4871 struct flat_binder_object
*fbo
)
4874 struct binder_proc
*proc
= filp
->private_data
;
4875 struct binder_context
*context
= proc
->context
;
4876 struct binder_node
*new_node
;
4877 kuid_t curr_euid
= current_euid();
4879 mutex_lock(&context
->context_mgr_node_lock
);
4880 if (context
->binder_context_mgr_node
) {
4881 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
4885 ret
= security_binder_set_context_mgr(proc
->tsk
);
4888 if (uid_valid(context
->binder_context_mgr_uid
)) {
4889 if (!uid_eq(context
->binder_context_mgr_uid
, curr_euid
)) {
4890 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
4891 from_kuid(&init_user_ns
, curr_euid
),
4892 from_kuid(&init_user_ns
,
4893 context
->binder_context_mgr_uid
));
4898 context
->binder_context_mgr_uid
= curr_euid
;
4900 new_node
= binder_new_node(proc
, fbo
);
4905 binder_node_lock(new_node
);
4906 new_node
->local_weak_refs
++;
4907 new_node
->local_strong_refs
++;
4908 new_node
->has_strong_ref
= 1;
4909 new_node
->has_weak_ref
= 1;
4910 context
->binder_context_mgr_node
= new_node
;
4911 binder_node_unlock(new_node
);
4912 binder_put_node(new_node
);
4914 mutex_unlock(&context
->context_mgr_node_lock
);
4918 static int binder_ioctl_get_node_debug_info(struct binder_proc
*proc
,
4919 struct binder_node_debug_info
*info
)
4922 binder_uintptr_t ptr
= info
->ptr
;
4924 memset(info
, 0, sizeof(*info
));
4926 binder_inner_proc_lock(proc
);
4927 for (n
= rb_first(&proc
->nodes
); n
!= NULL
; n
= rb_next(n
)) {
4928 struct binder_node
*node
= rb_entry(n
, struct binder_node
,
4930 if (node
->ptr
> ptr
) {
4931 info
->ptr
= node
->ptr
;
4932 info
->cookie
= node
->cookie
;
4933 info
->has_strong_ref
= node
->has_strong_ref
;
4934 info
->has_weak_ref
= node
->has_weak_ref
;
4938 binder_inner_proc_unlock(proc
);
4943 static long binder_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
4946 struct binder_proc
*proc
= filp
->private_data
;
4947 struct binder_thread
*thread
;
4948 unsigned int size
= _IOC_SIZE(cmd
);
4949 void __user
*ubuf
= (void __user
*)arg
;
4951 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
4952 proc->pid, current->pid, cmd, arg);*/
4954 binder_selftest_alloc(&proc
->alloc
);
4956 trace_binder_ioctl(cmd
, arg
);
4958 ret
= wait_event_interruptible(binder_user_error_wait
, binder_stop_on_user_error
< 2);
4962 thread
= binder_get_thread(proc
);
4963 if (thread
== NULL
) {
4969 case BINDER_WRITE_READ
:
4970 ret
= binder_ioctl_write_read(filp
, cmd
, arg
, thread
);
4974 case BINDER_SET_MAX_THREADS
: {
4977 if (copy_from_user(&max_threads
, ubuf
,
4978 sizeof(max_threads
))) {
4982 binder_inner_proc_lock(proc
);
4983 proc
->max_threads
= max_threads
;
4984 binder_inner_proc_unlock(proc
);
4987 case BINDER_SET_CONTEXT_MGR_EXT
: {
4988 struct flat_binder_object fbo
;
4990 if (copy_from_user(&fbo
, ubuf
, sizeof(fbo
))) {
4994 ret
= binder_ioctl_set_ctx_mgr(filp
, &fbo
);
4999 case BINDER_SET_CONTEXT_MGR
:
5000 ret
= binder_ioctl_set_ctx_mgr(filp
, NULL
);
5004 case BINDER_THREAD_EXIT
:
5005 binder_debug(BINDER_DEBUG_THREADS
, "%d:%d(%s:%s) exit\n",
5006 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
5007 binder_thread_release(proc
, thread
);
5010 case BINDER_VERSION
: {
5011 struct binder_version __user
*ver
= ubuf
;
5013 if (size
!= sizeof(struct binder_version
)) {
5017 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION
,
5018 &ver
->protocol_version
)) {
5024 case BINDER_GET_NODE_DEBUG_INFO
: {
5025 struct binder_node_debug_info info
;
5027 if (copy_from_user(&info
, ubuf
, sizeof(info
))) {
5032 ret
= binder_ioctl_get_node_debug_info(proc
, &info
);
5036 if (copy_to_user(ubuf
, &info
, sizeof(info
))) {
5049 thread
->looper_need_return
= false;
5050 wait_event_interruptible(binder_user_error_wait
, binder_stop_on_user_error
< 2);
5051 if (ret
&& ret
!= -ERESTARTSYS
)
5052 pr_info("%d:%d(%s:%s) ioctl %x %lx returned %d\n", proc
->pid
, current
->pid
,
5053 proc
->tsk
->comm
, current
->comm
, cmd
, arg
, ret
);
5055 trace_binder_ioctl_done(ret
);
5059 static void binder_vma_open(struct vm_area_struct
*vma
)
5061 struct binder_proc
*proc
= vma
->vm_private_data
;
5063 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
5064 "%d(%s) open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
5065 proc
->pid
, proc
->tsk
->comm
, vma
->vm_start
, vma
->vm_end
,
5066 (vma
->vm_end
- vma
->vm_start
) / SZ_1K
, vma
->vm_flags
,
5067 (unsigned long)pgprot_val(vma
->vm_page_prot
));
5070 static void binder_vma_close(struct vm_area_struct
*vma
)
5072 struct binder_proc
*proc
= vma
->vm_private_data
;
5074 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
5075 "%d(%s) close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
5076 proc
->pid
, proc
->tsk
->comm
, vma
->vm_start
, vma
->vm_end
,
5077 (vma
->vm_end
- vma
->vm_start
) / SZ_1K
, vma
->vm_flags
,
5078 (unsigned long)pgprot_val(vma
->vm_page_prot
));
5079 binder_alloc_vma_close(&proc
->alloc
);
5080 binder_defer_work(proc
, BINDER_DEFERRED_PUT_FILES
);
5083 static int binder_vm_fault(struct vm_fault
*vmf
)
5085 return VM_FAULT_SIGBUS
;
5088 static const struct vm_operations_struct binder_vm_ops
= {
5089 .open
= binder_vma_open
,
5090 .close
= binder_vma_close
,
5091 .fault
= binder_vm_fault
,
5094 static int binder_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
5097 struct binder_proc
*proc
= filp
->private_data
;
5098 const char *failure_string
;
5100 if (proc
->tsk
!= current
->group_leader
)
5103 if ((vma
->vm_end
- vma
->vm_start
) > SZ_4M
)
5104 vma
->vm_end
= vma
->vm_start
+ SZ_4M
;
5106 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
5107 "%s: %d(%s) %lx-%lx (%ld K) vma %lx pagep %lx\n",
5108 __func__
, proc
->pid
, proc
->tsk
->comm
, vma
->vm_start
, vma
->vm_end
,
5109 (vma
->vm_end
- vma
->vm_start
) / SZ_1K
, vma
->vm_flags
,
5110 (unsigned long)pgprot_val(vma
->vm_page_prot
));
5112 if (vma
->vm_flags
& FORBIDDEN_MMAP_FLAGS
) {
5114 failure_string
= "bad vm_flags";
5117 vma
->vm_flags
|= VM_DONTCOPY
| VM_MIXEDMAP
;
5118 vma
->vm_flags
&= ~VM_MAYWRITE
;
5120 vma
->vm_ops
= &binder_vm_ops
;
5121 vma
->vm_private_data
= proc
;
5123 ret
= binder_alloc_mmap_handler(&proc
->alloc
, vma
);
5125 failure_string
= "";
5128 mutex_lock(&proc
->files_lock
);
5129 proc
->files
= get_files_struct(current
);
5130 mutex_unlock(&proc
->files_lock
);
5134 pr_err("%s: %d(%s) %lx-%lx %s failed %d\n", __func__
,
5135 proc
->pid
, proc
->tsk
->comm
, vma
->vm_start
, vma
->vm_end
, failure_string
, ret
);
5139 static int binder_open(struct inode
*nodp
, struct file
*filp
)
5141 struct binder_proc
*proc
;
5142 struct binder_device
*binder_dev
;
5144 binder_debug(BINDER_DEBUG_OPEN_CLOSE
, "%s: %d:%d (%s:%s)\n", __func__
,
5145 current
->group_leader
->pid
, current
->pid
,
5146 current
->group_leader
->comm
, current
->comm
);
5148 proc
= kzalloc(sizeof(*proc
), GFP_KERNEL
);
5151 spin_lock_init(&proc
->inner_lock
);
5152 spin_lock_init(&proc
->outer_lock
);
5153 get_task_struct(current
->group_leader
);
5154 proc
->tsk
= current
->group_leader
;
5155 mutex_init(&proc
->files_lock
);
5156 INIT_LIST_HEAD(&proc
->todo
);
5157 if (binder_supported_policy(current
->policy
)) {
5158 proc
->default_priority
.sched_policy
= current
->policy
;
5159 proc
->default_priority
.prio
= current
->normal_prio
;
5161 proc
->default_priority
.sched_policy
= SCHED_NORMAL
;
5162 proc
->default_priority
.prio
= NICE_TO_PRIO(0);
5165 binder_dev
= container_of(filp
->private_data
, struct binder_device
,
5167 proc
->context
= &binder_dev
->context
;
5168 binder_alloc_init(&proc
->alloc
);
5170 binder_stats_created(BINDER_STAT_PROC
);
5171 proc
->pid
= current
->group_leader
->pid
;
5172 INIT_LIST_HEAD(&proc
->delivered_death
);
5173 INIT_LIST_HEAD(&proc
->waiting_threads
);
5174 filp
->private_data
= proc
;
5176 mutex_lock(&binder_procs_lock
);
5177 hlist_add_head(&proc
->proc_node
, &binder_procs
);
5178 mutex_unlock(&binder_procs_lock
);
5180 if (binder_debugfs_dir_entry_proc
) {
5183 snprintf(strbuf
, sizeof(strbuf
), "%u", proc
->pid
);
5185 * proc debug entries are shared between contexts, so
5186 * this will fail if the process tries to open the driver
5187 * again with a different context. The priting code will
5188 * anyway print all contexts that a given PID has, so this
5191 proc
->debugfs_entry
= debugfs_create_file(strbuf
, 0444,
5192 binder_debugfs_dir_entry_proc
,
5193 (void *)(unsigned long)proc
->pid
,
5200 static int binder_flush(struct file
*filp
, fl_owner_t id
)
5202 struct binder_proc
*proc
= filp
->private_data
;
5204 binder_defer_work(proc
, BINDER_DEFERRED_FLUSH
);
5209 static void binder_deferred_flush(struct binder_proc
*proc
)
5214 binder_inner_proc_lock(proc
);
5215 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
)) {
5216 struct binder_thread
*thread
= rb_entry(n
, struct binder_thread
, rb_node
);
5218 thread
->looper_need_return
= true;
5219 if (thread
->looper
& BINDER_LOOPER_STATE_WAITING
) {
5220 wake_up_interruptible(&thread
->wait
);
5224 binder_inner_proc_unlock(proc
);
5226 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
5227 "binder_flush: %d(%s) woke %d threads\n", proc
->pid
, proc
->tsk
->comm
,
5231 static int binder_release(struct inode
*nodp
, struct file
*filp
)
5233 struct binder_proc
*proc
= filp
->private_data
;
5235 debugfs_remove(proc
->debugfs_entry
);
5236 binder_defer_work(proc
, BINDER_DEFERRED_RELEASE
);
5241 static int binder_node_release(struct binder_node
*node
, int refs
)
5243 struct binder_ref
*ref
;
5245 struct binder_proc
*proc
= node
->proc
;
5247 binder_release_work(proc
, &node
->async_todo
);
5249 binder_node_lock(node
);
5250 binder_inner_proc_lock(proc
);
5251 binder_dequeue_work_ilocked(&node
->work
);
5253 * The caller must have taken a temporary ref on the node,
5255 BUG_ON(!node
->tmp_refs
);
5256 if (hlist_empty(&node
->refs
) && node
->tmp_refs
== 1) {
5257 binder_inner_proc_unlock(proc
);
5258 binder_node_unlock(node
);
5259 binder_free_node(node
);
5265 node
->local_strong_refs
= 0;
5266 node
->local_weak_refs
= 0;
5267 binder_inner_proc_unlock(proc
);
5269 spin_lock(&binder_dead_nodes_lock
);
5270 hlist_add_head(&node
->dead_node
, &binder_dead_nodes
);
5271 spin_unlock(&binder_dead_nodes_lock
);
5273 hlist_for_each_entry(ref
, &node
->refs
, node_entry
) {
5276 * Need the node lock to synchronize
5277 * with new notification requests and the
5278 * inner lock to synchronize with queued
5279 * death notifications.
5281 binder_inner_proc_lock(ref
->proc
);
5283 binder_inner_proc_unlock(ref
->proc
);
5289 BUG_ON(!list_empty(&ref
->death
->work
.entry
));
5290 ref
->death
->work
.type
= BINDER_WORK_DEAD_BINDER
;
5291 binder_enqueue_work_ilocked(&ref
->death
->work
,
5293 binder_wakeup_proc_ilocked(ref
->proc
);
5294 binder_inner_proc_unlock(ref
->proc
);
5297 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
5298 "node %d now dead, refs %d, death %d\n",
5299 node
->debug_id
, refs
, death
);
5300 binder_node_unlock(node
);
5301 binder_put_node(node
);
5306 static void binder_deferred_release(struct binder_proc
*proc
)
5308 struct binder_context
*context
= proc
->context
;
5310 int threads
, nodes
, incoming_refs
, outgoing_refs
, active_transactions
;
5312 BUG_ON(proc
->files
);
5314 mutex_lock(&binder_procs_lock
);
5315 hlist_del(&proc
->proc_node
);
5316 mutex_unlock(&binder_procs_lock
);
5318 mutex_lock(&context
->context_mgr_node_lock
);
5319 if (context
->binder_context_mgr_node
&&
5320 context
->binder_context_mgr_node
->proc
== proc
) {
5321 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
5322 "%s: %d(%s) context_mgr_node gone\n",
5323 __func__
, proc
->pid
, proc
->tsk
->comm
);
5324 context
->binder_context_mgr_node
= NULL
;
5326 mutex_unlock(&context
->context_mgr_node_lock
);
5327 binder_inner_proc_lock(proc
);
5329 * Make sure proc stays alive after we
5330 * remove all the threads
5334 proc
->is_dead
= true;
5336 active_transactions
= 0;
5337 while ((n
= rb_first(&proc
->threads
))) {
5338 struct binder_thread
*thread
;
5340 thread
= rb_entry(n
, struct binder_thread
, rb_node
);
5341 binder_inner_proc_unlock(proc
);
5343 active_transactions
+= binder_thread_release(proc
, thread
);
5344 binder_inner_proc_lock(proc
);
5349 while ((n
= rb_first(&proc
->nodes
))) {
5350 struct binder_node
*node
;
5352 node
= rb_entry(n
, struct binder_node
, rb_node
);
5355 * take a temporary ref on the node before
5356 * calling binder_node_release() which will either
5357 * kfree() the node or call binder_put_node()
5359 binder_inc_node_tmpref_ilocked(node
);
5360 rb_erase(&node
->rb_node
, &proc
->nodes
);
5361 binder_inner_proc_unlock(proc
);
5362 incoming_refs
= binder_node_release(node
, incoming_refs
);
5363 binder_inner_proc_lock(proc
);
5365 binder_inner_proc_unlock(proc
);
5368 binder_proc_lock(proc
);
5369 while ((n
= rb_first(&proc
->refs_by_desc
))) {
5370 struct binder_ref
*ref
;
5372 ref
= rb_entry(n
, struct binder_ref
, rb_node_desc
);
5374 binder_cleanup_ref_olocked(ref
);
5375 binder_proc_unlock(proc
);
5376 binder_free_ref(ref
);
5377 binder_proc_lock(proc
);
5379 binder_proc_unlock(proc
);
5381 binder_release_work(proc
, &proc
->todo
);
5382 binder_release_work(proc
, &proc
->delivered_death
);
5384 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
5385 "%s: %d(%s) threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
5386 __func__
, proc
->pid
, proc
->tsk
->comm
, threads
, nodes
, incoming_refs
,
5387 outgoing_refs
, active_transactions
);
5389 binder_proc_dec_tmpref(proc
);
5392 static void binder_deferred_func(struct work_struct
*work
)
5394 struct binder_proc
*proc
;
5395 struct files_struct
*files
;
5400 mutex_lock(&binder_deferred_lock
);
5401 if (!hlist_empty(&binder_deferred_list
)) {
5402 proc
= hlist_entry(binder_deferred_list
.first
,
5403 struct binder_proc
, deferred_work_node
);
5404 hlist_del_init(&proc
->deferred_work_node
);
5405 defer
= proc
->deferred_work
;
5406 proc
->deferred_work
= 0;
5411 mutex_unlock(&binder_deferred_lock
);
5414 if (defer
& BINDER_DEFERRED_PUT_FILES
) {
5415 mutex_lock(&proc
->files_lock
);
5416 files
= proc
->files
;
5419 mutex_unlock(&proc
->files_lock
);
5422 if (defer
& BINDER_DEFERRED_FLUSH
)
5423 binder_deferred_flush(proc
);
5425 if (defer
& BINDER_DEFERRED_RELEASE
)
5426 binder_deferred_release(proc
); /* frees proc */
5429 put_files_struct(files
);
5432 static DECLARE_WORK(binder_deferred_work
, binder_deferred_func
);
5435 binder_defer_work(struct binder_proc
*proc
, enum binder_deferred_state defer
)
5437 mutex_lock(&binder_deferred_lock
);
5438 proc
->deferred_work
|= defer
;
5439 if (hlist_unhashed(&proc
->deferred_work_node
)) {
5440 hlist_add_head(&proc
->deferred_work_node
,
5441 &binder_deferred_list
);
5442 schedule_work(&binder_deferred_work
);
5444 mutex_unlock(&binder_deferred_lock
);
5447 static void print_binder_transaction_ilocked(struct seq_file
*m
,
5448 struct binder_proc
*proc
,
5450 struct binder_transaction
*t
)
5452 struct binder_proc
*to_proc
;
5453 struct binder_buffer
*buffer
= t
->buffer
;
5455 spin_lock(&t
->lock
);
5456 to_proc
= t
->to_proc
;
5458 "%s %d: %pK from %d:%d(%s:%s) to %d:%d(%s:%s) code %x flags %x pri %d:%d r%d",
5459 prefix
, t
->debug_id
, t
,
5460 t
->from
? t
->from
->proc
->pid
: 0,
5461 t
->from
? t
->from
->pid
: 0,
5462 t
->from
? t
->from
->proc
->tsk
->comm
: "",
5463 t
->from
? t
->from
->task
->comm
: "",
5464 to_proc
? to_proc
->pid
: 0,
5465 t
->to_thread
? t
->to_thread
->pid
: 0,
5466 to_proc
? to_proc
->tsk
->comm
: "",
5467 t
->to_thread
? t
->to_thread
->task
->comm
: "",
5468 t
->code
, t
->flags
, t
->priority
.sched_policy
,
5469 t
->priority
.prio
, t
->need_reply
);
5470 spin_unlock(&t
->lock
);
5472 if (proc
!= to_proc
) {
5474 * Can only safely deref buffer if we are holding the
5475 * correct proc inner lock for this node
5481 if (buffer
== NULL
) {
5482 seq_puts(m
, " buffer free\n");
5485 if (buffer
->target_node
)
5486 seq_printf(m
, " node %d", buffer
->target_node
->debug_id
);
5487 seq_printf(m
, " size %zd:%zd data %pK\n",
5488 buffer
->data_size
, buffer
->offsets_size
,
5492 static void print_binder_work_ilocked(struct seq_file
*m
,
5493 struct binder_proc
*proc
,
5495 const char *transaction_prefix
,
5496 struct binder_work
*w
)
5498 struct binder_node
*node
;
5499 struct binder_transaction
*t
;
5502 case BINDER_WORK_TRANSACTION
:
5503 t
= container_of(w
, struct binder_transaction
, work
);
5504 print_binder_transaction_ilocked(
5505 m
, proc
, transaction_prefix
, t
);
5507 case BINDER_WORK_RETURN_ERROR
: {
5508 struct binder_error
*e
= container_of(
5509 w
, struct binder_error
, work
);
5511 seq_printf(m
, "%stransaction error: %u\n",
5514 case BINDER_WORK_TRANSACTION_COMPLETE
:
5515 seq_printf(m
, "%stransaction complete\n", prefix
);
5517 case BINDER_WORK_NODE
:
5518 node
= container_of(w
, struct binder_node
, work
);
5519 seq_printf(m
, "%snode work %d: u%016llx c%016llx\n",
5520 prefix
, node
->debug_id
,
5521 (u64
)node
->ptr
, (u64
)node
->cookie
);
5523 case BINDER_WORK_DEAD_BINDER
:
5524 seq_printf(m
, "%shas dead binder\n", prefix
);
5526 case BINDER_WORK_DEAD_BINDER_AND_CLEAR
:
5527 seq_printf(m
, "%shas cleared dead binder\n", prefix
);
5529 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION
:
5530 seq_printf(m
, "%shas cleared death notification\n", prefix
);
5533 seq_printf(m
, "%sunknown work: type %d\n", prefix
, w
->type
);
5538 static void print_binder_thread_ilocked(struct seq_file
*m
,
5539 struct binder_thread
*thread
,
5542 struct binder_transaction
*t
;
5543 struct binder_work
*w
;
5544 size_t start_pos
= m
->count
;
5547 seq_printf(m
, " thread %d %s: l %02x need_return %d tr %d\n",
5548 thread
->pid
, thread
->task
->comm
, thread
->looper
,
5549 thread
->looper_need_return
,
5550 atomic_read(&thread
->tmp_ref
));
5551 header_pos
= m
->count
;
5552 t
= thread
->transaction_stack
;
5554 if (t
->from
== thread
) {
5555 print_binder_transaction_ilocked(m
, thread
->proc
,
5556 " outgoing transaction", t
);
5558 } else if (t
->to_thread
== thread
) {
5559 print_binder_transaction_ilocked(m
, thread
->proc
,
5560 " incoming transaction", t
);
5563 print_binder_transaction_ilocked(m
, thread
->proc
,
5564 " bad transaction", t
);
5568 list_for_each_entry(w
, &thread
->todo
, entry
) {
5569 print_binder_work_ilocked(m
, thread
->proc
, " ",
5570 " pending transaction", w
);
5572 if (!print_always
&& m
->count
== header_pos
)
5573 m
->count
= start_pos
;
5576 static void print_binder_node_nilocked(struct seq_file
*m
,
5577 struct binder_node
*node
)
5579 struct binder_ref
*ref
;
5580 struct binder_work
*w
;
5584 hlist_for_each_entry(ref
, &node
->refs
, node_entry
)
5587 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",
5588 node
->debug_id
, (u64
)node
->ptr
, (u64
)node
->cookie
,
5589 node
->sched_policy
, node
->min_priority
,
5590 node
->has_strong_ref
, node
->has_weak_ref
,
5591 node
->local_strong_refs
, node
->local_weak_refs
,
5592 node
->internal_strong_refs
, count
, node
->tmp_refs
);
5594 seq_puts(m
, " proc");
5595 hlist_for_each_entry(ref
, &node
->refs
, node_entry
)
5596 seq_printf(m
, " %d %s", ref
->proc
->pid
, ref
->proc
->tsk
->comm
);
5600 list_for_each_entry(w
, &node
->async_todo
, entry
)
5601 print_binder_work_ilocked(m
, node
->proc
, " ",
5602 " pending async transaction", w
);
5606 static void print_binder_ref_olocked(struct seq_file
*m
,
5607 struct binder_ref
*ref
)
5609 binder_node_lock(ref
->node
);
5610 seq_printf(m
, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
5611 ref
->data
.debug_id
, ref
->data
.desc
,
5612 ref
->node
->proc
? "" : "dead ",
5613 ref
->node
->debug_id
, ref
->data
.strong
,
5614 ref
->data
.weak
, ref
->death
);
5615 binder_node_unlock(ref
->node
);
5618 static void print_binder_proc(struct seq_file
*m
,
5619 struct binder_proc
*proc
, int print_all
)
5621 struct binder_work
*w
;
5623 size_t start_pos
= m
->count
;
5625 struct binder_node
*last_node
= NULL
;
5627 seq_printf(m
, "proc %d %s\n", proc
->pid
, proc
->tsk
->comm
);
5628 seq_printf(m
, "context %s\n", proc
->context
->name
);
5629 header_pos
= m
->count
;
5631 binder_inner_proc_lock(proc
);
5632 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
))
5633 print_binder_thread_ilocked(m
, rb_entry(n
, struct binder_thread
,
5634 rb_node
), print_all
);
5636 for (n
= rb_first(&proc
->nodes
); n
!= NULL
; n
= rb_next(n
)) {
5637 struct binder_node
*node
= rb_entry(n
, struct binder_node
,
5640 * take a temporary reference on the node so it
5641 * survives and isn't removed from the tree
5642 * while we print it.
5644 binder_inc_node_tmpref_ilocked(node
);
5645 /* Need to drop inner lock to take node lock */
5646 binder_inner_proc_unlock(proc
);
5648 binder_put_node(last_node
);
5649 binder_node_inner_lock(node
);
5650 print_binder_node_nilocked(m
, node
);
5651 binder_node_inner_unlock(node
);
5653 binder_inner_proc_lock(proc
);
5655 binder_inner_proc_unlock(proc
);
5657 binder_put_node(last_node
);
5660 binder_proc_lock(proc
);
5661 for (n
= rb_first(&proc
->refs_by_desc
);
5664 print_binder_ref_olocked(m
, rb_entry(n
,
5667 binder_proc_unlock(proc
);
5669 binder_alloc_print_allocated(m
, &proc
->alloc
);
5670 binder_inner_proc_lock(proc
);
5671 list_for_each_entry(w
, &proc
->todo
, entry
)
5672 print_binder_work_ilocked(m
, proc
, " ",
5673 " pending transaction", w
);
5674 list_for_each_entry(w
, &proc
->delivered_death
, entry
) {
5675 seq_puts(m
, " has delivered dead binder\n");
5678 binder_inner_proc_unlock(proc
);
5679 if (!print_all
&& m
->count
== header_pos
)
5680 m
->count
= start_pos
;
5683 static const char * const binder_return_strings
[] = {
5688 "BR_ACQUIRE_RESULT",
5690 "BR_TRANSACTION_COMPLETE",
5695 "BR_ATTEMPT_ACQUIRE",
5700 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
5704 static const char * const binder_command_strings
[] = {
5707 "BC_ACQUIRE_RESULT",
5715 "BC_ATTEMPT_ACQUIRE",
5716 "BC_REGISTER_LOOPER",
5719 "BC_REQUEST_DEATH_NOTIFICATION",
5720 "BC_CLEAR_DEATH_NOTIFICATION",
5721 "BC_DEAD_BINDER_DONE",
5722 "BC_TRANSACTION_SG",
5726 static const char * const binder_objstat_strings
[] = {
5733 "transaction_complete"
5736 static void print_binder_stats(struct seq_file
*m
, const char *prefix
,
5737 struct binder_stats
*stats
)
5741 BUILD_BUG_ON(ARRAY_SIZE(stats
->bc
) !=
5742 ARRAY_SIZE(binder_command_strings
));
5743 for (i
= 0; i
< ARRAY_SIZE(stats
->bc
); i
++) {
5744 int temp
= atomic_read(&stats
->bc
[i
]);
5747 seq_printf(m
, "%s%s: %d\n", prefix
,
5748 binder_command_strings
[i
], temp
);
5751 BUILD_BUG_ON(ARRAY_SIZE(stats
->br
) !=
5752 ARRAY_SIZE(binder_return_strings
));
5753 for (i
= 0; i
< ARRAY_SIZE(stats
->br
); i
++) {
5754 int temp
= atomic_read(&stats
->br
[i
]);
5757 seq_printf(m
, "%s%s: %d\n", prefix
,
5758 binder_return_strings
[i
], temp
);
5761 BUILD_BUG_ON(ARRAY_SIZE(stats
->obj_created
) !=
5762 ARRAY_SIZE(binder_objstat_strings
));
5763 BUILD_BUG_ON(ARRAY_SIZE(stats
->obj_created
) !=
5764 ARRAY_SIZE(stats
->obj_deleted
));
5765 for (i
= 0; i
< ARRAY_SIZE(stats
->obj_created
); i
++) {
5766 int created
= atomic_read(&stats
->obj_created
[i
]);
5767 int deleted
= atomic_read(&stats
->obj_deleted
[i
]);
5769 if (created
|| deleted
)
5770 seq_printf(m
, "%s%s: active %d total %d\n",
5772 binder_objstat_strings
[i
],
5778 static void print_binder_proc_stats(struct seq_file
*m
,
5779 struct binder_proc
*proc
)
5781 struct binder_work
*w
;
5782 struct binder_thread
*thread
;
5784 int count
, strong
, weak
, ready_threads
;
5785 size_t free_async_space
=
5786 binder_alloc_get_free_async_space(&proc
->alloc
);
5788 seq_printf(m
, "proc %d %s\n", proc
->pid
, proc
->tsk
->comm
);
5789 seq_printf(m
, "context %s\n", proc
->context
->name
);
5792 binder_inner_proc_lock(proc
);
5793 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
))
5796 list_for_each_entry(thread
, &proc
->waiting_threads
, waiting_thread_node
)
5799 seq_printf(m
, " threads: %d\n", count
);
5800 seq_printf(m
, " requested threads: %d+%d/%d\n"
5801 " ready threads %d\n"
5802 " free async space %zd\n", proc
->requested_threads
,
5803 proc
->requested_threads_started
, proc
->max_threads
,
5807 for (n
= rb_first(&proc
->nodes
); n
!= NULL
; n
= rb_next(n
))
5809 binder_inner_proc_unlock(proc
);
5810 seq_printf(m
, " nodes: %d\n", count
);
5814 binder_proc_lock(proc
);
5815 for (n
= rb_first(&proc
->refs_by_desc
); n
!= NULL
; n
= rb_next(n
)) {
5816 struct binder_ref
*ref
= rb_entry(n
, struct binder_ref
,
5819 strong
+= ref
->data
.strong
;
5820 weak
+= ref
->data
.weak
;
5822 binder_proc_unlock(proc
);
5823 seq_printf(m
, " refs: %d s %d w %d\n", count
, strong
, weak
);
5825 count
= binder_alloc_get_allocated_count(&proc
->alloc
);
5826 seq_printf(m
, " buffers: %d\n", count
);
5828 binder_alloc_print_pages(m
, &proc
->alloc
);
5831 binder_inner_proc_lock(proc
);
5832 list_for_each_entry(w
, &proc
->todo
, entry
) {
5833 if (w
->type
== BINDER_WORK_TRANSACTION
)
5836 binder_inner_proc_unlock(proc
);
5837 seq_printf(m
, " pending transactions: %d\n", count
);
5839 print_binder_stats(m
, " ", &proc
->stats
);
5843 static int binder_state_show(struct seq_file
*m
, void *unused
)
5845 struct binder_proc
*proc
;
5846 struct binder_node
*node
;
5847 struct binder_node
*last_node
= NULL
;
5849 seq_puts(m
, "binder state:\n");
5851 spin_lock(&binder_dead_nodes_lock
);
5852 if (!hlist_empty(&binder_dead_nodes
))
5853 seq_puts(m
, "dead nodes:\n");
5854 hlist_for_each_entry(node
, &binder_dead_nodes
, dead_node
) {
5856 * take a temporary reference on the node so it
5857 * survives and isn't removed from the list
5858 * while we print it.
5861 spin_unlock(&binder_dead_nodes_lock
);
5863 binder_put_node(last_node
);
5864 binder_node_lock(node
);
5865 print_binder_node_nilocked(m
, node
);
5866 binder_node_unlock(node
);
5868 spin_lock(&binder_dead_nodes_lock
);
5870 spin_unlock(&binder_dead_nodes_lock
);
5872 binder_put_node(last_node
);
5874 mutex_lock(&binder_procs_lock
);
5875 hlist_for_each_entry(proc
, &binder_procs
, proc_node
)
5876 print_binder_proc(m
, proc
, 1);
5877 mutex_unlock(&binder_procs_lock
);
5882 static int binder_stats_show(struct seq_file
*m
, void *unused
)
5884 struct binder_proc
*proc
;
5886 seq_puts(m
, "binder stats:\n");
5888 print_binder_stats(m
, "", &binder_stats
);
5890 mutex_lock(&binder_procs_lock
);
5891 hlist_for_each_entry(proc
, &binder_procs
, proc_node
)
5892 print_binder_proc_stats(m
, proc
);
5893 mutex_unlock(&binder_procs_lock
);
5898 static int binder_transactions_show(struct seq_file
*m
, void *unused
)
5900 struct binder_proc
*proc
;
5902 seq_puts(m
, "binder transactions:\n");
5903 mutex_lock(&binder_procs_lock
);
5904 hlist_for_each_entry(proc
, &binder_procs
, proc_node
)
5905 print_binder_proc(m
, proc
, 0);
5906 mutex_unlock(&binder_procs_lock
);
5911 static int binder_proc_show(struct seq_file
*m
, void *unused
)
5913 struct binder_proc
*itr
;
5914 int pid
= (unsigned long)m
->private;
5916 mutex_lock(&binder_procs_lock
);
5917 hlist_for_each_entry(itr
, &binder_procs
, proc_node
) {
5918 if (itr
->pid
== pid
) {
5919 seq_puts(m
, "binder proc state:\n");
5920 print_binder_proc(m
, itr
, 1);
5923 mutex_unlock(&binder_procs_lock
);
5928 static void print_binder_transaction_log_entry(struct seq_file
*m
,
5929 struct binder_transaction_log_entry
*e
)
5931 int debug_id
= READ_ONCE(e
->debug_id_done
);
5933 * read barrier to guarantee debug_id_done read before
5934 * we print the log values
5938 "%d: %s from %d:%d to %d:%d context %s node %d handle %d size %d:%d ret %d/%d l=%d",
5939 e
->debug_id
, (e
->call_type
== 2) ? "reply" :
5940 ((e
->call_type
== 1) ? "async" : "call "), e
->from_proc
,
5941 e
->from_thread
, e
->to_proc
, e
->to_thread
, e
->context_name
,
5942 e
->to_node
, e
->target_handle
, e
->data_size
, e
->offsets_size
,
5943 e
->return_error
, e
->return_error_param
,
5944 e
->return_error_line
);
5946 * read-barrier to guarantee read of debug_id_done after
5947 * done printing the fields of the entry
5950 seq_printf(m
, debug_id
&& debug_id
== READ_ONCE(e
->debug_id_done
) ?
5951 "\n" : " (incomplete)\n");
5954 static int binder_transaction_log_show(struct seq_file
*m
, void *unused
)
5956 struct binder_transaction_log
*log
= m
->private;
5957 unsigned int log_cur
= atomic_read(&log
->cur
);
5962 count
= log_cur
+ 1;
5963 cur
= count
< ARRAY_SIZE(log
->entry
) && !log
->full
?
5964 0 : count
% ARRAY_SIZE(log
->entry
);
5965 if (count
> ARRAY_SIZE(log
->entry
) || log
->full
)
5966 count
= ARRAY_SIZE(log
->entry
);
5967 for (i
= 0; i
< count
; i
++) {
5968 unsigned int index
= cur
++ % ARRAY_SIZE(log
->entry
);
5970 print_binder_transaction_log_entry(m
, &log
->entry
[index
]);
5975 static const struct file_operations binder_fops
= {
5976 .owner
= THIS_MODULE
,
5977 .poll
= binder_poll
,
5978 .unlocked_ioctl
= binder_ioctl
,
5979 .compat_ioctl
= binder_ioctl
,
5980 .mmap
= binder_mmap
,
5981 .open
= binder_open
,
5982 .flush
= binder_flush
,
5983 .release
= binder_release
,
5986 BINDER_DEBUG_ENTRY(state
);
5987 BINDER_DEBUG_ENTRY(stats
);
5988 BINDER_DEBUG_ENTRY(transactions
);
5989 BINDER_DEBUG_ENTRY(transaction_log
);
5991 static int __init
init_binder_device(const char *name
)
5994 struct binder_device
*binder_device
;
5996 binder_device
= kzalloc(sizeof(*binder_device
), GFP_KERNEL
);
6000 binder_device
->miscdev
.fops
= &binder_fops
;
6001 binder_device
->miscdev
.minor
= MISC_DYNAMIC_MINOR
;
6002 binder_device
->miscdev
.name
= name
;
6004 binder_device
->context
.binder_context_mgr_uid
= INVALID_UID
;
6005 binder_device
->context
.name
= name
;
6006 mutex_init(&binder_device
->context
.context_mgr_node_lock
);
6008 ret
= misc_register(&binder_device
->miscdev
);
6010 kfree(binder_device
);
6014 hlist_add_head(&binder_device
->hlist
, &binder_devices
);
6019 static int __init
binder_init(void)
6022 char *device_name
, *device_names
, *device_tmp
;
6023 struct binder_device
*device
;
6024 struct hlist_node
*tmp
;
6026 ret
= binder_alloc_shrinker_init();
6030 atomic_set(&binder_transaction_log
.cur
, ~0U);
6031 atomic_set(&binder_transaction_log_failed
.cur
, ~0U);
6033 binder_debugfs_dir_entry_root
= debugfs_create_dir("binder", NULL
);
6034 if (binder_debugfs_dir_entry_root
)
6035 binder_debugfs_dir_entry_proc
= debugfs_create_dir("proc",
6036 binder_debugfs_dir_entry_root
);
6038 if (binder_debugfs_dir_entry_root
) {
6039 debugfs_create_file("state",
6041 binder_debugfs_dir_entry_root
,
6043 &binder_state_fops
);
6044 debugfs_create_file("stats",
6046 binder_debugfs_dir_entry_root
,
6048 &binder_stats_fops
);
6049 debugfs_create_file("transactions",
6051 binder_debugfs_dir_entry_root
,
6053 &binder_transactions_fops
);
6054 debugfs_create_file("transaction_log",
6056 binder_debugfs_dir_entry_root
,
6057 &binder_transaction_log
,
6058 &binder_transaction_log_fops
);
6059 debugfs_create_file("failed_transaction_log",
6061 binder_debugfs_dir_entry_root
,
6062 &binder_transaction_log_failed
,
6063 &binder_transaction_log_fops
);
6067 * Copy the module_parameter string, because we don't want to
6068 * tokenize it in-place.
6070 device_names
= kzalloc(strlen(binder_devices_param
) + 1, GFP_KERNEL
);
6071 if (!device_names
) {
6073 goto err_alloc_device_names_failed
;
6075 strcpy(device_names
, binder_devices_param
);
6077 device_tmp
= device_names
;
6078 while ((device_name
= strsep(&device_tmp
, ","))) {
6079 ret
= init_binder_device(device_name
);
6081 goto err_init_binder_device_failed
;
6086 err_init_binder_device_failed
:
6087 hlist_for_each_entry_safe(device
, tmp
, &binder_devices
, hlist
) {
6088 misc_deregister(&device
->miscdev
);
6089 hlist_del(&device
->hlist
);
6093 kfree(device_names
);
6095 err_alloc_device_names_failed
:
6096 debugfs_remove_recursive(binder_debugfs_dir_entry_root
);
6101 device_initcall(binder_init
);
6103 #define CREATE_TRACE_POINTS
6104 #include "binder_trace.h"
6106 MODULE_LICENSE("GPL v2");