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