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