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