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