1 // SPDX-License-Identifier: GPL-2.0-only
4 * Android IPC Subsystem
6 * Copyright (C) 2007-2008 Google, Inc.
12 * There are 3 main spinlocks which must be acquired in the
15 * 1) proc->outer_lock : protects binder_ref
16 * binder_proc_lock() and binder_proc_unlock() are
18 * 2) node->lock : protects most fields of binder_node.
19 * binder_node_lock() and binder_node_unlock() are
21 * 3) proc->inner_lock : protects the thread and node lists
22 * (proc->threads, proc->waiting_threads, proc->nodes)
23 * and all todo lists associated with the binder_proc
24 * (proc->todo, thread->todo, proc->delivered_death and
25 * node->async_todo), as well as thread->transaction_stack
26 * binder_inner_proc_lock() and binder_inner_proc_unlock()
29 * Any lock under procA must never be nested under any lock at the same
30 * level or below on procB.
32 * Functions that require a lock held on entry indicate which lock
33 * in the suffix of the function name:
35 * foo_olocked() : requires node->outer_lock
36 * foo_nlocked() : requires node->lock
37 * foo_ilocked() : requires proc->inner_lock
38 * foo_oilocked(): requires proc->outer_lock and proc->inner_lock
39 * foo_nilocked(): requires node->lock and proc->inner_lock
43 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
45 #include <linux/fdtable.h>
46 #include <linux/file.h>
47 #include <linux/freezer.h>
49 #include <linux/list.h>
50 #include <linux/miscdevice.h>
51 #include <linux/module.h>
52 #include <linux/mutex.h>
53 #include <linux/nsproxy.h>
54 #include <linux/poll.h>
55 #include <linux/debugfs.h>
56 #include <linux/rbtree.h>
57 #include <linux/sched/signal.h>
58 #include <linux/sched/mm.h>
59 #include <linux/seq_file.h>
60 #include <linux/string.h>
61 #include <linux/uaccess.h>
62 #include <linux/pid_namespace.h>
63 #include <linux/security.h>
64 #include <linux/spinlock.h>
65 #include <linux/ratelimit.h>
66 #include <linux/syscalls.h>
67 #include <linux/task_work.h>
68 #include <linux/sizes.h>
70 #include <uapi/linux/android/binder.h>
72 #include <linux/cacheflush.h>
74 #include "binder_internal.h"
75 #include "binder_trace.h"
77 static HLIST_HEAD(binder_deferred_list);
78 static DEFINE_MUTEX(binder_deferred_lock);
80 static HLIST_HEAD(binder_devices);
81 static HLIST_HEAD(binder_procs);
82 static DEFINE_MUTEX(binder_procs_lock);
84 static HLIST_HEAD(binder_dead_nodes);
85 static DEFINE_SPINLOCK(binder_dead_nodes_lock);
87 static struct dentry *binder_debugfs_dir_entry_root;
88 static struct dentry *binder_debugfs_dir_entry_proc;
89 static atomic_t binder_last_id;
91 static int proc_show(struct seq_file *m, void *unused);
92 DEFINE_SHOW_ATTRIBUTE(proc);
94 #define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
97 BINDER_DEBUG_USER_ERROR = 1U << 0,
98 BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1,
99 BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2,
100 BINDER_DEBUG_OPEN_CLOSE = 1U << 3,
101 BINDER_DEBUG_DEAD_BINDER = 1U << 4,
102 BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5,
103 BINDER_DEBUG_READ_WRITE = 1U << 6,
104 BINDER_DEBUG_USER_REFS = 1U << 7,
105 BINDER_DEBUG_THREADS = 1U << 8,
106 BINDER_DEBUG_TRANSACTION = 1U << 9,
107 BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10,
108 BINDER_DEBUG_FREE_BUFFER = 1U << 11,
109 BINDER_DEBUG_INTERNAL_REFS = 1U << 12,
110 BINDER_DEBUG_PRIORITY_CAP = 1U << 13,
111 BINDER_DEBUG_SPINLOCKS = 1U << 14,
113 static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR |
114 BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION;
115 module_param_named(debug_mask, binder_debug_mask, uint, 0644);
117 char *binder_devices_param = CONFIG_ANDROID_BINDER_DEVICES;
118 module_param_named(devices, binder_devices_param, charp, 0444);
120 static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
121 static int binder_stop_on_user_error;
123 static int binder_set_stop_on_user_error(const char *val,
124 const struct kernel_param *kp)
128 ret = param_set_int(val, kp);
129 if (binder_stop_on_user_error < 2)
130 wake_up(&binder_user_error_wait);
133 module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
134 param_get_int, &binder_stop_on_user_error, 0644);
136 static __printf(2, 3) void binder_debug(int mask, const char *format, ...)
138 struct va_format vaf;
141 if (binder_debug_mask & mask) {
142 va_start(args, format);
145 pr_info_ratelimited("%pV", &vaf);
150 #define binder_txn_error(x...) \
151 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, x)
153 static __printf(1, 2) void binder_user_error(const char *format, ...)
155 struct va_format vaf;
158 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) {
159 va_start(args, format);
162 pr_info_ratelimited("%pV", &vaf);
166 if (binder_stop_on_user_error)
167 binder_stop_on_user_error = 2;
170 #define binder_set_extended_error(ee, _id, _command, _param) \
173 (ee)->command = _command; \
174 (ee)->param = _param; \
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 static struct binder_stats binder_stats;
190 static inline void binder_stats_deleted(enum binder_stat_types type)
192 atomic_inc(&binder_stats.obj_deleted[type]);
195 static inline void binder_stats_created(enum binder_stat_types type)
197 atomic_inc(&binder_stats.obj_created[type]);
200 struct binder_transaction_log binder_transaction_log;
201 struct binder_transaction_log binder_transaction_log_failed;
203 static struct binder_transaction_log_entry *binder_transaction_log_add(
204 struct binder_transaction_log *log)
206 struct binder_transaction_log_entry *e;
207 unsigned int cur = atomic_inc_return(&log->cur);
209 if (cur >= ARRAY_SIZE(log->entry))
211 e = &log->entry[cur % ARRAY_SIZE(log->entry)];
212 WRITE_ONCE(e->debug_id_done, 0);
214 * write-barrier to synchronize access to e->debug_id_done.
215 * We make sure the initialized 0 value is seen before
216 * memset() other fields are zeroed by memset.
219 memset(e, 0, sizeof(*e));
223 enum binder_deferred_state {
224 BINDER_DEFERRED_FLUSH = 0x01,
225 BINDER_DEFERRED_RELEASE = 0x02,
229 BINDER_LOOPER_STATE_REGISTERED = 0x01,
230 BINDER_LOOPER_STATE_ENTERED = 0x02,
231 BINDER_LOOPER_STATE_EXITED = 0x04,
232 BINDER_LOOPER_STATE_INVALID = 0x08,
233 BINDER_LOOPER_STATE_WAITING = 0x10,
234 BINDER_LOOPER_STATE_POLL = 0x20,
238 * binder_proc_lock() - Acquire outer lock for given binder_proc
239 * @proc: struct binder_proc to acquire
241 * Acquires proc->outer_lock. Used to protect binder_ref
242 * structures associated with the given proc.
244 #define binder_proc_lock(proc) _binder_proc_lock(proc, __LINE__)
246 _binder_proc_lock(struct binder_proc *proc, int line)
247 __acquires(&proc->outer_lock)
249 binder_debug(BINDER_DEBUG_SPINLOCKS,
250 "%s: line=%d\n", __func__, line);
251 spin_lock(&proc->outer_lock);
255 * binder_proc_unlock() - Release spinlock for given binder_proc
256 * @proc: struct binder_proc to acquire
258 * Release lock acquired via binder_proc_lock()
260 #define binder_proc_unlock(_proc) _binder_proc_unlock(_proc, __LINE__)
262 _binder_proc_unlock(struct binder_proc *proc, int line)
263 __releases(&proc->outer_lock)
265 binder_debug(BINDER_DEBUG_SPINLOCKS,
266 "%s: line=%d\n", __func__, line);
267 spin_unlock(&proc->outer_lock);
271 * binder_inner_proc_lock() - Acquire inner lock for given binder_proc
272 * @proc: struct binder_proc to acquire
274 * Acquires proc->inner_lock. Used to protect todo lists
276 #define binder_inner_proc_lock(proc) _binder_inner_proc_lock(proc, __LINE__)
278 _binder_inner_proc_lock(struct binder_proc *proc, int line)
279 __acquires(&proc->inner_lock)
281 binder_debug(BINDER_DEBUG_SPINLOCKS,
282 "%s: line=%d\n", __func__, line);
283 spin_lock(&proc->inner_lock);
287 * binder_inner_proc_unlock() - Release inner lock for given binder_proc
288 * @proc: struct binder_proc to acquire
290 * Release lock acquired via binder_inner_proc_lock()
292 #define binder_inner_proc_unlock(proc) _binder_inner_proc_unlock(proc, __LINE__)
294 _binder_inner_proc_unlock(struct binder_proc *proc, int line)
295 __releases(&proc->inner_lock)
297 binder_debug(BINDER_DEBUG_SPINLOCKS,
298 "%s: line=%d\n", __func__, line);
299 spin_unlock(&proc->inner_lock);
303 * binder_node_lock() - Acquire spinlock for given binder_node
304 * @node: struct binder_node to acquire
306 * Acquires node->lock. Used to protect binder_node fields
308 #define binder_node_lock(node) _binder_node_lock(node, __LINE__)
310 _binder_node_lock(struct binder_node *node, int line)
311 __acquires(&node->lock)
313 binder_debug(BINDER_DEBUG_SPINLOCKS,
314 "%s: line=%d\n", __func__, line);
315 spin_lock(&node->lock);
319 * binder_node_unlock() - Release spinlock for given binder_proc
320 * @node: struct binder_node to acquire
322 * Release lock acquired via binder_node_lock()
324 #define binder_node_unlock(node) _binder_node_unlock(node, __LINE__)
326 _binder_node_unlock(struct binder_node *node, int line)
327 __releases(&node->lock)
329 binder_debug(BINDER_DEBUG_SPINLOCKS,
330 "%s: line=%d\n", __func__, line);
331 spin_unlock(&node->lock);
335 * binder_node_inner_lock() - Acquire node and inner locks
336 * @node: struct binder_node to acquire
338 * Acquires node->lock. If node->proc also acquires
339 * proc->inner_lock. Used to protect binder_node fields
341 #define binder_node_inner_lock(node) _binder_node_inner_lock(node, __LINE__)
343 _binder_node_inner_lock(struct binder_node *node, int line)
344 __acquires(&node->lock) __acquires(&node->proc->inner_lock)
346 binder_debug(BINDER_DEBUG_SPINLOCKS,
347 "%s: line=%d\n", __func__, line);
348 spin_lock(&node->lock);
350 binder_inner_proc_lock(node->proc);
352 /* annotation for sparse */
353 __acquire(&node->proc->inner_lock);
357 * binder_node_unlock() - Release node and inner locks
358 * @node: struct binder_node to acquire
360 * Release lock acquired via binder_node_lock()
362 #define binder_node_inner_unlock(node) _binder_node_inner_unlock(node, __LINE__)
364 _binder_node_inner_unlock(struct binder_node *node, int line)
365 __releases(&node->lock) __releases(&node->proc->inner_lock)
367 struct binder_proc *proc = node->proc;
369 binder_debug(BINDER_DEBUG_SPINLOCKS,
370 "%s: line=%d\n", __func__, line);
372 binder_inner_proc_unlock(proc);
374 /* annotation for sparse */
375 __release(&node->proc->inner_lock);
376 spin_unlock(&node->lock);
379 static bool binder_worklist_empty_ilocked(struct list_head *list)
381 return list_empty(list);
385 * binder_worklist_empty() - Check if no items on the work list
386 * @proc: binder_proc associated with list
387 * @list: list to check
389 * Return: true if there are no items on list, else false
391 static bool binder_worklist_empty(struct binder_proc *proc,
392 struct list_head *list)
396 binder_inner_proc_lock(proc);
397 ret = binder_worklist_empty_ilocked(list);
398 binder_inner_proc_unlock(proc);
403 * binder_enqueue_work_ilocked() - Add an item to the work list
404 * @work: struct binder_work to add to list
405 * @target_list: list to add work to
407 * Adds the work to the specified list. Asserts that work
408 * is not already on a list.
410 * Requires the proc->inner_lock to be held.
413 binder_enqueue_work_ilocked(struct binder_work *work,
414 struct list_head *target_list)
416 BUG_ON(target_list == NULL);
417 BUG_ON(work->entry.next && !list_empty(&work->entry));
418 list_add_tail(&work->entry, target_list);
422 * binder_enqueue_deferred_thread_work_ilocked() - Add deferred thread work
423 * @thread: thread to queue work to
424 * @work: struct binder_work to add to list
426 * Adds the work to the todo list of the thread. Doesn't set the process_todo
427 * flag, which means that (if it wasn't already set) the thread will go to
428 * sleep without handling this work when it calls read.
430 * Requires the proc->inner_lock to be held.
433 binder_enqueue_deferred_thread_work_ilocked(struct binder_thread *thread,
434 struct binder_work *work)
436 WARN_ON(!list_empty(&thread->waiting_thread_node));
437 binder_enqueue_work_ilocked(work, &thread->todo);
441 * binder_enqueue_thread_work_ilocked() - Add an item to the thread work list
442 * @thread: thread to queue work to
443 * @work: struct binder_work to add to list
445 * Adds the work to the todo list of the thread, and enables processing
448 * Requires the proc->inner_lock to be held.
451 binder_enqueue_thread_work_ilocked(struct binder_thread *thread,
452 struct binder_work *work)
454 WARN_ON(!list_empty(&thread->waiting_thread_node));
455 binder_enqueue_work_ilocked(work, &thread->todo);
456 thread->process_todo = true;
460 * binder_enqueue_thread_work() - Add an item to the thread work list
461 * @thread: thread to queue work to
462 * @work: struct binder_work to add to list
464 * Adds the work to the todo list of the thread, and enables processing
468 binder_enqueue_thread_work(struct binder_thread *thread,
469 struct binder_work *work)
471 binder_inner_proc_lock(thread->proc);
472 binder_enqueue_thread_work_ilocked(thread, work);
473 binder_inner_proc_unlock(thread->proc);
477 binder_dequeue_work_ilocked(struct binder_work *work)
479 list_del_init(&work->entry);
483 * binder_dequeue_work() - Removes an item from the work list
484 * @proc: binder_proc associated with list
485 * @work: struct binder_work to remove from list
487 * Removes the specified work item from whatever list it is on.
488 * Can safely be called if work is not on any list.
491 binder_dequeue_work(struct binder_proc *proc, struct binder_work *work)
493 binder_inner_proc_lock(proc);
494 binder_dequeue_work_ilocked(work);
495 binder_inner_proc_unlock(proc);
498 static struct binder_work *binder_dequeue_work_head_ilocked(
499 struct list_head *list)
501 struct binder_work *w;
503 w = list_first_entry_or_null(list, struct binder_work, entry);
505 list_del_init(&w->entry);
510 binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
511 static void binder_free_thread(struct binder_thread *thread);
512 static void binder_free_proc(struct binder_proc *proc);
513 static void binder_inc_node_tmpref_ilocked(struct binder_node *node);
515 static bool binder_has_work_ilocked(struct binder_thread *thread,
518 return thread->process_todo ||
519 thread->looper_need_return ||
521 !binder_worklist_empty_ilocked(&thread->proc->todo));
524 static bool binder_has_work(struct binder_thread *thread, bool do_proc_work)
528 binder_inner_proc_lock(thread->proc);
529 has_work = binder_has_work_ilocked(thread, do_proc_work);
530 binder_inner_proc_unlock(thread->proc);
535 static bool binder_available_for_proc_work_ilocked(struct binder_thread *thread)
537 return !thread->transaction_stack &&
538 binder_worklist_empty_ilocked(&thread->todo) &&
539 (thread->looper & (BINDER_LOOPER_STATE_ENTERED |
540 BINDER_LOOPER_STATE_REGISTERED));
543 static void binder_wakeup_poll_threads_ilocked(struct binder_proc *proc,
547 struct binder_thread *thread;
549 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
550 thread = rb_entry(n, struct binder_thread, rb_node);
551 if (thread->looper & BINDER_LOOPER_STATE_POLL &&
552 binder_available_for_proc_work_ilocked(thread)) {
554 wake_up_interruptible_sync(&thread->wait);
556 wake_up_interruptible(&thread->wait);
562 * binder_select_thread_ilocked() - selects a thread for doing proc work.
563 * @proc: process to select a thread from
565 * Note that calling this function moves the thread off the waiting_threads
566 * list, so it can only be woken up by the caller of this function, or a
567 * signal. Therefore, callers *should* always wake up the thread this function
570 * Return: If there's a thread currently waiting for process work,
571 * returns that thread. Otherwise returns NULL.
573 static struct binder_thread *
574 binder_select_thread_ilocked(struct binder_proc *proc)
576 struct binder_thread *thread;
578 assert_spin_locked(&proc->inner_lock);
579 thread = list_first_entry_or_null(&proc->waiting_threads,
580 struct binder_thread,
581 waiting_thread_node);
584 list_del_init(&thread->waiting_thread_node);
590 * binder_wakeup_thread_ilocked() - wakes up a thread for doing proc work.
591 * @proc: process to wake up a thread in
592 * @thread: specific thread to wake-up (may be NULL)
593 * @sync: whether to do a synchronous wake-up
595 * This function wakes up a thread in the @proc process.
596 * The caller may provide a specific thread to wake-up in
597 * the @thread parameter. If @thread is NULL, this function
598 * will wake up threads that have called poll().
600 * Note that for this function to work as expected, callers
601 * should first call binder_select_thread() to find a thread
602 * to handle the work (if they don't have a thread already),
603 * and pass the result into the @thread parameter.
605 static void binder_wakeup_thread_ilocked(struct binder_proc *proc,
606 struct binder_thread *thread,
609 assert_spin_locked(&proc->inner_lock);
613 wake_up_interruptible_sync(&thread->wait);
615 wake_up_interruptible(&thread->wait);
619 /* Didn't find a thread waiting for proc work; this can happen
621 * 1. All threads are busy handling transactions
622 * In that case, one of those threads should call back into
623 * the kernel driver soon and pick up this work.
624 * 2. Threads are using the (e)poll interface, in which case
625 * they may be blocked on the waitqueue without having been
626 * added to waiting_threads. For this case, we just iterate
627 * over all threads not handling transaction work, and
628 * wake them all up. We wake all because we don't know whether
629 * a thread that called into (e)poll is handling non-binder
632 binder_wakeup_poll_threads_ilocked(proc, sync);
635 static void binder_wakeup_proc_ilocked(struct binder_proc *proc)
637 struct binder_thread *thread = binder_select_thread_ilocked(proc);
639 binder_wakeup_thread_ilocked(proc, thread, /* sync = */false);
642 static void binder_set_nice(long nice)
646 if (can_nice(current, nice)) {
647 set_user_nice(current, nice);
650 min_nice = rlimit_to_nice(rlimit(RLIMIT_NICE));
651 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
652 "%d: nice value %ld not allowed use %ld instead\n",
653 current->pid, nice, min_nice);
654 set_user_nice(current, min_nice);
655 if (min_nice <= MAX_NICE)
657 binder_user_error("%d RLIMIT_NICE not set\n", current->pid);
660 static struct binder_node *binder_get_node_ilocked(struct binder_proc *proc,
661 binder_uintptr_t ptr)
663 struct rb_node *n = proc->nodes.rb_node;
664 struct binder_node *node;
666 assert_spin_locked(&proc->inner_lock);
669 node = rb_entry(n, struct binder_node, rb_node);
673 else if (ptr > node->ptr)
677 * take an implicit weak reference
678 * to ensure node stays alive until
679 * call to binder_put_node()
681 binder_inc_node_tmpref_ilocked(node);
688 static struct binder_node *binder_get_node(struct binder_proc *proc,
689 binder_uintptr_t ptr)
691 struct binder_node *node;
693 binder_inner_proc_lock(proc);
694 node = binder_get_node_ilocked(proc, ptr);
695 binder_inner_proc_unlock(proc);
699 static struct binder_node *binder_init_node_ilocked(
700 struct binder_proc *proc,
701 struct binder_node *new_node,
702 struct flat_binder_object *fp)
704 struct rb_node **p = &proc->nodes.rb_node;
705 struct rb_node *parent = NULL;
706 struct binder_node *node;
707 binder_uintptr_t ptr = fp ? fp->binder : 0;
708 binder_uintptr_t cookie = fp ? fp->cookie : 0;
709 __u32 flags = fp ? fp->flags : 0;
711 assert_spin_locked(&proc->inner_lock);
716 node = rb_entry(parent, struct binder_node, rb_node);
720 else if (ptr > node->ptr)
724 * A matching node is already in
725 * the rb tree. Abandon the init
728 binder_inc_node_tmpref_ilocked(node);
733 binder_stats_created(BINDER_STAT_NODE);
735 rb_link_node(&node->rb_node, parent, p);
736 rb_insert_color(&node->rb_node, &proc->nodes);
737 node->debug_id = atomic_inc_return(&binder_last_id);
740 node->cookie = cookie;
741 node->work.type = BINDER_WORK_NODE;
742 node->min_priority = flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
743 node->accept_fds = !!(flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
744 node->txn_security_ctx = !!(flags & FLAT_BINDER_FLAG_TXN_SECURITY_CTX);
745 spin_lock_init(&node->lock);
746 INIT_LIST_HEAD(&node->work.entry);
747 INIT_LIST_HEAD(&node->async_todo);
748 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
749 "%d:%d node %d u%016llx c%016llx created\n",
750 proc->pid, current->pid, node->debug_id,
751 (u64)node->ptr, (u64)node->cookie);
756 static struct binder_node *binder_new_node(struct binder_proc *proc,
757 struct flat_binder_object *fp)
759 struct binder_node *node;
760 struct binder_node *new_node = kzalloc(sizeof(*node), GFP_KERNEL);
764 binder_inner_proc_lock(proc);
765 node = binder_init_node_ilocked(proc, new_node, fp);
766 binder_inner_proc_unlock(proc);
767 if (node != new_node)
769 * The node was already added by another thread
776 static void binder_free_node(struct binder_node *node)
779 binder_stats_deleted(BINDER_STAT_NODE);
782 static int binder_inc_node_nilocked(struct binder_node *node, int strong,
784 struct list_head *target_list)
786 struct binder_proc *proc = node->proc;
788 assert_spin_locked(&node->lock);
790 assert_spin_locked(&proc->inner_lock);
793 if (target_list == NULL &&
794 node->internal_strong_refs == 0 &&
796 node == node->proc->context->binder_context_mgr_node &&
797 node->has_strong_ref)) {
798 pr_err("invalid inc strong node for %d\n",
802 node->internal_strong_refs++;
804 node->local_strong_refs++;
805 if (!node->has_strong_ref && target_list) {
806 struct binder_thread *thread = container_of(target_list,
807 struct binder_thread, todo);
808 binder_dequeue_work_ilocked(&node->work);
809 BUG_ON(&thread->todo != target_list);
810 binder_enqueue_deferred_thread_work_ilocked(thread,
815 node->local_weak_refs++;
816 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
817 if (target_list == NULL) {
818 pr_err("invalid inc weak node for %d\n",
825 binder_enqueue_work_ilocked(&node->work, target_list);
831 static int binder_inc_node(struct binder_node *node, int strong, int internal,
832 struct list_head *target_list)
836 binder_node_inner_lock(node);
837 ret = binder_inc_node_nilocked(node, strong, internal, target_list);
838 binder_node_inner_unlock(node);
843 static bool binder_dec_node_nilocked(struct binder_node *node,
844 int strong, int internal)
846 struct binder_proc *proc = node->proc;
848 assert_spin_locked(&node->lock);
850 assert_spin_locked(&proc->inner_lock);
853 node->internal_strong_refs--;
855 node->local_strong_refs--;
856 if (node->local_strong_refs || node->internal_strong_refs)
860 node->local_weak_refs--;
861 if (node->local_weak_refs || node->tmp_refs ||
862 !hlist_empty(&node->refs))
866 if (proc && (node->has_strong_ref || node->has_weak_ref)) {
867 if (list_empty(&node->work.entry)) {
868 binder_enqueue_work_ilocked(&node->work, &proc->todo);
869 binder_wakeup_proc_ilocked(proc);
872 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
873 !node->local_weak_refs && !node->tmp_refs) {
875 binder_dequeue_work_ilocked(&node->work);
876 rb_erase(&node->rb_node, &proc->nodes);
877 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
878 "refless node %d deleted\n",
881 BUG_ON(!list_empty(&node->work.entry));
882 spin_lock(&binder_dead_nodes_lock);
884 * tmp_refs could have changed so
887 if (node->tmp_refs) {
888 spin_unlock(&binder_dead_nodes_lock);
891 hlist_del(&node->dead_node);
892 spin_unlock(&binder_dead_nodes_lock);
893 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
894 "dead node %d deleted\n",
903 static void binder_dec_node(struct binder_node *node, int strong, int internal)
907 binder_node_inner_lock(node);
908 free_node = binder_dec_node_nilocked(node, strong, internal);
909 binder_node_inner_unlock(node);
911 binder_free_node(node);
914 static void binder_inc_node_tmpref_ilocked(struct binder_node *node)
917 * No call to binder_inc_node() is needed since we
918 * don't need to inform userspace of any changes to
925 * binder_inc_node_tmpref() - take a temporary reference on node
926 * @node: node to reference
928 * Take reference on node to prevent the node from being freed
929 * while referenced only by a local variable. The inner lock is
930 * needed to serialize with the node work on the queue (which
931 * isn't needed after the node is dead). If the node is dead
932 * (node->proc is NULL), use binder_dead_nodes_lock to protect
933 * node->tmp_refs against dead-node-only cases where the node
934 * lock cannot be acquired (eg traversing the dead node list to
937 static void binder_inc_node_tmpref(struct binder_node *node)
939 binder_node_lock(node);
941 binder_inner_proc_lock(node->proc);
943 spin_lock(&binder_dead_nodes_lock);
944 binder_inc_node_tmpref_ilocked(node);
946 binder_inner_proc_unlock(node->proc);
948 spin_unlock(&binder_dead_nodes_lock);
949 binder_node_unlock(node);
953 * binder_dec_node_tmpref() - remove a temporary reference on node
954 * @node: node to reference
956 * Release temporary reference on node taken via binder_inc_node_tmpref()
958 static void binder_dec_node_tmpref(struct binder_node *node)
962 binder_node_inner_lock(node);
964 spin_lock(&binder_dead_nodes_lock);
966 __acquire(&binder_dead_nodes_lock);
968 BUG_ON(node->tmp_refs < 0);
970 spin_unlock(&binder_dead_nodes_lock);
972 __release(&binder_dead_nodes_lock);
974 * Call binder_dec_node() to check if all refcounts are 0
975 * and cleanup is needed. Calling with strong=0 and internal=1
976 * causes no actual reference to be released in binder_dec_node().
977 * If that changes, a change is needed here too.
979 free_node = binder_dec_node_nilocked(node, 0, 1);
980 binder_node_inner_unlock(node);
982 binder_free_node(node);
985 static void binder_put_node(struct binder_node *node)
987 binder_dec_node_tmpref(node);
990 static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc,
991 u32 desc, bool need_strong_ref)
993 struct rb_node *n = proc->refs_by_desc.rb_node;
994 struct binder_ref *ref;
997 ref = rb_entry(n, struct binder_ref, rb_node_desc);
999 if (desc < ref->data.desc) {
1001 } else if (desc > ref->data.desc) {
1003 } else if (need_strong_ref && !ref->data.strong) {
1004 binder_user_error("tried to use weak ref as strong ref\n");
1014 * binder_get_ref_for_node_olocked() - get the ref associated with given node
1015 * @proc: binder_proc that owns the ref
1016 * @node: binder_node of target
1017 * @new_ref: newly allocated binder_ref to be initialized or %NULL
1019 * Look up the ref for the given node and return it if it exists
1021 * If it doesn't exist and the caller provides a newly allocated
1022 * ref, initialize the fields of the newly allocated ref and insert
1023 * into the given proc rb_trees and node refs list.
1025 * Return: the ref for node. It is possible that another thread
1026 * allocated/initialized the ref first in which case the
1027 * returned ref would be different than the passed-in
1028 * new_ref. new_ref must be kfree'd by the caller in
1031 static struct binder_ref *binder_get_ref_for_node_olocked(
1032 struct binder_proc *proc,
1033 struct binder_node *node,
1034 struct binder_ref *new_ref)
1036 struct binder_context *context = proc->context;
1037 struct rb_node **p = &proc->refs_by_node.rb_node;
1038 struct rb_node *parent = NULL;
1039 struct binder_ref *ref;
1044 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1046 if (node < ref->node)
1048 else if (node > ref->node)
1049 p = &(*p)->rb_right;
1056 binder_stats_created(BINDER_STAT_REF);
1057 new_ref->data.debug_id = atomic_inc_return(&binder_last_id);
1058 new_ref->proc = proc;
1059 new_ref->node = node;
1060 rb_link_node(&new_ref->rb_node_node, parent, p);
1061 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1063 new_ref->data.desc = (node == context->binder_context_mgr_node) ? 0 : 1;
1064 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1065 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1066 if (ref->data.desc > new_ref->data.desc)
1068 new_ref->data.desc = ref->data.desc + 1;
1071 p = &proc->refs_by_desc.rb_node;
1074 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1076 if (new_ref->data.desc < ref->data.desc)
1078 else if (new_ref->data.desc > ref->data.desc)
1079 p = &(*p)->rb_right;
1083 rb_link_node(&new_ref->rb_node_desc, parent, p);
1084 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
1086 binder_node_lock(node);
1087 hlist_add_head(&new_ref->node_entry, &node->refs);
1089 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1090 "%d new ref %d desc %d for node %d\n",
1091 proc->pid, new_ref->data.debug_id, new_ref->data.desc,
1093 binder_node_unlock(node);
1097 static void binder_cleanup_ref_olocked(struct binder_ref *ref)
1099 bool delete_node = false;
1101 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1102 "%d delete ref %d desc %d for node %d\n",
1103 ref->proc->pid, ref->data.debug_id, ref->data.desc,
1104 ref->node->debug_id);
1106 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1107 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
1109 binder_node_inner_lock(ref->node);
1110 if (ref->data.strong)
1111 binder_dec_node_nilocked(ref->node, 1, 1);
1113 hlist_del(&ref->node_entry);
1114 delete_node = binder_dec_node_nilocked(ref->node, 0, 1);
1115 binder_node_inner_unlock(ref->node);
1117 * Clear ref->node unless we want the caller to free the node
1121 * The caller uses ref->node to determine
1122 * whether the node needs to be freed. Clear
1123 * it since the node is still alive.
1129 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1130 "%d delete ref %d desc %d has death notification\n",
1131 ref->proc->pid, ref->data.debug_id,
1133 binder_dequeue_work(ref->proc, &ref->death->work);
1134 binder_stats_deleted(BINDER_STAT_DEATH);
1136 binder_stats_deleted(BINDER_STAT_REF);
1140 * binder_inc_ref_olocked() - increment the ref for given handle
1141 * @ref: ref to be incremented
1142 * @strong: if true, strong increment, else weak
1143 * @target_list: list to queue node work on
1145 * Increment the ref. @ref->proc->outer_lock must be held on entry
1147 * Return: 0, if successful, else errno
1149 static int binder_inc_ref_olocked(struct binder_ref *ref, int strong,
1150 struct list_head *target_list)
1155 if (ref->data.strong == 0) {
1156 ret = binder_inc_node(ref->node, 1, 1, target_list);
1162 if (ref->data.weak == 0) {
1163 ret = binder_inc_node(ref->node, 0, 1, target_list);
1173 * binder_dec_ref() - dec the ref for given handle
1174 * @ref: ref to be decremented
1175 * @strong: if true, strong decrement, else weak
1177 * Decrement the ref.
1179 * Return: true if ref is cleaned up and ready to be freed
1181 static bool binder_dec_ref_olocked(struct binder_ref *ref, int strong)
1184 if (ref->data.strong == 0) {
1185 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
1186 ref->proc->pid, ref->data.debug_id,
1187 ref->data.desc, ref->data.strong,
1192 if (ref->data.strong == 0)
1193 binder_dec_node(ref->node, strong, 1);
1195 if (ref->data.weak == 0) {
1196 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
1197 ref->proc->pid, ref->data.debug_id,
1198 ref->data.desc, ref->data.strong,
1204 if (ref->data.strong == 0 && ref->data.weak == 0) {
1205 binder_cleanup_ref_olocked(ref);
1212 * binder_get_node_from_ref() - get the node from the given proc/desc
1213 * @proc: proc containing the ref
1214 * @desc: the handle associated with the ref
1215 * @need_strong_ref: if true, only return node if ref is strong
1216 * @rdata: the id/refcount data for the ref
1218 * Given a proc and ref handle, return the associated binder_node
1220 * Return: a binder_node or NULL if not found or not strong when strong required
1222 static struct binder_node *binder_get_node_from_ref(
1223 struct binder_proc *proc,
1224 u32 desc, bool need_strong_ref,
1225 struct binder_ref_data *rdata)
1227 struct binder_node *node;
1228 struct binder_ref *ref;
1230 binder_proc_lock(proc);
1231 ref = binder_get_ref_olocked(proc, desc, need_strong_ref);
1236 * Take an implicit reference on the node to ensure
1237 * it stays alive until the call to binder_put_node()
1239 binder_inc_node_tmpref(node);
1242 binder_proc_unlock(proc);
1247 binder_proc_unlock(proc);
1252 * binder_free_ref() - free the binder_ref
1255 * Free the binder_ref. Free the binder_node indicated by ref->node
1256 * (if non-NULL) and the binder_ref_death indicated by ref->death.
1258 static void binder_free_ref(struct binder_ref *ref)
1261 binder_free_node(ref->node);
1267 * binder_update_ref_for_handle() - inc/dec the ref for given handle
1268 * @proc: proc containing the ref
1269 * @desc: the handle associated with the ref
1270 * @increment: true=inc reference, false=dec reference
1271 * @strong: true=strong reference, false=weak reference
1272 * @rdata: the id/refcount data for the ref
1274 * Given a proc and ref handle, increment or decrement the ref
1275 * according to "increment" arg.
1277 * Return: 0 if successful, else errno
1279 static int binder_update_ref_for_handle(struct binder_proc *proc,
1280 uint32_t desc, bool increment, bool strong,
1281 struct binder_ref_data *rdata)
1284 struct binder_ref *ref;
1285 bool delete_ref = false;
1287 binder_proc_lock(proc);
1288 ref = binder_get_ref_olocked(proc, desc, strong);
1294 ret = binder_inc_ref_olocked(ref, strong, NULL);
1296 delete_ref = binder_dec_ref_olocked(ref, strong);
1300 binder_proc_unlock(proc);
1303 binder_free_ref(ref);
1307 binder_proc_unlock(proc);
1312 * binder_dec_ref_for_handle() - dec the ref for given handle
1313 * @proc: proc containing the ref
1314 * @desc: the handle associated with the ref
1315 * @strong: true=strong reference, false=weak reference
1316 * @rdata: the id/refcount data for the ref
1318 * Just calls binder_update_ref_for_handle() to decrement the ref.
1320 * Return: 0 if successful, else errno
1322 static int binder_dec_ref_for_handle(struct binder_proc *proc,
1323 uint32_t desc, bool strong, struct binder_ref_data *rdata)
1325 return binder_update_ref_for_handle(proc, desc, false, strong, rdata);
1330 * binder_inc_ref_for_node() - increment the ref for given proc/node
1331 * @proc: proc containing the ref
1332 * @node: target node
1333 * @strong: true=strong reference, false=weak reference
1334 * @target_list: worklist to use if node is incremented
1335 * @rdata: the id/refcount data for the ref
1337 * Given a proc and node, increment the ref. Create the ref if it
1338 * doesn't already exist
1340 * Return: 0 if successful, else errno
1342 static int binder_inc_ref_for_node(struct binder_proc *proc,
1343 struct binder_node *node,
1345 struct list_head *target_list,
1346 struct binder_ref_data *rdata)
1348 struct binder_ref *ref;
1349 struct binder_ref *new_ref = NULL;
1352 binder_proc_lock(proc);
1353 ref = binder_get_ref_for_node_olocked(proc, node, NULL);
1355 binder_proc_unlock(proc);
1356 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1359 binder_proc_lock(proc);
1360 ref = binder_get_ref_for_node_olocked(proc, node, new_ref);
1362 ret = binder_inc_ref_olocked(ref, strong, target_list);
1364 binder_proc_unlock(proc);
1365 if (new_ref && ref != new_ref)
1367 * Another thread created the ref first so
1368 * free the one we allocated
1374 static void binder_pop_transaction_ilocked(struct binder_thread *target_thread,
1375 struct binder_transaction *t)
1377 BUG_ON(!target_thread);
1378 assert_spin_locked(&target_thread->proc->inner_lock);
1379 BUG_ON(target_thread->transaction_stack != t);
1380 BUG_ON(target_thread->transaction_stack->from != target_thread);
1381 target_thread->transaction_stack =
1382 target_thread->transaction_stack->from_parent;
1387 * binder_thread_dec_tmpref() - decrement thread->tmp_ref
1388 * @thread: thread to decrement
1390 * A thread needs to be kept alive while being used to create or
1391 * handle a transaction. binder_get_txn_from() is used to safely
1392 * extract t->from from a binder_transaction and keep the thread
1393 * indicated by t->from from being freed. When done with that
1394 * binder_thread, this function is called to decrement the
1395 * tmp_ref and free if appropriate (thread has been released
1396 * and no transaction being processed by the driver)
1398 static void binder_thread_dec_tmpref(struct binder_thread *thread)
1401 * atomic is used to protect the counter value while
1402 * it cannot reach zero or thread->is_dead is false
1404 binder_inner_proc_lock(thread->proc);
1405 atomic_dec(&thread->tmp_ref);
1406 if (thread->is_dead && !atomic_read(&thread->tmp_ref)) {
1407 binder_inner_proc_unlock(thread->proc);
1408 binder_free_thread(thread);
1411 binder_inner_proc_unlock(thread->proc);
1415 * binder_proc_dec_tmpref() - decrement proc->tmp_ref
1416 * @proc: proc to decrement
1418 * A binder_proc needs to be kept alive while being used to create or
1419 * handle a transaction. proc->tmp_ref is incremented when
1420 * creating a new transaction or the binder_proc is currently in-use
1421 * by threads that are being released. When done with the binder_proc,
1422 * this function is called to decrement the counter and free the
1423 * proc if appropriate (proc has been released, all threads have
1424 * been released and not currenly in-use to process a transaction).
1426 static void binder_proc_dec_tmpref(struct binder_proc *proc)
1428 binder_inner_proc_lock(proc);
1430 if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) &&
1432 binder_inner_proc_unlock(proc);
1433 binder_free_proc(proc);
1436 binder_inner_proc_unlock(proc);
1440 * binder_get_txn_from() - safely extract the "from" thread in transaction
1441 * @t: binder transaction for t->from
1443 * Atomically return the "from" thread and increment the tmp_ref
1444 * count for the thread to ensure it stays alive until
1445 * binder_thread_dec_tmpref() is called.
1447 * Return: the value of t->from
1449 static struct binder_thread *binder_get_txn_from(
1450 struct binder_transaction *t)
1452 struct binder_thread *from;
1454 spin_lock(&t->lock);
1457 atomic_inc(&from->tmp_ref);
1458 spin_unlock(&t->lock);
1463 * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock
1464 * @t: binder transaction for t->from
1466 * Same as binder_get_txn_from() except it also acquires the proc->inner_lock
1467 * to guarantee that the thread cannot be released while operating on it.
1468 * The caller must call binder_inner_proc_unlock() to release the inner lock
1469 * as well as call binder_dec_thread_txn() to release the reference.
1471 * Return: the value of t->from
1473 static struct binder_thread *binder_get_txn_from_and_acq_inner(
1474 struct binder_transaction *t)
1475 __acquires(&t->from->proc->inner_lock)
1477 struct binder_thread *from;
1479 from = binder_get_txn_from(t);
1481 __acquire(&from->proc->inner_lock);
1484 binder_inner_proc_lock(from->proc);
1486 BUG_ON(from != t->from);
1489 binder_inner_proc_unlock(from->proc);
1490 __acquire(&from->proc->inner_lock);
1491 binder_thread_dec_tmpref(from);
1496 * binder_free_txn_fixups() - free unprocessed fd fixups
1497 * @t: binder transaction for t->from
1499 * If the transaction is being torn down prior to being
1500 * processed by the target process, free all of the
1501 * fd fixups and fput the file structs. It is safe to
1502 * call this function after the fixups have been
1503 * processed -- in that case, the list will be empty.
1505 static void binder_free_txn_fixups(struct binder_transaction *t)
1507 struct binder_txn_fd_fixup *fixup, *tmp;
1509 list_for_each_entry_safe(fixup, tmp, &t->fd_fixups, fixup_entry) {
1511 if (fixup->target_fd >= 0)
1512 put_unused_fd(fixup->target_fd);
1513 list_del(&fixup->fixup_entry);
1518 static void binder_txn_latency_free(struct binder_transaction *t)
1520 int from_proc, from_thread, to_proc, to_thread;
1522 spin_lock(&t->lock);
1523 from_proc = t->from ? t->from->proc->pid : 0;
1524 from_thread = t->from ? t->from->pid : 0;
1525 to_proc = t->to_proc ? t->to_proc->pid : 0;
1526 to_thread = t->to_thread ? t->to_thread->pid : 0;
1527 spin_unlock(&t->lock);
1529 trace_binder_txn_latency_free(t, from_proc, from_thread, to_proc, to_thread);
1532 static void binder_free_transaction(struct binder_transaction *t)
1534 struct binder_proc *target_proc = t->to_proc;
1537 binder_inner_proc_lock(target_proc);
1538 target_proc->outstanding_txns--;
1539 if (target_proc->outstanding_txns < 0)
1540 pr_warn("%s: Unexpected outstanding_txns %d\n",
1541 __func__, target_proc->outstanding_txns);
1542 if (!target_proc->outstanding_txns && target_proc->is_frozen)
1543 wake_up_interruptible_all(&target_proc->freeze_wait);
1545 t->buffer->transaction = NULL;
1546 binder_inner_proc_unlock(target_proc);
1548 if (trace_binder_txn_latency_free_enabled())
1549 binder_txn_latency_free(t);
1551 * If the transaction has no target_proc, then
1552 * t->buffer->transaction has already been cleared.
1554 binder_free_txn_fixups(t);
1556 binder_stats_deleted(BINDER_STAT_TRANSACTION);
1559 static void binder_send_failed_reply(struct binder_transaction *t,
1560 uint32_t error_code)
1562 struct binder_thread *target_thread;
1563 struct binder_transaction *next;
1565 BUG_ON(t->flags & TF_ONE_WAY);
1567 target_thread = binder_get_txn_from_and_acq_inner(t);
1568 if (target_thread) {
1569 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1570 "send failed reply for transaction %d to %d:%d\n",
1572 target_thread->proc->pid,
1573 target_thread->pid);
1575 binder_pop_transaction_ilocked(target_thread, t);
1576 if (target_thread->reply_error.cmd == BR_OK) {
1577 target_thread->reply_error.cmd = error_code;
1578 binder_enqueue_thread_work_ilocked(
1580 &target_thread->reply_error.work);
1581 wake_up_interruptible(&target_thread->wait);
1584 * Cannot get here for normal operation, but
1585 * we can if multiple synchronous transactions
1586 * are sent without blocking for responses.
1587 * Just ignore the 2nd error in this case.
1589 pr_warn("Unexpected reply error: %u\n",
1590 target_thread->reply_error.cmd);
1592 binder_inner_proc_unlock(target_thread->proc);
1593 binder_thread_dec_tmpref(target_thread);
1594 binder_free_transaction(t);
1597 __release(&target_thread->proc->inner_lock);
1598 next = t->from_parent;
1600 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1601 "send failed reply for transaction %d, target dead\n",
1604 binder_free_transaction(t);
1606 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1607 "reply failed, no target thread at root\n");
1611 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1612 "reply failed, no target thread -- retry %d\n",
1618 * binder_cleanup_transaction() - cleans up undelivered transaction
1619 * @t: transaction that needs to be cleaned up
1620 * @reason: reason the transaction wasn't delivered
1621 * @error_code: error to return to caller (if synchronous call)
1623 static void binder_cleanup_transaction(struct binder_transaction *t,
1625 uint32_t error_code)
1627 if (t->buffer->target_node && !(t->flags & TF_ONE_WAY)) {
1628 binder_send_failed_reply(t, error_code);
1630 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
1631 "undelivered transaction %d, %s\n",
1632 t->debug_id, reason);
1633 binder_free_transaction(t);
1638 * binder_get_object() - gets object and checks for valid metadata
1639 * @proc: binder_proc owning the buffer
1640 * @u: sender's user pointer to base of buffer
1641 * @buffer: binder_buffer that we're parsing.
1642 * @offset: offset in the @buffer at which to validate an object.
1643 * @object: struct binder_object to read into
1645 * Copy the binder object at the given offset into @object. If @u is
1646 * provided then the copy is from the sender's buffer. If not, then
1647 * it is copied from the target's @buffer.
1649 * Return: If there's a valid metadata object at @offset, the
1650 * size of that object. Otherwise, it returns zero. The object
1651 * is read into the struct binder_object pointed to by @object.
1653 static size_t binder_get_object(struct binder_proc *proc,
1654 const void __user *u,
1655 struct binder_buffer *buffer,
1656 unsigned long offset,
1657 struct binder_object *object)
1660 struct binder_object_header *hdr;
1661 size_t object_size = 0;
1663 read_size = min_t(size_t, sizeof(*object), buffer->data_size - offset);
1664 if (offset > buffer->data_size || read_size < sizeof(*hdr))
1667 if (copy_from_user(object, u + offset, read_size))
1670 if (binder_alloc_copy_from_buffer(&proc->alloc, object, buffer,
1675 /* Ok, now see if we read a complete object. */
1677 switch (hdr->type) {
1678 case BINDER_TYPE_BINDER:
1679 case BINDER_TYPE_WEAK_BINDER:
1680 case BINDER_TYPE_HANDLE:
1681 case BINDER_TYPE_WEAK_HANDLE:
1682 object_size = sizeof(struct flat_binder_object);
1684 case BINDER_TYPE_FD:
1685 object_size = sizeof(struct binder_fd_object);
1687 case BINDER_TYPE_PTR:
1688 object_size = sizeof(struct binder_buffer_object);
1690 case BINDER_TYPE_FDA:
1691 object_size = sizeof(struct binder_fd_array_object);
1696 if (offset <= buffer->data_size - object_size &&
1697 buffer->data_size >= object_size)
1704 * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
1705 * @proc: binder_proc owning the buffer
1706 * @b: binder_buffer containing the object
1707 * @object: struct binder_object to read into
1708 * @index: index in offset array at which the binder_buffer_object is
1710 * @start_offset: points to the start of the offset array
1711 * @object_offsetp: offset of @object read from @b
1712 * @num_valid: the number of valid offsets in the offset array
1714 * Return: If @index is within the valid range of the offset array
1715 * described by @start and @num_valid, and if there's a valid
1716 * binder_buffer_object at the offset found in index @index
1717 * of the offset array, that object is returned. Otherwise,
1718 * %NULL is returned.
1719 * Note that the offset found in index @index itself is not
1720 * verified; this function assumes that @num_valid elements
1721 * from @start were previously verified to have valid offsets.
1722 * If @object_offsetp is non-NULL, then the offset within
1723 * @b is written to it.
1725 static struct binder_buffer_object *binder_validate_ptr(
1726 struct binder_proc *proc,
1727 struct binder_buffer *b,
1728 struct binder_object *object,
1729 binder_size_t index,
1730 binder_size_t start_offset,
1731 binder_size_t *object_offsetp,
1732 binder_size_t num_valid)
1735 binder_size_t object_offset;
1736 unsigned long buffer_offset;
1738 if (index >= num_valid)
1741 buffer_offset = start_offset + sizeof(binder_size_t) * index;
1742 if (binder_alloc_copy_from_buffer(&proc->alloc, &object_offset,
1744 sizeof(object_offset)))
1746 object_size = binder_get_object(proc, NULL, b, object_offset, object);
1747 if (!object_size || object->hdr.type != BINDER_TYPE_PTR)
1750 *object_offsetp = object_offset;
1752 return &object->bbo;
1756 * binder_validate_fixup() - validates pointer/fd fixups happen in order.
1757 * @proc: binder_proc owning the buffer
1758 * @b: transaction buffer
1759 * @objects_start_offset: offset to start of objects buffer
1760 * @buffer_obj_offset: offset to binder_buffer_object in which to fix up
1761 * @fixup_offset: start offset in @buffer to fix up
1762 * @last_obj_offset: offset to last binder_buffer_object that we fixed
1763 * @last_min_offset: minimum fixup offset in object at @last_obj_offset
1765 * Return: %true if a fixup in buffer @buffer at offset @offset is
1768 * For safety reasons, we only allow fixups inside a buffer to happen
1769 * at increasing offsets; additionally, we only allow fixup on the last
1770 * buffer object that was verified, or one of its parents.
1772 * Example of what is allowed:
1775 * B (parent = A, offset = 0)
1776 * C (parent = A, offset = 16)
1777 * D (parent = C, offset = 0)
1778 * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
1780 * Examples of what is not allowed:
1782 * Decreasing offsets within the same parent:
1784 * C (parent = A, offset = 16)
1785 * B (parent = A, offset = 0) // decreasing offset within A
1787 * Referring to a parent that wasn't the last object or any of its parents:
1789 * B (parent = A, offset = 0)
1790 * C (parent = A, offset = 0)
1791 * C (parent = A, offset = 16)
1792 * D (parent = B, offset = 0) // B is not A or any of A's parents
1794 static bool binder_validate_fixup(struct binder_proc *proc,
1795 struct binder_buffer *b,
1796 binder_size_t objects_start_offset,
1797 binder_size_t buffer_obj_offset,
1798 binder_size_t fixup_offset,
1799 binder_size_t last_obj_offset,
1800 binder_size_t last_min_offset)
1802 if (!last_obj_offset) {
1803 /* Nothing to fix up in */
1807 while (last_obj_offset != buffer_obj_offset) {
1808 unsigned long buffer_offset;
1809 struct binder_object last_object;
1810 struct binder_buffer_object *last_bbo;
1811 size_t object_size = binder_get_object(proc, NULL, b,
1814 if (object_size != sizeof(*last_bbo))
1817 last_bbo = &last_object.bbo;
1819 * Safe to retrieve the parent of last_obj, since it
1820 * was already previously verified by the driver.
1822 if ((last_bbo->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0)
1824 last_min_offset = last_bbo->parent_offset + sizeof(uintptr_t);
1825 buffer_offset = objects_start_offset +
1826 sizeof(binder_size_t) * last_bbo->parent;
1827 if (binder_alloc_copy_from_buffer(&proc->alloc,
1830 sizeof(last_obj_offset)))
1833 return (fixup_offset >= last_min_offset);
1837 * struct binder_task_work_cb - for deferred close
1839 * @twork: callback_head for task work
1842 * Structure to pass task work to be handled after
1843 * returning from binder_ioctl() via task_work_add().
1845 struct binder_task_work_cb {
1846 struct callback_head twork;
1851 * binder_do_fd_close() - close list of file descriptors
1852 * @twork: callback head for task work
1854 * It is not safe to call ksys_close() during the binder_ioctl()
1855 * function if there is a chance that binder's own file descriptor
1856 * might be closed. This is to meet the requirements for using
1857 * fdget() (see comments for __fget_light()). Therefore use
1858 * task_work_add() to schedule the close operation once we have
1859 * returned from binder_ioctl(). This function is a callback
1860 * for that mechanism and does the actual ksys_close() on the
1861 * given file descriptor.
1863 static void binder_do_fd_close(struct callback_head *twork)
1865 struct binder_task_work_cb *twcb = container_of(twork,
1866 struct binder_task_work_cb, twork);
1873 * binder_deferred_fd_close() - schedule a close for the given file-descriptor
1874 * @fd: file-descriptor to close
1876 * See comments in binder_do_fd_close(). This function is used to schedule
1877 * a file-descriptor to be closed after returning from binder_ioctl().
1879 static void binder_deferred_fd_close(int fd)
1881 struct binder_task_work_cb *twcb;
1883 twcb = kzalloc(sizeof(*twcb), GFP_KERNEL);
1886 init_task_work(&twcb->twork, binder_do_fd_close);
1887 twcb->file = close_fd_get_file(fd);
1889 filp_close(twcb->file, current->files);
1890 task_work_add(current, &twcb->twork, TWA_RESUME);
1896 static void binder_transaction_buffer_release(struct binder_proc *proc,
1897 struct binder_thread *thread,
1898 struct binder_buffer *buffer,
1899 binder_size_t failed_at,
1902 int debug_id = buffer->debug_id;
1903 binder_size_t off_start_offset, buffer_offset, off_end_offset;
1905 binder_debug(BINDER_DEBUG_TRANSACTION,
1906 "%d buffer release %d, size %zd-%zd, failed at %llx\n",
1907 proc->pid, buffer->debug_id,
1908 buffer->data_size, buffer->offsets_size,
1909 (unsigned long long)failed_at);
1911 if (buffer->target_node)
1912 binder_dec_node(buffer->target_node, 1, 0);
1914 off_start_offset = ALIGN(buffer->data_size, sizeof(void *));
1915 off_end_offset = is_failure && failed_at ? failed_at :
1916 off_start_offset + buffer->offsets_size;
1917 for (buffer_offset = off_start_offset; buffer_offset < off_end_offset;
1918 buffer_offset += sizeof(binder_size_t)) {
1919 struct binder_object_header *hdr;
1920 size_t object_size = 0;
1921 struct binder_object object;
1922 binder_size_t object_offset;
1924 if (!binder_alloc_copy_from_buffer(&proc->alloc, &object_offset,
1925 buffer, buffer_offset,
1926 sizeof(object_offset)))
1927 object_size = binder_get_object(proc, NULL, buffer,
1928 object_offset, &object);
1929 if (object_size == 0) {
1930 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
1931 debug_id, (u64)object_offset, buffer->data_size);
1935 switch (hdr->type) {
1936 case BINDER_TYPE_BINDER:
1937 case BINDER_TYPE_WEAK_BINDER: {
1938 struct flat_binder_object *fp;
1939 struct binder_node *node;
1941 fp = to_flat_binder_object(hdr);
1942 node = binder_get_node(proc, fp->binder);
1944 pr_err("transaction release %d bad node %016llx\n",
1945 debug_id, (u64)fp->binder);
1948 binder_debug(BINDER_DEBUG_TRANSACTION,
1949 " node %d u%016llx\n",
1950 node->debug_id, (u64)node->ptr);
1951 binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER,
1953 binder_put_node(node);
1955 case BINDER_TYPE_HANDLE:
1956 case BINDER_TYPE_WEAK_HANDLE: {
1957 struct flat_binder_object *fp;
1958 struct binder_ref_data rdata;
1961 fp = to_flat_binder_object(hdr);
1962 ret = binder_dec_ref_for_handle(proc, fp->handle,
1963 hdr->type == BINDER_TYPE_HANDLE, &rdata);
1966 pr_err("transaction release %d bad handle %d, ret = %d\n",
1967 debug_id, fp->handle, ret);
1970 binder_debug(BINDER_DEBUG_TRANSACTION,
1971 " ref %d desc %d\n",
1972 rdata.debug_id, rdata.desc);
1975 case BINDER_TYPE_FD: {
1977 * No need to close the file here since user-space
1978 * closes it for successfully delivered
1979 * transactions. For transactions that weren't
1980 * delivered, the new fd was never allocated so
1981 * there is no need to close and the fput on the
1982 * file is done when the transaction is torn
1986 case BINDER_TYPE_PTR:
1988 * Nothing to do here, this will get cleaned up when the
1989 * transaction buffer gets freed
1992 case BINDER_TYPE_FDA: {
1993 struct binder_fd_array_object *fda;
1994 struct binder_buffer_object *parent;
1995 struct binder_object ptr_object;
1996 binder_size_t fda_offset;
1998 binder_size_t fd_buf_size;
1999 binder_size_t num_valid;
2003 * The fd fixups have not been applied so no
2004 * fds need to be closed.
2009 num_valid = (buffer_offset - off_start_offset) /
2010 sizeof(binder_size_t);
2011 fda = to_binder_fd_array_object(hdr);
2012 parent = binder_validate_ptr(proc, buffer, &ptr_object,
2018 pr_err("transaction release %d bad parent offset\n",
2022 fd_buf_size = sizeof(u32) * fda->num_fds;
2023 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2024 pr_err("transaction release %d invalid number of fds (%lld)\n",
2025 debug_id, (u64)fda->num_fds);
2028 if (fd_buf_size > parent->length ||
2029 fda->parent_offset > parent->length - fd_buf_size) {
2030 /* No space for all file descriptors here. */
2031 pr_err("transaction release %d not enough space for %lld fds in buffer\n",
2032 debug_id, (u64)fda->num_fds);
2036 * the source data for binder_buffer_object is visible
2037 * to user-space and the @buffer element is the user
2038 * pointer to the buffer_object containing the fd_array.
2039 * Convert the address to an offset relative to
2040 * the base of the transaction buffer.
2043 (parent->buffer - (uintptr_t)buffer->user_data) +
2045 for (fd_index = 0; fd_index < fda->num_fds;
2049 binder_size_t offset = fda_offset +
2050 fd_index * sizeof(fd);
2052 err = binder_alloc_copy_from_buffer(
2053 &proc->alloc, &fd, buffer,
2054 offset, sizeof(fd));
2057 binder_deferred_fd_close(fd);
2059 * Need to make sure the thread goes
2060 * back to userspace to complete the
2064 thread->looper_need_return = true;
2069 pr_err("transaction release %d bad object type %x\n",
2070 debug_id, hdr->type);
2076 static int binder_translate_binder(struct flat_binder_object *fp,
2077 struct binder_transaction *t,
2078 struct binder_thread *thread)
2080 struct binder_node *node;
2081 struct binder_proc *proc = thread->proc;
2082 struct binder_proc *target_proc = t->to_proc;
2083 struct binder_ref_data rdata;
2086 node = binder_get_node(proc, fp->binder);
2088 node = binder_new_node(proc, fp);
2092 if (fp->cookie != node->cookie) {
2093 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
2094 proc->pid, thread->pid, (u64)fp->binder,
2095 node->debug_id, (u64)fp->cookie,
2100 if (security_binder_transfer_binder(proc->cred, target_proc->cred)) {
2105 ret = binder_inc_ref_for_node(target_proc, node,
2106 fp->hdr.type == BINDER_TYPE_BINDER,
2107 &thread->todo, &rdata);
2111 if (fp->hdr.type == BINDER_TYPE_BINDER)
2112 fp->hdr.type = BINDER_TYPE_HANDLE;
2114 fp->hdr.type = BINDER_TYPE_WEAK_HANDLE;
2116 fp->handle = rdata.desc;
2119 trace_binder_transaction_node_to_ref(t, node, &rdata);
2120 binder_debug(BINDER_DEBUG_TRANSACTION,
2121 " node %d u%016llx -> ref %d desc %d\n",
2122 node->debug_id, (u64)node->ptr,
2123 rdata.debug_id, rdata.desc);
2125 binder_put_node(node);
2129 static int binder_translate_handle(struct flat_binder_object *fp,
2130 struct binder_transaction *t,
2131 struct binder_thread *thread)
2133 struct binder_proc *proc = thread->proc;
2134 struct binder_proc *target_proc = t->to_proc;
2135 struct binder_node *node;
2136 struct binder_ref_data src_rdata;
2139 node = binder_get_node_from_ref(proc, fp->handle,
2140 fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata);
2142 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
2143 proc->pid, thread->pid, fp->handle);
2146 if (security_binder_transfer_binder(proc->cred, target_proc->cred)) {
2151 binder_node_lock(node);
2152 if (node->proc == target_proc) {
2153 if (fp->hdr.type == BINDER_TYPE_HANDLE)
2154 fp->hdr.type = BINDER_TYPE_BINDER;
2156 fp->hdr.type = BINDER_TYPE_WEAK_BINDER;
2157 fp->binder = node->ptr;
2158 fp->cookie = node->cookie;
2160 binder_inner_proc_lock(node->proc);
2162 __acquire(&node->proc->inner_lock);
2163 binder_inc_node_nilocked(node,
2164 fp->hdr.type == BINDER_TYPE_BINDER,
2167 binder_inner_proc_unlock(node->proc);
2169 __release(&node->proc->inner_lock);
2170 trace_binder_transaction_ref_to_node(t, node, &src_rdata);
2171 binder_debug(BINDER_DEBUG_TRANSACTION,
2172 " ref %d desc %d -> node %d u%016llx\n",
2173 src_rdata.debug_id, src_rdata.desc, node->debug_id,
2175 binder_node_unlock(node);
2177 struct binder_ref_data dest_rdata;
2179 binder_node_unlock(node);
2180 ret = binder_inc_ref_for_node(target_proc, node,
2181 fp->hdr.type == BINDER_TYPE_HANDLE,
2187 fp->handle = dest_rdata.desc;
2189 trace_binder_transaction_ref_to_ref(t, node, &src_rdata,
2191 binder_debug(BINDER_DEBUG_TRANSACTION,
2192 " ref %d desc %d -> ref %d desc %d (node %d)\n",
2193 src_rdata.debug_id, src_rdata.desc,
2194 dest_rdata.debug_id, dest_rdata.desc,
2198 binder_put_node(node);
2202 static int binder_translate_fd(u32 fd, binder_size_t fd_offset,
2203 struct binder_transaction *t,
2204 struct binder_thread *thread,
2205 struct binder_transaction *in_reply_to)
2207 struct binder_proc *proc = thread->proc;
2208 struct binder_proc *target_proc = t->to_proc;
2209 struct binder_txn_fd_fixup *fixup;
2212 bool target_allows_fd;
2215 target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS);
2217 target_allows_fd = t->buffer->target_node->accept_fds;
2218 if (!target_allows_fd) {
2219 binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
2220 proc->pid, thread->pid,
2221 in_reply_to ? "reply" : "transaction",
2224 goto err_fd_not_accepted;
2229 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
2230 proc->pid, thread->pid, fd);
2234 ret = security_binder_transfer_file(proc->cred, target_proc->cred, file);
2241 * Add fixup record for this transaction. The allocation
2242 * of the fd in the target needs to be done from a
2245 fixup = kzalloc(sizeof(*fixup), GFP_KERNEL);
2251 fixup->offset = fd_offset;
2252 fixup->target_fd = -1;
2253 trace_binder_transaction_fd_send(t, fd, fixup->offset);
2254 list_add_tail(&fixup->fixup_entry, &t->fd_fixups);
2262 err_fd_not_accepted:
2267 * struct binder_ptr_fixup - data to be fixed-up in target buffer
2268 * @offset offset in target buffer to fixup
2269 * @skip_size bytes to skip in copy (fixup will be written later)
2270 * @fixup_data data to write at fixup offset
2273 * This is used for the pointer fixup list (pf) which is created and consumed
2274 * during binder_transaction() and is only accessed locally. No
2275 * locking is necessary.
2277 * The list is ordered by @offset.
2279 struct binder_ptr_fixup {
2280 binder_size_t offset;
2282 binder_uintptr_t fixup_data;
2283 struct list_head node;
2287 * struct binder_sg_copy - scatter-gather data to be copied
2288 * @offset offset in target buffer
2289 * @sender_uaddr user address in source buffer
2290 * @length bytes to copy
2293 * This is used for the sg copy list (sgc) which is created and consumed
2294 * during binder_transaction() and is only accessed locally. No
2295 * locking is necessary.
2297 * The list is ordered by @offset.
2299 struct binder_sg_copy {
2300 binder_size_t offset;
2301 const void __user *sender_uaddr;
2303 struct list_head node;
2307 * binder_do_deferred_txn_copies() - copy and fixup scatter-gather data
2308 * @alloc: binder_alloc associated with @buffer
2309 * @buffer: binder buffer in target process
2310 * @sgc_head: list_head of scatter-gather copy list
2311 * @pf_head: list_head of pointer fixup list
2313 * Processes all elements of @sgc_head, applying fixups from @pf_head
2314 * and copying the scatter-gather data from the source process' user
2315 * buffer to the target's buffer. It is expected that the list creation
2316 * and processing all occurs during binder_transaction() so these lists
2317 * are only accessed in local context.
2319 * Return: 0=success, else -errno
2321 static int binder_do_deferred_txn_copies(struct binder_alloc *alloc,
2322 struct binder_buffer *buffer,
2323 struct list_head *sgc_head,
2324 struct list_head *pf_head)
2327 struct binder_sg_copy *sgc, *tmpsgc;
2328 struct binder_ptr_fixup *tmppf;
2329 struct binder_ptr_fixup *pf =
2330 list_first_entry_or_null(pf_head, struct binder_ptr_fixup,
2333 list_for_each_entry_safe(sgc, tmpsgc, sgc_head, node) {
2334 size_t bytes_copied = 0;
2336 while (bytes_copied < sgc->length) {
2338 size_t bytes_left = sgc->length - bytes_copied;
2339 size_t offset = sgc->offset + bytes_copied;
2342 * We copy up to the fixup (pointed to by pf)
2344 copy_size = pf ? min(bytes_left, (size_t)pf->offset - offset)
2346 if (!ret && copy_size)
2347 ret = binder_alloc_copy_user_to_buffer(
2350 sgc->sender_uaddr + bytes_copied,
2352 bytes_copied += copy_size;
2353 if (copy_size != bytes_left) {
2355 /* we stopped at a fixup offset */
2356 if (pf->skip_size) {
2358 * we are just skipping. This is for
2359 * BINDER_TYPE_FDA where the translated
2360 * fds will be fixed up when we get
2361 * to target context.
2363 bytes_copied += pf->skip_size;
2365 /* apply the fixup indicated by pf */
2367 ret = binder_alloc_copy_to_buffer(
2371 sizeof(pf->fixup_data));
2372 bytes_copied += sizeof(pf->fixup_data);
2374 list_del(&pf->node);
2376 pf = list_first_entry_or_null(pf_head,
2377 struct binder_ptr_fixup, node);
2380 list_del(&sgc->node);
2383 list_for_each_entry_safe(pf, tmppf, pf_head, node) {
2384 BUG_ON(pf->skip_size == 0);
2385 list_del(&pf->node);
2388 BUG_ON(!list_empty(sgc_head));
2390 return ret > 0 ? -EINVAL : ret;
2394 * binder_cleanup_deferred_txn_lists() - free specified lists
2395 * @sgc_head: list_head of scatter-gather copy list
2396 * @pf_head: list_head of pointer fixup list
2398 * Called to clean up @sgc_head and @pf_head if there is an
2401 static void binder_cleanup_deferred_txn_lists(struct list_head *sgc_head,
2402 struct list_head *pf_head)
2404 struct binder_sg_copy *sgc, *tmpsgc;
2405 struct binder_ptr_fixup *pf, *tmppf;
2407 list_for_each_entry_safe(sgc, tmpsgc, sgc_head, node) {
2408 list_del(&sgc->node);
2411 list_for_each_entry_safe(pf, tmppf, pf_head, node) {
2412 list_del(&pf->node);
2418 * binder_defer_copy() - queue a scatter-gather buffer for copy
2419 * @sgc_head: list_head of scatter-gather copy list
2420 * @offset: binder buffer offset in target process
2421 * @sender_uaddr: user address in source process
2422 * @length: bytes to copy
2424 * Specify a scatter-gather block to be copied. The actual copy must
2425 * be deferred until all the needed fixups are identified and queued.
2426 * Then the copy and fixups are done together so un-translated values
2427 * from the source are never visible in the target buffer.
2429 * We are guaranteed that repeated calls to this function will have
2430 * monotonically increasing @offset values so the list will naturally
2433 * Return: 0=success, else -errno
2435 static int binder_defer_copy(struct list_head *sgc_head, binder_size_t offset,
2436 const void __user *sender_uaddr, size_t length)
2438 struct binder_sg_copy *bc = kzalloc(sizeof(*bc), GFP_KERNEL);
2443 bc->offset = offset;
2444 bc->sender_uaddr = sender_uaddr;
2445 bc->length = length;
2446 INIT_LIST_HEAD(&bc->node);
2449 * We are guaranteed that the deferred copies are in-order
2450 * so just add to the tail.
2452 list_add_tail(&bc->node, sgc_head);
2458 * binder_add_fixup() - queue a fixup to be applied to sg copy
2459 * @pf_head: list_head of binder ptr fixup list
2460 * @offset: binder buffer offset in target process
2461 * @fixup: bytes to be copied for fixup
2462 * @skip_size: bytes to skip when copying (fixup will be applied later)
2464 * Add the specified fixup to a list ordered by @offset. When copying
2465 * the scatter-gather buffers, the fixup will be copied instead of
2466 * data from the source buffer. For BINDER_TYPE_FDA fixups, the fixup
2467 * will be applied later (in target process context), so we just skip
2468 * the bytes specified by @skip_size. If @skip_size is 0, we copy the
2471 * This function is called *mostly* in @offset order, but there are
2472 * exceptions. Since out-of-order inserts are relatively uncommon,
2473 * we insert the new element by searching backward from the tail of
2476 * Return: 0=success, else -errno
2478 static int binder_add_fixup(struct list_head *pf_head, binder_size_t offset,
2479 binder_uintptr_t fixup, size_t skip_size)
2481 struct binder_ptr_fixup *pf = kzalloc(sizeof(*pf), GFP_KERNEL);
2482 struct binder_ptr_fixup *tmppf;
2487 pf->offset = offset;
2488 pf->fixup_data = fixup;
2489 pf->skip_size = skip_size;
2490 INIT_LIST_HEAD(&pf->node);
2492 /* Fixups are *mostly* added in-order, but there are some
2493 * exceptions. Look backwards through list for insertion point.
2495 list_for_each_entry_reverse(tmppf, pf_head, node) {
2496 if (tmppf->offset < pf->offset) {
2497 list_add(&pf->node, &tmppf->node);
2502 * if we get here, then the new offset is the lowest so
2503 * insert at the head
2505 list_add(&pf->node, pf_head);
2509 static int binder_translate_fd_array(struct list_head *pf_head,
2510 struct binder_fd_array_object *fda,
2511 const void __user *sender_ubuffer,
2512 struct binder_buffer_object *parent,
2513 struct binder_buffer_object *sender_uparent,
2514 struct binder_transaction *t,
2515 struct binder_thread *thread,
2516 struct binder_transaction *in_reply_to)
2518 binder_size_t fdi, fd_buf_size;
2519 binder_size_t fda_offset;
2520 const void __user *sender_ufda_base;
2521 struct binder_proc *proc = thread->proc;
2524 if (fda->num_fds == 0)
2527 fd_buf_size = sizeof(u32) * fda->num_fds;
2528 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2529 binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
2530 proc->pid, thread->pid, (u64)fda->num_fds);
2533 if (fd_buf_size > parent->length ||
2534 fda->parent_offset > parent->length - fd_buf_size) {
2535 /* No space for all file descriptors here. */
2536 binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
2537 proc->pid, thread->pid, (u64)fda->num_fds);
2541 * the source data for binder_buffer_object is visible
2542 * to user-space and the @buffer element is the user
2543 * pointer to the buffer_object containing the fd_array.
2544 * Convert the address to an offset relative to
2545 * the base of the transaction buffer.
2547 fda_offset = (parent->buffer - (uintptr_t)t->buffer->user_data) +
2549 sender_ufda_base = (void __user *)(uintptr_t)sender_uparent->buffer +
2552 if (!IS_ALIGNED((unsigned long)fda_offset, sizeof(u32)) ||
2553 !IS_ALIGNED((unsigned long)sender_ufda_base, sizeof(u32))) {
2554 binder_user_error("%d:%d parent offset not aligned correctly.\n",
2555 proc->pid, thread->pid);
2558 ret = binder_add_fixup(pf_head, fda_offset, 0, fda->num_fds * sizeof(u32));
2562 for (fdi = 0; fdi < fda->num_fds; fdi++) {
2564 binder_size_t offset = fda_offset + fdi * sizeof(fd);
2565 binder_size_t sender_uoffset = fdi * sizeof(fd);
2567 ret = copy_from_user(&fd, sender_ufda_base + sender_uoffset, sizeof(fd));
2569 ret = binder_translate_fd(fd, offset, t, thread,
2572 return ret > 0 ? -EINVAL : ret;
2577 static int binder_fixup_parent(struct list_head *pf_head,
2578 struct binder_transaction *t,
2579 struct binder_thread *thread,
2580 struct binder_buffer_object *bp,
2581 binder_size_t off_start_offset,
2582 binder_size_t num_valid,
2583 binder_size_t last_fixup_obj_off,
2584 binder_size_t last_fixup_min_off)
2586 struct binder_buffer_object *parent;
2587 struct binder_buffer *b = t->buffer;
2588 struct binder_proc *proc = thread->proc;
2589 struct binder_proc *target_proc = t->to_proc;
2590 struct binder_object object;
2591 binder_size_t buffer_offset;
2592 binder_size_t parent_offset;
2594 if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT))
2597 parent = binder_validate_ptr(target_proc, b, &object, bp->parent,
2598 off_start_offset, &parent_offset,
2601 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
2602 proc->pid, thread->pid);
2606 if (!binder_validate_fixup(target_proc, b, off_start_offset,
2607 parent_offset, bp->parent_offset,
2609 last_fixup_min_off)) {
2610 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
2611 proc->pid, thread->pid);
2615 if (parent->length < sizeof(binder_uintptr_t) ||
2616 bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) {
2617 /* No space for a pointer here! */
2618 binder_user_error("%d:%d got transaction with invalid parent offset\n",
2619 proc->pid, thread->pid);
2622 buffer_offset = bp->parent_offset +
2623 (uintptr_t)parent->buffer - (uintptr_t)b->user_data;
2624 return binder_add_fixup(pf_head, buffer_offset, bp->buffer, 0);
2628 * binder_proc_transaction() - sends a transaction to a process and wakes it up
2629 * @t: transaction to send
2630 * @proc: process to send the transaction to
2631 * @thread: thread in @proc to send the transaction to (may be NULL)
2633 * This function queues a transaction to the specified process. It will try
2634 * to find a thread in the target process to handle the transaction and
2635 * wake it up. If no thread is found, the work is queued to the proc
2638 * If the @thread parameter is not NULL, the transaction is always queued
2639 * to the waitlist of that specific thread.
2641 * Return: 0 if the transaction was successfully queued
2642 * BR_DEAD_REPLY if the target process or thread is dead
2643 * BR_FROZEN_REPLY if the target process or thread is frozen
2645 static int binder_proc_transaction(struct binder_transaction *t,
2646 struct binder_proc *proc,
2647 struct binder_thread *thread)
2649 struct binder_node *node = t->buffer->target_node;
2650 bool oneway = !!(t->flags & TF_ONE_WAY);
2651 bool pending_async = false;
2654 binder_node_lock(node);
2657 if (node->has_async_transaction)
2658 pending_async = true;
2660 node->has_async_transaction = true;
2663 binder_inner_proc_lock(proc);
2664 if (proc->is_frozen) {
2665 proc->sync_recv |= !oneway;
2666 proc->async_recv |= oneway;
2669 if ((proc->is_frozen && !oneway) || proc->is_dead ||
2670 (thread && thread->is_dead)) {
2671 binder_inner_proc_unlock(proc);
2672 binder_node_unlock(node);
2673 return proc->is_frozen ? BR_FROZEN_REPLY : BR_DEAD_REPLY;
2676 if (!thread && !pending_async)
2677 thread = binder_select_thread_ilocked(proc);
2680 binder_enqueue_thread_work_ilocked(thread, &t->work);
2681 else if (!pending_async)
2682 binder_enqueue_work_ilocked(&t->work, &proc->todo);
2684 binder_enqueue_work_ilocked(&t->work, &node->async_todo);
2687 binder_wakeup_thread_ilocked(proc, thread, !oneway /* sync */);
2689 proc->outstanding_txns++;
2690 binder_inner_proc_unlock(proc);
2691 binder_node_unlock(node);
2697 * binder_get_node_refs_for_txn() - Get required refs on node for txn
2698 * @node: struct binder_node for which to get refs
2699 * @proc: returns @node->proc if valid
2700 * @error: if no @proc then returns BR_DEAD_REPLY
2702 * User-space normally keeps the node alive when creating a transaction
2703 * since it has a reference to the target. The local strong ref keeps it
2704 * alive if the sending process dies before the target process processes
2705 * the transaction. If the source process is malicious or has a reference
2706 * counting bug, relying on the local strong ref can fail.
2708 * Since user-space can cause the local strong ref to go away, we also take
2709 * a tmpref on the node to ensure it survives while we are constructing
2710 * the transaction. We also need a tmpref on the proc while we are
2711 * constructing the transaction, so we take that here as well.
2713 * Return: The target_node with refs taken or NULL if no @node->proc is NULL.
2714 * Also sets @proc if valid. If the @node->proc is NULL indicating that the
2715 * target proc has died, @error is set to BR_DEAD_REPLY
2717 static struct binder_node *binder_get_node_refs_for_txn(
2718 struct binder_node *node,
2719 struct binder_proc **procp,
2722 struct binder_node *target_node = NULL;
2724 binder_node_inner_lock(node);
2727 binder_inc_node_nilocked(node, 1, 0, NULL);
2728 binder_inc_node_tmpref_ilocked(node);
2729 node->proc->tmp_ref++;
2730 *procp = node->proc;
2732 *error = BR_DEAD_REPLY;
2733 binder_node_inner_unlock(node);
2738 static void binder_set_txn_from_error(struct binder_transaction *t, int id,
2739 uint32_t command, int32_t param)
2741 struct binder_thread *from = binder_get_txn_from_and_acq_inner(t);
2744 /* annotation for sparse */
2745 __release(&from->proc->inner_lock);
2749 /* don't override existing errors */
2750 if (from->ee.command == BR_OK)
2751 binder_set_extended_error(&from->ee, id, command, param);
2752 binder_inner_proc_unlock(from->proc);
2753 binder_thread_dec_tmpref(from);
2756 static void binder_transaction(struct binder_proc *proc,
2757 struct binder_thread *thread,
2758 struct binder_transaction_data *tr, int reply,
2759 binder_size_t extra_buffers_size)
2762 struct binder_transaction *t;
2763 struct binder_work *w;
2764 struct binder_work *tcomplete;
2765 binder_size_t buffer_offset = 0;
2766 binder_size_t off_start_offset, off_end_offset;
2767 binder_size_t off_min;
2768 binder_size_t sg_buf_offset, sg_buf_end_offset;
2769 binder_size_t user_offset = 0;
2770 struct binder_proc *target_proc = NULL;
2771 struct binder_thread *target_thread = NULL;
2772 struct binder_node *target_node = NULL;
2773 struct binder_transaction *in_reply_to = NULL;
2774 struct binder_transaction_log_entry *e;
2775 uint32_t return_error = 0;
2776 uint32_t return_error_param = 0;
2777 uint32_t return_error_line = 0;
2778 binder_size_t last_fixup_obj_off = 0;
2779 binder_size_t last_fixup_min_off = 0;
2780 struct binder_context *context = proc->context;
2781 int t_debug_id = atomic_inc_return(&binder_last_id);
2782 char *secctx = NULL;
2784 struct list_head sgc_head;
2785 struct list_head pf_head;
2786 const void __user *user_buffer = (const void __user *)
2787 (uintptr_t)tr->data.ptr.buffer;
2788 INIT_LIST_HEAD(&sgc_head);
2789 INIT_LIST_HEAD(&pf_head);
2791 e = binder_transaction_log_add(&binder_transaction_log);
2792 e->debug_id = t_debug_id;
2793 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
2794 e->from_proc = proc->pid;
2795 e->from_thread = thread->pid;
2796 e->target_handle = tr->target.handle;
2797 e->data_size = tr->data_size;
2798 e->offsets_size = tr->offsets_size;
2799 strscpy(e->context_name, proc->context->name, BINDERFS_MAX_NAME);
2801 binder_inner_proc_lock(proc);
2802 binder_set_extended_error(&thread->ee, t_debug_id, BR_OK, 0);
2803 binder_inner_proc_unlock(proc);
2806 binder_inner_proc_lock(proc);
2807 in_reply_to = thread->transaction_stack;
2808 if (in_reply_to == NULL) {
2809 binder_inner_proc_unlock(proc);
2810 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
2811 proc->pid, thread->pid);
2812 return_error = BR_FAILED_REPLY;
2813 return_error_param = -EPROTO;
2814 return_error_line = __LINE__;
2815 goto err_empty_call_stack;
2817 if (in_reply_to->to_thread != thread) {
2818 spin_lock(&in_reply_to->lock);
2819 binder_user_error("%d:%d got reply transaction with bad transaction stack, transaction %d has target %d:%d\n",
2820 proc->pid, thread->pid, in_reply_to->debug_id,
2821 in_reply_to->to_proc ?
2822 in_reply_to->to_proc->pid : 0,
2823 in_reply_to->to_thread ?
2824 in_reply_to->to_thread->pid : 0);
2825 spin_unlock(&in_reply_to->lock);
2826 binder_inner_proc_unlock(proc);
2827 return_error = BR_FAILED_REPLY;
2828 return_error_param = -EPROTO;
2829 return_error_line = __LINE__;
2831 goto err_bad_call_stack;
2833 thread->transaction_stack = in_reply_to->to_parent;
2834 binder_inner_proc_unlock(proc);
2835 binder_set_nice(in_reply_to->saved_priority);
2836 target_thread = binder_get_txn_from_and_acq_inner(in_reply_to);
2837 if (target_thread == NULL) {
2838 /* annotation for sparse */
2839 __release(&target_thread->proc->inner_lock);
2840 binder_txn_error("%d:%d reply target not found\n",
2841 thread->pid, proc->pid);
2842 return_error = BR_DEAD_REPLY;
2843 return_error_line = __LINE__;
2844 goto err_dead_binder;
2846 if (target_thread->transaction_stack != in_reply_to) {
2847 binder_user_error("%d:%d got reply transaction with bad target transaction stack %d, expected %d\n",
2848 proc->pid, thread->pid,
2849 target_thread->transaction_stack ?
2850 target_thread->transaction_stack->debug_id : 0,
2851 in_reply_to->debug_id);
2852 binder_inner_proc_unlock(target_thread->proc);
2853 return_error = BR_FAILED_REPLY;
2854 return_error_param = -EPROTO;
2855 return_error_line = __LINE__;
2857 target_thread = NULL;
2858 goto err_dead_binder;
2860 target_proc = target_thread->proc;
2861 target_proc->tmp_ref++;
2862 binder_inner_proc_unlock(target_thread->proc);
2864 if (tr->target.handle) {
2865 struct binder_ref *ref;
2868 * There must already be a strong ref
2869 * on this node. If so, do a strong
2870 * increment on the node to ensure it
2871 * stays alive until the transaction is
2874 binder_proc_lock(proc);
2875 ref = binder_get_ref_olocked(proc, tr->target.handle,
2878 target_node = binder_get_node_refs_for_txn(
2879 ref->node, &target_proc,
2882 binder_user_error("%d:%d got transaction to invalid handle, %u\n",
2883 proc->pid, thread->pid, tr->target.handle);
2884 return_error = BR_FAILED_REPLY;
2886 binder_proc_unlock(proc);
2888 mutex_lock(&context->context_mgr_node_lock);
2889 target_node = context->binder_context_mgr_node;
2891 target_node = binder_get_node_refs_for_txn(
2892 target_node, &target_proc,
2895 return_error = BR_DEAD_REPLY;
2896 mutex_unlock(&context->context_mgr_node_lock);
2897 if (target_node && target_proc->pid == proc->pid) {
2898 binder_user_error("%d:%d got transaction to context manager from process owning it\n",
2899 proc->pid, thread->pid);
2900 return_error = BR_FAILED_REPLY;
2901 return_error_param = -EINVAL;
2902 return_error_line = __LINE__;
2903 goto err_invalid_target_handle;
2907 binder_txn_error("%d:%d cannot find target node\n",
2908 thread->pid, proc->pid);
2910 * return_error is set above
2912 return_error_param = -EINVAL;
2913 return_error_line = __LINE__;
2914 goto err_dead_binder;
2916 e->to_node = target_node->debug_id;
2917 if (WARN_ON(proc == target_proc)) {
2918 binder_txn_error("%d:%d self transactions not allowed\n",
2919 thread->pid, proc->pid);
2920 return_error = BR_FAILED_REPLY;
2921 return_error_param = -EINVAL;
2922 return_error_line = __LINE__;
2923 goto err_invalid_target_handle;
2925 if (security_binder_transaction(proc->cred,
2926 target_proc->cred) < 0) {
2927 binder_txn_error("%d:%d transaction credentials failed\n",
2928 thread->pid, proc->pid);
2929 return_error = BR_FAILED_REPLY;
2930 return_error_param = -EPERM;
2931 return_error_line = __LINE__;
2932 goto err_invalid_target_handle;
2934 binder_inner_proc_lock(proc);
2936 w = list_first_entry_or_null(&thread->todo,
2937 struct binder_work, entry);
2938 if (!(tr->flags & TF_ONE_WAY) && w &&
2939 w->type == BINDER_WORK_TRANSACTION) {
2941 * Do not allow new outgoing transaction from a
2942 * thread that has a transaction at the head of
2943 * its todo list. Only need to check the head
2944 * because binder_select_thread_ilocked picks a
2945 * thread from proc->waiting_threads to enqueue
2946 * the transaction, and nothing is queued to the
2947 * todo list while the thread is on waiting_threads.
2949 binder_user_error("%d:%d new transaction not allowed when there is a transaction on thread todo\n",
2950 proc->pid, thread->pid);
2951 binder_inner_proc_unlock(proc);
2952 return_error = BR_FAILED_REPLY;
2953 return_error_param = -EPROTO;
2954 return_error_line = __LINE__;
2955 goto err_bad_todo_list;
2958 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
2959 struct binder_transaction *tmp;
2961 tmp = thread->transaction_stack;
2962 if (tmp->to_thread != thread) {
2963 spin_lock(&tmp->lock);
2964 binder_user_error("%d:%d got new transaction with bad transaction stack, transaction %d has target %d:%d\n",
2965 proc->pid, thread->pid, tmp->debug_id,
2966 tmp->to_proc ? tmp->to_proc->pid : 0,
2968 tmp->to_thread->pid : 0);
2969 spin_unlock(&tmp->lock);
2970 binder_inner_proc_unlock(proc);
2971 return_error = BR_FAILED_REPLY;
2972 return_error_param = -EPROTO;
2973 return_error_line = __LINE__;
2974 goto err_bad_call_stack;
2977 struct binder_thread *from;
2979 spin_lock(&tmp->lock);
2981 if (from && from->proc == target_proc) {
2982 atomic_inc(&from->tmp_ref);
2983 target_thread = from;
2984 spin_unlock(&tmp->lock);
2987 spin_unlock(&tmp->lock);
2988 tmp = tmp->from_parent;
2991 binder_inner_proc_unlock(proc);
2994 e->to_thread = target_thread->pid;
2995 e->to_proc = target_proc->pid;
2997 /* TODO: reuse incoming transaction for reply */
2998 t = kzalloc(sizeof(*t), GFP_KERNEL);
3000 binder_txn_error("%d:%d cannot allocate transaction\n",
3001 thread->pid, proc->pid);
3002 return_error = BR_FAILED_REPLY;
3003 return_error_param = -ENOMEM;
3004 return_error_line = __LINE__;
3005 goto err_alloc_t_failed;
3007 INIT_LIST_HEAD(&t->fd_fixups);
3008 binder_stats_created(BINDER_STAT_TRANSACTION);
3009 spin_lock_init(&t->lock);
3011 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
3012 if (tcomplete == NULL) {
3013 binder_txn_error("%d:%d cannot allocate work for transaction\n",
3014 thread->pid, proc->pid);
3015 return_error = BR_FAILED_REPLY;
3016 return_error_param = -ENOMEM;
3017 return_error_line = __LINE__;
3018 goto err_alloc_tcomplete_failed;
3020 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
3022 t->debug_id = t_debug_id;
3025 binder_debug(BINDER_DEBUG_TRANSACTION,
3026 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
3027 proc->pid, thread->pid, t->debug_id,
3028 target_proc->pid, target_thread->pid,
3029 (u64)tr->data.ptr.buffer,
3030 (u64)tr->data.ptr.offsets,
3031 (u64)tr->data_size, (u64)tr->offsets_size,
3032 (u64)extra_buffers_size);
3034 binder_debug(BINDER_DEBUG_TRANSACTION,
3035 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
3036 proc->pid, thread->pid, t->debug_id,
3037 target_proc->pid, target_node->debug_id,
3038 (u64)tr->data.ptr.buffer,
3039 (u64)tr->data.ptr.offsets,
3040 (u64)tr->data_size, (u64)tr->offsets_size,
3041 (u64)extra_buffers_size);
3043 if (!reply && !(tr->flags & TF_ONE_WAY))
3047 t->sender_euid = task_euid(proc->tsk);
3048 t->to_proc = target_proc;
3049 t->to_thread = target_thread;
3051 t->flags = tr->flags;
3052 t->priority = task_nice(current);
3054 if (target_node && target_node->txn_security_ctx) {
3058 security_cred_getsecid(proc->cred, &secid);
3059 ret = security_secid_to_secctx(secid, &secctx, &secctx_sz);
3061 binder_txn_error("%d:%d failed to get security context\n",
3062 thread->pid, proc->pid);
3063 return_error = BR_FAILED_REPLY;
3064 return_error_param = ret;
3065 return_error_line = __LINE__;
3066 goto err_get_secctx_failed;
3068 added_size = ALIGN(secctx_sz, sizeof(u64));
3069 extra_buffers_size += added_size;
3070 if (extra_buffers_size < added_size) {
3071 binder_txn_error("%d:%d integer overflow of extra_buffers_size\n",
3072 thread->pid, proc->pid);
3073 return_error = BR_FAILED_REPLY;
3074 return_error_param = -EINVAL;
3075 return_error_line = __LINE__;
3076 goto err_bad_extra_size;
3080 trace_binder_transaction(reply, t, target_node);
3082 t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size,
3083 tr->offsets_size, extra_buffers_size,
3084 !reply && (t->flags & TF_ONE_WAY), current->tgid);
3085 if (IS_ERR(t->buffer)) {
3088 ret = PTR_ERR(t->buffer);
3089 s = (ret == -ESRCH) ? ": vma cleared, target dead or dying"
3090 : (ret == -ENOSPC) ? ": no space left"
3091 : (ret == -ENOMEM) ? ": memory allocation failed"
3093 binder_txn_error("cannot allocate buffer%s", s);
3095 return_error_param = PTR_ERR(t->buffer);
3096 return_error = return_error_param == -ESRCH ?
3097 BR_DEAD_REPLY : BR_FAILED_REPLY;
3098 return_error_line = __LINE__;
3100 goto err_binder_alloc_buf_failed;
3104 size_t buf_offset = ALIGN(tr->data_size, sizeof(void *)) +
3105 ALIGN(tr->offsets_size, sizeof(void *)) +
3106 ALIGN(extra_buffers_size, sizeof(void *)) -
3107 ALIGN(secctx_sz, sizeof(u64));
3109 t->security_ctx = (uintptr_t)t->buffer->user_data + buf_offset;
3110 err = binder_alloc_copy_to_buffer(&target_proc->alloc,
3111 t->buffer, buf_offset,
3114 t->security_ctx = 0;
3117 security_release_secctx(secctx, secctx_sz);
3120 t->buffer->debug_id = t->debug_id;
3121 t->buffer->transaction = t;
3122 t->buffer->target_node = target_node;
3123 t->buffer->clear_on_free = !!(t->flags & TF_CLEAR_BUF);
3124 trace_binder_transaction_alloc_buf(t->buffer);
3126 if (binder_alloc_copy_user_to_buffer(
3127 &target_proc->alloc,
3129 ALIGN(tr->data_size, sizeof(void *)),
3130 (const void __user *)
3131 (uintptr_t)tr->data.ptr.offsets,
3132 tr->offsets_size)) {
3133 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3134 proc->pid, thread->pid);
3135 return_error = BR_FAILED_REPLY;
3136 return_error_param = -EFAULT;
3137 return_error_line = __LINE__;
3138 goto err_copy_data_failed;
3140 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
3141 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
3142 proc->pid, thread->pid, (u64)tr->offsets_size);
3143 return_error = BR_FAILED_REPLY;
3144 return_error_param = -EINVAL;
3145 return_error_line = __LINE__;
3146 goto err_bad_offset;
3148 if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) {
3149 binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
3150 proc->pid, thread->pid,
3151 (u64)extra_buffers_size);
3152 return_error = BR_FAILED_REPLY;
3153 return_error_param = -EINVAL;
3154 return_error_line = __LINE__;
3155 goto err_bad_offset;
3157 off_start_offset = ALIGN(tr->data_size, sizeof(void *));
3158 buffer_offset = off_start_offset;
3159 off_end_offset = off_start_offset + tr->offsets_size;
3160 sg_buf_offset = ALIGN(off_end_offset, sizeof(void *));
3161 sg_buf_end_offset = sg_buf_offset + extra_buffers_size -
3162 ALIGN(secctx_sz, sizeof(u64));
3164 for (buffer_offset = off_start_offset; buffer_offset < off_end_offset;
3165 buffer_offset += sizeof(binder_size_t)) {
3166 struct binder_object_header *hdr;
3168 struct binder_object object;
3169 binder_size_t object_offset;
3170 binder_size_t copy_size;
3172 if (binder_alloc_copy_from_buffer(&target_proc->alloc,
3176 sizeof(object_offset))) {
3177 binder_txn_error("%d:%d copy offset from buffer failed\n",
3178 thread->pid, proc->pid);
3179 return_error = BR_FAILED_REPLY;
3180 return_error_param = -EINVAL;
3181 return_error_line = __LINE__;
3182 goto err_bad_offset;
3186 * Copy the source user buffer up to the next object
3187 * that will be processed.
3189 copy_size = object_offset - user_offset;
3190 if (copy_size && (user_offset > object_offset ||
3191 binder_alloc_copy_user_to_buffer(
3192 &target_proc->alloc,
3193 t->buffer, user_offset,
3194 user_buffer + user_offset,
3196 binder_user_error("%d:%d got transaction with invalid data ptr\n",
3197 proc->pid, thread->pid);
3198 return_error = BR_FAILED_REPLY;
3199 return_error_param = -EFAULT;
3200 return_error_line = __LINE__;
3201 goto err_copy_data_failed;
3203 object_size = binder_get_object(target_proc, user_buffer,
3204 t->buffer, object_offset, &object);
3205 if (object_size == 0 || object_offset < off_min) {
3206 binder_user_error("%d:%d got transaction with invalid offset (%lld, min %lld max %lld) or object.\n",
3207 proc->pid, thread->pid,
3210 (u64)t->buffer->data_size);
3211 return_error = BR_FAILED_REPLY;
3212 return_error_param = -EINVAL;
3213 return_error_line = __LINE__;
3214 goto err_bad_offset;
3217 * Set offset to the next buffer fragment to be
3220 user_offset = object_offset + object_size;
3223 off_min = object_offset + object_size;
3224 switch (hdr->type) {
3225 case BINDER_TYPE_BINDER:
3226 case BINDER_TYPE_WEAK_BINDER: {
3227 struct flat_binder_object *fp;
3229 fp = to_flat_binder_object(hdr);
3230 ret = binder_translate_binder(fp, t, thread);
3233 binder_alloc_copy_to_buffer(&target_proc->alloc,
3237 binder_txn_error("%d:%d translate binder failed\n",
3238 thread->pid, proc->pid);
3239 return_error = BR_FAILED_REPLY;
3240 return_error_param = ret;
3241 return_error_line = __LINE__;
3242 goto err_translate_failed;
3245 case BINDER_TYPE_HANDLE:
3246 case BINDER_TYPE_WEAK_HANDLE: {
3247 struct flat_binder_object *fp;
3249 fp = to_flat_binder_object(hdr);
3250 ret = binder_translate_handle(fp, t, thread);
3252 binder_alloc_copy_to_buffer(&target_proc->alloc,
3256 binder_txn_error("%d:%d translate handle failed\n",
3257 thread->pid, proc->pid);
3258 return_error = BR_FAILED_REPLY;
3259 return_error_param = ret;
3260 return_error_line = __LINE__;
3261 goto err_translate_failed;
3265 case BINDER_TYPE_FD: {
3266 struct binder_fd_object *fp = to_binder_fd_object(hdr);
3267 binder_size_t fd_offset = object_offset +
3268 (uintptr_t)&fp->fd - (uintptr_t)fp;
3269 int ret = binder_translate_fd(fp->fd, fd_offset, t,
3270 thread, in_reply_to);
3274 binder_alloc_copy_to_buffer(&target_proc->alloc,
3278 binder_txn_error("%d:%d translate fd failed\n",
3279 thread->pid, proc->pid);
3280 return_error = BR_FAILED_REPLY;
3281 return_error_param = ret;
3282 return_error_line = __LINE__;
3283 goto err_translate_failed;
3286 case BINDER_TYPE_FDA: {
3287 struct binder_object ptr_object;
3288 binder_size_t parent_offset;
3289 struct binder_object user_object;
3290 size_t user_parent_size;
3291 struct binder_fd_array_object *fda =
3292 to_binder_fd_array_object(hdr);
3293 size_t num_valid = (buffer_offset - off_start_offset) /
3294 sizeof(binder_size_t);
3295 struct binder_buffer_object *parent =
3296 binder_validate_ptr(target_proc, t->buffer,
3297 &ptr_object, fda->parent,
3302 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
3303 proc->pid, thread->pid);
3304 return_error = BR_FAILED_REPLY;
3305 return_error_param = -EINVAL;
3306 return_error_line = __LINE__;
3307 goto err_bad_parent;
3309 if (!binder_validate_fixup(target_proc, t->buffer,
3314 last_fixup_min_off)) {
3315 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
3316 proc->pid, thread->pid);
3317 return_error = BR_FAILED_REPLY;
3318 return_error_param = -EINVAL;
3319 return_error_line = __LINE__;
3320 goto err_bad_parent;
3323 * We need to read the user version of the parent
3324 * object to get the original user offset
3327 binder_get_object(proc, user_buffer, t->buffer,
3328 parent_offset, &user_object);
3329 if (user_parent_size != sizeof(user_object.bbo)) {
3330 binder_user_error("%d:%d invalid ptr object size: %zd vs %zd\n",
3331 proc->pid, thread->pid,
3333 sizeof(user_object.bbo));
3334 return_error = BR_FAILED_REPLY;
3335 return_error_param = -EINVAL;
3336 return_error_line = __LINE__;
3337 goto err_bad_parent;
3339 ret = binder_translate_fd_array(&pf_head, fda,
3340 user_buffer, parent,
3341 &user_object.bbo, t,
3342 thread, in_reply_to);
3344 ret = binder_alloc_copy_to_buffer(&target_proc->alloc,
3349 binder_txn_error("%d:%d translate fd array failed\n",
3350 thread->pid, proc->pid);
3351 return_error = BR_FAILED_REPLY;
3352 return_error_param = ret > 0 ? -EINVAL : ret;
3353 return_error_line = __LINE__;
3354 goto err_translate_failed;
3356 last_fixup_obj_off = parent_offset;
3357 last_fixup_min_off =
3358 fda->parent_offset + sizeof(u32) * fda->num_fds;
3360 case BINDER_TYPE_PTR: {
3361 struct binder_buffer_object *bp =
3362 to_binder_buffer_object(hdr);
3363 size_t buf_left = sg_buf_end_offset - sg_buf_offset;
3366 if (bp->length > buf_left) {
3367 binder_user_error("%d:%d got transaction with too large buffer\n",
3368 proc->pid, thread->pid);
3369 return_error = BR_FAILED_REPLY;
3370 return_error_param = -EINVAL;
3371 return_error_line = __LINE__;
3372 goto err_bad_offset;
3374 ret = binder_defer_copy(&sgc_head, sg_buf_offset,
3375 (const void __user *)(uintptr_t)bp->buffer,
3378 binder_txn_error("%d:%d deferred copy failed\n",
3379 thread->pid, proc->pid);
3380 return_error = BR_FAILED_REPLY;
3381 return_error_param = ret;
3382 return_error_line = __LINE__;
3383 goto err_translate_failed;
3385 /* Fixup buffer pointer to target proc address space */
3386 bp->buffer = (uintptr_t)
3387 t->buffer->user_data + sg_buf_offset;
3388 sg_buf_offset += ALIGN(bp->length, sizeof(u64));
3390 num_valid = (buffer_offset - off_start_offset) /
3391 sizeof(binder_size_t);
3392 ret = binder_fixup_parent(&pf_head, t,
3397 last_fixup_min_off);
3399 binder_alloc_copy_to_buffer(&target_proc->alloc,
3403 binder_txn_error("%d:%d failed to fixup parent\n",
3404 thread->pid, proc->pid);
3405 return_error = BR_FAILED_REPLY;
3406 return_error_param = ret;
3407 return_error_line = __LINE__;
3408 goto err_translate_failed;
3410 last_fixup_obj_off = object_offset;
3411 last_fixup_min_off = 0;
3414 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
3415 proc->pid, thread->pid, hdr->type);
3416 return_error = BR_FAILED_REPLY;
3417 return_error_param = -EINVAL;
3418 return_error_line = __LINE__;
3419 goto err_bad_object_type;
3422 /* Done processing objects, copy the rest of the buffer */
3423 if (binder_alloc_copy_user_to_buffer(
3424 &target_proc->alloc,
3425 t->buffer, user_offset,
3426 user_buffer + user_offset,
3427 tr->data_size - user_offset)) {
3428 binder_user_error("%d:%d got transaction with invalid data ptr\n",
3429 proc->pid, thread->pid);
3430 return_error = BR_FAILED_REPLY;
3431 return_error_param = -EFAULT;
3432 return_error_line = __LINE__;
3433 goto err_copy_data_failed;
3436 ret = binder_do_deferred_txn_copies(&target_proc->alloc, t->buffer,
3437 &sgc_head, &pf_head);
3439 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3440 proc->pid, thread->pid);
3441 return_error = BR_FAILED_REPLY;
3442 return_error_param = ret;
3443 return_error_line = __LINE__;
3444 goto err_copy_data_failed;
3446 if (t->buffer->oneway_spam_suspect)
3447 tcomplete->type = BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT;
3449 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
3450 t->work.type = BINDER_WORK_TRANSACTION;
3453 binder_enqueue_thread_work(thread, tcomplete);
3454 binder_inner_proc_lock(target_proc);
3455 if (target_thread->is_dead) {
3456 return_error = BR_DEAD_REPLY;
3457 binder_inner_proc_unlock(target_proc);
3458 goto err_dead_proc_or_thread;
3460 BUG_ON(t->buffer->async_transaction != 0);
3461 binder_pop_transaction_ilocked(target_thread, in_reply_to);
3462 binder_enqueue_thread_work_ilocked(target_thread, &t->work);
3463 target_proc->outstanding_txns++;
3464 binder_inner_proc_unlock(target_proc);
3465 wake_up_interruptible_sync(&target_thread->wait);
3466 binder_free_transaction(in_reply_to);
3467 } else if (!(t->flags & TF_ONE_WAY)) {
3468 BUG_ON(t->buffer->async_transaction != 0);
3469 binder_inner_proc_lock(proc);
3471 * Defer the TRANSACTION_COMPLETE, so we don't return to
3472 * userspace immediately; this allows the target process to
3473 * immediately start processing this transaction, reducing
3474 * latency. We will then return the TRANSACTION_COMPLETE when
3475 * the target replies (or there is an error).
3477 binder_enqueue_deferred_thread_work_ilocked(thread, tcomplete);
3479 t->from_parent = thread->transaction_stack;
3480 thread->transaction_stack = t;
3481 binder_inner_proc_unlock(proc);
3482 return_error = binder_proc_transaction(t,
3483 target_proc, target_thread);
3485 binder_inner_proc_lock(proc);
3486 binder_pop_transaction_ilocked(thread, t);
3487 binder_inner_proc_unlock(proc);
3488 goto err_dead_proc_or_thread;
3491 BUG_ON(target_node == NULL);
3492 BUG_ON(t->buffer->async_transaction != 1);
3493 binder_enqueue_thread_work(thread, tcomplete);
3494 return_error = binder_proc_transaction(t, target_proc, NULL);
3496 goto err_dead_proc_or_thread;
3499 binder_thread_dec_tmpref(target_thread);
3500 binder_proc_dec_tmpref(target_proc);
3502 binder_dec_node_tmpref(target_node);
3504 * write barrier to synchronize with initialization
3508 WRITE_ONCE(e->debug_id_done, t_debug_id);
3511 err_dead_proc_or_thread:
3512 binder_txn_error("%d:%d dead process or thread\n",
3513 thread->pid, proc->pid);
3514 return_error_line = __LINE__;
3515 binder_dequeue_work(proc, tcomplete);
3516 err_translate_failed:
3517 err_bad_object_type:
3520 err_copy_data_failed:
3521 binder_cleanup_deferred_txn_lists(&sgc_head, &pf_head);
3522 binder_free_txn_fixups(t);
3523 trace_binder_transaction_failed_buffer_release(t->buffer);
3524 binder_transaction_buffer_release(target_proc, NULL, t->buffer,
3525 buffer_offset, true);
3527 binder_dec_node_tmpref(target_node);
3529 t->buffer->transaction = NULL;
3530 binder_alloc_free_buf(&target_proc->alloc, t->buffer);
3531 err_binder_alloc_buf_failed:
3534 security_release_secctx(secctx, secctx_sz);
3535 err_get_secctx_failed:
3537 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3538 err_alloc_tcomplete_failed:
3539 if (trace_binder_txn_latency_free_enabled())
3540 binder_txn_latency_free(t);
3542 binder_stats_deleted(BINDER_STAT_TRANSACTION);
3546 err_empty_call_stack:
3548 err_invalid_target_handle:
3550 binder_dec_node(target_node, 1, 0);
3551 binder_dec_node_tmpref(target_node);
3554 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
3555 "%d:%d transaction %s to %d:%d failed %d/%d/%d, size %lld-%lld line %d\n",
3556 proc->pid, thread->pid, reply ? "reply" :
3557 (tr->flags & TF_ONE_WAY ? "async" : "call"),
3558 target_proc ? target_proc->pid : 0,
3559 target_thread ? target_thread->pid : 0,
3560 t_debug_id, return_error, return_error_param,
3561 (u64)tr->data_size, (u64)tr->offsets_size,
3565 binder_thread_dec_tmpref(target_thread);
3567 binder_proc_dec_tmpref(target_proc);
3570 struct binder_transaction_log_entry *fe;
3572 e->return_error = return_error;
3573 e->return_error_param = return_error_param;
3574 e->return_error_line = return_error_line;
3575 fe = binder_transaction_log_add(&binder_transaction_log_failed);
3578 * write barrier to synchronize with initialization
3582 WRITE_ONCE(e->debug_id_done, t_debug_id);
3583 WRITE_ONCE(fe->debug_id_done, t_debug_id);
3586 BUG_ON(thread->return_error.cmd != BR_OK);
3588 binder_set_txn_from_error(in_reply_to, t_debug_id,
3589 return_error, return_error_param);
3590 thread->return_error.cmd = BR_TRANSACTION_COMPLETE;
3591 binder_enqueue_thread_work(thread, &thread->return_error.work);
3592 binder_send_failed_reply(in_reply_to, return_error);
3594 binder_inner_proc_lock(proc);
3595 binder_set_extended_error(&thread->ee, t_debug_id,
3596 return_error, return_error_param);
3597 binder_inner_proc_unlock(proc);
3598 thread->return_error.cmd = return_error;
3599 binder_enqueue_thread_work(thread, &thread->return_error.work);
3604 * binder_free_buf() - free the specified buffer
3605 * @proc: binder proc that owns buffer
3606 * @buffer: buffer to be freed
3607 * @is_failure: failed to send transaction
3609 * If buffer for an async transaction, enqueue the next async
3610 * transaction from the node.
3612 * Cleanup buffer and free it.
3615 binder_free_buf(struct binder_proc *proc,
3616 struct binder_thread *thread,
3617 struct binder_buffer *buffer, bool is_failure)
3619 binder_inner_proc_lock(proc);
3620 if (buffer->transaction) {
3621 buffer->transaction->buffer = NULL;
3622 buffer->transaction = NULL;
3624 binder_inner_proc_unlock(proc);
3625 if (buffer->async_transaction && buffer->target_node) {
3626 struct binder_node *buf_node;
3627 struct binder_work *w;
3629 buf_node = buffer->target_node;
3630 binder_node_inner_lock(buf_node);
3631 BUG_ON(!buf_node->has_async_transaction);
3632 BUG_ON(buf_node->proc != proc);
3633 w = binder_dequeue_work_head_ilocked(
3634 &buf_node->async_todo);
3636 buf_node->has_async_transaction = false;
3638 binder_enqueue_work_ilocked(
3640 binder_wakeup_proc_ilocked(proc);
3642 binder_node_inner_unlock(buf_node);
3644 trace_binder_transaction_buffer_release(buffer);
3645 binder_transaction_buffer_release(proc, thread, buffer, 0, is_failure);
3646 binder_alloc_free_buf(&proc->alloc, buffer);
3649 static int binder_thread_write(struct binder_proc *proc,
3650 struct binder_thread *thread,
3651 binder_uintptr_t binder_buffer, size_t size,
3652 binder_size_t *consumed)
3655 struct binder_context *context = proc->context;
3656 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
3657 void __user *ptr = buffer + *consumed;
3658 void __user *end = buffer + size;
3660 while (ptr < end && thread->return_error.cmd == BR_OK) {
3663 if (get_user(cmd, (uint32_t __user *)ptr))
3665 ptr += sizeof(uint32_t);
3666 trace_binder_command(cmd);
3667 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
3668 atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]);
3669 atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]);
3670 atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]);
3678 const char *debug_string;
3679 bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE;
3680 bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE;
3681 struct binder_ref_data rdata;
3683 if (get_user(target, (uint32_t __user *)ptr))
3686 ptr += sizeof(uint32_t);
3688 if (increment && !target) {
3689 struct binder_node *ctx_mgr_node;
3691 mutex_lock(&context->context_mgr_node_lock);
3692 ctx_mgr_node = context->binder_context_mgr_node;
3694 if (ctx_mgr_node->proc == proc) {
3695 binder_user_error("%d:%d context manager tried to acquire desc 0\n",
3696 proc->pid, thread->pid);
3697 mutex_unlock(&context->context_mgr_node_lock);
3700 ret = binder_inc_ref_for_node(
3702 strong, NULL, &rdata);
3704 mutex_unlock(&context->context_mgr_node_lock);
3707 ret = binder_update_ref_for_handle(
3708 proc, target, increment, strong,
3710 if (!ret && rdata.desc != target) {
3711 binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
3712 proc->pid, thread->pid,
3713 target, rdata.desc);
3717 debug_string = "IncRefs";
3720 debug_string = "Acquire";
3723 debug_string = "Release";
3727 debug_string = "DecRefs";
3731 binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
3732 proc->pid, thread->pid, debug_string,
3733 strong, target, ret);
3736 binder_debug(BINDER_DEBUG_USER_REFS,
3737 "%d:%d %s ref %d desc %d s %d w %d\n",
3738 proc->pid, thread->pid, debug_string,
3739 rdata.debug_id, rdata.desc, rdata.strong,
3743 case BC_INCREFS_DONE:
3744 case BC_ACQUIRE_DONE: {
3745 binder_uintptr_t node_ptr;
3746 binder_uintptr_t cookie;
3747 struct binder_node *node;
3750 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
3752 ptr += sizeof(binder_uintptr_t);
3753 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
3755 ptr += sizeof(binder_uintptr_t);
3756 node = binder_get_node(proc, node_ptr);
3758 binder_user_error("%d:%d %s u%016llx no match\n",
3759 proc->pid, thread->pid,
3760 cmd == BC_INCREFS_DONE ?
3766 if (cookie != node->cookie) {
3767 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
3768 proc->pid, thread->pid,
3769 cmd == BC_INCREFS_DONE ?
3770 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
3771 (u64)node_ptr, node->debug_id,
3772 (u64)cookie, (u64)node->cookie);
3773 binder_put_node(node);
3776 binder_node_inner_lock(node);
3777 if (cmd == BC_ACQUIRE_DONE) {
3778 if (node->pending_strong_ref == 0) {
3779 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
3780 proc->pid, thread->pid,
3782 binder_node_inner_unlock(node);
3783 binder_put_node(node);
3786 node->pending_strong_ref = 0;
3788 if (node->pending_weak_ref == 0) {
3789 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
3790 proc->pid, thread->pid,
3792 binder_node_inner_unlock(node);
3793 binder_put_node(node);
3796 node->pending_weak_ref = 0;
3798 free_node = binder_dec_node_nilocked(node,
3799 cmd == BC_ACQUIRE_DONE, 0);
3801 binder_debug(BINDER_DEBUG_USER_REFS,
3802 "%d:%d %s node %d ls %d lw %d tr %d\n",
3803 proc->pid, thread->pid,
3804 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
3805 node->debug_id, node->local_strong_refs,
3806 node->local_weak_refs, node->tmp_refs);
3807 binder_node_inner_unlock(node);
3808 binder_put_node(node);
3811 case BC_ATTEMPT_ACQUIRE:
3812 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
3814 case BC_ACQUIRE_RESULT:
3815 pr_err("BC_ACQUIRE_RESULT not supported\n");
3818 case BC_FREE_BUFFER: {
3819 binder_uintptr_t data_ptr;
3820 struct binder_buffer *buffer;
3822 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
3824 ptr += sizeof(binder_uintptr_t);
3826 buffer = binder_alloc_prepare_to_free(&proc->alloc,
3828 if (IS_ERR_OR_NULL(buffer)) {
3829 if (PTR_ERR(buffer) == -EPERM) {
3831 "%d:%d BC_FREE_BUFFER u%016llx matched unreturned or currently freeing buffer\n",
3832 proc->pid, thread->pid,
3836 "%d:%d BC_FREE_BUFFER u%016llx no match\n",
3837 proc->pid, thread->pid,
3842 binder_debug(BINDER_DEBUG_FREE_BUFFER,
3843 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
3844 proc->pid, thread->pid, (u64)data_ptr,
3846 buffer->transaction ? "active" : "finished");
3847 binder_free_buf(proc, thread, buffer, false);
3851 case BC_TRANSACTION_SG:
3853 struct binder_transaction_data_sg tr;
3855 if (copy_from_user(&tr, ptr, sizeof(tr)))
3858 binder_transaction(proc, thread, &tr.transaction_data,
3859 cmd == BC_REPLY_SG, tr.buffers_size);
3862 case BC_TRANSACTION:
3864 struct binder_transaction_data tr;
3866 if (copy_from_user(&tr, ptr, sizeof(tr)))
3869 binder_transaction(proc, thread, &tr,
3870 cmd == BC_REPLY, 0);
3874 case BC_REGISTER_LOOPER:
3875 binder_debug(BINDER_DEBUG_THREADS,
3876 "%d:%d BC_REGISTER_LOOPER\n",
3877 proc->pid, thread->pid);
3878 binder_inner_proc_lock(proc);
3879 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
3880 thread->looper |= BINDER_LOOPER_STATE_INVALID;
3881 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
3882 proc->pid, thread->pid);
3883 } else if (proc->requested_threads == 0) {
3884 thread->looper |= BINDER_LOOPER_STATE_INVALID;
3885 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
3886 proc->pid, thread->pid);
3888 proc->requested_threads--;
3889 proc->requested_threads_started++;
3891 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
3892 binder_inner_proc_unlock(proc);
3894 case BC_ENTER_LOOPER:
3895 binder_debug(BINDER_DEBUG_THREADS,
3896 "%d:%d BC_ENTER_LOOPER\n",
3897 proc->pid, thread->pid);
3898 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
3899 thread->looper |= BINDER_LOOPER_STATE_INVALID;
3900 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
3901 proc->pid, thread->pid);
3903 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
3905 case BC_EXIT_LOOPER:
3906 binder_debug(BINDER_DEBUG_THREADS,
3907 "%d:%d BC_EXIT_LOOPER\n",
3908 proc->pid, thread->pid);
3909 thread->looper |= BINDER_LOOPER_STATE_EXITED;
3912 case BC_REQUEST_DEATH_NOTIFICATION:
3913 case BC_CLEAR_DEATH_NOTIFICATION: {
3915 binder_uintptr_t cookie;
3916 struct binder_ref *ref;
3917 struct binder_ref_death *death = NULL;
3919 if (get_user(target, (uint32_t __user *)ptr))
3921 ptr += sizeof(uint32_t);
3922 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
3924 ptr += sizeof(binder_uintptr_t);
3925 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3927 * Allocate memory for death notification
3928 * before taking lock
3930 death = kzalloc(sizeof(*death), GFP_KERNEL);
3931 if (death == NULL) {
3932 WARN_ON(thread->return_error.cmd !=
3934 thread->return_error.cmd = BR_ERROR;
3935 binder_enqueue_thread_work(
3937 &thread->return_error.work);
3939 BINDER_DEBUG_FAILED_TRANSACTION,
3940 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
3941 proc->pid, thread->pid);
3945 binder_proc_lock(proc);
3946 ref = binder_get_ref_olocked(proc, target, false);
3948 binder_user_error("%d:%d %s invalid ref %d\n",
3949 proc->pid, thread->pid,
3950 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3951 "BC_REQUEST_DEATH_NOTIFICATION" :
3952 "BC_CLEAR_DEATH_NOTIFICATION",
3954 binder_proc_unlock(proc);
3959 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
3960 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
3961 proc->pid, thread->pid,
3962 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3963 "BC_REQUEST_DEATH_NOTIFICATION" :
3964 "BC_CLEAR_DEATH_NOTIFICATION",
3965 (u64)cookie, ref->data.debug_id,
3966 ref->data.desc, ref->data.strong,
3967 ref->data.weak, ref->node->debug_id);
3969 binder_node_lock(ref->node);
3970 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3972 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
3973 proc->pid, thread->pid);
3974 binder_node_unlock(ref->node);
3975 binder_proc_unlock(proc);
3979 binder_stats_created(BINDER_STAT_DEATH);
3980 INIT_LIST_HEAD(&death->work.entry);
3981 death->cookie = cookie;
3983 if (ref->node->proc == NULL) {
3984 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
3986 binder_inner_proc_lock(proc);
3987 binder_enqueue_work_ilocked(
3988 &ref->death->work, &proc->todo);
3989 binder_wakeup_proc_ilocked(proc);
3990 binder_inner_proc_unlock(proc);
3993 if (ref->death == NULL) {
3994 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
3995 proc->pid, thread->pid);
3996 binder_node_unlock(ref->node);
3997 binder_proc_unlock(proc);
4001 if (death->cookie != cookie) {
4002 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
4003 proc->pid, thread->pid,
4006 binder_node_unlock(ref->node);
4007 binder_proc_unlock(proc);
4011 binder_inner_proc_lock(proc);
4012 if (list_empty(&death->work.entry)) {
4013 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
4014 if (thread->looper &
4015 (BINDER_LOOPER_STATE_REGISTERED |
4016 BINDER_LOOPER_STATE_ENTERED))
4017 binder_enqueue_thread_work_ilocked(
4021 binder_enqueue_work_ilocked(
4024 binder_wakeup_proc_ilocked(
4028 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
4029 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
4031 binder_inner_proc_unlock(proc);
4033 binder_node_unlock(ref->node);
4034 binder_proc_unlock(proc);
4036 case BC_DEAD_BINDER_DONE: {
4037 struct binder_work *w;
4038 binder_uintptr_t cookie;
4039 struct binder_ref_death *death = NULL;
4041 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
4044 ptr += sizeof(cookie);
4045 binder_inner_proc_lock(proc);
4046 list_for_each_entry(w, &proc->delivered_death,
4048 struct binder_ref_death *tmp_death =
4050 struct binder_ref_death,
4053 if (tmp_death->cookie == cookie) {
4058 binder_debug(BINDER_DEBUG_DEAD_BINDER,
4059 "%d:%d BC_DEAD_BINDER_DONE %016llx found %pK\n",
4060 proc->pid, thread->pid, (u64)cookie,
4062 if (death == NULL) {
4063 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
4064 proc->pid, thread->pid, (u64)cookie);
4065 binder_inner_proc_unlock(proc);
4068 binder_dequeue_work_ilocked(&death->work);
4069 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
4070 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
4071 if (thread->looper &
4072 (BINDER_LOOPER_STATE_REGISTERED |
4073 BINDER_LOOPER_STATE_ENTERED))
4074 binder_enqueue_thread_work_ilocked(
4075 thread, &death->work);
4077 binder_enqueue_work_ilocked(
4080 binder_wakeup_proc_ilocked(proc);
4083 binder_inner_proc_unlock(proc);
4087 pr_err("%d:%d unknown command %u\n",
4088 proc->pid, thread->pid, cmd);
4091 *consumed = ptr - buffer;
4096 static void binder_stat_br(struct binder_proc *proc,
4097 struct binder_thread *thread, uint32_t cmd)
4099 trace_binder_return(cmd);
4100 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
4101 atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
4102 atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
4103 atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
4107 static int binder_put_node_cmd(struct binder_proc *proc,
4108 struct binder_thread *thread,
4110 binder_uintptr_t node_ptr,
4111 binder_uintptr_t node_cookie,
4113 uint32_t cmd, const char *cmd_name)
4115 void __user *ptr = *ptrp;
4117 if (put_user(cmd, (uint32_t __user *)ptr))
4119 ptr += sizeof(uint32_t);
4121 if (put_user(node_ptr, (binder_uintptr_t __user *)ptr))
4123 ptr += sizeof(binder_uintptr_t);
4125 if (put_user(node_cookie, (binder_uintptr_t __user *)ptr))
4127 ptr += sizeof(binder_uintptr_t);
4129 binder_stat_br(proc, thread, cmd);
4130 binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n",
4131 proc->pid, thread->pid, cmd_name, node_debug_id,
4132 (u64)node_ptr, (u64)node_cookie);
4138 static int binder_wait_for_work(struct binder_thread *thread,
4142 struct binder_proc *proc = thread->proc;
4145 freezer_do_not_count();
4146 binder_inner_proc_lock(proc);
4148 prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE);
4149 if (binder_has_work_ilocked(thread, do_proc_work))
4152 list_add(&thread->waiting_thread_node,
4153 &proc->waiting_threads);
4154 binder_inner_proc_unlock(proc);
4156 binder_inner_proc_lock(proc);
4157 list_del_init(&thread->waiting_thread_node);
4158 if (signal_pending(current)) {
4163 finish_wait(&thread->wait, &wait);
4164 binder_inner_proc_unlock(proc);
4171 * binder_apply_fd_fixups() - finish fd translation
4172 * @proc: binder_proc associated @t->buffer
4173 * @t: binder transaction with list of fd fixups
4175 * Now that we are in the context of the transaction target
4176 * process, we can allocate and install fds. Process the
4177 * list of fds to translate and fixup the buffer with the
4178 * new fds first and only then install the files.
4180 * If we fail to allocate an fd, skip the install and release
4181 * any fds that have already been allocated.
4183 static int binder_apply_fd_fixups(struct binder_proc *proc,
4184 struct binder_transaction *t)
4186 struct binder_txn_fd_fixup *fixup, *tmp;
4189 list_for_each_entry(fixup, &t->fd_fixups, fixup_entry) {
4190 int fd = get_unused_fd_flags(O_CLOEXEC);
4193 binder_debug(BINDER_DEBUG_TRANSACTION,
4194 "failed fd fixup txn %d fd %d\n",
4199 binder_debug(BINDER_DEBUG_TRANSACTION,
4200 "fd fixup txn %d fd %d\n",
4202 trace_binder_transaction_fd_recv(t, fd, fixup->offset);
4203 fixup->target_fd = fd;
4204 if (binder_alloc_copy_to_buffer(&proc->alloc, t->buffer,
4211 list_for_each_entry_safe(fixup, tmp, &t->fd_fixups, fixup_entry) {
4212 fd_install(fixup->target_fd, fixup->file);
4213 list_del(&fixup->fixup_entry);
4220 binder_free_txn_fixups(t);
4224 static int binder_thread_read(struct binder_proc *proc,
4225 struct binder_thread *thread,
4226 binder_uintptr_t binder_buffer, size_t size,
4227 binder_size_t *consumed, int non_block)
4229 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
4230 void __user *ptr = buffer + *consumed;
4231 void __user *end = buffer + size;
4234 int wait_for_proc_work;
4236 if (*consumed == 0) {
4237 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
4239 ptr += sizeof(uint32_t);
4243 binder_inner_proc_lock(proc);
4244 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
4245 binder_inner_proc_unlock(proc);
4247 thread->looper |= BINDER_LOOPER_STATE_WAITING;
4249 trace_binder_wait_for_work(wait_for_proc_work,
4250 !!thread->transaction_stack,
4251 !binder_worklist_empty(proc, &thread->todo));
4252 if (wait_for_proc_work) {
4253 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4254 BINDER_LOOPER_STATE_ENTERED))) {
4255 binder_user_error("%d:%d ERROR: Thread waiting for process work before calling BC_REGISTER_LOOPER or BC_ENTER_LOOPER (state %x)\n",
4256 proc->pid, thread->pid, thread->looper);
4257 wait_event_interruptible(binder_user_error_wait,
4258 binder_stop_on_user_error < 2);
4260 binder_set_nice(proc->default_priority);
4264 if (!binder_has_work(thread, wait_for_proc_work))
4267 ret = binder_wait_for_work(thread, wait_for_proc_work);
4270 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
4277 struct binder_transaction_data_secctx tr;
4278 struct binder_transaction_data *trd = &tr.transaction_data;
4279 struct binder_work *w = NULL;
4280 struct list_head *list = NULL;
4281 struct binder_transaction *t = NULL;
4282 struct binder_thread *t_from;
4283 size_t trsize = sizeof(*trd);
4285 binder_inner_proc_lock(proc);
4286 if (!binder_worklist_empty_ilocked(&thread->todo))
4287 list = &thread->todo;
4288 else if (!binder_worklist_empty_ilocked(&proc->todo) &&
4292 binder_inner_proc_unlock(proc);
4295 if (ptr - buffer == 4 && !thread->looper_need_return)
4300 if (end - ptr < sizeof(tr) + 4) {
4301 binder_inner_proc_unlock(proc);
4304 w = binder_dequeue_work_head_ilocked(list);
4305 if (binder_worklist_empty_ilocked(&thread->todo))
4306 thread->process_todo = false;
4309 case BINDER_WORK_TRANSACTION: {
4310 binder_inner_proc_unlock(proc);
4311 t = container_of(w, struct binder_transaction, work);
4313 case BINDER_WORK_RETURN_ERROR: {
4314 struct binder_error *e = container_of(
4315 w, struct binder_error, work);
4317 WARN_ON(e->cmd == BR_OK);
4318 binder_inner_proc_unlock(proc);
4319 if (put_user(e->cmd, (uint32_t __user *)ptr))
4323 ptr += sizeof(uint32_t);
4325 binder_stat_br(proc, thread, cmd);
4327 case BINDER_WORK_TRANSACTION_COMPLETE:
4328 case BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT: {
4329 if (proc->oneway_spam_detection_enabled &&
4330 w->type == BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT)
4331 cmd = BR_ONEWAY_SPAM_SUSPECT;
4333 cmd = BR_TRANSACTION_COMPLETE;
4334 binder_inner_proc_unlock(proc);
4336 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4337 if (put_user(cmd, (uint32_t __user *)ptr))
4339 ptr += sizeof(uint32_t);
4341 binder_stat_br(proc, thread, cmd);
4342 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
4343 "%d:%d BR_TRANSACTION_COMPLETE\n",
4344 proc->pid, thread->pid);
4346 case BINDER_WORK_NODE: {
4347 struct binder_node *node = container_of(w, struct binder_node, work);
4349 binder_uintptr_t node_ptr = node->ptr;
4350 binder_uintptr_t node_cookie = node->cookie;
4351 int node_debug_id = node->debug_id;
4354 void __user *orig_ptr = ptr;
4356 BUG_ON(proc != node->proc);
4357 strong = node->internal_strong_refs ||
4358 node->local_strong_refs;
4359 weak = !hlist_empty(&node->refs) ||
4360 node->local_weak_refs ||
4361 node->tmp_refs || strong;
4362 has_strong_ref = node->has_strong_ref;
4363 has_weak_ref = node->has_weak_ref;
4365 if (weak && !has_weak_ref) {
4366 node->has_weak_ref = 1;
4367 node->pending_weak_ref = 1;
4368 node->local_weak_refs++;
4370 if (strong && !has_strong_ref) {
4371 node->has_strong_ref = 1;
4372 node->pending_strong_ref = 1;
4373 node->local_strong_refs++;
4375 if (!strong && has_strong_ref)
4376 node->has_strong_ref = 0;
4377 if (!weak && has_weak_ref)
4378 node->has_weak_ref = 0;
4379 if (!weak && !strong) {
4380 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4381 "%d:%d node %d u%016llx c%016llx deleted\n",
4382 proc->pid, thread->pid,
4386 rb_erase(&node->rb_node, &proc->nodes);
4387 binder_inner_proc_unlock(proc);
4388 binder_node_lock(node);
4390 * Acquire the node lock before freeing the
4391 * node to serialize with other threads that
4392 * may have been holding the node lock while
4393 * decrementing this node (avoids race where
4394 * this thread frees while the other thread
4395 * is unlocking the node after the final
4398 binder_node_unlock(node);
4399 binder_free_node(node);
4401 binder_inner_proc_unlock(proc);
4403 if (weak && !has_weak_ref)
4404 ret = binder_put_node_cmd(
4405 proc, thread, &ptr, node_ptr,
4406 node_cookie, node_debug_id,
4407 BR_INCREFS, "BR_INCREFS");
4408 if (!ret && strong && !has_strong_ref)
4409 ret = binder_put_node_cmd(
4410 proc, thread, &ptr, node_ptr,
4411 node_cookie, node_debug_id,
4412 BR_ACQUIRE, "BR_ACQUIRE");
4413 if (!ret && !strong && has_strong_ref)
4414 ret = binder_put_node_cmd(
4415 proc, thread, &ptr, node_ptr,
4416 node_cookie, node_debug_id,
4417 BR_RELEASE, "BR_RELEASE");
4418 if (!ret && !weak && has_weak_ref)
4419 ret = binder_put_node_cmd(
4420 proc, thread, &ptr, node_ptr,
4421 node_cookie, node_debug_id,
4422 BR_DECREFS, "BR_DECREFS");
4423 if (orig_ptr == ptr)
4424 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4425 "%d:%d node %d u%016llx c%016llx state unchanged\n",
4426 proc->pid, thread->pid,
4433 case BINDER_WORK_DEAD_BINDER:
4434 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4435 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4436 struct binder_ref_death *death;
4438 binder_uintptr_t cookie;
4440 death = container_of(w, struct binder_ref_death, work);
4441 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
4442 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
4444 cmd = BR_DEAD_BINDER;
4445 cookie = death->cookie;
4447 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
4448 "%d:%d %s %016llx\n",
4449 proc->pid, thread->pid,
4450 cmd == BR_DEAD_BINDER ?
4452 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
4454 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
4455 binder_inner_proc_unlock(proc);
4457 binder_stats_deleted(BINDER_STAT_DEATH);
4459 binder_enqueue_work_ilocked(
4460 w, &proc->delivered_death);
4461 binder_inner_proc_unlock(proc);
4463 if (put_user(cmd, (uint32_t __user *)ptr))
4465 ptr += sizeof(uint32_t);
4466 if (put_user(cookie,
4467 (binder_uintptr_t __user *)ptr))
4469 ptr += sizeof(binder_uintptr_t);
4470 binder_stat_br(proc, thread, cmd);
4471 if (cmd == BR_DEAD_BINDER)
4472 goto done; /* DEAD_BINDER notifications can cause transactions */
4475 binder_inner_proc_unlock(proc);
4476 pr_err("%d:%d: bad work type %d\n",
4477 proc->pid, thread->pid, w->type);
4484 BUG_ON(t->buffer == NULL);
4485 if (t->buffer->target_node) {
4486 struct binder_node *target_node = t->buffer->target_node;
4488 trd->target.ptr = target_node->ptr;
4489 trd->cookie = target_node->cookie;
4490 t->saved_priority = task_nice(current);
4491 if (t->priority < target_node->min_priority &&
4492 !(t->flags & TF_ONE_WAY))
4493 binder_set_nice(t->priority);
4494 else if (!(t->flags & TF_ONE_WAY) ||
4495 t->saved_priority > target_node->min_priority)
4496 binder_set_nice(target_node->min_priority);
4497 cmd = BR_TRANSACTION;
4499 trd->target.ptr = 0;
4503 trd->code = t->code;
4504 trd->flags = t->flags;
4505 trd->sender_euid = from_kuid(current_user_ns(), t->sender_euid);
4507 t_from = binder_get_txn_from(t);
4509 struct task_struct *sender = t_from->proc->tsk;
4512 task_tgid_nr_ns(sender,
4513 task_active_pid_ns(current));
4515 trd->sender_pid = 0;
4518 ret = binder_apply_fd_fixups(proc, t);
4520 struct binder_buffer *buffer = t->buffer;
4521 bool oneway = !!(t->flags & TF_ONE_WAY);
4522 int tid = t->debug_id;
4525 binder_thread_dec_tmpref(t_from);
4526 buffer->transaction = NULL;
4527 binder_cleanup_transaction(t, "fd fixups failed",
4529 binder_free_buf(proc, thread, buffer, true);
4530 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
4531 "%d:%d %stransaction %d fd fixups failed %d/%d, line %d\n",
4532 proc->pid, thread->pid,
4534 (cmd == BR_REPLY ? "reply " : ""),
4535 tid, BR_FAILED_REPLY, ret, __LINE__);
4536 if (cmd == BR_REPLY) {
4537 cmd = BR_FAILED_REPLY;
4538 if (put_user(cmd, (uint32_t __user *)ptr))
4540 ptr += sizeof(uint32_t);
4541 binder_stat_br(proc, thread, cmd);
4546 trd->data_size = t->buffer->data_size;
4547 trd->offsets_size = t->buffer->offsets_size;
4548 trd->data.ptr.buffer = (uintptr_t)t->buffer->user_data;
4549 trd->data.ptr.offsets = trd->data.ptr.buffer +
4550 ALIGN(t->buffer->data_size,
4553 tr.secctx = t->security_ctx;
4554 if (t->security_ctx) {
4555 cmd = BR_TRANSACTION_SEC_CTX;
4556 trsize = sizeof(tr);
4558 if (put_user(cmd, (uint32_t __user *)ptr)) {
4560 binder_thread_dec_tmpref(t_from);
4562 binder_cleanup_transaction(t, "put_user failed",
4567 ptr += sizeof(uint32_t);
4568 if (copy_to_user(ptr, &tr, trsize)) {
4570 binder_thread_dec_tmpref(t_from);
4572 binder_cleanup_transaction(t, "copy_to_user failed",
4579 trace_binder_transaction_received(t);
4580 binder_stat_br(proc, thread, cmd);
4581 binder_debug(BINDER_DEBUG_TRANSACTION,
4582 "%d:%d %s %d %d:%d, cmd %u size %zd-%zd ptr %016llx-%016llx\n",
4583 proc->pid, thread->pid,
4584 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
4585 (cmd == BR_TRANSACTION_SEC_CTX) ?
4586 "BR_TRANSACTION_SEC_CTX" : "BR_REPLY",
4587 t->debug_id, t_from ? t_from->proc->pid : 0,
4588 t_from ? t_from->pid : 0, cmd,
4589 t->buffer->data_size, t->buffer->offsets_size,
4590 (u64)trd->data.ptr.buffer,
4591 (u64)trd->data.ptr.offsets);
4594 binder_thread_dec_tmpref(t_from);
4595 t->buffer->allow_user_free = 1;
4596 if (cmd != BR_REPLY && !(t->flags & TF_ONE_WAY)) {
4597 binder_inner_proc_lock(thread->proc);
4598 t->to_parent = thread->transaction_stack;
4599 t->to_thread = thread;
4600 thread->transaction_stack = t;
4601 binder_inner_proc_unlock(thread->proc);
4603 binder_free_transaction(t);
4610 *consumed = ptr - buffer;
4611 binder_inner_proc_lock(proc);
4612 if (proc->requested_threads == 0 &&
4613 list_empty(&thread->proc->waiting_threads) &&
4614 proc->requested_threads_started < proc->max_threads &&
4615 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4616 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
4617 /*spawn a new thread if we leave this out */) {
4618 proc->requested_threads++;
4619 binder_inner_proc_unlock(proc);
4620 binder_debug(BINDER_DEBUG_THREADS,
4621 "%d:%d BR_SPAWN_LOOPER\n",
4622 proc->pid, thread->pid);
4623 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
4625 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
4627 binder_inner_proc_unlock(proc);
4631 static void binder_release_work(struct binder_proc *proc,
4632 struct list_head *list)
4634 struct binder_work *w;
4635 enum binder_work_type wtype;
4638 binder_inner_proc_lock(proc);
4639 w = binder_dequeue_work_head_ilocked(list);
4640 wtype = w ? w->type : 0;
4641 binder_inner_proc_unlock(proc);
4646 case BINDER_WORK_TRANSACTION: {
4647 struct binder_transaction *t;
4649 t = container_of(w, struct binder_transaction, work);
4651 binder_cleanup_transaction(t, "process died.",
4654 case BINDER_WORK_RETURN_ERROR: {
4655 struct binder_error *e = container_of(
4656 w, struct binder_error, work);
4658 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4659 "undelivered TRANSACTION_ERROR: %u\n",
4662 case BINDER_WORK_TRANSACTION_COMPLETE: {
4663 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4664 "undelivered TRANSACTION_COMPLETE\n");
4666 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4668 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4669 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4670 struct binder_ref_death *death;
4672 death = container_of(w, struct binder_ref_death, work);
4673 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4674 "undelivered death notification, %016llx\n",
4675 (u64)death->cookie);
4677 binder_stats_deleted(BINDER_STAT_DEATH);
4679 case BINDER_WORK_NODE:
4682 pr_err("unexpected work type, %d, not freed\n",
4690 static struct binder_thread *binder_get_thread_ilocked(
4691 struct binder_proc *proc, struct binder_thread *new_thread)
4693 struct binder_thread *thread = NULL;
4694 struct rb_node *parent = NULL;
4695 struct rb_node **p = &proc->threads.rb_node;
4699 thread = rb_entry(parent, struct binder_thread, rb_node);
4701 if (current->pid < thread->pid)
4703 else if (current->pid > thread->pid)
4704 p = &(*p)->rb_right;
4710 thread = new_thread;
4711 binder_stats_created(BINDER_STAT_THREAD);
4712 thread->proc = proc;
4713 thread->pid = current->pid;
4714 atomic_set(&thread->tmp_ref, 0);
4715 init_waitqueue_head(&thread->wait);
4716 INIT_LIST_HEAD(&thread->todo);
4717 rb_link_node(&thread->rb_node, parent, p);
4718 rb_insert_color(&thread->rb_node, &proc->threads);
4719 thread->looper_need_return = true;
4720 thread->return_error.work.type = BINDER_WORK_RETURN_ERROR;
4721 thread->return_error.cmd = BR_OK;
4722 thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR;
4723 thread->reply_error.cmd = BR_OK;
4724 thread->ee.command = BR_OK;
4725 INIT_LIST_HEAD(&new_thread->waiting_thread_node);
4729 static struct binder_thread *binder_get_thread(struct binder_proc *proc)
4731 struct binder_thread *thread;
4732 struct binder_thread *new_thread;
4734 binder_inner_proc_lock(proc);
4735 thread = binder_get_thread_ilocked(proc, NULL);
4736 binder_inner_proc_unlock(proc);
4738 new_thread = kzalloc(sizeof(*thread), GFP_KERNEL);
4739 if (new_thread == NULL)
4741 binder_inner_proc_lock(proc);
4742 thread = binder_get_thread_ilocked(proc, new_thread);
4743 binder_inner_proc_unlock(proc);
4744 if (thread != new_thread)
4750 static void binder_free_proc(struct binder_proc *proc)
4752 struct binder_device *device;
4754 BUG_ON(!list_empty(&proc->todo));
4755 BUG_ON(!list_empty(&proc->delivered_death));
4756 if (proc->outstanding_txns)
4757 pr_warn("%s: Unexpected outstanding_txns %d\n",
4758 __func__, proc->outstanding_txns);
4759 device = container_of(proc->context, struct binder_device, context);
4760 if (refcount_dec_and_test(&device->ref)) {
4761 kfree(proc->context->name);
4764 binder_alloc_deferred_release(&proc->alloc);
4765 put_task_struct(proc->tsk);
4766 put_cred(proc->cred);
4767 binder_stats_deleted(BINDER_STAT_PROC);
4771 static void binder_free_thread(struct binder_thread *thread)
4773 BUG_ON(!list_empty(&thread->todo));
4774 binder_stats_deleted(BINDER_STAT_THREAD);
4775 binder_proc_dec_tmpref(thread->proc);
4779 static int binder_thread_release(struct binder_proc *proc,
4780 struct binder_thread *thread)
4782 struct binder_transaction *t;
4783 struct binder_transaction *send_reply = NULL;
4784 int active_transactions = 0;
4785 struct binder_transaction *last_t = NULL;
4787 binder_inner_proc_lock(thread->proc);
4789 * take a ref on the proc so it survives
4790 * after we remove this thread from proc->threads.
4791 * The corresponding dec is when we actually
4792 * free the thread in binder_free_thread()
4796 * take a ref on this thread to ensure it
4797 * survives while we are releasing it
4799 atomic_inc(&thread->tmp_ref);
4800 rb_erase(&thread->rb_node, &proc->threads);
4801 t = thread->transaction_stack;
4803 spin_lock(&t->lock);
4804 if (t->to_thread == thread)
4807 __acquire(&t->lock);
4809 thread->is_dead = true;
4813 active_transactions++;
4814 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4815 "release %d:%d transaction %d %s, still active\n",
4816 proc->pid, thread->pid,
4818 (t->to_thread == thread) ? "in" : "out");
4820 if (t->to_thread == thread) {
4821 thread->proc->outstanding_txns--;
4823 t->to_thread = NULL;
4825 t->buffer->transaction = NULL;
4829 } else if (t->from == thread) {
4834 spin_unlock(&last_t->lock);
4836 spin_lock(&t->lock);
4838 __acquire(&t->lock);
4840 /* annotation for sparse, lock not acquired in last iteration above */
4841 __release(&t->lock);
4844 * If this thread used poll, make sure we remove the waitqueue from any
4845 * poll data structures holding it.
4847 if (thread->looper & BINDER_LOOPER_STATE_POLL)
4848 wake_up_pollfree(&thread->wait);
4850 binder_inner_proc_unlock(thread->proc);
4853 * This is needed to avoid races between wake_up_pollfree() above and
4854 * someone else removing the last entry from the queue for other reasons
4855 * (e.g. ep_remove_wait_queue() being called due to an epoll file
4856 * descriptor being closed). Such other users hold an RCU read lock, so
4857 * we can be sure they're done after we call synchronize_rcu().
4859 if (thread->looper & BINDER_LOOPER_STATE_POLL)
4863 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
4864 binder_release_work(proc, &thread->todo);
4865 binder_thread_dec_tmpref(thread);
4866 return active_transactions;
4869 static __poll_t binder_poll(struct file *filp,
4870 struct poll_table_struct *wait)
4872 struct binder_proc *proc = filp->private_data;
4873 struct binder_thread *thread = NULL;
4874 bool wait_for_proc_work;
4876 thread = binder_get_thread(proc);
4880 binder_inner_proc_lock(thread->proc);
4881 thread->looper |= BINDER_LOOPER_STATE_POLL;
4882 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
4884 binder_inner_proc_unlock(thread->proc);
4886 poll_wait(filp, &thread->wait, wait);
4888 if (binder_has_work(thread, wait_for_proc_work))
4894 static int binder_ioctl_write_read(struct file *filp,
4895 unsigned int cmd, unsigned long arg,
4896 struct binder_thread *thread)
4899 struct binder_proc *proc = filp->private_data;
4900 unsigned int size = _IOC_SIZE(cmd);
4901 void __user *ubuf = (void __user *)arg;
4902 struct binder_write_read bwr;
4904 if (size != sizeof(struct binder_write_read)) {
4908 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
4912 binder_debug(BINDER_DEBUG_READ_WRITE,
4913 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
4914 proc->pid, thread->pid,
4915 (u64)bwr.write_size, (u64)bwr.write_buffer,
4916 (u64)bwr.read_size, (u64)bwr.read_buffer);
4918 if (bwr.write_size > 0) {
4919 ret = binder_thread_write(proc, thread,
4922 &bwr.write_consumed);
4923 trace_binder_write_done(ret);
4925 bwr.read_consumed = 0;
4926 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4931 if (bwr.read_size > 0) {
4932 ret = binder_thread_read(proc, thread, bwr.read_buffer,
4935 filp->f_flags & O_NONBLOCK);
4936 trace_binder_read_done(ret);
4937 binder_inner_proc_lock(proc);
4938 if (!binder_worklist_empty_ilocked(&proc->todo))
4939 binder_wakeup_proc_ilocked(proc);
4940 binder_inner_proc_unlock(proc);
4942 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4947 binder_debug(BINDER_DEBUG_READ_WRITE,
4948 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
4949 proc->pid, thread->pid,
4950 (u64)bwr.write_consumed, (u64)bwr.write_size,
4951 (u64)bwr.read_consumed, (u64)bwr.read_size);
4952 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
4960 static int binder_ioctl_set_ctx_mgr(struct file *filp,
4961 struct flat_binder_object *fbo)
4964 struct binder_proc *proc = filp->private_data;
4965 struct binder_context *context = proc->context;
4966 struct binder_node *new_node;
4967 kuid_t curr_euid = current_euid();
4969 mutex_lock(&context->context_mgr_node_lock);
4970 if (context->binder_context_mgr_node) {
4971 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
4975 ret = security_binder_set_context_mgr(proc->cred);
4978 if (uid_valid(context->binder_context_mgr_uid)) {
4979 if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
4980 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
4981 from_kuid(&init_user_ns, curr_euid),
4982 from_kuid(&init_user_ns,
4983 context->binder_context_mgr_uid));
4988 context->binder_context_mgr_uid = curr_euid;
4990 new_node = binder_new_node(proc, fbo);
4995 binder_node_lock(new_node);
4996 new_node->local_weak_refs++;
4997 new_node->local_strong_refs++;
4998 new_node->has_strong_ref = 1;
4999 new_node->has_weak_ref = 1;
5000 context->binder_context_mgr_node = new_node;
5001 binder_node_unlock(new_node);
5002 binder_put_node(new_node);
5004 mutex_unlock(&context->context_mgr_node_lock);
5008 static int binder_ioctl_get_node_info_for_ref(struct binder_proc *proc,
5009 struct binder_node_info_for_ref *info)
5011 struct binder_node *node;
5012 struct binder_context *context = proc->context;
5013 __u32 handle = info->handle;
5015 if (info->strong_count || info->weak_count || info->reserved1 ||
5016 info->reserved2 || info->reserved3) {
5017 binder_user_error("%d BINDER_GET_NODE_INFO_FOR_REF: only handle may be non-zero.",
5022 /* This ioctl may only be used by the context manager */
5023 mutex_lock(&context->context_mgr_node_lock);
5024 if (!context->binder_context_mgr_node ||
5025 context->binder_context_mgr_node->proc != proc) {
5026 mutex_unlock(&context->context_mgr_node_lock);
5029 mutex_unlock(&context->context_mgr_node_lock);
5031 node = binder_get_node_from_ref(proc, handle, true, NULL);
5035 info->strong_count = node->local_strong_refs +
5036 node->internal_strong_refs;
5037 info->weak_count = node->local_weak_refs;
5039 binder_put_node(node);
5044 static int binder_ioctl_get_node_debug_info(struct binder_proc *proc,
5045 struct binder_node_debug_info *info)
5048 binder_uintptr_t ptr = info->ptr;
5050 memset(info, 0, sizeof(*info));
5052 binder_inner_proc_lock(proc);
5053 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
5054 struct binder_node *node = rb_entry(n, struct binder_node,
5056 if (node->ptr > ptr) {
5057 info->ptr = node->ptr;
5058 info->cookie = node->cookie;
5059 info->has_strong_ref = node->has_strong_ref;
5060 info->has_weak_ref = node->has_weak_ref;
5064 binder_inner_proc_unlock(proc);
5069 static bool binder_txns_pending_ilocked(struct binder_proc *proc)
5072 struct binder_thread *thread;
5074 if (proc->outstanding_txns > 0)
5077 for (n = rb_first(&proc->threads); n; n = rb_next(n)) {
5078 thread = rb_entry(n, struct binder_thread, rb_node);
5079 if (thread->transaction_stack)
5085 static int binder_ioctl_freeze(struct binder_freeze_info *info,
5086 struct binder_proc *target_proc)
5090 if (!info->enable) {
5091 binder_inner_proc_lock(target_proc);
5092 target_proc->sync_recv = false;
5093 target_proc->async_recv = false;
5094 target_proc->is_frozen = false;
5095 binder_inner_proc_unlock(target_proc);
5100 * Freezing the target. Prevent new transactions by
5101 * setting frozen state. If timeout specified, wait
5102 * for transactions to drain.
5104 binder_inner_proc_lock(target_proc);
5105 target_proc->sync_recv = false;
5106 target_proc->async_recv = false;
5107 target_proc->is_frozen = true;
5108 binder_inner_proc_unlock(target_proc);
5110 if (info->timeout_ms > 0)
5111 ret = wait_event_interruptible_timeout(
5112 target_proc->freeze_wait,
5113 (!target_proc->outstanding_txns),
5114 msecs_to_jiffies(info->timeout_ms));
5116 /* Check pending transactions that wait for reply */
5118 binder_inner_proc_lock(target_proc);
5119 if (binder_txns_pending_ilocked(target_proc))
5121 binder_inner_proc_unlock(target_proc);
5125 binder_inner_proc_lock(target_proc);
5126 target_proc->is_frozen = false;
5127 binder_inner_proc_unlock(target_proc);
5133 static int binder_ioctl_get_freezer_info(
5134 struct binder_frozen_status_info *info)
5136 struct binder_proc *target_proc;
5140 info->sync_recv = 0;
5141 info->async_recv = 0;
5143 mutex_lock(&binder_procs_lock);
5144 hlist_for_each_entry(target_proc, &binder_procs, proc_node) {
5145 if (target_proc->pid == info->pid) {
5147 binder_inner_proc_lock(target_proc);
5148 txns_pending = binder_txns_pending_ilocked(target_proc);
5149 info->sync_recv |= target_proc->sync_recv |
5150 (txns_pending << 1);
5151 info->async_recv |= target_proc->async_recv;
5152 binder_inner_proc_unlock(target_proc);
5155 mutex_unlock(&binder_procs_lock);
5163 static int binder_ioctl_get_extended_error(struct binder_thread *thread,
5166 struct binder_extended_error ee;
5168 binder_inner_proc_lock(thread->proc);
5170 binder_set_extended_error(&thread->ee, 0, BR_OK, 0);
5171 binder_inner_proc_unlock(thread->proc);
5173 if (copy_to_user(ubuf, &ee, sizeof(ee)))
5179 static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
5182 struct binder_proc *proc = filp->private_data;
5183 struct binder_thread *thread;
5184 unsigned int size = _IOC_SIZE(cmd);
5185 void __user *ubuf = (void __user *)arg;
5187 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
5188 proc->pid, current->pid, cmd, arg);*/
5190 binder_selftest_alloc(&proc->alloc);
5192 trace_binder_ioctl(cmd, arg);
5194 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
5198 thread = binder_get_thread(proc);
5199 if (thread == NULL) {
5205 case BINDER_WRITE_READ:
5206 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
5210 case BINDER_SET_MAX_THREADS: {
5213 if (copy_from_user(&max_threads, ubuf,
5214 sizeof(max_threads))) {
5218 binder_inner_proc_lock(proc);
5219 proc->max_threads = max_threads;
5220 binder_inner_proc_unlock(proc);
5223 case BINDER_SET_CONTEXT_MGR_EXT: {
5224 struct flat_binder_object fbo;
5226 if (copy_from_user(&fbo, ubuf, sizeof(fbo))) {
5230 ret = binder_ioctl_set_ctx_mgr(filp, &fbo);
5235 case BINDER_SET_CONTEXT_MGR:
5236 ret = binder_ioctl_set_ctx_mgr(filp, NULL);
5240 case BINDER_THREAD_EXIT:
5241 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
5242 proc->pid, thread->pid);
5243 binder_thread_release(proc, thread);
5246 case BINDER_VERSION: {
5247 struct binder_version __user *ver = ubuf;
5249 if (size != sizeof(struct binder_version)) {
5253 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
5254 &ver->protocol_version)) {
5260 case BINDER_GET_NODE_INFO_FOR_REF: {
5261 struct binder_node_info_for_ref info;
5263 if (copy_from_user(&info, ubuf, sizeof(info))) {
5268 ret = binder_ioctl_get_node_info_for_ref(proc, &info);
5272 if (copy_to_user(ubuf, &info, sizeof(info))) {
5279 case BINDER_GET_NODE_DEBUG_INFO: {
5280 struct binder_node_debug_info info;
5282 if (copy_from_user(&info, ubuf, sizeof(info))) {
5287 ret = binder_ioctl_get_node_debug_info(proc, &info);
5291 if (copy_to_user(ubuf, &info, sizeof(info))) {
5297 case BINDER_FREEZE: {
5298 struct binder_freeze_info info;
5299 struct binder_proc **target_procs = NULL, *target_proc;
5300 int target_procs_count = 0, i = 0;
5304 if (copy_from_user(&info, ubuf, sizeof(info))) {
5309 mutex_lock(&binder_procs_lock);
5310 hlist_for_each_entry(target_proc, &binder_procs, proc_node) {
5311 if (target_proc->pid == info.pid)
5312 target_procs_count++;
5315 if (target_procs_count == 0) {
5316 mutex_unlock(&binder_procs_lock);
5321 target_procs = kcalloc(target_procs_count,
5322 sizeof(struct binder_proc *),
5325 if (!target_procs) {
5326 mutex_unlock(&binder_procs_lock);
5331 hlist_for_each_entry(target_proc, &binder_procs, proc_node) {
5332 if (target_proc->pid != info.pid)
5335 binder_inner_proc_lock(target_proc);
5336 target_proc->tmp_ref++;
5337 binder_inner_proc_unlock(target_proc);
5339 target_procs[i++] = target_proc;
5341 mutex_unlock(&binder_procs_lock);
5343 for (i = 0; i < target_procs_count; i++) {
5345 ret = binder_ioctl_freeze(&info,
5348 binder_proc_dec_tmpref(target_procs[i]);
5351 kfree(target_procs);
5357 case BINDER_GET_FROZEN_INFO: {
5358 struct binder_frozen_status_info info;
5360 if (copy_from_user(&info, ubuf, sizeof(info))) {
5365 ret = binder_ioctl_get_freezer_info(&info);
5369 if (copy_to_user(ubuf, &info, sizeof(info))) {
5375 case BINDER_ENABLE_ONEWAY_SPAM_DETECTION: {
5378 if (copy_from_user(&enable, ubuf, sizeof(enable))) {
5382 binder_inner_proc_lock(proc);
5383 proc->oneway_spam_detection_enabled = (bool)enable;
5384 binder_inner_proc_unlock(proc);
5387 case BINDER_GET_EXTENDED_ERROR:
5388 ret = binder_ioctl_get_extended_error(thread, ubuf);
5399 thread->looper_need_return = false;
5400 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
5401 if (ret && ret != -EINTR)
5402 pr_info("%d:%d ioctl %x %lx returned %d\n", proc->pid, current->pid, cmd, arg, ret);
5404 trace_binder_ioctl_done(ret);
5408 static void binder_vma_open(struct vm_area_struct *vma)
5410 struct binder_proc *proc = vma->vm_private_data;
5412 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5413 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
5414 proc->pid, vma->vm_start, vma->vm_end,
5415 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
5416 (unsigned long)pgprot_val(vma->vm_page_prot));
5419 static void binder_vma_close(struct vm_area_struct *vma)
5421 struct binder_proc *proc = vma->vm_private_data;
5423 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5424 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
5425 proc->pid, vma->vm_start, vma->vm_end,
5426 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
5427 (unsigned long)pgprot_val(vma->vm_page_prot));
5428 binder_alloc_vma_close(&proc->alloc);
5431 static vm_fault_t binder_vm_fault(struct vm_fault *vmf)
5433 return VM_FAULT_SIGBUS;
5436 static const struct vm_operations_struct binder_vm_ops = {
5437 .open = binder_vma_open,
5438 .close = binder_vma_close,
5439 .fault = binder_vm_fault,
5442 static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
5444 struct binder_proc *proc = filp->private_data;
5446 if (proc->tsk != current->group_leader)
5449 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5450 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
5451 __func__, proc->pid, vma->vm_start, vma->vm_end,
5452 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
5453 (unsigned long)pgprot_val(vma->vm_page_prot));
5455 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
5456 pr_err("%s: %d %lx-%lx %s failed %d\n", __func__,
5457 proc->pid, vma->vm_start, vma->vm_end, "bad vm_flags", -EPERM);
5460 vma->vm_flags |= VM_DONTCOPY | VM_MIXEDMAP;
5461 vma->vm_flags &= ~VM_MAYWRITE;
5463 vma->vm_ops = &binder_vm_ops;
5464 vma->vm_private_data = proc;
5466 return binder_alloc_mmap_handler(&proc->alloc, vma);
5469 static int binder_open(struct inode *nodp, struct file *filp)
5471 struct binder_proc *proc, *itr;
5472 struct binder_device *binder_dev;
5473 struct binderfs_info *info;
5474 struct dentry *binder_binderfs_dir_entry_proc = NULL;
5475 bool existing_pid = false;
5477 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "%s: %d:%d\n", __func__,
5478 current->group_leader->pid, current->pid);
5480 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
5483 spin_lock_init(&proc->inner_lock);
5484 spin_lock_init(&proc->outer_lock);
5485 get_task_struct(current->group_leader);
5486 proc->tsk = current->group_leader;
5487 proc->cred = get_cred(filp->f_cred);
5488 INIT_LIST_HEAD(&proc->todo);
5489 init_waitqueue_head(&proc->freeze_wait);
5490 proc->default_priority = task_nice(current);
5491 /* binderfs stashes devices in i_private */
5492 if (is_binderfs_device(nodp)) {
5493 binder_dev = nodp->i_private;
5494 info = nodp->i_sb->s_fs_info;
5495 binder_binderfs_dir_entry_proc = info->proc_log_dir;
5497 binder_dev = container_of(filp->private_data,
5498 struct binder_device, miscdev);
5500 refcount_inc(&binder_dev->ref);
5501 proc->context = &binder_dev->context;
5502 binder_alloc_init(&proc->alloc);
5504 binder_stats_created(BINDER_STAT_PROC);
5505 proc->pid = current->group_leader->pid;
5506 INIT_LIST_HEAD(&proc->delivered_death);
5507 INIT_LIST_HEAD(&proc->waiting_threads);
5508 filp->private_data = proc;
5510 mutex_lock(&binder_procs_lock);
5511 hlist_for_each_entry(itr, &binder_procs, proc_node) {
5512 if (itr->pid == proc->pid) {
5513 existing_pid = true;
5517 hlist_add_head(&proc->proc_node, &binder_procs);
5518 mutex_unlock(&binder_procs_lock);
5520 if (binder_debugfs_dir_entry_proc && !existing_pid) {
5523 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
5525 * proc debug entries are shared between contexts.
5526 * Only create for the first PID to avoid debugfs log spamming
5527 * The printing code will anyway print all contexts for a given
5528 * PID so this is not a problem.
5530 proc->debugfs_entry = debugfs_create_file(strbuf, 0444,
5531 binder_debugfs_dir_entry_proc,
5532 (void *)(unsigned long)proc->pid,
5536 if (binder_binderfs_dir_entry_proc && !existing_pid) {
5538 struct dentry *binderfs_entry;
5540 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
5542 * Similar to debugfs, the process specific log file is shared
5543 * between contexts. Only create for the first PID.
5544 * This is ok since same as debugfs, the log file will contain
5545 * information on all contexts of a given PID.
5547 binderfs_entry = binderfs_create_file(binder_binderfs_dir_entry_proc,
5548 strbuf, &proc_fops, (void *)(unsigned long)proc->pid);
5549 if (!IS_ERR(binderfs_entry)) {
5550 proc->binderfs_entry = binderfs_entry;
5554 error = PTR_ERR(binderfs_entry);
5555 pr_warn("Unable to create file %s in binderfs (error %d)\n",
5563 static int binder_flush(struct file *filp, fl_owner_t id)
5565 struct binder_proc *proc = filp->private_data;
5567 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
5572 static void binder_deferred_flush(struct binder_proc *proc)
5577 binder_inner_proc_lock(proc);
5578 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
5579 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
5581 thread->looper_need_return = true;
5582 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
5583 wake_up_interruptible(&thread->wait);
5587 binder_inner_proc_unlock(proc);
5589 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5590 "binder_flush: %d woke %d threads\n", proc->pid,
5594 static int binder_release(struct inode *nodp, struct file *filp)
5596 struct binder_proc *proc = filp->private_data;
5598 debugfs_remove(proc->debugfs_entry);
5600 if (proc->binderfs_entry) {
5601 binderfs_remove_file(proc->binderfs_entry);
5602 proc->binderfs_entry = NULL;
5605 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
5610 static int binder_node_release(struct binder_node *node, int refs)
5612 struct binder_ref *ref;
5614 struct binder_proc *proc = node->proc;
5616 binder_release_work(proc, &node->async_todo);
5618 binder_node_lock(node);
5619 binder_inner_proc_lock(proc);
5620 binder_dequeue_work_ilocked(&node->work);
5622 * The caller must have taken a temporary ref on the node,
5624 BUG_ON(!node->tmp_refs);
5625 if (hlist_empty(&node->refs) && node->tmp_refs == 1) {
5626 binder_inner_proc_unlock(proc);
5627 binder_node_unlock(node);
5628 binder_free_node(node);
5634 node->local_strong_refs = 0;
5635 node->local_weak_refs = 0;
5636 binder_inner_proc_unlock(proc);
5638 spin_lock(&binder_dead_nodes_lock);
5639 hlist_add_head(&node->dead_node, &binder_dead_nodes);
5640 spin_unlock(&binder_dead_nodes_lock);
5642 hlist_for_each_entry(ref, &node->refs, node_entry) {
5645 * Need the node lock to synchronize
5646 * with new notification requests and the
5647 * inner lock to synchronize with queued
5648 * death notifications.
5650 binder_inner_proc_lock(ref->proc);
5652 binder_inner_proc_unlock(ref->proc);
5658 BUG_ON(!list_empty(&ref->death->work.entry));
5659 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
5660 binder_enqueue_work_ilocked(&ref->death->work,
5662 binder_wakeup_proc_ilocked(ref->proc);
5663 binder_inner_proc_unlock(ref->proc);
5666 binder_debug(BINDER_DEBUG_DEAD_BINDER,
5667 "node %d now dead, refs %d, death %d\n",
5668 node->debug_id, refs, death);
5669 binder_node_unlock(node);
5670 binder_put_node(node);
5675 static void binder_deferred_release(struct binder_proc *proc)
5677 struct binder_context *context = proc->context;
5679 int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
5681 mutex_lock(&binder_procs_lock);
5682 hlist_del(&proc->proc_node);
5683 mutex_unlock(&binder_procs_lock);
5685 mutex_lock(&context->context_mgr_node_lock);
5686 if (context->binder_context_mgr_node &&
5687 context->binder_context_mgr_node->proc == proc) {
5688 binder_debug(BINDER_DEBUG_DEAD_BINDER,
5689 "%s: %d context_mgr_node gone\n",
5690 __func__, proc->pid);
5691 context->binder_context_mgr_node = NULL;
5693 mutex_unlock(&context->context_mgr_node_lock);
5694 binder_inner_proc_lock(proc);
5696 * Make sure proc stays alive after we
5697 * remove all the threads
5701 proc->is_dead = true;
5702 proc->is_frozen = false;
5703 proc->sync_recv = false;
5704 proc->async_recv = false;
5706 active_transactions = 0;
5707 while ((n = rb_first(&proc->threads))) {
5708 struct binder_thread *thread;
5710 thread = rb_entry(n, struct binder_thread, rb_node);
5711 binder_inner_proc_unlock(proc);
5713 active_transactions += binder_thread_release(proc, thread);
5714 binder_inner_proc_lock(proc);
5719 while ((n = rb_first(&proc->nodes))) {
5720 struct binder_node *node;
5722 node = rb_entry(n, struct binder_node, rb_node);
5725 * take a temporary ref on the node before
5726 * calling binder_node_release() which will either
5727 * kfree() the node or call binder_put_node()
5729 binder_inc_node_tmpref_ilocked(node);
5730 rb_erase(&node->rb_node, &proc->nodes);
5731 binder_inner_proc_unlock(proc);
5732 incoming_refs = binder_node_release(node, incoming_refs);
5733 binder_inner_proc_lock(proc);
5735 binder_inner_proc_unlock(proc);
5738 binder_proc_lock(proc);
5739 while ((n = rb_first(&proc->refs_by_desc))) {
5740 struct binder_ref *ref;
5742 ref = rb_entry(n, struct binder_ref, rb_node_desc);
5744 binder_cleanup_ref_olocked(ref);
5745 binder_proc_unlock(proc);
5746 binder_free_ref(ref);
5747 binder_proc_lock(proc);
5749 binder_proc_unlock(proc);
5751 binder_release_work(proc, &proc->todo);
5752 binder_release_work(proc, &proc->delivered_death);
5754 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5755 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
5756 __func__, proc->pid, threads, nodes, incoming_refs,
5757 outgoing_refs, active_transactions);
5759 binder_proc_dec_tmpref(proc);
5762 static void binder_deferred_func(struct work_struct *work)
5764 struct binder_proc *proc;
5769 mutex_lock(&binder_deferred_lock);
5770 if (!hlist_empty(&binder_deferred_list)) {
5771 proc = hlist_entry(binder_deferred_list.first,
5772 struct binder_proc, deferred_work_node);
5773 hlist_del_init(&proc->deferred_work_node);
5774 defer = proc->deferred_work;
5775 proc->deferred_work = 0;
5780 mutex_unlock(&binder_deferred_lock);
5782 if (defer & BINDER_DEFERRED_FLUSH)
5783 binder_deferred_flush(proc);
5785 if (defer & BINDER_DEFERRED_RELEASE)
5786 binder_deferred_release(proc); /* frees proc */
5789 static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
5792 binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
5794 mutex_lock(&binder_deferred_lock);
5795 proc->deferred_work |= defer;
5796 if (hlist_unhashed(&proc->deferred_work_node)) {
5797 hlist_add_head(&proc->deferred_work_node,
5798 &binder_deferred_list);
5799 schedule_work(&binder_deferred_work);
5801 mutex_unlock(&binder_deferred_lock);
5804 static void print_binder_transaction_ilocked(struct seq_file *m,
5805 struct binder_proc *proc,
5807 struct binder_transaction *t)
5809 struct binder_proc *to_proc;
5810 struct binder_buffer *buffer = t->buffer;
5812 spin_lock(&t->lock);
5813 to_proc = t->to_proc;
5815 "%s %d: %pK from %d:%d to %d:%d code %x flags %x pri %ld r%d",
5816 prefix, t->debug_id, t,
5817 t->from ? t->from->proc->pid : 0,
5818 t->from ? t->from->pid : 0,
5819 to_proc ? to_proc->pid : 0,
5820 t->to_thread ? t->to_thread->pid : 0,
5821 t->code, t->flags, t->priority, t->need_reply);
5822 spin_unlock(&t->lock);
5824 if (proc != to_proc) {
5826 * Can only safely deref buffer if we are holding the
5827 * correct proc inner lock for this node
5833 if (buffer == NULL) {
5834 seq_puts(m, " buffer free\n");
5837 if (buffer->target_node)
5838 seq_printf(m, " node %d", buffer->target_node->debug_id);
5839 seq_printf(m, " size %zd:%zd data %pK\n",
5840 buffer->data_size, buffer->offsets_size,
5844 static void print_binder_work_ilocked(struct seq_file *m,
5845 struct binder_proc *proc,
5847 const char *transaction_prefix,
5848 struct binder_work *w)
5850 struct binder_node *node;
5851 struct binder_transaction *t;
5854 case BINDER_WORK_TRANSACTION:
5855 t = container_of(w, struct binder_transaction, work);
5856 print_binder_transaction_ilocked(
5857 m, proc, transaction_prefix, t);
5859 case BINDER_WORK_RETURN_ERROR: {
5860 struct binder_error *e = container_of(
5861 w, struct binder_error, work);
5863 seq_printf(m, "%stransaction error: %u\n",
5866 case BINDER_WORK_TRANSACTION_COMPLETE:
5867 seq_printf(m, "%stransaction complete\n", prefix);
5869 case BINDER_WORK_NODE:
5870 node = container_of(w, struct binder_node, work);
5871 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
5872 prefix, node->debug_id,
5873 (u64)node->ptr, (u64)node->cookie);
5875 case BINDER_WORK_DEAD_BINDER:
5876 seq_printf(m, "%shas dead binder\n", prefix);
5878 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
5879 seq_printf(m, "%shas cleared dead binder\n", prefix);
5881 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
5882 seq_printf(m, "%shas cleared death notification\n", prefix);
5885 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
5890 static void print_binder_thread_ilocked(struct seq_file *m,
5891 struct binder_thread *thread,
5894 struct binder_transaction *t;
5895 struct binder_work *w;
5896 size_t start_pos = m->count;
5899 seq_printf(m, " thread %d: l %02x need_return %d tr %d\n",
5900 thread->pid, thread->looper,
5901 thread->looper_need_return,
5902 atomic_read(&thread->tmp_ref));
5903 header_pos = m->count;
5904 t = thread->transaction_stack;
5906 if (t->from == thread) {
5907 print_binder_transaction_ilocked(m, thread->proc,
5908 " outgoing transaction", t);
5910 } else if (t->to_thread == thread) {
5911 print_binder_transaction_ilocked(m, thread->proc,
5912 " incoming transaction", t);
5915 print_binder_transaction_ilocked(m, thread->proc,
5916 " bad transaction", t);
5920 list_for_each_entry(w, &thread->todo, entry) {
5921 print_binder_work_ilocked(m, thread->proc, " ",
5922 " pending transaction", w);
5924 if (!print_always && m->count == header_pos)
5925 m->count = start_pos;
5928 static void print_binder_node_nilocked(struct seq_file *m,
5929 struct binder_node *node)
5931 struct binder_ref *ref;
5932 struct binder_work *w;
5936 hlist_for_each_entry(ref, &node->refs, node_entry)
5939 seq_printf(m, " node %d: u%016llx c%016llx hs %d hw %d ls %d lw %d is %d iw %d tr %d",
5940 node->debug_id, (u64)node->ptr, (u64)node->cookie,
5941 node->has_strong_ref, node->has_weak_ref,
5942 node->local_strong_refs, node->local_weak_refs,
5943 node->internal_strong_refs, count, node->tmp_refs);
5945 seq_puts(m, " proc");
5946 hlist_for_each_entry(ref, &node->refs, node_entry)
5947 seq_printf(m, " %d", ref->proc->pid);
5951 list_for_each_entry(w, &node->async_todo, entry)
5952 print_binder_work_ilocked(m, node->proc, " ",
5953 " pending async transaction", w);
5957 static void print_binder_ref_olocked(struct seq_file *m,
5958 struct binder_ref *ref)
5960 binder_node_lock(ref->node);
5961 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
5962 ref->data.debug_id, ref->data.desc,
5963 ref->node->proc ? "" : "dead ",
5964 ref->node->debug_id, ref->data.strong,
5965 ref->data.weak, ref->death);
5966 binder_node_unlock(ref->node);
5969 static void print_binder_proc(struct seq_file *m,
5970 struct binder_proc *proc, int print_all)
5972 struct binder_work *w;
5974 size_t start_pos = m->count;
5976 struct binder_node *last_node = NULL;
5978 seq_printf(m, "proc %d\n", proc->pid);
5979 seq_printf(m, "context %s\n", proc->context->name);
5980 header_pos = m->count;
5982 binder_inner_proc_lock(proc);
5983 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
5984 print_binder_thread_ilocked(m, rb_entry(n, struct binder_thread,
5985 rb_node), print_all);
5987 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
5988 struct binder_node *node = rb_entry(n, struct binder_node,
5990 if (!print_all && !node->has_async_transaction)
5994 * take a temporary reference on the node so it
5995 * survives and isn't removed from the tree
5996 * while we print it.
5998 binder_inc_node_tmpref_ilocked(node);
5999 /* Need to drop inner lock to take node lock */
6000 binder_inner_proc_unlock(proc);
6002 binder_put_node(last_node);
6003 binder_node_inner_lock(node);
6004 print_binder_node_nilocked(m, node);
6005 binder_node_inner_unlock(node);
6007 binder_inner_proc_lock(proc);
6009 binder_inner_proc_unlock(proc);
6011 binder_put_node(last_node);
6014 binder_proc_lock(proc);
6015 for (n = rb_first(&proc->refs_by_desc);
6018 print_binder_ref_olocked(m, rb_entry(n,
6021 binder_proc_unlock(proc);
6023 binder_alloc_print_allocated(m, &proc->alloc);
6024 binder_inner_proc_lock(proc);
6025 list_for_each_entry(w, &proc->todo, entry)
6026 print_binder_work_ilocked(m, proc, " ",
6027 " pending transaction", w);
6028 list_for_each_entry(w, &proc->delivered_death, entry) {
6029 seq_puts(m, " has delivered dead binder\n");
6032 binder_inner_proc_unlock(proc);
6033 if (!print_all && m->count == header_pos)
6034 m->count = start_pos;
6037 static const char * const binder_return_strings[] = {
6042 "BR_ACQUIRE_RESULT",
6044 "BR_TRANSACTION_COMPLETE",
6049 "BR_ATTEMPT_ACQUIRE",
6054 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
6057 "BR_ONEWAY_SPAM_SUSPECT",
6060 static const char * const binder_command_strings[] = {
6063 "BC_ACQUIRE_RESULT",
6071 "BC_ATTEMPT_ACQUIRE",
6072 "BC_REGISTER_LOOPER",
6075 "BC_REQUEST_DEATH_NOTIFICATION",
6076 "BC_CLEAR_DEATH_NOTIFICATION",
6077 "BC_DEAD_BINDER_DONE",
6078 "BC_TRANSACTION_SG",
6082 static const char * const binder_objstat_strings[] = {
6089 "transaction_complete"
6092 static void print_binder_stats(struct seq_file *m, const char *prefix,
6093 struct binder_stats *stats)
6097 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
6098 ARRAY_SIZE(binder_command_strings));
6099 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
6100 int temp = atomic_read(&stats->bc[i]);
6103 seq_printf(m, "%s%s: %d\n", prefix,
6104 binder_command_strings[i], temp);
6107 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
6108 ARRAY_SIZE(binder_return_strings));
6109 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
6110 int temp = atomic_read(&stats->br[i]);
6113 seq_printf(m, "%s%s: %d\n", prefix,
6114 binder_return_strings[i], temp);
6117 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
6118 ARRAY_SIZE(binder_objstat_strings));
6119 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
6120 ARRAY_SIZE(stats->obj_deleted));
6121 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
6122 int created = atomic_read(&stats->obj_created[i]);
6123 int deleted = atomic_read(&stats->obj_deleted[i]);
6125 if (created || deleted)
6126 seq_printf(m, "%s%s: active %d total %d\n",
6128 binder_objstat_strings[i],
6134 static void print_binder_proc_stats(struct seq_file *m,
6135 struct binder_proc *proc)
6137 struct binder_work *w;
6138 struct binder_thread *thread;
6140 int count, strong, weak, ready_threads;
6141 size_t free_async_space =
6142 binder_alloc_get_free_async_space(&proc->alloc);
6144 seq_printf(m, "proc %d\n", proc->pid);
6145 seq_printf(m, "context %s\n", proc->context->name);
6148 binder_inner_proc_lock(proc);
6149 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
6152 list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node)
6155 seq_printf(m, " threads: %d\n", count);
6156 seq_printf(m, " requested threads: %d+%d/%d\n"
6157 " ready threads %d\n"
6158 " free async space %zd\n", proc->requested_threads,
6159 proc->requested_threads_started, proc->max_threads,
6163 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
6165 binder_inner_proc_unlock(proc);
6166 seq_printf(m, " nodes: %d\n", count);
6170 binder_proc_lock(proc);
6171 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
6172 struct binder_ref *ref = rb_entry(n, struct binder_ref,
6175 strong += ref->data.strong;
6176 weak += ref->data.weak;
6178 binder_proc_unlock(proc);
6179 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
6181 count = binder_alloc_get_allocated_count(&proc->alloc);
6182 seq_printf(m, " buffers: %d\n", count);
6184 binder_alloc_print_pages(m, &proc->alloc);
6187 binder_inner_proc_lock(proc);
6188 list_for_each_entry(w, &proc->todo, entry) {
6189 if (w->type == BINDER_WORK_TRANSACTION)
6192 binder_inner_proc_unlock(proc);
6193 seq_printf(m, " pending transactions: %d\n", count);
6195 print_binder_stats(m, " ", &proc->stats);
6199 int binder_state_show(struct seq_file *m, void *unused)
6201 struct binder_proc *proc;
6202 struct binder_node *node;
6203 struct binder_node *last_node = NULL;
6205 seq_puts(m, "binder state:\n");
6207 spin_lock(&binder_dead_nodes_lock);
6208 if (!hlist_empty(&binder_dead_nodes))
6209 seq_puts(m, "dead nodes:\n");
6210 hlist_for_each_entry(node, &binder_dead_nodes, dead_node) {
6212 * take a temporary reference on the node so it
6213 * survives and isn't removed from the list
6214 * while we print it.
6217 spin_unlock(&binder_dead_nodes_lock);
6219 binder_put_node(last_node);
6220 binder_node_lock(node);
6221 print_binder_node_nilocked(m, node);
6222 binder_node_unlock(node);
6224 spin_lock(&binder_dead_nodes_lock);
6226 spin_unlock(&binder_dead_nodes_lock);
6228 binder_put_node(last_node);
6230 mutex_lock(&binder_procs_lock);
6231 hlist_for_each_entry(proc, &binder_procs, proc_node)
6232 print_binder_proc(m, proc, 1);
6233 mutex_unlock(&binder_procs_lock);
6238 int binder_stats_show(struct seq_file *m, void *unused)
6240 struct binder_proc *proc;
6242 seq_puts(m, "binder stats:\n");
6244 print_binder_stats(m, "", &binder_stats);
6246 mutex_lock(&binder_procs_lock);
6247 hlist_for_each_entry(proc, &binder_procs, proc_node)
6248 print_binder_proc_stats(m, proc);
6249 mutex_unlock(&binder_procs_lock);
6254 int binder_transactions_show(struct seq_file *m, void *unused)
6256 struct binder_proc *proc;
6258 seq_puts(m, "binder transactions:\n");
6259 mutex_lock(&binder_procs_lock);
6260 hlist_for_each_entry(proc, &binder_procs, proc_node)
6261 print_binder_proc(m, proc, 0);
6262 mutex_unlock(&binder_procs_lock);
6267 static int proc_show(struct seq_file *m, void *unused)
6269 struct binder_proc *itr;
6270 int pid = (unsigned long)m->private;
6272 mutex_lock(&binder_procs_lock);
6273 hlist_for_each_entry(itr, &binder_procs, proc_node) {
6274 if (itr->pid == pid) {
6275 seq_puts(m, "binder proc state:\n");
6276 print_binder_proc(m, itr, 1);
6279 mutex_unlock(&binder_procs_lock);
6284 static void print_binder_transaction_log_entry(struct seq_file *m,
6285 struct binder_transaction_log_entry *e)
6287 int debug_id = READ_ONCE(e->debug_id_done);
6289 * read barrier to guarantee debug_id_done read before
6290 * we print the log values
6294 "%d: %s from %d:%d to %d:%d context %s node %d handle %d size %d:%d ret %d/%d l=%d",
6295 e->debug_id, (e->call_type == 2) ? "reply" :
6296 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
6297 e->from_thread, e->to_proc, e->to_thread, e->context_name,
6298 e->to_node, e->target_handle, e->data_size, e->offsets_size,
6299 e->return_error, e->return_error_param,
6300 e->return_error_line);
6302 * read-barrier to guarantee read of debug_id_done after
6303 * done printing the fields of the entry
6306 seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ?
6307 "\n" : " (incomplete)\n");
6310 int binder_transaction_log_show(struct seq_file *m, void *unused)
6312 struct binder_transaction_log *log = m->private;
6313 unsigned int log_cur = atomic_read(&log->cur);
6318 count = log_cur + 1;
6319 cur = count < ARRAY_SIZE(log->entry) && !log->full ?
6320 0 : count % ARRAY_SIZE(log->entry);
6321 if (count > ARRAY_SIZE(log->entry) || log->full)
6322 count = ARRAY_SIZE(log->entry);
6323 for (i = 0; i < count; i++) {
6324 unsigned int index = cur++ % ARRAY_SIZE(log->entry);
6326 print_binder_transaction_log_entry(m, &log->entry[index]);
6331 const struct file_operations binder_fops = {
6332 .owner = THIS_MODULE,
6333 .poll = binder_poll,
6334 .unlocked_ioctl = binder_ioctl,
6335 .compat_ioctl = compat_ptr_ioctl,
6336 .mmap = binder_mmap,
6337 .open = binder_open,
6338 .flush = binder_flush,
6339 .release = binder_release,
6342 static int __init init_binder_device(const char *name)
6345 struct binder_device *binder_device;
6347 binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
6351 binder_device->miscdev.fops = &binder_fops;
6352 binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
6353 binder_device->miscdev.name = name;
6355 refcount_set(&binder_device->ref, 1);
6356 binder_device->context.binder_context_mgr_uid = INVALID_UID;
6357 binder_device->context.name = name;
6358 mutex_init(&binder_device->context.context_mgr_node_lock);
6360 ret = misc_register(&binder_device->miscdev);
6362 kfree(binder_device);
6366 hlist_add_head(&binder_device->hlist, &binder_devices);
6371 static int __init binder_init(void)
6374 char *device_name, *device_tmp;
6375 struct binder_device *device;
6376 struct hlist_node *tmp;
6377 char *device_names = NULL;
6379 ret = binder_alloc_shrinker_init();
6383 atomic_set(&binder_transaction_log.cur, ~0U);
6384 atomic_set(&binder_transaction_log_failed.cur, ~0U);
6386 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
6387 if (binder_debugfs_dir_entry_root)
6388 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
6389 binder_debugfs_dir_entry_root);
6391 if (binder_debugfs_dir_entry_root) {
6392 debugfs_create_file("state",
6394 binder_debugfs_dir_entry_root,
6396 &binder_state_fops);
6397 debugfs_create_file("stats",
6399 binder_debugfs_dir_entry_root,
6401 &binder_stats_fops);
6402 debugfs_create_file("transactions",
6404 binder_debugfs_dir_entry_root,
6406 &binder_transactions_fops);
6407 debugfs_create_file("transaction_log",
6409 binder_debugfs_dir_entry_root,
6410 &binder_transaction_log,
6411 &binder_transaction_log_fops);
6412 debugfs_create_file("failed_transaction_log",
6414 binder_debugfs_dir_entry_root,
6415 &binder_transaction_log_failed,
6416 &binder_transaction_log_fops);
6419 if (!IS_ENABLED(CONFIG_ANDROID_BINDERFS) &&
6420 strcmp(binder_devices_param, "") != 0) {
6422 * Copy the module_parameter string, because we don't want to
6423 * tokenize it in-place.
6425 device_names = kstrdup(binder_devices_param, GFP_KERNEL);
6426 if (!device_names) {
6428 goto err_alloc_device_names_failed;
6431 device_tmp = device_names;
6432 while ((device_name = strsep(&device_tmp, ","))) {
6433 ret = init_binder_device(device_name);
6435 goto err_init_binder_device_failed;
6439 ret = init_binderfs();
6441 goto err_init_binder_device_failed;
6445 err_init_binder_device_failed:
6446 hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
6447 misc_deregister(&device->miscdev);
6448 hlist_del(&device->hlist);
6452 kfree(device_names);
6454 err_alloc_device_names_failed:
6455 debugfs_remove_recursive(binder_debugfs_dir_entry_root);
6460 device_initcall(binder_init);
6462 #define CREATE_TRACE_POINTS
6463 #include "binder_trace.h"
6465 MODULE_LICENSE("GPL v2");