3 * Android IPC Subsystem
5 * Copyright (C) 2007-2008 Google, Inc.
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
21 * There are 3 main spinlocks which must be acquired in the
24 * 1) proc->outer_lock : protects binder_ref
25 * binder_proc_lock() and binder_proc_unlock() are
27 * 2) node->lock : protects most fields of binder_node.
28 * binder_node_lock() and binder_node_unlock() are
30 * 3) proc->inner_lock : protects the thread and node lists
31 * (proc->threads, proc->waiting_threads, proc->nodes)
32 * and all todo lists associated with the binder_proc
33 * (proc->todo, thread->todo, proc->delivered_death and
34 * node->async_todo), as well as thread->transaction_stack
35 * binder_inner_proc_lock() and binder_inner_proc_unlock()
38 * Any lock under procA must never be nested under any lock at the same
39 * level or below on procB.
41 * Functions that require a lock held on entry indicate which lock
42 * in the suffix of the function name:
44 * foo_olocked() : requires node->outer_lock
45 * foo_nlocked() : requires node->lock
46 * foo_ilocked() : requires proc->inner_lock
47 * foo_oilocked(): requires proc->outer_lock and proc->inner_lock
48 * foo_nilocked(): requires node->lock and proc->inner_lock
52 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
54 #include <asm/cacheflush.h>
55 #include <linux/fdtable.h>
56 #include <linux/file.h>
57 #include <linux/freezer.h>
59 #include <linux/list.h>
60 #include <linux/miscdevice.h>
61 #include <linux/module.h>
62 #include <linux/mutex.h>
63 #include <linux/nsproxy.h>
64 #include <linux/poll.h>
65 #include <linux/debugfs.h>
66 #include <linux/rbtree.h>
67 #include <linux/sched.h>
68 #include <linux/seq_file.h>
69 #include <linux/uaccess.h>
70 #include <linux/pid_namespace.h>
71 #include <linux/security.h>
72 #include <linux/spinlock.h>
74 #include <linux/sched/rt.h>
77 #define NICE_WIDTH (MAX_NICE - MIN_NICE + 1)
78 #define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
79 #define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
81 #ifdef CONFIG_ANDROID_BINDER_IPC_32BIT
82 #define BINDER_IPC_32BIT 1
85 #include <uapi/linux/android/binder.h>
86 #include "binder_alloc.h"
87 #include "binder_trace.h"
89 static HLIST_HEAD(binder_deferred_list
);
90 static DEFINE_MUTEX(binder_deferred_lock
);
92 static HLIST_HEAD(binder_devices
);
93 static HLIST_HEAD(binder_procs
);
94 static DEFINE_MUTEX(binder_procs_lock
);
96 static HLIST_HEAD(binder_dead_nodes
);
97 static DEFINE_SPINLOCK(binder_dead_nodes_lock
);
99 static struct dentry
*binder_debugfs_dir_entry_root
;
100 static struct dentry
*binder_debugfs_dir_entry_proc
;
101 static atomic_t binder_last_id
;
102 static struct workqueue_struct
*binder_deferred_workqueue
;
104 #define BINDER_DEBUG_ENTRY(name) \
105 static int binder_##name##_open(struct inode *inode, struct file *file) \
107 return single_open(file, binder_##name##_show, inode->i_private); \
110 static const struct file_operations binder_##name##_fops = { \
111 .owner = THIS_MODULE, \
112 .open = binder_##name##_open, \
114 .llseek = seq_lseek, \
115 .release = single_release, \
118 static int binder_proc_show(struct seq_file
*m
, void *unused
);
119 BINDER_DEBUG_ENTRY(proc
);
121 /* This is only defined in include/asm-arm/sizes.h */
127 #define SZ_4M 0x400000
130 #define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
132 #define BINDER_SMALL_BUF_SIZE (PAGE_SIZE * 64)
135 BINDER_DEBUG_USER_ERROR
= 1U << 0,
136 BINDER_DEBUG_FAILED_TRANSACTION
= 1U << 1,
137 BINDER_DEBUG_DEAD_TRANSACTION
= 1U << 2,
138 BINDER_DEBUG_OPEN_CLOSE
= 1U << 3,
139 BINDER_DEBUG_DEAD_BINDER
= 1U << 4,
140 BINDER_DEBUG_DEATH_NOTIFICATION
= 1U << 5,
141 BINDER_DEBUG_READ_WRITE
= 1U << 6,
142 BINDER_DEBUG_USER_REFS
= 1U << 7,
143 BINDER_DEBUG_THREADS
= 1U << 8,
144 BINDER_DEBUG_TRANSACTION
= 1U << 9,
145 BINDER_DEBUG_TRANSACTION_COMPLETE
= 1U << 10,
146 BINDER_DEBUG_FREE_BUFFER
= 1U << 11,
147 BINDER_DEBUG_INTERNAL_REFS
= 1U << 12,
148 BINDER_DEBUG_PRIORITY_CAP
= 1U << 13,
149 BINDER_DEBUG_SPINLOCKS
= 1U << 14,
151 static uint32_t binder_debug_mask
;
153 module_param_named(debug_mask
, binder_debug_mask
, uint
, S_IWUSR
| S_IRUGO
);
155 static char *binder_devices_param
= CONFIG_ANDROID_BINDER_DEVICES
;
156 module_param_named(devices
, binder_devices_param
, charp
, S_IRUGO
);
158 static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait
);
159 static int binder_stop_on_user_error
;
161 static int binder_set_stop_on_user_error(const char *val
,
162 struct kernel_param
*kp
)
166 ret
= param_set_int(val
, kp
);
167 if (binder_stop_on_user_error
< 2)
168 wake_up(&binder_user_error_wait
);
171 module_param_call(stop_on_user_error
, binder_set_stop_on_user_error
,
172 param_get_int
, &binder_stop_on_user_error
, S_IWUSR
| S_IRUGO
);
174 #define binder_debug(mask, x...) \
176 if (binder_debug_mask & mask) \
180 #define binder_user_error(x...) \
182 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
184 if (binder_stop_on_user_error) \
185 binder_stop_on_user_error = 2; \
188 #define to_flat_binder_object(hdr) \
189 container_of(hdr, struct flat_binder_object, hdr)
191 #define to_binder_fd_object(hdr) container_of(hdr, struct binder_fd_object, hdr)
193 #define to_binder_buffer_object(hdr) \
194 container_of(hdr, struct binder_buffer_object, hdr)
196 #define to_binder_fd_array_object(hdr) \
197 container_of(hdr, struct binder_fd_array_object, hdr)
199 enum binder_stat_types
{
205 BINDER_STAT_TRANSACTION
,
206 BINDER_STAT_TRANSACTION_COMPLETE
,
210 struct binder_stats
{
211 atomic_t br
[_IOC_NR(BR_FAILED_REPLY
) + 1];
212 atomic_t bc
[_IOC_NR(BC_REPLY_SG
) + 1];
213 atomic_t obj_created
[BINDER_STAT_COUNT
];
214 atomic_t obj_deleted
[BINDER_STAT_COUNT
];
217 static struct binder_stats binder_stats
;
219 static inline void binder_stats_deleted(enum binder_stat_types type
)
221 atomic_inc(&binder_stats
.obj_deleted
[type
]);
224 static inline void binder_stats_created(enum binder_stat_types type
)
226 atomic_inc(&binder_stats
.obj_created
[type
]);
229 struct binder_transaction_log_entry
{
241 int return_error_line
;
242 uint32_t return_error
;
243 uint32_t return_error_param
;
244 const char *context_name
;
246 struct binder_transaction_log
{
249 struct binder_transaction_log_entry entry
[32];
251 static struct binder_transaction_log binder_transaction_log
;
252 static struct binder_transaction_log binder_transaction_log_failed
;
254 static struct binder_transaction_log_entry
*binder_transaction_log_add(
255 struct binder_transaction_log
*log
)
257 struct binder_transaction_log_entry
*e
;
258 unsigned int cur
= atomic_inc_return(&log
->cur
);
260 if (cur
>= ARRAY_SIZE(log
->entry
))
262 e
= &log
->entry
[cur
% ARRAY_SIZE(log
->entry
)];
263 WRITE_ONCE(e
->debug_id_done
, 0);
265 * write-barrier to synchronize access to e->debug_id_done.
266 * We make sure the initialized 0 value is seen before
267 * memset() other fields are zeroed by memset.
270 memset(e
, 0, sizeof(*e
));
274 struct binder_context
{
275 struct binder_node
*binder_context_mgr_node
;
276 struct mutex context_mgr_node_lock
;
278 kuid_t binder_context_mgr_uid
;
282 struct binder_device
{
283 struct hlist_node hlist
;
284 struct miscdevice miscdev
;
285 struct binder_context context
;
289 * struct binder_work - work enqueued on a worklist
290 * @entry: node enqueued on list
291 * @type: type of work to be performed
293 * There are separate work lists for proc, thread, and node (async).
296 struct list_head entry
;
299 BINDER_WORK_TRANSACTION
= 1,
300 BINDER_WORK_TRANSACTION_COMPLETE
,
301 BINDER_WORK_RETURN_ERROR
,
303 BINDER_WORK_DEAD_BINDER
,
304 BINDER_WORK_DEAD_BINDER_AND_CLEAR
,
305 BINDER_WORK_CLEAR_DEATH_NOTIFICATION
,
309 struct binder_error
{
310 struct binder_work work
;
315 * struct binder_node - binder node bookkeeping
316 * @debug_id: unique ID for debugging
317 * (invariant after initialized)
318 * @lock: lock for node fields
319 * @work: worklist element for node work
320 * (protected by @proc->inner_lock)
321 * @rb_node: element for proc->nodes tree
322 * (protected by @proc->inner_lock)
323 * @dead_node: element for binder_dead_nodes list
324 * (protected by binder_dead_nodes_lock)
325 * @proc: binder_proc that owns this node
326 * (invariant after initialized)
327 * @refs: list of references on this node
328 * (protected by @lock)
329 * @internal_strong_refs: used to take strong references when
330 * initiating a transaction
331 * (protected by @proc->inner_lock if @proc
333 * @local_weak_refs: weak user refs from local process
334 * (protected by @proc->inner_lock if @proc
336 * @local_strong_refs: strong user refs from local process
337 * (protected by @proc->inner_lock if @proc
339 * @tmp_refs: temporary kernel refs
340 * (protected by @proc->inner_lock while @proc
341 * is valid, and by binder_dead_nodes_lock
342 * if @proc is NULL. During inc/dec and node release
343 * it is also protected by @lock to provide safety
344 * as the node dies and @proc becomes NULL)
345 * @ptr: userspace pointer for node
346 * (invariant, no lock needed)
347 * @cookie: userspace cookie for node
348 * (invariant, no lock needed)
349 * @has_strong_ref: userspace notified of strong ref
350 * (protected by @proc->inner_lock if @proc
352 * @pending_strong_ref: userspace has acked notification of strong ref
353 * (protected by @proc->inner_lock if @proc
355 * @has_weak_ref: userspace notified of weak ref
356 * (protected by @proc->inner_lock if @proc
358 * @pending_weak_ref: userspace has acked notification of weak ref
359 * (protected by @proc->inner_lock if @proc
361 * @has_async_transaction: async transaction to node in progress
362 * (protected by @lock)
363 * @sched_policy: minimum scheduling policy for node
364 * (invariant after initialized)
365 * @accept_fds: file descriptor operations supported for node
366 * (invariant after initialized)
367 * @min_priority: minimum scheduling priority
368 * (invariant after initialized)
369 * @inherit_rt: inherit RT scheduling policy from caller
370 * @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_dequeue_work_head() - Dequeues the item at head of list
936 * @proc: binder_proc associated with list
937 * @list: list to dequeue head
939 * Removes the head of the list if there are items on the list
941 * Return: pointer dequeued binder_work, NULL if list was empty
943 static struct binder_work
*binder_dequeue_work_head(
944 struct binder_proc
*proc
,
945 struct list_head
*list
)
947 struct binder_work
*w
;
949 binder_inner_proc_lock(proc
);
950 w
= binder_dequeue_work_head_ilocked(list
);
951 binder_inner_proc_unlock(proc
);
956 binder_defer_work(struct binder_proc
*proc
, enum binder_deferred_state defer
);
957 static void binder_free_thread(struct binder_thread
*thread
);
958 static void binder_free_proc(struct binder_proc
*proc
);
959 static void binder_inc_node_tmpref_ilocked(struct binder_node
*node
);
961 static int task_get_unused_fd_flags(struct binder_proc
*proc
, int flags
)
963 unsigned long rlim_cur
;
967 mutex_lock(&proc
->files_lock
);
968 if (proc
->files
== NULL
) {
972 if (!lock_task_sighand(proc
->tsk
, &irqs
)) {
976 rlim_cur
= task_rlimit(proc
->tsk
, RLIMIT_NOFILE
);
977 unlock_task_sighand(proc
->tsk
, &irqs
);
979 ret
= __alloc_fd(proc
->files
, 0, rlim_cur
, flags
);
981 mutex_unlock(&proc
->files_lock
);
986 * copied from fd_install
988 static void task_fd_install(
989 struct binder_proc
*proc
, unsigned int fd
, struct file
*file
)
991 mutex_lock(&proc
->files_lock
);
993 __fd_install(proc
->files
, fd
, file
);
994 mutex_unlock(&proc
->files_lock
);
998 * copied from sys_close
1000 static long task_close_fd(struct binder_proc
*proc
, unsigned int fd
)
1004 mutex_lock(&proc
->files_lock
);
1005 if (proc
->files
== NULL
) {
1009 retval
= __close_fd(proc
->files
, fd
);
1010 /* can't restart close syscall because file table entry was cleared */
1011 if (unlikely(retval
== -ERESTARTSYS
||
1012 retval
== -ERESTARTNOINTR
||
1013 retval
== -ERESTARTNOHAND
||
1014 retval
== -ERESTART_RESTARTBLOCK
))
1017 mutex_unlock(&proc
->files_lock
);
1021 static bool binder_has_work_ilocked(struct binder_thread
*thread
,
1024 return thread
->process_todo
||
1025 thread
->looper_need_return
||
1027 !binder_worklist_empty_ilocked(&thread
->proc
->todo
));
1030 static bool binder_has_work(struct binder_thread
*thread
, bool do_proc_work
)
1034 binder_inner_proc_lock(thread
->proc
);
1035 has_work
= binder_has_work_ilocked(thread
, do_proc_work
);
1036 binder_inner_proc_unlock(thread
->proc
);
1041 static bool binder_available_for_proc_work_ilocked(struct binder_thread
*thread
)
1043 return !thread
->transaction_stack
&&
1044 binder_worklist_empty_ilocked(&thread
->todo
) &&
1045 (thread
->looper
& (BINDER_LOOPER_STATE_ENTERED
|
1046 BINDER_LOOPER_STATE_REGISTERED
));
1049 static void binder_wakeup_poll_threads_ilocked(struct binder_proc
*proc
,
1053 struct binder_thread
*thread
;
1055 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
)) {
1056 thread
= rb_entry(n
, struct binder_thread
, rb_node
);
1057 if (thread
->looper
& BINDER_LOOPER_STATE_POLL
&&
1058 binder_available_for_proc_work_ilocked(thread
)) {
1060 wake_up_interruptible_sync(&thread
->wait
);
1062 wake_up_interruptible(&thread
->wait
);
1068 * binder_select_thread_ilocked() - selects a thread for doing proc work.
1069 * @proc: process to select a thread from
1071 * Note that calling this function moves the thread off the waiting_threads
1072 * list, so it can only be woken up by the caller of this function, or a
1073 * signal. Therefore, callers *should* always wake up the thread this function
1076 * Return: If there's a thread currently waiting for process work,
1077 * returns that thread. Otherwise returns NULL.
1079 static struct binder_thread
*
1080 binder_select_thread_ilocked(struct binder_proc
*proc
)
1082 struct binder_thread
*thread
;
1084 assert_spin_locked(&proc
->inner_lock
);
1085 thread
= list_first_entry_or_null(&proc
->waiting_threads
,
1086 struct binder_thread
,
1087 waiting_thread_node
);
1090 list_del_init(&thread
->waiting_thread_node
);
1096 * binder_wakeup_thread_ilocked() - wakes up a thread for doing proc work.
1097 * @proc: process to wake up a thread in
1098 * @thread: specific thread to wake-up (may be NULL)
1099 * @sync: whether to do a synchronous wake-up
1101 * This function wakes up a thread in the @proc process.
1102 * The caller may provide a specific thread to wake-up in
1103 * the @thread parameter. If @thread is NULL, this function
1104 * will wake up threads that have called poll().
1106 * Note that for this function to work as expected, callers
1107 * should first call binder_select_thread() to find a thread
1108 * to handle the work (if they don't have a thread already),
1109 * and pass the result into the @thread parameter.
1111 static void binder_wakeup_thread_ilocked(struct binder_proc
*proc
,
1112 struct binder_thread
*thread
,
1115 assert_spin_locked(&proc
->inner_lock
);
1119 wake_up_interruptible_sync(&thread
->wait
);
1121 wake_up_interruptible(&thread
->wait
);
1125 /* Didn't find a thread waiting for proc work; this can happen
1127 * 1. All threads are busy handling transactions
1128 * In that case, one of those threads should call back into
1129 * the kernel driver soon and pick up this work.
1130 * 2. Threads are using the (e)poll interface, in which case
1131 * they may be blocked on the waitqueue without having been
1132 * added to waiting_threads. For this case, we just iterate
1133 * over all threads not handling transaction work, and
1134 * wake them all up. We wake all because we don't know whether
1135 * a thread that called into (e)poll is handling non-binder
1138 binder_wakeup_poll_threads_ilocked(proc
, sync
);
1141 static void binder_wakeup_proc_ilocked(struct binder_proc
*proc
)
1143 struct binder_thread
*thread
= binder_select_thread_ilocked(proc
);
1145 binder_wakeup_thread_ilocked(proc
, thread
, /* sync = */false);
1148 static bool is_rt_policy(int policy
)
1150 return policy
== SCHED_FIFO
|| policy
== SCHED_RR
;
1153 static bool is_fair_policy(int policy
)
1155 return policy
== SCHED_NORMAL
|| policy
== SCHED_BATCH
;
1158 static bool binder_supported_policy(int policy
)
1160 return is_fair_policy(policy
) || is_rt_policy(policy
);
1163 static int to_userspace_prio(int policy
, int kernel_priority
)
1165 if (is_fair_policy(policy
))
1166 return PRIO_TO_NICE(kernel_priority
);
1168 return MAX_USER_RT_PRIO
- 1 - kernel_priority
;
1171 static int to_kernel_prio(int policy
, int user_priority
)
1173 if (is_fair_policy(policy
))
1174 return NICE_TO_PRIO(user_priority
);
1176 return MAX_USER_RT_PRIO
- 1 - user_priority
;
1179 static void binder_do_set_priority(struct task_struct
*task
,
1180 struct binder_priority desired
,
1183 int priority
; /* user-space prio value */
1185 unsigned int policy
= desired
.sched_policy
;
1187 if (task
->policy
== policy
&& task
->normal_prio
== desired
.prio
)
1190 has_cap_nice
= has_capability_noaudit(task
, CAP_SYS_NICE
);
1192 priority
= to_userspace_prio(policy
, desired
.prio
);
1194 if (verify
&& is_rt_policy(policy
) && !has_cap_nice
) {
1195 long max_rtprio
= task_rlimit(task
, RLIMIT_RTPRIO
);
1197 if (max_rtprio
== 0) {
1198 policy
= SCHED_NORMAL
;
1199 priority
= MIN_NICE
;
1200 } else if (priority
> max_rtprio
) {
1201 priority
= max_rtprio
;
1205 if (verify
&& is_fair_policy(policy
) && !has_cap_nice
) {
1206 long min_nice
= (MAX_NICE
- task_rlimit(task
, RLIMIT_NICE
) + 1);
1208 if (min_nice
> MAX_NICE
) {
1209 binder_user_error("%d RLIMIT_NICE not set\n",
1212 } else if (priority
< min_nice
) {
1213 priority
= min_nice
;
1217 if (policy
!= desired
.sched_policy
||
1218 to_kernel_prio(policy
, priority
) != desired
.prio
)
1219 binder_debug(BINDER_DEBUG_PRIORITY_CAP
,
1220 "%d: priority %d not allowed, using %d instead\n",
1221 task
->pid
, desired
.prio
,
1222 to_kernel_prio(policy
, priority
));
1224 trace_binder_set_priority(task
->tgid
, task
->pid
, task
->normal_prio
,
1225 to_kernel_prio(policy
, priority
),
1228 /* Set the actual priority */
1229 if (task
->policy
!= policy
|| is_rt_policy(policy
)) {
1230 struct sched_param params
;
1232 params
.sched_priority
= is_rt_policy(policy
) ? priority
: 0;
1234 sched_setscheduler_nocheck(task
,
1235 policy
| SCHED_RESET_ON_FORK
,
1238 if (is_fair_policy(policy
))
1239 set_user_nice(task
, priority
);
1242 static void binder_set_priority(struct task_struct
*task
,
1243 struct binder_priority desired
)
1245 binder_do_set_priority(task
, desired
, /* verify = */ true);
1248 static void binder_restore_priority(struct task_struct
*task
,
1249 struct binder_priority desired
)
1251 binder_do_set_priority(task
, desired
, /* verify = */ false);
1254 static void binder_transaction_priority(struct task_struct
*task
,
1255 struct binder_transaction
*t
,
1256 struct binder_priority node_prio
,
1259 struct binder_priority desired_prio
= t
->priority
;
1261 if (t
->set_priority_called
)
1264 t
->set_priority_called
= true;
1265 t
->saved_priority
.sched_policy
= task
->policy
;
1266 t
->saved_priority
.prio
= task
->normal_prio
;
1268 if (!inherit_rt
&& is_rt_policy(desired_prio
.sched_policy
)) {
1269 desired_prio
.prio
= NICE_TO_PRIO(0);
1270 desired_prio
.sched_policy
= SCHED_NORMAL
;
1273 if (node_prio
.prio
< t
->priority
.prio
||
1274 (node_prio
.prio
== t
->priority
.prio
&&
1275 node_prio
.sched_policy
== SCHED_FIFO
)) {
1277 * In case the minimum priority on the node is
1278 * higher (lower value), use that priority. If
1279 * the priority is the same, but the node uses
1280 * SCHED_FIFO, prefer SCHED_FIFO, since it can
1281 * run unbounded, unlike SCHED_RR.
1283 desired_prio
= node_prio
;
1286 binder_set_priority(task
, desired_prio
);
1289 static struct binder_node
*binder_get_node_ilocked(struct binder_proc
*proc
,
1290 binder_uintptr_t ptr
)
1292 struct rb_node
*n
= proc
->nodes
.rb_node
;
1293 struct binder_node
*node
;
1295 assert_spin_locked(&proc
->inner_lock
);
1298 node
= rb_entry(n
, struct binder_node
, rb_node
);
1300 if (ptr
< node
->ptr
)
1302 else if (ptr
> node
->ptr
)
1306 * take an implicit weak reference
1307 * to ensure node stays alive until
1308 * call to binder_put_node()
1310 binder_inc_node_tmpref_ilocked(node
);
1317 static struct binder_node
*binder_get_node(struct binder_proc
*proc
,
1318 binder_uintptr_t ptr
)
1320 struct binder_node
*node
;
1322 binder_inner_proc_lock(proc
);
1323 node
= binder_get_node_ilocked(proc
, ptr
);
1324 binder_inner_proc_unlock(proc
);
1328 static struct binder_node
*binder_init_node_ilocked(
1329 struct binder_proc
*proc
,
1330 struct binder_node
*new_node
,
1331 struct flat_binder_object
*fp
)
1333 struct rb_node
**p
= &proc
->nodes
.rb_node
;
1334 struct rb_node
*parent
= NULL
;
1335 struct binder_node
*node
;
1336 binder_uintptr_t ptr
= fp
? fp
->binder
: 0;
1337 binder_uintptr_t cookie
= fp
? fp
->cookie
: 0;
1338 __u32 flags
= fp
? fp
->flags
: 0;
1341 assert_spin_locked(&proc
->inner_lock
);
1346 node
= rb_entry(parent
, struct binder_node
, rb_node
);
1348 if (ptr
< node
->ptr
)
1350 else if (ptr
> node
->ptr
)
1351 p
= &(*p
)->rb_right
;
1354 * A matching node is already in
1355 * the rb tree. Abandon the init
1358 binder_inc_node_tmpref_ilocked(node
);
1363 binder_stats_created(BINDER_STAT_NODE
);
1365 rb_link_node(&node
->rb_node
, parent
, p
);
1366 rb_insert_color(&node
->rb_node
, &proc
->nodes
);
1367 node
->debug_id
= atomic_inc_return(&binder_last_id
);
1370 node
->cookie
= cookie
;
1371 node
->work
.type
= BINDER_WORK_NODE
;
1372 priority
= flags
& FLAT_BINDER_FLAG_PRIORITY_MASK
;
1373 node
->sched_policy
= (flags
& FLAT_BINDER_FLAG_SCHED_POLICY_MASK
) >>
1374 FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT
;
1375 node
->min_priority
= to_kernel_prio(node
->sched_policy
, priority
);
1376 node
->accept_fds
= !!(flags
& FLAT_BINDER_FLAG_ACCEPTS_FDS
);
1377 node
->inherit_rt
= !!(flags
& FLAT_BINDER_FLAG_INHERIT_RT
);
1378 node
->txn_security_ctx
= !!(flags
& FLAT_BINDER_FLAG_TXN_SECURITY_CTX
);
1379 spin_lock_init(&node
->lock
);
1380 INIT_LIST_HEAD(&node
->work
.entry
);
1381 INIT_LIST_HEAD(&node
->async_todo
);
1382 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1383 "%d:%d node %d u%016llx c%016llx created\n",
1384 proc
->pid
, current
->pid
, node
->debug_id
,
1385 (u64
)node
->ptr
, (u64
)node
->cookie
);
1390 static struct binder_node
*binder_new_node(struct binder_proc
*proc
,
1391 struct flat_binder_object
*fp
)
1393 struct binder_node
*node
;
1394 struct binder_node
*new_node
= kzalloc(sizeof(*node
), GFP_KERNEL
);
1398 binder_inner_proc_lock(proc
);
1399 node
= binder_init_node_ilocked(proc
, new_node
, fp
);
1400 binder_inner_proc_unlock(proc
);
1401 if (node
!= new_node
)
1403 * The node was already added by another thread
1410 static void binder_free_node(struct binder_node
*node
)
1413 binder_stats_deleted(BINDER_STAT_NODE
);
1416 static int binder_inc_node_nilocked(struct binder_node
*node
, int strong
,
1418 struct list_head
*target_list
)
1420 struct binder_proc
*proc
= node
->proc
;
1422 assert_spin_locked(&node
->lock
);
1424 assert_spin_locked(&proc
->inner_lock
);
1427 if (target_list
== NULL
&&
1428 node
->internal_strong_refs
== 0 &&
1430 node
== node
->proc
->context
->
1431 binder_context_mgr_node
&&
1432 node
->has_strong_ref
)) {
1433 pr_err("invalid inc strong node for %d\n",
1437 node
->internal_strong_refs
++;
1439 node
->local_strong_refs
++;
1440 if (!node
->has_strong_ref
&& target_list
) {
1441 binder_dequeue_work_ilocked(&node
->work
);
1443 * Note: this function is the only place where we queue
1444 * directly to a thread->todo without using the
1445 * corresponding binder_enqueue_thread_work() helper
1446 * functions; in this case it's ok to not set the
1447 * process_todo flag, since we know this node work will
1448 * always be followed by other work that starts queue
1449 * processing: in case of synchronous transactions, a
1450 * BR_REPLY or BR_ERROR; in case of oneway
1451 * transactions, a BR_TRANSACTION_COMPLETE.
1453 binder_enqueue_work_ilocked(&node
->work
, target_list
);
1457 node
->local_weak_refs
++;
1458 if (!node
->has_weak_ref
&& list_empty(&node
->work
.entry
)) {
1459 if (target_list
== NULL
) {
1460 pr_err("invalid inc weak node for %d\n",
1467 binder_enqueue_work_ilocked(&node
->work
, target_list
);
1473 static int binder_inc_node(struct binder_node
*node
, int strong
, int internal
,
1474 struct list_head
*target_list
)
1478 binder_node_inner_lock(node
);
1479 ret
= binder_inc_node_nilocked(node
, strong
, internal
, target_list
);
1480 binder_node_inner_unlock(node
);
1485 static bool binder_dec_node_nilocked(struct binder_node
*node
,
1486 int strong
, int internal
)
1488 struct binder_proc
*proc
= node
->proc
;
1490 assert_spin_locked(&node
->lock
);
1492 assert_spin_locked(&proc
->inner_lock
);
1495 node
->internal_strong_refs
--;
1497 node
->local_strong_refs
--;
1498 if (node
->local_strong_refs
|| node
->internal_strong_refs
)
1502 node
->local_weak_refs
--;
1503 if (node
->local_weak_refs
|| node
->tmp_refs
||
1504 !hlist_empty(&node
->refs
))
1508 if (proc
&& (node
->has_strong_ref
|| node
->has_weak_ref
)) {
1509 if (list_empty(&node
->work
.entry
)) {
1510 binder_enqueue_work_ilocked(&node
->work
, &proc
->todo
);
1511 binder_wakeup_proc_ilocked(proc
);
1514 if (hlist_empty(&node
->refs
) && !node
->local_strong_refs
&&
1515 !node
->local_weak_refs
&& !node
->tmp_refs
) {
1517 binder_dequeue_work_ilocked(&node
->work
);
1518 rb_erase(&node
->rb_node
, &proc
->nodes
);
1519 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1520 "refless node %d deleted\n",
1523 BUG_ON(!list_empty(&node
->work
.entry
));
1524 spin_lock(&binder_dead_nodes_lock
);
1526 * tmp_refs could have changed so
1529 if (node
->tmp_refs
) {
1530 spin_unlock(&binder_dead_nodes_lock
);
1533 hlist_del(&node
->dead_node
);
1534 spin_unlock(&binder_dead_nodes_lock
);
1535 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1536 "dead node %d deleted\n",
1545 static void binder_dec_node(struct binder_node
*node
, int strong
, int internal
)
1549 binder_node_inner_lock(node
);
1550 free_node
= binder_dec_node_nilocked(node
, strong
, internal
);
1551 binder_node_inner_unlock(node
);
1553 binder_free_node(node
);
1556 static void binder_inc_node_tmpref_ilocked(struct binder_node
*node
)
1559 * No call to binder_inc_node() is needed since we
1560 * don't need to inform userspace of any changes to
1567 * binder_inc_node_tmpref() - take a temporary reference on node
1568 * @node: node to reference
1570 * Take reference on node to prevent the node from being freed
1571 * while referenced only by a local variable. The inner lock is
1572 * needed to serialize with the node work on the queue (which
1573 * isn't needed after the node is dead). If the node is dead
1574 * (node->proc is NULL), use binder_dead_nodes_lock to protect
1575 * node->tmp_refs against dead-node-only cases where the node
1576 * lock cannot be acquired (eg traversing the dead node list to
1579 static void binder_inc_node_tmpref(struct binder_node
*node
)
1581 binder_node_lock(node
);
1583 binder_inner_proc_lock(node
->proc
);
1585 spin_lock(&binder_dead_nodes_lock
);
1586 binder_inc_node_tmpref_ilocked(node
);
1588 binder_inner_proc_unlock(node
->proc
);
1590 spin_unlock(&binder_dead_nodes_lock
);
1591 binder_node_unlock(node
);
1595 * binder_dec_node_tmpref() - remove a temporary reference on node
1596 * @node: node to reference
1598 * Release temporary reference on node taken via binder_inc_node_tmpref()
1600 static void binder_dec_node_tmpref(struct binder_node
*node
)
1604 binder_node_inner_lock(node
);
1606 spin_lock(&binder_dead_nodes_lock
);
1608 BUG_ON(node
->tmp_refs
< 0);
1610 spin_unlock(&binder_dead_nodes_lock
);
1612 * Call binder_dec_node() to check if all refcounts are 0
1613 * and cleanup is needed. Calling with strong=0 and internal=1
1614 * causes no actual reference to be released in binder_dec_node().
1615 * If that changes, a change is needed here too.
1617 free_node
= binder_dec_node_nilocked(node
, 0, 1);
1618 binder_node_inner_unlock(node
);
1620 binder_free_node(node
);
1623 static void binder_put_node(struct binder_node
*node
)
1625 binder_dec_node_tmpref(node
);
1628 static struct binder_ref
*binder_get_ref_olocked(struct binder_proc
*proc
,
1629 u32 desc
, bool need_strong_ref
)
1631 struct rb_node
*n
= proc
->refs_by_desc
.rb_node
;
1632 struct binder_ref
*ref
;
1635 ref
= rb_entry(n
, struct binder_ref
, rb_node_desc
);
1637 if (desc
< ref
->data
.desc
) {
1639 } else if (desc
> ref
->data
.desc
) {
1641 } else if (need_strong_ref
&& !ref
->data
.strong
) {
1642 binder_user_error("tried to use weak ref as strong ref\n");
1652 * binder_get_ref_for_node_olocked() - get the ref associated with given node
1653 * @proc: binder_proc that owns the ref
1654 * @node: binder_node of target
1655 * @new_ref: newly allocated binder_ref to be initialized or %NULL
1657 * Look up the ref for the given node and return it if it exists
1659 * If it doesn't exist and the caller provides a newly allocated
1660 * ref, initialize the fields of the newly allocated ref and insert
1661 * into the given proc rb_trees and node refs list.
1663 * Return: the ref for node. It is possible that another thread
1664 * allocated/initialized the ref first in which case the
1665 * returned ref would be different than the passed-in
1666 * new_ref. new_ref must be kfree'd by the caller in
1669 static struct binder_ref
*binder_get_ref_for_node_olocked(
1670 struct binder_proc
*proc
,
1671 struct binder_node
*node
,
1672 struct binder_ref
*new_ref
)
1674 struct binder_context
*context
= proc
->context
;
1675 struct rb_node
**p
= &proc
->refs_by_node
.rb_node
;
1676 struct rb_node
*parent
= NULL
;
1677 struct binder_ref
*ref
;
1682 ref
= rb_entry(parent
, struct binder_ref
, rb_node_node
);
1684 if (node
< ref
->node
)
1686 else if (node
> ref
->node
)
1687 p
= &(*p
)->rb_right
;
1694 binder_stats_created(BINDER_STAT_REF
);
1695 new_ref
->data
.debug_id
= atomic_inc_return(&binder_last_id
);
1696 new_ref
->proc
= proc
;
1697 new_ref
->node
= node
;
1698 rb_link_node(&new_ref
->rb_node_node
, parent
, p
);
1699 rb_insert_color(&new_ref
->rb_node_node
, &proc
->refs_by_node
);
1701 new_ref
->data
.desc
= (node
== context
->binder_context_mgr_node
) ? 0 : 1;
1702 for (n
= rb_first(&proc
->refs_by_desc
); n
!= NULL
; n
= rb_next(n
)) {
1703 ref
= rb_entry(n
, struct binder_ref
, rb_node_desc
);
1704 if (ref
->data
.desc
> new_ref
->data
.desc
)
1706 new_ref
->data
.desc
= ref
->data
.desc
+ 1;
1709 p
= &proc
->refs_by_desc
.rb_node
;
1712 ref
= rb_entry(parent
, struct binder_ref
, rb_node_desc
);
1714 if (new_ref
->data
.desc
< ref
->data
.desc
)
1716 else if (new_ref
->data
.desc
> ref
->data
.desc
)
1717 p
= &(*p
)->rb_right
;
1721 rb_link_node(&new_ref
->rb_node_desc
, parent
, p
);
1722 rb_insert_color(&new_ref
->rb_node_desc
, &proc
->refs_by_desc
);
1724 binder_node_lock(node
);
1725 hlist_add_head(&new_ref
->node_entry
, &node
->refs
);
1727 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1728 "%d new ref %d desc %d for node %d\n",
1729 proc
->pid
, new_ref
->data
.debug_id
, new_ref
->data
.desc
,
1731 binder_node_unlock(node
);
1735 static void binder_cleanup_ref_olocked(struct binder_ref
*ref
)
1737 bool delete_node
= false;
1739 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1740 "%d delete ref %d desc %d for node %d\n",
1741 ref
->proc
->pid
, ref
->data
.debug_id
, ref
->data
.desc
,
1742 ref
->node
->debug_id
);
1744 rb_erase(&ref
->rb_node_desc
, &ref
->proc
->refs_by_desc
);
1745 rb_erase(&ref
->rb_node_node
, &ref
->proc
->refs_by_node
);
1747 binder_node_inner_lock(ref
->node
);
1748 if (ref
->data
.strong
)
1749 binder_dec_node_nilocked(ref
->node
, 1, 1);
1751 hlist_del(&ref
->node_entry
);
1752 delete_node
= binder_dec_node_nilocked(ref
->node
, 0, 1);
1753 binder_node_inner_unlock(ref
->node
);
1755 * Clear ref->node unless we want the caller to free the node
1759 * The caller uses ref->node to determine
1760 * whether the node needs to be freed. Clear
1761 * it since the node is still alive.
1767 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
1768 "%d delete ref %d desc %d has death notification\n",
1769 ref
->proc
->pid
, ref
->data
.debug_id
,
1771 binder_dequeue_work(ref
->proc
, &ref
->death
->work
);
1772 binder_stats_deleted(BINDER_STAT_DEATH
);
1774 binder_stats_deleted(BINDER_STAT_REF
);
1778 * binder_inc_ref_olocked() - increment the ref for given handle
1779 * @ref: ref to be incremented
1780 * @strong: if true, strong increment, else weak
1781 * @target_list: list to queue node work on
1783 * Increment the ref. @ref->proc->outer_lock must be held on entry
1785 * Return: 0, if successful, else errno
1787 static int binder_inc_ref_olocked(struct binder_ref
*ref
, int strong
,
1788 struct list_head
*target_list
)
1793 if (ref
->data
.strong
== 0) {
1794 ret
= binder_inc_node(ref
->node
, 1, 1, target_list
);
1800 if (ref
->data
.weak
== 0) {
1801 ret
= binder_inc_node(ref
->node
, 0, 1, target_list
);
1811 * binder_dec_ref() - dec the ref for given handle
1812 * @ref: ref to be decremented
1813 * @strong: if true, strong decrement, else weak
1815 * Decrement the ref.
1817 * Return: true if ref is cleaned up and ready to be freed
1819 static bool binder_dec_ref_olocked(struct binder_ref
*ref
, int strong
)
1822 if (ref
->data
.strong
== 0) {
1823 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
1824 ref
->proc
->pid
, ref
->data
.debug_id
,
1825 ref
->data
.desc
, ref
->data
.strong
,
1830 if (ref
->data
.strong
== 0)
1831 binder_dec_node(ref
->node
, strong
, 1);
1833 if (ref
->data
.weak
== 0) {
1834 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
1835 ref
->proc
->pid
, ref
->data
.debug_id
,
1836 ref
->data
.desc
, ref
->data
.strong
,
1842 if (ref
->data
.strong
== 0 && ref
->data
.weak
== 0) {
1843 binder_cleanup_ref_olocked(ref
);
1850 * binder_get_node_from_ref() - get the node from the given proc/desc
1851 * @proc: proc containing the ref
1852 * @desc: the handle associated with the ref
1853 * @need_strong_ref: if true, only return node if ref is strong
1854 * @rdata: the id/refcount data for the ref
1856 * Given a proc and ref handle, return the associated binder_node
1858 * Return: a binder_node or NULL if not found or not strong when strong required
1860 static struct binder_node
*binder_get_node_from_ref(
1861 struct binder_proc
*proc
,
1862 u32 desc
, bool need_strong_ref
,
1863 struct binder_ref_data
*rdata
)
1865 struct binder_node
*node
;
1866 struct binder_ref
*ref
;
1868 binder_proc_lock(proc
);
1869 ref
= binder_get_ref_olocked(proc
, desc
, need_strong_ref
);
1874 * Take an implicit reference on the node to ensure
1875 * it stays alive until the call to binder_put_node()
1877 binder_inc_node_tmpref(node
);
1880 binder_proc_unlock(proc
);
1885 binder_proc_unlock(proc
);
1890 * binder_free_ref() - free the binder_ref
1893 * Free the binder_ref. Free the binder_node indicated by ref->node
1894 * (if non-NULL) and the binder_ref_death indicated by ref->death.
1896 static void binder_free_ref(struct binder_ref
*ref
)
1899 binder_free_node(ref
->node
);
1905 * binder_update_ref_for_handle() - inc/dec the ref for given handle
1906 * @proc: proc containing the ref
1907 * @desc: the handle associated with the ref
1908 * @increment: true=inc reference, false=dec reference
1909 * @strong: true=strong reference, false=weak reference
1910 * @rdata: the id/refcount data for the ref
1912 * Given a proc and ref handle, increment or decrement the ref
1913 * according to "increment" arg.
1915 * Return: 0 if successful, else errno
1917 static int binder_update_ref_for_handle(struct binder_proc
*proc
,
1918 uint32_t desc
, bool increment
, bool strong
,
1919 struct binder_ref_data
*rdata
)
1922 struct binder_ref
*ref
;
1923 bool delete_ref
= false;
1925 binder_proc_lock(proc
);
1926 ref
= binder_get_ref_olocked(proc
, desc
, strong
);
1932 ret
= binder_inc_ref_olocked(ref
, strong
, NULL
);
1934 delete_ref
= binder_dec_ref_olocked(ref
, strong
);
1938 binder_proc_unlock(proc
);
1941 binder_free_ref(ref
);
1945 binder_proc_unlock(proc
);
1950 * binder_dec_ref_for_handle() - dec the ref for given handle
1951 * @proc: proc containing the ref
1952 * @desc: the handle associated with the ref
1953 * @strong: true=strong reference, false=weak reference
1954 * @rdata: the id/refcount data for the ref
1956 * Just calls binder_update_ref_for_handle() to decrement the ref.
1958 * Return: 0 if successful, else errno
1960 static int binder_dec_ref_for_handle(struct binder_proc
*proc
,
1961 uint32_t desc
, bool strong
, struct binder_ref_data
*rdata
)
1963 return binder_update_ref_for_handle(proc
, desc
, false, strong
, rdata
);
1968 * binder_inc_ref_for_node() - increment the ref for given proc/node
1969 * @proc: proc containing the ref
1970 * @node: target node
1971 * @strong: true=strong reference, false=weak reference
1972 * @target_list: worklist to use if node is incremented
1973 * @rdata: the id/refcount data for the ref
1975 * Given a proc and node, increment the ref. Create the ref if it
1976 * doesn't already exist
1978 * Return: 0 if successful, else errno
1980 static int binder_inc_ref_for_node(struct binder_proc
*proc
,
1981 struct binder_node
*node
,
1983 struct list_head
*target_list
,
1984 struct binder_ref_data
*rdata
)
1986 struct binder_ref
*ref
;
1987 struct binder_ref
*new_ref
= NULL
;
1990 binder_proc_lock(proc
);
1991 ref
= binder_get_ref_for_node_olocked(proc
, node
, NULL
);
1993 binder_proc_unlock(proc
);
1994 new_ref
= kzalloc(sizeof(*ref
), GFP_KERNEL
);
1997 binder_proc_lock(proc
);
1998 ref
= binder_get_ref_for_node_olocked(proc
, node
, new_ref
);
2000 ret
= binder_inc_ref_olocked(ref
, strong
, target_list
);
2002 binder_proc_unlock(proc
);
2003 if (new_ref
&& ref
!= new_ref
)
2005 * Another thread created the ref first so
2006 * free the one we allocated
2012 static void binder_pop_transaction_ilocked(struct binder_thread
*target_thread
,
2013 struct binder_transaction
*t
)
2015 BUG_ON(!target_thread
);
2016 assert_spin_locked(&target_thread
->proc
->inner_lock
);
2017 BUG_ON(target_thread
->transaction_stack
!= t
);
2018 BUG_ON(target_thread
->transaction_stack
->from
!= target_thread
);
2019 target_thread
->transaction_stack
=
2020 target_thread
->transaction_stack
->from_parent
;
2025 * binder_thread_dec_tmpref() - decrement thread->tmp_ref
2026 * @thread: thread to decrement
2028 * A thread needs to be kept alive while being used to create or
2029 * handle a transaction. binder_get_txn_from() is used to safely
2030 * extract t->from from a binder_transaction and keep the thread
2031 * indicated by t->from from being freed. When done with that
2032 * binder_thread, this function is called to decrement the
2033 * tmp_ref and free if appropriate (thread has been released
2034 * and no transaction being processed by the driver)
2036 static void binder_thread_dec_tmpref(struct binder_thread
*thread
)
2039 * atomic is used to protect the counter value while
2040 * it cannot reach zero or thread->is_dead is false
2042 binder_inner_proc_lock(thread
->proc
);
2043 atomic_dec(&thread
->tmp_ref
);
2044 if (thread
->is_dead
&& !atomic_read(&thread
->tmp_ref
)) {
2045 binder_inner_proc_unlock(thread
->proc
);
2046 binder_free_thread(thread
);
2049 binder_inner_proc_unlock(thread
->proc
);
2053 * binder_proc_dec_tmpref() - decrement proc->tmp_ref
2054 * @proc: proc to decrement
2056 * A binder_proc needs to be kept alive while being used to create or
2057 * handle a transaction. proc->tmp_ref is incremented when
2058 * creating a new transaction or the binder_proc is currently in-use
2059 * by threads that are being released. When done with the binder_proc,
2060 * this function is called to decrement the counter and free the
2061 * proc if appropriate (proc has been released, all threads have
2062 * been released and not currenly in-use to process a transaction).
2064 static void binder_proc_dec_tmpref(struct binder_proc
*proc
)
2066 binder_inner_proc_lock(proc
);
2068 if (proc
->is_dead
&& RB_EMPTY_ROOT(&proc
->threads
) &&
2070 binder_inner_proc_unlock(proc
);
2071 binder_free_proc(proc
);
2074 binder_inner_proc_unlock(proc
);
2078 * binder_get_txn_from() - safely extract the "from" thread in transaction
2079 * @t: binder transaction for t->from
2081 * Atomically return the "from" thread and increment the tmp_ref
2082 * count for the thread to ensure it stays alive until
2083 * binder_thread_dec_tmpref() is called.
2085 * Return: the value of t->from
2087 static struct binder_thread
*binder_get_txn_from(
2088 struct binder_transaction
*t
)
2090 struct binder_thread
*from
;
2092 spin_lock(&t
->lock
);
2095 atomic_inc(&from
->tmp_ref
);
2096 spin_unlock(&t
->lock
);
2101 * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock
2102 * @t: binder transaction for t->from
2104 * Same as binder_get_txn_from() except it also acquires the proc->inner_lock
2105 * to guarantee that the thread cannot be released while operating on it.
2106 * The caller must call binder_inner_proc_unlock() to release the inner lock
2107 * as well as call binder_dec_thread_txn() to release the reference.
2109 * Return: the value of t->from
2111 static struct binder_thread
*binder_get_txn_from_and_acq_inner(
2112 struct binder_transaction
*t
)
2114 struct binder_thread
*from
;
2116 from
= binder_get_txn_from(t
);
2119 binder_inner_proc_lock(from
->proc
);
2121 BUG_ON(from
!= t
->from
);
2124 binder_inner_proc_unlock(from
->proc
);
2125 binder_thread_dec_tmpref(from
);
2129 static void binder_free_transaction(struct binder_transaction
*t
)
2132 t
->buffer
->transaction
= NULL
;
2134 binder_stats_deleted(BINDER_STAT_TRANSACTION
);
2137 static void binder_send_failed_reply(struct binder_transaction
*t
,
2138 uint32_t error_code
)
2140 struct binder_thread
*target_thread
;
2141 struct binder_transaction
*next
;
2143 BUG_ON(t
->flags
& TF_ONE_WAY
);
2145 target_thread
= binder_get_txn_from_and_acq_inner(t
);
2146 if (target_thread
) {
2147 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
2148 "send failed reply for transaction %d to %d:%d\n",
2150 target_thread
->proc
->pid
,
2151 target_thread
->pid
);
2153 binder_pop_transaction_ilocked(target_thread
, t
);
2154 if (target_thread
->reply_error
.cmd
== BR_OK
) {
2155 target_thread
->reply_error
.cmd
= error_code
;
2156 binder_enqueue_thread_work_ilocked(
2158 &target_thread
->reply_error
.work
);
2159 wake_up_interruptible(&target_thread
->wait
);
2161 WARN(1, "Unexpected reply error: %u\n",
2162 target_thread
->reply_error
.cmd
);
2164 binder_inner_proc_unlock(target_thread
->proc
);
2165 binder_thread_dec_tmpref(target_thread
);
2166 binder_free_transaction(t
);
2169 next
= t
->from_parent
;
2171 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
2172 "send failed reply for transaction %d, target dead\n",
2175 binder_free_transaction(t
);
2177 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
2178 "reply failed, no target thread at root\n");
2182 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
2183 "reply failed, no target thread -- retry %d\n",
2189 * binder_cleanup_transaction() - cleans up undelivered transaction
2190 * @t: transaction that needs to be cleaned up
2191 * @reason: reason the transaction wasn't delivered
2192 * @error_code: error to return to caller (if synchronous call)
2194 static void binder_cleanup_transaction(struct binder_transaction
*t
,
2196 uint32_t error_code
)
2198 if (t
->buffer
->target_node
&& !(t
->flags
& TF_ONE_WAY
)) {
2199 binder_send_failed_reply(t
, error_code
);
2201 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
2202 "undelivered transaction %d, %s\n",
2203 t
->debug_id
, reason
);
2204 binder_free_transaction(t
);
2209 * binder_validate_object() - checks for a valid metadata object in a buffer.
2210 * @buffer: binder_buffer that we're parsing.
2211 * @offset: offset in the buffer at which to validate an object.
2213 * Return: If there's a valid metadata object at @offset in @buffer, the
2214 * size of that object. Otherwise, it returns zero.
2216 static size_t binder_validate_object(struct binder_buffer
*buffer
, u64 offset
)
2218 /* Check if we can read a header first */
2219 struct binder_object_header
*hdr
;
2220 size_t object_size
= 0;
2222 if (offset
> buffer
->data_size
- sizeof(*hdr
) ||
2223 buffer
->data_size
< sizeof(*hdr
) ||
2224 !IS_ALIGNED(offset
, sizeof(u32
)))
2227 /* Ok, now see if we can read a complete object. */
2228 hdr
= (struct binder_object_header
*)(buffer
->data
+ offset
);
2229 switch (hdr
->type
) {
2230 case BINDER_TYPE_BINDER
:
2231 case BINDER_TYPE_WEAK_BINDER
:
2232 case BINDER_TYPE_HANDLE
:
2233 case BINDER_TYPE_WEAK_HANDLE
:
2234 object_size
= sizeof(struct flat_binder_object
);
2236 case BINDER_TYPE_FD
:
2237 object_size
= sizeof(struct binder_fd_object
);
2239 case BINDER_TYPE_PTR
:
2240 object_size
= sizeof(struct binder_buffer_object
);
2242 case BINDER_TYPE_FDA
:
2243 object_size
= sizeof(struct binder_fd_array_object
);
2248 if (offset
<= buffer
->data_size
- object_size
&&
2249 buffer
->data_size
>= object_size
)
2256 * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
2257 * @b: binder_buffer containing the object
2258 * @index: index in offset array at which the binder_buffer_object is
2260 * @start: points to the start of the offset array
2261 * @num_valid: the number of valid offsets in the offset array
2263 * Return: If @index is within the valid range of the offset array
2264 * described by @start and @num_valid, and if there's a valid
2265 * binder_buffer_object at the offset found in index @index
2266 * of the offset array, that object is returned. Otherwise,
2267 * %NULL is returned.
2268 * Note that the offset found in index @index itself is not
2269 * verified; this function assumes that @num_valid elements
2270 * from @start were previously verified to have valid offsets.
2272 static struct binder_buffer_object
*binder_validate_ptr(struct binder_buffer
*b
,
2273 binder_size_t index
,
2274 binder_size_t
*start
,
2275 binder_size_t num_valid
)
2277 struct binder_buffer_object
*buffer_obj
;
2278 binder_size_t
*offp
;
2280 if (index
>= num_valid
)
2283 offp
= start
+ index
;
2284 buffer_obj
= (struct binder_buffer_object
*)(b
->data
+ *offp
);
2285 if (buffer_obj
->hdr
.type
!= BINDER_TYPE_PTR
)
2292 * binder_validate_fixup() - validates pointer/fd fixups happen in order.
2293 * @b: transaction buffer
2294 * @objects_start start of objects buffer
2295 * @buffer: binder_buffer_object in which to fix up
2296 * @offset: start offset in @buffer to fix up
2297 * @last_obj: last binder_buffer_object that we fixed up in
2298 * @last_min_offset: minimum fixup offset in @last_obj
2300 * Return: %true if a fixup in buffer @buffer at offset @offset is
2303 * For safety reasons, we only allow fixups inside a buffer to happen
2304 * at increasing offsets; additionally, we only allow fixup on the last
2305 * buffer object that was verified, or one of its parents.
2307 * Example of what is allowed:
2310 * B (parent = A, offset = 0)
2311 * C (parent = A, offset = 16)
2312 * D (parent = C, offset = 0)
2313 * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
2315 * Examples of what is not allowed:
2317 * Decreasing offsets within the same parent:
2319 * C (parent = A, offset = 16)
2320 * B (parent = A, offset = 0) // decreasing offset within A
2322 * Referring to a parent that wasn't the last object or any of its parents:
2324 * B (parent = A, offset = 0)
2325 * C (parent = A, offset = 0)
2326 * C (parent = A, offset = 16)
2327 * D (parent = B, offset = 0) // B is not A or any of A's parents
2329 static bool binder_validate_fixup(struct binder_buffer
*b
,
2330 binder_size_t
*objects_start
,
2331 struct binder_buffer_object
*buffer
,
2332 binder_size_t fixup_offset
,
2333 struct binder_buffer_object
*last_obj
,
2334 binder_size_t last_min_offset
)
2337 /* Nothing to fix up in */
2341 while (last_obj
!= buffer
) {
2343 * Safe to retrieve the parent of last_obj, since it
2344 * was already previously verified by the driver.
2346 if ((last_obj
->flags
& BINDER_BUFFER_FLAG_HAS_PARENT
) == 0)
2348 last_min_offset
= last_obj
->parent_offset
+ sizeof(uintptr_t);
2349 last_obj
= (struct binder_buffer_object
*)
2350 (b
->data
+ *(objects_start
+ last_obj
->parent
));
2352 return (fixup_offset
>= last_min_offset
);
2355 static void binder_transaction_buffer_release(struct binder_proc
*proc
,
2356 struct binder_buffer
*buffer
,
2357 binder_size_t
*failed_at
)
2359 binder_size_t
*offp
, *off_start
, *off_end
;
2360 int debug_id
= buffer
->debug_id
;
2362 binder_debug(BINDER_DEBUG_TRANSACTION
,
2363 "%d buffer release %d, size %zd-%zd, failed at %p\n",
2364 proc
->pid
, buffer
->debug_id
,
2365 buffer
->data_size
, buffer
->offsets_size
, failed_at
);
2367 if (buffer
->target_node
)
2368 binder_dec_node(buffer
->target_node
, 1, 0);
2370 off_start
= (binder_size_t
*)(buffer
->data
+
2371 ALIGN(buffer
->data_size
, sizeof(void *)));
2373 off_end
= failed_at
;
2375 off_end
= (void *)off_start
+ buffer
->offsets_size
;
2376 for (offp
= off_start
; offp
< off_end
; offp
++) {
2377 struct binder_object_header
*hdr
;
2378 size_t object_size
= binder_validate_object(buffer
, *offp
);
2380 if (object_size
== 0) {
2381 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
2382 debug_id
, (u64
)*offp
, buffer
->data_size
);
2385 hdr
= (struct binder_object_header
*)(buffer
->data
+ *offp
);
2386 switch (hdr
->type
) {
2387 case BINDER_TYPE_BINDER
:
2388 case BINDER_TYPE_WEAK_BINDER
: {
2389 struct flat_binder_object
*fp
;
2390 struct binder_node
*node
;
2392 fp
= to_flat_binder_object(hdr
);
2393 node
= binder_get_node(proc
, fp
->binder
);
2395 pr_err("transaction release %d bad node %016llx\n",
2396 debug_id
, (u64
)fp
->binder
);
2399 binder_debug(BINDER_DEBUG_TRANSACTION
,
2400 " node %d u%016llx\n",
2401 node
->debug_id
, (u64
)node
->ptr
);
2402 binder_dec_node(node
, hdr
->type
== BINDER_TYPE_BINDER
,
2404 binder_put_node(node
);
2406 case BINDER_TYPE_HANDLE
:
2407 case BINDER_TYPE_WEAK_HANDLE
: {
2408 struct flat_binder_object
*fp
;
2409 struct binder_ref_data rdata
;
2412 fp
= to_flat_binder_object(hdr
);
2413 ret
= binder_dec_ref_for_handle(proc
, fp
->handle
,
2414 hdr
->type
== BINDER_TYPE_HANDLE
, &rdata
);
2417 pr_err("transaction release %d bad handle %d, ret = %d\n",
2418 debug_id
, fp
->handle
, ret
);
2421 binder_debug(BINDER_DEBUG_TRANSACTION
,
2422 " ref %d desc %d\n",
2423 rdata
.debug_id
, rdata
.desc
);
2426 case BINDER_TYPE_FD
: {
2427 struct binder_fd_object
*fp
= to_binder_fd_object(hdr
);
2429 binder_debug(BINDER_DEBUG_TRANSACTION
,
2430 " fd %d\n", fp
->fd
);
2432 task_close_fd(proc
, fp
->fd
);
2434 case BINDER_TYPE_PTR
:
2436 * Nothing to do here, this will get cleaned up when the
2437 * transaction buffer gets freed
2440 case BINDER_TYPE_FDA
: {
2441 struct binder_fd_array_object
*fda
;
2442 struct binder_buffer_object
*parent
;
2443 uintptr_t parent_buffer
;
2446 binder_size_t fd_buf_size
;
2448 fda
= to_binder_fd_array_object(hdr
);
2449 parent
= binder_validate_ptr(buffer
, fda
->parent
,
2453 pr_err("transaction release %d bad parent offset",
2458 * Since the parent was already fixed up, convert it
2459 * back to kernel address space to access it
2461 parent_buffer
= parent
->buffer
-
2462 binder_alloc_get_user_buffer_offset(
2465 fd_buf_size
= sizeof(u32
) * fda
->num_fds
;
2466 if (fda
->num_fds
>= SIZE_MAX
/ sizeof(u32
)) {
2467 pr_err("transaction release %d invalid number of fds (%lld)\n",
2468 debug_id
, (u64
)fda
->num_fds
);
2471 if (fd_buf_size
> parent
->length
||
2472 fda
->parent_offset
> parent
->length
- fd_buf_size
) {
2473 /* No space for all file descriptors here. */
2474 pr_err("transaction release %d not enough space for %lld fds in buffer\n",
2475 debug_id
, (u64
)fda
->num_fds
);
2478 fd_array
= (u32
*)(parent_buffer
+ (uintptr_t)fda
->parent_offset
);
2479 for (fd_index
= 0; fd_index
< fda
->num_fds
; fd_index
++)
2480 task_close_fd(proc
, fd_array
[fd_index
]);
2483 pr_err("transaction release %d bad object type %x\n",
2484 debug_id
, hdr
->type
);
2490 static int binder_translate_binder(struct flat_binder_object
*fp
,
2491 struct binder_transaction
*t
,
2492 struct binder_thread
*thread
)
2494 struct binder_node
*node
;
2495 struct binder_proc
*proc
= thread
->proc
;
2496 struct binder_proc
*target_proc
= t
->to_proc
;
2497 struct binder_ref_data rdata
;
2500 node
= binder_get_node(proc
, fp
->binder
);
2502 node
= binder_new_node(proc
, fp
);
2506 if (fp
->cookie
!= node
->cookie
) {
2507 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
2508 proc
->pid
, thread
->pid
, (u64
)fp
->binder
,
2509 node
->debug_id
, (u64
)fp
->cookie
,
2514 if (security_binder_transfer_binder(proc
->tsk
, target_proc
->tsk
)) {
2519 ret
= binder_inc_ref_for_node(target_proc
, node
,
2520 fp
->hdr
.type
== BINDER_TYPE_BINDER
,
2521 &thread
->todo
, &rdata
);
2525 if (fp
->hdr
.type
== BINDER_TYPE_BINDER
)
2526 fp
->hdr
.type
= BINDER_TYPE_HANDLE
;
2528 fp
->hdr
.type
= BINDER_TYPE_WEAK_HANDLE
;
2530 fp
->handle
= rdata
.desc
;
2533 trace_binder_transaction_node_to_ref(t
, node
, &rdata
);
2534 binder_debug(BINDER_DEBUG_TRANSACTION
,
2535 " node %d u%016llx -> ref %d desc %d\n",
2536 node
->debug_id
, (u64
)node
->ptr
,
2537 rdata
.debug_id
, rdata
.desc
);
2539 binder_put_node(node
);
2543 static int binder_translate_handle(struct flat_binder_object
*fp
,
2544 struct binder_transaction
*t
,
2545 struct binder_thread
*thread
)
2547 struct binder_proc
*proc
= thread
->proc
;
2548 struct binder_proc
*target_proc
= t
->to_proc
;
2549 struct binder_node
*node
;
2550 struct binder_ref_data src_rdata
;
2553 node
= binder_get_node_from_ref(proc
, fp
->handle
,
2554 fp
->hdr
.type
== BINDER_TYPE_HANDLE
, &src_rdata
);
2556 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
2557 proc
->pid
, thread
->pid
, fp
->handle
);
2560 if (security_binder_transfer_binder(proc
->tsk
, target_proc
->tsk
)) {
2565 binder_node_lock(node
);
2566 if (node
->proc
== target_proc
) {
2567 if (fp
->hdr
.type
== BINDER_TYPE_HANDLE
)
2568 fp
->hdr
.type
= BINDER_TYPE_BINDER
;
2570 fp
->hdr
.type
= BINDER_TYPE_WEAK_BINDER
;
2571 fp
->binder
= node
->ptr
;
2572 fp
->cookie
= node
->cookie
;
2574 binder_inner_proc_lock(node
->proc
);
2575 binder_inc_node_nilocked(node
,
2576 fp
->hdr
.type
== BINDER_TYPE_BINDER
,
2579 binder_inner_proc_unlock(node
->proc
);
2580 trace_binder_transaction_ref_to_node(t
, node
, &src_rdata
);
2581 binder_debug(BINDER_DEBUG_TRANSACTION
,
2582 " ref %d desc %d -> node %d u%016llx\n",
2583 src_rdata
.debug_id
, src_rdata
.desc
, node
->debug_id
,
2585 binder_node_unlock(node
);
2587 struct binder_ref_data dest_rdata
;
2589 binder_node_unlock(node
);
2590 ret
= binder_inc_ref_for_node(target_proc
, node
,
2591 fp
->hdr
.type
== BINDER_TYPE_HANDLE
,
2597 fp
->handle
= dest_rdata
.desc
;
2599 trace_binder_transaction_ref_to_ref(t
, node
, &src_rdata
,
2601 binder_debug(BINDER_DEBUG_TRANSACTION
,
2602 " ref %d desc %d -> ref %d desc %d (node %d)\n",
2603 src_rdata
.debug_id
, src_rdata
.desc
,
2604 dest_rdata
.debug_id
, dest_rdata
.desc
,
2608 binder_put_node(node
);
2612 static int binder_translate_fd(int fd
,
2613 struct binder_transaction
*t
,
2614 struct binder_thread
*thread
,
2615 struct binder_transaction
*in_reply_to
)
2617 struct binder_proc
*proc
= thread
->proc
;
2618 struct binder_proc
*target_proc
= t
->to_proc
;
2622 bool target_allows_fd
;
2625 target_allows_fd
= !!(in_reply_to
->flags
& TF_ACCEPT_FDS
);
2627 target_allows_fd
= t
->buffer
->target_node
->accept_fds
;
2628 if (!target_allows_fd
) {
2629 binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
2630 proc
->pid
, thread
->pid
,
2631 in_reply_to
? "reply" : "transaction",
2634 goto err_fd_not_accepted
;
2639 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
2640 proc
->pid
, thread
->pid
, fd
);
2644 ret
= security_binder_transfer_file(proc
->tsk
, target_proc
->tsk
, file
);
2650 target_fd
= task_get_unused_fd_flags(target_proc
, O_CLOEXEC
);
2651 if (target_fd
< 0) {
2653 goto err_get_unused_fd
;
2655 task_fd_install(target_proc
, target_fd
, file
);
2656 trace_binder_transaction_fd(t
, fd
, target_fd
);
2657 binder_debug(BINDER_DEBUG_TRANSACTION
, " fd %d -> %d\n",
2666 err_fd_not_accepted
:
2670 static int binder_translate_fd_array(struct binder_fd_array_object
*fda
,
2671 struct binder_buffer_object
*parent
,
2672 struct binder_transaction
*t
,
2673 struct binder_thread
*thread
,
2674 struct binder_transaction
*in_reply_to
)
2676 binder_size_t fdi
, fd_buf_size
, num_installed_fds
;
2678 uintptr_t parent_buffer
;
2680 struct binder_proc
*proc
= thread
->proc
;
2681 struct binder_proc
*target_proc
= t
->to_proc
;
2683 fd_buf_size
= sizeof(u32
) * fda
->num_fds
;
2684 if (fda
->num_fds
>= SIZE_MAX
/ sizeof(u32
)) {
2685 binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
2686 proc
->pid
, thread
->pid
, (u64
)fda
->num_fds
);
2689 if (fd_buf_size
> parent
->length
||
2690 fda
->parent_offset
> parent
->length
- fd_buf_size
) {
2691 /* No space for all file descriptors here. */
2692 binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
2693 proc
->pid
, thread
->pid
, (u64
)fda
->num_fds
);
2697 * Since the parent was already fixed up, convert it
2698 * back to the kernel address space to access it
2700 parent_buffer
= parent
->buffer
-
2701 binder_alloc_get_user_buffer_offset(&target_proc
->alloc
);
2702 fd_array
= (u32
*)(parent_buffer
+ (uintptr_t)fda
->parent_offset
);
2703 if (!IS_ALIGNED((unsigned long)fd_array
, sizeof(u32
))) {
2704 binder_user_error("%d:%d parent offset not aligned correctly.\n",
2705 proc
->pid
, thread
->pid
);
2708 for (fdi
= 0; fdi
< fda
->num_fds
; fdi
++) {
2709 target_fd
= binder_translate_fd(fd_array
[fdi
], t
, thread
,
2712 goto err_translate_fd_failed
;
2713 fd_array
[fdi
] = target_fd
;
2717 err_translate_fd_failed
:
2719 * Failed to allocate fd or security error, free fds
2722 num_installed_fds
= fdi
;
2723 for (fdi
= 0; fdi
< num_installed_fds
; fdi
++)
2724 task_close_fd(target_proc
, fd_array
[fdi
]);
2728 static int binder_fixup_parent(struct binder_transaction
*t
,
2729 struct binder_thread
*thread
,
2730 struct binder_buffer_object
*bp
,
2731 binder_size_t
*off_start
,
2732 binder_size_t num_valid
,
2733 struct binder_buffer_object
*last_fixup_obj
,
2734 binder_size_t last_fixup_min_off
)
2736 struct binder_buffer_object
*parent
;
2738 struct binder_buffer
*b
= t
->buffer
;
2739 struct binder_proc
*proc
= thread
->proc
;
2740 struct binder_proc
*target_proc
= t
->to_proc
;
2742 if (!(bp
->flags
& BINDER_BUFFER_FLAG_HAS_PARENT
))
2745 parent
= binder_validate_ptr(b
, bp
->parent
, off_start
, num_valid
);
2747 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
2748 proc
->pid
, thread
->pid
);
2752 if (!binder_validate_fixup(b
, off_start
,
2753 parent
, bp
->parent_offset
,
2755 last_fixup_min_off
)) {
2756 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
2757 proc
->pid
, thread
->pid
);
2761 if (parent
->length
< sizeof(binder_uintptr_t
) ||
2762 bp
->parent_offset
> parent
->length
- sizeof(binder_uintptr_t
)) {
2763 /* No space for a pointer here! */
2764 binder_user_error("%d:%d got transaction with invalid parent offset\n",
2765 proc
->pid
, thread
->pid
);
2768 parent_buffer
= (u8
*)((uintptr_t)parent
->buffer
-
2769 binder_alloc_get_user_buffer_offset(
2770 &target_proc
->alloc
));
2771 *(binder_uintptr_t
*)(parent_buffer
+ bp
->parent_offset
) = bp
->buffer
;
2777 * binder_proc_transaction() - sends a transaction to a process and wakes it up
2778 * @t: transaction to send
2779 * @proc: process to send the transaction to
2780 * @thread: thread in @proc to send the transaction to (may be NULL)
2782 * This function queues a transaction to the specified process. It will try
2783 * to find a thread in the target process to handle the transaction and
2784 * wake it up. If no thread is found, the work is queued to the proc
2787 * If the @thread parameter is not NULL, the transaction is always queued
2788 * to the waitlist of that specific thread.
2790 * Return: true if the transactions was successfully queued
2791 * false if the target process or thread is dead
2793 static bool binder_proc_transaction(struct binder_transaction
*t
,
2794 struct binder_proc
*proc
,
2795 struct binder_thread
*thread
)
2797 struct binder_node
*node
= t
->buffer
->target_node
;
2798 struct binder_priority node_prio
;
2799 bool oneway
= !!(t
->flags
& TF_ONE_WAY
);
2800 bool pending_async
= false;
2803 binder_node_lock(node
);
2804 node_prio
.prio
= node
->min_priority
;
2805 node_prio
.sched_policy
= node
->sched_policy
;
2809 if (node
->has_async_transaction
) {
2810 pending_async
= true;
2812 node
->has_async_transaction
= 1;
2816 binder_inner_proc_lock(proc
);
2818 if (proc
->is_dead
|| (thread
&& thread
->is_dead
)) {
2819 binder_inner_proc_unlock(proc
);
2820 binder_node_unlock(node
);
2824 if (!thread
&& !pending_async
)
2825 thread
= binder_select_thread_ilocked(proc
);
2828 binder_transaction_priority(thread
->task
, t
, node_prio
,
2830 binder_enqueue_thread_work_ilocked(thread
, &t
->work
);
2831 } else if (!pending_async
) {
2832 binder_enqueue_work_ilocked(&t
->work
, &proc
->todo
);
2834 binder_enqueue_work_ilocked(&t
->work
, &node
->async_todo
);
2838 binder_wakeup_thread_ilocked(proc
, thread
, !oneway
/* sync */);
2840 binder_inner_proc_unlock(proc
);
2841 binder_node_unlock(node
);
2847 * binder_get_node_refs_for_txn() - Get required refs on node for txn
2848 * @node: struct binder_node for which to get refs
2849 * @proc: returns @node->proc if valid
2850 * @error: if no @proc then returns BR_DEAD_REPLY
2852 * User-space normally keeps the node alive when creating a transaction
2853 * since it has a reference to the target. The local strong ref keeps it
2854 * alive if the sending process dies before the target process processes
2855 * the transaction. If the source process is malicious or has a reference
2856 * counting bug, relying on the local strong ref can fail.
2858 * Since user-space can cause the local strong ref to go away, we also take
2859 * a tmpref on the node to ensure it survives while we are constructing
2860 * the transaction. We also need a tmpref on the proc while we are
2861 * constructing the transaction, so we take that here as well.
2863 * Return: The target_node with refs taken or NULL if no @node->proc is NULL.
2864 * Also sets @proc if valid. If the @node->proc is NULL indicating that the
2865 * target proc has died, @error is set to BR_DEAD_REPLY
2867 static struct binder_node
*binder_get_node_refs_for_txn(
2868 struct binder_node
*node
,
2869 struct binder_proc
**procp
,
2872 struct binder_node
*target_node
= NULL
;
2874 binder_node_inner_lock(node
);
2877 binder_inc_node_nilocked(node
, 1, 0, NULL
);
2878 binder_inc_node_tmpref_ilocked(node
);
2879 node
->proc
->tmp_ref
++;
2880 *procp
= node
->proc
;
2882 *error
= BR_DEAD_REPLY
;
2883 binder_node_inner_unlock(node
);
2888 static void binder_transaction(struct binder_proc
*proc
,
2889 struct binder_thread
*thread
,
2890 struct binder_transaction_data
*tr
, int reply
,
2891 binder_size_t extra_buffers_size
)
2894 struct binder_transaction
*t
;
2895 struct binder_work
*tcomplete
;
2896 binder_size_t
*offp
, *off_end
, *off_start
;
2897 binder_size_t off_min
;
2898 u8
*sg_bufp
, *sg_buf_end
;
2899 struct binder_proc
*target_proc
= NULL
;
2900 struct binder_thread
*target_thread
= NULL
;
2901 struct binder_node
*target_node
= NULL
;
2902 struct binder_transaction
*in_reply_to
= NULL
;
2903 struct binder_transaction_log_entry
*e
;
2904 uint32_t return_error
= 0;
2905 uint32_t return_error_param
= 0;
2906 uint32_t return_error_line
= 0;
2907 struct binder_buffer_object
*last_fixup_obj
= NULL
;
2908 binder_size_t last_fixup_min_off
= 0;
2909 struct binder_context
*context
= proc
->context
;
2910 int t_debug_id
= atomic_inc_return(&binder_last_id
);
2911 char *secctx
= NULL
;
2914 e
= binder_transaction_log_add(&binder_transaction_log
);
2915 e
->debug_id
= t_debug_id
;
2916 e
->call_type
= reply
? 2 : !!(tr
->flags
& TF_ONE_WAY
);
2917 e
->from_proc
= proc
->pid
;
2918 e
->from_thread
= thread
->pid
;
2919 e
->target_handle
= tr
->target
.handle
;
2920 e
->data_size
= tr
->data_size
;
2921 e
->offsets_size
= tr
->offsets_size
;
2922 e
->context_name
= proc
->context
->name
;
2925 binder_inner_proc_lock(proc
);
2926 in_reply_to
= thread
->transaction_stack
;
2927 if (in_reply_to
== NULL
) {
2928 binder_inner_proc_unlock(proc
);
2929 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
2930 proc
->pid
, thread
->pid
);
2931 return_error
= BR_FAILED_REPLY
;
2932 return_error_param
= -EPROTO
;
2933 return_error_line
= __LINE__
;
2934 goto err_empty_call_stack
;
2936 if (in_reply_to
->to_thread
!= thread
) {
2937 spin_lock(&in_reply_to
->lock
);
2938 binder_user_error("%d:%d got reply transaction with bad transaction stack, transaction %d has target %d:%d\n",
2939 proc
->pid
, thread
->pid
, in_reply_to
->debug_id
,
2940 in_reply_to
->to_proc
?
2941 in_reply_to
->to_proc
->pid
: 0,
2942 in_reply_to
->to_thread
?
2943 in_reply_to
->to_thread
->pid
: 0);
2944 spin_unlock(&in_reply_to
->lock
);
2945 binder_inner_proc_unlock(proc
);
2946 return_error
= BR_FAILED_REPLY
;
2947 return_error_param
= -EPROTO
;
2948 return_error_line
= __LINE__
;
2950 goto err_bad_call_stack
;
2952 thread
->transaction_stack
= in_reply_to
->to_parent
;
2953 binder_inner_proc_unlock(proc
);
2954 target_thread
= binder_get_txn_from_and_acq_inner(in_reply_to
);
2955 if (target_thread
== NULL
) {
2956 return_error
= BR_DEAD_REPLY
;
2957 return_error_line
= __LINE__
;
2958 goto err_dead_binder
;
2960 if (target_thread
->transaction_stack
!= in_reply_to
) {
2961 binder_user_error("%d:%d got reply transaction with bad target transaction stack %d, expected %d\n",
2962 proc
->pid
, thread
->pid
,
2963 target_thread
->transaction_stack
?
2964 target_thread
->transaction_stack
->debug_id
: 0,
2965 in_reply_to
->debug_id
);
2966 binder_inner_proc_unlock(target_thread
->proc
);
2967 return_error
= BR_FAILED_REPLY
;
2968 return_error_param
= -EPROTO
;
2969 return_error_line
= __LINE__
;
2971 target_thread
= NULL
;
2972 goto err_dead_binder
;
2974 target_proc
= target_thread
->proc
;
2975 target_proc
->tmp_ref
++;
2976 binder_inner_proc_unlock(target_thread
->proc
);
2978 if (tr
->target
.handle
) {
2979 struct binder_ref
*ref
;
2982 * There must already be a strong ref
2983 * on this node. If so, do a strong
2984 * increment on the node to ensure it
2985 * stays alive until the transaction is
2988 binder_proc_lock(proc
);
2989 ref
= binder_get_ref_olocked(proc
, tr
->target
.handle
,
2992 target_node
= binder_get_node_refs_for_txn(
2993 ref
->node
, &target_proc
,
2996 binder_user_error("%d:%d got transaction to invalid handle\n",
2997 proc
->pid
, thread
->pid
);
2998 return_error
= BR_FAILED_REPLY
;
3000 binder_proc_unlock(proc
);
3002 mutex_lock(&context
->context_mgr_node_lock
);
3003 target_node
= context
->binder_context_mgr_node
;
3005 target_node
= binder_get_node_refs_for_txn(
3006 target_node
, &target_proc
,
3009 return_error
= BR_DEAD_REPLY
;
3010 mutex_unlock(&context
->context_mgr_node_lock
);
3011 if (target_node
&& target_proc
== proc
) {
3012 binder_user_error("%d:%d got transaction to context manager from process owning it\n",
3013 proc
->pid
, thread
->pid
);
3014 return_error
= BR_FAILED_REPLY
;
3015 return_error_param
= -EINVAL
;
3016 return_error_line
= __LINE__
;
3017 goto err_invalid_target_handle
;
3022 * return_error is set above
3024 return_error_param
= -EINVAL
;
3025 return_error_line
= __LINE__
;
3026 goto err_dead_binder
;
3028 e
->to_node
= target_node
->debug_id
;
3029 if (security_binder_transaction(proc
->tsk
,
3030 target_proc
->tsk
) < 0) {
3031 return_error
= BR_FAILED_REPLY
;
3032 return_error_param
= -EPERM
;
3033 return_error_line
= __LINE__
;
3034 goto err_invalid_target_handle
;
3036 binder_inner_proc_lock(proc
);
3037 if (!(tr
->flags
& TF_ONE_WAY
) && thread
->transaction_stack
) {
3038 struct binder_transaction
*tmp
;
3040 tmp
= thread
->transaction_stack
;
3041 if (tmp
->to_thread
!= thread
) {
3042 spin_lock(&tmp
->lock
);
3043 binder_user_error("%d:%d got new transaction with bad transaction stack, transaction %d has target %d:%d\n",
3044 proc
->pid
, thread
->pid
, tmp
->debug_id
,
3045 tmp
->to_proc
? tmp
->to_proc
->pid
: 0,
3047 tmp
->to_thread
->pid
: 0);
3048 spin_unlock(&tmp
->lock
);
3049 binder_inner_proc_unlock(proc
);
3050 return_error
= BR_FAILED_REPLY
;
3051 return_error_param
= -EPROTO
;
3052 return_error_line
= __LINE__
;
3053 goto err_bad_call_stack
;
3056 struct binder_thread
*from
;
3058 spin_lock(&tmp
->lock
);
3060 if (from
&& from
->proc
== target_proc
) {
3061 atomic_inc(&from
->tmp_ref
);
3062 target_thread
= from
;
3063 spin_unlock(&tmp
->lock
);
3066 spin_unlock(&tmp
->lock
);
3067 tmp
= tmp
->from_parent
;
3070 binder_inner_proc_unlock(proc
);
3073 e
->to_thread
= target_thread
->pid
;
3074 e
->to_proc
= target_proc
->pid
;
3076 /* TODO: reuse incoming transaction for reply */
3077 t
= kzalloc(sizeof(*t
), GFP_KERNEL
);
3079 return_error
= BR_FAILED_REPLY
;
3080 return_error_param
= -ENOMEM
;
3081 return_error_line
= __LINE__
;
3082 goto err_alloc_t_failed
;
3084 binder_stats_created(BINDER_STAT_TRANSACTION
);
3085 spin_lock_init(&t
->lock
);
3087 tcomplete
= kzalloc(sizeof(*tcomplete
), GFP_KERNEL
);
3088 if (tcomplete
== NULL
) {
3089 return_error
= BR_FAILED_REPLY
;
3090 return_error_param
= -ENOMEM
;
3091 return_error_line
= __LINE__
;
3092 goto err_alloc_tcomplete_failed
;
3094 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE
);
3096 t
->debug_id
= t_debug_id
;
3099 binder_debug(BINDER_DEBUG_TRANSACTION
,
3100 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
3101 proc
->pid
, thread
->pid
, t
->debug_id
,
3102 target_proc
->pid
, target_thread
->pid
,
3103 (u64
)tr
->data
.ptr
.buffer
,
3104 (u64
)tr
->data
.ptr
.offsets
,
3105 (u64
)tr
->data_size
, (u64
)tr
->offsets_size
,
3106 (u64
)extra_buffers_size
);
3108 binder_debug(BINDER_DEBUG_TRANSACTION
,
3109 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
3110 proc
->pid
, thread
->pid
, t
->debug_id
,
3111 target_proc
->pid
, target_node
->debug_id
,
3112 (u64
)tr
->data
.ptr
.buffer
,
3113 (u64
)tr
->data
.ptr
.offsets
,
3114 (u64
)tr
->data_size
, (u64
)tr
->offsets_size
,
3115 (u64
)extra_buffers_size
);
3117 if (!reply
&& !(tr
->flags
& TF_ONE_WAY
))
3121 t
->sender_euid
= task_euid(proc
->tsk
);
3122 t
->to_proc
= target_proc
;
3123 t
->to_thread
= target_thread
;
3125 t
->flags
= tr
->flags
;
3126 if (!(t
->flags
& TF_ONE_WAY
) &&
3127 binder_supported_policy(current
->policy
)) {
3128 /* Inherit supported policies for synchronous transactions */
3129 t
->priority
.sched_policy
= current
->policy
;
3130 t
->priority
.prio
= current
->normal_prio
;
3132 /* Otherwise, fall back to the default priority */
3133 t
->priority
= target_proc
->default_priority
;
3136 if (target_node
&& target_node
->txn_security_ctx
) {
3140 security_task_getsecid(proc
->tsk
, &secid
);
3141 ret
= security_secid_to_secctx(secid
, &secctx
, &secctx_sz
);
3143 return_error
= BR_FAILED_REPLY
;
3144 return_error_param
= ret
;
3145 return_error_line
= __LINE__
;
3146 goto err_get_secctx_failed
;
3148 added_size
= ALIGN(secctx_sz
, sizeof(u64
));
3149 extra_buffers_size
+= added_size
;
3150 if (extra_buffers_size
< added_size
) {
3151 /* integer overflow of extra_buffers_size */
3152 return_error
= BR_FAILED_REPLY
;
3153 return_error_param
= EINVAL
;
3154 return_error_line
= __LINE__
;
3155 goto err_bad_extra_size
;
3159 trace_binder_transaction(reply
, t
, target_node
);
3161 t
->buffer
= binder_alloc_new_buf(&target_proc
->alloc
, tr
->data_size
,
3162 tr
->offsets_size
, extra_buffers_size
,
3163 !reply
&& (t
->flags
& TF_ONE_WAY
));
3164 if (IS_ERR(t
->buffer
)) {
3166 * -ESRCH indicates VMA cleared. The target is dying.
3168 return_error_param
= PTR_ERR(t
->buffer
);
3169 return_error
= return_error_param
== -ESRCH
?
3170 BR_DEAD_REPLY
: BR_FAILED_REPLY
;
3171 return_error_line
= __LINE__
;
3173 goto err_binder_alloc_buf_failed
;
3176 size_t buf_offset
= ALIGN(tr
->data_size
, sizeof(void *)) +
3177 ALIGN(tr
->offsets_size
, sizeof(void *)) +
3178 ALIGN(extra_buffers_size
, sizeof(void *)) -
3179 ALIGN(secctx_sz
, sizeof(u64
));
3180 char *kptr
= t
->buffer
->data
+ buf_offset
;
3182 t
->security_ctx
= (uintptr_t)kptr
+
3183 binder_alloc_get_user_buffer_offset(&target_proc
->alloc
);
3184 memcpy(kptr
, secctx
, secctx_sz
);
3185 security_release_secctx(secctx
, secctx_sz
);
3188 t
->buffer
->debug_id
= t
->debug_id
;
3189 t
->buffer
->transaction
= t
;
3190 t
->buffer
->target_node
= target_node
;
3191 trace_binder_transaction_alloc_buf(t
->buffer
);
3192 off_start
= (binder_size_t
*)(t
->buffer
->data
+
3193 ALIGN(tr
->data_size
, sizeof(void *)));
3196 if (copy_from_user(t
->buffer
->data
, (const void __user
*)(uintptr_t)
3197 tr
->data
.ptr
.buffer
, tr
->data_size
)) {
3198 binder_user_error("%d:%d got transaction with invalid data ptr\n",
3199 proc
->pid
, thread
->pid
);
3200 return_error
= BR_FAILED_REPLY
;
3201 return_error_param
= -EFAULT
;
3202 return_error_line
= __LINE__
;
3203 goto err_copy_data_failed
;
3205 if (copy_from_user(offp
, (const void __user
*)(uintptr_t)
3206 tr
->data
.ptr
.offsets
, tr
->offsets_size
)) {
3207 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3208 proc
->pid
, thread
->pid
);
3209 return_error
= BR_FAILED_REPLY
;
3210 return_error_param
= -EFAULT
;
3211 return_error_line
= __LINE__
;
3212 goto err_copy_data_failed
;
3214 if (!IS_ALIGNED(tr
->offsets_size
, sizeof(binder_size_t
))) {
3215 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
3216 proc
->pid
, thread
->pid
, (u64
)tr
->offsets_size
);
3217 return_error
= BR_FAILED_REPLY
;
3218 return_error_param
= -EINVAL
;
3219 return_error_line
= __LINE__
;
3220 goto err_bad_offset
;
3222 if (!IS_ALIGNED(extra_buffers_size
, sizeof(u64
))) {
3223 binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
3224 proc
->pid
, thread
->pid
,
3225 extra_buffers_size
);
3226 return_error
= BR_FAILED_REPLY
;
3227 return_error_param
= -EINVAL
;
3228 return_error_line
= __LINE__
;
3229 goto err_bad_offset
;
3231 off_end
= (void *)off_start
+ tr
->offsets_size
;
3232 sg_bufp
= (u8
*)(PTR_ALIGN(off_end
, sizeof(void *)));
3233 sg_buf_end
= sg_bufp
+ extra_buffers_size
;
3235 for (; offp
< off_end
; offp
++) {
3236 struct binder_object_header
*hdr
;
3237 size_t object_size
= binder_validate_object(t
->buffer
, *offp
);
3239 if (object_size
== 0 || *offp
< off_min
) {
3240 binder_user_error("%d:%d got transaction with invalid offset (%lld, min %lld max %lld) or object.\n",
3241 proc
->pid
, thread
->pid
, (u64
)*offp
,
3243 (u64
)t
->buffer
->data_size
);
3244 return_error
= BR_FAILED_REPLY
;
3245 return_error_param
= -EINVAL
;
3246 return_error_line
= __LINE__
;
3247 goto err_bad_offset
;
3250 hdr
= (struct binder_object_header
*)(t
->buffer
->data
+ *offp
);
3251 off_min
= *offp
+ object_size
;
3252 switch (hdr
->type
) {
3253 case BINDER_TYPE_BINDER
:
3254 case BINDER_TYPE_WEAK_BINDER
: {
3255 struct flat_binder_object
*fp
;
3257 fp
= to_flat_binder_object(hdr
);
3258 ret
= binder_translate_binder(fp
, t
, thread
);
3260 return_error
= BR_FAILED_REPLY
;
3261 return_error_param
= ret
;
3262 return_error_line
= __LINE__
;
3263 goto err_translate_failed
;
3266 case BINDER_TYPE_HANDLE
:
3267 case BINDER_TYPE_WEAK_HANDLE
: {
3268 struct flat_binder_object
*fp
;
3270 fp
= to_flat_binder_object(hdr
);
3271 ret
= binder_translate_handle(fp
, t
, thread
);
3273 return_error
= BR_FAILED_REPLY
;
3274 return_error_param
= ret
;
3275 return_error_line
= __LINE__
;
3276 goto err_translate_failed
;
3280 case BINDER_TYPE_FD
: {
3281 struct binder_fd_object
*fp
= to_binder_fd_object(hdr
);
3282 int target_fd
= binder_translate_fd(fp
->fd
, t
, thread
,
3285 if (target_fd
< 0) {
3286 return_error
= BR_FAILED_REPLY
;
3287 return_error_param
= target_fd
;
3288 return_error_line
= __LINE__
;
3289 goto err_translate_failed
;
3294 case BINDER_TYPE_FDA
: {
3295 struct binder_fd_array_object
*fda
=
3296 to_binder_fd_array_object(hdr
);
3297 struct binder_buffer_object
*parent
=
3298 binder_validate_ptr(t
->buffer
, fda
->parent
,
3302 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
3303 proc
->pid
, thread
->pid
);
3304 return_error
= BR_FAILED_REPLY
;
3305 return_error_param
= -EINVAL
;
3306 return_error_line
= __LINE__
;
3307 goto err_bad_parent
;
3309 if (!binder_validate_fixup(t
->buffer
, off_start
,
3310 parent
, fda
->parent_offset
,
3312 last_fixup_min_off
)) {
3313 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
3314 proc
->pid
, thread
->pid
);
3315 return_error
= BR_FAILED_REPLY
;
3316 return_error_param
= -EINVAL
;
3317 return_error_line
= __LINE__
;
3318 goto err_bad_parent
;
3320 ret
= binder_translate_fd_array(fda
, parent
, t
, thread
,
3323 return_error
= BR_FAILED_REPLY
;
3324 return_error_param
= ret
;
3325 return_error_line
= __LINE__
;
3326 goto err_translate_failed
;
3328 last_fixup_obj
= parent
;
3329 last_fixup_min_off
=
3330 fda
->parent_offset
+ sizeof(u32
) * fda
->num_fds
;
3332 case BINDER_TYPE_PTR
: {
3333 struct binder_buffer_object
*bp
=
3334 to_binder_buffer_object(hdr
);
3335 size_t buf_left
= sg_buf_end
- sg_bufp
;
3337 if (bp
->length
> buf_left
) {
3338 binder_user_error("%d:%d got transaction with too large buffer\n",
3339 proc
->pid
, thread
->pid
);
3340 return_error
= BR_FAILED_REPLY
;
3341 return_error_param
= -EINVAL
;
3342 return_error_line
= __LINE__
;
3343 goto err_bad_offset
;
3345 if (copy_from_user(sg_bufp
,
3346 (const void __user
*)(uintptr_t)
3347 bp
->buffer
, bp
->length
)) {
3348 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3349 proc
->pid
, thread
->pid
);
3350 return_error_param
= -EFAULT
;
3351 return_error
= BR_FAILED_REPLY
;
3352 return_error_line
= __LINE__
;
3353 goto err_copy_data_failed
;
3355 /* Fixup buffer pointer to target proc address space */
3356 bp
->buffer
= (uintptr_t)sg_bufp
+
3357 binder_alloc_get_user_buffer_offset(
3358 &target_proc
->alloc
);
3359 sg_bufp
+= ALIGN(bp
->length
, sizeof(u64
));
3361 ret
= binder_fixup_parent(t
, thread
, bp
, off_start
,
3364 last_fixup_min_off
);
3366 return_error
= BR_FAILED_REPLY
;
3367 return_error_param
= ret
;
3368 return_error_line
= __LINE__
;
3369 goto err_translate_failed
;
3371 last_fixup_obj
= bp
;
3372 last_fixup_min_off
= 0;
3375 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
3376 proc
->pid
, thread
->pid
, hdr
->type
);
3377 return_error
= BR_FAILED_REPLY
;
3378 return_error_param
= -EINVAL
;
3379 return_error_line
= __LINE__
;
3380 goto err_bad_object_type
;
3383 tcomplete
->type
= BINDER_WORK_TRANSACTION_COMPLETE
;
3384 t
->work
.type
= BINDER_WORK_TRANSACTION
;
3387 binder_enqueue_thread_work(thread
, tcomplete
);
3388 binder_inner_proc_lock(target_proc
);
3389 if (target_thread
->is_dead
) {
3390 binder_inner_proc_unlock(target_proc
);
3391 goto err_dead_proc_or_thread
;
3393 BUG_ON(t
->buffer
->async_transaction
!= 0);
3394 binder_pop_transaction_ilocked(target_thread
, in_reply_to
);
3395 binder_enqueue_thread_work_ilocked(target_thread
, &t
->work
);
3396 binder_inner_proc_unlock(target_proc
);
3397 wake_up_interruptible_sync(&target_thread
->wait
);
3398 binder_restore_priority(current
, in_reply_to
->saved_priority
);
3399 binder_free_transaction(in_reply_to
);
3400 } else if (!(t
->flags
& TF_ONE_WAY
)) {
3401 BUG_ON(t
->buffer
->async_transaction
!= 0);
3402 binder_inner_proc_lock(proc
);
3404 * Defer the TRANSACTION_COMPLETE, so we don't return to
3405 * userspace immediately; this allows the target process to
3406 * immediately start processing this transaction, reducing
3407 * latency. We will then return the TRANSACTION_COMPLETE when
3408 * the target replies (or there is an error).
3410 binder_enqueue_deferred_thread_work_ilocked(thread
, tcomplete
);
3412 t
->from_parent
= thread
->transaction_stack
;
3413 thread
->transaction_stack
= t
;
3414 binder_inner_proc_unlock(proc
);
3415 if (!binder_proc_transaction(t
, target_proc
, target_thread
)) {
3416 binder_inner_proc_lock(proc
);
3417 binder_pop_transaction_ilocked(thread
, t
);
3418 binder_inner_proc_unlock(proc
);
3419 goto err_dead_proc_or_thread
;
3422 BUG_ON(target_node
== NULL
);
3423 BUG_ON(t
->buffer
->async_transaction
!= 1);
3424 binder_enqueue_thread_work(thread
, tcomplete
);
3425 if (!binder_proc_transaction(t
, target_proc
, NULL
))
3426 goto err_dead_proc_or_thread
;
3429 binder_thread_dec_tmpref(target_thread
);
3430 binder_proc_dec_tmpref(target_proc
);
3432 binder_dec_node_tmpref(target_node
);
3434 * write barrier to synchronize with initialization
3438 WRITE_ONCE(e
->debug_id_done
, t_debug_id
);
3441 err_dead_proc_or_thread
:
3442 return_error
= BR_DEAD_REPLY
;
3443 return_error_line
= __LINE__
;
3444 binder_dequeue_work(proc
, tcomplete
);
3445 err_translate_failed
:
3446 err_bad_object_type
:
3449 err_copy_data_failed
:
3450 trace_binder_transaction_failed_buffer_release(t
->buffer
);
3451 binder_transaction_buffer_release(target_proc
, t
->buffer
, offp
);
3453 binder_dec_node_tmpref(target_node
);
3455 t
->buffer
->transaction
= NULL
;
3456 binder_alloc_free_buf(&target_proc
->alloc
, t
->buffer
);
3457 err_binder_alloc_buf_failed
:
3460 security_release_secctx(secctx
, secctx_sz
);
3461 err_get_secctx_failed
:
3463 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE
);
3464 err_alloc_tcomplete_failed
:
3466 binder_stats_deleted(BINDER_STAT_TRANSACTION
);
3469 err_empty_call_stack
:
3471 err_invalid_target_handle
:
3473 binder_thread_dec_tmpref(target_thread
);
3475 binder_proc_dec_tmpref(target_proc
);
3477 binder_dec_node(target_node
, 1, 0);
3478 binder_dec_node_tmpref(target_node
);
3481 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
3482 "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
3483 proc
->pid
, thread
->pid
, return_error
, return_error_param
,
3484 (u64
)tr
->data_size
, (u64
)tr
->offsets_size
,
3488 struct binder_transaction_log_entry
*fe
;
3490 e
->return_error
= return_error
;
3491 e
->return_error_param
= return_error_param
;
3492 e
->return_error_line
= return_error_line
;
3493 fe
= binder_transaction_log_add(&binder_transaction_log_failed
);
3496 * write barrier to synchronize with initialization
3500 WRITE_ONCE(e
->debug_id_done
, t_debug_id
);
3501 WRITE_ONCE(fe
->debug_id_done
, t_debug_id
);
3504 BUG_ON(thread
->return_error
.cmd
!= BR_OK
);
3506 binder_restore_priority(current
, in_reply_to
->saved_priority
);
3507 thread
->return_error
.cmd
= BR_TRANSACTION_COMPLETE
;
3508 binder_enqueue_thread_work(thread
, &thread
->return_error
.work
);
3509 binder_send_failed_reply(in_reply_to
, return_error
);
3511 thread
->return_error
.cmd
= return_error
;
3512 binder_enqueue_thread_work(thread
, &thread
->return_error
.work
);
3516 int binder_thread_write(struct binder_proc
*proc
,
3517 struct binder_thread
*thread
,
3518 binder_uintptr_t binder_buffer
, size_t size
,
3519 binder_size_t
*consumed
)
3522 struct binder_context
*context
= proc
->context
;
3523 void __user
*buffer
= (void __user
*)(uintptr_t)binder_buffer
;
3524 void __user
*ptr
= buffer
+ *consumed
;
3525 void __user
*end
= buffer
+ size
;
3527 while (ptr
< end
&& thread
->return_error
.cmd
== BR_OK
) {
3530 if (get_user(cmd
, (uint32_t __user
*)ptr
))
3532 ptr
+= sizeof(uint32_t);
3533 trace_binder_command(cmd
);
3534 if (_IOC_NR(cmd
) < ARRAY_SIZE(binder_stats
.bc
)) {
3535 atomic_inc(&binder_stats
.bc
[_IOC_NR(cmd
)]);
3536 atomic_inc(&proc
->stats
.bc
[_IOC_NR(cmd
)]);
3537 atomic_inc(&thread
->stats
.bc
[_IOC_NR(cmd
)]);
3545 const char *debug_string
;
3546 bool strong
= cmd
== BC_ACQUIRE
|| cmd
== BC_RELEASE
;
3547 bool increment
= cmd
== BC_INCREFS
|| cmd
== BC_ACQUIRE
;
3548 struct binder_ref_data rdata
;
3550 if (get_user(target
, (uint32_t __user
*)ptr
))
3553 ptr
+= sizeof(uint32_t);
3555 if (increment
&& !target
) {
3556 struct binder_node
*ctx_mgr_node
;
3557 mutex_lock(&context
->context_mgr_node_lock
);
3558 ctx_mgr_node
= context
->binder_context_mgr_node
;
3560 ret
= binder_inc_ref_for_node(
3562 strong
, NULL
, &rdata
);
3563 mutex_unlock(&context
->context_mgr_node_lock
);
3566 ret
= binder_update_ref_for_handle(
3567 proc
, target
, increment
, strong
,
3569 if (!ret
&& rdata
.desc
!= target
) {
3570 binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
3571 proc
->pid
, thread
->pid
,
3572 target
, rdata
.desc
);
3576 debug_string
= "IncRefs";
3579 debug_string
= "Acquire";
3582 debug_string
= "Release";
3586 debug_string
= "DecRefs";
3590 binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
3591 proc
->pid
, thread
->pid
, debug_string
,
3592 strong
, target
, ret
);
3595 binder_debug(BINDER_DEBUG_USER_REFS
,
3596 "%d:%d %s ref %d desc %d s %d w %d\n",
3597 proc
->pid
, thread
->pid
, debug_string
,
3598 rdata
.debug_id
, rdata
.desc
, rdata
.strong
,
3602 case BC_INCREFS_DONE
:
3603 case BC_ACQUIRE_DONE
: {
3604 binder_uintptr_t node_ptr
;
3605 binder_uintptr_t cookie
;
3606 struct binder_node
*node
;
3609 if (get_user(node_ptr
, (binder_uintptr_t __user
*)ptr
))
3611 ptr
+= sizeof(binder_uintptr_t
);
3612 if (get_user(cookie
, (binder_uintptr_t __user
*)ptr
))
3614 ptr
+= sizeof(binder_uintptr_t
);
3615 node
= binder_get_node(proc
, node_ptr
);
3617 binder_user_error("%d:%d %s u%016llx no match\n",
3618 proc
->pid
, thread
->pid
,
3619 cmd
== BC_INCREFS_DONE
?
3625 if (cookie
!= node
->cookie
) {
3626 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
3627 proc
->pid
, thread
->pid
,
3628 cmd
== BC_INCREFS_DONE
?
3629 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
3630 (u64
)node_ptr
, node
->debug_id
,
3631 (u64
)cookie
, (u64
)node
->cookie
);
3632 binder_put_node(node
);
3635 binder_node_inner_lock(node
);
3636 if (cmd
== BC_ACQUIRE_DONE
) {
3637 if (node
->pending_strong_ref
== 0) {
3638 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
3639 proc
->pid
, thread
->pid
,
3641 binder_node_inner_unlock(node
);
3642 binder_put_node(node
);
3645 node
->pending_strong_ref
= 0;
3647 if (node
->pending_weak_ref
== 0) {
3648 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
3649 proc
->pid
, thread
->pid
,
3651 binder_node_inner_unlock(node
);
3652 binder_put_node(node
);
3655 node
->pending_weak_ref
= 0;
3657 free_node
= binder_dec_node_nilocked(node
,
3658 cmd
== BC_ACQUIRE_DONE
, 0);
3660 binder_debug(BINDER_DEBUG_USER_REFS
,
3661 "%d:%d %s node %d ls %d lw %d tr %d\n",
3662 proc
->pid
, thread
->pid
,
3663 cmd
== BC_INCREFS_DONE
? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
3664 node
->debug_id
, node
->local_strong_refs
,
3665 node
->local_weak_refs
, node
->tmp_refs
);
3666 binder_node_inner_unlock(node
);
3667 binder_put_node(node
);
3670 case BC_ATTEMPT_ACQUIRE
:
3671 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
3673 case BC_ACQUIRE_RESULT
:
3674 pr_err("BC_ACQUIRE_RESULT not supported\n");
3677 case BC_FREE_BUFFER
: {
3678 binder_uintptr_t data_ptr
;
3679 struct binder_buffer
*buffer
;
3681 if (get_user(data_ptr
, (binder_uintptr_t __user
*)ptr
))
3683 ptr
+= sizeof(binder_uintptr_t
);
3685 buffer
= binder_alloc_prepare_to_free(&proc
->alloc
,
3687 if (IS_ERR_OR_NULL(buffer
)) {
3688 if (PTR_ERR(buffer
) == -EPERM
) {
3690 "%d:%d BC_FREE_BUFFER u%016llx matched unreturned or currently freeing buffer\n",
3691 proc
->pid
, thread
->pid
,
3695 "%d:%d BC_FREE_BUFFER u%016llx no match\n",
3696 proc
->pid
, thread
->pid
,
3701 binder_debug(BINDER_DEBUG_FREE_BUFFER
,
3702 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
3703 proc
->pid
, thread
->pid
, (u64
)data_ptr
,
3705 buffer
->transaction
? "active" : "finished");
3707 if (buffer
->transaction
) {
3708 buffer
->transaction
->buffer
= NULL
;
3709 buffer
->transaction
= NULL
;
3711 if (buffer
->async_transaction
&& buffer
->target_node
) {
3712 struct binder_node
*buf_node
;
3713 struct binder_work
*w
;
3715 buf_node
= buffer
->target_node
;
3716 binder_node_inner_lock(buf_node
);
3717 BUG_ON(!buf_node
->has_async_transaction
);
3718 BUG_ON(buf_node
->proc
!= proc
);
3719 w
= binder_dequeue_work_head_ilocked(
3720 &buf_node
->async_todo
);
3722 buf_node
->has_async_transaction
= 0;
3724 binder_enqueue_work_ilocked(
3726 binder_wakeup_proc_ilocked(proc
);
3728 binder_node_inner_unlock(buf_node
);
3730 trace_binder_transaction_buffer_release(buffer
);
3731 binder_transaction_buffer_release(proc
, buffer
, NULL
);
3732 binder_alloc_free_buf(&proc
->alloc
, buffer
);
3736 case BC_TRANSACTION_SG
:
3738 struct binder_transaction_data_sg tr
;
3740 if (copy_from_user(&tr
, ptr
, sizeof(tr
)))
3743 binder_transaction(proc
, thread
, &tr
.transaction_data
,
3744 cmd
== BC_REPLY_SG
, tr
.buffers_size
);
3747 case BC_TRANSACTION
:
3749 struct binder_transaction_data tr
;
3751 if (copy_from_user(&tr
, ptr
, sizeof(tr
)))
3754 binder_transaction(proc
, thread
, &tr
,
3755 cmd
== BC_REPLY
, 0);
3759 case BC_REGISTER_LOOPER
:
3760 binder_debug(BINDER_DEBUG_THREADS
,
3761 "%d:%d BC_REGISTER_LOOPER\n",
3762 proc
->pid
, thread
->pid
);
3763 binder_inner_proc_lock(proc
);
3764 if (thread
->looper
& BINDER_LOOPER_STATE_ENTERED
) {
3765 thread
->looper
|= BINDER_LOOPER_STATE_INVALID
;
3766 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
3767 proc
->pid
, thread
->pid
);
3768 } else if (proc
->requested_threads
== 0) {
3769 thread
->looper
|= BINDER_LOOPER_STATE_INVALID
;
3770 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
3771 proc
->pid
, thread
->pid
);
3773 proc
->requested_threads
--;
3774 proc
->requested_threads_started
++;
3776 thread
->looper
|= BINDER_LOOPER_STATE_REGISTERED
;
3777 binder_inner_proc_unlock(proc
);
3779 case BC_ENTER_LOOPER
:
3780 binder_debug(BINDER_DEBUG_THREADS
,
3781 "%d:%d BC_ENTER_LOOPER\n",
3782 proc
->pid
, thread
->pid
);
3783 if (thread
->looper
& BINDER_LOOPER_STATE_REGISTERED
) {
3784 thread
->looper
|= BINDER_LOOPER_STATE_INVALID
;
3785 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
3786 proc
->pid
, thread
->pid
);
3788 thread
->looper
|= BINDER_LOOPER_STATE_ENTERED
;
3790 case BC_EXIT_LOOPER
:
3791 binder_debug(BINDER_DEBUG_THREADS
,
3792 "%d:%d BC_EXIT_LOOPER\n",
3793 proc
->pid
, thread
->pid
);
3794 thread
->looper
|= BINDER_LOOPER_STATE_EXITED
;
3797 case BC_REQUEST_DEATH_NOTIFICATION
:
3798 case BC_CLEAR_DEATH_NOTIFICATION
: {
3800 binder_uintptr_t cookie
;
3801 struct binder_ref
*ref
;
3802 struct binder_ref_death
*death
= NULL
;
3804 if (get_user(target
, (uint32_t __user
*)ptr
))
3806 ptr
+= sizeof(uint32_t);
3807 if (get_user(cookie
, (binder_uintptr_t __user
*)ptr
))
3809 ptr
+= sizeof(binder_uintptr_t
);
3810 if (cmd
== BC_REQUEST_DEATH_NOTIFICATION
) {
3812 * Allocate memory for death notification
3813 * before taking lock
3815 death
= kzalloc(sizeof(*death
), GFP_KERNEL
);
3816 if (death
== NULL
) {
3817 WARN_ON(thread
->return_error
.cmd
!=
3819 thread
->return_error
.cmd
= BR_ERROR
;
3820 binder_enqueue_thread_work(
3822 &thread
->return_error
.work
);
3824 BINDER_DEBUG_FAILED_TRANSACTION
,
3825 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
3826 proc
->pid
, thread
->pid
);
3830 binder_proc_lock(proc
);
3831 ref
= binder_get_ref_olocked(proc
, target
, false);
3833 binder_user_error("%d:%d %s invalid ref %d\n",
3834 proc
->pid
, thread
->pid
,
3835 cmd
== BC_REQUEST_DEATH_NOTIFICATION
?
3836 "BC_REQUEST_DEATH_NOTIFICATION" :
3837 "BC_CLEAR_DEATH_NOTIFICATION",
3839 binder_proc_unlock(proc
);
3844 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION
,
3845 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
3846 proc
->pid
, thread
->pid
,
3847 cmd
== BC_REQUEST_DEATH_NOTIFICATION
?
3848 "BC_REQUEST_DEATH_NOTIFICATION" :
3849 "BC_CLEAR_DEATH_NOTIFICATION",
3850 (u64
)cookie
, ref
->data
.debug_id
,
3851 ref
->data
.desc
, ref
->data
.strong
,
3852 ref
->data
.weak
, ref
->node
->debug_id
);
3854 binder_node_lock(ref
->node
);
3855 if (cmd
== BC_REQUEST_DEATH_NOTIFICATION
) {
3857 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
3858 proc
->pid
, thread
->pid
);
3859 binder_node_unlock(ref
->node
);
3860 binder_proc_unlock(proc
);
3864 binder_stats_created(BINDER_STAT_DEATH
);
3865 INIT_LIST_HEAD(&death
->work
.entry
);
3866 death
->cookie
= cookie
;
3868 if (ref
->node
->proc
== NULL
) {
3869 ref
->death
->work
.type
= BINDER_WORK_DEAD_BINDER
;
3871 binder_inner_proc_lock(proc
);
3872 binder_enqueue_work_ilocked(
3873 &ref
->death
->work
, &proc
->todo
);
3874 binder_wakeup_proc_ilocked(proc
);
3875 binder_inner_proc_unlock(proc
);
3878 if (ref
->death
== NULL
) {
3879 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
3880 proc
->pid
, thread
->pid
);
3881 binder_node_unlock(ref
->node
);
3882 binder_proc_unlock(proc
);
3886 if (death
->cookie
!= cookie
) {
3887 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
3888 proc
->pid
, thread
->pid
,
3891 binder_node_unlock(ref
->node
);
3892 binder_proc_unlock(proc
);
3896 binder_inner_proc_lock(proc
);
3897 if (list_empty(&death
->work
.entry
)) {
3898 death
->work
.type
= BINDER_WORK_CLEAR_DEATH_NOTIFICATION
;
3899 if (thread
->looper
&
3900 (BINDER_LOOPER_STATE_REGISTERED
|
3901 BINDER_LOOPER_STATE_ENTERED
))
3902 binder_enqueue_thread_work_ilocked(
3906 binder_enqueue_work_ilocked(
3909 binder_wakeup_proc_ilocked(
3913 BUG_ON(death
->work
.type
!= BINDER_WORK_DEAD_BINDER
);
3914 death
->work
.type
= BINDER_WORK_DEAD_BINDER_AND_CLEAR
;
3916 binder_inner_proc_unlock(proc
);
3918 binder_node_unlock(ref
->node
);
3919 binder_proc_unlock(proc
);
3921 case BC_DEAD_BINDER_DONE
: {
3922 struct binder_work
*w
;
3923 binder_uintptr_t cookie
;
3924 struct binder_ref_death
*death
= NULL
;
3926 if (get_user(cookie
, (binder_uintptr_t __user
*)ptr
))
3929 ptr
+= sizeof(cookie
);
3930 binder_inner_proc_lock(proc
);
3931 list_for_each_entry(w
, &proc
->delivered_death
,
3933 struct binder_ref_death
*tmp_death
=
3935 struct binder_ref_death
,
3938 if (tmp_death
->cookie
== cookie
) {
3943 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
3944 "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n",
3945 proc
->pid
, thread
->pid
, (u64
)cookie
,
3947 if (death
== NULL
) {
3948 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
3949 proc
->pid
, thread
->pid
, (u64
)cookie
);
3950 binder_inner_proc_unlock(proc
);
3953 binder_dequeue_work_ilocked(&death
->work
);
3954 if (death
->work
.type
== BINDER_WORK_DEAD_BINDER_AND_CLEAR
) {
3955 death
->work
.type
= BINDER_WORK_CLEAR_DEATH_NOTIFICATION
;
3956 if (thread
->looper
&
3957 (BINDER_LOOPER_STATE_REGISTERED
|
3958 BINDER_LOOPER_STATE_ENTERED
))
3959 binder_enqueue_thread_work_ilocked(
3960 thread
, &death
->work
);
3962 binder_enqueue_work_ilocked(
3965 binder_wakeup_proc_ilocked(proc
);
3968 binder_inner_proc_unlock(proc
);
3972 pr_err("%d:%d unknown command %d\n",
3973 proc
->pid
, thread
->pid
, cmd
);
3976 *consumed
= ptr
- buffer
;
3981 static void binder_stat_br(struct binder_proc
*proc
,
3982 struct binder_thread
*thread
, uint32_t cmd
)
3984 trace_binder_return(cmd
);
3985 if (_IOC_NR(cmd
) < ARRAY_SIZE(binder_stats
.br
)) {
3986 atomic_inc(&binder_stats
.br
[_IOC_NR(cmd
)]);
3987 atomic_inc(&proc
->stats
.br
[_IOC_NR(cmd
)]);
3988 atomic_inc(&thread
->stats
.br
[_IOC_NR(cmd
)]);
3992 static int binder_put_node_cmd(struct binder_proc
*proc
,
3993 struct binder_thread
*thread
,
3995 binder_uintptr_t node_ptr
,
3996 binder_uintptr_t node_cookie
,
3998 uint32_t cmd
, const char *cmd_name
)
4000 void __user
*ptr
= *ptrp
;
4002 if (put_user(cmd
, (uint32_t __user
*)ptr
))
4004 ptr
+= sizeof(uint32_t);
4006 if (put_user(node_ptr
, (binder_uintptr_t __user
*)ptr
))
4008 ptr
+= sizeof(binder_uintptr_t
);
4010 if (put_user(node_cookie
, (binder_uintptr_t __user
*)ptr
))
4012 ptr
+= sizeof(binder_uintptr_t
);
4014 binder_stat_br(proc
, thread
, cmd
);
4015 binder_debug(BINDER_DEBUG_USER_REFS
, "%d:%d %s %d u%016llx c%016llx\n",
4016 proc
->pid
, thread
->pid
, cmd_name
, node_debug_id
,
4017 (u64
)node_ptr
, (u64
)node_cookie
);
4023 static int binder_wait_for_work(struct binder_thread
*thread
,
4027 struct binder_proc
*proc
= thread
->proc
;
4030 freezer_do_not_count();
4031 binder_inner_proc_lock(proc
);
4033 prepare_to_wait(&thread
->wait
, &wait
, TASK_INTERRUPTIBLE
);
4034 if (binder_has_work_ilocked(thread
, do_proc_work
))
4037 list_add(&thread
->waiting_thread_node
,
4038 &proc
->waiting_threads
);
4039 binder_inner_proc_unlock(proc
);
4041 binder_inner_proc_lock(proc
);
4042 list_del_init(&thread
->waiting_thread_node
);
4043 if (signal_pending(current
)) {
4048 finish_wait(&thread
->wait
, &wait
);
4049 binder_inner_proc_unlock(proc
);
4055 static int binder_thread_read(struct binder_proc
*proc
,
4056 struct binder_thread
*thread
,
4057 binder_uintptr_t binder_buffer
, size_t size
,
4058 binder_size_t
*consumed
, int non_block
)
4060 void __user
*buffer
= (void __user
*)(uintptr_t)binder_buffer
;
4061 void __user
*ptr
= buffer
+ *consumed
;
4062 void __user
*end
= buffer
+ size
;
4065 int wait_for_proc_work
;
4067 if (*consumed
== 0) {
4068 if (put_user(BR_NOOP
, (uint32_t __user
*)ptr
))
4070 ptr
+= sizeof(uint32_t);
4074 binder_inner_proc_lock(proc
);
4075 wait_for_proc_work
= binder_available_for_proc_work_ilocked(thread
);
4076 binder_inner_proc_unlock(proc
);
4078 thread
->looper
|= BINDER_LOOPER_STATE_WAITING
;
4080 trace_binder_wait_for_work(wait_for_proc_work
,
4081 !!thread
->transaction_stack
,
4082 !binder_worklist_empty(proc
, &thread
->todo
));
4083 if (wait_for_proc_work
) {
4084 if (!(thread
->looper
& (BINDER_LOOPER_STATE_REGISTERED
|
4085 BINDER_LOOPER_STATE_ENTERED
))) {
4086 binder_user_error("%d:%d ERROR: Thread waiting for process work before calling BC_REGISTER_LOOPER or BC_ENTER_LOOPER (state %x)\n",
4087 proc
->pid
, thread
->pid
, thread
->looper
);
4088 wait_event_interruptible(binder_user_error_wait
,
4089 binder_stop_on_user_error
< 2);
4091 binder_restore_priority(current
, proc
->default_priority
);
4095 if (!binder_has_work(thread
, wait_for_proc_work
))
4098 ret
= binder_wait_for_work(thread
, wait_for_proc_work
);
4101 thread
->looper
&= ~BINDER_LOOPER_STATE_WAITING
;
4108 struct binder_transaction_data_secctx tr
;
4109 struct binder_transaction_data
*trd
= &tr
.transaction_data
;
4110 struct binder_work
*w
= NULL
;
4111 struct list_head
*list
= NULL
;
4112 struct binder_transaction
*t
= NULL
;
4113 struct binder_thread
*t_from
;
4114 size_t trsize
= sizeof(*trd
);
4116 binder_inner_proc_lock(proc
);
4117 if (!binder_worklist_empty_ilocked(&thread
->todo
))
4118 list
= &thread
->todo
;
4119 else if (!binder_worklist_empty_ilocked(&proc
->todo
) &&
4123 binder_inner_proc_unlock(proc
);
4126 if (ptr
- buffer
== 4 && !thread
->looper_need_return
)
4131 if (end
- ptr
< sizeof(tr
) + 4) {
4132 binder_inner_proc_unlock(proc
);
4135 w
= binder_dequeue_work_head_ilocked(list
);
4136 if (binder_worklist_empty_ilocked(&thread
->todo
))
4137 thread
->process_todo
= false;
4140 case BINDER_WORK_TRANSACTION
: {
4141 binder_inner_proc_unlock(proc
);
4142 t
= container_of(w
, struct binder_transaction
, work
);
4144 case BINDER_WORK_RETURN_ERROR
: {
4145 struct binder_error
*e
= container_of(
4146 w
, struct binder_error
, work
);
4148 WARN_ON(e
->cmd
== BR_OK
);
4149 binder_inner_proc_unlock(proc
);
4150 if (put_user(e
->cmd
, (uint32_t __user
*)ptr
))
4153 ptr
+= sizeof(uint32_t);
4155 binder_stat_br(proc
, thread
, cmd
);
4157 case BINDER_WORK_TRANSACTION_COMPLETE
: {
4158 binder_inner_proc_unlock(proc
);
4159 cmd
= BR_TRANSACTION_COMPLETE
;
4160 if (put_user(cmd
, (uint32_t __user
*)ptr
))
4162 ptr
+= sizeof(uint32_t);
4164 binder_stat_br(proc
, thread
, cmd
);
4165 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE
,
4166 "%d:%d BR_TRANSACTION_COMPLETE\n",
4167 proc
->pid
, thread
->pid
);
4169 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE
);
4171 case BINDER_WORK_NODE
: {
4172 struct binder_node
*node
= container_of(w
, struct binder_node
, work
);
4174 binder_uintptr_t node_ptr
= node
->ptr
;
4175 binder_uintptr_t node_cookie
= node
->cookie
;
4176 int node_debug_id
= node
->debug_id
;
4179 void __user
*orig_ptr
= ptr
;
4181 BUG_ON(proc
!= node
->proc
);
4182 strong
= node
->internal_strong_refs
||
4183 node
->local_strong_refs
;
4184 weak
= !hlist_empty(&node
->refs
) ||
4185 node
->local_weak_refs
||
4186 node
->tmp_refs
|| strong
;
4187 has_strong_ref
= node
->has_strong_ref
;
4188 has_weak_ref
= node
->has_weak_ref
;
4190 if (weak
&& !has_weak_ref
) {
4191 node
->has_weak_ref
= 1;
4192 node
->pending_weak_ref
= 1;
4193 node
->local_weak_refs
++;
4195 if (strong
&& !has_strong_ref
) {
4196 node
->has_strong_ref
= 1;
4197 node
->pending_strong_ref
= 1;
4198 node
->local_strong_refs
++;
4200 if (!strong
&& has_strong_ref
)
4201 node
->has_strong_ref
= 0;
4202 if (!weak
&& has_weak_ref
)
4203 node
->has_weak_ref
= 0;
4204 if (!weak
&& !strong
) {
4205 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
4206 "%d:%d node %d u%016llx c%016llx deleted\n",
4207 proc
->pid
, thread
->pid
,
4211 rb_erase(&node
->rb_node
, &proc
->nodes
);
4212 binder_inner_proc_unlock(proc
);
4213 binder_node_lock(node
);
4215 * Acquire the node lock before freeing the
4216 * node to serialize with other threads that
4217 * may have been holding the node lock while
4218 * decrementing this node (avoids race where
4219 * this thread frees while the other thread
4220 * is unlocking the node after the final
4223 binder_node_unlock(node
);
4224 binder_free_node(node
);
4226 binder_inner_proc_unlock(proc
);
4228 if (weak
&& !has_weak_ref
)
4229 ret
= binder_put_node_cmd(
4230 proc
, thread
, &ptr
, node_ptr
,
4231 node_cookie
, node_debug_id
,
4232 BR_INCREFS
, "BR_INCREFS");
4233 if (!ret
&& strong
&& !has_strong_ref
)
4234 ret
= binder_put_node_cmd(
4235 proc
, thread
, &ptr
, node_ptr
,
4236 node_cookie
, node_debug_id
,
4237 BR_ACQUIRE
, "BR_ACQUIRE");
4238 if (!ret
&& !strong
&& has_strong_ref
)
4239 ret
= binder_put_node_cmd(
4240 proc
, thread
, &ptr
, node_ptr
,
4241 node_cookie
, node_debug_id
,
4242 BR_RELEASE
, "BR_RELEASE");
4243 if (!ret
&& !weak
&& has_weak_ref
)
4244 ret
= binder_put_node_cmd(
4245 proc
, thread
, &ptr
, node_ptr
,
4246 node_cookie
, node_debug_id
,
4247 BR_DECREFS
, "BR_DECREFS");
4248 if (orig_ptr
== ptr
)
4249 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
4250 "%d:%d node %d u%016llx c%016llx state unchanged\n",
4251 proc
->pid
, thread
->pid
,
4258 case BINDER_WORK_DEAD_BINDER
:
4259 case BINDER_WORK_DEAD_BINDER_AND_CLEAR
:
4260 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION
: {
4261 struct binder_ref_death
*death
;
4263 binder_uintptr_t cookie
;
4265 death
= container_of(w
, struct binder_ref_death
, work
);
4266 if (w
->type
== BINDER_WORK_CLEAR_DEATH_NOTIFICATION
)
4267 cmd
= BR_CLEAR_DEATH_NOTIFICATION_DONE
;
4269 cmd
= BR_DEAD_BINDER
;
4270 cookie
= death
->cookie
;
4272 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION
,
4273 "%d:%d %s %016llx\n",
4274 proc
->pid
, thread
->pid
,
4275 cmd
== BR_DEAD_BINDER
?
4277 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
4279 if (w
->type
== BINDER_WORK_CLEAR_DEATH_NOTIFICATION
) {
4280 binder_inner_proc_unlock(proc
);
4282 binder_stats_deleted(BINDER_STAT_DEATH
);
4284 binder_enqueue_work_ilocked(
4285 w
, &proc
->delivered_death
);
4286 binder_inner_proc_unlock(proc
);
4288 if (put_user(cmd
, (uint32_t __user
*)ptr
))
4290 ptr
+= sizeof(uint32_t);
4291 if (put_user(cookie
,
4292 (binder_uintptr_t __user
*)ptr
))
4294 ptr
+= sizeof(binder_uintptr_t
);
4295 binder_stat_br(proc
, thread
, cmd
);
4296 if (cmd
== BR_DEAD_BINDER
)
4297 goto done
; /* DEAD_BINDER notifications can cause transactions */
4304 BUG_ON(t
->buffer
== NULL
);
4305 if (t
->buffer
->target_node
) {
4306 struct binder_node
*target_node
= t
->buffer
->target_node
;
4307 struct binder_priority node_prio
;
4309 trd
->target
.ptr
= target_node
->ptr
;
4310 trd
->cookie
= target_node
->cookie
;
4311 node_prio
.sched_policy
= target_node
->sched_policy
;
4312 node_prio
.prio
= target_node
->min_priority
;
4313 binder_transaction_priority(current
, t
, node_prio
,
4314 target_node
->inherit_rt
);
4315 cmd
= BR_TRANSACTION
;
4317 trd
->target
.ptr
= 0;
4321 trd
->code
= t
->code
;
4322 trd
->flags
= t
->flags
;
4323 trd
->sender_euid
= from_kuid(current_user_ns(), t
->sender_euid
);
4325 t_from
= binder_get_txn_from(t
);
4327 struct task_struct
*sender
= t_from
->proc
->tsk
;
4330 task_tgid_nr_ns(sender
,
4331 task_active_pid_ns(current
));
4333 trd
->sender_pid
= 0;
4336 trd
->data_size
= t
->buffer
->data_size
;
4337 trd
->offsets_size
= t
->buffer
->offsets_size
;
4338 trd
->data
.ptr
.buffer
= (binder_uintptr_t
)
4339 ((uintptr_t)t
->buffer
->data
+
4340 binder_alloc_get_user_buffer_offset(&proc
->alloc
));
4341 trd
->data
.ptr
.offsets
= trd
->data
.ptr
.buffer
+
4342 ALIGN(t
->buffer
->data_size
,
4345 tr
.secctx
= t
->security_ctx
;
4346 if (t
->security_ctx
) {
4347 cmd
= BR_TRANSACTION_SEC_CTX
;
4348 trsize
= sizeof(tr
);
4350 if (put_user(cmd
, (uint32_t __user
*)ptr
)) {
4352 binder_thread_dec_tmpref(t_from
);
4354 binder_cleanup_transaction(t
, "put_user failed",
4359 ptr
+= sizeof(uint32_t);
4360 if (copy_to_user(ptr
, &tr
, trsize
)) {
4362 binder_thread_dec_tmpref(t_from
);
4364 binder_cleanup_transaction(t
, "copy_to_user failed",
4371 trace_binder_transaction_received(t
);
4372 binder_stat_br(proc
, thread
, cmd
);
4373 binder_debug(BINDER_DEBUG_TRANSACTION
,
4374 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
4375 proc
->pid
, thread
->pid
,
4376 (cmd
== BR_TRANSACTION
) ? "BR_TRANSACTION" :
4377 (cmd
== BR_TRANSACTION_SEC_CTX
) ?
4378 "BR_TRANSACTION_SEC_CTX" : "BR_REPLY",
4379 t
->debug_id
, t_from
? t_from
->proc
->pid
: 0,
4380 t_from
? t_from
->pid
: 0, cmd
,
4381 t
->buffer
->data_size
, t
->buffer
->offsets_size
,
4382 (u64
)trd
->data
.ptr
.buffer
,
4383 (u64
)trd
->data
.ptr
.offsets
);
4386 binder_thread_dec_tmpref(t_from
);
4387 t
->buffer
->allow_user_free
= 1;
4388 if (cmd
!= BR_REPLY
&& !(t
->flags
& TF_ONE_WAY
)) {
4389 binder_inner_proc_lock(thread
->proc
);
4390 t
->to_parent
= thread
->transaction_stack
;
4391 t
->to_thread
= thread
;
4392 thread
->transaction_stack
= t
;
4393 binder_inner_proc_unlock(thread
->proc
);
4395 binder_free_transaction(t
);
4402 *consumed
= ptr
- buffer
;
4403 binder_inner_proc_lock(proc
);
4404 if (proc
->requested_threads
== 0 &&
4405 list_empty(&thread
->proc
->waiting_threads
) &&
4406 proc
->requested_threads_started
< proc
->max_threads
&&
4407 (thread
->looper
& (BINDER_LOOPER_STATE_REGISTERED
|
4408 BINDER_LOOPER_STATE_ENTERED
)) /* the user-space code fails to */
4409 /*spawn a new thread if we leave this out */) {
4410 proc
->requested_threads
++;
4411 binder_inner_proc_unlock(proc
);
4412 binder_debug(BINDER_DEBUG_THREADS
,
4413 "%d:%d BR_SPAWN_LOOPER\n",
4414 proc
->pid
, thread
->pid
);
4415 if (put_user(BR_SPAWN_LOOPER
, (uint32_t __user
*)buffer
))
4417 binder_stat_br(proc
, thread
, BR_SPAWN_LOOPER
);
4419 binder_inner_proc_unlock(proc
);
4423 static void binder_release_work(struct binder_proc
*proc
,
4424 struct list_head
*list
)
4426 struct binder_work
*w
;
4429 w
= binder_dequeue_work_head(proc
, list
);
4434 case BINDER_WORK_TRANSACTION
: {
4435 struct binder_transaction
*t
;
4437 t
= container_of(w
, struct binder_transaction
, work
);
4439 binder_cleanup_transaction(t
, "process died.",
4442 case BINDER_WORK_RETURN_ERROR
: {
4443 struct binder_error
*e
= container_of(
4444 w
, struct binder_error
, work
);
4446 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
4447 "undelivered TRANSACTION_ERROR: %u\n",
4450 case BINDER_WORK_TRANSACTION_COMPLETE
: {
4451 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
4452 "undelivered TRANSACTION_COMPLETE\n");
4454 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE
);
4456 case BINDER_WORK_DEAD_BINDER_AND_CLEAR
:
4457 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION
: {
4458 struct binder_ref_death
*death
;
4460 death
= container_of(w
, struct binder_ref_death
, work
);
4461 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
4462 "undelivered death notification, %016llx\n",
4463 (u64
)death
->cookie
);
4465 binder_stats_deleted(BINDER_STAT_DEATH
);
4468 pr_err("unexpected work type, %d, not freed\n",
4476 static struct binder_thread
*binder_get_thread_ilocked(
4477 struct binder_proc
*proc
, struct binder_thread
*new_thread
)
4479 struct binder_thread
*thread
= NULL
;
4480 struct rb_node
*parent
= NULL
;
4481 struct rb_node
**p
= &proc
->threads
.rb_node
;
4485 thread
= rb_entry(parent
, struct binder_thread
, rb_node
);
4487 if (current
->pid
< thread
->pid
)
4489 else if (current
->pid
> thread
->pid
)
4490 p
= &(*p
)->rb_right
;
4496 thread
= new_thread
;
4497 binder_stats_created(BINDER_STAT_THREAD
);
4498 thread
->proc
= proc
;
4499 thread
->pid
= current
->pid
;
4500 get_task_struct(current
);
4501 thread
->task
= current
;
4502 atomic_set(&thread
->tmp_ref
, 0);
4503 init_waitqueue_head(&thread
->wait
);
4504 INIT_LIST_HEAD(&thread
->todo
);
4505 rb_link_node(&thread
->rb_node
, parent
, p
);
4506 rb_insert_color(&thread
->rb_node
, &proc
->threads
);
4507 thread
->looper_need_return
= true;
4508 thread
->return_error
.work
.type
= BINDER_WORK_RETURN_ERROR
;
4509 thread
->return_error
.cmd
= BR_OK
;
4510 thread
->reply_error
.work
.type
= BINDER_WORK_RETURN_ERROR
;
4511 thread
->reply_error
.cmd
= BR_OK
;
4512 INIT_LIST_HEAD(&new_thread
->waiting_thread_node
);
4516 static struct binder_thread
*binder_get_thread(struct binder_proc
*proc
)
4518 struct binder_thread
*thread
;
4519 struct binder_thread
*new_thread
;
4521 binder_inner_proc_lock(proc
);
4522 thread
= binder_get_thread_ilocked(proc
, NULL
);
4523 binder_inner_proc_unlock(proc
);
4525 new_thread
= kzalloc(sizeof(*thread
), GFP_KERNEL
);
4526 if (new_thread
== NULL
)
4528 binder_inner_proc_lock(proc
);
4529 thread
= binder_get_thread_ilocked(proc
, new_thread
);
4530 binder_inner_proc_unlock(proc
);
4531 if (thread
!= new_thread
)
4537 static void binder_free_proc(struct binder_proc
*proc
)
4539 BUG_ON(!list_empty(&proc
->todo
));
4540 BUG_ON(!list_empty(&proc
->delivered_death
));
4541 binder_alloc_deferred_release(&proc
->alloc
);
4542 put_task_struct(proc
->tsk
);
4543 binder_stats_deleted(BINDER_STAT_PROC
);
4547 static void binder_free_thread(struct binder_thread
*thread
)
4549 BUG_ON(!list_empty(&thread
->todo
));
4550 binder_stats_deleted(BINDER_STAT_THREAD
);
4551 binder_proc_dec_tmpref(thread
->proc
);
4552 put_task_struct(thread
->task
);
4556 static int binder_thread_release(struct binder_proc
*proc
,
4557 struct binder_thread
*thread
)
4559 struct binder_transaction
*t
;
4560 struct binder_transaction
*send_reply
= NULL
;
4561 int active_transactions
= 0;
4562 struct binder_transaction
*last_t
= NULL
;
4564 binder_inner_proc_lock(thread
->proc
);
4566 * take a ref on the proc so it survives
4567 * after we remove this thread from proc->threads.
4568 * The corresponding dec is when we actually
4569 * free the thread in binder_free_thread()
4573 * take a ref on this thread to ensure it
4574 * survives while we are releasing it
4576 atomic_inc(&thread
->tmp_ref
);
4577 rb_erase(&thread
->rb_node
, &proc
->threads
);
4578 t
= thread
->transaction_stack
;
4580 spin_lock(&t
->lock
);
4581 if (t
->to_thread
== thread
)
4584 thread
->is_dead
= true;
4588 active_transactions
++;
4589 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
4590 "release %d:%d transaction %d %s, still active\n",
4591 proc
->pid
, thread
->pid
,
4593 (t
->to_thread
== thread
) ? "in" : "out");
4595 if (t
->to_thread
== thread
) {
4597 t
->to_thread
= NULL
;
4599 t
->buffer
->transaction
= NULL
;
4603 } else if (t
->from
== thread
) {
4608 spin_unlock(&last_t
->lock
);
4610 spin_lock(&t
->lock
);
4614 * If this thread used poll, make sure we remove the waitqueue
4615 * from any epoll data structures holding it with POLLFREE.
4616 * waitqueue_active() is safe to use here because we're holding
4619 if ((thread
->looper
& BINDER_LOOPER_STATE_POLL
) &&
4620 waitqueue_active(&thread
->wait
)) {
4621 wake_up_poll(&thread
->wait
, POLLHUP
| POLLFREE
);
4624 binder_inner_proc_unlock(thread
->proc
);
4627 * This is needed to avoid races between wake_up_poll() above and
4628 * and ep_remove_waitqueue() called for other reasons (eg the epoll file
4629 * descriptor being closed); ep_remove_waitqueue() holds an RCU read
4630 * lock, so we can be sure it's done after calling synchronize_rcu().
4632 if (thread
->looper
& BINDER_LOOPER_STATE_POLL
)
4636 binder_send_failed_reply(send_reply
, BR_DEAD_REPLY
);
4637 binder_release_work(proc
, &thread
->todo
);
4638 binder_thread_dec_tmpref(thread
);
4639 return active_transactions
;
4642 static unsigned int binder_poll(struct file
*filp
,
4643 struct poll_table_struct
*wait
)
4645 struct binder_proc
*proc
= filp
->private_data
;
4646 struct binder_thread
*thread
= NULL
;
4647 bool wait_for_proc_work
;
4649 thread
= binder_get_thread(proc
);
4651 binder_inner_proc_lock(thread
->proc
);
4652 thread
->looper
|= BINDER_LOOPER_STATE_POLL
;
4653 wait_for_proc_work
= binder_available_for_proc_work_ilocked(thread
);
4655 binder_inner_proc_unlock(thread
->proc
);
4657 poll_wait(filp
, &thread
->wait
, wait
);
4659 if (binder_has_work(thread
, wait_for_proc_work
))
4665 static int binder_ioctl_write_read(struct file
*filp
,
4666 unsigned int cmd
, unsigned long arg
,
4667 struct binder_thread
*thread
)
4670 struct binder_proc
*proc
= filp
->private_data
;
4671 unsigned int size
= _IOC_SIZE(cmd
);
4672 void __user
*ubuf
= (void __user
*)arg
;
4673 struct binder_write_read bwr
;
4675 if (size
!= sizeof(struct binder_write_read
)) {
4679 if (copy_from_user(&bwr
, ubuf
, sizeof(bwr
))) {
4683 binder_debug(BINDER_DEBUG_READ_WRITE
,
4684 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
4685 proc
->pid
, thread
->pid
,
4686 (u64
)bwr
.write_size
, (u64
)bwr
.write_buffer
,
4687 (u64
)bwr
.read_size
, (u64
)bwr
.read_buffer
);
4689 if (bwr
.write_size
> 0) {
4690 ret
= binder_thread_write(proc
, thread
,
4693 &bwr
.write_consumed
);
4694 trace_binder_write_done(ret
);
4696 bwr
.read_consumed
= 0;
4697 if (copy_to_user(ubuf
, &bwr
, sizeof(bwr
)))
4702 if (bwr
.read_size
> 0) {
4703 ret
= binder_thread_read(proc
, thread
, bwr
.read_buffer
,
4706 filp
->f_flags
& O_NONBLOCK
);
4707 trace_binder_read_done(ret
);
4708 binder_inner_proc_lock(proc
);
4709 if (!binder_worklist_empty_ilocked(&proc
->todo
))
4710 binder_wakeup_proc_ilocked(proc
);
4711 binder_inner_proc_unlock(proc
);
4713 if (copy_to_user(ubuf
, &bwr
, sizeof(bwr
)))
4718 binder_debug(BINDER_DEBUG_READ_WRITE
,
4719 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
4720 proc
->pid
, thread
->pid
,
4721 (u64
)bwr
.write_consumed
, (u64
)bwr
.write_size
,
4722 (u64
)bwr
.read_consumed
, (u64
)bwr
.read_size
);
4723 if (copy_to_user(ubuf
, &bwr
, sizeof(bwr
))) {
4731 static int binder_ioctl_set_ctx_mgr(struct file
*filp
,
4732 struct flat_binder_object
*fbo
)
4735 struct binder_proc
*proc
= filp
->private_data
;
4736 struct binder_context
*context
= proc
->context
;
4737 struct binder_node
*new_node
;
4738 kuid_t curr_euid
= current_euid();
4740 mutex_lock(&context
->context_mgr_node_lock
);
4741 if (context
->binder_context_mgr_node
) {
4742 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
4746 ret
= security_binder_set_context_mgr(proc
->tsk
);
4749 if (uid_valid(context
->binder_context_mgr_uid
)) {
4750 if (!uid_eq(context
->binder_context_mgr_uid
, curr_euid
)) {
4751 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
4752 from_kuid(&init_user_ns
, curr_euid
),
4753 from_kuid(&init_user_ns
,
4754 context
->binder_context_mgr_uid
));
4759 context
->binder_context_mgr_uid
= curr_euid
;
4761 new_node
= binder_new_node(proc
, fbo
);
4766 binder_node_lock(new_node
);
4767 new_node
->local_weak_refs
++;
4768 new_node
->local_strong_refs
++;
4769 new_node
->has_strong_ref
= 1;
4770 new_node
->has_weak_ref
= 1;
4771 context
->binder_context_mgr_node
= new_node
;
4772 binder_node_unlock(new_node
);
4773 binder_put_node(new_node
);
4775 mutex_unlock(&context
->context_mgr_node_lock
);
4779 static int binder_ioctl_get_node_info_for_ref(struct binder_proc
*proc
,
4780 struct binder_node_info_for_ref
*info
)
4782 struct binder_node
*node
;
4783 struct binder_context
*context
= proc
->context
;
4784 __u32 handle
= info
->handle
;
4786 if (info
->strong_count
|| info
->weak_count
|| info
->reserved1
||
4787 info
->reserved2
|| info
->reserved3
) {
4788 binder_user_error("%d BINDER_GET_NODE_INFO_FOR_REF: only handle may be non-zero.",
4793 /* This ioctl may only be used by the context manager */
4794 mutex_lock(&context
->context_mgr_node_lock
);
4795 if (!context
->binder_context_mgr_node
||
4796 context
->binder_context_mgr_node
->proc
!= proc
) {
4797 mutex_unlock(&context
->context_mgr_node_lock
);
4800 mutex_unlock(&context
->context_mgr_node_lock
);
4802 node
= binder_get_node_from_ref(proc
, handle
, true, NULL
);
4806 info
->strong_count
= node
->local_strong_refs
+
4807 node
->internal_strong_refs
;
4808 info
->weak_count
= node
->local_weak_refs
;
4810 binder_put_node(node
);
4815 static int binder_ioctl_get_node_debug_info(struct binder_proc
*proc
,
4816 struct binder_node_debug_info
*info
) {
4818 binder_uintptr_t ptr
= info
->ptr
;
4820 memset(info
, 0, sizeof(*info
));
4822 binder_inner_proc_lock(proc
);
4823 for (n
= rb_first(&proc
->nodes
); n
!= NULL
; n
= rb_next(n
)) {
4824 struct binder_node
*node
= rb_entry(n
, struct binder_node
,
4826 if (node
->ptr
> ptr
) {
4827 info
->ptr
= node
->ptr
;
4828 info
->cookie
= node
->cookie
;
4829 info
->has_strong_ref
= node
->has_strong_ref
;
4830 info
->has_weak_ref
= node
->has_weak_ref
;
4834 binder_inner_proc_unlock(proc
);
4839 static long binder_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
4842 struct binder_proc
*proc
= filp
->private_data
;
4843 struct binder_thread
*thread
;
4844 unsigned int size
= _IOC_SIZE(cmd
);
4845 void __user
*ubuf
= (void __user
*)arg
;
4847 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
4848 proc->pid, current->pid, cmd, arg);*/
4850 binder_selftest_alloc(&proc
->alloc
);
4852 trace_binder_ioctl(cmd
, arg
);
4854 ret
= wait_event_interruptible(binder_user_error_wait
, binder_stop_on_user_error
< 2);
4858 thread
= binder_get_thread(proc
);
4859 if (thread
== NULL
) {
4865 case BINDER_WRITE_READ
:
4866 ret
= binder_ioctl_write_read(filp
, cmd
, arg
, thread
);
4870 case BINDER_SET_MAX_THREADS
: {
4873 if (copy_from_user(&max_threads
, ubuf
,
4874 sizeof(max_threads
))) {
4878 binder_inner_proc_lock(proc
);
4879 proc
->max_threads
= max_threads
;
4880 binder_inner_proc_unlock(proc
);
4883 case BINDER_SET_CONTEXT_MGR_EXT
: {
4884 struct flat_binder_object fbo
;
4886 if (copy_from_user(&fbo
, ubuf
, sizeof(fbo
))) {
4890 ret
= binder_ioctl_set_ctx_mgr(filp
, &fbo
);
4895 case BINDER_SET_CONTEXT_MGR
:
4896 ret
= binder_ioctl_set_ctx_mgr(filp
, NULL
);
4900 case BINDER_THREAD_EXIT
:
4901 binder_debug(BINDER_DEBUG_THREADS
, "%d:%d exit\n",
4902 proc
->pid
, thread
->pid
);
4903 binder_thread_release(proc
, thread
);
4906 case BINDER_VERSION
: {
4907 struct binder_version __user
*ver
= ubuf
;
4909 if (size
!= sizeof(struct binder_version
)) {
4913 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION
,
4914 &ver
->protocol_version
)) {
4920 case BINDER_GET_NODE_INFO_FOR_REF
: {
4921 struct binder_node_info_for_ref info
;
4923 if (copy_from_user(&info
, ubuf
, sizeof(info
))) {
4928 ret
= binder_ioctl_get_node_info_for_ref(proc
, &info
);
4932 if (copy_to_user(ubuf
, &info
, sizeof(info
))) {
4939 case BINDER_GET_NODE_DEBUG_INFO
: {
4940 struct binder_node_debug_info info
;
4942 if (copy_from_user(&info
, ubuf
, sizeof(info
))) {
4947 ret
= binder_ioctl_get_node_debug_info(proc
, &info
);
4951 if (copy_to_user(ubuf
, &info
, sizeof(info
))) {
4964 thread
->looper_need_return
= false;
4965 wait_event_interruptible(binder_user_error_wait
, binder_stop_on_user_error
< 2);
4966 if (ret
&& ret
!= -ERESTARTSYS
)
4967 pr_info("%d:%d ioctl %x %lx returned %d\n", proc
->pid
, current
->pid
, cmd
, arg
, ret
);
4969 trace_binder_ioctl_done(ret
);
4973 static void binder_vma_open(struct vm_area_struct
*vma
)
4975 struct binder_proc
*proc
= vma
->vm_private_data
;
4977 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
4978 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
4979 proc
->pid
, vma
->vm_start
, vma
->vm_end
,
4980 (vma
->vm_end
- vma
->vm_start
) / SZ_1K
, vma
->vm_flags
,
4981 (unsigned long)pgprot_val(vma
->vm_page_prot
));
4984 static void binder_vma_close(struct vm_area_struct
*vma
)
4986 struct binder_proc
*proc
= vma
->vm_private_data
;
4988 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
4989 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
4990 proc
->pid
, vma
->vm_start
, vma
->vm_end
,
4991 (vma
->vm_end
- vma
->vm_start
) / SZ_1K
, vma
->vm_flags
,
4992 (unsigned long)pgprot_val(vma
->vm_page_prot
));
4993 binder_alloc_vma_close(&proc
->alloc
);
4994 binder_defer_work(proc
, BINDER_DEFERRED_PUT_FILES
);
4997 static int binder_vm_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
4999 return VM_FAULT_SIGBUS
;
5002 static struct vm_operations_struct binder_vm_ops
= {
5003 .open
= binder_vma_open
,
5004 .close
= binder_vma_close
,
5005 .fault
= binder_vm_fault
,
5008 static int binder_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
5011 struct binder_proc
*proc
= filp
->private_data
;
5012 const char *failure_string
;
5014 if (proc
->tsk
!= current
->group_leader
)
5017 if ((vma
->vm_end
- vma
->vm_start
) > SZ_4M
)
5018 vma
->vm_end
= vma
->vm_start
+ SZ_4M
;
5020 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
5021 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
5022 __func__
, proc
->pid
, vma
->vm_start
, vma
->vm_end
,
5023 (vma
->vm_end
- vma
->vm_start
) / SZ_1K
, vma
->vm_flags
,
5024 (unsigned long)pgprot_val(vma
->vm_page_prot
));
5026 if (vma
->vm_flags
& FORBIDDEN_MMAP_FLAGS
) {
5028 failure_string
= "bad vm_flags";
5031 vma
->vm_flags
= (vma
->vm_flags
| VM_DONTCOPY
) & ~VM_MAYWRITE
;
5032 vma
->vm_ops
= &binder_vm_ops
;
5033 vma
->vm_private_data
= proc
;
5035 ret
= binder_alloc_mmap_handler(&proc
->alloc
, vma
);
5038 mutex_lock(&proc
->files_lock
);
5039 proc
->files
= get_files_struct(current
);
5040 mutex_unlock(&proc
->files_lock
);
5044 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
5045 proc
->pid
, vma
->vm_start
, vma
->vm_end
, failure_string
, ret
);
5049 static int binder_open(struct inode
*nodp
, struct file
*filp
)
5051 struct binder_proc
*proc
;
5052 struct binder_device
*binder_dev
;
5054 binder_debug(BINDER_DEBUG_OPEN_CLOSE
, "binder_open: %d:%d\n",
5055 current
->group_leader
->pid
, current
->pid
);
5057 proc
= kzalloc(sizeof(*proc
), GFP_KERNEL
);
5060 spin_lock_init(&proc
->inner_lock
);
5061 spin_lock_init(&proc
->outer_lock
);
5062 get_task_struct(current
->group_leader
);
5063 proc
->tsk
= current
->group_leader
;
5064 mutex_init(&proc
->files_lock
);
5065 INIT_LIST_HEAD(&proc
->todo
);
5066 if (binder_supported_policy(current
->policy
)) {
5067 proc
->default_priority
.sched_policy
= current
->policy
;
5068 proc
->default_priority
.prio
= current
->normal_prio
;
5070 proc
->default_priority
.sched_policy
= SCHED_NORMAL
;
5071 proc
->default_priority
.prio
= NICE_TO_PRIO(0);
5074 binder_dev
= container_of(filp
->private_data
, struct binder_device
,
5076 proc
->context
= &binder_dev
->context
;
5077 binder_alloc_init(&proc
->alloc
);
5079 binder_stats_created(BINDER_STAT_PROC
);
5080 proc
->pid
= current
->group_leader
->pid
;
5081 INIT_LIST_HEAD(&proc
->delivered_death
);
5082 INIT_LIST_HEAD(&proc
->waiting_threads
);
5083 filp
->private_data
= proc
;
5085 mutex_lock(&binder_procs_lock
);
5086 hlist_add_head(&proc
->proc_node
, &binder_procs
);
5087 mutex_unlock(&binder_procs_lock
);
5089 if (binder_debugfs_dir_entry_proc
) {
5092 snprintf(strbuf
, sizeof(strbuf
), "%u", proc
->pid
);
5094 * proc debug entries are shared between contexts, so
5095 * this will fail if the process tries to open the driver
5096 * again with a different context. The priting code will
5097 * anyway print all contexts that a given PID has, so this
5100 proc
->debugfs_entry
= debugfs_create_file(strbuf
, S_IRUGO
,
5101 binder_debugfs_dir_entry_proc
,
5102 (void *)(unsigned long)proc
->pid
,
5109 static int binder_flush(struct file
*filp
, fl_owner_t id
)
5111 struct binder_proc
*proc
= filp
->private_data
;
5113 binder_defer_work(proc
, BINDER_DEFERRED_FLUSH
);
5118 static void binder_deferred_flush(struct binder_proc
*proc
)
5123 binder_inner_proc_lock(proc
);
5124 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
)) {
5125 struct binder_thread
*thread
= rb_entry(n
, struct binder_thread
, rb_node
);
5127 thread
->looper_need_return
= true;
5128 if (thread
->looper
& BINDER_LOOPER_STATE_WAITING
) {
5129 wake_up_interruptible(&thread
->wait
);
5133 binder_inner_proc_unlock(proc
);
5135 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
5136 "binder_flush: %d woke %d threads\n", proc
->pid
,
5140 static int binder_release(struct inode
*nodp
, struct file
*filp
)
5142 struct binder_proc
*proc
= filp
->private_data
;
5144 debugfs_remove(proc
->debugfs_entry
);
5145 binder_defer_work(proc
, BINDER_DEFERRED_RELEASE
);
5150 static int binder_node_release(struct binder_node
*node
, int refs
)
5152 struct binder_ref
*ref
;
5154 struct binder_proc
*proc
= node
->proc
;
5156 binder_release_work(proc
, &node
->async_todo
);
5158 binder_node_lock(node
);
5159 binder_inner_proc_lock(proc
);
5160 binder_dequeue_work_ilocked(&node
->work
);
5162 * The caller must have taken a temporary ref on the node,
5164 BUG_ON(!node
->tmp_refs
);
5165 if (hlist_empty(&node
->refs
) && node
->tmp_refs
== 1) {
5166 binder_inner_proc_unlock(proc
);
5167 binder_node_unlock(node
);
5168 binder_free_node(node
);
5174 node
->local_strong_refs
= 0;
5175 node
->local_weak_refs
= 0;
5176 binder_inner_proc_unlock(proc
);
5178 spin_lock(&binder_dead_nodes_lock
);
5179 hlist_add_head(&node
->dead_node
, &binder_dead_nodes
);
5180 spin_unlock(&binder_dead_nodes_lock
);
5182 hlist_for_each_entry(ref
, &node
->refs
, node_entry
) {
5185 * Need the node lock to synchronize
5186 * with new notification requests and the
5187 * inner lock to synchronize with queued
5188 * death notifications.
5190 binder_inner_proc_lock(ref
->proc
);
5192 binder_inner_proc_unlock(ref
->proc
);
5198 BUG_ON(!list_empty(&ref
->death
->work
.entry
));
5199 ref
->death
->work
.type
= BINDER_WORK_DEAD_BINDER
;
5200 binder_enqueue_work_ilocked(&ref
->death
->work
,
5202 binder_wakeup_proc_ilocked(ref
->proc
);
5203 binder_inner_proc_unlock(ref
->proc
);
5206 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
5207 "node %d now dead, refs %d, death %d\n",
5208 node
->debug_id
, refs
, death
);
5209 binder_node_unlock(node
);
5210 binder_put_node(node
);
5215 static void binder_deferred_release(struct binder_proc
*proc
)
5217 struct binder_context
*context
= proc
->context
;
5219 int threads
, nodes
, incoming_refs
, outgoing_refs
, active_transactions
;
5221 BUG_ON(proc
->files
);
5223 mutex_lock(&binder_procs_lock
);
5224 hlist_del(&proc
->proc_node
);
5225 mutex_unlock(&binder_procs_lock
);
5227 mutex_lock(&context
->context_mgr_node_lock
);
5228 if (context
->binder_context_mgr_node
&&
5229 context
->binder_context_mgr_node
->proc
== proc
) {
5230 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
5231 "%s: %d context_mgr_node gone\n",
5232 __func__
, proc
->pid
);
5233 context
->binder_context_mgr_node
= NULL
;
5235 mutex_unlock(&context
->context_mgr_node_lock
);
5236 binder_inner_proc_lock(proc
);
5238 * Make sure proc stays alive after we
5239 * remove all the threads
5243 proc
->is_dead
= true;
5245 active_transactions
= 0;
5246 while ((n
= rb_first(&proc
->threads
))) {
5247 struct binder_thread
*thread
;
5249 thread
= rb_entry(n
, struct binder_thread
, rb_node
);
5250 binder_inner_proc_unlock(proc
);
5252 active_transactions
+= binder_thread_release(proc
, thread
);
5253 binder_inner_proc_lock(proc
);
5258 while ((n
= rb_first(&proc
->nodes
))) {
5259 struct binder_node
*node
;
5261 node
= rb_entry(n
, struct binder_node
, rb_node
);
5264 * take a temporary ref on the node before
5265 * calling binder_node_release() which will either
5266 * kfree() the node or call binder_put_node()
5268 binder_inc_node_tmpref_ilocked(node
);
5269 rb_erase(&node
->rb_node
, &proc
->nodes
);
5270 binder_inner_proc_unlock(proc
);
5271 incoming_refs
= binder_node_release(node
, incoming_refs
);
5272 binder_inner_proc_lock(proc
);
5274 binder_inner_proc_unlock(proc
);
5277 binder_proc_lock(proc
);
5278 while ((n
= rb_first(&proc
->refs_by_desc
))) {
5279 struct binder_ref
*ref
;
5281 ref
= rb_entry(n
, struct binder_ref
, rb_node_desc
);
5283 binder_cleanup_ref_olocked(ref
);
5284 binder_proc_unlock(proc
);
5285 binder_free_ref(ref
);
5286 binder_proc_lock(proc
);
5288 binder_proc_unlock(proc
);
5290 binder_release_work(proc
, &proc
->todo
);
5291 binder_release_work(proc
, &proc
->delivered_death
);
5293 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
5294 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
5295 __func__
, proc
->pid
, threads
, nodes
, incoming_refs
,
5296 outgoing_refs
, active_transactions
);
5298 binder_proc_dec_tmpref(proc
);
5301 static void binder_deferred_func(struct work_struct
*work
)
5303 struct binder_proc
*proc
;
5304 struct files_struct
*files
;
5309 mutex_lock(&binder_deferred_lock
);
5310 if (!hlist_empty(&binder_deferred_list
)) {
5311 proc
= hlist_entry(binder_deferred_list
.first
,
5312 struct binder_proc
, deferred_work_node
);
5313 hlist_del_init(&proc
->deferred_work_node
);
5314 defer
= proc
->deferred_work
;
5315 proc
->deferred_work
= 0;
5320 mutex_unlock(&binder_deferred_lock
);
5323 if (defer
& BINDER_DEFERRED_PUT_FILES
) {
5324 mutex_lock(&proc
->files_lock
);
5325 files
= proc
->files
;
5328 mutex_unlock(&proc
->files_lock
);
5331 if (defer
& BINDER_DEFERRED_FLUSH
)
5332 binder_deferred_flush(proc
);
5334 if (defer
& BINDER_DEFERRED_RELEASE
)
5335 binder_deferred_release(proc
); /* frees proc */
5338 put_files_struct(files
);
5341 static DECLARE_WORK(binder_deferred_work
, binder_deferred_func
);
5344 binder_defer_work(struct binder_proc
*proc
, enum binder_deferred_state defer
)
5346 mutex_lock(&binder_deferred_lock
);
5347 proc
->deferred_work
|= defer
;
5348 if (hlist_unhashed(&proc
->deferred_work_node
)) {
5349 hlist_add_head(&proc
->deferred_work_node
,
5350 &binder_deferred_list
);
5351 queue_work(binder_deferred_workqueue
, &binder_deferred_work
);
5353 mutex_unlock(&binder_deferred_lock
);
5356 static void print_binder_transaction_ilocked(struct seq_file
*m
,
5357 struct binder_proc
*proc
,
5359 struct binder_transaction
*t
)
5361 struct binder_proc
*to_proc
;
5362 struct binder_buffer
*buffer
= t
->buffer
;
5364 spin_lock(&t
->lock
);
5365 to_proc
= t
->to_proc
;
5367 "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %d:%d r%d",
5368 prefix
, t
->debug_id
, t
,
5369 t
->from
? t
->from
->proc
->pid
: 0,
5370 t
->from
? t
->from
->pid
: 0,
5371 to_proc
? to_proc
->pid
: 0,
5372 t
->to_thread
? t
->to_thread
->pid
: 0,
5373 t
->code
, t
->flags
, t
->priority
.sched_policy
,
5374 t
->priority
.prio
, t
->need_reply
);
5375 spin_unlock(&t
->lock
);
5377 if (proc
!= to_proc
) {
5379 * Can only safely deref buffer if we are holding the
5380 * correct proc inner lock for this node
5386 if (buffer
== NULL
) {
5387 seq_puts(m
, " buffer free\n");
5390 if (buffer
->target_node
)
5391 seq_printf(m
, " node %d", buffer
->target_node
->debug_id
);
5392 seq_printf(m
, " size %zd:%zd data %p\n",
5393 buffer
->data_size
, buffer
->offsets_size
,
5397 static void print_binder_work_ilocked(struct seq_file
*m
,
5398 struct binder_proc
*proc
,
5400 const char *transaction_prefix
,
5401 struct binder_work
*w
)
5403 struct binder_node
*node
;
5404 struct binder_transaction
*t
;
5407 case BINDER_WORK_TRANSACTION
:
5408 t
= container_of(w
, struct binder_transaction
, work
);
5409 print_binder_transaction_ilocked(
5410 m
, proc
, transaction_prefix
, t
);
5412 case BINDER_WORK_RETURN_ERROR
: {
5413 struct binder_error
*e
= container_of(
5414 w
, struct binder_error
, work
);
5416 seq_printf(m
, "%stransaction error: %u\n",
5419 case BINDER_WORK_TRANSACTION_COMPLETE
:
5420 seq_printf(m
, "%stransaction complete\n", prefix
);
5422 case BINDER_WORK_NODE
:
5423 node
= container_of(w
, struct binder_node
, work
);
5424 seq_printf(m
, "%snode work %d: u%016llx c%016llx\n",
5425 prefix
, node
->debug_id
,
5426 (u64
)node
->ptr
, (u64
)node
->cookie
);
5428 case BINDER_WORK_DEAD_BINDER
:
5429 seq_printf(m
, "%shas dead binder\n", prefix
);
5431 case BINDER_WORK_DEAD_BINDER_AND_CLEAR
:
5432 seq_printf(m
, "%shas cleared dead binder\n", prefix
);
5434 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION
:
5435 seq_printf(m
, "%shas cleared death notification\n", prefix
);
5438 seq_printf(m
, "%sunknown work: type %d\n", prefix
, w
->type
);
5443 static void print_binder_thread_ilocked(struct seq_file
*m
,
5444 struct binder_thread
*thread
,
5447 struct binder_transaction
*t
;
5448 struct binder_work
*w
;
5449 size_t start_pos
= m
->count
;
5452 seq_printf(m
, " thread %d: l %02x need_return %d tr %d\n",
5453 thread
->pid
, thread
->looper
,
5454 thread
->looper_need_return
,
5455 atomic_read(&thread
->tmp_ref
));
5456 header_pos
= m
->count
;
5457 t
= thread
->transaction_stack
;
5459 if (t
->from
== thread
) {
5460 print_binder_transaction_ilocked(m
, thread
->proc
,
5461 " outgoing transaction", t
);
5463 } else if (t
->to_thread
== thread
) {
5464 print_binder_transaction_ilocked(m
, thread
->proc
,
5465 " incoming transaction", t
);
5468 print_binder_transaction_ilocked(m
, thread
->proc
,
5469 " bad transaction", t
);
5473 list_for_each_entry(w
, &thread
->todo
, entry
) {
5474 print_binder_work_ilocked(m
, thread
->proc
, " ",
5475 " pending transaction", w
);
5477 if (!print_always
&& m
->count
== header_pos
)
5478 m
->count
= start_pos
;
5481 static void print_binder_node_nilocked(struct seq_file
*m
,
5482 struct binder_node
*node
)
5484 struct binder_ref
*ref
;
5485 struct binder_work
*w
;
5489 hlist_for_each_entry(ref
, &node
->refs
, node_entry
)
5492 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",
5493 node
->debug_id
, (u64
)node
->ptr
, (u64
)node
->cookie
,
5494 node
->sched_policy
, node
->min_priority
,
5495 node
->has_strong_ref
, node
->has_weak_ref
,
5496 node
->local_strong_refs
, node
->local_weak_refs
,
5497 node
->internal_strong_refs
, count
, node
->tmp_refs
);
5499 seq_puts(m
, " proc");
5500 hlist_for_each_entry(ref
, &node
->refs
, node_entry
)
5501 seq_printf(m
, " %d", ref
->proc
->pid
);
5505 list_for_each_entry(w
, &node
->async_todo
, entry
)
5506 print_binder_work_ilocked(m
, node
->proc
, " ",
5507 " pending async transaction", w
);
5511 static void print_binder_ref_olocked(struct seq_file
*m
,
5512 struct binder_ref
*ref
)
5514 binder_node_lock(ref
->node
);
5515 seq_printf(m
, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
5516 ref
->data
.debug_id
, ref
->data
.desc
,
5517 ref
->node
->proc
? "" : "dead ",
5518 ref
->node
->debug_id
, ref
->data
.strong
,
5519 ref
->data
.weak
, ref
->death
);
5520 binder_node_unlock(ref
->node
);
5523 static void print_binder_proc(struct seq_file
*m
,
5524 struct binder_proc
*proc
, int print_all
)
5526 struct binder_work
*w
;
5528 size_t start_pos
= m
->count
;
5530 struct binder_node
*last_node
= NULL
;
5532 seq_printf(m
, "proc %d\n", proc
->pid
);
5533 seq_printf(m
, "context %s\n", proc
->context
->name
);
5534 header_pos
= m
->count
;
5536 binder_inner_proc_lock(proc
);
5537 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
))
5538 print_binder_thread_ilocked(m
, rb_entry(n
, struct binder_thread
,
5539 rb_node
), print_all
);
5541 for (n
= rb_first(&proc
->nodes
); n
!= NULL
; n
= rb_next(n
)) {
5542 struct binder_node
*node
= rb_entry(n
, struct binder_node
,
5545 * take a temporary reference on the node so it
5546 * survives and isn't removed from the tree
5547 * while we print it.
5549 binder_inc_node_tmpref_ilocked(node
);
5550 /* Need to drop inner lock to take node lock */
5551 binder_inner_proc_unlock(proc
);
5553 binder_put_node(last_node
);
5554 binder_node_inner_lock(node
);
5555 print_binder_node_nilocked(m
, node
);
5556 binder_node_inner_unlock(node
);
5558 binder_inner_proc_lock(proc
);
5560 binder_inner_proc_unlock(proc
);
5562 binder_put_node(last_node
);
5565 binder_proc_lock(proc
);
5566 for (n
= rb_first(&proc
->refs_by_desc
);
5569 print_binder_ref_olocked(m
, rb_entry(n
,
5572 binder_proc_unlock(proc
);
5574 binder_alloc_print_allocated(m
, &proc
->alloc
);
5575 binder_inner_proc_lock(proc
);
5576 list_for_each_entry(w
, &proc
->todo
, entry
)
5577 print_binder_work_ilocked(m
, proc
, " ",
5578 " pending transaction", w
);
5579 list_for_each_entry(w
, &proc
->delivered_death
, entry
) {
5580 seq_puts(m
, " has delivered dead binder\n");
5583 binder_inner_proc_unlock(proc
);
5584 if (!print_all
&& m
->count
== header_pos
)
5585 m
->count
= start_pos
;
5588 static const char * const binder_return_strings
[] = {
5593 "BR_ACQUIRE_RESULT",
5595 "BR_TRANSACTION_COMPLETE",
5600 "BR_ATTEMPT_ACQUIRE",
5605 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
5609 static const char * const binder_command_strings
[] = {
5612 "BC_ACQUIRE_RESULT",
5620 "BC_ATTEMPT_ACQUIRE",
5621 "BC_REGISTER_LOOPER",
5624 "BC_REQUEST_DEATH_NOTIFICATION",
5625 "BC_CLEAR_DEATH_NOTIFICATION",
5626 "BC_DEAD_BINDER_DONE",
5627 "BC_TRANSACTION_SG",
5631 static const char * const binder_objstat_strings
[] = {
5638 "transaction_complete"
5641 static void print_binder_stats(struct seq_file
*m
, const char *prefix
,
5642 struct binder_stats
*stats
)
5646 BUILD_BUG_ON(ARRAY_SIZE(stats
->bc
) !=
5647 ARRAY_SIZE(binder_command_strings
));
5648 for (i
= 0; i
< ARRAY_SIZE(stats
->bc
); i
++) {
5649 int temp
= atomic_read(&stats
->bc
[i
]);
5652 seq_printf(m
, "%s%s: %d\n", prefix
,
5653 binder_command_strings
[i
], temp
);
5656 BUILD_BUG_ON(ARRAY_SIZE(stats
->br
) !=
5657 ARRAY_SIZE(binder_return_strings
));
5658 for (i
= 0; i
< ARRAY_SIZE(stats
->br
); i
++) {
5659 int temp
= atomic_read(&stats
->br
[i
]);
5662 seq_printf(m
, "%s%s: %d\n", prefix
,
5663 binder_return_strings
[i
], temp
);
5666 BUILD_BUG_ON(ARRAY_SIZE(stats
->obj_created
) !=
5667 ARRAY_SIZE(binder_objstat_strings
));
5668 BUILD_BUG_ON(ARRAY_SIZE(stats
->obj_created
) !=
5669 ARRAY_SIZE(stats
->obj_deleted
));
5670 for (i
= 0; i
< ARRAY_SIZE(stats
->obj_created
); i
++) {
5671 int created
= atomic_read(&stats
->obj_created
[i
]);
5672 int deleted
= atomic_read(&stats
->obj_deleted
[i
]);
5674 if (created
|| deleted
)
5675 seq_printf(m
, "%s%s: active %d total %d\n",
5677 binder_objstat_strings
[i
],
5683 static void print_binder_proc_stats(struct seq_file
*m
,
5684 struct binder_proc
*proc
)
5686 struct binder_work
*w
;
5687 struct binder_thread
*thread
;
5689 int count
, strong
, weak
, ready_threads
;
5690 size_t free_async_space
=
5691 binder_alloc_get_free_async_space(&proc
->alloc
);
5693 seq_printf(m
, "proc %d\n", proc
->pid
);
5694 seq_printf(m
, "context %s\n", proc
->context
->name
);
5697 binder_inner_proc_lock(proc
);
5698 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
))
5701 list_for_each_entry(thread
, &proc
->waiting_threads
, waiting_thread_node
)
5704 seq_printf(m
, " threads: %d\n", count
);
5705 seq_printf(m
, " requested threads: %d+%d/%d\n"
5706 " ready threads %d\n"
5707 " free async space %zd\n", proc
->requested_threads
,
5708 proc
->requested_threads_started
, proc
->max_threads
,
5712 for (n
= rb_first(&proc
->nodes
); n
!= NULL
; n
= rb_next(n
))
5714 binder_inner_proc_unlock(proc
);
5715 seq_printf(m
, " nodes: %d\n", count
);
5719 binder_proc_lock(proc
);
5720 for (n
= rb_first(&proc
->refs_by_desc
); n
!= NULL
; n
= rb_next(n
)) {
5721 struct binder_ref
*ref
= rb_entry(n
, struct binder_ref
,
5724 strong
+= ref
->data
.strong
;
5725 weak
+= ref
->data
.weak
;
5727 binder_proc_unlock(proc
);
5728 seq_printf(m
, " refs: %d s %d w %d\n", count
, strong
, weak
);
5730 count
= binder_alloc_get_allocated_count(&proc
->alloc
);
5731 seq_printf(m
, " buffers: %d\n", count
);
5734 binder_inner_proc_lock(proc
);
5735 list_for_each_entry(w
, &proc
->todo
, entry
) {
5736 if (w
->type
== BINDER_WORK_TRANSACTION
)
5739 binder_inner_proc_unlock(proc
);
5740 seq_printf(m
, " pending transactions: %d\n", count
);
5742 print_binder_stats(m
, " ", &proc
->stats
);
5746 static int binder_state_show(struct seq_file
*m
, void *unused
)
5748 struct binder_proc
*proc
;
5749 struct binder_node
*node
;
5750 struct binder_node
*last_node
= NULL
;
5752 seq_puts(m
, "binder state:\n");
5754 spin_lock(&binder_dead_nodes_lock
);
5755 if (!hlist_empty(&binder_dead_nodes
))
5756 seq_puts(m
, "dead nodes:\n");
5757 hlist_for_each_entry(node
, &binder_dead_nodes
, dead_node
) {
5759 * take a temporary reference on the node so it
5760 * survives and isn't removed from the list
5761 * while we print it.
5764 spin_unlock(&binder_dead_nodes_lock
);
5766 binder_put_node(last_node
);
5767 binder_node_lock(node
);
5768 print_binder_node_nilocked(m
, node
);
5769 binder_node_unlock(node
);
5771 spin_lock(&binder_dead_nodes_lock
);
5773 spin_unlock(&binder_dead_nodes_lock
);
5775 binder_put_node(last_node
);
5777 mutex_lock(&binder_procs_lock
);
5778 hlist_for_each_entry(proc
, &binder_procs
, proc_node
)
5779 print_binder_proc(m
, proc
, 1);
5780 mutex_unlock(&binder_procs_lock
);
5785 static int binder_stats_show(struct seq_file
*m
, void *unused
)
5787 struct binder_proc
*proc
;
5789 seq_puts(m
, "binder stats:\n");
5791 print_binder_stats(m
, "", &binder_stats
);
5793 mutex_lock(&binder_procs_lock
);
5794 hlist_for_each_entry(proc
, &binder_procs
, proc_node
)
5795 print_binder_proc_stats(m
, proc
);
5796 mutex_unlock(&binder_procs_lock
);
5801 static int binder_transactions_show(struct seq_file
*m
, void *unused
)
5803 struct binder_proc
*proc
;
5805 seq_puts(m
, "binder transactions:\n");
5806 mutex_lock(&binder_procs_lock
);
5807 hlist_for_each_entry(proc
, &binder_procs
, proc_node
)
5808 print_binder_proc(m
, proc
, 0);
5809 mutex_unlock(&binder_procs_lock
);
5814 static int binder_proc_show(struct seq_file
*m
, void *unused
)
5816 struct binder_proc
*itr
;
5817 int pid
= (unsigned long)m
->private;
5819 mutex_lock(&binder_procs_lock
);
5820 hlist_for_each_entry(itr
, &binder_procs
, proc_node
) {
5821 if (itr
->pid
== pid
) {
5822 seq_puts(m
, "binder proc state:\n");
5823 print_binder_proc(m
, itr
, 1);
5826 mutex_unlock(&binder_procs_lock
);
5831 static void print_binder_transaction_log_entry(struct seq_file
*m
,
5832 struct binder_transaction_log_entry
*e
)
5834 int debug_id
= READ_ONCE(e
->debug_id_done
);
5836 * read barrier to guarantee debug_id_done read before
5837 * we print the log values
5841 "%d: %s from %d:%d to %d:%d context %s node %d handle %d size %d:%d ret %d/%d l=%d",
5842 e
->debug_id
, (e
->call_type
== 2) ? "reply" :
5843 ((e
->call_type
== 1) ? "async" : "call "), e
->from_proc
,
5844 e
->from_thread
, e
->to_proc
, e
->to_thread
, e
->context_name
,
5845 e
->to_node
, e
->target_handle
, e
->data_size
, e
->offsets_size
,
5846 e
->return_error
, e
->return_error_param
,
5847 e
->return_error_line
);
5849 * read-barrier to guarantee read of debug_id_done after
5850 * done printing the fields of the entry
5853 seq_printf(m
, debug_id
&& debug_id
== READ_ONCE(e
->debug_id_done
) ?
5854 "\n" : " (incomplete)\n");
5857 static int binder_transaction_log_show(struct seq_file
*m
, void *unused
)
5859 struct binder_transaction_log
*log
= m
->private;
5860 unsigned int log_cur
= atomic_read(&log
->cur
);
5865 count
= log_cur
+ 1;
5866 cur
= count
< ARRAY_SIZE(log
->entry
) && !log
->full
?
5867 0 : count
% ARRAY_SIZE(log
->entry
);
5868 if (count
> ARRAY_SIZE(log
->entry
) || log
->full
)
5869 count
= ARRAY_SIZE(log
->entry
);
5870 for (i
= 0; i
< count
; i
++) {
5871 unsigned int index
= cur
++ % ARRAY_SIZE(log
->entry
);
5873 print_binder_transaction_log_entry(m
, &log
->entry
[index
]);
5878 static const struct file_operations binder_fops
= {
5879 .owner
= THIS_MODULE
,
5880 .poll
= binder_poll
,
5881 .unlocked_ioctl
= binder_ioctl
,
5882 .compat_ioctl
= binder_ioctl
,
5883 .mmap
= binder_mmap
,
5884 .open
= binder_open
,
5885 .flush
= binder_flush
,
5886 .release
= binder_release
,
5889 BINDER_DEBUG_ENTRY(state
);
5890 BINDER_DEBUG_ENTRY(stats
);
5891 BINDER_DEBUG_ENTRY(transactions
);
5892 BINDER_DEBUG_ENTRY(transaction_log
);
5894 static int __init
init_binder_device(const char *name
)
5897 struct binder_device
*binder_device
;
5899 binder_device
= kzalloc(sizeof(*binder_device
), GFP_KERNEL
);
5903 binder_device
->miscdev
.fops
= &binder_fops
;
5904 binder_device
->miscdev
.minor
= MISC_DYNAMIC_MINOR
;
5905 binder_device
->miscdev
.name
= name
;
5907 binder_device
->context
.binder_context_mgr_uid
= INVALID_UID
;
5908 binder_device
->context
.name
= name
;
5909 mutex_init(&binder_device
->context
.context_mgr_node_lock
);
5911 ret
= misc_register(&binder_device
->miscdev
);
5913 kfree(binder_device
);
5917 hlist_add_head(&binder_device
->hlist
, &binder_devices
);
5922 static int __init
binder_init(void)
5925 char *device_name
, *device_names
;
5926 struct binder_device
*device
;
5927 struct hlist_node
*tmp
;
5929 atomic_set(&binder_transaction_log
.cur
, ~0U);
5930 atomic_set(&binder_transaction_log_failed
.cur
, ~0U);
5931 binder_deferred_workqueue
= create_singlethread_workqueue("binder");
5932 if (!binder_deferred_workqueue
)
5935 binder_debugfs_dir_entry_root
= debugfs_create_dir("binder", NULL
);
5936 if (binder_debugfs_dir_entry_root
)
5937 binder_debugfs_dir_entry_proc
= debugfs_create_dir("proc",
5938 binder_debugfs_dir_entry_root
);
5940 if (binder_debugfs_dir_entry_root
) {
5941 debugfs_create_file("state",
5943 binder_debugfs_dir_entry_root
,
5945 &binder_state_fops
);
5946 debugfs_create_file("stats",
5948 binder_debugfs_dir_entry_root
,
5950 &binder_stats_fops
);
5951 debugfs_create_file("transactions",
5953 binder_debugfs_dir_entry_root
,
5955 &binder_transactions_fops
);
5956 debugfs_create_file("transaction_log",
5958 binder_debugfs_dir_entry_root
,
5959 &binder_transaction_log
,
5960 &binder_transaction_log_fops
);
5961 debugfs_create_file("failed_transaction_log",
5963 binder_debugfs_dir_entry_root
,
5964 &binder_transaction_log_failed
,
5965 &binder_transaction_log_fops
);
5969 * Copy the module_parameter string, because we don't want to
5970 * tokenize it in-place.
5972 device_names
= kzalloc(strlen(binder_devices_param
) + 1, GFP_KERNEL
);
5973 if (!device_names
) {
5975 goto err_alloc_device_names_failed
;
5977 strcpy(device_names
, binder_devices_param
);
5979 while ((device_name
= strsep(&device_names
, ","))) {
5980 ret
= init_binder_device(device_name
);
5982 goto err_init_binder_device_failed
;
5987 err_init_binder_device_failed
:
5988 hlist_for_each_entry_safe(device
, tmp
, &binder_devices
, hlist
) {
5989 misc_deregister(&device
->miscdev
);
5990 hlist_del(&device
->hlist
);
5993 err_alloc_device_names_failed
:
5994 debugfs_remove_recursive(binder_debugfs_dir_entry_root
);
5996 destroy_workqueue(binder_deferred_workqueue
);
6001 device_initcall(binder_init
);
6003 #define CREATE_TRACE_POINTS
6004 #include "binder_trace.h"
6006 MODULE_LICENSE("GPL v2");