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