UPSTREAM: binder: check for overflow when alloc for security context
[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 if (t->buffer)
2132 t->buffer->transaction = NULL;
2133 kfree(t);
2134 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2135 }
2136
2137 static void binder_send_failed_reply(struct binder_transaction *t,
2138 uint32_t error_code)
2139 {
2140 struct binder_thread *target_thread;
2141 struct binder_transaction *next;
2142
2143 BUG_ON(t->flags & TF_ONE_WAY);
2144 while (1) {
2145 target_thread = binder_get_txn_from_and_acq_inner(t);
2146 if (target_thread) {
2147 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2148 "send failed reply for transaction %d to %d:%d\n",
2149 t->debug_id,
2150 target_thread->proc->pid,
2151 target_thread->pid);
2152
2153 binder_pop_transaction_ilocked(target_thread, t);
2154 if (target_thread->reply_error.cmd == BR_OK) {
2155 target_thread->reply_error.cmd = error_code;
2156 binder_enqueue_thread_work_ilocked(
2157 target_thread,
2158 &target_thread->reply_error.work);
2159 wake_up_interruptible(&target_thread->wait);
2160 } else {
2161 WARN(1, "Unexpected reply error: %u\n",
2162 target_thread->reply_error.cmd);
2163 }
2164 binder_inner_proc_unlock(target_thread->proc);
2165 binder_thread_dec_tmpref(target_thread);
2166 binder_free_transaction(t);
2167 return;
2168 }
2169 next = t->from_parent;
2170
2171 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2172 "send failed reply for transaction %d, target dead\n",
2173 t->debug_id);
2174
2175 binder_free_transaction(t);
2176 if (next == NULL) {
2177 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2178 "reply failed, no target thread at root\n");
2179 return;
2180 }
2181 t = next;
2182 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2183 "reply failed, no target thread -- retry %d\n",
2184 t->debug_id);
2185 }
2186 }
2187
2188 /**
2189 * binder_cleanup_transaction() - cleans up undelivered transaction
2190 * @t: transaction that needs to be cleaned up
2191 * @reason: reason the transaction wasn't delivered
2192 * @error_code: error to return to caller (if synchronous call)
2193 */
2194 static void binder_cleanup_transaction(struct binder_transaction *t,
2195 const char *reason,
2196 uint32_t error_code)
2197 {
2198 if (t->buffer->target_node && !(t->flags & TF_ONE_WAY)) {
2199 binder_send_failed_reply(t, error_code);
2200 } else {
2201 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2202 "undelivered transaction %d, %s\n",
2203 t->debug_id, reason);
2204 binder_free_transaction(t);
2205 }
2206 }
2207
2208 /**
2209 * binder_validate_object() - checks for a valid metadata object in a buffer.
2210 * @buffer: binder_buffer that we're parsing.
2211 * @offset: offset in the buffer at which to validate an object.
2212 *
2213 * Return: If there's a valid metadata object at @offset in @buffer, the
2214 * size of that object. Otherwise, it returns zero.
2215 */
2216 static size_t binder_validate_object(struct binder_buffer *buffer, u64 offset)
2217 {
2218 /* Check if we can read a header first */
2219 struct binder_object_header *hdr;
2220 size_t object_size = 0;
2221
2222 if (offset > buffer->data_size - sizeof(*hdr) ||
2223 buffer->data_size < sizeof(*hdr) ||
2224 !IS_ALIGNED(offset, sizeof(u32)))
2225 return 0;
2226
2227 /* Ok, now see if we can read a complete object. */
2228 hdr = (struct binder_object_header *)(buffer->data + offset);
2229 switch (hdr->type) {
2230 case BINDER_TYPE_BINDER:
2231 case BINDER_TYPE_WEAK_BINDER:
2232 case BINDER_TYPE_HANDLE:
2233 case BINDER_TYPE_WEAK_HANDLE:
2234 object_size = sizeof(struct flat_binder_object);
2235 break;
2236 case BINDER_TYPE_FD:
2237 object_size = sizeof(struct binder_fd_object);
2238 break;
2239 case BINDER_TYPE_PTR:
2240 object_size = sizeof(struct binder_buffer_object);
2241 break;
2242 case BINDER_TYPE_FDA:
2243 object_size = sizeof(struct binder_fd_array_object);
2244 break;
2245 default:
2246 return 0;
2247 }
2248 if (offset <= buffer->data_size - object_size &&
2249 buffer->data_size >= object_size)
2250 return object_size;
2251 else
2252 return 0;
2253 }
2254
2255 /**
2256 * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
2257 * @b: binder_buffer containing the object
2258 * @index: index in offset array at which the binder_buffer_object is
2259 * located
2260 * @start: points to the start of the offset array
2261 * @num_valid: the number of valid offsets in the offset array
2262 *
2263 * Return: If @index is within the valid range of the offset array
2264 * described by @start and @num_valid, and if there's a valid
2265 * binder_buffer_object at the offset found in index @index
2266 * of the offset array, that object is returned. Otherwise,
2267 * %NULL is returned.
2268 * Note that the offset found in index @index itself is not
2269 * verified; this function assumes that @num_valid elements
2270 * from @start were previously verified to have valid offsets.
2271 */
2272 static struct binder_buffer_object *binder_validate_ptr(struct binder_buffer *b,
2273 binder_size_t index,
2274 binder_size_t *start,
2275 binder_size_t num_valid)
2276 {
2277 struct binder_buffer_object *buffer_obj;
2278 binder_size_t *offp;
2279
2280 if (index >= num_valid)
2281 return NULL;
2282
2283 offp = start + index;
2284 buffer_obj = (struct binder_buffer_object *)(b->data + *offp);
2285 if (buffer_obj->hdr.type != BINDER_TYPE_PTR)
2286 return NULL;
2287
2288 return buffer_obj;
2289 }
2290
2291 /**
2292 * binder_validate_fixup() - validates pointer/fd fixups happen in order.
2293 * @b: transaction buffer
2294 * @objects_start start of objects buffer
2295 * @buffer: binder_buffer_object in which to fix up
2296 * @offset: start offset in @buffer to fix up
2297 * @last_obj: last binder_buffer_object that we fixed up in
2298 * @last_min_offset: minimum fixup offset in @last_obj
2299 *
2300 * Return: %true if a fixup in buffer @buffer at offset @offset is
2301 * allowed.
2302 *
2303 * For safety reasons, we only allow fixups inside a buffer to happen
2304 * at increasing offsets; additionally, we only allow fixup on the last
2305 * buffer object that was verified, or one of its parents.
2306 *
2307 * Example of what is allowed:
2308 *
2309 * A
2310 * B (parent = A, offset = 0)
2311 * C (parent = A, offset = 16)
2312 * D (parent = C, offset = 0)
2313 * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
2314 *
2315 * Examples of what is not allowed:
2316 *
2317 * Decreasing offsets within the same parent:
2318 * A
2319 * C (parent = A, offset = 16)
2320 * B (parent = A, offset = 0) // decreasing offset within A
2321 *
2322 * Referring to a parent that wasn't the last object or any of its parents:
2323 * A
2324 * B (parent = A, offset = 0)
2325 * C (parent = A, offset = 0)
2326 * C (parent = A, offset = 16)
2327 * D (parent = B, offset = 0) // B is not A or any of A's parents
2328 */
2329 static bool binder_validate_fixup(struct binder_buffer *b,
2330 binder_size_t *objects_start,
2331 struct binder_buffer_object *buffer,
2332 binder_size_t fixup_offset,
2333 struct binder_buffer_object *last_obj,
2334 binder_size_t last_min_offset)
2335 {
2336 if (!last_obj) {
2337 /* Nothing to fix up in */
2338 return false;
2339 }
2340
2341 while (last_obj != buffer) {
2342 /*
2343 * Safe to retrieve the parent of last_obj, since it
2344 * was already previously verified by the driver.
2345 */
2346 if ((last_obj->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0)
2347 return false;
2348 last_min_offset = last_obj->parent_offset + sizeof(uintptr_t);
2349 last_obj = (struct binder_buffer_object *)
2350 (b->data + *(objects_start + last_obj->parent));
2351 }
2352 return (fixup_offset >= last_min_offset);
2353 }
2354
2355 static void binder_transaction_buffer_release(struct binder_proc *proc,
2356 struct binder_buffer *buffer,
2357 binder_size_t *failed_at)
2358 {
2359 binder_size_t *offp, *off_start, *off_end;
2360 int debug_id = buffer->debug_id;
2361
2362 binder_debug(BINDER_DEBUG_TRANSACTION,
2363 "%d buffer release %d, size %zd-%zd, failed at %p\n",
2364 proc->pid, buffer->debug_id,
2365 buffer->data_size, buffer->offsets_size, failed_at);
2366
2367 if (buffer->target_node)
2368 binder_dec_node(buffer->target_node, 1, 0);
2369
2370 off_start = (binder_size_t *)(buffer->data +
2371 ALIGN(buffer->data_size, sizeof(void *)));
2372 if (failed_at)
2373 off_end = failed_at;
2374 else
2375 off_end = (void *)off_start + buffer->offsets_size;
2376 for (offp = off_start; offp < off_end; offp++) {
2377 struct binder_object_header *hdr;
2378 size_t object_size = binder_validate_object(buffer, *offp);
2379
2380 if (object_size == 0) {
2381 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
2382 debug_id, (u64)*offp, buffer->data_size);
2383 continue;
2384 }
2385 hdr = (struct binder_object_header *)(buffer->data + *offp);
2386 switch (hdr->type) {
2387 case BINDER_TYPE_BINDER:
2388 case BINDER_TYPE_WEAK_BINDER: {
2389 struct flat_binder_object *fp;
2390 struct binder_node *node;
2391
2392 fp = to_flat_binder_object(hdr);
2393 node = binder_get_node(proc, fp->binder);
2394 if (node == NULL) {
2395 pr_err("transaction release %d bad node %016llx\n",
2396 debug_id, (u64)fp->binder);
2397 break;
2398 }
2399 binder_debug(BINDER_DEBUG_TRANSACTION,
2400 " node %d u%016llx\n",
2401 node->debug_id, (u64)node->ptr);
2402 binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER,
2403 0);
2404 binder_put_node(node);
2405 } break;
2406 case BINDER_TYPE_HANDLE:
2407 case BINDER_TYPE_WEAK_HANDLE: {
2408 struct flat_binder_object *fp;
2409 struct binder_ref_data rdata;
2410 int ret;
2411
2412 fp = to_flat_binder_object(hdr);
2413 ret = binder_dec_ref_for_handle(proc, fp->handle,
2414 hdr->type == BINDER_TYPE_HANDLE, &rdata);
2415
2416 if (ret) {
2417 pr_err("transaction release %d bad handle %d, ret = %d\n",
2418 debug_id, fp->handle, ret);
2419 break;
2420 }
2421 binder_debug(BINDER_DEBUG_TRANSACTION,
2422 " ref %d desc %d\n",
2423 rdata.debug_id, rdata.desc);
2424 } break;
2425
2426 case BINDER_TYPE_FD: {
2427 struct binder_fd_object *fp = to_binder_fd_object(hdr);
2428
2429 binder_debug(BINDER_DEBUG_TRANSACTION,
2430 " fd %d\n", fp->fd);
2431 if (failed_at)
2432 task_close_fd(proc, fp->fd);
2433 } break;
2434 case BINDER_TYPE_PTR:
2435 /*
2436 * Nothing to do here, this will get cleaned up when the
2437 * transaction buffer gets freed
2438 */
2439 break;
2440 case BINDER_TYPE_FDA: {
2441 struct binder_fd_array_object *fda;
2442 struct binder_buffer_object *parent;
2443 uintptr_t parent_buffer;
2444 u32 *fd_array;
2445 size_t fd_index;
2446 binder_size_t fd_buf_size;
2447
2448 fda = to_binder_fd_array_object(hdr);
2449 parent = binder_validate_ptr(buffer, fda->parent,
2450 off_start,
2451 offp - off_start);
2452 if (!parent) {
2453 pr_err("transaction release %d bad parent offset",
2454 debug_id);
2455 continue;
2456 }
2457 /*
2458 * Since the parent was already fixed up, convert it
2459 * back to kernel address space to access it
2460 */
2461 parent_buffer = parent->buffer -
2462 binder_alloc_get_user_buffer_offset(
2463 &proc->alloc);
2464
2465 fd_buf_size = sizeof(u32) * fda->num_fds;
2466 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2467 pr_err("transaction release %d invalid number of fds (%lld)\n",
2468 debug_id, (u64)fda->num_fds);
2469 continue;
2470 }
2471 if (fd_buf_size > parent->length ||
2472 fda->parent_offset > parent->length - fd_buf_size) {
2473 /* No space for all file descriptors here. */
2474 pr_err("transaction release %d not enough space for %lld fds in buffer\n",
2475 debug_id, (u64)fda->num_fds);
2476 continue;
2477 }
2478 fd_array = (u32 *)(parent_buffer + (uintptr_t)fda->parent_offset);
2479 for (fd_index = 0; fd_index < fda->num_fds; fd_index++)
2480 task_close_fd(proc, fd_array[fd_index]);
2481 } break;
2482 default:
2483 pr_err("transaction release %d bad object type %x\n",
2484 debug_id, hdr->type);
2485 break;
2486 }
2487 }
2488 }
2489
2490 static int binder_translate_binder(struct flat_binder_object *fp,
2491 struct binder_transaction *t,
2492 struct binder_thread *thread)
2493 {
2494 struct binder_node *node;
2495 struct binder_proc *proc = thread->proc;
2496 struct binder_proc *target_proc = t->to_proc;
2497 struct binder_ref_data rdata;
2498 int ret = 0;
2499
2500 node = binder_get_node(proc, fp->binder);
2501 if (!node) {
2502 node = binder_new_node(proc, fp);
2503 if (!node)
2504 return -ENOMEM;
2505 }
2506 if (fp->cookie != node->cookie) {
2507 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
2508 proc->pid, thread->pid, (u64)fp->binder,
2509 node->debug_id, (u64)fp->cookie,
2510 (u64)node->cookie);
2511 ret = -EINVAL;
2512 goto done;
2513 }
2514 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2515 ret = -EPERM;
2516 goto done;
2517 }
2518
2519 ret = binder_inc_ref_for_node(target_proc, node,
2520 fp->hdr.type == BINDER_TYPE_BINDER,
2521 &thread->todo, &rdata);
2522 if (ret)
2523 goto done;
2524
2525 if (fp->hdr.type == BINDER_TYPE_BINDER)
2526 fp->hdr.type = BINDER_TYPE_HANDLE;
2527 else
2528 fp->hdr.type = BINDER_TYPE_WEAK_HANDLE;
2529 fp->binder = 0;
2530 fp->handle = rdata.desc;
2531 fp->cookie = 0;
2532
2533 trace_binder_transaction_node_to_ref(t, node, &rdata);
2534 binder_debug(BINDER_DEBUG_TRANSACTION,
2535 " node %d u%016llx -> ref %d desc %d\n",
2536 node->debug_id, (u64)node->ptr,
2537 rdata.debug_id, rdata.desc);
2538 done:
2539 binder_put_node(node);
2540 return ret;
2541 }
2542
2543 static int binder_translate_handle(struct flat_binder_object *fp,
2544 struct binder_transaction *t,
2545 struct binder_thread *thread)
2546 {
2547 struct binder_proc *proc = thread->proc;
2548 struct binder_proc *target_proc = t->to_proc;
2549 struct binder_node *node;
2550 struct binder_ref_data src_rdata;
2551 int ret = 0;
2552
2553 node = binder_get_node_from_ref(proc, fp->handle,
2554 fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata);
2555 if (!node) {
2556 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
2557 proc->pid, thread->pid, fp->handle);
2558 return -EINVAL;
2559 }
2560 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2561 ret = -EPERM;
2562 goto done;
2563 }
2564
2565 binder_node_lock(node);
2566 if (node->proc == target_proc) {
2567 if (fp->hdr.type == BINDER_TYPE_HANDLE)
2568 fp->hdr.type = BINDER_TYPE_BINDER;
2569 else
2570 fp->hdr.type = BINDER_TYPE_WEAK_BINDER;
2571 fp->binder = node->ptr;
2572 fp->cookie = node->cookie;
2573 if (node->proc)
2574 binder_inner_proc_lock(node->proc);
2575 binder_inc_node_nilocked(node,
2576 fp->hdr.type == BINDER_TYPE_BINDER,
2577 0, NULL);
2578 if (node->proc)
2579 binder_inner_proc_unlock(node->proc);
2580 trace_binder_transaction_ref_to_node(t, node, &src_rdata);
2581 binder_debug(BINDER_DEBUG_TRANSACTION,
2582 " ref %d desc %d -> node %d u%016llx\n",
2583 src_rdata.debug_id, src_rdata.desc, node->debug_id,
2584 (u64)node->ptr);
2585 binder_node_unlock(node);
2586 } else {
2587 struct binder_ref_data dest_rdata;
2588
2589 binder_node_unlock(node);
2590 ret = binder_inc_ref_for_node(target_proc, node,
2591 fp->hdr.type == BINDER_TYPE_HANDLE,
2592 NULL, &dest_rdata);
2593 if (ret)
2594 goto done;
2595
2596 fp->binder = 0;
2597 fp->handle = dest_rdata.desc;
2598 fp->cookie = 0;
2599 trace_binder_transaction_ref_to_ref(t, node, &src_rdata,
2600 &dest_rdata);
2601 binder_debug(BINDER_DEBUG_TRANSACTION,
2602 " ref %d desc %d -> ref %d desc %d (node %d)\n",
2603 src_rdata.debug_id, src_rdata.desc,
2604 dest_rdata.debug_id, dest_rdata.desc,
2605 node->debug_id);
2606 }
2607 done:
2608 binder_put_node(node);
2609 return ret;
2610 }
2611
2612 static int binder_translate_fd(int fd,
2613 struct binder_transaction *t,
2614 struct binder_thread *thread,
2615 struct binder_transaction *in_reply_to)
2616 {
2617 struct binder_proc *proc = thread->proc;
2618 struct binder_proc *target_proc = t->to_proc;
2619 int target_fd;
2620 struct file *file;
2621 int ret;
2622 bool target_allows_fd;
2623
2624 if (in_reply_to)
2625 target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS);
2626 else
2627 target_allows_fd = t->buffer->target_node->accept_fds;
2628 if (!target_allows_fd) {
2629 binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
2630 proc->pid, thread->pid,
2631 in_reply_to ? "reply" : "transaction",
2632 fd);
2633 ret = -EPERM;
2634 goto err_fd_not_accepted;
2635 }
2636
2637 file = fget(fd);
2638 if (!file) {
2639 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
2640 proc->pid, thread->pid, fd);
2641 ret = -EBADF;
2642 goto err_fget;
2643 }
2644 ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file);
2645 if (ret < 0) {
2646 ret = -EPERM;
2647 goto err_security;
2648 }
2649
2650 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
2651 if (target_fd < 0) {
2652 ret = -ENOMEM;
2653 goto err_get_unused_fd;
2654 }
2655 task_fd_install(target_proc, target_fd, file);
2656 trace_binder_transaction_fd(t, fd, target_fd);
2657 binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n",
2658 fd, target_fd);
2659
2660 return target_fd;
2661
2662 err_get_unused_fd:
2663 err_security:
2664 fput(file);
2665 err_fget:
2666 err_fd_not_accepted:
2667 return ret;
2668 }
2669
2670 static int binder_translate_fd_array(struct binder_fd_array_object *fda,
2671 struct binder_buffer_object *parent,
2672 struct binder_transaction *t,
2673 struct binder_thread *thread,
2674 struct binder_transaction *in_reply_to)
2675 {
2676 binder_size_t fdi, fd_buf_size, num_installed_fds;
2677 int target_fd;
2678 uintptr_t parent_buffer;
2679 u32 *fd_array;
2680 struct binder_proc *proc = thread->proc;
2681 struct binder_proc *target_proc = t->to_proc;
2682
2683 fd_buf_size = sizeof(u32) * fda->num_fds;
2684 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2685 binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
2686 proc->pid, thread->pid, (u64)fda->num_fds);
2687 return -EINVAL;
2688 }
2689 if (fd_buf_size > parent->length ||
2690 fda->parent_offset > parent->length - fd_buf_size) {
2691 /* No space for all file descriptors here. */
2692 binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
2693 proc->pid, thread->pid, (u64)fda->num_fds);
2694 return -EINVAL;
2695 }
2696 /*
2697 * Since the parent was already fixed up, convert it
2698 * back to the kernel address space to access it
2699 */
2700 parent_buffer = parent->buffer -
2701 binder_alloc_get_user_buffer_offset(&target_proc->alloc);
2702 fd_array = (u32 *)(parent_buffer + (uintptr_t)fda->parent_offset);
2703 if (!IS_ALIGNED((unsigned long)fd_array, sizeof(u32))) {
2704 binder_user_error("%d:%d parent offset not aligned correctly.\n",
2705 proc->pid, thread->pid);
2706 return -EINVAL;
2707 }
2708 for (fdi = 0; fdi < fda->num_fds; fdi++) {
2709 target_fd = binder_translate_fd(fd_array[fdi], t, thread,
2710 in_reply_to);
2711 if (target_fd < 0)
2712 goto err_translate_fd_failed;
2713 fd_array[fdi] = target_fd;
2714 }
2715 return 0;
2716
2717 err_translate_fd_failed:
2718 /*
2719 * Failed to allocate fd or security error, free fds
2720 * installed so far.
2721 */
2722 num_installed_fds = fdi;
2723 for (fdi = 0; fdi < num_installed_fds; fdi++)
2724 task_close_fd(target_proc, fd_array[fdi]);
2725 return target_fd;
2726 }
2727
2728 static int binder_fixup_parent(struct binder_transaction *t,
2729 struct binder_thread *thread,
2730 struct binder_buffer_object *bp,
2731 binder_size_t *off_start,
2732 binder_size_t num_valid,
2733 struct binder_buffer_object *last_fixup_obj,
2734 binder_size_t last_fixup_min_off)
2735 {
2736 struct binder_buffer_object *parent;
2737 u8 *parent_buffer;
2738 struct binder_buffer *b = t->buffer;
2739 struct binder_proc *proc = thread->proc;
2740 struct binder_proc *target_proc = t->to_proc;
2741
2742 if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT))
2743 return 0;
2744
2745 parent = binder_validate_ptr(b, bp->parent, off_start, num_valid);
2746 if (!parent) {
2747 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
2748 proc->pid, thread->pid);
2749 return -EINVAL;
2750 }
2751
2752 if (!binder_validate_fixup(b, off_start,
2753 parent, bp->parent_offset,
2754 last_fixup_obj,
2755 last_fixup_min_off)) {
2756 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
2757 proc->pid, thread->pid);
2758 return -EINVAL;
2759 }
2760
2761 if (parent->length < sizeof(binder_uintptr_t) ||
2762 bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) {
2763 /* No space for a pointer here! */
2764 binder_user_error("%d:%d got transaction with invalid parent offset\n",
2765 proc->pid, thread->pid);
2766 return -EINVAL;
2767 }
2768 parent_buffer = (u8 *)((uintptr_t)parent->buffer -
2769 binder_alloc_get_user_buffer_offset(
2770 &target_proc->alloc));
2771 *(binder_uintptr_t *)(parent_buffer + bp->parent_offset) = bp->buffer;
2772
2773 return 0;
2774 }
2775
2776 /**
2777 * binder_proc_transaction() - sends a transaction to a process and wakes it up
2778 * @t: transaction to send
2779 * @proc: process to send the transaction to
2780 * @thread: thread in @proc to send the transaction to (may be NULL)
2781 *
2782 * This function queues a transaction to the specified process. It will try
2783 * to find a thread in the target process to handle the transaction and
2784 * wake it up. If no thread is found, the work is queued to the proc
2785 * waitqueue.
2786 *
2787 * If the @thread parameter is not NULL, the transaction is always queued
2788 * to the waitlist of that specific thread.
2789 *
2790 * Return: true if the transactions was successfully queued
2791 * false if the target process or thread is dead
2792 */
2793 static bool binder_proc_transaction(struct binder_transaction *t,
2794 struct binder_proc *proc,
2795 struct binder_thread *thread)
2796 {
2797 struct binder_node *node = t->buffer->target_node;
2798 struct binder_priority node_prio;
2799 bool oneway = !!(t->flags & TF_ONE_WAY);
2800 bool pending_async = false;
2801
2802 BUG_ON(!node);
2803 binder_node_lock(node);
2804 node_prio.prio = node->min_priority;
2805 node_prio.sched_policy = node->sched_policy;
2806
2807 if (oneway) {
2808 BUG_ON(thread);
2809 if (node->has_async_transaction) {
2810 pending_async = true;
2811 } else {
2812 node->has_async_transaction = 1;
2813 }
2814 }
2815
2816 binder_inner_proc_lock(proc);
2817
2818 if (proc->is_dead || (thread && thread->is_dead)) {
2819 binder_inner_proc_unlock(proc);
2820 binder_node_unlock(node);
2821 return false;
2822 }
2823
2824 if (!thread && !pending_async)
2825 thread = binder_select_thread_ilocked(proc);
2826
2827 if (thread) {
2828 binder_transaction_priority(thread->task, t, node_prio,
2829 node->inherit_rt);
2830 binder_enqueue_thread_work_ilocked(thread, &t->work);
2831 } else if (!pending_async) {
2832 binder_enqueue_work_ilocked(&t->work, &proc->todo);
2833 } else {
2834 binder_enqueue_work_ilocked(&t->work, &node->async_todo);
2835 }
2836
2837 if (!pending_async)
2838 binder_wakeup_thread_ilocked(proc, thread, !oneway /* sync */);
2839
2840 binder_inner_proc_unlock(proc);
2841 binder_node_unlock(node);
2842
2843 return true;
2844 }
2845
2846 /**
2847 * binder_get_node_refs_for_txn() - Get required refs on node for txn
2848 * @node: struct binder_node for which to get refs
2849 * @proc: returns @node->proc if valid
2850 * @error: if no @proc then returns BR_DEAD_REPLY
2851 *
2852 * User-space normally keeps the node alive when creating a transaction
2853 * since it has a reference to the target. The local strong ref keeps it
2854 * alive if the sending process dies before the target process processes
2855 * the transaction. If the source process is malicious or has a reference
2856 * counting bug, relying on the local strong ref can fail.
2857 *
2858 * Since user-space can cause the local strong ref to go away, we also take
2859 * a tmpref on the node to ensure it survives while we are constructing
2860 * the transaction. We also need a tmpref on the proc while we are
2861 * constructing the transaction, so we take that here as well.
2862 *
2863 * Return: The target_node with refs taken or NULL if no @node->proc is NULL.
2864 * Also sets @proc if valid. If the @node->proc is NULL indicating that the
2865 * target proc has died, @error is set to BR_DEAD_REPLY
2866 */
2867 static struct binder_node *binder_get_node_refs_for_txn(
2868 struct binder_node *node,
2869 struct binder_proc **procp,
2870 uint32_t *error)
2871 {
2872 struct binder_node *target_node = NULL;
2873
2874 binder_node_inner_lock(node);
2875 if (node->proc) {
2876 target_node = node;
2877 binder_inc_node_nilocked(node, 1, 0, NULL);
2878 binder_inc_node_tmpref_ilocked(node);
2879 node->proc->tmp_ref++;
2880 *procp = node->proc;
2881 } else
2882 *error = BR_DEAD_REPLY;
2883 binder_node_inner_unlock(node);
2884
2885 return target_node;
2886 }
2887
2888 static void binder_transaction(struct binder_proc *proc,
2889 struct binder_thread *thread,
2890 struct binder_transaction_data *tr, int reply,
2891 binder_size_t extra_buffers_size)
2892 {
2893 int ret;
2894 struct binder_transaction *t;
2895 struct binder_work *tcomplete;
2896 binder_size_t *offp, *off_end, *off_start;
2897 binder_size_t off_min;
2898 u8 *sg_bufp, *sg_buf_end;
2899 struct binder_proc *target_proc = NULL;
2900 struct binder_thread *target_thread = NULL;
2901 struct binder_node *target_node = NULL;
2902 struct binder_transaction *in_reply_to = NULL;
2903 struct binder_transaction_log_entry *e;
2904 uint32_t return_error = 0;
2905 uint32_t return_error_param = 0;
2906 uint32_t return_error_line = 0;
2907 struct binder_buffer_object *last_fixup_obj = NULL;
2908 binder_size_t last_fixup_min_off = 0;
2909 struct binder_context *context = proc->context;
2910 int t_debug_id = atomic_inc_return(&binder_last_id);
2911 char *secctx = NULL;
2912 u32 secctx_sz = 0;
2913
2914 e = binder_transaction_log_add(&binder_transaction_log);
2915 e->debug_id = t_debug_id;
2916 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
2917 e->from_proc = proc->pid;
2918 e->from_thread = thread->pid;
2919 e->target_handle = tr->target.handle;
2920 e->data_size = tr->data_size;
2921 e->offsets_size = tr->offsets_size;
2922 e->context_name = proc->context->name;
2923
2924 if (reply) {
2925 binder_inner_proc_lock(proc);
2926 in_reply_to = thread->transaction_stack;
2927 if (in_reply_to == NULL) {
2928 binder_inner_proc_unlock(proc);
2929 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
2930 proc->pid, thread->pid);
2931 return_error = BR_FAILED_REPLY;
2932 return_error_param = -EPROTO;
2933 return_error_line = __LINE__;
2934 goto err_empty_call_stack;
2935 }
2936 if (in_reply_to->to_thread != thread) {
2937 spin_lock(&in_reply_to->lock);
2938 binder_user_error("%d:%d got reply transaction with bad transaction stack, transaction %d has target %d:%d\n",
2939 proc->pid, thread->pid, in_reply_to->debug_id,
2940 in_reply_to->to_proc ?
2941 in_reply_to->to_proc->pid : 0,
2942 in_reply_to->to_thread ?
2943 in_reply_to->to_thread->pid : 0);
2944 spin_unlock(&in_reply_to->lock);
2945 binder_inner_proc_unlock(proc);
2946 return_error = BR_FAILED_REPLY;
2947 return_error_param = -EPROTO;
2948 return_error_line = __LINE__;
2949 in_reply_to = NULL;
2950 goto err_bad_call_stack;
2951 }
2952 thread->transaction_stack = in_reply_to->to_parent;
2953 binder_inner_proc_unlock(proc);
2954 target_thread = binder_get_txn_from_and_acq_inner(in_reply_to);
2955 if (target_thread == NULL) {
2956 return_error = BR_DEAD_REPLY;
2957 return_error_line = __LINE__;
2958 goto err_dead_binder;
2959 }
2960 if (target_thread->transaction_stack != in_reply_to) {
2961 binder_user_error("%d:%d got reply transaction with bad target transaction stack %d, expected %d\n",
2962 proc->pid, thread->pid,
2963 target_thread->transaction_stack ?
2964 target_thread->transaction_stack->debug_id : 0,
2965 in_reply_to->debug_id);
2966 binder_inner_proc_unlock(target_thread->proc);
2967 return_error = BR_FAILED_REPLY;
2968 return_error_param = -EPROTO;
2969 return_error_line = __LINE__;
2970 in_reply_to = NULL;
2971 target_thread = NULL;
2972 goto err_dead_binder;
2973 }
2974 target_proc = target_thread->proc;
2975 target_proc->tmp_ref++;
2976 binder_inner_proc_unlock(target_thread->proc);
2977 } else {
2978 if (tr->target.handle) {
2979 struct binder_ref *ref;
2980
2981 /*
2982 * There must already be a strong ref
2983 * on this node. If so, do a strong
2984 * increment on the node to ensure it
2985 * stays alive until the transaction is
2986 * done.
2987 */
2988 binder_proc_lock(proc);
2989 ref = binder_get_ref_olocked(proc, tr->target.handle,
2990 true);
2991 if (ref) {
2992 target_node = binder_get_node_refs_for_txn(
2993 ref->node, &target_proc,
2994 &return_error);
2995 } else {
2996 binder_user_error("%d:%d got transaction to invalid handle\n",
2997 proc->pid, thread->pid);
2998 return_error = BR_FAILED_REPLY;
2999 }
3000 binder_proc_unlock(proc);
3001 } else {
3002 mutex_lock(&context->context_mgr_node_lock);
3003 target_node = context->binder_context_mgr_node;
3004 if (target_node)
3005 target_node = binder_get_node_refs_for_txn(
3006 target_node, &target_proc,
3007 &return_error);
3008 else
3009 return_error = BR_DEAD_REPLY;
3010 mutex_unlock(&context->context_mgr_node_lock);
3011 if (target_node && target_proc == proc) {
3012 binder_user_error("%d:%d got transaction to context manager from process owning it\n",
3013 proc->pid, thread->pid);
3014 return_error = BR_FAILED_REPLY;
3015 return_error_param = -EINVAL;
3016 return_error_line = __LINE__;
3017 goto err_invalid_target_handle;
3018 }
3019 }
3020 if (!target_node) {
3021 /*
3022 * return_error is set above
3023 */
3024 return_error_param = -EINVAL;
3025 return_error_line = __LINE__;
3026 goto err_dead_binder;
3027 }
3028 e->to_node = target_node->debug_id;
3029 if (security_binder_transaction(proc->tsk,
3030 target_proc->tsk) < 0) {
3031 return_error = BR_FAILED_REPLY;
3032 return_error_param = -EPERM;
3033 return_error_line = __LINE__;
3034 goto err_invalid_target_handle;
3035 }
3036 binder_inner_proc_lock(proc);
3037 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
3038 struct binder_transaction *tmp;
3039
3040 tmp = thread->transaction_stack;
3041 if (tmp->to_thread != thread) {
3042 spin_lock(&tmp->lock);
3043 binder_user_error("%d:%d got new transaction with bad transaction stack, transaction %d has target %d:%d\n",
3044 proc->pid, thread->pid, tmp->debug_id,
3045 tmp->to_proc ? tmp->to_proc->pid : 0,
3046 tmp->to_thread ?
3047 tmp->to_thread->pid : 0);
3048 spin_unlock(&tmp->lock);
3049 binder_inner_proc_unlock(proc);
3050 return_error = BR_FAILED_REPLY;
3051 return_error_param = -EPROTO;
3052 return_error_line = __LINE__;
3053 goto err_bad_call_stack;
3054 }
3055 while (tmp) {
3056 struct binder_thread *from;
3057
3058 spin_lock(&tmp->lock);
3059 from = tmp->from;
3060 if (from && from->proc == target_proc) {
3061 atomic_inc(&from->tmp_ref);
3062 target_thread = from;
3063 spin_unlock(&tmp->lock);
3064 break;
3065 }
3066 spin_unlock(&tmp->lock);
3067 tmp = tmp->from_parent;
3068 }
3069 }
3070 binder_inner_proc_unlock(proc);
3071 }
3072 if (target_thread)
3073 e->to_thread = target_thread->pid;
3074 e->to_proc = target_proc->pid;
3075
3076 /* TODO: reuse incoming transaction for reply */
3077 t = kzalloc(sizeof(*t), GFP_KERNEL);
3078 if (t == NULL) {
3079 return_error = BR_FAILED_REPLY;
3080 return_error_param = -ENOMEM;
3081 return_error_line = __LINE__;
3082 goto err_alloc_t_failed;
3083 }
3084 binder_stats_created(BINDER_STAT_TRANSACTION);
3085 spin_lock_init(&t->lock);
3086
3087 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
3088 if (tcomplete == NULL) {
3089 return_error = BR_FAILED_REPLY;
3090 return_error_param = -ENOMEM;
3091 return_error_line = __LINE__;
3092 goto err_alloc_tcomplete_failed;
3093 }
3094 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
3095
3096 t->debug_id = t_debug_id;
3097
3098 if (reply)
3099 binder_debug(BINDER_DEBUG_TRANSACTION,
3100 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
3101 proc->pid, thread->pid, t->debug_id,
3102 target_proc->pid, target_thread->pid,
3103 (u64)tr->data.ptr.buffer,
3104 (u64)tr->data.ptr.offsets,
3105 (u64)tr->data_size, (u64)tr->offsets_size,
3106 (u64)extra_buffers_size);
3107 else
3108 binder_debug(BINDER_DEBUG_TRANSACTION,
3109 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
3110 proc->pid, thread->pid, t->debug_id,
3111 target_proc->pid, target_node->debug_id,
3112 (u64)tr->data.ptr.buffer,
3113 (u64)tr->data.ptr.offsets,
3114 (u64)tr->data_size, (u64)tr->offsets_size,
3115 (u64)extra_buffers_size);
3116
3117 if (!reply && !(tr->flags & TF_ONE_WAY))
3118 t->from = thread;
3119 else
3120 t->from = NULL;
3121 t->sender_euid = task_euid(proc->tsk);
3122 t->to_proc = target_proc;
3123 t->to_thread = target_thread;
3124 t->code = tr->code;
3125 t->flags = tr->flags;
3126 if (!(t->flags & TF_ONE_WAY) &&
3127 binder_supported_policy(current->policy)) {
3128 /* Inherit supported policies for synchronous transactions */
3129 t->priority.sched_policy = current->policy;
3130 t->priority.prio = current->normal_prio;
3131 } else {
3132 /* Otherwise, fall back to the default priority */
3133 t->priority = target_proc->default_priority;
3134 }
3135
3136 if (target_node && target_node->txn_security_ctx) {
3137 u32 secid;
3138 size_t added_size;
3139
3140 security_task_getsecid(proc->tsk, &secid);
3141 ret = security_secid_to_secctx(secid, &secctx, &secctx_sz);
3142 if (ret) {
3143 return_error = BR_FAILED_REPLY;
3144 return_error_param = ret;
3145 return_error_line = __LINE__;
3146 goto err_get_secctx_failed;
3147 }
3148 added_size = ALIGN(secctx_sz, sizeof(u64));
3149 extra_buffers_size += added_size;
3150 if (extra_buffers_size < added_size) {
3151 /* integer overflow of extra_buffers_size */
3152 return_error = BR_FAILED_REPLY;
3153 return_error_param = EINVAL;
3154 return_error_line = __LINE__;
3155 goto err_bad_extra_size;
3156 }
3157 }
3158
3159 trace_binder_transaction(reply, t, target_node);
3160
3161 t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size,
3162 tr->offsets_size, extra_buffers_size,
3163 !reply && (t->flags & TF_ONE_WAY));
3164 if (IS_ERR(t->buffer)) {
3165 /*
3166 * -ESRCH indicates VMA cleared. The target is dying.
3167 */
3168 return_error_param = PTR_ERR(t->buffer);
3169 return_error = return_error_param == -ESRCH ?
3170 BR_DEAD_REPLY : BR_FAILED_REPLY;
3171 return_error_line = __LINE__;
3172 t->buffer = NULL;
3173 goto err_binder_alloc_buf_failed;
3174 }
3175 if (secctx) {
3176 size_t buf_offset = ALIGN(tr->data_size, sizeof(void *)) +
3177 ALIGN(tr->offsets_size, sizeof(void *)) +
3178 ALIGN(extra_buffers_size, sizeof(void *)) -
3179 ALIGN(secctx_sz, sizeof(u64));
3180 char *kptr = t->buffer->data + buf_offset;
3181
3182 t->security_ctx = (uintptr_t)kptr +
3183 binder_alloc_get_user_buffer_offset(&target_proc->alloc);
3184 memcpy(kptr, secctx, secctx_sz);
3185 security_release_secctx(secctx, secctx_sz);
3186 secctx = NULL;
3187 }
3188 t->buffer->debug_id = t->debug_id;
3189 t->buffer->transaction = t;
3190 t->buffer->target_node = target_node;
3191 trace_binder_transaction_alloc_buf(t->buffer);
3192 off_start = (binder_size_t *)(t->buffer->data +
3193 ALIGN(tr->data_size, sizeof(void *)));
3194 offp = off_start;
3195
3196 if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t)
3197 tr->data.ptr.buffer, tr->data_size)) {
3198 binder_user_error("%d:%d got transaction with invalid data ptr\n",
3199 proc->pid, thread->pid);
3200 return_error = BR_FAILED_REPLY;
3201 return_error_param = -EFAULT;
3202 return_error_line = __LINE__;
3203 goto err_copy_data_failed;
3204 }
3205 if (copy_from_user(offp, (const void __user *)(uintptr_t)
3206 tr->data.ptr.offsets, tr->offsets_size)) {
3207 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3208 proc->pid, thread->pid);
3209 return_error = BR_FAILED_REPLY;
3210 return_error_param = -EFAULT;
3211 return_error_line = __LINE__;
3212 goto err_copy_data_failed;
3213 }
3214 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
3215 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
3216 proc->pid, thread->pid, (u64)tr->offsets_size);
3217 return_error = BR_FAILED_REPLY;
3218 return_error_param = -EINVAL;
3219 return_error_line = __LINE__;
3220 goto err_bad_offset;
3221 }
3222 if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) {
3223 binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
3224 proc->pid, thread->pid,
3225 extra_buffers_size);
3226 return_error = BR_FAILED_REPLY;
3227 return_error_param = -EINVAL;
3228 return_error_line = __LINE__;
3229 goto err_bad_offset;
3230 }
3231 off_end = (void *)off_start + tr->offsets_size;
3232 sg_bufp = (u8 *)(PTR_ALIGN(off_end, sizeof(void *)));
3233 sg_buf_end = sg_bufp + extra_buffers_size;
3234 off_min = 0;
3235 for (; offp < off_end; offp++) {
3236 struct binder_object_header *hdr;
3237 size_t object_size = binder_validate_object(t->buffer, *offp);
3238
3239 if (object_size == 0 || *offp < off_min) {
3240 binder_user_error("%d:%d got transaction with invalid offset (%lld, min %lld max %lld) or object.\n",
3241 proc->pid, thread->pid, (u64)*offp,
3242 (u64)off_min,
3243 (u64)t->buffer->data_size);
3244 return_error = BR_FAILED_REPLY;
3245 return_error_param = -EINVAL;
3246 return_error_line = __LINE__;
3247 goto err_bad_offset;
3248 }
3249
3250 hdr = (struct binder_object_header *)(t->buffer->data + *offp);
3251 off_min = *offp + object_size;
3252 switch (hdr->type) {
3253 case BINDER_TYPE_BINDER:
3254 case BINDER_TYPE_WEAK_BINDER: {
3255 struct flat_binder_object *fp;
3256
3257 fp = to_flat_binder_object(hdr);
3258 ret = binder_translate_binder(fp, t, thread);
3259 if (ret < 0) {
3260 return_error = BR_FAILED_REPLY;
3261 return_error_param = ret;
3262 return_error_line = __LINE__;
3263 goto err_translate_failed;
3264 }
3265 } break;
3266 case BINDER_TYPE_HANDLE:
3267 case BINDER_TYPE_WEAK_HANDLE: {
3268 struct flat_binder_object *fp;
3269
3270 fp = to_flat_binder_object(hdr);
3271 ret = binder_translate_handle(fp, t, thread);
3272 if (ret < 0) {
3273 return_error = BR_FAILED_REPLY;
3274 return_error_param = ret;
3275 return_error_line = __LINE__;
3276 goto err_translate_failed;
3277 }
3278 } break;
3279
3280 case BINDER_TYPE_FD: {
3281 struct binder_fd_object *fp = to_binder_fd_object(hdr);
3282 int target_fd = binder_translate_fd(fp->fd, t, thread,
3283 in_reply_to);
3284
3285 if (target_fd < 0) {
3286 return_error = BR_FAILED_REPLY;
3287 return_error_param = target_fd;
3288 return_error_line = __LINE__;
3289 goto err_translate_failed;
3290 }
3291 fp->pad_binder = 0;
3292 fp->fd = target_fd;
3293 } break;
3294 case BINDER_TYPE_FDA: {
3295 struct binder_fd_array_object *fda =
3296 to_binder_fd_array_object(hdr);
3297 struct binder_buffer_object *parent =
3298 binder_validate_ptr(t->buffer, fda->parent,
3299 off_start,
3300 offp - off_start);
3301 if (!parent) {
3302 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
3303 proc->pid, thread->pid);
3304 return_error = BR_FAILED_REPLY;
3305 return_error_param = -EINVAL;
3306 return_error_line = __LINE__;
3307 goto err_bad_parent;
3308 }
3309 if (!binder_validate_fixup(t->buffer, off_start,
3310 parent, fda->parent_offset,
3311 last_fixup_obj,
3312 last_fixup_min_off)) {
3313 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\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 ret = binder_translate_fd_array(fda, parent, t, thread,
3321 in_reply_to);
3322 if (ret < 0) {
3323 return_error = BR_FAILED_REPLY;
3324 return_error_param = ret;
3325 return_error_line = __LINE__;
3326 goto err_translate_failed;
3327 }
3328 last_fixup_obj = parent;
3329 last_fixup_min_off =
3330 fda->parent_offset + sizeof(u32) * fda->num_fds;
3331 } break;
3332 case BINDER_TYPE_PTR: {
3333 struct binder_buffer_object *bp =
3334 to_binder_buffer_object(hdr);
3335 size_t buf_left = sg_buf_end - sg_bufp;
3336
3337 if (bp->length > buf_left) {
3338 binder_user_error("%d:%d got transaction with too large buffer\n",
3339 proc->pid, thread->pid);
3340 return_error = BR_FAILED_REPLY;
3341 return_error_param = -EINVAL;
3342 return_error_line = __LINE__;
3343 goto err_bad_offset;
3344 }
3345 if (copy_from_user(sg_bufp,
3346 (const void __user *)(uintptr_t)
3347 bp->buffer, bp->length)) {
3348 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3349 proc->pid, thread->pid);
3350 return_error_param = -EFAULT;
3351 return_error = BR_FAILED_REPLY;
3352 return_error_line = __LINE__;
3353 goto err_copy_data_failed;
3354 }
3355 /* Fixup buffer pointer to target proc address space */
3356 bp->buffer = (uintptr_t)sg_bufp +
3357 binder_alloc_get_user_buffer_offset(
3358 &target_proc->alloc);
3359 sg_bufp += ALIGN(bp->length, sizeof(u64));
3360
3361 ret = binder_fixup_parent(t, thread, bp, off_start,
3362 offp - off_start,
3363 last_fixup_obj,
3364 last_fixup_min_off);
3365 if (ret < 0) {
3366 return_error = BR_FAILED_REPLY;
3367 return_error_param = ret;
3368 return_error_line = __LINE__;
3369 goto err_translate_failed;
3370 }
3371 last_fixup_obj = bp;
3372 last_fixup_min_off = 0;
3373 } break;
3374 default:
3375 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
3376 proc->pid, thread->pid, hdr->type);
3377 return_error = BR_FAILED_REPLY;
3378 return_error_param = -EINVAL;
3379 return_error_line = __LINE__;
3380 goto err_bad_object_type;
3381 }
3382 }
3383 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
3384 t->work.type = BINDER_WORK_TRANSACTION;
3385
3386 if (reply) {
3387 binder_enqueue_thread_work(thread, tcomplete);
3388 binder_inner_proc_lock(target_proc);
3389 if (target_thread->is_dead) {
3390 binder_inner_proc_unlock(target_proc);
3391 goto err_dead_proc_or_thread;
3392 }
3393 BUG_ON(t->buffer->async_transaction != 0);
3394 binder_pop_transaction_ilocked(target_thread, in_reply_to);
3395 binder_enqueue_thread_work_ilocked(target_thread, &t->work);
3396 binder_inner_proc_unlock(target_proc);
3397 wake_up_interruptible_sync(&target_thread->wait);
3398 binder_restore_priority(current, in_reply_to->saved_priority);
3399 binder_free_transaction(in_reply_to);
3400 } else if (!(t->flags & TF_ONE_WAY)) {
3401 BUG_ON(t->buffer->async_transaction != 0);
3402 binder_inner_proc_lock(proc);
3403 /*
3404 * Defer the TRANSACTION_COMPLETE, so we don't return to
3405 * userspace immediately; this allows the target process to
3406 * immediately start processing this transaction, reducing
3407 * latency. We will then return the TRANSACTION_COMPLETE when
3408 * the target replies (or there is an error).
3409 */
3410 binder_enqueue_deferred_thread_work_ilocked(thread, tcomplete);
3411 t->need_reply = 1;
3412 t->from_parent = thread->transaction_stack;
3413 thread->transaction_stack = t;
3414 binder_inner_proc_unlock(proc);
3415 if (!binder_proc_transaction(t, target_proc, target_thread)) {
3416 binder_inner_proc_lock(proc);
3417 binder_pop_transaction_ilocked(thread, t);
3418 binder_inner_proc_unlock(proc);
3419 goto err_dead_proc_or_thread;
3420 }
3421 } else {
3422 BUG_ON(target_node == NULL);
3423 BUG_ON(t->buffer->async_transaction != 1);
3424 binder_enqueue_thread_work(thread, tcomplete);
3425 if (!binder_proc_transaction(t, target_proc, NULL))
3426 goto err_dead_proc_or_thread;
3427 }
3428 if (target_thread)
3429 binder_thread_dec_tmpref(target_thread);
3430 binder_proc_dec_tmpref(target_proc);
3431 if (target_node)
3432 binder_dec_node_tmpref(target_node);
3433 /*
3434 * write barrier to synchronize with initialization
3435 * of log entry
3436 */
3437 smp_wmb();
3438 WRITE_ONCE(e->debug_id_done, t_debug_id);
3439 return;
3440
3441 err_dead_proc_or_thread:
3442 return_error = BR_DEAD_REPLY;
3443 return_error_line = __LINE__;
3444 binder_dequeue_work(proc, tcomplete);
3445 err_translate_failed:
3446 err_bad_object_type:
3447 err_bad_offset:
3448 err_bad_parent:
3449 err_copy_data_failed:
3450 trace_binder_transaction_failed_buffer_release(t->buffer);
3451 binder_transaction_buffer_release(target_proc, t->buffer, offp);
3452 if (target_node)
3453 binder_dec_node_tmpref(target_node);
3454 target_node = NULL;
3455 t->buffer->transaction = NULL;
3456 binder_alloc_free_buf(&target_proc->alloc, t->buffer);
3457 err_binder_alloc_buf_failed:
3458 err_bad_extra_size:
3459 if (secctx)
3460 security_release_secctx(secctx, secctx_sz);
3461 err_get_secctx_failed:
3462 kfree(tcomplete);
3463 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3464 err_alloc_tcomplete_failed:
3465 kfree(t);
3466 binder_stats_deleted(BINDER_STAT_TRANSACTION);
3467 err_alloc_t_failed:
3468 err_bad_call_stack:
3469 err_empty_call_stack:
3470 err_dead_binder:
3471 err_invalid_target_handle:
3472 if (target_thread)
3473 binder_thread_dec_tmpref(target_thread);
3474 if (target_proc)
3475 binder_proc_dec_tmpref(target_proc);
3476 if (target_node) {
3477 binder_dec_node(target_node, 1, 0);
3478 binder_dec_node_tmpref(target_node);
3479 }
3480
3481 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
3482 "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
3483 proc->pid, thread->pid, return_error, return_error_param,
3484 (u64)tr->data_size, (u64)tr->offsets_size,
3485 return_error_line);
3486
3487 {
3488 struct binder_transaction_log_entry *fe;
3489
3490 e->return_error = return_error;
3491 e->return_error_param = return_error_param;
3492 e->return_error_line = return_error_line;
3493 fe = binder_transaction_log_add(&binder_transaction_log_failed);
3494 *fe = *e;
3495 /*
3496 * write barrier to synchronize with initialization
3497 * of log entry
3498 */
3499 smp_wmb();
3500 WRITE_ONCE(e->debug_id_done, t_debug_id);
3501 WRITE_ONCE(fe->debug_id_done, t_debug_id);
3502 }
3503
3504 BUG_ON(thread->return_error.cmd != BR_OK);
3505 if (in_reply_to) {
3506 binder_restore_priority(current, in_reply_to->saved_priority);
3507 thread->return_error.cmd = BR_TRANSACTION_COMPLETE;
3508 binder_enqueue_thread_work(thread, &thread->return_error.work);
3509 binder_send_failed_reply(in_reply_to, return_error);
3510 } else {
3511 thread->return_error.cmd = return_error;
3512 binder_enqueue_thread_work(thread, &thread->return_error.work);
3513 }
3514 }
3515
3516 int binder_thread_write(struct binder_proc *proc,
3517 struct binder_thread *thread,
3518 binder_uintptr_t binder_buffer, size_t size,
3519 binder_size_t *consumed)
3520 {
3521 uint32_t cmd;
3522 struct binder_context *context = proc->context;
3523 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
3524 void __user *ptr = buffer + *consumed;
3525 void __user *end = buffer + size;
3526
3527 while (ptr < end && thread->return_error.cmd == BR_OK) {
3528 int ret;
3529
3530 if (get_user(cmd, (uint32_t __user *)ptr))
3531 return -EFAULT;
3532 ptr += sizeof(uint32_t);
3533 trace_binder_command(cmd);
3534 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
3535 atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]);
3536 atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]);
3537 atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]);
3538 }
3539 switch (cmd) {
3540 case BC_INCREFS:
3541 case BC_ACQUIRE:
3542 case BC_RELEASE:
3543 case BC_DECREFS: {
3544 uint32_t target;
3545 const char *debug_string;
3546 bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE;
3547 bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE;
3548 struct binder_ref_data rdata;
3549
3550 if (get_user(target, (uint32_t __user *)ptr))
3551 return -EFAULT;
3552
3553 ptr += sizeof(uint32_t);
3554 ret = -1;
3555 if (increment && !target) {
3556 struct binder_node *ctx_mgr_node;
3557 mutex_lock(&context->context_mgr_node_lock);
3558 ctx_mgr_node = context->binder_context_mgr_node;
3559 if (ctx_mgr_node)
3560 ret = binder_inc_ref_for_node(
3561 proc, ctx_mgr_node,
3562 strong, NULL, &rdata);
3563 mutex_unlock(&context->context_mgr_node_lock);
3564 }
3565 if (ret)
3566 ret = binder_update_ref_for_handle(
3567 proc, target, increment, strong,
3568 &rdata);
3569 if (!ret && rdata.desc != target) {
3570 binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
3571 proc->pid, thread->pid,
3572 target, rdata.desc);
3573 }
3574 switch (cmd) {
3575 case BC_INCREFS:
3576 debug_string = "IncRefs";
3577 break;
3578 case BC_ACQUIRE:
3579 debug_string = "Acquire";
3580 break;
3581 case BC_RELEASE:
3582 debug_string = "Release";
3583 break;
3584 case BC_DECREFS:
3585 default:
3586 debug_string = "DecRefs";
3587 break;
3588 }
3589 if (ret) {
3590 binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
3591 proc->pid, thread->pid, debug_string,
3592 strong, target, ret);
3593 break;
3594 }
3595 binder_debug(BINDER_DEBUG_USER_REFS,
3596 "%d:%d %s ref %d desc %d s %d w %d\n",
3597 proc->pid, thread->pid, debug_string,
3598 rdata.debug_id, rdata.desc, rdata.strong,
3599 rdata.weak);
3600 break;
3601 }
3602 case BC_INCREFS_DONE:
3603 case BC_ACQUIRE_DONE: {
3604 binder_uintptr_t node_ptr;
3605 binder_uintptr_t cookie;
3606 struct binder_node *node;
3607 bool free_node;
3608
3609 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
3610 return -EFAULT;
3611 ptr += sizeof(binder_uintptr_t);
3612 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
3613 return -EFAULT;
3614 ptr += sizeof(binder_uintptr_t);
3615 node = binder_get_node(proc, node_ptr);
3616 if (node == NULL) {
3617 binder_user_error("%d:%d %s u%016llx no match\n",
3618 proc->pid, thread->pid,
3619 cmd == BC_INCREFS_DONE ?
3620 "BC_INCREFS_DONE" :
3621 "BC_ACQUIRE_DONE",
3622 (u64)node_ptr);
3623 break;
3624 }
3625 if (cookie != node->cookie) {
3626 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
3627 proc->pid, thread->pid,
3628 cmd == BC_INCREFS_DONE ?
3629 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
3630 (u64)node_ptr, node->debug_id,
3631 (u64)cookie, (u64)node->cookie);
3632 binder_put_node(node);
3633 break;
3634 }
3635 binder_node_inner_lock(node);
3636 if (cmd == BC_ACQUIRE_DONE) {
3637 if (node->pending_strong_ref == 0) {
3638 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
3639 proc->pid, thread->pid,
3640 node->debug_id);
3641 binder_node_inner_unlock(node);
3642 binder_put_node(node);
3643 break;
3644 }
3645 node->pending_strong_ref = 0;
3646 } else {
3647 if (node->pending_weak_ref == 0) {
3648 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
3649 proc->pid, thread->pid,
3650 node->debug_id);
3651 binder_node_inner_unlock(node);
3652 binder_put_node(node);
3653 break;
3654 }
3655 node->pending_weak_ref = 0;
3656 }
3657 free_node = binder_dec_node_nilocked(node,
3658 cmd == BC_ACQUIRE_DONE, 0);
3659 WARN_ON(free_node);
3660 binder_debug(BINDER_DEBUG_USER_REFS,
3661 "%d:%d %s node %d ls %d lw %d tr %d\n",
3662 proc->pid, thread->pid,
3663 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
3664 node->debug_id, node->local_strong_refs,
3665 node->local_weak_refs, node->tmp_refs);
3666 binder_node_inner_unlock(node);
3667 binder_put_node(node);
3668 break;
3669 }
3670 case BC_ATTEMPT_ACQUIRE:
3671 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
3672 return -EINVAL;
3673 case BC_ACQUIRE_RESULT:
3674 pr_err("BC_ACQUIRE_RESULT not supported\n");
3675 return -EINVAL;
3676
3677 case BC_FREE_BUFFER: {
3678 binder_uintptr_t data_ptr;
3679 struct binder_buffer *buffer;
3680
3681 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
3682 return -EFAULT;
3683 ptr += sizeof(binder_uintptr_t);
3684
3685 buffer = binder_alloc_prepare_to_free(&proc->alloc,
3686 data_ptr);
3687 if (IS_ERR_OR_NULL(buffer)) {
3688 if (PTR_ERR(buffer) == -EPERM) {
3689 binder_user_error(
3690 "%d:%d BC_FREE_BUFFER u%016llx matched unreturned or currently freeing buffer\n",
3691 proc->pid, thread->pid,
3692 (u64)data_ptr);
3693 } else {
3694 binder_user_error(
3695 "%d:%d BC_FREE_BUFFER u%016llx no match\n",
3696 proc->pid, thread->pid,
3697 (u64)data_ptr);
3698 }
3699 break;
3700 }
3701 binder_debug(BINDER_DEBUG_FREE_BUFFER,
3702 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
3703 proc->pid, thread->pid, (u64)data_ptr,
3704 buffer->debug_id,
3705 buffer->transaction ? "active" : "finished");
3706
3707 if (buffer->transaction) {
3708 buffer->transaction->buffer = NULL;
3709 buffer->transaction = NULL;
3710 }
3711 if (buffer->async_transaction && buffer->target_node) {
3712 struct binder_node *buf_node;
3713 struct binder_work *w;
3714
3715 buf_node = buffer->target_node;
3716 binder_node_inner_lock(buf_node);
3717 BUG_ON(!buf_node->has_async_transaction);
3718 BUG_ON(buf_node->proc != proc);
3719 w = binder_dequeue_work_head_ilocked(
3720 &buf_node->async_todo);
3721 if (!w) {
3722 buf_node->has_async_transaction = 0;
3723 } else {
3724 binder_enqueue_work_ilocked(
3725 w, &proc->todo);
3726 binder_wakeup_proc_ilocked(proc);
3727 }
3728 binder_node_inner_unlock(buf_node);
3729 }
3730 trace_binder_transaction_buffer_release(buffer);
3731 binder_transaction_buffer_release(proc, buffer, NULL);
3732 binder_alloc_free_buf(&proc->alloc, buffer);
3733 break;
3734 }
3735
3736 case BC_TRANSACTION_SG:
3737 case BC_REPLY_SG: {
3738 struct binder_transaction_data_sg tr;
3739
3740 if (copy_from_user(&tr, ptr, sizeof(tr)))
3741 return -EFAULT;
3742 ptr += sizeof(tr);
3743 binder_transaction(proc, thread, &tr.transaction_data,
3744 cmd == BC_REPLY_SG, tr.buffers_size);
3745 break;
3746 }
3747 case BC_TRANSACTION:
3748 case BC_REPLY: {
3749 struct binder_transaction_data tr;
3750
3751 if (copy_from_user(&tr, ptr, sizeof(tr)))
3752 return -EFAULT;
3753 ptr += sizeof(tr);
3754 binder_transaction(proc, thread, &tr,
3755 cmd == BC_REPLY, 0);
3756 break;
3757 }
3758
3759 case BC_REGISTER_LOOPER:
3760 binder_debug(BINDER_DEBUG_THREADS,
3761 "%d:%d BC_REGISTER_LOOPER\n",
3762 proc->pid, thread->pid);
3763 binder_inner_proc_lock(proc);
3764 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
3765 thread->looper |= BINDER_LOOPER_STATE_INVALID;
3766 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
3767 proc->pid, thread->pid);
3768 } else if (proc->requested_threads == 0) {
3769 thread->looper |= BINDER_LOOPER_STATE_INVALID;
3770 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
3771 proc->pid, thread->pid);
3772 } else {
3773 proc->requested_threads--;
3774 proc->requested_threads_started++;
3775 }
3776 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
3777 binder_inner_proc_unlock(proc);
3778 break;
3779 case BC_ENTER_LOOPER:
3780 binder_debug(BINDER_DEBUG_THREADS,
3781 "%d:%d BC_ENTER_LOOPER\n",
3782 proc->pid, thread->pid);
3783 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
3784 thread->looper |= BINDER_LOOPER_STATE_INVALID;
3785 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
3786 proc->pid, thread->pid);
3787 }
3788 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
3789 break;
3790 case BC_EXIT_LOOPER:
3791 binder_debug(BINDER_DEBUG_THREADS,
3792 "%d:%d BC_EXIT_LOOPER\n",
3793 proc->pid, thread->pid);
3794 thread->looper |= BINDER_LOOPER_STATE_EXITED;
3795 break;
3796
3797 case BC_REQUEST_DEATH_NOTIFICATION:
3798 case BC_CLEAR_DEATH_NOTIFICATION: {
3799 uint32_t target;
3800 binder_uintptr_t cookie;
3801 struct binder_ref *ref;
3802 struct binder_ref_death *death = NULL;
3803
3804 if (get_user(target, (uint32_t __user *)ptr))
3805 return -EFAULT;
3806 ptr += sizeof(uint32_t);
3807 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
3808 return -EFAULT;
3809 ptr += sizeof(binder_uintptr_t);
3810 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3811 /*
3812 * Allocate memory for death notification
3813 * before taking lock
3814 */
3815 death = kzalloc(sizeof(*death), GFP_KERNEL);
3816 if (death == NULL) {
3817 WARN_ON(thread->return_error.cmd !=
3818 BR_OK);
3819 thread->return_error.cmd = BR_ERROR;
3820 binder_enqueue_thread_work(
3821 thread,
3822 &thread->return_error.work);
3823 binder_debug(
3824 BINDER_DEBUG_FAILED_TRANSACTION,
3825 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
3826 proc->pid, thread->pid);
3827 break;
3828 }
3829 }
3830 binder_proc_lock(proc);
3831 ref = binder_get_ref_olocked(proc, target, false);
3832 if (ref == NULL) {
3833 binder_user_error("%d:%d %s invalid ref %d\n",
3834 proc->pid, thread->pid,
3835 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3836 "BC_REQUEST_DEATH_NOTIFICATION" :
3837 "BC_CLEAR_DEATH_NOTIFICATION",
3838 target);
3839 binder_proc_unlock(proc);
3840 kfree(death);
3841 break;
3842 }
3843
3844 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
3845 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
3846 proc->pid, thread->pid,
3847 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3848 "BC_REQUEST_DEATH_NOTIFICATION" :
3849 "BC_CLEAR_DEATH_NOTIFICATION",
3850 (u64)cookie, ref->data.debug_id,
3851 ref->data.desc, ref->data.strong,
3852 ref->data.weak, ref->node->debug_id);
3853
3854 binder_node_lock(ref->node);
3855 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3856 if (ref->death) {
3857 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
3858 proc->pid, thread->pid);
3859 binder_node_unlock(ref->node);
3860 binder_proc_unlock(proc);
3861 kfree(death);
3862 break;
3863 }
3864 binder_stats_created(BINDER_STAT_DEATH);
3865 INIT_LIST_HEAD(&death->work.entry);
3866 death->cookie = cookie;
3867 ref->death = death;
3868 if (ref->node->proc == NULL) {
3869 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
3870
3871 binder_inner_proc_lock(proc);
3872 binder_enqueue_work_ilocked(
3873 &ref->death->work, &proc->todo);
3874 binder_wakeup_proc_ilocked(proc);
3875 binder_inner_proc_unlock(proc);
3876 }
3877 } else {
3878 if (ref->death == NULL) {
3879 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
3880 proc->pid, thread->pid);
3881 binder_node_unlock(ref->node);
3882 binder_proc_unlock(proc);
3883 break;
3884 }
3885 death = ref->death;
3886 if (death->cookie != cookie) {
3887 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
3888 proc->pid, thread->pid,
3889 (u64)death->cookie,
3890 (u64)cookie);
3891 binder_node_unlock(ref->node);
3892 binder_proc_unlock(proc);
3893 break;
3894 }
3895 ref->death = NULL;
3896 binder_inner_proc_lock(proc);
3897 if (list_empty(&death->work.entry)) {
3898 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
3899 if (thread->looper &
3900 (BINDER_LOOPER_STATE_REGISTERED |
3901 BINDER_LOOPER_STATE_ENTERED))
3902 binder_enqueue_thread_work_ilocked(
3903 thread,
3904 &death->work);
3905 else {
3906 binder_enqueue_work_ilocked(
3907 &death->work,
3908 &proc->todo);
3909 binder_wakeup_proc_ilocked(
3910 proc);
3911 }
3912 } else {
3913 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
3914 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
3915 }
3916 binder_inner_proc_unlock(proc);
3917 }
3918 binder_node_unlock(ref->node);
3919 binder_proc_unlock(proc);
3920 } break;
3921 case BC_DEAD_BINDER_DONE: {
3922 struct binder_work *w;
3923 binder_uintptr_t cookie;
3924 struct binder_ref_death *death = NULL;
3925
3926 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
3927 return -EFAULT;
3928
3929 ptr += sizeof(cookie);
3930 binder_inner_proc_lock(proc);
3931 list_for_each_entry(w, &proc->delivered_death,
3932 entry) {
3933 struct binder_ref_death *tmp_death =
3934 container_of(w,
3935 struct binder_ref_death,
3936 work);
3937
3938 if (tmp_death->cookie == cookie) {
3939 death = tmp_death;
3940 break;
3941 }
3942 }
3943 binder_debug(BINDER_DEBUG_DEAD_BINDER,
3944 "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n",
3945 proc->pid, thread->pid, (u64)cookie,
3946 death);
3947 if (death == NULL) {
3948 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
3949 proc->pid, thread->pid, (u64)cookie);
3950 binder_inner_proc_unlock(proc);
3951 break;
3952 }
3953 binder_dequeue_work_ilocked(&death->work);
3954 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
3955 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
3956 if (thread->looper &
3957 (BINDER_LOOPER_STATE_REGISTERED |
3958 BINDER_LOOPER_STATE_ENTERED))
3959 binder_enqueue_thread_work_ilocked(
3960 thread, &death->work);
3961 else {
3962 binder_enqueue_work_ilocked(
3963 &death->work,
3964 &proc->todo);
3965 binder_wakeup_proc_ilocked(proc);
3966 }
3967 }
3968 binder_inner_proc_unlock(proc);
3969 } break;
3970
3971 default:
3972 pr_err("%d:%d unknown command %d\n",
3973 proc->pid, thread->pid, cmd);
3974 return -EINVAL;
3975 }
3976 *consumed = ptr - buffer;
3977 }
3978 return 0;
3979 }
3980
3981 static void binder_stat_br(struct binder_proc *proc,
3982 struct binder_thread *thread, uint32_t cmd)
3983 {
3984 trace_binder_return(cmd);
3985 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
3986 atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
3987 atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
3988 atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
3989 }
3990 }
3991
3992 static int binder_put_node_cmd(struct binder_proc *proc,
3993 struct binder_thread *thread,
3994 void __user **ptrp,
3995 binder_uintptr_t node_ptr,
3996 binder_uintptr_t node_cookie,
3997 int node_debug_id,
3998 uint32_t cmd, const char *cmd_name)
3999 {
4000 void __user *ptr = *ptrp;
4001
4002 if (put_user(cmd, (uint32_t __user *)ptr))
4003 return -EFAULT;
4004 ptr += sizeof(uint32_t);
4005
4006 if (put_user(node_ptr, (binder_uintptr_t __user *)ptr))
4007 return -EFAULT;
4008 ptr += sizeof(binder_uintptr_t);
4009
4010 if (put_user(node_cookie, (binder_uintptr_t __user *)ptr))
4011 return -EFAULT;
4012 ptr += sizeof(binder_uintptr_t);
4013
4014 binder_stat_br(proc, thread, cmd);
4015 binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n",
4016 proc->pid, thread->pid, cmd_name, node_debug_id,
4017 (u64)node_ptr, (u64)node_cookie);
4018
4019 *ptrp = ptr;
4020 return 0;
4021 }
4022
4023 static int binder_wait_for_work(struct binder_thread *thread,
4024 bool do_proc_work)
4025 {
4026 DEFINE_WAIT(wait);
4027 struct binder_proc *proc = thread->proc;
4028 int ret = 0;
4029
4030 freezer_do_not_count();
4031 binder_inner_proc_lock(proc);
4032 for (;;) {
4033 prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE);
4034 if (binder_has_work_ilocked(thread, do_proc_work))
4035 break;
4036 if (do_proc_work)
4037 list_add(&thread->waiting_thread_node,
4038 &proc->waiting_threads);
4039 binder_inner_proc_unlock(proc);
4040 schedule();
4041 binder_inner_proc_lock(proc);
4042 list_del_init(&thread->waiting_thread_node);
4043 if (signal_pending(current)) {
4044 ret = -ERESTARTSYS;
4045 break;
4046 }
4047 }
4048 finish_wait(&thread->wait, &wait);
4049 binder_inner_proc_unlock(proc);
4050 freezer_count();
4051
4052 return ret;
4053 }
4054
4055 static int binder_thread_read(struct binder_proc *proc,
4056 struct binder_thread *thread,
4057 binder_uintptr_t binder_buffer, size_t size,
4058 binder_size_t *consumed, int non_block)
4059 {
4060 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
4061 void __user *ptr = buffer + *consumed;
4062 void __user *end = buffer + size;
4063
4064 int ret = 0;
4065 int wait_for_proc_work;
4066
4067 if (*consumed == 0) {
4068 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
4069 return -EFAULT;
4070 ptr += sizeof(uint32_t);
4071 }
4072
4073 retry:
4074 binder_inner_proc_lock(proc);
4075 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
4076 binder_inner_proc_unlock(proc);
4077
4078 thread->looper |= BINDER_LOOPER_STATE_WAITING;
4079
4080 trace_binder_wait_for_work(wait_for_proc_work,
4081 !!thread->transaction_stack,
4082 !binder_worklist_empty(proc, &thread->todo));
4083 if (wait_for_proc_work) {
4084 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4085 BINDER_LOOPER_STATE_ENTERED))) {
4086 binder_user_error("%d:%d ERROR: Thread waiting for process work before calling BC_REGISTER_LOOPER or BC_ENTER_LOOPER (state %x)\n",
4087 proc->pid, thread->pid, thread->looper);
4088 wait_event_interruptible(binder_user_error_wait,
4089 binder_stop_on_user_error < 2);
4090 }
4091 binder_restore_priority(current, proc->default_priority);
4092 }
4093
4094 if (non_block) {
4095 if (!binder_has_work(thread, wait_for_proc_work))
4096 ret = -EAGAIN;
4097 } else {
4098 ret = binder_wait_for_work(thread, wait_for_proc_work);
4099 }
4100
4101 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
4102
4103 if (ret)
4104 return ret;
4105
4106 while (1) {
4107 uint32_t cmd;
4108 struct binder_transaction_data_secctx tr;
4109 struct binder_transaction_data *trd = &tr.transaction_data;
4110 struct binder_work *w = NULL;
4111 struct list_head *list = NULL;
4112 struct binder_transaction *t = NULL;
4113 struct binder_thread *t_from;
4114 size_t trsize = sizeof(*trd);
4115
4116 binder_inner_proc_lock(proc);
4117 if (!binder_worklist_empty_ilocked(&thread->todo))
4118 list = &thread->todo;
4119 else if (!binder_worklist_empty_ilocked(&proc->todo) &&
4120 wait_for_proc_work)
4121 list = &proc->todo;
4122 else {
4123 binder_inner_proc_unlock(proc);
4124
4125 /* no data added */
4126 if (ptr - buffer == 4 && !thread->looper_need_return)
4127 goto retry;
4128 break;
4129 }
4130
4131 if (end - ptr < sizeof(tr) + 4) {
4132 binder_inner_proc_unlock(proc);
4133 break;
4134 }
4135 w = binder_dequeue_work_head_ilocked(list);
4136 if (binder_worklist_empty_ilocked(&thread->todo))
4137 thread->process_todo = false;
4138
4139 switch (w->type) {
4140 case BINDER_WORK_TRANSACTION: {
4141 binder_inner_proc_unlock(proc);
4142 t = container_of(w, struct binder_transaction, work);
4143 } break;
4144 case BINDER_WORK_RETURN_ERROR: {
4145 struct binder_error *e = container_of(
4146 w, struct binder_error, work);
4147
4148 WARN_ON(e->cmd == BR_OK);
4149 binder_inner_proc_unlock(proc);
4150 if (put_user(e->cmd, (uint32_t __user *)ptr))
4151 return -EFAULT;
4152 e->cmd = BR_OK;
4153 ptr += sizeof(uint32_t);
4154
4155 binder_stat_br(proc, thread, cmd);
4156 } break;
4157 case BINDER_WORK_TRANSACTION_COMPLETE: {
4158 binder_inner_proc_unlock(proc);
4159 cmd = BR_TRANSACTION_COMPLETE;
4160 if (put_user(cmd, (uint32_t __user *)ptr))
4161 return -EFAULT;
4162 ptr += sizeof(uint32_t);
4163
4164 binder_stat_br(proc, thread, cmd);
4165 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
4166 "%d:%d BR_TRANSACTION_COMPLETE\n",
4167 proc->pid, thread->pid);
4168 kfree(w);
4169 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4170 } break;
4171 case BINDER_WORK_NODE: {
4172 struct binder_node *node = container_of(w, struct binder_node, work);
4173 int strong, weak;
4174 binder_uintptr_t node_ptr = node->ptr;
4175 binder_uintptr_t node_cookie = node->cookie;
4176 int node_debug_id = node->debug_id;
4177 int has_weak_ref;
4178 int has_strong_ref;
4179 void __user *orig_ptr = ptr;
4180
4181 BUG_ON(proc != node->proc);
4182 strong = node->internal_strong_refs ||
4183 node->local_strong_refs;
4184 weak = !hlist_empty(&node->refs) ||
4185 node->local_weak_refs ||
4186 node->tmp_refs || strong;
4187 has_strong_ref = node->has_strong_ref;
4188 has_weak_ref = node->has_weak_ref;
4189
4190 if (weak && !has_weak_ref) {
4191 node->has_weak_ref = 1;
4192 node->pending_weak_ref = 1;
4193 node->local_weak_refs++;
4194 }
4195 if (strong && !has_strong_ref) {
4196 node->has_strong_ref = 1;
4197 node->pending_strong_ref = 1;
4198 node->local_strong_refs++;
4199 }
4200 if (!strong && has_strong_ref)
4201 node->has_strong_ref = 0;
4202 if (!weak && has_weak_ref)
4203 node->has_weak_ref = 0;
4204 if (!weak && !strong) {
4205 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4206 "%d:%d node %d u%016llx c%016llx deleted\n",
4207 proc->pid, thread->pid,
4208 node_debug_id,
4209 (u64)node_ptr,
4210 (u64)node_cookie);
4211 rb_erase(&node->rb_node, &proc->nodes);
4212 binder_inner_proc_unlock(proc);
4213 binder_node_lock(node);
4214 /*
4215 * Acquire the node lock before freeing the
4216 * node to serialize with other threads that
4217 * may have been holding the node lock while
4218 * decrementing this node (avoids race where
4219 * this thread frees while the other thread
4220 * is unlocking the node after the final
4221 * decrement)
4222 */
4223 binder_node_unlock(node);
4224 binder_free_node(node);
4225 } else
4226 binder_inner_proc_unlock(proc);
4227
4228 if (weak && !has_weak_ref)
4229 ret = binder_put_node_cmd(
4230 proc, thread, &ptr, node_ptr,
4231 node_cookie, node_debug_id,
4232 BR_INCREFS, "BR_INCREFS");
4233 if (!ret && strong && !has_strong_ref)
4234 ret = binder_put_node_cmd(
4235 proc, thread, &ptr, node_ptr,
4236 node_cookie, node_debug_id,
4237 BR_ACQUIRE, "BR_ACQUIRE");
4238 if (!ret && !strong && has_strong_ref)
4239 ret = binder_put_node_cmd(
4240 proc, thread, &ptr, node_ptr,
4241 node_cookie, node_debug_id,
4242 BR_RELEASE, "BR_RELEASE");
4243 if (!ret && !weak && has_weak_ref)
4244 ret = binder_put_node_cmd(
4245 proc, thread, &ptr, node_ptr,
4246 node_cookie, node_debug_id,
4247 BR_DECREFS, "BR_DECREFS");
4248 if (orig_ptr == ptr)
4249 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4250 "%d:%d node %d u%016llx c%016llx state unchanged\n",
4251 proc->pid, thread->pid,
4252 node_debug_id,
4253 (u64)node_ptr,
4254 (u64)node_cookie);
4255 if (ret)
4256 return ret;
4257 } break;
4258 case BINDER_WORK_DEAD_BINDER:
4259 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4260 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4261 struct binder_ref_death *death;
4262 uint32_t cmd;
4263 binder_uintptr_t cookie;
4264
4265 death = container_of(w, struct binder_ref_death, work);
4266 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
4267 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
4268 else
4269 cmd = BR_DEAD_BINDER;
4270 cookie = death->cookie;
4271
4272 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
4273 "%d:%d %s %016llx\n",
4274 proc->pid, thread->pid,
4275 cmd == BR_DEAD_BINDER ?
4276 "BR_DEAD_BINDER" :
4277 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
4278 (u64)cookie);
4279 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
4280 binder_inner_proc_unlock(proc);
4281 kfree(death);
4282 binder_stats_deleted(BINDER_STAT_DEATH);
4283 } else {
4284 binder_enqueue_work_ilocked(
4285 w, &proc->delivered_death);
4286 binder_inner_proc_unlock(proc);
4287 }
4288 if (put_user(cmd, (uint32_t __user *)ptr))
4289 return -EFAULT;
4290 ptr += sizeof(uint32_t);
4291 if (put_user(cookie,
4292 (binder_uintptr_t __user *)ptr))
4293 return -EFAULT;
4294 ptr += sizeof(binder_uintptr_t);
4295 binder_stat_br(proc, thread, cmd);
4296 if (cmd == BR_DEAD_BINDER)
4297 goto done; /* DEAD_BINDER notifications can cause transactions */
4298 } break;
4299 }
4300
4301 if (!t)
4302 continue;
4303
4304 BUG_ON(t->buffer == NULL);
4305 if (t->buffer->target_node) {
4306 struct binder_node *target_node = t->buffer->target_node;
4307 struct binder_priority node_prio;
4308
4309 trd->target.ptr = target_node->ptr;
4310 trd->cookie = target_node->cookie;
4311 node_prio.sched_policy = target_node->sched_policy;
4312 node_prio.prio = target_node->min_priority;
4313 binder_transaction_priority(current, t, node_prio,
4314 target_node->inherit_rt);
4315 cmd = BR_TRANSACTION;
4316 } else {
4317 trd->target.ptr = 0;
4318 trd->cookie = 0;
4319 cmd = BR_REPLY;
4320 }
4321 trd->code = t->code;
4322 trd->flags = t->flags;
4323 trd->sender_euid = from_kuid(current_user_ns(), t->sender_euid);
4324
4325 t_from = binder_get_txn_from(t);
4326 if (t_from) {
4327 struct task_struct *sender = t_from->proc->tsk;
4328
4329 trd->sender_pid =
4330 task_tgid_nr_ns(sender,
4331 task_active_pid_ns(current));
4332 } else {
4333 trd->sender_pid = 0;
4334 }
4335
4336 trd->data_size = t->buffer->data_size;
4337 trd->offsets_size = t->buffer->offsets_size;
4338 trd->data.ptr.buffer = (binder_uintptr_t)
4339 ((uintptr_t)t->buffer->data +
4340 binder_alloc_get_user_buffer_offset(&proc->alloc));
4341 trd->data.ptr.offsets = trd->data.ptr.buffer +
4342 ALIGN(t->buffer->data_size,
4343 sizeof(void *));
4344
4345 tr.secctx = t->security_ctx;
4346 if (t->security_ctx) {
4347 cmd = BR_TRANSACTION_SEC_CTX;
4348 trsize = sizeof(tr);
4349 }
4350 if (put_user(cmd, (uint32_t __user *)ptr)) {
4351 if (t_from)
4352 binder_thread_dec_tmpref(t_from);
4353
4354 binder_cleanup_transaction(t, "put_user failed",
4355 BR_FAILED_REPLY);
4356
4357 return -EFAULT;
4358 }
4359 ptr += sizeof(uint32_t);
4360 if (copy_to_user(ptr, &tr, trsize)) {
4361 if (t_from)
4362 binder_thread_dec_tmpref(t_from);
4363
4364 binder_cleanup_transaction(t, "copy_to_user failed",
4365 BR_FAILED_REPLY);
4366
4367 return -EFAULT;
4368 }
4369 ptr += trsize;
4370
4371 trace_binder_transaction_received(t);
4372 binder_stat_br(proc, thread, cmd);
4373 binder_debug(BINDER_DEBUG_TRANSACTION,
4374 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
4375 proc->pid, thread->pid,
4376 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
4377 (cmd == BR_TRANSACTION_SEC_CTX) ?
4378 "BR_TRANSACTION_SEC_CTX" : "BR_REPLY",
4379 t->debug_id, t_from ? t_from->proc->pid : 0,
4380 t_from ? t_from->pid : 0, cmd,
4381 t->buffer->data_size, t->buffer->offsets_size,
4382 (u64)trd->data.ptr.buffer,
4383 (u64)trd->data.ptr.offsets);
4384
4385 if (t_from)
4386 binder_thread_dec_tmpref(t_from);
4387 t->buffer->allow_user_free = 1;
4388 if (cmd != BR_REPLY && !(t->flags & TF_ONE_WAY)) {
4389 binder_inner_proc_lock(thread->proc);
4390 t->to_parent = thread->transaction_stack;
4391 t->to_thread = thread;
4392 thread->transaction_stack = t;
4393 binder_inner_proc_unlock(thread->proc);
4394 } else {
4395 binder_free_transaction(t);
4396 }
4397 break;
4398 }
4399
4400 done:
4401
4402 *consumed = ptr - buffer;
4403 binder_inner_proc_lock(proc);
4404 if (proc->requested_threads == 0 &&
4405 list_empty(&thread->proc->waiting_threads) &&
4406 proc->requested_threads_started < proc->max_threads &&
4407 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4408 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
4409 /*spawn a new thread if we leave this out */) {
4410 proc->requested_threads++;
4411 binder_inner_proc_unlock(proc);
4412 binder_debug(BINDER_DEBUG_THREADS,
4413 "%d:%d BR_SPAWN_LOOPER\n",
4414 proc->pid, thread->pid);
4415 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
4416 return -EFAULT;
4417 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
4418 } else
4419 binder_inner_proc_unlock(proc);
4420 return 0;
4421 }
4422
4423 static void binder_release_work(struct binder_proc *proc,
4424 struct list_head *list)
4425 {
4426 struct binder_work *w;
4427
4428 while (1) {
4429 w = binder_dequeue_work_head(proc, list);
4430 if (!w)
4431 return;
4432
4433 switch (w->type) {
4434 case BINDER_WORK_TRANSACTION: {
4435 struct binder_transaction *t;
4436
4437 t = container_of(w, struct binder_transaction, work);
4438
4439 binder_cleanup_transaction(t, "process died.",
4440 BR_DEAD_REPLY);
4441 } break;
4442 case BINDER_WORK_RETURN_ERROR: {
4443 struct binder_error *e = container_of(
4444 w, struct binder_error, work);
4445
4446 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4447 "undelivered TRANSACTION_ERROR: %u\n",
4448 e->cmd);
4449 } break;
4450 case BINDER_WORK_TRANSACTION_COMPLETE: {
4451 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4452 "undelivered TRANSACTION_COMPLETE\n");
4453 kfree(w);
4454 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4455 } break;
4456 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4457 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4458 struct binder_ref_death *death;
4459
4460 death = container_of(w, struct binder_ref_death, work);
4461 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4462 "undelivered death notification, %016llx\n",
4463 (u64)death->cookie);
4464 kfree(death);
4465 binder_stats_deleted(BINDER_STAT_DEATH);
4466 } break;
4467 default:
4468 pr_err("unexpected work type, %d, not freed\n",
4469 w->type);
4470 break;
4471 }
4472 }
4473
4474 }
4475
4476 static struct binder_thread *binder_get_thread_ilocked(
4477 struct binder_proc *proc, struct binder_thread *new_thread)
4478 {
4479 struct binder_thread *thread = NULL;
4480 struct rb_node *parent = NULL;
4481 struct rb_node **p = &proc->threads.rb_node;
4482
4483 while (*p) {
4484 parent = *p;
4485 thread = rb_entry(parent, struct binder_thread, rb_node);
4486
4487 if (current->pid < thread->pid)
4488 p = &(*p)->rb_left;
4489 else if (current->pid > thread->pid)
4490 p = &(*p)->rb_right;
4491 else
4492 return thread;
4493 }
4494 if (!new_thread)
4495 return NULL;
4496 thread = new_thread;
4497 binder_stats_created(BINDER_STAT_THREAD);
4498 thread->proc = proc;
4499 thread->pid = current->pid;
4500 get_task_struct(current);
4501 thread->task = current;
4502 atomic_set(&thread->tmp_ref, 0);
4503 init_waitqueue_head(&thread->wait);
4504 INIT_LIST_HEAD(&thread->todo);
4505 rb_link_node(&thread->rb_node, parent, p);
4506 rb_insert_color(&thread->rb_node, &proc->threads);
4507 thread->looper_need_return = true;
4508 thread->return_error.work.type = BINDER_WORK_RETURN_ERROR;
4509 thread->return_error.cmd = BR_OK;
4510 thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR;
4511 thread->reply_error.cmd = BR_OK;
4512 INIT_LIST_HEAD(&new_thread->waiting_thread_node);
4513 return thread;
4514 }
4515
4516 static struct binder_thread *binder_get_thread(struct binder_proc *proc)
4517 {
4518 struct binder_thread *thread;
4519 struct binder_thread *new_thread;
4520
4521 binder_inner_proc_lock(proc);
4522 thread = binder_get_thread_ilocked(proc, NULL);
4523 binder_inner_proc_unlock(proc);
4524 if (!thread) {
4525 new_thread = kzalloc(sizeof(*thread), GFP_KERNEL);
4526 if (new_thread == NULL)
4527 return NULL;
4528 binder_inner_proc_lock(proc);
4529 thread = binder_get_thread_ilocked(proc, new_thread);
4530 binder_inner_proc_unlock(proc);
4531 if (thread != new_thread)
4532 kfree(new_thread);
4533 }
4534 return thread;
4535 }
4536
4537 static void binder_free_proc(struct binder_proc *proc)
4538 {
4539 BUG_ON(!list_empty(&proc->todo));
4540 BUG_ON(!list_empty(&proc->delivered_death));
4541 binder_alloc_deferred_release(&proc->alloc);
4542 put_task_struct(proc->tsk);
4543 binder_stats_deleted(BINDER_STAT_PROC);
4544 kfree(proc);
4545 }
4546
4547 static void binder_free_thread(struct binder_thread *thread)
4548 {
4549 BUG_ON(!list_empty(&thread->todo));
4550 binder_stats_deleted(BINDER_STAT_THREAD);
4551 binder_proc_dec_tmpref(thread->proc);
4552 put_task_struct(thread->task);
4553 kfree(thread);
4554 }
4555
4556 static int binder_thread_release(struct binder_proc *proc,
4557 struct binder_thread *thread)
4558 {
4559 struct binder_transaction *t;
4560 struct binder_transaction *send_reply = NULL;
4561 int active_transactions = 0;
4562 struct binder_transaction *last_t = NULL;
4563
4564 binder_inner_proc_lock(thread->proc);
4565 /*
4566 * take a ref on the proc so it survives
4567 * after we remove this thread from proc->threads.
4568 * The corresponding dec is when we actually
4569 * free the thread in binder_free_thread()
4570 */
4571 proc->tmp_ref++;
4572 /*
4573 * take a ref on this thread to ensure it
4574 * survives while we are releasing it
4575 */
4576 atomic_inc(&thread->tmp_ref);
4577 rb_erase(&thread->rb_node, &proc->threads);
4578 t = thread->transaction_stack;
4579 if (t) {
4580 spin_lock(&t->lock);
4581 if (t->to_thread == thread)
4582 send_reply = t;
4583 }
4584 thread->is_dead = true;
4585
4586 while (t) {
4587 last_t = t;
4588 active_transactions++;
4589 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4590 "release %d:%d transaction %d %s, still active\n",
4591 proc->pid, thread->pid,
4592 t->debug_id,
4593 (t->to_thread == thread) ? "in" : "out");
4594
4595 if (t->to_thread == thread) {
4596 t->to_proc = NULL;
4597 t->to_thread = NULL;
4598 if (t->buffer) {
4599 t->buffer->transaction = NULL;
4600 t->buffer = NULL;
4601 }
4602 t = t->to_parent;
4603 } else if (t->from == thread) {
4604 t->from = NULL;
4605 t = t->from_parent;
4606 } else
4607 BUG();
4608 spin_unlock(&last_t->lock);
4609 if (t)
4610 spin_lock(&t->lock);
4611 }
4612
4613 /*
4614 * If this thread used poll, make sure we remove the waitqueue
4615 * from any epoll data structures holding it with POLLFREE.
4616 * waitqueue_active() is safe to use here because we're holding
4617 * the inner lock.
4618 */
4619 if ((thread->looper & BINDER_LOOPER_STATE_POLL) &&
4620 waitqueue_active(&thread->wait)) {
4621 wake_up_poll(&thread->wait, POLLHUP | POLLFREE);
4622 }
4623
4624 binder_inner_proc_unlock(thread->proc);
4625
4626 /*
4627 * This is needed to avoid races between wake_up_poll() above and
4628 * and ep_remove_waitqueue() called for other reasons (eg the epoll file
4629 * descriptor being closed); ep_remove_waitqueue() holds an RCU read
4630 * lock, so we can be sure it's done after calling synchronize_rcu().
4631 */
4632 if (thread->looper & BINDER_LOOPER_STATE_POLL)
4633 synchronize_rcu();
4634
4635 if (send_reply)
4636 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
4637 binder_release_work(proc, &thread->todo);
4638 binder_thread_dec_tmpref(thread);
4639 return active_transactions;
4640 }
4641
4642 static unsigned int binder_poll(struct file *filp,
4643 struct poll_table_struct *wait)
4644 {
4645 struct binder_proc *proc = filp->private_data;
4646 struct binder_thread *thread = NULL;
4647 bool wait_for_proc_work;
4648
4649 thread = binder_get_thread(proc);
4650
4651 binder_inner_proc_lock(thread->proc);
4652 thread->looper |= BINDER_LOOPER_STATE_POLL;
4653 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
4654
4655 binder_inner_proc_unlock(thread->proc);
4656
4657 poll_wait(filp, &thread->wait, wait);
4658
4659 if (binder_has_work(thread, wait_for_proc_work))
4660 return POLLIN;
4661
4662 return 0;
4663 }
4664
4665 static int binder_ioctl_write_read(struct file *filp,
4666 unsigned int cmd, unsigned long arg,
4667 struct binder_thread *thread)
4668 {
4669 int ret = 0;
4670 struct binder_proc *proc = filp->private_data;
4671 unsigned int size = _IOC_SIZE(cmd);
4672 void __user *ubuf = (void __user *)arg;
4673 struct binder_write_read bwr;
4674
4675 if (size != sizeof(struct binder_write_read)) {
4676 ret = -EINVAL;
4677 goto out;
4678 }
4679 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
4680 ret = -EFAULT;
4681 goto out;
4682 }
4683 binder_debug(BINDER_DEBUG_READ_WRITE,
4684 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
4685 proc->pid, thread->pid,
4686 (u64)bwr.write_size, (u64)bwr.write_buffer,
4687 (u64)bwr.read_size, (u64)bwr.read_buffer);
4688
4689 if (bwr.write_size > 0) {
4690 ret = binder_thread_write(proc, thread,
4691 bwr.write_buffer,
4692 bwr.write_size,
4693 &bwr.write_consumed);
4694 trace_binder_write_done(ret);
4695 if (ret < 0) {
4696 bwr.read_consumed = 0;
4697 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4698 ret = -EFAULT;
4699 goto out;
4700 }
4701 }
4702 if (bwr.read_size > 0) {
4703 ret = binder_thread_read(proc, thread, bwr.read_buffer,
4704 bwr.read_size,
4705 &bwr.read_consumed,
4706 filp->f_flags & O_NONBLOCK);
4707 trace_binder_read_done(ret);
4708 binder_inner_proc_lock(proc);
4709 if (!binder_worklist_empty_ilocked(&proc->todo))
4710 binder_wakeup_proc_ilocked(proc);
4711 binder_inner_proc_unlock(proc);
4712 if (ret < 0) {
4713 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4714 ret = -EFAULT;
4715 goto out;
4716 }
4717 }
4718 binder_debug(BINDER_DEBUG_READ_WRITE,
4719 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
4720 proc->pid, thread->pid,
4721 (u64)bwr.write_consumed, (u64)bwr.write_size,
4722 (u64)bwr.read_consumed, (u64)bwr.read_size);
4723 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
4724 ret = -EFAULT;
4725 goto out;
4726 }
4727 out:
4728 return ret;
4729 }
4730
4731 static int binder_ioctl_set_ctx_mgr(struct file *filp,
4732 struct flat_binder_object *fbo)
4733 {
4734 int ret = 0;
4735 struct binder_proc *proc = filp->private_data;
4736 struct binder_context *context = proc->context;
4737 struct binder_node *new_node;
4738 kuid_t curr_euid = current_euid();
4739
4740 mutex_lock(&context->context_mgr_node_lock);
4741 if (context->binder_context_mgr_node) {
4742 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
4743 ret = -EBUSY;
4744 goto out;
4745 }
4746 ret = security_binder_set_context_mgr(proc->tsk);
4747 if (ret < 0)
4748 goto out;
4749 if (uid_valid(context->binder_context_mgr_uid)) {
4750 if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
4751 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
4752 from_kuid(&init_user_ns, curr_euid),
4753 from_kuid(&init_user_ns,
4754 context->binder_context_mgr_uid));
4755 ret = -EPERM;
4756 goto out;
4757 }
4758 } else {
4759 context->binder_context_mgr_uid = curr_euid;
4760 }
4761 new_node = binder_new_node(proc, fbo);
4762 if (!new_node) {
4763 ret = -ENOMEM;
4764 goto out;
4765 }
4766 binder_node_lock(new_node);
4767 new_node->local_weak_refs++;
4768 new_node->local_strong_refs++;
4769 new_node->has_strong_ref = 1;
4770 new_node->has_weak_ref = 1;
4771 context->binder_context_mgr_node = new_node;
4772 binder_node_unlock(new_node);
4773 binder_put_node(new_node);
4774 out:
4775 mutex_unlock(&context->context_mgr_node_lock);
4776 return ret;
4777 }
4778
4779 static int binder_ioctl_get_node_info_for_ref(struct binder_proc *proc,
4780 struct binder_node_info_for_ref *info)
4781 {
4782 struct binder_node *node;
4783 struct binder_context *context = proc->context;
4784 __u32 handle = info->handle;
4785
4786 if (info->strong_count || info->weak_count || info->reserved1 ||
4787 info->reserved2 || info->reserved3) {
4788 binder_user_error("%d BINDER_GET_NODE_INFO_FOR_REF: only handle may be non-zero.",
4789 proc->pid);
4790 return -EINVAL;
4791 }
4792
4793 /* This ioctl may only be used by the context manager */
4794 mutex_lock(&context->context_mgr_node_lock);
4795 if (!context->binder_context_mgr_node ||
4796 context->binder_context_mgr_node->proc != proc) {
4797 mutex_unlock(&context->context_mgr_node_lock);
4798 return -EPERM;
4799 }
4800 mutex_unlock(&context->context_mgr_node_lock);
4801
4802 node = binder_get_node_from_ref(proc, handle, true, NULL);
4803 if (!node)
4804 return -EINVAL;
4805
4806 info->strong_count = node->local_strong_refs +
4807 node->internal_strong_refs;
4808 info->weak_count = node->local_weak_refs;
4809
4810 binder_put_node(node);
4811
4812 return 0;
4813 }
4814
4815 static int binder_ioctl_get_node_debug_info(struct binder_proc *proc,
4816 struct binder_node_debug_info *info) {
4817 struct rb_node *n;
4818 binder_uintptr_t ptr = info->ptr;
4819
4820 memset(info, 0, sizeof(*info));
4821
4822 binder_inner_proc_lock(proc);
4823 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
4824 struct binder_node *node = rb_entry(n, struct binder_node,
4825 rb_node);
4826 if (node->ptr > ptr) {
4827 info->ptr = node->ptr;
4828 info->cookie = node->cookie;
4829 info->has_strong_ref = node->has_strong_ref;
4830 info->has_weak_ref = node->has_weak_ref;
4831 break;
4832 }
4833 }
4834 binder_inner_proc_unlock(proc);
4835
4836 return 0;
4837 }
4838
4839 static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4840 {
4841 int ret;
4842 struct binder_proc *proc = filp->private_data;
4843 struct binder_thread *thread;
4844 unsigned int size = _IOC_SIZE(cmd);
4845 void __user *ubuf = (void __user *)arg;
4846
4847 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
4848 proc->pid, current->pid, cmd, arg);*/
4849
4850 binder_selftest_alloc(&proc->alloc);
4851
4852 trace_binder_ioctl(cmd, arg);
4853
4854 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4855 if (ret)
4856 goto err_unlocked;
4857
4858 thread = binder_get_thread(proc);
4859 if (thread == NULL) {
4860 ret = -ENOMEM;
4861 goto err;
4862 }
4863
4864 switch (cmd) {
4865 case BINDER_WRITE_READ:
4866 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
4867 if (ret)
4868 goto err;
4869 break;
4870 case BINDER_SET_MAX_THREADS: {
4871 int max_threads;
4872
4873 if (copy_from_user(&max_threads, ubuf,
4874 sizeof(max_threads))) {
4875 ret = -EINVAL;
4876 goto err;
4877 }
4878 binder_inner_proc_lock(proc);
4879 proc->max_threads = max_threads;
4880 binder_inner_proc_unlock(proc);
4881 break;
4882 }
4883 case BINDER_SET_CONTEXT_MGR_EXT: {
4884 struct flat_binder_object fbo;
4885
4886 if (copy_from_user(&fbo, ubuf, sizeof(fbo))) {
4887 ret = -EINVAL;
4888 goto err;
4889 }
4890 ret = binder_ioctl_set_ctx_mgr(filp, &fbo);
4891 if (ret)
4892 goto err;
4893 break;
4894 }
4895 case BINDER_SET_CONTEXT_MGR:
4896 ret = binder_ioctl_set_ctx_mgr(filp, NULL);
4897 if (ret)
4898 goto err;
4899 break;
4900 case BINDER_THREAD_EXIT:
4901 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
4902 proc->pid, thread->pid);
4903 binder_thread_release(proc, thread);
4904 thread = NULL;
4905 break;
4906 case BINDER_VERSION: {
4907 struct binder_version __user *ver = ubuf;
4908
4909 if (size != sizeof(struct binder_version)) {
4910 ret = -EINVAL;
4911 goto err;
4912 }
4913 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
4914 &ver->protocol_version)) {
4915 ret = -EINVAL;
4916 goto err;
4917 }
4918 break;
4919 }
4920 case BINDER_GET_NODE_INFO_FOR_REF: {
4921 struct binder_node_info_for_ref info;
4922
4923 if (copy_from_user(&info, ubuf, sizeof(info))) {
4924 ret = -EFAULT;
4925 goto err;
4926 }
4927
4928 ret = binder_ioctl_get_node_info_for_ref(proc, &info);
4929 if (ret < 0)
4930 goto err;
4931
4932 if (copy_to_user(ubuf, &info, sizeof(info))) {
4933 ret = -EFAULT;
4934 goto err;
4935 }
4936
4937 break;
4938 }
4939 case BINDER_GET_NODE_DEBUG_INFO: {
4940 struct binder_node_debug_info info;
4941
4942 if (copy_from_user(&info, ubuf, sizeof(info))) {
4943 ret = -EFAULT;
4944 goto err;
4945 }
4946
4947 ret = binder_ioctl_get_node_debug_info(proc, &info);
4948 if (ret < 0)
4949 goto err;
4950
4951 if (copy_to_user(ubuf, &info, sizeof(info))) {
4952 ret = -EFAULT;
4953 goto err;
4954 }
4955 break;
4956 }
4957 default:
4958 ret = -EINVAL;
4959 goto err;
4960 }
4961 ret = 0;
4962 err:
4963 if (thread)
4964 thread->looper_need_return = false;
4965 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4966 if (ret && ret != -ERESTARTSYS)
4967 pr_info("%d:%d ioctl %x %lx returned %d\n", proc->pid, current->pid, cmd, arg, ret);
4968 err_unlocked:
4969 trace_binder_ioctl_done(ret);
4970 return ret;
4971 }
4972
4973 static void binder_vma_open(struct vm_area_struct *vma)
4974 {
4975 struct binder_proc *proc = vma->vm_private_data;
4976
4977 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4978 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
4979 proc->pid, vma->vm_start, vma->vm_end,
4980 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4981 (unsigned long)pgprot_val(vma->vm_page_prot));
4982 }
4983
4984 static void binder_vma_close(struct vm_area_struct *vma)
4985 {
4986 struct binder_proc *proc = vma->vm_private_data;
4987
4988 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4989 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
4990 proc->pid, vma->vm_start, vma->vm_end,
4991 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4992 (unsigned long)pgprot_val(vma->vm_page_prot));
4993 binder_alloc_vma_close(&proc->alloc);
4994 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
4995 }
4996
4997 static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
4998 {
4999 return VM_FAULT_SIGBUS;
5000 }
5001
5002 static struct vm_operations_struct binder_vm_ops = {
5003 .open = binder_vma_open,
5004 .close = binder_vma_close,
5005 .fault = binder_vm_fault,
5006 };
5007
5008 static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
5009 {
5010 int ret;
5011 struct binder_proc *proc = filp->private_data;
5012 const char *failure_string;
5013
5014 if (proc->tsk != current->group_leader)
5015 return -EINVAL;
5016
5017 if ((vma->vm_end - vma->vm_start) > SZ_4M)
5018 vma->vm_end = vma->vm_start + SZ_4M;
5019
5020 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5021 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
5022 __func__, proc->pid, vma->vm_start, vma->vm_end,
5023 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
5024 (unsigned long)pgprot_val(vma->vm_page_prot));
5025
5026 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
5027 ret = -EPERM;
5028 failure_string = "bad vm_flags";
5029 goto err_bad_arg;
5030 }
5031 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
5032 vma->vm_ops = &binder_vm_ops;
5033 vma->vm_private_data = proc;
5034
5035 ret = binder_alloc_mmap_handler(&proc->alloc, vma);
5036 if (ret)
5037 return ret;
5038 mutex_lock(&proc->files_lock);
5039 proc->files = get_files_struct(current);
5040 mutex_unlock(&proc->files_lock);
5041 return 0;
5042
5043 err_bad_arg:
5044 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
5045 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
5046 return ret;
5047 }
5048
5049 static int binder_open(struct inode *nodp, struct file *filp)
5050 {
5051 struct binder_proc *proc;
5052 struct binder_device *binder_dev;
5053
5054 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
5055 current->group_leader->pid, current->pid);
5056
5057 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
5058 if (proc == NULL)
5059 return -ENOMEM;
5060 spin_lock_init(&proc->inner_lock);
5061 spin_lock_init(&proc->outer_lock);
5062 get_task_struct(current->group_leader);
5063 proc->tsk = current->group_leader;
5064 mutex_init(&proc->files_lock);
5065 INIT_LIST_HEAD(&proc->todo);
5066 if (binder_supported_policy(current->policy)) {
5067 proc->default_priority.sched_policy = current->policy;
5068 proc->default_priority.prio = current->normal_prio;
5069 } else {
5070 proc->default_priority.sched_policy = SCHED_NORMAL;
5071 proc->default_priority.prio = NICE_TO_PRIO(0);
5072 }
5073
5074 binder_dev = container_of(filp->private_data, struct binder_device,
5075 miscdev);
5076 proc->context = &binder_dev->context;
5077 binder_alloc_init(&proc->alloc);
5078
5079 binder_stats_created(BINDER_STAT_PROC);
5080 proc->pid = current->group_leader->pid;
5081 INIT_LIST_HEAD(&proc->delivered_death);
5082 INIT_LIST_HEAD(&proc->waiting_threads);
5083 filp->private_data = proc;
5084
5085 mutex_lock(&binder_procs_lock);
5086 hlist_add_head(&proc->proc_node, &binder_procs);
5087 mutex_unlock(&binder_procs_lock);
5088
5089 if (binder_debugfs_dir_entry_proc) {
5090 char strbuf[11];
5091
5092 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
5093 /*
5094 * proc debug entries are shared between contexts, so
5095 * this will fail if the process tries to open the driver
5096 * again with a different context. The priting code will
5097 * anyway print all contexts that a given PID has, so this
5098 * is not a problem.
5099 */
5100 proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
5101 binder_debugfs_dir_entry_proc,
5102 (void *)(unsigned long)proc->pid,
5103 &binder_proc_fops);
5104 }
5105
5106 return 0;
5107 }
5108
5109 static int binder_flush(struct file *filp, fl_owner_t id)
5110 {
5111 struct binder_proc *proc = filp->private_data;
5112
5113 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
5114
5115 return 0;
5116 }
5117
5118 static void binder_deferred_flush(struct binder_proc *proc)
5119 {
5120 struct rb_node *n;
5121 int wake_count = 0;
5122
5123 binder_inner_proc_lock(proc);
5124 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
5125 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
5126
5127 thread->looper_need_return = true;
5128 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
5129 wake_up_interruptible(&thread->wait);
5130 wake_count++;
5131 }
5132 }
5133 binder_inner_proc_unlock(proc);
5134
5135 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5136 "binder_flush: %d woke %d threads\n", proc->pid,
5137 wake_count);
5138 }
5139
5140 static int binder_release(struct inode *nodp, struct file *filp)
5141 {
5142 struct binder_proc *proc = filp->private_data;
5143
5144 debugfs_remove(proc->debugfs_entry);
5145 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
5146
5147 return 0;
5148 }
5149
5150 static int binder_node_release(struct binder_node *node, int refs)
5151 {
5152 struct binder_ref *ref;
5153 int death = 0;
5154 struct binder_proc *proc = node->proc;
5155
5156 binder_release_work(proc, &node->async_todo);
5157
5158 binder_node_lock(node);
5159 binder_inner_proc_lock(proc);
5160 binder_dequeue_work_ilocked(&node->work);
5161 /*
5162 * The caller must have taken a temporary ref on the node,
5163 */
5164 BUG_ON(!node->tmp_refs);
5165 if (hlist_empty(&node->refs) && node->tmp_refs == 1) {
5166 binder_inner_proc_unlock(proc);
5167 binder_node_unlock(node);
5168 binder_free_node(node);
5169
5170 return refs;
5171 }
5172
5173 node->proc = NULL;
5174 node->local_strong_refs = 0;
5175 node->local_weak_refs = 0;
5176 binder_inner_proc_unlock(proc);
5177
5178 spin_lock(&binder_dead_nodes_lock);
5179 hlist_add_head(&node->dead_node, &binder_dead_nodes);
5180 spin_unlock(&binder_dead_nodes_lock);
5181
5182 hlist_for_each_entry(ref, &node->refs, node_entry) {
5183 refs++;
5184 /*
5185 * Need the node lock to synchronize
5186 * with new notification requests and the
5187 * inner lock to synchronize with queued
5188 * death notifications.
5189 */
5190 binder_inner_proc_lock(ref->proc);
5191 if (!ref->death) {
5192 binder_inner_proc_unlock(ref->proc);
5193 continue;
5194 }
5195
5196 death++;
5197
5198 BUG_ON(!list_empty(&ref->death->work.entry));
5199 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
5200 binder_enqueue_work_ilocked(&ref->death->work,
5201 &ref->proc->todo);
5202 binder_wakeup_proc_ilocked(ref->proc);
5203 binder_inner_proc_unlock(ref->proc);
5204 }
5205
5206 binder_debug(BINDER_DEBUG_DEAD_BINDER,
5207 "node %d now dead, refs %d, death %d\n",
5208 node->debug_id, refs, death);
5209 binder_node_unlock(node);
5210 binder_put_node(node);
5211
5212 return refs;
5213 }
5214
5215 static void binder_deferred_release(struct binder_proc *proc)
5216 {
5217 struct binder_context *context = proc->context;
5218 struct rb_node *n;
5219 int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
5220
5221 BUG_ON(proc->files);
5222
5223 mutex_lock(&binder_procs_lock);
5224 hlist_del(&proc->proc_node);
5225 mutex_unlock(&binder_procs_lock);
5226
5227 mutex_lock(&context->context_mgr_node_lock);
5228 if (context->binder_context_mgr_node &&
5229 context->binder_context_mgr_node->proc == proc) {
5230 binder_debug(BINDER_DEBUG_DEAD_BINDER,
5231 "%s: %d context_mgr_node gone\n",
5232 __func__, proc->pid);
5233 context->binder_context_mgr_node = NULL;
5234 }
5235 mutex_unlock(&context->context_mgr_node_lock);
5236 binder_inner_proc_lock(proc);
5237 /*
5238 * Make sure proc stays alive after we
5239 * remove all the threads
5240 */
5241 proc->tmp_ref++;
5242
5243 proc->is_dead = true;
5244 threads = 0;
5245 active_transactions = 0;
5246 while ((n = rb_first(&proc->threads))) {
5247 struct binder_thread *thread;
5248
5249 thread = rb_entry(n, struct binder_thread, rb_node);
5250 binder_inner_proc_unlock(proc);
5251 threads++;
5252 active_transactions += binder_thread_release(proc, thread);
5253 binder_inner_proc_lock(proc);
5254 }
5255
5256 nodes = 0;
5257 incoming_refs = 0;
5258 while ((n = rb_first(&proc->nodes))) {
5259 struct binder_node *node;
5260
5261 node = rb_entry(n, struct binder_node, rb_node);
5262 nodes++;
5263 /*
5264 * take a temporary ref on the node before
5265 * calling binder_node_release() which will either
5266 * kfree() the node or call binder_put_node()
5267 */
5268 binder_inc_node_tmpref_ilocked(node);
5269 rb_erase(&node->rb_node, &proc->nodes);
5270 binder_inner_proc_unlock(proc);
5271 incoming_refs = binder_node_release(node, incoming_refs);
5272 binder_inner_proc_lock(proc);
5273 }
5274 binder_inner_proc_unlock(proc);
5275
5276 outgoing_refs = 0;
5277 binder_proc_lock(proc);
5278 while ((n = rb_first(&proc->refs_by_desc))) {
5279 struct binder_ref *ref;
5280
5281 ref = rb_entry(n, struct binder_ref, rb_node_desc);
5282 outgoing_refs++;
5283 binder_cleanup_ref_olocked(ref);
5284 binder_proc_unlock(proc);
5285 binder_free_ref(ref);
5286 binder_proc_lock(proc);
5287 }
5288 binder_proc_unlock(proc);
5289
5290 binder_release_work(proc, &proc->todo);
5291 binder_release_work(proc, &proc->delivered_death);
5292
5293 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5294 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
5295 __func__, proc->pid, threads, nodes, incoming_refs,
5296 outgoing_refs, active_transactions);
5297
5298 binder_proc_dec_tmpref(proc);
5299 }
5300
5301 static void binder_deferred_func(struct work_struct *work)
5302 {
5303 struct binder_proc *proc;
5304 struct files_struct *files;
5305
5306 int defer;
5307
5308 do {
5309 mutex_lock(&binder_deferred_lock);
5310 if (!hlist_empty(&binder_deferred_list)) {
5311 proc = hlist_entry(binder_deferred_list.first,
5312 struct binder_proc, deferred_work_node);
5313 hlist_del_init(&proc->deferred_work_node);
5314 defer = proc->deferred_work;
5315 proc->deferred_work = 0;
5316 } else {
5317 proc = NULL;
5318 defer = 0;
5319 }
5320 mutex_unlock(&binder_deferred_lock);
5321
5322 files = NULL;
5323 if (defer & BINDER_DEFERRED_PUT_FILES) {
5324 mutex_lock(&proc->files_lock);
5325 files = proc->files;
5326 if (files)
5327 proc->files = NULL;
5328 mutex_unlock(&proc->files_lock);
5329 }
5330
5331 if (defer & BINDER_DEFERRED_FLUSH)
5332 binder_deferred_flush(proc);
5333
5334 if (defer & BINDER_DEFERRED_RELEASE)
5335 binder_deferred_release(proc); /* frees proc */
5336
5337 if (files)
5338 put_files_struct(files);
5339 } while (proc);
5340 }
5341 static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
5342
5343 static void
5344 binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
5345 {
5346 mutex_lock(&binder_deferred_lock);
5347 proc->deferred_work |= defer;
5348 if (hlist_unhashed(&proc->deferred_work_node)) {
5349 hlist_add_head(&proc->deferred_work_node,
5350 &binder_deferred_list);
5351 queue_work(binder_deferred_workqueue, &binder_deferred_work);
5352 }
5353 mutex_unlock(&binder_deferred_lock);
5354 }
5355
5356 static void print_binder_transaction_ilocked(struct seq_file *m,
5357 struct binder_proc *proc,
5358 const char *prefix,
5359 struct binder_transaction *t)
5360 {
5361 struct binder_proc *to_proc;
5362 struct binder_buffer *buffer = t->buffer;
5363
5364 spin_lock(&t->lock);
5365 to_proc = t->to_proc;
5366 seq_printf(m,
5367 "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %d:%d r%d",
5368 prefix, t->debug_id, t,
5369 t->from ? t->from->proc->pid : 0,
5370 t->from ? t->from->pid : 0,
5371 to_proc ? to_proc->pid : 0,
5372 t->to_thread ? t->to_thread->pid : 0,
5373 t->code, t->flags, t->priority.sched_policy,
5374 t->priority.prio, t->need_reply);
5375 spin_unlock(&t->lock);
5376
5377 if (proc != to_proc) {
5378 /*
5379 * Can only safely deref buffer if we are holding the
5380 * correct proc inner lock for this node
5381 */
5382 seq_puts(m, "\n");
5383 return;
5384 }
5385
5386 if (buffer == NULL) {
5387 seq_puts(m, " buffer free\n");
5388 return;
5389 }
5390 if (buffer->target_node)
5391 seq_printf(m, " node %d", buffer->target_node->debug_id);
5392 seq_printf(m, " size %zd:%zd data %p\n",
5393 buffer->data_size, buffer->offsets_size,
5394 buffer->data);
5395 }
5396
5397 static void print_binder_work_ilocked(struct seq_file *m,
5398 struct binder_proc *proc,
5399 const char *prefix,
5400 const char *transaction_prefix,
5401 struct binder_work *w)
5402 {
5403 struct binder_node *node;
5404 struct binder_transaction *t;
5405
5406 switch (w->type) {
5407 case BINDER_WORK_TRANSACTION:
5408 t = container_of(w, struct binder_transaction, work);
5409 print_binder_transaction_ilocked(
5410 m, proc, transaction_prefix, t);
5411 break;
5412 case BINDER_WORK_RETURN_ERROR: {
5413 struct binder_error *e = container_of(
5414 w, struct binder_error, work);
5415
5416 seq_printf(m, "%stransaction error: %u\n",
5417 prefix, e->cmd);
5418 } break;
5419 case BINDER_WORK_TRANSACTION_COMPLETE:
5420 seq_printf(m, "%stransaction complete\n", prefix);
5421 break;
5422 case BINDER_WORK_NODE:
5423 node = container_of(w, struct binder_node, work);
5424 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
5425 prefix, node->debug_id,
5426 (u64)node->ptr, (u64)node->cookie);
5427 break;
5428 case BINDER_WORK_DEAD_BINDER:
5429 seq_printf(m, "%shas dead binder\n", prefix);
5430 break;
5431 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
5432 seq_printf(m, "%shas cleared dead binder\n", prefix);
5433 break;
5434 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
5435 seq_printf(m, "%shas cleared death notification\n", prefix);
5436 break;
5437 default:
5438 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
5439 break;
5440 }
5441 }
5442
5443 static void print_binder_thread_ilocked(struct seq_file *m,
5444 struct binder_thread *thread,
5445 int print_always)
5446 {
5447 struct binder_transaction *t;
5448 struct binder_work *w;
5449 size_t start_pos = m->count;
5450 size_t header_pos;
5451
5452 seq_printf(m, " thread %d: l %02x need_return %d tr %d\n",
5453 thread->pid, thread->looper,
5454 thread->looper_need_return,
5455 atomic_read(&thread->tmp_ref));
5456 header_pos = m->count;
5457 t = thread->transaction_stack;
5458 while (t) {
5459 if (t->from == thread) {
5460 print_binder_transaction_ilocked(m, thread->proc,
5461 " outgoing transaction", t);
5462 t = t->from_parent;
5463 } else if (t->to_thread == thread) {
5464 print_binder_transaction_ilocked(m, thread->proc,
5465 " incoming transaction", t);
5466 t = t->to_parent;
5467 } else {
5468 print_binder_transaction_ilocked(m, thread->proc,
5469 " bad transaction", t);
5470 t = NULL;
5471 }
5472 }
5473 list_for_each_entry(w, &thread->todo, entry) {
5474 print_binder_work_ilocked(m, thread->proc, " ",
5475 " pending transaction", w);
5476 }
5477 if (!print_always && m->count == header_pos)
5478 m->count = start_pos;
5479 }
5480
5481 static void print_binder_node_nilocked(struct seq_file *m,
5482 struct binder_node *node)
5483 {
5484 struct binder_ref *ref;
5485 struct binder_work *w;
5486 int count;
5487
5488 count = 0;
5489 hlist_for_each_entry(ref, &node->refs, node_entry)
5490 count++;
5491
5492 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",
5493 node->debug_id, (u64)node->ptr, (u64)node->cookie,
5494 node->sched_policy, node->min_priority,
5495 node->has_strong_ref, node->has_weak_ref,
5496 node->local_strong_refs, node->local_weak_refs,
5497 node->internal_strong_refs, count, node->tmp_refs);
5498 if (count) {
5499 seq_puts(m, " proc");
5500 hlist_for_each_entry(ref, &node->refs, node_entry)
5501 seq_printf(m, " %d", ref->proc->pid);
5502 }
5503 seq_puts(m, "\n");
5504 if (node->proc) {
5505 list_for_each_entry(w, &node->async_todo, entry)
5506 print_binder_work_ilocked(m, node->proc, " ",
5507 " pending async transaction", w);
5508 }
5509 }
5510
5511 static void print_binder_ref_olocked(struct seq_file *m,
5512 struct binder_ref *ref)
5513 {
5514 binder_node_lock(ref->node);
5515 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
5516 ref->data.debug_id, ref->data.desc,
5517 ref->node->proc ? "" : "dead ",
5518 ref->node->debug_id, ref->data.strong,
5519 ref->data.weak, ref->death);
5520 binder_node_unlock(ref->node);
5521 }
5522
5523 static void print_binder_proc(struct seq_file *m,
5524 struct binder_proc *proc, int print_all)
5525 {
5526 struct binder_work *w;
5527 struct rb_node *n;
5528 size_t start_pos = m->count;
5529 size_t header_pos;
5530 struct binder_node *last_node = NULL;
5531
5532 seq_printf(m, "proc %d\n", proc->pid);
5533 seq_printf(m, "context %s\n", proc->context->name);
5534 header_pos = m->count;
5535
5536 binder_inner_proc_lock(proc);
5537 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
5538 print_binder_thread_ilocked(m, rb_entry(n, struct binder_thread,
5539 rb_node), print_all);
5540
5541 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
5542 struct binder_node *node = rb_entry(n, struct binder_node,
5543 rb_node);
5544 /*
5545 * take a temporary reference on the node so it
5546 * survives and isn't removed from the tree
5547 * while we print it.
5548 */
5549 binder_inc_node_tmpref_ilocked(node);
5550 /* Need to drop inner lock to take node lock */
5551 binder_inner_proc_unlock(proc);
5552 if (last_node)
5553 binder_put_node(last_node);
5554 binder_node_inner_lock(node);
5555 print_binder_node_nilocked(m, node);
5556 binder_node_inner_unlock(node);
5557 last_node = node;
5558 binder_inner_proc_lock(proc);
5559 }
5560 binder_inner_proc_unlock(proc);
5561 if (last_node)
5562 binder_put_node(last_node);
5563
5564 if (print_all) {
5565 binder_proc_lock(proc);
5566 for (n = rb_first(&proc->refs_by_desc);
5567 n != NULL;
5568 n = rb_next(n))
5569 print_binder_ref_olocked(m, rb_entry(n,
5570 struct binder_ref,
5571 rb_node_desc));
5572 binder_proc_unlock(proc);
5573 }
5574 binder_alloc_print_allocated(m, &proc->alloc);
5575 binder_inner_proc_lock(proc);
5576 list_for_each_entry(w, &proc->todo, entry)
5577 print_binder_work_ilocked(m, proc, " ",
5578 " pending transaction", w);
5579 list_for_each_entry(w, &proc->delivered_death, entry) {
5580 seq_puts(m, " has delivered dead binder\n");
5581 break;
5582 }
5583 binder_inner_proc_unlock(proc);
5584 if (!print_all && m->count == header_pos)
5585 m->count = start_pos;
5586 }
5587
5588 static const char * const binder_return_strings[] = {
5589 "BR_ERROR",
5590 "BR_OK",
5591 "BR_TRANSACTION",
5592 "BR_REPLY",
5593 "BR_ACQUIRE_RESULT",
5594 "BR_DEAD_REPLY",
5595 "BR_TRANSACTION_COMPLETE",
5596 "BR_INCREFS",
5597 "BR_ACQUIRE",
5598 "BR_RELEASE",
5599 "BR_DECREFS",
5600 "BR_ATTEMPT_ACQUIRE",
5601 "BR_NOOP",
5602 "BR_SPAWN_LOOPER",
5603 "BR_FINISHED",
5604 "BR_DEAD_BINDER",
5605 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
5606 "BR_FAILED_REPLY"
5607 };
5608
5609 static const char * const binder_command_strings[] = {
5610 "BC_TRANSACTION",
5611 "BC_REPLY",
5612 "BC_ACQUIRE_RESULT",
5613 "BC_FREE_BUFFER",
5614 "BC_INCREFS",
5615 "BC_ACQUIRE",
5616 "BC_RELEASE",
5617 "BC_DECREFS",
5618 "BC_INCREFS_DONE",
5619 "BC_ACQUIRE_DONE",
5620 "BC_ATTEMPT_ACQUIRE",
5621 "BC_REGISTER_LOOPER",
5622 "BC_ENTER_LOOPER",
5623 "BC_EXIT_LOOPER",
5624 "BC_REQUEST_DEATH_NOTIFICATION",
5625 "BC_CLEAR_DEATH_NOTIFICATION",
5626 "BC_DEAD_BINDER_DONE",
5627 "BC_TRANSACTION_SG",
5628 "BC_REPLY_SG",
5629 };
5630
5631 static const char * const binder_objstat_strings[] = {
5632 "proc",
5633 "thread",
5634 "node",
5635 "ref",
5636 "death",
5637 "transaction",
5638 "transaction_complete"
5639 };
5640
5641 static void print_binder_stats(struct seq_file *m, const char *prefix,
5642 struct binder_stats *stats)
5643 {
5644 int i;
5645
5646 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
5647 ARRAY_SIZE(binder_command_strings));
5648 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
5649 int temp = atomic_read(&stats->bc[i]);
5650
5651 if (temp)
5652 seq_printf(m, "%s%s: %d\n", prefix,
5653 binder_command_strings[i], temp);
5654 }
5655
5656 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
5657 ARRAY_SIZE(binder_return_strings));
5658 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
5659 int temp = atomic_read(&stats->br[i]);
5660
5661 if (temp)
5662 seq_printf(m, "%s%s: %d\n", prefix,
5663 binder_return_strings[i], temp);
5664 }
5665
5666 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
5667 ARRAY_SIZE(binder_objstat_strings));
5668 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
5669 ARRAY_SIZE(stats->obj_deleted));
5670 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
5671 int created = atomic_read(&stats->obj_created[i]);
5672 int deleted = atomic_read(&stats->obj_deleted[i]);
5673
5674 if (created || deleted)
5675 seq_printf(m, "%s%s: active %d total %d\n",
5676 prefix,
5677 binder_objstat_strings[i],
5678 created - deleted,
5679 created);
5680 }
5681 }
5682
5683 static void print_binder_proc_stats(struct seq_file *m,
5684 struct binder_proc *proc)
5685 {
5686 struct binder_work *w;
5687 struct binder_thread *thread;
5688 struct rb_node *n;
5689 int count, strong, weak, ready_threads;
5690 size_t free_async_space =
5691 binder_alloc_get_free_async_space(&proc->alloc);
5692
5693 seq_printf(m, "proc %d\n", proc->pid);
5694 seq_printf(m, "context %s\n", proc->context->name);
5695 count = 0;
5696 ready_threads = 0;
5697 binder_inner_proc_lock(proc);
5698 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
5699 count++;
5700
5701 list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node)
5702 ready_threads++;
5703
5704 seq_printf(m, " threads: %d\n", count);
5705 seq_printf(m, " requested threads: %d+%d/%d\n"
5706 " ready threads %d\n"
5707 " free async space %zd\n", proc->requested_threads,
5708 proc->requested_threads_started, proc->max_threads,
5709 ready_threads,
5710 free_async_space);
5711 count = 0;
5712 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
5713 count++;
5714 binder_inner_proc_unlock(proc);
5715 seq_printf(m, " nodes: %d\n", count);
5716 count = 0;
5717 strong = 0;
5718 weak = 0;
5719 binder_proc_lock(proc);
5720 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
5721 struct binder_ref *ref = rb_entry(n, struct binder_ref,
5722 rb_node_desc);
5723 count++;
5724 strong += ref->data.strong;
5725 weak += ref->data.weak;
5726 }
5727 binder_proc_unlock(proc);
5728 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
5729
5730 count = binder_alloc_get_allocated_count(&proc->alloc);
5731 seq_printf(m, " buffers: %d\n", count);
5732
5733 count = 0;
5734 binder_inner_proc_lock(proc);
5735 list_for_each_entry(w, &proc->todo, entry) {
5736 if (w->type == BINDER_WORK_TRANSACTION)
5737 count++;
5738 }
5739 binder_inner_proc_unlock(proc);
5740 seq_printf(m, " pending transactions: %d\n", count);
5741
5742 print_binder_stats(m, " ", &proc->stats);
5743 }
5744
5745
5746 static int binder_state_show(struct seq_file *m, void *unused)
5747 {
5748 struct binder_proc *proc;
5749 struct binder_node *node;
5750 struct binder_node *last_node = NULL;
5751
5752 seq_puts(m, "binder state:\n");
5753
5754 spin_lock(&binder_dead_nodes_lock);
5755 if (!hlist_empty(&binder_dead_nodes))
5756 seq_puts(m, "dead nodes:\n");
5757 hlist_for_each_entry(node, &binder_dead_nodes, dead_node) {
5758 /*
5759 * take a temporary reference on the node so it
5760 * survives and isn't removed from the list
5761 * while we print it.
5762 */
5763 node->tmp_refs++;
5764 spin_unlock(&binder_dead_nodes_lock);
5765 if (last_node)
5766 binder_put_node(last_node);
5767 binder_node_lock(node);
5768 print_binder_node_nilocked(m, node);
5769 binder_node_unlock(node);
5770 last_node = node;
5771 spin_lock(&binder_dead_nodes_lock);
5772 }
5773 spin_unlock(&binder_dead_nodes_lock);
5774 if (last_node)
5775 binder_put_node(last_node);
5776
5777 mutex_lock(&binder_procs_lock);
5778 hlist_for_each_entry(proc, &binder_procs, proc_node)
5779 print_binder_proc(m, proc, 1);
5780 mutex_unlock(&binder_procs_lock);
5781
5782 return 0;
5783 }
5784
5785 static int binder_stats_show(struct seq_file *m, void *unused)
5786 {
5787 struct binder_proc *proc;
5788
5789 seq_puts(m, "binder stats:\n");
5790
5791 print_binder_stats(m, "", &binder_stats);
5792
5793 mutex_lock(&binder_procs_lock);
5794 hlist_for_each_entry(proc, &binder_procs, proc_node)
5795 print_binder_proc_stats(m, proc);
5796 mutex_unlock(&binder_procs_lock);
5797
5798 return 0;
5799 }
5800
5801 static int binder_transactions_show(struct seq_file *m, void *unused)
5802 {
5803 struct binder_proc *proc;
5804
5805 seq_puts(m, "binder transactions:\n");
5806 mutex_lock(&binder_procs_lock);
5807 hlist_for_each_entry(proc, &binder_procs, proc_node)
5808 print_binder_proc(m, proc, 0);
5809 mutex_unlock(&binder_procs_lock);
5810
5811 return 0;
5812 }
5813
5814 static int binder_proc_show(struct seq_file *m, void *unused)
5815 {
5816 struct binder_proc *itr;
5817 int pid = (unsigned long)m->private;
5818
5819 mutex_lock(&binder_procs_lock);
5820 hlist_for_each_entry(itr, &binder_procs, proc_node) {
5821 if (itr->pid == pid) {
5822 seq_puts(m, "binder proc state:\n");
5823 print_binder_proc(m, itr, 1);
5824 }
5825 }
5826 mutex_unlock(&binder_procs_lock);
5827
5828 return 0;
5829 }
5830
5831 static void print_binder_transaction_log_entry(struct seq_file *m,
5832 struct binder_transaction_log_entry *e)
5833 {
5834 int debug_id = READ_ONCE(e->debug_id_done);
5835 /*
5836 * read barrier to guarantee debug_id_done read before
5837 * we print the log values
5838 */
5839 smp_rmb();
5840 seq_printf(m,
5841 "%d: %s from %d:%d to %d:%d context %s node %d handle %d size %d:%d ret %d/%d l=%d",
5842 e->debug_id, (e->call_type == 2) ? "reply" :
5843 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
5844 e->from_thread, e->to_proc, e->to_thread, e->context_name,
5845 e->to_node, e->target_handle, e->data_size, e->offsets_size,
5846 e->return_error, e->return_error_param,
5847 e->return_error_line);
5848 /*
5849 * read-barrier to guarantee read of debug_id_done after
5850 * done printing the fields of the entry
5851 */
5852 smp_rmb();
5853 seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ?
5854 "\n" : " (incomplete)\n");
5855 }
5856
5857 static int binder_transaction_log_show(struct seq_file *m, void *unused)
5858 {
5859 struct binder_transaction_log *log = m->private;
5860 unsigned int log_cur = atomic_read(&log->cur);
5861 unsigned int count;
5862 unsigned int cur;
5863 int i;
5864
5865 count = log_cur + 1;
5866 cur = count < ARRAY_SIZE(log->entry) && !log->full ?
5867 0 : count % ARRAY_SIZE(log->entry);
5868 if (count > ARRAY_SIZE(log->entry) || log->full)
5869 count = ARRAY_SIZE(log->entry);
5870 for (i = 0; i < count; i++) {
5871 unsigned int index = cur++ % ARRAY_SIZE(log->entry);
5872
5873 print_binder_transaction_log_entry(m, &log->entry[index]);
5874 }
5875 return 0;
5876 }
5877
5878 static const struct file_operations binder_fops = {
5879 .owner = THIS_MODULE,
5880 .poll = binder_poll,
5881 .unlocked_ioctl = binder_ioctl,
5882 .compat_ioctl = binder_ioctl,
5883 .mmap = binder_mmap,
5884 .open = binder_open,
5885 .flush = binder_flush,
5886 .release = binder_release,
5887 };
5888
5889 BINDER_DEBUG_ENTRY(state);
5890 BINDER_DEBUG_ENTRY(stats);
5891 BINDER_DEBUG_ENTRY(transactions);
5892 BINDER_DEBUG_ENTRY(transaction_log);
5893
5894 static int __init init_binder_device(const char *name)
5895 {
5896 int ret;
5897 struct binder_device *binder_device;
5898
5899 binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
5900 if (!binder_device)
5901 return -ENOMEM;
5902
5903 binder_device->miscdev.fops = &binder_fops;
5904 binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
5905 binder_device->miscdev.name = name;
5906
5907 binder_device->context.binder_context_mgr_uid = INVALID_UID;
5908 binder_device->context.name = name;
5909 mutex_init(&binder_device->context.context_mgr_node_lock);
5910
5911 ret = misc_register(&binder_device->miscdev);
5912 if (ret < 0) {
5913 kfree(binder_device);
5914 return ret;
5915 }
5916
5917 hlist_add_head(&binder_device->hlist, &binder_devices);
5918
5919 return ret;
5920 }
5921
5922 static int __init binder_init(void)
5923 {
5924 int ret;
5925 char *device_name, *device_names;
5926 struct binder_device *device;
5927 struct hlist_node *tmp;
5928
5929 atomic_set(&binder_transaction_log.cur, ~0U);
5930 atomic_set(&binder_transaction_log_failed.cur, ~0U);
5931 binder_deferred_workqueue = create_singlethread_workqueue("binder");
5932 if (!binder_deferred_workqueue)
5933 return -ENOMEM;
5934
5935 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
5936 if (binder_debugfs_dir_entry_root)
5937 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
5938 binder_debugfs_dir_entry_root);
5939
5940 if (binder_debugfs_dir_entry_root) {
5941 debugfs_create_file("state",
5942 S_IRUGO,
5943 binder_debugfs_dir_entry_root,
5944 NULL,
5945 &binder_state_fops);
5946 debugfs_create_file("stats",
5947 S_IRUGO,
5948 binder_debugfs_dir_entry_root,
5949 NULL,
5950 &binder_stats_fops);
5951 debugfs_create_file("transactions",
5952 S_IRUGO,
5953 binder_debugfs_dir_entry_root,
5954 NULL,
5955 &binder_transactions_fops);
5956 debugfs_create_file("transaction_log",
5957 S_IRUGO,
5958 binder_debugfs_dir_entry_root,
5959 &binder_transaction_log,
5960 &binder_transaction_log_fops);
5961 debugfs_create_file("failed_transaction_log",
5962 S_IRUGO,
5963 binder_debugfs_dir_entry_root,
5964 &binder_transaction_log_failed,
5965 &binder_transaction_log_fops);
5966 }
5967
5968 /*
5969 * Copy the module_parameter string, because we don't want to
5970 * tokenize it in-place.
5971 */
5972 device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL);
5973 if (!device_names) {
5974 ret = -ENOMEM;
5975 goto err_alloc_device_names_failed;
5976 }
5977 strcpy(device_names, binder_devices_param);
5978
5979 while ((device_name = strsep(&device_names, ","))) {
5980 ret = init_binder_device(device_name);
5981 if (ret)
5982 goto err_init_binder_device_failed;
5983 }
5984
5985 return ret;
5986
5987 err_init_binder_device_failed:
5988 hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
5989 misc_deregister(&device->miscdev);
5990 hlist_del(&device->hlist);
5991 kfree(device);
5992 }
5993 err_alloc_device_names_failed:
5994 debugfs_remove_recursive(binder_debugfs_dir_entry_root);
5995
5996 destroy_workqueue(binder_deferred_workqueue);
5997
5998 return ret;
5999 }
6000
6001 device_initcall(binder_init);
6002
6003 #define CREATE_TRACE_POINTS
6004 #include "binder_trace.h"
6005
6006 MODULE_LICENSE("GPL v2");