ANDROID: binder: Add tracing for binder priority inheritance.
[GitHub/MotorolaMobilityLLC/kernel-slsi.git] / drivers / android / binder.c
1 /* binder.c
2 *
3 * Android IPC Subsystem
4 *
5 * Copyright (C) 2007-2008 Google, Inc.
6 *
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.
10 *
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.
15 *
16 */
17
18 /*
19 * Locking overview
20 *
21 * There are 3 main spinlocks which must be acquired in the
22 * order shown:
23 *
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
37 *
38 * Any lock under procA must never be nested under any lock at the same
39 * level or below on procB.
40 *
41 * Functions that require a lock held on entry indicate which lock
42 * in the suffix of the function name:
43 *
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 * ...
50 */
51
52 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
53
54 #include <asm/cacheflush.h>
55 #include <linux/fdtable.h>
56 #include <linux/file.h>
57 #include <linux/freezer.h>
58 #include <linux/fs.h>
59 #include <linux/list.h>
60 #include <linux/miscdevice.h>
61 #include <linux/module.h>
62 #include <linux/mutex.h>
63 #include <linux/nsproxy.h>
64 #include <linux/poll.h>
65 #include <linux/debugfs.h>
66 #include <linux/rbtree.h>
67 #include <linux/sched/signal.h>
68 #include <linux/sched/mm.h>
69 #include <linux/seq_file.h>
70 #include <linux/uaccess.h>
71 #include <linux/pid_namespace.h>
72 #include <linux/security.h>
73 #include <linux/spinlock.h>
74
75 #ifdef CONFIG_ANDROID_BINDER_IPC_32BIT
76 #define BINDER_IPC_32BIT 1
77 #endif
78
79 #include <uapi/linux/android/binder.h>
80 #include <uapi/linux/sched/types.h>
81 #include "binder_alloc.h"
82 #include "binder_trace.h"
83
84 static HLIST_HEAD(binder_deferred_list);
85 static DEFINE_MUTEX(binder_deferred_lock);
86
87 static HLIST_HEAD(binder_devices);
88 static HLIST_HEAD(binder_procs);
89 static DEFINE_MUTEX(binder_procs_lock);
90
91 static HLIST_HEAD(binder_dead_nodes);
92 static DEFINE_SPINLOCK(binder_dead_nodes_lock);
93
94 static struct dentry *binder_debugfs_dir_entry_root;
95 static struct dentry *binder_debugfs_dir_entry_proc;
96 static atomic_t binder_last_id;
97
98 #define BINDER_DEBUG_ENTRY(name) \
99 static int binder_##name##_open(struct inode *inode, struct file *file) \
100 { \
101 return single_open(file, binder_##name##_show, inode->i_private); \
102 } \
103 \
104 static const struct file_operations binder_##name##_fops = { \
105 .owner = THIS_MODULE, \
106 .open = binder_##name##_open, \
107 .read = seq_read, \
108 .llseek = seq_lseek, \
109 .release = single_release, \
110 }
111
112 static int binder_proc_show(struct seq_file *m, void *unused);
113 BINDER_DEBUG_ENTRY(proc);
114
115 /* This is only defined in include/asm-arm/sizes.h */
116 #ifndef SZ_1K
117 #define SZ_1K 0x400
118 #endif
119
120 #ifndef SZ_4M
121 #define SZ_4M 0x400000
122 #endif
123
124 #define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
125
126 enum {
127 BINDER_DEBUG_USER_ERROR = 1U << 0,
128 BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1,
129 BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2,
130 BINDER_DEBUG_OPEN_CLOSE = 1U << 3,
131 BINDER_DEBUG_DEAD_BINDER = 1U << 4,
132 BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5,
133 BINDER_DEBUG_READ_WRITE = 1U << 6,
134 BINDER_DEBUG_USER_REFS = 1U << 7,
135 BINDER_DEBUG_THREADS = 1U << 8,
136 BINDER_DEBUG_TRANSACTION = 1U << 9,
137 BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10,
138 BINDER_DEBUG_FREE_BUFFER = 1U << 11,
139 BINDER_DEBUG_INTERNAL_REFS = 1U << 12,
140 BINDER_DEBUG_PRIORITY_CAP = 1U << 13,
141 BINDER_DEBUG_SPINLOCKS = 1U << 14,
142 };
143 static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR |
144 BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION;
145 module_param_named(debug_mask, binder_debug_mask, uint, S_IWUSR | S_IRUGO);
146
147 static char *binder_devices_param = CONFIG_ANDROID_BINDER_DEVICES;
148 module_param_named(devices, binder_devices_param, charp, 0444);
149
150 static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
151 static int binder_stop_on_user_error;
152
153 static int binder_set_stop_on_user_error(const char *val,
154 struct kernel_param *kp)
155 {
156 int ret;
157
158 ret = param_set_int(val, kp);
159 if (binder_stop_on_user_error < 2)
160 wake_up(&binder_user_error_wait);
161 return ret;
162 }
163 module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
164 param_get_int, &binder_stop_on_user_error, S_IWUSR | S_IRUGO);
165
166 #define binder_debug(mask, x...) \
167 do { \
168 if (binder_debug_mask & mask) \
169 pr_info(x); \
170 } while (0)
171
172 #define binder_user_error(x...) \
173 do { \
174 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
175 pr_info(x); \
176 if (binder_stop_on_user_error) \
177 binder_stop_on_user_error = 2; \
178 } while (0)
179
180 #define to_flat_binder_object(hdr) \
181 container_of(hdr, struct flat_binder_object, hdr)
182
183 #define to_binder_fd_object(hdr) container_of(hdr, struct binder_fd_object, hdr)
184
185 #define to_binder_buffer_object(hdr) \
186 container_of(hdr, struct binder_buffer_object, hdr)
187
188 #define to_binder_fd_array_object(hdr) \
189 container_of(hdr, struct binder_fd_array_object, hdr)
190
191 enum binder_stat_types {
192 BINDER_STAT_PROC,
193 BINDER_STAT_THREAD,
194 BINDER_STAT_NODE,
195 BINDER_STAT_REF,
196 BINDER_STAT_DEATH,
197 BINDER_STAT_TRANSACTION,
198 BINDER_STAT_TRANSACTION_COMPLETE,
199 BINDER_STAT_COUNT
200 };
201
202 struct binder_stats {
203 atomic_t br[_IOC_NR(BR_FAILED_REPLY) + 1];
204 atomic_t bc[_IOC_NR(BC_REPLY_SG) + 1];
205 atomic_t obj_created[BINDER_STAT_COUNT];
206 atomic_t obj_deleted[BINDER_STAT_COUNT];
207 };
208
209 static struct binder_stats binder_stats;
210
211 static inline void binder_stats_deleted(enum binder_stat_types type)
212 {
213 atomic_inc(&binder_stats.obj_deleted[type]);
214 }
215
216 static inline void binder_stats_created(enum binder_stat_types type)
217 {
218 atomic_inc(&binder_stats.obj_created[type]);
219 }
220
221 struct binder_transaction_log_entry {
222 int debug_id;
223 int debug_id_done;
224 int call_type;
225 int from_proc;
226 int from_thread;
227 int target_handle;
228 int to_proc;
229 int to_thread;
230 int to_node;
231 int data_size;
232 int offsets_size;
233 int return_error_line;
234 uint32_t return_error;
235 uint32_t return_error_param;
236 const char *context_name;
237 };
238 struct binder_transaction_log {
239 atomic_t cur;
240 bool full;
241 struct binder_transaction_log_entry entry[32];
242 };
243 static struct binder_transaction_log binder_transaction_log;
244 static struct binder_transaction_log binder_transaction_log_failed;
245
246 static struct binder_transaction_log_entry *binder_transaction_log_add(
247 struct binder_transaction_log *log)
248 {
249 struct binder_transaction_log_entry *e;
250 unsigned int cur = atomic_inc_return(&log->cur);
251
252 if (cur >= ARRAY_SIZE(log->entry))
253 log->full = 1;
254 e = &log->entry[cur % ARRAY_SIZE(log->entry)];
255 WRITE_ONCE(e->debug_id_done, 0);
256 /*
257 * write-barrier to synchronize access to e->debug_id_done.
258 * We make sure the initialized 0 value is seen before
259 * memset() other fields are zeroed by memset.
260 */
261 smp_wmb();
262 memset(e, 0, sizeof(*e));
263 return e;
264 }
265
266 struct binder_context {
267 struct binder_node *binder_context_mgr_node;
268 struct mutex context_mgr_node_lock;
269
270 kuid_t binder_context_mgr_uid;
271 const char *name;
272 };
273
274 struct binder_device {
275 struct hlist_node hlist;
276 struct miscdevice miscdev;
277 struct binder_context context;
278 };
279
280 /**
281 * struct binder_work - work enqueued on a worklist
282 * @entry: node enqueued on list
283 * @type: type of work to be performed
284 *
285 * There are separate work lists for proc, thread, and node (async).
286 */
287 struct binder_work {
288 struct list_head entry;
289
290 enum {
291 BINDER_WORK_TRANSACTION = 1,
292 BINDER_WORK_TRANSACTION_COMPLETE,
293 BINDER_WORK_RETURN_ERROR,
294 BINDER_WORK_NODE,
295 BINDER_WORK_DEAD_BINDER,
296 BINDER_WORK_DEAD_BINDER_AND_CLEAR,
297 BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
298 } type;
299 };
300
301 struct binder_error {
302 struct binder_work work;
303 uint32_t cmd;
304 };
305
306 /**
307 * struct binder_node - binder node bookkeeping
308 * @debug_id: unique ID for debugging
309 * (invariant after initialized)
310 * @lock: lock for node fields
311 * @work: worklist element for node work
312 * (protected by @proc->inner_lock)
313 * @rb_node: element for proc->nodes tree
314 * (protected by @proc->inner_lock)
315 * @dead_node: element for binder_dead_nodes list
316 * (protected by binder_dead_nodes_lock)
317 * @proc: binder_proc that owns this node
318 * (invariant after initialized)
319 * @refs: list of references on this node
320 * (protected by @lock)
321 * @internal_strong_refs: used to take strong references when
322 * initiating a transaction
323 * (protected by @proc->inner_lock if @proc
324 * and by @lock)
325 * @local_weak_refs: weak user refs from local process
326 * (protected by @proc->inner_lock if @proc
327 * and by @lock)
328 * @local_strong_refs: strong user refs from local process
329 * (protected by @proc->inner_lock if @proc
330 * and by @lock)
331 * @tmp_refs: temporary kernel refs
332 * (protected by @proc->inner_lock while @proc
333 * is valid, and by binder_dead_nodes_lock
334 * if @proc is NULL. During inc/dec and node release
335 * it is also protected by @lock to provide safety
336 * as the node dies and @proc becomes NULL)
337 * @ptr: userspace pointer for node
338 * (invariant, no lock needed)
339 * @cookie: userspace cookie for node
340 * (invariant, no lock needed)
341 * @has_strong_ref: userspace notified of strong ref
342 * (protected by @proc->inner_lock if @proc
343 * and by @lock)
344 * @pending_strong_ref: userspace has acked notification of strong ref
345 * (protected by @proc->inner_lock if @proc
346 * and by @lock)
347 * @has_weak_ref: userspace notified of weak ref
348 * (protected by @proc->inner_lock if @proc
349 * and by @lock)
350 * @pending_weak_ref: userspace has acked notification of weak ref
351 * (protected by @proc->inner_lock if @proc
352 * and by @lock)
353 * @has_async_transaction: async transaction to node in progress
354 * (protected by @lock)
355 * @sched_policy: minimum scheduling policy for node
356 * (invariant after initialized)
357 * @accept_fds: file descriptor operations supported for node
358 * (invariant after initialized)
359 * @min_priority: minimum scheduling priority
360 * (invariant after initialized)
361 * @inherit_rt: inherit RT scheduling policy from caller
362 * (invariant after initialized)
363 * @async_todo: list of async work items
364 * (protected by @proc->inner_lock)
365 *
366 * Bookkeeping structure for binder nodes.
367 */
368 struct binder_node {
369 int debug_id;
370 spinlock_t lock;
371 struct binder_work work;
372 union {
373 struct rb_node rb_node;
374 struct hlist_node dead_node;
375 };
376 struct binder_proc *proc;
377 struct hlist_head refs;
378 int internal_strong_refs;
379 int local_weak_refs;
380 int local_strong_refs;
381 int tmp_refs;
382 binder_uintptr_t ptr;
383 binder_uintptr_t cookie;
384 struct {
385 /*
386 * bitfield elements protected by
387 * proc inner_lock
388 */
389 u8 has_strong_ref:1;
390 u8 pending_strong_ref:1;
391 u8 has_weak_ref:1;
392 u8 pending_weak_ref:1;
393 };
394 struct {
395 /*
396 * invariant after initialization
397 */
398 u8 sched_policy:2;
399 u8 inherit_rt:1;
400 u8 accept_fds:1;
401 u8 min_priority;
402 };
403 bool has_async_transaction;
404 struct list_head async_todo;
405 };
406
407 struct binder_ref_death {
408 /**
409 * @work: worklist element for death notifications
410 * (protected by inner_lock of the proc that
411 * this ref belongs to)
412 */
413 struct binder_work work;
414 binder_uintptr_t cookie;
415 };
416
417 /**
418 * struct binder_ref_data - binder_ref counts and id
419 * @debug_id: unique ID for the ref
420 * @desc: unique userspace handle for ref
421 * @strong: strong ref count (debugging only if not locked)
422 * @weak: weak ref count (debugging only if not locked)
423 *
424 * Structure to hold ref count and ref id information. Since
425 * the actual ref can only be accessed with a lock, this structure
426 * is used to return information about the ref to callers of
427 * ref inc/dec functions.
428 */
429 struct binder_ref_data {
430 int debug_id;
431 uint32_t desc;
432 int strong;
433 int weak;
434 };
435
436 /**
437 * struct binder_ref - struct to track references on nodes
438 * @data: binder_ref_data containing id, handle, and current refcounts
439 * @rb_node_desc: node for lookup by @data.desc in proc's rb_tree
440 * @rb_node_node: node for lookup by @node in proc's rb_tree
441 * @node_entry: list entry for node->refs list in target node
442 * (protected by @node->lock)
443 * @proc: binder_proc containing ref
444 * @node: binder_node of target node. When cleaning up a
445 * ref for deletion in binder_cleanup_ref, a non-NULL
446 * @node indicates the node must be freed
447 * @death: pointer to death notification (ref_death) if requested
448 * (protected by @node->lock)
449 *
450 * Structure to track references from procA to target node (on procB). This
451 * structure is unsafe to access without holding @proc->outer_lock.
452 */
453 struct binder_ref {
454 /* Lookups needed: */
455 /* node + proc => ref (transaction) */
456 /* desc + proc => ref (transaction, inc/dec ref) */
457 /* node => refs + procs (proc exit) */
458 struct binder_ref_data data;
459 struct rb_node rb_node_desc;
460 struct rb_node rb_node_node;
461 struct hlist_node node_entry;
462 struct binder_proc *proc;
463 struct binder_node *node;
464 struct binder_ref_death *death;
465 };
466
467 enum binder_deferred_state {
468 BINDER_DEFERRED_PUT_FILES = 0x01,
469 BINDER_DEFERRED_FLUSH = 0x02,
470 BINDER_DEFERRED_RELEASE = 0x04,
471 };
472
473 /**
474 * struct binder_priority - scheduler policy and priority
475 * @sched_policy scheduler policy
476 * @prio [100..139] for SCHED_NORMAL, [0..99] for FIFO/RT
477 *
478 * The binder driver supports inheriting the following scheduler policies:
479 * SCHED_NORMAL
480 * SCHED_BATCH
481 * SCHED_FIFO
482 * SCHED_RR
483 */
484 struct binder_priority {
485 unsigned int sched_policy;
486 int prio;
487 };
488
489 /**
490 * struct binder_proc - binder process bookkeeping
491 * @proc_node: element for binder_procs list
492 * @threads: rbtree of binder_threads in this proc
493 * (protected by @inner_lock)
494 * @nodes: rbtree of binder nodes associated with
495 * this proc ordered by node->ptr
496 * (protected by @inner_lock)
497 * @refs_by_desc: rbtree of refs ordered by ref->desc
498 * (protected by @outer_lock)
499 * @refs_by_node: rbtree of refs ordered by ref->node
500 * (protected by @outer_lock)
501 * @waiting_threads: threads currently waiting for proc work
502 * (protected by @inner_lock)
503 * @pid PID of group_leader of process
504 * (invariant after initialized)
505 * @tsk task_struct for group_leader of process
506 * (invariant after initialized)
507 * @files files_struct for process
508 * (invariant after initialized)
509 * @deferred_work_node: element for binder_deferred_list
510 * (protected by binder_deferred_lock)
511 * @deferred_work: bitmap of deferred work to perform
512 * (protected by binder_deferred_lock)
513 * @is_dead: process is dead and awaiting free
514 * when outstanding transactions are cleaned up
515 * (protected by @inner_lock)
516 * @todo: list of work for this process
517 * (protected by @inner_lock)
518 * @wait: wait queue head to wait for proc work
519 * (invariant after initialized)
520 * @stats: per-process binder statistics
521 * (atomics, no lock needed)
522 * @delivered_death: list of delivered death notification
523 * (protected by @inner_lock)
524 * @max_threads: cap on number of binder threads
525 * (protected by @inner_lock)
526 * @requested_threads: number of binder threads requested but not
527 * yet started. In current implementation, can
528 * only be 0 or 1.
529 * (protected by @inner_lock)
530 * @requested_threads_started: number binder threads started
531 * (protected by @inner_lock)
532 * @tmp_ref: temporary reference to indicate proc is in use
533 * (protected by @inner_lock)
534 * @default_priority: default scheduler priority
535 * (invariant after initialized)
536 * @debugfs_entry: debugfs node
537 * @alloc: binder allocator bookkeeping
538 * @context: binder_context for this proc
539 * (invariant after initialized)
540 * @inner_lock: can nest under outer_lock and/or node lock
541 * @outer_lock: no nesting under innor or node lock
542 * Lock order: 1) outer, 2) node, 3) inner
543 *
544 * Bookkeeping structure for binder processes
545 */
546 struct binder_proc {
547 struct hlist_node proc_node;
548 struct rb_root threads;
549 struct rb_root nodes;
550 struct rb_root refs_by_desc;
551 struct rb_root refs_by_node;
552 struct list_head waiting_threads;
553 int pid;
554 struct task_struct *tsk;
555 struct files_struct *files;
556 struct hlist_node deferred_work_node;
557 int deferred_work;
558 bool is_dead;
559
560 struct list_head todo;
561 wait_queue_head_t wait;
562 struct binder_stats stats;
563 struct list_head delivered_death;
564 int max_threads;
565 int requested_threads;
566 int requested_threads_started;
567 int tmp_ref;
568 struct binder_priority default_priority;
569 struct dentry *debugfs_entry;
570 struct binder_alloc alloc;
571 struct binder_context *context;
572 spinlock_t inner_lock;
573 spinlock_t outer_lock;
574 };
575
576 enum {
577 BINDER_LOOPER_STATE_REGISTERED = 0x01,
578 BINDER_LOOPER_STATE_ENTERED = 0x02,
579 BINDER_LOOPER_STATE_EXITED = 0x04,
580 BINDER_LOOPER_STATE_INVALID = 0x08,
581 BINDER_LOOPER_STATE_WAITING = 0x10,
582 BINDER_LOOPER_STATE_POLL = 0x20,
583 };
584
585 /**
586 * struct binder_thread - binder thread bookkeeping
587 * @proc: binder process for this thread
588 * (invariant after initialization)
589 * @rb_node: element for proc->threads rbtree
590 * (protected by @proc->inner_lock)
591 * @waiting_thread_node: element for @proc->waiting_threads list
592 * (protected by @proc->inner_lock)
593 * @pid: PID for this thread
594 * (invariant after initialization)
595 * @looper: bitmap of looping state
596 * (only accessed by this thread)
597 * @looper_needs_return: looping thread needs to exit driver
598 * (no lock needed)
599 * @transaction_stack: stack of in-progress transactions for this thread
600 * (protected by @proc->inner_lock)
601 * @todo: list of work to do for this thread
602 * (protected by @proc->inner_lock)
603 * @return_error: transaction errors reported by this thread
604 * (only accessed by this thread)
605 * @reply_error: transaction errors reported by target thread
606 * (protected by @proc->inner_lock)
607 * @wait: wait queue for thread work
608 * @stats: per-thread statistics
609 * (atomics, no lock needed)
610 * @tmp_ref: temporary reference to indicate thread is in use
611 * (atomic since @proc->inner_lock cannot
612 * always be acquired)
613 * @is_dead: thread is dead and awaiting free
614 * when outstanding transactions are cleaned up
615 * (protected by @proc->inner_lock)
616 * @task: struct task_struct for this thread
617 *
618 * Bookkeeping structure for binder threads.
619 */
620 struct binder_thread {
621 struct binder_proc *proc;
622 struct rb_node rb_node;
623 struct list_head waiting_thread_node;
624 int pid;
625 int looper; /* only modified by this thread */
626 bool looper_need_return; /* can be written by other thread */
627 struct binder_transaction *transaction_stack;
628 struct list_head todo;
629 struct binder_error return_error;
630 struct binder_error reply_error;
631 wait_queue_head_t wait;
632 struct binder_stats stats;
633 atomic_t tmp_ref;
634 bool is_dead;
635 struct task_struct *task;
636 };
637
638 struct binder_transaction {
639 int debug_id;
640 struct binder_work work;
641 struct binder_thread *from;
642 struct binder_transaction *from_parent;
643 struct binder_proc *to_proc;
644 struct binder_thread *to_thread;
645 struct binder_transaction *to_parent;
646 unsigned need_reply:1;
647 /* unsigned is_dead:1; */ /* not used at the moment */
648
649 struct binder_buffer *buffer;
650 unsigned int code;
651 unsigned int flags;
652 struct binder_priority priority;
653 struct binder_priority saved_priority;
654 bool set_priority_called;
655 kuid_t sender_euid;
656 /**
657 * @lock: protects @from, @to_proc, and @to_thread
658 *
659 * @from, @to_proc, and @to_thread can be set to NULL
660 * during thread teardown
661 */
662 spinlock_t lock;
663 };
664
665 /**
666 * binder_proc_lock() - Acquire outer lock for given binder_proc
667 * @proc: struct binder_proc to acquire
668 *
669 * Acquires proc->outer_lock. Used to protect binder_ref
670 * structures associated with the given proc.
671 */
672 #define binder_proc_lock(proc) _binder_proc_lock(proc, __LINE__)
673 static void
674 _binder_proc_lock(struct binder_proc *proc, int line)
675 {
676 binder_debug(BINDER_DEBUG_SPINLOCKS,
677 "%s: line=%d\n", __func__, line);
678 spin_lock(&proc->outer_lock);
679 }
680
681 /**
682 * binder_proc_unlock() - Release spinlock for given binder_proc
683 * @proc: struct binder_proc to acquire
684 *
685 * Release lock acquired via binder_proc_lock()
686 */
687 #define binder_proc_unlock(_proc) _binder_proc_unlock(_proc, __LINE__)
688 static void
689 _binder_proc_unlock(struct binder_proc *proc, int line)
690 {
691 binder_debug(BINDER_DEBUG_SPINLOCKS,
692 "%s: line=%d\n", __func__, line);
693 spin_unlock(&proc->outer_lock);
694 }
695
696 /**
697 * binder_inner_proc_lock() - Acquire inner lock for given binder_proc
698 * @proc: struct binder_proc to acquire
699 *
700 * Acquires proc->inner_lock. Used to protect todo lists
701 */
702 #define binder_inner_proc_lock(proc) _binder_inner_proc_lock(proc, __LINE__)
703 static void
704 _binder_inner_proc_lock(struct binder_proc *proc, int line)
705 {
706 binder_debug(BINDER_DEBUG_SPINLOCKS,
707 "%s: line=%d\n", __func__, line);
708 spin_lock(&proc->inner_lock);
709 }
710
711 /**
712 * binder_inner_proc_unlock() - Release inner lock for given binder_proc
713 * @proc: struct binder_proc to acquire
714 *
715 * Release lock acquired via binder_inner_proc_lock()
716 */
717 #define binder_inner_proc_unlock(proc) _binder_inner_proc_unlock(proc, __LINE__)
718 static void
719 _binder_inner_proc_unlock(struct binder_proc *proc, int line)
720 {
721 binder_debug(BINDER_DEBUG_SPINLOCKS,
722 "%s: line=%d\n", __func__, line);
723 spin_unlock(&proc->inner_lock);
724 }
725
726 /**
727 * binder_node_lock() - Acquire spinlock for given binder_node
728 * @node: struct binder_node to acquire
729 *
730 * Acquires node->lock. Used to protect binder_node fields
731 */
732 #define binder_node_lock(node) _binder_node_lock(node, __LINE__)
733 static void
734 _binder_node_lock(struct binder_node *node, int line)
735 {
736 binder_debug(BINDER_DEBUG_SPINLOCKS,
737 "%s: line=%d\n", __func__, line);
738 spin_lock(&node->lock);
739 }
740
741 /**
742 * binder_node_unlock() - Release spinlock for given binder_proc
743 * @node: struct binder_node to acquire
744 *
745 * Release lock acquired via binder_node_lock()
746 */
747 #define binder_node_unlock(node) _binder_node_unlock(node, __LINE__)
748 static void
749 _binder_node_unlock(struct binder_node *node, int line)
750 {
751 binder_debug(BINDER_DEBUG_SPINLOCKS,
752 "%s: line=%d\n", __func__, line);
753 spin_unlock(&node->lock);
754 }
755
756 /**
757 * binder_node_inner_lock() - Acquire node and inner locks
758 * @node: struct binder_node to acquire
759 *
760 * Acquires node->lock. If node->proc also acquires
761 * proc->inner_lock. Used to protect binder_node fields
762 */
763 #define binder_node_inner_lock(node) _binder_node_inner_lock(node, __LINE__)
764 static void
765 _binder_node_inner_lock(struct binder_node *node, int line)
766 {
767 binder_debug(BINDER_DEBUG_SPINLOCKS,
768 "%s: line=%d\n", __func__, line);
769 spin_lock(&node->lock);
770 if (node->proc)
771 binder_inner_proc_lock(node->proc);
772 }
773
774 /**
775 * binder_node_unlock() - Release node and inner locks
776 * @node: struct binder_node to acquire
777 *
778 * Release lock acquired via binder_node_lock()
779 */
780 #define binder_node_inner_unlock(node) _binder_node_inner_unlock(node, __LINE__)
781 static void
782 _binder_node_inner_unlock(struct binder_node *node, int line)
783 {
784 struct binder_proc *proc = node->proc;
785
786 binder_debug(BINDER_DEBUG_SPINLOCKS,
787 "%s: line=%d\n", __func__, line);
788 if (proc)
789 binder_inner_proc_unlock(proc);
790 spin_unlock(&node->lock);
791 }
792
793 static bool binder_worklist_empty_ilocked(struct list_head *list)
794 {
795 return list_empty(list);
796 }
797
798 /**
799 * binder_worklist_empty() - Check if no items on the work list
800 * @proc: binder_proc associated with list
801 * @list: list to check
802 *
803 * Return: true if there are no items on list, else false
804 */
805 static bool binder_worklist_empty(struct binder_proc *proc,
806 struct list_head *list)
807 {
808 bool ret;
809
810 binder_inner_proc_lock(proc);
811 ret = binder_worklist_empty_ilocked(list);
812 binder_inner_proc_unlock(proc);
813 return ret;
814 }
815
816 static void
817 binder_enqueue_work_ilocked(struct binder_work *work,
818 struct list_head *target_list)
819 {
820 BUG_ON(target_list == NULL);
821 BUG_ON(work->entry.next && !list_empty(&work->entry));
822 list_add_tail(&work->entry, target_list);
823 }
824
825 /**
826 * binder_enqueue_work() - Add an item to the work list
827 * @proc: binder_proc associated with list
828 * @work: struct binder_work to add to list
829 * @target_list: list to add work to
830 *
831 * Adds the work to the specified list. Asserts that work
832 * is not already on a list.
833 */
834 static void
835 binder_enqueue_work(struct binder_proc *proc,
836 struct binder_work *work,
837 struct list_head *target_list)
838 {
839 binder_inner_proc_lock(proc);
840 binder_enqueue_work_ilocked(work, target_list);
841 binder_inner_proc_unlock(proc);
842 }
843
844 static void
845 binder_dequeue_work_ilocked(struct binder_work *work)
846 {
847 list_del_init(&work->entry);
848 }
849
850 /**
851 * binder_dequeue_work() - Removes an item from the work list
852 * @proc: binder_proc associated with list
853 * @work: struct binder_work to remove from list
854 *
855 * Removes the specified work item from whatever list it is on.
856 * Can safely be called if work is not on any list.
857 */
858 static void
859 binder_dequeue_work(struct binder_proc *proc, struct binder_work *work)
860 {
861 binder_inner_proc_lock(proc);
862 binder_dequeue_work_ilocked(work);
863 binder_inner_proc_unlock(proc);
864 }
865
866 static struct binder_work *binder_dequeue_work_head_ilocked(
867 struct list_head *list)
868 {
869 struct binder_work *w;
870
871 w = list_first_entry_or_null(list, struct binder_work, entry);
872 if (w)
873 list_del_init(&w->entry);
874 return w;
875 }
876
877 /**
878 * binder_dequeue_work_head() - Dequeues the item at head of list
879 * @proc: binder_proc associated with list
880 * @list: list to dequeue head
881 *
882 * Removes the head of the list if there are items on the list
883 *
884 * Return: pointer dequeued binder_work, NULL if list was empty
885 */
886 static struct binder_work *binder_dequeue_work_head(
887 struct binder_proc *proc,
888 struct list_head *list)
889 {
890 struct binder_work *w;
891
892 binder_inner_proc_lock(proc);
893 w = binder_dequeue_work_head_ilocked(list);
894 binder_inner_proc_unlock(proc);
895 return w;
896 }
897
898 static void
899 binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
900 static void binder_free_thread(struct binder_thread *thread);
901 static void binder_free_proc(struct binder_proc *proc);
902 static void binder_inc_node_tmpref_ilocked(struct binder_node *node);
903
904 static int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
905 {
906 struct files_struct *files = proc->files;
907 unsigned long rlim_cur;
908 unsigned long irqs;
909
910 if (files == NULL)
911 return -ESRCH;
912
913 if (!lock_task_sighand(proc->tsk, &irqs))
914 return -EMFILE;
915
916 rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
917 unlock_task_sighand(proc->tsk, &irqs);
918
919 return __alloc_fd(files, 0, rlim_cur, flags);
920 }
921
922 /*
923 * copied from fd_install
924 */
925 static void task_fd_install(
926 struct binder_proc *proc, unsigned int fd, struct file *file)
927 {
928 if (proc->files)
929 __fd_install(proc->files, fd, file);
930 }
931
932 /*
933 * copied from sys_close
934 */
935 static long task_close_fd(struct binder_proc *proc, unsigned int fd)
936 {
937 int retval;
938
939 if (proc->files == NULL)
940 return -ESRCH;
941
942 retval = __close_fd(proc->files, fd);
943 /* can't restart close syscall because file table entry was cleared */
944 if (unlikely(retval == -ERESTARTSYS ||
945 retval == -ERESTARTNOINTR ||
946 retval == -ERESTARTNOHAND ||
947 retval == -ERESTART_RESTARTBLOCK))
948 retval = -EINTR;
949
950 return retval;
951 }
952
953 static bool binder_has_work_ilocked(struct binder_thread *thread,
954 bool do_proc_work)
955 {
956 return !binder_worklist_empty_ilocked(&thread->todo) ||
957 thread->looper_need_return ||
958 (do_proc_work &&
959 !binder_worklist_empty_ilocked(&thread->proc->todo));
960 }
961
962 static bool binder_has_work(struct binder_thread *thread, bool do_proc_work)
963 {
964 bool has_work;
965
966 binder_inner_proc_lock(thread->proc);
967 has_work = binder_has_work_ilocked(thread, do_proc_work);
968 binder_inner_proc_unlock(thread->proc);
969
970 return has_work;
971 }
972
973 static bool binder_available_for_proc_work_ilocked(struct binder_thread *thread)
974 {
975 return !thread->transaction_stack &&
976 binder_worklist_empty_ilocked(&thread->todo) &&
977 (thread->looper & (BINDER_LOOPER_STATE_ENTERED |
978 BINDER_LOOPER_STATE_REGISTERED));
979 }
980
981 static void binder_wakeup_poll_threads_ilocked(struct binder_proc *proc,
982 bool sync)
983 {
984 struct rb_node *n;
985 struct binder_thread *thread;
986
987 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
988 thread = rb_entry(n, struct binder_thread, rb_node);
989 if (thread->looper & BINDER_LOOPER_STATE_POLL &&
990 binder_available_for_proc_work_ilocked(thread)) {
991 if (sync)
992 wake_up_interruptible_sync(&thread->wait);
993 else
994 wake_up_interruptible(&thread->wait);
995 }
996 }
997 }
998
999 /**
1000 * binder_select_thread_ilocked() - selects a thread for doing proc work.
1001 * @proc: process to select a thread from
1002 *
1003 * Note that calling this function moves the thread off the waiting_threads
1004 * list, so it can only be woken up by the caller of this function, or a
1005 * signal. Therefore, callers *should* always wake up the thread this function
1006 * returns.
1007 *
1008 * Return: If there's a thread currently waiting for process work,
1009 * returns that thread. Otherwise returns NULL.
1010 */
1011 static struct binder_thread *
1012 binder_select_thread_ilocked(struct binder_proc *proc)
1013 {
1014 struct binder_thread *thread;
1015
1016 assert_spin_locked(&proc->inner_lock);
1017 thread = list_first_entry_or_null(&proc->waiting_threads,
1018 struct binder_thread,
1019 waiting_thread_node);
1020
1021 if (thread)
1022 list_del_init(&thread->waiting_thread_node);
1023
1024 return thread;
1025 }
1026
1027 /**
1028 * binder_wakeup_thread_ilocked() - wakes up a thread for doing proc work.
1029 * @proc: process to wake up a thread in
1030 * @thread: specific thread to wake-up (may be NULL)
1031 * @sync: whether to do a synchronous wake-up
1032 *
1033 * This function wakes up a thread in the @proc process.
1034 * The caller may provide a specific thread to wake-up in
1035 * the @thread parameter. If @thread is NULL, this function
1036 * will wake up threads that have called poll().
1037 *
1038 * Note that for this function to work as expected, callers
1039 * should first call binder_select_thread() to find a thread
1040 * to handle the work (if they don't have a thread already),
1041 * and pass the result into the @thread parameter.
1042 */
1043 static void binder_wakeup_thread_ilocked(struct binder_proc *proc,
1044 struct binder_thread *thread,
1045 bool sync)
1046 {
1047 assert_spin_locked(&proc->inner_lock);
1048
1049 if (thread) {
1050 if (sync)
1051 wake_up_interruptible_sync(&thread->wait);
1052 else
1053 wake_up_interruptible(&thread->wait);
1054 return;
1055 }
1056
1057 /* Didn't find a thread waiting for proc work; this can happen
1058 * in two scenarios:
1059 * 1. All threads are busy handling transactions
1060 * In that case, one of those threads should call back into
1061 * the kernel driver soon and pick up this work.
1062 * 2. Threads are using the (e)poll interface, in which case
1063 * they may be blocked on the waitqueue without having been
1064 * added to waiting_threads. For this case, we just iterate
1065 * over all threads not handling transaction work, and
1066 * wake them all up. We wake all because we don't know whether
1067 * a thread that called into (e)poll is handling non-binder
1068 * work currently.
1069 */
1070 binder_wakeup_poll_threads_ilocked(proc, sync);
1071 }
1072
1073 static void binder_wakeup_proc_ilocked(struct binder_proc *proc)
1074 {
1075 struct binder_thread *thread = binder_select_thread_ilocked(proc);
1076
1077 binder_wakeup_thread_ilocked(proc, thread, /* sync = */false);
1078 }
1079
1080 static bool is_rt_policy(int policy)
1081 {
1082 return policy == SCHED_FIFO || policy == SCHED_RR;
1083 }
1084
1085 static bool is_fair_policy(int policy)
1086 {
1087 return policy == SCHED_NORMAL || policy == SCHED_BATCH;
1088 }
1089
1090 static bool binder_supported_policy(int policy)
1091 {
1092 return is_fair_policy(policy) || is_rt_policy(policy);
1093 }
1094
1095 static int to_userspace_prio(int policy, int kernel_priority)
1096 {
1097 if (is_fair_policy(policy))
1098 return PRIO_TO_NICE(kernel_priority);
1099 else
1100 return MAX_USER_RT_PRIO - 1 - kernel_priority;
1101 }
1102
1103 static int to_kernel_prio(int policy, int user_priority)
1104 {
1105 if (is_fair_policy(policy))
1106 return NICE_TO_PRIO(user_priority);
1107 else
1108 return MAX_USER_RT_PRIO - 1 - user_priority;
1109 }
1110
1111 static void binder_do_set_priority(struct task_struct *task,
1112 struct binder_priority desired,
1113 bool verify)
1114 {
1115 int priority; /* user-space prio value */
1116 bool has_cap_nice;
1117 unsigned int policy = desired.sched_policy;
1118
1119 if (task->policy == policy && task->normal_prio == desired.prio)
1120 return;
1121
1122 has_cap_nice = has_capability_noaudit(task, CAP_SYS_NICE);
1123
1124 priority = to_userspace_prio(policy, desired.prio);
1125
1126 if (verify && is_rt_policy(policy) && !has_cap_nice) {
1127 long max_rtprio = task_rlimit(task, RLIMIT_RTPRIO);
1128
1129 if (max_rtprio == 0) {
1130 policy = SCHED_NORMAL;
1131 priority = MIN_NICE;
1132 } else if (priority > max_rtprio) {
1133 priority = max_rtprio;
1134 }
1135 }
1136
1137 if (verify && is_fair_policy(policy) && !has_cap_nice) {
1138 long min_nice = rlimit_to_nice(task_rlimit(task, RLIMIT_NICE));
1139
1140 if (min_nice > MAX_NICE) {
1141 binder_user_error("%d RLIMIT_NICE not set\n",
1142 task->pid);
1143 return;
1144 } else if (priority < min_nice) {
1145 priority = min_nice;
1146 }
1147 }
1148
1149 if (policy != desired.sched_policy ||
1150 to_kernel_prio(policy, priority) != desired.prio)
1151 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
1152 "%d: priority %d not allowed, using %d instead\n",
1153 task->pid, desired.prio,
1154 to_kernel_prio(policy, priority));
1155
1156 trace_binder_set_priority(task->tgid, task->pid, task->normal_prio,
1157 to_kernel_prio(policy, priority),
1158 desired.prio);
1159
1160 /* Set the actual priority */
1161 if (task->policy != policy || is_rt_policy(policy)) {
1162 struct sched_param params;
1163
1164 params.sched_priority = is_rt_policy(policy) ? priority : 0;
1165
1166 sched_setscheduler_nocheck(task,
1167 policy | SCHED_RESET_ON_FORK,
1168 &params);
1169 }
1170 if (is_fair_policy(policy))
1171 set_user_nice(task, priority);
1172 }
1173
1174 static void binder_set_priority(struct task_struct *task,
1175 struct binder_priority desired)
1176 {
1177 binder_do_set_priority(task, desired, /* verify = */ true);
1178 }
1179
1180 static void binder_restore_priority(struct task_struct *task,
1181 struct binder_priority desired)
1182 {
1183 binder_do_set_priority(task, desired, /* verify = */ false);
1184 }
1185
1186 static void binder_transaction_priority(struct task_struct *task,
1187 struct binder_transaction *t,
1188 struct binder_priority node_prio,
1189 bool inherit_rt)
1190 {
1191 struct binder_priority desired_prio;
1192
1193 if (t->set_priority_called)
1194 return;
1195
1196 t->set_priority_called = true;
1197 t->saved_priority.sched_policy = task->policy;
1198 t->saved_priority.prio = task->normal_prio;
1199
1200 if (!inherit_rt && is_rt_policy(desired_prio.sched_policy)) {
1201 desired_prio.prio = NICE_TO_PRIO(0);
1202 desired_prio.sched_policy = SCHED_NORMAL;
1203 } else {
1204 desired_prio.prio = t->priority.prio;
1205 desired_prio.sched_policy = t->priority.sched_policy;
1206 }
1207
1208 if (node_prio.prio < t->priority.prio ||
1209 (node_prio.prio == t->priority.prio &&
1210 node_prio.sched_policy == SCHED_FIFO)) {
1211 /*
1212 * In case the minimum priority on the node is
1213 * higher (lower value), use that priority. If
1214 * the priority is the same, but the node uses
1215 * SCHED_FIFO, prefer SCHED_FIFO, since it can
1216 * run unbounded, unlike SCHED_RR.
1217 */
1218 desired_prio = node_prio;
1219 }
1220
1221 binder_set_priority(task, desired_prio);
1222 }
1223
1224 static struct binder_node *binder_get_node_ilocked(struct binder_proc *proc,
1225 binder_uintptr_t ptr)
1226 {
1227 struct rb_node *n = proc->nodes.rb_node;
1228 struct binder_node *node;
1229
1230 assert_spin_locked(&proc->inner_lock);
1231
1232 while (n) {
1233 node = rb_entry(n, struct binder_node, rb_node);
1234
1235 if (ptr < node->ptr)
1236 n = n->rb_left;
1237 else if (ptr > node->ptr)
1238 n = n->rb_right;
1239 else {
1240 /*
1241 * take an implicit weak reference
1242 * to ensure node stays alive until
1243 * call to binder_put_node()
1244 */
1245 binder_inc_node_tmpref_ilocked(node);
1246 return node;
1247 }
1248 }
1249 return NULL;
1250 }
1251
1252 static struct binder_node *binder_get_node(struct binder_proc *proc,
1253 binder_uintptr_t ptr)
1254 {
1255 struct binder_node *node;
1256
1257 binder_inner_proc_lock(proc);
1258 node = binder_get_node_ilocked(proc, ptr);
1259 binder_inner_proc_unlock(proc);
1260 return node;
1261 }
1262
1263 static struct binder_node *binder_init_node_ilocked(
1264 struct binder_proc *proc,
1265 struct binder_node *new_node,
1266 struct flat_binder_object *fp)
1267 {
1268 struct rb_node **p = &proc->nodes.rb_node;
1269 struct rb_node *parent = NULL;
1270 struct binder_node *node;
1271 binder_uintptr_t ptr = fp ? fp->binder : 0;
1272 binder_uintptr_t cookie = fp ? fp->cookie : 0;
1273 __u32 flags = fp ? fp->flags : 0;
1274 s8 priority;
1275
1276 assert_spin_locked(&proc->inner_lock);
1277
1278 while (*p) {
1279
1280 parent = *p;
1281 node = rb_entry(parent, struct binder_node, rb_node);
1282
1283 if (ptr < node->ptr)
1284 p = &(*p)->rb_left;
1285 else if (ptr > node->ptr)
1286 p = &(*p)->rb_right;
1287 else {
1288 /*
1289 * A matching node is already in
1290 * the rb tree. Abandon the init
1291 * and return it.
1292 */
1293 binder_inc_node_tmpref_ilocked(node);
1294 return node;
1295 }
1296 }
1297 node = new_node;
1298 binder_stats_created(BINDER_STAT_NODE);
1299 node->tmp_refs++;
1300 rb_link_node(&node->rb_node, parent, p);
1301 rb_insert_color(&node->rb_node, &proc->nodes);
1302 node->debug_id = atomic_inc_return(&binder_last_id);
1303 node->proc = proc;
1304 node->ptr = ptr;
1305 node->cookie = cookie;
1306 node->work.type = BINDER_WORK_NODE;
1307 priority = flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
1308 node->sched_policy = (flags & FLAT_BINDER_FLAG_PRIORITY_MASK) >>
1309 FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT;
1310 node->min_priority = to_kernel_prio(node->sched_policy, priority);
1311 node->accept_fds = !!(flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
1312 node->inherit_rt = !!(flags & FLAT_BINDER_FLAG_INHERIT_RT);
1313 spin_lock_init(&node->lock);
1314 INIT_LIST_HEAD(&node->work.entry);
1315 INIT_LIST_HEAD(&node->async_todo);
1316 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1317 "%d:%d node %d u%016llx c%016llx created\n",
1318 proc->pid, current->pid, node->debug_id,
1319 (u64)node->ptr, (u64)node->cookie);
1320
1321 return node;
1322 }
1323
1324 static struct binder_node *binder_new_node(struct binder_proc *proc,
1325 struct flat_binder_object *fp)
1326 {
1327 struct binder_node *node;
1328 struct binder_node *new_node = kzalloc(sizeof(*node), GFP_KERNEL);
1329
1330 if (!new_node)
1331 return NULL;
1332 binder_inner_proc_lock(proc);
1333 node = binder_init_node_ilocked(proc, new_node, fp);
1334 binder_inner_proc_unlock(proc);
1335 if (node != new_node)
1336 /*
1337 * The node was already added by another thread
1338 */
1339 kfree(new_node);
1340
1341 return node;
1342 }
1343
1344 static void binder_free_node(struct binder_node *node)
1345 {
1346 kfree(node);
1347 binder_stats_deleted(BINDER_STAT_NODE);
1348 }
1349
1350 static int binder_inc_node_nilocked(struct binder_node *node, int strong,
1351 int internal,
1352 struct list_head *target_list)
1353 {
1354 struct binder_proc *proc = node->proc;
1355
1356 assert_spin_locked(&node->lock);
1357 if (proc)
1358 assert_spin_locked(&proc->inner_lock);
1359 if (strong) {
1360 if (internal) {
1361 if (target_list == NULL &&
1362 node->internal_strong_refs == 0 &&
1363 !(node->proc &&
1364 node == node->proc->context->binder_context_mgr_node &&
1365 node->has_strong_ref)) {
1366 pr_err("invalid inc strong node for %d\n",
1367 node->debug_id);
1368 return -EINVAL;
1369 }
1370 node->internal_strong_refs++;
1371 } else
1372 node->local_strong_refs++;
1373 if (!node->has_strong_ref && target_list) {
1374 binder_dequeue_work_ilocked(&node->work);
1375 binder_enqueue_work_ilocked(&node->work, target_list);
1376 }
1377 } else {
1378 if (!internal)
1379 node->local_weak_refs++;
1380 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
1381 if (target_list == NULL) {
1382 pr_err("invalid inc weak node for %d\n",
1383 node->debug_id);
1384 return -EINVAL;
1385 }
1386 binder_enqueue_work_ilocked(&node->work, target_list);
1387 }
1388 }
1389 return 0;
1390 }
1391
1392 static int binder_inc_node(struct binder_node *node, int strong, int internal,
1393 struct list_head *target_list)
1394 {
1395 int ret;
1396
1397 binder_node_inner_lock(node);
1398 ret = binder_inc_node_nilocked(node, strong, internal, target_list);
1399 binder_node_inner_unlock(node);
1400
1401 return ret;
1402 }
1403
1404 static bool binder_dec_node_nilocked(struct binder_node *node,
1405 int strong, int internal)
1406 {
1407 struct binder_proc *proc = node->proc;
1408
1409 assert_spin_locked(&node->lock);
1410 if (proc)
1411 assert_spin_locked(&proc->inner_lock);
1412 if (strong) {
1413 if (internal)
1414 node->internal_strong_refs--;
1415 else
1416 node->local_strong_refs--;
1417 if (node->local_strong_refs || node->internal_strong_refs)
1418 return false;
1419 } else {
1420 if (!internal)
1421 node->local_weak_refs--;
1422 if (node->local_weak_refs || node->tmp_refs ||
1423 !hlist_empty(&node->refs))
1424 return false;
1425 }
1426
1427 if (proc && (node->has_strong_ref || node->has_weak_ref)) {
1428 if (list_empty(&node->work.entry)) {
1429 binder_enqueue_work_ilocked(&node->work, &proc->todo);
1430 binder_wakeup_proc_ilocked(proc);
1431 }
1432 } else {
1433 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
1434 !node->local_weak_refs && !node->tmp_refs) {
1435 if (proc) {
1436 binder_dequeue_work_ilocked(&node->work);
1437 rb_erase(&node->rb_node, &proc->nodes);
1438 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1439 "refless node %d deleted\n",
1440 node->debug_id);
1441 } else {
1442 BUG_ON(!list_empty(&node->work.entry));
1443 spin_lock(&binder_dead_nodes_lock);
1444 /*
1445 * tmp_refs could have changed so
1446 * check it again
1447 */
1448 if (node->tmp_refs) {
1449 spin_unlock(&binder_dead_nodes_lock);
1450 return false;
1451 }
1452 hlist_del(&node->dead_node);
1453 spin_unlock(&binder_dead_nodes_lock);
1454 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1455 "dead node %d deleted\n",
1456 node->debug_id);
1457 }
1458 return true;
1459 }
1460 }
1461 return false;
1462 }
1463
1464 static void binder_dec_node(struct binder_node *node, int strong, int internal)
1465 {
1466 bool free_node;
1467
1468 binder_node_inner_lock(node);
1469 free_node = binder_dec_node_nilocked(node, strong, internal);
1470 binder_node_inner_unlock(node);
1471 if (free_node)
1472 binder_free_node(node);
1473 }
1474
1475 static void binder_inc_node_tmpref_ilocked(struct binder_node *node)
1476 {
1477 /*
1478 * No call to binder_inc_node() is needed since we
1479 * don't need to inform userspace of any changes to
1480 * tmp_refs
1481 */
1482 node->tmp_refs++;
1483 }
1484
1485 /**
1486 * binder_inc_node_tmpref() - take a temporary reference on node
1487 * @node: node to reference
1488 *
1489 * Take reference on node to prevent the node from being freed
1490 * while referenced only by a local variable. The inner lock is
1491 * needed to serialize with the node work on the queue (which
1492 * isn't needed after the node is dead). If the node is dead
1493 * (node->proc is NULL), use binder_dead_nodes_lock to protect
1494 * node->tmp_refs against dead-node-only cases where the node
1495 * lock cannot be acquired (eg traversing the dead node list to
1496 * print nodes)
1497 */
1498 static void binder_inc_node_tmpref(struct binder_node *node)
1499 {
1500 binder_node_lock(node);
1501 if (node->proc)
1502 binder_inner_proc_lock(node->proc);
1503 else
1504 spin_lock(&binder_dead_nodes_lock);
1505 binder_inc_node_tmpref_ilocked(node);
1506 if (node->proc)
1507 binder_inner_proc_unlock(node->proc);
1508 else
1509 spin_unlock(&binder_dead_nodes_lock);
1510 binder_node_unlock(node);
1511 }
1512
1513 /**
1514 * binder_dec_node_tmpref() - remove a temporary reference on node
1515 * @node: node to reference
1516 *
1517 * Release temporary reference on node taken via binder_inc_node_tmpref()
1518 */
1519 static void binder_dec_node_tmpref(struct binder_node *node)
1520 {
1521 bool free_node;
1522
1523 binder_node_inner_lock(node);
1524 if (!node->proc)
1525 spin_lock(&binder_dead_nodes_lock);
1526 node->tmp_refs--;
1527 BUG_ON(node->tmp_refs < 0);
1528 if (!node->proc)
1529 spin_unlock(&binder_dead_nodes_lock);
1530 /*
1531 * Call binder_dec_node() to check if all refcounts are 0
1532 * and cleanup is needed. Calling with strong=0 and internal=1
1533 * causes no actual reference to be released in binder_dec_node().
1534 * If that changes, a change is needed here too.
1535 */
1536 free_node = binder_dec_node_nilocked(node, 0, 1);
1537 binder_node_inner_unlock(node);
1538 if (free_node)
1539 binder_free_node(node);
1540 }
1541
1542 static void binder_put_node(struct binder_node *node)
1543 {
1544 binder_dec_node_tmpref(node);
1545 }
1546
1547 static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc,
1548 u32 desc, bool need_strong_ref)
1549 {
1550 struct rb_node *n = proc->refs_by_desc.rb_node;
1551 struct binder_ref *ref;
1552
1553 while (n) {
1554 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1555
1556 if (desc < ref->data.desc) {
1557 n = n->rb_left;
1558 } else if (desc > ref->data.desc) {
1559 n = n->rb_right;
1560 } else if (need_strong_ref && !ref->data.strong) {
1561 binder_user_error("tried to use weak ref as strong ref\n");
1562 return NULL;
1563 } else {
1564 return ref;
1565 }
1566 }
1567 return NULL;
1568 }
1569
1570 /**
1571 * binder_get_ref_for_node_olocked() - get the ref associated with given node
1572 * @proc: binder_proc that owns the ref
1573 * @node: binder_node of target
1574 * @new_ref: newly allocated binder_ref to be initialized or %NULL
1575 *
1576 * Look up the ref for the given node and return it if it exists
1577 *
1578 * If it doesn't exist and the caller provides a newly allocated
1579 * ref, initialize the fields of the newly allocated ref and insert
1580 * into the given proc rb_trees and node refs list.
1581 *
1582 * Return: the ref for node. It is possible that another thread
1583 * allocated/initialized the ref first in which case the
1584 * returned ref would be different than the passed-in
1585 * new_ref. new_ref must be kfree'd by the caller in
1586 * this case.
1587 */
1588 static struct binder_ref *binder_get_ref_for_node_olocked(
1589 struct binder_proc *proc,
1590 struct binder_node *node,
1591 struct binder_ref *new_ref)
1592 {
1593 struct binder_context *context = proc->context;
1594 struct rb_node **p = &proc->refs_by_node.rb_node;
1595 struct rb_node *parent = NULL;
1596 struct binder_ref *ref;
1597 struct rb_node *n;
1598
1599 while (*p) {
1600 parent = *p;
1601 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1602
1603 if (node < ref->node)
1604 p = &(*p)->rb_left;
1605 else if (node > ref->node)
1606 p = &(*p)->rb_right;
1607 else
1608 return ref;
1609 }
1610 if (!new_ref)
1611 return NULL;
1612
1613 binder_stats_created(BINDER_STAT_REF);
1614 new_ref->data.debug_id = atomic_inc_return(&binder_last_id);
1615 new_ref->proc = proc;
1616 new_ref->node = node;
1617 rb_link_node(&new_ref->rb_node_node, parent, p);
1618 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1619
1620 new_ref->data.desc = (node == context->binder_context_mgr_node) ? 0 : 1;
1621 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1622 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1623 if (ref->data.desc > new_ref->data.desc)
1624 break;
1625 new_ref->data.desc = ref->data.desc + 1;
1626 }
1627
1628 p = &proc->refs_by_desc.rb_node;
1629 while (*p) {
1630 parent = *p;
1631 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1632
1633 if (new_ref->data.desc < ref->data.desc)
1634 p = &(*p)->rb_left;
1635 else if (new_ref->data.desc > ref->data.desc)
1636 p = &(*p)->rb_right;
1637 else
1638 BUG();
1639 }
1640 rb_link_node(&new_ref->rb_node_desc, parent, p);
1641 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
1642
1643 binder_node_lock(node);
1644 hlist_add_head(&new_ref->node_entry, &node->refs);
1645
1646 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1647 "%d new ref %d desc %d for node %d\n",
1648 proc->pid, new_ref->data.debug_id, new_ref->data.desc,
1649 node->debug_id);
1650 binder_node_unlock(node);
1651 return new_ref;
1652 }
1653
1654 static void binder_cleanup_ref_olocked(struct binder_ref *ref)
1655 {
1656 bool delete_node = false;
1657
1658 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1659 "%d delete ref %d desc %d for node %d\n",
1660 ref->proc->pid, ref->data.debug_id, ref->data.desc,
1661 ref->node->debug_id);
1662
1663 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1664 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
1665
1666 binder_node_inner_lock(ref->node);
1667 if (ref->data.strong)
1668 binder_dec_node_nilocked(ref->node, 1, 1);
1669
1670 hlist_del(&ref->node_entry);
1671 delete_node = binder_dec_node_nilocked(ref->node, 0, 1);
1672 binder_node_inner_unlock(ref->node);
1673 /*
1674 * Clear ref->node unless we want the caller to free the node
1675 */
1676 if (!delete_node) {
1677 /*
1678 * The caller uses ref->node to determine
1679 * whether the node needs to be freed. Clear
1680 * it since the node is still alive.
1681 */
1682 ref->node = NULL;
1683 }
1684
1685 if (ref->death) {
1686 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1687 "%d delete ref %d desc %d has death notification\n",
1688 ref->proc->pid, ref->data.debug_id,
1689 ref->data.desc);
1690 binder_dequeue_work(ref->proc, &ref->death->work);
1691 binder_stats_deleted(BINDER_STAT_DEATH);
1692 }
1693 binder_stats_deleted(BINDER_STAT_REF);
1694 }
1695
1696 /**
1697 * binder_inc_ref_olocked() - increment the ref for given handle
1698 * @ref: ref to be incremented
1699 * @strong: if true, strong increment, else weak
1700 * @target_list: list to queue node work on
1701 *
1702 * Increment the ref. @ref->proc->outer_lock must be held on entry
1703 *
1704 * Return: 0, if successful, else errno
1705 */
1706 static int binder_inc_ref_olocked(struct binder_ref *ref, int strong,
1707 struct list_head *target_list)
1708 {
1709 int ret;
1710
1711 if (strong) {
1712 if (ref->data.strong == 0) {
1713 ret = binder_inc_node(ref->node, 1, 1, target_list);
1714 if (ret)
1715 return ret;
1716 }
1717 ref->data.strong++;
1718 } else {
1719 if (ref->data.weak == 0) {
1720 ret = binder_inc_node(ref->node, 0, 1, target_list);
1721 if (ret)
1722 return ret;
1723 }
1724 ref->data.weak++;
1725 }
1726 return 0;
1727 }
1728
1729 /**
1730 * binder_dec_ref() - dec the ref for given handle
1731 * @ref: ref to be decremented
1732 * @strong: if true, strong decrement, else weak
1733 *
1734 * Decrement the ref.
1735 *
1736 * Return: true if ref is cleaned up and ready to be freed
1737 */
1738 static bool binder_dec_ref_olocked(struct binder_ref *ref, int strong)
1739 {
1740 if (strong) {
1741 if (ref->data.strong == 0) {
1742 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
1743 ref->proc->pid, ref->data.debug_id,
1744 ref->data.desc, ref->data.strong,
1745 ref->data.weak);
1746 return false;
1747 }
1748 ref->data.strong--;
1749 if (ref->data.strong == 0)
1750 binder_dec_node(ref->node, strong, 1);
1751 } else {
1752 if (ref->data.weak == 0) {
1753 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
1754 ref->proc->pid, ref->data.debug_id,
1755 ref->data.desc, ref->data.strong,
1756 ref->data.weak);
1757 return false;
1758 }
1759 ref->data.weak--;
1760 }
1761 if (ref->data.strong == 0 && ref->data.weak == 0) {
1762 binder_cleanup_ref_olocked(ref);
1763 return true;
1764 }
1765 return false;
1766 }
1767
1768 /**
1769 * binder_get_node_from_ref() - get the node from the given proc/desc
1770 * @proc: proc containing the ref
1771 * @desc: the handle associated with the ref
1772 * @need_strong_ref: if true, only return node if ref is strong
1773 * @rdata: the id/refcount data for the ref
1774 *
1775 * Given a proc and ref handle, return the associated binder_node
1776 *
1777 * Return: a binder_node or NULL if not found or not strong when strong required
1778 */
1779 static struct binder_node *binder_get_node_from_ref(
1780 struct binder_proc *proc,
1781 u32 desc, bool need_strong_ref,
1782 struct binder_ref_data *rdata)
1783 {
1784 struct binder_node *node;
1785 struct binder_ref *ref;
1786
1787 binder_proc_lock(proc);
1788 ref = binder_get_ref_olocked(proc, desc, need_strong_ref);
1789 if (!ref)
1790 goto err_no_ref;
1791 node = ref->node;
1792 /*
1793 * Take an implicit reference on the node to ensure
1794 * it stays alive until the call to binder_put_node()
1795 */
1796 binder_inc_node_tmpref(node);
1797 if (rdata)
1798 *rdata = ref->data;
1799 binder_proc_unlock(proc);
1800
1801 return node;
1802
1803 err_no_ref:
1804 binder_proc_unlock(proc);
1805 return NULL;
1806 }
1807
1808 /**
1809 * binder_free_ref() - free the binder_ref
1810 * @ref: ref to free
1811 *
1812 * Free the binder_ref. Free the binder_node indicated by ref->node
1813 * (if non-NULL) and the binder_ref_death indicated by ref->death.
1814 */
1815 static void binder_free_ref(struct binder_ref *ref)
1816 {
1817 if (ref->node)
1818 binder_free_node(ref->node);
1819 kfree(ref->death);
1820 kfree(ref);
1821 }
1822
1823 /**
1824 * binder_update_ref_for_handle() - inc/dec the ref for given handle
1825 * @proc: proc containing the ref
1826 * @desc: the handle associated with the ref
1827 * @increment: true=inc reference, false=dec reference
1828 * @strong: true=strong reference, false=weak reference
1829 * @rdata: the id/refcount data for the ref
1830 *
1831 * Given a proc and ref handle, increment or decrement the ref
1832 * according to "increment" arg.
1833 *
1834 * Return: 0 if successful, else errno
1835 */
1836 static int binder_update_ref_for_handle(struct binder_proc *proc,
1837 uint32_t desc, bool increment, bool strong,
1838 struct binder_ref_data *rdata)
1839 {
1840 int ret = 0;
1841 struct binder_ref *ref;
1842 bool delete_ref = false;
1843
1844 binder_proc_lock(proc);
1845 ref = binder_get_ref_olocked(proc, desc, strong);
1846 if (!ref) {
1847 ret = -EINVAL;
1848 goto err_no_ref;
1849 }
1850 if (increment)
1851 ret = binder_inc_ref_olocked(ref, strong, NULL);
1852 else
1853 delete_ref = binder_dec_ref_olocked(ref, strong);
1854
1855 if (rdata)
1856 *rdata = ref->data;
1857 binder_proc_unlock(proc);
1858
1859 if (delete_ref)
1860 binder_free_ref(ref);
1861 return ret;
1862
1863 err_no_ref:
1864 binder_proc_unlock(proc);
1865 return ret;
1866 }
1867
1868 /**
1869 * binder_dec_ref_for_handle() - dec the ref for given handle
1870 * @proc: proc containing the ref
1871 * @desc: the handle associated with the ref
1872 * @strong: true=strong reference, false=weak reference
1873 * @rdata: the id/refcount data for the ref
1874 *
1875 * Just calls binder_update_ref_for_handle() to decrement the ref.
1876 *
1877 * Return: 0 if successful, else errno
1878 */
1879 static int binder_dec_ref_for_handle(struct binder_proc *proc,
1880 uint32_t desc, bool strong, struct binder_ref_data *rdata)
1881 {
1882 return binder_update_ref_for_handle(proc, desc, false, strong, rdata);
1883 }
1884
1885
1886 /**
1887 * binder_inc_ref_for_node() - increment the ref for given proc/node
1888 * @proc: proc containing the ref
1889 * @node: target node
1890 * @strong: true=strong reference, false=weak reference
1891 * @target_list: worklist to use if node is incremented
1892 * @rdata: the id/refcount data for the ref
1893 *
1894 * Given a proc and node, increment the ref. Create the ref if it
1895 * doesn't already exist
1896 *
1897 * Return: 0 if successful, else errno
1898 */
1899 static int binder_inc_ref_for_node(struct binder_proc *proc,
1900 struct binder_node *node,
1901 bool strong,
1902 struct list_head *target_list,
1903 struct binder_ref_data *rdata)
1904 {
1905 struct binder_ref *ref;
1906 struct binder_ref *new_ref = NULL;
1907 int ret = 0;
1908
1909 binder_proc_lock(proc);
1910 ref = binder_get_ref_for_node_olocked(proc, node, NULL);
1911 if (!ref) {
1912 binder_proc_unlock(proc);
1913 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1914 if (!new_ref)
1915 return -ENOMEM;
1916 binder_proc_lock(proc);
1917 ref = binder_get_ref_for_node_olocked(proc, node, new_ref);
1918 }
1919 ret = binder_inc_ref_olocked(ref, strong, target_list);
1920 *rdata = ref->data;
1921 binder_proc_unlock(proc);
1922 if (new_ref && ref != new_ref)
1923 /*
1924 * Another thread created the ref first so
1925 * free the one we allocated
1926 */
1927 kfree(new_ref);
1928 return ret;
1929 }
1930
1931 static void binder_pop_transaction_ilocked(struct binder_thread *target_thread,
1932 struct binder_transaction *t)
1933 {
1934 BUG_ON(!target_thread);
1935 assert_spin_locked(&target_thread->proc->inner_lock);
1936 BUG_ON(target_thread->transaction_stack != t);
1937 BUG_ON(target_thread->transaction_stack->from != target_thread);
1938 target_thread->transaction_stack =
1939 target_thread->transaction_stack->from_parent;
1940 t->from = NULL;
1941 }
1942
1943 /**
1944 * binder_thread_dec_tmpref() - decrement thread->tmp_ref
1945 * @thread: thread to decrement
1946 *
1947 * A thread needs to be kept alive while being used to create or
1948 * handle a transaction. binder_get_txn_from() is used to safely
1949 * extract t->from from a binder_transaction and keep the thread
1950 * indicated by t->from from being freed. When done with that
1951 * binder_thread, this function is called to decrement the
1952 * tmp_ref and free if appropriate (thread has been released
1953 * and no transaction being processed by the driver)
1954 */
1955 static void binder_thread_dec_tmpref(struct binder_thread *thread)
1956 {
1957 /*
1958 * atomic is used to protect the counter value while
1959 * it cannot reach zero or thread->is_dead is false
1960 */
1961 binder_inner_proc_lock(thread->proc);
1962 atomic_dec(&thread->tmp_ref);
1963 if (thread->is_dead && !atomic_read(&thread->tmp_ref)) {
1964 binder_inner_proc_unlock(thread->proc);
1965 binder_free_thread(thread);
1966 return;
1967 }
1968 binder_inner_proc_unlock(thread->proc);
1969 }
1970
1971 /**
1972 * binder_proc_dec_tmpref() - decrement proc->tmp_ref
1973 * @proc: proc to decrement
1974 *
1975 * A binder_proc needs to be kept alive while being used to create or
1976 * handle a transaction. proc->tmp_ref is incremented when
1977 * creating a new transaction or the binder_proc is currently in-use
1978 * by threads that are being released. When done with the binder_proc,
1979 * this function is called to decrement the counter and free the
1980 * proc if appropriate (proc has been released, all threads have
1981 * been released and not currenly in-use to process a transaction).
1982 */
1983 static void binder_proc_dec_tmpref(struct binder_proc *proc)
1984 {
1985 binder_inner_proc_lock(proc);
1986 proc->tmp_ref--;
1987 if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) &&
1988 !proc->tmp_ref) {
1989 binder_inner_proc_unlock(proc);
1990 binder_free_proc(proc);
1991 return;
1992 }
1993 binder_inner_proc_unlock(proc);
1994 }
1995
1996 /**
1997 * binder_get_txn_from() - safely extract the "from" thread in transaction
1998 * @t: binder transaction for t->from
1999 *
2000 * Atomically return the "from" thread and increment the tmp_ref
2001 * count for the thread to ensure it stays alive until
2002 * binder_thread_dec_tmpref() is called.
2003 *
2004 * Return: the value of t->from
2005 */
2006 static struct binder_thread *binder_get_txn_from(
2007 struct binder_transaction *t)
2008 {
2009 struct binder_thread *from;
2010
2011 spin_lock(&t->lock);
2012 from = t->from;
2013 if (from)
2014 atomic_inc(&from->tmp_ref);
2015 spin_unlock(&t->lock);
2016 return from;
2017 }
2018
2019 /**
2020 * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock
2021 * @t: binder transaction for t->from
2022 *
2023 * Same as binder_get_txn_from() except it also acquires the proc->inner_lock
2024 * to guarantee that the thread cannot be released while operating on it.
2025 * The caller must call binder_inner_proc_unlock() to release the inner lock
2026 * as well as call binder_dec_thread_txn() to release the reference.
2027 *
2028 * Return: the value of t->from
2029 */
2030 static struct binder_thread *binder_get_txn_from_and_acq_inner(
2031 struct binder_transaction *t)
2032 {
2033 struct binder_thread *from;
2034
2035 from = binder_get_txn_from(t);
2036 if (!from)
2037 return NULL;
2038 binder_inner_proc_lock(from->proc);
2039 if (t->from) {
2040 BUG_ON(from != t->from);
2041 return from;
2042 }
2043 binder_inner_proc_unlock(from->proc);
2044 binder_thread_dec_tmpref(from);
2045 return NULL;
2046 }
2047
2048 static void binder_free_transaction(struct binder_transaction *t)
2049 {
2050 if (t->buffer)
2051 t->buffer->transaction = NULL;
2052 kfree(t);
2053 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2054 }
2055
2056 static void binder_send_failed_reply(struct binder_transaction *t,
2057 uint32_t error_code)
2058 {
2059 struct binder_thread *target_thread;
2060 struct binder_transaction *next;
2061
2062 BUG_ON(t->flags & TF_ONE_WAY);
2063 while (1) {
2064 target_thread = binder_get_txn_from_and_acq_inner(t);
2065 if (target_thread) {
2066 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2067 "send failed reply for transaction %d to %d:%d\n",
2068 t->debug_id,
2069 target_thread->proc->pid,
2070 target_thread->pid);
2071
2072 binder_pop_transaction_ilocked(target_thread, t);
2073 if (target_thread->reply_error.cmd == BR_OK) {
2074 target_thread->reply_error.cmd = error_code;
2075 binder_enqueue_work_ilocked(
2076 &target_thread->reply_error.work,
2077 &target_thread->todo);
2078 wake_up_interruptible(&target_thread->wait);
2079 } else {
2080 WARN(1, "Unexpected reply error: %u\n",
2081 target_thread->reply_error.cmd);
2082 }
2083 binder_inner_proc_unlock(target_thread->proc);
2084 binder_thread_dec_tmpref(target_thread);
2085 binder_free_transaction(t);
2086 return;
2087 }
2088 next = t->from_parent;
2089
2090 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2091 "send failed reply for transaction %d, target dead\n",
2092 t->debug_id);
2093
2094 binder_free_transaction(t);
2095 if (next == NULL) {
2096 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2097 "reply failed, no target thread at root\n");
2098 return;
2099 }
2100 t = next;
2101 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2102 "reply failed, no target thread -- retry %d\n",
2103 t->debug_id);
2104 }
2105 }
2106
2107 /**
2108 * binder_validate_object() - checks for a valid metadata object in a buffer.
2109 * @buffer: binder_buffer that we're parsing.
2110 * @offset: offset in the buffer at which to validate an object.
2111 *
2112 * Return: If there's a valid metadata object at @offset in @buffer, the
2113 * size of that object. Otherwise, it returns zero.
2114 */
2115 static size_t binder_validate_object(struct binder_buffer *buffer, u64 offset)
2116 {
2117 /* Check if we can read a header first */
2118 struct binder_object_header *hdr;
2119 size_t object_size = 0;
2120
2121 if (offset > buffer->data_size - sizeof(*hdr) ||
2122 buffer->data_size < sizeof(*hdr) ||
2123 !IS_ALIGNED(offset, sizeof(u32)))
2124 return 0;
2125
2126 /* Ok, now see if we can read a complete object. */
2127 hdr = (struct binder_object_header *)(buffer->data + offset);
2128 switch (hdr->type) {
2129 case BINDER_TYPE_BINDER:
2130 case BINDER_TYPE_WEAK_BINDER:
2131 case BINDER_TYPE_HANDLE:
2132 case BINDER_TYPE_WEAK_HANDLE:
2133 object_size = sizeof(struct flat_binder_object);
2134 break;
2135 case BINDER_TYPE_FD:
2136 object_size = sizeof(struct binder_fd_object);
2137 break;
2138 case BINDER_TYPE_PTR:
2139 object_size = sizeof(struct binder_buffer_object);
2140 break;
2141 case BINDER_TYPE_FDA:
2142 object_size = sizeof(struct binder_fd_array_object);
2143 break;
2144 default:
2145 return 0;
2146 }
2147 if (offset <= buffer->data_size - object_size &&
2148 buffer->data_size >= object_size)
2149 return object_size;
2150 else
2151 return 0;
2152 }
2153
2154 /**
2155 * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
2156 * @b: binder_buffer containing the object
2157 * @index: index in offset array at which the binder_buffer_object is
2158 * located
2159 * @start: points to the start of the offset array
2160 * @num_valid: the number of valid offsets in the offset array
2161 *
2162 * Return: If @index is within the valid range of the offset array
2163 * described by @start and @num_valid, and if there's a valid
2164 * binder_buffer_object at the offset found in index @index
2165 * of the offset array, that object is returned. Otherwise,
2166 * %NULL is returned.
2167 * Note that the offset found in index @index itself is not
2168 * verified; this function assumes that @num_valid elements
2169 * from @start were previously verified to have valid offsets.
2170 */
2171 static struct binder_buffer_object *binder_validate_ptr(struct binder_buffer *b,
2172 binder_size_t index,
2173 binder_size_t *start,
2174 binder_size_t num_valid)
2175 {
2176 struct binder_buffer_object *buffer_obj;
2177 binder_size_t *offp;
2178
2179 if (index >= num_valid)
2180 return NULL;
2181
2182 offp = start + index;
2183 buffer_obj = (struct binder_buffer_object *)(b->data + *offp);
2184 if (buffer_obj->hdr.type != BINDER_TYPE_PTR)
2185 return NULL;
2186
2187 return buffer_obj;
2188 }
2189
2190 /**
2191 * binder_validate_fixup() - validates pointer/fd fixups happen in order.
2192 * @b: transaction buffer
2193 * @objects_start start of objects buffer
2194 * @buffer: binder_buffer_object in which to fix up
2195 * @offset: start offset in @buffer to fix up
2196 * @last_obj: last binder_buffer_object that we fixed up in
2197 * @last_min_offset: minimum fixup offset in @last_obj
2198 *
2199 * Return: %true if a fixup in buffer @buffer at offset @offset is
2200 * allowed.
2201 *
2202 * For safety reasons, we only allow fixups inside a buffer to happen
2203 * at increasing offsets; additionally, we only allow fixup on the last
2204 * buffer object that was verified, or one of its parents.
2205 *
2206 * Example of what is allowed:
2207 *
2208 * A
2209 * B (parent = A, offset = 0)
2210 * C (parent = A, offset = 16)
2211 * D (parent = C, offset = 0)
2212 * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
2213 *
2214 * Examples of what is not allowed:
2215 *
2216 * Decreasing offsets within the same parent:
2217 * A
2218 * C (parent = A, offset = 16)
2219 * B (parent = A, offset = 0) // decreasing offset within A
2220 *
2221 * Referring to a parent that wasn't the last object or any of its parents:
2222 * A
2223 * B (parent = A, offset = 0)
2224 * C (parent = A, offset = 0)
2225 * C (parent = A, offset = 16)
2226 * D (parent = B, offset = 0) // B is not A or any of A's parents
2227 */
2228 static bool binder_validate_fixup(struct binder_buffer *b,
2229 binder_size_t *objects_start,
2230 struct binder_buffer_object *buffer,
2231 binder_size_t fixup_offset,
2232 struct binder_buffer_object *last_obj,
2233 binder_size_t last_min_offset)
2234 {
2235 if (!last_obj) {
2236 /* Nothing to fix up in */
2237 return false;
2238 }
2239
2240 while (last_obj != buffer) {
2241 /*
2242 * Safe to retrieve the parent of last_obj, since it
2243 * was already previously verified by the driver.
2244 */
2245 if ((last_obj->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0)
2246 return false;
2247 last_min_offset = last_obj->parent_offset + sizeof(uintptr_t);
2248 last_obj = (struct binder_buffer_object *)
2249 (b->data + *(objects_start + last_obj->parent));
2250 }
2251 return (fixup_offset >= last_min_offset);
2252 }
2253
2254 static void binder_transaction_buffer_release(struct binder_proc *proc,
2255 struct binder_buffer *buffer,
2256 binder_size_t *failed_at)
2257 {
2258 binder_size_t *offp, *off_start, *off_end;
2259 int debug_id = buffer->debug_id;
2260
2261 binder_debug(BINDER_DEBUG_TRANSACTION,
2262 "%d buffer release %d, size %zd-%zd, failed at %p\n",
2263 proc->pid, buffer->debug_id,
2264 buffer->data_size, buffer->offsets_size, failed_at);
2265
2266 if (buffer->target_node)
2267 binder_dec_node(buffer->target_node, 1, 0);
2268
2269 off_start = (binder_size_t *)(buffer->data +
2270 ALIGN(buffer->data_size, sizeof(void *)));
2271 if (failed_at)
2272 off_end = failed_at;
2273 else
2274 off_end = (void *)off_start + buffer->offsets_size;
2275 for (offp = off_start; offp < off_end; offp++) {
2276 struct binder_object_header *hdr;
2277 size_t object_size = binder_validate_object(buffer, *offp);
2278
2279 if (object_size == 0) {
2280 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
2281 debug_id, (u64)*offp, buffer->data_size);
2282 continue;
2283 }
2284 hdr = (struct binder_object_header *)(buffer->data + *offp);
2285 switch (hdr->type) {
2286 case BINDER_TYPE_BINDER:
2287 case BINDER_TYPE_WEAK_BINDER: {
2288 struct flat_binder_object *fp;
2289 struct binder_node *node;
2290
2291 fp = to_flat_binder_object(hdr);
2292 node = binder_get_node(proc, fp->binder);
2293 if (node == NULL) {
2294 pr_err("transaction release %d bad node %016llx\n",
2295 debug_id, (u64)fp->binder);
2296 break;
2297 }
2298 binder_debug(BINDER_DEBUG_TRANSACTION,
2299 " node %d u%016llx\n",
2300 node->debug_id, (u64)node->ptr);
2301 binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER,
2302 0);
2303 binder_put_node(node);
2304 } break;
2305 case BINDER_TYPE_HANDLE:
2306 case BINDER_TYPE_WEAK_HANDLE: {
2307 struct flat_binder_object *fp;
2308 struct binder_ref_data rdata;
2309 int ret;
2310
2311 fp = to_flat_binder_object(hdr);
2312 ret = binder_dec_ref_for_handle(proc, fp->handle,
2313 hdr->type == BINDER_TYPE_HANDLE, &rdata);
2314
2315 if (ret) {
2316 pr_err("transaction release %d bad handle %d, ret = %d\n",
2317 debug_id, fp->handle, ret);
2318 break;
2319 }
2320 binder_debug(BINDER_DEBUG_TRANSACTION,
2321 " ref %d desc %d\n",
2322 rdata.debug_id, rdata.desc);
2323 } break;
2324
2325 case BINDER_TYPE_FD: {
2326 struct binder_fd_object *fp = to_binder_fd_object(hdr);
2327
2328 binder_debug(BINDER_DEBUG_TRANSACTION,
2329 " fd %d\n", fp->fd);
2330 if (failed_at)
2331 task_close_fd(proc, fp->fd);
2332 } break;
2333 case BINDER_TYPE_PTR:
2334 /*
2335 * Nothing to do here, this will get cleaned up when the
2336 * transaction buffer gets freed
2337 */
2338 break;
2339 case BINDER_TYPE_FDA: {
2340 struct binder_fd_array_object *fda;
2341 struct binder_buffer_object *parent;
2342 uintptr_t parent_buffer;
2343 u32 *fd_array;
2344 size_t fd_index;
2345 binder_size_t fd_buf_size;
2346
2347 fda = to_binder_fd_array_object(hdr);
2348 parent = binder_validate_ptr(buffer, fda->parent,
2349 off_start,
2350 offp - off_start);
2351 if (!parent) {
2352 pr_err("transaction release %d bad parent offset",
2353 debug_id);
2354 continue;
2355 }
2356 /*
2357 * Since the parent was already fixed up, convert it
2358 * back to kernel address space to access it
2359 */
2360 parent_buffer = parent->buffer -
2361 binder_alloc_get_user_buffer_offset(
2362 &proc->alloc);
2363
2364 fd_buf_size = sizeof(u32) * fda->num_fds;
2365 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2366 pr_err("transaction release %d invalid number of fds (%lld)\n",
2367 debug_id, (u64)fda->num_fds);
2368 continue;
2369 }
2370 if (fd_buf_size > parent->length ||
2371 fda->parent_offset > parent->length - fd_buf_size) {
2372 /* No space for all file descriptors here. */
2373 pr_err("transaction release %d not enough space for %lld fds in buffer\n",
2374 debug_id, (u64)fda->num_fds);
2375 continue;
2376 }
2377 fd_array = (u32 *)(parent_buffer + (uintptr_t)fda->parent_offset);
2378 for (fd_index = 0; fd_index < fda->num_fds; fd_index++)
2379 task_close_fd(proc, fd_array[fd_index]);
2380 } break;
2381 default:
2382 pr_err("transaction release %d bad object type %x\n",
2383 debug_id, hdr->type);
2384 break;
2385 }
2386 }
2387 }
2388
2389 static int binder_translate_binder(struct flat_binder_object *fp,
2390 struct binder_transaction *t,
2391 struct binder_thread *thread)
2392 {
2393 struct binder_node *node;
2394 struct binder_proc *proc = thread->proc;
2395 struct binder_proc *target_proc = t->to_proc;
2396 struct binder_ref_data rdata;
2397 int ret = 0;
2398
2399 node = binder_get_node(proc, fp->binder);
2400 if (!node) {
2401 node = binder_new_node(proc, fp);
2402 if (!node)
2403 return -ENOMEM;
2404 }
2405 if (fp->cookie != node->cookie) {
2406 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
2407 proc->pid, thread->pid, (u64)fp->binder,
2408 node->debug_id, (u64)fp->cookie,
2409 (u64)node->cookie);
2410 ret = -EINVAL;
2411 goto done;
2412 }
2413 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2414 ret = -EPERM;
2415 goto done;
2416 }
2417
2418 ret = binder_inc_ref_for_node(target_proc, node,
2419 fp->hdr.type == BINDER_TYPE_BINDER,
2420 &thread->todo, &rdata);
2421 if (ret)
2422 goto done;
2423
2424 if (fp->hdr.type == BINDER_TYPE_BINDER)
2425 fp->hdr.type = BINDER_TYPE_HANDLE;
2426 else
2427 fp->hdr.type = BINDER_TYPE_WEAK_HANDLE;
2428 fp->binder = 0;
2429 fp->handle = rdata.desc;
2430 fp->cookie = 0;
2431
2432 trace_binder_transaction_node_to_ref(t, node, &rdata);
2433 binder_debug(BINDER_DEBUG_TRANSACTION,
2434 " node %d u%016llx -> ref %d desc %d\n",
2435 node->debug_id, (u64)node->ptr,
2436 rdata.debug_id, rdata.desc);
2437 done:
2438 binder_put_node(node);
2439 return ret;
2440 }
2441
2442 static int binder_translate_handle(struct flat_binder_object *fp,
2443 struct binder_transaction *t,
2444 struct binder_thread *thread)
2445 {
2446 struct binder_proc *proc = thread->proc;
2447 struct binder_proc *target_proc = t->to_proc;
2448 struct binder_node *node;
2449 struct binder_ref_data src_rdata;
2450 int ret = 0;
2451
2452 node = binder_get_node_from_ref(proc, fp->handle,
2453 fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata);
2454 if (!node) {
2455 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
2456 proc->pid, thread->pid, fp->handle);
2457 return -EINVAL;
2458 }
2459 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2460 ret = -EPERM;
2461 goto done;
2462 }
2463
2464 binder_node_lock(node);
2465 if (node->proc == target_proc) {
2466 if (fp->hdr.type == BINDER_TYPE_HANDLE)
2467 fp->hdr.type = BINDER_TYPE_BINDER;
2468 else
2469 fp->hdr.type = BINDER_TYPE_WEAK_BINDER;
2470 fp->binder = node->ptr;
2471 fp->cookie = node->cookie;
2472 if (node->proc)
2473 binder_inner_proc_lock(node->proc);
2474 binder_inc_node_nilocked(node,
2475 fp->hdr.type == BINDER_TYPE_BINDER,
2476 0, NULL);
2477 if (node->proc)
2478 binder_inner_proc_unlock(node->proc);
2479 trace_binder_transaction_ref_to_node(t, node, &src_rdata);
2480 binder_debug(BINDER_DEBUG_TRANSACTION,
2481 " ref %d desc %d -> node %d u%016llx\n",
2482 src_rdata.debug_id, src_rdata.desc, node->debug_id,
2483 (u64)node->ptr);
2484 binder_node_unlock(node);
2485 } else {
2486 struct binder_ref_data dest_rdata;
2487
2488 binder_node_unlock(node);
2489 ret = binder_inc_ref_for_node(target_proc, node,
2490 fp->hdr.type == BINDER_TYPE_HANDLE,
2491 NULL, &dest_rdata);
2492 if (ret)
2493 goto done;
2494
2495 fp->binder = 0;
2496 fp->handle = dest_rdata.desc;
2497 fp->cookie = 0;
2498 trace_binder_transaction_ref_to_ref(t, node, &src_rdata,
2499 &dest_rdata);
2500 binder_debug(BINDER_DEBUG_TRANSACTION,
2501 " ref %d desc %d -> ref %d desc %d (node %d)\n",
2502 src_rdata.debug_id, src_rdata.desc,
2503 dest_rdata.debug_id, dest_rdata.desc,
2504 node->debug_id);
2505 }
2506 done:
2507 binder_put_node(node);
2508 return ret;
2509 }
2510
2511 static int binder_translate_fd(int fd,
2512 struct binder_transaction *t,
2513 struct binder_thread *thread,
2514 struct binder_transaction *in_reply_to)
2515 {
2516 struct binder_proc *proc = thread->proc;
2517 struct binder_proc *target_proc = t->to_proc;
2518 int target_fd;
2519 struct file *file;
2520 int ret;
2521 bool target_allows_fd;
2522
2523 if (in_reply_to)
2524 target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS);
2525 else
2526 target_allows_fd = t->buffer->target_node->accept_fds;
2527 if (!target_allows_fd) {
2528 binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
2529 proc->pid, thread->pid,
2530 in_reply_to ? "reply" : "transaction",
2531 fd);
2532 ret = -EPERM;
2533 goto err_fd_not_accepted;
2534 }
2535
2536 file = fget(fd);
2537 if (!file) {
2538 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
2539 proc->pid, thread->pid, fd);
2540 ret = -EBADF;
2541 goto err_fget;
2542 }
2543 ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file);
2544 if (ret < 0) {
2545 ret = -EPERM;
2546 goto err_security;
2547 }
2548
2549 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
2550 if (target_fd < 0) {
2551 ret = -ENOMEM;
2552 goto err_get_unused_fd;
2553 }
2554 task_fd_install(target_proc, target_fd, file);
2555 trace_binder_transaction_fd(t, fd, target_fd);
2556 binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n",
2557 fd, target_fd);
2558
2559 return target_fd;
2560
2561 err_get_unused_fd:
2562 err_security:
2563 fput(file);
2564 err_fget:
2565 err_fd_not_accepted:
2566 return ret;
2567 }
2568
2569 static int binder_translate_fd_array(struct binder_fd_array_object *fda,
2570 struct binder_buffer_object *parent,
2571 struct binder_transaction *t,
2572 struct binder_thread *thread,
2573 struct binder_transaction *in_reply_to)
2574 {
2575 binder_size_t fdi, fd_buf_size, num_installed_fds;
2576 int target_fd;
2577 uintptr_t parent_buffer;
2578 u32 *fd_array;
2579 struct binder_proc *proc = thread->proc;
2580 struct binder_proc *target_proc = t->to_proc;
2581
2582 fd_buf_size = sizeof(u32) * fda->num_fds;
2583 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2584 binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
2585 proc->pid, thread->pid, (u64)fda->num_fds);
2586 return -EINVAL;
2587 }
2588 if (fd_buf_size > parent->length ||
2589 fda->parent_offset > parent->length - fd_buf_size) {
2590 /* No space for all file descriptors here. */
2591 binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
2592 proc->pid, thread->pid, (u64)fda->num_fds);
2593 return -EINVAL;
2594 }
2595 /*
2596 * Since the parent was already fixed up, convert it
2597 * back to the kernel address space to access it
2598 */
2599 parent_buffer = parent->buffer -
2600 binder_alloc_get_user_buffer_offset(&target_proc->alloc);
2601 fd_array = (u32 *)(parent_buffer + (uintptr_t)fda->parent_offset);
2602 if (!IS_ALIGNED((unsigned long)fd_array, sizeof(u32))) {
2603 binder_user_error("%d:%d parent offset not aligned correctly.\n",
2604 proc->pid, thread->pid);
2605 return -EINVAL;
2606 }
2607 for (fdi = 0; fdi < fda->num_fds; fdi++) {
2608 target_fd = binder_translate_fd(fd_array[fdi], t, thread,
2609 in_reply_to);
2610 if (target_fd < 0)
2611 goto err_translate_fd_failed;
2612 fd_array[fdi] = target_fd;
2613 }
2614 return 0;
2615
2616 err_translate_fd_failed:
2617 /*
2618 * Failed to allocate fd or security error, free fds
2619 * installed so far.
2620 */
2621 num_installed_fds = fdi;
2622 for (fdi = 0; fdi < num_installed_fds; fdi++)
2623 task_close_fd(target_proc, fd_array[fdi]);
2624 return target_fd;
2625 }
2626
2627 static int binder_fixup_parent(struct binder_transaction *t,
2628 struct binder_thread *thread,
2629 struct binder_buffer_object *bp,
2630 binder_size_t *off_start,
2631 binder_size_t num_valid,
2632 struct binder_buffer_object *last_fixup_obj,
2633 binder_size_t last_fixup_min_off)
2634 {
2635 struct binder_buffer_object *parent;
2636 u8 *parent_buffer;
2637 struct binder_buffer *b = t->buffer;
2638 struct binder_proc *proc = thread->proc;
2639 struct binder_proc *target_proc = t->to_proc;
2640
2641 if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT))
2642 return 0;
2643
2644 parent = binder_validate_ptr(b, bp->parent, off_start, num_valid);
2645 if (!parent) {
2646 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
2647 proc->pid, thread->pid);
2648 return -EINVAL;
2649 }
2650
2651 if (!binder_validate_fixup(b, off_start,
2652 parent, bp->parent_offset,
2653 last_fixup_obj,
2654 last_fixup_min_off)) {
2655 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
2656 proc->pid, thread->pid);
2657 return -EINVAL;
2658 }
2659
2660 if (parent->length < sizeof(binder_uintptr_t) ||
2661 bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) {
2662 /* No space for a pointer here! */
2663 binder_user_error("%d:%d got transaction with invalid parent offset\n",
2664 proc->pid, thread->pid);
2665 return -EINVAL;
2666 }
2667 parent_buffer = (u8 *)((uintptr_t)parent->buffer -
2668 binder_alloc_get_user_buffer_offset(
2669 &target_proc->alloc));
2670 *(binder_uintptr_t *)(parent_buffer + bp->parent_offset) = bp->buffer;
2671
2672 return 0;
2673 }
2674
2675 /**
2676 * binder_proc_transaction() - sends a transaction to a process and wakes it up
2677 * @t: transaction to send
2678 * @proc: process to send the transaction to
2679 * @thread: thread in @proc to send the transaction to (may be NULL)
2680 *
2681 * This function queues a transaction to the specified process. It will try
2682 * to find a thread in the target process to handle the transaction and
2683 * wake it up. If no thread is found, the work is queued to the proc
2684 * waitqueue.
2685 *
2686 * If the @thread parameter is not NULL, the transaction is always queued
2687 * to the waitlist of that specific thread.
2688 *
2689 * Return: true if the transactions was successfully queued
2690 * false if the target process or thread is dead
2691 */
2692 static bool binder_proc_transaction(struct binder_transaction *t,
2693 struct binder_proc *proc,
2694 struct binder_thread *thread)
2695 {
2696 struct list_head *target_list = NULL;
2697 struct binder_node *node = t->buffer->target_node;
2698 struct binder_priority node_prio;
2699 bool oneway = !!(t->flags & TF_ONE_WAY);
2700 bool wakeup = true;
2701
2702 BUG_ON(!node);
2703 binder_node_lock(node);
2704 node_prio.prio = node->min_priority;
2705 node_prio.sched_policy = node->sched_policy;
2706
2707 if (oneway) {
2708 BUG_ON(thread);
2709 if (node->has_async_transaction) {
2710 target_list = &node->async_todo;
2711 wakeup = false;
2712 } else {
2713 node->has_async_transaction = 1;
2714 }
2715 }
2716
2717 binder_inner_proc_lock(proc);
2718
2719 if (proc->is_dead || (thread && thread->is_dead)) {
2720 binder_inner_proc_unlock(proc);
2721 binder_node_unlock(node);
2722 return false;
2723 }
2724
2725 if (!thread && !target_list)
2726 thread = binder_select_thread_ilocked(proc);
2727
2728 if (thread) {
2729 target_list = &thread->todo;
2730 binder_transaction_priority(thread->task, t, node_prio,
2731 node->inherit_rt);
2732 } else if (!target_list) {
2733 target_list = &proc->todo;
2734 } else {
2735 BUG_ON(target_list != &node->async_todo);
2736 }
2737
2738 binder_enqueue_work_ilocked(&t->work, target_list);
2739
2740 if (wakeup)
2741 binder_wakeup_thread_ilocked(proc, thread, !oneway /* sync */);
2742
2743 binder_inner_proc_unlock(proc);
2744 binder_node_unlock(node);
2745
2746 return true;
2747 }
2748
2749 /**
2750 * binder_get_node_refs_for_txn() - Get required refs on node for txn
2751 * @node: struct binder_node for which to get refs
2752 * @proc: returns @node->proc if valid
2753 * @error: if no @proc then returns BR_DEAD_REPLY
2754 *
2755 * User-space normally keeps the node alive when creating a transaction
2756 * since it has a reference to the target. The local strong ref keeps it
2757 * alive if the sending process dies before the target process processes
2758 * the transaction. If the source process is malicious or has a reference
2759 * counting bug, relying on the local strong ref can fail.
2760 *
2761 * Since user-space can cause the local strong ref to go away, we also take
2762 * a tmpref on the node to ensure it survives while we are constructing
2763 * the transaction. We also need a tmpref on the proc while we are
2764 * constructing the transaction, so we take that here as well.
2765 *
2766 * Return: The target_node with refs taken or NULL if no @node->proc is NULL.
2767 * Also sets @proc if valid. If the @node->proc is NULL indicating that the
2768 * target proc has died, @error is set to BR_DEAD_REPLY
2769 */
2770 static struct binder_node *binder_get_node_refs_for_txn(
2771 struct binder_node *node,
2772 struct binder_proc **procp,
2773 uint32_t *error)
2774 {
2775 struct binder_node *target_node = NULL;
2776
2777 binder_node_inner_lock(node);
2778 if (node->proc) {
2779 target_node = node;
2780 binder_inc_node_nilocked(node, 1, 0, NULL);
2781 binder_inc_node_tmpref_ilocked(node);
2782 node->proc->tmp_ref++;
2783 *procp = node->proc;
2784 } else
2785 *error = BR_DEAD_REPLY;
2786 binder_node_inner_unlock(node);
2787
2788 return target_node;
2789 }
2790
2791 static void binder_transaction(struct binder_proc *proc,
2792 struct binder_thread *thread,
2793 struct binder_transaction_data *tr, int reply,
2794 binder_size_t extra_buffers_size)
2795 {
2796 int ret;
2797 struct binder_transaction *t;
2798 struct binder_work *tcomplete;
2799 binder_size_t *offp, *off_end, *off_start;
2800 binder_size_t off_min;
2801 u8 *sg_bufp, *sg_buf_end;
2802 struct binder_proc *target_proc = NULL;
2803 struct binder_thread *target_thread = NULL;
2804 struct binder_node *target_node = NULL;
2805 struct binder_transaction *in_reply_to = NULL;
2806 struct binder_transaction_log_entry *e;
2807 uint32_t return_error = 0;
2808 uint32_t return_error_param = 0;
2809 uint32_t return_error_line = 0;
2810 struct binder_buffer_object *last_fixup_obj = NULL;
2811 binder_size_t last_fixup_min_off = 0;
2812 struct binder_context *context = proc->context;
2813 int t_debug_id = atomic_inc_return(&binder_last_id);
2814
2815 e = binder_transaction_log_add(&binder_transaction_log);
2816 e->debug_id = t_debug_id;
2817 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
2818 e->from_proc = proc->pid;
2819 e->from_thread = thread->pid;
2820 e->target_handle = tr->target.handle;
2821 e->data_size = tr->data_size;
2822 e->offsets_size = tr->offsets_size;
2823 e->context_name = proc->context->name;
2824
2825 if (reply) {
2826 binder_inner_proc_lock(proc);
2827 in_reply_to = thread->transaction_stack;
2828 if (in_reply_to == NULL) {
2829 binder_inner_proc_unlock(proc);
2830 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
2831 proc->pid, thread->pid);
2832 return_error = BR_FAILED_REPLY;
2833 return_error_param = -EPROTO;
2834 return_error_line = __LINE__;
2835 goto err_empty_call_stack;
2836 }
2837 if (in_reply_to->to_thread != thread) {
2838 spin_lock(&in_reply_to->lock);
2839 binder_user_error("%d:%d got reply transaction with bad transaction stack, transaction %d has target %d:%d\n",
2840 proc->pid, thread->pid, in_reply_to->debug_id,
2841 in_reply_to->to_proc ?
2842 in_reply_to->to_proc->pid : 0,
2843 in_reply_to->to_thread ?
2844 in_reply_to->to_thread->pid : 0);
2845 spin_unlock(&in_reply_to->lock);
2846 binder_inner_proc_unlock(proc);
2847 return_error = BR_FAILED_REPLY;
2848 return_error_param = -EPROTO;
2849 return_error_line = __LINE__;
2850 in_reply_to = NULL;
2851 goto err_bad_call_stack;
2852 }
2853 thread->transaction_stack = in_reply_to->to_parent;
2854 binder_inner_proc_unlock(proc);
2855 target_thread = binder_get_txn_from_and_acq_inner(in_reply_to);
2856 if (target_thread == NULL) {
2857 return_error = BR_DEAD_REPLY;
2858 return_error_line = __LINE__;
2859 goto err_dead_binder;
2860 }
2861 if (target_thread->transaction_stack != in_reply_to) {
2862 binder_user_error("%d:%d got reply transaction with bad target transaction stack %d, expected %d\n",
2863 proc->pid, thread->pid,
2864 target_thread->transaction_stack ?
2865 target_thread->transaction_stack->debug_id : 0,
2866 in_reply_to->debug_id);
2867 binder_inner_proc_unlock(target_thread->proc);
2868 return_error = BR_FAILED_REPLY;
2869 return_error_param = -EPROTO;
2870 return_error_line = __LINE__;
2871 in_reply_to = NULL;
2872 target_thread = NULL;
2873 goto err_dead_binder;
2874 }
2875 target_proc = target_thread->proc;
2876 target_proc->tmp_ref++;
2877 binder_inner_proc_unlock(target_thread->proc);
2878 } else {
2879 if (tr->target.handle) {
2880 struct binder_ref *ref;
2881
2882 /*
2883 * There must already be a strong ref
2884 * on this node. If so, do a strong
2885 * increment on the node to ensure it
2886 * stays alive until the transaction is
2887 * done.
2888 */
2889 binder_proc_lock(proc);
2890 ref = binder_get_ref_olocked(proc, tr->target.handle,
2891 true);
2892 if (ref) {
2893 target_node = binder_get_node_refs_for_txn(
2894 ref->node, &target_proc,
2895 &return_error);
2896 } else {
2897 binder_user_error("%d:%d got transaction to invalid handle\n",
2898 proc->pid, thread->pid);
2899 return_error = BR_FAILED_REPLY;
2900 }
2901 binder_proc_unlock(proc);
2902 } else {
2903 mutex_lock(&context->context_mgr_node_lock);
2904 target_node = context->binder_context_mgr_node;
2905 if (target_node)
2906 target_node = binder_get_node_refs_for_txn(
2907 target_node, &target_proc,
2908 &return_error);
2909 else
2910 return_error = BR_DEAD_REPLY;
2911 mutex_unlock(&context->context_mgr_node_lock);
2912 }
2913 if (!target_node) {
2914 /*
2915 * return_error is set above
2916 */
2917 return_error_param = -EINVAL;
2918 return_error_line = __LINE__;
2919 goto err_dead_binder;
2920 }
2921 e->to_node = target_node->debug_id;
2922 if (security_binder_transaction(proc->tsk,
2923 target_proc->tsk) < 0) {
2924 return_error = BR_FAILED_REPLY;
2925 return_error_param = -EPERM;
2926 return_error_line = __LINE__;
2927 goto err_invalid_target_handle;
2928 }
2929 binder_inner_proc_lock(proc);
2930 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
2931 struct binder_transaction *tmp;
2932
2933 tmp = thread->transaction_stack;
2934 if (tmp->to_thread != thread) {
2935 spin_lock(&tmp->lock);
2936 binder_user_error("%d:%d got new transaction with bad transaction stack, transaction %d has target %d:%d\n",
2937 proc->pid, thread->pid, tmp->debug_id,
2938 tmp->to_proc ? tmp->to_proc->pid : 0,
2939 tmp->to_thread ?
2940 tmp->to_thread->pid : 0);
2941 spin_unlock(&tmp->lock);
2942 binder_inner_proc_unlock(proc);
2943 return_error = BR_FAILED_REPLY;
2944 return_error_param = -EPROTO;
2945 return_error_line = __LINE__;
2946 goto err_bad_call_stack;
2947 }
2948 while (tmp) {
2949 struct binder_thread *from;
2950
2951 spin_lock(&tmp->lock);
2952 from = tmp->from;
2953 if (from && from->proc == target_proc) {
2954 atomic_inc(&from->tmp_ref);
2955 target_thread = from;
2956 spin_unlock(&tmp->lock);
2957 break;
2958 }
2959 spin_unlock(&tmp->lock);
2960 tmp = tmp->from_parent;
2961 }
2962 }
2963 binder_inner_proc_unlock(proc);
2964 }
2965 if (target_thread)
2966 e->to_thread = target_thread->pid;
2967 e->to_proc = target_proc->pid;
2968
2969 /* TODO: reuse incoming transaction for reply */
2970 t = kzalloc(sizeof(*t), GFP_KERNEL);
2971 if (t == NULL) {
2972 return_error = BR_FAILED_REPLY;
2973 return_error_param = -ENOMEM;
2974 return_error_line = __LINE__;
2975 goto err_alloc_t_failed;
2976 }
2977 binder_stats_created(BINDER_STAT_TRANSACTION);
2978 spin_lock_init(&t->lock);
2979
2980 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
2981 if (tcomplete == NULL) {
2982 return_error = BR_FAILED_REPLY;
2983 return_error_param = -ENOMEM;
2984 return_error_line = __LINE__;
2985 goto err_alloc_tcomplete_failed;
2986 }
2987 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
2988
2989 t->debug_id = t_debug_id;
2990
2991 if (reply)
2992 binder_debug(BINDER_DEBUG_TRANSACTION,
2993 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
2994 proc->pid, thread->pid, t->debug_id,
2995 target_proc->pid, target_thread->pid,
2996 (u64)tr->data.ptr.buffer,
2997 (u64)tr->data.ptr.offsets,
2998 (u64)tr->data_size, (u64)tr->offsets_size,
2999 (u64)extra_buffers_size);
3000 else
3001 binder_debug(BINDER_DEBUG_TRANSACTION,
3002 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
3003 proc->pid, thread->pid, t->debug_id,
3004 target_proc->pid, target_node->debug_id,
3005 (u64)tr->data.ptr.buffer,
3006 (u64)tr->data.ptr.offsets,
3007 (u64)tr->data_size, (u64)tr->offsets_size,
3008 (u64)extra_buffers_size);
3009
3010 if (!reply && !(tr->flags & TF_ONE_WAY))
3011 t->from = thread;
3012 else
3013 t->from = NULL;
3014 t->sender_euid = task_euid(proc->tsk);
3015 t->to_proc = target_proc;
3016 t->to_thread = target_thread;
3017 t->code = tr->code;
3018 t->flags = tr->flags;
3019 if (!(t->flags & TF_ONE_WAY) &&
3020 binder_supported_policy(current->policy)) {
3021 /* Inherit supported policies for synchronous transactions */
3022 t->priority.sched_policy = current->policy;
3023 t->priority.prio = current->normal_prio;
3024 } else {
3025 /* Otherwise, fall back to the default priority */
3026 t->priority = target_proc->default_priority;
3027 }
3028
3029 trace_binder_transaction(reply, t, target_node);
3030
3031 t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size,
3032 tr->offsets_size, extra_buffers_size,
3033 !reply && (t->flags & TF_ONE_WAY));
3034 if (IS_ERR(t->buffer)) {
3035 /*
3036 * -ESRCH indicates VMA cleared. The target is dying.
3037 */
3038 return_error_param = PTR_ERR(t->buffer);
3039 return_error = return_error_param == -ESRCH ?
3040 BR_DEAD_REPLY : BR_FAILED_REPLY;
3041 return_error_line = __LINE__;
3042 t->buffer = NULL;
3043 goto err_binder_alloc_buf_failed;
3044 }
3045 t->buffer->allow_user_free = 0;
3046 t->buffer->debug_id = t->debug_id;
3047 t->buffer->transaction = t;
3048 t->buffer->target_node = target_node;
3049 trace_binder_transaction_alloc_buf(t->buffer);
3050 off_start = (binder_size_t *)(t->buffer->data +
3051 ALIGN(tr->data_size, sizeof(void *)));
3052 offp = off_start;
3053
3054 if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t)
3055 tr->data.ptr.buffer, tr->data_size)) {
3056 binder_user_error("%d:%d got transaction with invalid data ptr\n",
3057 proc->pid, thread->pid);
3058 return_error = BR_FAILED_REPLY;
3059 return_error_param = -EFAULT;
3060 return_error_line = __LINE__;
3061 goto err_copy_data_failed;
3062 }
3063 if (copy_from_user(offp, (const void __user *)(uintptr_t)
3064 tr->data.ptr.offsets, tr->offsets_size)) {
3065 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3066 proc->pid, thread->pid);
3067 return_error = BR_FAILED_REPLY;
3068 return_error_param = -EFAULT;
3069 return_error_line = __LINE__;
3070 goto err_copy_data_failed;
3071 }
3072 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
3073 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
3074 proc->pid, thread->pid, (u64)tr->offsets_size);
3075 return_error = BR_FAILED_REPLY;
3076 return_error_param = -EINVAL;
3077 return_error_line = __LINE__;
3078 goto err_bad_offset;
3079 }
3080 if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) {
3081 binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
3082 proc->pid, thread->pid,
3083 (u64)extra_buffers_size);
3084 return_error = BR_FAILED_REPLY;
3085 return_error_param = -EINVAL;
3086 return_error_line = __LINE__;
3087 goto err_bad_offset;
3088 }
3089 off_end = (void *)off_start + tr->offsets_size;
3090 sg_bufp = (u8 *)(PTR_ALIGN(off_end, sizeof(void *)));
3091 sg_buf_end = sg_bufp + extra_buffers_size;
3092 off_min = 0;
3093 for (; offp < off_end; offp++) {
3094 struct binder_object_header *hdr;
3095 size_t object_size = binder_validate_object(t->buffer, *offp);
3096
3097 if (object_size == 0 || *offp < off_min) {
3098 binder_user_error("%d:%d got transaction with invalid offset (%lld, min %lld max %lld) or object.\n",
3099 proc->pid, thread->pid, (u64)*offp,
3100 (u64)off_min,
3101 (u64)t->buffer->data_size);
3102 return_error = BR_FAILED_REPLY;
3103 return_error_param = -EINVAL;
3104 return_error_line = __LINE__;
3105 goto err_bad_offset;
3106 }
3107
3108 hdr = (struct binder_object_header *)(t->buffer->data + *offp);
3109 off_min = *offp + object_size;
3110 switch (hdr->type) {
3111 case BINDER_TYPE_BINDER:
3112 case BINDER_TYPE_WEAK_BINDER: {
3113 struct flat_binder_object *fp;
3114
3115 fp = to_flat_binder_object(hdr);
3116 ret = binder_translate_binder(fp, t, thread);
3117 if (ret < 0) {
3118 return_error = BR_FAILED_REPLY;
3119 return_error_param = ret;
3120 return_error_line = __LINE__;
3121 goto err_translate_failed;
3122 }
3123 } break;
3124 case BINDER_TYPE_HANDLE:
3125 case BINDER_TYPE_WEAK_HANDLE: {
3126 struct flat_binder_object *fp;
3127
3128 fp = to_flat_binder_object(hdr);
3129 ret = binder_translate_handle(fp, t, thread);
3130 if (ret < 0) {
3131 return_error = BR_FAILED_REPLY;
3132 return_error_param = ret;
3133 return_error_line = __LINE__;
3134 goto err_translate_failed;
3135 }
3136 } break;
3137
3138 case BINDER_TYPE_FD: {
3139 struct binder_fd_object *fp = to_binder_fd_object(hdr);
3140 int target_fd = binder_translate_fd(fp->fd, t, thread,
3141 in_reply_to);
3142
3143 if (target_fd < 0) {
3144 return_error = BR_FAILED_REPLY;
3145 return_error_param = target_fd;
3146 return_error_line = __LINE__;
3147 goto err_translate_failed;
3148 }
3149 fp->pad_binder = 0;
3150 fp->fd = target_fd;
3151 } break;
3152 case BINDER_TYPE_FDA: {
3153 struct binder_fd_array_object *fda =
3154 to_binder_fd_array_object(hdr);
3155 struct binder_buffer_object *parent =
3156 binder_validate_ptr(t->buffer, fda->parent,
3157 off_start,
3158 offp - off_start);
3159 if (!parent) {
3160 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
3161 proc->pid, thread->pid);
3162 return_error = BR_FAILED_REPLY;
3163 return_error_param = -EINVAL;
3164 return_error_line = __LINE__;
3165 goto err_bad_parent;
3166 }
3167 if (!binder_validate_fixup(t->buffer, off_start,
3168 parent, fda->parent_offset,
3169 last_fixup_obj,
3170 last_fixup_min_off)) {
3171 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
3172 proc->pid, thread->pid);
3173 return_error = BR_FAILED_REPLY;
3174 return_error_param = -EINVAL;
3175 return_error_line = __LINE__;
3176 goto err_bad_parent;
3177 }
3178 ret = binder_translate_fd_array(fda, parent, t, thread,
3179 in_reply_to);
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;
3185 }
3186 last_fixup_obj = parent;
3187 last_fixup_min_off =
3188 fda->parent_offset + sizeof(u32) * fda->num_fds;
3189 } break;
3190 case BINDER_TYPE_PTR: {
3191 struct binder_buffer_object *bp =
3192 to_binder_buffer_object(hdr);
3193 size_t buf_left = sg_buf_end - sg_bufp;
3194
3195 if (bp->length > buf_left) {
3196 binder_user_error("%d:%d got transaction with too large buffer\n",
3197 proc->pid, thread->pid);
3198 return_error = BR_FAILED_REPLY;
3199 return_error_param = -EINVAL;
3200 return_error_line = __LINE__;
3201 goto err_bad_offset;
3202 }
3203 if (copy_from_user(sg_bufp,
3204 (const void __user *)(uintptr_t)
3205 bp->buffer, bp->length)) {
3206 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3207 proc->pid, thread->pid);
3208 return_error_param = -EFAULT;
3209 return_error = BR_FAILED_REPLY;
3210 return_error_line = __LINE__;
3211 goto err_copy_data_failed;
3212 }
3213 /* Fixup buffer pointer to target proc address space */
3214 bp->buffer = (uintptr_t)sg_bufp +
3215 binder_alloc_get_user_buffer_offset(
3216 &target_proc->alloc);
3217 sg_bufp += ALIGN(bp->length, sizeof(u64));
3218
3219 ret = binder_fixup_parent(t, thread, bp, off_start,
3220 offp - off_start,
3221 last_fixup_obj,
3222 last_fixup_min_off);
3223 if (ret < 0) {
3224 return_error = BR_FAILED_REPLY;
3225 return_error_param = ret;
3226 return_error_line = __LINE__;
3227 goto err_translate_failed;
3228 }
3229 last_fixup_obj = bp;
3230 last_fixup_min_off = 0;
3231 } break;
3232 default:
3233 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
3234 proc->pid, thread->pid, hdr->type);
3235 return_error = BR_FAILED_REPLY;
3236 return_error_param = -EINVAL;
3237 return_error_line = __LINE__;
3238 goto err_bad_object_type;
3239 }
3240 }
3241 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
3242 binder_enqueue_work(proc, tcomplete, &thread->todo);
3243 t->work.type = BINDER_WORK_TRANSACTION;
3244
3245 if (reply) {
3246 binder_inner_proc_lock(target_proc);
3247 if (target_thread->is_dead) {
3248 binder_inner_proc_unlock(target_proc);
3249 goto err_dead_proc_or_thread;
3250 }
3251 BUG_ON(t->buffer->async_transaction != 0);
3252 binder_pop_transaction_ilocked(target_thread, in_reply_to);
3253 binder_enqueue_work_ilocked(&t->work, &target_thread->todo);
3254 binder_inner_proc_unlock(target_proc);
3255 wake_up_interruptible_sync(&target_thread->wait);
3256 binder_restore_priority(current, in_reply_to->saved_priority);
3257 binder_free_transaction(in_reply_to);
3258 } else if (!(t->flags & TF_ONE_WAY)) {
3259 BUG_ON(t->buffer->async_transaction != 0);
3260 binder_inner_proc_lock(proc);
3261 t->need_reply = 1;
3262 t->from_parent = thread->transaction_stack;
3263 thread->transaction_stack = t;
3264 binder_inner_proc_unlock(proc);
3265 if (!binder_proc_transaction(t, target_proc, target_thread)) {
3266 binder_inner_proc_lock(proc);
3267 binder_pop_transaction_ilocked(thread, t);
3268 binder_inner_proc_unlock(proc);
3269 goto err_dead_proc_or_thread;
3270 }
3271 } else {
3272 BUG_ON(target_node == NULL);
3273 BUG_ON(t->buffer->async_transaction != 1);
3274 if (!binder_proc_transaction(t, target_proc, NULL))
3275 goto err_dead_proc_or_thread;
3276 }
3277 if (target_thread)
3278 binder_thread_dec_tmpref(target_thread);
3279 binder_proc_dec_tmpref(target_proc);
3280 if (target_node)
3281 binder_dec_node_tmpref(target_node);
3282 /*
3283 * write barrier to synchronize with initialization
3284 * of log entry
3285 */
3286 smp_wmb();
3287 WRITE_ONCE(e->debug_id_done, t_debug_id);
3288 return;
3289
3290 err_dead_proc_or_thread:
3291 return_error = BR_DEAD_REPLY;
3292 return_error_line = __LINE__;
3293 binder_dequeue_work(proc, tcomplete);
3294 err_translate_failed:
3295 err_bad_object_type:
3296 err_bad_offset:
3297 err_bad_parent:
3298 err_copy_data_failed:
3299 trace_binder_transaction_failed_buffer_release(t->buffer);
3300 binder_transaction_buffer_release(target_proc, t->buffer, offp);
3301 if (target_node)
3302 binder_dec_node_tmpref(target_node);
3303 target_node = NULL;
3304 t->buffer->transaction = NULL;
3305 binder_alloc_free_buf(&target_proc->alloc, t->buffer);
3306 err_binder_alloc_buf_failed:
3307 kfree(tcomplete);
3308 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3309 err_alloc_tcomplete_failed:
3310 kfree(t);
3311 binder_stats_deleted(BINDER_STAT_TRANSACTION);
3312 err_alloc_t_failed:
3313 err_bad_call_stack:
3314 err_empty_call_stack:
3315 err_dead_binder:
3316 err_invalid_target_handle:
3317 if (target_thread)
3318 binder_thread_dec_tmpref(target_thread);
3319 if (target_proc)
3320 binder_proc_dec_tmpref(target_proc);
3321 if (target_node) {
3322 binder_dec_node(target_node, 1, 0);
3323 binder_dec_node_tmpref(target_node);
3324 }
3325
3326 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
3327 "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
3328 proc->pid, thread->pid, return_error, return_error_param,
3329 (u64)tr->data_size, (u64)tr->offsets_size,
3330 return_error_line);
3331
3332 {
3333 struct binder_transaction_log_entry *fe;
3334
3335 e->return_error = return_error;
3336 e->return_error_param = return_error_param;
3337 e->return_error_line = return_error_line;
3338 fe = binder_transaction_log_add(&binder_transaction_log_failed);
3339 *fe = *e;
3340 /*
3341 * write barrier to synchronize with initialization
3342 * of log entry
3343 */
3344 smp_wmb();
3345 WRITE_ONCE(e->debug_id_done, t_debug_id);
3346 WRITE_ONCE(fe->debug_id_done, t_debug_id);
3347 }
3348
3349 BUG_ON(thread->return_error.cmd != BR_OK);
3350 if (in_reply_to) {
3351 binder_restore_priority(current, in_reply_to->saved_priority);
3352 thread->return_error.cmd = BR_TRANSACTION_COMPLETE;
3353 binder_enqueue_work(thread->proc,
3354 &thread->return_error.work,
3355 &thread->todo);
3356 binder_send_failed_reply(in_reply_to, return_error);
3357 } else {
3358 thread->return_error.cmd = return_error;
3359 binder_enqueue_work(thread->proc,
3360 &thread->return_error.work,
3361 &thread->todo);
3362 }
3363 }
3364
3365 static int binder_thread_write(struct binder_proc *proc,
3366 struct binder_thread *thread,
3367 binder_uintptr_t binder_buffer, size_t size,
3368 binder_size_t *consumed)
3369 {
3370 uint32_t cmd;
3371 struct binder_context *context = proc->context;
3372 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
3373 void __user *ptr = buffer + *consumed;
3374 void __user *end = buffer + size;
3375
3376 while (ptr < end && thread->return_error.cmd == BR_OK) {
3377 int ret;
3378
3379 if (get_user(cmd, (uint32_t __user *)ptr))
3380 return -EFAULT;
3381 ptr += sizeof(uint32_t);
3382 trace_binder_command(cmd);
3383 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
3384 atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]);
3385 atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]);
3386 atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]);
3387 }
3388 switch (cmd) {
3389 case BC_INCREFS:
3390 case BC_ACQUIRE:
3391 case BC_RELEASE:
3392 case BC_DECREFS: {
3393 uint32_t target;
3394 const char *debug_string;
3395 bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE;
3396 bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE;
3397 struct binder_ref_data rdata;
3398
3399 if (get_user(target, (uint32_t __user *)ptr))
3400 return -EFAULT;
3401
3402 ptr += sizeof(uint32_t);
3403 ret = -1;
3404 if (increment && !target) {
3405 struct binder_node *ctx_mgr_node;
3406 mutex_lock(&context->context_mgr_node_lock);
3407 ctx_mgr_node = context->binder_context_mgr_node;
3408 if (ctx_mgr_node)
3409 ret = binder_inc_ref_for_node(
3410 proc, ctx_mgr_node,
3411 strong, NULL, &rdata);
3412 mutex_unlock(&context->context_mgr_node_lock);
3413 }
3414 if (ret)
3415 ret = binder_update_ref_for_handle(
3416 proc, target, increment, strong,
3417 &rdata);
3418 if (!ret && rdata.desc != target) {
3419 binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
3420 proc->pid, thread->pid,
3421 target, rdata.desc);
3422 }
3423 switch (cmd) {
3424 case BC_INCREFS:
3425 debug_string = "IncRefs";
3426 break;
3427 case BC_ACQUIRE:
3428 debug_string = "Acquire";
3429 break;
3430 case BC_RELEASE:
3431 debug_string = "Release";
3432 break;
3433 case BC_DECREFS:
3434 default:
3435 debug_string = "DecRefs";
3436 break;
3437 }
3438 if (ret) {
3439 binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
3440 proc->pid, thread->pid, debug_string,
3441 strong, target, ret);
3442 break;
3443 }
3444 binder_debug(BINDER_DEBUG_USER_REFS,
3445 "%d:%d %s ref %d desc %d s %d w %d\n",
3446 proc->pid, thread->pid, debug_string,
3447 rdata.debug_id, rdata.desc, rdata.strong,
3448 rdata.weak);
3449 break;
3450 }
3451 case BC_INCREFS_DONE:
3452 case BC_ACQUIRE_DONE: {
3453 binder_uintptr_t node_ptr;
3454 binder_uintptr_t cookie;
3455 struct binder_node *node;
3456 bool free_node;
3457
3458 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
3459 return -EFAULT;
3460 ptr += sizeof(binder_uintptr_t);
3461 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
3462 return -EFAULT;
3463 ptr += sizeof(binder_uintptr_t);
3464 node = binder_get_node(proc, node_ptr);
3465 if (node == NULL) {
3466 binder_user_error("%d:%d %s u%016llx no match\n",
3467 proc->pid, thread->pid,
3468 cmd == BC_INCREFS_DONE ?
3469 "BC_INCREFS_DONE" :
3470 "BC_ACQUIRE_DONE",
3471 (u64)node_ptr);
3472 break;
3473 }
3474 if (cookie != node->cookie) {
3475 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
3476 proc->pid, thread->pid,
3477 cmd == BC_INCREFS_DONE ?
3478 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
3479 (u64)node_ptr, node->debug_id,
3480 (u64)cookie, (u64)node->cookie);
3481 binder_put_node(node);
3482 break;
3483 }
3484 binder_node_inner_lock(node);
3485 if (cmd == BC_ACQUIRE_DONE) {
3486 if (node->pending_strong_ref == 0) {
3487 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
3488 proc->pid, thread->pid,
3489 node->debug_id);
3490 binder_node_inner_unlock(node);
3491 binder_put_node(node);
3492 break;
3493 }
3494 node->pending_strong_ref = 0;
3495 } else {
3496 if (node->pending_weak_ref == 0) {
3497 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
3498 proc->pid, thread->pid,
3499 node->debug_id);
3500 binder_node_inner_unlock(node);
3501 binder_put_node(node);
3502 break;
3503 }
3504 node->pending_weak_ref = 0;
3505 }
3506 free_node = binder_dec_node_nilocked(node,
3507 cmd == BC_ACQUIRE_DONE, 0);
3508 WARN_ON(free_node);
3509 binder_debug(BINDER_DEBUG_USER_REFS,
3510 "%d:%d %s node %d ls %d lw %d tr %d\n",
3511 proc->pid, thread->pid,
3512 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
3513 node->debug_id, node->local_strong_refs,
3514 node->local_weak_refs, node->tmp_refs);
3515 binder_node_inner_unlock(node);
3516 binder_put_node(node);
3517 break;
3518 }
3519 case BC_ATTEMPT_ACQUIRE:
3520 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
3521 return -EINVAL;
3522 case BC_ACQUIRE_RESULT:
3523 pr_err("BC_ACQUIRE_RESULT not supported\n");
3524 return -EINVAL;
3525
3526 case BC_FREE_BUFFER: {
3527 binder_uintptr_t data_ptr;
3528 struct binder_buffer *buffer;
3529
3530 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
3531 return -EFAULT;
3532 ptr += sizeof(binder_uintptr_t);
3533
3534 buffer = binder_alloc_prepare_to_free(&proc->alloc,
3535 data_ptr);
3536 if (buffer == NULL) {
3537 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
3538 proc->pid, thread->pid, (u64)data_ptr);
3539 break;
3540 }
3541 if (!buffer->allow_user_free) {
3542 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
3543 proc->pid, thread->pid, (u64)data_ptr);
3544 break;
3545 }
3546 binder_debug(BINDER_DEBUG_FREE_BUFFER,
3547 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
3548 proc->pid, thread->pid, (u64)data_ptr,
3549 buffer->debug_id,
3550 buffer->transaction ? "active" : "finished");
3551
3552 if (buffer->transaction) {
3553 buffer->transaction->buffer = NULL;
3554 buffer->transaction = NULL;
3555 }
3556 if (buffer->async_transaction && buffer->target_node) {
3557 struct binder_node *buf_node;
3558 struct binder_work *w;
3559
3560 buf_node = buffer->target_node;
3561 binder_node_inner_lock(buf_node);
3562 BUG_ON(!buf_node->has_async_transaction);
3563 BUG_ON(buf_node->proc != proc);
3564 w = binder_dequeue_work_head_ilocked(
3565 &buf_node->async_todo);
3566 if (!w) {
3567 buf_node->has_async_transaction = 0;
3568 } else {
3569 binder_enqueue_work_ilocked(
3570 w, &proc->todo);
3571 binder_wakeup_proc_ilocked(proc);
3572 }
3573 binder_node_inner_unlock(buf_node);
3574 }
3575 trace_binder_transaction_buffer_release(buffer);
3576 binder_transaction_buffer_release(proc, buffer, NULL);
3577 binder_alloc_free_buf(&proc->alloc, buffer);
3578 break;
3579 }
3580
3581 case BC_TRANSACTION_SG:
3582 case BC_REPLY_SG: {
3583 struct binder_transaction_data_sg tr;
3584
3585 if (copy_from_user(&tr, ptr, sizeof(tr)))
3586 return -EFAULT;
3587 ptr += sizeof(tr);
3588 binder_transaction(proc, thread, &tr.transaction_data,
3589 cmd == BC_REPLY_SG, tr.buffers_size);
3590 break;
3591 }
3592 case BC_TRANSACTION:
3593 case BC_REPLY: {
3594 struct binder_transaction_data tr;
3595
3596 if (copy_from_user(&tr, ptr, sizeof(tr)))
3597 return -EFAULT;
3598 ptr += sizeof(tr);
3599 binder_transaction(proc, thread, &tr,
3600 cmd == BC_REPLY, 0);
3601 break;
3602 }
3603
3604 case BC_REGISTER_LOOPER:
3605 binder_debug(BINDER_DEBUG_THREADS,
3606 "%d:%d BC_REGISTER_LOOPER\n",
3607 proc->pid, thread->pid);
3608 binder_inner_proc_lock(proc);
3609 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
3610 thread->looper |= BINDER_LOOPER_STATE_INVALID;
3611 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
3612 proc->pid, thread->pid);
3613 } else if (proc->requested_threads == 0) {
3614 thread->looper |= BINDER_LOOPER_STATE_INVALID;
3615 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
3616 proc->pid, thread->pid);
3617 } else {
3618 proc->requested_threads--;
3619 proc->requested_threads_started++;
3620 }
3621 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
3622 binder_inner_proc_unlock(proc);
3623 break;
3624 case BC_ENTER_LOOPER:
3625 binder_debug(BINDER_DEBUG_THREADS,
3626 "%d:%d BC_ENTER_LOOPER\n",
3627 proc->pid, thread->pid);
3628 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
3629 thread->looper |= BINDER_LOOPER_STATE_INVALID;
3630 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
3631 proc->pid, thread->pid);
3632 }
3633 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
3634 break;
3635 case BC_EXIT_LOOPER:
3636 binder_debug(BINDER_DEBUG_THREADS,
3637 "%d:%d BC_EXIT_LOOPER\n",
3638 proc->pid, thread->pid);
3639 thread->looper |= BINDER_LOOPER_STATE_EXITED;
3640 break;
3641
3642 case BC_REQUEST_DEATH_NOTIFICATION:
3643 case BC_CLEAR_DEATH_NOTIFICATION: {
3644 uint32_t target;
3645 binder_uintptr_t cookie;
3646 struct binder_ref *ref;
3647 struct binder_ref_death *death = NULL;
3648
3649 if (get_user(target, (uint32_t __user *)ptr))
3650 return -EFAULT;
3651 ptr += sizeof(uint32_t);
3652 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
3653 return -EFAULT;
3654 ptr += sizeof(binder_uintptr_t);
3655 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3656 /*
3657 * Allocate memory for death notification
3658 * before taking lock
3659 */
3660 death = kzalloc(sizeof(*death), GFP_KERNEL);
3661 if (death == NULL) {
3662 WARN_ON(thread->return_error.cmd !=
3663 BR_OK);
3664 thread->return_error.cmd = BR_ERROR;
3665 binder_enqueue_work(
3666 thread->proc,
3667 &thread->return_error.work,
3668 &thread->todo);
3669 binder_debug(
3670 BINDER_DEBUG_FAILED_TRANSACTION,
3671 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
3672 proc->pid, thread->pid);
3673 break;
3674 }
3675 }
3676 binder_proc_lock(proc);
3677 ref = binder_get_ref_olocked(proc, target, false);
3678 if (ref == NULL) {
3679 binder_user_error("%d:%d %s invalid ref %d\n",
3680 proc->pid, thread->pid,
3681 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3682 "BC_REQUEST_DEATH_NOTIFICATION" :
3683 "BC_CLEAR_DEATH_NOTIFICATION",
3684 target);
3685 binder_proc_unlock(proc);
3686 kfree(death);
3687 break;
3688 }
3689
3690 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
3691 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
3692 proc->pid, thread->pid,
3693 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3694 "BC_REQUEST_DEATH_NOTIFICATION" :
3695 "BC_CLEAR_DEATH_NOTIFICATION",
3696 (u64)cookie, ref->data.debug_id,
3697 ref->data.desc, ref->data.strong,
3698 ref->data.weak, ref->node->debug_id);
3699
3700 binder_node_lock(ref->node);
3701 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3702 if (ref->death) {
3703 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
3704 proc->pid, thread->pid);
3705 binder_node_unlock(ref->node);
3706 binder_proc_unlock(proc);
3707 kfree(death);
3708 break;
3709 }
3710 binder_stats_created(BINDER_STAT_DEATH);
3711 INIT_LIST_HEAD(&death->work.entry);
3712 death->cookie = cookie;
3713 ref->death = death;
3714 if (ref->node->proc == NULL) {
3715 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
3716
3717 binder_inner_proc_lock(proc);
3718 binder_enqueue_work_ilocked(
3719 &ref->death->work, &proc->todo);
3720 binder_wakeup_proc_ilocked(proc);
3721 binder_inner_proc_unlock(proc);
3722 }
3723 } else {
3724 if (ref->death == NULL) {
3725 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
3726 proc->pid, thread->pid);
3727 binder_node_unlock(ref->node);
3728 binder_proc_unlock(proc);
3729 break;
3730 }
3731 death = ref->death;
3732 if (death->cookie != cookie) {
3733 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
3734 proc->pid, thread->pid,
3735 (u64)death->cookie,
3736 (u64)cookie);
3737 binder_node_unlock(ref->node);
3738 binder_proc_unlock(proc);
3739 break;
3740 }
3741 ref->death = NULL;
3742 binder_inner_proc_lock(proc);
3743 if (list_empty(&death->work.entry)) {
3744 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
3745 if (thread->looper &
3746 (BINDER_LOOPER_STATE_REGISTERED |
3747 BINDER_LOOPER_STATE_ENTERED))
3748 binder_enqueue_work_ilocked(
3749 &death->work,
3750 &thread->todo);
3751 else {
3752 binder_enqueue_work_ilocked(
3753 &death->work,
3754 &proc->todo);
3755 binder_wakeup_proc_ilocked(
3756 proc);
3757 }
3758 } else {
3759 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
3760 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
3761 }
3762 binder_inner_proc_unlock(proc);
3763 }
3764 binder_node_unlock(ref->node);
3765 binder_proc_unlock(proc);
3766 } break;
3767 case BC_DEAD_BINDER_DONE: {
3768 struct binder_work *w;
3769 binder_uintptr_t cookie;
3770 struct binder_ref_death *death = NULL;
3771
3772 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
3773 return -EFAULT;
3774
3775 ptr += sizeof(cookie);
3776 binder_inner_proc_lock(proc);
3777 list_for_each_entry(w, &proc->delivered_death,
3778 entry) {
3779 struct binder_ref_death *tmp_death =
3780 container_of(w,
3781 struct binder_ref_death,
3782 work);
3783
3784 if (tmp_death->cookie == cookie) {
3785 death = tmp_death;
3786 break;
3787 }
3788 }
3789 binder_debug(BINDER_DEBUG_DEAD_BINDER,
3790 "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n",
3791 proc->pid, thread->pid, (u64)cookie,
3792 death);
3793 if (death == NULL) {
3794 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
3795 proc->pid, thread->pid, (u64)cookie);
3796 binder_inner_proc_unlock(proc);
3797 break;
3798 }
3799 binder_dequeue_work_ilocked(&death->work);
3800 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
3801 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
3802 if (thread->looper &
3803 (BINDER_LOOPER_STATE_REGISTERED |
3804 BINDER_LOOPER_STATE_ENTERED))
3805 binder_enqueue_work_ilocked(
3806 &death->work, &thread->todo);
3807 else {
3808 binder_enqueue_work_ilocked(
3809 &death->work,
3810 &proc->todo);
3811 binder_wakeup_proc_ilocked(proc);
3812 }
3813 }
3814 binder_inner_proc_unlock(proc);
3815 } break;
3816
3817 default:
3818 pr_err("%d:%d unknown command %d\n",
3819 proc->pid, thread->pid, cmd);
3820 return -EINVAL;
3821 }
3822 *consumed = ptr - buffer;
3823 }
3824 return 0;
3825 }
3826
3827 static void binder_stat_br(struct binder_proc *proc,
3828 struct binder_thread *thread, uint32_t cmd)
3829 {
3830 trace_binder_return(cmd);
3831 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
3832 atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
3833 atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
3834 atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
3835 }
3836 }
3837
3838 static int binder_put_node_cmd(struct binder_proc *proc,
3839 struct binder_thread *thread,
3840 void __user **ptrp,
3841 binder_uintptr_t node_ptr,
3842 binder_uintptr_t node_cookie,
3843 int node_debug_id,
3844 uint32_t cmd, const char *cmd_name)
3845 {
3846 void __user *ptr = *ptrp;
3847
3848 if (put_user(cmd, (uint32_t __user *)ptr))
3849 return -EFAULT;
3850 ptr += sizeof(uint32_t);
3851
3852 if (put_user(node_ptr, (binder_uintptr_t __user *)ptr))
3853 return -EFAULT;
3854 ptr += sizeof(binder_uintptr_t);
3855
3856 if (put_user(node_cookie, (binder_uintptr_t __user *)ptr))
3857 return -EFAULT;
3858 ptr += sizeof(binder_uintptr_t);
3859
3860 binder_stat_br(proc, thread, cmd);
3861 binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n",
3862 proc->pid, thread->pid, cmd_name, node_debug_id,
3863 (u64)node_ptr, (u64)node_cookie);
3864
3865 *ptrp = ptr;
3866 return 0;
3867 }
3868
3869 static int binder_wait_for_work(struct binder_thread *thread,
3870 bool do_proc_work)
3871 {
3872 DEFINE_WAIT(wait);
3873 struct binder_proc *proc = thread->proc;
3874 int ret = 0;
3875
3876 freezer_do_not_count();
3877 binder_inner_proc_lock(proc);
3878 for (;;) {
3879 prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE);
3880 if (binder_has_work_ilocked(thread, do_proc_work))
3881 break;
3882 if (do_proc_work)
3883 list_add(&thread->waiting_thread_node,
3884 &proc->waiting_threads);
3885 binder_inner_proc_unlock(proc);
3886 schedule();
3887 binder_inner_proc_lock(proc);
3888 list_del_init(&thread->waiting_thread_node);
3889 if (signal_pending(current)) {
3890 ret = -ERESTARTSYS;
3891 break;
3892 }
3893 }
3894 finish_wait(&thread->wait, &wait);
3895 binder_inner_proc_unlock(proc);
3896 freezer_count();
3897
3898 return ret;
3899 }
3900
3901 static int binder_thread_read(struct binder_proc *proc,
3902 struct binder_thread *thread,
3903 binder_uintptr_t binder_buffer, size_t size,
3904 binder_size_t *consumed, int non_block)
3905 {
3906 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
3907 void __user *ptr = buffer + *consumed;
3908 void __user *end = buffer + size;
3909
3910 int ret = 0;
3911 int wait_for_proc_work;
3912
3913 if (*consumed == 0) {
3914 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
3915 return -EFAULT;
3916 ptr += sizeof(uint32_t);
3917 }
3918
3919 retry:
3920 binder_inner_proc_lock(proc);
3921 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
3922 binder_inner_proc_unlock(proc);
3923
3924 thread->looper |= BINDER_LOOPER_STATE_WAITING;
3925
3926 trace_binder_wait_for_work(wait_for_proc_work,
3927 !!thread->transaction_stack,
3928 !binder_worklist_empty(proc, &thread->todo));
3929 if (wait_for_proc_work) {
3930 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
3931 BINDER_LOOPER_STATE_ENTERED))) {
3932 binder_user_error("%d:%d ERROR: Thread waiting for process work before calling BC_REGISTER_LOOPER or BC_ENTER_LOOPER (state %x)\n",
3933 proc->pid, thread->pid, thread->looper);
3934 wait_event_interruptible(binder_user_error_wait,
3935 binder_stop_on_user_error < 2);
3936 }
3937 binder_restore_priority(current, proc->default_priority);
3938 }
3939
3940 if (non_block) {
3941 if (!binder_has_work(thread, wait_for_proc_work))
3942 ret = -EAGAIN;
3943 } else {
3944 ret = binder_wait_for_work(thread, wait_for_proc_work);
3945 }
3946
3947 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
3948
3949 if (ret)
3950 return ret;
3951
3952 while (1) {
3953 uint32_t cmd;
3954 struct binder_transaction_data tr;
3955 struct binder_work *w = NULL;
3956 struct list_head *list = NULL;
3957 struct binder_transaction *t = NULL;
3958 struct binder_thread *t_from;
3959
3960 binder_inner_proc_lock(proc);
3961 if (!binder_worklist_empty_ilocked(&thread->todo))
3962 list = &thread->todo;
3963 else if (!binder_worklist_empty_ilocked(&proc->todo) &&
3964 wait_for_proc_work)
3965 list = &proc->todo;
3966 else {
3967 binder_inner_proc_unlock(proc);
3968
3969 /* no data added */
3970 if (ptr - buffer == 4 && !thread->looper_need_return)
3971 goto retry;
3972 break;
3973 }
3974
3975 if (end - ptr < sizeof(tr) + 4) {
3976 binder_inner_proc_unlock(proc);
3977 break;
3978 }
3979 w = binder_dequeue_work_head_ilocked(list);
3980
3981 switch (w->type) {
3982 case BINDER_WORK_TRANSACTION: {
3983 binder_inner_proc_unlock(proc);
3984 t = container_of(w, struct binder_transaction, work);
3985 } break;
3986 case BINDER_WORK_RETURN_ERROR: {
3987 struct binder_error *e = container_of(
3988 w, struct binder_error, work);
3989
3990 WARN_ON(e->cmd == BR_OK);
3991 binder_inner_proc_unlock(proc);
3992 if (put_user(e->cmd, (uint32_t __user *)ptr))
3993 return -EFAULT;
3994 e->cmd = BR_OK;
3995 ptr += sizeof(uint32_t);
3996
3997 binder_stat_br(proc, thread, e->cmd);
3998 } break;
3999 case BINDER_WORK_TRANSACTION_COMPLETE: {
4000 binder_inner_proc_unlock(proc);
4001 cmd = BR_TRANSACTION_COMPLETE;
4002 if (put_user(cmd, (uint32_t __user *)ptr))
4003 return -EFAULT;
4004 ptr += sizeof(uint32_t);
4005
4006 binder_stat_br(proc, thread, cmd);
4007 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
4008 "%d:%d BR_TRANSACTION_COMPLETE\n",
4009 proc->pid, thread->pid);
4010 kfree(w);
4011 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4012 } break;
4013 case BINDER_WORK_NODE: {
4014 struct binder_node *node = container_of(w, struct binder_node, work);
4015 int strong, weak;
4016 binder_uintptr_t node_ptr = node->ptr;
4017 binder_uintptr_t node_cookie = node->cookie;
4018 int node_debug_id = node->debug_id;
4019 int has_weak_ref;
4020 int has_strong_ref;
4021 void __user *orig_ptr = ptr;
4022
4023 BUG_ON(proc != node->proc);
4024 strong = node->internal_strong_refs ||
4025 node->local_strong_refs;
4026 weak = !hlist_empty(&node->refs) ||
4027 node->local_weak_refs ||
4028 node->tmp_refs || strong;
4029 has_strong_ref = node->has_strong_ref;
4030 has_weak_ref = node->has_weak_ref;
4031
4032 if (weak && !has_weak_ref) {
4033 node->has_weak_ref = 1;
4034 node->pending_weak_ref = 1;
4035 node->local_weak_refs++;
4036 }
4037 if (strong && !has_strong_ref) {
4038 node->has_strong_ref = 1;
4039 node->pending_strong_ref = 1;
4040 node->local_strong_refs++;
4041 }
4042 if (!strong && has_strong_ref)
4043 node->has_strong_ref = 0;
4044 if (!weak && has_weak_ref)
4045 node->has_weak_ref = 0;
4046 if (!weak && !strong) {
4047 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4048 "%d:%d node %d u%016llx c%016llx deleted\n",
4049 proc->pid, thread->pid,
4050 node_debug_id,
4051 (u64)node_ptr,
4052 (u64)node_cookie);
4053 rb_erase(&node->rb_node, &proc->nodes);
4054 binder_inner_proc_unlock(proc);
4055 binder_node_lock(node);
4056 /*
4057 * Acquire the node lock before freeing the
4058 * node to serialize with other threads that
4059 * may have been holding the node lock while
4060 * decrementing this node (avoids race where
4061 * this thread frees while the other thread
4062 * is unlocking the node after the final
4063 * decrement)
4064 */
4065 binder_node_unlock(node);
4066 binder_free_node(node);
4067 } else
4068 binder_inner_proc_unlock(proc);
4069
4070 if (weak && !has_weak_ref)
4071 ret = binder_put_node_cmd(
4072 proc, thread, &ptr, node_ptr,
4073 node_cookie, node_debug_id,
4074 BR_INCREFS, "BR_INCREFS");
4075 if (!ret && strong && !has_strong_ref)
4076 ret = binder_put_node_cmd(
4077 proc, thread, &ptr, node_ptr,
4078 node_cookie, node_debug_id,
4079 BR_ACQUIRE, "BR_ACQUIRE");
4080 if (!ret && !strong && has_strong_ref)
4081 ret = binder_put_node_cmd(
4082 proc, thread, &ptr, node_ptr,
4083 node_cookie, node_debug_id,
4084 BR_RELEASE, "BR_RELEASE");
4085 if (!ret && !weak && has_weak_ref)
4086 ret = binder_put_node_cmd(
4087 proc, thread, &ptr, node_ptr,
4088 node_cookie, node_debug_id,
4089 BR_DECREFS, "BR_DECREFS");
4090 if (orig_ptr == ptr)
4091 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4092 "%d:%d node %d u%016llx c%016llx state unchanged\n",
4093 proc->pid, thread->pid,
4094 node_debug_id,
4095 (u64)node_ptr,
4096 (u64)node_cookie);
4097 if (ret)
4098 return ret;
4099 } break;
4100 case BINDER_WORK_DEAD_BINDER:
4101 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4102 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4103 struct binder_ref_death *death;
4104 uint32_t cmd;
4105 binder_uintptr_t cookie;
4106
4107 death = container_of(w, struct binder_ref_death, work);
4108 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
4109 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
4110 else
4111 cmd = BR_DEAD_BINDER;
4112 cookie = death->cookie;
4113
4114 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
4115 "%d:%d %s %016llx\n",
4116 proc->pid, thread->pid,
4117 cmd == BR_DEAD_BINDER ?
4118 "BR_DEAD_BINDER" :
4119 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
4120 (u64)cookie);
4121 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
4122 binder_inner_proc_unlock(proc);
4123 kfree(death);
4124 binder_stats_deleted(BINDER_STAT_DEATH);
4125 } else {
4126 binder_enqueue_work_ilocked(
4127 w, &proc->delivered_death);
4128 binder_inner_proc_unlock(proc);
4129 }
4130 if (put_user(cmd, (uint32_t __user *)ptr))
4131 return -EFAULT;
4132 ptr += sizeof(uint32_t);
4133 if (put_user(cookie,
4134 (binder_uintptr_t __user *)ptr))
4135 return -EFAULT;
4136 ptr += sizeof(binder_uintptr_t);
4137 binder_stat_br(proc, thread, cmd);
4138 if (cmd == BR_DEAD_BINDER)
4139 goto done; /* DEAD_BINDER notifications can cause transactions */
4140 } break;
4141 }
4142
4143 if (!t)
4144 continue;
4145
4146 BUG_ON(t->buffer == NULL);
4147 if (t->buffer->target_node) {
4148 struct binder_node *target_node = t->buffer->target_node;
4149 struct binder_priority node_prio;
4150
4151 tr.target.ptr = target_node->ptr;
4152 tr.cookie = target_node->cookie;
4153 node_prio.sched_policy = target_node->sched_policy;
4154 node_prio.prio = target_node->min_priority;
4155 binder_transaction_priority(current, t, node_prio,
4156 target_node->inherit_rt);
4157 cmd = BR_TRANSACTION;
4158 } else {
4159 tr.target.ptr = 0;
4160 tr.cookie = 0;
4161 cmd = BR_REPLY;
4162 }
4163 tr.code = t->code;
4164 tr.flags = t->flags;
4165 tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
4166
4167 t_from = binder_get_txn_from(t);
4168 if (t_from) {
4169 struct task_struct *sender = t_from->proc->tsk;
4170
4171 tr.sender_pid = task_tgid_nr_ns(sender,
4172 task_active_pid_ns(current));
4173 } else {
4174 tr.sender_pid = 0;
4175 }
4176
4177 tr.data_size = t->buffer->data_size;
4178 tr.offsets_size = t->buffer->offsets_size;
4179 tr.data.ptr.buffer = (binder_uintptr_t)
4180 ((uintptr_t)t->buffer->data +
4181 binder_alloc_get_user_buffer_offset(&proc->alloc));
4182 tr.data.ptr.offsets = tr.data.ptr.buffer +
4183 ALIGN(t->buffer->data_size,
4184 sizeof(void *));
4185
4186 if (put_user(cmd, (uint32_t __user *)ptr)) {
4187 if (t_from)
4188 binder_thread_dec_tmpref(t_from);
4189 return -EFAULT;
4190 }
4191 ptr += sizeof(uint32_t);
4192 if (copy_to_user(ptr, &tr, sizeof(tr))) {
4193 if (t_from)
4194 binder_thread_dec_tmpref(t_from);
4195 return -EFAULT;
4196 }
4197 ptr += sizeof(tr);
4198
4199 trace_binder_transaction_received(t);
4200 binder_stat_br(proc, thread, cmd);
4201 binder_debug(BINDER_DEBUG_TRANSACTION,
4202 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
4203 proc->pid, thread->pid,
4204 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
4205 "BR_REPLY",
4206 t->debug_id, t_from ? t_from->proc->pid : 0,
4207 t_from ? t_from->pid : 0, cmd,
4208 t->buffer->data_size, t->buffer->offsets_size,
4209 (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
4210
4211 if (t_from)
4212 binder_thread_dec_tmpref(t_from);
4213 t->buffer->allow_user_free = 1;
4214 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
4215 binder_inner_proc_lock(thread->proc);
4216 t->to_parent = thread->transaction_stack;
4217 t->to_thread = thread;
4218 thread->transaction_stack = t;
4219 binder_inner_proc_unlock(thread->proc);
4220 } else {
4221 binder_free_transaction(t);
4222 }
4223 break;
4224 }
4225
4226 done:
4227
4228 *consumed = ptr - buffer;
4229 binder_inner_proc_lock(proc);
4230 if (proc->requested_threads == 0 &&
4231 list_empty(&thread->proc->waiting_threads) &&
4232 proc->requested_threads_started < proc->max_threads &&
4233 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4234 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
4235 /*spawn a new thread if we leave this out */) {
4236 proc->requested_threads++;
4237 binder_inner_proc_unlock(proc);
4238 binder_debug(BINDER_DEBUG_THREADS,
4239 "%d:%d BR_SPAWN_LOOPER\n",
4240 proc->pid, thread->pid);
4241 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
4242 return -EFAULT;
4243 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
4244 } else
4245 binder_inner_proc_unlock(proc);
4246 return 0;
4247 }
4248
4249 static void binder_release_work(struct binder_proc *proc,
4250 struct list_head *list)
4251 {
4252 struct binder_work *w;
4253
4254 while (1) {
4255 w = binder_dequeue_work_head(proc, list);
4256 if (!w)
4257 return;
4258
4259 switch (w->type) {
4260 case BINDER_WORK_TRANSACTION: {
4261 struct binder_transaction *t;
4262
4263 t = container_of(w, struct binder_transaction, work);
4264 if (t->buffer->target_node &&
4265 !(t->flags & TF_ONE_WAY)) {
4266 binder_send_failed_reply(t, BR_DEAD_REPLY);
4267 } else {
4268 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4269 "undelivered transaction %d\n",
4270 t->debug_id);
4271 binder_free_transaction(t);
4272 }
4273 } break;
4274 case BINDER_WORK_RETURN_ERROR: {
4275 struct binder_error *e = container_of(
4276 w, struct binder_error, work);
4277
4278 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4279 "undelivered TRANSACTION_ERROR: %u\n",
4280 e->cmd);
4281 } break;
4282 case BINDER_WORK_TRANSACTION_COMPLETE: {
4283 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4284 "undelivered TRANSACTION_COMPLETE\n");
4285 kfree(w);
4286 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4287 } break;
4288 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4289 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4290 struct binder_ref_death *death;
4291
4292 death = container_of(w, struct binder_ref_death, work);
4293 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4294 "undelivered death notification, %016llx\n",
4295 (u64)death->cookie);
4296 kfree(death);
4297 binder_stats_deleted(BINDER_STAT_DEATH);
4298 } break;
4299 default:
4300 pr_err("unexpected work type, %d, not freed\n",
4301 w->type);
4302 break;
4303 }
4304 }
4305
4306 }
4307
4308 static struct binder_thread *binder_get_thread_ilocked(
4309 struct binder_proc *proc, struct binder_thread *new_thread)
4310 {
4311 struct binder_thread *thread = NULL;
4312 struct rb_node *parent = NULL;
4313 struct rb_node **p = &proc->threads.rb_node;
4314
4315 while (*p) {
4316 parent = *p;
4317 thread = rb_entry(parent, struct binder_thread, rb_node);
4318
4319 if (current->pid < thread->pid)
4320 p = &(*p)->rb_left;
4321 else if (current->pid > thread->pid)
4322 p = &(*p)->rb_right;
4323 else
4324 return thread;
4325 }
4326 if (!new_thread)
4327 return NULL;
4328 thread = new_thread;
4329 binder_stats_created(BINDER_STAT_THREAD);
4330 thread->proc = proc;
4331 thread->pid = current->pid;
4332 get_task_struct(current);
4333 thread->task = current;
4334 atomic_set(&thread->tmp_ref, 0);
4335 init_waitqueue_head(&thread->wait);
4336 INIT_LIST_HEAD(&thread->todo);
4337 rb_link_node(&thread->rb_node, parent, p);
4338 rb_insert_color(&thread->rb_node, &proc->threads);
4339 thread->looper_need_return = true;
4340 thread->return_error.work.type = BINDER_WORK_RETURN_ERROR;
4341 thread->return_error.cmd = BR_OK;
4342 thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR;
4343 thread->reply_error.cmd = BR_OK;
4344 INIT_LIST_HEAD(&new_thread->waiting_thread_node);
4345 return thread;
4346 }
4347
4348 static struct binder_thread *binder_get_thread(struct binder_proc *proc)
4349 {
4350 struct binder_thread *thread;
4351 struct binder_thread *new_thread;
4352
4353 binder_inner_proc_lock(proc);
4354 thread = binder_get_thread_ilocked(proc, NULL);
4355 binder_inner_proc_unlock(proc);
4356 if (!thread) {
4357 new_thread = kzalloc(sizeof(*thread), GFP_KERNEL);
4358 if (new_thread == NULL)
4359 return NULL;
4360 binder_inner_proc_lock(proc);
4361 thread = binder_get_thread_ilocked(proc, new_thread);
4362 binder_inner_proc_unlock(proc);
4363 if (thread != new_thread)
4364 kfree(new_thread);
4365 }
4366 return thread;
4367 }
4368
4369 static void binder_free_proc(struct binder_proc *proc)
4370 {
4371 BUG_ON(!list_empty(&proc->todo));
4372 BUG_ON(!list_empty(&proc->delivered_death));
4373 binder_alloc_deferred_release(&proc->alloc);
4374 put_task_struct(proc->tsk);
4375 binder_stats_deleted(BINDER_STAT_PROC);
4376 kfree(proc);
4377 }
4378
4379 static void binder_free_thread(struct binder_thread *thread)
4380 {
4381 BUG_ON(!list_empty(&thread->todo));
4382 binder_stats_deleted(BINDER_STAT_THREAD);
4383 binder_proc_dec_tmpref(thread->proc);
4384 put_task_struct(thread->task);
4385 kfree(thread);
4386 }
4387
4388 static int binder_thread_release(struct binder_proc *proc,
4389 struct binder_thread *thread)
4390 {
4391 struct binder_transaction *t;
4392 struct binder_transaction *send_reply = NULL;
4393 int active_transactions = 0;
4394 struct binder_transaction *last_t = NULL;
4395
4396 binder_inner_proc_lock(thread->proc);
4397 /*
4398 * take a ref on the proc so it survives
4399 * after we remove this thread from proc->threads.
4400 * The corresponding dec is when we actually
4401 * free the thread in binder_free_thread()
4402 */
4403 proc->tmp_ref++;
4404 /*
4405 * take a ref on this thread to ensure it
4406 * survives while we are releasing it
4407 */
4408 atomic_inc(&thread->tmp_ref);
4409 rb_erase(&thread->rb_node, &proc->threads);
4410 t = thread->transaction_stack;
4411 if (t) {
4412 spin_lock(&t->lock);
4413 if (t->to_thread == thread)
4414 send_reply = t;
4415 }
4416 thread->is_dead = true;
4417
4418 while (t) {
4419 last_t = t;
4420 active_transactions++;
4421 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4422 "release %d:%d transaction %d %s, still active\n",
4423 proc->pid, thread->pid,
4424 t->debug_id,
4425 (t->to_thread == thread) ? "in" : "out");
4426
4427 if (t->to_thread == thread) {
4428 t->to_proc = NULL;
4429 t->to_thread = NULL;
4430 if (t->buffer) {
4431 t->buffer->transaction = NULL;
4432 t->buffer = NULL;
4433 }
4434 t = t->to_parent;
4435 } else if (t->from == thread) {
4436 t->from = NULL;
4437 t = t->from_parent;
4438 } else
4439 BUG();
4440 spin_unlock(&last_t->lock);
4441 if (t)
4442 spin_lock(&t->lock);
4443 }
4444 binder_inner_proc_unlock(thread->proc);
4445
4446 if (send_reply)
4447 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
4448 binder_release_work(proc, &thread->todo);
4449 binder_thread_dec_tmpref(thread);
4450 return active_transactions;
4451 }
4452
4453 static unsigned int binder_poll(struct file *filp,
4454 struct poll_table_struct *wait)
4455 {
4456 struct binder_proc *proc = filp->private_data;
4457 struct binder_thread *thread = NULL;
4458 bool wait_for_proc_work;
4459
4460 thread = binder_get_thread(proc);
4461
4462 binder_inner_proc_lock(thread->proc);
4463 thread->looper |= BINDER_LOOPER_STATE_POLL;
4464 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
4465
4466 binder_inner_proc_unlock(thread->proc);
4467
4468 poll_wait(filp, &thread->wait, wait);
4469
4470 if (binder_has_work(thread, wait_for_proc_work))
4471 return POLLIN;
4472
4473 return 0;
4474 }
4475
4476 static int binder_ioctl_write_read(struct file *filp,
4477 unsigned int cmd, unsigned long arg,
4478 struct binder_thread *thread)
4479 {
4480 int ret = 0;
4481 struct binder_proc *proc = filp->private_data;
4482 unsigned int size = _IOC_SIZE(cmd);
4483 void __user *ubuf = (void __user *)arg;
4484 struct binder_write_read bwr;
4485
4486 if (size != sizeof(struct binder_write_read)) {
4487 ret = -EINVAL;
4488 goto out;
4489 }
4490 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
4491 ret = -EFAULT;
4492 goto out;
4493 }
4494 binder_debug(BINDER_DEBUG_READ_WRITE,
4495 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
4496 proc->pid, thread->pid,
4497 (u64)bwr.write_size, (u64)bwr.write_buffer,
4498 (u64)bwr.read_size, (u64)bwr.read_buffer);
4499
4500 if (bwr.write_size > 0) {
4501 ret = binder_thread_write(proc, thread,
4502 bwr.write_buffer,
4503 bwr.write_size,
4504 &bwr.write_consumed);
4505 trace_binder_write_done(ret);
4506 if (ret < 0) {
4507 bwr.read_consumed = 0;
4508 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4509 ret = -EFAULT;
4510 goto out;
4511 }
4512 }
4513 if (bwr.read_size > 0) {
4514 ret = binder_thread_read(proc, thread, bwr.read_buffer,
4515 bwr.read_size,
4516 &bwr.read_consumed,
4517 filp->f_flags & O_NONBLOCK);
4518 trace_binder_read_done(ret);
4519 binder_inner_proc_lock(proc);
4520 if (!binder_worklist_empty_ilocked(&proc->todo))
4521 binder_wakeup_proc_ilocked(proc);
4522 binder_inner_proc_unlock(proc);
4523 if (ret < 0) {
4524 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4525 ret = -EFAULT;
4526 goto out;
4527 }
4528 }
4529 binder_debug(BINDER_DEBUG_READ_WRITE,
4530 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
4531 proc->pid, thread->pid,
4532 (u64)bwr.write_consumed, (u64)bwr.write_size,
4533 (u64)bwr.read_consumed, (u64)bwr.read_size);
4534 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
4535 ret = -EFAULT;
4536 goto out;
4537 }
4538 out:
4539 return ret;
4540 }
4541
4542 static int binder_ioctl_set_ctx_mgr(struct file *filp)
4543 {
4544 int ret = 0;
4545 struct binder_proc *proc = filp->private_data;
4546 struct binder_context *context = proc->context;
4547 struct binder_node *new_node;
4548 kuid_t curr_euid = current_euid();
4549
4550 mutex_lock(&context->context_mgr_node_lock);
4551 if (context->binder_context_mgr_node) {
4552 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
4553 ret = -EBUSY;
4554 goto out;
4555 }
4556 ret = security_binder_set_context_mgr(proc->tsk);
4557 if (ret < 0)
4558 goto out;
4559 if (uid_valid(context->binder_context_mgr_uid)) {
4560 if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
4561 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
4562 from_kuid(&init_user_ns, curr_euid),
4563 from_kuid(&init_user_ns,
4564 context->binder_context_mgr_uid));
4565 ret = -EPERM;
4566 goto out;
4567 }
4568 } else {
4569 context->binder_context_mgr_uid = curr_euid;
4570 }
4571 new_node = binder_new_node(proc, NULL);
4572 if (!new_node) {
4573 ret = -ENOMEM;
4574 goto out;
4575 }
4576 binder_node_lock(new_node);
4577 new_node->local_weak_refs++;
4578 new_node->local_strong_refs++;
4579 new_node->has_strong_ref = 1;
4580 new_node->has_weak_ref = 1;
4581 context->binder_context_mgr_node = new_node;
4582 binder_node_unlock(new_node);
4583 binder_put_node(new_node);
4584 out:
4585 mutex_unlock(&context->context_mgr_node_lock);
4586 return ret;
4587 }
4588
4589 static int binder_ioctl_get_node_debug_info(struct binder_proc *proc,
4590 struct binder_node_debug_info *info)
4591 {
4592 struct rb_node *n;
4593 binder_uintptr_t ptr = info->ptr;
4594
4595 memset(info, 0, sizeof(*info));
4596
4597 binder_inner_proc_lock(proc);
4598 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
4599 struct binder_node *node = rb_entry(n, struct binder_node,
4600 rb_node);
4601 if (node->ptr > ptr) {
4602 info->ptr = node->ptr;
4603 info->cookie = node->cookie;
4604 info->has_strong_ref = node->has_strong_ref;
4605 info->has_weak_ref = node->has_weak_ref;
4606 break;
4607 }
4608 }
4609 binder_inner_proc_unlock(proc);
4610
4611 return 0;
4612 }
4613
4614 static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4615 {
4616 int ret;
4617 struct binder_proc *proc = filp->private_data;
4618 struct binder_thread *thread;
4619 unsigned int size = _IOC_SIZE(cmd);
4620 void __user *ubuf = (void __user *)arg;
4621
4622 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
4623 proc->pid, current->pid, cmd, arg);*/
4624
4625 binder_selftest_alloc(&proc->alloc);
4626
4627 trace_binder_ioctl(cmd, arg);
4628
4629 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4630 if (ret)
4631 goto err_unlocked;
4632
4633 thread = binder_get_thread(proc);
4634 if (thread == NULL) {
4635 ret = -ENOMEM;
4636 goto err;
4637 }
4638
4639 switch (cmd) {
4640 case BINDER_WRITE_READ:
4641 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
4642 if (ret)
4643 goto err;
4644 break;
4645 case BINDER_SET_MAX_THREADS: {
4646 int max_threads;
4647
4648 if (copy_from_user(&max_threads, ubuf,
4649 sizeof(max_threads))) {
4650 ret = -EINVAL;
4651 goto err;
4652 }
4653 binder_inner_proc_lock(proc);
4654 proc->max_threads = max_threads;
4655 binder_inner_proc_unlock(proc);
4656 break;
4657 }
4658 case BINDER_SET_CONTEXT_MGR:
4659 ret = binder_ioctl_set_ctx_mgr(filp);
4660 if (ret)
4661 goto err;
4662 break;
4663 case BINDER_THREAD_EXIT:
4664 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
4665 proc->pid, thread->pid);
4666 binder_thread_release(proc, thread);
4667 thread = NULL;
4668 break;
4669 case BINDER_VERSION: {
4670 struct binder_version __user *ver = ubuf;
4671
4672 if (size != sizeof(struct binder_version)) {
4673 ret = -EINVAL;
4674 goto err;
4675 }
4676 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
4677 &ver->protocol_version)) {
4678 ret = -EINVAL;
4679 goto err;
4680 }
4681 break;
4682 }
4683 case BINDER_GET_NODE_DEBUG_INFO: {
4684 struct binder_node_debug_info info;
4685
4686 if (copy_from_user(&info, ubuf, sizeof(info))) {
4687 ret = -EFAULT;
4688 goto err;
4689 }
4690
4691 ret = binder_ioctl_get_node_debug_info(proc, &info);
4692 if (ret < 0)
4693 goto err;
4694
4695 if (copy_to_user(ubuf, &info, sizeof(info))) {
4696 ret = -EFAULT;
4697 goto err;
4698 }
4699 break;
4700 }
4701 default:
4702 ret = -EINVAL;
4703 goto err;
4704 }
4705 ret = 0;
4706 err:
4707 if (thread)
4708 thread->looper_need_return = false;
4709 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4710 if (ret && ret != -ERESTARTSYS)
4711 pr_info("%d:%d ioctl %x %lx returned %d\n", proc->pid, current->pid, cmd, arg, ret);
4712 err_unlocked:
4713 trace_binder_ioctl_done(ret);
4714 return ret;
4715 }
4716
4717 static void binder_vma_open(struct vm_area_struct *vma)
4718 {
4719 struct binder_proc *proc = vma->vm_private_data;
4720
4721 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4722 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
4723 proc->pid, vma->vm_start, vma->vm_end,
4724 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4725 (unsigned long)pgprot_val(vma->vm_page_prot));
4726 }
4727
4728 static void binder_vma_close(struct vm_area_struct *vma)
4729 {
4730 struct binder_proc *proc = vma->vm_private_data;
4731
4732 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4733 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
4734 proc->pid, vma->vm_start, vma->vm_end,
4735 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4736 (unsigned long)pgprot_val(vma->vm_page_prot));
4737 binder_alloc_vma_close(&proc->alloc);
4738 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
4739 }
4740
4741 static int binder_vm_fault(struct vm_fault *vmf)
4742 {
4743 return VM_FAULT_SIGBUS;
4744 }
4745
4746 static const struct vm_operations_struct binder_vm_ops = {
4747 .open = binder_vma_open,
4748 .close = binder_vma_close,
4749 .fault = binder_vm_fault,
4750 };
4751
4752 static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
4753 {
4754 int ret;
4755 struct binder_proc *proc = filp->private_data;
4756 const char *failure_string;
4757
4758 if (proc->tsk != current->group_leader)
4759 return -EINVAL;
4760
4761 if ((vma->vm_end - vma->vm_start) > SZ_4M)
4762 vma->vm_end = vma->vm_start + SZ_4M;
4763
4764 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4765 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
4766 __func__, proc->pid, vma->vm_start, vma->vm_end,
4767 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4768 (unsigned long)pgprot_val(vma->vm_page_prot));
4769
4770 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
4771 ret = -EPERM;
4772 failure_string = "bad vm_flags";
4773 goto err_bad_arg;
4774 }
4775 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
4776 vma->vm_ops = &binder_vm_ops;
4777 vma->vm_private_data = proc;
4778
4779 ret = binder_alloc_mmap_handler(&proc->alloc, vma);
4780 if (ret)
4781 return ret;
4782 proc->files = get_files_struct(current);
4783 return 0;
4784
4785 err_bad_arg:
4786 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
4787 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
4788 return ret;
4789 }
4790
4791 static int binder_open(struct inode *nodp, struct file *filp)
4792 {
4793 struct binder_proc *proc;
4794 struct binder_device *binder_dev;
4795
4796 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
4797 current->group_leader->pid, current->pid);
4798
4799 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
4800 if (proc == NULL)
4801 return -ENOMEM;
4802 spin_lock_init(&proc->inner_lock);
4803 spin_lock_init(&proc->outer_lock);
4804 get_task_struct(current->group_leader);
4805 proc->tsk = current->group_leader;
4806 INIT_LIST_HEAD(&proc->todo);
4807 if (binder_supported_policy(current->policy)) {
4808 proc->default_priority.sched_policy = current->policy;
4809 proc->default_priority.prio = current->normal_prio;
4810 } else {
4811 proc->default_priority.sched_policy = SCHED_NORMAL;
4812 proc->default_priority.prio = NICE_TO_PRIO(0);
4813 }
4814
4815 binder_dev = container_of(filp->private_data, struct binder_device,
4816 miscdev);
4817 proc->context = &binder_dev->context;
4818 binder_alloc_init(&proc->alloc);
4819
4820 binder_stats_created(BINDER_STAT_PROC);
4821 proc->pid = current->group_leader->pid;
4822 INIT_LIST_HEAD(&proc->delivered_death);
4823 INIT_LIST_HEAD(&proc->waiting_threads);
4824 filp->private_data = proc;
4825
4826 mutex_lock(&binder_procs_lock);
4827 hlist_add_head(&proc->proc_node, &binder_procs);
4828 mutex_unlock(&binder_procs_lock);
4829
4830 if (binder_debugfs_dir_entry_proc) {
4831 char strbuf[11];
4832
4833 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
4834 /*
4835 * proc debug entries are shared between contexts, so
4836 * this will fail if the process tries to open the driver
4837 * again with a different context. The priting code will
4838 * anyway print all contexts that a given PID has, so this
4839 * is not a problem.
4840 */
4841 proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
4842 binder_debugfs_dir_entry_proc,
4843 (void *)(unsigned long)proc->pid,
4844 &binder_proc_fops);
4845 }
4846
4847 return 0;
4848 }
4849
4850 static int binder_flush(struct file *filp, fl_owner_t id)
4851 {
4852 struct binder_proc *proc = filp->private_data;
4853
4854 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
4855
4856 return 0;
4857 }
4858
4859 static void binder_deferred_flush(struct binder_proc *proc)
4860 {
4861 struct rb_node *n;
4862 int wake_count = 0;
4863
4864 binder_inner_proc_lock(proc);
4865 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
4866 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
4867
4868 thread->looper_need_return = true;
4869 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
4870 wake_up_interruptible(&thread->wait);
4871 wake_count++;
4872 }
4873 }
4874 binder_inner_proc_unlock(proc);
4875
4876 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4877 "binder_flush: %d woke %d threads\n", proc->pid,
4878 wake_count);
4879 }
4880
4881 static int binder_release(struct inode *nodp, struct file *filp)
4882 {
4883 struct binder_proc *proc = filp->private_data;
4884
4885 debugfs_remove(proc->debugfs_entry);
4886 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
4887
4888 return 0;
4889 }
4890
4891 static int binder_node_release(struct binder_node *node, int refs)
4892 {
4893 struct binder_ref *ref;
4894 int death = 0;
4895 struct binder_proc *proc = node->proc;
4896
4897 binder_release_work(proc, &node->async_todo);
4898
4899 binder_node_lock(node);
4900 binder_inner_proc_lock(proc);
4901 binder_dequeue_work_ilocked(&node->work);
4902 /*
4903 * The caller must have taken a temporary ref on the node,
4904 */
4905 BUG_ON(!node->tmp_refs);
4906 if (hlist_empty(&node->refs) && node->tmp_refs == 1) {
4907 binder_inner_proc_unlock(proc);
4908 binder_node_unlock(node);
4909 binder_free_node(node);
4910
4911 return refs;
4912 }
4913
4914 node->proc = NULL;
4915 node->local_strong_refs = 0;
4916 node->local_weak_refs = 0;
4917 binder_inner_proc_unlock(proc);
4918
4919 spin_lock(&binder_dead_nodes_lock);
4920 hlist_add_head(&node->dead_node, &binder_dead_nodes);
4921 spin_unlock(&binder_dead_nodes_lock);
4922
4923 hlist_for_each_entry(ref, &node->refs, node_entry) {
4924 refs++;
4925 /*
4926 * Need the node lock to synchronize
4927 * with new notification requests and the
4928 * inner lock to synchronize with queued
4929 * death notifications.
4930 */
4931 binder_inner_proc_lock(ref->proc);
4932 if (!ref->death) {
4933 binder_inner_proc_unlock(ref->proc);
4934 continue;
4935 }
4936
4937 death++;
4938
4939 BUG_ON(!list_empty(&ref->death->work.entry));
4940 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
4941 binder_enqueue_work_ilocked(&ref->death->work,
4942 &ref->proc->todo);
4943 binder_wakeup_proc_ilocked(ref->proc);
4944 binder_inner_proc_unlock(ref->proc);
4945 }
4946
4947 binder_debug(BINDER_DEBUG_DEAD_BINDER,
4948 "node %d now dead, refs %d, death %d\n",
4949 node->debug_id, refs, death);
4950 binder_node_unlock(node);
4951 binder_put_node(node);
4952
4953 return refs;
4954 }
4955
4956 static void binder_deferred_release(struct binder_proc *proc)
4957 {
4958 struct binder_context *context = proc->context;
4959 struct rb_node *n;
4960 int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
4961
4962 BUG_ON(proc->files);
4963
4964 mutex_lock(&binder_procs_lock);
4965 hlist_del(&proc->proc_node);
4966 mutex_unlock(&binder_procs_lock);
4967
4968 mutex_lock(&context->context_mgr_node_lock);
4969 if (context->binder_context_mgr_node &&
4970 context->binder_context_mgr_node->proc == proc) {
4971 binder_debug(BINDER_DEBUG_DEAD_BINDER,
4972 "%s: %d context_mgr_node gone\n",
4973 __func__, proc->pid);
4974 context->binder_context_mgr_node = NULL;
4975 }
4976 mutex_unlock(&context->context_mgr_node_lock);
4977 binder_inner_proc_lock(proc);
4978 /*
4979 * Make sure proc stays alive after we
4980 * remove all the threads
4981 */
4982 proc->tmp_ref++;
4983
4984 proc->is_dead = true;
4985 threads = 0;
4986 active_transactions = 0;
4987 while ((n = rb_first(&proc->threads))) {
4988 struct binder_thread *thread;
4989
4990 thread = rb_entry(n, struct binder_thread, rb_node);
4991 binder_inner_proc_unlock(proc);
4992 threads++;
4993 active_transactions += binder_thread_release(proc, thread);
4994 binder_inner_proc_lock(proc);
4995 }
4996
4997 nodes = 0;
4998 incoming_refs = 0;
4999 while ((n = rb_first(&proc->nodes))) {
5000 struct binder_node *node;
5001
5002 node = rb_entry(n, struct binder_node, rb_node);
5003 nodes++;
5004 /*
5005 * take a temporary ref on the node before
5006 * calling binder_node_release() which will either
5007 * kfree() the node or call binder_put_node()
5008 */
5009 binder_inc_node_tmpref_ilocked(node);
5010 rb_erase(&node->rb_node, &proc->nodes);
5011 binder_inner_proc_unlock(proc);
5012 incoming_refs = binder_node_release(node, incoming_refs);
5013 binder_inner_proc_lock(proc);
5014 }
5015 binder_inner_proc_unlock(proc);
5016
5017 outgoing_refs = 0;
5018 binder_proc_lock(proc);
5019 while ((n = rb_first(&proc->refs_by_desc))) {
5020 struct binder_ref *ref;
5021
5022 ref = rb_entry(n, struct binder_ref, rb_node_desc);
5023 outgoing_refs++;
5024 binder_cleanup_ref_olocked(ref);
5025 binder_proc_unlock(proc);
5026 binder_free_ref(ref);
5027 binder_proc_lock(proc);
5028 }
5029 binder_proc_unlock(proc);
5030
5031 binder_release_work(proc, &proc->todo);
5032 binder_release_work(proc, &proc->delivered_death);
5033
5034 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5035 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
5036 __func__, proc->pid, threads, nodes, incoming_refs,
5037 outgoing_refs, active_transactions);
5038
5039 binder_proc_dec_tmpref(proc);
5040 }
5041
5042 static void binder_deferred_func(struct work_struct *work)
5043 {
5044 struct binder_proc *proc;
5045 struct files_struct *files;
5046
5047 int defer;
5048
5049 do {
5050 mutex_lock(&binder_deferred_lock);
5051 if (!hlist_empty(&binder_deferred_list)) {
5052 proc = hlist_entry(binder_deferred_list.first,
5053 struct binder_proc, deferred_work_node);
5054 hlist_del_init(&proc->deferred_work_node);
5055 defer = proc->deferred_work;
5056 proc->deferred_work = 0;
5057 } else {
5058 proc = NULL;
5059 defer = 0;
5060 }
5061 mutex_unlock(&binder_deferred_lock);
5062
5063 files = NULL;
5064 if (defer & BINDER_DEFERRED_PUT_FILES) {
5065 files = proc->files;
5066 if (files)
5067 proc->files = NULL;
5068 }
5069
5070 if (defer & BINDER_DEFERRED_FLUSH)
5071 binder_deferred_flush(proc);
5072
5073 if (defer & BINDER_DEFERRED_RELEASE)
5074 binder_deferred_release(proc); /* frees proc */
5075
5076 if (files)
5077 put_files_struct(files);
5078 } while (proc);
5079 }
5080 static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
5081
5082 static void
5083 binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
5084 {
5085 mutex_lock(&binder_deferred_lock);
5086 proc->deferred_work |= defer;
5087 if (hlist_unhashed(&proc->deferred_work_node)) {
5088 hlist_add_head(&proc->deferred_work_node,
5089 &binder_deferred_list);
5090 schedule_work(&binder_deferred_work);
5091 }
5092 mutex_unlock(&binder_deferred_lock);
5093 }
5094
5095 static void print_binder_transaction_ilocked(struct seq_file *m,
5096 struct binder_proc *proc,
5097 const char *prefix,
5098 struct binder_transaction *t)
5099 {
5100 struct binder_proc *to_proc;
5101 struct binder_buffer *buffer = t->buffer;
5102
5103 spin_lock(&t->lock);
5104 to_proc = t->to_proc;
5105 seq_printf(m,
5106 "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %d:%d r%d",
5107 prefix, t->debug_id, t,
5108 t->from ? t->from->proc->pid : 0,
5109 t->from ? t->from->pid : 0,
5110 to_proc ? to_proc->pid : 0,
5111 t->to_thread ? t->to_thread->pid : 0,
5112 t->code, t->flags, t->priority.sched_policy,
5113 t->priority.prio, t->need_reply);
5114 spin_unlock(&t->lock);
5115
5116 if (proc != to_proc) {
5117 /*
5118 * Can only safely deref buffer if we are holding the
5119 * correct proc inner lock for this node
5120 */
5121 seq_puts(m, "\n");
5122 return;
5123 }
5124
5125 if (buffer == NULL) {
5126 seq_puts(m, " buffer free\n");
5127 return;
5128 }
5129 if (buffer->target_node)
5130 seq_printf(m, " node %d", buffer->target_node->debug_id);
5131 seq_printf(m, " size %zd:%zd data %p\n",
5132 buffer->data_size, buffer->offsets_size,
5133 buffer->data);
5134 }
5135
5136 static void print_binder_work_ilocked(struct seq_file *m,
5137 struct binder_proc *proc,
5138 const char *prefix,
5139 const char *transaction_prefix,
5140 struct binder_work *w)
5141 {
5142 struct binder_node *node;
5143 struct binder_transaction *t;
5144
5145 switch (w->type) {
5146 case BINDER_WORK_TRANSACTION:
5147 t = container_of(w, struct binder_transaction, work);
5148 print_binder_transaction_ilocked(
5149 m, proc, transaction_prefix, t);
5150 break;
5151 case BINDER_WORK_RETURN_ERROR: {
5152 struct binder_error *e = container_of(
5153 w, struct binder_error, work);
5154
5155 seq_printf(m, "%stransaction error: %u\n",
5156 prefix, e->cmd);
5157 } break;
5158 case BINDER_WORK_TRANSACTION_COMPLETE:
5159 seq_printf(m, "%stransaction complete\n", prefix);
5160 break;
5161 case BINDER_WORK_NODE:
5162 node = container_of(w, struct binder_node, work);
5163 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
5164 prefix, node->debug_id,
5165 (u64)node->ptr, (u64)node->cookie);
5166 break;
5167 case BINDER_WORK_DEAD_BINDER:
5168 seq_printf(m, "%shas dead binder\n", prefix);
5169 break;
5170 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
5171 seq_printf(m, "%shas cleared dead binder\n", prefix);
5172 break;
5173 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
5174 seq_printf(m, "%shas cleared death notification\n", prefix);
5175 break;
5176 default:
5177 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
5178 break;
5179 }
5180 }
5181
5182 static void print_binder_thread_ilocked(struct seq_file *m,
5183 struct binder_thread *thread,
5184 int print_always)
5185 {
5186 struct binder_transaction *t;
5187 struct binder_work *w;
5188 size_t start_pos = m->count;
5189 size_t header_pos;
5190
5191 seq_printf(m, " thread %d: l %02x need_return %d tr %d\n",
5192 thread->pid, thread->looper,
5193 thread->looper_need_return,
5194 atomic_read(&thread->tmp_ref));
5195 header_pos = m->count;
5196 t = thread->transaction_stack;
5197 while (t) {
5198 if (t->from == thread) {
5199 print_binder_transaction_ilocked(m, thread->proc,
5200 " outgoing transaction", t);
5201 t = t->from_parent;
5202 } else if (t->to_thread == thread) {
5203 print_binder_transaction_ilocked(m, thread->proc,
5204 " incoming transaction", t);
5205 t = t->to_parent;
5206 } else {
5207 print_binder_transaction_ilocked(m, thread->proc,
5208 " bad transaction", t);
5209 t = NULL;
5210 }
5211 }
5212 list_for_each_entry(w, &thread->todo, entry) {
5213 print_binder_work_ilocked(m, thread->proc, " ",
5214 " pending transaction", w);
5215 }
5216 if (!print_always && m->count == header_pos)
5217 m->count = start_pos;
5218 }
5219
5220 static void print_binder_node_nilocked(struct seq_file *m,
5221 struct binder_node *node)
5222 {
5223 struct binder_ref *ref;
5224 struct binder_work *w;
5225 int count;
5226
5227 count = 0;
5228 hlist_for_each_entry(ref, &node->refs, node_entry)
5229 count++;
5230
5231 seq_printf(m, " node %d: u%016llx c%016llx pri %d:%d hs %d hw %d ls %d lw %d is %d iw %d tr %d",
5232 node->debug_id, (u64)node->ptr, (u64)node->cookie,
5233 node->sched_policy, node->min_priority,
5234 node->has_strong_ref, node->has_weak_ref,
5235 node->local_strong_refs, node->local_weak_refs,
5236 node->internal_strong_refs, count, node->tmp_refs);
5237 if (count) {
5238 seq_puts(m, " proc");
5239 hlist_for_each_entry(ref, &node->refs, node_entry)
5240 seq_printf(m, " %d", ref->proc->pid);
5241 }
5242 seq_puts(m, "\n");
5243 if (node->proc) {
5244 list_for_each_entry(w, &node->async_todo, entry)
5245 print_binder_work_ilocked(m, node->proc, " ",
5246 " pending async transaction", w);
5247 }
5248 }
5249
5250 static void print_binder_ref_olocked(struct seq_file *m,
5251 struct binder_ref *ref)
5252 {
5253 binder_node_lock(ref->node);
5254 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
5255 ref->data.debug_id, ref->data.desc,
5256 ref->node->proc ? "" : "dead ",
5257 ref->node->debug_id, ref->data.strong,
5258 ref->data.weak, ref->death);
5259 binder_node_unlock(ref->node);
5260 }
5261
5262 static void print_binder_proc(struct seq_file *m,
5263 struct binder_proc *proc, int print_all)
5264 {
5265 struct binder_work *w;
5266 struct rb_node *n;
5267 size_t start_pos = m->count;
5268 size_t header_pos;
5269 struct binder_node *last_node = NULL;
5270
5271 seq_printf(m, "proc %d\n", proc->pid);
5272 seq_printf(m, "context %s\n", proc->context->name);
5273 header_pos = m->count;
5274
5275 binder_inner_proc_lock(proc);
5276 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
5277 print_binder_thread_ilocked(m, rb_entry(n, struct binder_thread,
5278 rb_node), print_all);
5279
5280 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
5281 struct binder_node *node = rb_entry(n, struct binder_node,
5282 rb_node);
5283 /*
5284 * take a temporary reference on the node so it
5285 * survives and isn't removed from the tree
5286 * while we print it.
5287 */
5288 binder_inc_node_tmpref_ilocked(node);
5289 /* Need to drop inner lock to take node lock */
5290 binder_inner_proc_unlock(proc);
5291 if (last_node)
5292 binder_put_node(last_node);
5293 binder_node_inner_lock(node);
5294 print_binder_node_nilocked(m, node);
5295 binder_node_inner_unlock(node);
5296 last_node = node;
5297 binder_inner_proc_lock(proc);
5298 }
5299 binder_inner_proc_unlock(proc);
5300 if (last_node)
5301 binder_put_node(last_node);
5302
5303 if (print_all) {
5304 binder_proc_lock(proc);
5305 for (n = rb_first(&proc->refs_by_desc);
5306 n != NULL;
5307 n = rb_next(n))
5308 print_binder_ref_olocked(m, rb_entry(n,
5309 struct binder_ref,
5310 rb_node_desc));
5311 binder_proc_unlock(proc);
5312 }
5313 binder_alloc_print_allocated(m, &proc->alloc);
5314 binder_inner_proc_lock(proc);
5315 list_for_each_entry(w, &proc->todo, entry)
5316 print_binder_work_ilocked(m, proc, " ",
5317 " pending transaction", w);
5318 list_for_each_entry(w, &proc->delivered_death, entry) {
5319 seq_puts(m, " has delivered dead binder\n");
5320 break;
5321 }
5322 binder_inner_proc_unlock(proc);
5323 if (!print_all && m->count == header_pos)
5324 m->count = start_pos;
5325 }
5326
5327 static const char * const binder_return_strings[] = {
5328 "BR_ERROR",
5329 "BR_OK",
5330 "BR_TRANSACTION",
5331 "BR_REPLY",
5332 "BR_ACQUIRE_RESULT",
5333 "BR_DEAD_REPLY",
5334 "BR_TRANSACTION_COMPLETE",
5335 "BR_INCREFS",
5336 "BR_ACQUIRE",
5337 "BR_RELEASE",
5338 "BR_DECREFS",
5339 "BR_ATTEMPT_ACQUIRE",
5340 "BR_NOOP",
5341 "BR_SPAWN_LOOPER",
5342 "BR_FINISHED",
5343 "BR_DEAD_BINDER",
5344 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
5345 "BR_FAILED_REPLY"
5346 };
5347
5348 static const char * const binder_command_strings[] = {
5349 "BC_TRANSACTION",
5350 "BC_REPLY",
5351 "BC_ACQUIRE_RESULT",
5352 "BC_FREE_BUFFER",
5353 "BC_INCREFS",
5354 "BC_ACQUIRE",
5355 "BC_RELEASE",
5356 "BC_DECREFS",
5357 "BC_INCREFS_DONE",
5358 "BC_ACQUIRE_DONE",
5359 "BC_ATTEMPT_ACQUIRE",
5360 "BC_REGISTER_LOOPER",
5361 "BC_ENTER_LOOPER",
5362 "BC_EXIT_LOOPER",
5363 "BC_REQUEST_DEATH_NOTIFICATION",
5364 "BC_CLEAR_DEATH_NOTIFICATION",
5365 "BC_DEAD_BINDER_DONE",
5366 "BC_TRANSACTION_SG",
5367 "BC_REPLY_SG",
5368 };
5369
5370 static const char * const binder_objstat_strings[] = {
5371 "proc",
5372 "thread",
5373 "node",
5374 "ref",
5375 "death",
5376 "transaction",
5377 "transaction_complete"
5378 };
5379
5380 static void print_binder_stats(struct seq_file *m, const char *prefix,
5381 struct binder_stats *stats)
5382 {
5383 int i;
5384
5385 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
5386 ARRAY_SIZE(binder_command_strings));
5387 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
5388 int temp = atomic_read(&stats->bc[i]);
5389
5390 if (temp)
5391 seq_printf(m, "%s%s: %d\n", prefix,
5392 binder_command_strings[i], temp);
5393 }
5394
5395 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
5396 ARRAY_SIZE(binder_return_strings));
5397 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
5398 int temp = atomic_read(&stats->br[i]);
5399
5400 if (temp)
5401 seq_printf(m, "%s%s: %d\n", prefix,
5402 binder_return_strings[i], temp);
5403 }
5404
5405 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
5406 ARRAY_SIZE(binder_objstat_strings));
5407 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
5408 ARRAY_SIZE(stats->obj_deleted));
5409 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
5410 int created = atomic_read(&stats->obj_created[i]);
5411 int deleted = atomic_read(&stats->obj_deleted[i]);
5412
5413 if (created || deleted)
5414 seq_printf(m, "%s%s: active %d total %d\n",
5415 prefix,
5416 binder_objstat_strings[i],
5417 created - deleted,
5418 created);
5419 }
5420 }
5421
5422 static void print_binder_proc_stats(struct seq_file *m,
5423 struct binder_proc *proc)
5424 {
5425 struct binder_work *w;
5426 struct binder_thread *thread;
5427 struct rb_node *n;
5428 int count, strong, weak, ready_threads;
5429 size_t free_async_space =
5430 binder_alloc_get_free_async_space(&proc->alloc);
5431
5432 seq_printf(m, "proc %d\n", proc->pid);
5433 seq_printf(m, "context %s\n", proc->context->name);
5434 count = 0;
5435 ready_threads = 0;
5436 binder_inner_proc_lock(proc);
5437 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
5438 count++;
5439
5440 list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node)
5441 ready_threads++;
5442
5443 seq_printf(m, " threads: %d\n", count);
5444 seq_printf(m, " requested threads: %d+%d/%d\n"
5445 " ready threads %d\n"
5446 " free async space %zd\n", proc->requested_threads,
5447 proc->requested_threads_started, proc->max_threads,
5448 ready_threads,
5449 free_async_space);
5450 count = 0;
5451 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
5452 count++;
5453 binder_inner_proc_unlock(proc);
5454 seq_printf(m, " nodes: %d\n", count);
5455 count = 0;
5456 strong = 0;
5457 weak = 0;
5458 binder_proc_lock(proc);
5459 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
5460 struct binder_ref *ref = rb_entry(n, struct binder_ref,
5461 rb_node_desc);
5462 count++;
5463 strong += ref->data.strong;
5464 weak += ref->data.weak;
5465 }
5466 binder_proc_unlock(proc);
5467 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
5468
5469 count = binder_alloc_get_allocated_count(&proc->alloc);
5470 seq_printf(m, " buffers: %d\n", count);
5471
5472 binder_alloc_print_pages(m, &proc->alloc);
5473
5474 count = 0;
5475 binder_inner_proc_lock(proc);
5476 list_for_each_entry(w, &proc->todo, entry) {
5477 if (w->type == BINDER_WORK_TRANSACTION)
5478 count++;
5479 }
5480 binder_inner_proc_unlock(proc);
5481 seq_printf(m, " pending transactions: %d\n", count);
5482
5483 print_binder_stats(m, " ", &proc->stats);
5484 }
5485
5486
5487 static int binder_state_show(struct seq_file *m, void *unused)
5488 {
5489 struct binder_proc *proc;
5490 struct binder_node *node;
5491 struct binder_node *last_node = NULL;
5492
5493 seq_puts(m, "binder state:\n");
5494
5495 spin_lock(&binder_dead_nodes_lock);
5496 if (!hlist_empty(&binder_dead_nodes))
5497 seq_puts(m, "dead nodes:\n");
5498 hlist_for_each_entry(node, &binder_dead_nodes, dead_node) {
5499 /*
5500 * take a temporary reference on the node so it
5501 * survives and isn't removed from the list
5502 * while we print it.
5503 */
5504 node->tmp_refs++;
5505 spin_unlock(&binder_dead_nodes_lock);
5506 if (last_node)
5507 binder_put_node(last_node);
5508 binder_node_lock(node);
5509 print_binder_node_nilocked(m, node);
5510 binder_node_unlock(node);
5511 last_node = node;
5512 spin_lock(&binder_dead_nodes_lock);
5513 }
5514 spin_unlock(&binder_dead_nodes_lock);
5515 if (last_node)
5516 binder_put_node(last_node);
5517
5518 mutex_lock(&binder_procs_lock);
5519 hlist_for_each_entry(proc, &binder_procs, proc_node)
5520 print_binder_proc(m, proc, 1);
5521 mutex_unlock(&binder_procs_lock);
5522
5523 return 0;
5524 }
5525
5526 static int binder_stats_show(struct seq_file *m, void *unused)
5527 {
5528 struct binder_proc *proc;
5529
5530 seq_puts(m, "binder stats:\n");
5531
5532 print_binder_stats(m, "", &binder_stats);
5533
5534 mutex_lock(&binder_procs_lock);
5535 hlist_for_each_entry(proc, &binder_procs, proc_node)
5536 print_binder_proc_stats(m, proc);
5537 mutex_unlock(&binder_procs_lock);
5538
5539 return 0;
5540 }
5541
5542 static int binder_transactions_show(struct seq_file *m, void *unused)
5543 {
5544 struct binder_proc *proc;
5545
5546 seq_puts(m, "binder transactions:\n");
5547 mutex_lock(&binder_procs_lock);
5548 hlist_for_each_entry(proc, &binder_procs, proc_node)
5549 print_binder_proc(m, proc, 0);
5550 mutex_unlock(&binder_procs_lock);
5551
5552 return 0;
5553 }
5554
5555 static int binder_proc_show(struct seq_file *m, void *unused)
5556 {
5557 struct binder_proc *itr;
5558 int pid = (unsigned long)m->private;
5559
5560 mutex_lock(&binder_procs_lock);
5561 hlist_for_each_entry(itr, &binder_procs, proc_node) {
5562 if (itr->pid == pid) {
5563 seq_puts(m, "binder proc state:\n");
5564 print_binder_proc(m, itr, 1);
5565 }
5566 }
5567 mutex_unlock(&binder_procs_lock);
5568
5569 return 0;
5570 }
5571
5572 static void print_binder_transaction_log_entry(struct seq_file *m,
5573 struct binder_transaction_log_entry *e)
5574 {
5575 int debug_id = READ_ONCE(e->debug_id_done);
5576 /*
5577 * read barrier to guarantee debug_id_done read before
5578 * we print the log values
5579 */
5580 smp_rmb();
5581 seq_printf(m,
5582 "%d: %s from %d:%d to %d:%d context %s node %d handle %d size %d:%d ret %d/%d l=%d",
5583 e->debug_id, (e->call_type == 2) ? "reply" :
5584 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
5585 e->from_thread, e->to_proc, e->to_thread, e->context_name,
5586 e->to_node, e->target_handle, e->data_size, e->offsets_size,
5587 e->return_error, e->return_error_param,
5588 e->return_error_line);
5589 /*
5590 * read-barrier to guarantee read of debug_id_done after
5591 * done printing the fields of the entry
5592 */
5593 smp_rmb();
5594 seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ?
5595 "\n" : " (incomplete)\n");
5596 }
5597
5598 static int binder_transaction_log_show(struct seq_file *m, void *unused)
5599 {
5600 struct binder_transaction_log *log = m->private;
5601 unsigned int log_cur = atomic_read(&log->cur);
5602 unsigned int count;
5603 unsigned int cur;
5604 int i;
5605
5606 count = log_cur + 1;
5607 cur = count < ARRAY_SIZE(log->entry) && !log->full ?
5608 0 : count % ARRAY_SIZE(log->entry);
5609 if (count > ARRAY_SIZE(log->entry) || log->full)
5610 count = ARRAY_SIZE(log->entry);
5611 for (i = 0; i < count; i++) {
5612 unsigned int index = cur++ % ARRAY_SIZE(log->entry);
5613
5614 print_binder_transaction_log_entry(m, &log->entry[index]);
5615 }
5616 return 0;
5617 }
5618
5619 static const struct file_operations binder_fops = {
5620 .owner = THIS_MODULE,
5621 .poll = binder_poll,
5622 .unlocked_ioctl = binder_ioctl,
5623 .compat_ioctl = binder_ioctl,
5624 .mmap = binder_mmap,
5625 .open = binder_open,
5626 .flush = binder_flush,
5627 .release = binder_release,
5628 };
5629
5630 BINDER_DEBUG_ENTRY(state);
5631 BINDER_DEBUG_ENTRY(stats);
5632 BINDER_DEBUG_ENTRY(transactions);
5633 BINDER_DEBUG_ENTRY(transaction_log);
5634
5635 static int __init init_binder_device(const char *name)
5636 {
5637 int ret;
5638 struct binder_device *binder_device;
5639
5640 binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
5641 if (!binder_device)
5642 return -ENOMEM;
5643
5644 binder_device->miscdev.fops = &binder_fops;
5645 binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
5646 binder_device->miscdev.name = name;
5647
5648 binder_device->context.binder_context_mgr_uid = INVALID_UID;
5649 binder_device->context.name = name;
5650 mutex_init(&binder_device->context.context_mgr_node_lock);
5651
5652 ret = misc_register(&binder_device->miscdev);
5653 if (ret < 0) {
5654 kfree(binder_device);
5655 return ret;
5656 }
5657
5658 hlist_add_head(&binder_device->hlist, &binder_devices);
5659
5660 return ret;
5661 }
5662
5663 static int __init binder_init(void)
5664 {
5665 int ret;
5666 char *device_name, *device_names, *device_tmp;
5667 struct binder_device *device;
5668 struct hlist_node *tmp;
5669
5670 binder_alloc_shrinker_init();
5671
5672 atomic_set(&binder_transaction_log.cur, ~0U);
5673 atomic_set(&binder_transaction_log_failed.cur, ~0U);
5674
5675 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
5676 if (binder_debugfs_dir_entry_root)
5677 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
5678 binder_debugfs_dir_entry_root);
5679
5680 if (binder_debugfs_dir_entry_root) {
5681 debugfs_create_file("state",
5682 S_IRUGO,
5683 binder_debugfs_dir_entry_root,
5684 NULL,
5685 &binder_state_fops);
5686 debugfs_create_file("stats",
5687 S_IRUGO,
5688 binder_debugfs_dir_entry_root,
5689 NULL,
5690 &binder_stats_fops);
5691 debugfs_create_file("transactions",
5692 S_IRUGO,
5693 binder_debugfs_dir_entry_root,
5694 NULL,
5695 &binder_transactions_fops);
5696 debugfs_create_file("transaction_log",
5697 S_IRUGO,
5698 binder_debugfs_dir_entry_root,
5699 &binder_transaction_log,
5700 &binder_transaction_log_fops);
5701 debugfs_create_file("failed_transaction_log",
5702 S_IRUGO,
5703 binder_debugfs_dir_entry_root,
5704 &binder_transaction_log_failed,
5705 &binder_transaction_log_fops);
5706 }
5707
5708 /*
5709 * Copy the module_parameter string, because we don't want to
5710 * tokenize it in-place.
5711 */
5712 device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL);
5713 if (!device_names) {
5714 ret = -ENOMEM;
5715 goto err_alloc_device_names_failed;
5716 }
5717 strcpy(device_names, binder_devices_param);
5718
5719 device_tmp = device_names;
5720 while ((device_name = strsep(&device_tmp, ","))) {
5721 ret = init_binder_device(device_name);
5722 if (ret)
5723 goto err_init_binder_device_failed;
5724 }
5725
5726 return ret;
5727
5728 err_init_binder_device_failed:
5729 hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
5730 misc_deregister(&device->miscdev);
5731 hlist_del(&device->hlist);
5732 kfree(device);
5733 }
5734
5735 kfree(device_names);
5736
5737 err_alloc_device_names_failed:
5738 debugfs_remove_recursive(binder_debugfs_dir_entry_root);
5739
5740 return ret;
5741 }
5742
5743 device_initcall(binder_init);
5744
5745 #define CREATE_TRACE_POINTS
5746 #include "binder_trace.h"
5747
5748 MODULE_LICENSE("GPL v2");