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 #ifdef CONFIG_ANDROID_BINDER_IPC_32BIT
76 #define BINDER_IPC_32BIT 1
79 #include <uapi/linux/android/binder.h>
80 #include <uapi/linux/sched/types.h>
81 #include "binder_alloc.h"
82 #include "binder_trace.h"
84 static HLIST_HEAD(binder_deferred_list
);
85 static DEFINE_MUTEX(binder_deferred_lock
);
87 static HLIST_HEAD(binder_devices
);
88 static HLIST_HEAD(binder_procs
);
89 static DEFINE_MUTEX(binder_procs_lock
);
91 static HLIST_HEAD(binder_dead_nodes
);
92 static DEFINE_SPINLOCK(binder_dead_nodes_lock
);
94 static struct dentry
*binder_debugfs_dir_entry_root
;
95 static struct dentry
*binder_debugfs_dir_entry_proc
;
96 static atomic_t binder_last_id
;
98 #define BINDER_DEBUG_ENTRY(name) \
99 static int binder_##name##_open(struct inode *inode, struct file *file) \
101 return single_open(file, binder_##name##_show, inode->i_private); \
104 static const struct file_operations binder_##name##_fops = { \
105 .owner = THIS_MODULE, \
106 .open = binder_##name##_open, \
108 .llseek = seq_lseek, \
109 .release = single_release, \
112 static int binder_proc_show(struct seq_file
*m
, void *unused
);
113 BINDER_DEBUG_ENTRY(proc
);
115 /* This is only defined in include/asm-arm/sizes.h */
121 #define SZ_4M 0x400000
124 #define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
127 BINDER_DEBUG_USER_ERROR
= 1U << 0,
128 BINDER_DEBUG_FAILED_TRANSACTION
= 1U << 1,
129 BINDER_DEBUG_DEAD_TRANSACTION
= 1U << 2,
130 BINDER_DEBUG_OPEN_CLOSE
= 1U << 3,
131 BINDER_DEBUG_DEAD_BINDER
= 1U << 4,
132 BINDER_DEBUG_DEATH_NOTIFICATION
= 1U << 5,
133 BINDER_DEBUG_READ_WRITE
= 1U << 6,
134 BINDER_DEBUG_USER_REFS
= 1U << 7,
135 BINDER_DEBUG_THREADS
= 1U << 8,
136 BINDER_DEBUG_TRANSACTION
= 1U << 9,
137 BINDER_DEBUG_TRANSACTION_COMPLETE
= 1U << 10,
138 BINDER_DEBUG_FREE_BUFFER
= 1U << 11,
139 BINDER_DEBUG_INTERNAL_REFS
= 1U << 12,
140 BINDER_DEBUG_PRIORITY_CAP
= 1U << 13,
141 BINDER_DEBUG_SPINLOCKS
= 1U << 14,
143 static uint32_t binder_debug_mask
= BINDER_DEBUG_USER_ERROR
|
144 BINDER_DEBUG_FAILED_TRANSACTION
| BINDER_DEBUG_DEAD_TRANSACTION
;
145 module_param_named(debug_mask
, binder_debug_mask
, uint
, S_IWUSR
| S_IRUGO
);
147 static char *binder_devices_param
= CONFIG_ANDROID_BINDER_DEVICES
;
148 module_param_named(devices
, binder_devices_param
, charp
, 0444);
150 static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait
);
151 static int binder_stop_on_user_error
;
153 static int binder_set_stop_on_user_error(const char *val
,
154 const struct kernel_param
*kp
)
158 ret
= param_set_int(val
, kp
);
159 if (binder_stop_on_user_error
< 2)
160 wake_up(&binder_user_error_wait
);
163 module_param_call(stop_on_user_error
, binder_set_stop_on_user_error
,
164 param_get_int
, &binder_stop_on_user_error
, S_IWUSR
| S_IRUGO
);
166 #define binder_debug(mask, x...) \
168 if (binder_debug_mask & mask) \
172 #define binder_user_error(x...) \
174 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
176 if (binder_stop_on_user_error) \
177 binder_stop_on_user_error = 2; \
180 #define to_flat_binder_object(hdr) \
181 container_of(hdr, struct flat_binder_object, hdr)
183 #define to_binder_fd_object(hdr) container_of(hdr, struct binder_fd_object, hdr)
185 #define to_binder_buffer_object(hdr) \
186 container_of(hdr, struct binder_buffer_object, hdr)
188 #define to_binder_fd_array_object(hdr) \
189 container_of(hdr, struct binder_fd_array_object, hdr)
191 enum binder_stat_types
{
197 BINDER_STAT_TRANSACTION
,
198 BINDER_STAT_TRANSACTION_COMPLETE
,
202 struct binder_stats
{
203 atomic_t br
[_IOC_NR(BR_FAILED_REPLY
) + 1];
204 atomic_t bc
[_IOC_NR(BC_REPLY_SG
) + 1];
205 atomic_t obj_created
[BINDER_STAT_COUNT
];
206 atomic_t obj_deleted
[BINDER_STAT_COUNT
];
209 static struct binder_stats binder_stats
;
211 static inline void binder_stats_deleted(enum binder_stat_types type
)
213 atomic_inc(&binder_stats
.obj_deleted
[type
]);
216 static inline void binder_stats_created(enum binder_stat_types type
)
218 atomic_inc(&binder_stats
.obj_created
[type
]);
221 struct binder_transaction_log_entry
{
233 int return_error_line
;
234 uint32_t return_error
;
235 uint32_t return_error_param
;
236 const char *context_name
;
238 struct binder_transaction_log
{
241 struct binder_transaction_log_entry entry
[32];
243 static struct binder_transaction_log binder_transaction_log
;
244 static struct binder_transaction_log binder_transaction_log_failed
;
246 static struct binder_transaction_log_entry
*binder_transaction_log_add(
247 struct binder_transaction_log
*log
)
249 struct binder_transaction_log_entry
*e
;
250 unsigned int cur
= atomic_inc_return(&log
->cur
);
252 if (cur
>= ARRAY_SIZE(log
->entry
))
254 e
= &log
->entry
[cur
% ARRAY_SIZE(log
->entry
)];
255 WRITE_ONCE(e
->debug_id_done
, 0);
257 * write-barrier to synchronize access to e->debug_id_done.
258 * We make sure the initialized 0 value is seen before
259 * memset() other fields are zeroed by memset.
262 memset(e
, 0, sizeof(*e
));
266 struct binder_context
{
267 struct binder_node
*binder_context_mgr_node
;
268 struct mutex context_mgr_node_lock
;
270 kuid_t binder_context_mgr_uid
;
274 struct binder_device
{
275 struct hlist_node hlist
;
276 struct miscdevice miscdev
;
277 struct binder_context context
;
281 * struct binder_work - work enqueued on a worklist
282 * @entry: node enqueued on list
283 * @type: type of work to be performed
285 * There are separate work lists for proc, thread, and node (async).
288 struct list_head entry
;
291 BINDER_WORK_TRANSACTION
= 1,
292 BINDER_WORK_TRANSACTION_COMPLETE
,
293 BINDER_WORK_RETURN_ERROR
,
295 BINDER_WORK_DEAD_BINDER
,
296 BINDER_WORK_DEAD_BINDER_AND_CLEAR
,
297 BINDER_WORK_CLEAR_DEATH_NOTIFICATION
,
301 struct binder_error
{
302 struct binder_work work
;
307 * struct binder_node - binder node bookkeeping
308 * @debug_id: unique ID for debugging
309 * (invariant after initialized)
310 * @lock: lock for node fields
311 * @work: worklist element for node work
312 * (protected by @proc->inner_lock)
313 * @rb_node: element for proc->nodes tree
314 * (protected by @proc->inner_lock)
315 * @dead_node: element for binder_dead_nodes list
316 * (protected by binder_dead_nodes_lock)
317 * @proc: binder_proc that owns this node
318 * (invariant after initialized)
319 * @refs: list of references on this node
320 * (protected by @lock)
321 * @internal_strong_refs: used to take strong references when
322 * initiating a transaction
323 * (protected by @proc->inner_lock if @proc
325 * @local_weak_refs: weak user refs from local process
326 * (protected by @proc->inner_lock if @proc
328 * @local_strong_refs: strong user refs from local process
329 * (protected by @proc->inner_lock if @proc
331 * @tmp_refs: temporary kernel refs
332 * (protected by @proc->inner_lock while @proc
333 * is valid, and by binder_dead_nodes_lock
334 * if @proc is NULL. During inc/dec and node release
335 * it is also protected by @lock to provide safety
336 * as the node dies and @proc becomes NULL)
337 * @ptr: userspace pointer for node
338 * (invariant, no lock needed)
339 * @cookie: userspace cookie for node
340 * (invariant, no lock needed)
341 * @has_strong_ref: userspace notified of strong ref
342 * (protected by @proc->inner_lock if @proc
344 * @pending_strong_ref: userspace has acked notification of strong ref
345 * (protected by @proc->inner_lock if @proc
347 * @has_weak_ref: userspace notified of weak ref
348 * (protected by @proc->inner_lock if @proc
350 * @pending_weak_ref: userspace has acked notification of weak ref
351 * (protected by @proc->inner_lock if @proc
353 * @has_async_transaction: async transaction to node in progress
354 * (protected by @lock)
355 * @sched_policy: minimum scheduling policy for node
356 * (invariant after initialized)
357 * @accept_fds: file descriptor operations supported for node
358 * (invariant after initialized)
359 * @min_priority: minimum scheduling priority
360 * (invariant after initialized)
361 * @inherit_rt: inherit RT scheduling policy from caller
362 * (invariant after initialized)
363 * @async_todo: list of async work items
364 * (protected by @proc->inner_lock)
366 * Bookkeeping structure for binder nodes.
371 struct binder_work work
;
373 struct rb_node rb_node
;
374 struct hlist_node dead_node
;
376 struct binder_proc
*proc
;
377 struct hlist_head refs
;
378 int internal_strong_refs
;
380 int local_strong_refs
;
382 binder_uintptr_t ptr
;
383 binder_uintptr_t cookie
;
386 * bitfield elements protected by
390 u8 pending_strong_ref
:1;
392 u8 pending_weak_ref
:1;
396 * invariant after initialization
403 bool has_async_transaction
;
404 struct list_head async_todo
;
407 struct binder_ref_death
{
409 * @work: worklist element for death notifications
410 * (protected by inner_lock of the proc that
411 * this ref belongs to)
413 struct binder_work work
;
414 binder_uintptr_t cookie
;
418 * struct binder_ref_data - binder_ref counts and id
419 * @debug_id: unique ID for the ref
420 * @desc: unique userspace handle for ref
421 * @strong: strong ref count (debugging only if not locked)
422 * @weak: weak ref count (debugging only if not locked)
424 * Structure to hold ref count and ref id information. Since
425 * the actual ref can only be accessed with a lock, this structure
426 * is used to return information about the ref to callers of
427 * ref inc/dec functions.
429 struct binder_ref_data
{
437 * struct binder_ref - struct to track references on nodes
438 * @data: binder_ref_data containing id, handle, and current refcounts
439 * @rb_node_desc: node for lookup by @data.desc in proc's rb_tree
440 * @rb_node_node: node for lookup by @node in proc's rb_tree
441 * @node_entry: list entry for node->refs list in target node
442 * (protected by @node->lock)
443 * @proc: binder_proc containing ref
444 * @node: binder_node of target node. When cleaning up a
445 * ref for deletion in binder_cleanup_ref, a non-NULL
446 * @node indicates the node must be freed
447 * @death: pointer to death notification (ref_death) if requested
448 * (protected by @node->lock)
450 * Structure to track references from procA to target node (on procB). This
451 * structure is unsafe to access without holding @proc->outer_lock.
454 /* Lookups needed: */
455 /* node + proc => ref (transaction) */
456 /* desc + proc => ref (transaction, inc/dec ref) */
457 /* node => refs + procs (proc exit) */
458 struct binder_ref_data data
;
459 struct rb_node rb_node_desc
;
460 struct rb_node rb_node_node
;
461 struct hlist_node node_entry
;
462 struct binder_proc
*proc
;
463 struct binder_node
*node
;
464 struct binder_ref_death
*death
;
467 enum binder_deferred_state
{
468 BINDER_DEFERRED_PUT_FILES
= 0x01,
469 BINDER_DEFERRED_FLUSH
= 0x02,
470 BINDER_DEFERRED_RELEASE
= 0x04,
474 * struct binder_priority - scheduler policy and priority
475 * @sched_policy scheduler policy
476 * @prio [100..139] for SCHED_NORMAL, [0..99] for FIFO/RT
478 * The binder driver supports inheriting the following scheduler policies:
484 struct binder_priority
{
485 unsigned int sched_policy
;
490 * struct binder_proc - binder process bookkeeping
491 * @proc_node: element for binder_procs list
492 * @threads: rbtree of binder_threads in this proc
493 * (protected by @inner_lock)
494 * @nodes: rbtree of binder nodes associated with
495 * this proc ordered by node->ptr
496 * (protected by @inner_lock)
497 * @refs_by_desc: rbtree of refs ordered by ref->desc
498 * (protected by @outer_lock)
499 * @refs_by_node: rbtree of refs ordered by ref->node
500 * (protected by @outer_lock)
501 * @waiting_threads: threads currently waiting for proc work
502 * (protected by @inner_lock)
503 * @pid PID of group_leader of process
504 * (invariant after initialized)
505 * @tsk task_struct for group_leader of process
506 * (invariant after initialized)
507 * @files files_struct for process
508 * (protected by @files_lock)
509 * @files_lock mutex to protect @files
510 * @deferred_work_node: element for binder_deferred_list
511 * (protected by binder_deferred_lock)
512 * @deferred_work: bitmap of deferred work to perform
513 * (protected by binder_deferred_lock)
514 * @is_dead: process is dead and awaiting free
515 * when outstanding transactions are cleaned up
516 * (protected by @inner_lock)
517 * @todo: list of work for this process
518 * (protected by @inner_lock)
519 * @stats: per-process binder statistics
520 * (atomics, no lock needed)
521 * @delivered_death: list of delivered death notification
522 * (protected by @inner_lock)
523 * @max_threads: cap on number of binder threads
524 * (protected by @inner_lock)
525 * @requested_threads: number of binder threads requested but not
526 * yet started. In current implementation, can
528 * (protected by @inner_lock)
529 * @requested_threads_started: number binder threads started
530 * (protected by @inner_lock)
531 * @tmp_ref: temporary reference to indicate proc is in use
532 * (protected by @inner_lock)
533 * @default_priority: default scheduler priority
534 * (invariant after initialized)
535 * @debugfs_entry: debugfs node
536 * @alloc: binder allocator bookkeeping
537 * @context: binder_context for this proc
538 * (invariant after initialized)
539 * @inner_lock: can nest under outer_lock and/or node lock
540 * @outer_lock: no nesting under innor or node lock
541 * Lock order: 1) outer, 2) node, 3) inner
543 * Bookkeeping structure for binder processes
546 struct hlist_node proc_node
;
547 struct rb_root threads
;
548 struct rb_root nodes
;
549 struct rb_root refs_by_desc
;
550 struct rb_root refs_by_node
;
551 struct list_head waiting_threads
;
553 struct task_struct
*tsk
;
554 struct files_struct
*files
;
555 struct mutex files_lock
;
556 struct hlist_node deferred_work_node
;
560 struct list_head todo
;
561 struct binder_stats stats
;
562 struct list_head delivered_death
;
564 int requested_threads
;
565 int requested_threads_started
;
567 struct binder_priority default_priority
;
568 struct dentry
*debugfs_entry
;
569 struct binder_alloc alloc
;
570 struct binder_context
*context
;
571 spinlock_t inner_lock
;
572 spinlock_t outer_lock
;
576 BINDER_LOOPER_STATE_REGISTERED
= 0x01,
577 BINDER_LOOPER_STATE_ENTERED
= 0x02,
578 BINDER_LOOPER_STATE_EXITED
= 0x04,
579 BINDER_LOOPER_STATE_INVALID
= 0x08,
580 BINDER_LOOPER_STATE_WAITING
= 0x10,
581 BINDER_LOOPER_STATE_POLL
= 0x20,
585 * struct binder_thread - binder thread bookkeeping
586 * @proc: binder process for this thread
587 * (invariant after initialization)
588 * @rb_node: element for proc->threads rbtree
589 * (protected by @proc->inner_lock)
590 * @waiting_thread_node: element for @proc->waiting_threads list
591 * (protected by @proc->inner_lock)
592 * @pid: PID for this thread
593 * (invariant after initialization)
594 * @looper: bitmap of looping state
595 * (only accessed by this thread)
596 * @looper_needs_return: looping thread needs to exit driver
598 * @transaction_stack: stack of in-progress transactions for this thread
599 * (protected by @proc->inner_lock)
600 * @todo: list of work to do for this thread
601 * (protected by @proc->inner_lock)
602 * @process_todo: whether work in @todo should be processed
603 * (protected by @proc->inner_lock)
604 * @return_error: transaction errors reported by this thread
605 * (only accessed by this thread)
606 * @reply_error: transaction errors reported by target thread
607 * (protected by @proc->inner_lock)
608 * @wait: wait queue for thread work
609 * @stats: per-thread statistics
610 * (atomics, no lock needed)
611 * @tmp_ref: temporary reference to indicate thread is in use
612 * (atomic since @proc->inner_lock cannot
613 * always be acquired)
614 * @is_dead: thread is dead and awaiting free
615 * when outstanding transactions are cleaned up
616 * (protected by @proc->inner_lock)
617 * @task: struct task_struct for this thread
619 * Bookkeeping structure for binder threads.
621 struct binder_thread
{
622 struct binder_proc
*proc
;
623 struct rb_node rb_node
;
624 struct list_head waiting_thread_node
;
626 int looper
; /* only modified by this thread */
627 bool looper_need_return
; /* can be written by other thread */
628 struct binder_transaction
*transaction_stack
;
629 struct list_head todo
;
631 struct binder_error return_error
;
632 struct binder_error reply_error
;
633 wait_queue_head_t wait
;
634 struct binder_stats stats
;
637 struct task_struct
*task
;
640 struct binder_transaction
{
642 struct binder_work work
;
643 struct binder_thread
*from
;
644 struct binder_transaction
*from_parent
;
645 struct binder_proc
*to_proc
;
646 struct binder_thread
*to_thread
;
647 struct binder_transaction
*to_parent
;
648 unsigned need_reply
:1;
649 /* unsigned is_dead:1; */ /* not used at the moment */
651 struct binder_buffer
*buffer
;
654 struct binder_priority priority
;
655 struct binder_priority saved_priority
;
656 bool set_priority_called
;
659 * @lock: protects @from, @to_proc, and @to_thread
661 * @from, @to_proc, and @to_thread can be set to NULL
662 * during thread teardown
668 * binder_proc_lock() - Acquire outer lock for given binder_proc
669 * @proc: struct binder_proc to acquire
671 * Acquires proc->outer_lock. Used to protect binder_ref
672 * structures associated with the given proc.
674 #define binder_proc_lock(proc) _binder_proc_lock(proc, __LINE__)
676 _binder_proc_lock(struct binder_proc
*proc
, int line
)
678 binder_debug(BINDER_DEBUG_SPINLOCKS
,
679 "%s: line=%d\n", __func__
, line
);
680 spin_lock(&proc
->outer_lock
);
684 * binder_proc_unlock() - Release spinlock for given binder_proc
685 * @proc: struct binder_proc to acquire
687 * Release lock acquired via binder_proc_lock()
689 #define binder_proc_unlock(_proc) _binder_proc_unlock(_proc, __LINE__)
691 _binder_proc_unlock(struct binder_proc
*proc
, int line
)
693 binder_debug(BINDER_DEBUG_SPINLOCKS
,
694 "%s: line=%d\n", __func__
, line
);
695 spin_unlock(&proc
->outer_lock
);
699 * binder_inner_proc_lock() - Acquire inner lock for given binder_proc
700 * @proc: struct binder_proc to acquire
702 * Acquires proc->inner_lock. Used to protect todo lists
704 #define binder_inner_proc_lock(proc) _binder_inner_proc_lock(proc, __LINE__)
706 _binder_inner_proc_lock(struct binder_proc
*proc
, int line
)
708 binder_debug(BINDER_DEBUG_SPINLOCKS
,
709 "%s: line=%d\n", __func__
, line
);
710 spin_lock(&proc
->inner_lock
);
714 * binder_inner_proc_unlock() - Release inner lock for given binder_proc
715 * @proc: struct binder_proc to acquire
717 * Release lock acquired via binder_inner_proc_lock()
719 #define binder_inner_proc_unlock(proc) _binder_inner_proc_unlock(proc, __LINE__)
721 _binder_inner_proc_unlock(struct binder_proc
*proc
, int line
)
723 binder_debug(BINDER_DEBUG_SPINLOCKS
,
724 "%s: line=%d\n", __func__
, line
);
725 spin_unlock(&proc
->inner_lock
);
729 * binder_node_lock() - Acquire spinlock for given binder_node
730 * @node: struct binder_node to acquire
732 * Acquires node->lock. Used to protect binder_node fields
734 #define binder_node_lock(node) _binder_node_lock(node, __LINE__)
736 _binder_node_lock(struct binder_node
*node
, int line
)
738 binder_debug(BINDER_DEBUG_SPINLOCKS
,
739 "%s: line=%d\n", __func__
, line
);
740 spin_lock(&node
->lock
);
744 * binder_node_unlock() - Release spinlock for given binder_proc
745 * @node: struct binder_node to acquire
747 * Release lock acquired via binder_node_lock()
749 #define binder_node_unlock(node) _binder_node_unlock(node, __LINE__)
751 _binder_node_unlock(struct binder_node
*node
, int line
)
753 binder_debug(BINDER_DEBUG_SPINLOCKS
,
754 "%s: line=%d\n", __func__
, line
);
755 spin_unlock(&node
->lock
);
759 * binder_node_inner_lock() - Acquire node and inner locks
760 * @node: struct binder_node to acquire
762 * Acquires node->lock. If node->proc also acquires
763 * proc->inner_lock. Used to protect binder_node fields
765 #define binder_node_inner_lock(node) _binder_node_inner_lock(node, __LINE__)
767 _binder_node_inner_lock(struct binder_node
*node
, int line
)
769 binder_debug(BINDER_DEBUG_SPINLOCKS
,
770 "%s: line=%d\n", __func__
, line
);
771 spin_lock(&node
->lock
);
773 binder_inner_proc_lock(node
->proc
);
777 * binder_node_unlock() - Release node and inner locks
778 * @node: struct binder_node to acquire
780 * Release lock acquired via binder_node_lock()
782 #define binder_node_inner_unlock(node) _binder_node_inner_unlock(node, __LINE__)
784 _binder_node_inner_unlock(struct binder_node
*node
, int line
)
786 struct binder_proc
*proc
= node
->proc
;
788 binder_debug(BINDER_DEBUG_SPINLOCKS
,
789 "%s: line=%d\n", __func__
, line
);
791 binder_inner_proc_unlock(proc
);
792 spin_unlock(&node
->lock
);
795 #ifdef CONFIG_DEBUG_SNAPSHOT_BINDER
797 * Binder Debug Snapshot
799 static void init_binder_transaction_base(int type
, struct trace_binder_transaction_base
*base
,
800 struct binder_transaction
*t
, struct binder_thread
*from
,
801 struct binder_thread
*to
)
803 struct binder_thread
*t_from
;
804 struct binder_thread
*t_to
;
809 t_from
= t
->from
? t
->from
: (from
? from
: NULL
);
810 t_to
= t
->to_thread
? t
->to_thread
: (to
? to
: NULL
);
811 base
->trace_type
= type
;
812 base
->transaction_id
= t
->debug_id
;
813 base
->from_pid
= t_from
? t_from
->proc
->pid
: 0;
814 base
->from_tid
= t_from
? t_from
->pid
: 0;
815 base
->to_pid
= t
->to_proc
? t
->to_proc
->pid
: 0;
816 base
->to_tid
= t_to
? t_to
->pid
: 0;
818 strncpy(base
->from_pid_comm
, t_from
->proc
->tsk
->comm
, TASK_COMM_LEN
);
819 strncpy(base
->from_tid_comm
, t_from
->task
->comm
, TASK_COMM_LEN
);
821 base
->from_pid_comm
[0] = '\0';
822 base
->from_tid_comm
[0] = '\0';
825 strncpy(base
->to_pid_comm
, t
->to_proc
->tsk
->comm
, TASK_COMM_LEN
);
827 base
->to_pid_comm
[0] = '\0';
829 strncpy(base
->to_tid_comm
, t_to
->task
->comm
, TASK_COMM_LEN
);
831 base
->to_tid_comm
[0] = '\0';
834 static void dss_binder_transaction(int reply
, struct binder_transaction
*t
, struct binder_thread
*from
, int to_node_id
)
836 struct trace_binder_transaction_base base
;
837 struct trace_binder_transaction transaction
;
839 init_binder_transaction_base(TRANSACTION
, &base
, t
, from
, NULL
);
840 transaction
.to_node_id
= to_node_id
;
841 transaction
.reply
= reply
;
842 transaction
.flags
= t
->flags
;
843 transaction
.code
= t
->code
;
845 dbg_snapshot_binder(&base
, &transaction
, NULL
);
848 static void dss_binder_transaction_received(struct binder_transaction
*t
, struct binder_thread
*to
)
850 struct trace_binder_transaction_base base
;
852 init_binder_transaction_base(TRANSACTION_DONE
, &base
, t
, NULL
, to
);
854 dbg_snapshot_binder(&base
, NULL
, NULL
);
857 static void dss_binder_transaction_failed(int reply
, struct binder_transaction_log_entry
*e
,
858 char *from_pid_comm
, char *from_tid_comm
,
859 unsigned int flags
, unsigned int code
)
861 struct trace_binder_transaction_base base
;
862 struct trace_binder_transaction transaction
;
863 struct trace_binder_transaction_error error
;
865 base
.trace_type
= TRANSACTION_ERROR
;
866 base
.transaction_id
= e
->debug_id
;
867 base
.from_pid
= e
->from_proc
;
868 base
.from_tid
= e
->from_thread
;
869 base
.to_pid
= e
->to_proc
;
870 base
.to_tid
= e
->to_thread
;
871 strncpy(base
.from_pid_comm
, from_pid_comm
, TASK_COMM_LEN
);
872 strncpy(base
.from_tid_comm
, from_tid_comm
, TASK_COMM_LEN
);
873 base
.to_pid_comm
[0] = '\0';
874 base
.to_tid_comm
[0] = '\0';
875 transaction
.to_node_id
= e
->to_node
;
876 transaction
.reply
= reply
;
877 transaction
.flags
= flags
;
878 transaction
.code
= code
;
879 error
.return_error
= e
->return_error
;
880 error
.return_error_param
= e
->return_error_param
;
881 error
.return_error_line
= e
->return_error_line
;
883 dbg_snapshot_binder(&base
, &transaction
, &error
);
885 #endif /* CONFIG_DEBUG_SNAPSHOT_BINDER */
887 static bool binder_worklist_empty_ilocked(struct list_head
*list
)
889 return list_empty(list
);
893 * binder_worklist_empty() - Check if no items on the work list
894 * @proc: binder_proc associated with list
895 * @list: list to check
897 * Return: true if there are no items on list, else false
899 static bool binder_worklist_empty(struct binder_proc
*proc
,
900 struct list_head
*list
)
904 binder_inner_proc_lock(proc
);
905 ret
= binder_worklist_empty_ilocked(list
);
906 binder_inner_proc_unlock(proc
);
911 * binder_enqueue_work_ilocked() - Add an item to the work list
912 * @work: struct binder_work to add to list
913 * @target_list: list to add work to
915 * Adds the work to the specified list. Asserts that work
916 * is not already on a list.
918 * Requires the proc->inner_lock to be held.
921 binder_enqueue_work_ilocked(struct binder_work
*work
,
922 struct list_head
*target_list
)
924 BUG_ON(target_list
== NULL
);
925 BUG_ON(work
->entry
.next
&& !list_empty(&work
->entry
));
926 list_add_tail(&work
->entry
, target_list
);
930 * binder_enqueue_deferred_thread_work_ilocked() - Add deferred thread work
931 * @thread: thread to queue work to
932 * @work: struct binder_work to add to list
934 * Adds the work to the todo list of the thread. Doesn't set the process_todo
935 * flag, which means that (if it wasn't already set) the thread will go to
936 * sleep without handling this work when it calls read.
938 * Requires the proc->inner_lock to be held.
941 binder_enqueue_deferred_thread_work_ilocked(struct binder_thread
*thread
,
942 struct binder_work
*work
)
944 binder_enqueue_work_ilocked(work
, &thread
->todo
);
948 * binder_enqueue_thread_work_ilocked() - Add an item to the thread work list
949 * @thread: thread to queue work to
950 * @work: struct binder_work to add to list
952 * Adds the work to the todo list of the thread, and enables processing
955 * Requires the proc->inner_lock to be held.
958 binder_enqueue_thread_work_ilocked(struct binder_thread
*thread
,
959 struct binder_work
*work
)
961 binder_enqueue_work_ilocked(work
, &thread
->todo
);
962 thread
->process_todo
= true;
966 * binder_enqueue_thread_work() - Add an item to the thread work list
967 * @thread: thread to queue work to
968 * @work: struct binder_work to add to list
970 * Adds the work to the todo list of the thread, and enables processing
974 binder_enqueue_thread_work(struct binder_thread
*thread
,
975 struct binder_work
*work
)
977 binder_inner_proc_lock(thread
->proc
);
978 binder_enqueue_thread_work_ilocked(thread
, work
);
979 binder_inner_proc_unlock(thread
->proc
);
983 binder_dequeue_work_ilocked(struct binder_work
*work
)
985 list_del_init(&work
->entry
);
989 * binder_dequeue_work() - Removes an item from the work list
990 * @proc: binder_proc associated with list
991 * @work: struct binder_work to remove from list
993 * Removes the specified work item from whatever list it is on.
994 * Can safely be called if work is not on any list.
997 binder_dequeue_work(struct binder_proc
*proc
, struct binder_work
*work
)
999 binder_inner_proc_lock(proc
);
1000 binder_dequeue_work_ilocked(work
);
1001 binder_inner_proc_unlock(proc
);
1004 static struct binder_work
*binder_dequeue_work_head_ilocked(
1005 struct list_head
*list
)
1007 struct binder_work
*w
;
1009 w
= list_first_entry_or_null(list
, struct binder_work
, entry
);
1011 list_del_init(&w
->entry
);
1016 * binder_dequeue_work_head() - Dequeues the item at head of list
1017 * @proc: binder_proc associated with list
1018 * @list: list to dequeue head
1020 * Removes the head of the list if there are items on the list
1022 * Return: pointer dequeued binder_work, NULL if list was empty
1024 static struct binder_work
*binder_dequeue_work_head(
1025 struct binder_proc
*proc
,
1026 struct list_head
*list
)
1028 struct binder_work
*w
;
1030 binder_inner_proc_lock(proc
);
1031 w
= binder_dequeue_work_head_ilocked(list
);
1032 binder_inner_proc_unlock(proc
);
1037 binder_defer_work(struct binder_proc
*proc
, enum binder_deferred_state defer
);
1038 static void binder_free_thread(struct binder_thread
*thread
);
1039 static void binder_free_proc(struct binder_proc
*proc
);
1040 static void binder_inc_node_tmpref_ilocked(struct binder_node
*node
);
1042 static int task_get_unused_fd_flags(struct binder_proc
*proc
, int flags
)
1044 unsigned long rlim_cur
;
1048 mutex_lock(&proc
->files_lock
);
1049 if (proc
->files
== NULL
) {
1053 if (!lock_task_sighand(proc
->tsk
, &irqs
)) {
1057 rlim_cur
= task_rlimit(proc
->tsk
, RLIMIT_NOFILE
);
1058 unlock_task_sighand(proc
->tsk
, &irqs
);
1060 ret
= __alloc_fd(proc
->files
, 0, rlim_cur
, flags
);
1062 mutex_unlock(&proc
->files_lock
);
1067 * copied from fd_install
1069 static void task_fd_install(
1070 struct binder_proc
*proc
, unsigned int fd
, struct file
*file
)
1072 mutex_lock(&proc
->files_lock
);
1074 __fd_install(proc
->files
, fd
, file
);
1075 mutex_unlock(&proc
->files_lock
);
1079 * copied from sys_close
1081 static long task_close_fd(struct binder_proc
*proc
, unsigned int fd
)
1085 mutex_lock(&proc
->files_lock
);
1086 if (proc
->files
== NULL
) {
1090 retval
= __close_fd(proc
->files
, fd
);
1091 /* can't restart close syscall because file table entry was cleared */
1092 if (unlikely(retval
== -ERESTARTSYS
||
1093 retval
== -ERESTARTNOINTR
||
1094 retval
== -ERESTARTNOHAND
||
1095 retval
== -ERESTART_RESTARTBLOCK
))
1098 mutex_unlock(&proc
->files_lock
);
1102 static bool binder_has_work_ilocked(struct binder_thread
*thread
,
1105 return thread
->process_todo
||
1106 thread
->looper_need_return
||
1108 !binder_worklist_empty_ilocked(&thread
->proc
->todo
));
1111 static bool binder_has_work(struct binder_thread
*thread
, bool do_proc_work
)
1115 binder_inner_proc_lock(thread
->proc
);
1116 has_work
= binder_has_work_ilocked(thread
, do_proc_work
);
1117 binder_inner_proc_unlock(thread
->proc
);
1122 static bool binder_available_for_proc_work_ilocked(struct binder_thread
*thread
)
1124 return !thread
->transaction_stack
&&
1125 binder_worklist_empty_ilocked(&thread
->todo
) &&
1126 (thread
->looper
& (BINDER_LOOPER_STATE_ENTERED
|
1127 BINDER_LOOPER_STATE_REGISTERED
));
1130 static void binder_wakeup_poll_threads_ilocked(struct binder_proc
*proc
,
1134 struct binder_thread
*thread
;
1136 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
)) {
1137 thread
= rb_entry(n
, struct binder_thread
, rb_node
);
1138 if (thread
->looper
& BINDER_LOOPER_STATE_POLL
&&
1139 binder_available_for_proc_work_ilocked(thread
)) {
1141 wake_up_interruptible_sync(&thread
->wait
);
1143 wake_up_interruptible(&thread
->wait
);
1149 * binder_select_thread_ilocked() - selects a thread for doing proc work.
1150 * @proc: process to select a thread from
1152 * Note that calling this function moves the thread off the waiting_threads
1153 * list, so it can only be woken up by the caller of this function, or a
1154 * signal. Therefore, callers *should* always wake up the thread this function
1157 * Return: If there's a thread currently waiting for process work,
1158 * returns that thread. Otherwise returns NULL.
1160 static struct binder_thread
*
1161 binder_select_thread_ilocked(struct binder_proc
*proc
)
1163 struct binder_thread
*thread
;
1165 assert_spin_locked(&proc
->inner_lock
);
1166 thread
= list_first_entry_or_null(&proc
->waiting_threads
,
1167 struct binder_thread
,
1168 waiting_thread_node
);
1171 list_del_init(&thread
->waiting_thread_node
);
1177 * binder_wakeup_thread_ilocked() - wakes up a thread for doing proc work.
1178 * @proc: process to wake up a thread in
1179 * @thread: specific thread to wake-up (may be NULL)
1180 * @sync: whether to do a synchronous wake-up
1182 * This function wakes up a thread in the @proc process.
1183 * The caller may provide a specific thread to wake-up in
1184 * the @thread parameter. If @thread is NULL, this function
1185 * will wake up threads that have called poll().
1187 * Note that for this function to work as expected, callers
1188 * should first call binder_select_thread() to find a thread
1189 * to handle the work (if they don't have a thread already),
1190 * and pass the result into the @thread parameter.
1192 static void binder_wakeup_thread_ilocked(struct binder_proc
*proc
,
1193 struct binder_thread
*thread
,
1196 assert_spin_locked(&proc
->inner_lock
);
1200 wake_up_interruptible_sync(&thread
->wait
);
1202 wake_up_interruptible(&thread
->wait
);
1206 /* Didn't find a thread waiting for proc work; this can happen
1208 * 1. All threads are busy handling transactions
1209 * In that case, one of those threads should call back into
1210 * the kernel driver soon and pick up this work.
1211 * 2. Threads are using the (e)poll interface, in which case
1212 * they may be blocked on the waitqueue without having been
1213 * added to waiting_threads. For this case, we just iterate
1214 * over all threads not handling transaction work, and
1215 * wake them all up. We wake all because we don't know whether
1216 * a thread that called into (e)poll is handling non-binder
1219 binder_wakeup_poll_threads_ilocked(proc
, sync
);
1222 static void binder_wakeup_proc_ilocked(struct binder_proc
*proc
)
1224 struct binder_thread
*thread
= binder_select_thread_ilocked(proc
);
1226 binder_wakeup_thread_ilocked(proc
, thread
, /* sync = */false);
1229 static bool is_rt_policy(int policy
)
1231 return policy
== SCHED_FIFO
|| policy
== SCHED_RR
;
1234 static bool is_fair_policy(int policy
)
1236 return policy
== SCHED_NORMAL
|| policy
== SCHED_BATCH
;
1239 static bool binder_supported_policy(int policy
)
1241 return is_fair_policy(policy
) || is_rt_policy(policy
);
1244 static int to_userspace_prio(int policy
, int kernel_priority
)
1246 if (is_fair_policy(policy
))
1247 return PRIO_TO_NICE(kernel_priority
);
1249 return MAX_USER_RT_PRIO
- 1 - kernel_priority
;
1252 static int to_kernel_prio(int policy
, int user_priority
)
1254 if (is_fair_policy(policy
))
1255 return NICE_TO_PRIO(user_priority
);
1257 return MAX_USER_RT_PRIO
- 1 - user_priority
;
1260 static void binder_do_set_priority(struct task_struct
*task
,
1261 struct binder_priority desired
,
1264 int priority
; /* user-space prio value */
1266 unsigned int policy
= desired
.sched_policy
;
1268 if (task
->policy
== policy
&& task
->normal_prio
== desired
.prio
)
1271 has_cap_nice
= has_capability_noaudit(task
, CAP_SYS_NICE
);
1273 priority
= to_userspace_prio(policy
, desired
.prio
);
1275 if (verify
&& is_rt_policy(policy
) && !has_cap_nice
) {
1276 long max_rtprio
= task_rlimit(task
, RLIMIT_RTPRIO
);
1278 if (max_rtprio
== 0) {
1279 policy
= SCHED_NORMAL
;
1280 priority
= MIN_NICE
;
1281 } else if (priority
> max_rtprio
) {
1282 priority
= max_rtprio
;
1286 if (verify
&& is_fair_policy(policy
) && !has_cap_nice
) {
1287 long min_nice
= rlimit_to_nice(task_rlimit(task
, RLIMIT_NICE
));
1289 if (min_nice
> MAX_NICE
) {
1290 binder_user_error("%d(%s) RLIMIT_NICE not set\n",
1291 task
->pid
, task
->comm
);
1293 } else if (priority
< min_nice
) {
1294 priority
= min_nice
;
1298 if (policy
!= desired
.sched_policy
||
1299 to_kernel_prio(policy
, priority
) != desired
.prio
)
1300 binder_debug(BINDER_DEBUG_PRIORITY_CAP
,
1301 "%d(%s): priority %d not allowed, using %d instead\n",
1302 task
->pid
, task
->comm
, desired
.prio
,
1303 to_kernel_prio(policy
, priority
));
1305 trace_binder_set_priority(task
->tgid
, task
->pid
, task
->normal_prio
,
1306 to_kernel_prio(policy
, priority
),
1309 /* Set the actual priority */
1310 if (task
->policy
!= policy
|| is_rt_policy(policy
)) {
1311 struct sched_param params
;
1313 params
.sched_priority
= is_rt_policy(policy
) ? priority
: 0;
1315 sched_setscheduler_nocheck(task
,
1316 policy
| SCHED_RESET_ON_FORK
,
1319 if (is_fair_policy(policy
))
1320 set_user_nice(task
, priority
);
1323 static void binder_set_priority(struct task_struct
*task
,
1324 struct binder_priority desired
)
1326 binder_do_set_priority(task
, desired
, /* verify = */ true);
1329 static void binder_restore_priority(struct task_struct
*task
,
1330 struct binder_priority desired
)
1332 binder_do_set_priority(task
, desired
, /* verify = */ false);
1335 static void binder_transaction_priority(struct task_struct
*task
,
1336 struct binder_transaction
*t
,
1337 struct binder_priority node_prio
,
1340 struct binder_priority desired_prio
= t
->priority
;
1342 if (t
->set_priority_called
)
1345 t
->set_priority_called
= true;
1346 t
->saved_priority
.sched_policy
= task
->policy
;
1347 t
->saved_priority
.prio
= task
->normal_prio
;
1349 if (!inherit_rt
&& is_rt_policy(desired_prio
.sched_policy
)) {
1350 desired_prio
.prio
= NICE_TO_PRIO(0);
1351 desired_prio
.sched_policy
= SCHED_NORMAL
;
1354 if (node_prio
.prio
< t
->priority
.prio
||
1355 (node_prio
.prio
== t
->priority
.prio
&&
1356 node_prio
.sched_policy
== SCHED_FIFO
)) {
1358 * In case the minimum priority on the node is
1359 * higher (lower value), use that priority. If
1360 * the priority is the same, but the node uses
1361 * SCHED_FIFO, prefer SCHED_FIFO, since it can
1362 * run unbounded, unlike SCHED_RR.
1364 desired_prio
= node_prio
;
1367 binder_set_priority(task
, desired_prio
);
1370 static struct binder_node
*binder_get_node_ilocked(struct binder_proc
*proc
,
1371 binder_uintptr_t ptr
)
1373 struct rb_node
*n
= proc
->nodes
.rb_node
;
1374 struct binder_node
*node
;
1376 assert_spin_locked(&proc
->inner_lock
);
1379 node
= rb_entry(n
, struct binder_node
, rb_node
);
1381 if (ptr
< node
->ptr
)
1383 else if (ptr
> node
->ptr
)
1387 * take an implicit weak reference
1388 * to ensure node stays alive until
1389 * call to binder_put_node()
1391 binder_inc_node_tmpref_ilocked(node
);
1398 static struct binder_node
*binder_get_node(struct binder_proc
*proc
,
1399 binder_uintptr_t ptr
)
1401 struct binder_node
*node
;
1403 binder_inner_proc_lock(proc
);
1404 node
= binder_get_node_ilocked(proc
, ptr
);
1405 binder_inner_proc_unlock(proc
);
1409 static struct binder_node
*binder_init_node_ilocked(
1410 struct binder_proc
*proc
,
1411 struct binder_node
*new_node
,
1412 struct flat_binder_object
*fp
)
1414 struct rb_node
**p
= &proc
->nodes
.rb_node
;
1415 struct rb_node
*parent
= NULL
;
1416 struct binder_node
*node
;
1417 binder_uintptr_t ptr
= fp
? fp
->binder
: 0;
1418 binder_uintptr_t cookie
= fp
? fp
->cookie
: 0;
1419 __u32 flags
= fp
? fp
->flags
: 0;
1422 assert_spin_locked(&proc
->inner_lock
);
1427 node
= rb_entry(parent
, struct binder_node
, rb_node
);
1429 if (ptr
< node
->ptr
)
1431 else if (ptr
> node
->ptr
)
1432 p
= &(*p
)->rb_right
;
1435 * A matching node is already in
1436 * the rb tree. Abandon the init
1439 binder_inc_node_tmpref_ilocked(node
);
1444 binder_stats_created(BINDER_STAT_NODE
);
1446 rb_link_node(&node
->rb_node
, parent
, p
);
1447 rb_insert_color(&node
->rb_node
, &proc
->nodes
);
1448 node
->debug_id
= atomic_inc_return(&binder_last_id
);
1451 node
->cookie
= cookie
;
1452 node
->work
.type
= BINDER_WORK_NODE
;
1453 priority
= flags
& FLAT_BINDER_FLAG_PRIORITY_MASK
;
1454 node
->sched_policy
= (flags
& FLAT_BINDER_FLAG_SCHED_POLICY_MASK
) >>
1455 FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT
;
1456 node
->min_priority
= to_kernel_prio(node
->sched_policy
, priority
);
1457 node
->accept_fds
= !!(flags
& FLAT_BINDER_FLAG_ACCEPTS_FDS
);
1458 node
->inherit_rt
= !!(flags
& FLAT_BINDER_FLAG_INHERIT_RT
);
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
)
2211 t
->buffer
->transaction
= NULL
;
2213 binder_stats_deleted(BINDER_STAT_TRANSACTION
);
2216 static void binder_send_failed_reply(struct binder_transaction
*t
,
2217 uint32_t error_code
)
2219 struct binder_thread
*target_thread
;
2220 struct binder_transaction
*next
;
2222 BUG_ON(t
->flags
& TF_ONE_WAY
);
2224 target_thread
= binder_get_txn_from_and_acq_inner(t
);
2225 if (target_thread
) {
2226 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
2227 "send failed reply for transaction %d to %d:%d(%s:%s)\n",
2229 target_thread
->proc
->pid
,
2231 target_thread
->proc
->tsk
->comm
,
2232 target_thread
->task
->comm
);
2234 binder_pop_transaction_ilocked(target_thread
, t
);
2235 if (target_thread
->reply_error
.cmd
== BR_OK
) {
2236 target_thread
->reply_error
.cmd
= error_code
;
2237 binder_enqueue_thread_work_ilocked(
2239 &target_thread
->reply_error
.work
);
2240 wake_up_interruptible(&target_thread
->wait
);
2243 * Cannot get here for normal operation, but
2244 * we can if multiple synchronous transactions
2245 * are sent without blocking for responses.
2246 * Just ignore the 2nd error in this case.
2248 pr_warn("Unexpected reply error: %u\n",
2249 target_thread
->reply_error
.cmd
);
2251 binder_inner_proc_unlock(target_thread
->proc
);
2252 binder_thread_dec_tmpref(target_thread
);
2253 binder_free_transaction(t
);
2256 next
= t
->from_parent
;
2258 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
2259 "send failed reply for transaction %d, target dead\n",
2262 binder_free_transaction(t
);
2264 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
2265 "reply failed, no target thread at root\n");
2269 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
2270 "reply failed, no target thread -- retry %d\n",
2276 * binder_cleanup_transaction() - cleans up undelivered transaction
2277 * @t: transaction that needs to be cleaned up
2278 * @reason: reason the transaction wasn't delivered
2279 * @error_code: error to return to caller (if synchronous call)
2281 static void binder_cleanup_transaction(struct binder_transaction
*t
,
2283 uint32_t error_code
)
2285 if (t
->buffer
->target_node
&& !(t
->flags
& TF_ONE_WAY
)) {
2286 binder_send_failed_reply(t
, error_code
);
2288 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
2289 "undelivered transaction %d, %s\n",
2290 t
->debug_id
, reason
);
2291 binder_free_transaction(t
);
2296 * binder_validate_object() - checks for a valid metadata object in a buffer.
2297 * @buffer: binder_buffer that we're parsing.
2298 * @offset: offset in the buffer at which to validate an object.
2300 * Return: If there's a valid metadata object at @offset in @buffer, the
2301 * size of that object. Otherwise, it returns zero.
2303 static size_t binder_validate_object(struct binder_buffer
*buffer
, u64 offset
)
2305 /* Check if we can read a header first */
2306 struct binder_object_header
*hdr
;
2307 size_t object_size
= 0;
2309 if (offset
> buffer
->data_size
- sizeof(*hdr
) ||
2310 buffer
->data_size
< sizeof(*hdr
) ||
2311 !IS_ALIGNED(offset
, sizeof(u32
)))
2314 /* Ok, now see if we can read a complete object. */
2315 hdr
= (struct binder_object_header
*)(buffer
->data
+ offset
);
2316 switch (hdr
->type
) {
2317 case BINDER_TYPE_BINDER
:
2318 case BINDER_TYPE_WEAK_BINDER
:
2319 case BINDER_TYPE_HANDLE
:
2320 case BINDER_TYPE_WEAK_HANDLE
:
2321 object_size
= sizeof(struct flat_binder_object
);
2323 case BINDER_TYPE_FD
:
2324 object_size
= sizeof(struct binder_fd_object
);
2326 case BINDER_TYPE_PTR
:
2327 object_size
= sizeof(struct binder_buffer_object
);
2329 case BINDER_TYPE_FDA
:
2330 object_size
= sizeof(struct binder_fd_array_object
);
2335 if (offset
<= buffer
->data_size
- object_size
&&
2336 buffer
->data_size
>= object_size
)
2343 * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
2344 * @b: binder_buffer containing the object
2345 * @index: index in offset array at which the binder_buffer_object is
2347 * @start: points to the start of the offset array
2348 * @num_valid: the number of valid offsets in the offset array
2350 * Return: If @index is within the valid range of the offset array
2351 * described by @start and @num_valid, and if there's a valid
2352 * binder_buffer_object at the offset found in index @index
2353 * of the offset array, that object is returned. Otherwise,
2354 * %NULL is returned.
2355 * Note that the offset found in index @index itself is not
2356 * verified; this function assumes that @num_valid elements
2357 * from @start were previously verified to have valid offsets.
2359 static struct binder_buffer_object
*binder_validate_ptr(struct binder_buffer
*b
,
2360 binder_size_t index
,
2361 binder_size_t
*start
,
2362 binder_size_t num_valid
)
2364 struct binder_buffer_object
*buffer_obj
;
2365 binder_size_t
*offp
;
2367 if (index
>= num_valid
)
2370 offp
= start
+ index
;
2371 buffer_obj
= (struct binder_buffer_object
*)(b
->data
+ *offp
);
2372 if (buffer_obj
->hdr
.type
!= BINDER_TYPE_PTR
)
2379 * binder_validate_fixup() - validates pointer/fd fixups happen in order.
2380 * @b: transaction buffer
2381 * @objects_start start of objects buffer
2382 * @buffer: binder_buffer_object in which to fix up
2383 * @offset: start offset in @buffer to fix up
2384 * @last_obj: last binder_buffer_object that we fixed up in
2385 * @last_min_offset: minimum fixup offset in @last_obj
2387 * Return: %true if a fixup in buffer @buffer at offset @offset is
2390 * For safety reasons, we only allow fixups inside a buffer to happen
2391 * at increasing offsets; additionally, we only allow fixup on the last
2392 * buffer object that was verified, or one of its parents.
2394 * Example of what is allowed:
2397 * B (parent = A, offset = 0)
2398 * C (parent = A, offset = 16)
2399 * D (parent = C, offset = 0)
2400 * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
2402 * Examples of what is not allowed:
2404 * Decreasing offsets within the same parent:
2406 * C (parent = A, offset = 16)
2407 * B (parent = A, offset = 0) // decreasing offset within A
2409 * Referring to a parent that wasn't the last object or any of its parents:
2411 * B (parent = A, offset = 0)
2412 * C (parent = A, offset = 0)
2413 * C (parent = A, offset = 16)
2414 * D (parent = B, offset = 0) // B is not A or any of A's parents
2416 static bool binder_validate_fixup(struct binder_buffer
*b
,
2417 binder_size_t
*objects_start
,
2418 struct binder_buffer_object
*buffer
,
2419 binder_size_t fixup_offset
,
2420 struct binder_buffer_object
*last_obj
,
2421 binder_size_t last_min_offset
)
2424 /* Nothing to fix up in */
2428 while (last_obj
!= buffer
) {
2430 * Safe to retrieve the parent of last_obj, since it
2431 * was already previously verified by the driver.
2433 if ((last_obj
->flags
& BINDER_BUFFER_FLAG_HAS_PARENT
) == 0)
2435 last_min_offset
= last_obj
->parent_offset
+ sizeof(uintptr_t);
2436 last_obj
= (struct binder_buffer_object
*)
2437 (b
->data
+ *(objects_start
+ last_obj
->parent
));
2439 return (fixup_offset
>= last_min_offset
);
2442 static void binder_transaction_buffer_release(struct binder_proc
*proc
,
2443 struct binder_buffer
*buffer
,
2444 binder_size_t
*failed_at
)
2446 binder_size_t
*offp
, *off_start
, *off_end
;
2447 int debug_id
= buffer
->debug_id
;
2449 binder_debug(BINDER_DEBUG_TRANSACTION
,
2450 "%d(%s) buffer release %d, size %zd-%zd, failed at %pK\n",
2451 proc
->pid
, proc
->tsk
->comm
, buffer
->debug_id
,
2452 buffer
->data_size
, buffer
->offsets_size
, failed_at
);
2454 if (buffer
->target_node
)
2455 binder_dec_node(buffer
->target_node
, 1, 0);
2457 off_start
= (binder_size_t
*)(buffer
->data
+
2458 ALIGN(buffer
->data_size
, sizeof(void *)));
2460 off_end
= failed_at
;
2462 off_end
= (void *)off_start
+ buffer
->offsets_size
;
2463 for (offp
= off_start
; offp
< off_end
; offp
++) {
2464 struct binder_object_header
*hdr
;
2465 size_t object_size
= binder_validate_object(buffer
, *offp
);
2467 if (object_size
== 0) {
2468 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
2469 debug_id
, (u64
)*offp
, buffer
->data_size
);
2472 hdr
= (struct binder_object_header
*)(buffer
->data
+ *offp
);
2473 switch (hdr
->type
) {
2474 case BINDER_TYPE_BINDER
:
2475 case BINDER_TYPE_WEAK_BINDER
: {
2476 struct flat_binder_object
*fp
;
2477 struct binder_node
*node
;
2479 fp
= to_flat_binder_object(hdr
);
2480 node
= binder_get_node(proc
, fp
->binder
);
2482 pr_err("transaction release %d bad node %016llx\n",
2483 debug_id
, (u64
)fp
->binder
);
2486 binder_debug(BINDER_DEBUG_TRANSACTION
,
2487 " node %d u%016llx\n",
2488 node
->debug_id
, (u64
)node
->ptr
);
2489 binder_dec_node(node
, hdr
->type
== BINDER_TYPE_BINDER
,
2491 binder_put_node(node
);
2493 case BINDER_TYPE_HANDLE
:
2494 case BINDER_TYPE_WEAK_HANDLE
: {
2495 struct flat_binder_object
*fp
;
2496 struct binder_ref_data rdata
;
2499 fp
= to_flat_binder_object(hdr
);
2500 ret
= binder_dec_ref_for_handle(proc
, fp
->handle
,
2501 hdr
->type
== BINDER_TYPE_HANDLE
, &rdata
);
2504 pr_err("transaction release %d bad handle %d, ret = %d\n",
2505 debug_id
, fp
->handle
, ret
);
2508 binder_debug(BINDER_DEBUG_TRANSACTION
,
2509 " ref %d desc %d\n",
2510 rdata
.debug_id
, rdata
.desc
);
2513 case BINDER_TYPE_FD
: {
2514 struct binder_fd_object
*fp
= to_binder_fd_object(hdr
);
2516 binder_debug(BINDER_DEBUG_TRANSACTION
,
2517 " fd %d\n", fp
->fd
);
2519 task_close_fd(proc
, fp
->fd
);
2521 case BINDER_TYPE_PTR
:
2523 * Nothing to do here, this will get cleaned up when the
2524 * transaction buffer gets freed
2527 case BINDER_TYPE_FDA
: {
2528 struct binder_fd_array_object
*fda
;
2529 struct binder_buffer_object
*parent
;
2530 uintptr_t parent_buffer
;
2533 binder_size_t fd_buf_size
;
2535 fda
= to_binder_fd_array_object(hdr
);
2536 parent
= binder_validate_ptr(buffer
, fda
->parent
,
2540 pr_err("transaction release %d bad parent offset",
2545 * Since the parent was already fixed up, convert it
2546 * back to kernel address space to access it
2548 parent_buffer
= parent
->buffer
-
2549 binder_alloc_get_user_buffer_offset(
2552 fd_buf_size
= sizeof(u32
) * fda
->num_fds
;
2553 if (fda
->num_fds
>= SIZE_MAX
/ sizeof(u32
)) {
2554 pr_err("transaction release %d invalid number of fds (%lld)\n",
2555 debug_id
, (u64
)fda
->num_fds
);
2558 if (fd_buf_size
> parent
->length
||
2559 fda
->parent_offset
> parent
->length
- fd_buf_size
) {
2560 /* No space for all file descriptors here. */
2561 pr_err("transaction release %d not enough space for %lld fds in buffer\n",
2562 debug_id
, (u64
)fda
->num_fds
);
2565 fd_array
= (u32
*)(parent_buffer
+ (uintptr_t)fda
->parent_offset
);
2566 for (fd_index
= 0; fd_index
< fda
->num_fds
; fd_index
++)
2567 task_close_fd(proc
, fd_array
[fd_index
]);
2570 pr_err("transaction release %d bad object type %x\n",
2571 debug_id
, hdr
->type
);
2577 static int binder_translate_binder(struct flat_binder_object
*fp
,
2578 struct binder_transaction
*t
,
2579 struct binder_thread
*thread
)
2581 struct binder_node
*node
;
2582 struct binder_proc
*proc
= thread
->proc
;
2583 struct binder_proc
*target_proc
= t
->to_proc
;
2584 struct binder_ref_data rdata
;
2587 node
= binder_get_node(proc
, fp
->binder
);
2589 node
= binder_new_node(proc
, fp
);
2593 if (fp
->cookie
!= node
->cookie
) {
2594 binder_user_error("%d:%d(%s:%s) sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
2595 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, (u64
)fp
->binder
,
2596 node
->debug_id
, (u64
)fp
->cookie
,
2601 if (security_binder_transfer_binder(proc
->tsk
, target_proc
->tsk
)) {
2606 ret
= binder_inc_ref_for_node(target_proc
, node
,
2607 fp
->hdr
.type
== BINDER_TYPE_BINDER
,
2608 &thread
->todo
, &rdata
);
2612 if (fp
->hdr
.type
== BINDER_TYPE_BINDER
)
2613 fp
->hdr
.type
= BINDER_TYPE_HANDLE
;
2615 fp
->hdr
.type
= BINDER_TYPE_WEAK_HANDLE
;
2617 fp
->handle
= rdata
.desc
;
2620 trace_binder_transaction_node_to_ref(t
, node
, &rdata
);
2621 binder_debug(BINDER_DEBUG_TRANSACTION
,
2622 " node %d u%016llx -> ref %d desc %d\n",
2623 node
->debug_id
, (u64
)node
->ptr
,
2624 rdata
.debug_id
, rdata
.desc
);
2626 binder_put_node(node
);
2630 static int binder_translate_handle(struct flat_binder_object
*fp
,
2631 struct binder_transaction
*t
,
2632 struct binder_thread
*thread
)
2634 struct binder_proc
*proc
= thread
->proc
;
2635 struct binder_proc
*target_proc
= t
->to_proc
;
2636 struct binder_node
*node
;
2637 struct binder_ref_data src_rdata
;
2640 node
= binder_get_node_from_ref(proc
, fp
->handle
,
2641 fp
->hdr
.type
== BINDER_TYPE_HANDLE
, &src_rdata
);
2643 binder_user_error("%d:%d(%s:%s) got transaction with invalid handle, %d\n",
2644 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, fp
->handle
);
2647 if (security_binder_transfer_binder(proc
->tsk
, target_proc
->tsk
)) {
2652 binder_node_lock(node
);
2653 if (node
->proc
== target_proc
) {
2654 if (fp
->hdr
.type
== BINDER_TYPE_HANDLE
)
2655 fp
->hdr
.type
= BINDER_TYPE_BINDER
;
2657 fp
->hdr
.type
= BINDER_TYPE_WEAK_BINDER
;
2658 fp
->binder
= node
->ptr
;
2659 fp
->cookie
= node
->cookie
;
2661 binder_inner_proc_lock(node
->proc
);
2662 binder_inc_node_nilocked(node
,
2663 fp
->hdr
.type
== BINDER_TYPE_BINDER
,
2666 binder_inner_proc_unlock(node
->proc
);
2667 trace_binder_transaction_ref_to_node(t
, node
, &src_rdata
);
2668 binder_debug(BINDER_DEBUG_TRANSACTION
,
2669 " ref %d desc %d -> node %d u%016llx\n",
2670 src_rdata
.debug_id
, src_rdata
.desc
, node
->debug_id
,
2672 binder_node_unlock(node
);
2674 struct binder_ref_data dest_rdata
;
2676 binder_node_unlock(node
);
2677 ret
= binder_inc_ref_for_node(target_proc
, node
,
2678 fp
->hdr
.type
== BINDER_TYPE_HANDLE
,
2684 fp
->handle
= dest_rdata
.desc
;
2686 trace_binder_transaction_ref_to_ref(t
, node
, &src_rdata
,
2688 binder_debug(BINDER_DEBUG_TRANSACTION
,
2689 " ref %d desc %d -> ref %d desc %d (node %d)\n",
2690 src_rdata
.debug_id
, src_rdata
.desc
,
2691 dest_rdata
.debug_id
, dest_rdata
.desc
,
2695 binder_put_node(node
);
2699 static int binder_translate_fd(int fd
,
2700 struct binder_transaction
*t
,
2701 struct binder_thread
*thread
,
2702 struct binder_transaction
*in_reply_to
)
2704 struct binder_proc
*proc
= thread
->proc
;
2705 struct binder_proc
*target_proc
= t
->to_proc
;
2709 bool target_allows_fd
;
2712 target_allows_fd
= !!(in_reply_to
->flags
& TF_ACCEPT_FDS
);
2714 target_allows_fd
= t
->buffer
->target_node
->accept_fds
;
2715 if (!target_allows_fd
) {
2716 binder_user_error("%d:%d(%s:%s) got %s with fd, %d, but target does not allow fds\n",
2717 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
2718 in_reply_to
? "reply" : "transaction",
2721 goto err_fd_not_accepted
;
2726 binder_user_error("%d:%d(%s:%s) got transaction with invalid fd, %d\n",
2727 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, fd
);
2731 ret
= security_binder_transfer_file(proc
->tsk
, target_proc
->tsk
, file
);
2737 target_fd
= task_get_unused_fd_flags(target_proc
, O_CLOEXEC
);
2738 if (target_fd
< 0) {
2740 goto err_get_unused_fd
;
2742 task_fd_install(target_proc
, target_fd
, file
);
2743 trace_binder_transaction_fd(t
, fd
, target_fd
);
2744 binder_debug(BINDER_DEBUG_TRANSACTION
, " fd %d -> %d\n",
2753 err_fd_not_accepted
:
2757 static int binder_translate_fd_array(struct binder_fd_array_object
*fda
,
2758 struct binder_buffer_object
*parent
,
2759 struct binder_transaction
*t
,
2760 struct binder_thread
*thread
,
2761 struct binder_transaction
*in_reply_to
)
2763 binder_size_t fdi
, fd_buf_size
, num_installed_fds
;
2765 uintptr_t parent_buffer
;
2767 struct binder_proc
*proc
= thread
->proc
;
2768 struct binder_proc
*target_proc
= t
->to_proc
;
2770 fd_buf_size
= sizeof(u32
) * fda
->num_fds
;
2771 if (fda
->num_fds
>= SIZE_MAX
/ sizeof(u32
)) {
2772 binder_user_error("%d:%d(%s:%s) got transaction with invalid number of fds (%lld)\n",
2773 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, (u64
)fda
->num_fds
);
2776 if (fd_buf_size
> parent
->length
||
2777 fda
->parent_offset
> parent
->length
- fd_buf_size
) {
2778 /* No space for all file descriptors here. */
2779 binder_user_error("%d:%d(%s:%s) not enough space to store %lld fds in buffer\n",
2780 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, (u64
)fda
->num_fds
);
2784 * Since the parent was already fixed up, convert it
2785 * back to the kernel address space to access it
2787 parent_buffer
= parent
->buffer
-
2788 binder_alloc_get_user_buffer_offset(&target_proc
->alloc
);
2789 fd_array
= (u32
*)(parent_buffer
+ (uintptr_t)fda
->parent_offset
);
2790 if (!IS_ALIGNED((unsigned long)fd_array
, sizeof(u32
))) {
2791 binder_user_error("%d:%d(%s:%s) parent offset not aligned correctly.\n",
2792 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
2795 for (fdi
= 0; fdi
< fda
->num_fds
; fdi
++) {
2796 target_fd
= binder_translate_fd(fd_array
[fdi
], t
, thread
,
2799 goto err_translate_fd_failed
;
2800 fd_array
[fdi
] = target_fd
;
2804 err_translate_fd_failed
:
2806 * Failed to allocate fd or security error, free fds
2809 num_installed_fds
= fdi
;
2810 for (fdi
= 0; fdi
< num_installed_fds
; fdi
++)
2811 task_close_fd(target_proc
, fd_array
[fdi
]);
2815 static int binder_fixup_parent(struct binder_transaction
*t
,
2816 struct binder_thread
*thread
,
2817 struct binder_buffer_object
*bp
,
2818 binder_size_t
*off_start
,
2819 binder_size_t num_valid
,
2820 struct binder_buffer_object
*last_fixup_obj
,
2821 binder_size_t last_fixup_min_off
)
2823 struct binder_buffer_object
*parent
;
2825 struct binder_buffer
*b
= t
->buffer
;
2826 struct binder_proc
*proc
= thread
->proc
;
2827 struct binder_proc
*target_proc
= t
->to_proc
;
2829 if (!(bp
->flags
& BINDER_BUFFER_FLAG_HAS_PARENT
))
2832 parent
= binder_validate_ptr(b
, bp
->parent
, off_start
, num_valid
);
2834 binder_user_error("%d:%d(%s:%s) got transaction with invalid parent offset or type\n",
2835 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
2839 if (!binder_validate_fixup(b
, off_start
,
2840 parent
, bp
->parent_offset
,
2842 last_fixup_min_off
)) {
2843 binder_user_error("%d:%d(%s:%s) got transaction with out-of-order buffer fixup\n",
2844 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
2848 if (parent
->length
< sizeof(binder_uintptr_t
) ||
2849 bp
->parent_offset
> parent
->length
- sizeof(binder_uintptr_t
)) {
2850 /* No space for a pointer here! */
2851 binder_user_error("%d:%d(%s:%s) got transaction with invalid parent offset\n",
2852 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
2855 parent_buffer
= (u8
*)((uintptr_t)parent
->buffer
-
2856 binder_alloc_get_user_buffer_offset(
2857 &target_proc
->alloc
));
2858 *(binder_uintptr_t
*)(parent_buffer
+ bp
->parent_offset
) = bp
->buffer
;
2864 * binder_proc_transaction() - sends a transaction to a process and wakes it up
2865 * @t: transaction to send
2866 * @proc: process to send the transaction to
2867 * @thread: thread in @proc to send the transaction to (may be NULL)
2869 * This function queues a transaction to the specified process. It will try
2870 * to find a thread in the target process to handle the transaction and
2871 * wake it up. If no thread is found, the work is queued to the proc
2874 * If the @thread parameter is not NULL, the transaction is always queued
2875 * to the waitlist of that specific thread.
2877 * Return: true if the transactions was successfully queued
2878 * false if the target process or thread is dead
2880 static bool binder_proc_transaction(struct binder_transaction
*t
,
2881 struct binder_proc
*proc
,
2882 struct binder_thread
*thread
)
2884 struct binder_node
*node
= t
->buffer
->target_node
;
2885 struct binder_priority node_prio
;
2886 bool oneway
= !!(t
->flags
& TF_ONE_WAY
);
2887 bool pending_async
= false;
2890 binder_node_lock(node
);
2891 node_prio
.prio
= node
->min_priority
;
2892 node_prio
.sched_policy
= node
->sched_policy
;
2896 if (node
->has_async_transaction
) {
2897 pending_async
= true;
2899 node
->has_async_transaction
= 1;
2903 binder_inner_proc_lock(proc
);
2905 if (proc
->is_dead
|| (thread
&& thread
->is_dead
)) {
2906 binder_inner_proc_unlock(proc
);
2907 binder_node_unlock(node
);
2911 if (!thread
&& !pending_async
)
2912 thread
= binder_select_thread_ilocked(proc
);
2915 binder_transaction_priority(thread
->task
, t
, node_prio
,
2917 binder_enqueue_thread_work_ilocked(thread
, &t
->work
);
2918 } else if (!pending_async
) {
2919 binder_enqueue_work_ilocked(&t
->work
, &proc
->todo
);
2921 binder_enqueue_work_ilocked(&t
->work
, &node
->async_todo
);
2925 binder_wakeup_thread_ilocked(proc
, thread
, !oneway
/* sync */);
2927 binder_inner_proc_unlock(proc
);
2928 binder_node_unlock(node
);
2934 * binder_get_node_refs_for_txn() - Get required refs on node for txn
2935 * @node: struct binder_node for which to get refs
2936 * @proc: returns @node->proc if valid
2937 * @error: if no @proc then returns BR_DEAD_REPLY
2939 * User-space normally keeps the node alive when creating a transaction
2940 * since it has a reference to the target. The local strong ref keeps it
2941 * alive if the sending process dies before the target process processes
2942 * the transaction. If the source process is malicious or has a reference
2943 * counting bug, relying on the local strong ref can fail.
2945 * Since user-space can cause the local strong ref to go away, we also take
2946 * a tmpref on the node to ensure it survives while we are constructing
2947 * the transaction. We also need a tmpref on the proc while we are
2948 * constructing the transaction, so we take that here as well.
2950 * Return: The target_node with refs taken or NULL if no @node->proc is NULL.
2951 * Also sets @proc if valid. If the @node->proc is NULL indicating that the
2952 * target proc has died, @error is set to BR_DEAD_REPLY
2954 static struct binder_node
*binder_get_node_refs_for_txn(
2955 struct binder_node
*node
,
2956 struct binder_proc
**procp
,
2959 struct binder_node
*target_node
= NULL
;
2961 binder_node_inner_lock(node
);
2964 binder_inc_node_nilocked(node
, 1, 0, NULL
);
2965 binder_inc_node_tmpref_ilocked(node
);
2966 node
->proc
->tmp_ref
++;
2967 *procp
= node
->proc
;
2969 *error
= BR_DEAD_REPLY
;
2970 binder_node_inner_unlock(node
);
2975 static void binder_transaction(struct binder_proc
*proc
,
2976 struct binder_thread
*thread
,
2977 struct binder_transaction_data
*tr
, int reply
,
2978 binder_size_t extra_buffers_size
)
2981 struct binder_transaction
*t
;
2982 struct binder_work
*tcomplete
;
2983 binder_size_t
*offp
, *off_end
, *off_start
;
2984 binder_size_t off_min
;
2985 u8
*sg_bufp
, *sg_buf_end
;
2986 struct binder_proc
*target_proc
= NULL
;
2987 struct binder_thread
*target_thread
= NULL
;
2988 struct binder_node
*target_node
= NULL
;
2989 struct binder_transaction
*in_reply_to
= NULL
;
2990 struct binder_transaction_log_entry
*e
;
2991 uint32_t return_error
= 0;
2992 uint32_t return_error_param
= 0;
2993 uint32_t return_error_line
= 0;
2994 struct binder_buffer_object
*last_fixup_obj
= NULL
;
2995 binder_size_t last_fixup_min_off
= 0;
2996 struct binder_context
*context
= proc
->context
;
2997 int t_debug_id
= atomic_inc_return(&binder_last_id
);
2999 e
= binder_transaction_log_add(&binder_transaction_log
);
3000 e
->debug_id
= t_debug_id
;
3001 e
->call_type
= reply
? 2 : !!(tr
->flags
& TF_ONE_WAY
);
3002 e
->from_proc
= proc
->pid
;
3003 e
->from_thread
= thread
->pid
;
3004 e
->target_handle
= tr
->target
.handle
;
3005 e
->data_size
= tr
->data_size
;
3006 e
->offsets_size
= tr
->offsets_size
;
3007 e
->context_name
= proc
->context
->name
;
3010 binder_inner_proc_lock(proc
);
3011 in_reply_to
= thread
->transaction_stack
;
3012 if (in_reply_to
== NULL
) {
3013 binder_inner_proc_unlock(proc
);
3014 binder_user_error("%d:%d(%s:%s) got reply transaction with no transaction stack\n",
3015 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3016 return_error
= BR_FAILED_REPLY
;
3017 return_error_param
= -EPROTO
;
3018 return_error_line
= __LINE__
;
3019 goto err_empty_call_stack
;
3021 if (in_reply_to
->to_thread
!= thread
) {
3022 spin_lock(&in_reply_to
->lock
);
3023 binder_user_error("%d:%d(%s:%s) got reply transaction with bad transaction stack, "\
3024 "transaction %d has target %d:%d(%s:%s)\n",
3025 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, in_reply_to
->debug_id
,
3026 in_reply_to
->to_proc
?
3027 in_reply_to
->to_proc
->pid
: 0,
3028 in_reply_to
->to_thread
?
3029 in_reply_to
->to_thread
->pid
: 0,
3030 in_reply_to
->to_proc
?
3031 in_reply_to
->to_proc
->tsk
->comm
: "",
3032 in_reply_to
->to_thread
?
3033 in_reply_to
->to_thread
->task
->comm
: "");
3034 spin_unlock(&in_reply_to
->lock
);
3035 binder_inner_proc_unlock(proc
);
3036 return_error
= BR_FAILED_REPLY
;
3037 return_error_param
= -EPROTO
;
3038 return_error_line
= __LINE__
;
3040 goto err_bad_call_stack
;
3042 thread
->transaction_stack
= in_reply_to
->to_parent
;
3043 binder_inner_proc_unlock(proc
);
3044 target_thread
= binder_get_txn_from_and_acq_inner(in_reply_to
);
3045 if (target_thread
== NULL
) {
3046 return_error
= BR_DEAD_REPLY
;
3047 return_error_line
= __LINE__
;
3048 goto err_dead_binder
;
3050 if (target_thread
->transaction_stack
!= in_reply_to
) {
3051 binder_user_error("%d:%d(%s:%s) got reply transaction with bad target transaction "\
3052 "stack %d, expected %d\n",
3053 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3054 target_thread
->transaction_stack
?
3055 target_thread
->transaction_stack
->debug_id
: 0,
3056 in_reply_to
->debug_id
);
3057 binder_inner_proc_unlock(target_thread
->proc
);
3058 return_error
= BR_FAILED_REPLY
;
3059 return_error_param
= -EPROTO
;
3060 return_error_line
= __LINE__
;
3062 target_thread
= NULL
;
3063 goto err_dead_binder
;
3065 target_proc
= target_thread
->proc
;
3066 target_proc
->tmp_ref
++;
3067 binder_inner_proc_unlock(target_thread
->proc
);
3069 if (tr
->target
.handle
) {
3070 struct binder_ref
*ref
;
3073 * There must already be a strong ref
3074 * on this node. If so, do a strong
3075 * increment on the node to ensure it
3076 * stays alive until the transaction is
3079 binder_proc_lock(proc
);
3080 ref
= binder_get_ref_olocked(proc
, tr
->target
.handle
,
3083 target_node
= binder_get_node_refs_for_txn(
3084 ref
->node
, &target_proc
,
3087 binder_user_error("%d:%d(%s:%s) got transaction to invalid handle\n",
3088 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3089 return_error
= BR_FAILED_REPLY
;
3091 binder_proc_unlock(proc
);
3093 mutex_lock(&context
->context_mgr_node_lock
);
3094 target_node
= context
->binder_context_mgr_node
;
3096 target_node
= binder_get_node_refs_for_txn(
3097 target_node
, &target_proc
,
3100 return_error
= BR_DEAD_REPLY
;
3101 mutex_unlock(&context
->context_mgr_node_lock
);
3102 if (target_node
&& target_proc
== proc
) {
3103 binder_user_error("%d:%d got transaction to context manager from process owning it\n",
3104 proc
->pid
, thread
->pid
);
3105 return_error
= BR_FAILED_REPLY
;
3106 return_error_param
= -EINVAL
;
3107 return_error_line
= __LINE__
;
3108 goto err_invalid_target_handle
;
3113 * return_error is set above
3115 return_error_param
= -EINVAL
;
3116 return_error_line
= __LINE__
;
3117 goto err_dead_binder
;
3119 e
->to_node
= target_node
->debug_id
;
3120 if (security_binder_transaction(proc
->tsk
,
3121 target_proc
->tsk
) < 0) {
3122 return_error
= BR_FAILED_REPLY
;
3123 return_error_param
= -EPERM
;
3124 return_error_line
= __LINE__
;
3125 goto err_invalid_target_handle
;
3127 binder_inner_proc_lock(proc
);
3128 if (!(tr
->flags
& TF_ONE_WAY
) && thread
->transaction_stack
) {
3129 struct binder_transaction
*tmp
;
3131 tmp
= thread
->transaction_stack
;
3132 if (tmp
->to_thread
!= thread
) {
3133 spin_lock(&tmp
->lock
);
3134 binder_user_error("%d:%d(%s:%s) got new transaction with bad transaction stack, "\
3135 "transaction %d has target %d:%d(%s:%s)\n",
3136 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, tmp
->debug_id
,
3137 tmp
->to_proc
? tmp
->to_proc
->pid
: 0,
3139 tmp
->to_thread
->pid
: 0,
3140 tmp
->to_proc
? tmp
->to_proc
->tsk
->comm
: "",
3141 tmp
->to_thread
? tmp
->to_thread
->task
->comm
: "");
3142 spin_unlock(&tmp
->lock
);
3143 binder_inner_proc_unlock(proc
);
3144 return_error
= BR_FAILED_REPLY
;
3145 return_error_param
= -EPROTO
;
3146 return_error_line
= __LINE__
;
3147 goto err_bad_call_stack
;
3150 struct binder_thread
*from
;
3152 spin_lock(&tmp
->lock
);
3154 if (from
&& from
->proc
== target_proc
) {
3155 atomic_inc(&from
->tmp_ref
);
3156 target_thread
= from
;
3157 spin_unlock(&tmp
->lock
);
3160 spin_unlock(&tmp
->lock
);
3161 tmp
= tmp
->from_parent
;
3164 binder_inner_proc_unlock(proc
);
3166 if (target_thread
) {
3167 e
->to_thread
= target_thread
->pid
;
3169 e
->to_proc
= target_proc
->pid
;
3171 /* TODO: reuse incoming transaction for reply */
3172 t
= kzalloc(sizeof(*t
), GFP_KERNEL
);
3174 return_error
= BR_FAILED_REPLY
;
3175 return_error_param
= -ENOMEM
;
3176 return_error_line
= __LINE__
;
3177 goto err_alloc_t_failed
;
3179 binder_stats_created(BINDER_STAT_TRANSACTION
);
3180 spin_lock_init(&t
->lock
);
3182 tcomplete
= kzalloc(sizeof(*tcomplete
), GFP_KERNEL
);
3183 if (tcomplete
== NULL
) {
3184 return_error
= BR_FAILED_REPLY
;
3185 return_error_param
= -ENOMEM
;
3186 return_error_line
= __LINE__
;
3187 goto err_alloc_tcomplete_failed
;
3189 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE
);
3191 t
->debug_id
= t_debug_id
;
3194 binder_debug(BINDER_DEBUG_TRANSACTION
,
3195 "%d:%d(%s:%s) BC_REPLY %d -> %d:%d (%s:%s), data %016llx-%016llx size %lld-%lld-%lld\n",
3196 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, t
->debug_id
,
3197 target_proc
->pid
, target_thread
->pid
, target_proc
->tsk
->comm
, target_thread
->task
->comm
,
3198 (u64
)tr
->data
.ptr
.buffer
,
3199 (u64
)tr
->data
.ptr
.offsets
,
3200 (u64
)tr
->data_size
, (u64
)tr
->offsets_size
,
3201 (u64
)extra_buffers_size
);
3203 binder_debug(BINDER_DEBUG_TRANSACTION
,
3204 "%d:%d(%s:%s) BC_TRANSACTION %d -> %d (%s) - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
3205 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, t
->debug_id
,
3206 target_proc
->pid
, target_proc
->tsk
->comm
, target_node
->debug_id
,
3207 (u64
)tr
->data
.ptr
.buffer
,
3208 (u64
)tr
->data
.ptr
.offsets
,
3209 (u64
)tr
->data_size
, (u64
)tr
->offsets_size
,
3210 (u64
)extra_buffers_size
);
3212 if (!reply
&& !(tr
->flags
& TF_ONE_WAY
))
3216 t
->sender_euid
= task_euid(proc
->tsk
);
3217 t
->to_proc
= target_proc
;
3218 t
->to_thread
= target_thread
;
3220 t
->flags
= tr
->flags
;
3221 if (!(t
->flags
& TF_ONE_WAY
) &&
3222 binder_supported_policy(current
->policy
)) {
3223 /* Inherit supported policies for synchronous transactions */
3224 t
->priority
.sched_policy
= current
->policy
;
3225 t
->priority
.prio
= current
->normal_prio
;
3227 /* Otherwise, fall back to the default priority */
3228 t
->priority
= target_proc
->default_priority
;
3231 #ifdef CONFIG_DEBUG_SNAPSHOT_BINDER
3232 dss_binder_transaction(reply
, t
, t
->from
? t
->from
: thread
, target_node
? target_node
->debug_id
: 0);
3234 trace_binder_transaction(reply
, t
, target_node
);
3236 t
->buffer
= binder_alloc_new_buf(&target_proc
->alloc
, tr
->data_size
,
3237 tr
->offsets_size
, extra_buffers_size
,
3238 !reply
&& (t
->flags
& TF_ONE_WAY
));
3239 if (IS_ERR(t
->buffer
)) {
3241 * -ESRCH indicates VMA cleared. The target is dying.
3243 return_error_param
= PTR_ERR(t
->buffer
);
3244 return_error
= return_error_param
== -ESRCH
?
3245 BR_DEAD_REPLY
: BR_FAILED_REPLY
;
3246 return_error_line
= __LINE__
;
3248 goto err_binder_alloc_buf_failed
;
3250 t
->buffer
->allow_user_free
= 0;
3251 t
->buffer
->debug_id
= t
->debug_id
;
3252 t
->buffer
->transaction
= t
;
3253 t
->buffer
->target_node
= target_node
;
3254 trace_binder_transaction_alloc_buf(t
->buffer
);
3255 off_start
= (binder_size_t
*)(t
->buffer
->data
+
3256 ALIGN(tr
->data_size
, sizeof(void *)));
3259 if (copy_from_user(t
->buffer
->data
, (const void __user
*)(uintptr_t)
3260 tr
->data
.ptr
.buffer
, tr
->data_size
)) {
3261 binder_user_error("%d:%d(%s:%s) got transaction with invalid data ptr\n",
3262 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3263 return_error
= BR_FAILED_REPLY
;
3264 return_error_param
= -EFAULT
;
3265 return_error_line
= __LINE__
;
3266 goto err_copy_data_failed
;
3268 if (copy_from_user(offp
, (const void __user
*)(uintptr_t)
3269 tr
->data
.ptr
.offsets
, tr
->offsets_size
)) {
3270 binder_user_error("%d:%d(%s:%s) got transaction with invalid offsets ptr\n",
3271 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3272 return_error
= BR_FAILED_REPLY
;
3273 return_error_param
= -EFAULT
;
3274 return_error_line
= __LINE__
;
3275 goto err_copy_data_failed
;
3277 if (!IS_ALIGNED(tr
->offsets_size
, sizeof(binder_size_t
))) {
3278 binder_user_error("%d:%d(%s:%s) got transaction with invalid offsets size, %lld\n",
3279 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, (u64
)tr
->offsets_size
);
3280 return_error
= BR_FAILED_REPLY
;
3281 return_error_param
= -EINVAL
;
3282 return_error_line
= __LINE__
;
3283 goto err_bad_offset
;
3285 if (!IS_ALIGNED(extra_buffers_size
, sizeof(u64
))) {
3286 binder_user_error("%d:%d(%s:%s) got transaction with unaligned buffers size, %lld\n",
3287 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3288 (u64
)extra_buffers_size
);
3289 return_error
= BR_FAILED_REPLY
;
3290 return_error_param
= -EINVAL
;
3291 return_error_line
= __LINE__
;
3292 goto err_bad_offset
;
3294 off_end
= (void *)off_start
+ tr
->offsets_size
;
3295 sg_bufp
= (u8
*)(PTR_ALIGN(off_end
, sizeof(void *)));
3296 sg_buf_end
= sg_bufp
+ extra_buffers_size
;
3298 for (; offp
< off_end
; offp
++) {
3299 struct binder_object_header
*hdr
;
3300 size_t object_size
= binder_validate_object(t
->buffer
, *offp
);
3302 if (object_size
== 0 || *offp
< off_min
) {
3303 binder_user_error("%d:%d(%s:%s) got transaction with invalid offset (%lld, min %lld max %lld)"\
3305 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3308 (u64
)t
->buffer
->data_size
);
3309 return_error
= BR_FAILED_REPLY
;
3310 return_error_param
= -EINVAL
;
3311 return_error_line
= __LINE__
;
3312 goto err_bad_offset
;
3315 hdr
= (struct binder_object_header
*)(t
->buffer
->data
+ *offp
);
3316 off_min
= *offp
+ object_size
;
3317 switch (hdr
->type
) {
3318 case BINDER_TYPE_BINDER
:
3319 case BINDER_TYPE_WEAK_BINDER
: {
3320 struct flat_binder_object
*fp
;
3322 fp
= to_flat_binder_object(hdr
);
3323 ret
= binder_translate_binder(fp
, t
, thread
);
3325 return_error
= BR_FAILED_REPLY
;
3326 return_error_param
= ret
;
3327 return_error_line
= __LINE__
;
3328 goto err_translate_failed
;
3331 case BINDER_TYPE_HANDLE
:
3332 case BINDER_TYPE_WEAK_HANDLE
: {
3333 struct flat_binder_object
*fp
;
3335 fp
= to_flat_binder_object(hdr
);
3336 ret
= binder_translate_handle(fp
, t
, thread
);
3338 return_error
= BR_FAILED_REPLY
;
3339 return_error_param
= ret
;
3340 return_error_line
= __LINE__
;
3341 goto err_translate_failed
;
3345 case BINDER_TYPE_FD
: {
3346 struct binder_fd_object
*fp
= to_binder_fd_object(hdr
);
3347 int target_fd
= binder_translate_fd(fp
->fd
, t
, thread
,
3350 if (target_fd
< 0) {
3351 return_error
= BR_FAILED_REPLY
;
3352 return_error_param
= target_fd
;
3353 return_error_line
= __LINE__
;
3354 goto err_translate_failed
;
3359 case BINDER_TYPE_FDA
: {
3360 struct binder_fd_array_object
*fda
=
3361 to_binder_fd_array_object(hdr
);
3362 struct binder_buffer_object
*parent
=
3363 binder_validate_ptr(t
->buffer
, fda
->parent
,
3367 binder_user_error("%d:%d(%s:%s) got transaction with invalid parent offset or type\n",
3368 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3369 return_error
= BR_FAILED_REPLY
;
3370 return_error_param
= -EINVAL
;
3371 return_error_line
= __LINE__
;
3372 goto err_bad_parent
;
3374 if (!binder_validate_fixup(t
->buffer
, off_start
,
3375 parent
, fda
->parent_offset
,
3377 last_fixup_min_off
)) {
3378 binder_user_error("%d:%d(%s:%s) got transaction with out-of-order buffer fixup\n",
3379 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3380 return_error
= BR_FAILED_REPLY
;
3381 return_error_param
= -EINVAL
;
3382 return_error_line
= __LINE__
;
3383 goto err_bad_parent
;
3385 ret
= binder_translate_fd_array(fda
, parent
, t
, thread
,
3388 return_error
= BR_FAILED_REPLY
;
3389 return_error_param
= ret
;
3390 return_error_line
= __LINE__
;
3391 goto err_translate_failed
;
3393 last_fixup_obj
= parent
;
3394 last_fixup_min_off
=
3395 fda
->parent_offset
+ sizeof(u32
) * fda
->num_fds
;
3397 case BINDER_TYPE_PTR
: {
3398 struct binder_buffer_object
*bp
=
3399 to_binder_buffer_object(hdr
);
3400 size_t buf_left
= sg_buf_end
- sg_bufp
;
3402 if (bp
->length
> buf_left
) {
3403 binder_user_error("%d:%d(%s:%s) got transaction with too large buffer\n",
3404 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3405 return_error
= BR_FAILED_REPLY
;
3406 return_error_param
= -EINVAL
;
3407 return_error_line
= __LINE__
;
3408 goto err_bad_offset
;
3410 if (copy_from_user(sg_bufp
,
3411 (const void __user
*)(uintptr_t)
3412 bp
->buffer
, bp
->length
)) {
3413 binder_user_error("%d:%d(%s:%s) got transaction with invalid offsets ptr\n",
3414 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3415 return_error_param
= -EFAULT
;
3416 return_error
= BR_FAILED_REPLY
;
3417 return_error_line
= __LINE__
;
3418 goto err_copy_data_failed
;
3420 /* Fixup buffer pointer to target proc address space */
3421 bp
->buffer
= (uintptr_t)sg_bufp
+
3422 binder_alloc_get_user_buffer_offset(
3423 &target_proc
->alloc
);
3424 sg_bufp
+= ALIGN(bp
->length
, sizeof(u64
));
3426 ret
= binder_fixup_parent(t
, thread
, bp
, off_start
,
3429 last_fixup_min_off
);
3431 return_error
= BR_FAILED_REPLY
;
3432 return_error_param
= ret
;
3433 return_error_line
= __LINE__
;
3434 goto err_translate_failed
;
3436 last_fixup_obj
= bp
;
3437 last_fixup_min_off
= 0;
3440 binder_user_error("%d:%d(%s:%s) got transaction with invalid object type, %x\n",
3441 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, hdr
->type
);
3442 return_error
= BR_FAILED_REPLY
;
3443 return_error_param
= -EINVAL
;
3444 return_error_line
= __LINE__
;
3445 goto err_bad_object_type
;
3448 tcomplete
->type
= BINDER_WORK_TRANSACTION_COMPLETE
;
3449 t
->work
.type
= BINDER_WORK_TRANSACTION
;
3452 binder_enqueue_thread_work(thread
, tcomplete
);
3453 binder_inner_proc_lock(target_proc
);
3454 if (target_thread
->is_dead
) {
3455 binder_inner_proc_unlock(target_proc
);
3456 goto err_dead_proc_or_thread
;
3458 BUG_ON(t
->buffer
->async_transaction
!= 0);
3459 binder_pop_transaction_ilocked(target_thread
, in_reply_to
);
3460 binder_enqueue_thread_work_ilocked(target_thread
, &t
->work
);
3461 binder_inner_proc_unlock(target_proc
);
3462 wake_up_interruptible_sync(&target_thread
->wait
);
3463 binder_restore_priority(current
, in_reply_to
->saved_priority
);
3464 binder_free_transaction(in_reply_to
);
3465 } else if (!(t
->flags
& TF_ONE_WAY
)) {
3466 BUG_ON(t
->buffer
->async_transaction
!= 0);
3467 binder_inner_proc_lock(proc
);
3469 * Defer the TRANSACTION_COMPLETE, so we don't return to
3470 * userspace immediately; this allows the target process to
3471 * immediately start processing this transaction, reducing
3472 * latency. We will then return the TRANSACTION_COMPLETE when
3473 * the target replies (or there is an error).
3475 binder_enqueue_deferred_thread_work_ilocked(thread
, tcomplete
);
3477 t
->from_parent
= thread
->transaction_stack
;
3478 thread
->transaction_stack
= t
;
3479 binder_inner_proc_unlock(proc
);
3480 if (!binder_proc_transaction(t
, target_proc
, target_thread
)) {
3481 binder_inner_proc_lock(proc
);
3482 binder_pop_transaction_ilocked(thread
, t
);
3483 binder_inner_proc_unlock(proc
);
3484 goto err_dead_proc_or_thread
;
3487 BUG_ON(target_node
== NULL
);
3488 BUG_ON(t
->buffer
->async_transaction
!= 1);
3489 binder_enqueue_thread_work(thread
, tcomplete
);
3490 if (!binder_proc_transaction(t
, target_proc
, NULL
))
3491 goto err_dead_proc_or_thread
;
3494 binder_thread_dec_tmpref(target_thread
);
3495 binder_proc_dec_tmpref(target_proc
);
3497 binder_dec_node_tmpref(target_node
);
3499 * write barrier to synchronize with initialization
3503 WRITE_ONCE(e
->debug_id_done
, t_debug_id
);
3506 err_dead_proc_or_thread
:
3507 return_error
= BR_DEAD_REPLY
;
3508 return_error_line
= __LINE__
;
3509 binder_dequeue_work(proc
, tcomplete
);
3510 err_translate_failed
:
3511 err_bad_object_type
:
3514 err_copy_data_failed
:
3515 trace_binder_transaction_failed_buffer_release(t
->buffer
);
3516 binder_transaction_buffer_release(target_proc
, t
->buffer
, offp
);
3518 binder_dec_node_tmpref(target_node
);
3520 t
->buffer
->transaction
= NULL
;
3521 binder_alloc_free_buf(&target_proc
->alloc
, t
->buffer
);
3522 err_binder_alloc_buf_failed
:
3524 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE
);
3525 err_alloc_tcomplete_failed
:
3527 binder_stats_deleted(BINDER_STAT_TRANSACTION
);
3530 err_empty_call_stack
:
3532 err_invalid_target_handle
:
3534 binder_thread_dec_tmpref(target_thread
);
3536 binder_proc_dec_tmpref(target_proc
);
3538 binder_dec_node(target_node
, 1, 0);
3539 binder_dec_node_tmpref(target_node
);
3542 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
3543 "%d:%d(%s:%s) transaction failed %d/%d, size %lld-%lld line %d\n",
3544 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3545 return_error
, return_error_param
,
3546 (u64
)tr
->data_size
, (u64
)tr
->offsets_size
,
3550 struct binder_transaction_log_entry
*fe
;
3552 e
->return_error
= return_error
;
3553 e
->return_error_param
= return_error_param
;
3554 e
->return_error_line
= return_error_line
;
3555 #ifdef CONFIG_DEBUG_SNAPSHOT_BINDER
3556 dss_binder_transaction_failed(reply
, e
, proc
->tsk
->comm
, thread
->task
->comm
, tr
->flags
, tr
->code
);
3558 fe
= binder_transaction_log_add(&binder_transaction_log_failed
);
3561 * write barrier to synchronize with initialization
3565 WRITE_ONCE(e
->debug_id_done
, t_debug_id
);
3566 WRITE_ONCE(fe
->debug_id_done
, t_debug_id
);
3569 BUG_ON(thread
->return_error
.cmd
!= BR_OK
);
3571 binder_restore_priority(current
, in_reply_to
->saved_priority
);
3572 thread
->return_error
.cmd
= BR_TRANSACTION_COMPLETE
;
3573 binder_enqueue_thread_work(thread
, &thread
->return_error
.work
);
3574 binder_send_failed_reply(in_reply_to
, return_error
);
3576 thread
->return_error
.cmd
= return_error
;
3577 binder_enqueue_thread_work(thread
, &thread
->return_error
.work
);
3581 static int binder_thread_write(struct binder_proc
*proc
,
3582 struct binder_thread
*thread
,
3583 binder_uintptr_t binder_buffer
, size_t size
,
3584 binder_size_t
*consumed
)
3587 struct binder_context
*context
= proc
->context
;
3588 void __user
*buffer
= (void __user
*)(uintptr_t)binder_buffer
;
3589 void __user
*ptr
= buffer
+ *consumed
;
3590 void __user
*end
= buffer
+ size
;
3592 while (ptr
< end
&& thread
->return_error
.cmd
== BR_OK
) {
3595 if (get_user(cmd
, (uint32_t __user
*)ptr
))
3597 ptr
+= sizeof(uint32_t);
3598 trace_binder_command(cmd
);
3599 if (_IOC_NR(cmd
) < ARRAY_SIZE(binder_stats
.bc
)) {
3600 atomic_inc(&binder_stats
.bc
[_IOC_NR(cmd
)]);
3601 atomic_inc(&proc
->stats
.bc
[_IOC_NR(cmd
)]);
3602 atomic_inc(&thread
->stats
.bc
[_IOC_NR(cmd
)]);
3610 const char *debug_string
;
3611 bool strong
= cmd
== BC_ACQUIRE
|| cmd
== BC_RELEASE
;
3612 bool increment
= cmd
== BC_INCREFS
|| cmd
== BC_ACQUIRE
;
3613 struct binder_ref_data rdata
;
3615 if (get_user(target
, (uint32_t __user
*)ptr
))
3618 ptr
+= sizeof(uint32_t);
3620 if (increment
&& !target
) {
3621 struct binder_node
*ctx_mgr_node
;
3622 mutex_lock(&context
->context_mgr_node_lock
);
3623 ctx_mgr_node
= context
->binder_context_mgr_node
;
3625 ret
= binder_inc_ref_for_node(
3627 strong
, NULL
, &rdata
);
3628 mutex_unlock(&context
->context_mgr_node_lock
);
3631 ret
= binder_update_ref_for_handle(
3632 proc
, target
, increment
, strong
,
3634 if (!ret
&& rdata
.desc
!= target
) {
3635 binder_user_error("%d:%d(%s:%s) tried to acquire reference to"\
3636 " desc %d, got %d instead\n",
3637 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3638 target
, rdata
.desc
);
3642 debug_string
= "IncRefs";
3645 debug_string
= "Acquire";
3648 debug_string
= "Release";
3652 debug_string
= "DecRefs";
3656 binder_user_error("%d:%d(%s:%s) %s %d refcount change on invalid ref %d ret %d\n",
3657 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3658 debug_string
, strong
, target
, ret
);
3661 binder_debug(BINDER_DEBUG_USER_REFS
,
3662 "%d:%d(%s:%s) %s ref %d desc %d s %d w %d\n",
3663 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3664 debug_string
, rdata
.debug_id
, rdata
.desc
, rdata
.strong
,
3668 case BC_INCREFS_DONE
:
3669 case BC_ACQUIRE_DONE
: {
3670 binder_uintptr_t node_ptr
;
3671 binder_uintptr_t cookie
;
3672 struct binder_node
*node
;
3675 if (get_user(node_ptr
, (binder_uintptr_t __user
*)ptr
))
3677 ptr
+= sizeof(binder_uintptr_t
);
3678 if (get_user(cookie
, (binder_uintptr_t __user
*)ptr
))
3680 ptr
+= sizeof(binder_uintptr_t
);
3681 node
= binder_get_node(proc
, node_ptr
);
3683 binder_user_error("%d:%d(%s:%s) %s u%016llx no match\n",
3684 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3685 cmd
== BC_INCREFS_DONE
?
3691 if (cookie
!= node
->cookie
) {
3692 binder_user_error("%d:%d(%s:%s) %s u%016llx node %d cookie mismatch"\
3693 " %016llx != %016llx\n",
3694 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3695 cmd
== BC_INCREFS_DONE
?
3696 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
3697 (u64
)node_ptr
, node
->debug_id
,
3698 (u64
)cookie
, (u64
)node
->cookie
);
3699 binder_put_node(node
);
3702 binder_node_inner_lock(node
);
3703 if (cmd
== BC_ACQUIRE_DONE
) {
3704 if (node
->pending_strong_ref
== 0) {
3705 binder_user_error("%d:%d(%s:%s) BC_ACQUIRE_DONE node %d"\
3706 " has no pending acquire request\n",
3707 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3709 binder_node_inner_unlock(node
);
3710 binder_put_node(node
);
3713 node
->pending_strong_ref
= 0;
3715 if (node
->pending_weak_ref
== 0) {
3716 binder_user_error("%d:%d(%s:%s) BC_INCREFS_DONE node %d"\
3717 " has no pending increfs request\n",
3718 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3720 binder_node_inner_unlock(node
);
3721 binder_put_node(node
);
3724 node
->pending_weak_ref
= 0;
3726 free_node
= binder_dec_node_nilocked(node
,
3727 cmd
== BC_ACQUIRE_DONE
, 0);
3729 binder_debug(BINDER_DEBUG_USER_REFS
,
3730 "%d:%d(%s:%s) %s node %d ls %d lw %d tr %d\n",
3731 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3732 cmd
== BC_INCREFS_DONE
? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
3733 node
->debug_id
, node
->local_strong_refs
,
3734 node
->local_weak_refs
, node
->tmp_refs
);
3735 binder_node_inner_unlock(node
);
3736 binder_put_node(node
);
3739 case BC_ATTEMPT_ACQUIRE
:
3740 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
3742 case BC_ACQUIRE_RESULT
:
3743 pr_err("BC_ACQUIRE_RESULT not supported\n");
3746 case BC_FREE_BUFFER
: {
3747 binder_uintptr_t data_ptr
;
3748 struct binder_buffer
*buffer
;
3750 if (get_user(data_ptr
, (binder_uintptr_t __user
*)ptr
))
3752 ptr
+= sizeof(binder_uintptr_t
);
3754 buffer
= binder_alloc_prepare_to_free(&proc
->alloc
,
3756 if (buffer
== NULL
) {
3757 binder_user_error("%d:%d(%s:%s) BC_FREE_BUFFER u%016llx no match\n",
3758 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, (u64
)data_ptr
);
3761 if (!buffer
->allow_user_free
) {
3762 binder_user_error("%d:%d(%s:%s) BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
3763 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, (u64
)data_ptr
);
3766 binder_debug(BINDER_DEBUG_FREE_BUFFER
,
3767 "%d:%d(%s:%s) BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
3768 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, (u64
)data_ptr
,
3770 buffer
->transaction
? "active" : "finished");
3772 if (buffer
->transaction
) {
3773 buffer
->transaction
->buffer
= NULL
;
3774 buffer
->transaction
= NULL
;
3776 if (buffer
->async_transaction
&& buffer
->target_node
) {
3777 struct binder_node
*buf_node
;
3778 struct binder_work
*w
;
3780 buf_node
= buffer
->target_node
;
3781 binder_node_inner_lock(buf_node
);
3782 BUG_ON(!buf_node
->has_async_transaction
);
3783 BUG_ON(buf_node
->proc
!= proc
);
3784 w
= binder_dequeue_work_head_ilocked(
3785 &buf_node
->async_todo
);
3787 buf_node
->has_async_transaction
= 0;
3789 binder_enqueue_work_ilocked(
3791 binder_wakeup_proc_ilocked(proc
);
3793 binder_node_inner_unlock(buf_node
);
3795 trace_binder_transaction_buffer_release(buffer
);
3796 binder_transaction_buffer_release(proc
, buffer
, NULL
);
3797 binder_alloc_free_buf(&proc
->alloc
, buffer
);
3801 case BC_TRANSACTION_SG
:
3803 struct binder_transaction_data_sg tr
;
3805 if (copy_from_user(&tr
, ptr
, sizeof(tr
)))
3808 binder_transaction(proc
, thread
, &tr
.transaction_data
,
3809 cmd
== BC_REPLY_SG
, tr
.buffers_size
);
3812 case BC_TRANSACTION
:
3814 struct binder_transaction_data tr
;
3816 if (copy_from_user(&tr
, ptr
, sizeof(tr
)))
3819 binder_transaction(proc
, thread
, &tr
,
3820 cmd
== BC_REPLY
, 0);
3824 case BC_REGISTER_LOOPER
:
3825 binder_debug(BINDER_DEBUG_THREADS
,
3826 "%d:%d(%s:%s) BC_REGISTER_LOOPER\n",
3827 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3828 binder_inner_proc_lock(proc
);
3829 if (thread
->looper
& BINDER_LOOPER_STATE_ENTERED
) {
3830 thread
->looper
|= BINDER_LOOPER_STATE_INVALID
;
3831 binder_user_error("%d:%d(%s:%s) ERROR: BC_REGISTER_LOOPER"\
3832 " called after BC_ENTER_LOOPER\n",
3833 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3834 } else if (proc
->requested_threads
== 0) {
3835 thread
->looper
|= BINDER_LOOPER_STATE_INVALID
;
3836 binder_user_error("%d:%d(%s:%s) ERROR: BC_REGISTER_LOOPER called without request\n",
3837 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3839 proc
->requested_threads
--;
3840 proc
->requested_threads_started
++;
3842 thread
->looper
|= BINDER_LOOPER_STATE_REGISTERED
;
3843 binder_inner_proc_unlock(proc
);
3845 case BC_ENTER_LOOPER
:
3846 binder_debug(BINDER_DEBUG_THREADS
,
3847 "%d:%d(%s:%s) BC_ENTER_LOOPER\n",
3848 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3849 if (thread
->looper
& BINDER_LOOPER_STATE_REGISTERED
) {
3850 thread
->looper
|= BINDER_LOOPER_STATE_INVALID
;
3851 binder_user_error("%d:%d(%s:%s) ERROR: BC_ENTER_LOOPER"\
3852 " called after BC_REGISTER_LOOPER\n",
3853 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3855 thread
->looper
|= BINDER_LOOPER_STATE_ENTERED
;
3857 case BC_EXIT_LOOPER
:
3858 binder_debug(BINDER_DEBUG_THREADS
,
3859 "%d:%d(%s:%s) BC_EXIT_LOOPER\n",
3860 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3861 thread
->looper
|= BINDER_LOOPER_STATE_EXITED
;
3864 case BC_REQUEST_DEATH_NOTIFICATION
:
3865 case BC_CLEAR_DEATH_NOTIFICATION
: {
3867 binder_uintptr_t cookie
;
3868 struct binder_ref
*ref
;
3869 struct binder_ref_death
*death
= NULL
;
3871 if (get_user(target
, (uint32_t __user
*)ptr
))
3873 ptr
+= sizeof(uint32_t);
3874 if (get_user(cookie
, (binder_uintptr_t __user
*)ptr
))
3876 ptr
+= sizeof(binder_uintptr_t
);
3877 if (cmd
== BC_REQUEST_DEATH_NOTIFICATION
) {
3879 * Allocate memory for death notification
3880 * before taking lock
3882 death
= kzalloc(sizeof(*death
), GFP_KERNEL
);
3883 if (death
== NULL
) {
3884 WARN_ON(thread
->return_error
.cmd
!=
3886 thread
->return_error
.cmd
= BR_ERROR
;
3887 binder_enqueue_thread_work(
3889 &thread
->return_error
.work
);
3891 BINDER_DEBUG_FAILED_TRANSACTION
,
3892 "%d:%d(%s:%s) BC_REQUEST_DEATH_NOTIFICATION failed\n",
3893 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3897 binder_proc_lock(proc
);
3898 ref
= binder_get_ref_olocked(proc
, target
, false);
3900 binder_user_error("%d:%d(%s:%s) %s invalid ref %d\n",
3901 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3902 cmd
== BC_REQUEST_DEATH_NOTIFICATION
?
3903 "BC_REQUEST_DEATH_NOTIFICATION" :
3904 "BC_CLEAR_DEATH_NOTIFICATION",
3906 binder_proc_unlock(proc
);
3911 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION
,
3912 "%d:%d(%s:%s) %s %016llx ref %d desc %d s %d w %d for node %d\n",
3913 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3914 cmd
== BC_REQUEST_DEATH_NOTIFICATION
?
3915 "BC_REQUEST_DEATH_NOTIFICATION" :
3916 "BC_CLEAR_DEATH_NOTIFICATION",
3917 (u64
)cookie
, ref
->data
.debug_id
,
3918 ref
->data
.desc
, ref
->data
.strong
,
3919 ref
->data
.weak
, ref
->node
->debug_id
);
3921 binder_node_lock(ref
->node
);
3922 if (cmd
== BC_REQUEST_DEATH_NOTIFICATION
) {
3924 binder_user_error("%d:%d(%s:%s) BC_REQUEST_DEATH_NOTIFICATION"\
3925 " death notification already set\n",
3926 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3927 binder_node_unlock(ref
->node
);
3928 binder_proc_unlock(proc
);
3932 binder_stats_created(BINDER_STAT_DEATH
);
3933 INIT_LIST_HEAD(&death
->work
.entry
);
3934 death
->cookie
= cookie
;
3936 if (ref
->node
->proc
== NULL
) {
3937 ref
->death
->work
.type
= BINDER_WORK_DEAD_BINDER
;
3939 binder_inner_proc_lock(proc
);
3940 binder_enqueue_work_ilocked(
3941 &ref
->death
->work
, &proc
->todo
);
3942 binder_wakeup_proc_ilocked(proc
);
3943 binder_inner_proc_unlock(proc
);
3946 if (ref
->death
== NULL
) {
3947 binder_user_error("%d:%d(%s:%s) BC_CLEAR_DEATH_NOTIFICATION"\
3948 " death notification not active\n",
3949 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
3950 binder_node_unlock(ref
->node
);
3951 binder_proc_unlock(proc
);
3955 if (death
->cookie
!= cookie
) {
3956 binder_user_error("%d:%d(%s:%s) BC_CLEAR_DEATH_NOTIFICATION"\
3957 " death notification cookie mismatch %016llx != %016llx\n",
3958 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
3961 binder_node_unlock(ref
->node
);
3962 binder_proc_unlock(proc
);
3966 binder_inner_proc_lock(proc
);
3967 if (list_empty(&death
->work
.entry
)) {
3968 death
->work
.type
= BINDER_WORK_CLEAR_DEATH_NOTIFICATION
;
3969 if (thread
->looper
&
3970 (BINDER_LOOPER_STATE_REGISTERED
|
3971 BINDER_LOOPER_STATE_ENTERED
))
3972 binder_enqueue_thread_work_ilocked(
3976 binder_enqueue_work_ilocked(
3979 binder_wakeup_proc_ilocked(
3983 BUG_ON(death
->work
.type
!= BINDER_WORK_DEAD_BINDER
);
3984 death
->work
.type
= BINDER_WORK_DEAD_BINDER_AND_CLEAR
;
3986 binder_inner_proc_unlock(proc
);
3988 binder_node_unlock(ref
->node
);
3989 binder_proc_unlock(proc
);
3991 case BC_DEAD_BINDER_DONE
: {
3992 struct binder_work
*w
;
3993 binder_uintptr_t cookie
;
3994 struct binder_ref_death
*death
= NULL
;
3996 if (get_user(cookie
, (binder_uintptr_t __user
*)ptr
))
3999 ptr
+= sizeof(cookie
);
4000 binder_inner_proc_lock(proc
);
4001 list_for_each_entry(w
, &proc
->delivered_death
,
4003 struct binder_ref_death
*tmp_death
=
4005 struct binder_ref_death
,
4008 if (tmp_death
->cookie
== cookie
) {
4013 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
4014 "%d:%d(%s:%s) BC_DEAD_BINDER_DONE %016llx found %pK\n",
4015 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, (u64
)cookie
,
4017 if (death
== NULL
) {
4018 binder_user_error("%d:%d(%s:%s) BC_DEAD_BINDER_DONE %016llx not found\n",
4019 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, (u64
)cookie
);
4020 binder_inner_proc_unlock(proc
);
4023 binder_dequeue_work_ilocked(&death
->work
);
4024 if (death
->work
.type
== BINDER_WORK_DEAD_BINDER_AND_CLEAR
) {
4025 death
->work
.type
= BINDER_WORK_CLEAR_DEATH_NOTIFICATION
;
4026 if (thread
->looper
&
4027 (BINDER_LOOPER_STATE_REGISTERED
|
4028 BINDER_LOOPER_STATE_ENTERED
))
4029 binder_enqueue_thread_work_ilocked(
4030 thread
, &death
->work
);
4032 binder_enqueue_work_ilocked(
4035 binder_wakeup_proc_ilocked(proc
);
4038 binder_inner_proc_unlock(proc
);
4042 pr_err("%d:%d(%s:%s) unknown command %d\n",
4043 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, cmd
);
4046 *consumed
= ptr
- buffer
;
4051 static void binder_stat_br(struct binder_proc
*proc
,
4052 struct binder_thread
*thread
, uint32_t cmd
)
4054 trace_binder_return(cmd
);
4055 if (_IOC_NR(cmd
) < ARRAY_SIZE(binder_stats
.br
)) {
4056 atomic_inc(&binder_stats
.br
[_IOC_NR(cmd
)]);
4057 atomic_inc(&proc
->stats
.br
[_IOC_NR(cmd
)]);
4058 atomic_inc(&thread
->stats
.br
[_IOC_NR(cmd
)]);
4062 static int binder_put_node_cmd(struct binder_proc
*proc
,
4063 struct binder_thread
*thread
,
4065 binder_uintptr_t node_ptr
,
4066 binder_uintptr_t node_cookie
,
4068 uint32_t cmd
, const char *cmd_name
)
4070 void __user
*ptr
= *ptrp
;
4072 if (put_user(cmd
, (uint32_t __user
*)ptr
))
4074 ptr
+= sizeof(uint32_t);
4076 if (put_user(node_ptr
, (binder_uintptr_t __user
*)ptr
))
4078 ptr
+= sizeof(binder_uintptr_t
);
4080 if (put_user(node_cookie
, (binder_uintptr_t __user
*)ptr
))
4082 ptr
+= sizeof(binder_uintptr_t
);
4084 binder_stat_br(proc
, thread
, cmd
);
4085 binder_debug(BINDER_DEBUG_USER_REFS
, "%d:%d(%s:%s) %s %d u%016llx c%016llx\n",
4086 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
4087 cmd_name
, node_debug_id
,
4088 (u64
)node_ptr
, (u64
)node_cookie
);
4094 static int binder_wait_for_work(struct binder_thread
*thread
,
4098 struct binder_proc
*proc
= thread
->proc
;
4101 freezer_do_not_count();
4102 binder_inner_proc_lock(proc
);
4104 prepare_to_wait(&thread
->wait
, &wait
, TASK_INTERRUPTIBLE
);
4105 if (binder_has_work_ilocked(thread
, do_proc_work
))
4108 list_add(&thread
->waiting_thread_node
,
4109 &proc
->waiting_threads
);
4110 binder_inner_proc_unlock(proc
);
4112 binder_inner_proc_lock(proc
);
4113 list_del_init(&thread
->waiting_thread_node
);
4114 if (signal_pending(current
)) {
4119 finish_wait(&thread
->wait
, &wait
);
4120 binder_inner_proc_unlock(proc
);
4126 static int binder_thread_read(struct binder_proc
*proc
,
4127 struct binder_thread
*thread
,
4128 binder_uintptr_t binder_buffer
, size_t size
,
4129 binder_size_t
*consumed
, int non_block
)
4131 void __user
*buffer
= (void __user
*)(uintptr_t)binder_buffer
;
4132 void __user
*ptr
= buffer
+ *consumed
;
4133 void __user
*end
= buffer
+ size
;
4136 int wait_for_proc_work
;
4138 if (*consumed
== 0) {
4139 if (put_user(BR_NOOP
, (uint32_t __user
*)ptr
))
4141 ptr
+= sizeof(uint32_t);
4145 binder_inner_proc_lock(proc
);
4146 wait_for_proc_work
= binder_available_for_proc_work_ilocked(thread
);
4147 binder_inner_proc_unlock(proc
);
4149 thread
->looper
|= BINDER_LOOPER_STATE_WAITING
;
4151 trace_binder_wait_for_work(wait_for_proc_work
,
4152 !!thread
->transaction_stack
,
4153 !binder_worklist_empty(proc
, &thread
->todo
));
4154 if (wait_for_proc_work
) {
4155 if (!(thread
->looper
& (BINDER_LOOPER_STATE_REGISTERED
|
4156 BINDER_LOOPER_STATE_ENTERED
))) {
4157 binder_user_error("%d:%d(%s:%s) ERROR: Thread waiting for process work"\
4158 " before calling BC_REGISTER_LOOPER or BC_ENTER_LOOPER (state %x)\n",
4159 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
, thread
->looper
);
4160 wait_event_interruptible(binder_user_error_wait
,
4161 binder_stop_on_user_error
< 2);
4163 binder_restore_priority(current
, proc
->default_priority
);
4167 if (!binder_has_work(thread
, wait_for_proc_work
))
4170 ret
= binder_wait_for_work(thread
, wait_for_proc_work
);
4173 thread
->looper
&= ~BINDER_LOOPER_STATE_WAITING
;
4180 struct binder_transaction_data tr
;
4181 struct binder_work
*w
= NULL
;
4182 struct list_head
*list
= NULL
;
4183 struct binder_transaction
*t
= NULL
;
4184 struct binder_thread
*t_from
;
4186 binder_inner_proc_lock(proc
);
4187 if (!binder_worklist_empty_ilocked(&thread
->todo
))
4188 list
= &thread
->todo
;
4189 else if (!binder_worklist_empty_ilocked(&proc
->todo
) &&
4193 binder_inner_proc_unlock(proc
);
4196 if (ptr
- buffer
== 4 && !thread
->looper_need_return
)
4201 if (end
- ptr
< sizeof(tr
) + 4) {
4202 binder_inner_proc_unlock(proc
);
4205 w
= binder_dequeue_work_head_ilocked(list
);
4206 if (binder_worklist_empty_ilocked(&thread
->todo
))
4207 thread
->process_todo
= false;
4210 case BINDER_WORK_TRANSACTION
: {
4211 binder_inner_proc_unlock(proc
);
4212 t
= container_of(w
, struct binder_transaction
, work
);
4214 case BINDER_WORK_RETURN_ERROR
: {
4215 struct binder_error
*e
= container_of(
4216 w
, struct binder_error
, work
);
4218 WARN_ON(e
->cmd
== BR_OK
);
4219 binder_inner_proc_unlock(proc
);
4220 if (put_user(e
->cmd
, (uint32_t __user
*)ptr
))
4223 ptr
+= sizeof(uint32_t);
4225 binder_stat_br(proc
, thread
, e
->cmd
);
4227 case BINDER_WORK_TRANSACTION_COMPLETE
: {
4228 binder_inner_proc_unlock(proc
);
4229 cmd
= BR_TRANSACTION_COMPLETE
;
4230 if (put_user(cmd
, (uint32_t __user
*)ptr
))
4232 ptr
+= sizeof(uint32_t);
4234 binder_stat_br(proc
, thread
, cmd
);
4235 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE
,
4236 "%d:%d(%s:%s) BR_TRANSACTION_COMPLETE\n",
4237 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
4239 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE
);
4241 case BINDER_WORK_NODE
: {
4242 struct binder_node
*node
= container_of(w
, struct binder_node
, work
);
4244 binder_uintptr_t node_ptr
= node
->ptr
;
4245 binder_uintptr_t node_cookie
= node
->cookie
;
4246 int node_debug_id
= node
->debug_id
;
4249 void __user
*orig_ptr
= ptr
;
4251 BUG_ON(proc
!= node
->proc
);
4252 strong
= node
->internal_strong_refs
||
4253 node
->local_strong_refs
;
4254 weak
= !hlist_empty(&node
->refs
) ||
4255 node
->local_weak_refs
||
4256 node
->tmp_refs
|| strong
;
4257 has_strong_ref
= node
->has_strong_ref
;
4258 has_weak_ref
= node
->has_weak_ref
;
4260 if (weak
&& !has_weak_ref
) {
4261 node
->has_weak_ref
= 1;
4262 node
->pending_weak_ref
= 1;
4263 node
->local_weak_refs
++;
4265 if (strong
&& !has_strong_ref
) {
4266 node
->has_strong_ref
= 1;
4267 node
->pending_strong_ref
= 1;
4268 node
->local_strong_refs
++;
4270 if (!strong
&& has_strong_ref
)
4271 node
->has_strong_ref
= 0;
4272 if (!weak
&& has_weak_ref
)
4273 node
->has_weak_ref
= 0;
4274 if (!weak
&& !strong
) {
4275 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
4276 "%d:%d(%s:%s) node %d u%016llx c%016llx deleted\n",
4277 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
4281 rb_erase(&node
->rb_node
, &proc
->nodes
);
4282 binder_inner_proc_unlock(proc
);
4283 binder_node_lock(node
);
4285 * Acquire the node lock before freeing the
4286 * node to serialize with other threads that
4287 * may have been holding the node lock while
4288 * decrementing this node (avoids race where
4289 * this thread frees while the other thread
4290 * is unlocking the node after the final
4293 binder_node_unlock(node
);
4294 binder_free_node(node
);
4296 binder_inner_proc_unlock(proc
);
4298 if (weak
&& !has_weak_ref
)
4299 ret
= binder_put_node_cmd(
4300 proc
, thread
, &ptr
, node_ptr
,
4301 node_cookie
, node_debug_id
,
4302 BR_INCREFS
, "BR_INCREFS");
4303 if (!ret
&& strong
&& !has_strong_ref
)
4304 ret
= binder_put_node_cmd(
4305 proc
, thread
, &ptr
, node_ptr
,
4306 node_cookie
, node_debug_id
,
4307 BR_ACQUIRE
, "BR_ACQUIRE");
4308 if (!ret
&& !strong
&& has_strong_ref
)
4309 ret
= binder_put_node_cmd(
4310 proc
, thread
, &ptr
, node_ptr
,
4311 node_cookie
, node_debug_id
,
4312 BR_RELEASE
, "BR_RELEASE");
4313 if (!ret
&& !weak
&& has_weak_ref
)
4314 ret
= binder_put_node_cmd(
4315 proc
, thread
, &ptr
, node_ptr
,
4316 node_cookie
, node_debug_id
,
4317 BR_DECREFS
, "BR_DECREFS");
4318 if (orig_ptr
== ptr
)
4319 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
4320 "%d:%d(%s:%s) node %d u%016llx c%016llx state unchanged\n",
4321 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
4328 case BINDER_WORK_DEAD_BINDER
:
4329 case BINDER_WORK_DEAD_BINDER_AND_CLEAR
:
4330 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION
: {
4331 struct binder_ref_death
*death
;
4333 binder_uintptr_t cookie
;
4335 death
= container_of(w
, struct binder_ref_death
, work
);
4336 if (w
->type
== BINDER_WORK_CLEAR_DEATH_NOTIFICATION
)
4337 cmd
= BR_CLEAR_DEATH_NOTIFICATION_DONE
;
4339 cmd
= BR_DEAD_BINDER
;
4340 cookie
= death
->cookie
;
4342 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION
,
4343 "%d:%d(%s:%s) %s %016llx\n",
4344 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
4345 cmd
== BR_DEAD_BINDER
?
4347 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
4349 if (w
->type
== BINDER_WORK_CLEAR_DEATH_NOTIFICATION
) {
4350 binder_inner_proc_unlock(proc
);
4352 binder_stats_deleted(BINDER_STAT_DEATH
);
4354 binder_enqueue_work_ilocked(
4355 w
, &proc
->delivered_death
);
4356 binder_inner_proc_unlock(proc
);
4358 if (put_user(cmd
, (uint32_t __user
*)ptr
))
4360 ptr
+= sizeof(uint32_t);
4361 if (put_user(cookie
,
4362 (binder_uintptr_t __user
*)ptr
))
4364 ptr
+= sizeof(binder_uintptr_t
);
4365 binder_stat_br(proc
, thread
, cmd
);
4366 if (cmd
== BR_DEAD_BINDER
)
4367 goto done
; /* DEAD_BINDER notifications can cause transactions */
4374 BUG_ON(t
->buffer
== NULL
);
4375 if (t
->buffer
->target_node
) {
4376 struct binder_node
*target_node
= t
->buffer
->target_node
;
4377 struct binder_priority node_prio
;
4379 tr
.target
.ptr
= target_node
->ptr
;
4380 tr
.cookie
= target_node
->cookie
;
4381 node_prio
.sched_policy
= target_node
->sched_policy
;
4382 node_prio
.prio
= target_node
->min_priority
;
4383 binder_transaction_priority(current
, t
, node_prio
,
4384 target_node
->inherit_rt
);
4385 cmd
= BR_TRANSACTION
;
4392 tr
.flags
= t
->flags
;
4393 tr
.sender_euid
= from_kuid(current_user_ns(), t
->sender_euid
);
4395 t_from
= binder_get_txn_from(t
);
4397 struct task_struct
*sender
= t_from
->proc
->tsk
;
4399 tr
.sender_pid
= task_tgid_nr_ns(sender
,
4400 task_active_pid_ns(current
));
4405 tr
.data_size
= t
->buffer
->data_size
;
4406 tr
.offsets_size
= t
->buffer
->offsets_size
;
4407 tr
.data
.ptr
.buffer
= (binder_uintptr_t
)
4408 ((uintptr_t)t
->buffer
->data
+
4409 binder_alloc_get_user_buffer_offset(&proc
->alloc
));
4410 tr
.data
.ptr
.offsets
= tr
.data
.ptr
.buffer
+
4411 ALIGN(t
->buffer
->data_size
,
4414 if (put_user(cmd
, (uint32_t __user
*)ptr
)) {
4416 binder_thread_dec_tmpref(t_from
);
4418 binder_cleanup_transaction(t
, "put_user failed",
4423 ptr
+= sizeof(uint32_t);
4424 if (copy_to_user(ptr
, &tr
, sizeof(tr
))) {
4426 binder_thread_dec_tmpref(t_from
);
4428 binder_cleanup_transaction(t
, "copy_to_user failed",
4435 #ifdef CONFIG_DEBUG_SNAPSHOT_BINDER
4436 dss_binder_transaction_received(t
, thread
);
4438 trace_binder_transaction_received(t
);
4439 binder_stat_br(proc
, thread
, cmd
);
4440 binder_debug(BINDER_DEBUG_TRANSACTION
,
4441 "%d:%d(%s:%s) %s %d %d:%d(%s:%s), cmd %d size %zd-%zd ptr %016llx-%016llx\n",
4442 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
4443 (cmd
== BR_TRANSACTION
) ? "BR_TRANSACTION" :
4445 t
->debug_id
, t_from
? t_from
->proc
->pid
: 0,
4446 t_from
? t_from
->pid
: 0,
4447 t_from
? t_from
->proc
->tsk
->comm
: "",
4448 t_from
? t_from
->task
->comm
: "",
4450 t
->buffer
->data_size
, t
->buffer
->offsets_size
,
4451 (u64
)tr
.data
.ptr
.buffer
, (u64
)tr
.data
.ptr
.offsets
);
4454 binder_thread_dec_tmpref(t_from
);
4455 t
->buffer
->allow_user_free
= 1;
4456 if (cmd
== BR_TRANSACTION
&& !(t
->flags
& TF_ONE_WAY
)) {
4457 binder_inner_proc_lock(thread
->proc
);
4458 t
->to_parent
= thread
->transaction_stack
;
4459 t
->to_thread
= thread
;
4460 thread
->transaction_stack
= t
;
4461 binder_inner_proc_unlock(thread
->proc
);
4463 binder_free_transaction(t
);
4470 *consumed
= ptr
- buffer
;
4471 binder_inner_proc_lock(proc
);
4472 if (proc
->requested_threads
== 0 &&
4473 list_empty(&thread
->proc
->waiting_threads
) &&
4474 proc
->requested_threads_started
< proc
->max_threads
&&
4475 (thread
->looper
& (BINDER_LOOPER_STATE_REGISTERED
|
4476 BINDER_LOOPER_STATE_ENTERED
)) /* the user-space code fails to */
4477 /*spawn a new thread if we leave this out */) {
4478 proc
->requested_threads
++;
4479 binder_inner_proc_unlock(proc
);
4480 binder_debug(BINDER_DEBUG_THREADS
,
4481 "%d:%d(%s:%s) BR_SPAWN_LOOPER\n",
4482 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
4483 if (put_user(BR_SPAWN_LOOPER
, (uint32_t __user
*)buffer
))
4485 binder_stat_br(proc
, thread
, BR_SPAWN_LOOPER
);
4487 binder_inner_proc_unlock(proc
);
4491 static void binder_release_work(struct binder_proc
*proc
,
4492 struct list_head
*list
)
4494 struct binder_work
*w
;
4497 w
= binder_dequeue_work_head(proc
, list
);
4502 case BINDER_WORK_TRANSACTION
: {
4503 struct binder_transaction
*t
;
4505 t
= container_of(w
, struct binder_transaction
, work
);
4507 binder_cleanup_transaction(t
, "process died.",
4510 case BINDER_WORK_RETURN_ERROR
: {
4511 struct binder_error
*e
= container_of(
4512 w
, struct binder_error
, work
);
4514 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
4515 "undelivered TRANSACTION_ERROR: %u\n",
4518 case BINDER_WORK_TRANSACTION_COMPLETE
: {
4519 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
4520 "undelivered TRANSACTION_COMPLETE\n");
4522 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE
);
4524 case BINDER_WORK_DEAD_BINDER_AND_CLEAR
:
4525 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION
: {
4526 struct binder_ref_death
*death
;
4528 death
= container_of(w
, struct binder_ref_death
, work
);
4529 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
4530 "undelivered death notification, %016llx\n",
4531 (u64
)death
->cookie
);
4533 binder_stats_deleted(BINDER_STAT_DEATH
);
4536 pr_err("unexpected work type, %d, not freed\n",
4544 static struct binder_thread
*binder_get_thread_ilocked(
4545 struct binder_proc
*proc
, struct binder_thread
*new_thread
)
4547 struct binder_thread
*thread
= NULL
;
4548 struct rb_node
*parent
= NULL
;
4549 struct rb_node
**p
= &proc
->threads
.rb_node
;
4553 thread
= rb_entry(parent
, struct binder_thread
, rb_node
);
4555 if (current
->pid
< thread
->pid
)
4557 else if (current
->pid
> thread
->pid
)
4558 p
= &(*p
)->rb_right
;
4564 thread
= new_thread
;
4565 binder_stats_created(BINDER_STAT_THREAD
);
4566 thread
->proc
= proc
;
4567 thread
->pid
= current
->pid
;
4568 get_task_struct(current
);
4569 thread
->task
= current
;
4570 atomic_set(&thread
->tmp_ref
, 0);
4571 init_waitqueue_head(&thread
->wait
);
4572 INIT_LIST_HEAD(&thread
->todo
);
4573 rb_link_node(&thread
->rb_node
, parent
, p
);
4574 rb_insert_color(&thread
->rb_node
, &proc
->threads
);
4575 thread
->looper_need_return
= true;
4576 thread
->return_error
.work
.type
= BINDER_WORK_RETURN_ERROR
;
4577 thread
->return_error
.cmd
= BR_OK
;
4578 thread
->reply_error
.work
.type
= BINDER_WORK_RETURN_ERROR
;
4579 thread
->reply_error
.cmd
= BR_OK
;
4580 INIT_LIST_HEAD(&new_thread
->waiting_thread_node
);
4584 static struct binder_thread
*binder_get_thread(struct binder_proc
*proc
)
4586 struct binder_thread
*thread
;
4587 struct binder_thread
*new_thread
;
4589 binder_inner_proc_lock(proc
);
4590 thread
= binder_get_thread_ilocked(proc
, NULL
);
4591 binder_inner_proc_unlock(proc
);
4593 new_thread
= kzalloc(sizeof(*thread
), GFP_KERNEL
);
4594 if (new_thread
== NULL
)
4596 binder_inner_proc_lock(proc
);
4597 thread
= binder_get_thread_ilocked(proc
, new_thread
);
4598 binder_inner_proc_unlock(proc
);
4599 if (thread
!= new_thread
)
4605 static void binder_free_proc(struct binder_proc
*proc
)
4607 BUG_ON(!list_empty(&proc
->todo
));
4608 BUG_ON(!list_empty(&proc
->delivered_death
));
4609 binder_alloc_deferred_release(&proc
->alloc
);
4610 put_task_struct(proc
->tsk
);
4611 binder_stats_deleted(BINDER_STAT_PROC
);
4615 static void binder_free_thread(struct binder_thread
*thread
)
4617 BUG_ON(!list_empty(&thread
->todo
));
4618 binder_stats_deleted(BINDER_STAT_THREAD
);
4619 binder_proc_dec_tmpref(thread
->proc
);
4620 put_task_struct(thread
->task
);
4624 static int binder_thread_release(struct binder_proc
*proc
,
4625 struct binder_thread
*thread
)
4627 struct binder_transaction
*t
;
4628 struct binder_transaction
*send_reply
= NULL
;
4629 int active_transactions
= 0;
4630 struct binder_transaction
*last_t
= NULL
;
4632 binder_inner_proc_lock(thread
->proc
);
4634 * take a ref on the proc so it survives
4635 * after we remove this thread from proc->threads.
4636 * The corresponding dec is when we actually
4637 * free the thread in binder_free_thread()
4641 * take a ref on this thread to ensure it
4642 * survives while we are releasing it
4644 atomic_inc(&thread
->tmp_ref
);
4645 rb_erase(&thread
->rb_node
, &proc
->threads
);
4646 t
= thread
->transaction_stack
;
4648 spin_lock(&t
->lock
);
4649 if (t
->to_thread
== thread
)
4652 thread
->is_dead
= true;
4656 active_transactions
++;
4657 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
4658 "release %d:%d(%s:%s) transaction %d %s, still active\n",
4659 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
4661 (t
->to_thread
== thread
) ? "in" : "out");
4663 if (t
->to_thread
== thread
) {
4665 t
->to_thread
= NULL
;
4667 t
->buffer
->transaction
= NULL
;
4671 } else if (t
->from
== thread
) {
4676 spin_unlock(&last_t
->lock
);
4678 spin_lock(&t
->lock
);
4682 * If this thread used poll, make sure we remove the waitqueue
4683 * from any epoll data structures holding it with POLLFREE.
4684 * waitqueue_active() is safe to use here because we're holding
4687 if ((thread
->looper
& BINDER_LOOPER_STATE_POLL
) &&
4688 waitqueue_active(&thread
->wait
)) {
4689 wake_up_poll(&thread
->wait
, POLLHUP
| POLLFREE
);
4692 binder_inner_proc_unlock(thread
->proc
);
4695 * This is needed to avoid races between wake_up_poll() above and
4696 * and ep_remove_waitqueue() called for other reasons (eg the epoll file
4697 * descriptor being closed); ep_remove_waitqueue() holds an RCU read
4698 * lock, so we can be sure it's done after calling synchronize_rcu().
4700 if (thread
->looper
& BINDER_LOOPER_STATE_POLL
)
4704 binder_send_failed_reply(send_reply
, BR_DEAD_REPLY
);
4705 binder_release_work(proc
, &thread
->todo
);
4706 binder_thread_dec_tmpref(thread
);
4707 return active_transactions
;
4710 static unsigned int binder_poll(struct file
*filp
,
4711 struct poll_table_struct
*wait
)
4713 struct binder_proc
*proc
= filp
->private_data
;
4714 struct binder_thread
*thread
= NULL
;
4715 bool wait_for_proc_work
;
4717 thread
= binder_get_thread(proc
);
4721 binder_inner_proc_lock(thread
->proc
);
4722 thread
->looper
|= BINDER_LOOPER_STATE_POLL
;
4723 wait_for_proc_work
= binder_available_for_proc_work_ilocked(thread
);
4725 binder_inner_proc_unlock(thread
->proc
);
4727 poll_wait(filp
, &thread
->wait
, wait
);
4729 if (binder_has_work(thread
, wait_for_proc_work
))
4735 static int binder_ioctl_write_read(struct file
*filp
,
4736 unsigned int cmd
, unsigned long arg
,
4737 struct binder_thread
*thread
)
4740 struct binder_proc
*proc
= filp
->private_data
;
4741 unsigned int size
= _IOC_SIZE(cmd
);
4742 void __user
*ubuf
= (void __user
*)arg
;
4743 struct binder_write_read bwr
;
4745 if (size
!= sizeof(struct binder_write_read
)) {
4749 if (copy_from_user(&bwr
, ubuf
, sizeof(bwr
))) {
4753 binder_debug(BINDER_DEBUG_READ_WRITE
,
4754 "%d:%d(%s:%s) write %lld at %016llx, read %lld at %016llx\n",
4755 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
4756 (u64
)bwr
.write_size
, (u64
)bwr
.write_buffer
,
4757 (u64
)bwr
.read_size
, (u64
)bwr
.read_buffer
);
4759 if (bwr
.write_size
> 0) {
4760 ret
= binder_thread_write(proc
, thread
,
4763 &bwr
.write_consumed
);
4764 trace_binder_write_done(ret
);
4766 bwr
.read_consumed
= 0;
4767 if (copy_to_user(ubuf
, &bwr
, sizeof(bwr
)))
4772 if (bwr
.read_size
> 0) {
4773 ret
= binder_thread_read(proc
, thread
, bwr
.read_buffer
,
4776 filp
->f_flags
& O_NONBLOCK
);
4777 trace_binder_read_done(ret
);
4778 binder_inner_proc_lock(proc
);
4779 if (!binder_worklist_empty_ilocked(&proc
->todo
))
4780 binder_wakeup_proc_ilocked(proc
);
4781 binder_inner_proc_unlock(proc
);
4783 if (copy_to_user(ubuf
, &bwr
, sizeof(bwr
)))
4788 binder_debug(BINDER_DEBUG_READ_WRITE
,
4789 "%d:%d(%s:%s) wrote %lld of %lld, read return %lld of %lld\n",
4790 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
,
4791 (u64
)bwr
.write_consumed
, (u64
)bwr
.write_size
,
4792 (u64
)bwr
.read_consumed
, (u64
)bwr
.read_size
);
4793 if (copy_to_user(ubuf
, &bwr
, sizeof(bwr
))) {
4801 static int binder_ioctl_set_ctx_mgr(struct file
*filp
)
4804 struct binder_proc
*proc
= filp
->private_data
;
4805 struct binder_context
*context
= proc
->context
;
4806 struct binder_node
*new_node
;
4807 kuid_t curr_euid
= current_euid();
4809 mutex_lock(&context
->context_mgr_node_lock
);
4810 if (context
->binder_context_mgr_node
) {
4811 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
4815 ret
= security_binder_set_context_mgr(proc
->tsk
);
4818 if (uid_valid(context
->binder_context_mgr_uid
)) {
4819 if (!uid_eq(context
->binder_context_mgr_uid
, curr_euid
)) {
4820 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
4821 from_kuid(&init_user_ns
, curr_euid
),
4822 from_kuid(&init_user_ns
,
4823 context
->binder_context_mgr_uid
));
4828 context
->binder_context_mgr_uid
= curr_euid
;
4830 new_node
= binder_new_node(proc
, NULL
);
4835 binder_node_lock(new_node
);
4836 new_node
->local_weak_refs
++;
4837 new_node
->local_strong_refs
++;
4838 new_node
->has_strong_ref
= 1;
4839 new_node
->has_weak_ref
= 1;
4840 context
->binder_context_mgr_node
= new_node
;
4841 binder_node_unlock(new_node
);
4842 binder_put_node(new_node
);
4844 mutex_unlock(&context
->context_mgr_node_lock
);
4848 static int binder_ioctl_get_node_debug_info(struct binder_proc
*proc
,
4849 struct binder_node_debug_info
*info
)
4852 binder_uintptr_t ptr
= info
->ptr
;
4854 memset(info
, 0, sizeof(*info
));
4856 binder_inner_proc_lock(proc
);
4857 for (n
= rb_first(&proc
->nodes
); n
!= NULL
; n
= rb_next(n
)) {
4858 struct binder_node
*node
= rb_entry(n
, struct binder_node
,
4860 if (node
->ptr
> ptr
) {
4861 info
->ptr
= node
->ptr
;
4862 info
->cookie
= node
->cookie
;
4863 info
->has_strong_ref
= node
->has_strong_ref
;
4864 info
->has_weak_ref
= node
->has_weak_ref
;
4868 binder_inner_proc_unlock(proc
);
4873 static long binder_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
4876 struct binder_proc
*proc
= filp
->private_data
;
4877 struct binder_thread
*thread
;
4878 unsigned int size
= _IOC_SIZE(cmd
);
4879 void __user
*ubuf
= (void __user
*)arg
;
4881 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
4882 proc->pid, current->pid, cmd, arg);*/
4884 binder_selftest_alloc(&proc
->alloc
);
4886 trace_binder_ioctl(cmd
, arg
);
4888 ret
= wait_event_interruptible(binder_user_error_wait
, binder_stop_on_user_error
< 2);
4892 thread
= binder_get_thread(proc
);
4893 if (thread
== NULL
) {
4899 case BINDER_WRITE_READ
:
4900 ret
= binder_ioctl_write_read(filp
, cmd
, arg
, thread
);
4904 case BINDER_SET_MAX_THREADS
: {
4907 if (copy_from_user(&max_threads
, ubuf
,
4908 sizeof(max_threads
))) {
4912 binder_inner_proc_lock(proc
);
4913 proc
->max_threads
= max_threads
;
4914 binder_inner_proc_unlock(proc
);
4917 case BINDER_SET_CONTEXT_MGR
:
4918 ret
= binder_ioctl_set_ctx_mgr(filp
);
4922 case BINDER_THREAD_EXIT
:
4923 binder_debug(BINDER_DEBUG_THREADS
, "%d:%d(%s:%s) exit\n",
4924 proc
->pid
, thread
->pid
, proc
->tsk
->comm
, thread
->task
->comm
);
4925 binder_thread_release(proc
, thread
);
4928 case BINDER_VERSION
: {
4929 struct binder_version __user
*ver
= ubuf
;
4931 if (size
!= sizeof(struct binder_version
)) {
4935 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION
,
4936 &ver
->protocol_version
)) {
4942 case BINDER_GET_NODE_DEBUG_INFO
: {
4943 struct binder_node_debug_info info
;
4945 if (copy_from_user(&info
, ubuf
, sizeof(info
))) {
4950 ret
= binder_ioctl_get_node_debug_info(proc
, &info
);
4954 if (copy_to_user(ubuf
, &info
, sizeof(info
))) {
4967 thread
->looper_need_return
= false;
4968 wait_event_interruptible(binder_user_error_wait
, binder_stop_on_user_error
< 2);
4969 if (ret
&& ret
!= -ERESTARTSYS
)
4970 pr_info("%d:%d(%s:%s) ioctl %x %lx returned %d\n", proc
->pid
, current
->pid
,
4971 proc
->tsk
->comm
, current
->comm
, cmd
, arg
, ret
);
4973 trace_binder_ioctl_done(ret
);
4977 static void binder_vma_open(struct vm_area_struct
*vma
)
4979 struct binder_proc
*proc
= vma
->vm_private_data
;
4981 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
4982 "%d(%s) open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
4983 proc
->pid
, proc
->tsk
->comm
, vma
->vm_start
, vma
->vm_end
,
4984 (vma
->vm_end
- vma
->vm_start
) / SZ_1K
, vma
->vm_flags
,
4985 (unsigned long)pgprot_val(vma
->vm_page_prot
));
4988 static void binder_vma_close(struct vm_area_struct
*vma
)
4990 struct binder_proc
*proc
= vma
->vm_private_data
;
4992 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
4993 "%d(%s) close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
4994 proc
->pid
, proc
->tsk
->comm
, vma
->vm_start
, vma
->vm_end
,
4995 (vma
->vm_end
- vma
->vm_start
) / SZ_1K
, vma
->vm_flags
,
4996 (unsigned long)pgprot_val(vma
->vm_page_prot
));
4997 binder_alloc_vma_close(&proc
->alloc
);
4998 binder_defer_work(proc
, BINDER_DEFERRED_PUT_FILES
);
5001 static int binder_vm_fault(struct vm_fault
*vmf
)
5003 return VM_FAULT_SIGBUS
;
5006 static const struct vm_operations_struct binder_vm_ops
= {
5007 .open
= binder_vma_open
,
5008 .close
= binder_vma_close
,
5009 .fault
= binder_vm_fault
,
5012 static int binder_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
5015 struct binder_proc
*proc
= filp
->private_data
;
5016 const char *failure_string
;
5018 if (proc
->tsk
!= current
->group_leader
)
5021 if ((vma
->vm_end
- vma
->vm_start
) > SZ_4M
)
5022 vma
->vm_end
= vma
->vm_start
+ SZ_4M
;
5024 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
5025 "%s: %d(%s) %lx-%lx (%ld K) vma %lx pagep %lx\n",
5026 __func__
, proc
->pid
, proc
->tsk
->comm
, vma
->vm_start
, vma
->vm_end
,
5027 (vma
->vm_end
- vma
->vm_start
) / SZ_1K
, vma
->vm_flags
,
5028 (unsigned long)pgprot_val(vma
->vm_page_prot
));
5030 if (vma
->vm_flags
& FORBIDDEN_MMAP_FLAGS
) {
5032 failure_string
= "bad vm_flags";
5035 vma
->vm_flags
= (vma
->vm_flags
| VM_DONTCOPY
) & ~VM_MAYWRITE
;
5036 vma
->vm_ops
= &binder_vm_ops
;
5037 vma
->vm_private_data
= proc
;
5039 ret
= binder_alloc_mmap_handler(&proc
->alloc
, vma
);
5041 failure_string
= "";
5044 mutex_lock(&proc
->files_lock
);
5045 proc
->files
= get_files_struct(current
);
5046 mutex_unlock(&proc
->files_lock
);
5050 pr_err("%s: %d(%s) %lx-%lx %s failed %d\n", __func__
,
5051 proc
->pid
, proc
->tsk
->comm
, vma
->vm_start
, vma
->vm_end
, failure_string
, ret
);
5055 static int binder_open(struct inode
*nodp
, struct file
*filp
)
5057 struct binder_proc
*proc
;
5058 struct binder_device
*binder_dev
;
5060 binder_debug(BINDER_DEBUG_OPEN_CLOSE
, "%s: %d:%d (%s:%s)\n", __func__
,
5061 current
->group_leader
->pid
, current
->pid
,
5062 current
->group_leader
->comm
, current
->comm
);
5064 proc
= kzalloc(sizeof(*proc
), GFP_KERNEL
);
5067 spin_lock_init(&proc
->inner_lock
);
5068 spin_lock_init(&proc
->outer_lock
);
5069 get_task_struct(current
->group_leader
);
5070 proc
->tsk
= current
->group_leader
;
5071 mutex_init(&proc
->files_lock
);
5072 INIT_LIST_HEAD(&proc
->todo
);
5073 if (binder_supported_policy(current
->policy
)) {
5074 proc
->default_priority
.sched_policy
= current
->policy
;
5075 proc
->default_priority
.prio
= current
->normal_prio
;
5077 proc
->default_priority
.sched_policy
= SCHED_NORMAL
;
5078 proc
->default_priority
.prio
= NICE_TO_PRIO(0);
5081 binder_dev
= container_of(filp
->private_data
, struct binder_device
,
5083 proc
->context
= &binder_dev
->context
;
5084 binder_alloc_init(&proc
->alloc
);
5086 binder_stats_created(BINDER_STAT_PROC
);
5087 proc
->pid
= current
->group_leader
->pid
;
5088 INIT_LIST_HEAD(&proc
->delivered_death
);
5089 INIT_LIST_HEAD(&proc
->waiting_threads
);
5090 filp
->private_data
= proc
;
5092 mutex_lock(&binder_procs_lock
);
5093 hlist_add_head(&proc
->proc_node
, &binder_procs
);
5094 mutex_unlock(&binder_procs_lock
);
5096 if (binder_debugfs_dir_entry_proc
) {
5099 snprintf(strbuf
, sizeof(strbuf
), "%u", proc
->pid
);
5101 * proc debug entries are shared between contexts, so
5102 * this will fail if the process tries to open the driver
5103 * again with a different context. The priting code will
5104 * anyway print all contexts that a given PID has, so this
5107 proc
->debugfs_entry
= debugfs_create_file(strbuf
, S_IRUGO
,
5108 binder_debugfs_dir_entry_proc
,
5109 (void *)(unsigned long)proc
->pid
,
5116 static int binder_flush(struct file
*filp
, fl_owner_t id
)
5118 struct binder_proc
*proc
= filp
->private_data
;
5120 binder_defer_work(proc
, BINDER_DEFERRED_FLUSH
);
5125 static void binder_deferred_flush(struct binder_proc
*proc
)
5130 binder_inner_proc_lock(proc
);
5131 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
)) {
5132 struct binder_thread
*thread
= rb_entry(n
, struct binder_thread
, rb_node
);
5134 thread
->looper_need_return
= true;
5135 if (thread
->looper
& BINDER_LOOPER_STATE_WAITING
) {
5136 wake_up_interruptible(&thread
->wait
);
5140 binder_inner_proc_unlock(proc
);
5142 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
5143 "binder_flush: %d(%s) woke %d threads\n", proc
->pid
, proc
->tsk
->comm
,
5147 static int binder_release(struct inode
*nodp
, struct file
*filp
)
5149 struct binder_proc
*proc
= filp
->private_data
;
5151 debugfs_remove(proc
->debugfs_entry
);
5152 binder_defer_work(proc
, BINDER_DEFERRED_RELEASE
);
5157 static int binder_node_release(struct binder_node
*node
, int refs
)
5159 struct binder_ref
*ref
;
5161 struct binder_proc
*proc
= node
->proc
;
5163 binder_release_work(proc
, &node
->async_todo
);
5165 binder_node_lock(node
);
5166 binder_inner_proc_lock(proc
);
5167 binder_dequeue_work_ilocked(&node
->work
);
5169 * The caller must have taken a temporary ref on the node,
5171 BUG_ON(!node
->tmp_refs
);
5172 if (hlist_empty(&node
->refs
) && node
->tmp_refs
== 1) {
5173 binder_inner_proc_unlock(proc
);
5174 binder_node_unlock(node
);
5175 binder_free_node(node
);
5181 node
->local_strong_refs
= 0;
5182 node
->local_weak_refs
= 0;
5183 binder_inner_proc_unlock(proc
);
5185 spin_lock(&binder_dead_nodes_lock
);
5186 hlist_add_head(&node
->dead_node
, &binder_dead_nodes
);
5187 spin_unlock(&binder_dead_nodes_lock
);
5189 hlist_for_each_entry(ref
, &node
->refs
, node_entry
) {
5192 * Need the node lock to synchronize
5193 * with new notification requests and the
5194 * inner lock to synchronize with queued
5195 * death notifications.
5197 binder_inner_proc_lock(ref
->proc
);
5199 binder_inner_proc_unlock(ref
->proc
);
5205 BUG_ON(!list_empty(&ref
->death
->work
.entry
));
5206 ref
->death
->work
.type
= BINDER_WORK_DEAD_BINDER
;
5207 binder_enqueue_work_ilocked(&ref
->death
->work
,
5209 binder_wakeup_proc_ilocked(ref
->proc
);
5210 binder_inner_proc_unlock(ref
->proc
);
5213 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
5214 "node %d now dead, refs %d, death %d\n",
5215 node
->debug_id
, refs
, death
);
5216 binder_node_unlock(node
);
5217 binder_put_node(node
);
5222 static void binder_deferred_release(struct binder_proc
*proc
)
5224 struct binder_context
*context
= proc
->context
;
5226 int threads
, nodes
, incoming_refs
, outgoing_refs
, active_transactions
;
5228 BUG_ON(proc
->files
);
5230 mutex_lock(&binder_procs_lock
);
5231 hlist_del(&proc
->proc_node
);
5232 mutex_unlock(&binder_procs_lock
);
5234 mutex_lock(&context
->context_mgr_node_lock
);
5235 if (context
->binder_context_mgr_node
&&
5236 context
->binder_context_mgr_node
->proc
== proc
) {
5237 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
5238 "%s: %d(%s) context_mgr_node gone\n",
5239 __func__
, proc
->pid
, proc
->tsk
->comm
);
5240 context
->binder_context_mgr_node
= NULL
;
5242 mutex_unlock(&context
->context_mgr_node_lock
);
5243 binder_inner_proc_lock(proc
);
5245 * Make sure proc stays alive after we
5246 * remove all the threads
5250 proc
->is_dead
= true;
5252 active_transactions
= 0;
5253 while ((n
= rb_first(&proc
->threads
))) {
5254 struct binder_thread
*thread
;
5256 thread
= rb_entry(n
, struct binder_thread
, rb_node
);
5257 binder_inner_proc_unlock(proc
);
5259 active_transactions
+= binder_thread_release(proc
, thread
);
5260 binder_inner_proc_lock(proc
);
5265 while ((n
= rb_first(&proc
->nodes
))) {
5266 struct binder_node
*node
;
5268 node
= rb_entry(n
, struct binder_node
, rb_node
);
5271 * take a temporary ref on the node before
5272 * calling binder_node_release() which will either
5273 * kfree() the node or call binder_put_node()
5275 binder_inc_node_tmpref_ilocked(node
);
5276 rb_erase(&node
->rb_node
, &proc
->nodes
);
5277 binder_inner_proc_unlock(proc
);
5278 incoming_refs
= binder_node_release(node
, incoming_refs
);
5279 binder_inner_proc_lock(proc
);
5281 binder_inner_proc_unlock(proc
);
5284 binder_proc_lock(proc
);
5285 while ((n
= rb_first(&proc
->refs_by_desc
))) {
5286 struct binder_ref
*ref
;
5288 ref
= rb_entry(n
, struct binder_ref
, rb_node_desc
);
5290 binder_cleanup_ref_olocked(ref
);
5291 binder_proc_unlock(proc
);
5292 binder_free_ref(ref
);
5293 binder_proc_lock(proc
);
5295 binder_proc_unlock(proc
);
5297 binder_release_work(proc
, &proc
->todo
);
5298 binder_release_work(proc
, &proc
->delivered_death
);
5300 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
5301 "%s: %d(%s) threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
5302 __func__
, proc
->pid
, proc
->tsk
->comm
, threads
, nodes
, incoming_refs
,
5303 outgoing_refs
, active_transactions
);
5305 binder_proc_dec_tmpref(proc
);
5308 static void binder_deferred_func(struct work_struct
*work
)
5310 struct binder_proc
*proc
;
5311 struct files_struct
*files
;
5316 mutex_lock(&binder_deferred_lock
);
5317 if (!hlist_empty(&binder_deferred_list
)) {
5318 proc
= hlist_entry(binder_deferred_list
.first
,
5319 struct binder_proc
, deferred_work_node
);
5320 hlist_del_init(&proc
->deferred_work_node
);
5321 defer
= proc
->deferred_work
;
5322 proc
->deferred_work
= 0;
5327 mutex_unlock(&binder_deferred_lock
);
5330 if (defer
& BINDER_DEFERRED_PUT_FILES
) {
5331 mutex_lock(&proc
->files_lock
);
5332 files
= proc
->files
;
5335 mutex_unlock(&proc
->files_lock
);
5338 if (defer
& BINDER_DEFERRED_FLUSH
)
5339 binder_deferred_flush(proc
);
5341 if (defer
& BINDER_DEFERRED_RELEASE
)
5342 binder_deferred_release(proc
); /* frees proc */
5345 put_files_struct(files
);
5348 static DECLARE_WORK(binder_deferred_work
, binder_deferred_func
);
5351 binder_defer_work(struct binder_proc
*proc
, enum binder_deferred_state defer
)
5353 mutex_lock(&binder_deferred_lock
);
5354 proc
->deferred_work
|= defer
;
5355 if (hlist_unhashed(&proc
->deferred_work_node
)) {
5356 hlist_add_head(&proc
->deferred_work_node
,
5357 &binder_deferred_list
);
5358 schedule_work(&binder_deferred_work
);
5360 mutex_unlock(&binder_deferred_lock
);
5363 static void print_binder_transaction_ilocked(struct seq_file
*m
,
5364 struct binder_proc
*proc
,
5366 struct binder_transaction
*t
)
5368 struct binder_proc
*to_proc
;
5369 struct binder_buffer
*buffer
= t
->buffer
;
5371 spin_lock(&t
->lock
);
5372 to_proc
= t
->to_proc
;
5374 "%s %d: %pK from %d:%d(%s:%s) to %d:%d(%s:%s) code %x flags %x pri %d:%d r%d",
5375 prefix
, t
->debug_id
, t
,
5376 t
->from
? t
->from
->proc
->pid
: 0,
5377 t
->from
? t
->from
->pid
: 0,
5378 t
->from
? t
->from
->proc
->tsk
->comm
: "",
5379 t
->from
? t
->from
->task
->comm
: "",
5380 to_proc
? to_proc
->pid
: 0,
5381 t
->to_thread
? t
->to_thread
->pid
: 0,
5382 to_proc
? to_proc
->tsk
->comm
: "",
5383 t
->to_thread
? t
->to_thread
->task
->comm
: "",
5384 t
->code
, t
->flags
, t
->priority
.sched_policy
,
5385 t
->priority
.prio
, t
->need_reply
);
5386 spin_unlock(&t
->lock
);
5388 if (proc
!= to_proc
) {
5390 * Can only safely deref buffer if we are holding the
5391 * correct proc inner lock for this node
5397 if (buffer
== NULL
) {
5398 seq_puts(m
, " buffer free\n");
5401 if (buffer
->target_node
)
5402 seq_printf(m
, " node %d", buffer
->target_node
->debug_id
);
5403 seq_printf(m
, " size %zd:%zd data %pK\n",
5404 buffer
->data_size
, buffer
->offsets_size
,
5408 static void print_binder_work_ilocked(struct seq_file
*m
,
5409 struct binder_proc
*proc
,
5411 const char *transaction_prefix
,
5412 struct binder_work
*w
)
5414 struct binder_node
*node
;
5415 struct binder_transaction
*t
;
5418 case BINDER_WORK_TRANSACTION
:
5419 t
= container_of(w
, struct binder_transaction
, work
);
5420 print_binder_transaction_ilocked(
5421 m
, proc
, transaction_prefix
, t
);
5423 case BINDER_WORK_RETURN_ERROR
: {
5424 struct binder_error
*e
= container_of(
5425 w
, struct binder_error
, work
);
5427 seq_printf(m
, "%stransaction error: %u\n",
5430 case BINDER_WORK_TRANSACTION_COMPLETE
:
5431 seq_printf(m
, "%stransaction complete\n", prefix
);
5433 case BINDER_WORK_NODE
:
5434 node
= container_of(w
, struct binder_node
, work
);
5435 seq_printf(m
, "%snode work %d: u%016llx c%016llx\n",
5436 prefix
, node
->debug_id
,
5437 (u64
)node
->ptr
, (u64
)node
->cookie
);
5439 case BINDER_WORK_DEAD_BINDER
:
5440 seq_printf(m
, "%shas dead binder\n", prefix
);
5442 case BINDER_WORK_DEAD_BINDER_AND_CLEAR
:
5443 seq_printf(m
, "%shas cleared dead binder\n", prefix
);
5445 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION
:
5446 seq_printf(m
, "%shas cleared death notification\n", prefix
);
5449 seq_printf(m
, "%sunknown work: type %d\n", prefix
, w
->type
);
5454 static void print_binder_thread_ilocked(struct seq_file
*m
,
5455 struct binder_thread
*thread
,
5458 struct binder_transaction
*t
;
5459 struct binder_work
*w
;
5460 size_t start_pos
= m
->count
;
5463 seq_printf(m
, " thread %d %s: l %02x need_return %d tr %d\n",
5464 thread
->pid
, thread
->task
->comm
, thread
->looper
,
5465 thread
->looper_need_return
,
5466 atomic_read(&thread
->tmp_ref
));
5467 header_pos
= m
->count
;
5468 t
= thread
->transaction_stack
;
5470 if (t
->from
== thread
) {
5471 print_binder_transaction_ilocked(m
, thread
->proc
,
5472 " outgoing transaction", t
);
5474 } else if (t
->to_thread
== thread
) {
5475 print_binder_transaction_ilocked(m
, thread
->proc
,
5476 " incoming transaction", t
);
5479 print_binder_transaction_ilocked(m
, thread
->proc
,
5480 " bad transaction", t
);
5484 list_for_each_entry(w
, &thread
->todo
, entry
) {
5485 print_binder_work_ilocked(m
, thread
->proc
, " ",
5486 " pending transaction", w
);
5488 if (!print_always
&& m
->count
== header_pos
)
5489 m
->count
= start_pos
;
5492 static void print_binder_node_nilocked(struct seq_file
*m
,
5493 struct binder_node
*node
)
5495 struct binder_ref
*ref
;
5496 struct binder_work
*w
;
5500 hlist_for_each_entry(ref
, &node
->refs
, node_entry
)
5503 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",
5504 node
->debug_id
, (u64
)node
->ptr
, (u64
)node
->cookie
,
5505 node
->sched_policy
, node
->min_priority
,
5506 node
->has_strong_ref
, node
->has_weak_ref
,
5507 node
->local_strong_refs
, node
->local_weak_refs
,
5508 node
->internal_strong_refs
, count
, node
->tmp_refs
);
5510 seq_puts(m
, " proc");
5511 hlist_for_each_entry(ref
, &node
->refs
, node_entry
)
5512 seq_printf(m
, " %d %s", ref
->proc
->pid
, ref
->proc
->tsk
->comm
);
5516 list_for_each_entry(w
, &node
->async_todo
, entry
)
5517 print_binder_work_ilocked(m
, node
->proc
, " ",
5518 " pending async transaction", w
);
5522 static void print_binder_ref_olocked(struct seq_file
*m
,
5523 struct binder_ref
*ref
)
5525 binder_node_lock(ref
->node
);
5526 seq_printf(m
, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
5527 ref
->data
.debug_id
, ref
->data
.desc
,
5528 ref
->node
->proc
? "" : "dead ",
5529 ref
->node
->debug_id
, ref
->data
.strong
,
5530 ref
->data
.weak
, ref
->death
);
5531 binder_node_unlock(ref
->node
);
5534 static void print_binder_proc(struct seq_file
*m
,
5535 struct binder_proc
*proc
, int print_all
)
5537 struct binder_work
*w
;
5539 size_t start_pos
= m
->count
;
5541 struct binder_node
*last_node
= NULL
;
5543 seq_printf(m
, "proc %d %s\n", proc
->pid
, proc
->tsk
->comm
);
5544 seq_printf(m
, "context %s\n", proc
->context
->name
);
5545 header_pos
= m
->count
;
5547 binder_inner_proc_lock(proc
);
5548 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
))
5549 print_binder_thread_ilocked(m
, rb_entry(n
, struct binder_thread
,
5550 rb_node
), print_all
);
5552 for (n
= rb_first(&proc
->nodes
); n
!= NULL
; n
= rb_next(n
)) {
5553 struct binder_node
*node
= rb_entry(n
, struct binder_node
,
5556 * take a temporary reference on the node so it
5557 * survives and isn't removed from the tree
5558 * while we print it.
5560 binder_inc_node_tmpref_ilocked(node
);
5561 /* Need to drop inner lock to take node lock */
5562 binder_inner_proc_unlock(proc
);
5564 binder_put_node(last_node
);
5565 binder_node_inner_lock(node
);
5566 print_binder_node_nilocked(m
, node
);
5567 binder_node_inner_unlock(node
);
5569 binder_inner_proc_lock(proc
);
5571 binder_inner_proc_unlock(proc
);
5573 binder_put_node(last_node
);
5576 binder_proc_lock(proc
);
5577 for (n
= rb_first(&proc
->refs_by_desc
);
5580 print_binder_ref_olocked(m
, rb_entry(n
,
5583 binder_proc_unlock(proc
);
5585 binder_alloc_print_allocated(m
, &proc
->alloc
);
5586 binder_inner_proc_lock(proc
);
5587 list_for_each_entry(w
, &proc
->todo
, entry
)
5588 print_binder_work_ilocked(m
, proc
, " ",
5589 " pending transaction", w
);
5590 list_for_each_entry(w
, &proc
->delivered_death
, entry
) {
5591 seq_puts(m
, " has delivered dead binder\n");
5594 binder_inner_proc_unlock(proc
);
5595 if (!print_all
&& m
->count
== header_pos
)
5596 m
->count
= start_pos
;
5599 static const char * const binder_return_strings
[] = {
5604 "BR_ACQUIRE_RESULT",
5606 "BR_TRANSACTION_COMPLETE",
5611 "BR_ATTEMPT_ACQUIRE",
5616 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
5620 static const char * const binder_command_strings
[] = {
5623 "BC_ACQUIRE_RESULT",
5631 "BC_ATTEMPT_ACQUIRE",
5632 "BC_REGISTER_LOOPER",
5635 "BC_REQUEST_DEATH_NOTIFICATION",
5636 "BC_CLEAR_DEATH_NOTIFICATION",
5637 "BC_DEAD_BINDER_DONE",
5638 "BC_TRANSACTION_SG",
5642 static const char * const binder_objstat_strings
[] = {
5649 "transaction_complete"
5652 static void print_binder_stats(struct seq_file
*m
, const char *prefix
,
5653 struct binder_stats
*stats
)
5657 BUILD_BUG_ON(ARRAY_SIZE(stats
->bc
) !=
5658 ARRAY_SIZE(binder_command_strings
));
5659 for (i
= 0; i
< ARRAY_SIZE(stats
->bc
); i
++) {
5660 int temp
= atomic_read(&stats
->bc
[i
]);
5663 seq_printf(m
, "%s%s: %d\n", prefix
,
5664 binder_command_strings
[i
], temp
);
5667 BUILD_BUG_ON(ARRAY_SIZE(stats
->br
) !=
5668 ARRAY_SIZE(binder_return_strings
));
5669 for (i
= 0; i
< ARRAY_SIZE(stats
->br
); i
++) {
5670 int temp
= atomic_read(&stats
->br
[i
]);
5673 seq_printf(m
, "%s%s: %d\n", prefix
,
5674 binder_return_strings
[i
], temp
);
5677 BUILD_BUG_ON(ARRAY_SIZE(stats
->obj_created
) !=
5678 ARRAY_SIZE(binder_objstat_strings
));
5679 BUILD_BUG_ON(ARRAY_SIZE(stats
->obj_created
) !=
5680 ARRAY_SIZE(stats
->obj_deleted
));
5681 for (i
= 0; i
< ARRAY_SIZE(stats
->obj_created
); i
++) {
5682 int created
= atomic_read(&stats
->obj_created
[i
]);
5683 int deleted
= atomic_read(&stats
->obj_deleted
[i
]);
5685 if (created
|| deleted
)
5686 seq_printf(m
, "%s%s: active %d total %d\n",
5688 binder_objstat_strings
[i
],
5694 static void print_binder_proc_stats(struct seq_file
*m
,
5695 struct binder_proc
*proc
)
5697 struct binder_work
*w
;
5698 struct binder_thread
*thread
;
5700 int count
, strong
, weak
, ready_threads
;
5701 size_t free_async_space
=
5702 binder_alloc_get_free_async_space(&proc
->alloc
);
5704 seq_printf(m
, "proc %d %s\n", proc
->pid
, proc
->tsk
->comm
);
5705 seq_printf(m
, "context %s\n", proc
->context
->name
);
5708 binder_inner_proc_lock(proc
);
5709 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
))
5712 list_for_each_entry(thread
, &proc
->waiting_threads
, waiting_thread_node
)
5715 seq_printf(m
, " threads: %d\n", count
);
5716 seq_printf(m
, " requested threads: %d+%d/%d\n"
5717 " ready threads %d\n"
5718 " free async space %zd\n", proc
->requested_threads
,
5719 proc
->requested_threads_started
, proc
->max_threads
,
5723 for (n
= rb_first(&proc
->nodes
); n
!= NULL
; n
= rb_next(n
))
5725 binder_inner_proc_unlock(proc
);
5726 seq_printf(m
, " nodes: %d\n", count
);
5730 binder_proc_lock(proc
);
5731 for (n
= rb_first(&proc
->refs_by_desc
); n
!= NULL
; n
= rb_next(n
)) {
5732 struct binder_ref
*ref
= rb_entry(n
, struct binder_ref
,
5735 strong
+= ref
->data
.strong
;
5736 weak
+= ref
->data
.weak
;
5738 binder_proc_unlock(proc
);
5739 seq_printf(m
, " refs: %d s %d w %d\n", count
, strong
, weak
);
5741 count
= binder_alloc_get_allocated_count(&proc
->alloc
);
5742 seq_printf(m
, " buffers: %d\n", count
);
5744 binder_alloc_print_pages(m
, &proc
->alloc
);
5747 binder_inner_proc_lock(proc
);
5748 list_for_each_entry(w
, &proc
->todo
, entry
) {
5749 if (w
->type
== BINDER_WORK_TRANSACTION
)
5752 binder_inner_proc_unlock(proc
);
5753 seq_printf(m
, " pending transactions: %d\n", count
);
5755 print_binder_stats(m
, " ", &proc
->stats
);
5759 static int binder_state_show(struct seq_file
*m
, void *unused
)
5761 struct binder_proc
*proc
;
5762 struct binder_node
*node
;
5763 struct binder_node
*last_node
= NULL
;
5765 seq_puts(m
, "binder state:\n");
5767 spin_lock(&binder_dead_nodes_lock
);
5768 if (!hlist_empty(&binder_dead_nodes
))
5769 seq_puts(m
, "dead nodes:\n");
5770 hlist_for_each_entry(node
, &binder_dead_nodes
, dead_node
) {
5772 * take a temporary reference on the node so it
5773 * survives and isn't removed from the list
5774 * while we print it.
5777 spin_unlock(&binder_dead_nodes_lock
);
5779 binder_put_node(last_node
);
5780 binder_node_lock(node
);
5781 print_binder_node_nilocked(m
, node
);
5782 binder_node_unlock(node
);
5784 spin_lock(&binder_dead_nodes_lock
);
5786 spin_unlock(&binder_dead_nodes_lock
);
5788 binder_put_node(last_node
);
5790 mutex_lock(&binder_procs_lock
);
5791 hlist_for_each_entry(proc
, &binder_procs
, proc_node
)
5792 print_binder_proc(m
, proc
, 1);
5793 mutex_unlock(&binder_procs_lock
);
5798 static int binder_stats_show(struct seq_file
*m
, void *unused
)
5800 struct binder_proc
*proc
;
5802 seq_puts(m
, "binder stats:\n");
5804 print_binder_stats(m
, "", &binder_stats
);
5806 mutex_lock(&binder_procs_lock
);
5807 hlist_for_each_entry(proc
, &binder_procs
, proc_node
)
5808 print_binder_proc_stats(m
, proc
);
5809 mutex_unlock(&binder_procs_lock
);
5814 static int binder_transactions_show(struct seq_file
*m
, void *unused
)
5816 struct binder_proc
*proc
;
5818 seq_puts(m
, "binder transactions:\n");
5819 mutex_lock(&binder_procs_lock
);
5820 hlist_for_each_entry(proc
, &binder_procs
, proc_node
)
5821 print_binder_proc(m
, proc
, 0);
5822 mutex_unlock(&binder_procs_lock
);
5827 static int binder_proc_show(struct seq_file
*m
, void *unused
)
5829 struct binder_proc
*itr
;
5830 int pid
= (unsigned long)m
->private;
5832 mutex_lock(&binder_procs_lock
);
5833 hlist_for_each_entry(itr
, &binder_procs
, proc_node
) {
5834 if (itr
->pid
== pid
) {
5835 seq_puts(m
, "binder proc state:\n");
5836 print_binder_proc(m
, itr
, 1);
5839 mutex_unlock(&binder_procs_lock
);
5844 static void print_binder_transaction_log_entry(struct seq_file
*m
,
5845 struct binder_transaction_log_entry
*e
)
5847 int debug_id
= READ_ONCE(e
->debug_id_done
);
5849 * read barrier to guarantee debug_id_done read before
5850 * we print the log values
5854 "%d: %s from %d:%d to %d:%d context %s node %d handle %d size %d:%d ret %d/%d l=%d",
5855 e
->debug_id
, (e
->call_type
== 2) ? "reply" :
5856 ((e
->call_type
== 1) ? "async" : "call "), e
->from_proc
,
5857 e
->from_thread
, e
->to_proc
, e
->to_thread
, e
->context_name
,
5858 e
->to_node
, e
->target_handle
, e
->data_size
, e
->offsets_size
,
5859 e
->return_error
, e
->return_error_param
,
5860 e
->return_error_line
);
5862 * read-barrier to guarantee read of debug_id_done after
5863 * done printing the fields of the entry
5866 seq_printf(m
, debug_id
&& debug_id
== READ_ONCE(e
->debug_id_done
) ?
5867 "\n" : " (incomplete)\n");
5870 static int binder_transaction_log_show(struct seq_file
*m
, void *unused
)
5872 struct binder_transaction_log
*log
= m
->private;
5873 unsigned int log_cur
= atomic_read(&log
->cur
);
5878 count
= log_cur
+ 1;
5879 cur
= count
< ARRAY_SIZE(log
->entry
) && !log
->full
?
5880 0 : count
% ARRAY_SIZE(log
->entry
);
5881 if (count
> ARRAY_SIZE(log
->entry
) || log
->full
)
5882 count
= ARRAY_SIZE(log
->entry
);
5883 for (i
= 0; i
< count
; i
++) {
5884 unsigned int index
= cur
++ % ARRAY_SIZE(log
->entry
);
5886 print_binder_transaction_log_entry(m
, &log
->entry
[index
]);
5891 static const struct file_operations binder_fops
= {
5892 .owner
= THIS_MODULE
,
5893 .poll
= binder_poll
,
5894 .unlocked_ioctl
= binder_ioctl
,
5895 .compat_ioctl
= binder_ioctl
,
5896 .mmap
= binder_mmap
,
5897 .open
= binder_open
,
5898 .flush
= binder_flush
,
5899 .release
= binder_release
,
5902 BINDER_DEBUG_ENTRY(state
);
5903 BINDER_DEBUG_ENTRY(stats
);
5904 BINDER_DEBUG_ENTRY(transactions
);
5905 BINDER_DEBUG_ENTRY(transaction_log
);
5907 static int __init
init_binder_device(const char *name
)
5910 struct binder_device
*binder_device
;
5912 binder_device
= kzalloc(sizeof(*binder_device
), GFP_KERNEL
);
5916 binder_device
->miscdev
.fops
= &binder_fops
;
5917 binder_device
->miscdev
.minor
= MISC_DYNAMIC_MINOR
;
5918 binder_device
->miscdev
.name
= name
;
5920 binder_device
->context
.binder_context_mgr_uid
= INVALID_UID
;
5921 binder_device
->context
.name
= name
;
5922 mutex_init(&binder_device
->context
.context_mgr_node_lock
);
5924 ret
= misc_register(&binder_device
->miscdev
);
5926 kfree(binder_device
);
5930 hlist_add_head(&binder_device
->hlist
, &binder_devices
);
5935 static int __init
binder_init(void)
5938 char *device_name
, *device_names
, *device_tmp
;
5939 struct binder_device
*device
;
5940 struct hlist_node
*tmp
;
5942 binder_alloc_shrinker_init();
5944 atomic_set(&binder_transaction_log
.cur
, ~0U);
5945 atomic_set(&binder_transaction_log_failed
.cur
, ~0U);
5947 binder_debugfs_dir_entry_root
= debugfs_create_dir("binder", NULL
);
5948 if (binder_debugfs_dir_entry_root
)
5949 binder_debugfs_dir_entry_proc
= debugfs_create_dir("proc",
5950 binder_debugfs_dir_entry_root
);
5952 if (binder_debugfs_dir_entry_root
) {
5953 debugfs_create_file("state",
5955 binder_debugfs_dir_entry_root
,
5957 &binder_state_fops
);
5958 debugfs_create_file("stats",
5960 binder_debugfs_dir_entry_root
,
5962 &binder_stats_fops
);
5963 debugfs_create_file("transactions",
5965 binder_debugfs_dir_entry_root
,
5967 &binder_transactions_fops
);
5968 debugfs_create_file("transaction_log",
5970 binder_debugfs_dir_entry_root
,
5971 &binder_transaction_log
,
5972 &binder_transaction_log_fops
);
5973 debugfs_create_file("failed_transaction_log",
5975 binder_debugfs_dir_entry_root
,
5976 &binder_transaction_log_failed
,
5977 &binder_transaction_log_fops
);
5981 * Copy the module_parameter string, because we don't want to
5982 * tokenize it in-place.
5984 device_names
= kzalloc(strlen(binder_devices_param
) + 1, GFP_KERNEL
);
5985 if (!device_names
) {
5987 goto err_alloc_device_names_failed
;
5989 strcpy(device_names
, binder_devices_param
);
5991 device_tmp
= device_names
;
5992 while ((device_name
= strsep(&device_tmp
, ","))) {
5993 ret
= init_binder_device(device_name
);
5995 goto err_init_binder_device_failed
;
6000 err_init_binder_device_failed
:
6001 hlist_for_each_entry_safe(device
, tmp
, &binder_devices
, hlist
) {
6002 misc_deregister(&device
->miscdev
);
6003 hlist_del(&device
->hlist
);
6007 kfree(device_names
);
6009 err_alloc_device_names_failed
:
6010 debugfs_remove_recursive(binder_debugfs_dir_entry_root
);
6015 device_initcall(binder_init
);
6017 #define CREATE_TRACE_POINTS
6018 #include "binder_trace.h"
6020 MODULE_LICENSE("GPL v2");