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