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