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