ANDROID: binder: don't check prio permissions on restore.
[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
1024 BUG_ON(!spin_is_locked(&proc->inner_lock));
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{
1055 BUG_ON(!spin_is_locked(&proc->inner_lock));
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
ceeaf28f
TK
1234 BUG_ON(!spin_is_locked(&proc->inner_lock));
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
ceeaf28f 1280 BUG_ON(!spin_is_locked(&proc->inner_lock));
355b0502 1281 while (*p) {
ceeaf28f 1282
355b0502
GKH
1283 parent = *p;
1284 node = rb_entry(parent, struct binder_node, rb_node);
1285
1286 if (ptr < node->ptr)
1287 p = &(*p)->rb_left;
1288 else if (ptr > node->ptr)
1289 p = &(*p)->rb_right;
ceeaf28f
TK
1290 else {
1291 /*
1292 * A matching node is already in
1293 * the rb tree. Abandon the init
1294 * and return it.
1295 */
1296 binder_inc_node_tmpref_ilocked(node);
1297 return node;
1298 }
355b0502 1299 }
ceeaf28f 1300 node = new_node;
355b0502 1301 binder_stats_created(BINDER_STAT_NODE);
9607bf70 1302 node->tmp_refs++;
355b0502
GKH
1303 rb_link_node(&node->rb_node, parent, p);
1304 rb_insert_color(&node->rb_node, &proc->nodes);
64e01e83 1305 node->debug_id = atomic_inc_return(&binder_last_id);
355b0502
GKH
1306 node->proc = proc;
1307 node->ptr = ptr;
1308 node->cookie = cookie;
1309 node->work.type = BINDER_WORK_NODE;
fbb541d2
MC
1310 priority = flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
1311 node->sched_policy = (flags & FLAT_BINDER_FLAG_PRIORITY_MASK) >>
1312 FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT;
1313 node->min_priority = to_kernel_prio(node->sched_policy, priority);
25de59a5 1314 node->accept_fds = !!(flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
21dfe3eb 1315 node->inherit_rt = !!(flags & FLAT_BINDER_FLAG_INHERIT_RT);
751c6a8e 1316 spin_lock_init(&node->lock);
355b0502
GKH
1317 INIT_LIST_HEAD(&node->work.entry);
1318 INIT_LIST_HEAD(&node->async_todo);
1319 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
9c0a1d02 1320 "%d:%d node %d u%016llx c%016llx created\n",
355b0502 1321 proc->pid, current->pid, node->debug_id,
9c0a1d02 1322 (u64)node->ptr, (u64)node->cookie);
ceeaf28f
TK
1323
1324 return node;
1325}
1326
1327static struct binder_node *binder_new_node(struct binder_proc *proc,
1328 struct flat_binder_object *fp)
1329{
1330 struct binder_node *node;
1331 struct binder_node *new_node = kzalloc(sizeof(*node), GFP_KERNEL);
1332
1333 if (!new_node)
1334 return NULL;
1335 binder_inner_proc_lock(proc);
1336 node = binder_init_node_ilocked(proc, new_node, fp);
1337 binder_inner_proc_unlock(proc);
1338 if (node != new_node)
1339 /*
1340 * The node was already added by another thread
1341 */
1342 kfree(new_node);
1343
355b0502
GKH
1344 return node;
1345}
1346
ccca76b6 1347static void binder_free_node(struct binder_node *node)
355b0502 1348{
ccca76b6
TK
1349 kfree(node);
1350 binder_stats_deleted(BINDER_STAT_NODE);
1351}
1352
25de59a5
TK
1353static int binder_inc_node_nilocked(struct binder_node *node, int strong,
1354 int internal,
1355 struct list_head *target_list)
ccca76b6 1356{
25de59a5
TK
1357 struct binder_proc *proc = node->proc;
1358
1359 BUG_ON(!spin_is_locked(&node->lock));
1360 if (proc)
1361 BUG_ON(!spin_is_locked(&proc->inner_lock));
355b0502
GKH
1362 if (strong) {
1363 if (internal) {
1364 if (target_list == NULL &&
1365 node->internal_strong_refs == 0 &&
913926d0
MC
1366 !(node->proc &&
1367 node == node->proc->context->
1368 binder_context_mgr_node &&
1369 node->has_strong_ref)) {
56b468fc
AS
1370 pr_err("invalid inc strong node for %d\n",
1371 node->debug_id);
355b0502
GKH
1372 return -EINVAL;
1373 }
1374 node->internal_strong_refs++;
1375 } else
1376 node->local_strong_refs++;
1377 if (!node->has_strong_ref && target_list) {
aad8732c
TK
1378 binder_dequeue_work_ilocked(&node->work);
1379 binder_enqueue_work_ilocked(&node->work, target_list);
355b0502
GKH
1380 }
1381 } else {
1382 if (!internal)
1383 node->local_weak_refs++;
1384 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
1385 if (target_list == NULL) {
56b468fc
AS
1386 pr_err("invalid inc weak node for %d\n",
1387 node->debug_id);
355b0502
GKH
1388 return -EINVAL;
1389 }
aad8732c 1390 binder_enqueue_work_ilocked(&node->work, target_list);
355b0502
GKH
1391 }
1392 }
1393 return 0;
1394}
1395
ccca76b6
TK
1396static int binder_inc_node(struct binder_node *node, int strong, int internal,
1397 struct list_head *target_list)
1398{
1399 int ret;
1400
25de59a5
TK
1401 binder_node_inner_lock(node);
1402 ret = binder_inc_node_nilocked(node, strong, internal, target_list);
1403 binder_node_inner_unlock(node);
ccca76b6
TK
1404
1405 return ret;
1406}
1407
25de59a5
TK
1408static bool binder_dec_node_nilocked(struct binder_node *node,
1409 int strong, int internal)
355b0502 1410{
ccca76b6
TK
1411 struct binder_proc *proc = node->proc;
1412
25de59a5 1413 BUG_ON(!spin_is_locked(&node->lock));
ccca76b6
TK
1414 if (proc)
1415 BUG_ON(!spin_is_locked(&proc->inner_lock));
355b0502
GKH
1416 if (strong) {
1417 if (internal)
1418 node->internal_strong_refs--;
1419 else
1420 node->local_strong_refs--;
1421 if (node->local_strong_refs || node->internal_strong_refs)
ccca76b6 1422 return false;
355b0502
GKH
1423 } else {
1424 if (!internal)
1425 node->local_weak_refs--;
9607bf70
TK
1426 if (node->local_weak_refs || node->tmp_refs ||
1427 !hlist_empty(&node->refs))
ccca76b6 1428 return false;
355b0502 1429 }
ccca76b6
TK
1430
1431 if (proc && (node->has_strong_ref || node->has_weak_ref)) {
355b0502 1432 if (list_empty(&node->work.entry)) {
aad8732c 1433 binder_enqueue_work_ilocked(&node->work, &proc->todo);
1200efe9 1434 binder_wakeup_proc_ilocked(proc);
355b0502
GKH
1435 }
1436 } else {
1437 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
9607bf70 1438 !node->local_weak_refs && !node->tmp_refs) {
ccca76b6 1439 if (proc) {
aad8732c
TK
1440 binder_dequeue_work_ilocked(&node->work);
1441 rb_erase(&node->rb_node, &proc->nodes);
355b0502 1442 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
56b468fc 1443 "refless node %d deleted\n",
355b0502
GKH
1444 node->debug_id);
1445 } else {
aad8732c 1446 BUG_ON(!list_empty(&node->work.entry));
76341f6f 1447 spin_lock(&binder_dead_nodes_lock);
ccca76b6
TK
1448 /*
1449 * tmp_refs could have changed so
1450 * check it again
1451 */
1452 if (node->tmp_refs) {
1453 spin_unlock(&binder_dead_nodes_lock);
1454 return false;
1455 }
355b0502 1456 hlist_del(&node->dead_node);
76341f6f 1457 spin_unlock(&binder_dead_nodes_lock);
355b0502 1458 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
56b468fc 1459 "dead node %d deleted\n",
355b0502
GKH
1460 node->debug_id);
1461 }
ccca76b6 1462 return true;
355b0502
GKH
1463 }
1464 }
ccca76b6
TK
1465 return false;
1466}
355b0502 1467
ccca76b6
TK
1468static void binder_dec_node(struct binder_node *node, int strong, int internal)
1469{
1470 bool free_node;
1471
25de59a5
TK
1472 binder_node_inner_lock(node);
1473 free_node = binder_dec_node_nilocked(node, strong, internal);
1474 binder_node_inner_unlock(node);
ccca76b6
TK
1475 if (free_node)
1476 binder_free_node(node);
1477}
1478
1479static void binder_inc_node_tmpref_ilocked(struct binder_node *node)
1480{
1481 /*
1482 * No call to binder_inc_node() is needed since we
1483 * don't need to inform userspace of any changes to
1484 * tmp_refs
1485 */
1486 node->tmp_refs++;
355b0502
GKH
1487}
1488
9607bf70
TK
1489/**
1490 * binder_inc_node_tmpref() - take a temporary reference on node
1491 * @node: node to reference
1492 *
1493 * Take reference on node to prevent the node from being freed
ccca76b6
TK
1494 * while referenced only by a local variable. The inner lock is
1495 * needed to serialize with the node work on the queue (which
1496 * isn't needed after the node is dead). If the node is dead
1497 * (node->proc is NULL), use binder_dead_nodes_lock to protect
1498 * node->tmp_refs against dead-node-only cases where the node
1499 * lock cannot be acquired (eg traversing the dead node list to
1500 * print nodes)
9607bf70
TK
1501 */
1502static void binder_inc_node_tmpref(struct binder_node *node)
1503{
25de59a5 1504 binder_node_lock(node);
ccca76b6
TK
1505 if (node->proc)
1506 binder_inner_proc_lock(node->proc);
1507 else
1508 spin_lock(&binder_dead_nodes_lock);
1509 binder_inc_node_tmpref_ilocked(node);
1510 if (node->proc)
1511 binder_inner_proc_unlock(node->proc);
1512 else
1513 spin_unlock(&binder_dead_nodes_lock);
25de59a5 1514 binder_node_unlock(node);
9607bf70
TK
1515}
1516
1517/**
1518 * binder_dec_node_tmpref() - remove a temporary reference on node
1519 * @node: node to reference
1520 *
1521 * Release temporary reference on node taken via binder_inc_node_tmpref()
1522 */
1523static void binder_dec_node_tmpref(struct binder_node *node)
1524{
ccca76b6
TK
1525 bool free_node;
1526
25de59a5
TK
1527 binder_node_inner_lock(node);
1528 if (!node->proc)
ccca76b6 1529 spin_lock(&binder_dead_nodes_lock);
9607bf70
TK
1530 node->tmp_refs--;
1531 BUG_ON(node->tmp_refs < 0);
ccca76b6
TK
1532 if (!node->proc)
1533 spin_unlock(&binder_dead_nodes_lock);
9607bf70
TK
1534 /*
1535 * Call binder_dec_node() to check if all refcounts are 0
1536 * and cleanup is needed. Calling with strong=0 and internal=1
1537 * causes no actual reference to be released in binder_dec_node().
1538 * If that changes, a change is needed here too.
1539 */
25de59a5
TK
1540 free_node = binder_dec_node_nilocked(node, 0, 1);
1541 binder_node_inner_unlock(node);
ccca76b6
TK
1542 if (free_node)
1543 binder_free_node(node);
9607bf70
TK
1544}
1545
1546static void binder_put_node(struct binder_node *node)
1547{
1548 binder_dec_node_tmpref(node);
1549}
355b0502 1550
5717118f
TK
1551static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc,
1552 u32 desc, bool need_strong_ref)
355b0502
GKH
1553{
1554 struct rb_node *n = proc->refs_by_desc.rb_node;
1555 struct binder_ref *ref;
1556
1557 while (n) {
1558 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1559
bc65c39a 1560 if (desc < ref->data.desc) {
355b0502 1561 n = n->rb_left;
bc65c39a 1562 } else if (desc > ref->data.desc) {
355b0502 1563 n = n->rb_right;
bc65c39a 1564 } else if (need_strong_ref && !ref->data.strong) {
d78eaaf9
AH
1565 binder_user_error("tried to use weak ref as strong ref\n");
1566 return NULL;
1567 } else {
355b0502 1568 return ref;
d78eaaf9 1569 }
355b0502
GKH
1570 }
1571 return NULL;
1572}
1573
bc65c39a 1574/**
5717118f 1575 * binder_get_ref_for_node_olocked() - get the ref associated with given node
bc65c39a
TK
1576 * @proc: binder_proc that owns the ref
1577 * @node: binder_node of target
1578 * @new_ref: newly allocated binder_ref to be initialized or %NULL
1579 *
1580 * Look up the ref for the given node and return it if it exists
1581 *
1582 * If it doesn't exist and the caller provides a newly allocated
1583 * ref, initialize the fields of the newly allocated ref and insert
1584 * into the given proc rb_trees and node refs list.
1585 *
1586 * Return: the ref for node. It is possible that another thread
1587 * allocated/initialized the ref first in which case the
1588 * returned ref would be different than the passed-in
1589 * new_ref. new_ref must be kfree'd by the caller in
1590 * this case.
1591 */
5717118f
TK
1592static struct binder_ref *binder_get_ref_for_node_olocked(
1593 struct binder_proc *proc,
1594 struct binder_node *node,
1595 struct binder_ref *new_ref)
355b0502 1596{
bc65c39a 1597 struct binder_context *context = proc->context;
355b0502
GKH
1598 struct rb_node **p = &proc->refs_by_node.rb_node;
1599 struct rb_node *parent = NULL;
bc65c39a
TK
1600 struct binder_ref *ref;
1601 struct rb_node *n;
355b0502
GKH
1602
1603 while (*p) {
1604 parent = *p;
1605 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1606
1607 if (node < ref->node)
1608 p = &(*p)->rb_left;
1609 else if (node > ref->node)
1610 p = &(*p)->rb_right;
1611 else
1612 return ref;
1613 }
bc65c39a 1614 if (!new_ref)
355b0502 1615 return NULL;
bc65c39a 1616
355b0502 1617 binder_stats_created(BINDER_STAT_REF);
bc65c39a 1618 new_ref->data.debug_id = atomic_inc_return(&binder_last_id);
355b0502
GKH
1619 new_ref->proc = proc;
1620 new_ref->node = node;
1621 rb_link_node(&new_ref->rb_node_node, parent, p);
1622 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1623
bc65c39a 1624 new_ref->data.desc = (node == context->binder_context_mgr_node) ? 0 : 1;
355b0502
GKH
1625 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1626 ref = rb_entry(n, struct binder_ref, rb_node_desc);
bc65c39a 1627 if (ref->data.desc > new_ref->data.desc)
355b0502 1628 break;
bc65c39a 1629 new_ref->data.desc = ref->data.desc + 1;
355b0502
GKH
1630 }
1631
1632 p = &proc->refs_by_desc.rb_node;
1633 while (*p) {
1634 parent = *p;
1635 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1636
bc65c39a 1637 if (new_ref->data.desc < ref->data.desc)
355b0502 1638 p = &(*p)->rb_left;
bc65c39a 1639 else if (new_ref->data.desc > ref->data.desc)
355b0502
GKH
1640 p = &(*p)->rb_right;
1641 else
1642 BUG();
1643 }
1644 rb_link_node(&new_ref->rb_node_desc, parent, p);
1645 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
25de59a5
TK
1646
1647 binder_node_lock(node);
cc13edea 1648 hlist_add_head(&new_ref->node_entry, &node->refs);
355b0502 1649
cc13edea
TK
1650 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1651 "%d new ref %d desc %d for node %d\n",
bc65c39a 1652 proc->pid, new_ref->data.debug_id, new_ref->data.desc,
cc13edea 1653 node->debug_id);
25de59a5 1654 binder_node_unlock(node);
355b0502
GKH
1655 return new_ref;
1656}
1657
5717118f 1658static void binder_cleanup_ref_olocked(struct binder_ref *ref)
355b0502 1659{
ccca76b6 1660 bool delete_node = false;
ccca76b6 1661
355b0502 1662 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
56b468fc 1663 "%d delete ref %d desc %d for node %d\n",
bc65c39a 1664 ref->proc->pid, ref->data.debug_id, ref->data.desc,
56b468fc 1665 ref->node->debug_id);
355b0502
GKH
1666
1667 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1668 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
bc65c39a 1669
25de59a5 1670 binder_node_inner_lock(ref->node);
bc65c39a 1671 if (ref->data.strong)
25de59a5 1672 binder_dec_node_nilocked(ref->node, 1, 1);
bc65c39a 1673
355b0502 1674 hlist_del(&ref->node_entry);
25de59a5
TK
1675 delete_node = binder_dec_node_nilocked(ref->node, 0, 1);
1676 binder_node_inner_unlock(ref->node);
ccca76b6
TK
1677 /*
1678 * Clear ref->node unless we want the caller to free the node
1679 */
1680 if (!delete_node) {
1681 /*
1682 * The caller uses ref->node to determine
1683 * whether the node needs to be freed. Clear
1684 * it since the node is still alive.
1685 */
1686 ref->node = NULL;
1687 }
bc65c39a 1688
355b0502
GKH
1689 if (ref->death) {
1690 binder_debug(BINDER_DEBUG_DEAD_BINDER,
56b468fc 1691 "%d delete ref %d desc %d has death notification\n",
bc65c39a
TK
1692 ref->proc->pid, ref->data.debug_id,
1693 ref->data.desc);
aad8732c 1694 binder_dequeue_work(ref->proc, &ref->death->work);
355b0502
GKH
1695 binder_stats_deleted(BINDER_STAT_DEATH);
1696 }
355b0502
GKH
1697 binder_stats_deleted(BINDER_STAT_REF);
1698}
1699
bc65c39a 1700/**
5717118f 1701 * binder_inc_ref_olocked() - increment the ref for given handle
bc65c39a
TK
1702 * @ref: ref to be incremented
1703 * @strong: if true, strong increment, else weak
1704 * @target_list: list to queue node work on
1705 *
5717118f 1706 * Increment the ref. @ref->proc->outer_lock must be held on entry
bc65c39a
TK
1707 *
1708 * Return: 0, if successful, else errno
1709 */
5717118f
TK
1710static int binder_inc_ref_olocked(struct binder_ref *ref, int strong,
1711 struct list_head *target_list)
355b0502
GKH
1712{
1713 int ret;
01c8cbde 1714
355b0502 1715 if (strong) {
bc65c39a 1716 if (ref->data.strong == 0) {
355b0502
GKH
1717 ret = binder_inc_node(ref->node, 1, 1, target_list);
1718 if (ret)
1719 return ret;
1720 }
bc65c39a 1721 ref->data.strong++;
355b0502 1722 } else {
bc65c39a 1723 if (ref->data.weak == 0) {
355b0502
GKH
1724 ret = binder_inc_node(ref->node, 0, 1, target_list);
1725 if (ret)
1726 return ret;
1727 }
bc65c39a 1728 ref->data.weak++;
355b0502
GKH
1729 }
1730 return 0;
1731}
1732
bc65c39a
TK
1733/**
1734 * binder_dec_ref() - dec the ref for given handle
1735 * @ref: ref to be decremented
1736 * @strong: if true, strong decrement, else weak
1737 *
1738 * Decrement the ref.
1739 *
bc65c39a
TK
1740 * Return: true if ref is cleaned up and ready to be freed
1741 */
5717118f 1742static bool binder_dec_ref_olocked(struct binder_ref *ref, int strong)
355b0502
GKH
1743{
1744 if (strong) {
bc65c39a 1745 if (ref->data.strong == 0) {
56b468fc 1746 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
bc65c39a
TK
1747 ref->proc->pid, ref->data.debug_id,
1748 ref->data.desc, ref->data.strong,
1749 ref->data.weak);
1750 return false;
355b0502 1751 }
bc65c39a 1752 ref->data.strong--;
ccca76b6
TK
1753 if (ref->data.strong == 0)
1754 binder_dec_node(ref->node, strong, 1);
355b0502 1755 } else {
bc65c39a 1756 if (ref->data.weak == 0) {
56b468fc 1757 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
bc65c39a
TK
1758 ref->proc->pid, ref->data.debug_id,
1759 ref->data.desc, ref->data.strong,
1760 ref->data.weak);
1761 return false;
355b0502 1762 }
bc65c39a 1763 ref->data.weak--;
355b0502 1764 }
bc65c39a 1765 if (ref->data.strong == 0 && ref->data.weak == 0) {
5717118f 1766 binder_cleanup_ref_olocked(ref);
bc65c39a
TK
1767 return true;
1768 }
1769 return false;
1770}
1771
1772/**
1773 * binder_get_node_from_ref() - get the node from the given proc/desc
1774 * @proc: proc containing the ref
1775 * @desc: the handle associated with the ref
1776 * @need_strong_ref: if true, only return node if ref is strong
1777 * @rdata: the id/refcount data for the ref
1778 *
1779 * Given a proc and ref handle, return the associated binder_node
1780 *
1781 * Return: a binder_node or NULL if not found or not strong when strong required
1782 */
1783static struct binder_node *binder_get_node_from_ref(
1784 struct binder_proc *proc,
1785 u32 desc, bool need_strong_ref,
1786 struct binder_ref_data *rdata)
1787{
1788 struct binder_node *node;
1789 struct binder_ref *ref;
1790
5717118f
TK
1791 binder_proc_lock(proc);
1792 ref = binder_get_ref_olocked(proc, desc, need_strong_ref);
bc65c39a
TK
1793 if (!ref)
1794 goto err_no_ref;
1795 node = ref->node;
9607bf70
TK
1796 /*
1797 * Take an implicit reference on the node to ensure
1798 * it stays alive until the call to binder_put_node()
1799 */
1800 binder_inc_node_tmpref(node);
bc65c39a
TK
1801 if (rdata)
1802 *rdata = ref->data;
5717118f 1803 binder_proc_unlock(proc);
bc65c39a
TK
1804
1805 return node;
1806
1807err_no_ref:
5717118f 1808 binder_proc_unlock(proc);
bc65c39a
TK
1809 return NULL;
1810}
1811
1812/**
1813 * binder_free_ref() - free the binder_ref
1814 * @ref: ref to free
1815 *
ccca76b6
TK
1816 * Free the binder_ref. Free the binder_node indicated by ref->node
1817 * (if non-NULL) and the binder_ref_death indicated by ref->death.
bc65c39a
TK
1818 */
1819static void binder_free_ref(struct binder_ref *ref)
1820{
ccca76b6
TK
1821 if (ref->node)
1822 binder_free_node(ref->node);
bc65c39a
TK
1823 kfree(ref->death);
1824 kfree(ref);
1825}
1826
1827/**
1828 * binder_update_ref_for_handle() - inc/dec the ref for given handle
1829 * @proc: proc containing the ref
1830 * @desc: the handle associated with the ref
1831 * @increment: true=inc reference, false=dec reference
1832 * @strong: true=strong reference, false=weak reference
1833 * @rdata: the id/refcount data for the ref
1834 *
1835 * Given a proc and ref handle, increment or decrement the ref
1836 * according to "increment" arg.
1837 *
1838 * Return: 0 if successful, else errno
1839 */
1840static int binder_update_ref_for_handle(struct binder_proc *proc,
1841 uint32_t desc, bool increment, bool strong,
1842 struct binder_ref_data *rdata)
1843{
1844 int ret = 0;
1845 struct binder_ref *ref;
1846 bool delete_ref = false;
1847
5717118f
TK
1848 binder_proc_lock(proc);
1849 ref = binder_get_ref_olocked(proc, desc, strong);
bc65c39a
TK
1850 if (!ref) {
1851 ret = -EINVAL;
1852 goto err_no_ref;
1853 }
1854 if (increment)
5717118f 1855 ret = binder_inc_ref_olocked(ref, strong, NULL);
bc65c39a 1856 else
5717118f 1857 delete_ref = binder_dec_ref_olocked(ref, strong);
bc65c39a
TK
1858
1859 if (rdata)
1860 *rdata = ref->data;
5717118f 1861 binder_proc_unlock(proc);
bc65c39a
TK
1862
1863 if (delete_ref)
1864 binder_free_ref(ref);
1865 return ret;
1866
1867err_no_ref:
5717118f 1868 binder_proc_unlock(proc);
bc65c39a
TK
1869 return ret;
1870}
1871
1872/**
1873 * binder_dec_ref_for_handle() - dec the ref for given handle
1874 * @proc: proc containing the ref
1875 * @desc: the handle associated with the ref
1876 * @strong: true=strong reference, false=weak reference
1877 * @rdata: the id/refcount data for the ref
1878 *
1879 * Just calls binder_update_ref_for_handle() to decrement the ref.
1880 *
1881 * Return: 0 if successful, else errno
1882 */
1883static int binder_dec_ref_for_handle(struct binder_proc *proc,
1884 uint32_t desc, bool strong, struct binder_ref_data *rdata)
1885{
1886 return binder_update_ref_for_handle(proc, desc, false, strong, rdata);
1887}
1888
1889
1890/**
1891 * binder_inc_ref_for_node() - increment the ref for given proc/node
1892 * @proc: proc containing the ref
1893 * @node: target node
1894 * @strong: true=strong reference, false=weak reference
1895 * @target_list: worklist to use if node is incremented
1896 * @rdata: the id/refcount data for the ref
1897 *
1898 * Given a proc and node, increment the ref. Create the ref if it
1899 * doesn't already exist
1900 *
1901 * Return: 0 if successful, else errno
1902 */
1903static int binder_inc_ref_for_node(struct binder_proc *proc,
1904 struct binder_node *node,
1905 bool strong,
1906 struct list_head *target_list,
1907 struct binder_ref_data *rdata)
1908{
1909 struct binder_ref *ref;
1910 struct binder_ref *new_ref = NULL;
1911 int ret = 0;
1912
5717118f
TK
1913 binder_proc_lock(proc);
1914 ref = binder_get_ref_for_node_olocked(proc, node, NULL);
bc65c39a 1915 if (!ref) {
5717118f 1916 binder_proc_unlock(proc);
bc65c39a
TK
1917 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1918 if (!new_ref)
1919 return -ENOMEM;
5717118f
TK
1920 binder_proc_lock(proc);
1921 ref = binder_get_ref_for_node_olocked(proc, node, new_ref);
bc65c39a 1922 }
5717118f 1923 ret = binder_inc_ref_olocked(ref, strong, target_list);
bc65c39a 1924 *rdata = ref->data;
5717118f 1925 binder_proc_unlock(proc);
bc65c39a
TK
1926 if (new_ref && ref != new_ref)
1927 /*
1928 * Another thread created the ref first so
1929 * free the one we allocated
1930 */
1931 kfree(new_ref);
1932 return ret;
355b0502
GKH
1933}
1934
a45c586f
MC
1935static void binder_pop_transaction_ilocked(struct binder_thread *target_thread,
1936 struct binder_transaction *t)
355b0502 1937{
7323366b 1938 BUG_ON(!target_thread);
a45c586f 1939 BUG_ON(!spin_is_locked(&target_thread->proc->inner_lock));
7323366b
TK
1940 BUG_ON(target_thread->transaction_stack != t);
1941 BUG_ON(target_thread->transaction_stack->from != target_thread);
1942 target_thread->transaction_stack =
1943 target_thread->transaction_stack->from_parent;
1944 t->from = NULL;
1945}
1946
82f6ad88
TK
1947/**
1948 * binder_thread_dec_tmpref() - decrement thread->tmp_ref
1949 * @thread: thread to decrement
1950 *
1951 * A thread needs to be kept alive while being used to create or
1952 * handle a transaction. binder_get_txn_from() is used to safely
1953 * extract t->from from a binder_transaction and keep the thread
1954 * indicated by t->from from being freed. When done with that
1955 * binder_thread, this function is called to decrement the
1956 * tmp_ref and free if appropriate (thread has been released
1957 * and no transaction being processed by the driver)
1958 */
1959static void binder_thread_dec_tmpref(struct binder_thread *thread)
1960{
1961 /*
1962 * atomic is used to protect the counter value while
1963 * it cannot reach zero or thread->is_dead is false
82f6ad88 1964 */
aed01731 1965 binder_inner_proc_lock(thread->proc);
82f6ad88
TK
1966 atomic_dec(&thread->tmp_ref);
1967 if (thread->is_dead && !atomic_read(&thread->tmp_ref)) {
aed01731 1968 binder_inner_proc_unlock(thread->proc);
82f6ad88
TK
1969 binder_free_thread(thread);
1970 return;
1971 }
aed01731 1972 binder_inner_proc_unlock(thread->proc);
82f6ad88
TK
1973}
1974
1975/**
1976 * binder_proc_dec_tmpref() - decrement proc->tmp_ref
1977 * @proc: proc to decrement
1978 *
1979 * A binder_proc needs to be kept alive while being used to create or
1980 * handle a transaction. proc->tmp_ref is incremented when
1981 * creating a new transaction or the binder_proc is currently in-use
1982 * by threads that are being released. When done with the binder_proc,
1983 * this function is called to decrement the counter and free the
1984 * proc if appropriate (proc has been released, all threads have
1985 * been released and not currenly in-use to process a transaction).
1986 */
1987static void binder_proc_dec_tmpref(struct binder_proc *proc)
1988{
aed01731 1989 binder_inner_proc_lock(proc);
82f6ad88
TK
1990 proc->tmp_ref--;
1991 if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) &&
1992 !proc->tmp_ref) {
aed01731 1993 binder_inner_proc_unlock(proc);
82f6ad88
TK
1994 binder_free_proc(proc);
1995 return;
1996 }
aed01731 1997 binder_inner_proc_unlock(proc);
82f6ad88
TK
1998}
1999
2000/**
2001 * binder_get_txn_from() - safely extract the "from" thread in transaction
2002 * @t: binder transaction for t->from
2003 *
2004 * Atomically return the "from" thread and increment the tmp_ref
2005 * count for the thread to ensure it stays alive until
2006 * binder_thread_dec_tmpref() is called.
2007 *
2008 * Return: the value of t->from
2009 */
2010static struct binder_thread *binder_get_txn_from(
2011 struct binder_transaction *t)
2012{
2013 struct binder_thread *from;
2014
2015 spin_lock(&t->lock);
2016 from = t->from;
2017 if (from)
2018 atomic_inc(&from->tmp_ref);
2019 spin_unlock(&t->lock);
2020 return from;
2021}
2022
a45c586f
MC
2023/**
2024 * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock
2025 * @t: binder transaction for t->from
2026 *
2027 * Same as binder_get_txn_from() except it also acquires the proc->inner_lock
2028 * to guarantee that the thread cannot be released while operating on it.
2029 * The caller must call binder_inner_proc_unlock() to release the inner lock
2030 * as well as call binder_dec_thread_txn() to release the reference.
2031 *
2032 * Return: the value of t->from
2033 */
2034static struct binder_thread *binder_get_txn_from_and_acq_inner(
2035 struct binder_transaction *t)
2036{
2037 struct binder_thread *from;
2038
2039 from = binder_get_txn_from(t);
2040 if (!from)
2041 return NULL;
2042 binder_inner_proc_lock(from->proc);
2043 if (t->from) {
2044 BUG_ON(from != t->from);
2045 return from;
2046 }
2047 binder_inner_proc_unlock(from->proc);
2048 binder_thread_dec_tmpref(from);
2049 return NULL;
2050}
2051
7323366b
TK
2052static void binder_free_transaction(struct binder_transaction *t)
2053{
355b0502
GKH
2054 if (t->buffer)
2055 t->buffer->transaction = NULL;
2056 kfree(t);
2057 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2058}
2059
2060static void binder_send_failed_reply(struct binder_transaction *t,
2061 uint32_t error_code)
2062{
2063 struct binder_thread *target_thread;
d6733a8a 2064 struct binder_transaction *next;
01c8cbde 2065
355b0502
GKH
2066 BUG_ON(t->flags & TF_ONE_WAY);
2067 while (1) {
a45c586f 2068 target_thread = binder_get_txn_from_and_acq_inner(t);
355b0502 2069 if (target_thread) {
795aa6bc
TK
2070 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2071 "send failed reply for transaction %d to %d:%d\n",
2072 t->debug_id,
2073 target_thread->proc->pid,
2074 target_thread->pid);
2075
a45c586f 2076 binder_pop_transaction_ilocked(target_thread, t);
795aa6bc
TK
2077 if (target_thread->reply_error.cmd == BR_OK) {
2078 target_thread->reply_error.cmd = error_code;
a45c586f 2079 binder_enqueue_work_ilocked(
aad8732c 2080 &target_thread->reply_error.work,
795aa6bc 2081 &target_thread->todo);
355b0502
GKH
2082 wake_up_interruptible(&target_thread->wait);
2083 } else {
795aa6bc
TK
2084 WARN(1, "Unexpected reply error: %u\n",
2085 target_thread->reply_error.cmd);
355b0502 2086 }
a45c586f 2087 binder_inner_proc_unlock(target_thread->proc);
82f6ad88 2088 binder_thread_dec_tmpref(target_thread);
795aa6bc 2089 binder_free_transaction(t);
355b0502 2090 return;
d6733a8a
LT
2091 }
2092 next = t->from_parent;
2093
2094 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2095 "send failed reply for transaction %d, target dead\n",
2096 t->debug_id);
2097
7323366b 2098 binder_free_transaction(t);
d6733a8a 2099 if (next == NULL) {
355b0502 2100 binder_debug(BINDER_DEBUG_DEAD_BINDER,
d6733a8a
LT
2101 "reply failed, no target thread at root\n");
2102 return;
355b0502 2103 }
d6733a8a
LT
2104 t = next;
2105 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2106 "reply failed, no target thread -- retry %d\n",
2107 t->debug_id);
355b0502
GKH
2108 }
2109}
2110
17f2cab3
MC
2111/**
2112 * binder_validate_object() - checks for a valid metadata object in a buffer.
2113 * @buffer: binder_buffer that we're parsing.
2114 * @offset: offset in the buffer at which to validate an object.
2115 *
2116 * Return: If there's a valid metadata object at @offset in @buffer, the
2117 * size of that object. Otherwise, it returns zero.
2118 */
2119static size_t binder_validate_object(struct binder_buffer *buffer, u64 offset)
2120{
2121 /* Check if we can read a header first */
2122 struct binder_object_header *hdr;
2123 size_t object_size = 0;
2124
2125 if (offset > buffer->data_size - sizeof(*hdr) ||
2126 buffer->data_size < sizeof(*hdr) ||
2127 !IS_ALIGNED(offset, sizeof(u32)))
2128 return 0;
2129
2130 /* Ok, now see if we can read a complete object. */
2131 hdr = (struct binder_object_header *)(buffer->data + offset);
2132 switch (hdr->type) {
2133 case BINDER_TYPE_BINDER:
2134 case BINDER_TYPE_WEAK_BINDER:
2135 case BINDER_TYPE_HANDLE:
2136 case BINDER_TYPE_WEAK_HANDLE:
2137 object_size = sizeof(struct flat_binder_object);
2138 break;
2139 case BINDER_TYPE_FD:
2140 object_size = sizeof(struct binder_fd_object);
2141 break;
e884979e
MC
2142 case BINDER_TYPE_PTR:
2143 object_size = sizeof(struct binder_buffer_object);
2144 break;
2f82efbc
MC
2145 case BINDER_TYPE_FDA:
2146 object_size = sizeof(struct binder_fd_array_object);
2147 break;
17f2cab3
MC
2148 default:
2149 return 0;
2150 }
2151 if (offset <= buffer->data_size - object_size &&
2152 buffer->data_size >= object_size)
2153 return object_size;
2154 else
2155 return 0;
2156}
2157
e884979e
MC
2158/**
2159 * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
2160 * @b: binder_buffer containing the object
2161 * @index: index in offset array at which the binder_buffer_object is
2162 * located
2163 * @start: points to the start of the offset array
2164 * @num_valid: the number of valid offsets in the offset array
2165 *
2166 * Return: If @index is within the valid range of the offset array
2167 * described by @start and @num_valid, and if there's a valid
2168 * binder_buffer_object at the offset found in index @index
2169 * of the offset array, that object is returned. Otherwise,
2170 * %NULL is returned.
2171 * Note that the offset found in index @index itself is not
2172 * verified; this function assumes that @num_valid elements
2173 * from @start were previously verified to have valid offsets.
2174 */
2175static struct binder_buffer_object *binder_validate_ptr(struct binder_buffer *b,
2176 binder_size_t index,
2177 binder_size_t *start,
2178 binder_size_t num_valid)
2179{
2180 struct binder_buffer_object *buffer_obj;
2181 binder_size_t *offp;
2182
2183 if (index >= num_valid)
2184 return NULL;
2185
2186 offp = start + index;
2187 buffer_obj = (struct binder_buffer_object *)(b->data + *offp);
2188 if (buffer_obj->hdr.type != BINDER_TYPE_PTR)
2189 return NULL;
2190
2191 return buffer_obj;
2192}
2193
2194/**
2195 * binder_validate_fixup() - validates pointer/fd fixups happen in order.
2196 * @b: transaction buffer
2197 * @objects_start start of objects buffer
2198 * @buffer: binder_buffer_object in which to fix up
2199 * @offset: start offset in @buffer to fix up
2200 * @last_obj: last binder_buffer_object that we fixed up in
2201 * @last_min_offset: minimum fixup offset in @last_obj
2202 *
2203 * Return: %true if a fixup in buffer @buffer at offset @offset is
2204 * allowed.
2205 *
2206 * For safety reasons, we only allow fixups inside a buffer to happen
2207 * at increasing offsets; additionally, we only allow fixup on the last
2208 * buffer object that was verified, or one of its parents.
2209 *
2210 * Example of what is allowed:
2211 *
2212 * A
2213 * B (parent = A, offset = 0)
2214 * C (parent = A, offset = 16)
2215 * D (parent = C, offset = 0)
2216 * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
2217 *
2218 * Examples of what is not allowed:
2219 *
2220 * Decreasing offsets within the same parent:
2221 * A
2222 * C (parent = A, offset = 16)
2223 * B (parent = A, offset = 0) // decreasing offset within A
2224 *
2225 * Referring to a parent that wasn't the last object or any of its parents:
2226 * A
2227 * B (parent = A, offset = 0)
2228 * C (parent = A, offset = 0)
2229 * C (parent = A, offset = 16)
2230 * D (parent = B, offset = 0) // B is not A or any of A's parents
2231 */
2232static bool binder_validate_fixup(struct binder_buffer *b,
2233 binder_size_t *objects_start,
2234 struct binder_buffer_object *buffer,
2235 binder_size_t fixup_offset,
2236 struct binder_buffer_object *last_obj,
2237 binder_size_t last_min_offset)
2238{
2239 if (!last_obj) {
2240 /* Nothing to fix up in */
2241 return false;
2242 }
2243
2244 while (last_obj != buffer) {
2245 /*
2246 * Safe to retrieve the parent of last_obj, since it
2247 * was already previously verified by the driver.
2248 */
2249 if ((last_obj->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0)
2250 return false;
2251 last_min_offset = last_obj->parent_offset + sizeof(uintptr_t);
2252 last_obj = (struct binder_buffer_object *)
2253 (b->data + *(objects_start + last_obj->parent));
2254 }
2255 return (fixup_offset >= last_min_offset);
2256}
2257
355b0502
GKH
2258static void binder_transaction_buffer_release(struct binder_proc *proc,
2259 struct binder_buffer *buffer,
9c0a1d02 2260 binder_size_t *failed_at)
355b0502 2261{
e884979e 2262 binder_size_t *offp, *off_start, *off_end;
355b0502
GKH
2263 int debug_id = buffer->debug_id;
2264
2265 binder_debug(BINDER_DEBUG_TRANSACTION,
3877fd57 2266 "%d buffer release %d, size %zd-%zd, failed at %p\n",
355b0502
GKH
2267 proc->pid, buffer->debug_id,
2268 buffer->data_size, buffer->offsets_size, failed_at);
2269
2270 if (buffer->target_node)
2271 binder_dec_node(buffer->target_node, 1, 0);
2272
e884979e
MC
2273 off_start = (binder_size_t *)(buffer->data +
2274 ALIGN(buffer->data_size, sizeof(void *)));
355b0502
GKH
2275 if (failed_at)
2276 off_end = failed_at;
2277 else
e884979e
MC
2278 off_end = (void *)off_start + buffer->offsets_size;
2279 for (offp = off_start; offp < off_end; offp++) {
17f2cab3
MC
2280 struct binder_object_header *hdr;
2281 size_t object_size = binder_validate_object(buffer, *offp);
01c8cbde 2282
17f2cab3
MC
2283 if (object_size == 0) {
2284 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
9c0a1d02 2285 debug_id, (u64)*offp, buffer->data_size);
355b0502
GKH
2286 continue;
2287 }
17f2cab3
MC
2288 hdr = (struct binder_object_header *)(buffer->data + *offp);
2289 switch (hdr->type) {
355b0502
GKH
2290 case BINDER_TYPE_BINDER:
2291 case BINDER_TYPE_WEAK_BINDER: {
17f2cab3
MC
2292 struct flat_binder_object *fp;
2293 struct binder_node *node;
01c8cbde 2294
17f2cab3
MC
2295 fp = to_flat_binder_object(hdr);
2296 node = binder_get_node(proc, fp->binder);
355b0502 2297 if (node == NULL) {
9c0a1d02
AH
2298 pr_err("transaction release %d bad node %016llx\n",
2299 debug_id, (u64)fp->binder);
355b0502
GKH
2300 break;
2301 }
2302 binder_debug(BINDER_DEBUG_TRANSACTION,
9c0a1d02
AH
2303 " node %d u%016llx\n",
2304 node->debug_id, (u64)node->ptr);
17f2cab3
MC
2305 binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER,
2306 0);
9607bf70 2307 binder_put_node(node);
355b0502
GKH
2308 } break;
2309 case BINDER_TYPE_HANDLE:
2310 case BINDER_TYPE_WEAK_HANDLE: {
17f2cab3 2311 struct flat_binder_object *fp;
bc65c39a
TK
2312 struct binder_ref_data rdata;
2313 int ret;
01c8cbde 2314
17f2cab3 2315 fp = to_flat_binder_object(hdr);
bc65c39a
TK
2316 ret = binder_dec_ref_for_handle(proc, fp->handle,
2317 hdr->type == BINDER_TYPE_HANDLE, &rdata);
2318
2319 if (ret) {
2320 pr_err("transaction release %d bad handle %d, ret = %d\n",
2321 debug_id, fp->handle, ret);
355b0502
GKH
2322 break;
2323 }
2324 binder_debug(BINDER_DEBUG_TRANSACTION,
bc65c39a
TK
2325 " ref %d desc %d\n",
2326 rdata.debug_id, rdata.desc);
355b0502
GKH
2327 } break;
2328
17f2cab3
MC
2329 case BINDER_TYPE_FD: {
2330 struct binder_fd_object *fp = to_binder_fd_object(hdr);
2331
355b0502 2332 binder_debug(BINDER_DEBUG_TRANSACTION,
17f2cab3 2333 " fd %d\n", fp->fd);
355b0502 2334 if (failed_at)
17f2cab3
MC
2335 task_close_fd(proc, fp->fd);
2336 } break;
e884979e
MC
2337 case BINDER_TYPE_PTR:
2338 /*
2339 * Nothing to do here, this will get cleaned up when the
2340 * transaction buffer gets freed
2341 */
2342 break;
2f82efbc
MC
2343 case BINDER_TYPE_FDA: {
2344 struct binder_fd_array_object *fda;
2345 struct binder_buffer_object *parent;
2346 uintptr_t parent_buffer;
2347 u32 *fd_array;
2348 size_t fd_index;
2349 binder_size_t fd_buf_size;
2350
2351 fda = to_binder_fd_array_object(hdr);
2352 parent = binder_validate_ptr(buffer, fda->parent,
2353 off_start,
2354 offp - off_start);
2355 if (!parent) {
2356 pr_err("transaction release %d bad parent offset",
2357 debug_id);
2358 continue;
2359 }
2360 /*
2361 * Since the parent was already fixed up, convert it
2362 * back to kernel address space to access it
2363 */
2364 parent_buffer = parent->buffer -
69c33bb1
TK
2365 binder_alloc_get_user_buffer_offset(
2366 &proc->alloc);
2f82efbc
MC
2367
2368 fd_buf_size = sizeof(u32) * fda->num_fds;
2369 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2370 pr_err("transaction release %d invalid number of fds (%lld)\n",
2371 debug_id, (u64)fda->num_fds);
2372 continue;
2373 }
2374 if (fd_buf_size > parent->length ||
2375 fda->parent_offset > parent->length - fd_buf_size) {
2376 /* No space for all file descriptors here. */
2377 pr_err("transaction release %d not enough space for %lld fds in buffer\n",
2378 debug_id, (u64)fda->num_fds);
2379 continue;
2380 }
2381 fd_array = (u32 *)(parent_buffer + fda->parent_offset);
2382 for (fd_index = 0; fd_index < fda->num_fds; fd_index++)
2383 task_close_fd(proc, fd_array[fd_index]);
2384 } break;
355b0502 2385 default:
3c2a0909 2386 pr_err("transaction release %d bad object type %x\n",
17f2cab3 2387 debug_id, hdr->type);
355b0502
GKH
2388 break;
2389 }
2390 }
2391}
2392
b4bdab80
MC
2393static int binder_translate_binder(struct flat_binder_object *fp,
2394 struct binder_transaction *t,
2395 struct binder_thread *thread)
2396{
2397 struct binder_node *node;
b4bdab80
MC
2398 struct binder_proc *proc = thread->proc;
2399 struct binder_proc *target_proc = t->to_proc;
bc65c39a 2400 struct binder_ref_data rdata;
9607bf70 2401 int ret = 0;
b4bdab80
MC
2402
2403 node = binder_get_node(proc, fp->binder);
2404 if (!node) {
25de59a5 2405 node = binder_new_node(proc, fp);
b4bdab80
MC
2406 if (!node)
2407 return -ENOMEM;
b4bdab80
MC
2408 }
2409 if (fp->cookie != node->cookie) {
2410 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
2411 proc->pid, thread->pid, (u64)fp->binder,
2412 node->debug_id, (u64)fp->cookie,
2413 (u64)node->cookie);
9607bf70
TK
2414 ret = -EINVAL;
2415 goto done;
2416 }
2417 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2418 ret = -EPERM;
2419 goto done;
b4bdab80 2420 }
b4bdab80 2421
bc65c39a
TK
2422 ret = binder_inc_ref_for_node(target_proc, node,
2423 fp->hdr.type == BINDER_TYPE_BINDER,
2424 &thread->todo, &rdata);
2425 if (ret)
9607bf70 2426 goto done;
b4bdab80
MC
2427
2428 if (fp->hdr.type == BINDER_TYPE_BINDER)
2429 fp->hdr.type = BINDER_TYPE_HANDLE;
2430 else
2431 fp->hdr.type = BINDER_TYPE_WEAK_HANDLE;
2432 fp->binder = 0;
bc65c39a 2433 fp->handle = rdata.desc;
b4bdab80 2434 fp->cookie = 0;
b4bdab80 2435
bc65c39a 2436 trace_binder_transaction_node_to_ref(t, node, &rdata);
b4bdab80
MC
2437 binder_debug(BINDER_DEBUG_TRANSACTION,
2438 " node %d u%016llx -> ref %d desc %d\n",
2439 node->debug_id, (u64)node->ptr,
bc65c39a 2440 rdata.debug_id, rdata.desc);
9607bf70
TK
2441done:
2442 binder_put_node(node);
2443 return ret;
b4bdab80
MC
2444}
2445
2446static int binder_translate_handle(struct flat_binder_object *fp,
2447 struct binder_transaction *t,
2448 struct binder_thread *thread)
2449{
b4bdab80
MC
2450 struct binder_proc *proc = thread->proc;
2451 struct binder_proc *target_proc = t->to_proc;
bc65c39a
TK
2452 struct binder_node *node;
2453 struct binder_ref_data src_rdata;
9607bf70 2454 int ret = 0;
b4bdab80 2455
bc65c39a
TK
2456 node = binder_get_node_from_ref(proc, fp->handle,
2457 fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata);
2458 if (!node) {
b4bdab80
MC
2459 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
2460 proc->pid, thread->pid, fp->handle);
2461 return -EINVAL;
2462 }
9607bf70
TK
2463 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2464 ret = -EPERM;
2465 goto done;
2466 }
b4bdab80 2467
25de59a5 2468 binder_node_lock(node);
bc65c39a 2469 if (node->proc == target_proc) {
b4bdab80
MC
2470 if (fp->hdr.type == BINDER_TYPE_HANDLE)
2471 fp->hdr.type = BINDER_TYPE_BINDER;
2472 else
2473 fp->hdr.type = BINDER_TYPE_WEAK_BINDER;
bc65c39a
TK
2474 fp->binder = node->ptr;
2475 fp->cookie = node->cookie;
25de59a5
TK
2476 if (node->proc)
2477 binder_inner_proc_lock(node->proc);
2478 binder_inc_node_nilocked(node,
2479 fp->hdr.type == BINDER_TYPE_BINDER,
2480 0, NULL);
2481 if (node->proc)
2482 binder_inner_proc_unlock(node->proc);
bc65c39a 2483 trace_binder_transaction_ref_to_node(t, node, &src_rdata);
b4bdab80
MC
2484 binder_debug(BINDER_DEBUG_TRANSACTION,
2485 " ref %d desc %d -> node %d u%016llx\n",
bc65c39a
TK
2486 src_rdata.debug_id, src_rdata.desc, node->debug_id,
2487 (u64)node->ptr);
25de59a5 2488 binder_node_unlock(node);
b4bdab80 2489 } else {
bc65c39a
TK
2490 int ret;
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__;
b4bdab80 3262err_translate_failed:
355b0502
GKH
3263err_bad_object_type:
3264err_bad_offset:
2f82efbc 3265err_bad_parent:
355b0502 3266err_copy_data_failed:
975a1ac9 3267 trace_binder_transaction_failed_buffer_release(t->buffer);
355b0502 3268 binder_transaction_buffer_release(target_proc, t->buffer, offp);
0b62b556 3269 target_node = NULL;
355b0502 3270 t->buffer->transaction = NULL;
69c33bb1 3271 binder_alloc_free_buf(&target_proc->alloc, t->buffer);
355b0502
GKH
3272err_binder_alloc_buf_failed:
3273 kfree(tcomplete);
3274 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3275err_alloc_tcomplete_failed:
3276 kfree(t);
3277 binder_stats_deleted(BINDER_STAT_TRANSACTION);
3278err_alloc_t_failed:
3279err_bad_call_stack:
3280err_empty_call_stack:
3281err_dead_binder:
3282err_invalid_target_handle:
3283err_no_context_mgr_node:
82f6ad88
TK
3284 if (target_thread)
3285 binder_thread_dec_tmpref(target_thread);
3286 if (target_proc)
3287 binder_proc_dec_tmpref(target_proc);
0b62b556
TK
3288 if (target_node)
3289 binder_dec_node(target_node, 1, 0);
3290
355b0502 3291 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
dfc99565
TK
3292 "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
3293 proc->pid, thread->pid, return_error, return_error_param,
3294 (u64)tr->data_size, (u64)tr->offsets_size,
3295 return_error_line);
355b0502
GKH
3296
3297 {
3298 struct binder_transaction_log_entry *fe;
01c8cbde 3299
dfc99565
TK
3300 e->return_error = return_error;
3301 e->return_error_param = return_error_param;
3302 e->return_error_line = return_error_line;
7eac155d 3303 fe = binder_transaction_log_add(&binder_transaction_log_failed);
355b0502 3304 *fe = *e;
7d94b2c7
TK
3305 /*
3306 * write barrier to synchronize with initialization
3307 * of log entry
3308 */
3309 smp_wmb();
3310 WRITE_ONCE(e->debug_id_done, t_debug_id);
3311 WRITE_ONCE(fe->debug_id_done, t_debug_id);
355b0502
GKH
3312 }
3313
795aa6bc 3314 BUG_ON(thread->return_error.cmd != BR_OK);
355b0502 3315 if (in_reply_to) {
dadba0f5 3316 binder_restore_priority(current, in_reply_to->saved_priority);
795aa6bc 3317 thread->return_error.cmd = BR_TRANSACTION_COMPLETE;
aad8732c
TK
3318 binder_enqueue_work(thread->proc,
3319 &thread->return_error.work,
3320 &thread->todo);
355b0502 3321 binder_send_failed_reply(in_reply_to, return_error);
795aa6bc
TK
3322 } else {
3323 thread->return_error.cmd = return_error;
aad8732c
TK
3324 binder_enqueue_work(thread->proc,
3325 &thread->return_error.work,
3326 &thread->todo);
795aa6bc 3327 }
355b0502
GKH
3328}
3329
9c0a1d02
AH
3330int binder_thread_write(struct binder_proc *proc,
3331 struct binder_thread *thread,
3332 binder_uintptr_t binder_buffer, size_t size,
3333 binder_size_t *consumed)
355b0502
GKH
3334{
3335 uint32_t cmd;
913926d0 3336 struct binder_context *context = proc->context;
9c0a1d02 3337 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
355b0502
GKH
3338 void __user *ptr = buffer + *consumed;
3339 void __user *end = buffer + size;
3340
795aa6bc 3341 while (ptr < end && thread->return_error.cmd == BR_OK) {
bc65c39a
TK
3342 int ret;
3343
d31f0c40 3344 if (get_user(cmd, (uint32_t __user *)ptr))
355b0502
GKH
3345 return -EFAULT;
3346 ptr += sizeof(uint32_t);
975a1ac9 3347 trace_binder_command(cmd);
7eac155d 3348 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
340b5f86
BJS
3349 atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]);
3350 atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]);
3351 atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]);
355b0502
GKH
3352 }
3353 switch (cmd) {
3354 case BC_INCREFS:
3355 case BC_ACQUIRE:
3356 case BC_RELEASE:
3357 case BC_DECREFS: {
3358 uint32_t target;
355b0502 3359 const char *debug_string;
bc65c39a
TK
3360 bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE;
3361 bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE;
3362 struct binder_ref_data rdata;
355b0502 3363
d31f0c40 3364 if (get_user(target, (uint32_t __user *)ptr))
355b0502 3365 return -EFAULT;
76341f6f 3366
355b0502 3367 ptr += sizeof(uint32_t);
bc65c39a
TK
3368 ret = -1;
3369 if (increment && !target) {
76341f6f 3370 struct binder_node *ctx_mgr_node;
76341f6f
TK
3371 mutex_lock(&context->context_mgr_node_lock);
3372 ctx_mgr_node = context->binder_context_mgr_node;
bc65c39a
TK
3373 if (ctx_mgr_node)
3374 ret = binder_inc_ref_for_node(
3375 proc, ctx_mgr_node,
3376 strong, NULL, &rdata);
76341f6f
TK
3377 mutex_unlock(&context->context_mgr_node_lock);
3378 }
bc65c39a
TK
3379 if (ret)
3380 ret = binder_update_ref_for_handle(
3381 proc, target, increment, strong,
3382 &rdata);
3383 if (!ret && rdata.desc != target) {
3384 binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
3385 proc->pid, thread->pid,
3386 target, rdata.desc);
355b0502
GKH
3387 }
3388 switch (cmd) {
3389 case BC_INCREFS:
3390 debug_string = "IncRefs";
355b0502
GKH
3391 break;
3392 case BC_ACQUIRE:
3393 debug_string = "Acquire";
355b0502
GKH
3394 break;
3395 case BC_RELEASE:
3396 debug_string = "Release";
355b0502
GKH
3397 break;
3398 case BC_DECREFS:
3399 default:
3400 debug_string = "DecRefs";
bc65c39a
TK
3401 break;
3402 }
3403 if (ret) {
3404 binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
3405 proc->pid, thread->pid, debug_string,
3406 strong, target, ret);
355b0502
GKH
3407 break;
3408 }
354ac4b4 3409 binder_debug(BINDER_DEBUG_USER_REFS,
bc65c39a
TK
3410 "%d:%d %s ref %d desc %d s %d w %d\n",
3411 proc->pid, thread->pid, debug_string,
3412 rdata.debug_id, rdata.desc, rdata.strong,
3413 rdata.weak);
8f35edf4 3414 break;
355b0502
GKH
3415 }
3416 case BC_INCREFS_DONE:
3417 case BC_ACQUIRE_DONE: {
9c0a1d02
AH
3418 binder_uintptr_t node_ptr;
3419 binder_uintptr_t cookie;
355b0502 3420 struct binder_node *node;
25de59a5 3421 bool free_node;
355b0502 3422
d31f0c40 3423 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
355b0502 3424 return -EFAULT;
9c0a1d02 3425 ptr += sizeof(binder_uintptr_t);
d31f0c40 3426 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
355b0502 3427 return -EFAULT;
9c0a1d02 3428 ptr += sizeof(binder_uintptr_t);
355b0502
GKH
3429 node = binder_get_node(proc, node_ptr);
3430 if (node == NULL) {
9c0a1d02 3431 binder_user_error("%d:%d %s u%016llx no match\n",
355b0502
GKH
3432 proc->pid, thread->pid,
3433 cmd == BC_INCREFS_DONE ?
3434 "BC_INCREFS_DONE" :
3435 "BC_ACQUIRE_DONE",
9c0a1d02 3436 (u64)node_ptr);
355b0502
GKH
3437 break;
3438 }
3439 if (cookie != node->cookie) {
9c0a1d02 3440 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
355b0502
GKH
3441 proc->pid, thread->pid,
3442 cmd == BC_INCREFS_DONE ?
3443 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
9c0a1d02
AH
3444 (u64)node_ptr, node->debug_id,
3445 (u64)cookie, (u64)node->cookie);
9607bf70 3446 binder_put_node(node);
355b0502
GKH
3447 break;
3448 }
25de59a5 3449 binder_node_inner_lock(node);
355b0502
GKH
3450 if (cmd == BC_ACQUIRE_DONE) {
3451 if (node->pending_strong_ref == 0) {
56b468fc 3452 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
355b0502
GKH
3453 proc->pid, thread->pid,
3454 node->debug_id);
25de59a5 3455 binder_node_inner_unlock(node);
9607bf70 3456 binder_put_node(node);
355b0502
GKH
3457 break;
3458 }
3459 node->pending_strong_ref = 0;
3460 } else {
3461 if (node->pending_weak_ref == 0) {
56b468fc 3462 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
355b0502
GKH
3463 proc->pid, thread->pid,
3464 node->debug_id);
25de59a5 3465 binder_node_inner_unlock(node);
9607bf70 3466 binder_put_node(node);
355b0502
GKH
3467 break;
3468 }
3469 node->pending_weak_ref = 0;
3470 }
25de59a5
TK
3471 free_node = binder_dec_node_nilocked(node,
3472 cmd == BC_ACQUIRE_DONE, 0);
3473 WARN_ON(free_node);
355b0502 3474 binder_debug(BINDER_DEBUG_USER_REFS,
9607bf70 3475 "%d:%d %s node %d ls %d lw %d tr %d\n",
355b0502
GKH
3476 proc->pid, thread->pid,
3477 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
9607bf70
TK
3478 node->debug_id, node->local_strong_refs,
3479 node->local_weak_refs, node->tmp_refs);
25de59a5 3480 binder_node_inner_unlock(node);
9607bf70 3481 binder_put_node(node);
355b0502
GKH
3482 break;
3483 }
3484 case BC_ATTEMPT_ACQUIRE:
56b468fc 3485 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
355b0502
GKH
3486 return -EINVAL;
3487 case BC_ACQUIRE_RESULT:
56b468fc 3488 pr_err("BC_ACQUIRE_RESULT not supported\n");
355b0502
GKH
3489 return -EINVAL;
3490
3491 case BC_FREE_BUFFER: {
9c0a1d02 3492 binder_uintptr_t data_ptr;
355b0502
GKH
3493 struct binder_buffer *buffer;
3494
d31f0c40 3495 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
355b0502 3496 return -EFAULT;
9c0a1d02 3497 ptr += sizeof(binder_uintptr_t);
355b0502 3498
382658e8
TK
3499 buffer = binder_alloc_prepare_to_free(&proc->alloc,
3500 data_ptr);
355b0502 3501 if (buffer == NULL) {
9c0a1d02
AH
3502 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
3503 proc->pid, thread->pid, (u64)data_ptr);
355b0502
GKH
3504 break;
3505 }
3506 if (!buffer->allow_user_free) {
9c0a1d02
AH
3507 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
3508 proc->pid, thread->pid, (u64)data_ptr);
355b0502
GKH
3509 break;
3510 }
3511 binder_debug(BINDER_DEBUG_FREE_BUFFER,
9c0a1d02
AH
3512 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
3513 proc->pid, thread->pid, (u64)data_ptr,
3514 buffer->debug_id,
355b0502
GKH
3515 buffer->transaction ? "active" : "finished");
3516
3517 if (buffer->transaction) {
3518 buffer->transaction->buffer = NULL;
3519 buffer->transaction = NULL;
3520 }
3521 if (buffer->async_transaction && buffer->target_node) {
aad8732c
TK
3522 struct binder_node *buf_node;
3523 struct binder_work *w;
3524
3525 buf_node = buffer->target_node;
25de59a5 3526 binder_node_inner_lock(buf_node);
aad8732c
TK
3527 BUG_ON(!buf_node->has_async_transaction);
3528 BUG_ON(buf_node->proc != proc);
aad8732c
TK
3529 w = binder_dequeue_work_head_ilocked(
3530 &buf_node->async_todo);
3531 if (!w)
3532 buf_node->has_async_transaction = 0;
21760f9f 3533 else
aad8732c
TK
3534 binder_enqueue_work_ilocked(
3535 w, &thread->todo);
25de59a5 3536 binder_node_inner_unlock(buf_node);
355b0502 3537 }
975a1ac9 3538 trace_binder_transaction_buffer_release(buffer);
355b0502 3539 binder_transaction_buffer_release(proc, buffer, NULL);
69c33bb1 3540 binder_alloc_free_buf(&proc->alloc, buffer);
355b0502
GKH
3541 break;
3542 }
3543
e884979e
MC
3544 case BC_TRANSACTION_SG:
3545 case BC_REPLY_SG: {
3546 struct binder_transaction_data_sg tr;
3547
c9f9a1ad 3548 if (copy_from_user(&tr, ptr, sizeof(tr)))
e884979e
MC
3549 return -EFAULT;
3550 ptr += sizeof(tr);
3551 binder_transaction(proc, thread, &tr.transaction_data,
3552 cmd == BC_REPLY_SG, tr.buffers_size);
3553 break;
3554 }
355b0502
GKH
3555 case BC_TRANSACTION:
3556 case BC_REPLY: {
3557 struct binder_transaction_data tr;
3558
d31f0c40 3559 if (copy_from_user(&tr, ptr, sizeof(tr)))
355b0502
GKH
3560 return -EFAULT;
3561 ptr += sizeof(tr);
d18dbf2f
MC
3562 binder_transaction(proc, thread, &tr,
3563 cmd == BC_REPLY, 0);
355b0502
GKH
3564 break;
3565 }
3566
3567 case BC_REGISTER_LOOPER:
3568 binder_debug(BINDER_DEBUG_THREADS,
56b468fc 3569 "%d:%d BC_REGISTER_LOOPER\n",
355b0502 3570 proc->pid, thread->pid);
e09d7b9a 3571 binder_inner_proc_lock(proc);
355b0502
GKH
3572 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
3573 thread->looper |= BINDER_LOOPER_STATE_INVALID;
56b468fc 3574 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
355b0502
GKH
3575 proc->pid, thread->pid);
3576 } else if (proc->requested_threads == 0) {
3577 thread->looper |= BINDER_LOOPER_STATE_INVALID;
56b468fc 3578 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
355b0502
GKH
3579 proc->pid, thread->pid);
3580 } else {
3581 proc->requested_threads--;
3582 proc->requested_threads_started++;
3583 }
3584 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
e09d7b9a 3585 binder_inner_proc_unlock(proc);
355b0502
GKH
3586 break;
3587 case BC_ENTER_LOOPER:
3588 binder_debug(BINDER_DEBUG_THREADS,
56b468fc 3589 "%d:%d BC_ENTER_LOOPER\n",
355b0502
GKH
3590 proc->pid, thread->pid);
3591 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
3592 thread->looper |= BINDER_LOOPER_STATE_INVALID;
56b468fc 3593 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
355b0502
GKH
3594 proc->pid, thread->pid);
3595 }
3596 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
3597 break;
3598 case BC_EXIT_LOOPER:
3599 binder_debug(BINDER_DEBUG_THREADS,
56b468fc 3600 "%d:%d BC_EXIT_LOOPER\n",
355b0502
GKH
3601 proc->pid, thread->pid);
3602 thread->looper |= BINDER_LOOPER_STATE_EXITED;
3603 break;
3604
3605 case BC_REQUEST_DEATH_NOTIFICATION:
3606 case BC_CLEAR_DEATH_NOTIFICATION: {
3607 uint32_t target;
9c0a1d02 3608 binder_uintptr_t cookie;
355b0502 3609 struct binder_ref *ref;
5717118f 3610 struct binder_ref_death *death = NULL;
355b0502 3611
d31f0c40 3612 if (get_user(target, (uint32_t __user *)ptr))
355b0502
GKH
3613 return -EFAULT;
3614 ptr += sizeof(uint32_t);
d31f0c40 3615 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
355b0502 3616 return -EFAULT;
9c0a1d02 3617 ptr += sizeof(binder_uintptr_t);
5717118f
TK
3618 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3619 /*
3620 * Allocate memory for death notification
3621 * before taking lock
3622 */
3623 death = kzalloc(sizeof(*death), GFP_KERNEL);
3624 if (death == NULL) {
3625 WARN_ON(thread->return_error.cmd !=
3626 BR_OK);
3627 thread->return_error.cmd = BR_ERROR;
3628 binder_enqueue_work(
3629 thread->proc,
3630 &thread->return_error.work,
3631 &thread->todo);
3632 binder_debug(
3633 BINDER_DEBUG_FAILED_TRANSACTION,
3634 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
3635 proc->pid, thread->pid);
3636 break;
3637 }
3638 }
3639 binder_proc_lock(proc);
3640 ref = binder_get_ref_olocked(proc, target, false);
355b0502 3641 if (ref == NULL) {
56b468fc 3642 binder_user_error("%d:%d %s invalid ref %d\n",
355b0502
GKH
3643 proc->pid, thread->pid,
3644 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3645 "BC_REQUEST_DEATH_NOTIFICATION" :
3646 "BC_CLEAR_DEATH_NOTIFICATION",
3647 target);
5717118f
TK
3648 binder_proc_unlock(proc);
3649 kfree(death);
355b0502
GKH
3650 break;
3651 }
3652
3653 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
9c0a1d02 3654 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
355b0502
GKH
3655 proc->pid, thread->pid,
3656 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3657 "BC_REQUEST_DEATH_NOTIFICATION" :
3658 "BC_CLEAR_DEATH_NOTIFICATION",
bc65c39a
TK
3659 (u64)cookie, ref->data.debug_id,
3660 ref->data.desc, ref->data.strong,
3661 ref->data.weak, ref->node->debug_id);
355b0502 3662
914f62a0 3663 binder_node_lock(ref->node);
355b0502
GKH
3664 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3665 if (ref->death) {
56b468fc 3666 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
355b0502 3667 proc->pid, thread->pid);
914f62a0 3668 binder_node_unlock(ref->node);
5717118f
TK
3669 binder_proc_unlock(proc);
3670 kfree(death);
355b0502
GKH
3671 break;
3672 }
3673 binder_stats_created(BINDER_STAT_DEATH);
3674 INIT_LIST_HEAD(&death->work.entry);
3675 death->cookie = cookie;
3676 ref->death = death;
3677 if (ref->node->proc == NULL) {
3678 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
aad8732c
TK
3679 if (thread->looper &
3680 (BINDER_LOOPER_STATE_REGISTERED |
3681 BINDER_LOOPER_STATE_ENTERED))
3682 binder_enqueue_work(
3683 proc,
3684 &ref->death->work,
3685 &thread->todo);
3686 else {
02545935
MC
3687 binder_inner_proc_lock(proc);
3688 binder_enqueue_work_ilocked(
aad8732c
TK
3689 &ref->death->work,
3690 &proc->todo);
02545935 3691 binder_wakeup_proc_ilocked(
1200efe9 3692 proc);
02545935 3693 binder_inner_proc_unlock(proc);
355b0502
GKH
3694 }
3695 }
3696 } else {
3697 if (ref->death == NULL) {
56b468fc 3698 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
355b0502 3699 proc->pid, thread->pid);
25de59a5 3700 binder_node_unlock(ref->node);
5717118f 3701 binder_proc_unlock(proc);
355b0502
GKH
3702 break;
3703 }
3704 death = ref->death;
3705 if (death->cookie != cookie) {
9c0a1d02 3706 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
355b0502 3707 proc->pid, thread->pid,
9c0a1d02
AH
3708 (u64)death->cookie,
3709 (u64)cookie);
25de59a5 3710 binder_node_unlock(ref->node);
5717118f 3711 binder_proc_unlock(proc);
355b0502
GKH
3712 break;
3713 }
3714 ref->death = NULL;
aad8732c 3715 binder_inner_proc_lock(proc);
355b0502
GKH
3716 if (list_empty(&death->work.entry)) {
3717 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
aad8732c
TK
3718 if (thread->looper &
3719 (BINDER_LOOPER_STATE_REGISTERED |
3720 BINDER_LOOPER_STATE_ENTERED))
3721 binder_enqueue_work_ilocked(
3722 &death->work,
3723 &thread->todo);
3724 else {
3725 binder_enqueue_work_ilocked(
3726 &death->work,
3727 &proc->todo);
02545935 3728 binder_wakeup_proc_ilocked(
1200efe9 3729 proc);
355b0502
GKH
3730 }
3731 } else {
3732 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
3733 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
3734 }
aad8732c 3735 binder_inner_proc_unlock(proc);
355b0502 3736 }
914f62a0 3737 binder_node_unlock(ref->node);
5717118f 3738 binder_proc_unlock(proc);
355b0502
GKH
3739 } break;
3740 case BC_DEAD_BINDER_DONE: {
3741 struct binder_work *w;
9c0a1d02 3742 binder_uintptr_t cookie;
355b0502 3743 struct binder_ref_death *death = NULL;
01c8cbde 3744
d31f0c40 3745 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
355b0502
GKH
3746 return -EFAULT;
3747
92716603 3748 ptr += sizeof(cookie);
aad8732c
TK
3749 binder_inner_proc_lock(proc);
3750 list_for_each_entry(w, &proc->delivered_death,
3751 entry) {
3752 struct binder_ref_death *tmp_death =
3753 container_of(w,
3754 struct binder_ref_death,
3755 work);
01c8cbde 3756
355b0502
GKH
3757 if (tmp_death->cookie == cookie) {
3758 death = tmp_death;
3759 break;
3760 }
3761 }
3762 binder_debug(BINDER_DEBUG_DEAD_BINDER,
3877fd57
DW
3763 "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n",
3764 proc->pid, thread->pid, (u64)cookie,
3765 death);
355b0502 3766 if (death == NULL) {
9c0a1d02
AH
3767 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
3768 proc->pid, thread->pid, (u64)cookie);
aad8732c 3769 binder_inner_proc_unlock(proc);
355b0502
GKH
3770 break;
3771 }
aad8732c 3772 binder_dequeue_work_ilocked(&death->work);
355b0502
GKH
3773 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
3774 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
aad8732c
TK
3775 if (thread->looper &
3776 (BINDER_LOOPER_STATE_REGISTERED |
3777 BINDER_LOOPER_STATE_ENTERED))
3778 binder_enqueue_work_ilocked(
3779 &death->work, &thread->todo);
3780 else {
3781 binder_enqueue_work_ilocked(
3782 &death->work,
3783 &proc->todo);
1200efe9 3784 binder_wakeup_proc_ilocked(proc);
355b0502
GKH
3785 }
3786 }
aad8732c 3787 binder_inner_proc_unlock(proc);
355b0502
GKH
3788 } break;
3789
3790 default:
56b468fc 3791 pr_err("%d:%d unknown command %d\n",
355b0502
GKH
3792 proc->pid, thread->pid, cmd);
3793 return -EINVAL;
3794 }
3795 *consumed = ptr - buffer;
3796 }
3797 return 0;
3798}
3799
48cd96f5
BP
3800static void binder_stat_br(struct binder_proc *proc,
3801 struct binder_thread *thread, uint32_t cmd)
355b0502 3802{
975a1ac9 3803 trace_binder_return(cmd);
7eac155d 3804 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
340b5f86
BJS
3805 atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
3806 atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
3807 atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
355b0502
GKH
3808 }
3809}
3810
355b0502
GKH
3811static int binder_has_thread_work(struct binder_thread *thread)
3812{
aad8732c
TK
3813 return !binder_worklist_empty(thread->proc, &thread->todo) ||
3814 thread->looper_need_return;
355b0502
GKH
3815}
3816
9342b49f
TK
3817static int binder_put_node_cmd(struct binder_proc *proc,
3818 struct binder_thread *thread,
3819 void __user **ptrp,
3820 binder_uintptr_t node_ptr,
3821 binder_uintptr_t node_cookie,
3822 int node_debug_id,
3823 uint32_t cmd, const char *cmd_name)
3824{
3825 void __user *ptr = *ptrp;
3826
3827 if (put_user(cmd, (uint32_t __user *)ptr))
3828 return -EFAULT;
3829 ptr += sizeof(uint32_t);
3830
3831 if (put_user(node_ptr, (binder_uintptr_t __user *)ptr))
3832 return -EFAULT;
3833 ptr += sizeof(binder_uintptr_t);
3834
3835 if (put_user(node_cookie, (binder_uintptr_t __user *)ptr))
3836 return -EFAULT;
3837 ptr += sizeof(binder_uintptr_t);
3838
3839 binder_stat_br(proc, thread, cmd);
3840 binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n",
3841 proc->pid, thread->pid, cmd_name, node_debug_id,
3842 (u64)node_ptr, (u64)node_cookie);
3843
3844 *ptrp = ptr;
3845 return 0;
3846}
3847
02545935
MC
3848static int binder_wait_for_work(struct binder_thread *thread,
3849 bool do_proc_work)
3850{
3851 DEFINE_WAIT(wait);
3852 struct binder_proc *proc = thread->proc;
3853 int ret = 0;
3854
3855 freezer_do_not_count();
3856 binder_inner_proc_lock(proc);
3857 for (;;) {
3858 prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE);
3859 if (binder_has_work_ilocked(thread, do_proc_work))
3860 break;
3861 if (do_proc_work)
3862 list_add(&thread->waiting_thread_node,
3863 &proc->waiting_threads);
3864 binder_inner_proc_unlock(proc);
3865 schedule();
3866 binder_inner_proc_lock(proc);
3867 list_del_init(&thread->waiting_thread_node);
3868 if (signal_pending(current)) {
3869 ret = -ERESTARTSYS;
3870 break;
3871 }
3872 }
3873 finish_wait(&thread->wait, &wait);
3874 binder_inner_proc_unlock(proc);
3875 freezer_count();
3876
3877 return ret;
3878}
3879
355b0502
GKH
3880static int binder_thread_read(struct binder_proc *proc,
3881 struct binder_thread *thread,
9c0a1d02
AH
3882 binder_uintptr_t binder_buffer, size_t size,
3883 binder_size_t *consumed, int non_block)
355b0502 3884{
9c0a1d02 3885 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
355b0502
GKH
3886 void __user *ptr = buffer + *consumed;
3887 void __user *end = buffer + size;
3888
3889 int ret = 0;
3890 int wait_for_proc_work;
3891
3892 if (*consumed == 0) {
d31f0c40 3893 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
355b0502
GKH
3894 return -EFAULT;
3895 ptr += sizeof(uint32_t);
3896 }
3897
3898retry:
a45c586f 3899 binder_inner_proc_lock(proc);
02545935 3900 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
a45c586f 3901 binder_inner_proc_unlock(proc);
355b0502 3902
355b0502 3903 thread->looper |= BINDER_LOOPER_STATE_WAITING;
975a1ac9 3904
975a1ac9
AH
3905 trace_binder_wait_for_work(wait_for_proc_work,
3906 !!thread->transaction_stack,
aad8732c 3907 !binder_worklist_empty(proc, &thread->todo));
355b0502
GKH
3908 if (wait_for_proc_work) {
3909 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
3910 BINDER_LOOPER_STATE_ENTERED))) {
56b468fc 3911 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
3912 proc->pid, thread->pid, thread->looper);
3913 wait_event_interruptible(binder_user_error_wait,
3914 binder_stop_on_user_error < 2);
3915 }
dadba0f5 3916 binder_restore_priority(current, proc->default_priority);
02545935
MC
3917 }
3918
3919 if (non_block) {
3920 if (!binder_has_work(thread, wait_for_proc_work))
3921 ret = -EAGAIN;
355b0502 3922 } else {
02545935 3923 ret = binder_wait_for_work(thread, wait_for_proc_work);
355b0502 3924 }
975a1ac9 3925
355b0502
GKH
3926 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
3927
3928 if (ret)
3929 return ret;
3930
3931 while (1) {
3932 uint32_t cmd;
3933 struct binder_transaction_data tr;
aad8732c
TK
3934 struct binder_work *w = NULL;
3935 struct list_head *list = NULL;
355b0502 3936 struct binder_transaction *t = NULL;
82f6ad88 3937 struct binder_thread *t_from;
355b0502 3938
ccca76b6 3939 binder_inner_proc_lock(proc);
aad8732c
TK
3940 if (!binder_worklist_empty_ilocked(&thread->todo))
3941 list = &thread->todo;
3942 else if (!binder_worklist_empty_ilocked(&proc->todo) &&
3943 wait_for_proc_work)
3944 list = &proc->todo;
3945 else {
3946 binder_inner_proc_unlock(proc);
3947
c789d154 3948 /* no data added */
85ab7b9c 3949 if (ptr - buffer == 4 && !thread->looper_need_return)
355b0502
GKH
3950 goto retry;
3951 break;
3952 }
3953
ccca76b6
TK
3954 if (end - ptr < sizeof(tr) + 4) {
3955 binder_inner_proc_unlock(proc);
355b0502 3956 break;
ccca76b6 3957 }
aad8732c 3958 w = binder_dequeue_work_head_ilocked(list);
355b0502
GKH
3959
3960 switch (w->type) {
3961 case BINDER_WORK_TRANSACTION: {
ccca76b6 3962 binder_inner_proc_unlock(proc);
355b0502
GKH
3963 t = container_of(w, struct binder_transaction, work);
3964 } break;
795aa6bc
TK
3965 case BINDER_WORK_RETURN_ERROR: {
3966 struct binder_error *e = container_of(
3967 w, struct binder_error, work);
3968
3969 WARN_ON(e->cmd == BR_OK);
ccca76b6 3970 binder_inner_proc_unlock(proc);
795aa6bc
TK
3971 if (put_user(e->cmd, (uint32_t __user *)ptr))
3972 return -EFAULT;
3973 e->cmd = BR_OK;
3974 ptr += sizeof(uint32_t);
3975
3976 binder_stat_br(proc, thread, cmd);
795aa6bc 3977 } break;
355b0502 3978 case BINDER_WORK_TRANSACTION_COMPLETE: {
ccca76b6 3979 binder_inner_proc_unlock(proc);
355b0502 3980 cmd = BR_TRANSACTION_COMPLETE;
d31f0c40 3981 if (put_user(cmd, (uint32_t __user *)ptr))
355b0502
GKH
3982 return -EFAULT;
3983 ptr += sizeof(uint32_t);
3984
3985 binder_stat_br(proc, thread, cmd);
3986 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
56b468fc 3987 "%d:%d BR_TRANSACTION_COMPLETE\n",
355b0502 3988 proc->pid, thread->pid);
355b0502
GKH
3989 kfree(w);
3990 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3991 } break;
3992 case BINDER_WORK_NODE: {
3993 struct binder_node *node = container_of(w, struct binder_node, work);
9342b49f
TK
3994 int strong, weak;
3995 binder_uintptr_t node_ptr = node->ptr;
3996 binder_uintptr_t node_cookie = node->cookie;
3997 int node_debug_id = node->debug_id;
3998 int has_weak_ref;
3999 int has_strong_ref;
4000 void __user *orig_ptr = ptr;
4001
4002 BUG_ON(proc != node->proc);
4003 strong = node->internal_strong_refs ||
4004 node->local_strong_refs;
4005 weak = !hlist_empty(&node->refs) ||
9607bf70
TK
4006 node->local_weak_refs ||
4007 node->tmp_refs || strong;
9342b49f
TK
4008 has_strong_ref = node->has_strong_ref;
4009 has_weak_ref = node->has_weak_ref;
4010
4011 if (weak && !has_weak_ref) {
355b0502
GKH
4012 node->has_weak_ref = 1;
4013 node->pending_weak_ref = 1;
4014 node->local_weak_refs++;
9342b49f
TK
4015 }
4016 if (strong && !has_strong_ref) {
355b0502
GKH
4017 node->has_strong_ref = 1;
4018 node->pending_strong_ref = 1;
4019 node->local_strong_refs++;
9342b49f
TK
4020 }
4021 if (!strong && has_strong_ref)
355b0502 4022 node->has_strong_ref = 0;
9342b49f 4023 if (!weak && has_weak_ref)
355b0502 4024 node->has_weak_ref = 0;
9342b49f
TK
4025 if (!weak && !strong) {
4026 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4027 "%d:%d node %d u%016llx c%016llx deleted\n",
4028 proc->pid, thread->pid,
4029 node_debug_id,
4030 (u64)node_ptr,
4031 (u64)node_cookie);
4032 rb_erase(&node->rb_node, &proc->nodes);
ccca76b6 4033 binder_inner_proc_unlock(proc);
25de59a5
TK
4034 binder_node_lock(node);
4035 /*
4036 * Acquire the node lock before freeing the
4037 * node to serialize with other threads that
4038 * may have been holding the node lock while
4039 * decrementing this node (avoids race where
4040 * this thread frees while the other thread
4041 * is unlocking the node after the final
4042 * decrement)
4043 */
4044 binder_node_unlock(node);
ccca76b6
TK
4045 binder_free_node(node);
4046 } else
4047 binder_inner_proc_unlock(proc);
4048
9342b49f
TK
4049 if (weak && !has_weak_ref)
4050 ret = binder_put_node_cmd(
4051 proc, thread, &ptr, node_ptr,
4052 node_cookie, node_debug_id,
4053 BR_INCREFS, "BR_INCREFS");
4054 if (!ret && strong && !has_strong_ref)
4055 ret = binder_put_node_cmd(
4056 proc, thread, &ptr, node_ptr,
4057 node_cookie, node_debug_id,
4058 BR_ACQUIRE, "BR_ACQUIRE");
4059 if (!ret && !strong && has_strong_ref)
4060 ret = binder_put_node_cmd(
4061 proc, thread, &ptr, node_ptr,
4062 node_cookie, node_debug_id,
4063 BR_RELEASE, "BR_RELEASE");
4064 if (!ret && !weak && has_weak_ref)
4065 ret = binder_put_node_cmd(
4066 proc, thread, &ptr, node_ptr,
4067 node_cookie, node_debug_id,
4068 BR_DECREFS, "BR_DECREFS");
4069 if (orig_ptr == ptr)
4070 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4071 "%d:%d node %d u%016llx c%016llx state unchanged\n",
4072 proc->pid, thread->pid,
4073 node_debug_id,
4074 (u64)node_ptr,
4075 (u64)node_cookie);
4076 if (ret)
4077 return ret;
355b0502
GKH
4078 } break;
4079 case BINDER_WORK_DEAD_BINDER:
4080 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4081 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4082 struct binder_ref_death *death;
4083 uint32_t cmd;
914f62a0 4084 binder_uintptr_t cookie;
355b0502
GKH
4085
4086 death = container_of(w, struct binder_ref_death, work);
4087 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
4088 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
4089 else
4090 cmd = BR_DEAD_BINDER;
914f62a0
MC
4091 cookie = death->cookie;
4092
355b0502 4093 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
9c0a1d02 4094 "%d:%d %s %016llx\n",
355b0502
GKH
4095 proc->pid, thread->pid,
4096 cmd == BR_DEAD_BINDER ?
4097 "BR_DEAD_BINDER" :
4098 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
914f62a0 4099 (u64)cookie);
355b0502 4100 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
914f62a0 4101 binder_inner_proc_unlock(proc);
355b0502
GKH
4102 kfree(death);
4103 binder_stats_deleted(BINDER_STAT_DEATH);
ccca76b6 4104 } else {
aad8732c
TK
4105 binder_enqueue_work_ilocked(
4106 w, &proc->delivered_death);
ccca76b6
TK
4107 binder_inner_proc_unlock(proc);
4108 }
914f62a0
MC
4109 if (put_user(cmd, (uint32_t __user *)ptr))
4110 return -EFAULT;
4111 ptr += sizeof(uint32_t);
4112 if (put_user(cookie,
4113 (binder_uintptr_t __user *)ptr))
4114 return -EFAULT;
4115 ptr += sizeof(binder_uintptr_t);
4116 binder_stat_br(proc, thread, cmd);
355b0502
GKH
4117 if (cmd == BR_DEAD_BINDER)
4118 goto done; /* DEAD_BINDER notifications can cause transactions */
4119 } break;
4120 }
4121
4122 if (!t)
4123 continue;
4124
4125 BUG_ON(t->buffer == NULL);
4126 if (t->buffer->target_node) {
4127 struct binder_node *target_node = t->buffer->target_node;
c1dd125f 4128 struct binder_priority node_prio;
01c8cbde 4129
355b0502
GKH
4130 tr.target.ptr = target_node->ptr;
4131 tr.cookie = target_node->cookie;
c1dd125f
MC
4132 node_prio.sched_policy = target_node->sched_policy;
4133 node_prio.prio = target_node->min_priority;
21dfe3eb
MC
4134 binder_transaction_priority(current, t, node_prio,
4135 target_node->inherit_rt);
355b0502
GKH
4136 cmd = BR_TRANSACTION;
4137 } else {
9c0a1d02
AH
4138 tr.target.ptr = 0;
4139 tr.cookie = 0;
355b0502
GKH
4140 cmd = BR_REPLY;
4141 }
4142 tr.code = t->code;
4143 tr.flags = t->flags;
4a2ebb93 4144 tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
355b0502 4145
82f6ad88
TK
4146 t_from = binder_get_txn_from(t);
4147 if (t_from) {
4148 struct task_struct *sender = t_from->proc->tsk;
01c8cbde 4149
355b0502 4150 tr.sender_pid = task_tgid_nr_ns(sender,
17cf22c3 4151 task_active_pid_ns(current));
355b0502
GKH
4152 } else {
4153 tr.sender_pid = 0;
4154 }
4155
4156 tr.data_size = t->buffer->data_size;
4157 tr.offsets_size = t->buffer->offsets_size;
69c33bb1
TK
4158 tr.data.ptr.buffer = (binder_uintptr_t)
4159 ((uintptr_t)t->buffer->data +
4160 binder_alloc_get_user_buffer_offset(&proc->alloc));
355b0502
GKH
4161 tr.data.ptr.offsets = tr.data.ptr.buffer +
4162 ALIGN(t->buffer->data_size,
4163 sizeof(void *));
4164
82f6ad88
TK
4165 if (put_user(cmd, (uint32_t __user *)ptr)) {
4166 if (t_from)
4167 binder_thread_dec_tmpref(t_from);
355b0502 4168 return -EFAULT;
82f6ad88 4169 }
355b0502 4170 ptr += sizeof(uint32_t);
82f6ad88
TK
4171 if (copy_to_user(ptr, &tr, sizeof(tr))) {
4172 if (t_from)
4173 binder_thread_dec_tmpref(t_from);
355b0502 4174 return -EFAULT;
82f6ad88 4175 }
355b0502
GKH
4176 ptr += sizeof(tr);
4177
975a1ac9 4178 trace_binder_transaction_received(t);
355b0502
GKH
4179 binder_stat_br(proc, thread, cmd);
4180 binder_debug(BINDER_DEBUG_TRANSACTION,
9c0a1d02 4181 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
355b0502
GKH
4182 proc->pid, thread->pid,
4183 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
4184 "BR_REPLY",
82f6ad88
TK
4185 t->debug_id, t_from ? t_from->proc->pid : 0,
4186 t_from ? t_from->pid : 0, cmd,
355b0502 4187 t->buffer->data_size, t->buffer->offsets_size,
9c0a1d02 4188 (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
355b0502 4189
82f6ad88
TK
4190 if (t_from)
4191 binder_thread_dec_tmpref(t_from);
355b0502
GKH
4192 t->buffer->allow_user_free = 1;
4193 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
a45c586f 4194 binder_inner_proc_lock(thread->proc);
355b0502
GKH
4195 t->to_parent = thread->transaction_stack;
4196 t->to_thread = thread;
4197 thread->transaction_stack = t;
a45c586f 4198 binder_inner_proc_unlock(thread->proc);
355b0502 4199 } else {
7323366b 4200 binder_free_transaction(t);
355b0502
GKH
4201 }
4202 break;
4203 }
4204
4205done:
4206
4207 *consumed = ptr - buffer;
e09d7b9a 4208 binder_inner_proc_lock(proc);
02545935
MC
4209 if (proc->requested_threads == 0 &&
4210 list_empty(&thread->proc->waiting_threads) &&
355b0502
GKH
4211 proc->requested_threads_started < proc->max_threads &&
4212 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4213 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
4214 /*spawn a new thread if we leave this out */) {
4215 proc->requested_threads++;
e09d7b9a 4216 binder_inner_proc_unlock(proc);
355b0502 4217 binder_debug(BINDER_DEBUG_THREADS,
56b468fc 4218 "%d:%d BR_SPAWN_LOOPER\n",
355b0502 4219 proc->pid, thread->pid);
d31f0c40 4220 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
355b0502 4221 return -EFAULT;
89334ab4 4222 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
e09d7b9a
TK
4223 } else
4224 binder_inner_proc_unlock(proc);
355b0502
GKH
4225 return 0;
4226}
4227
aad8732c
TK
4228static void binder_release_work(struct binder_proc *proc,
4229 struct list_head *list)
355b0502
GKH
4230{
4231 struct binder_work *w;
01c8cbde 4232
aad8732c
TK
4233 while (1) {
4234 w = binder_dequeue_work_head(proc, list);
4235 if (!w)
4236 return;
4237
355b0502
GKH
4238 switch (w->type) {
4239 case BINDER_WORK_TRANSACTION: {
4240 struct binder_transaction *t;
4241
4242 t = container_of(w, struct binder_transaction, work);
675d66b0
AH
4243 if (t->buffer->target_node &&
4244 !(t->flags & TF_ONE_WAY)) {
355b0502 4245 binder_send_failed_reply(t, BR_DEAD_REPLY);
675d66b0
AH
4246 } else {
4247 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
56b468fc 4248 "undelivered transaction %d\n",
675d66b0 4249 t->debug_id);
7323366b 4250 binder_free_transaction(t);
675d66b0 4251 }
355b0502 4252 } break;
795aa6bc
TK
4253 case BINDER_WORK_RETURN_ERROR: {
4254 struct binder_error *e = container_of(
4255 w, struct binder_error, work);
4256
4257 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4258 "undelivered TRANSACTION_ERROR: %u\n",
4259 e->cmd);
4260 } break;
355b0502 4261 case BINDER_WORK_TRANSACTION_COMPLETE: {
675d66b0 4262 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
56b468fc 4263 "undelivered TRANSACTION_COMPLETE\n");
355b0502
GKH
4264 kfree(w);
4265 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4266 } break;
675d66b0
AH
4267 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4268 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4269 struct binder_ref_death *death;
4270
4271 death = container_of(w, struct binder_ref_death, work);
4272 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
9c0a1d02
AH
4273 "undelivered death notification, %016llx\n",
4274 (u64)death->cookie);
675d66b0
AH
4275 kfree(death);
4276 binder_stats_deleted(BINDER_STAT_DEATH);
4277 } break;
355b0502 4278 default:
56b468fc 4279 pr_err("unexpected work type, %d, not freed\n",
675d66b0 4280 w->type);
355b0502
GKH
4281 break;
4282 }
4283 }
4284
4285}
4286
aed01731
TK
4287static struct binder_thread *binder_get_thread_ilocked(
4288 struct binder_proc *proc, struct binder_thread *new_thread)
355b0502
GKH
4289{
4290 struct binder_thread *thread = NULL;
4291 struct rb_node *parent = NULL;
4292 struct rb_node **p = &proc->threads.rb_node;
4293
4294 while (*p) {
4295 parent = *p;
4296 thread = rb_entry(parent, struct binder_thread, rb_node);
4297
4298 if (current->pid < thread->pid)
4299 p = &(*p)->rb_left;
4300 else if (current->pid > thread->pid)
4301 p = &(*p)->rb_right;
4302 else
aed01731 4303 return thread;
355b0502 4304 }
aed01731
TK
4305 if (!new_thread)
4306 return NULL;
4307 thread = new_thread;
4308 binder_stats_created(BINDER_STAT_THREAD);
4309 thread->proc = proc;
4310 thread->pid = current->pid;
c1dd125f
MC
4311 get_task_struct(current);
4312 thread->task = current;
aed01731
TK
4313 atomic_set(&thread->tmp_ref, 0);
4314 init_waitqueue_head(&thread->wait);
4315 INIT_LIST_HEAD(&thread->todo);
4316 rb_link_node(&thread->rb_node, parent, p);
4317 rb_insert_color(&thread->rb_node, &proc->threads);
4318 thread->looper_need_return = true;
4319 thread->return_error.work.type = BINDER_WORK_RETURN_ERROR;
4320 thread->return_error.cmd = BR_OK;
4321 thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR;
4322 thread->reply_error.cmd = BR_OK;
02545935 4323 INIT_LIST_HEAD(&new_thread->waiting_thread_node);
aed01731
TK
4324 return thread;
4325}
4326
4327static struct binder_thread *binder_get_thread(struct binder_proc *proc)
4328{
4329 struct binder_thread *thread;
4330 struct binder_thread *new_thread;
4331
4332 binder_inner_proc_lock(proc);
4333 thread = binder_get_thread_ilocked(proc, NULL);
4334 binder_inner_proc_unlock(proc);
4335 if (!thread) {
4336 new_thread = kzalloc(sizeof(*thread), GFP_KERNEL);
4337 if (new_thread == NULL)
355b0502 4338 return NULL;
aed01731
TK
4339 binder_inner_proc_lock(proc);
4340 thread = binder_get_thread_ilocked(proc, new_thread);
4341 binder_inner_proc_unlock(proc);
4342 if (thread != new_thread)
4343 kfree(new_thread);
355b0502
GKH
4344 }
4345 return thread;
4346}
4347
82f6ad88
TK
4348static void binder_free_proc(struct binder_proc *proc)
4349{
4350 BUG_ON(!list_empty(&proc->todo));
4351 BUG_ON(!list_empty(&proc->delivered_death));
4352 binder_alloc_deferred_release(&proc->alloc);
4353 put_task_struct(proc->tsk);
4354 binder_stats_deleted(BINDER_STAT_PROC);
4355 kfree(proc);
4356}
4357
4358static void binder_free_thread(struct binder_thread *thread)
4359{
4360 BUG_ON(!list_empty(&thread->todo));
4361 binder_stats_deleted(BINDER_STAT_THREAD);
4362 binder_proc_dec_tmpref(thread->proc);
c1dd125f 4363 put_task_struct(thread->task);
82f6ad88
TK
4364 kfree(thread);
4365}
4366
4367static int binder_thread_release(struct binder_proc *proc,
4368 struct binder_thread *thread)
355b0502
GKH
4369{
4370 struct binder_transaction *t;
4371 struct binder_transaction *send_reply = NULL;
4372 int active_transactions = 0;
82f6ad88 4373 struct binder_transaction *last_t = NULL;
355b0502 4374
aed01731 4375 binder_inner_proc_lock(thread->proc);
82f6ad88
TK
4376 /*
4377 * take a ref on the proc so it survives
4378 * after we remove this thread from proc->threads.
4379 * The corresponding dec is when we actually
4380 * free the thread in binder_free_thread()
4381 */
4382 proc->tmp_ref++;
4383 /*
4384 * take a ref on this thread to ensure it
4385 * survives while we are releasing it
4386 */
4387 atomic_inc(&thread->tmp_ref);
355b0502
GKH
4388 rb_erase(&thread->rb_node, &proc->threads);
4389 t = thread->transaction_stack;
82f6ad88
TK
4390 if (t) {
4391 spin_lock(&t->lock);
4392 if (t->to_thread == thread)
4393 send_reply = t;
4394 }
4395 thread->is_dead = true;
4396
355b0502 4397 while (t) {
82f6ad88 4398 last_t = t;
355b0502
GKH
4399 active_transactions++;
4400 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
56b468fc
AS
4401 "release %d:%d transaction %d %s, still active\n",
4402 proc->pid, thread->pid,
355b0502
GKH
4403 t->debug_id,
4404 (t->to_thread == thread) ? "in" : "out");
4405
4406 if (t->to_thread == thread) {
4407 t->to_proc = NULL;
4408 t->to_thread = NULL;
4409 if (t->buffer) {
4410 t->buffer->transaction = NULL;
4411 t->buffer = NULL;
4412 }
4413 t = t->to_parent;
4414 } else if (t->from == thread) {
4415 t->from = NULL;
4416 t = t->from_parent;
4417 } else
4418 BUG();
82f6ad88
TK
4419 spin_unlock(&last_t->lock);
4420 if (t)
4421 spin_lock(&t->lock);
355b0502 4422 }
aed01731 4423 binder_inner_proc_unlock(thread->proc);
82f6ad88 4424
355b0502
GKH
4425 if (send_reply)
4426 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
aad8732c 4427 binder_release_work(proc, &thread->todo);
82f6ad88 4428 binder_thread_dec_tmpref(thread);
355b0502
GKH
4429 return active_transactions;
4430}
4431
4432static unsigned int binder_poll(struct file *filp,
4433 struct poll_table_struct *wait)
4434{
4435 struct binder_proc *proc = filp->private_data;
4436 struct binder_thread *thread = NULL;
02545935 4437 bool wait_for_proc_work;
355b0502 4438
355b0502
GKH
4439 thread = binder_get_thread(proc);
4440
a45c586f 4441 binder_inner_proc_lock(thread->proc);
02545935
MC
4442 thread->looper |= BINDER_LOOPER_STATE_POLL;
4443 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
4444
a45c586f 4445 binder_inner_proc_unlock(thread->proc);
975a1ac9 4446
02545935
MC
4447 if (binder_has_work(thread, wait_for_proc_work))
4448 return POLLIN;
4449
4450 poll_wait(filp, &thread->wait, wait);
4451
4452 if (binder_has_thread_work(thread))
4453 return POLLIN;
4454
355b0502
GKH
4455 return 0;
4456}
4457
74961582
RA
4458static int binder_ioctl_write_read(struct file *filp,
4459 unsigned int cmd, unsigned long arg,
4460 struct binder_thread *thread)
4461{
4462 int ret = 0;
4463 struct binder_proc *proc = filp->private_data;
4464 unsigned int size = _IOC_SIZE(cmd);
4465 void __user *ubuf = (void __user *)arg;
4466 struct binder_write_read bwr;
4467
4468 if (size != sizeof(struct binder_write_read)) {
4469 ret = -EINVAL;
4470 goto out;
4471 }
d31f0c40 4472 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
74961582
RA
4473 ret = -EFAULT;
4474 goto out;
4475 }
4476 binder_debug(BINDER_DEBUG_READ_WRITE,
4477 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
4478 proc->pid, thread->pid,
4479 (u64)bwr.write_size, (u64)bwr.write_buffer,
4480 (u64)bwr.read_size, (u64)bwr.read_buffer);
4481
4482 if (bwr.write_size > 0) {
4483 ret = binder_thread_write(proc, thread,
4484 bwr.write_buffer,
4485 bwr.write_size,
4486 &bwr.write_consumed);
4487 trace_binder_write_done(ret);
4488 if (ret < 0) {
4489 bwr.read_consumed = 0;
d31f0c40 4490 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
74961582
RA
4491 ret = -EFAULT;
4492 goto out;
4493 }
4494 }
4495 if (bwr.read_size > 0) {
4496 ret = binder_thread_read(proc, thread, bwr.read_buffer,
4497 bwr.read_size,
4498 &bwr.read_consumed,
4499 filp->f_flags & O_NONBLOCK);
4500 trace_binder_read_done(ret);
02545935
MC
4501 binder_inner_proc_lock(proc);
4502 if (!binder_worklist_empty_ilocked(&proc->todo))
1200efe9 4503 binder_wakeup_proc_ilocked(proc);
02545935 4504 binder_inner_proc_unlock(proc);
74961582 4505 if (ret < 0) {
d31f0c40 4506 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
74961582
RA
4507 ret = -EFAULT;
4508 goto out;
4509 }
4510 }
4511 binder_debug(BINDER_DEBUG_READ_WRITE,
4512 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
4513 proc->pid, thread->pid,
4514 (u64)bwr.write_consumed, (u64)bwr.write_size,
4515 (u64)bwr.read_consumed, (u64)bwr.read_size);
d31f0c40 4516 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
74961582
RA
4517 ret = -EFAULT;
4518 goto out;
4519 }
4520out:
4521 return ret;
4522}
4523
4524static int binder_ioctl_set_ctx_mgr(struct file *filp)
4525{
4526 int ret = 0;
4527 struct binder_proc *proc = filp->private_data;
913926d0 4528 struct binder_context *context = proc->context;
76341f6f 4529 struct binder_node *new_node;
74961582
RA
4530 kuid_t curr_euid = current_euid();
4531
76341f6f 4532 mutex_lock(&context->context_mgr_node_lock);
913926d0 4533 if (context->binder_context_mgr_node) {
74961582
RA
4534 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
4535 ret = -EBUSY;
4536 goto out;
4537 }
3b090f5e
S
4538 ret = security_binder_set_context_mgr(proc->tsk);
4539 if (ret < 0)
4540 goto out;
913926d0
MC
4541 if (uid_valid(context->binder_context_mgr_uid)) {
4542 if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
74961582
RA
4543 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
4544 from_kuid(&init_user_ns, curr_euid),
4545 from_kuid(&init_user_ns,
913926d0 4546 context->binder_context_mgr_uid));
74961582
RA
4547 ret = -EPERM;
4548 goto out;
4549 }
4550 } else {
913926d0 4551 context->binder_context_mgr_uid = curr_euid;
74961582 4552 }
25de59a5 4553 new_node = binder_new_node(proc, NULL);
76341f6f 4554 if (!new_node) {
74961582
RA
4555 ret = -ENOMEM;
4556 goto out;
4557 }
25de59a5 4558 binder_node_lock(new_node);
76341f6f
TK
4559 new_node->local_weak_refs++;
4560 new_node->local_strong_refs++;
4561 new_node->has_strong_ref = 1;
4562 new_node->has_weak_ref = 1;
4563 context->binder_context_mgr_node = new_node;
25de59a5 4564 binder_node_unlock(new_node);
9607bf70 4565 binder_put_node(new_node);
74961582 4566out:
76341f6f 4567 mutex_unlock(&context->context_mgr_node_lock);
74961582
RA
4568 return ret;
4569}
4570
66266ad3
CC
4571static int binder_ioctl_get_node_debug_info(struct binder_proc *proc,
4572 struct binder_node_debug_info *info) {
4573 struct rb_node *n;
4574 binder_uintptr_t ptr = info->ptr;
4575
4576 memset(info, 0, sizeof(*info));
4577
4578 binder_inner_proc_lock(proc);
4579 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
4580 struct binder_node *node = rb_entry(n, struct binder_node,
4581 rb_node);
4582 if (node->ptr > ptr) {
4583 info->ptr = node->ptr;
4584 info->cookie = node->cookie;
4585 info->has_strong_ref = node->has_strong_ref;
4586 info->has_weak_ref = node->has_weak_ref;
4587 break;
4588 }
4589 }
4590 binder_inner_proc_unlock(proc);
4591
4592 return 0;
4593}
4594
355b0502
GKH
4595static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4596{
4597 int ret;
4598 struct binder_proc *proc = filp->private_data;
4599 struct binder_thread *thread;
4600 unsigned int size = _IOC_SIZE(cmd);
4601 void __user *ubuf = (void __user *)arg;
4602
74961582
RA
4603 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
4604 proc->pid, current->pid, cmd, arg);*/
355b0502 4605
975a1ac9
AH
4606 trace_binder_ioctl(cmd, arg);
4607
355b0502
GKH
4608 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4609 if (ret)
975a1ac9 4610 goto err_unlocked;
355b0502 4611
355b0502
GKH
4612 thread = binder_get_thread(proc);
4613 if (thread == NULL) {
4614 ret = -ENOMEM;
4615 goto err;
4616 }
4617
4618 switch (cmd) {
74961582
RA
4619 case BINDER_WRITE_READ:
4620 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
4621 if (ret)
355b0502 4622 goto err;
355b0502 4623 break;
e09d7b9a
TK
4624 case BINDER_SET_MAX_THREADS: {
4625 int max_threads;
4626
4627 if (copy_from_user(&max_threads, ubuf,
4628 sizeof(max_threads))) {
355b0502
GKH
4629 ret = -EINVAL;
4630 goto err;
4631 }
e09d7b9a
TK
4632 binder_inner_proc_lock(proc);
4633 proc->max_threads = max_threads;
4634 binder_inner_proc_unlock(proc);
355b0502 4635 break;
e09d7b9a 4636 }
355b0502 4637 case BINDER_SET_CONTEXT_MGR:
74961582
RA
4638 ret = binder_ioctl_set_ctx_mgr(filp);
4639 if (ret)
355b0502 4640 goto err;
355b0502
GKH
4641 break;
4642 case BINDER_THREAD_EXIT:
56b468fc 4643 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
355b0502 4644 proc->pid, thread->pid);
82f6ad88 4645 binder_thread_release(proc, thread);
355b0502
GKH
4646 thread = NULL;
4647 break;
1b4ce188
MM
4648 case BINDER_VERSION: {
4649 struct binder_version __user *ver = ubuf;
4650
355b0502
GKH
4651 if (size != sizeof(struct binder_version)) {
4652 ret = -EINVAL;
4653 goto err;
4654 }
d31f0c40 4655 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
1b4ce188 4656 &ver->protocol_version)) {
355b0502
GKH
4657 ret = -EINVAL;
4658 goto err;
4659 }
4660 break;
1b4ce188 4661 }
66266ad3
CC
4662 case BINDER_GET_NODE_DEBUG_INFO: {
4663 struct binder_node_debug_info info;
4664
4665 if (copy_from_user(&info, ubuf, sizeof(info))) {
4666 ret = -EFAULT;
4667 goto err;
4668 }
4669
4670 ret = binder_ioctl_get_node_debug_info(proc, &info);
4671 if (ret < 0)
4672 goto err;
4673
4674 if (copy_to_user(ubuf, &info, sizeof(info))) {
4675 ret = -EFAULT;
4676 goto err;
4677 }
4678 break;
4679 }
355b0502
GKH
4680 default:
4681 ret = -EINVAL;
4682 goto err;
4683 }
4684 ret = 0;
4685err:
4686 if (thread)
85ab7b9c 4687 thread->looper_need_return = false;
355b0502
GKH
4688 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4689 if (ret && ret != -ERESTARTSYS)
56b468fc 4690 pr_info("%d:%d ioctl %x %lx returned %d\n", proc->pid, current->pid, cmd, arg, ret);
975a1ac9
AH
4691err_unlocked:
4692 trace_binder_ioctl_done(ret);
355b0502
GKH
4693 return ret;
4694}
4695
4696static void binder_vma_open(struct vm_area_struct *vma)
4697{
4698 struct binder_proc *proc = vma->vm_private_data;
01c8cbde 4699
355b0502 4700 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
56b468fc 4701 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
355b0502
GKH
4702 proc->pid, vma->vm_start, vma->vm_end,
4703 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4704 (unsigned long)pgprot_val(vma->vm_page_prot));
355b0502
GKH
4705}
4706
4707static void binder_vma_close(struct vm_area_struct *vma)
4708{
4709 struct binder_proc *proc = vma->vm_private_data;
01c8cbde 4710
355b0502 4711 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
56b468fc 4712 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
355b0502
GKH
4713 proc->pid, vma->vm_start, vma->vm_end,
4714 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4715 (unsigned long)pgprot_val(vma->vm_page_prot));
69c33bb1 4716 binder_alloc_vma_close(&proc->alloc);
355b0502
GKH
4717 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
4718}
4719
430f7ee8
VM
4720static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
4721{
4722 return VM_FAULT_SIGBUS;
4723}
4724
355b0502
GKH
4725static struct vm_operations_struct binder_vm_ops = {
4726 .open = binder_vma_open,
4727 .close = binder_vma_close,
430f7ee8 4728 .fault = binder_vm_fault,
355b0502
GKH
4729};
4730
69c33bb1
TK
4731static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
4732{
4733 int ret;
4734 struct binder_proc *proc = filp->private_data;
4735 const char *failure_string;
4736
4737 if (proc->tsk != current->group_leader)
4738 return -EINVAL;
4739
4740 if ((vma->vm_end - vma->vm_start) > SZ_4M)
4741 vma->vm_end = vma->vm_start + SZ_4M;
4742
4743 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4744 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
4745 __func__, proc->pid, vma->vm_start, vma->vm_end,
4746 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4747 (unsigned long)pgprot_val(vma->vm_page_prot));
4748
4749 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
4750 ret = -EPERM;
4751 failure_string = "bad vm_flags";
4752 goto err_bad_arg;
4753 }
4754 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
4755 vma->vm_ops = &binder_vm_ops;
4756 vma->vm_private_data = proc;
4757
4758 ret = binder_alloc_mmap_handler(&proc->alloc, vma);
4759 if (ret)
4760 return ret;
4761 proc->files = get_files_struct(current);
4762 return 0;
4763
355b0502 4764err_bad_arg:
258767fe 4765 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
355b0502
GKH
4766 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
4767 return ret;
4768}
4769
4770static int binder_open(struct inode *nodp, struct file *filp)
4771{
4772 struct binder_proc *proc;
bfd99b42 4773 struct binder_device *binder_dev;
355b0502
GKH
4774
4775 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
4776 current->group_leader->pid, current->pid);
4777
4778 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
4779 if (proc == NULL)
4780 return -ENOMEM;
751c6a8e
TK
4781 spin_lock_init(&proc->inner_lock);
4782 spin_lock_init(&proc->outer_lock);
f43b3d58
TK
4783 get_task_struct(current->group_leader);
4784 proc->tsk = current->group_leader;
355b0502 4785 INIT_LIST_HEAD(&proc->todo);
425bb8fc
MC
4786 if (binder_supported_policy(current->policy)) {
4787 proc->default_priority.sched_policy = current->policy;
4788 proc->default_priority.prio = current->normal_prio;
4789 } else {
4790 proc->default_priority.sched_policy = SCHED_NORMAL;
4791 proc->default_priority.prio = NICE_TO_PRIO(0);
4792 }
4793
bfd99b42
MC
4794 binder_dev = container_of(filp->private_data, struct binder_device,
4795 miscdev);
4796 proc->context = &binder_dev->context;
69c33bb1 4797 binder_alloc_init(&proc->alloc);
975a1ac9 4798
355b0502 4799 binder_stats_created(BINDER_STAT_PROC);
355b0502
GKH
4800 proc->pid = current->group_leader->pid;
4801 INIT_LIST_HEAD(&proc->delivered_death);
02545935 4802 INIT_LIST_HEAD(&proc->waiting_threads);
355b0502 4803 filp->private_data = proc;
975a1ac9 4804
76341f6f
TK
4805 mutex_lock(&binder_procs_lock);
4806 hlist_add_head(&proc->proc_node, &binder_procs);
4807 mutex_unlock(&binder_procs_lock);
4808
16b66554 4809 if (binder_debugfs_dir_entry_proc) {
355b0502 4810 char strbuf[11];
01c8cbde 4811
355b0502 4812 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
a64af2cf
MC
4813 /*
4814 * proc debug entries are shared between contexts, so
4815 * this will fail if the process tries to open the driver
4816 * again with a different context. The priting code will
4817 * anyway print all contexts that a given PID has, so this
4818 * is not a problem.
4819 */
16b66554 4820 proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
a64af2cf
MC
4821 binder_debugfs_dir_entry_proc,
4822 (void *)(unsigned long)proc->pid,
4823 &binder_proc_fops);
355b0502
GKH
4824 }
4825
4826 return 0;
4827}
4828
4829static int binder_flush(struct file *filp, fl_owner_t id)
4830{
4831 struct binder_proc *proc = filp->private_data;
4832
4833 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
4834
4835 return 0;
4836}
4837
4838static void binder_deferred_flush(struct binder_proc *proc)
4839{
4840 struct rb_node *n;
4841 int wake_count = 0;
01c8cbde 4842
aed01731 4843 binder_inner_proc_lock(proc);
355b0502
GKH
4844 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
4845 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
01c8cbde 4846
85ab7b9c 4847 thread->looper_need_return = true;
355b0502
GKH
4848 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
4849 wake_up_interruptible(&thread->wait);
4850 wake_count++;
4851 }
4852 }
aed01731 4853 binder_inner_proc_unlock(proc);
355b0502
GKH
4854
4855 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4856 "binder_flush: %d woke %d threads\n", proc->pid,
4857 wake_count);
4858}
4859
4860static int binder_release(struct inode *nodp, struct file *filp)
4861{
4862 struct binder_proc *proc = filp->private_data;
01c8cbde 4863
16b66554 4864 debugfs_remove(proc->debugfs_entry);
355b0502
GKH
4865 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
4866
4867 return 0;
4868}
4869
008fa749
ME
4870static int binder_node_release(struct binder_node *node, int refs)
4871{
4872 struct binder_ref *ref;
4873 int death = 0;
ccca76b6 4874 struct binder_proc *proc = node->proc;
008fa749 4875
aad8732c 4876 binder_release_work(proc, &node->async_todo);
ccca76b6 4877
25de59a5 4878 binder_node_lock(node);
ccca76b6 4879 binder_inner_proc_lock(proc);
aad8732c 4880 binder_dequeue_work_ilocked(&node->work);
9607bf70
TK
4881 /*
4882 * The caller must have taken a temporary ref on the node,
4883 */
4884 BUG_ON(!node->tmp_refs);
4885 if (hlist_empty(&node->refs) && node->tmp_refs == 1) {
ccca76b6 4886 binder_inner_proc_unlock(proc);
25de59a5 4887 binder_node_unlock(node);
ccca76b6 4888 binder_free_node(node);
008fa749
ME
4889
4890 return refs;
4891 }
4892
4893 node->proc = NULL;
4894 node->local_strong_refs = 0;
4895 node->local_weak_refs = 0;
ccca76b6 4896 binder_inner_proc_unlock(proc);
76341f6f
TK
4897
4898 spin_lock(&binder_dead_nodes_lock);
7eac155d 4899 hlist_add_head(&node->dead_node, &binder_dead_nodes);
76341f6f 4900 spin_unlock(&binder_dead_nodes_lock);
008fa749
ME
4901
4902 hlist_for_each_entry(ref, &node->refs, node_entry) {
4903 refs++;
914f62a0
MC
4904 /*
4905 * Need the node lock to synchronize
4906 * with new notification requests and the
4907 * inner lock to synchronize with queued
4908 * death notifications.
4909 */
4910 binder_inner_proc_lock(ref->proc);
4911 if (!ref->death) {
4912 binder_inner_proc_unlock(ref->proc);
b3a59abf 4913 continue;
914f62a0 4914 }
008fa749
ME
4915
4916 death++;
4917
914f62a0
MC
4918 BUG_ON(!list_empty(&ref->death->work.entry));
4919 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
4920 binder_enqueue_work_ilocked(&ref->death->work,
4921 &ref->proc->todo);
1200efe9 4922 binder_wakeup_proc_ilocked(ref->proc);
aad8732c 4923 binder_inner_proc_unlock(ref->proc);
008fa749
ME
4924 }
4925
008fa749
ME
4926 binder_debug(BINDER_DEBUG_DEAD_BINDER,
4927 "node %d now dead, refs %d, death %d\n",
4928 node->debug_id, refs, death);
25de59a5 4929 binder_node_unlock(node);
9607bf70 4930 binder_put_node(node);
008fa749
ME
4931
4932 return refs;
4933}
4934
355b0502
GKH
4935static void binder_deferred_release(struct binder_proc *proc)
4936{
913926d0 4937 struct binder_context *context = proc->context;
355b0502 4938 struct rb_node *n;
69c33bb1 4939 int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
355b0502 4940
355b0502
GKH
4941 BUG_ON(proc->files);
4942
76341f6f 4943 mutex_lock(&binder_procs_lock);
355b0502 4944 hlist_del(&proc->proc_node);
76341f6f 4945 mutex_unlock(&binder_procs_lock);
53413e7d 4946
76341f6f 4947 mutex_lock(&context->context_mgr_node_lock);
913926d0
MC
4948 if (context->binder_context_mgr_node &&
4949 context->binder_context_mgr_node->proc == proc) {
355b0502 4950 binder_debug(BINDER_DEBUG_DEAD_BINDER,
c07c933f
ME
4951 "%s: %d context_mgr_node gone\n",
4952 __func__, proc->pid);
913926d0 4953 context->binder_context_mgr_node = NULL;
355b0502 4954 }
76341f6f 4955 mutex_unlock(&context->context_mgr_node_lock);
aed01731 4956 binder_inner_proc_lock(proc);
82f6ad88
TK
4957 /*
4958 * Make sure proc stays alive after we
4959 * remove all the threads
4960 */
4961 proc->tmp_ref++;
355b0502 4962
82f6ad88 4963 proc->is_dead = true;
355b0502
GKH
4964 threads = 0;
4965 active_transactions = 0;
4966 while ((n = rb_first(&proc->threads))) {
53413e7d
ME
4967 struct binder_thread *thread;
4968
4969 thread = rb_entry(n, struct binder_thread, rb_node);
aed01731 4970 binder_inner_proc_unlock(proc);
355b0502 4971 threads++;
82f6ad88 4972 active_transactions += binder_thread_release(proc, thread);
aed01731 4973 binder_inner_proc_lock(proc);
355b0502 4974 }
53413e7d 4975
355b0502
GKH
4976 nodes = 0;
4977 incoming_refs = 0;
4978 while ((n = rb_first(&proc->nodes))) {
53413e7d 4979 struct binder_node *node;
355b0502 4980
53413e7d 4981 node = rb_entry(n, struct binder_node, rb_node);
355b0502 4982 nodes++;
9607bf70
TK
4983 /*
4984 * take a temporary ref on the node before
4985 * calling binder_node_release() which will either
4986 * kfree() the node or call binder_put_node()
4987 */
ceeaf28f 4988 binder_inc_node_tmpref_ilocked(node);
355b0502 4989 rb_erase(&node->rb_node, &proc->nodes);
ceeaf28f 4990 binder_inner_proc_unlock(proc);
7eac155d 4991 incoming_refs = binder_node_release(node, incoming_refs);
ceeaf28f 4992 binder_inner_proc_lock(proc);
355b0502 4993 }
ceeaf28f 4994 binder_inner_proc_unlock(proc);
53413e7d 4995
355b0502 4996 outgoing_refs = 0;
5717118f 4997 binder_proc_lock(proc);
355b0502 4998 while ((n = rb_first(&proc->refs_by_desc))) {
53413e7d
ME
4999 struct binder_ref *ref;
5000
5001 ref = rb_entry(n, struct binder_ref, rb_node_desc);
355b0502 5002 outgoing_refs++;
5717118f
TK
5003 binder_cleanup_ref_olocked(ref);
5004 binder_proc_unlock(proc);
bc65c39a 5005 binder_free_ref(ref);
5717118f 5006 binder_proc_lock(proc);
355b0502 5007 }
5717118f 5008 binder_proc_unlock(proc);
53413e7d 5009
aad8732c
TK
5010 binder_release_work(proc, &proc->todo);
5011 binder_release_work(proc, &proc->delivered_death);
355b0502 5012
355b0502 5013 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
69c33bb1 5014 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
c07c933f 5015 __func__, proc->pid, threads, nodes, incoming_refs,
69c33bb1 5016 outgoing_refs, active_transactions);
355b0502 5017
82f6ad88 5018 binder_proc_dec_tmpref(proc);
355b0502
GKH
5019}
5020
5021static void binder_deferred_func(struct work_struct *work)
5022{
5023 struct binder_proc *proc;
5024 struct files_struct *files;
5025
5026 int defer;
01c8cbde 5027
355b0502 5028 do {
7eac155d
DW
5029 mutex_lock(&binder_deferred_lock);
5030 if (!hlist_empty(&binder_deferred_list)) {
5031 proc = hlist_entry(binder_deferred_list.first,
5032 struct binder_proc, deferred_work_node);
355b0502
GKH
5033 hlist_del_init(&proc->deferred_work_node);
5034 defer = proc->deferred_work;
5035 proc->deferred_work = 0;
5036 } else {
5037 proc = NULL;
5038 defer = 0;
5039 }
7eac155d 5040 mutex_unlock(&binder_deferred_lock);
355b0502
GKH
5041
5042 files = NULL;
5043 if (defer & BINDER_DEFERRED_PUT_FILES) {
5044 files = proc->files;
5045 if (files)
5046 proc->files = NULL;
5047 }
5048
5049 if (defer & BINDER_DEFERRED_FLUSH)
5050 binder_deferred_flush(proc);
5051
5052 if (defer & BINDER_DEFERRED_RELEASE)
5053 binder_deferred_release(proc); /* frees proc */
5054
355b0502
GKH
5055 if (files)
5056 put_files_struct(files);
5057 } while (proc);
5058}
7eac155d 5059static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
355b0502
GKH
5060
5061static void
5062binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
5063{
7eac155d 5064 mutex_lock(&binder_deferred_lock);
355b0502
GKH
5065 proc->deferred_work |= defer;
5066 if (hlist_unhashed(&proc->deferred_work_node)) {
5067 hlist_add_head(&proc->deferred_work_node,
7eac155d
DW
5068 &binder_deferred_list);
5069 queue_work(binder_deferred_workqueue, &binder_deferred_work);
355b0502 5070 }
7eac155d 5071 mutex_unlock(&binder_deferred_lock);
355b0502
GKH
5072}
5073
c2bdc4c4
TK
5074static void print_binder_transaction_ilocked(struct seq_file *m,
5075 struct binder_proc *proc,
5076 const char *prefix,
5077 struct binder_transaction *t)
5249f488 5078{
c2bdc4c4
TK
5079 struct binder_proc *to_proc;
5080 struct binder_buffer *buffer = t->buffer;
5081
5082 WARN_ON(!spin_is_locked(&proc->inner_lock));
82f6ad88 5083 spin_lock(&t->lock);
c2bdc4c4 5084 to_proc = t->to_proc;
5249f488 5085 seq_printf(m,
425bb8fc 5086 "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %d:%d r%d",
5249f488
AH
5087 prefix, t->debug_id, t,
5088 t->from ? t->from->proc->pid : 0,
5089 t->from ? t->from->pid : 0,
c2bdc4c4 5090 to_proc ? to_proc->pid : 0,
5249f488 5091 t->to_thread ? t->to_thread->pid : 0,
425bb8fc
MC
5092 t->code, t->flags, t->priority.sched_policy,
5093 t->priority.prio, t->need_reply);
82f6ad88
TK
5094 spin_unlock(&t->lock);
5095
c2bdc4c4
TK
5096 if (proc != to_proc) {
5097 /*
5098 * Can only safely deref buffer if we are holding the
5099 * correct proc inner lock for this node
5100 */
5101 seq_puts(m, "\n");
5102 return;
5103 }
5104
5105 if (buffer == NULL) {
5249f488
AH
5106 seq_puts(m, " buffer free\n");
5107 return;
355b0502 5108 }
c2bdc4c4
TK
5109 if (buffer->target_node)
5110 seq_printf(m, " node %d", buffer->target_node->debug_id);
3877fd57 5111 seq_printf(m, " size %zd:%zd data %p\n",
c2bdc4c4
TK
5112 buffer->data_size, buffer->offsets_size,
5113 buffer->data);
355b0502
GKH
5114}
5115
c2bdc4c4
TK
5116static void print_binder_work_ilocked(struct seq_file *m,
5117 struct binder_proc *proc,
5118 const char *prefix,
5119 const char *transaction_prefix,
5120 struct binder_work *w)
355b0502
GKH
5121{
5122 struct binder_node *node;
5123 struct binder_transaction *t;
5124
5125 switch (w->type) {
5126 case BINDER_WORK_TRANSACTION:
5127 t = container_of(w, struct binder_transaction, work);
c2bdc4c4
TK
5128 print_binder_transaction_ilocked(
5129 m, proc, transaction_prefix, t);
355b0502 5130 break;
795aa6bc
TK
5131 case BINDER_WORK_RETURN_ERROR: {
5132 struct binder_error *e = container_of(
5133 w, struct binder_error, work);
5134
5135 seq_printf(m, "%stransaction error: %u\n",
5136 prefix, e->cmd);
5137 } break;
355b0502 5138 case BINDER_WORK_TRANSACTION_COMPLETE:
5249f488 5139 seq_printf(m, "%stransaction complete\n", prefix);
355b0502
GKH
5140 break;
5141 case BINDER_WORK_NODE:
5142 node = container_of(w, struct binder_node, work);
9c0a1d02
AH
5143 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
5144 prefix, node->debug_id,
5145 (u64)node->ptr, (u64)node->cookie);
355b0502
GKH
5146 break;
5147 case BINDER_WORK_DEAD_BINDER:
5249f488 5148 seq_printf(m, "%shas dead binder\n", prefix);
355b0502
GKH
5149 break;
5150 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
5249f488 5151 seq_printf(m, "%shas cleared dead binder\n", prefix);
355b0502
GKH
5152 break;
5153 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
5249f488 5154 seq_printf(m, "%shas cleared death notification\n", prefix);
355b0502
GKH
5155 break;
5156 default:
5249f488 5157 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
355b0502
GKH
5158 break;
5159 }
355b0502
GKH
5160}
5161
aad8732c
TK
5162static void print_binder_thread_ilocked(struct seq_file *m,
5163 struct binder_thread *thread,
5164 int print_always)
355b0502
GKH
5165{
5166 struct binder_transaction *t;
5167 struct binder_work *w;
5249f488
AH
5168 size_t start_pos = m->count;
5169 size_t header_pos;
355b0502 5170
aad8732c 5171 WARN_ON(!spin_is_locked(&thread->proc->inner_lock));
82f6ad88 5172 seq_printf(m, " thread %d: l %02x need_return %d tr %d\n",
85ab7b9c 5173 thread->pid, thread->looper,
82f6ad88
TK
5174 thread->looper_need_return,
5175 atomic_read(&thread->tmp_ref));
5249f488 5176 header_pos = m->count;
355b0502
GKH
5177 t = thread->transaction_stack;
5178 while (t) {
355b0502 5179 if (t->from == thread) {
c2bdc4c4
TK
5180 print_binder_transaction_ilocked(m, thread->proc,
5181 " outgoing transaction", t);
355b0502
GKH
5182 t = t->from_parent;
5183 } else if (t->to_thread == thread) {
c2bdc4c4 5184 print_binder_transaction_ilocked(m, thread->proc,
5249f488 5185 " incoming transaction", t);
355b0502
GKH
5186 t = t->to_parent;
5187 } else {
c2bdc4c4
TK
5188 print_binder_transaction_ilocked(m, thread->proc,
5189 " bad transaction", t);
355b0502
GKH
5190 t = NULL;
5191 }
5192 }
5193 list_for_each_entry(w, &thread->todo, entry) {
c2bdc4c4 5194 print_binder_work_ilocked(m, thread->proc, " ",
aad8732c 5195 " pending transaction", w);
355b0502 5196 }
5249f488
AH
5197 if (!print_always && m->count == header_pos)
5198 m->count = start_pos;
355b0502
GKH
5199}
5200
ceeaf28f
TK
5201static void print_binder_node_nilocked(struct seq_file *m,
5202 struct binder_node *node)
355b0502
GKH
5203{
5204 struct binder_ref *ref;
355b0502
GKH
5205 struct binder_work *w;
5206 int count;
5207
25de59a5 5208 WARN_ON(!spin_is_locked(&node->lock));
ceeaf28f
TK
5209 if (node->proc)
5210 WARN_ON(!spin_is_locked(&node->proc->inner_lock));
25de59a5 5211
355b0502 5212 count = 0;
b67bfe0d 5213 hlist_for_each_entry(ref, &node->refs, node_entry)
355b0502
GKH
5214 count++;
5215
fbb541d2 5216 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 5217 node->debug_id, (u64)node->ptr, (u64)node->cookie,
fbb541d2 5218 node->sched_policy, node->min_priority,
5249f488
AH
5219 node->has_strong_ref, node->has_weak_ref,
5220 node->local_strong_refs, node->local_weak_refs,
9607bf70 5221 node->internal_strong_refs, count, node->tmp_refs);
355b0502 5222 if (count) {
5249f488 5223 seq_puts(m, " proc");
b67bfe0d 5224 hlist_for_each_entry(ref, &node->refs, node_entry)
5249f488 5225 seq_printf(m, " %d", ref->proc->pid);
355b0502 5226 }
5249f488 5227 seq_puts(m, "\n");
aad8732c 5228 if (node->proc) {
aad8732c 5229 list_for_each_entry(w, &node->async_todo, entry)
c2bdc4c4 5230 print_binder_work_ilocked(m, node->proc, " ",
aad8732c 5231 " pending async transaction", w);
aad8732c 5232 }
355b0502
GKH
5233}
5234
5717118f
TK
5235static void print_binder_ref_olocked(struct seq_file *m,
5236 struct binder_ref *ref)
355b0502 5237{
5717118f 5238 WARN_ON(!spin_is_locked(&ref->proc->outer_lock));
25de59a5 5239 binder_node_lock(ref->node);
bc65c39a
TK
5240 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
5241 ref->data.debug_id, ref->data.desc,
5242 ref->node->proc ? "" : "dead ",
5243 ref->node->debug_id, ref->data.strong,
5244 ref->data.weak, ref->death);
25de59a5 5245 binder_node_unlock(ref->node);
355b0502
GKH
5246}
5247
5249f488
AH
5248static void print_binder_proc(struct seq_file *m,
5249 struct binder_proc *proc, int print_all)
355b0502
GKH
5250{
5251 struct binder_work *w;
5252 struct rb_node *n;
5249f488
AH
5253 size_t start_pos = m->count;
5254 size_t header_pos;
ceeaf28f 5255 struct binder_node *last_node = NULL;
5249f488
AH
5256
5257 seq_printf(m, "proc %d\n", proc->pid);
a64af2cf 5258 seq_printf(m, "context %s\n", proc->context->name);
5249f488
AH
5259 header_pos = m->count;
5260
aad8732c 5261 binder_inner_proc_lock(proc);
5249f488 5262 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
aad8732c 5263 print_binder_thread_ilocked(m, rb_entry(n, struct binder_thread,
5249f488 5264 rb_node), print_all);
ceeaf28f 5265
5249f488 5266 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
355b0502
GKH
5267 struct binder_node *node = rb_entry(n, struct binder_node,
5268 rb_node);
ceeaf28f
TK
5269 /*
5270 * take a temporary reference on the node so it
5271 * survives and isn't removed from the tree
5272 * while we print it.
5273 */
5274 binder_inc_node_tmpref_ilocked(node);
5275 /* Need to drop inner lock to take node lock */
5276 binder_inner_proc_unlock(proc);
5277 if (last_node)
5278 binder_put_node(last_node);
5279 binder_node_inner_lock(node);
5280 print_binder_node_nilocked(m, node);
5281 binder_node_inner_unlock(node);
5282 last_node = node;
5283 binder_inner_proc_lock(proc);
355b0502 5284 }
ceeaf28f
TK
5285 binder_inner_proc_unlock(proc);
5286 if (last_node)
5287 binder_put_node(last_node);
5288
355b0502 5289 if (print_all) {
5717118f 5290 binder_proc_lock(proc);
355b0502 5291 for (n = rb_first(&proc->refs_by_desc);
5249f488 5292 n != NULL;
355b0502 5293 n = rb_next(n))
5717118f
TK
5294 print_binder_ref_olocked(m, rb_entry(n,
5295 struct binder_ref,
5296 rb_node_desc));
5297 binder_proc_unlock(proc);
355b0502 5298 }
69c33bb1 5299 binder_alloc_print_allocated(m, &proc->alloc);
aad8732c 5300 binder_inner_proc_lock(proc);
5249f488 5301 list_for_each_entry(w, &proc->todo, entry)
c2bdc4c4
TK
5302 print_binder_work_ilocked(m, proc, " ",
5303 " pending transaction", w);
355b0502 5304 list_for_each_entry(w, &proc->delivered_death, entry) {
5249f488 5305 seq_puts(m, " has delivered dead binder\n");
355b0502
GKH
5306 break;
5307 }
aad8732c 5308 binder_inner_proc_unlock(proc);
5249f488
AH
5309 if (!print_all && m->count == header_pos)
5310 m->count = start_pos;
355b0502
GKH
5311}
5312
167bccbd 5313static const char * const binder_return_strings[] = {
355b0502
GKH
5314 "BR_ERROR",
5315 "BR_OK",
5316 "BR_TRANSACTION",
5317 "BR_REPLY",
5318 "BR_ACQUIRE_RESULT",
5319 "BR_DEAD_REPLY",
5320 "BR_TRANSACTION_COMPLETE",
5321 "BR_INCREFS",
5322 "BR_ACQUIRE",
5323 "BR_RELEASE",
5324 "BR_DECREFS",
5325 "BR_ATTEMPT_ACQUIRE",
5326 "BR_NOOP",
5327 "BR_SPAWN_LOOPER",
5328 "BR_FINISHED",
5329 "BR_DEAD_BINDER",
5330 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
5331 "BR_FAILED_REPLY"
5332};
5333
167bccbd 5334static const char * const binder_command_strings[] = {
355b0502
GKH
5335 "BC_TRANSACTION",
5336 "BC_REPLY",
5337 "BC_ACQUIRE_RESULT",
5338 "BC_FREE_BUFFER",
5339 "BC_INCREFS",
5340 "BC_ACQUIRE",
5341 "BC_RELEASE",
5342 "BC_DECREFS",
5343 "BC_INCREFS_DONE",
5344 "BC_ACQUIRE_DONE",
5345 "BC_ATTEMPT_ACQUIRE",
5346 "BC_REGISTER_LOOPER",
5347 "BC_ENTER_LOOPER",
5348 "BC_EXIT_LOOPER",
5349 "BC_REQUEST_DEATH_NOTIFICATION",
5350 "BC_CLEAR_DEATH_NOTIFICATION",
e884979e
MC
5351 "BC_DEAD_BINDER_DONE",
5352 "BC_TRANSACTION_SG",
5353 "BC_REPLY_SG",
355b0502
GKH
5354};
5355
167bccbd 5356static const char * const binder_objstat_strings[] = {
355b0502
GKH
5357 "proc",
5358 "thread",
5359 "node",
5360 "ref",
5361 "death",
5362 "transaction",
5363 "transaction_complete"
5364};
5365
5249f488 5366static void print_binder_stats(struct seq_file *m, const char *prefix,
7eac155d 5367 struct binder_stats *stats)
355b0502
GKH
5368{
5369 int i;
5370
5371 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
5249f488 5372 ARRAY_SIZE(binder_command_strings));
355b0502 5373 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
340b5f86
BJS
5374 int temp = atomic_read(&stats->bc[i]);
5375
5376 if (temp)
5249f488 5377 seq_printf(m, "%s%s: %d\n", prefix,
340b5f86 5378 binder_command_strings[i], temp);
355b0502
GKH
5379 }
5380
5381 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
5249f488 5382 ARRAY_SIZE(binder_return_strings));
355b0502 5383 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
340b5f86
BJS
5384 int temp = atomic_read(&stats->br[i]);
5385
5386 if (temp)
5249f488 5387 seq_printf(m, "%s%s: %d\n", prefix,
340b5f86 5388 binder_return_strings[i], temp);
355b0502
GKH
5389 }
5390
7eac155d 5391 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
5249f488 5392 ARRAY_SIZE(binder_objstat_strings));
7eac155d
DW
5393 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
5394 ARRAY_SIZE(stats->obj_deleted));
5395 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
340b5f86
BJS
5396 int created = atomic_read(&stats->obj_created[i]);
5397 int deleted = atomic_read(&stats->obj_deleted[i]);
5398
5399 if (created || deleted)
5400 seq_printf(m, "%s%s: active %d total %d\n",
5401 prefix,
7eac155d 5402 binder_objstat_strings[i],
340b5f86
BJS
5403 created - deleted,
5404 created);
355b0502 5405 }
69c33bb1
TK
5406}
5407
5249f488
AH
5408static void print_binder_proc_stats(struct seq_file *m,
5409 struct binder_proc *proc)
355b0502
GKH
5410{
5411 struct binder_work *w;
02545935 5412 struct binder_thread *thread;
355b0502 5413 struct rb_node *n;
02545935 5414 int count, strong, weak, ready_threads;
aed01731
TK
5415 size_t free_async_space =
5416 binder_alloc_get_free_async_space(&proc->alloc);
355b0502 5417
5249f488 5418 seq_printf(m, "proc %d\n", proc->pid);
a64af2cf 5419 seq_printf(m, "context %s\n", proc->context->name);
355b0502 5420 count = 0;
02545935 5421 ready_threads = 0;
aed01731 5422 binder_inner_proc_lock(proc);
355b0502
GKH
5423 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
5424 count++;
02545935
MC
5425
5426 list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node)
5427 ready_threads++;
5428
5249f488
AH
5429 seq_printf(m, " threads: %d\n", count);
5430 seq_printf(m, " requested threads: %d+%d/%d\n"
355b0502
GKH
5431 " ready threads %d\n"
5432 " free async space %zd\n", proc->requested_threads,
5433 proc->requested_threads_started, proc->max_threads,
02545935 5434 ready_threads,
aed01731 5435 free_async_space);
355b0502
GKH
5436 count = 0;
5437 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
5438 count++;
ceeaf28f 5439 binder_inner_proc_unlock(proc);
5249f488 5440 seq_printf(m, " nodes: %d\n", count);
355b0502
GKH
5441 count = 0;
5442 strong = 0;
5443 weak = 0;
5717118f 5444 binder_proc_lock(proc);
355b0502
GKH
5445 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
5446 struct binder_ref *ref = rb_entry(n, struct binder_ref,
5447 rb_node_desc);
5448 count++;
bc65c39a
TK
5449 strong += ref->data.strong;
5450 weak += ref->data.weak;
355b0502 5451 }
5717118f 5452 binder_proc_unlock(proc);
5249f488 5453 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
355b0502 5454
69c33bb1 5455 count = binder_alloc_get_allocated_count(&proc->alloc);
5249f488 5456 seq_printf(m, " buffers: %d\n", count);
355b0502
GKH
5457
5458 count = 0;
aad8732c 5459 binder_inner_proc_lock(proc);
355b0502 5460 list_for_each_entry(w, &proc->todo, entry) {
aad8732c 5461 if (w->type == BINDER_WORK_TRANSACTION)
355b0502 5462 count++;
355b0502 5463 }
aad8732c 5464 binder_inner_proc_unlock(proc);
5249f488 5465 seq_printf(m, " pending transactions: %d\n", count);
355b0502 5466
7eac155d 5467 print_binder_stats(m, " ", &proc->stats);
355b0502
GKH
5468}
5469
5470
5249f488 5471static int binder_state_show(struct seq_file *m, void *unused)
355b0502
GKH
5472{
5473 struct binder_proc *proc;
355b0502 5474 struct binder_node *node;
25de59a5 5475 struct binder_node *last_node = NULL;
355b0502 5476
7eac155d 5477 seq_puts(m, "binder state:\n");
355b0502 5478
76341f6f 5479 spin_lock(&binder_dead_nodes_lock);
7eac155d
DW
5480 if (!hlist_empty(&binder_dead_nodes))
5481 seq_puts(m, "dead nodes:\n");
25de59a5
TK
5482 hlist_for_each_entry(node, &binder_dead_nodes, dead_node) {
5483 /*
5484 * take a temporary reference on the node so it
5485 * survives and isn't removed from the list
5486 * while we print it.
5487 */
5488 node->tmp_refs++;
5489 spin_unlock(&binder_dead_nodes_lock);
5490 if (last_node)
5491 binder_put_node(last_node);
5492 binder_node_lock(node);
ceeaf28f 5493 print_binder_node_nilocked(m, node);
25de59a5
TK
5494 binder_node_unlock(node);
5495 last_node = node;
5496 spin_lock(&binder_dead_nodes_lock);
5497 }
76341f6f 5498 spin_unlock(&binder_dead_nodes_lock);
25de59a5
TK
5499 if (last_node)
5500 binder_put_node(last_node);
9f28e23f 5501
76341f6f 5502 mutex_lock(&binder_procs_lock);
7eac155d
DW
5503 hlist_for_each_entry(proc, &binder_procs, proc_node)
5504 print_binder_proc(m, proc, 1);
76341f6f 5505 mutex_unlock(&binder_procs_lock);
9f71b6a6 5506
5249f488 5507 return 0;
355b0502
GKH
5508}
5509
5249f488 5510static int binder_stats_show(struct seq_file *m, void *unused)
355b0502
GKH
5511{
5512 struct binder_proc *proc;
355b0502 5513
5249f488 5514 seq_puts(m, "binder stats:\n");
355b0502 5515
7eac155d 5516 print_binder_stats(m, "", &binder_stats);
355b0502 5517
76341f6f 5518 mutex_lock(&binder_procs_lock);
7eac155d
DW
5519 hlist_for_each_entry(proc, &binder_procs, proc_node)
5520 print_binder_proc_stats(m, proc);
76341f6f 5521 mutex_unlock(&binder_procs_lock);
9f71b6a6 5522
5249f488 5523 return 0;
355b0502
GKH
5524}
5525
5249f488 5526static int binder_transactions_show(struct seq_file *m, void *unused)
355b0502
GKH
5527{
5528 struct binder_proc *proc;
355b0502 5529
7eac155d 5530 seq_puts(m, "binder transactions:\n");
76341f6f 5531 mutex_lock(&binder_procs_lock);
7eac155d
DW
5532 hlist_for_each_entry(proc, &binder_procs, proc_node)
5533 print_binder_proc(m, proc, 0);
76341f6f 5534 mutex_unlock(&binder_procs_lock);
9f71b6a6 5535
5249f488 5536 return 0;
355b0502
GKH
5537}
5538
5249f488 5539static int binder_proc_show(struct seq_file *m, void *unused)
355b0502 5540{
1bfa8186 5541 struct binder_proc *itr;
a64af2cf 5542 int pid = (unsigned long)m->private;
355b0502 5543
76341f6f 5544 mutex_lock(&binder_procs_lock);
7eac155d
DW
5545 hlist_for_each_entry(itr, &binder_procs, proc_node) {
5546 if (itr->pid == pid) {
5547 seq_puts(m, "binder proc state:\n");
5548 print_binder_proc(m, itr, 1);
1bfa8186
RA
5549 }
5550 }
76341f6f
TK
5551 mutex_unlock(&binder_procs_lock);
5552
5249f488 5553 return 0;
355b0502
GKH
5554}
5555
5249f488 5556static void print_binder_transaction_log_entry(struct seq_file *m,
355b0502
GKH
5557 struct binder_transaction_log_entry *e)
5558{
7d94b2c7
TK
5559 int debug_id = READ_ONCE(e->debug_id_done);
5560 /*
5561 * read barrier to guarantee debug_id_done read before
5562 * we print the log values
5563 */
5564 smp_rmb();
5249f488 5565 seq_printf(m,
7d94b2c7 5566 "%d: %s from %d:%d to %d:%d context %s node %d handle %d size %d:%d ret %d/%d l=%d",
5249f488
AH
5567 e->debug_id, (e->call_type == 2) ? "reply" :
5568 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
a64af2cf 5569 e->from_thread, e->to_proc, e->to_thread, e->context_name,
dfc99565
TK
5570 e->to_node, e->target_handle, e->data_size, e->offsets_size,
5571 e->return_error, e->return_error_param,
5572 e->return_error_line);
7d94b2c7
TK
5573 /*
5574 * read-barrier to guarantee read of debug_id_done after
5575 * done printing the fields of the entry
5576 */
5577 smp_rmb();
5578 seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ?
5579 "\n" : " (incomplete)\n");
355b0502
GKH
5580}
5581
7eac155d 5582static int binder_transaction_log_show(struct seq_file *m, void *unused)
355b0502 5583{
7eac155d 5584 struct binder_transaction_log *log = m->private;
7d94b2c7
TK
5585 unsigned int log_cur = atomic_read(&log->cur);
5586 unsigned int count;
5587 unsigned int cur;
355b0502 5588 int i;
7eac155d 5589
7d94b2c7
TK
5590 count = log_cur + 1;
5591 cur = count < ARRAY_SIZE(log->entry) && !log->full ?
5592 0 : count % ARRAY_SIZE(log->entry);
5593 if (count > ARRAY_SIZE(log->entry) || log->full)
5594 count = ARRAY_SIZE(log->entry);
5595 for (i = 0; i < count; i++) {
5596 unsigned int index = cur++ % ARRAY_SIZE(log->entry);
5597
5598 print_binder_transaction_log_entry(m, &log->entry[index]);
355b0502 5599 }
5249f488 5600 return 0;
355b0502
GKH
5601}
5602
5603static const struct file_operations binder_fops = {
5604 .owner = THIS_MODULE,
5605 .poll = binder_poll,
5606 .unlocked_ioctl = binder_ioctl,
9c0a1d02 5607 .compat_ioctl = binder_ioctl,
355b0502
GKH
5608 .mmap = binder_mmap,
5609 .open = binder_open,
5610 .flush = binder_flush,
5611 .release = binder_release,
5612};
5613
5249f488
AH
5614BINDER_DEBUG_ENTRY(state);
5615BINDER_DEBUG_ENTRY(stats);
5616BINDER_DEBUG_ENTRY(transactions);
5617BINDER_DEBUG_ENTRY(transaction_log);
5618
bfd99b42
MC
5619static int __init init_binder_device(const char *name)
5620{
5621 int ret;
5622 struct binder_device *binder_device;
5623
5624 binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
5625 if (!binder_device)
5626 return -ENOMEM;
5627
5628 binder_device->miscdev.fops = &binder_fops;
5629 binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
5630 binder_device->miscdev.name = name;
5631
7eac155d
DW
5632 binder_device->context.binder_context_mgr_uid = INVALID_UID;
5633 binder_device->context.name = name;
76341f6f 5634 mutex_init(&binder_device->context.context_mgr_node_lock);
bfd99b42
MC
5635
5636 ret = misc_register(&binder_device->miscdev);
5637 if (ret < 0) {
7eac155d
DW
5638 kfree(binder_device);
5639 return ret;
bfd99b42
MC
5640 }
5641
5642 hlist_add_head(&binder_device->hlist, &binder_devices);
5643
5644 return ret;
5645}
5646
355b0502
GKH
5647static int __init binder_init(void)
5648{
7eac155d 5649 int ret;
bfd99b42
MC
5650 char *device_name, *device_names;
5651 struct binder_device *device;
5652 struct hlist_node *tmp;
355b0502 5653
7d94b2c7
TK
5654 atomic_set(&binder_transaction_log.cur, ~0U);
5655 atomic_set(&binder_transaction_log_failed.cur, ~0U);
7eac155d
DW
5656 binder_deferred_workqueue = create_singlethread_workqueue("binder");
5657 if (!binder_deferred_workqueue)
3c762a49
AH
5658 return -ENOMEM;
5659
16b66554
AH
5660 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
5661 if (binder_debugfs_dir_entry_root)
5662 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
5663 binder_debugfs_dir_entry_root);
bfd99b42 5664
16b66554
AH
5665 if (binder_debugfs_dir_entry_root) {
5666 debugfs_create_file("state",
5667 S_IRUGO,
5668 binder_debugfs_dir_entry_root,
5669 NULL,
5670 &binder_state_fops);
5671 debugfs_create_file("stats",
5672 S_IRUGO,
5673 binder_debugfs_dir_entry_root,
5674 NULL,
5675 &binder_stats_fops);
5676 debugfs_create_file("transactions",
5677 S_IRUGO,
5678 binder_debugfs_dir_entry_root,
5679 NULL,
5680 &binder_transactions_fops);
5681 debugfs_create_file("transaction_log",
5682 S_IRUGO,
5683 binder_debugfs_dir_entry_root,
7eac155d 5684 &binder_transaction_log,
16b66554
AH
5685 &binder_transaction_log_fops);
5686 debugfs_create_file("failed_transaction_log",
5687 S_IRUGO,
5688 binder_debugfs_dir_entry_root,
7eac155d
DW
5689 &binder_transaction_log_failed,
5690 &binder_transaction_log_fops);
5691 }
5692
5693 /*
5694 * Copy the module_parameter string, because we don't want to
5695 * tokenize it in-place.
5696 */
5697 device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL);
5698 if (!device_names) {
5699 ret = -ENOMEM;
5700 goto err_alloc_device_names_failed;
5701 }
5702 strcpy(device_names, binder_devices_param);
5703
5704 while ((device_name = strsep(&device_names, ","))) {
5705 ret = init_binder_device(device_name);
5706 if (ret)
5707 goto err_init_binder_device_failed;
bfd99b42
MC
5708 }
5709
5710 return ret;
5711
5712err_init_binder_device_failed:
5713 hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
5714 misc_deregister(&device->miscdev);
5715 hlist_del(&device->hlist);
7eac155d 5716 kfree(device);
bfd99b42 5717 }
7eac155d
DW
5718err_alloc_device_names_failed:
5719 debugfs_remove_recursive(binder_debugfs_dir_entry_root);
5720
5721 destroy_workqueue(binder_deferred_workqueue);
bfd99b42 5722
355b0502
GKH
5723 return ret;
5724}
5725
5726device_initcall(binder_init);
5727
975a1ac9
AH
5728#define CREATE_TRACE_POINTS
5729#include "binder_trace.h"
5730
355b0502 5731MODULE_LICENSE("GPL v2");