blob: 35f48a39144c04b515db0cc4c096a5527b5d5786 [file] [log] [blame]
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001/* 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
Todd Kjos9630fe82017-06-29 12:02:00 -070018/*
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
Martijn Coenen1b77e9d2017-08-31 10:04:18 +020031 * (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
Todd Kjos9630fe82017-06-29 12:02:00 -070035 * 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
Anmol Sarma56b468f2012-10-30 22:35:43 +053052#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
53
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090054#include <asm/cacheflush.h>
55#include <linux/fdtable.h>
56#include <linux/file.h>
Colin Crosse2610b22013-05-06 23:50:15 +000057#include <linux/freezer.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090058#include <linux/fs.h>
59#include <linux/list.h>
60#include <linux/miscdevice.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090061#include <linux/module.h>
62#include <linux/mutex.h>
63#include <linux/nsproxy.h>
64#include <linux/poll.h>
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070065#include <linux/debugfs.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090066#include <linux/rbtree.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010067#include <linux/sched/signal.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010068#include <linux/sched/mm.h>
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070069#include <linux/seq_file.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090070#include <linux/uaccess.h>
Eric W. Biederman17cf22c2010-03-02 14:51:53 -080071#include <linux/pid_namespace.h>
Stephen Smalley79af7302015-01-21 10:54:10 -050072#include <linux/security.h>
Todd Kjos9630fe82017-06-29 12:02:00 -070073#include <linux/spinlock.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090074
Greg Kroah-Hartman9246a4a2014-10-16 15:26:51 +020075#include <uapi/linux/android/binder.h>
Todd Kjos0c972a02017-06-29 12:01:41 -070076#include "binder_alloc.h"
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -070077#include "binder_trace.h"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090078
Todd Kjosc44b1232017-06-29 12:01:43 -070079static HLIST_HEAD(binder_deferred_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090080static DEFINE_MUTEX(binder_deferred_lock);
81
Martijn Coenenac4812c2017-02-03 14:40:48 -080082static HLIST_HEAD(binder_devices);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090083static HLIST_HEAD(binder_procs);
Todd Kjosc44b1232017-06-29 12:01:43 -070084static DEFINE_MUTEX(binder_procs_lock);
85
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090086static HLIST_HEAD(binder_dead_nodes);
Todd Kjosc44b1232017-06-29 12:01:43 -070087static DEFINE_SPINLOCK(binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090088
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070089static struct dentry *binder_debugfs_dir_entry_root;
90static struct dentry *binder_debugfs_dir_entry_proc;
Todd Kjos656a8002017-06-29 12:01:45 -070091static atomic_t binder_last_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090092
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070093#define BINDER_DEBUG_ENTRY(name) \
94static int binder_##name##_open(struct inode *inode, struct file *file) \
95{ \
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070096 return single_open(file, binder_##name##_show, inode->i_private); \
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070097} \
98\
99static const struct file_operations binder_##name##_fops = { \
100 .owner = THIS_MODULE, \
101 .open = binder_##name##_open, \
102 .read = seq_read, \
103 .llseek = seq_lseek, \
104 .release = single_release, \
105}
106
107static int binder_proc_show(struct seq_file *m, void *unused);
108BINDER_DEBUG_ENTRY(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900109
110/* This is only defined in include/asm-arm/sizes.h */
111#ifndef SZ_1K
112#define SZ_1K 0x400
113#endif
114
115#ifndef SZ_4M
116#define SZ_4M 0x400000
117#endif
118
119#define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
120
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900121enum {
122 BINDER_DEBUG_USER_ERROR = 1U << 0,
123 BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1,
124 BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2,
125 BINDER_DEBUG_OPEN_CLOSE = 1U << 3,
126 BINDER_DEBUG_DEAD_BINDER = 1U << 4,
127 BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5,
128 BINDER_DEBUG_READ_WRITE = 1U << 6,
129 BINDER_DEBUG_USER_REFS = 1U << 7,
130 BINDER_DEBUG_THREADS = 1U << 8,
131 BINDER_DEBUG_TRANSACTION = 1U << 9,
132 BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10,
133 BINDER_DEBUG_FREE_BUFFER = 1U << 11,
134 BINDER_DEBUG_INTERNAL_REFS = 1U << 12,
Todd Kjos19c98722017-06-29 12:01:40 -0700135 BINDER_DEBUG_PRIORITY_CAP = 1U << 13,
Todd Kjos9630fe82017-06-29 12:02:00 -0700136 BINDER_DEBUG_SPINLOCKS = 1U << 14,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900137};
138static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR |
139 BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION;
Harsh Shandilya21d02dd2017-12-22 19:37:02 +0530140module_param_named(debug_mask, binder_debug_mask, uint, 0644);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900141
Martijn Coenenac4812c2017-02-03 14:40:48 -0800142static char *binder_devices_param = CONFIG_ANDROID_BINDER_DEVICES;
143module_param_named(devices, binder_devices_param, charp, 0444);
144
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900145static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
146static int binder_stop_on_user_error;
147
148static int binder_set_stop_on_user_error(const char *val,
Kees Cooke4dca7b2017-10-17 19:04:42 -0700149 const struct kernel_param *kp)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900150{
151 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +0900152
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900153 ret = param_set_int(val, kp);
154 if (binder_stop_on_user_error < 2)
155 wake_up(&binder_user_error_wait);
156 return ret;
157}
158module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
Harsh Shandilya21d02dd2017-12-22 19:37:02 +0530159 param_get_int, &binder_stop_on_user_error, 0644);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900160
161#define binder_debug(mask, x...) \
162 do { \
163 if (binder_debug_mask & mask) \
Sherwin Soltani258767f2012-06-26 02:00:30 -0400164 pr_info(x); \
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900165 } while (0)
166
167#define binder_user_error(x...) \
168 do { \
169 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
Sherwin Soltani258767f2012-06-26 02:00:30 -0400170 pr_info(x); \
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900171 if (binder_stop_on_user_error) \
172 binder_stop_on_user_error = 2; \
173 } while (0)
174
Martijn Coenenfeba3902017-02-03 14:40:45 -0800175#define to_flat_binder_object(hdr) \
176 container_of(hdr, struct flat_binder_object, hdr)
177
178#define to_binder_fd_object(hdr) container_of(hdr, struct binder_fd_object, hdr)
179
Martijn Coenen79802402017-02-03 14:40:51 -0800180#define to_binder_buffer_object(hdr) \
181 container_of(hdr, struct binder_buffer_object, hdr)
182
Martijn Coenendef95c72017-02-03 14:40:52 -0800183#define to_binder_fd_array_object(hdr) \
184 container_of(hdr, struct binder_fd_array_object, hdr)
185
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900186enum binder_stat_types {
187 BINDER_STAT_PROC,
188 BINDER_STAT_THREAD,
189 BINDER_STAT_NODE,
190 BINDER_STAT_REF,
191 BINDER_STAT_DEATH,
192 BINDER_STAT_TRANSACTION,
193 BINDER_STAT_TRANSACTION_COMPLETE,
194 BINDER_STAT_COUNT
195};
196
197struct binder_stats {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -0700198 atomic_t br[_IOC_NR(BR_FAILED_REPLY) + 1];
199 atomic_t bc[_IOC_NR(BC_REPLY_SG) + 1];
200 atomic_t obj_created[BINDER_STAT_COUNT];
201 atomic_t obj_deleted[BINDER_STAT_COUNT];
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900202};
203
204static struct binder_stats binder_stats;
205
206static inline void binder_stats_deleted(enum binder_stat_types type)
207{
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -0700208 atomic_inc(&binder_stats.obj_deleted[type]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900209}
210
211static inline void binder_stats_created(enum binder_stat_types type)
212{
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -0700213 atomic_inc(&binder_stats.obj_created[type]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900214}
215
216struct binder_transaction_log_entry {
217 int debug_id;
Todd Kjosd99c7332017-06-29 12:01:53 -0700218 int debug_id_done;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900219 int call_type;
220 int from_proc;
221 int from_thread;
222 int target_handle;
223 int to_proc;
224 int to_thread;
225 int to_node;
226 int data_size;
227 int offsets_size;
Todd Kjos57ada2f2017-06-29 12:01:46 -0700228 int return_error_line;
229 uint32_t return_error;
230 uint32_t return_error_param;
Martijn Coenen14db3182017-02-03 14:40:47 -0800231 const char *context_name;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900232};
233struct binder_transaction_log {
Todd Kjosd99c7332017-06-29 12:01:53 -0700234 atomic_t cur;
235 bool full;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900236 struct binder_transaction_log_entry entry[32];
237};
238static struct binder_transaction_log binder_transaction_log;
239static struct binder_transaction_log binder_transaction_log_failed;
240
241static struct binder_transaction_log_entry *binder_transaction_log_add(
242 struct binder_transaction_log *log)
243{
244 struct binder_transaction_log_entry *e;
Todd Kjosd99c7332017-06-29 12:01:53 -0700245 unsigned int cur = atomic_inc_return(&log->cur);
Seunghun Lee10f62862014-05-01 01:30:23 +0900246
Todd Kjosd99c7332017-06-29 12:01:53 -0700247 if (cur >= ARRAY_SIZE(log->entry))
Gustavo A. R. Silva197410a2018-01-23 12:04:27 -0600248 log->full = true;
Todd Kjosd99c7332017-06-29 12:01:53 -0700249 e = &log->entry[cur % ARRAY_SIZE(log->entry)];
250 WRITE_ONCE(e->debug_id_done, 0);
251 /*
252 * write-barrier to synchronize access to e->debug_id_done.
253 * We make sure the initialized 0 value is seen before
254 * memset() other fields are zeroed by memset.
255 */
256 smp_wmb();
257 memset(e, 0, sizeof(*e));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900258 return e;
259}
260
Martijn Coenen342e5c92017-02-03 14:40:46 -0800261struct binder_context {
262 struct binder_node *binder_context_mgr_node;
Todd Kjosc44b1232017-06-29 12:01:43 -0700263 struct mutex context_mgr_node_lock;
264
Martijn Coenen342e5c92017-02-03 14:40:46 -0800265 kuid_t binder_context_mgr_uid;
Martijn Coenen14db3182017-02-03 14:40:47 -0800266 const char *name;
Martijn Coenen342e5c92017-02-03 14:40:46 -0800267};
268
Martijn Coenenac4812c2017-02-03 14:40:48 -0800269struct binder_device {
270 struct hlist_node hlist;
271 struct miscdevice miscdev;
272 struct binder_context context;
Martijn Coenen342e5c92017-02-03 14:40:46 -0800273};
274
Todd Kjos72196392017-06-29 12:02:02 -0700275/**
276 * struct binder_work - work enqueued on a worklist
277 * @entry: node enqueued on list
278 * @type: type of work to be performed
279 *
280 * There are separate work lists for proc, thread, and node (async).
281 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900282struct binder_work {
283 struct list_head entry;
Todd Kjos72196392017-06-29 12:02:02 -0700284
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900285 enum {
286 BINDER_WORK_TRANSACTION = 1,
287 BINDER_WORK_TRANSACTION_COMPLETE,
Todd Kjos26549d12017-06-29 12:01:55 -0700288 BINDER_WORK_RETURN_ERROR,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900289 BINDER_WORK_NODE,
290 BINDER_WORK_DEAD_BINDER,
291 BINDER_WORK_DEAD_BINDER_AND_CLEAR,
292 BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
293 } type;
294};
295
Todd Kjos26549d12017-06-29 12:01:55 -0700296struct binder_error {
297 struct binder_work work;
298 uint32_t cmd;
299};
300
Todd Kjos9630fe82017-06-29 12:02:00 -0700301/**
302 * struct binder_node - binder node bookkeeping
303 * @debug_id: unique ID for debugging
304 * (invariant after initialized)
305 * @lock: lock for node fields
306 * @work: worklist element for node work
Todd Kjos72196392017-06-29 12:02:02 -0700307 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700308 * @rb_node: element for proc->nodes tree
Todd Kjosda0fa9e2017-06-29 12:02:04 -0700309 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700310 * @dead_node: element for binder_dead_nodes list
311 * (protected by binder_dead_nodes_lock)
312 * @proc: binder_proc that owns this node
313 * (invariant after initialized)
314 * @refs: list of references on this node
Todd Kjos673068e2017-06-29 12:02:03 -0700315 * (protected by @lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700316 * @internal_strong_refs: used to take strong references when
317 * initiating a transaction
Todd Kjosed297212017-06-29 12:02:01 -0700318 * (protected by @proc->inner_lock if @proc
319 * and by @lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700320 * @local_weak_refs: weak user refs from local process
Todd Kjosed297212017-06-29 12:02:01 -0700321 * (protected by @proc->inner_lock if @proc
322 * and by @lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700323 * @local_strong_refs: strong user refs from local process
Todd Kjosed297212017-06-29 12:02:01 -0700324 * (protected by @proc->inner_lock if @proc
325 * and by @lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700326 * @tmp_refs: temporary kernel refs
Todd Kjosed297212017-06-29 12:02:01 -0700327 * (protected by @proc->inner_lock while @proc
328 * is valid, and by binder_dead_nodes_lock
329 * if @proc is NULL. During inc/dec and node release
330 * it is also protected by @lock to provide safety
331 * as the node dies and @proc becomes NULL)
Todd Kjos9630fe82017-06-29 12:02:00 -0700332 * @ptr: userspace pointer for node
333 * (invariant, no lock needed)
334 * @cookie: userspace cookie for node
335 * (invariant, no lock needed)
336 * @has_strong_ref: userspace notified of strong ref
Todd Kjosed297212017-06-29 12:02:01 -0700337 * (protected by @proc->inner_lock if @proc
338 * and by @lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700339 * @pending_strong_ref: userspace has acked notification of strong ref
Todd Kjosed297212017-06-29 12:02:01 -0700340 * (protected by @proc->inner_lock if @proc
341 * and by @lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700342 * @has_weak_ref: userspace notified of weak ref
Todd Kjosed297212017-06-29 12:02:01 -0700343 * (protected by @proc->inner_lock if @proc
344 * and by @lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700345 * @pending_weak_ref: userspace has acked notification of weak ref
Todd Kjosed297212017-06-29 12:02:01 -0700346 * (protected by @proc->inner_lock if @proc
347 * and by @lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700348 * @has_async_transaction: async transaction to node in progress
Todd Kjos673068e2017-06-29 12:02:03 -0700349 * (protected by @lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700350 * @accept_fds: file descriptor operations supported for node
351 * (invariant after initialized)
352 * @min_priority: minimum scheduling priority
353 * (invariant after initialized)
354 * @async_todo: list of async work items
Todd Kjos72196392017-06-29 12:02:02 -0700355 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700356 *
357 * Bookkeeping structure for binder nodes.
358 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900359struct binder_node {
360 int debug_id;
Todd Kjos9630fe82017-06-29 12:02:00 -0700361 spinlock_t lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900362 struct binder_work work;
363 union {
364 struct rb_node rb_node;
365 struct hlist_node dead_node;
366 };
367 struct binder_proc *proc;
368 struct hlist_head refs;
369 int internal_strong_refs;
370 int local_weak_refs;
371 int local_strong_refs;
Todd Kjosadc18842017-06-29 12:01:59 -0700372 int tmp_refs;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800373 binder_uintptr_t ptr;
374 binder_uintptr_t cookie;
Todd Kjosed297212017-06-29 12:02:01 -0700375 struct {
376 /*
377 * bitfield elements protected by
378 * proc inner_lock
379 */
380 u8 has_strong_ref:1;
381 u8 pending_strong_ref:1;
382 u8 has_weak_ref:1;
383 u8 pending_weak_ref:1;
384 };
385 struct {
386 /*
387 * invariant after initialization
388 */
389 u8 accept_fds:1;
390 u8 min_priority;
391 };
392 bool has_async_transaction;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900393 struct list_head async_todo;
394};
395
396struct binder_ref_death {
Todd Kjos72196392017-06-29 12:02:02 -0700397 /**
398 * @work: worklist element for death notifications
399 * (protected by inner_lock of the proc that
400 * this ref belongs to)
401 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900402 struct binder_work work;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800403 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900404};
405
Todd Kjos372e3142017-06-29 12:01:58 -0700406/**
407 * struct binder_ref_data - binder_ref counts and id
408 * @debug_id: unique ID for the ref
409 * @desc: unique userspace handle for ref
410 * @strong: strong ref count (debugging only if not locked)
411 * @weak: weak ref count (debugging only if not locked)
412 *
413 * Structure to hold ref count and ref id information. Since
414 * the actual ref can only be accessed with a lock, this structure
415 * is used to return information about the ref to callers of
416 * ref inc/dec functions.
417 */
418struct binder_ref_data {
419 int debug_id;
420 uint32_t desc;
421 int strong;
422 int weak;
423};
424
425/**
426 * struct binder_ref - struct to track references on nodes
427 * @data: binder_ref_data containing id, handle, and current refcounts
428 * @rb_node_desc: node for lookup by @data.desc in proc's rb_tree
429 * @rb_node_node: node for lookup by @node in proc's rb_tree
430 * @node_entry: list entry for node->refs list in target node
Todd Kjos673068e2017-06-29 12:02:03 -0700431 * (protected by @node->lock)
Todd Kjos372e3142017-06-29 12:01:58 -0700432 * @proc: binder_proc containing ref
433 * @node: binder_node of target node. When cleaning up a
434 * ref for deletion in binder_cleanup_ref, a non-NULL
435 * @node indicates the node must be freed
436 * @death: pointer to death notification (ref_death) if requested
Martijn Coenenab51ec62017-06-29 12:02:10 -0700437 * (protected by @node->lock)
Todd Kjos372e3142017-06-29 12:01:58 -0700438 *
439 * Structure to track references from procA to target node (on procB). This
440 * structure is unsafe to access without holding @proc->outer_lock.
441 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900442struct binder_ref {
443 /* Lookups needed: */
444 /* node + proc => ref (transaction) */
445 /* desc + proc => ref (transaction, inc/dec ref) */
446 /* node => refs + procs (proc exit) */
Todd Kjos372e3142017-06-29 12:01:58 -0700447 struct binder_ref_data data;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900448 struct rb_node rb_node_desc;
449 struct rb_node rb_node_node;
450 struct hlist_node node_entry;
451 struct binder_proc *proc;
452 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900453 struct binder_ref_death *death;
454};
455
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900456enum binder_deferred_state {
457 BINDER_DEFERRED_PUT_FILES = 0x01,
458 BINDER_DEFERRED_FLUSH = 0x02,
459 BINDER_DEFERRED_RELEASE = 0x04,
460};
461
Todd Kjos9630fe82017-06-29 12:02:00 -0700462/**
463 * struct binder_proc - binder process bookkeeping
464 * @proc_node: element for binder_procs list
465 * @threads: rbtree of binder_threads in this proc
Todd Kjos7bd7b0e2017-06-29 12:02:05 -0700466 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700467 * @nodes: rbtree of binder nodes associated with
468 * this proc ordered by node->ptr
Todd Kjosda0fa9e2017-06-29 12:02:04 -0700469 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700470 * @refs_by_desc: rbtree of refs ordered by ref->desc
Todd Kjos2c1838d2017-06-29 12:02:08 -0700471 * (protected by @outer_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700472 * @refs_by_node: rbtree of refs ordered by ref->node
Todd Kjos2c1838d2017-06-29 12:02:08 -0700473 * (protected by @outer_lock)
Martijn Coenen1b77e9d2017-08-31 10:04:18 +0200474 * @waiting_threads: threads currently waiting for proc work
475 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700476 * @pid PID of group_leader of process
477 * (invariant after initialized)
478 * @tsk task_struct for group_leader of process
479 * (invariant after initialized)
480 * @files files_struct for process
Todd Kjos7f3dc002017-11-27 09:32:33 -0800481 * (protected by @files_lock)
482 * @files_lock mutex to protect @files
Todd Kjos9630fe82017-06-29 12:02:00 -0700483 * @deferred_work_node: element for binder_deferred_list
484 * (protected by binder_deferred_lock)
485 * @deferred_work: bitmap of deferred work to perform
486 * (protected by binder_deferred_lock)
487 * @is_dead: process is dead and awaiting free
488 * when outstanding transactions are cleaned up
Todd Kjos7bd7b0e2017-06-29 12:02:05 -0700489 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700490 * @todo: list of work for this process
Todd Kjos72196392017-06-29 12:02:02 -0700491 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700492 * @stats: per-process binder statistics
493 * (atomics, no lock needed)
494 * @delivered_death: list of delivered death notification
Todd Kjos72196392017-06-29 12:02:02 -0700495 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700496 * @max_threads: cap on number of binder threads
Todd Kjosb3e68612017-06-29 12:02:07 -0700497 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700498 * @requested_threads: number of binder threads requested but not
499 * yet started. In current implementation, can
500 * only be 0 or 1.
Todd Kjosb3e68612017-06-29 12:02:07 -0700501 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700502 * @requested_threads_started: number binder threads started
Todd Kjosb3e68612017-06-29 12:02:07 -0700503 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700504 * @tmp_ref: temporary reference to indicate proc is in use
Todd Kjos7bd7b0e2017-06-29 12:02:05 -0700505 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700506 * @default_priority: default scheduler priority
507 * (invariant after initialized)
508 * @debugfs_entry: debugfs node
509 * @alloc: binder allocator bookkeeping
510 * @context: binder_context for this proc
511 * (invariant after initialized)
512 * @inner_lock: can nest under outer_lock and/or node lock
513 * @outer_lock: no nesting under innor or node lock
514 * Lock order: 1) outer, 2) node, 3) inner
515 *
516 * Bookkeeping structure for binder processes
517 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900518struct binder_proc {
519 struct hlist_node proc_node;
520 struct rb_root threads;
521 struct rb_root nodes;
522 struct rb_root refs_by_desc;
523 struct rb_root refs_by_node;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +0200524 struct list_head waiting_threads;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900525 int pid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900526 struct task_struct *tsk;
527 struct files_struct *files;
Todd Kjos7f3dc002017-11-27 09:32:33 -0800528 struct mutex files_lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900529 struct hlist_node deferred_work_node;
530 int deferred_work;
Todd Kjos7a4408c2017-06-29 12:01:57 -0700531 bool is_dead;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900532
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900533 struct list_head todo;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900534 struct binder_stats stats;
535 struct list_head delivered_death;
536 int max_threads;
537 int requested_threads;
538 int requested_threads_started;
Todd Kjos7a4408c2017-06-29 12:01:57 -0700539 int tmp_ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900540 long default_priority;
Arve Hjønnevåg16b66552009-04-28 20:57:50 -0700541 struct dentry *debugfs_entry;
Todd Kjosfdfb4a92017-06-29 12:01:38 -0700542 struct binder_alloc alloc;
Martijn Coenen342e5c92017-02-03 14:40:46 -0800543 struct binder_context *context;
Todd Kjos9630fe82017-06-29 12:02:00 -0700544 spinlock_t inner_lock;
545 spinlock_t outer_lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900546};
547
548enum {
549 BINDER_LOOPER_STATE_REGISTERED = 0x01,
550 BINDER_LOOPER_STATE_ENTERED = 0x02,
551 BINDER_LOOPER_STATE_EXITED = 0x04,
552 BINDER_LOOPER_STATE_INVALID = 0x08,
553 BINDER_LOOPER_STATE_WAITING = 0x10,
Martijn Coenen1b77e9d2017-08-31 10:04:18 +0200554 BINDER_LOOPER_STATE_POLL = 0x20,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900555};
556
Todd Kjos9630fe82017-06-29 12:02:00 -0700557/**
558 * struct binder_thread - binder thread bookkeeping
559 * @proc: binder process for this thread
560 * (invariant after initialization)
561 * @rb_node: element for proc->threads rbtree
Todd Kjos7bd7b0e2017-06-29 12:02:05 -0700562 * (protected by @proc->inner_lock)
Martijn Coenen1b77e9d2017-08-31 10:04:18 +0200563 * @waiting_thread_node: element for @proc->waiting_threads list
564 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700565 * @pid: PID for this thread
566 * (invariant after initialization)
567 * @looper: bitmap of looping state
568 * (only accessed by this thread)
569 * @looper_needs_return: looping thread needs to exit driver
570 * (no lock needed)
571 * @transaction_stack: stack of in-progress transactions for this thread
Martijn Coenen0b89d692017-06-29 12:02:06 -0700572 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700573 * @todo: list of work to do for this thread
Todd Kjos72196392017-06-29 12:02:02 -0700574 * (protected by @proc->inner_lock)
Martijn Coenen148ade22017-11-15 09:21:35 +0100575 * @process_todo: whether work in @todo should be processed
576 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700577 * @return_error: transaction errors reported by this thread
578 * (only accessed by this thread)
579 * @reply_error: transaction errors reported by target thread
Martijn Coenen0b89d692017-06-29 12:02:06 -0700580 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700581 * @wait: wait queue for thread work
582 * @stats: per-thread statistics
583 * (atomics, no lock needed)
584 * @tmp_ref: temporary reference to indicate thread is in use
585 * (atomic since @proc->inner_lock cannot
586 * always be acquired)
587 * @is_dead: thread is dead and awaiting free
588 * when outstanding transactions are cleaned up
Todd Kjos7bd7b0e2017-06-29 12:02:05 -0700589 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700590 *
591 * Bookkeeping structure for binder threads.
592 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900593struct binder_thread {
594 struct binder_proc *proc;
595 struct rb_node rb_node;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +0200596 struct list_head waiting_thread_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900597 int pid;
Todd Kjos08dabce2017-06-29 12:01:49 -0700598 int looper; /* only modified by this thread */
599 bool looper_need_return; /* can be written by other thread */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900600 struct binder_transaction *transaction_stack;
601 struct list_head todo;
Martijn Coenen148ade22017-11-15 09:21:35 +0100602 bool process_todo;
Todd Kjos26549d12017-06-29 12:01:55 -0700603 struct binder_error return_error;
604 struct binder_error reply_error;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900605 wait_queue_head_t wait;
606 struct binder_stats stats;
Todd Kjos7a4408c2017-06-29 12:01:57 -0700607 atomic_t tmp_ref;
608 bool is_dead;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900609};
610
611struct binder_transaction {
612 int debug_id;
613 struct binder_work work;
614 struct binder_thread *from;
615 struct binder_transaction *from_parent;
616 struct binder_proc *to_proc;
617 struct binder_thread *to_thread;
618 struct binder_transaction *to_parent;
619 unsigned need_reply:1;
620 /* unsigned is_dead:1; */ /* not used at the moment */
621
622 struct binder_buffer *buffer;
623 unsigned int code;
624 unsigned int flags;
625 long priority;
626 long saved_priority;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -0600627 kuid_t sender_euid;
Todd Kjos7a4408c2017-06-29 12:01:57 -0700628 /**
629 * @lock: protects @from, @to_proc, and @to_thread
630 *
631 * @from, @to_proc, and @to_thread can be set to NULL
632 * during thread teardown
633 */
634 spinlock_t lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900635};
636
Todd Kjos9630fe82017-06-29 12:02:00 -0700637/**
638 * binder_proc_lock() - Acquire outer lock for given binder_proc
639 * @proc: struct binder_proc to acquire
640 *
641 * Acquires proc->outer_lock. Used to protect binder_ref
642 * structures associated with the given proc.
643 */
644#define binder_proc_lock(proc) _binder_proc_lock(proc, __LINE__)
645static void
646_binder_proc_lock(struct binder_proc *proc, int line)
647{
648 binder_debug(BINDER_DEBUG_SPINLOCKS,
649 "%s: line=%d\n", __func__, line);
650 spin_lock(&proc->outer_lock);
651}
652
653/**
654 * binder_proc_unlock() - Release spinlock for given binder_proc
655 * @proc: struct binder_proc to acquire
656 *
657 * Release lock acquired via binder_proc_lock()
658 */
659#define binder_proc_unlock(_proc) _binder_proc_unlock(_proc, __LINE__)
660static void
661_binder_proc_unlock(struct binder_proc *proc, int line)
662{
663 binder_debug(BINDER_DEBUG_SPINLOCKS,
664 "%s: line=%d\n", __func__, line);
665 spin_unlock(&proc->outer_lock);
666}
667
668/**
669 * binder_inner_proc_lock() - Acquire inner lock for given binder_proc
670 * @proc: struct binder_proc to acquire
671 *
672 * Acquires proc->inner_lock. Used to protect todo lists
673 */
674#define binder_inner_proc_lock(proc) _binder_inner_proc_lock(proc, __LINE__)
675static void
676_binder_inner_proc_lock(struct binder_proc *proc, int line)
677{
678 binder_debug(BINDER_DEBUG_SPINLOCKS,
679 "%s: line=%d\n", __func__, line);
680 spin_lock(&proc->inner_lock);
681}
682
683/**
684 * binder_inner_proc_unlock() - Release inner lock for given binder_proc
685 * @proc: struct binder_proc to acquire
686 *
687 * Release lock acquired via binder_inner_proc_lock()
688 */
689#define binder_inner_proc_unlock(proc) _binder_inner_proc_unlock(proc, __LINE__)
690static void
691_binder_inner_proc_unlock(struct binder_proc *proc, int line)
692{
693 binder_debug(BINDER_DEBUG_SPINLOCKS,
694 "%s: line=%d\n", __func__, line);
695 spin_unlock(&proc->inner_lock);
696}
697
698/**
699 * binder_node_lock() - Acquire spinlock for given binder_node
700 * @node: struct binder_node to acquire
701 *
702 * Acquires node->lock. Used to protect binder_node fields
703 */
704#define binder_node_lock(node) _binder_node_lock(node, __LINE__)
705static void
706_binder_node_lock(struct binder_node *node, int line)
707{
708 binder_debug(BINDER_DEBUG_SPINLOCKS,
709 "%s: line=%d\n", __func__, line);
710 spin_lock(&node->lock);
711}
712
713/**
714 * binder_node_unlock() - Release spinlock for given binder_proc
715 * @node: struct binder_node to acquire
716 *
717 * Release lock acquired via binder_node_lock()
718 */
719#define binder_node_unlock(node) _binder_node_unlock(node, __LINE__)
720static void
721_binder_node_unlock(struct binder_node *node, int line)
722{
723 binder_debug(BINDER_DEBUG_SPINLOCKS,
724 "%s: line=%d\n", __func__, line);
725 spin_unlock(&node->lock);
726}
727
Todd Kjos673068e2017-06-29 12:02:03 -0700728/**
729 * binder_node_inner_lock() - Acquire node and inner locks
730 * @node: struct binder_node to acquire
731 *
732 * Acquires node->lock. If node->proc also acquires
733 * proc->inner_lock. Used to protect binder_node fields
734 */
735#define binder_node_inner_lock(node) _binder_node_inner_lock(node, __LINE__)
736static void
737_binder_node_inner_lock(struct binder_node *node, int line)
738{
739 binder_debug(BINDER_DEBUG_SPINLOCKS,
740 "%s: line=%d\n", __func__, line);
741 spin_lock(&node->lock);
742 if (node->proc)
743 binder_inner_proc_lock(node->proc);
744}
745
746/**
747 * binder_node_unlock() - Release node and inner locks
748 * @node: struct binder_node to acquire
749 *
750 * Release lock acquired via binder_node_lock()
751 */
752#define binder_node_inner_unlock(node) _binder_node_inner_unlock(node, __LINE__)
753static void
754_binder_node_inner_unlock(struct binder_node *node, int line)
755{
756 struct binder_proc *proc = node->proc;
757
758 binder_debug(BINDER_DEBUG_SPINLOCKS,
759 "%s: line=%d\n", __func__, line);
760 if (proc)
761 binder_inner_proc_unlock(proc);
762 spin_unlock(&node->lock);
763}
764
Todd Kjos72196392017-06-29 12:02:02 -0700765static bool binder_worklist_empty_ilocked(struct list_head *list)
766{
767 return list_empty(list);
768}
769
770/**
771 * binder_worklist_empty() - Check if no items on the work list
772 * @proc: binder_proc associated with list
773 * @list: list to check
774 *
775 * Return: true if there are no items on list, else false
776 */
777static bool binder_worklist_empty(struct binder_proc *proc,
778 struct list_head *list)
779{
780 bool ret;
781
782 binder_inner_proc_lock(proc);
783 ret = binder_worklist_empty_ilocked(list);
784 binder_inner_proc_unlock(proc);
785 return ret;
786}
787
Martijn Coenen148ade22017-11-15 09:21:35 +0100788/**
789 * binder_enqueue_work_ilocked() - Add an item to the work list
790 * @work: struct binder_work to add to list
791 * @target_list: list to add work to
792 *
793 * Adds the work to the specified list. Asserts that work
794 * is not already on a list.
795 *
796 * Requires the proc->inner_lock to be held.
797 */
Todd Kjos72196392017-06-29 12:02:02 -0700798static void
799binder_enqueue_work_ilocked(struct binder_work *work,
800 struct list_head *target_list)
801{
802 BUG_ON(target_list == NULL);
803 BUG_ON(work->entry.next && !list_empty(&work->entry));
804 list_add_tail(&work->entry, target_list);
805}
806
807/**
Martijn Coenen148ade22017-11-15 09:21:35 +0100808 * binder_enqueue_deferred_thread_work_ilocked() - Add deferred thread work
809 * @thread: thread to queue work to
Todd Kjos72196392017-06-29 12:02:02 -0700810 * @work: struct binder_work to add to list
Todd Kjos72196392017-06-29 12:02:02 -0700811 *
Martijn Coenen148ade22017-11-15 09:21:35 +0100812 * Adds the work to the todo list of the thread. Doesn't set the process_todo
813 * flag, which means that (if it wasn't already set) the thread will go to
814 * sleep without handling this work when it calls read.
815 *
816 * Requires the proc->inner_lock to be held.
Todd Kjos72196392017-06-29 12:02:02 -0700817 */
818static void
Martijn Coenen148ade22017-11-15 09:21:35 +0100819binder_enqueue_deferred_thread_work_ilocked(struct binder_thread *thread,
820 struct binder_work *work)
Todd Kjos72196392017-06-29 12:02:02 -0700821{
Martijn Coenen148ade22017-11-15 09:21:35 +0100822 binder_enqueue_work_ilocked(work, &thread->todo);
823}
824
825/**
826 * binder_enqueue_thread_work_ilocked() - Add an item to the thread work list
827 * @thread: thread to queue work to
828 * @work: struct binder_work to add to list
829 *
830 * Adds the work to the todo list of the thread, and enables processing
831 * of the todo queue.
832 *
833 * Requires the proc->inner_lock to be held.
834 */
835static void
836binder_enqueue_thread_work_ilocked(struct binder_thread *thread,
837 struct binder_work *work)
838{
839 binder_enqueue_work_ilocked(work, &thread->todo);
840 thread->process_todo = true;
841}
842
843/**
844 * binder_enqueue_thread_work() - Add an item to the thread work list
845 * @thread: thread to queue work to
846 * @work: struct binder_work to add to list
847 *
848 * Adds the work to the todo list of the thread, and enables processing
849 * of the todo queue.
850 */
851static void
852binder_enqueue_thread_work(struct binder_thread *thread,
853 struct binder_work *work)
854{
855 binder_inner_proc_lock(thread->proc);
856 binder_enqueue_thread_work_ilocked(thread, work);
857 binder_inner_proc_unlock(thread->proc);
Todd Kjos72196392017-06-29 12:02:02 -0700858}
859
860static void
861binder_dequeue_work_ilocked(struct binder_work *work)
862{
863 list_del_init(&work->entry);
864}
865
866/**
867 * binder_dequeue_work() - Removes an item from the work list
868 * @proc: binder_proc associated with list
869 * @work: struct binder_work to remove from list
870 *
871 * Removes the specified work item from whatever list it is on.
872 * Can safely be called if work is not on any list.
873 */
874static void
875binder_dequeue_work(struct binder_proc *proc, struct binder_work *work)
876{
877 binder_inner_proc_lock(proc);
878 binder_dequeue_work_ilocked(work);
879 binder_inner_proc_unlock(proc);
880}
881
882static struct binder_work *binder_dequeue_work_head_ilocked(
883 struct list_head *list)
884{
885 struct binder_work *w;
886
887 w = list_first_entry_or_null(list, struct binder_work, entry);
888 if (w)
889 list_del_init(&w->entry);
890 return w;
891}
892
893/**
894 * binder_dequeue_work_head() - Dequeues the item at head of list
895 * @proc: binder_proc associated with list
896 * @list: list to dequeue head
897 *
898 * Removes the head of the list if there are items on the list
899 *
900 * Return: pointer dequeued binder_work, NULL if list was empty
901 */
902static struct binder_work *binder_dequeue_work_head(
903 struct binder_proc *proc,
904 struct list_head *list)
905{
906 struct binder_work *w;
907
908 binder_inner_proc_lock(proc);
909 w = binder_dequeue_work_head_ilocked(list);
910 binder_inner_proc_unlock(proc);
911 return w;
912}
913
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900914static void
915binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
Todd Kjos7a4408c2017-06-29 12:01:57 -0700916static void binder_free_thread(struct binder_thread *thread);
917static void binder_free_proc(struct binder_proc *proc);
Todd Kjosda0fa9e2017-06-29 12:02:04 -0700918static void binder_inc_node_tmpref_ilocked(struct binder_node *node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900919
Sachin Kamatefde99c2012-08-17 16:39:36 +0530920static int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900921{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900922 unsigned long rlim_cur;
923 unsigned long irqs;
Todd Kjos7f3dc002017-11-27 09:32:33 -0800924 int ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900925
Todd Kjos7f3dc002017-11-27 09:32:33 -0800926 mutex_lock(&proc->files_lock);
927 if (proc->files == NULL) {
928 ret = -ESRCH;
929 goto err;
930 }
931 if (!lock_task_sighand(proc->tsk, &irqs)) {
932 ret = -EMFILE;
933 goto err;
934 }
Al Virodcfadfa2012-08-12 17:27:30 -0400935 rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
936 unlock_task_sighand(proc->tsk, &irqs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900937
Todd Kjos7f3dc002017-11-27 09:32:33 -0800938 ret = __alloc_fd(proc->files, 0, rlim_cur, flags);
939err:
940 mutex_unlock(&proc->files_lock);
941 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900942}
943
944/*
945 * copied from fd_install
946 */
947static void task_fd_install(
948 struct binder_proc *proc, unsigned int fd, struct file *file)
949{
Todd Kjos7f3dc002017-11-27 09:32:33 -0800950 mutex_lock(&proc->files_lock);
Al Virof869e8a2012-08-15 21:06:33 -0400951 if (proc->files)
952 __fd_install(proc->files, fd, file);
Todd Kjos7f3dc002017-11-27 09:32:33 -0800953 mutex_unlock(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900954}
955
956/*
957 * copied from sys_close
958 */
959static long task_close_fd(struct binder_proc *proc, unsigned int fd)
960{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900961 int retval;
962
Todd Kjos7f3dc002017-11-27 09:32:33 -0800963 mutex_lock(&proc->files_lock);
964 if (proc->files == NULL) {
965 retval = -ESRCH;
966 goto err;
967 }
Al Viro483ce1d2012-08-19 12:04:24 -0400968 retval = __close_fd(proc->files, fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900969 /* can't restart close syscall because file table entry was cleared */
970 if (unlikely(retval == -ERESTARTSYS ||
971 retval == -ERESTARTNOINTR ||
972 retval == -ERESTARTNOHAND ||
973 retval == -ERESTART_RESTARTBLOCK))
974 retval = -EINTR;
Todd Kjos7f3dc002017-11-27 09:32:33 -0800975err:
976 mutex_unlock(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900977 return retval;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900978}
979
Martijn Coenen1b77e9d2017-08-31 10:04:18 +0200980static bool binder_has_work_ilocked(struct binder_thread *thread,
981 bool do_proc_work)
982{
Martijn Coenen148ade22017-11-15 09:21:35 +0100983 return thread->process_todo ||
Martijn Coenen1b77e9d2017-08-31 10:04:18 +0200984 thread->looper_need_return ||
985 (do_proc_work &&
986 !binder_worklist_empty_ilocked(&thread->proc->todo));
987}
988
989static bool binder_has_work(struct binder_thread *thread, bool do_proc_work)
990{
991 bool has_work;
992
993 binder_inner_proc_lock(thread->proc);
994 has_work = binder_has_work_ilocked(thread, do_proc_work);
995 binder_inner_proc_unlock(thread->proc);
996
997 return has_work;
998}
999
1000static bool binder_available_for_proc_work_ilocked(struct binder_thread *thread)
1001{
1002 return !thread->transaction_stack &&
1003 binder_worklist_empty_ilocked(&thread->todo) &&
1004 (thread->looper & (BINDER_LOOPER_STATE_ENTERED |
1005 BINDER_LOOPER_STATE_REGISTERED));
1006}
1007
1008static void binder_wakeup_poll_threads_ilocked(struct binder_proc *proc,
1009 bool sync)
1010{
1011 struct rb_node *n;
1012 struct binder_thread *thread;
1013
1014 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
1015 thread = rb_entry(n, struct binder_thread, rb_node);
1016 if (thread->looper & BINDER_LOOPER_STATE_POLL &&
1017 binder_available_for_proc_work_ilocked(thread)) {
1018 if (sync)
1019 wake_up_interruptible_sync(&thread->wait);
1020 else
1021 wake_up_interruptible(&thread->wait);
1022 }
1023 }
1024}
1025
Martijn Coenen408c68b2017-08-31 10:04:19 +02001026/**
1027 * binder_select_thread_ilocked() - selects a thread for doing proc work.
1028 * @proc: process to select a thread from
1029 *
1030 * Note that calling this function moves the thread off the waiting_threads
1031 * list, so it can only be woken up by the caller of this function, or a
1032 * signal. Therefore, callers *should* always wake up the thread this function
1033 * returns.
1034 *
1035 * Return: If there's a thread currently waiting for process work,
1036 * returns that thread. Otherwise returns NULL.
1037 */
1038static struct binder_thread *
1039binder_select_thread_ilocked(struct binder_proc *proc)
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02001040{
1041 struct binder_thread *thread;
1042
Martijn Coenen858b2712017-08-31 10:04:26 +02001043 assert_spin_locked(&proc->inner_lock);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02001044 thread = list_first_entry_or_null(&proc->waiting_threads,
1045 struct binder_thread,
1046 waiting_thread_node);
1047
Martijn Coenen408c68b2017-08-31 10:04:19 +02001048 if (thread)
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02001049 list_del_init(&thread->waiting_thread_node);
Martijn Coenen408c68b2017-08-31 10:04:19 +02001050
1051 return thread;
1052}
1053
1054/**
1055 * binder_wakeup_thread_ilocked() - wakes up a thread for doing proc work.
1056 * @proc: process to wake up a thread in
1057 * @thread: specific thread to wake-up (may be NULL)
1058 * @sync: whether to do a synchronous wake-up
1059 *
1060 * This function wakes up a thread in the @proc process.
1061 * The caller may provide a specific thread to wake-up in
1062 * the @thread parameter. If @thread is NULL, this function
1063 * will wake up threads that have called poll().
1064 *
1065 * Note that for this function to work as expected, callers
1066 * should first call binder_select_thread() to find a thread
1067 * to handle the work (if they don't have a thread already),
1068 * and pass the result into the @thread parameter.
1069 */
1070static void binder_wakeup_thread_ilocked(struct binder_proc *proc,
1071 struct binder_thread *thread,
1072 bool sync)
1073{
Martijn Coenen858b2712017-08-31 10:04:26 +02001074 assert_spin_locked(&proc->inner_lock);
Martijn Coenen408c68b2017-08-31 10:04:19 +02001075
1076 if (thread) {
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02001077 if (sync)
1078 wake_up_interruptible_sync(&thread->wait);
1079 else
1080 wake_up_interruptible(&thread->wait);
1081 return;
1082 }
1083
1084 /* Didn't find a thread waiting for proc work; this can happen
1085 * in two scenarios:
1086 * 1. All threads are busy handling transactions
1087 * In that case, one of those threads should call back into
1088 * the kernel driver soon and pick up this work.
1089 * 2. Threads are using the (e)poll interface, in which case
1090 * they may be blocked on the waitqueue without having been
1091 * added to waiting_threads. For this case, we just iterate
1092 * over all threads not handling transaction work, and
1093 * wake them all up. We wake all because we don't know whether
1094 * a thread that called into (e)poll is handling non-binder
1095 * work currently.
1096 */
1097 binder_wakeup_poll_threads_ilocked(proc, sync);
1098}
1099
Martijn Coenen408c68b2017-08-31 10:04:19 +02001100static void binder_wakeup_proc_ilocked(struct binder_proc *proc)
1101{
1102 struct binder_thread *thread = binder_select_thread_ilocked(proc);
1103
1104 binder_wakeup_thread_ilocked(proc, thread, /* sync = */false);
1105}
1106
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001107static void binder_set_nice(long nice)
1108{
1109 long min_nice;
Seunghun Lee10f62862014-05-01 01:30:23 +09001110
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001111 if (can_nice(current, nice)) {
1112 set_user_nice(current, nice);
1113 return;
1114 }
Krzysztof Opasiakc3643b62017-07-05 19:24:44 +02001115 min_nice = rlimit_to_nice(rlimit(RLIMIT_NICE));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001116 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301117 "%d: nice value %ld not allowed use %ld instead\n",
1118 current->pid, nice, min_nice);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001119 set_user_nice(current, min_nice);
Dongsheng Yang8698a742014-03-11 18:09:12 +08001120 if (min_nice <= MAX_NICE)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001121 return;
Anmol Sarma56b468f2012-10-30 22:35:43 +05301122 binder_user_error("%d RLIMIT_NICE not set\n", current->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001123}
1124
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001125static struct binder_node *binder_get_node_ilocked(struct binder_proc *proc,
1126 binder_uintptr_t ptr)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001127{
1128 struct rb_node *n = proc->nodes.rb_node;
1129 struct binder_node *node;
1130
Martijn Coenen858b2712017-08-31 10:04:26 +02001131 assert_spin_locked(&proc->inner_lock);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001132
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001133 while (n) {
1134 node = rb_entry(n, struct binder_node, rb_node);
1135
1136 if (ptr < node->ptr)
1137 n = n->rb_left;
1138 else if (ptr > node->ptr)
1139 n = n->rb_right;
Todd Kjosadc18842017-06-29 12:01:59 -07001140 else {
1141 /*
1142 * take an implicit weak reference
1143 * to ensure node stays alive until
1144 * call to binder_put_node()
1145 */
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001146 binder_inc_node_tmpref_ilocked(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001147 return node;
Todd Kjosadc18842017-06-29 12:01:59 -07001148 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001149 }
1150 return NULL;
1151}
1152
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001153static struct binder_node *binder_get_node(struct binder_proc *proc,
1154 binder_uintptr_t ptr)
1155{
1156 struct binder_node *node;
1157
1158 binder_inner_proc_lock(proc);
1159 node = binder_get_node_ilocked(proc, ptr);
1160 binder_inner_proc_unlock(proc);
1161 return node;
1162}
1163
1164static struct binder_node *binder_init_node_ilocked(
1165 struct binder_proc *proc,
1166 struct binder_node *new_node,
1167 struct flat_binder_object *fp)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001168{
1169 struct rb_node **p = &proc->nodes.rb_node;
1170 struct rb_node *parent = NULL;
1171 struct binder_node *node;
Todd Kjos673068e2017-06-29 12:02:03 -07001172 binder_uintptr_t ptr = fp ? fp->binder : 0;
1173 binder_uintptr_t cookie = fp ? fp->cookie : 0;
1174 __u32 flags = fp ? fp->flags : 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001175
Martijn Coenen858b2712017-08-31 10:04:26 +02001176 assert_spin_locked(&proc->inner_lock);
1177
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001178 while (*p) {
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001179
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001180 parent = *p;
1181 node = rb_entry(parent, struct binder_node, rb_node);
1182
1183 if (ptr < node->ptr)
1184 p = &(*p)->rb_left;
1185 else if (ptr > node->ptr)
1186 p = &(*p)->rb_right;
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001187 else {
1188 /*
1189 * A matching node is already in
1190 * the rb tree. Abandon the init
1191 * and return it.
1192 */
1193 binder_inc_node_tmpref_ilocked(node);
1194 return node;
1195 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001196 }
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001197 node = new_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001198 binder_stats_created(BINDER_STAT_NODE);
Todd Kjosadc18842017-06-29 12:01:59 -07001199 node->tmp_refs++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001200 rb_link_node(&node->rb_node, parent, p);
1201 rb_insert_color(&node->rb_node, &proc->nodes);
Todd Kjos656a8002017-06-29 12:01:45 -07001202 node->debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001203 node->proc = proc;
1204 node->ptr = ptr;
1205 node->cookie = cookie;
1206 node->work.type = BINDER_WORK_NODE;
Todd Kjos673068e2017-06-29 12:02:03 -07001207 node->min_priority = flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
1208 node->accept_fds = !!(flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
Todd Kjos9630fe82017-06-29 12:02:00 -07001209 spin_lock_init(&node->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001210 INIT_LIST_HEAD(&node->work.entry);
1211 INIT_LIST_HEAD(&node->async_todo);
1212 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001213 "%d:%d node %d u%016llx c%016llx created\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001214 proc->pid, current->pid, node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001215 (u64)node->ptr, (u64)node->cookie);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001216
1217 return node;
1218}
1219
1220static struct binder_node *binder_new_node(struct binder_proc *proc,
1221 struct flat_binder_object *fp)
1222{
1223 struct binder_node *node;
1224 struct binder_node *new_node = kzalloc(sizeof(*node), GFP_KERNEL);
1225
1226 if (!new_node)
1227 return NULL;
1228 binder_inner_proc_lock(proc);
1229 node = binder_init_node_ilocked(proc, new_node, fp);
1230 binder_inner_proc_unlock(proc);
1231 if (node != new_node)
1232 /*
1233 * The node was already added by another thread
1234 */
1235 kfree(new_node);
1236
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001237 return node;
1238}
1239
Todd Kjosed297212017-06-29 12:02:01 -07001240static void binder_free_node(struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001241{
Todd Kjosed297212017-06-29 12:02:01 -07001242 kfree(node);
1243 binder_stats_deleted(BINDER_STAT_NODE);
1244}
1245
Todd Kjos673068e2017-06-29 12:02:03 -07001246static int binder_inc_node_nilocked(struct binder_node *node, int strong,
1247 int internal,
1248 struct list_head *target_list)
Todd Kjosed297212017-06-29 12:02:01 -07001249{
Todd Kjos673068e2017-06-29 12:02:03 -07001250 struct binder_proc *proc = node->proc;
1251
Martijn Coenen858b2712017-08-31 10:04:26 +02001252 assert_spin_locked(&node->lock);
Todd Kjos673068e2017-06-29 12:02:03 -07001253 if (proc)
Martijn Coenen858b2712017-08-31 10:04:26 +02001254 assert_spin_locked(&proc->inner_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001255 if (strong) {
1256 if (internal) {
1257 if (target_list == NULL &&
1258 node->internal_strong_refs == 0 &&
Martijn Coenen342e5c92017-02-03 14:40:46 -08001259 !(node->proc &&
1260 node == node->proc->context->binder_context_mgr_node &&
1261 node->has_strong_ref)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301262 pr_err("invalid inc strong node for %d\n",
1263 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001264 return -EINVAL;
1265 }
1266 node->internal_strong_refs++;
1267 } else
1268 node->local_strong_refs++;
1269 if (!node->has_strong_ref && target_list) {
Todd Kjos72196392017-06-29 12:02:02 -07001270 binder_dequeue_work_ilocked(&node->work);
Martijn Coenen148ade22017-11-15 09:21:35 +01001271 /*
1272 * Note: this function is the only place where we queue
1273 * directly to a thread->todo without using the
1274 * corresponding binder_enqueue_thread_work() helper
1275 * functions; in this case it's ok to not set the
1276 * process_todo flag, since we know this node work will
1277 * always be followed by other work that starts queue
1278 * processing: in case of synchronous transactions, a
1279 * BR_REPLY or BR_ERROR; in case of oneway
1280 * transactions, a BR_TRANSACTION_COMPLETE.
1281 */
Todd Kjos72196392017-06-29 12:02:02 -07001282 binder_enqueue_work_ilocked(&node->work, target_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001283 }
1284 } else {
1285 if (!internal)
1286 node->local_weak_refs++;
1287 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
1288 if (target_list == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301289 pr_err("invalid inc weak node for %d\n",
1290 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001291 return -EINVAL;
1292 }
Martijn Coenen148ade22017-11-15 09:21:35 +01001293 /*
1294 * See comment above
1295 */
Todd Kjos72196392017-06-29 12:02:02 -07001296 binder_enqueue_work_ilocked(&node->work, target_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001297 }
1298 }
1299 return 0;
1300}
1301
Todd Kjosed297212017-06-29 12:02:01 -07001302static int binder_inc_node(struct binder_node *node, int strong, int internal,
1303 struct list_head *target_list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001304{
Todd Kjosed297212017-06-29 12:02:01 -07001305 int ret;
1306
Todd Kjos673068e2017-06-29 12:02:03 -07001307 binder_node_inner_lock(node);
1308 ret = binder_inc_node_nilocked(node, strong, internal, target_list);
1309 binder_node_inner_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07001310
1311 return ret;
1312}
1313
Todd Kjos673068e2017-06-29 12:02:03 -07001314static bool binder_dec_node_nilocked(struct binder_node *node,
1315 int strong, int internal)
Todd Kjosed297212017-06-29 12:02:01 -07001316{
1317 struct binder_proc *proc = node->proc;
1318
Martijn Coenen858b2712017-08-31 10:04:26 +02001319 assert_spin_locked(&node->lock);
Todd Kjosed297212017-06-29 12:02:01 -07001320 if (proc)
Martijn Coenen858b2712017-08-31 10:04:26 +02001321 assert_spin_locked(&proc->inner_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001322 if (strong) {
1323 if (internal)
1324 node->internal_strong_refs--;
1325 else
1326 node->local_strong_refs--;
1327 if (node->local_strong_refs || node->internal_strong_refs)
Todd Kjosed297212017-06-29 12:02:01 -07001328 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001329 } else {
1330 if (!internal)
1331 node->local_weak_refs--;
Todd Kjosadc18842017-06-29 12:01:59 -07001332 if (node->local_weak_refs || node->tmp_refs ||
1333 !hlist_empty(&node->refs))
Todd Kjosed297212017-06-29 12:02:01 -07001334 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001335 }
Todd Kjosed297212017-06-29 12:02:01 -07001336
1337 if (proc && (node->has_strong_ref || node->has_weak_ref)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001338 if (list_empty(&node->work.entry)) {
Todd Kjos72196392017-06-29 12:02:02 -07001339 binder_enqueue_work_ilocked(&node->work, &proc->todo);
Martijn Coenen408c68b2017-08-31 10:04:19 +02001340 binder_wakeup_proc_ilocked(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001341 }
1342 } else {
1343 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
Todd Kjosadc18842017-06-29 12:01:59 -07001344 !node->local_weak_refs && !node->tmp_refs) {
Todd Kjosed297212017-06-29 12:02:01 -07001345 if (proc) {
Todd Kjos72196392017-06-29 12:02:02 -07001346 binder_dequeue_work_ilocked(&node->work);
1347 rb_erase(&node->rb_node, &proc->nodes);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001348 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301349 "refless node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001350 node->debug_id);
1351 } else {
Todd Kjos72196392017-06-29 12:02:02 -07001352 BUG_ON(!list_empty(&node->work.entry));
Todd Kjosc44b1232017-06-29 12:01:43 -07001353 spin_lock(&binder_dead_nodes_lock);
Todd Kjosed297212017-06-29 12:02:01 -07001354 /*
1355 * tmp_refs could have changed so
1356 * check it again
1357 */
1358 if (node->tmp_refs) {
1359 spin_unlock(&binder_dead_nodes_lock);
1360 return false;
1361 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001362 hlist_del(&node->dead_node);
Todd Kjosc44b1232017-06-29 12:01:43 -07001363 spin_unlock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001364 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301365 "dead node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001366 node->debug_id);
1367 }
Todd Kjosed297212017-06-29 12:02:01 -07001368 return true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001369 }
1370 }
Todd Kjosed297212017-06-29 12:02:01 -07001371 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001372}
1373
Todd Kjosed297212017-06-29 12:02:01 -07001374static void binder_dec_node(struct binder_node *node, int strong, int internal)
1375{
1376 bool free_node;
1377
Todd Kjos673068e2017-06-29 12:02:03 -07001378 binder_node_inner_lock(node);
1379 free_node = binder_dec_node_nilocked(node, strong, internal);
1380 binder_node_inner_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07001381 if (free_node)
1382 binder_free_node(node);
1383}
1384
1385static void binder_inc_node_tmpref_ilocked(struct binder_node *node)
Todd Kjosadc18842017-06-29 12:01:59 -07001386{
1387 /*
1388 * No call to binder_inc_node() is needed since we
1389 * don't need to inform userspace of any changes to
1390 * tmp_refs
1391 */
1392 node->tmp_refs++;
1393}
1394
1395/**
Todd Kjosed297212017-06-29 12:02:01 -07001396 * binder_inc_node_tmpref() - take a temporary reference on node
1397 * @node: node to reference
1398 *
1399 * Take reference on node to prevent the node from being freed
1400 * while referenced only by a local variable. The inner lock is
1401 * needed to serialize with the node work on the queue (which
1402 * isn't needed after the node is dead). If the node is dead
1403 * (node->proc is NULL), use binder_dead_nodes_lock to protect
1404 * node->tmp_refs against dead-node-only cases where the node
1405 * lock cannot be acquired (eg traversing the dead node list to
1406 * print nodes)
1407 */
1408static void binder_inc_node_tmpref(struct binder_node *node)
1409{
Todd Kjos673068e2017-06-29 12:02:03 -07001410 binder_node_lock(node);
Todd Kjosed297212017-06-29 12:02:01 -07001411 if (node->proc)
1412 binder_inner_proc_lock(node->proc);
1413 else
1414 spin_lock(&binder_dead_nodes_lock);
1415 binder_inc_node_tmpref_ilocked(node);
1416 if (node->proc)
1417 binder_inner_proc_unlock(node->proc);
1418 else
1419 spin_unlock(&binder_dead_nodes_lock);
Todd Kjos673068e2017-06-29 12:02:03 -07001420 binder_node_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07001421}
1422
1423/**
Todd Kjosadc18842017-06-29 12:01:59 -07001424 * binder_dec_node_tmpref() - remove a temporary reference on node
1425 * @node: node to reference
1426 *
1427 * Release temporary reference on node taken via binder_inc_node_tmpref()
1428 */
1429static void binder_dec_node_tmpref(struct binder_node *node)
1430{
Todd Kjosed297212017-06-29 12:02:01 -07001431 bool free_node;
1432
Todd Kjos673068e2017-06-29 12:02:03 -07001433 binder_node_inner_lock(node);
1434 if (!node->proc)
Todd Kjosed297212017-06-29 12:02:01 -07001435 spin_lock(&binder_dead_nodes_lock);
Todd Kjosadc18842017-06-29 12:01:59 -07001436 node->tmp_refs--;
1437 BUG_ON(node->tmp_refs < 0);
Todd Kjosed297212017-06-29 12:02:01 -07001438 if (!node->proc)
1439 spin_unlock(&binder_dead_nodes_lock);
Todd Kjosadc18842017-06-29 12:01:59 -07001440 /*
1441 * Call binder_dec_node() to check if all refcounts are 0
1442 * and cleanup is needed. Calling with strong=0 and internal=1
1443 * causes no actual reference to be released in binder_dec_node().
1444 * If that changes, a change is needed here too.
1445 */
Todd Kjos673068e2017-06-29 12:02:03 -07001446 free_node = binder_dec_node_nilocked(node, 0, 1);
1447 binder_node_inner_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07001448 if (free_node)
1449 binder_free_node(node);
Todd Kjosadc18842017-06-29 12:01:59 -07001450}
1451
1452static void binder_put_node(struct binder_node *node)
1453{
1454 binder_dec_node_tmpref(node);
1455}
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001456
Todd Kjos2c1838d2017-06-29 12:02:08 -07001457static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc,
1458 u32 desc, bool need_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001459{
1460 struct rb_node *n = proc->refs_by_desc.rb_node;
1461 struct binder_ref *ref;
1462
1463 while (n) {
1464 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1465
Todd Kjos372e3142017-06-29 12:01:58 -07001466 if (desc < ref->data.desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001467 n = n->rb_left;
Todd Kjos372e3142017-06-29 12:01:58 -07001468 } else if (desc > ref->data.desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001469 n = n->rb_right;
Todd Kjos372e3142017-06-29 12:01:58 -07001470 } else if (need_strong_ref && !ref->data.strong) {
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001471 binder_user_error("tried to use weak ref as strong ref\n");
1472 return NULL;
1473 } else {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001474 return ref;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001475 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001476 }
1477 return NULL;
1478}
1479
Todd Kjos372e3142017-06-29 12:01:58 -07001480/**
Todd Kjos2c1838d2017-06-29 12:02:08 -07001481 * binder_get_ref_for_node_olocked() - get the ref associated with given node
Todd Kjos372e3142017-06-29 12:01:58 -07001482 * @proc: binder_proc that owns the ref
1483 * @node: binder_node of target
1484 * @new_ref: newly allocated binder_ref to be initialized or %NULL
1485 *
1486 * Look up the ref for the given node and return it if it exists
1487 *
1488 * If it doesn't exist and the caller provides a newly allocated
1489 * ref, initialize the fields of the newly allocated ref and insert
1490 * into the given proc rb_trees and node refs list.
1491 *
1492 * Return: the ref for node. It is possible that another thread
1493 * allocated/initialized the ref first in which case the
1494 * returned ref would be different than the passed-in
1495 * new_ref. new_ref must be kfree'd by the caller in
1496 * this case.
1497 */
Todd Kjos2c1838d2017-06-29 12:02:08 -07001498static struct binder_ref *binder_get_ref_for_node_olocked(
1499 struct binder_proc *proc,
1500 struct binder_node *node,
1501 struct binder_ref *new_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001502{
Todd Kjos372e3142017-06-29 12:01:58 -07001503 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001504 struct rb_node **p = &proc->refs_by_node.rb_node;
1505 struct rb_node *parent = NULL;
Todd Kjos372e3142017-06-29 12:01:58 -07001506 struct binder_ref *ref;
1507 struct rb_node *n;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001508
1509 while (*p) {
1510 parent = *p;
1511 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1512
1513 if (node < ref->node)
1514 p = &(*p)->rb_left;
1515 else if (node > ref->node)
1516 p = &(*p)->rb_right;
1517 else
1518 return ref;
1519 }
Todd Kjos372e3142017-06-29 12:01:58 -07001520 if (!new_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001521 return NULL;
Todd Kjos372e3142017-06-29 12:01:58 -07001522
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001523 binder_stats_created(BINDER_STAT_REF);
Todd Kjos372e3142017-06-29 12:01:58 -07001524 new_ref->data.debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001525 new_ref->proc = proc;
1526 new_ref->node = node;
1527 rb_link_node(&new_ref->rb_node_node, parent, p);
1528 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1529
Todd Kjos372e3142017-06-29 12:01:58 -07001530 new_ref->data.desc = (node == context->binder_context_mgr_node) ? 0 : 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001531 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1532 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Todd Kjos372e3142017-06-29 12:01:58 -07001533 if (ref->data.desc > new_ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001534 break;
Todd Kjos372e3142017-06-29 12:01:58 -07001535 new_ref->data.desc = ref->data.desc + 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001536 }
1537
1538 p = &proc->refs_by_desc.rb_node;
1539 while (*p) {
1540 parent = *p;
1541 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1542
Todd Kjos372e3142017-06-29 12:01:58 -07001543 if (new_ref->data.desc < ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001544 p = &(*p)->rb_left;
Todd Kjos372e3142017-06-29 12:01:58 -07001545 else if (new_ref->data.desc > ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001546 p = &(*p)->rb_right;
1547 else
1548 BUG();
1549 }
1550 rb_link_node(&new_ref->rb_node_desc, parent, p);
1551 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
Todd Kjos673068e2017-06-29 12:02:03 -07001552
1553 binder_node_lock(node);
Todd Kjose4cffcf2017-06-29 12:01:50 -07001554 hlist_add_head(&new_ref->node_entry, &node->refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001555
Todd Kjose4cffcf2017-06-29 12:01:50 -07001556 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1557 "%d new ref %d desc %d for node %d\n",
Todd Kjos372e3142017-06-29 12:01:58 -07001558 proc->pid, new_ref->data.debug_id, new_ref->data.desc,
Todd Kjose4cffcf2017-06-29 12:01:50 -07001559 node->debug_id);
Todd Kjos673068e2017-06-29 12:02:03 -07001560 binder_node_unlock(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001561 return new_ref;
1562}
1563
Todd Kjos2c1838d2017-06-29 12:02:08 -07001564static void binder_cleanup_ref_olocked(struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001565{
Todd Kjosed297212017-06-29 12:02:01 -07001566 bool delete_node = false;
Todd Kjosed297212017-06-29 12:02:01 -07001567
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001568 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301569 "%d delete ref %d desc %d for node %d\n",
Todd Kjos372e3142017-06-29 12:01:58 -07001570 ref->proc->pid, ref->data.debug_id, ref->data.desc,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301571 ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001572
1573 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1574 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
Todd Kjos372e3142017-06-29 12:01:58 -07001575
Todd Kjos673068e2017-06-29 12:02:03 -07001576 binder_node_inner_lock(ref->node);
Todd Kjos372e3142017-06-29 12:01:58 -07001577 if (ref->data.strong)
Todd Kjos673068e2017-06-29 12:02:03 -07001578 binder_dec_node_nilocked(ref->node, 1, 1);
Todd Kjos372e3142017-06-29 12:01:58 -07001579
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001580 hlist_del(&ref->node_entry);
Todd Kjos673068e2017-06-29 12:02:03 -07001581 delete_node = binder_dec_node_nilocked(ref->node, 0, 1);
1582 binder_node_inner_unlock(ref->node);
Todd Kjosed297212017-06-29 12:02:01 -07001583 /*
1584 * Clear ref->node unless we want the caller to free the node
1585 */
1586 if (!delete_node) {
1587 /*
1588 * The caller uses ref->node to determine
1589 * whether the node needs to be freed. Clear
1590 * it since the node is still alive.
1591 */
1592 ref->node = NULL;
1593 }
Todd Kjos372e3142017-06-29 12:01:58 -07001594
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001595 if (ref->death) {
1596 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301597 "%d delete ref %d desc %d has death notification\n",
Todd Kjos372e3142017-06-29 12:01:58 -07001598 ref->proc->pid, ref->data.debug_id,
1599 ref->data.desc);
Todd Kjos72196392017-06-29 12:02:02 -07001600 binder_dequeue_work(ref->proc, &ref->death->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001601 binder_stats_deleted(BINDER_STAT_DEATH);
1602 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001603 binder_stats_deleted(BINDER_STAT_REF);
1604}
1605
Todd Kjos372e3142017-06-29 12:01:58 -07001606/**
Todd Kjos2c1838d2017-06-29 12:02:08 -07001607 * binder_inc_ref_olocked() - increment the ref for given handle
Todd Kjos372e3142017-06-29 12:01:58 -07001608 * @ref: ref to be incremented
1609 * @strong: if true, strong increment, else weak
1610 * @target_list: list to queue node work on
1611 *
Todd Kjos2c1838d2017-06-29 12:02:08 -07001612 * Increment the ref. @ref->proc->outer_lock must be held on entry
Todd Kjos372e3142017-06-29 12:01:58 -07001613 *
1614 * Return: 0, if successful, else errno
1615 */
Todd Kjos2c1838d2017-06-29 12:02:08 -07001616static int binder_inc_ref_olocked(struct binder_ref *ref, int strong,
1617 struct list_head *target_list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001618{
1619 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +09001620
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001621 if (strong) {
Todd Kjos372e3142017-06-29 12:01:58 -07001622 if (ref->data.strong == 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001623 ret = binder_inc_node(ref->node, 1, 1, target_list);
1624 if (ret)
1625 return ret;
1626 }
Todd Kjos372e3142017-06-29 12:01:58 -07001627 ref->data.strong++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001628 } else {
Todd Kjos372e3142017-06-29 12:01:58 -07001629 if (ref->data.weak == 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001630 ret = binder_inc_node(ref->node, 0, 1, target_list);
1631 if (ret)
1632 return ret;
1633 }
Todd Kjos372e3142017-06-29 12:01:58 -07001634 ref->data.weak++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001635 }
1636 return 0;
1637}
1638
Todd Kjos372e3142017-06-29 12:01:58 -07001639/**
1640 * binder_dec_ref() - dec the ref for given handle
1641 * @ref: ref to be decremented
1642 * @strong: if true, strong decrement, else weak
1643 *
1644 * Decrement the ref.
1645 *
Todd Kjos372e3142017-06-29 12:01:58 -07001646 * Return: true if ref is cleaned up and ready to be freed
1647 */
Todd Kjos2c1838d2017-06-29 12:02:08 -07001648static bool binder_dec_ref_olocked(struct binder_ref *ref, int strong)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001649{
1650 if (strong) {
Todd Kjos372e3142017-06-29 12:01:58 -07001651 if (ref->data.strong == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301652 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
Todd Kjos372e3142017-06-29 12:01:58 -07001653 ref->proc->pid, ref->data.debug_id,
1654 ref->data.desc, ref->data.strong,
1655 ref->data.weak);
1656 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001657 }
Todd Kjos372e3142017-06-29 12:01:58 -07001658 ref->data.strong--;
Todd Kjosed297212017-06-29 12:02:01 -07001659 if (ref->data.strong == 0)
1660 binder_dec_node(ref->node, strong, 1);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001661 } else {
Todd Kjos372e3142017-06-29 12:01:58 -07001662 if (ref->data.weak == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301663 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
Todd Kjos372e3142017-06-29 12:01:58 -07001664 ref->proc->pid, ref->data.debug_id,
1665 ref->data.desc, ref->data.strong,
1666 ref->data.weak);
1667 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001668 }
Todd Kjos372e3142017-06-29 12:01:58 -07001669 ref->data.weak--;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001670 }
Todd Kjos372e3142017-06-29 12:01:58 -07001671 if (ref->data.strong == 0 && ref->data.weak == 0) {
Todd Kjos2c1838d2017-06-29 12:02:08 -07001672 binder_cleanup_ref_olocked(ref);
Todd Kjos372e3142017-06-29 12:01:58 -07001673 return true;
1674 }
1675 return false;
1676}
1677
1678/**
1679 * binder_get_node_from_ref() - get the node from the given proc/desc
1680 * @proc: proc containing the ref
1681 * @desc: the handle associated with the ref
1682 * @need_strong_ref: if true, only return node if ref is strong
1683 * @rdata: the id/refcount data for the ref
1684 *
1685 * Given a proc and ref handle, return the associated binder_node
1686 *
1687 * Return: a binder_node or NULL if not found or not strong when strong required
1688 */
1689static struct binder_node *binder_get_node_from_ref(
1690 struct binder_proc *proc,
1691 u32 desc, bool need_strong_ref,
1692 struct binder_ref_data *rdata)
1693{
1694 struct binder_node *node;
1695 struct binder_ref *ref;
1696
Todd Kjos2c1838d2017-06-29 12:02:08 -07001697 binder_proc_lock(proc);
1698 ref = binder_get_ref_olocked(proc, desc, need_strong_ref);
Todd Kjos372e3142017-06-29 12:01:58 -07001699 if (!ref)
1700 goto err_no_ref;
1701 node = ref->node;
Todd Kjosadc18842017-06-29 12:01:59 -07001702 /*
1703 * Take an implicit reference on the node to ensure
1704 * it stays alive until the call to binder_put_node()
1705 */
1706 binder_inc_node_tmpref(node);
Todd Kjos372e3142017-06-29 12:01:58 -07001707 if (rdata)
1708 *rdata = ref->data;
Todd Kjos2c1838d2017-06-29 12:02:08 -07001709 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07001710
1711 return node;
1712
1713err_no_ref:
Todd Kjos2c1838d2017-06-29 12:02:08 -07001714 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07001715 return NULL;
1716}
1717
1718/**
1719 * binder_free_ref() - free the binder_ref
1720 * @ref: ref to free
1721 *
Todd Kjosed297212017-06-29 12:02:01 -07001722 * Free the binder_ref. Free the binder_node indicated by ref->node
1723 * (if non-NULL) and the binder_ref_death indicated by ref->death.
Todd Kjos372e3142017-06-29 12:01:58 -07001724 */
1725static void binder_free_ref(struct binder_ref *ref)
1726{
Todd Kjosed297212017-06-29 12:02:01 -07001727 if (ref->node)
1728 binder_free_node(ref->node);
Todd Kjos372e3142017-06-29 12:01:58 -07001729 kfree(ref->death);
1730 kfree(ref);
1731}
1732
1733/**
1734 * binder_update_ref_for_handle() - inc/dec the ref for given handle
1735 * @proc: proc containing the ref
1736 * @desc: the handle associated with the ref
1737 * @increment: true=inc reference, false=dec reference
1738 * @strong: true=strong reference, false=weak reference
1739 * @rdata: the id/refcount data for the ref
1740 *
1741 * Given a proc and ref handle, increment or decrement the ref
1742 * according to "increment" arg.
1743 *
1744 * Return: 0 if successful, else errno
1745 */
1746static int binder_update_ref_for_handle(struct binder_proc *proc,
1747 uint32_t desc, bool increment, bool strong,
1748 struct binder_ref_data *rdata)
1749{
1750 int ret = 0;
1751 struct binder_ref *ref;
1752 bool delete_ref = false;
1753
Todd Kjos2c1838d2017-06-29 12:02:08 -07001754 binder_proc_lock(proc);
1755 ref = binder_get_ref_olocked(proc, desc, strong);
Todd Kjos372e3142017-06-29 12:01:58 -07001756 if (!ref) {
1757 ret = -EINVAL;
1758 goto err_no_ref;
1759 }
1760 if (increment)
Todd Kjos2c1838d2017-06-29 12:02:08 -07001761 ret = binder_inc_ref_olocked(ref, strong, NULL);
Todd Kjos372e3142017-06-29 12:01:58 -07001762 else
Todd Kjos2c1838d2017-06-29 12:02:08 -07001763 delete_ref = binder_dec_ref_olocked(ref, strong);
Todd Kjos372e3142017-06-29 12:01:58 -07001764
1765 if (rdata)
1766 *rdata = ref->data;
Todd Kjos2c1838d2017-06-29 12:02:08 -07001767 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07001768
1769 if (delete_ref)
1770 binder_free_ref(ref);
1771 return ret;
1772
1773err_no_ref:
Todd Kjos2c1838d2017-06-29 12:02:08 -07001774 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07001775 return ret;
1776}
1777
1778/**
1779 * binder_dec_ref_for_handle() - dec the ref for given handle
1780 * @proc: proc containing the ref
1781 * @desc: the handle associated with the ref
1782 * @strong: true=strong reference, false=weak reference
1783 * @rdata: the id/refcount data for the ref
1784 *
1785 * Just calls binder_update_ref_for_handle() to decrement the ref.
1786 *
1787 * Return: 0 if successful, else errno
1788 */
1789static int binder_dec_ref_for_handle(struct binder_proc *proc,
1790 uint32_t desc, bool strong, struct binder_ref_data *rdata)
1791{
1792 return binder_update_ref_for_handle(proc, desc, false, strong, rdata);
1793}
1794
1795
1796/**
1797 * binder_inc_ref_for_node() - increment the ref for given proc/node
1798 * @proc: proc containing the ref
1799 * @node: target node
1800 * @strong: true=strong reference, false=weak reference
1801 * @target_list: worklist to use if node is incremented
1802 * @rdata: the id/refcount data for the ref
1803 *
1804 * Given a proc and node, increment the ref. Create the ref if it
1805 * doesn't already exist
1806 *
1807 * Return: 0 if successful, else errno
1808 */
1809static int binder_inc_ref_for_node(struct binder_proc *proc,
1810 struct binder_node *node,
1811 bool strong,
1812 struct list_head *target_list,
1813 struct binder_ref_data *rdata)
1814{
1815 struct binder_ref *ref;
1816 struct binder_ref *new_ref = NULL;
1817 int ret = 0;
1818
Todd Kjos2c1838d2017-06-29 12:02:08 -07001819 binder_proc_lock(proc);
1820 ref = binder_get_ref_for_node_olocked(proc, node, NULL);
Todd Kjos372e3142017-06-29 12:01:58 -07001821 if (!ref) {
Todd Kjos2c1838d2017-06-29 12:02:08 -07001822 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07001823 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1824 if (!new_ref)
1825 return -ENOMEM;
Todd Kjos2c1838d2017-06-29 12:02:08 -07001826 binder_proc_lock(proc);
1827 ref = binder_get_ref_for_node_olocked(proc, node, new_ref);
Todd Kjos372e3142017-06-29 12:01:58 -07001828 }
Todd Kjos2c1838d2017-06-29 12:02:08 -07001829 ret = binder_inc_ref_olocked(ref, strong, target_list);
Todd Kjos372e3142017-06-29 12:01:58 -07001830 *rdata = ref->data;
Todd Kjos2c1838d2017-06-29 12:02:08 -07001831 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07001832 if (new_ref && ref != new_ref)
1833 /*
1834 * Another thread created the ref first so
1835 * free the one we allocated
1836 */
1837 kfree(new_ref);
1838 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001839}
1840
Martijn Coenen0b89d692017-06-29 12:02:06 -07001841static void binder_pop_transaction_ilocked(struct binder_thread *target_thread,
1842 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001843{
Todd Kjosb6d282c2017-06-29 12:01:54 -07001844 BUG_ON(!target_thread);
Martijn Coenen858b2712017-08-31 10:04:26 +02001845 assert_spin_locked(&target_thread->proc->inner_lock);
Todd Kjosb6d282c2017-06-29 12:01:54 -07001846 BUG_ON(target_thread->transaction_stack != t);
1847 BUG_ON(target_thread->transaction_stack->from != target_thread);
1848 target_thread->transaction_stack =
1849 target_thread->transaction_stack->from_parent;
1850 t->from = NULL;
1851}
1852
Todd Kjos7a4408c2017-06-29 12:01:57 -07001853/**
1854 * binder_thread_dec_tmpref() - decrement thread->tmp_ref
1855 * @thread: thread to decrement
1856 *
1857 * A thread needs to be kept alive while being used to create or
1858 * handle a transaction. binder_get_txn_from() is used to safely
1859 * extract t->from from a binder_transaction and keep the thread
1860 * indicated by t->from from being freed. When done with that
1861 * binder_thread, this function is called to decrement the
1862 * tmp_ref and free if appropriate (thread has been released
1863 * and no transaction being processed by the driver)
1864 */
1865static void binder_thread_dec_tmpref(struct binder_thread *thread)
1866{
1867 /*
1868 * atomic is used to protect the counter value while
1869 * it cannot reach zero or thread->is_dead is false
Todd Kjos7a4408c2017-06-29 12:01:57 -07001870 */
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07001871 binder_inner_proc_lock(thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07001872 atomic_dec(&thread->tmp_ref);
1873 if (thread->is_dead && !atomic_read(&thread->tmp_ref)) {
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07001874 binder_inner_proc_unlock(thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07001875 binder_free_thread(thread);
1876 return;
1877 }
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07001878 binder_inner_proc_unlock(thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07001879}
1880
1881/**
1882 * binder_proc_dec_tmpref() - decrement proc->tmp_ref
1883 * @proc: proc to decrement
1884 *
1885 * A binder_proc needs to be kept alive while being used to create or
1886 * handle a transaction. proc->tmp_ref is incremented when
1887 * creating a new transaction or the binder_proc is currently in-use
1888 * by threads that are being released. When done with the binder_proc,
1889 * this function is called to decrement the counter and free the
1890 * proc if appropriate (proc has been released, all threads have
1891 * been released and not currenly in-use to process a transaction).
1892 */
1893static void binder_proc_dec_tmpref(struct binder_proc *proc)
1894{
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07001895 binder_inner_proc_lock(proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07001896 proc->tmp_ref--;
1897 if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) &&
1898 !proc->tmp_ref) {
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07001899 binder_inner_proc_unlock(proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07001900 binder_free_proc(proc);
1901 return;
1902 }
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07001903 binder_inner_proc_unlock(proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07001904}
1905
1906/**
1907 * binder_get_txn_from() - safely extract the "from" thread in transaction
1908 * @t: binder transaction for t->from
1909 *
1910 * Atomically return the "from" thread and increment the tmp_ref
1911 * count for the thread to ensure it stays alive until
1912 * binder_thread_dec_tmpref() is called.
1913 *
1914 * Return: the value of t->from
1915 */
1916static struct binder_thread *binder_get_txn_from(
1917 struct binder_transaction *t)
1918{
1919 struct binder_thread *from;
1920
1921 spin_lock(&t->lock);
1922 from = t->from;
1923 if (from)
1924 atomic_inc(&from->tmp_ref);
1925 spin_unlock(&t->lock);
1926 return from;
1927}
1928
Martijn Coenen0b89d692017-06-29 12:02:06 -07001929/**
1930 * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock
1931 * @t: binder transaction for t->from
1932 *
1933 * Same as binder_get_txn_from() except it also acquires the proc->inner_lock
1934 * to guarantee that the thread cannot be released while operating on it.
1935 * The caller must call binder_inner_proc_unlock() to release the inner lock
1936 * as well as call binder_dec_thread_txn() to release the reference.
1937 *
1938 * Return: the value of t->from
1939 */
1940static struct binder_thread *binder_get_txn_from_and_acq_inner(
1941 struct binder_transaction *t)
1942{
1943 struct binder_thread *from;
1944
1945 from = binder_get_txn_from(t);
1946 if (!from)
1947 return NULL;
1948 binder_inner_proc_lock(from->proc);
1949 if (t->from) {
1950 BUG_ON(from != t->from);
1951 return from;
1952 }
1953 binder_inner_proc_unlock(from->proc);
1954 binder_thread_dec_tmpref(from);
1955 return NULL;
1956}
1957
Todd Kjosb6d282c2017-06-29 12:01:54 -07001958static void binder_free_transaction(struct binder_transaction *t)
1959{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001960 if (t->buffer)
1961 t->buffer->transaction = NULL;
1962 kfree(t);
1963 binder_stats_deleted(BINDER_STAT_TRANSACTION);
1964}
1965
1966static void binder_send_failed_reply(struct binder_transaction *t,
1967 uint32_t error_code)
1968{
1969 struct binder_thread *target_thread;
Lucas Tanured4ec15e2014-07-13 21:31:05 -03001970 struct binder_transaction *next;
Seunghun Lee10f62862014-05-01 01:30:23 +09001971
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001972 BUG_ON(t->flags & TF_ONE_WAY);
1973 while (1) {
Martijn Coenen0b89d692017-06-29 12:02:06 -07001974 target_thread = binder_get_txn_from_and_acq_inner(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001975 if (target_thread) {
Todd Kjos26549d12017-06-29 12:01:55 -07001976 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1977 "send failed reply for transaction %d to %d:%d\n",
1978 t->debug_id,
1979 target_thread->proc->pid,
1980 target_thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001981
Martijn Coenen0b89d692017-06-29 12:02:06 -07001982 binder_pop_transaction_ilocked(target_thread, t);
Todd Kjos26549d12017-06-29 12:01:55 -07001983 if (target_thread->reply_error.cmd == BR_OK) {
1984 target_thread->reply_error.cmd = error_code;
Martijn Coenen148ade22017-11-15 09:21:35 +01001985 binder_enqueue_thread_work_ilocked(
1986 target_thread,
1987 &target_thread->reply_error.work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001988 wake_up_interruptible(&target_thread->wait);
1989 } else {
Todd Kjose46a3b32018-02-07 12:38:47 -08001990 /*
1991 * Cannot get here for normal operation, but
1992 * we can if multiple synchronous transactions
1993 * are sent without blocking for responses.
1994 * Just ignore the 2nd error in this case.
1995 */
1996 pr_warn("Unexpected reply error: %u\n",
1997 target_thread->reply_error.cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001998 }
Martijn Coenen0b89d692017-06-29 12:02:06 -07001999 binder_inner_proc_unlock(target_thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07002000 binder_thread_dec_tmpref(target_thread);
Todd Kjos26549d12017-06-29 12:01:55 -07002001 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002002 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002003 }
Lucas Tanured4ec15e2014-07-13 21:31:05 -03002004 next = t->from_parent;
2005
2006 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2007 "send failed reply for transaction %d, target dead\n",
2008 t->debug_id);
2009
Todd Kjosb6d282c2017-06-29 12:01:54 -07002010 binder_free_transaction(t);
Lucas Tanured4ec15e2014-07-13 21:31:05 -03002011 if (next == NULL) {
2012 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2013 "reply failed, no target thread at root\n");
2014 return;
2015 }
2016 t = next;
2017 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2018 "reply failed, no target thread -- retry %d\n",
2019 t->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002020 }
2021}
2022
Martijn Coenenfeba3902017-02-03 14:40:45 -08002023/**
Martijn Coenenfb2c4452017-11-13 10:06:08 +01002024 * binder_cleanup_transaction() - cleans up undelivered transaction
2025 * @t: transaction that needs to be cleaned up
2026 * @reason: reason the transaction wasn't delivered
2027 * @error_code: error to return to caller (if synchronous call)
2028 */
2029static void binder_cleanup_transaction(struct binder_transaction *t,
2030 const char *reason,
2031 uint32_t error_code)
2032{
2033 if (t->buffer->target_node && !(t->flags & TF_ONE_WAY)) {
2034 binder_send_failed_reply(t, error_code);
2035 } else {
2036 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2037 "undelivered transaction %d, %s\n",
2038 t->debug_id, reason);
2039 binder_free_transaction(t);
2040 }
2041}
2042
2043/**
Martijn Coenenfeba3902017-02-03 14:40:45 -08002044 * binder_validate_object() - checks for a valid metadata object in a buffer.
2045 * @buffer: binder_buffer that we're parsing.
2046 * @offset: offset in the buffer at which to validate an object.
2047 *
2048 * Return: If there's a valid metadata object at @offset in @buffer, the
2049 * size of that object. Otherwise, it returns zero.
2050 */
2051static size_t binder_validate_object(struct binder_buffer *buffer, u64 offset)
2052{
2053 /* Check if we can read a header first */
2054 struct binder_object_header *hdr;
2055 size_t object_size = 0;
2056
Dan Carpenter361f2dd2018-03-29 12:14:40 +03002057 if (buffer->data_size < sizeof(*hdr) ||
2058 offset > buffer->data_size - sizeof(*hdr) ||
Martijn Coenenfeba3902017-02-03 14:40:45 -08002059 !IS_ALIGNED(offset, sizeof(u32)))
2060 return 0;
2061
2062 /* Ok, now see if we can read a complete object. */
2063 hdr = (struct binder_object_header *)(buffer->data + offset);
2064 switch (hdr->type) {
2065 case BINDER_TYPE_BINDER:
2066 case BINDER_TYPE_WEAK_BINDER:
2067 case BINDER_TYPE_HANDLE:
2068 case BINDER_TYPE_WEAK_HANDLE:
2069 object_size = sizeof(struct flat_binder_object);
2070 break;
2071 case BINDER_TYPE_FD:
2072 object_size = sizeof(struct binder_fd_object);
2073 break;
Martijn Coenen79802402017-02-03 14:40:51 -08002074 case BINDER_TYPE_PTR:
2075 object_size = sizeof(struct binder_buffer_object);
2076 break;
Martijn Coenendef95c72017-02-03 14:40:52 -08002077 case BINDER_TYPE_FDA:
2078 object_size = sizeof(struct binder_fd_array_object);
2079 break;
Martijn Coenenfeba3902017-02-03 14:40:45 -08002080 default:
2081 return 0;
2082 }
2083 if (offset <= buffer->data_size - object_size &&
2084 buffer->data_size >= object_size)
2085 return object_size;
2086 else
2087 return 0;
2088}
2089
Martijn Coenen79802402017-02-03 14:40:51 -08002090/**
2091 * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
2092 * @b: binder_buffer containing the object
2093 * @index: index in offset array at which the binder_buffer_object is
2094 * located
2095 * @start: points to the start of the offset array
2096 * @num_valid: the number of valid offsets in the offset array
2097 *
2098 * Return: If @index is within the valid range of the offset array
2099 * described by @start and @num_valid, and if there's a valid
2100 * binder_buffer_object at the offset found in index @index
2101 * of the offset array, that object is returned. Otherwise,
2102 * %NULL is returned.
2103 * Note that the offset found in index @index itself is not
2104 * verified; this function assumes that @num_valid elements
2105 * from @start were previously verified to have valid offsets.
2106 */
2107static struct binder_buffer_object *binder_validate_ptr(struct binder_buffer *b,
2108 binder_size_t index,
2109 binder_size_t *start,
2110 binder_size_t num_valid)
2111{
2112 struct binder_buffer_object *buffer_obj;
2113 binder_size_t *offp;
2114
2115 if (index >= num_valid)
2116 return NULL;
2117
2118 offp = start + index;
2119 buffer_obj = (struct binder_buffer_object *)(b->data + *offp);
2120 if (buffer_obj->hdr.type != BINDER_TYPE_PTR)
2121 return NULL;
2122
2123 return buffer_obj;
2124}
2125
2126/**
2127 * binder_validate_fixup() - validates pointer/fd fixups happen in order.
2128 * @b: transaction buffer
2129 * @objects_start start of objects buffer
2130 * @buffer: binder_buffer_object in which to fix up
2131 * @offset: start offset in @buffer to fix up
2132 * @last_obj: last binder_buffer_object that we fixed up in
2133 * @last_min_offset: minimum fixup offset in @last_obj
2134 *
2135 * Return: %true if a fixup in buffer @buffer at offset @offset is
2136 * allowed.
2137 *
2138 * For safety reasons, we only allow fixups inside a buffer to happen
2139 * at increasing offsets; additionally, we only allow fixup on the last
2140 * buffer object that was verified, or one of its parents.
2141 *
2142 * Example of what is allowed:
2143 *
2144 * A
2145 * B (parent = A, offset = 0)
2146 * C (parent = A, offset = 16)
2147 * D (parent = C, offset = 0)
2148 * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
2149 *
2150 * Examples of what is not allowed:
2151 *
2152 * Decreasing offsets within the same parent:
2153 * A
2154 * C (parent = A, offset = 16)
2155 * B (parent = A, offset = 0) // decreasing offset within A
2156 *
2157 * Referring to a parent that wasn't the last object or any of its parents:
2158 * A
2159 * B (parent = A, offset = 0)
2160 * C (parent = A, offset = 0)
2161 * C (parent = A, offset = 16)
2162 * D (parent = B, offset = 0) // B is not A or any of A's parents
2163 */
2164static bool binder_validate_fixup(struct binder_buffer *b,
2165 binder_size_t *objects_start,
2166 struct binder_buffer_object *buffer,
2167 binder_size_t fixup_offset,
2168 struct binder_buffer_object *last_obj,
2169 binder_size_t last_min_offset)
2170{
2171 if (!last_obj) {
2172 /* Nothing to fix up in */
2173 return false;
2174 }
2175
2176 while (last_obj != buffer) {
2177 /*
2178 * Safe to retrieve the parent of last_obj, since it
2179 * was already previously verified by the driver.
2180 */
2181 if ((last_obj->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0)
2182 return false;
2183 last_min_offset = last_obj->parent_offset + sizeof(uintptr_t);
2184 last_obj = (struct binder_buffer_object *)
2185 (b->data + *(objects_start + last_obj->parent));
2186 }
2187 return (fixup_offset >= last_min_offset);
2188}
2189
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002190static void binder_transaction_buffer_release(struct binder_proc *proc,
2191 struct binder_buffer *buffer,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002192 binder_size_t *failed_at)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002193{
Martijn Coenen79802402017-02-03 14:40:51 -08002194 binder_size_t *offp, *off_start, *off_end;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002195 int debug_id = buffer->debug_id;
2196
2197 binder_debug(BINDER_DEBUG_TRANSACTION,
Todd Kjos8ca86f12018-02-07 13:57:37 -08002198 "%d buffer release %d, size %zd-%zd, failed at %pK\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002199 proc->pid, buffer->debug_id,
2200 buffer->data_size, buffer->offsets_size, failed_at);
2201
2202 if (buffer->target_node)
2203 binder_dec_node(buffer->target_node, 1, 0);
2204
Martijn Coenen79802402017-02-03 14:40:51 -08002205 off_start = (binder_size_t *)(buffer->data +
2206 ALIGN(buffer->data_size, sizeof(void *)));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002207 if (failed_at)
2208 off_end = failed_at;
2209 else
Martijn Coenen79802402017-02-03 14:40:51 -08002210 off_end = (void *)off_start + buffer->offsets_size;
2211 for (offp = off_start; offp < off_end; offp++) {
Martijn Coenenfeba3902017-02-03 14:40:45 -08002212 struct binder_object_header *hdr;
2213 size_t object_size = binder_validate_object(buffer, *offp);
Seunghun Lee10f62862014-05-01 01:30:23 +09002214
Martijn Coenenfeba3902017-02-03 14:40:45 -08002215 if (object_size == 0) {
2216 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
Arve Hjønnevågda498892014-02-21 14:40:26 -08002217 debug_id, (u64)*offp, buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002218 continue;
2219 }
Martijn Coenenfeba3902017-02-03 14:40:45 -08002220 hdr = (struct binder_object_header *)(buffer->data + *offp);
2221 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002222 case BINDER_TYPE_BINDER:
2223 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08002224 struct flat_binder_object *fp;
2225 struct binder_node *node;
Seunghun Lee10f62862014-05-01 01:30:23 +09002226
Martijn Coenenfeba3902017-02-03 14:40:45 -08002227 fp = to_flat_binder_object(hdr);
2228 node = binder_get_node(proc, fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002229 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002230 pr_err("transaction release %d bad node %016llx\n",
2231 debug_id, (u64)fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002232 break;
2233 }
2234 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002235 " node %d u%016llx\n",
2236 node->debug_id, (u64)node->ptr);
Martijn Coenenfeba3902017-02-03 14:40:45 -08002237 binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER,
2238 0);
Todd Kjosadc18842017-06-29 12:01:59 -07002239 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002240 } break;
2241 case BINDER_TYPE_HANDLE:
2242 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08002243 struct flat_binder_object *fp;
Todd Kjos372e3142017-06-29 12:01:58 -07002244 struct binder_ref_data rdata;
2245 int ret;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02002246
Martijn Coenenfeba3902017-02-03 14:40:45 -08002247 fp = to_flat_binder_object(hdr);
Todd Kjos372e3142017-06-29 12:01:58 -07002248 ret = binder_dec_ref_for_handle(proc, fp->handle,
2249 hdr->type == BINDER_TYPE_HANDLE, &rdata);
2250
2251 if (ret) {
2252 pr_err("transaction release %d bad handle %d, ret = %d\n",
2253 debug_id, fp->handle, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002254 break;
2255 }
2256 binder_debug(BINDER_DEBUG_TRANSACTION,
Todd Kjos372e3142017-06-29 12:01:58 -07002257 " ref %d desc %d\n",
2258 rdata.debug_id, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002259 } break;
2260
Martijn Coenenfeba3902017-02-03 14:40:45 -08002261 case BINDER_TYPE_FD: {
2262 struct binder_fd_object *fp = to_binder_fd_object(hdr);
2263
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002264 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenenfeba3902017-02-03 14:40:45 -08002265 " fd %d\n", fp->fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002266 if (failed_at)
Martijn Coenenfeba3902017-02-03 14:40:45 -08002267 task_close_fd(proc, fp->fd);
2268 } break;
Martijn Coenen79802402017-02-03 14:40:51 -08002269 case BINDER_TYPE_PTR:
2270 /*
2271 * Nothing to do here, this will get cleaned up when the
2272 * transaction buffer gets freed
2273 */
2274 break;
Martijn Coenendef95c72017-02-03 14:40:52 -08002275 case BINDER_TYPE_FDA: {
2276 struct binder_fd_array_object *fda;
2277 struct binder_buffer_object *parent;
2278 uintptr_t parent_buffer;
2279 u32 *fd_array;
2280 size_t fd_index;
2281 binder_size_t fd_buf_size;
2282
2283 fda = to_binder_fd_array_object(hdr);
2284 parent = binder_validate_ptr(buffer, fda->parent,
2285 off_start,
2286 offp - off_start);
2287 if (!parent) {
Arvind Yadavf7f84fd2017-09-25 12:52:11 +05302288 pr_err("transaction release %d bad parent offset\n",
Martijn Coenendef95c72017-02-03 14:40:52 -08002289 debug_id);
2290 continue;
2291 }
2292 /*
2293 * Since the parent was already fixed up, convert it
2294 * back to kernel address space to access it
2295 */
2296 parent_buffer = parent->buffer -
Todd Kjos19c98722017-06-29 12:01:40 -07002297 binder_alloc_get_user_buffer_offset(
2298 &proc->alloc);
Martijn Coenendef95c72017-02-03 14:40:52 -08002299
2300 fd_buf_size = sizeof(u32) * fda->num_fds;
2301 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2302 pr_err("transaction release %d invalid number of fds (%lld)\n",
2303 debug_id, (u64)fda->num_fds);
2304 continue;
2305 }
2306 if (fd_buf_size > parent->length ||
2307 fda->parent_offset > parent->length - fd_buf_size) {
2308 /* No space for all file descriptors here. */
2309 pr_err("transaction release %d not enough space for %lld fds in buffer\n",
2310 debug_id, (u64)fda->num_fds);
2311 continue;
2312 }
Arnd Bergmann1c363ea2017-09-05 10:56:13 +02002313 fd_array = (u32 *)(parent_buffer + (uintptr_t)fda->parent_offset);
Martijn Coenendef95c72017-02-03 14:40:52 -08002314 for (fd_index = 0; fd_index < fda->num_fds; fd_index++)
2315 task_close_fd(proc, fd_array[fd_index]);
2316 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002317 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01002318 pr_err("transaction release %d bad object type %x\n",
Martijn Coenenfeba3902017-02-03 14:40:45 -08002319 debug_id, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002320 break;
2321 }
2322 }
2323}
2324
Martijn Coenena056af42017-02-03 14:40:49 -08002325static int binder_translate_binder(struct flat_binder_object *fp,
2326 struct binder_transaction *t,
2327 struct binder_thread *thread)
2328{
2329 struct binder_node *node;
Martijn Coenena056af42017-02-03 14:40:49 -08002330 struct binder_proc *proc = thread->proc;
2331 struct binder_proc *target_proc = t->to_proc;
Todd Kjos372e3142017-06-29 12:01:58 -07002332 struct binder_ref_data rdata;
Todd Kjosadc18842017-06-29 12:01:59 -07002333 int ret = 0;
Martijn Coenena056af42017-02-03 14:40:49 -08002334
2335 node = binder_get_node(proc, fp->binder);
2336 if (!node) {
Todd Kjos673068e2017-06-29 12:02:03 -07002337 node = binder_new_node(proc, fp);
Martijn Coenena056af42017-02-03 14:40:49 -08002338 if (!node)
2339 return -ENOMEM;
Martijn Coenena056af42017-02-03 14:40:49 -08002340 }
2341 if (fp->cookie != node->cookie) {
2342 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
2343 proc->pid, thread->pid, (u64)fp->binder,
2344 node->debug_id, (u64)fp->cookie,
2345 (u64)node->cookie);
Todd Kjosadc18842017-06-29 12:01:59 -07002346 ret = -EINVAL;
2347 goto done;
Martijn Coenena056af42017-02-03 14:40:49 -08002348 }
Todd Kjosadc18842017-06-29 12:01:59 -07002349 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2350 ret = -EPERM;
2351 goto done;
2352 }
Martijn Coenena056af42017-02-03 14:40:49 -08002353
Todd Kjos372e3142017-06-29 12:01:58 -07002354 ret = binder_inc_ref_for_node(target_proc, node,
2355 fp->hdr.type == BINDER_TYPE_BINDER,
2356 &thread->todo, &rdata);
2357 if (ret)
Todd Kjosadc18842017-06-29 12:01:59 -07002358 goto done;
Martijn Coenena056af42017-02-03 14:40:49 -08002359
2360 if (fp->hdr.type == BINDER_TYPE_BINDER)
2361 fp->hdr.type = BINDER_TYPE_HANDLE;
2362 else
2363 fp->hdr.type = BINDER_TYPE_WEAK_HANDLE;
2364 fp->binder = 0;
Todd Kjos372e3142017-06-29 12:01:58 -07002365 fp->handle = rdata.desc;
Martijn Coenena056af42017-02-03 14:40:49 -08002366 fp->cookie = 0;
Martijn Coenena056af42017-02-03 14:40:49 -08002367
Todd Kjos372e3142017-06-29 12:01:58 -07002368 trace_binder_transaction_node_to_ref(t, node, &rdata);
Martijn Coenena056af42017-02-03 14:40:49 -08002369 binder_debug(BINDER_DEBUG_TRANSACTION,
2370 " node %d u%016llx -> ref %d desc %d\n",
2371 node->debug_id, (u64)node->ptr,
Todd Kjos372e3142017-06-29 12:01:58 -07002372 rdata.debug_id, rdata.desc);
Todd Kjosadc18842017-06-29 12:01:59 -07002373done:
2374 binder_put_node(node);
2375 return ret;
Martijn Coenena056af42017-02-03 14:40:49 -08002376}
2377
2378static int binder_translate_handle(struct flat_binder_object *fp,
2379 struct binder_transaction *t,
2380 struct binder_thread *thread)
2381{
Martijn Coenena056af42017-02-03 14:40:49 -08002382 struct binder_proc *proc = thread->proc;
2383 struct binder_proc *target_proc = t->to_proc;
Todd Kjos372e3142017-06-29 12:01:58 -07002384 struct binder_node *node;
2385 struct binder_ref_data src_rdata;
Todd Kjosadc18842017-06-29 12:01:59 -07002386 int ret = 0;
Martijn Coenena056af42017-02-03 14:40:49 -08002387
Todd Kjos372e3142017-06-29 12:01:58 -07002388 node = binder_get_node_from_ref(proc, fp->handle,
2389 fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata);
2390 if (!node) {
Martijn Coenena056af42017-02-03 14:40:49 -08002391 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
2392 proc->pid, thread->pid, fp->handle);
2393 return -EINVAL;
2394 }
Todd Kjosadc18842017-06-29 12:01:59 -07002395 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2396 ret = -EPERM;
2397 goto done;
2398 }
Martijn Coenena056af42017-02-03 14:40:49 -08002399
Todd Kjos673068e2017-06-29 12:02:03 -07002400 binder_node_lock(node);
Todd Kjos372e3142017-06-29 12:01:58 -07002401 if (node->proc == target_proc) {
Martijn Coenena056af42017-02-03 14:40:49 -08002402 if (fp->hdr.type == BINDER_TYPE_HANDLE)
2403 fp->hdr.type = BINDER_TYPE_BINDER;
2404 else
2405 fp->hdr.type = BINDER_TYPE_WEAK_BINDER;
Todd Kjos372e3142017-06-29 12:01:58 -07002406 fp->binder = node->ptr;
2407 fp->cookie = node->cookie;
Todd Kjos673068e2017-06-29 12:02:03 -07002408 if (node->proc)
2409 binder_inner_proc_lock(node->proc);
2410 binder_inc_node_nilocked(node,
2411 fp->hdr.type == BINDER_TYPE_BINDER,
2412 0, NULL);
2413 if (node->proc)
2414 binder_inner_proc_unlock(node->proc);
Todd Kjos372e3142017-06-29 12:01:58 -07002415 trace_binder_transaction_ref_to_node(t, node, &src_rdata);
Martijn Coenena056af42017-02-03 14:40:49 -08002416 binder_debug(BINDER_DEBUG_TRANSACTION,
2417 " ref %d desc %d -> node %d u%016llx\n",
Todd Kjos372e3142017-06-29 12:01:58 -07002418 src_rdata.debug_id, src_rdata.desc, node->debug_id,
2419 (u64)node->ptr);
Todd Kjos673068e2017-06-29 12:02:03 -07002420 binder_node_unlock(node);
Martijn Coenena056af42017-02-03 14:40:49 -08002421 } else {
Todd Kjos372e3142017-06-29 12:01:58 -07002422 struct binder_ref_data dest_rdata;
Martijn Coenena056af42017-02-03 14:40:49 -08002423
Todd Kjos673068e2017-06-29 12:02:03 -07002424 binder_node_unlock(node);
Todd Kjos372e3142017-06-29 12:01:58 -07002425 ret = binder_inc_ref_for_node(target_proc, node,
2426 fp->hdr.type == BINDER_TYPE_HANDLE,
2427 NULL, &dest_rdata);
2428 if (ret)
Todd Kjosadc18842017-06-29 12:01:59 -07002429 goto done;
Martijn Coenena056af42017-02-03 14:40:49 -08002430
2431 fp->binder = 0;
Todd Kjos372e3142017-06-29 12:01:58 -07002432 fp->handle = dest_rdata.desc;
Martijn Coenena056af42017-02-03 14:40:49 -08002433 fp->cookie = 0;
Todd Kjos372e3142017-06-29 12:01:58 -07002434 trace_binder_transaction_ref_to_ref(t, node, &src_rdata,
2435 &dest_rdata);
Martijn Coenena056af42017-02-03 14:40:49 -08002436 binder_debug(BINDER_DEBUG_TRANSACTION,
2437 " ref %d desc %d -> ref %d desc %d (node %d)\n",
Todd Kjos372e3142017-06-29 12:01:58 -07002438 src_rdata.debug_id, src_rdata.desc,
2439 dest_rdata.debug_id, dest_rdata.desc,
2440 node->debug_id);
Martijn Coenena056af42017-02-03 14:40:49 -08002441 }
Todd Kjosadc18842017-06-29 12:01:59 -07002442done:
2443 binder_put_node(node);
2444 return ret;
Martijn Coenena056af42017-02-03 14:40:49 -08002445}
2446
2447static int binder_translate_fd(int fd,
2448 struct binder_transaction *t,
2449 struct binder_thread *thread,
2450 struct binder_transaction *in_reply_to)
2451{
2452 struct binder_proc *proc = thread->proc;
2453 struct binder_proc *target_proc = t->to_proc;
2454 int target_fd;
2455 struct file *file;
2456 int ret;
2457 bool target_allows_fd;
2458
2459 if (in_reply_to)
2460 target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS);
2461 else
2462 target_allows_fd = t->buffer->target_node->accept_fds;
2463 if (!target_allows_fd) {
2464 binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
2465 proc->pid, thread->pid,
2466 in_reply_to ? "reply" : "transaction",
2467 fd);
2468 ret = -EPERM;
2469 goto err_fd_not_accepted;
2470 }
2471
2472 file = fget(fd);
2473 if (!file) {
2474 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
2475 proc->pid, thread->pid, fd);
2476 ret = -EBADF;
2477 goto err_fget;
2478 }
2479 ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file);
2480 if (ret < 0) {
2481 ret = -EPERM;
2482 goto err_security;
2483 }
2484
2485 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
2486 if (target_fd < 0) {
2487 ret = -ENOMEM;
2488 goto err_get_unused_fd;
2489 }
2490 task_fd_install(target_proc, target_fd, file);
2491 trace_binder_transaction_fd(t, fd, target_fd);
2492 binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n",
2493 fd, target_fd);
2494
2495 return target_fd;
2496
2497err_get_unused_fd:
2498err_security:
2499 fput(file);
2500err_fget:
2501err_fd_not_accepted:
2502 return ret;
2503}
2504
Martijn Coenendef95c72017-02-03 14:40:52 -08002505static int binder_translate_fd_array(struct binder_fd_array_object *fda,
2506 struct binder_buffer_object *parent,
2507 struct binder_transaction *t,
2508 struct binder_thread *thread,
2509 struct binder_transaction *in_reply_to)
2510{
2511 binder_size_t fdi, fd_buf_size, num_installed_fds;
2512 int target_fd;
2513 uintptr_t parent_buffer;
2514 u32 *fd_array;
2515 struct binder_proc *proc = thread->proc;
2516 struct binder_proc *target_proc = t->to_proc;
2517
2518 fd_buf_size = sizeof(u32) * fda->num_fds;
2519 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2520 binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
2521 proc->pid, thread->pid, (u64)fda->num_fds);
2522 return -EINVAL;
2523 }
2524 if (fd_buf_size > parent->length ||
2525 fda->parent_offset > parent->length - fd_buf_size) {
2526 /* No space for all file descriptors here. */
2527 binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
2528 proc->pid, thread->pid, (u64)fda->num_fds);
2529 return -EINVAL;
2530 }
2531 /*
2532 * Since the parent was already fixed up, convert it
2533 * back to the kernel address space to access it
2534 */
Todd Kjos19c98722017-06-29 12:01:40 -07002535 parent_buffer = parent->buffer -
2536 binder_alloc_get_user_buffer_offset(&target_proc->alloc);
Arnd Bergmann1c363ea2017-09-05 10:56:13 +02002537 fd_array = (u32 *)(parent_buffer + (uintptr_t)fda->parent_offset);
Martijn Coenendef95c72017-02-03 14:40:52 -08002538 if (!IS_ALIGNED((unsigned long)fd_array, sizeof(u32))) {
2539 binder_user_error("%d:%d parent offset not aligned correctly.\n",
2540 proc->pid, thread->pid);
2541 return -EINVAL;
2542 }
2543 for (fdi = 0; fdi < fda->num_fds; fdi++) {
2544 target_fd = binder_translate_fd(fd_array[fdi], t, thread,
2545 in_reply_to);
2546 if (target_fd < 0)
2547 goto err_translate_fd_failed;
2548 fd_array[fdi] = target_fd;
2549 }
2550 return 0;
2551
2552err_translate_fd_failed:
2553 /*
2554 * Failed to allocate fd or security error, free fds
2555 * installed so far.
2556 */
2557 num_installed_fds = fdi;
2558 for (fdi = 0; fdi < num_installed_fds; fdi++)
2559 task_close_fd(target_proc, fd_array[fdi]);
2560 return target_fd;
2561}
2562
Martijn Coenen79802402017-02-03 14:40:51 -08002563static int binder_fixup_parent(struct binder_transaction *t,
2564 struct binder_thread *thread,
2565 struct binder_buffer_object *bp,
2566 binder_size_t *off_start,
2567 binder_size_t num_valid,
2568 struct binder_buffer_object *last_fixup_obj,
2569 binder_size_t last_fixup_min_off)
2570{
2571 struct binder_buffer_object *parent;
2572 u8 *parent_buffer;
2573 struct binder_buffer *b = t->buffer;
2574 struct binder_proc *proc = thread->proc;
2575 struct binder_proc *target_proc = t->to_proc;
2576
2577 if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT))
2578 return 0;
2579
2580 parent = binder_validate_ptr(b, bp->parent, off_start, num_valid);
2581 if (!parent) {
2582 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
2583 proc->pid, thread->pid);
2584 return -EINVAL;
2585 }
2586
2587 if (!binder_validate_fixup(b, off_start,
2588 parent, bp->parent_offset,
2589 last_fixup_obj,
2590 last_fixup_min_off)) {
2591 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
2592 proc->pid, thread->pid);
2593 return -EINVAL;
2594 }
2595
2596 if (parent->length < sizeof(binder_uintptr_t) ||
2597 bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) {
2598 /* No space for a pointer here! */
2599 binder_user_error("%d:%d got transaction with invalid parent offset\n",
2600 proc->pid, thread->pid);
2601 return -EINVAL;
2602 }
Arnd Bergmann1c363ea2017-09-05 10:56:13 +02002603 parent_buffer = (u8 *)((uintptr_t)parent->buffer -
Todd Kjos19c98722017-06-29 12:01:40 -07002604 binder_alloc_get_user_buffer_offset(
2605 &target_proc->alloc));
Martijn Coenen79802402017-02-03 14:40:51 -08002606 *(binder_uintptr_t *)(parent_buffer + bp->parent_offset) = bp->buffer;
2607
2608 return 0;
2609}
2610
Martijn Coenen408c68b2017-08-31 10:04:19 +02002611/**
2612 * binder_proc_transaction() - sends a transaction to a process and wakes it up
2613 * @t: transaction to send
2614 * @proc: process to send the transaction to
2615 * @thread: thread in @proc to send the transaction to (may be NULL)
2616 *
2617 * This function queues a transaction to the specified process. It will try
2618 * to find a thread in the target process to handle the transaction and
2619 * wake it up. If no thread is found, the work is queued to the proc
2620 * waitqueue.
2621 *
2622 * If the @thread parameter is not NULL, the transaction is always queued
2623 * to the waitlist of that specific thread.
2624 *
2625 * Return: true if the transactions was successfully queued
2626 * false if the target process or thread is dead
2627 */
2628static bool binder_proc_transaction(struct binder_transaction *t,
2629 struct binder_proc *proc,
2630 struct binder_thread *thread)
2631{
Martijn Coenen408c68b2017-08-31 10:04:19 +02002632 struct binder_node *node = t->buffer->target_node;
2633 bool oneway = !!(t->flags & TF_ONE_WAY);
Martijn Coenen148ade22017-11-15 09:21:35 +01002634 bool pending_async = false;
Martijn Coenen408c68b2017-08-31 10:04:19 +02002635
2636 BUG_ON(!node);
2637 binder_node_lock(node);
2638 if (oneway) {
2639 BUG_ON(thread);
2640 if (node->has_async_transaction) {
Martijn Coenen148ade22017-11-15 09:21:35 +01002641 pending_async = true;
Martijn Coenen408c68b2017-08-31 10:04:19 +02002642 } else {
Gustavo A. R. Silva197410a2018-01-23 12:04:27 -06002643 node->has_async_transaction = true;
Martijn Coenen408c68b2017-08-31 10:04:19 +02002644 }
2645 }
2646
2647 binder_inner_proc_lock(proc);
2648
2649 if (proc->is_dead || (thread && thread->is_dead)) {
2650 binder_inner_proc_unlock(proc);
2651 binder_node_unlock(node);
2652 return false;
2653 }
2654
Martijn Coenen148ade22017-11-15 09:21:35 +01002655 if (!thread && !pending_async)
Martijn Coenen408c68b2017-08-31 10:04:19 +02002656 thread = binder_select_thread_ilocked(proc);
2657
2658 if (thread)
Martijn Coenen148ade22017-11-15 09:21:35 +01002659 binder_enqueue_thread_work_ilocked(thread, &t->work);
2660 else if (!pending_async)
2661 binder_enqueue_work_ilocked(&t->work, &proc->todo);
Martijn Coenen408c68b2017-08-31 10:04:19 +02002662 else
Martijn Coenen148ade22017-11-15 09:21:35 +01002663 binder_enqueue_work_ilocked(&t->work, &node->async_todo);
Martijn Coenen408c68b2017-08-31 10:04:19 +02002664
Martijn Coenen148ade22017-11-15 09:21:35 +01002665 if (!pending_async)
Martijn Coenen408c68b2017-08-31 10:04:19 +02002666 binder_wakeup_thread_ilocked(proc, thread, !oneway /* sync */);
2667
2668 binder_inner_proc_unlock(proc);
2669 binder_node_unlock(node);
2670
2671 return true;
2672}
2673
Todd Kjos512cf462017-09-29 15:39:49 -07002674/**
2675 * binder_get_node_refs_for_txn() - Get required refs on node for txn
2676 * @node: struct binder_node for which to get refs
2677 * @proc: returns @node->proc if valid
2678 * @error: if no @proc then returns BR_DEAD_REPLY
2679 *
2680 * User-space normally keeps the node alive when creating a transaction
2681 * since it has a reference to the target. The local strong ref keeps it
2682 * alive if the sending process dies before the target process processes
2683 * the transaction. If the source process is malicious or has a reference
2684 * counting bug, relying on the local strong ref can fail.
2685 *
2686 * Since user-space can cause the local strong ref to go away, we also take
2687 * a tmpref on the node to ensure it survives while we are constructing
2688 * the transaction. We also need a tmpref on the proc while we are
2689 * constructing the transaction, so we take that here as well.
2690 *
2691 * Return: The target_node with refs taken or NULL if no @node->proc is NULL.
2692 * Also sets @proc if valid. If the @node->proc is NULL indicating that the
2693 * target proc has died, @error is set to BR_DEAD_REPLY
2694 */
2695static struct binder_node *binder_get_node_refs_for_txn(
2696 struct binder_node *node,
2697 struct binder_proc **procp,
2698 uint32_t *error)
2699{
2700 struct binder_node *target_node = NULL;
2701
2702 binder_node_inner_lock(node);
2703 if (node->proc) {
2704 target_node = node;
2705 binder_inc_node_nilocked(node, 1, 0, NULL);
2706 binder_inc_node_tmpref_ilocked(node);
2707 node->proc->tmp_ref++;
2708 *procp = node->proc;
2709 } else
2710 *error = BR_DEAD_REPLY;
2711 binder_node_inner_unlock(node);
2712
2713 return target_node;
2714}
2715
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002716static void binder_transaction(struct binder_proc *proc,
2717 struct binder_thread *thread,
Martijn Coenen4bfac802017-02-03 14:40:50 -08002718 struct binder_transaction_data *tr, int reply,
2719 binder_size_t extra_buffers_size)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002720{
Martijn Coenena056af42017-02-03 14:40:49 -08002721 int ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002722 struct binder_transaction *t;
2723 struct binder_work *tcomplete;
Martijn Coenen79802402017-02-03 14:40:51 -08002724 binder_size_t *offp, *off_end, *off_start;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08002725 binder_size_t off_min;
Martijn Coenen79802402017-02-03 14:40:51 -08002726 u8 *sg_bufp, *sg_buf_end;
Todd Kjos7a4408c2017-06-29 12:01:57 -07002727 struct binder_proc *target_proc = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002728 struct binder_thread *target_thread = NULL;
2729 struct binder_node *target_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002730 struct binder_transaction *in_reply_to = NULL;
2731 struct binder_transaction_log_entry *e;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002732 uint32_t return_error = 0;
2733 uint32_t return_error_param = 0;
2734 uint32_t return_error_line = 0;
Martijn Coenen79802402017-02-03 14:40:51 -08002735 struct binder_buffer_object *last_fixup_obj = NULL;
2736 binder_size_t last_fixup_min_off = 0;
Martijn Coenen342e5c92017-02-03 14:40:46 -08002737 struct binder_context *context = proc->context;
Todd Kjosd99c7332017-06-29 12:01:53 -07002738 int t_debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002739
2740 e = binder_transaction_log_add(&binder_transaction_log);
Todd Kjosd99c7332017-06-29 12:01:53 -07002741 e->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002742 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
2743 e->from_proc = proc->pid;
2744 e->from_thread = thread->pid;
2745 e->target_handle = tr->target.handle;
2746 e->data_size = tr->data_size;
2747 e->offsets_size = tr->offsets_size;
Martijn Coenen14db3182017-02-03 14:40:47 -08002748 e->context_name = proc->context->name;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002749
2750 if (reply) {
Martijn Coenen0b89d692017-06-29 12:02:06 -07002751 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002752 in_reply_to = thread->transaction_stack;
2753 if (in_reply_to == NULL) {
Martijn Coenen0b89d692017-06-29 12:02:06 -07002754 binder_inner_proc_unlock(proc);
Anmol Sarma56b468f2012-10-30 22:35:43 +05302755 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002756 proc->pid, thread->pid);
2757 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002758 return_error_param = -EPROTO;
2759 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002760 goto err_empty_call_stack;
2761 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002762 if (in_reply_to->to_thread != thread) {
Todd Kjos7a4408c2017-06-29 12:01:57 -07002763 spin_lock(&in_reply_to->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05302764 binder_user_error("%d:%d got reply transaction with bad transaction stack, transaction %d has target %d:%d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002765 proc->pid, thread->pid, in_reply_to->debug_id,
2766 in_reply_to->to_proc ?
2767 in_reply_to->to_proc->pid : 0,
2768 in_reply_to->to_thread ?
2769 in_reply_to->to_thread->pid : 0);
Todd Kjos7a4408c2017-06-29 12:01:57 -07002770 spin_unlock(&in_reply_to->lock);
Martijn Coenen0b89d692017-06-29 12:02:06 -07002771 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002772 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002773 return_error_param = -EPROTO;
2774 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002775 in_reply_to = NULL;
2776 goto err_bad_call_stack;
2777 }
2778 thread->transaction_stack = in_reply_to->to_parent;
Martijn Coenen0b89d692017-06-29 12:02:06 -07002779 binder_inner_proc_unlock(proc);
2780 binder_set_nice(in_reply_to->saved_priority);
2781 target_thread = binder_get_txn_from_and_acq_inner(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002782 if (target_thread == NULL) {
2783 return_error = BR_DEAD_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002784 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002785 goto err_dead_binder;
2786 }
2787 if (target_thread->transaction_stack != in_reply_to) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302788 binder_user_error("%d:%d got reply transaction with bad target transaction stack %d, expected %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002789 proc->pid, thread->pid,
2790 target_thread->transaction_stack ?
2791 target_thread->transaction_stack->debug_id : 0,
2792 in_reply_to->debug_id);
Martijn Coenen0b89d692017-06-29 12:02:06 -07002793 binder_inner_proc_unlock(target_thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002794 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002795 return_error_param = -EPROTO;
2796 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002797 in_reply_to = NULL;
2798 target_thread = NULL;
2799 goto err_dead_binder;
2800 }
2801 target_proc = target_thread->proc;
Todd Kjos7a4408c2017-06-29 12:01:57 -07002802 target_proc->tmp_ref++;
Martijn Coenen0b89d692017-06-29 12:02:06 -07002803 binder_inner_proc_unlock(target_thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002804 } else {
2805 if (tr->target.handle) {
2806 struct binder_ref *ref;
Seunghun Lee10f62862014-05-01 01:30:23 +09002807
Todd Kjoseb349832017-06-29 12:01:56 -07002808 /*
2809 * There must already be a strong ref
2810 * on this node. If so, do a strong
2811 * increment on the node to ensure it
2812 * stays alive until the transaction is
2813 * done.
2814 */
Todd Kjos2c1838d2017-06-29 12:02:08 -07002815 binder_proc_lock(proc);
2816 ref = binder_get_ref_olocked(proc, tr->target.handle,
2817 true);
Todd Kjoseb349832017-06-29 12:01:56 -07002818 if (ref) {
Todd Kjos512cf462017-09-29 15:39:49 -07002819 target_node = binder_get_node_refs_for_txn(
2820 ref->node, &target_proc,
2821 &return_error);
2822 } else {
2823 binder_user_error("%d:%d got transaction to invalid handle\n",
2824 proc->pid, thread->pid);
2825 return_error = BR_FAILED_REPLY;
Todd Kjoseb349832017-06-29 12:01:56 -07002826 }
Todd Kjos2c1838d2017-06-29 12:02:08 -07002827 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002828 } else {
Todd Kjosc44b1232017-06-29 12:01:43 -07002829 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen342e5c92017-02-03 14:40:46 -08002830 target_node = context->binder_context_mgr_node;
Todd Kjos512cf462017-09-29 15:39:49 -07002831 if (target_node)
2832 target_node = binder_get_node_refs_for_txn(
2833 target_node, &target_proc,
2834 &return_error);
2835 else
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002836 return_error = BR_DEAD_REPLY;
Todd Kjosc44b1232017-06-29 12:01:43 -07002837 mutex_unlock(&context->context_mgr_node_lock);
Martijn Coenen7aa135f2018-03-28 11:14:50 +02002838 if (target_node && target_proc == proc) {
2839 binder_user_error("%d:%d got transaction to context manager from process owning it\n",
2840 proc->pid, thread->pid);
2841 return_error = BR_FAILED_REPLY;
2842 return_error_param = -EINVAL;
2843 return_error_line = __LINE__;
2844 goto err_invalid_target_handle;
2845 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002846 }
Todd Kjos512cf462017-09-29 15:39:49 -07002847 if (!target_node) {
2848 /*
2849 * return_error is set above
2850 */
2851 return_error_param = -EINVAL;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002852 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002853 goto err_dead_binder;
2854 }
Todd Kjos512cf462017-09-29 15:39:49 -07002855 e->to_node = target_node->debug_id;
Stephen Smalley79af7302015-01-21 10:54:10 -05002856 if (security_binder_transaction(proc->tsk,
2857 target_proc->tsk) < 0) {
2858 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002859 return_error_param = -EPERM;
2860 return_error_line = __LINE__;
Stephen Smalley79af7302015-01-21 10:54:10 -05002861 goto err_invalid_target_handle;
2862 }
Martijn Coenen0b89d692017-06-29 12:02:06 -07002863 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002864 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
2865 struct binder_transaction *tmp;
Seunghun Lee10f62862014-05-01 01:30:23 +09002866
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002867 tmp = thread->transaction_stack;
2868 if (tmp->to_thread != thread) {
Todd Kjos7a4408c2017-06-29 12:01:57 -07002869 spin_lock(&tmp->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05302870 binder_user_error("%d:%d got new transaction with bad transaction stack, transaction %d has target %d:%d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002871 proc->pid, thread->pid, tmp->debug_id,
2872 tmp->to_proc ? tmp->to_proc->pid : 0,
2873 tmp->to_thread ?
2874 tmp->to_thread->pid : 0);
Todd Kjos7a4408c2017-06-29 12:01:57 -07002875 spin_unlock(&tmp->lock);
Martijn Coenen0b89d692017-06-29 12:02:06 -07002876 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002877 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002878 return_error_param = -EPROTO;
2879 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002880 goto err_bad_call_stack;
2881 }
2882 while (tmp) {
Todd Kjos7a4408c2017-06-29 12:01:57 -07002883 struct binder_thread *from;
2884
2885 spin_lock(&tmp->lock);
2886 from = tmp->from;
2887 if (from && from->proc == target_proc) {
2888 atomic_inc(&from->tmp_ref);
2889 target_thread = from;
2890 spin_unlock(&tmp->lock);
2891 break;
2892 }
2893 spin_unlock(&tmp->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002894 tmp = tmp->from_parent;
2895 }
2896 }
Martijn Coenen0b89d692017-06-29 12:02:06 -07002897 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002898 }
Martijn Coenen408c68b2017-08-31 10:04:19 +02002899 if (target_thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002900 e->to_thread = target_thread->pid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002901 e->to_proc = target_proc->pid;
2902
2903 /* TODO: reuse incoming transaction for reply */
2904 t = kzalloc(sizeof(*t), GFP_KERNEL);
2905 if (t == NULL) {
2906 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002907 return_error_param = -ENOMEM;
2908 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002909 goto err_alloc_t_failed;
2910 }
2911 binder_stats_created(BINDER_STAT_TRANSACTION);
Todd Kjos7a4408c2017-06-29 12:01:57 -07002912 spin_lock_init(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002913
2914 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
2915 if (tcomplete == NULL) {
2916 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002917 return_error_param = -ENOMEM;
2918 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002919 goto err_alloc_tcomplete_failed;
2920 }
2921 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
2922
Todd Kjosd99c7332017-06-29 12:01:53 -07002923 t->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002924
2925 if (reply)
2926 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen4bfac802017-02-03 14:40:50 -08002927 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002928 proc->pid, thread->pid, t->debug_id,
2929 target_proc->pid, target_thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002930 (u64)tr->data.ptr.buffer,
2931 (u64)tr->data.ptr.offsets,
Martijn Coenen4bfac802017-02-03 14:40:50 -08002932 (u64)tr->data_size, (u64)tr->offsets_size,
2933 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002934 else
2935 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen4bfac802017-02-03 14:40:50 -08002936 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002937 proc->pid, thread->pid, t->debug_id,
2938 target_proc->pid, target_node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002939 (u64)tr->data.ptr.buffer,
2940 (u64)tr->data.ptr.offsets,
Martijn Coenen4bfac802017-02-03 14:40:50 -08002941 (u64)tr->data_size, (u64)tr->offsets_size,
2942 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002943
2944 if (!reply && !(tr->flags & TF_ONE_WAY))
2945 t->from = thread;
2946 else
2947 t->from = NULL;
Tair Rzayev57bab7c2014-05-31 22:43:34 +03002948 t->sender_euid = task_euid(proc->tsk);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002949 t->to_proc = target_proc;
2950 t->to_thread = target_thread;
2951 t->code = tr->code;
2952 t->flags = tr->flags;
2953 t->priority = task_nice(current);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002954
2955 trace_binder_transaction(reply, t, target_node);
2956
Todd Kjos19c98722017-06-29 12:01:40 -07002957 t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size,
Martijn Coenen4bfac802017-02-03 14:40:50 -08002958 tr->offsets_size, extra_buffers_size,
2959 !reply && (t->flags & TF_ONE_WAY));
Todd Kjos57ada2f2017-06-29 12:01:46 -07002960 if (IS_ERR(t->buffer)) {
2961 /*
2962 * -ESRCH indicates VMA cleared. The target is dying.
2963 */
2964 return_error_param = PTR_ERR(t->buffer);
2965 return_error = return_error_param == -ESRCH ?
2966 BR_DEAD_REPLY : BR_FAILED_REPLY;
2967 return_error_line = __LINE__;
2968 t->buffer = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002969 goto err_binder_alloc_buf_failed;
2970 }
2971 t->buffer->allow_user_free = 0;
2972 t->buffer->debug_id = t->debug_id;
2973 t->buffer->transaction = t;
2974 t->buffer->target_node = target_node;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002975 trace_binder_transaction_alloc_buf(t->buffer);
Martijn Coenen79802402017-02-03 14:40:51 -08002976 off_start = (binder_size_t *)(t->buffer->data +
2977 ALIGN(tr->data_size, sizeof(void *)));
2978 offp = off_start;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002979
Arve Hjønnevågda498892014-02-21 14:40:26 -08002980 if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t)
2981 tr->data.ptr.buffer, tr->data_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302982 binder_user_error("%d:%d got transaction with invalid data ptr\n",
2983 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002984 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002985 return_error_param = -EFAULT;
2986 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002987 goto err_copy_data_failed;
2988 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08002989 if (copy_from_user(offp, (const void __user *)(uintptr_t)
2990 tr->data.ptr.offsets, tr->offsets_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302991 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
2992 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002993 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002994 return_error_param = -EFAULT;
2995 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002996 goto err_copy_data_failed;
2997 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08002998 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
2999 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
3000 proc->pid, thread->pid, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003001 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003002 return_error_param = -EINVAL;
3003 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003004 goto err_bad_offset;
3005 }
Martijn Coenen79802402017-02-03 14:40:51 -08003006 if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) {
3007 binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
3008 proc->pid, thread->pid,
3009 (u64)extra_buffers_size);
3010 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003011 return_error_param = -EINVAL;
3012 return_error_line = __LINE__;
Martijn Coenen79802402017-02-03 14:40:51 -08003013 goto err_bad_offset;
3014 }
3015 off_end = (void *)off_start + tr->offsets_size;
3016 sg_bufp = (u8 *)(PTR_ALIGN(off_end, sizeof(void *)));
3017 sg_buf_end = sg_bufp + extra_buffers_size;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08003018 off_min = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003019 for (; offp < off_end; offp++) {
Martijn Coenenfeba3902017-02-03 14:40:45 -08003020 struct binder_object_header *hdr;
3021 size_t object_size = binder_validate_object(t->buffer, *offp);
Seunghun Lee10f62862014-05-01 01:30:23 +09003022
Martijn Coenenfeba3902017-02-03 14:40:45 -08003023 if (object_size == 0 || *offp < off_min) {
3024 binder_user_error("%d:%d got transaction with invalid offset (%lld, min %lld max %lld) or object.\n",
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08003025 proc->pid, thread->pid, (u64)*offp,
3026 (u64)off_min,
Martijn Coenenfeba3902017-02-03 14:40:45 -08003027 (u64)t->buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003028 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003029 return_error_param = -EINVAL;
3030 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003031 goto err_bad_offset;
3032 }
Martijn Coenenfeba3902017-02-03 14:40:45 -08003033
3034 hdr = (struct binder_object_header *)(t->buffer->data + *offp);
3035 off_min = *offp + object_size;
3036 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003037 case BINDER_TYPE_BINDER:
3038 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08003039 struct flat_binder_object *fp;
Seunghun Lee10f62862014-05-01 01:30:23 +09003040
Martijn Coenenfeba3902017-02-03 14:40:45 -08003041 fp = to_flat_binder_object(hdr);
Martijn Coenena056af42017-02-03 14:40:49 -08003042 ret = binder_translate_binder(fp, t, thread);
3043 if (ret < 0) {
Christian Engelmayer7d420432014-05-07 21:44:53 +02003044 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003045 return_error_param = ret;
3046 return_error_line = __LINE__;
Martijn Coenena056af42017-02-03 14:40:49 -08003047 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003048 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003049 } break;
3050 case BINDER_TYPE_HANDLE:
3051 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08003052 struct flat_binder_object *fp;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02003053
Martijn Coenenfeba3902017-02-03 14:40:45 -08003054 fp = to_flat_binder_object(hdr);
Martijn Coenena056af42017-02-03 14:40:49 -08003055 ret = binder_translate_handle(fp, t, thread);
3056 if (ret < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003057 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003058 return_error_param = ret;
3059 return_error_line = __LINE__;
Martijn Coenena056af42017-02-03 14:40:49 -08003060 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003061 }
3062 } break;
3063
3064 case BINDER_TYPE_FD: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08003065 struct binder_fd_object *fp = to_binder_fd_object(hdr);
Martijn Coenena056af42017-02-03 14:40:49 -08003066 int target_fd = binder_translate_fd(fp->fd, t, thread,
3067 in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003068
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003069 if (target_fd < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003070 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003071 return_error_param = target_fd;
3072 return_error_line = __LINE__;
Martijn Coenena056af42017-02-03 14:40:49 -08003073 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003074 }
Martijn Coenenfeba3902017-02-03 14:40:45 -08003075 fp->pad_binder = 0;
3076 fp->fd = target_fd;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003077 } break;
Martijn Coenendef95c72017-02-03 14:40:52 -08003078 case BINDER_TYPE_FDA: {
3079 struct binder_fd_array_object *fda =
3080 to_binder_fd_array_object(hdr);
3081 struct binder_buffer_object *parent =
3082 binder_validate_ptr(t->buffer, fda->parent,
3083 off_start,
3084 offp - off_start);
3085 if (!parent) {
3086 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
3087 proc->pid, thread->pid);
3088 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003089 return_error_param = -EINVAL;
3090 return_error_line = __LINE__;
Martijn Coenendef95c72017-02-03 14:40:52 -08003091 goto err_bad_parent;
3092 }
3093 if (!binder_validate_fixup(t->buffer, off_start,
3094 parent, fda->parent_offset,
3095 last_fixup_obj,
3096 last_fixup_min_off)) {
3097 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
3098 proc->pid, thread->pid);
3099 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003100 return_error_param = -EINVAL;
3101 return_error_line = __LINE__;
Martijn Coenendef95c72017-02-03 14:40:52 -08003102 goto err_bad_parent;
3103 }
3104 ret = binder_translate_fd_array(fda, parent, t, thread,
3105 in_reply_to);
3106 if (ret < 0) {
3107 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003108 return_error_param = ret;
3109 return_error_line = __LINE__;
Martijn Coenendef95c72017-02-03 14:40:52 -08003110 goto err_translate_failed;
3111 }
3112 last_fixup_obj = parent;
3113 last_fixup_min_off =
3114 fda->parent_offset + sizeof(u32) * fda->num_fds;
3115 } break;
Martijn Coenen79802402017-02-03 14:40:51 -08003116 case BINDER_TYPE_PTR: {
3117 struct binder_buffer_object *bp =
3118 to_binder_buffer_object(hdr);
3119 size_t buf_left = sg_buf_end - sg_bufp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003120
Martijn Coenen79802402017-02-03 14:40:51 -08003121 if (bp->length > buf_left) {
3122 binder_user_error("%d:%d got transaction with too large buffer\n",
3123 proc->pid, thread->pid);
3124 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003125 return_error_param = -EINVAL;
3126 return_error_line = __LINE__;
Martijn Coenen79802402017-02-03 14:40:51 -08003127 goto err_bad_offset;
3128 }
3129 if (copy_from_user(sg_bufp,
3130 (const void __user *)(uintptr_t)
3131 bp->buffer, bp->length)) {
3132 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3133 proc->pid, thread->pid);
Todd Kjos57ada2f2017-06-29 12:01:46 -07003134 return_error_param = -EFAULT;
Martijn Coenen79802402017-02-03 14:40:51 -08003135 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003136 return_error_line = __LINE__;
Martijn Coenen79802402017-02-03 14:40:51 -08003137 goto err_copy_data_failed;
3138 }
3139 /* Fixup buffer pointer to target proc address space */
3140 bp->buffer = (uintptr_t)sg_bufp +
Todd Kjos19c98722017-06-29 12:01:40 -07003141 binder_alloc_get_user_buffer_offset(
3142 &target_proc->alloc);
Martijn Coenen79802402017-02-03 14:40:51 -08003143 sg_bufp += ALIGN(bp->length, sizeof(u64));
3144
3145 ret = binder_fixup_parent(t, thread, bp, off_start,
3146 offp - off_start,
3147 last_fixup_obj,
3148 last_fixup_min_off);
3149 if (ret < 0) {
3150 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003151 return_error_param = ret;
3152 return_error_line = __LINE__;
Martijn Coenen79802402017-02-03 14:40:51 -08003153 goto err_translate_failed;
3154 }
3155 last_fixup_obj = bp;
3156 last_fixup_min_off = 0;
3157 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003158 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01003159 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
Martijn Coenenfeba3902017-02-03 14:40:45 -08003160 proc->pid, thread->pid, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003161 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003162 return_error_param = -EINVAL;
3163 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003164 goto err_bad_object_type;
3165 }
3166 }
Todd Kjosccae6f62017-06-29 12:01:48 -07003167 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
Todd Kjos673068e2017-06-29 12:02:03 -07003168 t->work.type = BINDER_WORK_TRANSACTION;
Todd Kjosccae6f62017-06-29 12:01:48 -07003169
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003170 if (reply) {
Martijn Coenen148ade22017-11-15 09:21:35 +01003171 binder_enqueue_thread_work(thread, tcomplete);
Martijn Coenen0b89d692017-06-29 12:02:06 -07003172 binder_inner_proc_lock(target_proc);
3173 if (target_thread->is_dead) {
3174 binder_inner_proc_unlock(target_proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07003175 goto err_dead_proc_or_thread;
Martijn Coenen0b89d692017-06-29 12:02:06 -07003176 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003177 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen0b89d692017-06-29 12:02:06 -07003178 binder_pop_transaction_ilocked(target_thread, in_reply_to);
Martijn Coenen148ade22017-11-15 09:21:35 +01003179 binder_enqueue_thread_work_ilocked(target_thread, &t->work);
Martijn Coenen0b89d692017-06-29 12:02:06 -07003180 binder_inner_proc_unlock(target_proc);
Martijn Coenen408c68b2017-08-31 10:04:19 +02003181 wake_up_interruptible_sync(&target_thread->wait);
Todd Kjosb6d282c2017-06-29 12:01:54 -07003182 binder_free_transaction(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003183 } else if (!(t->flags & TF_ONE_WAY)) {
3184 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen0b89d692017-06-29 12:02:06 -07003185 binder_inner_proc_lock(proc);
Martijn Coenen148ade22017-11-15 09:21:35 +01003186 /*
3187 * Defer the TRANSACTION_COMPLETE, so we don't return to
3188 * userspace immediately; this allows the target process to
3189 * immediately start processing this transaction, reducing
3190 * latency. We will then return the TRANSACTION_COMPLETE when
3191 * the target replies (or there is an error).
3192 */
3193 binder_enqueue_deferred_thread_work_ilocked(thread, tcomplete);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003194 t->need_reply = 1;
3195 t->from_parent = thread->transaction_stack;
3196 thread->transaction_stack = t;
Martijn Coenen0b89d692017-06-29 12:02:06 -07003197 binder_inner_proc_unlock(proc);
Martijn Coenen408c68b2017-08-31 10:04:19 +02003198 if (!binder_proc_transaction(t, target_proc, target_thread)) {
Martijn Coenen0b89d692017-06-29 12:02:06 -07003199 binder_inner_proc_lock(proc);
3200 binder_pop_transaction_ilocked(thread, t);
3201 binder_inner_proc_unlock(proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07003202 goto err_dead_proc_or_thread;
3203 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003204 } else {
3205 BUG_ON(target_node == NULL);
3206 BUG_ON(t->buffer->async_transaction != 1);
Martijn Coenen148ade22017-11-15 09:21:35 +01003207 binder_enqueue_thread_work(thread, tcomplete);
Martijn Coenen408c68b2017-08-31 10:04:19 +02003208 if (!binder_proc_transaction(t, target_proc, NULL))
Todd Kjos7a4408c2017-06-29 12:01:57 -07003209 goto err_dead_proc_or_thread;
Riley Andrews00b40d62017-06-29 12:01:37 -07003210 }
Todd Kjos7a4408c2017-06-29 12:01:57 -07003211 if (target_thread)
3212 binder_thread_dec_tmpref(target_thread);
3213 binder_proc_dec_tmpref(target_proc);
Todd Kjos512cf462017-09-29 15:39:49 -07003214 if (target_node)
3215 binder_dec_node_tmpref(target_node);
Todd Kjosd99c7332017-06-29 12:01:53 -07003216 /*
3217 * write barrier to synchronize with initialization
3218 * of log entry
3219 */
3220 smp_wmb();
3221 WRITE_ONCE(e->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003222 return;
3223
Todd Kjos7a4408c2017-06-29 12:01:57 -07003224err_dead_proc_or_thread:
3225 return_error = BR_DEAD_REPLY;
3226 return_error_line = __LINE__;
Xu YiPingd53bebd2017-09-05 10:21:52 -07003227 binder_dequeue_work(proc, tcomplete);
Martijn Coenena056af42017-02-03 14:40:49 -08003228err_translate_failed:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003229err_bad_object_type:
3230err_bad_offset:
Martijn Coenendef95c72017-02-03 14:40:52 -08003231err_bad_parent:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003232err_copy_data_failed:
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003233 trace_binder_transaction_failed_buffer_release(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003234 binder_transaction_buffer_release(target_proc, t->buffer, offp);
Todd Kjos512cf462017-09-29 15:39:49 -07003235 if (target_node)
3236 binder_dec_node_tmpref(target_node);
Todd Kjoseb349832017-06-29 12:01:56 -07003237 target_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003238 t->buffer->transaction = NULL;
Todd Kjos19c98722017-06-29 12:01:40 -07003239 binder_alloc_free_buf(&target_proc->alloc, t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003240err_binder_alloc_buf_failed:
3241 kfree(tcomplete);
3242 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3243err_alloc_tcomplete_failed:
3244 kfree(t);
3245 binder_stats_deleted(BINDER_STAT_TRANSACTION);
3246err_alloc_t_failed:
3247err_bad_call_stack:
3248err_empty_call_stack:
3249err_dead_binder:
3250err_invalid_target_handle:
Todd Kjos7a4408c2017-06-29 12:01:57 -07003251 if (target_thread)
3252 binder_thread_dec_tmpref(target_thread);
3253 if (target_proc)
3254 binder_proc_dec_tmpref(target_proc);
Todd Kjos512cf462017-09-29 15:39:49 -07003255 if (target_node) {
Todd Kjoseb349832017-06-29 12:01:56 -07003256 binder_dec_node(target_node, 1, 0);
Todd Kjos512cf462017-09-29 15:39:49 -07003257 binder_dec_node_tmpref(target_node);
3258 }
Todd Kjoseb349832017-06-29 12:01:56 -07003259
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003260 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Todd Kjos57ada2f2017-06-29 12:01:46 -07003261 "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
3262 proc->pid, thread->pid, return_error, return_error_param,
3263 (u64)tr->data_size, (u64)tr->offsets_size,
3264 return_error_line);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003265
3266 {
3267 struct binder_transaction_log_entry *fe;
Seunghun Lee10f62862014-05-01 01:30:23 +09003268
Todd Kjos57ada2f2017-06-29 12:01:46 -07003269 e->return_error = return_error;
3270 e->return_error_param = return_error_param;
3271 e->return_error_line = return_error_line;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003272 fe = binder_transaction_log_add(&binder_transaction_log_failed);
3273 *fe = *e;
Todd Kjosd99c7332017-06-29 12:01:53 -07003274 /*
3275 * write barrier to synchronize with initialization
3276 * of log entry
3277 */
3278 smp_wmb();
3279 WRITE_ONCE(e->debug_id_done, t_debug_id);
3280 WRITE_ONCE(fe->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003281 }
3282
Todd Kjos26549d12017-06-29 12:01:55 -07003283 BUG_ON(thread->return_error.cmd != BR_OK);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003284 if (in_reply_to) {
Todd Kjos26549d12017-06-29 12:01:55 -07003285 thread->return_error.cmd = BR_TRANSACTION_COMPLETE;
Martijn Coenen148ade22017-11-15 09:21:35 +01003286 binder_enqueue_thread_work(thread, &thread->return_error.work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003287 binder_send_failed_reply(in_reply_to, return_error);
Todd Kjos26549d12017-06-29 12:01:55 -07003288 } else {
3289 thread->return_error.cmd = return_error;
Martijn Coenen148ade22017-11-15 09:21:35 +01003290 binder_enqueue_thread_work(thread, &thread->return_error.work);
Todd Kjos26549d12017-06-29 12:01:55 -07003291 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003292}
3293
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02003294static int binder_thread_write(struct binder_proc *proc,
3295 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003296 binder_uintptr_t binder_buffer, size_t size,
3297 binder_size_t *consumed)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003298{
3299 uint32_t cmd;
Martijn Coenen342e5c92017-02-03 14:40:46 -08003300 struct binder_context *context = proc->context;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003301 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003302 void __user *ptr = buffer + *consumed;
3303 void __user *end = buffer + size;
3304
Todd Kjos26549d12017-06-29 12:01:55 -07003305 while (ptr < end && thread->return_error.cmd == BR_OK) {
Todd Kjos372e3142017-06-29 12:01:58 -07003306 int ret;
3307
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003308 if (get_user(cmd, (uint32_t __user *)ptr))
3309 return -EFAULT;
3310 ptr += sizeof(uint32_t);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003311 trace_binder_command(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003312 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07003313 atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]);
3314 atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]);
3315 atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003316 }
3317 switch (cmd) {
3318 case BC_INCREFS:
3319 case BC_ACQUIRE:
3320 case BC_RELEASE:
3321 case BC_DECREFS: {
3322 uint32_t target;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003323 const char *debug_string;
Todd Kjos372e3142017-06-29 12:01:58 -07003324 bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE;
3325 bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE;
3326 struct binder_ref_data rdata;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003327
3328 if (get_user(target, (uint32_t __user *)ptr))
3329 return -EFAULT;
Todd Kjosc44b1232017-06-29 12:01:43 -07003330
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003331 ptr += sizeof(uint32_t);
Todd Kjos372e3142017-06-29 12:01:58 -07003332 ret = -1;
3333 if (increment && !target) {
Todd Kjosc44b1232017-06-29 12:01:43 -07003334 struct binder_node *ctx_mgr_node;
Todd Kjosc44b1232017-06-29 12:01:43 -07003335 mutex_lock(&context->context_mgr_node_lock);
3336 ctx_mgr_node = context->binder_context_mgr_node;
Todd Kjos372e3142017-06-29 12:01:58 -07003337 if (ctx_mgr_node)
3338 ret = binder_inc_ref_for_node(
3339 proc, ctx_mgr_node,
3340 strong, NULL, &rdata);
Todd Kjosc44b1232017-06-29 12:01:43 -07003341 mutex_unlock(&context->context_mgr_node_lock);
3342 }
Todd Kjos372e3142017-06-29 12:01:58 -07003343 if (ret)
3344 ret = binder_update_ref_for_handle(
3345 proc, target, increment, strong,
3346 &rdata);
3347 if (!ret && rdata.desc != target) {
3348 binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
3349 proc->pid, thread->pid,
3350 target, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003351 }
3352 switch (cmd) {
3353 case BC_INCREFS:
3354 debug_string = "IncRefs";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003355 break;
3356 case BC_ACQUIRE:
3357 debug_string = "Acquire";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003358 break;
3359 case BC_RELEASE:
3360 debug_string = "Release";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003361 break;
3362 case BC_DECREFS:
3363 default:
3364 debug_string = "DecRefs";
Todd Kjos372e3142017-06-29 12:01:58 -07003365 break;
3366 }
3367 if (ret) {
3368 binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
3369 proc->pid, thread->pid, debug_string,
3370 strong, target, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003371 break;
3372 }
3373 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjos372e3142017-06-29 12:01:58 -07003374 "%d:%d %s ref %d desc %d s %d w %d\n",
3375 proc->pid, thread->pid, debug_string,
3376 rdata.debug_id, rdata.desc, rdata.strong,
3377 rdata.weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003378 break;
3379 }
3380 case BC_INCREFS_DONE:
3381 case BC_ACQUIRE_DONE: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003382 binder_uintptr_t node_ptr;
3383 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003384 struct binder_node *node;
Todd Kjos673068e2017-06-29 12:02:03 -07003385 bool free_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003386
Arve Hjønnevågda498892014-02-21 14:40:26 -08003387 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003388 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003389 ptr += sizeof(binder_uintptr_t);
3390 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003391 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003392 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003393 node = binder_get_node(proc, node_ptr);
3394 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003395 binder_user_error("%d:%d %s u%016llx no match\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003396 proc->pid, thread->pid,
3397 cmd == BC_INCREFS_DONE ?
3398 "BC_INCREFS_DONE" :
3399 "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003400 (u64)node_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003401 break;
3402 }
3403 if (cookie != node->cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003404 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003405 proc->pid, thread->pid,
3406 cmd == BC_INCREFS_DONE ?
3407 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003408 (u64)node_ptr, node->debug_id,
3409 (u64)cookie, (u64)node->cookie);
Todd Kjosadc18842017-06-29 12:01:59 -07003410 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003411 break;
3412 }
Todd Kjos673068e2017-06-29 12:02:03 -07003413 binder_node_inner_lock(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003414 if (cmd == BC_ACQUIRE_DONE) {
3415 if (node->pending_strong_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303416 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003417 proc->pid, thread->pid,
3418 node->debug_id);
Todd Kjos673068e2017-06-29 12:02:03 -07003419 binder_node_inner_unlock(node);
Todd Kjosadc18842017-06-29 12:01:59 -07003420 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003421 break;
3422 }
3423 node->pending_strong_ref = 0;
3424 } else {
3425 if (node->pending_weak_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303426 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003427 proc->pid, thread->pid,
3428 node->debug_id);
Todd Kjos673068e2017-06-29 12:02:03 -07003429 binder_node_inner_unlock(node);
Todd Kjosadc18842017-06-29 12:01:59 -07003430 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003431 break;
3432 }
3433 node->pending_weak_ref = 0;
3434 }
Todd Kjos673068e2017-06-29 12:02:03 -07003435 free_node = binder_dec_node_nilocked(node,
3436 cmd == BC_ACQUIRE_DONE, 0);
3437 WARN_ON(free_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003438 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjosadc18842017-06-29 12:01:59 -07003439 "%d:%d %s node %d ls %d lw %d tr %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003440 proc->pid, thread->pid,
3441 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Todd Kjosadc18842017-06-29 12:01:59 -07003442 node->debug_id, node->local_strong_refs,
3443 node->local_weak_refs, node->tmp_refs);
Todd Kjos673068e2017-06-29 12:02:03 -07003444 binder_node_inner_unlock(node);
Todd Kjosadc18842017-06-29 12:01:59 -07003445 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003446 break;
3447 }
3448 case BC_ATTEMPT_ACQUIRE:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303449 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003450 return -EINVAL;
3451 case BC_ACQUIRE_RESULT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303452 pr_err("BC_ACQUIRE_RESULT not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003453 return -EINVAL;
3454
3455 case BC_FREE_BUFFER: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003456 binder_uintptr_t data_ptr;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003457 struct binder_buffer *buffer;
3458
Arve Hjønnevågda498892014-02-21 14:40:26 -08003459 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003460 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003461 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003462
Todd Kjos53d311cf2017-06-29 12:01:51 -07003463 buffer = binder_alloc_prepare_to_free(&proc->alloc,
3464 data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003465 if (buffer == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003466 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
3467 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003468 break;
3469 }
3470 if (!buffer->allow_user_free) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003471 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
3472 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003473 break;
3474 }
3475 binder_debug(BINDER_DEBUG_FREE_BUFFER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003476 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
3477 proc->pid, thread->pid, (u64)data_ptr,
3478 buffer->debug_id,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003479 buffer->transaction ? "active" : "finished");
3480
3481 if (buffer->transaction) {
3482 buffer->transaction->buffer = NULL;
3483 buffer->transaction = NULL;
3484 }
3485 if (buffer->async_transaction && buffer->target_node) {
Todd Kjos72196392017-06-29 12:02:02 -07003486 struct binder_node *buf_node;
3487 struct binder_work *w;
3488
3489 buf_node = buffer->target_node;
Todd Kjos673068e2017-06-29 12:02:03 -07003490 binder_node_inner_lock(buf_node);
Todd Kjos72196392017-06-29 12:02:02 -07003491 BUG_ON(!buf_node->has_async_transaction);
3492 BUG_ON(buf_node->proc != proc);
Todd Kjos72196392017-06-29 12:02:02 -07003493 w = binder_dequeue_work_head_ilocked(
3494 &buf_node->async_todo);
Martijn Coenen3a6430c2017-08-31 10:04:29 +02003495 if (!w) {
Gustavo A. R. Silva197410a2018-01-23 12:04:27 -06003496 buf_node->has_async_transaction = false;
Martijn Coenen3a6430c2017-08-31 10:04:29 +02003497 } else {
Todd Kjos72196392017-06-29 12:02:02 -07003498 binder_enqueue_work_ilocked(
Martijn Coenen3a6430c2017-08-31 10:04:29 +02003499 w, &proc->todo);
3500 binder_wakeup_proc_ilocked(proc);
3501 }
Todd Kjos673068e2017-06-29 12:02:03 -07003502 binder_node_inner_unlock(buf_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003503 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003504 trace_binder_transaction_buffer_release(buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003505 binder_transaction_buffer_release(proc, buffer, NULL);
Todd Kjos19c98722017-06-29 12:01:40 -07003506 binder_alloc_free_buf(&proc->alloc, buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003507 break;
3508 }
3509
Martijn Coenen79802402017-02-03 14:40:51 -08003510 case BC_TRANSACTION_SG:
3511 case BC_REPLY_SG: {
3512 struct binder_transaction_data_sg tr;
3513
3514 if (copy_from_user(&tr, ptr, sizeof(tr)))
3515 return -EFAULT;
3516 ptr += sizeof(tr);
3517 binder_transaction(proc, thread, &tr.transaction_data,
3518 cmd == BC_REPLY_SG, tr.buffers_size);
3519 break;
3520 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003521 case BC_TRANSACTION:
3522 case BC_REPLY: {
3523 struct binder_transaction_data tr;
3524
3525 if (copy_from_user(&tr, ptr, sizeof(tr)))
3526 return -EFAULT;
3527 ptr += sizeof(tr);
Martijn Coenen4bfac802017-02-03 14:40:50 -08003528 binder_transaction(proc, thread, &tr,
3529 cmd == BC_REPLY, 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003530 break;
3531 }
3532
3533 case BC_REGISTER_LOOPER:
3534 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303535 "%d:%d BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003536 proc->pid, thread->pid);
Todd Kjosb3e68612017-06-29 12:02:07 -07003537 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003538 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
3539 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303540 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003541 proc->pid, thread->pid);
3542 } else if (proc->requested_threads == 0) {
3543 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303544 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003545 proc->pid, thread->pid);
3546 } else {
3547 proc->requested_threads--;
3548 proc->requested_threads_started++;
3549 }
3550 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
Todd Kjosb3e68612017-06-29 12:02:07 -07003551 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003552 break;
3553 case BC_ENTER_LOOPER:
3554 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303555 "%d:%d BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003556 proc->pid, thread->pid);
3557 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
3558 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303559 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003560 proc->pid, thread->pid);
3561 }
3562 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
3563 break;
3564 case BC_EXIT_LOOPER:
3565 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303566 "%d:%d BC_EXIT_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003567 proc->pid, thread->pid);
3568 thread->looper |= BINDER_LOOPER_STATE_EXITED;
3569 break;
3570
3571 case BC_REQUEST_DEATH_NOTIFICATION:
3572 case BC_CLEAR_DEATH_NOTIFICATION: {
3573 uint32_t target;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003574 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003575 struct binder_ref *ref;
Todd Kjos2c1838d2017-06-29 12:02:08 -07003576 struct binder_ref_death *death = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003577
3578 if (get_user(target, (uint32_t __user *)ptr))
3579 return -EFAULT;
3580 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08003581 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003582 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003583 ptr += sizeof(binder_uintptr_t);
Todd Kjos2c1838d2017-06-29 12:02:08 -07003584 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3585 /*
3586 * Allocate memory for death notification
3587 * before taking lock
3588 */
3589 death = kzalloc(sizeof(*death), GFP_KERNEL);
3590 if (death == NULL) {
3591 WARN_ON(thread->return_error.cmd !=
3592 BR_OK);
3593 thread->return_error.cmd = BR_ERROR;
Martijn Coenen148ade22017-11-15 09:21:35 +01003594 binder_enqueue_thread_work(
3595 thread,
3596 &thread->return_error.work);
Todd Kjos2c1838d2017-06-29 12:02:08 -07003597 binder_debug(
3598 BINDER_DEBUG_FAILED_TRANSACTION,
3599 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
3600 proc->pid, thread->pid);
3601 break;
3602 }
3603 }
3604 binder_proc_lock(proc);
3605 ref = binder_get_ref_olocked(proc, target, false);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003606 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303607 binder_user_error("%d:%d %s invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003608 proc->pid, thread->pid,
3609 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3610 "BC_REQUEST_DEATH_NOTIFICATION" :
3611 "BC_CLEAR_DEATH_NOTIFICATION",
3612 target);
Todd Kjos2c1838d2017-06-29 12:02:08 -07003613 binder_proc_unlock(proc);
3614 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003615 break;
3616 }
3617
3618 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003619 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003620 proc->pid, thread->pid,
3621 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3622 "BC_REQUEST_DEATH_NOTIFICATION" :
3623 "BC_CLEAR_DEATH_NOTIFICATION",
Todd Kjos372e3142017-06-29 12:01:58 -07003624 (u64)cookie, ref->data.debug_id,
3625 ref->data.desc, ref->data.strong,
3626 ref->data.weak, ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003627
Martijn Coenenab51ec62017-06-29 12:02:10 -07003628 binder_node_lock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003629 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3630 if (ref->death) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303631 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003632 proc->pid, thread->pid);
Martijn Coenenab51ec62017-06-29 12:02:10 -07003633 binder_node_unlock(ref->node);
Todd Kjos2c1838d2017-06-29 12:02:08 -07003634 binder_proc_unlock(proc);
3635 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003636 break;
3637 }
3638 binder_stats_created(BINDER_STAT_DEATH);
3639 INIT_LIST_HEAD(&death->work.entry);
3640 death->cookie = cookie;
3641 ref->death = death;
3642 if (ref->node->proc == NULL) {
3643 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
Martijn Coenenbb745622017-08-31 10:04:28 +02003644
3645 binder_inner_proc_lock(proc);
3646 binder_enqueue_work_ilocked(
3647 &ref->death->work, &proc->todo);
3648 binder_wakeup_proc_ilocked(proc);
3649 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003650 }
3651 } else {
3652 if (ref->death == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303653 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003654 proc->pid, thread->pid);
Todd Kjos673068e2017-06-29 12:02:03 -07003655 binder_node_unlock(ref->node);
Todd Kjos2c1838d2017-06-29 12:02:08 -07003656 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003657 break;
3658 }
3659 death = ref->death;
3660 if (death->cookie != cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003661 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003662 proc->pid, thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003663 (u64)death->cookie,
3664 (u64)cookie);
Todd Kjos673068e2017-06-29 12:02:03 -07003665 binder_node_unlock(ref->node);
Todd Kjos2c1838d2017-06-29 12:02:08 -07003666 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003667 break;
3668 }
3669 ref->death = NULL;
Todd Kjos72196392017-06-29 12:02:02 -07003670 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003671 if (list_empty(&death->work.entry)) {
3672 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos72196392017-06-29 12:02:02 -07003673 if (thread->looper &
3674 (BINDER_LOOPER_STATE_REGISTERED |
3675 BINDER_LOOPER_STATE_ENTERED))
Martijn Coenen148ade22017-11-15 09:21:35 +01003676 binder_enqueue_thread_work_ilocked(
3677 thread,
3678 &death->work);
Todd Kjos72196392017-06-29 12:02:02 -07003679 else {
3680 binder_enqueue_work_ilocked(
3681 &death->work,
3682 &proc->todo);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02003683 binder_wakeup_proc_ilocked(
Martijn Coenen408c68b2017-08-31 10:04:19 +02003684 proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003685 }
3686 } else {
3687 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
3688 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
3689 }
Todd Kjos72196392017-06-29 12:02:02 -07003690 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003691 }
Martijn Coenenab51ec62017-06-29 12:02:10 -07003692 binder_node_unlock(ref->node);
Todd Kjos2c1838d2017-06-29 12:02:08 -07003693 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003694 } break;
3695 case BC_DEAD_BINDER_DONE: {
3696 struct binder_work *w;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003697 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003698 struct binder_ref_death *death = NULL;
Seunghun Lee10f62862014-05-01 01:30:23 +09003699
Arve Hjønnevågda498892014-02-21 14:40:26 -08003700 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003701 return -EFAULT;
3702
Lisa Du7a64cd82016-02-17 09:32:52 +08003703 ptr += sizeof(cookie);
Todd Kjos72196392017-06-29 12:02:02 -07003704 binder_inner_proc_lock(proc);
3705 list_for_each_entry(w, &proc->delivered_death,
3706 entry) {
3707 struct binder_ref_death *tmp_death =
3708 container_of(w,
3709 struct binder_ref_death,
3710 work);
Seunghun Lee10f62862014-05-01 01:30:23 +09003711
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003712 if (tmp_death->cookie == cookie) {
3713 death = tmp_death;
3714 break;
3715 }
3716 }
3717 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Todd Kjos8ca86f12018-02-07 13:57:37 -08003718 "%d:%d BC_DEAD_BINDER_DONE %016llx found %pK\n",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003719 proc->pid, thread->pid, (u64)cookie,
3720 death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003721 if (death == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003722 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
3723 proc->pid, thread->pid, (u64)cookie);
Todd Kjos72196392017-06-29 12:02:02 -07003724 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003725 break;
3726 }
Todd Kjos72196392017-06-29 12:02:02 -07003727 binder_dequeue_work_ilocked(&death->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003728 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
3729 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos72196392017-06-29 12:02:02 -07003730 if (thread->looper &
3731 (BINDER_LOOPER_STATE_REGISTERED |
3732 BINDER_LOOPER_STATE_ENTERED))
Martijn Coenen148ade22017-11-15 09:21:35 +01003733 binder_enqueue_thread_work_ilocked(
3734 thread, &death->work);
Todd Kjos72196392017-06-29 12:02:02 -07003735 else {
3736 binder_enqueue_work_ilocked(
3737 &death->work,
3738 &proc->todo);
Martijn Coenen408c68b2017-08-31 10:04:19 +02003739 binder_wakeup_proc_ilocked(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003740 }
3741 }
Todd Kjos72196392017-06-29 12:02:02 -07003742 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003743 } break;
3744
3745 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303746 pr_err("%d:%d unknown command %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003747 proc->pid, thread->pid, cmd);
3748 return -EINVAL;
3749 }
3750 *consumed = ptr - buffer;
3751 }
3752 return 0;
3753}
3754
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02003755static void binder_stat_br(struct binder_proc *proc,
3756 struct binder_thread *thread, uint32_t cmd)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003757{
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003758 trace_binder_return(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003759 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07003760 atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
3761 atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
3762 atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003763 }
3764}
3765
Todd Kjos26b47d82017-06-29 12:01:47 -07003766static int binder_put_node_cmd(struct binder_proc *proc,
3767 struct binder_thread *thread,
3768 void __user **ptrp,
3769 binder_uintptr_t node_ptr,
3770 binder_uintptr_t node_cookie,
3771 int node_debug_id,
3772 uint32_t cmd, const char *cmd_name)
3773{
3774 void __user *ptr = *ptrp;
3775
3776 if (put_user(cmd, (uint32_t __user *)ptr))
3777 return -EFAULT;
3778 ptr += sizeof(uint32_t);
3779
3780 if (put_user(node_ptr, (binder_uintptr_t __user *)ptr))
3781 return -EFAULT;
3782 ptr += sizeof(binder_uintptr_t);
3783
3784 if (put_user(node_cookie, (binder_uintptr_t __user *)ptr))
3785 return -EFAULT;
3786 ptr += sizeof(binder_uintptr_t);
3787
3788 binder_stat_br(proc, thread, cmd);
3789 binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n",
3790 proc->pid, thread->pid, cmd_name, node_debug_id,
3791 (u64)node_ptr, (u64)node_cookie);
3792
3793 *ptrp = ptr;
3794 return 0;
3795}
3796
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02003797static int binder_wait_for_work(struct binder_thread *thread,
3798 bool do_proc_work)
3799{
3800 DEFINE_WAIT(wait);
3801 struct binder_proc *proc = thread->proc;
3802 int ret = 0;
3803
3804 freezer_do_not_count();
3805 binder_inner_proc_lock(proc);
3806 for (;;) {
3807 prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE);
3808 if (binder_has_work_ilocked(thread, do_proc_work))
3809 break;
3810 if (do_proc_work)
3811 list_add(&thread->waiting_thread_node,
3812 &proc->waiting_threads);
3813 binder_inner_proc_unlock(proc);
3814 schedule();
3815 binder_inner_proc_lock(proc);
3816 list_del_init(&thread->waiting_thread_node);
3817 if (signal_pending(current)) {
3818 ret = -ERESTARTSYS;
3819 break;
3820 }
3821 }
3822 finish_wait(&thread->wait, &wait);
3823 binder_inner_proc_unlock(proc);
3824 freezer_count();
3825
3826 return ret;
3827}
3828
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003829static int binder_thread_read(struct binder_proc *proc,
3830 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003831 binder_uintptr_t binder_buffer, size_t size,
3832 binder_size_t *consumed, int non_block)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003833{
Arve Hjønnevågda498892014-02-21 14:40:26 -08003834 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003835 void __user *ptr = buffer + *consumed;
3836 void __user *end = buffer + size;
3837
3838 int ret = 0;
3839 int wait_for_proc_work;
3840
3841 if (*consumed == 0) {
3842 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
3843 return -EFAULT;
3844 ptr += sizeof(uint32_t);
3845 }
3846
3847retry:
Martijn Coenen0b89d692017-06-29 12:02:06 -07003848 binder_inner_proc_lock(proc);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02003849 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
Martijn Coenen0b89d692017-06-29 12:02:06 -07003850 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003851
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003852 thread->looper |= BINDER_LOOPER_STATE_WAITING;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003853
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003854 trace_binder_wait_for_work(wait_for_proc_work,
3855 !!thread->transaction_stack,
Todd Kjos72196392017-06-29 12:02:02 -07003856 !binder_worklist_empty(proc, &thread->todo));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003857 if (wait_for_proc_work) {
3858 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
3859 BINDER_LOOPER_STATE_ENTERED))) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303860 binder_user_error("%d:%d ERROR: Thread waiting for process work before calling BC_REGISTER_LOOPER or BC_ENTER_LOOPER (state %x)\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003861 proc->pid, thread->pid, thread->looper);
3862 wait_event_interruptible(binder_user_error_wait,
3863 binder_stop_on_user_error < 2);
3864 }
3865 binder_set_nice(proc->default_priority);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003866 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003867
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02003868 if (non_block) {
3869 if (!binder_has_work(thread, wait_for_proc_work))
3870 ret = -EAGAIN;
3871 } else {
3872 ret = binder_wait_for_work(thread, wait_for_proc_work);
3873 }
3874
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003875 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
3876
3877 if (ret)
3878 return ret;
3879
3880 while (1) {
3881 uint32_t cmd;
3882 struct binder_transaction_data tr;
Todd Kjos72196392017-06-29 12:02:02 -07003883 struct binder_work *w = NULL;
3884 struct list_head *list = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003885 struct binder_transaction *t = NULL;
Todd Kjos7a4408c2017-06-29 12:01:57 -07003886 struct binder_thread *t_from;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003887
Todd Kjosed297212017-06-29 12:02:01 -07003888 binder_inner_proc_lock(proc);
Todd Kjos72196392017-06-29 12:02:02 -07003889 if (!binder_worklist_empty_ilocked(&thread->todo))
3890 list = &thread->todo;
3891 else if (!binder_worklist_empty_ilocked(&proc->todo) &&
3892 wait_for_proc_work)
3893 list = &proc->todo;
3894 else {
3895 binder_inner_proc_unlock(proc);
3896
Dmitry Voytik395262a2014-09-08 18:16:34 +04003897 /* no data added */
Todd Kjos08dabce2017-06-29 12:01:49 -07003898 if (ptr - buffer == 4 && !thread->looper_need_return)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003899 goto retry;
3900 break;
3901 }
3902
Todd Kjosed297212017-06-29 12:02:01 -07003903 if (end - ptr < sizeof(tr) + 4) {
3904 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003905 break;
Todd Kjosed297212017-06-29 12:02:01 -07003906 }
Todd Kjos72196392017-06-29 12:02:02 -07003907 w = binder_dequeue_work_head_ilocked(list);
Martijn Coenen148ade22017-11-15 09:21:35 +01003908 if (binder_worklist_empty_ilocked(&thread->todo))
3909 thread->process_todo = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003910
3911 switch (w->type) {
3912 case BINDER_WORK_TRANSACTION: {
Todd Kjosed297212017-06-29 12:02:01 -07003913 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003914 t = container_of(w, struct binder_transaction, work);
3915 } break;
Todd Kjos26549d12017-06-29 12:01:55 -07003916 case BINDER_WORK_RETURN_ERROR: {
3917 struct binder_error *e = container_of(
3918 w, struct binder_error, work);
3919
3920 WARN_ON(e->cmd == BR_OK);
Todd Kjosed297212017-06-29 12:02:01 -07003921 binder_inner_proc_unlock(proc);
Todd Kjos26549d12017-06-29 12:01:55 -07003922 if (put_user(e->cmd, (uint32_t __user *)ptr))
3923 return -EFAULT;
3924 e->cmd = BR_OK;
3925 ptr += sizeof(uint32_t);
3926
Todd Kjos4f9adc82017-08-08 15:48:36 -07003927 binder_stat_br(proc, thread, e->cmd);
Todd Kjos26549d12017-06-29 12:01:55 -07003928 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003929 case BINDER_WORK_TRANSACTION_COMPLETE: {
Todd Kjosed297212017-06-29 12:02:01 -07003930 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003931 cmd = BR_TRANSACTION_COMPLETE;
3932 if (put_user(cmd, (uint32_t __user *)ptr))
3933 return -EFAULT;
3934 ptr += sizeof(uint32_t);
3935
3936 binder_stat_br(proc, thread, cmd);
3937 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303938 "%d:%d BR_TRANSACTION_COMPLETE\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003939 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003940 kfree(w);
3941 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3942 } break;
3943 case BINDER_WORK_NODE: {
3944 struct binder_node *node = container_of(w, struct binder_node, work);
Todd Kjos26b47d82017-06-29 12:01:47 -07003945 int strong, weak;
3946 binder_uintptr_t node_ptr = node->ptr;
3947 binder_uintptr_t node_cookie = node->cookie;
3948 int node_debug_id = node->debug_id;
3949 int has_weak_ref;
3950 int has_strong_ref;
3951 void __user *orig_ptr = ptr;
Seunghun Lee10f62862014-05-01 01:30:23 +09003952
Todd Kjos26b47d82017-06-29 12:01:47 -07003953 BUG_ON(proc != node->proc);
3954 strong = node->internal_strong_refs ||
3955 node->local_strong_refs;
3956 weak = !hlist_empty(&node->refs) ||
Todd Kjosadc18842017-06-29 12:01:59 -07003957 node->local_weak_refs ||
3958 node->tmp_refs || strong;
Todd Kjos26b47d82017-06-29 12:01:47 -07003959 has_strong_ref = node->has_strong_ref;
3960 has_weak_ref = node->has_weak_ref;
3961
3962 if (weak && !has_weak_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003963 node->has_weak_ref = 1;
3964 node->pending_weak_ref = 1;
3965 node->local_weak_refs++;
Todd Kjos26b47d82017-06-29 12:01:47 -07003966 }
3967 if (strong && !has_strong_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003968 node->has_strong_ref = 1;
3969 node->pending_strong_ref = 1;
3970 node->local_strong_refs++;
Todd Kjos26b47d82017-06-29 12:01:47 -07003971 }
3972 if (!strong && has_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003973 node->has_strong_ref = 0;
Todd Kjos26b47d82017-06-29 12:01:47 -07003974 if (!weak && has_weak_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003975 node->has_weak_ref = 0;
Todd Kjos26b47d82017-06-29 12:01:47 -07003976 if (!weak && !strong) {
3977 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
3978 "%d:%d node %d u%016llx c%016llx deleted\n",
3979 proc->pid, thread->pid,
3980 node_debug_id,
3981 (u64)node_ptr,
3982 (u64)node_cookie);
3983 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjosed297212017-06-29 12:02:01 -07003984 binder_inner_proc_unlock(proc);
Todd Kjos673068e2017-06-29 12:02:03 -07003985 binder_node_lock(node);
3986 /*
3987 * Acquire the node lock before freeing the
3988 * node to serialize with other threads that
3989 * may have been holding the node lock while
3990 * decrementing this node (avoids race where
3991 * this thread frees while the other thread
3992 * is unlocking the node after the final
3993 * decrement)
3994 */
3995 binder_node_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07003996 binder_free_node(node);
3997 } else
3998 binder_inner_proc_unlock(proc);
3999
Todd Kjos26b47d82017-06-29 12:01:47 -07004000 if (weak && !has_weak_ref)
4001 ret = binder_put_node_cmd(
4002 proc, thread, &ptr, node_ptr,
4003 node_cookie, node_debug_id,
4004 BR_INCREFS, "BR_INCREFS");
4005 if (!ret && strong && !has_strong_ref)
4006 ret = binder_put_node_cmd(
4007 proc, thread, &ptr, node_ptr,
4008 node_cookie, node_debug_id,
4009 BR_ACQUIRE, "BR_ACQUIRE");
4010 if (!ret && !strong && has_strong_ref)
4011 ret = binder_put_node_cmd(
4012 proc, thread, &ptr, node_ptr,
4013 node_cookie, node_debug_id,
4014 BR_RELEASE, "BR_RELEASE");
4015 if (!ret && !weak && has_weak_ref)
4016 ret = binder_put_node_cmd(
4017 proc, thread, &ptr, node_ptr,
4018 node_cookie, node_debug_id,
4019 BR_DECREFS, "BR_DECREFS");
4020 if (orig_ptr == ptr)
4021 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4022 "%d:%d node %d u%016llx c%016llx state unchanged\n",
4023 proc->pid, thread->pid,
4024 node_debug_id,
4025 (u64)node_ptr,
4026 (u64)node_cookie);
4027 if (ret)
4028 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004029 } break;
4030 case BINDER_WORK_DEAD_BINDER:
4031 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4032 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4033 struct binder_ref_death *death;
4034 uint32_t cmd;
Martijn Coenenab51ec62017-06-29 12:02:10 -07004035 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004036
4037 death = container_of(w, struct binder_ref_death, work);
4038 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
4039 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
4040 else
4041 cmd = BR_DEAD_BINDER;
Martijn Coenenab51ec62017-06-29 12:02:10 -07004042 cookie = death->cookie;
4043
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004044 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004045 "%d:%d %s %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004046 proc->pid, thread->pid,
4047 cmd == BR_DEAD_BINDER ?
4048 "BR_DEAD_BINDER" :
4049 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
Martijn Coenenab51ec62017-06-29 12:02:10 -07004050 (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004051 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
Martijn Coenenab51ec62017-06-29 12:02:10 -07004052 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004053 kfree(death);
4054 binder_stats_deleted(BINDER_STAT_DEATH);
Todd Kjosed297212017-06-29 12:02:01 -07004055 } else {
Todd Kjos72196392017-06-29 12:02:02 -07004056 binder_enqueue_work_ilocked(
4057 w, &proc->delivered_death);
Todd Kjosed297212017-06-29 12:02:01 -07004058 binder_inner_proc_unlock(proc);
4059 }
Martijn Coenenab51ec62017-06-29 12:02:10 -07004060 if (put_user(cmd, (uint32_t __user *)ptr))
4061 return -EFAULT;
4062 ptr += sizeof(uint32_t);
4063 if (put_user(cookie,
4064 (binder_uintptr_t __user *)ptr))
4065 return -EFAULT;
4066 ptr += sizeof(binder_uintptr_t);
4067 binder_stat_br(proc, thread, cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004068 if (cmd == BR_DEAD_BINDER)
4069 goto done; /* DEAD_BINDER notifications can cause transactions */
4070 } break;
4071 }
4072
4073 if (!t)
4074 continue;
4075
4076 BUG_ON(t->buffer == NULL);
4077 if (t->buffer->target_node) {
4078 struct binder_node *target_node = t->buffer->target_node;
Seunghun Lee10f62862014-05-01 01:30:23 +09004079
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004080 tr.target.ptr = target_node->ptr;
4081 tr.cookie = target_node->cookie;
4082 t->saved_priority = task_nice(current);
4083 if (t->priority < target_node->min_priority &&
4084 !(t->flags & TF_ONE_WAY))
4085 binder_set_nice(t->priority);
4086 else if (!(t->flags & TF_ONE_WAY) ||
4087 t->saved_priority > target_node->min_priority)
4088 binder_set_nice(target_node->min_priority);
4089 cmd = BR_TRANSACTION;
4090 } else {
Arve Hjønnevågda498892014-02-21 14:40:26 -08004091 tr.target.ptr = 0;
4092 tr.cookie = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004093 cmd = BR_REPLY;
4094 }
4095 tr.code = t->code;
4096 tr.flags = t->flags;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -06004097 tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004098
Todd Kjos7a4408c2017-06-29 12:01:57 -07004099 t_from = binder_get_txn_from(t);
4100 if (t_from) {
4101 struct task_struct *sender = t_from->proc->tsk;
Seunghun Lee10f62862014-05-01 01:30:23 +09004102
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004103 tr.sender_pid = task_tgid_nr_ns(sender,
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08004104 task_active_pid_ns(current));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004105 } else {
4106 tr.sender_pid = 0;
4107 }
4108
4109 tr.data_size = t->buffer->data_size;
4110 tr.offsets_size = t->buffer->offsets_size;
Todd Kjos19c98722017-06-29 12:01:40 -07004111 tr.data.ptr.buffer = (binder_uintptr_t)
4112 ((uintptr_t)t->buffer->data +
4113 binder_alloc_get_user_buffer_offset(&proc->alloc));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004114 tr.data.ptr.offsets = tr.data.ptr.buffer +
4115 ALIGN(t->buffer->data_size,
4116 sizeof(void *));
4117
Todd Kjos7a4408c2017-06-29 12:01:57 -07004118 if (put_user(cmd, (uint32_t __user *)ptr)) {
4119 if (t_from)
4120 binder_thread_dec_tmpref(t_from);
Martijn Coenenfb2c4452017-11-13 10:06:08 +01004121
4122 binder_cleanup_transaction(t, "put_user failed",
4123 BR_FAILED_REPLY);
4124
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004125 return -EFAULT;
Todd Kjos7a4408c2017-06-29 12:01:57 -07004126 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004127 ptr += sizeof(uint32_t);
Todd Kjos7a4408c2017-06-29 12:01:57 -07004128 if (copy_to_user(ptr, &tr, sizeof(tr))) {
4129 if (t_from)
4130 binder_thread_dec_tmpref(t_from);
Martijn Coenenfb2c4452017-11-13 10:06:08 +01004131
4132 binder_cleanup_transaction(t, "copy_to_user failed",
4133 BR_FAILED_REPLY);
4134
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004135 return -EFAULT;
Todd Kjos7a4408c2017-06-29 12:01:57 -07004136 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004137 ptr += sizeof(tr);
4138
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004139 trace_binder_transaction_received(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004140 binder_stat_br(proc, thread, cmd);
4141 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004142 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004143 proc->pid, thread->pid,
4144 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
4145 "BR_REPLY",
Todd Kjos7a4408c2017-06-29 12:01:57 -07004146 t->debug_id, t_from ? t_from->proc->pid : 0,
4147 t_from ? t_from->pid : 0, cmd,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004148 t->buffer->data_size, t->buffer->offsets_size,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004149 (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004150
Todd Kjos7a4408c2017-06-29 12:01:57 -07004151 if (t_from)
4152 binder_thread_dec_tmpref(t_from);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004153 t->buffer->allow_user_free = 1;
4154 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
Martijn Coenen0b89d692017-06-29 12:02:06 -07004155 binder_inner_proc_lock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004156 t->to_parent = thread->transaction_stack;
4157 t->to_thread = thread;
4158 thread->transaction_stack = t;
Martijn Coenen0b89d692017-06-29 12:02:06 -07004159 binder_inner_proc_unlock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004160 } else {
Todd Kjosb6d282c2017-06-29 12:01:54 -07004161 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004162 }
4163 break;
4164 }
4165
4166done:
4167
4168 *consumed = ptr - buffer;
Todd Kjosb3e68612017-06-29 12:02:07 -07004169 binder_inner_proc_lock(proc);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004170 if (proc->requested_threads == 0 &&
4171 list_empty(&thread->proc->waiting_threads) &&
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004172 proc->requested_threads_started < proc->max_threads &&
4173 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4174 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
4175 /*spawn a new thread if we leave this out */) {
4176 proc->requested_threads++;
Todd Kjosb3e68612017-06-29 12:02:07 -07004177 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004178 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304179 "%d:%d BR_SPAWN_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004180 proc->pid, thread->pid);
4181 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
4182 return -EFAULT;
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07004183 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
Todd Kjosb3e68612017-06-29 12:02:07 -07004184 } else
4185 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004186 return 0;
4187}
4188
Todd Kjos72196392017-06-29 12:02:02 -07004189static void binder_release_work(struct binder_proc *proc,
4190 struct list_head *list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004191{
4192 struct binder_work *w;
Seunghun Lee10f62862014-05-01 01:30:23 +09004193
Todd Kjos72196392017-06-29 12:02:02 -07004194 while (1) {
4195 w = binder_dequeue_work_head(proc, list);
4196 if (!w)
4197 return;
4198
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004199 switch (w->type) {
4200 case BINDER_WORK_TRANSACTION: {
4201 struct binder_transaction *t;
4202
4203 t = container_of(w, struct binder_transaction, work);
Martijn Coenenfb2c4452017-11-13 10:06:08 +01004204
4205 binder_cleanup_transaction(t, "process died.",
4206 BR_DEAD_REPLY);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004207 } break;
Todd Kjos26549d12017-06-29 12:01:55 -07004208 case BINDER_WORK_RETURN_ERROR: {
4209 struct binder_error *e = container_of(
4210 w, struct binder_error, work);
4211
4212 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4213 "undelivered TRANSACTION_ERROR: %u\n",
4214 e->cmd);
4215 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004216 case BINDER_WORK_TRANSACTION_COMPLETE: {
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004217 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304218 "undelivered TRANSACTION_COMPLETE\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004219 kfree(w);
4220 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4221 } break;
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004222 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4223 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4224 struct binder_ref_death *death;
4225
4226 death = container_of(w, struct binder_ref_death, work);
4227 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004228 "undelivered death notification, %016llx\n",
4229 (u64)death->cookie);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004230 kfree(death);
4231 binder_stats_deleted(BINDER_STAT_DEATH);
4232 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004233 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304234 pr_err("unexpected work type, %d, not freed\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004235 w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004236 break;
4237 }
4238 }
4239
4240}
4241
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004242static struct binder_thread *binder_get_thread_ilocked(
4243 struct binder_proc *proc, struct binder_thread *new_thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004244{
4245 struct binder_thread *thread = NULL;
4246 struct rb_node *parent = NULL;
4247 struct rb_node **p = &proc->threads.rb_node;
4248
4249 while (*p) {
4250 parent = *p;
4251 thread = rb_entry(parent, struct binder_thread, rb_node);
4252
4253 if (current->pid < thread->pid)
4254 p = &(*p)->rb_left;
4255 else if (current->pid > thread->pid)
4256 p = &(*p)->rb_right;
4257 else
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004258 return thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004259 }
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004260 if (!new_thread)
4261 return NULL;
4262 thread = new_thread;
4263 binder_stats_created(BINDER_STAT_THREAD);
4264 thread->proc = proc;
4265 thread->pid = current->pid;
4266 atomic_set(&thread->tmp_ref, 0);
4267 init_waitqueue_head(&thread->wait);
4268 INIT_LIST_HEAD(&thread->todo);
4269 rb_link_node(&thread->rb_node, parent, p);
4270 rb_insert_color(&thread->rb_node, &proc->threads);
4271 thread->looper_need_return = true;
4272 thread->return_error.work.type = BINDER_WORK_RETURN_ERROR;
4273 thread->return_error.cmd = BR_OK;
4274 thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR;
4275 thread->reply_error.cmd = BR_OK;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004276 INIT_LIST_HEAD(&new_thread->waiting_thread_node);
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004277 return thread;
4278}
4279
4280static struct binder_thread *binder_get_thread(struct binder_proc *proc)
4281{
4282 struct binder_thread *thread;
4283 struct binder_thread *new_thread;
4284
4285 binder_inner_proc_lock(proc);
4286 thread = binder_get_thread_ilocked(proc, NULL);
4287 binder_inner_proc_unlock(proc);
4288 if (!thread) {
4289 new_thread = kzalloc(sizeof(*thread), GFP_KERNEL);
4290 if (new_thread == NULL)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004291 return NULL;
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004292 binder_inner_proc_lock(proc);
4293 thread = binder_get_thread_ilocked(proc, new_thread);
4294 binder_inner_proc_unlock(proc);
4295 if (thread != new_thread)
4296 kfree(new_thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004297 }
4298 return thread;
4299}
4300
Todd Kjos7a4408c2017-06-29 12:01:57 -07004301static void binder_free_proc(struct binder_proc *proc)
4302{
4303 BUG_ON(!list_empty(&proc->todo));
4304 BUG_ON(!list_empty(&proc->delivered_death));
4305 binder_alloc_deferred_release(&proc->alloc);
4306 put_task_struct(proc->tsk);
4307 binder_stats_deleted(BINDER_STAT_PROC);
4308 kfree(proc);
4309}
4310
4311static void binder_free_thread(struct binder_thread *thread)
4312{
4313 BUG_ON(!list_empty(&thread->todo));
4314 binder_stats_deleted(BINDER_STAT_THREAD);
4315 binder_proc_dec_tmpref(thread->proc);
4316 kfree(thread);
4317}
4318
4319static int binder_thread_release(struct binder_proc *proc,
4320 struct binder_thread *thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004321{
4322 struct binder_transaction *t;
4323 struct binder_transaction *send_reply = NULL;
4324 int active_transactions = 0;
Todd Kjos7a4408c2017-06-29 12:01:57 -07004325 struct binder_transaction *last_t = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004326
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004327 binder_inner_proc_lock(thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07004328 /*
4329 * take a ref on the proc so it survives
4330 * after we remove this thread from proc->threads.
4331 * The corresponding dec is when we actually
4332 * free the thread in binder_free_thread()
4333 */
4334 proc->tmp_ref++;
4335 /*
4336 * take a ref on this thread to ensure it
4337 * survives while we are releasing it
4338 */
4339 atomic_inc(&thread->tmp_ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004340 rb_erase(&thread->rb_node, &proc->threads);
4341 t = thread->transaction_stack;
Todd Kjos7a4408c2017-06-29 12:01:57 -07004342 if (t) {
4343 spin_lock(&t->lock);
4344 if (t->to_thread == thread)
4345 send_reply = t;
4346 }
4347 thread->is_dead = true;
4348
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004349 while (t) {
Todd Kjos7a4408c2017-06-29 12:01:57 -07004350 last_t = t;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004351 active_transactions++;
4352 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304353 "release %d:%d transaction %d %s, still active\n",
4354 proc->pid, thread->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004355 t->debug_id,
4356 (t->to_thread == thread) ? "in" : "out");
4357
4358 if (t->to_thread == thread) {
4359 t->to_proc = NULL;
4360 t->to_thread = NULL;
4361 if (t->buffer) {
4362 t->buffer->transaction = NULL;
4363 t->buffer = NULL;
4364 }
4365 t = t->to_parent;
4366 } else if (t->from == thread) {
4367 t->from = NULL;
4368 t = t->from_parent;
4369 } else
4370 BUG();
Todd Kjos7a4408c2017-06-29 12:01:57 -07004371 spin_unlock(&last_t->lock);
4372 if (t)
4373 spin_lock(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004374 }
Martijn Coenenf5cb7792018-01-05 11:27:07 +01004375
4376 /*
4377 * If this thread used poll, make sure we remove the waitqueue
4378 * from any epoll data structures holding it with POLLFREE.
4379 * waitqueue_active() is safe to use here because we're holding
4380 * the inner lock.
4381 */
4382 if ((thread->looper & BINDER_LOOPER_STATE_POLL) &&
4383 waitqueue_active(&thread->wait)) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08004384 wake_up_poll(&thread->wait, EPOLLHUP | POLLFREE);
Martijn Coenenf5cb7792018-01-05 11:27:07 +01004385 }
4386
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004387 binder_inner_proc_unlock(thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07004388
Martijn Coenen5eeb2ca2018-02-16 09:47:15 +01004389 /*
4390 * This is needed to avoid races between wake_up_poll() above and
4391 * and ep_remove_waitqueue() called for other reasons (eg the epoll file
4392 * descriptor being closed); ep_remove_waitqueue() holds an RCU read
4393 * lock, so we can be sure it's done after calling synchronize_rcu().
4394 */
4395 if (thread->looper & BINDER_LOOPER_STATE_POLL)
4396 synchronize_rcu();
4397
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004398 if (send_reply)
4399 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
Todd Kjos72196392017-06-29 12:02:02 -07004400 binder_release_work(proc, &thread->todo);
Todd Kjos7a4408c2017-06-29 12:01:57 -07004401 binder_thread_dec_tmpref(thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004402 return active_transactions;
4403}
4404
Al Viroafc9a422017-07-03 06:39:46 -04004405static __poll_t binder_poll(struct file *filp,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004406 struct poll_table_struct *wait)
4407{
4408 struct binder_proc *proc = filp->private_data;
4409 struct binder_thread *thread = NULL;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004410 bool wait_for_proc_work;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004411
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004412 thread = binder_get_thread(proc);
Eric Biggersf8898262018-01-30 23:11:24 -08004413 if (!thread)
4414 return POLLERR;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004415
Martijn Coenen0b89d692017-06-29 12:02:06 -07004416 binder_inner_proc_lock(thread->proc);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004417 thread->looper |= BINDER_LOOPER_STATE_POLL;
4418 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
4419
Martijn Coenen0b89d692017-06-29 12:02:06 -07004420 binder_inner_proc_unlock(thread->proc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004421
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004422 poll_wait(filp, &thread->wait, wait);
4423
Martijn Coenen66b83a42017-10-09 14:26:56 +02004424 if (binder_has_work(thread, wait_for_proc_work))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08004425 return EPOLLIN;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004426
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004427 return 0;
4428}
4429
Tair Rzayev78260ac2014-06-03 22:27:21 +03004430static int binder_ioctl_write_read(struct file *filp,
4431 unsigned int cmd, unsigned long arg,
4432 struct binder_thread *thread)
4433{
4434 int ret = 0;
4435 struct binder_proc *proc = filp->private_data;
4436 unsigned int size = _IOC_SIZE(cmd);
4437 void __user *ubuf = (void __user *)arg;
4438 struct binder_write_read bwr;
4439
4440 if (size != sizeof(struct binder_write_read)) {
4441 ret = -EINVAL;
4442 goto out;
4443 }
4444 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
4445 ret = -EFAULT;
4446 goto out;
4447 }
4448 binder_debug(BINDER_DEBUG_READ_WRITE,
4449 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
4450 proc->pid, thread->pid,
4451 (u64)bwr.write_size, (u64)bwr.write_buffer,
4452 (u64)bwr.read_size, (u64)bwr.read_buffer);
4453
4454 if (bwr.write_size > 0) {
4455 ret = binder_thread_write(proc, thread,
4456 bwr.write_buffer,
4457 bwr.write_size,
4458 &bwr.write_consumed);
4459 trace_binder_write_done(ret);
4460 if (ret < 0) {
4461 bwr.read_consumed = 0;
4462 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4463 ret = -EFAULT;
4464 goto out;
4465 }
4466 }
4467 if (bwr.read_size > 0) {
4468 ret = binder_thread_read(proc, thread, bwr.read_buffer,
4469 bwr.read_size,
4470 &bwr.read_consumed,
4471 filp->f_flags & O_NONBLOCK);
4472 trace_binder_read_done(ret);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004473 binder_inner_proc_lock(proc);
4474 if (!binder_worklist_empty_ilocked(&proc->todo))
Martijn Coenen408c68b2017-08-31 10:04:19 +02004475 binder_wakeup_proc_ilocked(proc);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004476 binder_inner_proc_unlock(proc);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004477 if (ret < 0) {
4478 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4479 ret = -EFAULT;
4480 goto out;
4481 }
4482 }
4483 binder_debug(BINDER_DEBUG_READ_WRITE,
4484 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
4485 proc->pid, thread->pid,
4486 (u64)bwr.write_consumed, (u64)bwr.write_size,
4487 (u64)bwr.read_consumed, (u64)bwr.read_size);
4488 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
4489 ret = -EFAULT;
4490 goto out;
4491 }
4492out:
4493 return ret;
4494}
4495
4496static int binder_ioctl_set_ctx_mgr(struct file *filp)
4497{
4498 int ret = 0;
4499 struct binder_proc *proc = filp->private_data;
Martijn Coenen342e5c92017-02-03 14:40:46 -08004500 struct binder_context *context = proc->context;
Todd Kjosc44b1232017-06-29 12:01:43 -07004501 struct binder_node *new_node;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004502 kuid_t curr_euid = current_euid();
4503
Todd Kjosc44b1232017-06-29 12:01:43 -07004504 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen342e5c92017-02-03 14:40:46 -08004505 if (context->binder_context_mgr_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004506 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
4507 ret = -EBUSY;
4508 goto out;
4509 }
Stephen Smalley79af7302015-01-21 10:54:10 -05004510 ret = security_binder_set_context_mgr(proc->tsk);
4511 if (ret < 0)
4512 goto out;
Martijn Coenen342e5c92017-02-03 14:40:46 -08004513 if (uid_valid(context->binder_context_mgr_uid)) {
4514 if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004515 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
4516 from_kuid(&init_user_ns, curr_euid),
4517 from_kuid(&init_user_ns,
Martijn Coenen342e5c92017-02-03 14:40:46 -08004518 context->binder_context_mgr_uid));
Tair Rzayev78260ac2014-06-03 22:27:21 +03004519 ret = -EPERM;
4520 goto out;
4521 }
4522 } else {
Martijn Coenen342e5c92017-02-03 14:40:46 -08004523 context->binder_context_mgr_uid = curr_euid;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004524 }
Todd Kjos673068e2017-06-29 12:02:03 -07004525 new_node = binder_new_node(proc, NULL);
Todd Kjosc44b1232017-06-29 12:01:43 -07004526 if (!new_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004527 ret = -ENOMEM;
4528 goto out;
4529 }
Todd Kjos673068e2017-06-29 12:02:03 -07004530 binder_node_lock(new_node);
Todd Kjosc44b1232017-06-29 12:01:43 -07004531 new_node->local_weak_refs++;
4532 new_node->local_strong_refs++;
4533 new_node->has_strong_ref = 1;
4534 new_node->has_weak_ref = 1;
4535 context->binder_context_mgr_node = new_node;
Todd Kjos673068e2017-06-29 12:02:03 -07004536 binder_node_unlock(new_node);
Todd Kjosadc18842017-06-29 12:01:59 -07004537 binder_put_node(new_node);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004538out:
Todd Kjosc44b1232017-06-29 12:01:43 -07004539 mutex_unlock(&context->context_mgr_node_lock);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004540 return ret;
4541}
4542
Colin Crossabcc6152017-08-31 10:04:24 +02004543static int binder_ioctl_get_node_debug_info(struct binder_proc *proc,
4544 struct binder_node_debug_info *info)
4545{
4546 struct rb_node *n;
4547 binder_uintptr_t ptr = info->ptr;
4548
4549 memset(info, 0, sizeof(*info));
4550
4551 binder_inner_proc_lock(proc);
4552 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
4553 struct binder_node *node = rb_entry(n, struct binder_node,
4554 rb_node);
4555 if (node->ptr > ptr) {
4556 info->ptr = node->ptr;
4557 info->cookie = node->cookie;
4558 info->has_strong_ref = node->has_strong_ref;
4559 info->has_weak_ref = node->has_weak_ref;
4560 break;
4561 }
4562 }
4563 binder_inner_proc_unlock(proc);
4564
4565 return 0;
4566}
4567
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004568static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4569{
4570 int ret;
4571 struct binder_proc *proc = filp->private_data;
4572 struct binder_thread *thread;
4573 unsigned int size = _IOC_SIZE(cmd);
4574 void __user *ubuf = (void __user *)arg;
4575
Tair Rzayev78260ac2014-06-03 22:27:21 +03004576 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
4577 proc->pid, current->pid, cmd, arg);*/
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004578
Sherry Yang4175e2b2017-08-23 08:46:40 -07004579 binder_selftest_alloc(&proc->alloc);
4580
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004581 trace_binder_ioctl(cmd, arg);
4582
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004583 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4584 if (ret)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004585 goto err_unlocked;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004586
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004587 thread = binder_get_thread(proc);
4588 if (thread == NULL) {
4589 ret = -ENOMEM;
4590 goto err;
4591 }
4592
4593 switch (cmd) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004594 case BINDER_WRITE_READ:
4595 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
4596 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004597 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004598 break;
Todd Kjosb3e68612017-06-29 12:02:07 -07004599 case BINDER_SET_MAX_THREADS: {
4600 int max_threads;
4601
4602 if (copy_from_user(&max_threads, ubuf,
4603 sizeof(max_threads))) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004604 ret = -EINVAL;
4605 goto err;
4606 }
Todd Kjosb3e68612017-06-29 12:02:07 -07004607 binder_inner_proc_lock(proc);
4608 proc->max_threads = max_threads;
4609 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004610 break;
Todd Kjosb3e68612017-06-29 12:02:07 -07004611 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004612 case BINDER_SET_CONTEXT_MGR:
Tair Rzayev78260ac2014-06-03 22:27:21 +03004613 ret = binder_ioctl_set_ctx_mgr(filp);
4614 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004615 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004616 break;
4617 case BINDER_THREAD_EXIT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304618 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004619 proc->pid, thread->pid);
Todd Kjos7a4408c2017-06-29 12:01:57 -07004620 binder_thread_release(proc, thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004621 thread = NULL;
4622 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02004623 case BINDER_VERSION: {
4624 struct binder_version __user *ver = ubuf;
4625
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004626 if (size != sizeof(struct binder_version)) {
4627 ret = -EINVAL;
4628 goto err;
4629 }
Mathieu Maret36c89c02014-04-15 12:03:05 +02004630 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
4631 &ver->protocol_version)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004632 ret = -EINVAL;
4633 goto err;
4634 }
4635 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02004636 }
Colin Crossabcc6152017-08-31 10:04:24 +02004637 case BINDER_GET_NODE_DEBUG_INFO: {
4638 struct binder_node_debug_info info;
4639
4640 if (copy_from_user(&info, ubuf, sizeof(info))) {
4641 ret = -EFAULT;
4642 goto err;
4643 }
4644
4645 ret = binder_ioctl_get_node_debug_info(proc, &info);
4646 if (ret < 0)
4647 goto err;
4648
4649 if (copy_to_user(ubuf, &info, sizeof(info))) {
4650 ret = -EFAULT;
4651 goto err;
4652 }
4653 break;
4654 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004655 default:
4656 ret = -EINVAL;
4657 goto err;
4658 }
4659 ret = 0;
4660err:
4661 if (thread)
Todd Kjos08dabce2017-06-29 12:01:49 -07004662 thread->looper_need_return = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004663 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4664 if (ret && ret != -ERESTARTSYS)
Anmol Sarma56b468f2012-10-30 22:35:43 +05304665 pr_info("%d:%d ioctl %x %lx returned %d\n", proc->pid, current->pid, cmd, arg, ret);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004666err_unlocked:
4667 trace_binder_ioctl_done(ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004668 return ret;
4669}
4670
4671static void binder_vma_open(struct vm_area_struct *vma)
4672{
4673 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004674
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004675 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304676 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004677 proc->pid, vma->vm_start, vma->vm_end,
4678 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4679 (unsigned long)pgprot_val(vma->vm_page_prot));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004680}
4681
4682static void binder_vma_close(struct vm_area_struct *vma)
4683{
4684 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004685
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004686 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304687 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004688 proc->pid, vma->vm_start, vma->vm_end,
4689 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4690 (unsigned long)pgprot_val(vma->vm_page_prot));
Todd Kjos19c98722017-06-29 12:01:40 -07004691 binder_alloc_vma_close(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004692 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
4693}
4694
Dave Jiang11bac802017-02-24 14:56:41 -08004695static int binder_vm_fault(struct vm_fault *vmf)
Vinayak Menonddac7d52014-06-02 18:17:59 +05304696{
4697 return VM_FAULT_SIGBUS;
4698}
4699
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07004700static const struct vm_operations_struct binder_vm_ops = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004701 .open = binder_vma_open,
4702 .close = binder_vma_close,
Vinayak Menonddac7d52014-06-02 18:17:59 +05304703 .fault = binder_vm_fault,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004704};
4705
Todd Kjos19c98722017-06-29 12:01:40 -07004706static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
4707{
4708 int ret;
4709 struct binder_proc *proc = filp->private_data;
4710 const char *failure_string;
4711
4712 if (proc->tsk != current->group_leader)
4713 return -EINVAL;
4714
4715 if ((vma->vm_end - vma->vm_start) > SZ_4M)
4716 vma->vm_end = vma->vm_start + SZ_4M;
4717
4718 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4719 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
4720 __func__, proc->pid, vma->vm_start, vma->vm_end,
4721 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4722 (unsigned long)pgprot_val(vma->vm_page_prot));
4723
4724 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
4725 ret = -EPERM;
4726 failure_string = "bad vm_flags";
4727 goto err_bad_arg;
4728 }
4729 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
4730 vma->vm_ops = &binder_vm_ops;
4731 vma->vm_private_data = proc;
4732
4733 ret = binder_alloc_mmap_handler(&proc->alloc, vma);
4734 if (ret)
4735 return ret;
Todd Kjos7f3dc002017-11-27 09:32:33 -08004736 mutex_lock(&proc->files_lock);
Todd Kjos19c98722017-06-29 12:01:40 -07004737 proc->files = get_files_struct(current);
Todd Kjos7f3dc002017-11-27 09:32:33 -08004738 mutex_unlock(&proc->files_lock);
Todd Kjos19c98722017-06-29 12:01:40 -07004739 return 0;
4740
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004741err_bad_arg:
Elad Wexler00c41cd2017-12-29 11:03:37 +02004742 pr_err("%s: %d %lx-%lx %s failed %d\n", __func__,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004743 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
4744 return ret;
4745}
4746
4747static int binder_open(struct inode *nodp, struct file *filp)
4748{
4749 struct binder_proc *proc;
Martijn Coenenac4812c2017-02-03 14:40:48 -08004750 struct binder_device *binder_dev;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004751
Elad Wexler00c41cd2017-12-29 11:03:37 +02004752 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "%s: %d:%d\n", __func__,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004753 current->group_leader->pid, current->pid);
4754
4755 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
4756 if (proc == NULL)
4757 return -ENOMEM;
Todd Kjos9630fe82017-06-29 12:02:00 -07004758 spin_lock_init(&proc->inner_lock);
4759 spin_lock_init(&proc->outer_lock);
Todd Kjosc4ea41b2017-06-29 12:01:36 -07004760 get_task_struct(current->group_leader);
4761 proc->tsk = current->group_leader;
Todd Kjos7f3dc002017-11-27 09:32:33 -08004762 mutex_init(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004763 INIT_LIST_HEAD(&proc->todo);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004764 proc->default_priority = task_nice(current);
Martijn Coenenac4812c2017-02-03 14:40:48 -08004765 binder_dev = container_of(filp->private_data, struct binder_device,
4766 miscdev);
4767 proc->context = &binder_dev->context;
Todd Kjos19c98722017-06-29 12:01:40 -07004768 binder_alloc_init(&proc->alloc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004769
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004770 binder_stats_created(BINDER_STAT_PROC);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004771 proc->pid = current->group_leader->pid;
4772 INIT_LIST_HEAD(&proc->delivered_death);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004773 INIT_LIST_HEAD(&proc->waiting_threads);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004774 filp->private_data = proc;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004775
Todd Kjosc44b1232017-06-29 12:01:43 -07004776 mutex_lock(&binder_procs_lock);
4777 hlist_add_head(&proc->proc_node, &binder_procs);
4778 mutex_unlock(&binder_procs_lock);
4779
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004780 if (binder_debugfs_dir_entry_proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004781 char strbuf[11];
Seunghun Lee10f62862014-05-01 01:30:23 +09004782
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004783 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
Martijn Coenen14db3182017-02-03 14:40:47 -08004784 /*
4785 * proc debug entries are shared between contexts, so
4786 * this will fail if the process tries to open the driver
4787 * again with a different context. The priting code will
4788 * anyway print all contexts that a given PID has, so this
4789 * is not a problem.
4790 */
Harsh Shandilya21d02dd2017-12-22 19:37:02 +05304791 proc->debugfs_entry = debugfs_create_file(strbuf, 0444,
Martijn Coenen14db3182017-02-03 14:40:47 -08004792 binder_debugfs_dir_entry_proc,
4793 (void *)(unsigned long)proc->pid,
4794 &binder_proc_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004795 }
4796
4797 return 0;
4798}
4799
4800static int binder_flush(struct file *filp, fl_owner_t id)
4801{
4802 struct binder_proc *proc = filp->private_data;
4803
4804 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
4805
4806 return 0;
4807}
4808
4809static void binder_deferred_flush(struct binder_proc *proc)
4810{
4811 struct rb_node *n;
4812 int wake_count = 0;
Seunghun Lee10f62862014-05-01 01:30:23 +09004813
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004814 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004815 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
4816 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
Seunghun Lee10f62862014-05-01 01:30:23 +09004817
Todd Kjos08dabce2017-06-29 12:01:49 -07004818 thread->looper_need_return = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004819 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
4820 wake_up_interruptible(&thread->wait);
4821 wake_count++;
4822 }
4823 }
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004824 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004825
4826 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4827 "binder_flush: %d woke %d threads\n", proc->pid,
4828 wake_count);
4829}
4830
4831static int binder_release(struct inode *nodp, struct file *filp)
4832{
4833 struct binder_proc *proc = filp->private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004834
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004835 debugfs_remove(proc->debugfs_entry);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004836 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
4837
4838 return 0;
4839}
4840
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004841static int binder_node_release(struct binder_node *node, int refs)
4842{
4843 struct binder_ref *ref;
4844 int death = 0;
Todd Kjosed297212017-06-29 12:02:01 -07004845 struct binder_proc *proc = node->proc;
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004846
Todd Kjos72196392017-06-29 12:02:02 -07004847 binder_release_work(proc, &node->async_todo);
Todd Kjosed297212017-06-29 12:02:01 -07004848
Todd Kjos673068e2017-06-29 12:02:03 -07004849 binder_node_lock(node);
Todd Kjosed297212017-06-29 12:02:01 -07004850 binder_inner_proc_lock(proc);
Todd Kjos72196392017-06-29 12:02:02 -07004851 binder_dequeue_work_ilocked(&node->work);
Todd Kjosadc18842017-06-29 12:01:59 -07004852 /*
4853 * The caller must have taken a temporary ref on the node,
4854 */
4855 BUG_ON(!node->tmp_refs);
4856 if (hlist_empty(&node->refs) && node->tmp_refs == 1) {
Todd Kjosed297212017-06-29 12:02:01 -07004857 binder_inner_proc_unlock(proc);
Todd Kjos673068e2017-06-29 12:02:03 -07004858 binder_node_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07004859 binder_free_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004860
4861 return refs;
4862 }
4863
4864 node->proc = NULL;
4865 node->local_strong_refs = 0;
4866 node->local_weak_refs = 0;
Todd Kjosed297212017-06-29 12:02:01 -07004867 binder_inner_proc_unlock(proc);
Todd Kjosc44b1232017-06-29 12:01:43 -07004868
4869 spin_lock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004870 hlist_add_head(&node->dead_node, &binder_dead_nodes);
Todd Kjosc44b1232017-06-29 12:01:43 -07004871 spin_unlock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004872
4873 hlist_for_each_entry(ref, &node->refs, node_entry) {
4874 refs++;
Martijn Coenenab51ec62017-06-29 12:02:10 -07004875 /*
4876 * Need the node lock to synchronize
4877 * with new notification requests and the
4878 * inner lock to synchronize with queued
4879 * death notifications.
4880 */
4881 binder_inner_proc_lock(ref->proc);
4882 if (!ref->death) {
4883 binder_inner_proc_unlock(ref->proc);
Arve Hjønnevåge194fd82014-02-17 13:58:29 -08004884 continue;
Martijn Coenenab51ec62017-06-29 12:02:10 -07004885 }
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004886
4887 death++;
4888
Martijn Coenenab51ec62017-06-29 12:02:10 -07004889 BUG_ON(!list_empty(&ref->death->work.entry));
4890 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
4891 binder_enqueue_work_ilocked(&ref->death->work,
4892 &ref->proc->todo);
Martijn Coenen408c68b2017-08-31 10:04:19 +02004893 binder_wakeup_proc_ilocked(ref->proc);
Todd Kjos72196392017-06-29 12:02:02 -07004894 binder_inner_proc_unlock(ref->proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004895 }
4896
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004897 binder_debug(BINDER_DEBUG_DEAD_BINDER,
4898 "node %d now dead, refs %d, death %d\n",
4899 node->debug_id, refs, death);
Todd Kjos673068e2017-06-29 12:02:03 -07004900 binder_node_unlock(node);
Todd Kjosadc18842017-06-29 12:01:59 -07004901 binder_put_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004902
4903 return refs;
4904}
4905
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004906static void binder_deferred_release(struct binder_proc *proc)
4907{
Martijn Coenen342e5c92017-02-03 14:40:46 -08004908 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004909 struct rb_node *n;
Todd Kjos19c98722017-06-29 12:01:40 -07004910 int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004911
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004912 BUG_ON(proc->files);
4913
Todd Kjosc44b1232017-06-29 12:01:43 -07004914 mutex_lock(&binder_procs_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004915 hlist_del(&proc->proc_node);
Todd Kjosc44b1232017-06-29 12:01:43 -07004916 mutex_unlock(&binder_procs_lock);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004917
Todd Kjosc44b1232017-06-29 12:01:43 -07004918 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen342e5c92017-02-03 14:40:46 -08004919 if (context->binder_context_mgr_node &&
4920 context->binder_context_mgr_node->proc == proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004921 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01004922 "%s: %d context_mgr_node gone\n",
4923 __func__, proc->pid);
Martijn Coenen342e5c92017-02-03 14:40:46 -08004924 context->binder_context_mgr_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004925 }
Todd Kjosc44b1232017-06-29 12:01:43 -07004926 mutex_unlock(&context->context_mgr_node_lock);
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004927 binder_inner_proc_lock(proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07004928 /*
4929 * Make sure proc stays alive after we
4930 * remove all the threads
4931 */
4932 proc->tmp_ref++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004933
Todd Kjos7a4408c2017-06-29 12:01:57 -07004934 proc->is_dead = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004935 threads = 0;
4936 active_transactions = 0;
4937 while ((n = rb_first(&proc->threads))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004938 struct binder_thread *thread;
4939
4940 thread = rb_entry(n, struct binder_thread, rb_node);
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004941 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004942 threads++;
Todd Kjos7a4408c2017-06-29 12:01:57 -07004943 active_transactions += binder_thread_release(proc, thread);
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004944 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004945 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004946
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004947 nodes = 0;
4948 incoming_refs = 0;
4949 while ((n = rb_first(&proc->nodes))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004950 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004951
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004952 node = rb_entry(n, struct binder_node, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004953 nodes++;
Todd Kjosadc18842017-06-29 12:01:59 -07004954 /*
4955 * take a temporary ref on the node before
4956 * calling binder_node_release() which will either
4957 * kfree() the node or call binder_put_node()
4958 */
Todd Kjosda0fa9e2017-06-29 12:02:04 -07004959 binder_inc_node_tmpref_ilocked(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004960 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07004961 binder_inner_proc_unlock(proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004962 incoming_refs = binder_node_release(node, incoming_refs);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07004963 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004964 }
Todd Kjosda0fa9e2017-06-29 12:02:04 -07004965 binder_inner_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004966
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004967 outgoing_refs = 0;
Todd Kjos2c1838d2017-06-29 12:02:08 -07004968 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004969 while ((n = rb_first(&proc->refs_by_desc))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004970 struct binder_ref *ref;
4971
4972 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004973 outgoing_refs++;
Todd Kjos2c1838d2017-06-29 12:02:08 -07004974 binder_cleanup_ref_olocked(ref);
4975 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07004976 binder_free_ref(ref);
Todd Kjos2c1838d2017-06-29 12:02:08 -07004977 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004978 }
Todd Kjos2c1838d2017-06-29 12:02:08 -07004979 binder_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004980
Todd Kjos72196392017-06-29 12:02:02 -07004981 binder_release_work(proc, &proc->todo);
4982 binder_release_work(proc, &proc->delivered_death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004983
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004984 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Todd Kjos19c98722017-06-29 12:01:40 -07004985 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01004986 __func__, proc->pid, threads, nodes, incoming_refs,
Todd Kjos19c98722017-06-29 12:01:40 -07004987 outgoing_refs, active_transactions);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004988
Todd Kjos7a4408c2017-06-29 12:01:57 -07004989 binder_proc_dec_tmpref(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004990}
4991
4992static void binder_deferred_func(struct work_struct *work)
4993{
4994 struct binder_proc *proc;
4995 struct files_struct *files;
4996
4997 int defer;
Seunghun Lee10f62862014-05-01 01:30:23 +09004998
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004999 do {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005000 mutex_lock(&binder_deferred_lock);
5001 if (!hlist_empty(&binder_deferred_list)) {
5002 proc = hlist_entry(binder_deferred_list.first,
5003 struct binder_proc, deferred_work_node);
5004 hlist_del_init(&proc->deferred_work_node);
5005 defer = proc->deferred_work;
5006 proc->deferred_work = 0;
5007 } else {
5008 proc = NULL;
5009 defer = 0;
5010 }
5011 mutex_unlock(&binder_deferred_lock);
5012
5013 files = NULL;
5014 if (defer & BINDER_DEFERRED_PUT_FILES) {
Todd Kjos7f3dc002017-11-27 09:32:33 -08005015 mutex_lock(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005016 files = proc->files;
5017 if (files)
5018 proc->files = NULL;
Todd Kjos7f3dc002017-11-27 09:32:33 -08005019 mutex_unlock(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005020 }
5021
5022 if (defer & BINDER_DEFERRED_FLUSH)
5023 binder_deferred_flush(proc);
5024
5025 if (defer & BINDER_DEFERRED_RELEASE)
5026 binder_deferred_release(proc); /* frees proc */
5027
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005028 if (files)
5029 put_files_struct(files);
5030 } while (proc);
5031}
5032static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
5033
5034static void
5035binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
5036{
5037 mutex_lock(&binder_deferred_lock);
5038 proc->deferred_work |= defer;
5039 if (hlist_unhashed(&proc->deferred_work_node)) {
5040 hlist_add_head(&proc->deferred_work_node,
5041 &binder_deferred_list);
Bhaktipriya Shridhar1beba522016-08-13 22:16:24 +05305042 schedule_work(&binder_deferred_work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005043 }
5044 mutex_unlock(&binder_deferred_lock);
5045}
5046
Todd Kjos5f2f6362017-06-29 12:02:09 -07005047static void print_binder_transaction_ilocked(struct seq_file *m,
5048 struct binder_proc *proc,
5049 const char *prefix,
5050 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005051{
Todd Kjos5f2f6362017-06-29 12:02:09 -07005052 struct binder_proc *to_proc;
5053 struct binder_buffer *buffer = t->buffer;
5054
Todd Kjos7a4408c2017-06-29 12:01:57 -07005055 spin_lock(&t->lock);
Todd Kjos5f2f6362017-06-29 12:02:09 -07005056 to_proc = t->to_proc;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005057 seq_printf(m,
Todd Kjos8ca86f12018-02-07 13:57:37 -08005058 "%s %d: %pK from %d:%d to %d:%d code %x flags %x pri %ld r%d",
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005059 prefix, t->debug_id, t,
5060 t->from ? t->from->proc->pid : 0,
5061 t->from ? t->from->pid : 0,
Todd Kjos5f2f6362017-06-29 12:02:09 -07005062 to_proc ? to_proc->pid : 0,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005063 t->to_thread ? t->to_thread->pid : 0,
5064 t->code, t->flags, t->priority, t->need_reply);
Todd Kjos7a4408c2017-06-29 12:01:57 -07005065 spin_unlock(&t->lock);
5066
Todd Kjos5f2f6362017-06-29 12:02:09 -07005067 if (proc != to_proc) {
5068 /*
5069 * Can only safely deref buffer if we are holding the
5070 * correct proc inner lock for this node
5071 */
5072 seq_puts(m, "\n");
5073 return;
5074 }
5075
5076 if (buffer == NULL) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005077 seq_puts(m, " buffer free\n");
5078 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005079 }
Todd Kjos5f2f6362017-06-29 12:02:09 -07005080 if (buffer->target_node)
5081 seq_printf(m, " node %d", buffer->target_node->debug_id);
Todd Kjos8ca86f12018-02-07 13:57:37 -08005082 seq_printf(m, " size %zd:%zd data %pK\n",
Todd Kjos5f2f6362017-06-29 12:02:09 -07005083 buffer->data_size, buffer->offsets_size,
5084 buffer->data);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005085}
5086
Todd Kjos5f2f6362017-06-29 12:02:09 -07005087static void print_binder_work_ilocked(struct seq_file *m,
5088 struct binder_proc *proc,
5089 const char *prefix,
5090 const char *transaction_prefix,
5091 struct binder_work *w)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005092{
5093 struct binder_node *node;
5094 struct binder_transaction *t;
5095
5096 switch (w->type) {
5097 case BINDER_WORK_TRANSACTION:
5098 t = container_of(w, struct binder_transaction, work);
Todd Kjos5f2f6362017-06-29 12:02:09 -07005099 print_binder_transaction_ilocked(
5100 m, proc, transaction_prefix, t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005101 break;
Todd Kjos26549d12017-06-29 12:01:55 -07005102 case BINDER_WORK_RETURN_ERROR: {
5103 struct binder_error *e = container_of(
5104 w, struct binder_error, work);
5105
5106 seq_printf(m, "%stransaction error: %u\n",
5107 prefix, e->cmd);
5108 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005109 case BINDER_WORK_TRANSACTION_COMPLETE:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005110 seq_printf(m, "%stransaction complete\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005111 break;
5112 case BINDER_WORK_NODE:
5113 node = container_of(w, struct binder_node, work);
Arve Hjønnevågda498892014-02-21 14:40:26 -08005114 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
5115 prefix, node->debug_id,
5116 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005117 break;
5118 case BINDER_WORK_DEAD_BINDER:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005119 seq_printf(m, "%shas dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005120 break;
5121 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005122 seq_printf(m, "%shas cleared dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005123 break;
5124 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005125 seq_printf(m, "%shas cleared death notification\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005126 break;
5127 default:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005128 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005129 break;
5130 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005131}
5132
Todd Kjos72196392017-06-29 12:02:02 -07005133static void print_binder_thread_ilocked(struct seq_file *m,
5134 struct binder_thread *thread,
5135 int print_always)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005136{
5137 struct binder_transaction *t;
5138 struct binder_work *w;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005139 size_t start_pos = m->count;
5140 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005141
Todd Kjos7a4408c2017-06-29 12:01:57 -07005142 seq_printf(m, " thread %d: l %02x need_return %d tr %d\n",
Todd Kjos08dabce2017-06-29 12:01:49 -07005143 thread->pid, thread->looper,
Todd Kjos7a4408c2017-06-29 12:01:57 -07005144 thread->looper_need_return,
5145 atomic_read(&thread->tmp_ref));
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005146 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005147 t = thread->transaction_stack;
5148 while (t) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005149 if (t->from == thread) {
Todd Kjos5f2f6362017-06-29 12:02:09 -07005150 print_binder_transaction_ilocked(m, thread->proc,
5151 " outgoing transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005152 t = t->from_parent;
5153 } else if (t->to_thread == thread) {
Todd Kjos5f2f6362017-06-29 12:02:09 -07005154 print_binder_transaction_ilocked(m, thread->proc,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005155 " incoming transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005156 t = t->to_parent;
5157 } else {
Todd Kjos5f2f6362017-06-29 12:02:09 -07005158 print_binder_transaction_ilocked(m, thread->proc,
5159 " bad transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005160 t = NULL;
5161 }
5162 }
5163 list_for_each_entry(w, &thread->todo, entry) {
Todd Kjos5f2f6362017-06-29 12:02:09 -07005164 print_binder_work_ilocked(m, thread->proc, " ",
Todd Kjos72196392017-06-29 12:02:02 -07005165 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005166 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005167 if (!print_always && m->count == header_pos)
5168 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005169}
5170
Todd Kjosda0fa9e2017-06-29 12:02:04 -07005171static void print_binder_node_nilocked(struct seq_file *m,
5172 struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005173{
5174 struct binder_ref *ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005175 struct binder_work *w;
5176 int count;
5177
5178 count = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08005179 hlist_for_each_entry(ref, &node->refs, node_entry)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005180 count++;
5181
Todd Kjosadc18842017-06-29 12:01:59 -07005182 seq_printf(m, " node %d: u%016llx c%016llx hs %d hw %d ls %d lw %d is %d iw %d tr %d",
Arve Hjønnevågda498892014-02-21 14:40:26 -08005183 node->debug_id, (u64)node->ptr, (u64)node->cookie,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005184 node->has_strong_ref, node->has_weak_ref,
5185 node->local_strong_refs, node->local_weak_refs,
Todd Kjosadc18842017-06-29 12:01:59 -07005186 node->internal_strong_refs, count, node->tmp_refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005187 if (count) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005188 seq_puts(m, " proc");
Sasha Levinb67bfe02013-02-27 17:06:00 -08005189 hlist_for_each_entry(ref, &node->refs, node_entry)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005190 seq_printf(m, " %d", ref->proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005191 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005192 seq_puts(m, "\n");
Todd Kjos72196392017-06-29 12:02:02 -07005193 if (node->proc) {
Todd Kjos72196392017-06-29 12:02:02 -07005194 list_for_each_entry(w, &node->async_todo, entry)
Todd Kjos5f2f6362017-06-29 12:02:09 -07005195 print_binder_work_ilocked(m, node->proc, " ",
Todd Kjos72196392017-06-29 12:02:02 -07005196 " pending async transaction", w);
Todd Kjos72196392017-06-29 12:02:02 -07005197 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005198}
5199
Todd Kjos2c1838d2017-06-29 12:02:08 -07005200static void print_binder_ref_olocked(struct seq_file *m,
5201 struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005202{
Todd Kjos673068e2017-06-29 12:02:03 -07005203 binder_node_lock(ref->node);
Todd Kjos372e3142017-06-29 12:01:58 -07005204 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
5205 ref->data.debug_id, ref->data.desc,
5206 ref->node->proc ? "" : "dead ",
5207 ref->node->debug_id, ref->data.strong,
5208 ref->data.weak, ref->death);
Todd Kjos673068e2017-06-29 12:02:03 -07005209 binder_node_unlock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005210}
5211
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005212static void print_binder_proc(struct seq_file *m,
5213 struct binder_proc *proc, int print_all)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005214{
5215 struct binder_work *w;
5216 struct rb_node *n;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005217 size_t start_pos = m->count;
5218 size_t header_pos;
Todd Kjosda0fa9e2017-06-29 12:02:04 -07005219 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005220
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005221 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen14db3182017-02-03 14:40:47 -08005222 seq_printf(m, "context %s\n", proc->context->name);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005223 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005224
Todd Kjos72196392017-06-29 12:02:02 -07005225 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005226 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
Todd Kjos72196392017-06-29 12:02:02 -07005227 print_binder_thread_ilocked(m, rb_entry(n, struct binder_thread,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005228 rb_node), print_all);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07005229
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005230 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005231 struct binder_node *node = rb_entry(n, struct binder_node,
5232 rb_node);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07005233 /*
5234 * take a temporary reference on the node so it
5235 * survives and isn't removed from the tree
5236 * while we print it.
5237 */
5238 binder_inc_node_tmpref_ilocked(node);
5239 /* Need to drop inner lock to take node lock */
5240 binder_inner_proc_unlock(proc);
5241 if (last_node)
5242 binder_put_node(last_node);
5243 binder_node_inner_lock(node);
5244 print_binder_node_nilocked(m, node);
5245 binder_node_inner_unlock(node);
5246 last_node = node;
5247 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005248 }
Todd Kjosda0fa9e2017-06-29 12:02:04 -07005249 binder_inner_proc_unlock(proc);
5250 if (last_node)
5251 binder_put_node(last_node);
5252
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005253 if (print_all) {
Todd Kjos2c1838d2017-06-29 12:02:08 -07005254 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005255 for (n = rb_first(&proc->refs_by_desc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005256 n != NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005257 n = rb_next(n))
Todd Kjos2c1838d2017-06-29 12:02:08 -07005258 print_binder_ref_olocked(m, rb_entry(n,
5259 struct binder_ref,
5260 rb_node_desc));
5261 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005262 }
Todd Kjos19c98722017-06-29 12:01:40 -07005263 binder_alloc_print_allocated(m, &proc->alloc);
Todd Kjos72196392017-06-29 12:02:02 -07005264 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005265 list_for_each_entry(w, &proc->todo, entry)
Todd Kjos5f2f6362017-06-29 12:02:09 -07005266 print_binder_work_ilocked(m, proc, " ",
5267 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005268 list_for_each_entry(w, &proc->delivered_death, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005269 seq_puts(m, " has delivered dead binder\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005270 break;
5271 }
Todd Kjos72196392017-06-29 12:02:02 -07005272 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005273 if (!print_all && m->count == header_pos)
5274 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005275}
5276
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005277static const char * const binder_return_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005278 "BR_ERROR",
5279 "BR_OK",
5280 "BR_TRANSACTION",
5281 "BR_REPLY",
5282 "BR_ACQUIRE_RESULT",
5283 "BR_DEAD_REPLY",
5284 "BR_TRANSACTION_COMPLETE",
5285 "BR_INCREFS",
5286 "BR_ACQUIRE",
5287 "BR_RELEASE",
5288 "BR_DECREFS",
5289 "BR_ATTEMPT_ACQUIRE",
5290 "BR_NOOP",
5291 "BR_SPAWN_LOOPER",
5292 "BR_FINISHED",
5293 "BR_DEAD_BINDER",
5294 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
5295 "BR_FAILED_REPLY"
5296};
5297
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005298static const char * const binder_command_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005299 "BC_TRANSACTION",
5300 "BC_REPLY",
5301 "BC_ACQUIRE_RESULT",
5302 "BC_FREE_BUFFER",
5303 "BC_INCREFS",
5304 "BC_ACQUIRE",
5305 "BC_RELEASE",
5306 "BC_DECREFS",
5307 "BC_INCREFS_DONE",
5308 "BC_ACQUIRE_DONE",
5309 "BC_ATTEMPT_ACQUIRE",
5310 "BC_REGISTER_LOOPER",
5311 "BC_ENTER_LOOPER",
5312 "BC_EXIT_LOOPER",
5313 "BC_REQUEST_DEATH_NOTIFICATION",
5314 "BC_CLEAR_DEATH_NOTIFICATION",
Martijn Coenen79802402017-02-03 14:40:51 -08005315 "BC_DEAD_BINDER_DONE",
5316 "BC_TRANSACTION_SG",
5317 "BC_REPLY_SG",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005318};
5319
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005320static const char * const binder_objstat_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005321 "proc",
5322 "thread",
5323 "node",
5324 "ref",
5325 "death",
5326 "transaction",
5327 "transaction_complete"
5328};
5329
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005330static void print_binder_stats(struct seq_file *m, const char *prefix,
5331 struct binder_stats *stats)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005332{
5333 int i;
5334
5335 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005336 ARRAY_SIZE(binder_command_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005337 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07005338 int temp = atomic_read(&stats->bc[i]);
5339
5340 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005341 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07005342 binder_command_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005343 }
5344
5345 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005346 ARRAY_SIZE(binder_return_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005347 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07005348 int temp = atomic_read(&stats->br[i]);
5349
5350 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005351 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07005352 binder_return_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005353 }
5354
5355 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005356 ARRAY_SIZE(binder_objstat_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005357 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005358 ARRAY_SIZE(stats->obj_deleted));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005359 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07005360 int created = atomic_read(&stats->obj_created[i]);
5361 int deleted = atomic_read(&stats->obj_deleted[i]);
5362
5363 if (created || deleted)
5364 seq_printf(m, "%s%s: active %d total %d\n",
5365 prefix,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005366 binder_objstat_strings[i],
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07005367 created - deleted,
5368 created);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005369 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005370}
5371
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005372static void print_binder_proc_stats(struct seq_file *m,
5373 struct binder_proc *proc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005374{
5375 struct binder_work *w;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02005376 struct binder_thread *thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005377 struct rb_node *n;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02005378 int count, strong, weak, ready_threads;
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07005379 size_t free_async_space =
5380 binder_alloc_get_free_async_space(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005381
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005382 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen14db3182017-02-03 14:40:47 -08005383 seq_printf(m, "context %s\n", proc->context->name);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005384 count = 0;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02005385 ready_threads = 0;
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07005386 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005387 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
5388 count++;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02005389
5390 list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node)
5391 ready_threads++;
5392
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005393 seq_printf(m, " threads: %d\n", count);
5394 seq_printf(m, " requested threads: %d+%d/%d\n"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005395 " ready threads %d\n"
5396 " free async space %zd\n", proc->requested_threads,
5397 proc->requested_threads_started, proc->max_threads,
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02005398 ready_threads,
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07005399 free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005400 count = 0;
5401 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
5402 count++;
Todd Kjosda0fa9e2017-06-29 12:02:04 -07005403 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005404 seq_printf(m, " nodes: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005405 count = 0;
5406 strong = 0;
5407 weak = 0;
Todd Kjos2c1838d2017-06-29 12:02:08 -07005408 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005409 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
5410 struct binder_ref *ref = rb_entry(n, struct binder_ref,
5411 rb_node_desc);
5412 count++;
Todd Kjos372e3142017-06-29 12:01:58 -07005413 strong += ref->data.strong;
5414 weak += ref->data.weak;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005415 }
Todd Kjos2c1838d2017-06-29 12:02:08 -07005416 binder_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005417 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005418
Todd Kjos19c98722017-06-29 12:01:40 -07005419 count = binder_alloc_get_allocated_count(&proc->alloc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005420 seq_printf(m, " buffers: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005421
Sherry Yang8ef46652017-08-31 11:56:36 -07005422 binder_alloc_print_pages(m, &proc->alloc);
5423
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005424 count = 0;
Todd Kjos72196392017-06-29 12:02:02 -07005425 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005426 list_for_each_entry(w, &proc->todo, entry) {
Todd Kjos72196392017-06-29 12:02:02 -07005427 if (w->type == BINDER_WORK_TRANSACTION)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005428 count++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005429 }
Todd Kjos72196392017-06-29 12:02:02 -07005430 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005431 seq_printf(m, " pending transactions: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005432
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005433 print_binder_stats(m, " ", &proc->stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005434}
5435
5436
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005437static int binder_state_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005438{
5439 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005440 struct binder_node *node;
Todd Kjos673068e2017-06-29 12:02:03 -07005441 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005442
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005443 seq_puts(m, "binder state:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005444
Todd Kjosc44b1232017-06-29 12:01:43 -07005445 spin_lock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005446 if (!hlist_empty(&binder_dead_nodes))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005447 seq_puts(m, "dead nodes:\n");
Todd Kjos673068e2017-06-29 12:02:03 -07005448 hlist_for_each_entry(node, &binder_dead_nodes, dead_node) {
5449 /*
5450 * take a temporary reference on the node so it
5451 * survives and isn't removed from the list
5452 * while we print it.
5453 */
5454 node->tmp_refs++;
5455 spin_unlock(&binder_dead_nodes_lock);
5456 if (last_node)
5457 binder_put_node(last_node);
5458 binder_node_lock(node);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07005459 print_binder_node_nilocked(m, node);
Todd Kjos673068e2017-06-29 12:02:03 -07005460 binder_node_unlock(node);
5461 last_node = node;
5462 spin_lock(&binder_dead_nodes_lock);
5463 }
Todd Kjosc44b1232017-06-29 12:01:43 -07005464 spin_unlock(&binder_dead_nodes_lock);
Todd Kjos673068e2017-06-29 12:02:03 -07005465 if (last_node)
5466 binder_put_node(last_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005467
Todd Kjosc44b1232017-06-29 12:01:43 -07005468 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005469 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005470 print_binder_proc(m, proc, 1);
Todd Kjosc44b1232017-06-29 12:01:43 -07005471 mutex_unlock(&binder_procs_lock);
Todd Kjosa60b8902017-06-29 12:02:11 -07005472
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005473 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005474}
5475
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005476static int binder_stats_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005477{
5478 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005479
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005480 seq_puts(m, "binder stats:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005481
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005482 print_binder_stats(m, "", &binder_stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005483
Todd Kjosc44b1232017-06-29 12:01:43 -07005484 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005485 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005486 print_binder_proc_stats(m, proc);
Todd Kjosc44b1232017-06-29 12:01:43 -07005487 mutex_unlock(&binder_procs_lock);
Todd Kjosa60b8902017-06-29 12:02:11 -07005488
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005489 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005490}
5491
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005492static int binder_transactions_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005493{
5494 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005495
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005496 seq_puts(m, "binder transactions:\n");
Todd Kjosc44b1232017-06-29 12:01:43 -07005497 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005498 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005499 print_binder_proc(m, proc, 0);
Todd Kjosc44b1232017-06-29 12:01:43 -07005500 mutex_unlock(&binder_procs_lock);
Todd Kjosa60b8902017-06-29 12:02:11 -07005501
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005502 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005503}
5504
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005505static int binder_proc_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005506{
Riley Andrews83050a42016-02-09 21:05:33 -08005507 struct binder_proc *itr;
Martijn Coenen14db3182017-02-03 14:40:47 -08005508 int pid = (unsigned long)m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005509
Todd Kjosc44b1232017-06-29 12:01:43 -07005510 mutex_lock(&binder_procs_lock);
Riley Andrews83050a42016-02-09 21:05:33 -08005511 hlist_for_each_entry(itr, &binder_procs, proc_node) {
Martijn Coenen14db3182017-02-03 14:40:47 -08005512 if (itr->pid == pid) {
5513 seq_puts(m, "binder proc state:\n");
5514 print_binder_proc(m, itr, 1);
Riley Andrews83050a42016-02-09 21:05:33 -08005515 }
5516 }
Todd Kjosc44b1232017-06-29 12:01:43 -07005517 mutex_unlock(&binder_procs_lock);
5518
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005519 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005520}
5521
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005522static void print_binder_transaction_log_entry(struct seq_file *m,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005523 struct binder_transaction_log_entry *e)
5524{
Todd Kjosd99c7332017-06-29 12:01:53 -07005525 int debug_id = READ_ONCE(e->debug_id_done);
5526 /*
5527 * read barrier to guarantee debug_id_done read before
5528 * we print the log values
5529 */
5530 smp_rmb();
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005531 seq_printf(m,
Todd Kjosd99c7332017-06-29 12:01:53 -07005532 "%d: %s from %d:%d to %d:%d context %s node %d handle %d size %d:%d ret %d/%d l=%d",
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005533 e->debug_id, (e->call_type == 2) ? "reply" :
5534 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
Martijn Coenen14db3182017-02-03 14:40:47 -08005535 e->from_thread, e->to_proc, e->to_thread, e->context_name,
Todd Kjos57ada2f2017-06-29 12:01:46 -07005536 e->to_node, e->target_handle, e->data_size, e->offsets_size,
5537 e->return_error, e->return_error_param,
5538 e->return_error_line);
Todd Kjosd99c7332017-06-29 12:01:53 -07005539 /*
5540 * read-barrier to guarantee read of debug_id_done after
5541 * done printing the fields of the entry
5542 */
5543 smp_rmb();
5544 seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ?
5545 "\n" : " (incomplete)\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005546}
5547
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005548static int binder_transaction_log_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005549{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005550 struct binder_transaction_log *log = m->private;
Todd Kjosd99c7332017-06-29 12:01:53 -07005551 unsigned int log_cur = atomic_read(&log->cur);
5552 unsigned int count;
5553 unsigned int cur;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005554 int i;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005555
Todd Kjosd99c7332017-06-29 12:01:53 -07005556 count = log_cur + 1;
5557 cur = count < ARRAY_SIZE(log->entry) && !log->full ?
5558 0 : count % ARRAY_SIZE(log->entry);
5559 if (count > ARRAY_SIZE(log->entry) || log->full)
5560 count = ARRAY_SIZE(log->entry);
5561 for (i = 0; i < count; i++) {
5562 unsigned int index = cur++ % ARRAY_SIZE(log->entry);
5563
5564 print_binder_transaction_log_entry(m, &log->entry[index]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005565 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005566 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005567}
5568
5569static const struct file_operations binder_fops = {
5570 .owner = THIS_MODULE,
5571 .poll = binder_poll,
5572 .unlocked_ioctl = binder_ioctl,
Arve Hjønnevågda498892014-02-21 14:40:26 -08005573 .compat_ioctl = binder_ioctl,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005574 .mmap = binder_mmap,
5575 .open = binder_open,
5576 .flush = binder_flush,
5577 .release = binder_release,
5578};
5579
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005580BINDER_DEBUG_ENTRY(state);
5581BINDER_DEBUG_ENTRY(stats);
5582BINDER_DEBUG_ENTRY(transactions);
5583BINDER_DEBUG_ENTRY(transaction_log);
5584
Martijn Coenenac4812c2017-02-03 14:40:48 -08005585static int __init init_binder_device(const char *name)
5586{
5587 int ret;
5588 struct binder_device *binder_device;
5589
5590 binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
5591 if (!binder_device)
5592 return -ENOMEM;
5593
5594 binder_device->miscdev.fops = &binder_fops;
5595 binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
5596 binder_device->miscdev.name = name;
5597
5598 binder_device->context.binder_context_mgr_uid = INVALID_UID;
5599 binder_device->context.name = name;
Todd Kjosc44b1232017-06-29 12:01:43 -07005600 mutex_init(&binder_device->context.context_mgr_node_lock);
Martijn Coenenac4812c2017-02-03 14:40:48 -08005601
5602 ret = misc_register(&binder_device->miscdev);
5603 if (ret < 0) {
5604 kfree(binder_device);
5605 return ret;
5606 }
5607
5608 hlist_add_head(&binder_device->hlist, &binder_devices);
5609
5610 return ret;
5611}
5612
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005613static int __init binder_init(void)
5614{
5615 int ret;
Christian Brauner22eb9472017-08-21 16:13:28 +02005616 char *device_name, *device_names, *device_tmp;
Martijn Coenenac4812c2017-02-03 14:40:48 -08005617 struct binder_device *device;
5618 struct hlist_node *tmp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005619
Tetsuo Handa533dfb22017-11-29 22:29:47 +09005620 ret = binder_alloc_shrinker_init();
5621 if (ret)
5622 return ret;
Sherry Yangf2517eb2017-08-23 08:46:42 -07005623
Todd Kjosd99c7332017-06-29 12:01:53 -07005624 atomic_set(&binder_transaction_log.cur, ~0U);
5625 atomic_set(&binder_transaction_log_failed.cur, ~0U);
5626
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005627 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
5628 if (binder_debugfs_dir_entry_root)
5629 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
5630 binder_debugfs_dir_entry_root);
Martijn Coenenac4812c2017-02-03 14:40:48 -08005631
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005632 if (binder_debugfs_dir_entry_root) {
5633 debugfs_create_file("state",
Harsh Shandilya21d02dd2017-12-22 19:37:02 +05305634 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005635 binder_debugfs_dir_entry_root,
5636 NULL,
5637 &binder_state_fops);
5638 debugfs_create_file("stats",
Harsh Shandilya21d02dd2017-12-22 19:37:02 +05305639 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005640 binder_debugfs_dir_entry_root,
5641 NULL,
5642 &binder_stats_fops);
5643 debugfs_create_file("transactions",
Harsh Shandilya21d02dd2017-12-22 19:37:02 +05305644 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005645 binder_debugfs_dir_entry_root,
5646 NULL,
5647 &binder_transactions_fops);
5648 debugfs_create_file("transaction_log",
Harsh Shandilya21d02dd2017-12-22 19:37:02 +05305649 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005650 binder_debugfs_dir_entry_root,
5651 &binder_transaction_log,
5652 &binder_transaction_log_fops);
5653 debugfs_create_file("failed_transaction_log",
Harsh Shandilya21d02dd2017-12-22 19:37:02 +05305654 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005655 binder_debugfs_dir_entry_root,
5656 &binder_transaction_log_failed,
5657 &binder_transaction_log_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005658 }
Martijn Coenenac4812c2017-02-03 14:40:48 -08005659
5660 /*
5661 * Copy the module_parameter string, because we don't want to
5662 * tokenize it in-place.
5663 */
5664 device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL);
5665 if (!device_names) {
5666 ret = -ENOMEM;
5667 goto err_alloc_device_names_failed;
5668 }
5669 strcpy(device_names, binder_devices_param);
5670
Christian Brauner22eb9472017-08-21 16:13:28 +02005671 device_tmp = device_names;
5672 while ((device_name = strsep(&device_tmp, ","))) {
Martijn Coenenac4812c2017-02-03 14:40:48 -08005673 ret = init_binder_device(device_name);
5674 if (ret)
5675 goto err_init_binder_device_failed;
5676 }
5677
5678 return ret;
5679
5680err_init_binder_device_failed:
5681 hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
5682 misc_deregister(&device->miscdev);
5683 hlist_del(&device->hlist);
5684 kfree(device);
5685 }
Christian Brauner22eb9472017-08-21 16:13:28 +02005686
5687 kfree(device_names);
5688
Martijn Coenenac4812c2017-02-03 14:40:48 -08005689err_alloc_device_names_failed:
5690 debugfs_remove_recursive(binder_debugfs_dir_entry_root);
5691
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005692 return ret;
5693}
5694
5695device_initcall(binder_init);
5696
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07005697#define CREATE_TRACE_POINTS
5698#include "binder_trace.h"
5699
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005700MODULE_LICENSE("GPL v2");