FROMLIST: ANDROID: binder: Add BINDER_GET_NODE_INFO_FOR_REF ioctl.
[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);
a987b61f
MC
2992 if (target_node && target_proc == proc) {
2993 binder_user_error("%d:%d got transaction to context manager from process owning it\n",
2994 proc->pid, thread->pid);
2995 return_error = BR_FAILED_REPLY;
2996 return_error_param = -EINVAL;
2997 return_error_line = __LINE__;
2998 goto err_invalid_target_handle;
2999 }
355b0502 3000 }
361f527d
TK
3001 if (!target_node) {
3002 /*
3003 * return_error is set above
3004 */
3005 return_error_param = -EINVAL;
dfc99565 3006 return_error_line = __LINE__;
355b0502
GKH
3007 goto err_dead_binder;
3008 }
361f527d 3009 e->to_node = target_node->debug_id;
82f6ad88
TK
3010 if (security_binder_transaction(proc->tsk,
3011 target_proc->tsk) < 0) {
3b090f5e 3012 return_error = BR_FAILED_REPLY;
dfc99565
TK
3013 return_error_param = -EPERM;
3014 return_error_line = __LINE__;
3b090f5e
S
3015 goto err_invalid_target_handle;
3016 }
a45c586f 3017 binder_inner_proc_lock(proc);
355b0502
GKH
3018 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
3019 struct binder_transaction *tmp;
01c8cbde 3020
355b0502
GKH
3021 tmp = thread->transaction_stack;
3022 if (tmp->to_thread != thread) {
82f6ad88 3023 spin_lock(&tmp->lock);
56b468fc 3024 binder_user_error("%d:%d got new transaction with bad transaction stack, transaction %d has target %d:%d\n",
355b0502
GKH
3025 proc->pid, thread->pid, tmp->debug_id,
3026 tmp->to_proc ? tmp->to_proc->pid : 0,
3027 tmp->to_thread ?
3028 tmp->to_thread->pid : 0);
82f6ad88 3029 spin_unlock(&tmp->lock);
a45c586f 3030 binder_inner_proc_unlock(proc);
355b0502 3031 return_error = BR_FAILED_REPLY;
dfc99565
TK
3032 return_error_param = -EPROTO;
3033 return_error_line = __LINE__;
355b0502
GKH
3034 goto err_bad_call_stack;
3035 }
3036 while (tmp) {
82f6ad88
TK
3037 struct binder_thread *from;
3038
3039 spin_lock(&tmp->lock);
3040 from = tmp->from;
3041 if (from && from->proc == target_proc) {
3042 atomic_inc(&from->tmp_ref);
3043 target_thread = from;
3044 spin_unlock(&tmp->lock);
3045 break;
3046 }
3047 spin_unlock(&tmp->lock);
355b0502
GKH
3048 tmp = tmp->from_parent;
3049 }
3050 }
a45c586f 3051 binder_inner_proc_unlock(proc);
355b0502 3052 }
1200efe9 3053 if (target_thread)
355b0502 3054 e->to_thread = target_thread->pid;
355b0502
GKH
3055 e->to_proc = target_proc->pid;
3056
3057 /* TODO: reuse incoming transaction for reply */
d31f0c40 3058 t = kzalloc(sizeof(*t), GFP_KERNEL);
355b0502
GKH
3059 if (t == NULL) {
3060 return_error = BR_FAILED_REPLY;
dfc99565
TK
3061 return_error_param = -ENOMEM;
3062 return_error_line = __LINE__;
355b0502
GKH
3063 goto err_alloc_t_failed;
3064 }
3065 binder_stats_created(BINDER_STAT_TRANSACTION);
82f6ad88 3066 spin_lock_init(&t->lock);
355b0502 3067
d31f0c40 3068 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
355b0502
GKH
3069 if (tcomplete == NULL) {
3070 return_error = BR_FAILED_REPLY;
dfc99565
TK
3071 return_error_param = -ENOMEM;
3072 return_error_line = __LINE__;
355b0502
GKH
3073 goto err_alloc_tcomplete_failed;
3074 }
3075 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
3076
7d94b2c7 3077 t->debug_id = t_debug_id;
355b0502
GKH
3078
3079 if (reply)
3080 binder_debug(BINDER_DEBUG_TRANSACTION,
d18dbf2f 3081 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
355b0502
GKH
3082 proc->pid, thread->pid, t->debug_id,
3083 target_proc->pid, target_thread->pid,
9c0a1d02
AH
3084 (u64)tr->data.ptr.buffer,
3085 (u64)tr->data.ptr.offsets,
d18dbf2f
MC
3086 (u64)tr->data_size, (u64)tr->offsets_size,
3087 (u64)extra_buffers_size);
355b0502
GKH
3088 else
3089 binder_debug(BINDER_DEBUG_TRANSACTION,
d18dbf2f 3090 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
355b0502
GKH
3091 proc->pid, thread->pid, t->debug_id,
3092 target_proc->pid, target_node->debug_id,
9c0a1d02
AH
3093 (u64)tr->data.ptr.buffer,
3094 (u64)tr->data.ptr.offsets,
d18dbf2f
MC
3095 (u64)tr->data_size, (u64)tr->offsets_size,
3096 (u64)extra_buffers_size);
355b0502
GKH
3097
3098 if (!reply && !(tr->flags & TF_ONE_WAY))
3099 t->from = thread;
3100 else
3101 t->from = NULL;
1da696b1 3102 t->sender_euid = task_euid(proc->tsk);
355b0502
GKH
3103 t->to_proc = target_proc;
3104 t->to_thread = target_thread;
3105 t->code = tr->code;
3106 t->flags = tr->flags;
425bb8fc
MC
3107 if (!(t->flags & TF_ONE_WAY) &&
3108 binder_supported_policy(current->policy)) {
3109 /* Inherit supported policies for synchronous transactions */
3110 t->priority.sched_policy = current->policy;
3111 t->priority.prio = current->normal_prio;
3112 } else {
3113 /* Otherwise, fall back to the default priority */
3114 t->priority = target_proc->default_priority;
3115 }
975a1ac9
AH
3116
3117 trace_binder_transaction(reply, t, target_node);
3118
69c33bb1 3119 t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size,
d18dbf2f
MC
3120 tr->offsets_size, extra_buffers_size,
3121 !reply && (t->flags & TF_ONE_WAY));
dfc99565
TK
3122 if (IS_ERR(t->buffer)) {
3123 /*
3124 * -ESRCH indicates VMA cleared. The target is dying.
3125 */
3126 return_error_param = PTR_ERR(t->buffer);
3127 return_error = return_error_param == -ESRCH ?
3128 BR_DEAD_REPLY : BR_FAILED_REPLY;
3129 return_error_line = __LINE__;
3130 t->buffer = NULL;
355b0502
GKH
3131 goto err_binder_alloc_buf_failed;
3132 }
3133 t->buffer->allow_user_free = 0;
3134 t->buffer->debug_id = t->debug_id;
3135 t->buffer->transaction = t;
3136 t->buffer->target_node = target_node;
975a1ac9 3137 trace_binder_transaction_alloc_buf(t->buffer);
e884979e
MC
3138 off_start = (binder_size_t *)(t->buffer->data +
3139 ALIGN(tr->data_size, sizeof(void *)));
3140 offp = off_start;
355b0502 3141
d31f0c40 3142 if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t)
9c0a1d02 3143 tr->data.ptr.buffer, tr->data_size)) {
56b468fc
AS
3144 binder_user_error("%d:%d got transaction with invalid data ptr\n",
3145 proc->pid, thread->pid);
355b0502 3146 return_error = BR_FAILED_REPLY;
dfc99565
TK
3147 return_error_param = -EFAULT;
3148 return_error_line = __LINE__;
355b0502
GKH
3149 goto err_copy_data_failed;
3150 }
d31f0c40 3151 if (copy_from_user(offp, (const void __user *)(uintptr_t)
9c0a1d02 3152 tr->data.ptr.offsets, tr->offsets_size)) {
56b468fc
AS
3153 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3154 proc->pid, thread->pid);
355b0502 3155 return_error = BR_FAILED_REPLY;
dfc99565
TK
3156 return_error_param = -EFAULT;
3157 return_error_line = __LINE__;
355b0502
GKH
3158 goto err_copy_data_failed;
3159 }
9c0a1d02
AH
3160 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
3161 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
3162 proc->pid, thread->pid, (u64)tr->offsets_size);
355b0502 3163 return_error = BR_FAILED_REPLY;
dfc99565
TK
3164 return_error_param = -EINVAL;
3165 return_error_line = __LINE__;
355b0502
GKH
3166 goto err_bad_offset;
3167 }
e884979e
MC
3168 if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) {
3169 binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
3170 proc->pid, thread->pid,
3171 extra_buffers_size);
3172 return_error = BR_FAILED_REPLY;
dfc99565
TK
3173 return_error_param = -EINVAL;
3174 return_error_line = __LINE__;
e884979e
MC
3175 goto err_bad_offset;
3176 }
3177 off_end = (void *)off_start + tr->offsets_size;
3178 sg_bufp = (u8 *)(PTR_ALIGN(off_end, sizeof(void *)));
3179 sg_buf_end = sg_bufp + extra_buffers_size;
e71dbe5a 3180 off_min = 0;
355b0502 3181 for (; offp < off_end; offp++) {
17f2cab3
MC
3182 struct binder_object_header *hdr;
3183 size_t object_size = binder_validate_object(t->buffer, *offp);
01c8cbde 3184
17f2cab3
MC
3185 if (object_size == 0 || *offp < off_min) {
3186 binder_user_error("%d:%d got transaction with invalid offset (%lld, min %lld max %lld) or object.\n",
e71dbe5a
AH
3187 proc->pid, thread->pid, (u64)*offp,
3188 (u64)off_min,
17f2cab3 3189 (u64)t->buffer->data_size);
355b0502 3190 return_error = BR_FAILED_REPLY;
dfc99565
TK
3191 return_error_param = -EINVAL;
3192 return_error_line = __LINE__;
355b0502
GKH
3193 goto err_bad_offset;
3194 }
17f2cab3
MC
3195
3196 hdr = (struct binder_object_header *)(t->buffer->data + *offp);
3197 off_min = *offp + object_size;
3198 switch (hdr->type) {
355b0502
GKH
3199 case BINDER_TYPE_BINDER:
3200 case BINDER_TYPE_WEAK_BINDER: {
17f2cab3 3201 struct flat_binder_object *fp;
01c8cbde 3202
17f2cab3 3203 fp = to_flat_binder_object(hdr);
b4bdab80
MC
3204 ret = binder_translate_binder(fp, t, thread);
3205 if (ret < 0) {
355b0502 3206 return_error = BR_FAILED_REPLY;
dfc99565
TK
3207 return_error_param = ret;
3208 return_error_line = __LINE__;
b4bdab80 3209 goto err_translate_failed;
355b0502 3210 }
355b0502
GKH
3211 } break;
3212 case BINDER_TYPE_HANDLE:
3213 case BINDER_TYPE_WEAK_HANDLE: {
17f2cab3 3214 struct flat_binder_object *fp;
01c8cbde 3215
17f2cab3 3216 fp = to_flat_binder_object(hdr);
b4bdab80
MC
3217 ret = binder_translate_handle(fp, t, thread);
3218 if (ret < 0) {
3b090f5e 3219 return_error = BR_FAILED_REPLY;
dfc99565
TK
3220 return_error_param = ret;
3221 return_error_line = __LINE__;
b4bdab80 3222 goto err_translate_failed;
355b0502
GKH
3223 }
3224 } break;
3225
3226 case BINDER_TYPE_FD: {
17f2cab3 3227 struct binder_fd_object *fp = to_binder_fd_object(hdr);
b4bdab80
MC
3228 int target_fd = binder_translate_fd(fp->fd, t, thread,
3229 in_reply_to);
355b0502 3230
355b0502 3231 if (target_fd < 0) {
355b0502 3232 return_error = BR_FAILED_REPLY;
dfc99565
TK
3233 return_error_param = target_fd;
3234 return_error_line = __LINE__;
b4bdab80 3235 goto err_translate_failed;
355b0502 3236 }
17f2cab3
MC
3237 fp->pad_binder = 0;
3238 fp->fd = target_fd;
355b0502 3239 } break;
2f82efbc
MC
3240 case BINDER_TYPE_FDA: {
3241 struct binder_fd_array_object *fda =
3242 to_binder_fd_array_object(hdr);
3243 struct binder_buffer_object *parent =
3244 binder_validate_ptr(t->buffer, fda->parent,
3245 off_start,
3246 offp - off_start);
3247 if (!parent) {
3248 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
3249 proc->pid, thread->pid);
3250 return_error = BR_FAILED_REPLY;
dfc99565
TK
3251 return_error_param = -EINVAL;
3252 return_error_line = __LINE__;
2f82efbc
MC
3253 goto err_bad_parent;
3254 }
3255 if (!binder_validate_fixup(t->buffer, off_start,
3256 parent, fda->parent_offset,
3257 last_fixup_obj,
3258 last_fixup_min_off)) {
3259 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
3260 proc->pid, thread->pid);
3261 return_error = BR_FAILED_REPLY;
dfc99565
TK
3262 return_error_param = -EINVAL;
3263 return_error_line = __LINE__;
2f82efbc
MC
3264 goto err_bad_parent;
3265 }
3266 ret = binder_translate_fd_array(fda, parent, t, thread,
3267 in_reply_to);
3268 if (ret < 0) {
3269 return_error = BR_FAILED_REPLY;
dfc99565
TK
3270 return_error_param = ret;
3271 return_error_line = __LINE__;
2f82efbc
MC
3272 goto err_translate_failed;
3273 }
3274 last_fixup_obj = parent;
3275 last_fixup_min_off =
3276 fda->parent_offset + sizeof(u32) * fda->num_fds;
3277 } break;
e884979e
MC
3278 case BINDER_TYPE_PTR: {
3279 struct binder_buffer_object *bp =
3280 to_binder_buffer_object(hdr);
3281 size_t buf_left = sg_buf_end - sg_bufp;
3282
3283 if (bp->length > buf_left) {
3284 binder_user_error("%d:%d got transaction with too large buffer\n",
3285 proc->pid, thread->pid);
3286 return_error = BR_FAILED_REPLY;
dfc99565
TK
3287 return_error_param = -EINVAL;
3288 return_error_line = __LINE__;
e884979e
MC
3289 goto err_bad_offset;
3290 }
c9f9a1ad
DW
3291 if (copy_from_user(sg_bufp,
3292 (const void __user *)(uintptr_t)
3293 bp->buffer, bp->length)) {
e884979e
MC
3294 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3295 proc->pid, thread->pid);
dfc99565 3296 return_error_param = -EFAULT;
e884979e 3297 return_error = BR_FAILED_REPLY;
dfc99565 3298 return_error_line = __LINE__;
e884979e
MC
3299 goto err_copy_data_failed;
3300 }
3301 /* Fixup buffer pointer to target proc address space */
3302 bp->buffer = (uintptr_t)sg_bufp +
69c33bb1
TK
3303 binder_alloc_get_user_buffer_offset(
3304 &target_proc->alloc);
e884979e
MC
3305 sg_bufp += ALIGN(bp->length, sizeof(u64));
3306
3307 ret = binder_fixup_parent(t, thread, bp, off_start,
3308 offp - off_start,
3309 last_fixup_obj,
3310 last_fixup_min_off);
3311 if (ret < 0) {
3312 return_error = BR_FAILED_REPLY;
dfc99565
TK
3313 return_error_param = ret;
3314 return_error_line = __LINE__;
e884979e
MC
3315 goto err_translate_failed;
3316 }
3317 last_fixup_obj = bp;
3318 last_fixup_min_off = 0;
3319 } break;
355b0502 3320 default:
3c2a0909 3321 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
17f2cab3 3322 proc->pid, thread->pid, hdr->type);
355b0502 3323 return_error = BR_FAILED_REPLY;
dfc99565
TK
3324 return_error_param = -EINVAL;
3325 return_error_line = __LINE__;
355b0502
GKH
3326 goto err_bad_object_type;
3327 }
3328 }
531e255c 3329 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
25de59a5 3330 t->work.type = BINDER_WORK_TRANSACTION;
531e255c 3331
355b0502 3332 if (reply) {
3fb0a5b6 3333 binder_enqueue_thread_work(thread, tcomplete);
a45c586f
MC
3334 binder_inner_proc_lock(target_proc);
3335 if (target_thread->is_dead) {
3336 binder_inner_proc_unlock(target_proc);
82f6ad88 3337 goto err_dead_proc_or_thread;
a45c586f 3338 }
355b0502 3339 BUG_ON(t->buffer->async_transaction != 0);
a45c586f 3340 binder_pop_transaction_ilocked(target_thread, in_reply_to);
3fb0a5b6 3341 binder_enqueue_thread_work_ilocked(target_thread, &t->work);
a45c586f 3342 binder_inner_proc_unlock(target_proc);
1200efe9 3343 wake_up_interruptible_sync(&target_thread->wait);
dadba0f5 3344 binder_restore_priority(current, in_reply_to->saved_priority);
7323366b 3345 binder_free_transaction(in_reply_to);
355b0502
GKH
3346 } else if (!(t->flags & TF_ONE_WAY)) {
3347 BUG_ON(t->buffer->async_transaction != 0);
a45c586f 3348 binder_inner_proc_lock(proc);
b8b45e4d
MC
3349 /*
3350 * Defer the TRANSACTION_COMPLETE, so we don't return to
3351 * userspace immediately; this allows the target process to
3352 * immediately start processing this transaction, reducing
3353 * latency. We will then return the TRANSACTION_COMPLETE when
3354 * the target replies (or there is an error).
3355 */
3356 binder_enqueue_deferred_thread_work_ilocked(thread, tcomplete);
355b0502
GKH
3357 t->need_reply = 1;
3358 t->from_parent = thread->transaction_stack;
3359 thread->transaction_stack = t;
a45c586f 3360 binder_inner_proc_unlock(proc);
1200efe9 3361 if (!binder_proc_transaction(t, target_proc, target_thread)) {
a45c586f
MC
3362 binder_inner_proc_lock(proc);
3363 binder_pop_transaction_ilocked(thread, t);
3364 binder_inner_proc_unlock(proc);
82f6ad88
TK
3365 goto err_dead_proc_or_thread;
3366 }
355b0502
GKH
3367 } else {
3368 BUG_ON(target_node == NULL);
3369 BUG_ON(t->buffer->async_transaction != 1);
3fb0a5b6 3370 binder_enqueue_thread_work(thread, tcomplete);
1200efe9 3371 if (!binder_proc_transaction(t, target_proc, NULL))
82f6ad88 3372 goto err_dead_proc_or_thread;
d999e705 3373 }
82f6ad88
TK
3374 if (target_thread)
3375 binder_thread_dec_tmpref(target_thread);
3376 binder_proc_dec_tmpref(target_proc);
361f527d
TK
3377 if (target_node)
3378 binder_dec_node_tmpref(target_node);
7d94b2c7
TK
3379 /*
3380 * write barrier to synchronize with initialization
3381 * of log entry
3382 */
3383 smp_wmb();
3384 WRITE_ONCE(e->debug_id_done, t_debug_id);
355b0502
GKH
3385 return;
3386
82f6ad88
TK
3387err_dead_proc_or_thread:
3388 return_error = BR_DEAD_REPLY;
3389 return_error_line = __LINE__;
41fad392 3390 binder_dequeue_work(proc, tcomplete);
b4bdab80 3391err_translate_failed:
355b0502
GKH
3392err_bad_object_type:
3393err_bad_offset:
2f82efbc 3394err_bad_parent:
355b0502 3395err_copy_data_failed:
975a1ac9 3396 trace_binder_transaction_failed_buffer_release(t->buffer);
355b0502 3397 binder_transaction_buffer_release(target_proc, t->buffer, offp);
361f527d
TK
3398 if (target_node)
3399 binder_dec_node_tmpref(target_node);
0b62b556 3400 target_node = NULL;
355b0502 3401 t->buffer->transaction = NULL;
69c33bb1 3402 binder_alloc_free_buf(&target_proc->alloc, t->buffer);
355b0502
GKH
3403err_binder_alloc_buf_failed:
3404 kfree(tcomplete);
3405 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3406err_alloc_tcomplete_failed:
3407 kfree(t);
3408 binder_stats_deleted(BINDER_STAT_TRANSACTION);
3409err_alloc_t_failed:
3410err_bad_call_stack:
3411err_empty_call_stack:
3412err_dead_binder:
3413err_invalid_target_handle:
82f6ad88
TK
3414 if (target_thread)
3415 binder_thread_dec_tmpref(target_thread);
3416 if (target_proc)
3417 binder_proc_dec_tmpref(target_proc);
361f527d 3418 if (target_node) {
0b62b556 3419 binder_dec_node(target_node, 1, 0);
361f527d
TK
3420 binder_dec_node_tmpref(target_node);
3421 }
0b62b556 3422
355b0502 3423 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
dfc99565
TK
3424 "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
3425 proc->pid, thread->pid, return_error, return_error_param,
3426 (u64)tr->data_size, (u64)tr->offsets_size,
3427 return_error_line);
355b0502
GKH
3428
3429 {
3430 struct binder_transaction_log_entry *fe;
01c8cbde 3431
dfc99565
TK
3432 e->return_error = return_error;
3433 e->return_error_param = return_error_param;
3434 e->return_error_line = return_error_line;
7eac155d 3435 fe = binder_transaction_log_add(&binder_transaction_log_failed);
355b0502 3436 *fe = *e;
7d94b2c7
TK
3437 /*
3438 * write barrier to synchronize with initialization
3439 * of log entry
3440 */
3441 smp_wmb();
3442 WRITE_ONCE(e->debug_id_done, t_debug_id);
3443 WRITE_ONCE(fe->debug_id_done, t_debug_id);
355b0502
GKH
3444 }
3445
795aa6bc 3446 BUG_ON(thread->return_error.cmd != BR_OK);
355b0502 3447 if (in_reply_to) {
dadba0f5 3448 binder_restore_priority(current, in_reply_to->saved_priority);
795aa6bc 3449 thread->return_error.cmd = BR_TRANSACTION_COMPLETE;
3fb0a5b6 3450 binder_enqueue_thread_work(thread, &thread->return_error.work);
355b0502 3451 binder_send_failed_reply(in_reply_to, return_error);
795aa6bc
TK
3452 } else {
3453 thread->return_error.cmd = return_error;
3fb0a5b6 3454 binder_enqueue_thread_work(thread, &thread->return_error.work);
795aa6bc 3455 }
355b0502
GKH
3456}
3457
9c0a1d02
AH
3458int binder_thread_write(struct binder_proc *proc,
3459 struct binder_thread *thread,
3460 binder_uintptr_t binder_buffer, size_t size,
3461 binder_size_t *consumed)
355b0502
GKH
3462{
3463 uint32_t cmd;
913926d0 3464 struct binder_context *context = proc->context;
9c0a1d02 3465 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
355b0502
GKH
3466 void __user *ptr = buffer + *consumed;
3467 void __user *end = buffer + size;
3468
795aa6bc 3469 while (ptr < end && thread->return_error.cmd == BR_OK) {
bc65c39a
TK
3470 int ret;
3471
d31f0c40 3472 if (get_user(cmd, (uint32_t __user *)ptr))
355b0502
GKH
3473 return -EFAULT;
3474 ptr += sizeof(uint32_t);
975a1ac9 3475 trace_binder_command(cmd);
7eac155d 3476 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
340b5f86
BJS
3477 atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]);
3478 atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]);
3479 atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]);
355b0502
GKH
3480 }
3481 switch (cmd) {
3482 case BC_INCREFS:
3483 case BC_ACQUIRE:
3484 case BC_RELEASE:
3485 case BC_DECREFS: {
3486 uint32_t target;
355b0502 3487 const char *debug_string;
bc65c39a
TK
3488 bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE;
3489 bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE;
3490 struct binder_ref_data rdata;
355b0502 3491
d31f0c40 3492 if (get_user(target, (uint32_t __user *)ptr))
355b0502 3493 return -EFAULT;
76341f6f 3494
355b0502 3495 ptr += sizeof(uint32_t);
bc65c39a
TK
3496 ret = -1;
3497 if (increment && !target) {
76341f6f 3498 struct binder_node *ctx_mgr_node;
76341f6f
TK
3499 mutex_lock(&context->context_mgr_node_lock);
3500 ctx_mgr_node = context->binder_context_mgr_node;
bc65c39a
TK
3501 if (ctx_mgr_node)
3502 ret = binder_inc_ref_for_node(
3503 proc, ctx_mgr_node,
3504 strong, NULL, &rdata);
76341f6f
TK
3505 mutex_unlock(&context->context_mgr_node_lock);
3506 }
bc65c39a
TK
3507 if (ret)
3508 ret = binder_update_ref_for_handle(
3509 proc, target, increment, strong,
3510 &rdata);
3511 if (!ret && rdata.desc != target) {
3512 binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
3513 proc->pid, thread->pid,
3514 target, rdata.desc);
355b0502
GKH
3515 }
3516 switch (cmd) {
3517 case BC_INCREFS:
3518 debug_string = "IncRefs";
355b0502
GKH
3519 break;
3520 case BC_ACQUIRE:
3521 debug_string = "Acquire";
355b0502
GKH
3522 break;
3523 case BC_RELEASE:
3524 debug_string = "Release";
355b0502
GKH
3525 break;
3526 case BC_DECREFS:
3527 default:
3528 debug_string = "DecRefs";
bc65c39a
TK
3529 break;
3530 }
3531 if (ret) {
3532 binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
3533 proc->pid, thread->pid, debug_string,
3534 strong, target, ret);
355b0502
GKH
3535 break;
3536 }
354ac4b4 3537 binder_debug(BINDER_DEBUG_USER_REFS,
bc65c39a
TK
3538 "%d:%d %s ref %d desc %d s %d w %d\n",
3539 proc->pid, thread->pid, debug_string,
3540 rdata.debug_id, rdata.desc, rdata.strong,
3541 rdata.weak);
8f35edf4 3542 break;
355b0502
GKH
3543 }
3544 case BC_INCREFS_DONE:
3545 case BC_ACQUIRE_DONE: {
9c0a1d02
AH
3546 binder_uintptr_t node_ptr;
3547 binder_uintptr_t cookie;
355b0502 3548 struct binder_node *node;
25de59a5 3549 bool free_node;
355b0502 3550
d31f0c40 3551 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
355b0502 3552 return -EFAULT;
9c0a1d02 3553 ptr += sizeof(binder_uintptr_t);
d31f0c40 3554 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
355b0502 3555 return -EFAULT;
9c0a1d02 3556 ptr += sizeof(binder_uintptr_t);
355b0502
GKH
3557 node = binder_get_node(proc, node_ptr);
3558 if (node == NULL) {
9c0a1d02 3559 binder_user_error("%d:%d %s u%016llx no match\n",
355b0502
GKH
3560 proc->pid, thread->pid,
3561 cmd == BC_INCREFS_DONE ?
3562 "BC_INCREFS_DONE" :
3563 "BC_ACQUIRE_DONE",
9c0a1d02 3564 (u64)node_ptr);
355b0502
GKH
3565 break;
3566 }
3567 if (cookie != node->cookie) {
9c0a1d02 3568 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
355b0502
GKH
3569 proc->pid, thread->pid,
3570 cmd == BC_INCREFS_DONE ?
3571 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
9c0a1d02
AH
3572 (u64)node_ptr, node->debug_id,
3573 (u64)cookie, (u64)node->cookie);
9607bf70 3574 binder_put_node(node);
355b0502
GKH
3575 break;
3576 }
25de59a5 3577 binder_node_inner_lock(node);
355b0502
GKH
3578 if (cmd == BC_ACQUIRE_DONE) {
3579 if (node->pending_strong_ref == 0) {
56b468fc 3580 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
355b0502
GKH
3581 proc->pid, thread->pid,
3582 node->debug_id);
25de59a5 3583 binder_node_inner_unlock(node);
9607bf70 3584 binder_put_node(node);
355b0502
GKH
3585 break;
3586 }
3587 node->pending_strong_ref = 0;
3588 } else {
3589 if (node->pending_weak_ref == 0) {
56b468fc 3590 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
355b0502
GKH
3591 proc->pid, thread->pid,
3592 node->debug_id);
25de59a5 3593 binder_node_inner_unlock(node);
9607bf70 3594 binder_put_node(node);
355b0502
GKH
3595 break;
3596 }
3597 node->pending_weak_ref = 0;
3598 }
25de59a5
TK
3599 free_node = binder_dec_node_nilocked(node,
3600 cmd == BC_ACQUIRE_DONE, 0);
3601 WARN_ON(free_node);
355b0502 3602 binder_debug(BINDER_DEBUG_USER_REFS,
9607bf70 3603 "%d:%d %s node %d ls %d lw %d tr %d\n",
355b0502
GKH
3604 proc->pid, thread->pid,
3605 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
9607bf70
TK
3606 node->debug_id, node->local_strong_refs,
3607 node->local_weak_refs, node->tmp_refs);
25de59a5 3608 binder_node_inner_unlock(node);
9607bf70 3609 binder_put_node(node);
355b0502
GKH
3610 break;
3611 }
3612 case BC_ATTEMPT_ACQUIRE:
56b468fc 3613 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
355b0502
GKH
3614 return -EINVAL;
3615 case BC_ACQUIRE_RESULT:
56b468fc 3616 pr_err("BC_ACQUIRE_RESULT not supported\n");
355b0502
GKH
3617 return -EINVAL;
3618
3619 case BC_FREE_BUFFER: {
9c0a1d02 3620 binder_uintptr_t data_ptr;
355b0502
GKH
3621 struct binder_buffer *buffer;
3622
d31f0c40 3623 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
355b0502 3624 return -EFAULT;
9c0a1d02 3625 ptr += sizeof(binder_uintptr_t);
355b0502 3626
382658e8
TK
3627 buffer = binder_alloc_prepare_to_free(&proc->alloc,
3628 data_ptr);
355b0502 3629 if (buffer == NULL) {
9c0a1d02
AH
3630 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
3631 proc->pid, thread->pid, (u64)data_ptr);
355b0502
GKH
3632 break;
3633 }
3634 if (!buffer->allow_user_free) {
9c0a1d02
AH
3635 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
3636 proc->pid, thread->pid, (u64)data_ptr);
355b0502
GKH
3637 break;
3638 }
3639 binder_debug(BINDER_DEBUG_FREE_BUFFER,
9c0a1d02
AH
3640 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
3641 proc->pid, thread->pid, (u64)data_ptr,
3642 buffer->debug_id,
355b0502
GKH
3643 buffer->transaction ? "active" : "finished");
3644
3645 if (buffer->transaction) {
3646 buffer->transaction->buffer = NULL;
3647 buffer->transaction = NULL;
3648 }
3649 if (buffer->async_transaction && buffer->target_node) {
aad8732c
TK
3650 struct binder_node *buf_node;
3651 struct binder_work *w;
3652
3653 buf_node = buffer->target_node;
25de59a5 3654 binder_node_inner_lock(buf_node);
aad8732c
TK
3655 BUG_ON(!buf_node->has_async_transaction);
3656 BUG_ON(buf_node->proc != proc);
aad8732c
TK
3657 w = binder_dequeue_work_head_ilocked(
3658 &buf_node->async_todo);
9b9577b0 3659 if (!w) {
aad8732c 3660 buf_node->has_async_transaction = 0;
9b9577b0 3661 } else {
aad8732c 3662 binder_enqueue_work_ilocked(
9b9577b0
MC
3663 w, &proc->todo);
3664 binder_wakeup_proc_ilocked(proc);
3665 }
25de59a5 3666 binder_node_inner_unlock(buf_node);
355b0502 3667 }
975a1ac9 3668 trace_binder_transaction_buffer_release(buffer);
355b0502 3669 binder_transaction_buffer_release(proc, buffer, NULL);
69c33bb1 3670 binder_alloc_free_buf(&proc->alloc, buffer);
355b0502
GKH
3671 break;
3672 }
3673
e884979e
MC
3674 case BC_TRANSACTION_SG:
3675 case BC_REPLY_SG: {
3676 struct binder_transaction_data_sg tr;
3677
c9f9a1ad 3678 if (copy_from_user(&tr, ptr, sizeof(tr)))
e884979e
MC
3679 return -EFAULT;
3680 ptr += sizeof(tr);
3681 binder_transaction(proc, thread, &tr.transaction_data,
3682 cmd == BC_REPLY_SG, tr.buffers_size);
3683 break;
3684 }
355b0502
GKH
3685 case BC_TRANSACTION:
3686 case BC_REPLY: {
3687 struct binder_transaction_data tr;
3688
d31f0c40 3689 if (copy_from_user(&tr, ptr, sizeof(tr)))
355b0502
GKH
3690 return -EFAULT;
3691 ptr += sizeof(tr);
d18dbf2f
MC
3692 binder_transaction(proc, thread, &tr,
3693 cmd == BC_REPLY, 0);
355b0502
GKH
3694 break;
3695 }
3696
3697 case BC_REGISTER_LOOPER:
3698 binder_debug(BINDER_DEBUG_THREADS,
56b468fc 3699 "%d:%d BC_REGISTER_LOOPER\n",
355b0502 3700 proc->pid, thread->pid);
e09d7b9a 3701 binder_inner_proc_lock(proc);
355b0502
GKH
3702 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
3703 thread->looper |= BINDER_LOOPER_STATE_INVALID;
56b468fc 3704 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
355b0502
GKH
3705 proc->pid, thread->pid);
3706 } else if (proc->requested_threads == 0) {
3707 thread->looper |= BINDER_LOOPER_STATE_INVALID;
56b468fc 3708 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
355b0502
GKH
3709 proc->pid, thread->pid);
3710 } else {
3711 proc->requested_threads--;
3712 proc->requested_threads_started++;
3713 }
3714 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
e09d7b9a 3715 binder_inner_proc_unlock(proc);
355b0502
GKH
3716 break;
3717 case BC_ENTER_LOOPER:
3718 binder_debug(BINDER_DEBUG_THREADS,
56b468fc 3719 "%d:%d BC_ENTER_LOOPER\n",
355b0502
GKH
3720 proc->pid, thread->pid);
3721 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
3722 thread->looper |= BINDER_LOOPER_STATE_INVALID;
56b468fc 3723 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
355b0502
GKH
3724 proc->pid, thread->pid);
3725 }
3726 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
3727 break;
3728 case BC_EXIT_LOOPER:
3729 binder_debug(BINDER_DEBUG_THREADS,
56b468fc 3730 "%d:%d BC_EXIT_LOOPER\n",
355b0502
GKH
3731 proc->pid, thread->pid);
3732 thread->looper |= BINDER_LOOPER_STATE_EXITED;
3733 break;
3734
3735 case BC_REQUEST_DEATH_NOTIFICATION:
3736 case BC_CLEAR_DEATH_NOTIFICATION: {
3737 uint32_t target;
9c0a1d02 3738 binder_uintptr_t cookie;
355b0502 3739 struct binder_ref *ref;
5717118f 3740 struct binder_ref_death *death = NULL;
355b0502 3741
d31f0c40 3742 if (get_user(target, (uint32_t __user *)ptr))
355b0502
GKH
3743 return -EFAULT;
3744 ptr += sizeof(uint32_t);
d31f0c40 3745 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
355b0502 3746 return -EFAULT;
9c0a1d02 3747 ptr += sizeof(binder_uintptr_t);
5717118f
TK
3748 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3749 /*
3750 * Allocate memory for death notification
3751 * before taking lock
3752 */
3753 death = kzalloc(sizeof(*death), GFP_KERNEL);
3754 if (death == NULL) {
3755 WARN_ON(thread->return_error.cmd !=
3756 BR_OK);
3757 thread->return_error.cmd = BR_ERROR;
3fb0a5b6
MC
3758 binder_enqueue_thread_work(
3759 thread,
3760 &thread->return_error.work);
5717118f
TK
3761 binder_debug(
3762 BINDER_DEBUG_FAILED_TRANSACTION,
3763 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
3764 proc->pid, thread->pid);
3765 break;
3766 }
3767 }
3768 binder_proc_lock(proc);
3769 ref = binder_get_ref_olocked(proc, target, false);
355b0502 3770 if (ref == NULL) {
56b468fc 3771 binder_user_error("%d:%d %s invalid ref %d\n",
355b0502
GKH
3772 proc->pid, thread->pid,
3773 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3774 "BC_REQUEST_DEATH_NOTIFICATION" :
3775 "BC_CLEAR_DEATH_NOTIFICATION",
3776 target);
5717118f
TK
3777 binder_proc_unlock(proc);
3778 kfree(death);
355b0502
GKH
3779 break;
3780 }
3781
3782 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
9c0a1d02 3783 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
355b0502
GKH
3784 proc->pid, thread->pid,
3785 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3786 "BC_REQUEST_DEATH_NOTIFICATION" :
3787 "BC_CLEAR_DEATH_NOTIFICATION",
bc65c39a
TK
3788 (u64)cookie, ref->data.debug_id,
3789 ref->data.desc, ref->data.strong,
3790 ref->data.weak, ref->node->debug_id);
355b0502 3791
914f62a0 3792 binder_node_lock(ref->node);
355b0502
GKH
3793 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3794 if (ref->death) {
56b468fc 3795 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
355b0502 3796 proc->pid, thread->pid);
914f62a0 3797 binder_node_unlock(ref->node);
5717118f
TK
3798 binder_proc_unlock(proc);
3799 kfree(death);
355b0502
GKH
3800 break;
3801 }
3802 binder_stats_created(BINDER_STAT_DEATH);
3803 INIT_LIST_HEAD(&death->work.entry);
3804 death->cookie = cookie;
3805 ref->death = death;
3806 if (ref->node->proc == NULL) {
3807 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
bd3a2956
MC
3808
3809 binder_inner_proc_lock(proc);
3810 binder_enqueue_work_ilocked(
3811 &ref->death->work, &proc->todo);
3812 binder_wakeup_proc_ilocked(proc);
3813 binder_inner_proc_unlock(proc);
355b0502
GKH
3814 }
3815 } else {
3816 if (ref->death == NULL) {
56b468fc 3817 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
355b0502 3818 proc->pid, thread->pid);
25de59a5 3819 binder_node_unlock(ref->node);
5717118f 3820 binder_proc_unlock(proc);
355b0502
GKH
3821 break;
3822 }
3823 death = ref->death;
3824 if (death->cookie != cookie) {
9c0a1d02 3825 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
355b0502 3826 proc->pid, thread->pid,
9c0a1d02
AH
3827 (u64)death->cookie,
3828 (u64)cookie);
25de59a5 3829 binder_node_unlock(ref->node);
5717118f 3830 binder_proc_unlock(proc);
355b0502
GKH
3831 break;
3832 }
3833 ref->death = NULL;
aad8732c 3834 binder_inner_proc_lock(proc);
355b0502
GKH
3835 if (list_empty(&death->work.entry)) {
3836 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
aad8732c
TK
3837 if (thread->looper &
3838 (BINDER_LOOPER_STATE_REGISTERED |
3839 BINDER_LOOPER_STATE_ENTERED))
3fb0a5b6
MC
3840 binder_enqueue_thread_work_ilocked(
3841 thread,
3842 &death->work);
aad8732c
TK
3843 else {
3844 binder_enqueue_work_ilocked(
3845 &death->work,
3846 &proc->todo);
02545935 3847 binder_wakeup_proc_ilocked(
1200efe9 3848 proc);
355b0502
GKH
3849 }
3850 } else {
3851 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
3852 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
3853 }
aad8732c 3854 binder_inner_proc_unlock(proc);
355b0502 3855 }
914f62a0 3856 binder_node_unlock(ref->node);
5717118f 3857 binder_proc_unlock(proc);
355b0502
GKH
3858 } break;
3859 case BC_DEAD_BINDER_DONE: {
3860 struct binder_work *w;
9c0a1d02 3861 binder_uintptr_t cookie;
355b0502 3862 struct binder_ref_death *death = NULL;
01c8cbde 3863
d31f0c40 3864 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
355b0502
GKH
3865 return -EFAULT;
3866
92716603 3867 ptr += sizeof(cookie);
aad8732c
TK
3868 binder_inner_proc_lock(proc);
3869 list_for_each_entry(w, &proc->delivered_death,
3870 entry) {
3871 struct binder_ref_death *tmp_death =
3872 container_of(w,
3873 struct binder_ref_death,
3874 work);
01c8cbde 3875
355b0502
GKH
3876 if (tmp_death->cookie == cookie) {
3877 death = tmp_death;
3878 break;
3879 }
3880 }
3881 binder_debug(BINDER_DEBUG_DEAD_BINDER,
3877fd57
DW
3882 "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n",
3883 proc->pid, thread->pid, (u64)cookie,
3884 death);
355b0502 3885 if (death == NULL) {
9c0a1d02
AH
3886 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
3887 proc->pid, thread->pid, (u64)cookie);
aad8732c 3888 binder_inner_proc_unlock(proc);
355b0502
GKH
3889 break;
3890 }
aad8732c 3891 binder_dequeue_work_ilocked(&death->work);
355b0502
GKH
3892 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
3893 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
aad8732c
TK
3894 if (thread->looper &
3895 (BINDER_LOOPER_STATE_REGISTERED |
3896 BINDER_LOOPER_STATE_ENTERED))
3fb0a5b6
MC
3897 binder_enqueue_thread_work_ilocked(
3898 thread, &death->work);
aad8732c
TK
3899 else {
3900 binder_enqueue_work_ilocked(
3901 &death->work,
3902 &proc->todo);
1200efe9 3903 binder_wakeup_proc_ilocked(proc);
355b0502
GKH
3904 }
3905 }
aad8732c 3906 binder_inner_proc_unlock(proc);
355b0502
GKH
3907 } break;
3908
3909 default:
56b468fc 3910 pr_err("%d:%d unknown command %d\n",
355b0502
GKH
3911 proc->pid, thread->pid, cmd);
3912 return -EINVAL;
3913 }
3914 *consumed = ptr - buffer;
3915 }
3916 return 0;
3917}
3918
48cd96f5
BP
3919static void binder_stat_br(struct binder_proc *proc,
3920 struct binder_thread *thread, uint32_t cmd)
355b0502 3921{
975a1ac9 3922 trace_binder_return(cmd);
7eac155d 3923 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
340b5f86
BJS
3924 atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
3925 atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
3926 atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
355b0502
GKH
3927 }
3928}
3929
9342b49f
TK
3930static int binder_put_node_cmd(struct binder_proc *proc,
3931 struct binder_thread *thread,
3932 void __user **ptrp,
3933 binder_uintptr_t node_ptr,
3934 binder_uintptr_t node_cookie,
3935 int node_debug_id,
3936 uint32_t cmd, const char *cmd_name)
3937{
3938 void __user *ptr = *ptrp;
3939
3940 if (put_user(cmd, (uint32_t __user *)ptr))
3941 return -EFAULT;
3942 ptr += sizeof(uint32_t);
3943
3944 if (put_user(node_ptr, (binder_uintptr_t __user *)ptr))
3945 return -EFAULT;
3946 ptr += sizeof(binder_uintptr_t);
3947
3948 if (put_user(node_cookie, (binder_uintptr_t __user *)ptr))
3949 return -EFAULT;
3950 ptr += sizeof(binder_uintptr_t);
3951
3952 binder_stat_br(proc, thread, cmd);
3953 binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n",
3954 proc->pid, thread->pid, cmd_name, node_debug_id,
3955 (u64)node_ptr, (u64)node_cookie);
3956
3957 *ptrp = ptr;
3958 return 0;
3959}
3960
02545935
MC
3961static int binder_wait_for_work(struct binder_thread *thread,
3962 bool do_proc_work)
3963{
3964 DEFINE_WAIT(wait);
3965 struct binder_proc *proc = thread->proc;
3966 int ret = 0;
3967
3968 freezer_do_not_count();
3969 binder_inner_proc_lock(proc);
3970 for (;;) {
3971 prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE);
3972 if (binder_has_work_ilocked(thread, do_proc_work))
3973 break;
3974 if (do_proc_work)
3975 list_add(&thread->waiting_thread_node,
3976 &proc->waiting_threads);
3977 binder_inner_proc_unlock(proc);
3978 schedule();
3979 binder_inner_proc_lock(proc);
3980 list_del_init(&thread->waiting_thread_node);
3981 if (signal_pending(current)) {
3982 ret = -ERESTARTSYS;
3983 break;
3984 }
3985 }
3986 finish_wait(&thread->wait, &wait);
3987 binder_inner_proc_unlock(proc);
3988 freezer_count();
3989
3990 return ret;
3991}
3992
355b0502
GKH
3993static int binder_thread_read(struct binder_proc *proc,
3994 struct binder_thread *thread,
9c0a1d02
AH
3995 binder_uintptr_t binder_buffer, size_t size,
3996 binder_size_t *consumed, int non_block)
355b0502 3997{
9c0a1d02 3998 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
355b0502
GKH
3999 void __user *ptr = buffer + *consumed;
4000 void __user *end = buffer + size;
4001
4002 int ret = 0;
4003 int wait_for_proc_work;
4004
4005 if (*consumed == 0) {
d31f0c40 4006 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
355b0502
GKH
4007 return -EFAULT;
4008 ptr += sizeof(uint32_t);
4009 }
4010
4011retry:
a45c586f 4012 binder_inner_proc_lock(proc);
02545935 4013 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
a45c586f 4014 binder_inner_proc_unlock(proc);
355b0502 4015
355b0502 4016 thread->looper |= BINDER_LOOPER_STATE_WAITING;
975a1ac9 4017
975a1ac9
AH
4018 trace_binder_wait_for_work(wait_for_proc_work,
4019 !!thread->transaction_stack,
aad8732c 4020 !binder_worklist_empty(proc, &thread->todo));
355b0502
GKH
4021 if (wait_for_proc_work) {
4022 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4023 BINDER_LOOPER_STATE_ENTERED))) {
56b468fc 4024 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
4025 proc->pid, thread->pid, thread->looper);
4026 wait_event_interruptible(binder_user_error_wait,
4027 binder_stop_on_user_error < 2);
4028 }
dadba0f5 4029 binder_restore_priority(current, proc->default_priority);
02545935
MC
4030 }
4031
4032 if (non_block) {
4033 if (!binder_has_work(thread, wait_for_proc_work))
4034 ret = -EAGAIN;
355b0502 4035 } else {
02545935 4036 ret = binder_wait_for_work(thread, wait_for_proc_work);
355b0502 4037 }
975a1ac9 4038
355b0502
GKH
4039 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
4040
4041 if (ret)
4042 return ret;
4043
4044 while (1) {
4045 uint32_t cmd;
4046 struct binder_transaction_data tr;
aad8732c
TK
4047 struct binder_work *w = NULL;
4048 struct list_head *list = NULL;
355b0502 4049 struct binder_transaction *t = NULL;
82f6ad88 4050 struct binder_thread *t_from;
355b0502 4051
ccca76b6 4052 binder_inner_proc_lock(proc);
aad8732c
TK
4053 if (!binder_worklist_empty_ilocked(&thread->todo))
4054 list = &thread->todo;
4055 else if (!binder_worklist_empty_ilocked(&proc->todo) &&
4056 wait_for_proc_work)
4057 list = &proc->todo;
4058 else {
4059 binder_inner_proc_unlock(proc);
4060
c789d154 4061 /* no data added */
85ab7b9c 4062 if (ptr - buffer == 4 && !thread->looper_need_return)
355b0502
GKH
4063 goto retry;
4064 break;
4065 }
4066
ccca76b6
TK
4067 if (end - ptr < sizeof(tr) + 4) {
4068 binder_inner_proc_unlock(proc);
355b0502 4069 break;
ccca76b6 4070 }
aad8732c 4071 w = binder_dequeue_work_head_ilocked(list);
3fb0a5b6
MC
4072 if (binder_worklist_empty_ilocked(&thread->todo))
4073 thread->process_todo = false;
355b0502
GKH
4074
4075 switch (w->type) {
4076 case BINDER_WORK_TRANSACTION: {
ccca76b6 4077 binder_inner_proc_unlock(proc);
355b0502
GKH
4078 t = container_of(w, struct binder_transaction, work);
4079 } break;
795aa6bc
TK
4080 case BINDER_WORK_RETURN_ERROR: {
4081 struct binder_error *e = container_of(
4082 w, struct binder_error, work);
4083
4084 WARN_ON(e->cmd == BR_OK);
ccca76b6 4085 binder_inner_proc_unlock(proc);
795aa6bc
TK
4086 if (put_user(e->cmd, (uint32_t __user *)ptr))
4087 return -EFAULT;
4088 e->cmd = BR_OK;
4089 ptr += sizeof(uint32_t);
4090
4091 binder_stat_br(proc, thread, cmd);
795aa6bc 4092 } break;
355b0502 4093 case BINDER_WORK_TRANSACTION_COMPLETE: {
ccca76b6 4094 binder_inner_proc_unlock(proc);
355b0502 4095 cmd = BR_TRANSACTION_COMPLETE;
d31f0c40 4096 if (put_user(cmd, (uint32_t __user *)ptr))
355b0502
GKH
4097 return -EFAULT;
4098 ptr += sizeof(uint32_t);
4099
4100 binder_stat_br(proc, thread, cmd);
4101 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
56b468fc 4102 "%d:%d BR_TRANSACTION_COMPLETE\n",
355b0502 4103 proc->pid, thread->pid);
355b0502
GKH
4104 kfree(w);
4105 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4106 } break;
4107 case BINDER_WORK_NODE: {
4108 struct binder_node *node = container_of(w, struct binder_node, work);
9342b49f
TK
4109 int strong, weak;
4110 binder_uintptr_t node_ptr = node->ptr;
4111 binder_uintptr_t node_cookie = node->cookie;
4112 int node_debug_id = node->debug_id;
4113 int has_weak_ref;
4114 int has_strong_ref;
4115 void __user *orig_ptr = ptr;
4116
4117 BUG_ON(proc != node->proc);
4118 strong = node->internal_strong_refs ||
4119 node->local_strong_refs;
4120 weak = !hlist_empty(&node->refs) ||
9607bf70
TK
4121 node->local_weak_refs ||
4122 node->tmp_refs || strong;
9342b49f
TK
4123 has_strong_ref = node->has_strong_ref;
4124 has_weak_ref = node->has_weak_ref;
4125
4126 if (weak && !has_weak_ref) {
355b0502
GKH
4127 node->has_weak_ref = 1;
4128 node->pending_weak_ref = 1;
4129 node->local_weak_refs++;
9342b49f
TK
4130 }
4131 if (strong && !has_strong_ref) {
355b0502
GKH
4132 node->has_strong_ref = 1;
4133 node->pending_strong_ref = 1;
4134 node->local_strong_refs++;
9342b49f
TK
4135 }
4136 if (!strong && has_strong_ref)
355b0502 4137 node->has_strong_ref = 0;
9342b49f 4138 if (!weak && has_weak_ref)
355b0502 4139 node->has_weak_ref = 0;
9342b49f
TK
4140 if (!weak && !strong) {
4141 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4142 "%d:%d node %d u%016llx c%016llx deleted\n",
4143 proc->pid, thread->pid,
4144 node_debug_id,
4145 (u64)node_ptr,
4146 (u64)node_cookie);
4147 rb_erase(&node->rb_node, &proc->nodes);
ccca76b6 4148 binder_inner_proc_unlock(proc);
25de59a5
TK
4149 binder_node_lock(node);
4150 /*
4151 * Acquire the node lock before freeing the
4152 * node to serialize with other threads that
4153 * may have been holding the node lock while
4154 * decrementing this node (avoids race where
4155 * this thread frees while the other thread
4156 * is unlocking the node after the final
4157 * decrement)
4158 */
4159 binder_node_unlock(node);
ccca76b6
TK
4160 binder_free_node(node);
4161 } else
4162 binder_inner_proc_unlock(proc);
4163
9342b49f
TK
4164 if (weak && !has_weak_ref)
4165 ret = binder_put_node_cmd(
4166 proc, thread, &ptr, node_ptr,
4167 node_cookie, node_debug_id,
4168 BR_INCREFS, "BR_INCREFS");
4169 if (!ret && strong && !has_strong_ref)
4170 ret = binder_put_node_cmd(
4171 proc, thread, &ptr, node_ptr,
4172 node_cookie, node_debug_id,
4173 BR_ACQUIRE, "BR_ACQUIRE");
4174 if (!ret && !strong && has_strong_ref)
4175 ret = binder_put_node_cmd(
4176 proc, thread, &ptr, node_ptr,
4177 node_cookie, node_debug_id,
4178 BR_RELEASE, "BR_RELEASE");
4179 if (!ret && !weak && has_weak_ref)
4180 ret = binder_put_node_cmd(
4181 proc, thread, &ptr, node_ptr,
4182 node_cookie, node_debug_id,
4183 BR_DECREFS, "BR_DECREFS");
4184 if (orig_ptr == ptr)
4185 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4186 "%d:%d node %d u%016llx c%016llx state unchanged\n",
4187 proc->pid, thread->pid,
4188 node_debug_id,
4189 (u64)node_ptr,
4190 (u64)node_cookie);
4191 if (ret)
4192 return ret;
355b0502
GKH
4193 } break;
4194 case BINDER_WORK_DEAD_BINDER:
4195 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4196 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4197 struct binder_ref_death *death;
4198 uint32_t cmd;
914f62a0 4199 binder_uintptr_t cookie;
355b0502
GKH
4200
4201 death = container_of(w, struct binder_ref_death, work);
4202 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
4203 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
4204 else
4205 cmd = BR_DEAD_BINDER;
914f62a0
MC
4206 cookie = death->cookie;
4207
355b0502 4208 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
9c0a1d02 4209 "%d:%d %s %016llx\n",
355b0502
GKH
4210 proc->pid, thread->pid,
4211 cmd == BR_DEAD_BINDER ?
4212 "BR_DEAD_BINDER" :
4213 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
914f62a0 4214 (u64)cookie);
355b0502 4215 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
914f62a0 4216 binder_inner_proc_unlock(proc);
355b0502
GKH
4217 kfree(death);
4218 binder_stats_deleted(BINDER_STAT_DEATH);
ccca76b6 4219 } else {
aad8732c
TK
4220 binder_enqueue_work_ilocked(
4221 w, &proc->delivered_death);
ccca76b6
TK
4222 binder_inner_proc_unlock(proc);
4223 }
914f62a0
MC
4224 if (put_user(cmd, (uint32_t __user *)ptr))
4225 return -EFAULT;
4226 ptr += sizeof(uint32_t);
4227 if (put_user(cookie,
4228 (binder_uintptr_t __user *)ptr))
4229 return -EFAULT;
4230 ptr += sizeof(binder_uintptr_t);
4231 binder_stat_br(proc, thread, cmd);
355b0502
GKH
4232 if (cmd == BR_DEAD_BINDER)
4233 goto done; /* DEAD_BINDER notifications can cause transactions */
4234 } break;
4235 }
4236
4237 if (!t)
4238 continue;
4239
4240 BUG_ON(t->buffer == NULL);
4241 if (t->buffer->target_node) {
4242 struct binder_node *target_node = t->buffer->target_node;
c1dd125f 4243 struct binder_priority node_prio;
01c8cbde 4244
355b0502
GKH
4245 tr.target.ptr = target_node->ptr;
4246 tr.cookie = target_node->cookie;
c1dd125f
MC
4247 node_prio.sched_policy = target_node->sched_policy;
4248 node_prio.prio = target_node->min_priority;
21dfe3eb
MC
4249 binder_transaction_priority(current, t, node_prio,
4250 target_node->inherit_rt);
355b0502
GKH
4251 cmd = BR_TRANSACTION;
4252 } else {
9c0a1d02
AH
4253 tr.target.ptr = 0;
4254 tr.cookie = 0;
355b0502
GKH
4255 cmd = BR_REPLY;
4256 }
4257 tr.code = t->code;
4258 tr.flags = t->flags;
4a2ebb93 4259 tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
355b0502 4260
82f6ad88
TK
4261 t_from = binder_get_txn_from(t);
4262 if (t_from) {
4263 struct task_struct *sender = t_from->proc->tsk;
01c8cbde 4264
355b0502 4265 tr.sender_pid = task_tgid_nr_ns(sender,
17cf22c3 4266 task_active_pid_ns(current));
355b0502
GKH
4267 } else {
4268 tr.sender_pid = 0;
4269 }
4270
4271 tr.data_size = t->buffer->data_size;
4272 tr.offsets_size = t->buffer->offsets_size;
69c33bb1
TK
4273 tr.data.ptr.buffer = (binder_uintptr_t)
4274 ((uintptr_t)t->buffer->data +
4275 binder_alloc_get_user_buffer_offset(&proc->alloc));
355b0502
GKH
4276 tr.data.ptr.offsets = tr.data.ptr.buffer +
4277 ALIGN(t->buffer->data_size,
4278 sizeof(void *));
4279
82f6ad88
TK
4280 if (put_user(cmd, (uint32_t __user *)ptr)) {
4281 if (t_from)
4282 binder_thread_dec_tmpref(t_from);
1d6de5d0
MC
4283
4284 binder_cleanup_transaction(t, "put_user failed",
4285 BR_FAILED_REPLY);
4286
355b0502 4287 return -EFAULT;
82f6ad88 4288 }
355b0502 4289 ptr += sizeof(uint32_t);
82f6ad88
TK
4290 if (copy_to_user(ptr, &tr, sizeof(tr))) {
4291 if (t_from)
4292 binder_thread_dec_tmpref(t_from);
1d6de5d0
MC
4293
4294 binder_cleanup_transaction(t, "copy_to_user failed",
4295 BR_FAILED_REPLY);
4296
355b0502 4297 return -EFAULT;
82f6ad88 4298 }
355b0502
GKH
4299 ptr += sizeof(tr);
4300
975a1ac9 4301 trace_binder_transaction_received(t);
355b0502
GKH
4302 binder_stat_br(proc, thread, cmd);
4303 binder_debug(BINDER_DEBUG_TRANSACTION,
9c0a1d02 4304 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
355b0502
GKH
4305 proc->pid, thread->pid,
4306 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
4307 "BR_REPLY",
82f6ad88
TK
4308 t->debug_id, t_from ? t_from->proc->pid : 0,
4309 t_from ? t_from->pid : 0, cmd,
355b0502 4310 t->buffer->data_size, t->buffer->offsets_size,
9c0a1d02 4311 (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
355b0502 4312
82f6ad88
TK
4313 if (t_from)
4314 binder_thread_dec_tmpref(t_from);
355b0502
GKH
4315 t->buffer->allow_user_free = 1;
4316 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
a45c586f 4317 binder_inner_proc_lock(thread->proc);
355b0502
GKH
4318 t->to_parent = thread->transaction_stack;
4319 t->to_thread = thread;
4320 thread->transaction_stack = t;
a45c586f 4321 binder_inner_proc_unlock(thread->proc);
355b0502 4322 } else {
7323366b 4323 binder_free_transaction(t);
355b0502
GKH
4324 }
4325 break;
4326 }
4327
4328done:
4329
4330 *consumed = ptr - buffer;
e09d7b9a 4331 binder_inner_proc_lock(proc);
02545935
MC
4332 if (proc->requested_threads == 0 &&
4333 list_empty(&thread->proc->waiting_threads) &&
355b0502
GKH
4334 proc->requested_threads_started < proc->max_threads &&
4335 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4336 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
4337 /*spawn a new thread if we leave this out */) {
4338 proc->requested_threads++;
e09d7b9a 4339 binder_inner_proc_unlock(proc);
355b0502 4340 binder_debug(BINDER_DEBUG_THREADS,
56b468fc 4341 "%d:%d BR_SPAWN_LOOPER\n",
355b0502 4342 proc->pid, thread->pid);
d31f0c40 4343 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
355b0502 4344 return -EFAULT;
89334ab4 4345 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
e09d7b9a
TK
4346 } else
4347 binder_inner_proc_unlock(proc);
355b0502
GKH
4348 return 0;
4349}
4350
aad8732c
TK
4351static void binder_release_work(struct binder_proc *proc,
4352 struct list_head *list)
355b0502
GKH
4353{
4354 struct binder_work *w;
01c8cbde 4355
aad8732c
TK
4356 while (1) {
4357 w = binder_dequeue_work_head(proc, list);
4358 if (!w)
4359 return;
4360
355b0502
GKH
4361 switch (w->type) {
4362 case BINDER_WORK_TRANSACTION: {
4363 struct binder_transaction *t;
4364
4365 t = container_of(w, struct binder_transaction, work);
1d6de5d0
MC
4366
4367 binder_cleanup_transaction(t, "process died.",
4368 BR_DEAD_REPLY);
355b0502 4369 } break;
795aa6bc
TK
4370 case BINDER_WORK_RETURN_ERROR: {
4371 struct binder_error *e = container_of(
4372 w, struct binder_error, work);
4373
4374 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4375 "undelivered TRANSACTION_ERROR: %u\n",
4376 e->cmd);
4377 } break;
355b0502 4378 case BINDER_WORK_TRANSACTION_COMPLETE: {
675d66b0 4379 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
56b468fc 4380 "undelivered TRANSACTION_COMPLETE\n");
355b0502
GKH
4381 kfree(w);
4382 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4383 } break;
675d66b0
AH
4384 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4385 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4386 struct binder_ref_death *death;
4387
4388 death = container_of(w, struct binder_ref_death, work);
4389 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
9c0a1d02
AH
4390 "undelivered death notification, %016llx\n",
4391 (u64)death->cookie);
675d66b0
AH
4392 kfree(death);
4393 binder_stats_deleted(BINDER_STAT_DEATH);
4394 } break;
355b0502 4395 default:
56b468fc 4396 pr_err("unexpected work type, %d, not freed\n",
675d66b0 4397 w->type);
355b0502
GKH
4398 break;
4399 }
4400 }
4401
4402}
4403
aed01731
TK
4404static struct binder_thread *binder_get_thread_ilocked(
4405 struct binder_proc *proc, struct binder_thread *new_thread)
355b0502
GKH
4406{
4407 struct binder_thread *thread = NULL;
4408 struct rb_node *parent = NULL;
4409 struct rb_node **p = &proc->threads.rb_node;
4410
4411 while (*p) {
4412 parent = *p;
4413 thread = rb_entry(parent, struct binder_thread, rb_node);
4414
4415 if (current->pid < thread->pid)
4416 p = &(*p)->rb_left;
4417 else if (current->pid > thread->pid)
4418 p = &(*p)->rb_right;
4419 else
aed01731 4420 return thread;
355b0502 4421 }
aed01731
TK
4422 if (!new_thread)
4423 return NULL;
4424 thread = new_thread;
4425 binder_stats_created(BINDER_STAT_THREAD);
4426 thread->proc = proc;
4427 thread->pid = current->pid;
c1dd125f
MC
4428 get_task_struct(current);
4429 thread->task = current;
aed01731
TK
4430 atomic_set(&thread->tmp_ref, 0);
4431 init_waitqueue_head(&thread->wait);
4432 INIT_LIST_HEAD(&thread->todo);
4433 rb_link_node(&thread->rb_node, parent, p);
4434 rb_insert_color(&thread->rb_node, &proc->threads);
4435 thread->looper_need_return = true;
4436 thread->return_error.work.type = BINDER_WORK_RETURN_ERROR;
4437 thread->return_error.cmd = BR_OK;
4438 thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR;
4439 thread->reply_error.cmd = BR_OK;
02545935 4440 INIT_LIST_HEAD(&new_thread->waiting_thread_node);
aed01731
TK
4441 return thread;
4442}
4443
4444static struct binder_thread *binder_get_thread(struct binder_proc *proc)
4445{
4446 struct binder_thread *thread;
4447 struct binder_thread *new_thread;
4448
4449 binder_inner_proc_lock(proc);
4450 thread = binder_get_thread_ilocked(proc, NULL);
4451 binder_inner_proc_unlock(proc);
4452 if (!thread) {
4453 new_thread = kzalloc(sizeof(*thread), GFP_KERNEL);
4454 if (new_thread == NULL)
355b0502 4455 return NULL;
aed01731
TK
4456 binder_inner_proc_lock(proc);
4457 thread = binder_get_thread_ilocked(proc, new_thread);
4458 binder_inner_proc_unlock(proc);
4459 if (thread != new_thread)
4460 kfree(new_thread);
355b0502
GKH
4461 }
4462 return thread;
4463}
4464
82f6ad88
TK
4465static void binder_free_proc(struct binder_proc *proc)
4466{
4467 BUG_ON(!list_empty(&proc->todo));
4468 BUG_ON(!list_empty(&proc->delivered_death));
4469 binder_alloc_deferred_release(&proc->alloc);
4470 put_task_struct(proc->tsk);
4471 binder_stats_deleted(BINDER_STAT_PROC);
4472 kfree(proc);
4473}
4474
4475static void binder_free_thread(struct binder_thread *thread)
4476{
4477 BUG_ON(!list_empty(&thread->todo));
4478 binder_stats_deleted(BINDER_STAT_THREAD);
4479 binder_proc_dec_tmpref(thread->proc);
c1dd125f 4480 put_task_struct(thread->task);
82f6ad88
TK
4481 kfree(thread);
4482}
4483
4484static int binder_thread_release(struct binder_proc *proc,
4485 struct binder_thread *thread)
355b0502
GKH
4486{
4487 struct binder_transaction *t;
4488 struct binder_transaction *send_reply = NULL;
4489 int active_transactions = 0;
82f6ad88 4490 struct binder_transaction *last_t = NULL;
355b0502 4491
aed01731 4492 binder_inner_proc_lock(thread->proc);
82f6ad88
TK
4493 /*
4494 * take a ref on the proc so it survives
4495 * after we remove this thread from proc->threads.
4496 * The corresponding dec is when we actually
4497 * free the thread in binder_free_thread()
4498 */
4499 proc->tmp_ref++;
4500 /*
4501 * take a ref on this thread to ensure it
4502 * survives while we are releasing it
4503 */
4504 atomic_inc(&thread->tmp_ref);
355b0502
GKH
4505 rb_erase(&thread->rb_node, &proc->threads);
4506 t = thread->transaction_stack;
82f6ad88
TK
4507 if (t) {
4508 spin_lock(&t->lock);
4509 if (t->to_thread == thread)
4510 send_reply = t;
4511 }
4512 thread->is_dead = true;
4513
355b0502 4514 while (t) {
82f6ad88 4515 last_t = t;
355b0502
GKH
4516 active_transactions++;
4517 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
56b468fc
AS
4518 "release %d:%d transaction %d %s, still active\n",
4519 proc->pid, thread->pid,
355b0502
GKH
4520 t->debug_id,
4521 (t->to_thread == thread) ? "in" : "out");
4522
4523 if (t->to_thread == thread) {
4524 t->to_proc = NULL;
4525 t->to_thread = NULL;
4526 if (t->buffer) {
4527 t->buffer->transaction = NULL;
4528 t->buffer = NULL;
4529 }
4530 t = t->to_parent;
4531 } else if (t->from == thread) {
4532 t->from = NULL;
4533 t = t->from_parent;
4534 } else
4535 BUG();
82f6ad88
TK
4536 spin_unlock(&last_t->lock);
4537 if (t)
4538 spin_lock(&t->lock);
355b0502 4539 }
e0391afe
MC
4540
4541 /*
4542 * If this thread used poll, make sure we remove the waitqueue
4543 * from any epoll data structures holding it with POLLFREE.
4544 * waitqueue_active() is safe to use here because we're holding
4545 * the inner lock.
4546 */
4547 if ((thread->looper & BINDER_LOOPER_STATE_POLL) &&
4548 waitqueue_active(&thread->wait)) {
4549 wake_up_poll(&thread->wait, POLLHUP | POLLFREE);
4550 }
4551
aed01731 4552 binder_inner_proc_unlock(thread->proc);
82f6ad88 4553
c0bf025b
MC
4554 /*
4555 * This is needed to avoid races between wake_up_poll() above and
4556 * and ep_remove_waitqueue() called for other reasons (eg the epoll file
4557 * descriptor being closed); ep_remove_waitqueue() holds an RCU read
4558 * lock, so we can be sure it's done after calling synchronize_rcu().
4559 */
4560 if (thread->looper & BINDER_LOOPER_STATE_POLL)
4561 synchronize_rcu();
4562
355b0502
GKH
4563 if (send_reply)
4564 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
aad8732c 4565 binder_release_work(proc, &thread->todo);
82f6ad88 4566 binder_thread_dec_tmpref(thread);
355b0502
GKH
4567 return active_transactions;
4568}
4569
4570static unsigned int binder_poll(struct file *filp,
4571 struct poll_table_struct *wait)
4572{
4573 struct binder_proc *proc = filp->private_data;
4574 struct binder_thread *thread = NULL;
02545935 4575 bool wait_for_proc_work;
355b0502 4576
355b0502
GKH
4577 thread = binder_get_thread(proc);
4578
a45c586f 4579 binder_inner_proc_lock(thread->proc);
02545935
MC
4580 thread->looper |= BINDER_LOOPER_STATE_POLL;
4581 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
4582
a45c586f 4583 binder_inner_proc_unlock(thread->proc);
975a1ac9 4584
02545935
MC
4585 poll_wait(filp, &thread->wait, wait);
4586
21ad255d 4587 if (binder_has_work(thread, wait_for_proc_work))
02545935
MC
4588 return POLLIN;
4589
355b0502
GKH
4590 return 0;
4591}
4592
74961582
RA
4593static int binder_ioctl_write_read(struct file *filp,
4594 unsigned int cmd, unsigned long arg,
4595 struct binder_thread *thread)
4596{
4597 int ret = 0;
4598 struct binder_proc *proc = filp->private_data;
4599 unsigned int size = _IOC_SIZE(cmd);
4600 void __user *ubuf = (void __user *)arg;
4601 struct binder_write_read bwr;
4602
4603 if (size != sizeof(struct binder_write_read)) {
4604 ret = -EINVAL;
4605 goto out;
4606 }
d31f0c40 4607 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
74961582
RA
4608 ret = -EFAULT;
4609 goto out;
4610 }
4611 binder_debug(BINDER_DEBUG_READ_WRITE,
4612 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
4613 proc->pid, thread->pid,
4614 (u64)bwr.write_size, (u64)bwr.write_buffer,
4615 (u64)bwr.read_size, (u64)bwr.read_buffer);
4616
4617 if (bwr.write_size > 0) {
4618 ret = binder_thread_write(proc, thread,
4619 bwr.write_buffer,
4620 bwr.write_size,
4621 &bwr.write_consumed);
4622 trace_binder_write_done(ret);
4623 if (ret < 0) {
4624 bwr.read_consumed = 0;
d31f0c40 4625 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
74961582
RA
4626 ret = -EFAULT;
4627 goto out;
4628 }
4629 }
4630 if (bwr.read_size > 0) {
4631 ret = binder_thread_read(proc, thread, bwr.read_buffer,
4632 bwr.read_size,
4633 &bwr.read_consumed,
4634 filp->f_flags & O_NONBLOCK);
4635 trace_binder_read_done(ret);
02545935
MC
4636 binder_inner_proc_lock(proc);
4637 if (!binder_worklist_empty_ilocked(&proc->todo))
1200efe9 4638 binder_wakeup_proc_ilocked(proc);
02545935 4639 binder_inner_proc_unlock(proc);
74961582 4640 if (ret < 0) {
d31f0c40 4641 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
74961582
RA
4642 ret = -EFAULT;
4643 goto out;
4644 }
4645 }
4646 binder_debug(BINDER_DEBUG_READ_WRITE,
4647 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
4648 proc->pid, thread->pid,
4649 (u64)bwr.write_consumed, (u64)bwr.write_size,
4650 (u64)bwr.read_consumed, (u64)bwr.read_size);
d31f0c40 4651 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
74961582
RA
4652 ret = -EFAULT;
4653 goto out;
4654 }
4655out:
4656 return ret;
4657}
4658
4659static int binder_ioctl_set_ctx_mgr(struct file *filp)
4660{
4661 int ret = 0;
4662 struct binder_proc *proc = filp->private_data;
913926d0 4663 struct binder_context *context = proc->context;
76341f6f 4664 struct binder_node *new_node;
74961582
RA
4665 kuid_t curr_euid = current_euid();
4666
76341f6f 4667 mutex_lock(&context->context_mgr_node_lock);
913926d0 4668 if (context->binder_context_mgr_node) {
74961582
RA
4669 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
4670 ret = -EBUSY;
4671 goto out;
4672 }
3b090f5e
S
4673 ret = security_binder_set_context_mgr(proc->tsk);
4674 if (ret < 0)
4675 goto out;
913926d0
MC
4676 if (uid_valid(context->binder_context_mgr_uid)) {
4677 if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
74961582
RA
4678 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
4679 from_kuid(&init_user_ns, curr_euid),
4680 from_kuid(&init_user_ns,
913926d0 4681 context->binder_context_mgr_uid));
74961582
RA
4682 ret = -EPERM;
4683 goto out;
4684 }
4685 } else {
913926d0 4686 context->binder_context_mgr_uid = curr_euid;
74961582 4687 }
25de59a5 4688 new_node = binder_new_node(proc, NULL);
76341f6f 4689 if (!new_node) {
74961582
RA
4690 ret = -ENOMEM;
4691 goto out;
4692 }
25de59a5 4693 binder_node_lock(new_node);
76341f6f
TK
4694 new_node->local_weak_refs++;
4695 new_node->local_strong_refs++;
4696 new_node->has_strong_ref = 1;
4697 new_node->has_weak_ref = 1;
4698 context->binder_context_mgr_node = new_node;
25de59a5 4699 binder_node_unlock(new_node);
9607bf70 4700 binder_put_node(new_node);
74961582 4701out:
76341f6f 4702 mutex_unlock(&context->context_mgr_node_lock);
74961582
RA
4703 return ret;
4704}
4705
96f60059
MC
4706static int binder_ioctl_get_node_info_for_ref(struct binder_proc *proc,
4707 struct binder_node_info_for_ref *info)
4708{
4709 struct binder_node *node;
4710 struct binder_context *context = proc->context;
4711 __u32 handle = info->handle;
4712
4713 if (info->strong_count || info->weak_count || info->reserved1 ||
4714 info->reserved2 || info->reserved3) {
4715 binder_user_error("%d BINDER_GET_NODE_INFO_FOR_REF: only handle may be non-zero.",
4716 proc->pid);
4717 return -EINVAL;
4718 }
4719
4720 /* This ioctl may only be used by the context manager */
4721 mutex_lock(&context->context_mgr_node_lock);
4722 if (!context->binder_context_mgr_node ||
4723 context->binder_context_mgr_node->proc != proc) {
4724 mutex_unlock(&context->context_mgr_node_lock);
4725 return -EPERM;
4726 }
4727 mutex_unlock(&context->context_mgr_node_lock);
4728
4729 node = binder_get_node_from_ref(proc, handle, true, NULL);
4730 if (!node)
4731 return -EINVAL;
4732
4733 info->strong_count = node->local_strong_refs +
4734 node->internal_strong_refs;
4735 info->weak_count = node->local_weak_refs;
4736
4737 binder_put_node(node);
4738
4739 return 0;
4740}
4741
66266ad3
CC
4742static int binder_ioctl_get_node_debug_info(struct binder_proc *proc,
4743 struct binder_node_debug_info *info) {
4744 struct rb_node *n;
4745 binder_uintptr_t ptr = info->ptr;
4746
4747 memset(info, 0, sizeof(*info));
4748
4749 binder_inner_proc_lock(proc);
4750 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
4751 struct binder_node *node = rb_entry(n, struct binder_node,
4752 rb_node);
4753 if (node->ptr > ptr) {
4754 info->ptr = node->ptr;
4755 info->cookie = node->cookie;
4756 info->has_strong_ref = node->has_strong_ref;
4757 info->has_weak_ref = node->has_weak_ref;
4758 break;
4759 }
4760 }
4761 binder_inner_proc_unlock(proc);
4762
4763 return 0;
4764}
4765
355b0502
GKH
4766static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4767{
4768 int ret;
4769 struct binder_proc *proc = filp->private_data;
4770 struct binder_thread *thread;
4771 unsigned int size = _IOC_SIZE(cmd);
4772 void __user *ubuf = (void __user *)arg;
4773
74961582
RA
4774 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
4775 proc->pid, current->pid, cmd, arg);*/
355b0502 4776
b020aa3f
SY
4777 binder_selftest_alloc(&proc->alloc);
4778
975a1ac9
AH
4779 trace_binder_ioctl(cmd, arg);
4780
355b0502
GKH
4781 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4782 if (ret)
975a1ac9 4783 goto err_unlocked;
355b0502 4784
355b0502
GKH
4785 thread = binder_get_thread(proc);
4786 if (thread == NULL) {
4787 ret = -ENOMEM;
4788 goto err;
4789 }
4790
4791 switch (cmd) {
74961582
RA
4792 case BINDER_WRITE_READ:
4793 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
4794 if (ret)
355b0502 4795 goto err;
355b0502 4796 break;
e09d7b9a
TK
4797 case BINDER_SET_MAX_THREADS: {
4798 int max_threads;
4799
4800 if (copy_from_user(&max_threads, ubuf,
4801 sizeof(max_threads))) {
355b0502
GKH
4802 ret = -EINVAL;
4803 goto err;
4804 }
e09d7b9a
TK
4805 binder_inner_proc_lock(proc);
4806 proc->max_threads = max_threads;
4807 binder_inner_proc_unlock(proc);
355b0502 4808 break;
e09d7b9a 4809 }
355b0502 4810 case BINDER_SET_CONTEXT_MGR:
74961582
RA
4811 ret = binder_ioctl_set_ctx_mgr(filp);
4812 if (ret)
355b0502 4813 goto err;
355b0502
GKH
4814 break;
4815 case BINDER_THREAD_EXIT:
56b468fc 4816 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
355b0502 4817 proc->pid, thread->pid);
82f6ad88 4818 binder_thread_release(proc, thread);
355b0502
GKH
4819 thread = NULL;
4820 break;
1b4ce188
MM
4821 case BINDER_VERSION: {
4822 struct binder_version __user *ver = ubuf;
4823
355b0502
GKH
4824 if (size != sizeof(struct binder_version)) {
4825 ret = -EINVAL;
4826 goto err;
4827 }
d31f0c40 4828 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
1b4ce188 4829 &ver->protocol_version)) {
355b0502
GKH
4830 ret = -EINVAL;
4831 goto err;
4832 }
4833 break;
1b4ce188 4834 }
96f60059
MC
4835 case BINDER_GET_NODE_INFO_FOR_REF: {
4836 struct binder_node_info_for_ref info;
4837
4838 if (copy_from_user(&info, ubuf, sizeof(info))) {
4839 ret = -EFAULT;
4840 goto err;
4841 }
4842
4843 ret = binder_ioctl_get_node_info_for_ref(proc, &info);
4844 if (ret < 0)
4845 goto err;
4846
4847 if (copy_to_user(ubuf, &info, sizeof(info))) {
4848 ret = -EFAULT;
4849 goto err;
4850 }
4851
4852 break;
4853 }
66266ad3
CC
4854 case BINDER_GET_NODE_DEBUG_INFO: {
4855 struct binder_node_debug_info info;
4856
4857 if (copy_from_user(&info, ubuf, sizeof(info))) {
4858 ret = -EFAULT;
4859 goto err;
4860 }
4861
4862 ret = binder_ioctl_get_node_debug_info(proc, &info);
4863 if (ret < 0)
4864 goto err;
4865
4866 if (copy_to_user(ubuf, &info, sizeof(info))) {
4867 ret = -EFAULT;
4868 goto err;
4869 }
4870 break;
4871 }
355b0502
GKH
4872 default:
4873 ret = -EINVAL;
4874 goto err;
4875 }
4876 ret = 0;
4877err:
4878 if (thread)
85ab7b9c 4879 thread->looper_need_return = false;
355b0502
GKH
4880 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4881 if (ret && ret != -ERESTARTSYS)
56b468fc 4882 pr_info("%d:%d ioctl %x %lx returned %d\n", proc->pid, current->pid, cmd, arg, ret);
975a1ac9
AH
4883err_unlocked:
4884 trace_binder_ioctl_done(ret);
355b0502
GKH
4885 return ret;
4886}
4887
4888static void binder_vma_open(struct vm_area_struct *vma)
4889{
4890 struct binder_proc *proc = vma->vm_private_data;
01c8cbde 4891
355b0502 4892 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
56b468fc 4893 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
355b0502
GKH
4894 proc->pid, vma->vm_start, vma->vm_end,
4895 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4896 (unsigned long)pgprot_val(vma->vm_page_prot));
355b0502
GKH
4897}
4898
4899static void binder_vma_close(struct vm_area_struct *vma)
4900{
4901 struct binder_proc *proc = vma->vm_private_data;
01c8cbde 4902
355b0502 4903 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
56b468fc 4904 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
355b0502
GKH
4905 proc->pid, vma->vm_start, vma->vm_end,
4906 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4907 (unsigned long)pgprot_val(vma->vm_page_prot));
69c33bb1 4908 binder_alloc_vma_close(&proc->alloc);
355b0502
GKH
4909 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
4910}
4911
430f7ee8
VM
4912static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
4913{
4914 return VM_FAULT_SIGBUS;
4915}
4916
355b0502
GKH
4917static struct vm_operations_struct binder_vm_ops = {
4918 .open = binder_vma_open,
4919 .close = binder_vma_close,
430f7ee8 4920 .fault = binder_vm_fault,
355b0502
GKH
4921};
4922
69c33bb1
TK
4923static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
4924{
4925 int ret;
4926 struct binder_proc *proc = filp->private_data;
4927 const char *failure_string;
4928
4929 if (proc->tsk != current->group_leader)
4930 return -EINVAL;
4931
4932 if ((vma->vm_end - vma->vm_start) > SZ_4M)
4933 vma->vm_end = vma->vm_start + SZ_4M;
4934
4935 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4936 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
4937 __func__, proc->pid, vma->vm_start, vma->vm_end,
4938 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4939 (unsigned long)pgprot_val(vma->vm_page_prot));
4940
4941 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
4942 ret = -EPERM;
4943 failure_string = "bad vm_flags";
4944 goto err_bad_arg;
4945 }
4946 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
4947 vma->vm_ops = &binder_vm_ops;
4948 vma->vm_private_data = proc;
4949
4950 ret = binder_alloc_mmap_handler(&proc->alloc, vma);
4951 if (ret)
4952 return ret;
4953 proc->files = get_files_struct(current);
4954 return 0;
4955
355b0502 4956err_bad_arg:
258767fe 4957 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
355b0502
GKH
4958 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
4959 return ret;
4960}
4961
4962static int binder_open(struct inode *nodp, struct file *filp)
4963{
4964 struct binder_proc *proc;
bfd99b42 4965 struct binder_device *binder_dev;
355b0502
GKH
4966
4967 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
4968 current->group_leader->pid, current->pid);
4969
4970 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
4971 if (proc == NULL)
4972 return -ENOMEM;
751c6a8e
TK
4973 spin_lock_init(&proc->inner_lock);
4974 spin_lock_init(&proc->outer_lock);
f43b3d58
TK
4975 get_task_struct(current->group_leader);
4976 proc->tsk = current->group_leader;
355b0502 4977 INIT_LIST_HEAD(&proc->todo);
425bb8fc
MC
4978 if (binder_supported_policy(current->policy)) {
4979 proc->default_priority.sched_policy = current->policy;
4980 proc->default_priority.prio = current->normal_prio;
4981 } else {
4982 proc->default_priority.sched_policy = SCHED_NORMAL;
4983 proc->default_priority.prio = NICE_TO_PRIO(0);
4984 }
4985
bfd99b42
MC
4986 binder_dev = container_of(filp->private_data, struct binder_device,
4987 miscdev);
4988 proc->context = &binder_dev->context;
69c33bb1 4989 binder_alloc_init(&proc->alloc);
975a1ac9 4990
355b0502 4991 binder_stats_created(BINDER_STAT_PROC);
355b0502
GKH
4992 proc->pid = current->group_leader->pid;
4993 INIT_LIST_HEAD(&proc->delivered_death);
02545935 4994 INIT_LIST_HEAD(&proc->waiting_threads);
355b0502 4995 filp->private_data = proc;
975a1ac9 4996
76341f6f
TK
4997 mutex_lock(&binder_procs_lock);
4998 hlist_add_head(&proc->proc_node, &binder_procs);
4999 mutex_unlock(&binder_procs_lock);
5000
16b66554 5001 if (binder_debugfs_dir_entry_proc) {
355b0502 5002 char strbuf[11];
01c8cbde 5003
355b0502 5004 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
a64af2cf
MC
5005 /*
5006 * proc debug entries are shared between contexts, so
5007 * this will fail if the process tries to open the driver
5008 * again with a different context. The priting code will
5009 * anyway print all contexts that a given PID has, so this
5010 * is not a problem.
5011 */
16b66554 5012 proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
a64af2cf
MC
5013 binder_debugfs_dir_entry_proc,
5014 (void *)(unsigned long)proc->pid,
5015 &binder_proc_fops);
355b0502
GKH
5016 }
5017
5018 return 0;
5019}
5020
5021static int binder_flush(struct file *filp, fl_owner_t id)
5022{
5023 struct binder_proc *proc = filp->private_data;
5024
5025 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
5026
5027 return 0;
5028}
5029
5030static void binder_deferred_flush(struct binder_proc *proc)
5031{
5032 struct rb_node *n;
5033 int wake_count = 0;
01c8cbde 5034
aed01731 5035 binder_inner_proc_lock(proc);
355b0502
GKH
5036 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
5037 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
01c8cbde 5038
85ab7b9c 5039 thread->looper_need_return = true;
355b0502
GKH
5040 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
5041 wake_up_interruptible(&thread->wait);
5042 wake_count++;
5043 }
5044 }
aed01731 5045 binder_inner_proc_unlock(proc);
355b0502
GKH
5046
5047 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5048 "binder_flush: %d woke %d threads\n", proc->pid,
5049 wake_count);
5050}
5051
5052static int binder_release(struct inode *nodp, struct file *filp)
5053{
5054 struct binder_proc *proc = filp->private_data;
01c8cbde 5055
16b66554 5056 debugfs_remove(proc->debugfs_entry);
355b0502
GKH
5057 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
5058
5059 return 0;
5060}
5061
008fa749
ME
5062static int binder_node_release(struct binder_node *node, int refs)
5063{
5064 struct binder_ref *ref;
5065 int death = 0;
ccca76b6 5066 struct binder_proc *proc = node->proc;
008fa749 5067
aad8732c 5068 binder_release_work(proc, &node->async_todo);
ccca76b6 5069
25de59a5 5070 binder_node_lock(node);
ccca76b6 5071 binder_inner_proc_lock(proc);
aad8732c 5072 binder_dequeue_work_ilocked(&node->work);
9607bf70
TK
5073 /*
5074 * The caller must have taken a temporary ref on the node,
5075 */
5076 BUG_ON(!node->tmp_refs);
5077 if (hlist_empty(&node->refs) && node->tmp_refs == 1) {
ccca76b6 5078 binder_inner_proc_unlock(proc);
25de59a5 5079 binder_node_unlock(node);
ccca76b6 5080 binder_free_node(node);
008fa749
ME
5081
5082 return refs;
5083 }
5084
5085 node->proc = NULL;
5086 node->local_strong_refs = 0;
5087 node->local_weak_refs = 0;
ccca76b6 5088 binder_inner_proc_unlock(proc);
76341f6f
TK
5089
5090 spin_lock(&binder_dead_nodes_lock);
7eac155d 5091 hlist_add_head(&node->dead_node, &binder_dead_nodes);
76341f6f 5092 spin_unlock(&binder_dead_nodes_lock);
008fa749
ME
5093
5094 hlist_for_each_entry(ref, &node->refs, node_entry) {
5095 refs++;
914f62a0
MC
5096 /*
5097 * Need the node lock to synchronize
5098 * with new notification requests and the
5099 * inner lock to synchronize with queued
5100 * death notifications.
5101 */
5102 binder_inner_proc_lock(ref->proc);
5103 if (!ref->death) {
5104 binder_inner_proc_unlock(ref->proc);
b3a59abf 5105 continue;
914f62a0 5106 }
008fa749
ME
5107
5108 death++;
5109
914f62a0
MC
5110 BUG_ON(!list_empty(&ref->death->work.entry));
5111 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
5112 binder_enqueue_work_ilocked(&ref->death->work,
5113 &ref->proc->todo);
1200efe9 5114 binder_wakeup_proc_ilocked(ref->proc);
aad8732c 5115 binder_inner_proc_unlock(ref->proc);
008fa749
ME
5116 }
5117
008fa749
ME
5118 binder_debug(BINDER_DEBUG_DEAD_BINDER,
5119 "node %d now dead, refs %d, death %d\n",
5120 node->debug_id, refs, death);
25de59a5 5121 binder_node_unlock(node);
9607bf70 5122 binder_put_node(node);
008fa749
ME
5123
5124 return refs;
5125}
5126
355b0502
GKH
5127static void binder_deferred_release(struct binder_proc *proc)
5128{
913926d0 5129 struct binder_context *context = proc->context;
355b0502 5130 struct rb_node *n;
69c33bb1 5131 int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
355b0502 5132
355b0502
GKH
5133 BUG_ON(proc->files);
5134
76341f6f 5135 mutex_lock(&binder_procs_lock);
355b0502 5136 hlist_del(&proc->proc_node);
76341f6f 5137 mutex_unlock(&binder_procs_lock);
53413e7d 5138
76341f6f 5139 mutex_lock(&context->context_mgr_node_lock);
913926d0
MC
5140 if (context->binder_context_mgr_node &&
5141 context->binder_context_mgr_node->proc == proc) {
355b0502 5142 binder_debug(BINDER_DEBUG_DEAD_BINDER,
c07c933f
ME
5143 "%s: %d context_mgr_node gone\n",
5144 __func__, proc->pid);
913926d0 5145 context->binder_context_mgr_node = NULL;
355b0502 5146 }
76341f6f 5147 mutex_unlock(&context->context_mgr_node_lock);
aed01731 5148 binder_inner_proc_lock(proc);
82f6ad88
TK
5149 /*
5150 * Make sure proc stays alive after we
5151 * remove all the threads
5152 */
5153 proc->tmp_ref++;
355b0502 5154
82f6ad88 5155 proc->is_dead = true;
355b0502
GKH
5156 threads = 0;
5157 active_transactions = 0;
5158 while ((n = rb_first(&proc->threads))) {
53413e7d
ME
5159 struct binder_thread *thread;
5160
5161 thread = rb_entry(n, struct binder_thread, rb_node);
aed01731 5162 binder_inner_proc_unlock(proc);
355b0502 5163 threads++;
82f6ad88 5164 active_transactions += binder_thread_release(proc, thread);
aed01731 5165 binder_inner_proc_lock(proc);
355b0502 5166 }
53413e7d 5167
355b0502
GKH
5168 nodes = 0;
5169 incoming_refs = 0;
5170 while ((n = rb_first(&proc->nodes))) {
53413e7d 5171 struct binder_node *node;
355b0502 5172
53413e7d 5173 node = rb_entry(n, struct binder_node, rb_node);
355b0502 5174 nodes++;
9607bf70
TK
5175 /*
5176 * take a temporary ref on the node before
5177 * calling binder_node_release() which will either
5178 * kfree() the node or call binder_put_node()
5179 */
ceeaf28f 5180 binder_inc_node_tmpref_ilocked(node);
355b0502 5181 rb_erase(&node->rb_node, &proc->nodes);
ceeaf28f 5182 binder_inner_proc_unlock(proc);
7eac155d 5183 incoming_refs = binder_node_release(node, incoming_refs);
ceeaf28f 5184 binder_inner_proc_lock(proc);
355b0502 5185 }
ceeaf28f 5186 binder_inner_proc_unlock(proc);
53413e7d 5187
355b0502 5188 outgoing_refs = 0;
5717118f 5189 binder_proc_lock(proc);
355b0502 5190 while ((n = rb_first(&proc->refs_by_desc))) {
53413e7d
ME
5191 struct binder_ref *ref;
5192
5193 ref = rb_entry(n, struct binder_ref, rb_node_desc);
355b0502 5194 outgoing_refs++;
5717118f
TK
5195 binder_cleanup_ref_olocked(ref);
5196 binder_proc_unlock(proc);
bc65c39a 5197 binder_free_ref(ref);
5717118f 5198 binder_proc_lock(proc);
355b0502 5199 }
5717118f 5200 binder_proc_unlock(proc);
53413e7d 5201
aad8732c
TK
5202 binder_release_work(proc, &proc->todo);
5203 binder_release_work(proc, &proc->delivered_death);
355b0502 5204
355b0502 5205 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
69c33bb1 5206 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
c07c933f 5207 __func__, proc->pid, threads, nodes, incoming_refs,
69c33bb1 5208 outgoing_refs, active_transactions);
355b0502 5209
82f6ad88 5210 binder_proc_dec_tmpref(proc);
355b0502
GKH
5211}
5212
5213static void binder_deferred_func(struct work_struct *work)
5214{
5215 struct binder_proc *proc;
5216 struct files_struct *files;
5217
5218 int defer;
01c8cbde 5219
355b0502 5220 do {
7eac155d
DW
5221 mutex_lock(&binder_deferred_lock);
5222 if (!hlist_empty(&binder_deferred_list)) {
5223 proc = hlist_entry(binder_deferred_list.first,
5224 struct binder_proc, deferred_work_node);
355b0502
GKH
5225 hlist_del_init(&proc->deferred_work_node);
5226 defer = proc->deferred_work;
5227 proc->deferred_work = 0;
5228 } else {
5229 proc = NULL;
5230 defer = 0;
5231 }
7eac155d 5232 mutex_unlock(&binder_deferred_lock);
355b0502
GKH
5233
5234 files = NULL;
5235 if (defer & BINDER_DEFERRED_PUT_FILES) {
5236 files = proc->files;
5237 if (files)
5238 proc->files = NULL;
5239 }
5240
5241 if (defer & BINDER_DEFERRED_FLUSH)
5242 binder_deferred_flush(proc);
5243
5244 if (defer & BINDER_DEFERRED_RELEASE)
5245 binder_deferred_release(proc); /* frees proc */
5246
355b0502
GKH
5247 if (files)
5248 put_files_struct(files);
5249 } while (proc);
5250}
7eac155d 5251static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
355b0502
GKH
5252
5253static void
5254binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
5255{
7eac155d 5256 mutex_lock(&binder_deferred_lock);
355b0502
GKH
5257 proc->deferred_work |= defer;
5258 if (hlist_unhashed(&proc->deferred_work_node)) {
5259 hlist_add_head(&proc->deferred_work_node,
7eac155d
DW
5260 &binder_deferred_list);
5261 queue_work(binder_deferred_workqueue, &binder_deferred_work);
355b0502 5262 }
7eac155d 5263 mutex_unlock(&binder_deferred_lock);
355b0502
GKH
5264}
5265
c2bdc4c4
TK
5266static void print_binder_transaction_ilocked(struct seq_file *m,
5267 struct binder_proc *proc,
5268 const char *prefix,
5269 struct binder_transaction *t)
5249f488 5270{
c2bdc4c4
TK
5271 struct binder_proc *to_proc;
5272 struct binder_buffer *buffer = t->buffer;
5273
82f6ad88 5274 spin_lock(&t->lock);
c2bdc4c4 5275 to_proc = t->to_proc;
5249f488 5276 seq_printf(m,
425bb8fc 5277 "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %d:%d r%d",
5249f488
AH
5278 prefix, t->debug_id, t,
5279 t->from ? t->from->proc->pid : 0,
5280 t->from ? t->from->pid : 0,
c2bdc4c4 5281 to_proc ? to_proc->pid : 0,
5249f488 5282 t->to_thread ? t->to_thread->pid : 0,
425bb8fc
MC
5283 t->code, t->flags, t->priority.sched_policy,
5284 t->priority.prio, t->need_reply);
82f6ad88
TK
5285 spin_unlock(&t->lock);
5286
c2bdc4c4
TK
5287 if (proc != to_proc) {
5288 /*
5289 * Can only safely deref buffer if we are holding the
5290 * correct proc inner lock for this node
5291 */
5292 seq_puts(m, "\n");
5293 return;
5294 }
5295
5296 if (buffer == NULL) {
5249f488
AH
5297 seq_puts(m, " buffer free\n");
5298 return;
355b0502 5299 }
c2bdc4c4
TK
5300 if (buffer->target_node)
5301 seq_printf(m, " node %d", buffer->target_node->debug_id);
3877fd57 5302 seq_printf(m, " size %zd:%zd data %p\n",
c2bdc4c4
TK
5303 buffer->data_size, buffer->offsets_size,
5304 buffer->data);
355b0502
GKH
5305}
5306
c2bdc4c4
TK
5307static void print_binder_work_ilocked(struct seq_file *m,
5308 struct binder_proc *proc,
5309 const char *prefix,
5310 const char *transaction_prefix,
5311 struct binder_work *w)
355b0502
GKH
5312{
5313 struct binder_node *node;
5314 struct binder_transaction *t;
5315
5316 switch (w->type) {
5317 case BINDER_WORK_TRANSACTION:
5318 t = container_of(w, struct binder_transaction, work);
c2bdc4c4
TK
5319 print_binder_transaction_ilocked(
5320 m, proc, transaction_prefix, t);
355b0502 5321 break;
795aa6bc
TK
5322 case BINDER_WORK_RETURN_ERROR: {
5323 struct binder_error *e = container_of(
5324 w, struct binder_error, work);
5325
5326 seq_printf(m, "%stransaction error: %u\n",
5327 prefix, e->cmd);
5328 } break;
355b0502 5329 case BINDER_WORK_TRANSACTION_COMPLETE:
5249f488 5330 seq_printf(m, "%stransaction complete\n", prefix);
355b0502
GKH
5331 break;
5332 case BINDER_WORK_NODE:
5333 node = container_of(w, struct binder_node, work);
9c0a1d02
AH
5334 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
5335 prefix, node->debug_id,
5336 (u64)node->ptr, (u64)node->cookie);
355b0502
GKH
5337 break;
5338 case BINDER_WORK_DEAD_BINDER:
5249f488 5339 seq_printf(m, "%shas dead binder\n", prefix);
355b0502
GKH
5340 break;
5341 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
5249f488 5342 seq_printf(m, "%shas cleared dead binder\n", prefix);
355b0502
GKH
5343 break;
5344 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
5249f488 5345 seq_printf(m, "%shas cleared death notification\n", prefix);
355b0502
GKH
5346 break;
5347 default:
5249f488 5348 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
355b0502
GKH
5349 break;
5350 }
355b0502
GKH
5351}
5352
aad8732c
TK
5353static void print_binder_thread_ilocked(struct seq_file *m,
5354 struct binder_thread *thread,
5355 int print_always)
355b0502
GKH
5356{
5357 struct binder_transaction *t;
5358 struct binder_work *w;
5249f488
AH
5359 size_t start_pos = m->count;
5360 size_t header_pos;
355b0502 5361
82f6ad88 5362 seq_printf(m, " thread %d: l %02x need_return %d tr %d\n",
85ab7b9c 5363 thread->pid, thread->looper,
82f6ad88
TK
5364 thread->looper_need_return,
5365 atomic_read(&thread->tmp_ref));
5249f488 5366 header_pos = m->count;
355b0502
GKH
5367 t = thread->transaction_stack;
5368 while (t) {
355b0502 5369 if (t->from == thread) {
c2bdc4c4
TK
5370 print_binder_transaction_ilocked(m, thread->proc,
5371 " outgoing transaction", t);
355b0502
GKH
5372 t = t->from_parent;
5373 } else if (t->to_thread == thread) {
c2bdc4c4 5374 print_binder_transaction_ilocked(m, thread->proc,
5249f488 5375 " incoming transaction", t);
355b0502
GKH
5376 t = t->to_parent;
5377 } else {
c2bdc4c4
TK
5378 print_binder_transaction_ilocked(m, thread->proc,
5379 " bad transaction", t);
355b0502
GKH
5380 t = NULL;
5381 }
5382 }
5383 list_for_each_entry(w, &thread->todo, entry) {
c2bdc4c4 5384 print_binder_work_ilocked(m, thread->proc, " ",
aad8732c 5385 " pending transaction", w);
355b0502 5386 }
5249f488
AH
5387 if (!print_always && m->count == header_pos)
5388 m->count = start_pos;
355b0502
GKH
5389}
5390
ceeaf28f
TK
5391static void print_binder_node_nilocked(struct seq_file *m,
5392 struct binder_node *node)
355b0502
GKH
5393{
5394 struct binder_ref *ref;
355b0502
GKH
5395 struct binder_work *w;
5396 int count;
5397
5398 count = 0;
b67bfe0d 5399 hlist_for_each_entry(ref, &node->refs, node_entry)
355b0502
GKH
5400 count++;
5401
fbb541d2 5402 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 5403 node->debug_id, (u64)node->ptr, (u64)node->cookie,
fbb541d2 5404 node->sched_policy, node->min_priority,
5249f488
AH
5405 node->has_strong_ref, node->has_weak_ref,
5406 node->local_strong_refs, node->local_weak_refs,
9607bf70 5407 node->internal_strong_refs, count, node->tmp_refs);
355b0502 5408 if (count) {
5249f488 5409 seq_puts(m, " proc");
b67bfe0d 5410 hlist_for_each_entry(ref, &node->refs, node_entry)
5249f488 5411 seq_printf(m, " %d", ref->proc->pid);
355b0502 5412 }
5249f488 5413 seq_puts(m, "\n");
aad8732c 5414 if (node->proc) {
aad8732c 5415 list_for_each_entry(w, &node->async_todo, entry)
c2bdc4c4 5416 print_binder_work_ilocked(m, node->proc, " ",
aad8732c 5417 " pending async transaction", w);
aad8732c 5418 }
355b0502
GKH
5419}
5420
5717118f
TK
5421static void print_binder_ref_olocked(struct seq_file *m,
5422 struct binder_ref *ref)
355b0502 5423{
25de59a5 5424 binder_node_lock(ref->node);
bc65c39a
TK
5425 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
5426 ref->data.debug_id, ref->data.desc,
5427 ref->node->proc ? "" : "dead ",
5428 ref->node->debug_id, ref->data.strong,
5429 ref->data.weak, ref->death);
25de59a5 5430 binder_node_unlock(ref->node);
355b0502
GKH
5431}
5432
5249f488
AH
5433static void print_binder_proc(struct seq_file *m,
5434 struct binder_proc *proc, int print_all)
355b0502
GKH
5435{
5436 struct binder_work *w;
5437 struct rb_node *n;
5249f488
AH
5438 size_t start_pos = m->count;
5439 size_t header_pos;
ceeaf28f 5440 struct binder_node *last_node = NULL;
5249f488
AH
5441
5442 seq_printf(m, "proc %d\n", proc->pid);
a64af2cf 5443 seq_printf(m, "context %s\n", proc->context->name);
5249f488
AH
5444 header_pos = m->count;
5445
aad8732c 5446 binder_inner_proc_lock(proc);
5249f488 5447 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
aad8732c 5448 print_binder_thread_ilocked(m, rb_entry(n, struct binder_thread,
5249f488 5449 rb_node), print_all);
ceeaf28f 5450
5249f488 5451 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
355b0502
GKH
5452 struct binder_node *node = rb_entry(n, struct binder_node,
5453 rb_node);
ceeaf28f
TK
5454 /*
5455 * take a temporary reference on the node so it
5456 * survives and isn't removed from the tree
5457 * while we print it.
5458 */
5459 binder_inc_node_tmpref_ilocked(node);
5460 /* Need to drop inner lock to take node lock */
5461 binder_inner_proc_unlock(proc);
5462 if (last_node)
5463 binder_put_node(last_node);
5464 binder_node_inner_lock(node);
5465 print_binder_node_nilocked(m, node);
5466 binder_node_inner_unlock(node);
5467 last_node = node;
5468 binder_inner_proc_lock(proc);
355b0502 5469 }
ceeaf28f
TK
5470 binder_inner_proc_unlock(proc);
5471 if (last_node)
5472 binder_put_node(last_node);
5473
355b0502 5474 if (print_all) {
5717118f 5475 binder_proc_lock(proc);
355b0502 5476 for (n = rb_first(&proc->refs_by_desc);
5249f488 5477 n != NULL;
355b0502 5478 n = rb_next(n))
5717118f
TK
5479 print_binder_ref_olocked(m, rb_entry(n,
5480 struct binder_ref,
5481 rb_node_desc));
5482 binder_proc_unlock(proc);
355b0502 5483 }
69c33bb1 5484 binder_alloc_print_allocated(m, &proc->alloc);
aad8732c 5485 binder_inner_proc_lock(proc);
5249f488 5486 list_for_each_entry(w, &proc->todo, entry)
c2bdc4c4
TK
5487 print_binder_work_ilocked(m, proc, " ",
5488 " pending transaction", w);
355b0502 5489 list_for_each_entry(w, &proc->delivered_death, entry) {
5249f488 5490 seq_puts(m, " has delivered dead binder\n");
355b0502
GKH
5491 break;
5492 }
aad8732c 5493 binder_inner_proc_unlock(proc);
5249f488
AH
5494 if (!print_all && m->count == header_pos)
5495 m->count = start_pos;
355b0502
GKH
5496}
5497
167bccbd 5498static const char * const binder_return_strings[] = {
355b0502
GKH
5499 "BR_ERROR",
5500 "BR_OK",
5501 "BR_TRANSACTION",
5502 "BR_REPLY",
5503 "BR_ACQUIRE_RESULT",
5504 "BR_DEAD_REPLY",
5505 "BR_TRANSACTION_COMPLETE",
5506 "BR_INCREFS",
5507 "BR_ACQUIRE",
5508 "BR_RELEASE",
5509 "BR_DECREFS",
5510 "BR_ATTEMPT_ACQUIRE",
5511 "BR_NOOP",
5512 "BR_SPAWN_LOOPER",
5513 "BR_FINISHED",
5514 "BR_DEAD_BINDER",
5515 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
5516 "BR_FAILED_REPLY"
5517};
5518
167bccbd 5519static const char * const binder_command_strings[] = {
355b0502
GKH
5520 "BC_TRANSACTION",
5521 "BC_REPLY",
5522 "BC_ACQUIRE_RESULT",
5523 "BC_FREE_BUFFER",
5524 "BC_INCREFS",
5525 "BC_ACQUIRE",
5526 "BC_RELEASE",
5527 "BC_DECREFS",
5528 "BC_INCREFS_DONE",
5529 "BC_ACQUIRE_DONE",
5530 "BC_ATTEMPT_ACQUIRE",
5531 "BC_REGISTER_LOOPER",
5532 "BC_ENTER_LOOPER",
5533 "BC_EXIT_LOOPER",
5534 "BC_REQUEST_DEATH_NOTIFICATION",
5535 "BC_CLEAR_DEATH_NOTIFICATION",
e884979e
MC
5536 "BC_DEAD_BINDER_DONE",
5537 "BC_TRANSACTION_SG",
5538 "BC_REPLY_SG",
355b0502
GKH
5539};
5540
167bccbd 5541static const char * const binder_objstat_strings[] = {
355b0502
GKH
5542 "proc",
5543 "thread",
5544 "node",
5545 "ref",
5546 "death",
5547 "transaction",
5548 "transaction_complete"
5549};
5550
5249f488 5551static void print_binder_stats(struct seq_file *m, const char *prefix,
7eac155d 5552 struct binder_stats *stats)
355b0502
GKH
5553{
5554 int i;
5555
5556 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
5249f488 5557 ARRAY_SIZE(binder_command_strings));
355b0502 5558 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
340b5f86
BJS
5559 int temp = atomic_read(&stats->bc[i]);
5560
5561 if (temp)
5249f488 5562 seq_printf(m, "%s%s: %d\n", prefix,
340b5f86 5563 binder_command_strings[i], temp);
355b0502
GKH
5564 }
5565
5566 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
5249f488 5567 ARRAY_SIZE(binder_return_strings));
355b0502 5568 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
340b5f86
BJS
5569 int temp = atomic_read(&stats->br[i]);
5570
5571 if (temp)
5249f488 5572 seq_printf(m, "%s%s: %d\n", prefix,
340b5f86 5573 binder_return_strings[i], temp);
355b0502
GKH
5574 }
5575
7eac155d 5576 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
5249f488 5577 ARRAY_SIZE(binder_objstat_strings));
7eac155d
DW
5578 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
5579 ARRAY_SIZE(stats->obj_deleted));
5580 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
340b5f86
BJS
5581 int created = atomic_read(&stats->obj_created[i]);
5582 int deleted = atomic_read(&stats->obj_deleted[i]);
5583
5584 if (created || deleted)
5585 seq_printf(m, "%s%s: active %d total %d\n",
5586 prefix,
7eac155d 5587 binder_objstat_strings[i],
340b5f86
BJS
5588 created - deleted,
5589 created);
355b0502 5590 }
69c33bb1
TK
5591}
5592
5249f488
AH
5593static void print_binder_proc_stats(struct seq_file *m,
5594 struct binder_proc *proc)
355b0502
GKH
5595{
5596 struct binder_work *w;
02545935 5597 struct binder_thread *thread;
355b0502 5598 struct rb_node *n;
02545935 5599 int count, strong, weak, ready_threads;
aed01731
TK
5600 size_t free_async_space =
5601 binder_alloc_get_free_async_space(&proc->alloc);
355b0502 5602
5249f488 5603 seq_printf(m, "proc %d\n", proc->pid);
a64af2cf 5604 seq_printf(m, "context %s\n", proc->context->name);
355b0502 5605 count = 0;
02545935 5606 ready_threads = 0;
aed01731 5607 binder_inner_proc_lock(proc);
355b0502
GKH
5608 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
5609 count++;
02545935
MC
5610
5611 list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node)
5612 ready_threads++;
5613
5249f488
AH
5614 seq_printf(m, " threads: %d\n", count);
5615 seq_printf(m, " requested threads: %d+%d/%d\n"
355b0502
GKH
5616 " ready threads %d\n"
5617 " free async space %zd\n", proc->requested_threads,
5618 proc->requested_threads_started, proc->max_threads,
02545935 5619 ready_threads,
aed01731 5620 free_async_space);
355b0502
GKH
5621 count = 0;
5622 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
5623 count++;
ceeaf28f 5624 binder_inner_proc_unlock(proc);
5249f488 5625 seq_printf(m, " nodes: %d\n", count);
355b0502
GKH
5626 count = 0;
5627 strong = 0;
5628 weak = 0;
5717118f 5629 binder_proc_lock(proc);
355b0502
GKH
5630 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
5631 struct binder_ref *ref = rb_entry(n, struct binder_ref,
5632 rb_node_desc);
5633 count++;
bc65c39a
TK
5634 strong += ref->data.strong;
5635 weak += ref->data.weak;
355b0502 5636 }
5717118f 5637 binder_proc_unlock(proc);
5249f488 5638 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
355b0502 5639
69c33bb1 5640 count = binder_alloc_get_allocated_count(&proc->alloc);
5249f488 5641 seq_printf(m, " buffers: %d\n", count);
355b0502
GKH
5642
5643 count = 0;
aad8732c 5644 binder_inner_proc_lock(proc);
355b0502 5645 list_for_each_entry(w, &proc->todo, entry) {
aad8732c 5646 if (w->type == BINDER_WORK_TRANSACTION)
355b0502 5647 count++;
355b0502 5648 }
aad8732c 5649 binder_inner_proc_unlock(proc);
5249f488 5650 seq_printf(m, " pending transactions: %d\n", count);
355b0502 5651
7eac155d 5652 print_binder_stats(m, " ", &proc->stats);
355b0502
GKH
5653}
5654
5655
5249f488 5656static int binder_state_show(struct seq_file *m, void *unused)
355b0502
GKH
5657{
5658 struct binder_proc *proc;
355b0502 5659 struct binder_node *node;
25de59a5 5660 struct binder_node *last_node = NULL;
355b0502 5661
7eac155d 5662 seq_puts(m, "binder state:\n");
355b0502 5663
76341f6f 5664 spin_lock(&binder_dead_nodes_lock);
7eac155d
DW
5665 if (!hlist_empty(&binder_dead_nodes))
5666 seq_puts(m, "dead nodes:\n");
25de59a5
TK
5667 hlist_for_each_entry(node, &binder_dead_nodes, dead_node) {
5668 /*
5669 * take a temporary reference on the node so it
5670 * survives and isn't removed from the list
5671 * while we print it.
5672 */
5673 node->tmp_refs++;
5674 spin_unlock(&binder_dead_nodes_lock);
5675 if (last_node)
5676 binder_put_node(last_node);
5677 binder_node_lock(node);
ceeaf28f 5678 print_binder_node_nilocked(m, node);
25de59a5
TK
5679 binder_node_unlock(node);
5680 last_node = node;
5681 spin_lock(&binder_dead_nodes_lock);
5682 }
76341f6f 5683 spin_unlock(&binder_dead_nodes_lock);
25de59a5
TK
5684 if (last_node)
5685 binder_put_node(last_node);
9f28e23f 5686
76341f6f 5687 mutex_lock(&binder_procs_lock);
7eac155d
DW
5688 hlist_for_each_entry(proc, &binder_procs, proc_node)
5689 print_binder_proc(m, proc, 1);
76341f6f 5690 mutex_unlock(&binder_procs_lock);
9f71b6a6 5691
5249f488 5692 return 0;
355b0502
GKH
5693}
5694
5249f488 5695static int binder_stats_show(struct seq_file *m, void *unused)
355b0502
GKH
5696{
5697 struct binder_proc *proc;
355b0502 5698
5249f488 5699 seq_puts(m, "binder stats:\n");
355b0502 5700
7eac155d 5701 print_binder_stats(m, "", &binder_stats);
355b0502 5702
76341f6f 5703 mutex_lock(&binder_procs_lock);
7eac155d
DW
5704 hlist_for_each_entry(proc, &binder_procs, proc_node)
5705 print_binder_proc_stats(m, proc);
76341f6f 5706 mutex_unlock(&binder_procs_lock);
9f71b6a6 5707
5249f488 5708 return 0;
355b0502
GKH
5709}
5710
5249f488 5711static int binder_transactions_show(struct seq_file *m, void *unused)
355b0502
GKH
5712{
5713 struct binder_proc *proc;
355b0502 5714
7eac155d 5715 seq_puts(m, "binder transactions:\n");
76341f6f 5716 mutex_lock(&binder_procs_lock);
7eac155d
DW
5717 hlist_for_each_entry(proc, &binder_procs, proc_node)
5718 print_binder_proc(m, proc, 0);
76341f6f 5719 mutex_unlock(&binder_procs_lock);
9f71b6a6 5720
5249f488 5721 return 0;
355b0502
GKH
5722}
5723
5249f488 5724static int binder_proc_show(struct seq_file *m, void *unused)
355b0502 5725{
1bfa8186 5726 struct binder_proc *itr;
a64af2cf 5727 int pid = (unsigned long)m->private;
355b0502 5728
76341f6f 5729 mutex_lock(&binder_procs_lock);
7eac155d
DW
5730 hlist_for_each_entry(itr, &binder_procs, proc_node) {
5731 if (itr->pid == pid) {
5732 seq_puts(m, "binder proc state:\n");
5733 print_binder_proc(m, itr, 1);
1bfa8186
RA
5734 }
5735 }
76341f6f
TK
5736 mutex_unlock(&binder_procs_lock);
5737
5249f488 5738 return 0;
355b0502
GKH
5739}
5740
5249f488 5741static void print_binder_transaction_log_entry(struct seq_file *m,
355b0502
GKH
5742 struct binder_transaction_log_entry *e)
5743{
7d94b2c7
TK
5744 int debug_id = READ_ONCE(e->debug_id_done);
5745 /*
5746 * read barrier to guarantee debug_id_done read before
5747 * we print the log values
5748 */
5749 smp_rmb();
5249f488 5750 seq_printf(m,
7d94b2c7 5751 "%d: %s from %d:%d to %d:%d context %s node %d handle %d size %d:%d ret %d/%d l=%d",
5249f488
AH
5752 e->debug_id, (e->call_type == 2) ? "reply" :
5753 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
a64af2cf 5754 e->from_thread, e->to_proc, e->to_thread, e->context_name,
dfc99565
TK
5755 e->to_node, e->target_handle, e->data_size, e->offsets_size,
5756 e->return_error, e->return_error_param,
5757 e->return_error_line);
7d94b2c7
TK
5758 /*
5759 * read-barrier to guarantee read of debug_id_done after
5760 * done printing the fields of the entry
5761 */
5762 smp_rmb();
5763 seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ?
5764 "\n" : " (incomplete)\n");
355b0502
GKH
5765}
5766
7eac155d 5767static int binder_transaction_log_show(struct seq_file *m, void *unused)
355b0502 5768{
7eac155d 5769 struct binder_transaction_log *log = m->private;
7d94b2c7
TK
5770 unsigned int log_cur = atomic_read(&log->cur);
5771 unsigned int count;
5772 unsigned int cur;
355b0502 5773 int i;
7eac155d 5774
7d94b2c7
TK
5775 count = log_cur + 1;
5776 cur = count < ARRAY_SIZE(log->entry) && !log->full ?
5777 0 : count % ARRAY_SIZE(log->entry);
5778 if (count > ARRAY_SIZE(log->entry) || log->full)
5779 count = ARRAY_SIZE(log->entry);
5780 for (i = 0; i < count; i++) {
5781 unsigned int index = cur++ % ARRAY_SIZE(log->entry);
5782
5783 print_binder_transaction_log_entry(m, &log->entry[index]);
355b0502 5784 }
5249f488 5785 return 0;
355b0502
GKH
5786}
5787
5788static const struct file_operations binder_fops = {
5789 .owner = THIS_MODULE,
5790 .poll = binder_poll,
5791 .unlocked_ioctl = binder_ioctl,
9c0a1d02 5792 .compat_ioctl = binder_ioctl,
355b0502
GKH
5793 .mmap = binder_mmap,
5794 .open = binder_open,
5795 .flush = binder_flush,
5796 .release = binder_release,
5797};
5798
5249f488
AH
5799BINDER_DEBUG_ENTRY(state);
5800BINDER_DEBUG_ENTRY(stats);
5801BINDER_DEBUG_ENTRY(transactions);
5802BINDER_DEBUG_ENTRY(transaction_log);
5803
bfd99b42
MC
5804static int __init init_binder_device(const char *name)
5805{
5806 int ret;
5807 struct binder_device *binder_device;
5808
5809 binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
5810 if (!binder_device)
5811 return -ENOMEM;
5812
5813 binder_device->miscdev.fops = &binder_fops;
5814 binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
5815 binder_device->miscdev.name = name;
5816
7eac155d
DW
5817 binder_device->context.binder_context_mgr_uid = INVALID_UID;
5818 binder_device->context.name = name;
76341f6f 5819 mutex_init(&binder_device->context.context_mgr_node_lock);
bfd99b42
MC
5820
5821 ret = misc_register(&binder_device->miscdev);
5822 if (ret < 0) {
7eac155d
DW
5823 kfree(binder_device);
5824 return ret;
bfd99b42
MC
5825 }
5826
5827 hlist_add_head(&binder_device->hlist, &binder_devices);
5828
5829 return ret;
5830}
5831
355b0502
GKH
5832static int __init binder_init(void)
5833{
7eac155d 5834 int ret;
bfd99b42
MC
5835 char *device_name, *device_names;
5836 struct binder_device *device;
5837 struct hlist_node *tmp;
355b0502 5838
7d94b2c7
TK
5839 atomic_set(&binder_transaction_log.cur, ~0U);
5840 atomic_set(&binder_transaction_log_failed.cur, ~0U);
7eac155d
DW
5841 binder_deferred_workqueue = create_singlethread_workqueue("binder");
5842 if (!binder_deferred_workqueue)
3c762a49
AH
5843 return -ENOMEM;
5844
16b66554
AH
5845 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
5846 if (binder_debugfs_dir_entry_root)
5847 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
5848 binder_debugfs_dir_entry_root);
bfd99b42 5849
16b66554
AH
5850 if (binder_debugfs_dir_entry_root) {
5851 debugfs_create_file("state",
5852 S_IRUGO,
5853 binder_debugfs_dir_entry_root,
5854 NULL,
5855 &binder_state_fops);
5856 debugfs_create_file("stats",
5857 S_IRUGO,
5858 binder_debugfs_dir_entry_root,
5859 NULL,
5860 &binder_stats_fops);
5861 debugfs_create_file("transactions",
5862 S_IRUGO,
5863 binder_debugfs_dir_entry_root,
5864 NULL,
5865 &binder_transactions_fops);
5866 debugfs_create_file("transaction_log",
5867 S_IRUGO,
5868 binder_debugfs_dir_entry_root,
7eac155d 5869 &binder_transaction_log,
16b66554
AH
5870 &binder_transaction_log_fops);
5871 debugfs_create_file("failed_transaction_log",
5872 S_IRUGO,
5873 binder_debugfs_dir_entry_root,
7eac155d
DW
5874 &binder_transaction_log_failed,
5875 &binder_transaction_log_fops);
5876 }
5877
5878 /*
5879 * Copy the module_parameter string, because we don't want to
5880 * tokenize it in-place.
5881 */
5882 device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL);
5883 if (!device_names) {
5884 ret = -ENOMEM;
5885 goto err_alloc_device_names_failed;
5886 }
5887 strcpy(device_names, binder_devices_param);
5888
5889 while ((device_name = strsep(&device_names, ","))) {
5890 ret = init_binder_device(device_name);
5891 if (ret)
5892 goto err_init_binder_device_failed;
bfd99b42
MC
5893 }
5894
5895 return ret;
5896
5897err_init_binder_device_failed:
5898 hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
5899 misc_deregister(&device->miscdev);
5900 hlist_del(&device->hlist);
7eac155d 5901 kfree(device);
bfd99b42 5902 }
7eac155d
DW
5903err_alloc_device_names_failed:
5904 debugfs_remove_recursive(binder_debugfs_dir_entry_root);
5905
5906 destroy_workqueue(binder_deferred_workqueue);
bfd99b42 5907
355b0502
GKH
5908 return ret;
5909}
5910
5911device_initcall(binder_init);
5912
975a1ac9
AH
5913#define CREATE_TRACE_POINTS
5914#include "binder_trace.h"
5915
355b0502 5916MODULE_LICENSE("GPL v2");