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