input: touchscreen: k29_i2c_goodix: fix compilation warning
[firefly-linux-kernel-4.4.55.git] / drivers / staging / android / binder.c
1 /* binder.c
2  *
3  * Android IPC Subsystem
4  *
5  * Copyright (C) 2007-2008 Google, Inc.
6  *
7  * This software is licensed under the terms of the GNU General Public
8  * License version 2, as published by the Free Software Foundation, and
9  * may be copied, distributed, and modified under those terms.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  */
17
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
20 #include <asm/cacheflush.h>
21 #include <linux/fdtable.h>
22 #include <linux/file.h>
23 #include <linux/freezer.h>
24 #include <linux/fs.h>
25 #include <linux/list.h>
26 #include <linux/miscdevice.h>
27 #include <linux/mm.h>
28 #include <linux/module.h>
29 #include <linux/mutex.h>
30 #include <linux/nsproxy.h>
31 #include <linux/poll.h>
32 #include <linux/debugfs.h>
33 #include <linux/rbtree.h>
34 #include <linux/sched.h>
35 #include <linux/seq_file.h>
36 #include <linux/uaccess.h>
37 #include <linux/vmalloc.h>
38 #include <linux/slab.h>
39 #include <linux/pid_namespace.h>
40 #include <linux/security.h>
41
42 #include "binder.h"
43 #include "binder_trace.h"
44
45 static DEFINE_MUTEX(binder_main_lock);
46 static DEFINE_MUTEX(binder_deferred_lock);
47 static DEFINE_MUTEX(binder_mmap_lock);
48
49 static HLIST_HEAD(binder_procs);
50 static HLIST_HEAD(binder_deferred_list);
51 static HLIST_HEAD(binder_dead_nodes);
52
53 static struct dentry *binder_debugfs_dir_entry_root;
54 static struct dentry *binder_debugfs_dir_entry_proc;
55 static struct binder_node *binder_context_mgr_node;
56 static kuid_t binder_context_mgr_uid = INVALID_UID;
57 static int binder_last_id;
58 static struct workqueue_struct *binder_deferred_workqueue;
59
60 #define BINDER_DEBUG_ENTRY(name) \
61 static int binder_##name##_open(struct inode *inode, struct file *file) \
62 { \
63         return single_open(file, binder_##name##_show, inode->i_private); \
64 } \
65 \
66 static const struct file_operations binder_##name##_fops = { \
67         .owner = THIS_MODULE, \
68         .open = binder_##name##_open, \
69         .read = seq_read, \
70         .llseek = seq_lseek, \
71         .release = single_release, \
72 }
73
74 static int binder_proc_show(struct seq_file *m, void *unused);
75 BINDER_DEBUG_ENTRY(proc);
76
77 /* This is only defined in include/asm-arm/sizes.h */
78 #ifndef SZ_1K
79 #define SZ_1K                               0x400
80 #endif
81
82 #ifndef SZ_4M
83 #define SZ_4M                               0x400000
84 #endif
85
86 #define FORBIDDEN_MMAP_FLAGS                (VM_WRITE)
87
88 #define BINDER_SMALL_BUF_SIZE (PAGE_SIZE * 64)
89
90 enum {
91         BINDER_DEBUG_USER_ERROR             = 1U << 0,
92         BINDER_DEBUG_FAILED_TRANSACTION     = 1U << 1,
93         BINDER_DEBUG_DEAD_TRANSACTION       = 1U << 2,
94         BINDER_DEBUG_OPEN_CLOSE             = 1U << 3,
95         BINDER_DEBUG_DEAD_BINDER            = 1U << 4,
96         BINDER_DEBUG_DEATH_NOTIFICATION     = 1U << 5,
97         BINDER_DEBUG_READ_WRITE             = 1U << 6,
98         BINDER_DEBUG_USER_REFS              = 1U << 7,
99         BINDER_DEBUG_THREADS                = 1U << 8,
100         BINDER_DEBUG_TRANSACTION            = 1U << 9,
101         BINDER_DEBUG_TRANSACTION_COMPLETE   = 1U << 10,
102         BINDER_DEBUG_FREE_BUFFER            = 1U << 11,
103         BINDER_DEBUG_INTERNAL_REFS          = 1U << 12,
104         BINDER_DEBUG_BUFFER_ALLOC           = 1U << 13,
105         BINDER_DEBUG_PRIORITY_CAP           = 1U << 14,
106         BINDER_DEBUG_BUFFER_ALLOC_ASYNC     = 1U << 15,
107 };
108 static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR |
109         BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION;
110 module_param_named(debug_mask, binder_debug_mask, uint, S_IWUSR | S_IRUGO);
111
112 static bool binder_debug_no_lock;
113 module_param_named(proc_no_lock, binder_debug_no_lock, bool, S_IWUSR | S_IRUGO);
114
115 static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
116 static int binder_stop_on_user_error;
117
118 static int binder_set_stop_on_user_error(const char *val,
119                                          struct kernel_param *kp)
120 {
121         int ret;
122         ret = param_set_int(val, kp);
123         if (binder_stop_on_user_error < 2)
124                 wake_up(&binder_user_error_wait);
125         return ret;
126 }
127 module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
128         param_get_int, &binder_stop_on_user_error, S_IWUSR | S_IRUGO);
129
130 #define binder_debug(mask, x...) \
131         do { \
132                 if (binder_debug_mask & mask) \
133                         pr_info(x); \
134         } while (0)
135
136 #define binder_user_error(x...) \
137         do { \
138                 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
139                         pr_info(x); \
140                 if (binder_stop_on_user_error) \
141                         binder_stop_on_user_error = 2; \
142         } while (0)
143
144 enum binder_stat_types {
145         BINDER_STAT_PROC,
146         BINDER_STAT_THREAD,
147         BINDER_STAT_NODE,
148         BINDER_STAT_REF,
149         BINDER_STAT_DEATH,
150         BINDER_STAT_TRANSACTION,
151         BINDER_STAT_TRANSACTION_COMPLETE,
152         BINDER_STAT_COUNT
153 };
154
155 struct binder_stats {
156         int br[_IOC_NR(BR_FAILED_REPLY) + 1];
157         int bc[_IOC_NR(BC_DEAD_BINDER_DONE) + 1];
158         int obj_created[BINDER_STAT_COUNT];
159         int obj_deleted[BINDER_STAT_COUNT];
160 };
161
162 static struct binder_stats binder_stats;
163
164 static inline void binder_stats_deleted(enum binder_stat_types type)
165 {
166         binder_stats.obj_deleted[type]++;
167 }
168
169 static inline void binder_stats_created(enum binder_stat_types type)
170 {
171         binder_stats.obj_created[type]++;
172 }
173
174 struct binder_transaction_log_entry {
175         int debug_id;
176         int call_type;
177         int from_proc;
178         int from_thread;
179         int target_handle;
180         int to_proc;
181         int to_thread;
182         int to_node;
183         int data_size;
184         int offsets_size;
185 };
186 struct binder_transaction_log {
187         int next;
188         int full;
189         struct binder_transaction_log_entry entry[32];
190 };
191 static struct binder_transaction_log binder_transaction_log;
192 static struct binder_transaction_log binder_transaction_log_failed;
193
194 static struct binder_transaction_log_entry *binder_transaction_log_add(
195         struct binder_transaction_log *log)
196 {
197         struct binder_transaction_log_entry *e;
198         e = &log->entry[log->next];
199         memset(e, 0, sizeof(*e));
200         log->next++;
201         if (log->next == ARRAY_SIZE(log->entry)) {
202                 log->next = 0;
203                 log->full = 1;
204         }
205         return e;
206 }
207
208 struct binder_work {
209         struct list_head entry;
210         enum {
211                 BINDER_WORK_TRANSACTION = 1,
212                 BINDER_WORK_TRANSACTION_COMPLETE,
213                 BINDER_WORK_NODE,
214                 BINDER_WORK_DEAD_BINDER,
215                 BINDER_WORK_DEAD_BINDER_AND_CLEAR,
216                 BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
217         } type;
218 };
219
220 struct binder_node {
221         int debug_id;
222         struct binder_work work;
223         union {
224                 struct rb_node rb_node;
225                 struct hlist_node dead_node;
226         };
227         struct binder_proc *proc;
228         struct hlist_head refs;
229         int internal_strong_refs;
230         int local_weak_refs;
231         int local_strong_refs;
232         binder_uintptr_t ptr;
233         binder_uintptr_t cookie;
234         unsigned has_strong_ref:1;
235         unsigned pending_strong_ref:1;
236         unsigned has_weak_ref:1;
237         unsigned pending_weak_ref:1;
238         unsigned has_async_transaction:1;
239         unsigned accept_fds:1;
240         unsigned min_priority:8;
241         struct list_head async_todo;
242 };
243
244 struct binder_ref_death {
245         struct binder_work work;
246         binder_uintptr_t cookie;
247 };
248
249 struct binder_ref {
250         /* Lookups needed: */
251         /*   node + proc => ref (transaction) */
252         /*   desc + proc => ref (transaction, inc/dec ref) */
253         /*   node => refs + procs (proc exit) */
254         int debug_id;
255         struct rb_node rb_node_desc;
256         struct rb_node rb_node_node;
257         struct hlist_node node_entry;
258         struct binder_proc *proc;
259         struct binder_node *node;
260         uint32_t desc;
261         int strong;
262         int weak;
263         struct binder_ref_death *death;
264 };
265
266 struct binder_buffer {
267         struct list_head entry; /* free and allocated entries by address */
268         struct rb_node rb_node; /* free entry by size or allocated entry */
269                                 /* by address */
270         unsigned free:1;
271         unsigned allow_user_free:1;
272         unsigned async_transaction:1;
273         unsigned debug_id:29;
274
275         struct binder_transaction *transaction;
276
277         struct binder_node *target_node;
278         size_t data_size;
279         size_t offsets_size;
280         uint8_t data[0];
281 };
282
283 enum binder_deferred_state {
284         BINDER_DEFERRED_PUT_FILES    = 0x01,
285         BINDER_DEFERRED_FLUSH        = 0x02,
286         BINDER_DEFERRED_RELEASE      = 0x04,
287 };
288
289 struct binder_proc {
290         struct hlist_node proc_node;
291         struct rb_root threads;
292         struct rb_root nodes;
293         struct rb_root refs_by_desc;
294         struct rb_root refs_by_node;
295         int pid;
296         struct vm_area_struct *vma;
297         struct mm_struct *vma_vm_mm;
298         struct task_struct *tsk;
299         struct files_struct *files;
300         struct hlist_node deferred_work_node;
301         int deferred_work;
302         void *buffer;
303         ptrdiff_t user_buffer_offset;
304
305         struct list_head buffers;
306         struct rb_root free_buffers;
307         struct rb_root allocated_buffers;
308         size_t free_async_space;
309
310         struct page **pages;
311         size_t buffer_size;
312         uint32_t buffer_free;
313         struct list_head todo;
314         wait_queue_head_t wait;
315         struct binder_stats stats;
316         struct list_head delivered_death;
317         int max_threads;
318         int requested_threads;
319         int requested_threads_started;
320         int ready_threads;
321         long default_priority;
322         struct dentry *debugfs_entry;
323 };
324
325 enum {
326         BINDER_LOOPER_STATE_REGISTERED  = 0x01,
327         BINDER_LOOPER_STATE_ENTERED     = 0x02,
328         BINDER_LOOPER_STATE_EXITED      = 0x04,
329         BINDER_LOOPER_STATE_INVALID     = 0x08,
330         BINDER_LOOPER_STATE_WAITING     = 0x10,
331         BINDER_LOOPER_STATE_NEED_RETURN = 0x20
332 };
333
334 struct binder_thread {
335         struct binder_proc *proc;
336         struct rb_node rb_node;
337         int pid;
338         int looper;
339         struct binder_transaction *transaction_stack;
340         struct list_head todo;
341         uint32_t return_error; /* Write failed, return error code in read buf */
342         uint32_t return_error2; /* Write failed, return error code in read */
343                 /* buffer. Used when sending a reply to a dead process that */
344                 /* we are also waiting on */
345         wait_queue_head_t wait;
346         struct binder_stats stats;
347 };
348
349 struct binder_transaction {
350         int debug_id;
351         struct binder_work work;
352         struct binder_thread *from;
353         struct binder_transaction *from_parent;
354         struct binder_proc *to_proc;
355         struct binder_thread *to_thread;
356         struct binder_transaction *to_parent;
357         unsigned need_reply:1;
358         /* unsigned is_dead:1; */       /* not used at the moment */
359
360         struct binder_buffer *buffer;
361         unsigned int    code;
362         unsigned int    flags;
363         long    priority;
364         long    saved_priority;
365         kuid_t  sender_euid;
366 };
367
368 static void
369 binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
370
371 static int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
372 {
373         struct files_struct *files = proc->files;
374         unsigned long rlim_cur;
375         unsigned long irqs;
376
377         if (files == NULL)
378                 return -ESRCH;
379
380         if (!lock_task_sighand(proc->tsk, &irqs))
381                 return -EMFILE;
382
383         rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
384         unlock_task_sighand(proc->tsk, &irqs);
385
386         return __alloc_fd(files, 0, rlim_cur, flags);
387 }
388
389 /*
390  * copied from fd_install
391  */
392 static void task_fd_install(
393         struct binder_proc *proc, unsigned int fd, struct file *file)
394 {
395         if (proc->files)
396                 __fd_install(proc->files, fd, file);
397 }
398
399 /*
400  * copied from sys_close
401  */
402 static long task_close_fd(struct binder_proc *proc, unsigned int fd)
403 {
404         int retval;
405
406         if (proc->files == NULL)
407                 return -ESRCH;
408
409         retval = __close_fd(proc->files, fd);
410         /* can't restart close syscall because file table entry was cleared */
411         if (unlikely(retval == -ERESTARTSYS ||
412                      retval == -ERESTARTNOINTR ||
413                      retval == -ERESTARTNOHAND ||
414                      retval == -ERESTART_RESTARTBLOCK))
415                 retval = -EINTR;
416
417         return retval;
418 }
419
420 static inline void binder_lock(const char *tag)
421 {
422         trace_binder_lock(tag);
423         mutex_lock(&binder_main_lock);
424         trace_binder_locked(tag);
425 }
426
427 static inline void binder_unlock(const char *tag)
428 {
429         trace_binder_unlock(tag);
430         mutex_unlock(&binder_main_lock);
431 }
432
433 static void binder_set_nice(long nice)
434 {
435         long min_nice;
436         if (can_nice(current, nice)) {
437                 set_user_nice(current, nice);
438                 return;
439         }
440         min_nice = 20 - current->signal->rlim[RLIMIT_NICE].rlim_cur;
441         binder_debug(BINDER_DEBUG_PRIORITY_CAP,
442                      "%d: nice value %ld not allowed use %ld instead\n",
443                       current->pid, nice, min_nice);
444         set_user_nice(current, min_nice);
445         if (min_nice < 20)
446                 return;
447         binder_user_error("%d RLIMIT_NICE not set\n", current->pid);
448 }
449
450 static size_t binder_buffer_size(struct binder_proc *proc,
451                                  struct binder_buffer *buffer)
452 {
453         if (list_is_last(&buffer->entry, &proc->buffers))
454                 return proc->buffer + proc->buffer_size - (void *)buffer->data;
455         else
456                 return (size_t)list_entry(buffer->entry.next,
457                         struct binder_buffer, entry) - (size_t)buffer->data;
458 }
459
460 static void binder_insert_free_buffer(struct binder_proc *proc,
461                                       struct binder_buffer *new_buffer)
462 {
463         struct rb_node **p = &proc->free_buffers.rb_node;
464         struct rb_node *parent = NULL;
465         struct binder_buffer *buffer;
466         size_t buffer_size;
467         size_t new_buffer_size;
468
469         BUG_ON(!new_buffer->free);
470
471         new_buffer_size = binder_buffer_size(proc, new_buffer);
472
473         binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
474                      "%d: add free buffer, size %zd, at %p\n",
475                       proc->pid, new_buffer_size, new_buffer);
476
477         while (*p) {
478                 parent = *p;
479                 buffer = rb_entry(parent, struct binder_buffer, rb_node);
480                 BUG_ON(!buffer->free);
481
482                 buffer_size = binder_buffer_size(proc, buffer);
483
484                 if (new_buffer_size < buffer_size)
485                         p = &parent->rb_left;
486                 else
487                         p = &parent->rb_right;
488         }
489         rb_link_node(&new_buffer->rb_node, parent, p);
490         rb_insert_color(&new_buffer->rb_node, &proc->free_buffers);
491 }
492
493 static void binder_insert_allocated_buffer(struct binder_proc *proc,
494                                            struct binder_buffer *new_buffer)
495 {
496         struct rb_node **p = &proc->allocated_buffers.rb_node;
497         struct rb_node *parent = NULL;
498         struct binder_buffer *buffer;
499
500         BUG_ON(new_buffer->free);
501
502         while (*p) {
503                 parent = *p;
504                 buffer = rb_entry(parent, struct binder_buffer, rb_node);
505                 BUG_ON(buffer->free);
506
507                 if (new_buffer < buffer)
508                         p = &parent->rb_left;
509                 else if (new_buffer > buffer)
510                         p = &parent->rb_right;
511                 else
512                         BUG();
513         }
514         rb_link_node(&new_buffer->rb_node, parent, p);
515         rb_insert_color(&new_buffer->rb_node, &proc->allocated_buffers);
516 }
517
518 static struct binder_buffer *binder_buffer_lookup(struct binder_proc *proc,
519                                                   uintptr_t user_ptr)
520 {
521         struct rb_node *n = proc->allocated_buffers.rb_node;
522         struct binder_buffer *buffer;
523         struct binder_buffer *kern_ptr;
524
525         kern_ptr = (struct binder_buffer *)(user_ptr - proc->user_buffer_offset
526                 - offsetof(struct binder_buffer, data));
527
528         while (n) {
529                 buffer = rb_entry(n, struct binder_buffer, rb_node);
530                 BUG_ON(buffer->free);
531
532                 if (kern_ptr < buffer)
533                         n = n->rb_left;
534                 else if (kern_ptr > buffer)
535                         n = n->rb_right;
536                 else
537                         return buffer;
538         }
539         return NULL;
540 }
541
542 static int binder_update_page_range(struct binder_proc *proc, int allocate,
543                                     void *start, void *end,
544                                     struct vm_area_struct *vma)
545 {
546         void *page_addr;
547         unsigned long user_page_addr;
548         struct vm_struct tmp_area;
549         struct page **page;
550         struct mm_struct *mm;
551
552         binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
553                      "%d: %s pages %p-%p\n", proc->pid,
554                      allocate ? "allocate" : "free", start, end);
555
556         if (end <= start)
557                 return 0;
558
559         trace_binder_update_page_range(proc, allocate, start, end);
560
561         if (vma)
562                 mm = NULL;
563         else
564                 mm = get_task_mm(proc->tsk);
565
566         if (mm) {
567                 down_write(&mm->mmap_sem);
568                 vma = proc->vma;
569                 if (vma && mm != proc->vma_vm_mm) {
570                         pr_err("%d: vma mm and task mm mismatch\n",
571                                 proc->pid);
572                         vma = NULL;
573                 }
574         }
575
576         if (allocate == 0)
577                 goto free_range;
578
579         if (vma == NULL) {
580                 pr_err("%d: binder_alloc_buf failed to map pages in userspace, no vma\n",
581                         proc->pid);
582                 goto err_no_vma;
583         }
584
585         for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
586                 int ret;
587                 struct page **page_array_ptr;
588                 page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
589
590                 BUG_ON(*page);
591                 *page = alloc_page(GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
592                 if (*page == NULL) {
593                         pr_err("%d: binder_alloc_buf failed for page at %p\n",
594                                 proc->pid, page_addr);
595                         goto err_alloc_page_failed;
596                 }
597                 tmp_area.addr = page_addr;
598                 tmp_area.size = PAGE_SIZE + PAGE_SIZE /* guard page? */;
599                 page_array_ptr = page;
600                 ret = map_vm_area(&tmp_area, PAGE_KERNEL, &page_array_ptr);
601                 if (ret) {
602                         pr_err("%d: binder_alloc_buf failed to map page at %p in kernel\n",
603                                proc->pid, page_addr);
604                         goto err_map_kernel_failed;
605                 }
606                 user_page_addr =
607                         (uintptr_t)page_addr + proc->user_buffer_offset;
608                 ret = vm_insert_page(vma, user_page_addr, page[0]);
609                 if (ret) {
610                         pr_err("%d: binder_alloc_buf failed to map page at %lx in userspace\n",
611                                proc->pid, user_page_addr);
612                         goto err_vm_insert_page_failed;
613                 }
614                 /* vm_insert_page does not seem to increment the refcount */
615         }
616         if (mm) {
617                 up_write(&mm->mmap_sem);
618                 mmput(mm);
619         }
620         return 0;
621
622 free_range:
623         for (page_addr = end - PAGE_SIZE; page_addr >= start;
624              page_addr -= PAGE_SIZE) {
625                 page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
626                 if (vma)
627                         zap_page_range(vma, (uintptr_t)page_addr +
628                                 proc->user_buffer_offset, PAGE_SIZE, NULL);
629 err_vm_insert_page_failed:
630                 unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
631 err_map_kernel_failed:
632                 __free_page(*page);
633                 *page = NULL;
634 err_alloc_page_failed:
635                 ;
636         }
637 err_no_vma:
638         if (mm) {
639                 up_write(&mm->mmap_sem);
640                 mmput(mm);
641         }
642         return -ENOMEM;
643 }
644
645 static struct binder_buffer *binder_alloc_buf(struct binder_proc *proc,
646                                               size_t data_size,
647                                               size_t offsets_size, int is_async)
648 {
649         struct rb_node *n = proc->free_buffers.rb_node;
650         struct binder_buffer *buffer;
651         size_t buffer_size;
652         struct rb_node *best_fit = NULL;
653         void *has_page_addr;
654         void *end_page_addr;
655         size_t size;
656
657         if (proc->vma == NULL) {
658                 pr_err("%d: binder_alloc_buf, no vma\n",
659                        proc->pid);
660                 return NULL;
661         }
662
663         size = ALIGN(data_size, sizeof(void *)) +
664                 ALIGN(offsets_size, sizeof(void *));
665
666         if (size < data_size || size < offsets_size) {
667                 binder_user_error("%d: got transaction with invalid size %zd-%zd\n",
668                                 proc->pid, data_size, offsets_size);
669                 return NULL;
670         }
671
672         if (is_async &&
673             proc->free_async_space < size + sizeof(struct binder_buffer)) {
674                 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
675                              "%d: binder_alloc_buf size %zd failed, no async space left\n",
676                               proc->pid, size);
677                 return NULL;
678         }
679
680         while (n) {
681                 buffer = rb_entry(n, struct binder_buffer, rb_node);
682                 BUG_ON(!buffer->free);
683                 buffer_size = binder_buffer_size(proc, buffer);
684
685                 if (size < buffer_size) {
686                         best_fit = n;
687                         n = n->rb_left;
688                 } else if (size > buffer_size)
689                         n = n->rb_right;
690                 else {
691                         best_fit = n;
692                         break;
693                 }
694         }
695         if (best_fit == NULL) {
696                 pr_err("%d: binder_alloc_buf size %zd failed, no address space\n",
697                         proc->pid, size);
698                 return NULL;
699         }
700         if (n == NULL) {
701                 buffer = rb_entry(best_fit, struct binder_buffer, rb_node);
702                 buffer_size = binder_buffer_size(proc, buffer);
703         }
704
705         binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
706                      "%d: binder_alloc_buf size %zd got buffer %p size %zd\n",
707                       proc->pid, size, buffer, buffer_size);
708
709         has_page_addr =
710                 (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK);
711         if (n == NULL) {
712                 if (size + sizeof(struct binder_buffer) + 4 >= buffer_size)
713                         buffer_size = size; /* no room for other buffers */
714                 else
715                         buffer_size = size + sizeof(struct binder_buffer);
716         }
717         end_page_addr =
718                 (void *)PAGE_ALIGN((uintptr_t)buffer->data + buffer_size);
719         if (end_page_addr > has_page_addr)
720                 end_page_addr = has_page_addr;
721         if (binder_update_page_range(proc, 1,
722             (void *)PAGE_ALIGN((uintptr_t)buffer->data), end_page_addr, NULL))
723                 return NULL;
724
725         rb_erase(best_fit, &proc->free_buffers);
726         buffer->free = 0;
727         binder_insert_allocated_buffer(proc, buffer);
728         if (buffer_size != size) {
729                 struct binder_buffer *new_buffer = (void *)buffer->data + size;
730                 list_add(&new_buffer->entry, &buffer->entry);
731                 new_buffer->free = 1;
732                 binder_insert_free_buffer(proc, new_buffer);
733         }
734         binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
735                      "%d: binder_alloc_buf size %zd got %p\n",
736                       proc->pid, size, buffer);
737         buffer->data_size = data_size;
738         buffer->offsets_size = offsets_size;
739         buffer->async_transaction = is_async;
740         if (is_async) {
741                 proc->free_async_space -= size + sizeof(struct binder_buffer);
742                 binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
743                              "%d: binder_alloc_buf size %zd async free %zd\n",
744                               proc->pid, size, proc->free_async_space);
745         }
746
747         return buffer;
748 }
749
750 static void *buffer_start_page(struct binder_buffer *buffer)
751 {
752         return (void *)((uintptr_t)buffer & PAGE_MASK);
753 }
754
755 static void *buffer_end_page(struct binder_buffer *buffer)
756 {
757         return (void *)(((uintptr_t)(buffer + 1) - 1) & PAGE_MASK);
758 }
759
760 static void binder_delete_free_buffer(struct binder_proc *proc,
761                                       struct binder_buffer *buffer)
762 {
763         struct binder_buffer *prev, *next = NULL;
764         int free_page_end = 1;
765         int free_page_start = 1;
766
767         BUG_ON(proc->buffers.next == &buffer->entry);
768         prev = list_entry(buffer->entry.prev, struct binder_buffer, entry);
769         BUG_ON(!prev->free);
770         if (buffer_end_page(prev) == buffer_start_page(buffer)) {
771                 free_page_start = 0;
772                 if (buffer_end_page(prev) == buffer_end_page(buffer))
773                         free_page_end = 0;
774                 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
775                              "%d: merge free, buffer %p share page with %p\n",
776                               proc->pid, buffer, prev);
777         }
778
779         if (!list_is_last(&buffer->entry, &proc->buffers)) {
780                 next = list_entry(buffer->entry.next,
781                                   struct binder_buffer, entry);
782                 if (buffer_start_page(next) == buffer_end_page(buffer)) {
783                         free_page_end = 0;
784                         if (buffer_start_page(next) ==
785                             buffer_start_page(buffer))
786                                 free_page_start = 0;
787                         binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
788                                      "%d: merge free, buffer %p share page with %p\n",
789                                       proc->pid, buffer, prev);
790                 }
791         }
792         list_del(&buffer->entry);
793         if (free_page_start || free_page_end) {
794                 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
795                              "%d: merge free, buffer %p do not share page%s%s with %p or %p\n",
796                              proc->pid, buffer, free_page_start ? "" : " end",
797                              free_page_end ? "" : " start", prev, next);
798                 binder_update_page_range(proc, 0, free_page_start ?
799                         buffer_start_page(buffer) : buffer_end_page(buffer),
800                         (free_page_end ? buffer_end_page(buffer) :
801                         buffer_start_page(buffer)) + PAGE_SIZE, NULL);
802         }
803 }
804
805 static void binder_free_buf(struct binder_proc *proc,
806                             struct binder_buffer *buffer)
807 {
808         size_t size, buffer_size;
809
810         buffer_size = binder_buffer_size(proc, buffer);
811
812         size = ALIGN(buffer->data_size, sizeof(void *)) +
813                 ALIGN(buffer->offsets_size, sizeof(void *));
814
815         binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
816                      "%d: binder_free_buf %p size %zd buffer_size %zd\n",
817                       proc->pid, buffer, size, buffer_size);
818
819         BUG_ON(buffer->free);
820         BUG_ON(size > buffer_size);
821         BUG_ON(buffer->transaction != NULL);
822         BUG_ON((void *)buffer < proc->buffer);
823         BUG_ON((void *)buffer > proc->buffer + proc->buffer_size);
824
825         if (buffer->async_transaction) {
826                 proc->free_async_space += size + sizeof(struct binder_buffer);
827
828                 binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
829                              "%d: binder_free_buf size %zd async free %zd\n",
830                               proc->pid, size, proc->free_async_space);
831         }
832
833         binder_update_page_range(proc, 0,
834                 (void *)PAGE_ALIGN((uintptr_t)buffer->data),
835                 (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK),
836                 NULL);
837         rb_erase(&buffer->rb_node, &proc->allocated_buffers);
838         buffer->free = 1;
839         if (!list_is_last(&buffer->entry, &proc->buffers)) {
840                 struct binder_buffer *next = list_entry(buffer->entry.next,
841                                                 struct binder_buffer, entry);
842                 if (next->free) {
843                         rb_erase(&next->rb_node, &proc->free_buffers);
844                         binder_delete_free_buffer(proc, next);
845                 }
846         }
847         if (proc->buffers.next != &buffer->entry) {
848                 struct binder_buffer *prev = list_entry(buffer->entry.prev,
849                                                 struct binder_buffer, entry);
850                 if (prev->free) {
851                         binder_delete_free_buffer(proc, buffer);
852                         rb_erase(&prev->rb_node, &proc->free_buffers);
853                         buffer = prev;
854                 }
855         }
856         binder_insert_free_buffer(proc, buffer);
857 }
858
859 static struct binder_node *binder_get_node(struct binder_proc *proc,
860                                            binder_uintptr_t ptr)
861 {
862         struct rb_node *n = proc->nodes.rb_node;
863         struct binder_node *node;
864
865         while (n) {
866                 node = rb_entry(n, struct binder_node, rb_node);
867
868                 if (ptr < node->ptr)
869                         n = n->rb_left;
870                 else if (ptr > node->ptr)
871                         n = n->rb_right;
872                 else
873                         return node;
874         }
875         return NULL;
876 }
877
878 static struct binder_node *binder_new_node(struct binder_proc *proc,
879                                            binder_uintptr_t ptr,
880                                            binder_uintptr_t cookie)
881 {
882         struct rb_node **p = &proc->nodes.rb_node;
883         struct rb_node *parent = NULL;
884         struct binder_node *node;
885
886         while (*p) {
887                 parent = *p;
888                 node = rb_entry(parent, struct binder_node, rb_node);
889
890                 if (ptr < node->ptr)
891                         p = &(*p)->rb_left;
892                 else if (ptr > node->ptr)
893                         p = &(*p)->rb_right;
894                 else
895                         return NULL;
896         }
897
898         node = kzalloc(sizeof(*node), GFP_KERNEL);
899         if (node == NULL)
900                 return NULL;
901         binder_stats_created(BINDER_STAT_NODE);
902         rb_link_node(&node->rb_node, parent, p);
903         rb_insert_color(&node->rb_node, &proc->nodes);
904         node->debug_id = ++binder_last_id;
905         node->proc = proc;
906         node->ptr = ptr;
907         node->cookie = cookie;
908         node->work.type = BINDER_WORK_NODE;
909         INIT_LIST_HEAD(&node->work.entry);
910         INIT_LIST_HEAD(&node->async_todo);
911         binder_debug(BINDER_DEBUG_INTERNAL_REFS,
912                      "%d:%d node %d u%016llx c%016llx created\n",
913                      proc->pid, current->pid, node->debug_id,
914                      (u64)node->ptr, (u64)node->cookie);
915         return node;
916 }
917
918 static int binder_inc_node(struct binder_node *node, int strong, int internal,
919                            struct list_head *target_list)
920 {
921         if (strong) {
922                 if (internal) {
923                         if (target_list == NULL &&
924                             node->internal_strong_refs == 0 &&
925                             !(node == binder_context_mgr_node &&
926                             node->has_strong_ref)) {
927                                 pr_err("invalid inc strong node for %d\n",
928                                         node->debug_id);
929                                 return -EINVAL;
930                         }
931                         node->internal_strong_refs++;
932                 } else
933                         node->local_strong_refs++;
934                 if (!node->has_strong_ref && target_list) {
935                         list_del_init(&node->work.entry);
936                         list_add_tail(&node->work.entry, target_list);
937                 }
938         } else {
939                 if (!internal)
940                         node->local_weak_refs++;
941                 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
942                         if (target_list == NULL) {
943                                 pr_err("invalid inc weak node for %d\n",
944                                         node->debug_id);
945                                 return -EINVAL;
946                         }
947                         list_add_tail(&node->work.entry, target_list);
948                 }
949         }
950         return 0;
951 }
952
953 static int binder_dec_node(struct binder_node *node, int strong, int internal)
954 {
955         if (strong) {
956                 if (internal)
957                         node->internal_strong_refs--;
958                 else
959                         node->local_strong_refs--;
960                 if (node->local_strong_refs || node->internal_strong_refs)
961                         return 0;
962         } else {
963                 if (!internal)
964                         node->local_weak_refs--;
965                 if (node->local_weak_refs || !hlist_empty(&node->refs))
966                         return 0;
967         }
968         if (node->proc && (node->has_strong_ref || node->has_weak_ref)) {
969                 if (list_empty(&node->work.entry)) {
970                         list_add_tail(&node->work.entry, &node->proc->todo);
971                         wake_up_interruptible(&node->proc->wait);
972                 }
973         } else {
974                 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
975                     !node->local_weak_refs) {
976                         list_del_init(&node->work.entry);
977                         if (node->proc) {
978                                 rb_erase(&node->rb_node, &node->proc->nodes);
979                                 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
980                                              "refless node %d deleted\n",
981                                              node->debug_id);
982                         } else {
983                                 hlist_del(&node->dead_node);
984                                 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
985                                              "dead node %d deleted\n",
986                                              node->debug_id);
987                         }
988                         kfree(node);
989                         binder_stats_deleted(BINDER_STAT_NODE);
990                 }
991         }
992
993         return 0;
994 }
995
996
997 static struct binder_ref *binder_get_ref(struct binder_proc *proc,
998                                          uint32_t desc)
999 {
1000         struct rb_node *n = proc->refs_by_desc.rb_node;
1001         struct binder_ref *ref;
1002
1003         while (n) {
1004                 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1005
1006                 if (desc < ref->desc)
1007                         n = n->rb_left;
1008                 else if (desc > ref->desc)
1009                         n = n->rb_right;
1010                 else
1011                         return ref;
1012         }
1013         return NULL;
1014 }
1015
1016 static struct binder_ref *binder_get_ref_for_node(struct binder_proc *proc,
1017                                                   struct binder_node *node)
1018 {
1019         struct rb_node *n;
1020         struct rb_node **p = &proc->refs_by_node.rb_node;
1021         struct rb_node *parent = NULL;
1022         struct binder_ref *ref, *new_ref;
1023
1024         while (*p) {
1025                 parent = *p;
1026                 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1027
1028                 if (node < ref->node)
1029                         p = &(*p)->rb_left;
1030                 else if (node > ref->node)
1031                         p = &(*p)->rb_right;
1032                 else
1033                         return ref;
1034         }
1035         new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1036         if (new_ref == NULL)
1037                 return NULL;
1038         binder_stats_created(BINDER_STAT_REF);
1039         new_ref->debug_id = ++binder_last_id;
1040         new_ref->proc = proc;
1041         new_ref->node = node;
1042         rb_link_node(&new_ref->rb_node_node, parent, p);
1043         rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1044
1045         new_ref->desc = (node == binder_context_mgr_node) ? 0 : 1;
1046         for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1047                 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1048                 if (ref->desc > new_ref->desc)
1049                         break;
1050                 new_ref->desc = ref->desc + 1;
1051         }
1052
1053         p = &proc->refs_by_desc.rb_node;
1054         while (*p) {
1055                 parent = *p;
1056                 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1057
1058                 if (new_ref->desc < ref->desc)
1059                         p = &(*p)->rb_left;
1060                 else if (new_ref->desc > ref->desc)
1061                         p = &(*p)->rb_right;
1062                 else
1063                         BUG();
1064         }
1065         rb_link_node(&new_ref->rb_node_desc, parent, p);
1066         rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
1067         if (node) {
1068                 hlist_add_head(&new_ref->node_entry, &node->refs);
1069
1070                 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1071                              "%d new ref %d desc %d for node %d\n",
1072                               proc->pid, new_ref->debug_id, new_ref->desc,
1073                               node->debug_id);
1074         } else {
1075                 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1076                              "%d new ref %d desc %d for dead node\n",
1077                               proc->pid, new_ref->debug_id, new_ref->desc);
1078         }
1079         return new_ref;
1080 }
1081
1082 static void binder_delete_ref(struct binder_ref *ref)
1083 {
1084         binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1085                      "%d delete ref %d desc %d for node %d\n",
1086                       ref->proc->pid, ref->debug_id, ref->desc,
1087                       ref->node->debug_id);
1088
1089         rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1090         rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
1091         if (ref->strong)
1092                 binder_dec_node(ref->node, 1, 1);
1093         hlist_del(&ref->node_entry);
1094         binder_dec_node(ref->node, 0, 1);
1095         if (ref->death) {
1096                 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1097                              "%d delete ref %d desc %d has death notification\n",
1098                               ref->proc->pid, ref->debug_id, ref->desc);
1099                 list_del(&ref->death->work.entry);
1100                 kfree(ref->death);
1101                 binder_stats_deleted(BINDER_STAT_DEATH);
1102         }
1103         kfree(ref);
1104         binder_stats_deleted(BINDER_STAT_REF);
1105 }
1106
1107 static int binder_inc_ref(struct binder_ref *ref, int strong,
1108                           struct list_head *target_list)
1109 {
1110         int ret;
1111         if (strong) {
1112                 if (ref->strong == 0) {
1113                         ret = binder_inc_node(ref->node, 1, 1, target_list);
1114                         if (ret)
1115                                 return ret;
1116                 }
1117                 ref->strong++;
1118         } else {
1119                 if (ref->weak == 0) {
1120                         ret = binder_inc_node(ref->node, 0, 1, target_list);
1121                         if (ret)
1122                                 return ret;
1123                 }
1124                 ref->weak++;
1125         }
1126         return 0;
1127 }
1128
1129
1130 static int binder_dec_ref(struct binder_ref *ref, int strong)
1131 {
1132         if (strong) {
1133                 if (ref->strong == 0) {
1134                         binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
1135                                           ref->proc->pid, ref->debug_id,
1136                                           ref->desc, ref->strong, ref->weak);
1137                         return -EINVAL;
1138                 }
1139                 ref->strong--;
1140                 if (ref->strong == 0) {
1141                         int ret;
1142                         ret = binder_dec_node(ref->node, strong, 1);
1143                         if (ret)
1144                                 return ret;
1145                 }
1146         } else {
1147                 if (ref->weak == 0) {
1148                         binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
1149                                           ref->proc->pid, ref->debug_id,
1150                                           ref->desc, ref->strong, ref->weak);
1151                         return -EINVAL;
1152                 }
1153                 ref->weak--;
1154         }
1155         if (ref->strong == 0 && ref->weak == 0)
1156                 binder_delete_ref(ref);
1157         return 0;
1158 }
1159
1160 static void binder_pop_transaction(struct binder_thread *target_thread,
1161                                    struct binder_transaction *t)
1162 {
1163         if (target_thread) {
1164                 BUG_ON(target_thread->transaction_stack != t);
1165                 BUG_ON(target_thread->transaction_stack->from != target_thread);
1166                 target_thread->transaction_stack =
1167                         target_thread->transaction_stack->from_parent;
1168                 t->from = NULL;
1169         }
1170         t->need_reply = 0;
1171         if (t->buffer)
1172                 t->buffer->transaction = NULL;
1173         kfree(t);
1174         binder_stats_deleted(BINDER_STAT_TRANSACTION);
1175 }
1176
1177 static void binder_send_failed_reply(struct binder_transaction *t,
1178                                      uint32_t error_code)
1179 {
1180         struct binder_thread *target_thread;
1181         BUG_ON(t->flags & TF_ONE_WAY);
1182         while (1) {
1183                 target_thread = t->from;
1184                 if (target_thread) {
1185                         if (target_thread->return_error != BR_OK &&
1186                            target_thread->return_error2 == BR_OK) {
1187                                 target_thread->return_error2 =
1188                                         target_thread->return_error;
1189                                 target_thread->return_error = BR_OK;
1190                         }
1191                         if (target_thread->return_error == BR_OK) {
1192                                 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1193                                              "send failed reply for transaction %d to %d:%d\n",
1194                                               t->debug_id, target_thread->proc->pid,
1195                                               target_thread->pid);
1196
1197                                 binder_pop_transaction(target_thread, t);
1198                                 target_thread->return_error = error_code;
1199                                 wake_up_interruptible(&target_thread->wait);
1200                         } else {
1201                                 pr_err("reply failed, target thread, %d:%d, has error code %d already\n",
1202                                         target_thread->proc->pid,
1203                                         target_thread->pid,
1204                                         target_thread->return_error);
1205                         }
1206                         return;
1207                 } else {
1208                         struct binder_transaction *next = t->from_parent;
1209
1210                         binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1211                                      "send failed reply for transaction %d, target dead\n",
1212                                      t->debug_id);
1213
1214                         binder_pop_transaction(target_thread, t);
1215                         if (next == NULL) {
1216                                 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1217                                              "reply failed, no target thread at root\n");
1218                                 return;
1219                         }
1220                         t = next;
1221                         binder_debug(BINDER_DEBUG_DEAD_BINDER,
1222                                      "reply failed, no target thread -- retry %d\n",
1223                                       t->debug_id);
1224                 }
1225         }
1226 }
1227
1228 static void binder_transaction_buffer_release(struct binder_proc *proc,
1229                                               struct binder_buffer *buffer,
1230                                               binder_size_t *failed_at)
1231 {
1232         binder_size_t *offp, *off_end;
1233         int debug_id = buffer->debug_id;
1234
1235         binder_debug(BINDER_DEBUG_TRANSACTION,
1236                      "%d buffer release %d, size %zd-%zd, failed at %p\n",
1237                      proc->pid, buffer->debug_id,
1238                      buffer->data_size, buffer->offsets_size, failed_at);
1239
1240         if (buffer->target_node)
1241                 binder_dec_node(buffer->target_node, 1, 0);
1242
1243         offp = (binder_size_t *)(buffer->data +
1244                                  ALIGN(buffer->data_size, sizeof(void *)));
1245         if (failed_at)
1246                 off_end = failed_at;
1247         else
1248                 off_end = (void *)offp + buffer->offsets_size;
1249         for (; offp < off_end; offp++) {
1250                 struct flat_binder_object *fp;
1251                 if (*offp > buffer->data_size - sizeof(*fp) ||
1252                     buffer->data_size < sizeof(*fp) ||
1253                     !IS_ALIGNED(*offp, sizeof(u32))) {
1254                         pr_err("transaction release %d bad offset %lld, size %zd\n",
1255                                debug_id, (u64)*offp, buffer->data_size);
1256                         continue;
1257                 }
1258                 fp = (struct flat_binder_object *)(buffer->data + *offp);
1259                 switch (fp->type) {
1260                 case BINDER_TYPE_BINDER:
1261                 case BINDER_TYPE_WEAK_BINDER: {
1262                         struct binder_node *node = binder_get_node(proc, fp->binder);
1263                         if (node == NULL) {
1264                                 pr_err("transaction release %d bad node %016llx\n",
1265                                        debug_id, (u64)fp->binder);
1266                                 break;
1267                         }
1268                         binder_debug(BINDER_DEBUG_TRANSACTION,
1269                                      "        node %d u%016llx\n",
1270                                      node->debug_id, (u64)node->ptr);
1271                         binder_dec_node(node, fp->type == BINDER_TYPE_BINDER, 0);
1272                 } break;
1273                 case BINDER_TYPE_HANDLE:
1274                 case BINDER_TYPE_WEAK_HANDLE: {
1275                         struct binder_ref *ref = binder_get_ref(proc, fp->handle);
1276                         if (ref == NULL) {
1277                                 pr_err("transaction release %d bad handle %d\n",
1278                                  debug_id, fp->handle);
1279                                 break;
1280                         }
1281                         binder_debug(BINDER_DEBUG_TRANSACTION,
1282                                      "        ref %d desc %d (node %d)\n",
1283                                      ref->debug_id, ref->desc, ref->node->debug_id);
1284                         binder_dec_ref(ref, fp->type == BINDER_TYPE_HANDLE);
1285                 } break;
1286
1287                 case BINDER_TYPE_FD:
1288                         binder_debug(BINDER_DEBUG_TRANSACTION,
1289                                      "        fd %d\n", fp->handle);
1290                         if (failed_at)
1291                                 task_close_fd(proc, fp->handle);
1292                         break;
1293
1294                 default:
1295                         pr_err("transaction release %d bad object type %x\n",
1296                                 debug_id, fp->type);
1297                         break;
1298                 }
1299         }
1300 }
1301
1302 static void binder_transaction(struct binder_proc *proc,
1303                                struct binder_thread *thread,
1304                                struct binder_transaction_data *tr, int reply)
1305 {
1306         struct binder_transaction *t;
1307         struct binder_work *tcomplete;
1308         binder_size_t *offp, *off_end;
1309         binder_size_t off_min;
1310         struct binder_proc *target_proc;
1311         struct binder_thread *target_thread = NULL;
1312         struct binder_node *target_node = NULL;
1313         struct list_head *target_list;
1314         wait_queue_head_t *target_wait;
1315         struct binder_transaction *in_reply_to = NULL;
1316         struct binder_transaction_log_entry *e;
1317         uint32_t return_error;
1318
1319         e = binder_transaction_log_add(&binder_transaction_log);
1320         e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
1321         e->from_proc = proc->pid;
1322         e->from_thread = thread->pid;
1323         e->target_handle = tr->target.handle;
1324         e->data_size = tr->data_size;
1325         e->offsets_size = tr->offsets_size;
1326
1327         if (reply) {
1328                 in_reply_to = thread->transaction_stack;
1329                 if (in_reply_to == NULL) {
1330                         binder_user_error("%d:%d got reply transaction with no transaction stack\n",
1331                                           proc->pid, thread->pid);
1332                         return_error = BR_FAILED_REPLY;
1333                         goto err_empty_call_stack;
1334                 }
1335                 binder_set_nice(in_reply_to->saved_priority);
1336                 if (in_reply_to->to_thread != thread) {
1337                         binder_user_error("%d:%d got reply transaction with bad transaction stack, transaction %d has target %d:%d\n",
1338                                 proc->pid, thread->pid, in_reply_to->debug_id,
1339                                 in_reply_to->to_proc ?
1340                                 in_reply_to->to_proc->pid : 0,
1341                                 in_reply_to->to_thread ?
1342                                 in_reply_to->to_thread->pid : 0);
1343                         return_error = BR_FAILED_REPLY;
1344                         in_reply_to = NULL;
1345                         goto err_bad_call_stack;
1346                 }
1347                 thread->transaction_stack = in_reply_to->to_parent;
1348                 target_thread = in_reply_to->from;
1349                 if (target_thread == NULL) {
1350                         return_error = BR_DEAD_REPLY;
1351                         goto err_dead_binder;
1352                 }
1353                 if (target_thread->transaction_stack != in_reply_to) {
1354                         binder_user_error("%d:%d got reply transaction with bad target transaction stack %d, expected %d\n",
1355                                 proc->pid, thread->pid,
1356                                 target_thread->transaction_stack ?
1357                                 target_thread->transaction_stack->debug_id : 0,
1358                                 in_reply_to->debug_id);
1359                         return_error = BR_FAILED_REPLY;
1360                         in_reply_to = NULL;
1361                         target_thread = NULL;
1362                         goto err_dead_binder;
1363                 }
1364                 target_proc = target_thread->proc;
1365         } else {
1366                 if (tr->target.handle) {
1367                         struct binder_ref *ref;
1368                         ref = binder_get_ref(proc, tr->target.handle);
1369                         if (ref == NULL) {
1370                                 binder_user_error("%d:%d got transaction to invalid handle\n",
1371                                         proc->pid, thread->pid);
1372                                 return_error = BR_FAILED_REPLY;
1373                                 goto err_invalid_target_handle;
1374                         }
1375                         target_node = ref->node;
1376                 } else {
1377                         target_node = binder_context_mgr_node;
1378                         if (target_node == NULL) {
1379                                 return_error = BR_DEAD_REPLY;
1380                                 goto err_no_context_mgr_node;
1381                         }
1382                 }
1383                 e->to_node = target_node->debug_id;
1384                 target_proc = target_node->proc;
1385                 if (target_proc == NULL) {
1386                         return_error = BR_DEAD_REPLY;
1387                         goto err_dead_binder;
1388                 }
1389                 if (security_binder_transaction(proc->tsk, target_proc->tsk) < 0) {
1390                         return_error = BR_FAILED_REPLY;
1391                         goto err_invalid_target_handle;
1392                 }
1393                 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
1394                         struct binder_transaction *tmp;
1395                         tmp = thread->transaction_stack;
1396                         if (tmp->to_thread != thread) {
1397                                 binder_user_error("%d:%d got new transaction with bad transaction stack, transaction %d has target %d:%d\n",
1398                                         proc->pid, thread->pid, tmp->debug_id,
1399                                         tmp->to_proc ? tmp->to_proc->pid : 0,
1400                                         tmp->to_thread ?
1401                                         tmp->to_thread->pid : 0);
1402                                 return_error = BR_FAILED_REPLY;
1403                                 goto err_bad_call_stack;
1404                         }
1405                         while (tmp) {
1406                                 if (tmp->from && tmp->from->proc == target_proc)
1407                                         target_thread = tmp->from;
1408                                 tmp = tmp->from_parent;
1409                         }
1410                 }
1411         }
1412         if (target_thread) {
1413                 e->to_thread = target_thread->pid;
1414                 target_list = &target_thread->todo;
1415                 target_wait = &target_thread->wait;
1416         } else {
1417                 target_list = &target_proc->todo;
1418                 target_wait = &target_proc->wait;
1419         }
1420         e->to_proc = target_proc->pid;
1421
1422         /* TODO: reuse incoming transaction for reply */
1423         t = kzalloc(sizeof(*t), GFP_KERNEL);
1424         if (t == NULL) {
1425                 return_error = BR_FAILED_REPLY;
1426                 goto err_alloc_t_failed;
1427         }
1428         binder_stats_created(BINDER_STAT_TRANSACTION);
1429
1430         tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
1431         if (tcomplete == NULL) {
1432                 return_error = BR_FAILED_REPLY;
1433                 goto err_alloc_tcomplete_failed;
1434         }
1435         binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
1436
1437         t->debug_id = ++binder_last_id;
1438         e->debug_id = t->debug_id;
1439
1440         if (reply)
1441                 binder_debug(BINDER_DEBUG_TRANSACTION,
1442                              "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld\n",
1443                              proc->pid, thread->pid, t->debug_id,
1444                              target_proc->pid, target_thread->pid,
1445                              (u64)tr->data.ptr.buffer,
1446                              (u64)tr->data.ptr.offsets,
1447                              (u64)tr->data_size, (u64)tr->offsets_size);
1448         else
1449                 binder_debug(BINDER_DEBUG_TRANSACTION,
1450                              "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld\n",
1451                              proc->pid, thread->pid, t->debug_id,
1452                              target_proc->pid, target_node->debug_id,
1453                              (u64)tr->data.ptr.buffer,
1454                              (u64)tr->data.ptr.offsets,
1455                              (u64)tr->data_size, (u64)tr->offsets_size);
1456
1457         if (!reply && !(tr->flags & TF_ONE_WAY))
1458                 t->from = thread;
1459         else
1460                 t->from = NULL;
1461         t->sender_euid = proc->tsk->cred->euid;
1462         t->to_proc = target_proc;
1463         t->to_thread = target_thread;
1464         t->code = tr->code;
1465         t->flags = tr->flags;
1466         t->priority = task_nice(current);
1467
1468         trace_binder_transaction(reply, t, target_node);
1469
1470         t->buffer = binder_alloc_buf(target_proc, tr->data_size,
1471                 tr->offsets_size, !reply && (t->flags & TF_ONE_WAY));
1472         if (t->buffer == NULL) {
1473                 return_error = BR_FAILED_REPLY;
1474                 goto err_binder_alloc_buf_failed;
1475         }
1476         t->buffer->allow_user_free = 0;
1477         t->buffer->debug_id = t->debug_id;
1478         t->buffer->transaction = t;
1479         t->buffer->target_node = target_node;
1480         trace_binder_transaction_alloc_buf(t->buffer);
1481         if (target_node)
1482                 binder_inc_node(target_node, 1, 0, NULL);
1483
1484         offp = (binder_size_t *)(t->buffer->data +
1485                                  ALIGN(tr->data_size, sizeof(void *)));
1486
1487         if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t)
1488                            tr->data.ptr.buffer, tr->data_size)) {
1489                 binder_user_error("%d:%d got transaction with invalid data ptr\n",
1490                                 proc->pid, thread->pid);
1491                 return_error = BR_FAILED_REPLY;
1492                 goto err_copy_data_failed;
1493         }
1494         if (copy_from_user(offp, (const void __user *)(uintptr_t)
1495                            tr->data.ptr.offsets, tr->offsets_size)) {
1496                 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
1497                                 proc->pid, thread->pid);
1498                 return_error = BR_FAILED_REPLY;
1499                 goto err_copy_data_failed;
1500         }
1501         if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
1502                 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
1503                                 proc->pid, thread->pid, (u64)tr->offsets_size);
1504                 return_error = BR_FAILED_REPLY;
1505                 goto err_bad_offset;
1506         }
1507         off_end = (void *)offp + tr->offsets_size;
1508         off_min = 0;
1509         for (; offp < off_end; offp++) {
1510                 struct flat_binder_object *fp;
1511                 if (*offp > t->buffer->data_size - sizeof(*fp) ||
1512                     *offp < off_min ||
1513                     t->buffer->data_size < sizeof(*fp) ||
1514                     !IS_ALIGNED(*offp, sizeof(u32))) {
1515                         binder_user_error("%d:%d got transaction with invalid offset, %lld (min %lld, max %lld)\n",
1516                                           proc->pid, thread->pid, (u64)*offp,
1517                                           (u64)off_min,
1518                                           (u64)(t->buffer->data_size -
1519                                           sizeof(*fp)));
1520                         return_error = BR_FAILED_REPLY;
1521                         goto err_bad_offset;
1522                 }
1523                 fp = (struct flat_binder_object *)(t->buffer->data + *offp);
1524                 off_min = *offp + sizeof(struct flat_binder_object);
1525                 switch (fp->type) {
1526                 case BINDER_TYPE_BINDER:
1527                 case BINDER_TYPE_WEAK_BINDER: {
1528                         struct binder_ref *ref;
1529                         struct binder_node *node = binder_get_node(proc, fp->binder);
1530                         if (node == NULL) {
1531                                 node = binder_new_node(proc, fp->binder, fp->cookie);
1532                                 if (node == NULL) {
1533                                         return_error = BR_FAILED_REPLY;
1534                                         goto err_binder_new_node_failed;
1535                                 }
1536                                 node->min_priority = fp->flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
1537                                 node->accept_fds = !!(fp->flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
1538                         }
1539                         if (fp->cookie != node->cookie) {
1540                                 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
1541                                         proc->pid, thread->pid,
1542                                         (u64)fp->binder, node->debug_id,
1543                                         (u64)fp->cookie, (u64)node->cookie);
1544                                 goto err_binder_get_ref_for_node_failed;
1545                         }
1546                         if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
1547                                 return_error = BR_FAILED_REPLY;
1548                                 goto err_binder_get_ref_for_node_failed;
1549                         }
1550                         ref = binder_get_ref_for_node(target_proc, node);
1551                         if (ref == NULL) {
1552                                 return_error = BR_FAILED_REPLY;
1553                                 goto err_binder_get_ref_for_node_failed;
1554                         }
1555                         if (fp->type == BINDER_TYPE_BINDER)
1556                                 fp->type = BINDER_TYPE_HANDLE;
1557                         else
1558                                 fp->type = BINDER_TYPE_WEAK_HANDLE;
1559                         fp->handle = ref->desc;
1560                         binder_inc_ref(ref, fp->type == BINDER_TYPE_HANDLE,
1561                                        &thread->todo);
1562
1563                         trace_binder_transaction_node_to_ref(t, node, ref);
1564                         binder_debug(BINDER_DEBUG_TRANSACTION,
1565                                      "        node %d u%016llx -> ref %d desc %d\n",
1566                                      node->debug_id, (u64)node->ptr,
1567                                      ref->debug_id, ref->desc);
1568                 } break;
1569                 case BINDER_TYPE_HANDLE:
1570                 case BINDER_TYPE_WEAK_HANDLE: {
1571                         struct binder_ref *ref = binder_get_ref(proc, fp->handle);
1572                         if (ref == NULL) {
1573                                 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
1574                                                 proc->pid,
1575                                                 thread->pid, fp->handle);
1576                                 return_error = BR_FAILED_REPLY;
1577                                 goto err_binder_get_ref_failed;
1578                         }
1579                         if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
1580                                 return_error = BR_FAILED_REPLY;
1581                                 goto err_binder_get_ref_failed;
1582                         }
1583                         if (ref->node->proc == target_proc) {
1584                                 if (fp->type == BINDER_TYPE_HANDLE)
1585                                         fp->type = BINDER_TYPE_BINDER;
1586                                 else
1587                                         fp->type = BINDER_TYPE_WEAK_BINDER;
1588                                 fp->binder = ref->node->ptr;
1589                                 fp->cookie = ref->node->cookie;
1590                                 binder_inc_node(ref->node, fp->type == BINDER_TYPE_BINDER, 0, NULL);
1591                                 trace_binder_transaction_ref_to_node(t, ref);
1592                                 binder_debug(BINDER_DEBUG_TRANSACTION,
1593                                              "        ref %d desc %d -> node %d u%016llx\n",
1594                                              ref->debug_id, ref->desc, ref->node->debug_id,
1595                                              (u64)ref->node->ptr);
1596                         } else {
1597                                 struct binder_ref *new_ref;
1598                                 new_ref = binder_get_ref_for_node(target_proc, ref->node);
1599                                 if (new_ref == NULL) {
1600                                         return_error = BR_FAILED_REPLY;
1601                                         goto err_binder_get_ref_for_node_failed;
1602                                 }
1603                                 fp->handle = new_ref->desc;
1604                                 binder_inc_ref(new_ref, fp->type == BINDER_TYPE_HANDLE, NULL);
1605                                 trace_binder_transaction_ref_to_ref(t, ref,
1606                                                                     new_ref);
1607                                 binder_debug(BINDER_DEBUG_TRANSACTION,
1608                                              "        ref %d desc %d -> ref %d desc %d (node %d)\n",
1609                                              ref->debug_id, ref->desc, new_ref->debug_id,
1610                                              new_ref->desc, ref->node->debug_id);
1611                         }
1612                 } break;
1613
1614                 case BINDER_TYPE_FD: {
1615                         int target_fd;
1616                         struct file *file;
1617
1618                         if (reply) {
1619                                 if (!(in_reply_to->flags & TF_ACCEPT_FDS)) {
1620                                         binder_user_error("%d:%d got reply with fd, %d, but target does not allow fds\n",
1621                                                 proc->pid, thread->pid, fp->handle);
1622                                         return_error = BR_FAILED_REPLY;
1623                                         goto err_fd_not_allowed;
1624                                 }
1625                         } else if (!target_node->accept_fds) {
1626                                 binder_user_error("%d:%d got transaction with fd, %d, but target does not allow fds\n",
1627                                         proc->pid, thread->pid, fp->handle);
1628                                 return_error = BR_FAILED_REPLY;
1629                                 goto err_fd_not_allowed;
1630                         }
1631
1632                         file = fget(fp->handle);
1633                         if (file == NULL) {
1634                                 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
1635                                         proc->pid, thread->pid, fp->handle);
1636                                 return_error = BR_FAILED_REPLY;
1637                                 goto err_fget_failed;
1638                         }
1639                         if (security_binder_transfer_file(proc->tsk, target_proc->tsk, file) < 0) {
1640                                 fput(file);
1641                                 return_error = BR_FAILED_REPLY;
1642                                 goto err_get_unused_fd_failed;
1643                         }
1644                         target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
1645                         if (target_fd < 0) {
1646                                 fput(file);
1647                                 return_error = BR_FAILED_REPLY;
1648                                 goto err_get_unused_fd_failed;
1649                         }
1650                         task_fd_install(target_proc, target_fd, file);
1651                         trace_binder_transaction_fd(t, fp->handle, target_fd);
1652                         binder_debug(BINDER_DEBUG_TRANSACTION,
1653                                      "        fd %d -> %d\n", fp->handle, target_fd);
1654                         /* TODO: fput? */
1655                         fp->handle = target_fd;
1656                 } break;
1657
1658                 default:
1659                         binder_user_error("%d:%d got transaction with invalid object type, %x\n",
1660                                 proc->pid, thread->pid, fp->type);
1661                         return_error = BR_FAILED_REPLY;
1662                         goto err_bad_object_type;
1663                 }
1664         }
1665         if (reply) {
1666                 BUG_ON(t->buffer->async_transaction != 0);
1667                 binder_pop_transaction(target_thread, in_reply_to);
1668         } else if (!(t->flags & TF_ONE_WAY)) {
1669                 BUG_ON(t->buffer->async_transaction != 0);
1670                 t->need_reply = 1;
1671                 t->from_parent = thread->transaction_stack;
1672                 thread->transaction_stack = t;
1673         } else {
1674                 BUG_ON(target_node == NULL);
1675                 BUG_ON(t->buffer->async_transaction != 1);
1676                 if (target_node->has_async_transaction) {
1677                         target_list = &target_node->async_todo;
1678                         target_wait = NULL;
1679                 } else
1680                         target_node->has_async_transaction = 1;
1681         }
1682         t->work.type = BINDER_WORK_TRANSACTION;
1683         list_add_tail(&t->work.entry, target_list);
1684         tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
1685         list_add_tail(&tcomplete->entry, &thread->todo);
1686         if (target_wait)
1687                 wake_up_interruptible(target_wait);
1688         return;
1689
1690 err_get_unused_fd_failed:
1691 err_fget_failed:
1692 err_fd_not_allowed:
1693 err_binder_get_ref_for_node_failed:
1694 err_binder_get_ref_failed:
1695 err_binder_new_node_failed:
1696 err_bad_object_type:
1697 err_bad_offset:
1698 err_copy_data_failed:
1699         trace_binder_transaction_failed_buffer_release(t->buffer);
1700         binder_transaction_buffer_release(target_proc, t->buffer, offp);
1701         t->buffer->transaction = NULL;
1702         binder_free_buf(target_proc, t->buffer);
1703 err_binder_alloc_buf_failed:
1704         kfree(tcomplete);
1705         binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
1706 err_alloc_tcomplete_failed:
1707         kfree(t);
1708         binder_stats_deleted(BINDER_STAT_TRANSACTION);
1709 err_alloc_t_failed:
1710 err_bad_call_stack:
1711 err_empty_call_stack:
1712 err_dead_binder:
1713 err_invalid_target_handle:
1714 err_no_context_mgr_node:
1715         binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1716                      "%d:%d transaction failed %d, size %lld-%lld\n",
1717                      proc->pid, thread->pid, return_error,
1718                      (u64)tr->data_size, (u64)tr->offsets_size);
1719
1720         {
1721                 struct binder_transaction_log_entry *fe;
1722                 fe = binder_transaction_log_add(&binder_transaction_log_failed);
1723                 *fe = *e;
1724         }
1725
1726         BUG_ON(thread->return_error != BR_OK);
1727         if (in_reply_to) {
1728                 thread->return_error = BR_TRANSACTION_COMPLETE;
1729                 binder_send_failed_reply(in_reply_to, return_error);
1730         } else
1731                 thread->return_error = return_error;
1732 }
1733
1734 int binder_thread_write(struct binder_proc *proc, struct binder_thread *thread,
1735                         binder_uintptr_t binder_buffer, size_t size,
1736                         binder_size_t *consumed)
1737 {
1738         uint32_t cmd;
1739         void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
1740         void __user *ptr = buffer + *consumed;
1741         void __user *end = buffer + size;
1742
1743         while (ptr < end && thread->return_error == BR_OK) {
1744                 if (get_user(cmd, (uint32_t __user *)ptr))
1745                         return -EFAULT;
1746                 ptr += sizeof(uint32_t);
1747                 trace_binder_command(cmd);
1748                 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
1749                         binder_stats.bc[_IOC_NR(cmd)]++;
1750                         proc->stats.bc[_IOC_NR(cmd)]++;
1751                         thread->stats.bc[_IOC_NR(cmd)]++;
1752                 }
1753                 switch (cmd) {
1754                 case BC_INCREFS:
1755                 case BC_ACQUIRE:
1756                 case BC_RELEASE:
1757                 case BC_DECREFS: {
1758                         uint32_t target;
1759                         struct binder_ref *ref;
1760                         const char *debug_string;
1761
1762                         if (get_user(target, (uint32_t __user *)ptr))
1763                                 return -EFAULT;
1764                         ptr += sizeof(uint32_t);
1765                         if (target == 0 && binder_context_mgr_node &&
1766                             (cmd == BC_INCREFS || cmd == BC_ACQUIRE)) {
1767                                 ref = binder_get_ref_for_node(proc,
1768                                                binder_context_mgr_node);
1769                                 if (ref->desc != target) {
1770                                         binder_user_error("%d:%d tried to acquire reference to desc 0, got %d instead\n",
1771                                                 proc->pid, thread->pid,
1772                                                 ref->desc);
1773                                 }
1774                         } else
1775                                 ref = binder_get_ref(proc, target);
1776                         if (ref == NULL) {
1777                                 binder_user_error("%d:%d refcount change on invalid ref %d\n",
1778                                         proc->pid, thread->pid, target);
1779                                 break;
1780                         }
1781                         switch (cmd) {
1782                         case BC_INCREFS:
1783                                 debug_string = "IncRefs";
1784                                 binder_inc_ref(ref, 0, NULL);
1785                                 break;
1786                         case BC_ACQUIRE:
1787                                 debug_string = "Acquire";
1788                                 binder_inc_ref(ref, 1, NULL);
1789                                 break;
1790                         case BC_RELEASE:
1791                                 debug_string = "Release";
1792                                 binder_dec_ref(ref, 1);
1793                                 break;
1794                         case BC_DECREFS:
1795                         default:
1796                                 debug_string = "DecRefs";
1797                                 binder_dec_ref(ref, 0);
1798                                 break;
1799                         }
1800                         binder_debug(BINDER_DEBUG_USER_REFS,
1801                                      "%d:%d %s ref %d desc %d s %d w %d for node %d\n",
1802                                      proc->pid, thread->pid, debug_string, ref->debug_id,
1803                                      ref->desc, ref->strong, ref->weak, ref->node->debug_id);
1804                         break;
1805                 }
1806                 case BC_INCREFS_DONE:
1807                 case BC_ACQUIRE_DONE: {
1808                         binder_uintptr_t node_ptr;
1809                         binder_uintptr_t cookie;
1810                         struct binder_node *node;
1811
1812                         if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
1813                                 return -EFAULT;
1814                         ptr += sizeof(binder_uintptr_t);
1815                         if (get_user(cookie, (binder_uintptr_t __user *)ptr))
1816                                 return -EFAULT;
1817                         ptr += sizeof(binder_uintptr_t);
1818                         node = binder_get_node(proc, node_ptr);
1819                         if (node == NULL) {
1820                                 binder_user_error("%d:%d %s u%016llx no match\n",
1821                                         proc->pid, thread->pid,
1822                                         cmd == BC_INCREFS_DONE ?
1823                                         "BC_INCREFS_DONE" :
1824                                         "BC_ACQUIRE_DONE",
1825                                         (u64)node_ptr);
1826                                 break;
1827                         }
1828                         if (cookie != node->cookie) {
1829                                 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
1830                                         proc->pid, thread->pid,
1831                                         cmd == BC_INCREFS_DONE ?
1832                                         "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
1833                                         (u64)node_ptr, node->debug_id,
1834                                         (u64)cookie, (u64)node->cookie);
1835                                 break;
1836                         }
1837                         if (cmd == BC_ACQUIRE_DONE) {
1838                                 if (node->pending_strong_ref == 0) {
1839                                         binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
1840                                                 proc->pid, thread->pid,
1841                                                 node->debug_id);
1842                                         break;
1843                                 }
1844                                 node->pending_strong_ref = 0;
1845                         } else {
1846                                 if (node->pending_weak_ref == 0) {
1847                                         binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
1848                                                 proc->pid, thread->pid,
1849                                                 node->debug_id);
1850                                         break;
1851                                 }
1852                                 node->pending_weak_ref = 0;
1853                         }
1854                         binder_dec_node(node, cmd == BC_ACQUIRE_DONE, 0);
1855                         binder_debug(BINDER_DEBUG_USER_REFS,
1856                                      "%d:%d %s node %d ls %d lw %d\n",
1857                                      proc->pid, thread->pid,
1858                                      cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
1859                                      node->debug_id, node->local_strong_refs, node->local_weak_refs);
1860                         break;
1861                 }
1862                 case BC_ATTEMPT_ACQUIRE:
1863                         pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
1864                         return -EINVAL;
1865                 case BC_ACQUIRE_RESULT:
1866                         pr_err("BC_ACQUIRE_RESULT not supported\n");
1867                         return -EINVAL;
1868
1869                 case BC_FREE_BUFFER: {
1870                         binder_uintptr_t data_ptr;
1871                         struct binder_buffer *buffer;
1872
1873                         if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
1874                                 return -EFAULT;
1875                         ptr += sizeof(binder_uintptr_t);
1876
1877                         buffer = binder_buffer_lookup(proc, data_ptr);
1878                         if (buffer == NULL) {
1879                                 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
1880                                         proc->pid, thread->pid, (u64)data_ptr);
1881                                 break;
1882                         }
1883                         if (!buffer->allow_user_free) {
1884                                 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
1885                                         proc->pid, thread->pid, (u64)data_ptr);
1886                                 break;
1887                         }
1888                         binder_debug(BINDER_DEBUG_FREE_BUFFER,
1889                                      "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
1890                                      proc->pid, thread->pid, (u64)data_ptr, buffer->debug_id,
1891                                      buffer->transaction ? "active" : "finished");
1892
1893                         if (buffer->transaction) {
1894                                 buffer->transaction->buffer = NULL;
1895                                 buffer->transaction = NULL;
1896                         }
1897                         if (buffer->async_transaction && buffer->target_node) {
1898                                 BUG_ON(!buffer->target_node->has_async_transaction);
1899                                 if (list_empty(&buffer->target_node->async_todo))
1900                                         buffer->target_node->has_async_transaction = 0;
1901                                 else
1902                                         list_move_tail(buffer->target_node->async_todo.next, &thread->todo);
1903                         }
1904                         trace_binder_transaction_buffer_release(buffer);
1905                         binder_transaction_buffer_release(proc, buffer, NULL);
1906                         binder_free_buf(proc, buffer);
1907                         break;
1908                 }
1909
1910                 case BC_TRANSACTION:
1911                 case BC_REPLY: {
1912                         struct binder_transaction_data tr;
1913
1914                         if (copy_from_user(&tr, ptr, sizeof(tr)))
1915                                 return -EFAULT;
1916                         ptr += sizeof(tr);
1917                         binder_transaction(proc, thread, &tr, cmd == BC_REPLY);
1918                         break;
1919                 }
1920
1921                 case BC_REGISTER_LOOPER:
1922                         binder_debug(BINDER_DEBUG_THREADS,
1923                                      "%d:%d BC_REGISTER_LOOPER\n",
1924                                      proc->pid, thread->pid);
1925                         if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
1926                                 thread->looper |= BINDER_LOOPER_STATE_INVALID;
1927                                 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
1928                                         proc->pid, thread->pid);
1929                         } else if (proc->requested_threads == 0) {
1930                                 thread->looper |= BINDER_LOOPER_STATE_INVALID;
1931                                 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
1932                                         proc->pid, thread->pid);
1933                         } else {
1934                                 proc->requested_threads--;
1935                                 proc->requested_threads_started++;
1936                         }
1937                         thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
1938                         break;
1939                 case BC_ENTER_LOOPER:
1940                         binder_debug(BINDER_DEBUG_THREADS,
1941                                      "%d:%d BC_ENTER_LOOPER\n",
1942                                      proc->pid, thread->pid);
1943                         if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
1944                                 thread->looper |= BINDER_LOOPER_STATE_INVALID;
1945                                 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
1946                                         proc->pid, thread->pid);
1947                         }
1948                         thread->looper |= BINDER_LOOPER_STATE_ENTERED;
1949                         break;
1950                 case BC_EXIT_LOOPER:
1951                         binder_debug(BINDER_DEBUG_THREADS,
1952                                      "%d:%d BC_EXIT_LOOPER\n",
1953                                      proc->pid, thread->pid);
1954                         thread->looper |= BINDER_LOOPER_STATE_EXITED;
1955                         break;
1956
1957                 case BC_REQUEST_DEATH_NOTIFICATION:
1958                 case BC_CLEAR_DEATH_NOTIFICATION: {
1959                         uint32_t target;
1960                         binder_uintptr_t cookie;
1961                         struct binder_ref *ref;
1962                         struct binder_ref_death *death;
1963
1964                         if (get_user(target, (uint32_t __user *)ptr))
1965                                 return -EFAULT;
1966                         ptr += sizeof(uint32_t);
1967                         if (get_user(cookie, (binder_uintptr_t __user *)ptr))
1968                                 return -EFAULT;
1969                         ptr += sizeof(binder_uintptr_t);
1970                         ref = binder_get_ref(proc, target);
1971                         if (ref == NULL) {
1972                                 binder_user_error("%d:%d %s invalid ref %d\n",
1973                                         proc->pid, thread->pid,
1974                                         cmd == BC_REQUEST_DEATH_NOTIFICATION ?
1975                                         "BC_REQUEST_DEATH_NOTIFICATION" :
1976                                         "BC_CLEAR_DEATH_NOTIFICATION",
1977                                         target);
1978                                 break;
1979                         }
1980
1981                         binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
1982                                      "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
1983                                      proc->pid, thread->pid,
1984                                      cmd == BC_REQUEST_DEATH_NOTIFICATION ?
1985                                      "BC_REQUEST_DEATH_NOTIFICATION" :
1986                                      "BC_CLEAR_DEATH_NOTIFICATION",
1987                                      (u64)cookie, ref->debug_id, ref->desc,
1988                                      ref->strong, ref->weak, ref->node->debug_id);
1989
1990                         if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
1991                                 if (ref->death) {
1992                                         binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
1993                                                 proc->pid, thread->pid);
1994                                         break;
1995                                 }
1996                                 death = kzalloc(sizeof(*death), GFP_KERNEL);
1997                                 if (death == NULL) {
1998                                         thread->return_error = BR_ERROR;
1999                                         binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2000                                                      "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
2001                                                      proc->pid, thread->pid);
2002                                         break;
2003                                 }
2004                                 binder_stats_created(BINDER_STAT_DEATH);
2005                                 INIT_LIST_HEAD(&death->work.entry);
2006                                 death->cookie = cookie;
2007                                 ref->death = death;
2008                                 if (ref->node->proc == NULL) {
2009                                         ref->death->work.type = BINDER_WORK_DEAD_BINDER;
2010                                         if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2011                                                 list_add_tail(&ref->death->work.entry, &thread->todo);
2012                                         } else {
2013                                                 list_add_tail(&ref->death->work.entry, &proc->todo);
2014                                                 wake_up_interruptible(&proc->wait);
2015                                         }
2016                                 }
2017                         } else {
2018                                 if (ref->death == NULL) {
2019                                         binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
2020                                                 proc->pid, thread->pid);
2021                                         break;
2022                                 }
2023                                 death = ref->death;
2024                                 if (death->cookie != cookie) {
2025                                         binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
2026                                                 proc->pid, thread->pid,
2027                                                 (u64)death->cookie, (u64)cookie);
2028                                         break;
2029                                 }
2030                                 ref->death = NULL;
2031                                 if (list_empty(&death->work.entry)) {
2032                                         death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2033                                         if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2034                                                 list_add_tail(&death->work.entry, &thread->todo);
2035                                         } else {
2036                                                 list_add_tail(&death->work.entry, &proc->todo);
2037                                                 wake_up_interruptible(&proc->wait);
2038                                         }
2039                                 } else {
2040                                         BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
2041                                         death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
2042                                 }
2043                         }
2044                 } break;
2045                 case BC_DEAD_BINDER_DONE: {
2046                         struct binder_work *w;
2047                         binder_uintptr_t cookie;
2048                         struct binder_ref_death *death = NULL;
2049                         if (get_user(cookie, (binder_uintptr_t __user *)ptr))
2050                                 return -EFAULT;
2051
2052                         ptr += sizeof(void *);
2053                         list_for_each_entry(w, &proc->delivered_death, entry) {
2054                                 struct binder_ref_death *tmp_death = container_of(w, struct binder_ref_death, work);
2055                                 if (tmp_death->cookie == cookie) {
2056                                         death = tmp_death;
2057                                         break;
2058                                 }
2059                         }
2060                         binder_debug(BINDER_DEBUG_DEAD_BINDER,
2061                                      "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n",
2062                                      proc->pid, thread->pid, (u64)cookie, death);
2063                         if (death == NULL) {
2064                                 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
2065                                         proc->pid, thread->pid, (u64)cookie);
2066                                 break;
2067                         }
2068
2069                         list_del_init(&death->work.entry);
2070                         if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
2071                                 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2072                                 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2073                                         list_add_tail(&death->work.entry, &thread->todo);
2074                                 } else {
2075                                         list_add_tail(&death->work.entry, &proc->todo);
2076                                         wake_up_interruptible(&proc->wait);
2077                                 }
2078                         }
2079                 } break;
2080
2081                 default:
2082                         pr_err("%d:%d unknown command %d\n",
2083                                proc->pid, thread->pid, cmd);
2084                         return -EINVAL;
2085                 }
2086                 *consumed = ptr - buffer;
2087         }
2088         return 0;
2089 }
2090
2091 void binder_stat_br(struct binder_proc *proc, struct binder_thread *thread,
2092                     uint32_t cmd)
2093 {
2094         trace_binder_return(cmd);
2095         if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
2096                 binder_stats.br[_IOC_NR(cmd)]++;
2097                 proc->stats.br[_IOC_NR(cmd)]++;
2098                 thread->stats.br[_IOC_NR(cmd)]++;
2099         }
2100 }
2101
2102 static int binder_has_proc_work(struct binder_proc *proc,
2103                                 struct binder_thread *thread)
2104 {
2105         return !list_empty(&proc->todo) ||
2106                 (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2107 }
2108
2109 static int binder_has_thread_work(struct binder_thread *thread)
2110 {
2111         return !list_empty(&thread->todo) || thread->return_error != BR_OK ||
2112                 (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2113 }
2114
2115 static int binder_thread_read(struct binder_proc *proc,
2116                               struct binder_thread *thread,
2117                               binder_uintptr_t binder_buffer, size_t size,
2118                               binder_size_t *consumed, int non_block)
2119 {
2120         void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
2121         void __user *ptr = buffer + *consumed;
2122         void __user *end = buffer + size;
2123
2124         int ret = 0;
2125         int wait_for_proc_work;
2126
2127         if (*consumed == 0) {
2128                 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
2129                         return -EFAULT;
2130                 ptr += sizeof(uint32_t);
2131         }
2132
2133 retry:
2134         wait_for_proc_work = thread->transaction_stack == NULL &&
2135                                 list_empty(&thread->todo);
2136
2137         if (thread->return_error != BR_OK && ptr < end) {
2138                 if (thread->return_error2 != BR_OK) {
2139                         if (put_user(thread->return_error2, (uint32_t __user *)ptr))
2140                                 return -EFAULT;
2141                         ptr += sizeof(uint32_t);
2142                         binder_stat_br(proc, thread, thread->return_error2);
2143                         if (ptr == end)
2144                                 goto done;
2145                         thread->return_error2 = BR_OK;
2146                 }
2147                 if (put_user(thread->return_error, (uint32_t __user *)ptr))
2148                         return -EFAULT;
2149                 ptr += sizeof(uint32_t);
2150                 binder_stat_br(proc, thread, thread->return_error);
2151                 thread->return_error = BR_OK;
2152                 goto done;
2153         }
2154
2155
2156         thread->looper |= BINDER_LOOPER_STATE_WAITING;
2157         if (wait_for_proc_work)
2158                 proc->ready_threads++;
2159
2160         binder_unlock(__func__);
2161
2162         trace_binder_wait_for_work(wait_for_proc_work,
2163                                    !!thread->transaction_stack,
2164                                    !list_empty(&thread->todo));
2165         if (wait_for_proc_work) {
2166                 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2167                                         BINDER_LOOPER_STATE_ENTERED))) {
2168                         binder_user_error("%d:%d ERROR: Thread waiting for process work before calling BC_REGISTER_LOOPER or BC_ENTER_LOOPER (state %x)\n",
2169                                 proc->pid, thread->pid, thread->looper);
2170                         wait_event_interruptible(binder_user_error_wait,
2171                                                  binder_stop_on_user_error < 2);
2172                 }
2173                 binder_set_nice(proc->default_priority);
2174                 if (non_block) {
2175                         if (!binder_has_proc_work(proc, thread))
2176                                 ret = -EAGAIN;
2177                 } else
2178                         ret = wait_event_freezable_exclusive(proc->wait, binder_has_proc_work(proc, thread));
2179         } else {
2180                 if (non_block) {
2181                         if (!binder_has_thread_work(thread))
2182                                 ret = -EAGAIN;
2183                 } else
2184                         ret = wait_event_freezable(thread->wait, binder_has_thread_work(thread));
2185         }
2186
2187         binder_lock(__func__);
2188
2189         if (wait_for_proc_work)
2190                 proc->ready_threads--;
2191         thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
2192
2193         if (ret)
2194                 return ret;
2195
2196         while (1) {
2197                 uint32_t cmd;
2198                 struct binder_transaction_data tr;
2199                 struct binder_work *w;
2200                 struct binder_transaction *t = NULL;
2201
2202                 if (!list_empty(&thread->todo))
2203                         w = list_first_entry(&thread->todo, struct binder_work, entry);
2204                 else if (!list_empty(&proc->todo) && wait_for_proc_work)
2205                         w = list_first_entry(&proc->todo, struct binder_work, entry);
2206                 else {
2207                         if (ptr - buffer == 4 && !(thread->looper & BINDER_LOOPER_STATE_NEED_RETURN)) /* no data added */
2208                                 goto retry;
2209                         break;
2210                 }
2211
2212                 if (end - ptr < sizeof(tr) + 4)
2213                         break;
2214
2215                 switch (w->type) {
2216                 case BINDER_WORK_TRANSACTION: {
2217                         t = container_of(w, struct binder_transaction, work);
2218                 } break;
2219                 case BINDER_WORK_TRANSACTION_COMPLETE: {
2220                         cmd = BR_TRANSACTION_COMPLETE;
2221                         if (put_user(cmd, (uint32_t __user *)ptr))
2222                                 return -EFAULT;
2223                         ptr += sizeof(uint32_t);
2224
2225                         binder_stat_br(proc, thread, cmd);
2226                         binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
2227                                      "%d:%d BR_TRANSACTION_COMPLETE\n",
2228                                      proc->pid, thread->pid);
2229
2230                         list_del(&w->entry);
2231                         kfree(w);
2232                         binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2233                 } break;
2234                 case BINDER_WORK_NODE: {
2235                         struct binder_node *node = container_of(w, struct binder_node, work);
2236                         uint32_t cmd = BR_NOOP;
2237                         const char *cmd_name;
2238                         int strong = node->internal_strong_refs || node->local_strong_refs;
2239                         int weak = !hlist_empty(&node->refs) || node->local_weak_refs || strong;
2240                         if (weak && !node->has_weak_ref) {
2241                                 cmd = BR_INCREFS;
2242                                 cmd_name = "BR_INCREFS";
2243                                 node->has_weak_ref = 1;
2244                                 node->pending_weak_ref = 1;
2245                                 node->local_weak_refs++;
2246                         } else if (strong && !node->has_strong_ref) {
2247                                 cmd = BR_ACQUIRE;
2248                                 cmd_name = "BR_ACQUIRE";
2249                                 node->has_strong_ref = 1;
2250                                 node->pending_strong_ref = 1;
2251                                 node->local_strong_refs++;
2252                         } else if (!strong && node->has_strong_ref) {
2253                                 cmd = BR_RELEASE;
2254                                 cmd_name = "BR_RELEASE";
2255                                 node->has_strong_ref = 0;
2256                         } else if (!weak && node->has_weak_ref) {
2257                                 cmd = BR_DECREFS;
2258                                 cmd_name = "BR_DECREFS";
2259                                 node->has_weak_ref = 0;
2260                         }
2261                         if (cmd != BR_NOOP) {
2262                                 if (put_user(cmd, (uint32_t __user *)ptr))
2263                                         return -EFAULT;
2264                                 ptr += sizeof(uint32_t);
2265                                 if (put_user(node->ptr,
2266                                              (binder_uintptr_t __user *)ptr))
2267                                         return -EFAULT;
2268                                 ptr += sizeof(binder_uintptr_t);
2269                                 if (put_user(node->cookie,
2270                                              (binder_uintptr_t __user *)ptr))
2271                                         return -EFAULT;
2272                                 ptr += sizeof(binder_uintptr_t);
2273
2274                                 binder_stat_br(proc, thread, cmd);
2275                                 binder_debug(BINDER_DEBUG_USER_REFS,
2276                                              "%d:%d %s %d u%016llx c%016llx\n",
2277                                              proc->pid, thread->pid, cmd_name,
2278                                              node->debug_id,
2279                                              (u64)node->ptr, (u64)node->cookie);
2280                         } else {
2281                                 list_del_init(&w->entry);
2282                                 if (!weak && !strong) {
2283                                         binder_debug(BINDER_DEBUG_INTERNAL_REFS,
2284                                                      "%d:%d node %d u%016llx c%016llx deleted\n",
2285                                                      proc->pid, thread->pid, node->debug_id,
2286                                                      (u64)node->ptr, (u64)node->cookie);
2287                                         rb_erase(&node->rb_node, &proc->nodes);
2288                                         kfree(node);
2289                                         binder_stats_deleted(BINDER_STAT_NODE);
2290                                 } else {
2291                                         binder_debug(BINDER_DEBUG_INTERNAL_REFS,
2292                                                      "%d:%d node %d u%016llx c%016llx state unchanged\n",
2293                                                      proc->pid, thread->pid, node->debug_id,
2294                                                      (u64)node->ptr, (u64)node->cookie);
2295                                 }
2296                         }
2297                 } break;
2298                 case BINDER_WORK_DEAD_BINDER:
2299                 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2300                 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2301                         struct binder_ref_death *death;
2302                         uint32_t cmd;
2303
2304                         death = container_of(w, struct binder_ref_death, work);
2305                         if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
2306                                 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
2307                         else
2308                                 cmd = BR_DEAD_BINDER;
2309                         if (put_user(cmd, (uint32_t __user *)ptr))
2310                                 return -EFAULT;
2311                         ptr += sizeof(uint32_t);
2312                         if (put_user(death->cookie,
2313                                      (binder_uintptr_t __user *)ptr))
2314                                 return -EFAULT;
2315                         ptr += sizeof(binder_uintptr_t);
2316                         binder_stat_br(proc, thread, cmd);
2317                         binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
2318                                      "%d:%d %s %016llx\n",
2319                                       proc->pid, thread->pid,
2320                                       cmd == BR_DEAD_BINDER ?
2321                                       "BR_DEAD_BINDER" :
2322                                       "BR_CLEAR_DEATH_NOTIFICATION_DONE",
2323                                       (u64)death->cookie);
2324
2325                         if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
2326                                 list_del(&w->entry);
2327                                 kfree(death);
2328                                 binder_stats_deleted(BINDER_STAT_DEATH);
2329                         } else
2330                                 list_move(&w->entry, &proc->delivered_death);
2331                         if (cmd == BR_DEAD_BINDER)
2332                                 goto done; /* DEAD_BINDER notifications can cause transactions */
2333                 } break;
2334                 }
2335
2336                 if (!t)
2337                         continue;
2338
2339                 BUG_ON(t->buffer == NULL);
2340                 if (t->buffer->target_node) {
2341                         struct binder_node *target_node = t->buffer->target_node;
2342                         tr.target.ptr = target_node->ptr;
2343                         tr.cookie =  target_node->cookie;
2344                         t->saved_priority = task_nice(current);
2345                         if (t->priority < target_node->min_priority &&
2346                             !(t->flags & TF_ONE_WAY))
2347                                 binder_set_nice(t->priority);
2348                         else if (!(t->flags & TF_ONE_WAY) ||
2349                                  t->saved_priority > target_node->min_priority)
2350                                 binder_set_nice(target_node->min_priority);
2351                         cmd = BR_TRANSACTION;
2352                 } else {
2353                         tr.target.ptr = 0;
2354                         tr.cookie = 0;
2355                         cmd = BR_REPLY;
2356                 }
2357                 tr.code = t->code;
2358                 tr.flags = t->flags;
2359                 tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
2360
2361                 if (t->from) {
2362                         struct task_struct *sender = t->from->proc->tsk;
2363                         tr.sender_pid = task_tgid_nr_ns(sender,
2364                                                         task_active_pid_ns(current));
2365                 } else {
2366                         tr.sender_pid = 0;
2367                 }
2368
2369                 tr.data_size = t->buffer->data_size;
2370                 tr.offsets_size = t->buffer->offsets_size;
2371                 tr.data.ptr.buffer = (binder_uintptr_t)(
2372                                         (uintptr_t)t->buffer->data +
2373                                         proc->user_buffer_offset);
2374                 tr.data.ptr.offsets = tr.data.ptr.buffer +
2375                                         ALIGN(t->buffer->data_size,
2376                                             sizeof(void *));
2377
2378                 if (put_user(cmd, (uint32_t __user *)ptr))
2379                         return -EFAULT;
2380                 ptr += sizeof(uint32_t);
2381                 if (copy_to_user(ptr, &tr, sizeof(tr)))
2382                         return -EFAULT;
2383                 ptr += sizeof(tr);
2384
2385                 trace_binder_transaction_received(t);
2386                 binder_stat_br(proc, thread, cmd);
2387                 binder_debug(BINDER_DEBUG_TRANSACTION,
2388                              "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
2389                              proc->pid, thread->pid,
2390                              (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
2391                              "BR_REPLY",
2392                              t->debug_id, t->from ? t->from->proc->pid : 0,
2393                              t->from ? t->from->pid : 0, cmd,
2394                              t->buffer->data_size, t->buffer->offsets_size,
2395                              (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
2396
2397                 list_del(&t->work.entry);
2398                 t->buffer->allow_user_free = 1;
2399                 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
2400                         t->to_parent = thread->transaction_stack;
2401                         t->to_thread = thread;
2402                         thread->transaction_stack = t;
2403                 } else {
2404                         t->buffer->transaction = NULL;
2405                         kfree(t);
2406                         binder_stats_deleted(BINDER_STAT_TRANSACTION);
2407                 }
2408                 break;
2409         }
2410
2411 done:
2412
2413         *consumed = ptr - buffer;
2414         if (proc->requested_threads + proc->ready_threads == 0 &&
2415             proc->requested_threads_started < proc->max_threads &&
2416             (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2417              BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
2418              /*spawn a new thread if we leave this out */) {
2419                 proc->requested_threads++;
2420                 binder_debug(BINDER_DEBUG_THREADS,
2421                              "%d:%d BR_SPAWN_LOOPER\n",
2422                              proc->pid, thread->pid);
2423                 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
2424                         return -EFAULT;
2425                 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
2426         }
2427         return 0;
2428 }
2429
2430 static void binder_release_work(struct list_head *list)
2431 {
2432         struct binder_work *w;
2433         while (!list_empty(list)) {
2434                 w = list_first_entry(list, struct binder_work, entry);
2435                 list_del_init(&w->entry);
2436                 switch (w->type) {
2437                 case BINDER_WORK_TRANSACTION: {
2438                         struct binder_transaction *t;
2439
2440                         t = container_of(w, struct binder_transaction, work);
2441                         if (t->buffer->target_node &&
2442                             !(t->flags & TF_ONE_WAY)) {
2443                                 binder_send_failed_reply(t, BR_DEAD_REPLY);
2444                         } else {
2445                                 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2446                                         "undelivered transaction %d\n",
2447                                         t->debug_id);
2448                                 t->buffer->transaction = NULL;
2449                                 kfree(t);
2450                                 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2451                         }
2452                 } break;
2453                 case BINDER_WORK_TRANSACTION_COMPLETE: {
2454                         binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2455                                 "undelivered TRANSACTION_COMPLETE\n");
2456                         kfree(w);
2457                         binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2458                 } break;
2459                 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2460                 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2461                         struct binder_ref_death *death;
2462
2463                         death = container_of(w, struct binder_ref_death, work);
2464                         binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2465                                 "undelivered death notification, %016llx\n",
2466                                 (u64)death->cookie);
2467                         kfree(death);
2468                         binder_stats_deleted(BINDER_STAT_DEATH);
2469                 } break;
2470                 default:
2471                         pr_err("unexpected work type, %d, not freed\n",
2472                                w->type);
2473                         break;
2474                 }
2475         }
2476
2477 }
2478
2479 static struct binder_thread *binder_get_thread(struct binder_proc *proc)
2480 {
2481         struct binder_thread *thread = NULL;
2482         struct rb_node *parent = NULL;
2483         struct rb_node **p = &proc->threads.rb_node;
2484
2485         while (*p) {
2486                 parent = *p;
2487                 thread = rb_entry(parent, struct binder_thread, rb_node);
2488
2489                 if (current->pid < thread->pid)
2490                         p = &(*p)->rb_left;
2491                 else if (current->pid > thread->pid)
2492                         p = &(*p)->rb_right;
2493                 else
2494                         break;
2495         }
2496         if (*p == NULL) {
2497                 thread = kzalloc(sizeof(*thread), GFP_KERNEL);
2498                 if (thread == NULL)
2499                         return NULL;
2500                 binder_stats_created(BINDER_STAT_THREAD);
2501                 thread->proc = proc;
2502                 thread->pid = current->pid;
2503                 init_waitqueue_head(&thread->wait);
2504                 INIT_LIST_HEAD(&thread->todo);
2505                 rb_link_node(&thread->rb_node, parent, p);
2506                 rb_insert_color(&thread->rb_node, &proc->threads);
2507                 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
2508                 thread->return_error = BR_OK;
2509                 thread->return_error2 = BR_OK;
2510         }
2511         return thread;
2512 }
2513
2514 static int binder_free_thread(struct binder_proc *proc,
2515                               struct binder_thread *thread)
2516 {
2517         struct binder_transaction *t;
2518         struct binder_transaction *send_reply = NULL;
2519         int active_transactions = 0;
2520
2521         rb_erase(&thread->rb_node, &proc->threads);
2522         t = thread->transaction_stack;
2523         if (t && t->to_thread == thread)
2524                 send_reply = t;
2525         while (t) {
2526                 active_transactions++;
2527                 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2528                              "release %d:%d transaction %d %s, still active\n",
2529                               proc->pid, thread->pid,
2530                              t->debug_id,
2531                              (t->to_thread == thread) ? "in" : "out");
2532
2533                 if (t->to_thread == thread) {
2534                         t->to_proc = NULL;
2535                         t->to_thread = NULL;
2536                         if (t->buffer) {
2537                                 t->buffer->transaction = NULL;
2538                                 t->buffer = NULL;
2539                         }
2540                         t = t->to_parent;
2541                 } else if (t->from == thread) {
2542                         t->from = NULL;
2543                         t = t->from_parent;
2544                 } else
2545                         BUG();
2546         }
2547         if (send_reply)
2548                 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
2549         binder_release_work(&thread->todo);
2550         kfree(thread);
2551         binder_stats_deleted(BINDER_STAT_THREAD);
2552         return active_transactions;
2553 }
2554
2555 static unsigned int binder_poll(struct file *filp,
2556                                 struct poll_table_struct *wait)
2557 {
2558         struct binder_proc *proc = filp->private_data;
2559         struct binder_thread *thread = NULL;
2560         int wait_for_proc_work;
2561
2562         binder_lock(__func__);
2563
2564         thread = binder_get_thread(proc);
2565
2566         wait_for_proc_work = thread->transaction_stack == NULL &&
2567                 list_empty(&thread->todo) && thread->return_error == BR_OK;
2568
2569         binder_unlock(__func__);
2570
2571         if (wait_for_proc_work) {
2572                 if (binder_has_proc_work(proc, thread))
2573                         return POLLIN;
2574                 poll_wait(filp, &proc->wait, wait);
2575                 if (binder_has_proc_work(proc, thread))
2576                         return POLLIN;
2577         } else {
2578                 if (binder_has_thread_work(thread))
2579                         return POLLIN;
2580                 poll_wait(filp, &thread->wait, wait);
2581                 if (binder_has_thread_work(thread))
2582                         return POLLIN;
2583         }
2584         return 0;
2585 }
2586
2587 static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2588 {
2589         int ret;
2590         struct binder_proc *proc = filp->private_data;
2591         struct binder_thread *thread;
2592         unsigned int size = _IOC_SIZE(cmd);
2593         void __user *ubuf = (void __user *)arg;
2594
2595         /*pr_info("binder_ioctl: %d:%d %x %lx\n", proc->pid, current->pid, cmd, arg);*/
2596
2597         trace_binder_ioctl(cmd, arg);
2598
2599         ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2600         if (ret)
2601                 goto err_unlocked;
2602
2603         binder_lock(__func__);
2604         thread = binder_get_thread(proc);
2605         if (thread == NULL) {
2606                 ret = -ENOMEM;
2607                 goto err;
2608         }
2609
2610         switch (cmd) {
2611         case BINDER_WRITE_READ: {
2612                 struct binder_write_read bwr;
2613                 if (size != sizeof(struct binder_write_read)) {
2614                         ret = -EINVAL;
2615                         goto err;
2616                 }
2617                 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
2618                         ret = -EFAULT;
2619                         goto err;
2620                 }
2621                 binder_debug(BINDER_DEBUG_READ_WRITE,
2622                              "%d:%d write %lld at %016llx, read %lld at %016llx\n",
2623                              proc->pid, thread->pid,
2624                              (u64)bwr.write_size, (u64)bwr.write_buffer,
2625                              (u64)bwr.read_size, (u64)bwr.read_buffer);
2626
2627                 if (bwr.write_size > 0) {
2628                         ret = binder_thread_write(proc, thread, bwr.write_buffer, bwr.write_size, &bwr.write_consumed);
2629                         trace_binder_write_done(ret);
2630                         if (ret < 0) {
2631                                 bwr.read_consumed = 0;
2632                                 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2633                                         ret = -EFAULT;
2634                                 goto err;
2635                         }
2636                 }
2637                 if (bwr.read_size > 0) {
2638                         ret = binder_thread_read(proc, thread, bwr.read_buffer, bwr.read_size, &bwr.read_consumed, filp->f_flags & O_NONBLOCK);
2639                         trace_binder_read_done(ret);
2640                         if (!list_empty(&proc->todo))
2641                                 wake_up_interruptible(&proc->wait);
2642                         if (ret < 0) {
2643                                 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2644                                         ret = -EFAULT;
2645                                 goto err;
2646                         }
2647                 }
2648                 binder_debug(BINDER_DEBUG_READ_WRITE,
2649                              "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
2650                              proc->pid, thread->pid,
2651                              (u64)bwr.write_consumed, (u64)bwr.write_size,
2652                              (u64)bwr.read_consumed, (u64)bwr.read_size);
2653                 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
2654                         ret = -EFAULT;
2655                         goto err;
2656                 }
2657                 break;
2658         }
2659         case BINDER_SET_MAX_THREADS:
2660                 if (copy_from_user(&proc->max_threads, ubuf, sizeof(proc->max_threads))) {
2661                         ret = -EINVAL;
2662                         goto err;
2663                 }
2664                 break;
2665         case BINDER_SET_CONTEXT_MGR:
2666                 if (binder_context_mgr_node != NULL) {
2667                         pr_err("BINDER_SET_CONTEXT_MGR already set\n");
2668                         ret = -EBUSY;
2669                         goto err;
2670                 }
2671                 ret = security_binder_set_context_mgr(proc->tsk);
2672                 if (ret < 0)
2673                         goto err;
2674                 if (uid_valid(binder_context_mgr_uid)) {
2675                         if (!uid_eq(binder_context_mgr_uid, current->cred->euid)) {
2676                                 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
2677                                        from_kuid(&init_user_ns, current->cred->euid),
2678                                        from_kuid(&init_user_ns, binder_context_mgr_uid));
2679                                 ret = -EPERM;
2680                                 goto err;
2681                         }
2682                 } else
2683                         binder_context_mgr_uid = current->cred->euid;
2684                 binder_context_mgr_node = binder_new_node(proc, 0, 0);
2685                 if (binder_context_mgr_node == NULL) {
2686                         ret = -ENOMEM;
2687                         goto err;
2688                 }
2689                 binder_context_mgr_node->local_weak_refs++;
2690                 binder_context_mgr_node->local_strong_refs++;
2691                 binder_context_mgr_node->has_strong_ref = 1;
2692                 binder_context_mgr_node->has_weak_ref = 1;
2693                 break;
2694         case BINDER_THREAD_EXIT:
2695                 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
2696                              proc->pid, thread->pid);
2697                 binder_free_thread(proc, thread);
2698                 thread = NULL;
2699                 break;
2700         case BINDER_VERSION:
2701                 if (size != sizeof(struct binder_version)) {
2702                         ret = -EINVAL;
2703                         goto err;
2704                 }
2705                 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION, &((struct binder_version *)ubuf)->protocol_version)) {
2706                         ret = -EINVAL;
2707                         goto err;
2708                 }
2709                 break;
2710         default:
2711                 ret = -EINVAL;
2712                 goto err;
2713         }
2714         ret = 0;
2715 err:
2716         if (thread)
2717                 thread->looper &= ~BINDER_LOOPER_STATE_NEED_RETURN;
2718         binder_unlock(__func__);
2719         wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2720         if (ret && ret != -ERESTARTSYS)
2721                 pr_info("%d:%d ioctl %x %lx returned %d\n", proc->pid, current->pid, cmd, arg, ret);
2722 err_unlocked:
2723         trace_binder_ioctl_done(ret);
2724         return ret;
2725 }
2726
2727 static void binder_vma_open(struct vm_area_struct *vma)
2728 {
2729         struct binder_proc *proc = vma->vm_private_data;
2730         binder_debug(BINDER_DEBUG_OPEN_CLOSE,
2731                      "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
2732                      proc->pid, vma->vm_start, vma->vm_end,
2733                      (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2734                      (unsigned long)pgprot_val(vma->vm_page_prot));
2735 }
2736
2737 static void binder_vma_close(struct vm_area_struct *vma)
2738 {
2739         struct binder_proc *proc = vma->vm_private_data;
2740         binder_debug(BINDER_DEBUG_OPEN_CLOSE,
2741                      "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
2742                      proc->pid, vma->vm_start, vma->vm_end,
2743                      (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2744                      (unsigned long)pgprot_val(vma->vm_page_prot));
2745         proc->vma = NULL;
2746         proc->vma_vm_mm = NULL;
2747         binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
2748 }
2749
2750 static struct vm_operations_struct binder_vm_ops = {
2751         .open = binder_vma_open,
2752         .close = binder_vma_close,
2753 };
2754
2755 static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
2756 {
2757         int ret;
2758         struct vm_struct *area;
2759         struct binder_proc *proc = filp->private_data;
2760         const char *failure_string;
2761         struct binder_buffer *buffer;
2762
2763         if (proc->tsk != current)
2764                 return -EINVAL;
2765
2766         if ((vma->vm_end - vma->vm_start) > SZ_4M)
2767                 vma->vm_end = vma->vm_start + SZ_4M;
2768
2769         binder_debug(BINDER_DEBUG_OPEN_CLOSE,
2770                      "binder_mmap: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
2771                      proc->pid, vma->vm_start, vma->vm_end,
2772                      (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2773                      (unsigned long)pgprot_val(vma->vm_page_prot));
2774
2775         if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
2776                 ret = -EPERM;
2777                 failure_string = "bad vm_flags";
2778                 goto err_bad_arg;
2779         }
2780         vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
2781
2782         mutex_lock(&binder_mmap_lock);
2783         if (proc->buffer) {
2784                 ret = -EBUSY;
2785                 failure_string = "already mapped";
2786                 goto err_already_mapped;
2787         }
2788
2789         area = get_vm_area(vma->vm_end - vma->vm_start, VM_IOREMAP);
2790         if (area == NULL) {
2791                 ret = -ENOMEM;
2792                 failure_string = "get_vm_area";
2793                 goto err_get_vm_area_failed;
2794         }
2795         proc->buffer = area->addr;
2796         proc->user_buffer_offset = vma->vm_start - (uintptr_t)proc->buffer;
2797         mutex_unlock(&binder_mmap_lock);
2798
2799 #ifdef CONFIG_CPU_CACHE_VIPT
2800         if (cache_is_vipt_aliasing()) {
2801                 while (CACHE_COLOUR((vma->vm_start ^ (uint32_t)proc->buffer))) {
2802                         pr_info("binder_mmap: %d %lx-%lx maps %p bad alignment\n", proc->pid, vma->vm_start, vma->vm_end, proc->buffer);
2803                         vma->vm_start += PAGE_SIZE;
2804                 }
2805         }
2806 #endif
2807         proc->pages = kzalloc(sizeof(proc->pages[0]) * ((vma->vm_end - vma->vm_start) / PAGE_SIZE), GFP_KERNEL);
2808         if (proc->pages == NULL) {
2809                 ret = -ENOMEM;
2810                 failure_string = "alloc page array";
2811                 goto err_alloc_pages_failed;
2812         }
2813         proc->buffer_size = vma->vm_end - vma->vm_start;
2814
2815         vma->vm_ops = &binder_vm_ops;
2816         vma->vm_private_data = proc;
2817
2818         if (binder_update_page_range(proc, 1, proc->buffer, proc->buffer + PAGE_SIZE, vma)) {
2819                 ret = -ENOMEM;
2820                 failure_string = "alloc small buf";
2821                 goto err_alloc_small_buf_failed;
2822         }
2823         buffer = proc->buffer;
2824         INIT_LIST_HEAD(&proc->buffers);
2825         list_add(&buffer->entry, &proc->buffers);
2826         buffer->free = 1;
2827         binder_insert_free_buffer(proc, buffer);
2828         proc->free_async_space = proc->buffer_size / 2;
2829         barrier();
2830         proc->files = get_files_struct(current);
2831         proc->vma = vma;
2832         proc->vma_vm_mm = vma->vm_mm;
2833
2834         /*pr_info("binder_mmap: %d %lx-%lx maps %p\n",
2835                  proc->pid, vma->vm_start, vma->vm_end, proc->buffer);*/
2836         return 0;
2837
2838 err_alloc_small_buf_failed:
2839         kfree(proc->pages);
2840         proc->pages = NULL;
2841 err_alloc_pages_failed:
2842         mutex_lock(&binder_mmap_lock);
2843         vfree(proc->buffer);
2844         proc->buffer = NULL;
2845 err_get_vm_area_failed:
2846 err_already_mapped:
2847         mutex_unlock(&binder_mmap_lock);
2848 err_bad_arg:
2849         pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
2850                proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
2851         return ret;
2852 }
2853
2854 static int binder_open(struct inode *nodp, struct file *filp)
2855 {
2856         struct binder_proc *proc;
2857
2858         binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
2859                      current->group_leader->pid, current->pid);
2860
2861         proc = kzalloc(sizeof(*proc), GFP_KERNEL);
2862         if (proc == NULL)
2863                 return -ENOMEM;
2864         get_task_struct(current);
2865         proc->tsk = current;
2866         INIT_LIST_HEAD(&proc->todo);
2867         init_waitqueue_head(&proc->wait);
2868         proc->default_priority = task_nice(current);
2869
2870         binder_lock(__func__);
2871
2872         binder_stats_created(BINDER_STAT_PROC);
2873         hlist_add_head(&proc->proc_node, &binder_procs);
2874         proc->pid = current->group_leader->pid;
2875         INIT_LIST_HEAD(&proc->delivered_death);
2876         filp->private_data = proc;
2877
2878         binder_unlock(__func__);
2879
2880         if (binder_debugfs_dir_entry_proc) {
2881                 char strbuf[11];
2882                 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
2883                 proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
2884                         binder_debugfs_dir_entry_proc, proc, &binder_proc_fops);
2885         }
2886
2887         return 0;
2888 }
2889
2890 static int binder_flush(struct file *filp, fl_owner_t id)
2891 {
2892         struct binder_proc *proc = filp->private_data;
2893
2894         binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
2895
2896         return 0;
2897 }
2898
2899 static void binder_deferred_flush(struct binder_proc *proc)
2900 {
2901         struct rb_node *n;
2902         int wake_count = 0;
2903         for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
2904                 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
2905                 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
2906                 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
2907                         wake_up_interruptible(&thread->wait);
2908                         wake_count++;
2909                 }
2910         }
2911         wake_up_interruptible_all(&proc->wait);
2912
2913         binder_debug(BINDER_DEBUG_OPEN_CLOSE,
2914                      "binder_flush: %d woke %d threads\n", proc->pid,
2915                      wake_count);
2916 }
2917
2918 static int binder_release(struct inode *nodp, struct file *filp)
2919 {
2920         struct binder_proc *proc = filp->private_data;
2921         debugfs_remove(proc->debugfs_entry);
2922         binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
2923
2924         return 0;
2925 }
2926
2927 static int binder_node_release(struct binder_node *node, int refs)
2928 {
2929         struct binder_ref *ref;
2930         int death = 0;
2931
2932         list_del_init(&node->work.entry);
2933         binder_release_work(&node->async_todo);
2934
2935         if (hlist_empty(&node->refs)) {
2936                 kfree(node);
2937                 binder_stats_deleted(BINDER_STAT_NODE);
2938
2939                 return refs;
2940         }
2941
2942         node->proc = NULL;
2943         node->local_strong_refs = 0;
2944         node->local_weak_refs = 0;
2945         hlist_add_head(&node->dead_node, &binder_dead_nodes);
2946
2947         hlist_for_each_entry(ref, &node->refs, node_entry) {
2948                 refs++;
2949
2950                 if (!ref->death)
2951                         continue;
2952
2953                 death++;
2954
2955                 if (list_empty(&ref->death->work.entry)) {
2956                         ref->death->work.type = BINDER_WORK_DEAD_BINDER;
2957                         list_add_tail(&ref->death->work.entry,
2958                                       &ref->proc->todo);
2959                         wake_up_interruptible(&ref->proc->wait);
2960                 } else
2961                         BUG();
2962         }
2963
2964         binder_debug(BINDER_DEBUG_DEAD_BINDER,
2965                      "node %d now dead, refs %d, death %d\n",
2966                      node->debug_id, refs, death);
2967
2968         return refs;
2969 }
2970
2971 static void binder_deferred_release(struct binder_proc *proc)
2972 {
2973         struct binder_transaction *t;
2974         struct rb_node *n;
2975         int threads, nodes, incoming_refs, outgoing_refs, buffers,
2976                 active_transactions, page_count;
2977
2978         BUG_ON(proc->vma);
2979         BUG_ON(proc->files);
2980
2981         hlist_del(&proc->proc_node);
2982
2983         if (binder_context_mgr_node && binder_context_mgr_node->proc == proc) {
2984                 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2985                              "%s: %d context_mgr_node gone\n",
2986                              __func__, proc->pid);
2987                 binder_context_mgr_node = NULL;
2988         }
2989
2990         threads = 0;
2991         active_transactions = 0;
2992         while ((n = rb_first(&proc->threads))) {
2993                 struct binder_thread *thread;
2994
2995                 thread = rb_entry(n, struct binder_thread, rb_node);
2996                 threads++;
2997                 active_transactions += binder_free_thread(proc, thread);
2998         }
2999
3000         nodes = 0;
3001         incoming_refs = 0;
3002         while ((n = rb_first(&proc->nodes))) {
3003                 struct binder_node *node;
3004
3005                 node = rb_entry(n, struct binder_node, rb_node);
3006                 nodes++;
3007                 rb_erase(&node->rb_node, &proc->nodes);
3008                 incoming_refs = binder_node_release(node, incoming_refs);
3009         }
3010
3011         outgoing_refs = 0;
3012         while ((n = rb_first(&proc->refs_by_desc))) {
3013                 struct binder_ref *ref;
3014
3015                 ref = rb_entry(n, struct binder_ref, rb_node_desc);
3016                 outgoing_refs++;
3017                 binder_delete_ref(ref);
3018         }
3019
3020         binder_release_work(&proc->todo);
3021         binder_release_work(&proc->delivered_death);
3022
3023         buffers = 0;
3024         while ((n = rb_first(&proc->allocated_buffers))) {
3025                 struct binder_buffer *buffer;
3026
3027                 buffer = rb_entry(n, struct binder_buffer, rb_node);
3028
3029                 t = buffer->transaction;
3030                 if (t) {
3031                         t->buffer = NULL;
3032                         buffer->transaction = NULL;
3033                         pr_err("release proc %d, transaction %d, not freed\n",
3034                                proc->pid, t->debug_id);
3035                         /*BUG();*/
3036                 }
3037
3038                 binder_free_buf(proc, buffer);
3039                 buffers++;
3040         }
3041
3042         binder_stats_deleted(BINDER_STAT_PROC);
3043
3044         page_count = 0;
3045         if (proc->pages) {
3046                 int i;
3047
3048                 for (i = 0; i < proc->buffer_size / PAGE_SIZE; i++) {
3049                         void *page_addr;
3050
3051                         if (!proc->pages[i])
3052                                 continue;
3053
3054                         page_addr = proc->buffer + i * PAGE_SIZE;
3055                         binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
3056                                      "%s: %d: page %d at %p not freed\n",
3057                                      __func__, proc->pid, i, page_addr);
3058                         unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
3059                         __free_page(proc->pages[i]);
3060                         page_count++;
3061                 }
3062                 kfree(proc->pages);
3063                 vfree(proc->buffer);
3064         }
3065
3066         put_task_struct(proc->tsk);
3067
3068         binder_debug(BINDER_DEBUG_OPEN_CLOSE,
3069                      "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d, buffers %d, pages %d\n",
3070                      __func__, proc->pid, threads, nodes, incoming_refs,
3071                      outgoing_refs, active_transactions, buffers, page_count);
3072
3073         kfree(proc);
3074 }
3075
3076 static void binder_deferred_func(struct work_struct *work)
3077 {
3078         struct binder_proc *proc;
3079         struct files_struct *files;
3080
3081         int defer;
3082         do {
3083                 binder_lock(__func__);
3084                 mutex_lock(&binder_deferred_lock);
3085                 if (!hlist_empty(&binder_deferred_list)) {
3086                         proc = hlist_entry(binder_deferred_list.first,
3087                                         struct binder_proc, deferred_work_node);
3088                         hlist_del_init(&proc->deferred_work_node);
3089                         defer = proc->deferred_work;
3090                         proc->deferred_work = 0;
3091                 } else {
3092                         proc = NULL;
3093                         defer = 0;
3094                 }
3095                 mutex_unlock(&binder_deferred_lock);
3096
3097                 files = NULL;
3098                 if (defer & BINDER_DEFERRED_PUT_FILES) {
3099                         files = proc->files;
3100                         if (files)
3101                                 proc->files = NULL;
3102                 }
3103
3104                 if (defer & BINDER_DEFERRED_FLUSH)
3105                         binder_deferred_flush(proc);
3106
3107                 if (defer & BINDER_DEFERRED_RELEASE)
3108                         binder_deferred_release(proc); /* frees proc */
3109
3110                 binder_unlock(__func__);
3111                 if (files)
3112                         put_files_struct(files);
3113         } while (proc);
3114 }
3115 static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
3116
3117 static void
3118 binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
3119 {
3120         mutex_lock(&binder_deferred_lock);
3121         proc->deferred_work |= defer;
3122         if (hlist_unhashed(&proc->deferred_work_node)) {
3123                 hlist_add_head(&proc->deferred_work_node,
3124                                 &binder_deferred_list);
3125                 queue_work(binder_deferred_workqueue, &binder_deferred_work);
3126         }
3127         mutex_unlock(&binder_deferred_lock);
3128 }
3129
3130 static void print_binder_transaction(struct seq_file *m, const char *prefix,
3131                                      struct binder_transaction *t)
3132 {
3133         seq_printf(m,
3134                    "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %ld r%d",
3135                    prefix, t->debug_id, t,
3136                    t->from ? t->from->proc->pid : 0,
3137                    t->from ? t->from->pid : 0,
3138                    t->to_proc ? t->to_proc->pid : 0,
3139                    t->to_thread ? t->to_thread->pid : 0,
3140                    t->code, t->flags, t->priority, t->need_reply);
3141         if (t->buffer == NULL) {
3142                 seq_puts(m, " buffer free\n");
3143                 return;
3144         }
3145         if (t->buffer->target_node)
3146                 seq_printf(m, " node %d",
3147                            t->buffer->target_node->debug_id);
3148         seq_printf(m, " size %zd:%zd data %p\n",
3149                    t->buffer->data_size, t->buffer->offsets_size,
3150                    t->buffer->data);
3151 }
3152
3153 static void print_binder_buffer(struct seq_file *m, const char *prefix,
3154                                 struct binder_buffer *buffer)
3155 {
3156         seq_printf(m, "%s %d: %p size %zd:%zd %s\n",
3157                    prefix, buffer->debug_id, buffer->data,
3158                    buffer->data_size, buffer->offsets_size,
3159                    buffer->transaction ? "active" : "delivered");
3160 }
3161
3162 static void print_binder_work(struct seq_file *m, const char *prefix,
3163                               const char *transaction_prefix,
3164                               struct binder_work *w)
3165 {
3166         struct binder_node *node;
3167         struct binder_transaction *t;
3168
3169         switch (w->type) {
3170         case BINDER_WORK_TRANSACTION:
3171                 t = container_of(w, struct binder_transaction, work);
3172                 print_binder_transaction(m, transaction_prefix, t);
3173                 break;
3174         case BINDER_WORK_TRANSACTION_COMPLETE:
3175                 seq_printf(m, "%stransaction complete\n", prefix);
3176                 break;
3177         case BINDER_WORK_NODE:
3178                 node = container_of(w, struct binder_node, work);
3179                 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
3180                            prefix, node->debug_id,
3181                            (u64)node->ptr, (u64)node->cookie);
3182                 break;
3183         case BINDER_WORK_DEAD_BINDER:
3184                 seq_printf(m, "%shas dead binder\n", prefix);
3185                 break;
3186         case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
3187                 seq_printf(m, "%shas cleared dead binder\n", prefix);
3188                 break;
3189         case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
3190                 seq_printf(m, "%shas cleared death notification\n", prefix);
3191                 break;
3192         default:
3193                 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
3194                 break;
3195         }
3196 }
3197
3198 static void print_binder_thread(struct seq_file *m,
3199                                 struct binder_thread *thread,
3200                                 int print_always)
3201 {
3202         struct binder_transaction *t;
3203         struct binder_work *w;
3204         size_t start_pos = m->count;
3205         size_t header_pos;
3206
3207         seq_printf(m, "  thread %d: l %02x\n", thread->pid, thread->looper);
3208         header_pos = m->count;
3209         t = thread->transaction_stack;
3210         while (t) {
3211                 if (t->from == thread) {
3212                         print_binder_transaction(m,
3213                                                  "    outgoing transaction", t);
3214                         t = t->from_parent;
3215                 } else if (t->to_thread == thread) {
3216                         print_binder_transaction(m,
3217                                                  "    incoming transaction", t);
3218                         t = t->to_parent;
3219                 } else {
3220                         print_binder_transaction(m, "    bad transaction", t);
3221                         t = NULL;
3222                 }
3223         }
3224         list_for_each_entry(w, &thread->todo, entry) {
3225                 print_binder_work(m, "    ", "    pending transaction", w);
3226         }
3227         if (!print_always && m->count == header_pos)
3228                 m->count = start_pos;
3229 }
3230
3231 static void print_binder_node(struct seq_file *m, struct binder_node *node)
3232 {
3233         struct binder_ref *ref;
3234         struct binder_work *w;
3235         int count;
3236
3237         count = 0;
3238         hlist_for_each_entry(ref, &node->refs, node_entry)
3239                 count++;
3240
3241         seq_printf(m, "  node %d: u%016llx c%016llx hs %d hw %d ls %d lw %d is %d iw %d",
3242                    node->debug_id, (u64)node->ptr, (u64)node->cookie,
3243                    node->has_strong_ref, node->has_weak_ref,
3244                    node->local_strong_refs, node->local_weak_refs,
3245                    node->internal_strong_refs, count);
3246         if (count) {
3247                 seq_puts(m, " proc");
3248                 hlist_for_each_entry(ref, &node->refs, node_entry)
3249                         seq_printf(m, " %d", ref->proc->pid);
3250         }
3251         seq_puts(m, "\n");
3252         list_for_each_entry(w, &node->async_todo, entry)
3253                 print_binder_work(m, "    ",
3254                                   "    pending async transaction", w);
3255 }
3256
3257 static void print_binder_ref(struct seq_file *m, struct binder_ref *ref)
3258 {
3259         seq_printf(m, "  ref %d: desc %d %snode %d s %d w %d d %p\n",
3260                    ref->debug_id, ref->desc, ref->node->proc ? "" : "dead ",
3261                    ref->node->debug_id, ref->strong, ref->weak, ref->death);
3262 }
3263
3264 static void print_binder_proc(struct seq_file *m,
3265                               struct binder_proc *proc, int print_all)
3266 {
3267         struct binder_work *w;
3268         struct rb_node *n;
3269         size_t start_pos = m->count;
3270         size_t header_pos;
3271
3272         seq_printf(m, "proc %d\n", proc->pid);
3273         header_pos = m->count;
3274
3275         for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3276                 print_binder_thread(m, rb_entry(n, struct binder_thread,
3277                                                 rb_node), print_all);
3278         for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
3279                 struct binder_node *node = rb_entry(n, struct binder_node,
3280                                                     rb_node);
3281                 if (print_all || node->has_async_transaction)
3282                         print_binder_node(m, node);
3283         }
3284         if (print_all) {
3285                 for (n = rb_first(&proc->refs_by_desc);
3286                      n != NULL;
3287                      n = rb_next(n))
3288                         print_binder_ref(m, rb_entry(n, struct binder_ref,
3289                                                      rb_node_desc));
3290         }
3291         for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
3292                 print_binder_buffer(m, "  buffer",
3293                                     rb_entry(n, struct binder_buffer, rb_node));
3294         list_for_each_entry(w, &proc->todo, entry)
3295                 print_binder_work(m, "  ", "  pending transaction", w);
3296         list_for_each_entry(w, &proc->delivered_death, entry) {
3297                 seq_puts(m, "  has delivered dead binder\n");
3298                 break;
3299         }
3300         if (!print_all && m->count == header_pos)
3301                 m->count = start_pos;
3302 }
3303
3304 static const char * const binder_return_strings[] = {
3305         "BR_ERROR",
3306         "BR_OK",
3307         "BR_TRANSACTION",
3308         "BR_REPLY",
3309         "BR_ACQUIRE_RESULT",
3310         "BR_DEAD_REPLY",
3311         "BR_TRANSACTION_COMPLETE",
3312         "BR_INCREFS",
3313         "BR_ACQUIRE",
3314         "BR_RELEASE",
3315         "BR_DECREFS",
3316         "BR_ATTEMPT_ACQUIRE",
3317         "BR_NOOP",
3318         "BR_SPAWN_LOOPER",
3319         "BR_FINISHED",
3320         "BR_DEAD_BINDER",
3321         "BR_CLEAR_DEATH_NOTIFICATION_DONE",
3322         "BR_FAILED_REPLY"
3323 };
3324
3325 static const char * const binder_command_strings[] = {
3326         "BC_TRANSACTION",
3327         "BC_REPLY",
3328         "BC_ACQUIRE_RESULT",
3329         "BC_FREE_BUFFER",
3330         "BC_INCREFS",
3331         "BC_ACQUIRE",
3332         "BC_RELEASE",
3333         "BC_DECREFS",
3334         "BC_INCREFS_DONE",
3335         "BC_ACQUIRE_DONE",
3336         "BC_ATTEMPT_ACQUIRE",
3337         "BC_REGISTER_LOOPER",
3338         "BC_ENTER_LOOPER",
3339         "BC_EXIT_LOOPER",
3340         "BC_REQUEST_DEATH_NOTIFICATION",
3341         "BC_CLEAR_DEATH_NOTIFICATION",
3342         "BC_DEAD_BINDER_DONE"
3343 };
3344
3345 static const char * const binder_objstat_strings[] = {
3346         "proc",
3347         "thread",
3348         "node",
3349         "ref",
3350         "death",
3351         "transaction",
3352         "transaction_complete"
3353 };
3354
3355 static void print_binder_stats(struct seq_file *m, const char *prefix,
3356                                struct binder_stats *stats)
3357 {
3358         int i;
3359
3360         BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
3361                      ARRAY_SIZE(binder_command_strings));
3362         for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
3363                 if (stats->bc[i])
3364                         seq_printf(m, "%s%s: %d\n", prefix,
3365                                    binder_command_strings[i], stats->bc[i]);
3366         }
3367
3368         BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
3369                      ARRAY_SIZE(binder_return_strings));
3370         for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
3371                 if (stats->br[i])
3372                         seq_printf(m, "%s%s: %d\n", prefix,
3373                                    binder_return_strings[i], stats->br[i]);
3374         }
3375
3376         BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
3377                      ARRAY_SIZE(binder_objstat_strings));
3378         BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
3379                      ARRAY_SIZE(stats->obj_deleted));
3380         for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
3381                 if (stats->obj_created[i] || stats->obj_deleted[i])
3382                         seq_printf(m, "%s%s: active %d total %d\n", prefix,
3383                                 binder_objstat_strings[i],
3384                                 stats->obj_created[i] - stats->obj_deleted[i],
3385                                 stats->obj_created[i]);
3386         }
3387 }
3388
3389 static void print_binder_proc_stats(struct seq_file *m,
3390                                     struct binder_proc *proc)
3391 {
3392         struct binder_work *w;
3393         struct rb_node *n;
3394         int count, strong, weak;
3395
3396         seq_printf(m, "proc %d\n", proc->pid);
3397         count = 0;
3398         for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3399                 count++;
3400         seq_printf(m, "  threads: %d\n", count);
3401         seq_printf(m, "  requested threads: %d+%d/%d\n"
3402                         "  ready threads %d\n"
3403                         "  free async space %zd\n", proc->requested_threads,
3404                         proc->requested_threads_started, proc->max_threads,
3405                         proc->ready_threads, proc->free_async_space);
3406         count = 0;
3407         for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
3408                 count++;
3409         seq_printf(m, "  nodes: %d\n", count);
3410         count = 0;
3411         strong = 0;
3412         weak = 0;
3413         for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
3414                 struct binder_ref *ref = rb_entry(n, struct binder_ref,
3415                                                   rb_node_desc);
3416                 count++;
3417                 strong += ref->strong;
3418                 weak += ref->weak;
3419         }
3420         seq_printf(m, "  refs: %d s %d w %d\n", count, strong, weak);
3421
3422         count = 0;
3423         for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
3424                 count++;
3425         seq_printf(m, "  buffers: %d\n", count);
3426
3427         count = 0;
3428         list_for_each_entry(w, &proc->todo, entry) {
3429                 switch (w->type) {
3430                 case BINDER_WORK_TRANSACTION:
3431                         count++;
3432                         break;
3433                 default:
3434                         break;
3435                 }
3436         }
3437         seq_printf(m, "  pending transactions: %d\n", count);
3438
3439         print_binder_stats(m, "  ", &proc->stats);
3440 }
3441
3442
3443 static int binder_state_show(struct seq_file *m, void *unused)
3444 {
3445         struct binder_proc *proc;
3446         struct binder_node *node;
3447         int do_lock = !binder_debug_no_lock;
3448
3449         if (do_lock)
3450                 binder_lock(__func__);
3451
3452         seq_puts(m, "binder state:\n");
3453
3454         if (!hlist_empty(&binder_dead_nodes))
3455                 seq_puts(m, "dead nodes:\n");
3456         hlist_for_each_entry(node, &binder_dead_nodes, dead_node)
3457                 print_binder_node(m, node);
3458
3459         hlist_for_each_entry(proc, &binder_procs, proc_node)
3460                 print_binder_proc(m, proc, 1);
3461         if (do_lock)
3462                 binder_unlock(__func__);
3463         return 0;
3464 }
3465
3466 static int binder_stats_show(struct seq_file *m, void *unused)
3467 {
3468         struct binder_proc *proc;
3469         int do_lock = !binder_debug_no_lock;
3470
3471         if (do_lock)
3472                 binder_lock(__func__);
3473
3474         seq_puts(m, "binder stats:\n");
3475
3476         print_binder_stats(m, "", &binder_stats);
3477
3478         hlist_for_each_entry(proc, &binder_procs, proc_node)
3479                 print_binder_proc_stats(m, proc);
3480         if (do_lock)
3481                 binder_unlock(__func__);
3482         return 0;
3483 }
3484
3485 static int binder_transactions_show(struct seq_file *m, void *unused)
3486 {
3487         struct binder_proc *proc;
3488         int do_lock = !binder_debug_no_lock;
3489
3490         if (do_lock)
3491                 binder_lock(__func__);
3492
3493         seq_puts(m, "binder transactions:\n");
3494         hlist_for_each_entry(proc, &binder_procs, proc_node)
3495                 print_binder_proc(m, proc, 0);
3496         if (do_lock)
3497                 binder_unlock(__func__);
3498         return 0;
3499 }
3500
3501 static int binder_proc_show(struct seq_file *m, void *unused)
3502 {
3503         struct binder_proc *proc = m->private;
3504         int do_lock = !binder_debug_no_lock;
3505
3506         if (do_lock)
3507                 binder_lock(__func__);
3508         seq_puts(m, "binder proc state:\n");
3509         print_binder_proc(m, proc, 1);
3510         if (do_lock)
3511                 binder_unlock(__func__);
3512         return 0;
3513 }
3514
3515 static void print_binder_transaction_log_entry(struct seq_file *m,
3516                                         struct binder_transaction_log_entry *e)
3517 {
3518         seq_printf(m,
3519                    "%d: %s from %d:%d to %d:%d node %d handle %d size %d:%d\n",
3520                    e->debug_id, (e->call_type == 2) ? "reply" :
3521                    ((e->call_type == 1) ? "async" : "call "), e->from_proc,
3522                    e->from_thread, e->to_proc, e->to_thread, e->to_node,
3523                    e->target_handle, e->data_size, e->offsets_size);
3524 }
3525
3526 static int binder_transaction_log_show(struct seq_file *m, void *unused)
3527 {
3528         struct binder_transaction_log *log = m->private;
3529         int i;
3530
3531         if (log->full) {
3532                 for (i = log->next; i < ARRAY_SIZE(log->entry); i++)
3533                         print_binder_transaction_log_entry(m, &log->entry[i]);
3534         }
3535         for (i = 0; i < log->next; i++)
3536                 print_binder_transaction_log_entry(m, &log->entry[i]);
3537         return 0;
3538 }
3539
3540 static const struct file_operations binder_fops = {
3541         .owner = THIS_MODULE,
3542         .poll = binder_poll,
3543         .unlocked_ioctl = binder_ioctl,
3544         .compat_ioctl = binder_ioctl,
3545         .mmap = binder_mmap,
3546         .open = binder_open,
3547         .flush = binder_flush,
3548         .release = binder_release,
3549 };
3550
3551 static struct miscdevice binder_miscdev = {
3552         .minor = MISC_DYNAMIC_MINOR,
3553         .name = "binder",
3554         .fops = &binder_fops
3555 };
3556
3557 BINDER_DEBUG_ENTRY(state);
3558 BINDER_DEBUG_ENTRY(stats);
3559 BINDER_DEBUG_ENTRY(transactions);
3560 BINDER_DEBUG_ENTRY(transaction_log);
3561
3562 static int __init binder_init(void)
3563 {
3564         int ret;
3565
3566         binder_deferred_workqueue = create_singlethread_workqueue("binder");
3567         if (!binder_deferred_workqueue)
3568                 return -ENOMEM;
3569
3570         binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
3571         if (binder_debugfs_dir_entry_root)
3572                 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
3573                                                  binder_debugfs_dir_entry_root);
3574         ret = misc_register(&binder_miscdev);
3575         if (binder_debugfs_dir_entry_root) {
3576                 debugfs_create_file("state",
3577                                     S_IRUGO,
3578                                     binder_debugfs_dir_entry_root,
3579                                     NULL,
3580                                     &binder_state_fops);
3581                 debugfs_create_file("stats",
3582                                     S_IRUGO,
3583                                     binder_debugfs_dir_entry_root,
3584                                     NULL,
3585                                     &binder_stats_fops);
3586                 debugfs_create_file("transactions",
3587                                     S_IRUGO,
3588                                     binder_debugfs_dir_entry_root,
3589                                     NULL,
3590                                     &binder_transactions_fops);
3591                 debugfs_create_file("transaction_log",
3592                                     S_IRUGO,
3593                                     binder_debugfs_dir_entry_root,
3594                                     &binder_transaction_log,
3595                                     &binder_transaction_log_fops);
3596                 debugfs_create_file("failed_transaction_log",
3597                                     S_IRUGO,
3598                                     binder_debugfs_dir_entry_root,
3599                                     &binder_transaction_log_failed,
3600                                     &binder_transaction_log_fops);
3601         }
3602         return ret;
3603 }
3604
3605 device_initcall(binder_init);
3606
3607 #define CREATE_TRACE_POINTS
3608 #include "binder_trace.h"
3609
3610 MODULE_LICENSE("GPL v2");