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 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 static bool binder_worklist_empty_ilocked(struct list_head
*list
)
797 return list_empty(list
);
801 * binder_worklist_empty() - Check if no items on the work list
802 * @proc: binder_proc associated with list
803 * @list: list to check
805 * Return: true if there are no items on list, else false
807 static bool binder_worklist_empty(struct binder_proc
*proc
,
808 struct list_head
*list
)
812 binder_inner_proc_lock(proc
);
813 ret
= binder_worklist_empty_ilocked(list
);
814 binder_inner_proc_unlock(proc
);
819 * binder_enqueue_work_ilocked() - Add an item to the work list
820 * @work: struct binder_work to add to list
821 * @target_list: list to add work to
823 * Adds the work to the specified list. Asserts that work
824 * is not already on a list.
826 * Requires the proc->inner_lock to be held.
829 binder_enqueue_work_ilocked(struct binder_work
*work
,
830 struct list_head
*target_list
)
832 BUG_ON(target_list
== NULL
);
833 BUG_ON(work
->entry
.next
&& !list_empty(&work
->entry
));
834 list_add_tail(&work
->entry
, target_list
);
838 * binder_enqueue_deferred_thread_work_ilocked() - Add deferred thread work
839 * @thread: thread to queue work to
840 * @work: struct binder_work to add to list
842 * Adds the work to the todo list of the thread. Doesn't set the process_todo
843 * flag, which means that (if it wasn't already set) the thread will go to
844 * sleep without handling this work when it calls read.
846 * Requires the proc->inner_lock to be held.
849 binder_enqueue_deferred_thread_work_ilocked(struct binder_thread
*thread
,
850 struct binder_work
*work
)
852 binder_enqueue_work_ilocked(work
, &thread
->todo
);
856 * binder_enqueue_thread_work_ilocked() - Add an item to the thread work list
857 * @thread: thread to queue work to
858 * @work: struct binder_work to add to list
860 * Adds the work to the todo list of the thread, and enables processing
863 * Requires the proc->inner_lock to be held.
866 binder_enqueue_thread_work_ilocked(struct binder_thread
*thread
,
867 struct binder_work
*work
)
869 binder_enqueue_work_ilocked(work
, &thread
->todo
);
870 thread
->process_todo
= true;
874 * binder_enqueue_thread_work() - Add an item to the thread work list
875 * @thread: thread to queue work to
876 * @work: struct binder_work to add to list
878 * Adds the work to the todo list of the thread, and enables processing
882 binder_enqueue_thread_work(struct binder_thread
*thread
,
883 struct binder_work
*work
)
885 binder_inner_proc_lock(thread
->proc
);
886 binder_enqueue_thread_work_ilocked(thread
, work
);
887 binder_inner_proc_unlock(thread
->proc
);
891 binder_dequeue_work_ilocked(struct binder_work
*work
)
893 list_del_init(&work
->entry
);
897 * binder_dequeue_work() - Removes an item from the work list
898 * @proc: binder_proc associated with list
899 * @work: struct binder_work to remove from list
901 * Removes the specified work item from whatever list it is on.
902 * Can safely be called if work is not on any list.
905 binder_dequeue_work(struct binder_proc
*proc
, struct binder_work
*work
)
907 binder_inner_proc_lock(proc
);
908 binder_dequeue_work_ilocked(work
);
909 binder_inner_proc_unlock(proc
);
912 static struct binder_work
*binder_dequeue_work_head_ilocked(
913 struct list_head
*list
)
915 struct binder_work
*w
;
917 w
= list_first_entry_or_null(list
, struct binder_work
, entry
);
919 list_del_init(&w
->entry
);
924 * binder_dequeue_work_head() - Dequeues the item at head of list
925 * @proc: binder_proc associated with list
926 * @list: list to dequeue head
928 * Removes the head of the list if there are items on the list
930 * Return: pointer dequeued binder_work, NULL if list was empty
932 static struct binder_work
*binder_dequeue_work_head(
933 struct binder_proc
*proc
,
934 struct list_head
*list
)
936 struct binder_work
*w
;
938 binder_inner_proc_lock(proc
);
939 w
= binder_dequeue_work_head_ilocked(list
);
940 binder_inner_proc_unlock(proc
);
945 binder_defer_work(struct binder_proc
*proc
, enum binder_deferred_state defer
);
946 static void binder_free_thread(struct binder_thread
*thread
);
947 static void binder_free_proc(struct binder_proc
*proc
);
948 static void binder_inc_node_tmpref_ilocked(struct binder_node
*node
);
950 static int task_get_unused_fd_flags(struct binder_proc
*proc
, int flags
)
952 unsigned long rlim_cur
;
956 mutex_lock(&proc
->files_lock
);
957 if (proc
->files
== NULL
) {
961 if (!lock_task_sighand(proc
->tsk
, &irqs
)) {
965 rlim_cur
= task_rlimit(proc
->tsk
, RLIMIT_NOFILE
);
966 unlock_task_sighand(proc
->tsk
, &irqs
);
968 ret
= __alloc_fd(proc
->files
, 0, rlim_cur
, flags
);
970 mutex_unlock(&proc
->files_lock
);
975 * copied from fd_install
977 static void task_fd_install(
978 struct binder_proc
*proc
, unsigned int fd
, struct file
*file
)
980 mutex_lock(&proc
->files_lock
);
982 __fd_install(proc
->files
, fd
, file
);
983 mutex_unlock(&proc
->files_lock
);
987 * copied from sys_close
989 static long task_close_fd(struct binder_proc
*proc
, unsigned int fd
)
993 mutex_lock(&proc
->files_lock
);
994 if (proc
->files
== NULL
) {
998 retval
= __close_fd(proc
->files
, fd
);
999 /* can't restart close syscall because file table entry was cleared */
1000 if (unlikely(retval
== -ERESTARTSYS
||
1001 retval
== -ERESTARTNOINTR
||
1002 retval
== -ERESTARTNOHAND
||
1003 retval
== -ERESTART_RESTARTBLOCK
))
1006 mutex_unlock(&proc
->files_lock
);
1010 static bool binder_has_work_ilocked(struct binder_thread
*thread
,
1013 return thread
->process_todo
||
1014 thread
->looper_need_return
||
1016 !binder_worklist_empty_ilocked(&thread
->proc
->todo
));
1019 static bool binder_has_work(struct binder_thread
*thread
, bool do_proc_work
)
1023 binder_inner_proc_lock(thread
->proc
);
1024 has_work
= binder_has_work_ilocked(thread
, do_proc_work
);
1025 binder_inner_proc_unlock(thread
->proc
);
1030 static bool binder_available_for_proc_work_ilocked(struct binder_thread
*thread
)
1032 return !thread
->transaction_stack
&&
1033 binder_worklist_empty_ilocked(&thread
->todo
) &&
1034 (thread
->looper
& (BINDER_LOOPER_STATE_ENTERED
|
1035 BINDER_LOOPER_STATE_REGISTERED
));
1038 static void binder_wakeup_poll_threads_ilocked(struct binder_proc
*proc
,
1042 struct binder_thread
*thread
;
1044 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
)) {
1045 thread
= rb_entry(n
, struct binder_thread
, rb_node
);
1046 if (thread
->looper
& BINDER_LOOPER_STATE_POLL
&&
1047 binder_available_for_proc_work_ilocked(thread
)) {
1049 wake_up_interruptible_sync(&thread
->wait
);
1051 wake_up_interruptible(&thread
->wait
);
1057 * binder_select_thread_ilocked() - selects a thread for doing proc work.
1058 * @proc: process to select a thread from
1060 * Note that calling this function moves the thread off the waiting_threads
1061 * list, so it can only be woken up by the caller of this function, or a
1062 * signal. Therefore, callers *should* always wake up the thread this function
1065 * Return: If there's a thread currently waiting for process work,
1066 * returns that thread. Otherwise returns NULL.
1068 static struct binder_thread
*
1069 binder_select_thread_ilocked(struct binder_proc
*proc
)
1071 struct binder_thread
*thread
;
1073 assert_spin_locked(&proc
->inner_lock
);
1074 thread
= list_first_entry_or_null(&proc
->waiting_threads
,
1075 struct binder_thread
,
1076 waiting_thread_node
);
1079 list_del_init(&thread
->waiting_thread_node
);
1085 * binder_wakeup_thread_ilocked() - wakes up a thread for doing proc work.
1086 * @proc: process to wake up a thread in
1087 * @thread: specific thread to wake-up (may be NULL)
1088 * @sync: whether to do a synchronous wake-up
1090 * This function wakes up a thread in the @proc process.
1091 * The caller may provide a specific thread to wake-up in
1092 * the @thread parameter. If @thread is NULL, this function
1093 * will wake up threads that have called poll().
1095 * Note that for this function to work as expected, callers
1096 * should first call binder_select_thread() to find a thread
1097 * to handle the work (if they don't have a thread already),
1098 * and pass the result into the @thread parameter.
1100 static void binder_wakeup_thread_ilocked(struct binder_proc
*proc
,
1101 struct binder_thread
*thread
,
1104 assert_spin_locked(&proc
->inner_lock
);
1108 wake_up_interruptible_sync(&thread
->wait
);
1110 wake_up_interruptible(&thread
->wait
);
1114 /* Didn't find a thread waiting for proc work; this can happen
1116 * 1. All threads are busy handling transactions
1117 * In that case, one of those threads should call back into
1118 * the kernel driver soon and pick up this work.
1119 * 2. Threads are using the (e)poll interface, in which case
1120 * they may be blocked on the waitqueue without having been
1121 * added to waiting_threads. For this case, we just iterate
1122 * over all threads not handling transaction work, and
1123 * wake them all up. We wake all because we don't know whether
1124 * a thread that called into (e)poll is handling non-binder
1127 binder_wakeup_poll_threads_ilocked(proc
, sync
);
1130 static void binder_wakeup_proc_ilocked(struct binder_proc
*proc
)
1132 struct binder_thread
*thread
= binder_select_thread_ilocked(proc
);
1134 binder_wakeup_thread_ilocked(proc
, thread
, /* sync = */false);
1137 static bool is_rt_policy(int policy
)
1139 return policy
== SCHED_FIFO
|| policy
== SCHED_RR
;
1142 static bool is_fair_policy(int policy
)
1144 return policy
== SCHED_NORMAL
|| policy
== SCHED_BATCH
;
1147 static bool binder_supported_policy(int policy
)
1149 return is_fair_policy(policy
) || is_rt_policy(policy
);
1152 static int to_userspace_prio(int policy
, int kernel_priority
)
1154 if (is_fair_policy(policy
))
1155 return PRIO_TO_NICE(kernel_priority
);
1157 return MAX_USER_RT_PRIO
- 1 - kernel_priority
;
1160 static int to_kernel_prio(int policy
, int user_priority
)
1162 if (is_fair_policy(policy
))
1163 return NICE_TO_PRIO(user_priority
);
1165 return MAX_USER_RT_PRIO
- 1 - user_priority
;
1168 static void binder_do_set_priority(struct task_struct
*task
,
1169 struct binder_priority desired
,
1172 int priority
; /* user-space prio value */
1174 unsigned int policy
= desired
.sched_policy
;
1176 if (task
->policy
== policy
&& task
->normal_prio
== desired
.prio
)
1179 has_cap_nice
= has_capability_noaudit(task
, CAP_SYS_NICE
);
1181 priority
= to_userspace_prio(policy
, desired
.prio
);
1183 if (verify
&& is_rt_policy(policy
) && !has_cap_nice
) {
1184 long max_rtprio
= task_rlimit(task
, RLIMIT_RTPRIO
);
1186 if (max_rtprio
== 0) {
1187 policy
= SCHED_NORMAL
;
1188 priority
= MIN_NICE
;
1189 } else if (priority
> max_rtprio
) {
1190 priority
= max_rtprio
;
1194 if (verify
&& is_fair_policy(policy
) && !has_cap_nice
) {
1195 long min_nice
= rlimit_to_nice(task_rlimit(task
, RLIMIT_NICE
));
1197 if (min_nice
> MAX_NICE
) {
1198 binder_user_error("%d RLIMIT_NICE not set\n",
1201 } else if (priority
< min_nice
) {
1202 priority
= min_nice
;
1206 if (policy
!= desired
.sched_policy
||
1207 to_kernel_prio(policy
, priority
) != desired
.prio
)
1208 binder_debug(BINDER_DEBUG_PRIORITY_CAP
,
1209 "%d: priority %d not allowed, using %d instead\n",
1210 task
->pid
, desired
.prio
,
1211 to_kernel_prio(policy
, priority
));
1213 trace_binder_set_priority(task
->tgid
, task
->pid
, task
->normal_prio
,
1214 to_kernel_prio(policy
, priority
),
1217 /* Set the actual priority */
1218 if (task
->policy
!= policy
|| is_rt_policy(policy
)) {
1219 struct sched_param params
;
1221 params
.sched_priority
= is_rt_policy(policy
) ? priority
: 0;
1223 sched_setscheduler_nocheck(task
,
1224 policy
| SCHED_RESET_ON_FORK
,
1227 if (is_fair_policy(policy
))
1228 set_user_nice(task
, priority
);
1231 static void binder_set_priority(struct task_struct
*task
,
1232 struct binder_priority desired
)
1234 binder_do_set_priority(task
, desired
, /* verify = */ true);
1237 static void binder_restore_priority(struct task_struct
*task
,
1238 struct binder_priority desired
)
1240 binder_do_set_priority(task
, desired
, /* verify = */ false);
1243 static void binder_transaction_priority(struct task_struct
*task
,
1244 struct binder_transaction
*t
,
1245 struct binder_priority node_prio
,
1248 struct binder_priority desired_prio
= t
->priority
;
1250 if (t
->set_priority_called
)
1253 t
->set_priority_called
= true;
1254 t
->saved_priority
.sched_policy
= task
->policy
;
1255 t
->saved_priority
.prio
= task
->normal_prio
;
1257 if (!inherit_rt
&& is_rt_policy(desired_prio
.sched_policy
)) {
1258 desired_prio
.prio
= NICE_TO_PRIO(0);
1259 desired_prio
.sched_policy
= SCHED_NORMAL
;
1262 if (node_prio
.prio
< t
->priority
.prio
||
1263 (node_prio
.prio
== t
->priority
.prio
&&
1264 node_prio
.sched_policy
== SCHED_FIFO
)) {
1266 * In case the minimum priority on the node is
1267 * higher (lower value), use that priority. If
1268 * the priority is the same, but the node uses
1269 * SCHED_FIFO, prefer SCHED_FIFO, since it can
1270 * run unbounded, unlike SCHED_RR.
1272 desired_prio
= node_prio
;
1275 binder_set_priority(task
, desired_prio
);
1278 static struct binder_node
*binder_get_node_ilocked(struct binder_proc
*proc
,
1279 binder_uintptr_t ptr
)
1281 struct rb_node
*n
= proc
->nodes
.rb_node
;
1282 struct binder_node
*node
;
1284 assert_spin_locked(&proc
->inner_lock
);
1287 node
= rb_entry(n
, struct binder_node
, rb_node
);
1289 if (ptr
< node
->ptr
)
1291 else if (ptr
> node
->ptr
)
1295 * take an implicit weak reference
1296 * to ensure node stays alive until
1297 * call to binder_put_node()
1299 binder_inc_node_tmpref_ilocked(node
);
1306 static struct binder_node
*binder_get_node(struct binder_proc
*proc
,
1307 binder_uintptr_t ptr
)
1309 struct binder_node
*node
;
1311 binder_inner_proc_lock(proc
);
1312 node
= binder_get_node_ilocked(proc
, ptr
);
1313 binder_inner_proc_unlock(proc
);
1317 static struct binder_node
*binder_init_node_ilocked(
1318 struct binder_proc
*proc
,
1319 struct binder_node
*new_node
,
1320 struct flat_binder_object
*fp
)
1322 struct rb_node
**p
= &proc
->nodes
.rb_node
;
1323 struct rb_node
*parent
= NULL
;
1324 struct binder_node
*node
;
1325 binder_uintptr_t ptr
= fp
? fp
->binder
: 0;
1326 binder_uintptr_t cookie
= fp
? fp
->cookie
: 0;
1327 __u32 flags
= fp
? fp
->flags
: 0;
1330 assert_spin_locked(&proc
->inner_lock
);
1335 node
= rb_entry(parent
, struct binder_node
, rb_node
);
1337 if (ptr
< node
->ptr
)
1339 else if (ptr
> node
->ptr
)
1340 p
= &(*p
)->rb_right
;
1343 * A matching node is already in
1344 * the rb tree. Abandon the init
1347 binder_inc_node_tmpref_ilocked(node
);
1352 binder_stats_created(BINDER_STAT_NODE
);
1354 rb_link_node(&node
->rb_node
, parent
, p
);
1355 rb_insert_color(&node
->rb_node
, &proc
->nodes
);
1356 node
->debug_id
= atomic_inc_return(&binder_last_id
);
1359 node
->cookie
= cookie
;
1360 node
->work
.type
= BINDER_WORK_NODE
;
1361 priority
= flags
& FLAT_BINDER_FLAG_PRIORITY_MASK
;
1362 node
->sched_policy
= (flags
& FLAT_BINDER_FLAG_SCHED_POLICY_MASK
) >>
1363 FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT
;
1364 node
->min_priority
= to_kernel_prio(node
->sched_policy
, priority
);
1365 node
->accept_fds
= !!(flags
& FLAT_BINDER_FLAG_ACCEPTS_FDS
);
1366 node
->inherit_rt
= !!(flags
& FLAT_BINDER_FLAG_INHERIT_RT
);
1367 spin_lock_init(&node
->lock
);
1368 INIT_LIST_HEAD(&node
->work
.entry
);
1369 INIT_LIST_HEAD(&node
->async_todo
);
1370 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1371 "%d:%d node %d u%016llx c%016llx created\n",
1372 proc
->pid
, current
->pid
, node
->debug_id
,
1373 (u64
)node
->ptr
, (u64
)node
->cookie
);
1378 static struct binder_node
*binder_new_node(struct binder_proc
*proc
,
1379 struct flat_binder_object
*fp
)
1381 struct binder_node
*node
;
1382 struct binder_node
*new_node
= kzalloc(sizeof(*node
), GFP_KERNEL
);
1386 binder_inner_proc_lock(proc
);
1387 node
= binder_init_node_ilocked(proc
, new_node
, fp
);
1388 binder_inner_proc_unlock(proc
);
1389 if (node
!= new_node
)
1391 * The node was already added by another thread
1398 static void binder_free_node(struct binder_node
*node
)
1401 binder_stats_deleted(BINDER_STAT_NODE
);
1404 static int binder_inc_node_nilocked(struct binder_node
*node
, int strong
,
1406 struct list_head
*target_list
)
1408 struct binder_proc
*proc
= node
->proc
;
1410 assert_spin_locked(&node
->lock
);
1412 assert_spin_locked(&proc
->inner_lock
);
1415 if (target_list
== NULL
&&
1416 node
->internal_strong_refs
== 0 &&
1418 node
== node
->proc
->context
->binder_context_mgr_node
&&
1419 node
->has_strong_ref
)) {
1420 pr_err("invalid inc strong node for %d\n",
1424 node
->internal_strong_refs
++;
1426 node
->local_strong_refs
++;
1427 if (!node
->has_strong_ref
&& target_list
) {
1428 binder_dequeue_work_ilocked(&node
->work
);
1430 * Note: this function is the only place where we queue
1431 * directly to a thread->todo without using the
1432 * corresponding binder_enqueue_thread_work() helper
1433 * functions; in this case it's ok to not set the
1434 * process_todo flag, since we know this node work will
1435 * always be followed by other work that starts queue
1436 * processing: in case of synchronous transactions, a
1437 * BR_REPLY or BR_ERROR; in case of oneway
1438 * transactions, a BR_TRANSACTION_COMPLETE.
1440 binder_enqueue_work_ilocked(&node
->work
, target_list
);
1444 node
->local_weak_refs
++;
1445 if (!node
->has_weak_ref
&& list_empty(&node
->work
.entry
)) {
1446 if (target_list
== NULL
) {
1447 pr_err("invalid inc weak node for %d\n",
1454 binder_enqueue_work_ilocked(&node
->work
, target_list
);
1460 static int binder_inc_node(struct binder_node
*node
, int strong
, int internal
,
1461 struct list_head
*target_list
)
1465 binder_node_inner_lock(node
);
1466 ret
= binder_inc_node_nilocked(node
, strong
, internal
, target_list
);
1467 binder_node_inner_unlock(node
);
1472 static bool binder_dec_node_nilocked(struct binder_node
*node
,
1473 int strong
, int internal
)
1475 struct binder_proc
*proc
= node
->proc
;
1477 assert_spin_locked(&node
->lock
);
1479 assert_spin_locked(&proc
->inner_lock
);
1482 node
->internal_strong_refs
--;
1484 node
->local_strong_refs
--;
1485 if (node
->local_strong_refs
|| node
->internal_strong_refs
)
1489 node
->local_weak_refs
--;
1490 if (node
->local_weak_refs
|| node
->tmp_refs
||
1491 !hlist_empty(&node
->refs
))
1495 if (proc
&& (node
->has_strong_ref
|| node
->has_weak_ref
)) {
1496 if (list_empty(&node
->work
.entry
)) {
1497 binder_enqueue_work_ilocked(&node
->work
, &proc
->todo
);
1498 binder_wakeup_proc_ilocked(proc
);
1501 if (hlist_empty(&node
->refs
) && !node
->local_strong_refs
&&
1502 !node
->local_weak_refs
&& !node
->tmp_refs
) {
1504 binder_dequeue_work_ilocked(&node
->work
);
1505 rb_erase(&node
->rb_node
, &proc
->nodes
);
1506 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1507 "refless node %d deleted\n",
1510 BUG_ON(!list_empty(&node
->work
.entry
));
1511 spin_lock(&binder_dead_nodes_lock
);
1513 * tmp_refs could have changed so
1516 if (node
->tmp_refs
) {
1517 spin_unlock(&binder_dead_nodes_lock
);
1520 hlist_del(&node
->dead_node
);
1521 spin_unlock(&binder_dead_nodes_lock
);
1522 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1523 "dead node %d deleted\n",
1532 static void binder_dec_node(struct binder_node
*node
, int strong
, int internal
)
1536 binder_node_inner_lock(node
);
1537 free_node
= binder_dec_node_nilocked(node
, strong
, internal
);
1538 binder_node_inner_unlock(node
);
1540 binder_free_node(node
);
1543 static void binder_inc_node_tmpref_ilocked(struct binder_node
*node
)
1546 * No call to binder_inc_node() is needed since we
1547 * don't need to inform userspace of any changes to
1554 * binder_inc_node_tmpref() - take a temporary reference on node
1555 * @node: node to reference
1557 * Take reference on node to prevent the node from being freed
1558 * while referenced only by a local variable. The inner lock is
1559 * needed to serialize with the node work on the queue (which
1560 * isn't needed after the node is dead). If the node is dead
1561 * (node->proc is NULL), use binder_dead_nodes_lock to protect
1562 * node->tmp_refs against dead-node-only cases where the node
1563 * lock cannot be acquired (eg traversing the dead node list to
1566 static void binder_inc_node_tmpref(struct binder_node
*node
)
1568 binder_node_lock(node
);
1570 binder_inner_proc_lock(node
->proc
);
1572 spin_lock(&binder_dead_nodes_lock
);
1573 binder_inc_node_tmpref_ilocked(node
);
1575 binder_inner_proc_unlock(node
->proc
);
1577 spin_unlock(&binder_dead_nodes_lock
);
1578 binder_node_unlock(node
);
1582 * binder_dec_node_tmpref() - remove a temporary reference on node
1583 * @node: node to reference
1585 * Release temporary reference on node taken via binder_inc_node_tmpref()
1587 static void binder_dec_node_tmpref(struct binder_node
*node
)
1591 binder_node_inner_lock(node
);
1593 spin_lock(&binder_dead_nodes_lock
);
1595 BUG_ON(node
->tmp_refs
< 0);
1597 spin_unlock(&binder_dead_nodes_lock
);
1599 * Call binder_dec_node() to check if all refcounts are 0
1600 * and cleanup is needed. Calling with strong=0 and internal=1
1601 * causes no actual reference to be released in binder_dec_node().
1602 * If that changes, a change is needed here too.
1604 free_node
= binder_dec_node_nilocked(node
, 0, 1);
1605 binder_node_inner_unlock(node
);
1607 binder_free_node(node
);
1610 static void binder_put_node(struct binder_node
*node
)
1612 binder_dec_node_tmpref(node
);
1615 static struct binder_ref
*binder_get_ref_olocked(struct binder_proc
*proc
,
1616 u32 desc
, bool need_strong_ref
)
1618 struct rb_node
*n
= proc
->refs_by_desc
.rb_node
;
1619 struct binder_ref
*ref
;
1622 ref
= rb_entry(n
, struct binder_ref
, rb_node_desc
);
1624 if (desc
< ref
->data
.desc
) {
1626 } else if (desc
> ref
->data
.desc
) {
1628 } else if (need_strong_ref
&& !ref
->data
.strong
) {
1629 binder_user_error("tried to use weak ref as strong ref\n");
1639 * binder_get_ref_for_node_olocked() - get the ref associated with given node
1640 * @proc: binder_proc that owns the ref
1641 * @node: binder_node of target
1642 * @new_ref: newly allocated binder_ref to be initialized or %NULL
1644 * Look up the ref for the given node and return it if it exists
1646 * If it doesn't exist and the caller provides a newly allocated
1647 * ref, initialize the fields of the newly allocated ref and insert
1648 * into the given proc rb_trees and node refs list.
1650 * Return: the ref for node. It is possible that another thread
1651 * allocated/initialized the ref first in which case the
1652 * returned ref would be different than the passed-in
1653 * new_ref. new_ref must be kfree'd by the caller in
1656 static struct binder_ref
*binder_get_ref_for_node_olocked(
1657 struct binder_proc
*proc
,
1658 struct binder_node
*node
,
1659 struct binder_ref
*new_ref
)
1661 struct binder_context
*context
= proc
->context
;
1662 struct rb_node
**p
= &proc
->refs_by_node
.rb_node
;
1663 struct rb_node
*parent
= NULL
;
1664 struct binder_ref
*ref
;
1669 ref
= rb_entry(parent
, struct binder_ref
, rb_node_node
);
1671 if (node
< ref
->node
)
1673 else if (node
> ref
->node
)
1674 p
= &(*p
)->rb_right
;
1681 binder_stats_created(BINDER_STAT_REF
);
1682 new_ref
->data
.debug_id
= atomic_inc_return(&binder_last_id
);
1683 new_ref
->proc
= proc
;
1684 new_ref
->node
= node
;
1685 rb_link_node(&new_ref
->rb_node_node
, parent
, p
);
1686 rb_insert_color(&new_ref
->rb_node_node
, &proc
->refs_by_node
);
1688 new_ref
->data
.desc
= (node
== context
->binder_context_mgr_node
) ? 0 : 1;
1689 for (n
= rb_first(&proc
->refs_by_desc
); n
!= NULL
; n
= rb_next(n
)) {
1690 ref
= rb_entry(n
, struct binder_ref
, rb_node_desc
);
1691 if (ref
->data
.desc
> new_ref
->data
.desc
)
1693 new_ref
->data
.desc
= ref
->data
.desc
+ 1;
1696 p
= &proc
->refs_by_desc
.rb_node
;
1699 ref
= rb_entry(parent
, struct binder_ref
, rb_node_desc
);
1701 if (new_ref
->data
.desc
< ref
->data
.desc
)
1703 else if (new_ref
->data
.desc
> ref
->data
.desc
)
1704 p
= &(*p
)->rb_right
;
1708 rb_link_node(&new_ref
->rb_node_desc
, parent
, p
);
1709 rb_insert_color(&new_ref
->rb_node_desc
, &proc
->refs_by_desc
);
1711 binder_node_lock(node
);
1712 hlist_add_head(&new_ref
->node_entry
, &node
->refs
);
1714 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1715 "%d new ref %d desc %d for node %d\n",
1716 proc
->pid
, new_ref
->data
.debug_id
, new_ref
->data
.desc
,
1718 binder_node_unlock(node
);
1722 static void binder_cleanup_ref_olocked(struct binder_ref
*ref
)
1724 bool delete_node
= false;
1726 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1727 "%d delete ref %d desc %d for node %d\n",
1728 ref
->proc
->pid
, ref
->data
.debug_id
, ref
->data
.desc
,
1729 ref
->node
->debug_id
);
1731 rb_erase(&ref
->rb_node_desc
, &ref
->proc
->refs_by_desc
);
1732 rb_erase(&ref
->rb_node_node
, &ref
->proc
->refs_by_node
);
1734 binder_node_inner_lock(ref
->node
);
1735 if (ref
->data
.strong
)
1736 binder_dec_node_nilocked(ref
->node
, 1, 1);
1738 hlist_del(&ref
->node_entry
);
1739 delete_node
= binder_dec_node_nilocked(ref
->node
, 0, 1);
1740 binder_node_inner_unlock(ref
->node
);
1742 * Clear ref->node unless we want the caller to free the node
1746 * The caller uses ref->node to determine
1747 * whether the node needs to be freed. Clear
1748 * it since the node is still alive.
1754 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
1755 "%d delete ref %d desc %d has death notification\n",
1756 ref
->proc
->pid
, ref
->data
.debug_id
,
1758 binder_dequeue_work(ref
->proc
, &ref
->death
->work
);
1759 binder_stats_deleted(BINDER_STAT_DEATH
);
1761 binder_stats_deleted(BINDER_STAT_REF
);
1765 * binder_inc_ref_olocked() - increment the ref for given handle
1766 * @ref: ref to be incremented
1767 * @strong: if true, strong increment, else weak
1768 * @target_list: list to queue node work on
1770 * Increment the ref. @ref->proc->outer_lock must be held on entry
1772 * Return: 0, if successful, else errno
1774 static int binder_inc_ref_olocked(struct binder_ref
*ref
, int strong
,
1775 struct list_head
*target_list
)
1780 if (ref
->data
.strong
== 0) {
1781 ret
= binder_inc_node(ref
->node
, 1, 1, target_list
);
1787 if (ref
->data
.weak
== 0) {
1788 ret
= binder_inc_node(ref
->node
, 0, 1, target_list
);
1798 * binder_dec_ref() - dec the ref for given handle
1799 * @ref: ref to be decremented
1800 * @strong: if true, strong decrement, else weak
1802 * Decrement the ref.
1804 * Return: true if ref is cleaned up and ready to be freed
1806 static bool binder_dec_ref_olocked(struct binder_ref
*ref
, int strong
)
1809 if (ref
->data
.strong
== 0) {
1810 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
1811 ref
->proc
->pid
, ref
->data
.debug_id
,
1812 ref
->data
.desc
, ref
->data
.strong
,
1817 if (ref
->data
.strong
== 0)
1818 binder_dec_node(ref
->node
, strong
, 1);
1820 if (ref
->data
.weak
== 0) {
1821 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
1822 ref
->proc
->pid
, ref
->data
.debug_id
,
1823 ref
->data
.desc
, ref
->data
.strong
,
1829 if (ref
->data
.strong
== 0 && ref
->data
.weak
== 0) {
1830 binder_cleanup_ref_olocked(ref
);
1837 * binder_get_node_from_ref() - get the node from the given proc/desc
1838 * @proc: proc containing the ref
1839 * @desc: the handle associated with the ref
1840 * @need_strong_ref: if true, only return node if ref is strong
1841 * @rdata: the id/refcount data for the ref
1843 * Given a proc and ref handle, return the associated binder_node
1845 * Return: a binder_node or NULL if not found or not strong when strong required
1847 static struct binder_node
*binder_get_node_from_ref(
1848 struct binder_proc
*proc
,
1849 u32 desc
, bool need_strong_ref
,
1850 struct binder_ref_data
*rdata
)
1852 struct binder_node
*node
;
1853 struct binder_ref
*ref
;
1855 binder_proc_lock(proc
);
1856 ref
= binder_get_ref_olocked(proc
, desc
, need_strong_ref
);
1861 * Take an implicit reference on the node to ensure
1862 * it stays alive until the call to binder_put_node()
1864 binder_inc_node_tmpref(node
);
1867 binder_proc_unlock(proc
);
1872 binder_proc_unlock(proc
);
1877 * binder_free_ref() - free the binder_ref
1880 * Free the binder_ref. Free the binder_node indicated by ref->node
1881 * (if non-NULL) and the binder_ref_death indicated by ref->death.
1883 static void binder_free_ref(struct binder_ref
*ref
)
1886 binder_free_node(ref
->node
);
1892 * binder_update_ref_for_handle() - inc/dec the ref for given handle
1893 * @proc: proc containing the ref
1894 * @desc: the handle associated with the ref
1895 * @increment: true=inc reference, false=dec reference
1896 * @strong: true=strong reference, false=weak reference
1897 * @rdata: the id/refcount data for the ref
1899 * Given a proc and ref handle, increment or decrement the ref
1900 * according to "increment" arg.
1902 * Return: 0 if successful, else errno
1904 static int binder_update_ref_for_handle(struct binder_proc
*proc
,
1905 uint32_t desc
, bool increment
, bool strong
,
1906 struct binder_ref_data
*rdata
)
1909 struct binder_ref
*ref
;
1910 bool delete_ref
= false;
1912 binder_proc_lock(proc
);
1913 ref
= binder_get_ref_olocked(proc
, desc
, strong
);
1919 ret
= binder_inc_ref_olocked(ref
, strong
, NULL
);
1921 delete_ref
= binder_dec_ref_olocked(ref
, strong
);
1925 binder_proc_unlock(proc
);
1928 binder_free_ref(ref
);
1932 binder_proc_unlock(proc
);
1937 * binder_dec_ref_for_handle() - dec the ref for given handle
1938 * @proc: proc containing the ref
1939 * @desc: the handle associated with the ref
1940 * @strong: true=strong reference, false=weak reference
1941 * @rdata: the id/refcount data for the ref
1943 * Just calls binder_update_ref_for_handle() to decrement the ref.
1945 * Return: 0 if successful, else errno
1947 static int binder_dec_ref_for_handle(struct binder_proc
*proc
,
1948 uint32_t desc
, bool strong
, struct binder_ref_data
*rdata
)
1950 return binder_update_ref_for_handle(proc
, desc
, false, strong
, rdata
);
1955 * binder_inc_ref_for_node() - increment the ref for given proc/node
1956 * @proc: proc containing the ref
1957 * @node: target node
1958 * @strong: true=strong reference, false=weak reference
1959 * @target_list: worklist to use if node is incremented
1960 * @rdata: the id/refcount data for the ref
1962 * Given a proc and node, increment the ref. Create the ref if it
1963 * doesn't already exist
1965 * Return: 0 if successful, else errno
1967 static int binder_inc_ref_for_node(struct binder_proc
*proc
,
1968 struct binder_node
*node
,
1970 struct list_head
*target_list
,
1971 struct binder_ref_data
*rdata
)
1973 struct binder_ref
*ref
;
1974 struct binder_ref
*new_ref
= NULL
;
1977 binder_proc_lock(proc
);
1978 ref
= binder_get_ref_for_node_olocked(proc
, node
, NULL
);
1980 binder_proc_unlock(proc
);
1981 new_ref
= kzalloc(sizeof(*ref
), GFP_KERNEL
);
1984 binder_proc_lock(proc
);
1985 ref
= binder_get_ref_for_node_olocked(proc
, node
, new_ref
);
1987 ret
= binder_inc_ref_olocked(ref
, strong
, target_list
);
1989 binder_proc_unlock(proc
);
1990 if (new_ref
&& ref
!= new_ref
)
1992 * Another thread created the ref first so
1993 * free the one we allocated
1999 static void binder_pop_transaction_ilocked(struct binder_thread
*target_thread
,
2000 struct binder_transaction
*t
)
2002 BUG_ON(!target_thread
);
2003 assert_spin_locked(&target_thread
->proc
->inner_lock
);
2004 BUG_ON(target_thread
->transaction_stack
!= t
);
2005 BUG_ON(target_thread
->transaction_stack
->from
!= target_thread
);
2006 target_thread
->transaction_stack
=
2007 target_thread
->transaction_stack
->from_parent
;
2012 * binder_thread_dec_tmpref() - decrement thread->tmp_ref
2013 * @thread: thread to decrement
2015 * A thread needs to be kept alive while being used to create or
2016 * handle a transaction. binder_get_txn_from() is used to safely
2017 * extract t->from from a binder_transaction and keep the thread
2018 * indicated by t->from from being freed. When done with that
2019 * binder_thread, this function is called to decrement the
2020 * tmp_ref and free if appropriate (thread has been released
2021 * and no transaction being processed by the driver)
2023 static void binder_thread_dec_tmpref(struct binder_thread
*thread
)
2026 * atomic is used to protect the counter value while
2027 * it cannot reach zero or thread->is_dead is false
2029 binder_inner_proc_lock(thread
->proc
);
2030 atomic_dec(&thread
->tmp_ref
);
2031 if (thread
->is_dead
&& !atomic_read(&thread
->tmp_ref
)) {
2032 binder_inner_proc_unlock(thread
->proc
);
2033 binder_free_thread(thread
);
2036 binder_inner_proc_unlock(thread
->proc
);
2040 * binder_proc_dec_tmpref() - decrement proc->tmp_ref
2041 * @proc: proc to decrement
2043 * A binder_proc needs to be kept alive while being used to create or
2044 * handle a transaction. proc->tmp_ref is incremented when
2045 * creating a new transaction or the binder_proc is currently in-use
2046 * by threads that are being released. When done with the binder_proc,
2047 * this function is called to decrement the counter and free the
2048 * proc if appropriate (proc has been released, all threads have
2049 * been released and not currenly in-use to process a transaction).
2051 static void binder_proc_dec_tmpref(struct binder_proc
*proc
)
2053 binder_inner_proc_lock(proc
);
2055 if (proc
->is_dead
&& RB_EMPTY_ROOT(&proc
->threads
) &&
2057 binder_inner_proc_unlock(proc
);
2058 binder_free_proc(proc
);
2061 binder_inner_proc_unlock(proc
);
2065 * binder_get_txn_from() - safely extract the "from" thread in transaction
2066 * @t: binder transaction for t->from
2068 * Atomically return the "from" thread and increment the tmp_ref
2069 * count for the thread to ensure it stays alive until
2070 * binder_thread_dec_tmpref() is called.
2072 * Return: the value of t->from
2074 static struct binder_thread
*binder_get_txn_from(
2075 struct binder_transaction
*t
)
2077 struct binder_thread
*from
;
2079 spin_lock(&t
->lock
);
2082 atomic_inc(&from
->tmp_ref
);
2083 spin_unlock(&t
->lock
);
2088 * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock
2089 * @t: binder transaction for t->from
2091 * Same as binder_get_txn_from() except it also acquires the proc->inner_lock
2092 * to guarantee that the thread cannot be released while operating on it.
2093 * The caller must call binder_inner_proc_unlock() to release the inner lock
2094 * as well as call binder_dec_thread_txn() to release the reference.
2096 * Return: the value of t->from
2098 static struct binder_thread
*binder_get_txn_from_and_acq_inner(
2099 struct binder_transaction
*t
)
2101 struct binder_thread
*from
;
2103 from
= binder_get_txn_from(t
);
2106 binder_inner_proc_lock(from
->proc
);
2108 BUG_ON(from
!= t
->from
);
2111 binder_inner_proc_unlock(from
->proc
);
2112 binder_thread_dec_tmpref(from
);
2116 static void binder_free_transaction(struct binder_transaction
*t
)
2119 t
->buffer
->transaction
= NULL
;
2121 binder_stats_deleted(BINDER_STAT_TRANSACTION
);
2124 static void binder_send_failed_reply(struct binder_transaction
*t
,
2125 uint32_t error_code
)
2127 struct binder_thread
*target_thread
;
2128 struct binder_transaction
*next
;
2130 BUG_ON(t
->flags
& TF_ONE_WAY
);
2132 target_thread
= binder_get_txn_from_and_acq_inner(t
);
2133 if (target_thread
) {
2134 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
2135 "send failed reply for transaction %d to %d:%d\n",
2137 target_thread
->proc
->pid
,
2138 target_thread
->pid
);
2140 binder_pop_transaction_ilocked(target_thread
, t
);
2141 if (target_thread
->reply_error
.cmd
== BR_OK
) {
2142 target_thread
->reply_error
.cmd
= error_code
;
2143 binder_enqueue_thread_work_ilocked(
2145 &target_thread
->reply_error
.work
);
2146 wake_up_interruptible(&target_thread
->wait
);
2148 WARN(1, "Unexpected reply error: %u\n",
2149 target_thread
->reply_error
.cmd
);
2151 binder_inner_proc_unlock(target_thread
->proc
);
2152 binder_thread_dec_tmpref(target_thread
);
2153 binder_free_transaction(t
);
2156 next
= t
->from_parent
;
2158 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
2159 "send failed reply for transaction %d, target dead\n",
2162 binder_free_transaction(t
);
2164 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
2165 "reply failed, no target thread at root\n");
2169 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
2170 "reply failed, no target thread -- retry %d\n",
2176 * binder_cleanup_transaction() - cleans up undelivered transaction
2177 * @t: transaction that needs to be cleaned up
2178 * @reason: reason the transaction wasn't delivered
2179 * @error_code: error to return to caller (if synchronous call)
2181 static void binder_cleanup_transaction(struct binder_transaction
*t
,
2183 uint32_t error_code
)
2185 if (t
->buffer
->target_node
&& !(t
->flags
& TF_ONE_WAY
)) {
2186 binder_send_failed_reply(t
, error_code
);
2188 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
2189 "undelivered transaction %d, %s\n",
2190 t
->debug_id
, reason
);
2191 binder_free_transaction(t
);
2196 * binder_validate_object() - checks for a valid metadata object in a buffer.
2197 * @buffer: binder_buffer that we're parsing.
2198 * @offset: offset in the buffer at which to validate an object.
2200 * Return: If there's a valid metadata object at @offset in @buffer, the
2201 * size of that object. Otherwise, it returns zero.
2203 static size_t binder_validate_object(struct binder_buffer
*buffer
, u64 offset
)
2205 /* Check if we can read a header first */
2206 struct binder_object_header
*hdr
;
2207 size_t object_size
= 0;
2209 if (offset
> buffer
->data_size
- sizeof(*hdr
) ||
2210 buffer
->data_size
< sizeof(*hdr
) ||
2211 !IS_ALIGNED(offset
, sizeof(u32
)))
2214 /* Ok, now see if we can read a complete object. */
2215 hdr
= (struct binder_object_header
*)(buffer
->data
+ offset
);
2216 switch (hdr
->type
) {
2217 case BINDER_TYPE_BINDER
:
2218 case BINDER_TYPE_WEAK_BINDER
:
2219 case BINDER_TYPE_HANDLE
:
2220 case BINDER_TYPE_WEAK_HANDLE
:
2221 object_size
= sizeof(struct flat_binder_object
);
2223 case BINDER_TYPE_FD
:
2224 object_size
= sizeof(struct binder_fd_object
);
2226 case BINDER_TYPE_PTR
:
2227 object_size
= sizeof(struct binder_buffer_object
);
2229 case BINDER_TYPE_FDA
:
2230 object_size
= sizeof(struct binder_fd_array_object
);
2235 if (offset
<= buffer
->data_size
- object_size
&&
2236 buffer
->data_size
>= object_size
)
2243 * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
2244 * @b: binder_buffer containing the object
2245 * @index: index in offset array at which the binder_buffer_object is
2247 * @start: points to the start of the offset array
2248 * @num_valid: the number of valid offsets in the offset array
2250 * Return: If @index is within the valid range of the offset array
2251 * described by @start and @num_valid, and if there's a valid
2252 * binder_buffer_object at the offset found in index @index
2253 * of the offset array, that object is returned. Otherwise,
2254 * %NULL is returned.
2255 * Note that the offset found in index @index itself is not
2256 * verified; this function assumes that @num_valid elements
2257 * from @start were previously verified to have valid offsets.
2259 static struct binder_buffer_object
*binder_validate_ptr(struct binder_buffer
*b
,
2260 binder_size_t index
,
2261 binder_size_t
*start
,
2262 binder_size_t num_valid
)
2264 struct binder_buffer_object
*buffer_obj
;
2265 binder_size_t
*offp
;
2267 if (index
>= num_valid
)
2270 offp
= start
+ index
;
2271 buffer_obj
= (struct binder_buffer_object
*)(b
->data
+ *offp
);
2272 if (buffer_obj
->hdr
.type
!= BINDER_TYPE_PTR
)
2279 * binder_validate_fixup() - validates pointer/fd fixups happen in order.
2280 * @b: transaction buffer
2281 * @objects_start start of objects buffer
2282 * @buffer: binder_buffer_object in which to fix up
2283 * @offset: start offset in @buffer to fix up
2284 * @last_obj: last binder_buffer_object that we fixed up in
2285 * @last_min_offset: minimum fixup offset in @last_obj
2287 * Return: %true if a fixup in buffer @buffer at offset @offset is
2290 * For safety reasons, we only allow fixups inside a buffer to happen
2291 * at increasing offsets; additionally, we only allow fixup on the last
2292 * buffer object that was verified, or one of its parents.
2294 * Example of what is allowed:
2297 * B (parent = A, offset = 0)
2298 * C (parent = A, offset = 16)
2299 * D (parent = C, offset = 0)
2300 * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
2302 * Examples of what is not allowed:
2304 * Decreasing offsets within the same parent:
2306 * C (parent = A, offset = 16)
2307 * B (parent = A, offset = 0) // decreasing offset within A
2309 * Referring to a parent that wasn't the last object or any of its parents:
2311 * B (parent = A, offset = 0)
2312 * C (parent = A, offset = 0)
2313 * C (parent = A, offset = 16)
2314 * D (parent = B, offset = 0) // B is not A or any of A's parents
2316 static bool binder_validate_fixup(struct binder_buffer
*b
,
2317 binder_size_t
*objects_start
,
2318 struct binder_buffer_object
*buffer
,
2319 binder_size_t fixup_offset
,
2320 struct binder_buffer_object
*last_obj
,
2321 binder_size_t last_min_offset
)
2324 /* Nothing to fix up in */
2328 while (last_obj
!= buffer
) {
2330 * Safe to retrieve the parent of last_obj, since it
2331 * was already previously verified by the driver.
2333 if ((last_obj
->flags
& BINDER_BUFFER_FLAG_HAS_PARENT
) == 0)
2335 last_min_offset
= last_obj
->parent_offset
+ sizeof(uintptr_t);
2336 last_obj
= (struct binder_buffer_object
*)
2337 (b
->data
+ *(objects_start
+ last_obj
->parent
));
2339 return (fixup_offset
>= last_min_offset
);
2342 static void binder_transaction_buffer_release(struct binder_proc
*proc
,
2343 struct binder_buffer
*buffer
,
2344 binder_size_t
*failed_at
)
2346 binder_size_t
*offp
, *off_start
, *off_end
;
2347 int debug_id
= buffer
->debug_id
;
2349 binder_debug(BINDER_DEBUG_TRANSACTION
,
2350 "%d buffer release %d, size %zd-%zd, failed at %p\n",
2351 proc
->pid
, buffer
->debug_id
,
2352 buffer
->data_size
, buffer
->offsets_size
, failed_at
);
2354 if (buffer
->target_node
)
2355 binder_dec_node(buffer
->target_node
, 1, 0);
2357 off_start
= (binder_size_t
*)(buffer
->data
+
2358 ALIGN(buffer
->data_size
, sizeof(void *)));
2360 off_end
= failed_at
;
2362 off_end
= (void *)off_start
+ buffer
->offsets_size
;
2363 for (offp
= off_start
; offp
< off_end
; offp
++) {
2364 struct binder_object_header
*hdr
;
2365 size_t object_size
= binder_validate_object(buffer
, *offp
);
2367 if (object_size
== 0) {
2368 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
2369 debug_id
, (u64
)*offp
, buffer
->data_size
);
2372 hdr
= (struct binder_object_header
*)(buffer
->data
+ *offp
);
2373 switch (hdr
->type
) {
2374 case BINDER_TYPE_BINDER
:
2375 case BINDER_TYPE_WEAK_BINDER
: {
2376 struct flat_binder_object
*fp
;
2377 struct binder_node
*node
;
2379 fp
= to_flat_binder_object(hdr
);
2380 node
= binder_get_node(proc
, fp
->binder
);
2382 pr_err("transaction release %d bad node %016llx\n",
2383 debug_id
, (u64
)fp
->binder
);
2386 binder_debug(BINDER_DEBUG_TRANSACTION
,
2387 " node %d u%016llx\n",
2388 node
->debug_id
, (u64
)node
->ptr
);
2389 binder_dec_node(node
, hdr
->type
== BINDER_TYPE_BINDER
,
2391 binder_put_node(node
);
2393 case BINDER_TYPE_HANDLE
:
2394 case BINDER_TYPE_WEAK_HANDLE
: {
2395 struct flat_binder_object
*fp
;
2396 struct binder_ref_data rdata
;
2399 fp
= to_flat_binder_object(hdr
);
2400 ret
= binder_dec_ref_for_handle(proc
, fp
->handle
,
2401 hdr
->type
== BINDER_TYPE_HANDLE
, &rdata
);
2404 pr_err("transaction release %d bad handle %d, ret = %d\n",
2405 debug_id
, fp
->handle
, ret
);
2408 binder_debug(BINDER_DEBUG_TRANSACTION
,
2409 " ref %d desc %d\n",
2410 rdata
.debug_id
, rdata
.desc
);
2413 case BINDER_TYPE_FD
: {
2414 struct binder_fd_object
*fp
= to_binder_fd_object(hdr
);
2416 binder_debug(BINDER_DEBUG_TRANSACTION
,
2417 " fd %d\n", fp
->fd
);
2419 task_close_fd(proc
, fp
->fd
);
2421 case BINDER_TYPE_PTR
:
2423 * Nothing to do here, this will get cleaned up when the
2424 * transaction buffer gets freed
2427 case BINDER_TYPE_FDA
: {
2428 struct binder_fd_array_object
*fda
;
2429 struct binder_buffer_object
*parent
;
2430 uintptr_t parent_buffer
;
2433 binder_size_t fd_buf_size
;
2435 fda
= to_binder_fd_array_object(hdr
);
2436 parent
= binder_validate_ptr(buffer
, fda
->parent
,
2440 pr_err("transaction release %d bad parent offset",
2445 * Since the parent was already fixed up, convert it
2446 * back to kernel address space to access it
2448 parent_buffer
= parent
->buffer
-
2449 binder_alloc_get_user_buffer_offset(
2452 fd_buf_size
= sizeof(u32
) * fda
->num_fds
;
2453 if (fda
->num_fds
>= SIZE_MAX
/ sizeof(u32
)) {
2454 pr_err("transaction release %d invalid number of fds (%lld)\n",
2455 debug_id
, (u64
)fda
->num_fds
);
2458 if (fd_buf_size
> parent
->length
||
2459 fda
->parent_offset
> parent
->length
- fd_buf_size
) {
2460 /* No space for all file descriptors here. */
2461 pr_err("transaction release %d not enough space for %lld fds in buffer\n",
2462 debug_id
, (u64
)fda
->num_fds
);
2465 fd_array
= (u32
*)(parent_buffer
+ (uintptr_t)fda
->parent_offset
);
2466 for (fd_index
= 0; fd_index
< fda
->num_fds
; fd_index
++)
2467 task_close_fd(proc
, fd_array
[fd_index
]);
2470 pr_err("transaction release %d bad object type %x\n",
2471 debug_id
, hdr
->type
);
2477 static int binder_translate_binder(struct flat_binder_object
*fp
,
2478 struct binder_transaction
*t
,
2479 struct binder_thread
*thread
)
2481 struct binder_node
*node
;
2482 struct binder_proc
*proc
= thread
->proc
;
2483 struct binder_proc
*target_proc
= t
->to_proc
;
2484 struct binder_ref_data rdata
;
2487 node
= binder_get_node(proc
, fp
->binder
);
2489 node
= binder_new_node(proc
, fp
);
2493 if (fp
->cookie
!= node
->cookie
) {
2494 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
2495 proc
->pid
, thread
->pid
, (u64
)fp
->binder
,
2496 node
->debug_id
, (u64
)fp
->cookie
,
2501 if (security_binder_transfer_binder(proc
->tsk
, target_proc
->tsk
)) {
2506 ret
= binder_inc_ref_for_node(target_proc
, node
,
2507 fp
->hdr
.type
== BINDER_TYPE_BINDER
,
2508 &thread
->todo
, &rdata
);
2512 if (fp
->hdr
.type
== BINDER_TYPE_BINDER
)
2513 fp
->hdr
.type
= BINDER_TYPE_HANDLE
;
2515 fp
->hdr
.type
= BINDER_TYPE_WEAK_HANDLE
;
2517 fp
->handle
= rdata
.desc
;
2520 trace_binder_transaction_node_to_ref(t
, node
, &rdata
);
2521 binder_debug(BINDER_DEBUG_TRANSACTION
,
2522 " node %d u%016llx -> ref %d desc %d\n",
2523 node
->debug_id
, (u64
)node
->ptr
,
2524 rdata
.debug_id
, rdata
.desc
);
2526 binder_put_node(node
);
2530 static int binder_translate_handle(struct flat_binder_object
*fp
,
2531 struct binder_transaction
*t
,
2532 struct binder_thread
*thread
)
2534 struct binder_proc
*proc
= thread
->proc
;
2535 struct binder_proc
*target_proc
= t
->to_proc
;
2536 struct binder_node
*node
;
2537 struct binder_ref_data src_rdata
;
2540 node
= binder_get_node_from_ref(proc
, fp
->handle
,
2541 fp
->hdr
.type
== BINDER_TYPE_HANDLE
, &src_rdata
);
2543 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
2544 proc
->pid
, thread
->pid
, fp
->handle
);
2547 if (security_binder_transfer_binder(proc
->tsk
, target_proc
->tsk
)) {
2552 binder_node_lock(node
);
2553 if (node
->proc
== target_proc
) {
2554 if (fp
->hdr
.type
== BINDER_TYPE_HANDLE
)
2555 fp
->hdr
.type
= BINDER_TYPE_BINDER
;
2557 fp
->hdr
.type
= BINDER_TYPE_WEAK_BINDER
;
2558 fp
->binder
= node
->ptr
;
2559 fp
->cookie
= node
->cookie
;
2561 binder_inner_proc_lock(node
->proc
);
2562 binder_inc_node_nilocked(node
,
2563 fp
->hdr
.type
== BINDER_TYPE_BINDER
,
2566 binder_inner_proc_unlock(node
->proc
);
2567 trace_binder_transaction_ref_to_node(t
, node
, &src_rdata
);
2568 binder_debug(BINDER_DEBUG_TRANSACTION
,
2569 " ref %d desc %d -> node %d u%016llx\n",
2570 src_rdata
.debug_id
, src_rdata
.desc
, node
->debug_id
,
2572 binder_node_unlock(node
);
2574 struct binder_ref_data dest_rdata
;
2576 binder_node_unlock(node
);
2577 ret
= binder_inc_ref_for_node(target_proc
, node
,
2578 fp
->hdr
.type
== BINDER_TYPE_HANDLE
,
2584 fp
->handle
= dest_rdata
.desc
;
2586 trace_binder_transaction_ref_to_ref(t
, node
, &src_rdata
,
2588 binder_debug(BINDER_DEBUG_TRANSACTION
,
2589 " ref %d desc %d -> ref %d desc %d (node %d)\n",
2590 src_rdata
.debug_id
, src_rdata
.desc
,
2591 dest_rdata
.debug_id
, dest_rdata
.desc
,
2595 binder_put_node(node
);
2599 static int binder_translate_fd(int fd
,
2600 struct binder_transaction
*t
,
2601 struct binder_thread
*thread
,
2602 struct binder_transaction
*in_reply_to
)
2604 struct binder_proc
*proc
= thread
->proc
;
2605 struct binder_proc
*target_proc
= t
->to_proc
;
2609 bool target_allows_fd
;
2612 target_allows_fd
= !!(in_reply_to
->flags
& TF_ACCEPT_FDS
);
2614 target_allows_fd
= t
->buffer
->target_node
->accept_fds
;
2615 if (!target_allows_fd
) {
2616 binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
2617 proc
->pid
, thread
->pid
,
2618 in_reply_to
? "reply" : "transaction",
2621 goto err_fd_not_accepted
;
2626 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
2627 proc
->pid
, thread
->pid
, fd
);
2631 ret
= security_binder_transfer_file(proc
->tsk
, target_proc
->tsk
, file
);
2637 target_fd
= task_get_unused_fd_flags(target_proc
, O_CLOEXEC
);
2638 if (target_fd
< 0) {
2640 goto err_get_unused_fd
;
2642 task_fd_install(target_proc
, target_fd
, file
);
2643 trace_binder_transaction_fd(t
, fd
, target_fd
);
2644 binder_debug(BINDER_DEBUG_TRANSACTION
, " fd %d -> %d\n",
2653 err_fd_not_accepted
:
2657 static int binder_translate_fd_array(struct binder_fd_array_object
*fda
,
2658 struct binder_buffer_object
*parent
,
2659 struct binder_transaction
*t
,
2660 struct binder_thread
*thread
,
2661 struct binder_transaction
*in_reply_to
)
2663 binder_size_t fdi
, fd_buf_size
, num_installed_fds
;
2665 uintptr_t parent_buffer
;
2667 struct binder_proc
*proc
= thread
->proc
;
2668 struct binder_proc
*target_proc
= t
->to_proc
;
2670 fd_buf_size
= sizeof(u32
) * fda
->num_fds
;
2671 if (fda
->num_fds
>= SIZE_MAX
/ sizeof(u32
)) {
2672 binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
2673 proc
->pid
, thread
->pid
, (u64
)fda
->num_fds
);
2676 if (fd_buf_size
> parent
->length
||
2677 fda
->parent_offset
> parent
->length
- fd_buf_size
) {
2678 /* No space for all file descriptors here. */
2679 binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
2680 proc
->pid
, thread
->pid
, (u64
)fda
->num_fds
);
2684 * Since the parent was already fixed up, convert it
2685 * back to the kernel address space to access it
2687 parent_buffer
= parent
->buffer
-
2688 binder_alloc_get_user_buffer_offset(&target_proc
->alloc
);
2689 fd_array
= (u32
*)(parent_buffer
+ (uintptr_t)fda
->parent_offset
);
2690 if (!IS_ALIGNED((unsigned long)fd_array
, sizeof(u32
))) {
2691 binder_user_error("%d:%d parent offset not aligned correctly.\n",
2692 proc
->pid
, thread
->pid
);
2695 for (fdi
= 0; fdi
< fda
->num_fds
; fdi
++) {
2696 target_fd
= binder_translate_fd(fd_array
[fdi
], t
, thread
,
2699 goto err_translate_fd_failed
;
2700 fd_array
[fdi
] = target_fd
;
2704 err_translate_fd_failed
:
2706 * Failed to allocate fd or security error, free fds
2709 num_installed_fds
= fdi
;
2710 for (fdi
= 0; fdi
< num_installed_fds
; fdi
++)
2711 task_close_fd(target_proc
, fd_array
[fdi
]);
2715 static int binder_fixup_parent(struct binder_transaction
*t
,
2716 struct binder_thread
*thread
,
2717 struct binder_buffer_object
*bp
,
2718 binder_size_t
*off_start
,
2719 binder_size_t num_valid
,
2720 struct binder_buffer_object
*last_fixup_obj
,
2721 binder_size_t last_fixup_min_off
)
2723 struct binder_buffer_object
*parent
;
2725 struct binder_buffer
*b
= t
->buffer
;
2726 struct binder_proc
*proc
= thread
->proc
;
2727 struct binder_proc
*target_proc
= t
->to_proc
;
2729 if (!(bp
->flags
& BINDER_BUFFER_FLAG_HAS_PARENT
))
2732 parent
= binder_validate_ptr(b
, bp
->parent
, off_start
, num_valid
);
2734 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
2735 proc
->pid
, thread
->pid
);
2739 if (!binder_validate_fixup(b
, off_start
,
2740 parent
, bp
->parent_offset
,
2742 last_fixup_min_off
)) {
2743 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
2744 proc
->pid
, thread
->pid
);
2748 if (parent
->length
< sizeof(binder_uintptr_t
) ||
2749 bp
->parent_offset
> parent
->length
- sizeof(binder_uintptr_t
)) {
2750 /* No space for a pointer here! */
2751 binder_user_error("%d:%d got transaction with invalid parent offset\n",
2752 proc
->pid
, thread
->pid
);
2755 parent_buffer
= (u8
*)((uintptr_t)parent
->buffer
-
2756 binder_alloc_get_user_buffer_offset(
2757 &target_proc
->alloc
));
2758 *(binder_uintptr_t
*)(parent_buffer
+ bp
->parent_offset
) = bp
->buffer
;
2764 * binder_proc_transaction() - sends a transaction to a process and wakes it up
2765 * @t: transaction to send
2766 * @proc: process to send the transaction to
2767 * @thread: thread in @proc to send the transaction to (may be NULL)
2769 * This function queues a transaction to the specified process. It will try
2770 * to find a thread in the target process to handle the transaction and
2771 * wake it up. If no thread is found, the work is queued to the proc
2774 * If the @thread parameter is not NULL, the transaction is always queued
2775 * to the waitlist of that specific thread.
2777 * Return: true if the transactions was successfully queued
2778 * false if the target process or thread is dead
2780 static bool binder_proc_transaction(struct binder_transaction
*t
,
2781 struct binder_proc
*proc
,
2782 struct binder_thread
*thread
)
2784 struct binder_node
*node
= t
->buffer
->target_node
;
2785 struct binder_priority node_prio
;
2786 bool oneway
= !!(t
->flags
& TF_ONE_WAY
);
2787 bool pending_async
= false;
2790 binder_node_lock(node
);
2791 node_prio
.prio
= node
->min_priority
;
2792 node_prio
.sched_policy
= node
->sched_policy
;
2796 if (node
->has_async_transaction
) {
2797 pending_async
= true;
2799 node
->has_async_transaction
= 1;
2803 binder_inner_proc_lock(proc
);
2805 if (proc
->is_dead
|| (thread
&& thread
->is_dead
)) {
2806 binder_inner_proc_unlock(proc
);
2807 binder_node_unlock(node
);
2811 if (!thread
&& !pending_async
)
2812 thread
= binder_select_thread_ilocked(proc
);
2815 binder_transaction_priority(thread
->task
, t
, node_prio
,
2817 binder_enqueue_thread_work_ilocked(thread
, &t
->work
);
2818 } else if (!pending_async
) {
2819 binder_enqueue_work_ilocked(&t
->work
, &proc
->todo
);
2821 binder_enqueue_work_ilocked(&t
->work
, &node
->async_todo
);
2825 binder_wakeup_thread_ilocked(proc
, thread
, !oneway
/* sync */);
2827 binder_inner_proc_unlock(proc
);
2828 binder_node_unlock(node
);
2834 * binder_get_node_refs_for_txn() - Get required refs on node for txn
2835 * @node: struct binder_node for which to get refs
2836 * @proc: returns @node->proc if valid
2837 * @error: if no @proc then returns BR_DEAD_REPLY
2839 * User-space normally keeps the node alive when creating a transaction
2840 * since it has a reference to the target. The local strong ref keeps it
2841 * alive if the sending process dies before the target process processes
2842 * the transaction. If the source process is malicious or has a reference
2843 * counting bug, relying on the local strong ref can fail.
2845 * Since user-space can cause the local strong ref to go away, we also take
2846 * a tmpref on the node to ensure it survives while we are constructing
2847 * the transaction. We also need a tmpref on the proc while we are
2848 * constructing the transaction, so we take that here as well.
2850 * Return: The target_node with refs taken or NULL if no @node->proc is NULL.
2851 * Also sets @proc if valid. If the @node->proc is NULL indicating that the
2852 * target proc has died, @error is set to BR_DEAD_REPLY
2854 static struct binder_node
*binder_get_node_refs_for_txn(
2855 struct binder_node
*node
,
2856 struct binder_proc
**procp
,
2859 struct binder_node
*target_node
= NULL
;
2861 binder_node_inner_lock(node
);
2864 binder_inc_node_nilocked(node
, 1, 0, NULL
);
2865 binder_inc_node_tmpref_ilocked(node
);
2866 node
->proc
->tmp_ref
++;
2867 *procp
= node
->proc
;
2869 *error
= BR_DEAD_REPLY
;
2870 binder_node_inner_unlock(node
);
2875 static void binder_transaction(struct binder_proc
*proc
,
2876 struct binder_thread
*thread
,
2877 struct binder_transaction_data
*tr
, int reply
,
2878 binder_size_t extra_buffers_size
)
2881 struct binder_transaction
*t
;
2882 struct binder_work
*tcomplete
;
2883 binder_size_t
*offp
, *off_end
, *off_start
;
2884 binder_size_t off_min
;
2885 u8
*sg_bufp
, *sg_buf_end
;
2886 struct binder_proc
*target_proc
= NULL
;
2887 struct binder_thread
*target_thread
= NULL
;
2888 struct binder_node
*target_node
= NULL
;
2889 struct binder_transaction
*in_reply_to
= NULL
;
2890 struct binder_transaction_log_entry
*e
;
2891 uint32_t return_error
= 0;
2892 uint32_t return_error_param
= 0;
2893 uint32_t return_error_line
= 0;
2894 struct binder_buffer_object
*last_fixup_obj
= NULL
;
2895 binder_size_t last_fixup_min_off
= 0;
2896 struct binder_context
*context
= proc
->context
;
2897 int t_debug_id
= atomic_inc_return(&binder_last_id
);
2899 e
= binder_transaction_log_add(&binder_transaction_log
);
2900 e
->debug_id
= t_debug_id
;
2901 e
->call_type
= reply
? 2 : !!(tr
->flags
& TF_ONE_WAY
);
2902 e
->from_proc
= proc
->pid
;
2903 e
->from_thread
= thread
->pid
;
2904 e
->target_handle
= tr
->target
.handle
;
2905 e
->data_size
= tr
->data_size
;
2906 e
->offsets_size
= tr
->offsets_size
;
2907 e
->context_name
= proc
->context
->name
;
2910 binder_inner_proc_lock(proc
);
2911 in_reply_to
= thread
->transaction_stack
;
2912 if (in_reply_to
== NULL
) {
2913 binder_inner_proc_unlock(proc
);
2914 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
2915 proc
->pid
, thread
->pid
);
2916 return_error
= BR_FAILED_REPLY
;
2917 return_error_param
= -EPROTO
;
2918 return_error_line
= __LINE__
;
2919 goto err_empty_call_stack
;
2921 if (in_reply_to
->to_thread
!= thread
) {
2922 spin_lock(&in_reply_to
->lock
);
2923 binder_user_error("%d:%d got reply transaction with bad transaction stack, transaction %d has target %d:%d\n",
2924 proc
->pid
, thread
->pid
, in_reply_to
->debug_id
,
2925 in_reply_to
->to_proc
?
2926 in_reply_to
->to_proc
->pid
: 0,
2927 in_reply_to
->to_thread
?
2928 in_reply_to
->to_thread
->pid
: 0);
2929 spin_unlock(&in_reply_to
->lock
);
2930 binder_inner_proc_unlock(proc
);
2931 return_error
= BR_FAILED_REPLY
;
2932 return_error_param
= -EPROTO
;
2933 return_error_line
= __LINE__
;
2935 goto err_bad_call_stack
;
2937 thread
->transaction_stack
= in_reply_to
->to_parent
;
2938 binder_inner_proc_unlock(proc
);
2939 target_thread
= binder_get_txn_from_and_acq_inner(in_reply_to
);
2940 if (target_thread
== NULL
) {
2941 return_error
= BR_DEAD_REPLY
;
2942 return_error_line
= __LINE__
;
2943 goto err_dead_binder
;
2945 if (target_thread
->transaction_stack
!= in_reply_to
) {
2946 binder_user_error("%d:%d got reply transaction with bad target transaction stack %d, expected %d\n",
2947 proc
->pid
, thread
->pid
,
2948 target_thread
->transaction_stack
?
2949 target_thread
->transaction_stack
->debug_id
: 0,
2950 in_reply_to
->debug_id
);
2951 binder_inner_proc_unlock(target_thread
->proc
);
2952 return_error
= BR_FAILED_REPLY
;
2953 return_error_param
= -EPROTO
;
2954 return_error_line
= __LINE__
;
2956 target_thread
= NULL
;
2957 goto err_dead_binder
;
2959 target_proc
= target_thread
->proc
;
2960 target_proc
->tmp_ref
++;
2961 binder_inner_proc_unlock(target_thread
->proc
);
2963 if (tr
->target
.handle
) {
2964 struct binder_ref
*ref
;
2967 * There must already be a strong ref
2968 * on this node. If so, do a strong
2969 * increment on the node to ensure it
2970 * stays alive until the transaction is
2973 binder_proc_lock(proc
);
2974 ref
= binder_get_ref_olocked(proc
, tr
->target
.handle
,
2977 target_node
= binder_get_node_refs_for_txn(
2978 ref
->node
, &target_proc
,
2981 binder_user_error("%d:%d got transaction to invalid handle\n",
2982 proc
->pid
, thread
->pid
);
2983 return_error
= BR_FAILED_REPLY
;
2985 binder_proc_unlock(proc
);
2987 mutex_lock(&context
->context_mgr_node_lock
);
2988 target_node
= context
->binder_context_mgr_node
;
2990 target_node
= binder_get_node_refs_for_txn(
2991 target_node
, &target_proc
,
2994 return_error
= BR_DEAD_REPLY
;
2995 mutex_unlock(&context
->context_mgr_node_lock
);
2999 * return_error is set above
3001 return_error_param
= -EINVAL
;
3002 return_error_line
= __LINE__
;
3003 goto err_dead_binder
;
3005 e
->to_node
= target_node
->debug_id
;
3006 if (security_binder_transaction(proc
->tsk
,
3007 target_proc
->tsk
) < 0) {
3008 return_error
= BR_FAILED_REPLY
;
3009 return_error_param
= -EPERM
;
3010 return_error_line
= __LINE__
;
3011 goto err_invalid_target_handle
;
3013 binder_inner_proc_lock(proc
);
3014 if (!(tr
->flags
& TF_ONE_WAY
) && thread
->transaction_stack
) {
3015 struct binder_transaction
*tmp
;
3017 tmp
= thread
->transaction_stack
;
3018 if (tmp
->to_thread
!= thread
) {
3019 spin_lock(&tmp
->lock
);
3020 binder_user_error("%d:%d got new transaction with bad transaction stack, transaction %d has target %d:%d\n",
3021 proc
->pid
, thread
->pid
, tmp
->debug_id
,
3022 tmp
->to_proc
? tmp
->to_proc
->pid
: 0,
3024 tmp
->to_thread
->pid
: 0);
3025 spin_unlock(&tmp
->lock
);
3026 binder_inner_proc_unlock(proc
);
3027 return_error
= BR_FAILED_REPLY
;
3028 return_error_param
= -EPROTO
;
3029 return_error_line
= __LINE__
;
3030 goto err_bad_call_stack
;
3033 struct binder_thread
*from
;
3035 spin_lock(&tmp
->lock
);
3037 if (from
&& from
->proc
== target_proc
) {
3038 atomic_inc(&from
->tmp_ref
);
3039 target_thread
= from
;
3040 spin_unlock(&tmp
->lock
);
3043 spin_unlock(&tmp
->lock
);
3044 tmp
= tmp
->from_parent
;
3047 binder_inner_proc_unlock(proc
);
3050 e
->to_thread
= target_thread
->pid
;
3051 e
->to_proc
= target_proc
->pid
;
3053 /* TODO: reuse incoming transaction for reply */
3054 t
= kzalloc(sizeof(*t
), GFP_KERNEL
);
3056 return_error
= BR_FAILED_REPLY
;
3057 return_error_param
= -ENOMEM
;
3058 return_error_line
= __LINE__
;
3059 goto err_alloc_t_failed
;
3061 binder_stats_created(BINDER_STAT_TRANSACTION
);
3062 spin_lock_init(&t
->lock
);
3064 tcomplete
= kzalloc(sizeof(*tcomplete
), GFP_KERNEL
);
3065 if (tcomplete
== NULL
) {
3066 return_error
= BR_FAILED_REPLY
;
3067 return_error_param
= -ENOMEM
;
3068 return_error_line
= __LINE__
;
3069 goto err_alloc_tcomplete_failed
;
3071 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE
);
3073 t
->debug_id
= t_debug_id
;
3076 binder_debug(BINDER_DEBUG_TRANSACTION
,
3077 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
3078 proc
->pid
, thread
->pid
, t
->debug_id
,
3079 target_proc
->pid
, target_thread
->pid
,
3080 (u64
)tr
->data
.ptr
.buffer
,
3081 (u64
)tr
->data
.ptr
.offsets
,
3082 (u64
)tr
->data_size
, (u64
)tr
->offsets_size
,
3083 (u64
)extra_buffers_size
);
3085 binder_debug(BINDER_DEBUG_TRANSACTION
,
3086 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
3087 proc
->pid
, thread
->pid
, t
->debug_id
,
3088 target_proc
->pid
, target_node
->debug_id
,
3089 (u64
)tr
->data
.ptr
.buffer
,
3090 (u64
)tr
->data
.ptr
.offsets
,
3091 (u64
)tr
->data_size
, (u64
)tr
->offsets_size
,
3092 (u64
)extra_buffers_size
);
3094 if (!reply
&& !(tr
->flags
& TF_ONE_WAY
))
3098 t
->sender_euid
= task_euid(proc
->tsk
);
3099 t
->to_proc
= target_proc
;
3100 t
->to_thread
= target_thread
;
3102 t
->flags
= tr
->flags
;
3103 if (!(t
->flags
& TF_ONE_WAY
) &&
3104 binder_supported_policy(current
->policy
)) {
3105 /* Inherit supported policies for synchronous transactions */
3106 t
->priority
.sched_policy
= current
->policy
;
3107 t
->priority
.prio
= current
->normal_prio
;
3109 /* Otherwise, fall back to the default priority */
3110 t
->priority
= target_proc
->default_priority
;
3113 trace_binder_transaction(reply
, t
, target_node
);
3115 t
->buffer
= binder_alloc_new_buf(&target_proc
->alloc
, tr
->data_size
,
3116 tr
->offsets_size
, extra_buffers_size
,
3117 !reply
&& (t
->flags
& TF_ONE_WAY
));
3118 if (IS_ERR(t
->buffer
)) {
3120 * -ESRCH indicates VMA cleared. The target is dying.
3122 return_error_param
= PTR_ERR(t
->buffer
);
3123 return_error
= return_error_param
== -ESRCH
?
3124 BR_DEAD_REPLY
: BR_FAILED_REPLY
;
3125 return_error_line
= __LINE__
;
3127 goto err_binder_alloc_buf_failed
;
3129 t
->buffer
->allow_user_free
= 0;
3130 t
->buffer
->debug_id
= t
->debug_id
;
3131 t
->buffer
->transaction
= t
;
3132 t
->buffer
->target_node
= target_node
;
3133 trace_binder_transaction_alloc_buf(t
->buffer
);
3134 off_start
= (binder_size_t
*)(t
->buffer
->data
+
3135 ALIGN(tr
->data_size
, sizeof(void *)));
3138 if (copy_from_user(t
->buffer
->data
, (const void __user
*)(uintptr_t)
3139 tr
->data
.ptr
.buffer
, tr
->data_size
)) {
3140 binder_user_error("%d:%d got transaction with invalid data ptr\n",
3141 proc
->pid
, thread
->pid
);
3142 return_error
= BR_FAILED_REPLY
;
3143 return_error_param
= -EFAULT
;
3144 return_error_line
= __LINE__
;
3145 goto err_copy_data_failed
;
3147 if (copy_from_user(offp
, (const void __user
*)(uintptr_t)
3148 tr
->data
.ptr
.offsets
, tr
->offsets_size
)) {
3149 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3150 proc
->pid
, thread
->pid
);
3151 return_error
= BR_FAILED_REPLY
;
3152 return_error_param
= -EFAULT
;
3153 return_error_line
= __LINE__
;
3154 goto err_copy_data_failed
;
3156 if (!IS_ALIGNED(tr
->offsets_size
, sizeof(binder_size_t
))) {
3157 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
3158 proc
->pid
, thread
->pid
, (u64
)tr
->offsets_size
);
3159 return_error
= BR_FAILED_REPLY
;
3160 return_error_param
= -EINVAL
;
3161 return_error_line
= __LINE__
;
3162 goto err_bad_offset
;
3164 if (!IS_ALIGNED(extra_buffers_size
, sizeof(u64
))) {
3165 binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
3166 proc
->pid
, thread
->pid
,
3167 (u64
)extra_buffers_size
);
3168 return_error
= BR_FAILED_REPLY
;
3169 return_error_param
= -EINVAL
;
3170 return_error_line
= __LINE__
;
3171 goto err_bad_offset
;
3173 off_end
= (void *)off_start
+ tr
->offsets_size
;
3174 sg_bufp
= (u8
*)(PTR_ALIGN(off_end
, sizeof(void *)));
3175 sg_buf_end
= sg_bufp
+ extra_buffers_size
;
3177 for (; offp
< off_end
; offp
++) {
3178 struct binder_object_header
*hdr
;
3179 size_t object_size
= binder_validate_object(t
->buffer
, *offp
);
3181 if (object_size
== 0 || *offp
< off_min
) {
3182 binder_user_error("%d:%d got transaction with invalid offset (%lld, min %lld max %lld) or object.\n",
3183 proc
->pid
, thread
->pid
, (u64
)*offp
,
3185 (u64
)t
->buffer
->data_size
);
3186 return_error
= BR_FAILED_REPLY
;
3187 return_error_param
= -EINVAL
;
3188 return_error_line
= __LINE__
;
3189 goto err_bad_offset
;
3192 hdr
= (struct binder_object_header
*)(t
->buffer
->data
+ *offp
);
3193 off_min
= *offp
+ object_size
;
3194 switch (hdr
->type
) {
3195 case BINDER_TYPE_BINDER
:
3196 case BINDER_TYPE_WEAK_BINDER
: {
3197 struct flat_binder_object
*fp
;
3199 fp
= to_flat_binder_object(hdr
);
3200 ret
= binder_translate_binder(fp
, t
, thread
);
3202 return_error
= BR_FAILED_REPLY
;
3203 return_error_param
= ret
;
3204 return_error_line
= __LINE__
;
3205 goto err_translate_failed
;
3208 case BINDER_TYPE_HANDLE
:
3209 case BINDER_TYPE_WEAK_HANDLE
: {
3210 struct flat_binder_object
*fp
;
3212 fp
= to_flat_binder_object(hdr
);
3213 ret
= binder_translate_handle(fp
, t
, thread
);
3215 return_error
= BR_FAILED_REPLY
;
3216 return_error_param
= ret
;
3217 return_error_line
= __LINE__
;
3218 goto err_translate_failed
;
3222 case BINDER_TYPE_FD
: {
3223 struct binder_fd_object
*fp
= to_binder_fd_object(hdr
);
3224 int target_fd
= binder_translate_fd(fp
->fd
, t
, thread
,
3227 if (target_fd
< 0) {
3228 return_error
= BR_FAILED_REPLY
;
3229 return_error_param
= target_fd
;
3230 return_error_line
= __LINE__
;
3231 goto err_translate_failed
;
3236 case BINDER_TYPE_FDA
: {
3237 struct binder_fd_array_object
*fda
=
3238 to_binder_fd_array_object(hdr
);
3239 struct binder_buffer_object
*parent
=
3240 binder_validate_ptr(t
->buffer
, fda
->parent
,
3244 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
3245 proc
->pid
, thread
->pid
);
3246 return_error
= BR_FAILED_REPLY
;
3247 return_error_param
= -EINVAL
;
3248 return_error_line
= __LINE__
;
3249 goto err_bad_parent
;
3251 if (!binder_validate_fixup(t
->buffer
, off_start
,
3252 parent
, fda
->parent_offset
,
3254 last_fixup_min_off
)) {
3255 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
3256 proc
->pid
, thread
->pid
);
3257 return_error
= BR_FAILED_REPLY
;
3258 return_error_param
= -EINVAL
;
3259 return_error_line
= __LINE__
;
3260 goto err_bad_parent
;
3262 ret
= binder_translate_fd_array(fda
, parent
, t
, thread
,
3265 return_error
= BR_FAILED_REPLY
;
3266 return_error_param
= ret
;
3267 return_error_line
= __LINE__
;
3268 goto err_translate_failed
;
3270 last_fixup_obj
= parent
;
3271 last_fixup_min_off
=
3272 fda
->parent_offset
+ sizeof(u32
) * fda
->num_fds
;
3274 case BINDER_TYPE_PTR
: {
3275 struct binder_buffer_object
*bp
=
3276 to_binder_buffer_object(hdr
);
3277 size_t buf_left
= sg_buf_end
- sg_bufp
;
3279 if (bp
->length
> buf_left
) {
3280 binder_user_error("%d:%d got transaction with too large buffer\n",
3281 proc
->pid
, thread
->pid
);
3282 return_error
= BR_FAILED_REPLY
;
3283 return_error_param
= -EINVAL
;
3284 return_error_line
= __LINE__
;
3285 goto err_bad_offset
;
3287 if (copy_from_user(sg_bufp
,
3288 (const void __user
*)(uintptr_t)
3289 bp
->buffer
, bp
->length
)) {
3290 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3291 proc
->pid
, thread
->pid
);
3292 return_error_param
= -EFAULT
;
3293 return_error
= BR_FAILED_REPLY
;
3294 return_error_line
= __LINE__
;
3295 goto err_copy_data_failed
;
3297 /* Fixup buffer pointer to target proc address space */
3298 bp
->buffer
= (uintptr_t)sg_bufp
+
3299 binder_alloc_get_user_buffer_offset(
3300 &target_proc
->alloc
);
3301 sg_bufp
+= ALIGN(bp
->length
, sizeof(u64
));
3303 ret
= binder_fixup_parent(t
, thread
, bp
, off_start
,
3306 last_fixup_min_off
);
3308 return_error
= BR_FAILED_REPLY
;
3309 return_error_param
= ret
;
3310 return_error_line
= __LINE__
;
3311 goto err_translate_failed
;
3313 last_fixup_obj
= bp
;
3314 last_fixup_min_off
= 0;
3317 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
3318 proc
->pid
, thread
->pid
, hdr
->type
);
3319 return_error
= BR_FAILED_REPLY
;
3320 return_error_param
= -EINVAL
;
3321 return_error_line
= __LINE__
;
3322 goto err_bad_object_type
;
3325 tcomplete
->type
= BINDER_WORK_TRANSACTION_COMPLETE
;
3326 t
->work
.type
= BINDER_WORK_TRANSACTION
;
3329 binder_enqueue_thread_work(thread
, tcomplete
);
3330 binder_inner_proc_lock(target_proc
);
3331 if (target_thread
->is_dead
) {
3332 binder_inner_proc_unlock(target_proc
);
3333 goto err_dead_proc_or_thread
;
3335 BUG_ON(t
->buffer
->async_transaction
!= 0);
3336 binder_pop_transaction_ilocked(target_thread
, in_reply_to
);
3337 binder_enqueue_thread_work_ilocked(target_thread
, &t
->work
);
3338 binder_inner_proc_unlock(target_proc
);
3339 wake_up_interruptible_sync(&target_thread
->wait
);
3340 binder_restore_priority(current
, in_reply_to
->saved_priority
);
3341 binder_free_transaction(in_reply_to
);
3342 } else if (!(t
->flags
& TF_ONE_WAY
)) {
3343 BUG_ON(t
->buffer
->async_transaction
!= 0);
3344 binder_inner_proc_lock(proc
);
3346 * Defer the TRANSACTION_COMPLETE, so we don't return to
3347 * userspace immediately; this allows the target process to
3348 * immediately start processing this transaction, reducing
3349 * latency. We will then return the TRANSACTION_COMPLETE when
3350 * the target replies (or there is an error).
3352 binder_enqueue_deferred_thread_work_ilocked(thread
, tcomplete
);
3354 t
->from_parent
= thread
->transaction_stack
;
3355 thread
->transaction_stack
= t
;
3356 binder_inner_proc_unlock(proc
);
3357 if (!binder_proc_transaction(t
, target_proc
, target_thread
)) {
3358 binder_inner_proc_lock(proc
);
3359 binder_pop_transaction_ilocked(thread
, t
);
3360 binder_inner_proc_unlock(proc
);
3361 goto err_dead_proc_or_thread
;
3364 BUG_ON(target_node
== NULL
);
3365 BUG_ON(t
->buffer
->async_transaction
!= 1);
3366 binder_enqueue_thread_work(thread
, tcomplete
);
3367 if (!binder_proc_transaction(t
, target_proc
, NULL
))
3368 goto err_dead_proc_or_thread
;
3371 binder_thread_dec_tmpref(target_thread
);
3372 binder_proc_dec_tmpref(target_proc
);
3374 binder_dec_node_tmpref(target_node
);
3376 * write barrier to synchronize with initialization
3380 WRITE_ONCE(e
->debug_id_done
, t_debug_id
);
3383 err_dead_proc_or_thread
:
3384 return_error
= BR_DEAD_REPLY
;
3385 return_error_line
= __LINE__
;
3386 binder_dequeue_work(proc
, tcomplete
);
3387 err_translate_failed
:
3388 err_bad_object_type
:
3391 err_copy_data_failed
:
3392 trace_binder_transaction_failed_buffer_release(t
->buffer
);
3393 binder_transaction_buffer_release(target_proc
, t
->buffer
, offp
);
3395 binder_dec_node_tmpref(target_node
);
3397 t
->buffer
->transaction
= NULL
;
3398 binder_alloc_free_buf(&target_proc
->alloc
, t
->buffer
);
3399 err_binder_alloc_buf_failed
:
3401 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE
);
3402 err_alloc_tcomplete_failed
:
3404 binder_stats_deleted(BINDER_STAT_TRANSACTION
);
3407 err_empty_call_stack
:
3409 err_invalid_target_handle
:
3411 binder_thread_dec_tmpref(target_thread
);
3413 binder_proc_dec_tmpref(target_proc
);
3415 binder_dec_node(target_node
, 1, 0);
3416 binder_dec_node_tmpref(target_node
);
3419 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
3420 "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
3421 proc
->pid
, thread
->pid
, return_error
, return_error_param
,
3422 (u64
)tr
->data_size
, (u64
)tr
->offsets_size
,
3426 struct binder_transaction_log_entry
*fe
;
3428 e
->return_error
= return_error
;
3429 e
->return_error_param
= return_error_param
;
3430 e
->return_error_line
= return_error_line
;
3431 fe
= binder_transaction_log_add(&binder_transaction_log_failed
);
3434 * write barrier to synchronize with initialization
3438 WRITE_ONCE(e
->debug_id_done
, t_debug_id
);
3439 WRITE_ONCE(fe
->debug_id_done
, t_debug_id
);
3442 BUG_ON(thread
->return_error
.cmd
!= BR_OK
);
3444 binder_restore_priority(current
, in_reply_to
->saved_priority
);
3445 thread
->return_error
.cmd
= BR_TRANSACTION_COMPLETE
;
3446 binder_enqueue_thread_work(thread
, &thread
->return_error
.work
);
3447 binder_send_failed_reply(in_reply_to
, return_error
);
3449 thread
->return_error
.cmd
= return_error
;
3450 binder_enqueue_thread_work(thread
, &thread
->return_error
.work
);
3454 static int binder_thread_write(struct binder_proc
*proc
,
3455 struct binder_thread
*thread
,
3456 binder_uintptr_t binder_buffer
, size_t size
,
3457 binder_size_t
*consumed
)
3460 struct binder_context
*context
= proc
->context
;
3461 void __user
*buffer
= (void __user
*)(uintptr_t)binder_buffer
;
3462 void __user
*ptr
= buffer
+ *consumed
;
3463 void __user
*end
= buffer
+ size
;
3465 while (ptr
< end
&& thread
->return_error
.cmd
== BR_OK
) {
3468 if (get_user(cmd
, (uint32_t __user
*)ptr
))
3470 ptr
+= sizeof(uint32_t);
3471 trace_binder_command(cmd
);
3472 if (_IOC_NR(cmd
) < ARRAY_SIZE(binder_stats
.bc
)) {
3473 atomic_inc(&binder_stats
.bc
[_IOC_NR(cmd
)]);
3474 atomic_inc(&proc
->stats
.bc
[_IOC_NR(cmd
)]);
3475 atomic_inc(&thread
->stats
.bc
[_IOC_NR(cmd
)]);
3483 const char *debug_string
;
3484 bool strong
= cmd
== BC_ACQUIRE
|| cmd
== BC_RELEASE
;
3485 bool increment
= cmd
== BC_INCREFS
|| cmd
== BC_ACQUIRE
;
3486 struct binder_ref_data rdata
;
3488 if (get_user(target
, (uint32_t __user
*)ptr
))
3491 ptr
+= sizeof(uint32_t);
3493 if (increment
&& !target
) {
3494 struct binder_node
*ctx_mgr_node
;
3495 mutex_lock(&context
->context_mgr_node_lock
);
3496 ctx_mgr_node
= context
->binder_context_mgr_node
;
3498 ret
= binder_inc_ref_for_node(
3500 strong
, NULL
, &rdata
);
3501 mutex_unlock(&context
->context_mgr_node_lock
);
3504 ret
= binder_update_ref_for_handle(
3505 proc
, target
, increment
, strong
,
3507 if (!ret
&& rdata
.desc
!= target
) {
3508 binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
3509 proc
->pid
, thread
->pid
,
3510 target
, rdata
.desc
);
3514 debug_string
= "IncRefs";
3517 debug_string
= "Acquire";
3520 debug_string
= "Release";
3524 debug_string
= "DecRefs";
3528 binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
3529 proc
->pid
, thread
->pid
, debug_string
,
3530 strong
, target
, ret
);
3533 binder_debug(BINDER_DEBUG_USER_REFS
,
3534 "%d:%d %s ref %d desc %d s %d w %d\n",
3535 proc
->pid
, thread
->pid
, debug_string
,
3536 rdata
.debug_id
, rdata
.desc
, rdata
.strong
,
3540 case BC_INCREFS_DONE
:
3541 case BC_ACQUIRE_DONE
: {
3542 binder_uintptr_t node_ptr
;
3543 binder_uintptr_t cookie
;
3544 struct binder_node
*node
;
3547 if (get_user(node_ptr
, (binder_uintptr_t __user
*)ptr
))
3549 ptr
+= sizeof(binder_uintptr_t
);
3550 if (get_user(cookie
, (binder_uintptr_t __user
*)ptr
))
3552 ptr
+= sizeof(binder_uintptr_t
);
3553 node
= binder_get_node(proc
, node_ptr
);
3555 binder_user_error("%d:%d %s u%016llx no match\n",
3556 proc
->pid
, thread
->pid
,
3557 cmd
== BC_INCREFS_DONE
?
3563 if (cookie
!= node
->cookie
) {
3564 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
3565 proc
->pid
, thread
->pid
,
3566 cmd
== BC_INCREFS_DONE
?
3567 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
3568 (u64
)node_ptr
, node
->debug_id
,
3569 (u64
)cookie
, (u64
)node
->cookie
);
3570 binder_put_node(node
);
3573 binder_node_inner_lock(node
);
3574 if (cmd
== BC_ACQUIRE_DONE
) {
3575 if (node
->pending_strong_ref
== 0) {
3576 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
3577 proc
->pid
, thread
->pid
,
3579 binder_node_inner_unlock(node
);
3580 binder_put_node(node
);
3583 node
->pending_strong_ref
= 0;
3585 if (node
->pending_weak_ref
== 0) {
3586 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
3587 proc
->pid
, thread
->pid
,
3589 binder_node_inner_unlock(node
);
3590 binder_put_node(node
);
3593 node
->pending_weak_ref
= 0;
3595 free_node
= binder_dec_node_nilocked(node
,
3596 cmd
== BC_ACQUIRE_DONE
, 0);
3598 binder_debug(BINDER_DEBUG_USER_REFS
,
3599 "%d:%d %s node %d ls %d lw %d tr %d\n",
3600 proc
->pid
, thread
->pid
,
3601 cmd
== BC_INCREFS_DONE
? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
3602 node
->debug_id
, node
->local_strong_refs
,
3603 node
->local_weak_refs
, node
->tmp_refs
);
3604 binder_node_inner_unlock(node
);
3605 binder_put_node(node
);
3608 case BC_ATTEMPT_ACQUIRE
:
3609 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
3611 case BC_ACQUIRE_RESULT
:
3612 pr_err("BC_ACQUIRE_RESULT not supported\n");
3615 case BC_FREE_BUFFER
: {
3616 binder_uintptr_t data_ptr
;
3617 struct binder_buffer
*buffer
;
3619 if (get_user(data_ptr
, (binder_uintptr_t __user
*)ptr
))
3621 ptr
+= sizeof(binder_uintptr_t
);
3623 buffer
= binder_alloc_prepare_to_free(&proc
->alloc
,
3625 if (buffer
== NULL
) {
3626 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
3627 proc
->pid
, thread
->pid
, (u64
)data_ptr
);
3630 if (!buffer
->allow_user_free
) {
3631 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
3632 proc
->pid
, thread
->pid
, (u64
)data_ptr
);
3635 binder_debug(BINDER_DEBUG_FREE_BUFFER
,
3636 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
3637 proc
->pid
, thread
->pid
, (u64
)data_ptr
,
3639 buffer
->transaction
? "active" : "finished");
3641 if (buffer
->transaction
) {
3642 buffer
->transaction
->buffer
= NULL
;
3643 buffer
->transaction
= NULL
;
3645 if (buffer
->async_transaction
&& buffer
->target_node
) {
3646 struct binder_node
*buf_node
;
3647 struct binder_work
*w
;
3649 buf_node
= buffer
->target_node
;
3650 binder_node_inner_lock(buf_node
);
3651 BUG_ON(!buf_node
->has_async_transaction
);
3652 BUG_ON(buf_node
->proc
!= proc
);
3653 w
= binder_dequeue_work_head_ilocked(
3654 &buf_node
->async_todo
);
3656 buf_node
->has_async_transaction
= 0;
3658 binder_enqueue_work_ilocked(
3660 binder_wakeup_proc_ilocked(proc
);
3662 binder_node_inner_unlock(buf_node
);
3664 trace_binder_transaction_buffer_release(buffer
);
3665 binder_transaction_buffer_release(proc
, buffer
, NULL
);
3666 binder_alloc_free_buf(&proc
->alloc
, buffer
);
3670 case BC_TRANSACTION_SG
:
3672 struct binder_transaction_data_sg tr
;
3674 if (copy_from_user(&tr
, ptr
, sizeof(tr
)))
3677 binder_transaction(proc
, thread
, &tr
.transaction_data
,
3678 cmd
== BC_REPLY_SG
, tr
.buffers_size
);
3681 case BC_TRANSACTION
:
3683 struct binder_transaction_data tr
;
3685 if (copy_from_user(&tr
, ptr
, sizeof(tr
)))
3688 binder_transaction(proc
, thread
, &tr
,
3689 cmd
== BC_REPLY
, 0);
3693 case BC_REGISTER_LOOPER
:
3694 binder_debug(BINDER_DEBUG_THREADS
,
3695 "%d:%d BC_REGISTER_LOOPER\n",
3696 proc
->pid
, thread
->pid
);
3697 binder_inner_proc_lock(proc
);
3698 if (thread
->looper
& BINDER_LOOPER_STATE_ENTERED
) {
3699 thread
->looper
|= BINDER_LOOPER_STATE_INVALID
;
3700 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
3701 proc
->pid
, thread
->pid
);
3702 } else if (proc
->requested_threads
== 0) {
3703 thread
->looper
|= BINDER_LOOPER_STATE_INVALID
;
3704 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
3705 proc
->pid
, thread
->pid
);
3707 proc
->requested_threads
--;
3708 proc
->requested_threads_started
++;
3710 thread
->looper
|= BINDER_LOOPER_STATE_REGISTERED
;
3711 binder_inner_proc_unlock(proc
);
3713 case BC_ENTER_LOOPER
:
3714 binder_debug(BINDER_DEBUG_THREADS
,
3715 "%d:%d BC_ENTER_LOOPER\n",
3716 proc
->pid
, thread
->pid
);
3717 if (thread
->looper
& BINDER_LOOPER_STATE_REGISTERED
) {
3718 thread
->looper
|= BINDER_LOOPER_STATE_INVALID
;
3719 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
3720 proc
->pid
, thread
->pid
);
3722 thread
->looper
|= BINDER_LOOPER_STATE_ENTERED
;
3724 case BC_EXIT_LOOPER
:
3725 binder_debug(BINDER_DEBUG_THREADS
,
3726 "%d:%d BC_EXIT_LOOPER\n",
3727 proc
->pid
, thread
->pid
);
3728 thread
->looper
|= BINDER_LOOPER_STATE_EXITED
;
3731 case BC_REQUEST_DEATH_NOTIFICATION
:
3732 case BC_CLEAR_DEATH_NOTIFICATION
: {
3734 binder_uintptr_t cookie
;
3735 struct binder_ref
*ref
;
3736 struct binder_ref_death
*death
= NULL
;
3738 if (get_user(target
, (uint32_t __user
*)ptr
))
3740 ptr
+= sizeof(uint32_t);
3741 if (get_user(cookie
, (binder_uintptr_t __user
*)ptr
))
3743 ptr
+= sizeof(binder_uintptr_t
);
3744 if (cmd
== BC_REQUEST_DEATH_NOTIFICATION
) {
3746 * Allocate memory for death notification
3747 * before taking lock
3749 death
= kzalloc(sizeof(*death
), GFP_KERNEL
);
3750 if (death
== NULL
) {
3751 WARN_ON(thread
->return_error
.cmd
!=
3753 thread
->return_error
.cmd
= BR_ERROR
;
3754 binder_enqueue_thread_work(
3756 &thread
->return_error
.work
);
3758 BINDER_DEBUG_FAILED_TRANSACTION
,
3759 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
3760 proc
->pid
, thread
->pid
);
3764 binder_proc_lock(proc
);
3765 ref
= binder_get_ref_olocked(proc
, target
, false);
3767 binder_user_error("%d:%d %s invalid ref %d\n",
3768 proc
->pid
, thread
->pid
,
3769 cmd
== BC_REQUEST_DEATH_NOTIFICATION
?
3770 "BC_REQUEST_DEATH_NOTIFICATION" :
3771 "BC_CLEAR_DEATH_NOTIFICATION",
3773 binder_proc_unlock(proc
);
3778 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION
,
3779 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
3780 proc
->pid
, thread
->pid
,
3781 cmd
== BC_REQUEST_DEATH_NOTIFICATION
?
3782 "BC_REQUEST_DEATH_NOTIFICATION" :
3783 "BC_CLEAR_DEATH_NOTIFICATION",
3784 (u64
)cookie
, ref
->data
.debug_id
,
3785 ref
->data
.desc
, ref
->data
.strong
,
3786 ref
->data
.weak
, ref
->node
->debug_id
);
3788 binder_node_lock(ref
->node
);
3789 if (cmd
== BC_REQUEST_DEATH_NOTIFICATION
) {
3791 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
3792 proc
->pid
, thread
->pid
);
3793 binder_node_unlock(ref
->node
);
3794 binder_proc_unlock(proc
);
3798 binder_stats_created(BINDER_STAT_DEATH
);
3799 INIT_LIST_HEAD(&death
->work
.entry
);
3800 death
->cookie
= cookie
;
3802 if (ref
->node
->proc
== NULL
) {
3803 ref
->death
->work
.type
= BINDER_WORK_DEAD_BINDER
;
3805 binder_inner_proc_lock(proc
);
3806 binder_enqueue_work_ilocked(
3807 &ref
->death
->work
, &proc
->todo
);
3808 binder_wakeup_proc_ilocked(proc
);
3809 binder_inner_proc_unlock(proc
);
3812 if (ref
->death
== NULL
) {
3813 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
3814 proc
->pid
, thread
->pid
);
3815 binder_node_unlock(ref
->node
);
3816 binder_proc_unlock(proc
);
3820 if (death
->cookie
!= cookie
) {
3821 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
3822 proc
->pid
, thread
->pid
,
3825 binder_node_unlock(ref
->node
);
3826 binder_proc_unlock(proc
);
3830 binder_inner_proc_lock(proc
);
3831 if (list_empty(&death
->work
.entry
)) {
3832 death
->work
.type
= BINDER_WORK_CLEAR_DEATH_NOTIFICATION
;
3833 if (thread
->looper
&
3834 (BINDER_LOOPER_STATE_REGISTERED
|
3835 BINDER_LOOPER_STATE_ENTERED
))
3836 binder_enqueue_thread_work_ilocked(
3840 binder_enqueue_work_ilocked(
3843 binder_wakeup_proc_ilocked(
3847 BUG_ON(death
->work
.type
!= BINDER_WORK_DEAD_BINDER
);
3848 death
->work
.type
= BINDER_WORK_DEAD_BINDER_AND_CLEAR
;
3850 binder_inner_proc_unlock(proc
);
3852 binder_node_unlock(ref
->node
);
3853 binder_proc_unlock(proc
);
3855 case BC_DEAD_BINDER_DONE
: {
3856 struct binder_work
*w
;
3857 binder_uintptr_t cookie
;
3858 struct binder_ref_death
*death
= NULL
;
3860 if (get_user(cookie
, (binder_uintptr_t __user
*)ptr
))
3863 ptr
+= sizeof(cookie
);
3864 binder_inner_proc_lock(proc
);
3865 list_for_each_entry(w
, &proc
->delivered_death
,
3867 struct binder_ref_death
*tmp_death
=
3869 struct binder_ref_death
,
3872 if (tmp_death
->cookie
== cookie
) {
3877 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
3878 "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n",
3879 proc
->pid
, thread
->pid
, (u64
)cookie
,
3881 if (death
== NULL
) {
3882 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
3883 proc
->pid
, thread
->pid
, (u64
)cookie
);
3884 binder_inner_proc_unlock(proc
);
3887 binder_dequeue_work_ilocked(&death
->work
);
3888 if (death
->work
.type
== BINDER_WORK_DEAD_BINDER_AND_CLEAR
) {
3889 death
->work
.type
= BINDER_WORK_CLEAR_DEATH_NOTIFICATION
;
3890 if (thread
->looper
&
3891 (BINDER_LOOPER_STATE_REGISTERED
|
3892 BINDER_LOOPER_STATE_ENTERED
))
3893 binder_enqueue_thread_work_ilocked(
3894 thread
, &death
->work
);
3896 binder_enqueue_work_ilocked(
3899 binder_wakeup_proc_ilocked(proc
);
3902 binder_inner_proc_unlock(proc
);
3906 pr_err("%d:%d unknown command %d\n",
3907 proc
->pid
, thread
->pid
, cmd
);
3910 *consumed
= ptr
- buffer
;
3915 static void binder_stat_br(struct binder_proc
*proc
,
3916 struct binder_thread
*thread
, uint32_t cmd
)
3918 trace_binder_return(cmd
);
3919 if (_IOC_NR(cmd
) < ARRAY_SIZE(binder_stats
.br
)) {
3920 atomic_inc(&binder_stats
.br
[_IOC_NR(cmd
)]);
3921 atomic_inc(&proc
->stats
.br
[_IOC_NR(cmd
)]);
3922 atomic_inc(&thread
->stats
.br
[_IOC_NR(cmd
)]);
3926 static int binder_put_node_cmd(struct binder_proc
*proc
,
3927 struct binder_thread
*thread
,
3929 binder_uintptr_t node_ptr
,
3930 binder_uintptr_t node_cookie
,
3932 uint32_t cmd
, const char *cmd_name
)
3934 void __user
*ptr
= *ptrp
;
3936 if (put_user(cmd
, (uint32_t __user
*)ptr
))
3938 ptr
+= sizeof(uint32_t);
3940 if (put_user(node_ptr
, (binder_uintptr_t __user
*)ptr
))
3942 ptr
+= sizeof(binder_uintptr_t
);
3944 if (put_user(node_cookie
, (binder_uintptr_t __user
*)ptr
))
3946 ptr
+= sizeof(binder_uintptr_t
);
3948 binder_stat_br(proc
, thread
, cmd
);
3949 binder_debug(BINDER_DEBUG_USER_REFS
, "%d:%d %s %d u%016llx c%016llx\n",
3950 proc
->pid
, thread
->pid
, cmd_name
, node_debug_id
,
3951 (u64
)node_ptr
, (u64
)node_cookie
);
3957 static int binder_wait_for_work(struct binder_thread
*thread
,
3961 struct binder_proc
*proc
= thread
->proc
;
3964 freezer_do_not_count();
3965 binder_inner_proc_lock(proc
);
3967 prepare_to_wait(&thread
->wait
, &wait
, TASK_INTERRUPTIBLE
);
3968 if (binder_has_work_ilocked(thread
, do_proc_work
))
3971 list_add(&thread
->waiting_thread_node
,
3972 &proc
->waiting_threads
);
3973 binder_inner_proc_unlock(proc
);
3975 binder_inner_proc_lock(proc
);
3976 list_del_init(&thread
->waiting_thread_node
);
3977 if (signal_pending(current
)) {
3982 finish_wait(&thread
->wait
, &wait
);
3983 binder_inner_proc_unlock(proc
);
3989 static int binder_thread_read(struct binder_proc
*proc
,
3990 struct binder_thread
*thread
,
3991 binder_uintptr_t binder_buffer
, size_t size
,
3992 binder_size_t
*consumed
, int non_block
)
3994 void __user
*buffer
= (void __user
*)(uintptr_t)binder_buffer
;
3995 void __user
*ptr
= buffer
+ *consumed
;
3996 void __user
*end
= buffer
+ size
;
3999 int wait_for_proc_work
;
4001 if (*consumed
== 0) {
4002 if (put_user(BR_NOOP
, (uint32_t __user
*)ptr
))
4004 ptr
+= sizeof(uint32_t);
4008 binder_inner_proc_lock(proc
);
4009 wait_for_proc_work
= binder_available_for_proc_work_ilocked(thread
);
4010 binder_inner_proc_unlock(proc
);
4012 thread
->looper
|= BINDER_LOOPER_STATE_WAITING
;
4014 trace_binder_wait_for_work(wait_for_proc_work
,
4015 !!thread
->transaction_stack
,
4016 !binder_worklist_empty(proc
, &thread
->todo
));
4017 if (wait_for_proc_work
) {
4018 if (!(thread
->looper
& (BINDER_LOOPER_STATE_REGISTERED
|
4019 BINDER_LOOPER_STATE_ENTERED
))) {
4020 binder_user_error("%d:%d ERROR: Thread waiting for process work before calling BC_REGISTER_LOOPER or BC_ENTER_LOOPER (state %x)\n",
4021 proc
->pid
, thread
->pid
, thread
->looper
);
4022 wait_event_interruptible(binder_user_error_wait
,
4023 binder_stop_on_user_error
< 2);
4025 binder_restore_priority(current
, proc
->default_priority
);
4029 if (!binder_has_work(thread
, wait_for_proc_work
))
4032 ret
= binder_wait_for_work(thread
, wait_for_proc_work
);
4035 thread
->looper
&= ~BINDER_LOOPER_STATE_WAITING
;
4042 struct binder_transaction_data tr
;
4043 struct binder_work
*w
= NULL
;
4044 struct list_head
*list
= NULL
;
4045 struct binder_transaction
*t
= NULL
;
4046 struct binder_thread
*t_from
;
4048 binder_inner_proc_lock(proc
);
4049 if (!binder_worklist_empty_ilocked(&thread
->todo
))
4050 list
= &thread
->todo
;
4051 else if (!binder_worklist_empty_ilocked(&proc
->todo
) &&
4055 binder_inner_proc_unlock(proc
);
4058 if (ptr
- buffer
== 4 && !thread
->looper_need_return
)
4063 if (end
- ptr
< sizeof(tr
) + 4) {
4064 binder_inner_proc_unlock(proc
);
4067 w
= binder_dequeue_work_head_ilocked(list
);
4068 if (binder_worklist_empty_ilocked(&thread
->todo
))
4069 thread
->process_todo
= false;
4072 case BINDER_WORK_TRANSACTION
: {
4073 binder_inner_proc_unlock(proc
);
4074 t
= container_of(w
, struct binder_transaction
, work
);
4076 case BINDER_WORK_RETURN_ERROR
: {
4077 struct binder_error
*e
= container_of(
4078 w
, struct binder_error
, work
);
4080 WARN_ON(e
->cmd
== BR_OK
);
4081 binder_inner_proc_unlock(proc
);
4082 if (put_user(e
->cmd
, (uint32_t __user
*)ptr
))
4085 ptr
+= sizeof(uint32_t);
4087 binder_stat_br(proc
, thread
, e
->cmd
);
4089 case BINDER_WORK_TRANSACTION_COMPLETE
: {
4090 binder_inner_proc_unlock(proc
);
4091 cmd
= BR_TRANSACTION_COMPLETE
;
4092 if (put_user(cmd
, (uint32_t __user
*)ptr
))
4094 ptr
+= sizeof(uint32_t);
4096 binder_stat_br(proc
, thread
, cmd
);
4097 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE
,
4098 "%d:%d BR_TRANSACTION_COMPLETE\n",
4099 proc
->pid
, thread
->pid
);
4101 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE
);
4103 case BINDER_WORK_NODE
: {
4104 struct binder_node
*node
= container_of(w
, struct binder_node
, work
);
4106 binder_uintptr_t node_ptr
= node
->ptr
;
4107 binder_uintptr_t node_cookie
= node
->cookie
;
4108 int node_debug_id
= node
->debug_id
;
4111 void __user
*orig_ptr
= ptr
;
4113 BUG_ON(proc
!= node
->proc
);
4114 strong
= node
->internal_strong_refs
||
4115 node
->local_strong_refs
;
4116 weak
= !hlist_empty(&node
->refs
) ||
4117 node
->local_weak_refs
||
4118 node
->tmp_refs
|| strong
;
4119 has_strong_ref
= node
->has_strong_ref
;
4120 has_weak_ref
= node
->has_weak_ref
;
4122 if (weak
&& !has_weak_ref
) {
4123 node
->has_weak_ref
= 1;
4124 node
->pending_weak_ref
= 1;
4125 node
->local_weak_refs
++;
4127 if (strong
&& !has_strong_ref
) {
4128 node
->has_strong_ref
= 1;
4129 node
->pending_strong_ref
= 1;
4130 node
->local_strong_refs
++;
4132 if (!strong
&& has_strong_ref
)
4133 node
->has_strong_ref
= 0;
4134 if (!weak
&& has_weak_ref
)
4135 node
->has_weak_ref
= 0;
4136 if (!weak
&& !strong
) {
4137 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
4138 "%d:%d node %d u%016llx c%016llx deleted\n",
4139 proc
->pid
, thread
->pid
,
4143 rb_erase(&node
->rb_node
, &proc
->nodes
);
4144 binder_inner_proc_unlock(proc
);
4145 binder_node_lock(node
);
4147 * Acquire the node lock before freeing the
4148 * node to serialize with other threads that
4149 * may have been holding the node lock while
4150 * decrementing this node (avoids race where
4151 * this thread frees while the other thread
4152 * is unlocking the node after the final
4155 binder_node_unlock(node
);
4156 binder_free_node(node
);
4158 binder_inner_proc_unlock(proc
);
4160 if (weak
&& !has_weak_ref
)
4161 ret
= binder_put_node_cmd(
4162 proc
, thread
, &ptr
, node_ptr
,
4163 node_cookie
, node_debug_id
,
4164 BR_INCREFS
, "BR_INCREFS");
4165 if (!ret
&& strong
&& !has_strong_ref
)
4166 ret
= binder_put_node_cmd(
4167 proc
, thread
, &ptr
, node_ptr
,
4168 node_cookie
, node_debug_id
,
4169 BR_ACQUIRE
, "BR_ACQUIRE");
4170 if (!ret
&& !strong
&& has_strong_ref
)
4171 ret
= binder_put_node_cmd(
4172 proc
, thread
, &ptr
, node_ptr
,
4173 node_cookie
, node_debug_id
,
4174 BR_RELEASE
, "BR_RELEASE");
4175 if (!ret
&& !weak
&& has_weak_ref
)
4176 ret
= binder_put_node_cmd(
4177 proc
, thread
, &ptr
, node_ptr
,
4178 node_cookie
, node_debug_id
,
4179 BR_DECREFS
, "BR_DECREFS");
4180 if (orig_ptr
== ptr
)
4181 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
4182 "%d:%d node %d u%016llx c%016llx state unchanged\n",
4183 proc
->pid
, thread
->pid
,
4190 case BINDER_WORK_DEAD_BINDER
:
4191 case BINDER_WORK_DEAD_BINDER_AND_CLEAR
:
4192 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION
: {
4193 struct binder_ref_death
*death
;
4195 binder_uintptr_t cookie
;
4197 death
= container_of(w
, struct binder_ref_death
, work
);
4198 if (w
->type
== BINDER_WORK_CLEAR_DEATH_NOTIFICATION
)
4199 cmd
= BR_CLEAR_DEATH_NOTIFICATION_DONE
;
4201 cmd
= BR_DEAD_BINDER
;
4202 cookie
= death
->cookie
;
4204 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION
,
4205 "%d:%d %s %016llx\n",
4206 proc
->pid
, thread
->pid
,
4207 cmd
== BR_DEAD_BINDER
?
4209 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
4211 if (w
->type
== BINDER_WORK_CLEAR_DEATH_NOTIFICATION
) {
4212 binder_inner_proc_unlock(proc
);
4214 binder_stats_deleted(BINDER_STAT_DEATH
);
4216 binder_enqueue_work_ilocked(
4217 w
, &proc
->delivered_death
);
4218 binder_inner_proc_unlock(proc
);
4220 if (put_user(cmd
, (uint32_t __user
*)ptr
))
4222 ptr
+= sizeof(uint32_t);
4223 if (put_user(cookie
,
4224 (binder_uintptr_t __user
*)ptr
))
4226 ptr
+= sizeof(binder_uintptr_t
);
4227 binder_stat_br(proc
, thread
, cmd
);
4228 if (cmd
== BR_DEAD_BINDER
)
4229 goto done
; /* DEAD_BINDER notifications can cause transactions */
4236 BUG_ON(t
->buffer
== NULL
);
4237 if (t
->buffer
->target_node
) {
4238 struct binder_node
*target_node
= t
->buffer
->target_node
;
4239 struct binder_priority node_prio
;
4241 tr
.target
.ptr
= target_node
->ptr
;
4242 tr
.cookie
= target_node
->cookie
;
4243 node_prio
.sched_policy
= target_node
->sched_policy
;
4244 node_prio
.prio
= target_node
->min_priority
;
4245 binder_transaction_priority(current
, t
, node_prio
,
4246 target_node
->inherit_rt
);
4247 cmd
= BR_TRANSACTION
;
4254 tr
.flags
= t
->flags
;
4255 tr
.sender_euid
= from_kuid(current_user_ns(), t
->sender_euid
);
4257 t_from
= binder_get_txn_from(t
);
4259 struct task_struct
*sender
= t_from
->proc
->tsk
;
4261 tr
.sender_pid
= task_tgid_nr_ns(sender
,
4262 task_active_pid_ns(current
));
4267 tr
.data_size
= t
->buffer
->data_size
;
4268 tr
.offsets_size
= t
->buffer
->offsets_size
;
4269 tr
.data
.ptr
.buffer
= (binder_uintptr_t
)
4270 ((uintptr_t)t
->buffer
->data
+
4271 binder_alloc_get_user_buffer_offset(&proc
->alloc
));
4272 tr
.data
.ptr
.offsets
= tr
.data
.ptr
.buffer
+
4273 ALIGN(t
->buffer
->data_size
,
4276 if (put_user(cmd
, (uint32_t __user
*)ptr
)) {
4278 binder_thread_dec_tmpref(t_from
);
4280 binder_cleanup_transaction(t
, "put_user failed",
4285 ptr
+= sizeof(uint32_t);
4286 if (copy_to_user(ptr
, &tr
, sizeof(tr
))) {
4288 binder_thread_dec_tmpref(t_from
);
4290 binder_cleanup_transaction(t
, "copy_to_user failed",
4297 trace_binder_transaction_received(t
);
4298 binder_stat_br(proc
, thread
, cmd
);
4299 binder_debug(BINDER_DEBUG_TRANSACTION
,
4300 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
4301 proc
->pid
, thread
->pid
,
4302 (cmd
== BR_TRANSACTION
) ? "BR_TRANSACTION" :
4304 t
->debug_id
, t_from
? t_from
->proc
->pid
: 0,
4305 t_from
? t_from
->pid
: 0, cmd
,
4306 t
->buffer
->data_size
, t
->buffer
->offsets_size
,
4307 (u64
)tr
.data
.ptr
.buffer
, (u64
)tr
.data
.ptr
.offsets
);
4310 binder_thread_dec_tmpref(t_from
);
4311 t
->buffer
->allow_user_free
= 1;
4312 if (cmd
== BR_TRANSACTION
&& !(t
->flags
& TF_ONE_WAY
)) {
4313 binder_inner_proc_lock(thread
->proc
);
4314 t
->to_parent
= thread
->transaction_stack
;
4315 t
->to_thread
= thread
;
4316 thread
->transaction_stack
= t
;
4317 binder_inner_proc_unlock(thread
->proc
);
4319 binder_free_transaction(t
);
4326 *consumed
= ptr
- buffer
;
4327 binder_inner_proc_lock(proc
);
4328 if (proc
->requested_threads
== 0 &&
4329 list_empty(&thread
->proc
->waiting_threads
) &&
4330 proc
->requested_threads_started
< proc
->max_threads
&&
4331 (thread
->looper
& (BINDER_LOOPER_STATE_REGISTERED
|
4332 BINDER_LOOPER_STATE_ENTERED
)) /* the user-space code fails to */
4333 /*spawn a new thread if we leave this out */) {
4334 proc
->requested_threads
++;
4335 binder_inner_proc_unlock(proc
);
4336 binder_debug(BINDER_DEBUG_THREADS
,
4337 "%d:%d BR_SPAWN_LOOPER\n",
4338 proc
->pid
, thread
->pid
);
4339 if (put_user(BR_SPAWN_LOOPER
, (uint32_t __user
*)buffer
))
4341 binder_stat_br(proc
, thread
, BR_SPAWN_LOOPER
);
4343 binder_inner_proc_unlock(proc
);
4347 static void binder_release_work(struct binder_proc
*proc
,
4348 struct list_head
*list
)
4350 struct binder_work
*w
;
4353 w
= binder_dequeue_work_head(proc
, list
);
4358 case BINDER_WORK_TRANSACTION
: {
4359 struct binder_transaction
*t
;
4361 t
= container_of(w
, struct binder_transaction
, work
);
4363 binder_cleanup_transaction(t
, "process died.",
4366 case BINDER_WORK_RETURN_ERROR
: {
4367 struct binder_error
*e
= container_of(
4368 w
, struct binder_error
, work
);
4370 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
4371 "undelivered TRANSACTION_ERROR: %u\n",
4374 case BINDER_WORK_TRANSACTION_COMPLETE
: {
4375 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
4376 "undelivered TRANSACTION_COMPLETE\n");
4378 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE
);
4380 case BINDER_WORK_DEAD_BINDER_AND_CLEAR
:
4381 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION
: {
4382 struct binder_ref_death
*death
;
4384 death
= container_of(w
, struct binder_ref_death
, work
);
4385 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
4386 "undelivered death notification, %016llx\n",
4387 (u64
)death
->cookie
);
4389 binder_stats_deleted(BINDER_STAT_DEATH
);
4392 pr_err("unexpected work type, %d, not freed\n",
4400 static struct binder_thread
*binder_get_thread_ilocked(
4401 struct binder_proc
*proc
, struct binder_thread
*new_thread
)
4403 struct binder_thread
*thread
= NULL
;
4404 struct rb_node
*parent
= NULL
;
4405 struct rb_node
**p
= &proc
->threads
.rb_node
;
4409 thread
= rb_entry(parent
, struct binder_thread
, rb_node
);
4411 if (current
->pid
< thread
->pid
)
4413 else if (current
->pid
> thread
->pid
)
4414 p
= &(*p
)->rb_right
;
4420 thread
= new_thread
;
4421 binder_stats_created(BINDER_STAT_THREAD
);
4422 thread
->proc
= proc
;
4423 thread
->pid
= current
->pid
;
4424 get_task_struct(current
);
4425 thread
->task
= current
;
4426 atomic_set(&thread
->tmp_ref
, 0);
4427 init_waitqueue_head(&thread
->wait
);
4428 INIT_LIST_HEAD(&thread
->todo
);
4429 rb_link_node(&thread
->rb_node
, parent
, p
);
4430 rb_insert_color(&thread
->rb_node
, &proc
->threads
);
4431 thread
->looper_need_return
= true;
4432 thread
->return_error
.work
.type
= BINDER_WORK_RETURN_ERROR
;
4433 thread
->return_error
.cmd
= BR_OK
;
4434 thread
->reply_error
.work
.type
= BINDER_WORK_RETURN_ERROR
;
4435 thread
->reply_error
.cmd
= BR_OK
;
4436 INIT_LIST_HEAD(&new_thread
->waiting_thread_node
);
4440 static struct binder_thread
*binder_get_thread(struct binder_proc
*proc
)
4442 struct binder_thread
*thread
;
4443 struct binder_thread
*new_thread
;
4445 binder_inner_proc_lock(proc
);
4446 thread
= binder_get_thread_ilocked(proc
, NULL
);
4447 binder_inner_proc_unlock(proc
);
4449 new_thread
= kzalloc(sizeof(*thread
), GFP_KERNEL
);
4450 if (new_thread
== NULL
)
4452 binder_inner_proc_lock(proc
);
4453 thread
= binder_get_thread_ilocked(proc
, new_thread
);
4454 binder_inner_proc_unlock(proc
);
4455 if (thread
!= new_thread
)
4461 static void binder_free_proc(struct binder_proc
*proc
)
4463 BUG_ON(!list_empty(&proc
->todo
));
4464 BUG_ON(!list_empty(&proc
->delivered_death
));
4465 binder_alloc_deferred_release(&proc
->alloc
);
4466 put_task_struct(proc
->tsk
);
4467 binder_stats_deleted(BINDER_STAT_PROC
);
4471 static void binder_free_thread(struct binder_thread
*thread
)
4473 BUG_ON(!list_empty(&thread
->todo
));
4474 binder_stats_deleted(BINDER_STAT_THREAD
);
4475 binder_proc_dec_tmpref(thread
->proc
);
4476 put_task_struct(thread
->task
);
4480 static int binder_thread_release(struct binder_proc
*proc
,
4481 struct binder_thread
*thread
)
4483 struct binder_transaction
*t
;
4484 struct binder_transaction
*send_reply
= NULL
;
4485 int active_transactions
= 0;
4486 struct binder_transaction
*last_t
= NULL
;
4488 binder_inner_proc_lock(thread
->proc
);
4490 * take a ref on the proc so it survives
4491 * after we remove this thread from proc->threads.
4492 * The corresponding dec is when we actually
4493 * free the thread in binder_free_thread()
4497 * take a ref on this thread to ensure it
4498 * survives while we are releasing it
4500 atomic_inc(&thread
->tmp_ref
);
4501 rb_erase(&thread
->rb_node
, &proc
->threads
);
4502 t
= thread
->transaction_stack
;
4504 spin_lock(&t
->lock
);
4505 if (t
->to_thread
== thread
)
4508 thread
->is_dead
= true;
4512 active_transactions
++;
4513 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
4514 "release %d:%d transaction %d %s, still active\n",
4515 proc
->pid
, thread
->pid
,
4517 (t
->to_thread
== thread
) ? "in" : "out");
4519 if (t
->to_thread
== thread
) {
4521 t
->to_thread
= NULL
;
4523 t
->buffer
->transaction
= NULL
;
4527 } else if (t
->from
== thread
) {
4532 spin_unlock(&last_t
->lock
);
4534 spin_lock(&t
->lock
);
4538 * If this thread used poll, make sure we remove the waitqueue
4539 * from any epoll data structures holding it with POLLFREE.
4540 * waitqueue_active() is safe to use here because we're holding
4543 if ((thread
->looper
& BINDER_LOOPER_STATE_POLL
) &&
4544 waitqueue_active(&thread
->wait
)) {
4545 wake_up_poll(&thread
->wait
, POLLHUP
| POLLFREE
);
4548 binder_inner_proc_unlock(thread
->proc
);
4551 binder_send_failed_reply(send_reply
, BR_DEAD_REPLY
);
4552 binder_release_work(proc
, &thread
->todo
);
4553 binder_thread_dec_tmpref(thread
);
4554 return active_transactions
;
4557 static unsigned int binder_poll(struct file
*filp
,
4558 struct poll_table_struct
*wait
)
4560 struct binder_proc
*proc
= filp
->private_data
;
4561 struct binder_thread
*thread
= NULL
;
4562 bool wait_for_proc_work
;
4564 thread
= binder_get_thread(proc
);
4566 binder_inner_proc_lock(thread
->proc
);
4567 thread
->looper
|= BINDER_LOOPER_STATE_POLL
;
4568 wait_for_proc_work
= binder_available_for_proc_work_ilocked(thread
);
4570 binder_inner_proc_unlock(thread
->proc
);
4572 poll_wait(filp
, &thread
->wait
, wait
);
4574 if (binder_has_work(thread
, wait_for_proc_work
))
4580 static int binder_ioctl_write_read(struct file
*filp
,
4581 unsigned int cmd
, unsigned long arg
,
4582 struct binder_thread
*thread
)
4585 struct binder_proc
*proc
= filp
->private_data
;
4586 unsigned int size
= _IOC_SIZE(cmd
);
4587 void __user
*ubuf
= (void __user
*)arg
;
4588 struct binder_write_read bwr
;
4590 if (size
!= sizeof(struct binder_write_read
)) {
4594 if (copy_from_user(&bwr
, ubuf
, sizeof(bwr
))) {
4598 binder_debug(BINDER_DEBUG_READ_WRITE
,
4599 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
4600 proc
->pid
, thread
->pid
,
4601 (u64
)bwr
.write_size
, (u64
)bwr
.write_buffer
,
4602 (u64
)bwr
.read_size
, (u64
)bwr
.read_buffer
);
4604 if (bwr
.write_size
> 0) {
4605 ret
= binder_thread_write(proc
, thread
,
4608 &bwr
.write_consumed
);
4609 trace_binder_write_done(ret
);
4611 bwr
.read_consumed
= 0;
4612 if (copy_to_user(ubuf
, &bwr
, sizeof(bwr
)))
4617 if (bwr
.read_size
> 0) {
4618 ret
= binder_thread_read(proc
, thread
, bwr
.read_buffer
,
4621 filp
->f_flags
& O_NONBLOCK
);
4622 trace_binder_read_done(ret
);
4623 binder_inner_proc_lock(proc
);
4624 if (!binder_worklist_empty_ilocked(&proc
->todo
))
4625 binder_wakeup_proc_ilocked(proc
);
4626 binder_inner_proc_unlock(proc
);
4628 if (copy_to_user(ubuf
, &bwr
, sizeof(bwr
)))
4633 binder_debug(BINDER_DEBUG_READ_WRITE
,
4634 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
4635 proc
->pid
, thread
->pid
,
4636 (u64
)bwr
.write_consumed
, (u64
)bwr
.write_size
,
4637 (u64
)bwr
.read_consumed
, (u64
)bwr
.read_size
);
4638 if (copy_to_user(ubuf
, &bwr
, sizeof(bwr
))) {
4646 static int binder_ioctl_set_ctx_mgr(struct file
*filp
)
4649 struct binder_proc
*proc
= filp
->private_data
;
4650 struct binder_context
*context
= proc
->context
;
4651 struct binder_node
*new_node
;
4652 kuid_t curr_euid
= current_euid();
4654 mutex_lock(&context
->context_mgr_node_lock
);
4655 if (context
->binder_context_mgr_node
) {
4656 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
4660 ret
= security_binder_set_context_mgr(proc
->tsk
);
4663 if (uid_valid(context
->binder_context_mgr_uid
)) {
4664 if (!uid_eq(context
->binder_context_mgr_uid
, curr_euid
)) {
4665 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
4666 from_kuid(&init_user_ns
, curr_euid
),
4667 from_kuid(&init_user_ns
,
4668 context
->binder_context_mgr_uid
));
4673 context
->binder_context_mgr_uid
= curr_euid
;
4675 new_node
= binder_new_node(proc
, NULL
);
4680 binder_node_lock(new_node
);
4681 new_node
->local_weak_refs
++;
4682 new_node
->local_strong_refs
++;
4683 new_node
->has_strong_ref
= 1;
4684 new_node
->has_weak_ref
= 1;
4685 context
->binder_context_mgr_node
= new_node
;
4686 binder_node_unlock(new_node
);
4687 binder_put_node(new_node
);
4689 mutex_unlock(&context
->context_mgr_node_lock
);
4693 static int binder_ioctl_get_node_debug_info(struct binder_proc
*proc
,
4694 struct binder_node_debug_info
*info
)
4697 binder_uintptr_t ptr
= info
->ptr
;
4699 memset(info
, 0, sizeof(*info
));
4701 binder_inner_proc_lock(proc
);
4702 for (n
= rb_first(&proc
->nodes
); n
!= NULL
; n
= rb_next(n
)) {
4703 struct binder_node
*node
= rb_entry(n
, struct binder_node
,
4705 if (node
->ptr
> ptr
) {
4706 info
->ptr
= node
->ptr
;
4707 info
->cookie
= node
->cookie
;
4708 info
->has_strong_ref
= node
->has_strong_ref
;
4709 info
->has_weak_ref
= node
->has_weak_ref
;
4713 binder_inner_proc_unlock(proc
);
4718 static long binder_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
4721 struct binder_proc
*proc
= filp
->private_data
;
4722 struct binder_thread
*thread
;
4723 unsigned int size
= _IOC_SIZE(cmd
);
4724 void __user
*ubuf
= (void __user
*)arg
;
4726 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
4727 proc->pid, current->pid, cmd, arg);*/
4729 binder_selftest_alloc(&proc
->alloc
);
4731 trace_binder_ioctl(cmd
, arg
);
4733 ret
= wait_event_interruptible(binder_user_error_wait
, binder_stop_on_user_error
< 2);
4737 thread
= binder_get_thread(proc
);
4738 if (thread
== NULL
) {
4744 case BINDER_WRITE_READ
:
4745 ret
= binder_ioctl_write_read(filp
, cmd
, arg
, thread
);
4749 case BINDER_SET_MAX_THREADS
: {
4752 if (copy_from_user(&max_threads
, ubuf
,
4753 sizeof(max_threads
))) {
4757 binder_inner_proc_lock(proc
);
4758 proc
->max_threads
= max_threads
;
4759 binder_inner_proc_unlock(proc
);
4762 case BINDER_SET_CONTEXT_MGR
:
4763 ret
= binder_ioctl_set_ctx_mgr(filp
);
4767 case BINDER_THREAD_EXIT
:
4768 binder_debug(BINDER_DEBUG_THREADS
, "%d:%d exit\n",
4769 proc
->pid
, thread
->pid
);
4770 binder_thread_release(proc
, thread
);
4773 case BINDER_VERSION
: {
4774 struct binder_version __user
*ver
= ubuf
;
4776 if (size
!= sizeof(struct binder_version
)) {
4780 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION
,
4781 &ver
->protocol_version
)) {
4787 case BINDER_GET_NODE_DEBUG_INFO
: {
4788 struct binder_node_debug_info info
;
4790 if (copy_from_user(&info
, ubuf
, sizeof(info
))) {
4795 ret
= binder_ioctl_get_node_debug_info(proc
, &info
);
4799 if (copy_to_user(ubuf
, &info
, sizeof(info
))) {
4812 thread
->looper_need_return
= false;
4813 wait_event_interruptible(binder_user_error_wait
, binder_stop_on_user_error
< 2);
4814 if (ret
&& ret
!= -ERESTARTSYS
)
4815 pr_info("%d:%d ioctl %x %lx returned %d\n", proc
->pid
, current
->pid
, cmd
, arg
, ret
);
4817 trace_binder_ioctl_done(ret
);
4821 static void binder_vma_open(struct vm_area_struct
*vma
)
4823 struct binder_proc
*proc
= vma
->vm_private_data
;
4825 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
4826 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
4827 proc
->pid
, vma
->vm_start
, vma
->vm_end
,
4828 (vma
->vm_end
- vma
->vm_start
) / SZ_1K
, vma
->vm_flags
,
4829 (unsigned long)pgprot_val(vma
->vm_page_prot
));
4832 static void binder_vma_close(struct vm_area_struct
*vma
)
4834 struct binder_proc
*proc
= vma
->vm_private_data
;
4836 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
4837 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
4838 proc
->pid
, vma
->vm_start
, vma
->vm_end
,
4839 (vma
->vm_end
- vma
->vm_start
) / SZ_1K
, vma
->vm_flags
,
4840 (unsigned long)pgprot_val(vma
->vm_page_prot
));
4841 binder_alloc_vma_close(&proc
->alloc
);
4842 binder_defer_work(proc
, BINDER_DEFERRED_PUT_FILES
);
4845 static int binder_vm_fault(struct vm_fault
*vmf
)
4847 return VM_FAULT_SIGBUS
;
4850 static const struct vm_operations_struct binder_vm_ops
= {
4851 .open
= binder_vma_open
,
4852 .close
= binder_vma_close
,
4853 .fault
= binder_vm_fault
,
4856 static int binder_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
4859 struct binder_proc
*proc
= filp
->private_data
;
4860 const char *failure_string
;
4862 if (proc
->tsk
!= current
->group_leader
)
4865 if ((vma
->vm_end
- vma
->vm_start
) > SZ_4M
)
4866 vma
->vm_end
= vma
->vm_start
+ SZ_4M
;
4868 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
4869 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
4870 __func__
, proc
->pid
, vma
->vm_start
, vma
->vm_end
,
4871 (vma
->vm_end
- vma
->vm_start
) / SZ_1K
, vma
->vm_flags
,
4872 (unsigned long)pgprot_val(vma
->vm_page_prot
));
4874 if (vma
->vm_flags
& FORBIDDEN_MMAP_FLAGS
) {
4876 failure_string
= "bad vm_flags";
4879 vma
->vm_flags
= (vma
->vm_flags
| VM_DONTCOPY
) & ~VM_MAYWRITE
;
4880 vma
->vm_ops
= &binder_vm_ops
;
4881 vma
->vm_private_data
= proc
;
4883 ret
= binder_alloc_mmap_handler(&proc
->alloc
, vma
);
4886 mutex_lock(&proc
->files_lock
);
4887 proc
->files
= get_files_struct(current
);
4888 mutex_unlock(&proc
->files_lock
);
4892 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
4893 proc
->pid
, vma
->vm_start
, vma
->vm_end
, failure_string
, ret
);
4897 static int binder_open(struct inode
*nodp
, struct file
*filp
)
4899 struct binder_proc
*proc
;
4900 struct binder_device
*binder_dev
;
4902 binder_debug(BINDER_DEBUG_OPEN_CLOSE
, "binder_open: %d:%d\n",
4903 current
->group_leader
->pid
, current
->pid
);
4905 proc
= kzalloc(sizeof(*proc
), GFP_KERNEL
);
4908 spin_lock_init(&proc
->inner_lock
);
4909 spin_lock_init(&proc
->outer_lock
);
4910 get_task_struct(current
->group_leader
);
4911 proc
->tsk
= current
->group_leader
;
4912 mutex_init(&proc
->files_lock
);
4913 INIT_LIST_HEAD(&proc
->todo
);
4914 if (binder_supported_policy(current
->policy
)) {
4915 proc
->default_priority
.sched_policy
= current
->policy
;
4916 proc
->default_priority
.prio
= current
->normal_prio
;
4918 proc
->default_priority
.sched_policy
= SCHED_NORMAL
;
4919 proc
->default_priority
.prio
= NICE_TO_PRIO(0);
4922 binder_dev
= container_of(filp
->private_data
, struct binder_device
,
4924 proc
->context
= &binder_dev
->context
;
4925 binder_alloc_init(&proc
->alloc
);
4927 binder_stats_created(BINDER_STAT_PROC
);
4928 proc
->pid
= current
->group_leader
->pid
;
4929 INIT_LIST_HEAD(&proc
->delivered_death
);
4930 INIT_LIST_HEAD(&proc
->waiting_threads
);
4931 filp
->private_data
= proc
;
4933 mutex_lock(&binder_procs_lock
);
4934 hlist_add_head(&proc
->proc_node
, &binder_procs
);
4935 mutex_unlock(&binder_procs_lock
);
4937 if (binder_debugfs_dir_entry_proc
) {
4940 snprintf(strbuf
, sizeof(strbuf
), "%u", proc
->pid
);
4942 * proc debug entries are shared between contexts, so
4943 * this will fail if the process tries to open the driver
4944 * again with a different context. The priting code will
4945 * anyway print all contexts that a given PID has, so this
4948 proc
->debugfs_entry
= debugfs_create_file(strbuf
, S_IRUGO
,
4949 binder_debugfs_dir_entry_proc
,
4950 (void *)(unsigned long)proc
->pid
,
4957 static int binder_flush(struct file
*filp
, fl_owner_t id
)
4959 struct binder_proc
*proc
= filp
->private_data
;
4961 binder_defer_work(proc
, BINDER_DEFERRED_FLUSH
);
4966 static void binder_deferred_flush(struct binder_proc
*proc
)
4971 binder_inner_proc_lock(proc
);
4972 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
)) {
4973 struct binder_thread
*thread
= rb_entry(n
, struct binder_thread
, rb_node
);
4975 thread
->looper_need_return
= true;
4976 if (thread
->looper
& BINDER_LOOPER_STATE_WAITING
) {
4977 wake_up_interruptible(&thread
->wait
);
4981 binder_inner_proc_unlock(proc
);
4983 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
4984 "binder_flush: %d woke %d threads\n", proc
->pid
,
4988 static int binder_release(struct inode
*nodp
, struct file
*filp
)
4990 struct binder_proc
*proc
= filp
->private_data
;
4992 debugfs_remove(proc
->debugfs_entry
);
4993 binder_defer_work(proc
, BINDER_DEFERRED_RELEASE
);
4998 static int binder_node_release(struct binder_node
*node
, int refs
)
5000 struct binder_ref
*ref
;
5002 struct binder_proc
*proc
= node
->proc
;
5004 binder_release_work(proc
, &node
->async_todo
);
5006 binder_node_lock(node
);
5007 binder_inner_proc_lock(proc
);
5008 binder_dequeue_work_ilocked(&node
->work
);
5010 * The caller must have taken a temporary ref on the node,
5012 BUG_ON(!node
->tmp_refs
);
5013 if (hlist_empty(&node
->refs
) && node
->tmp_refs
== 1) {
5014 binder_inner_proc_unlock(proc
);
5015 binder_node_unlock(node
);
5016 binder_free_node(node
);
5022 node
->local_strong_refs
= 0;
5023 node
->local_weak_refs
= 0;
5024 binder_inner_proc_unlock(proc
);
5026 spin_lock(&binder_dead_nodes_lock
);
5027 hlist_add_head(&node
->dead_node
, &binder_dead_nodes
);
5028 spin_unlock(&binder_dead_nodes_lock
);
5030 hlist_for_each_entry(ref
, &node
->refs
, node_entry
) {
5033 * Need the node lock to synchronize
5034 * with new notification requests and the
5035 * inner lock to synchronize with queued
5036 * death notifications.
5038 binder_inner_proc_lock(ref
->proc
);
5040 binder_inner_proc_unlock(ref
->proc
);
5046 BUG_ON(!list_empty(&ref
->death
->work
.entry
));
5047 ref
->death
->work
.type
= BINDER_WORK_DEAD_BINDER
;
5048 binder_enqueue_work_ilocked(&ref
->death
->work
,
5050 binder_wakeup_proc_ilocked(ref
->proc
);
5051 binder_inner_proc_unlock(ref
->proc
);
5054 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
5055 "node %d now dead, refs %d, death %d\n",
5056 node
->debug_id
, refs
, death
);
5057 binder_node_unlock(node
);
5058 binder_put_node(node
);
5063 static void binder_deferred_release(struct binder_proc
*proc
)
5065 struct binder_context
*context
= proc
->context
;
5067 int threads
, nodes
, incoming_refs
, outgoing_refs
, active_transactions
;
5069 BUG_ON(proc
->files
);
5071 mutex_lock(&binder_procs_lock
);
5072 hlist_del(&proc
->proc_node
);
5073 mutex_unlock(&binder_procs_lock
);
5075 mutex_lock(&context
->context_mgr_node_lock
);
5076 if (context
->binder_context_mgr_node
&&
5077 context
->binder_context_mgr_node
->proc
== proc
) {
5078 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
5079 "%s: %d context_mgr_node gone\n",
5080 __func__
, proc
->pid
);
5081 context
->binder_context_mgr_node
= NULL
;
5083 mutex_unlock(&context
->context_mgr_node_lock
);
5084 binder_inner_proc_lock(proc
);
5086 * Make sure proc stays alive after we
5087 * remove all the threads
5091 proc
->is_dead
= true;
5093 active_transactions
= 0;
5094 while ((n
= rb_first(&proc
->threads
))) {
5095 struct binder_thread
*thread
;
5097 thread
= rb_entry(n
, struct binder_thread
, rb_node
);
5098 binder_inner_proc_unlock(proc
);
5100 active_transactions
+= binder_thread_release(proc
, thread
);
5101 binder_inner_proc_lock(proc
);
5106 while ((n
= rb_first(&proc
->nodes
))) {
5107 struct binder_node
*node
;
5109 node
= rb_entry(n
, struct binder_node
, rb_node
);
5112 * take a temporary ref on the node before
5113 * calling binder_node_release() which will either
5114 * kfree() the node or call binder_put_node()
5116 binder_inc_node_tmpref_ilocked(node
);
5117 rb_erase(&node
->rb_node
, &proc
->nodes
);
5118 binder_inner_proc_unlock(proc
);
5119 incoming_refs
= binder_node_release(node
, incoming_refs
);
5120 binder_inner_proc_lock(proc
);
5122 binder_inner_proc_unlock(proc
);
5125 binder_proc_lock(proc
);
5126 while ((n
= rb_first(&proc
->refs_by_desc
))) {
5127 struct binder_ref
*ref
;
5129 ref
= rb_entry(n
, struct binder_ref
, rb_node_desc
);
5131 binder_cleanup_ref_olocked(ref
);
5132 binder_proc_unlock(proc
);
5133 binder_free_ref(ref
);
5134 binder_proc_lock(proc
);
5136 binder_proc_unlock(proc
);
5138 binder_release_work(proc
, &proc
->todo
);
5139 binder_release_work(proc
, &proc
->delivered_death
);
5141 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
5142 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
5143 __func__
, proc
->pid
, threads
, nodes
, incoming_refs
,
5144 outgoing_refs
, active_transactions
);
5146 binder_proc_dec_tmpref(proc
);
5149 static void binder_deferred_func(struct work_struct
*work
)
5151 struct binder_proc
*proc
;
5152 struct files_struct
*files
;
5157 mutex_lock(&binder_deferred_lock
);
5158 if (!hlist_empty(&binder_deferred_list
)) {
5159 proc
= hlist_entry(binder_deferred_list
.first
,
5160 struct binder_proc
, deferred_work_node
);
5161 hlist_del_init(&proc
->deferred_work_node
);
5162 defer
= proc
->deferred_work
;
5163 proc
->deferred_work
= 0;
5168 mutex_unlock(&binder_deferred_lock
);
5171 if (defer
& BINDER_DEFERRED_PUT_FILES
) {
5172 mutex_lock(&proc
->files_lock
);
5173 files
= proc
->files
;
5176 mutex_unlock(&proc
->files_lock
);
5179 if (defer
& BINDER_DEFERRED_FLUSH
)
5180 binder_deferred_flush(proc
);
5182 if (defer
& BINDER_DEFERRED_RELEASE
)
5183 binder_deferred_release(proc
); /* frees proc */
5186 put_files_struct(files
);
5189 static DECLARE_WORK(binder_deferred_work
, binder_deferred_func
);
5192 binder_defer_work(struct binder_proc
*proc
, enum binder_deferred_state defer
)
5194 mutex_lock(&binder_deferred_lock
);
5195 proc
->deferred_work
|= defer
;
5196 if (hlist_unhashed(&proc
->deferred_work_node
)) {
5197 hlist_add_head(&proc
->deferred_work_node
,
5198 &binder_deferred_list
);
5199 schedule_work(&binder_deferred_work
);
5201 mutex_unlock(&binder_deferred_lock
);
5204 static void print_binder_transaction_ilocked(struct seq_file
*m
,
5205 struct binder_proc
*proc
,
5207 struct binder_transaction
*t
)
5209 struct binder_proc
*to_proc
;
5210 struct binder_buffer
*buffer
= t
->buffer
;
5212 spin_lock(&t
->lock
);
5213 to_proc
= t
->to_proc
;
5215 "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %d:%d r%d",
5216 prefix
, t
->debug_id
, t
,
5217 t
->from
? t
->from
->proc
->pid
: 0,
5218 t
->from
? t
->from
->pid
: 0,
5219 to_proc
? to_proc
->pid
: 0,
5220 t
->to_thread
? t
->to_thread
->pid
: 0,
5221 t
->code
, t
->flags
, t
->priority
.sched_policy
,
5222 t
->priority
.prio
, t
->need_reply
);
5223 spin_unlock(&t
->lock
);
5225 if (proc
!= to_proc
) {
5227 * Can only safely deref buffer if we are holding the
5228 * correct proc inner lock for this node
5234 if (buffer
== NULL
) {
5235 seq_puts(m
, " buffer free\n");
5238 if (buffer
->target_node
)
5239 seq_printf(m
, " node %d", buffer
->target_node
->debug_id
);
5240 seq_printf(m
, " size %zd:%zd data %p\n",
5241 buffer
->data_size
, buffer
->offsets_size
,
5245 static void print_binder_work_ilocked(struct seq_file
*m
,
5246 struct binder_proc
*proc
,
5248 const char *transaction_prefix
,
5249 struct binder_work
*w
)
5251 struct binder_node
*node
;
5252 struct binder_transaction
*t
;
5255 case BINDER_WORK_TRANSACTION
:
5256 t
= container_of(w
, struct binder_transaction
, work
);
5257 print_binder_transaction_ilocked(
5258 m
, proc
, transaction_prefix
, t
);
5260 case BINDER_WORK_RETURN_ERROR
: {
5261 struct binder_error
*e
= container_of(
5262 w
, struct binder_error
, work
);
5264 seq_printf(m
, "%stransaction error: %u\n",
5267 case BINDER_WORK_TRANSACTION_COMPLETE
:
5268 seq_printf(m
, "%stransaction complete\n", prefix
);
5270 case BINDER_WORK_NODE
:
5271 node
= container_of(w
, struct binder_node
, work
);
5272 seq_printf(m
, "%snode work %d: u%016llx c%016llx\n",
5273 prefix
, node
->debug_id
,
5274 (u64
)node
->ptr
, (u64
)node
->cookie
);
5276 case BINDER_WORK_DEAD_BINDER
:
5277 seq_printf(m
, "%shas dead binder\n", prefix
);
5279 case BINDER_WORK_DEAD_BINDER_AND_CLEAR
:
5280 seq_printf(m
, "%shas cleared dead binder\n", prefix
);
5282 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION
:
5283 seq_printf(m
, "%shas cleared death notification\n", prefix
);
5286 seq_printf(m
, "%sunknown work: type %d\n", prefix
, w
->type
);
5291 static void print_binder_thread_ilocked(struct seq_file
*m
,
5292 struct binder_thread
*thread
,
5295 struct binder_transaction
*t
;
5296 struct binder_work
*w
;
5297 size_t start_pos
= m
->count
;
5300 seq_printf(m
, " thread %d: l %02x need_return %d tr %d\n",
5301 thread
->pid
, thread
->looper
,
5302 thread
->looper_need_return
,
5303 atomic_read(&thread
->tmp_ref
));
5304 header_pos
= m
->count
;
5305 t
= thread
->transaction_stack
;
5307 if (t
->from
== thread
) {
5308 print_binder_transaction_ilocked(m
, thread
->proc
,
5309 " outgoing transaction", t
);
5311 } else if (t
->to_thread
== thread
) {
5312 print_binder_transaction_ilocked(m
, thread
->proc
,
5313 " incoming transaction", t
);
5316 print_binder_transaction_ilocked(m
, thread
->proc
,
5317 " bad transaction", t
);
5321 list_for_each_entry(w
, &thread
->todo
, entry
) {
5322 print_binder_work_ilocked(m
, thread
->proc
, " ",
5323 " pending transaction", w
);
5325 if (!print_always
&& m
->count
== header_pos
)
5326 m
->count
= start_pos
;
5329 static void print_binder_node_nilocked(struct seq_file
*m
,
5330 struct binder_node
*node
)
5332 struct binder_ref
*ref
;
5333 struct binder_work
*w
;
5337 hlist_for_each_entry(ref
, &node
->refs
, node_entry
)
5340 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",
5341 node
->debug_id
, (u64
)node
->ptr
, (u64
)node
->cookie
,
5342 node
->sched_policy
, node
->min_priority
,
5343 node
->has_strong_ref
, node
->has_weak_ref
,
5344 node
->local_strong_refs
, node
->local_weak_refs
,
5345 node
->internal_strong_refs
, count
, node
->tmp_refs
);
5347 seq_puts(m
, " proc");
5348 hlist_for_each_entry(ref
, &node
->refs
, node_entry
)
5349 seq_printf(m
, " %d", ref
->proc
->pid
);
5353 list_for_each_entry(w
, &node
->async_todo
, entry
)
5354 print_binder_work_ilocked(m
, node
->proc
, " ",
5355 " pending async transaction", w
);
5359 static void print_binder_ref_olocked(struct seq_file
*m
,
5360 struct binder_ref
*ref
)
5362 binder_node_lock(ref
->node
);
5363 seq_printf(m
, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
5364 ref
->data
.debug_id
, ref
->data
.desc
,
5365 ref
->node
->proc
? "" : "dead ",
5366 ref
->node
->debug_id
, ref
->data
.strong
,
5367 ref
->data
.weak
, ref
->death
);
5368 binder_node_unlock(ref
->node
);
5371 static void print_binder_proc(struct seq_file
*m
,
5372 struct binder_proc
*proc
, int print_all
)
5374 struct binder_work
*w
;
5376 size_t start_pos
= m
->count
;
5378 struct binder_node
*last_node
= NULL
;
5380 seq_printf(m
, "proc %d\n", proc
->pid
);
5381 seq_printf(m
, "context %s\n", proc
->context
->name
);
5382 header_pos
= m
->count
;
5384 binder_inner_proc_lock(proc
);
5385 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
))
5386 print_binder_thread_ilocked(m
, rb_entry(n
, struct binder_thread
,
5387 rb_node
), print_all
);
5389 for (n
= rb_first(&proc
->nodes
); n
!= NULL
; n
= rb_next(n
)) {
5390 struct binder_node
*node
= rb_entry(n
, struct binder_node
,
5393 * take a temporary reference on the node so it
5394 * survives and isn't removed from the tree
5395 * while we print it.
5397 binder_inc_node_tmpref_ilocked(node
);
5398 /* Need to drop inner lock to take node lock */
5399 binder_inner_proc_unlock(proc
);
5401 binder_put_node(last_node
);
5402 binder_node_inner_lock(node
);
5403 print_binder_node_nilocked(m
, node
);
5404 binder_node_inner_unlock(node
);
5406 binder_inner_proc_lock(proc
);
5408 binder_inner_proc_unlock(proc
);
5410 binder_put_node(last_node
);
5413 binder_proc_lock(proc
);
5414 for (n
= rb_first(&proc
->refs_by_desc
);
5417 print_binder_ref_olocked(m
, rb_entry(n
,
5420 binder_proc_unlock(proc
);
5422 binder_alloc_print_allocated(m
, &proc
->alloc
);
5423 binder_inner_proc_lock(proc
);
5424 list_for_each_entry(w
, &proc
->todo
, entry
)
5425 print_binder_work_ilocked(m
, proc
, " ",
5426 " pending transaction", w
);
5427 list_for_each_entry(w
, &proc
->delivered_death
, entry
) {
5428 seq_puts(m
, " has delivered dead binder\n");
5431 binder_inner_proc_unlock(proc
);
5432 if (!print_all
&& m
->count
== header_pos
)
5433 m
->count
= start_pos
;
5436 static const char * const binder_return_strings
[] = {
5441 "BR_ACQUIRE_RESULT",
5443 "BR_TRANSACTION_COMPLETE",
5448 "BR_ATTEMPT_ACQUIRE",
5453 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
5457 static const char * const binder_command_strings
[] = {
5460 "BC_ACQUIRE_RESULT",
5468 "BC_ATTEMPT_ACQUIRE",
5469 "BC_REGISTER_LOOPER",
5472 "BC_REQUEST_DEATH_NOTIFICATION",
5473 "BC_CLEAR_DEATH_NOTIFICATION",
5474 "BC_DEAD_BINDER_DONE",
5475 "BC_TRANSACTION_SG",
5479 static const char * const binder_objstat_strings
[] = {
5486 "transaction_complete"
5489 static void print_binder_stats(struct seq_file
*m
, const char *prefix
,
5490 struct binder_stats
*stats
)
5494 BUILD_BUG_ON(ARRAY_SIZE(stats
->bc
) !=
5495 ARRAY_SIZE(binder_command_strings
));
5496 for (i
= 0; i
< ARRAY_SIZE(stats
->bc
); i
++) {
5497 int temp
= atomic_read(&stats
->bc
[i
]);
5500 seq_printf(m
, "%s%s: %d\n", prefix
,
5501 binder_command_strings
[i
], temp
);
5504 BUILD_BUG_ON(ARRAY_SIZE(stats
->br
) !=
5505 ARRAY_SIZE(binder_return_strings
));
5506 for (i
= 0; i
< ARRAY_SIZE(stats
->br
); i
++) {
5507 int temp
= atomic_read(&stats
->br
[i
]);
5510 seq_printf(m
, "%s%s: %d\n", prefix
,
5511 binder_return_strings
[i
], temp
);
5514 BUILD_BUG_ON(ARRAY_SIZE(stats
->obj_created
) !=
5515 ARRAY_SIZE(binder_objstat_strings
));
5516 BUILD_BUG_ON(ARRAY_SIZE(stats
->obj_created
) !=
5517 ARRAY_SIZE(stats
->obj_deleted
));
5518 for (i
= 0; i
< ARRAY_SIZE(stats
->obj_created
); i
++) {
5519 int created
= atomic_read(&stats
->obj_created
[i
]);
5520 int deleted
= atomic_read(&stats
->obj_deleted
[i
]);
5522 if (created
|| deleted
)
5523 seq_printf(m
, "%s%s: active %d total %d\n",
5525 binder_objstat_strings
[i
],
5531 static void print_binder_proc_stats(struct seq_file
*m
,
5532 struct binder_proc
*proc
)
5534 struct binder_work
*w
;
5535 struct binder_thread
*thread
;
5537 int count
, strong
, weak
, ready_threads
;
5538 size_t free_async_space
=
5539 binder_alloc_get_free_async_space(&proc
->alloc
);
5541 seq_printf(m
, "proc %d\n", proc
->pid
);
5542 seq_printf(m
, "context %s\n", proc
->context
->name
);
5545 binder_inner_proc_lock(proc
);
5546 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
))
5549 list_for_each_entry(thread
, &proc
->waiting_threads
, waiting_thread_node
)
5552 seq_printf(m
, " threads: %d\n", count
);
5553 seq_printf(m
, " requested threads: %d+%d/%d\n"
5554 " ready threads %d\n"
5555 " free async space %zd\n", proc
->requested_threads
,
5556 proc
->requested_threads_started
, proc
->max_threads
,
5560 for (n
= rb_first(&proc
->nodes
); n
!= NULL
; n
= rb_next(n
))
5562 binder_inner_proc_unlock(proc
);
5563 seq_printf(m
, " nodes: %d\n", count
);
5567 binder_proc_lock(proc
);
5568 for (n
= rb_first(&proc
->refs_by_desc
); n
!= NULL
; n
= rb_next(n
)) {
5569 struct binder_ref
*ref
= rb_entry(n
, struct binder_ref
,
5572 strong
+= ref
->data
.strong
;
5573 weak
+= ref
->data
.weak
;
5575 binder_proc_unlock(proc
);
5576 seq_printf(m
, " refs: %d s %d w %d\n", count
, strong
, weak
);
5578 count
= binder_alloc_get_allocated_count(&proc
->alloc
);
5579 seq_printf(m
, " buffers: %d\n", count
);
5581 binder_alloc_print_pages(m
, &proc
->alloc
);
5584 binder_inner_proc_lock(proc
);
5585 list_for_each_entry(w
, &proc
->todo
, entry
) {
5586 if (w
->type
== BINDER_WORK_TRANSACTION
)
5589 binder_inner_proc_unlock(proc
);
5590 seq_printf(m
, " pending transactions: %d\n", count
);
5592 print_binder_stats(m
, " ", &proc
->stats
);
5596 static int binder_state_show(struct seq_file
*m
, void *unused
)
5598 struct binder_proc
*proc
;
5599 struct binder_node
*node
;
5600 struct binder_node
*last_node
= NULL
;
5602 seq_puts(m
, "binder state:\n");
5604 spin_lock(&binder_dead_nodes_lock
);
5605 if (!hlist_empty(&binder_dead_nodes
))
5606 seq_puts(m
, "dead nodes:\n");
5607 hlist_for_each_entry(node
, &binder_dead_nodes
, dead_node
) {
5609 * take a temporary reference on the node so it
5610 * survives and isn't removed from the list
5611 * while we print it.
5614 spin_unlock(&binder_dead_nodes_lock
);
5616 binder_put_node(last_node
);
5617 binder_node_lock(node
);
5618 print_binder_node_nilocked(m
, node
);
5619 binder_node_unlock(node
);
5621 spin_lock(&binder_dead_nodes_lock
);
5623 spin_unlock(&binder_dead_nodes_lock
);
5625 binder_put_node(last_node
);
5627 mutex_lock(&binder_procs_lock
);
5628 hlist_for_each_entry(proc
, &binder_procs
, proc_node
)
5629 print_binder_proc(m
, proc
, 1);
5630 mutex_unlock(&binder_procs_lock
);
5635 static int binder_stats_show(struct seq_file
*m
, void *unused
)
5637 struct binder_proc
*proc
;
5639 seq_puts(m
, "binder stats:\n");
5641 print_binder_stats(m
, "", &binder_stats
);
5643 mutex_lock(&binder_procs_lock
);
5644 hlist_for_each_entry(proc
, &binder_procs
, proc_node
)
5645 print_binder_proc_stats(m
, proc
);
5646 mutex_unlock(&binder_procs_lock
);
5651 static int binder_transactions_show(struct seq_file
*m
, void *unused
)
5653 struct binder_proc
*proc
;
5655 seq_puts(m
, "binder transactions:\n");
5656 mutex_lock(&binder_procs_lock
);
5657 hlist_for_each_entry(proc
, &binder_procs
, proc_node
)
5658 print_binder_proc(m
, proc
, 0);
5659 mutex_unlock(&binder_procs_lock
);
5664 static int binder_proc_show(struct seq_file
*m
, void *unused
)
5666 struct binder_proc
*itr
;
5667 int pid
= (unsigned long)m
->private;
5669 mutex_lock(&binder_procs_lock
);
5670 hlist_for_each_entry(itr
, &binder_procs
, proc_node
) {
5671 if (itr
->pid
== pid
) {
5672 seq_puts(m
, "binder proc state:\n");
5673 print_binder_proc(m
, itr
, 1);
5676 mutex_unlock(&binder_procs_lock
);
5681 static void print_binder_transaction_log_entry(struct seq_file
*m
,
5682 struct binder_transaction_log_entry
*e
)
5684 int debug_id
= READ_ONCE(e
->debug_id_done
);
5686 * read barrier to guarantee debug_id_done read before
5687 * we print the log values
5691 "%d: %s from %d:%d to %d:%d context %s node %d handle %d size %d:%d ret %d/%d l=%d",
5692 e
->debug_id
, (e
->call_type
== 2) ? "reply" :
5693 ((e
->call_type
== 1) ? "async" : "call "), e
->from_proc
,
5694 e
->from_thread
, e
->to_proc
, e
->to_thread
, e
->context_name
,
5695 e
->to_node
, e
->target_handle
, e
->data_size
, e
->offsets_size
,
5696 e
->return_error
, e
->return_error_param
,
5697 e
->return_error_line
);
5699 * read-barrier to guarantee read of debug_id_done after
5700 * done printing the fields of the entry
5703 seq_printf(m
, debug_id
&& debug_id
== READ_ONCE(e
->debug_id_done
) ?
5704 "\n" : " (incomplete)\n");
5707 static int binder_transaction_log_show(struct seq_file
*m
, void *unused
)
5709 struct binder_transaction_log
*log
= m
->private;
5710 unsigned int log_cur
= atomic_read(&log
->cur
);
5715 count
= log_cur
+ 1;
5716 cur
= count
< ARRAY_SIZE(log
->entry
) && !log
->full
?
5717 0 : count
% ARRAY_SIZE(log
->entry
);
5718 if (count
> ARRAY_SIZE(log
->entry
) || log
->full
)
5719 count
= ARRAY_SIZE(log
->entry
);
5720 for (i
= 0; i
< count
; i
++) {
5721 unsigned int index
= cur
++ % ARRAY_SIZE(log
->entry
);
5723 print_binder_transaction_log_entry(m
, &log
->entry
[index
]);
5728 static const struct file_operations binder_fops
= {
5729 .owner
= THIS_MODULE
,
5730 .poll
= binder_poll
,
5731 .unlocked_ioctl
= binder_ioctl
,
5732 .compat_ioctl
= binder_ioctl
,
5733 .mmap
= binder_mmap
,
5734 .open
= binder_open
,
5735 .flush
= binder_flush
,
5736 .release
= binder_release
,
5739 BINDER_DEBUG_ENTRY(state
);
5740 BINDER_DEBUG_ENTRY(stats
);
5741 BINDER_DEBUG_ENTRY(transactions
);
5742 BINDER_DEBUG_ENTRY(transaction_log
);
5744 static int __init
init_binder_device(const char *name
)
5747 struct binder_device
*binder_device
;
5749 binder_device
= kzalloc(sizeof(*binder_device
), GFP_KERNEL
);
5753 binder_device
->miscdev
.fops
= &binder_fops
;
5754 binder_device
->miscdev
.minor
= MISC_DYNAMIC_MINOR
;
5755 binder_device
->miscdev
.name
= name
;
5757 binder_device
->context
.binder_context_mgr_uid
= INVALID_UID
;
5758 binder_device
->context
.name
= name
;
5759 mutex_init(&binder_device
->context
.context_mgr_node_lock
);
5761 ret
= misc_register(&binder_device
->miscdev
);
5763 kfree(binder_device
);
5767 hlist_add_head(&binder_device
->hlist
, &binder_devices
);
5772 static int __init
binder_init(void)
5775 char *device_name
, *device_names
, *device_tmp
;
5776 struct binder_device
*device
;
5777 struct hlist_node
*tmp
;
5779 binder_alloc_shrinker_init();
5781 atomic_set(&binder_transaction_log
.cur
, ~0U);
5782 atomic_set(&binder_transaction_log_failed
.cur
, ~0U);
5784 binder_debugfs_dir_entry_root
= debugfs_create_dir("binder", NULL
);
5785 if (binder_debugfs_dir_entry_root
)
5786 binder_debugfs_dir_entry_proc
= debugfs_create_dir("proc",
5787 binder_debugfs_dir_entry_root
);
5789 if (binder_debugfs_dir_entry_root
) {
5790 debugfs_create_file("state",
5792 binder_debugfs_dir_entry_root
,
5794 &binder_state_fops
);
5795 debugfs_create_file("stats",
5797 binder_debugfs_dir_entry_root
,
5799 &binder_stats_fops
);
5800 debugfs_create_file("transactions",
5802 binder_debugfs_dir_entry_root
,
5804 &binder_transactions_fops
);
5805 debugfs_create_file("transaction_log",
5807 binder_debugfs_dir_entry_root
,
5808 &binder_transaction_log
,
5809 &binder_transaction_log_fops
);
5810 debugfs_create_file("failed_transaction_log",
5812 binder_debugfs_dir_entry_root
,
5813 &binder_transaction_log_failed
,
5814 &binder_transaction_log_fops
);
5818 * Copy the module_parameter string, because we don't want to
5819 * tokenize it in-place.
5821 device_names
= kzalloc(strlen(binder_devices_param
) + 1, GFP_KERNEL
);
5822 if (!device_names
) {
5824 goto err_alloc_device_names_failed
;
5826 strcpy(device_names
, binder_devices_param
);
5828 device_tmp
= device_names
;
5829 while ((device_name
= strsep(&device_tmp
, ","))) {
5830 ret
= init_binder_device(device_name
);
5832 goto err_init_binder_device_failed
;
5837 err_init_binder_device_failed
:
5838 hlist_for_each_entry_safe(device
, tmp
, &binder_devices
, hlist
) {
5839 misc_deregister(&device
->miscdev
);
5840 hlist_del(&device
->hlist
);
5844 kfree(device_names
);
5846 err_alloc_device_names_failed
:
5847 debugfs_remove_recursive(binder_debugfs_dir_entry_root
);
5852 device_initcall(binder_init
);
5854 #define CREATE_TRACE_POINTS
5855 #include "binder_trace.h"
5857 MODULE_LICENSE("GPL v2");