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