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