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