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