Linux 4.19.133
[linux/fpc-iii.git] / drivers / android / binder.c
blobcf4367135a00bc65798847b2854b637f0045f5fd
1 /* binder.c
3 * Android IPC Subsystem
5 * Copyright (C) 2007-2008 Google, Inc.
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
19 * Locking overview
21 * There are 3 main spinlocks which must be acquired in the
22 * order shown:
24 * 1) proc->outer_lock : protects binder_ref
25 * binder_proc_lock() and binder_proc_unlock() are
26 * used to acq/rel.
27 * 2) node->lock : protects most fields of binder_node.
28 * binder_node_lock() and binder_node_unlock() are
29 * used to acq/rel
30 * 3) proc->inner_lock : protects the thread and node lists
31 * (proc->threads, proc->waiting_threads, proc->nodes)
32 * and all todo lists associated with the binder_proc
33 * (proc->todo, thread->todo, proc->delivered_death and
34 * node->async_todo), as well as thread->transaction_stack
35 * binder_inner_proc_lock() and binder_inner_proc_unlock()
36 * are used to acq/rel
38 * Any lock under procA must never be nested under any lock at the same
39 * level or below on procB.
41 * Functions that require a lock held on entry indicate which lock
42 * in the suffix of the function name:
44 * foo_olocked() : requires node->outer_lock
45 * foo_nlocked() : requires node->lock
46 * foo_ilocked() : requires proc->inner_lock
47 * foo_oilocked(): requires proc->outer_lock and proc->inner_lock
48 * foo_nilocked(): requires node->lock and proc->inner_lock
49 * ...
52 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
54 #include <linux/fdtable.h>
55 #include <linux/file.h>
56 #include <linux/freezer.h>
57 #include <linux/fs.h>
58 #include <linux/list.h>
59 #include <linux/miscdevice.h>
60 #include <linux/module.h>
61 #include <linux/mutex.h>
62 #include <linux/nsproxy.h>
63 #include <linux/poll.h>
64 #include <linux/debugfs.h>
65 #include <linux/rbtree.h>
66 #include <linux/sched/signal.h>
67 #include <linux/sched/mm.h>
68 #include <linux/seq_file.h>
69 #include <linux/uaccess.h>
70 #include <linux/pid_namespace.h>
71 #include <linux/security.h>
72 #include <linux/spinlock.h>
73 #include <linux/ratelimit.h>
75 #include <uapi/linux/android/binder.h>
77 #include <asm/cacheflush.h>
79 #include "binder_alloc.h"
80 #include "binder_trace.h"
82 static HLIST_HEAD(binder_deferred_list);
83 static DEFINE_MUTEX(binder_deferred_lock);
85 static HLIST_HEAD(binder_devices);
86 static HLIST_HEAD(binder_procs);
87 static DEFINE_MUTEX(binder_procs_lock);
89 static HLIST_HEAD(binder_dead_nodes);
90 static DEFINE_SPINLOCK(binder_dead_nodes_lock);
92 static struct dentry *binder_debugfs_dir_entry_root;
93 static struct dentry *binder_debugfs_dir_entry_proc;
94 static atomic_t binder_last_id;
96 #define BINDER_DEBUG_ENTRY(name) \
97 static int binder_##name##_open(struct inode *inode, struct file *file) \
98 { \
99 return single_open(file, binder_##name##_show, inode->i_private); \
102 static const struct file_operations binder_##name##_fops = { \
103 .owner = THIS_MODULE, \
104 .open = binder_##name##_open, \
105 .read = seq_read, \
106 .llseek = seq_lseek, \
107 .release = single_release, \
110 static int binder_proc_show(struct seq_file *m, void *unused);
111 BINDER_DEBUG_ENTRY(proc);
113 /* This is only defined in include/asm-arm/sizes.h */
114 #ifndef SZ_1K
115 #define SZ_1K 0x400
116 #endif
118 #ifndef SZ_4M
119 #define SZ_4M 0x400000
120 #endif
122 #define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
124 enum {
125 BINDER_DEBUG_USER_ERROR = 1U << 0,
126 BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1,
127 BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2,
128 BINDER_DEBUG_OPEN_CLOSE = 1U << 3,
129 BINDER_DEBUG_DEAD_BINDER = 1U << 4,
130 BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5,
131 BINDER_DEBUG_READ_WRITE = 1U << 6,
132 BINDER_DEBUG_USER_REFS = 1U << 7,
133 BINDER_DEBUG_THREADS = 1U << 8,
134 BINDER_DEBUG_TRANSACTION = 1U << 9,
135 BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10,
136 BINDER_DEBUG_FREE_BUFFER = 1U << 11,
137 BINDER_DEBUG_INTERNAL_REFS = 1U << 12,
138 BINDER_DEBUG_PRIORITY_CAP = 1U << 13,
139 BINDER_DEBUG_SPINLOCKS = 1U << 14,
141 static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR |
142 BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION;
143 module_param_named(debug_mask, binder_debug_mask, uint, 0644);
145 static char *binder_devices_param = CONFIG_ANDROID_BINDER_DEVICES;
146 module_param_named(devices, binder_devices_param, charp, 0444);
148 static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
149 static int binder_stop_on_user_error;
151 static int binder_set_stop_on_user_error(const char *val,
152 const struct kernel_param *kp)
154 int ret;
156 ret = param_set_int(val, kp);
157 if (binder_stop_on_user_error < 2)
158 wake_up(&binder_user_error_wait);
159 return ret;
161 module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
162 param_get_int, &binder_stop_on_user_error, 0644);
164 #define binder_debug(mask, x...) \
165 do { \
166 if (binder_debug_mask & mask) \
167 pr_info_ratelimited(x); \
168 } while (0)
170 #define binder_user_error(x...) \
171 do { \
172 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
173 pr_info_ratelimited(x); \
174 if (binder_stop_on_user_error) \
175 binder_stop_on_user_error = 2; \
176 } while (0)
178 #define to_flat_binder_object(hdr) \
179 container_of(hdr, struct flat_binder_object, hdr)
181 #define to_binder_fd_object(hdr) container_of(hdr, struct binder_fd_object, hdr)
183 #define to_binder_buffer_object(hdr) \
184 container_of(hdr, struct binder_buffer_object, hdr)
186 #define to_binder_fd_array_object(hdr) \
187 container_of(hdr, struct binder_fd_array_object, hdr)
189 enum binder_stat_types {
190 BINDER_STAT_PROC,
191 BINDER_STAT_THREAD,
192 BINDER_STAT_NODE,
193 BINDER_STAT_REF,
194 BINDER_STAT_DEATH,
195 BINDER_STAT_TRANSACTION,
196 BINDER_STAT_TRANSACTION_COMPLETE,
197 BINDER_STAT_COUNT
200 struct binder_stats {
201 atomic_t br[_IOC_NR(BR_FAILED_REPLY) + 1];
202 atomic_t bc[_IOC_NR(BC_REPLY_SG) + 1];
203 atomic_t obj_created[BINDER_STAT_COUNT];
204 atomic_t obj_deleted[BINDER_STAT_COUNT];
207 static struct binder_stats binder_stats;
209 static inline void binder_stats_deleted(enum binder_stat_types type)
211 atomic_inc(&binder_stats.obj_deleted[type]);
214 static inline void binder_stats_created(enum binder_stat_types type)
216 atomic_inc(&binder_stats.obj_created[type]);
219 struct binder_transaction_log_entry {
220 int debug_id;
221 int debug_id_done;
222 int call_type;
223 int from_proc;
224 int from_thread;
225 int target_handle;
226 int to_proc;
227 int to_thread;
228 int to_node;
229 int data_size;
230 int offsets_size;
231 int return_error_line;
232 uint32_t return_error;
233 uint32_t return_error_param;
234 const char *context_name;
236 struct binder_transaction_log {
237 atomic_t cur;
238 bool full;
239 struct binder_transaction_log_entry entry[32];
241 static struct binder_transaction_log binder_transaction_log;
242 static struct binder_transaction_log binder_transaction_log_failed;
244 static struct binder_transaction_log_entry *binder_transaction_log_add(
245 struct binder_transaction_log *log)
247 struct binder_transaction_log_entry *e;
248 unsigned int cur = atomic_inc_return(&log->cur);
250 if (cur >= ARRAY_SIZE(log->entry))
251 log->full = true;
252 e = &log->entry[cur % ARRAY_SIZE(log->entry)];
253 WRITE_ONCE(e->debug_id_done, 0);
255 * write-barrier to synchronize access to e->debug_id_done.
256 * We make sure the initialized 0 value is seen before
257 * memset() other fields are zeroed by memset.
259 smp_wmb();
260 memset(e, 0, sizeof(*e));
261 return e;
264 struct binder_context {
265 struct binder_node *binder_context_mgr_node;
266 struct mutex context_mgr_node_lock;
268 kuid_t binder_context_mgr_uid;
269 const char *name;
272 struct binder_device {
273 struct hlist_node hlist;
274 struct miscdevice miscdev;
275 struct binder_context context;
279 * struct binder_work - work enqueued on a worklist
280 * @entry: node enqueued on list
281 * @type: type of work to be performed
283 * There are separate work lists for proc, thread, and node (async).
285 struct binder_work {
286 struct list_head entry;
288 enum {
289 BINDER_WORK_TRANSACTION = 1,
290 BINDER_WORK_TRANSACTION_COMPLETE,
291 BINDER_WORK_RETURN_ERROR,
292 BINDER_WORK_NODE,
293 BINDER_WORK_DEAD_BINDER,
294 BINDER_WORK_DEAD_BINDER_AND_CLEAR,
295 BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
296 } type;
299 struct binder_error {
300 struct binder_work work;
301 uint32_t cmd;
305 * struct binder_node - binder node bookkeeping
306 * @debug_id: unique ID for debugging
307 * (invariant after initialized)
308 * @lock: lock for node fields
309 * @work: worklist element for node work
310 * (protected by @proc->inner_lock)
311 * @rb_node: element for proc->nodes tree
312 * (protected by @proc->inner_lock)
313 * @dead_node: element for binder_dead_nodes list
314 * (protected by binder_dead_nodes_lock)
315 * @proc: binder_proc that owns this node
316 * (invariant after initialized)
317 * @refs: list of references on this node
318 * (protected by @lock)
319 * @internal_strong_refs: used to take strong references when
320 * initiating a transaction
321 * (protected by @proc->inner_lock if @proc
322 * and by @lock)
323 * @local_weak_refs: weak user refs from local process
324 * (protected by @proc->inner_lock if @proc
325 * and by @lock)
326 * @local_strong_refs: strong user refs from local process
327 * (protected by @proc->inner_lock if @proc
328 * and by @lock)
329 * @tmp_refs: temporary kernel refs
330 * (protected by @proc->inner_lock while @proc
331 * is valid, and by binder_dead_nodes_lock
332 * if @proc is NULL. During inc/dec and node release
333 * it is also protected by @lock to provide safety
334 * as the node dies and @proc becomes NULL)
335 * @ptr: userspace pointer for node
336 * (invariant, no lock needed)
337 * @cookie: userspace cookie for node
338 * (invariant, no lock needed)
339 * @has_strong_ref: userspace notified of strong ref
340 * (protected by @proc->inner_lock if @proc
341 * and by @lock)
342 * @pending_strong_ref: userspace has acked notification of strong ref
343 * (protected by @proc->inner_lock if @proc
344 * and by @lock)
345 * @has_weak_ref: userspace notified of weak ref
346 * (protected by @proc->inner_lock if @proc
347 * and by @lock)
348 * @pending_weak_ref: userspace has acked notification of weak ref
349 * (protected by @proc->inner_lock if @proc
350 * and by @lock)
351 * @has_async_transaction: async transaction to node in progress
352 * (protected by @lock)
353 * @accept_fds: file descriptor operations supported for node
354 * (invariant after initialized)
355 * @min_priority: minimum scheduling priority
356 * (invariant after initialized)
357 * @async_todo: list of async work items
358 * (protected by @proc->inner_lock)
360 * Bookkeeping structure for binder nodes.
362 struct binder_node {
363 int debug_id;
364 spinlock_t lock;
365 struct binder_work work;
366 union {
367 struct rb_node rb_node;
368 struct hlist_node dead_node;
370 struct binder_proc *proc;
371 struct hlist_head refs;
372 int internal_strong_refs;
373 int local_weak_refs;
374 int local_strong_refs;
375 int tmp_refs;
376 binder_uintptr_t ptr;
377 binder_uintptr_t cookie;
378 struct {
380 * bitfield elements protected by
381 * proc inner_lock
383 u8 has_strong_ref:1;
384 u8 pending_strong_ref:1;
385 u8 has_weak_ref:1;
386 u8 pending_weak_ref:1;
388 struct {
390 * invariant after initialization
392 u8 accept_fds:1;
393 u8 min_priority;
395 bool has_async_transaction;
396 struct list_head async_todo;
399 struct binder_ref_death {
401 * @work: worklist element for death notifications
402 * (protected by inner_lock of the proc that
403 * this ref belongs to)
405 struct binder_work work;
406 binder_uintptr_t cookie;
410 * struct binder_ref_data - binder_ref counts and id
411 * @debug_id: unique ID for the ref
412 * @desc: unique userspace handle for ref
413 * @strong: strong ref count (debugging only if not locked)
414 * @weak: weak ref count (debugging only if not locked)
416 * Structure to hold ref count and ref id information. Since
417 * the actual ref can only be accessed with a lock, this structure
418 * is used to return information about the ref to callers of
419 * ref inc/dec functions.
421 struct binder_ref_data {
422 int debug_id;
423 uint32_t desc;
424 int strong;
425 int weak;
429 * struct binder_ref - struct to track references on nodes
430 * @data: binder_ref_data containing id, handle, and current refcounts
431 * @rb_node_desc: node for lookup by @data.desc in proc's rb_tree
432 * @rb_node_node: node for lookup by @node in proc's rb_tree
433 * @node_entry: list entry for node->refs list in target node
434 * (protected by @node->lock)
435 * @proc: binder_proc containing ref
436 * @node: binder_node of target node. When cleaning up a
437 * ref for deletion in binder_cleanup_ref, a non-NULL
438 * @node indicates the node must be freed
439 * @death: pointer to death notification (ref_death) if requested
440 * (protected by @node->lock)
442 * Structure to track references from procA to target node (on procB). This
443 * structure is unsafe to access without holding @proc->outer_lock.
445 struct binder_ref {
446 /* Lookups needed: */
447 /* node + proc => ref (transaction) */
448 /* desc + proc => ref (transaction, inc/dec ref) */
449 /* node => refs + procs (proc exit) */
450 struct binder_ref_data data;
451 struct rb_node rb_node_desc;
452 struct rb_node rb_node_node;
453 struct hlist_node node_entry;
454 struct binder_proc *proc;
455 struct binder_node *node;
456 struct binder_ref_death *death;
459 enum binder_deferred_state {
460 BINDER_DEFERRED_PUT_FILES = 0x01,
461 BINDER_DEFERRED_FLUSH = 0x02,
462 BINDER_DEFERRED_RELEASE = 0x04,
466 * struct binder_proc - binder process bookkeeping
467 * @proc_node: element for binder_procs list
468 * @threads: rbtree of binder_threads in this proc
469 * (protected by @inner_lock)
470 * @nodes: rbtree of binder nodes associated with
471 * this proc ordered by node->ptr
472 * (protected by @inner_lock)
473 * @refs_by_desc: rbtree of refs ordered by ref->desc
474 * (protected by @outer_lock)
475 * @refs_by_node: rbtree of refs ordered by ref->node
476 * (protected by @outer_lock)
477 * @waiting_threads: threads currently waiting for proc work
478 * (protected by @inner_lock)
479 * @pid PID of group_leader of process
480 * (invariant after initialized)
481 * @tsk task_struct for group_leader of process
482 * (invariant after initialized)
483 * @files files_struct for process
484 * (protected by @files_lock)
485 * @files_lock mutex to protect @files
486 * @deferred_work_node: element for binder_deferred_list
487 * (protected by binder_deferred_lock)
488 * @deferred_work: bitmap of deferred work to perform
489 * (protected by binder_deferred_lock)
490 * @is_dead: process is dead and awaiting free
491 * when outstanding transactions are cleaned up
492 * (protected by @inner_lock)
493 * @todo: list of work for this process
494 * (protected by @inner_lock)
495 * @stats: per-process binder statistics
496 * (atomics, no lock needed)
497 * @delivered_death: list of delivered death notification
498 * (protected by @inner_lock)
499 * @max_threads: cap on number of binder threads
500 * (protected by @inner_lock)
501 * @requested_threads: number of binder threads requested but not
502 * yet started. In current implementation, can
503 * only be 0 or 1.
504 * (protected by @inner_lock)
505 * @requested_threads_started: number binder threads started
506 * (protected by @inner_lock)
507 * @tmp_ref: temporary reference to indicate proc is in use
508 * (protected by @inner_lock)
509 * @default_priority: default scheduler priority
510 * (invariant after initialized)
511 * @debugfs_entry: debugfs node
512 * @alloc: binder allocator bookkeeping
513 * @context: binder_context for this proc
514 * (invariant after initialized)
515 * @inner_lock: can nest under outer_lock and/or node lock
516 * @outer_lock: no nesting under innor or node lock
517 * Lock order: 1) outer, 2) node, 3) inner
519 * Bookkeeping structure for binder processes
521 struct binder_proc {
522 struct hlist_node proc_node;
523 struct rb_root threads;
524 struct rb_root nodes;
525 struct rb_root refs_by_desc;
526 struct rb_root refs_by_node;
527 struct list_head waiting_threads;
528 int pid;
529 struct task_struct *tsk;
530 struct files_struct *files;
531 struct mutex files_lock;
532 struct hlist_node deferred_work_node;
533 int deferred_work;
534 bool is_dead;
536 struct list_head todo;
537 struct binder_stats stats;
538 struct list_head delivered_death;
539 int max_threads;
540 int requested_threads;
541 int requested_threads_started;
542 int tmp_ref;
543 long default_priority;
544 struct dentry *debugfs_entry;
545 struct binder_alloc alloc;
546 struct binder_context *context;
547 spinlock_t inner_lock;
548 spinlock_t outer_lock;
551 enum {
552 BINDER_LOOPER_STATE_REGISTERED = 0x01,
553 BINDER_LOOPER_STATE_ENTERED = 0x02,
554 BINDER_LOOPER_STATE_EXITED = 0x04,
555 BINDER_LOOPER_STATE_INVALID = 0x08,
556 BINDER_LOOPER_STATE_WAITING = 0x10,
557 BINDER_LOOPER_STATE_POLL = 0x20,
561 * struct binder_thread - binder thread bookkeeping
562 * @proc: binder process for this thread
563 * (invariant after initialization)
564 * @rb_node: element for proc->threads rbtree
565 * (protected by @proc->inner_lock)
566 * @waiting_thread_node: element for @proc->waiting_threads list
567 * (protected by @proc->inner_lock)
568 * @pid: PID for this thread
569 * (invariant after initialization)
570 * @looper: bitmap of looping state
571 * (only accessed by this thread)
572 * @looper_needs_return: looping thread needs to exit driver
573 * (no lock needed)
574 * @transaction_stack: stack of in-progress transactions for this thread
575 * (protected by @proc->inner_lock)
576 * @todo: list of work to do for this thread
577 * (protected by @proc->inner_lock)
578 * @process_todo: whether work in @todo should be processed
579 * (protected by @proc->inner_lock)
580 * @return_error: transaction errors reported by this thread
581 * (only accessed by this thread)
582 * @reply_error: transaction errors reported by target thread
583 * (protected by @proc->inner_lock)
584 * @wait: wait queue for thread work
585 * @stats: per-thread statistics
586 * (atomics, no lock needed)
587 * @tmp_ref: temporary reference to indicate thread is in use
588 * (atomic since @proc->inner_lock cannot
589 * always be acquired)
590 * @is_dead: thread is dead and awaiting free
591 * when outstanding transactions are cleaned up
592 * (protected by @proc->inner_lock)
594 * Bookkeeping structure for binder threads.
596 struct binder_thread {
597 struct binder_proc *proc;
598 struct rb_node rb_node;
599 struct list_head waiting_thread_node;
600 int pid;
601 int looper; /* only modified by this thread */
602 bool looper_need_return; /* can be written by other thread */
603 struct binder_transaction *transaction_stack;
604 struct list_head todo;
605 bool process_todo;
606 struct binder_error return_error;
607 struct binder_error reply_error;
608 wait_queue_head_t wait;
609 struct binder_stats stats;
610 atomic_t tmp_ref;
611 bool is_dead;
614 struct binder_transaction {
615 int debug_id;
616 struct binder_work work;
617 struct binder_thread *from;
618 struct binder_transaction *from_parent;
619 struct binder_proc *to_proc;
620 struct binder_thread *to_thread;
621 struct binder_transaction *to_parent;
622 unsigned need_reply:1;
623 /* unsigned is_dead:1; */ /* not used at the moment */
625 struct binder_buffer *buffer;
626 unsigned int code;
627 unsigned int flags;
628 long priority;
629 long saved_priority;
630 kuid_t sender_euid;
632 * @lock: protects @from, @to_proc, and @to_thread
634 * @from, @to_proc, and @to_thread can be set to NULL
635 * during thread teardown
637 spinlock_t lock;
641 * binder_proc_lock() - Acquire outer lock for given binder_proc
642 * @proc: struct binder_proc to acquire
644 * Acquires proc->outer_lock. Used to protect binder_ref
645 * structures associated with the given proc.
647 #define binder_proc_lock(proc) _binder_proc_lock(proc, __LINE__)
648 static void
649 _binder_proc_lock(struct binder_proc *proc, int line)
651 binder_debug(BINDER_DEBUG_SPINLOCKS,
652 "%s: line=%d\n", __func__, line);
653 spin_lock(&proc->outer_lock);
657 * binder_proc_unlock() - Release spinlock for given binder_proc
658 * @proc: struct binder_proc to acquire
660 * Release lock acquired via binder_proc_lock()
662 #define binder_proc_unlock(_proc) _binder_proc_unlock(_proc, __LINE__)
663 static void
664 _binder_proc_unlock(struct binder_proc *proc, int line)
666 binder_debug(BINDER_DEBUG_SPINLOCKS,
667 "%s: line=%d\n", __func__, line);
668 spin_unlock(&proc->outer_lock);
672 * binder_inner_proc_lock() - Acquire inner lock for given binder_proc
673 * @proc: struct binder_proc to acquire
675 * Acquires proc->inner_lock. Used to protect todo lists
677 #define binder_inner_proc_lock(proc) _binder_inner_proc_lock(proc, __LINE__)
678 static void
679 _binder_inner_proc_lock(struct binder_proc *proc, int line)
681 binder_debug(BINDER_DEBUG_SPINLOCKS,
682 "%s: line=%d\n", __func__, line);
683 spin_lock(&proc->inner_lock);
687 * binder_inner_proc_unlock() - Release inner lock for given binder_proc
688 * @proc: struct binder_proc to acquire
690 * Release lock acquired via binder_inner_proc_lock()
692 #define binder_inner_proc_unlock(proc) _binder_inner_proc_unlock(proc, __LINE__)
693 static void
694 _binder_inner_proc_unlock(struct binder_proc *proc, int line)
696 binder_debug(BINDER_DEBUG_SPINLOCKS,
697 "%s: line=%d\n", __func__, line);
698 spin_unlock(&proc->inner_lock);
702 * binder_node_lock() - Acquire spinlock for given binder_node
703 * @node: struct binder_node to acquire
705 * Acquires node->lock. Used to protect binder_node fields
707 #define binder_node_lock(node) _binder_node_lock(node, __LINE__)
708 static void
709 _binder_node_lock(struct binder_node *node, int line)
711 binder_debug(BINDER_DEBUG_SPINLOCKS,
712 "%s: line=%d\n", __func__, line);
713 spin_lock(&node->lock);
717 * binder_node_unlock() - Release spinlock for given binder_proc
718 * @node: struct binder_node to acquire
720 * Release lock acquired via binder_node_lock()
722 #define binder_node_unlock(node) _binder_node_unlock(node, __LINE__)
723 static void
724 _binder_node_unlock(struct binder_node *node, int line)
726 binder_debug(BINDER_DEBUG_SPINLOCKS,
727 "%s: line=%d\n", __func__, line);
728 spin_unlock(&node->lock);
732 * binder_node_inner_lock() - Acquire node and inner locks
733 * @node: struct binder_node to acquire
735 * Acquires node->lock. If node->proc also acquires
736 * proc->inner_lock. Used to protect binder_node fields
738 #define binder_node_inner_lock(node) _binder_node_inner_lock(node, __LINE__)
739 static void
740 _binder_node_inner_lock(struct binder_node *node, int line)
742 binder_debug(BINDER_DEBUG_SPINLOCKS,
743 "%s: line=%d\n", __func__, line);
744 spin_lock(&node->lock);
745 if (node->proc)
746 binder_inner_proc_lock(node->proc);
750 * binder_node_unlock() - Release node and inner locks
751 * @node: struct binder_node to acquire
753 * Release lock acquired via binder_node_lock()
755 #define binder_node_inner_unlock(node) _binder_node_inner_unlock(node, __LINE__)
756 static void
757 _binder_node_inner_unlock(struct binder_node *node, int line)
759 struct binder_proc *proc = node->proc;
761 binder_debug(BINDER_DEBUG_SPINLOCKS,
762 "%s: line=%d\n", __func__, line);
763 if (proc)
764 binder_inner_proc_unlock(proc);
765 spin_unlock(&node->lock);
768 static bool binder_worklist_empty_ilocked(struct list_head *list)
770 return list_empty(list);
774 * binder_worklist_empty() - Check if no items on the work list
775 * @proc: binder_proc associated with list
776 * @list: list to check
778 * Return: true if there are no items on list, else false
780 static bool binder_worklist_empty(struct binder_proc *proc,
781 struct list_head *list)
783 bool ret;
785 binder_inner_proc_lock(proc);
786 ret = binder_worklist_empty_ilocked(list);
787 binder_inner_proc_unlock(proc);
788 return ret;
792 * binder_enqueue_work_ilocked() - Add an item to the work list
793 * @work: struct binder_work to add to list
794 * @target_list: list to add work to
796 * Adds the work to the specified list. Asserts that work
797 * is not already on a list.
799 * Requires the proc->inner_lock to be held.
801 static void
802 binder_enqueue_work_ilocked(struct binder_work *work,
803 struct list_head *target_list)
805 BUG_ON(target_list == NULL);
806 BUG_ON(work->entry.next && !list_empty(&work->entry));
807 list_add_tail(&work->entry, target_list);
811 * binder_enqueue_deferred_thread_work_ilocked() - Add deferred thread work
812 * @thread: thread to queue work to
813 * @work: struct binder_work to add to list
815 * Adds the work to the todo list of the thread. Doesn't set the process_todo
816 * flag, which means that (if it wasn't already set) the thread will go to
817 * sleep without handling this work when it calls read.
819 * Requires the proc->inner_lock to be held.
821 static void
822 binder_enqueue_deferred_thread_work_ilocked(struct binder_thread *thread,
823 struct binder_work *work)
825 WARN_ON(!list_empty(&thread->waiting_thread_node));
826 binder_enqueue_work_ilocked(work, &thread->todo);
830 * binder_enqueue_thread_work_ilocked() - Add an item to the thread work list
831 * @thread: thread to queue work to
832 * @work: struct binder_work to add to list
834 * Adds the work to the todo list of the thread, and enables processing
835 * of the todo queue.
837 * Requires the proc->inner_lock to be held.
839 static void
840 binder_enqueue_thread_work_ilocked(struct binder_thread *thread,
841 struct binder_work *work)
843 WARN_ON(!list_empty(&thread->waiting_thread_node));
844 binder_enqueue_work_ilocked(work, &thread->todo);
845 thread->process_todo = true;
849 * binder_enqueue_thread_work() - Add an item to the thread work list
850 * @thread: thread to queue work to
851 * @work: struct binder_work to add to list
853 * Adds the work to the todo list of the thread, and enables processing
854 * of the todo queue.
856 static void
857 binder_enqueue_thread_work(struct binder_thread *thread,
858 struct binder_work *work)
860 binder_inner_proc_lock(thread->proc);
861 binder_enqueue_thread_work_ilocked(thread, work);
862 binder_inner_proc_unlock(thread->proc);
865 static void
866 binder_dequeue_work_ilocked(struct binder_work *work)
868 list_del_init(&work->entry);
872 * binder_dequeue_work() - Removes an item from the work list
873 * @proc: binder_proc associated with list
874 * @work: struct binder_work to remove from list
876 * Removes the specified work item from whatever list it is on.
877 * Can safely be called if work is not on any list.
879 static void
880 binder_dequeue_work(struct binder_proc *proc, struct binder_work *work)
882 binder_inner_proc_lock(proc);
883 binder_dequeue_work_ilocked(work);
884 binder_inner_proc_unlock(proc);
887 static struct binder_work *binder_dequeue_work_head_ilocked(
888 struct list_head *list)
890 struct binder_work *w;
892 w = list_first_entry_or_null(list, struct binder_work, entry);
893 if (w)
894 list_del_init(&w->entry);
895 return w;
899 * binder_dequeue_work_head() - Dequeues the item at head of list
900 * @proc: binder_proc associated with list
901 * @list: list to dequeue head
903 * Removes the head of the list if there are items on the list
905 * Return: pointer dequeued binder_work, NULL if list was empty
907 static struct binder_work *binder_dequeue_work_head(
908 struct binder_proc *proc,
909 struct list_head *list)
911 struct binder_work *w;
913 binder_inner_proc_lock(proc);
914 w = binder_dequeue_work_head_ilocked(list);
915 binder_inner_proc_unlock(proc);
916 return w;
919 static void
920 binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
921 static void binder_free_thread(struct binder_thread *thread);
922 static void binder_free_proc(struct binder_proc *proc);
923 static void binder_inc_node_tmpref_ilocked(struct binder_node *node);
925 static int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
927 unsigned long rlim_cur;
928 unsigned long irqs;
929 int ret;
931 mutex_lock(&proc->files_lock);
932 if (proc->files == NULL) {
933 ret = -ESRCH;
934 goto err;
936 if (!lock_task_sighand(proc->tsk, &irqs)) {
937 ret = -EMFILE;
938 goto err;
940 rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
941 unlock_task_sighand(proc->tsk, &irqs);
943 ret = __alloc_fd(proc->files, 0, rlim_cur, flags);
944 err:
945 mutex_unlock(&proc->files_lock);
946 return ret;
950 * copied from fd_install
952 static void task_fd_install(
953 struct binder_proc *proc, unsigned int fd, struct file *file)
955 mutex_lock(&proc->files_lock);
956 if (proc->files)
957 __fd_install(proc->files, fd, file);
958 mutex_unlock(&proc->files_lock);
962 * copied from sys_close
964 static long task_close_fd(struct binder_proc *proc, unsigned int fd)
966 int retval;
968 mutex_lock(&proc->files_lock);
969 if (proc->files == NULL) {
970 retval = -ESRCH;
971 goto err;
973 retval = __close_fd(proc->files, fd);
974 /* can't restart close syscall because file table entry was cleared */
975 if (unlikely(retval == -ERESTARTSYS ||
976 retval == -ERESTARTNOINTR ||
977 retval == -ERESTARTNOHAND ||
978 retval == -ERESTART_RESTARTBLOCK))
979 retval = -EINTR;
980 err:
981 mutex_unlock(&proc->files_lock);
982 return retval;
985 static bool binder_has_work_ilocked(struct binder_thread *thread,
986 bool do_proc_work)
988 return thread->process_todo ||
989 thread->looper_need_return ||
990 (do_proc_work &&
991 !binder_worklist_empty_ilocked(&thread->proc->todo));
994 static bool binder_has_work(struct binder_thread *thread, bool do_proc_work)
996 bool has_work;
998 binder_inner_proc_lock(thread->proc);
999 has_work = binder_has_work_ilocked(thread, do_proc_work);
1000 binder_inner_proc_unlock(thread->proc);
1002 return has_work;
1005 static bool binder_available_for_proc_work_ilocked(struct binder_thread *thread)
1007 return !thread->transaction_stack &&
1008 binder_worklist_empty_ilocked(&thread->todo) &&
1009 (thread->looper & (BINDER_LOOPER_STATE_ENTERED |
1010 BINDER_LOOPER_STATE_REGISTERED));
1013 static void binder_wakeup_poll_threads_ilocked(struct binder_proc *proc,
1014 bool sync)
1016 struct rb_node *n;
1017 struct binder_thread *thread;
1019 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
1020 thread = rb_entry(n, struct binder_thread, rb_node);
1021 if (thread->looper & BINDER_LOOPER_STATE_POLL &&
1022 binder_available_for_proc_work_ilocked(thread)) {
1023 if (sync)
1024 wake_up_interruptible_sync(&thread->wait);
1025 else
1026 wake_up_interruptible(&thread->wait);
1032 * binder_select_thread_ilocked() - selects a thread for doing proc work.
1033 * @proc: process to select a thread from
1035 * Note that calling this function moves the thread off the waiting_threads
1036 * list, so it can only be woken up by the caller of this function, or a
1037 * signal. Therefore, callers *should* always wake up the thread this function
1038 * returns.
1040 * Return: If there's a thread currently waiting for process work,
1041 * returns that thread. Otherwise returns NULL.
1043 static struct binder_thread *
1044 binder_select_thread_ilocked(struct binder_proc *proc)
1046 struct binder_thread *thread;
1048 assert_spin_locked(&proc->inner_lock);
1049 thread = list_first_entry_or_null(&proc->waiting_threads,
1050 struct binder_thread,
1051 waiting_thread_node);
1053 if (thread)
1054 list_del_init(&thread->waiting_thread_node);
1056 return thread;
1060 * binder_wakeup_thread_ilocked() - wakes up a thread for doing proc work.
1061 * @proc: process to wake up a thread in
1062 * @thread: specific thread to wake-up (may be NULL)
1063 * @sync: whether to do a synchronous wake-up
1065 * This function wakes up a thread in the @proc process.
1066 * The caller may provide a specific thread to wake-up in
1067 * the @thread parameter. If @thread is NULL, this function
1068 * will wake up threads that have called poll().
1070 * Note that for this function to work as expected, callers
1071 * should first call binder_select_thread() to find a thread
1072 * to handle the work (if they don't have a thread already),
1073 * and pass the result into the @thread parameter.
1075 static void binder_wakeup_thread_ilocked(struct binder_proc *proc,
1076 struct binder_thread *thread,
1077 bool sync)
1079 assert_spin_locked(&proc->inner_lock);
1081 if (thread) {
1082 if (sync)
1083 wake_up_interruptible_sync(&thread->wait);
1084 else
1085 wake_up_interruptible(&thread->wait);
1086 return;
1089 /* Didn't find a thread waiting for proc work; this can happen
1090 * in two scenarios:
1091 * 1. All threads are busy handling transactions
1092 * In that case, one of those threads should call back into
1093 * the kernel driver soon and pick up this work.
1094 * 2. Threads are using the (e)poll interface, in which case
1095 * they may be blocked on the waitqueue without having been
1096 * added to waiting_threads. For this case, we just iterate
1097 * over all threads not handling transaction work, and
1098 * wake them all up. We wake all because we don't know whether
1099 * a thread that called into (e)poll is handling non-binder
1100 * work currently.
1102 binder_wakeup_poll_threads_ilocked(proc, sync);
1105 static void binder_wakeup_proc_ilocked(struct binder_proc *proc)
1107 struct binder_thread *thread = binder_select_thread_ilocked(proc);
1109 binder_wakeup_thread_ilocked(proc, thread, /* sync = */false);
1112 static void binder_set_nice(long nice)
1114 long min_nice;
1116 if (can_nice(current, nice)) {
1117 set_user_nice(current, nice);
1118 return;
1120 min_nice = rlimit_to_nice(rlimit(RLIMIT_NICE));
1121 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
1122 "%d: nice value %ld not allowed use %ld instead\n",
1123 current->pid, nice, min_nice);
1124 set_user_nice(current, min_nice);
1125 if (min_nice <= MAX_NICE)
1126 return;
1127 binder_user_error("%d RLIMIT_NICE not set\n", current->pid);
1130 static struct binder_node *binder_get_node_ilocked(struct binder_proc *proc,
1131 binder_uintptr_t ptr)
1133 struct rb_node *n = proc->nodes.rb_node;
1134 struct binder_node *node;
1136 assert_spin_locked(&proc->inner_lock);
1138 while (n) {
1139 node = rb_entry(n, struct binder_node, rb_node);
1141 if (ptr < node->ptr)
1142 n = n->rb_left;
1143 else if (ptr > node->ptr)
1144 n = n->rb_right;
1145 else {
1147 * take an implicit weak reference
1148 * to ensure node stays alive until
1149 * call to binder_put_node()
1151 binder_inc_node_tmpref_ilocked(node);
1152 return node;
1155 return NULL;
1158 static struct binder_node *binder_get_node(struct binder_proc *proc,
1159 binder_uintptr_t ptr)
1161 struct binder_node *node;
1163 binder_inner_proc_lock(proc);
1164 node = binder_get_node_ilocked(proc, ptr);
1165 binder_inner_proc_unlock(proc);
1166 return node;
1169 static struct binder_node *binder_init_node_ilocked(
1170 struct binder_proc *proc,
1171 struct binder_node *new_node,
1172 struct flat_binder_object *fp)
1174 struct rb_node **p = &proc->nodes.rb_node;
1175 struct rb_node *parent = NULL;
1176 struct binder_node *node;
1177 binder_uintptr_t ptr = fp ? fp->binder : 0;
1178 binder_uintptr_t cookie = fp ? fp->cookie : 0;
1179 __u32 flags = fp ? fp->flags : 0;
1181 assert_spin_locked(&proc->inner_lock);
1183 while (*p) {
1185 parent = *p;
1186 node = rb_entry(parent, struct binder_node, rb_node);
1188 if (ptr < node->ptr)
1189 p = &(*p)->rb_left;
1190 else if (ptr > node->ptr)
1191 p = &(*p)->rb_right;
1192 else {
1194 * A matching node is already in
1195 * the rb tree. Abandon the init
1196 * and return it.
1198 binder_inc_node_tmpref_ilocked(node);
1199 return node;
1202 node = new_node;
1203 binder_stats_created(BINDER_STAT_NODE);
1204 node->tmp_refs++;
1205 rb_link_node(&node->rb_node, parent, p);
1206 rb_insert_color(&node->rb_node, &proc->nodes);
1207 node->debug_id = atomic_inc_return(&binder_last_id);
1208 node->proc = proc;
1209 node->ptr = ptr;
1210 node->cookie = cookie;
1211 node->work.type = BINDER_WORK_NODE;
1212 node->min_priority = flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
1213 node->accept_fds = !!(flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
1214 spin_lock_init(&node->lock);
1215 INIT_LIST_HEAD(&node->work.entry);
1216 INIT_LIST_HEAD(&node->async_todo);
1217 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1218 "%d:%d node %d u%016llx c%016llx created\n",
1219 proc->pid, current->pid, node->debug_id,
1220 (u64)node->ptr, (u64)node->cookie);
1222 return node;
1225 static struct binder_node *binder_new_node(struct binder_proc *proc,
1226 struct flat_binder_object *fp)
1228 struct binder_node *node;
1229 struct binder_node *new_node = kzalloc(sizeof(*node), GFP_KERNEL);
1231 if (!new_node)
1232 return NULL;
1233 binder_inner_proc_lock(proc);
1234 node = binder_init_node_ilocked(proc, new_node, fp);
1235 binder_inner_proc_unlock(proc);
1236 if (node != new_node)
1238 * The node was already added by another thread
1240 kfree(new_node);
1242 return node;
1245 static void binder_free_node(struct binder_node *node)
1247 kfree(node);
1248 binder_stats_deleted(BINDER_STAT_NODE);
1251 static int binder_inc_node_nilocked(struct binder_node *node, int strong,
1252 int internal,
1253 struct list_head *target_list)
1255 struct binder_proc *proc = node->proc;
1257 assert_spin_locked(&node->lock);
1258 if (proc)
1259 assert_spin_locked(&proc->inner_lock);
1260 if (strong) {
1261 if (internal) {
1262 if (target_list == NULL &&
1263 node->internal_strong_refs == 0 &&
1264 !(node->proc &&
1265 node == node->proc->context->binder_context_mgr_node &&
1266 node->has_strong_ref)) {
1267 pr_err("invalid inc strong node for %d\n",
1268 node->debug_id);
1269 return -EINVAL;
1271 node->internal_strong_refs++;
1272 } else
1273 node->local_strong_refs++;
1274 if (!node->has_strong_ref && target_list) {
1275 struct binder_thread *thread = container_of(target_list,
1276 struct binder_thread, todo);
1277 binder_dequeue_work_ilocked(&node->work);
1278 BUG_ON(&thread->todo != target_list);
1279 binder_enqueue_deferred_thread_work_ilocked(thread,
1280 &node->work);
1282 } else {
1283 if (!internal)
1284 node->local_weak_refs++;
1285 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
1286 if (target_list == NULL) {
1287 pr_err("invalid inc weak node for %d\n",
1288 node->debug_id);
1289 return -EINVAL;
1292 * See comment above
1294 binder_enqueue_work_ilocked(&node->work, target_list);
1297 return 0;
1300 static int binder_inc_node(struct binder_node *node, int strong, int internal,
1301 struct list_head *target_list)
1303 int ret;
1305 binder_node_inner_lock(node);
1306 ret = binder_inc_node_nilocked(node, strong, internal, target_list);
1307 binder_node_inner_unlock(node);
1309 return ret;
1312 static bool binder_dec_node_nilocked(struct binder_node *node,
1313 int strong, int internal)
1315 struct binder_proc *proc = node->proc;
1317 assert_spin_locked(&node->lock);
1318 if (proc)
1319 assert_spin_locked(&proc->inner_lock);
1320 if (strong) {
1321 if (internal)
1322 node->internal_strong_refs--;
1323 else
1324 node->local_strong_refs--;
1325 if (node->local_strong_refs || node->internal_strong_refs)
1326 return false;
1327 } else {
1328 if (!internal)
1329 node->local_weak_refs--;
1330 if (node->local_weak_refs || node->tmp_refs ||
1331 !hlist_empty(&node->refs))
1332 return false;
1335 if (proc && (node->has_strong_ref || node->has_weak_ref)) {
1336 if (list_empty(&node->work.entry)) {
1337 binder_enqueue_work_ilocked(&node->work, &proc->todo);
1338 binder_wakeup_proc_ilocked(proc);
1340 } else {
1341 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
1342 !node->local_weak_refs && !node->tmp_refs) {
1343 if (proc) {
1344 binder_dequeue_work_ilocked(&node->work);
1345 rb_erase(&node->rb_node, &proc->nodes);
1346 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1347 "refless node %d deleted\n",
1348 node->debug_id);
1349 } else {
1350 BUG_ON(!list_empty(&node->work.entry));
1351 spin_lock(&binder_dead_nodes_lock);
1353 * tmp_refs could have changed so
1354 * check it again
1356 if (node->tmp_refs) {
1357 spin_unlock(&binder_dead_nodes_lock);
1358 return false;
1360 hlist_del(&node->dead_node);
1361 spin_unlock(&binder_dead_nodes_lock);
1362 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1363 "dead node %d deleted\n",
1364 node->debug_id);
1366 return true;
1369 return false;
1372 static void binder_dec_node(struct binder_node *node, int strong, int internal)
1374 bool free_node;
1376 binder_node_inner_lock(node);
1377 free_node = binder_dec_node_nilocked(node, strong, internal);
1378 binder_node_inner_unlock(node);
1379 if (free_node)
1380 binder_free_node(node);
1383 static void binder_inc_node_tmpref_ilocked(struct binder_node *node)
1386 * No call to binder_inc_node() is needed since we
1387 * don't need to inform userspace of any changes to
1388 * tmp_refs
1390 node->tmp_refs++;
1394 * binder_inc_node_tmpref() - take a temporary reference on node
1395 * @node: node to reference
1397 * Take reference on node to prevent the node from being freed
1398 * while referenced only by a local variable. The inner lock is
1399 * needed to serialize with the node work on the queue (which
1400 * isn't needed after the node is dead). If the node is dead
1401 * (node->proc is NULL), use binder_dead_nodes_lock to protect
1402 * node->tmp_refs against dead-node-only cases where the node
1403 * lock cannot be acquired (eg traversing the dead node list to
1404 * print nodes)
1406 static void binder_inc_node_tmpref(struct binder_node *node)
1408 binder_node_lock(node);
1409 if (node->proc)
1410 binder_inner_proc_lock(node->proc);
1411 else
1412 spin_lock(&binder_dead_nodes_lock);
1413 binder_inc_node_tmpref_ilocked(node);
1414 if (node->proc)
1415 binder_inner_proc_unlock(node->proc);
1416 else
1417 spin_unlock(&binder_dead_nodes_lock);
1418 binder_node_unlock(node);
1422 * binder_dec_node_tmpref() - remove a temporary reference on node
1423 * @node: node to reference
1425 * Release temporary reference on node taken via binder_inc_node_tmpref()
1427 static void binder_dec_node_tmpref(struct binder_node *node)
1429 bool free_node;
1431 binder_node_inner_lock(node);
1432 if (!node->proc)
1433 spin_lock(&binder_dead_nodes_lock);
1434 node->tmp_refs--;
1435 BUG_ON(node->tmp_refs < 0);
1436 if (!node->proc)
1437 spin_unlock(&binder_dead_nodes_lock);
1439 * Call binder_dec_node() to check if all refcounts are 0
1440 * and cleanup is needed. Calling with strong=0 and internal=1
1441 * causes no actual reference to be released in binder_dec_node().
1442 * If that changes, a change is needed here too.
1444 free_node = binder_dec_node_nilocked(node, 0, 1);
1445 binder_node_inner_unlock(node);
1446 if (free_node)
1447 binder_free_node(node);
1450 static void binder_put_node(struct binder_node *node)
1452 binder_dec_node_tmpref(node);
1455 static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc,
1456 u32 desc, bool need_strong_ref)
1458 struct rb_node *n = proc->refs_by_desc.rb_node;
1459 struct binder_ref *ref;
1461 while (n) {
1462 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1464 if (desc < ref->data.desc) {
1465 n = n->rb_left;
1466 } else if (desc > ref->data.desc) {
1467 n = n->rb_right;
1468 } else if (need_strong_ref && !ref->data.strong) {
1469 binder_user_error("tried to use weak ref as strong ref\n");
1470 return NULL;
1471 } else {
1472 return ref;
1475 return NULL;
1479 * binder_get_ref_for_node_olocked() - get the ref associated with given node
1480 * @proc: binder_proc that owns the ref
1481 * @node: binder_node of target
1482 * @new_ref: newly allocated binder_ref to be initialized or %NULL
1484 * Look up the ref for the given node and return it if it exists
1486 * If it doesn't exist and the caller provides a newly allocated
1487 * ref, initialize the fields of the newly allocated ref and insert
1488 * into the given proc rb_trees and node refs list.
1490 * Return: the ref for node. It is possible that another thread
1491 * allocated/initialized the ref first in which case the
1492 * returned ref would be different than the passed-in
1493 * new_ref. new_ref must be kfree'd by the caller in
1494 * this case.
1496 static struct binder_ref *binder_get_ref_for_node_olocked(
1497 struct binder_proc *proc,
1498 struct binder_node *node,
1499 struct binder_ref *new_ref)
1501 struct binder_context *context = proc->context;
1502 struct rb_node **p = &proc->refs_by_node.rb_node;
1503 struct rb_node *parent = NULL;
1504 struct binder_ref *ref;
1505 struct rb_node *n;
1507 while (*p) {
1508 parent = *p;
1509 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1511 if (node < ref->node)
1512 p = &(*p)->rb_left;
1513 else if (node > ref->node)
1514 p = &(*p)->rb_right;
1515 else
1516 return ref;
1518 if (!new_ref)
1519 return NULL;
1521 binder_stats_created(BINDER_STAT_REF);
1522 new_ref->data.debug_id = atomic_inc_return(&binder_last_id);
1523 new_ref->proc = proc;
1524 new_ref->node = node;
1525 rb_link_node(&new_ref->rb_node_node, parent, p);
1526 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1528 new_ref->data.desc = (node == context->binder_context_mgr_node) ? 0 : 1;
1529 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1530 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1531 if (ref->data.desc > new_ref->data.desc)
1532 break;
1533 new_ref->data.desc = ref->data.desc + 1;
1536 p = &proc->refs_by_desc.rb_node;
1537 while (*p) {
1538 parent = *p;
1539 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1541 if (new_ref->data.desc < ref->data.desc)
1542 p = &(*p)->rb_left;
1543 else if (new_ref->data.desc > ref->data.desc)
1544 p = &(*p)->rb_right;
1545 else
1546 BUG();
1548 rb_link_node(&new_ref->rb_node_desc, parent, p);
1549 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
1551 binder_node_lock(node);
1552 hlist_add_head(&new_ref->node_entry, &node->refs);
1554 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1555 "%d new ref %d desc %d for node %d\n",
1556 proc->pid, new_ref->data.debug_id, new_ref->data.desc,
1557 node->debug_id);
1558 binder_node_unlock(node);
1559 return new_ref;
1562 static void binder_cleanup_ref_olocked(struct binder_ref *ref)
1564 bool delete_node = false;
1566 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1567 "%d delete ref %d desc %d for node %d\n",
1568 ref->proc->pid, ref->data.debug_id, ref->data.desc,
1569 ref->node->debug_id);
1571 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1572 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
1574 binder_node_inner_lock(ref->node);
1575 if (ref->data.strong)
1576 binder_dec_node_nilocked(ref->node, 1, 1);
1578 hlist_del(&ref->node_entry);
1579 delete_node = binder_dec_node_nilocked(ref->node, 0, 1);
1580 binder_node_inner_unlock(ref->node);
1582 * Clear ref->node unless we want the caller to free the node
1584 if (!delete_node) {
1586 * The caller uses ref->node to determine
1587 * whether the node needs to be freed. Clear
1588 * it since the node is still alive.
1590 ref->node = NULL;
1593 if (ref->death) {
1594 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1595 "%d delete ref %d desc %d has death notification\n",
1596 ref->proc->pid, ref->data.debug_id,
1597 ref->data.desc);
1598 binder_dequeue_work(ref->proc, &ref->death->work);
1599 binder_stats_deleted(BINDER_STAT_DEATH);
1601 binder_stats_deleted(BINDER_STAT_REF);
1605 * binder_inc_ref_olocked() - increment the ref for given handle
1606 * @ref: ref to be incremented
1607 * @strong: if true, strong increment, else weak
1608 * @target_list: list to queue node work on
1610 * Increment the ref. @ref->proc->outer_lock must be held on entry
1612 * Return: 0, if successful, else errno
1614 static int binder_inc_ref_olocked(struct binder_ref *ref, int strong,
1615 struct list_head *target_list)
1617 int ret;
1619 if (strong) {
1620 if (ref->data.strong == 0) {
1621 ret = binder_inc_node(ref->node, 1, 1, target_list);
1622 if (ret)
1623 return ret;
1625 ref->data.strong++;
1626 } else {
1627 if (ref->data.weak == 0) {
1628 ret = binder_inc_node(ref->node, 0, 1, target_list);
1629 if (ret)
1630 return ret;
1632 ref->data.weak++;
1634 return 0;
1638 * binder_dec_ref() - dec the ref for given handle
1639 * @ref: ref to be decremented
1640 * @strong: if true, strong decrement, else weak
1642 * Decrement the ref.
1644 * Return: true if ref is cleaned up and ready to be freed
1646 static bool binder_dec_ref_olocked(struct binder_ref *ref, int strong)
1648 if (strong) {
1649 if (ref->data.strong == 0) {
1650 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
1651 ref->proc->pid, ref->data.debug_id,
1652 ref->data.desc, ref->data.strong,
1653 ref->data.weak);
1654 return false;
1656 ref->data.strong--;
1657 if (ref->data.strong == 0)
1658 binder_dec_node(ref->node, strong, 1);
1659 } else {
1660 if (ref->data.weak == 0) {
1661 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
1662 ref->proc->pid, ref->data.debug_id,
1663 ref->data.desc, ref->data.strong,
1664 ref->data.weak);
1665 return false;
1667 ref->data.weak--;
1669 if (ref->data.strong == 0 && ref->data.weak == 0) {
1670 binder_cleanup_ref_olocked(ref);
1671 return true;
1673 return false;
1677 * binder_get_node_from_ref() - get the node from the given proc/desc
1678 * @proc: proc containing the ref
1679 * @desc: the handle associated with the ref
1680 * @need_strong_ref: if true, only return node if ref is strong
1681 * @rdata: the id/refcount data for the ref
1683 * Given a proc and ref handle, return the associated binder_node
1685 * Return: a binder_node or NULL if not found or not strong when strong required
1687 static struct binder_node *binder_get_node_from_ref(
1688 struct binder_proc *proc,
1689 u32 desc, bool need_strong_ref,
1690 struct binder_ref_data *rdata)
1692 struct binder_node *node;
1693 struct binder_ref *ref;
1695 binder_proc_lock(proc);
1696 ref = binder_get_ref_olocked(proc, desc, need_strong_ref);
1697 if (!ref)
1698 goto err_no_ref;
1699 node = ref->node;
1701 * Take an implicit reference on the node to ensure
1702 * it stays alive until the call to binder_put_node()
1704 binder_inc_node_tmpref(node);
1705 if (rdata)
1706 *rdata = ref->data;
1707 binder_proc_unlock(proc);
1709 return node;
1711 err_no_ref:
1712 binder_proc_unlock(proc);
1713 return NULL;
1717 * binder_free_ref() - free the binder_ref
1718 * @ref: ref to free
1720 * Free the binder_ref. Free the binder_node indicated by ref->node
1721 * (if non-NULL) and the binder_ref_death indicated by ref->death.
1723 static void binder_free_ref(struct binder_ref *ref)
1725 if (ref->node)
1726 binder_free_node(ref->node);
1727 kfree(ref->death);
1728 kfree(ref);
1732 * binder_update_ref_for_handle() - inc/dec the ref for given handle
1733 * @proc: proc containing the ref
1734 * @desc: the handle associated with the ref
1735 * @increment: true=inc reference, false=dec reference
1736 * @strong: true=strong reference, false=weak reference
1737 * @rdata: the id/refcount data for the ref
1739 * Given a proc and ref handle, increment or decrement the ref
1740 * according to "increment" arg.
1742 * Return: 0 if successful, else errno
1744 static int binder_update_ref_for_handle(struct binder_proc *proc,
1745 uint32_t desc, bool increment, bool strong,
1746 struct binder_ref_data *rdata)
1748 int ret = 0;
1749 struct binder_ref *ref;
1750 bool delete_ref = false;
1752 binder_proc_lock(proc);
1753 ref = binder_get_ref_olocked(proc, desc, strong);
1754 if (!ref) {
1755 ret = -EINVAL;
1756 goto err_no_ref;
1758 if (increment)
1759 ret = binder_inc_ref_olocked(ref, strong, NULL);
1760 else
1761 delete_ref = binder_dec_ref_olocked(ref, strong);
1763 if (rdata)
1764 *rdata = ref->data;
1765 binder_proc_unlock(proc);
1767 if (delete_ref)
1768 binder_free_ref(ref);
1769 return ret;
1771 err_no_ref:
1772 binder_proc_unlock(proc);
1773 return ret;
1777 * binder_dec_ref_for_handle() - dec the ref for given handle
1778 * @proc: proc containing the ref
1779 * @desc: the handle associated with the ref
1780 * @strong: true=strong reference, false=weak reference
1781 * @rdata: the id/refcount data for the ref
1783 * Just calls binder_update_ref_for_handle() to decrement the ref.
1785 * Return: 0 if successful, else errno
1787 static int binder_dec_ref_for_handle(struct binder_proc *proc,
1788 uint32_t desc, bool strong, struct binder_ref_data *rdata)
1790 return binder_update_ref_for_handle(proc, desc, false, strong, rdata);
1795 * binder_inc_ref_for_node() - increment the ref for given proc/node
1796 * @proc: proc containing the ref
1797 * @node: target node
1798 * @strong: true=strong reference, false=weak reference
1799 * @target_list: worklist to use if node is incremented
1800 * @rdata: the id/refcount data for the ref
1802 * Given a proc and node, increment the ref. Create the ref if it
1803 * doesn't already exist
1805 * Return: 0 if successful, else errno
1807 static int binder_inc_ref_for_node(struct binder_proc *proc,
1808 struct binder_node *node,
1809 bool strong,
1810 struct list_head *target_list,
1811 struct binder_ref_data *rdata)
1813 struct binder_ref *ref;
1814 struct binder_ref *new_ref = NULL;
1815 int ret = 0;
1817 binder_proc_lock(proc);
1818 ref = binder_get_ref_for_node_olocked(proc, node, NULL);
1819 if (!ref) {
1820 binder_proc_unlock(proc);
1821 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1822 if (!new_ref)
1823 return -ENOMEM;
1824 binder_proc_lock(proc);
1825 ref = binder_get_ref_for_node_olocked(proc, node, new_ref);
1827 ret = binder_inc_ref_olocked(ref, strong, target_list);
1828 *rdata = ref->data;
1829 binder_proc_unlock(proc);
1830 if (new_ref && ref != new_ref)
1832 * Another thread created the ref first so
1833 * free the one we allocated
1835 kfree(new_ref);
1836 return ret;
1839 static void binder_pop_transaction_ilocked(struct binder_thread *target_thread,
1840 struct binder_transaction *t)
1842 BUG_ON(!target_thread);
1843 assert_spin_locked(&target_thread->proc->inner_lock);
1844 BUG_ON(target_thread->transaction_stack != t);
1845 BUG_ON(target_thread->transaction_stack->from != target_thread);
1846 target_thread->transaction_stack =
1847 target_thread->transaction_stack->from_parent;
1848 t->from = NULL;
1852 * binder_thread_dec_tmpref() - decrement thread->tmp_ref
1853 * @thread: thread to decrement
1855 * A thread needs to be kept alive while being used to create or
1856 * handle a transaction. binder_get_txn_from() is used to safely
1857 * extract t->from from a binder_transaction and keep the thread
1858 * indicated by t->from from being freed. When done with that
1859 * binder_thread, this function is called to decrement the
1860 * tmp_ref and free if appropriate (thread has been released
1861 * and no transaction being processed by the driver)
1863 static void binder_thread_dec_tmpref(struct binder_thread *thread)
1866 * atomic is used to protect the counter value while
1867 * it cannot reach zero or thread->is_dead is false
1869 binder_inner_proc_lock(thread->proc);
1870 atomic_dec(&thread->tmp_ref);
1871 if (thread->is_dead && !atomic_read(&thread->tmp_ref)) {
1872 binder_inner_proc_unlock(thread->proc);
1873 binder_free_thread(thread);
1874 return;
1876 binder_inner_proc_unlock(thread->proc);
1880 * binder_proc_dec_tmpref() - decrement proc->tmp_ref
1881 * @proc: proc to decrement
1883 * A binder_proc needs to be kept alive while being used to create or
1884 * handle a transaction. proc->tmp_ref is incremented when
1885 * creating a new transaction or the binder_proc is currently in-use
1886 * by threads that are being released. When done with the binder_proc,
1887 * this function is called to decrement the counter and free the
1888 * proc if appropriate (proc has been released, all threads have
1889 * been released and not currenly in-use to process a transaction).
1891 static void binder_proc_dec_tmpref(struct binder_proc *proc)
1893 binder_inner_proc_lock(proc);
1894 proc->tmp_ref--;
1895 if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) &&
1896 !proc->tmp_ref) {
1897 binder_inner_proc_unlock(proc);
1898 binder_free_proc(proc);
1899 return;
1901 binder_inner_proc_unlock(proc);
1905 * binder_get_txn_from() - safely extract the "from" thread in transaction
1906 * @t: binder transaction for t->from
1908 * Atomically return the "from" thread and increment the tmp_ref
1909 * count for the thread to ensure it stays alive until
1910 * binder_thread_dec_tmpref() is called.
1912 * Return: the value of t->from
1914 static struct binder_thread *binder_get_txn_from(
1915 struct binder_transaction *t)
1917 struct binder_thread *from;
1919 spin_lock(&t->lock);
1920 from = t->from;
1921 if (from)
1922 atomic_inc(&from->tmp_ref);
1923 spin_unlock(&t->lock);
1924 return from;
1928 * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock
1929 * @t: binder transaction for t->from
1931 * Same as binder_get_txn_from() except it also acquires the proc->inner_lock
1932 * to guarantee that the thread cannot be released while operating on it.
1933 * The caller must call binder_inner_proc_unlock() to release the inner lock
1934 * as well as call binder_dec_thread_txn() to release the reference.
1936 * Return: the value of t->from
1938 static struct binder_thread *binder_get_txn_from_and_acq_inner(
1939 struct binder_transaction *t)
1941 struct binder_thread *from;
1943 from = binder_get_txn_from(t);
1944 if (!from)
1945 return NULL;
1946 binder_inner_proc_lock(from->proc);
1947 if (t->from) {
1948 BUG_ON(from != t->from);
1949 return from;
1951 binder_inner_proc_unlock(from->proc);
1952 binder_thread_dec_tmpref(from);
1953 return NULL;
1956 static void binder_free_transaction(struct binder_transaction *t)
1958 struct binder_proc *target_proc = t->to_proc;
1960 if (target_proc) {
1961 binder_inner_proc_lock(target_proc);
1962 if (t->buffer)
1963 t->buffer->transaction = NULL;
1964 binder_inner_proc_unlock(target_proc);
1967 * If the transaction has no target_proc, then
1968 * t->buffer->transaction has already been cleared.
1970 kfree(t);
1971 binder_stats_deleted(BINDER_STAT_TRANSACTION);
1974 static void binder_send_failed_reply(struct binder_transaction *t,
1975 uint32_t error_code)
1977 struct binder_thread *target_thread;
1978 struct binder_transaction *next;
1980 BUG_ON(t->flags & TF_ONE_WAY);
1981 while (1) {
1982 target_thread = binder_get_txn_from_and_acq_inner(t);
1983 if (target_thread) {
1984 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1985 "send failed reply for transaction %d to %d:%d\n",
1986 t->debug_id,
1987 target_thread->proc->pid,
1988 target_thread->pid);
1990 binder_pop_transaction_ilocked(target_thread, t);
1991 if (target_thread->reply_error.cmd == BR_OK) {
1992 target_thread->reply_error.cmd = error_code;
1993 binder_enqueue_thread_work_ilocked(
1994 target_thread,
1995 &target_thread->reply_error.work);
1996 wake_up_interruptible(&target_thread->wait);
1997 } else {
1999 * Cannot get here for normal operation, but
2000 * we can if multiple synchronous transactions
2001 * are sent without blocking for responses.
2002 * Just ignore the 2nd error in this case.
2004 pr_warn("Unexpected reply error: %u\n",
2005 target_thread->reply_error.cmd);
2007 binder_inner_proc_unlock(target_thread->proc);
2008 binder_thread_dec_tmpref(target_thread);
2009 binder_free_transaction(t);
2010 return;
2012 next = t->from_parent;
2014 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2015 "send failed reply for transaction %d, target dead\n",
2016 t->debug_id);
2018 binder_free_transaction(t);
2019 if (next == NULL) {
2020 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2021 "reply failed, no target thread at root\n");
2022 return;
2024 t = next;
2025 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2026 "reply failed, no target thread -- retry %d\n",
2027 t->debug_id);
2032 * binder_cleanup_transaction() - cleans up undelivered transaction
2033 * @t: transaction that needs to be cleaned up
2034 * @reason: reason the transaction wasn't delivered
2035 * @error_code: error to return to caller (if synchronous call)
2037 static void binder_cleanup_transaction(struct binder_transaction *t,
2038 const char *reason,
2039 uint32_t error_code)
2041 if (t->buffer->target_node && !(t->flags & TF_ONE_WAY)) {
2042 binder_send_failed_reply(t, error_code);
2043 } else {
2044 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2045 "undelivered transaction %d, %s\n",
2046 t->debug_id, reason);
2047 binder_free_transaction(t);
2052 * binder_validate_object() - checks for a valid metadata object in a buffer.
2053 * @buffer: binder_buffer that we're parsing.
2054 * @offset: offset in the buffer at which to validate an object.
2056 * Return: If there's a valid metadata object at @offset in @buffer, the
2057 * size of that object. Otherwise, it returns zero.
2059 static size_t binder_validate_object(struct binder_buffer *buffer, u64 offset)
2061 /* Check if we can read a header first */
2062 struct binder_object_header *hdr;
2063 size_t object_size = 0;
2065 if (buffer->data_size < sizeof(*hdr) ||
2066 offset > buffer->data_size - sizeof(*hdr) ||
2067 !IS_ALIGNED(offset, sizeof(u32)))
2068 return 0;
2070 /* Ok, now see if we can read a complete object. */
2071 hdr = (struct binder_object_header *)(buffer->data + offset);
2072 switch (hdr->type) {
2073 case BINDER_TYPE_BINDER:
2074 case BINDER_TYPE_WEAK_BINDER:
2075 case BINDER_TYPE_HANDLE:
2076 case BINDER_TYPE_WEAK_HANDLE:
2077 object_size = sizeof(struct flat_binder_object);
2078 break;
2079 case BINDER_TYPE_FD:
2080 object_size = sizeof(struct binder_fd_object);
2081 break;
2082 case BINDER_TYPE_PTR:
2083 object_size = sizeof(struct binder_buffer_object);
2084 break;
2085 case BINDER_TYPE_FDA:
2086 object_size = sizeof(struct binder_fd_array_object);
2087 break;
2088 default:
2089 return 0;
2091 if (offset <= buffer->data_size - object_size &&
2092 buffer->data_size >= object_size)
2093 return object_size;
2094 else
2095 return 0;
2099 * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
2100 * @b: binder_buffer containing the object
2101 * @index: index in offset array at which the binder_buffer_object is
2102 * located
2103 * @start: points to the start of the offset array
2104 * @num_valid: the number of valid offsets in the offset array
2106 * Return: If @index is within the valid range of the offset array
2107 * described by @start and @num_valid, and if there's a valid
2108 * binder_buffer_object at the offset found in index @index
2109 * of the offset array, that object is returned. Otherwise,
2110 * %NULL is returned.
2111 * Note that the offset found in index @index itself is not
2112 * verified; this function assumes that @num_valid elements
2113 * from @start were previously verified to have valid offsets.
2115 static struct binder_buffer_object *binder_validate_ptr(struct binder_buffer *b,
2116 binder_size_t index,
2117 binder_size_t *start,
2118 binder_size_t num_valid)
2120 struct binder_buffer_object *buffer_obj;
2121 binder_size_t *offp;
2123 if (index >= num_valid)
2124 return NULL;
2126 offp = start + index;
2127 buffer_obj = (struct binder_buffer_object *)(b->data + *offp);
2128 if (buffer_obj->hdr.type != BINDER_TYPE_PTR)
2129 return NULL;
2131 return buffer_obj;
2135 * binder_validate_fixup() - validates pointer/fd fixups happen in order.
2136 * @b: transaction buffer
2137 * @objects_start start of objects buffer
2138 * @buffer: binder_buffer_object in which to fix up
2139 * @offset: start offset in @buffer to fix up
2140 * @last_obj: last binder_buffer_object that we fixed up in
2141 * @last_min_offset: minimum fixup offset in @last_obj
2143 * Return: %true if a fixup in buffer @buffer at offset @offset is
2144 * allowed.
2146 * For safety reasons, we only allow fixups inside a buffer to happen
2147 * at increasing offsets; additionally, we only allow fixup on the last
2148 * buffer object that was verified, or one of its parents.
2150 * Example of what is allowed:
2153 * B (parent = A, offset = 0)
2154 * C (parent = A, offset = 16)
2155 * D (parent = C, offset = 0)
2156 * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
2158 * Examples of what is not allowed:
2160 * Decreasing offsets within the same parent:
2162 * C (parent = A, offset = 16)
2163 * B (parent = A, offset = 0) // decreasing offset within A
2165 * Referring to a parent that wasn't the last object or any of its parents:
2167 * B (parent = A, offset = 0)
2168 * C (parent = A, offset = 0)
2169 * C (parent = A, offset = 16)
2170 * D (parent = B, offset = 0) // B is not A or any of A's parents
2172 static bool binder_validate_fixup(struct binder_buffer *b,
2173 binder_size_t *objects_start,
2174 struct binder_buffer_object *buffer,
2175 binder_size_t fixup_offset,
2176 struct binder_buffer_object *last_obj,
2177 binder_size_t last_min_offset)
2179 if (!last_obj) {
2180 /* Nothing to fix up in */
2181 return false;
2184 while (last_obj != buffer) {
2186 * Safe to retrieve the parent of last_obj, since it
2187 * was already previously verified by the driver.
2189 if ((last_obj->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0)
2190 return false;
2191 last_min_offset = last_obj->parent_offset + sizeof(uintptr_t);
2192 last_obj = (struct binder_buffer_object *)
2193 (b->data + *(objects_start + last_obj->parent));
2195 return (fixup_offset >= last_min_offset);
2198 static void binder_transaction_buffer_release(struct binder_proc *proc,
2199 struct binder_buffer *buffer,
2200 binder_size_t *failed_at)
2202 binder_size_t *offp, *off_start, *off_end;
2203 int debug_id = buffer->debug_id;
2205 binder_debug(BINDER_DEBUG_TRANSACTION,
2206 "%d buffer release %d, size %zd-%zd, failed at %pK\n",
2207 proc->pid, buffer->debug_id,
2208 buffer->data_size, buffer->offsets_size, failed_at);
2210 if (buffer->target_node)
2211 binder_dec_node(buffer->target_node, 1, 0);
2213 off_start = (binder_size_t *)(buffer->data +
2214 ALIGN(buffer->data_size, sizeof(void *)));
2215 if (failed_at)
2216 off_end = failed_at;
2217 else
2218 off_end = (void *)off_start + buffer->offsets_size;
2219 for (offp = off_start; offp < off_end; offp++) {
2220 struct binder_object_header *hdr;
2221 size_t object_size = binder_validate_object(buffer, *offp);
2223 if (object_size == 0) {
2224 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
2225 debug_id, (u64)*offp, buffer->data_size);
2226 continue;
2228 hdr = (struct binder_object_header *)(buffer->data + *offp);
2229 switch (hdr->type) {
2230 case BINDER_TYPE_BINDER:
2231 case BINDER_TYPE_WEAK_BINDER: {
2232 struct flat_binder_object *fp;
2233 struct binder_node *node;
2235 fp = to_flat_binder_object(hdr);
2236 node = binder_get_node(proc, fp->binder);
2237 if (node == NULL) {
2238 pr_err("transaction release %d bad node %016llx\n",
2239 debug_id, (u64)fp->binder);
2240 break;
2242 binder_debug(BINDER_DEBUG_TRANSACTION,
2243 " node %d u%016llx\n",
2244 node->debug_id, (u64)node->ptr);
2245 binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER,
2247 binder_put_node(node);
2248 } break;
2249 case BINDER_TYPE_HANDLE:
2250 case BINDER_TYPE_WEAK_HANDLE: {
2251 struct flat_binder_object *fp;
2252 struct binder_ref_data rdata;
2253 int ret;
2255 fp = to_flat_binder_object(hdr);
2256 ret = binder_dec_ref_for_handle(proc, fp->handle,
2257 hdr->type == BINDER_TYPE_HANDLE, &rdata);
2259 if (ret) {
2260 pr_err("transaction release %d bad handle %d, ret = %d\n",
2261 debug_id, fp->handle, ret);
2262 break;
2264 binder_debug(BINDER_DEBUG_TRANSACTION,
2265 " ref %d desc %d\n",
2266 rdata.debug_id, rdata.desc);
2267 } break;
2269 case BINDER_TYPE_FD: {
2270 struct binder_fd_object *fp = to_binder_fd_object(hdr);
2272 binder_debug(BINDER_DEBUG_TRANSACTION,
2273 " fd %d\n", fp->fd);
2274 if (failed_at)
2275 task_close_fd(proc, fp->fd);
2276 } break;
2277 case BINDER_TYPE_PTR:
2279 * Nothing to do here, this will get cleaned up when the
2280 * transaction buffer gets freed
2282 break;
2283 case BINDER_TYPE_FDA: {
2284 struct binder_fd_array_object *fda;
2285 struct binder_buffer_object *parent;
2286 uintptr_t parent_buffer;
2287 u32 *fd_array;
2288 size_t fd_index;
2289 binder_size_t fd_buf_size;
2291 fda = to_binder_fd_array_object(hdr);
2292 parent = binder_validate_ptr(buffer, fda->parent,
2293 off_start,
2294 offp - off_start);
2295 if (!parent) {
2296 pr_err("transaction release %d bad parent offset\n",
2297 debug_id);
2298 continue;
2301 * Since the parent was already fixed up, convert it
2302 * back to kernel address space to access it
2304 parent_buffer = parent->buffer -
2305 binder_alloc_get_user_buffer_offset(
2306 &proc->alloc);
2308 fd_buf_size = sizeof(u32) * fda->num_fds;
2309 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2310 pr_err("transaction release %d invalid number of fds (%lld)\n",
2311 debug_id, (u64)fda->num_fds);
2312 continue;
2314 if (fd_buf_size > parent->length ||
2315 fda->parent_offset > parent->length - fd_buf_size) {
2316 /* No space for all file descriptors here. */
2317 pr_err("transaction release %d not enough space for %lld fds in buffer\n",
2318 debug_id, (u64)fda->num_fds);
2319 continue;
2321 fd_array = (u32 *)(parent_buffer + (uintptr_t)fda->parent_offset);
2322 for (fd_index = 0; fd_index < fda->num_fds; fd_index++)
2323 task_close_fd(proc, fd_array[fd_index]);
2324 } break;
2325 default:
2326 pr_err("transaction release %d bad object type %x\n",
2327 debug_id, hdr->type);
2328 break;
2333 static int binder_translate_binder(struct flat_binder_object *fp,
2334 struct binder_transaction *t,
2335 struct binder_thread *thread)
2337 struct binder_node *node;
2338 struct binder_proc *proc = thread->proc;
2339 struct binder_proc *target_proc = t->to_proc;
2340 struct binder_ref_data rdata;
2341 int ret = 0;
2343 node = binder_get_node(proc, fp->binder);
2344 if (!node) {
2345 node = binder_new_node(proc, fp);
2346 if (!node)
2347 return -ENOMEM;
2349 if (fp->cookie != node->cookie) {
2350 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
2351 proc->pid, thread->pid, (u64)fp->binder,
2352 node->debug_id, (u64)fp->cookie,
2353 (u64)node->cookie);
2354 ret = -EINVAL;
2355 goto done;
2357 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2358 ret = -EPERM;
2359 goto done;
2362 ret = binder_inc_ref_for_node(target_proc, node,
2363 fp->hdr.type == BINDER_TYPE_BINDER,
2364 &thread->todo, &rdata);
2365 if (ret)
2366 goto done;
2368 if (fp->hdr.type == BINDER_TYPE_BINDER)
2369 fp->hdr.type = BINDER_TYPE_HANDLE;
2370 else
2371 fp->hdr.type = BINDER_TYPE_WEAK_HANDLE;
2372 fp->binder = 0;
2373 fp->handle = rdata.desc;
2374 fp->cookie = 0;
2376 trace_binder_transaction_node_to_ref(t, node, &rdata);
2377 binder_debug(BINDER_DEBUG_TRANSACTION,
2378 " node %d u%016llx -> ref %d desc %d\n",
2379 node->debug_id, (u64)node->ptr,
2380 rdata.debug_id, rdata.desc);
2381 done:
2382 binder_put_node(node);
2383 return ret;
2386 static int binder_translate_handle(struct flat_binder_object *fp,
2387 struct binder_transaction *t,
2388 struct binder_thread *thread)
2390 struct binder_proc *proc = thread->proc;
2391 struct binder_proc *target_proc = t->to_proc;
2392 struct binder_node *node;
2393 struct binder_ref_data src_rdata;
2394 int ret = 0;
2396 node = binder_get_node_from_ref(proc, fp->handle,
2397 fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata);
2398 if (!node) {
2399 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
2400 proc->pid, thread->pid, fp->handle);
2401 return -EINVAL;
2403 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2404 ret = -EPERM;
2405 goto done;
2408 binder_node_lock(node);
2409 if (node->proc == target_proc) {
2410 if (fp->hdr.type == BINDER_TYPE_HANDLE)
2411 fp->hdr.type = BINDER_TYPE_BINDER;
2412 else
2413 fp->hdr.type = BINDER_TYPE_WEAK_BINDER;
2414 fp->binder = node->ptr;
2415 fp->cookie = node->cookie;
2416 if (node->proc)
2417 binder_inner_proc_lock(node->proc);
2418 binder_inc_node_nilocked(node,
2419 fp->hdr.type == BINDER_TYPE_BINDER,
2420 0, NULL);
2421 if (node->proc)
2422 binder_inner_proc_unlock(node->proc);
2423 trace_binder_transaction_ref_to_node(t, node, &src_rdata);
2424 binder_debug(BINDER_DEBUG_TRANSACTION,
2425 " ref %d desc %d -> node %d u%016llx\n",
2426 src_rdata.debug_id, src_rdata.desc, node->debug_id,
2427 (u64)node->ptr);
2428 binder_node_unlock(node);
2429 } else {
2430 struct binder_ref_data dest_rdata;
2432 binder_node_unlock(node);
2433 ret = binder_inc_ref_for_node(target_proc, node,
2434 fp->hdr.type == BINDER_TYPE_HANDLE,
2435 NULL, &dest_rdata);
2436 if (ret)
2437 goto done;
2439 fp->binder = 0;
2440 fp->handle = dest_rdata.desc;
2441 fp->cookie = 0;
2442 trace_binder_transaction_ref_to_ref(t, node, &src_rdata,
2443 &dest_rdata);
2444 binder_debug(BINDER_DEBUG_TRANSACTION,
2445 " ref %d desc %d -> ref %d desc %d (node %d)\n",
2446 src_rdata.debug_id, src_rdata.desc,
2447 dest_rdata.debug_id, dest_rdata.desc,
2448 node->debug_id);
2450 done:
2451 binder_put_node(node);
2452 return ret;
2455 static int binder_translate_fd(int fd,
2456 struct binder_transaction *t,
2457 struct binder_thread *thread,
2458 struct binder_transaction *in_reply_to)
2460 struct binder_proc *proc = thread->proc;
2461 struct binder_proc *target_proc = t->to_proc;
2462 int target_fd;
2463 struct file *file;
2464 int ret;
2465 bool target_allows_fd;
2467 if (in_reply_to)
2468 target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS);
2469 else
2470 target_allows_fd = t->buffer->target_node->accept_fds;
2471 if (!target_allows_fd) {
2472 binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
2473 proc->pid, thread->pid,
2474 in_reply_to ? "reply" : "transaction",
2475 fd);
2476 ret = -EPERM;
2477 goto err_fd_not_accepted;
2480 file = fget(fd);
2481 if (!file) {
2482 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
2483 proc->pid, thread->pid, fd);
2484 ret = -EBADF;
2485 goto err_fget;
2487 ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file);
2488 if (ret < 0) {
2489 ret = -EPERM;
2490 goto err_security;
2493 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
2494 if (target_fd < 0) {
2495 ret = -ENOMEM;
2496 goto err_get_unused_fd;
2498 task_fd_install(target_proc, target_fd, file);
2499 trace_binder_transaction_fd(t, fd, target_fd);
2500 binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n",
2501 fd, target_fd);
2503 return target_fd;
2505 err_get_unused_fd:
2506 err_security:
2507 fput(file);
2508 err_fget:
2509 err_fd_not_accepted:
2510 return ret;
2513 static int binder_translate_fd_array(struct binder_fd_array_object *fda,
2514 struct binder_buffer_object *parent,
2515 struct binder_transaction *t,
2516 struct binder_thread *thread,
2517 struct binder_transaction *in_reply_to)
2519 binder_size_t fdi, fd_buf_size, num_installed_fds;
2520 int target_fd;
2521 uintptr_t parent_buffer;
2522 u32 *fd_array;
2523 struct binder_proc *proc = thread->proc;
2524 struct binder_proc *target_proc = t->to_proc;
2526 fd_buf_size = sizeof(u32) * fda->num_fds;
2527 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2528 binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
2529 proc->pid, thread->pid, (u64)fda->num_fds);
2530 return -EINVAL;
2532 if (fd_buf_size > parent->length ||
2533 fda->parent_offset > parent->length - fd_buf_size) {
2534 /* No space for all file descriptors here. */
2535 binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
2536 proc->pid, thread->pid, (u64)fda->num_fds);
2537 return -EINVAL;
2540 * Since the parent was already fixed up, convert it
2541 * back to the kernel address space to access it
2543 parent_buffer = parent->buffer -
2544 binder_alloc_get_user_buffer_offset(&target_proc->alloc);
2545 fd_array = (u32 *)(parent_buffer + (uintptr_t)fda->parent_offset);
2546 if (!IS_ALIGNED((unsigned long)fd_array, sizeof(u32))) {
2547 binder_user_error("%d:%d parent offset not aligned correctly.\n",
2548 proc->pid, thread->pid);
2549 return -EINVAL;
2551 for (fdi = 0; fdi < fda->num_fds; fdi++) {
2552 target_fd = binder_translate_fd(fd_array[fdi], t, thread,
2553 in_reply_to);
2554 if (target_fd < 0)
2555 goto err_translate_fd_failed;
2556 fd_array[fdi] = target_fd;
2558 return 0;
2560 err_translate_fd_failed:
2562 * Failed to allocate fd or security error, free fds
2563 * installed so far.
2565 num_installed_fds = fdi;
2566 for (fdi = 0; fdi < num_installed_fds; fdi++)
2567 task_close_fd(target_proc, fd_array[fdi]);
2568 return target_fd;
2571 static int binder_fixup_parent(struct binder_transaction *t,
2572 struct binder_thread *thread,
2573 struct binder_buffer_object *bp,
2574 binder_size_t *off_start,
2575 binder_size_t num_valid,
2576 struct binder_buffer_object *last_fixup_obj,
2577 binder_size_t last_fixup_min_off)
2579 struct binder_buffer_object *parent;
2580 u8 *parent_buffer;
2581 struct binder_buffer *b = t->buffer;
2582 struct binder_proc *proc = thread->proc;
2583 struct binder_proc *target_proc = t->to_proc;
2585 if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT))
2586 return 0;
2588 parent = binder_validate_ptr(b, bp->parent, off_start, num_valid);
2589 if (!parent) {
2590 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
2591 proc->pid, thread->pid);
2592 return -EINVAL;
2595 if (!binder_validate_fixup(b, off_start,
2596 parent, bp->parent_offset,
2597 last_fixup_obj,
2598 last_fixup_min_off)) {
2599 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
2600 proc->pid, thread->pid);
2601 return -EINVAL;
2604 if (parent->length < sizeof(binder_uintptr_t) ||
2605 bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) {
2606 /* No space for a pointer here! */
2607 binder_user_error("%d:%d got transaction with invalid parent offset\n",
2608 proc->pid, thread->pid);
2609 return -EINVAL;
2611 parent_buffer = (u8 *)((uintptr_t)parent->buffer -
2612 binder_alloc_get_user_buffer_offset(
2613 &target_proc->alloc));
2614 *(binder_uintptr_t *)(parent_buffer + bp->parent_offset) = bp->buffer;
2616 return 0;
2620 * binder_proc_transaction() - sends a transaction to a process and wakes it up
2621 * @t: transaction to send
2622 * @proc: process to send the transaction to
2623 * @thread: thread in @proc to send the transaction to (may be NULL)
2625 * This function queues a transaction to the specified process. It will try
2626 * to find a thread in the target process to handle the transaction and
2627 * wake it up. If no thread is found, the work is queued to the proc
2628 * waitqueue.
2630 * If the @thread parameter is not NULL, the transaction is always queued
2631 * to the waitlist of that specific thread.
2633 * Return: true if the transactions was successfully queued
2634 * false if the target process or thread is dead
2636 static bool binder_proc_transaction(struct binder_transaction *t,
2637 struct binder_proc *proc,
2638 struct binder_thread *thread)
2640 struct binder_node *node = t->buffer->target_node;
2641 bool oneway = !!(t->flags & TF_ONE_WAY);
2642 bool pending_async = false;
2644 BUG_ON(!node);
2645 binder_node_lock(node);
2646 if (oneway) {
2647 BUG_ON(thread);
2648 if (node->has_async_transaction) {
2649 pending_async = true;
2650 } else {
2651 node->has_async_transaction = true;
2655 binder_inner_proc_lock(proc);
2657 if (proc->is_dead || (thread && thread->is_dead)) {
2658 binder_inner_proc_unlock(proc);
2659 binder_node_unlock(node);
2660 return false;
2663 if (!thread && !pending_async)
2664 thread = binder_select_thread_ilocked(proc);
2666 if (thread)
2667 binder_enqueue_thread_work_ilocked(thread, &t->work);
2668 else if (!pending_async)
2669 binder_enqueue_work_ilocked(&t->work, &proc->todo);
2670 else
2671 binder_enqueue_work_ilocked(&t->work, &node->async_todo);
2673 if (!pending_async)
2674 binder_wakeup_thread_ilocked(proc, thread, !oneway /* sync */);
2676 binder_inner_proc_unlock(proc);
2677 binder_node_unlock(node);
2679 return true;
2683 * binder_get_node_refs_for_txn() - Get required refs on node for txn
2684 * @node: struct binder_node for which to get refs
2685 * @proc: returns @node->proc if valid
2686 * @error: if no @proc then returns BR_DEAD_REPLY
2688 * User-space normally keeps the node alive when creating a transaction
2689 * since it has a reference to the target. The local strong ref keeps it
2690 * alive if the sending process dies before the target process processes
2691 * the transaction. If the source process is malicious or has a reference
2692 * counting bug, relying on the local strong ref can fail.
2694 * Since user-space can cause the local strong ref to go away, we also take
2695 * a tmpref on the node to ensure it survives while we are constructing
2696 * the transaction. We also need a tmpref on the proc while we are
2697 * constructing the transaction, so we take that here as well.
2699 * Return: The target_node with refs taken or NULL if no @node->proc is NULL.
2700 * Also sets @proc if valid. If the @node->proc is NULL indicating that the
2701 * target proc has died, @error is set to BR_DEAD_REPLY
2703 static struct binder_node *binder_get_node_refs_for_txn(
2704 struct binder_node *node,
2705 struct binder_proc **procp,
2706 uint32_t *error)
2708 struct binder_node *target_node = NULL;
2710 binder_node_inner_lock(node);
2711 if (node->proc) {
2712 target_node = node;
2713 binder_inc_node_nilocked(node, 1, 0, NULL);
2714 binder_inc_node_tmpref_ilocked(node);
2715 node->proc->tmp_ref++;
2716 *procp = node->proc;
2717 } else
2718 *error = BR_DEAD_REPLY;
2719 binder_node_inner_unlock(node);
2721 return target_node;
2724 static void binder_transaction(struct binder_proc *proc,
2725 struct binder_thread *thread,
2726 struct binder_transaction_data *tr, int reply,
2727 binder_size_t extra_buffers_size)
2729 int ret;
2730 struct binder_transaction *t;
2731 struct binder_work *w;
2732 struct binder_work *tcomplete;
2733 binder_size_t *offp, *off_end, *off_start;
2734 binder_size_t off_min;
2735 u8 *sg_bufp, *sg_buf_end;
2736 struct binder_proc *target_proc = NULL;
2737 struct binder_thread *target_thread = NULL;
2738 struct binder_node *target_node = NULL;
2739 struct binder_transaction *in_reply_to = NULL;
2740 struct binder_transaction_log_entry *e;
2741 uint32_t return_error = 0;
2742 uint32_t return_error_param = 0;
2743 uint32_t return_error_line = 0;
2744 struct binder_buffer_object *last_fixup_obj = NULL;
2745 binder_size_t last_fixup_min_off = 0;
2746 struct binder_context *context = proc->context;
2747 int t_debug_id = atomic_inc_return(&binder_last_id);
2749 e = binder_transaction_log_add(&binder_transaction_log);
2750 e->debug_id = t_debug_id;
2751 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
2752 e->from_proc = proc->pid;
2753 e->from_thread = thread->pid;
2754 e->target_handle = tr->target.handle;
2755 e->data_size = tr->data_size;
2756 e->offsets_size = tr->offsets_size;
2757 e->context_name = proc->context->name;
2759 if (reply) {
2760 binder_inner_proc_lock(proc);
2761 in_reply_to = thread->transaction_stack;
2762 if (in_reply_to == NULL) {
2763 binder_inner_proc_unlock(proc);
2764 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
2765 proc->pid, thread->pid);
2766 return_error = BR_FAILED_REPLY;
2767 return_error_param = -EPROTO;
2768 return_error_line = __LINE__;
2769 goto err_empty_call_stack;
2771 if (in_reply_to->to_thread != thread) {
2772 spin_lock(&in_reply_to->lock);
2773 binder_user_error("%d:%d got reply transaction with bad transaction stack, transaction %d has target %d:%d\n",
2774 proc->pid, thread->pid, in_reply_to->debug_id,
2775 in_reply_to->to_proc ?
2776 in_reply_to->to_proc->pid : 0,
2777 in_reply_to->to_thread ?
2778 in_reply_to->to_thread->pid : 0);
2779 spin_unlock(&in_reply_to->lock);
2780 binder_inner_proc_unlock(proc);
2781 return_error = BR_FAILED_REPLY;
2782 return_error_param = -EPROTO;
2783 return_error_line = __LINE__;
2784 in_reply_to = NULL;
2785 goto err_bad_call_stack;
2787 thread->transaction_stack = in_reply_to->to_parent;
2788 binder_inner_proc_unlock(proc);
2789 binder_set_nice(in_reply_to->saved_priority);
2790 target_thread = binder_get_txn_from_and_acq_inner(in_reply_to);
2791 if (target_thread == NULL) {
2792 return_error = BR_DEAD_REPLY;
2793 return_error_line = __LINE__;
2794 goto err_dead_binder;
2796 if (target_thread->transaction_stack != in_reply_to) {
2797 binder_user_error("%d:%d got reply transaction with bad target transaction stack %d, expected %d\n",
2798 proc->pid, thread->pid,
2799 target_thread->transaction_stack ?
2800 target_thread->transaction_stack->debug_id : 0,
2801 in_reply_to->debug_id);
2802 binder_inner_proc_unlock(target_thread->proc);
2803 return_error = BR_FAILED_REPLY;
2804 return_error_param = -EPROTO;
2805 return_error_line = __LINE__;
2806 in_reply_to = NULL;
2807 target_thread = NULL;
2808 goto err_dead_binder;
2810 target_proc = target_thread->proc;
2811 target_proc->tmp_ref++;
2812 binder_inner_proc_unlock(target_thread->proc);
2813 } else {
2814 if (tr->target.handle) {
2815 struct binder_ref *ref;
2818 * There must already be a strong ref
2819 * on this node. If so, do a strong
2820 * increment on the node to ensure it
2821 * stays alive until the transaction is
2822 * done.
2824 binder_proc_lock(proc);
2825 ref = binder_get_ref_olocked(proc, tr->target.handle,
2826 true);
2827 if (ref) {
2828 target_node = binder_get_node_refs_for_txn(
2829 ref->node, &target_proc,
2830 &return_error);
2831 } else {
2832 binder_user_error("%d:%d got transaction to invalid handle\n",
2833 proc->pid, thread->pid);
2834 return_error = BR_FAILED_REPLY;
2836 binder_proc_unlock(proc);
2837 } else {
2838 mutex_lock(&context->context_mgr_node_lock);
2839 target_node = context->binder_context_mgr_node;
2840 if (target_node)
2841 target_node = binder_get_node_refs_for_txn(
2842 target_node, &target_proc,
2843 &return_error);
2844 else
2845 return_error = BR_DEAD_REPLY;
2846 mutex_unlock(&context->context_mgr_node_lock);
2847 if (target_node && target_proc->pid == proc->pid) {
2848 binder_user_error("%d:%d got transaction to context manager from process owning it\n",
2849 proc->pid, thread->pid);
2850 return_error = BR_FAILED_REPLY;
2851 return_error_param = -EINVAL;
2852 return_error_line = __LINE__;
2853 goto err_invalid_target_handle;
2856 if (!target_node) {
2858 * return_error is set above
2860 return_error_param = -EINVAL;
2861 return_error_line = __LINE__;
2862 goto err_dead_binder;
2864 e->to_node = target_node->debug_id;
2865 if (security_binder_transaction(proc->tsk,
2866 target_proc->tsk) < 0) {
2867 return_error = BR_FAILED_REPLY;
2868 return_error_param = -EPERM;
2869 return_error_line = __LINE__;
2870 goto err_invalid_target_handle;
2872 binder_inner_proc_lock(proc);
2874 w = list_first_entry_or_null(&thread->todo,
2875 struct binder_work, entry);
2876 if (!(tr->flags & TF_ONE_WAY) && w &&
2877 w->type == BINDER_WORK_TRANSACTION) {
2879 * Do not allow new outgoing transaction from a
2880 * thread that has a transaction at the head of
2881 * its todo list. Only need to check the head
2882 * because binder_select_thread_ilocked picks a
2883 * thread from proc->waiting_threads to enqueue
2884 * the transaction, and nothing is queued to the
2885 * todo list while the thread is on waiting_threads.
2887 binder_user_error("%d:%d new transaction not allowed when there is a transaction on thread todo\n",
2888 proc->pid, thread->pid);
2889 binder_inner_proc_unlock(proc);
2890 return_error = BR_FAILED_REPLY;
2891 return_error_param = -EPROTO;
2892 return_error_line = __LINE__;
2893 goto err_bad_todo_list;
2896 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
2897 struct binder_transaction *tmp;
2899 tmp = thread->transaction_stack;
2900 if (tmp->to_thread != thread) {
2901 spin_lock(&tmp->lock);
2902 binder_user_error("%d:%d got new transaction with bad transaction stack, transaction %d has target %d:%d\n",
2903 proc->pid, thread->pid, tmp->debug_id,
2904 tmp->to_proc ? tmp->to_proc->pid : 0,
2905 tmp->to_thread ?
2906 tmp->to_thread->pid : 0);
2907 spin_unlock(&tmp->lock);
2908 binder_inner_proc_unlock(proc);
2909 return_error = BR_FAILED_REPLY;
2910 return_error_param = -EPROTO;
2911 return_error_line = __LINE__;
2912 goto err_bad_call_stack;
2914 while (tmp) {
2915 struct binder_thread *from;
2917 spin_lock(&tmp->lock);
2918 from = tmp->from;
2919 if (from && from->proc == target_proc) {
2920 atomic_inc(&from->tmp_ref);
2921 target_thread = from;
2922 spin_unlock(&tmp->lock);
2923 break;
2925 spin_unlock(&tmp->lock);
2926 tmp = tmp->from_parent;
2929 binder_inner_proc_unlock(proc);
2931 if (target_thread)
2932 e->to_thread = target_thread->pid;
2933 e->to_proc = target_proc->pid;
2935 /* TODO: reuse incoming transaction for reply */
2936 t = kzalloc(sizeof(*t), GFP_KERNEL);
2937 if (t == NULL) {
2938 return_error = BR_FAILED_REPLY;
2939 return_error_param = -ENOMEM;
2940 return_error_line = __LINE__;
2941 goto err_alloc_t_failed;
2943 binder_stats_created(BINDER_STAT_TRANSACTION);
2944 spin_lock_init(&t->lock);
2946 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
2947 if (tcomplete == NULL) {
2948 return_error = BR_FAILED_REPLY;
2949 return_error_param = -ENOMEM;
2950 return_error_line = __LINE__;
2951 goto err_alloc_tcomplete_failed;
2953 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
2955 t->debug_id = t_debug_id;
2957 if (reply)
2958 binder_debug(BINDER_DEBUG_TRANSACTION,
2959 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
2960 proc->pid, thread->pid, t->debug_id,
2961 target_proc->pid, target_thread->pid,
2962 (u64)tr->data.ptr.buffer,
2963 (u64)tr->data.ptr.offsets,
2964 (u64)tr->data_size, (u64)tr->offsets_size,
2965 (u64)extra_buffers_size);
2966 else
2967 binder_debug(BINDER_DEBUG_TRANSACTION,
2968 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
2969 proc->pid, thread->pid, t->debug_id,
2970 target_proc->pid, target_node->debug_id,
2971 (u64)tr->data.ptr.buffer,
2972 (u64)tr->data.ptr.offsets,
2973 (u64)tr->data_size, (u64)tr->offsets_size,
2974 (u64)extra_buffers_size);
2976 if (!reply && !(tr->flags & TF_ONE_WAY))
2977 t->from = thread;
2978 else
2979 t->from = NULL;
2980 t->sender_euid = task_euid(proc->tsk);
2981 t->to_proc = target_proc;
2982 t->to_thread = target_thread;
2983 t->code = tr->code;
2984 t->flags = tr->flags;
2985 t->priority = task_nice(current);
2987 trace_binder_transaction(reply, t, target_node);
2989 t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size,
2990 tr->offsets_size, extra_buffers_size,
2991 !reply && (t->flags & TF_ONE_WAY));
2992 if (IS_ERR(t->buffer)) {
2994 * -ESRCH indicates VMA cleared. The target is dying.
2996 return_error_param = PTR_ERR(t->buffer);
2997 return_error = return_error_param == -ESRCH ?
2998 BR_DEAD_REPLY : BR_FAILED_REPLY;
2999 return_error_line = __LINE__;
3000 t->buffer = NULL;
3001 goto err_binder_alloc_buf_failed;
3003 t->buffer->debug_id = t->debug_id;
3004 t->buffer->transaction = t;
3005 t->buffer->target_node = target_node;
3006 trace_binder_transaction_alloc_buf(t->buffer);
3007 off_start = (binder_size_t *)(t->buffer->data +
3008 ALIGN(tr->data_size, sizeof(void *)));
3009 offp = off_start;
3011 if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t)
3012 tr->data.ptr.buffer, tr->data_size)) {
3013 binder_user_error("%d:%d got transaction with invalid data ptr\n",
3014 proc->pid, thread->pid);
3015 return_error = BR_FAILED_REPLY;
3016 return_error_param = -EFAULT;
3017 return_error_line = __LINE__;
3018 goto err_copy_data_failed;
3020 if (copy_from_user(offp, (const void __user *)(uintptr_t)
3021 tr->data.ptr.offsets, tr->offsets_size)) {
3022 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3023 proc->pid, thread->pid);
3024 return_error = BR_FAILED_REPLY;
3025 return_error_param = -EFAULT;
3026 return_error_line = __LINE__;
3027 goto err_copy_data_failed;
3029 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
3030 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
3031 proc->pid, thread->pid, (u64)tr->offsets_size);
3032 return_error = BR_FAILED_REPLY;
3033 return_error_param = -EINVAL;
3034 return_error_line = __LINE__;
3035 goto err_bad_offset;
3037 if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) {
3038 binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
3039 proc->pid, thread->pid,
3040 (u64)extra_buffers_size);
3041 return_error = BR_FAILED_REPLY;
3042 return_error_param = -EINVAL;
3043 return_error_line = __LINE__;
3044 goto err_bad_offset;
3046 off_end = (void *)off_start + tr->offsets_size;
3047 sg_bufp = (u8 *)(PTR_ALIGN(off_end, sizeof(void *)));
3048 sg_buf_end = sg_bufp + extra_buffers_size;
3049 off_min = 0;
3050 for (; offp < off_end; offp++) {
3051 struct binder_object_header *hdr;
3052 size_t object_size = binder_validate_object(t->buffer, *offp);
3054 if (object_size == 0 || *offp < off_min) {
3055 binder_user_error("%d:%d got transaction with invalid offset (%lld, min %lld max %lld) or object.\n",
3056 proc->pid, thread->pid, (u64)*offp,
3057 (u64)off_min,
3058 (u64)t->buffer->data_size);
3059 return_error = BR_FAILED_REPLY;
3060 return_error_param = -EINVAL;
3061 return_error_line = __LINE__;
3062 goto err_bad_offset;
3065 hdr = (struct binder_object_header *)(t->buffer->data + *offp);
3066 off_min = *offp + object_size;
3067 switch (hdr->type) {
3068 case BINDER_TYPE_BINDER:
3069 case BINDER_TYPE_WEAK_BINDER: {
3070 struct flat_binder_object *fp;
3072 fp = to_flat_binder_object(hdr);
3073 ret = binder_translate_binder(fp, t, thread);
3074 if (ret < 0) {
3075 return_error = BR_FAILED_REPLY;
3076 return_error_param = ret;
3077 return_error_line = __LINE__;
3078 goto err_translate_failed;
3080 } break;
3081 case BINDER_TYPE_HANDLE:
3082 case BINDER_TYPE_WEAK_HANDLE: {
3083 struct flat_binder_object *fp;
3085 fp = to_flat_binder_object(hdr);
3086 ret = binder_translate_handle(fp, t, thread);
3087 if (ret < 0) {
3088 return_error = BR_FAILED_REPLY;
3089 return_error_param = ret;
3090 return_error_line = __LINE__;
3091 goto err_translate_failed;
3093 } break;
3095 case BINDER_TYPE_FD: {
3096 struct binder_fd_object *fp = to_binder_fd_object(hdr);
3097 int target_fd = binder_translate_fd(fp->fd, t, thread,
3098 in_reply_to);
3100 if (target_fd < 0) {
3101 return_error = BR_FAILED_REPLY;
3102 return_error_param = target_fd;
3103 return_error_line = __LINE__;
3104 goto err_translate_failed;
3106 fp->pad_binder = 0;
3107 fp->fd = target_fd;
3108 } break;
3109 case BINDER_TYPE_FDA: {
3110 struct binder_fd_array_object *fda =
3111 to_binder_fd_array_object(hdr);
3112 struct binder_buffer_object *parent =
3113 binder_validate_ptr(t->buffer, fda->parent,
3114 off_start,
3115 offp - off_start);
3116 if (!parent) {
3117 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
3118 proc->pid, thread->pid);
3119 return_error = BR_FAILED_REPLY;
3120 return_error_param = -EINVAL;
3121 return_error_line = __LINE__;
3122 goto err_bad_parent;
3124 if (!binder_validate_fixup(t->buffer, off_start,
3125 parent, fda->parent_offset,
3126 last_fixup_obj,
3127 last_fixup_min_off)) {
3128 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
3129 proc->pid, thread->pid);
3130 return_error = BR_FAILED_REPLY;
3131 return_error_param = -EINVAL;
3132 return_error_line = __LINE__;
3133 goto err_bad_parent;
3135 ret = binder_translate_fd_array(fda, parent, t, thread,
3136 in_reply_to);
3137 if (ret < 0) {
3138 return_error = BR_FAILED_REPLY;
3139 return_error_param = ret;
3140 return_error_line = __LINE__;
3141 goto err_translate_failed;
3143 last_fixup_obj = parent;
3144 last_fixup_min_off =
3145 fda->parent_offset + sizeof(u32) * fda->num_fds;
3146 } break;
3147 case BINDER_TYPE_PTR: {
3148 struct binder_buffer_object *bp =
3149 to_binder_buffer_object(hdr);
3150 size_t buf_left = sg_buf_end - sg_bufp;
3152 if (bp->length > buf_left) {
3153 binder_user_error("%d:%d got transaction with too large buffer\n",
3154 proc->pid, thread->pid);
3155 return_error = BR_FAILED_REPLY;
3156 return_error_param = -EINVAL;
3157 return_error_line = __LINE__;
3158 goto err_bad_offset;
3160 if (copy_from_user(sg_bufp,
3161 (const void __user *)(uintptr_t)
3162 bp->buffer, bp->length)) {
3163 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3164 proc->pid, thread->pid);
3165 return_error_param = -EFAULT;
3166 return_error = BR_FAILED_REPLY;
3167 return_error_line = __LINE__;
3168 goto err_copy_data_failed;
3170 /* Fixup buffer pointer to target proc address space */
3171 bp->buffer = (uintptr_t)sg_bufp +
3172 binder_alloc_get_user_buffer_offset(
3173 &target_proc->alloc);
3174 sg_bufp += ALIGN(bp->length, sizeof(u64));
3176 ret = binder_fixup_parent(t, thread, bp, off_start,
3177 offp - off_start,
3178 last_fixup_obj,
3179 last_fixup_min_off);
3180 if (ret < 0) {
3181 return_error = BR_FAILED_REPLY;
3182 return_error_param = ret;
3183 return_error_line = __LINE__;
3184 goto err_translate_failed;
3186 last_fixup_obj = bp;
3187 last_fixup_min_off = 0;
3188 } break;
3189 default:
3190 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
3191 proc->pid, thread->pid, hdr->type);
3192 return_error = BR_FAILED_REPLY;
3193 return_error_param = -EINVAL;
3194 return_error_line = __LINE__;
3195 goto err_bad_object_type;
3198 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
3199 t->work.type = BINDER_WORK_TRANSACTION;
3201 if (reply) {
3202 binder_enqueue_thread_work(thread, tcomplete);
3203 binder_inner_proc_lock(target_proc);
3204 if (target_thread->is_dead) {
3205 binder_inner_proc_unlock(target_proc);
3206 goto err_dead_proc_or_thread;
3208 BUG_ON(t->buffer->async_transaction != 0);
3209 binder_pop_transaction_ilocked(target_thread, in_reply_to);
3210 binder_enqueue_thread_work_ilocked(target_thread, &t->work);
3211 binder_inner_proc_unlock(target_proc);
3212 wake_up_interruptible_sync(&target_thread->wait);
3213 binder_free_transaction(in_reply_to);
3214 } else if (!(t->flags & TF_ONE_WAY)) {
3215 BUG_ON(t->buffer->async_transaction != 0);
3216 binder_inner_proc_lock(proc);
3218 * Defer the TRANSACTION_COMPLETE, so we don't return to
3219 * userspace immediately; this allows the target process to
3220 * immediately start processing this transaction, reducing
3221 * latency. We will then return the TRANSACTION_COMPLETE when
3222 * the target replies (or there is an error).
3224 binder_enqueue_deferred_thread_work_ilocked(thread, tcomplete);
3225 t->need_reply = 1;
3226 t->from_parent = thread->transaction_stack;
3227 thread->transaction_stack = t;
3228 binder_inner_proc_unlock(proc);
3229 if (!binder_proc_transaction(t, target_proc, target_thread)) {
3230 binder_inner_proc_lock(proc);
3231 binder_pop_transaction_ilocked(thread, t);
3232 binder_inner_proc_unlock(proc);
3233 goto err_dead_proc_or_thread;
3235 } else {
3236 BUG_ON(target_node == NULL);
3237 BUG_ON(t->buffer->async_transaction != 1);
3238 binder_enqueue_thread_work(thread, tcomplete);
3239 if (!binder_proc_transaction(t, target_proc, NULL))
3240 goto err_dead_proc_or_thread;
3242 if (target_thread)
3243 binder_thread_dec_tmpref(target_thread);
3244 binder_proc_dec_tmpref(target_proc);
3245 if (target_node)
3246 binder_dec_node_tmpref(target_node);
3248 * write barrier to synchronize with initialization
3249 * of log entry
3251 smp_wmb();
3252 WRITE_ONCE(e->debug_id_done, t_debug_id);
3253 return;
3255 err_dead_proc_or_thread:
3256 return_error = BR_DEAD_REPLY;
3257 return_error_line = __LINE__;
3258 binder_dequeue_work(proc, tcomplete);
3259 err_translate_failed:
3260 err_bad_object_type:
3261 err_bad_offset:
3262 err_bad_parent:
3263 err_copy_data_failed:
3264 trace_binder_transaction_failed_buffer_release(t->buffer);
3265 binder_transaction_buffer_release(target_proc, t->buffer, offp);
3266 if (target_node)
3267 binder_dec_node_tmpref(target_node);
3268 target_node = NULL;
3269 t->buffer->transaction = NULL;
3270 binder_alloc_free_buf(&target_proc->alloc, t->buffer);
3271 err_binder_alloc_buf_failed:
3272 kfree(tcomplete);
3273 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3274 err_alloc_tcomplete_failed:
3275 kfree(t);
3276 binder_stats_deleted(BINDER_STAT_TRANSACTION);
3277 err_alloc_t_failed:
3278 err_bad_todo_list:
3279 err_bad_call_stack:
3280 err_empty_call_stack:
3281 err_dead_binder:
3282 err_invalid_target_handle:
3283 if (target_thread)
3284 binder_thread_dec_tmpref(target_thread);
3285 if (target_proc)
3286 binder_proc_dec_tmpref(target_proc);
3287 if (target_node) {
3288 binder_dec_node(target_node, 1, 0);
3289 binder_dec_node_tmpref(target_node);
3292 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
3293 "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
3294 proc->pid, thread->pid, return_error, return_error_param,
3295 (u64)tr->data_size, (u64)tr->offsets_size,
3296 return_error_line);
3299 struct binder_transaction_log_entry *fe;
3301 e->return_error = return_error;
3302 e->return_error_param = return_error_param;
3303 e->return_error_line = return_error_line;
3304 fe = binder_transaction_log_add(&binder_transaction_log_failed);
3305 *fe = *e;
3307 * write barrier to synchronize with initialization
3308 * of log entry
3310 smp_wmb();
3311 WRITE_ONCE(e->debug_id_done, t_debug_id);
3312 WRITE_ONCE(fe->debug_id_done, t_debug_id);
3315 BUG_ON(thread->return_error.cmd != BR_OK);
3316 if (in_reply_to) {
3317 thread->return_error.cmd = BR_TRANSACTION_COMPLETE;
3318 binder_enqueue_thread_work(thread, &thread->return_error.work);
3319 binder_send_failed_reply(in_reply_to, return_error);
3320 } else {
3321 thread->return_error.cmd = return_error;
3322 binder_enqueue_thread_work(thread, &thread->return_error.work);
3326 static int binder_thread_write(struct binder_proc *proc,
3327 struct binder_thread *thread,
3328 binder_uintptr_t binder_buffer, size_t size,
3329 binder_size_t *consumed)
3331 uint32_t cmd;
3332 struct binder_context *context = proc->context;
3333 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
3334 void __user *ptr = buffer + *consumed;
3335 void __user *end = buffer + size;
3337 while (ptr < end && thread->return_error.cmd == BR_OK) {
3338 int ret;
3340 if (get_user(cmd, (uint32_t __user *)ptr))
3341 return -EFAULT;
3342 ptr += sizeof(uint32_t);
3343 trace_binder_command(cmd);
3344 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
3345 atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]);
3346 atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]);
3347 atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]);
3349 switch (cmd) {
3350 case BC_INCREFS:
3351 case BC_ACQUIRE:
3352 case BC_RELEASE:
3353 case BC_DECREFS: {
3354 uint32_t target;
3355 const char *debug_string;
3356 bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE;
3357 bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE;
3358 struct binder_ref_data rdata;
3360 if (get_user(target, (uint32_t __user *)ptr))
3361 return -EFAULT;
3363 ptr += sizeof(uint32_t);
3364 ret = -1;
3365 if (increment && !target) {
3366 struct binder_node *ctx_mgr_node;
3367 mutex_lock(&context->context_mgr_node_lock);
3368 ctx_mgr_node = context->binder_context_mgr_node;
3369 if (ctx_mgr_node)
3370 ret = binder_inc_ref_for_node(
3371 proc, ctx_mgr_node,
3372 strong, NULL, &rdata);
3373 mutex_unlock(&context->context_mgr_node_lock);
3375 if (ret)
3376 ret = binder_update_ref_for_handle(
3377 proc, target, increment, strong,
3378 &rdata);
3379 if (!ret && rdata.desc != target) {
3380 binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
3381 proc->pid, thread->pid,
3382 target, rdata.desc);
3384 switch (cmd) {
3385 case BC_INCREFS:
3386 debug_string = "IncRefs";
3387 break;
3388 case BC_ACQUIRE:
3389 debug_string = "Acquire";
3390 break;
3391 case BC_RELEASE:
3392 debug_string = "Release";
3393 break;
3394 case BC_DECREFS:
3395 default:
3396 debug_string = "DecRefs";
3397 break;
3399 if (ret) {
3400 binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
3401 proc->pid, thread->pid, debug_string,
3402 strong, target, ret);
3403 break;
3405 binder_debug(BINDER_DEBUG_USER_REFS,
3406 "%d:%d %s ref %d desc %d s %d w %d\n",
3407 proc->pid, thread->pid, debug_string,
3408 rdata.debug_id, rdata.desc, rdata.strong,
3409 rdata.weak);
3410 break;
3412 case BC_INCREFS_DONE:
3413 case BC_ACQUIRE_DONE: {
3414 binder_uintptr_t node_ptr;
3415 binder_uintptr_t cookie;
3416 struct binder_node *node;
3417 bool free_node;
3419 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
3420 return -EFAULT;
3421 ptr += sizeof(binder_uintptr_t);
3422 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
3423 return -EFAULT;
3424 ptr += sizeof(binder_uintptr_t);
3425 node = binder_get_node(proc, node_ptr);
3426 if (node == NULL) {
3427 binder_user_error("%d:%d %s u%016llx no match\n",
3428 proc->pid, thread->pid,
3429 cmd == BC_INCREFS_DONE ?
3430 "BC_INCREFS_DONE" :
3431 "BC_ACQUIRE_DONE",
3432 (u64)node_ptr);
3433 break;
3435 if (cookie != node->cookie) {
3436 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
3437 proc->pid, thread->pid,
3438 cmd == BC_INCREFS_DONE ?
3439 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
3440 (u64)node_ptr, node->debug_id,
3441 (u64)cookie, (u64)node->cookie);
3442 binder_put_node(node);
3443 break;
3445 binder_node_inner_lock(node);
3446 if (cmd == BC_ACQUIRE_DONE) {
3447 if (node->pending_strong_ref == 0) {
3448 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
3449 proc->pid, thread->pid,
3450 node->debug_id);
3451 binder_node_inner_unlock(node);
3452 binder_put_node(node);
3453 break;
3455 node->pending_strong_ref = 0;
3456 } else {
3457 if (node->pending_weak_ref == 0) {
3458 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
3459 proc->pid, thread->pid,
3460 node->debug_id);
3461 binder_node_inner_unlock(node);
3462 binder_put_node(node);
3463 break;
3465 node->pending_weak_ref = 0;
3467 free_node = binder_dec_node_nilocked(node,
3468 cmd == BC_ACQUIRE_DONE, 0);
3469 WARN_ON(free_node);
3470 binder_debug(BINDER_DEBUG_USER_REFS,
3471 "%d:%d %s node %d ls %d lw %d tr %d\n",
3472 proc->pid, thread->pid,
3473 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
3474 node->debug_id, node->local_strong_refs,
3475 node->local_weak_refs, node->tmp_refs);
3476 binder_node_inner_unlock(node);
3477 binder_put_node(node);
3478 break;
3480 case BC_ATTEMPT_ACQUIRE:
3481 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
3482 return -EINVAL;
3483 case BC_ACQUIRE_RESULT:
3484 pr_err("BC_ACQUIRE_RESULT not supported\n");
3485 return -EINVAL;
3487 case BC_FREE_BUFFER: {
3488 binder_uintptr_t data_ptr;
3489 struct binder_buffer *buffer;
3491 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
3492 return -EFAULT;
3493 ptr += sizeof(binder_uintptr_t);
3495 buffer = binder_alloc_prepare_to_free(&proc->alloc,
3496 data_ptr);
3497 if (IS_ERR_OR_NULL(buffer)) {
3498 if (PTR_ERR(buffer) == -EPERM) {
3499 binder_user_error(
3500 "%d:%d BC_FREE_BUFFER u%016llx matched unreturned or currently freeing buffer\n",
3501 proc->pid, thread->pid,
3502 (u64)data_ptr);
3503 } else {
3504 binder_user_error(
3505 "%d:%d BC_FREE_BUFFER u%016llx no match\n",
3506 proc->pid, thread->pid,
3507 (u64)data_ptr);
3509 break;
3511 binder_debug(BINDER_DEBUG_FREE_BUFFER,
3512 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
3513 proc->pid, thread->pid, (u64)data_ptr,
3514 buffer->debug_id,
3515 buffer->transaction ? "active" : "finished");
3517 binder_inner_proc_lock(proc);
3518 if (buffer->transaction) {
3519 buffer->transaction->buffer = NULL;
3520 buffer->transaction = NULL;
3522 binder_inner_proc_unlock(proc);
3523 if (buffer->async_transaction && buffer->target_node) {
3524 struct binder_node *buf_node;
3525 struct binder_work *w;
3527 buf_node = buffer->target_node;
3528 binder_node_inner_lock(buf_node);
3529 BUG_ON(!buf_node->has_async_transaction);
3530 BUG_ON(buf_node->proc != proc);
3531 w = binder_dequeue_work_head_ilocked(
3532 &buf_node->async_todo);
3533 if (!w) {
3534 buf_node->has_async_transaction = false;
3535 } else {
3536 binder_enqueue_work_ilocked(
3537 w, &proc->todo);
3538 binder_wakeup_proc_ilocked(proc);
3540 binder_node_inner_unlock(buf_node);
3542 trace_binder_transaction_buffer_release(buffer);
3543 binder_transaction_buffer_release(proc, buffer, NULL);
3544 binder_alloc_free_buf(&proc->alloc, buffer);
3545 break;
3548 case BC_TRANSACTION_SG:
3549 case BC_REPLY_SG: {
3550 struct binder_transaction_data_sg tr;
3552 if (copy_from_user(&tr, ptr, sizeof(tr)))
3553 return -EFAULT;
3554 ptr += sizeof(tr);
3555 binder_transaction(proc, thread, &tr.transaction_data,
3556 cmd == BC_REPLY_SG, tr.buffers_size);
3557 break;
3559 case BC_TRANSACTION:
3560 case BC_REPLY: {
3561 struct binder_transaction_data tr;
3563 if (copy_from_user(&tr, ptr, sizeof(tr)))
3564 return -EFAULT;
3565 ptr += sizeof(tr);
3566 binder_transaction(proc, thread, &tr,
3567 cmd == BC_REPLY, 0);
3568 break;
3571 case BC_REGISTER_LOOPER:
3572 binder_debug(BINDER_DEBUG_THREADS,
3573 "%d:%d BC_REGISTER_LOOPER\n",
3574 proc->pid, thread->pid);
3575 binder_inner_proc_lock(proc);
3576 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
3577 thread->looper |= BINDER_LOOPER_STATE_INVALID;
3578 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
3579 proc->pid, thread->pid);
3580 } else if (proc->requested_threads == 0) {
3581 thread->looper |= BINDER_LOOPER_STATE_INVALID;
3582 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
3583 proc->pid, thread->pid);
3584 } else {
3585 proc->requested_threads--;
3586 proc->requested_threads_started++;
3588 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
3589 binder_inner_proc_unlock(proc);
3590 break;
3591 case BC_ENTER_LOOPER:
3592 binder_debug(BINDER_DEBUG_THREADS,
3593 "%d:%d BC_ENTER_LOOPER\n",
3594 proc->pid, thread->pid);
3595 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
3596 thread->looper |= BINDER_LOOPER_STATE_INVALID;
3597 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
3598 proc->pid, thread->pid);
3600 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
3601 break;
3602 case BC_EXIT_LOOPER:
3603 binder_debug(BINDER_DEBUG_THREADS,
3604 "%d:%d BC_EXIT_LOOPER\n",
3605 proc->pid, thread->pid);
3606 thread->looper |= BINDER_LOOPER_STATE_EXITED;
3607 break;
3609 case BC_REQUEST_DEATH_NOTIFICATION:
3610 case BC_CLEAR_DEATH_NOTIFICATION: {
3611 uint32_t target;
3612 binder_uintptr_t cookie;
3613 struct binder_ref *ref;
3614 struct binder_ref_death *death = NULL;
3616 if (get_user(target, (uint32_t __user *)ptr))
3617 return -EFAULT;
3618 ptr += sizeof(uint32_t);
3619 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
3620 return -EFAULT;
3621 ptr += sizeof(binder_uintptr_t);
3622 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3624 * Allocate memory for death notification
3625 * before taking lock
3627 death = kzalloc(sizeof(*death), GFP_KERNEL);
3628 if (death == NULL) {
3629 WARN_ON(thread->return_error.cmd !=
3630 BR_OK);
3631 thread->return_error.cmd = BR_ERROR;
3632 binder_enqueue_thread_work(
3633 thread,
3634 &thread->return_error.work);
3635 binder_debug(
3636 BINDER_DEBUG_FAILED_TRANSACTION,
3637 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
3638 proc->pid, thread->pid);
3639 break;
3642 binder_proc_lock(proc);
3643 ref = binder_get_ref_olocked(proc, target, false);
3644 if (ref == NULL) {
3645 binder_user_error("%d:%d %s invalid ref %d\n",
3646 proc->pid, thread->pid,
3647 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3648 "BC_REQUEST_DEATH_NOTIFICATION" :
3649 "BC_CLEAR_DEATH_NOTIFICATION",
3650 target);
3651 binder_proc_unlock(proc);
3652 kfree(death);
3653 break;
3656 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
3657 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
3658 proc->pid, thread->pid,
3659 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3660 "BC_REQUEST_DEATH_NOTIFICATION" :
3661 "BC_CLEAR_DEATH_NOTIFICATION",
3662 (u64)cookie, ref->data.debug_id,
3663 ref->data.desc, ref->data.strong,
3664 ref->data.weak, ref->node->debug_id);
3666 binder_node_lock(ref->node);
3667 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3668 if (ref->death) {
3669 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
3670 proc->pid, thread->pid);
3671 binder_node_unlock(ref->node);
3672 binder_proc_unlock(proc);
3673 kfree(death);
3674 break;
3676 binder_stats_created(BINDER_STAT_DEATH);
3677 INIT_LIST_HEAD(&death->work.entry);
3678 death->cookie = cookie;
3679 ref->death = death;
3680 if (ref->node->proc == NULL) {
3681 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
3683 binder_inner_proc_lock(proc);
3684 binder_enqueue_work_ilocked(
3685 &ref->death->work, &proc->todo);
3686 binder_wakeup_proc_ilocked(proc);
3687 binder_inner_proc_unlock(proc);
3689 } else {
3690 if (ref->death == NULL) {
3691 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
3692 proc->pid, thread->pid);
3693 binder_node_unlock(ref->node);
3694 binder_proc_unlock(proc);
3695 break;
3697 death = ref->death;
3698 if (death->cookie != cookie) {
3699 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
3700 proc->pid, thread->pid,
3701 (u64)death->cookie,
3702 (u64)cookie);
3703 binder_node_unlock(ref->node);
3704 binder_proc_unlock(proc);
3705 break;
3707 ref->death = NULL;
3708 binder_inner_proc_lock(proc);
3709 if (list_empty(&death->work.entry)) {
3710 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
3711 if (thread->looper &
3712 (BINDER_LOOPER_STATE_REGISTERED |
3713 BINDER_LOOPER_STATE_ENTERED))
3714 binder_enqueue_thread_work_ilocked(
3715 thread,
3716 &death->work);
3717 else {
3718 binder_enqueue_work_ilocked(
3719 &death->work,
3720 &proc->todo);
3721 binder_wakeup_proc_ilocked(
3722 proc);
3724 } else {
3725 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
3726 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
3728 binder_inner_proc_unlock(proc);
3730 binder_node_unlock(ref->node);
3731 binder_proc_unlock(proc);
3732 } break;
3733 case BC_DEAD_BINDER_DONE: {
3734 struct binder_work *w;
3735 binder_uintptr_t cookie;
3736 struct binder_ref_death *death = NULL;
3738 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
3739 return -EFAULT;
3741 ptr += sizeof(cookie);
3742 binder_inner_proc_lock(proc);
3743 list_for_each_entry(w, &proc->delivered_death,
3744 entry) {
3745 struct binder_ref_death *tmp_death =
3746 container_of(w,
3747 struct binder_ref_death,
3748 work);
3750 if (tmp_death->cookie == cookie) {
3751 death = tmp_death;
3752 break;
3755 binder_debug(BINDER_DEBUG_DEAD_BINDER,
3756 "%d:%d BC_DEAD_BINDER_DONE %016llx found %pK\n",
3757 proc->pid, thread->pid, (u64)cookie,
3758 death);
3759 if (death == NULL) {
3760 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
3761 proc->pid, thread->pid, (u64)cookie);
3762 binder_inner_proc_unlock(proc);
3763 break;
3765 binder_dequeue_work_ilocked(&death->work);
3766 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
3767 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
3768 if (thread->looper &
3769 (BINDER_LOOPER_STATE_REGISTERED |
3770 BINDER_LOOPER_STATE_ENTERED))
3771 binder_enqueue_thread_work_ilocked(
3772 thread, &death->work);
3773 else {
3774 binder_enqueue_work_ilocked(
3775 &death->work,
3776 &proc->todo);
3777 binder_wakeup_proc_ilocked(proc);
3780 binder_inner_proc_unlock(proc);
3781 } break;
3783 default:
3784 pr_err("%d:%d unknown command %d\n",
3785 proc->pid, thread->pid, cmd);
3786 return -EINVAL;
3788 *consumed = ptr - buffer;
3790 return 0;
3793 static void binder_stat_br(struct binder_proc *proc,
3794 struct binder_thread *thread, uint32_t cmd)
3796 trace_binder_return(cmd);
3797 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
3798 atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
3799 atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
3800 atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
3804 static int binder_put_node_cmd(struct binder_proc *proc,
3805 struct binder_thread *thread,
3806 void __user **ptrp,
3807 binder_uintptr_t node_ptr,
3808 binder_uintptr_t node_cookie,
3809 int node_debug_id,
3810 uint32_t cmd, const char *cmd_name)
3812 void __user *ptr = *ptrp;
3814 if (put_user(cmd, (uint32_t __user *)ptr))
3815 return -EFAULT;
3816 ptr += sizeof(uint32_t);
3818 if (put_user(node_ptr, (binder_uintptr_t __user *)ptr))
3819 return -EFAULT;
3820 ptr += sizeof(binder_uintptr_t);
3822 if (put_user(node_cookie, (binder_uintptr_t __user *)ptr))
3823 return -EFAULT;
3824 ptr += sizeof(binder_uintptr_t);
3826 binder_stat_br(proc, thread, cmd);
3827 binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n",
3828 proc->pid, thread->pid, cmd_name, node_debug_id,
3829 (u64)node_ptr, (u64)node_cookie);
3831 *ptrp = ptr;
3832 return 0;
3835 static int binder_wait_for_work(struct binder_thread *thread,
3836 bool do_proc_work)
3838 DEFINE_WAIT(wait);
3839 struct binder_proc *proc = thread->proc;
3840 int ret = 0;
3842 freezer_do_not_count();
3843 binder_inner_proc_lock(proc);
3844 for (;;) {
3845 prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE);
3846 if (binder_has_work_ilocked(thread, do_proc_work))
3847 break;
3848 if (do_proc_work)
3849 list_add(&thread->waiting_thread_node,
3850 &proc->waiting_threads);
3851 binder_inner_proc_unlock(proc);
3852 schedule();
3853 binder_inner_proc_lock(proc);
3854 list_del_init(&thread->waiting_thread_node);
3855 if (signal_pending(current)) {
3856 ret = -ERESTARTSYS;
3857 break;
3860 finish_wait(&thread->wait, &wait);
3861 binder_inner_proc_unlock(proc);
3862 freezer_count();
3864 return ret;
3867 static int binder_thread_read(struct binder_proc *proc,
3868 struct binder_thread *thread,
3869 binder_uintptr_t binder_buffer, size_t size,
3870 binder_size_t *consumed, int non_block)
3872 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
3873 void __user *ptr = buffer + *consumed;
3874 void __user *end = buffer + size;
3876 int ret = 0;
3877 int wait_for_proc_work;
3879 if (*consumed == 0) {
3880 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
3881 return -EFAULT;
3882 ptr += sizeof(uint32_t);
3885 retry:
3886 binder_inner_proc_lock(proc);
3887 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
3888 binder_inner_proc_unlock(proc);
3890 thread->looper |= BINDER_LOOPER_STATE_WAITING;
3892 trace_binder_wait_for_work(wait_for_proc_work,
3893 !!thread->transaction_stack,
3894 !binder_worklist_empty(proc, &thread->todo));
3895 if (wait_for_proc_work) {
3896 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
3897 BINDER_LOOPER_STATE_ENTERED))) {
3898 binder_user_error("%d:%d ERROR: Thread waiting for process work before calling BC_REGISTER_LOOPER or BC_ENTER_LOOPER (state %x)\n",
3899 proc->pid, thread->pid, thread->looper);
3900 wait_event_interruptible(binder_user_error_wait,
3901 binder_stop_on_user_error < 2);
3903 binder_set_nice(proc->default_priority);
3906 if (non_block) {
3907 if (!binder_has_work(thread, wait_for_proc_work))
3908 ret = -EAGAIN;
3909 } else {
3910 ret = binder_wait_for_work(thread, wait_for_proc_work);
3913 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
3915 if (ret)
3916 return ret;
3918 while (1) {
3919 uint32_t cmd;
3920 struct binder_transaction_data tr;
3921 struct binder_work *w = NULL;
3922 struct list_head *list = NULL;
3923 struct binder_transaction *t = NULL;
3924 struct binder_thread *t_from;
3926 binder_inner_proc_lock(proc);
3927 if (!binder_worklist_empty_ilocked(&thread->todo))
3928 list = &thread->todo;
3929 else if (!binder_worklist_empty_ilocked(&proc->todo) &&
3930 wait_for_proc_work)
3931 list = &proc->todo;
3932 else {
3933 binder_inner_proc_unlock(proc);
3935 /* no data added */
3936 if (ptr - buffer == 4 && !thread->looper_need_return)
3937 goto retry;
3938 break;
3941 if (end - ptr < sizeof(tr) + 4) {
3942 binder_inner_proc_unlock(proc);
3943 break;
3945 w = binder_dequeue_work_head_ilocked(list);
3946 if (binder_worklist_empty_ilocked(&thread->todo))
3947 thread->process_todo = false;
3949 switch (w->type) {
3950 case BINDER_WORK_TRANSACTION: {
3951 binder_inner_proc_unlock(proc);
3952 t = container_of(w, struct binder_transaction, work);
3953 } break;
3954 case BINDER_WORK_RETURN_ERROR: {
3955 struct binder_error *e = container_of(
3956 w, struct binder_error, work);
3958 WARN_ON(e->cmd == BR_OK);
3959 binder_inner_proc_unlock(proc);
3960 if (put_user(e->cmd, (uint32_t __user *)ptr))
3961 return -EFAULT;
3962 cmd = e->cmd;
3963 e->cmd = BR_OK;
3964 ptr += sizeof(uint32_t);
3966 binder_stat_br(proc, thread, cmd);
3967 } break;
3968 case BINDER_WORK_TRANSACTION_COMPLETE: {
3969 binder_inner_proc_unlock(proc);
3970 cmd = BR_TRANSACTION_COMPLETE;
3971 kfree(w);
3972 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3973 if (put_user(cmd, (uint32_t __user *)ptr))
3974 return -EFAULT;
3975 ptr += sizeof(uint32_t);
3977 binder_stat_br(proc, thread, cmd);
3978 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
3979 "%d:%d BR_TRANSACTION_COMPLETE\n",
3980 proc->pid, thread->pid);
3981 } break;
3982 case BINDER_WORK_NODE: {
3983 struct binder_node *node = container_of(w, struct binder_node, work);
3984 int strong, weak;
3985 binder_uintptr_t node_ptr = node->ptr;
3986 binder_uintptr_t node_cookie = node->cookie;
3987 int node_debug_id = node->debug_id;
3988 int has_weak_ref;
3989 int has_strong_ref;
3990 void __user *orig_ptr = ptr;
3992 BUG_ON(proc != node->proc);
3993 strong = node->internal_strong_refs ||
3994 node->local_strong_refs;
3995 weak = !hlist_empty(&node->refs) ||
3996 node->local_weak_refs ||
3997 node->tmp_refs || strong;
3998 has_strong_ref = node->has_strong_ref;
3999 has_weak_ref = node->has_weak_ref;
4001 if (weak && !has_weak_ref) {
4002 node->has_weak_ref = 1;
4003 node->pending_weak_ref = 1;
4004 node->local_weak_refs++;
4006 if (strong && !has_strong_ref) {
4007 node->has_strong_ref = 1;
4008 node->pending_strong_ref = 1;
4009 node->local_strong_refs++;
4011 if (!strong && has_strong_ref)
4012 node->has_strong_ref = 0;
4013 if (!weak && has_weak_ref)
4014 node->has_weak_ref = 0;
4015 if (!weak && !strong) {
4016 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4017 "%d:%d node %d u%016llx c%016llx deleted\n",
4018 proc->pid, thread->pid,
4019 node_debug_id,
4020 (u64)node_ptr,
4021 (u64)node_cookie);
4022 rb_erase(&node->rb_node, &proc->nodes);
4023 binder_inner_proc_unlock(proc);
4024 binder_node_lock(node);
4026 * Acquire the node lock before freeing the
4027 * node to serialize with other threads that
4028 * may have been holding the node lock while
4029 * decrementing this node (avoids race where
4030 * this thread frees while the other thread
4031 * is unlocking the node after the final
4032 * decrement)
4034 binder_node_unlock(node);
4035 binder_free_node(node);
4036 } else
4037 binder_inner_proc_unlock(proc);
4039 if (weak && !has_weak_ref)
4040 ret = binder_put_node_cmd(
4041 proc, thread, &ptr, node_ptr,
4042 node_cookie, node_debug_id,
4043 BR_INCREFS, "BR_INCREFS");
4044 if (!ret && strong && !has_strong_ref)
4045 ret = binder_put_node_cmd(
4046 proc, thread, &ptr, node_ptr,
4047 node_cookie, node_debug_id,
4048 BR_ACQUIRE, "BR_ACQUIRE");
4049 if (!ret && !strong && has_strong_ref)
4050 ret = binder_put_node_cmd(
4051 proc, thread, &ptr, node_ptr,
4052 node_cookie, node_debug_id,
4053 BR_RELEASE, "BR_RELEASE");
4054 if (!ret && !weak && has_weak_ref)
4055 ret = binder_put_node_cmd(
4056 proc, thread, &ptr, node_ptr,
4057 node_cookie, node_debug_id,
4058 BR_DECREFS, "BR_DECREFS");
4059 if (orig_ptr == ptr)
4060 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4061 "%d:%d node %d u%016llx c%016llx state unchanged\n",
4062 proc->pid, thread->pid,
4063 node_debug_id,
4064 (u64)node_ptr,
4065 (u64)node_cookie);
4066 if (ret)
4067 return ret;
4068 } break;
4069 case BINDER_WORK_DEAD_BINDER:
4070 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4071 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4072 struct binder_ref_death *death;
4073 uint32_t cmd;
4074 binder_uintptr_t cookie;
4076 death = container_of(w, struct binder_ref_death, work);
4077 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
4078 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
4079 else
4080 cmd = BR_DEAD_BINDER;
4081 cookie = death->cookie;
4083 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
4084 "%d:%d %s %016llx\n",
4085 proc->pid, thread->pid,
4086 cmd == BR_DEAD_BINDER ?
4087 "BR_DEAD_BINDER" :
4088 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
4089 (u64)cookie);
4090 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
4091 binder_inner_proc_unlock(proc);
4092 kfree(death);
4093 binder_stats_deleted(BINDER_STAT_DEATH);
4094 } else {
4095 binder_enqueue_work_ilocked(
4096 w, &proc->delivered_death);
4097 binder_inner_proc_unlock(proc);
4099 if (put_user(cmd, (uint32_t __user *)ptr))
4100 return -EFAULT;
4101 ptr += sizeof(uint32_t);
4102 if (put_user(cookie,
4103 (binder_uintptr_t __user *)ptr))
4104 return -EFAULT;
4105 ptr += sizeof(binder_uintptr_t);
4106 binder_stat_br(proc, thread, cmd);
4107 if (cmd == BR_DEAD_BINDER)
4108 goto done; /* DEAD_BINDER notifications can cause transactions */
4109 } break;
4112 if (!t)
4113 continue;
4115 BUG_ON(t->buffer == NULL);
4116 if (t->buffer->target_node) {
4117 struct binder_node *target_node = t->buffer->target_node;
4119 tr.target.ptr = target_node->ptr;
4120 tr.cookie = target_node->cookie;
4121 t->saved_priority = task_nice(current);
4122 if (t->priority < target_node->min_priority &&
4123 !(t->flags & TF_ONE_WAY))
4124 binder_set_nice(t->priority);
4125 else if (!(t->flags & TF_ONE_WAY) ||
4126 t->saved_priority > target_node->min_priority)
4127 binder_set_nice(target_node->min_priority);
4128 cmd = BR_TRANSACTION;
4129 } else {
4130 tr.target.ptr = 0;
4131 tr.cookie = 0;
4132 cmd = BR_REPLY;
4134 tr.code = t->code;
4135 tr.flags = t->flags;
4136 tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
4138 t_from = binder_get_txn_from(t);
4139 if (t_from) {
4140 struct task_struct *sender = t_from->proc->tsk;
4142 tr.sender_pid = task_tgid_nr_ns(sender,
4143 task_active_pid_ns(current));
4144 } else {
4145 tr.sender_pid = 0;
4148 tr.data_size = t->buffer->data_size;
4149 tr.offsets_size = t->buffer->offsets_size;
4150 tr.data.ptr.buffer = (binder_uintptr_t)
4151 ((uintptr_t)t->buffer->data +
4152 binder_alloc_get_user_buffer_offset(&proc->alloc));
4153 tr.data.ptr.offsets = tr.data.ptr.buffer +
4154 ALIGN(t->buffer->data_size,
4155 sizeof(void *));
4157 if (put_user(cmd, (uint32_t __user *)ptr)) {
4158 if (t_from)
4159 binder_thread_dec_tmpref(t_from);
4161 binder_cleanup_transaction(t, "put_user failed",
4162 BR_FAILED_REPLY);
4164 return -EFAULT;
4166 ptr += sizeof(uint32_t);
4167 if (copy_to_user(ptr, &tr, sizeof(tr))) {
4168 if (t_from)
4169 binder_thread_dec_tmpref(t_from);
4171 binder_cleanup_transaction(t, "copy_to_user failed",
4172 BR_FAILED_REPLY);
4174 return -EFAULT;
4176 ptr += sizeof(tr);
4178 trace_binder_transaction_received(t);
4179 binder_stat_br(proc, thread, cmd);
4180 binder_debug(BINDER_DEBUG_TRANSACTION,
4181 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
4182 proc->pid, thread->pid,
4183 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
4184 "BR_REPLY",
4185 t->debug_id, t_from ? t_from->proc->pid : 0,
4186 t_from ? t_from->pid : 0, cmd,
4187 t->buffer->data_size, t->buffer->offsets_size,
4188 (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
4190 if (t_from)
4191 binder_thread_dec_tmpref(t_from);
4192 t->buffer->allow_user_free = 1;
4193 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
4194 binder_inner_proc_lock(thread->proc);
4195 t->to_parent = thread->transaction_stack;
4196 t->to_thread = thread;
4197 thread->transaction_stack = t;
4198 binder_inner_proc_unlock(thread->proc);
4199 } else {
4200 binder_free_transaction(t);
4202 break;
4205 done:
4207 *consumed = ptr - buffer;
4208 binder_inner_proc_lock(proc);
4209 if (proc->requested_threads == 0 &&
4210 list_empty(&thread->proc->waiting_threads) &&
4211 proc->requested_threads_started < proc->max_threads &&
4212 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4213 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
4214 /*spawn a new thread if we leave this out */) {
4215 proc->requested_threads++;
4216 binder_inner_proc_unlock(proc);
4217 binder_debug(BINDER_DEBUG_THREADS,
4218 "%d:%d BR_SPAWN_LOOPER\n",
4219 proc->pid, thread->pid);
4220 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
4221 return -EFAULT;
4222 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
4223 } else
4224 binder_inner_proc_unlock(proc);
4225 return 0;
4228 static void binder_release_work(struct binder_proc *proc,
4229 struct list_head *list)
4231 struct binder_work *w;
4233 while (1) {
4234 w = binder_dequeue_work_head(proc, list);
4235 if (!w)
4236 return;
4238 switch (w->type) {
4239 case BINDER_WORK_TRANSACTION: {
4240 struct binder_transaction *t;
4242 t = container_of(w, struct binder_transaction, work);
4244 binder_cleanup_transaction(t, "process died.",
4245 BR_DEAD_REPLY);
4246 } break;
4247 case BINDER_WORK_RETURN_ERROR: {
4248 struct binder_error *e = container_of(
4249 w, struct binder_error, work);
4251 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4252 "undelivered TRANSACTION_ERROR: %u\n",
4253 e->cmd);
4254 } break;
4255 case BINDER_WORK_TRANSACTION_COMPLETE: {
4256 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4257 "undelivered TRANSACTION_COMPLETE\n");
4258 kfree(w);
4259 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4260 } break;
4261 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4262 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4263 struct binder_ref_death *death;
4265 death = container_of(w, struct binder_ref_death, work);
4266 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4267 "undelivered death notification, %016llx\n",
4268 (u64)death->cookie);
4269 kfree(death);
4270 binder_stats_deleted(BINDER_STAT_DEATH);
4271 } break;
4272 default:
4273 pr_err("unexpected work type, %d, not freed\n",
4274 w->type);
4275 break;
4281 static struct binder_thread *binder_get_thread_ilocked(
4282 struct binder_proc *proc, struct binder_thread *new_thread)
4284 struct binder_thread *thread = NULL;
4285 struct rb_node *parent = NULL;
4286 struct rb_node **p = &proc->threads.rb_node;
4288 while (*p) {
4289 parent = *p;
4290 thread = rb_entry(parent, struct binder_thread, rb_node);
4292 if (current->pid < thread->pid)
4293 p = &(*p)->rb_left;
4294 else if (current->pid > thread->pid)
4295 p = &(*p)->rb_right;
4296 else
4297 return thread;
4299 if (!new_thread)
4300 return NULL;
4301 thread = new_thread;
4302 binder_stats_created(BINDER_STAT_THREAD);
4303 thread->proc = proc;
4304 thread->pid = current->pid;
4305 atomic_set(&thread->tmp_ref, 0);
4306 init_waitqueue_head(&thread->wait);
4307 INIT_LIST_HEAD(&thread->todo);
4308 rb_link_node(&thread->rb_node, parent, p);
4309 rb_insert_color(&thread->rb_node, &proc->threads);
4310 thread->looper_need_return = true;
4311 thread->return_error.work.type = BINDER_WORK_RETURN_ERROR;
4312 thread->return_error.cmd = BR_OK;
4313 thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR;
4314 thread->reply_error.cmd = BR_OK;
4315 INIT_LIST_HEAD(&new_thread->waiting_thread_node);
4316 return thread;
4319 static struct binder_thread *binder_get_thread(struct binder_proc *proc)
4321 struct binder_thread *thread;
4322 struct binder_thread *new_thread;
4324 binder_inner_proc_lock(proc);
4325 thread = binder_get_thread_ilocked(proc, NULL);
4326 binder_inner_proc_unlock(proc);
4327 if (!thread) {
4328 new_thread = kzalloc(sizeof(*thread), GFP_KERNEL);
4329 if (new_thread == NULL)
4330 return NULL;
4331 binder_inner_proc_lock(proc);
4332 thread = binder_get_thread_ilocked(proc, new_thread);
4333 binder_inner_proc_unlock(proc);
4334 if (thread != new_thread)
4335 kfree(new_thread);
4337 return thread;
4340 static void binder_free_proc(struct binder_proc *proc)
4342 BUG_ON(!list_empty(&proc->todo));
4343 BUG_ON(!list_empty(&proc->delivered_death));
4344 binder_alloc_deferred_release(&proc->alloc);
4345 put_task_struct(proc->tsk);
4346 binder_stats_deleted(BINDER_STAT_PROC);
4347 kfree(proc);
4350 static void binder_free_thread(struct binder_thread *thread)
4352 BUG_ON(!list_empty(&thread->todo));
4353 binder_stats_deleted(BINDER_STAT_THREAD);
4354 binder_proc_dec_tmpref(thread->proc);
4355 kfree(thread);
4358 static int binder_thread_release(struct binder_proc *proc,
4359 struct binder_thread *thread)
4361 struct binder_transaction *t;
4362 struct binder_transaction *send_reply = NULL;
4363 int active_transactions = 0;
4364 struct binder_transaction *last_t = NULL;
4366 binder_inner_proc_lock(thread->proc);
4368 * take a ref on the proc so it survives
4369 * after we remove this thread from proc->threads.
4370 * The corresponding dec is when we actually
4371 * free the thread in binder_free_thread()
4373 proc->tmp_ref++;
4375 * take a ref on this thread to ensure it
4376 * survives while we are releasing it
4378 atomic_inc(&thread->tmp_ref);
4379 rb_erase(&thread->rb_node, &proc->threads);
4380 t = thread->transaction_stack;
4381 if (t) {
4382 spin_lock(&t->lock);
4383 if (t->to_thread == thread)
4384 send_reply = t;
4386 thread->is_dead = true;
4388 while (t) {
4389 last_t = t;
4390 active_transactions++;
4391 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4392 "release %d:%d transaction %d %s, still active\n",
4393 proc->pid, thread->pid,
4394 t->debug_id,
4395 (t->to_thread == thread) ? "in" : "out");
4397 if (t->to_thread == thread) {
4398 t->to_proc = NULL;
4399 t->to_thread = NULL;
4400 if (t->buffer) {
4401 t->buffer->transaction = NULL;
4402 t->buffer = NULL;
4404 t = t->to_parent;
4405 } else if (t->from == thread) {
4406 t->from = NULL;
4407 t = t->from_parent;
4408 } else
4409 BUG();
4410 spin_unlock(&last_t->lock);
4411 if (t)
4412 spin_lock(&t->lock);
4416 * If this thread used poll, make sure we remove the waitqueue
4417 * from any epoll data structures holding it with POLLFREE.
4418 * waitqueue_active() is safe to use here because we're holding
4419 * the inner lock.
4421 if ((thread->looper & BINDER_LOOPER_STATE_POLL) &&
4422 waitqueue_active(&thread->wait)) {
4423 wake_up_poll(&thread->wait, EPOLLHUP | POLLFREE);
4426 binder_inner_proc_unlock(thread->proc);
4429 * This is needed to avoid races between wake_up_poll() above and
4430 * and ep_remove_waitqueue() called for other reasons (eg the epoll file
4431 * descriptor being closed); ep_remove_waitqueue() holds an RCU read
4432 * lock, so we can be sure it's done after calling synchronize_rcu().
4434 if (thread->looper & BINDER_LOOPER_STATE_POLL)
4435 synchronize_rcu();
4437 if (send_reply)
4438 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
4439 binder_release_work(proc, &thread->todo);
4440 binder_thread_dec_tmpref(thread);
4441 return active_transactions;
4444 static __poll_t binder_poll(struct file *filp,
4445 struct poll_table_struct *wait)
4447 struct binder_proc *proc = filp->private_data;
4448 struct binder_thread *thread = NULL;
4449 bool wait_for_proc_work;
4451 thread = binder_get_thread(proc);
4452 if (!thread)
4453 return POLLERR;
4455 binder_inner_proc_lock(thread->proc);
4456 thread->looper |= BINDER_LOOPER_STATE_POLL;
4457 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
4459 binder_inner_proc_unlock(thread->proc);
4461 poll_wait(filp, &thread->wait, wait);
4463 if (binder_has_work(thread, wait_for_proc_work))
4464 return EPOLLIN;
4466 return 0;
4469 static int binder_ioctl_write_read(struct file *filp,
4470 unsigned int cmd, unsigned long arg,
4471 struct binder_thread *thread)
4473 int ret = 0;
4474 struct binder_proc *proc = filp->private_data;
4475 unsigned int size = _IOC_SIZE(cmd);
4476 void __user *ubuf = (void __user *)arg;
4477 struct binder_write_read bwr;
4479 if (size != sizeof(struct binder_write_read)) {
4480 ret = -EINVAL;
4481 goto out;
4483 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
4484 ret = -EFAULT;
4485 goto out;
4487 binder_debug(BINDER_DEBUG_READ_WRITE,
4488 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
4489 proc->pid, thread->pid,
4490 (u64)bwr.write_size, (u64)bwr.write_buffer,
4491 (u64)bwr.read_size, (u64)bwr.read_buffer);
4493 if (bwr.write_size > 0) {
4494 ret = binder_thread_write(proc, thread,
4495 bwr.write_buffer,
4496 bwr.write_size,
4497 &bwr.write_consumed);
4498 trace_binder_write_done(ret);
4499 if (ret < 0) {
4500 bwr.read_consumed = 0;
4501 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4502 ret = -EFAULT;
4503 goto out;
4506 if (bwr.read_size > 0) {
4507 ret = binder_thread_read(proc, thread, bwr.read_buffer,
4508 bwr.read_size,
4509 &bwr.read_consumed,
4510 filp->f_flags & O_NONBLOCK);
4511 trace_binder_read_done(ret);
4512 binder_inner_proc_lock(proc);
4513 if (!binder_worklist_empty_ilocked(&proc->todo))
4514 binder_wakeup_proc_ilocked(proc);
4515 binder_inner_proc_unlock(proc);
4516 if (ret < 0) {
4517 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4518 ret = -EFAULT;
4519 goto out;
4522 binder_debug(BINDER_DEBUG_READ_WRITE,
4523 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
4524 proc->pid, thread->pid,
4525 (u64)bwr.write_consumed, (u64)bwr.write_size,
4526 (u64)bwr.read_consumed, (u64)bwr.read_size);
4527 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
4528 ret = -EFAULT;
4529 goto out;
4531 out:
4532 return ret;
4535 static int binder_ioctl_set_ctx_mgr(struct file *filp)
4537 int ret = 0;
4538 struct binder_proc *proc = filp->private_data;
4539 struct binder_context *context = proc->context;
4540 struct binder_node *new_node;
4541 kuid_t curr_euid = current_euid();
4543 mutex_lock(&context->context_mgr_node_lock);
4544 if (context->binder_context_mgr_node) {
4545 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
4546 ret = -EBUSY;
4547 goto out;
4549 ret = security_binder_set_context_mgr(proc->tsk);
4550 if (ret < 0)
4551 goto out;
4552 if (uid_valid(context->binder_context_mgr_uid)) {
4553 if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
4554 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
4555 from_kuid(&init_user_ns, curr_euid),
4556 from_kuid(&init_user_ns,
4557 context->binder_context_mgr_uid));
4558 ret = -EPERM;
4559 goto out;
4561 } else {
4562 context->binder_context_mgr_uid = curr_euid;
4564 new_node = binder_new_node(proc, NULL);
4565 if (!new_node) {
4566 ret = -ENOMEM;
4567 goto out;
4569 binder_node_lock(new_node);
4570 new_node->local_weak_refs++;
4571 new_node->local_strong_refs++;
4572 new_node->has_strong_ref = 1;
4573 new_node->has_weak_ref = 1;
4574 context->binder_context_mgr_node = new_node;
4575 binder_node_unlock(new_node);
4576 binder_put_node(new_node);
4577 out:
4578 mutex_unlock(&context->context_mgr_node_lock);
4579 return ret;
4582 static int binder_ioctl_get_node_debug_info(struct binder_proc *proc,
4583 struct binder_node_debug_info *info)
4585 struct rb_node *n;
4586 binder_uintptr_t ptr = info->ptr;
4588 memset(info, 0, sizeof(*info));
4590 binder_inner_proc_lock(proc);
4591 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
4592 struct binder_node *node = rb_entry(n, struct binder_node,
4593 rb_node);
4594 if (node->ptr > ptr) {
4595 info->ptr = node->ptr;
4596 info->cookie = node->cookie;
4597 info->has_strong_ref = node->has_strong_ref;
4598 info->has_weak_ref = node->has_weak_ref;
4599 break;
4602 binder_inner_proc_unlock(proc);
4604 return 0;
4607 static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4609 int ret;
4610 struct binder_proc *proc = filp->private_data;
4611 struct binder_thread *thread;
4612 unsigned int size = _IOC_SIZE(cmd);
4613 void __user *ubuf = (void __user *)arg;
4615 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
4616 proc->pid, current->pid, cmd, arg);*/
4618 binder_selftest_alloc(&proc->alloc);
4620 trace_binder_ioctl(cmd, arg);
4622 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4623 if (ret)
4624 goto err_unlocked;
4626 thread = binder_get_thread(proc);
4627 if (thread == NULL) {
4628 ret = -ENOMEM;
4629 goto err;
4632 switch (cmd) {
4633 case BINDER_WRITE_READ:
4634 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
4635 if (ret)
4636 goto err;
4637 break;
4638 case BINDER_SET_MAX_THREADS: {
4639 int max_threads;
4641 if (copy_from_user(&max_threads, ubuf,
4642 sizeof(max_threads))) {
4643 ret = -EINVAL;
4644 goto err;
4646 binder_inner_proc_lock(proc);
4647 proc->max_threads = max_threads;
4648 binder_inner_proc_unlock(proc);
4649 break;
4651 case BINDER_SET_CONTEXT_MGR:
4652 ret = binder_ioctl_set_ctx_mgr(filp);
4653 if (ret)
4654 goto err;
4655 break;
4656 case BINDER_THREAD_EXIT:
4657 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
4658 proc->pid, thread->pid);
4659 binder_thread_release(proc, thread);
4660 thread = NULL;
4661 break;
4662 case BINDER_VERSION: {
4663 struct binder_version __user *ver = ubuf;
4665 if (size != sizeof(struct binder_version)) {
4666 ret = -EINVAL;
4667 goto err;
4669 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
4670 &ver->protocol_version)) {
4671 ret = -EINVAL;
4672 goto err;
4674 break;
4676 case BINDER_GET_NODE_DEBUG_INFO: {
4677 struct binder_node_debug_info info;
4679 if (copy_from_user(&info, ubuf, sizeof(info))) {
4680 ret = -EFAULT;
4681 goto err;
4684 ret = binder_ioctl_get_node_debug_info(proc, &info);
4685 if (ret < 0)
4686 goto err;
4688 if (copy_to_user(ubuf, &info, sizeof(info))) {
4689 ret = -EFAULT;
4690 goto err;
4692 break;
4694 default:
4695 ret = -EINVAL;
4696 goto err;
4698 ret = 0;
4699 err:
4700 if (thread)
4701 thread->looper_need_return = false;
4702 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4703 if (ret && ret != -ERESTARTSYS)
4704 pr_info("%d:%d ioctl %x %lx returned %d\n", proc->pid, current->pid, cmd, arg, ret);
4705 err_unlocked:
4706 trace_binder_ioctl_done(ret);
4707 return ret;
4710 static void binder_vma_open(struct vm_area_struct *vma)
4712 struct binder_proc *proc = vma->vm_private_data;
4714 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4715 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
4716 proc->pid, vma->vm_start, vma->vm_end,
4717 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4718 (unsigned long)pgprot_val(vma->vm_page_prot));
4721 static void binder_vma_close(struct vm_area_struct *vma)
4723 struct binder_proc *proc = vma->vm_private_data;
4725 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4726 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
4727 proc->pid, vma->vm_start, vma->vm_end,
4728 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4729 (unsigned long)pgprot_val(vma->vm_page_prot));
4730 binder_alloc_vma_close(&proc->alloc);
4731 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
4734 static vm_fault_t binder_vm_fault(struct vm_fault *vmf)
4736 return VM_FAULT_SIGBUS;
4739 static const struct vm_operations_struct binder_vm_ops = {
4740 .open = binder_vma_open,
4741 .close = binder_vma_close,
4742 .fault = binder_vm_fault,
4745 static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
4747 int ret;
4748 struct binder_proc *proc = filp->private_data;
4749 const char *failure_string;
4751 if (proc->tsk != current->group_leader)
4752 return -EINVAL;
4754 if ((vma->vm_end - vma->vm_start) > SZ_4M)
4755 vma->vm_end = vma->vm_start + SZ_4M;
4757 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4758 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
4759 __func__, proc->pid, vma->vm_start, vma->vm_end,
4760 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4761 (unsigned long)pgprot_val(vma->vm_page_prot));
4763 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
4764 ret = -EPERM;
4765 failure_string = "bad vm_flags";
4766 goto err_bad_arg;
4768 vma->vm_flags |= VM_DONTCOPY | VM_MIXEDMAP;
4769 vma->vm_flags &= ~VM_MAYWRITE;
4771 vma->vm_ops = &binder_vm_ops;
4772 vma->vm_private_data = proc;
4774 ret = binder_alloc_mmap_handler(&proc->alloc, vma);
4775 if (ret)
4776 return ret;
4777 mutex_lock(&proc->files_lock);
4778 proc->files = get_files_struct(current);
4779 mutex_unlock(&proc->files_lock);
4780 return 0;
4782 err_bad_arg:
4783 pr_err("%s: %d %lx-%lx %s failed %d\n", __func__,
4784 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
4785 return ret;
4788 static int binder_open(struct inode *nodp, struct file *filp)
4790 struct binder_proc *proc;
4791 struct binder_device *binder_dev;
4793 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "%s: %d:%d\n", __func__,
4794 current->group_leader->pid, current->pid);
4796 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
4797 if (proc == NULL)
4798 return -ENOMEM;
4799 spin_lock_init(&proc->inner_lock);
4800 spin_lock_init(&proc->outer_lock);
4801 get_task_struct(current->group_leader);
4802 proc->tsk = current->group_leader;
4803 mutex_init(&proc->files_lock);
4804 INIT_LIST_HEAD(&proc->todo);
4805 proc->default_priority = task_nice(current);
4806 binder_dev = container_of(filp->private_data, struct binder_device,
4807 miscdev);
4808 proc->context = &binder_dev->context;
4809 binder_alloc_init(&proc->alloc);
4811 binder_stats_created(BINDER_STAT_PROC);
4812 proc->pid = current->group_leader->pid;
4813 INIT_LIST_HEAD(&proc->delivered_death);
4814 INIT_LIST_HEAD(&proc->waiting_threads);
4815 filp->private_data = proc;
4817 mutex_lock(&binder_procs_lock);
4818 hlist_add_head(&proc->proc_node, &binder_procs);
4819 mutex_unlock(&binder_procs_lock);
4821 if (binder_debugfs_dir_entry_proc) {
4822 char strbuf[11];
4824 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
4826 * proc debug entries are shared between contexts, so
4827 * this will fail if the process tries to open the driver
4828 * again with a different context. The priting code will
4829 * anyway print all contexts that a given PID has, so this
4830 * is not a problem.
4832 proc->debugfs_entry = debugfs_create_file(strbuf, 0444,
4833 binder_debugfs_dir_entry_proc,
4834 (void *)(unsigned long)proc->pid,
4835 &binder_proc_fops);
4838 return 0;
4841 static int binder_flush(struct file *filp, fl_owner_t id)
4843 struct binder_proc *proc = filp->private_data;
4845 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
4847 return 0;
4850 static void binder_deferred_flush(struct binder_proc *proc)
4852 struct rb_node *n;
4853 int wake_count = 0;
4855 binder_inner_proc_lock(proc);
4856 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
4857 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
4859 thread->looper_need_return = true;
4860 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
4861 wake_up_interruptible(&thread->wait);
4862 wake_count++;
4865 binder_inner_proc_unlock(proc);
4867 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4868 "binder_flush: %d woke %d threads\n", proc->pid,
4869 wake_count);
4872 static int binder_release(struct inode *nodp, struct file *filp)
4874 struct binder_proc *proc = filp->private_data;
4876 debugfs_remove(proc->debugfs_entry);
4877 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
4879 return 0;
4882 static int binder_node_release(struct binder_node *node, int refs)
4884 struct binder_ref *ref;
4885 int death = 0;
4886 struct binder_proc *proc = node->proc;
4888 binder_release_work(proc, &node->async_todo);
4890 binder_node_lock(node);
4891 binder_inner_proc_lock(proc);
4892 binder_dequeue_work_ilocked(&node->work);
4894 * The caller must have taken a temporary ref on the node,
4896 BUG_ON(!node->tmp_refs);
4897 if (hlist_empty(&node->refs) && node->tmp_refs == 1) {
4898 binder_inner_proc_unlock(proc);
4899 binder_node_unlock(node);
4900 binder_free_node(node);
4902 return refs;
4905 node->proc = NULL;
4906 node->local_strong_refs = 0;
4907 node->local_weak_refs = 0;
4908 binder_inner_proc_unlock(proc);
4910 spin_lock(&binder_dead_nodes_lock);
4911 hlist_add_head(&node->dead_node, &binder_dead_nodes);
4912 spin_unlock(&binder_dead_nodes_lock);
4914 hlist_for_each_entry(ref, &node->refs, node_entry) {
4915 refs++;
4917 * Need the node lock to synchronize
4918 * with new notification requests and the
4919 * inner lock to synchronize with queued
4920 * death notifications.
4922 binder_inner_proc_lock(ref->proc);
4923 if (!ref->death) {
4924 binder_inner_proc_unlock(ref->proc);
4925 continue;
4928 death++;
4930 BUG_ON(!list_empty(&ref->death->work.entry));
4931 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
4932 binder_enqueue_work_ilocked(&ref->death->work,
4933 &ref->proc->todo);
4934 binder_wakeup_proc_ilocked(ref->proc);
4935 binder_inner_proc_unlock(ref->proc);
4938 binder_debug(BINDER_DEBUG_DEAD_BINDER,
4939 "node %d now dead, refs %d, death %d\n",
4940 node->debug_id, refs, death);
4941 binder_node_unlock(node);
4942 binder_put_node(node);
4944 return refs;
4947 static void binder_deferred_release(struct binder_proc *proc)
4949 struct binder_context *context = proc->context;
4950 struct rb_node *n;
4951 int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
4953 BUG_ON(proc->files);
4955 mutex_lock(&binder_procs_lock);
4956 hlist_del(&proc->proc_node);
4957 mutex_unlock(&binder_procs_lock);
4959 mutex_lock(&context->context_mgr_node_lock);
4960 if (context->binder_context_mgr_node &&
4961 context->binder_context_mgr_node->proc == proc) {
4962 binder_debug(BINDER_DEBUG_DEAD_BINDER,
4963 "%s: %d context_mgr_node gone\n",
4964 __func__, proc->pid);
4965 context->binder_context_mgr_node = NULL;
4967 mutex_unlock(&context->context_mgr_node_lock);
4968 binder_inner_proc_lock(proc);
4970 * Make sure proc stays alive after we
4971 * remove all the threads
4973 proc->tmp_ref++;
4975 proc->is_dead = true;
4976 threads = 0;
4977 active_transactions = 0;
4978 while ((n = rb_first(&proc->threads))) {
4979 struct binder_thread *thread;
4981 thread = rb_entry(n, struct binder_thread, rb_node);
4982 binder_inner_proc_unlock(proc);
4983 threads++;
4984 active_transactions += binder_thread_release(proc, thread);
4985 binder_inner_proc_lock(proc);
4988 nodes = 0;
4989 incoming_refs = 0;
4990 while ((n = rb_first(&proc->nodes))) {
4991 struct binder_node *node;
4993 node = rb_entry(n, struct binder_node, rb_node);
4994 nodes++;
4996 * take a temporary ref on the node before
4997 * calling binder_node_release() which will either
4998 * kfree() the node or call binder_put_node()
5000 binder_inc_node_tmpref_ilocked(node);
5001 rb_erase(&node->rb_node, &proc->nodes);
5002 binder_inner_proc_unlock(proc);
5003 incoming_refs = binder_node_release(node, incoming_refs);
5004 binder_inner_proc_lock(proc);
5006 binder_inner_proc_unlock(proc);
5008 outgoing_refs = 0;
5009 binder_proc_lock(proc);
5010 while ((n = rb_first(&proc->refs_by_desc))) {
5011 struct binder_ref *ref;
5013 ref = rb_entry(n, struct binder_ref, rb_node_desc);
5014 outgoing_refs++;
5015 binder_cleanup_ref_olocked(ref);
5016 binder_proc_unlock(proc);
5017 binder_free_ref(ref);
5018 binder_proc_lock(proc);
5020 binder_proc_unlock(proc);
5022 binder_release_work(proc, &proc->todo);
5023 binder_release_work(proc, &proc->delivered_death);
5025 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5026 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
5027 __func__, proc->pid, threads, nodes, incoming_refs,
5028 outgoing_refs, active_transactions);
5030 binder_proc_dec_tmpref(proc);
5033 static void binder_deferred_func(struct work_struct *work)
5035 struct binder_proc *proc;
5036 struct files_struct *files;
5038 int defer;
5040 do {
5041 mutex_lock(&binder_deferred_lock);
5042 if (!hlist_empty(&binder_deferred_list)) {
5043 proc = hlist_entry(binder_deferred_list.first,
5044 struct binder_proc, deferred_work_node);
5045 hlist_del_init(&proc->deferred_work_node);
5046 defer = proc->deferred_work;
5047 proc->deferred_work = 0;
5048 } else {
5049 proc = NULL;
5050 defer = 0;
5052 mutex_unlock(&binder_deferred_lock);
5054 files = NULL;
5055 if (defer & BINDER_DEFERRED_PUT_FILES) {
5056 mutex_lock(&proc->files_lock);
5057 files = proc->files;
5058 if (files)
5059 proc->files = NULL;
5060 mutex_unlock(&proc->files_lock);
5063 if (defer & BINDER_DEFERRED_FLUSH)
5064 binder_deferred_flush(proc);
5066 if (defer & BINDER_DEFERRED_RELEASE)
5067 binder_deferred_release(proc); /* frees proc */
5069 if (files)
5070 put_files_struct(files);
5071 } while (proc);
5073 static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
5075 static void
5076 binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
5078 mutex_lock(&binder_deferred_lock);
5079 proc->deferred_work |= defer;
5080 if (hlist_unhashed(&proc->deferred_work_node)) {
5081 hlist_add_head(&proc->deferred_work_node,
5082 &binder_deferred_list);
5083 schedule_work(&binder_deferred_work);
5085 mutex_unlock(&binder_deferred_lock);
5088 static void print_binder_transaction_ilocked(struct seq_file *m,
5089 struct binder_proc *proc,
5090 const char *prefix,
5091 struct binder_transaction *t)
5093 struct binder_proc *to_proc;
5094 struct binder_buffer *buffer = t->buffer;
5096 spin_lock(&t->lock);
5097 to_proc = t->to_proc;
5098 seq_printf(m,
5099 "%s %d: %pK from %d:%d to %d:%d code %x flags %x pri %ld r%d",
5100 prefix, t->debug_id, t,
5101 t->from ? t->from->proc->pid : 0,
5102 t->from ? t->from->pid : 0,
5103 to_proc ? to_proc->pid : 0,
5104 t->to_thread ? t->to_thread->pid : 0,
5105 t->code, t->flags, t->priority, t->need_reply);
5106 spin_unlock(&t->lock);
5108 if (proc != to_proc) {
5110 * Can only safely deref buffer if we are holding the
5111 * correct proc inner lock for this node
5113 seq_puts(m, "\n");
5114 return;
5117 if (buffer == NULL) {
5118 seq_puts(m, " buffer free\n");
5119 return;
5121 if (buffer->target_node)
5122 seq_printf(m, " node %d", buffer->target_node->debug_id);
5123 seq_printf(m, " size %zd:%zd data %pK\n",
5124 buffer->data_size, buffer->offsets_size,
5125 buffer->data);
5128 static void print_binder_work_ilocked(struct seq_file *m,
5129 struct binder_proc *proc,
5130 const char *prefix,
5131 const char *transaction_prefix,
5132 struct binder_work *w)
5134 struct binder_node *node;
5135 struct binder_transaction *t;
5137 switch (w->type) {
5138 case BINDER_WORK_TRANSACTION:
5139 t = container_of(w, struct binder_transaction, work);
5140 print_binder_transaction_ilocked(
5141 m, proc, transaction_prefix, t);
5142 break;
5143 case BINDER_WORK_RETURN_ERROR: {
5144 struct binder_error *e = container_of(
5145 w, struct binder_error, work);
5147 seq_printf(m, "%stransaction error: %u\n",
5148 prefix, e->cmd);
5149 } break;
5150 case BINDER_WORK_TRANSACTION_COMPLETE:
5151 seq_printf(m, "%stransaction complete\n", prefix);
5152 break;
5153 case BINDER_WORK_NODE:
5154 node = container_of(w, struct binder_node, work);
5155 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
5156 prefix, node->debug_id,
5157 (u64)node->ptr, (u64)node->cookie);
5158 break;
5159 case BINDER_WORK_DEAD_BINDER:
5160 seq_printf(m, "%shas dead binder\n", prefix);
5161 break;
5162 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
5163 seq_printf(m, "%shas cleared dead binder\n", prefix);
5164 break;
5165 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
5166 seq_printf(m, "%shas cleared death notification\n", prefix);
5167 break;
5168 default:
5169 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
5170 break;
5174 static void print_binder_thread_ilocked(struct seq_file *m,
5175 struct binder_thread *thread,
5176 int print_always)
5178 struct binder_transaction *t;
5179 struct binder_work *w;
5180 size_t start_pos = m->count;
5181 size_t header_pos;
5183 seq_printf(m, " thread %d: l %02x need_return %d tr %d\n",
5184 thread->pid, thread->looper,
5185 thread->looper_need_return,
5186 atomic_read(&thread->tmp_ref));
5187 header_pos = m->count;
5188 t = thread->transaction_stack;
5189 while (t) {
5190 if (t->from == thread) {
5191 print_binder_transaction_ilocked(m, thread->proc,
5192 " outgoing transaction", t);
5193 t = t->from_parent;
5194 } else if (t->to_thread == thread) {
5195 print_binder_transaction_ilocked(m, thread->proc,
5196 " incoming transaction", t);
5197 t = t->to_parent;
5198 } else {
5199 print_binder_transaction_ilocked(m, thread->proc,
5200 " bad transaction", t);
5201 t = NULL;
5204 list_for_each_entry(w, &thread->todo, entry) {
5205 print_binder_work_ilocked(m, thread->proc, " ",
5206 " pending transaction", w);
5208 if (!print_always && m->count == header_pos)
5209 m->count = start_pos;
5212 static void print_binder_node_nilocked(struct seq_file *m,
5213 struct binder_node *node)
5215 struct binder_ref *ref;
5216 struct binder_work *w;
5217 int count;
5219 count = 0;
5220 hlist_for_each_entry(ref, &node->refs, node_entry)
5221 count++;
5223 seq_printf(m, " node %d: u%016llx c%016llx hs %d hw %d ls %d lw %d is %d iw %d tr %d",
5224 node->debug_id, (u64)node->ptr, (u64)node->cookie,
5225 node->has_strong_ref, node->has_weak_ref,
5226 node->local_strong_refs, node->local_weak_refs,
5227 node->internal_strong_refs, count, node->tmp_refs);
5228 if (count) {
5229 seq_puts(m, " proc");
5230 hlist_for_each_entry(ref, &node->refs, node_entry)
5231 seq_printf(m, " %d", ref->proc->pid);
5233 seq_puts(m, "\n");
5234 if (node->proc) {
5235 list_for_each_entry(w, &node->async_todo, entry)
5236 print_binder_work_ilocked(m, node->proc, " ",
5237 " pending async transaction", w);
5241 static void print_binder_ref_olocked(struct seq_file *m,
5242 struct binder_ref *ref)
5244 binder_node_lock(ref->node);
5245 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
5246 ref->data.debug_id, ref->data.desc,
5247 ref->node->proc ? "" : "dead ",
5248 ref->node->debug_id, ref->data.strong,
5249 ref->data.weak, ref->death);
5250 binder_node_unlock(ref->node);
5253 static void print_binder_proc(struct seq_file *m,
5254 struct binder_proc *proc, int print_all)
5256 struct binder_work *w;
5257 struct rb_node *n;
5258 size_t start_pos = m->count;
5259 size_t header_pos;
5260 struct binder_node *last_node = NULL;
5262 seq_printf(m, "proc %d\n", proc->pid);
5263 seq_printf(m, "context %s\n", proc->context->name);
5264 header_pos = m->count;
5266 binder_inner_proc_lock(proc);
5267 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
5268 print_binder_thread_ilocked(m, rb_entry(n, struct binder_thread,
5269 rb_node), print_all);
5271 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
5272 struct binder_node *node = rb_entry(n, struct binder_node,
5273 rb_node);
5275 * take a temporary reference on the node so it
5276 * survives and isn't removed from the tree
5277 * while we print it.
5279 binder_inc_node_tmpref_ilocked(node);
5280 /* Need to drop inner lock to take node lock */
5281 binder_inner_proc_unlock(proc);
5282 if (last_node)
5283 binder_put_node(last_node);
5284 binder_node_inner_lock(node);
5285 print_binder_node_nilocked(m, node);
5286 binder_node_inner_unlock(node);
5287 last_node = node;
5288 binder_inner_proc_lock(proc);
5290 binder_inner_proc_unlock(proc);
5291 if (last_node)
5292 binder_put_node(last_node);
5294 if (print_all) {
5295 binder_proc_lock(proc);
5296 for (n = rb_first(&proc->refs_by_desc);
5297 n != NULL;
5298 n = rb_next(n))
5299 print_binder_ref_olocked(m, rb_entry(n,
5300 struct binder_ref,
5301 rb_node_desc));
5302 binder_proc_unlock(proc);
5304 binder_alloc_print_allocated(m, &proc->alloc);
5305 binder_inner_proc_lock(proc);
5306 list_for_each_entry(w, &proc->todo, entry)
5307 print_binder_work_ilocked(m, proc, " ",
5308 " pending transaction", w);
5309 list_for_each_entry(w, &proc->delivered_death, entry) {
5310 seq_puts(m, " has delivered dead binder\n");
5311 break;
5313 binder_inner_proc_unlock(proc);
5314 if (!print_all && m->count == header_pos)
5315 m->count = start_pos;
5318 static const char * const binder_return_strings[] = {
5319 "BR_ERROR",
5320 "BR_OK",
5321 "BR_TRANSACTION",
5322 "BR_REPLY",
5323 "BR_ACQUIRE_RESULT",
5324 "BR_DEAD_REPLY",
5325 "BR_TRANSACTION_COMPLETE",
5326 "BR_INCREFS",
5327 "BR_ACQUIRE",
5328 "BR_RELEASE",
5329 "BR_DECREFS",
5330 "BR_ATTEMPT_ACQUIRE",
5331 "BR_NOOP",
5332 "BR_SPAWN_LOOPER",
5333 "BR_FINISHED",
5334 "BR_DEAD_BINDER",
5335 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
5336 "BR_FAILED_REPLY"
5339 static const char * const binder_command_strings[] = {
5340 "BC_TRANSACTION",
5341 "BC_REPLY",
5342 "BC_ACQUIRE_RESULT",
5343 "BC_FREE_BUFFER",
5344 "BC_INCREFS",
5345 "BC_ACQUIRE",
5346 "BC_RELEASE",
5347 "BC_DECREFS",
5348 "BC_INCREFS_DONE",
5349 "BC_ACQUIRE_DONE",
5350 "BC_ATTEMPT_ACQUIRE",
5351 "BC_REGISTER_LOOPER",
5352 "BC_ENTER_LOOPER",
5353 "BC_EXIT_LOOPER",
5354 "BC_REQUEST_DEATH_NOTIFICATION",
5355 "BC_CLEAR_DEATH_NOTIFICATION",
5356 "BC_DEAD_BINDER_DONE",
5357 "BC_TRANSACTION_SG",
5358 "BC_REPLY_SG",
5361 static const char * const binder_objstat_strings[] = {
5362 "proc",
5363 "thread",
5364 "node",
5365 "ref",
5366 "death",
5367 "transaction",
5368 "transaction_complete"
5371 static void print_binder_stats(struct seq_file *m, const char *prefix,
5372 struct binder_stats *stats)
5374 int i;
5376 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
5377 ARRAY_SIZE(binder_command_strings));
5378 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
5379 int temp = atomic_read(&stats->bc[i]);
5381 if (temp)
5382 seq_printf(m, "%s%s: %d\n", prefix,
5383 binder_command_strings[i], temp);
5386 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
5387 ARRAY_SIZE(binder_return_strings));
5388 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
5389 int temp = atomic_read(&stats->br[i]);
5391 if (temp)
5392 seq_printf(m, "%s%s: %d\n", prefix,
5393 binder_return_strings[i], temp);
5396 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
5397 ARRAY_SIZE(binder_objstat_strings));
5398 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
5399 ARRAY_SIZE(stats->obj_deleted));
5400 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
5401 int created = atomic_read(&stats->obj_created[i]);
5402 int deleted = atomic_read(&stats->obj_deleted[i]);
5404 if (created || deleted)
5405 seq_printf(m, "%s%s: active %d total %d\n",
5406 prefix,
5407 binder_objstat_strings[i],
5408 created - deleted,
5409 created);
5413 static void print_binder_proc_stats(struct seq_file *m,
5414 struct binder_proc *proc)
5416 struct binder_work *w;
5417 struct binder_thread *thread;
5418 struct rb_node *n;
5419 int count, strong, weak, ready_threads;
5420 size_t free_async_space =
5421 binder_alloc_get_free_async_space(&proc->alloc);
5423 seq_printf(m, "proc %d\n", proc->pid);
5424 seq_printf(m, "context %s\n", proc->context->name);
5425 count = 0;
5426 ready_threads = 0;
5427 binder_inner_proc_lock(proc);
5428 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
5429 count++;
5431 list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node)
5432 ready_threads++;
5434 seq_printf(m, " threads: %d\n", count);
5435 seq_printf(m, " requested threads: %d+%d/%d\n"
5436 " ready threads %d\n"
5437 " free async space %zd\n", proc->requested_threads,
5438 proc->requested_threads_started, proc->max_threads,
5439 ready_threads,
5440 free_async_space);
5441 count = 0;
5442 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
5443 count++;
5444 binder_inner_proc_unlock(proc);
5445 seq_printf(m, " nodes: %d\n", count);
5446 count = 0;
5447 strong = 0;
5448 weak = 0;
5449 binder_proc_lock(proc);
5450 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
5451 struct binder_ref *ref = rb_entry(n, struct binder_ref,
5452 rb_node_desc);
5453 count++;
5454 strong += ref->data.strong;
5455 weak += ref->data.weak;
5457 binder_proc_unlock(proc);
5458 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
5460 count = binder_alloc_get_allocated_count(&proc->alloc);
5461 seq_printf(m, " buffers: %d\n", count);
5463 binder_alloc_print_pages(m, &proc->alloc);
5465 count = 0;
5466 binder_inner_proc_lock(proc);
5467 list_for_each_entry(w, &proc->todo, entry) {
5468 if (w->type == BINDER_WORK_TRANSACTION)
5469 count++;
5471 binder_inner_proc_unlock(proc);
5472 seq_printf(m, " pending transactions: %d\n", count);
5474 print_binder_stats(m, " ", &proc->stats);
5478 static int binder_state_show(struct seq_file *m, void *unused)
5480 struct binder_proc *proc;
5481 struct binder_node *node;
5482 struct binder_node *last_node = NULL;
5484 seq_puts(m, "binder state:\n");
5486 spin_lock(&binder_dead_nodes_lock);
5487 if (!hlist_empty(&binder_dead_nodes))
5488 seq_puts(m, "dead nodes:\n");
5489 hlist_for_each_entry(node, &binder_dead_nodes, dead_node) {
5491 * take a temporary reference on the node so it
5492 * survives and isn't removed from the list
5493 * while we print it.
5495 node->tmp_refs++;
5496 spin_unlock(&binder_dead_nodes_lock);
5497 if (last_node)
5498 binder_put_node(last_node);
5499 binder_node_lock(node);
5500 print_binder_node_nilocked(m, node);
5501 binder_node_unlock(node);
5502 last_node = node;
5503 spin_lock(&binder_dead_nodes_lock);
5505 spin_unlock(&binder_dead_nodes_lock);
5506 if (last_node)
5507 binder_put_node(last_node);
5509 mutex_lock(&binder_procs_lock);
5510 hlist_for_each_entry(proc, &binder_procs, proc_node)
5511 print_binder_proc(m, proc, 1);
5512 mutex_unlock(&binder_procs_lock);
5514 return 0;
5517 static int binder_stats_show(struct seq_file *m, void *unused)
5519 struct binder_proc *proc;
5521 seq_puts(m, "binder stats:\n");
5523 print_binder_stats(m, "", &binder_stats);
5525 mutex_lock(&binder_procs_lock);
5526 hlist_for_each_entry(proc, &binder_procs, proc_node)
5527 print_binder_proc_stats(m, proc);
5528 mutex_unlock(&binder_procs_lock);
5530 return 0;
5533 static int binder_transactions_show(struct seq_file *m, void *unused)
5535 struct binder_proc *proc;
5537 seq_puts(m, "binder transactions:\n");
5538 mutex_lock(&binder_procs_lock);
5539 hlist_for_each_entry(proc, &binder_procs, proc_node)
5540 print_binder_proc(m, proc, 0);
5541 mutex_unlock(&binder_procs_lock);
5543 return 0;
5546 static int binder_proc_show(struct seq_file *m, void *unused)
5548 struct binder_proc *itr;
5549 int pid = (unsigned long)m->private;
5551 mutex_lock(&binder_procs_lock);
5552 hlist_for_each_entry(itr, &binder_procs, proc_node) {
5553 if (itr->pid == pid) {
5554 seq_puts(m, "binder proc state:\n");
5555 print_binder_proc(m, itr, 1);
5558 mutex_unlock(&binder_procs_lock);
5560 return 0;
5563 static void print_binder_transaction_log_entry(struct seq_file *m,
5564 struct binder_transaction_log_entry *e)
5566 int debug_id = READ_ONCE(e->debug_id_done);
5568 * read barrier to guarantee debug_id_done read before
5569 * we print the log values
5571 smp_rmb();
5572 seq_printf(m,
5573 "%d: %s from %d:%d to %d:%d context %s node %d handle %d size %d:%d ret %d/%d l=%d",
5574 e->debug_id, (e->call_type == 2) ? "reply" :
5575 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
5576 e->from_thread, e->to_proc, e->to_thread, e->context_name,
5577 e->to_node, e->target_handle, e->data_size, e->offsets_size,
5578 e->return_error, e->return_error_param,
5579 e->return_error_line);
5581 * read-barrier to guarantee read of debug_id_done after
5582 * done printing the fields of the entry
5584 smp_rmb();
5585 seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ?
5586 "\n" : " (incomplete)\n");
5589 static int binder_transaction_log_show(struct seq_file *m, void *unused)
5591 struct binder_transaction_log *log = m->private;
5592 unsigned int log_cur = atomic_read(&log->cur);
5593 unsigned int count;
5594 unsigned int cur;
5595 int i;
5597 count = log_cur + 1;
5598 cur = count < ARRAY_SIZE(log->entry) && !log->full ?
5599 0 : count % ARRAY_SIZE(log->entry);
5600 if (count > ARRAY_SIZE(log->entry) || log->full)
5601 count = ARRAY_SIZE(log->entry);
5602 for (i = 0; i < count; i++) {
5603 unsigned int index = cur++ % ARRAY_SIZE(log->entry);
5605 print_binder_transaction_log_entry(m, &log->entry[index]);
5607 return 0;
5610 static const struct file_operations binder_fops = {
5611 .owner = THIS_MODULE,
5612 .poll = binder_poll,
5613 .unlocked_ioctl = binder_ioctl,
5614 .compat_ioctl = binder_ioctl,
5615 .mmap = binder_mmap,
5616 .open = binder_open,
5617 .flush = binder_flush,
5618 .release = binder_release,
5621 BINDER_DEBUG_ENTRY(state);
5622 BINDER_DEBUG_ENTRY(stats);
5623 BINDER_DEBUG_ENTRY(transactions);
5624 BINDER_DEBUG_ENTRY(transaction_log);
5626 static int __init init_binder_device(const char *name)
5628 int ret;
5629 struct binder_device *binder_device;
5631 binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
5632 if (!binder_device)
5633 return -ENOMEM;
5635 binder_device->miscdev.fops = &binder_fops;
5636 binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
5637 binder_device->miscdev.name = name;
5639 binder_device->context.binder_context_mgr_uid = INVALID_UID;
5640 binder_device->context.name = name;
5641 mutex_init(&binder_device->context.context_mgr_node_lock);
5643 ret = misc_register(&binder_device->miscdev);
5644 if (ret < 0) {
5645 kfree(binder_device);
5646 return ret;
5649 hlist_add_head(&binder_device->hlist, &binder_devices);
5651 return ret;
5654 static int __init binder_init(void)
5656 int ret;
5657 char *device_name, *device_names, *device_tmp;
5658 struct binder_device *device;
5659 struct hlist_node *tmp;
5661 ret = binder_alloc_shrinker_init();
5662 if (ret)
5663 return ret;
5665 atomic_set(&binder_transaction_log.cur, ~0U);
5666 atomic_set(&binder_transaction_log_failed.cur, ~0U);
5668 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
5669 if (binder_debugfs_dir_entry_root)
5670 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
5671 binder_debugfs_dir_entry_root);
5673 if (binder_debugfs_dir_entry_root) {
5674 debugfs_create_file("state",
5675 0444,
5676 binder_debugfs_dir_entry_root,
5677 NULL,
5678 &binder_state_fops);
5679 debugfs_create_file("stats",
5680 0444,
5681 binder_debugfs_dir_entry_root,
5682 NULL,
5683 &binder_stats_fops);
5684 debugfs_create_file("transactions",
5685 0444,
5686 binder_debugfs_dir_entry_root,
5687 NULL,
5688 &binder_transactions_fops);
5689 debugfs_create_file("transaction_log",
5690 0444,
5691 binder_debugfs_dir_entry_root,
5692 &binder_transaction_log,
5693 &binder_transaction_log_fops);
5694 debugfs_create_file("failed_transaction_log",
5695 0444,
5696 binder_debugfs_dir_entry_root,
5697 &binder_transaction_log_failed,
5698 &binder_transaction_log_fops);
5702 * Copy the module_parameter string, because we don't want to
5703 * tokenize it in-place.
5705 device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL);
5706 if (!device_names) {
5707 ret = -ENOMEM;
5708 goto err_alloc_device_names_failed;
5710 strcpy(device_names, binder_devices_param);
5712 device_tmp = device_names;
5713 while ((device_name = strsep(&device_tmp, ","))) {
5714 ret = init_binder_device(device_name);
5715 if (ret)
5716 goto err_init_binder_device_failed;
5719 return ret;
5721 err_init_binder_device_failed:
5722 hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
5723 misc_deregister(&device->miscdev);
5724 hlist_del(&device->hlist);
5725 kfree(device);
5728 kfree(device_names);
5730 err_alloc_device_names_failed:
5731 debugfs_remove_recursive(binder_debugfs_dir_entry_root);
5733 return ret;
5736 device_initcall(binder_init);
5738 #define CREATE_TRACE_POINTS
5739 #include "binder_trace.h"
5741 MODULE_LICENSE("GPL v2");