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