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