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