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