1 // SPDX-License-Identifier: GPL-2.0-only
4 * Android IPC Subsystem
6 * Copyright (C) 2007-2008 Google, Inc.
12 * There are 3 main spinlocks which must be acquired in the
15 * 1) proc->outer_lock : protects binder_ref
16 * binder_proc_lock() and binder_proc_unlock() are
18 * 2) node->lock : protects most fields of binder_node.
19 * binder_node_lock() and binder_node_unlock() are
21 * 3) proc->inner_lock : protects the thread and node lists
22 * (proc->threads, proc->waiting_threads, proc->nodes)
23 * and all todo lists associated with the binder_proc
24 * (proc->todo, thread->todo, proc->delivered_death and
25 * node->async_todo), as well as thread->transaction_stack
26 * binder_inner_proc_lock() and binder_inner_proc_unlock()
29 * Any lock under procA must never be nested under any lock at the same
30 * level or below on procB.
32 * Functions that require a lock held on entry indicate which lock
33 * in the suffix of the function name:
35 * foo_olocked() : requires node->outer_lock
36 * foo_nlocked() : requires node->lock
37 * foo_ilocked() : requires proc->inner_lock
38 * foo_oilocked(): requires proc->outer_lock and proc->inner_lock
39 * foo_nilocked(): requires node->lock and proc->inner_lock
43 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
45 #include <linux/fdtable.h>
46 #include <linux/file.h>
47 #include <linux/freezer.h>
49 #include <linux/list.h>
50 #include <linux/miscdevice.h>
51 #include <linux/module.h>
52 #include <linux/mutex.h>
53 #include <linux/nsproxy.h>
54 #include <linux/poll.h>
55 #include <linux/debugfs.h>
56 #include <linux/rbtree.h>
57 #include <linux/sched/signal.h>
58 #include <linux/sched/mm.h>
59 #include <linux/seq_file.h>
60 #include <linux/string.h>
61 #include <linux/uaccess.h>
62 #include <linux/pid_namespace.h>
63 #include <linux/security.h>
64 #include <linux/spinlock.h>
65 #include <linux/ratelimit.h>
66 #include <linux/syscalls.h>
67 #include <linux/task_work.h>
68 #include <linux/sizes.h>
70 #include <uapi/linux/android/binder.h>
71 #include <uapi/linux/android/binderfs.h>
73 #include <asm/cacheflush.h>
75 #include "binder_alloc.h"
76 #include "binder_internal.h"
77 #include "binder_trace.h"
79 static HLIST_HEAD(binder_deferred_list);
80 static DEFINE_MUTEX(binder_deferred_lock);
82 static HLIST_HEAD(binder_devices);
83 static HLIST_HEAD(binder_procs);
84 static DEFINE_MUTEX(binder_procs_lock);
86 static HLIST_HEAD(binder_dead_nodes);
87 static DEFINE_SPINLOCK(binder_dead_nodes_lock);
89 static struct dentry *binder_debugfs_dir_entry_root;
90 static struct dentry *binder_debugfs_dir_entry_proc;
91 static atomic_t binder_last_id;
93 static int proc_show(struct seq_file *m, void *unused);
94 DEFINE_SHOW_ATTRIBUTE(proc);
96 #define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
99 BINDER_DEBUG_USER_ERROR = 1U << 0,
100 BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1,
101 BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2,
102 BINDER_DEBUG_OPEN_CLOSE = 1U << 3,
103 BINDER_DEBUG_DEAD_BINDER = 1U << 4,
104 BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5,
105 BINDER_DEBUG_READ_WRITE = 1U << 6,
106 BINDER_DEBUG_USER_REFS = 1U << 7,
107 BINDER_DEBUG_THREADS = 1U << 8,
108 BINDER_DEBUG_TRANSACTION = 1U << 9,
109 BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10,
110 BINDER_DEBUG_FREE_BUFFER = 1U << 11,
111 BINDER_DEBUG_INTERNAL_REFS = 1U << 12,
112 BINDER_DEBUG_PRIORITY_CAP = 1U << 13,
113 BINDER_DEBUG_SPINLOCKS = 1U << 14,
115 static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR |
116 BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION;
117 module_param_named(debug_mask, binder_debug_mask, uint, 0644);
119 char *binder_devices_param = CONFIG_ANDROID_BINDER_DEVICES;
120 module_param_named(devices, binder_devices_param, charp, 0444);
122 static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
123 static int binder_stop_on_user_error;
125 static int binder_set_stop_on_user_error(const char *val,
126 const struct kernel_param *kp)
130 ret = param_set_int(val, kp);
131 if (binder_stop_on_user_error < 2)
132 wake_up(&binder_user_error_wait);
135 module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
136 param_get_int, &binder_stop_on_user_error, 0644);
138 #define binder_debug(mask, x...) \
140 if (binder_debug_mask & mask) \
141 pr_info_ratelimited(x); \
144 #define binder_user_error(x...) \
146 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
147 pr_info_ratelimited(x); \
148 if (binder_stop_on_user_error) \
149 binder_stop_on_user_error = 2; \
152 #define to_flat_binder_object(hdr) \
153 container_of(hdr, struct flat_binder_object, hdr)
155 #define to_binder_fd_object(hdr) container_of(hdr, struct binder_fd_object, hdr)
157 #define to_binder_buffer_object(hdr) \
158 container_of(hdr, struct binder_buffer_object, hdr)
160 #define to_binder_fd_array_object(hdr) \
161 container_of(hdr, struct binder_fd_array_object, hdr)
163 enum binder_stat_types {
169 BINDER_STAT_TRANSACTION,
170 BINDER_STAT_TRANSACTION_COMPLETE,
174 struct binder_stats {
175 atomic_t br[_IOC_NR(BR_FAILED_REPLY) + 1];
176 atomic_t bc[_IOC_NR(BC_REPLY_SG) + 1];
177 atomic_t obj_created[BINDER_STAT_COUNT];
178 atomic_t obj_deleted[BINDER_STAT_COUNT];
181 static struct binder_stats binder_stats;
183 static inline void binder_stats_deleted(enum binder_stat_types type)
185 atomic_inc(&binder_stats.obj_deleted[type]);
188 static inline void binder_stats_created(enum binder_stat_types type)
190 atomic_inc(&binder_stats.obj_created[type]);
193 struct binder_transaction_log binder_transaction_log;
194 struct binder_transaction_log binder_transaction_log_failed;
196 static struct binder_transaction_log_entry *binder_transaction_log_add(
197 struct binder_transaction_log *log)
199 struct binder_transaction_log_entry *e;
200 unsigned int cur = atomic_inc_return(&log->cur);
202 if (cur >= ARRAY_SIZE(log->entry))
204 e = &log->entry[cur % ARRAY_SIZE(log->entry)];
205 WRITE_ONCE(e->debug_id_done, 0);
207 * write-barrier to synchronize access to e->debug_id_done.
208 * We make sure the initialized 0 value is seen before
209 * memset() other fields are zeroed by memset.
212 memset(e, 0, sizeof(*e));
217 * struct binder_work - work enqueued on a worklist
218 * @entry: node enqueued on list
219 * @type: type of work to be performed
221 * There are separate work lists for proc, thread, and node (async).
224 struct list_head entry;
226 enum binder_work_type {
227 BINDER_WORK_TRANSACTION = 1,
228 BINDER_WORK_TRANSACTION_COMPLETE,
229 BINDER_WORK_RETURN_ERROR,
231 BINDER_WORK_DEAD_BINDER,
232 BINDER_WORK_DEAD_BINDER_AND_CLEAR,
233 BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
237 struct binder_error {
238 struct binder_work work;
243 * struct binder_node - binder node bookkeeping
244 * @debug_id: unique ID for debugging
245 * (invariant after initialized)
246 * @lock: lock for node fields
247 * @work: worklist element for node work
248 * (protected by @proc->inner_lock)
249 * @rb_node: element for proc->nodes tree
250 * (protected by @proc->inner_lock)
251 * @dead_node: element for binder_dead_nodes list
252 * (protected by binder_dead_nodes_lock)
253 * @proc: binder_proc that owns this node
254 * (invariant after initialized)
255 * @refs: list of references on this node
256 * (protected by @lock)
257 * @internal_strong_refs: used to take strong references when
258 * initiating a transaction
259 * (protected by @proc->inner_lock if @proc
261 * @local_weak_refs: weak user refs from local process
262 * (protected by @proc->inner_lock if @proc
264 * @local_strong_refs: strong user refs from local process
265 * (protected by @proc->inner_lock if @proc
267 * @tmp_refs: temporary kernel refs
268 * (protected by @proc->inner_lock while @proc
269 * is valid, and by binder_dead_nodes_lock
270 * if @proc is NULL. During inc/dec and node release
271 * it is also protected by @lock to provide safety
272 * as the node dies and @proc becomes NULL)
273 * @ptr: userspace pointer for node
274 * (invariant, no lock needed)
275 * @cookie: userspace cookie for node
276 * (invariant, no lock needed)
277 * @has_strong_ref: userspace notified of strong ref
278 * (protected by @proc->inner_lock if @proc
280 * @pending_strong_ref: userspace has acked notification of strong ref
281 * (protected by @proc->inner_lock if @proc
283 * @has_weak_ref: userspace notified of weak ref
284 * (protected by @proc->inner_lock if @proc
286 * @pending_weak_ref: userspace has acked notification of weak ref
287 * (protected by @proc->inner_lock if @proc
289 * @has_async_transaction: async transaction to node in progress
290 * (protected by @lock)
291 * @accept_fds: file descriptor operations supported for node
292 * (invariant after initialized)
293 * @min_priority: minimum scheduling priority
294 * (invariant after initialized)
295 * @txn_security_ctx: require sender's security context
296 * (invariant after initialized)
297 * @async_todo: list of async work items
298 * (protected by @proc->inner_lock)
300 * Bookkeeping structure for binder nodes.
305 struct binder_work work;
307 struct rb_node rb_node;
308 struct hlist_node dead_node;
310 struct binder_proc *proc;
311 struct hlist_head refs;
312 int internal_strong_refs;
314 int local_strong_refs;
316 binder_uintptr_t ptr;
317 binder_uintptr_t cookie;
320 * bitfield elements protected by
324 u8 pending_strong_ref:1;
326 u8 pending_weak_ref:1;
330 * invariant after initialization
333 u8 txn_security_ctx:1;
336 bool has_async_transaction;
337 struct list_head async_todo;
340 struct binder_ref_death {
342 * @work: worklist element for death notifications
343 * (protected by inner_lock of the proc that
344 * this ref belongs to)
346 struct binder_work work;
347 binder_uintptr_t cookie;
351 * struct binder_ref_data - binder_ref counts and id
352 * @debug_id: unique ID for the ref
353 * @desc: unique userspace handle for ref
354 * @strong: strong ref count (debugging only if not locked)
355 * @weak: weak ref count (debugging only if not locked)
357 * Structure to hold ref count and ref id information. Since
358 * the actual ref can only be accessed with a lock, this structure
359 * is used to return information about the ref to callers of
360 * ref inc/dec functions.
362 struct binder_ref_data {
370 * struct binder_ref - struct to track references on nodes
371 * @data: binder_ref_data containing id, handle, and current refcounts
372 * @rb_node_desc: node for lookup by @data.desc in proc's rb_tree
373 * @rb_node_node: node for lookup by @node in proc's rb_tree
374 * @node_entry: list entry for node->refs list in target node
375 * (protected by @node->lock)
376 * @proc: binder_proc containing ref
377 * @node: binder_node of target node. When cleaning up a
378 * ref for deletion in binder_cleanup_ref, a non-NULL
379 * @node indicates the node must be freed
380 * @death: pointer to death notification (ref_death) if requested
381 * (protected by @node->lock)
383 * Structure to track references from procA to target node (on procB). This
384 * structure is unsafe to access without holding @proc->outer_lock.
387 /* Lookups needed: */
388 /* node + proc => ref (transaction) */
389 /* desc + proc => ref (transaction, inc/dec ref) */
390 /* node => refs + procs (proc exit) */
391 struct binder_ref_data data;
392 struct rb_node rb_node_desc;
393 struct rb_node rb_node_node;
394 struct hlist_node node_entry;
395 struct binder_proc *proc;
396 struct binder_node *node;
397 struct binder_ref_death *death;
400 enum binder_deferred_state {
401 BINDER_DEFERRED_FLUSH = 0x01,
402 BINDER_DEFERRED_RELEASE = 0x02,
406 * struct binder_proc - binder process bookkeeping
407 * @proc_node: element for binder_procs list
408 * @threads: rbtree of binder_threads in this proc
409 * (protected by @inner_lock)
410 * @nodes: rbtree of binder nodes associated with
411 * this proc ordered by node->ptr
412 * (protected by @inner_lock)
413 * @refs_by_desc: rbtree of refs ordered by ref->desc
414 * (protected by @outer_lock)
415 * @refs_by_node: rbtree of refs ordered by ref->node
416 * (protected by @outer_lock)
417 * @waiting_threads: threads currently waiting for proc work
418 * (protected by @inner_lock)
419 * @pid PID of group_leader of process
420 * (invariant after initialized)
421 * @tsk task_struct for group_leader of process
422 * (invariant after initialized)
423 * @deferred_work_node: element for binder_deferred_list
424 * (protected by binder_deferred_lock)
425 * @deferred_work: bitmap of deferred work to perform
426 * (protected by binder_deferred_lock)
427 * @is_dead: process is dead and awaiting free
428 * when outstanding transactions are cleaned up
429 * (protected by @inner_lock)
430 * @todo: list of work for this process
431 * (protected by @inner_lock)
432 * @stats: per-process binder statistics
433 * (atomics, no lock needed)
434 * @delivered_death: list of delivered death notification
435 * (protected by @inner_lock)
436 * @max_threads: cap on number of binder threads
437 * (protected by @inner_lock)
438 * @requested_threads: number of binder threads requested but not
439 * yet started. In current implementation, can
441 * (protected by @inner_lock)
442 * @requested_threads_started: number binder threads started
443 * (protected by @inner_lock)
444 * @tmp_ref: temporary reference to indicate proc is in use
445 * (protected by @inner_lock)
446 * @default_priority: default scheduler priority
447 * (invariant after initialized)
448 * @debugfs_entry: debugfs node
449 * @alloc: binder allocator bookkeeping
450 * @context: binder_context for this proc
451 * (invariant after initialized)
452 * @inner_lock: can nest under outer_lock and/or node lock
453 * @outer_lock: no nesting under innor or node lock
454 * Lock order: 1) outer, 2) node, 3) inner
455 * @binderfs_entry: process-specific binderfs log file
457 * Bookkeeping structure for binder processes
460 struct hlist_node proc_node;
461 struct rb_root threads;
462 struct rb_root nodes;
463 struct rb_root refs_by_desc;
464 struct rb_root refs_by_node;
465 struct list_head waiting_threads;
467 struct task_struct *tsk;
468 struct hlist_node deferred_work_node;
472 struct list_head todo;
473 struct binder_stats stats;
474 struct list_head delivered_death;
476 int requested_threads;
477 int requested_threads_started;
479 long default_priority;
480 struct dentry *debugfs_entry;
481 struct binder_alloc alloc;
482 struct binder_context *context;
483 spinlock_t inner_lock;
484 spinlock_t outer_lock;
485 struct dentry *binderfs_entry;
489 BINDER_LOOPER_STATE_REGISTERED = 0x01,
490 BINDER_LOOPER_STATE_ENTERED = 0x02,
491 BINDER_LOOPER_STATE_EXITED = 0x04,
492 BINDER_LOOPER_STATE_INVALID = 0x08,
493 BINDER_LOOPER_STATE_WAITING = 0x10,
494 BINDER_LOOPER_STATE_POLL = 0x20,
498 * struct binder_thread - binder thread bookkeeping
499 * @proc: binder process for this thread
500 * (invariant after initialization)
501 * @rb_node: element for proc->threads rbtree
502 * (protected by @proc->inner_lock)
503 * @waiting_thread_node: element for @proc->waiting_threads list
504 * (protected by @proc->inner_lock)
505 * @pid: PID for this thread
506 * (invariant after initialization)
507 * @looper: bitmap of looping state
508 * (only accessed by this thread)
509 * @looper_needs_return: looping thread needs to exit driver
511 * @transaction_stack: stack of in-progress transactions for this thread
512 * (protected by @proc->inner_lock)
513 * @todo: list of work to do for this thread
514 * (protected by @proc->inner_lock)
515 * @process_todo: whether work in @todo should be processed
516 * (protected by @proc->inner_lock)
517 * @return_error: transaction errors reported by this thread
518 * (only accessed by this thread)
519 * @reply_error: transaction errors reported by target thread
520 * (protected by @proc->inner_lock)
521 * @wait: wait queue for thread work
522 * @stats: per-thread statistics
523 * (atomics, no lock needed)
524 * @tmp_ref: temporary reference to indicate thread is in use
525 * (atomic since @proc->inner_lock cannot
526 * always be acquired)
527 * @is_dead: thread is dead and awaiting free
528 * when outstanding transactions are cleaned up
529 * (protected by @proc->inner_lock)
531 * Bookkeeping structure for binder threads.
533 struct binder_thread {
534 struct binder_proc *proc;
535 struct rb_node rb_node;
536 struct list_head waiting_thread_node;
538 int looper; /* only modified by this thread */
539 bool looper_need_return; /* can be written by other thread */
540 struct binder_transaction *transaction_stack;
541 struct list_head todo;
543 struct binder_error return_error;
544 struct binder_error reply_error;
545 wait_queue_head_t wait;
546 struct binder_stats stats;
552 * struct binder_txn_fd_fixup - transaction fd fixup list element
553 * @fixup_entry: list entry
554 * @file: struct file to be associated with new fd
555 * @offset: offset in buffer data to this fixup
557 * List element for fd fixups in a transaction. Since file
558 * descriptors need to be allocated in the context of the
559 * target process, we pass each fd to be processed in this
562 struct binder_txn_fd_fixup {
563 struct list_head fixup_entry;
568 struct binder_transaction {
570 struct binder_work work;
571 struct binder_thread *from;
572 struct binder_transaction *from_parent;
573 struct binder_proc *to_proc;
574 struct binder_thread *to_thread;
575 struct binder_transaction *to_parent;
576 unsigned need_reply:1;
577 /* unsigned is_dead:1; */ /* not used at the moment */
579 struct binder_buffer *buffer;
585 struct list_head fd_fixups;
586 binder_uintptr_t security_ctx;
588 * @lock: protects @from, @to_proc, and @to_thread
590 * @from, @to_proc, and @to_thread can be set to NULL
591 * during thread teardown
597 * struct binder_object - union of flat binder object types
598 * @hdr: generic object header
599 * @fbo: binder object (nodes and refs)
600 * @fdo: file descriptor object
601 * @bbo: binder buffer pointer
602 * @fdao: file descriptor array
604 * Used for type-independent object copies
606 struct binder_object {
608 struct binder_object_header hdr;
609 struct flat_binder_object fbo;
610 struct binder_fd_object fdo;
611 struct binder_buffer_object bbo;
612 struct binder_fd_array_object fdao;
617 * binder_proc_lock() - Acquire outer lock for given binder_proc
618 * @proc: struct binder_proc to acquire
620 * Acquires proc->outer_lock. Used to protect binder_ref
621 * structures associated with the given proc.
623 #define binder_proc_lock(proc) _binder_proc_lock(proc, __LINE__)
625 _binder_proc_lock(struct binder_proc *proc, int line)
626 __acquires(&proc->outer_lock)
628 binder_debug(BINDER_DEBUG_SPINLOCKS,
629 "%s: line=%d\n", __func__, line);
630 spin_lock(&proc->outer_lock);
634 * binder_proc_unlock() - Release spinlock for given binder_proc
635 * @proc: struct binder_proc to acquire
637 * Release lock acquired via binder_proc_lock()
639 #define binder_proc_unlock(_proc) _binder_proc_unlock(_proc, __LINE__)
641 _binder_proc_unlock(struct binder_proc *proc, int line)
642 __releases(&proc->outer_lock)
644 binder_debug(BINDER_DEBUG_SPINLOCKS,
645 "%s: line=%d\n", __func__, line);
646 spin_unlock(&proc->outer_lock);
650 * binder_inner_proc_lock() - Acquire inner lock for given binder_proc
651 * @proc: struct binder_proc to acquire
653 * Acquires proc->inner_lock. Used to protect todo lists
655 #define binder_inner_proc_lock(proc) _binder_inner_proc_lock(proc, __LINE__)
657 _binder_inner_proc_lock(struct binder_proc *proc, int line)
658 __acquires(&proc->inner_lock)
660 binder_debug(BINDER_DEBUG_SPINLOCKS,
661 "%s: line=%d\n", __func__, line);
662 spin_lock(&proc->inner_lock);
666 * binder_inner_proc_unlock() - Release inner lock for given binder_proc
667 * @proc: struct binder_proc to acquire
669 * Release lock acquired via binder_inner_proc_lock()
671 #define binder_inner_proc_unlock(proc) _binder_inner_proc_unlock(proc, __LINE__)
673 _binder_inner_proc_unlock(struct binder_proc *proc, int line)
674 __releases(&proc->inner_lock)
676 binder_debug(BINDER_DEBUG_SPINLOCKS,
677 "%s: line=%d\n", __func__, line);
678 spin_unlock(&proc->inner_lock);
682 * binder_node_lock() - Acquire spinlock for given binder_node
683 * @node: struct binder_node to acquire
685 * Acquires node->lock. Used to protect binder_node fields
687 #define binder_node_lock(node) _binder_node_lock(node, __LINE__)
689 _binder_node_lock(struct binder_node *node, int line)
690 __acquires(&node->lock)
692 binder_debug(BINDER_DEBUG_SPINLOCKS,
693 "%s: line=%d\n", __func__, line);
694 spin_lock(&node->lock);
698 * binder_node_unlock() - Release spinlock for given binder_proc
699 * @node: struct binder_node to acquire
701 * Release lock acquired via binder_node_lock()
703 #define binder_node_unlock(node) _binder_node_unlock(node, __LINE__)
705 _binder_node_unlock(struct binder_node *node, int line)
706 __releases(&node->lock)
708 binder_debug(BINDER_DEBUG_SPINLOCKS,
709 "%s: line=%d\n", __func__, line);
710 spin_unlock(&node->lock);
714 * binder_node_inner_lock() - Acquire node and inner locks
715 * @node: struct binder_node to acquire
717 * Acquires node->lock. If node->proc also acquires
718 * proc->inner_lock. Used to protect binder_node fields
720 #define binder_node_inner_lock(node) _binder_node_inner_lock(node, __LINE__)
722 _binder_node_inner_lock(struct binder_node *node, int line)
723 __acquires(&node->lock) __acquires(&node->proc->inner_lock)
725 binder_debug(BINDER_DEBUG_SPINLOCKS,
726 "%s: line=%d\n", __func__, line);
727 spin_lock(&node->lock);
729 binder_inner_proc_lock(node->proc);
731 /* annotation for sparse */
732 __acquire(&node->proc->inner_lock);
736 * binder_node_unlock() - Release node and inner locks
737 * @node: struct binder_node to acquire
739 * Release lock acquired via binder_node_lock()
741 #define binder_node_inner_unlock(node) _binder_node_inner_unlock(node, __LINE__)
743 _binder_node_inner_unlock(struct binder_node *node, int line)
744 __releases(&node->lock) __releases(&node->proc->inner_lock)
746 struct binder_proc *proc = node->proc;
748 binder_debug(BINDER_DEBUG_SPINLOCKS,
749 "%s: line=%d\n", __func__, line);
751 binder_inner_proc_unlock(proc);
753 /* annotation for sparse */
754 __release(&node->proc->inner_lock);
755 spin_unlock(&node->lock);
758 static bool binder_worklist_empty_ilocked(struct list_head *list)
760 return list_empty(list);
764 * binder_worklist_empty() - Check if no items on the work list
765 * @proc: binder_proc associated with list
766 * @list: list to check
768 * Return: true if there are no items on list, else false
770 static bool binder_worklist_empty(struct binder_proc *proc,
771 struct list_head *list)
775 binder_inner_proc_lock(proc);
776 ret = binder_worklist_empty_ilocked(list);
777 binder_inner_proc_unlock(proc);
782 * binder_enqueue_work_ilocked() - Add an item to the work list
783 * @work: struct binder_work to add to list
784 * @target_list: list to add work to
786 * Adds the work to the specified list. Asserts that work
787 * is not already on a list.
789 * Requires the proc->inner_lock to be held.
792 binder_enqueue_work_ilocked(struct binder_work *work,
793 struct list_head *target_list)
795 BUG_ON(target_list == NULL);
796 BUG_ON(work->entry.next && !list_empty(&work->entry));
797 list_add_tail(&work->entry, target_list);
801 * binder_enqueue_deferred_thread_work_ilocked() - Add deferred thread work
802 * @thread: thread to queue work to
803 * @work: struct binder_work to add to list
805 * Adds the work to the todo list of the thread. Doesn't set the process_todo
806 * flag, which means that (if it wasn't already set) the thread will go to
807 * sleep without handling this work when it calls read.
809 * Requires the proc->inner_lock to be held.
812 binder_enqueue_deferred_thread_work_ilocked(struct binder_thread *thread,
813 struct binder_work *work)
815 WARN_ON(!list_empty(&thread->waiting_thread_node));
816 binder_enqueue_work_ilocked(work, &thread->todo);
820 * binder_enqueue_thread_work_ilocked() - Add an item to the thread work list
821 * @thread: thread to queue work to
822 * @work: struct binder_work to add to list
824 * Adds the work to the todo list of the thread, and enables processing
827 * Requires the proc->inner_lock to be held.
830 binder_enqueue_thread_work_ilocked(struct binder_thread *thread,
831 struct binder_work *work)
833 WARN_ON(!list_empty(&thread->waiting_thread_node));
834 binder_enqueue_work_ilocked(work, &thread->todo);
835 thread->process_todo = true;
839 * binder_enqueue_thread_work() - Add an item to the thread work list
840 * @thread: thread to queue work to
841 * @work: struct binder_work to add to list
843 * Adds the work to the todo list of the thread, and enables processing
847 binder_enqueue_thread_work(struct binder_thread *thread,
848 struct binder_work *work)
850 binder_inner_proc_lock(thread->proc);
851 binder_enqueue_thread_work_ilocked(thread, work);
852 binder_inner_proc_unlock(thread->proc);
856 binder_dequeue_work_ilocked(struct binder_work *work)
858 list_del_init(&work->entry);
862 * binder_dequeue_work() - Removes an item from the work list
863 * @proc: binder_proc associated with list
864 * @work: struct binder_work to remove from list
866 * Removes the specified work item from whatever list it is on.
867 * Can safely be called if work is not on any list.
870 binder_dequeue_work(struct binder_proc *proc, struct binder_work *work)
872 binder_inner_proc_lock(proc);
873 binder_dequeue_work_ilocked(work);
874 binder_inner_proc_unlock(proc);
877 static struct binder_work *binder_dequeue_work_head_ilocked(
878 struct list_head *list)
880 struct binder_work *w;
882 w = list_first_entry_or_null(list, struct binder_work, entry);
884 list_del_init(&w->entry);
889 binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
890 static void binder_free_thread(struct binder_thread *thread);
891 static void binder_free_proc(struct binder_proc *proc);
892 static void binder_inc_node_tmpref_ilocked(struct binder_node *node);
894 static bool binder_has_work_ilocked(struct binder_thread *thread,
897 return thread->process_todo ||
898 thread->looper_need_return ||
900 !binder_worklist_empty_ilocked(&thread->proc->todo));
903 static bool binder_has_work(struct binder_thread *thread, bool do_proc_work)
907 binder_inner_proc_lock(thread->proc);
908 has_work = binder_has_work_ilocked(thread, do_proc_work);
909 binder_inner_proc_unlock(thread->proc);
914 static bool binder_available_for_proc_work_ilocked(struct binder_thread *thread)
916 return !thread->transaction_stack &&
917 binder_worklist_empty_ilocked(&thread->todo) &&
918 (thread->looper & (BINDER_LOOPER_STATE_ENTERED |
919 BINDER_LOOPER_STATE_REGISTERED));
922 static void binder_wakeup_poll_threads_ilocked(struct binder_proc *proc,
926 struct binder_thread *thread;
928 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
929 thread = rb_entry(n, struct binder_thread, rb_node);
930 if (thread->looper & BINDER_LOOPER_STATE_POLL &&
931 binder_available_for_proc_work_ilocked(thread)) {
933 wake_up_interruptible_sync(&thread->wait);
935 wake_up_interruptible(&thread->wait);
941 * binder_select_thread_ilocked() - selects a thread for doing proc work.
942 * @proc: process to select a thread from
944 * Note that calling this function moves the thread off the waiting_threads
945 * list, so it can only be woken up by the caller of this function, or a
946 * signal. Therefore, callers *should* always wake up the thread this function
949 * Return: If there's a thread currently waiting for process work,
950 * returns that thread. Otherwise returns NULL.
952 static struct binder_thread *
953 binder_select_thread_ilocked(struct binder_proc *proc)
955 struct binder_thread *thread;
957 assert_spin_locked(&proc->inner_lock);
958 thread = list_first_entry_or_null(&proc->waiting_threads,
959 struct binder_thread,
960 waiting_thread_node);
963 list_del_init(&thread->waiting_thread_node);
969 * binder_wakeup_thread_ilocked() - wakes up a thread for doing proc work.
970 * @proc: process to wake up a thread in
971 * @thread: specific thread to wake-up (may be NULL)
972 * @sync: whether to do a synchronous wake-up
974 * This function wakes up a thread in the @proc process.
975 * The caller may provide a specific thread to wake-up in
976 * the @thread parameter. If @thread is NULL, this function
977 * will wake up threads that have called poll().
979 * Note that for this function to work as expected, callers
980 * should first call binder_select_thread() to find a thread
981 * to handle the work (if they don't have a thread already),
982 * and pass the result into the @thread parameter.
984 static void binder_wakeup_thread_ilocked(struct binder_proc *proc,
985 struct binder_thread *thread,
988 assert_spin_locked(&proc->inner_lock);
992 wake_up_interruptible_sync(&thread->wait);
994 wake_up_interruptible(&thread->wait);
998 /* Didn't find a thread waiting for proc work; this can happen
1000 * 1. All threads are busy handling transactions
1001 * In that case, one of those threads should call back into
1002 * the kernel driver soon and pick up this work.
1003 * 2. Threads are using the (e)poll interface, in which case
1004 * they may be blocked on the waitqueue without having been
1005 * added to waiting_threads. For this case, we just iterate
1006 * over all threads not handling transaction work, and
1007 * wake them all up. We wake all because we don't know whether
1008 * a thread that called into (e)poll is handling non-binder
1011 binder_wakeup_poll_threads_ilocked(proc, sync);
1014 static void binder_wakeup_proc_ilocked(struct binder_proc *proc)
1016 struct binder_thread *thread = binder_select_thread_ilocked(proc);
1018 binder_wakeup_thread_ilocked(proc, thread, /* sync = */false);
1021 static void binder_set_nice(long nice)
1025 if (can_nice(current, nice)) {
1026 set_user_nice(current, nice);
1029 min_nice = rlimit_to_nice(rlimit(RLIMIT_NICE));
1030 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
1031 "%d: nice value %ld not allowed use %ld instead\n",
1032 current->pid, nice, min_nice);
1033 set_user_nice(current, min_nice);
1034 if (min_nice <= MAX_NICE)
1036 binder_user_error("%d RLIMIT_NICE not set\n", current->pid);
1039 static struct binder_node *binder_get_node_ilocked(struct binder_proc *proc,
1040 binder_uintptr_t ptr)
1042 struct rb_node *n = proc->nodes.rb_node;
1043 struct binder_node *node;
1045 assert_spin_locked(&proc->inner_lock);
1048 node = rb_entry(n, struct binder_node, rb_node);
1050 if (ptr < node->ptr)
1052 else if (ptr > node->ptr)
1056 * take an implicit weak reference
1057 * to ensure node stays alive until
1058 * call to binder_put_node()
1060 binder_inc_node_tmpref_ilocked(node);
1067 static struct binder_node *binder_get_node(struct binder_proc *proc,
1068 binder_uintptr_t ptr)
1070 struct binder_node *node;
1072 binder_inner_proc_lock(proc);
1073 node = binder_get_node_ilocked(proc, ptr);
1074 binder_inner_proc_unlock(proc);
1078 static struct binder_node *binder_init_node_ilocked(
1079 struct binder_proc *proc,
1080 struct binder_node *new_node,
1081 struct flat_binder_object *fp)
1083 struct rb_node **p = &proc->nodes.rb_node;
1084 struct rb_node *parent = NULL;
1085 struct binder_node *node;
1086 binder_uintptr_t ptr = fp ? fp->binder : 0;
1087 binder_uintptr_t cookie = fp ? fp->cookie : 0;
1088 __u32 flags = fp ? fp->flags : 0;
1090 assert_spin_locked(&proc->inner_lock);
1095 node = rb_entry(parent, struct binder_node, rb_node);
1097 if (ptr < node->ptr)
1099 else if (ptr > node->ptr)
1100 p = &(*p)->rb_right;
1103 * A matching node is already in
1104 * the rb tree. Abandon the init
1107 binder_inc_node_tmpref_ilocked(node);
1112 binder_stats_created(BINDER_STAT_NODE);
1114 rb_link_node(&node->rb_node, parent, p);
1115 rb_insert_color(&node->rb_node, &proc->nodes);
1116 node->debug_id = atomic_inc_return(&binder_last_id);
1119 node->cookie = cookie;
1120 node->work.type = BINDER_WORK_NODE;
1121 node->min_priority = flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
1122 node->accept_fds = !!(flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
1123 node->txn_security_ctx = !!(flags & FLAT_BINDER_FLAG_TXN_SECURITY_CTX);
1124 spin_lock_init(&node->lock);
1125 INIT_LIST_HEAD(&node->work.entry);
1126 INIT_LIST_HEAD(&node->async_todo);
1127 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1128 "%d:%d node %d u%016llx c%016llx created\n",
1129 proc->pid, current->pid, node->debug_id,
1130 (u64)node->ptr, (u64)node->cookie);
1135 static struct binder_node *binder_new_node(struct binder_proc *proc,
1136 struct flat_binder_object *fp)
1138 struct binder_node *node;
1139 struct binder_node *new_node = kzalloc(sizeof(*node), GFP_KERNEL);
1143 binder_inner_proc_lock(proc);
1144 node = binder_init_node_ilocked(proc, new_node, fp);
1145 binder_inner_proc_unlock(proc);
1146 if (node != new_node)
1148 * The node was already added by another thread
1155 static void binder_free_node(struct binder_node *node)
1158 binder_stats_deleted(BINDER_STAT_NODE);
1161 static int binder_inc_node_nilocked(struct binder_node *node, int strong,
1163 struct list_head *target_list)
1165 struct binder_proc *proc = node->proc;
1167 assert_spin_locked(&node->lock);
1169 assert_spin_locked(&proc->inner_lock);
1172 if (target_list == NULL &&
1173 node->internal_strong_refs == 0 &&
1175 node == node->proc->context->binder_context_mgr_node &&
1176 node->has_strong_ref)) {
1177 pr_err("invalid inc strong node for %d\n",
1181 node->internal_strong_refs++;
1183 node->local_strong_refs++;
1184 if (!node->has_strong_ref && target_list) {
1185 struct binder_thread *thread = container_of(target_list,
1186 struct binder_thread, todo);
1187 binder_dequeue_work_ilocked(&node->work);
1188 BUG_ON(&thread->todo != target_list);
1189 binder_enqueue_deferred_thread_work_ilocked(thread,
1194 node->local_weak_refs++;
1195 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
1196 if (target_list == NULL) {
1197 pr_err("invalid inc weak node for %d\n",
1204 binder_enqueue_work_ilocked(&node->work, target_list);
1210 static int binder_inc_node(struct binder_node *node, int strong, int internal,
1211 struct list_head *target_list)
1215 binder_node_inner_lock(node);
1216 ret = binder_inc_node_nilocked(node, strong, internal, target_list);
1217 binder_node_inner_unlock(node);
1222 static bool binder_dec_node_nilocked(struct binder_node *node,
1223 int strong, int internal)
1225 struct binder_proc *proc = node->proc;
1227 assert_spin_locked(&node->lock);
1229 assert_spin_locked(&proc->inner_lock);
1232 node->internal_strong_refs--;
1234 node->local_strong_refs--;
1235 if (node->local_strong_refs || node->internal_strong_refs)
1239 node->local_weak_refs--;
1240 if (node->local_weak_refs || node->tmp_refs ||
1241 !hlist_empty(&node->refs))
1245 if (proc && (node->has_strong_ref || node->has_weak_ref)) {
1246 if (list_empty(&node->work.entry)) {
1247 binder_enqueue_work_ilocked(&node->work, &proc->todo);
1248 binder_wakeup_proc_ilocked(proc);
1251 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
1252 !node->local_weak_refs && !node->tmp_refs) {
1254 binder_dequeue_work_ilocked(&node->work);
1255 rb_erase(&node->rb_node, &proc->nodes);
1256 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1257 "refless node %d deleted\n",
1260 BUG_ON(!list_empty(&node->work.entry));
1261 spin_lock(&binder_dead_nodes_lock);
1263 * tmp_refs could have changed so
1266 if (node->tmp_refs) {
1267 spin_unlock(&binder_dead_nodes_lock);
1270 hlist_del(&node->dead_node);
1271 spin_unlock(&binder_dead_nodes_lock);
1272 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1273 "dead node %d deleted\n",
1282 static void binder_dec_node(struct binder_node *node, int strong, int internal)
1286 binder_node_inner_lock(node);
1287 free_node = binder_dec_node_nilocked(node, strong, internal);
1288 binder_node_inner_unlock(node);
1290 binder_free_node(node);
1293 static void binder_inc_node_tmpref_ilocked(struct binder_node *node)
1296 * No call to binder_inc_node() is needed since we
1297 * don't need to inform userspace of any changes to
1304 * binder_inc_node_tmpref() - take a temporary reference on node
1305 * @node: node to reference
1307 * Take reference on node to prevent the node from being freed
1308 * while referenced only by a local variable. The inner lock is
1309 * needed to serialize with the node work on the queue (which
1310 * isn't needed after the node is dead). If the node is dead
1311 * (node->proc is NULL), use binder_dead_nodes_lock to protect
1312 * node->tmp_refs against dead-node-only cases where the node
1313 * lock cannot be acquired (eg traversing the dead node list to
1316 static void binder_inc_node_tmpref(struct binder_node *node)
1318 binder_node_lock(node);
1320 binder_inner_proc_lock(node->proc);
1322 spin_lock(&binder_dead_nodes_lock);
1323 binder_inc_node_tmpref_ilocked(node);
1325 binder_inner_proc_unlock(node->proc);
1327 spin_unlock(&binder_dead_nodes_lock);
1328 binder_node_unlock(node);
1332 * binder_dec_node_tmpref() - remove a temporary reference on node
1333 * @node: node to reference
1335 * Release temporary reference on node taken via binder_inc_node_tmpref()
1337 static void binder_dec_node_tmpref(struct binder_node *node)
1341 binder_node_inner_lock(node);
1343 spin_lock(&binder_dead_nodes_lock);
1345 __acquire(&binder_dead_nodes_lock);
1347 BUG_ON(node->tmp_refs < 0);
1349 spin_unlock(&binder_dead_nodes_lock);
1351 __release(&binder_dead_nodes_lock);
1353 * Call binder_dec_node() to check if all refcounts are 0
1354 * and cleanup is needed. Calling with strong=0 and internal=1
1355 * causes no actual reference to be released in binder_dec_node().
1356 * If that changes, a change is needed here too.
1358 free_node = binder_dec_node_nilocked(node, 0, 1);
1359 binder_node_inner_unlock(node);
1361 binder_free_node(node);
1364 static void binder_put_node(struct binder_node *node)
1366 binder_dec_node_tmpref(node);
1369 static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc,
1370 u32 desc, bool need_strong_ref)
1372 struct rb_node *n = proc->refs_by_desc.rb_node;
1373 struct binder_ref *ref;
1376 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1378 if (desc < ref->data.desc) {
1380 } else if (desc > ref->data.desc) {
1382 } else if (need_strong_ref && !ref->data.strong) {
1383 binder_user_error("tried to use weak ref as strong ref\n");
1393 * binder_get_ref_for_node_olocked() - get the ref associated with given node
1394 * @proc: binder_proc that owns the ref
1395 * @node: binder_node of target
1396 * @new_ref: newly allocated binder_ref to be initialized or %NULL
1398 * Look up the ref for the given node and return it if it exists
1400 * If it doesn't exist and the caller provides a newly allocated
1401 * ref, initialize the fields of the newly allocated ref and insert
1402 * into the given proc rb_trees and node refs list.
1404 * Return: the ref for node. It is possible that another thread
1405 * allocated/initialized the ref first in which case the
1406 * returned ref would be different than the passed-in
1407 * new_ref. new_ref must be kfree'd by the caller in
1410 static struct binder_ref *binder_get_ref_for_node_olocked(
1411 struct binder_proc *proc,
1412 struct binder_node *node,
1413 struct binder_ref *new_ref)
1415 struct binder_context *context = proc->context;
1416 struct rb_node **p = &proc->refs_by_node.rb_node;
1417 struct rb_node *parent = NULL;
1418 struct binder_ref *ref;
1423 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1425 if (node < ref->node)
1427 else if (node > ref->node)
1428 p = &(*p)->rb_right;
1435 binder_stats_created(BINDER_STAT_REF);
1436 new_ref->data.debug_id = atomic_inc_return(&binder_last_id);
1437 new_ref->proc = proc;
1438 new_ref->node = node;
1439 rb_link_node(&new_ref->rb_node_node, parent, p);
1440 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1442 new_ref->data.desc = (node == context->binder_context_mgr_node) ? 0 : 1;
1443 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1444 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1445 if (ref->data.desc > new_ref->data.desc)
1447 new_ref->data.desc = ref->data.desc + 1;
1450 p = &proc->refs_by_desc.rb_node;
1453 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1455 if (new_ref->data.desc < ref->data.desc)
1457 else if (new_ref->data.desc > ref->data.desc)
1458 p = &(*p)->rb_right;
1462 rb_link_node(&new_ref->rb_node_desc, parent, p);
1463 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
1465 binder_node_lock(node);
1466 hlist_add_head(&new_ref->node_entry, &node->refs);
1468 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1469 "%d new ref %d desc %d for node %d\n",
1470 proc->pid, new_ref->data.debug_id, new_ref->data.desc,
1472 binder_node_unlock(node);
1476 static void binder_cleanup_ref_olocked(struct binder_ref *ref)
1478 bool delete_node = false;
1480 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1481 "%d delete ref %d desc %d for node %d\n",
1482 ref->proc->pid, ref->data.debug_id, ref->data.desc,
1483 ref->node->debug_id);
1485 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1486 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
1488 binder_node_inner_lock(ref->node);
1489 if (ref->data.strong)
1490 binder_dec_node_nilocked(ref->node, 1, 1);
1492 hlist_del(&ref->node_entry);
1493 delete_node = binder_dec_node_nilocked(ref->node, 0, 1);
1494 binder_node_inner_unlock(ref->node);
1496 * Clear ref->node unless we want the caller to free the node
1500 * The caller uses ref->node to determine
1501 * whether the node needs to be freed. Clear
1502 * it since the node is still alive.
1508 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1509 "%d delete ref %d desc %d has death notification\n",
1510 ref->proc->pid, ref->data.debug_id,
1512 binder_dequeue_work(ref->proc, &ref->death->work);
1513 binder_stats_deleted(BINDER_STAT_DEATH);
1515 binder_stats_deleted(BINDER_STAT_REF);
1519 * binder_inc_ref_olocked() - increment the ref for given handle
1520 * @ref: ref to be incremented
1521 * @strong: if true, strong increment, else weak
1522 * @target_list: list to queue node work on
1524 * Increment the ref. @ref->proc->outer_lock must be held on entry
1526 * Return: 0, if successful, else errno
1528 static int binder_inc_ref_olocked(struct binder_ref *ref, int strong,
1529 struct list_head *target_list)
1534 if (ref->data.strong == 0) {
1535 ret = binder_inc_node(ref->node, 1, 1, target_list);
1541 if (ref->data.weak == 0) {
1542 ret = binder_inc_node(ref->node, 0, 1, target_list);
1552 * binder_dec_ref() - dec the ref for given handle
1553 * @ref: ref to be decremented
1554 * @strong: if true, strong decrement, else weak
1556 * Decrement the ref.
1558 * Return: true if ref is cleaned up and ready to be freed
1560 static bool binder_dec_ref_olocked(struct binder_ref *ref, int strong)
1563 if (ref->data.strong == 0) {
1564 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
1565 ref->proc->pid, ref->data.debug_id,
1566 ref->data.desc, ref->data.strong,
1571 if (ref->data.strong == 0)
1572 binder_dec_node(ref->node, strong, 1);
1574 if (ref->data.weak == 0) {
1575 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
1576 ref->proc->pid, ref->data.debug_id,
1577 ref->data.desc, ref->data.strong,
1583 if (ref->data.strong == 0 && ref->data.weak == 0) {
1584 binder_cleanup_ref_olocked(ref);
1591 * binder_get_node_from_ref() - get the node from the given proc/desc
1592 * @proc: proc containing the ref
1593 * @desc: the handle associated with the ref
1594 * @need_strong_ref: if true, only return node if ref is strong
1595 * @rdata: the id/refcount data for the ref
1597 * Given a proc and ref handle, return the associated binder_node
1599 * Return: a binder_node or NULL if not found or not strong when strong required
1601 static struct binder_node *binder_get_node_from_ref(
1602 struct binder_proc *proc,
1603 u32 desc, bool need_strong_ref,
1604 struct binder_ref_data *rdata)
1606 struct binder_node *node;
1607 struct binder_ref *ref;
1609 binder_proc_lock(proc);
1610 ref = binder_get_ref_olocked(proc, desc, need_strong_ref);
1615 * Take an implicit reference on the node to ensure
1616 * it stays alive until the call to binder_put_node()
1618 binder_inc_node_tmpref(node);
1621 binder_proc_unlock(proc);
1626 binder_proc_unlock(proc);
1631 * binder_free_ref() - free the binder_ref
1634 * Free the binder_ref. Free the binder_node indicated by ref->node
1635 * (if non-NULL) and the binder_ref_death indicated by ref->death.
1637 static void binder_free_ref(struct binder_ref *ref)
1640 binder_free_node(ref->node);
1646 * binder_update_ref_for_handle() - inc/dec the ref for given handle
1647 * @proc: proc containing the ref
1648 * @desc: the handle associated with the ref
1649 * @increment: true=inc reference, false=dec reference
1650 * @strong: true=strong reference, false=weak reference
1651 * @rdata: the id/refcount data for the ref
1653 * Given a proc and ref handle, increment or decrement the ref
1654 * according to "increment" arg.
1656 * Return: 0 if successful, else errno
1658 static int binder_update_ref_for_handle(struct binder_proc *proc,
1659 uint32_t desc, bool increment, bool strong,
1660 struct binder_ref_data *rdata)
1663 struct binder_ref *ref;
1664 bool delete_ref = false;
1666 binder_proc_lock(proc);
1667 ref = binder_get_ref_olocked(proc, desc, strong);
1673 ret = binder_inc_ref_olocked(ref, strong, NULL);
1675 delete_ref = binder_dec_ref_olocked(ref, strong);
1679 binder_proc_unlock(proc);
1682 binder_free_ref(ref);
1686 binder_proc_unlock(proc);
1691 * binder_dec_ref_for_handle() - dec the ref for given handle
1692 * @proc: proc containing the ref
1693 * @desc: the handle associated with the ref
1694 * @strong: true=strong reference, false=weak reference
1695 * @rdata: the id/refcount data for the ref
1697 * Just calls binder_update_ref_for_handle() to decrement the ref.
1699 * Return: 0 if successful, else errno
1701 static int binder_dec_ref_for_handle(struct binder_proc *proc,
1702 uint32_t desc, bool strong, struct binder_ref_data *rdata)
1704 return binder_update_ref_for_handle(proc, desc, false, strong, rdata);
1709 * binder_inc_ref_for_node() - increment the ref for given proc/node
1710 * @proc: proc containing the ref
1711 * @node: target node
1712 * @strong: true=strong reference, false=weak reference
1713 * @target_list: worklist to use if node is incremented
1714 * @rdata: the id/refcount data for the ref
1716 * Given a proc and node, increment the ref. Create the ref if it
1717 * doesn't already exist
1719 * Return: 0 if successful, else errno
1721 static int binder_inc_ref_for_node(struct binder_proc *proc,
1722 struct binder_node *node,
1724 struct list_head *target_list,
1725 struct binder_ref_data *rdata)
1727 struct binder_ref *ref;
1728 struct binder_ref *new_ref = NULL;
1731 binder_proc_lock(proc);
1732 ref = binder_get_ref_for_node_olocked(proc, node, NULL);
1734 binder_proc_unlock(proc);
1735 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1738 binder_proc_lock(proc);
1739 ref = binder_get_ref_for_node_olocked(proc, node, new_ref);
1741 ret = binder_inc_ref_olocked(ref, strong, target_list);
1743 binder_proc_unlock(proc);
1744 if (new_ref && ref != new_ref)
1746 * Another thread created the ref first so
1747 * free the one we allocated
1753 static void binder_pop_transaction_ilocked(struct binder_thread *target_thread,
1754 struct binder_transaction *t)
1756 BUG_ON(!target_thread);
1757 assert_spin_locked(&target_thread->proc->inner_lock);
1758 BUG_ON(target_thread->transaction_stack != t);
1759 BUG_ON(target_thread->transaction_stack->from != target_thread);
1760 target_thread->transaction_stack =
1761 target_thread->transaction_stack->from_parent;
1766 * binder_thread_dec_tmpref() - decrement thread->tmp_ref
1767 * @thread: thread to decrement
1769 * A thread needs to be kept alive while being used to create or
1770 * handle a transaction. binder_get_txn_from() is used to safely
1771 * extract t->from from a binder_transaction and keep the thread
1772 * indicated by t->from from being freed. When done with that
1773 * binder_thread, this function is called to decrement the
1774 * tmp_ref and free if appropriate (thread has been released
1775 * and no transaction being processed by the driver)
1777 static void binder_thread_dec_tmpref(struct binder_thread *thread)
1780 * atomic is used to protect the counter value while
1781 * it cannot reach zero or thread->is_dead is false
1783 binder_inner_proc_lock(thread->proc);
1784 atomic_dec(&thread->tmp_ref);
1785 if (thread->is_dead && !atomic_read(&thread->tmp_ref)) {
1786 binder_inner_proc_unlock(thread->proc);
1787 binder_free_thread(thread);
1790 binder_inner_proc_unlock(thread->proc);
1794 * binder_proc_dec_tmpref() - decrement proc->tmp_ref
1795 * @proc: proc to decrement
1797 * A binder_proc needs to be kept alive while being used to create or
1798 * handle a transaction. proc->tmp_ref is incremented when
1799 * creating a new transaction or the binder_proc is currently in-use
1800 * by threads that are being released. When done with the binder_proc,
1801 * this function is called to decrement the counter and free the
1802 * proc if appropriate (proc has been released, all threads have
1803 * been released and not currenly in-use to process a transaction).
1805 static void binder_proc_dec_tmpref(struct binder_proc *proc)
1807 binder_inner_proc_lock(proc);
1809 if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) &&
1811 binder_inner_proc_unlock(proc);
1812 binder_free_proc(proc);
1815 binder_inner_proc_unlock(proc);
1819 * binder_get_txn_from() - safely extract the "from" thread in transaction
1820 * @t: binder transaction for t->from
1822 * Atomically return the "from" thread and increment the tmp_ref
1823 * count for the thread to ensure it stays alive until
1824 * binder_thread_dec_tmpref() is called.
1826 * Return: the value of t->from
1828 static struct binder_thread *binder_get_txn_from(
1829 struct binder_transaction *t)
1831 struct binder_thread *from;
1833 spin_lock(&t->lock);
1836 atomic_inc(&from->tmp_ref);
1837 spin_unlock(&t->lock);
1842 * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock
1843 * @t: binder transaction for t->from
1845 * Same as binder_get_txn_from() except it also acquires the proc->inner_lock
1846 * to guarantee that the thread cannot be released while operating on it.
1847 * The caller must call binder_inner_proc_unlock() to release the inner lock
1848 * as well as call binder_dec_thread_txn() to release the reference.
1850 * Return: the value of t->from
1852 static struct binder_thread *binder_get_txn_from_and_acq_inner(
1853 struct binder_transaction *t)
1854 __acquires(&t->from->proc->inner_lock)
1856 struct binder_thread *from;
1858 from = binder_get_txn_from(t);
1860 __acquire(&from->proc->inner_lock);
1863 binder_inner_proc_lock(from->proc);
1865 BUG_ON(from != t->from);
1868 binder_inner_proc_unlock(from->proc);
1869 __acquire(&from->proc->inner_lock);
1870 binder_thread_dec_tmpref(from);
1875 * binder_free_txn_fixups() - free unprocessed fd fixups
1876 * @t: binder transaction for t->from
1878 * If the transaction is being torn down prior to being
1879 * processed by the target process, free all of the
1880 * fd fixups and fput the file structs. It is safe to
1881 * call this function after the fixups have been
1882 * processed -- in that case, the list will be empty.
1884 static void binder_free_txn_fixups(struct binder_transaction *t)
1886 struct binder_txn_fd_fixup *fixup, *tmp;
1888 list_for_each_entry_safe(fixup, tmp, &t->fd_fixups, fixup_entry) {
1890 list_del(&fixup->fixup_entry);
1895 static void binder_free_transaction(struct binder_transaction *t)
1897 struct binder_proc *target_proc = t->to_proc;
1900 binder_inner_proc_lock(target_proc);
1902 t->buffer->transaction = NULL;
1903 binder_inner_proc_unlock(target_proc);
1906 * If the transaction has no target_proc, then
1907 * t->buffer->transaction has already been cleared.
1909 binder_free_txn_fixups(t);
1911 binder_stats_deleted(BINDER_STAT_TRANSACTION);
1914 static void binder_send_failed_reply(struct binder_transaction *t,
1915 uint32_t error_code)
1917 struct binder_thread *target_thread;
1918 struct binder_transaction *next;
1920 BUG_ON(t->flags & TF_ONE_WAY);
1922 target_thread = binder_get_txn_from_and_acq_inner(t);
1923 if (target_thread) {
1924 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1925 "send failed reply for transaction %d to %d:%d\n",
1927 target_thread->proc->pid,
1928 target_thread->pid);
1930 binder_pop_transaction_ilocked(target_thread, t);
1931 if (target_thread->reply_error.cmd == BR_OK) {
1932 target_thread->reply_error.cmd = error_code;
1933 binder_enqueue_thread_work_ilocked(
1935 &target_thread->reply_error.work);
1936 wake_up_interruptible(&target_thread->wait);
1939 * Cannot get here for normal operation, but
1940 * we can if multiple synchronous transactions
1941 * are sent without blocking for responses.
1942 * Just ignore the 2nd error in this case.
1944 pr_warn("Unexpected reply error: %u\n",
1945 target_thread->reply_error.cmd);
1947 binder_inner_proc_unlock(target_thread->proc);
1948 binder_thread_dec_tmpref(target_thread);
1949 binder_free_transaction(t);
1952 __release(&target_thread->proc->inner_lock);
1953 next = t->from_parent;
1955 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1956 "send failed reply for transaction %d, target dead\n",
1959 binder_free_transaction(t);
1961 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1962 "reply failed, no target thread at root\n");
1966 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1967 "reply failed, no target thread -- retry %d\n",
1973 * binder_cleanup_transaction() - cleans up undelivered transaction
1974 * @t: transaction that needs to be cleaned up
1975 * @reason: reason the transaction wasn't delivered
1976 * @error_code: error to return to caller (if synchronous call)
1978 static void binder_cleanup_transaction(struct binder_transaction *t,
1980 uint32_t error_code)
1982 if (t->buffer->target_node && !(t->flags & TF_ONE_WAY)) {
1983 binder_send_failed_reply(t, error_code);
1985 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
1986 "undelivered transaction %d, %s\n",
1987 t->debug_id, reason);
1988 binder_free_transaction(t);
1993 * binder_get_object() - gets object and checks for valid metadata
1994 * @proc: binder_proc owning the buffer
1995 * @buffer: binder_buffer that we're parsing.
1996 * @offset: offset in the @buffer at which to validate an object.
1997 * @object: struct binder_object to read into
1999 * Return: If there's a valid metadata object at @offset in @buffer, the
2000 * size of that object. Otherwise, it returns zero. The object
2001 * is read into the struct binder_object pointed to by @object.
2003 static size_t binder_get_object(struct binder_proc *proc,
2004 struct binder_buffer *buffer,
2005 unsigned long offset,
2006 struct binder_object *object)
2009 struct binder_object_header *hdr;
2010 size_t object_size = 0;
2012 read_size = min_t(size_t, sizeof(*object), buffer->data_size - offset);
2013 if (offset > buffer->data_size || read_size < sizeof(*hdr) ||
2014 binder_alloc_copy_from_buffer(&proc->alloc, object, buffer,
2018 /* Ok, now see if we read a complete object. */
2020 switch (hdr->type) {
2021 case BINDER_TYPE_BINDER:
2022 case BINDER_TYPE_WEAK_BINDER:
2023 case BINDER_TYPE_HANDLE:
2024 case BINDER_TYPE_WEAK_HANDLE:
2025 object_size = sizeof(struct flat_binder_object);
2027 case BINDER_TYPE_FD:
2028 object_size = sizeof(struct binder_fd_object);
2030 case BINDER_TYPE_PTR:
2031 object_size = sizeof(struct binder_buffer_object);
2033 case BINDER_TYPE_FDA:
2034 object_size = sizeof(struct binder_fd_array_object);
2039 if (offset <= buffer->data_size - object_size &&
2040 buffer->data_size >= object_size)
2047 * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
2048 * @proc: binder_proc owning the buffer
2049 * @b: binder_buffer containing the object
2050 * @object: struct binder_object to read into
2051 * @index: index in offset array at which the binder_buffer_object is
2053 * @start_offset: points to the start of the offset array
2054 * @object_offsetp: offset of @object read from @b
2055 * @num_valid: the number of valid offsets in the offset array
2057 * Return: If @index is within the valid range of the offset array
2058 * described by @start and @num_valid, and if there's a valid
2059 * binder_buffer_object at the offset found in index @index
2060 * of the offset array, that object is returned. Otherwise,
2061 * %NULL is returned.
2062 * Note that the offset found in index @index itself is not
2063 * verified; this function assumes that @num_valid elements
2064 * from @start were previously verified to have valid offsets.
2065 * If @object_offsetp is non-NULL, then the offset within
2066 * @b is written to it.
2068 static struct binder_buffer_object *binder_validate_ptr(
2069 struct binder_proc *proc,
2070 struct binder_buffer *b,
2071 struct binder_object *object,
2072 binder_size_t index,
2073 binder_size_t start_offset,
2074 binder_size_t *object_offsetp,
2075 binder_size_t num_valid)
2078 binder_size_t object_offset;
2079 unsigned long buffer_offset;
2081 if (index >= num_valid)
2084 buffer_offset = start_offset + sizeof(binder_size_t) * index;
2085 if (binder_alloc_copy_from_buffer(&proc->alloc, &object_offset,
2087 sizeof(object_offset)))
2089 object_size = binder_get_object(proc, b, object_offset, object);
2090 if (!object_size || object->hdr.type != BINDER_TYPE_PTR)
2093 *object_offsetp = object_offset;
2095 return &object->bbo;
2099 * binder_validate_fixup() - validates pointer/fd fixups happen in order.
2100 * @proc: binder_proc owning the buffer
2101 * @b: transaction buffer
2102 * @objects_start_offset: offset to start of objects buffer
2103 * @buffer_obj_offset: offset to binder_buffer_object in which to fix up
2104 * @fixup_offset: start offset in @buffer to fix up
2105 * @last_obj_offset: offset to last binder_buffer_object that we fixed
2106 * @last_min_offset: minimum fixup offset in object at @last_obj_offset
2108 * Return: %true if a fixup in buffer @buffer at offset @offset is
2111 * For safety reasons, we only allow fixups inside a buffer to happen
2112 * at increasing offsets; additionally, we only allow fixup on the last
2113 * buffer object that was verified, or one of its parents.
2115 * Example of what is allowed:
2118 * B (parent = A, offset = 0)
2119 * C (parent = A, offset = 16)
2120 * D (parent = C, offset = 0)
2121 * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
2123 * Examples of what is not allowed:
2125 * Decreasing offsets within the same parent:
2127 * C (parent = A, offset = 16)
2128 * B (parent = A, offset = 0) // decreasing offset within A
2130 * Referring to a parent that wasn't the last object or any of its parents:
2132 * B (parent = A, offset = 0)
2133 * C (parent = A, offset = 0)
2134 * C (parent = A, offset = 16)
2135 * D (parent = B, offset = 0) // B is not A or any of A's parents
2137 static bool binder_validate_fixup(struct binder_proc *proc,
2138 struct binder_buffer *b,
2139 binder_size_t objects_start_offset,
2140 binder_size_t buffer_obj_offset,
2141 binder_size_t fixup_offset,
2142 binder_size_t last_obj_offset,
2143 binder_size_t last_min_offset)
2145 if (!last_obj_offset) {
2146 /* Nothing to fix up in */
2150 while (last_obj_offset != buffer_obj_offset) {
2151 unsigned long buffer_offset;
2152 struct binder_object last_object;
2153 struct binder_buffer_object *last_bbo;
2154 size_t object_size = binder_get_object(proc, b, last_obj_offset,
2156 if (object_size != sizeof(*last_bbo))
2159 last_bbo = &last_object.bbo;
2161 * Safe to retrieve the parent of last_obj, since it
2162 * was already previously verified by the driver.
2164 if ((last_bbo->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0)
2166 last_min_offset = last_bbo->parent_offset + sizeof(uintptr_t);
2167 buffer_offset = objects_start_offset +
2168 sizeof(binder_size_t) * last_bbo->parent;
2169 if (binder_alloc_copy_from_buffer(&proc->alloc,
2172 sizeof(last_obj_offset)))
2175 return (fixup_offset >= last_min_offset);
2179 * struct binder_task_work_cb - for deferred close
2181 * @twork: callback_head for task work
2184 * Structure to pass task work to be handled after
2185 * returning from binder_ioctl() via task_work_add().
2187 struct binder_task_work_cb {
2188 struct callback_head twork;
2193 * binder_do_fd_close() - close list of file descriptors
2194 * @twork: callback head for task work
2196 * It is not safe to call ksys_close() during the binder_ioctl()
2197 * function if there is a chance that binder's own file descriptor
2198 * might be closed. This is to meet the requirements for using
2199 * fdget() (see comments for __fget_light()). Therefore use
2200 * task_work_add() to schedule the close operation once we have
2201 * returned from binder_ioctl(). This function is a callback
2202 * for that mechanism and does the actual ksys_close() on the
2203 * given file descriptor.
2205 static void binder_do_fd_close(struct callback_head *twork)
2207 struct binder_task_work_cb *twcb = container_of(twork,
2208 struct binder_task_work_cb, twork);
2215 * binder_deferred_fd_close() - schedule a close for the given file-descriptor
2216 * @fd: file-descriptor to close
2218 * See comments in binder_do_fd_close(). This function is used to schedule
2219 * a file-descriptor to be closed after returning from binder_ioctl().
2221 static void binder_deferred_fd_close(int fd)
2223 struct binder_task_work_cb *twcb;
2225 twcb = kzalloc(sizeof(*twcb), GFP_KERNEL);
2228 init_task_work(&twcb->twork, binder_do_fd_close);
2229 close_fd_get_file(fd, &twcb->file);
2231 filp_close(twcb->file, current->files);
2232 task_work_add(current, &twcb->twork, TWA_RESUME);
2238 static void binder_transaction_buffer_release(struct binder_proc *proc,
2239 struct binder_buffer *buffer,
2240 binder_size_t failed_at,
2243 int debug_id = buffer->debug_id;
2244 binder_size_t off_start_offset, buffer_offset, off_end_offset;
2246 binder_debug(BINDER_DEBUG_TRANSACTION,
2247 "%d buffer release %d, size %zd-%zd, failed at %llx\n",
2248 proc->pid, buffer->debug_id,
2249 buffer->data_size, buffer->offsets_size,
2250 (unsigned long long)failed_at);
2252 if (buffer->target_node)
2253 binder_dec_node(buffer->target_node, 1, 0);
2255 off_start_offset = ALIGN(buffer->data_size, sizeof(void *));
2256 off_end_offset = is_failure ? failed_at :
2257 off_start_offset + buffer->offsets_size;
2258 for (buffer_offset = off_start_offset; buffer_offset < off_end_offset;
2259 buffer_offset += sizeof(binder_size_t)) {
2260 struct binder_object_header *hdr;
2261 size_t object_size = 0;
2262 struct binder_object object;
2263 binder_size_t object_offset;
2265 if (!binder_alloc_copy_from_buffer(&proc->alloc, &object_offset,
2266 buffer, buffer_offset,
2267 sizeof(object_offset)))
2268 object_size = binder_get_object(proc, buffer,
2269 object_offset, &object);
2270 if (object_size == 0) {
2271 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
2272 debug_id, (u64)object_offset, buffer->data_size);
2276 switch (hdr->type) {
2277 case BINDER_TYPE_BINDER:
2278 case BINDER_TYPE_WEAK_BINDER: {
2279 struct flat_binder_object *fp;
2280 struct binder_node *node;
2282 fp = to_flat_binder_object(hdr);
2283 node = binder_get_node(proc, fp->binder);
2285 pr_err("transaction release %d bad node %016llx\n",
2286 debug_id, (u64)fp->binder);
2289 binder_debug(BINDER_DEBUG_TRANSACTION,
2290 " node %d u%016llx\n",
2291 node->debug_id, (u64)node->ptr);
2292 binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER,
2294 binder_put_node(node);
2296 case BINDER_TYPE_HANDLE:
2297 case BINDER_TYPE_WEAK_HANDLE: {
2298 struct flat_binder_object *fp;
2299 struct binder_ref_data rdata;
2302 fp = to_flat_binder_object(hdr);
2303 ret = binder_dec_ref_for_handle(proc, fp->handle,
2304 hdr->type == BINDER_TYPE_HANDLE, &rdata);
2307 pr_err("transaction release %d bad handle %d, ret = %d\n",
2308 debug_id, fp->handle, ret);
2311 binder_debug(BINDER_DEBUG_TRANSACTION,
2312 " ref %d desc %d\n",
2313 rdata.debug_id, rdata.desc);
2316 case BINDER_TYPE_FD: {
2318 * No need to close the file here since user-space
2319 * closes it for for successfully delivered
2320 * transactions. For transactions that weren't
2321 * delivered, the new fd was never allocated so
2322 * there is no need to close and the fput on the
2323 * file is done when the transaction is torn
2327 case BINDER_TYPE_PTR:
2329 * Nothing to do here, this will get cleaned up when the
2330 * transaction buffer gets freed
2333 case BINDER_TYPE_FDA: {
2334 struct binder_fd_array_object *fda;
2335 struct binder_buffer_object *parent;
2336 struct binder_object ptr_object;
2337 binder_size_t fda_offset;
2339 binder_size_t fd_buf_size;
2340 binder_size_t num_valid;
2342 if (proc->tsk != current->group_leader) {
2344 * Nothing to do if running in sender context
2345 * The fd fixups have not been applied so no
2346 * fds need to be closed.
2351 num_valid = (buffer_offset - off_start_offset) /
2352 sizeof(binder_size_t);
2353 fda = to_binder_fd_array_object(hdr);
2354 parent = binder_validate_ptr(proc, buffer, &ptr_object,
2360 pr_err("transaction release %d bad parent offset\n",
2364 fd_buf_size = sizeof(u32) * fda->num_fds;
2365 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2366 pr_err("transaction release %d invalid number of fds (%lld)\n",
2367 debug_id, (u64)fda->num_fds);
2370 if (fd_buf_size > parent->length ||
2371 fda->parent_offset > parent->length - fd_buf_size) {
2372 /* No space for all file descriptors here. */
2373 pr_err("transaction release %d not enough space for %lld fds in buffer\n",
2374 debug_id, (u64)fda->num_fds);
2378 * the source data for binder_buffer_object is visible
2379 * to user-space and the @buffer element is the user
2380 * pointer to the buffer_object containing the fd_array.
2381 * Convert the address to an offset relative to
2382 * the base of the transaction buffer.
2385 (parent->buffer - (uintptr_t)buffer->user_data) +
2387 for (fd_index = 0; fd_index < fda->num_fds;
2391 binder_size_t offset = fda_offset +
2392 fd_index * sizeof(fd);
2394 err = binder_alloc_copy_from_buffer(
2395 &proc->alloc, &fd, buffer,
2396 offset, sizeof(fd));
2399 binder_deferred_fd_close(fd);
2403 pr_err("transaction release %d bad object type %x\n",
2404 debug_id, hdr->type);
2410 static int binder_translate_binder(struct flat_binder_object *fp,
2411 struct binder_transaction *t,
2412 struct binder_thread *thread)
2414 struct binder_node *node;
2415 struct binder_proc *proc = thread->proc;
2416 struct binder_proc *target_proc = t->to_proc;
2417 struct binder_ref_data rdata;
2420 node = binder_get_node(proc, fp->binder);
2422 node = binder_new_node(proc, fp);
2426 if (fp->cookie != node->cookie) {
2427 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
2428 proc->pid, thread->pid, (u64)fp->binder,
2429 node->debug_id, (u64)fp->cookie,
2434 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2439 ret = binder_inc_ref_for_node(target_proc, node,
2440 fp->hdr.type == BINDER_TYPE_BINDER,
2441 &thread->todo, &rdata);
2445 if (fp->hdr.type == BINDER_TYPE_BINDER)
2446 fp->hdr.type = BINDER_TYPE_HANDLE;
2448 fp->hdr.type = BINDER_TYPE_WEAK_HANDLE;
2450 fp->handle = rdata.desc;
2453 trace_binder_transaction_node_to_ref(t, node, &rdata);
2454 binder_debug(BINDER_DEBUG_TRANSACTION,
2455 " node %d u%016llx -> ref %d desc %d\n",
2456 node->debug_id, (u64)node->ptr,
2457 rdata.debug_id, rdata.desc);
2459 binder_put_node(node);
2463 static int binder_translate_handle(struct flat_binder_object *fp,
2464 struct binder_transaction *t,
2465 struct binder_thread *thread)
2467 struct binder_proc *proc = thread->proc;
2468 struct binder_proc *target_proc = t->to_proc;
2469 struct binder_node *node;
2470 struct binder_ref_data src_rdata;
2473 node = binder_get_node_from_ref(proc, fp->handle,
2474 fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata);
2476 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
2477 proc->pid, thread->pid, fp->handle);
2480 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2485 binder_node_lock(node);
2486 if (node->proc == target_proc) {
2487 if (fp->hdr.type == BINDER_TYPE_HANDLE)
2488 fp->hdr.type = BINDER_TYPE_BINDER;
2490 fp->hdr.type = BINDER_TYPE_WEAK_BINDER;
2491 fp->binder = node->ptr;
2492 fp->cookie = node->cookie;
2494 binder_inner_proc_lock(node->proc);
2496 __acquire(&node->proc->inner_lock);
2497 binder_inc_node_nilocked(node,
2498 fp->hdr.type == BINDER_TYPE_BINDER,
2501 binder_inner_proc_unlock(node->proc);
2503 __release(&node->proc->inner_lock);
2504 trace_binder_transaction_ref_to_node(t, node, &src_rdata);
2505 binder_debug(BINDER_DEBUG_TRANSACTION,
2506 " ref %d desc %d -> node %d u%016llx\n",
2507 src_rdata.debug_id, src_rdata.desc, node->debug_id,
2509 binder_node_unlock(node);
2511 struct binder_ref_data dest_rdata;
2513 binder_node_unlock(node);
2514 ret = binder_inc_ref_for_node(target_proc, node,
2515 fp->hdr.type == BINDER_TYPE_HANDLE,
2521 fp->handle = dest_rdata.desc;
2523 trace_binder_transaction_ref_to_ref(t, node, &src_rdata,
2525 binder_debug(BINDER_DEBUG_TRANSACTION,
2526 " ref %d desc %d -> ref %d desc %d (node %d)\n",
2527 src_rdata.debug_id, src_rdata.desc,
2528 dest_rdata.debug_id, dest_rdata.desc,
2532 binder_put_node(node);
2536 static int binder_translate_fd(u32 fd, binder_size_t fd_offset,
2537 struct binder_transaction *t,
2538 struct binder_thread *thread,
2539 struct binder_transaction *in_reply_to)
2541 struct binder_proc *proc = thread->proc;
2542 struct binder_proc *target_proc = t->to_proc;
2543 struct binder_txn_fd_fixup *fixup;
2546 bool target_allows_fd;
2549 target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS);
2551 target_allows_fd = t->buffer->target_node->accept_fds;
2552 if (!target_allows_fd) {
2553 binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
2554 proc->pid, thread->pid,
2555 in_reply_to ? "reply" : "transaction",
2558 goto err_fd_not_accepted;
2563 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
2564 proc->pid, thread->pid, fd);
2568 ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file);
2575 * Add fixup record for this transaction. The allocation
2576 * of the fd in the target needs to be done from a
2579 fixup = kzalloc(sizeof(*fixup), GFP_KERNEL);
2585 fixup->offset = fd_offset;
2586 trace_binder_transaction_fd_send(t, fd, fixup->offset);
2587 list_add_tail(&fixup->fixup_entry, &t->fd_fixups);
2595 err_fd_not_accepted:
2599 static int binder_translate_fd_array(struct binder_fd_array_object *fda,
2600 struct binder_buffer_object *parent,
2601 struct binder_transaction *t,
2602 struct binder_thread *thread,
2603 struct binder_transaction *in_reply_to)
2605 binder_size_t fdi, fd_buf_size;
2606 binder_size_t fda_offset;
2607 struct binder_proc *proc = thread->proc;
2608 struct binder_proc *target_proc = t->to_proc;
2610 fd_buf_size = sizeof(u32) * fda->num_fds;
2611 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2612 binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
2613 proc->pid, thread->pid, (u64)fda->num_fds);
2616 if (fd_buf_size > parent->length ||
2617 fda->parent_offset > parent->length - fd_buf_size) {
2618 /* No space for all file descriptors here. */
2619 binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
2620 proc->pid, thread->pid, (u64)fda->num_fds);
2624 * the source data for binder_buffer_object is visible
2625 * to user-space and the @buffer element is the user
2626 * pointer to the buffer_object containing the fd_array.
2627 * Convert the address to an offset relative to
2628 * the base of the transaction buffer.
2630 fda_offset = (parent->buffer - (uintptr_t)t->buffer->user_data) +
2632 if (!IS_ALIGNED((unsigned long)fda_offset, sizeof(u32))) {
2633 binder_user_error("%d:%d parent offset not aligned correctly.\n",
2634 proc->pid, thread->pid);
2637 for (fdi = 0; fdi < fda->num_fds; fdi++) {
2640 binder_size_t offset = fda_offset + fdi * sizeof(fd);
2642 ret = binder_alloc_copy_from_buffer(&target_proc->alloc,
2644 offset, sizeof(fd));
2646 ret = binder_translate_fd(fd, offset, t, thread,
2654 static int binder_fixup_parent(struct binder_transaction *t,
2655 struct binder_thread *thread,
2656 struct binder_buffer_object *bp,
2657 binder_size_t off_start_offset,
2658 binder_size_t num_valid,
2659 binder_size_t last_fixup_obj_off,
2660 binder_size_t last_fixup_min_off)
2662 struct binder_buffer_object *parent;
2663 struct binder_buffer *b = t->buffer;
2664 struct binder_proc *proc = thread->proc;
2665 struct binder_proc *target_proc = t->to_proc;
2666 struct binder_object object;
2667 binder_size_t buffer_offset;
2668 binder_size_t parent_offset;
2670 if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT))
2673 parent = binder_validate_ptr(target_proc, b, &object, bp->parent,
2674 off_start_offset, &parent_offset,
2677 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
2678 proc->pid, thread->pid);
2682 if (!binder_validate_fixup(target_proc, b, off_start_offset,
2683 parent_offset, bp->parent_offset,
2685 last_fixup_min_off)) {
2686 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
2687 proc->pid, thread->pid);
2691 if (parent->length < sizeof(binder_uintptr_t) ||
2692 bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) {
2693 /* No space for a pointer here! */
2694 binder_user_error("%d:%d got transaction with invalid parent offset\n",
2695 proc->pid, thread->pid);
2698 buffer_offset = bp->parent_offset +
2699 (uintptr_t)parent->buffer - (uintptr_t)b->user_data;
2700 if (binder_alloc_copy_to_buffer(&target_proc->alloc, b, buffer_offset,
2701 &bp->buffer, sizeof(bp->buffer))) {
2702 binder_user_error("%d:%d got transaction with invalid parent offset\n",
2703 proc->pid, thread->pid);
2711 * binder_proc_transaction() - sends a transaction to a process and wakes it up
2712 * @t: transaction to send
2713 * @proc: process to send the transaction to
2714 * @thread: thread in @proc to send the transaction to (may be NULL)
2716 * This function queues a transaction to the specified process. It will try
2717 * to find a thread in the target process to handle the transaction and
2718 * wake it up. If no thread is found, the work is queued to the proc
2721 * If the @thread parameter is not NULL, the transaction is always queued
2722 * to the waitlist of that specific thread.
2724 * Return: true if the transactions was successfully queued
2725 * false if the target process or thread is dead
2727 static bool binder_proc_transaction(struct binder_transaction *t,
2728 struct binder_proc *proc,
2729 struct binder_thread *thread)
2731 struct binder_node *node = t->buffer->target_node;
2732 bool oneway = !!(t->flags & TF_ONE_WAY);
2733 bool pending_async = false;
2736 binder_node_lock(node);
2739 if (node->has_async_transaction)
2740 pending_async = true;
2742 node->has_async_transaction = true;
2745 binder_inner_proc_lock(proc);
2747 if (proc->is_dead || (thread && thread->is_dead)) {
2748 binder_inner_proc_unlock(proc);
2749 binder_node_unlock(node);
2753 if (!thread && !pending_async)
2754 thread = binder_select_thread_ilocked(proc);
2757 binder_enqueue_thread_work_ilocked(thread, &t->work);
2758 else if (!pending_async)
2759 binder_enqueue_work_ilocked(&t->work, &proc->todo);
2761 binder_enqueue_work_ilocked(&t->work, &node->async_todo);
2764 binder_wakeup_thread_ilocked(proc, thread, !oneway /* sync */);
2766 binder_inner_proc_unlock(proc);
2767 binder_node_unlock(node);
2773 * binder_get_node_refs_for_txn() - Get required refs on node for txn
2774 * @node: struct binder_node for which to get refs
2775 * @proc: returns @node->proc if valid
2776 * @error: if no @proc then returns BR_DEAD_REPLY
2778 * User-space normally keeps the node alive when creating a transaction
2779 * since it has a reference to the target. The local strong ref keeps it
2780 * alive if the sending process dies before the target process processes
2781 * the transaction. If the source process is malicious or has a reference
2782 * counting bug, relying on the local strong ref can fail.
2784 * Since user-space can cause the local strong ref to go away, we also take
2785 * a tmpref on the node to ensure it survives while we are constructing
2786 * the transaction. We also need a tmpref on the proc while we are
2787 * constructing the transaction, so we take that here as well.
2789 * Return: The target_node with refs taken or NULL if no @node->proc is NULL.
2790 * Also sets @proc if valid. If the @node->proc is NULL indicating that the
2791 * target proc has died, @error is set to BR_DEAD_REPLY
2793 static struct binder_node *binder_get_node_refs_for_txn(
2794 struct binder_node *node,
2795 struct binder_proc **procp,
2798 struct binder_node *target_node = NULL;
2800 binder_node_inner_lock(node);
2803 binder_inc_node_nilocked(node, 1, 0, NULL);
2804 binder_inc_node_tmpref_ilocked(node);
2805 node->proc->tmp_ref++;
2806 *procp = node->proc;
2808 *error = BR_DEAD_REPLY;
2809 binder_node_inner_unlock(node);
2814 static void binder_transaction(struct binder_proc *proc,
2815 struct binder_thread *thread,
2816 struct binder_transaction_data *tr, int reply,
2817 binder_size_t extra_buffers_size)
2820 struct binder_transaction *t;
2821 struct binder_work *w;
2822 struct binder_work *tcomplete;
2823 binder_size_t buffer_offset = 0;
2824 binder_size_t off_start_offset, off_end_offset;
2825 binder_size_t off_min;
2826 binder_size_t sg_buf_offset, sg_buf_end_offset;
2827 struct binder_proc *target_proc = NULL;
2828 struct binder_thread *target_thread = NULL;
2829 struct binder_node *target_node = NULL;
2830 struct binder_transaction *in_reply_to = NULL;
2831 struct binder_transaction_log_entry *e;
2832 uint32_t return_error = 0;
2833 uint32_t return_error_param = 0;
2834 uint32_t return_error_line = 0;
2835 binder_size_t last_fixup_obj_off = 0;
2836 binder_size_t last_fixup_min_off = 0;
2837 struct binder_context *context = proc->context;
2838 int t_debug_id = atomic_inc_return(&binder_last_id);
2839 char *secctx = NULL;
2842 e = binder_transaction_log_add(&binder_transaction_log);
2843 e->debug_id = t_debug_id;
2844 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
2845 e->from_proc = proc->pid;
2846 e->from_thread = thread->pid;
2847 e->target_handle = tr->target.handle;
2848 e->data_size = tr->data_size;
2849 e->offsets_size = tr->offsets_size;
2850 strscpy(e->context_name, proc->context->name, BINDERFS_MAX_NAME);
2853 binder_inner_proc_lock(proc);
2854 in_reply_to = thread->transaction_stack;
2855 if (in_reply_to == NULL) {
2856 binder_inner_proc_unlock(proc);
2857 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
2858 proc->pid, thread->pid);
2859 return_error = BR_FAILED_REPLY;
2860 return_error_param = -EPROTO;
2861 return_error_line = __LINE__;
2862 goto err_empty_call_stack;
2864 if (in_reply_to->to_thread != thread) {
2865 spin_lock(&in_reply_to->lock);
2866 binder_user_error("%d:%d got reply transaction with bad transaction stack, transaction %d has target %d:%d\n",
2867 proc->pid, thread->pid, in_reply_to->debug_id,
2868 in_reply_to->to_proc ?
2869 in_reply_to->to_proc->pid : 0,
2870 in_reply_to->to_thread ?
2871 in_reply_to->to_thread->pid : 0);
2872 spin_unlock(&in_reply_to->lock);
2873 binder_inner_proc_unlock(proc);
2874 return_error = BR_FAILED_REPLY;
2875 return_error_param = -EPROTO;
2876 return_error_line = __LINE__;
2878 goto err_bad_call_stack;
2880 thread->transaction_stack = in_reply_to->to_parent;
2881 binder_inner_proc_unlock(proc);
2882 binder_set_nice(in_reply_to->saved_priority);
2883 target_thread = binder_get_txn_from_and_acq_inner(in_reply_to);
2884 if (target_thread == NULL) {
2885 /* annotation for sparse */
2886 __release(&target_thread->proc->inner_lock);
2887 return_error = BR_DEAD_REPLY;
2888 return_error_line = __LINE__;
2889 goto err_dead_binder;
2891 if (target_thread->transaction_stack != in_reply_to) {
2892 binder_user_error("%d:%d got reply transaction with bad target transaction stack %d, expected %d\n",
2893 proc->pid, thread->pid,
2894 target_thread->transaction_stack ?
2895 target_thread->transaction_stack->debug_id : 0,
2896 in_reply_to->debug_id);
2897 binder_inner_proc_unlock(target_thread->proc);
2898 return_error = BR_FAILED_REPLY;
2899 return_error_param = -EPROTO;
2900 return_error_line = __LINE__;
2902 target_thread = NULL;
2903 goto err_dead_binder;
2905 target_proc = target_thread->proc;
2906 target_proc->tmp_ref++;
2907 binder_inner_proc_unlock(target_thread->proc);
2909 if (tr->target.handle) {
2910 struct binder_ref *ref;
2913 * There must already be a strong ref
2914 * on this node. If so, do a strong
2915 * increment on the node to ensure it
2916 * stays alive until the transaction is
2919 binder_proc_lock(proc);
2920 ref = binder_get_ref_olocked(proc, tr->target.handle,
2923 target_node = binder_get_node_refs_for_txn(
2924 ref->node, &target_proc,
2927 binder_user_error("%d:%d got transaction to invalid handle\n",
2928 proc->pid, thread->pid);
2929 return_error = BR_FAILED_REPLY;
2931 binder_proc_unlock(proc);
2933 mutex_lock(&context->context_mgr_node_lock);
2934 target_node = context->binder_context_mgr_node;
2936 target_node = binder_get_node_refs_for_txn(
2937 target_node, &target_proc,
2940 return_error = BR_DEAD_REPLY;
2941 mutex_unlock(&context->context_mgr_node_lock);
2942 if (target_node && target_proc->pid == proc->pid) {
2943 binder_user_error("%d:%d got transaction to context manager from process owning it\n",
2944 proc->pid, thread->pid);
2945 return_error = BR_FAILED_REPLY;
2946 return_error_param = -EINVAL;
2947 return_error_line = __LINE__;
2948 goto err_invalid_target_handle;
2953 * return_error is set above
2955 return_error_param = -EINVAL;
2956 return_error_line = __LINE__;
2957 goto err_dead_binder;
2959 e->to_node = target_node->debug_id;
2960 if (WARN_ON(proc == target_proc)) {
2961 return_error = BR_FAILED_REPLY;
2962 return_error_param = -EINVAL;
2963 return_error_line = __LINE__;
2964 goto err_invalid_target_handle;
2966 if (security_binder_transaction(proc->tsk,
2967 target_proc->tsk) < 0) {
2968 return_error = BR_FAILED_REPLY;
2969 return_error_param = -EPERM;
2970 return_error_line = __LINE__;
2971 goto err_invalid_target_handle;
2973 binder_inner_proc_lock(proc);
2975 w = list_first_entry_or_null(&thread->todo,
2976 struct binder_work, entry);
2977 if (!(tr->flags & TF_ONE_WAY) && w &&
2978 w->type == BINDER_WORK_TRANSACTION) {
2980 * Do not allow new outgoing transaction from a
2981 * thread that has a transaction at the head of
2982 * its todo list. Only need to check the head
2983 * because binder_select_thread_ilocked picks a
2984 * thread from proc->waiting_threads to enqueue
2985 * the transaction, and nothing is queued to the
2986 * todo list while the thread is on waiting_threads.
2988 binder_user_error("%d:%d new transaction not allowed when there is a transaction on thread todo\n",
2989 proc->pid, thread->pid);
2990 binder_inner_proc_unlock(proc);
2991 return_error = BR_FAILED_REPLY;
2992 return_error_param = -EPROTO;
2993 return_error_line = __LINE__;
2994 goto err_bad_todo_list;
2997 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
2998 struct binder_transaction *tmp;
3000 tmp = thread->transaction_stack;
3001 if (tmp->to_thread != thread) {
3002 spin_lock(&tmp->lock);
3003 binder_user_error("%d:%d got new transaction with bad transaction stack, transaction %d has target %d:%d\n",
3004 proc->pid, thread->pid, tmp->debug_id,
3005 tmp->to_proc ? tmp->to_proc->pid : 0,
3007 tmp->to_thread->pid : 0);
3008 spin_unlock(&tmp->lock);
3009 binder_inner_proc_unlock(proc);
3010 return_error = BR_FAILED_REPLY;
3011 return_error_param = -EPROTO;
3012 return_error_line = __LINE__;
3013 goto err_bad_call_stack;
3016 struct binder_thread *from;
3018 spin_lock(&tmp->lock);
3020 if (from && from->proc == target_proc) {
3021 atomic_inc(&from->tmp_ref);
3022 target_thread = from;
3023 spin_unlock(&tmp->lock);
3026 spin_unlock(&tmp->lock);
3027 tmp = tmp->from_parent;
3030 binder_inner_proc_unlock(proc);
3033 e->to_thread = target_thread->pid;
3034 e->to_proc = target_proc->pid;
3036 /* TODO: reuse incoming transaction for reply */
3037 t = kzalloc(sizeof(*t), GFP_KERNEL);
3039 return_error = BR_FAILED_REPLY;
3040 return_error_param = -ENOMEM;
3041 return_error_line = __LINE__;
3042 goto err_alloc_t_failed;
3044 INIT_LIST_HEAD(&t->fd_fixups);
3045 binder_stats_created(BINDER_STAT_TRANSACTION);
3046 spin_lock_init(&t->lock);
3048 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
3049 if (tcomplete == NULL) {
3050 return_error = BR_FAILED_REPLY;
3051 return_error_param = -ENOMEM;
3052 return_error_line = __LINE__;
3053 goto err_alloc_tcomplete_failed;
3055 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
3057 t->debug_id = t_debug_id;
3060 binder_debug(BINDER_DEBUG_TRANSACTION,
3061 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
3062 proc->pid, thread->pid, t->debug_id,
3063 target_proc->pid, target_thread->pid,
3064 (u64)tr->data.ptr.buffer,
3065 (u64)tr->data.ptr.offsets,
3066 (u64)tr->data_size, (u64)tr->offsets_size,
3067 (u64)extra_buffers_size);
3069 binder_debug(BINDER_DEBUG_TRANSACTION,
3070 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
3071 proc->pid, thread->pid, t->debug_id,
3072 target_proc->pid, target_node->debug_id,
3073 (u64)tr->data.ptr.buffer,
3074 (u64)tr->data.ptr.offsets,
3075 (u64)tr->data_size, (u64)tr->offsets_size,
3076 (u64)extra_buffers_size);
3078 if (!reply && !(tr->flags & TF_ONE_WAY))
3082 t->sender_euid = task_euid(proc->tsk);
3083 t->to_proc = target_proc;
3084 t->to_thread = target_thread;
3086 t->flags = tr->flags;
3087 t->priority = task_nice(current);
3089 if (target_node && target_node->txn_security_ctx) {
3093 security_task_getsecid(proc->tsk, &secid);
3094 ret = security_secid_to_secctx(secid, &secctx, &secctx_sz);
3096 return_error = BR_FAILED_REPLY;
3097 return_error_param = ret;
3098 return_error_line = __LINE__;
3099 goto err_get_secctx_failed;
3101 added_size = ALIGN(secctx_sz, sizeof(u64));
3102 extra_buffers_size += added_size;
3103 if (extra_buffers_size < added_size) {
3104 /* integer overflow of extra_buffers_size */
3105 return_error = BR_FAILED_REPLY;
3106 return_error_param = EINVAL;
3107 return_error_line = __LINE__;
3108 goto err_bad_extra_size;
3112 trace_binder_transaction(reply, t, target_node);
3114 t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size,
3115 tr->offsets_size, extra_buffers_size,
3116 !reply && (t->flags & TF_ONE_WAY), current->tgid);
3117 if (IS_ERR(t->buffer)) {
3119 * -ESRCH indicates VMA cleared. The target is dying.
3121 return_error_param = PTR_ERR(t->buffer);
3122 return_error = return_error_param == -ESRCH ?
3123 BR_DEAD_REPLY : BR_FAILED_REPLY;
3124 return_error_line = __LINE__;
3126 goto err_binder_alloc_buf_failed;
3130 size_t buf_offset = ALIGN(tr->data_size, sizeof(void *)) +
3131 ALIGN(tr->offsets_size, sizeof(void *)) +
3132 ALIGN(extra_buffers_size, sizeof(void *)) -
3133 ALIGN(secctx_sz, sizeof(u64));
3135 t->security_ctx = (uintptr_t)t->buffer->user_data + buf_offset;
3136 err = binder_alloc_copy_to_buffer(&target_proc->alloc,
3137 t->buffer, buf_offset,
3140 t->security_ctx = 0;
3143 security_release_secctx(secctx, secctx_sz);
3146 t->buffer->debug_id = t->debug_id;
3147 t->buffer->transaction = t;
3148 t->buffer->target_node = target_node;
3149 trace_binder_transaction_alloc_buf(t->buffer);
3151 if (binder_alloc_copy_user_to_buffer(
3152 &target_proc->alloc,
3154 (const void __user *)
3155 (uintptr_t)tr->data.ptr.buffer,
3157 binder_user_error("%d:%d got transaction with invalid data ptr\n",
3158 proc->pid, thread->pid);
3159 return_error = BR_FAILED_REPLY;
3160 return_error_param = -EFAULT;
3161 return_error_line = __LINE__;
3162 goto err_copy_data_failed;
3164 if (binder_alloc_copy_user_to_buffer(
3165 &target_proc->alloc,
3167 ALIGN(tr->data_size, sizeof(void *)),
3168 (const void __user *)
3169 (uintptr_t)tr->data.ptr.offsets,
3170 tr->offsets_size)) {
3171 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3172 proc->pid, thread->pid);
3173 return_error = BR_FAILED_REPLY;
3174 return_error_param = -EFAULT;
3175 return_error_line = __LINE__;
3176 goto err_copy_data_failed;
3178 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
3179 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
3180 proc->pid, thread->pid, (u64)tr->offsets_size);
3181 return_error = BR_FAILED_REPLY;
3182 return_error_param = -EINVAL;
3183 return_error_line = __LINE__;
3184 goto err_bad_offset;
3186 if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) {
3187 binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
3188 proc->pid, thread->pid,
3189 (u64)extra_buffers_size);
3190 return_error = BR_FAILED_REPLY;
3191 return_error_param = -EINVAL;
3192 return_error_line = __LINE__;
3193 goto err_bad_offset;
3195 off_start_offset = ALIGN(tr->data_size, sizeof(void *));
3196 buffer_offset = off_start_offset;
3197 off_end_offset = off_start_offset + tr->offsets_size;
3198 sg_buf_offset = ALIGN(off_end_offset, sizeof(void *));
3199 sg_buf_end_offset = sg_buf_offset + extra_buffers_size -
3200 ALIGN(secctx_sz, sizeof(u64));
3202 for (buffer_offset = off_start_offset; buffer_offset < off_end_offset;
3203 buffer_offset += sizeof(binder_size_t)) {
3204 struct binder_object_header *hdr;
3206 struct binder_object object;
3207 binder_size_t object_offset;
3209 if (binder_alloc_copy_from_buffer(&target_proc->alloc,
3213 sizeof(object_offset))) {
3214 return_error = BR_FAILED_REPLY;
3215 return_error_param = -EINVAL;
3216 return_error_line = __LINE__;
3217 goto err_bad_offset;
3219 object_size = binder_get_object(target_proc, t->buffer,
3220 object_offset, &object);
3221 if (object_size == 0 || object_offset < off_min) {
3222 binder_user_error("%d:%d got transaction with invalid offset (%lld, min %lld max %lld) or object.\n",
3223 proc->pid, thread->pid,
3226 (u64)t->buffer->data_size);
3227 return_error = BR_FAILED_REPLY;
3228 return_error_param = -EINVAL;
3229 return_error_line = __LINE__;
3230 goto err_bad_offset;
3234 off_min = object_offset + object_size;
3235 switch (hdr->type) {
3236 case BINDER_TYPE_BINDER:
3237 case BINDER_TYPE_WEAK_BINDER: {
3238 struct flat_binder_object *fp;
3240 fp = to_flat_binder_object(hdr);
3241 ret = binder_translate_binder(fp, t, thread);
3244 binder_alloc_copy_to_buffer(&target_proc->alloc,
3248 return_error = BR_FAILED_REPLY;
3249 return_error_param = ret;
3250 return_error_line = __LINE__;
3251 goto err_translate_failed;
3254 case BINDER_TYPE_HANDLE:
3255 case BINDER_TYPE_WEAK_HANDLE: {
3256 struct flat_binder_object *fp;
3258 fp = to_flat_binder_object(hdr);
3259 ret = binder_translate_handle(fp, t, thread);
3261 binder_alloc_copy_to_buffer(&target_proc->alloc,
3265 return_error = BR_FAILED_REPLY;
3266 return_error_param = ret;
3267 return_error_line = __LINE__;
3268 goto err_translate_failed;
3272 case BINDER_TYPE_FD: {
3273 struct binder_fd_object *fp = to_binder_fd_object(hdr);
3274 binder_size_t fd_offset = object_offset +
3275 (uintptr_t)&fp->fd - (uintptr_t)fp;
3276 int ret = binder_translate_fd(fp->fd, fd_offset, t,
3277 thread, in_reply_to);
3281 binder_alloc_copy_to_buffer(&target_proc->alloc,
3285 return_error = BR_FAILED_REPLY;
3286 return_error_param = ret;
3287 return_error_line = __LINE__;
3288 goto err_translate_failed;
3291 case BINDER_TYPE_FDA: {
3292 struct binder_object ptr_object;
3293 binder_size_t parent_offset;
3294 struct binder_fd_array_object *fda =
3295 to_binder_fd_array_object(hdr);
3296 size_t num_valid = (buffer_offset - off_start_offset) /
3297 sizeof(binder_size_t);
3298 struct binder_buffer_object *parent =
3299 binder_validate_ptr(target_proc, t->buffer,
3300 &ptr_object, fda->parent,
3305 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
3306 proc->pid, thread->pid);
3307 return_error = BR_FAILED_REPLY;
3308 return_error_param = -EINVAL;
3309 return_error_line = __LINE__;
3310 goto err_bad_parent;
3312 if (!binder_validate_fixup(target_proc, t->buffer,
3317 last_fixup_min_off)) {
3318 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
3319 proc->pid, thread->pid);
3320 return_error = BR_FAILED_REPLY;
3321 return_error_param = -EINVAL;
3322 return_error_line = __LINE__;
3323 goto err_bad_parent;
3325 ret = binder_translate_fd_array(fda, parent, t, thread,
3328 return_error = BR_FAILED_REPLY;
3329 return_error_param = ret;
3330 return_error_line = __LINE__;
3331 goto err_translate_failed;
3333 last_fixup_obj_off = parent_offset;
3334 last_fixup_min_off =
3335 fda->parent_offset + sizeof(u32) * fda->num_fds;
3337 case BINDER_TYPE_PTR: {
3338 struct binder_buffer_object *bp =
3339 to_binder_buffer_object(hdr);
3340 size_t buf_left = sg_buf_end_offset - sg_buf_offset;
3343 if (bp->length > buf_left) {
3344 binder_user_error("%d:%d got transaction with too large buffer\n",
3345 proc->pid, thread->pid);
3346 return_error = BR_FAILED_REPLY;
3347 return_error_param = -EINVAL;
3348 return_error_line = __LINE__;
3349 goto err_bad_offset;
3351 if (binder_alloc_copy_user_to_buffer(
3352 &target_proc->alloc,
3355 (const void __user *)
3356 (uintptr_t)bp->buffer,
3358 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3359 proc->pid, thread->pid);
3360 return_error_param = -EFAULT;
3361 return_error = BR_FAILED_REPLY;
3362 return_error_line = __LINE__;
3363 goto err_copy_data_failed;
3365 /* Fixup buffer pointer to target proc address space */
3366 bp->buffer = (uintptr_t)
3367 t->buffer->user_data + sg_buf_offset;
3368 sg_buf_offset += ALIGN(bp->length, sizeof(u64));
3370 num_valid = (buffer_offset - off_start_offset) /
3371 sizeof(binder_size_t);
3372 ret = binder_fixup_parent(t, thread, bp,
3376 last_fixup_min_off);
3378 binder_alloc_copy_to_buffer(&target_proc->alloc,
3382 return_error = BR_FAILED_REPLY;
3383 return_error_param = ret;
3384 return_error_line = __LINE__;
3385 goto err_translate_failed;
3387 last_fixup_obj_off = object_offset;
3388 last_fixup_min_off = 0;
3391 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
3392 proc->pid, thread->pid, hdr->type);
3393 return_error = BR_FAILED_REPLY;
3394 return_error_param = -EINVAL;
3395 return_error_line = __LINE__;
3396 goto err_bad_object_type;
3399 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
3400 t->work.type = BINDER_WORK_TRANSACTION;
3403 binder_enqueue_thread_work(thread, tcomplete);
3404 binder_inner_proc_lock(target_proc);
3405 if (target_thread->is_dead) {
3406 binder_inner_proc_unlock(target_proc);
3407 goto err_dead_proc_or_thread;
3409 BUG_ON(t->buffer->async_transaction != 0);
3410 binder_pop_transaction_ilocked(target_thread, in_reply_to);
3411 binder_enqueue_thread_work_ilocked(target_thread, &t->work);
3412 binder_inner_proc_unlock(target_proc);
3413 wake_up_interruptible_sync(&target_thread->wait);
3414 binder_free_transaction(in_reply_to);
3415 } else if (!(t->flags & TF_ONE_WAY)) {
3416 BUG_ON(t->buffer->async_transaction != 0);
3417 binder_inner_proc_lock(proc);
3419 * Defer the TRANSACTION_COMPLETE, so we don't return to
3420 * userspace immediately; this allows the target process to
3421 * immediately start processing this transaction, reducing
3422 * latency. We will then return the TRANSACTION_COMPLETE when
3423 * the target replies (or there is an error).
3425 binder_enqueue_deferred_thread_work_ilocked(thread, tcomplete);
3427 t->from_parent = thread->transaction_stack;
3428 thread->transaction_stack = t;
3429 binder_inner_proc_unlock(proc);
3430 if (!binder_proc_transaction(t, target_proc, target_thread)) {
3431 binder_inner_proc_lock(proc);
3432 binder_pop_transaction_ilocked(thread, t);
3433 binder_inner_proc_unlock(proc);
3434 goto err_dead_proc_or_thread;
3437 BUG_ON(target_node == NULL);
3438 BUG_ON(t->buffer->async_transaction != 1);
3439 binder_enqueue_thread_work(thread, tcomplete);
3440 if (!binder_proc_transaction(t, target_proc, NULL))
3441 goto err_dead_proc_or_thread;
3444 binder_thread_dec_tmpref(target_thread);
3445 binder_proc_dec_tmpref(target_proc);
3447 binder_dec_node_tmpref(target_node);
3449 * write barrier to synchronize with initialization
3453 WRITE_ONCE(e->debug_id_done, t_debug_id);
3456 err_dead_proc_or_thread:
3457 return_error = BR_DEAD_REPLY;
3458 return_error_line = __LINE__;
3459 binder_dequeue_work(proc, tcomplete);
3460 err_translate_failed:
3461 err_bad_object_type:
3464 err_copy_data_failed:
3465 binder_free_txn_fixups(t);
3466 trace_binder_transaction_failed_buffer_release(t->buffer);
3467 binder_transaction_buffer_release(target_proc, t->buffer,
3468 buffer_offset, true);
3470 binder_dec_node_tmpref(target_node);
3472 t->buffer->transaction = NULL;
3473 binder_alloc_free_buf(&target_proc->alloc, t->buffer);
3474 err_binder_alloc_buf_failed:
3477 security_release_secctx(secctx, secctx_sz);
3478 err_get_secctx_failed:
3480 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3481 err_alloc_tcomplete_failed:
3483 binder_stats_deleted(BINDER_STAT_TRANSACTION);
3487 err_empty_call_stack:
3489 err_invalid_target_handle:
3491 binder_thread_dec_tmpref(target_thread);
3493 binder_proc_dec_tmpref(target_proc);
3495 binder_dec_node(target_node, 1, 0);
3496 binder_dec_node_tmpref(target_node);
3499 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
3500 "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
3501 proc->pid, thread->pid, return_error, return_error_param,
3502 (u64)tr->data_size, (u64)tr->offsets_size,
3506 struct binder_transaction_log_entry *fe;
3508 e->return_error = return_error;
3509 e->return_error_param = return_error_param;
3510 e->return_error_line = return_error_line;
3511 fe = binder_transaction_log_add(&binder_transaction_log_failed);
3514 * write barrier to synchronize with initialization
3518 WRITE_ONCE(e->debug_id_done, t_debug_id);
3519 WRITE_ONCE(fe->debug_id_done, t_debug_id);
3522 BUG_ON(thread->return_error.cmd != BR_OK);
3524 thread->return_error.cmd = BR_TRANSACTION_COMPLETE;
3525 binder_enqueue_thread_work(thread, &thread->return_error.work);
3526 binder_send_failed_reply(in_reply_to, return_error);
3528 thread->return_error.cmd = return_error;
3529 binder_enqueue_thread_work(thread, &thread->return_error.work);
3534 * binder_free_buf() - free the specified buffer
3535 * @proc: binder proc that owns buffer
3536 * @buffer: buffer to be freed
3538 * If buffer for an async transaction, enqueue the next async
3539 * transaction from the node.
3541 * Cleanup buffer and free it.
3544 binder_free_buf(struct binder_proc *proc, struct binder_buffer *buffer)
3546 binder_inner_proc_lock(proc);
3547 if (buffer->transaction) {
3548 buffer->transaction->buffer = NULL;
3549 buffer->transaction = NULL;
3551 binder_inner_proc_unlock(proc);
3552 if (buffer->async_transaction && buffer->target_node) {
3553 struct binder_node *buf_node;
3554 struct binder_work *w;
3556 buf_node = buffer->target_node;
3557 binder_node_inner_lock(buf_node);
3558 BUG_ON(!buf_node->has_async_transaction);
3559 BUG_ON(buf_node->proc != proc);
3560 w = binder_dequeue_work_head_ilocked(
3561 &buf_node->async_todo);
3563 buf_node->has_async_transaction = false;
3565 binder_enqueue_work_ilocked(
3567 binder_wakeup_proc_ilocked(proc);
3569 binder_node_inner_unlock(buf_node);
3571 trace_binder_transaction_buffer_release(buffer);
3572 binder_transaction_buffer_release(proc, buffer, 0, false);
3573 binder_alloc_free_buf(&proc->alloc, buffer);
3576 static int binder_thread_write(struct binder_proc *proc,
3577 struct binder_thread *thread,
3578 binder_uintptr_t binder_buffer, size_t size,
3579 binder_size_t *consumed)
3582 struct binder_context *context = proc->context;
3583 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
3584 void __user *ptr = buffer + *consumed;
3585 void __user *end = buffer + size;
3587 while (ptr < end && thread->return_error.cmd == BR_OK) {
3590 if (get_user(cmd, (uint32_t __user *)ptr))
3592 ptr += sizeof(uint32_t);
3593 trace_binder_command(cmd);
3594 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
3595 atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]);
3596 atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]);
3597 atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]);
3605 const char *debug_string;
3606 bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE;
3607 bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE;
3608 struct binder_ref_data rdata;
3610 if (get_user(target, (uint32_t __user *)ptr))
3613 ptr += sizeof(uint32_t);
3615 if (increment && !target) {
3616 struct binder_node *ctx_mgr_node;
3617 mutex_lock(&context->context_mgr_node_lock);
3618 ctx_mgr_node = context->binder_context_mgr_node;
3620 if (ctx_mgr_node->proc == proc) {
3621 binder_user_error("%d:%d context manager tried to acquire desc 0\n",
3622 proc->pid, thread->pid);
3623 mutex_unlock(&context->context_mgr_node_lock);
3626 ret = binder_inc_ref_for_node(
3628 strong, NULL, &rdata);
3630 mutex_unlock(&context->context_mgr_node_lock);
3633 ret = binder_update_ref_for_handle(
3634 proc, target, increment, strong,
3636 if (!ret && rdata.desc != target) {
3637 binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
3638 proc->pid, thread->pid,
3639 target, rdata.desc);
3643 debug_string = "IncRefs";
3646 debug_string = "Acquire";
3649 debug_string = "Release";
3653 debug_string = "DecRefs";
3657 binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
3658 proc->pid, thread->pid, debug_string,
3659 strong, target, ret);
3662 binder_debug(BINDER_DEBUG_USER_REFS,
3663 "%d:%d %s ref %d desc %d s %d w %d\n",
3664 proc->pid, thread->pid, debug_string,
3665 rdata.debug_id, rdata.desc, rdata.strong,
3669 case BC_INCREFS_DONE:
3670 case BC_ACQUIRE_DONE: {
3671 binder_uintptr_t node_ptr;
3672 binder_uintptr_t cookie;
3673 struct binder_node *node;
3676 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
3678 ptr += sizeof(binder_uintptr_t);
3679 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
3681 ptr += sizeof(binder_uintptr_t);
3682 node = binder_get_node(proc, node_ptr);
3684 binder_user_error("%d:%d %s u%016llx no match\n",
3685 proc->pid, thread->pid,
3686 cmd == BC_INCREFS_DONE ?
3692 if (cookie != node->cookie) {
3693 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
3694 proc->pid, thread->pid,
3695 cmd == BC_INCREFS_DONE ?
3696 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
3697 (u64)node_ptr, node->debug_id,
3698 (u64)cookie, (u64)node->cookie);
3699 binder_put_node(node);
3702 binder_node_inner_lock(node);
3703 if (cmd == BC_ACQUIRE_DONE) {
3704 if (node->pending_strong_ref == 0) {
3705 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
3706 proc->pid, thread->pid,
3708 binder_node_inner_unlock(node);
3709 binder_put_node(node);
3712 node->pending_strong_ref = 0;
3714 if (node->pending_weak_ref == 0) {
3715 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
3716 proc->pid, thread->pid,
3718 binder_node_inner_unlock(node);
3719 binder_put_node(node);
3722 node->pending_weak_ref = 0;
3724 free_node = binder_dec_node_nilocked(node,
3725 cmd == BC_ACQUIRE_DONE, 0);
3727 binder_debug(BINDER_DEBUG_USER_REFS,
3728 "%d:%d %s node %d ls %d lw %d tr %d\n",
3729 proc->pid, thread->pid,
3730 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
3731 node->debug_id, node->local_strong_refs,
3732 node->local_weak_refs, node->tmp_refs);
3733 binder_node_inner_unlock(node);
3734 binder_put_node(node);
3737 case BC_ATTEMPT_ACQUIRE:
3738 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
3740 case BC_ACQUIRE_RESULT:
3741 pr_err("BC_ACQUIRE_RESULT not supported\n");
3744 case BC_FREE_BUFFER: {
3745 binder_uintptr_t data_ptr;
3746 struct binder_buffer *buffer;
3748 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
3750 ptr += sizeof(binder_uintptr_t);
3752 buffer = binder_alloc_prepare_to_free(&proc->alloc,
3754 if (IS_ERR_OR_NULL(buffer)) {
3755 if (PTR_ERR(buffer) == -EPERM) {
3757 "%d:%d BC_FREE_BUFFER u%016llx matched unreturned or currently freeing buffer\n",
3758 proc->pid, thread->pid,
3762 "%d:%d BC_FREE_BUFFER u%016llx no match\n",
3763 proc->pid, thread->pid,
3768 binder_debug(BINDER_DEBUG_FREE_BUFFER,
3769 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
3770 proc->pid, thread->pid, (u64)data_ptr,
3772 buffer->transaction ? "active" : "finished");
3773 binder_free_buf(proc, buffer);
3777 case BC_TRANSACTION_SG:
3779 struct binder_transaction_data_sg tr;
3781 if (copy_from_user(&tr, ptr, sizeof(tr)))
3784 binder_transaction(proc, thread, &tr.transaction_data,
3785 cmd == BC_REPLY_SG, tr.buffers_size);
3788 case BC_TRANSACTION:
3790 struct binder_transaction_data tr;
3792 if (copy_from_user(&tr, ptr, sizeof(tr)))
3795 binder_transaction(proc, thread, &tr,
3796 cmd == BC_REPLY, 0);
3800 case BC_REGISTER_LOOPER:
3801 binder_debug(BINDER_DEBUG_THREADS,
3802 "%d:%d BC_REGISTER_LOOPER\n",
3803 proc->pid, thread->pid);
3804 binder_inner_proc_lock(proc);
3805 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
3806 thread->looper |= BINDER_LOOPER_STATE_INVALID;
3807 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
3808 proc->pid, thread->pid);
3809 } else if (proc->requested_threads == 0) {
3810 thread->looper |= BINDER_LOOPER_STATE_INVALID;
3811 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
3812 proc->pid, thread->pid);
3814 proc->requested_threads--;
3815 proc->requested_threads_started++;
3817 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
3818 binder_inner_proc_unlock(proc);
3820 case BC_ENTER_LOOPER:
3821 binder_debug(BINDER_DEBUG_THREADS,
3822 "%d:%d BC_ENTER_LOOPER\n",
3823 proc->pid, thread->pid);
3824 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
3825 thread->looper |= BINDER_LOOPER_STATE_INVALID;
3826 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
3827 proc->pid, thread->pid);
3829 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
3831 case BC_EXIT_LOOPER:
3832 binder_debug(BINDER_DEBUG_THREADS,
3833 "%d:%d BC_EXIT_LOOPER\n",
3834 proc->pid, thread->pid);
3835 thread->looper |= BINDER_LOOPER_STATE_EXITED;
3838 case BC_REQUEST_DEATH_NOTIFICATION:
3839 case BC_CLEAR_DEATH_NOTIFICATION: {
3841 binder_uintptr_t cookie;
3842 struct binder_ref *ref;
3843 struct binder_ref_death *death = NULL;
3845 if (get_user(target, (uint32_t __user *)ptr))
3847 ptr += sizeof(uint32_t);
3848 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
3850 ptr += sizeof(binder_uintptr_t);
3851 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3853 * Allocate memory for death notification
3854 * before taking lock
3856 death = kzalloc(sizeof(*death), GFP_KERNEL);
3857 if (death == NULL) {
3858 WARN_ON(thread->return_error.cmd !=
3860 thread->return_error.cmd = BR_ERROR;
3861 binder_enqueue_thread_work(
3863 &thread->return_error.work);
3865 BINDER_DEBUG_FAILED_TRANSACTION,
3866 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
3867 proc->pid, thread->pid);
3871 binder_proc_lock(proc);
3872 ref = binder_get_ref_olocked(proc, target, false);
3874 binder_user_error("%d:%d %s invalid ref %d\n",
3875 proc->pid, thread->pid,
3876 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3877 "BC_REQUEST_DEATH_NOTIFICATION" :
3878 "BC_CLEAR_DEATH_NOTIFICATION",
3880 binder_proc_unlock(proc);
3885 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
3886 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
3887 proc->pid, thread->pid,
3888 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3889 "BC_REQUEST_DEATH_NOTIFICATION" :
3890 "BC_CLEAR_DEATH_NOTIFICATION",
3891 (u64)cookie, ref->data.debug_id,
3892 ref->data.desc, ref->data.strong,
3893 ref->data.weak, ref->node->debug_id);
3895 binder_node_lock(ref->node);
3896 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3898 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
3899 proc->pid, thread->pid);
3900 binder_node_unlock(ref->node);
3901 binder_proc_unlock(proc);
3905 binder_stats_created(BINDER_STAT_DEATH);
3906 INIT_LIST_HEAD(&death->work.entry);
3907 death->cookie = cookie;
3909 if (ref->node->proc == NULL) {
3910 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
3912 binder_inner_proc_lock(proc);
3913 binder_enqueue_work_ilocked(
3914 &ref->death->work, &proc->todo);
3915 binder_wakeup_proc_ilocked(proc);
3916 binder_inner_proc_unlock(proc);
3919 if (ref->death == NULL) {
3920 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
3921 proc->pid, thread->pid);
3922 binder_node_unlock(ref->node);
3923 binder_proc_unlock(proc);
3927 if (death->cookie != cookie) {
3928 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
3929 proc->pid, thread->pid,
3932 binder_node_unlock(ref->node);
3933 binder_proc_unlock(proc);
3937 binder_inner_proc_lock(proc);
3938 if (list_empty(&death->work.entry)) {
3939 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
3940 if (thread->looper &
3941 (BINDER_LOOPER_STATE_REGISTERED |
3942 BINDER_LOOPER_STATE_ENTERED))
3943 binder_enqueue_thread_work_ilocked(
3947 binder_enqueue_work_ilocked(
3950 binder_wakeup_proc_ilocked(
3954 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
3955 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
3957 binder_inner_proc_unlock(proc);
3959 binder_node_unlock(ref->node);
3960 binder_proc_unlock(proc);
3962 case BC_DEAD_BINDER_DONE: {
3963 struct binder_work *w;
3964 binder_uintptr_t cookie;
3965 struct binder_ref_death *death = NULL;
3967 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
3970 ptr += sizeof(cookie);
3971 binder_inner_proc_lock(proc);
3972 list_for_each_entry(w, &proc->delivered_death,
3974 struct binder_ref_death *tmp_death =
3976 struct binder_ref_death,
3979 if (tmp_death->cookie == cookie) {
3984 binder_debug(BINDER_DEBUG_DEAD_BINDER,
3985 "%d:%d BC_DEAD_BINDER_DONE %016llx found %pK\n",
3986 proc->pid, thread->pid, (u64)cookie,
3988 if (death == NULL) {
3989 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
3990 proc->pid, thread->pid, (u64)cookie);
3991 binder_inner_proc_unlock(proc);
3994 binder_dequeue_work_ilocked(&death->work);
3995 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
3996 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
3997 if (thread->looper &
3998 (BINDER_LOOPER_STATE_REGISTERED |
3999 BINDER_LOOPER_STATE_ENTERED))
4000 binder_enqueue_thread_work_ilocked(
4001 thread, &death->work);
4003 binder_enqueue_work_ilocked(
4006 binder_wakeup_proc_ilocked(proc);
4009 binder_inner_proc_unlock(proc);
4013 pr_err("%d:%d unknown command %d\n",
4014 proc->pid, thread->pid, cmd);
4017 *consumed = ptr - buffer;
4022 static void binder_stat_br(struct binder_proc *proc,
4023 struct binder_thread *thread, uint32_t cmd)
4025 trace_binder_return(cmd);
4026 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
4027 atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
4028 atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
4029 atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
4033 static int binder_put_node_cmd(struct binder_proc *proc,
4034 struct binder_thread *thread,
4036 binder_uintptr_t node_ptr,
4037 binder_uintptr_t node_cookie,
4039 uint32_t cmd, const char *cmd_name)
4041 void __user *ptr = *ptrp;
4043 if (put_user(cmd, (uint32_t __user *)ptr))
4045 ptr += sizeof(uint32_t);
4047 if (put_user(node_ptr, (binder_uintptr_t __user *)ptr))
4049 ptr += sizeof(binder_uintptr_t);
4051 if (put_user(node_cookie, (binder_uintptr_t __user *)ptr))
4053 ptr += sizeof(binder_uintptr_t);
4055 binder_stat_br(proc, thread, cmd);
4056 binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n",
4057 proc->pid, thread->pid, cmd_name, node_debug_id,
4058 (u64)node_ptr, (u64)node_cookie);
4064 static int binder_wait_for_work(struct binder_thread *thread,
4068 struct binder_proc *proc = thread->proc;
4071 freezer_do_not_count();
4072 binder_inner_proc_lock(proc);
4074 prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE);
4075 if (binder_has_work_ilocked(thread, do_proc_work))
4078 list_add(&thread->waiting_thread_node,
4079 &proc->waiting_threads);
4080 binder_inner_proc_unlock(proc);
4082 binder_inner_proc_lock(proc);
4083 list_del_init(&thread->waiting_thread_node);
4084 if (signal_pending(current)) {
4089 finish_wait(&thread->wait, &wait);
4090 binder_inner_proc_unlock(proc);
4097 * binder_apply_fd_fixups() - finish fd translation
4098 * @proc: binder_proc associated @t->buffer
4099 * @t: binder transaction with list of fd fixups
4101 * Now that we are in the context of the transaction target
4102 * process, we can allocate and install fds. Process the
4103 * list of fds to translate and fixup the buffer with the
4106 * If we fail to allocate an fd, then free the resources by
4107 * fput'ing files that have not been processed and ksys_close'ing
4108 * any fds that have already been allocated.
4110 static int binder_apply_fd_fixups(struct binder_proc *proc,
4111 struct binder_transaction *t)
4113 struct binder_txn_fd_fixup *fixup, *tmp;
4116 list_for_each_entry(fixup, &t->fd_fixups, fixup_entry) {
4117 int fd = get_unused_fd_flags(O_CLOEXEC);
4120 binder_debug(BINDER_DEBUG_TRANSACTION,
4121 "failed fd fixup txn %d fd %d\n",
4126 binder_debug(BINDER_DEBUG_TRANSACTION,
4127 "fd fixup txn %d fd %d\n",
4129 trace_binder_transaction_fd_recv(t, fd, fixup->offset);
4130 fd_install(fd, fixup->file);
4132 if (binder_alloc_copy_to_buffer(&proc->alloc, t->buffer,
4139 list_for_each_entry_safe(fixup, tmp, &t->fd_fixups, fixup_entry) {
4146 err = binder_alloc_copy_from_buffer(&proc->alloc, &fd,
4152 binder_deferred_fd_close(fd);
4154 list_del(&fixup->fixup_entry);
4161 static int binder_thread_read(struct binder_proc *proc,
4162 struct binder_thread *thread,
4163 binder_uintptr_t binder_buffer, size_t size,
4164 binder_size_t *consumed, int non_block)
4166 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
4167 void __user *ptr = buffer + *consumed;
4168 void __user *end = buffer + size;
4171 int wait_for_proc_work;
4173 if (*consumed == 0) {
4174 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
4176 ptr += sizeof(uint32_t);
4180 binder_inner_proc_lock(proc);
4181 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
4182 binder_inner_proc_unlock(proc);
4184 thread->looper |= BINDER_LOOPER_STATE_WAITING;
4186 trace_binder_wait_for_work(wait_for_proc_work,
4187 !!thread->transaction_stack,
4188 !binder_worklist_empty(proc, &thread->todo));
4189 if (wait_for_proc_work) {
4190 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4191 BINDER_LOOPER_STATE_ENTERED))) {
4192 binder_user_error("%d:%d ERROR: Thread waiting for process work before calling BC_REGISTER_LOOPER or BC_ENTER_LOOPER (state %x)\n",
4193 proc->pid, thread->pid, thread->looper);
4194 wait_event_interruptible(binder_user_error_wait,
4195 binder_stop_on_user_error < 2);
4197 binder_set_nice(proc->default_priority);
4201 if (!binder_has_work(thread, wait_for_proc_work))
4204 ret = binder_wait_for_work(thread, wait_for_proc_work);
4207 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
4214 struct binder_transaction_data_secctx tr;
4215 struct binder_transaction_data *trd = &tr.transaction_data;
4216 struct binder_work *w = NULL;
4217 struct list_head *list = NULL;
4218 struct binder_transaction *t = NULL;
4219 struct binder_thread *t_from;
4220 size_t trsize = sizeof(*trd);
4222 binder_inner_proc_lock(proc);
4223 if (!binder_worklist_empty_ilocked(&thread->todo))
4224 list = &thread->todo;
4225 else if (!binder_worklist_empty_ilocked(&proc->todo) &&
4229 binder_inner_proc_unlock(proc);
4232 if (ptr - buffer == 4 && !thread->looper_need_return)
4237 if (end - ptr < sizeof(tr) + 4) {
4238 binder_inner_proc_unlock(proc);
4241 w = binder_dequeue_work_head_ilocked(list);
4242 if (binder_worklist_empty_ilocked(&thread->todo))
4243 thread->process_todo = false;
4246 case BINDER_WORK_TRANSACTION: {
4247 binder_inner_proc_unlock(proc);
4248 t = container_of(w, struct binder_transaction, work);
4250 case BINDER_WORK_RETURN_ERROR: {
4251 struct binder_error *e = container_of(
4252 w, struct binder_error, work);
4254 WARN_ON(e->cmd == BR_OK);
4255 binder_inner_proc_unlock(proc);
4256 if (put_user(e->cmd, (uint32_t __user *)ptr))
4260 ptr += sizeof(uint32_t);
4262 binder_stat_br(proc, thread, cmd);
4264 case BINDER_WORK_TRANSACTION_COMPLETE: {
4265 binder_inner_proc_unlock(proc);
4266 cmd = BR_TRANSACTION_COMPLETE;
4268 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4269 if (put_user(cmd, (uint32_t __user *)ptr))
4271 ptr += sizeof(uint32_t);
4273 binder_stat_br(proc, thread, cmd);
4274 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
4275 "%d:%d BR_TRANSACTION_COMPLETE\n",
4276 proc->pid, thread->pid);
4278 case BINDER_WORK_NODE: {
4279 struct binder_node *node = container_of(w, struct binder_node, work);
4281 binder_uintptr_t node_ptr = node->ptr;
4282 binder_uintptr_t node_cookie = node->cookie;
4283 int node_debug_id = node->debug_id;
4286 void __user *orig_ptr = ptr;
4288 BUG_ON(proc != node->proc);
4289 strong = node->internal_strong_refs ||
4290 node->local_strong_refs;
4291 weak = !hlist_empty(&node->refs) ||
4292 node->local_weak_refs ||
4293 node->tmp_refs || strong;
4294 has_strong_ref = node->has_strong_ref;
4295 has_weak_ref = node->has_weak_ref;
4297 if (weak && !has_weak_ref) {
4298 node->has_weak_ref = 1;
4299 node->pending_weak_ref = 1;
4300 node->local_weak_refs++;
4302 if (strong && !has_strong_ref) {
4303 node->has_strong_ref = 1;
4304 node->pending_strong_ref = 1;
4305 node->local_strong_refs++;
4307 if (!strong && has_strong_ref)
4308 node->has_strong_ref = 0;
4309 if (!weak && has_weak_ref)
4310 node->has_weak_ref = 0;
4311 if (!weak && !strong) {
4312 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4313 "%d:%d node %d u%016llx c%016llx deleted\n",
4314 proc->pid, thread->pid,
4318 rb_erase(&node->rb_node, &proc->nodes);
4319 binder_inner_proc_unlock(proc);
4320 binder_node_lock(node);
4322 * Acquire the node lock before freeing the
4323 * node to serialize with other threads that
4324 * may have been holding the node lock while
4325 * decrementing this node (avoids race where
4326 * this thread frees while the other thread
4327 * is unlocking the node after the final
4330 binder_node_unlock(node);
4331 binder_free_node(node);
4333 binder_inner_proc_unlock(proc);
4335 if (weak && !has_weak_ref)
4336 ret = binder_put_node_cmd(
4337 proc, thread, &ptr, node_ptr,
4338 node_cookie, node_debug_id,
4339 BR_INCREFS, "BR_INCREFS");
4340 if (!ret && strong && !has_strong_ref)
4341 ret = binder_put_node_cmd(
4342 proc, thread, &ptr, node_ptr,
4343 node_cookie, node_debug_id,
4344 BR_ACQUIRE, "BR_ACQUIRE");
4345 if (!ret && !strong && has_strong_ref)
4346 ret = binder_put_node_cmd(
4347 proc, thread, &ptr, node_ptr,
4348 node_cookie, node_debug_id,
4349 BR_RELEASE, "BR_RELEASE");
4350 if (!ret && !weak && has_weak_ref)
4351 ret = binder_put_node_cmd(
4352 proc, thread, &ptr, node_ptr,
4353 node_cookie, node_debug_id,
4354 BR_DECREFS, "BR_DECREFS");
4355 if (orig_ptr == ptr)
4356 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4357 "%d:%d node %d u%016llx c%016llx state unchanged\n",
4358 proc->pid, thread->pid,
4365 case BINDER_WORK_DEAD_BINDER:
4366 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4367 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4368 struct binder_ref_death *death;
4370 binder_uintptr_t cookie;
4372 death = container_of(w, struct binder_ref_death, work);
4373 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
4374 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
4376 cmd = BR_DEAD_BINDER;
4377 cookie = death->cookie;
4379 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
4380 "%d:%d %s %016llx\n",
4381 proc->pid, thread->pid,
4382 cmd == BR_DEAD_BINDER ?
4384 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
4386 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
4387 binder_inner_proc_unlock(proc);
4389 binder_stats_deleted(BINDER_STAT_DEATH);
4391 binder_enqueue_work_ilocked(
4392 w, &proc->delivered_death);
4393 binder_inner_proc_unlock(proc);
4395 if (put_user(cmd, (uint32_t __user *)ptr))
4397 ptr += sizeof(uint32_t);
4398 if (put_user(cookie,
4399 (binder_uintptr_t __user *)ptr))
4401 ptr += sizeof(binder_uintptr_t);
4402 binder_stat_br(proc, thread, cmd);
4403 if (cmd == BR_DEAD_BINDER)
4404 goto done; /* DEAD_BINDER notifications can cause transactions */
4407 binder_inner_proc_unlock(proc);
4408 pr_err("%d:%d: bad work type %d\n",
4409 proc->pid, thread->pid, w->type);
4416 BUG_ON(t->buffer == NULL);
4417 if (t->buffer->target_node) {
4418 struct binder_node *target_node = t->buffer->target_node;
4420 trd->target.ptr = target_node->ptr;
4421 trd->cookie = target_node->cookie;
4422 t->saved_priority = task_nice(current);
4423 if (t->priority < target_node->min_priority &&
4424 !(t->flags & TF_ONE_WAY))
4425 binder_set_nice(t->priority);
4426 else if (!(t->flags & TF_ONE_WAY) ||
4427 t->saved_priority > target_node->min_priority)
4428 binder_set_nice(target_node->min_priority);
4429 cmd = BR_TRANSACTION;
4431 trd->target.ptr = 0;
4435 trd->code = t->code;
4436 trd->flags = t->flags;
4437 trd->sender_euid = from_kuid(current_user_ns(), t->sender_euid);
4439 t_from = binder_get_txn_from(t);
4441 struct task_struct *sender = t_from->proc->tsk;
4444 task_tgid_nr_ns(sender,
4445 task_active_pid_ns(current));
4447 trd->sender_pid = 0;
4450 ret = binder_apply_fd_fixups(proc, t);
4452 struct binder_buffer *buffer = t->buffer;
4453 bool oneway = !!(t->flags & TF_ONE_WAY);
4454 int tid = t->debug_id;
4457 binder_thread_dec_tmpref(t_from);
4458 buffer->transaction = NULL;
4459 binder_cleanup_transaction(t, "fd fixups failed",
4461 binder_free_buf(proc, buffer);
4462 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
4463 "%d:%d %stransaction %d fd fixups failed %d/%d, line %d\n",
4464 proc->pid, thread->pid,
4466 (cmd == BR_REPLY ? "reply " : ""),
4467 tid, BR_FAILED_REPLY, ret, __LINE__);
4468 if (cmd == BR_REPLY) {
4469 cmd = BR_FAILED_REPLY;
4470 if (put_user(cmd, (uint32_t __user *)ptr))
4472 ptr += sizeof(uint32_t);
4473 binder_stat_br(proc, thread, cmd);
4478 trd->data_size = t->buffer->data_size;
4479 trd->offsets_size = t->buffer->offsets_size;
4480 trd->data.ptr.buffer = (uintptr_t)t->buffer->user_data;
4481 trd->data.ptr.offsets = trd->data.ptr.buffer +
4482 ALIGN(t->buffer->data_size,
4485 tr.secctx = t->security_ctx;
4486 if (t->security_ctx) {
4487 cmd = BR_TRANSACTION_SEC_CTX;
4488 trsize = sizeof(tr);
4490 if (put_user(cmd, (uint32_t __user *)ptr)) {
4492 binder_thread_dec_tmpref(t_from);
4494 binder_cleanup_transaction(t, "put_user failed",
4499 ptr += sizeof(uint32_t);
4500 if (copy_to_user(ptr, &tr, trsize)) {
4502 binder_thread_dec_tmpref(t_from);
4504 binder_cleanup_transaction(t, "copy_to_user failed",
4511 trace_binder_transaction_received(t);
4512 binder_stat_br(proc, thread, cmd);
4513 binder_debug(BINDER_DEBUG_TRANSACTION,
4514 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
4515 proc->pid, thread->pid,
4516 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
4517 (cmd == BR_TRANSACTION_SEC_CTX) ?
4518 "BR_TRANSACTION_SEC_CTX" : "BR_REPLY",
4519 t->debug_id, t_from ? t_from->proc->pid : 0,
4520 t_from ? t_from->pid : 0, cmd,
4521 t->buffer->data_size, t->buffer->offsets_size,
4522 (u64)trd->data.ptr.buffer,
4523 (u64)trd->data.ptr.offsets);
4526 binder_thread_dec_tmpref(t_from);
4527 t->buffer->allow_user_free = 1;
4528 if (cmd != BR_REPLY && !(t->flags & TF_ONE_WAY)) {
4529 binder_inner_proc_lock(thread->proc);
4530 t->to_parent = thread->transaction_stack;
4531 t->to_thread = thread;
4532 thread->transaction_stack = t;
4533 binder_inner_proc_unlock(thread->proc);
4535 binder_free_transaction(t);
4542 *consumed = ptr - buffer;
4543 binder_inner_proc_lock(proc);
4544 if (proc->requested_threads == 0 &&
4545 list_empty(&thread->proc->waiting_threads) &&
4546 proc->requested_threads_started < proc->max_threads &&
4547 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4548 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
4549 /*spawn a new thread if we leave this out */) {
4550 proc->requested_threads++;
4551 binder_inner_proc_unlock(proc);
4552 binder_debug(BINDER_DEBUG_THREADS,
4553 "%d:%d BR_SPAWN_LOOPER\n",
4554 proc->pid, thread->pid);
4555 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
4557 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
4559 binder_inner_proc_unlock(proc);
4563 static void binder_release_work(struct binder_proc *proc,
4564 struct list_head *list)
4566 struct binder_work *w;
4567 enum binder_work_type wtype;
4570 binder_inner_proc_lock(proc);
4571 w = binder_dequeue_work_head_ilocked(list);
4572 wtype = w ? w->type : 0;
4573 binder_inner_proc_unlock(proc);
4578 case BINDER_WORK_TRANSACTION: {
4579 struct binder_transaction *t;
4581 t = container_of(w, struct binder_transaction, work);
4583 binder_cleanup_transaction(t, "process died.",
4586 case BINDER_WORK_RETURN_ERROR: {
4587 struct binder_error *e = container_of(
4588 w, struct binder_error, work);
4590 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4591 "undelivered TRANSACTION_ERROR: %u\n",
4594 case BINDER_WORK_TRANSACTION_COMPLETE: {
4595 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4596 "undelivered TRANSACTION_COMPLETE\n");
4598 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4600 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4601 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4602 struct binder_ref_death *death;
4604 death = container_of(w, struct binder_ref_death, work);
4605 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4606 "undelivered death notification, %016llx\n",
4607 (u64)death->cookie);
4609 binder_stats_deleted(BINDER_STAT_DEATH);
4611 case BINDER_WORK_NODE:
4614 pr_err("unexpected work type, %d, not freed\n",
4622 static struct binder_thread *binder_get_thread_ilocked(
4623 struct binder_proc *proc, struct binder_thread *new_thread)
4625 struct binder_thread *thread = NULL;
4626 struct rb_node *parent = NULL;
4627 struct rb_node **p = &proc->threads.rb_node;
4631 thread = rb_entry(parent, struct binder_thread, rb_node);
4633 if (current->pid < thread->pid)
4635 else if (current->pid > thread->pid)
4636 p = &(*p)->rb_right;
4642 thread = new_thread;
4643 binder_stats_created(BINDER_STAT_THREAD);
4644 thread->proc = proc;
4645 thread->pid = current->pid;
4646 atomic_set(&thread->tmp_ref, 0);
4647 init_waitqueue_head(&thread->wait);
4648 INIT_LIST_HEAD(&thread->todo);
4649 rb_link_node(&thread->rb_node, parent, p);
4650 rb_insert_color(&thread->rb_node, &proc->threads);
4651 thread->looper_need_return = true;
4652 thread->return_error.work.type = BINDER_WORK_RETURN_ERROR;
4653 thread->return_error.cmd = BR_OK;
4654 thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR;
4655 thread->reply_error.cmd = BR_OK;
4656 INIT_LIST_HEAD(&new_thread->waiting_thread_node);
4660 static struct binder_thread *binder_get_thread(struct binder_proc *proc)
4662 struct binder_thread *thread;
4663 struct binder_thread *new_thread;
4665 binder_inner_proc_lock(proc);
4666 thread = binder_get_thread_ilocked(proc, NULL);
4667 binder_inner_proc_unlock(proc);
4669 new_thread = kzalloc(sizeof(*thread), GFP_KERNEL);
4670 if (new_thread == NULL)
4672 binder_inner_proc_lock(proc);
4673 thread = binder_get_thread_ilocked(proc, new_thread);
4674 binder_inner_proc_unlock(proc);
4675 if (thread != new_thread)
4681 static void binder_free_proc(struct binder_proc *proc)
4683 struct binder_device *device;
4685 BUG_ON(!list_empty(&proc->todo));
4686 BUG_ON(!list_empty(&proc->delivered_death));
4687 device = container_of(proc->context, struct binder_device, context);
4688 if (refcount_dec_and_test(&device->ref)) {
4689 kfree(proc->context->name);
4692 binder_alloc_deferred_release(&proc->alloc);
4693 put_task_struct(proc->tsk);
4694 binder_stats_deleted(BINDER_STAT_PROC);
4698 static void binder_free_thread(struct binder_thread *thread)
4700 BUG_ON(!list_empty(&thread->todo));
4701 binder_stats_deleted(BINDER_STAT_THREAD);
4702 binder_proc_dec_tmpref(thread->proc);
4706 static int binder_thread_release(struct binder_proc *proc,
4707 struct binder_thread *thread)
4709 struct binder_transaction *t;
4710 struct binder_transaction *send_reply = NULL;
4711 int active_transactions = 0;
4712 struct binder_transaction *last_t = NULL;
4714 binder_inner_proc_lock(thread->proc);
4716 * take a ref on the proc so it survives
4717 * after we remove this thread from proc->threads.
4718 * The corresponding dec is when we actually
4719 * free the thread in binder_free_thread()
4723 * take a ref on this thread to ensure it
4724 * survives while we are releasing it
4726 atomic_inc(&thread->tmp_ref);
4727 rb_erase(&thread->rb_node, &proc->threads);
4728 t = thread->transaction_stack;
4730 spin_lock(&t->lock);
4731 if (t->to_thread == thread)
4734 __acquire(&t->lock);
4736 thread->is_dead = true;
4740 active_transactions++;
4741 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4742 "release %d:%d transaction %d %s, still active\n",
4743 proc->pid, thread->pid,
4745 (t->to_thread == thread) ? "in" : "out");
4747 if (t->to_thread == thread) {
4749 t->to_thread = NULL;
4751 t->buffer->transaction = NULL;
4755 } else if (t->from == thread) {
4760 spin_unlock(&last_t->lock);
4762 spin_lock(&t->lock);
4764 __acquire(&t->lock);
4766 /* annotation for sparse, lock not acquired in last iteration above */
4767 __release(&t->lock);
4770 * If this thread used poll, make sure we remove the waitqueue
4771 * from any epoll data structures holding it with POLLFREE.
4772 * waitqueue_active() is safe to use here because we're holding
4775 if ((thread->looper & BINDER_LOOPER_STATE_POLL) &&
4776 waitqueue_active(&thread->wait)) {
4777 wake_up_poll(&thread->wait, EPOLLHUP | POLLFREE);
4780 binder_inner_proc_unlock(thread->proc);
4783 * This is needed to avoid races between wake_up_poll() above and
4784 * and ep_remove_waitqueue() called for other reasons (eg the epoll file
4785 * descriptor being closed); ep_remove_waitqueue() holds an RCU read
4786 * lock, so we can be sure it's done after calling synchronize_rcu().
4788 if (thread->looper & BINDER_LOOPER_STATE_POLL)
4792 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
4793 binder_release_work(proc, &thread->todo);
4794 binder_thread_dec_tmpref(thread);
4795 return active_transactions;
4798 static __poll_t binder_poll(struct file *filp,
4799 struct poll_table_struct *wait)
4801 struct binder_proc *proc = filp->private_data;
4802 struct binder_thread *thread = NULL;
4803 bool wait_for_proc_work;
4805 thread = binder_get_thread(proc);
4809 binder_inner_proc_lock(thread->proc);
4810 thread->looper |= BINDER_LOOPER_STATE_POLL;
4811 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
4813 binder_inner_proc_unlock(thread->proc);
4815 poll_wait(filp, &thread->wait, wait);
4817 if (binder_has_work(thread, wait_for_proc_work))
4823 static int binder_ioctl_write_read(struct file *filp,
4824 unsigned int cmd, unsigned long arg,
4825 struct binder_thread *thread)
4828 struct binder_proc *proc = filp->private_data;
4829 unsigned int size = _IOC_SIZE(cmd);
4830 void __user *ubuf = (void __user *)arg;
4831 struct binder_write_read bwr;
4833 if (size != sizeof(struct binder_write_read)) {
4837 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
4841 binder_debug(BINDER_DEBUG_READ_WRITE,
4842 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
4843 proc->pid, thread->pid,
4844 (u64)bwr.write_size, (u64)bwr.write_buffer,
4845 (u64)bwr.read_size, (u64)bwr.read_buffer);
4847 if (bwr.write_size > 0) {
4848 ret = binder_thread_write(proc, thread,
4851 &bwr.write_consumed);
4852 trace_binder_write_done(ret);
4854 bwr.read_consumed = 0;
4855 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4860 if (bwr.read_size > 0) {
4861 ret = binder_thread_read(proc, thread, bwr.read_buffer,
4864 filp->f_flags & O_NONBLOCK);
4865 trace_binder_read_done(ret);
4866 binder_inner_proc_lock(proc);
4867 if (!binder_worklist_empty_ilocked(&proc->todo))
4868 binder_wakeup_proc_ilocked(proc);
4869 binder_inner_proc_unlock(proc);
4871 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4876 binder_debug(BINDER_DEBUG_READ_WRITE,
4877 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
4878 proc->pid, thread->pid,
4879 (u64)bwr.write_consumed, (u64)bwr.write_size,
4880 (u64)bwr.read_consumed, (u64)bwr.read_size);
4881 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
4889 static int binder_ioctl_set_ctx_mgr(struct file *filp,
4890 struct flat_binder_object *fbo)
4893 struct binder_proc *proc = filp->private_data;
4894 struct binder_context *context = proc->context;
4895 struct binder_node *new_node;
4896 kuid_t curr_euid = current_euid();
4898 mutex_lock(&context->context_mgr_node_lock);
4899 if (context->binder_context_mgr_node) {
4900 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
4904 ret = security_binder_set_context_mgr(proc->tsk);
4907 if (uid_valid(context->binder_context_mgr_uid)) {
4908 if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
4909 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
4910 from_kuid(&init_user_ns, curr_euid),
4911 from_kuid(&init_user_ns,
4912 context->binder_context_mgr_uid));
4917 context->binder_context_mgr_uid = curr_euid;
4919 new_node = binder_new_node(proc, fbo);
4924 binder_node_lock(new_node);
4925 new_node->local_weak_refs++;
4926 new_node->local_strong_refs++;
4927 new_node->has_strong_ref = 1;
4928 new_node->has_weak_ref = 1;
4929 context->binder_context_mgr_node = new_node;
4930 binder_node_unlock(new_node);
4931 binder_put_node(new_node);
4933 mutex_unlock(&context->context_mgr_node_lock);
4937 static int binder_ioctl_get_node_info_for_ref(struct binder_proc *proc,
4938 struct binder_node_info_for_ref *info)
4940 struct binder_node *node;
4941 struct binder_context *context = proc->context;
4942 __u32 handle = info->handle;
4944 if (info->strong_count || info->weak_count || info->reserved1 ||
4945 info->reserved2 || info->reserved3) {
4946 binder_user_error("%d BINDER_GET_NODE_INFO_FOR_REF: only handle may be non-zero.",
4951 /* This ioctl may only be used by the context manager */
4952 mutex_lock(&context->context_mgr_node_lock);
4953 if (!context->binder_context_mgr_node ||
4954 context->binder_context_mgr_node->proc != proc) {
4955 mutex_unlock(&context->context_mgr_node_lock);
4958 mutex_unlock(&context->context_mgr_node_lock);
4960 node = binder_get_node_from_ref(proc, handle, true, NULL);
4964 info->strong_count = node->local_strong_refs +
4965 node->internal_strong_refs;
4966 info->weak_count = node->local_weak_refs;
4968 binder_put_node(node);
4973 static int binder_ioctl_get_node_debug_info(struct binder_proc *proc,
4974 struct binder_node_debug_info *info)
4977 binder_uintptr_t ptr = info->ptr;
4979 memset(info, 0, sizeof(*info));
4981 binder_inner_proc_lock(proc);
4982 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
4983 struct binder_node *node = rb_entry(n, struct binder_node,
4985 if (node->ptr > ptr) {
4986 info->ptr = node->ptr;
4987 info->cookie = node->cookie;
4988 info->has_strong_ref = node->has_strong_ref;
4989 info->has_weak_ref = node->has_weak_ref;
4993 binder_inner_proc_unlock(proc);
4998 static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
5001 struct binder_proc *proc = filp->private_data;
5002 struct binder_thread *thread;
5003 unsigned int size = _IOC_SIZE(cmd);
5004 void __user *ubuf = (void __user *)arg;
5006 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
5007 proc->pid, current->pid, cmd, arg);*/
5009 binder_selftest_alloc(&proc->alloc);
5011 trace_binder_ioctl(cmd, arg);
5013 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
5017 thread = binder_get_thread(proc);
5018 if (thread == NULL) {
5024 case BINDER_WRITE_READ:
5025 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
5029 case BINDER_SET_MAX_THREADS: {
5032 if (copy_from_user(&max_threads, ubuf,
5033 sizeof(max_threads))) {
5037 binder_inner_proc_lock(proc);
5038 proc->max_threads = max_threads;
5039 binder_inner_proc_unlock(proc);
5042 case BINDER_SET_CONTEXT_MGR_EXT: {
5043 struct flat_binder_object fbo;
5045 if (copy_from_user(&fbo, ubuf, sizeof(fbo))) {
5049 ret = binder_ioctl_set_ctx_mgr(filp, &fbo);
5054 case BINDER_SET_CONTEXT_MGR:
5055 ret = binder_ioctl_set_ctx_mgr(filp, NULL);
5059 case BINDER_THREAD_EXIT:
5060 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
5061 proc->pid, thread->pid);
5062 binder_thread_release(proc, thread);
5065 case BINDER_VERSION: {
5066 struct binder_version __user *ver = ubuf;
5068 if (size != sizeof(struct binder_version)) {
5072 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
5073 &ver->protocol_version)) {
5079 case BINDER_GET_NODE_INFO_FOR_REF: {
5080 struct binder_node_info_for_ref info;
5082 if (copy_from_user(&info, ubuf, sizeof(info))) {
5087 ret = binder_ioctl_get_node_info_for_ref(proc, &info);
5091 if (copy_to_user(ubuf, &info, sizeof(info))) {
5098 case BINDER_GET_NODE_DEBUG_INFO: {
5099 struct binder_node_debug_info info;
5101 if (copy_from_user(&info, ubuf, sizeof(info))) {
5106 ret = binder_ioctl_get_node_debug_info(proc, &info);
5110 if (copy_to_user(ubuf, &info, sizeof(info))) {
5123 thread->looper_need_return = false;
5124 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
5125 if (ret && ret != -ERESTARTSYS)
5126 pr_info("%d:%d ioctl %x %lx returned %d\n", proc->pid, current->pid, cmd, arg, ret);
5128 trace_binder_ioctl_done(ret);
5132 static void binder_vma_open(struct vm_area_struct *vma)
5134 struct binder_proc *proc = vma->vm_private_data;
5136 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5137 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
5138 proc->pid, vma->vm_start, vma->vm_end,
5139 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
5140 (unsigned long)pgprot_val(vma->vm_page_prot));
5143 static void binder_vma_close(struct vm_area_struct *vma)
5145 struct binder_proc *proc = vma->vm_private_data;
5147 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5148 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
5149 proc->pid, vma->vm_start, vma->vm_end,
5150 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
5151 (unsigned long)pgprot_val(vma->vm_page_prot));
5152 binder_alloc_vma_close(&proc->alloc);
5155 static vm_fault_t binder_vm_fault(struct vm_fault *vmf)
5157 return VM_FAULT_SIGBUS;
5160 static const struct vm_operations_struct binder_vm_ops = {
5161 .open = binder_vma_open,
5162 .close = binder_vma_close,
5163 .fault = binder_vm_fault,
5166 static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
5168 struct binder_proc *proc = filp->private_data;
5170 if (proc->tsk != current->group_leader)
5173 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5174 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
5175 __func__, proc->pid, vma->vm_start, vma->vm_end,
5176 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
5177 (unsigned long)pgprot_val(vma->vm_page_prot));
5179 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
5180 pr_err("%s: %d %lx-%lx %s failed %d\n", __func__,
5181 proc->pid, vma->vm_start, vma->vm_end, "bad vm_flags", -EPERM);
5184 vma->vm_flags |= VM_DONTCOPY | VM_MIXEDMAP;
5185 vma->vm_flags &= ~VM_MAYWRITE;
5187 vma->vm_ops = &binder_vm_ops;
5188 vma->vm_private_data = proc;
5190 return binder_alloc_mmap_handler(&proc->alloc, vma);
5193 static int binder_open(struct inode *nodp, struct file *filp)
5195 struct binder_proc *proc, *itr;
5196 struct binder_device *binder_dev;
5197 struct binderfs_info *info;
5198 struct dentry *binder_binderfs_dir_entry_proc = NULL;
5199 bool existing_pid = false;
5201 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "%s: %d:%d\n", __func__,
5202 current->group_leader->pid, current->pid);
5204 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
5207 spin_lock_init(&proc->inner_lock);
5208 spin_lock_init(&proc->outer_lock);
5209 get_task_struct(current->group_leader);
5210 proc->tsk = current->group_leader;
5211 INIT_LIST_HEAD(&proc->todo);
5212 proc->default_priority = task_nice(current);
5213 /* binderfs stashes devices in i_private */
5214 if (is_binderfs_device(nodp)) {
5215 binder_dev = nodp->i_private;
5216 info = nodp->i_sb->s_fs_info;
5217 binder_binderfs_dir_entry_proc = info->proc_log_dir;
5219 binder_dev = container_of(filp->private_data,
5220 struct binder_device, miscdev);
5222 refcount_inc(&binder_dev->ref);
5223 proc->context = &binder_dev->context;
5224 binder_alloc_init(&proc->alloc);
5226 binder_stats_created(BINDER_STAT_PROC);
5227 proc->pid = current->group_leader->pid;
5228 INIT_LIST_HEAD(&proc->delivered_death);
5229 INIT_LIST_HEAD(&proc->waiting_threads);
5230 filp->private_data = proc;
5232 mutex_lock(&binder_procs_lock);
5233 hlist_for_each_entry(itr, &binder_procs, proc_node) {
5234 if (itr->pid == proc->pid) {
5235 existing_pid = true;
5239 hlist_add_head(&proc->proc_node, &binder_procs);
5240 mutex_unlock(&binder_procs_lock);
5242 if (binder_debugfs_dir_entry_proc && !existing_pid) {
5245 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
5247 * proc debug entries are shared between contexts.
5248 * Only create for the first PID to avoid debugfs log spamming
5249 * The printing code will anyway print all contexts for a given
5250 * PID so this is not a problem.
5252 proc->debugfs_entry = debugfs_create_file(strbuf, 0444,
5253 binder_debugfs_dir_entry_proc,
5254 (void *)(unsigned long)proc->pid,
5258 if (binder_binderfs_dir_entry_proc && !existing_pid) {
5260 struct dentry *binderfs_entry;
5262 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
5264 * Similar to debugfs, the process specific log file is shared
5265 * between contexts. Only create for the first PID.
5266 * This is ok since same as debugfs, the log file will contain
5267 * information on all contexts of a given PID.
5269 binderfs_entry = binderfs_create_file(binder_binderfs_dir_entry_proc,
5270 strbuf, &proc_fops, (void *)(unsigned long)proc->pid);
5271 if (!IS_ERR(binderfs_entry)) {
5272 proc->binderfs_entry = binderfs_entry;
5276 error = PTR_ERR(binderfs_entry);
5277 pr_warn("Unable to create file %s in binderfs (error %d)\n",
5285 static int binder_flush(struct file *filp, fl_owner_t id)
5287 struct binder_proc *proc = filp->private_data;
5289 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
5294 static void binder_deferred_flush(struct binder_proc *proc)
5299 binder_inner_proc_lock(proc);
5300 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
5301 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
5303 thread->looper_need_return = true;
5304 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
5305 wake_up_interruptible(&thread->wait);
5309 binder_inner_proc_unlock(proc);
5311 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5312 "binder_flush: %d woke %d threads\n", proc->pid,
5316 static int binder_release(struct inode *nodp, struct file *filp)
5318 struct binder_proc *proc = filp->private_data;
5320 debugfs_remove(proc->debugfs_entry);
5322 if (proc->binderfs_entry) {
5323 binderfs_remove_file(proc->binderfs_entry);
5324 proc->binderfs_entry = NULL;
5327 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
5332 static int binder_node_release(struct binder_node *node, int refs)
5334 struct binder_ref *ref;
5336 struct binder_proc *proc = node->proc;
5338 binder_release_work(proc, &node->async_todo);
5340 binder_node_lock(node);
5341 binder_inner_proc_lock(proc);
5342 binder_dequeue_work_ilocked(&node->work);
5344 * The caller must have taken a temporary ref on the node,
5346 BUG_ON(!node->tmp_refs);
5347 if (hlist_empty(&node->refs) && node->tmp_refs == 1) {
5348 binder_inner_proc_unlock(proc);
5349 binder_node_unlock(node);
5350 binder_free_node(node);
5356 node->local_strong_refs = 0;
5357 node->local_weak_refs = 0;
5358 binder_inner_proc_unlock(proc);
5360 spin_lock(&binder_dead_nodes_lock);
5361 hlist_add_head(&node->dead_node, &binder_dead_nodes);
5362 spin_unlock(&binder_dead_nodes_lock);
5364 hlist_for_each_entry(ref, &node->refs, node_entry) {
5367 * Need the node lock to synchronize
5368 * with new notification requests and the
5369 * inner lock to synchronize with queued
5370 * death notifications.
5372 binder_inner_proc_lock(ref->proc);
5374 binder_inner_proc_unlock(ref->proc);
5380 BUG_ON(!list_empty(&ref->death->work.entry));
5381 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
5382 binder_enqueue_work_ilocked(&ref->death->work,
5384 binder_wakeup_proc_ilocked(ref->proc);
5385 binder_inner_proc_unlock(ref->proc);
5388 binder_debug(BINDER_DEBUG_DEAD_BINDER,
5389 "node %d now dead, refs %d, death %d\n",
5390 node->debug_id, refs, death);
5391 binder_node_unlock(node);
5392 binder_put_node(node);
5397 static void binder_deferred_release(struct binder_proc *proc)
5399 struct binder_context *context = proc->context;
5401 int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
5403 mutex_lock(&binder_procs_lock);
5404 hlist_del(&proc->proc_node);
5405 mutex_unlock(&binder_procs_lock);
5407 mutex_lock(&context->context_mgr_node_lock);
5408 if (context->binder_context_mgr_node &&
5409 context->binder_context_mgr_node->proc == proc) {
5410 binder_debug(BINDER_DEBUG_DEAD_BINDER,
5411 "%s: %d context_mgr_node gone\n",
5412 __func__, proc->pid);
5413 context->binder_context_mgr_node = NULL;
5415 mutex_unlock(&context->context_mgr_node_lock);
5416 binder_inner_proc_lock(proc);
5418 * Make sure proc stays alive after we
5419 * remove all the threads
5423 proc->is_dead = true;
5425 active_transactions = 0;
5426 while ((n = rb_first(&proc->threads))) {
5427 struct binder_thread *thread;
5429 thread = rb_entry(n, struct binder_thread, rb_node);
5430 binder_inner_proc_unlock(proc);
5432 active_transactions += binder_thread_release(proc, thread);
5433 binder_inner_proc_lock(proc);
5438 while ((n = rb_first(&proc->nodes))) {
5439 struct binder_node *node;
5441 node = rb_entry(n, struct binder_node, rb_node);
5444 * take a temporary ref on the node before
5445 * calling binder_node_release() which will either
5446 * kfree() the node or call binder_put_node()
5448 binder_inc_node_tmpref_ilocked(node);
5449 rb_erase(&node->rb_node, &proc->nodes);
5450 binder_inner_proc_unlock(proc);
5451 incoming_refs = binder_node_release(node, incoming_refs);
5452 binder_inner_proc_lock(proc);
5454 binder_inner_proc_unlock(proc);
5457 binder_proc_lock(proc);
5458 while ((n = rb_first(&proc->refs_by_desc))) {
5459 struct binder_ref *ref;
5461 ref = rb_entry(n, struct binder_ref, rb_node_desc);
5463 binder_cleanup_ref_olocked(ref);
5464 binder_proc_unlock(proc);
5465 binder_free_ref(ref);
5466 binder_proc_lock(proc);
5468 binder_proc_unlock(proc);
5470 binder_release_work(proc, &proc->todo);
5471 binder_release_work(proc, &proc->delivered_death);
5473 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5474 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
5475 __func__, proc->pid, threads, nodes, incoming_refs,
5476 outgoing_refs, active_transactions);
5478 binder_proc_dec_tmpref(proc);
5481 static void binder_deferred_func(struct work_struct *work)
5483 struct binder_proc *proc;
5488 mutex_lock(&binder_deferred_lock);
5489 if (!hlist_empty(&binder_deferred_list)) {
5490 proc = hlist_entry(binder_deferred_list.first,
5491 struct binder_proc, deferred_work_node);
5492 hlist_del_init(&proc->deferred_work_node);
5493 defer = proc->deferred_work;
5494 proc->deferred_work = 0;
5499 mutex_unlock(&binder_deferred_lock);
5501 if (defer & BINDER_DEFERRED_FLUSH)
5502 binder_deferred_flush(proc);
5504 if (defer & BINDER_DEFERRED_RELEASE)
5505 binder_deferred_release(proc); /* frees proc */
5508 static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
5511 binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
5513 mutex_lock(&binder_deferred_lock);
5514 proc->deferred_work |= defer;
5515 if (hlist_unhashed(&proc->deferred_work_node)) {
5516 hlist_add_head(&proc->deferred_work_node,
5517 &binder_deferred_list);
5518 schedule_work(&binder_deferred_work);
5520 mutex_unlock(&binder_deferred_lock);
5523 static void print_binder_transaction_ilocked(struct seq_file *m,
5524 struct binder_proc *proc,
5526 struct binder_transaction *t)
5528 struct binder_proc *to_proc;
5529 struct binder_buffer *buffer = t->buffer;
5531 spin_lock(&t->lock);
5532 to_proc = t->to_proc;
5534 "%s %d: %pK from %d:%d to %d:%d code %x flags %x pri %ld r%d",
5535 prefix, t->debug_id, t,
5536 t->from ? t->from->proc->pid : 0,
5537 t->from ? t->from->pid : 0,
5538 to_proc ? to_proc->pid : 0,
5539 t->to_thread ? t->to_thread->pid : 0,
5540 t->code, t->flags, t->priority, t->need_reply);
5541 spin_unlock(&t->lock);
5543 if (proc != to_proc) {
5545 * Can only safely deref buffer if we are holding the
5546 * correct proc inner lock for this node
5552 if (buffer == NULL) {
5553 seq_puts(m, " buffer free\n");
5556 if (buffer->target_node)
5557 seq_printf(m, " node %d", buffer->target_node->debug_id);
5558 seq_printf(m, " size %zd:%zd data %pK\n",
5559 buffer->data_size, buffer->offsets_size,
5563 static void print_binder_work_ilocked(struct seq_file *m,
5564 struct binder_proc *proc,
5566 const char *transaction_prefix,
5567 struct binder_work *w)
5569 struct binder_node *node;
5570 struct binder_transaction *t;
5573 case BINDER_WORK_TRANSACTION:
5574 t = container_of(w, struct binder_transaction, work);
5575 print_binder_transaction_ilocked(
5576 m, proc, transaction_prefix, t);
5578 case BINDER_WORK_RETURN_ERROR: {
5579 struct binder_error *e = container_of(
5580 w, struct binder_error, work);
5582 seq_printf(m, "%stransaction error: %u\n",
5585 case BINDER_WORK_TRANSACTION_COMPLETE:
5586 seq_printf(m, "%stransaction complete\n", prefix);
5588 case BINDER_WORK_NODE:
5589 node = container_of(w, struct binder_node, work);
5590 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
5591 prefix, node->debug_id,
5592 (u64)node->ptr, (u64)node->cookie);
5594 case BINDER_WORK_DEAD_BINDER:
5595 seq_printf(m, "%shas dead binder\n", prefix);
5597 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
5598 seq_printf(m, "%shas cleared dead binder\n", prefix);
5600 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
5601 seq_printf(m, "%shas cleared death notification\n", prefix);
5604 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
5609 static void print_binder_thread_ilocked(struct seq_file *m,
5610 struct binder_thread *thread,
5613 struct binder_transaction *t;
5614 struct binder_work *w;
5615 size_t start_pos = m->count;
5618 seq_printf(m, " thread %d: l %02x need_return %d tr %d\n",
5619 thread->pid, thread->looper,
5620 thread->looper_need_return,
5621 atomic_read(&thread->tmp_ref));
5622 header_pos = m->count;
5623 t = thread->transaction_stack;
5625 if (t->from == thread) {
5626 print_binder_transaction_ilocked(m, thread->proc,
5627 " outgoing transaction", t);
5629 } else if (t->to_thread == thread) {
5630 print_binder_transaction_ilocked(m, thread->proc,
5631 " incoming transaction", t);
5634 print_binder_transaction_ilocked(m, thread->proc,
5635 " bad transaction", t);
5639 list_for_each_entry(w, &thread->todo, entry) {
5640 print_binder_work_ilocked(m, thread->proc, " ",
5641 " pending transaction", w);
5643 if (!print_always && m->count == header_pos)
5644 m->count = start_pos;
5647 static void print_binder_node_nilocked(struct seq_file *m,
5648 struct binder_node *node)
5650 struct binder_ref *ref;
5651 struct binder_work *w;
5655 hlist_for_each_entry(ref, &node->refs, node_entry)
5658 seq_printf(m, " node %d: u%016llx c%016llx hs %d hw %d ls %d lw %d is %d iw %d tr %d",
5659 node->debug_id, (u64)node->ptr, (u64)node->cookie,
5660 node->has_strong_ref, node->has_weak_ref,
5661 node->local_strong_refs, node->local_weak_refs,
5662 node->internal_strong_refs, count, node->tmp_refs);
5664 seq_puts(m, " proc");
5665 hlist_for_each_entry(ref, &node->refs, node_entry)
5666 seq_printf(m, " %d", ref->proc->pid);
5670 list_for_each_entry(w, &node->async_todo, entry)
5671 print_binder_work_ilocked(m, node->proc, " ",
5672 " pending async transaction", w);
5676 static void print_binder_ref_olocked(struct seq_file *m,
5677 struct binder_ref *ref)
5679 binder_node_lock(ref->node);
5680 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
5681 ref->data.debug_id, ref->data.desc,
5682 ref->node->proc ? "" : "dead ",
5683 ref->node->debug_id, ref->data.strong,
5684 ref->data.weak, ref->death);
5685 binder_node_unlock(ref->node);
5688 static void print_binder_proc(struct seq_file *m,
5689 struct binder_proc *proc, int print_all)
5691 struct binder_work *w;
5693 size_t start_pos = m->count;
5695 struct binder_node *last_node = NULL;
5697 seq_printf(m, "proc %d\n", proc->pid);
5698 seq_printf(m, "context %s\n", proc->context->name);
5699 header_pos = m->count;
5701 binder_inner_proc_lock(proc);
5702 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
5703 print_binder_thread_ilocked(m, rb_entry(n, struct binder_thread,
5704 rb_node), print_all);
5706 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
5707 struct binder_node *node = rb_entry(n, struct binder_node,
5709 if (!print_all && !node->has_async_transaction)
5713 * take a temporary reference on the node so it
5714 * survives and isn't removed from the tree
5715 * while we print it.
5717 binder_inc_node_tmpref_ilocked(node);
5718 /* Need to drop inner lock to take node lock */
5719 binder_inner_proc_unlock(proc);
5721 binder_put_node(last_node);
5722 binder_node_inner_lock(node);
5723 print_binder_node_nilocked(m, node);
5724 binder_node_inner_unlock(node);
5726 binder_inner_proc_lock(proc);
5728 binder_inner_proc_unlock(proc);
5730 binder_put_node(last_node);
5733 binder_proc_lock(proc);
5734 for (n = rb_first(&proc->refs_by_desc);
5737 print_binder_ref_olocked(m, rb_entry(n,
5740 binder_proc_unlock(proc);
5742 binder_alloc_print_allocated(m, &proc->alloc);
5743 binder_inner_proc_lock(proc);
5744 list_for_each_entry(w, &proc->todo, entry)
5745 print_binder_work_ilocked(m, proc, " ",
5746 " pending transaction", w);
5747 list_for_each_entry(w, &proc->delivered_death, entry) {
5748 seq_puts(m, " has delivered dead binder\n");
5751 binder_inner_proc_unlock(proc);
5752 if (!print_all && m->count == header_pos)
5753 m->count = start_pos;
5756 static const char * const binder_return_strings[] = {
5761 "BR_ACQUIRE_RESULT",
5763 "BR_TRANSACTION_COMPLETE",
5768 "BR_ATTEMPT_ACQUIRE",
5773 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
5777 static const char * const binder_command_strings[] = {
5780 "BC_ACQUIRE_RESULT",
5788 "BC_ATTEMPT_ACQUIRE",
5789 "BC_REGISTER_LOOPER",
5792 "BC_REQUEST_DEATH_NOTIFICATION",
5793 "BC_CLEAR_DEATH_NOTIFICATION",
5794 "BC_DEAD_BINDER_DONE",
5795 "BC_TRANSACTION_SG",
5799 static const char * const binder_objstat_strings[] = {
5806 "transaction_complete"
5809 static void print_binder_stats(struct seq_file *m, const char *prefix,
5810 struct binder_stats *stats)
5814 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
5815 ARRAY_SIZE(binder_command_strings));
5816 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
5817 int temp = atomic_read(&stats->bc[i]);
5820 seq_printf(m, "%s%s: %d\n", prefix,
5821 binder_command_strings[i], temp);
5824 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
5825 ARRAY_SIZE(binder_return_strings));
5826 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
5827 int temp = atomic_read(&stats->br[i]);
5830 seq_printf(m, "%s%s: %d\n", prefix,
5831 binder_return_strings[i], temp);
5834 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
5835 ARRAY_SIZE(binder_objstat_strings));
5836 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
5837 ARRAY_SIZE(stats->obj_deleted));
5838 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
5839 int created = atomic_read(&stats->obj_created[i]);
5840 int deleted = atomic_read(&stats->obj_deleted[i]);
5842 if (created || deleted)
5843 seq_printf(m, "%s%s: active %d total %d\n",
5845 binder_objstat_strings[i],
5851 static void print_binder_proc_stats(struct seq_file *m,
5852 struct binder_proc *proc)
5854 struct binder_work *w;
5855 struct binder_thread *thread;
5857 int count, strong, weak, ready_threads;
5858 size_t free_async_space =
5859 binder_alloc_get_free_async_space(&proc->alloc);
5861 seq_printf(m, "proc %d\n", proc->pid);
5862 seq_printf(m, "context %s\n", proc->context->name);
5865 binder_inner_proc_lock(proc);
5866 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
5869 list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node)
5872 seq_printf(m, " threads: %d\n", count);
5873 seq_printf(m, " requested threads: %d+%d/%d\n"
5874 " ready threads %d\n"
5875 " free async space %zd\n", proc->requested_threads,
5876 proc->requested_threads_started, proc->max_threads,
5880 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
5882 binder_inner_proc_unlock(proc);
5883 seq_printf(m, " nodes: %d\n", count);
5887 binder_proc_lock(proc);
5888 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
5889 struct binder_ref *ref = rb_entry(n, struct binder_ref,
5892 strong += ref->data.strong;
5893 weak += ref->data.weak;
5895 binder_proc_unlock(proc);
5896 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
5898 count = binder_alloc_get_allocated_count(&proc->alloc);
5899 seq_printf(m, " buffers: %d\n", count);
5901 binder_alloc_print_pages(m, &proc->alloc);
5904 binder_inner_proc_lock(proc);
5905 list_for_each_entry(w, &proc->todo, entry) {
5906 if (w->type == BINDER_WORK_TRANSACTION)
5909 binder_inner_proc_unlock(proc);
5910 seq_printf(m, " pending transactions: %d\n", count);
5912 print_binder_stats(m, " ", &proc->stats);
5916 int binder_state_show(struct seq_file *m, void *unused)
5918 struct binder_proc *proc;
5919 struct binder_node *node;
5920 struct binder_node *last_node = NULL;
5922 seq_puts(m, "binder state:\n");
5924 spin_lock(&binder_dead_nodes_lock);
5925 if (!hlist_empty(&binder_dead_nodes))
5926 seq_puts(m, "dead nodes:\n");
5927 hlist_for_each_entry(node, &binder_dead_nodes, dead_node) {
5929 * take a temporary reference on the node so it
5930 * survives and isn't removed from the list
5931 * while we print it.
5934 spin_unlock(&binder_dead_nodes_lock);
5936 binder_put_node(last_node);
5937 binder_node_lock(node);
5938 print_binder_node_nilocked(m, node);
5939 binder_node_unlock(node);
5941 spin_lock(&binder_dead_nodes_lock);
5943 spin_unlock(&binder_dead_nodes_lock);
5945 binder_put_node(last_node);
5947 mutex_lock(&binder_procs_lock);
5948 hlist_for_each_entry(proc, &binder_procs, proc_node)
5949 print_binder_proc(m, proc, 1);
5950 mutex_unlock(&binder_procs_lock);
5955 int binder_stats_show(struct seq_file *m, void *unused)
5957 struct binder_proc *proc;
5959 seq_puts(m, "binder stats:\n");
5961 print_binder_stats(m, "", &binder_stats);
5963 mutex_lock(&binder_procs_lock);
5964 hlist_for_each_entry(proc, &binder_procs, proc_node)
5965 print_binder_proc_stats(m, proc);
5966 mutex_unlock(&binder_procs_lock);
5971 int binder_transactions_show(struct seq_file *m, void *unused)
5973 struct binder_proc *proc;
5975 seq_puts(m, "binder transactions:\n");
5976 mutex_lock(&binder_procs_lock);
5977 hlist_for_each_entry(proc, &binder_procs, proc_node)
5978 print_binder_proc(m, proc, 0);
5979 mutex_unlock(&binder_procs_lock);
5984 static int proc_show(struct seq_file *m, void *unused)
5986 struct binder_proc *itr;
5987 int pid = (unsigned long)m->private;
5989 mutex_lock(&binder_procs_lock);
5990 hlist_for_each_entry(itr, &binder_procs, proc_node) {
5991 if (itr->pid == pid) {
5992 seq_puts(m, "binder proc state:\n");
5993 print_binder_proc(m, itr, 1);
5996 mutex_unlock(&binder_procs_lock);
6001 static void print_binder_transaction_log_entry(struct seq_file *m,
6002 struct binder_transaction_log_entry *e)
6004 int debug_id = READ_ONCE(e->debug_id_done);
6006 * read barrier to guarantee debug_id_done read before
6007 * we print the log values
6011 "%d: %s from %d:%d to %d:%d context %s node %d handle %d size %d:%d ret %d/%d l=%d",
6012 e->debug_id, (e->call_type == 2) ? "reply" :
6013 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
6014 e->from_thread, e->to_proc, e->to_thread, e->context_name,
6015 e->to_node, e->target_handle, e->data_size, e->offsets_size,
6016 e->return_error, e->return_error_param,
6017 e->return_error_line);
6019 * read-barrier to guarantee read of debug_id_done after
6020 * done printing the fields of the entry
6023 seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ?
6024 "\n" : " (incomplete)\n");
6027 int binder_transaction_log_show(struct seq_file *m, void *unused)
6029 struct binder_transaction_log *log = m->private;
6030 unsigned int log_cur = atomic_read(&log->cur);
6035 count = log_cur + 1;
6036 cur = count < ARRAY_SIZE(log->entry) && !log->full ?
6037 0 : count % ARRAY_SIZE(log->entry);
6038 if (count > ARRAY_SIZE(log->entry) || log->full)
6039 count = ARRAY_SIZE(log->entry);
6040 for (i = 0; i < count; i++) {
6041 unsigned int index = cur++ % ARRAY_SIZE(log->entry);
6043 print_binder_transaction_log_entry(m, &log->entry[index]);
6048 const struct file_operations binder_fops = {
6049 .owner = THIS_MODULE,
6050 .poll = binder_poll,
6051 .unlocked_ioctl = binder_ioctl,
6052 .compat_ioctl = compat_ptr_ioctl,
6053 .mmap = binder_mmap,
6054 .open = binder_open,
6055 .flush = binder_flush,
6056 .release = binder_release,
6059 static int __init init_binder_device(const char *name)
6062 struct binder_device *binder_device;
6064 binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
6068 binder_device->miscdev.fops = &binder_fops;
6069 binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
6070 binder_device->miscdev.name = name;
6072 refcount_set(&binder_device->ref, 1);
6073 binder_device->context.binder_context_mgr_uid = INVALID_UID;
6074 binder_device->context.name = name;
6075 mutex_init(&binder_device->context.context_mgr_node_lock);
6077 ret = misc_register(&binder_device->miscdev);
6079 kfree(binder_device);
6083 hlist_add_head(&binder_device->hlist, &binder_devices);
6088 static int __init binder_init(void)
6091 char *device_name, *device_tmp;
6092 struct binder_device *device;
6093 struct hlist_node *tmp;
6094 char *device_names = NULL;
6096 ret = binder_alloc_shrinker_init();
6100 atomic_set(&binder_transaction_log.cur, ~0U);
6101 atomic_set(&binder_transaction_log_failed.cur, ~0U);
6103 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
6104 if (binder_debugfs_dir_entry_root)
6105 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
6106 binder_debugfs_dir_entry_root);
6108 if (binder_debugfs_dir_entry_root) {
6109 debugfs_create_file("state",
6111 binder_debugfs_dir_entry_root,
6113 &binder_state_fops);
6114 debugfs_create_file("stats",
6116 binder_debugfs_dir_entry_root,
6118 &binder_stats_fops);
6119 debugfs_create_file("transactions",
6121 binder_debugfs_dir_entry_root,
6123 &binder_transactions_fops);
6124 debugfs_create_file("transaction_log",
6126 binder_debugfs_dir_entry_root,
6127 &binder_transaction_log,
6128 &binder_transaction_log_fops);
6129 debugfs_create_file("failed_transaction_log",
6131 binder_debugfs_dir_entry_root,
6132 &binder_transaction_log_failed,
6133 &binder_transaction_log_fops);
6136 if (!IS_ENABLED(CONFIG_ANDROID_BINDERFS) &&
6137 strcmp(binder_devices_param, "") != 0) {
6139 * Copy the module_parameter string, because we don't want to
6140 * tokenize it in-place.
6142 device_names = kstrdup(binder_devices_param, GFP_KERNEL);
6143 if (!device_names) {
6145 goto err_alloc_device_names_failed;
6148 device_tmp = device_names;
6149 while ((device_name = strsep(&device_tmp, ","))) {
6150 ret = init_binder_device(device_name);
6152 goto err_init_binder_device_failed;
6156 ret = init_binderfs();
6158 goto err_init_binder_device_failed;
6162 err_init_binder_device_failed:
6163 hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
6164 misc_deregister(&device->miscdev);
6165 hlist_del(&device->hlist);
6169 kfree(device_names);
6171 err_alloc_device_names_failed:
6172 debugfs_remove_recursive(binder_debugfs_dir_entry_root);
6177 device_initcall(binder_init);
6179 #define CREATE_TRACE_POINTS
6180 #include "binder_trace.h"
6182 MODULE_LICENSE("GPL v2");