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