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