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