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