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