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