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