3 * Android IPC Subsystem
5 * Copyright (C) 2007-2008 Google, Inc.
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
21 * There are 3 main spinlocks which must be acquired in the
24 * 1) proc->outer_lock : protects binder_ref
25 * binder_proc_lock() and binder_proc_unlock() are
27 * 2) node->lock : protects most fields of binder_node.
28 * binder_node_lock() and binder_node_unlock() are
30 * 3) proc->inner_lock : protects the thread and node lists
31 * (proc->threads, proc->waiting_threads, proc->nodes)
32 * and all todo lists associated with the binder_proc
33 * (proc->todo, thread->todo, proc->delivered_death and
34 * node->async_todo), as well as thread->transaction_stack
35 * binder_inner_proc_lock() and binder_inner_proc_unlock()
38 * Any lock under procA must never be nested under any lock at the same
39 * level or below on procB.
41 * Functions that require a lock held on entry indicate which lock
42 * in the suffix of the function name:
44 * foo_olocked() : requires node->outer_lock
45 * foo_nlocked() : requires node->lock
46 * foo_ilocked() : requires proc->inner_lock
47 * foo_oilocked(): requires proc->outer_lock and proc->inner_lock
48 * foo_nilocked(): requires node->lock and proc->inner_lock
52 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
54 #include <asm/cacheflush.h>
55 #include <linux/fdtable.h>
56 #include <linux/file.h>
57 #include <linux/freezer.h>
59 #include <linux/list.h>
60 #include <linux/miscdevice.h>
61 #include <linux/module.h>
62 #include <linux/mutex.h>
63 #include <linux/nsproxy.h>
64 #include <linux/poll.h>
65 #include <linux/debugfs.h>
66 #include <linux/rbtree.h>
67 #include <linux/sched.h>
68 #include <linux/seq_file.h>
69 #include <linux/uaccess.h>
70 #include <linux/pid_namespace.h>
71 #include <linux/security.h>
72 #include <linux/spinlock.h>
74 #include <uapi/linux/android/binder.h>
75 #include "binder_alloc.h"
76 #include "binder_trace.h"
78 static HLIST_HEAD(binder_deferred_list);
79 static DEFINE_MUTEX(binder_deferred_lock);
81 static HLIST_HEAD(binder_devices);
82 static HLIST_HEAD(binder_procs);
83 static DEFINE_MUTEX(binder_procs_lock);
85 static HLIST_HEAD(binder_dead_nodes);
86 static DEFINE_SPINLOCK(binder_dead_nodes_lock);
88 static struct dentry *binder_debugfs_dir_entry_root;
89 static struct dentry *binder_debugfs_dir_entry_proc;
90 static atomic_t binder_last_id;
91 static struct workqueue_struct *binder_deferred_workqueue;
93 #define BINDER_DEBUG_ENTRY(name) \
94 static int binder_##name##_open(struct inode *inode, struct file *file) \
96 return single_open(file, binder_##name##_show, inode->i_private); \
99 static const struct file_operations binder_##name##_fops = { \
100 .owner = THIS_MODULE, \
101 .open = binder_##name##_open, \
103 .llseek = seq_lseek, \
104 .release = single_release, \
107 static int binder_proc_show(struct seq_file *m, void *unused);
108 BINDER_DEBUG_ENTRY(proc);
110 /* This is only defined in include/asm-arm/sizes.h */
116 #define SZ_4M 0x400000
119 #define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
121 #define BINDER_SMALL_BUF_SIZE (PAGE_SIZE * 64)
124 BINDER_DEBUG_USER_ERROR = 1U << 0,
125 BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1,
126 BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2,
127 BINDER_DEBUG_OPEN_CLOSE = 1U << 3,
128 BINDER_DEBUG_DEAD_BINDER = 1U << 4,
129 BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5,
130 BINDER_DEBUG_READ_WRITE = 1U << 6,
131 BINDER_DEBUG_USER_REFS = 1U << 7,
132 BINDER_DEBUG_THREADS = 1U << 8,
133 BINDER_DEBUG_TRANSACTION = 1U << 9,
134 BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10,
135 BINDER_DEBUG_FREE_BUFFER = 1U << 11,
136 BINDER_DEBUG_INTERNAL_REFS = 1U << 12,
137 BINDER_DEBUG_PRIORITY_CAP = 1U << 13,
138 BINDER_DEBUG_SPINLOCKS = 1U << 14,
140 static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR |
141 BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION;
142 module_param_named(debug_mask, binder_debug_mask, uint, 0644);
144 static char *binder_devices_param = CONFIG_ANDROID_BINDER_DEVICES;
145 module_param_named(devices, binder_devices_param, charp, S_IRUGO);
147 static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
148 static int binder_stop_on_user_error;
150 static int binder_set_stop_on_user_error(const char *val,
151 struct kernel_param *kp)
155 ret = param_set_int(val, kp);
156 if (binder_stop_on_user_error < 2)
157 wake_up(&binder_user_error_wait);
160 module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
161 param_get_int, &binder_stop_on_user_error, 0644);
163 #define binder_debug(mask, x...) \
165 if (binder_debug_mask & mask) \
169 #define binder_user_error(x...) \
171 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
173 if (binder_stop_on_user_error) \
174 binder_stop_on_user_error = 2; \
177 #define to_flat_binder_object(hdr) \
178 container_of(hdr, struct flat_binder_object, hdr)
180 #define to_binder_fd_object(hdr) container_of(hdr, struct binder_fd_object, hdr)
182 #define to_binder_buffer_object(hdr) \
183 container_of(hdr, struct binder_buffer_object, hdr)
185 #define to_binder_fd_array_object(hdr) \
186 container_of(hdr, struct binder_fd_array_object, hdr)
188 enum binder_stat_types {
194 BINDER_STAT_TRANSACTION,
195 BINDER_STAT_TRANSACTION_COMPLETE,
199 struct binder_stats {
200 atomic_t br[_IOC_NR(BR_FAILED_REPLY) + 1];
201 atomic_t bc[_IOC_NR(BC_REPLY_SG) + 1];
202 atomic_t obj_created[BINDER_STAT_COUNT];
203 atomic_t obj_deleted[BINDER_STAT_COUNT];
206 static struct binder_stats binder_stats;
208 static inline void binder_stats_deleted(enum binder_stat_types type)
210 atomic_inc(&binder_stats.obj_deleted[type]);
213 static inline void binder_stats_created(enum binder_stat_types type)
215 atomic_inc(&binder_stats.obj_created[type]);
218 struct binder_transaction_log_entry {
230 int return_error_line;
231 uint32_t return_error;
232 uint32_t return_error_param;
233 const char *context_name;
235 struct binder_transaction_log {
238 struct binder_transaction_log_entry entry[32];
240 static struct binder_transaction_log binder_transaction_log;
241 static struct binder_transaction_log binder_transaction_log_failed;
243 static struct binder_transaction_log_entry *binder_transaction_log_add(
244 struct binder_transaction_log *log)
246 struct binder_transaction_log_entry *e;
247 unsigned int cur = atomic_inc_return(&log->cur);
249 if (cur >= ARRAY_SIZE(log->entry))
251 e = &log->entry[cur % ARRAY_SIZE(log->entry)];
252 WRITE_ONCE(e->debug_id_done, 0);
254 * write-barrier to synchronize access to e->debug_id_done.
255 * We make sure the initialized 0 value is seen before
256 * memset() other fields are zeroed by memset.
259 memset(e, 0, sizeof(*e));
263 struct binder_context {
264 struct binder_node *binder_context_mgr_node;
265 struct mutex context_mgr_node_lock;
267 kuid_t binder_context_mgr_uid;
271 struct binder_device {
272 struct hlist_node hlist;
273 struct miscdevice miscdev;
274 struct binder_context context;
278 * struct binder_work - work enqueued on a worklist
279 * @entry: node enqueued on list
280 * @type: type of work to be performed
282 * There are separate work lists for proc, thread, and node (async).
285 struct list_head entry;
288 BINDER_WORK_TRANSACTION = 1,
289 BINDER_WORK_TRANSACTION_COMPLETE,
290 BINDER_WORK_RETURN_ERROR,
292 BINDER_WORK_DEAD_BINDER,
293 BINDER_WORK_DEAD_BINDER_AND_CLEAR,
294 BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
298 struct binder_error {
299 struct binder_work work;
304 * struct binder_node - binder node bookkeeping
305 * @debug_id: unique ID for debugging
306 * (invariant after initialized)
307 * @lock: lock for node fields
308 * @work: worklist element for node work
309 * (protected by @proc->inner_lock)
310 * @rb_node: element for proc->nodes tree
311 * (protected by @proc->inner_lock)
312 * @dead_node: element for binder_dead_nodes list
313 * (protected by binder_dead_nodes_lock)
314 * @proc: binder_proc that owns this node
315 * (invariant after initialized)
316 * @refs: list of references on this node
317 * (protected by @lock)
318 * @internal_strong_refs: used to take strong references when
319 * initiating a transaction
320 * (protected by @proc->inner_lock if @proc
322 * @local_weak_refs: weak user refs from local process
323 * (protected by @proc->inner_lock if @proc
325 * @local_strong_refs: strong user refs from local process
326 * (protected by @proc->inner_lock if @proc
328 * @tmp_refs: temporary kernel refs
329 * (protected by @proc->inner_lock while @proc
330 * is valid, and by binder_dead_nodes_lock
331 * if @proc is NULL. During inc/dec and node release
332 * it is also protected by @lock to provide safety
333 * as the node dies and @proc becomes NULL)
334 * @ptr: userspace pointer for node
335 * (invariant, no lock needed)
336 * @cookie: userspace cookie for node
337 * (invariant, no lock needed)
338 * @has_strong_ref: userspace notified of strong ref
339 * (protected by @proc->inner_lock if @proc
341 * @pending_strong_ref: userspace has acked notification of strong ref
342 * (protected by @proc->inner_lock if @proc
344 * @has_weak_ref: userspace notified of weak ref
345 * (protected by @proc->inner_lock if @proc
347 * @pending_weak_ref: userspace has acked notification of weak ref
348 * (protected by @proc->inner_lock if @proc
350 * @has_async_transaction: async transaction to node in progress
351 * (protected by @lock)
352 * @sched_policy: minimum scheduling policy for node
353 * (invariant after initialized)
354 * @accept_fds: file descriptor operations supported for node
355 * (invariant after initialized)
356 * @min_priority: minimum scheduling priority
357 * (invariant after initialized)
358 * @inherit_rt: inherit RT scheduling policy from caller
359 * @txn_security_ctx: require sender's security context
360 * (invariant after initialized)
361 * @async_todo: list of async work items
362 * (protected by @proc->inner_lock)
364 * Bookkeeping structure for binder nodes.
369 struct binder_work work;
371 struct rb_node rb_node;
372 struct hlist_node dead_node;
374 struct binder_proc *proc;
375 struct hlist_head refs;
376 int internal_strong_refs;
378 int local_strong_refs;
380 binder_uintptr_t ptr;
381 binder_uintptr_t cookie;
384 * bitfield elements protected by
388 u8 pending_strong_ref:1;
390 u8 pending_weak_ref:1;
394 * invariant after initialization
399 u8 txn_security_ctx:1;
402 bool has_async_transaction;
403 struct list_head async_todo;
406 struct binder_ref_death {
408 * @work: worklist element for death notifications
409 * (protected by inner_lock of the proc that
410 * this ref belongs to)
412 struct binder_work work;
413 binder_uintptr_t cookie;
417 * struct binder_ref_data - binder_ref counts and id
418 * @debug_id: unique ID for the ref
419 * @desc: unique userspace handle for ref
420 * @strong: strong ref count (debugging only if not locked)
421 * @weak: weak ref count (debugging only if not locked)
423 * Structure to hold ref count and ref id information. Since
424 * the actual ref can only be accessed with a lock, this structure
425 * is used to return information about the ref to callers of
426 * ref inc/dec functions.
428 struct binder_ref_data {
436 * struct binder_ref - struct to track references on nodes
437 * @data: binder_ref_data containing id, handle, and current refcounts
438 * @rb_node_desc: node for lookup by @data.desc in proc's rb_tree
439 * @rb_node_node: node for lookup by @node in proc's rb_tree
440 * @node_entry: list entry for node->refs list in target node
441 * (protected by @node->lock)
442 * @proc: binder_proc containing ref
443 * @node: binder_node of target node. When cleaning up a
444 * ref for deletion in binder_cleanup_ref, a non-NULL
445 * @node indicates the node must be freed
446 * @death: pointer to death notification (ref_death) if requested
447 * (protected by @node->lock)
449 * Structure to track references from procA to target node (on procB). This
450 * structure is unsafe to access without holding @proc->outer_lock.
453 /* Lookups needed: */
454 /* node + proc => ref (transaction) */
455 /* desc + proc => ref (transaction, inc/dec ref) */
456 /* node => refs + procs (proc exit) */
457 struct binder_ref_data data;
458 struct rb_node rb_node_desc;
459 struct rb_node rb_node_node;
460 struct hlist_node node_entry;
461 struct binder_proc *proc;
462 struct binder_node *node;
463 struct binder_ref_death *death;
466 enum binder_deferred_state {
467 BINDER_DEFERRED_PUT_FILES = 0x01,
468 BINDER_DEFERRED_FLUSH = 0x02,
469 BINDER_DEFERRED_RELEASE = 0x04,
473 * struct binder_priority - scheduler policy and priority
474 * @sched_policy scheduler policy
475 * @prio [100..139] for SCHED_NORMAL, [0..99] for FIFO/RT
477 * The binder driver supports inheriting the following scheduler policies:
483 struct binder_priority {
484 unsigned int sched_policy;
489 * struct binder_proc - binder process bookkeeping
490 * @proc_node: element for binder_procs list
491 * @threads: rbtree of binder_threads in this proc
492 * (protected by @inner_lock)
493 * @nodes: rbtree of binder nodes associated with
494 * this proc ordered by node->ptr
495 * (protected by @inner_lock)
496 * @refs_by_desc: rbtree of refs ordered by ref->desc
497 * (protected by @outer_lock)
498 * @refs_by_node: rbtree of refs ordered by ref->node
499 * (protected by @outer_lock)
500 * @waiting_threads: threads currently waiting for proc work
501 * (protected by @inner_lock)
502 * @pid PID of group_leader of process
503 * (invariant after initialized)
504 * @tsk task_struct for group_leader of process
505 * (invariant after initialized)
506 * @files files_struct for process
507 * (protected by @files_lock)
508 * @files_lock mutex to protect @files
509 * @deferred_work_node: element for binder_deferred_list
510 * (protected by binder_deferred_lock)
511 * @deferred_work: bitmap of deferred work to perform
512 * (protected by binder_deferred_lock)
513 * @is_dead: process is dead and awaiting free
514 * when outstanding transactions are cleaned up
515 * (protected by @inner_lock)
516 * @todo: list of work for this process
517 * (protected by @inner_lock)
518 * @stats: per-process binder statistics
519 * (atomics, no lock needed)
520 * @delivered_death: list of delivered death notification
521 * (protected by @inner_lock)
522 * @max_threads: cap on number of binder threads
523 * (protected by @inner_lock)
524 * @requested_threads: number of binder threads requested but not
525 * yet started. In current implementation, can
527 * (protected by @inner_lock)
528 * @requested_threads_started: number binder threads started
529 * (protected by @inner_lock)
530 * @tmp_ref: temporary reference to indicate proc is in use
531 * (protected by @inner_lock)
532 * @default_priority: default scheduler priority
533 * (invariant after initialized)
534 * @debugfs_entry: debugfs node
535 * @alloc: binder allocator bookkeeping
536 * @context: binder_context for this proc
537 * (invariant after initialized)
538 * @inner_lock: can nest under outer_lock and/or node lock
539 * @outer_lock: no nesting under innor or node lock
540 * Lock order: 1) outer, 2) node, 3) inner
542 * Bookkeeping structure for binder processes
545 struct hlist_node proc_node;
546 struct rb_root threads;
547 struct rb_root nodes;
548 struct rb_root refs_by_desc;
549 struct rb_root refs_by_node;
550 struct list_head waiting_threads;
552 struct task_struct *tsk;
553 struct files_struct *files;
554 struct mutex files_lock;
555 struct hlist_node deferred_work_node;
559 struct list_head todo;
560 struct binder_stats stats;
561 struct list_head delivered_death;
563 int requested_threads;
564 int requested_threads_started;
566 struct binder_priority default_priority;
567 struct dentry *debugfs_entry;
568 struct binder_alloc alloc;
569 struct binder_context *context;
570 spinlock_t inner_lock;
571 spinlock_t outer_lock;
575 BINDER_LOOPER_STATE_REGISTERED = 0x01,
576 BINDER_LOOPER_STATE_ENTERED = 0x02,
577 BINDER_LOOPER_STATE_EXITED = 0x04,
578 BINDER_LOOPER_STATE_INVALID = 0x08,
579 BINDER_LOOPER_STATE_WAITING = 0x10,
580 BINDER_LOOPER_STATE_POLL = 0x20,
584 * struct binder_thread - binder thread bookkeeping
585 * @proc: binder process for this thread
586 * (invariant after initialization)
587 * @rb_node: element for proc->threads rbtree
588 * (protected by @proc->inner_lock)
589 * @waiting_thread_node: element for @proc->waiting_threads list
590 * (protected by @proc->inner_lock)
591 * @pid: PID for this thread
592 * (invariant after initialization)
593 * @looper: bitmap of looping state
594 * (only accessed by this thread)
595 * @looper_needs_return: looping thread needs to exit driver
597 * @transaction_stack: stack of in-progress transactions for this thread
598 * (protected by @proc->inner_lock)
599 * @todo: list of work to do for this thread
600 * (protected by @proc->inner_lock)
601 * @process_todo: whether work in @todo should be processed
602 * (protected by @proc->inner_lock)
603 * @return_error: transaction errors reported by this thread
604 * (only accessed by this thread)
605 * @reply_error: transaction errors reported by target thread
606 * (protected by @proc->inner_lock)
607 * @wait: wait queue for thread work
608 * @stats: per-thread statistics
609 * (atomics, no lock needed)
610 * @tmp_ref: temporary reference to indicate thread is in use
611 * (atomic since @proc->inner_lock cannot
612 * always be acquired)
613 * @is_dead: thread is dead and awaiting free
614 * when outstanding transactions are cleaned up
615 * (protected by @proc->inner_lock)
616 * @task: struct task_struct for this thread
618 * Bookkeeping structure for binder threads.
620 struct binder_thread {
621 struct binder_proc *proc;
622 struct rb_node rb_node;
623 struct list_head waiting_thread_node;
625 int looper; /* only modified by this thread */
626 bool looper_need_return; /* can be written by other thread */
627 struct binder_transaction *transaction_stack;
628 struct list_head todo;
630 struct binder_error return_error;
631 struct binder_error reply_error;
632 wait_queue_head_t wait;
633 struct binder_stats stats;
636 struct task_struct *task;
639 struct binder_transaction {
641 struct binder_work work;
642 struct binder_thread *from;
643 struct binder_transaction *from_parent;
644 struct binder_proc *to_proc;
645 struct binder_thread *to_thread;
646 struct binder_transaction *to_parent;
647 unsigned need_reply:1;
648 /* unsigned is_dead:1; */ /* not used at the moment */
650 struct binder_buffer *buffer;
653 struct binder_priority priority;
654 struct binder_priority saved_priority;
655 bool set_priority_called;
657 binder_uintptr_t security_ctx;
659 * @lock: protects @from, @to_proc, and @to_thread
661 * @from, @to_proc, and @to_thread can be set to NULL
662 * during thread teardown
668 * binder_proc_lock() - Acquire outer lock for given binder_proc
669 * @proc: struct binder_proc to acquire
671 * Acquires proc->outer_lock. Used to protect binder_ref
672 * structures associated with the given proc.
674 #define binder_proc_lock(proc) _binder_proc_lock(proc, __LINE__)
676 _binder_proc_lock(struct binder_proc *proc, int line)
678 binder_debug(BINDER_DEBUG_SPINLOCKS,
679 "%s: line=%d\n", __func__, line);
680 spin_lock(&proc->outer_lock);
684 * binder_proc_unlock() - Release spinlock for given binder_proc
685 * @proc: struct binder_proc to acquire
687 * Release lock acquired via binder_proc_lock()
689 #define binder_proc_unlock(_proc) _binder_proc_unlock(_proc, __LINE__)
691 _binder_proc_unlock(struct binder_proc *proc, int line)
693 binder_debug(BINDER_DEBUG_SPINLOCKS,
694 "%s: line=%d\n", __func__, line);
695 spin_unlock(&proc->outer_lock);
699 * binder_inner_proc_lock() - Acquire inner lock for given binder_proc
700 * @proc: struct binder_proc to acquire
702 * Acquires proc->inner_lock. Used to protect todo lists
704 #define binder_inner_proc_lock(proc) _binder_inner_proc_lock(proc, __LINE__)
706 _binder_inner_proc_lock(struct binder_proc *proc, int line)
708 binder_debug(BINDER_DEBUG_SPINLOCKS,
709 "%s: line=%d\n", __func__, line);
710 spin_lock(&proc->inner_lock);
714 * binder_inner_proc_unlock() - Release inner lock for given binder_proc
715 * @proc: struct binder_proc to acquire
717 * Release lock acquired via binder_inner_proc_lock()
719 #define binder_inner_proc_unlock(proc) _binder_inner_proc_unlock(proc, __LINE__)
721 _binder_inner_proc_unlock(struct binder_proc *proc, int line)
723 binder_debug(BINDER_DEBUG_SPINLOCKS,
724 "%s: line=%d\n", __func__, line);
725 spin_unlock(&proc->inner_lock);
729 * binder_node_lock() - Acquire spinlock for given binder_node
730 * @node: struct binder_node to acquire
732 * Acquires node->lock. Used to protect binder_node fields
734 #define binder_node_lock(node) _binder_node_lock(node, __LINE__)
736 _binder_node_lock(struct binder_node *node, int line)
738 binder_debug(BINDER_DEBUG_SPINLOCKS,
739 "%s: line=%d\n", __func__, line);
740 spin_lock(&node->lock);
744 * binder_node_unlock() - Release spinlock for given binder_proc
745 * @node: struct binder_node to acquire
747 * Release lock acquired via binder_node_lock()
749 #define binder_node_unlock(node) _binder_node_unlock(node, __LINE__)
751 _binder_node_unlock(struct binder_node *node, int line)
753 binder_debug(BINDER_DEBUG_SPINLOCKS,
754 "%s: line=%d\n", __func__, line);
755 spin_unlock(&node->lock);
759 * binder_node_inner_lock() - Acquire node and inner locks
760 * @node: struct binder_node to acquire
762 * Acquires node->lock. If node->proc also acquires
763 * proc->inner_lock. Used to protect binder_node fields
765 #define binder_node_inner_lock(node) _binder_node_inner_lock(node, __LINE__)
767 _binder_node_inner_lock(struct binder_node *node, int line)
769 binder_debug(BINDER_DEBUG_SPINLOCKS,
770 "%s: line=%d\n", __func__, line);
771 spin_lock(&node->lock);
773 binder_inner_proc_lock(node->proc);
777 * binder_node_unlock() - Release node and inner locks
778 * @node: struct binder_node to acquire
780 * Release lock acquired via binder_node_lock()
782 #define binder_node_inner_unlock(node) _binder_node_inner_unlock(node, __LINE__)
784 _binder_node_inner_unlock(struct binder_node *node, int line)
786 struct binder_proc *proc = node->proc;
788 binder_debug(BINDER_DEBUG_SPINLOCKS,
789 "%s: line=%d\n", __func__, line);
791 binder_inner_proc_unlock(proc);
792 spin_unlock(&node->lock);
795 static bool binder_worklist_empty_ilocked(struct list_head *list)
797 return list_empty(list);
801 * binder_worklist_empty() - Check if no items on the work list
802 * @proc: binder_proc associated with list
803 * @list: list to check
805 * Return: true if there are no items on list, else false
807 static bool binder_worklist_empty(struct binder_proc *proc,
808 struct list_head *list)
812 binder_inner_proc_lock(proc);
813 ret = binder_worklist_empty_ilocked(list);
814 binder_inner_proc_unlock(proc);
819 * binder_enqueue_work_ilocked() - Add an item to the work list
820 * @work: struct binder_work to add to list
821 * @target_list: list to add work to
823 * Adds the work to the specified list. Asserts that work
824 * is not already on a list.
826 * Requires the proc->inner_lock to be held.
829 binder_enqueue_work_ilocked(struct binder_work *work,
830 struct list_head *target_list)
832 BUG_ON(target_list == NULL);
833 BUG_ON(work->entry.next && !list_empty(&work->entry));
834 list_add_tail(&work->entry, target_list);
838 * binder_enqueue_deferred_thread_work_ilocked() - Add deferred thread work
839 * @thread: thread to queue work to
840 * @work: struct binder_work to add to list
842 * Adds the work to the todo list of the thread. Doesn't set the process_todo
843 * flag, which means that (if it wasn't already set) the thread will go to
844 * sleep without handling this work when it calls read.
846 * Requires the proc->inner_lock to be held.
849 binder_enqueue_deferred_thread_work_ilocked(struct binder_thread *thread,
850 struct binder_work *work)
852 binder_enqueue_work_ilocked(work, &thread->todo);
856 * binder_enqueue_thread_work_ilocked() - Add an item to the thread work list
857 * @thread: thread to queue work to
858 * @work: struct binder_work to add to list
860 * Adds the work to the todo list of the thread, and enables processing
863 * Requires the proc->inner_lock to be held.
866 binder_enqueue_thread_work_ilocked(struct binder_thread *thread,
867 struct binder_work *work)
869 binder_enqueue_work_ilocked(work, &thread->todo);
870 thread->process_todo = true;
874 * binder_enqueue_thread_work() - Add an item to the thread work list
875 * @thread: thread to queue work to
876 * @work: struct binder_work to add to list
878 * Adds the work to the todo list of the thread, and enables processing
882 binder_enqueue_thread_work(struct binder_thread *thread,
883 struct binder_work *work)
885 binder_inner_proc_lock(thread->proc);
886 binder_enqueue_thread_work_ilocked(thread, work);
887 binder_inner_proc_unlock(thread->proc);
891 binder_dequeue_work_ilocked(struct binder_work *work)
893 list_del_init(&work->entry);
897 * binder_dequeue_work() - Removes an item from the work list
898 * @proc: binder_proc associated with list
899 * @work: struct binder_work to remove from list
901 * Removes the specified work item from whatever list it is on.
902 * Can safely be called if work is not on any list.
905 binder_dequeue_work(struct binder_proc *proc, struct binder_work *work)
907 binder_inner_proc_lock(proc);
908 binder_dequeue_work_ilocked(work);
909 binder_inner_proc_unlock(proc);
912 static struct binder_work *binder_dequeue_work_head_ilocked(
913 struct list_head *list)
915 struct binder_work *w;
917 w = list_first_entry_or_null(list, struct binder_work, entry);
919 list_del_init(&w->entry);
924 * binder_dequeue_work_head() - Dequeues the item at head of list
925 * @proc: binder_proc associated with list
926 * @list: list to dequeue head
928 * Removes the head of the list if there are items on the list
930 * Return: pointer dequeued binder_work, NULL if list was empty
932 static struct binder_work *binder_dequeue_work_head(
933 struct binder_proc *proc,
934 struct list_head *list)
936 struct binder_work *w;
938 binder_inner_proc_lock(proc);
939 w = binder_dequeue_work_head_ilocked(list);
940 binder_inner_proc_unlock(proc);
945 binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
946 static void binder_free_thread(struct binder_thread *thread);
947 static void binder_free_proc(struct binder_proc *proc);
948 static void binder_inc_node_tmpref_ilocked(struct binder_node *node);
950 static int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
952 unsigned long rlim_cur;
956 mutex_lock(&proc->files_lock);
957 if (proc->files == NULL) {
961 if (!lock_task_sighand(proc->tsk, &irqs)) {
965 rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
966 unlock_task_sighand(proc->tsk, &irqs);
968 ret = __alloc_fd(proc->files, 0, rlim_cur, flags);
970 mutex_unlock(&proc->files_lock);
975 * copied from fd_install
977 static void task_fd_install(
978 struct binder_proc *proc, unsigned int fd, struct file *file)
980 mutex_lock(&proc->files_lock);
982 __fd_install(proc->files, fd, file);
983 mutex_unlock(&proc->files_lock);
987 * copied from sys_close
989 static long task_close_fd(struct binder_proc *proc, unsigned int fd)
993 mutex_lock(&proc->files_lock);
994 if (proc->files == NULL) {
998 retval = __close_fd(proc->files, fd);
999 /* can't restart close syscall because file table entry was cleared */
1000 if (unlikely(retval == -ERESTARTSYS ||
1001 retval == -ERESTARTNOINTR ||
1002 retval == -ERESTARTNOHAND ||
1003 retval == -ERESTART_RESTARTBLOCK))
1006 mutex_unlock(&proc->files_lock);
1010 static bool binder_has_work_ilocked(struct binder_thread *thread,
1013 return thread->process_todo ||
1014 thread->looper_need_return ||
1016 !binder_worklist_empty_ilocked(&thread->proc->todo));
1019 static bool binder_has_work(struct binder_thread *thread, bool do_proc_work)
1023 binder_inner_proc_lock(thread->proc);
1024 has_work = binder_has_work_ilocked(thread, do_proc_work);
1025 binder_inner_proc_unlock(thread->proc);
1030 static bool binder_available_for_proc_work_ilocked(struct binder_thread *thread)
1032 return !thread->transaction_stack &&
1033 binder_worklist_empty_ilocked(&thread->todo) &&
1034 (thread->looper & (BINDER_LOOPER_STATE_ENTERED |
1035 BINDER_LOOPER_STATE_REGISTERED));
1038 static void binder_wakeup_poll_threads_ilocked(struct binder_proc *proc,
1042 struct binder_thread *thread;
1044 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
1045 thread = rb_entry(n, struct binder_thread, rb_node);
1046 if (thread->looper & BINDER_LOOPER_STATE_POLL &&
1047 binder_available_for_proc_work_ilocked(thread)) {
1049 wake_up_interruptible_sync(&thread->wait);
1051 wake_up_interruptible(&thread->wait);
1057 * binder_select_thread_ilocked() - selects a thread for doing proc work.
1058 * @proc: process to select a thread from
1060 * Note that calling this function moves the thread off the waiting_threads
1061 * list, so it can only be woken up by the caller of this function, or a
1062 * signal. Therefore, callers *should* always wake up the thread this function
1065 * Return: If there's a thread currently waiting for process work,
1066 * returns that thread. Otherwise returns NULL.
1068 static struct binder_thread *
1069 binder_select_thread_ilocked(struct binder_proc *proc)
1071 struct binder_thread *thread;
1073 assert_spin_locked(&proc->inner_lock);
1074 thread = list_first_entry_or_null(&proc->waiting_threads,
1075 struct binder_thread,
1076 waiting_thread_node);
1079 list_del_init(&thread->waiting_thread_node);
1085 * binder_wakeup_thread_ilocked() - wakes up a thread for doing proc work.
1086 * @proc: process to wake up a thread in
1087 * @thread: specific thread to wake-up (may be NULL)
1088 * @sync: whether to do a synchronous wake-up
1090 * This function wakes up a thread in the @proc process.
1091 * The caller may provide a specific thread to wake-up in
1092 * the @thread parameter. If @thread is NULL, this function
1093 * will wake up threads that have called poll().
1095 * Note that for this function to work as expected, callers
1096 * should first call binder_select_thread() to find a thread
1097 * to handle the work (if they don't have a thread already),
1098 * and pass the result into the @thread parameter.
1100 static void binder_wakeup_thread_ilocked(struct binder_proc *proc,
1101 struct binder_thread *thread,
1104 assert_spin_locked(&proc->inner_lock);
1108 wake_up_interruptible_sync(&thread->wait);
1110 wake_up_interruptible(&thread->wait);
1114 /* Didn't find a thread waiting for proc work; this can happen
1116 * 1. All threads are busy handling transactions
1117 * In that case, one of those threads should call back into
1118 * the kernel driver soon and pick up this work.
1119 * 2. Threads are using the (e)poll interface, in which case
1120 * they may be blocked on the waitqueue without having been
1121 * added to waiting_threads. For this case, we just iterate
1122 * over all threads not handling transaction work, and
1123 * wake them all up. We wake all because we don't know whether
1124 * a thread that called into (e)poll is handling non-binder
1127 binder_wakeup_poll_threads_ilocked(proc, sync);
1130 static void binder_wakeup_proc_ilocked(struct binder_proc *proc)
1132 struct binder_thread *thread = binder_select_thread_ilocked(proc);
1134 binder_wakeup_thread_ilocked(proc, thread, /* sync = */false);
1137 static bool is_rt_policy(int policy)
1139 return policy == SCHED_FIFO || policy == SCHED_RR;
1142 static bool is_fair_policy(int policy)
1144 return policy == SCHED_NORMAL || policy == SCHED_BATCH;
1147 static bool binder_supported_policy(int policy)
1149 return is_fair_policy(policy) || is_rt_policy(policy);
1152 static int to_userspace_prio(int policy, int kernel_priority)
1154 if (is_fair_policy(policy))
1155 return PRIO_TO_NICE(kernel_priority);
1157 return MAX_USER_RT_PRIO - 1 - kernel_priority;
1160 static int to_kernel_prio(int policy, int user_priority)
1162 if (is_fair_policy(policy))
1163 return NICE_TO_PRIO(user_priority);
1165 return MAX_USER_RT_PRIO - 1 - user_priority;
1168 static void binder_do_set_priority(struct task_struct *task,
1169 struct binder_priority desired,
1172 int priority; /* user-space prio value */
1174 unsigned int policy = desired.sched_policy;
1176 if (task->policy == policy && task->normal_prio == desired.prio)
1179 has_cap_nice = has_capability_noaudit(task, CAP_SYS_NICE);
1181 priority = to_userspace_prio(policy, desired.prio);
1183 if (verify && is_rt_policy(policy) && !has_cap_nice) {
1184 long max_rtprio = task_rlimit(task, RLIMIT_RTPRIO);
1186 if (max_rtprio == 0) {
1187 policy = SCHED_NORMAL;
1188 priority = MIN_NICE;
1189 } else if (priority > max_rtprio) {
1190 priority = max_rtprio;
1194 if (verify && is_fair_policy(policy) && !has_cap_nice) {
1195 long min_nice = rlimit_to_nice(task_rlimit(task, RLIMIT_NICE));
1197 if (min_nice > MAX_NICE) {
1198 binder_user_error("%d RLIMIT_NICE not set\n",
1201 } else if (priority < min_nice) {
1202 priority = min_nice;
1206 if (policy != desired.sched_policy ||
1207 to_kernel_prio(policy, priority) != desired.prio)
1208 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
1209 "%d: priority %d not allowed, using %d instead\n",
1210 task->pid, desired.prio,
1211 to_kernel_prio(policy, priority));
1213 trace_binder_set_priority(task->tgid, task->pid, task->normal_prio,
1214 to_kernel_prio(policy, priority),
1217 /* Set the actual priority */
1218 if (task->policy != policy || is_rt_policy(policy)) {
1219 struct sched_param params;
1221 params.sched_priority = is_rt_policy(policy) ? priority : 0;
1223 sched_setscheduler_nocheck(task,
1224 policy | SCHED_RESET_ON_FORK,
1227 if (is_fair_policy(policy))
1228 set_user_nice(task, priority);
1231 static void binder_set_priority(struct task_struct *task,
1232 struct binder_priority desired)
1234 binder_do_set_priority(task, desired, /* verify = */ true);
1237 static void binder_restore_priority(struct task_struct *task,
1238 struct binder_priority desired)
1240 binder_do_set_priority(task, desired, /* verify = */ false);
1243 static void binder_transaction_priority(struct task_struct *task,
1244 struct binder_transaction *t,
1245 struct binder_priority node_prio,
1248 struct binder_priority desired_prio = t->priority;
1250 if (t->set_priority_called)
1253 t->set_priority_called = true;
1254 t->saved_priority.sched_policy = task->policy;
1255 t->saved_priority.prio = task->normal_prio;
1257 if (!inherit_rt && is_rt_policy(desired_prio.sched_policy)) {
1258 desired_prio.prio = NICE_TO_PRIO(0);
1259 desired_prio.sched_policy = SCHED_NORMAL;
1262 if (node_prio.prio < t->priority.prio ||
1263 (node_prio.prio == t->priority.prio &&
1264 node_prio.sched_policy == SCHED_FIFO)) {
1266 * In case the minimum priority on the node is
1267 * higher (lower value), use that priority. If
1268 * the priority is the same, but the node uses
1269 * SCHED_FIFO, prefer SCHED_FIFO, since it can
1270 * run unbounded, unlike SCHED_RR.
1272 desired_prio = node_prio;
1275 binder_set_priority(task, desired_prio);
1278 static struct binder_node *binder_get_node_ilocked(struct binder_proc *proc,
1279 binder_uintptr_t ptr)
1281 struct rb_node *n = proc->nodes.rb_node;
1282 struct binder_node *node;
1284 assert_spin_locked(&proc->inner_lock);
1287 node = rb_entry(n, struct binder_node, rb_node);
1289 if (ptr < node->ptr)
1291 else if (ptr > node->ptr)
1295 * take an implicit weak reference
1296 * to ensure node stays alive until
1297 * call to binder_put_node()
1299 binder_inc_node_tmpref_ilocked(node);
1306 static struct binder_node *binder_get_node(struct binder_proc *proc,
1307 binder_uintptr_t ptr)
1309 struct binder_node *node;
1311 binder_inner_proc_lock(proc);
1312 node = binder_get_node_ilocked(proc, ptr);
1313 binder_inner_proc_unlock(proc);
1317 static struct binder_node *binder_init_node_ilocked(
1318 struct binder_proc *proc,
1319 struct binder_node *new_node,
1320 struct flat_binder_object *fp)
1322 struct rb_node **p = &proc->nodes.rb_node;
1323 struct rb_node *parent = NULL;
1324 struct binder_node *node;
1325 binder_uintptr_t ptr = fp ? fp->binder : 0;
1326 binder_uintptr_t cookie = fp ? fp->cookie : 0;
1327 __u32 flags = fp ? fp->flags : 0;
1330 assert_spin_locked(&proc->inner_lock);
1335 node = rb_entry(parent, struct binder_node, rb_node);
1337 if (ptr < node->ptr)
1339 else if (ptr > node->ptr)
1340 p = &(*p)->rb_right;
1343 * A matching node is already in
1344 * the rb tree. Abandon the init
1347 binder_inc_node_tmpref_ilocked(node);
1352 binder_stats_created(BINDER_STAT_NODE);
1354 rb_link_node(&node->rb_node, parent, p);
1355 rb_insert_color(&node->rb_node, &proc->nodes);
1356 node->debug_id = atomic_inc_return(&binder_last_id);
1359 node->cookie = cookie;
1360 node->work.type = BINDER_WORK_NODE;
1361 priority = flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
1362 node->sched_policy = (flags & FLAT_BINDER_FLAG_SCHED_POLICY_MASK) >>
1363 FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT;
1364 node->min_priority = to_kernel_prio(node->sched_policy, priority);
1365 node->accept_fds = !!(flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
1366 node->inherit_rt = !!(flags & FLAT_BINDER_FLAG_INHERIT_RT);
1367 node->txn_security_ctx = !!(flags & FLAT_BINDER_FLAG_TXN_SECURITY_CTX);
1368 spin_lock_init(&node->lock);
1369 INIT_LIST_HEAD(&node->work.entry);
1370 INIT_LIST_HEAD(&node->async_todo);
1371 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1372 "%d:%d node %d u%016llx c%016llx created\n",
1373 proc->pid, current->pid, node->debug_id,
1374 (u64)node->ptr, (u64)node->cookie);
1379 static struct binder_node *binder_new_node(struct binder_proc *proc,
1380 struct flat_binder_object *fp)
1382 struct binder_node *node;
1383 struct binder_node *new_node = kzalloc(sizeof(*node), GFP_KERNEL);
1387 binder_inner_proc_lock(proc);
1388 node = binder_init_node_ilocked(proc, new_node, fp);
1389 binder_inner_proc_unlock(proc);
1390 if (node != new_node)
1392 * The node was already added by another thread
1399 static void binder_free_node(struct binder_node *node)
1402 binder_stats_deleted(BINDER_STAT_NODE);
1405 static int binder_inc_node_nilocked(struct binder_node *node, int strong,
1407 struct list_head *target_list)
1409 struct binder_proc *proc = node->proc;
1411 assert_spin_locked(&node->lock);
1413 assert_spin_locked(&proc->inner_lock);
1416 if (target_list == NULL &&
1417 node->internal_strong_refs == 0 &&
1419 node == node->proc->context->
1420 binder_context_mgr_node &&
1421 node->has_strong_ref)) {
1422 pr_err("invalid inc strong node for %d\n",
1426 node->internal_strong_refs++;
1428 node->local_strong_refs++;
1429 if (!node->has_strong_ref && target_list) {
1430 binder_dequeue_work_ilocked(&node->work);
1432 * Note: this function is the only place where we queue
1433 * directly to a thread->todo without using the
1434 * corresponding binder_enqueue_thread_work() helper
1435 * functions; in this case it's ok to not set the
1436 * process_todo flag, since we know this node work will
1437 * always be followed by other work that starts queue
1438 * processing: in case of synchronous transactions, a
1439 * BR_REPLY or BR_ERROR; in case of oneway
1440 * transactions, a BR_TRANSACTION_COMPLETE.
1442 binder_enqueue_work_ilocked(&node->work, target_list);
1446 node->local_weak_refs++;
1447 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
1448 if (target_list == NULL) {
1449 pr_err("invalid inc weak node for %d\n",
1456 binder_enqueue_work_ilocked(&node->work, target_list);
1462 static int binder_inc_node(struct binder_node *node, int strong, int internal,
1463 struct list_head *target_list)
1467 binder_node_inner_lock(node);
1468 ret = binder_inc_node_nilocked(node, strong, internal, target_list);
1469 binder_node_inner_unlock(node);
1474 static bool binder_dec_node_nilocked(struct binder_node *node,
1475 int strong, int internal)
1477 struct binder_proc *proc = node->proc;
1479 assert_spin_locked(&node->lock);
1481 assert_spin_locked(&proc->inner_lock);
1484 node->internal_strong_refs--;
1486 node->local_strong_refs--;
1487 if (node->local_strong_refs || node->internal_strong_refs)
1491 node->local_weak_refs--;
1492 if (node->local_weak_refs || node->tmp_refs ||
1493 !hlist_empty(&node->refs))
1497 if (proc && (node->has_strong_ref || node->has_weak_ref)) {
1498 if (list_empty(&node->work.entry)) {
1499 binder_enqueue_work_ilocked(&node->work, &proc->todo);
1500 binder_wakeup_proc_ilocked(proc);
1503 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
1504 !node->local_weak_refs && !node->tmp_refs) {
1506 binder_dequeue_work_ilocked(&node->work);
1507 rb_erase(&node->rb_node, &proc->nodes);
1508 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1509 "refless node %d deleted\n",
1512 BUG_ON(!list_empty(&node->work.entry));
1513 spin_lock(&binder_dead_nodes_lock);
1515 * tmp_refs could have changed so
1518 if (node->tmp_refs) {
1519 spin_unlock(&binder_dead_nodes_lock);
1522 hlist_del(&node->dead_node);
1523 spin_unlock(&binder_dead_nodes_lock);
1524 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1525 "dead node %d deleted\n",
1534 static void binder_dec_node(struct binder_node *node, int strong, int internal)
1538 binder_node_inner_lock(node);
1539 free_node = binder_dec_node_nilocked(node, strong, internal);
1540 binder_node_inner_unlock(node);
1542 binder_free_node(node);
1545 static void binder_inc_node_tmpref_ilocked(struct binder_node *node)
1548 * No call to binder_inc_node() is needed since we
1549 * don't need to inform userspace of any changes to
1556 * binder_inc_node_tmpref() - take a temporary reference on node
1557 * @node: node to reference
1559 * Take reference on node to prevent the node from being freed
1560 * while referenced only by a local variable. The inner lock is
1561 * needed to serialize with the node work on the queue (which
1562 * isn't needed after the node is dead). If the node is dead
1563 * (node->proc is NULL), use binder_dead_nodes_lock to protect
1564 * node->tmp_refs against dead-node-only cases where the node
1565 * lock cannot be acquired (eg traversing the dead node list to
1568 static void binder_inc_node_tmpref(struct binder_node *node)
1570 binder_node_lock(node);
1572 binder_inner_proc_lock(node->proc);
1574 spin_lock(&binder_dead_nodes_lock);
1575 binder_inc_node_tmpref_ilocked(node);
1577 binder_inner_proc_unlock(node->proc);
1579 spin_unlock(&binder_dead_nodes_lock);
1580 binder_node_unlock(node);
1584 * binder_dec_node_tmpref() - remove a temporary reference on node
1585 * @node: node to reference
1587 * Release temporary reference on node taken via binder_inc_node_tmpref()
1589 static void binder_dec_node_tmpref(struct binder_node *node)
1593 binder_node_inner_lock(node);
1595 spin_lock(&binder_dead_nodes_lock);
1597 BUG_ON(node->tmp_refs < 0);
1599 spin_unlock(&binder_dead_nodes_lock);
1601 * Call binder_dec_node() to check if all refcounts are 0
1602 * and cleanup is needed. Calling with strong=0 and internal=1
1603 * causes no actual reference to be released in binder_dec_node().
1604 * If that changes, a change is needed here too.
1606 free_node = binder_dec_node_nilocked(node, 0, 1);
1607 binder_node_inner_unlock(node);
1609 binder_free_node(node);
1612 static void binder_put_node(struct binder_node *node)
1614 binder_dec_node_tmpref(node);
1617 static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc,
1618 u32 desc, bool need_strong_ref)
1620 struct rb_node *n = proc->refs_by_desc.rb_node;
1621 struct binder_ref *ref;
1624 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1626 if (desc < ref->data.desc) {
1628 } else if (desc > ref->data.desc) {
1630 } else if (need_strong_ref && !ref->data.strong) {
1631 binder_user_error("tried to use weak ref as strong ref\n");
1641 * binder_get_ref_for_node_olocked() - get the ref associated with given node
1642 * @proc: binder_proc that owns the ref
1643 * @node: binder_node of target
1644 * @new_ref: newly allocated binder_ref to be initialized or %NULL
1646 * Look up the ref for the given node and return it if it exists
1648 * If it doesn't exist and the caller provides a newly allocated
1649 * ref, initialize the fields of the newly allocated ref and insert
1650 * into the given proc rb_trees and node refs list.
1652 * Return: the ref for node. It is possible that another thread
1653 * allocated/initialized the ref first in which case the
1654 * returned ref would be different than the passed-in
1655 * new_ref. new_ref must be kfree'd by the caller in
1658 static struct binder_ref *binder_get_ref_for_node_olocked(
1659 struct binder_proc *proc,
1660 struct binder_node *node,
1661 struct binder_ref *new_ref)
1663 struct binder_context *context = proc->context;
1664 struct rb_node **p = &proc->refs_by_node.rb_node;
1665 struct rb_node *parent = NULL;
1666 struct binder_ref *ref;
1671 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1673 if (node < ref->node)
1675 else if (node > ref->node)
1676 p = &(*p)->rb_right;
1683 binder_stats_created(BINDER_STAT_REF);
1684 new_ref->data.debug_id = atomic_inc_return(&binder_last_id);
1685 new_ref->proc = proc;
1686 new_ref->node = node;
1687 rb_link_node(&new_ref->rb_node_node, parent, p);
1688 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1690 new_ref->data.desc = (node == context->binder_context_mgr_node) ? 0 : 1;
1691 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1692 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1693 if (ref->data.desc > new_ref->data.desc)
1695 new_ref->data.desc = ref->data.desc + 1;
1698 p = &proc->refs_by_desc.rb_node;
1701 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1703 if (new_ref->data.desc < ref->data.desc)
1705 else if (new_ref->data.desc > ref->data.desc)
1706 p = &(*p)->rb_right;
1710 rb_link_node(&new_ref->rb_node_desc, parent, p);
1711 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
1713 binder_node_lock(node);
1714 hlist_add_head(&new_ref->node_entry, &node->refs);
1716 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1717 "%d new ref %d desc %d for node %d\n",
1718 proc->pid, new_ref->data.debug_id, new_ref->data.desc,
1720 binder_node_unlock(node);
1724 static void binder_cleanup_ref_olocked(struct binder_ref *ref)
1726 bool delete_node = false;
1728 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1729 "%d delete ref %d desc %d for node %d\n",
1730 ref->proc->pid, ref->data.debug_id, ref->data.desc,
1731 ref->node->debug_id);
1733 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1734 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
1736 binder_node_inner_lock(ref->node);
1737 if (ref->data.strong)
1738 binder_dec_node_nilocked(ref->node, 1, 1);
1740 hlist_del(&ref->node_entry);
1741 delete_node = binder_dec_node_nilocked(ref->node, 0, 1);
1742 binder_node_inner_unlock(ref->node);
1744 * Clear ref->node unless we want the caller to free the node
1748 * The caller uses ref->node to determine
1749 * whether the node needs to be freed. Clear
1750 * it since the node is still alive.
1756 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1757 "%d delete ref %d desc %d has death notification\n",
1758 ref->proc->pid, ref->data.debug_id,
1760 binder_dequeue_work(ref->proc, &ref->death->work);
1761 binder_stats_deleted(BINDER_STAT_DEATH);
1763 binder_stats_deleted(BINDER_STAT_REF);
1767 * binder_inc_ref_olocked() - increment the ref for given handle
1768 * @ref: ref to be incremented
1769 * @strong: if true, strong increment, else weak
1770 * @target_list: list to queue node work on
1772 * Increment the ref. @ref->proc->outer_lock must be held on entry
1774 * Return: 0, if successful, else errno
1776 static int binder_inc_ref_olocked(struct binder_ref *ref, int strong,
1777 struct list_head *target_list)
1782 if (ref->data.strong == 0) {
1783 ret = binder_inc_node(ref->node, 1, 1, target_list);
1789 if (ref->data.weak == 0) {
1790 ret = binder_inc_node(ref->node, 0, 1, target_list);
1800 * binder_dec_ref() - dec the ref for given handle
1801 * @ref: ref to be decremented
1802 * @strong: if true, strong decrement, else weak
1804 * Decrement the ref.
1806 * Return: true if ref is cleaned up and ready to be freed
1808 static bool binder_dec_ref_olocked(struct binder_ref *ref, int strong)
1811 if (ref->data.strong == 0) {
1812 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
1813 ref->proc->pid, ref->data.debug_id,
1814 ref->data.desc, ref->data.strong,
1819 if (ref->data.strong == 0)
1820 binder_dec_node(ref->node, strong, 1);
1822 if (ref->data.weak == 0) {
1823 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
1824 ref->proc->pid, ref->data.debug_id,
1825 ref->data.desc, ref->data.strong,
1831 if (ref->data.strong == 0 && ref->data.weak == 0) {
1832 binder_cleanup_ref_olocked(ref);
1839 * binder_get_node_from_ref() - get the node from the given proc/desc
1840 * @proc: proc containing the ref
1841 * @desc: the handle associated with the ref
1842 * @need_strong_ref: if true, only return node if ref is strong
1843 * @rdata: the id/refcount data for the ref
1845 * Given a proc and ref handle, return the associated binder_node
1847 * Return: a binder_node or NULL if not found or not strong when strong required
1849 static struct binder_node *binder_get_node_from_ref(
1850 struct binder_proc *proc,
1851 u32 desc, bool need_strong_ref,
1852 struct binder_ref_data *rdata)
1854 struct binder_node *node;
1855 struct binder_ref *ref;
1857 binder_proc_lock(proc);
1858 ref = binder_get_ref_olocked(proc, desc, need_strong_ref);
1863 * Take an implicit reference on the node to ensure
1864 * it stays alive until the call to binder_put_node()
1866 binder_inc_node_tmpref(node);
1869 binder_proc_unlock(proc);
1874 binder_proc_unlock(proc);
1879 * binder_free_ref() - free the binder_ref
1882 * Free the binder_ref. Free the binder_node indicated by ref->node
1883 * (if non-NULL) and the binder_ref_death indicated by ref->death.
1885 static void binder_free_ref(struct binder_ref *ref)
1888 binder_free_node(ref->node);
1894 * binder_update_ref_for_handle() - inc/dec the ref for given handle
1895 * @proc: proc containing the ref
1896 * @desc: the handle associated with the ref
1897 * @increment: true=inc reference, false=dec reference
1898 * @strong: true=strong reference, false=weak reference
1899 * @rdata: the id/refcount data for the ref
1901 * Given a proc and ref handle, increment or decrement the ref
1902 * according to "increment" arg.
1904 * Return: 0 if successful, else errno
1906 static int binder_update_ref_for_handle(struct binder_proc *proc,
1907 uint32_t desc, bool increment, bool strong,
1908 struct binder_ref_data *rdata)
1911 struct binder_ref *ref;
1912 bool delete_ref = false;
1914 binder_proc_lock(proc);
1915 ref = binder_get_ref_olocked(proc, desc, strong);
1921 ret = binder_inc_ref_olocked(ref, strong, NULL);
1923 delete_ref = binder_dec_ref_olocked(ref, strong);
1927 binder_proc_unlock(proc);
1930 binder_free_ref(ref);
1934 binder_proc_unlock(proc);
1939 * binder_dec_ref_for_handle() - dec the ref for given handle
1940 * @proc: proc containing the ref
1941 * @desc: the handle associated with the ref
1942 * @strong: true=strong reference, false=weak reference
1943 * @rdata: the id/refcount data for the ref
1945 * Just calls binder_update_ref_for_handle() to decrement the ref.
1947 * Return: 0 if successful, else errno
1949 static int binder_dec_ref_for_handle(struct binder_proc *proc,
1950 uint32_t desc, bool strong, struct binder_ref_data *rdata)
1952 return binder_update_ref_for_handle(proc, desc, false, strong, rdata);
1957 * binder_inc_ref_for_node() - increment the ref for given proc/node
1958 * @proc: proc containing the ref
1959 * @node: target node
1960 * @strong: true=strong reference, false=weak reference
1961 * @target_list: worklist to use if node is incremented
1962 * @rdata: the id/refcount data for the ref
1964 * Given a proc and node, increment the ref. Create the ref if it
1965 * doesn't already exist
1967 * Return: 0 if successful, else errno
1969 static int binder_inc_ref_for_node(struct binder_proc *proc,
1970 struct binder_node *node,
1972 struct list_head *target_list,
1973 struct binder_ref_data *rdata)
1975 struct binder_ref *ref;
1976 struct binder_ref *new_ref = NULL;
1979 binder_proc_lock(proc);
1980 ref = binder_get_ref_for_node_olocked(proc, node, NULL);
1982 binder_proc_unlock(proc);
1983 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1986 binder_proc_lock(proc);
1987 ref = binder_get_ref_for_node_olocked(proc, node, new_ref);
1989 ret = binder_inc_ref_olocked(ref, strong, target_list);
1991 binder_proc_unlock(proc);
1992 if (new_ref && ref != new_ref)
1994 * Another thread created the ref first so
1995 * free the one we allocated
2001 static void binder_pop_transaction_ilocked(struct binder_thread *target_thread,
2002 struct binder_transaction *t)
2004 BUG_ON(!target_thread);
2005 assert_spin_locked(&target_thread->proc->inner_lock);
2006 BUG_ON(target_thread->transaction_stack != t);
2007 BUG_ON(target_thread->transaction_stack->from != target_thread);
2008 target_thread->transaction_stack =
2009 target_thread->transaction_stack->from_parent;
2014 * binder_thread_dec_tmpref() - decrement thread->tmp_ref
2015 * @thread: thread to decrement
2017 * A thread needs to be kept alive while being used to create or
2018 * handle a transaction. binder_get_txn_from() is used to safely
2019 * extract t->from from a binder_transaction and keep the thread
2020 * indicated by t->from from being freed. When done with that
2021 * binder_thread, this function is called to decrement the
2022 * tmp_ref and free if appropriate (thread has been released
2023 * and no transaction being processed by the driver)
2025 static void binder_thread_dec_tmpref(struct binder_thread *thread)
2028 * atomic is used to protect the counter value while
2029 * it cannot reach zero or thread->is_dead is false
2031 binder_inner_proc_lock(thread->proc);
2032 atomic_dec(&thread->tmp_ref);
2033 if (thread->is_dead && !atomic_read(&thread->tmp_ref)) {
2034 binder_inner_proc_unlock(thread->proc);
2035 binder_free_thread(thread);
2038 binder_inner_proc_unlock(thread->proc);
2042 * binder_proc_dec_tmpref() - decrement proc->tmp_ref
2043 * @proc: proc to decrement
2045 * A binder_proc needs to be kept alive while being used to create or
2046 * handle a transaction. proc->tmp_ref is incremented when
2047 * creating a new transaction or the binder_proc is currently in-use
2048 * by threads that are being released. When done with the binder_proc,
2049 * this function is called to decrement the counter and free the
2050 * proc if appropriate (proc has been released, all threads have
2051 * been released and not currenly in-use to process a transaction).
2053 static void binder_proc_dec_tmpref(struct binder_proc *proc)
2055 binder_inner_proc_lock(proc);
2057 if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) &&
2059 binder_inner_proc_unlock(proc);
2060 binder_free_proc(proc);
2063 binder_inner_proc_unlock(proc);
2067 * binder_get_txn_from() - safely extract the "from" thread in transaction
2068 * @t: binder transaction for t->from
2070 * Atomically return the "from" thread and increment the tmp_ref
2071 * count for the thread to ensure it stays alive until
2072 * binder_thread_dec_tmpref() is called.
2074 * Return: the value of t->from
2076 static struct binder_thread *binder_get_txn_from(
2077 struct binder_transaction *t)
2079 struct binder_thread *from;
2081 spin_lock(&t->lock);
2084 atomic_inc(&from->tmp_ref);
2085 spin_unlock(&t->lock);
2090 * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock
2091 * @t: binder transaction for t->from
2093 * Same as binder_get_txn_from() except it also acquires the proc->inner_lock
2094 * to guarantee that the thread cannot be released while operating on it.
2095 * The caller must call binder_inner_proc_unlock() to release the inner lock
2096 * as well as call binder_dec_thread_txn() to release the reference.
2098 * Return: the value of t->from
2100 static struct binder_thread *binder_get_txn_from_and_acq_inner(
2101 struct binder_transaction *t)
2103 struct binder_thread *from;
2105 from = binder_get_txn_from(t);
2108 binder_inner_proc_lock(from->proc);
2110 BUG_ON(from != t->from);
2113 binder_inner_proc_unlock(from->proc);
2114 binder_thread_dec_tmpref(from);
2118 static void binder_free_transaction(struct binder_transaction *t)
2121 t->buffer->transaction = NULL;
2123 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2126 static void binder_send_failed_reply(struct binder_transaction *t,
2127 uint32_t error_code)
2129 struct binder_thread *target_thread;
2130 struct binder_transaction *next;
2132 BUG_ON(t->flags & TF_ONE_WAY);
2134 target_thread = binder_get_txn_from_and_acq_inner(t);
2135 if (target_thread) {
2136 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2137 "send failed reply for transaction %d to %d:%d\n",
2139 target_thread->proc->pid,
2140 target_thread->pid);
2142 binder_pop_transaction_ilocked(target_thread, t);
2143 if (target_thread->reply_error.cmd == BR_OK) {
2144 target_thread->reply_error.cmd = error_code;
2145 binder_enqueue_thread_work_ilocked(
2147 &target_thread->reply_error.work);
2148 wake_up_interruptible(&target_thread->wait);
2151 * Cannot get here for normal operation, but
2152 * we can if multiple synchronous transactions
2153 * are sent without blocking for responses.
2154 * Just ignore the 2nd error in this case.
2156 pr_warn("Unexpected reply error: %u\n",
2157 target_thread->reply_error.cmd);
2159 binder_inner_proc_unlock(target_thread->proc);
2160 binder_thread_dec_tmpref(target_thread);
2161 binder_free_transaction(t);
2164 next = t->from_parent;
2166 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2167 "send failed reply for transaction %d, target dead\n",
2170 binder_free_transaction(t);
2172 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2173 "reply failed, no target thread at root\n");
2177 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2178 "reply failed, no target thread -- retry %d\n",
2184 * binder_cleanup_transaction() - cleans up undelivered transaction
2185 * @t: transaction that needs to be cleaned up
2186 * @reason: reason the transaction wasn't delivered
2187 * @error_code: error to return to caller (if synchronous call)
2189 static void binder_cleanup_transaction(struct binder_transaction *t,
2191 uint32_t error_code)
2193 if (t->buffer->target_node && !(t->flags & TF_ONE_WAY)) {
2194 binder_send_failed_reply(t, error_code);
2196 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2197 "undelivered transaction %d, %s\n",
2198 t->debug_id, reason);
2199 binder_free_transaction(t);
2204 * binder_validate_object() - checks for a valid metadata object in a buffer.
2205 * @buffer: binder_buffer that we're parsing.
2206 * @offset: offset in the buffer at which to validate an object.
2208 * Return: If there's a valid metadata object at @offset in @buffer, the
2209 * size of that object. Otherwise, it returns zero.
2211 static size_t binder_validate_object(struct binder_buffer *buffer, u64 offset)
2213 /* Check if we can read a header first */
2214 struct binder_object_header *hdr;
2215 size_t object_size = 0;
2217 if (buffer->data_size < sizeof(*hdr) ||
2218 offset > buffer->data_size - sizeof(*hdr) ||
2219 !IS_ALIGNED(offset, sizeof(u32)))
2222 /* Ok, now see if we can read a complete object. */
2223 hdr = (struct binder_object_header *)(buffer->data + offset);
2224 switch (hdr->type) {
2225 case BINDER_TYPE_BINDER:
2226 case BINDER_TYPE_WEAK_BINDER:
2227 case BINDER_TYPE_HANDLE:
2228 case BINDER_TYPE_WEAK_HANDLE:
2229 object_size = sizeof(struct flat_binder_object);
2231 case BINDER_TYPE_FD:
2232 object_size = sizeof(struct binder_fd_object);
2234 case BINDER_TYPE_PTR:
2235 object_size = sizeof(struct binder_buffer_object);
2237 case BINDER_TYPE_FDA:
2238 object_size = sizeof(struct binder_fd_array_object);
2243 if (offset <= buffer->data_size - object_size &&
2244 buffer->data_size >= object_size)
2251 * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
2252 * @b: binder_buffer containing the object
2253 * @index: index in offset array at which the binder_buffer_object is
2255 * @start: points to the start of the offset array
2256 * @num_valid: the number of valid offsets in the offset array
2258 * Return: If @index is within the valid range of the offset array
2259 * described by @start and @num_valid, and if there's a valid
2260 * binder_buffer_object at the offset found in index @index
2261 * of the offset array, that object is returned. Otherwise,
2262 * %NULL is returned.
2263 * Note that the offset found in index @index itself is not
2264 * verified; this function assumes that @num_valid elements
2265 * from @start were previously verified to have valid offsets.
2267 static struct binder_buffer_object *binder_validate_ptr(struct binder_buffer *b,
2268 binder_size_t index,
2269 binder_size_t *start,
2270 binder_size_t num_valid)
2272 struct binder_buffer_object *buffer_obj;
2273 binder_size_t *offp;
2275 if (index >= num_valid)
2278 offp = start + index;
2279 buffer_obj = (struct binder_buffer_object *)(b->data + *offp);
2280 if (buffer_obj->hdr.type != BINDER_TYPE_PTR)
2287 * binder_validate_fixup() - validates pointer/fd fixups happen in order.
2288 * @b: transaction buffer
2289 * @objects_start start of objects buffer
2290 * @buffer: binder_buffer_object in which to fix up
2291 * @offset: start offset in @buffer to fix up
2292 * @last_obj: last binder_buffer_object that we fixed up in
2293 * @last_min_offset: minimum fixup offset in @last_obj
2295 * Return: %true if a fixup in buffer @buffer at offset @offset is
2298 * For safety reasons, we only allow fixups inside a buffer to happen
2299 * at increasing offsets; additionally, we only allow fixup on the last
2300 * buffer object that was verified, or one of its parents.
2302 * Example of what is allowed:
2305 * B (parent = A, offset = 0)
2306 * C (parent = A, offset = 16)
2307 * D (parent = C, offset = 0)
2308 * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
2310 * Examples of what is not allowed:
2312 * Decreasing offsets within the same parent:
2314 * C (parent = A, offset = 16)
2315 * B (parent = A, offset = 0) // decreasing offset within A
2317 * Referring to a parent that wasn't the last object or any of its parents:
2319 * B (parent = A, offset = 0)
2320 * C (parent = A, offset = 0)
2321 * C (parent = A, offset = 16)
2322 * D (parent = B, offset = 0) // B is not A or any of A's parents
2324 static bool binder_validate_fixup(struct binder_buffer *b,
2325 binder_size_t *objects_start,
2326 struct binder_buffer_object *buffer,
2327 binder_size_t fixup_offset,
2328 struct binder_buffer_object *last_obj,
2329 binder_size_t last_min_offset)
2332 /* Nothing to fix up in */
2336 while (last_obj != buffer) {
2338 * Safe to retrieve the parent of last_obj, since it
2339 * was already previously verified by the driver.
2341 if ((last_obj->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0)
2343 last_min_offset = last_obj->parent_offset + sizeof(uintptr_t);
2344 last_obj = (struct binder_buffer_object *)
2345 (b->data + *(objects_start + last_obj->parent));
2347 return (fixup_offset >= last_min_offset);
2350 static void binder_transaction_buffer_release(struct binder_proc *proc,
2351 struct binder_buffer *buffer,
2352 binder_size_t *failed_at)
2354 binder_size_t *offp, *off_start, *off_end;
2355 int debug_id = buffer->debug_id;
2357 binder_debug(BINDER_DEBUG_TRANSACTION,
2358 "%d buffer release %d, size %zd-%zd, failed at %pK\n",
2359 proc->pid, buffer->debug_id,
2360 buffer->data_size, buffer->offsets_size, failed_at);
2362 if (buffer->target_node)
2363 binder_dec_node(buffer->target_node, 1, 0);
2365 off_start = (binder_size_t *)(buffer->data +
2366 ALIGN(buffer->data_size, sizeof(void *)));
2368 off_end = failed_at;
2370 off_end = (void *)off_start + buffer->offsets_size;
2371 for (offp = off_start; offp < off_end; offp++) {
2372 struct binder_object_header *hdr;
2373 size_t object_size = binder_validate_object(buffer, *offp);
2375 if (object_size == 0) {
2376 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
2377 debug_id, (u64)*offp, buffer->data_size);
2380 hdr = (struct binder_object_header *)(buffer->data + *offp);
2381 switch (hdr->type) {
2382 case BINDER_TYPE_BINDER:
2383 case BINDER_TYPE_WEAK_BINDER: {
2384 struct flat_binder_object *fp;
2385 struct binder_node *node;
2387 fp = to_flat_binder_object(hdr);
2388 node = binder_get_node(proc, fp->binder);
2390 pr_err("transaction release %d bad node %016llx\n",
2391 debug_id, (u64)fp->binder);
2394 binder_debug(BINDER_DEBUG_TRANSACTION,
2395 " node %d u%016llx\n",
2396 node->debug_id, (u64)node->ptr);
2397 binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER,
2399 binder_put_node(node);
2401 case BINDER_TYPE_HANDLE:
2402 case BINDER_TYPE_WEAK_HANDLE: {
2403 struct flat_binder_object *fp;
2404 struct binder_ref_data rdata;
2407 fp = to_flat_binder_object(hdr);
2408 ret = binder_dec_ref_for_handle(proc, fp->handle,
2409 hdr->type == BINDER_TYPE_HANDLE, &rdata);
2412 pr_err("transaction release %d bad handle %d, ret = %d\n",
2413 debug_id, fp->handle, ret);
2416 binder_debug(BINDER_DEBUG_TRANSACTION,
2417 " ref %d desc %d\n",
2418 rdata.debug_id, rdata.desc);
2421 case BINDER_TYPE_FD: {
2422 struct binder_fd_object *fp = to_binder_fd_object(hdr);
2424 binder_debug(BINDER_DEBUG_TRANSACTION,
2425 " fd %d\n", fp->fd);
2427 task_close_fd(proc, fp->fd);
2429 case BINDER_TYPE_PTR:
2431 * Nothing to do here, this will get cleaned up when the
2432 * transaction buffer gets freed
2435 case BINDER_TYPE_FDA: {
2436 struct binder_fd_array_object *fda;
2437 struct binder_buffer_object *parent;
2438 uintptr_t parent_buffer;
2441 binder_size_t fd_buf_size;
2443 fda = to_binder_fd_array_object(hdr);
2444 parent = binder_validate_ptr(buffer, fda->parent,
2448 pr_err("transaction release %d bad parent offset",
2453 * Since the parent was already fixed up, convert it
2454 * back to kernel address space to access it
2456 parent_buffer = parent->buffer -
2457 binder_alloc_get_user_buffer_offset(
2460 fd_buf_size = sizeof(u32) * fda->num_fds;
2461 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2462 pr_err("transaction release %d invalid number of fds (%lld)\n",
2463 debug_id, (u64)fda->num_fds);
2466 if (fd_buf_size > parent->length ||
2467 fda->parent_offset > parent->length - fd_buf_size) {
2468 /* No space for all file descriptors here. */
2469 pr_err("transaction release %d not enough space for %lld fds in buffer\n",
2470 debug_id, (u64)fda->num_fds);
2473 fd_array = (u32 *)(parent_buffer + (uintptr_t)fda->parent_offset);
2474 for (fd_index = 0; fd_index < fda->num_fds; fd_index++)
2475 task_close_fd(proc, fd_array[fd_index]);
2478 pr_err("transaction release %d bad object type %x\n",
2479 debug_id, hdr->type);
2485 static int binder_translate_binder(struct flat_binder_object *fp,
2486 struct binder_transaction *t,
2487 struct binder_thread *thread)
2489 struct binder_node *node;
2490 struct binder_proc *proc = thread->proc;
2491 struct binder_proc *target_proc = t->to_proc;
2492 struct binder_ref_data rdata;
2495 node = binder_get_node(proc, fp->binder);
2497 node = binder_new_node(proc, fp);
2501 if (fp->cookie != node->cookie) {
2502 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
2503 proc->pid, thread->pid, (u64)fp->binder,
2504 node->debug_id, (u64)fp->cookie,
2509 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2514 ret = binder_inc_ref_for_node(target_proc, node,
2515 fp->hdr.type == BINDER_TYPE_BINDER,
2516 &thread->todo, &rdata);
2520 if (fp->hdr.type == BINDER_TYPE_BINDER)
2521 fp->hdr.type = BINDER_TYPE_HANDLE;
2523 fp->hdr.type = BINDER_TYPE_WEAK_HANDLE;
2525 fp->handle = rdata.desc;
2528 trace_binder_transaction_node_to_ref(t, node, &rdata);
2529 binder_debug(BINDER_DEBUG_TRANSACTION,
2530 " node %d u%016llx -> ref %d desc %d\n",
2531 node->debug_id, (u64)node->ptr,
2532 rdata.debug_id, rdata.desc);
2534 binder_put_node(node);
2538 static int binder_translate_handle(struct flat_binder_object *fp,
2539 struct binder_transaction *t,
2540 struct binder_thread *thread)
2542 struct binder_proc *proc = thread->proc;
2543 struct binder_proc *target_proc = t->to_proc;
2544 struct binder_node *node;
2545 struct binder_ref_data src_rdata;
2548 node = binder_get_node_from_ref(proc, fp->handle,
2549 fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata);
2551 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
2552 proc->pid, thread->pid, fp->handle);
2555 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2560 binder_node_lock(node);
2561 if (node->proc == target_proc) {
2562 if (fp->hdr.type == BINDER_TYPE_HANDLE)
2563 fp->hdr.type = BINDER_TYPE_BINDER;
2565 fp->hdr.type = BINDER_TYPE_WEAK_BINDER;
2566 fp->binder = node->ptr;
2567 fp->cookie = node->cookie;
2569 binder_inner_proc_lock(node->proc);
2570 binder_inc_node_nilocked(node,
2571 fp->hdr.type == BINDER_TYPE_BINDER,
2574 binder_inner_proc_unlock(node->proc);
2575 trace_binder_transaction_ref_to_node(t, node, &src_rdata);
2576 binder_debug(BINDER_DEBUG_TRANSACTION,
2577 " ref %d desc %d -> node %d u%016llx\n",
2578 src_rdata.debug_id, src_rdata.desc, node->debug_id,
2580 binder_node_unlock(node);
2582 struct binder_ref_data dest_rdata;
2584 binder_node_unlock(node);
2585 ret = binder_inc_ref_for_node(target_proc, node,
2586 fp->hdr.type == BINDER_TYPE_HANDLE,
2592 fp->handle = dest_rdata.desc;
2594 trace_binder_transaction_ref_to_ref(t, node, &src_rdata,
2596 binder_debug(BINDER_DEBUG_TRANSACTION,
2597 " ref %d desc %d -> ref %d desc %d (node %d)\n",
2598 src_rdata.debug_id, src_rdata.desc,
2599 dest_rdata.debug_id, dest_rdata.desc,
2603 binder_put_node(node);
2607 static int binder_translate_fd(int fd,
2608 struct binder_transaction *t,
2609 struct binder_thread *thread,
2610 struct binder_transaction *in_reply_to)
2612 struct binder_proc *proc = thread->proc;
2613 struct binder_proc *target_proc = t->to_proc;
2617 bool target_allows_fd;
2620 target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS);
2622 target_allows_fd = t->buffer->target_node->accept_fds;
2623 if (!target_allows_fd) {
2624 binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
2625 proc->pid, thread->pid,
2626 in_reply_to ? "reply" : "transaction",
2629 goto err_fd_not_accepted;
2634 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
2635 proc->pid, thread->pid, fd);
2639 ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file);
2645 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
2646 if (target_fd < 0) {
2648 goto err_get_unused_fd;
2650 task_fd_install(target_proc, target_fd, file);
2651 trace_binder_transaction_fd(t, fd, target_fd);
2652 binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n",
2661 err_fd_not_accepted:
2665 static int binder_translate_fd_array(struct binder_fd_array_object *fda,
2666 struct binder_buffer_object *parent,
2667 struct binder_transaction *t,
2668 struct binder_thread *thread,
2669 struct binder_transaction *in_reply_to)
2671 binder_size_t fdi, fd_buf_size, num_installed_fds;
2673 uintptr_t parent_buffer;
2675 struct binder_proc *proc = thread->proc;
2676 struct binder_proc *target_proc = t->to_proc;
2678 fd_buf_size = sizeof(u32) * fda->num_fds;
2679 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2680 binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
2681 proc->pid, thread->pid, (u64)fda->num_fds);
2684 if (fd_buf_size > parent->length ||
2685 fda->parent_offset > parent->length - fd_buf_size) {
2686 /* No space for all file descriptors here. */
2687 binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
2688 proc->pid, thread->pid, (u64)fda->num_fds);
2692 * Since the parent was already fixed up, convert it
2693 * back to the kernel address space to access it
2695 parent_buffer = parent->buffer -
2696 binder_alloc_get_user_buffer_offset(&target_proc->alloc);
2697 fd_array = (u32 *)(parent_buffer + (uintptr_t)fda->parent_offset);
2698 if (!IS_ALIGNED((unsigned long)fd_array, sizeof(u32))) {
2699 binder_user_error("%d:%d parent offset not aligned correctly.\n",
2700 proc->pid, thread->pid);
2703 for (fdi = 0; fdi < fda->num_fds; fdi++) {
2704 target_fd = binder_translate_fd(fd_array[fdi], t, thread,
2707 goto err_translate_fd_failed;
2708 fd_array[fdi] = target_fd;
2712 err_translate_fd_failed:
2714 * Failed to allocate fd or security error, free fds
2717 num_installed_fds = fdi;
2718 for (fdi = 0; fdi < num_installed_fds; fdi++)
2719 task_close_fd(target_proc, fd_array[fdi]);
2723 static int binder_fixup_parent(struct binder_transaction *t,
2724 struct binder_thread *thread,
2725 struct binder_buffer_object *bp,
2726 binder_size_t *off_start,
2727 binder_size_t num_valid,
2728 struct binder_buffer_object *last_fixup_obj,
2729 binder_size_t last_fixup_min_off)
2731 struct binder_buffer_object *parent;
2733 struct binder_buffer *b = t->buffer;
2734 struct binder_proc *proc = thread->proc;
2735 struct binder_proc *target_proc = t->to_proc;
2737 if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT))
2740 parent = binder_validate_ptr(b, bp->parent, off_start, num_valid);
2742 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
2743 proc->pid, thread->pid);
2747 if (!binder_validate_fixup(b, off_start,
2748 parent, bp->parent_offset,
2750 last_fixup_min_off)) {
2751 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
2752 proc->pid, thread->pid);
2756 if (parent->length < sizeof(binder_uintptr_t) ||
2757 bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) {
2758 /* No space for a pointer here! */
2759 binder_user_error("%d:%d got transaction with invalid parent offset\n",
2760 proc->pid, thread->pid);
2763 parent_buffer = (u8 *)((uintptr_t)parent->buffer -
2764 binder_alloc_get_user_buffer_offset(
2765 &target_proc->alloc));
2766 *(binder_uintptr_t *)(parent_buffer + bp->parent_offset) = bp->buffer;
2772 * binder_proc_transaction() - sends a transaction to a process and wakes it up
2773 * @t: transaction to send
2774 * @proc: process to send the transaction to
2775 * @thread: thread in @proc to send the transaction to (may be NULL)
2777 * This function queues a transaction to the specified process. It will try
2778 * to find a thread in the target process to handle the transaction and
2779 * wake it up. If no thread is found, the work is queued to the proc
2782 * If the @thread parameter is not NULL, the transaction is always queued
2783 * to the waitlist of that specific thread.
2785 * Return: true if the transactions was successfully queued
2786 * false if the target process or thread is dead
2788 static bool binder_proc_transaction(struct binder_transaction *t,
2789 struct binder_proc *proc,
2790 struct binder_thread *thread)
2792 struct binder_node *node = t->buffer->target_node;
2793 struct binder_priority node_prio;
2794 bool oneway = !!(t->flags & TF_ONE_WAY);
2795 bool pending_async = false;
2798 binder_node_lock(node);
2799 node_prio.prio = node->min_priority;
2800 node_prio.sched_policy = node->sched_policy;
2804 if (node->has_async_transaction) {
2805 pending_async = true;
2807 node->has_async_transaction = true;
2811 binder_inner_proc_lock(proc);
2813 if (proc->is_dead || (thread && thread->is_dead)) {
2814 binder_inner_proc_unlock(proc);
2815 binder_node_unlock(node);
2819 if (!thread && !pending_async)
2820 thread = binder_select_thread_ilocked(proc);
2823 binder_transaction_priority(thread->task, t, node_prio,
2825 binder_enqueue_thread_work_ilocked(thread, &t->work);
2826 } else if (!pending_async) {
2827 binder_enqueue_work_ilocked(&t->work, &proc->todo);
2829 binder_enqueue_work_ilocked(&t->work, &node->async_todo);
2833 binder_wakeup_thread_ilocked(proc, thread, !oneway /* sync */);
2835 binder_inner_proc_unlock(proc);
2836 binder_node_unlock(node);
2842 * binder_get_node_refs_for_txn() - Get required refs on node for txn
2843 * @node: struct binder_node for which to get refs
2844 * @proc: returns @node->proc if valid
2845 * @error: if no @proc then returns BR_DEAD_REPLY
2847 * User-space normally keeps the node alive when creating a transaction
2848 * since it has a reference to the target. The local strong ref keeps it
2849 * alive if the sending process dies before the target process processes
2850 * the transaction. If the source process is malicious or has a reference
2851 * counting bug, relying on the local strong ref can fail.
2853 * Since user-space can cause the local strong ref to go away, we also take
2854 * a tmpref on the node to ensure it survives while we are constructing
2855 * the transaction. We also need a tmpref on the proc while we are
2856 * constructing the transaction, so we take that here as well.
2858 * Return: The target_node with refs taken or NULL if no @node->proc is NULL.
2859 * Also sets @proc if valid. If the @node->proc is NULL indicating that the
2860 * target proc has died, @error is set to BR_DEAD_REPLY
2862 static struct binder_node *binder_get_node_refs_for_txn(
2863 struct binder_node *node,
2864 struct binder_proc **procp,
2867 struct binder_node *target_node = NULL;
2869 binder_node_inner_lock(node);
2872 binder_inc_node_nilocked(node, 1, 0, NULL);
2873 binder_inc_node_tmpref_ilocked(node);
2874 node->proc->tmp_ref++;
2875 *procp = node->proc;
2877 *error = BR_DEAD_REPLY;
2878 binder_node_inner_unlock(node);
2883 static void binder_transaction(struct binder_proc *proc,
2884 struct binder_thread *thread,
2885 struct binder_transaction_data *tr, int reply,
2886 binder_size_t extra_buffers_size)
2889 struct binder_transaction *t;
2890 struct binder_work *tcomplete;
2891 binder_size_t *offp, *off_end, *off_start;
2892 binder_size_t off_min;
2893 u8 *sg_bufp, *sg_buf_end;
2894 struct binder_proc *target_proc = NULL;
2895 struct binder_thread *target_thread = NULL;
2896 struct binder_node *target_node = NULL;
2897 struct binder_transaction *in_reply_to = NULL;
2898 struct binder_transaction_log_entry *e;
2899 uint32_t return_error = 0;
2900 uint32_t return_error_param = 0;
2901 uint32_t return_error_line = 0;
2902 struct binder_buffer_object *last_fixup_obj = NULL;
2903 binder_size_t last_fixup_min_off = 0;
2904 struct binder_context *context = proc->context;
2905 int t_debug_id = atomic_inc_return(&binder_last_id);
2906 char *secctx = NULL;
2909 e = binder_transaction_log_add(&binder_transaction_log);
2910 e->debug_id = t_debug_id;
2911 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
2912 e->from_proc = proc->pid;
2913 e->from_thread = thread->pid;
2914 e->target_handle = tr->target.handle;
2915 e->data_size = tr->data_size;
2916 e->offsets_size = tr->offsets_size;
2917 e->context_name = proc->context->name;
2920 binder_inner_proc_lock(proc);
2921 in_reply_to = thread->transaction_stack;
2922 if (in_reply_to == NULL) {
2923 binder_inner_proc_unlock(proc);
2924 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
2925 proc->pid, thread->pid);
2926 return_error = BR_FAILED_REPLY;
2927 return_error_param = -EPROTO;
2928 return_error_line = __LINE__;
2929 goto err_empty_call_stack;
2931 if (in_reply_to->to_thread != thread) {
2932 spin_lock(&in_reply_to->lock);
2933 binder_user_error("%d:%d got reply transaction with bad transaction stack, transaction %d has target %d:%d\n",
2934 proc->pid, thread->pid, in_reply_to->debug_id,
2935 in_reply_to->to_proc ?
2936 in_reply_to->to_proc->pid : 0,
2937 in_reply_to->to_thread ?
2938 in_reply_to->to_thread->pid : 0);
2939 spin_unlock(&in_reply_to->lock);
2940 binder_inner_proc_unlock(proc);
2941 return_error = BR_FAILED_REPLY;
2942 return_error_param = -EPROTO;
2943 return_error_line = __LINE__;
2945 goto err_bad_call_stack;
2947 thread->transaction_stack = in_reply_to->to_parent;
2948 binder_inner_proc_unlock(proc);
2949 target_thread = binder_get_txn_from_and_acq_inner(in_reply_to);
2950 if (target_thread == NULL) {
2951 return_error = BR_DEAD_REPLY;
2952 return_error_line = __LINE__;
2953 goto err_dead_binder;
2955 if (target_thread->transaction_stack != in_reply_to) {
2956 binder_user_error("%d:%d got reply transaction with bad target transaction stack %d, expected %d\n",
2957 proc->pid, thread->pid,
2958 target_thread->transaction_stack ?
2959 target_thread->transaction_stack->debug_id : 0,
2960 in_reply_to->debug_id);
2961 binder_inner_proc_unlock(target_thread->proc);
2962 return_error = BR_FAILED_REPLY;
2963 return_error_param = -EPROTO;
2964 return_error_line = __LINE__;
2966 target_thread = NULL;
2967 goto err_dead_binder;
2969 target_proc = target_thread->proc;
2970 target_proc->tmp_ref++;
2971 binder_inner_proc_unlock(target_thread->proc);
2973 if (tr->target.handle) {
2974 struct binder_ref *ref;
2977 * There must already be a strong ref
2978 * on this node. If so, do a strong
2979 * increment on the node to ensure it
2980 * stays alive until the transaction is
2983 binder_proc_lock(proc);
2984 ref = binder_get_ref_olocked(proc, tr->target.handle,
2987 target_node = binder_get_node_refs_for_txn(
2988 ref->node, &target_proc,
2991 binder_user_error("%d:%d got transaction to invalid handle\n",
2992 proc->pid, thread->pid);
2993 return_error = BR_FAILED_REPLY;
2995 binder_proc_unlock(proc);
2997 mutex_lock(&context->context_mgr_node_lock);
2998 target_node = context->binder_context_mgr_node;
3000 target_node = binder_get_node_refs_for_txn(
3001 target_node, &target_proc,
3004 return_error = BR_DEAD_REPLY;
3005 mutex_unlock(&context->context_mgr_node_lock);
3006 if (target_node && target_proc == proc) {
3007 binder_user_error("%d:%d got transaction to context manager from process owning it\n",
3008 proc->pid, thread->pid);
3009 return_error = BR_FAILED_REPLY;
3010 return_error_param = -EINVAL;
3011 return_error_line = __LINE__;
3012 goto err_invalid_target_handle;
3017 * return_error is set above
3019 return_error_param = -EINVAL;
3020 return_error_line = __LINE__;
3021 goto err_dead_binder;
3023 e->to_node = target_node->debug_id;
3024 if (security_binder_transaction(proc->tsk,
3025 target_proc->tsk) < 0) {
3026 return_error = BR_FAILED_REPLY;
3027 return_error_param = -EPERM;
3028 return_error_line = __LINE__;
3029 goto err_invalid_target_handle;
3031 binder_inner_proc_lock(proc);
3032 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
3033 struct binder_transaction *tmp;
3035 tmp = thread->transaction_stack;
3036 if (tmp->to_thread != thread) {
3037 spin_lock(&tmp->lock);
3038 binder_user_error("%d:%d got new transaction with bad transaction stack, transaction %d has target %d:%d\n",
3039 proc->pid, thread->pid, tmp->debug_id,
3040 tmp->to_proc ? tmp->to_proc->pid : 0,
3042 tmp->to_thread->pid : 0);
3043 spin_unlock(&tmp->lock);
3044 binder_inner_proc_unlock(proc);
3045 return_error = BR_FAILED_REPLY;
3046 return_error_param = -EPROTO;
3047 return_error_line = __LINE__;
3048 goto err_bad_call_stack;
3051 struct binder_thread *from;
3053 spin_lock(&tmp->lock);
3055 if (from && from->proc == target_proc) {
3056 atomic_inc(&from->tmp_ref);
3057 target_thread = from;
3058 spin_unlock(&tmp->lock);
3061 spin_unlock(&tmp->lock);
3062 tmp = tmp->from_parent;
3065 binder_inner_proc_unlock(proc);
3068 e->to_thread = target_thread->pid;
3069 e->to_proc = target_proc->pid;
3071 /* TODO: reuse incoming transaction for reply */
3072 t = kzalloc(sizeof(*t), GFP_KERNEL);
3074 return_error = BR_FAILED_REPLY;
3075 return_error_param = -ENOMEM;
3076 return_error_line = __LINE__;
3077 goto err_alloc_t_failed;
3079 binder_stats_created(BINDER_STAT_TRANSACTION);
3080 spin_lock_init(&t->lock);
3082 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
3083 if (tcomplete == NULL) {
3084 return_error = BR_FAILED_REPLY;
3085 return_error_param = -ENOMEM;
3086 return_error_line = __LINE__;
3087 goto err_alloc_tcomplete_failed;
3089 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
3091 t->debug_id = t_debug_id;
3094 binder_debug(BINDER_DEBUG_TRANSACTION,
3095 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
3096 proc->pid, thread->pid, t->debug_id,
3097 target_proc->pid, target_thread->pid,
3098 (u64)tr->data.ptr.buffer,
3099 (u64)tr->data.ptr.offsets,
3100 (u64)tr->data_size, (u64)tr->offsets_size,
3101 (u64)extra_buffers_size);
3103 binder_debug(BINDER_DEBUG_TRANSACTION,
3104 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
3105 proc->pid, thread->pid, t->debug_id,
3106 target_proc->pid, target_node->debug_id,
3107 (u64)tr->data.ptr.buffer,
3108 (u64)tr->data.ptr.offsets,
3109 (u64)tr->data_size, (u64)tr->offsets_size,
3110 (u64)extra_buffers_size);
3112 if (!reply && !(tr->flags & TF_ONE_WAY))
3116 t->sender_euid = task_euid(proc->tsk);
3117 t->to_proc = target_proc;
3118 t->to_thread = target_thread;
3120 t->flags = tr->flags;
3121 if (!(t->flags & TF_ONE_WAY) &&
3122 binder_supported_policy(current->policy)) {
3123 /* Inherit supported policies for synchronous transactions */
3124 t->priority.sched_policy = current->policy;
3125 t->priority.prio = current->normal_prio;
3127 /* Otherwise, fall back to the default priority */
3128 t->priority = target_proc->default_priority;
3131 if (target_node && target_node->txn_security_ctx) {
3135 security_task_getsecid(proc->tsk, &secid);
3136 ret = security_secid_to_secctx(secid, &secctx, &secctx_sz);
3138 return_error = BR_FAILED_REPLY;
3139 return_error_param = ret;
3140 return_error_line = __LINE__;
3141 goto err_get_secctx_failed;
3143 added_size = ALIGN(secctx_sz, sizeof(u64));
3144 extra_buffers_size += added_size;
3145 if (extra_buffers_size < added_size) {
3146 /* integer overflow of extra_buffers_size */
3147 return_error = BR_FAILED_REPLY;
3148 return_error_param = EINVAL;
3149 return_error_line = __LINE__;
3150 goto err_bad_extra_size;
3154 trace_binder_transaction(reply, t, target_node);
3156 t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size,
3157 tr->offsets_size, extra_buffers_size,
3158 !reply && (t->flags & TF_ONE_WAY));
3159 if (IS_ERR(t->buffer)) {
3161 * -ESRCH indicates VMA cleared. The target is dying.
3163 return_error_param = PTR_ERR(t->buffer);
3164 return_error = return_error_param == -ESRCH ?
3165 BR_DEAD_REPLY : BR_FAILED_REPLY;
3166 return_error_line = __LINE__;
3168 goto err_binder_alloc_buf_failed;
3171 size_t buf_offset = ALIGN(tr->data_size, sizeof(void *)) +
3172 ALIGN(tr->offsets_size, sizeof(void *)) +
3173 ALIGN(extra_buffers_size, sizeof(void *)) -
3174 ALIGN(secctx_sz, sizeof(u64));
3175 char *kptr = t->buffer->data + buf_offset;
3177 t->security_ctx = (uintptr_t)kptr +
3178 binder_alloc_get_user_buffer_offset(&target_proc->alloc);
3179 memcpy(kptr, secctx, secctx_sz);
3180 security_release_secctx(secctx, secctx_sz);
3183 t->buffer->debug_id = t->debug_id;
3184 t->buffer->transaction = t;
3185 t->buffer->target_node = target_node;
3186 trace_binder_transaction_alloc_buf(t->buffer);
3187 off_start = (binder_size_t *)(t->buffer->data +
3188 ALIGN(tr->data_size, sizeof(void *)));
3191 if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t)
3192 tr->data.ptr.buffer, tr->data_size)) {
3193 binder_user_error("%d:%d got transaction with invalid data ptr\n",
3194 proc->pid, thread->pid);
3195 return_error = BR_FAILED_REPLY;
3196 return_error_param = -EFAULT;
3197 return_error_line = __LINE__;
3198 goto err_copy_data_failed;
3200 if (copy_from_user(offp, (const void __user *)(uintptr_t)
3201 tr->data.ptr.offsets, tr->offsets_size)) {
3202 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3203 proc->pid, thread->pid);
3204 return_error = BR_FAILED_REPLY;
3205 return_error_param = -EFAULT;
3206 return_error_line = __LINE__;
3207 goto err_copy_data_failed;
3209 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
3210 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
3211 proc->pid, thread->pid, (u64)tr->offsets_size);
3212 return_error = BR_FAILED_REPLY;
3213 return_error_param = -EINVAL;
3214 return_error_line = __LINE__;
3215 goto err_bad_offset;
3217 if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) {
3218 binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
3219 proc->pid, thread->pid,
3220 (u64)extra_buffers_size);
3221 return_error = BR_FAILED_REPLY;
3222 return_error_param = -EINVAL;
3223 return_error_line = __LINE__;
3224 goto err_bad_offset;
3226 off_end = (void *)off_start + tr->offsets_size;
3227 sg_bufp = (u8 *)(PTR_ALIGN(off_end, sizeof(void *)));
3228 sg_buf_end = sg_bufp + extra_buffers_size -
3229 ALIGN(secctx_sz, sizeof(u64));
3231 for (; offp < off_end; offp++) {
3232 struct binder_object_header *hdr;
3233 size_t object_size = binder_validate_object(t->buffer, *offp);
3235 if (object_size == 0 || *offp < off_min) {
3236 binder_user_error("%d:%d got transaction with invalid offset (%lld, min %lld max %lld) or object.\n",
3237 proc->pid, thread->pid, (u64)*offp,
3239 (u64)t->buffer->data_size);
3240 return_error = BR_FAILED_REPLY;
3241 return_error_param = -EINVAL;
3242 return_error_line = __LINE__;
3243 goto err_bad_offset;
3246 hdr = (struct binder_object_header *)(t->buffer->data + *offp);
3247 off_min = *offp + object_size;
3248 switch (hdr->type) {
3249 case BINDER_TYPE_BINDER:
3250 case BINDER_TYPE_WEAK_BINDER: {
3251 struct flat_binder_object *fp;
3253 fp = to_flat_binder_object(hdr);
3254 ret = binder_translate_binder(fp, t, thread);
3256 return_error = BR_FAILED_REPLY;
3257 return_error_param = ret;
3258 return_error_line = __LINE__;
3259 goto err_translate_failed;
3262 case BINDER_TYPE_HANDLE:
3263 case BINDER_TYPE_WEAK_HANDLE: {
3264 struct flat_binder_object *fp;
3266 fp = to_flat_binder_object(hdr);
3267 ret = binder_translate_handle(fp, t, thread);
3269 return_error = BR_FAILED_REPLY;
3270 return_error_param = ret;
3271 return_error_line = __LINE__;
3272 goto err_translate_failed;
3276 case BINDER_TYPE_FD: {
3277 struct binder_fd_object *fp = to_binder_fd_object(hdr);
3278 int target_fd = binder_translate_fd(fp->fd, t, thread,
3281 if (target_fd < 0) {
3282 return_error = BR_FAILED_REPLY;
3283 return_error_param = target_fd;
3284 return_error_line = __LINE__;
3285 goto err_translate_failed;
3290 case BINDER_TYPE_FDA: {
3291 struct binder_fd_array_object *fda =
3292 to_binder_fd_array_object(hdr);
3293 struct binder_buffer_object *parent =
3294 binder_validate_ptr(t->buffer, fda->parent,
3298 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
3299 proc->pid, thread->pid);
3300 return_error = BR_FAILED_REPLY;
3301 return_error_param = -EINVAL;
3302 return_error_line = __LINE__;
3303 goto err_bad_parent;
3305 if (!binder_validate_fixup(t->buffer, off_start,
3306 parent, fda->parent_offset,
3308 last_fixup_min_off)) {
3309 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
3310 proc->pid, thread->pid);
3311 return_error = BR_FAILED_REPLY;
3312 return_error_param = -EINVAL;
3313 return_error_line = __LINE__;
3314 goto err_bad_parent;
3316 ret = binder_translate_fd_array(fda, parent, t, thread,
3319 return_error = BR_FAILED_REPLY;
3320 return_error_param = ret;
3321 return_error_line = __LINE__;
3322 goto err_translate_failed;
3324 last_fixup_obj = parent;
3325 last_fixup_min_off =
3326 fda->parent_offset + sizeof(u32) * fda->num_fds;
3328 case BINDER_TYPE_PTR: {
3329 struct binder_buffer_object *bp =
3330 to_binder_buffer_object(hdr);
3331 size_t buf_left = sg_buf_end - sg_bufp;
3333 if (bp->length > buf_left) {
3334 binder_user_error("%d:%d got transaction with too large buffer\n",
3335 proc->pid, thread->pid);
3336 return_error = BR_FAILED_REPLY;
3337 return_error_param = -EINVAL;
3338 return_error_line = __LINE__;
3339 goto err_bad_offset;
3341 if (copy_from_user(sg_bufp,
3342 (const void __user *)(uintptr_t)
3343 bp->buffer, bp->length)) {
3344 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3345 proc->pid, thread->pid);
3346 return_error_param = -EFAULT;
3347 return_error = BR_FAILED_REPLY;
3348 return_error_line = __LINE__;
3349 goto err_copy_data_failed;
3351 /* Fixup buffer pointer to target proc address space */
3352 bp->buffer = (uintptr_t)sg_bufp +
3353 binder_alloc_get_user_buffer_offset(
3354 &target_proc->alloc);
3355 sg_bufp += ALIGN(bp->length, sizeof(u64));
3357 ret = binder_fixup_parent(t, thread, bp, off_start,
3360 last_fixup_min_off);
3362 return_error = BR_FAILED_REPLY;
3363 return_error_param = ret;
3364 return_error_line = __LINE__;
3365 goto err_translate_failed;
3367 last_fixup_obj = bp;
3368 last_fixup_min_off = 0;
3371 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
3372 proc->pid, thread->pid, hdr->type);
3373 return_error = BR_FAILED_REPLY;
3374 return_error_param = -EINVAL;
3375 return_error_line = __LINE__;
3376 goto err_bad_object_type;
3379 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
3380 t->work.type = BINDER_WORK_TRANSACTION;
3383 binder_enqueue_thread_work(thread, tcomplete);
3384 binder_inner_proc_lock(target_proc);
3385 if (target_thread->is_dead) {
3386 binder_inner_proc_unlock(target_proc);
3387 goto err_dead_proc_or_thread;
3389 BUG_ON(t->buffer->async_transaction != 0);
3390 binder_pop_transaction_ilocked(target_thread, in_reply_to);
3391 binder_enqueue_thread_work_ilocked(target_thread, &t->work);
3392 binder_inner_proc_unlock(target_proc);
3393 wake_up_interruptible_sync(&target_thread->wait);
3394 binder_restore_priority(current, in_reply_to->saved_priority);
3395 binder_free_transaction(in_reply_to);
3396 } else if (!(t->flags & TF_ONE_WAY)) {
3397 BUG_ON(t->buffer->async_transaction != 0);
3398 binder_inner_proc_lock(proc);
3400 * Defer the TRANSACTION_COMPLETE, so we don't return to
3401 * userspace immediately; this allows the target process to
3402 * immediately start processing this transaction, reducing
3403 * latency. We will then return the TRANSACTION_COMPLETE when
3404 * the target replies (or there is an error).
3406 binder_enqueue_deferred_thread_work_ilocked(thread, tcomplete);
3408 t->from_parent = thread->transaction_stack;
3409 thread->transaction_stack = t;
3410 binder_inner_proc_unlock(proc);
3411 if (!binder_proc_transaction(t, target_proc, target_thread)) {
3412 binder_inner_proc_lock(proc);
3413 binder_pop_transaction_ilocked(thread, t);
3414 binder_inner_proc_unlock(proc);
3415 goto err_dead_proc_or_thread;
3418 BUG_ON(target_node == NULL);
3419 BUG_ON(t->buffer->async_transaction != 1);
3420 binder_enqueue_thread_work(thread, tcomplete);
3421 if (!binder_proc_transaction(t, target_proc, NULL))
3422 goto err_dead_proc_or_thread;
3425 binder_thread_dec_tmpref(target_thread);
3426 binder_proc_dec_tmpref(target_proc);
3428 binder_dec_node_tmpref(target_node);
3430 * write barrier to synchronize with initialization
3434 WRITE_ONCE(e->debug_id_done, t_debug_id);
3437 err_dead_proc_or_thread:
3438 return_error = BR_DEAD_REPLY;
3439 return_error_line = __LINE__;
3440 binder_dequeue_work(proc, tcomplete);
3441 err_translate_failed:
3442 err_bad_object_type:
3445 err_copy_data_failed:
3446 trace_binder_transaction_failed_buffer_release(t->buffer);
3447 binder_transaction_buffer_release(target_proc, t->buffer, offp);
3449 binder_dec_node_tmpref(target_node);
3451 t->buffer->transaction = NULL;
3452 binder_alloc_free_buf(&target_proc->alloc, t->buffer);
3453 err_binder_alloc_buf_failed:
3456 security_release_secctx(secctx, secctx_sz);
3457 err_get_secctx_failed:
3459 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3460 err_alloc_tcomplete_failed:
3462 binder_stats_deleted(BINDER_STAT_TRANSACTION);
3465 err_empty_call_stack:
3467 err_invalid_target_handle:
3469 binder_thread_dec_tmpref(target_thread);
3471 binder_proc_dec_tmpref(target_proc);
3473 binder_dec_node(target_node, 1, 0);
3474 binder_dec_node_tmpref(target_node);
3477 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
3478 "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
3479 proc->pid, thread->pid, return_error, return_error_param,
3480 (u64)tr->data_size, (u64)tr->offsets_size,
3484 struct binder_transaction_log_entry *fe;
3486 e->return_error = return_error;
3487 e->return_error_param = return_error_param;
3488 e->return_error_line = return_error_line;
3489 fe = binder_transaction_log_add(&binder_transaction_log_failed);
3492 * write barrier to synchronize with initialization
3496 WRITE_ONCE(e->debug_id_done, t_debug_id);
3497 WRITE_ONCE(fe->debug_id_done, t_debug_id);
3500 BUG_ON(thread->return_error.cmd != BR_OK);
3502 binder_restore_priority(current, in_reply_to->saved_priority);
3503 thread->return_error.cmd = BR_TRANSACTION_COMPLETE;
3504 binder_enqueue_thread_work(thread, &thread->return_error.work);
3505 binder_send_failed_reply(in_reply_to, return_error);
3507 thread->return_error.cmd = return_error;
3508 binder_enqueue_thread_work(thread, &thread->return_error.work);
3512 static int binder_thread_write(struct binder_proc *proc,
3513 struct binder_thread *thread,
3514 binder_uintptr_t binder_buffer, size_t size,
3515 binder_size_t *consumed)
3518 struct binder_context *context = proc->context;
3519 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
3520 void __user *ptr = buffer + *consumed;
3521 void __user *end = buffer + size;
3523 while (ptr < end && thread->return_error.cmd == BR_OK) {
3526 if (get_user(cmd, (uint32_t __user *)ptr))
3528 ptr += sizeof(uint32_t);
3529 trace_binder_command(cmd);
3530 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
3531 atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]);
3532 atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]);
3533 atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]);
3541 const char *debug_string;
3542 bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE;
3543 bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE;
3544 struct binder_ref_data rdata;
3546 if (get_user(target, (uint32_t __user *)ptr))
3549 ptr += sizeof(uint32_t);
3551 if (increment && !target) {
3552 struct binder_node *ctx_mgr_node;
3553 mutex_lock(&context->context_mgr_node_lock);
3554 ctx_mgr_node = context->binder_context_mgr_node;
3556 ret = binder_inc_ref_for_node(
3558 strong, NULL, &rdata);
3559 mutex_unlock(&context->context_mgr_node_lock);
3562 ret = binder_update_ref_for_handle(
3563 proc, target, increment, strong,
3565 if (!ret && rdata.desc != target) {
3566 binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
3567 proc->pid, thread->pid,
3568 target, rdata.desc);
3572 debug_string = "IncRefs";
3575 debug_string = "Acquire";
3578 debug_string = "Release";
3582 debug_string = "DecRefs";
3586 binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
3587 proc->pid, thread->pid, debug_string,
3588 strong, target, ret);
3591 binder_debug(BINDER_DEBUG_USER_REFS,
3592 "%d:%d %s ref %d desc %d s %d w %d\n",
3593 proc->pid, thread->pid, debug_string,
3594 rdata.debug_id, rdata.desc, rdata.strong,
3598 case BC_INCREFS_DONE:
3599 case BC_ACQUIRE_DONE: {
3600 binder_uintptr_t node_ptr;
3601 binder_uintptr_t cookie;
3602 struct binder_node *node;
3605 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
3607 ptr += sizeof(binder_uintptr_t);
3608 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
3610 ptr += sizeof(binder_uintptr_t);
3611 node = binder_get_node(proc, node_ptr);
3613 binder_user_error("%d:%d %s u%016llx no match\n",
3614 proc->pid, thread->pid,
3615 cmd == BC_INCREFS_DONE ?
3621 if (cookie != node->cookie) {
3622 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
3623 proc->pid, thread->pid,
3624 cmd == BC_INCREFS_DONE ?
3625 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
3626 (u64)node_ptr, node->debug_id,
3627 (u64)cookie, (u64)node->cookie);
3628 binder_put_node(node);
3631 binder_node_inner_lock(node);
3632 if (cmd == BC_ACQUIRE_DONE) {
3633 if (node->pending_strong_ref == 0) {
3634 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
3635 proc->pid, thread->pid,
3637 binder_node_inner_unlock(node);
3638 binder_put_node(node);
3641 node->pending_strong_ref = 0;
3643 if (node->pending_weak_ref == 0) {
3644 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
3645 proc->pid, thread->pid,
3647 binder_node_inner_unlock(node);
3648 binder_put_node(node);
3651 node->pending_weak_ref = 0;
3653 free_node = binder_dec_node_nilocked(node,
3654 cmd == BC_ACQUIRE_DONE, 0);
3656 binder_debug(BINDER_DEBUG_USER_REFS,
3657 "%d:%d %s node %d ls %d lw %d tr %d\n",
3658 proc->pid, thread->pid,
3659 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
3660 node->debug_id, node->local_strong_refs,
3661 node->local_weak_refs, node->tmp_refs);
3662 binder_node_inner_unlock(node);
3663 binder_put_node(node);
3666 case BC_ATTEMPT_ACQUIRE:
3667 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
3669 case BC_ACQUIRE_RESULT:
3670 pr_err("BC_ACQUIRE_RESULT not supported\n");
3673 case BC_FREE_BUFFER: {
3674 binder_uintptr_t data_ptr;
3675 struct binder_buffer *buffer;
3677 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
3679 ptr += sizeof(binder_uintptr_t);
3681 buffer = binder_alloc_prepare_to_free(&proc->alloc,
3683 if (IS_ERR_OR_NULL(buffer)) {
3684 if (PTR_ERR(buffer) == -EPERM) {
3686 "%d:%d BC_FREE_BUFFER u%016llx matched unreturned or currently freeing buffer\n",
3687 proc->pid, thread->pid,
3691 "%d:%d BC_FREE_BUFFER u%016llx no match\n",
3692 proc->pid, thread->pid,
3697 binder_debug(BINDER_DEBUG_FREE_BUFFER,
3698 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
3699 proc->pid, thread->pid, (u64)data_ptr,
3701 buffer->transaction ? "active" : "finished");
3703 if (buffer->transaction) {
3704 buffer->transaction->buffer = NULL;
3705 buffer->transaction = NULL;
3707 if (buffer->async_transaction && buffer->target_node) {
3708 struct binder_node *buf_node;
3709 struct binder_work *w;
3711 buf_node = buffer->target_node;
3712 binder_node_inner_lock(buf_node);
3713 BUG_ON(!buf_node->has_async_transaction);
3714 BUG_ON(buf_node->proc != proc);
3715 w = binder_dequeue_work_head_ilocked(
3716 &buf_node->async_todo);
3718 buf_node->has_async_transaction = false;
3720 binder_enqueue_work_ilocked(
3722 binder_wakeup_proc_ilocked(proc);
3724 binder_node_inner_unlock(buf_node);
3726 trace_binder_transaction_buffer_release(buffer);
3727 binder_transaction_buffer_release(proc, buffer, NULL);
3728 binder_alloc_free_buf(&proc->alloc, buffer);
3732 case BC_TRANSACTION_SG:
3734 struct binder_transaction_data_sg tr;
3736 if (copy_from_user(&tr, ptr, sizeof(tr)))
3739 binder_transaction(proc, thread, &tr.transaction_data,
3740 cmd == BC_REPLY_SG, tr.buffers_size);
3743 case BC_TRANSACTION:
3745 struct binder_transaction_data tr;
3747 if (copy_from_user(&tr, ptr, sizeof(tr)))
3750 binder_transaction(proc, thread, &tr,
3751 cmd == BC_REPLY, 0);
3755 case BC_REGISTER_LOOPER:
3756 binder_debug(BINDER_DEBUG_THREADS,
3757 "%d:%d BC_REGISTER_LOOPER\n",
3758 proc->pid, thread->pid);
3759 binder_inner_proc_lock(proc);
3760 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
3761 thread->looper |= BINDER_LOOPER_STATE_INVALID;
3762 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
3763 proc->pid, thread->pid);
3764 } else if (proc->requested_threads == 0) {
3765 thread->looper |= BINDER_LOOPER_STATE_INVALID;
3766 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
3767 proc->pid, thread->pid);
3769 proc->requested_threads--;
3770 proc->requested_threads_started++;
3772 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
3773 binder_inner_proc_unlock(proc);
3775 case BC_ENTER_LOOPER:
3776 binder_debug(BINDER_DEBUG_THREADS,
3777 "%d:%d BC_ENTER_LOOPER\n",
3778 proc->pid, thread->pid);
3779 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
3780 thread->looper |= BINDER_LOOPER_STATE_INVALID;
3781 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
3782 proc->pid, thread->pid);
3784 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
3786 case BC_EXIT_LOOPER:
3787 binder_debug(BINDER_DEBUG_THREADS,
3788 "%d:%d BC_EXIT_LOOPER\n",
3789 proc->pid, thread->pid);
3790 thread->looper |= BINDER_LOOPER_STATE_EXITED;
3793 case BC_REQUEST_DEATH_NOTIFICATION:
3794 case BC_CLEAR_DEATH_NOTIFICATION: {
3796 binder_uintptr_t cookie;
3797 struct binder_ref *ref;
3798 struct binder_ref_death *death = NULL;
3800 if (get_user(target, (uint32_t __user *)ptr))
3802 ptr += sizeof(uint32_t);
3803 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
3805 ptr += sizeof(binder_uintptr_t);
3806 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3808 * Allocate memory for death notification
3809 * before taking lock
3811 death = kzalloc(sizeof(*death), GFP_KERNEL);
3812 if (death == NULL) {
3813 WARN_ON(thread->return_error.cmd !=
3815 thread->return_error.cmd = BR_ERROR;
3816 binder_enqueue_thread_work(
3818 &thread->return_error.work);
3820 BINDER_DEBUG_FAILED_TRANSACTION,
3821 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
3822 proc->pid, thread->pid);
3826 binder_proc_lock(proc);
3827 ref = binder_get_ref_olocked(proc, target, false);
3829 binder_user_error("%d:%d %s invalid ref %d\n",
3830 proc->pid, thread->pid,
3831 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3832 "BC_REQUEST_DEATH_NOTIFICATION" :
3833 "BC_CLEAR_DEATH_NOTIFICATION",
3835 binder_proc_unlock(proc);
3840 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
3841 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
3842 proc->pid, thread->pid,
3843 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3844 "BC_REQUEST_DEATH_NOTIFICATION" :
3845 "BC_CLEAR_DEATH_NOTIFICATION",
3846 (u64)cookie, ref->data.debug_id,
3847 ref->data.desc, ref->data.strong,
3848 ref->data.weak, ref->node->debug_id);
3850 binder_node_lock(ref->node);
3851 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3853 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
3854 proc->pid, thread->pid);
3855 binder_node_unlock(ref->node);
3856 binder_proc_unlock(proc);
3860 binder_stats_created(BINDER_STAT_DEATH);
3861 INIT_LIST_HEAD(&death->work.entry);
3862 death->cookie = cookie;
3864 if (ref->node->proc == NULL) {
3865 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
3867 binder_inner_proc_lock(proc);
3868 binder_enqueue_work_ilocked(
3869 &ref->death->work, &proc->todo);
3870 binder_wakeup_proc_ilocked(proc);
3871 binder_inner_proc_unlock(proc);
3874 if (ref->death == NULL) {
3875 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
3876 proc->pid, thread->pid);
3877 binder_node_unlock(ref->node);
3878 binder_proc_unlock(proc);
3882 if (death->cookie != cookie) {
3883 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
3884 proc->pid, thread->pid,
3887 binder_node_unlock(ref->node);
3888 binder_proc_unlock(proc);
3892 binder_inner_proc_lock(proc);
3893 if (list_empty(&death->work.entry)) {
3894 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
3895 if (thread->looper &
3896 (BINDER_LOOPER_STATE_REGISTERED |
3897 BINDER_LOOPER_STATE_ENTERED))
3898 binder_enqueue_thread_work_ilocked(
3902 binder_enqueue_work_ilocked(
3905 binder_wakeup_proc_ilocked(
3909 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
3910 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
3912 binder_inner_proc_unlock(proc);
3914 binder_node_unlock(ref->node);
3915 binder_proc_unlock(proc);
3917 case BC_DEAD_BINDER_DONE: {
3918 struct binder_work *w;
3919 binder_uintptr_t cookie;
3920 struct binder_ref_death *death = NULL;
3922 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
3925 ptr += sizeof(cookie);
3926 binder_inner_proc_lock(proc);
3927 list_for_each_entry(w, &proc->delivered_death,
3929 struct binder_ref_death *tmp_death =
3931 struct binder_ref_death,
3934 if (tmp_death->cookie == cookie) {
3939 binder_debug(BINDER_DEBUG_DEAD_BINDER,
3940 "%d:%d BC_DEAD_BINDER_DONE %016llx found %pK\n",
3941 proc->pid, thread->pid, (u64)cookie,
3943 if (death == NULL) {
3944 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
3945 proc->pid, thread->pid, (u64)cookie);
3946 binder_inner_proc_unlock(proc);
3949 binder_dequeue_work_ilocked(&death->work);
3950 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
3951 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
3952 if (thread->looper &
3953 (BINDER_LOOPER_STATE_REGISTERED |
3954 BINDER_LOOPER_STATE_ENTERED))
3955 binder_enqueue_thread_work_ilocked(
3956 thread, &death->work);
3958 binder_enqueue_work_ilocked(
3961 binder_wakeup_proc_ilocked(proc);
3964 binder_inner_proc_unlock(proc);
3968 pr_err("%d:%d unknown command %d\n",
3969 proc->pid, thread->pid, cmd);
3972 *consumed = ptr - buffer;
3977 static void binder_stat_br(struct binder_proc *proc,
3978 struct binder_thread *thread, uint32_t cmd)
3980 trace_binder_return(cmd);
3981 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
3982 atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
3983 atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
3984 atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
3988 static int binder_put_node_cmd(struct binder_proc *proc,
3989 struct binder_thread *thread,
3991 binder_uintptr_t node_ptr,
3992 binder_uintptr_t node_cookie,
3994 uint32_t cmd, const char *cmd_name)
3996 void __user *ptr = *ptrp;
3998 if (put_user(cmd, (uint32_t __user *)ptr))
4000 ptr += sizeof(uint32_t);
4002 if (put_user(node_ptr, (binder_uintptr_t __user *)ptr))
4004 ptr += sizeof(binder_uintptr_t);
4006 if (put_user(node_cookie, (binder_uintptr_t __user *)ptr))
4008 ptr += sizeof(binder_uintptr_t);
4010 binder_stat_br(proc, thread, cmd);
4011 binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n",
4012 proc->pid, thread->pid, cmd_name, node_debug_id,
4013 (u64)node_ptr, (u64)node_cookie);
4019 static int binder_wait_for_work(struct binder_thread *thread,
4023 struct binder_proc *proc = thread->proc;
4026 freezer_do_not_count();
4027 binder_inner_proc_lock(proc);
4029 prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE);
4030 if (binder_has_work_ilocked(thread, do_proc_work))
4033 list_add(&thread->waiting_thread_node,
4034 &proc->waiting_threads);
4035 binder_inner_proc_unlock(proc);
4037 binder_inner_proc_lock(proc);
4038 list_del_init(&thread->waiting_thread_node);
4039 if (signal_pending(current)) {
4044 finish_wait(&thread->wait, &wait);
4045 binder_inner_proc_unlock(proc);
4051 static int binder_thread_read(struct binder_proc *proc,
4052 struct binder_thread *thread,
4053 binder_uintptr_t binder_buffer, size_t size,
4054 binder_size_t *consumed, int non_block)
4056 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
4057 void __user *ptr = buffer + *consumed;
4058 void __user *end = buffer + size;
4061 int wait_for_proc_work;
4063 if (*consumed == 0) {
4064 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
4066 ptr += sizeof(uint32_t);
4070 binder_inner_proc_lock(proc);
4071 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
4072 binder_inner_proc_unlock(proc);
4074 thread->looper |= BINDER_LOOPER_STATE_WAITING;
4076 trace_binder_wait_for_work(wait_for_proc_work,
4077 !!thread->transaction_stack,
4078 !binder_worklist_empty(proc, &thread->todo));
4079 if (wait_for_proc_work) {
4080 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4081 BINDER_LOOPER_STATE_ENTERED))) {
4082 binder_user_error("%d:%d ERROR: Thread waiting for process work before calling BC_REGISTER_LOOPER or BC_ENTER_LOOPER (state %x)\n",
4083 proc->pid, thread->pid, thread->looper);
4084 wait_event_interruptible(binder_user_error_wait,
4085 binder_stop_on_user_error < 2);
4087 binder_restore_priority(current, proc->default_priority);
4091 if (!binder_has_work(thread, wait_for_proc_work))
4094 ret = binder_wait_for_work(thread, wait_for_proc_work);
4097 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
4104 struct binder_transaction_data_secctx tr;
4105 struct binder_transaction_data *trd = &tr.transaction_data;
4106 struct binder_work *w = NULL;
4107 struct list_head *list = NULL;
4108 struct binder_transaction *t = NULL;
4109 struct binder_thread *t_from;
4110 size_t trsize = sizeof(*trd);
4112 binder_inner_proc_lock(proc);
4113 if (!binder_worklist_empty_ilocked(&thread->todo))
4114 list = &thread->todo;
4115 else if (!binder_worklist_empty_ilocked(&proc->todo) &&
4119 binder_inner_proc_unlock(proc);
4122 if (ptr - buffer == 4 && !thread->looper_need_return)
4127 if (end - ptr < sizeof(tr) + 4) {
4128 binder_inner_proc_unlock(proc);
4131 w = binder_dequeue_work_head_ilocked(list);
4132 if (binder_worklist_empty_ilocked(&thread->todo))
4133 thread->process_todo = false;
4136 case BINDER_WORK_TRANSACTION: {
4137 binder_inner_proc_unlock(proc);
4138 t = container_of(w, struct binder_transaction, work);
4140 case BINDER_WORK_RETURN_ERROR: {
4141 struct binder_error *e = container_of(
4142 w, struct binder_error, work);
4144 WARN_ON(e->cmd == BR_OK);
4145 binder_inner_proc_unlock(proc);
4146 if (put_user(e->cmd, (uint32_t __user *)ptr))
4150 ptr += sizeof(uint32_t);
4152 binder_stat_br(proc, thread, cmd);
4154 case BINDER_WORK_TRANSACTION_COMPLETE: {
4155 binder_inner_proc_unlock(proc);
4156 cmd = BR_TRANSACTION_COMPLETE;
4157 if (put_user(cmd, (uint32_t __user *)ptr))
4159 ptr += sizeof(uint32_t);
4161 binder_stat_br(proc, thread, cmd);
4162 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
4163 "%d:%d BR_TRANSACTION_COMPLETE\n",
4164 proc->pid, thread->pid);
4166 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4168 case BINDER_WORK_NODE: {
4169 struct binder_node *node = container_of(w, struct binder_node, work);
4171 binder_uintptr_t node_ptr = node->ptr;
4172 binder_uintptr_t node_cookie = node->cookie;
4173 int node_debug_id = node->debug_id;
4176 void __user *orig_ptr = ptr;
4178 BUG_ON(proc != node->proc);
4179 strong = node->internal_strong_refs ||
4180 node->local_strong_refs;
4181 weak = !hlist_empty(&node->refs) ||
4182 node->local_weak_refs ||
4183 node->tmp_refs || strong;
4184 has_strong_ref = node->has_strong_ref;
4185 has_weak_ref = node->has_weak_ref;
4187 if (weak && !has_weak_ref) {
4188 node->has_weak_ref = 1;
4189 node->pending_weak_ref = 1;
4190 node->local_weak_refs++;
4192 if (strong && !has_strong_ref) {
4193 node->has_strong_ref = 1;
4194 node->pending_strong_ref = 1;
4195 node->local_strong_refs++;
4197 if (!strong && has_strong_ref)
4198 node->has_strong_ref = 0;
4199 if (!weak && has_weak_ref)
4200 node->has_weak_ref = 0;
4201 if (!weak && !strong) {
4202 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4203 "%d:%d node %d u%016llx c%016llx deleted\n",
4204 proc->pid, thread->pid,
4208 rb_erase(&node->rb_node, &proc->nodes);
4209 binder_inner_proc_unlock(proc);
4210 binder_node_lock(node);
4212 * Acquire the node lock before freeing the
4213 * node to serialize with other threads that
4214 * may have been holding the node lock while
4215 * decrementing this node (avoids race where
4216 * this thread frees while the other thread
4217 * is unlocking the node after the final
4220 binder_node_unlock(node);
4221 binder_free_node(node);
4223 binder_inner_proc_unlock(proc);
4225 if (weak && !has_weak_ref)
4226 ret = binder_put_node_cmd(
4227 proc, thread, &ptr, node_ptr,
4228 node_cookie, node_debug_id,
4229 BR_INCREFS, "BR_INCREFS");
4230 if (!ret && strong && !has_strong_ref)
4231 ret = binder_put_node_cmd(
4232 proc, thread, &ptr, node_ptr,
4233 node_cookie, node_debug_id,
4234 BR_ACQUIRE, "BR_ACQUIRE");
4235 if (!ret && !strong && has_strong_ref)
4236 ret = binder_put_node_cmd(
4237 proc, thread, &ptr, node_ptr,
4238 node_cookie, node_debug_id,
4239 BR_RELEASE, "BR_RELEASE");
4240 if (!ret && !weak && has_weak_ref)
4241 ret = binder_put_node_cmd(
4242 proc, thread, &ptr, node_ptr,
4243 node_cookie, node_debug_id,
4244 BR_DECREFS, "BR_DECREFS");
4245 if (orig_ptr == ptr)
4246 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4247 "%d:%d node %d u%016llx c%016llx state unchanged\n",
4248 proc->pid, thread->pid,
4255 case BINDER_WORK_DEAD_BINDER:
4256 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4257 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4258 struct binder_ref_death *death;
4260 binder_uintptr_t cookie;
4262 death = container_of(w, struct binder_ref_death, work);
4263 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
4264 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
4266 cmd = BR_DEAD_BINDER;
4267 cookie = death->cookie;
4269 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
4270 "%d:%d %s %016llx\n",
4271 proc->pid, thread->pid,
4272 cmd == BR_DEAD_BINDER ?
4274 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
4276 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
4277 binder_inner_proc_unlock(proc);
4279 binder_stats_deleted(BINDER_STAT_DEATH);
4281 binder_enqueue_work_ilocked(
4282 w, &proc->delivered_death);
4283 binder_inner_proc_unlock(proc);
4285 if (put_user(cmd, (uint32_t __user *)ptr))
4287 ptr += sizeof(uint32_t);
4288 if (put_user(cookie,
4289 (binder_uintptr_t __user *)ptr))
4291 ptr += sizeof(binder_uintptr_t);
4292 binder_stat_br(proc, thread, cmd);
4293 if (cmd == BR_DEAD_BINDER)
4294 goto done; /* DEAD_BINDER notifications can cause transactions */
4301 BUG_ON(t->buffer == NULL);
4302 if (t->buffer->target_node) {
4303 struct binder_node *target_node = t->buffer->target_node;
4304 struct binder_priority node_prio;
4306 trd->target.ptr = target_node->ptr;
4307 trd->cookie = target_node->cookie;
4308 node_prio.sched_policy = target_node->sched_policy;
4309 node_prio.prio = target_node->min_priority;
4310 binder_transaction_priority(current, t, node_prio,
4311 target_node->inherit_rt);
4312 cmd = BR_TRANSACTION;
4314 trd->target.ptr = 0;
4318 trd->code = t->code;
4319 trd->flags = t->flags;
4320 trd->sender_euid = from_kuid(current_user_ns(), t->sender_euid);
4322 t_from = binder_get_txn_from(t);
4324 struct task_struct *sender = t_from->proc->tsk;
4327 task_tgid_nr_ns(sender,
4328 task_active_pid_ns(current));
4330 trd->sender_pid = 0;
4333 trd->data_size = t->buffer->data_size;
4334 trd->offsets_size = t->buffer->offsets_size;
4335 trd->data.ptr.buffer = (binder_uintptr_t)
4336 ((uintptr_t)t->buffer->data +
4337 binder_alloc_get_user_buffer_offset(&proc->alloc));
4338 trd->data.ptr.offsets = trd->data.ptr.buffer +
4339 ALIGN(t->buffer->data_size,
4342 tr.secctx = t->security_ctx;
4343 if (t->security_ctx) {
4344 cmd = BR_TRANSACTION_SEC_CTX;
4345 trsize = sizeof(tr);
4347 if (put_user(cmd, (uint32_t __user *)ptr)) {
4349 binder_thread_dec_tmpref(t_from);
4351 binder_cleanup_transaction(t, "put_user failed",
4356 ptr += sizeof(uint32_t);
4357 if (copy_to_user(ptr, &tr, trsize)) {
4359 binder_thread_dec_tmpref(t_from);
4361 binder_cleanup_transaction(t, "copy_to_user failed",
4368 trace_binder_transaction_received(t);
4369 binder_stat_br(proc, thread, cmd);
4370 binder_debug(BINDER_DEBUG_TRANSACTION,
4371 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
4372 proc->pid, thread->pid,
4373 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
4374 (cmd == BR_TRANSACTION_SEC_CTX) ?
4375 "BR_TRANSACTION_SEC_CTX" : "BR_REPLY",
4376 t->debug_id, t_from ? t_from->proc->pid : 0,
4377 t_from ? t_from->pid : 0, cmd,
4378 t->buffer->data_size, t->buffer->offsets_size,
4379 (u64)trd->data.ptr.buffer,
4380 (u64)trd->data.ptr.offsets);
4383 binder_thread_dec_tmpref(t_from);
4384 t->buffer->allow_user_free = 1;
4385 if (cmd != BR_REPLY && !(t->flags & TF_ONE_WAY)) {
4386 binder_inner_proc_lock(thread->proc);
4387 t->to_parent = thread->transaction_stack;
4388 t->to_thread = thread;
4389 thread->transaction_stack = t;
4390 binder_inner_proc_unlock(thread->proc);
4392 binder_free_transaction(t);
4399 *consumed = ptr - buffer;
4400 binder_inner_proc_lock(proc);
4401 if (proc->requested_threads == 0 &&
4402 list_empty(&thread->proc->waiting_threads) &&
4403 proc->requested_threads_started < proc->max_threads &&
4404 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4405 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
4406 /*spawn a new thread if we leave this out */) {
4407 proc->requested_threads++;
4408 binder_inner_proc_unlock(proc);
4409 binder_debug(BINDER_DEBUG_THREADS,
4410 "%d:%d BR_SPAWN_LOOPER\n",
4411 proc->pid, thread->pid);
4412 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
4414 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
4416 binder_inner_proc_unlock(proc);
4420 static void binder_release_work(struct binder_proc *proc,
4421 struct list_head *list)
4423 struct binder_work *w;
4426 w = binder_dequeue_work_head(proc, list);
4431 case BINDER_WORK_TRANSACTION: {
4432 struct binder_transaction *t;
4434 t = container_of(w, struct binder_transaction, work);
4436 binder_cleanup_transaction(t, "process died.",
4439 case BINDER_WORK_RETURN_ERROR: {
4440 struct binder_error *e = container_of(
4441 w, struct binder_error, work);
4443 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4444 "undelivered TRANSACTION_ERROR: %u\n",
4447 case BINDER_WORK_TRANSACTION_COMPLETE: {
4448 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4449 "undelivered TRANSACTION_COMPLETE\n");
4451 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4453 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4454 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4455 struct binder_ref_death *death;
4457 death = container_of(w, struct binder_ref_death, work);
4458 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4459 "undelivered death notification, %016llx\n",
4460 (u64)death->cookie);
4462 binder_stats_deleted(BINDER_STAT_DEATH);
4465 pr_err("unexpected work type, %d, not freed\n",
4473 static struct binder_thread *binder_get_thread_ilocked(
4474 struct binder_proc *proc, struct binder_thread *new_thread)
4476 struct binder_thread *thread = NULL;
4477 struct rb_node *parent = NULL;
4478 struct rb_node **p = &proc->threads.rb_node;
4482 thread = rb_entry(parent, struct binder_thread, rb_node);
4484 if (current->pid < thread->pid)
4486 else if (current->pid > thread->pid)
4487 p = &(*p)->rb_right;
4493 thread = new_thread;
4494 binder_stats_created(BINDER_STAT_THREAD);
4495 thread->proc = proc;
4496 thread->pid = current->pid;
4497 get_task_struct(current);
4498 thread->task = current;
4499 atomic_set(&thread->tmp_ref, 0);
4500 init_waitqueue_head(&thread->wait);
4501 INIT_LIST_HEAD(&thread->todo);
4502 rb_link_node(&thread->rb_node, parent, p);
4503 rb_insert_color(&thread->rb_node, &proc->threads);
4504 thread->looper_need_return = true;
4505 thread->return_error.work.type = BINDER_WORK_RETURN_ERROR;
4506 thread->return_error.cmd = BR_OK;
4507 thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR;
4508 thread->reply_error.cmd = BR_OK;
4509 INIT_LIST_HEAD(&new_thread->waiting_thread_node);
4513 static struct binder_thread *binder_get_thread(struct binder_proc *proc)
4515 struct binder_thread *thread;
4516 struct binder_thread *new_thread;
4518 binder_inner_proc_lock(proc);
4519 thread = binder_get_thread_ilocked(proc, NULL);
4520 binder_inner_proc_unlock(proc);
4522 new_thread = kzalloc(sizeof(*thread), GFP_KERNEL);
4523 if (new_thread == NULL)
4525 binder_inner_proc_lock(proc);
4526 thread = binder_get_thread_ilocked(proc, new_thread);
4527 binder_inner_proc_unlock(proc);
4528 if (thread != new_thread)
4534 static void binder_free_proc(struct binder_proc *proc)
4536 BUG_ON(!list_empty(&proc->todo));
4537 BUG_ON(!list_empty(&proc->delivered_death));
4538 binder_alloc_deferred_release(&proc->alloc);
4539 put_task_struct(proc->tsk);
4540 binder_stats_deleted(BINDER_STAT_PROC);
4544 static void binder_free_thread(struct binder_thread *thread)
4546 BUG_ON(!list_empty(&thread->todo));
4547 binder_stats_deleted(BINDER_STAT_THREAD);
4548 binder_proc_dec_tmpref(thread->proc);
4549 put_task_struct(thread->task);
4553 static int binder_thread_release(struct binder_proc *proc,
4554 struct binder_thread *thread)
4556 struct binder_transaction *t;
4557 struct binder_transaction *send_reply = NULL;
4558 int active_transactions = 0;
4559 struct binder_transaction *last_t = NULL;
4561 binder_inner_proc_lock(thread->proc);
4563 * take a ref on the proc so it survives
4564 * after we remove this thread from proc->threads.
4565 * The corresponding dec is when we actually
4566 * free the thread in binder_free_thread()
4570 * take a ref on this thread to ensure it
4571 * survives while we are releasing it
4573 atomic_inc(&thread->tmp_ref);
4574 rb_erase(&thread->rb_node, &proc->threads);
4575 t = thread->transaction_stack;
4577 spin_lock(&t->lock);
4578 if (t->to_thread == thread)
4581 thread->is_dead = true;
4585 active_transactions++;
4586 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4587 "release %d:%d transaction %d %s, still active\n",
4588 proc->pid, thread->pid,
4590 (t->to_thread == thread) ? "in" : "out");
4592 if (t->to_thread == thread) {
4594 t->to_thread = NULL;
4596 t->buffer->transaction = NULL;
4600 } else if (t->from == thread) {
4605 spin_unlock(&last_t->lock);
4607 spin_lock(&t->lock);
4611 * If this thread used poll, make sure we remove the waitqueue
4612 * from any epoll data structures holding it with POLLFREE.
4613 * waitqueue_active() is safe to use here because we're holding
4616 if ((thread->looper & BINDER_LOOPER_STATE_POLL) &&
4617 waitqueue_active(&thread->wait)) {
4618 wake_up_poll(&thread->wait, POLLHUP | POLLFREE);
4621 binder_inner_proc_unlock(thread->proc);
4624 * This is needed to avoid races between wake_up_poll() above and
4625 * and ep_remove_waitqueue() called for other reasons (eg the epoll file
4626 * descriptor being closed); ep_remove_waitqueue() holds an RCU read
4627 * lock, so we can be sure it's done after calling synchronize_rcu().
4629 if (thread->looper & BINDER_LOOPER_STATE_POLL)
4633 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
4634 binder_release_work(proc, &thread->todo);
4635 binder_thread_dec_tmpref(thread);
4636 return active_transactions;
4639 static unsigned int binder_poll(struct file *filp,
4640 struct poll_table_struct *wait)
4642 struct binder_proc *proc = filp->private_data;
4643 struct binder_thread *thread = NULL;
4644 bool wait_for_proc_work;
4646 thread = binder_get_thread(proc);
4650 binder_inner_proc_lock(thread->proc);
4651 thread->looper |= BINDER_LOOPER_STATE_POLL;
4652 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
4654 binder_inner_proc_unlock(thread->proc);
4656 poll_wait(filp, &thread->wait, wait);
4658 if (binder_has_work(thread, wait_for_proc_work))
4664 static int binder_ioctl_write_read(struct file *filp,
4665 unsigned int cmd, unsigned long arg,
4666 struct binder_thread *thread)
4669 struct binder_proc *proc = filp->private_data;
4670 unsigned int size = _IOC_SIZE(cmd);
4671 void __user *ubuf = (void __user *)arg;
4672 struct binder_write_read bwr;
4674 if (size != sizeof(struct binder_write_read)) {
4678 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
4682 binder_debug(BINDER_DEBUG_READ_WRITE,
4683 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
4684 proc->pid, thread->pid,
4685 (u64)bwr.write_size, (u64)bwr.write_buffer,
4686 (u64)bwr.read_size, (u64)bwr.read_buffer);
4688 if (bwr.write_size > 0) {
4689 ret = binder_thread_write(proc, thread,
4692 &bwr.write_consumed);
4693 trace_binder_write_done(ret);
4695 bwr.read_consumed = 0;
4696 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4701 if (bwr.read_size > 0) {
4702 ret = binder_thread_read(proc, thread, bwr.read_buffer,
4705 filp->f_flags & O_NONBLOCK);
4706 trace_binder_read_done(ret);
4707 binder_inner_proc_lock(proc);
4708 if (!binder_worklist_empty_ilocked(&proc->todo))
4709 binder_wakeup_proc_ilocked(proc);
4710 binder_inner_proc_unlock(proc);
4712 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4717 binder_debug(BINDER_DEBUG_READ_WRITE,
4718 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
4719 proc->pid, thread->pid,
4720 (u64)bwr.write_consumed, (u64)bwr.write_size,
4721 (u64)bwr.read_consumed, (u64)bwr.read_size);
4722 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
4730 static int binder_ioctl_set_ctx_mgr(struct file *filp,
4731 struct flat_binder_object *fbo)
4734 struct binder_proc *proc = filp->private_data;
4735 struct binder_context *context = proc->context;
4736 struct binder_node *new_node;
4737 kuid_t curr_euid = current_euid();
4739 mutex_lock(&context->context_mgr_node_lock);
4740 if (context->binder_context_mgr_node) {
4741 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
4745 ret = security_binder_set_context_mgr(proc->tsk);
4748 if (uid_valid(context->binder_context_mgr_uid)) {
4749 if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
4750 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
4751 from_kuid(&init_user_ns, curr_euid),
4752 from_kuid(&init_user_ns,
4753 context->binder_context_mgr_uid));
4758 context->binder_context_mgr_uid = curr_euid;
4760 new_node = binder_new_node(proc, fbo);
4765 binder_node_lock(new_node);
4766 new_node->local_weak_refs++;
4767 new_node->local_strong_refs++;
4768 new_node->has_strong_ref = 1;
4769 new_node->has_weak_ref = 1;
4770 context->binder_context_mgr_node = new_node;
4771 binder_node_unlock(new_node);
4772 binder_put_node(new_node);
4774 mutex_unlock(&context->context_mgr_node_lock);
4778 static int binder_ioctl_get_node_info_for_ref(struct binder_proc *proc,
4779 struct binder_node_info_for_ref *info)
4781 struct binder_node *node;
4782 struct binder_context *context = proc->context;
4783 __u32 handle = info->handle;
4785 if (info->strong_count || info->weak_count || info->reserved1 ||
4786 info->reserved2 || info->reserved3) {
4787 binder_user_error("%d BINDER_GET_NODE_INFO_FOR_REF: only handle may be non-zero.",
4792 /* This ioctl may only be used by the context manager */
4793 mutex_lock(&context->context_mgr_node_lock);
4794 if (!context->binder_context_mgr_node ||
4795 context->binder_context_mgr_node->proc != proc) {
4796 mutex_unlock(&context->context_mgr_node_lock);
4799 mutex_unlock(&context->context_mgr_node_lock);
4801 node = binder_get_node_from_ref(proc, handle, true, NULL);
4805 info->strong_count = node->local_strong_refs +
4806 node->internal_strong_refs;
4807 info->weak_count = node->local_weak_refs;
4809 binder_put_node(node);
4814 static int binder_ioctl_get_node_debug_info(struct binder_proc *proc,
4815 struct binder_node_debug_info *info) {
4817 binder_uintptr_t ptr = info->ptr;
4819 memset(info, 0, sizeof(*info));
4821 binder_inner_proc_lock(proc);
4822 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
4823 struct binder_node *node = rb_entry(n, struct binder_node,
4825 if (node->ptr > ptr) {
4826 info->ptr = node->ptr;
4827 info->cookie = node->cookie;
4828 info->has_strong_ref = node->has_strong_ref;
4829 info->has_weak_ref = node->has_weak_ref;
4833 binder_inner_proc_unlock(proc);
4838 static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4841 struct binder_proc *proc = filp->private_data;
4842 struct binder_thread *thread;
4843 unsigned int size = _IOC_SIZE(cmd);
4844 void __user *ubuf = (void __user *)arg;
4846 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
4847 proc->pid, current->pid, cmd, arg);*/
4849 binder_selftest_alloc(&proc->alloc);
4851 trace_binder_ioctl(cmd, arg);
4853 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4857 thread = binder_get_thread(proc);
4858 if (thread == NULL) {
4864 case BINDER_WRITE_READ:
4865 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
4869 case BINDER_SET_MAX_THREADS: {
4872 if (copy_from_user(&max_threads, ubuf,
4873 sizeof(max_threads))) {
4877 binder_inner_proc_lock(proc);
4878 proc->max_threads = max_threads;
4879 binder_inner_proc_unlock(proc);
4882 case BINDER_SET_CONTEXT_MGR_EXT: {
4883 struct flat_binder_object fbo;
4885 if (copy_from_user(&fbo, ubuf, sizeof(fbo))) {
4889 ret = binder_ioctl_set_ctx_mgr(filp, &fbo);
4894 case BINDER_SET_CONTEXT_MGR:
4895 ret = binder_ioctl_set_ctx_mgr(filp, NULL);
4899 case BINDER_THREAD_EXIT:
4900 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
4901 proc->pid, thread->pid);
4902 binder_thread_release(proc, thread);
4905 case BINDER_VERSION: {
4906 struct binder_version __user *ver = ubuf;
4908 if (size != sizeof(struct binder_version)) {
4912 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
4913 &ver->protocol_version)) {
4919 case BINDER_GET_NODE_INFO_FOR_REF: {
4920 struct binder_node_info_for_ref info;
4922 if (copy_from_user(&info, ubuf, sizeof(info))) {
4927 ret = binder_ioctl_get_node_info_for_ref(proc, &info);
4931 if (copy_to_user(ubuf, &info, sizeof(info))) {
4938 case BINDER_GET_NODE_DEBUG_INFO: {
4939 struct binder_node_debug_info info;
4941 if (copy_from_user(&info, ubuf, sizeof(info))) {
4946 ret = binder_ioctl_get_node_debug_info(proc, &info);
4950 if (copy_to_user(ubuf, &info, sizeof(info))) {
4963 thread->looper_need_return = false;
4964 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4965 if (ret && ret != -ERESTARTSYS)
4966 pr_info("%d:%d ioctl %x %lx returned %d\n", proc->pid, current->pid, cmd, arg, ret);
4968 trace_binder_ioctl_done(ret);
4972 static void binder_vma_open(struct vm_area_struct *vma)
4974 struct binder_proc *proc = vma->vm_private_data;
4976 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4977 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
4978 proc->pid, vma->vm_start, vma->vm_end,
4979 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4980 (unsigned long)pgprot_val(vma->vm_page_prot));
4983 static void binder_vma_close(struct vm_area_struct *vma)
4985 struct binder_proc *proc = vma->vm_private_data;
4987 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4988 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
4989 proc->pid, vma->vm_start, vma->vm_end,
4990 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4991 (unsigned long)pgprot_val(vma->vm_page_prot));
4992 binder_alloc_vma_close(&proc->alloc);
4993 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
4996 static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
4998 return VM_FAULT_SIGBUS;
5001 static const struct vm_operations_struct binder_vm_ops = {
5002 .open = binder_vma_open,
5003 .close = binder_vma_close,
5004 .fault = binder_vm_fault,
5007 static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
5010 struct binder_proc *proc = filp->private_data;
5011 const char *failure_string;
5013 if (proc->tsk != current->group_leader)
5016 if ((vma->vm_end - vma->vm_start) > SZ_4M)
5017 vma->vm_end = vma->vm_start + SZ_4M;
5019 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5020 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
5021 __func__, proc->pid, vma->vm_start, vma->vm_end,
5022 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
5023 (unsigned long)pgprot_val(vma->vm_page_prot));
5025 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
5027 failure_string = "bad vm_flags";
5030 vma->vm_flags |= VM_DONTCOPY | VM_MIXEDMAP;
5031 vma->vm_flags &= ~VM_MAYWRITE;
5033 vma->vm_ops = &binder_vm_ops;
5034 vma->vm_private_data = proc;
5036 ret = binder_alloc_mmap_handler(&proc->alloc, vma);
5039 mutex_lock(&proc->files_lock);
5040 proc->files = get_files_struct(current);
5041 mutex_unlock(&proc->files_lock);
5045 pr_err("%s: %d %lx-%lx %s failed %d\n", __func__,
5046 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
5050 static int binder_open(struct inode *nodp, struct file *filp)
5052 struct binder_proc *proc;
5053 struct binder_device *binder_dev;
5055 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "%s: %d:%d\n", __func__,
5056 current->group_leader->pid, current->pid);
5058 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
5061 spin_lock_init(&proc->inner_lock);
5062 spin_lock_init(&proc->outer_lock);
5063 get_task_struct(current->group_leader);
5064 proc->tsk = current->group_leader;
5065 mutex_init(&proc->files_lock);
5066 INIT_LIST_HEAD(&proc->todo);
5067 if (binder_supported_policy(current->policy)) {
5068 proc->default_priority.sched_policy = current->policy;
5069 proc->default_priority.prio = current->normal_prio;
5071 proc->default_priority.sched_policy = SCHED_NORMAL;
5072 proc->default_priority.prio = NICE_TO_PRIO(0);
5075 binder_dev = container_of(filp->private_data, struct binder_device,
5077 proc->context = &binder_dev->context;
5078 binder_alloc_init(&proc->alloc);
5080 binder_stats_created(BINDER_STAT_PROC);
5081 proc->pid = current->group_leader->pid;
5082 INIT_LIST_HEAD(&proc->delivered_death);
5083 INIT_LIST_HEAD(&proc->waiting_threads);
5084 filp->private_data = proc;
5086 mutex_lock(&binder_procs_lock);
5087 hlist_add_head(&proc->proc_node, &binder_procs);
5088 mutex_unlock(&binder_procs_lock);
5090 if (binder_debugfs_dir_entry_proc) {
5093 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
5095 * proc debug entries are shared between contexts, so
5096 * this will fail if the process tries to open the driver
5097 * again with a different context. The priting code will
5098 * anyway print all contexts that a given PID has, so this
5101 proc->debugfs_entry = debugfs_create_file(strbuf, 0444,
5102 binder_debugfs_dir_entry_proc,
5103 (void *)(unsigned long)proc->pid,
5110 static int binder_flush(struct file *filp, fl_owner_t id)
5112 struct binder_proc *proc = filp->private_data;
5114 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
5119 static void binder_deferred_flush(struct binder_proc *proc)
5124 binder_inner_proc_lock(proc);
5125 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
5126 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
5128 thread->looper_need_return = true;
5129 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
5130 wake_up_interruptible(&thread->wait);
5134 binder_inner_proc_unlock(proc);
5136 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5137 "binder_flush: %d woke %d threads\n", proc->pid,
5141 static int binder_release(struct inode *nodp, struct file *filp)
5143 struct binder_proc *proc = filp->private_data;
5145 debugfs_remove(proc->debugfs_entry);
5146 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
5151 static int binder_node_release(struct binder_node *node, int refs)
5153 struct binder_ref *ref;
5155 struct binder_proc *proc = node->proc;
5157 binder_release_work(proc, &node->async_todo);
5159 binder_node_lock(node);
5160 binder_inner_proc_lock(proc);
5161 binder_dequeue_work_ilocked(&node->work);
5163 * The caller must have taken a temporary ref on the node,
5165 BUG_ON(!node->tmp_refs);
5166 if (hlist_empty(&node->refs) && node->tmp_refs == 1) {
5167 binder_inner_proc_unlock(proc);
5168 binder_node_unlock(node);
5169 binder_free_node(node);
5175 node->local_strong_refs = 0;
5176 node->local_weak_refs = 0;
5177 binder_inner_proc_unlock(proc);
5179 spin_lock(&binder_dead_nodes_lock);
5180 hlist_add_head(&node->dead_node, &binder_dead_nodes);
5181 spin_unlock(&binder_dead_nodes_lock);
5183 hlist_for_each_entry(ref, &node->refs, node_entry) {
5186 * Need the node lock to synchronize
5187 * with new notification requests and the
5188 * inner lock to synchronize with queued
5189 * death notifications.
5191 binder_inner_proc_lock(ref->proc);
5193 binder_inner_proc_unlock(ref->proc);
5199 BUG_ON(!list_empty(&ref->death->work.entry));
5200 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
5201 binder_enqueue_work_ilocked(&ref->death->work,
5203 binder_wakeup_proc_ilocked(ref->proc);
5204 binder_inner_proc_unlock(ref->proc);
5207 binder_debug(BINDER_DEBUG_DEAD_BINDER,
5208 "node %d now dead, refs %d, death %d\n",
5209 node->debug_id, refs, death);
5210 binder_node_unlock(node);
5211 binder_put_node(node);
5216 static void binder_deferred_release(struct binder_proc *proc)
5218 struct binder_context *context = proc->context;
5220 int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
5222 BUG_ON(proc->files);
5224 mutex_lock(&binder_procs_lock);
5225 hlist_del(&proc->proc_node);
5226 mutex_unlock(&binder_procs_lock);
5228 mutex_lock(&context->context_mgr_node_lock);
5229 if (context->binder_context_mgr_node &&
5230 context->binder_context_mgr_node->proc == proc) {
5231 binder_debug(BINDER_DEBUG_DEAD_BINDER,
5232 "%s: %d context_mgr_node gone\n",
5233 __func__, proc->pid);
5234 context->binder_context_mgr_node = NULL;
5236 mutex_unlock(&context->context_mgr_node_lock);
5237 binder_inner_proc_lock(proc);
5239 * Make sure proc stays alive after we
5240 * remove all the threads
5244 proc->is_dead = true;
5246 active_transactions = 0;
5247 while ((n = rb_first(&proc->threads))) {
5248 struct binder_thread *thread;
5250 thread = rb_entry(n, struct binder_thread, rb_node);
5251 binder_inner_proc_unlock(proc);
5253 active_transactions += binder_thread_release(proc, thread);
5254 binder_inner_proc_lock(proc);
5259 while ((n = rb_first(&proc->nodes))) {
5260 struct binder_node *node;
5262 node = rb_entry(n, struct binder_node, rb_node);
5265 * take a temporary ref on the node before
5266 * calling binder_node_release() which will either
5267 * kfree() the node or call binder_put_node()
5269 binder_inc_node_tmpref_ilocked(node);
5270 rb_erase(&node->rb_node, &proc->nodes);
5271 binder_inner_proc_unlock(proc);
5272 incoming_refs = binder_node_release(node, incoming_refs);
5273 binder_inner_proc_lock(proc);
5275 binder_inner_proc_unlock(proc);
5278 binder_proc_lock(proc);
5279 while ((n = rb_first(&proc->refs_by_desc))) {
5280 struct binder_ref *ref;
5282 ref = rb_entry(n, struct binder_ref, rb_node_desc);
5284 binder_cleanup_ref_olocked(ref);
5285 binder_proc_unlock(proc);
5286 binder_free_ref(ref);
5287 binder_proc_lock(proc);
5289 binder_proc_unlock(proc);
5291 binder_release_work(proc, &proc->todo);
5292 binder_release_work(proc, &proc->delivered_death);
5294 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5295 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
5296 __func__, proc->pid, threads, nodes, incoming_refs,
5297 outgoing_refs, active_transactions);
5299 binder_proc_dec_tmpref(proc);
5302 static void binder_deferred_func(struct work_struct *work)
5304 struct binder_proc *proc;
5305 struct files_struct *files;
5310 mutex_lock(&binder_deferred_lock);
5311 if (!hlist_empty(&binder_deferred_list)) {
5312 proc = hlist_entry(binder_deferred_list.first,
5313 struct binder_proc, deferred_work_node);
5314 hlist_del_init(&proc->deferred_work_node);
5315 defer = proc->deferred_work;
5316 proc->deferred_work = 0;
5321 mutex_unlock(&binder_deferred_lock);
5324 if (defer & BINDER_DEFERRED_PUT_FILES) {
5325 mutex_lock(&proc->files_lock);
5326 files = proc->files;
5329 mutex_unlock(&proc->files_lock);
5332 if (defer & BINDER_DEFERRED_FLUSH)
5333 binder_deferred_flush(proc);
5335 if (defer & BINDER_DEFERRED_RELEASE)
5336 binder_deferred_release(proc); /* frees proc */
5339 put_files_struct(files);
5342 static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
5345 binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
5347 mutex_lock(&binder_deferred_lock);
5348 proc->deferred_work |= defer;
5349 if (hlist_unhashed(&proc->deferred_work_node)) {
5350 hlist_add_head(&proc->deferred_work_node,
5351 &binder_deferred_list);
5352 queue_work(binder_deferred_workqueue, &binder_deferred_work);
5354 mutex_unlock(&binder_deferred_lock);
5357 static void print_binder_transaction_ilocked(struct seq_file *m,
5358 struct binder_proc *proc,
5360 struct binder_transaction *t)
5362 struct binder_proc *to_proc;
5363 struct binder_buffer *buffer = t->buffer;
5365 spin_lock(&t->lock);
5366 to_proc = t->to_proc;
5368 "%s %d: %pK from %d:%d to %d:%d code %x flags %x pri %d:%d r%d",
5369 prefix, t->debug_id, t,
5370 t->from ? t->from->proc->pid : 0,
5371 t->from ? t->from->pid : 0,
5372 to_proc ? to_proc->pid : 0,
5373 t->to_thread ? t->to_thread->pid : 0,
5374 t->code, t->flags, t->priority.sched_policy,
5375 t->priority.prio, t->need_reply);
5376 spin_unlock(&t->lock);
5378 if (proc != to_proc) {
5380 * Can only safely deref buffer if we are holding the
5381 * correct proc inner lock for this node
5387 if (buffer == NULL) {
5388 seq_puts(m, " buffer free\n");
5391 if (buffer->target_node)
5392 seq_printf(m, " node %d", buffer->target_node->debug_id);
5393 seq_printf(m, " size %zd:%zd data %pK\n",
5394 buffer->data_size, buffer->offsets_size,
5398 static void print_binder_work_ilocked(struct seq_file *m,
5399 struct binder_proc *proc,
5401 const char *transaction_prefix,
5402 struct binder_work *w)
5404 struct binder_node *node;
5405 struct binder_transaction *t;
5408 case BINDER_WORK_TRANSACTION:
5409 t = container_of(w, struct binder_transaction, work);
5410 print_binder_transaction_ilocked(
5411 m, proc, transaction_prefix, t);
5413 case BINDER_WORK_RETURN_ERROR: {
5414 struct binder_error *e = container_of(
5415 w, struct binder_error, work);
5417 seq_printf(m, "%stransaction error: %u\n",
5420 case BINDER_WORK_TRANSACTION_COMPLETE:
5421 seq_printf(m, "%stransaction complete\n", prefix);
5423 case BINDER_WORK_NODE:
5424 node = container_of(w, struct binder_node, work);
5425 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
5426 prefix, node->debug_id,
5427 (u64)node->ptr, (u64)node->cookie);
5429 case BINDER_WORK_DEAD_BINDER:
5430 seq_printf(m, "%shas dead binder\n", prefix);
5432 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
5433 seq_printf(m, "%shas cleared dead binder\n", prefix);
5435 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
5436 seq_printf(m, "%shas cleared death notification\n", prefix);
5439 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
5444 static void print_binder_thread_ilocked(struct seq_file *m,
5445 struct binder_thread *thread,
5448 struct binder_transaction *t;
5449 struct binder_work *w;
5450 size_t start_pos = m->count;
5453 seq_printf(m, " thread %d: l %02x need_return %d tr %d\n",
5454 thread->pid, thread->looper,
5455 thread->looper_need_return,
5456 atomic_read(&thread->tmp_ref));
5457 header_pos = m->count;
5458 t = thread->transaction_stack;
5460 if (t->from == thread) {
5461 print_binder_transaction_ilocked(m, thread->proc,
5462 " outgoing transaction", t);
5464 } else if (t->to_thread == thread) {
5465 print_binder_transaction_ilocked(m, thread->proc,
5466 " incoming transaction", t);
5469 print_binder_transaction_ilocked(m, thread->proc,
5470 " bad transaction", t);
5474 list_for_each_entry(w, &thread->todo, entry) {
5475 print_binder_work_ilocked(m, thread->proc, " ",
5476 " pending transaction", w);
5478 if (!print_always && m->count == header_pos)
5479 m->count = start_pos;
5482 static void print_binder_node_nilocked(struct seq_file *m,
5483 struct binder_node *node)
5485 struct binder_ref *ref;
5486 struct binder_work *w;
5490 hlist_for_each_entry(ref, &node->refs, node_entry)
5493 seq_printf(m, " node %d: u%016llx c%016llx pri %d:%d hs %d hw %d ls %d lw %d is %d iw %d tr %d",
5494 node->debug_id, (u64)node->ptr, (u64)node->cookie,
5495 node->sched_policy, node->min_priority,
5496 node->has_strong_ref, node->has_weak_ref,
5497 node->local_strong_refs, node->local_weak_refs,
5498 node->internal_strong_refs, count, node->tmp_refs);
5500 seq_puts(m, " proc");
5501 hlist_for_each_entry(ref, &node->refs, node_entry)
5502 seq_printf(m, " %d", ref->proc->pid);
5506 list_for_each_entry(w, &node->async_todo, entry)
5507 print_binder_work_ilocked(m, node->proc, " ",
5508 " pending async transaction", w);
5512 static void print_binder_ref_olocked(struct seq_file *m,
5513 struct binder_ref *ref)
5515 binder_node_lock(ref->node);
5516 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
5517 ref->data.debug_id, ref->data.desc,
5518 ref->node->proc ? "" : "dead ",
5519 ref->node->debug_id, ref->data.strong,
5520 ref->data.weak, ref->death);
5521 binder_node_unlock(ref->node);
5524 static void print_binder_proc(struct seq_file *m,
5525 struct binder_proc *proc, int print_all)
5527 struct binder_work *w;
5529 size_t start_pos = m->count;
5531 struct binder_node *last_node = NULL;
5533 seq_printf(m, "proc %d\n", proc->pid);
5534 seq_printf(m, "context %s\n", proc->context->name);
5535 header_pos = m->count;
5537 binder_inner_proc_lock(proc);
5538 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
5539 print_binder_thread_ilocked(m, rb_entry(n, struct binder_thread,
5540 rb_node), print_all);
5542 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
5543 struct binder_node *node = rb_entry(n, struct binder_node,
5545 if (!print_all && !node->has_async_transaction)
5549 * take a temporary reference on the node so it
5550 * survives and isn't removed from the tree
5551 * while we print it.
5553 binder_inc_node_tmpref_ilocked(node);
5554 /* Need to drop inner lock to take node lock */
5555 binder_inner_proc_unlock(proc);
5557 binder_put_node(last_node);
5558 binder_node_inner_lock(node);
5559 print_binder_node_nilocked(m, node);
5560 binder_node_inner_unlock(node);
5562 binder_inner_proc_lock(proc);
5564 binder_inner_proc_unlock(proc);
5566 binder_put_node(last_node);
5569 binder_proc_lock(proc);
5570 for (n = rb_first(&proc->refs_by_desc);
5573 print_binder_ref_olocked(m, rb_entry(n,
5576 binder_proc_unlock(proc);
5578 binder_alloc_print_allocated(m, &proc->alloc);
5579 binder_inner_proc_lock(proc);
5580 list_for_each_entry(w, &proc->todo, entry)
5581 print_binder_work_ilocked(m, proc, " ",
5582 " pending transaction", w);
5583 list_for_each_entry(w, &proc->delivered_death, entry) {
5584 seq_puts(m, " has delivered dead binder\n");
5587 binder_inner_proc_unlock(proc);
5588 if (!print_all && m->count == header_pos)
5589 m->count = start_pos;
5592 static const char * const binder_return_strings[] = {
5597 "BR_ACQUIRE_RESULT",
5599 "BR_TRANSACTION_COMPLETE",
5604 "BR_ATTEMPT_ACQUIRE",
5609 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
5613 static const char * const binder_command_strings[] = {
5616 "BC_ACQUIRE_RESULT",
5624 "BC_ATTEMPT_ACQUIRE",
5625 "BC_REGISTER_LOOPER",
5628 "BC_REQUEST_DEATH_NOTIFICATION",
5629 "BC_CLEAR_DEATH_NOTIFICATION",
5630 "BC_DEAD_BINDER_DONE",
5631 "BC_TRANSACTION_SG",
5635 static const char * const binder_objstat_strings[] = {
5642 "transaction_complete"
5645 static void print_binder_stats(struct seq_file *m, const char *prefix,
5646 struct binder_stats *stats)
5650 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
5651 ARRAY_SIZE(binder_command_strings));
5652 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
5653 int temp = atomic_read(&stats->bc[i]);
5656 seq_printf(m, "%s%s: %d\n", prefix,
5657 binder_command_strings[i], temp);
5660 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
5661 ARRAY_SIZE(binder_return_strings));
5662 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
5663 int temp = atomic_read(&stats->br[i]);
5666 seq_printf(m, "%s%s: %d\n", prefix,
5667 binder_return_strings[i], temp);
5670 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
5671 ARRAY_SIZE(binder_objstat_strings));
5672 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
5673 ARRAY_SIZE(stats->obj_deleted));
5674 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
5675 int created = atomic_read(&stats->obj_created[i]);
5676 int deleted = atomic_read(&stats->obj_deleted[i]);
5678 if (created || deleted)
5679 seq_printf(m, "%s%s: active %d total %d\n",
5681 binder_objstat_strings[i],
5687 static void print_binder_proc_stats(struct seq_file *m,
5688 struct binder_proc *proc)
5690 struct binder_work *w;
5691 struct binder_thread *thread;
5693 int count, strong, weak, ready_threads;
5694 size_t free_async_space =
5695 binder_alloc_get_free_async_space(&proc->alloc);
5697 seq_printf(m, "proc %d\n", proc->pid);
5698 seq_printf(m, "context %s\n", proc->context->name);
5701 binder_inner_proc_lock(proc);
5702 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
5705 list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node)
5708 seq_printf(m, " threads: %d\n", count);
5709 seq_printf(m, " requested threads: %d+%d/%d\n"
5710 " ready threads %d\n"
5711 " free async space %zd\n", proc->requested_threads,
5712 proc->requested_threads_started, proc->max_threads,
5716 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
5718 binder_inner_proc_unlock(proc);
5719 seq_printf(m, " nodes: %d\n", count);
5723 binder_proc_lock(proc);
5724 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
5725 struct binder_ref *ref = rb_entry(n, struct binder_ref,
5728 strong += ref->data.strong;
5729 weak += ref->data.weak;
5731 binder_proc_unlock(proc);
5732 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
5734 count = binder_alloc_get_allocated_count(&proc->alloc);
5735 seq_printf(m, " buffers: %d\n", count);
5737 binder_alloc_print_pages(m, &proc->alloc);
5740 binder_inner_proc_lock(proc);
5741 list_for_each_entry(w, &proc->todo, entry) {
5742 if (w->type == BINDER_WORK_TRANSACTION)
5745 binder_inner_proc_unlock(proc);
5746 seq_printf(m, " pending transactions: %d\n", count);
5748 print_binder_stats(m, " ", &proc->stats);
5752 static int binder_state_show(struct seq_file *m, void *unused)
5754 struct binder_proc *proc;
5755 struct binder_node *node;
5756 struct binder_node *last_node = NULL;
5758 seq_puts(m, "binder state:\n");
5760 spin_lock(&binder_dead_nodes_lock);
5761 if (!hlist_empty(&binder_dead_nodes))
5762 seq_puts(m, "dead nodes:\n");
5763 hlist_for_each_entry(node, &binder_dead_nodes, dead_node) {
5765 * take a temporary reference on the node so it
5766 * survives and isn't removed from the list
5767 * while we print it.
5770 spin_unlock(&binder_dead_nodes_lock);
5772 binder_put_node(last_node);
5773 binder_node_lock(node);
5774 print_binder_node_nilocked(m, node);
5775 binder_node_unlock(node);
5777 spin_lock(&binder_dead_nodes_lock);
5779 spin_unlock(&binder_dead_nodes_lock);
5781 binder_put_node(last_node);
5783 mutex_lock(&binder_procs_lock);
5784 hlist_for_each_entry(proc, &binder_procs, proc_node)
5785 print_binder_proc(m, proc, 1);
5786 mutex_unlock(&binder_procs_lock);
5791 static int binder_stats_show(struct seq_file *m, void *unused)
5793 struct binder_proc *proc;
5795 seq_puts(m, "binder stats:\n");
5797 print_binder_stats(m, "", &binder_stats);
5799 mutex_lock(&binder_procs_lock);
5800 hlist_for_each_entry(proc, &binder_procs, proc_node)
5801 print_binder_proc_stats(m, proc);
5802 mutex_unlock(&binder_procs_lock);
5807 static int binder_transactions_show(struct seq_file *m, void *unused)
5809 struct binder_proc *proc;
5811 seq_puts(m, "binder transactions:\n");
5812 mutex_lock(&binder_procs_lock);
5813 hlist_for_each_entry(proc, &binder_procs, proc_node)
5814 print_binder_proc(m, proc, 0);
5815 mutex_unlock(&binder_procs_lock);
5820 static int binder_proc_show(struct seq_file *m, void *unused)
5822 struct binder_proc *itr;
5823 int pid = (unsigned long)m->private;
5825 mutex_lock(&binder_procs_lock);
5826 hlist_for_each_entry(itr, &binder_procs, proc_node) {
5827 if (itr->pid == pid) {
5828 seq_puts(m, "binder proc state:\n");
5829 print_binder_proc(m, itr, 1);
5832 mutex_unlock(&binder_procs_lock);
5837 static void print_binder_transaction_log_entry(struct seq_file *m,
5838 struct binder_transaction_log_entry *e)
5840 int debug_id = READ_ONCE(e->debug_id_done);
5842 * read barrier to guarantee debug_id_done read before
5843 * we print the log values
5847 "%d: %s from %d:%d to %d:%d context %s node %d handle %d size %d:%d ret %d/%d l=%d",
5848 e->debug_id, (e->call_type == 2) ? "reply" :
5849 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
5850 e->from_thread, e->to_proc, e->to_thread, e->context_name,
5851 e->to_node, e->target_handle, e->data_size, e->offsets_size,
5852 e->return_error, e->return_error_param,
5853 e->return_error_line);
5855 * read-barrier to guarantee read of debug_id_done after
5856 * done printing the fields of the entry
5859 seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ?
5860 "\n" : " (incomplete)\n");
5863 static int binder_transaction_log_show(struct seq_file *m, void *unused)
5865 struct binder_transaction_log *log = m->private;
5866 unsigned int log_cur = atomic_read(&log->cur);
5871 count = log_cur + 1;
5872 cur = count < ARRAY_SIZE(log->entry) && !log->full ?
5873 0 : count % ARRAY_SIZE(log->entry);
5874 if (count > ARRAY_SIZE(log->entry) || log->full)
5875 count = ARRAY_SIZE(log->entry);
5876 for (i = 0; i < count; i++) {
5877 unsigned int index = cur++ % ARRAY_SIZE(log->entry);
5879 print_binder_transaction_log_entry(m, &log->entry[index]);
5884 static const struct file_operations binder_fops = {
5885 .owner = THIS_MODULE,
5886 .poll = binder_poll,
5887 .unlocked_ioctl = binder_ioctl,
5888 .compat_ioctl = binder_ioctl,
5889 .mmap = binder_mmap,
5890 .open = binder_open,
5891 .flush = binder_flush,
5892 .release = binder_release,
5895 BINDER_DEBUG_ENTRY(state);
5896 BINDER_DEBUG_ENTRY(stats);
5897 BINDER_DEBUG_ENTRY(transactions);
5898 BINDER_DEBUG_ENTRY(transaction_log);
5900 static int __init init_binder_device(const char *name)
5903 struct binder_device *binder_device;
5905 binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
5909 binder_device->miscdev.fops = &binder_fops;
5910 binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
5911 binder_device->miscdev.name = name;
5913 binder_device->context.binder_context_mgr_uid = INVALID_UID;
5914 binder_device->context.name = name;
5915 mutex_init(&binder_device->context.context_mgr_node_lock);
5917 ret = misc_register(&binder_device->miscdev);
5919 kfree(binder_device);
5923 hlist_add_head(&binder_device->hlist, &binder_devices);
5928 static int __init binder_init(void)
5931 char *device_name, *device_names, *device_tmp;
5932 struct binder_device *device;
5933 struct hlist_node *tmp;
5935 ret = binder_alloc_shrinker_init();
5939 atomic_set(&binder_transaction_log.cur, ~0U);
5940 atomic_set(&binder_transaction_log_failed.cur, ~0U);
5941 binder_deferred_workqueue = create_singlethread_workqueue("binder");
5942 if (!binder_deferred_workqueue)
5945 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
5946 if (binder_debugfs_dir_entry_root)
5947 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
5948 binder_debugfs_dir_entry_root);
5950 if (binder_debugfs_dir_entry_root) {
5951 debugfs_create_file("state",
5953 binder_debugfs_dir_entry_root,
5955 &binder_state_fops);
5956 debugfs_create_file("stats",
5958 binder_debugfs_dir_entry_root,
5960 &binder_stats_fops);
5961 debugfs_create_file("transactions",
5963 binder_debugfs_dir_entry_root,
5965 &binder_transactions_fops);
5966 debugfs_create_file("transaction_log",
5968 binder_debugfs_dir_entry_root,
5969 &binder_transaction_log,
5970 &binder_transaction_log_fops);
5971 debugfs_create_file("failed_transaction_log",
5973 binder_debugfs_dir_entry_root,
5974 &binder_transaction_log_failed,
5975 &binder_transaction_log_fops);
5979 * Copy the module_parameter string, because we don't want to
5980 * tokenize it in-place.
5982 device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL);
5983 if (!device_names) {
5985 goto err_alloc_device_names_failed;
5987 strcpy(device_names, binder_devices_param);
5989 device_tmp = device_names;
5990 while ((device_name = strsep(&device_tmp, ","))) {
5991 ret = init_binder_device(device_name);
5993 goto err_init_binder_device_failed;
5998 err_init_binder_device_failed:
5999 hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
6000 misc_deregister(&device->miscdev);
6001 hlist_del(&device->hlist);
6005 kfree(device_names);
6007 err_alloc_device_names_failed:
6008 debugfs_remove_recursive(binder_debugfs_dir_entry_root);
6010 destroy_workqueue(binder_deferred_workqueue);
6015 device_initcall(binder_init);
6017 #define CREATE_TRACE_POINTS
6018 #include "binder_trace.h"
6020 MODULE_LICENSE("GPL v2");