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