Merge branch 'linux-linaro-lsk-v4.4' into linux-linaro-lsk-v4.4-android
[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         binder_size_t off_min;
1332         struct binder_proc *target_proc;
1333         struct binder_thread *target_thread = NULL;
1334         struct binder_node *target_node = NULL;
1335         struct list_head *target_list;
1336         wait_queue_head_t *target_wait;
1337         struct binder_transaction *in_reply_to = NULL;
1338         struct binder_transaction_log_entry *e;
1339         uint32_t return_error;
1340
1341         e = binder_transaction_log_add(&binder_transaction_log);
1342         e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
1343         e->from_proc = proc->pid;
1344         e->from_thread = thread->pid;
1345         e->target_handle = tr->target.handle;
1346         e->data_size = tr->data_size;
1347         e->offsets_size = tr->offsets_size;
1348
1349         if (reply) {
1350                 in_reply_to = thread->transaction_stack;
1351                 if (in_reply_to == NULL) {
1352                         binder_user_error("%d:%d got reply transaction with no transaction stack\n",
1353                                           proc->pid, thread->pid);
1354                         return_error = BR_FAILED_REPLY;
1355                         goto err_empty_call_stack;
1356                 }
1357                 binder_set_nice(in_reply_to->saved_priority);
1358                 if (in_reply_to->to_thread != thread) {
1359                         binder_user_error("%d:%d got reply transaction with bad transaction stack, transaction %d has target %d:%d\n",
1360                                 proc->pid, thread->pid, in_reply_to->debug_id,
1361                                 in_reply_to->to_proc ?
1362                                 in_reply_to->to_proc->pid : 0,
1363                                 in_reply_to->to_thread ?
1364                                 in_reply_to->to_thread->pid : 0);
1365                         return_error = BR_FAILED_REPLY;
1366                         in_reply_to = NULL;
1367                         goto err_bad_call_stack;
1368                 }
1369                 thread->transaction_stack = in_reply_to->to_parent;
1370                 target_thread = in_reply_to->from;
1371                 if (target_thread == NULL) {
1372                         return_error = BR_DEAD_REPLY;
1373                         goto err_dead_binder;
1374                 }
1375                 if (target_thread->transaction_stack != in_reply_to) {
1376                         binder_user_error("%d:%d got reply transaction with bad target transaction stack %d, expected %d\n",
1377                                 proc->pid, thread->pid,
1378                                 target_thread->transaction_stack ?
1379                                 target_thread->transaction_stack->debug_id : 0,
1380                                 in_reply_to->debug_id);
1381                         return_error = BR_FAILED_REPLY;
1382                         in_reply_to = NULL;
1383                         target_thread = NULL;
1384                         goto err_dead_binder;
1385                 }
1386                 target_proc = target_thread->proc;
1387         } else {
1388                 if (tr->target.handle) {
1389                         struct binder_ref *ref;
1390
1391                         ref = binder_get_ref(proc, tr->target.handle, true);
1392                         if (ref == NULL) {
1393                                 binder_user_error("%d:%d got transaction to invalid handle\n",
1394                                         proc->pid, thread->pid);
1395                                 return_error = BR_FAILED_REPLY;
1396                                 goto err_invalid_target_handle;
1397                         }
1398                         target_node = ref->node;
1399                 } else {
1400                         target_node = binder_context_mgr_node;
1401                         if (target_node == NULL) {
1402                                 return_error = BR_DEAD_REPLY;
1403                                 goto err_no_context_mgr_node;
1404                         }
1405                 }
1406                 e->to_node = target_node->debug_id;
1407                 target_proc = target_node->proc;
1408                 if (target_proc == NULL) {
1409                         return_error = BR_DEAD_REPLY;
1410                         goto err_dead_binder;
1411                 }
1412                 if (security_binder_transaction(proc->tsk,
1413                                                 target_proc->tsk) < 0) {
1414                         return_error = BR_FAILED_REPLY;
1415                         goto err_invalid_target_handle;
1416                 }
1417                 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
1418                         struct binder_transaction *tmp;
1419
1420                         tmp = thread->transaction_stack;
1421                         if (tmp->to_thread != thread) {
1422                                 binder_user_error("%d:%d got new transaction with bad transaction stack, transaction %d has target %d:%d\n",
1423                                         proc->pid, thread->pid, tmp->debug_id,
1424                                         tmp->to_proc ? tmp->to_proc->pid : 0,
1425                                         tmp->to_thread ?
1426                                         tmp->to_thread->pid : 0);
1427                                 return_error = BR_FAILED_REPLY;
1428                                 goto err_bad_call_stack;
1429                         }
1430                         while (tmp) {
1431                                 if (tmp->from && tmp->from->proc == target_proc)
1432                                         target_thread = tmp->from;
1433                                 tmp = tmp->from_parent;
1434                         }
1435                 }
1436         }
1437         if (target_thread) {
1438                 e->to_thread = target_thread->pid;
1439                 target_list = &target_thread->todo;
1440                 target_wait = &target_thread->wait;
1441         } else {
1442                 target_list = &target_proc->todo;
1443                 target_wait = &target_proc->wait;
1444         }
1445         e->to_proc = target_proc->pid;
1446
1447         /* TODO: reuse incoming transaction for reply */
1448         t = kzalloc(sizeof(*t), GFP_KERNEL);
1449         if (t == NULL) {
1450                 return_error = BR_FAILED_REPLY;
1451                 goto err_alloc_t_failed;
1452         }
1453         binder_stats_created(BINDER_STAT_TRANSACTION);
1454
1455         tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
1456         if (tcomplete == NULL) {
1457                 return_error = BR_FAILED_REPLY;
1458                 goto err_alloc_tcomplete_failed;
1459         }
1460         binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
1461
1462         t->debug_id = ++binder_last_id;
1463         e->debug_id = t->debug_id;
1464
1465         if (reply)
1466                 binder_debug(BINDER_DEBUG_TRANSACTION,
1467                              "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld\n",
1468                              proc->pid, thread->pid, t->debug_id,
1469                              target_proc->pid, target_thread->pid,
1470                              (u64)tr->data.ptr.buffer,
1471                              (u64)tr->data.ptr.offsets,
1472                              (u64)tr->data_size, (u64)tr->offsets_size);
1473         else
1474                 binder_debug(BINDER_DEBUG_TRANSACTION,
1475                              "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld\n",
1476                              proc->pid, thread->pid, t->debug_id,
1477                              target_proc->pid, target_node->debug_id,
1478                              (u64)tr->data.ptr.buffer,
1479                              (u64)tr->data.ptr.offsets,
1480                              (u64)tr->data_size, (u64)tr->offsets_size);
1481
1482         if (!reply && !(tr->flags & TF_ONE_WAY))
1483                 t->from = thread;
1484         else
1485                 t->from = NULL;
1486         t->sender_euid = task_euid(proc->tsk);
1487         t->to_proc = target_proc;
1488         t->to_thread = target_thread;
1489         t->code = tr->code;
1490         t->flags = tr->flags;
1491         t->priority = task_nice(current);
1492
1493         trace_binder_transaction(reply, t, target_node);
1494
1495         t->buffer = binder_alloc_buf(target_proc, tr->data_size,
1496                 tr->offsets_size, !reply && (t->flags & TF_ONE_WAY));
1497         if (t->buffer == NULL) {
1498                 return_error = BR_FAILED_REPLY;
1499                 goto err_binder_alloc_buf_failed;
1500         }
1501         t->buffer->allow_user_free = 0;
1502         t->buffer->debug_id = t->debug_id;
1503         t->buffer->transaction = t;
1504         t->buffer->target_node = target_node;
1505         trace_binder_transaction_alloc_buf(t->buffer);
1506         if (target_node)
1507                 binder_inc_node(target_node, 1, 0, NULL);
1508
1509         offp = (binder_size_t *)(t->buffer->data +
1510                                  ALIGN(tr->data_size, sizeof(void *)));
1511
1512         if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t)
1513                            tr->data.ptr.buffer, tr->data_size)) {
1514                 binder_user_error("%d:%d got transaction with invalid data ptr\n",
1515                                 proc->pid, thread->pid);
1516                 return_error = BR_FAILED_REPLY;
1517                 goto err_copy_data_failed;
1518         }
1519         if (copy_from_user(offp, (const void __user *)(uintptr_t)
1520                            tr->data.ptr.offsets, tr->offsets_size)) {
1521                 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
1522                                 proc->pid, thread->pid);
1523                 return_error = BR_FAILED_REPLY;
1524                 goto err_copy_data_failed;
1525         }
1526         if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
1527                 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
1528                                 proc->pid, thread->pid, (u64)tr->offsets_size);
1529                 return_error = BR_FAILED_REPLY;
1530                 goto err_bad_offset;
1531         }
1532         off_end = (void *)offp + tr->offsets_size;
1533         off_min = 0;
1534         for (; offp < off_end; offp++) {
1535                 struct flat_binder_object *fp;
1536
1537                 if (*offp > t->buffer->data_size - sizeof(*fp) ||
1538                     *offp < off_min ||
1539                     t->buffer->data_size < sizeof(*fp) ||
1540                     !IS_ALIGNED(*offp, sizeof(u32))) {
1541                         binder_user_error("%d:%d got transaction with invalid offset, %lld (min %lld, max %lld)\n",
1542                                           proc->pid, thread->pid, (u64)*offp,
1543                                           (u64)off_min,
1544                                           (u64)(t->buffer->data_size -
1545                                           sizeof(*fp)));
1546                         return_error = BR_FAILED_REPLY;
1547                         goto err_bad_offset;
1548                 }
1549                 fp = (struct flat_binder_object *)(t->buffer->data + *offp);
1550                 off_min = *offp + sizeof(struct flat_binder_object);
1551                 switch (fp->type) {
1552                 case BINDER_TYPE_BINDER:
1553                 case BINDER_TYPE_WEAK_BINDER: {
1554                         struct binder_ref *ref;
1555                         struct binder_node *node = binder_get_node(proc, fp->binder);
1556
1557                         if (node == NULL) {
1558                                 node = binder_new_node(proc, fp->binder, fp->cookie);
1559                                 if (node == NULL) {
1560                                         return_error = BR_FAILED_REPLY;
1561                                         goto err_binder_new_node_failed;
1562                                 }
1563                                 node->min_priority = fp->flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
1564                                 node->accept_fds = !!(fp->flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
1565                         }
1566                         if (fp->cookie != node->cookie) {
1567                                 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
1568                                         proc->pid, thread->pid,
1569                                         (u64)fp->binder, node->debug_id,
1570                                         (u64)fp->cookie, (u64)node->cookie);
1571                                 return_error = BR_FAILED_REPLY;
1572                                 goto err_binder_get_ref_for_node_failed;
1573                         }
1574                         if (security_binder_transfer_binder(proc->tsk,
1575                                                             target_proc->tsk)) {
1576                                 return_error = BR_FAILED_REPLY;
1577                                 goto err_binder_get_ref_for_node_failed;
1578                         }
1579                         ref = binder_get_ref_for_node(target_proc, node);
1580                         if (ref == NULL) {
1581                                 return_error = BR_FAILED_REPLY;
1582                                 goto err_binder_get_ref_for_node_failed;
1583                         }
1584                         if (fp->type == BINDER_TYPE_BINDER)
1585                                 fp->type = BINDER_TYPE_HANDLE;
1586                         else
1587                                 fp->type = BINDER_TYPE_WEAK_HANDLE;
1588                         fp->binder = 0;
1589                         fp->handle = ref->desc;
1590                         fp->cookie = 0;
1591                         binder_inc_ref(ref, fp->type == BINDER_TYPE_HANDLE,
1592                                        &thread->todo);
1593
1594                         trace_binder_transaction_node_to_ref(t, node, ref);
1595                         binder_debug(BINDER_DEBUG_TRANSACTION,
1596                                      "        node %d u%016llx -> ref %d desc %d\n",
1597                                      node->debug_id, (u64)node->ptr,
1598                                      ref->debug_id, ref->desc);
1599                 } break;
1600                 case BINDER_TYPE_HANDLE:
1601                 case BINDER_TYPE_WEAK_HANDLE: {
1602                         struct binder_ref *ref;
1603
1604                         ref = binder_get_ref(proc, fp->handle,
1605                                              fp->type == BINDER_TYPE_HANDLE);
1606
1607                         if (ref == NULL) {
1608                                 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
1609                                                 proc->pid,
1610                                                 thread->pid, fp->handle);
1611                                 return_error = BR_FAILED_REPLY;
1612                                 goto err_binder_get_ref_failed;
1613                         }
1614                         if (security_binder_transfer_binder(proc->tsk,
1615                                                             target_proc->tsk)) {
1616                                 return_error = BR_FAILED_REPLY;
1617                                 goto err_binder_get_ref_failed;
1618                         }
1619                         if (ref->node->proc == target_proc) {
1620                                 if (fp->type == BINDER_TYPE_HANDLE)
1621                                         fp->type = BINDER_TYPE_BINDER;
1622                                 else
1623                                         fp->type = BINDER_TYPE_WEAK_BINDER;
1624                                 fp->binder = ref->node->ptr;
1625                                 fp->cookie = ref->node->cookie;
1626                                 binder_inc_node(ref->node, fp->type == BINDER_TYPE_BINDER, 0, NULL);
1627                                 trace_binder_transaction_ref_to_node(t, ref);
1628                                 binder_debug(BINDER_DEBUG_TRANSACTION,
1629                                              "        ref %d desc %d -> node %d u%016llx\n",
1630                                              ref->debug_id, ref->desc, ref->node->debug_id,
1631                                              (u64)ref->node->ptr);
1632                         } else {
1633                                 struct binder_ref *new_ref;
1634
1635                                 new_ref = binder_get_ref_for_node(target_proc, ref->node);
1636                                 if (new_ref == NULL) {
1637                                         return_error = BR_FAILED_REPLY;
1638                                         goto err_binder_get_ref_for_node_failed;
1639                                 }
1640                                 fp->binder = 0;
1641                                 fp->handle = new_ref->desc;
1642                                 fp->cookie = 0;
1643                                 binder_inc_ref(new_ref, fp->type == BINDER_TYPE_HANDLE, NULL);
1644                                 trace_binder_transaction_ref_to_ref(t, ref,
1645                                                                     new_ref);
1646                                 binder_debug(BINDER_DEBUG_TRANSACTION,
1647                                              "        ref %d desc %d -> ref %d desc %d (node %d)\n",
1648                                              ref->debug_id, ref->desc, new_ref->debug_id,
1649                                              new_ref->desc, ref->node->debug_id);
1650                         }
1651                 } break;
1652
1653                 case BINDER_TYPE_FD: {
1654                         int target_fd;
1655                         struct file *file;
1656
1657                         if (reply) {
1658                                 if (!(in_reply_to->flags & TF_ACCEPT_FDS)) {
1659                                         binder_user_error("%d:%d got reply with fd, %d, but target does not allow fds\n",
1660                                                 proc->pid, thread->pid, fp->handle);
1661                                         return_error = BR_FAILED_REPLY;
1662                                         goto err_fd_not_allowed;
1663                                 }
1664                         } else if (!target_node->accept_fds) {
1665                                 binder_user_error("%d:%d got transaction with fd, %d, but target does not allow fds\n",
1666                                         proc->pid, thread->pid, fp->handle);
1667                                 return_error = BR_FAILED_REPLY;
1668                                 goto err_fd_not_allowed;
1669                         }
1670
1671                         file = fget(fp->handle);
1672                         if (file == NULL) {
1673                                 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
1674                                         proc->pid, thread->pid, fp->handle);
1675                                 return_error = BR_FAILED_REPLY;
1676                                 goto err_fget_failed;
1677                         }
1678                         if (security_binder_transfer_file(proc->tsk,
1679                                                           target_proc->tsk,
1680                                                           file) < 0) {
1681                                 fput(file);
1682                                 return_error = BR_FAILED_REPLY;
1683                                 goto err_get_unused_fd_failed;
1684                         }
1685                         target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
1686                         if (target_fd < 0) {
1687                                 fput(file);
1688                                 return_error = BR_FAILED_REPLY;
1689                                 goto err_get_unused_fd_failed;
1690                         }
1691                         task_fd_install(target_proc, target_fd, file);
1692                         trace_binder_transaction_fd(t, fp->handle, target_fd);
1693                         binder_debug(BINDER_DEBUG_TRANSACTION,
1694                                      "        fd %d -> %d\n", fp->handle, target_fd);
1695                         /* TODO: fput? */
1696                         fp->binder = 0;
1697                         fp->handle = target_fd;
1698                 } break;
1699
1700                 default:
1701                         binder_user_error("%d:%d got transaction with invalid object type, %x\n",
1702                                 proc->pid, thread->pid, fp->type);
1703                         return_error = BR_FAILED_REPLY;
1704                         goto err_bad_object_type;
1705                 }
1706         }
1707         if (reply) {
1708                 BUG_ON(t->buffer->async_transaction != 0);
1709                 binder_pop_transaction(target_thread, in_reply_to);
1710         } else if (!(t->flags & TF_ONE_WAY)) {
1711                 BUG_ON(t->buffer->async_transaction != 0);
1712                 t->need_reply = 1;
1713                 t->from_parent = thread->transaction_stack;
1714                 thread->transaction_stack = t;
1715         } else {
1716                 BUG_ON(target_node == NULL);
1717                 BUG_ON(t->buffer->async_transaction != 1);
1718                 if (target_node->has_async_transaction) {
1719                         target_list = &target_node->async_todo;
1720                         target_wait = NULL;
1721                 } else
1722                         target_node->has_async_transaction = 1;
1723         }
1724         t->work.type = BINDER_WORK_TRANSACTION;
1725         list_add_tail(&t->work.entry, target_list);
1726         tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
1727         list_add_tail(&tcomplete->entry, &thread->todo);
1728         if (target_wait)
1729                 wake_up_interruptible(target_wait);
1730         return;
1731
1732 err_get_unused_fd_failed:
1733 err_fget_failed:
1734 err_fd_not_allowed:
1735 err_binder_get_ref_for_node_failed:
1736 err_binder_get_ref_failed:
1737 err_binder_new_node_failed:
1738 err_bad_object_type:
1739 err_bad_offset:
1740 err_copy_data_failed:
1741         trace_binder_transaction_failed_buffer_release(t->buffer);
1742         binder_transaction_buffer_release(target_proc, t->buffer, offp);
1743         t->buffer->transaction = NULL;
1744         binder_free_buf(target_proc, t->buffer);
1745 err_binder_alloc_buf_failed:
1746         kfree(tcomplete);
1747         binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
1748 err_alloc_tcomplete_failed:
1749         kfree(t);
1750         binder_stats_deleted(BINDER_STAT_TRANSACTION);
1751 err_alloc_t_failed:
1752 err_bad_call_stack:
1753 err_empty_call_stack:
1754 err_dead_binder:
1755 err_invalid_target_handle:
1756 err_no_context_mgr_node:
1757         binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1758                      "%d:%d transaction failed %d, size %lld-%lld\n",
1759                      proc->pid, thread->pid, return_error,
1760                      (u64)tr->data_size, (u64)tr->offsets_size);
1761
1762         {
1763                 struct binder_transaction_log_entry *fe;
1764
1765                 fe = binder_transaction_log_add(&binder_transaction_log_failed);
1766                 *fe = *e;
1767         }
1768
1769         BUG_ON(thread->return_error != BR_OK);
1770         if (in_reply_to) {
1771                 thread->return_error = BR_TRANSACTION_COMPLETE;
1772                 binder_send_failed_reply(in_reply_to, return_error);
1773         } else
1774                 thread->return_error = return_error;
1775 }
1776
1777 static int binder_thread_write(struct binder_proc *proc,
1778                         struct binder_thread *thread,
1779                         binder_uintptr_t binder_buffer, size_t size,
1780                         binder_size_t *consumed)
1781 {
1782         uint32_t cmd;
1783         void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
1784         void __user *ptr = buffer + *consumed;
1785         void __user *end = buffer + size;
1786
1787         while (ptr < end && thread->return_error == BR_OK) {
1788                 if (get_user(cmd, (uint32_t __user *)ptr))
1789                         return -EFAULT;
1790                 ptr += sizeof(uint32_t);
1791                 trace_binder_command(cmd);
1792                 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
1793                         binder_stats.bc[_IOC_NR(cmd)]++;
1794                         proc->stats.bc[_IOC_NR(cmd)]++;
1795                         thread->stats.bc[_IOC_NR(cmd)]++;
1796                 }
1797                 switch (cmd) {
1798                 case BC_INCREFS:
1799                 case BC_ACQUIRE:
1800                 case BC_RELEASE:
1801                 case BC_DECREFS: {
1802                         uint32_t target;
1803                         struct binder_ref *ref;
1804                         const char *debug_string;
1805
1806                         if (get_user(target, (uint32_t __user *)ptr))
1807                                 return -EFAULT;
1808                         ptr += sizeof(uint32_t);
1809                         if (target == 0 && binder_context_mgr_node &&
1810                             (cmd == BC_INCREFS || cmd == BC_ACQUIRE)) {
1811                                 ref = binder_get_ref_for_node(proc,
1812                                                binder_context_mgr_node);
1813                                 if (ref->desc != target) {
1814                                         binder_user_error("%d:%d tried to acquire reference to desc 0, got %d instead\n",
1815                                                 proc->pid, thread->pid,
1816                                                 ref->desc);
1817                                 }
1818                         } else
1819                                 ref = binder_get_ref(proc, target,
1820                                                      cmd == BC_ACQUIRE ||
1821                                                      cmd == BC_RELEASE);
1822                         if (ref == NULL) {
1823                                 binder_user_error("%d:%d refcount change on invalid ref %d\n",
1824                                         proc->pid, thread->pid, target);
1825                                 break;
1826                         }
1827                         switch (cmd) {
1828                         case BC_INCREFS:
1829                                 debug_string = "IncRefs";
1830                                 binder_inc_ref(ref, 0, NULL);
1831                                 break;
1832                         case BC_ACQUIRE:
1833                                 debug_string = "Acquire";
1834                                 binder_inc_ref(ref, 1, NULL);
1835                                 break;
1836                         case BC_RELEASE:
1837                                 debug_string = "Release";
1838                                 binder_dec_ref(ref, 1);
1839                                 break;
1840                         case BC_DECREFS:
1841                         default:
1842                                 debug_string = "DecRefs";
1843                                 binder_dec_ref(ref, 0);
1844                                 break;
1845                         }
1846                         binder_debug(BINDER_DEBUG_USER_REFS,
1847                                      "%d:%d %s ref %d desc %d s %d w %d for node %d\n",
1848                                      proc->pid, thread->pid, debug_string, ref->debug_id,
1849                                      ref->desc, ref->strong, ref->weak, ref->node->debug_id);
1850                         break;
1851                 }
1852                 case BC_INCREFS_DONE:
1853                 case BC_ACQUIRE_DONE: {
1854                         binder_uintptr_t node_ptr;
1855                         binder_uintptr_t cookie;
1856                         struct binder_node *node;
1857
1858                         if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
1859                                 return -EFAULT;
1860                         ptr += sizeof(binder_uintptr_t);
1861                         if (get_user(cookie, (binder_uintptr_t __user *)ptr))
1862                                 return -EFAULT;
1863                         ptr += sizeof(binder_uintptr_t);
1864                         node = binder_get_node(proc, node_ptr);
1865                         if (node == NULL) {
1866                                 binder_user_error("%d:%d %s u%016llx no match\n",
1867                                         proc->pid, thread->pid,
1868                                         cmd == BC_INCREFS_DONE ?
1869                                         "BC_INCREFS_DONE" :
1870                                         "BC_ACQUIRE_DONE",
1871                                         (u64)node_ptr);
1872                                 break;
1873                         }
1874                         if (cookie != node->cookie) {
1875                                 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
1876                                         proc->pid, thread->pid,
1877                                         cmd == BC_INCREFS_DONE ?
1878                                         "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
1879                                         (u64)node_ptr, node->debug_id,
1880                                         (u64)cookie, (u64)node->cookie);
1881                                 break;
1882                         }
1883                         if (cmd == BC_ACQUIRE_DONE) {
1884                                 if (node->pending_strong_ref == 0) {
1885                                         binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
1886                                                 proc->pid, thread->pid,
1887                                                 node->debug_id);
1888                                         break;
1889                                 }
1890                                 node->pending_strong_ref = 0;
1891                         } else {
1892                                 if (node->pending_weak_ref == 0) {
1893                                         binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
1894                                                 proc->pid, thread->pid,
1895                                                 node->debug_id);
1896                                         break;
1897                                 }
1898                                 node->pending_weak_ref = 0;
1899                         }
1900                         binder_dec_node(node, cmd == BC_ACQUIRE_DONE, 0);
1901                         binder_debug(BINDER_DEBUG_USER_REFS,
1902                                      "%d:%d %s node %d ls %d lw %d\n",
1903                                      proc->pid, thread->pid,
1904                                      cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
1905                                      node->debug_id, node->local_strong_refs, node->local_weak_refs);
1906                         break;
1907                 }
1908                 case BC_ATTEMPT_ACQUIRE:
1909                         pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
1910                         return -EINVAL;
1911                 case BC_ACQUIRE_RESULT:
1912                         pr_err("BC_ACQUIRE_RESULT not supported\n");
1913                         return -EINVAL;
1914
1915                 case BC_FREE_BUFFER: {
1916                         binder_uintptr_t data_ptr;
1917                         struct binder_buffer *buffer;
1918
1919                         if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
1920                                 return -EFAULT;
1921                         ptr += sizeof(binder_uintptr_t);
1922
1923                         buffer = binder_buffer_lookup(proc, data_ptr);
1924                         if (buffer == NULL) {
1925                                 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
1926                                         proc->pid, thread->pid, (u64)data_ptr);
1927                                 break;
1928                         }
1929                         if (!buffer->allow_user_free) {
1930                                 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
1931                                         proc->pid, thread->pid, (u64)data_ptr);
1932                                 break;
1933                         }
1934                         binder_debug(BINDER_DEBUG_FREE_BUFFER,
1935                                      "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
1936                                      proc->pid, thread->pid, (u64)data_ptr,
1937                                      buffer->debug_id,
1938                                      buffer->transaction ? "active" : "finished");
1939
1940                         if (buffer->transaction) {
1941                                 buffer->transaction->buffer = NULL;
1942                                 buffer->transaction = NULL;
1943                         }
1944                         if (buffer->async_transaction && buffer->target_node) {
1945                                 BUG_ON(!buffer->target_node->has_async_transaction);
1946                                 if (list_empty(&buffer->target_node->async_todo))
1947                                         buffer->target_node->has_async_transaction = 0;
1948                                 else
1949                                         list_move_tail(buffer->target_node->async_todo.next, &thread->todo);
1950                         }
1951                         trace_binder_transaction_buffer_release(buffer);
1952                         binder_transaction_buffer_release(proc, buffer, NULL);
1953                         binder_free_buf(proc, buffer);
1954                         break;
1955                 }
1956
1957                 case BC_TRANSACTION:
1958                 case BC_REPLY: {
1959                         struct binder_transaction_data tr;
1960
1961                         if (copy_from_user(&tr, ptr, sizeof(tr)))
1962                                 return -EFAULT;
1963                         ptr += sizeof(tr);
1964                         binder_transaction(proc, thread, &tr, cmd == BC_REPLY);
1965                         break;
1966                 }
1967
1968                 case BC_REGISTER_LOOPER:
1969                         binder_debug(BINDER_DEBUG_THREADS,
1970                                      "%d:%d BC_REGISTER_LOOPER\n",
1971                                      proc->pid, thread->pid);
1972                         if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
1973                                 thread->looper |= BINDER_LOOPER_STATE_INVALID;
1974                                 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
1975                                         proc->pid, thread->pid);
1976                         } else if (proc->requested_threads == 0) {
1977                                 thread->looper |= BINDER_LOOPER_STATE_INVALID;
1978                                 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
1979                                         proc->pid, thread->pid);
1980                         } else {
1981                                 proc->requested_threads--;
1982                                 proc->requested_threads_started++;
1983                         }
1984                         thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
1985                         break;
1986                 case BC_ENTER_LOOPER:
1987                         binder_debug(BINDER_DEBUG_THREADS,
1988                                      "%d:%d BC_ENTER_LOOPER\n",
1989                                      proc->pid, thread->pid);
1990                         if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
1991                                 thread->looper |= BINDER_LOOPER_STATE_INVALID;
1992                                 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
1993                                         proc->pid, thread->pid);
1994                         }
1995                         thread->looper |= BINDER_LOOPER_STATE_ENTERED;
1996                         break;
1997                 case BC_EXIT_LOOPER:
1998                         binder_debug(BINDER_DEBUG_THREADS,
1999                                      "%d:%d BC_EXIT_LOOPER\n",
2000                                      proc->pid, thread->pid);
2001                         thread->looper |= BINDER_LOOPER_STATE_EXITED;
2002                         break;
2003
2004                 case BC_REQUEST_DEATH_NOTIFICATION:
2005                 case BC_CLEAR_DEATH_NOTIFICATION: {
2006                         uint32_t target;
2007                         binder_uintptr_t cookie;
2008                         struct binder_ref *ref;
2009                         struct binder_ref_death *death;
2010
2011                         if (get_user(target, (uint32_t __user *)ptr))
2012                                 return -EFAULT;
2013                         ptr += sizeof(uint32_t);
2014                         if (get_user(cookie, (binder_uintptr_t __user *)ptr))
2015                                 return -EFAULT;
2016                         ptr += sizeof(binder_uintptr_t);
2017                         ref = binder_get_ref(proc, target, false);
2018                         if (ref == NULL) {
2019                                 binder_user_error("%d:%d %s invalid ref %d\n",
2020                                         proc->pid, thread->pid,
2021                                         cmd == BC_REQUEST_DEATH_NOTIFICATION ?
2022                                         "BC_REQUEST_DEATH_NOTIFICATION" :
2023                                         "BC_CLEAR_DEATH_NOTIFICATION",
2024                                         target);
2025                                 break;
2026                         }
2027
2028                         binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
2029                                      "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
2030                                      proc->pid, thread->pid,
2031                                      cmd == BC_REQUEST_DEATH_NOTIFICATION ?
2032                                      "BC_REQUEST_DEATH_NOTIFICATION" :
2033                                      "BC_CLEAR_DEATH_NOTIFICATION",
2034                                      (u64)cookie, ref->debug_id, ref->desc,
2035                                      ref->strong, ref->weak, ref->node->debug_id);
2036
2037                         if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
2038                                 if (ref->death) {
2039                                         binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
2040                                                 proc->pid, thread->pid);
2041                                         break;
2042                                 }
2043                                 death = kzalloc(sizeof(*death), GFP_KERNEL);
2044                                 if (death == NULL) {
2045                                         thread->return_error = BR_ERROR;
2046                                         binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2047                                                      "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
2048                                                      proc->pid, thread->pid);
2049                                         break;
2050                                 }
2051                                 binder_stats_created(BINDER_STAT_DEATH);
2052                                 INIT_LIST_HEAD(&death->work.entry);
2053                                 death->cookie = cookie;
2054                                 ref->death = death;
2055                                 if (ref->node->proc == NULL) {
2056                                         ref->death->work.type = BINDER_WORK_DEAD_BINDER;
2057                                         if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2058                                                 list_add_tail(&ref->death->work.entry, &thread->todo);
2059                                         } else {
2060                                                 list_add_tail(&ref->death->work.entry, &proc->todo);
2061                                                 wake_up_interruptible(&proc->wait);
2062                                         }
2063                                 }
2064                         } else {
2065                                 if (ref->death == NULL) {
2066                                         binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
2067                                                 proc->pid, thread->pid);
2068                                         break;
2069                                 }
2070                                 death = ref->death;
2071                                 if (death->cookie != cookie) {
2072                                         binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
2073                                                 proc->pid, thread->pid,
2074                                                 (u64)death->cookie,
2075                                                 (u64)cookie);
2076                                         break;
2077                                 }
2078                                 ref->death = NULL;
2079                                 if (list_empty(&death->work.entry)) {
2080                                         death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2081                                         if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2082                                                 list_add_tail(&death->work.entry, &thread->todo);
2083                                         } else {
2084                                                 list_add_tail(&death->work.entry, &proc->todo);
2085                                                 wake_up_interruptible(&proc->wait);
2086                                         }
2087                                 } else {
2088                                         BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
2089                                         death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
2090                                 }
2091                         }
2092                 } break;
2093                 case BC_DEAD_BINDER_DONE: {
2094                         struct binder_work *w;
2095                         binder_uintptr_t cookie;
2096                         struct binder_ref_death *death = NULL;
2097
2098                         if (get_user(cookie, (binder_uintptr_t __user *)ptr))
2099                                 return -EFAULT;
2100
2101                         ptr += sizeof(cookie);
2102                         list_for_each_entry(w, &proc->delivered_death, entry) {
2103                                 struct binder_ref_death *tmp_death = container_of(w, struct binder_ref_death, work);
2104
2105                                 if (tmp_death->cookie == cookie) {
2106                                         death = tmp_death;
2107                                         break;
2108                                 }
2109                         }
2110                         binder_debug(BINDER_DEBUG_DEAD_BINDER,
2111                                      "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n",
2112                                      proc->pid, thread->pid, (u64)cookie,
2113                                      death);
2114                         if (death == NULL) {
2115                                 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
2116                                         proc->pid, thread->pid, (u64)cookie);
2117                                 break;
2118                         }
2119
2120                         list_del_init(&death->work.entry);
2121                         if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
2122                                 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2123                                 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2124                                         list_add_tail(&death->work.entry, &thread->todo);
2125                                 } else {
2126                                         list_add_tail(&death->work.entry, &proc->todo);
2127                                         wake_up_interruptible(&proc->wait);
2128                                 }
2129                         }
2130                 } break;
2131
2132                 default:
2133                         pr_err("%d:%d unknown command %d\n",
2134                                proc->pid, thread->pid, cmd);
2135                         return -EINVAL;
2136                 }
2137                 *consumed = ptr - buffer;
2138         }
2139         return 0;
2140 }
2141
2142 static void binder_stat_br(struct binder_proc *proc,
2143                            struct binder_thread *thread, uint32_t cmd)
2144 {
2145         trace_binder_return(cmd);
2146         if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
2147                 binder_stats.br[_IOC_NR(cmd)]++;
2148                 proc->stats.br[_IOC_NR(cmd)]++;
2149                 thread->stats.br[_IOC_NR(cmd)]++;
2150         }
2151 }
2152
2153 static int binder_has_proc_work(struct binder_proc *proc,
2154                                 struct binder_thread *thread)
2155 {
2156         return !list_empty(&proc->todo) ||
2157                 (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2158 }
2159
2160 static int binder_has_thread_work(struct binder_thread *thread)
2161 {
2162         return !list_empty(&thread->todo) || thread->return_error != BR_OK ||
2163                 (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2164 }
2165
2166 static int binder_thread_read(struct binder_proc *proc,
2167                               struct binder_thread *thread,
2168                               binder_uintptr_t binder_buffer, size_t size,
2169                               binder_size_t *consumed, int non_block)
2170 {
2171         void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
2172         void __user *ptr = buffer + *consumed;
2173         void __user *end = buffer + size;
2174
2175         int ret = 0;
2176         int wait_for_proc_work;
2177
2178         if (*consumed == 0) {
2179                 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
2180                         return -EFAULT;
2181                 ptr += sizeof(uint32_t);
2182         }
2183
2184 retry:
2185         wait_for_proc_work = thread->transaction_stack == NULL &&
2186                                 list_empty(&thread->todo);
2187
2188         if (thread->return_error != BR_OK && ptr < end) {
2189                 if (thread->return_error2 != BR_OK) {
2190                         if (put_user(thread->return_error2, (uint32_t __user *)ptr))
2191                                 return -EFAULT;
2192                         ptr += sizeof(uint32_t);
2193                         binder_stat_br(proc, thread, thread->return_error2);
2194                         if (ptr == end)
2195                                 goto done;
2196                         thread->return_error2 = BR_OK;
2197                 }
2198                 if (put_user(thread->return_error, (uint32_t __user *)ptr))
2199                         return -EFAULT;
2200                 ptr += sizeof(uint32_t);
2201                 binder_stat_br(proc, thread, thread->return_error);
2202                 thread->return_error = BR_OK;
2203                 goto done;
2204         }
2205
2206
2207         thread->looper |= BINDER_LOOPER_STATE_WAITING;
2208         if (wait_for_proc_work)
2209                 proc->ready_threads++;
2210
2211         binder_unlock(__func__);
2212
2213         trace_binder_wait_for_work(wait_for_proc_work,
2214                                    !!thread->transaction_stack,
2215                                    !list_empty(&thread->todo));
2216         if (wait_for_proc_work) {
2217                 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2218                                         BINDER_LOOPER_STATE_ENTERED))) {
2219                         binder_user_error("%d:%d ERROR: Thread waiting for process work before calling BC_REGISTER_LOOPER or BC_ENTER_LOOPER (state %x)\n",
2220                                 proc->pid, thread->pid, thread->looper);
2221                         wait_event_interruptible(binder_user_error_wait,
2222                                                  binder_stop_on_user_error < 2);
2223                 }
2224                 binder_set_nice(proc->default_priority);
2225                 if (non_block) {
2226                         if (!binder_has_proc_work(proc, thread))
2227                                 ret = -EAGAIN;
2228                 } else
2229                         ret = wait_event_freezable_exclusive(proc->wait, binder_has_proc_work(proc, thread));
2230         } else {
2231                 if (non_block) {
2232                         if (!binder_has_thread_work(thread))
2233                                 ret = -EAGAIN;
2234                 } else
2235                         ret = wait_event_freezable(thread->wait, binder_has_thread_work(thread));
2236         }
2237
2238         binder_lock(__func__);
2239
2240         if (wait_for_proc_work)
2241                 proc->ready_threads--;
2242         thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
2243
2244         if (ret)
2245                 return ret;
2246
2247         while (1) {
2248                 uint32_t cmd;
2249                 struct binder_transaction_data tr;
2250                 struct binder_work *w;
2251                 struct binder_transaction *t = NULL;
2252
2253                 if (!list_empty(&thread->todo)) {
2254                         w = list_first_entry(&thread->todo, struct binder_work,
2255                                              entry);
2256                 } else if (!list_empty(&proc->todo) && wait_for_proc_work) {
2257                         w = list_first_entry(&proc->todo, struct binder_work,
2258                                              entry);
2259                 } else {
2260                         /* no data added */
2261                         if (ptr - buffer == 4 &&
2262                             !(thread->looper & BINDER_LOOPER_STATE_NEED_RETURN))
2263                                 goto retry;
2264                         break;
2265                 }
2266
2267                 if (end - ptr < sizeof(tr) + 4)
2268                         break;
2269
2270                 switch (w->type) {
2271                 case BINDER_WORK_TRANSACTION: {
2272                         t = container_of(w, struct binder_transaction, work);
2273                 } break;
2274                 case BINDER_WORK_TRANSACTION_COMPLETE: {
2275                         cmd = BR_TRANSACTION_COMPLETE;
2276                         if (put_user(cmd, (uint32_t __user *)ptr))
2277                                 return -EFAULT;
2278                         ptr += sizeof(uint32_t);
2279
2280                         binder_stat_br(proc, thread, cmd);
2281                         binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
2282                                      "%d:%d BR_TRANSACTION_COMPLETE\n",
2283                                      proc->pid, thread->pid);
2284
2285                         list_del(&w->entry);
2286                         kfree(w);
2287                         binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2288                 } break;
2289                 case BINDER_WORK_NODE: {
2290                         struct binder_node *node = container_of(w, struct binder_node, work);
2291                         uint32_t cmd = BR_NOOP;
2292                         const char *cmd_name;
2293                         int strong = node->internal_strong_refs || node->local_strong_refs;
2294                         int weak = !hlist_empty(&node->refs) || node->local_weak_refs || strong;
2295
2296                         if (weak && !node->has_weak_ref) {
2297                                 cmd = BR_INCREFS;
2298                                 cmd_name = "BR_INCREFS";
2299                                 node->has_weak_ref = 1;
2300                                 node->pending_weak_ref = 1;
2301                                 node->local_weak_refs++;
2302                         } else if (strong && !node->has_strong_ref) {
2303                                 cmd = BR_ACQUIRE;
2304                                 cmd_name = "BR_ACQUIRE";
2305                                 node->has_strong_ref = 1;
2306                                 node->pending_strong_ref = 1;
2307                                 node->local_strong_refs++;
2308                         } else if (!strong && node->has_strong_ref) {
2309                                 cmd = BR_RELEASE;
2310                                 cmd_name = "BR_RELEASE";
2311                                 node->has_strong_ref = 0;
2312                         } else if (!weak && node->has_weak_ref) {
2313                                 cmd = BR_DECREFS;
2314                                 cmd_name = "BR_DECREFS";
2315                                 node->has_weak_ref = 0;
2316                         }
2317                         if (cmd != BR_NOOP) {
2318                                 if (put_user(cmd, (uint32_t __user *)ptr))
2319                                         return -EFAULT;
2320                                 ptr += sizeof(uint32_t);
2321                                 if (put_user(node->ptr,
2322                                              (binder_uintptr_t __user *)ptr))
2323                                         return -EFAULT;
2324                                 ptr += sizeof(binder_uintptr_t);
2325                                 if (put_user(node->cookie,
2326                                              (binder_uintptr_t __user *)ptr))
2327                                         return -EFAULT;
2328                                 ptr += sizeof(binder_uintptr_t);
2329
2330                                 binder_stat_br(proc, thread, cmd);
2331                                 binder_debug(BINDER_DEBUG_USER_REFS,
2332                                              "%d:%d %s %d u%016llx c%016llx\n",
2333                                              proc->pid, thread->pid, cmd_name,
2334                                              node->debug_id,
2335                                              (u64)node->ptr, (u64)node->cookie);
2336                         } else {
2337                                 list_del_init(&w->entry);
2338                                 if (!weak && !strong) {
2339                                         binder_debug(BINDER_DEBUG_INTERNAL_REFS,
2340                                                      "%d:%d node %d u%016llx c%016llx deleted\n",
2341                                                      proc->pid, thread->pid,
2342                                                      node->debug_id,
2343                                                      (u64)node->ptr,
2344                                                      (u64)node->cookie);
2345                                         rb_erase(&node->rb_node, &proc->nodes);
2346                                         kfree(node);
2347                                         binder_stats_deleted(BINDER_STAT_NODE);
2348                                 } else {
2349                                         binder_debug(BINDER_DEBUG_INTERNAL_REFS,
2350                                                      "%d:%d node %d u%016llx c%016llx state unchanged\n",
2351                                                      proc->pid, thread->pid,
2352                                                      node->debug_id,
2353                                                      (u64)node->ptr,
2354                                                      (u64)node->cookie);
2355                                 }
2356                         }
2357                 } break;
2358                 case BINDER_WORK_DEAD_BINDER:
2359                 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2360                 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2361                         struct binder_ref_death *death;
2362                         uint32_t cmd;
2363
2364                         death = container_of(w, struct binder_ref_death, work);
2365                         if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
2366                                 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
2367                         else
2368                                 cmd = BR_DEAD_BINDER;
2369                         if (put_user(cmd, (uint32_t __user *)ptr))
2370                                 return -EFAULT;
2371                         ptr += sizeof(uint32_t);
2372                         if (put_user(death->cookie,
2373                                      (binder_uintptr_t __user *)ptr))
2374                                 return -EFAULT;
2375                         ptr += sizeof(binder_uintptr_t);
2376                         binder_stat_br(proc, thread, cmd);
2377                         binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
2378                                      "%d:%d %s %016llx\n",
2379                                       proc->pid, thread->pid,
2380                                       cmd == BR_DEAD_BINDER ?
2381                                       "BR_DEAD_BINDER" :
2382                                       "BR_CLEAR_DEATH_NOTIFICATION_DONE",
2383                                       (u64)death->cookie);
2384
2385                         if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
2386                                 list_del(&w->entry);
2387                                 kfree(death);
2388                                 binder_stats_deleted(BINDER_STAT_DEATH);
2389                         } else
2390                                 list_move(&w->entry, &proc->delivered_death);
2391                         if (cmd == BR_DEAD_BINDER)
2392                                 goto done; /* DEAD_BINDER notifications can cause transactions */
2393                 } break;
2394                 }
2395
2396                 if (!t)
2397                         continue;
2398
2399                 BUG_ON(t->buffer == NULL);
2400                 if (t->buffer->target_node) {
2401                         struct binder_node *target_node = t->buffer->target_node;
2402
2403                         tr.target.ptr = target_node->ptr;
2404                         tr.cookie =  target_node->cookie;
2405                         t->saved_priority = task_nice(current);
2406                         if (t->priority < target_node->min_priority &&
2407                             !(t->flags & TF_ONE_WAY))
2408                                 binder_set_nice(t->priority);
2409                         else if (!(t->flags & TF_ONE_WAY) ||
2410                                  t->saved_priority > target_node->min_priority)
2411                                 binder_set_nice(target_node->min_priority);
2412                         cmd = BR_TRANSACTION;
2413                 } else {
2414                         tr.target.ptr = 0;
2415                         tr.cookie = 0;
2416                         cmd = BR_REPLY;
2417                 }
2418                 tr.code = t->code;
2419                 tr.flags = t->flags;
2420                 tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
2421
2422                 if (t->from) {
2423                         struct task_struct *sender = t->from->proc->tsk;
2424
2425                         tr.sender_pid = task_tgid_nr_ns(sender,
2426                                                         task_active_pid_ns(current));
2427                 } else {
2428                         tr.sender_pid = 0;
2429                 }
2430
2431                 tr.data_size = t->buffer->data_size;
2432                 tr.offsets_size = t->buffer->offsets_size;
2433                 tr.data.ptr.buffer = (binder_uintptr_t)(
2434                                         (uintptr_t)t->buffer->data +
2435                                         proc->user_buffer_offset);
2436                 tr.data.ptr.offsets = tr.data.ptr.buffer +
2437                                         ALIGN(t->buffer->data_size,
2438                                             sizeof(void *));
2439
2440                 if (put_user(cmd, (uint32_t __user *)ptr))
2441                         return -EFAULT;
2442                 ptr += sizeof(uint32_t);
2443                 if (copy_to_user(ptr, &tr, sizeof(tr)))
2444                         return -EFAULT;
2445                 ptr += sizeof(tr);
2446
2447                 trace_binder_transaction_received(t);
2448                 binder_stat_br(proc, thread, cmd);
2449                 binder_debug(BINDER_DEBUG_TRANSACTION,
2450                              "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
2451                              proc->pid, thread->pid,
2452                              (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
2453                              "BR_REPLY",
2454                              t->debug_id, t->from ? t->from->proc->pid : 0,
2455                              t->from ? t->from->pid : 0, cmd,
2456                              t->buffer->data_size, t->buffer->offsets_size,
2457                              (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
2458
2459                 list_del(&t->work.entry);
2460                 t->buffer->allow_user_free = 1;
2461                 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
2462                         t->to_parent = thread->transaction_stack;
2463                         t->to_thread = thread;
2464                         thread->transaction_stack = t;
2465                 } else {
2466                         t->buffer->transaction = NULL;
2467                         kfree(t);
2468                         binder_stats_deleted(BINDER_STAT_TRANSACTION);
2469                 }
2470                 break;
2471         }
2472
2473 done:
2474
2475         *consumed = ptr - buffer;
2476         if (proc->requested_threads + proc->ready_threads == 0 &&
2477             proc->requested_threads_started < proc->max_threads &&
2478             (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2479              BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
2480              /*spawn a new thread if we leave this out */) {
2481                 proc->requested_threads++;
2482                 binder_debug(BINDER_DEBUG_THREADS,
2483                              "%d:%d BR_SPAWN_LOOPER\n",
2484                              proc->pid, thread->pid);
2485                 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
2486                         return -EFAULT;
2487                 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
2488         }
2489         return 0;
2490 }
2491
2492 static void binder_release_work(struct list_head *list)
2493 {
2494         struct binder_work *w;
2495
2496         while (!list_empty(list)) {
2497                 w = list_first_entry(list, struct binder_work, entry);
2498                 list_del_init(&w->entry);
2499                 switch (w->type) {
2500                 case BINDER_WORK_TRANSACTION: {
2501                         struct binder_transaction *t;
2502
2503                         t = container_of(w, struct binder_transaction, work);
2504                         if (t->buffer->target_node &&
2505                             !(t->flags & TF_ONE_WAY)) {
2506                                 binder_send_failed_reply(t, BR_DEAD_REPLY);
2507                         } else {
2508                                 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2509                                         "undelivered transaction %d\n",
2510                                         t->debug_id);
2511                                 t->buffer->transaction = NULL;
2512                                 kfree(t);
2513                                 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2514                         }
2515                 } break;
2516                 case BINDER_WORK_TRANSACTION_COMPLETE: {
2517                         binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2518                                 "undelivered TRANSACTION_COMPLETE\n");
2519                         kfree(w);
2520                         binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2521                 } break;
2522                 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2523                 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2524                         struct binder_ref_death *death;
2525
2526                         death = container_of(w, struct binder_ref_death, work);
2527                         binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2528                                 "undelivered death notification, %016llx\n",
2529                                 (u64)death->cookie);
2530                         kfree(death);
2531                         binder_stats_deleted(BINDER_STAT_DEATH);
2532                 } break;
2533                 default:
2534                         pr_err("unexpected work type, %d, not freed\n",
2535                                w->type);
2536                         break;
2537                 }
2538         }
2539
2540 }
2541
2542 static struct binder_thread *binder_get_thread(struct binder_proc *proc)
2543 {
2544         struct binder_thread *thread = NULL;
2545         struct rb_node *parent = NULL;
2546         struct rb_node **p = &proc->threads.rb_node;
2547
2548         while (*p) {
2549                 parent = *p;
2550                 thread = rb_entry(parent, struct binder_thread, rb_node);
2551
2552                 if (current->pid < thread->pid)
2553                         p = &(*p)->rb_left;
2554                 else if (current->pid > thread->pid)
2555                         p = &(*p)->rb_right;
2556                 else
2557                         break;
2558         }
2559         if (*p == NULL) {
2560                 thread = kzalloc(sizeof(*thread), GFP_KERNEL);
2561                 if (thread == NULL)
2562                         return NULL;
2563                 binder_stats_created(BINDER_STAT_THREAD);
2564                 thread->proc = proc;
2565                 thread->pid = current->pid;
2566                 init_waitqueue_head(&thread->wait);
2567                 INIT_LIST_HEAD(&thread->todo);
2568                 rb_link_node(&thread->rb_node, parent, p);
2569                 rb_insert_color(&thread->rb_node, &proc->threads);
2570                 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
2571                 thread->return_error = BR_OK;
2572                 thread->return_error2 = BR_OK;
2573         }
2574         return thread;
2575 }
2576
2577 static int binder_free_thread(struct binder_proc *proc,
2578                               struct binder_thread *thread)
2579 {
2580         struct binder_transaction *t;
2581         struct binder_transaction *send_reply = NULL;
2582         int active_transactions = 0;
2583
2584         rb_erase(&thread->rb_node, &proc->threads);
2585         t = thread->transaction_stack;
2586         if (t && t->to_thread == thread)
2587                 send_reply = t;
2588         while (t) {
2589                 active_transactions++;
2590                 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2591                              "release %d:%d transaction %d %s, still active\n",
2592                               proc->pid, thread->pid,
2593                              t->debug_id,
2594                              (t->to_thread == thread) ? "in" : "out");
2595
2596                 if (t->to_thread == thread) {
2597                         t->to_proc = NULL;
2598                         t->to_thread = NULL;
2599                         if (t->buffer) {
2600                                 t->buffer->transaction = NULL;
2601                                 t->buffer = NULL;
2602                         }
2603                         t = t->to_parent;
2604                 } else if (t->from == thread) {
2605                         t->from = NULL;
2606                         t = t->from_parent;
2607                 } else
2608                         BUG();
2609         }
2610         if (send_reply)
2611                 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
2612         binder_release_work(&thread->todo);
2613         kfree(thread);
2614         binder_stats_deleted(BINDER_STAT_THREAD);
2615         return active_transactions;
2616 }
2617
2618 static unsigned int binder_poll(struct file *filp,
2619                                 struct poll_table_struct *wait)
2620 {
2621         struct binder_proc *proc = filp->private_data;
2622         struct binder_thread *thread = NULL;
2623         int wait_for_proc_work;
2624
2625         binder_lock(__func__);
2626
2627         thread = binder_get_thread(proc);
2628
2629         wait_for_proc_work = thread->transaction_stack == NULL &&
2630                 list_empty(&thread->todo) && thread->return_error == BR_OK;
2631
2632         binder_unlock(__func__);
2633
2634         if (wait_for_proc_work) {
2635                 if (binder_has_proc_work(proc, thread))
2636                         return POLLIN;
2637                 poll_wait(filp, &proc->wait, wait);
2638                 if (binder_has_proc_work(proc, thread))
2639                         return POLLIN;
2640         } else {
2641                 if (binder_has_thread_work(thread))
2642                         return POLLIN;
2643                 poll_wait(filp, &thread->wait, wait);
2644                 if (binder_has_thread_work(thread))
2645                         return POLLIN;
2646         }
2647         return 0;
2648 }
2649
2650 static int binder_ioctl_write_read(struct file *filp,
2651                                 unsigned int cmd, unsigned long arg,
2652                                 struct binder_thread *thread)
2653 {
2654         int ret = 0;
2655         struct binder_proc *proc = filp->private_data;
2656         unsigned int size = _IOC_SIZE(cmd);
2657         void __user *ubuf = (void __user *)arg;
2658         struct binder_write_read bwr;
2659
2660         if (size != sizeof(struct binder_write_read)) {
2661                 ret = -EINVAL;
2662                 goto out;
2663         }
2664         if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
2665                 ret = -EFAULT;
2666                 goto out;
2667         }
2668         binder_debug(BINDER_DEBUG_READ_WRITE,
2669                      "%d:%d write %lld at %016llx, read %lld at %016llx\n",
2670                      proc->pid, thread->pid,
2671                      (u64)bwr.write_size, (u64)bwr.write_buffer,
2672                      (u64)bwr.read_size, (u64)bwr.read_buffer);
2673
2674         if (bwr.write_size > 0) {
2675                 ret = binder_thread_write(proc, thread,
2676                                           bwr.write_buffer,
2677                                           bwr.write_size,
2678                                           &bwr.write_consumed);
2679                 trace_binder_write_done(ret);
2680                 if (ret < 0) {
2681                         bwr.read_consumed = 0;
2682                         if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2683                                 ret = -EFAULT;
2684                         goto out;
2685                 }
2686         }
2687         if (bwr.read_size > 0) {
2688                 ret = binder_thread_read(proc, thread, bwr.read_buffer,
2689                                          bwr.read_size,
2690                                          &bwr.read_consumed,
2691                                          filp->f_flags & O_NONBLOCK);
2692                 trace_binder_read_done(ret);
2693                 if (!list_empty(&proc->todo))
2694                         wake_up_interruptible(&proc->wait);
2695                 if (ret < 0) {
2696                         if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2697                                 ret = -EFAULT;
2698                         goto out;
2699                 }
2700         }
2701         binder_debug(BINDER_DEBUG_READ_WRITE,
2702                      "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
2703                      proc->pid, thread->pid,
2704                      (u64)bwr.write_consumed, (u64)bwr.write_size,
2705                      (u64)bwr.read_consumed, (u64)bwr.read_size);
2706         if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
2707                 ret = -EFAULT;
2708                 goto out;
2709         }
2710 out:
2711         return ret;
2712 }
2713
2714 static int binder_ioctl_set_ctx_mgr(struct file *filp)
2715 {
2716         int ret = 0;
2717         struct binder_proc *proc = filp->private_data;
2718         kuid_t curr_euid = current_euid();
2719
2720         if (binder_context_mgr_node != NULL) {
2721                 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
2722                 ret = -EBUSY;
2723                 goto out;
2724         }
2725         ret = security_binder_set_context_mgr(proc->tsk);
2726         if (ret < 0)
2727                 goto out;
2728         if (uid_valid(binder_context_mgr_uid)) {
2729                 if (!uid_eq(binder_context_mgr_uid, curr_euid)) {
2730                         pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
2731                                from_kuid(&init_user_ns, curr_euid),
2732                                from_kuid(&init_user_ns,
2733                                         binder_context_mgr_uid));
2734                         ret = -EPERM;
2735                         goto out;
2736                 }
2737         } else {
2738                 binder_context_mgr_uid = curr_euid;
2739         }
2740         binder_context_mgr_node = binder_new_node(proc, 0, 0);
2741         if (binder_context_mgr_node == NULL) {
2742                 ret = -ENOMEM;
2743                 goto out;
2744         }
2745         binder_context_mgr_node->local_weak_refs++;
2746         binder_context_mgr_node->local_strong_refs++;
2747         binder_context_mgr_node->has_strong_ref = 1;
2748         binder_context_mgr_node->has_weak_ref = 1;
2749 out:
2750         return ret;
2751 }
2752
2753 static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2754 {
2755         int ret;
2756         struct binder_proc *proc = filp->private_data;
2757         struct binder_thread *thread;
2758         unsigned int size = _IOC_SIZE(cmd);
2759         void __user *ubuf = (void __user *)arg;
2760
2761         /*pr_info("binder_ioctl: %d:%d %x %lx\n",
2762                         proc->pid, current->pid, cmd, arg);*/
2763
2764         trace_binder_ioctl(cmd, arg);
2765
2766         ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2767         if (ret)
2768                 goto err_unlocked;
2769
2770         binder_lock(__func__);
2771         thread = binder_get_thread(proc);
2772         if (thread == NULL) {
2773                 ret = -ENOMEM;
2774                 goto err;
2775         }
2776
2777         switch (cmd) {
2778         case BINDER_WRITE_READ:
2779                 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
2780                 if (ret)
2781                         goto err;
2782                 break;
2783         case BINDER_SET_MAX_THREADS:
2784                 if (copy_from_user(&proc->max_threads, ubuf, sizeof(proc->max_threads))) {
2785                         ret = -EINVAL;
2786                         goto err;
2787                 }
2788                 break;
2789         case BINDER_SET_CONTEXT_MGR:
2790                 ret = binder_ioctl_set_ctx_mgr(filp);
2791                 if (ret)
2792                         goto err;
2793                 break;
2794         case BINDER_THREAD_EXIT:
2795                 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
2796                              proc->pid, thread->pid);
2797                 binder_free_thread(proc, thread);
2798                 thread = NULL;
2799                 break;
2800         case BINDER_VERSION: {
2801                 struct binder_version __user *ver = ubuf;
2802
2803                 if (size != sizeof(struct binder_version)) {
2804                         ret = -EINVAL;
2805                         goto err;
2806                 }
2807                 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
2808                              &ver->protocol_version)) {
2809                         ret = -EINVAL;
2810                         goto err;
2811                 }
2812                 break;
2813         }
2814         default:
2815                 ret = -EINVAL;
2816                 goto err;
2817         }
2818         ret = 0;
2819 err:
2820         if (thread)
2821                 thread->looper &= ~BINDER_LOOPER_STATE_NEED_RETURN;
2822         binder_unlock(__func__);
2823         wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2824         if (ret && ret != -ERESTARTSYS)
2825                 pr_info("%d:%d ioctl %x %lx returned %d\n", proc->pid, current->pid, cmd, arg, ret);
2826 err_unlocked:
2827         trace_binder_ioctl_done(ret);
2828         return ret;
2829 }
2830
2831 static void binder_vma_open(struct vm_area_struct *vma)
2832 {
2833         struct binder_proc *proc = vma->vm_private_data;
2834
2835         binder_debug(BINDER_DEBUG_OPEN_CLOSE,
2836                      "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
2837                      proc->pid, vma->vm_start, vma->vm_end,
2838                      (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2839                      (unsigned long)pgprot_val(vma->vm_page_prot));
2840 }
2841
2842 static void binder_vma_close(struct vm_area_struct *vma)
2843 {
2844         struct binder_proc *proc = vma->vm_private_data;
2845
2846         binder_debug(BINDER_DEBUG_OPEN_CLOSE,
2847                      "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
2848                      proc->pid, vma->vm_start, vma->vm_end,
2849                      (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2850                      (unsigned long)pgprot_val(vma->vm_page_prot));
2851         proc->vma = NULL;
2852         proc->vma_vm_mm = NULL;
2853         binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
2854 }
2855
2856 static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2857 {
2858         return VM_FAULT_SIGBUS;
2859 }
2860
2861 static const struct vm_operations_struct binder_vm_ops = {
2862         .open = binder_vma_open,
2863         .close = binder_vma_close,
2864         .fault = binder_vm_fault,
2865 };
2866
2867 static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
2868 {
2869         int ret;
2870         struct vm_struct *area;
2871         struct binder_proc *proc = filp->private_data;
2872         const char *failure_string;
2873         struct binder_buffer *buffer;
2874
2875         if (proc->tsk != current)
2876                 return -EINVAL;
2877
2878         if ((vma->vm_end - vma->vm_start) > SZ_4M)
2879                 vma->vm_end = vma->vm_start + SZ_4M;
2880
2881         binder_debug(BINDER_DEBUG_OPEN_CLOSE,
2882                      "binder_mmap: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
2883                      proc->pid, vma->vm_start, vma->vm_end,
2884                      (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2885                      (unsigned long)pgprot_val(vma->vm_page_prot));
2886
2887         if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
2888                 ret = -EPERM;
2889                 failure_string = "bad vm_flags";
2890                 goto err_bad_arg;
2891         }
2892         vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
2893
2894         mutex_lock(&binder_mmap_lock);
2895         if (proc->buffer) {
2896                 ret = -EBUSY;
2897                 failure_string = "already mapped";
2898                 goto err_already_mapped;
2899         }
2900
2901         area = get_vm_area(vma->vm_end - vma->vm_start, VM_IOREMAP);
2902         if (area == NULL) {
2903                 ret = -ENOMEM;
2904                 failure_string = "get_vm_area";
2905                 goto err_get_vm_area_failed;
2906         }
2907         proc->buffer = area->addr;
2908         proc->user_buffer_offset = vma->vm_start - (uintptr_t)proc->buffer;
2909         mutex_unlock(&binder_mmap_lock);
2910
2911 #ifdef CONFIG_CPU_CACHE_VIPT
2912         if (cache_is_vipt_aliasing()) {
2913                 while (CACHE_COLOUR((vma->vm_start ^ (uint32_t)proc->buffer))) {
2914                         pr_info("binder_mmap: %d %lx-%lx maps %p bad alignment\n", proc->pid, vma->vm_start, vma->vm_end, proc->buffer);
2915                         vma->vm_start += PAGE_SIZE;
2916                 }
2917         }
2918 #endif
2919         proc->pages = kzalloc(sizeof(proc->pages[0]) * ((vma->vm_end - vma->vm_start) / PAGE_SIZE), GFP_KERNEL);
2920         if (proc->pages == NULL) {
2921                 ret = -ENOMEM;
2922                 failure_string = "alloc page array";
2923                 goto err_alloc_pages_failed;
2924         }
2925         proc->buffer_size = vma->vm_end - vma->vm_start;
2926
2927         vma->vm_ops = &binder_vm_ops;
2928         vma->vm_private_data = proc;
2929
2930         if (binder_update_page_range(proc, 1, proc->buffer, proc->buffer + PAGE_SIZE, vma)) {
2931                 ret = -ENOMEM;
2932                 failure_string = "alloc small buf";
2933                 goto err_alloc_small_buf_failed;
2934         }
2935         buffer = proc->buffer;
2936         INIT_LIST_HEAD(&proc->buffers);
2937         list_add(&buffer->entry, &proc->buffers);
2938         buffer->free = 1;
2939         binder_insert_free_buffer(proc, buffer);
2940         proc->free_async_space = proc->buffer_size / 2;
2941         barrier();
2942         proc->files = get_files_struct(current);
2943         proc->vma = vma;
2944         proc->vma_vm_mm = vma->vm_mm;
2945
2946         /*pr_info("binder_mmap: %d %lx-%lx maps %p\n",
2947                  proc->pid, vma->vm_start, vma->vm_end, proc->buffer);*/
2948         return 0;
2949
2950 err_alloc_small_buf_failed:
2951         kfree(proc->pages);
2952         proc->pages = NULL;
2953 err_alloc_pages_failed:
2954         mutex_lock(&binder_mmap_lock);
2955         vfree(proc->buffer);
2956         proc->buffer = NULL;
2957 err_get_vm_area_failed:
2958 err_already_mapped:
2959         mutex_unlock(&binder_mmap_lock);
2960 err_bad_arg:
2961         pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
2962                proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
2963         return ret;
2964 }
2965
2966 static int binder_open(struct inode *nodp, struct file *filp)
2967 {
2968         struct binder_proc *proc;
2969
2970         binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
2971                      current->group_leader->pid, current->pid);
2972
2973         proc = kzalloc(sizeof(*proc), GFP_KERNEL);
2974         if (proc == NULL)
2975                 return -ENOMEM;
2976         get_task_struct(current);
2977         proc->tsk = current;
2978         INIT_LIST_HEAD(&proc->todo);
2979         init_waitqueue_head(&proc->wait);
2980         proc->default_priority = task_nice(current);
2981
2982         binder_lock(__func__);
2983
2984         binder_stats_created(BINDER_STAT_PROC);
2985         hlist_add_head(&proc->proc_node, &binder_procs);
2986         proc->pid = current->group_leader->pid;
2987         INIT_LIST_HEAD(&proc->delivered_death);
2988         filp->private_data = proc;
2989
2990         binder_unlock(__func__);
2991
2992         if (binder_debugfs_dir_entry_proc) {
2993                 char strbuf[11];
2994
2995                 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
2996                 proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
2997                         binder_debugfs_dir_entry_proc, proc, &binder_proc_fops);
2998         }
2999
3000         return 0;
3001 }
3002
3003 static int binder_flush(struct file *filp, fl_owner_t id)
3004 {
3005         struct binder_proc *proc = filp->private_data;
3006
3007         binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
3008
3009         return 0;
3010 }
3011
3012 static void binder_deferred_flush(struct binder_proc *proc)
3013 {
3014         struct rb_node *n;
3015         int wake_count = 0;
3016
3017         for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
3018                 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
3019
3020                 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
3021                 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
3022                         wake_up_interruptible(&thread->wait);
3023                         wake_count++;
3024                 }
3025         }
3026         wake_up_interruptible_all(&proc->wait);
3027
3028         binder_debug(BINDER_DEBUG_OPEN_CLOSE,
3029                      "binder_flush: %d woke %d threads\n", proc->pid,
3030                      wake_count);
3031 }
3032
3033 static int binder_release(struct inode *nodp, struct file *filp)
3034 {
3035         struct binder_proc *proc = filp->private_data;
3036
3037         debugfs_remove(proc->debugfs_entry);
3038         binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
3039
3040         return 0;
3041 }
3042
3043 static int binder_node_release(struct binder_node *node, int refs)
3044 {
3045         struct binder_ref *ref;
3046         int death = 0;
3047
3048         list_del_init(&node->work.entry);
3049         binder_release_work(&node->async_todo);
3050
3051         if (hlist_empty(&node->refs)) {
3052                 kfree(node);
3053                 binder_stats_deleted(BINDER_STAT_NODE);
3054
3055                 return refs;
3056         }
3057
3058         node->proc = NULL;
3059         node->local_strong_refs = 0;
3060         node->local_weak_refs = 0;
3061         hlist_add_head(&node->dead_node, &binder_dead_nodes);
3062
3063         hlist_for_each_entry(ref, &node->refs, node_entry) {
3064                 refs++;
3065
3066                 if (!ref->death)
3067                         continue;
3068
3069                 death++;
3070
3071                 if (list_empty(&ref->death->work.entry)) {
3072                         ref->death->work.type = BINDER_WORK_DEAD_BINDER;
3073                         list_add_tail(&ref->death->work.entry,
3074                                       &ref->proc->todo);
3075                         wake_up_interruptible(&ref->proc->wait);
3076                 } else
3077                         BUG();
3078         }
3079
3080         binder_debug(BINDER_DEBUG_DEAD_BINDER,
3081                      "node %d now dead, refs %d, death %d\n",
3082                      node->debug_id, refs, death);
3083
3084         return refs;
3085 }
3086
3087 static void binder_deferred_release(struct binder_proc *proc)
3088 {
3089         struct binder_transaction *t;
3090         struct rb_node *n;
3091         int threads, nodes, incoming_refs, outgoing_refs, buffers,
3092                 active_transactions, page_count;
3093
3094         BUG_ON(proc->vma);
3095         BUG_ON(proc->files);
3096
3097         hlist_del(&proc->proc_node);
3098
3099         if (binder_context_mgr_node && binder_context_mgr_node->proc == proc) {
3100                 binder_debug(BINDER_DEBUG_DEAD_BINDER,
3101                              "%s: %d context_mgr_node gone\n",
3102                              __func__, proc->pid);
3103                 binder_context_mgr_node = NULL;
3104         }
3105
3106         threads = 0;
3107         active_transactions = 0;
3108         while ((n = rb_first(&proc->threads))) {
3109                 struct binder_thread *thread;
3110
3111                 thread = rb_entry(n, struct binder_thread, rb_node);
3112                 threads++;
3113                 active_transactions += binder_free_thread(proc, thread);
3114         }
3115
3116         nodes = 0;
3117         incoming_refs = 0;
3118         while ((n = rb_first(&proc->nodes))) {
3119                 struct binder_node *node;
3120
3121                 node = rb_entry(n, struct binder_node, rb_node);
3122                 nodes++;
3123                 rb_erase(&node->rb_node, &proc->nodes);
3124                 incoming_refs = binder_node_release(node, incoming_refs);
3125         }
3126
3127         outgoing_refs = 0;
3128         while ((n = rb_first(&proc->refs_by_desc))) {
3129                 struct binder_ref *ref;
3130
3131                 ref = rb_entry(n, struct binder_ref, rb_node_desc);
3132                 outgoing_refs++;
3133                 binder_delete_ref(ref);
3134         }
3135
3136         binder_release_work(&proc->todo);
3137         binder_release_work(&proc->delivered_death);
3138
3139         buffers = 0;
3140         while ((n = rb_first(&proc->allocated_buffers))) {
3141                 struct binder_buffer *buffer;
3142
3143                 buffer = rb_entry(n, struct binder_buffer, rb_node);
3144
3145                 t = buffer->transaction;
3146                 if (t) {
3147                         t->buffer = NULL;
3148                         buffer->transaction = NULL;
3149                         pr_err("release proc %d, transaction %d, not freed\n",
3150                                proc->pid, t->debug_id);
3151                         /*BUG();*/
3152                 }
3153
3154                 binder_free_buf(proc, buffer);
3155                 buffers++;
3156         }
3157
3158         binder_stats_deleted(BINDER_STAT_PROC);
3159
3160         page_count = 0;
3161         if (proc->pages) {
3162                 int i;
3163
3164                 for (i = 0; i < proc->buffer_size / PAGE_SIZE; i++) {
3165                         void *page_addr;
3166
3167                         if (!proc->pages[i])
3168                                 continue;
3169
3170                         page_addr = proc->buffer + i * PAGE_SIZE;
3171                         binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
3172                                      "%s: %d: page %d at %p not freed\n",
3173                                      __func__, proc->pid, i, page_addr);
3174                         unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
3175                         __free_page(proc->pages[i]);
3176                         page_count++;
3177                 }
3178                 kfree(proc->pages);
3179                 vfree(proc->buffer);
3180         }
3181
3182         put_task_struct(proc->tsk);
3183
3184         binder_debug(BINDER_DEBUG_OPEN_CLOSE,
3185                      "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d, buffers %d, pages %d\n",
3186                      __func__, proc->pid, threads, nodes, incoming_refs,
3187                      outgoing_refs, active_transactions, buffers, page_count);
3188
3189         kfree(proc);
3190 }
3191
3192 static void binder_deferred_func(struct work_struct *work)
3193 {
3194         struct binder_proc *proc;
3195         struct files_struct *files;
3196
3197         int defer;
3198
3199         do {
3200                 binder_lock(__func__);
3201                 mutex_lock(&binder_deferred_lock);
3202                 if (!hlist_empty(&binder_deferred_list)) {
3203                         proc = hlist_entry(binder_deferred_list.first,
3204                                         struct binder_proc, deferred_work_node);
3205                         hlist_del_init(&proc->deferred_work_node);
3206                         defer = proc->deferred_work;
3207                         proc->deferred_work = 0;
3208                 } else {
3209                         proc = NULL;
3210                         defer = 0;
3211                 }
3212                 mutex_unlock(&binder_deferred_lock);
3213
3214                 files = NULL;
3215                 if (defer & BINDER_DEFERRED_PUT_FILES) {
3216                         files = proc->files;
3217                         if (files)
3218                                 proc->files = NULL;
3219                 }
3220
3221                 if (defer & BINDER_DEFERRED_FLUSH)
3222                         binder_deferred_flush(proc);
3223
3224                 if (defer & BINDER_DEFERRED_RELEASE)
3225                         binder_deferred_release(proc); /* frees proc */
3226
3227                 binder_unlock(__func__);
3228                 if (files)
3229                         put_files_struct(files);
3230         } while (proc);
3231 }
3232 static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
3233
3234 static void
3235 binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
3236 {
3237         mutex_lock(&binder_deferred_lock);
3238         proc->deferred_work |= defer;
3239         if (hlist_unhashed(&proc->deferred_work_node)) {
3240                 hlist_add_head(&proc->deferred_work_node,
3241                                 &binder_deferred_list);
3242                 queue_work(binder_deferred_workqueue, &binder_deferred_work);
3243         }
3244         mutex_unlock(&binder_deferred_lock);
3245 }
3246
3247 static void print_binder_transaction(struct seq_file *m, const char *prefix,
3248                                      struct binder_transaction *t)
3249 {
3250         seq_printf(m,
3251                    "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %ld r%d",
3252                    prefix, t->debug_id, t,
3253                    t->from ? t->from->proc->pid : 0,
3254                    t->from ? t->from->pid : 0,
3255                    t->to_proc ? t->to_proc->pid : 0,
3256                    t->to_thread ? t->to_thread->pid : 0,
3257                    t->code, t->flags, t->priority, t->need_reply);
3258         if (t->buffer == NULL) {
3259                 seq_puts(m, " buffer free\n");
3260                 return;
3261         }
3262         if (t->buffer->target_node)
3263                 seq_printf(m, " node %d",
3264                            t->buffer->target_node->debug_id);
3265         seq_printf(m, " size %zd:%zd data %p\n",
3266                    t->buffer->data_size, t->buffer->offsets_size,
3267                    t->buffer->data);
3268 }
3269
3270 static void print_binder_buffer(struct seq_file *m, const char *prefix,
3271                                 struct binder_buffer *buffer)
3272 {
3273         seq_printf(m, "%s %d: %p size %zd:%zd %s\n",
3274                    prefix, buffer->debug_id, buffer->data,
3275                    buffer->data_size, buffer->offsets_size,
3276                    buffer->transaction ? "active" : "delivered");
3277 }
3278
3279 static void print_binder_work(struct seq_file *m, const char *prefix,
3280                               const char *transaction_prefix,
3281                               struct binder_work *w)
3282 {
3283         struct binder_node *node;
3284         struct binder_transaction *t;
3285
3286         switch (w->type) {
3287         case BINDER_WORK_TRANSACTION:
3288                 t = container_of(w, struct binder_transaction, work);
3289                 print_binder_transaction(m, transaction_prefix, t);
3290                 break;
3291         case BINDER_WORK_TRANSACTION_COMPLETE:
3292                 seq_printf(m, "%stransaction complete\n", prefix);
3293                 break;
3294         case BINDER_WORK_NODE:
3295                 node = container_of(w, struct binder_node, work);
3296                 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
3297                            prefix, node->debug_id,
3298                            (u64)node->ptr, (u64)node->cookie);
3299                 break;
3300         case BINDER_WORK_DEAD_BINDER:
3301                 seq_printf(m, "%shas dead binder\n", prefix);
3302                 break;
3303         case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
3304                 seq_printf(m, "%shas cleared dead binder\n", prefix);
3305                 break;
3306         case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
3307                 seq_printf(m, "%shas cleared death notification\n", prefix);
3308                 break;
3309         default:
3310                 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
3311                 break;
3312         }
3313 }
3314
3315 static void print_binder_thread(struct seq_file *m,
3316                                 struct binder_thread *thread,
3317                                 int print_always)
3318 {
3319         struct binder_transaction *t;
3320         struct binder_work *w;
3321         size_t start_pos = m->count;
3322         size_t header_pos;
3323
3324         seq_printf(m, "  thread %d: l %02x\n", thread->pid, thread->looper);
3325         header_pos = m->count;
3326         t = thread->transaction_stack;
3327         while (t) {
3328                 if (t->from == thread) {
3329                         print_binder_transaction(m,
3330                                                  "    outgoing transaction", t);
3331                         t = t->from_parent;
3332                 } else if (t->to_thread == thread) {
3333                         print_binder_transaction(m,
3334                                                  "    incoming transaction", t);
3335                         t = t->to_parent;
3336                 } else {
3337                         print_binder_transaction(m, "    bad transaction", t);
3338                         t = NULL;
3339                 }
3340         }
3341         list_for_each_entry(w, &thread->todo, entry) {
3342                 print_binder_work(m, "    ", "    pending transaction", w);
3343         }
3344         if (!print_always && m->count == header_pos)
3345                 m->count = start_pos;
3346 }
3347
3348 static void print_binder_node(struct seq_file *m, struct binder_node *node)
3349 {
3350         struct binder_ref *ref;
3351         struct binder_work *w;
3352         int count;
3353
3354         count = 0;
3355         hlist_for_each_entry(ref, &node->refs, node_entry)
3356                 count++;
3357
3358         seq_printf(m, "  node %d: u%016llx c%016llx hs %d hw %d ls %d lw %d is %d iw %d",
3359                    node->debug_id, (u64)node->ptr, (u64)node->cookie,
3360                    node->has_strong_ref, node->has_weak_ref,
3361                    node->local_strong_refs, node->local_weak_refs,
3362                    node->internal_strong_refs, count);
3363         if (count) {
3364                 seq_puts(m, " proc");
3365                 hlist_for_each_entry(ref, &node->refs, node_entry)
3366                         seq_printf(m, " %d", ref->proc->pid);
3367         }
3368         seq_puts(m, "\n");
3369         list_for_each_entry(w, &node->async_todo, entry)
3370                 print_binder_work(m, "    ",
3371                                   "    pending async transaction", w);
3372 }
3373
3374 static void print_binder_ref(struct seq_file *m, struct binder_ref *ref)
3375 {
3376         seq_printf(m, "  ref %d: desc %d %snode %d s %d w %d d %p\n",
3377                    ref->debug_id, ref->desc, ref->node->proc ? "" : "dead ",
3378                    ref->node->debug_id, ref->strong, ref->weak, ref->death);
3379 }
3380
3381 static void print_binder_proc(struct seq_file *m,
3382                               struct binder_proc *proc, int print_all)
3383 {
3384         struct binder_work *w;
3385         struct rb_node *n;
3386         size_t start_pos = m->count;
3387         size_t header_pos;
3388
3389         seq_printf(m, "proc %d\n", proc->pid);
3390         header_pos = m->count;
3391
3392         for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3393                 print_binder_thread(m, rb_entry(n, struct binder_thread,
3394                                                 rb_node), print_all);
3395         for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
3396                 struct binder_node *node = rb_entry(n, struct binder_node,
3397                                                     rb_node);
3398                 if (print_all || node->has_async_transaction)
3399                         print_binder_node(m, node);
3400         }
3401         if (print_all) {
3402                 for (n = rb_first(&proc->refs_by_desc);
3403                      n != NULL;
3404                      n = rb_next(n))
3405                         print_binder_ref(m, rb_entry(n, struct binder_ref,
3406                                                      rb_node_desc));
3407         }
3408         for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
3409                 print_binder_buffer(m, "  buffer",
3410                                     rb_entry(n, struct binder_buffer, rb_node));
3411         list_for_each_entry(w, &proc->todo, entry)
3412                 print_binder_work(m, "  ", "  pending transaction", w);
3413         list_for_each_entry(w, &proc->delivered_death, entry) {
3414                 seq_puts(m, "  has delivered dead binder\n");
3415                 break;
3416         }
3417         if (!print_all && m->count == header_pos)
3418                 m->count = start_pos;
3419 }
3420
3421 static const char * const binder_return_strings[] = {
3422         "BR_ERROR",
3423         "BR_OK",
3424         "BR_TRANSACTION",
3425         "BR_REPLY",
3426         "BR_ACQUIRE_RESULT",
3427         "BR_DEAD_REPLY",
3428         "BR_TRANSACTION_COMPLETE",
3429         "BR_INCREFS",
3430         "BR_ACQUIRE",
3431         "BR_RELEASE",
3432         "BR_DECREFS",
3433         "BR_ATTEMPT_ACQUIRE",
3434         "BR_NOOP",
3435         "BR_SPAWN_LOOPER",
3436         "BR_FINISHED",
3437         "BR_DEAD_BINDER",
3438         "BR_CLEAR_DEATH_NOTIFICATION_DONE",
3439         "BR_FAILED_REPLY"
3440 };
3441
3442 static const char * const binder_command_strings[] = {
3443         "BC_TRANSACTION",
3444         "BC_REPLY",
3445         "BC_ACQUIRE_RESULT",
3446         "BC_FREE_BUFFER",
3447         "BC_INCREFS",
3448         "BC_ACQUIRE",
3449         "BC_RELEASE",
3450         "BC_DECREFS",
3451         "BC_INCREFS_DONE",
3452         "BC_ACQUIRE_DONE",
3453         "BC_ATTEMPT_ACQUIRE",
3454         "BC_REGISTER_LOOPER",
3455         "BC_ENTER_LOOPER",
3456         "BC_EXIT_LOOPER",
3457         "BC_REQUEST_DEATH_NOTIFICATION",
3458         "BC_CLEAR_DEATH_NOTIFICATION",
3459         "BC_DEAD_BINDER_DONE"
3460 };
3461
3462 static const char * const binder_objstat_strings[] = {
3463         "proc",
3464         "thread",
3465         "node",
3466         "ref",
3467         "death",
3468         "transaction",
3469         "transaction_complete"
3470 };
3471
3472 static void print_binder_stats(struct seq_file *m, const char *prefix,
3473                                struct binder_stats *stats)
3474 {
3475         int i;
3476
3477         BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
3478                      ARRAY_SIZE(binder_command_strings));
3479         for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
3480                 if (stats->bc[i])
3481                         seq_printf(m, "%s%s: %d\n", prefix,
3482                                    binder_command_strings[i], stats->bc[i]);
3483         }
3484
3485         BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
3486                      ARRAY_SIZE(binder_return_strings));
3487         for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
3488                 if (stats->br[i])
3489                         seq_printf(m, "%s%s: %d\n", prefix,
3490                                    binder_return_strings[i], stats->br[i]);
3491         }
3492
3493         BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
3494                      ARRAY_SIZE(binder_objstat_strings));
3495         BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
3496                      ARRAY_SIZE(stats->obj_deleted));
3497         for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
3498                 if (stats->obj_created[i] || stats->obj_deleted[i])
3499                         seq_printf(m, "%s%s: active %d total %d\n", prefix,
3500                                 binder_objstat_strings[i],
3501                                 stats->obj_created[i] - stats->obj_deleted[i],
3502                                 stats->obj_created[i]);
3503         }
3504 }
3505
3506 static void print_binder_proc_stats(struct seq_file *m,
3507                                     struct binder_proc *proc)
3508 {
3509         struct binder_work *w;
3510         struct rb_node *n;
3511         int count, strong, weak;
3512
3513         seq_printf(m, "proc %d\n", proc->pid);
3514         count = 0;
3515         for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3516                 count++;
3517         seq_printf(m, "  threads: %d\n", count);
3518         seq_printf(m, "  requested threads: %d+%d/%d\n"
3519                         "  ready threads %d\n"
3520                         "  free async space %zd\n", proc->requested_threads,
3521                         proc->requested_threads_started, proc->max_threads,
3522                         proc->ready_threads, proc->free_async_space);
3523         count = 0;
3524         for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
3525                 count++;
3526         seq_printf(m, "  nodes: %d\n", count);
3527         count = 0;
3528         strong = 0;
3529         weak = 0;
3530         for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
3531                 struct binder_ref *ref = rb_entry(n, struct binder_ref,
3532                                                   rb_node_desc);
3533                 count++;
3534                 strong += ref->strong;
3535                 weak += ref->weak;
3536         }
3537         seq_printf(m, "  refs: %d s %d w %d\n", count, strong, weak);
3538
3539         count = 0;
3540         for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
3541                 count++;
3542         seq_printf(m, "  buffers: %d\n", count);
3543
3544         count = 0;
3545         list_for_each_entry(w, &proc->todo, entry) {
3546                 switch (w->type) {
3547                 case BINDER_WORK_TRANSACTION:
3548                         count++;
3549                         break;
3550                 default:
3551                         break;
3552                 }
3553         }
3554         seq_printf(m, "  pending transactions: %d\n", count);
3555
3556         print_binder_stats(m, "  ", &proc->stats);
3557 }
3558
3559
3560 static int binder_state_show(struct seq_file *m, void *unused)
3561 {
3562         struct binder_proc *proc;
3563         struct binder_node *node;
3564         int do_lock = !binder_debug_no_lock;
3565
3566         if (do_lock)
3567                 binder_lock(__func__);
3568
3569         seq_puts(m, "binder state:\n");
3570
3571         if (!hlist_empty(&binder_dead_nodes))
3572                 seq_puts(m, "dead nodes:\n");
3573         hlist_for_each_entry(node, &binder_dead_nodes, dead_node)
3574                 print_binder_node(m, node);
3575
3576         hlist_for_each_entry(proc, &binder_procs, proc_node)
3577                 print_binder_proc(m, proc, 1);
3578         if (do_lock)
3579                 binder_unlock(__func__);
3580         return 0;
3581 }
3582
3583 static int binder_stats_show(struct seq_file *m, void *unused)
3584 {
3585         struct binder_proc *proc;
3586         int do_lock = !binder_debug_no_lock;
3587
3588         if (do_lock)
3589                 binder_lock(__func__);
3590
3591         seq_puts(m, "binder stats:\n");
3592
3593         print_binder_stats(m, "", &binder_stats);
3594
3595         hlist_for_each_entry(proc, &binder_procs, proc_node)
3596                 print_binder_proc_stats(m, proc);
3597         if (do_lock)
3598                 binder_unlock(__func__);
3599         return 0;
3600 }
3601
3602 static int binder_transactions_show(struct seq_file *m, void *unused)
3603 {
3604         struct binder_proc *proc;
3605         int do_lock = !binder_debug_no_lock;
3606
3607         if (do_lock)
3608                 binder_lock(__func__);
3609
3610         seq_puts(m, "binder transactions:\n");
3611         hlist_for_each_entry(proc, &binder_procs, proc_node)
3612                 print_binder_proc(m, proc, 0);
3613         if (do_lock)
3614                 binder_unlock(__func__);
3615         return 0;
3616 }
3617
3618 static int binder_proc_show(struct seq_file *m, void *unused)
3619 {
3620         struct binder_proc *itr;
3621         struct binder_proc *proc = m->private;
3622         int do_lock = !binder_debug_no_lock;
3623         bool valid_proc = false;
3624
3625         if (do_lock)
3626                 binder_lock(__func__);
3627
3628         hlist_for_each_entry(itr, &binder_procs, proc_node) {
3629                 if (itr == proc) {
3630                         valid_proc = true;
3631                         break;
3632                 }
3633         }
3634         if (valid_proc) {
3635                 seq_puts(m, "binder proc state:\n");
3636                 print_binder_proc(m, proc, 1);
3637         }
3638         if (do_lock)
3639                 binder_unlock(__func__);
3640         return 0;
3641 }
3642
3643 static void print_binder_transaction_log_entry(struct seq_file *m,
3644                                         struct binder_transaction_log_entry *e)
3645 {
3646         seq_printf(m,
3647                    "%d: %s from %d:%d to %d:%d node %d handle %d size %d:%d\n",
3648                    e->debug_id, (e->call_type == 2) ? "reply" :
3649                    ((e->call_type == 1) ? "async" : "call "), e->from_proc,
3650                    e->from_thread, e->to_proc, e->to_thread, e->to_node,
3651                    e->target_handle, e->data_size, e->offsets_size);
3652 }
3653
3654 static int binder_transaction_log_show(struct seq_file *m, void *unused)
3655 {
3656         struct binder_transaction_log *log = m->private;
3657         int i;
3658
3659         if (log->full) {
3660                 for (i = log->next; i < ARRAY_SIZE(log->entry); i++)
3661                         print_binder_transaction_log_entry(m, &log->entry[i]);
3662         }
3663         for (i = 0; i < log->next; i++)
3664                 print_binder_transaction_log_entry(m, &log->entry[i]);
3665         return 0;
3666 }
3667
3668 static const struct file_operations binder_fops = {
3669         .owner = THIS_MODULE,
3670         .poll = binder_poll,
3671         .unlocked_ioctl = binder_ioctl,
3672         .compat_ioctl = binder_ioctl,
3673         .mmap = binder_mmap,
3674         .open = binder_open,
3675         .flush = binder_flush,
3676         .release = binder_release,
3677 };
3678
3679 static struct miscdevice binder_miscdev = {
3680         .minor = MISC_DYNAMIC_MINOR,
3681         .name = "binder",
3682         .fops = &binder_fops
3683 };
3684
3685 BINDER_DEBUG_ENTRY(state);
3686 BINDER_DEBUG_ENTRY(stats);
3687 BINDER_DEBUG_ENTRY(transactions);
3688 BINDER_DEBUG_ENTRY(transaction_log);
3689
3690 static int __init binder_init(void)
3691 {
3692         int ret;
3693
3694         binder_deferred_workqueue = create_singlethread_workqueue("binder");
3695         if (!binder_deferred_workqueue)
3696                 return -ENOMEM;
3697
3698         binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
3699         if (binder_debugfs_dir_entry_root)
3700                 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
3701                                                  binder_debugfs_dir_entry_root);
3702         ret = misc_register(&binder_miscdev);
3703         if (binder_debugfs_dir_entry_root) {
3704                 debugfs_create_file("state",
3705                                     S_IRUGO,
3706                                     binder_debugfs_dir_entry_root,
3707                                     NULL,
3708                                     &binder_state_fops);
3709                 debugfs_create_file("stats",
3710                                     S_IRUGO,
3711                                     binder_debugfs_dir_entry_root,
3712                                     NULL,
3713                                     &binder_stats_fops);
3714                 debugfs_create_file("transactions",
3715                                     S_IRUGO,
3716                                     binder_debugfs_dir_entry_root,
3717                                     NULL,
3718                                     &binder_transactions_fops);
3719                 debugfs_create_file("transaction_log",
3720                                     S_IRUGO,
3721                                     binder_debugfs_dir_entry_root,
3722                                     &binder_transaction_log,
3723                                     &binder_transaction_log_fops);
3724                 debugfs_create_file("failed_transaction_log",
3725                                     S_IRUGO,
3726                                     binder_debugfs_dir_entry_root,
3727                                     &binder_transaction_log_failed,
3728                                     &binder_transaction_log_fops);
3729         }
3730         return ret;
3731 }
3732
3733 device_initcall(binder_init);
3734
3735 #define CREATE_TRACE_POINTS
3736 #include "binder_trace.h"
3737
3738 MODULE_LICENSE("GPL v2");