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