OSDN Git Service

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