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