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