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