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.h>
68 #include <linux/seq_file.h>
69 #include <linux/uaccess.h>
70 #include <linux/pid_namespace.h>
71 #include <linux/security.h>
72 #include <linux/spinlock.h>
74 #include <linux/sched/rt.h>
77 #define NICE_WIDTH (MAX_NICE - MIN_NICE + 1)
78 #define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
79 #define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
81 #ifdef CONFIG_ANDROID_BINDER_IPC_32BIT
82 #define BINDER_IPC_32BIT 1
85 #include <uapi/linux/android/binder.h>
86 #include "binder_alloc.h"
87 #include "binder_trace.h"
89 static HLIST_HEAD(binder_deferred_list
);
90 static DEFINE_MUTEX(binder_deferred_lock
);
92 static HLIST_HEAD(binder_devices
);
93 static HLIST_HEAD(binder_procs
);
94 static DEFINE_MUTEX(binder_procs_lock
);
96 static HLIST_HEAD(binder_dead_nodes
);
97 static DEFINE_SPINLOCK(binder_dead_nodes_lock
);
99 static struct dentry
*binder_debugfs_dir_entry_root
;
100 static struct dentry
*binder_debugfs_dir_entry_proc
;
101 static atomic_t binder_last_id
;
102 static struct workqueue_struct
*binder_deferred_workqueue
;
104 #define BINDER_DEBUG_ENTRY(name) \
105 static int binder_##name##_open(struct inode *inode, struct file *file) \
107 return single_open(file, binder_##name##_show, inode->i_private); \
110 static const struct file_operations binder_##name##_fops = { \
111 .owner = THIS_MODULE, \
112 .open = binder_##name##_open, \
114 .llseek = seq_lseek, \
115 .release = single_release, \
118 static int binder_proc_show(struct seq_file
*m
, void *unused
);
119 BINDER_DEBUG_ENTRY(proc
);
121 /* This is only defined in include/asm-arm/sizes.h */
127 #define SZ_4M 0x400000
130 #define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
132 #define BINDER_SMALL_BUF_SIZE (PAGE_SIZE * 64)
135 BINDER_DEBUG_USER_ERROR
= 1U << 0,
136 BINDER_DEBUG_FAILED_TRANSACTION
= 1U << 1,
137 BINDER_DEBUG_DEAD_TRANSACTION
= 1U << 2,
138 BINDER_DEBUG_OPEN_CLOSE
= 1U << 3,
139 BINDER_DEBUG_DEAD_BINDER
= 1U << 4,
140 BINDER_DEBUG_DEATH_NOTIFICATION
= 1U << 5,
141 BINDER_DEBUG_READ_WRITE
= 1U << 6,
142 BINDER_DEBUG_USER_REFS
= 1U << 7,
143 BINDER_DEBUG_THREADS
= 1U << 8,
144 BINDER_DEBUG_TRANSACTION
= 1U << 9,
145 BINDER_DEBUG_TRANSACTION_COMPLETE
= 1U << 10,
146 BINDER_DEBUG_FREE_BUFFER
= 1U << 11,
147 BINDER_DEBUG_INTERNAL_REFS
= 1U << 12,
148 BINDER_DEBUG_PRIORITY_CAP
= 1U << 13,
149 BINDER_DEBUG_SPINLOCKS
= 1U << 14,
151 static uint32_t binder_debug_mask
;
153 module_param_named(debug_mask
, binder_debug_mask
, uint
, S_IWUSR
| S_IRUGO
);
155 static char *binder_devices_param
= CONFIG_ANDROID_BINDER_DEVICES
;
156 module_param_named(devices
, binder_devices_param
, charp
, S_IRUGO
);
158 static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait
);
159 static int binder_stop_on_user_error
;
161 static int binder_set_stop_on_user_error(const char *val
,
162 struct kernel_param
*kp
)
166 ret
= param_set_int(val
, kp
);
167 if (binder_stop_on_user_error
< 2)
168 wake_up(&binder_user_error_wait
);
171 module_param_call(stop_on_user_error
, binder_set_stop_on_user_error
,
172 param_get_int
, &binder_stop_on_user_error
, S_IWUSR
| S_IRUGO
);
174 #define binder_debug(mask, x...) \
176 if (binder_debug_mask & mask) \
180 #define binder_user_error(x...) \
182 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
184 if (binder_stop_on_user_error) \
185 binder_stop_on_user_error = 2; \
188 #define to_flat_binder_object(hdr) \
189 container_of(hdr, struct flat_binder_object, hdr)
191 #define to_binder_fd_object(hdr) container_of(hdr, struct binder_fd_object, hdr)
193 #define to_binder_buffer_object(hdr) \
194 container_of(hdr, struct binder_buffer_object, hdr)
196 #define to_binder_fd_array_object(hdr) \
197 container_of(hdr, struct binder_fd_array_object, hdr)
199 enum binder_stat_types
{
205 BINDER_STAT_TRANSACTION
,
206 BINDER_STAT_TRANSACTION_COMPLETE
,
210 struct binder_stats
{
211 atomic_t br
[_IOC_NR(BR_FAILED_REPLY
) + 1];
212 atomic_t bc
[_IOC_NR(BC_REPLY_SG
) + 1];
213 atomic_t obj_created
[BINDER_STAT_COUNT
];
214 atomic_t obj_deleted
[BINDER_STAT_COUNT
];
217 static struct binder_stats binder_stats
;
219 static inline void binder_stats_deleted(enum binder_stat_types type
)
221 atomic_inc(&binder_stats
.obj_deleted
[type
]);
224 static inline void binder_stats_created(enum binder_stat_types type
)
226 atomic_inc(&binder_stats
.obj_created
[type
]);
229 struct binder_transaction_log_entry
{
241 int return_error_line
;
242 uint32_t return_error
;
243 uint32_t return_error_param
;
244 const char *context_name
;
246 struct binder_transaction_log
{
249 struct binder_transaction_log_entry entry
[32];
251 static struct binder_transaction_log binder_transaction_log
;
252 static struct binder_transaction_log binder_transaction_log_failed
;
254 static struct binder_transaction_log_entry
*binder_transaction_log_add(
255 struct binder_transaction_log
*log
)
257 struct binder_transaction_log_entry
*e
;
258 unsigned int cur
= atomic_inc_return(&log
->cur
);
260 if (cur
>= ARRAY_SIZE(log
->entry
))
262 e
= &log
->entry
[cur
% ARRAY_SIZE(log
->entry
)];
263 WRITE_ONCE(e
->debug_id_done
, 0);
265 * write-barrier to synchronize access to e->debug_id_done.
266 * We make sure the initialized 0 value is seen before
267 * memset() other fields are zeroed by memset.
270 memset(e
, 0, sizeof(*e
));
274 struct binder_context
{
275 struct binder_node
*binder_context_mgr_node
;
276 struct mutex context_mgr_node_lock
;
278 kuid_t binder_context_mgr_uid
;
282 struct binder_device
{
283 struct hlist_node hlist
;
284 struct miscdevice miscdev
;
285 struct binder_context context
;
289 * struct binder_work - work enqueued on a worklist
290 * @entry: node enqueued on list
291 * @type: type of work to be performed
293 * There are separate work lists for proc, thread, and node (async).
296 struct list_head entry
;
299 BINDER_WORK_TRANSACTION
= 1,
300 BINDER_WORK_TRANSACTION_COMPLETE
,
301 BINDER_WORK_RETURN_ERROR
,
303 BINDER_WORK_DEAD_BINDER
,
304 BINDER_WORK_DEAD_BINDER_AND_CLEAR
,
305 BINDER_WORK_CLEAR_DEATH_NOTIFICATION
,
309 struct binder_error
{
310 struct binder_work work
;
315 * struct binder_node - binder node bookkeeping
316 * @debug_id: unique ID for debugging
317 * (invariant after initialized)
318 * @lock: lock for node fields
319 * @work: worklist element for node work
320 * (protected by @proc->inner_lock)
321 * @rb_node: element for proc->nodes tree
322 * (protected by @proc->inner_lock)
323 * @dead_node: element for binder_dead_nodes list
324 * (protected by binder_dead_nodes_lock)
325 * @proc: binder_proc that owns this node
326 * (invariant after initialized)
327 * @refs: list of references on this node
328 * (protected by @lock)
329 * @internal_strong_refs: used to take strong references when
330 * initiating a transaction
331 * (protected by @proc->inner_lock if @proc
333 * @local_weak_refs: weak user refs from local process
334 * (protected by @proc->inner_lock if @proc
336 * @local_strong_refs: strong user refs from local process
337 * (protected by @proc->inner_lock if @proc
339 * @tmp_refs: temporary kernel refs
340 * (protected by @proc->inner_lock while @proc
341 * is valid, and by binder_dead_nodes_lock
342 * if @proc is NULL. During inc/dec and node release
343 * it is also protected by @lock to provide safety
344 * as the node dies and @proc becomes NULL)
345 * @ptr: userspace pointer for node
346 * (invariant, no lock needed)
347 * @cookie: userspace cookie for node
348 * (invariant, no lock needed)
349 * @has_strong_ref: userspace notified of strong ref
350 * (protected by @proc->inner_lock if @proc
352 * @pending_strong_ref: userspace has acked notification of strong ref
353 * (protected by @proc->inner_lock if @proc
355 * @has_weak_ref: userspace notified of weak ref
356 * (protected by @proc->inner_lock if @proc
358 * @pending_weak_ref: userspace has acked notification of weak ref
359 * (protected by @proc->inner_lock if @proc
361 * @has_async_transaction: async transaction to node in progress
362 * (protected by @lock)
363 * @sched_policy: minimum scheduling policy for node
364 * (invariant after initialized)
365 * @accept_fds: file descriptor operations supported for node
366 * (invariant after initialized)
367 * @min_priority: minimum scheduling priority
368 * (invariant after initialized)
369 * @inherit_rt: inherit RT scheduling policy from caller
370 * (invariant after initialized)
371 * @async_todo: list of async work items
372 * (protected by @proc->inner_lock)
374 * Bookkeeping structure for binder nodes.
379 struct binder_work work
;
381 struct rb_node rb_node
;
382 struct hlist_node dead_node
;
384 struct binder_proc
*proc
;
385 struct hlist_head refs
;
386 int internal_strong_refs
;
388 int local_strong_refs
;
390 binder_uintptr_t ptr
;
391 binder_uintptr_t cookie
;
394 * bitfield elements protected by
398 u8 pending_strong_ref
:1;
400 u8 pending_weak_ref
:1;
404 * invariant after initialization
411 bool has_async_transaction
;
412 struct list_head async_todo
;
415 struct binder_ref_death
{
417 * @work: worklist element for death notifications
418 * (protected by inner_lock of the proc that
419 * this ref belongs to)
421 struct binder_work work
;
422 binder_uintptr_t cookie
;
426 * struct binder_ref_data - binder_ref counts and id
427 * @debug_id: unique ID for the ref
428 * @desc: unique userspace handle for ref
429 * @strong: strong ref count (debugging only if not locked)
430 * @weak: weak ref count (debugging only if not locked)
432 * Structure to hold ref count and ref id information. Since
433 * the actual ref can only be accessed with a lock, this structure
434 * is used to return information about the ref to callers of
435 * ref inc/dec functions.
437 struct binder_ref_data
{
445 * struct binder_ref - struct to track references on nodes
446 * @data: binder_ref_data containing id, handle, and current refcounts
447 * @rb_node_desc: node for lookup by @data.desc in proc's rb_tree
448 * @rb_node_node: node for lookup by @node in proc's rb_tree
449 * @node_entry: list entry for node->refs list in target node
450 * (protected by @node->lock)
451 * @proc: binder_proc containing ref
452 * @node: binder_node of target node. When cleaning up a
453 * ref for deletion in binder_cleanup_ref, a non-NULL
454 * @node indicates the node must be freed
455 * @death: pointer to death notification (ref_death) if requested
456 * (protected by @node->lock)
458 * Structure to track references from procA to target node (on procB). This
459 * structure is unsafe to access without holding @proc->outer_lock.
462 /* Lookups needed: */
463 /* node + proc => ref (transaction) */
464 /* desc + proc => ref (transaction, inc/dec ref) */
465 /* node => refs + procs (proc exit) */
466 struct binder_ref_data data
;
467 struct rb_node rb_node_desc
;
468 struct rb_node rb_node_node
;
469 struct hlist_node node_entry
;
470 struct binder_proc
*proc
;
471 struct binder_node
*node
;
472 struct binder_ref_death
*death
;
475 enum binder_deferred_state
{
476 BINDER_DEFERRED_PUT_FILES
= 0x01,
477 BINDER_DEFERRED_FLUSH
= 0x02,
478 BINDER_DEFERRED_RELEASE
= 0x04,
482 * struct binder_priority - scheduler policy and priority
483 * @sched_policy scheduler policy
484 * @prio [100..139] for SCHED_NORMAL, [0..99] for FIFO/RT
486 * The binder driver supports inheriting the following scheduler policies:
492 struct binder_priority
{
493 unsigned int sched_policy
;
498 * struct binder_proc - binder process bookkeeping
499 * @proc_node: element for binder_procs list
500 * @threads: rbtree of binder_threads in this proc
501 * (protected by @inner_lock)
502 * @nodes: rbtree of binder nodes associated with
503 * this proc ordered by node->ptr
504 * (protected by @inner_lock)
505 * @refs_by_desc: rbtree of refs ordered by ref->desc
506 * (protected by @outer_lock)
507 * @refs_by_node: rbtree of refs ordered by ref->node
508 * (protected by @outer_lock)
509 * @waiting_threads: threads currently waiting for proc work
510 * (protected by @inner_lock)
511 * @pid PID of group_leader of process
512 * (invariant after initialized)
513 * @tsk task_struct for group_leader of process
514 * (invariant after initialized)
515 * @files files_struct for process
516 * (invariant after initialized)
517 * @deferred_work_node: element for binder_deferred_list
518 * (protected by binder_deferred_lock)
519 * @deferred_work: bitmap of deferred work to perform
520 * (protected by binder_deferred_lock)
521 * @is_dead: process is dead and awaiting free
522 * when outstanding transactions are cleaned up
523 * (protected by @inner_lock)
524 * @todo: list of work for this process
525 * (protected by @inner_lock)
526 * @stats: per-process binder statistics
527 * (atomics, no lock needed)
528 * @delivered_death: list of delivered death notification
529 * (protected by @inner_lock)
530 * @max_threads: cap on number of binder threads
531 * (protected by @inner_lock)
532 * @requested_threads: number of binder threads requested but not
533 * yet started. In current implementation, can
535 * (protected by @inner_lock)
536 * @requested_threads_started: number binder threads started
537 * (protected by @inner_lock)
538 * @tmp_ref: temporary reference to indicate proc is in use
539 * (protected by @inner_lock)
540 * @default_priority: default scheduler priority
541 * (invariant after initialized)
542 * @debugfs_entry: debugfs node
543 * @alloc: binder allocator bookkeeping
544 * @context: binder_context for this proc
545 * (invariant after initialized)
546 * @inner_lock: can nest under outer_lock and/or node lock
547 * @outer_lock: no nesting under innor or node lock
548 * Lock order: 1) outer, 2) node, 3) inner
550 * Bookkeeping structure for binder processes
553 struct hlist_node proc_node
;
554 struct rb_root threads
;
555 struct rb_root nodes
;
556 struct rb_root refs_by_desc
;
557 struct rb_root refs_by_node
;
558 struct list_head waiting_threads
;
560 struct task_struct
*tsk
;
561 struct files_struct
*files
;
562 struct hlist_node deferred_work_node
;
566 struct list_head todo
;
567 struct binder_stats stats
;
568 struct list_head delivered_death
;
570 int requested_threads
;
571 int requested_threads_started
;
573 struct binder_priority default_priority
;
574 struct dentry
*debugfs_entry
;
575 struct binder_alloc alloc
;
576 struct binder_context
*context
;
577 spinlock_t inner_lock
;
578 spinlock_t outer_lock
;
582 BINDER_LOOPER_STATE_REGISTERED
= 0x01,
583 BINDER_LOOPER_STATE_ENTERED
= 0x02,
584 BINDER_LOOPER_STATE_EXITED
= 0x04,
585 BINDER_LOOPER_STATE_INVALID
= 0x08,
586 BINDER_LOOPER_STATE_WAITING
= 0x10,
587 BINDER_LOOPER_STATE_POLL
= 0x20,
591 * struct binder_thread - binder thread bookkeeping
592 * @proc: binder process for this thread
593 * (invariant after initialization)
594 * @rb_node: element for proc->threads rbtree
595 * (protected by @proc->inner_lock)
596 * @waiting_thread_node: element for @proc->waiting_threads list
597 * (protected by @proc->inner_lock)
598 * @pid: PID for this thread
599 * (invariant after initialization)
600 * @looper: bitmap of looping state
601 * (only accessed by this thread)
602 * @looper_needs_return: looping thread needs to exit driver
604 * @transaction_stack: stack of in-progress transactions for this thread
605 * (protected by @proc->inner_lock)
606 * @todo: list of work to do for this thread
607 * (protected by @proc->inner_lock)
608 * @process_todo: whether work in @todo should be processed
609 * (protected by @proc->inner_lock)
610 * @return_error: transaction errors reported by this thread
611 * (only accessed by this thread)
612 * @reply_error: transaction errors reported by target thread
613 * (protected by @proc->inner_lock)
614 * @wait: wait queue for thread work
615 * @stats: per-thread statistics
616 * (atomics, no lock needed)
617 * @tmp_ref: temporary reference to indicate thread is in use
618 * (atomic since @proc->inner_lock cannot
619 * always be acquired)
620 * @is_dead: thread is dead and awaiting free
621 * when outstanding transactions are cleaned up
622 * (protected by @proc->inner_lock)
623 * @task: struct task_struct for this thread
625 * Bookkeeping structure for binder threads.
627 struct binder_thread
{
628 struct binder_proc
*proc
;
629 struct rb_node rb_node
;
630 struct list_head waiting_thread_node
;
632 int looper
; /* only modified by this thread */
633 bool looper_need_return
; /* can be written by other thread */
634 struct binder_transaction
*transaction_stack
;
635 struct list_head todo
;
637 struct binder_error return_error
;
638 struct binder_error reply_error
;
639 wait_queue_head_t wait
;
640 struct binder_stats stats
;
643 struct task_struct
*task
;
646 struct binder_transaction
{
648 struct binder_work work
;
649 struct binder_thread
*from
;
650 struct binder_transaction
*from_parent
;
651 struct binder_proc
*to_proc
;
652 struct binder_thread
*to_thread
;
653 struct binder_transaction
*to_parent
;
654 unsigned need_reply
:1;
655 /* unsigned is_dead:1; */ /* not used at the moment */
657 struct binder_buffer
*buffer
;
660 struct binder_priority priority
;
661 struct binder_priority saved_priority
;
662 bool set_priority_called
;
665 * @lock: protects @from, @to_proc, and @to_thread
667 * @from, @to_proc, and @to_thread can be set to NULL
668 * during thread teardown
674 * binder_proc_lock() - Acquire outer lock for given binder_proc
675 * @proc: struct binder_proc to acquire
677 * Acquires proc->outer_lock. Used to protect binder_ref
678 * structures associated with the given proc.
680 #define binder_proc_lock(proc) _binder_proc_lock(proc, __LINE__)
682 _binder_proc_lock(struct binder_proc
*proc
, int line
)
684 binder_debug(BINDER_DEBUG_SPINLOCKS
,
685 "%s: line=%d\n", __func__
, line
);
686 spin_lock(&proc
->outer_lock
);
690 * binder_proc_unlock() - Release spinlock for given binder_proc
691 * @proc: struct binder_proc to acquire
693 * Release lock acquired via binder_proc_lock()
695 #define binder_proc_unlock(_proc) _binder_proc_unlock(_proc, __LINE__)
697 _binder_proc_unlock(struct binder_proc
*proc
, int line
)
699 binder_debug(BINDER_DEBUG_SPINLOCKS
,
700 "%s: line=%d\n", __func__
, line
);
701 spin_unlock(&proc
->outer_lock
);
705 * binder_inner_proc_lock() - Acquire inner lock for given binder_proc
706 * @proc: struct binder_proc to acquire
708 * Acquires proc->inner_lock. Used to protect todo lists
710 #define binder_inner_proc_lock(proc) _binder_inner_proc_lock(proc, __LINE__)
712 _binder_inner_proc_lock(struct binder_proc
*proc
, int line
)
714 binder_debug(BINDER_DEBUG_SPINLOCKS
,
715 "%s: line=%d\n", __func__
, line
);
716 spin_lock(&proc
->inner_lock
);
720 * binder_inner_proc_unlock() - Release inner lock for given binder_proc
721 * @proc: struct binder_proc to acquire
723 * Release lock acquired via binder_inner_proc_lock()
725 #define binder_inner_proc_unlock(proc) _binder_inner_proc_unlock(proc, __LINE__)
727 _binder_inner_proc_unlock(struct binder_proc
*proc
, int line
)
729 binder_debug(BINDER_DEBUG_SPINLOCKS
,
730 "%s: line=%d\n", __func__
, line
);
731 spin_unlock(&proc
->inner_lock
);
735 * binder_node_lock() - Acquire spinlock for given binder_node
736 * @node: struct binder_node to acquire
738 * Acquires node->lock. Used to protect binder_node fields
740 #define binder_node_lock(node) _binder_node_lock(node, __LINE__)
742 _binder_node_lock(struct binder_node
*node
, int line
)
744 binder_debug(BINDER_DEBUG_SPINLOCKS
,
745 "%s: line=%d\n", __func__
, line
);
746 spin_lock(&node
->lock
);
750 * binder_node_unlock() - Release spinlock for given binder_proc
751 * @node: struct binder_node to acquire
753 * Release lock acquired via binder_node_lock()
755 #define binder_node_unlock(node) _binder_node_unlock(node, __LINE__)
757 _binder_node_unlock(struct binder_node
*node
, int line
)
759 binder_debug(BINDER_DEBUG_SPINLOCKS
,
760 "%s: line=%d\n", __func__
, line
);
761 spin_unlock(&node
->lock
);
765 * binder_node_inner_lock() - Acquire node and inner locks
766 * @node: struct binder_node to acquire
768 * Acquires node->lock. If node->proc also acquires
769 * proc->inner_lock. Used to protect binder_node fields
771 #define binder_node_inner_lock(node) _binder_node_inner_lock(node, __LINE__)
773 _binder_node_inner_lock(struct binder_node
*node
, int line
)
775 binder_debug(BINDER_DEBUG_SPINLOCKS
,
776 "%s: line=%d\n", __func__
, line
);
777 spin_lock(&node
->lock
);
779 binder_inner_proc_lock(node
->proc
);
783 * binder_node_unlock() - Release node and inner locks
784 * @node: struct binder_node to acquire
786 * Release lock acquired via binder_node_lock()
788 #define binder_node_inner_unlock(node) _binder_node_inner_unlock(node, __LINE__)
790 _binder_node_inner_unlock(struct binder_node
*node
, int line
)
792 struct binder_proc
*proc
= node
->proc
;
794 binder_debug(BINDER_DEBUG_SPINLOCKS
,
795 "%s: line=%d\n", __func__
, line
);
797 binder_inner_proc_unlock(proc
);
798 spin_unlock(&node
->lock
);
801 static bool binder_worklist_empty_ilocked(struct list_head
*list
)
803 return list_empty(list
);
807 * binder_worklist_empty() - Check if no items on the work list
808 * @proc: binder_proc associated with list
809 * @list: list to check
811 * Return: true if there are no items on list, else false
813 static bool binder_worklist_empty(struct binder_proc
*proc
,
814 struct list_head
*list
)
818 binder_inner_proc_lock(proc
);
819 ret
= binder_worklist_empty_ilocked(list
);
820 binder_inner_proc_unlock(proc
);
825 * binder_enqueue_work_ilocked() - Add an item to the work list
826 * @work: struct binder_work to add to list
827 * @target_list: list to add work to
829 * Adds the work to the specified list. Asserts that work
830 * is not already on a list.
832 * Requires the proc->inner_lock to be held.
835 binder_enqueue_work_ilocked(struct binder_work
*work
,
836 struct list_head
*target_list
)
838 BUG_ON(target_list
== NULL
);
839 BUG_ON(work
->entry
.next
&& !list_empty(&work
->entry
));
840 list_add_tail(&work
->entry
, target_list
);
844 * binder_enqueue_deferred_thread_work_ilocked() - Add deferred thread work
845 * @thread: thread to queue work to
846 * @work: struct binder_work to add to list
848 * Adds the work to the todo list of the thread. Doesn't set the process_todo
849 * flag, which means that (if it wasn't already set) the thread will go to
850 * sleep without handling this work when it calls read.
852 * Requires the proc->inner_lock to be held.
855 binder_enqueue_deferred_thread_work_ilocked(struct binder_thread
*thread
,
856 struct binder_work
*work
)
858 binder_enqueue_work_ilocked(work
, &thread
->todo
);
862 * binder_enqueue_thread_work_ilocked() - Add an item to the thread work list
863 * @thread: thread to queue work to
864 * @work: struct binder_work to add to list
866 * Adds the work to the todo list of the thread, and enables processing
869 * Requires the proc->inner_lock to be held.
872 binder_enqueue_thread_work_ilocked(struct binder_thread
*thread
,
873 struct binder_work
*work
)
875 binder_enqueue_work_ilocked(work
, &thread
->todo
);
876 thread
->process_todo
= true;
880 * binder_enqueue_thread_work() - Add an item to the thread work list
881 * @thread: thread to queue work to
882 * @work: struct binder_work to add to list
884 * Adds the work to the todo list of the thread, and enables processing
888 binder_enqueue_thread_work(struct binder_thread
*thread
,
889 struct binder_work
*work
)
891 binder_inner_proc_lock(thread
->proc
);
892 binder_enqueue_thread_work_ilocked(thread
, work
);
893 binder_inner_proc_unlock(thread
->proc
);
897 binder_dequeue_work_ilocked(struct binder_work
*work
)
899 list_del_init(&work
->entry
);
903 * binder_dequeue_work() - Removes an item from the work list
904 * @proc: binder_proc associated with list
905 * @work: struct binder_work to remove from list
907 * Removes the specified work item from whatever list it is on.
908 * Can safely be called if work is not on any list.
911 binder_dequeue_work(struct binder_proc
*proc
, struct binder_work
*work
)
913 binder_inner_proc_lock(proc
);
914 binder_dequeue_work_ilocked(work
);
915 binder_inner_proc_unlock(proc
);
918 static struct binder_work
*binder_dequeue_work_head_ilocked(
919 struct list_head
*list
)
921 struct binder_work
*w
;
923 w
= list_first_entry_or_null(list
, struct binder_work
, entry
);
925 list_del_init(&w
->entry
);
930 * binder_dequeue_work_head() - Dequeues the item at head of list
931 * @proc: binder_proc associated with list
932 * @list: list to dequeue head
934 * Removes the head of the list if there are items on the list
936 * Return: pointer dequeued binder_work, NULL if list was empty
938 static struct binder_work
*binder_dequeue_work_head(
939 struct binder_proc
*proc
,
940 struct list_head
*list
)
942 struct binder_work
*w
;
944 binder_inner_proc_lock(proc
);
945 w
= binder_dequeue_work_head_ilocked(list
);
946 binder_inner_proc_unlock(proc
);
951 binder_defer_work(struct binder_proc
*proc
, enum binder_deferred_state defer
);
952 static void binder_free_thread(struct binder_thread
*thread
);
953 static void binder_free_proc(struct binder_proc
*proc
);
954 static void binder_inc_node_tmpref_ilocked(struct binder_node
*node
);
956 static int task_get_unused_fd_flags(struct binder_proc
*proc
, int flags
)
958 struct files_struct
*files
= proc
->files
;
959 unsigned long rlim_cur
;
965 if (!lock_task_sighand(proc
->tsk
, &irqs
))
968 rlim_cur
= task_rlimit(proc
->tsk
, RLIMIT_NOFILE
);
969 unlock_task_sighand(proc
->tsk
, &irqs
);
971 return __alloc_fd(files
, 0, rlim_cur
, flags
);
975 * copied from fd_install
977 static void task_fd_install(
978 struct binder_proc
*proc
, unsigned int fd
, struct file
*file
)
981 __fd_install(proc
->files
, fd
, file
);
985 * copied from sys_close
987 static long task_close_fd(struct binder_proc
*proc
, unsigned int fd
)
991 if (proc
->files
== NULL
)
994 retval
= __close_fd(proc
->files
, fd
);
995 /* can't restart close syscall because file table entry was cleared */
996 if (unlikely(retval
== -ERESTARTSYS
||
997 retval
== -ERESTARTNOINTR
||
998 retval
== -ERESTARTNOHAND
||
999 retval
== -ERESTART_RESTARTBLOCK
))
1005 static bool binder_has_work_ilocked(struct binder_thread
*thread
,
1008 return thread
->process_todo
||
1009 thread
->looper_need_return
||
1011 !binder_worklist_empty_ilocked(&thread
->proc
->todo
));
1014 static bool binder_has_work(struct binder_thread
*thread
, bool do_proc_work
)
1018 binder_inner_proc_lock(thread
->proc
);
1019 has_work
= binder_has_work_ilocked(thread
, do_proc_work
);
1020 binder_inner_proc_unlock(thread
->proc
);
1025 static bool binder_available_for_proc_work_ilocked(struct binder_thread
*thread
)
1027 return !thread
->transaction_stack
&&
1028 binder_worklist_empty_ilocked(&thread
->todo
) &&
1029 (thread
->looper
& (BINDER_LOOPER_STATE_ENTERED
|
1030 BINDER_LOOPER_STATE_REGISTERED
));
1033 static void binder_wakeup_poll_threads_ilocked(struct binder_proc
*proc
,
1037 struct binder_thread
*thread
;
1039 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
)) {
1040 thread
= rb_entry(n
, struct binder_thread
, rb_node
);
1041 if (thread
->looper
& BINDER_LOOPER_STATE_POLL
&&
1042 binder_available_for_proc_work_ilocked(thread
)) {
1044 wake_up_interruptible_sync(&thread
->wait
);
1046 wake_up_interruptible(&thread
->wait
);
1052 * binder_select_thread_ilocked() - selects a thread for doing proc work.
1053 * @proc: process to select a thread from
1055 * Note that calling this function moves the thread off the waiting_threads
1056 * list, so it can only be woken up by the caller of this function, or a
1057 * signal. Therefore, callers *should* always wake up the thread this function
1060 * Return: If there's a thread currently waiting for process work,
1061 * returns that thread. Otherwise returns NULL.
1063 static struct binder_thread
*
1064 binder_select_thread_ilocked(struct binder_proc
*proc
)
1066 struct binder_thread
*thread
;
1068 assert_spin_locked(&proc
->inner_lock
);
1069 thread
= list_first_entry_or_null(&proc
->waiting_threads
,
1070 struct binder_thread
,
1071 waiting_thread_node
);
1074 list_del_init(&thread
->waiting_thread_node
);
1080 * binder_wakeup_thread_ilocked() - wakes up a thread for doing proc work.
1081 * @proc: process to wake up a thread in
1082 * @thread: specific thread to wake-up (may be NULL)
1083 * @sync: whether to do a synchronous wake-up
1085 * This function wakes up a thread in the @proc process.
1086 * The caller may provide a specific thread to wake-up in
1087 * the @thread parameter. If @thread is NULL, this function
1088 * will wake up threads that have called poll().
1090 * Note that for this function to work as expected, callers
1091 * should first call binder_select_thread() to find a thread
1092 * to handle the work (if they don't have a thread already),
1093 * and pass the result into the @thread parameter.
1095 static void binder_wakeup_thread_ilocked(struct binder_proc
*proc
,
1096 struct binder_thread
*thread
,
1099 assert_spin_locked(&proc
->inner_lock
);
1103 wake_up_interruptible_sync(&thread
->wait
);
1105 wake_up_interruptible(&thread
->wait
);
1109 /* Didn't find a thread waiting for proc work; this can happen
1111 * 1. All threads are busy handling transactions
1112 * In that case, one of those threads should call back into
1113 * the kernel driver soon and pick up this work.
1114 * 2. Threads are using the (e)poll interface, in which case
1115 * they may be blocked on the waitqueue without having been
1116 * added to waiting_threads. For this case, we just iterate
1117 * over all threads not handling transaction work, and
1118 * wake them all up. We wake all because we don't know whether
1119 * a thread that called into (e)poll is handling non-binder
1122 binder_wakeup_poll_threads_ilocked(proc
, sync
);
1125 static void binder_wakeup_proc_ilocked(struct binder_proc
*proc
)
1127 struct binder_thread
*thread
= binder_select_thread_ilocked(proc
);
1129 binder_wakeup_thread_ilocked(proc
, thread
, /* sync = */false);
1132 static bool is_rt_policy(int policy
)
1134 return policy
== SCHED_FIFO
|| policy
== SCHED_RR
;
1137 static bool is_fair_policy(int policy
)
1139 return policy
== SCHED_NORMAL
|| policy
== SCHED_BATCH
;
1142 static bool binder_supported_policy(int policy
)
1144 return is_fair_policy(policy
) || is_rt_policy(policy
);
1147 static int to_userspace_prio(int policy
, int kernel_priority
)
1149 if (is_fair_policy(policy
))
1150 return PRIO_TO_NICE(kernel_priority
);
1152 return MAX_USER_RT_PRIO
- 1 - kernel_priority
;
1155 static int to_kernel_prio(int policy
, int user_priority
)
1157 if (is_fair_policy(policy
))
1158 return NICE_TO_PRIO(user_priority
);
1160 return MAX_USER_RT_PRIO
- 1 - user_priority
;
1163 static void binder_do_set_priority(struct task_struct
*task
,
1164 struct binder_priority desired
,
1167 int priority
; /* user-space prio value */
1169 unsigned int policy
= desired
.sched_policy
;
1171 if (task
->policy
== policy
&& task
->normal_prio
== desired
.prio
)
1174 has_cap_nice
= has_capability_noaudit(task
, CAP_SYS_NICE
);
1176 priority
= to_userspace_prio(policy
, desired
.prio
);
1178 if (verify
&& is_rt_policy(policy
) && !has_cap_nice
) {
1179 long max_rtprio
= task_rlimit(task
, RLIMIT_RTPRIO
);
1181 if (max_rtprio
== 0) {
1182 policy
= SCHED_NORMAL
;
1183 priority
= MIN_NICE
;
1184 } else if (priority
> max_rtprio
) {
1185 priority
= max_rtprio
;
1189 if (verify
&& is_fair_policy(policy
) && !has_cap_nice
) {
1190 long min_nice
= (MAX_NICE
- task_rlimit(task
, RLIMIT_NICE
) + 1);
1192 if (min_nice
> MAX_NICE
) {
1193 binder_user_error("%d RLIMIT_NICE not set\n",
1196 } else if (priority
< min_nice
) {
1197 priority
= min_nice
;
1201 if (policy
!= desired
.sched_policy
||
1202 to_kernel_prio(policy
, priority
) != desired
.prio
)
1203 binder_debug(BINDER_DEBUG_PRIORITY_CAP
,
1204 "%d: priority %d not allowed, using %d instead\n",
1205 task
->pid
, desired
.prio
,
1206 to_kernel_prio(policy
, priority
));
1208 trace_binder_set_priority(task
->tgid
, task
->pid
, task
->normal_prio
,
1209 to_kernel_prio(policy
, priority
),
1212 /* Set the actual priority */
1213 if (task
->policy
!= policy
|| is_rt_policy(policy
)) {
1214 struct sched_param params
;
1216 params
.sched_priority
= is_rt_policy(policy
) ? priority
: 0;
1218 sched_setscheduler_nocheck(task
,
1219 policy
| SCHED_RESET_ON_FORK
,
1222 if (is_fair_policy(policy
))
1223 set_user_nice(task
, priority
);
1226 static void binder_set_priority(struct task_struct
*task
,
1227 struct binder_priority desired
)
1229 binder_do_set_priority(task
, desired
, /* verify = */ true);
1232 static void binder_restore_priority(struct task_struct
*task
,
1233 struct binder_priority desired
)
1235 binder_do_set_priority(task
, desired
, /* verify = */ false);
1238 static void binder_transaction_priority(struct task_struct
*task
,
1239 struct binder_transaction
*t
,
1240 struct binder_priority node_prio
,
1243 struct binder_priority desired_prio
= t
->priority
;
1245 if (t
->set_priority_called
)
1248 t
->set_priority_called
= true;
1249 t
->saved_priority
.sched_policy
= task
->policy
;
1250 t
->saved_priority
.prio
= task
->normal_prio
;
1252 if (!inherit_rt
&& is_rt_policy(desired_prio
.sched_policy
)) {
1253 desired_prio
.prio
= NICE_TO_PRIO(0);
1254 desired_prio
.sched_policy
= SCHED_NORMAL
;
1257 if (node_prio
.prio
< t
->priority
.prio
||
1258 (node_prio
.prio
== t
->priority
.prio
&&
1259 node_prio
.sched_policy
== SCHED_FIFO
)) {
1261 * In case the minimum priority on the node is
1262 * higher (lower value), use that priority. If
1263 * the priority is the same, but the node uses
1264 * SCHED_FIFO, prefer SCHED_FIFO, since it can
1265 * run unbounded, unlike SCHED_RR.
1267 desired_prio
= node_prio
;
1270 binder_set_priority(task
, desired_prio
);
1273 static struct binder_node
*binder_get_node_ilocked(struct binder_proc
*proc
,
1274 binder_uintptr_t ptr
)
1276 struct rb_node
*n
= proc
->nodes
.rb_node
;
1277 struct binder_node
*node
;
1279 assert_spin_locked(&proc
->inner_lock
);
1282 node
= rb_entry(n
, struct binder_node
, rb_node
);
1284 if (ptr
< node
->ptr
)
1286 else if (ptr
> node
->ptr
)
1290 * take an implicit weak reference
1291 * to ensure node stays alive until
1292 * call to binder_put_node()
1294 binder_inc_node_tmpref_ilocked(node
);
1301 static struct binder_node
*binder_get_node(struct binder_proc
*proc
,
1302 binder_uintptr_t ptr
)
1304 struct binder_node
*node
;
1306 binder_inner_proc_lock(proc
);
1307 node
= binder_get_node_ilocked(proc
, ptr
);
1308 binder_inner_proc_unlock(proc
);
1312 static struct binder_node
*binder_init_node_ilocked(
1313 struct binder_proc
*proc
,
1314 struct binder_node
*new_node
,
1315 struct flat_binder_object
*fp
)
1317 struct rb_node
**p
= &proc
->nodes
.rb_node
;
1318 struct rb_node
*parent
= NULL
;
1319 struct binder_node
*node
;
1320 binder_uintptr_t ptr
= fp
? fp
->binder
: 0;
1321 binder_uintptr_t cookie
= fp
? fp
->cookie
: 0;
1322 __u32 flags
= fp
? fp
->flags
: 0;
1325 assert_spin_locked(&proc
->inner_lock
);
1330 node
= rb_entry(parent
, struct binder_node
, rb_node
);
1332 if (ptr
< node
->ptr
)
1334 else if (ptr
> node
->ptr
)
1335 p
= &(*p
)->rb_right
;
1338 * A matching node is already in
1339 * the rb tree. Abandon the init
1342 binder_inc_node_tmpref_ilocked(node
);
1347 binder_stats_created(BINDER_STAT_NODE
);
1349 rb_link_node(&node
->rb_node
, parent
, p
);
1350 rb_insert_color(&node
->rb_node
, &proc
->nodes
);
1351 node
->debug_id
= atomic_inc_return(&binder_last_id
);
1354 node
->cookie
= cookie
;
1355 node
->work
.type
= BINDER_WORK_NODE
;
1356 priority
= flags
& FLAT_BINDER_FLAG_PRIORITY_MASK
;
1357 node
->sched_policy
= (flags
& FLAT_BINDER_FLAG_SCHED_POLICY_MASK
) >>
1358 FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT
;
1359 node
->min_priority
= to_kernel_prio(node
->sched_policy
, priority
);
1360 node
->accept_fds
= !!(flags
& FLAT_BINDER_FLAG_ACCEPTS_FDS
);
1361 node
->inherit_rt
= !!(flags
& FLAT_BINDER_FLAG_INHERIT_RT
);
1362 spin_lock_init(&node
->lock
);
1363 INIT_LIST_HEAD(&node
->work
.entry
);
1364 INIT_LIST_HEAD(&node
->async_todo
);
1365 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1366 "%d:%d node %d u%016llx c%016llx created\n",
1367 proc
->pid
, current
->pid
, node
->debug_id
,
1368 (u64
)node
->ptr
, (u64
)node
->cookie
);
1373 static struct binder_node
*binder_new_node(struct binder_proc
*proc
,
1374 struct flat_binder_object
*fp
)
1376 struct binder_node
*node
;
1377 struct binder_node
*new_node
= kzalloc(sizeof(*node
), GFP_KERNEL
);
1381 binder_inner_proc_lock(proc
);
1382 node
= binder_init_node_ilocked(proc
, new_node
, fp
);
1383 binder_inner_proc_unlock(proc
);
1384 if (node
!= new_node
)
1386 * The node was already added by another thread
1393 static void binder_free_node(struct binder_node
*node
)
1396 binder_stats_deleted(BINDER_STAT_NODE
);
1399 static int binder_inc_node_nilocked(struct binder_node
*node
, int strong
,
1401 struct list_head
*target_list
)
1403 struct binder_proc
*proc
= node
->proc
;
1405 assert_spin_locked(&node
->lock
);
1407 assert_spin_locked(&proc
->inner_lock
);
1410 if (target_list
== NULL
&&
1411 node
->internal_strong_refs
== 0 &&
1413 node
== node
->proc
->context
->
1414 binder_context_mgr_node
&&
1415 node
->has_strong_ref
)) {
1416 pr_err("invalid inc strong node for %d\n",
1420 node
->internal_strong_refs
++;
1422 node
->local_strong_refs
++;
1423 if (!node
->has_strong_ref
&& target_list
) {
1424 binder_dequeue_work_ilocked(&node
->work
);
1426 * Note: this function is the only place where we queue
1427 * directly to a thread->todo without using the
1428 * corresponding binder_enqueue_thread_work() helper
1429 * functions; in this case it's ok to not set the
1430 * process_todo flag, since we know this node work will
1431 * always be followed by other work that starts queue
1432 * processing: in case of synchronous transactions, a
1433 * BR_REPLY or BR_ERROR; in case of oneway
1434 * transactions, a BR_TRANSACTION_COMPLETE.
1436 binder_enqueue_work_ilocked(&node
->work
, target_list
);
1440 node
->local_weak_refs
++;
1441 if (!node
->has_weak_ref
&& list_empty(&node
->work
.entry
)) {
1442 if (target_list
== NULL
) {
1443 pr_err("invalid inc weak node for %d\n",
1450 binder_enqueue_work_ilocked(&node
->work
, target_list
);
1456 static int binder_inc_node(struct binder_node
*node
, int strong
, int internal
,
1457 struct list_head
*target_list
)
1461 binder_node_inner_lock(node
);
1462 ret
= binder_inc_node_nilocked(node
, strong
, internal
, target_list
);
1463 binder_node_inner_unlock(node
);
1468 static bool binder_dec_node_nilocked(struct binder_node
*node
,
1469 int strong
, int internal
)
1471 struct binder_proc
*proc
= node
->proc
;
1473 assert_spin_locked(&node
->lock
);
1475 assert_spin_locked(&proc
->inner_lock
);
1478 node
->internal_strong_refs
--;
1480 node
->local_strong_refs
--;
1481 if (node
->local_strong_refs
|| node
->internal_strong_refs
)
1485 node
->local_weak_refs
--;
1486 if (node
->local_weak_refs
|| node
->tmp_refs
||
1487 !hlist_empty(&node
->refs
))
1491 if (proc
&& (node
->has_strong_ref
|| node
->has_weak_ref
)) {
1492 if (list_empty(&node
->work
.entry
)) {
1493 binder_enqueue_work_ilocked(&node
->work
, &proc
->todo
);
1494 binder_wakeup_proc_ilocked(proc
);
1497 if (hlist_empty(&node
->refs
) && !node
->local_strong_refs
&&
1498 !node
->local_weak_refs
&& !node
->tmp_refs
) {
1500 binder_dequeue_work_ilocked(&node
->work
);
1501 rb_erase(&node
->rb_node
, &proc
->nodes
);
1502 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1503 "refless node %d deleted\n",
1506 BUG_ON(!list_empty(&node
->work
.entry
));
1507 spin_lock(&binder_dead_nodes_lock
);
1509 * tmp_refs could have changed so
1512 if (node
->tmp_refs
) {
1513 spin_unlock(&binder_dead_nodes_lock
);
1516 hlist_del(&node
->dead_node
);
1517 spin_unlock(&binder_dead_nodes_lock
);
1518 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1519 "dead node %d deleted\n",
1528 static void binder_dec_node(struct binder_node
*node
, int strong
, int internal
)
1532 binder_node_inner_lock(node
);
1533 free_node
= binder_dec_node_nilocked(node
, strong
, internal
);
1534 binder_node_inner_unlock(node
);
1536 binder_free_node(node
);
1539 static void binder_inc_node_tmpref_ilocked(struct binder_node
*node
)
1542 * No call to binder_inc_node() is needed since we
1543 * don't need to inform userspace of any changes to
1550 * binder_inc_node_tmpref() - take a temporary reference on node
1551 * @node: node to reference
1553 * Take reference on node to prevent the node from being freed
1554 * while referenced only by a local variable. The inner lock is
1555 * needed to serialize with the node work on the queue (which
1556 * isn't needed after the node is dead). If the node is dead
1557 * (node->proc is NULL), use binder_dead_nodes_lock to protect
1558 * node->tmp_refs against dead-node-only cases where the node
1559 * lock cannot be acquired (eg traversing the dead node list to
1562 static void binder_inc_node_tmpref(struct binder_node
*node
)
1564 binder_node_lock(node
);
1566 binder_inner_proc_lock(node
->proc
);
1568 spin_lock(&binder_dead_nodes_lock
);
1569 binder_inc_node_tmpref_ilocked(node
);
1571 binder_inner_proc_unlock(node
->proc
);
1573 spin_unlock(&binder_dead_nodes_lock
);
1574 binder_node_unlock(node
);
1578 * binder_dec_node_tmpref() - remove a temporary reference on node
1579 * @node: node to reference
1581 * Release temporary reference on node taken via binder_inc_node_tmpref()
1583 static void binder_dec_node_tmpref(struct binder_node
*node
)
1587 binder_node_inner_lock(node
);
1589 spin_lock(&binder_dead_nodes_lock
);
1591 BUG_ON(node
->tmp_refs
< 0);
1593 spin_unlock(&binder_dead_nodes_lock
);
1595 * Call binder_dec_node() to check if all refcounts are 0
1596 * and cleanup is needed. Calling with strong=0 and internal=1
1597 * causes no actual reference to be released in binder_dec_node().
1598 * If that changes, a change is needed here too.
1600 free_node
= binder_dec_node_nilocked(node
, 0, 1);
1601 binder_node_inner_unlock(node
);
1603 binder_free_node(node
);
1606 static void binder_put_node(struct binder_node
*node
)
1608 binder_dec_node_tmpref(node
);
1611 static struct binder_ref
*binder_get_ref_olocked(struct binder_proc
*proc
,
1612 u32 desc
, bool need_strong_ref
)
1614 struct rb_node
*n
= proc
->refs_by_desc
.rb_node
;
1615 struct binder_ref
*ref
;
1618 ref
= rb_entry(n
, struct binder_ref
, rb_node_desc
);
1620 if (desc
< ref
->data
.desc
) {
1622 } else if (desc
> ref
->data
.desc
) {
1624 } else if (need_strong_ref
&& !ref
->data
.strong
) {
1625 binder_user_error("tried to use weak ref as strong ref\n");
1635 * binder_get_ref_for_node_olocked() - get the ref associated with given node
1636 * @proc: binder_proc that owns the ref
1637 * @node: binder_node of target
1638 * @new_ref: newly allocated binder_ref to be initialized or %NULL
1640 * Look up the ref for the given node and return it if it exists
1642 * If it doesn't exist and the caller provides a newly allocated
1643 * ref, initialize the fields of the newly allocated ref and insert
1644 * into the given proc rb_trees and node refs list.
1646 * Return: the ref for node. It is possible that another thread
1647 * allocated/initialized the ref first in which case the
1648 * returned ref would be different than the passed-in
1649 * new_ref. new_ref must be kfree'd by the caller in
1652 static struct binder_ref
*binder_get_ref_for_node_olocked(
1653 struct binder_proc
*proc
,
1654 struct binder_node
*node
,
1655 struct binder_ref
*new_ref
)
1657 struct binder_context
*context
= proc
->context
;
1658 struct rb_node
**p
= &proc
->refs_by_node
.rb_node
;
1659 struct rb_node
*parent
= NULL
;
1660 struct binder_ref
*ref
;
1665 ref
= rb_entry(parent
, struct binder_ref
, rb_node_node
);
1667 if (node
< ref
->node
)
1669 else if (node
> ref
->node
)
1670 p
= &(*p
)->rb_right
;
1677 binder_stats_created(BINDER_STAT_REF
);
1678 new_ref
->data
.debug_id
= atomic_inc_return(&binder_last_id
);
1679 new_ref
->proc
= proc
;
1680 new_ref
->node
= node
;
1681 rb_link_node(&new_ref
->rb_node_node
, parent
, p
);
1682 rb_insert_color(&new_ref
->rb_node_node
, &proc
->refs_by_node
);
1684 new_ref
->data
.desc
= (node
== context
->binder_context_mgr_node
) ? 0 : 1;
1685 for (n
= rb_first(&proc
->refs_by_desc
); n
!= NULL
; n
= rb_next(n
)) {
1686 ref
= rb_entry(n
, struct binder_ref
, rb_node_desc
);
1687 if (ref
->data
.desc
> new_ref
->data
.desc
)
1689 new_ref
->data
.desc
= ref
->data
.desc
+ 1;
1692 p
= &proc
->refs_by_desc
.rb_node
;
1695 ref
= rb_entry(parent
, struct binder_ref
, rb_node_desc
);
1697 if (new_ref
->data
.desc
< ref
->data
.desc
)
1699 else if (new_ref
->data
.desc
> ref
->data
.desc
)
1700 p
= &(*p
)->rb_right
;
1704 rb_link_node(&new_ref
->rb_node_desc
, parent
, p
);
1705 rb_insert_color(&new_ref
->rb_node_desc
, &proc
->refs_by_desc
);
1707 binder_node_lock(node
);
1708 hlist_add_head(&new_ref
->node_entry
, &node
->refs
);
1710 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1711 "%d new ref %d desc %d for node %d\n",
1712 proc
->pid
, new_ref
->data
.debug_id
, new_ref
->data
.desc
,
1714 binder_node_unlock(node
);
1718 static void binder_cleanup_ref_olocked(struct binder_ref
*ref
)
1720 bool delete_node
= false;
1722 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1723 "%d delete ref %d desc %d for node %d\n",
1724 ref
->proc
->pid
, ref
->data
.debug_id
, ref
->data
.desc
,
1725 ref
->node
->debug_id
);
1727 rb_erase(&ref
->rb_node_desc
, &ref
->proc
->refs_by_desc
);
1728 rb_erase(&ref
->rb_node_node
, &ref
->proc
->refs_by_node
);
1730 binder_node_inner_lock(ref
->node
);
1731 if (ref
->data
.strong
)
1732 binder_dec_node_nilocked(ref
->node
, 1, 1);
1734 hlist_del(&ref
->node_entry
);
1735 delete_node
= binder_dec_node_nilocked(ref
->node
, 0, 1);
1736 binder_node_inner_unlock(ref
->node
);
1738 * Clear ref->node unless we want the caller to free the node
1742 * The caller uses ref->node to determine
1743 * whether the node needs to be freed. Clear
1744 * it since the node is still alive.
1750 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
1751 "%d delete ref %d desc %d has death notification\n",
1752 ref
->proc
->pid
, ref
->data
.debug_id
,
1754 binder_dequeue_work(ref
->proc
, &ref
->death
->work
);
1755 binder_stats_deleted(BINDER_STAT_DEATH
);
1757 binder_stats_deleted(BINDER_STAT_REF
);
1761 * binder_inc_ref_olocked() - increment the ref for given handle
1762 * @ref: ref to be incremented
1763 * @strong: if true, strong increment, else weak
1764 * @target_list: list to queue node work on
1766 * Increment the ref. @ref->proc->outer_lock must be held on entry
1768 * Return: 0, if successful, else errno
1770 static int binder_inc_ref_olocked(struct binder_ref
*ref
, int strong
,
1771 struct list_head
*target_list
)
1776 if (ref
->data
.strong
== 0) {
1777 ret
= binder_inc_node(ref
->node
, 1, 1, target_list
);
1783 if (ref
->data
.weak
== 0) {
1784 ret
= binder_inc_node(ref
->node
, 0, 1, target_list
);
1794 * binder_dec_ref() - dec the ref for given handle
1795 * @ref: ref to be decremented
1796 * @strong: if true, strong decrement, else weak
1798 * Decrement the ref.
1800 * Return: true if ref is cleaned up and ready to be freed
1802 static bool binder_dec_ref_olocked(struct binder_ref
*ref
, int strong
)
1805 if (ref
->data
.strong
== 0) {
1806 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
1807 ref
->proc
->pid
, ref
->data
.debug_id
,
1808 ref
->data
.desc
, ref
->data
.strong
,
1813 if (ref
->data
.strong
== 0)
1814 binder_dec_node(ref
->node
, strong
, 1);
1816 if (ref
->data
.weak
== 0) {
1817 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
1818 ref
->proc
->pid
, ref
->data
.debug_id
,
1819 ref
->data
.desc
, ref
->data
.strong
,
1825 if (ref
->data
.strong
== 0 && ref
->data
.weak
== 0) {
1826 binder_cleanup_ref_olocked(ref
);
1833 * binder_get_node_from_ref() - get the node from the given proc/desc
1834 * @proc: proc containing the ref
1835 * @desc: the handle associated with the ref
1836 * @need_strong_ref: if true, only return node if ref is strong
1837 * @rdata: the id/refcount data for the ref
1839 * Given a proc and ref handle, return the associated binder_node
1841 * Return: a binder_node or NULL if not found or not strong when strong required
1843 static struct binder_node
*binder_get_node_from_ref(
1844 struct binder_proc
*proc
,
1845 u32 desc
, bool need_strong_ref
,
1846 struct binder_ref_data
*rdata
)
1848 struct binder_node
*node
;
1849 struct binder_ref
*ref
;
1851 binder_proc_lock(proc
);
1852 ref
= binder_get_ref_olocked(proc
, desc
, need_strong_ref
);
1857 * Take an implicit reference on the node to ensure
1858 * it stays alive until the call to binder_put_node()
1860 binder_inc_node_tmpref(node
);
1863 binder_proc_unlock(proc
);
1868 binder_proc_unlock(proc
);
1873 * binder_free_ref() - free the binder_ref
1876 * Free the binder_ref. Free the binder_node indicated by ref->node
1877 * (if non-NULL) and the binder_ref_death indicated by ref->death.
1879 static void binder_free_ref(struct binder_ref
*ref
)
1882 binder_free_node(ref
->node
);
1888 * binder_update_ref_for_handle() - inc/dec the ref for given handle
1889 * @proc: proc containing the ref
1890 * @desc: the handle associated with the ref
1891 * @increment: true=inc reference, false=dec reference
1892 * @strong: true=strong reference, false=weak reference
1893 * @rdata: the id/refcount data for the ref
1895 * Given a proc and ref handle, increment or decrement the ref
1896 * according to "increment" arg.
1898 * Return: 0 if successful, else errno
1900 static int binder_update_ref_for_handle(struct binder_proc
*proc
,
1901 uint32_t desc
, bool increment
, bool strong
,
1902 struct binder_ref_data
*rdata
)
1905 struct binder_ref
*ref
;
1906 bool delete_ref
= false;
1908 binder_proc_lock(proc
);
1909 ref
= binder_get_ref_olocked(proc
, desc
, strong
);
1915 ret
= binder_inc_ref_olocked(ref
, strong
, NULL
);
1917 delete_ref
= binder_dec_ref_olocked(ref
, strong
);
1921 binder_proc_unlock(proc
);
1924 binder_free_ref(ref
);
1928 binder_proc_unlock(proc
);
1933 * binder_dec_ref_for_handle() - dec the ref for given handle
1934 * @proc: proc containing the ref
1935 * @desc: the handle associated with the ref
1936 * @strong: true=strong reference, false=weak reference
1937 * @rdata: the id/refcount data for the ref
1939 * Just calls binder_update_ref_for_handle() to decrement the ref.
1941 * Return: 0 if successful, else errno
1943 static int binder_dec_ref_for_handle(struct binder_proc
*proc
,
1944 uint32_t desc
, bool strong
, struct binder_ref_data
*rdata
)
1946 return binder_update_ref_for_handle(proc
, desc
, false, strong
, rdata
);
1951 * binder_inc_ref_for_node() - increment the ref for given proc/node
1952 * @proc: proc containing the ref
1953 * @node: target node
1954 * @strong: true=strong reference, false=weak reference
1955 * @target_list: worklist to use if node is incremented
1956 * @rdata: the id/refcount data for the ref
1958 * Given a proc and node, increment the ref. Create the ref if it
1959 * doesn't already exist
1961 * Return: 0 if successful, else errno
1963 static int binder_inc_ref_for_node(struct binder_proc
*proc
,
1964 struct binder_node
*node
,
1966 struct list_head
*target_list
,
1967 struct binder_ref_data
*rdata
)
1969 struct binder_ref
*ref
;
1970 struct binder_ref
*new_ref
= NULL
;
1973 binder_proc_lock(proc
);
1974 ref
= binder_get_ref_for_node_olocked(proc
, node
, NULL
);
1976 binder_proc_unlock(proc
);
1977 new_ref
= kzalloc(sizeof(*ref
), GFP_KERNEL
);
1980 binder_proc_lock(proc
);
1981 ref
= binder_get_ref_for_node_olocked(proc
, node
, new_ref
);
1983 ret
= binder_inc_ref_olocked(ref
, strong
, target_list
);
1985 binder_proc_unlock(proc
);
1986 if (new_ref
&& ref
!= new_ref
)
1988 * Another thread created the ref first so
1989 * free the one we allocated
1995 static void binder_pop_transaction_ilocked(struct binder_thread
*target_thread
,
1996 struct binder_transaction
*t
)
1998 BUG_ON(!target_thread
);
1999 assert_spin_locked(&target_thread
->proc
->inner_lock
);
2000 BUG_ON(target_thread
->transaction_stack
!= t
);
2001 BUG_ON(target_thread
->transaction_stack
->from
!= target_thread
);
2002 target_thread
->transaction_stack
=
2003 target_thread
->transaction_stack
->from_parent
;
2008 * binder_thread_dec_tmpref() - decrement thread->tmp_ref
2009 * @thread: thread to decrement
2011 * A thread needs to be kept alive while being used to create or
2012 * handle a transaction. binder_get_txn_from() is used to safely
2013 * extract t->from from a binder_transaction and keep the thread
2014 * indicated by t->from from being freed. When done with that
2015 * binder_thread, this function is called to decrement the
2016 * tmp_ref and free if appropriate (thread has been released
2017 * and no transaction being processed by the driver)
2019 static void binder_thread_dec_tmpref(struct binder_thread
*thread
)
2022 * atomic is used to protect the counter value while
2023 * it cannot reach zero or thread->is_dead is false
2025 binder_inner_proc_lock(thread
->proc
);
2026 atomic_dec(&thread
->tmp_ref
);
2027 if (thread
->is_dead
&& !atomic_read(&thread
->tmp_ref
)) {
2028 binder_inner_proc_unlock(thread
->proc
);
2029 binder_free_thread(thread
);
2032 binder_inner_proc_unlock(thread
->proc
);
2036 * binder_proc_dec_tmpref() - decrement proc->tmp_ref
2037 * @proc: proc to decrement
2039 * A binder_proc needs to be kept alive while being used to create or
2040 * handle a transaction. proc->tmp_ref is incremented when
2041 * creating a new transaction or the binder_proc is currently in-use
2042 * by threads that are being released. When done with the binder_proc,
2043 * this function is called to decrement the counter and free the
2044 * proc if appropriate (proc has been released, all threads have
2045 * been released and not currenly in-use to process a transaction).
2047 static void binder_proc_dec_tmpref(struct binder_proc
*proc
)
2049 binder_inner_proc_lock(proc
);
2051 if (proc
->is_dead
&& RB_EMPTY_ROOT(&proc
->threads
) &&
2053 binder_inner_proc_unlock(proc
);
2054 binder_free_proc(proc
);
2057 binder_inner_proc_unlock(proc
);
2061 * binder_get_txn_from() - safely extract the "from" thread in transaction
2062 * @t: binder transaction for t->from
2064 * Atomically return the "from" thread and increment the tmp_ref
2065 * count for the thread to ensure it stays alive until
2066 * binder_thread_dec_tmpref() is called.
2068 * Return: the value of t->from
2070 static struct binder_thread
*binder_get_txn_from(
2071 struct binder_transaction
*t
)
2073 struct binder_thread
*from
;
2075 spin_lock(&t
->lock
);
2078 atomic_inc(&from
->tmp_ref
);
2079 spin_unlock(&t
->lock
);
2084 * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock
2085 * @t: binder transaction for t->from
2087 * Same as binder_get_txn_from() except it also acquires the proc->inner_lock
2088 * to guarantee that the thread cannot be released while operating on it.
2089 * The caller must call binder_inner_proc_unlock() to release the inner lock
2090 * as well as call binder_dec_thread_txn() to release the reference.
2092 * Return: the value of t->from
2094 static struct binder_thread
*binder_get_txn_from_and_acq_inner(
2095 struct binder_transaction
*t
)
2097 struct binder_thread
*from
;
2099 from
= binder_get_txn_from(t
);
2102 binder_inner_proc_lock(from
->proc
);
2104 BUG_ON(from
!= t
->from
);
2107 binder_inner_proc_unlock(from
->proc
);
2108 binder_thread_dec_tmpref(from
);
2112 static void binder_free_transaction(struct binder_transaction
*t
)
2115 t
->buffer
->transaction
= NULL
;
2117 binder_stats_deleted(BINDER_STAT_TRANSACTION
);
2120 static void binder_send_failed_reply(struct binder_transaction
*t
,
2121 uint32_t error_code
)
2123 struct binder_thread
*target_thread
;
2124 struct binder_transaction
*next
;
2126 BUG_ON(t
->flags
& TF_ONE_WAY
);
2128 target_thread
= binder_get_txn_from_and_acq_inner(t
);
2129 if (target_thread
) {
2130 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
2131 "send failed reply for transaction %d to %d:%d\n",
2133 target_thread
->proc
->pid
,
2134 target_thread
->pid
);
2136 binder_pop_transaction_ilocked(target_thread
, t
);
2137 if (target_thread
->reply_error
.cmd
== BR_OK
) {
2138 target_thread
->reply_error
.cmd
= error_code
;
2139 binder_enqueue_thread_work_ilocked(
2141 &target_thread
->reply_error
.work
);
2142 wake_up_interruptible(&target_thread
->wait
);
2144 WARN(1, "Unexpected reply error: %u\n",
2145 target_thread
->reply_error
.cmd
);
2147 binder_inner_proc_unlock(target_thread
->proc
);
2148 binder_thread_dec_tmpref(target_thread
);
2149 binder_free_transaction(t
);
2152 next
= t
->from_parent
;
2154 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
2155 "send failed reply for transaction %d, target dead\n",
2158 binder_free_transaction(t
);
2160 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
2161 "reply failed, no target thread at root\n");
2165 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
2166 "reply failed, no target thread -- retry %d\n",
2172 * binder_cleanup_transaction() - cleans up undelivered transaction
2173 * @t: transaction that needs to be cleaned up
2174 * @reason: reason the transaction wasn't delivered
2175 * @error_code: error to return to caller (if synchronous call)
2177 static void binder_cleanup_transaction(struct binder_transaction
*t
,
2179 uint32_t error_code
)
2181 if (t
->buffer
->target_node
&& !(t
->flags
& TF_ONE_WAY
)) {
2182 binder_send_failed_reply(t
, error_code
);
2184 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
2185 "undelivered transaction %d, %s\n",
2186 t
->debug_id
, reason
);
2187 binder_free_transaction(t
);
2192 * binder_validate_object() - checks for a valid metadata object in a buffer.
2193 * @buffer: binder_buffer that we're parsing.
2194 * @offset: offset in the buffer at which to validate an object.
2196 * Return: If there's a valid metadata object at @offset in @buffer, the
2197 * size of that object. Otherwise, it returns zero.
2199 static size_t binder_validate_object(struct binder_buffer
*buffer
, u64 offset
)
2201 /* Check if we can read a header first */
2202 struct binder_object_header
*hdr
;
2203 size_t object_size
= 0;
2205 if (offset
> buffer
->data_size
- sizeof(*hdr
) ||
2206 buffer
->data_size
< sizeof(*hdr
) ||
2207 !IS_ALIGNED(offset
, sizeof(u32
)))
2210 /* Ok, now see if we can read a complete object. */
2211 hdr
= (struct binder_object_header
*)(buffer
->data
+ offset
);
2212 switch (hdr
->type
) {
2213 case BINDER_TYPE_BINDER
:
2214 case BINDER_TYPE_WEAK_BINDER
:
2215 case BINDER_TYPE_HANDLE
:
2216 case BINDER_TYPE_WEAK_HANDLE
:
2217 object_size
= sizeof(struct flat_binder_object
);
2219 case BINDER_TYPE_FD
:
2220 object_size
= sizeof(struct binder_fd_object
);
2222 case BINDER_TYPE_PTR
:
2223 object_size
= sizeof(struct binder_buffer_object
);
2225 case BINDER_TYPE_FDA
:
2226 object_size
= sizeof(struct binder_fd_array_object
);
2231 if (offset
<= buffer
->data_size
- object_size
&&
2232 buffer
->data_size
>= object_size
)
2239 * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
2240 * @b: binder_buffer containing the object
2241 * @index: index in offset array at which the binder_buffer_object is
2243 * @start: points to the start of the offset array
2244 * @num_valid: the number of valid offsets in the offset array
2246 * Return: If @index is within the valid range of the offset array
2247 * described by @start and @num_valid, and if there's a valid
2248 * binder_buffer_object at the offset found in index @index
2249 * of the offset array, that object is returned. Otherwise,
2250 * %NULL is returned.
2251 * Note that the offset found in index @index itself is not
2252 * verified; this function assumes that @num_valid elements
2253 * from @start were previously verified to have valid offsets.
2255 static struct binder_buffer_object
*binder_validate_ptr(struct binder_buffer
*b
,
2256 binder_size_t index
,
2257 binder_size_t
*start
,
2258 binder_size_t num_valid
)
2260 struct binder_buffer_object
*buffer_obj
;
2261 binder_size_t
*offp
;
2263 if (index
>= num_valid
)
2266 offp
= start
+ index
;
2267 buffer_obj
= (struct binder_buffer_object
*)(b
->data
+ *offp
);
2268 if (buffer_obj
->hdr
.type
!= BINDER_TYPE_PTR
)
2275 * binder_validate_fixup() - validates pointer/fd fixups happen in order.
2276 * @b: transaction buffer
2277 * @objects_start start of objects buffer
2278 * @buffer: binder_buffer_object in which to fix up
2279 * @offset: start offset in @buffer to fix up
2280 * @last_obj: last binder_buffer_object that we fixed up in
2281 * @last_min_offset: minimum fixup offset in @last_obj
2283 * Return: %true if a fixup in buffer @buffer at offset @offset is
2286 * For safety reasons, we only allow fixups inside a buffer to happen
2287 * at increasing offsets; additionally, we only allow fixup on the last
2288 * buffer object that was verified, or one of its parents.
2290 * Example of what is allowed:
2293 * B (parent = A, offset = 0)
2294 * C (parent = A, offset = 16)
2295 * D (parent = C, offset = 0)
2296 * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
2298 * Examples of what is not allowed:
2300 * Decreasing offsets within the same parent:
2302 * C (parent = A, offset = 16)
2303 * B (parent = A, offset = 0) // decreasing offset within A
2305 * Referring to a parent that wasn't the last object or any of its parents:
2307 * B (parent = A, offset = 0)
2308 * C (parent = A, offset = 0)
2309 * C (parent = A, offset = 16)
2310 * D (parent = B, offset = 0) // B is not A or any of A's parents
2312 static bool binder_validate_fixup(struct binder_buffer
*b
,
2313 binder_size_t
*objects_start
,
2314 struct binder_buffer_object
*buffer
,
2315 binder_size_t fixup_offset
,
2316 struct binder_buffer_object
*last_obj
,
2317 binder_size_t last_min_offset
)
2320 /* Nothing to fix up in */
2324 while (last_obj
!= buffer
) {
2326 * Safe to retrieve the parent of last_obj, since it
2327 * was already previously verified by the driver.
2329 if ((last_obj
->flags
& BINDER_BUFFER_FLAG_HAS_PARENT
) == 0)
2331 last_min_offset
= last_obj
->parent_offset
+ sizeof(uintptr_t);
2332 last_obj
= (struct binder_buffer_object
*)
2333 (b
->data
+ *(objects_start
+ last_obj
->parent
));
2335 return (fixup_offset
>= last_min_offset
);
2338 static void binder_transaction_buffer_release(struct binder_proc
*proc
,
2339 struct binder_buffer
*buffer
,
2340 binder_size_t
*failed_at
)
2342 binder_size_t
*offp
, *off_start
, *off_end
;
2343 int debug_id
= buffer
->debug_id
;
2345 binder_debug(BINDER_DEBUG_TRANSACTION
,
2346 "%d buffer release %d, size %zd-%zd, failed at %p\n",
2347 proc
->pid
, buffer
->debug_id
,
2348 buffer
->data_size
, buffer
->offsets_size
, failed_at
);
2350 if (buffer
->target_node
)
2351 binder_dec_node(buffer
->target_node
, 1, 0);
2353 off_start
= (binder_size_t
*)(buffer
->data
+
2354 ALIGN(buffer
->data_size
, sizeof(void *)));
2356 off_end
= failed_at
;
2358 off_end
= (void *)off_start
+ buffer
->offsets_size
;
2359 for (offp
= off_start
; offp
< off_end
; offp
++) {
2360 struct binder_object_header
*hdr
;
2361 size_t object_size
= binder_validate_object(buffer
, *offp
);
2363 if (object_size
== 0) {
2364 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
2365 debug_id
, (u64
)*offp
, buffer
->data_size
);
2368 hdr
= (struct binder_object_header
*)(buffer
->data
+ *offp
);
2369 switch (hdr
->type
) {
2370 case BINDER_TYPE_BINDER
:
2371 case BINDER_TYPE_WEAK_BINDER
: {
2372 struct flat_binder_object
*fp
;
2373 struct binder_node
*node
;
2375 fp
= to_flat_binder_object(hdr
);
2376 node
= binder_get_node(proc
, fp
->binder
);
2378 pr_err("transaction release %d bad node %016llx\n",
2379 debug_id
, (u64
)fp
->binder
);
2382 binder_debug(BINDER_DEBUG_TRANSACTION
,
2383 " node %d u%016llx\n",
2384 node
->debug_id
, (u64
)node
->ptr
);
2385 binder_dec_node(node
, hdr
->type
== BINDER_TYPE_BINDER
,
2387 binder_put_node(node
);
2389 case BINDER_TYPE_HANDLE
:
2390 case BINDER_TYPE_WEAK_HANDLE
: {
2391 struct flat_binder_object
*fp
;
2392 struct binder_ref_data rdata
;
2395 fp
= to_flat_binder_object(hdr
);
2396 ret
= binder_dec_ref_for_handle(proc
, fp
->handle
,
2397 hdr
->type
== BINDER_TYPE_HANDLE
, &rdata
);
2400 pr_err("transaction release %d bad handle %d, ret = %d\n",
2401 debug_id
, fp
->handle
, ret
);
2404 binder_debug(BINDER_DEBUG_TRANSACTION
,
2405 " ref %d desc %d\n",
2406 rdata
.debug_id
, rdata
.desc
);
2409 case BINDER_TYPE_FD
: {
2410 struct binder_fd_object
*fp
= to_binder_fd_object(hdr
);
2412 binder_debug(BINDER_DEBUG_TRANSACTION
,
2413 " fd %d\n", fp
->fd
);
2415 task_close_fd(proc
, fp
->fd
);
2417 case BINDER_TYPE_PTR
:
2419 * Nothing to do here, this will get cleaned up when the
2420 * transaction buffer gets freed
2423 case BINDER_TYPE_FDA
: {
2424 struct binder_fd_array_object
*fda
;
2425 struct binder_buffer_object
*parent
;
2426 uintptr_t parent_buffer
;
2429 binder_size_t fd_buf_size
;
2431 fda
= to_binder_fd_array_object(hdr
);
2432 parent
= binder_validate_ptr(buffer
, fda
->parent
,
2436 pr_err("transaction release %d bad parent offset",
2441 * Since the parent was already fixed up, convert it
2442 * back to kernel address space to access it
2444 parent_buffer
= parent
->buffer
-
2445 binder_alloc_get_user_buffer_offset(
2448 fd_buf_size
= sizeof(u32
) * fda
->num_fds
;
2449 if (fda
->num_fds
>= SIZE_MAX
/ sizeof(u32
)) {
2450 pr_err("transaction release %d invalid number of fds (%lld)\n",
2451 debug_id
, (u64
)fda
->num_fds
);
2454 if (fd_buf_size
> parent
->length
||
2455 fda
->parent_offset
> parent
->length
- fd_buf_size
) {
2456 /* No space for all file descriptors here. */
2457 pr_err("transaction release %d not enough space for %lld fds in buffer\n",
2458 debug_id
, (u64
)fda
->num_fds
);
2461 fd_array
= (u32
*)(parent_buffer
+ (uintptr_t)fda
->parent_offset
);
2462 for (fd_index
= 0; fd_index
< fda
->num_fds
; fd_index
++)
2463 task_close_fd(proc
, fd_array
[fd_index
]);
2466 pr_err("transaction release %d bad object type %x\n",
2467 debug_id
, hdr
->type
);
2473 static int binder_translate_binder(struct flat_binder_object
*fp
,
2474 struct binder_transaction
*t
,
2475 struct binder_thread
*thread
)
2477 struct binder_node
*node
;
2478 struct binder_proc
*proc
= thread
->proc
;
2479 struct binder_proc
*target_proc
= t
->to_proc
;
2480 struct binder_ref_data rdata
;
2483 node
= binder_get_node(proc
, fp
->binder
);
2485 node
= binder_new_node(proc
, fp
);
2489 if (fp
->cookie
!= node
->cookie
) {
2490 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
2491 proc
->pid
, thread
->pid
, (u64
)fp
->binder
,
2492 node
->debug_id
, (u64
)fp
->cookie
,
2497 if (security_binder_transfer_binder(proc
->tsk
, target_proc
->tsk
)) {
2502 ret
= binder_inc_ref_for_node(target_proc
, node
,
2503 fp
->hdr
.type
== BINDER_TYPE_BINDER
,
2504 &thread
->todo
, &rdata
);
2508 if (fp
->hdr
.type
== BINDER_TYPE_BINDER
)
2509 fp
->hdr
.type
= BINDER_TYPE_HANDLE
;
2511 fp
->hdr
.type
= BINDER_TYPE_WEAK_HANDLE
;
2513 fp
->handle
= rdata
.desc
;
2516 trace_binder_transaction_node_to_ref(t
, node
, &rdata
);
2517 binder_debug(BINDER_DEBUG_TRANSACTION
,
2518 " node %d u%016llx -> ref %d desc %d\n",
2519 node
->debug_id
, (u64
)node
->ptr
,
2520 rdata
.debug_id
, rdata
.desc
);
2522 binder_put_node(node
);
2526 static int binder_translate_handle(struct flat_binder_object
*fp
,
2527 struct binder_transaction
*t
,
2528 struct binder_thread
*thread
)
2530 struct binder_proc
*proc
= thread
->proc
;
2531 struct binder_proc
*target_proc
= t
->to_proc
;
2532 struct binder_node
*node
;
2533 struct binder_ref_data src_rdata
;
2536 node
= binder_get_node_from_ref(proc
, fp
->handle
,
2537 fp
->hdr
.type
== BINDER_TYPE_HANDLE
, &src_rdata
);
2539 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
2540 proc
->pid
, thread
->pid
, fp
->handle
);
2543 if (security_binder_transfer_binder(proc
->tsk
, target_proc
->tsk
)) {
2548 binder_node_lock(node
);
2549 if (node
->proc
== target_proc
) {
2550 if (fp
->hdr
.type
== BINDER_TYPE_HANDLE
)
2551 fp
->hdr
.type
= BINDER_TYPE_BINDER
;
2553 fp
->hdr
.type
= BINDER_TYPE_WEAK_BINDER
;
2554 fp
->binder
= node
->ptr
;
2555 fp
->cookie
= node
->cookie
;
2557 binder_inner_proc_lock(node
->proc
);
2558 binder_inc_node_nilocked(node
,
2559 fp
->hdr
.type
== BINDER_TYPE_BINDER
,
2562 binder_inner_proc_unlock(node
->proc
);
2563 trace_binder_transaction_ref_to_node(t
, node
, &src_rdata
);
2564 binder_debug(BINDER_DEBUG_TRANSACTION
,
2565 " ref %d desc %d -> node %d u%016llx\n",
2566 src_rdata
.debug_id
, src_rdata
.desc
, node
->debug_id
,
2568 binder_node_unlock(node
);
2570 struct binder_ref_data dest_rdata
;
2572 binder_node_unlock(node
);
2573 ret
= binder_inc_ref_for_node(target_proc
, node
,
2574 fp
->hdr
.type
== BINDER_TYPE_HANDLE
,
2580 fp
->handle
= dest_rdata
.desc
;
2582 trace_binder_transaction_ref_to_ref(t
, node
, &src_rdata
,
2584 binder_debug(BINDER_DEBUG_TRANSACTION
,
2585 " ref %d desc %d -> ref %d desc %d (node %d)\n",
2586 src_rdata
.debug_id
, src_rdata
.desc
,
2587 dest_rdata
.debug_id
, dest_rdata
.desc
,
2591 binder_put_node(node
);
2595 static int binder_translate_fd(int fd
,
2596 struct binder_transaction
*t
,
2597 struct binder_thread
*thread
,
2598 struct binder_transaction
*in_reply_to
)
2600 struct binder_proc
*proc
= thread
->proc
;
2601 struct binder_proc
*target_proc
= t
->to_proc
;
2605 bool target_allows_fd
;
2608 target_allows_fd
= !!(in_reply_to
->flags
& TF_ACCEPT_FDS
);
2610 target_allows_fd
= t
->buffer
->target_node
->accept_fds
;
2611 if (!target_allows_fd
) {
2612 binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
2613 proc
->pid
, thread
->pid
,
2614 in_reply_to
? "reply" : "transaction",
2617 goto err_fd_not_accepted
;
2622 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
2623 proc
->pid
, thread
->pid
, fd
);
2627 ret
= security_binder_transfer_file(proc
->tsk
, target_proc
->tsk
, file
);
2633 target_fd
= task_get_unused_fd_flags(target_proc
, O_CLOEXEC
);
2634 if (target_fd
< 0) {
2636 goto err_get_unused_fd
;
2638 task_fd_install(target_proc
, target_fd
, file
);
2639 trace_binder_transaction_fd(t
, fd
, target_fd
);
2640 binder_debug(BINDER_DEBUG_TRANSACTION
, " fd %d -> %d\n",
2649 err_fd_not_accepted
:
2653 static int binder_translate_fd_array(struct binder_fd_array_object
*fda
,
2654 struct binder_buffer_object
*parent
,
2655 struct binder_transaction
*t
,
2656 struct binder_thread
*thread
,
2657 struct binder_transaction
*in_reply_to
)
2659 binder_size_t fdi
, fd_buf_size
, num_installed_fds
;
2661 uintptr_t parent_buffer
;
2663 struct binder_proc
*proc
= thread
->proc
;
2664 struct binder_proc
*target_proc
= t
->to_proc
;
2666 fd_buf_size
= sizeof(u32
) * fda
->num_fds
;
2667 if (fda
->num_fds
>= SIZE_MAX
/ sizeof(u32
)) {
2668 binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
2669 proc
->pid
, thread
->pid
, (u64
)fda
->num_fds
);
2672 if (fd_buf_size
> parent
->length
||
2673 fda
->parent_offset
> parent
->length
- fd_buf_size
) {
2674 /* No space for all file descriptors here. */
2675 binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
2676 proc
->pid
, thread
->pid
, (u64
)fda
->num_fds
);
2680 * Since the parent was already fixed up, convert it
2681 * back to the kernel address space to access it
2683 parent_buffer
= parent
->buffer
-
2684 binder_alloc_get_user_buffer_offset(&target_proc
->alloc
);
2685 fd_array
= (u32
*)(parent_buffer
+ (uintptr_t)fda
->parent_offset
);
2686 if (!IS_ALIGNED((unsigned long)fd_array
, sizeof(u32
))) {
2687 binder_user_error("%d:%d parent offset not aligned correctly.\n",
2688 proc
->pid
, thread
->pid
);
2691 for (fdi
= 0; fdi
< fda
->num_fds
; fdi
++) {
2692 target_fd
= binder_translate_fd(fd_array
[fdi
], t
, thread
,
2695 goto err_translate_fd_failed
;
2696 fd_array
[fdi
] = target_fd
;
2700 err_translate_fd_failed
:
2702 * Failed to allocate fd or security error, free fds
2705 num_installed_fds
= fdi
;
2706 for (fdi
= 0; fdi
< num_installed_fds
; fdi
++)
2707 task_close_fd(target_proc
, fd_array
[fdi
]);
2711 static int binder_fixup_parent(struct binder_transaction
*t
,
2712 struct binder_thread
*thread
,
2713 struct binder_buffer_object
*bp
,
2714 binder_size_t
*off_start
,
2715 binder_size_t num_valid
,
2716 struct binder_buffer_object
*last_fixup_obj
,
2717 binder_size_t last_fixup_min_off
)
2719 struct binder_buffer_object
*parent
;
2721 struct binder_buffer
*b
= t
->buffer
;
2722 struct binder_proc
*proc
= thread
->proc
;
2723 struct binder_proc
*target_proc
= t
->to_proc
;
2725 if (!(bp
->flags
& BINDER_BUFFER_FLAG_HAS_PARENT
))
2728 parent
= binder_validate_ptr(b
, bp
->parent
, off_start
, num_valid
);
2730 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
2731 proc
->pid
, thread
->pid
);
2735 if (!binder_validate_fixup(b
, off_start
,
2736 parent
, bp
->parent_offset
,
2738 last_fixup_min_off
)) {
2739 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
2740 proc
->pid
, thread
->pid
);
2744 if (parent
->length
< sizeof(binder_uintptr_t
) ||
2745 bp
->parent_offset
> parent
->length
- sizeof(binder_uintptr_t
)) {
2746 /* No space for a pointer here! */
2747 binder_user_error("%d:%d got transaction with invalid parent offset\n",
2748 proc
->pid
, thread
->pid
);
2751 parent_buffer
= (u8
*)((uintptr_t)parent
->buffer
-
2752 binder_alloc_get_user_buffer_offset(
2753 &target_proc
->alloc
));
2754 *(binder_uintptr_t
*)(parent_buffer
+ bp
->parent_offset
) = bp
->buffer
;
2760 * binder_proc_transaction() - sends a transaction to a process and wakes it up
2761 * @t: transaction to send
2762 * @proc: process to send the transaction to
2763 * @thread: thread in @proc to send the transaction to (may be NULL)
2765 * This function queues a transaction to the specified process. It will try
2766 * to find a thread in the target process to handle the transaction and
2767 * wake it up. If no thread is found, the work is queued to the proc
2770 * If the @thread parameter is not NULL, the transaction is always queued
2771 * to the waitlist of that specific thread.
2773 * Return: true if the transactions was successfully queued
2774 * false if the target process or thread is dead
2776 static bool binder_proc_transaction(struct binder_transaction
*t
,
2777 struct binder_proc
*proc
,
2778 struct binder_thread
*thread
)
2780 struct binder_node
*node
= t
->buffer
->target_node
;
2781 struct binder_priority node_prio
;
2782 bool oneway
= !!(t
->flags
& TF_ONE_WAY
);
2783 bool pending_async
= false;
2786 binder_node_lock(node
);
2787 node_prio
.prio
= node
->min_priority
;
2788 node_prio
.sched_policy
= node
->sched_policy
;
2792 if (node
->has_async_transaction
) {
2793 pending_async
= true;
2795 node
->has_async_transaction
= 1;
2799 binder_inner_proc_lock(proc
);
2801 if (proc
->is_dead
|| (thread
&& thread
->is_dead
)) {
2802 binder_inner_proc_unlock(proc
);
2803 binder_node_unlock(node
);
2807 if (!thread
&& !pending_async
)
2808 thread
= binder_select_thread_ilocked(proc
);
2811 binder_transaction_priority(thread
->task
, t
, node_prio
,
2813 binder_enqueue_thread_work_ilocked(thread
, &t
->work
);
2814 } else if (!pending_async
) {
2815 binder_enqueue_work_ilocked(&t
->work
, &proc
->todo
);
2817 binder_enqueue_work_ilocked(&t
->work
, &node
->async_todo
);
2821 binder_wakeup_thread_ilocked(proc
, thread
, !oneway
/* sync */);
2823 binder_inner_proc_unlock(proc
);
2824 binder_node_unlock(node
);
2830 * binder_get_node_refs_for_txn() - Get required refs on node for txn
2831 * @node: struct binder_node for which to get refs
2832 * @proc: returns @node->proc if valid
2833 * @error: if no @proc then returns BR_DEAD_REPLY
2835 * User-space normally keeps the node alive when creating a transaction
2836 * since it has a reference to the target. The local strong ref keeps it
2837 * alive if the sending process dies before the target process processes
2838 * the transaction. If the source process is malicious or has a reference
2839 * counting bug, relying on the local strong ref can fail.
2841 * Since user-space can cause the local strong ref to go away, we also take
2842 * a tmpref on the node to ensure it survives while we are constructing
2843 * the transaction. We also need a tmpref on the proc while we are
2844 * constructing the transaction, so we take that here as well.
2846 * Return: The target_node with refs taken or NULL if no @node->proc is NULL.
2847 * Also sets @proc if valid. If the @node->proc is NULL indicating that the
2848 * target proc has died, @error is set to BR_DEAD_REPLY
2850 static struct binder_node
*binder_get_node_refs_for_txn(
2851 struct binder_node
*node
,
2852 struct binder_proc
**procp
,
2855 struct binder_node
*target_node
= NULL
;
2857 binder_node_inner_lock(node
);
2860 binder_inc_node_nilocked(node
, 1, 0, NULL
);
2861 binder_inc_node_tmpref_ilocked(node
);
2862 node
->proc
->tmp_ref
++;
2863 *procp
= node
->proc
;
2865 *error
= BR_DEAD_REPLY
;
2866 binder_node_inner_unlock(node
);
2871 static void binder_transaction(struct binder_proc
*proc
,
2872 struct binder_thread
*thread
,
2873 struct binder_transaction_data
*tr
, int reply
,
2874 binder_size_t extra_buffers_size
)
2877 struct binder_transaction
*t
;
2878 struct binder_work
*tcomplete
;
2879 binder_size_t
*offp
, *off_end
, *off_start
;
2880 binder_size_t off_min
;
2881 u8
*sg_bufp
, *sg_buf_end
;
2882 struct binder_proc
*target_proc
= NULL
;
2883 struct binder_thread
*target_thread
= NULL
;
2884 struct binder_node
*target_node
= NULL
;
2885 struct binder_transaction
*in_reply_to
= NULL
;
2886 struct binder_transaction_log_entry
*e
;
2887 uint32_t return_error
= 0;
2888 uint32_t return_error_param
= 0;
2889 uint32_t return_error_line
= 0;
2890 struct binder_buffer_object
*last_fixup_obj
= NULL
;
2891 binder_size_t last_fixup_min_off
= 0;
2892 struct binder_context
*context
= proc
->context
;
2893 int t_debug_id
= atomic_inc_return(&binder_last_id
);
2895 e
= binder_transaction_log_add(&binder_transaction_log
);
2896 e
->debug_id
= t_debug_id
;
2897 e
->call_type
= reply
? 2 : !!(tr
->flags
& TF_ONE_WAY
);
2898 e
->from_proc
= proc
->pid
;
2899 e
->from_thread
= thread
->pid
;
2900 e
->target_handle
= tr
->target
.handle
;
2901 e
->data_size
= tr
->data_size
;
2902 e
->offsets_size
= tr
->offsets_size
;
2903 e
->context_name
= proc
->context
->name
;
2906 binder_inner_proc_lock(proc
);
2907 in_reply_to
= thread
->transaction_stack
;
2908 if (in_reply_to
== NULL
) {
2909 binder_inner_proc_unlock(proc
);
2910 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
2911 proc
->pid
, thread
->pid
);
2912 return_error
= BR_FAILED_REPLY
;
2913 return_error_param
= -EPROTO
;
2914 return_error_line
= __LINE__
;
2915 goto err_empty_call_stack
;
2917 if (in_reply_to
->to_thread
!= thread
) {
2918 spin_lock(&in_reply_to
->lock
);
2919 binder_user_error("%d:%d got reply transaction with bad transaction stack, transaction %d has target %d:%d\n",
2920 proc
->pid
, thread
->pid
, in_reply_to
->debug_id
,
2921 in_reply_to
->to_proc
?
2922 in_reply_to
->to_proc
->pid
: 0,
2923 in_reply_to
->to_thread
?
2924 in_reply_to
->to_thread
->pid
: 0);
2925 spin_unlock(&in_reply_to
->lock
);
2926 binder_inner_proc_unlock(proc
);
2927 return_error
= BR_FAILED_REPLY
;
2928 return_error_param
= -EPROTO
;
2929 return_error_line
= __LINE__
;
2931 goto err_bad_call_stack
;
2933 thread
->transaction_stack
= in_reply_to
->to_parent
;
2934 binder_inner_proc_unlock(proc
);
2935 target_thread
= binder_get_txn_from_and_acq_inner(in_reply_to
);
2936 if (target_thread
== NULL
) {
2937 return_error
= BR_DEAD_REPLY
;
2938 return_error_line
= __LINE__
;
2939 goto err_dead_binder
;
2941 if (target_thread
->transaction_stack
!= in_reply_to
) {
2942 binder_user_error("%d:%d got reply transaction with bad target transaction stack %d, expected %d\n",
2943 proc
->pid
, thread
->pid
,
2944 target_thread
->transaction_stack
?
2945 target_thread
->transaction_stack
->debug_id
: 0,
2946 in_reply_to
->debug_id
);
2947 binder_inner_proc_unlock(target_thread
->proc
);
2948 return_error
= BR_FAILED_REPLY
;
2949 return_error_param
= -EPROTO
;
2950 return_error_line
= __LINE__
;
2952 target_thread
= NULL
;
2953 goto err_dead_binder
;
2955 target_proc
= target_thread
->proc
;
2956 target_proc
->tmp_ref
++;
2957 binder_inner_proc_unlock(target_thread
->proc
);
2959 if (tr
->target
.handle
) {
2960 struct binder_ref
*ref
;
2963 * There must already be a strong ref
2964 * on this node. If so, do a strong
2965 * increment on the node to ensure it
2966 * stays alive until the transaction is
2969 binder_proc_lock(proc
);
2970 ref
= binder_get_ref_olocked(proc
, tr
->target
.handle
,
2973 target_node
= binder_get_node_refs_for_txn(
2974 ref
->node
, &target_proc
,
2977 binder_user_error("%d:%d got transaction to invalid handle\n",
2978 proc
->pid
, thread
->pid
);
2979 return_error
= BR_FAILED_REPLY
;
2981 binder_proc_unlock(proc
);
2983 mutex_lock(&context
->context_mgr_node_lock
);
2984 target_node
= context
->binder_context_mgr_node
;
2986 target_node
= binder_get_node_refs_for_txn(
2987 target_node
, &target_proc
,
2990 return_error
= BR_DEAD_REPLY
;
2991 mutex_unlock(&context
->context_mgr_node_lock
);
2995 * return_error is set above
2997 return_error_param
= -EINVAL
;
2998 return_error_line
= __LINE__
;
2999 goto err_dead_binder
;
3001 e
->to_node
= target_node
->debug_id
;
3002 if (security_binder_transaction(proc
->tsk
,
3003 target_proc
->tsk
) < 0) {
3004 return_error
= BR_FAILED_REPLY
;
3005 return_error_param
= -EPERM
;
3006 return_error_line
= __LINE__
;
3007 goto err_invalid_target_handle
;
3009 binder_inner_proc_lock(proc
);
3010 if (!(tr
->flags
& TF_ONE_WAY
) && thread
->transaction_stack
) {
3011 struct binder_transaction
*tmp
;
3013 tmp
= thread
->transaction_stack
;
3014 if (tmp
->to_thread
!= thread
) {
3015 spin_lock(&tmp
->lock
);
3016 binder_user_error("%d:%d got new transaction with bad transaction stack, transaction %d has target %d:%d\n",
3017 proc
->pid
, thread
->pid
, tmp
->debug_id
,
3018 tmp
->to_proc
? tmp
->to_proc
->pid
: 0,
3020 tmp
->to_thread
->pid
: 0);
3021 spin_unlock(&tmp
->lock
);
3022 binder_inner_proc_unlock(proc
);
3023 return_error
= BR_FAILED_REPLY
;
3024 return_error_param
= -EPROTO
;
3025 return_error_line
= __LINE__
;
3026 goto err_bad_call_stack
;
3029 struct binder_thread
*from
;
3031 spin_lock(&tmp
->lock
);
3033 if (from
&& from
->proc
== target_proc
) {
3034 atomic_inc(&from
->tmp_ref
);
3035 target_thread
= from
;
3036 spin_unlock(&tmp
->lock
);
3039 spin_unlock(&tmp
->lock
);
3040 tmp
= tmp
->from_parent
;
3043 binder_inner_proc_unlock(proc
);
3046 e
->to_thread
= target_thread
->pid
;
3047 e
->to_proc
= target_proc
->pid
;
3049 /* TODO: reuse incoming transaction for reply */
3050 t
= kzalloc(sizeof(*t
), GFP_KERNEL
);
3052 return_error
= BR_FAILED_REPLY
;
3053 return_error_param
= -ENOMEM
;
3054 return_error_line
= __LINE__
;
3055 goto err_alloc_t_failed
;
3057 binder_stats_created(BINDER_STAT_TRANSACTION
);
3058 spin_lock_init(&t
->lock
);
3060 tcomplete
= kzalloc(sizeof(*tcomplete
), GFP_KERNEL
);
3061 if (tcomplete
== NULL
) {
3062 return_error
= BR_FAILED_REPLY
;
3063 return_error_param
= -ENOMEM
;
3064 return_error_line
= __LINE__
;
3065 goto err_alloc_tcomplete_failed
;
3067 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE
);
3069 t
->debug_id
= t_debug_id
;
3072 binder_debug(BINDER_DEBUG_TRANSACTION
,
3073 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
3074 proc
->pid
, thread
->pid
, t
->debug_id
,
3075 target_proc
->pid
, target_thread
->pid
,
3076 (u64
)tr
->data
.ptr
.buffer
,
3077 (u64
)tr
->data
.ptr
.offsets
,
3078 (u64
)tr
->data_size
, (u64
)tr
->offsets_size
,
3079 (u64
)extra_buffers_size
);
3081 binder_debug(BINDER_DEBUG_TRANSACTION
,
3082 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
3083 proc
->pid
, thread
->pid
, t
->debug_id
,
3084 target_proc
->pid
, target_node
->debug_id
,
3085 (u64
)tr
->data
.ptr
.buffer
,
3086 (u64
)tr
->data
.ptr
.offsets
,
3087 (u64
)tr
->data_size
, (u64
)tr
->offsets_size
,
3088 (u64
)extra_buffers_size
);
3090 if (!reply
&& !(tr
->flags
& TF_ONE_WAY
))
3094 t
->sender_euid
= task_euid(proc
->tsk
);
3095 t
->to_proc
= target_proc
;
3096 t
->to_thread
= target_thread
;
3098 t
->flags
= tr
->flags
;
3099 if (!(t
->flags
& TF_ONE_WAY
) &&
3100 binder_supported_policy(current
->policy
)) {
3101 /* Inherit supported policies for synchronous transactions */
3102 t
->priority
.sched_policy
= current
->policy
;
3103 t
->priority
.prio
= current
->normal_prio
;
3105 /* Otherwise, fall back to the default priority */
3106 t
->priority
= target_proc
->default_priority
;
3109 trace_binder_transaction(reply
, t
, target_node
);
3111 t
->buffer
= binder_alloc_new_buf(&target_proc
->alloc
, tr
->data_size
,
3112 tr
->offsets_size
, extra_buffers_size
,
3113 !reply
&& (t
->flags
& TF_ONE_WAY
));
3114 if (IS_ERR(t
->buffer
)) {
3116 * -ESRCH indicates VMA cleared. The target is dying.
3118 return_error_param
= PTR_ERR(t
->buffer
);
3119 return_error
= return_error_param
== -ESRCH
?
3120 BR_DEAD_REPLY
: BR_FAILED_REPLY
;
3121 return_error_line
= __LINE__
;
3123 goto err_binder_alloc_buf_failed
;
3125 t
->buffer
->allow_user_free
= 0;
3126 t
->buffer
->debug_id
= t
->debug_id
;
3127 t
->buffer
->transaction
= t
;
3128 t
->buffer
->target_node
= target_node
;
3129 trace_binder_transaction_alloc_buf(t
->buffer
);
3130 off_start
= (binder_size_t
*)(t
->buffer
->data
+
3131 ALIGN(tr
->data_size
, sizeof(void *)));
3134 if (copy_from_user(t
->buffer
->data
, (const void __user
*)(uintptr_t)
3135 tr
->data
.ptr
.buffer
, tr
->data_size
)) {
3136 binder_user_error("%d:%d got transaction with invalid data ptr\n",
3137 proc
->pid
, thread
->pid
);
3138 return_error
= BR_FAILED_REPLY
;
3139 return_error_param
= -EFAULT
;
3140 return_error_line
= __LINE__
;
3141 goto err_copy_data_failed
;
3143 if (copy_from_user(offp
, (const void __user
*)(uintptr_t)
3144 tr
->data
.ptr
.offsets
, tr
->offsets_size
)) {
3145 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3146 proc
->pid
, thread
->pid
);
3147 return_error
= BR_FAILED_REPLY
;
3148 return_error_param
= -EFAULT
;
3149 return_error_line
= __LINE__
;
3150 goto err_copy_data_failed
;
3152 if (!IS_ALIGNED(tr
->offsets_size
, sizeof(binder_size_t
))) {
3153 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
3154 proc
->pid
, thread
->pid
, (u64
)tr
->offsets_size
);
3155 return_error
= BR_FAILED_REPLY
;
3156 return_error_param
= -EINVAL
;
3157 return_error_line
= __LINE__
;
3158 goto err_bad_offset
;
3160 if (!IS_ALIGNED(extra_buffers_size
, sizeof(u64
))) {
3161 binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
3162 proc
->pid
, thread
->pid
,
3163 extra_buffers_size
);
3164 return_error
= BR_FAILED_REPLY
;
3165 return_error_param
= -EINVAL
;
3166 return_error_line
= __LINE__
;
3167 goto err_bad_offset
;
3169 off_end
= (void *)off_start
+ tr
->offsets_size
;
3170 sg_bufp
= (u8
*)(PTR_ALIGN(off_end
, sizeof(void *)));
3171 sg_buf_end
= sg_bufp
+ extra_buffers_size
;
3173 for (; offp
< off_end
; offp
++) {
3174 struct binder_object_header
*hdr
;
3175 size_t object_size
= binder_validate_object(t
->buffer
, *offp
);
3177 if (object_size
== 0 || *offp
< off_min
) {
3178 binder_user_error("%d:%d got transaction with invalid offset (%lld, min %lld max %lld) or object.\n",
3179 proc
->pid
, thread
->pid
, (u64
)*offp
,
3181 (u64
)t
->buffer
->data_size
);
3182 return_error
= BR_FAILED_REPLY
;
3183 return_error_param
= -EINVAL
;
3184 return_error_line
= __LINE__
;
3185 goto err_bad_offset
;
3188 hdr
= (struct binder_object_header
*)(t
->buffer
->data
+ *offp
);
3189 off_min
= *offp
+ object_size
;
3190 switch (hdr
->type
) {
3191 case BINDER_TYPE_BINDER
:
3192 case BINDER_TYPE_WEAK_BINDER
: {
3193 struct flat_binder_object
*fp
;
3195 fp
= to_flat_binder_object(hdr
);
3196 ret
= binder_translate_binder(fp
, t
, thread
);
3198 return_error
= BR_FAILED_REPLY
;
3199 return_error_param
= ret
;
3200 return_error_line
= __LINE__
;
3201 goto err_translate_failed
;
3204 case BINDER_TYPE_HANDLE
:
3205 case BINDER_TYPE_WEAK_HANDLE
: {
3206 struct flat_binder_object
*fp
;
3208 fp
= to_flat_binder_object(hdr
);
3209 ret
= binder_translate_handle(fp
, t
, thread
);
3211 return_error
= BR_FAILED_REPLY
;
3212 return_error_param
= ret
;
3213 return_error_line
= __LINE__
;
3214 goto err_translate_failed
;
3218 case BINDER_TYPE_FD
: {
3219 struct binder_fd_object
*fp
= to_binder_fd_object(hdr
);
3220 int target_fd
= binder_translate_fd(fp
->fd
, t
, thread
,
3223 if (target_fd
< 0) {
3224 return_error
= BR_FAILED_REPLY
;
3225 return_error_param
= target_fd
;
3226 return_error_line
= __LINE__
;
3227 goto err_translate_failed
;
3232 case BINDER_TYPE_FDA
: {
3233 struct binder_fd_array_object
*fda
=
3234 to_binder_fd_array_object(hdr
);
3235 struct binder_buffer_object
*parent
=
3236 binder_validate_ptr(t
->buffer
, fda
->parent
,
3240 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
3241 proc
->pid
, thread
->pid
);
3242 return_error
= BR_FAILED_REPLY
;
3243 return_error_param
= -EINVAL
;
3244 return_error_line
= __LINE__
;
3245 goto err_bad_parent
;
3247 if (!binder_validate_fixup(t
->buffer
, off_start
,
3248 parent
, fda
->parent_offset
,
3250 last_fixup_min_off
)) {
3251 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
3252 proc
->pid
, thread
->pid
);
3253 return_error
= BR_FAILED_REPLY
;
3254 return_error_param
= -EINVAL
;
3255 return_error_line
= __LINE__
;
3256 goto err_bad_parent
;
3258 ret
= binder_translate_fd_array(fda
, parent
, t
, thread
,
3261 return_error
= BR_FAILED_REPLY
;
3262 return_error_param
= ret
;
3263 return_error_line
= __LINE__
;
3264 goto err_translate_failed
;
3266 last_fixup_obj
= parent
;
3267 last_fixup_min_off
=
3268 fda
->parent_offset
+ sizeof(u32
) * fda
->num_fds
;
3270 case BINDER_TYPE_PTR
: {
3271 struct binder_buffer_object
*bp
=
3272 to_binder_buffer_object(hdr
);
3273 size_t buf_left
= sg_buf_end
- sg_bufp
;
3275 if (bp
->length
> buf_left
) {
3276 binder_user_error("%d:%d got transaction with too large buffer\n",
3277 proc
->pid
, thread
->pid
);
3278 return_error
= BR_FAILED_REPLY
;
3279 return_error_param
= -EINVAL
;
3280 return_error_line
= __LINE__
;
3281 goto err_bad_offset
;
3283 if (copy_from_user(sg_bufp
,
3284 (const void __user
*)(uintptr_t)
3285 bp
->buffer
, bp
->length
)) {
3286 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3287 proc
->pid
, thread
->pid
);
3288 return_error_param
= -EFAULT
;
3289 return_error
= BR_FAILED_REPLY
;
3290 return_error_line
= __LINE__
;
3291 goto err_copy_data_failed
;
3293 /* Fixup buffer pointer to target proc address space */
3294 bp
->buffer
= (uintptr_t)sg_bufp
+
3295 binder_alloc_get_user_buffer_offset(
3296 &target_proc
->alloc
);
3297 sg_bufp
+= ALIGN(bp
->length
, sizeof(u64
));
3299 ret
= binder_fixup_parent(t
, thread
, bp
, off_start
,
3302 last_fixup_min_off
);
3304 return_error
= BR_FAILED_REPLY
;
3305 return_error_param
= ret
;
3306 return_error_line
= __LINE__
;
3307 goto err_translate_failed
;
3309 last_fixup_obj
= bp
;
3310 last_fixup_min_off
= 0;
3313 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
3314 proc
->pid
, thread
->pid
, hdr
->type
);
3315 return_error
= BR_FAILED_REPLY
;
3316 return_error_param
= -EINVAL
;
3317 return_error_line
= __LINE__
;
3318 goto err_bad_object_type
;
3321 tcomplete
->type
= BINDER_WORK_TRANSACTION_COMPLETE
;
3322 t
->work
.type
= BINDER_WORK_TRANSACTION
;
3325 binder_enqueue_thread_work(thread
, tcomplete
);
3326 binder_inner_proc_lock(target_proc
);
3327 if (target_thread
->is_dead
) {
3328 binder_inner_proc_unlock(target_proc
);
3329 goto err_dead_proc_or_thread
;
3331 BUG_ON(t
->buffer
->async_transaction
!= 0);
3332 binder_pop_transaction_ilocked(target_thread
, in_reply_to
);
3333 binder_enqueue_thread_work_ilocked(target_thread
, &t
->work
);
3334 binder_inner_proc_unlock(target_proc
);
3335 wake_up_interruptible_sync(&target_thread
->wait
);
3336 binder_restore_priority(current
, in_reply_to
->saved_priority
);
3337 binder_free_transaction(in_reply_to
);
3338 } else if (!(t
->flags
& TF_ONE_WAY
)) {
3339 BUG_ON(t
->buffer
->async_transaction
!= 0);
3340 binder_inner_proc_lock(proc
);
3342 * Defer the TRANSACTION_COMPLETE, so we don't return to
3343 * userspace immediately; this allows the target process to
3344 * immediately start processing this transaction, reducing
3345 * latency. We will then return the TRANSACTION_COMPLETE when
3346 * the target replies (or there is an error).
3348 binder_enqueue_deferred_thread_work_ilocked(thread
, tcomplete
);
3350 t
->from_parent
= thread
->transaction_stack
;
3351 thread
->transaction_stack
= t
;
3352 binder_inner_proc_unlock(proc
);
3353 if (!binder_proc_transaction(t
, target_proc
, target_thread
)) {
3354 binder_inner_proc_lock(proc
);
3355 binder_pop_transaction_ilocked(thread
, t
);
3356 binder_inner_proc_unlock(proc
);
3357 goto err_dead_proc_or_thread
;
3360 BUG_ON(target_node
== NULL
);
3361 BUG_ON(t
->buffer
->async_transaction
!= 1);
3362 binder_enqueue_thread_work(thread
, tcomplete
);
3363 if (!binder_proc_transaction(t
, target_proc
, NULL
))
3364 goto err_dead_proc_or_thread
;
3367 binder_thread_dec_tmpref(target_thread
);
3368 binder_proc_dec_tmpref(target_proc
);
3370 binder_dec_node_tmpref(target_node
);
3372 * write barrier to synchronize with initialization
3376 WRITE_ONCE(e
->debug_id_done
, t_debug_id
);
3379 err_dead_proc_or_thread
:
3380 return_error
= BR_DEAD_REPLY
;
3381 return_error_line
= __LINE__
;
3382 binder_dequeue_work(proc
, tcomplete
);
3383 err_translate_failed
:
3384 err_bad_object_type
:
3387 err_copy_data_failed
:
3388 trace_binder_transaction_failed_buffer_release(t
->buffer
);
3389 binder_transaction_buffer_release(target_proc
, t
->buffer
, offp
);
3391 binder_dec_node_tmpref(target_node
);
3393 t
->buffer
->transaction
= NULL
;
3394 binder_alloc_free_buf(&target_proc
->alloc
, t
->buffer
);
3395 err_binder_alloc_buf_failed
:
3397 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE
);
3398 err_alloc_tcomplete_failed
:
3400 binder_stats_deleted(BINDER_STAT_TRANSACTION
);
3403 err_empty_call_stack
:
3405 err_invalid_target_handle
:
3407 binder_thread_dec_tmpref(target_thread
);
3409 binder_proc_dec_tmpref(target_proc
);
3411 binder_dec_node(target_node
, 1, 0);
3412 binder_dec_node_tmpref(target_node
);
3415 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
3416 "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
3417 proc
->pid
, thread
->pid
, return_error
, return_error_param
,
3418 (u64
)tr
->data_size
, (u64
)tr
->offsets_size
,
3422 struct binder_transaction_log_entry
*fe
;
3424 e
->return_error
= return_error
;
3425 e
->return_error_param
= return_error_param
;
3426 e
->return_error_line
= return_error_line
;
3427 fe
= binder_transaction_log_add(&binder_transaction_log_failed
);
3430 * write barrier to synchronize with initialization
3434 WRITE_ONCE(e
->debug_id_done
, t_debug_id
);
3435 WRITE_ONCE(fe
->debug_id_done
, t_debug_id
);
3438 BUG_ON(thread
->return_error
.cmd
!= BR_OK
);
3440 binder_restore_priority(current
, in_reply_to
->saved_priority
);
3441 thread
->return_error
.cmd
= BR_TRANSACTION_COMPLETE
;
3442 binder_enqueue_thread_work(thread
, &thread
->return_error
.work
);
3443 binder_send_failed_reply(in_reply_to
, return_error
);
3445 thread
->return_error
.cmd
= return_error
;
3446 binder_enqueue_thread_work(thread
, &thread
->return_error
.work
);
3450 int binder_thread_write(struct binder_proc
*proc
,
3451 struct binder_thread
*thread
,
3452 binder_uintptr_t binder_buffer
, size_t size
,
3453 binder_size_t
*consumed
)
3456 struct binder_context
*context
= proc
->context
;
3457 void __user
*buffer
= (void __user
*)(uintptr_t)binder_buffer
;
3458 void __user
*ptr
= buffer
+ *consumed
;
3459 void __user
*end
= buffer
+ size
;
3461 while (ptr
< end
&& thread
->return_error
.cmd
== BR_OK
) {
3464 if (get_user(cmd
, (uint32_t __user
*)ptr
))
3466 ptr
+= sizeof(uint32_t);
3467 trace_binder_command(cmd
);
3468 if (_IOC_NR(cmd
) < ARRAY_SIZE(binder_stats
.bc
)) {
3469 atomic_inc(&binder_stats
.bc
[_IOC_NR(cmd
)]);
3470 atomic_inc(&proc
->stats
.bc
[_IOC_NR(cmd
)]);
3471 atomic_inc(&thread
->stats
.bc
[_IOC_NR(cmd
)]);
3479 const char *debug_string
;
3480 bool strong
= cmd
== BC_ACQUIRE
|| cmd
== BC_RELEASE
;
3481 bool increment
= cmd
== BC_INCREFS
|| cmd
== BC_ACQUIRE
;
3482 struct binder_ref_data rdata
;
3484 if (get_user(target
, (uint32_t __user
*)ptr
))
3487 ptr
+= sizeof(uint32_t);
3489 if (increment
&& !target
) {
3490 struct binder_node
*ctx_mgr_node
;
3491 mutex_lock(&context
->context_mgr_node_lock
);
3492 ctx_mgr_node
= context
->binder_context_mgr_node
;
3494 ret
= binder_inc_ref_for_node(
3496 strong
, NULL
, &rdata
);
3497 mutex_unlock(&context
->context_mgr_node_lock
);
3500 ret
= binder_update_ref_for_handle(
3501 proc
, target
, increment
, strong
,
3503 if (!ret
&& rdata
.desc
!= target
) {
3504 binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
3505 proc
->pid
, thread
->pid
,
3506 target
, rdata
.desc
);
3510 debug_string
= "IncRefs";
3513 debug_string
= "Acquire";
3516 debug_string
= "Release";
3520 debug_string
= "DecRefs";
3524 binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
3525 proc
->pid
, thread
->pid
, debug_string
,
3526 strong
, target
, ret
);
3529 binder_debug(BINDER_DEBUG_USER_REFS
,
3530 "%d:%d %s ref %d desc %d s %d w %d\n",
3531 proc
->pid
, thread
->pid
, debug_string
,
3532 rdata
.debug_id
, rdata
.desc
, rdata
.strong
,
3536 case BC_INCREFS_DONE
:
3537 case BC_ACQUIRE_DONE
: {
3538 binder_uintptr_t node_ptr
;
3539 binder_uintptr_t cookie
;
3540 struct binder_node
*node
;
3543 if (get_user(node_ptr
, (binder_uintptr_t __user
*)ptr
))
3545 ptr
+= sizeof(binder_uintptr_t
);
3546 if (get_user(cookie
, (binder_uintptr_t __user
*)ptr
))
3548 ptr
+= sizeof(binder_uintptr_t
);
3549 node
= binder_get_node(proc
, node_ptr
);
3551 binder_user_error("%d:%d %s u%016llx no match\n",
3552 proc
->pid
, thread
->pid
,
3553 cmd
== BC_INCREFS_DONE
?
3559 if (cookie
!= node
->cookie
) {
3560 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
3561 proc
->pid
, thread
->pid
,
3562 cmd
== BC_INCREFS_DONE
?
3563 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
3564 (u64
)node_ptr
, node
->debug_id
,
3565 (u64
)cookie
, (u64
)node
->cookie
);
3566 binder_put_node(node
);
3569 binder_node_inner_lock(node
);
3570 if (cmd
== BC_ACQUIRE_DONE
) {
3571 if (node
->pending_strong_ref
== 0) {
3572 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
3573 proc
->pid
, thread
->pid
,
3575 binder_node_inner_unlock(node
);
3576 binder_put_node(node
);
3579 node
->pending_strong_ref
= 0;
3581 if (node
->pending_weak_ref
== 0) {
3582 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
3583 proc
->pid
, thread
->pid
,
3585 binder_node_inner_unlock(node
);
3586 binder_put_node(node
);
3589 node
->pending_weak_ref
= 0;
3591 free_node
= binder_dec_node_nilocked(node
,
3592 cmd
== BC_ACQUIRE_DONE
, 0);
3594 binder_debug(BINDER_DEBUG_USER_REFS
,
3595 "%d:%d %s node %d ls %d lw %d tr %d\n",
3596 proc
->pid
, thread
->pid
,
3597 cmd
== BC_INCREFS_DONE
? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
3598 node
->debug_id
, node
->local_strong_refs
,
3599 node
->local_weak_refs
, node
->tmp_refs
);
3600 binder_node_inner_unlock(node
);
3601 binder_put_node(node
);
3604 case BC_ATTEMPT_ACQUIRE
:
3605 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
3607 case BC_ACQUIRE_RESULT
:
3608 pr_err("BC_ACQUIRE_RESULT not supported\n");
3611 case BC_FREE_BUFFER
: {
3612 binder_uintptr_t data_ptr
;
3613 struct binder_buffer
*buffer
;
3615 if (get_user(data_ptr
, (binder_uintptr_t __user
*)ptr
))
3617 ptr
+= sizeof(binder_uintptr_t
);
3619 buffer
= binder_alloc_prepare_to_free(&proc
->alloc
,
3621 if (buffer
== NULL
) {
3622 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
3623 proc
->pid
, thread
->pid
, (u64
)data_ptr
);
3626 if (!buffer
->allow_user_free
) {
3627 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
3628 proc
->pid
, thread
->pid
, (u64
)data_ptr
);
3631 binder_debug(BINDER_DEBUG_FREE_BUFFER
,
3632 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
3633 proc
->pid
, thread
->pid
, (u64
)data_ptr
,
3635 buffer
->transaction
? "active" : "finished");
3637 if (buffer
->transaction
) {
3638 buffer
->transaction
->buffer
= NULL
;
3639 buffer
->transaction
= NULL
;
3641 if (buffer
->async_transaction
&& buffer
->target_node
) {
3642 struct binder_node
*buf_node
;
3643 struct binder_work
*w
;
3645 buf_node
= buffer
->target_node
;
3646 binder_node_inner_lock(buf_node
);
3647 BUG_ON(!buf_node
->has_async_transaction
);
3648 BUG_ON(buf_node
->proc
!= proc
);
3649 w
= binder_dequeue_work_head_ilocked(
3650 &buf_node
->async_todo
);
3652 buf_node
->has_async_transaction
= 0;
3654 binder_enqueue_work_ilocked(
3656 binder_wakeup_proc_ilocked(proc
);
3658 binder_node_inner_unlock(buf_node
);
3660 trace_binder_transaction_buffer_release(buffer
);
3661 binder_transaction_buffer_release(proc
, buffer
, NULL
);
3662 binder_alloc_free_buf(&proc
->alloc
, buffer
);
3666 case BC_TRANSACTION_SG
:
3668 struct binder_transaction_data_sg tr
;
3670 if (copy_from_user(&tr
, ptr
, sizeof(tr
)))
3673 binder_transaction(proc
, thread
, &tr
.transaction_data
,
3674 cmd
== BC_REPLY_SG
, tr
.buffers_size
);
3677 case BC_TRANSACTION
:
3679 struct binder_transaction_data tr
;
3681 if (copy_from_user(&tr
, ptr
, sizeof(tr
)))
3684 binder_transaction(proc
, thread
, &tr
,
3685 cmd
== BC_REPLY
, 0);
3689 case BC_REGISTER_LOOPER
:
3690 binder_debug(BINDER_DEBUG_THREADS
,
3691 "%d:%d BC_REGISTER_LOOPER\n",
3692 proc
->pid
, thread
->pid
);
3693 binder_inner_proc_lock(proc
);
3694 if (thread
->looper
& BINDER_LOOPER_STATE_ENTERED
) {
3695 thread
->looper
|= BINDER_LOOPER_STATE_INVALID
;
3696 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
3697 proc
->pid
, thread
->pid
);
3698 } else if (proc
->requested_threads
== 0) {
3699 thread
->looper
|= BINDER_LOOPER_STATE_INVALID
;
3700 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
3701 proc
->pid
, thread
->pid
);
3703 proc
->requested_threads
--;
3704 proc
->requested_threads_started
++;
3706 thread
->looper
|= BINDER_LOOPER_STATE_REGISTERED
;
3707 binder_inner_proc_unlock(proc
);
3709 case BC_ENTER_LOOPER
:
3710 binder_debug(BINDER_DEBUG_THREADS
,
3711 "%d:%d BC_ENTER_LOOPER\n",
3712 proc
->pid
, thread
->pid
);
3713 if (thread
->looper
& BINDER_LOOPER_STATE_REGISTERED
) {
3714 thread
->looper
|= BINDER_LOOPER_STATE_INVALID
;
3715 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
3716 proc
->pid
, thread
->pid
);
3718 thread
->looper
|= BINDER_LOOPER_STATE_ENTERED
;
3720 case BC_EXIT_LOOPER
:
3721 binder_debug(BINDER_DEBUG_THREADS
,
3722 "%d:%d BC_EXIT_LOOPER\n",
3723 proc
->pid
, thread
->pid
);
3724 thread
->looper
|= BINDER_LOOPER_STATE_EXITED
;
3727 case BC_REQUEST_DEATH_NOTIFICATION
:
3728 case BC_CLEAR_DEATH_NOTIFICATION
: {
3730 binder_uintptr_t cookie
;
3731 struct binder_ref
*ref
;
3732 struct binder_ref_death
*death
= NULL
;
3734 if (get_user(target
, (uint32_t __user
*)ptr
))
3736 ptr
+= sizeof(uint32_t);
3737 if (get_user(cookie
, (binder_uintptr_t __user
*)ptr
))
3739 ptr
+= sizeof(binder_uintptr_t
);
3740 if (cmd
== BC_REQUEST_DEATH_NOTIFICATION
) {
3742 * Allocate memory for death notification
3743 * before taking lock
3745 death
= kzalloc(sizeof(*death
), GFP_KERNEL
);
3746 if (death
== NULL
) {
3747 WARN_ON(thread
->return_error
.cmd
!=
3749 thread
->return_error
.cmd
= BR_ERROR
;
3750 binder_enqueue_thread_work(
3752 &thread
->return_error
.work
);
3754 BINDER_DEBUG_FAILED_TRANSACTION
,
3755 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
3756 proc
->pid
, thread
->pid
);
3760 binder_proc_lock(proc
);
3761 ref
= binder_get_ref_olocked(proc
, target
, false);
3763 binder_user_error("%d:%d %s invalid ref %d\n",
3764 proc
->pid
, thread
->pid
,
3765 cmd
== BC_REQUEST_DEATH_NOTIFICATION
?
3766 "BC_REQUEST_DEATH_NOTIFICATION" :
3767 "BC_CLEAR_DEATH_NOTIFICATION",
3769 binder_proc_unlock(proc
);
3774 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION
,
3775 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
3776 proc
->pid
, thread
->pid
,
3777 cmd
== BC_REQUEST_DEATH_NOTIFICATION
?
3778 "BC_REQUEST_DEATH_NOTIFICATION" :
3779 "BC_CLEAR_DEATH_NOTIFICATION",
3780 (u64
)cookie
, ref
->data
.debug_id
,
3781 ref
->data
.desc
, ref
->data
.strong
,
3782 ref
->data
.weak
, ref
->node
->debug_id
);
3784 binder_node_lock(ref
->node
);
3785 if (cmd
== BC_REQUEST_DEATH_NOTIFICATION
) {
3787 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
3788 proc
->pid
, thread
->pid
);
3789 binder_node_unlock(ref
->node
);
3790 binder_proc_unlock(proc
);
3794 binder_stats_created(BINDER_STAT_DEATH
);
3795 INIT_LIST_HEAD(&death
->work
.entry
);
3796 death
->cookie
= cookie
;
3798 if (ref
->node
->proc
== NULL
) {
3799 ref
->death
->work
.type
= BINDER_WORK_DEAD_BINDER
;
3801 binder_inner_proc_lock(proc
);
3802 binder_enqueue_work_ilocked(
3803 &ref
->death
->work
, &proc
->todo
);
3804 binder_wakeup_proc_ilocked(proc
);
3805 binder_inner_proc_unlock(proc
);
3808 if (ref
->death
== NULL
) {
3809 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
3810 proc
->pid
, thread
->pid
);
3811 binder_node_unlock(ref
->node
);
3812 binder_proc_unlock(proc
);
3816 if (death
->cookie
!= cookie
) {
3817 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
3818 proc
->pid
, thread
->pid
,
3821 binder_node_unlock(ref
->node
);
3822 binder_proc_unlock(proc
);
3826 binder_inner_proc_lock(proc
);
3827 if (list_empty(&death
->work
.entry
)) {
3828 death
->work
.type
= BINDER_WORK_CLEAR_DEATH_NOTIFICATION
;
3829 if (thread
->looper
&
3830 (BINDER_LOOPER_STATE_REGISTERED
|
3831 BINDER_LOOPER_STATE_ENTERED
))
3832 binder_enqueue_thread_work_ilocked(
3836 binder_enqueue_work_ilocked(
3839 binder_wakeup_proc_ilocked(
3843 BUG_ON(death
->work
.type
!= BINDER_WORK_DEAD_BINDER
);
3844 death
->work
.type
= BINDER_WORK_DEAD_BINDER_AND_CLEAR
;
3846 binder_inner_proc_unlock(proc
);
3848 binder_node_unlock(ref
->node
);
3849 binder_proc_unlock(proc
);
3851 case BC_DEAD_BINDER_DONE
: {
3852 struct binder_work
*w
;
3853 binder_uintptr_t cookie
;
3854 struct binder_ref_death
*death
= NULL
;
3856 if (get_user(cookie
, (binder_uintptr_t __user
*)ptr
))
3859 ptr
+= sizeof(cookie
);
3860 binder_inner_proc_lock(proc
);
3861 list_for_each_entry(w
, &proc
->delivered_death
,
3863 struct binder_ref_death
*tmp_death
=
3865 struct binder_ref_death
,
3868 if (tmp_death
->cookie
== cookie
) {
3873 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
3874 "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n",
3875 proc
->pid
, thread
->pid
, (u64
)cookie
,
3877 if (death
== NULL
) {
3878 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
3879 proc
->pid
, thread
->pid
, (u64
)cookie
);
3880 binder_inner_proc_unlock(proc
);
3883 binder_dequeue_work_ilocked(&death
->work
);
3884 if (death
->work
.type
== BINDER_WORK_DEAD_BINDER_AND_CLEAR
) {
3885 death
->work
.type
= BINDER_WORK_CLEAR_DEATH_NOTIFICATION
;
3886 if (thread
->looper
&
3887 (BINDER_LOOPER_STATE_REGISTERED
|
3888 BINDER_LOOPER_STATE_ENTERED
))
3889 binder_enqueue_thread_work_ilocked(
3890 thread
, &death
->work
);
3892 binder_enqueue_work_ilocked(
3895 binder_wakeup_proc_ilocked(proc
);
3898 binder_inner_proc_unlock(proc
);
3902 pr_err("%d:%d unknown command %d\n",
3903 proc
->pid
, thread
->pid
, cmd
);
3906 *consumed
= ptr
- buffer
;
3911 static void binder_stat_br(struct binder_proc
*proc
,
3912 struct binder_thread
*thread
, uint32_t cmd
)
3914 trace_binder_return(cmd
);
3915 if (_IOC_NR(cmd
) < ARRAY_SIZE(binder_stats
.br
)) {
3916 atomic_inc(&binder_stats
.br
[_IOC_NR(cmd
)]);
3917 atomic_inc(&proc
->stats
.br
[_IOC_NR(cmd
)]);
3918 atomic_inc(&thread
->stats
.br
[_IOC_NR(cmd
)]);
3922 static int binder_put_node_cmd(struct binder_proc
*proc
,
3923 struct binder_thread
*thread
,
3925 binder_uintptr_t node_ptr
,
3926 binder_uintptr_t node_cookie
,
3928 uint32_t cmd
, const char *cmd_name
)
3930 void __user
*ptr
= *ptrp
;
3932 if (put_user(cmd
, (uint32_t __user
*)ptr
))
3934 ptr
+= sizeof(uint32_t);
3936 if (put_user(node_ptr
, (binder_uintptr_t __user
*)ptr
))
3938 ptr
+= sizeof(binder_uintptr_t
);
3940 if (put_user(node_cookie
, (binder_uintptr_t __user
*)ptr
))
3942 ptr
+= sizeof(binder_uintptr_t
);
3944 binder_stat_br(proc
, thread
, cmd
);
3945 binder_debug(BINDER_DEBUG_USER_REFS
, "%d:%d %s %d u%016llx c%016llx\n",
3946 proc
->pid
, thread
->pid
, cmd_name
, node_debug_id
,
3947 (u64
)node_ptr
, (u64
)node_cookie
);
3953 static int binder_wait_for_work(struct binder_thread
*thread
,
3957 struct binder_proc
*proc
= thread
->proc
;
3960 freezer_do_not_count();
3961 binder_inner_proc_lock(proc
);
3963 prepare_to_wait(&thread
->wait
, &wait
, TASK_INTERRUPTIBLE
);
3964 if (binder_has_work_ilocked(thread
, do_proc_work
))
3967 list_add(&thread
->waiting_thread_node
,
3968 &proc
->waiting_threads
);
3969 binder_inner_proc_unlock(proc
);
3971 binder_inner_proc_lock(proc
);
3972 list_del_init(&thread
->waiting_thread_node
);
3973 if (signal_pending(current
)) {
3978 finish_wait(&thread
->wait
, &wait
);
3979 binder_inner_proc_unlock(proc
);
3985 static int binder_thread_read(struct binder_proc
*proc
,
3986 struct binder_thread
*thread
,
3987 binder_uintptr_t binder_buffer
, size_t size
,
3988 binder_size_t
*consumed
, int non_block
)
3990 void __user
*buffer
= (void __user
*)(uintptr_t)binder_buffer
;
3991 void __user
*ptr
= buffer
+ *consumed
;
3992 void __user
*end
= buffer
+ size
;
3995 int wait_for_proc_work
;
3997 if (*consumed
== 0) {
3998 if (put_user(BR_NOOP
, (uint32_t __user
*)ptr
))
4000 ptr
+= sizeof(uint32_t);
4004 binder_inner_proc_lock(proc
);
4005 wait_for_proc_work
= binder_available_for_proc_work_ilocked(thread
);
4006 binder_inner_proc_unlock(proc
);
4008 thread
->looper
|= BINDER_LOOPER_STATE_WAITING
;
4010 trace_binder_wait_for_work(wait_for_proc_work
,
4011 !!thread
->transaction_stack
,
4012 !binder_worklist_empty(proc
, &thread
->todo
));
4013 if (wait_for_proc_work
) {
4014 if (!(thread
->looper
& (BINDER_LOOPER_STATE_REGISTERED
|
4015 BINDER_LOOPER_STATE_ENTERED
))) {
4016 binder_user_error("%d:%d ERROR: Thread waiting for process work before calling BC_REGISTER_LOOPER or BC_ENTER_LOOPER (state %x)\n",
4017 proc
->pid
, thread
->pid
, thread
->looper
);
4018 wait_event_interruptible(binder_user_error_wait
,
4019 binder_stop_on_user_error
< 2);
4021 binder_restore_priority(current
, proc
->default_priority
);
4025 if (!binder_has_work(thread
, wait_for_proc_work
))
4028 ret
= binder_wait_for_work(thread
, wait_for_proc_work
);
4031 thread
->looper
&= ~BINDER_LOOPER_STATE_WAITING
;
4038 struct binder_transaction_data tr
;
4039 struct binder_work
*w
= NULL
;
4040 struct list_head
*list
= NULL
;
4041 struct binder_transaction
*t
= NULL
;
4042 struct binder_thread
*t_from
;
4044 binder_inner_proc_lock(proc
);
4045 if (!binder_worklist_empty_ilocked(&thread
->todo
))
4046 list
= &thread
->todo
;
4047 else if (!binder_worklist_empty_ilocked(&proc
->todo
) &&
4051 binder_inner_proc_unlock(proc
);
4054 if (ptr
- buffer
== 4 && !thread
->looper_need_return
)
4059 if (end
- ptr
< sizeof(tr
) + 4) {
4060 binder_inner_proc_unlock(proc
);
4063 w
= binder_dequeue_work_head_ilocked(list
);
4064 if (binder_worklist_empty_ilocked(&thread
->todo
))
4065 thread
->process_todo
= false;
4068 case BINDER_WORK_TRANSACTION
: {
4069 binder_inner_proc_unlock(proc
);
4070 t
= container_of(w
, struct binder_transaction
, work
);
4072 case BINDER_WORK_RETURN_ERROR
: {
4073 struct binder_error
*e
= container_of(
4074 w
, struct binder_error
, work
);
4076 WARN_ON(e
->cmd
== BR_OK
);
4077 binder_inner_proc_unlock(proc
);
4078 if (put_user(e
->cmd
, (uint32_t __user
*)ptr
))
4081 ptr
+= sizeof(uint32_t);
4083 binder_stat_br(proc
, thread
, cmd
);
4085 case BINDER_WORK_TRANSACTION_COMPLETE
: {
4086 binder_inner_proc_unlock(proc
);
4087 cmd
= BR_TRANSACTION_COMPLETE
;
4088 if (put_user(cmd
, (uint32_t __user
*)ptr
))
4090 ptr
+= sizeof(uint32_t);
4092 binder_stat_br(proc
, thread
, cmd
);
4093 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE
,
4094 "%d:%d BR_TRANSACTION_COMPLETE\n",
4095 proc
->pid
, thread
->pid
);
4097 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE
);
4099 case BINDER_WORK_NODE
: {
4100 struct binder_node
*node
= container_of(w
, struct binder_node
, work
);
4102 binder_uintptr_t node_ptr
= node
->ptr
;
4103 binder_uintptr_t node_cookie
= node
->cookie
;
4104 int node_debug_id
= node
->debug_id
;
4107 void __user
*orig_ptr
= ptr
;
4109 BUG_ON(proc
!= node
->proc
);
4110 strong
= node
->internal_strong_refs
||
4111 node
->local_strong_refs
;
4112 weak
= !hlist_empty(&node
->refs
) ||
4113 node
->local_weak_refs
||
4114 node
->tmp_refs
|| strong
;
4115 has_strong_ref
= node
->has_strong_ref
;
4116 has_weak_ref
= node
->has_weak_ref
;
4118 if (weak
&& !has_weak_ref
) {
4119 node
->has_weak_ref
= 1;
4120 node
->pending_weak_ref
= 1;
4121 node
->local_weak_refs
++;
4123 if (strong
&& !has_strong_ref
) {
4124 node
->has_strong_ref
= 1;
4125 node
->pending_strong_ref
= 1;
4126 node
->local_strong_refs
++;
4128 if (!strong
&& has_strong_ref
)
4129 node
->has_strong_ref
= 0;
4130 if (!weak
&& has_weak_ref
)
4131 node
->has_weak_ref
= 0;
4132 if (!weak
&& !strong
) {
4133 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
4134 "%d:%d node %d u%016llx c%016llx deleted\n",
4135 proc
->pid
, thread
->pid
,
4139 rb_erase(&node
->rb_node
, &proc
->nodes
);
4140 binder_inner_proc_unlock(proc
);
4141 binder_node_lock(node
);
4143 * Acquire the node lock before freeing the
4144 * node to serialize with other threads that
4145 * may have been holding the node lock while
4146 * decrementing this node (avoids race where
4147 * this thread frees while the other thread
4148 * is unlocking the node after the final
4151 binder_node_unlock(node
);
4152 binder_free_node(node
);
4154 binder_inner_proc_unlock(proc
);
4156 if (weak
&& !has_weak_ref
)
4157 ret
= binder_put_node_cmd(
4158 proc
, thread
, &ptr
, node_ptr
,
4159 node_cookie
, node_debug_id
,
4160 BR_INCREFS
, "BR_INCREFS");
4161 if (!ret
&& strong
&& !has_strong_ref
)
4162 ret
= binder_put_node_cmd(
4163 proc
, thread
, &ptr
, node_ptr
,
4164 node_cookie
, node_debug_id
,
4165 BR_ACQUIRE
, "BR_ACQUIRE");
4166 if (!ret
&& !strong
&& has_strong_ref
)
4167 ret
= binder_put_node_cmd(
4168 proc
, thread
, &ptr
, node_ptr
,
4169 node_cookie
, node_debug_id
,
4170 BR_RELEASE
, "BR_RELEASE");
4171 if (!ret
&& !weak
&& has_weak_ref
)
4172 ret
= binder_put_node_cmd(
4173 proc
, thread
, &ptr
, node_ptr
,
4174 node_cookie
, node_debug_id
,
4175 BR_DECREFS
, "BR_DECREFS");
4176 if (orig_ptr
== ptr
)
4177 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
4178 "%d:%d node %d u%016llx c%016llx state unchanged\n",
4179 proc
->pid
, thread
->pid
,
4186 case BINDER_WORK_DEAD_BINDER
:
4187 case BINDER_WORK_DEAD_BINDER_AND_CLEAR
:
4188 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION
: {
4189 struct binder_ref_death
*death
;
4191 binder_uintptr_t cookie
;
4193 death
= container_of(w
, struct binder_ref_death
, work
);
4194 if (w
->type
== BINDER_WORK_CLEAR_DEATH_NOTIFICATION
)
4195 cmd
= BR_CLEAR_DEATH_NOTIFICATION_DONE
;
4197 cmd
= BR_DEAD_BINDER
;
4198 cookie
= death
->cookie
;
4200 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION
,
4201 "%d:%d %s %016llx\n",
4202 proc
->pid
, thread
->pid
,
4203 cmd
== BR_DEAD_BINDER
?
4205 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
4207 if (w
->type
== BINDER_WORK_CLEAR_DEATH_NOTIFICATION
) {
4208 binder_inner_proc_unlock(proc
);
4210 binder_stats_deleted(BINDER_STAT_DEATH
);
4212 binder_enqueue_work_ilocked(
4213 w
, &proc
->delivered_death
);
4214 binder_inner_proc_unlock(proc
);
4216 if (put_user(cmd
, (uint32_t __user
*)ptr
))
4218 ptr
+= sizeof(uint32_t);
4219 if (put_user(cookie
,
4220 (binder_uintptr_t __user
*)ptr
))
4222 ptr
+= sizeof(binder_uintptr_t
);
4223 binder_stat_br(proc
, thread
, cmd
);
4224 if (cmd
== BR_DEAD_BINDER
)
4225 goto done
; /* DEAD_BINDER notifications can cause transactions */
4232 BUG_ON(t
->buffer
== NULL
);
4233 if (t
->buffer
->target_node
) {
4234 struct binder_node
*target_node
= t
->buffer
->target_node
;
4235 struct binder_priority node_prio
;
4237 tr
.target
.ptr
= target_node
->ptr
;
4238 tr
.cookie
= target_node
->cookie
;
4239 node_prio
.sched_policy
= target_node
->sched_policy
;
4240 node_prio
.prio
= target_node
->min_priority
;
4241 binder_transaction_priority(current
, t
, node_prio
,
4242 target_node
->inherit_rt
);
4243 cmd
= BR_TRANSACTION
;
4250 tr
.flags
= t
->flags
;
4251 tr
.sender_euid
= from_kuid(current_user_ns(), t
->sender_euid
);
4253 t_from
= binder_get_txn_from(t
);
4255 struct task_struct
*sender
= t_from
->proc
->tsk
;
4257 tr
.sender_pid
= task_tgid_nr_ns(sender
,
4258 task_active_pid_ns(current
));
4263 tr
.data_size
= t
->buffer
->data_size
;
4264 tr
.offsets_size
= t
->buffer
->offsets_size
;
4265 tr
.data
.ptr
.buffer
= (binder_uintptr_t
)
4266 ((uintptr_t)t
->buffer
->data
+
4267 binder_alloc_get_user_buffer_offset(&proc
->alloc
));
4268 tr
.data
.ptr
.offsets
= tr
.data
.ptr
.buffer
+
4269 ALIGN(t
->buffer
->data_size
,
4272 if (put_user(cmd
, (uint32_t __user
*)ptr
)) {
4274 binder_thread_dec_tmpref(t_from
);
4276 binder_cleanup_transaction(t
, "put_user failed",
4281 ptr
+= sizeof(uint32_t);
4282 if (copy_to_user(ptr
, &tr
, sizeof(tr
))) {
4284 binder_thread_dec_tmpref(t_from
);
4286 binder_cleanup_transaction(t
, "copy_to_user failed",
4293 trace_binder_transaction_received(t
);
4294 binder_stat_br(proc
, thread
, cmd
);
4295 binder_debug(BINDER_DEBUG_TRANSACTION
,
4296 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
4297 proc
->pid
, thread
->pid
,
4298 (cmd
== BR_TRANSACTION
) ? "BR_TRANSACTION" :
4300 t
->debug_id
, t_from
? t_from
->proc
->pid
: 0,
4301 t_from
? t_from
->pid
: 0, cmd
,
4302 t
->buffer
->data_size
, t
->buffer
->offsets_size
,
4303 (u64
)tr
.data
.ptr
.buffer
, (u64
)tr
.data
.ptr
.offsets
);
4306 binder_thread_dec_tmpref(t_from
);
4307 t
->buffer
->allow_user_free
= 1;
4308 if (cmd
== BR_TRANSACTION
&& !(t
->flags
& TF_ONE_WAY
)) {
4309 binder_inner_proc_lock(thread
->proc
);
4310 t
->to_parent
= thread
->transaction_stack
;
4311 t
->to_thread
= thread
;
4312 thread
->transaction_stack
= t
;
4313 binder_inner_proc_unlock(thread
->proc
);
4315 binder_free_transaction(t
);
4322 *consumed
= ptr
- buffer
;
4323 binder_inner_proc_lock(proc
);
4324 if (proc
->requested_threads
== 0 &&
4325 list_empty(&thread
->proc
->waiting_threads
) &&
4326 proc
->requested_threads_started
< proc
->max_threads
&&
4327 (thread
->looper
& (BINDER_LOOPER_STATE_REGISTERED
|
4328 BINDER_LOOPER_STATE_ENTERED
)) /* the user-space code fails to */
4329 /*spawn a new thread if we leave this out */) {
4330 proc
->requested_threads
++;
4331 binder_inner_proc_unlock(proc
);
4332 binder_debug(BINDER_DEBUG_THREADS
,
4333 "%d:%d BR_SPAWN_LOOPER\n",
4334 proc
->pid
, thread
->pid
);
4335 if (put_user(BR_SPAWN_LOOPER
, (uint32_t __user
*)buffer
))
4337 binder_stat_br(proc
, thread
, BR_SPAWN_LOOPER
);
4339 binder_inner_proc_unlock(proc
);
4343 static void binder_release_work(struct binder_proc
*proc
,
4344 struct list_head
*list
)
4346 struct binder_work
*w
;
4349 w
= binder_dequeue_work_head(proc
, list
);
4354 case BINDER_WORK_TRANSACTION
: {
4355 struct binder_transaction
*t
;
4357 t
= container_of(w
, struct binder_transaction
, work
);
4359 binder_cleanup_transaction(t
, "process died.",
4362 case BINDER_WORK_RETURN_ERROR
: {
4363 struct binder_error
*e
= container_of(
4364 w
, struct binder_error
, work
);
4366 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
4367 "undelivered TRANSACTION_ERROR: %u\n",
4370 case BINDER_WORK_TRANSACTION_COMPLETE
: {
4371 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
4372 "undelivered TRANSACTION_COMPLETE\n");
4374 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE
);
4376 case BINDER_WORK_DEAD_BINDER_AND_CLEAR
:
4377 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION
: {
4378 struct binder_ref_death
*death
;
4380 death
= container_of(w
, struct binder_ref_death
, work
);
4381 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
4382 "undelivered death notification, %016llx\n",
4383 (u64
)death
->cookie
);
4385 binder_stats_deleted(BINDER_STAT_DEATH
);
4388 pr_err("unexpected work type, %d, not freed\n",
4396 static struct binder_thread
*binder_get_thread_ilocked(
4397 struct binder_proc
*proc
, struct binder_thread
*new_thread
)
4399 struct binder_thread
*thread
= NULL
;
4400 struct rb_node
*parent
= NULL
;
4401 struct rb_node
**p
= &proc
->threads
.rb_node
;
4405 thread
= rb_entry(parent
, struct binder_thread
, rb_node
);
4407 if (current
->pid
< thread
->pid
)
4409 else if (current
->pid
> thread
->pid
)
4410 p
= &(*p
)->rb_right
;
4416 thread
= new_thread
;
4417 binder_stats_created(BINDER_STAT_THREAD
);
4418 thread
->proc
= proc
;
4419 thread
->pid
= current
->pid
;
4420 get_task_struct(current
);
4421 thread
->task
= current
;
4422 atomic_set(&thread
->tmp_ref
, 0);
4423 init_waitqueue_head(&thread
->wait
);
4424 INIT_LIST_HEAD(&thread
->todo
);
4425 rb_link_node(&thread
->rb_node
, parent
, p
);
4426 rb_insert_color(&thread
->rb_node
, &proc
->threads
);
4427 thread
->looper_need_return
= true;
4428 thread
->return_error
.work
.type
= BINDER_WORK_RETURN_ERROR
;
4429 thread
->return_error
.cmd
= BR_OK
;
4430 thread
->reply_error
.work
.type
= BINDER_WORK_RETURN_ERROR
;
4431 thread
->reply_error
.cmd
= BR_OK
;
4432 INIT_LIST_HEAD(&new_thread
->waiting_thread_node
);
4436 static struct binder_thread
*binder_get_thread(struct binder_proc
*proc
)
4438 struct binder_thread
*thread
;
4439 struct binder_thread
*new_thread
;
4441 binder_inner_proc_lock(proc
);
4442 thread
= binder_get_thread_ilocked(proc
, NULL
);
4443 binder_inner_proc_unlock(proc
);
4445 new_thread
= kzalloc(sizeof(*thread
), GFP_KERNEL
);
4446 if (new_thread
== NULL
)
4448 binder_inner_proc_lock(proc
);
4449 thread
= binder_get_thread_ilocked(proc
, new_thread
);
4450 binder_inner_proc_unlock(proc
);
4451 if (thread
!= new_thread
)
4457 static void binder_free_proc(struct binder_proc
*proc
)
4459 BUG_ON(!list_empty(&proc
->todo
));
4460 BUG_ON(!list_empty(&proc
->delivered_death
));
4461 binder_alloc_deferred_release(&proc
->alloc
);
4462 put_task_struct(proc
->tsk
);
4463 binder_stats_deleted(BINDER_STAT_PROC
);
4467 static void binder_free_thread(struct binder_thread
*thread
)
4469 BUG_ON(!list_empty(&thread
->todo
));
4470 binder_stats_deleted(BINDER_STAT_THREAD
);
4471 binder_proc_dec_tmpref(thread
->proc
);
4472 put_task_struct(thread
->task
);
4476 static int binder_thread_release(struct binder_proc
*proc
,
4477 struct binder_thread
*thread
)
4479 struct binder_transaction
*t
;
4480 struct binder_transaction
*send_reply
= NULL
;
4481 int active_transactions
= 0;
4482 struct binder_transaction
*last_t
= NULL
;
4484 binder_inner_proc_lock(thread
->proc
);
4486 * take a ref on the proc so it survives
4487 * after we remove this thread from proc->threads.
4488 * The corresponding dec is when we actually
4489 * free the thread in binder_free_thread()
4493 * take a ref on this thread to ensure it
4494 * survives while we are releasing it
4496 atomic_inc(&thread
->tmp_ref
);
4497 rb_erase(&thread
->rb_node
, &proc
->threads
);
4498 t
= thread
->transaction_stack
;
4500 spin_lock(&t
->lock
);
4501 if (t
->to_thread
== thread
)
4504 thread
->is_dead
= true;
4508 active_transactions
++;
4509 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
4510 "release %d:%d transaction %d %s, still active\n",
4511 proc
->pid
, thread
->pid
,
4513 (t
->to_thread
== thread
) ? "in" : "out");
4515 if (t
->to_thread
== thread
) {
4517 t
->to_thread
= NULL
;
4519 t
->buffer
->transaction
= NULL
;
4523 } else if (t
->from
== thread
) {
4528 spin_unlock(&last_t
->lock
);
4530 spin_lock(&t
->lock
);
4532 binder_inner_proc_unlock(thread
->proc
);
4535 binder_send_failed_reply(send_reply
, BR_DEAD_REPLY
);
4536 binder_release_work(proc
, &thread
->todo
);
4537 binder_thread_dec_tmpref(thread
);
4538 return active_transactions
;
4541 static unsigned int binder_poll(struct file
*filp
,
4542 struct poll_table_struct
*wait
)
4544 struct binder_proc
*proc
= filp
->private_data
;
4545 struct binder_thread
*thread
= NULL
;
4546 bool wait_for_proc_work
;
4548 thread
= binder_get_thread(proc
);
4550 binder_inner_proc_lock(thread
->proc
);
4551 thread
->looper
|= BINDER_LOOPER_STATE_POLL
;
4552 wait_for_proc_work
= binder_available_for_proc_work_ilocked(thread
);
4554 binder_inner_proc_unlock(thread
->proc
);
4556 poll_wait(filp
, &thread
->wait
, wait
);
4558 if (binder_has_work(thread
, wait_for_proc_work
))
4564 static int binder_ioctl_write_read(struct file
*filp
,
4565 unsigned int cmd
, unsigned long arg
,
4566 struct binder_thread
*thread
)
4569 struct binder_proc
*proc
= filp
->private_data
;
4570 unsigned int size
= _IOC_SIZE(cmd
);
4571 void __user
*ubuf
= (void __user
*)arg
;
4572 struct binder_write_read bwr
;
4574 if (size
!= sizeof(struct binder_write_read
)) {
4578 if (copy_from_user(&bwr
, ubuf
, sizeof(bwr
))) {
4582 binder_debug(BINDER_DEBUG_READ_WRITE
,
4583 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
4584 proc
->pid
, thread
->pid
,
4585 (u64
)bwr
.write_size
, (u64
)bwr
.write_buffer
,
4586 (u64
)bwr
.read_size
, (u64
)bwr
.read_buffer
);
4588 if (bwr
.write_size
> 0) {
4589 ret
= binder_thread_write(proc
, thread
,
4592 &bwr
.write_consumed
);
4593 trace_binder_write_done(ret
);
4595 bwr
.read_consumed
= 0;
4596 if (copy_to_user(ubuf
, &bwr
, sizeof(bwr
)))
4601 if (bwr
.read_size
> 0) {
4602 ret
= binder_thread_read(proc
, thread
, bwr
.read_buffer
,
4605 filp
->f_flags
& O_NONBLOCK
);
4606 trace_binder_read_done(ret
);
4607 binder_inner_proc_lock(proc
);
4608 if (!binder_worklist_empty_ilocked(&proc
->todo
))
4609 binder_wakeup_proc_ilocked(proc
);
4610 binder_inner_proc_unlock(proc
);
4612 if (copy_to_user(ubuf
, &bwr
, sizeof(bwr
)))
4617 binder_debug(BINDER_DEBUG_READ_WRITE
,
4618 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
4619 proc
->pid
, thread
->pid
,
4620 (u64
)bwr
.write_consumed
, (u64
)bwr
.write_size
,
4621 (u64
)bwr
.read_consumed
, (u64
)bwr
.read_size
);
4622 if (copy_to_user(ubuf
, &bwr
, sizeof(bwr
))) {
4630 static int binder_ioctl_set_ctx_mgr(struct file
*filp
)
4633 struct binder_proc
*proc
= filp
->private_data
;
4634 struct binder_context
*context
= proc
->context
;
4635 struct binder_node
*new_node
;
4636 kuid_t curr_euid
= current_euid();
4638 mutex_lock(&context
->context_mgr_node_lock
);
4639 if (context
->binder_context_mgr_node
) {
4640 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
4644 ret
= security_binder_set_context_mgr(proc
->tsk
);
4647 if (uid_valid(context
->binder_context_mgr_uid
)) {
4648 if (!uid_eq(context
->binder_context_mgr_uid
, curr_euid
)) {
4649 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
4650 from_kuid(&init_user_ns
, curr_euid
),
4651 from_kuid(&init_user_ns
,
4652 context
->binder_context_mgr_uid
));
4657 context
->binder_context_mgr_uid
= curr_euid
;
4659 new_node
= binder_new_node(proc
, NULL
);
4664 binder_node_lock(new_node
);
4665 new_node
->local_weak_refs
++;
4666 new_node
->local_strong_refs
++;
4667 new_node
->has_strong_ref
= 1;
4668 new_node
->has_weak_ref
= 1;
4669 context
->binder_context_mgr_node
= new_node
;
4670 binder_node_unlock(new_node
);
4671 binder_put_node(new_node
);
4673 mutex_unlock(&context
->context_mgr_node_lock
);
4677 static int binder_ioctl_get_node_debug_info(struct binder_proc
*proc
,
4678 struct binder_node_debug_info
*info
) {
4680 binder_uintptr_t ptr
= info
->ptr
;
4682 memset(info
, 0, sizeof(*info
));
4684 binder_inner_proc_lock(proc
);
4685 for (n
= rb_first(&proc
->nodes
); n
!= NULL
; n
= rb_next(n
)) {
4686 struct binder_node
*node
= rb_entry(n
, struct binder_node
,
4688 if (node
->ptr
> ptr
) {
4689 info
->ptr
= node
->ptr
;
4690 info
->cookie
= node
->cookie
;
4691 info
->has_strong_ref
= node
->has_strong_ref
;
4692 info
->has_weak_ref
= node
->has_weak_ref
;
4696 binder_inner_proc_unlock(proc
);
4701 static long binder_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
4704 struct binder_proc
*proc
= filp
->private_data
;
4705 struct binder_thread
*thread
;
4706 unsigned int size
= _IOC_SIZE(cmd
);
4707 void __user
*ubuf
= (void __user
*)arg
;
4709 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
4710 proc->pid, current->pid, cmd, arg);*/
4712 binder_selftest_alloc(&proc
->alloc
);
4714 trace_binder_ioctl(cmd
, arg
);
4716 ret
= wait_event_interruptible(binder_user_error_wait
, binder_stop_on_user_error
< 2);
4720 thread
= binder_get_thread(proc
);
4721 if (thread
== NULL
) {
4727 case BINDER_WRITE_READ
:
4728 ret
= binder_ioctl_write_read(filp
, cmd
, arg
, thread
);
4732 case BINDER_SET_MAX_THREADS
: {
4735 if (copy_from_user(&max_threads
, ubuf
,
4736 sizeof(max_threads
))) {
4740 binder_inner_proc_lock(proc
);
4741 proc
->max_threads
= max_threads
;
4742 binder_inner_proc_unlock(proc
);
4745 case BINDER_SET_CONTEXT_MGR
:
4746 ret
= binder_ioctl_set_ctx_mgr(filp
);
4750 case BINDER_THREAD_EXIT
:
4751 binder_debug(BINDER_DEBUG_THREADS
, "%d:%d exit\n",
4752 proc
->pid
, thread
->pid
);
4753 binder_thread_release(proc
, thread
);
4756 case BINDER_VERSION
: {
4757 struct binder_version __user
*ver
= ubuf
;
4759 if (size
!= sizeof(struct binder_version
)) {
4763 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION
,
4764 &ver
->protocol_version
)) {
4770 case BINDER_GET_NODE_DEBUG_INFO
: {
4771 struct binder_node_debug_info info
;
4773 if (copy_from_user(&info
, ubuf
, sizeof(info
))) {
4778 ret
= binder_ioctl_get_node_debug_info(proc
, &info
);
4782 if (copy_to_user(ubuf
, &info
, sizeof(info
))) {
4795 thread
->looper_need_return
= false;
4796 wait_event_interruptible(binder_user_error_wait
, binder_stop_on_user_error
< 2);
4797 if (ret
&& ret
!= -ERESTARTSYS
)
4798 pr_info("%d:%d ioctl %x %lx returned %d\n", proc
->pid
, current
->pid
, cmd
, arg
, ret
);
4800 trace_binder_ioctl_done(ret
);
4804 static void binder_vma_open(struct vm_area_struct
*vma
)
4806 struct binder_proc
*proc
= vma
->vm_private_data
;
4808 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
4809 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
4810 proc
->pid
, vma
->vm_start
, vma
->vm_end
,
4811 (vma
->vm_end
- vma
->vm_start
) / SZ_1K
, vma
->vm_flags
,
4812 (unsigned long)pgprot_val(vma
->vm_page_prot
));
4815 static void binder_vma_close(struct vm_area_struct
*vma
)
4817 struct binder_proc
*proc
= vma
->vm_private_data
;
4819 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
4820 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
4821 proc
->pid
, vma
->vm_start
, vma
->vm_end
,
4822 (vma
->vm_end
- vma
->vm_start
) / SZ_1K
, vma
->vm_flags
,
4823 (unsigned long)pgprot_val(vma
->vm_page_prot
));
4824 binder_alloc_vma_close(&proc
->alloc
);
4825 binder_defer_work(proc
, BINDER_DEFERRED_PUT_FILES
);
4828 static int binder_vm_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
4830 return VM_FAULT_SIGBUS
;
4833 static struct vm_operations_struct binder_vm_ops
= {
4834 .open
= binder_vma_open
,
4835 .close
= binder_vma_close
,
4836 .fault
= binder_vm_fault
,
4839 static int binder_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
4842 struct binder_proc
*proc
= filp
->private_data
;
4843 const char *failure_string
;
4845 if (proc
->tsk
!= current
->group_leader
)
4848 if ((vma
->vm_end
- vma
->vm_start
) > SZ_4M
)
4849 vma
->vm_end
= vma
->vm_start
+ SZ_4M
;
4851 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
4852 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
4853 __func__
, proc
->pid
, vma
->vm_start
, vma
->vm_end
,
4854 (vma
->vm_end
- vma
->vm_start
) / SZ_1K
, vma
->vm_flags
,
4855 (unsigned long)pgprot_val(vma
->vm_page_prot
));
4857 if (vma
->vm_flags
& FORBIDDEN_MMAP_FLAGS
) {
4859 failure_string
= "bad vm_flags";
4862 vma
->vm_flags
= (vma
->vm_flags
| VM_DONTCOPY
) & ~VM_MAYWRITE
;
4863 vma
->vm_ops
= &binder_vm_ops
;
4864 vma
->vm_private_data
= proc
;
4866 ret
= binder_alloc_mmap_handler(&proc
->alloc
, vma
);
4869 proc
->files
= get_files_struct(current
);
4873 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
4874 proc
->pid
, vma
->vm_start
, vma
->vm_end
, failure_string
, ret
);
4878 static int binder_open(struct inode
*nodp
, struct file
*filp
)
4880 struct binder_proc
*proc
;
4881 struct binder_device
*binder_dev
;
4883 binder_debug(BINDER_DEBUG_OPEN_CLOSE
, "binder_open: %d:%d\n",
4884 current
->group_leader
->pid
, current
->pid
);
4886 proc
= kzalloc(sizeof(*proc
), GFP_KERNEL
);
4889 spin_lock_init(&proc
->inner_lock
);
4890 spin_lock_init(&proc
->outer_lock
);
4891 get_task_struct(current
->group_leader
);
4892 proc
->tsk
= current
->group_leader
;
4893 INIT_LIST_HEAD(&proc
->todo
);
4894 if (binder_supported_policy(current
->policy
)) {
4895 proc
->default_priority
.sched_policy
= current
->policy
;
4896 proc
->default_priority
.prio
= current
->normal_prio
;
4898 proc
->default_priority
.sched_policy
= SCHED_NORMAL
;
4899 proc
->default_priority
.prio
= NICE_TO_PRIO(0);
4902 binder_dev
= container_of(filp
->private_data
, struct binder_device
,
4904 proc
->context
= &binder_dev
->context
;
4905 binder_alloc_init(&proc
->alloc
);
4907 binder_stats_created(BINDER_STAT_PROC
);
4908 proc
->pid
= current
->group_leader
->pid
;
4909 INIT_LIST_HEAD(&proc
->delivered_death
);
4910 INIT_LIST_HEAD(&proc
->waiting_threads
);
4911 filp
->private_data
= proc
;
4913 mutex_lock(&binder_procs_lock
);
4914 hlist_add_head(&proc
->proc_node
, &binder_procs
);
4915 mutex_unlock(&binder_procs_lock
);
4917 if (binder_debugfs_dir_entry_proc
) {
4920 snprintf(strbuf
, sizeof(strbuf
), "%u", proc
->pid
);
4922 * proc debug entries are shared between contexts, so
4923 * this will fail if the process tries to open the driver
4924 * again with a different context. The priting code will
4925 * anyway print all contexts that a given PID has, so this
4928 proc
->debugfs_entry
= debugfs_create_file(strbuf
, S_IRUGO
,
4929 binder_debugfs_dir_entry_proc
,
4930 (void *)(unsigned long)proc
->pid
,
4937 static int binder_flush(struct file
*filp
, fl_owner_t id
)
4939 struct binder_proc
*proc
= filp
->private_data
;
4941 binder_defer_work(proc
, BINDER_DEFERRED_FLUSH
);
4946 static void binder_deferred_flush(struct binder_proc
*proc
)
4951 binder_inner_proc_lock(proc
);
4952 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
)) {
4953 struct binder_thread
*thread
= rb_entry(n
, struct binder_thread
, rb_node
);
4955 thread
->looper_need_return
= true;
4956 if (thread
->looper
& BINDER_LOOPER_STATE_WAITING
) {
4957 wake_up_interruptible(&thread
->wait
);
4961 binder_inner_proc_unlock(proc
);
4963 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
4964 "binder_flush: %d woke %d threads\n", proc
->pid
,
4968 static int binder_release(struct inode
*nodp
, struct file
*filp
)
4970 struct binder_proc
*proc
= filp
->private_data
;
4972 debugfs_remove(proc
->debugfs_entry
);
4973 binder_defer_work(proc
, BINDER_DEFERRED_RELEASE
);
4978 static int binder_node_release(struct binder_node
*node
, int refs
)
4980 struct binder_ref
*ref
;
4982 struct binder_proc
*proc
= node
->proc
;
4984 binder_release_work(proc
, &node
->async_todo
);
4986 binder_node_lock(node
);
4987 binder_inner_proc_lock(proc
);
4988 binder_dequeue_work_ilocked(&node
->work
);
4990 * The caller must have taken a temporary ref on the node,
4992 BUG_ON(!node
->tmp_refs
);
4993 if (hlist_empty(&node
->refs
) && node
->tmp_refs
== 1) {
4994 binder_inner_proc_unlock(proc
);
4995 binder_node_unlock(node
);
4996 binder_free_node(node
);
5002 node
->local_strong_refs
= 0;
5003 node
->local_weak_refs
= 0;
5004 binder_inner_proc_unlock(proc
);
5006 spin_lock(&binder_dead_nodes_lock
);
5007 hlist_add_head(&node
->dead_node
, &binder_dead_nodes
);
5008 spin_unlock(&binder_dead_nodes_lock
);
5010 hlist_for_each_entry(ref
, &node
->refs
, node_entry
) {
5013 * Need the node lock to synchronize
5014 * with new notification requests and the
5015 * inner lock to synchronize with queued
5016 * death notifications.
5018 binder_inner_proc_lock(ref
->proc
);
5020 binder_inner_proc_unlock(ref
->proc
);
5026 BUG_ON(!list_empty(&ref
->death
->work
.entry
));
5027 ref
->death
->work
.type
= BINDER_WORK_DEAD_BINDER
;
5028 binder_enqueue_work_ilocked(&ref
->death
->work
,
5030 binder_wakeup_proc_ilocked(ref
->proc
);
5031 binder_inner_proc_unlock(ref
->proc
);
5034 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
5035 "node %d now dead, refs %d, death %d\n",
5036 node
->debug_id
, refs
, death
);
5037 binder_node_unlock(node
);
5038 binder_put_node(node
);
5043 static void binder_deferred_release(struct binder_proc
*proc
)
5045 struct binder_context
*context
= proc
->context
;
5047 int threads
, nodes
, incoming_refs
, outgoing_refs
, active_transactions
;
5049 BUG_ON(proc
->files
);
5051 mutex_lock(&binder_procs_lock
);
5052 hlist_del(&proc
->proc_node
);
5053 mutex_unlock(&binder_procs_lock
);
5055 mutex_lock(&context
->context_mgr_node_lock
);
5056 if (context
->binder_context_mgr_node
&&
5057 context
->binder_context_mgr_node
->proc
== proc
) {
5058 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
5059 "%s: %d context_mgr_node gone\n",
5060 __func__
, proc
->pid
);
5061 context
->binder_context_mgr_node
= NULL
;
5063 mutex_unlock(&context
->context_mgr_node_lock
);
5064 binder_inner_proc_lock(proc
);
5066 * Make sure proc stays alive after we
5067 * remove all the threads
5071 proc
->is_dead
= true;
5073 active_transactions
= 0;
5074 while ((n
= rb_first(&proc
->threads
))) {
5075 struct binder_thread
*thread
;
5077 thread
= rb_entry(n
, struct binder_thread
, rb_node
);
5078 binder_inner_proc_unlock(proc
);
5080 active_transactions
+= binder_thread_release(proc
, thread
);
5081 binder_inner_proc_lock(proc
);
5086 while ((n
= rb_first(&proc
->nodes
))) {
5087 struct binder_node
*node
;
5089 node
= rb_entry(n
, struct binder_node
, rb_node
);
5092 * take a temporary ref on the node before
5093 * calling binder_node_release() which will either
5094 * kfree() the node or call binder_put_node()
5096 binder_inc_node_tmpref_ilocked(node
);
5097 rb_erase(&node
->rb_node
, &proc
->nodes
);
5098 binder_inner_proc_unlock(proc
);
5099 incoming_refs
= binder_node_release(node
, incoming_refs
);
5100 binder_inner_proc_lock(proc
);
5102 binder_inner_proc_unlock(proc
);
5105 binder_proc_lock(proc
);
5106 while ((n
= rb_first(&proc
->refs_by_desc
))) {
5107 struct binder_ref
*ref
;
5109 ref
= rb_entry(n
, struct binder_ref
, rb_node_desc
);
5111 binder_cleanup_ref_olocked(ref
);
5112 binder_proc_unlock(proc
);
5113 binder_free_ref(ref
);
5114 binder_proc_lock(proc
);
5116 binder_proc_unlock(proc
);
5118 binder_release_work(proc
, &proc
->todo
);
5119 binder_release_work(proc
, &proc
->delivered_death
);
5121 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
5122 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
5123 __func__
, proc
->pid
, threads
, nodes
, incoming_refs
,
5124 outgoing_refs
, active_transactions
);
5126 binder_proc_dec_tmpref(proc
);
5129 static void binder_deferred_func(struct work_struct
*work
)
5131 struct binder_proc
*proc
;
5132 struct files_struct
*files
;
5137 mutex_lock(&binder_deferred_lock
);
5138 if (!hlist_empty(&binder_deferred_list
)) {
5139 proc
= hlist_entry(binder_deferred_list
.first
,
5140 struct binder_proc
, deferred_work_node
);
5141 hlist_del_init(&proc
->deferred_work_node
);
5142 defer
= proc
->deferred_work
;
5143 proc
->deferred_work
= 0;
5148 mutex_unlock(&binder_deferred_lock
);
5151 if (defer
& BINDER_DEFERRED_PUT_FILES
) {
5152 files
= proc
->files
;
5157 if (defer
& BINDER_DEFERRED_FLUSH
)
5158 binder_deferred_flush(proc
);
5160 if (defer
& BINDER_DEFERRED_RELEASE
)
5161 binder_deferred_release(proc
); /* frees proc */
5164 put_files_struct(files
);
5167 static DECLARE_WORK(binder_deferred_work
, binder_deferred_func
);
5170 binder_defer_work(struct binder_proc
*proc
, enum binder_deferred_state defer
)
5172 mutex_lock(&binder_deferred_lock
);
5173 proc
->deferred_work
|= defer
;
5174 if (hlist_unhashed(&proc
->deferred_work_node
)) {
5175 hlist_add_head(&proc
->deferred_work_node
,
5176 &binder_deferred_list
);
5177 queue_work(binder_deferred_workqueue
, &binder_deferred_work
);
5179 mutex_unlock(&binder_deferred_lock
);
5182 static void print_binder_transaction_ilocked(struct seq_file
*m
,
5183 struct binder_proc
*proc
,
5185 struct binder_transaction
*t
)
5187 struct binder_proc
*to_proc
;
5188 struct binder_buffer
*buffer
= t
->buffer
;
5190 spin_lock(&t
->lock
);
5191 to_proc
= t
->to_proc
;
5193 "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %d:%d r%d",
5194 prefix
, t
->debug_id
, t
,
5195 t
->from
? t
->from
->proc
->pid
: 0,
5196 t
->from
? t
->from
->pid
: 0,
5197 to_proc
? to_proc
->pid
: 0,
5198 t
->to_thread
? t
->to_thread
->pid
: 0,
5199 t
->code
, t
->flags
, t
->priority
.sched_policy
,
5200 t
->priority
.prio
, t
->need_reply
);
5201 spin_unlock(&t
->lock
);
5203 if (proc
!= to_proc
) {
5205 * Can only safely deref buffer if we are holding the
5206 * correct proc inner lock for this node
5212 if (buffer
== NULL
) {
5213 seq_puts(m
, " buffer free\n");
5216 if (buffer
->target_node
)
5217 seq_printf(m
, " node %d", buffer
->target_node
->debug_id
);
5218 seq_printf(m
, " size %zd:%zd data %p\n",
5219 buffer
->data_size
, buffer
->offsets_size
,
5223 static void print_binder_work_ilocked(struct seq_file
*m
,
5224 struct binder_proc
*proc
,
5226 const char *transaction_prefix
,
5227 struct binder_work
*w
)
5229 struct binder_node
*node
;
5230 struct binder_transaction
*t
;
5233 case BINDER_WORK_TRANSACTION
:
5234 t
= container_of(w
, struct binder_transaction
, work
);
5235 print_binder_transaction_ilocked(
5236 m
, proc
, transaction_prefix
, t
);
5238 case BINDER_WORK_RETURN_ERROR
: {
5239 struct binder_error
*e
= container_of(
5240 w
, struct binder_error
, work
);
5242 seq_printf(m
, "%stransaction error: %u\n",
5245 case BINDER_WORK_TRANSACTION_COMPLETE
:
5246 seq_printf(m
, "%stransaction complete\n", prefix
);
5248 case BINDER_WORK_NODE
:
5249 node
= container_of(w
, struct binder_node
, work
);
5250 seq_printf(m
, "%snode work %d: u%016llx c%016llx\n",
5251 prefix
, node
->debug_id
,
5252 (u64
)node
->ptr
, (u64
)node
->cookie
);
5254 case BINDER_WORK_DEAD_BINDER
:
5255 seq_printf(m
, "%shas dead binder\n", prefix
);
5257 case BINDER_WORK_DEAD_BINDER_AND_CLEAR
:
5258 seq_printf(m
, "%shas cleared dead binder\n", prefix
);
5260 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION
:
5261 seq_printf(m
, "%shas cleared death notification\n", prefix
);
5264 seq_printf(m
, "%sunknown work: type %d\n", prefix
, w
->type
);
5269 static void print_binder_thread_ilocked(struct seq_file
*m
,
5270 struct binder_thread
*thread
,
5273 struct binder_transaction
*t
;
5274 struct binder_work
*w
;
5275 size_t start_pos
= m
->count
;
5278 seq_printf(m
, " thread %d: l %02x need_return %d tr %d\n",
5279 thread
->pid
, thread
->looper
,
5280 thread
->looper_need_return
,
5281 atomic_read(&thread
->tmp_ref
));
5282 header_pos
= m
->count
;
5283 t
= thread
->transaction_stack
;
5285 if (t
->from
== thread
) {
5286 print_binder_transaction_ilocked(m
, thread
->proc
,
5287 " outgoing transaction", t
);
5289 } else if (t
->to_thread
== thread
) {
5290 print_binder_transaction_ilocked(m
, thread
->proc
,
5291 " incoming transaction", t
);
5294 print_binder_transaction_ilocked(m
, thread
->proc
,
5295 " bad transaction", t
);
5299 list_for_each_entry(w
, &thread
->todo
, entry
) {
5300 print_binder_work_ilocked(m
, thread
->proc
, " ",
5301 " pending transaction", w
);
5303 if (!print_always
&& m
->count
== header_pos
)
5304 m
->count
= start_pos
;
5307 static void print_binder_node_nilocked(struct seq_file
*m
,
5308 struct binder_node
*node
)
5310 struct binder_ref
*ref
;
5311 struct binder_work
*w
;
5315 hlist_for_each_entry(ref
, &node
->refs
, node_entry
)
5318 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",
5319 node
->debug_id
, (u64
)node
->ptr
, (u64
)node
->cookie
,
5320 node
->sched_policy
, node
->min_priority
,
5321 node
->has_strong_ref
, node
->has_weak_ref
,
5322 node
->local_strong_refs
, node
->local_weak_refs
,
5323 node
->internal_strong_refs
, count
, node
->tmp_refs
);
5325 seq_puts(m
, " proc");
5326 hlist_for_each_entry(ref
, &node
->refs
, node_entry
)
5327 seq_printf(m
, " %d", ref
->proc
->pid
);
5331 list_for_each_entry(w
, &node
->async_todo
, entry
)
5332 print_binder_work_ilocked(m
, node
->proc
, " ",
5333 " pending async transaction", w
);
5337 static void print_binder_ref_olocked(struct seq_file
*m
,
5338 struct binder_ref
*ref
)
5340 binder_node_lock(ref
->node
);
5341 seq_printf(m
, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
5342 ref
->data
.debug_id
, ref
->data
.desc
,
5343 ref
->node
->proc
? "" : "dead ",
5344 ref
->node
->debug_id
, ref
->data
.strong
,
5345 ref
->data
.weak
, ref
->death
);
5346 binder_node_unlock(ref
->node
);
5349 static void print_binder_proc(struct seq_file
*m
,
5350 struct binder_proc
*proc
, int print_all
)
5352 struct binder_work
*w
;
5354 size_t start_pos
= m
->count
;
5356 struct binder_node
*last_node
= NULL
;
5358 seq_printf(m
, "proc %d\n", proc
->pid
);
5359 seq_printf(m
, "context %s\n", proc
->context
->name
);
5360 header_pos
= m
->count
;
5362 binder_inner_proc_lock(proc
);
5363 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
))
5364 print_binder_thread_ilocked(m
, rb_entry(n
, struct binder_thread
,
5365 rb_node
), print_all
);
5367 for (n
= rb_first(&proc
->nodes
); n
!= NULL
; n
= rb_next(n
)) {
5368 struct binder_node
*node
= rb_entry(n
, struct binder_node
,
5371 * take a temporary reference on the node so it
5372 * survives and isn't removed from the tree
5373 * while we print it.
5375 binder_inc_node_tmpref_ilocked(node
);
5376 /* Need to drop inner lock to take node lock */
5377 binder_inner_proc_unlock(proc
);
5379 binder_put_node(last_node
);
5380 binder_node_inner_lock(node
);
5381 print_binder_node_nilocked(m
, node
);
5382 binder_node_inner_unlock(node
);
5384 binder_inner_proc_lock(proc
);
5386 binder_inner_proc_unlock(proc
);
5388 binder_put_node(last_node
);
5391 binder_proc_lock(proc
);
5392 for (n
= rb_first(&proc
->refs_by_desc
);
5395 print_binder_ref_olocked(m
, rb_entry(n
,
5398 binder_proc_unlock(proc
);
5400 binder_alloc_print_allocated(m
, &proc
->alloc
);
5401 binder_inner_proc_lock(proc
);
5402 list_for_each_entry(w
, &proc
->todo
, entry
)
5403 print_binder_work_ilocked(m
, proc
, " ",
5404 " pending transaction", w
);
5405 list_for_each_entry(w
, &proc
->delivered_death
, entry
) {
5406 seq_puts(m
, " has delivered dead binder\n");
5409 binder_inner_proc_unlock(proc
);
5410 if (!print_all
&& m
->count
== header_pos
)
5411 m
->count
= start_pos
;
5414 static const char * const binder_return_strings
[] = {
5419 "BR_ACQUIRE_RESULT",
5421 "BR_TRANSACTION_COMPLETE",
5426 "BR_ATTEMPT_ACQUIRE",
5431 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
5435 static const char * const binder_command_strings
[] = {
5438 "BC_ACQUIRE_RESULT",
5446 "BC_ATTEMPT_ACQUIRE",
5447 "BC_REGISTER_LOOPER",
5450 "BC_REQUEST_DEATH_NOTIFICATION",
5451 "BC_CLEAR_DEATH_NOTIFICATION",
5452 "BC_DEAD_BINDER_DONE",
5453 "BC_TRANSACTION_SG",
5457 static const char * const binder_objstat_strings
[] = {
5464 "transaction_complete"
5467 static void print_binder_stats(struct seq_file
*m
, const char *prefix
,
5468 struct binder_stats
*stats
)
5472 BUILD_BUG_ON(ARRAY_SIZE(stats
->bc
) !=
5473 ARRAY_SIZE(binder_command_strings
));
5474 for (i
= 0; i
< ARRAY_SIZE(stats
->bc
); i
++) {
5475 int temp
= atomic_read(&stats
->bc
[i
]);
5478 seq_printf(m
, "%s%s: %d\n", prefix
,
5479 binder_command_strings
[i
], temp
);
5482 BUILD_BUG_ON(ARRAY_SIZE(stats
->br
) !=
5483 ARRAY_SIZE(binder_return_strings
));
5484 for (i
= 0; i
< ARRAY_SIZE(stats
->br
); i
++) {
5485 int temp
= atomic_read(&stats
->br
[i
]);
5488 seq_printf(m
, "%s%s: %d\n", prefix
,
5489 binder_return_strings
[i
], temp
);
5492 BUILD_BUG_ON(ARRAY_SIZE(stats
->obj_created
) !=
5493 ARRAY_SIZE(binder_objstat_strings
));
5494 BUILD_BUG_ON(ARRAY_SIZE(stats
->obj_created
) !=
5495 ARRAY_SIZE(stats
->obj_deleted
));
5496 for (i
= 0; i
< ARRAY_SIZE(stats
->obj_created
); i
++) {
5497 int created
= atomic_read(&stats
->obj_created
[i
]);
5498 int deleted
= atomic_read(&stats
->obj_deleted
[i
]);
5500 if (created
|| deleted
)
5501 seq_printf(m
, "%s%s: active %d total %d\n",
5503 binder_objstat_strings
[i
],
5509 static void print_binder_proc_stats(struct seq_file
*m
,
5510 struct binder_proc
*proc
)
5512 struct binder_work
*w
;
5513 struct binder_thread
*thread
;
5515 int count
, strong
, weak
, ready_threads
;
5516 size_t free_async_space
=
5517 binder_alloc_get_free_async_space(&proc
->alloc
);
5519 seq_printf(m
, "proc %d\n", proc
->pid
);
5520 seq_printf(m
, "context %s\n", proc
->context
->name
);
5523 binder_inner_proc_lock(proc
);
5524 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
))
5527 list_for_each_entry(thread
, &proc
->waiting_threads
, waiting_thread_node
)
5530 seq_printf(m
, " threads: %d\n", count
);
5531 seq_printf(m
, " requested threads: %d+%d/%d\n"
5532 " ready threads %d\n"
5533 " free async space %zd\n", proc
->requested_threads
,
5534 proc
->requested_threads_started
, proc
->max_threads
,
5538 for (n
= rb_first(&proc
->nodes
); n
!= NULL
; n
= rb_next(n
))
5540 binder_inner_proc_unlock(proc
);
5541 seq_printf(m
, " nodes: %d\n", count
);
5545 binder_proc_lock(proc
);
5546 for (n
= rb_first(&proc
->refs_by_desc
); n
!= NULL
; n
= rb_next(n
)) {
5547 struct binder_ref
*ref
= rb_entry(n
, struct binder_ref
,
5550 strong
+= ref
->data
.strong
;
5551 weak
+= ref
->data
.weak
;
5553 binder_proc_unlock(proc
);
5554 seq_printf(m
, " refs: %d s %d w %d\n", count
, strong
, weak
);
5556 count
= binder_alloc_get_allocated_count(&proc
->alloc
);
5557 seq_printf(m
, " buffers: %d\n", count
);
5560 binder_inner_proc_lock(proc
);
5561 list_for_each_entry(w
, &proc
->todo
, entry
) {
5562 if (w
->type
== BINDER_WORK_TRANSACTION
)
5565 binder_inner_proc_unlock(proc
);
5566 seq_printf(m
, " pending transactions: %d\n", count
);
5568 print_binder_stats(m
, " ", &proc
->stats
);
5572 static int binder_state_show(struct seq_file
*m
, void *unused
)
5574 struct binder_proc
*proc
;
5575 struct binder_node
*node
;
5576 struct binder_node
*last_node
= NULL
;
5578 seq_puts(m
, "binder state:\n");
5580 spin_lock(&binder_dead_nodes_lock
);
5581 if (!hlist_empty(&binder_dead_nodes
))
5582 seq_puts(m
, "dead nodes:\n");
5583 hlist_for_each_entry(node
, &binder_dead_nodes
, dead_node
) {
5585 * take a temporary reference on the node so it
5586 * survives and isn't removed from the list
5587 * while we print it.
5590 spin_unlock(&binder_dead_nodes_lock
);
5592 binder_put_node(last_node
);
5593 binder_node_lock(node
);
5594 print_binder_node_nilocked(m
, node
);
5595 binder_node_unlock(node
);
5597 spin_lock(&binder_dead_nodes_lock
);
5599 spin_unlock(&binder_dead_nodes_lock
);
5601 binder_put_node(last_node
);
5603 mutex_lock(&binder_procs_lock
);
5604 hlist_for_each_entry(proc
, &binder_procs
, proc_node
)
5605 print_binder_proc(m
, proc
, 1);
5606 mutex_unlock(&binder_procs_lock
);
5611 static int binder_stats_show(struct seq_file
*m
, void *unused
)
5613 struct binder_proc
*proc
;
5615 seq_puts(m
, "binder stats:\n");
5617 print_binder_stats(m
, "", &binder_stats
);
5619 mutex_lock(&binder_procs_lock
);
5620 hlist_for_each_entry(proc
, &binder_procs
, proc_node
)
5621 print_binder_proc_stats(m
, proc
);
5622 mutex_unlock(&binder_procs_lock
);
5627 static int binder_transactions_show(struct seq_file
*m
, void *unused
)
5629 struct binder_proc
*proc
;
5631 seq_puts(m
, "binder transactions:\n");
5632 mutex_lock(&binder_procs_lock
);
5633 hlist_for_each_entry(proc
, &binder_procs
, proc_node
)
5634 print_binder_proc(m
, proc
, 0);
5635 mutex_unlock(&binder_procs_lock
);
5640 static int binder_proc_show(struct seq_file
*m
, void *unused
)
5642 struct binder_proc
*itr
;
5643 int pid
= (unsigned long)m
->private;
5645 mutex_lock(&binder_procs_lock
);
5646 hlist_for_each_entry(itr
, &binder_procs
, proc_node
) {
5647 if (itr
->pid
== pid
) {
5648 seq_puts(m
, "binder proc state:\n");
5649 print_binder_proc(m
, itr
, 1);
5652 mutex_unlock(&binder_procs_lock
);
5657 static void print_binder_transaction_log_entry(struct seq_file
*m
,
5658 struct binder_transaction_log_entry
*e
)
5660 int debug_id
= READ_ONCE(e
->debug_id_done
);
5662 * read barrier to guarantee debug_id_done read before
5663 * we print the log values
5667 "%d: %s from %d:%d to %d:%d context %s node %d handle %d size %d:%d ret %d/%d l=%d",
5668 e
->debug_id
, (e
->call_type
== 2) ? "reply" :
5669 ((e
->call_type
== 1) ? "async" : "call "), e
->from_proc
,
5670 e
->from_thread
, e
->to_proc
, e
->to_thread
, e
->context_name
,
5671 e
->to_node
, e
->target_handle
, e
->data_size
, e
->offsets_size
,
5672 e
->return_error
, e
->return_error_param
,
5673 e
->return_error_line
);
5675 * read-barrier to guarantee read of debug_id_done after
5676 * done printing the fields of the entry
5679 seq_printf(m
, debug_id
&& debug_id
== READ_ONCE(e
->debug_id_done
) ?
5680 "\n" : " (incomplete)\n");
5683 static int binder_transaction_log_show(struct seq_file
*m
, void *unused
)
5685 struct binder_transaction_log
*log
= m
->private;
5686 unsigned int log_cur
= atomic_read(&log
->cur
);
5691 count
= log_cur
+ 1;
5692 cur
= count
< ARRAY_SIZE(log
->entry
) && !log
->full
?
5693 0 : count
% ARRAY_SIZE(log
->entry
);
5694 if (count
> ARRAY_SIZE(log
->entry
) || log
->full
)
5695 count
= ARRAY_SIZE(log
->entry
);
5696 for (i
= 0; i
< count
; i
++) {
5697 unsigned int index
= cur
++ % ARRAY_SIZE(log
->entry
);
5699 print_binder_transaction_log_entry(m
, &log
->entry
[index
]);
5704 static const struct file_operations binder_fops
= {
5705 .owner
= THIS_MODULE
,
5706 .poll
= binder_poll
,
5707 .unlocked_ioctl
= binder_ioctl
,
5708 .compat_ioctl
= binder_ioctl
,
5709 .mmap
= binder_mmap
,
5710 .open
= binder_open
,
5711 .flush
= binder_flush
,
5712 .release
= binder_release
,
5715 BINDER_DEBUG_ENTRY(state
);
5716 BINDER_DEBUG_ENTRY(stats
);
5717 BINDER_DEBUG_ENTRY(transactions
);
5718 BINDER_DEBUG_ENTRY(transaction_log
);
5720 static int __init
init_binder_device(const char *name
)
5723 struct binder_device
*binder_device
;
5725 binder_device
= kzalloc(sizeof(*binder_device
), GFP_KERNEL
);
5729 binder_device
->miscdev
.fops
= &binder_fops
;
5730 binder_device
->miscdev
.minor
= MISC_DYNAMIC_MINOR
;
5731 binder_device
->miscdev
.name
= name
;
5733 binder_device
->context
.binder_context_mgr_uid
= INVALID_UID
;
5734 binder_device
->context
.name
= name
;
5735 mutex_init(&binder_device
->context
.context_mgr_node_lock
);
5737 ret
= misc_register(&binder_device
->miscdev
);
5739 kfree(binder_device
);
5743 hlist_add_head(&binder_device
->hlist
, &binder_devices
);
5748 static int __init
binder_init(void)
5751 char *device_name
, *device_names
;
5752 struct binder_device
*device
;
5753 struct hlist_node
*tmp
;
5755 atomic_set(&binder_transaction_log
.cur
, ~0U);
5756 atomic_set(&binder_transaction_log_failed
.cur
, ~0U);
5757 binder_deferred_workqueue
= create_singlethread_workqueue("binder");
5758 if (!binder_deferred_workqueue
)
5761 binder_debugfs_dir_entry_root
= debugfs_create_dir("binder", NULL
);
5762 if (binder_debugfs_dir_entry_root
)
5763 binder_debugfs_dir_entry_proc
= debugfs_create_dir("proc",
5764 binder_debugfs_dir_entry_root
);
5766 if (binder_debugfs_dir_entry_root
) {
5767 debugfs_create_file("state",
5769 binder_debugfs_dir_entry_root
,
5771 &binder_state_fops
);
5772 debugfs_create_file("stats",
5774 binder_debugfs_dir_entry_root
,
5776 &binder_stats_fops
);
5777 debugfs_create_file("transactions",
5779 binder_debugfs_dir_entry_root
,
5781 &binder_transactions_fops
);
5782 debugfs_create_file("transaction_log",
5784 binder_debugfs_dir_entry_root
,
5785 &binder_transaction_log
,
5786 &binder_transaction_log_fops
);
5787 debugfs_create_file("failed_transaction_log",
5789 binder_debugfs_dir_entry_root
,
5790 &binder_transaction_log_failed
,
5791 &binder_transaction_log_fops
);
5795 * Copy the module_parameter string, because we don't want to
5796 * tokenize it in-place.
5798 device_names
= kzalloc(strlen(binder_devices_param
) + 1, GFP_KERNEL
);
5799 if (!device_names
) {
5801 goto err_alloc_device_names_failed
;
5803 strcpy(device_names
, binder_devices_param
);
5805 while ((device_name
= strsep(&device_names
, ","))) {
5806 ret
= init_binder_device(device_name
);
5808 goto err_init_binder_device_failed
;
5813 err_init_binder_device_failed
:
5814 hlist_for_each_entry_safe(device
, tmp
, &binder_devices
, hlist
) {
5815 misc_deregister(&device
->miscdev
);
5816 hlist_del(&device
->hlist
);
5819 err_alloc_device_names_failed
:
5820 debugfs_remove_recursive(binder_debugfs_dir_entry_root
);
5822 destroy_workqueue(binder_deferred_workqueue
);
5827 device_initcall(binder_init
);
5829 #define CREATE_TRACE_POINTS
5830 #include "binder_trace.h"
5832 MODULE_LICENSE("GPL v2");