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
;
298 enum binder_work_type
{
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 * @txn_security_ctx: require sender's security context
371 * (invariant after initialized)
372 * @async_todo: list of async work items
373 * (protected by @proc->inner_lock)
375 * Bookkeeping structure for binder nodes.
380 struct binder_work work
;
382 struct rb_node rb_node
;
383 struct hlist_node dead_node
;
385 struct binder_proc
*proc
;
386 struct hlist_head refs
;
387 int internal_strong_refs
;
389 int local_strong_refs
;
391 binder_uintptr_t ptr
;
392 binder_uintptr_t cookie
;
395 * bitfield elements protected by
399 u8 pending_strong_ref
:1;
401 u8 pending_weak_ref
:1;
405 * invariant after initialization
410 u8 txn_security_ctx
:1;
413 bool has_async_transaction
;
414 struct list_head async_todo
;
417 struct binder_ref_death
{
419 * @work: worklist element for death notifications
420 * (protected by inner_lock of the proc that
421 * this ref belongs to)
423 struct binder_work work
;
424 binder_uintptr_t cookie
;
428 * struct binder_ref_data - binder_ref counts and id
429 * @debug_id: unique ID for the ref
430 * @desc: unique userspace handle for ref
431 * @strong: strong ref count (debugging only if not locked)
432 * @weak: weak ref count (debugging only if not locked)
434 * Structure to hold ref count and ref id information. Since
435 * the actual ref can only be accessed with a lock, this structure
436 * is used to return information about the ref to callers of
437 * ref inc/dec functions.
439 struct binder_ref_data
{
447 * struct binder_ref - struct to track references on nodes
448 * @data: binder_ref_data containing id, handle, and current refcounts
449 * @rb_node_desc: node for lookup by @data.desc in proc's rb_tree
450 * @rb_node_node: node for lookup by @node in proc's rb_tree
451 * @node_entry: list entry for node->refs list in target node
452 * (protected by @node->lock)
453 * @proc: binder_proc containing ref
454 * @node: binder_node of target node. When cleaning up a
455 * ref for deletion in binder_cleanup_ref, a non-NULL
456 * @node indicates the node must be freed
457 * @death: pointer to death notification (ref_death) if requested
458 * (protected by @node->lock)
460 * Structure to track references from procA to target node (on procB). This
461 * structure is unsafe to access without holding @proc->outer_lock.
464 /* Lookups needed: */
465 /* node + proc => ref (transaction) */
466 /* desc + proc => ref (transaction, inc/dec ref) */
467 /* node => refs + procs (proc exit) */
468 struct binder_ref_data data
;
469 struct rb_node rb_node_desc
;
470 struct rb_node rb_node_node
;
471 struct hlist_node node_entry
;
472 struct binder_proc
*proc
;
473 struct binder_node
*node
;
474 struct binder_ref_death
*death
;
477 enum binder_deferred_state
{
478 BINDER_DEFERRED_PUT_FILES
= 0x01,
479 BINDER_DEFERRED_FLUSH
= 0x02,
480 BINDER_DEFERRED_RELEASE
= 0x04,
484 * struct binder_priority - scheduler policy and priority
485 * @sched_policy scheduler policy
486 * @prio [100..139] for SCHED_NORMAL, [0..99] for FIFO/RT
488 * The binder driver supports inheriting the following scheduler policies:
494 struct binder_priority
{
495 unsigned int sched_policy
;
500 * struct binder_proc - binder process bookkeeping
501 * @proc_node: element for binder_procs list
502 * @threads: rbtree of binder_threads in this proc
503 * (protected by @inner_lock)
504 * @nodes: rbtree of binder nodes associated with
505 * this proc ordered by node->ptr
506 * (protected by @inner_lock)
507 * @refs_by_desc: rbtree of refs ordered by ref->desc
508 * (protected by @outer_lock)
509 * @refs_by_node: rbtree of refs ordered by ref->node
510 * (protected by @outer_lock)
511 * @waiting_threads: threads currently waiting for proc work
512 * (protected by @inner_lock)
513 * @pid PID of group_leader of process
514 * (invariant after initialized)
515 * @tsk task_struct for group_leader of process
516 * (invariant after initialized)
517 * @files files_struct for process
518 * (protected by @files_lock)
519 * @files_lock mutex to protect @files
520 * @deferred_work_node: element for binder_deferred_list
521 * (protected by binder_deferred_lock)
522 * @deferred_work: bitmap of deferred work to perform
523 * (protected by binder_deferred_lock)
524 * @is_dead: process is dead and awaiting free
525 * when outstanding transactions are cleaned up
526 * (protected by @inner_lock)
527 * @todo: list of work for this process
528 * (protected by @inner_lock)
529 * @stats: per-process binder statistics
530 * (atomics, no lock needed)
531 * @delivered_death: list of delivered death notification
532 * (protected by @inner_lock)
533 * @max_threads: cap on number of binder threads
534 * (protected by @inner_lock)
535 * @requested_threads: number of binder threads requested but not
536 * yet started. In current implementation, can
538 * (protected by @inner_lock)
539 * @requested_threads_started: number binder threads started
540 * (protected by @inner_lock)
541 * @tmp_ref: temporary reference to indicate proc is in use
542 * (protected by @inner_lock)
543 * @default_priority: default scheduler priority
544 * (invariant after initialized)
545 * @debugfs_entry: debugfs node
546 * @alloc: binder allocator bookkeeping
547 * @context: binder_context for this proc
548 * (invariant after initialized)
549 * @inner_lock: can nest under outer_lock and/or node lock
550 * @outer_lock: no nesting under innor or node lock
551 * Lock order: 1) outer, 2) node, 3) inner
553 * Bookkeeping structure for binder processes
556 struct hlist_node proc_node
;
557 struct rb_root threads
;
558 struct rb_root nodes
;
559 struct rb_root refs_by_desc
;
560 struct rb_root refs_by_node
;
561 struct list_head waiting_threads
;
563 struct task_struct
*tsk
;
564 struct files_struct
*files
;
565 struct mutex files_lock
;
566 struct hlist_node deferred_work_node
;
570 struct list_head todo
;
571 struct binder_stats stats
;
572 struct list_head delivered_death
;
574 int requested_threads
;
575 int requested_threads_started
;
577 struct binder_priority default_priority
;
578 struct dentry
*debugfs_entry
;
579 struct binder_alloc alloc
;
580 struct binder_context
*context
;
581 spinlock_t inner_lock
;
582 spinlock_t outer_lock
;
586 BINDER_LOOPER_STATE_REGISTERED
= 0x01,
587 BINDER_LOOPER_STATE_ENTERED
= 0x02,
588 BINDER_LOOPER_STATE_EXITED
= 0x04,
589 BINDER_LOOPER_STATE_INVALID
= 0x08,
590 BINDER_LOOPER_STATE_WAITING
= 0x10,
591 BINDER_LOOPER_STATE_POLL
= 0x20,
595 * struct binder_thread - binder thread bookkeeping
596 * @proc: binder process for this thread
597 * (invariant after initialization)
598 * @rb_node: element for proc->threads rbtree
599 * (protected by @proc->inner_lock)
600 * @waiting_thread_node: element for @proc->waiting_threads list
601 * (protected by @proc->inner_lock)
602 * @pid: PID for this thread
603 * (invariant after initialization)
604 * @looper: bitmap of looping state
605 * (only accessed by this thread)
606 * @looper_needs_return: looping thread needs to exit driver
608 * @transaction_stack: stack of in-progress transactions for this thread
609 * (protected by @proc->inner_lock)
610 * @todo: list of work to do for this thread
611 * (protected by @proc->inner_lock)
612 * @process_todo: whether work in @todo should be processed
613 * (protected by @proc->inner_lock)
614 * @return_error: transaction errors reported by this thread
615 * (only accessed by this thread)
616 * @reply_error: transaction errors reported by target thread
617 * (protected by @proc->inner_lock)
618 * @wait: wait queue for thread work
619 * @stats: per-thread statistics
620 * (atomics, no lock needed)
621 * @tmp_ref: temporary reference to indicate thread is in use
622 * (atomic since @proc->inner_lock cannot
623 * always be acquired)
624 * @is_dead: thread is dead and awaiting free
625 * when outstanding transactions are cleaned up
626 * (protected by @proc->inner_lock)
627 * @task: struct task_struct for this thread
629 * Bookkeeping structure for binder threads.
631 struct binder_thread
{
632 struct binder_proc
*proc
;
633 struct rb_node rb_node
;
634 struct list_head waiting_thread_node
;
636 int looper
; /* only modified by this thread */
637 bool looper_need_return
; /* can be written by other thread */
638 struct binder_transaction
*transaction_stack
;
639 struct list_head todo
;
641 struct binder_error return_error
;
642 struct binder_error reply_error
;
643 wait_queue_head_t wait
;
644 struct binder_stats stats
;
647 struct task_struct
*task
;
650 struct binder_transaction
{
652 struct binder_work work
;
653 struct binder_thread
*from
;
654 struct binder_transaction
*from_parent
;
655 struct binder_proc
*to_proc
;
656 struct binder_thread
*to_thread
;
657 struct binder_transaction
*to_parent
;
658 unsigned need_reply
:1;
659 /* unsigned is_dead:1; */ /* not used at the moment */
661 struct binder_buffer
*buffer
;
664 struct binder_priority priority
;
665 struct binder_priority saved_priority
;
666 bool set_priority_called
;
668 binder_uintptr_t security_ctx
;
670 * @lock: protects @from, @to_proc, and @to_thread
672 * @from, @to_proc, and @to_thread can be set to NULL
673 * during thread teardown
679 * binder_proc_lock() - Acquire outer lock for given binder_proc
680 * @proc: struct binder_proc to acquire
682 * Acquires proc->outer_lock. Used to protect binder_ref
683 * structures associated with the given proc.
685 #define binder_proc_lock(proc) _binder_proc_lock(proc, __LINE__)
687 _binder_proc_lock(struct binder_proc
*proc
, int line
)
689 binder_debug(BINDER_DEBUG_SPINLOCKS
,
690 "%s: line=%d\n", __func__
, line
);
691 spin_lock(&proc
->outer_lock
);
695 * binder_proc_unlock() - Release spinlock for given binder_proc
696 * @proc: struct binder_proc to acquire
698 * Release lock acquired via binder_proc_lock()
700 #define binder_proc_unlock(_proc) _binder_proc_unlock(_proc, __LINE__)
702 _binder_proc_unlock(struct binder_proc
*proc
, int line
)
704 binder_debug(BINDER_DEBUG_SPINLOCKS
,
705 "%s: line=%d\n", __func__
, line
);
706 spin_unlock(&proc
->outer_lock
);
710 * binder_inner_proc_lock() - Acquire inner lock for given binder_proc
711 * @proc: struct binder_proc to acquire
713 * Acquires proc->inner_lock. Used to protect todo lists
715 #define binder_inner_proc_lock(proc) _binder_inner_proc_lock(proc, __LINE__)
717 _binder_inner_proc_lock(struct binder_proc
*proc
, int line
)
719 binder_debug(BINDER_DEBUG_SPINLOCKS
,
720 "%s: line=%d\n", __func__
, line
);
721 spin_lock(&proc
->inner_lock
);
725 * binder_inner_proc_unlock() - Release inner lock for given binder_proc
726 * @proc: struct binder_proc to acquire
728 * Release lock acquired via binder_inner_proc_lock()
730 #define binder_inner_proc_unlock(proc) _binder_inner_proc_unlock(proc, __LINE__)
732 _binder_inner_proc_unlock(struct binder_proc
*proc
, int line
)
734 binder_debug(BINDER_DEBUG_SPINLOCKS
,
735 "%s: line=%d\n", __func__
, line
);
736 spin_unlock(&proc
->inner_lock
);
740 * binder_node_lock() - Acquire spinlock for given binder_node
741 * @node: struct binder_node to acquire
743 * Acquires node->lock. Used to protect binder_node fields
745 #define binder_node_lock(node) _binder_node_lock(node, __LINE__)
747 _binder_node_lock(struct binder_node
*node
, int line
)
749 binder_debug(BINDER_DEBUG_SPINLOCKS
,
750 "%s: line=%d\n", __func__
, line
);
751 spin_lock(&node
->lock
);
755 * binder_node_unlock() - Release spinlock for given binder_proc
756 * @node: struct binder_node to acquire
758 * Release lock acquired via binder_node_lock()
760 #define binder_node_unlock(node) _binder_node_unlock(node, __LINE__)
762 _binder_node_unlock(struct binder_node
*node
, int line
)
764 binder_debug(BINDER_DEBUG_SPINLOCKS
,
765 "%s: line=%d\n", __func__
, line
);
766 spin_unlock(&node
->lock
);
770 * binder_node_inner_lock() - Acquire node and inner locks
771 * @node: struct binder_node to acquire
773 * Acquires node->lock. If node->proc also acquires
774 * proc->inner_lock. Used to protect binder_node fields
776 #define binder_node_inner_lock(node) _binder_node_inner_lock(node, __LINE__)
778 _binder_node_inner_lock(struct binder_node
*node
, int line
)
780 binder_debug(BINDER_DEBUG_SPINLOCKS
,
781 "%s: line=%d\n", __func__
, line
);
782 spin_lock(&node
->lock
);
784 binder_inner_proc_lock(node
->proc
);
788 * binder_node_unlock() - Release node and inner locks
789 * @node: struct binder_node to acquire
791 * Release lock acquired via binder_node_lock()
793 #define binder_node_inner_unlock(node) _binder_node_inner_unlock(node, __LINE__)
795 _binder_node_inner_unlock(struct binder_node
*node
, int line
)
797 struct binder_proc
*proc
= node
->proc
;
799 binder_debug(BINDER_DEBUG_SPINLOCKS
,
800 "%s: line=%d\n", __func__
, line
);
802 binder_inner_proc_unlock(proc
);
803 spin_unlock(&node
->lock
);
806 static bool binder_worklist_empty_ilocked(struct list_head
*list
)
808 return list_empty(list
);
812 * binder_worklist_empty() - Check if no items on the work list
813 * @proc: binder_proc associated with list
814 * @list: list to check
816 * Return: true if there are no items on list, else false
818 static bool binder_worklist_empty(struct binder_proc
*proc
,
819 struct list_head
*list
)
823 binder_inner_proc_lock(proc
);
824 ret
= binder_worklist_empty_ilocked(list
);
825 binder_inner_proc_unlock(proc
);
830 * binder_enqueue_work_ilocked() - Add an item to the work list
831 * @work: struct binder_work to add to list
832 * @target_list: list to add work to
834 * Adds the work to the specified list. Asserts that work
835 * is not already on a list.
837 * Requires the proc->inner_lock to be held.
840 binder_enqueue_work_ilocked(struct binder_work
*work
,
841 struct list_head
*target_list
)
843 BUG_ON(target_list
== NULL
);
844 BUG_ON(work
->entry
.next
&& !list_empty(&work
->entry
));
845 list_add_tail(&work
->entry
, target_list
);
849 * binder_enqueue_deferred_thread_work_ilocked() - Add deferred thread work
850 * @thread: thread to queue work to
851 * @work: struct binder_work to add to list
853 * Adds the work to the todo list of the thread. Doesn't set the process_todo
854 * flag, which means that (if it wasn't already set) the thread will go to
855 * sleep without handling this work when it calls read.
857 * Requires the proc->inner_lock to be held.
860 binder_enqueue_deferred_thread_work_ilocked(struct binder_thread
*thread
,
861 struct binder_work
*work
)
863 binder_enqueue_work_ilocked(work
, &thread
->todo
);
867 * binder_enqueue_thread_work_ilocked() - Add an item to the thread work list
868 * @thread: thread to queue work to
869 * @work: struct binder_work to add to list
871 * Adds the work to the todo list of the thread, and enables processing
874 * Requires the proc->inner_lock to be held.
877 binder_enqueue_thread_work_ilocked(struct binder_thread
*thread
,
878 struct binder_work
*work
)
880 binder_enqueue_work_ilocked(work
, &thread
->todo
);
881 thread
->process_todo
= true;
885 * binder_enqueue_thread_work() - Add an item to the thread work list
886 * @thread: thread to queue work to
887 * @work: struct binder_work to add to list
889 * Adds the work to the todo list of the thread, and enables processing
893 binder_enqueue_thread_work(struct binder_thread
*thread
,
894 struct binder_work
*work
)
896 binder_inner_proc_lock(thread
->proc
);
897 binder_enqueue_thread_work_ilocked(thread
, work
);
898 binder_inner_proc_unlock(thread
->proc
);
902 binder_dequeue_work_ilocked(struct binder_work
*work
)
904 list_del_init(&work
->entry
);
908 * binder_dequeue_work() - Removes an item from the work list
909 * @proc: binder_proc associated with list
910 * @work: struct binder_work to remove from list
912 * Removes the specified work item from whatever list it is on.
913 * Can safely be called if work is not on any list.
916 binder_dequeue_work(struct binder_proc
*proc
, struct binder_work
*work
)
918 binder_inner_proc_lock(proc
);
919 binder_dequeue_work_ilocked(work
);
920 binder_inner_proc_unlock(proc
);
923 static struct binder_work
*binder_dequeue_work_head_ilocked(
924 struct list_head
*list
)
926 struct binder_work
*w
;
928 w
= list_first_entry_or_null(list
, struct binder_work
, entry
);
930 list_del_init(&w
->entry
);
935 binder_defer_work(struct binder_proc
*proc
, enum binder_deferred_state defer
);
936 static void binder_free_thread(struct binder_thread
*thread
);
937 static void binder_free_proc(struct binder_proc
*proc
);
938 static void binder_inc_node_tmpref_ilocked(struct binder_node
*node
);
940 static int task_get_unused_fd_flags(struct binder_proc
*proc
, int flags
)
942 unsigned long rlim_cur
;
946 mutex_lock(&proc
->files_lock
);
947 if (proc
->files
== NULL
) {
951 if (!lock_task_sighand(proc
->tsk
, &irqs
)) {
955 rlim_cur
= task_rlimit(proc
->tsk
, RLIMIT_NOFILE
);
956 unlock_task_sighand(proc
->tsk
, &irqs
);
958 ret
= __alloc_fd(proc
->files
, 0, rlim_cur
, flags
);
960 mutex_unlock(&proc
->files_lock
);
965 * copied from fd_install
967 static void task_fd_install(
968 struct binder_proc
*proc
, unsigned int fd
, struct file
*file
)
970 mutex_lock(&proc
->files_lock
);
972 __fd_install(proc
->files
, fd
, file
);
973 mutex_unlock(&proc
->files_lock
);
977 * copied from sys_close
979 static long task_close_fd(struct binder_proc
*proc
, unsigned int fd
)
983 mutex_lock(&proc
->files_lock
);
984 if (proc
->files
== NULL
) {
988 retval
= __close_fd(proc
->files
, fd
);
989 /* can't restart close syscall because file table entry was cleared */
990 if (unlikely(retval
== -ERESTARTSYS
||
991 retval
== -ERESTARTNOINTR
||
992 retval
== -ERESTARTNOHAND
||
993 retval
== -ERESTART_RESTARTBLOCK
))
996 mutex_unlock(&proc
->files_lock
);
1000 static bool binder_has_work_ilocked(struct binder_thread
*thread
,
1003 return thread
->process_todo
||
1004 thread
->looper_need_return
||
1006 !binder_worklist_empty_ilocked(&thread
->proc
->todo
));
1009 static bool binder_has_work(struct binder_thread
*thread
, bool do_proc_work
)
1013 binder_inner_proc_lock(thread
->proc
);
1014 has_work
= binder_has_work_ilocked(thread
, do_proc_work
);
1015 binder_inner_proc_unlock(thread
->proc
);
1020 static bool binder_available_for_proc_work_ilocked(struct binder_thread
*thread
)
1022 return !thread
->transaction_stack
&&
1023 binder_worklist_empty_ilocked(&thread
->todo
) &&
1024 (thread
->looper
& (BINDER_LOOPER_STATE_ENTERED
|
1025 BINDER_LOOPER_STATE_REGISTERED
));
1028 static void binder_wakeup_poll_threads_ilocked(struct binder_proc
*proc
,
1032 struct binder_thread
*thread
;
1034 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
)) {
1035 thread
= rb_entry(n
, struct binder_thread
, rb_node
);
1036 if (thread
->looper
& BINDER_LOOPER_STATE_POLL
&&
1037 binder_available_for_proc_work_ilocked(thread
)) {
1039 wake_up_interruptible_sync(&thread
->wait
);
1041 wake_up_interruptible(&thread
->wait
);
1047 * binder_select_thread_ilocked() - selects a thread for doing proc work.
1048 * @proc: process to select a thread from
1050 * Note that calling this function moves the thread off the waiting_threads
1051 * list, so it can only be woken up by the caller of this function, or a
1052 * signal. Therefore, callers *should* always wake up the thread this function
1055 * Return: If there's a thread currently waiting for process work,
1056 * returns that thread. Otherwise returns NULL.
1058 static struct binder_thread
*
1059 binder_select_thread_ilocked(struct binder_proc
*proc
)
1061 struct binder_thread
*thread
;
1063 assert_spin_locked(&proc
->inner_lock
);
1064 thread
= list_first_entry_or_null(&proc
->waiting_threads
,
1065 struct binder_thread
,
1066 waiting_thread_node
);
1069 list_del_init(&thread
->waiting_thread_node
);
1075 * binder_wakeup_thread_ilocked() - wakes up a thread for doing proc work.
1076 * @proc: process to wake up a thread in
1077 * @thread: specific thread to wake-up (may be NULL)
1078 * @sync: whether to do a synchronous wake-up
1080 * This function wakes up a thread in the @proc process.
1081 * The caller may provide a specific thread to wake-up in
1082 * the @thread parameter. If @thread is NULL, this function
1083 * will wake up threads that have called poll().
1085 * Note that for this function to work as expected, callers
1086 * should first call binder_select_thread() to find a thread
1087 * to handle the work (if they don't have a thread already),
1088 * and pass the result into the @thread parameter.
1090 static void binder_wakeup_thread_ilocked(struct binder_proc
*proc
,
1091 struct binder_thread
*thread
,
1094 assert_spin_locked(&proc
->inner_lock
);
1098 wake_up_interruptible_sync(&thread
->wait
);
1100 wake_up_interruptible(&thread
->wait
);
1104 /* Didn't find a thread waiting for proc work; this can happen
1106 * 1. All threads are busy handling transactions
1107 * In that case, one of those threads should call back into
1108 * the kernel driver soon and pick up this work.
1109 * 2. Threads are using the (e)poll interface, in which case
1110 * they may be blocked on the waitqueue without having been
1111 * added to waiting_threads. For this case, we just iterate
1112 * over all threads not handling transaction work, and
1113 * wake them all up. We wake all because we don't know whether
1114 * a thread that called into (e)poll is handling non-binder
1117 binder_wakeup_poll_threads_ilocked(proc
, sync
);
1120 static void binder_wakeup_proc_ilocked(struct binder_proc
*proc
)
1122 struct binder_thread
*thread
= binder_select_thread_ilocked(proc
);
1124 binder_wakeup_thread_ilocked(proc
, thread
, /* sync = */false);
1127 static bool is_rt_policy(int policy
)
1129 return policy
== SCHED_FIFO
|| policy
== SCHED_RR
;
1132 static bool is_fair_policy(int policy
)
1134 return policy
== SCHED_NORMAL
|| policy
== SCHED_BATCH
;
1137 static bool binder_supported_policy(int policy
)
1139 return is_fair_policy(policy
) || is_rt_policy(policy
);
1142 static int to_userspace_prio(int policy
, int kernel_priority
)
1144 if (is_fair_policy(policy
))
1145 return PRIO_TO_NICE(kernel_priority
);
1147 return MAX_USER_RT_PRIO
- 1 - kernel_priority
;
1150 static int to_kernel_prio(int policy
, int user_priority
)
1152 if (is_fair_policy(policy
))
1153 return NICE_TO_PRIO(user_priority
);
1155 return MAX_USER_RT_PRIO
- 1 - user_priority
;
1158 static void binder_do_set_priority(struct task_struct
*task
,
1159 struct binder_priority desired
,
1162 int priority
; /* user-space prio value */
1164 unsigned int policy
= desired
.sched_policy
;
1166 if (task
->policy
== policy
&& task
->normal_prio
== desired
.prio
)
1169 has_cap_nice
= has_capability_noaudit(task
, CAP_SYS_NICE
);
1171 priority
= to_userspace_prio(policy
, desired
.prio
);
1173 if (verify
&& is_rt_policy(policy
) && !has_cap_nice
) {
1174 long max_rtprio
= task_rlimit(task
, RLIMIT_RTPRIO
);
1176 if (max_rtprio
== 0) {
1177 policy
= SCHED_NORMAL
;
1178 priority
= MIN_NICE
;
1179 } else if (priority
> max_rtprio
) {
1180 priority
= max_rtprio
;
1184 if (verify
&& is_fair_policy(policy
) && !has_cap_nice
) {
1185 long min_nice
= (MAX_NICE
- task_rlimit(task
, RLIMIT_NICE
) + 1);
1187 if (min_nice
> MAX_NICE
) {
1188 binder_user_error("%d RLIMIT_NICE not set\n",
1191 } else if (priority
< min_nice
) {
1192 priority
= min_nice
;
1196 if (policy
!= desired
.sched_policy
||
1197 to_kernel_prio(policy
, priority
) != desired
.prio
)
1198 binder_debug(BINDER_DEBUG_PRIORITY_CAP
,
1199 "%d: priority %d not allowed, using %d instead\n",
1200 task
->pid
, desired
.prio
,
1201 to_kernel_prio(policy
, priority
));
1203 trace_binder_set_priority(task
->tgid
, task
->pid
, task
->normal_prio
,
1204 to_kernel_prio(policy
, priority
),
1207 /* Set the actual priority */
1208 if (task
->policy
!= policy
|| is_rt_policy(policy
)) {
1209 struct sched_param params
;
1211 params
.sched_priority
= is_rt_policy(policy
) ? priority
: 0;
1213 sched_setscheduler_nocheck(task
,
1214 policy
| SCHED_RESET_ON_FORK
,
1217 if (is_fair_policy(policy
))
1218 set_user_nice(task
, priority
);
1221 static void binder_set_priority(struct task_struct
*task
,
1222 struct binder_priority desired
)
1224 binder_do_set_priority(task
, desired
, /* verify = */ true);
1227 static void binder_restore_priority(struct task_struct
*task
,
1228 struct binder_priority desired
)
1230 binder_do_set_priority(task
, desired
, /* verify = */ false);
1233 static void binder_transaction_priority(struct task_struct
*task
,
1234 struct binder_transaction
*t
,
1235 struct binder_priority node_prio
,
1238 struct binder_priority desired_prio
= t
->priority
;
1240 if (t
->set_priority_called
)
1243 t
->set_priority_called
= true;
1244 t
->saved_priority
.sched_policy
= task
->policy
;
1245 t
->saved_priority
.prio
= task
->normal_prio
;
1247 if (!inherit_rt
&& is_rt_policy(desired_prio
.sched_policy
)) {
1248 desired_prio
.prio
= NICE_TO_PRIO(0);
1249 desired_prio
.sched_policy
= SCHED_NORMAL
;
1252 if (node_prio
.prio
< t
->priority
.prio
||
1253 (node_prio
.prio
== t
->priority
.prio
&&
1254 node_prio
.sched_policy
== SCHED_FIFO
)) {
1256 * In case the minimum priority on the node is
1257 * higher (lower value), use that priority. If
1258 * the priority is the same, but the node uses
1259 * SCHED_FIFO, prefer SCHED_FIFO, since it can
1260 * run unbounded, unlike SCHED_RR.
1262 desired_prio
= node_prio
;
1265 binder_set_priority(task
, desired_prio
);
1268 static struct binder_node
*binder_get_node_ilocked(struct binder_proc
*proc
,
1269 binder_uintptr_t ptr
)
1271 struct rb_node
*n
= proc
->nodes
.rb_node
;
1272 struct binder_node
*node
;
1274 assert_spin_locked(&proc
->inner_lock
);
1277 node
= rb_entry(n
, struct binder_node
, rb_node
);
1279 if (ptr
< node
->ptr
)
1281 else if (ptr
> node
->ptr
)
1285 * take an implicit weak reference
1286 * to ensure node stays alive until
1287 * call to binder_put_node()
1289 binder_inc_node_tmpref_ilocked(node
);
1296 static struct binder_node
*binder_get_node(struct binder_proc
*proc
,
1297 binder_uintptr_t ptr
)
1299 struct binder_node
*node
;
1301 binder_inner_proc_lock(proc
);
1302 node
= binder_get_node_ilocked(proc
, ptr
);
1303 binder_inner_proc_unlock(proc
);
1307 static struct binder_node
*binder_init_node_ilocked(
1308 struct binder_proc
*proc
,
1309 struct binder_node
*new_node
,
1310 struct flat_binder_object
*fp
)
1312 struct rb_node
**p
= &proc
->nodes
.rb_node
;
1313 struct rb_node
*parent
= NULL
;
1314 struct binder_node
*node
;
1315 binder_uintptr_t ptr
= fp
? fp
->binder
: 0;
1316 binder_uintptr_t cookie
= fp
? fp
->cookie
: 0;
1317 __u32 flags
= fp
? fp
->flags
: 0;
1320 assert_spin_locked(&proc
->inner_lock
);
1325 node
= rb_entry(parent
, struct binder_node
, rb_node
);
1327 if (ptr
< node
->ptr
)
1329 else if (ptr
> node
->ptr
)
1330 p
= &(*p
)->rb_right
;
1333 * A matching node is already in
1334 * the rb tree. Abandon the init
1337 binder_inc_node_tmpref_ilocked(node
);
1342 binder_stats_created(BINDER_STAT_NODE
);
1344 rb_link_node(&node
->rb_node
, parent
, p
);
1345 rb_insert_color(&node
->rb_node
, &proc
->nodes
);
1346 node
->debug_id
= atomic_inc_return(&binder_last_id
);
1349 node
->cookie
= cookie
;
1350 node
->work
.type
= BINDER_WORK_NODE
;
1351 priority
= flags
& FLAT_BINDER_FLAG_PRIORITY_MASK
;
1352 node
->sched_policy
= (flags
& FLAT_BINDER_FLAG_SCHED_POLICY_MASK
) >>
1353 FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT
;
1354 node
->min_priority
= to_kernel_prio(node
->sched_policy
, priority
);
1355 node
->accept_fds
= !!(flags
& FLAT_BINDER_FLAG_ACCEPTS_FDS
);
1356 node
->inherit_rt
= !!(flags
& FLAT_BINDER_FLAG_INHERIT_RT
);
1357 node
->txn_security_ctx
= !!(flags
& FLAT_BINDER_FLAG_TXN_SECURITY_CTX
);
1358 spin_lock_init(&node
->lock
);
1359 INIT_LIST_HEAD(&node
->work
.entry
);
1360 INIT_LIST_HEAD(&node
->async_todo
);
1361 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1362 "%d:%d node %d u%016llx c%016llx created\n",
1363 proc
->pid
, current
->pid
, node
->debug_id
,
1364 (u64
)node
->ptr
, (u64
)node
->cookie
);
1369 static struct binder_node
*binder_new_node(struct binder_proc
*proc
,
1370 struct flat_binder_object
*fp
)
1372 struct binder_node
*node
;
1373 struct binder_node
*new_node
= kzalloc(sizeof(*node
), GFP_KERNEL
);
1377 binder_inner_proc_lock(proc
);
1378 node
= binder_init_node_ilocked(proc
, new_node
, fp
);
1379 binder_inner_proc_unlock(proc
);
1380 if (node
!= new_node
)
1382 * The node was already added by another thread
1389 static void binder_free_node(struct binder_node
*node
)
1392 binder_stats_deleted(BINDER_STAT_NODE
);
1395 static int binder_inc_node_nilocked(struct binder_node
*node
, int strong
,
1397 struct list_head
*target_list
)
1399 struct binder_proc
*proc
= node
->proc
;
1401 assert_spin_locked(&node
->lock
);
1403 assert_spin_locked(&proc
->inner_lock
);
1406 if (target_list
== NULL
&&
1407 node
->internal_strong_refs
== 0 &&
1409 node
== node
->proc
->context
->
1410 binder_context_mgr_node
&&
1411 node
->has_strong_ref
)) {
1412 pr_err("invalid inc strong node for %d\n",
1416 node
->internal_strong_refs
++;
1418 node
->local_strong_refs
++;
1419 if (!node
->has_strong_ref
&& target_list
) {
1420 binder_dequeue_work_ilocked(&node
->work
);
1422 * Note: this function is the only place where we queue
1423 * directly to a thread->todo without using the
1424 * corresponding binder_enqueue_thread_work() helper
1425 * functions; in this case it's ok to not set the
1426 * process_todo flag, since we know this node work will
1427 * always be followed by other work that starts queue
1428 * processing: in case of synchronous transactions, a
1429 * BR_REPLY or BR_ERROR; in case of oneway
1430 * transactions, a BR_TRANSACTION_COMPLETE.
1432 binder_enqueue_work_ilocked(&node
->work
, target_list
);
1436 node
->local_weak_refs
++;
1437 if (!node
->has_weak_ref
&& list_empty(&node
->work
.entry
)) {
1438 if (target_list
== NULL
) {
1439 pr_err("invalid inc weak node for %d\n",
1446 binder_enqueue_work_ilocked(&node
->work
, target_list
);
1452 static int binder_inc_node(struct binder_node
*node
, int strong
, int internal
,
1453 struct list_head
*target_list
)
1457 binder_node_inner_lock(node
);
1458 ret
= binder_inc_node_nilocked(node
, strong
, internal
, target_list
);
1459 binder_node_inner_unlock(node
);
1464 static bool binder_dec_node_nilocked(struct binder_node
*node
,
1465 int strong
, int internal
)
1467 struct binder_proc
*proc
= node
->proc
;
1469 assert_spin_locked(&node
->lock
);
1471 assert_spin_locked(&proc
->inner_lock
);
1474 node
->internal_strong_refs
--;
1476 node
->local_strong_refs
--;
1477 if (node
->local_strong_refs
|| node
->internal_strong_refs
)
1481 node
->local_weak_refs
--;
1482 if (node
->local_weak_refs
|| node
->tmp_refs
||
1483 !hlist_empty(&node
->refs
))
1487 if (proc
&& (node
->has_strong_ref
|| node
->has_weak_ref
)) {
1488 if (list_empty(&node
->work
.entry
)) {
1489 binder_enqueue_work_ilocked(&node
->work
, &proc
->todo
);
1490 binder_wakeup_proc_ilocked(proc
);
1493 if (hlist_empty(&node
->refs
) && !node
->local_strong_refs
&&
1494 !node
->local_weak_refs
&& !node
->tmp_refs
) {
1496 binder_dequeue_work_ilocked(&node
->work
);
1497 rb_erase(&node
->rb_node
, &proc
->nodes
);
1498 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1499 "refless node %d deleted\n",
1502 BUG_ON(!list_empty(&node
->work
.entry
));
1503 spin_lock(&binder_dead_nodes_lock
);
1505 * tmp_refs could have changed so
1508 if (node
->tmp_refs
) {
1509 spin_unlock(&binder_dead_nodes_lock
);
1512 hlist_del(&node
->dead_node
);
1513 spin_unlock(&binder_dead_nodes_lock
);
1514 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1515 "dead node %d deleted\n",
1524 static void binder_dec_node(struct binder_node
*node
, int strong
, int internal
)
1528 binder_node_inner_lock(node
);
1529 free_node
= binder_dec_node_nilocked(node
, strong
, internal
);
1530 binder_node_inner_unlock(node
);
1532 binder_free_node(node
);
1535 static void binder_inc_node_tmpref_ilocked(struct binder_node
*node
)
1538 * No call to binder_inc_node() is needed since we
1539 * don't need to inform userspace of any changes to
1546 * binder_inc_node_tmpref() - take a temporary reference on node
1547 * @node: node to reference
1549 * Take reference on node to prevent the node from being freed
1550 * while referenced only by a local variable. The inner lock is
1551 * needed to serialize with the node work on the queue (which
1552 * isn't needed after the node is dead). If the node is dead
1553 * (node->proc is NULL), use binder_dead_nodes_lock to protect
1554 * node->tmp_refs against dead-node-only cases where the node
1555 * lock cannot be acquired (eg traversing the dead node list to
1558 static void binder_inc_node_tmpref(struct binder_node
*node
)
1560 binder_node_lock(node
);
1562 binder_inner_proc_lock(node
->proc
);
1564 spin_lock(&binder_dead_nodes_lock
);
1565 binder_inc_node_tmpref_ilocked(node
);
1567 binder_inner_proc_unlock(node
->proc
);
1569 spin_unlock(&binder_dead_nodes_lock
);
1570 binder_node_unlock(node
);
1574 * binder_dec_node_tmpref() - remove a temporary reference on node
1575 * @node: node to reference
1577 * Release temporary reference on node taken via binder_inc_node_tmpref()
1579 static void binder_dec_node_tmpref(struct binder_node
*node
)
1583 binder_node_inner_lock(node
);
1585 spin_lock(&binder_dead_nodes_lock
);
1587 BUG_ON(node
->tmp_refs
< 0);
1589 spin_unlock(&binder_dead_nodes_lock
);
1591 * Call binder_dec_node() to check if all refcounts are 0
1592 * and cleanup is needed. Calling with strong=0 and internal=1
1593 * causes no actual reference to be released in binder_dec_node().
1594 * If that changes, a change is needed here too.
1596 free_node
= binder_dec_node_nilocked(node
, 0, 1);
1597 binder_node_inner_unlock(node
);
1599 binder_free_node(node
);
1602 static void binder_put_node(struct binder_node
*node
)
1604 binder_dec_node_tmpref(node
);
1607 static struct binder_ref
*binder_get_ref_olocked(struct binder_proc
*proc
,
1608 u32 desc
, bool need_strong_ref
)
1610 struct rb_node
*n
= proc
->refs_by_desc
.rb_node
;
1611 struct binder_ref
*ref
;
1614 ref
= rb_entry(n
, struct binder_ref
, rb_node_desc
);
1616 if (desc
< ref
->data
.desc
) {
1618 } else if (desc
> ref
->data
.desc
) {
1620 } else if (need_strong_ref
&& !ref
->data
.strong
) {
1621 binder_user_error("tried to use weak ref as strong ref\n");
1631 * binder_get_ref_for_node_olocked() - get the ref associated with given node
1632 * @proc: binder_proc that owns the ref
1633 * @node: binder_node of target
1634 * @new_ref: newly allocated binder_ref to be initialized or %NULL
1636 * Look up the ref for the given node and return it if it exists
1638 * If it doesn't exist and the caller provides a newly allocated
1639 * ref, initialize the fields of the newly allocated ref and insert
1640 * into the given proc rb_trees and node refs list.
1642 * Return: the ref for node. It is possible that another thread
1643 * allocated/initialized the ref first in which case the
1644 * returned ref would be different than the passed-in
1645 * new_ref. new_ref must be kfree'd by the caller in
1648 static struct binder_ref
*binder_get_ref_for_node_olocked(
1649 struct binder_proc
*proc
,
1650 struct binder_node
*node
,
1651 struct binder_ref
*new_ref
)
1653 struct binder_context
*context
= proc
->context
;
1654 struct rb_node
**p
= &proc
->refs_by_node
.rb_node
;
1655 struct rb_node
*parent
= NULL
;
1656 struct binder_ref
*ref
;
1661 ref
= rb_entry(parent
, struct binder_ref
, rb_node_node
);
1663 if (node
< ref
->node
)
1665 else if (node
> ref
->node
)
1666 p
= &(*p
)->rb_right
;
1673 binder_stats_created(BINDER_STAT_REF
);
1674 new_ref
->data
.debug_id
= atomic_inc_return(&binder_last_id
);
1675 new_ref
->proc
= proc
;
1676 new_ref
->node
= node
;
1677 rb_link_node(&new_ref
->rb_node_node
, parent
, p
);
1678 rb_insert_color(&new_ref
->rb_node_node
, &proc
->refs_by_node
);
1680 new_ref
->data
.desc
= (node
== context
->binder_context_mgr_node
) ? 0 : 1;
1681 for (n
= rb_first(&proc
->refs_by_desc
); n
!= NULL
; n
= rb_next(n
)) {
1682 ref
= rb_entry(n
, struct binder_ref
, rb_node_desc
);
1683 if (ref
->data
.desc
> new_ref
->data
.desc
)
1685 new_ref
->data
.desc
= ref
->data
.desc
+ 1;
1688 p
= &proc
->refs_by_desc
.rb_node
;
1691 ref
= rb_entry(parent
, struct binder_ref
, rb_node_desc
);
1693 if (new_ref
->data
.desc
< ref
->data
.desc
)
1695 else if (new_ref
->data
.desc
> ref
->data
.desc
)
1696 p
= &(*p
)->rb_right
;
1700 rb_link_node(&new_ref
->rb_node_desc
, parent
, p
);
1701 rb_insert_color(&new_ref
->rb_node_desc
, &proc
->refs_by_desc
);
1703 binder_node_lock(node
);
1704 hlist_add_head(&new_ref
->node_entry
, &node
->refs
);
1706 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1707 "%d new ref %d desc %d for node %d\n",
1708 proc
->pid
, new_ref
->data
.debug_id
, new_ref
->data
.desc
,
1710 binder_node_unlock(node
);
1714 static void binder_cleanup_ref_olocked(struct binder_ref
*ref
)
1716 bool delete_node
= false;
1718 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1719 "%d delete ref %d desc %d for node %d\n",
1720 ref
->proc
->pid
, ref
->data
.debug_id
, ref
->data
.desc
,
1721 ref
->node
->debug_id
);
1723 rb_erase(&ref
->rb_node_desc
, &ref
->proc
->refs_by_desc
);
1724 rb_erase(&ref
->rb_node_node
, &ref
->proc
->refs_by_node
);
1726 binder_node_inner_lock(ref
->node
);
1727 if (ref
->data
.strong
)
1728 binder_dec_node_nilocked(ref
->node
, 1, 1);
1730 hlist_del(&ref
->node_entry
);
1731 delete_node
= binder_dec_node_nilocked(ref
->node
, 0, 1);
1732 binder_node_inner_unlock(ref
->node
);
1734 * Clear ref->node unless we want the caller to free the node
1738 * The caller uses ref->node to determine
1739 * whether the node needs to be freed. Clear
1740 * it since the node is still alive.
1746 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
1747 "%d delete ref %d desc %d has death notification\n",
1748 ref
->proc
->pid
, ref
->data
.debug_id
,
1750 binder_dequeue_work(ref
->proc
, &ref
->death
->work
);
1751 binder_stats_deleted(BINDER_STAT_DEATH
);
1753 binder_stats_deleted(BINDER_STAT_REF
);
1757 * binder_inc_ref_olocked() - increment the ref for given handle
1758 * @ref: ref to be incremented
1759 * @strong: if true, strong increment, else weak
1760 * @target_list: list to queue node work on
1762 * Increment the ref. @ref->proc->outer_lock must be held on entry
1764 * Return: 0, if successful, else errno
1766 static int binder_inc_ref_olocked(struct binder_ref
*ref
, int strong
,
1767 struct list_head
*target_list
)
1772 if (ref
->data
.strong
== 0) {
1773 ret
= binder_inc_node(ref
->node
, 1, 1, target_list
);
1779 if (ref
->data
.weak
== 0) {
1780 ret
= binder_inc_node(ref
->node
, 0, 1, target_list
);
1790 * binder_dec_ref() - dec the ref for given handle
1791 * @ref: ref to be decremented
1792 * @strong: if true, strong decrement, else weak
1794 * Decrement the ref.
1796 * Return: true if ref is cleaned up and ready to be freed
1798 static bool binder_dec_ref_olocked(struct binder_ref
*ref
, int strong
)
1801 if (ref
->data
.strong
== 0) {
1802 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
1803 ref
->proc
->pid
, ref
->data
.debug_id
,
1804 ref
->data
.desc
, ref
->data
.strong
,
1809 if (ref
->data
.strong
== 0)
1810 binder_dec_node(ref
->node
, strong
, 1);
1812 if (ref
->data
.weak
== 0) {
1813 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
1814 ref
->proc
->pid
, ref
->data
.debug_id
,
1815 ref
->data
.desc
, ref
->data
.strong
,
1821 if (ref
->data
.strong
== 0 && ref
->data
.weak
== 0) {
1822 binder_cleanup_ref_olocked(ref
);
1829 * binder_get_node_from_ref() - get the node from the given proc/desc
1830 * @proc: proc containing the ref
1831 * @desc: the handle associated with the ref
1832 * @need_strong_ref: if true, only return node if ref is strong
1833 * @rdata: the id/refcount data for the ref
1835 * Given a proc and ref handle, return the associated binder_node
1837 * Return: a binder_node or NULL if not found or not strong when strong required
1839 static struct binder_node
*binder_get_node_from_ref(
1840 struct binder_proc
*proc
,
1841 u32 desc
, bool need_strong_ref
,
1842 struct binder_ref_data
*rdata
)
1844 struct binder_node
*node
;
1845 struct binder_ref
*ref
;
1847 binder_proc_lock(proc
);
1848 ref
= binder_get_ref_olocked(proc
, desc
, need_strong_ref
);
1853 * Take an implicit reference on the node to ensure
1854 * it stays alive until the call to binder_put_node()
1856 binder_inc_node_tmpref(node
);
1859 binder_proc_unlock(proc
);
1864 binder_proc_unlock(proc
);
1869 * binder_free_ref() - free the binder_ref
1872 * Free the binder_ref. Free the binder_node indicated by ref->node
1873 * (if non-NULL) and the binder_ref_death indicated by ref->death.
1875 static void binder_free_ref(struct binder_ref
*ref
)
1878 binder_free_node(ref
->node
);
1884 * binder_update_ref_for_handle() - inc/dec the ref for given handle
1885 * @proc: proc containing the ref
1886 * @desc: the handle associated with the ref
1887 * @increment: true=inc reference, false=dec reference
1888 * @strong: true=strong reference, false=weak reference
1889 * @rdata: the id/refcount data for the ref
1891 * Given a proc and ref handle, increment or decrement the ref
1892 * according to "increment" arg.
1894 * Return: 0 if successful, else errno
1896 static int binder_update_ref_for_handle(struct binder_proc
*proc
,
1897 uint32_t desc
, bool increment
, bool strong
,
1898 struct binder_ref_data
*rdata
)
1901 struct binder_ref
*ref
;
1902 bool delete_ref
= false;
1904 binder_proc_lock(proc
);
1905 ref
= binder_get_ref_olocked(proc
, desc
, strong
);
1911 ret
= binder_inc_ref_olocked(ref
, strong
, NULL
);
1913 delete_ref
= binder_dec_ref_olocked(ref
, strong
);
1917 binder_proc_unlock(proc
);
1920 binder_free_ref(ref
);
1924 binder_proc_unlock(proc
);
1929 * binder_dec_ref_for_handle() - dec the ref for given handle
1930 * @proc: proc containing the ref
1931 * @desc: the handle associated with the ref
1932 * @strong: true=strong reference, false=weak reference
1933 * @rdata: the id/refcount data for the ref
1935 * Just calls binder_update_ref_for_handle() to decrement the ref.
1937 * Return: 0 if successful, else errno
1939 static int binder_dec_ref_for_handle(struct binder_proc
*proc
,
1940 uint32_t desc
, bool strong
, struct binder_ref_data
*rdata
)
1942 return binder_update_ref_for_handle(proc
, desc
, false, strong
, rdata
);
1947 * binder_inc_ref_for_node() - increment the ref for given proc/node
1948 * @proc: proc containing the ref
1949 * @node: target node
1950 * @strong: true=strong reference, false=weak reference
1951 * @target_list: worklist to use if node is incremented
1952 * @rdata: the id/refcount data for the ref
1954 * Given a proc and node, increment the ref. Create the ref if it
1955 * doesn't already exist
1957 * Return: 0 if successful, else errno
1959 static int binder_inc_ref_for_node(struct binder_proc
*proc
,
1960 struct binder_node
*node
,
1962 struct list_head
*target_list
,
1963 struct binder_ref_data
*rdata
)
1965 struct binder_ref
*ref
;
1966 struct binder_ref
*new_ref
= NULL
;
1969 binder_proc_lock(proc
);
1970 ref
= binder_get_ref_for_node_olocked(proc
, node
, NULL
);
1972 binder_proc_unlock(proc
);
1973 new_ref
= kzalloc(sizeof(*ref
), GFP_KERNEL
);
1976 binder_proc_lock(proc
);
1977 ref
= binder_get_ref_for_node_olocked(proc
, node
, new_ref
);
1979 ret
= binder_inc_ref_olocked(ref
, strong
, target_list
);
1981 binder_proc_unlock(proc
);
1982 if (new_ref
&& ref
!= new_ref
)
1984 * Another thread created the ref first so
1985 * free the one we allocated
1991 static void binder_pop_transaction_ilocked(struct binder_thread
*target_thread
,
1992 struct binder_transaction
*t
)
1994 BUG_ON(!target_thread
);
1995 assert_spin_locked(&target_thread
->proc
->inner_lock
);
1996 BUG_ON(target_thread
->transaction_stack
!= t
);
1997 BUG_ON(target_thread
->transaction_stack
->from
!= target_thread
);
1998 target_thread
->transaction_stack
=
1999 target_thread
->transaction_stack
->from_parent
;
2004 * binder_thread_dec_tmpref() - decrement thread->tmp_ref
2005 * @thread: thread to decrement
2007 * A thread needs to be kept alive while being used to create or
2008 * handle a transaction. binder_get_txn_from() is used to safely
2009 * extract t->from from a binder_transaction and keep the thread
2010 * indicated by t->from from being freed. When done with that
2011 * binder_thread, this function is called to decrement the
2012 * tmp_ref and free if appropriate (thread has been released
2013 * and no transaction being processed by the driver)
2015 static void binder_thread_dec_tmpref(struct binder_thread
*thread
)
2018 * atomic is used to protect the counter value while
2019 * it cannot reach zero or thread->is_dead is false
2021 binder_inner_proc_lock(thread
->proc
);
2022 atomic_dec(&thread
->tmp_ref
);
2023 if (thread
->is_dead
&& !atomic_read(&thread
->tmp_ref
)) {
2024 binder_inner_proc_unlock(thread
->proc
);
2025 binder_free_thread(thread
);
2028 binder_inner_proc_unlock(thread
->proc
);
2032 * binder_proc_dec_tmpref() - decrement proc->tmp_ref
2033 * @proc: proc to decrement
2035 * A binder_proc needs to be kept alive while being used to create or
2036 * handle a transaction. proc->tmp_ref is incremented when
2037 * creating a new transaction or the binder_proc is currently in-use
2038 * by threads that are being released. When done with the binder_proc,
2039 * this function is called to decrement the counter and free the
2040 * proc if appropriate (proc has been released, all threads have
2041 * been released and not currenly in-use to process a transaction).
2043 static void binder_proc_dec_tmpref(struct binder_proc
*proc
)
2045 binder_inner_proc_lock(proc
);
2047 if (proc
->is_dead
&& RB_EMPTY_ROOT(&proc
->threads
) &&
2049 binder_inner_proc_unlock(proc
);
2050 binder_free_proc(proc
);
2053 binder_inner_proc_unlock(proc
);
2057 * binder_get_txn_from() - safely extract the "from" thread in transaction
2058 * @t: binder transaction for t->from
2060 * Atomically return the "from" thread and increment the tmp_ref
2061 * count for the thread to ensure it stays alive until
2062 * binder_thread_dec_tmpref() is called.
2064 * Return: the value of t->from
2066 static struct binder_thread
*binder_get_txn_from(
2067 struct binder_transaction
*t
)
2069 struct binder_thread
*from
;
2071 spin_lock(&t
->lock
);
2074 atomic_inc(&from
->tmp_ref
);
2075 spin_unlock(&t
->lock
);
2080 * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock
2081 * @t: binder transaction for t->from
2083 * Same as binder_get_txn_from() except it also acquires the proc->inner_lock
2084 * to guarantee that the thread cannot be released while operating on it.
2085 * The caller must call binder_inner_proc_unlock() to release the inner lock
2086 * as well as call binder_dec_thread_txn() to release the reference.
2088 * Return: the value of t->from
2090 static struct binder_thread
*binder_get_txn_from_and_acq_inner(
2091 struct binder_transaction
*t
)
2093 struct binder_thread
*from
;
2095 from
= binder_get_txn_from(t
);
2098 binder_inner_proc_lock(from
->proc
);
2100 BUG_ON(from
!= t
->from
);
2103 binder_inner_proc_unlock(from
->proc
);
2104 binder_thread_dec_tmpref(from
);
2108 static void binder_free_transaction(struct binder_transaction
*t
)
2110 struct binder_proc
*target_proc
= t
->to_proc
;
2113 binder_inner_proc_lock(target_proc
);
2115 t
->buffer
->transaction
= NULL
;
2116 binder_inner_proc_unlock(target_proc
);
2119 * If the transaction has no target_proc, then
2120 * t->buffer->transaction has already been cleared.
2123 binder_stats_deleted(BINDER_STAT_TRANSACTION
);
2126 static void binder_send_failed_reply(struct binder_transaction
*t
,
2127 uint32_t error_code
)
2129 struct binder_thread
*target_thread
;
2130 struct binder_transaction
*next
;
2132 BUG_ON(t
->flags
& TF_ONE_WAY
);
2134 target_thread
= binder_get_txn_from_and_acq_inner(t
);
2135 if (target_thread
) {
2136 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
2137 "send failed reply for transaction %d to %d:%d\n",
2139 target_thread
->proc
->pid
,
2140 target_thread
->pid
);
2142 binder_pop_transaction_ilocked(target_thread
, t
);
2143 if (target_thread
->reply_error
.cmd
== BR_OK
) {
2144 target_thread
->reply_error
.cmd
= error_code
;
2145 binder_enqueue_thread_work_ilocked(
2147 &target_thread
->reply_error
.work
);
2148 wake_up_interruptible(&target_thread
->wait
);
2150 WARN(1, "Unexpected reply error: %u\n",
2151 target_thread
->reply_error
.cmd
);
2153 binder_inner_proc_unlock(target_thread
->proc
);
2154 binder_thread_dec_tmpref(target_thread
);
2155 binder_free_transaction(t
);
2158 next
= t
->from_parent
;
2160 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
2161 "send failed reply for transaction %d, target dead\n",
2164 binder_free_transaction(t
);
2166 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
2167 "reply failed, no target thread at root\n");
2171 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
2172 "reply failed, no target thread -- retry %d\n",
2178 * binder_cleanup_transaction() - cleans up undelivered transaction
2179 * @t: transaction that needs to be cleaned up
2180 * @reason: reason the transaction wasn't delivered
2181 * @error_code: error to return to caller (if synchronous call)
2183 static void binder_cleanup_transaction(struct binder_transaction
*t
,
2185 uint32_t error_code
)
2187 if (t
->buffer
->target_node
&& !(t
->flags
& TF_ONE_WAY
)) {
2188 binder_send_failed_reply(t
, error_code
);
2190 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
2191 "undelivered transaction %d, %s\n",
2192 t
->debug_id
, reason
);
2193 binder_free_transaction(t
);
2198 * binder_validate_object() - checks for a valid metadata object in a buffer.
2199 * @buffer: binder_buffer that we're parsing.
2200 * @offset: offset in the buffer at which to validate an object.
2202 * Return: If there's a valid metadata object at @offset in @buffer, the
2203 * size of that object. Otherwise, it returns zero.
2205 static size_t binder_validate_object(struct binder_buffer
*buffer
, u64 offset
)
2207 /* Check if we can read a header first */
2208 struct binder_object_header
*hdr
;
2209 size_t object_size
= 0;
2211 if (offset
> buffer
->data_size
- sizeof(*hdr
) ||
2212 buffer
->data_size
< sizeof(*hdr
) ||
2213 !IS_ALIGNED(offset
, sizeof(u32
)))
2216 /* Ok, now see if we can read a complete object. */
2217 hdr
= (struct binder_object_header
*)(buffer
->data
+ offset
);
2218 switch (hdr
->type
) {
2219 case BINDER_TYPE_BINDER
:
2220 case BINDER_TYPE_WEAK_BINDER
:
2221 case BINDER_TYPE_HANDLE
:
2222 case BINDER_TYPE_WEAK_HANDLE
:
2223 object_size
= sizeof(struct flat_binder_object
);
2225 case BINDER_TYPE_FD
:
2226 object_size
= sizeof(struct binder_fd_object
);
2228 case BINDER_TYPE_PTR
:
2229 object_size
= sizeof(struct binder_buffer_object
);
2231 case BINDER_TYPE_FDA
:
2232 object_size
= sizeof(struct binder_fd_array_object
);
2237 if (offset
<= buffer
->data_size
- object_size
&&
2238 buffer
->data_size
>= object_size
)
2245 * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
2246 * @b: binder_buffer containing the object
2247 * @index: index in offset array at which the binder_buffer_object is
2249 * @start: points to the start of the offset array
2250 * @num_valid: the number of valid offsets in the offset array
2252 * Return: If @index is within the valid range of the offset array
2253 * described by @start and @num_valid, and if there's a valid
2254 * binder_buffer_object at the offset found in index @index
2255 * of the offset array, that object is returned. Otherwise,
2256 * %NULL is returned.
2257 * Note that the offset found in index @index itself is not
2258 * verified; this function assumes that @num_valid elements
2259 * from @start were previously verified to have valid offsets.
2261 static struct binder_buffer_object
*binder_validate_ptr(struct binder_buffer
*b
,
2262 binder_size_t index
,
2263 binder_size_t
*start
,
2264 binder_size_t num_valid
)
2266 struct binder_buffer_object
*buffer_obj
;
2267 binder_size_t
*offp
;
2269 if (index
>= num_valid
)
2272 offp
= start
+ index
;
2273 buffer_obj
= (struct binder_buffer_object
*)(b
->data
+ *offp
);
2274 if (buffer_obj
->hdr
.type
!= BINDER_TYPE_PTR
)
2281 * binder_validate_fixup() - validates pointer/fd fixups happen in order.
2282 * @b: transaction buffer
2283 * @objects_start start of objects buffer
2284 * @buffer: binder_buffer_object in which to fix up
2285 * @offset: start offset in @buffer to fix up
2286 * @last_obj: last binder_buffer_object that we fixed up in
2287 * @last_min_offset: minimum fixup offset in @last_obj
2289 * Return: %true if a fixup in buffer @buffer at offset @offset is
2292 * For safety reasons, we only allow fixups inside a buffer to happen
2293 * at increasing offsets; additionally, we only allow fixup on the last
2294 * buffer object that was verified, or one of its parents.
2296 * Example of what is allowed:
2299 * B (parent = A, offset = 0)
2300 * C (parent = A, offset = 16)
2301 * D (parent = C, offset = 0)
2302 * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
2304 * Examples of what is not allowed:
2306 * Decreasing offsets within the same parent:
2308 * C (parent = A, offset = 16)
2309 * B (parent = A, offset = 0) // decreasing offset within A
2311 * Referring to a parent that wasn't the last object or any of its parents:
2313 * B (parent = A, offset = 0)
2314 * C (parent = A, offset = 0)
2315 * C (parent = A, offset = 16)
2316 * D (parent = B, offset = 0) // B is not A or any of A's parents
2318 static bool binder_validate_fixup(struct binder_buffer
*b
,
2319 binder_size_t
*objects_start
,
2320 struct binder_buffer_object
*buffer
,
2321 binder_size_t fixup_offset
,
2322 struct binder_buffer_object
*last_obj
,
2323 binder_size_t last_min_offset
)
2326 /* Nothing to fix up in */
2330 while (last_obj
!= buffer
) {
2332 * Safe to retrieve the parent of last_obj, since it
2333 * was already previously verified by the driver.
2335 if ((last_obj
->flags
& BINDER_BUFFER_FLAG_HAS_PARENT
) == 0)
2337 last_min_offset
= last_obj
->parent_offset
+ sizeof(uintptr_t);
2338 last_obj
= (struct binder_buffer_object
*)
2339 (b
->data
+ *(objects_start
+ last_obj
->parent
));
2341 return (fixup_offset
>= last_min_offset
);
2344 static void binder_transaction_buffer_release(struct binder_proc
*proc
,
2345 struct binder_buffer
*buffer
,
2346 binder_size_t
*failed_at
)
2348 binder_size_t
*offp
, *off_start
, *off_end
;
2349 int debug_id
= buffer
->debug_id
;
2351 binder_debug(BINDER_DEBUG_TRANSACTION
,
2352 "%d buffer release %d, size %zd-%zd, failed at %p\n",
2353 proc
->pid
, buffer
->debug_id
,
2354 buffer
->data_size
, buffer
->offsets_size
, failed_at
);
2356 if (buffer
->target_node
)
2357 binder_dec_node(buffer
->target_node
, 1, 0);
2359 off_start
= (binder_size_t
*)(buffer
->data
+
2360 ALIGN(buffer
->data_size
, sizeof(void *)));
2362 off_end
= failed_at
;
2364 off_end
= (void *)off_start
+ buffer
->offsets_size
;
2365 for (offp
= off_start
; offp
< off_end
; offp
++) {
2366 struct binder_object_header
*hdr
;
2367 size_t object_size
= binder_validate_object(buffer
, *offp
);
2369 if (object_size
== 0) {
2370 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
2371 debug_id
, (u64
)*offp
, buffer
->data_size
);
2374 hdr
= (struct binder_object_header
*)(buffer
->data
+ *offp
);
2375 switch (hdr
->type
) {
2376 case BINDER_TYPE_BINDER
:
2377 case BINDER_TYPE_WEAK_BINDER
: {
2378 struct flat_binder_object
*fp
;
2379 struct binder_node
*node
;
2381 fp
= to_flat_binder_object(hdr
);
2382 node
= binder_get_node(proc
, fp
->binder
);
2384 pr_err("transaction release %d bad node %016llx\n",
2385 debug_id
, (u64
)fp
->binder
);
2388 binder_debug(BINDER_DEBUG_TRANSACTION
,
2389 " node %d u%016llx\n",
2390 node
->debug_id
, (u64
)node
->ptr
);
2391 binder_dec_node(node
, hdr
->type
== BINDER_TYPE_BINDER
,
2393 binder_put_node(node
);
2395 case BINDER_TYPE_HANDLE
:
2396 case BINDER_TYPE_WEAK_HANDLE
: {
2397 struct flat_binder_object
*fp
;
2398 struct binder_ref_data rdata
;
2401 fp
= to_flat_binder_object(hdr
);
2402 ret
= binder_dec_ref_for_handle(proc
, fp
->handle
,
2403 hdr
->type
== BINDER_TYPE_HANDLE
, &rdata
);
2406 pr_err("transaction release %d bad handle %d, ret = %d\n",
2407 debug_id
, fp
->handle
, ret
);
2410 binder_debug(BINDER_DEBUG_TRANSACTION
,
2411 " ref %d desc %d\n",
2412 rdata
.debug_id
, rdata
.desc
);
2415 case BINDER_TYPE_FD
: {
2416 struct binder_fd_object
*fp
= to_binder_fd_object(hdr
);
2418 binder_debug(BINDER_DEBUG_TRANSACTION
,
2419 " fd %d\n", fp
->fd
);
2421 task_close_fd(proc
, fp
->fd
);
2423 case BINDER_TYPE_PTR
:
2425 * Nothing to do here, this will get cleaned up when the
2426 * transaction buffer gets freed
2429 case BINDER_TYPE_FDA
: {
2430 struct binder_fd_array_object
*fda
;
2431 struct binder_buffer_object
*parent
;
2432 uintptr_t parent_buffer
;
2435 binder_size_t fd_buf_size
;
2437 fda
= to_binder_fd_array_object(hdr
);
2438 parent
= binder_validate_ptr(buffer
, fda
->parent
,
2442 pr_err("transaction release %d bad parent offset",
2447 * Since the parent was already fixed up, convert it
2448 * back to kernel address space to access it
2450 parent_buffer
= parent
->buffer
-
2451 binder_alloc_get_user_buffer_offset(
2454 fd_buf_size
= sizeof(u32
) * fda
->num_fds
;
2455 if (fda
->num_fds
>= SIZE_MAX
/ sizeof(u32
)) {
2456 pr_err("transaction release %d invalid number of fds (%lld)\n",
2457 debug_id
, (u64
)fda
->num_fds
);
2460 if (fd_buf_size
> parent
->length
||
2461 fda
->parent_offset
> parent
->length
- fd_buf_size
) {
2462 /* No space for all file descriptors here. */
2463 pr_err("transaction release %d not enough space for %lld fds in buffer\n",
2464 debug_id
, (u64
)fda
->num_fds
);
2467 fd_array
= (u32
*)(parent_buffer
+ (uintptr_t)fda
->parent_offset
);
2468 for (fd_index
= 0; fd_index
< fda
->num_fds
; fd_index
++)
2469 task_close_fd(proc
, fd_array
[fd_index
]);
2472 pr_err("transaction release %d bad object type %x\n",
2473 debug_id
, hdr
->type
);
2479 static int binder_translate_binder(struct flat_binder_object
*fp
,
2480 struct binder_transaction
*t
,
2481 struct binder_thread
*thread
)
2483 struct binder_node
*node
;
2484 struct binder_proc
*proc
= thread
->proc
;
2485 struct binder_proc
*target_proc
= t
->to_proc
;
2486 struct binder_ref_data rdata
;
2489 node
= binder_get_node(proc
, fp
->binder
);
2491 node
= binder_new_node(proc
, fp
);
2495 if (fp
->cookie
!= node
->cookie
) {
2496 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
2497 proc
->pid
, thread
->pid
, (u64
)fp
->binder
,
2498 node
->debug_id
, (u64
)fp
->cookie
,
2503 if (security_binder_transfer_binder(proc
->tsk
, target_proc
->tsk
)) {
2508 ret
= binder_inc_ref_for_node(target_proc
, node
,
2509 fp
->hdr
.type
== BINDER_TYPE_BINDER
,
2510 &thread
->todo
, &rdata
);
2514 if (fp
->hdr
.type
== BINDER_TYPE_BINDER
)
2515 fp
->hdr
.type
= BINDER_TYPE_HANDLE
;
2517 fp
->hdr
.type
= BINDER_TYPE_WEAK_HANDLE
;
2519 fp
->handle
= rdata
.desc
;
2522 trace_binder_transaction_node_to_ref(t
, node
, &rdata
);
2523 binder_debug(BINDER_DEBUG_TRANSACTION
,
2524 " node %d u%016llx -> ref %d desc %d\n",
2525 node
->debug_id
, (u64
)node
->ptr
,
2526 rdata
.debug_id
, rdata
.desc
);
2528 binder_put_node(node
);
2532 static int binder_translate_handle(struct flat_binder_object
*fp
,
2533 struct binder_transaction
*t
,
2534 struct binder_thread
*thread
)
2536 struct binder_proc
*proc
= thread
->proc
;
2537 struct binder_proc
*target_proc
= t
->to_proc
;
2538 struct binder_node
*node
;
2539 struct binder_ref_data src_rdata
;
2542 node
= binder_get_node_from_ref(proc
, fp
->handle
,
2543 fp
->hdr
.type
== BINDER_TYPE_HANDLE
, &src_rdata
);
2545 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
2546 proc
->pid
, thread
->pid
, fp
->handle
);
2549 if (security_binder_transfer_binder(proc
->tsk
, target_proc
->tsk
)) {
2554 binder_node_lock(node
);
2555 if (node
->proc
== target_proc
) {
2556 if (fp
->hdr
.type
== BINDER_TYPE_HANDLE
)
2557 fp
->hdr
.type
= BINDER_TYPE_BINDER
;
2559 fp
->hdr
.type
= BINDER_TYPE_WEAK_BINDER
;
2560 fp
->binder
= node
->ptr
;
2561 fp
->cookie
= node
->cookie
;
2563 binder_inner_proc_lock(node
->proc
);
2564 binder_inc_node_nilocked(node
,
2565 fp
->hdr
.type
== BINDER_TYPE_BINDER
,
2568 binder_inner_proc_unlock(node
->proc
);
2569 trace_binder_transaction_ref_to_node(t
, node
, &src_rdata
);
2570 binder_debug(BINDER_DEBUG_TRANSACTION
,
2571 " ref %d desc %d -> node %d u%016llx\n",
2572 src_rdata
.debug_id
, src_rdata
.desc
, node
->debug_id
,
2574 binder_node_unlock(node
);
2576 struct binder_ref_data dest_rdata
;
2578 binder_node_unlock(node
);
2579 ret
= binder_inc_ref_for_node(target_proc
, node
,
2580 fp
->hdr
.type
== BINDER_TYPE_HANDLE
,
2586 fp
->handle
= dest_rdata
.desc
;
2588 trace_binder_transaction_ref_to_ref(t
, node
, &src_rdata
,
2590 binder_debug(BINDER_DEBUG_TRANSACTION
,
2591 " ref %d desc %d -> ref %d desc %d (node %d)\n",
2592 src_rdata
.debug_id
, src_rdata
.desc
,
2593 dest_rdata
.debug_id
, dest_rdata
.desc
,
2597 binder_put_node(node
);
2601 static int binder_translate_fd(int fd
,
2602 struct binder_transaction
*t
,
2603 struct binder_thread
*thread
,
2604 struct binder_transaction
*in_reply_to
)
2606 struct binder_proc
*proc
= thread
->proc
;
2607 struct binder_proc
*target_proc
= t
->to_proc
;
2611 bool target_allows_fd
;
2614 target_allows_fd
= !!(in_reply_to
->flags
& TF_ACCEPT_FDS
);
2616 target_allows_fd
= t
->buffer
->target_node
->accept_fds
;
2617 if (!target_allows_fd
) {
2618 binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
2619 proc
->pid
, thread
->pid
,
2620 in_reply_to
? "reply" : "transaction",
2623 goto err_fd_not_accepted
;
2628 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
2629 proc
->pid
, thread
->pid
, fd
);
2633 ret
= security_binder_transfer_file(proc
->tsk
, target_proc
->tsk
, file
);
2639 target_fd
= task_get_unused_fd_flags(target_proc
, O_CLOEXEC
);
2640 if (target_fd
< 0) {
2642 goto err_get_unused_fd
;
2644 task_fd_install(target_proc
, target_fd
, file
);
2645 trace_binder_transaction_fd(t
, fd
, target_fd
);
2646 binder_debug(BINDER_DEBUG_TRANSACTION
, " fd %d -> %d\n",
2655 err_fd_not_accepted
:
2659 static int binder_translate_fd_array(struct binder_fd_array_object
*fda
,
2660 struct binder_buffer_object
*parent
,
2661 struct binder_transaction
*t
,
2662 struct binder_thread
*thread
,
2663 struct binder_transaction
*in_reply_to
)
2665 binder_size_t fdi
, fd_buf_size
, num_installed_fds
;
2667 uintptr_t parent_buffer
;
2669 struct binder_proc
*proc
= thread
->proc
;
2670 struct binder_proc
*target_proc
= t
->to_proc
;
2672 fd_buf_size
= sizeof(u32
) * fda
->num_fds
;
2673 if (fda
->num_fds
>= SIZE_MAX
/ sizeof(u32
)) {
2674 binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
2675 proc
->pid
, thread
->pid
, (u64
)fda
->num_fds
);
2678 if (fd_buf_size
> parent
->length
||
2679 fda
->parent_offset
> parent
->length
- fd_buf_size
) {
2680 /* No space for all file descriptors here. */
2681 binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
2682 proc
->pid
, thread
->pid
, (u64
)fda
->num_fds
);
2686 * Since the parent was already fixed up, convert it
2687 * back to the kernel address space to access it
2689 parent_buffer
= parent
->buffer
-
2690 binder_alloc_get_user_buffer_offset(&target_proc
->alloc
);
2691 fd_array
= (u32
*)(parent_buffer
+ (uintptr_t)fda
->parent_offset
);
2692 if (!IS_ALIGNED((unsigned long)fd_array
, sizeof(u32
))) {
2693 binder_user_error("%d:%d parent offset not aligned correctly.\n",
2694 proc
->pid
, thread
->pid
);
2697 for (fdi
= 0; fdi
< fda
->num_fds
; fdi
++) {
2698 target_fd
= binder_translate_fd(fd_array
[fdi
], t
, thread
,
2701 goto err_translate_fd_failed
;
2702 fd_array
[fdi
] = target_fd
;
2706 err_translate_fd_failed
:
2708 * Failed to allocate fd or security error, free fds
2711 num_installed_fds
= fdi
;
2712 for (fdi
= 0; fdi
< num_installed_fds
; fdi
++)
2713 task_close_fd(target_proc
, fd_array
[fdi
]);
2717 static int binder_fixup_parent(struct binder_transaction
*t
,
2718 struct binder_thread
*thread
,
2719 struct binder_buffer_object
*bp
,
2720 binder_size_t
*off_start
,
2721 binder_size_t num_valid
,
2722 struct binder_buffer_object
*last_fixup_obj
,
2723 binder_size_t last_fixup_min_off
)
2725 struct binder_buffer_object
*parent
;
2727 struct binder_buffer
*b
= t
->buffer
;
2728 struct binder_proc
*proc
= thread
->proc
;
2729 struct binder_proc
*target_proc
= t
->to_proc
;
2731 if (!(bp
->flags
& BINDER_BUFFER_FLAG_HAS_PARENT
))
2734 parent
= binder_validate_ptr(b
, bp
->parent
, off_start
, num_valid
);
2736 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
2737 proc
->pid
, thread
->pid
);
2741 if (!binder_validate_fixup(b
, off_start
,
2742 parent
, bp
->parent_offset
,
2744 last_fixup_min_off
)) {
2745 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
2746 proc
->pid
, thread
->pid
);
2750 if (parent
->length
< sizeof(binder_uintptr_t
) ||
2751 bp
->parent_offset
> parent
->length
- sizeof(binder_uintptr_t
)) {
2752 /* No space for a pointer here! */
2753 binder_user_error("%d:%d got transaction with invalid parent offset\n",
2754 proc
->pid
, thread
->pid
);
2757 parent_buffer
= (u8
*)((uintptr_t)parent
->buffer
-
2758 binder_alloc_get_user_buffer_offset(
2759 &target_proc
->alloc
));
2760 *(binder_uintptr_t
*)(parent_buffer
+ bp
->parent_offset
) = bp
->buffer
;
2766 * binder_proc_transaction() - sends a transaction to a process and wakes it up
2767 * @t: transaction to send
2768 * @proc: process to send the transaction to
2769 * @thread: thread in @proc to send the transaction to (may be NULL)
2771 * This function queues a transaction to the specified process. It will try
2772 * to find a thread in the target process to handle the transaction and
2773 * wake it up. If no thread is found, the work is queued to the proc
2776 * If the @thread parameter is not NULL, the transaction is always queued
2777 * to the waitlist of that specific thread.
2779 * Return: true if the transactions was successfully queued
2780 * false if the target process or thread is dead
2782 static bool binder_proc_transaction(struct binder_transaction
*t
,
2783 struct binder_proc
*proc
,
2784 struct binder_thread
*thread
)
2786 struct binder_node
*node
= t
->buffer
->target_node
;
2787 struct binder_priority node_prio
;
2788 bool oneway
= !!(t
->flags
& TF_ONE_WAY
);
2789 bool pending_async
= false;
2792 binder_node_lock(node
);
2793 node_prio
.prio
= node
->min_priority
;
2794 node_prio
.sched_policy
= node
->sched_policy
;
2798 if (node
->has_async_transaction
) {
2799 pending_async
= true;
2801 node
->has_async_transaction
= 1;
2805 binder_inner_proc_lock(proc
);
2807 if (proc
->is_dead
|| (thread
&& thread
->is_dead
)) {
2808 binder_inner_proc_unlock(proc
);
2809 binder_node_unlock(node
);
2813 if (!thread
&& !pending_async
)
2814 thread
= binder_select_thread_ilocked(proc
);
2817 binder_transaction_priority(thread
->task
, t
, node_prio
,
2819 binder_enqueue_thread_work_ilocked(thread
, &t
->work
);
2820 } else if (!pending_async
) {
2821 binder_enqueue_work_ilocked(&t
->work
, &proc
->todo
);
2823 binder_enqueue_work_ilocked(&t
->work
, &node
->async_todo
);
2827 binder_wakeup_thread_ilocked(proc
, thread
, !oneway
/* sync */);
2829 binder_inner_proc_unlock(proc
);
2830 binder_node_unlock(node
);
2836 * binder_get_node_refs_for_txn() - Get required refs on node for txn
2837 * @node: struct binder_node for which to get refs
2838 * @proc: returns @node->proc if valid
2839 * @error: if no @proc then returns BR_DEAD_REPLY
2841 * User-space normally keeps the node alive when creating a transaction
2842 * since it has a reference to the target. The local strong ref keeps it
2843 * alive if the sending process dies before the target process processes
2844 * the transaction. If the source process is malicious or has a reference
2845 * counting bug, relying on the local strong ref can fail.
2847 * Since user-space can cause the local strong ref to go away, we also take
2848 * a tmpref on the node to ensure it survives while we are constructing
2849 * the transaction. We also need a tmpref on the proc while we are
2850 * constructing the transaction, so we take that here as well.
2852 * Return: The target_node with refs taken or NULL if no @node->proc is NULL.
2853 * Also sets @proc if valid. If the @node->proc is NULL indicating that the
2854 * target proc has died, @error is set to BR_DEAD_REPLY
2856 static struct binder_node
*binder_get_node_refs_for_txn(
2857 struct binder_node
*node
,
2858 struct binder_proc
**procp
,
2861 struct binder_node
*target_node
= NULL
;
2863 binder_node_inner_lock(node
);
2866 binder_inc_node_nilocked(node
, 1, 0, NULL
);
2867 binder_inc_node_tmpref_ilocked(node
);
2868 node
->proc
->tmp_ref
++;
2869 *procp
= node
->proc
;
2871 *error
= BR_DEAD_REPLY
;
2872 binder_node_inner_unlock(node
);
2877 static void binder_transaction(struct binder_proc
*proc
,
2878 struct binder_thread
*thread
,
2879 struct binder_transaction_data
*tr
, int reply
,
2880 binder_size_t extra_buffers_size
)
2883 struct binder_transaction
*t
;
2884 struct binder_work
*tcomplete
;
2885 binder_size_t
*offp
, *off_end
, *off_start
;
2886 binder_size_t off_min
;
2887 u8
*sg_bufp
, *sg_buf_end
;
2888 struct binder_proc
*target_proc
= NULL
;
2889 struct binder_thread
*target_thread
= NULL
;
2890 struct binder_node
*target_node
= NULL
;
2891 struct binder_transaction
*in_reply_to
= NULL
;
2892 struct binder_transaction_log_entry
*e
;
2893 uint32_t return_error
= 0;
2894 uint32_t return_error_param
= 0;
2895 uint32_t return_error_line
= 0;
2896 struct binder_buffer_object
*last_fixup_obj
= NULL
;
2897 binder_size_t last_fixup_min_off
= 0;
2898 struct binder_context
*context
= proc
->context
;
2899 int t_debug_id
= atomic_inc_return(&binder_last_id
);
2900 char *secctx
= NULL
;
2903 e
= binder_transaction_log_add(&binder_transaction_log
);
2904 e
->debug_id
= t_debug_id
;
2905 e
->call_type
= reply
? 2 : !!(tr
->flags
& TF_ONE_WAY
);
2906 e
->from_proc
= proc
->pid
;
2907 e
->from_thread
= thread
->pid
;
2908 e
->target_handle
= tr
->target
.handle
;
2909 e
->data_size
= tr
->data_size
;
2910 e
->offsets_size
= tr
->offsets_size
;
2911 e
->context_name
= proc
->context
->name
;
2914 binder_inner_proc_lock(proc
);
2915 in_reply_to
= thread
->transaction_stack
;
2916 if (in_reply_to
== NULL
) {
2917 binder_inner_proc_unlock(proc
);
2918 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
2919 proc
->pid
, thread
->pid
);
2920 return_error
= BR_FAILED_REPLY
;
2921 return_error_param
= -EPROTO
;
2922 return_error_line
= __LINE__
;
2923 goto err_empty_call_stack
;
2925 if (in_reply_to
->to_thread
!= thread
) {
2926 spin_lock(&in_reply_to
->lock
);
2927 binder_user_error("%d:%d got reply transaction with bad transaction stack, transaction %d has target %d:%d\n",
2928 proc
->pid
, thread
->pid
, in_reply_to
->debug_id
,
2929 in_reply_to
->to_proc
?
2930 in_reply_to
->to_proc
->pid
: 0,
2931 in_reply_to
->to_thread
?
2932 in_reply_to
->to_thread
->pid
: 0);
2933 spin_unlock(&in_reply_to
->lock
);
2934 binder_inner_proc_unlock(proc
);
2935 return_error
= BR_FAILED_REPLY
;
2936 return_error_param
= -EPROTO
;
2937 return_error_line
= __LINE__
;
2939 goto err_bad_call_stack
;
2941 thread
->transaction_stack
= in_reply_to
->to_parent
;
2942 binder_inner_proc_unlock(proc
);
2943 target_thread
= binder_get_txn_from_and_acq_inner(in_reply_to
);
2944 if (target_thread
== NULL
) {
2945 return_error
= BR_DEAD_REPLY
;
2946 return_error_line
= __LINE__
;
2947 goto err_dead_binder
;
2949 if (target_thread
->transaction_stack
!= in_reply_to
) {
2950 binder_user_error("%d:%d got reply transaction with bad target transaction stack %d, expected %d\n",
2951 proc
->pid
, thread
->pid
,
2952 target_thread
->transaction_stack
?
2953 target_thread
->transaction_stack
->debug_id
: 0,
2954 in_reply_to
->debug_id
);
2955 binder_inner_proc_unlock(target_thread
->proc
);
2956 return_error
= BR_FAILED_REPLY
;
2957 return_error_param
= -EPROTO
;
2958 return_error_line
= __LINE__
;
2960 target_thread
= NULL
;
2961 goto err_dead_binder
;
2963 target_proc
= target_thread
->proc
;
2964 target_proc
->tmp_ref
++;
2965 binder_inner_proc_unlock(target_thread
->proc
);
2967 if (tr
->target
.handle
) {
2968 struct binder_ref
*ref
;
2971 * There must already be a strong ref
2972 * on this node. If so, do a strong
2973 * increment on the node to ensure it
2974 * stays alive until the transaction is
2977 binder_proc_lock(proc
);
2978 ref
= binder_get_ref_olocked(proc
, tr
->target
.handle
,
2981 target_node
= binder_get_node_refs_for_txn(
2982 ref
->node
, &target_proc
,
2985 binder_user_error("%d:%d got transaction to invalid handle\n",
2986 proc
->pid
, thread
->pid
);
2987 return_error
= BR_FAILED_REPLY
;
2989 binder_proc_unlock(proc
);
2991 mutex_lock(&context
->context_mgr_node_lock
);
2992 target_node
= context
->binder_context_mgr_node
;
2994 target_node
= binder_get_node_refs_for_txn(
2995 target_node
, &target_proc
,
2998 return_error
= BR_DEAD_REPLY
;
2999 mutex_unlock(&context
->context_mgr_node_lock
);
3000 if (target_node
&& target_proc
== proc
) {
3001 binder_user_error("%d:%d got transaction to context manager from process owning it\n",
3002 proc
->pid
, thread
->pid
);
3003 return_error
= BR_FAILED_REPLY
;
3004 return_error_param
= -EINVAL
;
3005 return_error_line
= __LINE__
;
3006 goto err_invalid_target_handle
;
3011 * return_error is set above
3013 return_error_param
= -EINVAL
;
3014 return_error_line
= __LINE__
;
3015 goto err_dead_binder
;
3017 e
->to_node
= target_node
->debug_id
;
3018 if (security_binder_transaction(proc
->tsk
,
3019 target_proc
->tsk
) < 0) {
3020 return_error
= BR_FAILED_REPLY
;
3021 return_error_param
= -EPERM
;
3022 return_error_line
= __LINE__
;
3023 goto err_invalid_target_handle
;
3025 binder_inner_proc_lock(proc
);
3026 if (!(tr
->flags
& TF_ONE_WAY
) && thread
->transaction_stack
) {
3027 struct binder_transaction
*tmp
;
3029 tmp
= thread
->transaction_stack
;
3030 if (tmp
->to_thread
!= thread
) {
3031 spin_lock(&tmp
->lock
);
3032 binder_user_error("%d:%d got new transaction with bad transaction stack, transaction %d has target %d:%d\n",
3033 proc
->pid
, thread
->pid
, tmp
->debug_id
,
3034 tmp
->to_proc
? tmp
->to_proc
->pid
: 0,
3036 tmp
->to_thread
->pid
: 0);
3037 spin_unlock(&tmp
->lock
);
3038 binder_inner_proc_unlock(proc
);
3039 return_error
= BR_FAILED_REPLY
;
3040 return_error_param
= -EPROTO
;
3041 return_error_line
= __LINE__
;
3042 goto err_bad_call_stack
;
3045 struct binder_thread
*from
;
3047 spin_lock(&tmp
->lock
);
3049 if (from
&& from
->proc
== target_proc
) {
3050 atomic_inc(&from
->tmp_ref
);
3051 target_thread
= from
;
3052 spin_unlock(&tmp
->lock
);
3055 spin_unlock(&tmp
->lock
);
3056 tmp
= tmp
->from_parent
;
3059 binder_inner_proc_unlock(proc
);
3062 e
->to_thread
= target_thread
->pid
;
3063 e
->to_proc
= target_proc
->pid
;
3065 /* TODO: reuse incoming transaction for reply */
3066 t
= kzalloc(sizeof(*t
), GFP_KERNEL
);
3068 return_error
= BR_FAILED_REPLY
;
3069 return_error_param
= -ENOMEM
;
3070 return_error_line
= __LINE__
;
3071 goto err_alloc_t_failed
;
3073 binder_stats_created(BINDER_STAT_TRANSACTION
);
3074 spin_lock_init(&t
->lock
);
3076 tcomplete
= kzalloc(sizeof(*tcomplete
), GFP_KERNEL
);
3077 if (tcomplete
== NULL
) {
3078 return_error
= BR_FAILED_REPLY
;
3079 return_error_param
= -ENOMEM
;
3080 return_error_line
= __LINE__
;
3081 goto err_alloc_tcomplete_failed
;
3083 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE
);
3085 t
->debug_id
= t_debug_id
;
3088 binder_debug(BINDER_DEBUG_TRANSACTION
,
3089 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
3090 proc
->pid
, thread
->pid
, t
->debug_id
,
3091 target_proc
->pid
, target_thread
->pid
,
3092 (u64
)tr
->data
.ptr
.buffer
,
3093 (u64
)tr
->data
.ptr
.offsets
,
3094 (u64
)tr
->data_size
, (u64
)tr
->offsets_size
,
3095 (u64
)extra_buffers_size
);
3097 binder_debug(BINDER_DEBUG_TRANSACTION
,
3098 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
3099 proc
->pid
, thread
->pid
, t
->debug_id
,
3100 target_proc
->pid
, target_node
->debug_id
,
3101 (u64
)tr
->data
.ptr
.buffer
,
3102 (u64
)tr
->data
.ptr
.offsets
,
3103 (u64
)tr
->data_size
, (u64
)tr
->offsets_size
,
3104 (u64
)extra_buffers_size
);
3106 if (!reply
&& !(tr
->flags
& TF_ONE_WAY
))
3110 t
->sender_euid
= task_euid(proc
->tsk
);
3111 t
->to_proc
= target_proc
;
3112 t
->to_thread
= target_thread
;
3114 t
->flags
= tr
->flags
;
3115 if (!(t
->flags
& TF_ONE_WAY
) &&
3116 binder_supported_policy(current
->policy
)) {
3117 /* Inherit supported policies for synchronous transactions */
3118 t
->priority
.sched_policy
= current
->policy
;
3119 t
->priority
.prio
= current
->normal_prio
;
3121 /* Otherwise, fall back to the default priority */
3122 t
->priority
= target_proc
->default_priority
;
3125 if (target_node
&& target_node
->txn_security_ctx
) {
3129 security_task_getsecid(proc
->tsk
, &secid
);
3130 ret
= security_secid_to_secctx(secid
, &secctx
, &secctx_sz
);
3132 return_error
= BR_FAILED_REPLY
;
3133 return_error_param
= ret
;
3134 return_error_line
= __LINE__
;
3135 goto err_get_secctx_failed
;
3137 added_size
= ALIGN(secctx_sz
, sizeof(u64
));
3138 extra_buffers_size
+= added_size
;
3139 if (extra_buffers_size
< added_size
) {
3140 /* integer overflow of extra_buffers_size */
3141 return_error
= BR_FAILED_REPLY
;
3142 return_error_param
= EINVAL
;
3143 return_error_line
= __LINE__
;
3144 goto err_bad_extra_size
;
3148 trace_binder_transaction(reply
, t
, target_node
);
3150 t
->buffer
= binder_alloc_new_buf(&target_proc
->alloc
, tr
->data_size
,
3151 tr
->offsets_size
, extra_buffers_size
,
3152 !reply
&& (t
->flags
& TF_ONE_WAY
));
3153 if (IS_ERR(t
->buffer
)) {
3155 * -ESRCH indicates VMA cleared. The target is dying.
3157 return_error_param
= PTR_ERR(t
->buffer
);
3158 return_error
= return_error_param
== -ESRCH
?
3159 BR_DEAD_REPLY
: BR_FAILED_REPLY
;
3160 return_error_line
= __LINE__
;
3162 goto err_binder_alloc_buf_failed
;
3165 size_t buf_offset
= ALIGN(tr
->data_size
, sizeof(void *)) +
3166 ALIGN(tr
->offsets_size
, sizeof(void *)) +
3167 ALIGN(extra_buffers_size
, sizeof(void *)) -
3168 ALIGN(secctx_sz
, sizeof(u64
));
3169 char *kptr
= t
->buffer
->data
+ buf_offset
;
3171 t
->security_ctx
= (uintptr_t)kptr
+
3172 binder_alloc_get_user_buffer_offset(&target_proc
->alloc
);
3173 memcpy(kptr
, secctx
, secctx_sz
);
3174 security_release_secctx(secctx
, secctx_sz
);
3177 t
->buffer
->debug_id
= t
->debug_id
;
3178 t
->buffer
->transaction
= t
;
3179 t
->buffer
->target_node
= target_node
;
3180 trace_binder_transaction_alloc_buf(t
->buffer
);
3181 off_start
= (binder_size_t
*)(t
->buffer
->data
+
3182 ALIGN(tr
->data_size
, sizeof(void *)));
3185 if (copy_from_user(t
->buffer
->data
, (const void __user
*)(uintptr_t)
3186 tr
->data
.ptr
.buffer
, tr
->data_size
)) {
3187 binder_user_error("%d:%d got transaction with invalid data ptr\n",
3188 proc
->pid
, thread
->pid
);
3189 return_error
= BR_FAILED_REPLY
;
3190 return_error_param
= -EFAULT
;
3191 return_error_line
= __LINE__
;
3192 goto err_copy_data_failed
;
3194 if (copy_from_user(offp
, (const void __user
*)(uintptr_t)
3195 tr
->data
.ptr
.offsets
, tr
->offsets_size
)) {
3196 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3197 proc
->pid
, thread
->pid
);
3198 return_error
= BR_FAILED_REPLY
;
3199 return_error_param
= -EFAULT
;
3200 return_error_line
= __LINE__
;
3201 goto err_copy_data_failed
;
3203 if (!IS_ALIGNED(tr
->offsets_size
, sizeof(binder_size_t
))) {
3204 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
3205 proc
->pid
, thread
->pid
, (u64
)tr
->offsets_size
);
3206 return_error
= BR_FAILED_REPLY
;
3207 return_error_param
= -EINVAL
;
3208 return_error_line
= __LINE__
;
3209 goto err_bad_offset
;
3211 if (!IS_ALIGNED(extra_buffers_size
, sizeof(u64
))) {
3212 binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
3213 proc
->pid
, thread
->pid
,
3214 extra_buffers_size
);
3215 return_error
= BR_FAILED_REPLY
;
3216 return_error_param
= -EINVAL
;
3217 return_error_line
= __LINE__
;
3218 goto err_bad_offset
;
3220 off_end
= (void *)off_start
+ tr
->offsets_size
;
3221 sg_bufp
= (u8
*)(PTR_ALIGN(off_end
, sizeof(void *)));
3222 sg_buf_end
= sg_bufp
+ extra_buffers_size
-
3223 ALIGN(secctx_sz
, sizeof(u64
));
3225 for (; offp
< off_end
; offp
++) {
3226 struct binder_object_header
*hdr
;
3227 size_t object_size
= binder_validate_object(t
->buffer
, *offp
);
3229 if (object_size
== 0 || *offp
< off_min
) {
3230 binder_user_error("%d:%d got transaction with invalid offset (%lld, min %lld max %lld) or object.\n",
3231 proc
->pid
, thread
->pid
, (u64
)*offp
,
3233 (u64
)t
->buffer
->data_size
);
3234 return_error
= BR_FAILED_REPLY
;
3235 return_error_param
= -EINVAL
;
3236 return_error_line
= __LINE__
;
3237 goto err_bad_offset
;
3240 hdr
= (struct binder_object_header
*)(t
->buffer
->data
+ *offp
);
3241 off_min
= *offp
+ object_size
;
3242 switch (hdr
->type
) {
3243 case BINDER_TYPE_BINDER
:
3244 case BINDER_TYPE_WEAK_BINDER
: {
3245 struct flat_binder_object
*fp
;
3247 fp
= to_flat_binder_object(hdr
);
3248 ret
= binder_translate_binder(fp
, t
, thread
);
3250 return_error
= BR_FAILED_REPLY
;
3251 return_error_param
= ret
;
3252 return_error_line
= __LINE__
;
3253 goto err_translate_failed
;
3256 case BINDER_TYPE_HANDLE
:
3257 case BINDER_TYPE_WEAK_HANDLE
: {
3258 struct flat_binder_object
*fp
;
3260 fp
= to_flat_binder_object(hdr
);
3261 ret
= binder_translate_handle(fp
, t
, thread
);
3263 return_error
= BR_FAILED_REPLY
;
3264 return_error_param
= ret
;
3265 return_error_line
= __LINE__
;
3266 goto err_translate_failed
;
3270 case BINDER_TYPE_FD
: {
3271 struct binder_fd_object
*fp
= to_binder_fd_object(hdr
);
3272 int target_fd
= binder_translate_fd(fp
->fd
, t
, thread
,
3275 if (target_fd
< 0) {
3276 return_error
= BR_FAILED_REPLY
;
3277 return_error_param
= target_fd
;
3278 return_error_line
= __LINE__
;
3279 goto err_translate_failed
;
3284 case BINDER_TYPE_FDA
: {
3285 struct binder_fd_array_object
*fda
=
3286 to_binder_fd_array_object(hdr
);
3287 struct binder_buffer_object
*parent
=
3288 binder_validate_ptr(t
->buffer
, fda
->parent
,
3292 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
3293 proc
->pid
, thread
->pid
);
3294 return_error
= BR_FAILED_REPLY
;
3295 return_error_param
= -EINVAL
;
3296 return_error_line
= __LINE__
;
3297 goto err_bad_parent
;
3299 if (!binder_validate_fixup(t
->buffer
, off_start
,
3300 parent
, fda
->parent_offset
,
3302 last_fixup_min_off
)) {
3303 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
3304 proc
->pid
, thread
->pid
);
3305 return_error
= BR_FAILED_REPLY
;
3306 return_error_param
= -EINVAL
;
3307 return_error_line
= __LINE__
;
3308 goto err_bad_parent
;
3310 ret
= binder_translate_fd_array(fda
, parent
, t
, thread
,
3313 return_error
= BR_FAILED_REPLY
;
3314 return_error_param
= ret
;
3315 return_error_line
= __LINE__
;
3316 goto err_translate_failed
;
3318 last_fixup_obj
= parent
;
3319 last_fixup_min_off
=
3320 fda
->parent_offset
+ sizeof(u32
) * fda
->num_fds
;
3322 case BINDER_TYPE_PTR
: {
3323 struct binder_buffer_object
*bp
=
3324 to_binder_buffer_object(hdr
);
3325 size_t buf_left
= sg_buf_end
- sg_bufp
;
3327 if (bp
->length
> buf_left
) {
3328 binder_user_error("%d:%d got transaction with too large buffer\n",
3329 proc
->pid
, thread
->pid
);
3330 return_error
= BR_FAILED_REPLY
;
3331 return_error_param
= -EINVAL
;
3332 return_error_line
= __LINE__
;
3333 goto err_bad_offset
;
3335 if (copy_from_user(sg_bufp
,
3336 (const void __user
*)(uintptr_t)
3337 bp
->buffer
, bp
->length
)) {
3338 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3339 proc
->pid
, thread
->pid
);
3340 return_error_param
= -EFAULT
;
3341 return_error
= BR_FAILED_REPLY
;
3342 return_error_line
= __LINE__
;
3343 goto err_copy_data_failed
;
3345 /* Fixup buffer pointer to target proc address space */
3346 bp
->buffer
= (uintptr_t)sg_bufp
+
3347 binder_alloc_get_user_buffer_offset(
3348 &target_proc
->alloc
);
3349 sg_bufp
+= ALIGN(bp
->length
, sizeof(u64
));
3351 ret
= binder_fixup_parent(t
, thread
, bp
, off_start
,
3354 last_fixup_min_off
);
3356 return_error
= BR_FAILED_REPLY
;
3357 return_error_param
= ret
;
3358 return_error_line
= __LINE__
;
3359 goto err_translate_failed
;
3361 last_fixup_obj
= bp
;
3362 last_fixup_min_off
= 0;
3365 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
3366 proc
->pid
, thread
->pid
, hdr
->type
);
3367 return_error
= BR_FAILED_REPLY
;
3368 return_error_param
= -EINVAL
;
3369 return_error_line
= __LINE__
;
3370 goto err_bad_object_type
;
3373 tcomplete
->type
= BINDER_WORK_TRANSACTION_COMPLETE
;
3374 t
->work
.type
= BINDER_WORK_TRANSACTION
;
3377 binder_enqueue_thread_work(thread
, tcomplete
);
3378 binder_inner_proc_lock(target_proc
);
3379 if (target_thread
->is_dead
) {
3380 binder_inner_proc_unlock(target_proc
);
3381 goto err_dead_proc_or_thread
;
3383 BUG_ON(t
->buffer
->async_transaction
!= 0);
3384 binder_pop_transaction_ilocked(target_thread
, in_reply_to
);
3385 binder_enqueue_thread_work_ilocked(target_thread
, &t
->work
);
3386 binder_inner_proc_unlock(target_proc
);
3387 wake_up_interruptible_sync(&target_thread
->wait
);
3388 binder_restore_priority(current
, in_reply_to
->saved_priority
);
3389 binder_free_transaction(in_reply_to
);
3390 } else if (!(t
->flags
& TF_ONE_WAY
)) {
3391 BUG_ON(t
->buffer
->async_transaction
!= 0);
3392 binder_inner_proc_lock(proc
);
3394 * Defer the TRANSACTION_COMPLETE, so we don't return to
3395 * userspace immediately; this allows the target process to
3396 * immediately start processing this transaction, reducing
3397 * latency. We will then return the TRANSACTION_COMPLETE when
3398 * the target replies (or there is an error).
3400 binder_enqueue_deferred_thread_work_ilocked(thread
, tcomplete
);
3402 t
->from_parent
= thread
->transaction_stack
;
3403 thread
->transaction_stack
= t
;
3404 binder_inner_proc_unlock(proc
);
3405 if (!binder_proc_transaction(t
, target_proc
, target_thread
)) {
3406 binder_inner_proc_lock(proc
);
3407 binder_pop_transaction_ilocked(thread
, t
);
3408 binder_inner_proc_unlock(proc
);
3409 goto err_dead_proc_or_thread
;
3412 BUG_ON(target_node
== NULL
);
3413 BUG_ON(t
->buffer
->async_transaction
!= 1);
3414 binder_enqueue_thread_work(thread
, tcomplete
);
3415 if (!binder_proc_transaction(t
, target_proc
, NULL
))
3416 goto err_dead_proc_or_thread
;
3419 binder_thread_dec_tmpref(target_thread
);
3420 binder_proc_dec_tmpref(target_proc
);
3422 binder_dec_node_tmpref(target_node
);
3424 * write barrier to synchronize with initialization
3428 WRITE_ONCE(e
->debug_id_done
, t_debug_id
);
3431 err_dead_proc_or_thread
:
3432 return_error
= BR_DEAD_REPLY
;
3433 return_error_line
= __LINE__
;
3434 binder_dequeue_work(proc
, tcomplete
);
3435 err_translate_failed
:
3436 err_bad_object_type
:
3439 err_copy_data_failed
:
3440 trace_binder_transaction_failed_buffer_release(t
->buffer
);
3441 binder_transaction_buffer_release(target_proc
, t
->buffer
, offp
);
3443 binder_dec_node_tmpref(target_node
);
3445 t
->buffer
->transaction
= NULL
;
3446 binder_alloc_free_buf(&target_proc
->alloc
, t
->buffer
);
3447 err_binder_alloc_buf_failed
:
3450 security_release_secctx(secctx
, secctx_sz
);
3451 err_get_secctx_failed
:
3453 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE
);
3454 err_alloc_tcomplete_failed
:
3456 binder_stats_deleted(BINDER_STAT_TRANSACTION
);
3459 err_empty_call_stack
:
3461 err_invalid_target_handle
:
3463 binder_thread_dec_tmpref(target_thread
);
3465 binder_proc_dec_tmpref(target_proc
);
3467 binder_dec_node(target_node
, 1, 0);
3468 binder_dec_node_tmpref(target_node
);
3471 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
3472 "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
3473 proc
->pid
, thread
->pid
, return_error
, return_error_param
,
3474 (u64
)tr
->data_size
, (u64
)tr
->offsets_size
,
3478 struct binder_transaction_log_entry
*fe
;
3480 e
->return_error
= return_error
;
3481 e
->return_error_param
= return_error_param
;
3482 e
->return_error_line
= return_error_line
;
3483 fe
= binder_transaction_log_add(&binder_transaction_log_failed
);
3486 * write barrier to synchronize with initialization
3490 WRITE_ONCE(e
->debug_id_done
, t_debug_id
);
3491 WRITE_ONCE(fe
->debug_id_done
, t_debug_id
);
3494 BUG_ON(thread
->return_error
.cmd
!= BR_OK
);
3496 binder_restore_priority(current
, in_reply_to
->saved_priority
);
3497 thread
->return_error
.cmd
= BR_TRANSACTION_COMPLETE
;
3498 binder_enqueue_thread_work(thread
, &thread
->return_error
.work
);
3499 binder_send_failed_reply(in_reply_to
, return_error
);
3501 thread
->return_error
.cmd
= return_error
;
3502 binder_enqueue_thread_work(thread
, &thread
->return_error
.work
);
3506 int binder_thread_write(struct binder_proc
*proc
,
3507 struct binder_thread
*thread
,
3508 binder_uintptr_t binder_buffer
, size_t size
,
3509 binder_size_t
*consumed
)
3512 struct binder_context
*context
= proc
->context
;
3513 void __user
*buffer
= (void __user
*)(uintptr_t)binder_buffer
;
3514 void __user
*ptr
= buffer
+ *consumed
;
3515 void __user
*end
= buffer
+ size
;
3517 while (ptr
< end
&& thread
->return_error
.cmd
== BR_OK
) {
3520 if (get_user(cmd
, (uint32_t __user
*)ptr
))
3522 ptr
+= sizeof(uint32_t);
3523 trace_binder_command(cmd
);
3524 if (_IOC_NR(cmd
) < ARRAY_SIZE(binder_stats
.bc
)) {
3525 atomic_inc(&binder_stats
.bc
[_IOC_NR(cmd
)]);
3526 atomic_inc(&proc
->stats
.bc
[_IOC_NR(cmd
)]);
3527 atomic_inc(&thread
->stats
.bc
[_IOC_NR(cmd
)]);
3535 const char *debug_string
;
3536 bool strong
= cmd
== BC_ACQUIRE
|| cmd
== BC_RELEASE
;
3537 bool increment
= cmd
== BC_INCREFS
|| cmd
== BC_ACQUIRE
;
3538 struct binder_ref_data rdata
;
3540 if (get_user(target
, (uint32_t __user
*)ptr
))
3543 ptr
+= sizeof(uint32_t);
3545 if (increment
&& !target
) {
3546 struct binder_node
*ctx_mgr_node
;
3547 mutex_lock(&context
->context_mgr_node_lock
);
3548 ctx_mgr_node
= context
->binder_context_mgr_node
;
3550 ret
= binder_inc_ref_for_node(
3552 strong
, NULL
, &rdata
);
3553 mutex_unlock(&context
->context_mgr_node_lock
);
3556 ret
= binder_update_ref_for_handle(
3557 proc
, target
, increment
, strong
,
3559 if (!ret
&& rdata
.desc
!= target
) {
3560 binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
3561 proc
->pid
, thread
->pid
,
3562 target
, rdata
.desc
);
3566 debug_string
= "IncRefs";
3569 debug_string
= "Acquire";
3572 debug_string
= "Release";
3576 debug_string
= "DecRefs";
3580 binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
3581 proc
->pid
, thread
->pid
, debug_string
,
3582 strong
, target
, ret
);
3585 binder_debug(BINDER_DEBUG_USER_REFS
,
3586 "%d:%d %s ref %d desc %d s %d w %d\n",
3587 proc
->pid
, thread
->pid
, debug_string
,
3588 rdata
.debug_id
, rdata
.desc
, rdata
.strong
,
3592 case BC_INCREFS_DONE
:
3593 case BC_ACQUIRE_DONE
: {
3594 binder_uintptr_t node_ptr
;
3595 binder_uintptr_t cookie
;
3596 struct binder_node
*node
;
3599 if (get_user(node_ptr
, (binder_uintptr_t __user
*)ptr
))
3601 ptr
+= sizeof(binder_uintptr_t
);
3602 if (get_user(cookie
, (binder_uintptr_t __user
*)ptr
))
3604 ptr
+= sizeof(binder_uintptr_t
);
3605 node
= binder_get_node(proc
, node_ptr
);
3607 binder_user_error("%d:%d %s u%016llx no match\n",
3608 proc
->pid
, thread
->pid
,
3609 cmd
== BC_INCREFS_DONE
?
3615 if (cookie
!= node
->cookie
) {
3616 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
3617 proc
->pid
, thread
->pid
,
3618 cmd
== BC_INCREFS_DONE
?
3619 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
3620 (u64
)node_ptr
, node
->debug_id
,
3621 (u64
)cookie
, (u64
)node
->cookie
);
3622 binder_put_node(node
);
3625 binder_node_inner_lock(node
);
3626 if (cmd
== BC_ACQUIRE_DONE
) {
3627 if (node
->pending_strong_ref
== 0) {
3628 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
3629 proc
->pid
, thread
->pid
,
3631 binder_node_inner_unlock(node
);
3632 binder_put_node(node
);
3635 node
->pending_strong_ref
= 0;
3637 if (node
->pending_weak_ref
== 0) {
3638 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
3639 proc
->pid
, thread
->pid
,
3641 binder_node_inner_unlock(node
);
3642 binder_put_node(node
);
3645 node
->pending_weak_ref
= 0;
3647 free_node
= binder_dec_node_nilocked(node
,
3648 cmd
== BC_ACQUIRE_DONE
, 0);
3650 binder_debug(BINDER_DEBUG_USER_REFS
,
3651 "%d:%d %s node %d ls %d lw %d tr %d\n",
3652 proc
->pid
, thread
->pid
,
3653 cmd
== BC_INCREFS_DONE
? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
3654 node
->debug_id
, node
->local_strong_refs
,
3655 node
->local_weak_refs
, node
->tmp_refs
);
3656 binder_node_inner_unlock(node
);
3657 binder_put_node(node
);
3660 case BC_ATTEMPT_ACQUIRE
:
3661 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
3663 case BC_ACQUIRE_RESULT
:
3664 pr_err("BC_ACQUIRE_RESULT not supported\n");
3667 case BC_FREE_BUFFER
: {
3668 binder_uintptr_t data_ptr
;
3669 struct binder_buffer
*buffer
;
3671 if (get_user(data_ptr
, (binder_uintptr_t __user
*)ptr
))
3673 ptr
+= sizeof(binder_uintptr_t
);
3675 buffer
= binder_alloc_prepare_to_free(&proc
->alloc
,
3677 if (IS_ERR_OR_NULL(buffer
)) {
3678 if (PTR_ERR(buffer
) == -EPERM
) {
3680 "%d:%d BC_FREE_BUFFER u%016llx matched unreturned or currently freeing buffer\n",
3681 proc
->pid
, thread
->pid
,
3685 "%d:%d BC_FREE_BUFFER u%016llx no match\n",
3686 proc
->pid
, thread
->pid
,
3691 binder_debug(BINDER_DEBUG_FREE_BUFFER
,
3692 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
3693 proc
->pid
, thread
->pid
, (u64
)data_ptr
,
3695 buffer
->transaction
? "active" : "finished");
3697 binder_inner_proc_lock(proc
);
3698 if (buffer
->transaction
) {
3699 buffer
->transaction
->buffer
= NULL
;
3700 buffer
->transaction
= NULL
;
3702 binder_inner_proc_unlock(proc
);
3703 if (buffer
->async_transaction
&& buffer
->target_node
) {
3704 struct binder_node
*buf_node
;
3705 struct binder_work
*w
;
3707 buf_node
= buffer
->target_node
;
3708 binder_node_inner_lock(buf_node
);
3709 BUG_ON(!buf_node
->has_async_transaction
);
3710 BUG_ON(buf_node
->proc
!= proc
);
3711 w
= binder_dequeue_work_head_ilocked(
3712 &buf_node
->async_todo
);
3714 buf_node
->has_async_transaction
= 0;
3716 binder_enqueue_work_ilocked(
3718 binder_wakeup_proc_ilocked(proc
);
3720 binder_node_inner_unlock(buf_node
);
3722 trace_binder_transaction_buffer_release(buffer
);
3723 binder_transaction_buffer_release(proc
, buffer
, NULL
);
3724 binder_alloc_free_buf(&proc
->alloc
, buffer
);
3728 case BC_TRANSACTION_SG
:
3730 struct binder_transaction_data_sg tr
;
3732 if (copy_from_user(&tr
, ptr
, sizeof(tr
)))
3735 binder_transaction(proc
, thread
, &tr
.transaction_data
,
3736 cmd
== BC_REPLY_SG
, tr
.buffers_size
);
3739 case BC_TRANSACTION
:
3741 struct binder_transaction_data tr
;
3743 if (copy_from_user(&tr
, ptr
, sizeof(tr
)))
3746 binder_transaction(proc
, thread
, &tr
,
3747 cmd
== BC_REPLY
, 0);
3751 case BC_REGISTER_LOOPER
:
3752 binder_debug(BINDER_DEBUG_THREADS
,
3753 "%d:%d BC_REGISTER_LOOPER\n",
3754 proc
->pid
, thread
->pid
);
3755 binder_inner_proc_lock(proc
);
3756 if (thread
->looper
& BINDER_LOOPER_STATE_ENTERED
) {
3757 thread
->looper
|= BINDER_LOOPER_STATE_INVALID
;
3758 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
3759 proc
->pid
, thread
->pid
);
3760 } else if (proc
->requested_threads
== 0) {
3761 thread
->looper
|= BINDER_LOOPER_STATE_INVALID
;
3762 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
3763 proc
->pid
, thread
->pid
);
3765 proc
->requested_threads
--;
3766 proc
->requested_threads_started
++;
3768 thread
->looper
|= BINDER_LOOPER_STATE_REGISTERED
;
3769 binder_inner_proc_unlock(proc
);
3771 case BC_ENTER_LOOPER
:
3772 binder_debug(BINDER_DEBUG_THREADS
,
3773 "%d:%d BC_ENTER_LOOPER\n",
3774 proc
->pid
, thread
->pid
);
3775 if (thread
->looper
& BINDER_LOOPER_STATE_REGISTERED
) {
3776 thread
->looper
|= BINDER_LOOPER_STATE_INVALID
;
3777 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
3778 proc
->pid
, thread
->pid
);
3780 thread
->looper
|= BINDER_LOOPER_STATE_ENTERED
;
3782 case BC_EXIT_LOOPER
:
3783 binder_debug(BINDER_DEBUG_THREADS
,
3784 "%d:%d BC_EXIT_LOOPER\n",
3785 proc
->pid
, thread
->pid
);
3786 thread
->looper
|= BINDER_LOOPER_STATE_EXITED
;
3789 case BC_REQUEST_DEATH_NOTIFICATION
:
3790 case BC_CLEAR_DEATH_NOTIFICATION
: {
3792 binder_uintptr_t cookie
;
3793 struct binder_ref
*ref
;
3794 struct binder_ref_death
*death
= NULL
;
3796 if (get_user(target
, (uint32_t __user
*)ptr
))
3798 ptr
+= sizeof(uint32_t);
3799 if (get_user(cookie
, (binder_uintptr_t __user
*)ptr
))
3801 ptr
+= sizeof(binder_uintptr_t
);
3802 if (cmd
== BC_REQUEST_DEATH_NOTIFICATION
) {
3804 * Allocate memory for death notification
3805 * before taking lock
3807 death
= kzalloc(sizeof(*death
), GFP_KERNEL
);
3808 if (death
== NULL
) {
3809 WARN_ON(thread
->return_error
.cmd
!=
3811 thread
->return_error
.cmd
= BR_ERROR
;
3812 binder_enqueue_thread_work(
3814 &thread
->return_error
.work
);
3816 BINDER_DEBUG_FAILED_TRANSACTION
,
3817 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
3818 proc
->pid
, thread
->pid
);
3822 binder_proc_lock(proc
);
3823 ref
= binder_get_ref_olocked(proc
, target
, false);
3825 binder_user_error("%d:%d %s invalid ref %d\n",
3826 proc
->pid
, thread
->pid
,
3827 cmd
== BC_REQUEST_DEATH_NOTIFICATION
?
3828 "BC_REQUEST_DEATH_NOTIFICATION" :
3829 "BC_CLEAR_DEATH_NOTIFICATION",
3831 binder_proc_unlock(proc
);
3836 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION
,
3837 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
3838 proc
->pid
, thread
->pid
,
3839 cmd
== BC_REQUEST_DEATH_NOTIFICATION
?
3840 "BC_REQUEST_DEATH_NOTIFICATION" :
3841 "BC_CLEAR_DEATH_NOTIFICATION",
3842 (u64
)cookie
, ref
->data
.debug_id
,
3843 ref
->data
.desc
, ref
->data
.strong
,
3844 ref
->data
.weak
, ref
->node
->debug_id
);
3846 binder_node_lock(ref
->node
);
3847 if (cmd
== BC_REQUEST_DEATH_NOTIFICATION
) {
3849 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
3850 proc
->pid
, thread
->pid
);
3851 binder_node_unlock(ref
->node
);
3852 binder_proc_unlock(proc
);
3856 binder_stats_created(BINDER_STAT_DEATH
);
3857 INIT_LIST_HEAD(&death
->work
.entry
);
3858 death
->cookie
= cookie
;
3860 if (ref
->node
->proc
== NULL
) {
3861 ref
->death
->work
.type
= BINDER_WORK_DEAD_BINDER
;
3863 binder_inner_proc_lock(proc
);
3864 binder_enqueue_work_ilocked(
3865 &ref
->death
->work
, &proc
->todo
);
3866 binder_wakeup_proc_ilocked(proc
);
3867 binder_inner_proc_unlock(proc
);
3870 if (ref
->death
== NULL
) {
3871 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
3872 proc
->pid
, thread
->pid
);
3873 binder_node_unlock(ref
->node
);
3874 binder_proc_unlock(proc
);
3878 if (death
->cookie
!= cookie
) {
3879 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
3880 proc
->pid
, thread
->pid
,
3883 binder_node_unlock(ref
->node
);
3884 binder_proc_unlock(proc
);
3888 binder_inner_proc_lock(proc
);
3889 if (list_empty(&death
->work
.entry
)) {
3890 death
->work
.type
= BINDER_WORK_CLEAR_DEATH_NOTIFICATION
;
3891 if (thread
->looper
&
3892 (BINDER_LOOPER_STATE_REGISTERED
|
3893 BINDER_LOOPER_STATE_ENTERED
))
3894 binder_enqueue_thread_work_ilocked(
3898 binder_enqueue_work_ilocked(
3901 binder_wakeup_proc_ilocked(
3905 BUG_ON(death
->work
.type
!= BINDER_WORK_DEAD_BINDER
);
3906 death
->work
.type
= BINDER_WORK_DEAD_BINDER_AND_CLEAR
;
3908 binder_inner_proc_unlock(proc
);
3910 binder_node_unlock(ref
->node
);
3911 binder_proc_unlock(proc
);
3913 case BC_DEAD_BINDER_DONE
: {
3914 struct binder_work
*w
;
3915 binder_uintptr_t cookie
;
3916 struct binder_ref_death
*death
= NULL
;
3918 if (get_user(cookie
, (binder_uintptr_t __user
*)ptr
))
3921 ptr
+= sizeof(cookie
);
3922 binder_inner_proc_lock(proc
);
3923 list_for_each_entry(w
, &proc
->delivered_death
,
3925 struct binder_ref_death
*tmp_death
=
3927 struct binder_ref_death
,
3930 if (tmp_death
->cookie
== cookie
) {
3935 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
3936 "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n",
3937 proc
->pid
, thread
->pid
, (u64
)cookie
,
3939 if (death
== NULL
) {
3940 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
3941 proc
->pid
, thread
->pid
, (u64
)cookie
);
3942 binder_inner_proc_unlock(proc
);
3945 binder_dequeue_work_ilocked(&death
->work
);
3946 if (death
->work
.type
== BINDER_WORK_DEAD_BINDER_AND_CLEAR
) {
3947 death
->work
.type
= BINDER_WORK_CLEAR_DEATH_NOTIFICATION
;
3948 if (thread
->looper
&
3949 (BINDER_LOOPER_STATE_REGISTERED
|
3950 BINDER_LOOPER_STATE_ENTERED
))
3951 binder_enqueue_thread_work_ilocked(
3952 thread
, &death
->work
);
3954 binder_enqueue_work_ilocked(
3957 binder_wakeup_proc_ilocked(proc
);
3960 binder_inner_proc_unlock(proc
);
3964 pr_err("%d:%d unknown command %d\n",
3965 proc
->pid
, thread
->pid
, cmd
);
3968 *consumed
= ptr
- buffer
;
3973 static void binder_stat_br(struct binder_proc
*proc
,
3974 struct binder_thread
*thread
, uint32_t cmd
)
3976 trace_binder_return(cmd
);
3977 if (_IOC_NR(cmd
) < ARRAY_SIZE(binder_stats
.br
)) {
3978 atomic_inc(&binder_stats
.br
[_IOC_NR(cmd
)]);
3979 atomic_inc(&proc
->stats
.br
[_IOC_NR(cmd
)]);
3980 atomic_inc(&thread
->stats
.br
[_IOC_NR(cmd
)]);
3984 static int binder_put_node_cmd(struct binder_proc
*proc
,
3985 struct binder_thread
*thread
,
3987 binder_uintptr_t node_ptr
,
3988 binder_uintptr_t node_cookie
,
3990 uint32_t cmd
, const char *cmd_name
)
3992 void __user
*ptr
= *ptrp
;
3994 if (put_user(cmd
, (uint32_t __user
*)ptr
))
3996 ptr
+= sizeof(uint32_t);
3998 if (put_user(node_ptr
, (binder_uintptr_t __user
*)ptr
))
4000 ptr
+= sizeof(binder_uintptr_t
);
4002 if (put_user(node_cookie
, (binder_uintptr_t __user
*)ptr
))
4004 ptr
+= sizeof(binder_uintptr_t
);
4006 binder_stat_br(proc
, thread
, cmd
);
4007 binder_debug(BINDER_DEBUG_USER_REFS
, "%d:%d %s %d u%016llx c%016llx\n",
4008 proc
->pid
, thread
->pid
, cmd_name
, node_debug_id
,
4009 (u64
)node_ptr
, (u64
)node_cookie
);
4015 static int binder_wait_for_work(struct binder_thread
*thread
,
4019 struct binder_proc
*proc
= thread
->proc
;
4022 freezer_do_not_count();
4023 binder_inner_proc_lock(proc
);
4025 prepare_to_wait(&thread
->wait
, &wait
, TASK_INTERRUPTIBLE
);
4026 if (binder_has_work_ilocked(thread
, do_proc_work
))
4029 list_add(&thread
->waiting_thread_node
,
4030 &proc
->waiting_threads
);
4031 binder_inner_proc_unlock(proc
);
4033 binder_inner_proc_lock(proc
);
4034 list_del_init(&thread
->waiting_thread_node
);
4035 if (signal_pending(current
)) {
4040 finish_wait(&thread
->wait
, &wait
);
4041 binder_inner_proc_unlock(proc
);
4047 static int binder_thread_read(struct binder_proc
*proc
,
4048 struct binder_thread
*thread
,
4049 binder_uintptr_t binder_buffer
, size_t size
,
4050 binder_size_t
*consumed
, int non_block
)
4052 void __user
*buffer
= (void __user
*)(uintptr_t)binder_buffer
;
4053 void __user
*ptr
= buffer
+ *consumed
;
4054 void __user
*end
= buffer
+ size
;
4057 int wait_for_proc_work
;
4059 if (*consumed
== 0) {
4060 if (put_user(BR_NOOP
, (uint32_t __user
*)ptr
))
4062 ptr
+= sizeof(uint32_t);
4066 binder_inner_proc_lock(proc
);
4067 wait_for_proc_work
= binder_available_for_proc_work_ilocked(thread
);
4068 binder_inner_proc_unlock(proc
);
4070 thread
->looper
|= BINDER_LOOPER_STATE_WAITING
;
4072 trace_binder_wait_for_work(wait_for_proc_work
,
4073 !!thread
->transaction_stack
,
4074 !binder_worklist_empty(proc
, &thread
->todo
));
4075 if (wait_for_proc_work
) {
4076 if (!(thread
->looper
& (BINDER_LOOPER_STATE_REGISTERED
|
4077 BINDER_LOOPER_STATE_ENTERED
))) {
4078 binder_user_error("%d:%d ERROR: Thread waiting for process work before calling BC_REGISTER_LOOPER or BC_ENTER_LOOPER (state %x)\n",
4079 proc
->pid
, thread
->pid
, thread
->looper
);
4080 wait_event_interruptible(binder_user_error_wait
,
4081 binder_stop_on_user_error
< 2);
4083 binder_restore_priority(current
, proc
->default_priority
);
4087 if (!binder_has_work(thread
, wait_for_proc_work
))
4090 ret
= binder_wait_for_work(thread
, wait_for_proc_work
);
4093 thread
->looper
&= ~BINDER_LOOPER_STATE_WAITING
;
4100 struct binder_transaction_data_secctx tr
;
4101 struct binder_transaction_data
*trd
= &tr
.transaction_data
;
4102 struct binder_work
*w
= NULL
;
4103 struct list_head
*list
= NULL
;
4104 struct binder_transaction
*t
= NULL
;
4105 struct binder_thread
*t_from
;
4106 size_t trsize
= sizeof(*trd
);
4108 binder_inner_proc_lock(proc
);
4109 if (!binder_worklist_empty_ilocked(&thread
->todo
))
4110 list
= &thread
->todo
;
4111 else if (!binder_worklist_empty_ilocked(&proc
->todo
) &&
4115 binder_inner_proc_unlock(proc
);
4118 if (ptr
- buffer
== 4 && !thread
->looper_need_return
)
4123 if (end
- ptr
< sizeof(tr
) + 4) {
4124 binder_inner_proc_unlock(proc
);
4127 w
= binder_dequeue_work_head_ilocked(list
);
4128 if (binder_worklist_empty_ilocked(&thread
->todo
))
4129 thread
->process_todo
= false;
4132 case BINDER_WORK_TRANSACTION
: {
4133 binder_inner_proc_unlock(proc
);
4134 t
= container_of(w
, struct binder_transaction
, work
);
4136 case BINDER_WORK_RETURN_ERROR
: {
4137 struct binder_error
*e
= container_of(
4138 w
, struct binder_error
, work
);
4140 WARN_ON(e
->cmd
== BR_OK
);
4141 binder_inner_proc_unlock(proc
);
4142 if (put_user(e
->cmd
, (uint32_t __user
*)ptr
))
4145 ptr
+= sizeof(uint32_t);
4147 binder_stat_br(proc
, thread
, e
->cmd
);
4149 case BINDER_WORK_TRANSACTION_COMPLETE
: {
4150 binder_inner_proc_unlock(proc
);
4151 cmd
= BR_TRANSACTION_COMPLETE
;
4152 if (put_user(cmd
, (uint32_t __user
*)ptr
))
4154 ptr
+= sizeof(uint32_t);
4156 binder_stat_br(proc
, thread
, cmd
);
4157 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE
,
4158 "%d:%d BR_TRANSACTION_COMPLETE\n",
4159 proc
->pid
, thread
->pid
);
4161 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE
);
4163 case BINDER_WORK_NODE
: {
4164 struct binder_node
*node
= container_of(w
, struct binder_node
, work
);
4166 binder_uintptr_t node_ptr
= node
->ptr
;
4167 binder_uintptr_t node_cookie
= node
->cookie
;
4168 int node_debug_id
= node
->debug_id
;
4171 void __user
*orig_ptr
= ptr
;
4173 BUG_ON(proc
!= node
->proc
);
4174 strong
= node
->internal_strong_refs
||
4175 node
->local_strong_refs
;
4176 weak
= !hlist_empty(&node
->refs
) ||
4177 node
->local_weak_refs
||
4178 node
->tmp_refs
|| strong
;
4179 has_strong_ref
= node
->has_strong_ref
;
4180 has_weak_ref
= node
->has_weak_ref
;
4182 if (weak
&& !has_weak_ref
) {
4183 node
->has_weak_ref
= 1;
4184 node
->pending_weak_ref
= 1;
4185 node
->local_weak_refs
++;
4187 if (strong
&& !has_strong_ref
) {
4188 node
->has_strong_ref
= 1;
4189 node
->pending_strong_ref
= 1;
4190 node
->local_strong_refs
++;
4192 if (!strong
&& has_strong_ref
)
4193 node
->has_strong_ref
= 0;
4194 if (!weak
&& has_weak_ref
)
4195 node
->has_weak_ref
= 0;
4196 if (!weak
&& !strong
) {
4197 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
4198 "%d:%d node %d u%016llx c%016llx deleted\n",
4199 proc
->pid
, thread
->pid
,
4203 rb_erase(&node
->rb_node
, &proc
->nodes
);
4204 binder_inner_proc_unlock(proc
);
4205 binder_node_lock(node
);
4207 * Acquire the node lock before freeing the
4208 * node to serialize with other threads that
4209 * may have been holding the node lock while
4210 * decrementing this node (avoids race where
4211 * this thread frees while the other thread
4212 * is unlocking the node after the final
4215 binder_node_unlock(node
);
4216 binder_free_node(node
);
4218 binder_inner_proc_unlock(proc
);
4220 if (weak
&& !has_weak_ref
)
4221 ret
= binder_put_node_cmd(
4222 proc
, thread
, &ptr
, node_ptr
,
4223 node_cookie
, node_debug_id
,
4224 BR_INCREFS
, "BR_INCREFS");
4225 if (!ret
&& strong
&& !has_strong_ref
)
4226 ret
= binder_put_node_cmd(
4227 proc
, thread
, &ptr
, node_ptr
,
4228 node_cookie
, node_debug_id
,
4229 BR_ACQUIRE
, "BR_ACQUIRE");
4230 if (!ret
&& !strong
&& has_strong_ref
)
4231 ret
= binder_put_node_cmd(
4232 proc
, thread
, &ptr
, node_ptr
,
4233 node_cookie
, node_debug_id
,
4234 BR_RELEASE
, "BR_RELEASE");
4235 if (!ret
&& !weak
&& has_weak_ref
)
4236 ret
= binder_put_node_cmd(
4237 proc
, thread
, &ptr
, node_ptr
,
4238 node_cookie
, node_debug_id
,
4239 BR_DECREFS
, "BR_DECREFS");
4240 if (orig_ptr
== ptr
)
4241 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
4242 "%d:%d node %d u%016llx c%016llx state unchanged\n",
4243 proc
->pid
, thread
->pid
,
4250 case BINDER_WORK_DEAD_BINDER
:
4251 case BINDER_WORK_DEAD_BINDER_AND_CLEAR
:
4252 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION
: {
4253 struct binder_ref_death
*death
;
4255 binder_uintptr_t cookie
;
4257 death
= container_of(w
, struct binder_ref_death
, work
);
4258 if (w
->type
== BINDER_WORK_CLEAR_DEATH_NOTIFICATION
)
4259 cmd
= BR_CLEAR_DEATH_NOTIFICATION_DONE
;
4261 cmd
= BR_DEAD_BINDER
;
4262 cookie
= death
->cookie
;
4264 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION
,
4265 "%d:%d %s %016llx\n",
4266 proc
->pid
, thread
->pid
,
4267 cmd
== BR_DEAD_BINDER
?
4269 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
4271 if (w
->type
== BINDER_WORK_CLEAR_DEATH_NOTIFICATION
) {
4272 binder_inner_proc_unlock(proc
);
4274 binder_stats_deleted(BINDER_STAT_DEATH
);
4276 binder_enqueue_work_ilocked(
4277 w
, &proc
->delivered_death
);
4278 binder_inner_proc_unlock(proc
);
4280 if (put_user(cmd
, (uint32_t __user
*)ptr
))
4282 ptr
+= sizeof(uint32_t);
4283 if (put_user(cookie
,
4284 (binder_uintptr_t __user
*)ptr
))
4286 ptr
+= sizeof(binder_uintptr_t
);
4287 binder_stat_br(proc
, thread
, cmd
);
4288 if (cmd
== BR_DEAD_BINDER
)
4289 goto done
; /* DEAD_BINDER notifications can cause transactions */
4296 BUG_ON(t
->buffer
== NULL
);
4297 if (t
->buffer
->target_node
) {
4298 struct binder_node
*target_node
= t
->buffer
->target_node
;
4299 struct binder_priority node_prio
;
4301 trd
->target
.ptr
= target_node
->ptr
;
4302 trd
->cookie
= target_node
->cookie
;
4303 node_prio
.sched_policy
= target_node
->sched_policy
;
4304 node_prio
.prio
= target_node
->min_priority
;
4305 binder_transaction_priority(current
, t
, node_prio
,
4306 target_node
->inherit_rt
);
4307 cmd
= BR_TRANSACTION
;
4309 trd
->target
.ptr
= 0;
4313 trd
->code
= t
->code
;
4314 trd
->flags
= t
->flags
;
4315 trd
->sender_euid
= from_kuid(current_user_ns(), t
->sender_euid
);
4317 t_from
= binder_get_txn_from(t
);
4319 struct task_struct
*sender
= t_from
->proc
->tsk
;
4322 task_tgid_nr_ns(sender
,
4323 task_active_pid_ns(current
));
4325 trd
->sender_pid
= 0;
4328 trd
->data_size
= t
->buffer
->data_size
;
4329 trd
->offsets_size
= t
->buffer
->offsets_size
;
4330 trd
->data
.ptr
.buffer
= (binder_uintptr_t
)
4331 ((uintptr_t)t
->buffer
->data
+
4332 binder_alloc_get_user_buffer_offset(&proc
->alloc
));
4333 trd
->data
.ptr
.offsets
= trd
->data
.ptr
.buffer
+
4334 ALIGN(t
->buffer
->data_size
,
4337 tr
.secctx
= t
->security_ctx
;
4338 if (t
->security_ctx
) {
4339 cmd
= BR_TRANSACTION_SEC_CTX
;
4340 trsize
= sizeof(tr
);
4342 if (put_user(cmd
, (uint32_t __user
*)ptr
)) {
4344 binder_thread_dec_tmpref(t_from
);
4346 binder_cleanup_transaction(t
, "put_user failed",
4351 ptr
+= sizeof(uint32_t);
4352 if (copy_to_user(ptr
, &tr
, trsize
)) {
4354 binder_thread_dec_tmpref(t_from
);
4356 binder_cleanup_transaction(t
, "copy_to_user failed",
4363 trace_binder_transaction_received(t
);
4364 binder_stat_br(proc
, thread
, cmd
);
4365 binder_debug(BINDER_DEBUG_TRANSACTION
,
4366 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
4367 proc
->pid
, thread
->pid
,
4368 (cmd
== BR_TRANSACTION
) ? "BR_TRANSACTION" :
4369 (cmd
== BR_TRANSACTION_SEC_CTX
) ?
4370 "BR_TRANSACTION_SEC_CTX" : "BR_REPLY",
4371 t
->debug_id
, t_from
? t_from
->proc
->pid
: 0,
4372 t_from
? t_from
->pid
: 0, cmd
,
4373 t
->buffer
->data_size
, t
->buffer
->offsets_size
,
4374 (u64
)trd
->data
.ptr
.buffer
,
4375 (u64
)trd
->data
.ptr
.offsets
);
4378 binder_thread_dec_tmpref(t_from
);
4379 t
->buffer
->allow_user_free
= 1;
4380 if (cmd
!= BR_REPLY
&& !(t
->flags
& TF_ONE_WAY
)) {
4381 binder_inner_proc_lock(thread
->proc
);
4382 t
->to_parent
= thread
->transaction_stack
;
4383 t
->to_thread
= thread
;
4384 thread
->transaction_stack
= t
;
4385 binder_inner_proc_unlock(thread
->proc
);
4387 binder_free_transaction(t
);
4394 *consumed
= ptr
- buffer
;
4395 binder_inner_proc_lock(proc
);
4396 if (proc
->requested_threads
== 0 &&
4397 list_empty(&thread
->proc
->waiting_threads
) &&
4398 proc
->requested_threads_started
< proc
->max_threads
&&
4399 (thread
->looper
& (BINDER_LOOPER_STATE_REGISTERED
|
4400 BINDER_LOOPER_STATE_ENTERED
)) /* the user-space code fails to */
4401 /*spawn a new thread if we leave this out */) {
4402 proc
->requested_threads
++;
4403 binder_inner_proc_unlock(proc
);
4404 binder_debug(BINDER_DEBUG_THREADS
,
4405 "%d:%d BR_SPAWN_LOOPER\n",
4406 proc
->pid
, thread
->pid
);
4407 if (put_user(BR_SPAWN_LOOPER
, (uint32_t __user
*)buffer
))
4409 binder_stat_br(proc
, thread
, BR_SPAWN_LOOPER
);
4411 binder_inner_proc_unlock(proc
);
4415 static void binder_release_work(struct binder_proc
*proc
,
4416 struct list_head
*list
)
4418 struct binder_work
*w
;
4419 enum binder_work_type wtype
;
4422 binder_inner_proc_lock(proc
);
4423 w
= binder_dequeue_work_head_ilocked(list
);
4424 wtype
= w
? w
->type
: 0;
4425 binder_inner_proc_unlock(proc
);
4430 case BINDER_WORK_TRANSACTION
: {
4431 struct binder_transaction
*t
;
4433 t
= container_of(w
, struct binder_transaction
, work
);
4435 binder_cleanup_transaction(t
, "process died.",
4438 case BINDER_WORK_RETURN_ERROR
: {
4439 struct binder_error
*e
= container_of(
4440 w
, struct binder_error
, work
);
4442 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
4443 "undelivered TRANSACTION_ERROR: %u\n",
4446 case BINDER_WORK_TRANSACTION_COMPLETE
: {
4447 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
4448 "undelivered TRANSACTION_COMPLETE\n");
4450 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE
);
4452 case BINDER_WORK_DEAD_BINDER_AND_CLEAR
:
4453 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION
: {
4454 struct binder_ref_death
*death
;
4456 death
= container_of(w
, struct binder_ref_death
, work
);
4457 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
4458 "undelivered death notification, %016llx\n",
4459 (u64
)death
->cookie
);
4461 binder_stats_deleted(BINDER_STAT_DEATH
);
4463 case BINDER_WORK_NODE
:
4466 pr_err("unexpected work type, %d, not freed\n",
4474 static struct binder_thread
*binder_get_thread_ilocked(
4475 struct binder_proc
*proc
, struct binder_thread
*new_thread
)
4477 struct binder_thread
*thread
= NULL
;
4478 struct rb_node
*parent
= NULL
;
4479 struct rb_node
**p
= &proc
->threads
.rb_node
;
4483 thread
= rb_entry(parent
, struct binder_thread
, rb_node
);
4485 if (current
->pid
< thread
->pid
)
4487 else if (current
->pid
> thread
->pid
)
4488 p
= &(*p
)->rb_right
;
4494 thread
= new_thread
;
4495 binder_stats_created(BINDER_STAT_THREAD
);
4496 thread
->proc
= proc
;
4497 thread
->pid
= current
->pid
;
4498 get_task_struct(current
);
4499 thread
->task
= current
;
4500 atomic_set(&thread
->tmp_ref
, 0);
4501 init_waitqueue_head(&thread
->wait
);
4502 INIT_LIST_HEAD(&thread
->todo
);
4503 rb_link_node(&thread
->rb_node
, parent
, p
);
4504 rb_insert_color(&thread
->rb_node
, &proc
->threads
);
4505 thread
->looper_need_return
= true;
4506 thread
->return_error
.work
.type
= BINDER_WORK_RETURN_ERROR
;
4507 thread
->return_error
.cmd
= BR_OK
;
4508 thread
->reply_error
.work
.type
= BINDER_WORK_RETURN_ERROR
;
4509 thread
->reply_error
.cmd
= BR_OK
;
4510 INIT_LIST_HEAD(&new_thread
->waiting_thread_node
);
4514 static struct binder_thread
*binder_get_thread(struct binder_proc
*proc
)
4516 struct binder_thread
*thread
;
4517 struct binder_thread
*new_thread
;
4519 binder_inner_proc_lock(proc
);
4520 thread
= binder_get_thread_ilocked(proc
, NULL
);
4521 binder_inner_proc_unlock(proc
);
4523 new_thread
= kzalloc(sizeof(*thread
), GFP_KERNEL
);
4524 if (new_thread
== NULL
)
4526 binder_inner_proc_lock(proc
);
4527 thread
= binder_get_thread_ilocked(proc
, new_thread
);
4528 binder_inner_proc_unlock(proc
);
4529 if (thread
!= new_thread
)
4535 static void binder_free_proc(struct binder_proc
*proc
)
4537 BUG_ON(!list_empty(&proc
->todo
));
4538 BUG_ON(!list_empty(&proc
->delivered_death
));
4539 binder_alloc_deferred_release(&proc
->alloc
);
4540 put_task_struct(proc
->tsk
);
4541 binder_stats_deleted(BINDER_STAT_PROC
);
4545 static void binder_free_thread(struct binder_thread
*thread
)
4547 BUG_ON(!list_empty(&thread
->todo
));
4548 binder_stats_deleted(BINDER_STAT_THREAD
);
4549 binder_proc_dec_tmpref(thread
->proc
);
4550 put_task_struct(thread
->task
);
4554 static int binder_thread_release(struct binder_proc
*proc
,
4555 struct binder_thread
*thread
)
4557 struct binder_transaction
*t
;
4558 struct binder_transaction
*send_reply
= NULL
;
4559 int active_transactions
= 0;
4560 struct binder_transaction
*last_t
= NULL
;
4562 binder_inner_proc_lock(thread
->proc
);
4564 * take a ref on the proc so it survives
4565 * after we remove this thread from proc->threads.
4566 * The corresponding dec is when we actually
4567 * free the thread in binder_free_thread()
4571 * take a ref on this thread to ensure it
4572 * survives while we are releasing it
4574 atomic_inc(&thread
->tmp_ref
);
4575 rb_erase(&thread
->rb_node
, &proc
->threads
);
4576 t
= thread
->transaction_stack
;
4578 spin_lock(&t
->lock
);
4579 if (t
->to_thread
== thread
)
4582 thread
->is_dead
= true;
4586 active_transactions
++;
4587 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
4588 "release %d:%d transaction %d %s, still active\n",
4589 proc
->pid
, thread
->pid
,
4591 (t
->to_thread
== thread
) ? "in" : "out");
4593 if (t
->to_thread
== thread
) {
4595 t
->to_thread
= NULL
;
4597 t
->buffer
->transaction
= NULL
;
4601 } else if (t
->from
== thread
) {
4606 spin_unlock(&last_t
->lock
);
4608 spin_lock(&t
->lock
);
4612 * If this thread used poll, make sure we remove the waitqueue
4613 * from any epoll data structures holding it with POLLFREE.
4614 * waitqueue_active() is safe to use here because we're holding
4617 if ((thread
->looper
& BINDER_LOOPER_STATE_POLL
) &&
4618 waitqueue_active(&thread
->wait
)) {
4619 wake_up_poll(&thread
->wait
, POLLHUP
| POLLFREE
);
4622 binder_inner_proc_unlock(thread
->proc
);
4625 * This is needed to avoid races between wake_up_poll() above and
4626 * and ep_remove_waitqueue() called for other reasons (eg the epoll file
4627 * descriptor being closed); ep_remove_waitqueue() holds an RCU read
4628 * lock, so we can be sure it's done after calling synchronize_rcu().
4630 if (thread
->looper
& BINDER_LOOPER_STATE_POLL
)
4634 binder_send_failed_reply(send_reply
, BR_DEAD_REPLY
);
4635 binder_release_work(proc
, &thread
->todo
);
4636 binder_thread_dec_tmpref(thread
);
4637 return active_transactions
;
4640 static unsigned int binder_poll(struct file
*filp
,
4641 struct poll_table_struct
*wait
)
4643 struct binder_proc
*proc
= filp
->private_data
;
4644 struct binder_thread
*thread
= NULL
;
4645 bool wait_for_proc_work
;
4647 thread
= binder_get_thread(proc
);
4649 binder_inner_proc_lock(thread
->proc
);
4650 thread
->looper
|= BINDER_LOOPER_STATE_POLL
;
4651 wait_for_proc_work
= binder_available_for_proc_work_ilocked(thread
);
4653 binder_inner_proc_unlock(thread
->proc
);
4655 poll_wait(filp
, &thread
->wait
, wait
);
4657 if (binder_has_work(thread
, wait_for_proc_work
))
4663 static int binder_ioctl_write_read(struct file
*filp
,
4664 unsigned int cmd
, unsigned long arg
,
4665 struct binder_thread
*thread
)
4668 struct binder_proc
*proc
= filp
->private_data
;
4669 unsigned int size
= _IOC_SIZE(cmd
);
4670 void __user
*ubuf
= (void __user
*)arg
;
4671 struct binder_write_read bwr
;
4673 if (size
!= sizeof(struct binder_write_read
)) {
4677 if (copy_from_user(&bwr
, ubuf
, sizeof(bwr
))) {
4681 binder_debug(BINDER_DEBUG_READ_WRITE
,
4682 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
4683 proc
->pid
, thread
->pid
,
4684 (u64
)bwr
.write_size
, (u64
)bwr
.write_buffer
,
4685 (u64
)bwr
.read_size
, (u64
)bwr
.read_buffer
);
4687 if (bwr
.write_size
> 0) {
4688 ret
= binder_thread_write(proc
, thread
,
4691 &bwr
.write_consumed
);
4692 trace_binder_write_done(ret
);
4694 bwr
.read_consumed
= 0;
4695 if (copy_to_user(ubuf
, &bwr
, sizeof(bwr
)))
4700 if (bwr
.read_size
> 0) {
4701 ret
= binder_thread_read(proc
, thread
, bwr
.read_buffer
,
4704 filp
->f_flags
& O_NONBLOCK
);
4705 trace_binder_read_done(ret
);
4706 binder_inner_proc_lock(proc
);
4707 if (!binder_worklist_empty_ilocked(&proc
->todo
))
4708 binder_wakeup_proc_ilocked(proc
);
4709 binder_inner_proc_unlock(proc
);
4711 if (copy_to_user(ubuf
, &bwr
, sizeof(bwr
)))
4716 binder_debug(BINDER_DEBUG_READ_WRITE
,
4717 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
4718 proc
->pid
, thread
->pid
,
4719 (u64
)bwr
.write_consumed
, (u64
)bwr
.write_size
,
4720 (u64
)bwr
.read_consumed
, (u64
)bwr
.read_size
);
4721 if (copy_to_user(ubuf
, &bwr
, sizeof(bwr
))) {
4729 static int binder_ioctl_set_ctx_mgr(struct file
*filp
,
4730 struct flat_binder_object
*fbo
)
4733 struct binder_proc
*proc
= filp
->private_data
;
4734 struct binder_context
*context
= proc
->context
;
4735 struct binder_node
*new_node
;
4736 kuid_t curr_euid
= current_euid();
4738 mutex_lock(&context
->context_mgr_node_lock
);
4739 if (context
->binder_context_mgr_node
) {
4740 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
4744 ret
= security_binder_set_context_mgr(proc
->tsk
);
4747 if (uid_valid(context
->binder_context_mgr_uid
)) {
4748 if (!uid_eq(context
->binder_context_mgr_uid
, curr_euid
)) {
4749 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
4750 from_kuid(&init_user_ns
, curr_euid
),
4751 from_kuid(&init_user_ns
,
4752 context
->binder_context_mgr_uid
));
4757 context
->binder_context_mgr_uid
= curr_euid
;
4759 new_node
= binder_new_node(proc
, fbo
);
4764 binder_node_lock(new_node
);
4765 new_node
->local_weak_refs
++;
4766 new_node
->local_strong_refs
++;
4767 new_node
->has_strong_ref
= 1;
4768 new_node
->has_weak_ref
= 1;
4769 context
->binder_context_mgr_node
= new_node
;
4770 binder_node_unlock(new_node
);
4771 binder_put_node(new_node
);
4773 mutex_unlock(&context
->context_mgr_node_lock
);
4777 static int binder_ioctl_get_node_info_for_ref(struct binder_proc
*proc
,
4778 struct binder_node_info_for_ref
*info
)
4780 struct binder_node
*node
;
4781 struct binder_context
*context
= proc
->context
;
4782 __u32 handle
= info
->handle
;
4784 if (info
->strong_count
|| info
->weak_count
|| info
->reserved1
||
4785 info
->reserved2
|| info
->reserved3
) {
4786 binder_user_error("%d BINDER_GET_NODE_INFO_FOR_REF: only handle may be non-zero.",
4791 /* This ioctl may only be used by the context manager */
4792 mutex_lock(&context
->context_mgr_node_lock
);
4793 if (!context
->binder_context_mgr_node
||
4794 context
->binder_context_mgr_node
->proc
!= proc
) {
4795 mutex_unlock(&context
->context_mgr_node_lock
);
4798 mutex_unlock(&context
->context_mgr_node_lock
);
4800 node
= binder_get_node_from_ref(proc
, handle
, true, NULL
);
4804 info
->strong_count
= node
->local_strong_refs
+
4805 node
->internal_strong_refs
;
4806 info
->weak_count
= node
->local_weak_refs
;
4808 binder_put_node(node
);
4813 static int binder_ioctl_get_node_debug_info(struct binder_proc
*proc
,
4814 struct binder_node_debug_info
*info
) {
4816 binder_uintptr_t ptr
= info
->ptr
;
4818 memset(info
, 0, sizeof(*info
));
4820 binder_inner_proc_lock(proc
);
4821 for (n
= rb_first(&proc
->nodes
); n
!= NULL
; n
= rb_next(n
)) {
4822 struct binder_node
*node
= rb_entry(n
, struct binder_node
,
4824 if (node
->ptr
> ptr
) {
4825 info
->ptr
= node
->ptr
;
4826 info
->cookie
= node
->cookie
;
4827 info
->has_strong_ref
= node
->has_strong_ref
;
4828 info
->has_weak_ref
= node
->has_weak_ref
;
4832 binder_inner_proc_unlock(proc
);
4837 static long binder_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
4840 struct binder_proc
*proc
= filp
->private_data
;
4841 struct binder_thread
*thread
;
4842 unsigned int size
= _IOC_SIZE(cmd
);
4843 void __user
*ubuf
= (void __user
*)arg
;
4845 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
4846 proc->pid, current->pid, cmd, arg);*/
4848 binder_selftest_alloc(&proc
->alloc
);
4850 trace_binder_ioctl(cmd
, arg
);
4852 ret
= wait_event_interruptible(binder_user_error_wait
, binder_stop_on_user_error
< 2);
4856 thread
= binder_get_thread(proc
);
4857 if (thread
== NULL
) {
4863 case BINDER_WRITE_READ
:
4864 ret
= binder_ioctl_write_read(filp
, cmd
, arg
, thread
);
4868 case BINDER_SET_MAX_THREADS
: {
4871 if (copy_from_user(&max_threads
, ubuf
,
4872 sizeof(max_threads
))) {
4876 binder_inner_proc_lock(proc
);
4877 proc
->max_threads
= max_threads
;
4878 binder_inner_proc_unlock(proc
);
4881 case BINDER_SET_CONTEXT_MGR_EXT
: {
4882 struct flat_binder_object fbo
;
4884 if (copy_from_user(&fbo
, ubuf
, sizeof(fbo
))) {
4888 ret
= binder_ioctl_set_ctx_mgr(filp
, &fbo
);
4893 case BINDER_SET_CONTEXT_MGR
:
4894 ret
= binder_ioctl_set_ctx_mgr(filp
, NULL
);
4898 case BINDER_THREAD_EXIT
:
4899 binder_debug(BINDER_DEBUG_THREADS
, "%d:%d exit\n",
4900 proc
->pid
, thread
->pid
);
4901 binder_thread_release(proc
, thread
);
4904 case BINDER_VERSION
: {
4905 struct binder_version __user
*ver
= ubuf
;
4907 if (size
!= sizeof(struct binder_version
)) {
4911 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION
,
4912 &ver
->protocol_version
)) {
4918 case BINDER_GET_NODE_INFO_FOR_REF
: {
4919 struct binder_node_info_for_ref info
;
4921 if (copy_from_user(&info
, ubuf
, sizeof(info
))) {
4926 ret
= binder_ioctl_get_node_info_for_ref(proc
, &info
);
4930 if (copy_to_user(ubuf
, &info
, sizeof(info
))) {
4937 case BINDER_GET_NODE_DEBUG_INFO
: {
4938 struct binder_node_debug_info info
;
4940 if (copy_from_user(&info
, ubuf
, sizeof(info
))) {
4945 ret
= binder_ioctl_get_node_debug_info(proc
, &info
);
4949 if (copy_to_user(ubuf
, &info
, sizeof(info
))) {
4962 thread
->looper_need_return
= false;
4963 wait_event_interruptible(binder_user_error_wait
, binder_stop_on_user_error
< 2);
4964 if (ret
&& ret
!= -ERESTARTSYS
)
4965 pr_info("%d:%d ioctl %x %lx returned %d\n", proc
->pid
, current
->pid
, cmd
, arg
, ret
);
4967 trace_binder_ioctl_done(ret
);
4971 static void binder_vma_open(struct vm_area_struct
*vma
)
4973 struct binder_proc
*proc
= vma
->vm_private_data
;
4975 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
4976 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
4977 proc
->pid
, vma
->vm_start
, vma
->vm_end
,
4978 (vma
->vm_end
- vma
->vm_start
) / SZ_1K
, vma
->vm_flags
,
4979 (unsigned long)pgprot_val(vma
->vm_page_prot
));
4982 static void binder_vma_close(struct vm_area_struct
*vma
)
4984 struct binder_proc
*proc
= vma
->vm_private_data
;
4986 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
4987 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
4988 proc
->pid
, vma
->vm_start
, vma
->vm_end
,
4989 (vma
->vm_end
- vma
->vm_start
) / SZ_1K
, vma
->vm_flags
,
4990 (unsigned long)pgprot_val(vma
->vm_page_prot
));
4991 binder_alloc_vma_close(&proc
->alloc
);
4992 binder_defer_work(proc
, BINDER_DEFERRED_PUT_FILES
);
4995 static int binder_vm_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
4997 return VM_FAULT_SIGBUS
;
5000 static struct vm_operations_struct binder_vm_ops
= {
5001 .open
= binder_vma_open
,
5002 .close
= binder_vma_close
,
5003 .fault
= binder_vm_fault
,
5006 static int binder_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
5009 struct binder_proc
*proc
= filp
->private_data
;
5010 const char *failure_string
;
5012 if (proc
->tsk
!= current
->group_leader
)
5015 if ((vma
->vm_end
- vma
->vm_start
) > SZ_4M
)
5016 vma
->vm_end
= vma
->vm_start
+ SZ_4M
;
5018 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
5019 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
5020 __func__
, proc
->pid
, vma
->vm_start
, vma
->vm_end
,
5021 (vma
->vm_end
- vma
->vm_start
) / SZ_1K
, vma
->vm_flags
,
5022 (unsigned long)pgprot_val(vma
->vm_page_prot
));
5024 if (vma
->vm_flags
& FORBIDDEN_MMAP_FLAGS
) {
5026 failure_string
= "bad vm_flags";
5029 vma
->vm_flags
= (vma
->vm_flags
| VM_DONTCOPY
) & ~VM_MAYWRITE
;
5030 vma
->vm_ops
= &binder_vm_ops
;
5031 vma
->vm_private_data
= proc
;
5033 ret
= binder_alloc_mmap_handler(&proc
->alloc
, vma
);
5036 mutex_lock(&proc
->files_lock
);
5037 proc
->files
= get_files_struct(current
);
5038 mutex_unlock(&proc
->files_lock
);
5042 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
5043 proc
->pid
, vma
->vm_start
, vma
->vm_end
, failure_string
, ret
);
5047 static int binder_open(struct inode
*nodp
, struct file
*filp
)
5049 struct binder_proc
*proc
;
5050 struct binder_device
*binder_dev
;
5052 binder_debug(BINDER_DEBUG_OPEN_CLOSE
, "binder_open: %d:%d\n",
5053 current
->group_leader
->pid
, current
->pid
);
5055 proc
= kzalloc(sizeof(*proc
), GFP_KERNEL
);
5058 spin_lock_init(&proc
->inner_lock
);
5059 spin_lock_init(&proc
->outer_lock
);
5060 get_task_struct(current
->group_leader
);
5061 proc
->tsk
= current
->group_leader
;
5062 mutex_init(&proc
->files_lock
);
5063 INIT_LIST_HEAD(&proc
->todo
);
5064 if (binder_supported_policy(current
->policy
)) {
5065 proc
->default_priority
.sched_policy
= current
->policy
;
5066 proc
->default_priority
.prio
= current
->normal_prio
;
5068 proc
->default_priority
.sched_policy
= SCHED_NORMAL
;
5069 proc
->default_priority
.prio
= NICE_TO_PRIO(0);
5072 binder_dev
= container_of(filp
->private_data
, struct binder_device
,
5074 proc
->context
= &binder_dev
->context
;
5075 binder_alloc_init(&proc
->alloc
);
5077 binder_stats_created(BINDER_STAT_PROC
);
5078 proc
->pid
= current
->group_leader
->pid
;
5079 INIT_LIST_HEAD(&proc
->delivered_death
);
5080 INIT_LIST_HEAD(&proc
->waiting_threads
);
5081 filp
->private_data
= proc
;
5083 mutex_lock(&binder_procs_lock
);
5084 hlist_add_head(&proc
->proc_node
, &binder_procs
);
5085 mutex_unlock(&binder_procs_lock
);
5087 if (binder_debugfs_dir_entry_proc
) {
5090 snprintf(strbuf
, sizeof(strbuf
), "%u", proc
->pid
);
5092 * proc debug entries are shared between contexts, so
5093 * this will fail if the process tries to open the driver
5094 * again with a different context. The priting code will
5095 * anyway print all contexts that a given PID has, so this
5098 proc
->debugfs_entry
= debugfs_create_file(strbuf
, S_IRUGO
,
5099 binder_debugfs_dir_entry_proc
,
5100 (void *)(unsigned long)proc
->pid
,
5107 static int binder_flush(struct file
*filp
, fl_owner_t id
)
5109 struct binder_proc
*proc
= filp
->private_data
;
5111 binder_defer_work(proc
, BINDER_DEFERRED_FLUSH
);
5116 static void binder_deferred_flush(struct binder_proc
*proc
)
5121 binder_inner_proc_lock(proc
);
5122 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
)) {
5123 struct binder_thread
*thread
= rb_entry(n
, struct binder_thread
, rb_node
);
5125 thread
->looper_need_return
= true;
5126 if (thread
->looper
& BINDER_LOOPER_STATE_WAITING
) {
5127 wake_up_interruptible(&thread
->wait
);
5131 binder_inner_proc_unlock(proc
);
5133 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
5134 "binder_flush: %d woke %d threads\n", proc
->pid
,
5138 static int binder_release(struct inode
*nodp
, struct file
*filp
)
5140 struct binder_proc
*proc
= filp
->private_data
;
5142 debugfs_remove(proc
->debugfs_entry
);
5143 binder_defer_work(proc
, BINDER_DEFERRED_RELEASE
);
5148 static int binder_node_release(struct binder_node
*node
, int refs
)
5150 struct binder_ref
*ref
;
5152 struct binder_proc
*proc
= node
->proc
;
5154 binder_release_work(proc
, &node
->async_todo
);
5156 binder_node_lock(node
);
5157 binder_inner_proc_lock(proc
);
5158 binder_dequeue_work_ilocked(&node
->work
);
5160 * The caller must have taken a temporary ref on the node,
5162 BUG_ON(!node
->tmp_refs
);
5163 if (hlist_empty(&node
->refs
) && node
->tmp_refs
== 1) {
5164 binder_inner_proc_unlock(proc
);
5165 binder_node_unlock(node
);
5166 binder_free_node(node
);
5172 node
->local_strong_refs
= 0;
5173 node
->local_weak_refs
= 0;
5174 binder_inner_proc_unlock(proc
);
5176 spin_lock(&binder_dead_nodes_lock
);
5177 hlist_add_head(&node
->dead_node
, &binder_dead_nodes
);
5178 spin_unlock(&binder_dead_nodes_lock
);
5180 hlist_for_each_entry(ref
, &node
->refs
, node_entry
) {
5183 * Need the node lock to synchronize
5184 * with new notification requests and the
5185 * inner lock to synchronize with queued
5186 * death notifications.
5188 binder_inner_proc_lock(ref
->proc
);
5190 binder_inner_proc_unlock(ref
->proc
);
5196 BUG_ON(!list_empty(&ref
->death
->work
.entry
));
5197 ref
->death
->work
.type
= BINDER_WORK_DEAD_BINDER
;
5198 binder_enqueue_work_ilocked(&ref
->death
->work
,
5200 binder_wakeup_proc_ilocked(ref
->proc
);
5201 binder_inner_proc_unlock(ref
->proc
);
5204 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
5205 "node %d now dead, refs %d, death %d\n",
5206 node
->debug_id
, refs
, death
);
5207 binder_node_unlock(node
);
5208 binder_put_node(node
);
5213 static void binder_deferred_release(struct binder_proc
*proc
)
5215 struct binder_context
*context
= proc
->context
;
5217 int threads
, nodes
, incoming_refs
, outgoing_refs
, active_transactions
;
5219 BUG_ON(proc
->files
);
5221 mutex_lock(&binder_procs_lock
);
5222 hlist_del(&proc
->proc_node
);
5223 mutex_unlock(&binder_procs_lock
);
5225 mutex_lock(&context
->context_mgr_node_lock
);
5226 if (context
->binder_context_mgr_node
&&
5227 context
->binder_context_mgr_node
->proc
== proc
) {
5228 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
5229 "%s: %d context_mgr_node gone\n",
5230 __func__
, proc
->pid
);
5231 context
->binder_context_mgr_node
= NULL
;
5233 mutex_unlock(&context
->context_mgr_node_lock
);
5234 binder_inner_proc_lock(proc
);
5236 * Make sure proc stays alive after we
5237 * remove all the threads
5241 proc
->is_dead
= true;
5243 active_transactions
= 0;
5244 while ((n
= rb_first(&proc
->threads
))) {
5245 struct binder_thread
*thread
;
5247 thread
= rb_entry(n
, struct binder_thread
, rb_node
);
5248 binder_inner_proc_unlock(proc
);
5250 active_transactions
+= binder_thread_release(proc
, thread
);
5251 binder_inner_proc_lock(proc
);
5256 while ((n
= rb_first(&proc
->nodes
))) {
5257 struct binder_node
*node
;
5259 node
= rb_entry(n
, struct binder_node
, rb_node
);
5262 * take a temporary ref on the node before
5263 * calling binder_node_release() which will either
5264 * kfree() the node or call binder_put_node()
5266 binder_inc_node_tmpref_ilocked(node
);
5267 rb_erase(&node
->rb_node
, &proc
->nodes
);
5268 binder_inner_proc_unlock(proc
);
5269 incoming_refs
= binder_node_release(node
, incoming_refs
);
5270 binder_inner_proc_lock(proc
);
5272 binder_inner_proc_unlock(proc
);
5275 binder_proc_lock(proc
);
5276 while ((n
= rb_first(&proc
->refs_by_desc
))) {
5277 struct binder_ref
*ref
;
5279 ref
= rb_entry(n
, struct binder_ref
, rb_node_desc
);
5281 binder_cleanup_ref_olocked(ref
);
5282 binder_proc_unlock(proc
);
5283 binder_free_ref(ref
);
5284 binder_proc_lock(proc
);
5286 binder_proc_unlock(proc
);
5288 binder_release_work(proc
, &proc
->todo
);
5289 binder_release_work(proc
, &proc
->delivered_death
);
5291 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
5292 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
5293 __func__
, proc
->pid
, threads
, nodes
, incoming_refs
,
5294 outgoing_refs
, active_transactions
);
5296 binder_proc_dec_tmpref(proc
);
5299 static void binder_deferred_func(struct work_struct
*work
)
5301 struct binder_proc
*proc
;
5302 struct files_struct
*files
;
5307 mutex_lock(&binder_deferred_lock
);
5308 if (!hlist_empty(&binder_deferred_list
)) {
5309 proc
= hlist_entry(binder_deferred_list
.first
,
5310 struct binder_proc
, deferred_work_node
);
5311 hlist_del_init(&proc
->deferred_work_node
);
5312 defer
= proc
->deferred_work
;
5313 proc
->deferred_work
= 0;
5318 mutex_unlock(&binder_deferred_lock
);
5321 if (defer
& BINDER_DEFERRED_PUT_FILES
) {
5322 mutex_lock(&proc
->files_lock
);
5323 files
= proc
->files
;
5326 mutex_unlock(&proc
->files_lock
);
5329 if (defer
& BINDER_DEFERRED_FLUSH
)
5330 binder_deferred_flush(proc
);
5332 if (defer
& BINDER_DEFERRED_RELEASE
)
5333 binder_deferred_release(proc
); /* frees proc */
5336 put_files_struct(files
);
5339 static DECLARE_WORK(binder_deferred_work
, binder_deferred_func
);
5342 binder_defer_work(struct binder_proc
*proc
, enum binder_deferred_state defer
)
5344 mutex_lock(&binder_deferred_lock
);
5345 proc
->deferred_work
|= defer
;
5346 if (hlist_unhashed(&proc
->deferred_work_node
)) {
5347 hlist_add_head(&proc
->deferred_work_node
,
5348 &binder_deferred_list
);
5349 queue_work(binder_deferred_workqueue
, &binder_deferred_work
);
5351 mutex_unlock(&binder_deferred_lock
);
5354 static void print_binder_transaction_ilocked(struct seq_file
*m
,
5355 struct binder_proc
*proc
,
5357 struct binder_transaction
*t
)
5359 struct binder_proc
*to_proc
;
5360 struct binder_buffer
*buffer
= t
->buffer
;
5362 spin_lock(&t
->lock
);
5363 to_proc
= t
->to_proc
;
5365 "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %d:%d r%d",
5366 prefix
, t
->debug_id
, t
,
5367 t
->from
? t
->from
->proc
->pid
: 0,
5368 t
->from
? t
->from
->pid
: 0,
5369 to_proc
? to_proc
->pid
: 0,
5370 t
->to_thread
? t
->to_thread
->pid
: 0,
5371 t
->code
, t
->flags
, t
->priority
.sched_policy
,
5372 t
->priority
.prio
, t
->need_reply
);
5373 spin_unlock(&t
->lock
);
5375 if (proc
!= to_proc
) {
5377 * Can only safely deref buffer if we are holding the
5378 * correct proc inner lock for this node
5384 if (buffer
== NULL
) {
5385 seq_puts(m
, " buffer free\n");
5388 if (buffer
->target_node
)
5389 seq_printf(m
, " node %d", buffer
->target_node
->debug_id
);
5390 seq_printf(m
, " size %zd:%zd data %p\n",
5391 buffer
->data_size
, buffer
->offsets_size
,
5395 static void print_binder_work_ilocked(struct seq_file
*m
,
5396 struct binder_proc
*proc
,
5398 const char *transaction_prefix
,
5399 struct binder_work
*w
)
5401 struct binder_node
*node
;
5402 struct binder_transaction
*t
;
5405 case BINDER_WORK_TRANSACTION
:
5406 t
= container_of(w
, struct binder_transaction
, work
);
5407 print_binder_transaction_ilocked(
5408 m
, proc
, transaction_prefix
, t
);
5410 case BINDER_WORK_RETURN_ERROR
: {
5411 struct binder_error
*e
= container_of(
5412 w
, struct binder_error
, work
);
5414 seq_printf(m
, "%stransaction error: %u\n",
5417 case BINDER_WORK_TRANSACTION_COMPLETE
:
5418 seq_printf(m
, "%stransaction complete\n", prefix
);
5420 case BINDER_WORK_NODE
:
5421 node
= container_of(w
, struct binder_node
, work
);
5422 seq_printf(m
, "%snode work %d: u%016llx c%016llx\n",
5423 prefix
, node
->debug_id
,
5424 (u64
)node
->ptr
, (u64
)node
->cookie
);
5426 case BINDER_WORK_DEAD_BINDER
:
5427 seq_printf(m
, "%shas dead binder\n", prefix
);
5429 case BINDER_WORK_DEAD_BINDER_AND_CLEAR
:
5430 seq_printf(m
, "%shas cleared dead binder\n", prefix
);
5432 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION
:
5433 seq_printf(m
, "%shas cleared death notification\n", prefix
);
5436 seq_printf(m
, "%sunknown work: type %d\n", prefix
, w
->type
);
5441 static void print_binder_thread_ilocked(struct seq_file
*m
,
5442 struct binder_thread
*thread
,
5445 struct binder_transaction
*t
;
5446 struct binder_work
*w
;
5447 size_t start_pos
= m
->count
;
5450 seq_printf(m
, " thread %d: l %02x need_return %d tr %d\n",
5451 thread
->pid
, thread
->looper
,
5452 thread
->looper_need_return
,
5453 atomic_read(&thread
->tmp_ref
));
5454 header_pos
= m
->count
;
5455 t
= thread
->transaction_stack
;
5457 if (t
->from
== thread
) {
5458 print_binder_transaction_ilocked(m
, thread
->proc
,
5459 " outgoing transaction", t
);
5461 } else if (t
->to_thread
== thread
) {
5462 print_binder_transaction_ilocked(m
, thread
->proc
,
5463 " incoming transaction", t
);
5466 print_binder_transaction_ilocked(m
, thread
->proc
,
5467 " bad transaction", t
);
5471 list_for_each_entry(w
, &thread
->todo
, entry
) {
5472 print_binder_work_ilocked(m
, thread
->proc
, " ",
5473 " pending transaction", w
);
5475 if (!print_always
&& m
->count
== header_pos
)
5476 m
->count
= start_pos
;
5479 static void print_binder_node_nilocked(struct seq_file
*m
,
5480 struct binder_node
*node
)
5482 struct binder_ref
*ref
;
5483 struct binder_work
*w
;
5487 hlist_for_each_entry(ref
, &node
->refs
, node_entry
)
5490 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",
5491 node
->debug_id
, (u64
)node
->ptr
, (u64
)node
->cookie
,
5492 node
->sched_policy
, node
->min_priority
,
5493 node
->has_strong_ref
, node
->has_weak_ref
,
5494 node
->local_strong_refs
, node
->local_weak_refs
,
5495 node
->internal_strong_refs
, count
, node
->tmp_refs
);
5497 seq_puts(m
, " proc");
5498 hlist_for_each_entry(ref
, &node
->refs
, node_entry
)
5499 seq_printf(m
, " %d", ref
->proc
->pid
);
5503 list_for_each_entry(w
, &node
->async_todo
, entry
)
5504 print_binder_work_ilocked(m
, node
->proc
, " ",
5505 " pending async transaction", w
);
5509 static void print_binder_ref_olocked(struct seq_file
*m
,
5510 struct binder_ref
*ref
)
5512 binder_node_lock(ref
->node
);
5513 seq_printf(m
, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
5514 ref
->data
.debug_id
, ref
->data
.desc
,
5515 ref
->node
->proc
? "" : "dead ",
5516 ref
->node
->debug_id
, ref
->data
.strong
,
5517 ref
->data
.weak
, ref
->death
);
5518 binder_node_unlock(ref
->node
);
5521 static void print_binder_proc(struct seq_file
*m
,
5522 struct binder_proc
*proc
, int print_all
)
5524 struct binder_work
*w
;
5526 size_t start_pos
= m
->count
;
5528 struct binder_node
*last_node
= NULL
;
5530 seq_printf(m
, "proc %d\n", proc
->pid
);
5531 seq_printf(m
, "context %s\n", proc
->context
->name
);
5532 header_pos
= m
->count
;
5534 binder_inner_proc_lock(proc
);
5535 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
))
5536 print_binder_thread_ilocked(m
, rb_entry(n
, struct binder_thread
,
5537 rb_node
), print_all
);
5539 for (n
= rb_first(&proc
->nodes
); n
!= NULL
; n
= rb_next(n
)) {
5540 struct binder_node
*node
= rb_entry(n
, struct binder_node
,
5543 * take a temporary reference on the node so it
5544 * survives and isn't removed from the tree
5545 * while we print it.
5547 binder_inc_node_tmpref_ilocked(node
);
5548 /* Need to drop inner lock to take node lock */
5549 binder_inner_proc_unlock(proc
);
5551 binder_put_node(last_node
);
5552 binder_node_inner_lock(node
);
5553 print_binder_node_nilocked(m
, node
);
5554 binder_node_inner_unlock(node
);
5556 binder_inner_proc_lock(proc
);
5558 binder_inner_proc_unlock(proc
);
5560 binder_put_node(last_node
);
5563 binder_proc_lock(proc
);
5564 for (n
= rb_first(&proc
->refs_by_desc
);
5567 print_binder_ref_olocked(m
, rb_entry(n
,
5570 binder_proc_unlock(proc
);
5572 binder_alloc_print_allocated(m
, &proc
->alloc
);
5573 binder_inner_proc_lock(proc
);
5574 list_for_each_entry(w
, &proc
->todo
, entry
)
5575 print_binder_work_ilocked(m
, proc
, " ",
5576 " pending transaction", w
);
5577 list_for_each_entry(w
, &proc
->delivered_death
, entry
) {
5578 seq_puts(m
, " has delivered dead binder\n");
5581 binder_inner_proc_unlock(proc
);
5582 if (!print_all
&& m
->count
== header_pos
)
5583 m
->count
= start_pos
;
5586 static const char * const binder_return_strings
[] = {
5591 "BR_ACQUIRE_RESULT",
5593 "BR_TRANSACTION_COMPLETE",
5598 "BR_ATTEMPT_ACQUIRE",
5603 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
5607 static const char * const binder_command_strings
[] = {
5610 "BC_ACQUIRE_RESULT",
5618 "BC_ATTEMPT_ACQUIRE",
5619 "BC_REGISTER_LOOPER",
5622 "BC_REQUEST_DEATH_NOTIFICATION",
5623 "BC_CLEAR_DEATH_NOTIFICATION",
5624 "BC_DEAD_BINDER_DONE",
5625 "BC_TRANSACTION_SG",
5629 static const char * const binder_objstat_strings
[] = {
5636 "transaction_complete"
5639 static void print_binder_stats(struct seq_file
*m
, const char *prefix
,
5640 struct binder_stats
*stats
)
5644 BUILD_BUG_ON(ARRAY_SIZE(stats
->bc
) !=
5645 ARRAY_SIZE(binder_command_strings
));
5646 for (i
= 0; i
< ARRAY_SIZE(stats
->bc
); i
++) {
5647 int temp
= atomic_read(&stats
->bc
[i
]);
5650 seq_printf(m
, "%s%s: %d\n", prefix
,
5651 binder_command_strings
[i
], temp
);
5654 BUILD_BUG_ON(ARRAY_SIZE(stats
->br
) !=
5655 ARRAY_SIZE(binder_return_strings
));
5656 for (i
= 0; i
< ARRAY_SIZE(stats
->br
); i
++) {
5657 int temp
= atomic_read(&stats
->br
[i
]);
5660 seq_printf(m
, "%s%s: %d\n", prefix
,
5661 binder_return_strings
[i
], temp
);
5664 BUILD_BUG_ON(ARRAY_SIZE(stats
->obj_created
) !=
5665 ARRAY_SIZE(binder_objstat_strings
));
5666 BUILD_BUG_ON(ARRAY_SIZE(stats
->obj_created
) !=
5667 ARRAY_SIZE(stats
->obj_deleted
));
5668 for (i
= 0; i
< ARRAY_SIZE(stats
->obj_created
); i
++) {
5669 int created
= atomic_read(&stats
->obj_created
[i
]);
5670 int deleted
= atomic_read(&stats
->obj_deleted
[i
]);
5672 if (created
|| deleted
)
5673 seq_printf(m
, "%s%s: active %d total %d\n",
5675 binder_objstat_strings
[i
],
5681 static void print_binder_proc_stats(struct seq_file
*m
,
5682 struct binder_proc
*proc
)
5684 struct binder_work
*w
;
5685 struct binder_thread
*thread
;
5687 int count
, strong
, weak
, ready_threads
;
5688 size_t free_async_space
=
5689 binder_alloc_get_free_async_space(&proc
->alloc
);
5691 seq_printf(m
, "proc %d\n", proc
->pid
);
5692 seq_printf(m
, "context %s\n", proc
->context
->name
);
5695 binder_inner_proc_lock(proc
);
5696 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
))
5699 list_for_each_entry(thread
, &proc
->waiting_threads
, waiting_thread_node
)
5702 seq_printf(m
, " threads: %d\n", count
);
5703 seq_printf(m
, " requested threads: %d+%d/%d\n"
5704 " ready threads %d\n"
5705 " free async space %zd\n", proc
->requested_threads
,
5706 proc
->requested_threads_started
, proc
->max_threads
,
5710 for (n
= rb_first(&proc
->nodes
); n
!= NULL
; n
= rb_next(n
))
5712 binder_inner_proc_unlock(proc
);
5713 seq_printf(m
, " nodes: %d\n", count
);
5717 binder_proc_lock(proc
);
5718 for (n
= rb_first(&proc
->refs_by_desc
); n
!= NULL
; n
= rb_next(n
)) {
5719 struct binder_ref
*ref
= rb_entry(n
, struct binder_ref
,
5722 strong
+= ref
->data
.strong
;
5723 weak
+= ref
->data
.weak
;
5725 binder_proc_unlock(proc
);
5726 seq_printf(m
, " refs: %d s %d w %d\n", count
, strong
, weak
);
5728 count
= binder_alloc_get_allocated_count(&proc
->alloc
);
5729 seq_printf(m
, " buffers: %d\n", count
);
5732 binder_inner_proc_lock(proc
);
5733 list_for_each_entry(w
, &proc
->todo
, entry
) {
5734 if (w
->type
== BINDER_WORK_TRANSACTION
)
5737 binder_inner_proc_unlock(proc
);
5738 seq_printf(m
, " pending transactions: %d\n", count
);
5740 print_binder_stats(m
, " ", &proc
->stats
);
5744 static int binder_state_show(struct seq_file
*m
, void *unused
)
5746 struct binder_proc
*proc
;
5747 struct binder_node
*node
;
5748 struct binder_node
*last_node
= NULL
;
5750 seq_puts(m
, "binder state:\n");
5752 spin_lock(&binder_dead_nodes_lock
);
5753 if (!hlist_empty(&binder_dead_nodes
))
5754 seq_puts(m
, "dead nodes:\n");
5755 hlist_for_each_entry(node
, &binder_dead_nodes
, dead_node
) {
5757 * take a temporary reference on the node so it
5758 * survives and isn't removed from the list
5759 * while we print it.
5762 spin_unlock(&binder_dead_nodes_lock
);
5764 binder_put_node(last_node
);
5765 binder_node_lock(node
);
5766 print_binder_node_nilocked(m
, node
);
5767 binder_node_unlock(node
);
5769 spin_lock(&binder_dead_nodes_lock
);
5771 spin_unlock(&binder_dead_nodes_lock
);
5773 binder_put_node(last_node
);
5775 mutex_lock(&binder_procs_lock
);
5776 hlist_for_each_entry(proc
, &binder_procs
, proc_node
)
5777 print_binder_proc(m
, proc
, 1);
5778 mutex_unlock(&binder_procs_lock
);
5783 static int binder_stats_show(struct seq_file
*m
, void *unused
)
5785 struct binder_proc
*proc
;
5787 seq_puts(m
, "binder stats:\n");
5789 print_binder_stats(m
, "", &binder_stats
);
5791 mutex_lock(&binder_procs_lock
);
5792 hlist_for_each_entry(proc
, &binder_procs
, proc_node
)
5793 print_binder_proc_stats(m
, proc
);
5794 mutex_unlock(&binder_procs_lock
);
5799 static int binder_transactions_show(struct seq_file
*m
, void *unused
)
5801 struct binder_proc
*proc
;
5803 seq_puts(m
, "binder transactions:\n");
5804 mutex_lock(&binder_procs_lock
);
5805 hlist_for_each_entry(proc
, &binder_procs
, proc_node
)
5806 print_binder_proc(m
, proc
, 0);
5807 mutex_unlock(&binder_procs_lock
);
5812 static int binder_proc_show(struct seq_file
*m
, void *unused
)
5814 struct binder_proc
*itr
;
5815 int pid
= (unsigned long)m
->private;
5817 mutex_lock(&binder_procs_lock
);
5818 hlist_for_each_entry(itr
, &binder_procs
, proc_node
) {
5819 if (itr
->pid
== pid
) {
5820 seq_puts(m
, "binder proc state:\n");
5821 print_binder_proc(m
, itr
, 1);
5824 mutex_unlock(&binder_procs_lock
);
5829 static void print_binder_transaction_log_entry(struct seq_file
*m
,
5830 struct binder_transaction_log_entry
*e
)
5832 int debug_id
= READ_ONCE(e
->debug_id_done
);
5834 * read barrier to guarantee debug_id_done read before
5835 * we print the log values
5839 "%d: %s from %d:%d to %d:%d context %s node %d handle %d size %d:%d ret %d/%d l=%d",
5840 e
->debug_id
, (e
->call_type
== 2) ? "reply" :
5841 ((e
->call_type
== 1) ? "async" : "call "), e
->from_proc
,
5842 e
->from_thread
, e
->to_proc
, e
->to_thread
, e
->context_name
,
5843 e
->to_node
, e
->target_handle
, e
->data_size
, e
->offsets_size
,
5844 e
->return_error
, e
->return_error_param
,
5845 e
->return_error_line
);
5847 * read-barrier to guarantee read of debug_id_done after
5848 * done printing the fields of the entry
5851 seq_printf(m
, debug_id
&& debug_id
== READ_ONCE(e
->debug_id_done
) ?
5852 "\n" : " (incomplete)\n");
5855 static int binder_transaction_log_show(struct seq_file
*m
, void *unused
)
5857 struct binder_transaction_log
*log
= m
->private;
5858 unsigned int log_cur
= atomic_read(&log
->cur
);
5863 count
= log_cur
+ 1;
5864 cur
= count
< ARRAY_SIZE(log
->entry
) && !log
->full
?
5865 0 : count
% ARRAY_SIZE(log
->entry
);
5866 if (count
> ARRAY_SIZE(log
->entry
) || log
->full
)
5867 count
= ARRAY_SIZE(log
->entry
);
5868 for (i
= 0; i
< count
; i
++) {
5869 unsigned int index
= cur
++ % ARRAY_SIZE(log
->entry
);
5871 print_binder_transaction_log_entry(m
, &log
->entry
[index
]);
5876 static const struct file_operations binder_fops
= {
5877 .owner
= THIS_MODULE
,
5878 .poll
= binder_poll
,
5879 .unlocked_ioctl
= binder_ioctl
,
5880 .compat_ioctl
= binder_ioctl
,
5881 .mmap
= binder_mmap
,
5882 .open
= binder_open
,
5883 .flush
= binder_flush
,
5884 .release
= binder_release
,
5887 BINDER_DEBUG_ENTRY(state
);
5888 BINDER_DEBUG_ENTRY(stats
);
5889 BINDER_DEBUG_ENTRY(transactions
);
5890 BINDER_DEBUG_ENTRY(transaction_log
);
5892 static int __init
init_binder_device(const char *name
)
5895 struct binder_device
*binder_device
;
5897 binder_device
= kzalloc(sizeof(*binder_device
), GFP_KERNEL
);
5901 binder_device
->miscdev
.fops
= &binder_fops
;
5902 binder_device
->miscdev
.minor
= MISC_DYNAMIC_MINOR
;
5903 binder_device
->miscdev
.name
= name
;
5905 binder_device
->context
.binder_context_mgr_uid
= INVALID_UID
;
5906 binder_device
->context
.name
= name
;
5907 mutex_init(&binder_device
->context
.context_mgr_node_lock
);
5909 ret
= misc_register(&binder_device
->miscdev
);
5911 kfree(binder_device
);
5915 hlist_add_head(&binder_device
->hlist
, &binder_devices
);
5920 static int __init
binder_init(void)
5923 char *device_name
, *device_names
;
5924 struct binder_device
*device
;
5925 struct hlist_node
*tmp
;
5927 atomic_set(&binder_transaction_log
.cur
, ~0U);
5928 atomic_set(&binder_transaction_log_failed
.cur
, ~0U);
5929 binder_deferred_workqueue
= create_singlethread_workqueue("binder");
5930 if (!binder_deferred_workqueue
)
5933 binder_debugfs_dir_entry_root
= debugfs_create_dir("binder", NULL
);
5934 if (binder_debugfs_dir_entry_root
)
5935 binder_debugfs_dir_entry_proc
= debugfs_create_dir("proc",
5936 binder_debugfs_dir_entry_root
);
5938 if (binder_debugfs_dir_entry_root
) {
5939 debugfs_create_file("state",
5941 binder_debugfs_dir_entry_root
,
5943 &binder_state_fops
);
5944 debugfs_create_file("stats",
5946 binder_debugfs_dir_entry_root
,
5948 &binder_stats_fops
);
5949 debugfs_create_file("transactions",
5951 binder_debugfs_dir_entry_root
,
5953 &binder_transactions_fops
);
5954 debugfs_create_file("transaction_log",
5956 binder_debugfs_dir_entry_root
,
5957 &binder_transaction_log
,
5958 &binder_transaction_log_fops
);
5959 debugfs_create_file("failed_transaction_log",
5961 binder_debugfs_dir_entry_root
,
5962 &binder_transaction_log_failed
,
5963 &binder_transaction_log_fops
);
5967 * Copy the module_parameter string, because we don't want to
5968 * tokenize it in-place.
5970 device_names
= kzalloc(strlen(binder_devices_param
) + 1, GFP_KERNEL
);
5971 if (!device_names
) {
5973 goto err_alloc_device_names_failed
;
5975 strcpy(device_names
, binder_devices_param
);
5977 while ((device_name
= strsep(&device_names
, ","))) {
5978 ret
= init_binder_device(device_name
);
5980 goto err_init_binder_device_failed
;
5985 err_init_binder_device_failed
:
5986 hlist_for_each_entry_safe(device
, tmp
, &binder_devices
, hlist
) {
5987 misc_deregister(&device
->miscdev
);
5988 hlist_del(&device
->hlist
);
5991 err_alloc_device_names_failed
:
5992 debugfs_remove_recursive(binder_debugfs_dir_entry_root
);
5994 destroy_workqueue(binder_deferred_workqueue
);
5999 device_initcall(binder_init
);
6001 #define CREATE_TRACE_POINTS
6002 #include "binder_trace.h"
6004 MODULE_LICENSE("GPL v2");