blob: ce0e4d317d241a298865550b1c9cc0fa0d4d47e5 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/* binder.c
2 *
3 * Android IPC Subsystem
4 *
5 * Copyright (C) 2007-2008 Google, Inc.
6 *
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
18/*
19 * Locking overview
20 *
21 * There are 3 main spinlocks which must be acquired in the
22 * order shown:
23 *
24 * 1) proc->outer_lock : protects binder_ref
25 * binder_proc_lock() and binder_proc_unlock() are
26 * used to acq/rel.
27 * 2) node->lock : protects most fields of binder_node.
28 * binder_node_lock() and binder_node_unlock() are
29 * used to acq/rel
30 * 3) proc->inner_lock : protects the thread and node lists
31 * (proc->threads, proc->waiting_threads, proc->nodes)
32 * and all todo lists associated with the binder_proc
33 * (proc->todo, thread->todo, proc->delivered_death and
34 * node->async_todo), as well as thread->transaction_stack
35 * binder_inner_proc_lock() and binder_inner_proc_unlock()
36 * are used to acq/rel
37 *
38 * Any lock under procA must never be nested under any lock at the same
39 * level or below on procB.
40 *
41 * Functions that require a lock held on entry indicate which lock
42 * in the suffix of the function name:
43 *
44 * foo_olocked() : requires node->outer_lock
45 * foo_nlocked() : requires node->lock
46 * foo_ilocked() : requires proc->inner_lock
47 * foo_oilocked(): requires proc->outer_lock and proc->inner_lock
48 * foo_nilocked(): requires node->lock and proc->inner_lock
49 * ...
50 */
51
52#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
53
54#include <linux/fdtable.h>
55#include <linux/file.h>
56#include <linux/freezer.h>
57#include <linux/fs.h>
58#include <linux/list.h>
59#include <linux/miscdevice.h>
60#include <linux/module.h>
61#include <linux/mutex.h>
62#include <linux/nsproxy.h>
63#include <linux/poll.h>
64#include <linux/debugfs.h>
65#include <linux/rbtree.h>
66#include <linux/sched/signal.h>
67#include <linux/sched/mm.h>
68#include <linux/seq_file.h>
69#include <linux/uaccess.h>
70#include <linux/pid_namespace.h>
71#include <linux/security.h>
72#include <linux/spinlock.h>
73#include <linux/ratelimit.h>
74
75#include <uapi/linux/android/binder.h>
76
77#include <asm/cacheflush.h>
78
79#include "binder_alloc.h"
80#include "binder_trace.h"
81
82static HLIST_HEAD(binder_deferred_list);
83static DEFINE_MUTEX(binder_deferred_lock);
84
85static HLIST_HEAD(binder_devices);
86static HLIST_HEAD(binder_procs);
87static DEFINE_MUTEX(binder_procs_lock);
88
89static HLIST_HEAD(binder_dead_nodes);
90static DEFINE_SPINLOCK(binder_dead_nodes_lock);
91
92static struct dentry *binder_debugfs_dir_entry_root;
93static struct dentry *binder_debugfs_dir_entry_proc;
94static atomic_t binder_last_id;
95
96#define BINDER_DEBUG_ENTRY(name) \
97static int binder_##name##_open(struct inode *inode, struct file *file) \
98{ \
99 return single_open(file, binder_##name##_show, inode->i_private); \
100} \
101\
102static const struct file_operations binder_##name##_fops = { \
103 .owner = THIS_MODULE, \
104 .open = binder_##name##_open, \
105 .read = seq_read, \
106 .llseek = seq_lseek, \
107 .release = single_release, \
108}
109
110static int binder_proc_show(struct seq_file *m, void *unused);
111BINDER_DEBUG_ENTRY(proc);
112
113/* This is only defined in include/asm-arm/sizes.h */
114#ifndef SZ_1K
115#define SZ_1K 0x400
116#endif
117
118#ifndef SZ_4M
119#define SZ_4M 0x400000
120#endif
121
122#define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
123
124enum {
125 BINDER_DEBUG_USER_ERROR = 1U << 0,
126 BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1,
127 BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2,
128 BINDER_DEBUG_OPEN_CLOSE = 1U << 3,
129 BINDER_DEBUG_DEAD_BINDER = 1U << 4,
130 BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5,
131 BINDER_DEBUG_READ_WRITE = 1U << 6,
132 BINDER_DEBUG_USER_REFS = 1U << 7,
133 BINDER_DEBUG_THREADS = 1U << 8,
134 BINDER_DEBUG_TRANSACTION = 1U << 9,
135 BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10,
136 BINDER_DEBUG_FREE_BUFFER = 1U << 11,
137 BINDER_DEBUG_INTERNAL_REFS = 1U << 12,
138 BINDER_DEBUG_PRIORITY_CAP = 1U << 13,
139 BINDER_DEBUG_SPINLOCKS = 1U << 14,
140};
141static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR |
142 BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION;
143module_param_named(debug_mask, binder_debug_mask, uint, 0644);
144
145static char *binder_devices_param = CONFIG_ANDROID_BINDER_DEVICES;
146module_param_named(devices, binder_devices_param, charp, 0444);
147
148static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
149static int binder_stop_on_user_error;
150
151static int binder_set_stop_on_user_error(const char *val,
152 const struct kernel_param *kp)
153{
154 int ret;
155
156 ret = param_set_int(val, kp);
157 if (binder_stop_on_user_error < 2)
158 wake_up(&binder_user_error_wait);
159 return ret;
160}
161module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
162 param_get_int, &binder_stop_on_user_error, 0644);
163
164#define binder_debug(mask, x...) \
165 do { \
166 if (binder_debug_mask & mask) \
167 pr_info_ratelimited(x); \
168 } while (0)
169
170#define binder_user_error(x...) \
171 do { \
172 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
173 pr_info_ratelimited(x); \
174 if (binder_stop_on_user_error) \
175 binder_stop_on_user_error = 2; \
176 } while (0)
177
178#define to_flat_binder_object(hdr) \
179 container_of(hdr, struct flat_binder_object, hdr)
180
181#define to_binder_fd_object(hdr) container_of(hdr, struct binder_fd_object, hdr)
182
183#define to_binder_buffer_object(hdr) \
184 container_of(hdr, struct binder_buffer_object, hdr)
185
186#define to_binder_fd_array_object(hdr) \
187 container_of(hdr, struct binder_fd_array_object, hdr)
188
189enum binder_stat_types {
190 BINDER_STAT_PROC,
191 BINDER_STAT_THREAD,
192 BINDER_STAT_NODE,
193 BINDER_STAT_REF,
194 BINDER_STAT_DEATH,
195 BINDER_STAT_TRANSACTION,
196 BINDER_STAT_TRANSACTION_COMPLETE,
197 BINDER_STAT_COUNT
198};
199
200struct binder_stats {
201 atomic_t br[_IOC_NR(BR_FAILED_REPLY) + 1];
202 atomic_t bc[_IOC_NR(BC_REPLY_SG) + 1];
203 atomic_t obj_created[BINDER_STAT_COUNT];
204 atomic_t obj_deleted[BINDER_STAT_COUNT];
205};
206
207static struct binder_stats binder_stats;
208
209static inline void binder_stats_deleted(enum binder_stat_types type)
210{
211 atomic_inc(&binder_stats.obj_deleted[type]);
212}
213
214static inline void binder_stats_created(enum binder_stat_types type)
215{
216 atomic_inc(&binder_stats.obj_created[type]);
217}
218
219struct binder_transaction_log_entry {
220 int debug_id;
221 int debug_id_done;
222 int call_type;
223 int from_proc;
224 int from_thread;
225 int target_handle;
226 int to_proc;
227 int to_thread;
228 int to_node;
229 int data_size;
230 int offsets_size;
231 int return_error_line;
232 uint32_t return_error;
233 uint32_t return_error_param;
234 const char *context_name;
235};
236struct binder_transaction_log {
237 atomic_t cur;
238 bool full;
239 struct binder_transaction_log_entry entry[32];
240};
241static struct binder_transaction_log binder_transaction_log;
242static struct binder_transaction_log binder_transaction_log_failed;
243
244static struct binder_transaction_log_entry *binder_transaction_log_add(
245 struct binder_transaction_log *log)
246{
247 struct binder_transaction_log_entry *e;
248 unsigned int cur = atomic_inc_return(&log->cur);
249
250 if (cur >= ARRAY_SIZE(log->entry))
251 log->full = true;
252 e = &log->entry[cur % ARRAY_SIZE(log->entry)];
253 WRITE_ONCE(e->debug_id_done, 0);
254 /*
255 * write-barrier to synchronize access to e->debug_id_done.
256 * We make sure the initialized 0 value is seen before
257 * memset() other fields are zeroed by memset.
258 */
259 smp_wmb();
260 memset(e, 0, sizeof(*e));
261 return e;
262}
263
264struct binder_context {
265 struct binder_node *binder_context_mgr_node;
266 struct mutex context_mgr_node_lock;
267
268 kuid_t binder_context_mgr_uid;
269 const char *name;
270};
271
272struct binder_device {
273 struct hlist_node hlist;
274 struct miscdevice miscdev;
275 struct binder_context context;
276};
277
278/**
279 * struct binder_work - work enqueued on a worklist
280 * @entry: node enqueued on list
281 * @type: type of work to be performed
282 *
283 * There are separate work lists for proc, thread, and node (async).
284 */
285struct binder_work {
286 struct list_head entry;
287
288 enum {
289 BINDER_WORK_TRANSACTION = 1,
290 BINDER_WORK_TRANSACTION_COMPLETE,
291 BINDER_WORK_RETURN_ERROR,
292 BINDER_WORK_NODE,
293 BINDER_WORK_DEAD_BINDER,
294 BINDER_WORK_DEAD_BINDER_AND_CLEAR,
295 BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
296 } type;
297};
298
299struct binder_error {
300 struct binder_work work;
301 uint32_t cmd;
302};
303
304/**
305 * struct binder_node - binder node bookkeeping
306 * @debug_id: unique ID for debugging
307 * (invariant after initialized)
308 * @lock: lock for node fields
309 * @work: worklist element for node work
310 * (protected by @proc->inner_lock)
311 * @rb_node: element for proc->nodes tree
312 * (protected by @proc->inner_lock)
313 * @dead_node: element for binder_dead_nodes list
314 * (protected by binder_dead_nodes_lock)
315 * @proc: binder_proc that owns this node
316 * (invariant after initialized)
317 * @refs: list of references on this node
318 * (protected by @lock)
319 * @internal_strong_refs: used to take strong references when
320 * initiating a transaction
321 * (protected by @proc->inner_lock if @proc
322 * and by @lock)
323 * @local_weak_refs: weak user refs from local process
324 * (protected by @proc->inner_lock if @proc
325 * and by @lock)
326 * @local_strong_refs: strong user refs from local process
327 * (protected by @proc->inner_lock if @proc
328 * and by @lock)
329 * @tmp_refs: temporary kernel refs
330 * (protected by @proc->inner_lock while @proc
331 * is valid, and by binder_dead_nodes_lock
332 * if @proc is NULL. During inc/dec and node release
333 * it is also protected by @lock to provide safety
334 * as the node dies and @proc becomes NULL)
335 * @ptr: userspace pointer for node
336 * (invariant, no lock needed)
337 * @cookie: userspace cookie for node
338 * (invariant, no lock needed)
339 * @has_strong_ref: userspace notified of strong ref
340 * (protected by @proc->inner_lock if @proc
341 * and by @lock)
342 * @pending_strong_ref: userspace has acked notification of strong ref
343 * (protected by @proc->inner_lock if @proc
344 * and by @lock)
345 * @has_weak_ref: userspace notified of weak ref
346 * (protected by @proc->inner_lock if @proc
347 * and by @lock)
348 * @pending_weak_ref: userspace has acked notification of weak ref
349 * (protected by @proc->inner_lock if @proc
350 * and by @lock)
351 * @has_async_transaction: async transaction to node in progress
352 * (protected by @lock)
353 * @accept_fds: file descriptor operations supported for node
354 * (invariant after initialized)
355 * @min_priority: minimum scheduling priority
356 * (invariant after initialized)
357 * @async_todo: list of async work items
358 * (protected by @proc->inner_lock)
359 *
360 * Bookkeeping structure for binder nodes.
361 */
362struct binder_node {
363 int debug_id;
364 spinlock_t lock;
365 struct binder_work work;
366 union {
367 struct rb_node rb_node;
368 struct hlist_node dead_node;
369 };
370 struct binder_proc *proc;
371 struct hlist_head refs;
372 int internal_strong_refs;
373 int local_weak_refs;
374 int local_strong_refs;
375 int tmp_refs;
376 binder_uintptr_t ptr;
377 binder_uintptr_t cookie;
378 struct {
379 /*
380 * bitfield elements protected by
381 * proc inner_lock
382 */
383 u8 has_strong_ref:1;
384 u8 pending_strong_ref:1;
385 u8 has_weak_ref:1;
386 u8 pending_weak_ref:1;
387 };
388 struct {
389 /*
390 * invariant after initialization
391 */
392 u8 accept_fds:1;
393 u8 min_priority;
394 };
395 bool has_async_transaction;
396 struct list_head async_todo;
397};
398
399struct binder_ref_death {
400 /**
401 * @work: worklist element for death notifications
402 * (protected by inner_lock of the proc that
403 * this ref belongs to)
404 */
405 struct binder_work work;
406 binder_uintptr_t cookie;
407};
408
409/**
410 * struct binder_ref_data - binder_ref counts and id
411 * @debug_id: unique ID for the ref
412 * @desc: unique userspace handle for ref
413 * @strong: strong ref count (debugging only if not locked)
414 * @weak: weak ref count (debugging only if not locked)
415 *
416 * Structure to hold ref count and ref id information. Since
417 * the actual ref can only be accessed with a lock, this structure
418 * is used to return information about the ref to callers of
419 * ref inc/dec functions.
420 */
421struct binder_ref_data {
422 int debug_id;
423 uint32_t desc;
424 int strong;
425 int weak;
426};
427
428/**
429 * struct binder_ref - struct to track references on nodes
430 * @data: binder_ref_data containing id, handle, and current refcounts
431 * @rb_node_desc: node for lookup by @data.desc in proc's rb_tree
432 * @rb_node_node: node for lookup by @node in proc's rb_tree
433 * @node_entry: list entry for node->refs list in target node
434 * (protected by @node->lock)
435 * @proc: binder_proc containing ref
436 * @node: binder_node of target node. When cleaning up a
437 * ref for deletion in binder_cleanup_ref, a non-NULL
438 * @node indicates the node must be freed
439 * @death: pointer to death notification (ref_death) if requested
440 * (protected by @node->lock)
441 *
442 * Structure to track references from procA to target node (on procB). This
443 * structure is unsafe to access without holding @proc->outer_lock.
444 */
445struct binder_ref {
446 /* Lookups needed: */
447 /* node + proc => ref (transaction) */
448 /* desc + proc => ref (transaction, inc/dec ref) */
449 /* node => refs + procs (proc exit) */
450 struct binder_ref_data data;
451 struct rb_node rb_node_desc;
452 struct rb_node rb_node_node;
453 struct hlist_node node_entry;
454 struct binder_proc *proc;
455 struct binder_node *node;
456 struct binder_ref_death *death;
457};
458
459enum binder_deferred_state {
460 BINDER_DEFERRED_PUT_FILES = 0x01,
461 BINDER_DEFERRED_FLUSH = 0x02,
462 BINDER_DEFERRED_RELEASE = 0x04,
463};
464
465/**
466 * struct binder_proc - binder process bookkeeping
467 * @proc_node: element for binder_procs list
468 * @threads: rbtree of binder_threads in this proc
469 * (protected by @inner_lock)
470 * @nodes: rbtree of binder nodes associated with
471 * this proc ordered by node->ptr
472 * (protected by @inner_lock)
473 * @refs_by_desc: rbtree of refs ordered by ref->desc
474 * (protected by @outer_lock)
475 * @refs_by_node: rbtree of refs ordered by ref->node
476 * (protected by @outer_lock)
477 * @waiting_threads: threads currently waiting for proc work
478 * (protected by @inner_lock)
479 * @pid PID of group_leader of process
480 * (invariant after initialized)
481 * @tsk task_struct for group_leader of process
482 * (invariant after initialized)
483 * @files files_struct for process
484 * (protected by @files_lock)
485 * @files_lock mutex to protect @files
486 * @deferred_work_node: element for binder_deferred_list
487 * (protected by binder_deferred_lock)
488 * @deferred_work: bitmap of deferred work to perform
489 * (protected by binder_deferred_lock)
490 * @is_dead: process is dead and awaiting free
491 * when outstanding transactions are cleaned up
492 * (protected by @inner_lock)
493 * @todo: list of work for this process
494 * (protected by @inner_lock)
495 * @stats: per-process binder statistics
496 * (atomics, no lock needed)
497 * @delivered_death: list of delivered death notification
498 * (protected by @inner_lock)
499 * @max_threads: cap on number of binder threads
500 * (protected by @inner_lock)
501 * @requested_threads: number of binder threads requested but not
502 * yet started. In current implementation, can
503 * only be 0 or 1.
504 * (protected by @inner_lock)
505 * @requested_threads_started: number binder threads started
506 * (protected by @inner_lock)
507 * @tmp_ref: temporary reference to indicate proc is in use
508 * (protected by @inner_lock)
509 * @default_priority: default scheduler priority
510 * (invariant after initialized)
511 * @debugfs_entry: debugfs node
512 * @alloc: binder allocator bookkeeping
513 * @context: binder_context for this proc
514 * (invariant after initialized)
515 * @inner_lock: can nest under outer_lock and/or node lock
516 * @outer_lock: no nesting under innor or node lock
517 * Lock order: 1) outer, 2) node, 3) inner
518 *
519 * Bookkeeping structure for binder processes
520 */
521struct binder_proc {
522 struct hlist_node proc_node;
523 struct rb_root threads;
524 struct rb_root nodes;
525 struct rb_root refs_by_desc;
526 struct rb_root refs_by_node;
527 struct list_head waiting_threads;
528 int pid;
529 struct task_struct *tsk;
530 struct files_struct *files;
531 struct mutex files_lock;
532 struct hlist_node deferred_work_node;
533 int deferred_work;
534 bool is_dead;
535
536 struct list_head todo;
537 struct binder_stats stats;
538 struct list_head delivered_death;
539 int max_threads;
540 int requested_threads;
541 int requested_threads_started;
542 int tmp_ref;
543 long default_priority;
544 struct dentry *debugfs_entry;
545 struct binder_alloc alloc;
546 struct binder_context *context;
547 spinlock_t inner_lock;
548 spinlock_t outer_lock;
549};
550
551enum {
552 BINDER_LOOPER_STATE_REGISTERED = 0x01,
553 BINDER_LOOPER_STATE_ENTERED = 0x02,
554 BINDER_LOOPER_STATE_EXITED = 0x04,
555 BINDER_LOOPER_STATE_INVALID = 0x08,
556 BINDER_LOOPER_STATE_WAITING = 0x10,
557 BINDER_LOOPER_STATE_POLL = 0x20,
558};
559
560/**
561 * struct binder_thread - binder thread bookkeeping
562 * @proc: binder process for this thread
563 * (invariant after initialization)
564 * @rb_node: element for proc->threads rbtree
565 * (protected by @proc->inner_lock)
566 * @waiting_thread_node: element for @proc->waiting_threads list
567 * (protected by @proc->inner_lock)
568 * @pid: PID for this thread
569 * (invariant after initialization)
570 * @looper: bitmap of looping state
571 * (only accessed by this thread)
572 * @looper_needs_return: looping thread needs to exit driver
573 * (no lock needed)
574 * @transaction_stack: stack of in-progress transactions for this thread
575 * (protected by @proc->inner_lock)
576 * @todo: list of work to do for this thread
577 * (protected by @proc->inner_lock)
578 * @process_todo: whether work in @todo should be processed
579 * (protected by @proc->inner_lock)
580 * @return_error: transaction errors reported by this thread
581 * (only accessed by this thread)
582 * @reply_error: transaction errors reported by target thread
583 * (protected by @proc->inner_lock)
584 * @wait: wait queue for thread work
585 * @stats: per-thread statistics
586 * (atomics, no lock needed)
587 * @tmp_ref: temporary reference to indicate thread is in use
588 * (atomic since @proc->inner_lock cannot
589 * always be acquired)
590 * @is_dead: thread is dead and awaiting free
591 * when outstanding transactions are cleaned up
592 * (protected by @proc->inner_lock)
593 *
594 * Bookkeeping structure for binder threads.
595 */
596struct binder_thread {
597 struct binder_proc *proc;
598 struct rb_node rb_node;
599 struct list_head waiting_thread_node;
600 int pid;
601 int looper; /* only modified by this thread */
602 bool looper_need_return; /* can be written by other thread */
603 struct binder_transaction *transaction_stack;
604 struct list_head todo;
605 bool process_todo;
606 struct binder_error return_error;
607 struct binder_error reply_error;
608 wait_queue_head_t wait;
609 struct binder_stats stats;
610 atomic_t tmp_ref;
611 bool is_dead;
612};
613
614struct binder_transaction {
615 int debug_id;
616 struct binder_work work;
617 struct binder_thread *from;
618 struct binder_transaction *from_parent;
619 struct binder_proc *to_proc;
620 struct binder_thread *to_thread;
621 struct binder_transaction *to_parent;
622 unsigned need_reply:1;
623 /* unsigned is_dead:1; */ /* not used at the moment */
624
625 struct binder_buffer *buffer;
626 unsigned int code;
627 unsigned int flags;
628 long priority;
629 long saved_priority;
630 kuid_t sender_euid;
631 /**
632 * @lock: protects @from, @to_proc, and @to_thread
633 *
634 * @from, @to_proc, and @to_thread can be set to NULL
635 * during thread teardown
636 */
637 spinlock_t lock;
638};
639
640/**
641 * binder_proc_lock() - Acquire outer lock for given binder_proc
642 * @proc: struct binder_proc to acquire
643 *
644 * Acquires proc->outer_lock. Used to protect binder_ref
645 * structures associated with the given proc.
646 */
647#define binder_proc_lock(proc) _binder_proc_lock(proc, __LINE__)
648static void
649_binder_proc_lock(struct binder_proc *proc, int line)
650{
651 binder_debug(BINDER_DEBUG_SPINLOCKS,
652 "%s: line=%d\n", __func__, line);
653 spin_lock(&proc->outer_lock);
654}
655
656/**
657 * binder_proc_unlock() - Release spinlock for given binder_proc
658 * @proc: struct binder_proc to acquire
659 *
660 * Release lock acquired via binder_proc_lock()
661 */
662#define binder_proc_unlock(_proc) _binder_proc_unlock(_proc, __LINE__)
663static void
664_binder_proc_unlock(struct binder_proc *proc, int line)
665{
666 binder_debug(BINDER_DEBUG_SPINLOCKS,
667 "%s: line=%d\n", __func__, line);
668 spin_unlock(&proc->outer_lock);
669}
670
671/**
672 * binder_inner_proc_lock() - Acquire inner lock for given binder_proc
673 * @proc: struct binder_proc to acquire
674 *
675 * Acquires proc->inner_lock. Used to protect todo lists
676 */
677#define binder_inner_proc_lock(proc) _binder_inner_proc_lock(proc, __LINE__)
678static void
679_binder_inner_proc_lock(struct binder_proc *proc, int line)
680{
681 binder_debug(BINDER_DEBUG_SPINLOCKS,
682 "%s: line=%d\n", __func__, line);
683 spin_lock(&proc->inner_lock);
684}
685
686/**
687 * binder_inner_proc_unlock() - Release inner lock for given binder_proc
688 * @proc: struct binder_proc to acquire
689 *
690 * Release lock acquired via binder_inner_proc_lock()
691 */
692#define binder_inner_proc_unlock(proc) _binder_inner_proc_unlock(proc, __LINE__)
693static void
694_binder_inner_proc_unlock(struct binder_proc *proc, int line)
695{
696 binder_debug(BINDER_DEBUG_SPINLOCKS,
697 "%s: line=%d\n", __func__, line);
698 spin_unlock(&proc->inner_lock);
699}
700
701/**
702 * binder_node_lock() - Acquire spinlock for given binder_node
703 * @node: struct binder_node to acquire
704 *
705 * Acquires node->lock. Used to protect binder_node fields
706 */
707#define binder_node_lock(node) _binder_node_lock(node, __LINE__)
708static void
709_binder_node_lock(struct binder_node *node, int line)
710{
711 binder_debug(BINDER_DEBUG_SPINLOCKS,
712 "%s: line=%d\n", __func__, line);
713 spin_lock(&node->lock);
714}
715
716/**
717 * binder_node_unlock() - Release spinlock for given binder_proc
718 * @node: struct binder_node to acquire
719 *
720 * Release lock acquired via binder_node_lock()
721 */
722#define binder_node_unlock(node) _binder_node_unlock(node, __LINE__)
723static void
724_binder_node_unlock(struct binder_node *node, int line)
725{
726 binder_debug(BINDER_DEBUG_SPINLOCKS,
727 "%s: line=%d\n", __func__, line);
728 spin_unlock(&node->lock);
729}
730
731/**
732 * binder_node_inner_lock() - Acquire node and inner locks
733 * @node: struct binder_node to acquire
734 *
735 * Acquires node->lock. If node->proc also acquires
736 * proc->inner_lock. Used to protect binder_node fields
737 */
738#define binder_node_inner_lock(node) _binder_node_inner_lock(node, __LINE__)
739static void
740_binder_node_inner_lock(struct binder_node *node, int line)
741{
742 binder_debug(BINDER_DEBUG_SPINLOCKS,
743 "%s: line=%d\n", __func__, line);
744 spin_lock(&node->lock);
745 if (node->proc)
746 binder_inner_proc_lock(node->proc);
747}
748
749/**
750 * binder_node_unlock() - Release node and inner locks
751 * @node: struct binder_node to acquire
752 *
753 * Release lock acquired via binder_node_lock()
754 */
755#define binder_node_inner_unlock(node) _binder_node_inner_unlock(node, __LINE__)
756static void
757_binder_node_inner_unlock(struct binder_node *node, int line)
758{
759 struct binder_proc *proc = node->proc;
760
761 binder_debug(BINDER_DEBUG_SPINLOCKS,
762 "%s: line=%d\n", __func__, line);
763 if (proc)
764 binder_inner_proc_unlock(proc);
765 spin_unlock(&node->lock);
766}
767
768static bool binder_worklist_empty_ilocked(struct list_head *list)
769{
770 return list_empty(list);
771}
772
773/**
774 * binder_worklist_empty() - Check if no items on the work list
775 * @proc: binder_proc associated with list
776 * @list: list to check
777 *
778 * Return: true if there are no items on list, else false
779 */
780static bool binder_worklist_empty(struct binder_proc *proc,
781 struct list_head *list)
782{
783 bool ret;
784
785 binder_inner_proc_lock(proc);
786 ret = binder_worklist_empty_ilocked(list);
787 binder_inner_proc_unlock(proc);
788 return ret;
789}
790
791/**
792 * binder_enqueue_work_ilocked() - Add an item to the work list
793 * @work: struct binder_work to add to list
794 * @target_list: list to add work to
795 *
796 * Adds the work to the specified list. Asserts that work
797 * is not already on a list.
798 *
799 * Requires the proc->inner_lock to be held.
800 */
801static void
802binder_enqueue_work_ilocked(struct binder_work *work,
803 struct list_head *target_list)
804{
805 BUG_ON(target_list == NULL);
806 BUG_ON(work->entry.next && !list_empty(&work->entry));
807 list_add_tail(&work->entry, target_list);
808}
809
810/**
811 * binder_enqueue_deferred_thread_work_ilocked() - Add deferred thread work
812 * @thread: thread to queue work to
813 * @work: struct binder_work to add to list
814 *
815 * Adds the work to the todo list of the thread. Doesn't set the process_todo
816 * flag, which means that (if it wasn't already set) the thread will go to
817 * sleep without handling this work when it calls read.
818 *
819 * Requires the proc->inner_lock to be held.
820 */
821static void
822binder_enqueue_deferred_thread_work_ilocked(struct binder_thread *thread,
823 struct binder_work *work)
824{
825 binder_enqueue_work_ilocked(work, &thread->todo);
826}
827
828/**
829 * binder_enqueue_thread_work_ilocked() - Add an item to the thread work list
830 * @thread: thread to queue work to
831 * @work: struct binder_work to add to list
832 *
833 * Adds the work to the todo list of the thread, and enables processing
834 * of the todo queue.
835 *
836 * Requires the proc->inner_lock to be held.
837 */
838static void
839binder_enqueue_thread_work_ilocked(struct binder_thread *thread,
840 struct binder_work *work)
841{
842 binder_enqueue_work_ilocked(work, &thread->todo);
843 thread->process_todo = true;
844}
845
846/**
847 * binder_enqueue_thread_work() - Add an item to the thread work list
848 * @thread: thread to queue work to
849 * @work: struct binder_work to add to list
850 *
851 * Adds the work to the todo list of the thread, and enables processing
852 * of the todo queue.
853 */
854static void
855binder_enqueue_thread_work(struct binder_thread *thread,
856 struct binder_work *work)
857{
858 binder_inner_proc_lock(thread->proc);
859 binder_enqueue_thread_work_ilocked(thread, work);
860 binder_inner_proc_unlock(thread->proc);
861}
862
863static void
864binder_dequeue_work_ilocked(struct binder_work *work)
865{
866 list_del_init(&work->entry);
867}
868
869/**
870 * binder_dequeue_work() - Removes an item from the work list
871 * @proc: binder_proc associated with list
872 * @work: struct binder_work to remove from list
873 *
874 * Removes the specified work item from whatever list it is on.
875 * Can safely be called if work is not on any list.
876 */
877static void
878binder_dequeue_work(struct binder_proc *proc, struct binder_work *work)
879{
880 binder_inner_proc_lock(proc);
881 binder_dequeue_work_ilocked(work);
882 binder_inner_proc_unlock(proc);
883}
884
885static struct binder_work *binder_dequeue_work_head_ilocked(
886 struct list_head *list)
887{
888 struct binder_work *w;
889
890 w = list_first_entry_or_null(list, struct binder_work, entry);
891 if (w)
892 list_del_init(&w->entry);
893 return w;
894}
895
896/**
897 * binder_dequeue_work_head() - Dequeues the item at head of list
898 * @proc: binder_proc associated with list
899 * @list: list to dequeue head
900 *
901 * Removes the head of the list if there are items on the list
902 *
903 * Return: pointer dequeued binder_work, NULL if list was empty
904 */
905static struct binder_work *binder_dequeue_work_head(
906 struct binder_proc *proc,
907 struct list_head *list)
908{
909 struct binder_work *w;
910
911 binder_inner_proc_lock(proc);
912 w = binder_dequeue_work_head_ilocked(list);
913 binder_inner_proc_unlock(proc);
914 return w;
915}
916
917static void
918binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
919static void binder_free_thread(struct binder_thread *thread);
920static void binder_free_proc(struct binder_proc *proc);
921static void binder_inc_node_tmpref_ilocked(struct binder_node *node);
922
923static int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
924{
925 unsigned long rlim_cur;
926 unsigned long irqs;
927 int ret;
928
929 mutex_lock(&proc->files_lock);
930 if (proc->files == NULL) {
931 ret = -ESRCH;
932 goto err;
933 }
934 if (!lock_task_sighand(proc->tsk, &irqs)) {
935 ret = -EMFILE;
936 goto err;
937 }
938 rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
939 unlock_task_sighand(proc->tsk, &irqs);
940
941 ret = __alloc_fd(proc->files, 0, rlim_cur, flags);
942err:
943 mutex_unlock(&proc->files_lock);
944 return ret;
945}
946
947/*
948 * copied from fd_install
949 */
950static void task_fd_install(
951 struct binder_proc *proc, unsigned int fd, struct file *file)
952{
953 mutex_lock(&proc->files_lock);
954 if (proc->files)
955 __fd_install(proc->files, fd, file);
956 mutex_unlock(&proc->files_lock);
957}
958
959/*
960 * copied from sys_close
961 */
962static long task_close_fd(struct binder_proc *proc, unsigned int fd)
963{
964 int retval;
965
966 mutex_lock(&proc->files_lock);
967 if (proc->files == NULL) {
968 retval = -ESRCH;
969 goto err;
970 }
971 retval = __close_fd(proc->files, fd);
972 /* can't restart close syscall because file table entry was cleared */
973 if (unlikely(retval == -ERESTARTSYS ||
974 retval == -ERESTARTNOINTR ||
975 retval == -ERESTARTNOHAND ||
976 retval == -ERESTART_RESTARTBLOCK))
977 retval = -EINTR;
978err:
979 mutex_unlock(&proc->files_lock);
980 return retval;
981}
982
983static bool binder_has_work_ilocked(struct binder_thread *thread,
984 bool do_proc_work)
985{
986 return thread->process_todo ||
987 thread->looper_need_return ||
988 (do_proc_work &&
989 !binder_worklist_empty_ilocked(&thread->proc->todo));
990}
991
992static bool binder_has_work(struct binder_thread *thread, bool do_proc_work)
993{
994 bool has_work;
995
996 binder_inner_proc_lock(thread->proc);
997 has_work = binder_has_work_ilocked(thread, do_proc_work);
998 binder_inner_proc_unlock(thread->proc);
999
1000 return has_work;
1001}
1002
1003static bool binder_available_for_proc_work_ilocked(struct binder_thread *thread)
1004{
1005 return !thread->transaction_stack &&
1006 binder_worklist_empty_ilocked(&thread->todo) &&
1007 (thread->looper & (BINDER_LOOPER_STATE_ENTERED |
1008 BINDER_LOOPER_STATE_REGISTERED));
1009}
1010
1011static void binder_wakeup_poll_threads_ilocked(struct binder_proc *proc,
1012 bool sync)
1013{
1014 struct rb_node *n;
1015 struct binder_thread *thread;
1016
1017 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
1018 thread = rb_entry(n, struct binder_thread, rb_node);
1019 if (thread->looper & BINDER_LOOPER_STATE_POLL &&
1020 binder_available_for_proc_work_ilocked(thread)) {
1021 if (sync)
1022 wake_up_interruptible_sync(&thread->wait);
1023 else
1024 wake_up_interruptible(&thread->wait);
1025 }
1026 }
1027}
1028
1029/**
1030 * binder_select_thread_ilocked() - selects a thread for doing proc work.
1031 * @proc: process to select a thread from
1032 *
1033 * Note that calling this function moves the thread off the waiting_threads
1034 * list, so it can only be woken up by the caller of this function, or a
1035 * signal. Therefore, callers *should* always wake up the thread this function
1036 * returns.
1037 *
1038 * Return: If there's a thread currently waiting for process work,
1039 * returns that thread. Otherwise returns NULL.
1040 */
1041static struct binder_thread *
1042binder_select_thread_ilocked(struct binder_proc *proc)
1043{
1044 struct binder_thread *thread;
1045
1046 assert_spin_locked(&proc->inner_lock);
1047 thread = list_first_entry_or_null(&proc->waiting_threads,
1048 struct binder_thread,
1049 waiting_thread_node);
1050
1051 if (thread)
1052 list_del_init(&thread->waiting_thread_node);
1053
1054 return thread;
1055}
1056
1057/**
1058 * binder_wakeup_thread_ilocked() - wakes up a thread for doing proc work.
1059 * @proc: process to wake up a thread in
1060 * @thread: specific thread to wake-up (may be NULL)
1061 * @sync: whether to do a synchronous wake-up
1062 *
1063 * This function wakes up a thread in the @proc process.
1064 * The caller may provide a specific thread to wake-up in
1065 * the @thread parameter. If @thread is NULL, this function
1066 * will wake up threads that have called poll().
1067 *
1068 * Note that for this function to work as expected, callers
1069 * should first call binder_select_thread() to find a thread
1070 * to handle the work (if they don't have a thread already),
1071 * and pass the result into the @thread parameter.
1072 */
1073static void binder_wakeup_thread_ilocked(struct binder_proc *proc,
1074 struct binder_thread *thread,
1075 bool sync)
1076{
1077 assert_spin_locked(&proc->inner_lock);
1078
1079 if (thread) {
1080 if (sync)
1081 wake_up_interruptible_sync(&thread->wait);
1082 else
1083 wake_up_interruptible(&thread->wait);
1084 return;
1085 }
1086
1087 /* Didn't find a thread waiting for proc work; this can happen
1088 * in two scenarios:
1089 * 1. All threads are busy handling transactions
1090 * In that case, one of those threads should call back into
1091 * the kernel driver soon and pick up this work.
1092 * 2. Threads are using the (e)poll interface, in which case
1093 * they may be blocked on the waitqueue without having been
1094 * added to waiting_threads. For this case, we just iterate
1095 * over all threads not handling transaction work, and
1096 * wake them all up. We wake all because we don't know whether
1097 * a thread that called into (e)poll is handling non-binder
1098 * work currently.
1099 */
1100 binder_wakeup_poll_threads_ilocked(proc, sync);
1101}
1102
1103static void binder_wakeup_proc_ilocked(struct binder_proc *proc)
1104{
1105 struct binder_thread *thread = binder_select_thread_ilocked(proc);
1106
1107 binder_wakeup_thread_ilocked(proc, thread, /* sync = */false);
1108}
1109
1110static void binder_set_nice(long nice)
1111{
1112 long min_nice;
1113
1114 if (can_nice(current, nice)) {
1115 set_user_nice(current, nice);
1116 return;
1117 }
1118 min_nice = rlimit_to_nice(rlimit(RLIMIT_NICE));
1119 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
1120 "%d: nice value %ld not allowed use %ld instead\n",
1121 current->pid, nice, min_nice);
1122 set_user_nice(current, min_nice);
1123 if (min_nice <= MAX_NICE)
1124 return;
1125 binder_user_error("%d RLIMIT_NICE not set\n", current->pid);
1126}
1127
1128static struct binder_node *binder_get_node_ilocked(struct binder_proc *proc,
1129 binder_uintptr_t ptr)
1130{
1131 struct rb_node *n = proc->nodes.rb_node;
1132 struct binder_node *node;
1133
1134 assert_spin_locked(&proc->inner_lock);
1135
1136 while (n) {
1137 node = rb_entry(n, struct binder_node, rb_node);
1138
1139 if (ptr < node->ptr)
1140 n = n->rb_left;
1141 else if (ptr > node->ptr)
1142 n = n->rb_right;
1143 else {
1144 /*
1145 * take an implicit weak reference
1146 * to ensure node stays alive until
1147 * call to binder_put_node()
1148 */
1149 binder_inc_node_tmpref_ilocked(node);
1150 return node;
1151 }
1152 }
1153 return NULL;
1154}
1155
1156static struct binder_node *binder_get_node(struct binder_proc *proc,
1157 binder_uintptr_t ptr)
1158{
1159 struct binder_node *node;
1160
1161 binder_inner_proc_lock(proc);
1162 node = binder_get_node_ilocked(proc, ptr);
1163 binder_inner_proc_unlock(proc);
1164 return node;
1165}
1166
1167static struct binder_node *binder_init_node_ilocked(
1168 struct binder_proc *proc,
1169 struct binder_node *new_node,
1170 struct flat_binder_object *fp)
1171{
1172 struct rb_node **p = &proc->nodes.rb_node;
1173 struct rb_node *parent = NULL;
1174 struct binder_node *node;
1175 binder_uintptr_t ptr = fp ? fp->binder : 0;
1176 binder_uintptr_t cookie = fp ? fp->cookie : 0;
1177 __u32 flags = fp ? fp->flags : 0;
1178
1179 assert_spin_locked(&proc->inner_lock);
1180
1181 while (*p) {
1182
1183 parent = *p;
1184 node = rb_entry(parent, struct binder_node, rb_node);
1185
1186 if (ptr < node->ptr)
1187 p = &(*p)->rb_left;
1188 else if (ptr > node->ptr)
1189 p = &(*p)->rb_right;
1190 else {
1191 /*
1192 * A matching node is already in
1193 * the rb tree. Abandon the init
1194 * and return it.
1195 */
1196 binder_inc_node_tmpref_ilocked(node);
1197 return node;
1198 }
1199 }
1200 node = new_node;
1201 binder_stats_created(BINDER_STAT_NODE);
1202 node->tmp_refs++;
1203 rb_link_node(&node->rb_node, parent, p);
1204 rb_insert_color(&node->rb_node, &proc->nodes);
1205 node->debug_id = atomic_inc_return(&binder_last_id);
1206 node->proc = proc;
1207 node->ptr = ptr;
1208 node->cookie = cookie;
1209 node->work.type = BINDER_WORK_NODE;
1210 node->min_priority = flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
1211 node->accept_fds = !!(flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
1212 spin_lock_init(&node->lock);
1213 INIT_LIST_HEAD(&node->work.entry);
1214 INIT_LIST_HEAD(&node->async_todo);
1215 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1216 "%d:%d node %d u%016llx c%016llx created\n",
1217 proc->pid, current->pid, node->debug_id,
1218 (u64)node->ptr, (u64)node->cookie);
1219
1220 return node;
1221}
1222
1223static struct binder_node *binder_new_node(struct binder_proc *proc,
1224 struct flat_binder_object *fp)
1225{
1226 struct binder_node *node;
1227 struct binder_node *new_node = kzalloc(sizeof(*node), GFP_KERNEL);
1228
1229 if (!new_node)
1230 return NULL;
1231 binder_inner_proc_lock(proc);
1232 node = binder_init_node_ilocked(proc, new_node, fp);
1233 binder_inner_proc_unlock(proc);
1234 if (node != new_node)
1235 /*
1236 * The node was already added by another thread
1237 */
1238 kfree(new_node);
1239
1240 return node;
1241}
1242
1243static void binder_free_node(struct binder_node *node)
1244{
1245 kfree(node);
1246 binder_stats_deleted(BINDER_STAT_NODE);
1247}
1248
1249static int binder_inc_node_nilocked(struct binder_node *node, int strong,
1250 int internal,
1251 struct list_head *target_list)
1252{
1253 struct binder_proc *proc = node->proc;
1254
1255 assert_spin_locked(&node->lock);
1256 if (proc)
1257 assert_spin_locked(&proc->inner_lock);
1258 if (strong) {
1259 if (internal) {
1260 if (target_list == NULL &&
1261 node->internal_strong_refs == 0 &&
1262 !(node->proc &&
1263 node == node->proc->context->binder_context_mgr_node &&
1264 node->has_strong_ref)) {
1265 pr_err("invalid inc strong node for %d\n",
1266 node->debug_id);
1267 return -EINVAL;
1268 }
1269 node->internal_strong_refs++;
1270 } else
1271 node->local_strong_refs++;
1272 if (!node->has_strong_ref && target_list) {
1273 binder_dequeue_work_ilocked(&node->work);
1274 /*
1275 * Note: this function is the only place where we queue
1276 * directly to a thread->todo without using the
1277 * corresponding binder_enqueue_thread_work() helper
1278 * functions; in this case it's ok to not set the
1279 * process_todo flag, since we know this node work will
1280 * always be followed by other work that starts queue
1281 * processing: in case of synchronous transactions, a
1282 * BR_REPLY or BR_ERROR; in case of oneway
1283 * transactions, a BR_TRANSACTION_COMPLETE.
1284 */
1285 binder_enqueue_work_ilocked(&node->work, target_list);
1286 }
1287 } else {
1288 if (!internal)
1289 node->local_weak_refs++;
1290 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
1291 if (target_list == NULL) {
1292 pr_err("invalid inc weak node for %d\n",
1293 node->debug_id);
1294 return -EINVAL;
1295 }
1296 /*
1297 * See comment above
1298 */
1299 binder_enqueue_work_ilocked(&node->work, target_list);
1300 }
1301 }
1302 return 0;
1303}
1304
1305static int binder_inc_node(struct binder_node *node, int strong, int internal,
1306 struct list_head *target_list)
1307{
1308 int ret;
1309
1310 binder_node_inner_lock(node);
1311 ret = binder_inc_node_nilocked(node, strong, internal, target_list);
1312 binder_node_inner_unlock(node);
1313
1314 return ret;
1315}
1316
1317static bool binder_dec_node_nilocked(struct binder_node *node,
1318 int strong, int internal)
1319{
1320 struct binder_proc *proc = node->proc;
1321
1322 assert_spin_locked(&node->lock);
1323 if (proc)
1324 assert_spin_locked(&proc->inner_lock);
1325 if (strong) {
1326 if (internal)
1327 node->internal_strong_refs--;
1328 else
1329 node->local_strong_refs--;
1330 if (node->local_strong_refs || node->internal_strong_refs)
1331 return false;
1332 } else {
1333 if (!internal)
1334 node->local_weak_refs--;
1335 if (node->local_weak_refs || node->tmp_refs ||
1336 !hlist_empty(&node->refs))
1337 return false;
1338 }
1339
1340 if (proc && (node->has_strong_ref || node->has_weak_ref)) {
1341 if (list_empty(&node->work.entry)) {
1342 binder_enqueue_work_ilocked(&node->work, &proc->todo);
1343 binder_wakeup_proc_ilocked(proc);
1344 }
1345 } else {
1346 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
1347 !node->local_weak_refs && !node->tmp_refs) {
1348 if (proc) {
1349 binder_dequeue_work_ilocked(&node->work);
1350 rb_erase(&node->rb_node, &proc->nodes);
1351 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1352 "refless node %d deleted\n",
1353 node->debug_id);
1354 } else {
1355 BUG_ON(!list_empty(&node->work.entry));
1356 spin_lock(&binder_dead_nodes_lock);
1357 /*
1358 * tmp_refs could have changed so
1359 * check it again
1360 */
1361 if (node->tmp_refs) {
1362 spin_unlock(&binder_dead_nodes_lock);
1363 return false;
1364 }
1365 hlist_del(&node->dead_node);
1366 spin_unlock(&binder_dead_nodes_lock);
1367 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1368 "dead node %d deleted\n",
1369 node->debug_id);
1370 }
1371 return true;
1372 }
1373 }
1374 return false;
1375}
1376
1377static void binder_dec_node(struct binder_node *node, int strong, int internal)
1378{
1379 bool free_node;
1380
1381 binder_node_inner_lock(node);
1382 free_node = binder_dec_node_nilocked(node, strong, internal);
1383 binder_node_inner_unlock(node);
1384 if (free_node)
1385 binder_free_node(node);
1386}
1387
1388static void binder_inc_node_tmpref_ilocked(struct binder_node *node)
1389{
1390 /*
1391 * No call to binder_inc_node() is needed since we
1392 * don't need to inform userspace of any changes to
1393 * tmp_refs
1394 */
1395 node->tmp_refs++;
1396}
1397
1398/**
1399 * binder_inc_node_tmpref() - take a temporary reference on node
1400 * @node: node to reference
1401 *
1402 * Take reference on node to prevent the node from being freed
1403 * while referenced only by a local variable. The inner lock is
1404 * needed to serialize with the node work on the queue (which
1405 * isn't needed after the node is dead). If the node is dead
1406 * (node->proc is NULL), use binder_dead_nodes_lock to protect
1407 * node->tmp_refs against dead-node-only cases where the node
1408 * lock cannot be acquired (eg traversing the dead node list to
1409 * print nodes)
1410 */
1411static void binder_inc_node_tmpref(struct binder_node *node)
1412{
1413 binder_node_lock(node);
1414 if (node->proc)
1415 binder_inner_proc_lock(node->proc);
1416 else
1417 spin_lock(&binder_dead_nodes_lock);
1418 binder_inc_node_tmpref_ilocked(node);
1419 if (node->proc)
1420 binder_inner_proc_unlock(node->proc);
1421 else
1422 spin_unlock(&binder_dead_nodes_lock);
1423 binder_node_unlock(node);
1424}
1425
1426/**
1427 * binder_dec_node_tmpref() - remove a temporary reference on node
1428 * @node: node to reference
1429 *
1430 * Release temporary reference on node taken via binder_inc_node_tmpref()
1431 */
1432static void binder_dec_node_tmpref(struct binder_node *node)
1433{
1434 bool free_node;
1435
1436 binder_node_inner_lock(node);
1437 if (!node->proc)
1438 spin_lock(&binder_dead_nodes_lock);
1439 node->tmp_refs--;
1440 BUG_ON(node->tmp_refs < 0);
1441 if (!node->proc)
1442 spin_unlock(&binder_dead_nodes_lock);
1443 /*
1444 * Call binder_dec_node() to check if all refcounts are 0
1445 * and cleanup is needed. Calling with strong=0 and internal=1
1446 * causes no actual reference to be released in binder_dec_node().
1447 * If that changes, a change is needed here too.
1448 */
1449 free_node = binder_dec_node_nilocked(node, 0, 1);
1450 binder_node_inner_unlock(node);
1451 if (free_node)
1452 binder_free_node(node);
1453}
1454
1455static void binder_put_node(struct binder_node *node)
1456{
1457 binder_dec_node_tmpref(node);
1458}
1459
1460static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc,
1461 u32 desc, bool need_strong_ref)
1462{
1463 struct rb_node *n = proc->refs_by_desc.rb_node;
1464 struct binder_ref *ref;
1465
1466 while (n) {
1467 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1468
1469 if (desc < ref->data.desc) {
1470 n = n->rb_left;
1471 } else if (desc > ref->data.desc) {
1472 n = n->rb_right;
1473 } else if (need_strong_ref && !ref->data.strong) {
1474 binder_user_error("tried to use weak ref as strong ref\n");
1475 return NULL;
1476 } else {
1477 return ref;
1478 }
1479 }
1480 return NULL;
1481}
1482
1483/**
1484 * binder_get_ref_for_node_olocked() - get the ref associated with given node
1485 * @proc: binder_proc that owns the ref
1486 * @node: binder_node of target
1487 * @new_ref: newly allocated binder_ref to be initialized or %NULL
1488 *
1489 * Look up the ref for the given node and return it if it exists
1490 *
1491 * If it doesn't exist and the caller provides a newly allocated
1492 * ref, initialize the fields of the newly allocated ref and insert
1493 * into the given proc rb_trees and node refs list.
1494 *
1495 * Return: the ref for node. It is possible that another thread
1496 * allocated/initialized the ref first in which case the
1497 * returned ref would be different than the passed-in
1498 * new_ref. new_ref must be kfree'd by the caller in
1499 * this case.
1500 */
1501static struct binder_ref *binder_get_ref_for_node_olocked(
1502 struct binder_proc *proc,
1503 struct binder_node *node,
1504 struct binder_ref *new_ref)
1505{
1506 struct binder_context *context = proc->context;
1507 struct rb_node **p = &proc->refs_by_node.rb_node;
1508 struct rb_node *parent = NULL;
1509 struct binder_ref *ref;
1510 struct rb_node *n;
1511
1512 while (*p) {
1513 parent = *p;
1514 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1515
1516 if (node < ref->node)
1517 p = &(*p)->rb_left;
1518 else if (node > ref->node)
1519 p = &(*p)->rb_right;
1520 else
1521 return ref;
1522 }
1523 if (!new_ref)
1524 return NULL;
1525
1526 binder_stats_created(BINDER_STAT_REF);
1527 new_ref->data.debug_id = atomic_inc_return(&binder_last_id);
1528 new_ref->proc = proc;
1529 new_ref->node = node;
1530 rb_link_node(&new_ref->rb_node_node, parent, p);
1531 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1532
1533 new_ref->data.desc = (node == context->binder_context_mgr_node) ? 0 : 1;
1534 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1535 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1536 if (ref->data.desc > new_ref->data.desc)
1537 break;
1538 new_ref->data.desc = ref->data.desc + 1;
1539 }
1540
1541 p = &proc->refs_by_desc.rb_node;
1542 while (*p) {
1543 parent = *p;
1544 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1545
1546 if (new_ref->data.desc < ref->data.desc)
1547 p = &(*p)->rb_left;
1548 else if (new_ref->data.desc > ref->data.desc)
1549 p = &(*p)->rb_right;
1550 else
1551 BUG();
1552 }
1553 rb_link_node(&new_ref->rb_node_desc, parent, p);
1554 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
1555
1556 binder_node_lock(node);
1557 hlist_add_head(&new_ref->node_entry, &node->refs);
1558
1559 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1560 "%d new ref %d desc %d for node %d\n",
1561 proc->pid, new_ref->data.debug_id, new_ref->data.desc,
1562 node->debug_id);
1563 binder_node_unlock(node);
1564 return new_ref;
1565}
1566
1567static void binder_cleanup_ref_olocked(struct binder_ref *ref)
1568{
1569 bool delete_node = false;
1570
1571 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1572 "%d delete ref %d desc %d for node %d\n",
1573 ref->proc->pid, ref->data.debug_id, ref->data.desc,
1574 ref->node->debug_id);
1575
1576 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1577 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
1578
1579 binder_node_inner_lock(ref->node);
1580 if (ref->data.strong)
1581 binder_dec_node_nilocked(ref->node, 1, 1);
1582
1583 hlist_del(&ref->node_entry);
1584 delete_node = binder_dec_node_nilocked(ref->node, 0, 1);
1585 binder_node_inner_unlock(ref->node);
1586 /*
1587 * Clear ref->node unless we want the caller to free the node
1588 */
1589 if (!delete_node) {
1590 /*
1591 * The caller uses ref->node to determine
1592 * whether the node needs to be freed. Clear
1593 * it since the node is still alive.
1594 */
1595 ref->node = NULL;
1596 }
1597
1598 if (ref->death) {
1599 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1600 "%d delete ref %d desc %d has death notification\n",
1601 ref->proc->pid, ref->data.debug_id,
1602 ref->data.desc);
1603 binder_dequeue_work(ref->proc, &ref->death->work);
1604 binder_stats_deleted(BINDER_STAT_DEATH);
1605 }
1606 binder_stats_deleted(BINDER_STAT_REF);
1607}
1608
1609/**
1610 * binder_inc_ref_olocked() - increment the ref for given handle
1611 * @ref: ref to be incremented
1612 * @strong: if true, strong increment, else weak
1613 * @target_list: list to queue node work on
1614 *
1615 * Increment the ref. @ref->proc->outer_lock must be held on entry
1616 *
1617 * Return: 0, if successful, else errno
1618 */
1619static int binder_inc_ref_olocked(struct binder_ref *ref, int strong,
1620 struct list_head *target_list)
1621{
1622 int ret;
1623
1624 if (strong) {
1625 if (ref->data.strong == 0) {
1626 ret = binder_inc_node(ref->node, 1, 1, target_list);
1627 if (ret)
1628 return ret;
1629 }
1630 ref->data.strong++;
1631 } else {
1632 if (ref->data.weak == 0) {
1633 ret = binder_inc_node(ref->node, 0, 1, target_list);
1634 if (ret)
1635 return ret;
1636 }
1637 ref->data.weak++;
1638 }
1639 return 0;
1640}
1641
1642/**
1643 * binder_dec_ref() - dec the ref for given handle
1644 * @ref: ref to be decremented
1645 * @strong: if true, strong decrement, else weak
1646 *
1647 * Decrement the ref.
1648 *
1649 * Return: true if ref is cleaned up and ready to be freed
1650 */
1651static bool binder_dec_ref_olocked(struct binder_ref *ref, int strong)
1652{
1653 if (strong) {
1654 if (ref->data.strong == 0) {
1655 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
1656 ref->proc->pid, ref->data.debug_id,
1657 ref->data.desc, ref->data.strong,
1658 ref->data.weak);
1659 return false;
1660 }
1661 ref->data.strong--;
1662 if (ref->data.strong == 0)
1663 binder_dec_node(ref->node, strong, 1);
1664 } else {
1665 if (ref->data.weak == 0) {
1666 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
1667 ref->proc->pid, ref->data.debug_id,
1668 ref->data.desc, ref->data.strong,
1669 ref->data.weak);
1670 return false;
1671 }
1672 ref->data.weak--;
1673 }
1674 if (ref->data.strong == 0 && ref->data.weak == 0) {
1675 binder_cleanup_ref_olocked(ref);
1676 return true;
1677 }
1678 return false;
1679}
1680
1681/**
1682 * binder_get_node_from_ref() - get the node from the given proc/desc
1683 * @proc: proc containing the ref
1684 * @desc: the handle associated with the ref
1685 * @need_strong_ref: if true, only return node if ref is strong
1686 * @rdata: the id/refcount data for the ref
1687 *
1688 * Given a proc and ref handle, return the associated binder_node
1689 *
1690 * Return: a binder_node or NULL if not found or not strong when strong required
1691 */
1692static struct binder_node *binder_get_node_from_ref(
1693 struct binder_proc *proc,
1694 u32 desc, bool need_strong_ref,
1695 struct binder_ref_data *rdata)
1696{
1697 struct binder_node *node;
1698 struct binder_ref *ref;
1699
1700 binder_proc_lock(proc);
1701 ref = binder_get_ref_olocked(proc, desc, need_strong_ref);
1702 if (!ref)
1703 goto err_no_ref;
1704 node = ref->node;
1705 /*
1706 * Take an implicit reference on the node to ensure
1707 * it stays alive until the call to binder_put_node()
1708 */
1709 binder_inc_node_tmpref(node);
1710 if (rdata)
1711 *rdata = ref->data;
1712 binder_proc_unlock(proc);
1713
1714 return node;
1715
1716err_no_ref:
1717 binder_proc_unlock(proc);
1718 return NULL;
1719}
1720
1721/**
1722 * binder_free_ref() - free the binder_ref
1723 * @ref: ref to free
1724 *
1725 * Free the binder_ref. Free the binder_node indicated by ref->node
1726 * (if non-NULL) and the binder_ref_death indicated by ref->death.
1727 */
1728static void binder_free_ref(struct binder_ref *ref)
1729{
1730 if (ref->node)
1731 binder_free_node(ref->node);
1732 kfree(ref->death);
1733 kfree(ref);
1734}
1735
1736/**
1737 * binder_update_ref_for_handle() - inc/dec the ref for given handle
1738 * @proc: proc containing the ref
1739 * @desc: the handle associated with the ref
1740 * @increment: true=inc reference, false=dec reference
1741 * @strong: true=strong reference, false=weak reference
1742 * @rdata: the id/refcount data for the ref
1743 *
1744 * Given a proc and ref handle, increment or decrement the ref
1745 * according to "increment" arg.
1746 *
1747 * Return: 0 if successful, else errno
1748 */
1749static int binder_update_ref_for_handle(struct binder_proc *proc,
1750 uint32_t desc, bool increment, bool strong,
1751 struct binder_ref_data *rdata)
1752{
1753 int ret = 0;
1754 struct binder_ref *ref;
1755 bool delete_ref = false;
1756
1757 binder_proc_lock(proc);
1758 ref = binder_get_ref_olocked(proc, desc, strong);
1759 if (!ref) {
1760 ret = -EINVAL;
1761 goto err_no_ref;
1762 }
1763 if (increment)
1764 ret = binder_inc_ref_olocked(ref, strong, NULL);
1765 else
1766 delete_ref = binder_dec_ref_olocked(ref, strong);
1767
1768 if (rdata)
1769 *rdata = ref->data;
1770 binder_proc_unlock(proc);
1771
1772 if (delete_ref)
1773 binder_free_ref(ref);
1774 return ret;
1775
1776err_no_ref:
1777 binder_proc_unlock(proc);
1778 return ret;
1779}
1780
1781/**
1782 * binder_dec_ref_for_handle() - dec the ref for given handle
1783 * @proc: proc containing the ref
1784 * @desc: the handle associated with the ref
1785 * @strong: true=strong reference, false=weak reference
1786 * @rdata: the id/refcount data for the ref
1787 *
1788 * Just calls binder_update_ref_for_handle() to decrement the ref.
1789 *
1790 * Return: 0 if successful, else errno
1791 */
1792static int binder_dec_ref_for_handle(struct binder_proc *proc,
1793 uint32_t desc, bool strong, struct binder_ref_data *rdata)
1794{
1795 return binder_update_ref_for_handle(proc, desc, false, strong, rdata);
1796}
1797
1798
1799/**
1800 * binder_inc_ref_for_node() - increment the ref for given proc/node
1801 * @proc: proc containing the ref
1802 * @node: target node
1803 * @strong: true=strong reference, false=weak reference
1804 * @target_list: worklist to use if node is incremented
1805 * @rdata: the id/refcount data for the ref
1806 *
1807 * Given a proc and node, increment the ref. Create the ref if it
1808 * doesn't already exist
1809 *
1810 * Return: 0 if successful, else errno
1811 */
1812static int binder_inc_ref_for_node(struct binder_proc *proc,
1813 struct binder_node *node,
1814 bool strong,
1815 struct list_head *target_list,
1816 struct binder_ref_data *rdata)
1817{
1818 struct binder_ref *ref;
1819 struct binder_ref *new_ref = NULL;
1820 int ret = 0;
1821
1822 binder_proc_lock(proc);
1823 ref = binder_get_ref_for_node_olocked(proc, node, NULL);
1824 if (!ref) {
1825 binder_proc_unlock(proc);
1826 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1827 if (!new_ref)
1828 return -ENOMEM;
1829 binder_proc_lock(proc);
1830 ref = binder_get_ref_for_node_olocked(proc, node, new_ref);
1831 }
1832 ret = binder_inc_ref_olocked(ref, strong, target_list);
1833 *rdata = ref->data;
1834 binder_proc_unlock(proc);
1835 if (new_ref && ref != new_ref)
1836 /*
1837 * Another thread created the ref first so
1838 * free the one we allocated
1839 */
1840 kfree(new_ref);
1841 return ret;
1842}
1843
1844static void binder_pop_transaction_ilocked(struct binder_thread *target_thread,
1845 struct binder_transaction *t)
1846{
1847 BUG_ON(!target_thread);
1848 assert_spin_locked(&target_thread->proc->inner_lock);
1849 BUG_ON(target_thread->transaction_stack != t);
1850 BUG_ON(target_thread->transaction_stack->from != target_thread);
1851 target_thread->transaction_stack =
1852 target_thread->transaction_stack->from_parent;
1853 t->from = NULL;
1854}
1855
1856/**
1857 * binder_thread_dec_tmpref() - decrement thread->tmp_ref
1858 * @thread: thread to decrement
1859 *
1860 * A thread needs to be kept alive while being used to create or
1861 * handle a transaction. binder_get_txn_from() is used to safely
1862 * extract t->from from a binder_transaction and keep the thread
1863 * indicated by t->from from being freed. When done with that
1864 * binder_thread, this function is called to decrement the
1865 * tmp_ref and free if appropriate (thread has been released
1866 * and no transaction being processed by the driver)
1867 */
1868static void binder_thread_dec_tmpref(struct binder_thread *thread)
1869{
1870 /*
1871 * atomic is used to protect the counter value while
1872 * it cannot reach zero or thread->is_dead is false
1873 */
1874 binder_inner_proc_lock(thread->proc);
1875 atomic_dec(&thread->tmp_ref);
1876 if (thread->is_dead && !atomic_read(&thread->tmp_ref)) {
1877 binder_inner_proc_unlock(thread->proc);
1878 binder_free_thread(thread);
1879 return;
1880 }
1881 binder_inner_proc_unlock(thread->proc);
1882}
1883
1884/**
1885 * binder_proc_dec_tmpref() - decrement proc->tmp_ref
1886 * @proc: proc to decrement
1887 *
1888 * A binder_proc needs to be kept alive while being used to create or
1889 * handle a transaction. proc->tmp_ref is incremented when
1890 * creating a new transaction or the binder_proc is currently in-use
1891 * by threads that are being released. When done with the binder_proc,
1892 * this function is called to decrement the counter and free the
1893 * proc if appropriate (proc has been released, all threads have
1894 * been released and not currenly in-use to process a transaction).
1895 */
1896static void binder_proc_dec_tmpref(struct binder_proc *proc)
1897{
1898 binder_inner_proc_lock(proc);
1899 proc->tmp_ref--;
1900 if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) &&
1901 !proc->tmp_ref) {
1902 binder_inner_proc_unlock(proc);
1903 binder_free_proc(proc);
1904 return;
1905 }
1906 binder_inner_proc_unlock(proc);
1907}
1908
1909/**
1910 * binder_get_txn_from() - safely extract the "from" thread in transaction
1911 * @t: binder transaction for t->from
1912 *
1913 * Atomically return the "from" thread and increment the tmp_ref
1914 * count for the thread to ensure it stays alive until
1915 * binder_thread_dec_tmpref() is called.
1916 *
1917 * Return: the value of t->from
1918 */
1919static struct binder_thread *binder_get_txn_from(
1920 struct binder_transaction *t)
1921{
1922 struct binder_thread *from;
1923
1924 spin_lock(&t->lock);
1925 from = t->from;
1926 if (from)
1927 atomic_inc(&from->tmp_ref);
1928 spin_unlock(&t->lock);
1929 return from;
1930}
1931
1932/**
1933 * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock
1934 * @t: binder transaction for t->from
1935 *
1936 * Same as binder_get_txn_from() except it also acquires the proc->inner_lock
1937 * to guarantee that the thread cannot be released while operating on it.
1938 * The caller must call binder_inner_proc_unlock() to release the inner lock
1939 * as well as call binder_dec_thread_txn() to release the reference.
1940 *
1941 * Return: the value of t->from
1942 */
1943static struct binder_thread *binder_get_txn_from_and_acq_inner(
1944 struct binder_transaction *t)
1945{
1946 struct binder_thread *from;
1947
1948 from = binder_get_txn_from(t);
1949 if (!from)
1950 return NULL;
1951 binder_inner_proc_lock(from->proc);
1952 if (t->from) {
1953 BUG_ON(from != t->from);
1954 return from;
1955 }
1956 binder_inner_proc_unlock(from->proc);
1957 binder_thread_dec_tmpref(from);
1958 return NULL;
1959}
1960
1961static void binder_free_transaction(struct binder_transaction *t)
1962{
1963 if (t->buffer)
1964 t->buffer->transaction = NULL;
1965 kfree(t);
1966 binder_stats_deleted(BINDER_STAT_TRANSACTION);
1967}
1968
1969static void binder_send_failed_reply(struct binder_transaction *t,
1970 uint32_t error_code)
1971{
1972 struct binder_thread *target_thread;
1973 struct binder_transaction *next;
1974
1975 BUG_ON(t->flags & TF_ONE_WAY);
1976 while (1) {
1977 target_thread = binder_get_txn_from_and_acq_inner(t);
1978 if (target_thread) {
1979 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1980 "send failed reply for transaction %d to %d:%d\n",
1981 t->debug_id,
1982 target_thread->proc->pid,
1983 target_thread->pid);
1984
1985 binder_pop_transaction_ilocked(target_thread, t);
1986 if (target_thread->reply_error.cmd == BR_OK) {
1987 target_thread->reply_error.cmd = error_code;
1988 binder_enqueue_thread_work_ilocked(
1989 target_thread,
1990 &target_thread->reply_error.work);
1991 wake_up_interruptible(&target_thread->wait);
1992 } else {
1993 /*
1994 * Cannot get here for normal operation, but
1995 * we can if multiple synchronous transactions
1996 * are sent without blocking for responses.
1997 * Just ignore the 2nd error in this case.
1998 */
1999 pr_warn("Unexpected reply error: %u\n",
2000 target_thread->reply_error.cmd);
2001 }
2002 binder_inner_proc_unlock(target_thread->proc);
2003 binder_thread_dec_tmpref(target_thread);
2004 binder_free_transaction(t);
2005 return;
2006 }
2007 next = t->from_parent;
2008
2009 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2010 "send failed reply for transaction %d, target dead\n",
2011 t->debug_id);
2012
2013 binder_free_transaction(t);
2014 if (next == NULL) {
2015 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2016 "reply failed, no target thread at root\n");
2017 return;
2018 }
2019 t = next;
2020 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2021 "reply failed, no target thread -- retry %d\n",
2022 t->debug_id);
2023 }
2024}
2025
2026/**
2027 * binder_cleanup_transaction() - cleans up undelivered transaction
2028 * @t: transaction that needs to be cleaned up
2029 * @reason: reason the transaction wasn't delivered
2030 * @error_code: error to return to caller (if synchronous call)
2031 */
2032static void binder_cleanup_transaction(struct binder_transaction *t,
2033 const char *reason,
2034 uint32_t error_code)
2035{
2036 if (t->buffer->target_node && !(t->flags & TF_ONE_WAY)) {
2037 binder_send_failed_reply(t, error_code);
2038 } else {
2039 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2040 "undelivered transaction %d, %s\n",
2041 t->debug_id, reason);
2042 binder_free_transaction(t);
2043 }
2044}
2045
2046/**
2047 * binder_validate_object() - checks for a valid metadata object in a buffer.
2048 * @buffer: binder_buffer that we're parsing.
2049 * @offset: offset in the buffer at which to validate an object.
2050 *
2051 * Return: If there's a valid metadata object at @offset in @buffer, the
2052 * size of that object. Otherwise, it returns zero.
2053 */
2054static size_t binder_validate_object(struct binder_buffer *buffer, u64 offset)
2055{
2056 /* Check if we can read a header first */
2057 struct binder_object_header *hdr;
2058 size_t object_size = 0;
2059
2060 if (buffer->data_size < sizeof(*hdr) ||
2061 offset > buffer->data_size - sizeof(*hdr) ||
2062 !IS_ALIGNED(offset, sizeof(u32)))
2063 return 0;
2064
2065 /* Ok, now see if we can read a complete object. */
2066 hdr = (struct binder_object_header *)(buffer->data + offset);
2067 switch (hdr->type) {
2068 case BINDER_TYPE_BINDER:
2069 case BINDER_TYPE_WEAK_BINDER:
2070 case BINDER_TYPE_HANDLE:
2071 case BINDER_TYPE_WEAK_HANDLE:
2072 object_size = sizeof(struct flat_binder_object);
2073 break;
2074 case BINDER_TYPE_FD:
2075 object_size = sizeof(struct binder_fd_object);
2076 break;
2077 case BINDER_TYPE_PTR:
2078 object_size = sizeof(struct binder_buffer_object);
2079 break;
2080 case BINDER_TYPE_FDA:
2081 object_size = sizeof(struct binder_fd_array_object);
2082 break;
2083 default:
2084 return 0;
2085 }
2086 if (offset <= buffer->data_size - object_size &&
2087 buffer->data_size >= object_size)
2088 return object_size;
2089 else
2090 return 0;
2091}
2092
2093/**
2094 * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
2095 * @b: binder_buffer containing the object
2096 * @index: index in offset array at which the binder_buffer_object is
2097 * located
2098 * @start: points to the start of the offset array
2099 * @num_valid: the number of valid offsets in the offset array
2100 *
2101 * Return: If @index is within the valid range of the offset array
2102 * described by @start and @num_valid, and if there's a valid
2103 * binder_buffer_object at the offset found in index @index
2104 * of the offset array, that object is returned. Otherwise,
2105 * %NULL is returned.
2106 * Note that the offset found in index @index itself is not
2107 * verified; this function assumes that @num_valid elements
2108 * from @start were previously verified to have valid offsets.
2109 */
2110static struct binder_buffer_object *binder_validate_ptr(struct binder_buffer *b,
2111 binder_size_t index,
2112 binder_size_t *start,
2113 binder_size_t num_valid)
2114{
2115 struct binder_buffer_object *buffer_obj;
2116 binder_size_t *offp;
2117
2118 if (index >= num_valid)
2119 return NULL;
2120
2121 offp = start + index;
2122 buffer_obj = (struct binder_buffer_object *)(b->data + *offp);
2123 if (buffer_obj->hdr.type != BINDER_TYPE_PTR)
2124 return NULL;
2125
2126 return buffer_obj;
2127}
2128
2129/**
2130 * binder_validate_fixup() - validates pointer/fd fixups happen in order.
2131 * @b: transaction buffer
2132 * @objects_start start of objects buffer
2133 * @buffer: binder_buffer_object in which to fix up
2134 * @offset: start offset in @buffer to fix up
2135 * @last_obj: last binder_buffer_object that we fixed up in
2136 * @last_min_offset: minimum fixup offset in @last_obj
2137 *
2138 * Return: %true if a fixup in buffer @buffer at offset @offset is
2139 * allowed.
2140 *
2141 * For safety reasons, we only allow fixups inside a buffer to happen
2142 * at increasing offsets; additionally, we only allow fixup on the last
2143 * buffer object that was verified, or one of its parents.
2144 *
2145 * Example of what is allowed:
2146 *
2147 * A
2148 * B (parent = A, offset = 0)
2149 * C (parent = A, offset = 16)
2150 * D (parent = C, offset = 0)
2151 * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
2152 *
2153 * Examples of what is not allowed:
2154 *
2155 * Decreasing offsets within the same parent:
2156 * A
2157 * C (parent = A, offset = 16)
2158 * B (parent = A, offset = 0) // decreasing offset within A
2159 *
2160 * Referring to a parent that wasn't the last object or any of its parents:
2161 * A
2162 * B (parent = A, offset = 0)
2163 * C (parent = A, offset = 0)
2164 * C (parent = A, offset = 16)
2165 * D (parent = B, offset = 0) // B is not A or any of A's parents
2166 */
2167static bool binder_validate_fixup(struct binder_buffer *b,
2168 binder_size_t *objects_start,
2169 struct binder_buffer_object *buffer,
2170 binder_size_t fixup_offset,
2171 struct binder_buffer_object *last_obj,
2172 binder_size_t last_min_offset)
2173{
2174 if (!last_obj) {
2175 /* Nothing to fix up in */
2176 return false;
2177 }
2178
2179 while (last_obj != buffer) {
2180 /*
2181 * Safe to retrieve the parent of last_obj, since it
2182 * was already previously verified by the driver.
2183 */
2184 if ((last_obj->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0)
2185 return false;
2186 last_min_offset = last_obj->parent_offset + sizeof(uintptr_t);
2187 last_obj = (struct binder_buffer_object *)
2188 (b->data + *(objects_start + last_obj->parent));
2189 }
2190 return (fixup_offset >= last_min_offset);
2191}
2192
2193static void binder_transaction_buffer_release(struct binder_proc *proc,
2194 struct binder_buffer *buffer,
2195 binder_size_t *failed_at)
2196{
2197 binder_size_t *offp, *off_start, *off_end;
2198 int debug_id = buffer->debug_id;
2199
2200 binder_debug(BINDER_DEBUG_TRANSACTION,
2201 "%d buffer release %d, size %zd-%zd, failed at %pK\n",
2202 proc->pid, buffer->debug_id,
2203 buffer->data_size, buffer->offsets_size, failed_at);
2204
2205 if (buffer->target_node)
2206 binder_dec_node(buffer->target_node, 1, 0);
2207
2208 off_start = (binder_size_t *)(buffer->data +
2209 ALIGN(buffer->data_size, sizeof(void *)));
2210 if (failed_at)
2211 off_end = failed_at;
2212 else
2213 off_end = (void *)off_start + buffer->offsets_size;
2214 for (offp = off_start; offp < off_end; offp++) {
2215 struct binder_object_header *hdr;
2216 size_t object_size = binder_validate_object(buffer, *offp);
2217
2218 if (object_size == 0) {
2219 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
2220 debug_id, (u64)*offp, buffer->data_size);
2221 continue;
2222 }
2223 hdr = (struct binder_object_header *)(buffer->data + *offp);
2224 switch (hdr->type) {
2225 case BINDER_TYPE_BINDER:
2226 case BINDER_TYPE_WEAK_BINDER: {
2227 struct flat_binder_object *fp;
2228 struct binder_node *node;
2229
2230 fp = to_flat_binder_object(hdr);
2231 node = binder_get_node(proc, fp->binder);
2232 if (node == NULL) {
2233 pr_err("transaction release %d bad node %016llx\n",
2234 debug_id, (u64)fp->binder);
2235 break;
2236 }
2237 binder_debug(BINDER_DEBUG_TRANSACTION,
2238 " node %d u%016llx\n",
2239 node->debug_id, (u64)node->ptr);
2240 binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER,
2241 0);
2242 binder_put_node(node);
2243 } break;
2244 case BINDER_TYPE_HANDLE:
2245 case BINDER_TYPE_WEAK_HANDLE: {
2246 struct flat_binder_object *fp;
2247 struct binder_ref_data rdata;
2248 int ret;
2249
2250 fp = to_flat_binder_object(hdr);
2251 ret = binder_dec_ref_for_handle(proc, fp->handle,
2252 hdr->type == BINDER_TYPE_HANDLE, &rdata);
2253
2254 if (ret) {
2255 pr_err("transaction release %d bad handle %d, ret = %d\n",
2256 debug_id, fp->handle, ret);
2257 break;
2258 }
2259 binder_debug(BINDER_DEBUG_TRANSACTION,
2260 " ref %d desc %d\n",
2261 rdata.debug_id, rdata.desc);
2262 } break;
2263
2264 case BINDER_TYPE_FD: {
2265 struct binder_fd_object *fp = to_binder_fd_object(hdr);
2266
2267 binder_debug(BINDER_DEBUG_TRANSACTION,
2268 " fd %d\n", fp->fd);
2269 if (failed_at)
2270 task_close_fd(proc, fp->fd);
2271 } break;
2272 case BINDER_TYPE_PTR:
2273 /*
2274 * Nothing to do here, this will get cleaned up when the
2275 * transaction buffer gets freed
2276 */
2277 break;
2278 case BINDER_TYPE_FDA: {
2279 struct binder_fd_array_object *fda;
2280 struct binder_buffer_object *parent;
2281 uintptr_t parent_buffer;
2282 u32 *fd_array;
2283 size_t fd_index;
2284 binder_size_t fd_buf_size;
2285
2286 fda = to_binder_fd_array_object(hdr);
2287 parent = binder_validate_ptr(buffer, fda->parent,
2288 off_start,
2289 offp - off_start);
2290 if (!parent) {
2291 pr_err("transaction release %d bad parent offset\n",
2292 debug_id);
2293 continue;
2294 }
2295 /*
2296 * Since the parent was already fixed up, convert it
2297 * back to kernel address space to access it
2298 */
2299 parent_buffer = parent->buffer -
2300 binder_alloc_get_user_buffer_offset(
2301 &proc->alloc);
2302
2303 fd_buf_size = sizeof(u32) * fda->num_fds;
2304 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2305 pr_err("transaction release %d invalid number of fds (%lld)\n",
2306 debug_id, (u64)fda->num_fds);
2307 continue;
2308 }
2309 if (fd_buf_size > parent->length ||
2310 fda->parent_offset > parent->length - fd_buf_size) {
2311 /* No space for all file descriptors here. */
2312 pr_err("transaction release %d not enough space for %lld fds in buffer\n",
2313 debug_id, (u64)fda->num_fds);
2314 continue;
2315 }
2316 fd_array = (u32 *)(parent_buffer + (uintptr_t)fda->parent_offset);
2317 for (fd_index = 0; fd_index < fda->num_fds; fd_index++)
2318 task_close_fd(proc, fd_array[fd_index]);
2319 } break;
2320 default:
2321 pr_err("transaction release %d bad object type %x\n",
2322 debug_id, hdr->type);
2323 break;
2324 }
2325 }
2326}
2327
2328static int binder_translate_binder(struct flat_binder_object *fp,
2329 struct binder_transaction *t,
2330 struct binder_thread *thread)
2331{
2332 struct binder_node *node;
2333 struct binder_proc *proc = thread->proc;
2334 struct binder_proc *target_proc = t->to_proc;
2335 struct binder_ref_data rdata;
2336 int ret = 0;
2337
2338 node = binder_get_node(proc, fp->binder);
2339 if (!node) {
2340 node = binder_new_node(proc, fp);
2341 if (!node)
2342 return -ENOMEM;
2343 }
2344 if (fp->cookie != node->cookie) {
2345 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
2346 proc->pid, thread->pid, (u64)fp->binder,
2347 node->debug_id, (u64)fp->cookie,
2348 (u64)node->cookie);
2349 ret = -EINVAL;
2350 goto done;
2351 }
2352 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2353 ret = -EPERM;
2354 goto done;
2355 }
2356
2357 ret = binder_inc_ref_for_node(target_proc, node,
2358 fp->hdr.type == BINDER_TYPE_BINDER,
2359 &thread->todo, &rdata);
2360 if (ret)
2361 goto done;
2362
2363 if (fp->hdr.type == BINDER_TYPE_BINDER)
2364 fp->hdr.type = BINDER_TYPE_HANDLE;
2365 else
2366 fp->hdr.type = BINDER_TYPE_WEAK_HANDLE;
2367 fp->binder = 0;
2368 fp->handle = rdata.desc;
2369 fp->cookie = 0;
2370
2371 trace_binder_transaction_node_to_ref(t, node, &rdata);
2372 binder_debug(BINDER_DEBUG_TRANSACTION,
2373 " node %d u%016llx -> ref %d desc %d\n",
2374 node->debug_id, (u64)node->ptr,
2375 rdata.debug_id, rdata.desc);
2376done:
2377 binder_put_node(node);
2378 return ret;
2379}
2380
2381static int binder_translate_handle(struct flat_binder_object *fp,
2382 struct binder_transaction *t,
2383 struct binder_thread *thread)
2384{
2385 struct binder_proc *proc = thread->proc;
2386 struct binder_proc *target_proc = t->to_proc;
2387 struct binder_node *node;
2388 struct binder_ref_data src_rdata;
2389 int ret = 0;
2390
2391 node = binder_get_node_from_ref(proc, fp->handle,
2392 fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata);
2393 if (!node) {
2394 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
2395 proc->pid, thread->pid, fp->handle);
2396 return -EINVAL;
2397 }
2398 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2399 ret = -EPERM;
2400 goto done;
2401 }
2402
2403 binder_node_lock(node);
2404 if (node->proc == target_proc) {
2405 if (fp->hdr.type == BINDER_TYPE_HANDLE)
2406 fp->hdr.type = BINDER_TYPE_BINDER;
2407 else
2408 fp->hdr.type = BINDER_TYPE_WEAK_BINDER;
2409 fp->binder = node->ptr;
2410 fp->cookie = node->cookie;
2411 if (node->proc)
2412 binder_inner_proc_lock(node->proc);
2413 binder_inc_node_nilocked(node,
2414 fp->hdr.type == BINDER_TYPE_BINDER,
2415 0, NULL);
2416 if (node->proc)
2417 binder_inner_proc_unlock(node->proc);
2418 trace_binder_transaction_ref_to_node(t, node, &src_rdata);
2419 binder_debug(BINDER_DEBUG_TRANSACTION,
2420 " ref %d desc %d -> node %d u%016llx\n",
2421 src_rdata.debug_id, src_rdata.desc, node->debug_id,
2422 (u64)node->ptr);
2423 binder_node_unlock(node);
2424 } else {
2425 struct binder_ref_data dest_rdata;
2426
2427 binder_node_unlock(node);
2428 ret = binder_inc_ref_for_node(target_proc, node,
2429 fp->hdr.type == BINDER_TYPE_HANDLE,
2430 NULL, &dest_rdata);
2431 if (ret)
2432 goto done;
2433
2434 fp->binder = 0;
2435 fp->handle = dest_rdata.desc;
2436 fp->cookie = 0;
2437 trace_binder_transaction_ref_to_ref(t, node, &src_rdata,
2438 &dest_rdata);
2439 binder_debug(BINDER_DEBUG_TRANSACTION,
2440 " ref %d desc %d -> ref %d desc %d (node %d)\n",
2441 src_rdata.debug_id, src_rdata.desc,
2442 dest_rdata.debug_id, dest_rdata.desc,
2443 node->debug_id);
2444 }
2445done:
2446 binder_put_node(node);
2447 return ret;
2448}
2449
2450static int binder_translate_fd(int fd,
2451 struct binder_transaction *t,
2452 struct binder_thread *thread,
2453 struct binder_transaction *in_reply_to)
2454{
2455 struct binder_proc *proc = thread->proc;
2456 struct binder_proc *target_proc = t->to_proc;
2457 int target_fd;
2458 struct file *file;
2459 int ret;
2460 bool target_allows_fd;
2461
2462 if (in_reply_to)
2463 target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS);
2464 else
2465 target_allows_fd = t->buffer->target_node->accept_fds;
2466 if (!target_allows_fd) {
2467 binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
2468 proc->pid, thread->pid,
2469 in_reply_to ? "reply" : "transaction",
2470 fd);
2471 ret = -EPERM;
2472 goto err_fd_not_accepted;
2473 }
2474
2475 file = fget(fd);
2476 if (!file) {
2477 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
2478 proc->pid, thread->pid, fd);
2479 ret = -EBADF;
2480 goto err_fget;
2481 }
2482 ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file);
2483 if (ret < 0) {
2484 ret = -EPERM;
2485 goto err_security;
2486 }
2487
2488 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
2489 if (target_fd < 0) {
2490 ret = -ENOMEM;
2491 goto err_get_unused_fd;
2492 }
2493 task_fd_install(target_proc, target_fd, file);
2494 trace_binder_transaction_fd(t, fd, target_fd);
2495 binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n",
2496 fd, target_fd);
2497
2498 return target_fd;
2499
2500err_get_unused_fd:
2501err_security:
2502 fput(file);
2503err_fget:
2504err_fd_not_accepted:
2505 return ret;
2506}
2507
2508static int binder_translate_fd_array(struct binder_fd_array_object *fda,
2509 struct binder_buffer_object *parent,
2510 struct binder_transaction *t,
2511 struct binder_thread *thread,
2512 struct binder_transaction *in_reply_to)
2513{
2514 binder_size_t fdi, fd_buf_size, num_installed_fds;
2515 int target_fd;
2516 uintptr_t parent_buffer;
2517 u32 *fd_array;
2518 struct binder_proc *proc = thread->proc;
2519 struct binder_proc *target_proc = t->to_proc;
2520
2521 fd_buf_size = sizeof(u32) * fda->num_fds;
2522 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2523 binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
2524 proc->pid, thread->pid, (u64)fda->num_fds);
2525 return -EINVAL;
2526 }
2527 if (fd_buf_size > parent->length ||
2528 fda->parent_offset > parent->length - fd_buf_size) {
2529 /* No space for all file descriptors here. */
2530 binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
2531 proc->pid, thread->pid, (u64)fda->num_fds);
2532 return -EINVAL;
2533 }
2534 /*
2535 * Since the parent was already fixed up, convert it
2536 * back to the kernel address space to access it
2537 */
2538 parent_buffer = parent->buffer -
2539 binder_alloc_get_user_buffer_offset(&target_proc->alloc);
2540 fd_array = (u32 *)(parent_buffer + (uintptr_t)fda->parent_offset);
2541 if (!IS_ALIGNED((unsigned long)fd_array, sizeof(u32))) {
2542 binder_user_error("%d:%d parent offset not aligned correctly.\n",
2543 proc->pid, thread->pid);
2544 return -EINVAL;
2545 }
2546 for (fdi = 0; fdi < fda->num_fds; fdi++) {
2547 target_fd = binder_translate_fd(fd_array[fdi], t, thread,
2548 in_reply_to);
2549 if (target_fd < 0)
2550 goto err_translate_fd_failed;
2551 fd_array[fdi] = target_fd;
2552 }
2553 return 0;
2554
2555err_translate_fd_failed:
2556 /*
2557 * Failed to allocate fd or security error, free fds
2558 * installed so far.
2559 */
2560 num_installed_fds = fdi;
2561 for (fdi = 0; fdi < num_installed_fds; fdi++)
2562 task_close_fd(target_proc, fd_array[fdi]);
2563 return target_fd;
2564}
2565
2566static int binder_fixup_parent(struct binder_transaction *t,
2567 struct binder_thread *thread,
2568 struct binder_buffer_object *bp,
2569 binder_size_t *off_start,
2570 binder_size_t num_valid,
2571 struct binder_buffer_object *last_fixup_obj,
2572 binder_size_t last_fixup_min_off)
2573{
2574 struct binder_buffer_object *parent;
2575 u8 *parent_buffer;
2576 struct binder_buffer *b = t->buffer;
2577 struct binder_proc *proc = thread->proc;
2578 struct binder_proc *target_proc = t->to_proc;
2579
2580 if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT))
2581 return 0;
2582
2583 parent = binder_validate_ptr(b, bp->parent, off_start, num_valid);
2584 if (!parent) {
2585 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
2586 proc->pid, thread->pid);
2587 return -EINVAL;
2588 }
2589
2590 if (!binder_validate_fixup(b, off_start,
2591 parent, bp->parent_offset,
2592 last_fixup_obj,
2593 last_fixup_min_off)) {
2594 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
2595 proc->pid, thread->pid);
2596 return -EINVAL;
2597 }
2598
2599 if (parent->length < sizeof(binder_uintptr_t) ||
2600 bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) {
2601 /* No space for a pointer here! */
2602 binder_user_error("%d:%d got transaction with invalid parent offset\n",
2603 proc->pid, thread->pid);
2604 return -EINVAL;
2605 }
2606 parent_buffer = (u8 *)((uintptr_t)parent->buffer -
2607 binder_alloc_get_user_buffer_offset(
2608 &target_proc->alloc));
2609 *(binder_uintptr_t *)(parent_buffer + bp->parent_offset) = bp->buffer;
2610
2611 return 0;
2612}
2613
2614/**
2615 * binder_proc_transaction() - sends a transaction to a process and wakes it up
2616 * @t: transaction to send
2617 * @proc: process to send the transaction to
2618 * @thread: thread in @proc to send the transaction to (may be NULL)
2619 *
2620 * This function queues a transaction to the specified process. It will try
2621 * to find a thread in the target process to handle the transaction and
2622 * wake it up. If no thread is found, the work is queued to the proc
2623 * waitqueue.
2624 *
2625 * If the @thread parameter is not NULL, the transaction is always queued
2626 * to the waitlist of that specific thread.
2627 *
2628 * Return: true if the transactions was successfully queued
2629 * false if the target process or thread is dead
2630 */
2631static bool binder_proc_transaction(struct binder_transaction *t,
2632 struct binder_proc *proc,
2633 struct binder_thread *thread)
2634{
2635 struct binder_node *node = t->buffer->target_node;
2636 bool oneway = !!(t->flags & TF_ONE_WAY);
2637 bool pending_async = false;
2638
2639 BUG_ON(!node);
2640 binder_node_lock(node);
2641 if (oneway) {
2642 BUG_ON(thread);
2643 if (node->has_async_transaction) {
2644 pending_async = true;
2645 } else {
2646 node->has_async_transaction = true;
2647 }
2648 }
2649
2650 binder_inner_proc_lock(proc);
2651
2652 if (proc->is_dead || (thread && thread->is_dead)) {
2653 binder_inner_proc_unlock(proc);
2654 binder_node_unlock(node);
2655 return false;
2656 }
2657
2658 if (!thread && !pending_async)
2659 thread = binder_select_thread_ilocked(proc);
2660
2661 if (thread)
2662 binder_enqueue_thread_work_ilocked(thread, &t->work);
2663 else if (!pending_async)
2664 binder_enqueue_work_ilocked(&t->work, &proc->todo);
2665 else
2666 binder_enqueue_work_ilocked(&t->work, &node->async_todo);
2667
2668 if (!pending_async)
2669 binder_wakeup_thread_ilocked(proc, thread, !oneway /* sync */);
2670
2671 binder_inner_proc_unlock(proc);
2672 binder_node_unlock(node);
2673
2674 return true;
2675}
2676
2677/**
2678 * binder_get_node_refs_for_txn() - Get required refs on node for txn
2679 * @node: struct binder_node for which to get refs
2680 * @proc: returns @node->proc if valid
2681 * @error: if no @proc then returns BR_DEAD_REPLY
2682 *
2683 * User-space normally keeps the node alive when creating a transaction
2684 * since it has a reference to the target. The local strong ref keeps it
2685 * alive if the sending process dies before the target process processes
2686 * the transaction. If the source process is malicious or has a reference
2687 * counting bug, relying on the local strong ref can fail.
2688 *
2689 * Since user-space can cause the local strong ref to go away, we also take
2690 * a tmpref on the node to ensure it survives while we are constructing
2691 * the transaction. We also need a tmpref on the proc while we are
2692 * constructing the transaction, so we take that here as well.
2693 *
2694 * Return: The target_node with refs taken or NULL if no @node->proc is NULL.
2695 * Also sets @proc if valid. If the @node->proc is NULL indicating that the
2696 * target proc has died, @error is set to BR_DEAD_REPLY
2697 */
2698static struct binder_node *binder_get_node_refs_for_txn(
2699 struct binder_node *node,
2700 struct binder_proc **procp,
2701 uint32_t *error)
2702{
2703 struct binder_node *target_node = NULL;
2704
2705 binder_node_inner_lock(node);
2706 if (node->proc) {
2707 target_node = node;
2708 binder_inc_node_nilocked(node, 1, 0, NULL);
2709 binder_inc_node_tmpref_ilocked(node);
2710 node->proc->tmp_ref++;
2711 *procp = node->proc;
2712 } else
2713 *error = BR_DEAD_REPLY;
2714 binder_node_inner_unlock(node);
2715
2716 return target_node;
2717}
2718
2719static void binder_transaction(struct binder_proc *proc,
2720 struct binder_thread *thread,
2721 struct binder_transaction_data *tr, int reply,
2722 binder_size_t extra_buffers_size)
2723{
2724 int ret;
2725 struct binder_transaction *t;
2726 struct binder_work *tcomplete;
2727 binder_size_t *offp, *off_end, *off_start;
2728 binder_size_t off_min;
2729 u8 *sg_bufp, *sg_buf_end;
2730 struct binder_proc *target_proc = NULL;
2731 struct binder_thread *target_thread = NULL;
2732 struct binder_node *target_node = NULL;
2733 struct binder_transaction *in_reply_to = NULL;
2734 struct binder_transaction_log_entry *e;
2735 uint32_t return_error = 0;
2736 uint32_t return_error_param = 0;
2737 uint32_t return_error_line = 0;
2738 struct binder_buffer_object *last_fixup_obj = NULL;
2739 binder_size_t last_fixup_min_off = 0;
2740 struct binder_context *context = proc->context;
2741 int t_debug_id = atomic_inc_return(&binder_last_id);
2742
2743 e = binder_transaction_log_add(&binder_transaction_log);
2744 e->debug_id = t_debug_id;
2745 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
2746 e->from_proc = proc->pid;
2747 e->from_thread = thread->pid;
2748 e->target_handle = tr->target.handle;
2749 e->data_size = tr->data_size;
2750 e->offsets_size = tr->offsets_size;
2751 e->context_name = proc->context->name;
2752
2753 if (reply) {
2754 binder_inner_proc_lock(proc);
2755 in_reply_to = thread->transaction_stack;
2756 if (in_reply_to == NULL) {
2757 binder_inner_proc_unlock(proc);
2758 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
2759 proc->pid, thread->pid);
2760 return_error = BR_FAILED_REPLY;
2761 return_error_param = -EPROTO;
2762 return_error_line = __LINE__;
2763 goto err_empty_call_stack;
2764 }
2765 if (in_reply_to->to_thread != thread) {
2766 spin_lock(&in_reply_to->lock);
2767 binder_user_error("%d:%d got reply transaction with bad transaction stack, transaction %d has target %d:%d\n",
2768 proc->pid, thread->pid, in_reply_to->debug_id,
2769 in_reply_to->to_proc ?
2770 in_reply_to->to_proc->pid : 0,
2771 in_reply_to->to_thread ?
2772 in_reply_to->to_thread->pid : 0);
2773 spin_unlock(&in_reply_to->lock);
2774 binder_inner_proc_unlock(proc);
2775 return_error = BR_FAILED_REPLY;
2776 return_error_param = -EPROTO;
2777 return_error_line = __LINE__;
2778 in_reply_to = NULL;
2779 goto err_bad_call_stack;
2780 }
2781 thread->transaction_stack = in_reply_to->to_parent;
2782 binder_inner_proc_unlock(proc);
2783 binder_set_nice(in_reply_to->saved_priority);
2784 target_thread = binder_get_txn_from_and_acq_inner(in_reply_to);
2785 if (target_thread == NULL) {
2786 return_error = BR_DEAD_REPLY;
2787 return_error_line = __LINE__;
2788 goto err_dead_binder;
2789 }
2790 if (target_thread->transaction_stack != in_reply_to) {
2791 binder_user_error("%d:%d got reply transaction with bad target transaction stack %d, expected %d\n",
2792 proc->pid, thread->pid,
2793 target_thread->transaction_stack ?
2794 target_thread->transaction_stack->debug_id : 0,
2795 in_reply_to->debug_id);
2796 binder_inner_proc_unlock(target_thread->proc);
2797 return_error = BR_FAILED_REPLY;
2798 return_error_param = -EPROTO;
2799 return_error_line = __LINE__;
2800 in_reply_to = NULL;
2801 target_thread = NULL;
2802 goto err_dead_binder;
2803 }
2804 target_proc = target_thread->proc;
2805 target_proc->tmp_ref++;
2806 binder_inner_proc_unlock(target_thread->proc);
2807 } else {
2808 if (tr->target.handle) {
2809 struct binder_ref *ref;
2810
2811 /*
2812 * There must already be a strong ref
2813 * on this node. If so, do a strong
2814 * increment on the node to ensure it
2815 * stays alive until the transaction is
2816 * done.
2817 */
2818 binder_proc_lock(proc);
2819 ref = binder_get_ref_olocked(proc, tr->target.handle,
2820 true);
2821 if (ref) {
2822 target_node = binder_get_node_refs_for_txn(
2823 ref->node, &target_proc,
2824 &return_error);
2825 } else {
2826 binder_user_error("%d:%d got transaction to invalid handle\n",
2827 proc->pid, thread->pid);
2828 return_error = BR_FAILED_REPLY;
2829 }
2830 binder_proc_unlock(proc);
2831 } else {
2832 mutex_lock(&context->context_mgr_node_lock);
2833 target_node = context->binder_context_mgr_node;
2834 if (target_node)
2835 target_node = binder_get_node_refs_for_txn(
2836 target_node, &target_proc,
2837 &return_error);
2838 else
2839 return_error = BR_DEAD_REPLY;
2840 mutex_unlock(&context->context_mgr_node_lock);
2841 if (target_node && target_proc == proc) {
2842 binder_user_error("%d:%d got transaction to context manager from process owning it\n",
2843 proc->pid, thread->pid);
2844 return_error = BR_FAILED_REPLY;
2845 return_error_param = -EINVAL;
2846 return_error_line = __LINE__;
2847 goto err_invalid_target_handle;
2848 }
2849 }
2850 if (!target_node) {
2851 /*
2852 * return_error is set above
2853 */
2854 return_error_param = -EINVAL;
2855 return_error_line = __LINE__;
2856 goto err_dead_binder;
2857 }
2858 e->to_node = target_node->debug_id;
2859 if (security_binder_transaction(proc->tsk,
2860 target_proc->tsk) < 0) {
2861 return_error = BR_FAILED_REPLY;
2862 return_error_param = -EPERM;
2863 return_error_line = __LINE__;
2864 goto err_invalid_target_handle;
2865 }
2866 binder_inner_proc_lock(proc);
2867 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
2868 struct binder_transaction *tmp;
2869
2870 tmp = thread->transaction_stack;
2871 if (tmp->to_thread != thread) {
2872 spin_lock(&tmp->lock);
2873 binder_user_error("%d:%d got new transaction with bad transaction stack, transaction %d has target %d:%d\n",
2874 proc->pid, thread->pid, tmp->debug_id,
2875 tmp->to_proc ? tmp->to_proc->pid : 0,
2876 tmp->to_thread ?
2877 tmp->to_thread->pid : 0);
2878 spin_unlock(&tmp->lock);
2879 binder_inner_proc_unlock(proc);
2880 return_error = BR_FAILED_REPLY;
2881 return_error_param = -EPROTO;
2882 return_error_line = __LINE__;
2883 goto err_bad_call_stack;
2884 }
2885 while (tmp) {
2886 struct binder_thread *from;
2887
2888 spin_lock(&tmp->lock);
2889 from = tmp->from;
2890 if (from && from->proc == target_proc) {
2891 atomic_inc(&from->tmp_ref);
2892 target_thread = from;
2893 spin_unlock(&tmp->lock);
2894 break;
2895 }
2896 spin_unlock(&tmp->lock);
2897 tmp = tmp->from_parent;
2898 }
2899 }
2900 binder_inner_proc_unlock(proc);
2901 }
2902 if (target_thread)
2903 e->to_thread = target_thread->pid;
2904 e->to_proc = target_proc->pid;
2905
2906 /* TODO: reuse incoming transaction for reply */
2907 t = kzalloc(sizeof(*t), GFP_KERNEL);
2908 if (t == NULL) {
2909 return_error = BR_FAILED_REPLY;
2910 return_error_param = -ENOMEM;
2911 return_error_line = __LINE__;
2912 goto err_alloc_t_failed;
2913 }
2914 binder_stats_created(BINDER_STAT_TRANSACTION);
2915 spin_lock_init(&t->lock);
2916
2917 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
2918 if (tcomplete == NULL) {
2919 return_error = BR_FAILED_REPLY;
2920 return_error_param = -ENOMEM;
2921 return_error_line = __LINE__;
2922 goto err_alloc_tcomplete_failed;
2923 }
2924 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
2925
2926 t->debug_id = t_debug_id;
2927
2928 if (reply)
2929 binder_debug(BINDER_DEBUG_TRANSACTION,
2930 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
2931 proc->pid, thread->pid, t->debug_id,
2932 target_proc->pid, target_thread->pid,
2933 (u64)tr->data.ptr.buffer,
2934 (u64)tr->data.ptr.offsets,
2935 (u64)tr->data_size, (u64)tr->offsets_size,
2936 (u64)extra_buffers_size);
2937 else
2938 binder_debug(BINDER_DEBUG_TRANSACTION,
2939 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
2940 proc->pid, thread->pid, t->debug_id,
2941 target_proc->pid, target_node->debug_id,
2942 (u64)tr->data.ptr.buffer,
2943 (u64)tr->data.ptr.offsets,
2944 (u64)tr->data_size, (u64)tr->offsets_size,
2945 (u64)extra_buffers_size);
2946
2947 if (!reply && !(tr->flags & TF_ONE_WAY))
2948 t->from = thread;
2949 else
2950 t->from = NULL;
2951 t->sender_euid = task_euid(proc->tsk);
2952 t->to_proc = target_proc;
2953 t->to_thread = target_thread;
2954 t->code = tr->code;
2955 t->flags = tr->flags;
2956 t->priority = task_nice(current);
2957
2958 trace_binder_transaction(reply, t, target_node);
2959
2960 t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size,
2961 tr->offsets_size, extra_buffers_size,
2962 !reply && (t->flags & TF_ONE_WAY));
2963 if (IS_ERR(t->buffer)) {
2964 /*
2965 * -ESRCH indicates VMA cleared. The target is dying.
2966 */
2967 return_error_param = PTR_ERR(t->buffer);
2968 return_error = return_error_param == -ESRCH ?
2969 BR_DEAD_REPLY : BR_FAILED_REPLY;
2970 return_error_line = __LINE__;
2971 t->buffer = NULL;
2972 goto err_binder_alloc_buf_failed;
2973 }
2974 t->buffer->debug_id = t->debug_id;
2975 t->buffer->transaction = t;
2976 t->buffer->target_node = target_node;
2977 trace_binder_transaction_alloc_buf(t->buffer);
2978 off_start = (binder_size_t *)(t->buffer->data +
2979 ALIGN(tr->data_size, sizeof(void *)));
2980 offp = off_start;
2981
2982 if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t)
2983 tr->data.ptr.buffer, tr->data_size)) {
2984 binder_user_error("%d:%d got transaction with invalid data ptr\n",
2985 proc->pid, thread->pid);
2986 return_error = BR_FAILED_REPLY;
2987 return_error_param = -EFAULT;
2988 return_error_line = __LINE__;
2989 goto err_copy_data_failed;
2990 }
2991 if (copy_from_user(offp, (const void __user *)(uintptr_t)
2992 tr->data.ptr.offsets, tr->offsets_size)) {
2993 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
2994 proc->pid, thread->pid);
2995 return_error = BR_FAILED_REPLY;
2996 return_error_param = -EFAULT;
2997 return_error_line = __LINE__;
2998 goto err_copy_data_failed;
2999 }
3000 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
3001 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
3002 proc->pid, thread->pid, (u64)tr->offsets_size);
3003 return_error = BR_FAILED_REPLY;
3004 return_error_param = -EINVAL;
3005 return_error_line = __LINE__;
3006 goto err_bad_offset;
3007 }
3008 if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) {
3009 binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
3010 proc->pid, thread->pid,
3011 (u64)extra_buffers_size);
3012 return_error = BR_FAILED_REPLY;
3013 return_error_param = -EINVAL;
3014 return_error_line = __LINE__;
3015 goto err_bad_offset;
3016 }
3017 off_end = (void *)off_start + tr->offsets_size;
3018 sg_bufp = (u8 *)(PTR_ALIGN(off_end, sizeof(void *)));
3019 sg_buf_end = sg_bufp + extra_buffers_size;
3020 off_min = 0;
3021 for (; offp < off_end; offp++) {
3022 struct binder_object_header *hdr;
3023 size_t object_size = binder_validate_object(t->buffer, *offp);
3024
3025 if (object_size == 0 || *offp < off_min) {
3026 binder_user_error("%d:%d got transaction with invalid offset (%lld, min %lld max %lld) or object.\n",
3027 proc->pid, thread->pid, (u64)*offp,
3028 (u64)off_min,
3029 (u64)t->buffer->data_size);
3030 return_error = BR_FAILED_REPLY;
3031 return_error_param = -EINVAL;
3032 return_error_line = __LINE__;
3033 goto err_bad_offset;
3034 }
3035
3036 hdr = (struct binder_object_header *)(t->buffer->data + *offp);
3037 off_min = *offp + object_size;
3038 switch (hdr->type) {
3039 case BINDER_TYPE_BINDER:
3040 case BINDER_TYPE_WEAK_BINDER: {
3041 struct flat_binder_object *fp;
3042
3043 fp = to_flat_binder_object(hdr);
3044 ret = binder_translate_binder(fp, t, thread);
3045 if (ret < 0) {
3046 return_error = BR_FAILED_REPLY;
3047 return_error_param = ret;
3048 return_error_line = __LINE__;
3049 goto err_translate_failed;
3050 }
3051 } break;
3052 case BINDER_TYPE_HANDLE:
3053 case BINDER_TYPE_WEAK_HANDLE: {
3054 struct flat_binder_object *fp;
3055
3056 fp = to_flat_binder_object(hdr);
3057 ret = binder_translate_handle(fp, t, thread);
3058 if (ret < 0) {
3059 return_error = BR_FAILED_REPLY;
3060 return_error_param = ret;
3061 return_error_line = __LINE__;
3062 goto err_translate_failed;
3063 }
3064 } break;
3065
3066 case BINDER_TYPE_FD: {
3067 struct binder_fd_object *fp = to_binder_fd_object(hdr);
3068 int target_fd = binder_translate_fd(fp->fd, t, thread,
3069 in_reply_to);
3070
3071 if (target_fd < 0) {
3072 return_error = BR_FAILED_REPLY;
3073 return_error_param = target_fd;
3074 return_error_line = __LINE__;
3075 goto err_translate_failed;
3076 }
3077 fp->pad_binder = 0;
3078 fp->fd = target_fd;
3079 } break;
3080 case BINDER_TYPE_FDA: {
3081 struct binder_fd_array_object *fda =
3082 to_binder_fd_array_object(hdr);
3083 struct binder_buffer_object *parent =
3084 binder_validate_ptr(t->buffer, fda->parent,
3085 off_start,
3086 offp - off_start);
3087 if (!parent) {
3088 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
3089 proc->pid, thread->pid);
3090 return_error = BR_FAILED_REPLY;
3091 return_error_param = -EINVAL;
3092 return_error_line = __LINE__;
3093 goto err_bad_parent;
3094 }
3095 if (!binder_validate_fixup(t->buffer, off_start,
3096 parent, fda->parent_offset,
3097 last_fixup_obj,
3098 last_fixup_min_off)) {
3099 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
3100 proc->pid, thread->pid);
3101 return_error = BR_FAILED_REPLY;
3102 return_error_param = -EINVAL;
3103 return_error_line = __LINE__;
3104 goto err_bad_parent;
3105 }
3106 ret = binder_translate_fd_array(fda, parent, t, thread,
3107 in_reply_to);
3108 if (ret < 0) {
3109 return_error = BR_FAILED_REPLY;
3110 return_error_param = ret;
3111 return_error_line = __LINE__;
3112 goto err_translate_failed;
3113 }
3114 last_fixup_obj = parent;
3115 last_fixup_min_off =
3116 fda->parent_offset + sizeof(u32) * fda->num_fds;
3117 } break;
3118 case BINDER_TYPE_PTR: {
3119 struct binder_buffer_object *bp =
3120 to_binder_buffer_object(hdr);
3121 size_t buf_left = sg_buf_end - sg_bufp;
3122
3123 if (bp->length > buf_left) {
3124 binder_user_error("%d:%d got transaction with too large buffer\n",
3125 proc->pid, thread->pid);
3126 return_error = BR_FAILED_REPLY;
3127 return_error_param = -EINVAL;
3128 return_error_line = __LINE__;
3129 goto err_bad_offset;
3130 }
3131 if (copy_from_user(sg_bufp,
3132 (const void __user *)(uintptr_t)
3133 bp->buffer, bp->length)) {
3134 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3135 proc->pid, thread->pid);
3136 return_error_param = -EFAULT;
3137 return_error = BR_FAILED_REPLY;
3138 return_error_line = __LINE__;
3139 goto err_copy_data_failed;
3140 }
3141 /* Fixup buffer pointer to target proc address space */
3142 bp->buffer = (uintptr_t)sg_bufp +
3143 binder_alloc_get_user_buffer_offset(
3144 &target_proc->alloc);
3145 sg_bufp += ALIGN(bp->length, sizeof(u64));
3146
3147 ret = binder_fixup_parent(t, thread, bp, off_start,
3148 offp - off_start,
3149 last_fixup_obj,
3150 last_fixup_min_off);
3151 if (ret < 0) {
3152 return_error = BR_FAILED_REPLY;
3153 return_error_param = ret;
3154 return_error_line = __LINE__;
3155 goto err_translate_failed;
3156 }
3157 last_fixup_obj = bp;
3158 last_fixup_min_off = 0;
3159 } break;
3160 default:
3161 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
3162 proc->pid, thread->pid, hdr->type);
3163 return_error = BR_FAILED_REPLY;
3164 return_error_param = -EINVAL;
3165 return_error_line = __LINE__;
3166 goto err_bad_object_type;
3167 }
3168 }
3169 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
3170 t->work.type = BINDER_WORK_TRANSACTION;
3171
3172 if (reply) {
3173 binder_enqueue_thread_work(thread, tcomplete);
3174 binder_inner_proc_lock(target_proc);
3175 if (target_thread->is_dead) {
3176 binder_inner_proc_unlock(target_proc);
3177 goto err_dead_proc_or_thread;
3178 }
3179 BUG_ON(t->buffer->async_transaction != 0);
3180 binder_pop_transaction_ilocked(target_thread, in_reply_to);
3181 binder_enqueue_thread_work_ilocked(target_thread, &t->work);
3182 binder_inner_proc_unlock(target_proc);
3183 wake_up_interruptible_sync(&target_thread->wait);
3184 binder_free_transaction(in_reply_to);
3185 } else if (!(t->flags & TF_ONE_WAY)) {
3186 BUG_ON(t->buffer->async_transaction != 0);
3187 binder_inner_proc_lock(proc);
3188 /*
3189 * Defer the TRANSACTION_COMPLETE, so we don't return to
3190 * userspace immediately; this allows the target process to
3191 * immediately start processing this transaction, reducing
3192 * latency. We will then return the TRANSACTION_COMPLETE when
3193 * the target replies (or there is an error).
3194 */
3195 binder_enqueue_deferred_thread_work_ilocked(thread, tcomplete);
3196 t->need_reply = 1;
3197 t->from_parent = thread->transaction_stack;
3198 thread->transaction_stack = t;
3199 binder_inner_proc_unlock(proc);
3200 if (!binder_proc_transaction(t, target_proc, target_thread)) {
3201 binder_inner_proc_lock(proc);
3202 binder_pop_transaction_ilocked(thread, t);
3203 binder_inner_proc_unlock(proc);
3204 goto err_dead_proc_or_thread;
3205 }
3206 } else {
3207 BUG_ON(target_node == NULL);
3208 BUG_ON(t->buffer->async_transaction != 1);
3209 binder_enqueue_thread_work(thread, tcomplete);
3210 if (!binder_proc_transaction(t, target_proc, NULL))
3211 goto err_dead_proc_or_thread;
3212 }
3213 if (target_thread)
3214 binder_thread_dec_tmpref(target_thread);
3215 binder_proc_dec_tmpref(target_proc);
3216 if (target_node)
3217 binder_dec_node_tmpref(target_node);
3218 /*
3219 * write barrier to synchronize with initialization
3220 * of log entry
3221 */
3222 smp_wmb();
3223 WRITE_ONCE(e->debug_id_done, t_debug_id);
3224 return;
3225
3226err_dead_proc_or_thread:
3227 return_error = BR_DEAD_REPLY;
3228 return_error_line = __LINE__;
3229 binder_dequeue_work(proc, tcomplete);
3230err_translate_failed:
3231err_bad_object_type:
3232err_bad_offset:
3233err_bad_parent:
3234err_copy_data_failed:
3235 trace_binder_transaction_failed_buffer_release(t->buffer);
3236 binder_transaction_buffer_release(target_proc, t->buffer, offp);
3237 if (target_node)
3238 binder_dec_node_tmpref(target_node);
3239 target_node = NULL;
3240 t->buffer->transaction = NULL;
3241 binder_alloc_free_buf(&target_proc->alloc, t->buffer);
3242err_binder_alloc_buf_failed:
3243 kfree(tcomplete);
3244 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3245err_alloc_tcomplete_failed:
3246 kfree(t);
3247 binder_stats_deleted(BINDER_STAT_TRANSACTION);
3248err_alloc_t_failed:
3249err_bad_call_stack:
3250err_empty_call_stack:
3251err_dead_binder:
3252err_invalid_target_handle:
3253 if (target_thread)
3254 binder_thread_dec_tmpref(target_thread);
3255 if (target_proc)
3256 binder_proc_dec_tmpref(target_proc);
3257 if (target_node) {
3258 binder_dec_node(target_node, 1, 0);
3259 binder_dec_node_tmpref(target_node);
3260 }
3261
3262 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
3263 "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
3264 proc->pid, thread->pid, return_error, return_error_param,
3265 (u64)tr->data_size, (u64)tr->offsets_size,
3266 return_error_line);
3267
3268 {
3269 struct binder_transaction_log_entry *fe;
3270
3271 e->return_error = return_error;
3272 e->return_error_param = return_error_param;
3273 e->return_error_line = return_error_line;
3274 fe = binder_transaction_log_add(&binder_transaction_log_failed);
3275 *fe = *e;
3276 /*
3277 * write barrier to synchronize with initialization
3278 * of log entry
3279 */
3280 smp_wmb();
3281 WRITE_ONCE(e->debug_id_done, t_debug_id);
3282 WRITE_ONCE(fe->debug_id_done, t_debug_id);
3283 }
3284
3285 BUG_ON(thread->return_error.cmd != BR_OK);
3286 if (in_reply_to) {
3287 thread->return_error.cmd = BR_TRANSACTION_COMPLETE;
3288 binder_enqueue_thread_work(thread, &thread->return_error.work);
3289 binder_send_failed_reply(in_reply_to, return_error);
3290 } else {
3291 thread->return_error.cmd = return_error;
3292 binder_enqueue_thread_work(thread, &thread->return_error.work);
3293 }
3294}
3295
3296static int binder_thread_write(struct binder_proc *proc,
3297 struct binder_thread *thread,
3298 binder_uintptr_t binder_buffer, size_t size,
3299 binder_size_t *consumed)
3300{
3301 uint32_t cmd;
3302 struct binder_context *context = proc->context;
3303 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
3304 void __user *ptr = buffer + *consumed;
3305 void __user *end = buffer + size;
3306
3307 while (ptr < end && thread->return_error.cmd == BR_OK) {
3308 int ret;
3309
3310 if (get_user(cmd, (uint32_t __user *)ptr))
3311 return -EFAULT;
3312 ptr += sizeof(uint32_t);
3313 trace_binder_command(cmd);
3314 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
3315 atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]);
3316 atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]);
3317 atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]);
3318 }
3319 switch (cmd) {
3320 case BC_INCREFS:
3321 case BC_ACQUIRE:
3322 case BC_RELEASE:
3323 case BC_DECREFS: {
3324 uint32_t target;
3325 const char *debug_string;
3326 bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE;
3327 bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE;
3328 struct binder_ref_data rdata;
3329
3330 if (get_user(target, (uint32_t __user *)ptr))
3331 return -EFAULT;
3332
3333 ptr += sizeof(uint32_t);
3334 ret = -1;
3335 if (increment && !target) {
3336 struct binder_node *ctx_mgr_node;
3337 mutex_lock(&context->context_mgr_node_lock);
3338 ctx_mgr_node = context->binder_context_mgr_node;
3339 if (ctx_mgr_node)
3340 ret = binder_inc_ref_for_node(
3341 proc, ctx_mgr_node,
3342 strong, NULL, &rdata);
3343 mutex_unlock(&context->context_mgr_node_lock);
3344 }
3345 if (ret)
3346 ret = binder_update_ref_for_handle(
3347 proc, target, increment, strong,
3348 &rdata);
3349 if (!ret && rdata.desc != target) {
3350 binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
3351 proc->pid, thread->pid,
3352 target, rdata.desc);
3353 }
3354 switch (cmd) {
3355 case BC_INCREFS:
3356 debug_string = "IncRefs";
3357 break;
3358 case BC_ACQUIRE:
3359 debug_string = "Acquire";
3360 break;
3361 case BC_RELEASE:
3362 debug_string = "Release";
3363 break;
3364 case BC_DECREFS:
3365 default:
3366 debug_string = "DecRefs";
3367 break;
3368 }
3369 if (ret) {
3370 binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
3371 proc->pid, thread->pid, debug_string,
3372 strong, target, ret);
3373 break;
3374 }
3375 binder_debug(BINDER_DEBUG_USER_REFS,
3376 "%d:%d %s ref %d desc %d s %d w %d\n",
3377 proc->pid, thread->pid, debug_string,
3378 rdata.debug_id, rdata.desc, rdata.strong,
3379 rdata.weak);
3380 break;
3381 }
3382 case BC_INCREFS_DONE:
3383 case BC_ACQUIRE_DONE: {
3384 binder_uintptr_t node_ptr;
3385 binder_uintptr_t cookie;
3386 struct binder_node *node;
3387 bool free_node;
3388
3389 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
3390 return -EFAULT;
3391 ptr += sizeof(binder_uintptr_t);
3392 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
3393 return -EFAULT;
3394 ptr += sizeof(binder_uintptr_t);
3395 node = binder_get_node(proc, node_ptr);
3396 if (node == NULL) {
3397 binder_user_error("%d:%d %s u%016llx no match\n",
3398 proc->pid, thread->pid,
3399 cmd == BC_INCREFS_DONE ?
3400 "BC_INCREFS_DONE" :
3401 "BC_ACQUIRE_DONE",
3402 (u64)node_ptr);
3403 break;
3404 }
3405 if (cookie != node->cookie) {
3406 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
3407 proc->pid, thread->pid,
3408 cmd == BC_INCREFS_DONE ?
3409 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
3410 (u64)node_ptr, node->debug_id,
3411 (u64)cookie, (u64)node->cookie);
3412 binder_put_node(node);
3413 break;
3414 }
3415 binder_node_inner_lock(node);
3416 if (cmd == BC_ACQUIRE_DONE) {
3417 if (node->pending_strong_ref == 0) {
3418 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
3419 proc->pid, thread->pid,
3420 node->debug_id);
3421 binder_node_inner_unlock(node);
3422 binder_put_node(node);
3423 break;
3424 }
3425 node->pending_strong_ref = 0;
3426 } else {
3427 if (node->pending_weak_ref == 0) {
3428 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
3429 proc->pid, thread->pid,
3430 node->debug_id);
3431 binder_node_inner_unlock(node);
3432 binder_put_node(node);
3433 break;
3434 }
3435 node->pending_weak_ref = 0;
3436 }
3437 free_node = binder_dec_node_nilocked(node,
3438 cmd == BC_ACQUIRE_DONE, 0);
3439 WARN_ON(free_node);
3440 binder_debug(BINDER_DEBUG_USER_REFS,
3441 "%d:%d %s node %d ls %d lw %d tr %d\n",
3442 proc->pid, thread->pid,
3443 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
3444 node->debug_id, node->local_strong_refs,
3445 node->local_weak_refs, node->tmp_refs);
3446 binder_node_inner_unlock(node);
3447 binder_put_node(node);
3448 break;
3449 }
3450 case BC_ATTEMPT_ACQUIRE:
3451 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
3452 return -EINVAL;
3453 case BC_ACQUIRE_RESULT:
3454 pr_err("BC_ACQUIRE_RESULT not supported\n");
3455 return -EINVAL;
3456
3457 case BC_FREE_BUFFER: {
3458 binder_uintptr_t data_ptr;
3459 struct binder_buffer *buffer;
3460
3461 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
3462 return -EFAULT;
3463 ptr += sizeof(binder_uintptr_t);
3464
3465 buffer = binder_alloc_prepare_to_free(&proc->alloc,
3466 data_ptr);
3467 if (IS_ERR_OR_NULL(buffer)) {
3468 if (PTR_ERR(buffer) == -EPERM) {
3469 binder_user_error(
3470 "%d:%d BC_FREE_BUFFER u%016llx matched unreturned or currently freeing buffer\n",
3471 proc->pid, thread->pid,
3472 (u64)data_ptr);
3473 } else {
3474 binder_user_error(
3475 "%d:%d BC_FREE_BUFFER u%016llx no match\n",
3476 proc->pid, thread->pid,
3477 (u64)data_ptr);
3478 }
3479 break;
3480 }
3481 binder_debug(BINDER_DEBUG_FREE_BUFFER,
3482 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
3483 proc->pid, thread->pid, (u64)data_ptr,
3484 buffer->debug_id,
3485 buffer->transaction ? "active" : "finished");
3486
3487 if (buffer->transaction) {
3488 buffer->transaction->buffer = NULL;
3489 buffer->transaction = NULL;
3490 }
3491 if (buffer->async_transaction && buffer->target_node) {
3492 struct binder_node *buf_node;
3493 struct binder_work *w;
3494
3495 buf_node = buffer->target_node;
3496 binder_node_inner_lock(buf_node);
3497 BUG_ON(!buf_node->has_async_transaction);
3498 BUG_ON(buf_node->proc != proc);
3499 w = binder_dequeue_work_head_ilocked(
3500 &buf_node->async_todo);
3501 if (!w) {
3502 buf_node->has_async_transaction = false;
3503 } else {
3504 binder_enqueue_work_ilocked(
3505 w, &proc->todo);
3506 binder_wakeup_proc_ilocked(proc);
3507 }
3508 binder_node_inner_unlock(buf_node);
3509 }
3510 trace_binder_transaction_buffer_release(buffer);
3511 binder_transaction_buffer_release(proc, buffer, NULL);
3512 binder_alloc_free_buf(&proc->alloc, buffer);
3513 break;
3514 }
3515
3516 case BC_TRANSACTION_SG:
3517 case BC_REPLY_SG: {
3518 struct binder_transaction_data_sg tr;
3519
3520 if (copy_from_user(&tr, ptr, sizeof(tr)))
3521 return -EFAULT;
3522 ptr += sizeof(tr);
3523 binder_transaction(proc, thread, &tr.transaction_data,
3524 cmd == BC_REPLY_SG, tr.buffers_size);
3525 break;
3526 }
3527 case BC_TRANSACTION:
3528 case BC_REPLY: {
3529 struct binder_transaction_data tr;
3530
3531 if (copy_from_user(&tr, ptr, sizeof(tr)))
3532 return -EFAULT;
3533 ptr += sizeof(tr);
3534 binder_transaction(proc, thread, &tr,
3535 cmd == BC_REPLY, 0);
3536 break;
3537 }
3538
3539 case BC_REGISTER_LOOPER:
3540 binder_debug(BINDER_DEBUG_THREADS,
3541 "%d:%d BC_REGISTER_LOOPER\n",
3542 proc->pid, thread->pid);
3543 binder_inner_proc_lock(proc);
3544 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
3545 thread->looper |= BINDER_LOOPER_STATE_INVALID;
3546 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
3547 proc->pid, thread->pid);
3548 } else if (proc->requested_threads == 0) {
3549 thread->looper |= BINDER_LOOPER_STATE_INVALID;
3550 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
3551 proc->pid, thread->pid);
3552 } else {
3553 proc->requested_threads--;
3554 proc->requested_threads_started++;
3555 }
3556 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
3557 binder_inner_proc_unlock(proc);
3558 break;
3559 case BC_ENTER_LOOPER:
3560 binder_debug(BINDER_DEBUG_THREADS,
3561 "%d:%d BC_ENTER_LOOPER\n",
3562 proc->pid, thread->pid);
3563 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
3564 thread->looper |= BINDER_LOOPER_STATE_INVALID;
3565 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
3566 proc->pid, thread->pid);
3567 }
3568 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
3569 break;
3570 case BC_EXIT_LOOPER:
3571 binder_debug(BINDER_DEBUG_THREADS,
3572 "%d:%d BC_EXIT_LOOPER\n",
3573 proc->pid, thread->pid);
3574 thread->looper |= BINDER_LOOPER_STATE_EXITED;
3575 break;
3576
3577 case BC_REQUEST_DEATH_NOTIFICATION:
3578 case BC_CLEAR_DEATH_NOTIFICATION: {
3579 uint32_t target;
3580 binder_uintptr_t cookie;
3581 struct binder_ref *ref;
3582 struct binder_ref_death *death = NULL;
3583
3584 if (get_user(target, (uint32_t __user *)ptr))
3585 return -EFAULT;
3586 ptr += sizeof(uint32_t);
3587 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
3588 return -EFAULT;
3589 ptr += sizeof(binder_uintptr_t);
3590 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3591 /*
3592 * Allocate memory for death notification
3593 * before taking lock
3594 */
3595 death = kzalloc(sizeof(*death), GFP_KERNEL);
3596 if (death == NULL) {
3597 WARN_ON(thread->return_error.cmd !=
3598 BR_OK);
3599 thread->return_error.cmd = BR_ERROR;
3600 binder_enqueue_thread_work(
3601 thread,
3602 &thread->return_error.work);
3603 binder_debug(
3604 BINDER_DEBUG_FAILED_TRANSACTION,
3605 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
3606 proc->pid, thread->pid);
3607 break;
3608 }
3609 }
3610 binder_proc_lock(proc);
3611 ref = binder_get_ref_olocked(proc, target, false);
3612 if (ref == NULL) {
3613 binder_user_error("%d:%d %s invalid ref %d\n",
3614 proc->pid, thread->pid,
3615 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3616 "BC_REQUEST_DEATH_NOTIFICATION" :
3617 "BC_CLEAR_DEATH_NOTIFICATION",
3618 target);
3619 binder_proc_unlock(proc);
3620 kfree(death);
3621 break;
3622 }
3623
3624 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
3625 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
3626 proc->pid, thread->pid,
3627 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3628 "BC_REQUEST_DEATH_NOTIFICATION" :
3629 "BC_CLEAR_DEATH_NOTIFICATION",
3630 (u64)cookie, ref->data.debug_id,
3631 ref->data.desc, ref->data.strong,
3632 ref->data.weak, ref->node->debug_id);
3633
3634 binder_node_lock(ref->node);
3635 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3636 if (ref->death) {
3637 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
3638 proc->pid, thread->pid);
3639 binder_node_unlock(ref->node);
3640 binder_proc_unlock(proc);
3641 kfree(death);
3642 break;
3643 }
3644 binder_stats_created(BINDER_STAT_DEATH);
3645 INIT_LIST_HEAD(&death->work.entry);
3646 death->cookie = cookie;
3647 ref->death = death;
3648 if (ref->node->proc == NULL) {
3649 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
3650
3651 binder_inner_proc_lock(proc);
3652 binder_enqueue_work_ilocked(
3653 &ref->death->work, &proc->todo);
3654 binder_wakeup_proc_ilocked(proc);
3655 binder_inner_proc_unlock(proc);
3656 }
3657 } else {
3658 if (ref->death == NULL) {
3659 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
3660 proc->pid, thread->pid);
3661 binder_node_unlock(ref->node);
3662 binder_proc_unlock(proc);
3663 break;
3664 }
3665 death = ref->death;
3666 if (death->cookie != cookie) {
3667 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
3668 proc->pid, thread->pid,
3669 (u64)death->cookie,
3670 (u64)cookie);
3671 binder_node_unlock(ref->node);
3672 binder_proc_unlock(proc);
3673 break;
3674 }
3675 ref->death = NULL;
3676 binder_inner_proc_lock(proc);
3677 if (list_empty(&death->work.entry)) {
3678 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
3679 if (thread->looper &
3680 (BINDER_LOOPER_STATE_REGISTERED |
3681 BINDER_LOOPER_STATE_ENTERED))
3682 binder_enqueue_thread_work_ilocked(
3683 thread,
3684 &death->work);
3685 else {
3686 binder_enqueue_work_ilocked(
3687 &death->work,
3688 &proc->todo);
3689 binder_wakeup_proc_ilocked(
3690 proc);
3691 }
3692 } else {
3693 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
3694 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
3695 }
3696 binder_inner_proc_unlock(proc);
3697 }
3698 binder_node_unlock(ref->node);
3699 binder_proc_unlock(proc);
3700 } break;
3701 case BC_DEAD_BINDER_DONE: {
3702 struct binder_work *w;
3703 binder_uintptr_t cookie;
3704 struct binder_ref_death *death = NULL;
3705
3706 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
3707 return -EFAULT;
3708
3709 ptr += sizeof(cookie);
3710 binder_inner_proc_lock(proc);
3711 list_for_each_entry(w, &proc->delivered_death,
3712 entry) {
3713 struct binder_ref_death *tmp_death =
3714 container_of(w,
3715 struct binder_ref_death,
3716 work);
3717
3718 if (tmp_death->cookie == cookie) {
3719 death = tmp_death;
3720 break;
3721 }
3722 }
3723 binder_debug(BINDER_DEBUG_DEAD_BINDER,
3724 "%d:%d BC_DEAD_BINDER_DONE %016llx found %pK\n",
3725 proc->pid, thread->pid, (u64)cookie,
3726 death);
3727 if (death == NULL) {
3728 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
3729 proc->pid, thread->pid, (u64)cookie);
3730 binder_inner_proc_unlock(proc);
3731 break;
3732 }
3733 binder_dequeue_work_ilocked(&death->work);
3734 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
3735 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
3736 if (thread->looper &
3737 (BINDER_LOOPER_STATE_REGISTERED |
3738 BINDER_LOOPER_STATE_ENTERED))
3739 binder_enqueue_thread_work_ilocked(
3740 thread, &death->work);
3741 else {
3742 binder_enqueue_work_ilocked(
3743 &death->work,
3744 &proc->todo);
3745 binder_wakeup_proc_ilocked(proc);
3746 }
3747 }
3748 binder_inner_proc_unlock(proc);
3749 } break;
3750
3751 default:
3752 pr_err("%d:%d unknown command %d\n",
3753 proc->pid, thread->pid, cmd);
3754 return -EINVAL;
3755 }
3756 *consumed = ptr - buffer;
3757 }
3758 return 0;
3759}
3760
3761static void binder_stat_br(struct binder_proc *proc,
3762 struct binder_thread *thread, uint32_t cmd)
3763{
3764 trace_binder_return(cmd);
3765 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
3766 atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
3767 atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
3768 atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
3769 }
3770}
3771
3772static int binder_put_node_cmd(struct binder_proc *proc,
3773 struct binder_thread *thread,
3774 void __user **ptrp,
3775 binder_uintptr_t node_ptr,
3776 binder_uintptr_t node_cookie,
3777 int node_debug_id,
3778 uint32_t cmd, const char *cmd_name)
3779{
3780 void __user *ptr = *ptrp;
3781
3782 if (put_user(cmd, (uint32_t __user *)ptr))
3783 return -EFAULT;
3784 ptr += sizeof(uint32_t);
3785
3786 if (put_user(node_ptr, (binder_uintptr_t __user *)ptr))
3787 return -EFAULT;
3788 ptr += sizeof(binder_uintptr_t);
3789
3790 if (put_user(node_cookie, (binder_uintptr_t __user *)ptr))
3791 return -EFAULT;
3792 ptr += sizeof(binder_uintptr_t);
3793
3794 binder_stat_br(proc, thread, cmd);
3795 binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n",
3796 proc->pid, thread->pid, cmd_name, node_debug_id,
3797 (u64)node_ptr, (u64)node_cookie);
3798
3799 *ptrp = ptr;
3800 return 0;
3801}
3802
3803static int binder_wait_for_work(struct binder_thread *thread,
3804 bool do_proc_work)
3805{
3806 DEFINE_WAIT(wait);
3807 struct binder_proc *proc = thread->proc;
3808 int ret = 0;
3809
3810 freezer_do_not_count();
3811 binder_inner_proc_lock(proc);
3812 for (;;) {
3813 prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE);
3814 if (binder_has_work_ilocked(thread, do_proc_work))
3815 break;
3816 if (do_proc_work)
3817 list_add(&thread->waiting_thread_node,
3818 &proc->waiting_threads);
3819 binder_inner_proc_unlock(proc);
3820 schedule();
3821 binder_inner_proc_lock(proc);
3822 list_del_init(&thread->waiting_thread_node);
3823 if (signal_pending(current)) {
3824 ret = -ERESTARTSYS;
3825 break;
3826 }
3827 }
3828 finish_wait(&thread->wait, &wait);
3829 binder_inner_proc_unlock(proc);
3830 freezer_count();
3831
3832 return ret;
3833}
3834
3835static int binder_thread_read(struct binder_proc *proc,
3836 struct binder_thread *thread,
3837 binder_uintptr_t binder_buffer, size_t size,
3838 binder_size_t *consumed, int non_block)
3839{
3840 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
3841 void __user *ptr = buffer + *consumed;
3842 void __user *end = buffer + size;
3843
3844 int ret = 0;
3845 int wait_for_proc_work;
3846
3847 if (*consumed == 0) {
3848 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
3849 return -EFAULT;
3850 ptr += sizeof(uint32_t);
3851 }
3852
3853retry:
3854 binder_inner_proc_lock(proc);
3855 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
3856 binder_inner_proc_unlock(proc);
3857
3858 thread->looper |= BINDER_LOOPER_STATE_WAITING;
3859
3860 trace_binder_wait_for_work(wait_for_proc_work,
3861 !!thread->transaction_stack,
3862 !binder_worklist_empty(proc, &thread->todo));
3863 if (wait_for_proc_work) {
3864 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
3865 BINDER_LOOPER_STATE_ENTERED))) {
3866 binder_user_error("%d:%d ERROR: Thread waiting for process work before calling BC_REGISTER_LOOPER or BC_ENTER_LOOPER (state %x)\n",
3867 proc->pid, thread->pid, thread->looper);
3868 wait_event_interruptible(binder_user_error_wait,
3869 binder_stop_on_user_error < 2);
3870 }
3871 binder_set_nice(proc->default_priority);
3872 }
3873
3874 if (non_block) {
3875 if (!binder_has_work(thread, wait_for_proc_work))
3876 ret = -EAGAIN;
3877 } else {
3878 ret = binder_wait_for_work(thread, wait_for_proc_work);
3879 }
3880
3881 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
3882
3883 if (ret)
3884 return ret;
3885
3886 while (1) {
3887 uint32_t cmd;
3888 struct binder_transaction_data tr;
3889 struct binder_work *w = NULL;
3890 struct list_head *list = NULL;
3891 struct binder_transaction *t = NULL;
3892 struct binder_thread *t_from;
3893
3894 binder_inner_proc_lock(proc);
3895 if (!binder_worklist_empty_ilocked(&thread->todo))
3896 list = &thread->todo;
3897 else if (!binder_worklist_empty_ilocked(&proc->todo) &&
3898 wait_for_proc_work)
3899 list = &proc->todo;
3900 else {
3901 binder_inner_proc_unlock(proc);
3902
3903 /* no data added */
3904 if (ptr - buffer == 4 && !thread->looper_need_return)
3905 goto retry;
3906 break;
3907 }
3908
3909 if (end - ptr < sizeof(tr) + 4) {
3910 binder_inner_proc_unlock(proc);
3911 break;
3912 }
3913 w = binder_dequeue_work_head_ilocked(list);
3914 if (binder_worklist_empty_ilocked(&thread->todo))
3915 thread->process_todo = false;
3916
3917 switch (w->type) {
3918 case BINDER_WORK_TRANSACTION: {
3919 binder_inner_proc_unlock(proc);
3920 t = container_of(w, struct binder_transaction, work);
3921 } break;
3922 case BINDER_WORK_RETURN_ERROR: {
3923 struct binder_error *e = container_of(
3924 w, struct binder_error, work);
3925
3926 WARN_ON(e->cmd == BR_OK);
3927 binder_inner_proc_unlock(proc);
3928 if (put_user(e->cmd, (uint32_t __user *)ptr))
3929 return -EFAULT;
3930 cmd = e->cmd;
3931 e->cmd = BR_OK;
3932 ptr += sizeof(uint32_t);
3933
3934 binder_stat_br(proc, thread, cmd);
3935 } break;
3936 case BINDER_WORK_TRANSACTION_COMPLETE: {
3937 binder_inner_proc_unlock(proc);
3938 cmd = BR_TRANSACTION_COMPLETE;
3939 if (put_user(cmd, (uint32_t __user *)ptr))
3940 return -EFAULT;
3941 ptr += sizeof(uint32_t);
3942
3943 binder_stat_br(proc, thread, cmd);
3944 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
3945 "%d:%d BR_TRANSACTION_COMPLETE\n",
3946 proc->pid, thread->pid);
3947 kfree(w);
3948 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3949 } break;
3950 case BINDER_WORK_NODE: {
3951 struct binder_node *node = container_of(w, struct binder_node, work);
3952 int strong, weak;
3953 binder_uintptr_t node_ptr = node->ptr;
3954 binder_uintptr_t node_cookie = node->cookie;
3955 int node_debug_id = node->debug_id;
3956 int has_weak_ref;
3957 int has_strong_ref;
3958 void __user *orig_ptr = ptr;
3959
3960 BUG_ON(proc != node->proc);
3961 strong = node->internal_strong_refs ||
3962 node->local_strong_refs;
3963 weak = !hlist_empty(&node->refs) ||
3964 node->local_weak_refs ||
3965 node->tmp_refs || strong;
3966 has_strong_ref = node->has_strong_ref;
3967 has_weak_ref = node->has_weak_ref;
3968
3969 if (weak && !has_weak_ref) {
3970 node->has_weak_ref = 1;
3971 node->pending_weak_ref = 1;
3972 node->local_weak_refs++;
3973 }
3974 if (strong && !has_strong_ref) {
3975 node->has_strong_ref = 1;
3976 node->pending_strong_ref = 1;
3977 node->local_strong_refs++;
3978 }
3979 if (!strong && has_strong_ref)
3980 node->has_strong_ref = 0;
3981 if (!weak && has_weak_ref)
3982 node->has_weak_ref = 0;
3983 if (!weak && !strong) {
3984 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
3985 "%d:%d node %d u%016llx c%016llx deleted\n",
3986 proc->pid, thread->pid,
3987 node_debug_id,
3988 (u64)node_ptr,
3989 (u64)node_cookie);
3990 rb_erase(&node->rb_node, &proc->nodes);
3991 binder_inner_proc_unlock(proc);
3992 binder_node_lock(node);
3993 /*
3994 * Acquire the node lock before freeing the
3995 * node to serialize with other threads that
3996 * may have been holding the node lock while
3997 * decrementing this node (avoids race where
3998 * this thread frees while the other thread
3999 * is unlocking the node after the final
4000 * decrement)
4001 */
4002 binder_node_unlock(node);
4003 binder_free_node(node);
4004 } else
4005 binder_inner_proc_unlock(proc);
4006
4007 if (weak && !has_weak_ref)
4008 ret = binder_put_node_cmd(
4009 proc, thread, &ptr, node_ptr,
4010 node_cookie, node_debug_id,
4011 BR_INCREFS, "BR_INCREFS");
4012 if (!ret && strong && !has_strong_ref)
4013 ret = binder_put_node_cmd(
4014 proc, thread, &ptr, node_ptr,
4015 node_cookie, node_debug_id,
4016 BR_ACQUIRE, "BR_ACQUIRE");
4017 if (!ret && !strong && has_strong_ref)
4018 ret = binder_put_node_cmd(
4019 proc, thread, &ptr, node_ptr,
4020 node_cookie, node_debug_id,
4021 BR_RELEASE, "BR_RELEASE");
4022 if (!ret && !weak && has_weak_ref)
4023 ret = binder_put_node_cmd(
4024 proc, thread, &ptr, node_ptr,
4025 node_cookie, node_debug_id,
4026 BR_DECREFS, "BR_DECREFS");
4027 if (orig_ptr == ptr)
4028 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4029 "%d:%d node %d u%016llx c%016llx state unchanged\n",
4030 proc->pid, thread->pid,
4031 node_debug_id,
4032 (u64)node_ptr,
4033 (u64)node_cookie);
4034 if (ret)
4035 return ret;
4036 } break;
4037 case BINDER_WORK_DEAD_BINDER:
4038 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4039 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4040 struct binder_ref_death *death;
4041 uint32_t cmd;
4042 binder_uintptr_t cookie;
4043
4044 death = container_of(w, struct binder_ref_death, work);
4045 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
4046 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
4047 else
4048 cmd = BR_DEAD_BINDER;
4049 cookie = death->cookie;
4050
4051 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
4052 "%d:%d %s %016llx\n",
4053 proc->pid, thread->pid,
4054 cmd == BR_DEAD_BINDER ?
4055 "BR_DEAD_BINDER" :
4056 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
4057 (u64)cookie);
4058 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
4059 binder_inner_proc_unlock(proc);
4060 kfree(death);
4061 binder_stats_deleted(BINDER_STAT_DEATH);
4062 } else {
4063 binder_enqueue_work_ilocked(
4064 w, &proc->delivered_death);
4065 binder_inner_proc_unlock(proc);
4066 }
4067 if (put_user(cmd, (uint32_t __user *)ptr))
4068 return -EFAULT;
4069 ptr += sizeof(uint32_t);
4070 if (put_user(cookie,
4071 (binder_uintptr_t __user *)ptr))
4072 return -EFAULT;
4073 ptr += sizeof(binder_uintptr_t);
4074 binder_stat_br(proc, thread, cmd);
4075 if (cmd == BR_DEAD_BINDER)
4076 goto done; /* DEAD_BINDER notifications can cause transactions */
4077 } break;
4078 }
4079
4080 if (!t)
4081 continue;
4082
4083 BUG_ON(t->buffer == NULL);
4084 if (t->buffer->target_node) {
4085 struct binder_node *target_node = t->buffer->target_node;
4086
4087 tr.target.ptr = target_node->ptr;
4088 tr.cookie = target_node->cookie;
4089 t->saved_priority = task_nice(current);
4090 if (t->priority < target_node->min_priority &&
4091 !(t->flags & TF_ONE_WAY))
4092 binder_set_nice(t->priority);
4093 else if (!(t->flags & TF_ONE_WAY) ||
4094 t->saved_priority > target_node->min_priority)
4095 binder_set_nice(target_node->min_priority);
4096 cmd = BR_TRANSACTION;
4097 } else {
4098 tr.target.ptr = 0;
4099 tr.cookie = 0;
4100 cmd = BR_REPLY;
4101 }
4102 tr.code = t->code;
4103 tr.flags = t->flags;
4104 tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
4105
4106 t_from = binder_get_txn_from(t);
4107 if (t_from) {
4108 struct task_struct *sender = t_from->proc->tsk;
4109
4110 tr.sender_pid = task_tgid_nr_ns(sender,
4111 task_active_pid_ns(current));
4112 } else {
4113 tr.sender_pid = 0;
4114 }
4115
4116 tr.data_size = t->buffer->data_size;
4117 tr.offsets_size = t->buffer->offsets_size;
4118 tr.data.ptr.buffer = (binder_uintptr_t)
4119 ((uintptr_t)t->buffer->data +
4120 binder_alloc_get_user_buffer_offset(&proc->alloc));
4121 tr.data.ptr.offsets = tr.data.ptr.buffer +
4122 ALIGN(t->buffer->data_size,
4123 sizeof(void *));
4124
4125 if (put_user(cmd, (uint32_t __user *)ptr)) {
4126 if (t_from)
4127 binder_thread_dec_tmpref(t_from);
4128
4129 binder_cleanup_transaction(t, "put_user failed",
4130 BR_FAILED_REPLY);
4131
4132 return -EFAULT;
4133 }
4134 ptr += sizeof(uint32_t);
4135 if (copy_to_user(ptr, &tr, sizeof(tr))) {
4136 if (t_from)
4137 binder_thread_dec_tmpref(t_from);
4138
4139 binder_cleanup_transaction(t, "copy_to_user failed",
4140 BR_FAILED_REPLY);
4141
4142 return -EFAULT;
4143 }
4144 ptr += sizeof(tr);
4145
4146 trace_binder_transaction_received(t);
4147 binder_stat_br(proc, thread, cmd);
4148 binder_debug(BINDER_DEBUG_TRANSACTION,
4149 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
4150 proc->pid, thread->pid,
4151 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
4152 "BR_REPLY",
4153 t->debug_id, t_from ? t_from->proc->pid : 0,
4154 t_from ? t_from->pid : 0, cmd,
4155 t->buffer->data_size, t->buffer->offsets_size,
4156 (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
4157
4158 if (t_from)
4159 binder_thread_dec_tmpref(t_from);
4160 t->buffer->allow_user_free = 1;
4161 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
4162 binder_inner_proc_lock(thread->proc);
4163 t->to_parent = thread->transaction_stack;
4164 t->to_thread = thread;
4165 thread->transaction_stack = t;
4166 binder_inner_proc_unlock(thread->proc);
4167 } else {
4168 binder_free_transaction(t);
4169 }
4170 break;
4171 }
4172
4173done:
4174
4175 *consumed = ptr - buffer;
4176 binder_inner_proc_lock(proc);
4177 if (proc->requested_threads == 0 &&
4178 list_empty(&thread->proc->waiting_threads) &&
4179 proc->requested_threads_started < proc->max_threads &&
4180 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4181 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
4182 /*spawn a new thread if we leave this out */) {
4183 proc->requested_threads++;
4184 binder_inner_proc_unlock(proc);
4185 binder_debug(BINDER_DEBUG_THREADS,
4186 "%d:%d BR_SPAWN_LOOPER\n",
4187 proc->pid, thread->pid);
4188 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
4189 return -EFAULT;
4190 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
4191 } else
4192 binder_inner_proc_unlock(proc);
4193 return 0;
4194}
4195
4196static void binder_release_work(struct binder_proc *proc,
4197 struct list_head *list)
4198{
4199 struct binder_work *w;
4200
4201 while (1) {
4202 w = binder_dequeue_work_head(proc, list);
4203 if (!w)
4204 return;
4205
4206 switch (w->type) {
4207 case BINDER_WORK_TRANSACTION: {
4208 struct binder_transaction *t;
4209
4210 t = container_of(w, struct binder_transaction, work);
4211
4212 binder_cleanup_transaction(t, "process died.",
4213 BR_DEAD_REPLY);
4214 } break;
4215 case BINDER_WORK_RETURN_ERROR: {
4216 struct binder_error *e = container_of(
4217 w, struct binder_error, work);
4218
4219 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4220 "undelivered TRANSACTION_ERROR: %u\n",
4221 e->cmd);
4222 } break;
4223 case BINDER_WORK_TRANSACTION_COMPLETE: {
4224 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4225 "undelivered TRANSACTION_COMPLETE\n");
4226 kfree(w);
4227 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4228 } break;
4229 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4230 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4231 struct binder_ref_death *death;
4232
4233 death = container_of(w, struct binder_ref_death, work);
4234 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4235 "undelivered death notification, %016llx\n",
4236 (u64)death->cookie);
4237 kfree(death);
4238 binder_stats_deleted(BINDER_STAT_DEATH);
4239 } break;
4240 default:
4241 pr_err("unexpected work type, %d, not freed\n",
4242 w->type);
4243 break;
4244 }
4245 }
4246
4247}
4248
4249static struct binder_thread *binder_get_thread_ilocked(
4250 struct binder_proc *proc, struct binder_thread *new_thread)
4251{
4252 struct binder_thread *thread = NULL;
4253 struct rb_node *parent = NULL;
4254 struct rb_node **p = &proc->threads.rb_node;
4255
4256 while (*p) {
4257 parent = *p;
4258 thread = rb_entry(parent, struct binder_thread, rb_node);
4259
4260 if (current->pid < thread->pid)
4261 p = &(*p)->rb_left;
4262 else if (current->pid > thread->pid)
4263 p = &(*p)->rb_right;
4264 else
4265 return thread;
4266 }
4267 if (!new_thread)
4268 return NULL;
4269 thread = new_thread;
4270 binder_stats_created(BINDER_STAT_THREAD);
4271 thread->proc = proc;
4272 thread->pid = current->pid;
4273 atomic_set(&thread->tmp_ref, 0);
4274 init_waitqueue_head(&thread->wait);
4275 INIT_LIST_HEAD(&thread->todo);
4276 rb_link_node(&thread->rb_node, parent, p);
4277 rb_insert_color(&thread->rb_node, &proc->threads);
4278 thread->looper_need_return = true;
4279 thread->return_error.work.type = BINDER_WORK_RETURN_ERROR;
4280 thread->return_error.cmd = BR_OK;
4281 thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR;
4282 thread->reply_error.cmd = BR_OK;
4283 INIT_LIST_HEAD(&new_thread->waiting_thread_node);
4284 return thread;
4285}
4286
4287static struct binder_thread *binder_get_thread(struct binder_proc *proc)
4288{
4289 struct binder_thread *thread;
4290 struct binder_thread *new_thread;
4291
4292 binder_inner_proc_lock(proc);
4293 thread = binder_get_thread_ilocked(proc, NULL);
4294 binder_inner_proc_unlock(proc);
4295 if (!thread) {
4296 new_thread = kzalloc(sizeof(*thread), GFP_KERNEL);
4297 if (new_thread == NULL)
4298 return NULL;
4299 binder_inner_proc_lock(proc);
4300 thread = binder_get_thread_ilocked(proc, new_thread);
4301 binder_inner_proc_unlock(proc);
4302 if (thread != new_thread)
4303 kfree(new_thread);
4304 }
4305 return thread;
4306}
4307
4308static void binder_free_proc(struct binder_proc *proc)
4309{
4310 BUG_ON(!list_empty(&proc->todo));
4311 BUG_ON(!list_empty(&proc->delivered_death));
4312 binder_alloc_deferred_release(&proc->alloc);
4313 put_task_struct(proc->tsk);
4314 binder_stats_deleted(BINDER_STAT_PROC);
4315 kfree(proc);
4316}
4317
4318static void binder_free_thread(struct binder_thread *thread)
4319{
4320 BUG_ON(!list_empty(&thread->todo));
4321 binder_stats_deleted(BINDER_STAT_THREAD);
4322 binder_proc_dec_tmpref(thread->proc);
4323 kfree(thread);
4324}
4325
4326static int binder_thread_release(struct binder_proc *proc,
4327 struct binder_thread *thread)
4328{
4329 struct binder_transaction *t;
4330 struct binder_transaction *send_reply = NULL;
4331 int active_transactions = 0;
4332 struct binder_transaction *last_t = NULL;
4333
4334 binder_inner_proc_lock(thread->proc);
4335 /*
4336 * take a ref on the proc so it survives
4337 * after we remove this thread from proc->threads.
4338 * The corresponding dec is when we actually
4339 * free the thread in binder_free_thread()
4340 */
4341 proc->tmp_ref++;
4342 /*
4343 * take a ref on this thread to ensure it
4344 * survives while we are releasing it
4345 */
4346 atomic_inc(&thread->tmp_ref);
4347 rb_erase(&thread->rb_node, &proc->threads);
4348 t = thread->transaction_stack;
4349 if (t) {
4350 spin_lock(&t->lock);
4351 if (t->to_thread == thread)
4352 send_reply = t;
4353 }
4354 thread->is_dead = true;
4355
4356 while (t) {
4357 last_t = t;
4358 active_transactions++;
4359 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4360 "release %d:%d transaction %d %s, still active\n",
4361 proc->pid, thread->pid,
4362 t->debug_id,
4363 (t->to_thread == thread) ? "in" : "out");
4364
4365 if (t->to_thread == thread) {
4366 t->to_proc = NULL;
4367 t->to_thread = NULL;
4368 if (t->buffer) {
4369 t->buffer->transaction = NULL;
4370 t->buffer = NULL;
4371 }
4372 t = t->to_parent;
4373 } else if (t->from == thread) {
4374 t->from = NULL;
4375 t = t->from_parent;
4376 } else
4377 BUG();
4378 spin_unlock(&last_t->lock);
4379 if (t)
4380 spin_lock(&t->lock);
4381 }
4382
4383 /*
4384 * If this thread used poll, make sure we remove the waitqueue
4385 * from any epoll data structures holding it with POLLFREE.
4386 * waitqueue_active() is safe to use here because we're holding
4387 * the inner lock.
4388 */
4389 if ((thread->looper & BINDER_LOOPER_STATE_POLL) &&
4390 waitqueue_active(&thread->wait)) {
4391 wake_up_poll(&thread->wait, EPOLLHUP | POLLFREE);
4392 }
4393
4394 binder_inner_proc_unlock(thread->proc);
4395
4396 /*
4397 * This is needed to avoid races between wake_up_poll() above and
4398 * and ep_remove_waitqueue() called for other reasons (eg the epoll file
4399 * descriptor being closed); ep_remove_waitqueue() holds an RCU read
4400 * lock, so we can be sure it's done after calling synchronize_rcu().
4401 */
4402 if (thread->looper & BINDER_LOOPER_STATE_POLL)
4403 synchronize_rcu();
4404
4405 if (send_reply)
4406 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
4407 binder_release_work(proc, &thread->todo);
4408 binder_thread_dec_tmpref(thread);
4409 return active_transactions;
4410}
4411
4412static __poll_t binder_poll(struct file *filp,
4413 struct poll_table_struct *wait)
4414{
4415 struct binder_proc *proc = filp->private_data;
4416 struct binder_thread *thread = NULL;
4417 bool wait_for_proc_work;
4418
4419 thread = binder_get_thread(proc);
4420 if (!thread)
4421 return POLLERR;
4422
4423 binder_inner_proc_lock(thread->proc);
4424 thread->looper |= BINDER_LOOPER_STATE_POLL;
4425 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
4426
4427 binder_inner_proc_unlock(thread->proc);
4428
4429 poll_wait(filp, &thread->wait, wait);
4430
4431 if (binder_has_work(thread, wait_for_proc_work))
4432 return EPOLLIN;
4433
4434 return 0;
4435}
4436
4437static int binder_ioctl_write_read(struct file *filp,
4438 unsigned int cmd, unsigned long arg,
4439 struct binder_thread *thread)
4440{
4441 int ret = 0;
4442 struct binder_proc *proc = filp->private_data;
4443 unsigned int size = _IOC_SIZE(cmd);
4444 void __user *ubuf = (void __user *)arg;
4445 struct binder_write_read bwr;
4446
4447 if (size != sizeof(struct binder_write_read)) {
4448 ret = -EINVAL;
4449 goto out;
4450 }
4451 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
4452 ret = -EFAULT;
4453 goto out;
4454 }
4455 binder_debug(BINDER_DEBUG_READ_WRITE,
4456 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
4457 proc->pid, thread->pid,
4458 (u64)bwr.write_size, (u64)bwr.write_buffer,
4459 (u64)bwr.read_size, (u64)bwr.read_buffer);
4460
4461 if (bwr.write_size > 0) {
4462 ret = binder_thread_write(proc, thread,
4463 bwr.write_buffer,
4464 bwr.write_size,
4465 &bwr.write_consumed);
4466 trace_binder_write_done(ret);
4467 if (ret < 0) {
4468 bwr.read_consumed = 0;
4469 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4470 ret = -EFAULT;
4471 goto out;
4472 }
4473 }
4474 if (bwr.read_size > 0) {
4475 ret = binder_thread_read(proc, thread, bwr.read_buffer,
4476 bwr.read_size,
4477 &bwr.read_consumed,
4478 filp->f_flags & O_NONBLOCK);
4479 trace_binder_read_done(ret);
4480 binder_inner_proc_lock(proc);
4481 if (!binder_worklist_empty_ilocked(&proc->todo))
4482 binder_wakeup_proc_ilocked(proc);
4483 binder_inner_proc_unlock(proc);
4484 if (ret < 0) {
4485 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4486 ret = -EFAULT;
4487 goto out;
4488 }
4489 }
4490 binder_debug(BINDER_DEBUG_READ_WRITE,
4491 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
4492 proc->pid, thread->pid,
4493 (u64)bwr.write_consumed, (u64)bwr.write_size,
4494 (u64)bwr.read_consumed, (u64)bwr.read_size);
4495 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
4496 ret = -EFAULT;
4497 goto out;
4498 }
4499out:
4500 return ret;
4501}
4502
4503static int binder_ioctl_set_ctx_mgr(struct file *filp)
4504{
4505 int ret = 0;
4506 struct binder_proc *proc = filp->private_data;
4507 struct binder_context *context = proc->context;
4508 struct binder_node *new_node;
4509 kuid_t curr_euid = current_euid();
4510
4511 mutex_lock(&context->context_mgr_node_lock);
4512 if (context->binder_context_mgr_node) {
4513 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
4514 ret = -EBUSY;
4515 goto out;
4516 }
4517 ret = security_binder_set_context_mgr(proc->tsk);
4518 if (ret < 0)
4519 goto out;
4520 if (uid_valid(context->binder_context_mgr_uid)) {
4521 if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
4522 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
4523 from_kuid(&init_user_ns, curr_euid),
4524 from_kuid(&init_user_ns,
4525 context->binder_context_mgr_uid));
4526 ret = -EPERM;
4527 goto out;
4528 }
4529 } else {
4530 context->binder_context_mgr_uid = curr_euid;
4531 }
4532 new_node = binder_new_node(proc, NULL);
4533 if (!new_node) {
4534 ret = -ENOMEM;
4535 goto out;
4536 }
4537 binder_node_lock(new_node);
4538 new_node->local_weak_refs++;
4539 new_node->local_strong_refs++;
4540 new_node->has_strong_ref = 1;
4541 new_node->has_weak_ref = 1;
4542 context->binder_context_mgr_node = new_node;
4543 binder_node_unlock(new_node);
4544 binder_put_node(new_node);
4545out:
4546 mutex_unlock(&context->context_mgr_node_lock);
4547 return ret;
4548}
4549
4550static int binder_ioctl_get_node_debug_info(struct binder_proc *proc,
4551 struct binder_node_debug_info *info)
4552{
4553 struct rb_node *n;
4554 binder_uintptr_t ptr = info->ptr;
4555
4556 memset(info, 0, sizeof(*info));
4557
4558 binder_inner_proc_lock(proc);
4559 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
4560 struct binder_node *node = rb_entry(n, struct binder_node,
4561 rb_node);
4562 if (node->ptr > ptr) {
4563 info->ptr = node->ptr;
4564 info->cookie = node->cookie;
4565 info->has_strong_ref = node->has_strong_ref;
4566 info->has_weak_ref = node->has_weak_ref;
4567 break;
4568 }
4569 }
4570 binder_inner_proc_unlock(proc);
4571
4572 return 0;
4573}
4574
4575static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4576{
4577 int ret;
4578 struct binder_proc *proc = filp->private_data;
4579 struct binder_thread *thread;
4580 unsigned int size = _IOC_SIZE(cmd);
4581 void __user *ubuf = (void __user *)arg;
4582
4583 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
4584 proc->pid, current->pid, cmd, arg);*/
4585
4586 binder_selftest_alloc(&proc->alloc);
4587
4588 trace_binder_ioctl(cmd, arg);
4589
4590 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4591 if (ret)
4592 goto err_unlocked;
4593
4594 thread = binder_get_thread(proc);
4595 if (thread == NULL) {
4596 ret = -ENOMEM;
4597 goto err;
4598 }
4599
4600 switch (cmd) {
4601 case BINDER_WRITE_READ:
4602 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
4603 if (ret)
4604 goto err;
4605 break;
4606 case BINDER_SET_MAX_THREADS: {
4607 int max_threads;
4608
4609 if (copy_from_user(&max_threads, ubuf,
4610 sizeof(max_threads))) {
4611 ret = -EINVAL;
4612 goto err;
4613 }
4614 binder_inner_proc_lock(proc);
4615 proc->max_threads = max_threads;
4616 binder_inner_proc_unlock(proc);
4617 break;
4618 }
4619 case BINDER_SET_CONTEXT_MGR:
4620 ret = binder_ioctl_set_ctx_mgr(filp);
4621 if (ret)
4622 goto err;
4623 break;
4624 case BINDER_THREAD_EXIT:
4625 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
4626 proc->pid, thread->pid);
4627 binder_thread_release(proc, thread);
4628 thread = NULL;
4629 break;
4630 case BINDER_VERSION: {
4631 struct binder_version __user *ver = ubuf;
4632
4633 if (size != sizeof(struct binder_version)) {
4634 ret = -EINVAL;
4635 goto err;
4636 }
4637 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
4638 &ver->protocol_version)) {
4639 ret = -EINVAL;
4640 goto err;
4641 }
4642 break;
4643 }
4644 case BINDER_GET_NODE_DEBUG_INFO: {
4645 struct binder_node_debug_info info;
4646
4647 if (copy_from_user(&info, ubuf, sizeof(info))) {
4648 ret = -EFAULT;
4649 goto err;
4650 }
4651
4652 ret = binder_ioctl_get_node_debug_info(proc, &info);
4653 if (ret < 0)
4654 goto err;
4655
4656 if (copy_to_user(ubuf, &info, sizeof(info))) {
4657 ret = -EFAULT;
4658 goto err;
4659 }
4660 break;
4661 }
4662 default:
4663 ret = -EINVAL;
4664 goto err;
4665 }
4666 ret = 0;
4667err:
4668 if (thread)
4669 thread->looper_need_return = false;
4670 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4671 if (ret && ret != -ERESTARTSYS)
4672 pr_info("%d:%d ioctl %x %lx returned %d\n", proc->pid, current->pid, cmd, arg, ret);
4673err_unlocked:
4674 trace_binder_ioctl_done(ret);
4675 return ret;
4676}
4677
4678static void binder_vma_open(struct vm_area_struct *vma)
4679{
4680 struct binder_proc *proc = vma->vm_private_data;
4681
4682 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4683 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
4684 proc->pid, vma->vm_start, vma->vm_end,
4685 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4686 (unsigned long)pgprot_val(vma->vm_page_prot));
4687}
4688
4689static void binder_vma_close(struct vm_area_struct *vma)
4690{
4691 struct binder_proc *proc = vma->vm_private_data;
4692
4693 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4694 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
4695 proc->pid, vma->vm_start, vma->vm_end,
4696 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4697 (unsigned long)pgprot_val(vma->vm_page_prot));
4698 binder_alloc_vma_close(&proc->alloc);
4699 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
4700}
4701
4702static vm_fault_t binder_vm_fault(struct vm_fault *vmf)
4703{
4704 return VM_FAULT_SIGBUS;
4705}
4706
4707static const struct vm_operations_struct binder_vm_ops = {
4708 .open = binder_vma_open,
4709 .close = binder_vma_close,
4710 .fault = binder_vm_fault,
4711};
4712
4713static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
4714{
4715 int ret;
4716 struct binder_proc *proc = filp->private_data;
4717 const char *failure_string;
4718
4719 if (proc->tsk != current->group_leader)
4720 return -EINVAL;
4721
4722 if ((vma->vm_end - vma->vm_start) > SZ_4M)
4723 vma->vm_end = vma->vm_start + SZ_4M;
4724
4725 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4726 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
4727 __func__, proc->pid, vma->vm_start, vma->vm_end,
4728 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4729 (unsigned long)pgprot_val(vma->vm_page_prot));
4730
4731 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
4732 ret = -EPERM;
4733 failure_string = "bad vm_flags";
4734 goto err_bad_arg;
4735 }
4736 vma->vm_flags |= VM_DONTCOPY | VM_MIXEDMAP;
4737 vma->vm_flags &= ~VM_MAYWRITE;
4738
4739 vma->vm_ops = &binder_vm_ops;
4740 vma->vm_private_data = proc;
4741
4742 ret = binder_alloc_mmap_handler(&proc->alloc, vma);
4743 if (ret)
4744 return ret;
4745 mutex_lock(&proc->files_lock);
4746 proc->files = get_files_struct(current);
4747 mutex_unlock(&proc->files_lock);
4748 return 0;
4749
4750err_bad_arg:
4751 pr_err("%s: %d %lx-%lx %s failed %d\n", __func__,
4752 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
4753 return ret;
4754}
4755
4756static int binder_open(struct inode *nodp, struct file *filp)
4757{
4758 struct binder_proc *proc;
4759 struct binder_device *binder_dev;
4760
4761 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "%s: %d:%d\n", __func__,
4762 current->group_leader->pid, current->pid);
4763
4764 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
4765 if (proc == NULL)
4766 return -ENOMEM;
4767 spin_lock_init(&proc->inner_lock);
4768 spin_lock_init(&proc->outer_lock);
4769 get_task_struct(current->group_leader);
4770 proc->tsk = current->group_leader;
4771 mutex_init(&proc->files_lock);
4772 INIT_LIST_HEAD(&proc->todo);
4773 proc->default_priority = task_nice(current);
4774 binder_dev = container_of(filp->private_data, struct binder_device,
4775 miscdev);
4776 proc->context = &binder_dev->context;
4777 binder_alloc_init(&proc->alloc);
4778
4779 binder_stats_created(BINDER_STAT_PROC);
4780 proc->pid = current->group_leader->pid;
4781 INIT_LIST_HEAD(&proc->delivered_death);
4782 INIT_LIST_HEAD(&proc->waiting_threads);
4783 filp->private_data = proc;
4784
4785 mutex_lock(&binder_procs_lock);
4786 hlist_add_head(&proc->proc_node, &binder_procs);
4787 mutex_unlock(&binder_procs_lock);
4788
4789 if (binder_debugfs_dir_entry_proc) {
4790 char strbuf[11];
4791
4792 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
4793 /*
4794 * proc debug entries are shared between contexts, so
4795 * this will fail if the process tries to open the driver
4796 * again with a different context. The priting code will
4797 * anyway print all contexts that a given PID has, so this
4798 * is not a problem.
4799 */
4800 proc->debugfs_entry = debugfs_create_file(strbuf, 0444,
4801 binder_debugfs_dir_entry_proc,
4802 (void *)(unsigned long)proc->pid,
4803 &binder_proc_fops);
4804 }
4805
4806 return 0;
4807}
4808
4809static int binder_flush(struct file *filp, fl_owner_t id)
4810{
4811 struct binder_proc *proc = filp->private_data;
4812
4813 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
4814
4815 return 0;
4816}
4817
4818static void binder_deferred_flush(struct binder_proc *proc)
4819{
4820 struct rb_node *n;
4821 int wake_count = 0;
4822
4823 binder_inner_proc_lock(proc);
4824 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
4825 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
4826
4827 thread->looper_need_return = true;
4828 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
4829 wake_up_interruptible(&thread->wait);
4830 wake_count++;
4831 }
4832 }
4833 binder_inner_proc_unlock(proc);
4834
4835 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4836 "binder_flush: %d woke %d threads\n", proc->pid,
4837 wake_count);
4838}
4839
4840static int binder_release(struct inode *nodp, struct file *filp)
4841{
4842 struct binder_proc *proc = filp->private_data;
4843
4844 debugfs_remove(proc->debugfs_entry);
4845 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
4846
4847 return 0;
4848}
4849
4850static int binder_node_release(struct binder_node *node, int refs)
4851{
4852 struct binder_ref *ref;
4853 int death = 0;
4854 struct binder_proc *proc = node->proc;
4855
4856 binder_release_work(proc, &node->async_todo);
4857
4858 binder_node_lock(node);
4859 binder_inner_proc_lock(proc);
4860 binder_dequeue_work_ilocked(&node->work);
4861 /*
4862 * The caller must have taken a temporary ref on the node,
4863 */
4864 BUG_ON(!node->tmp_refs);
4865 if (hlist_empty(&node->refs) && node->tmp_refs == 1) {
4866 binder_inner_proc_unlock(proc);
4867 binder_node_unlock(node);
4868 binder_free_node(node);
4869
4870 return refs;
4871 }
4872
4873 node->proc = NULL;
4874 node->local_strong_refs = 0;
4875 node->local_weak_refs = 0;
4876 binder_inner_proc_unlock(proc);
4877
4878 spin_lock(&binder_dead_nodes_lock);
4879 hlist_add_head(&node->dead_node, &binder_dead_nodes);
4880 spin_unlock(&binder_dead_nodes_lock);
4881
4882 hlist_for_each_entry(ref, &node->refs, node_entry) {
4883 refs++;
4884 /*
4885 * Need the node lock to synchronize
4886 * with new notification requests and the
4887 * inner lock to synchronize with queued
4888 * death notifications.
4889 */
4890 binder_inner_proc_lock(ref->proc);
4891 if (!ref->death) {
4892 binder_inner_proc_unlock(ref->proc);
4893 continue;
4894 }
4895
4896 death++;
4897
4898 BUG_ON(!list_empty(&ref->death->work.entry));
4899 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
4900 binder_enqueue_work_ilocked(&ref->death->work,
4901 &ref->proc->todo);
4902 binder_wakeup_proc_ilocked(ref->proc);
4903 binder_inner_proc_unlock(ref->proc);
4904 }
4905
4906 binder_debug(BINDER_DEBUG_DEAD_BINDER,
4907 "node %d now dead, refs %d, death %d\n",
4908 node->debug_id, refs, death);
4909 binder_node_unlock(node);
4910 binder_put_node(node);
4911
4912 return refs;
4913}
4914
4915static void binder_deferred_release(struct binder_proc *proc)
4916{
4917 struct binder_context *context = proc->context;
4918 struct rb_node *n;
4919 int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
4920
4921 BUG_ON(proc->files);
4922
4923 mutex_lock(&binder_procs_lock);
4924 hlist_del(&proc->proc_node);
4925 mutex_unlock(&binder_procs_lock);
4926
4927 mutex_lock(&context->context_mgr_node_lock);
4928 if (context->binder_context_mgr_node &&
4929 context->binder_context_mgr_node->proc == proc) {
4930 binder_debug(BINDER_DEBUG_DEAD_BINDER,
4931 "%s: %d context_mgr_node gone\n",
4932 __func__, proc->pid);
4933 context->binder_context_mgr_node = NULL;
4934 }
4935 mutex_unlock(&context->context_mgr_node_lock);
4936 binder_inner_proc_lock(proc);
4937 /*
4938 * Make sure proc stays alive after we
4939 * remove all the threads
4940 */
4941 proc->tmp_ref++;
4942
4943 proc->is_dead = true;
4944 threads = 0;
4945 active_transactions = 0;
4946 while ((n = rb_first(&proc->threads))) {
4947 struct binder_thread *thread;
4948
4949 thread = rb_entry(n, struct binder_thread, rb_node);
4950 binder_inner_proc_unlock(proc);
4951 threads++;
4952 active_transactions += binder_thread_release(proc, thread);
4953 binder_inner_proc_lock(proc);
4954 }
4955
4956 nodes = 0;
4957 incoming_refs = 0;
4958 while ((n = rb_first(&proc->nodes))) {
4959 struct binder_node *node;
4960
4961 node = rb_entry(n, struct binder_node, rb_node);
4962 nodes++;
4963 /*
4964 * take a temporary ref on the node before
4965 * calling binder_node_release() which will either
4966 * kfree() the node or call binder_put_node()
4967 */
4968 binder_inc_node_tmpref_ilocked(node);
4969 rb_erase(&node->rb_node, &proc->nodes);
4970 binder_inner_proc_unlock(proc);
4971 incoming_refs = binder_node_release(node, incoming_refs);
4972 binder_inner_proc_lock(proc);
4973 }
4974 binder_inner_proc_unlock(proc);
4975
4976 outgoing_refs = 0;
4977 binder_proc_lock(proc);
4978 while ((n = rb_first(&proc->refs_by_desc))) {
4979 struct binder_ref *ref;
4980
4981 ref = rb_entry(n, struct binder_ref, rb_node_desc);
4982 outgoing_refs++;
4983 binder_cleanup_ref_olocked(ref);
4984 binder_proc_unlock(proc);
4985 binder_free_ref(ref);
4986 binder_proc_lock(proc);
4987 }
4988 binder_proc_unlock(proc);
4989
4990 binder_release_work(proc, &proc->todo);
4991 binder_release_work(proc, &proc->delivered_death);
4992
4993 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4994 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
4995 __func__, proc->pid, threads, nodes, incoming_refs,
4996 outgoing_refs, active_transactions);
4997
4998 binder_proc_dec_tmpref(proc);
4999}
5000
5001static void binder_deferred_func(struct work_struct *work)
5002{
5003 struct binder_proc *proc;
5004 struct files_struct *files;
5005
5006 int defer;
5007
5008 do {
5009 mutex_lock(&binder_deferred_lock);
5010 if (!hlist_empty(&binder_deferred_list)) {
5011 proc = hlist_entry(binder_deferred_list.first,
5012 struct binder_proc, deferred_work_node);
5013 hlist_del_init(&proc->deferred_work_node);
5014 defer = proc->deferred_work;
5015 proc->deferred_work = 0;
5016 } else {
5017 proc = NULL;
5018 defer = 0;
5019 }
5020 mutex_unlock(&binder_deferred_lock);
5021
5022 files = NULL;
5023 if (defer & BINDER_DEFERRED_PUT_FILES) {
5024 mutex_lock(&proc->files_lock);
5025 files = proc->files;
5026 if (files)
5027 proc->files = NULL;
5028 mutex_unlock(&proc->files_lock);
5029 }
5030
5031 if (defer & BINDER_DEFERRED_FLUSH)
5032 binder_deferred_flush(proc);
5033
5034 if (defer & BINDER_DEFERRED_RELEASE)
5035 binder_deferred_release(proc); /* frees proc */
5036
5037 if (files)
5038 put_files_struct(files);
5039 } while (proc);
5040}
5041static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
5042
5043static void
5044binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
5045{
5046 mutex_lock(&binder_deferred_lock);
5047 proc->deferred_work |= defer;
5048 if (hlist_unhashed(&proc->deferred_work_node)) {
5049 hlist_add_head(&proc->deferred_work_node,
5050 &binder_deferred_list);
5051 schedule_work(&binder_deferred_work);
5052 }
5053 mutex_unlock(&binder_deferred_lock);
5054}
5055
5056static void print_binder_transaction_ilocked(struct seq_file *m,
5057 struct binder_proc *proc,
5058 const char *prefix,
5059 struct binder_transaction *t)
5060{
5061 struct binder_proc *to_proc;
5062 struct binder_buffer *buffer = t->buffer;
5063
5064 spin_lock(&t->lock);
5065 to_proc = t->to_proc;
5066 seq_printf(m,
5067 "%s %d: %pK from %d:%d to %d:%d code %x flags %x pri %ld r%d",
5068 prefix, t->debug_id, t,
5069 t->from ? t->from->proc->pid : 0,
5070 t->from ? t->from->pid : 0,
5071 to_proc ? to_proc->pid : 0,
5072 t->to_thread ? t->to_thread->pid : 0,
5073 t->code, t->flags, t->priority, t->need_reply);
5074 spin_unlock(&t->lock);
5075
5076 if (proc != to_proc) {
5077 /*
5078 * Can only safely deref buffer if we are holding the
5079 * correct proc inner lock for this node
5080 */
5081 seq_puts(m, "\n");
5082 return;
5083 }
5084
5085 if (buffer == NULL) {
5086 seq_puts(m, " buffer free\n");
5087 return;
5088 }
5089 if (buffer->target_node)
5090 seq_printf(m, " node %d", buffer->target_node->debug_id);
5091 seq_printf(m, " size %zd:%zd data %pK\n",
5092 buffer->data_size, buffer->offsets_size,
5093 buffer->data);
5094}
5095
5096static void print_binder_work_ilocked(struct seq_file *m,
5097 struct binder_proc *proc,
5098 const char *prefix,
5099 const char *transaction_prefix,
5100 struct binder_work *w)
5101{
5102 struct binder_node *node;
5103 struct binder_transaction *t;
5104
5105 switch (w->type) {
5106 case BINDER_WORK_TRANSACTION:
5107 t = container_of(w, struct binder_transaction, work);
5108 print_binder_transaction_ilocked(
5109 m, proc, transaction_prefix, t);
5110 break;
5111 case BINDER_WORK_RETURN_ERROR: {
5112 struct binder_error *e = container_of(
5113 w, struct binder_error, work);
5114
5115 seq_printf(m, "%stransaction error: %u\n",
5116 prefix, e->cmd);
5117 } break;
5118 case BINDER_WORK_TRANSACTION_COMPLETE:
5119 seq_printf(m, "%stransaction complete\n", prefix);
5120 break;
5121 case BINDER_WORK_NODE:
5122 node = container_of(w, struct binder_node, work);
5123 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
5124 prefix, node->debug_id,
5125 (u64)node->ptr, (u64)node->cookie);
5126 break;
5127 case BINDER_WORK_DEAD_BINDER:
5128 seq_printf(m, "%shas dead binder\n", prefix);
5129 break;
5130 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
5131 seq_printf(m, "%shas cleared dead binder\n", prefix);
5132 break;
5133 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
5134 seq_printf(m, "%shas cleared death notification\n", prefix);
5135 break;
5136 default:
5137 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
5138 break;
5139 }
5140}
5141
5142static void print_binder_thread_ilocked(struct seq_file *m,
5143 struct binder_thread *thread,
5144 int print_always)
5145{
5146 struct binder_transaction *t;
5147 struct binder_work *w;
5148 size_t start_pos = m->count;
5149 size_t header_pos;
5150
5151 seq_printf(m, " thread %d: l %02x need_return %d tr %d\n",
5152 thread->pid, thread->looper,
5153 thread->looper_need_return,
5154 atomic_read(&thread->tmp_ref));
5155 header_pos = m->count;
5156 t = thread->transaction_stack;
5157 while (t) {
5158 if (t->from == thread) {
5159 print_binder_transaction_ilocked(m, thread->proc,
5160 " outgoing transaction", t);
5161 t = t->from_parent;
5162 } else if (t->to_thread == thread) {
5163 print_binder_transaction_ilocked(m, thread->proc,
5164 " incoming transaction", t);
5165 t = t->to_parent;
5166 } else {
5167 print_binder_transaction_ilocked(m, thread->proc,
5168 " bad transaction", t);
5169 t = NULL;
5170 }
5171 }
5172 list_for_each_entry(w, &thread->todo, entry) {
5173 print_binder_work_ilocked(m, thread->proc, " ",
5174 " pending transaction", w);
5175 }
5176 if (!print_always && m->count == header_pos)
5177 m->count = start_pos;
5178}
5179
5180static void print_binder_node_nilocked(struct seq_file *m,
5181 struct binder_node *node)
5182{
5183 struct binder_ref *ref;
5184 struct binder_work *w;
5185 int count;
5186
5187 count = 0;
5188 hlist_for_each_entry(ref, &node->refs, node_entry)
5189 count++;
5190
5191 seq_printf(m, " node %d: u%016llx c%016llx hs %d hw %d ls %d lw %d is %d iw %d tr %d",
5192 node->debug_id, (u64)node->ptr, (u64)node->cookie,
5193 node->has_strong_ref, node->has_weak_ref,
5194 node->local_strong_refs, node->local_weak_refs,
5195 node->internal_strong_refs, count, node->tmp_refs);
5196 if (count) {
5197 seq_puts(m, " proc");
5198 hlist_for_each_entry(ref, &node->refs, node_entry)
5199 seq_printf(m, " %d", ref->proc->pid);
5200 }
5201 seq_puts(m, "\n");
5202 if (node->proc) {
5203 list_for_each_entry(w, &node->async_todo, entry)
5204 print_binder_work_ilocked(m, node->proc, " ",
5205 " pending async transaction", w);
5206 }
5207}
5208
5209static void print_binder_ref_olocked(struct seq_file *m,
5210 struct binder_ref *ref)
5211{
5212 binder_node_lock(ref->node);
5213 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
5214 ref->data.debug_id, ref->data.desc,
5215 ref->node->proc ? "" : "dead ",
5216 ref->node->debug_id, ref->data.strong,
5217 ref->data.weak, ref->death);
5218 binder_node_unlock(ref->node);
5219}
5220
5221static void print_binder_proc(struct seq_file *m,
5222 struct binder_proc *proc, int print_all)
5223{
5224 struct binder_work *w;
5225 struct rb_node *n;
5226 size_t start_pos = m->count;
5227 size_t header_pos;
5228 struct binder_node *last_node = NULL;
5229
5230 seq_printf(m, "proc %d\n", proc->pid);
5231 seq_printf(m, "context %s\n", proc->context->name);
5232 header_pos = m->count;
5233
5234 binder_inner_proc_lock(proc);
5235 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
5236 print_binder_thread_ilocked(m, rb_entry(n, struct binder_thread,
5237 rb_node), print_all);
5238
5239 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
5240 struct binder_node *node = rb_entry(n, struct binder_node,
5241 rb_node);
5242 /*
5243 * take a temporary reference on the node so it
5244 * survives and isn't removed from the tree
5245 * while we print it.
5246 */
5247 binder_inc_node_tmpref_ilocked(node);
5248 /* Need to drop inner lock to take node lock */
5249 binder_inner_proc_unlock(proc);
5250 if (last_node)
5251 binder_put_node(last_node);
5252 binder_node_inner_lock(node);
5253 print_binder_node_nilocked(m, node);
5254 binder_node_inner_unlock(node);
5255 last_node = node;
5256 binder_inner_proc_lock(proc);
5257 }
5258 binder_inner_proc_unlock(proc);
5259 if (last_node)
5260 binder_put_node(last_node);
5261
5262 if (print_all) {
5263 binder_proc_lock(proc);
5264 for (n = rb_first(&proc->refs_by_desc);
5265 n != NULL;
5266 n = rb_next(n))
5267 print_binder_ref_olocked(m, rb_entry(n,
5268 struct binder_ref,
5269 rb_node_desc));
5270 binder_proc_unlock(proc);
5271 }
5272 binder_alloc_print_allocated(m, &proc->alloc);
5273 binder_inner_proc_lock(proc);
5274 list_for_each_entry(w, &proc->todo, entry)
5275 print_binder_work_ilocked(m, proc, " ",
5276 " pending transaction", w);
5277 list_for_each_entry(w, &proc->delivered_death, entry) {
5278 seq_puts(m, " has delivered dead binder\n");
5279 break;
5280 }
5281 binder_inner_proc_unlock(proc);
5282 if (!print_all && m->count == header_pos)
5283 m->count = start_pos;
5284}
5285
5286static const char * const binder_return_strings[] = {
5287 "BR_ERROR",
5288 "BR_OK",
5289 "BR_TRANSACTION",
5290 "BR_REPLY",
5291 "BR_ACQUIRE_RESULT",
5292 "BR_DEAD_REPLY",
5293 "BR_TRANSACTION_COMPLETE",
5294 "BR_INCREFS",
5295 "BR_ACQUIRE",
5296 "BR_RELEASE",
5297 "BR_DECREFS",
5298 "BR_ATTEMPT_ACQUIRE",
5299 "BR_NOOP",
5300 "BR_SPAWN_LOOPER",
5301 "BR_FINISHED",
5302 "BR_DEAD_BINDER",
5303 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
5304 "BR_FAILED_REPLY"
5305};
5306
5307static const char * const binder_command_strings[] = {
5308 "BC_TRANSACTION",
5309 "BC_REPLY",
5310 "BC_ACQUIRE_RESULT",
5311 "BC_FREE_BUFFER",
5312 "BC_INCREFS",
5313 "BC_ACQUIRE",
5314 "BC_RELEASE",
5315 "BC_DECREFS",
5316 "BC_INCREFS_DONE",
5317 "BC_ACQUIRE_DONE",
5318 "BC_ATTEMPT_ACQUIRE",
5319 "BC_REGISTER_LOOPER",
5320 "BC_ENTER_LOOPER",
5321 "BC_EXIT_LOOPER",
5322 "BC_REQUEST_DEATH_NOTIFICATION",
5323 "BC_CLEAR_DEATH_NOTIFICATION",
5324 "BC_DEAD_BINDER_DONE",
5325 "BC_TRANSACTION_SG",
5326 "BC_REPLY_SG",
5327};
5328
5329static const char * const binder_objstat_strings[] = {
5330 "proc",
5331 "thread",
5332 "node",
5333 "ref",
5334 "death",
5335 "transaction",
5336 "transaction_complete"
5337};
5338
5339static void print_binder_stats(struct seq_file *m, const char *prefix,
5340 struct binder_stats *stats)
5341{
5342 int i;
5343
5344 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
5345 ARRAY_SIZE(binder_command_strings));
5346 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
5347 int temp = atomic_read(&stats->bc[i]);
5348
5349 if (temp)
5350 seq_printf(m, "%s%s: %d\n", prefix,
5351 binder_command_strings[i], temp);
5352 }
5353
5354 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
5355 ARRAY_SIZE(binder_return_strings));
5356 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
5357 int temp = atomic_read(&stats->br[i]);
5358
5359 if (temp)
5360 seq_printf(m, "%s%s: %d\n", prefix,
5361 binder_return_strings[i], temp);
5362 }
5363
5364 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
5365 ARRAY_SIZE(binder_objstat_strings));
5366 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
5367 ARRAY_SIZE(stats->obj_deleted));
5368 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
5369 int created = atomic_read(&stats->obj_created[i]);
5370 int deleted = atomic_read(&stats->obj_deleted[i]);
5371
5372 if (created || deleted)
5373 seq_printf(m, "%s%s: active %d total %d\n",
5374 prefix,
5375 binder_objstat_strings[i],
5376 created - deleted,
5377 created);
5378 }
5379}
5380
5381static void print_binder_proc_stats(struct seq_file *m,
5382 struct binder_proc *proc)
5383{
5384 struct binder_work *w;
5385 struct binder_thread *thread;
5386 struct rb_node *n;
5387 int count, strong, weak, ready_threads;
5388 size_t free_async_space =
5389 binder_alloc_get_free_async_space(&proc->alloc);
5390
5391 seq_printf(m, "proc %d\n", proc->pid);
5392 seq_printf(m, "context %s\n", proc->context->name);
5393 count = 0;
5394 ready_threads = 0;
5395 binder_inner_proc_lock(proc);
5396 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
5397 count++;
5398
5399 list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node)
5400 ready_threads++;
5401
5402 seq_printf(m, " threads: %d\n", count);
5403 seq_printf(m, " requested threads: %d+%d/%d\n"
5404 " ready threads %d\n"
5405 " free async space %zd\n", proc->requested_threads,
5406 proc->requested_threads_started, proc->max_threads,
5407 ready_threads,
5408 free_async_space);
5409 count = 0;
5410 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
5411 count++;
5412 binder_inner_proc_unlock(proc);
5413 seq_printf(m, " nodes: %d\n", count);
5414 count = 0;
5415 strong = 0;
5416 weak = 0;
5417 binder_proc_lock(proc);
5418 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
5419 struct binder_ref *ref = rb_entry(n, struct binder_ref,
5420 rb_node_desc);
5421 count++;
5422 strong += ref->data.strong;
5423 weak += ref->data.weak;
5424 }
5425 binder_proc_unlock(proc);
5426 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
5427
5428 count = binder_alloc_get_allocated_count(&proc->alloc);
5429 seq_printf(m, " buffers: %d\n", count);
5430
5431 binder_alloc_print_pages(m, &proc->alloc);
5432
5433 count = 0;
5434 binder_inner_proc_lock(proc);
5435 list_for_each_entry(w, &proc->todo, entry) {
5436 if (w->type == BINDER_WORK_TRANSACTION)
5437 count++;
5438 }
5439 binder_inner_proc_unlock(proc);
5440 seq_printf(m, " pending transactions: %d\n", count);
5441
5442 print_binder_stats(m, " ", &proc->stats);
5443}
5444
5445
5446static int binder_state_show(struct seq_file *m, void *unused)
5447{
5448 struct binder_proc *proc;
5449 struct binder_node *node;
5450 struct binder_node *last_node = NULL;
5451
5452 seq_puts(m, "binder state:\n");
5453
5454 spin_lock(&binder_dead_nodes_lock);
5455 if (!hlist_empty(&binder_dead_nodes))
5456 seq_puts(m, "dead nodes:\n");
5457 hlist_for_each_entry(node, &binder_dead_nodes, dead_node) {
5458 /*
5459 * take a temporary reference on the node so it
5460 * survives and isn't removed from the list
5461 * while we print it.
5462 */
5463 node->tmp_refs++;
5464 spin_unlock(&binder_dead_nodes_lock);
5465 if (last_node)
5466 binder_put_node(last_node);
5467 binder_node_lock(node);
5468 print_binder_node_nilocked(m, node);
5469 binder_node_unlock(node);
5470 last_node = node;
5471 spin_lock(&binder_dead_nodes_lock);
5472 }
5473 spin_unlock(&binder_dead_nodes_lock);
5474 if (last_node)
5475 binder_put_node(last_node);
5476
5477 mutex_lock(&binder_procs_lock);
5478 hlist_for_each_entry(proc, &binder_procs, proc_node)
5479 print_binder_proc(m, proc, 1);
5480 mutex_unlock(&binder_procs_lock);
5481
5482 return 0;
5483}
5484
5485static int binder_stats_show(struct seq_file *m, void *unused)
5486{
5487 struct binder_proc *proc;
5488
5489 seq_puts(m, "binder stats:\n");
5490
5491 print_binder_stats(m, "", &binder_stats);
5492
5493 mutex_lock(&binder_procs_lock);
5494 hlist_for_each_entry(proc, &binder_procs, proc_node)
5495 print_binder_proc_stats(m, proc);
5496 mutex_unlock(&binder_procs_lock);
5497
5498 return 0;
5499}
5500
5501static int binder_transactions_show(struct seq_file *m, void *unused)
5502{
5503 struct binder_proc *proc;
5504
5505 seq_puts(m, "binder transactions:\n");
5506 mutex_lock(&binder_procs_lock);
5507 hlist_for_each_entry(proc, &binder_procs, proc_node)
5508 print_binder_proc(m, proc, 0);
5509 mutex_unlock(&binder_procs_lock);
5510
5511 return 0;
5512}
5513
5514static int binder_proc_show(struct seq_file *m, void *unused)
5515{
5516 struct binder_proc *itr;
5517 int pid = (unsigned long)m->private;
5518
5519 mutex_lock(&binder_procs_lock);
5520 hlist_for_each_entry(itr, &binder_procs, proc_node) {
5521 if (itr->pid == pid) {
5522 seq_puts(m, "binder proc state:\n");
5523 print_binder_proc(m, itr, 1);
5524 }
5525 }
5526 mutex_unlock(&binder_procs_lock);
5527
5528 return 0;
5529}
5530
5531static void print_binder_transaction_log_entry(struct seq_file *m,
5532 struct binder_transaction_log_entry *e)
5533{
5534 int debug_id = READ_ONCE(e->debug_id_done);
5535 /*
5536 * read barrier to guarantee debug_id_done read before
5537 * we print the log values
5538 */
5539 smp_rmb();
5540 seq_printf(m,
5541 "%d: %s from %d:%d to %d:%d context %s node %d handle %d size %d:%d ret %d/%d l=%d",
5542 e->debug_id, (e->call_type == 2) ? "reply" :
5543 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
5544 e->from_thread, e->to_proc, e->to_thread, e->context_name,
5545 e->to_node, e->target_handle, e->data_size, e->offsets_size,
5546 e->return_error, e->return_error_param,
5547 e->return_error_line);
5548 /*
5549 * read-barrier to guarantee read of debug_id_done after
5550 * done printing the fields of the entry
5551 */
5552 smp_rmb();
5553 seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ?
5554 "\n" : " (incomplete)\n");
5555}
5556
5557static int binder_transaction_log_show(struct seq_file *m, void *unused)
5558{
5559 struct binder_transaction_log *log = m->private;
5560 unsigned int log_cur = atomic_read(&log->cur);
5561 unsigned int count;
5562 unsigned int cur;
5563 int i;
5564
5565 count = log_cur + 1;
5566 cur = count < ARRAY_SIZE(log->entry) && !log->full ?
5567 0 : count % ARRAY_SIZE(log->entry);
5568 if (count > ARRAY_SIZE(log->entry) || log->full)
5569 count = ARRAY_SIZE(log->entry);
5570 for (i = 0; i < count; i++) {
5571 unsigned int index = cur++ % ARRAY_SIZE(log->entry);
5572
5573 print_binder_transaction_log_entry(m, &log->entry[index]);
5574 }
5575 return 0;
5576}
5577
5578static const struct file_operations binder_fops = {
5579 .owner = THIS_MODULE,
5580 .poll = binder_poll,
5581 .unlocked_ioctl = binder_ioctl,
5582 .compat_ioctl = binder_ioctl,
5583 .mmap = binder_mmap,
5584 .open = binder_open,
5585 .flush = binder_flush,
5586 .release = binder_release,
5587};
5588
5589BINDER_DEBUG_ENTRY(state);
5590BINDER_DEBUG_ENTRY(stats);
5591BINDER_DEBUG_ENTRY(transactions);
5592BINDER_DEBUG_ENTRY(transaction_log);
5593
5594static int __init init_binder_device(const char *name)
5595{
5596 int ret;
5597 struct binder_device *binder_device;
5598
5599 binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
5600 if (!binder_device)
5601 return -ENOMEM;
5602
5603 binder_device->miscdev.fops = &binder_fops;
5604 binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
5605 binder_device->miscdev.name = name;
5606
5607 binder_device->context.binder_context_mgr_uid = INVALID_UID;
5608 binder_device->context.name = name;
5609 mutex_init(&binder_device->context.context_mgr_node_lock);
5610
5611 ret = misc_register(&binder_device->miscdev);
5612 if (ret < 0) {
5613 kfree(binder_device);
5614 return ret;
5615 }
5616
5617 hlist_add_head(&binder_device->hlist, &binder_devices);
5618
5619 return ret;
5620}
5621
5622static int __init binder_init(void)
5623{
5624 int ret;
5625 char *device_name, *device_names, *device_tmp;
5626 struct binder_device *device;
5627 struct hlist_node *tmp;
5628
5629 ret = binder_alloc_shrinker_init();
5630 if (ret)
5631 return ret;
5632
5633 atomic_set(&binder_transaction_log.cur, ~0U);
5634 atomic_set(&binder_transaction_log_failed.cur, ~0U);
5635
5636 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
5637 if (binder_debugfs_dir_entry_root)
5638 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
5639 binder_debugfs_dir_entry_root);
5640
5641 if (binder_debugfs_dir_entry_root) {
5642 debugfs_create_file("state",
5643 0444,
5644 binder_debugfs_dir_entry_root,
5645 NULL,
5646 &binder_state_fops);
5647 debugfs_create_file("stats",
5648 0444,
5649 binder_debugfs_dir_entry_root,
5650 NULL,
5651 &binder_stats_fops);
5652 debugfs_create_file("transactions",
5653 0444,
5654 binder_debugfs_dir_entry_root,
5655 NULL,
5656 &binder_transactions_fops);
5657 debugfs_create_file("transaction_log",
5658 0444,
5659 binder_debugfs_dir_entry_root,
5660 &binder_transaction_log,
5661 &binder_transaction_log_fops);
5662 debugfs_create_file("failed_transaction_log",
5663 0444,
5664 binder_debugfs_dir_entry_root,
5665 &binder_transaction_log_failed,
5666 &binder_transaction_log_fops);
5667 }
5668
5669 /*
5670 * Copy the module_parameter string, because we don't want to
5671 * tokenize it in-place.
5672 */
5673 device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL);
5674 if (!device_names) {
5675 ret = -ENOMEM;
5676 goto err_alloc_device_names_failed;
5677 }
5678 strcpy(device_names, binder_devices_param);
5679
5680 device_tmp = device_names;
5681 while ((device_name = strsep(&device_tmp, ","))) {
5682 ret = init_binder_device(device_name);
5683 if (ret)
5684 goto err_init_binder_device_failed;
5685 }
5686
5687 return ret;
5688
5689err_init_binder_device_failed:
5690 hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
5691 misc_deregister(&device->miscdev);
5692 hlist_del(&device->hlist);
5693 kfree(device);
5694 }
5695
5696 kfree(device_names);
5697
5698err_alloc_device_names_failed:
5699 debugfs_remove_recursive(binder_debugfs_dir_entry_root);
5700
5701 return ret;
5702}
5703
5704device_initcall(binder_init);
5705
5706#define CREATE_TRACE_POINTS
5707#include "binder_trace.h"
5708
5709MODULE_LICENSE("GPL v2");