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