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