blob: f2081934eb8bab1ab682dbcfff7ae1a789d8f3ab [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 <linux/fdtable.h>
55#include <linux/file.h>
Colin Crosse2610b22013-05-06 23:50:15 +000056#include <linux/freezer.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090057#include <linux/fs.h>
58#include <linux/list.h>
59#include <linux/miscdevice.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090060#include <linux/module.h>
61#include <linux/mutex.h>
62#include <linux/nsproxy.h>
63#include <linux/poll.h>
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070064#include <linux/debugfs.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090065#include <linux/rbtree.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010066#include <linux/sched/signal.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010067#include <linux/sched/mm.h>
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070068#include <linux/seq_file.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090069#include <linux/uaccess.h>
Eric W. Biederman17cf22c2010-03-02 14:51:53 -080070#include <linux/pid_namespace.h>
Stephen Smalley79af7302015-01-21 10:54:10 -050071#include <linux/security.h>
Todd Kjos9630fe82017-06-29 12:02:00 -070072#include <linux/spinlock.h>
Sherry Yang128f3802018-08-07 12:57:13 -070073#include <linux/ratelimit.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>
Guenter Roeckf371a7c2018-07-23 14:41:38 -070076
77#include <asm/cacheflush.h>
78
Todd Kjos0c972a02017-06-29 12:01:41 -070079#include "binder_alloc.h"
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -070080#include "binder_trace.h"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090081
Todd Kjosc44b1232017-06-29 12:01:43 -070082static HLIST_HEAD(binder_deferred_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090083static DEFINE_MUTEX(binder_deferred_lock);
84
Martijn Coenenac4812c2017-02-03 14:40:48 -080085static HLIST_HEAD(binder_devices);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090086static HLIST_HEAD(binder_procs);
Todd Kjosc44b1232017-06-29 12:01:43 -070087static DEFINE_MUTEX(binder_procs_lock);
88
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090089static HLIST_HEAD(binder_dead_nodes);
Todd Kjosc44b1232017-06-29 12:01:43 -070090static DEFINE_SPINLOCK(binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090091
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070092static struct dentry *binder_debugfs_dir_entry_root;
93static struct dentry *binder_debugfs_dir_entry_proc;
Todd Kjos656a8002017-06-29 12:01:45 -070094static atomic_t binder_last_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090095
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070096#define BINDER_DEBUG_ENTRY(name) \
97static int binder_##name##_open(struct inode *inode, struct file *file) \
98{ \
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070099 return single_open(file, binder_##name##_show, inode->i_private); \
Arve Hjønnevåg5249f482009-04-28 20:57:50 -0700100} \
101\
102static const struct file_operations binder_##name##_fops = { \
103 .owner = THIS_MODULE, \
104 .open = binder_##name##_open, \
105 .read = seq_read, \
106 .llseek = seq_lseek, \
107 .release = single_release, \
108}
109
110static int binder_proc_show(struct seq_file *m, void *unused);
111BINDER_DEBUG_ENTRY(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900112
113/* This is only defined in include/asm-arm/sizes.h */
114#ifndef SZ_1K
115#define SZ_1K 0x400
116#endif
117
118#ifndef SZ_4M
119#define SZ_4M 0x400000
120#endif
121
122#define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
123
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900124enum {
125 BINDER_DEBUG_USER_ERROR = 1U << 0,
126 BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1,
127 BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2,
128 BINDER_DEBUG_OPEN_CLOSE = 1U << 3,
129 BINDER_DEBUG_DEAD_BINDER = 1U << 4,
130 BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5,
131 BINDER_DEBUG_READ_WRITE = 1U << 6,
132 BINDER_DEBUG_USER_REFS = 1U << 7,
133 BINDER_DEBUG_THREADS = 1U << 8,
134 BINDER_DEBUG_TRANSACTION = 1U << 9,
135 BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10,
136 BINDER_DEBUG_FREE_BUFFER = 1U << 11,
137 BINDER_DEBUG_INTERNAL_REFS = 1U << 12,
Todd Kjos19c98722017-06-29 12:01:40 -0700138 BINDER_DEBUG_PRIORITY_CAP = 1U << 13,
Todd Kjos9630fe82017-06-29 12:02:00 -0700139 BINDER_DEBUG_SPINLOCKS = 1U << 14,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900140};
141static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR |
142 BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION;
Harsh Shandilya21d02dd2017-12-22 19:37:02 +0530143module_param_named(debug_mask, binder_debug_mask, uint, 0644);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900144
Martijn Coenenac4812c2017-02-03 14:40:48 -0800145static char *binder_devices_param = CONFIG_ANDROID_BINDER_DEVICES;
146module_param_named(devices, binder_devices_param, charp, 0444);
147
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900148static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
149static int binder_stop_on_user_error;
150
151static int binder_set_stop_on_user_error(const char *val,
Kees Cooke4dca7b2017-10-17 19:04:42 -0700152 const struct kernel_param *kp)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900153{
154 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +0900155
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900156 ret = param_set_int(val, kp);
157 if (binder_stop_on_user_error < 2)
158 wake_up(&binder_user_error_wait);
159 return ret;
160}
161module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
Harsh Shandilya21d02dd2017-12-22 19:37:02 +0530162 param_get_int, &binder_stop_on_user_error, 0644);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900163
164#define binder_debug(mask, x...) \
165 do { \
166 if (binder_debug_mask & mask) \
Sherry Yang128f3802018-08-07 12:57:13 -0700167 pr_info_ratelimited(x); \
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900168 } while (0)
169
170#define binder_user_error(x...) \
171 do { \
172 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
Sherry Yang128f3802018-08-07 12:57:13 -0700173 pr_info_ratelimited(x); \
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900174 if (binder_stop_on_user_error) \
175 binder_stop_on_user_error = 2; \
176 } while (0)
177
Martijn Coenenfeba3902017-02-03 14:40:45 -0800178#define to_flat_binder_object(hdr) \
179 container_of(hdr, struct flat_binder_object, hdr)
180
181#define to_binder_fd_object(hdr) container_of(hdr, struct binder_fd_object, hdr)
182
Martijn Coenen79802402017-02-03 14:40:51 -0800183#define to_binder_buffer_object(hdr) \
184 container_of(hdr, struct binder_buffer_object, hdr)
185
Martijn Coenendef95c72017-02-03 14:40:52 -0800186#define to_binder_fd_array_object(hdr) \
187 container_of(hdr, struct binder_fd_array_object, hdr)
188
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900189enum binder_stat_types {
190 BINDER_STAT_PROC,
191 BINDER_STAT_THREAD,
192 BINDER_STAT_NODE,
193 BINDER_STAT_REF,
194 BINDER_STAT_DEATH,
195 BINDER_STAT_TRANSACTION,
196 BINDER_STAT_TRANSACTION_COMPLETE,
197 BINDER_STAT_COUNT
198};
199
200struct binder_stats {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -0700201 atomic_t br[_IOC_NR(BR_FAILED_REPLY) + 1];
202 atomic_t bc[_IOC_NR(BC_REPLY_SG) + 1];
203 atomic_t obj_created[BINDER_STAT_COUNT];
204 atomic_t obj_deleted[BINDER_STAT_COUNT];
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900205};
206
207static struct binder_stats binder_stats;
208
209static inline void binder_stats_deleted(enum binder_stat_types type)
210{
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -0700211 atomic_inc(&binder_stats.obj_deleted[type]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900212}
213
214static inline void binder_stats_created(enum binder_stat_types type)
215{
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -0700216 atomic_inc(&binder_stats.obj_created[type]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900217}
218
219struct binder_transaction_log_entry {
220 int debug_id;
Todd Kjosd99c7332017-06-29 12:01:53 -0700221 int debug_id_done;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900222 int call_type;
223 int from_proc;
224 int from_thread;
225 int target_handle;
226 int to_proc;
227 int to_thread;
228 int to_node;
229 int data_size;
230 int offsets_size;
Todd Kjos57ada2f2017-06-29 12:01:46 -0700231 int return_error_line;
232 uint32_t return_error;
233 uint32_t return_error_param;
Martijn Coenen14db3182017-02-03 14:40:47 -0800234 const char *context_name;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900235};
236struct binder_transaction_log {
Todd Kjosd99c7332017-06-29 12:01:53 -0700237 atomic_t cur;
238 bool full;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900239 struct binder_transaction_log_entry entry[32];
240};
241static struct binder_transaction_log binder_transaction_log;
242static struct binder_transaction_log binder_transaction_log_failed;
243
244static struct binder_transaction_log_entry *binder_transaction_log_add(
245 struct binder_transaction_log *log)
246{
247 struct binder_transaction_log_entry *e;
Todd Kjosd99c7332017-06-29 12:01:53 -0700248 unsigned int cur = atomic_inc_return(&log->cur);
Seunghun Lee10f62862014-05-01 01:30:23 +0900249
Todd Kjosd99c7332017-06-29 12:01:53 -0700250 if (cur >= ARRAY_SIZE(log->entry))
Gustavo A. R. Silva197410a2018-01-23 12:04:27 -0600251 log->full = true;
Todd Kjosd99c7332017-06-29 12:01:53 -0700252 e = &log->entry[cur % ARRAY_SIZE(log->entry)];
253 WRITE_ONCE(e->debug_id_done, 0);
254 /*
255 * write-barrier to synchronize access to e->debug_id_done.
256 * We make sure the initialized 0 value is seen before
257 * memset() other fields are zeroed by memset.
258 */
259 smp_wmb();
260 memset(e, 0, sizeof(*e));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900261 return e;
262}
263
Martijn Coenen342e5c92017-02-03 14:40:46 -0800264struct binder_context {
265 struct binder_node *binder_context_mgr_node;
Todd Kjosc44b1232017-06-29 12:01:43 -0700266 struct mutex context_mgr_node_lock;
267
Martijn Coenen342e5c92017-02-03 14:40:46 -0800268 kuid_t binder_context_mgr_uid;
Martijn Coenen14db3182017-02-03 14:40:47 -0800269 const char *name;
Martijn Coenen342e5c92017-02-03 14:40:46 -0800270};
271
Martijn Coenenac4812c2017-02-03 14:40:48 -0800272struct binder_device {
273 struct hlist_node hlist;
274 struct miscdevice miscdev;
275 struct binder_context context;
Martijn Coenen342e5c92017-02-03 14:40:46 -0800276};
277
Todd Kjos72196392017-06-29 12:02:02 -0700278/**
279 * struct binder_work - work enqueued on a worklist
280 * @entry: node enqueued on list
281 * @type: type of work to be performed
282 *
283 * There are separate work lists for proc, thread, and node (async).
284 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900285struct binder_work {
286 struct list_head entry;
Todd Kjos72196392017-06-29 12:02:02 -0700287
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900288 enum {
289 BINDER_WORK_TRANSACTION = 1,
290 BINDER_WORK_TRANSACTION_COMPLETE,
Todd Kjos26549d12017-06-29 12:01:55 -0700291 BINDER_WORK_RETURN_ERROR,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900292 BINDER_WORK_NODE,
293 BINDER_WORK_DEAD_BINDER,
294 BINDER_WORK_DEAD_BINDER_AND_CLEAR,
295 BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
296 } type;
297};
298
Todd Kjos26549d12017-06-29 12:01:55 -0700299struct binder_error {
300 struct binder_work work;
301 uint32_t cmd;
302};
303
Todd Kjos9630fe82017-06-29 12:02:00 -0700304/**
305 * struct binder_node - binder node bookkeeping
306 * @debug_id: unique ID for debugging
307 * (invariant after initialized)
308 * @lock: lock for node fields
309 * @work: worklist element for node work
Todd Kjos72196392017-06-29 12:02:02 -0700310 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700311 * @rb_node: element for proc->nodes tree
Todd Kjosda0fa9e2017-06-29 12:02:04 -0700312 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700313 * @dead_node: element for binder_dead_nodes list
314 * (protected by binder_dead_nodes_lock)
315 * @proc: binder_proc that owns this node
316 * (invariant after initialized)
317 * @refs: list of references on this node
Todd Kjos673068e2017-06-29 12:02:03 -0700318 * (protected by @lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700319 * @internal_strong_refs: used to take strong references when
320 * initiating a transaction
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_weak_refs: weak 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 * @local_strong_refs: strong user refs from local process
Todd Kjosed297212017-06-29 12:02:01 -0700327 * (protected by @proc->inner_lock if @proc
328 * and by @lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700329 * @tmp_refs: temporary kernel refs
Todd Kjosed297212017-06-29 12:02:01 -0700330 * (protected by @proc->inner_lock while @proc
331 * is valid, and by binder_dead_nodes_lock
332 * if @proc is NULL. During inc/dec and node release
333 * it is also protected by @lock to provide safety
334 * as the node dies and @proc becomes NULL)
Todd Kjos9630fe82017-06-29 12:02:00 -0700335 * @ptr: userspace pointer for node
336 * (invariant, no lock needed)
337 * @cookie: userspace cookie for node
338 * (invariant, no lock needed)
339 * @has_strong_ref: userspace notified 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 * @pending_strong_ref: userspace has acked notification of strong 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 * @has_weak_ref: userspace notified 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 * @pending_weak_ref: userspace has acked notification of weak ref
Todd Kjosed297212017-06-29 12:02:01 -0700349 * (protected by @proc->inner_lock if @proc
350 * and by @lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700351 * @has_async_transaction: async transaction to node in progress
Todd Kjos673068e2017-06-29 12:02:03 -0700352 * (protected by @lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700353 * @accept_fds: file descriptor operations supported for node
354 * (invariant after initialized)
355 * @min_priority: minimum scheduling priority
356 * (invariant after initialized)
357 * @async_todo: list of async work items
Todd Kjos72196392017-06-29 12:02:02 -0700358 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700359 *
360 * Bookkeeping structure for binder nodes.
361 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900362struct binder_node {
363 int debug_id;
Todd Kjos9630fe82017-06-29 12:02:00 -0700364 spinlock_t lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900365 struct binder_work work;
366 union {
367 struct rb_node rb_node;
368 struct hlist_node dead_node;
369 };
370 struct binder_proc *proc;
371 struct hlist_head refs;
372 int internal_strong_refs;
373 int local_weak_refs;
374 int local_strong_refs;
Todd Kjosadc18842017-06-29 12:01:59 -0700375 int tmp_refs;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800376 binder_uintptr_t ptr;
377 binder_uintptr_t cookie;
Todd Kjosed297212017-06-29 12:02:01 -0700378 struct {
379 /*
380 * bitfield elements protected by
381 * proc inner_lock
382 */
383 u8 has_strong_ref:1;
384 u8 pending_strong_ref:1;
385 u8 has_weak_ref:1;
386 u8 pending_weak_ref:1;
387 };
388 struct {
389 /*
390 * invariant after initialization
391 */
392 u8 accept_fds:1;
393 u8 min_priority;
394 };
395 bool has_async_transaction;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900396 struct list_head async_todo;
397};
398
399struct binder_ref_death {
Todd Kjos72196392017-06-29 12:02:02 -0700400 /**
401 * @work: worklist element for death notifications
402 * (protected by inner_lock of the proc that
403 * this ref belongs to)
404 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900405 struct binder_work work;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800406 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900407};
408
Todd Kjos372e3142017-06-29 12:01:58 -0700409/**
410 * struct binder_ref_data - binder_ref counts and id
411 * @debug_id: unique ID for the ref
412 * @desc: unique userspace handle for ref
413 * @strong: strong ref count (debugging only if not locked)
414 * @weak: weak ref count (debugging only if not locked)
415 *
416 * Structure to hold ref count and ref id information. Since
417 * the actual ref can only be accessed with a lock, this structure
418 * is used to return information about the ref to callers of
419 * ref inc/dec functions.
420 */
421struct binder_ref_data {
422 int debug_id;
423 uint32_t desc;
424 int strong;
425 int weak;
426};
427
428/**
429 * struct binder_ref - struct to track references on nodes
430 * @data: binder_ref_data containing id, handle, and current refcounts
431 * @rb_node_desc: node for lookup by @data.desc in proc's rb_tree
432 * @rb_node_node: node for lookup by @node in proc's rb_tree
433 * @node_entry: list entry for node->refs list in target node
Todd Kjos673068e2017-06-29 12:02:03 -0700434 * (protected by @node->lock)
Todd Kjos372e3142017-06-29 12:01:58 -0700435 * @proc: binder_proc containing ref
436 * @node: binder_node of target node. When cleaning up a
437 * ref for deletion in binder_cleanup_ref, a non-NULL
438 * @node indicates the node must be freed
439 * @death: pointer to death notification (ref_death) if requested
Martijn Coenenab51ec62017-06-29 12:02:10 -0700440 * (protected by @node->lock)
Todd Kjos372e3142017-06-29 12:01:58 -0700441 *
442 * Structure to track references from procA to target node (on procB). This
443 * structure is unsafe to access without holding @proc->outer_lock.
444 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900445struct binder_ref {
446 /* Lookups needed: */
447 /* node + proc => ref (transaction) */
448 /* desc + proc => ref (transaction, inc/dec ref) */
449 /* node => refs + procs (proc exit) */
Todd Kjos372e3142017-06-29 12:01:58 -0700450 struct binder_ref_data data;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900451 struct rb_node rb_node_desc;
452 struct rb_node rb_node_node;
453 struct hlist_node node_entry;
454 struct binder_proc *proc;
455 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900456 struct binder_ref_death *death;
457};
458
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900459enum binder_deferred_state {
460 BINDER_DEFERRED_PUT_FILES = 0x01,
461 BINDER_DEFERRED_FLUSH = 0x02,
462 BINDER_DEFERRED_RELEASE = 0x04,
463};
464
Todd Kjos9630fe82017-06-29 12:02:00 -0700465/**
466 * struct binder_proc - binder process bookkeeping
467 * @proc_node: element for binder_procs list
468 * @threads: rbtree of binder_threads in this proc
Todd Kjos7bd7b0e2017-06-29 12:02:05 -0700469 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700470 * @nodes: rbtree of binder nodes associated with
471 * this proc ordered by node->ptr
Todd Kjosda0fa9e2017-06-29 12:02:04 -0700472 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700473 * @refs_by_desc: rbtree of refs ordered by ref->desc
Todd Kjos2c1838d2017-06-29 12:02:08 -0700474 * (protected by @outer_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700475 * @refs_by_node: rbtree of refs ordered by ref->node
Todd Kjos2c1838d2017-06-29 12:02:08 -0700476 * (protected by @outer_lock)
Martijn Coenen1b77e9d2017-08-31 10:04:18 +0200477 * @waiting_threads: threads currently waiting for proc work
478 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700479 * @pid PID of group_leader of process
480 * (invariant after initialized)
481 * @tsk task_struct for group_leader of process
482 * (invariant after initialized)
483 * @files files_struct for process
Todd Kjos7f3dc002017-11-27 09:32:33 -0800484 * (protected by @files_lock)
485 * @files_lock mutex to protect @files
Todd Kjos9630fe82017-06-29 12:02:00 -0700486 * @deferred_work_node: element for binder_deferred_list
487 * (protected by binder_deferred_lock)
488 * @deferred_work: bitmap of deferred work to perform
489 * (protected by binder_deferred_lock)
490 * @is_dead: process is dead and awaiting free
491 * when outstanding transactions are cleaned up
Todd Kjos7bd7b0e2017-06-29 12:02:05 -0700492 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700493 * @todo: list of work for this process
Todd Kjos72196392017-06-29 12:02:02 -0700494 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700495 * @stats: per-process binder statistics
496 * (atomics, no lock needed)
497 * @delivered_death: list of delivered death notification
Todd Kjos72196392017-06-29 12:02:02 -0700498 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700499 * @max_threads: cap on number of binder threads
Todd Kjosb3e68612017-06-29 12:02:07 -0700500 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700501 * @requested_threads: number of binder threads requested but not
502 * yet started. In current implementation, can
503 * only be 0 or 1.
Todd Kjosb3e68612017-06-29 12:02:07 -0700504 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700505 * @requested_threads_started: number binder threads started
Todd Kjosb3e68612017-06-29 12:02:07 -0700506 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700507 * @tmp_ref: temporary reference to indicate proc is in use
Todd Kjos7bd7b0e2017-06-29 12:02:05 -0700508 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700509 * @default_priority: default scheduler priority
510 * (invariant after initialized)
511 * @debugfs_entry: debugfs node
512 * @alloc: binder allocator bookkeeping
513 * @context: binder_context for this proc
514 * (invariant after initialized)
515 * @inner_lock: can nest under outer_lock and/or node lock
516 * @outer_lock: no nesting under innor or node lock
517 * Lock order: 1) outer, 2) node, 3) inner
518 *
519 * Bookkeeping structure for binder processes
520 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900521struct binder_proc {
522 struct hlist_node proc_node;
523 struct rb_root threads;
524 struct rb_root nodes;
525 struct rb_root refs_by_desc;
526 struct rb_root refs_by_node;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +0200527 struct list_head waiting_threads;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900528 int pid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900529 struct task_struct *tsk;
530 struct files_struct *files;
Todd Kjos7f3dc002017-11-27 09:32:33 -0800531 struct mutex files_lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900532 struct hlist_node deferred_work_node;
533 int deferred_work;
Todd Kjos7a4408c2017-06-29 12:01:57 -0700534 bool is_dead;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900535
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900536 struct list_head todo;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900537 struct binder_stats stats;
538 struct list_head delivered_death;
539 int max_threads;
540 int requested_threads;
541 int requested_threads_started;
Todd Kjos7a4408c2017-06-29 12:01:57 -0700542 int tmp_ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900543 long default_priority;
Arve Hjønnevåg16b66552009-04-28 20:57:50 -0700544 struct dentry *debugfs_entry;
Todd Kjosfdfb4a92017-06-29 12:01:38 -0700545 struct binder_alloc alloc;
Martijn Coenen342e5c92017-02-03 14:40:46 -0800546 struct binder_context *context;
Todd Kjos9630fe82017-06-29 12:02:00 -0700547 spinlock_t inner_lock;
548 spinlock_t outer_lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900549};
550
551enum {
552 BINDER_LOOPER_STATE_REGISTERED = 0x01,
553 BINDER_LOOPER_STATE_ENTERED = 0x02,
554 BINDER_LOOPER_STATE_EXITED = 0x04,
555 BINDER_LOOPER_STATE_INVALID = 0x08,
556 BINDER_LOOPER_STATE_WAITING = 0x10,
Martijn Coenen1b77e9d2017-08-31 10:04:18 +0200557 BINDER_LOOPER_STATE_POLL = 0x20,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900558};
559
Todd Kjos9630fe82017-06-29 12:02:00 -0700560/**
561 * struct binder_thread - binder thread bookkeeping
562 * @proc: binder process for this thread
563 * (invariant after initialization)
564 * @rb_node: element for proc->threads rbtree
Todd Kjos7bd7b0e2017-06-29 12:02:05 -0700565 * (protected by @proc->inner_lock)
Martijn Coenen1b77e9d2017-08-31 10:04:18 +0200566 * @waiting_thread_node: element for @proc->waiting_threads list
567 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700568 * @pid: PID for this thread
569 * (invariant after initialization)
570 * @looper: bitmap of looping state
571 * (only accessed by this thread)
572 * @looper_needs_return: looping thread needs to exit driver
573 * (no lock needed)
574 * @transaction_stack: stack of in-progress transactions for this thread
Martijn Coenen0b89d692017-06-29 12:02:06 -0700575 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700576 * @todo: list of work to do for this thread
Todd Kjos72196392017-06-29 12:02:02 -0700577 * (protected by @proc->inner_lock)
Martijn Coenen148ade22017-11-15 09:21:35 +0100578 * @process_todo: whether work in @todo should be processed
579 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700580 * @return_error: transaction errors reported by this thread
581 * (only accessed by this thread)
582 * @reply_error: transaction errors reported by target thread
Martijn Coenen0b89d692017-06-29 12:02:06 -0700583 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700584 * @wait: wait queue for thread work
585 * @stats: per-thread statistics
586 * (atomics, no lock needed)
587 * @tmp_ref: temporary reference to indicate thread is in use
588 * (atomic since @proc->inner_lock cannot
589 * always be acquired)
590 * @is_dead: thread is dead and awaiting free
591 * when outstanding transactions are cleaned up
Todd Kjos7bd7b0e2017-06-29 12:02:05 -0700592 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700593 *
594 * Bookkeeping structure for binder threads.
595 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900596struct binder_thread {
597 struct binder_proc *proc;
598 struct rb_node rb_node;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +0200599 struct list_head waiting_thread_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900600 int pid;
Todd Kjos08dabce2017-06-29 12:01:49 -0700601 int looper; /* only modified by this thread */
602 bool looper_need_return; /* can be written by other thread */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900603 struct binder_transaction *transaction_stack;
604 struct list_head todo;
Martijn Coenen148ade22017-11-15 09:21:35 +0100605 bool process_todo;
Todd Kjos26549d12017-06-29 12:01:55 -0700606 struct binder_error return_error;
607 struct binder_error reply_error;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900608 wait_queue_head_t wait;
609 struct binder_stats stats;
Todd Kjos7a4408c2017-06-29 12:01:57 -0700610 atomic_t tmp_ref;
611 bool is_dead;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900612};
613
614struct binder_transaction {
615 int debug_id;
616 struct binder_work work;
617 struct binder_thread *from;
618 struct binder_transaction *from_parent;
619 struct binder_proc *to_proc;
620 struct binder_thread *to_thread;
621 struct binder_transaction *to_parent;
622 unsigned need_reply:1;
623 /* unsigned is_dead:1; */ /* not used at the moment */
624
625 struct binder_buffer *buffer;
626 unsigned int code;
627 unsigned int flags;
628 long priority;
629 long saved_priority;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -0600630 kuid_t sender_euid;
Todd Kjos7a4408c2017-06-29 12:01:57 -0700631 /**
632 * @lock: protects @from, @to_proc, and @to_thread
633 *
634 * @from, @to_proc, and @to_thread can be set to NULL
635 * during thread teardown
636 */
637 spinlock_t lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900638};
639
Todd Kjos9630fe82017-06-29 12:02:00 -0700640/**
641 * binder_proc_lock() - Acquire outer lock for given binder_proc
642 * @proc: struct binder_proc to acquire
643 *
644 * Acquires proc->outer_lock. Used to protect binder_ref
645 * structures associated with the given proc.
646 */
647#define binder_proc_lock(proc) _binder_proc_lock(proc, __LINE__)
648static void
649_binder_proc_lock(struct binder_proc *proc, int line)
650{
651 binder_debug(BINDER_DEBUG_SPINLOCKS,
652 "%s: line=%d\n", __func__, line);
653 spin_lock(&proc->outer_lock);
654}
655
656/**
657 * binder_proc_unlock() - Release spinlock for given binder_proc
658 * @proc: struct binder_proc to acquire
659 *
660 * Release lock acquired via binder_proc_lock()
661 */
662#define binder_proc_unlock(_proc) _binder_proc_unlock(_proc, __LINE__)
663static void
664_binder_proc_unlock(struct binder_proc *proc, int line)
665{
666 binder_debug(BINDER_DEBUG_SPINLOCKS,
667 "%s: line=%d\n", __func__, line);
668 spin_unlock(&proc->outer_lock);
669}
670
671/**
672 * binder_inner_proc_lock() - Acquire inner lock for given binder_proc
673 * @proc: struct binder_proc to acquire
674 *
675 * Acquires proc->inner_lock. Used to protect todo lists
676 */
677#define binder_inner_proc_lock(proc) _binder_inner_proc_lock(proc, __LINE__)
678static void
679_binder_inner_proc_lock(struct binder_proc *proc, int line)
680{
681 binder_debug(BINDER_DEBUG_SPINLOCKS,
682 "%s: line=%d\n", __func__, line);
683 spin_lock(&proc->inner_lock);
684}
685
686/**
687 * binder_inner_proc_unlock() - Release inner lock for given binder_proc
688 * @proc: struct binder_proc to acquire
689 *
690 * Release lock acquired via binder_inner_proc_lock()
691 */
692#define binder_inner_proc_unlock(proc) _binder_inner_proc_unlock(proc, __LINE__)
693static void
694_binder_inner_proc_unlock(struct binder_proc *proc, int line)
695{
696 binder_debug(BINDER_DEBUG_SPINLOCKS,
697 "%s: line=%d\n", __func__, line);
698 spin_unlock(&proc->inner_lock);
699}
700
701/**
702 * binder_node_lock() - Acquire spinlock for given binder_node
703 * @node: struct binder_node to acquire
704 *
705 * Acquires node->lock. Used to protect binder_node fields
706 */
707#define binder_node_lock(node) _binder_node_lock(node, __LINE__)
708static void
709_binder_node_lock(struct binder_node *node, int line)
710{
711 binder_debug(BINDER_DEBUG_SPINLOCKS,
712 "%s: line=%d\n", __func__, line);
713 spin_lock(&node->lock);
714}
715
716/**
717 * binder_node_unlock() - Release spinlock for given binder_proc
718 * @node: struct binder_node to acquire
719 *
720 * Release lock acquired via binder_node_lock()
721 */
722#define binder_node_unlock(node) _binder_node_unlock(node, __LINE__)
723static void
724_binder_node_unlock(struct binder_node *node, int line)
725{
726 binder_debug(BINDER_DEBUG_SPINLOCKS,
727 "%s: line=%d\n", __func__, line);
728 spin_unlock(&node->lock);
729}
730
Todd Kjos673068e2017-06-29 12:02:03 -0700731/**
732 * binder_node_inner_lock() - Acquire node and inner locks
733 * @node: struct binder_node to acquire
734 *
735 * Acquires node->lock. If node->proc also acquires
736 * proc->inner_lock. Used to protect binder_node fields
737 */
738#define binder_node_inner_lock(node) _binder_node_inner_lock(node, __LINE__)
739static void
740_binder_node_inner_lock(struct binder_node *node, int line)
741{
742 binder_debug(BINDER_DEBUG_SPINLOCKS,
743 "%s: line=%d\n", __func__, line);
744 spin_lock(&node->lock);
745 if (node->proc)
746 binder_inner_proc_lock(node->proc);
747}
748
749/**
750 * binder_node_unlock() - Release node and inner locks
751 * @node: struct binder_node to acquire
752 *
753 * Release lock acquired via binder_node_lock()
754 */
755#define binder_node_inner_unlock(node) _binder_node_inner_unlock(node, __LINE__)
756static void
757_binder_node_inner_unlock(struct binder_node *node, int line)
758{
759 struct binder_proc *proc = node->proc;
760
761 binder_debug(BINDER_DEBUG_SPINLOCKS,
762 "%s: line=%d\n", __func__, line);
763 if (proc)
764 binder_inner_proc_unlock(proc);
765 spin_unlock(&node->lock);
766}
767
Todd Kjos72196392017-06-29 12:02:02 -0700768static bool binder_worklist_empty_ilocked(struct list_head *list)
769{
770 return list_empty(list);
771}
772
773/**
774 * binder_worklist_empty() - Check if no items on the work list
775 * @proc: binder_proc associated with list
776 * @list: list to check
777 *
778 * Return: true if there are no items on list, else false
779 */
780static bool binder_worklist_empty(struct binder_proc *proc,
781 struct list_head *list)
782{
783 bool ret;
784
785 binder_inner_proc_lock(proc);
786 ret = binder_worklist_empty_ilocked(list);
787 binder_inner_proc_unlock(proc);
788 return ret;
789}
790
Martijn Coenen148ade22017-11-15 09:21:35 +0100791/**
792 * binder_enqueue_work_ilocked() - Add an item to the work list
793 * @work: struct binder_work to add to list
794 * @target_list: list to add work to
795 *
796 * Adds the work to the specified list. Asserts that work
797 * is not already on a list.
798 *
799 * Requires the proc->inner_lock to be held.
800 */
Todd Kjos72196392017-06-29 12:02:02 -0700801static void
802binder_enqueue_work_ilocked(struct binder_work *work,
803 struct list_head *target_list)
804{
805 BUG_ON(target_list == NULL);
806 BUG_ON(work->entry.next && !list_empty(&work->entry));
807 list_add_tail(&work->entry, target_list);
808}
809
810/**
Martijn Coenen148ade22017-11-15 09:21:35 +0100811 * binder_enqueue_deferred_thread_work_ilocked() - Add deferred thread work
812 * @thread: thread to queue work to
Todd Kjos72196392017-06-29 12:02:02 -0700813 * @work: struct binder_work to add to list
Todd Kjos72196392017-06-29 12:02:02 -0700814 *
Martijn Coenen148ade22017-11-15 09:21:35 +0100815 * Adds the work to the todo list of the thread. Doesn't set the process_todo
816 * flag, which means that (if it wasn't already set) the thread will go to
817 * sleep without handling this work when it calls read.
818 *
819 * Requires the proc->inner_lock to be held.
Todd Kjos72196392017-06-29 12:02:02 -0700820 */
821static void
Martijn Coenen148ade22017-11-15 09:21:35 +0100822binder_enqueue_deferred_thread_work_ilocked(struct binder_thread *thread,
823 struct binder_work *work)
Todd Kjos72196392017-06-29 12:02:02 -0700824{
Sherry Yang44b73962018-08-13 17:28:53 -0700825 WARN_ON(!list_empty(&thread->waiting_thread_node));
Martijn Coenen148ade22017-11-15 09:21:35 +0100826 binder_enqueue_work_ilocked(work, &thread->todo);
827}
828
829/**
830 * binder_enqueue_thread_work_ilocked() - Add an item to the thread work list
831 * @thread: thread to queue work to
832 * @work: struct binder_work to add to list
833 *
834 * Adds the work to the todo list of the thread, and enables processing
835 * of the todo queue.
836 *
837 * Requires the proc->inner_lock to be held.
838 */
839static void
840binder_enqueue_thread_work_ilocked(struct binder_thread *thread,
841 struct binder_work *work)
842{
Sherry Yang44b73962018-08-13 17:28:53 -0700843 WARN_ON(!list_empty(&thread->waiting_thread_node));
Martijn Coenen148ade22017-11-15 09:21:35 +0100844 binder_enqueue_work_ilocked(work, &thread->todo);
845 thread->process_todo = true;
846}
847
848/**
849 * binder_enqueue_thread_work() - Add an item to the thread work list
850 * @thread: thread to queue work to
851 * @work: struct binder_work to add to list
852 *
853 * Adds the work to the todo list of the thread, and enables processing
854 * of the todo queue.
855 */
856static void
857binder_enqueue_thread_work(struct binder_thread *thread,
858 struct binder_work *work)
859{
860 binder_inner_proc_lock(thread->proc);
861 binder_enqueue_thread_work_ilocked(thread, work);
862 binder_inner_proc_unlock(thread->proc);
Todd Kjos72196392017-06-29 12:02:02 -0700863}
864
865static void
866binder_dequeue_work_ilocked(struct binder_work *work)
867{
868 list_del_init(&work->entry);
869}
870
871/**
872 * binder_dequeue_work() - Removes an item from the work list
873 * @proc: binder_proc associated with list
874 * @work: struct binder_work to remove from list
875 *
876 * Removes the specified work item from whatever list it is on.
877 * Can safely be called if work is not on any list.
878 */
879static void
880binder_dequeue_work(struct binder_proc *proc, struct binder_work *work)
881{
882 binder_inner_proc_lock(proc);
883 binder_dequeue_work_ilocked(work);
884 binder_inner_proc_unlock(proc);
885}
886
887static struct binder_work *binder_dequeue_work_head_ilocked(
888 struct list_head *list)
889{
890 struct binder_work *w;
891
892 w = list_first_entry_or_null(list, struct binder_work, entry);
893 if (w)
894 list_del_init(&w->entry);
895 return w;
896}
897
898/**
899 * binder_dequeue_work_head() - Dequeues the item at head of list
900 * @proc: binder_proc associated with list
901 * @list: list to dequeue head
902 *
903 * Removes the head of the list if there are items on the list
904 *
905 * Return: pointer dequeued binder_work, NULL if list was empty
906 */
907static struct binder_work *binder_dequeue_work_head(
908 struct binder_proc *proc,
909 struct list_head *list)
910{
911 struct binder_work *w;
912
913 binder_inner_proc_lock(proc);
914 w = binder_dequeue_work_head_ilocked(list);
915 binder_inner_proc_unlock(proc);
916 return w;
917}
918
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900919static void
920binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
Todd Kjos7a4408c2017-06-29 12:01:57 -0700921static void binder_free_thread(struct binder_thread *thread);
922static void binder_free_proc(struct binder_proc *proc);
Todd Kjosda0fa9e2017-06-29 12:02:04 -0700923static void binder_inc_node_tmpref_ilocked(struct binder_node *node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900924
Sachin Kamatefde99c2012-08-17 16:39:36 +0530925static int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900926{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900927 unsigned long rlim_cur;
928 unsigned long irqs;
Todd Kjos7f3dc002017-11-27 09:32:33 -0800929 int ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900930
Todd Kjos7f3dc002017-11-27 09:32:33 -0800931 mutex_lock(&proc->files_lock);
932 if (proc->files == NULL) {
933 ret = -ESRCH;
934 goto err;
935 }
936 if (!lock_task_sighand(proc->tsk, &irqs)) {
937 ret = -EMFILE;
938 goto err;
939 }
Al Virodcfadfa2012-08-12 17:27:30 -0400940 rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
941 unlock_task_sighand(proc->tsk, &irqs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900942
Todd Kjos7f3dc002017-11-27 09:32:33 -0800943 ret = __alloc_fd(proc->files, 0, rlim_cur, flags);
944err:
945 mutex_unlock(&proc->files_lock);
946 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900947}
948
949/*
950 * copied from fd_install
951 */
952static void task_fd_install(
953 struct binder_proc *proc, unsigned int fd, struct file *file)
954{
Todd Kjos7f3dc002017-11-27 09:32:33 -0800955 mutex_lock(&proc->files_lock);
Al Virof869e8a2012-08-15 21:06:33 -0400956 if (proc->files)
957 __fd_install(proc->files, fd, file);
Todd Kjos7f3dc002017-11-27 09:32:33 -0800958 mutex_unlock(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900959}
960
961/*
962 * copied from sys_close
963 */
964static long task_close_fd(struct binder_proc *proc, unsigned int fd)
965{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900966 int retval;
967
Todd Kjos7f3dc002017-11-27 09:32:33 -0800968 mutex_lock(&proc->files_lock);
969 if (proc->files == NULL) {
970 retval = -ESRCH;
971 goto err;
972 }
Al Viro483ce1d2012-08-19 12:04:24 -0400973 retval = __close_fd(proc->files, fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900974 /* can't restart close syscall because file table entry was cleared */
975 if (unlikely(retval == -ERESTARTSYS ||
976 retval == -ERESTARTNOINTR ||
977 retval == -ERESTARTNOHAND ||
978 retval == -ERESTART_RESTARTBLOCK))
979 retval = -EINTR;
Todd Kjos7f3dc002017-11-27 09:32:33 -0800980err:
981 mutex_unlock(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900982 return retval;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900983}
984
Martijn Coenen1b77e9d2017-08-31 10:04:18 +0200985static bool binder_has_work_ilocked(struct binder_thread *thread,
986 bool do_proc_work)
987{
Martijn Coenen148ade22017-11-15 09:21:35 +0100988 return thread->process_todo ||
Martijn Coenen1b77e9d2017-08-31 10:04:18 +0200989 thread->looper_need_return ||
990 (do_proc_work &&
991 !binder_worklist_empty_ilocked(&thread->proc->todo));
992}
993
994static bool binder_has_work(struct binder_thread *thread, bool do_proc_work)
995{
996 bool has_work;
997
998 binder_inner_proc_lock(thread->proc);
999 has_work = binder_has_work_ilocked(thread, do_proc_work);
1000 binder_inner_proc_unlock(thread->proc);
1001
1002 return has_work;
1003}
1004
1005static bool binder_available_for_proc_work_ilocked(struct binder_thread *thread)
1006{
1007 return !thread->transaction_stack &&
1008 binder_worklist_empty_ilocked(&thread->todo) &&
1009 (thread->looper & (BINDER_LOOPER_STATE_ENTERED |
1010 BINDER_LOOPER_STATE_REGISTERED));
1011}
1012
1013static void binder_wakeup_poll_threads_ilocked(struct binder_proc *proc,
1014 bool sync)
1015{
1016 struct rb_node *n;
1017 struct binder_thread *thread;
1018
1019 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
1020 thread = rb_entry(n, struct binder_thread, rb_node);
1021 if (thread->looper & BINDER_LOOPER_STATE_POLL &&
1022 binder_available_for_proc_work_ilocked(thread)) {
1023 if (sync)
1024 wake_up_interruptible_sync(&thread->wait);
1025 else
1026 wake_up_interruptible(&thread->wait);
1027 }
1028 }
1029}
1030
Martijn Coenen408c68b2017-08-31 10:04:19 +02001031/**
1032 * binder_select_thread_ilocked() - selects a thread for doing proc work.
1033 * @proc: process to select a thread from
1034 *
1035 * Note that calling this function moves the thread off the waiting_threads
1036 * list, so it can only be woken up by the caller of this function, or a
1037 * signal. Therefore, callers *should* always wake up the thread this function
1038 * returns.
1039 *
1040 * Return: If there's a thread currently waiting for process work,
1041 * returns that thread. Otherwise returns NULL.
1042 */
1043static struct binder_thread *
1044binder_select_thread_ilocked(struct binder_proc *proc)
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02001045{
1046 struct binder_thread *thread;
1047
Martijn Coenen858b2712017-08-31 10:04:26 +02001048 assert_spin_locked(&proc->inner_lock);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02001049 thread = list_first_entry_or_null(&proc->waiting_threads,
1050 struct binder_thread,
1051 waiting_thread_node);
1052
Martijn Coenen408c68b2017-08-31 10:04:19 +02001053 if (thread)
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02001054 list_del_init(&thread->waiting_thread_node);
Martijn Coenen408c68b2017-08-31 10:04:19 +02001055
1056 return thread;
1057}
1058
1059/**
1060 * binder_wakeup_thread_ilocked() - wakes up a thread for doing proc work.
1061 * @proc: process to wake up a thread in
1062 * @thread: specific thread to wake-up (may be NULL)
1063 * @sync: whether to do a synchronous wake-up
1064 *
1065 * This function wakes up a thread in the @proc process.
1066 * The caller may provide a specific thread to wake-up in
1067 * the @thread parameter. If @thread is NULL, this function
1068 * will wake up threads that have called poll().
1069 *
1070 * Note that for this function to work as expected, callers
1071 * should first call binder_select_thread() to find a thread
1072 * to handle the work (if they don't have a thread already),
1073 * and pass the result into the @thread parameter.
1074 */
1075static void binder_wakeup_thread_ilocked(struct binder_proc *proc,
1076 struct binder_thread *thread,
1077 bool sync)
1078{
Martijn Coenen858b2712017-08-31 10:04:26 +02001079 assert_spin_locked(&proc->inner_lock);
Martijn Coenen408c68b2017-08-31 10:04:19 +02001080
1081 if (thread) {
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02001082 if (sync)
1083 wake_up_interruptible_sync(&thread->wait);
1084 else
1085 wake_up_interruptible(&thread->wait);
1086 return;
1087 }
1088
1089 /* Didn't find a thread waiting for proc work; this can happen
1090 * in two scenarios:
1091 * 1. All threads are busy handling transactions
1092 * In that case, one of those threads should call back into
1093 * the kernel driver soon and pick up this work.
1094 * 2. Threads are using the (e)poll interface, in which case
1095 * they may be blocked on the waitqueue without having been
1096 * added to waiting_threads. For this case, we just iterate
1097 * over all threads not handling transaction work, and
1098 * wake them all up. We wake all because we don't know whether
1099 * a thread that called into (e)poll is handling non-binder
1100 * work currently.
1101 */
1102 binder_wakeup_poll_threads_ilocked(proc, sync);
1103}
1104
Martijn Coenen408c68b2017-08-31 10:04:19 +02001105static void binder_wakeup_proc_ilocked(struct binder_proc *proc)
1106{
1107 struct binder_thread *thread = binder_select_thread_ilocked(proc);
1108
1109 binder_wakeup_thread_ilocked(proc, thread, /* sync = */false);
1110}
1111
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001112static void binder_set_nice(long nice)
1113{
1114 long min_nice;
Seunghun Lee10f62862014-05-01 01:30:23 +09001115
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001116 if (can_nice(current, nice)) {
1117 set_user_nice(current, nice);
1118 return;
1119 }
Krzysztof Opasiakc3643b62017-07-05 19:24:44 +02001120 min_nice = rlimit_to_nice(rlimit(RLIMIT_NICE));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001121 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301122 "%d: nice value %ld not allowed use %ld instead\n",
1123 current->pid, nice, min_nice);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001124 set_user_nice(current, min_nice);
Dongsheng Yang8698a742014-03-11 18:09:12 +08001125 if (min_nice <= MAX_NICE)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001126 return;
Anmol Sarma56b468f2012-10-30 22:35:43 +05301127 binder_user_error("%d RLIMIT_NICE not set\n", current->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001128}
1129
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001130static struct binder_node *binder_get_node_ilocked(struct binder_proc *proc,
1131 binder_uintptr_t ptr)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001132{
1133 struct rb_node *n = proc->nodes.rb_node;
1134 struct binder_node *node;
1135
Martijn Coenen858b2712017-08-31 10:04:26 +02001136 assert_spin_locked(&proc->inner_lock);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001137
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001138 while (n) {
1139 node = rb_entry(n, struct binder_node, rb_node);
1140
1141 if (ptr < node->ptr)
1142 n = n->rb_left;
1143 else if (ptr > node->ptr)
1144 n = n->rb_right;
Todd Kjosadc18842017-06-29 12:01:59 -07001145 else {
1146 /*
1147 * take an implicit weak reference
1148 * to ensure node stays alive until
1149 * call to binder_put_node()
1150 */
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001151 binder_inc_node_tmpref_ilocked(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001152 return node;
Todd Kjosadc18842017-06-29 12:01:59 -07001153 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001154 }
1155 return NULL;
1156}
1157
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001158static struct binder_node *binder_get_node(struct binder_proc *proc,
1159 binder_uintptr_t ptr)
1160{
1161 struct binder_node *node;
1162
1163 binder_inner_proc_lock(proc);
1164 node = binder_get_node_ilocked(proc, ptr);
1165 binder_inner_proc_unlock(proc);
1166 return node;
1167}
1168
1169static struct binder_node *binder_init_node_ilocked(
1170 struct binder_proc *proc,
1171 struct binder_node *new_node,
1172 struct flat_binder_object *fp)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001173{
1174 struct rb_node **p = &proc->nodes.rb_node;
1175 struct rb_node *parent = NULL;
1176 struct binder_node *node;
Todd Kjos673068e2017-06-29 12:02:03 -07001177 binder_uintptr_t ptr = fp ? fp->binder : 0;
1178 binder_uintptr_t cookie = fp ? fp->cookie : 0;
1179 __u32 flags = fp ? fp->flags : 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001180
Martijn Coenen858b2712017-08-31 10:04:26 +02001181 assert_spin_locked(&proc->inner_lock);
1182
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001183 while (*p) {
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001184
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001185 parent = *p;
1186 node = rb_entry(parent, struct binder_node, rb_node);
1187
1188 if (ptr < node->ptr)
1189 p = &(*p)->rb_left;
1190 else if (ptr > node->ptr)
1191 p = &(*p)->rb_right;
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001192 else {
1193 /*
1194 * A matching node is already in
1195 * the rb tree. Abandon the init
1196 * and return it.
1197 */
1198 binder_inc_node_tmpref_ilocked(node);
1199 return node;
1200 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001201 }
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001202 node = new_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001203 binder_stats_created(BINDER_STAT_NODE);
Todd Kjosadc18842017-06-29 12:01:59 -07001204 node->tmp_refs++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001205 rb_link_node(&node->rb_node, parent, p);
1206 rb_insert_color(&node->rb_node, &proc->nodes);
Todd Kjos656a8002017-06-29 12:01:45 -07001207 node->debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001208 node->proc = proc;
1209 node->ptr = ptr;
1210 node->cookie = cookie;
1211 node->work.type = BINDER_WORK_NODE;
Todd Kjos673068e2017-06-29 12:02:03 -07001212 node->min_priority = flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
1213 node->accept_fds = !!(flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
Todd Kjos9630fe82017-06-29 12:02:00 -07001214 spin_lock_init(&node->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001215 INIT_LIST_HEAD(&node->work.entry);
1216 INIT_LIST_HEAD(&node->async_todo);
1217 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001218 "%d:%d node %d u%016llx c%016llx created\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001219 proc->pid, current->pid, node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001220 (u64)node->ptr, (u64)node->cookie);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001221
1222 return node;
1223}
1224
1225static struct binder_node *binder_new_node(struct binder_proc *proc,
1226 struct flat_binder_object *fp)
1227{
1228 struct binder_node *node;
1229 struct binder_node *new_node = kzalloc(sizeof(*node), GFP_KERNEL);
1230
1231 if (!new_node)
1232 return NULL;
1233 binder_inner_proc_lock(proc);
1234 node = binder_init_node_ilocked(proc, new_node, fp);
1235 binder_inner_proc_unlock(proc);
1236 if (node != new_node)
1237 /*
1238 * The node was already added by another thread
1239 */
1240 kfree(new_node);
1241
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001242 return node;
1243}
1244
Todd Kjosed297212017-06-29 12:02:01 -07001245static void binder_free_node(struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001246{
Todd Kjosed297212017-06-29 12:02:01 -07001247 kfree(node);
1248 binder_stats_deleted(BINDER_STAT_NODE);
1249}
1250
Todd Kjos673068e2017-06-29 12:02:03 -07001251static int binder_inc_node_nilocked(struct binder_node *node, int strong,
1252 int internal,
1253 struct list_head *target_list)
Todd Kjosed297212017-06-29 12:02:01 -07001254{
Todd Kjos673068e2017-06-29 12:02:03 -07001255 struct binder_proc *proc = node->proc;
1256
Martijn Coenen858b2712017-08-31 10:04:26 +02001257 assert_spin_locked(&node->lock);
Todd Kjos673068e2017-06-29 12:02:03 -07001258 if (proc)
Martijn Coenen858b2712017-08-31 10:04:26 +02001259 assert_spin_locked(&proc->inner_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001260 if (strong) {
1261 if (internal) {
1262 if (target_list == NULL &&
1263 node->internal_strong_refs == 0 &&
Martijn Coenen342e5c92017-02-03 14:40:46 -08001264 !(node->proc &&
1265 node == node->proc->context->binder_context_mgr_node &&
1266 node->has_strong_ref)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301267 pr_err("invalid inc strong node for %d\n",
1268 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001269 return -EINVAL;
1270 }
1271 node->internal_strong_refs++;
1272 } else
1273 node->local_strong_refs++;
1274 if (!node->has_strong_ref && target_list) {
Sherry Yang44b73962018-08-13 17:28:53 -07001275 struct binder_thread *thread = container_of(target_list,
1276 struct binder_thread, todo);
Todd Kjos72196392017-06-29 12:02:02 -07001277 binder_dequeue_work_ilocked(&node->work);
Sherry Yang44b73962018-08-13 17:28:53 -07001278 BUG_ON(&thread->todo != target_list);
1279 binder_enqueue_deferred_thread_work_ilocked(thread,
1280 &node->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001281 }
1282 } else {
1283 if (!internal)
1284 node->local_weak_refs++;
1285 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
1286 if (target_list == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301287 pr_err("invalid inc weak node for %d\n",
1288 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001289 return -EINVAL;
1290 }
Martijn Coenen148ade22017-11-15 09:21:35 +01001291 /*
1292 * See comment above
1293 */
Todd Kjos72196392017-06-29 12:02:02 -07001294 binder_enqueue_work_ilocked(&node->work, target_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001295 }
1296 }
1297 return 0;
1298}
1299
Todd Kjosed297212017-06-29 12:02:01 -07001300static int binder_inc_node(struct binder_node *node, int strong, int internal,
1301 struct list_head *target_list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001302{
Todd Kjosed297212017-06-29 12:02:01 -07001303 int ret;
1304
Todd Kjos673068e2017-06-29 12:02:03 -07001305 binder_node_inner_lock(node);
1306 ret = binder_inc_node_nilocked(node, strong, internal, target_list);
1307 binder_node_inner_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07001308
1309 return ret;
1310}
1311
Todd Kjos673068e2017-06-29 12:02:03 -07001312static bool binder_dec_node_nilocked(struct binder_node *node,
1313 int strong, int internal)
Todd Kjosed297212017-06-29 12:02:01 -07001314{
1315 struct binder_proc *proc = node->proc;
1316
Martijn Coenen858b2712017-08-31 10:04:26 +02001317 assert_spin_locked(&node->lock);
Todd Kjosed297212017-06-29 12:02:01 -07001318 if (proc)
Martijn Coenen858b2712017-08-31 10:04:26 +02001319 assert_spin_locked(&proc->inner_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001320 if (strong) {
1321 if (internal)
1322 node->internal_strong_refs--;
1323 else
1324 node->local_strong_refs--;
1325 if (node->local_strong_refs || node->internal_strong_refs)
Todd Kjosed297212017-06-29 12:02:01 -07001326 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001327 } else {
1328 if (!internal)
1329 node->local_weak_refs--;
Todd Kjosadc18842017-06-29 12:01:59 -07001330 if (node->local_weak_refs || node->tmp_refs ||
1331 !hlist_empty(&node->refs))
Todd Kjosed297212017-06-29 12:02:01 -07001332 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001333 }
Todd Kjosed297212017-06-29 12:02:01 -07001334
1335 if (proc && (node->has_strong_ref || node->has_weak_ref)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001336 if (list_empty(&node->work.entry)) {
Todd Kjos72196392017-06-29 12:02:02 -07001337 binder_enqueue_work_ilocked(&node->work, &proc->todo);
Martijn Coenen408c68b2017-08-31 10:04:19 +02001338 binder_wakeup_proc_ilocked(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001339 }
1340 } else {
1341 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
Todd Kjosadc18842017-06-29 12:01:59 -07001342 !node->local_weak_refs && !node->tmp_refs) {
Todd Kjosed297212017-06-29 12:02:01 -07001343 if (proc) {
Todd Kjos72196392017-06-29 12:02:02 -07001344 binder_dequeue_work_ilocked(&node->work);
1345 rb_erase(&node->rb_node, &proc->nodes);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001346 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301347 "refless node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001348 node->debug_id);
1349 } else {
Todd Kjos72196392017-06-29 12:02:02 -07001350 BUG_ON(!list_empty(&node->work.entry));
Todd Kjosc44b1232017-06-29 12:01:43 -07001351 spin_lock(&binder_dead_nodes_lock);
Todd Kjosed297212017-06-29 12:02:01 -07001352 /*
1353 * tmp_refs could have changed so
1354 * check it again
1355 */
1356 if (node->tmp_refs) {
1357 spin_unlock(&binder_dead_nodes_lock);
1358 return false;
1359 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001360 hlist_del(&node->dead_node);
Todd Kjosc44b1232017-06-29 12:01:43 -07001361 spin_unlock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001362 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301363 "dead node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001364 node->debug_id);
1365 }
Todd Kjosed297212017-06-29 12:02:01 -07001366 return true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001367 }
1368 }
Todd Kjosed297212017-06-29 12:02:01 -07001369 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001370}
1371
Todd Kjosed297212017-06-29 12:02:01 -07001372static void binder_dec_node(struct binder_node *node, int strong, int internal)
1373{
1374 bool free_node;
1375
Todd Kjos673068e2017-06-29 12:02:03 -07001376 binder_node_inner_lock(node);
1377 free_node = binder_dec_node_nilocked(node, strong, internal);
1378 binder_node_inner_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07001379 if (free_node)
1380 binder_free_node(node);
1381}
1382
1383static void binder_inc_node_tmpref_ilocked(struct binder_node *node)
Todd Kjosadc18842017-06-29 12:01:59 -07001384{
1385 /*
1386 * No call to binder_inc_node() is needed since we
1387 * don't need to inform userspace of any changes to
1388 * tmp_refs
1389 */
1390 node->tmp_refs++;
1391}
1392
1393/**
Todd Kjosed297212017-06-29 12:02:01 -07001394 * binder_inc_node_tmpref() - take a temporary reference on node
1395 * @node: node to reference
1396 *
1397 * Take reference on node to prevent the node from being freed
1398 * while referenced only by a local variable. The inner lock is
1399 * needed to serialize with the node work on the queue (which
1400 * isn't needed after the node is dead). If the node is dead
1401 * (node->proc is NULL), use binder_dead_nodes_lock to protect
1402 * node->tmp_refs against dead-node-only cases where the node
1403 * lock cannot be acquired (eg traversing the dead node list to
1404 * print nodes)
1405 */
1406static void binder_inc_node_tmpref(struct binder_node *node)
1407{
Todd Kjos673068e2017-06-29 12:02:03 -07001408 binder_node_lock(node);
Todd Kjosed297212017-06-29 12:02:01 -07001409 if (node->proc)
1410 binder_inner_proc_lock(node->proc);
1411 else
1412 spin_lock(&binder_dead_nodes_lock);
1413 binder_inc_node_tmpref_ilocked(node);
1414 if (node->proc)
1415 binder_inner_proc_unlock(node->proc);
1416 else
1417 spin_unlock(&binder_dead_nodes_lock);
Todd Kjos673068e2017-06-29 12:02:03 -07001418 binder_node_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07001419}
1420
1421/**
Todd Kjosadc18842017-06-29 12:01:59 -07001422 * binder_dec_node_tmpref() - remove a temporary reference on node
1423 * @node: node to reference
1424 *
1425 * Release temporary reference on node taken via binder_inc_node_tmpref()
1426 */
1427static void binder_dec_node_tmpref(struct binder_node *node)
1428{
Todd Kjosed297212017-06-29 12:02:01 -07001429 bool free_node;
1430
Todd Kjos673068e2017-06-29 12:02:03 -07001431 binder_node_inner_lock(node);
1432 if (!node->proc)
Todd Kjosed297212017-06-29 12:02:01 -07001433 spin_lock(&binder_dead_nodes_lock);
Todd Kjosadc18842017-06-29 12:01:59 -07001434 node->tmp_refs--;
1435 BUG_ON(node->tmp_refs < 0);
Todd Kjosed297212017-06-29 12:02:01 -07001436 if (!node->proc)
1437 spin_unlock(&binder_dead_nodes_lock);
Todd Kjosadc18842017-06-29 12:01:59 -07001438 /*
1439 * Call binder_dec_node() to check if all refcounts are 0
1440 * and cleanup is needed. Calling with strong=0 and internal=1
1441 * causes no actual reference to be released in binder_dec_node().
1442 * If that changes, a change is needed here too.
1443 */
Todd Kjos673068e2017-06-29 12:02:03 -07001444 free_node = binder_dec_node_nilocked(node, 0, 1);
1445 binder_node_inner_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07001446 if (free_node)
1447 binder_free_node(node);
Todd Kjosadc18842017-06-29 12:01:59 -07001448}
1449
1450static void binder_put_node(struct binder_node *node)
1451{
1452 binder_dec_node_tmpref(node);
1453}
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001454
Todd Kjos2c1838d2017-06-29 12:02:08 -07001455static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc,
1456 u32 desc, bool need_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001457{
1458 struct rb_node *n = proc->refs_by_desc.rb_node;
1459 struct binder_ref *ref;
1460
1461 while (n) {
1462 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1463
Todd Kjos372e3142017-06-29 12:01:58 -07001464 if (desc < ref->data.desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001465 n = n->rb_left;
Todd Kjos372e3142017-06-29 12:01:58 -07001466 } else if (desc > ref->data.desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001467 n = n->rb_right;
Todd Kjos372e3142017-06-29 12:01:58 -07001468 } else if (need_strong_ref && !ref->data.strong) {
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001469 binder_user_error("tried to use weak ref as strong ref\n");
1470 return NULL;
1471 } else {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001472 return ref;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001473 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001474 }
1475 return NULL;
1476}
1477
Todd Kjos372e3142017-06-29 12:01:58 -07001478/**
Todd Kjos2c1838d2017-06-29 12:02:08 -07001479 * binder_get_ref_for_node_olocked() - get the ref associated with given node
Todd Kjos372e3142017-06-29 12:01:58 -07001480 * @proc: binder_proc that owns the ref
1481 * @node: binder_node of target
1482 * @new_ref: newly allocated binder_ref to be initialized or %NULL
1483 *
1484 * Look up the ref for the given node and return it if it exists
1485 *
1486 * If it doesn't exist and the caller provides a newly allocated
1487 * ref, initialize the fields of the newly allocated ref and insert
1488 * into the given proc rb_trees and node refs list.
1489 *
1490 * Return: the ref for node. It is possible that another thread
1491 * allocated/initialized the ref first in which case the
1492 * returned ref would be different than the passed-in
1493 * new_ref. new_ref must be kfree'd by the caller in
1494 * this case.
1495 */
Todd Kjos2c1838d2017-06-29 12:02:08 -07001496static struct binder_ref *binder_get_ref_for_node_olocked(
1497 struct binder_proc *proc,
1498 struct binder_node *node,
1499 struct binder_ref *new_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001500{
Todd Kjos372e3142017-06-29 12:01:58 -07001501 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001502 struct rb_node **p = &proc->refs_by_node.rb_node;
1503 struct rb_node *parent = NULL;
Todd Kjos372e3142017-06-29 12:01:58 -07001504 struct binder_ref *ref;
1505 struct rb_node *n;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001506
1507 while (*p) {
1508 parent = *p;
1509 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1510
1511 if (node < ref->node)
1512 p = &(*p)->rb_left;
1513 else if (node > ref->node)
1514 p = &(*p)->rb_right;
1515 else
1516 return ref;
1517 }
Todd Kjos372e3142017-06-29 12:01:58 -07001518 if (!new_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001519 return NULL;
Todd Kjos372e3142017-06-29 12:01:58 -07001520
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001521 binder_stats_created(BINDER_STAT_REF);
Todd Kjos372e3142017-06-29 12:01:58 -07001522 new_ref->data.debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001523 new_ref->proc = proc;
1524 new_ref->node = node;
1525 rb_link_node(&new_ref->rb_node_node, parent, p);
1526 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1527
Todd Kjos372e3142017-06-29 12:01:58 -07001528 new_ref->data.desc = (node == context->binder_context_mgr_node) ? 0 : 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001529 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1530 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Todd Kjos372e3142017-06-29 12:01:58 -07001531 if (ref->data.desc > new_ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001532 break;
Todd Kjos372e3142017-06-29 12:01:58 -07001533 new_ref->data.desc = ref->data.desc + 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001534 }
1535
1536 p = &proc->refs_by_desc.rb_node;
1537 while (*p) {
1538 parent = *p;
1539 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1540
Todd Kjos372e3142017-06-29 12:01:58 -07001541 if (new_ref->data.desc < ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001542 p = &(*p)->rb_left;
Todd Kjos372e3142017-06-29 12:01:58 -07001543 else if (new_ref->data.desc > ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001544 p = &(*p)->rb_right;
1545 else
1546 BUG();
1547 }
1548 rb_link_node(&new_ref->rb_node_desc, parent, p);
1549 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
Todd Kjos673068e2017-06-29 12:02:03 -07001550
1551 binder_node_lock(node);
Todd Kjose4cffcf2017-06-29 12:01:50 -07001552 hlist_add_head(&new_ref->node_entry, &node->refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001553
Todd Kjose4cffcf2017-06-29 12:01:50 -07001554 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1555 "%d new ref %d desc %d for node %d\n",
Todd Kjos372e3142017-06-29 12:01:58 -07001556 proc->pid, new_ref->data.debug_id, new_ref->data.desc,
Todd Kjose4cffcf2017-06-29 12:01:50 -07001557 node->debug_id);
Todd Kjos673068e2017-06-29 12:02:03 -07001558 binder_node_unlock(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001559 return new_ref;
1560}
1561
Todd Kjos2c1838d2017-06-29 12:02:08 -07001562static void binder_cleanup_ref_olocked(struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001563{
Todd Kjosed297212017-06-29 12:02:01 -07001564 bool delete_node = false;
Todd Kjosed297212017-06-29 12:02:01 -07001565
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001566 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301567 "%d delete ref %d desc %d for node %d\n",
Todd Kjos372e3142017-06-29 12:01:58 -07001568 ref->proc->pid, ref->data.debug_id, ref->data.desc,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301569 ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001570
1571 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1572 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
Todd Kjos372e3142017-06-29 12:01:58 -07001573
Todd Kjos673068e2017-06-29 12:02:03 -07001574 binder_node_inner_lock(ref->node);
Todd Kjos372e3142017-06-29 12:01:58 -07001575 if (ref->data.strong)
Todd Kjos673068e2017-06-29 12:02:03 -07001576 binder_dec_node_nilocked(ref->node, 1, 1);
Todd Kjos372e3142017-06-29 12:01:58 -07001577
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001578 hlist_del(&ref->node_entry);
Todd Kjos673068e2017-06-29 12:02:03 -07001579 delete_node = binder_dec_node_nilocked(ref->node, 0, 1);
1580 binder_node_inner_unlock(ref->node);
Todd Kjosed297212017-06-29 12:02:01 -07001581 /*
1582 * Clear ref->node unless we want the caller to free the node
1583 */
1584 if (!delete_node) {
1585 /*
1586 * The caller uses ref->node to determine
1587 * whether the node needs to be freed. Clear
1588 * it since the node is still alive.
1589 */
1590 ref->node = NULL;
1591 }
Todd Kjos372e3142017-06-29 12:01:58 -07001592
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001593 if (ref->death) {
1594 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301595 "%d delete ref %d desc %d has death notification\n",
Todd Kjos372e3142017-06-29 12:01:58 -07001596 ref->proc->pid, ref->data.debug_id,
1597 ref->data.desc);
Todd Kjos72196392017-06-29 12:02:02 -07001598 binder_dequeue_work(ref->proc, &ref->death->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001599 binder_stats_deleted(BINDER_STAT_DEATH);
1600 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001601 binder_stats_deleted(BINDER_STAT_REF);
1602}
1603
Todd Kjos372e3142017-06-29 12:01:58 -07001604/**
Todd Kjos2c1838d2017-06-29 12:02:08 -07001605 * binder_inc_ref_olocked() - increment the ref for given handle
Todd Kjos372e3142017-06-29 12:01:58 -07001606 * @ref: ref to be incremented
1607 * @strong: if true, strong increment, else weak
1608 * @target_list: list to queue node work on
1609 *
Todd Kjos2c1838d2017-06-29 12:02:08 -07001610 * Increment the ref. @ref->proc->outer_lock must be held on entry
Todd Kjos372e3142017-06-29 12:01:58 -07001611 *
1612 * Return: 0, if successful, else errno
1613 */
Todd Kjos2c1838d2017-06-29 12:02:08 -07001614static int binder_inc_ref_olocked(struct binder_ref *ref, int strong,
1615 struct list_head *target_list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001616{
1617 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +09001618
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001619 if (strong) {
Todd Kjos372e3142017-06-29 12:01:58 -07001620 if (ref->data.strong == 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001621 ret = binder_inc_node(ref->node, 1, 1, target_list);
1622 if (ret)
1623 return ret;
1624 }
Todd Kjos372e3142017-06-29 12:01:58 -07001625 ref->data.strong++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001626 } else {
Todd Kjos372e3142017-06-29 12:01:58 -07001627 if (ref->data.weak == 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001628 ret = binder_inc_node(ref->node, 0, 1, target_list);
1629 if (ret)
1630 return ret;
1631 }
Todd Kjos372e3142017-06-29 12:01:58 -07001632 ref->data.weak++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001633 }
1634 return 0;
1635}
1636
Todd Kjos372e3142017-06-29 12:01:58 -07001637/**
1638 * binder_dec_ref() - dec the ref for given handle
1639 * @ref: ref to be decremented
1640 * @strong: if true, strong decrement, else weak
1641 *
1642 * Decrement the ref.
1643 *
Todd Kjos372e3142017-06-29 12:01:58 -07001644 * Return: true if ref is cleaned up and ready to be freed
1645 */
Todd Kjos2c1838d2017-06-29 12:02:08 -07001646static bool binder_dec_ref_olocked(struct binder_ref *ref, int strong)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001647{
1648 if (strong) {
Todd Kjos372e3142017-06-29 12:01:58 -07001649 if (ref->data.strong == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301650 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
Todd Kjos372e3142017-06-29 12:01:58 -07001651 ref->proc->pid, ref->data.debug_id,
1652 ref->data.desc, ref->data.strong,
1653 ref->data.weak);
1654 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001655 }
Todd Kjos372e3142017-06-29 12:01:58 -07001656 ref->data.strong--;
Todd Kjosed297212017-06-29 12:02:01 -07001657 if (ref->data.strong == 0)
1658 binder_dec_node(ref->node, strong, 1);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001659 } else {
Todd Kjos372e3142017-06-29 12:01:58 -07001660 if (ref->data.weak == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301661 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
Todd Kjos372e3142017-06-29 12:01:58 -07001662 ref->proc->pid, ref->data.debug_id,
1663 ref->data.desc, ref->data.strong,
1664 ref->data.weak);
1665 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001666 }
Todd Kjos372e3142017-06-29 12:01:58 -07001667 ref->data.weak--;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001668 }
Todd Kjos372e3142017-06-29 12:01:58 -07001669 if (ref->data.strong == 0 && ref->data.weak == 0) {
Todd Kjos2c1838d2017-06-29 12:02:08 -07001670 binder_cleanup_ref_olocked(ref);
Todd Kjos372e3142017-06-29 12:01:58 -07001671 return true;
1672 }
1673 return false;
1674}
1675
1676/**
1677 * binder_get_node_from_ref() - get the node from the given proc/desc
1678 * @proc: proc containing the ref
1679 * @desc: the handle associated with the ref
1680 * @need_strong_ref: if true, only return node if ref is strong
1681 * @rdata: the id/refcount data for the ref
1682 *
1683 * Given a proc and ref handle, return the associated binder_node
1684 *
1685 * Return: a binder_node or NULL if not found or not strong when strong required
1686 */
1687static struct binder_node *binder_get_node_from_ref(
1688 struct binder_proc *proc,
1689 u32 desc, bool need_strong_ref,
1690 struct binder_ref_data *rdata)
1691{
1692 struct binder_node *node;
1693 struct binder_ref *ref;
1694
Todd Kjos2c1838d2017-06-29 12:02:08 -07001695 binder_proc_lock(proc);
1696 ref = binder_get_ref_olocked(proc, desc, need_strong_ref);
Todd Kjos372e3142017-06-29 12:01:58 -07001697 if (!ref)
1698 goto err_no_ref;
1699 node = ref->node;
Todd Kjosadc18842017-06-29 12:01:59 -07001700 /*
1701 * Take an implicit reference on the node to ensure
1702 * it stays alive until the call to binder_put_node()
1703 */
1704 binder_inc_node_tmpref(node);
Todd Kjos372e3142017-06-29 12:01:58 -07001705 if (rdata)
1706 *rdata = ref->data;
Todd Kjos2c1838d2017-06-29 12:02:08 -07001707 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07001708
1709 return node;
1710
1711err_no_ref:
Todd Kjos2c1838d2017-06-29 12:02:08 -07001712 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07001713 return NULL;
1714}
1715
1716/**
1717 * binder_free_ref() - free the binder_ref
1718 * @ref: ref to free
1719 *
Todd Kjosed297212017-06-29 12:02:01 -07001720 * Free the binder_ref. Free the binder_node indicated by ref->node
1721 * (if non-NULL) and the binder_ref_death indicated by ref->death.
Todd Kjos372e3142017-06-29 12:01:58 -07001722 */
1723static void binder_free_ref(struct binder_ref *ref)
1724{
Todd Kjosed297212017-06-29 12:02:01 -07001725 if (ref->node)
1726 binder_free_node(ref->node);
Todd Kjos372e3142017-06-29 12:01:58 -07001727 kfree(ref->death);
1728 kfree(ref);
1729}
1730
1731/**
1732 * binder_update_ref_for_handle() - inc/dec the ref for given handle
1733 * @proc: proc containing the ref
1734 * @desc: the handle associated with the ref
1735 * @increment: true=inc reference, false=dec reference
1736 * @strong: true=strong reference, false=weak reference
1737 * @rdata: the id/refcount data for the ref
1738 *
1739 * Given a proc and ref handle, increment or decrement the ref
1740 * according to "increment" arg.
1741 *
1742 * Return: 0 if successful, else errno
1743 */
1744static int binder_update_ref_for_handle(struct binder_proc *proc,
1745 uint32_t desc, bool increment, bool strong,
1746 struct binder_ref_data *rdata)
1747{
1748 int ret = 0;
1749 struct binder_ref *ref;
1750 bool delete_ref = false;
1751
Todd Kjos2c1838d2017-06-29 12:02:08 -07001752 binder_proc_lock(proc);
1753 ref = binder_get_ref_olocked(proc, desc, strong);
Todd Kjos372e3142017-06-29 12:01:58 -07001754 if (!ref) {
1755 ret = -EINVAL;
1756 goto err_no_ref;
1757 }
1758 if (increment)
Todd Kjos2c1838d2017-06-29 12:02:08 -07001759 ret = binder_inc_ref_olocked(ref, strong, NULL);
Todd Kjos372e3142017-06-29 12:01:58 -07001760 else
Todd Kjos2c1838d2017-06-29 12:02:08 -07001761 delete_ref = binder_dec_ref_olocked(ref, strong);
Todd Kjos372e3142017-06-29 12:01:58 -07001762
1763 if (rdata)
1764 *rdata = ref->data;
Todd Kjos2c1838d2017-06-29 12:02:08 -07001765 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07001766
1767 if (delete_ref)
1768 binder_free_ref(ref);
1769 return ret;
1770
1771err_no_ref:
Todd Kjos2c1838d2017-06-29 12:02:08 -07001772 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07001773 return ret;
1774}
1775
1776/**
1777 * binder_dec_ref_for_handle() - dec the ref for given handle
1778 * @proc: proc containing the ref
1779 * @desc: the handle associated with the ref
1780 * @strong: true=strong reference, false=weak reference
1781 * @rdata: the id/refcount data for the ref
1782 *
1783 * Just calls binder_update_ref_for_handle() to decrement the ref.
1784 *
1785 * Return: 0 if successful, else errno
1786 */
1787static int binder_dec_ref_for_handle(struct binder_proc *proc,
1788 uint32_t desc, bool strong, struct binder_ref_data *rdata)
1789{
1790 return binder_update_ref_for_handle(proc, desc, false, strong, rdata);
1791}
1792
1793
1794/**
1795 * binder_inc_ref_for_node() - increment the ref for given proc/node
1796 * @proc: proc containing the ref
1797 * @node: target node
1798 * @strong: true=strong reference, false=weak reference
1799 * @target_list: worklist to use if node is incremented
1800 * @rdata: the id/refcount data for the ref
1801 *
1802 * Given a proc and node, increment the ref. Create the ref if it
1803 * doesn't already exist
1804 *
1805 * Return: 0 if successful, else errno
1806 */
1807static int binder_inc_ref_for_node(struct binder_proc *proc,
1808 struct binder_node *node,
1809 bool strong,
1810 struct list_head *target_list,
1811 struct binder_ref_data *rdata)
1812{
1813 struct binder_ref *ref;
1814 struct binder_ref *new_ref = NULL;
1815 int ret = 0;
1816
Todd Kjos2c1838d2017-06-29 12:02:08 -07001817 binder_proc_lock(proc);
1818 ref = binder_get_ref_for_node_olocked(proc, node, NULL);
Todd Kjos372e3142017-06-29 12:01:58 -07001819 if (!ref) {
Todd Kjos2c1838d2017-06-29 12:02:08 -07001820 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07001821 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1822 if (!new_ref)
1823 return -ENOMEM;
Todd Kjos2c1838d2017-06-29 12:02:08 -07001824 binder_proc_lock(proc);
1825 ref = binder_get_ref_for_node_olocked(proc, node, new_ref);
Todd Kjos372e3142017-06-29 12:01:58 -07001826 }
Todd Kjos2c1838d2017-06-29 12:02:08 -07001827 ret = binder_inc_ref_olocked(ref, strong, target_list);
Todd Kjos372e3142017-06-29 12:01:58 -07001828 *rdata = ref->data;
Todd Kjos2c1838d2017-06-29 12:02:08 -07001829 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07001830 if (new_ref && ref != new_ref)
1831 /*
1832 * Another thread created the ref first so
1833 * free the one we allocated
1834 */
1835 kfree(new_ref);
1836 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001837}
1838
Martijn Coenen0b89d692017-06-29 12:02:06 -07001839static void binder_pop_transaction_ilocked(struct binder_thread *target_thread,
1840 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001841{
Todd Kjosb6d282c2017-06-29 12:01:54 -07001842 BUG_ON(!target_thread);
Martijn Coenen858b2712017-08-31 10:04:26 +02001843 assert_spin_locked(&target_thread->proc->inner_lock);
Todd Kjosb6d282c2017-06-29 12:01:54 -07001844 BUG_ON(target_thread->transaction_stack != t);
1845 BUG_ON(target_thread->transaction_stack->from != target_thread);
1846 target_thread->transaction_stack =
1847 target_thread->transaction_stack->from_parent;
1848 t->from = NULL;
1849}
1850
Todd Kjos7a4408c2017-06-29 12:01:57 -07001851/**
1852 * binder_thread_dec_tmpref() - decrement thread->tmp_ref
1853 * @thread: thread to decrement
1854 *
1855 * A thread needs to be kept alive while being used to create or
1856 * handle a transaction. binder_get_txn_from() is used to safely
1857 * extract t->from from a binder_transaction and keep the thread
1858 * indicated by t->from from being freed. When done with that
1859 * binder_thread, this function is called to decrement the
1860 * tmp_ref and free if appropriate (thread has been released
1861 * and no transaction being processed by the driver)
1862 */
1863static void binder_thread_dec_tmpref(struct binder_thread *thread)
1864{
1865 /*
1866 * atomic is used to protect the counter value while
1867 * it cannot reach zero or thread->is_dead is false
Todd Kjos7a4408c2017-06-29 12:01:57 -07001868 */
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07001869 binder_inner_proc_lock(thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07001870 atomic_dec(&thread->tmp_ref);
1871 if (thread->is_dead && !atomic_read(&thread->tmp_ref)) {
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07001872 binder_inner_proc_unlock(thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07001873 binder_free_thread(thread);
1874 return;
1875 }
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07001876 binder_inner_proc_unlock(thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07001877}
1878
1879/**
1880 * binder_proc_dec_tmpref() - decrement proc->tmp_ref
1881 * @proc: proc to decrement
1882 *
1883 * A binder_proc needs to be kept alive while being used to create or
1884 * handle a transaction. proc->tmp_ref is incremented when
1885 * creating a new transaction or the binder_proc is currently in-use
1886 * by threads that are being released. When done with the binder_proc,
1887 * this function is called to decrement the counter and free the
1888 * proc if appropriate (proc has been released, all threads have
1889 * been released and not currenly in-use to process a transaction).
1890 */
1891static void binder_proc_dec_tmpref(struct binder_proc *proc)
1892{
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07001893 binder_inner_proc_lock(proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07001894 proc->tmp_ref--;
1895 if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) &&
1896 !proc->tmp_ref) {
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07001897 binder_inner_proc_unlock(proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07001898 binder_free_proc(proc);
1899 return;
1900 }
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07001901 binder_inner_proc_unlock(proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07001902}
1903
1904/**
1905 * binder_get_txn_from() - safely extract the "from" thread in transaction
1906 * @t: binder transaction for t->from
1907 *
1908 * Atomically return the "from" thread and increment the tmp_ref
1909 * count for the thread to ensure it stays alive until
1910 * binder_thread_dec_tmpref() is called.
1911 *
1912 * Return: the value of t->from
1913 */
1914static struct binder_thread *binder_get_txn_from(
1915 struct binder_transaction *t)
1916{
1917 struct binder_thread *from;
1918
1919 spin_lock(&t->lock);
1920 from = t->from;
1921 if (from)
1922 atomic_inc(&from->tmp_ref);
1923 spin_unlock(&t->lock);
1924 return from;
1925}
1926
Martijn Coenen0b89d692017-06-29 12:02:06 -07001927/**
1928 * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock
1929 * @t: binder transaction for t->from
1930 *
1931 * Same as binder_get_txn_from() except it also acquires the proc->inner_lock
1932 * to guarantee that the thread cannot be released while operating on it.
1933 * The caller must call binder_inner_proc_unlock() to release the inner lock
1934 * as well as call binder_dec_thread_txn() to release the reference.
1935 *
1936 * Return: the value of t->from
1937 */
1938static struct binder_thread *binder_get_txn_from_and_acq_inner(
1939 struct binder_transaction *t)
1940{
1941 struct binder_thread *from;
1942
1943 from = binder_get_txn_from(t);
1944 if (!from)
1945 return NULL;
1946 binder_inner_proc_lock(from->proc);
1947 if (t->from) {
1948 BUG_ON(from != t->from);
1949 return from;
1950 }
1951 binder_inner_proc_unlock(from->proc);
1952 binder_thread_dec_tmpref(from);
1953 return NULL;
1954}
1955
Todd Kjosb6d282c2017-06-29 12:01:54 -07001956static void binder_free_transaction(struct binder_transaction *t)
1957{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001958 if (t->buffer)
1959 t->buffer->transaction = NULL;
1960 kfree(t);
1961 binder_stats_deleted(BINDER_STAT_TRANSACTION);
1962}
1963
1964static void binder_send_failed_reply(struct binder_transaction *t,
1965 uint32_t error_code)
1966{
1967 struct binder_thread *target_thread;
Lucas Tanured4ec15e2014-07-13 21:31:05 -03001968 struct binder_transaction *next;
Seunghun Lee10f62862014-05-01 01:30:23 +09001969
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001970 BUG_ON(t->flags & TF_ONE_WAY);
1971 while (1) {
Martijn Coenen0b89d692017-06-29 12:02:06 -07001972 target_thread = binder_get_txn_from_and_acq_inner(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001973 if (target_thread) {
Todd Kjos26549d12017-06-29 12:01:55 -07001974 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1975 "send failed reply for transaction %d to %d:%d\n",
1976 t->debug_id,
1977 target_thread->proc->pid,
1978 target_thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001979
Martijn Coenen0b89d692017-06-29 12:02:06 -07001980 binder_pop_transaction_ilocked(target_thread, t);
Todd Kjos26549d12017-06-29 12:01:55 -07001981 if (target_thread->reply_error.cmd == BR_OK) {
1982 target_thread->reply_error.cmd = error_code;
Martijn Coenen148ade22017-11-15 09:21:35 +01001983 binder_enqueue_thread_work_ilocked(
1984 target_thread,
1985 &target_thread->reply_error.work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001986 wake_up_interruptible(&target_thread->wait);
1987 } else {
Todd Kjose46a3b32018-02-07 12:38:47 -08001988 /*
1989 * Cannot get here for normal operation, but
1990 * we can if multiple synchronous transactions
1991 * are sent without blocking for responses.
1992 * Just ignore the 2nd error in this case.
1993 */
1994 pr_warn("Unexpected reply error: %u\n",
1995 target_thread->reply_error.cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001996 }
Martijn Coenen0b89d692017-06-29 12:02:06 -07001997 binder_inner_proc_unlock(target_thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07001998 binder_thread_dec_tmpref(target_thread);
Todd Kjos26549d12017-06-29 12:01:55 -07001999 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002000 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002001 }
Lucas Tanured4ec15e2014-07-13 21:31:05 -03002002 next = t->from_parent;
2003
2004 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2005 "send failed reply for transaction %d, target dead\n",
2006 t->debug_id);
2007
Todd Kjosb6d282c2017-06-29 12:01:54 -07002008 binder_free_transaction(t);
Lucas Tanured4ec15e2014-07-13 21:31:05 -03002009 if (next == NULL) {
2010 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2011 "reply failed, no target thread at root\n");
2012 return;
2013 }
2014 t = next;
2015 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2016 "reply failed, no target thread -- retry %d\n",
2017 t->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002018 }
2019}
2020
Martijn Coenenfeba3902017-02-03 14:40:45 -08002021/**
Martijn Coenenfb2c4452017-11-13 10:06:08 +01002022 * binder_cleanup_transaction() - cleans up undelivered transaction
2023 * @t: transaction that needs to be cleaned up
2024 * @reason: reason the transaction wasn't delivered
2025 * @error_code: error to return to caller (if synchronous call)
2026 */
2027static void binder_cleanup_transaction(struct binder_transaction *t,
2028 const char *reason,
2029 uint32_t error_code)
2030{
2031 if (t->buffer->target_node && !(t->flags & TF_ONE_WAY)) {
2032 binder_send_failed_reply(t, error_code);
2033 } else {
2034 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2035 "undelivered transaction %d, %s\n",
2036 t->debug_id, reason);
2037 binder_free_transaction(t);
2038 }
2039}
2040
2041/**
Martijn Coenenfeba3902017-02-03 14:40:45 -08002042 * binder_validate_object() - checks for a valid metadata object in a buffer.
2043 * @buffer: binder_buffer that we're parsing.
2044 * @offset: offset in the buffer at which to validate an object.
2045 *
2046 * Return: If there's a valid metadata object at @offset in @buffer, the
2047 * size of that object. Otherwise, it returns zero.
2048 */
2049static size_t binder_validate_object(struct binder_buffer *buffer, u64 offset)
2050{
2051 /* Check if we can read a header first */
2052 struct binder_object_header *hdr;
2053 size_t object_size = 0;
2054
Dan Carpenter361f2dd2018-03-29 12:14:40 +03002055 if (buffer->data_size < sizeof(*hdr) ||
2056 offset > buffer->data_size - sizeof(*hdr) ||
Martijn Coenenfeba3902017-02-03 14:40:45 -08002057 !IS_ALIGNED(offset, sizeof(u32)))
2058 return 0;
2059
2060 /* Ok, now see if we can read a complete object. */
2061 hdr = (struct binder_object_header *)(buffer->data + offset);
2062 switch (hdr->type) {
2063 case BINDER_TYPE_BINDER:
2064 case BINDER_TYPE_WEAK_BINDER:
2065 case BINDER_TYPE_HANDLE:
2066 case BINDER_TYPE_WEAK_HANDLE:
2067 object_size = sizeof(struct flat_binder_object);
2068 break;
2069 case BINDER_TYPE_FD:
2070 object_size = sizeof(struct binder_fd_object);
2071 break;
Martijn Coenen79802402017-02-03 14:40:51 -08002072 case BINDER_TYPE_PTR:
2073 object_size = sizeof(struct binder_buffer_object);
2074 break;
Martijn Coenendef95c72017-02-03 14:40:52 -08002075 case BINDER_TYPE_FDA:
2076 object_size = sizeof(struct binder_fd_array_object);
2077 break;
Martijn Coenenfeba3902017-02-03 14:40:45 -08002078 default:
2079 return 0;
2080 }
2081 if (offset <= buffer->data_size - object_size &&
2082 buffer->data_size >= object_size)
2083 return object_size;
2084 else
2085 return 0;
2086}
2087
Martijn Coenen79802402017-02-03 14:40:51 -08002088/**
2089 * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
2090 * @b: binder_buffer containing the object
2091 * @index: index in offset array at which the binder_buffer_object is
2092 * located
2093 * @start: points to the start of the offset array
2094 * @num_valid: the number of valid offsets in the offset array
2095 *
2096 * Return: If @index is within the valid range of the offset array
2097 * described by @start and @num_valid, and if there's a valid
2098 * binder_buffer_object at the offset found in index @index
2099 * of the offset array, that object is returned. Otherwise,
2100 * %NULL is returned.
2101 * Note that the offset found in index @index itself is not
2102 * verified; this function assumes that @num_valid elements
2103 * from @start were previously verified to have valid offsets.
2104 */
2105static struct binder_buffer_object *binder_validate_ptr(struct binder_buffer *b,
2106 binder_size_t index,
2107 binder_size_t *start,
2108 binder_size_t num_valid)
2109{
2110 struct binder_buffer_object *buffer_obj;
2111 binder_size_t *offp;
2112
2113 if (index >= num_valid)
2114 return NULL;
2115
2116 offp = start + index;
2117 buffer_obj = (struct binder_buffer_object *)(b->data + *offp);
2118 if (buffer_obj->hdr.type != BINDER_TYPE_PTR)
2119 return NULL;
2120
2121 return buffer_obj;
2122}
2123
2124/**
2125 * binder_validate_fixup() - validates pointer/fd fixups happen in order.
2126 * @b: transaction buffer
2127 * @objects_start start of objects buffer
2128 * @buffer: binder_buffer_object in which to fix up
2129 * @offset: start offset in @buffer to fix up
2130 * @last_obj: last binder_buffer_object that we fixed up in
2131 * @last_min_offset: minimum fixup offset in @last_obj
2132 *
2133 * Return: %true if a fixup in buffer @buffer at offset @offset is
2134 * allowed.
2135 *
2136 * For safety reasons, we only allow fixups inside a buffer to happen
2137 * at increasing offsets; additionally, we only allow fixup on the last
2138 * buffer object that was verified, or one of its parents.
2139 *
2140 * Example of what is allowed:
2141 *
2142 * A
2143 * B (parent = A, offset = 0)
2144 * C (parent = A, offset = 16)
2145 * D (parent = C, offset = 0)
2146 * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
2147 *
2148 * Examples of what is not allowed:
2149 *
2150 * Decreasing offsets within the same parent:
2151 * A
2152 * C (parent = A, offset = 16)
2153 * B (parent = A, offset = 0) // decreasing offset within A
2154 *
2155 * Referring to a parent that wasn't the last object or any of its parents:
2156 * A
2157 * B (parent = A, offset = 0)
2158 * C (parent = A, offset = 0)
2159 * C (parent = A, offset = 16)
2160 * D (parent = B, offset = 0) // B is not A or any of A's parents
2161 */
2162static bool binder_validate_fixup(struct binder_buffer *b,
2163 binder_size_t *objects_start,
2164 struct binder_buffer_object *buffer,
2165 binder_size_t fixup_offset,
2166 struct binder_buffer_object *last_obj,
2167 binder_size_t last_min_offset)
2168{
2169 if (!last_obj) {
2170 /* Nothing to fix up in */
2171 return false;
2172 }
2173
2174 while (last_obj != buffer) {
2175 /*
2176 * Safe to retrieve the parent of last_obj, since it
2177 * was already previously verified by the driver.
2178 */
2179 if ((last_obj->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0)
2180 return false;
2181 last_min_offset = last_obj->parent_offset + sizeof(uintptr_t);
2182 last_obj = (struct binder_buffer_object *)
2183 (b->data + *(objects_start + last_obj->parent));
2184 }
2185 return (fixup_offset >= last_min_offset);
2186}
2187
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002188static void binder_transaction_buffer_release(struct binder_proc *proc,
2189 struct binder_buffer *buffer,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002190 binder_size_t *failed_at)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002191{
Martijn Coenen79802402017-02-03 14:40:51 -08002192 binder_size_t *offp, *off_start, *off_end;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002193 int debug_id = buffer->debug_id;
2194
2195 binder_debug(BINDER_DEBUG_TRANSACTION,
Todd Kjos8ca86f12018-02-07 13:57:37 -08002196 "%d buffer release %d, size %zd-%zd, failed at %pK\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002197 proc->pid, buffer->debug_id,
2198 buffer->data_size, buffer->offsets_size, failed_at);
2199
2200 if (buffer->target_node)
2201 binder_dec_node(buffer->target_node, 1, 0);
2202
Martijn Coenen79802402017-02-03 14:40:51 -08002203 off_start = (binder_size_t *)(buffer->data +
2204 ALIGN(buffer->data_size, sizeof(void *)));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002205 if (failed_at)
2206 off_end = failed_at;
2207 else
Martijn Coenen79802402017-02-03 14:40:51 -08002208 off_end = (void *)off_start + buffer->offsets_size;
2209 for (offp = off_start; offp < off_end; offp++) {
Martijn Coenenfeba3902017-02-03 14:40:45 -08002210 struct binder_object_header *hdr;
2211 size_t object_size = binder_validate_object(buffer, *offp);
Seunghun Lee10f62862014-05-01 01:30:23 +09002212
Martijn Coenenfeba3902017-02-03 14:40:45 -08002213 if (object_size == 0) {
2214 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
Arve Hjønnevågda498892014-02-21 14:40:26 -08002215 debug_id, (u64)*offp, buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002216 continue;
2217 }
Martijn Coenenfeba3902017-02-03 14:40:45 -08002218 hdr = (struct binder_object_header *)(buffer->data + *offp);
2219 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002220 case BINDER_TYPE_BINDER:
2221 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08002222 struct flat_binder_object *fp;
2223 struct binder_node *node;
Seunghun Lee10f62862014-05-01 01:30:23 +09002224
Martijn Coenenfeba3902017-02-03 14:40:45 -08002225 fp = to_flat_binder_object(hdr);
2226 node = binder_get_node(proc, fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002227 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002228 pr_err("transaction release %d bad node %016llx\n",
2229 debug_id, (u64)fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002230 break;
2231 }
2232 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002233 " node %d u%016llx\n",
2234 node->debug_id, (u64)node->ptr);
Martijn Coenenfeba3902017-02-03 14:40:45 -08002235 binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER,
2236 0);
Todd Kjosadc18842017-06-29 12:01:59 -07002237 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002238 } break;
2239 case BINDER_TYPE_HANDLE:
2240 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08002241 struct flat_binder_object *fp;
Todd Kjos372e3142017-06-29 12:01:58 -07002242 struct binder_ref_data rdata;
2243 int ret;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02002244
Martijn Coenenfeba3902017-02-03 14:40:45 -08002245 fp = to_flat_binder_object(hdr);
Todd Kjos372e3142017-06-29 12:01:58 -07002246 ret = binder_dec_ref_for_handle(proc, fp->handle,
2247 hdr->type == BINDER_TYPE_HANDLE, &rdata);
2248
2249 if (ret) {
2250 pr_err("transaction release %d bad handle %d, ret = %d\n",
2251 debug_id, fp->handle, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002252 break;
2253 }
2254 binder_debug(BINDER_DEBUG_TRANSACTION,
Todd Kjos372e3142017-06-29 12:01:58 -07002255 " ref %d desc %d\n",
2256 rdata.debug_id, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002257 } break;
2258
Martijn Coenenfeba3902017-02-03 14:40:45 -08002259 case BINDER_TYPE_FD: {
2260 struct binder_fd_object *fp = to_binder_fd_object(hdr);
2261
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002262 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenenfeba3902017-02-03 14:40:45 -08002263 " fd %d\n", fp->fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002264 if (failed_at)
Martijn Coenenfeba3902017-02-03 14:40:45 -08002265 task_close_fd(proc, fp->fd);
2266 } break;
Martijn Coenen79802402017-02-03 14:40:51 -08002267 case BINDER_TYPE_PTR:
2268 /*
2269 * Nothing to do here, this will get cleaned up when the
2270 * transaction buffer gets freed
2271 */
2272 break;
Martijn Coenendef95c72017-02-03 14:40:52 -08002273 case BINDER_TYPE_FDA: {
2274 struct binder_fd_array_object *fda;
2275 struct binder_buffer_object *parent;
2276 uintptr_t parent_buffer;
2277 u32 *fd_array;
2278 size_t fd_index;
2279 binder_size_t fd_buf_size;
2280
2281 fda = to_binder_fd_array_object(hdr);
2282 parent = binder_validate_ptr(buffer, fda->parent,
2283 off_start,
2284 offp - off_start);
2285 if (!parent) {
Arvind Yadavf7f84fd2017-09-25 12:52:11 +05302286 pr_err("transaction release %d bad parent offset\n",
Martijn Coenendef95c72017-02-03 14:40:52 -08002287 debug_id);
2288 continue;
2289 }
2290 /*
2291 * Since the parent was already fixed up, convert it
2292 * back to kernel address space to access it
2293 */
2294 parent_buffer = parent->buffer -
Todd Kjos19c98722017-06-29 12:01:40 -07002295 binder_alloc_get_user_buffer_offset(
2296 &proc->alloc);
Martijn Coenendef95c72017-02-03 14:40:52 -08002297
2298 fd_buf_size = sizeof(u32) * fda->num_fds;
2299 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2300 pr_err("transaction release %d invalid number of fds (%lld)\n",
2301 debug_id, (u64)fda->num_fds);
2302 continue;
2303 }
2304 if (fd_buf_size > parent->length ||
2305 fda->parent_offset > parent->length - fd_buf_size) {
2306 /* No space for all file descriptors here. */
2307 pr_err("transaction release %d not enough space for %lld fds in buffer\n",
2308 debug_id, (u64)fda->num_fds);
2309 continue;
2310 }
Arnd Bergmann1c363eae2017-09-05 10:56:13 +02002311 fd_array = (u32 *)(parent_buffer + (uintptr_t)fda->parent_offset);
Martijn Coenendef95c72017-02-03 14:40:52 -08002312 for (fd_index = 0; fd_index < fda->num_fds; fd_index++)
2313 task_close_fd(proc, fd_array[fd_index]);
2314 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002315 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01002316 pr_err("transaction release %d bad object type %x\n",
Martijn Coenenfeba3902017-02-03 14:40:45 -08002317 debug_id, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002318 break;
2319 }
2320 }
2321}
2322
Martijn Coenena056af42017-02-03 14:40:49 -08002323static int binder_translate_binder(struct flat_binder_object *fp,
2324 struct binder_transaction *t,
2325 struct binder_thread *thread)
2326{
2327 struct binder_node *node;
Martijn Coenena056af42017-02-03 14:40:49 -08002328 struct binder_proc *proc = thread->proc;
2329 struct binder_proc *target_proc = t->to_proc;
Todd Kjos372e3142017-06-29 12:01:58 -07002330 struct binder_ref_data rdata;
Todd Kjosadc18842017-06-29 12:01:59 -07002331 int ret = 0;
Martijn Coenena056af42017-02-03 14:40:49 -08002332
2333 node = binder_get_node(proc, fp->binder);
2334 if (!node) {
Todd Kjos673068e2017-06-29 12:02:03 -07002335 node = binder_new_node(proc, fp);
Martijn Coenena056af42017-02-03 14:40:49 -08002336 if (!node)
2337 return -ENOMEM;
Martijn Coenena056af42017-02-03 14:40:49 -08002338 }
2339 if (fp->cookie != node->cookie) {
2340 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
2341 proc->pid, thread->pid, (u64)fp->binder,
2342 node->debug_id, (u64)fp->cookie,
2343 (u64)node->cookie);
Todd Kjosadc18842017-06-29 12:01:59 -07002344 ret = -EINVAL;
2345 goto done;
Martijn Coenena056af42017-02-03 14:40:49 -08002346 }
Todd Kjosadc18842017-06-29 12:01:59 -07002347 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2348 ret = -EPERM;
2349 goto done;
2350 }
Martijn Coenena056af42017-02-03 14:40:49 -08002351
Todd Kjos372e3142017-06-29 12:01:58 -07002352 ret = binder_inc_ref_for_node(target_proc, node,
2353 fp->hdr.type == BINDER_TYPE_BINDER,
2354 &thread->todo, &rdata);
2355 if (ret)
Todd Kjosadc18842017-06-29 12:01:59 -07002356 goto done;
Martijn Coenena056af42017-02-03 14:40:49 -08002357
2358 if (fp->hdr.type == BINDER_TYPE_BINDER)
2359 fp->hdr.type = BINDER_TYPE_HANDLE;
2360 else
2361 fp->hdr.type = BINDER_TYPE_WEAK_HANDLE;
2362 fp->binder = 0;
Todd Kjos372e3142017-06-29 12:01:58 -07002363 fp->handle = rdata.desc;
Martijn Coenena056af42017-02-03 14:40:49 -08002364 fp->cookie = 0;
Martijn Coenena056af42017-02-03 14:40:49 -08002365
Todd Kjos372e3142017-06-29 12:01:58 -07002366 trace_binder_transaction_node_to_ref(t, node, &rdata);
Martijn Coenena056af42017-02-03 14:40:49 -08002367 binder_debug(BINDER_DEBUG_TRANSACTION,
2368 " node %d u%016llx -> ref %d desc %d\n",
2369 node->debug_id, (u64)node->ptr,
Todd Kjos372e3142017-06-29 12:01:58 -07002370 rdata.debug_id, rdata.desc);
Todd Kjosadc18842017-06-29 12:01:59 -07002371done:
2372 binder_put_node(node);
2373 return ret;
Martijn Coenena056af42017-02-03 14:40:49 -08002374}
2375
2376static int binder_translate_handle(struct flat_binder_object *fp,
2377 struct binder_transaction *t,
2378 struct binder_thread *thread)
2379{
Martijn Coenena056af42017-02-03 14:40:49 -08002380 struct binder_proc *proc = thread->proc;
2381 struct binder_proc *target_proc = t->to_proc;
Todd Kjos372e3142017-06-29 12:01:58 -07002382 struct binder_node *node;
2383 struct binder_ref_data src_rdata;
Todd Kjosadc18842017-06-29 12:01:59 -07002384 int ret = 0;
Martijn Coenena056af42017-02-03 14:40:49 -08002385
Todd Kjos372e3142017-06-29 12:01:58 -07002386 node = binder_get_node_from_ref(proc, fp->handle,
2387 fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata);
2388 if (!node) {
Martijn Coenena056af42017-02-03 14:40:49 -08002389 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
2390 proc->pid, thread->pid, fp->handle);
2391 return -EINVAL;
2392 }
Todd Kjosadc18842017-06-29 12:01:59 -07002393 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2394 ret = -EPERM;
2395 goto done;
2396 }
Martijn Coenena056af42017-02-03 14:40:49 -08002397
Todd Kjos673068e2017-06-29 12:02:03 -07002398 binder_node_lock(node);
Todd Kjos372e3142017-06-29 12:01:58 -07002399 if (node->proc == target_proc) {
Martijn Coenena056af42017-02-03 14:40:49 -08002400 if (fp->hdr.type == BINDER_TYPE_HANDLE)
2401 fp->hdr.type = BINDER_TYPE_BINDER;
2402 else
2403 fp->hdr.type = BINDER_TYPE_WEAK_BINDER;
Todd Kjos372e3142017-06-29 12:01:58 -07002404 fp->binder = node->ptr;
2405 fp->cookie = node->cookie;
Todd Kjos673068e2017-06-29 12:02:03 -07002406 if (node->proc)
2407 binder_inner_proc_lock(node->proc);
2408 binder_inc_node_nilocked(node,
2409 fp->hdr.type == BINDER_TYPE_BINDER,
2410 0, NULL);
2411 if (node->proc)
2412 binder_inner_proc_unlock(node->proc);
Todd Kjos372e3142017-06-29 12:01:58 -07002413 trace_binder_transaction_ref_to_node(t, node, &src_rdata);
Martijn Coenena056af42017-02-03 14:40:49 -08002414 binder_debug(BINDER_DEBUG_TRANSACTION,
2415 " ref %d desc %d -> node %d u%016llx\n",
Todd Kjos372e3142017-06-29 12:01:58 -07002416 src_rdata.debug_id, src_rdata.desc, node->debug_id,
2417 (u64)node->ptr);
Todd Kjos673068e2017-06-29 12:02:03 -07002418 binder_node_unlock(node);
Martijn Coenena056af42017-02-03 14:40:49 -08002419 } else {
Todd Kjos372e3142017-06-29 12:01:58 -07002420 struct binder_ref_data dest_rdata;
Martijn Coenena056af42017-02-03 14:40:49 -08002421
Todd Kjos673068e2017-06-29 12:02:03 -07002422 binder_node_unlock(node);
Todd Kjos372e3142017-06-29 12:01:58 -07002423 ret = binder_inc_ref_for_node(target_proc, node,
2424 fp->hdr.type == BINDER_TYPE_HANDLE,
2425 NULL, &dest_rdata);
2426 if (ret)
Todd Kjosadc18842017-06-29 12:01:59 -07002427 goto done;
Martijn Coenena056af42017-02-03 14:40:49 -08002428
2429 fp->binder = 0;
Todd Kjos372e3142017-06-29 12:01:58 -07002430 fp->handle = dest_rdata.desc;
Martijn Coenena056af42017-02-03 14:40:49 -08002431 fp->cookie = 0;
Todd Kjos372e3142017-06-29 12:01:58 -07002432 trace_binder_transaction_ref_to_ref(t, node, &src_rdata,
2433 &dest_rdata);
Martijn Coenena056af42017-02-03 14:40:49 -08002434 binder_debug(BINDER_DEBUG_TRANSACTION,
2435 " ref %d desc %d -> ref %d desc %d (node %d)\n",
Todd Kjos372e3142017-06-29 12:01:58 -07002436 src_rdata.debug_id, src_rdata.desc,
2437 dest_rdata.debug_id, dest_rdata.desc,
2438 node->debug_id);
Martijn Coenena056af42017-02-03 14:40:49 -08002439 }
Todd Kjosadc18842017-06-29 12:01:59 -07002440done:
2441 binder_put_node(node);
2442 return ret;
Martijn Coenena056af42017-02-03 14:40:49 -08002443}
2444
2445static int binder_translate_fd(int fd,
2446 struct binder_transaction *t,
2447 struct binder_thread *thread,
2448 struct binder_transaction *in_reply_to)
2449{
2450 struct binder_proc *proc = thread->proc;
2451 struct binder_proc *target_proc = t->to_proc;
2452 int target_fd;
2453 struct file *file;
2454 int ret;
2455 bool target_allows_fd;
2456
2457 if (in_reply_to)
2458 target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS);
2459 else
2460 target_allows_fd = t->buffer->target_node->accept_fds;
2461 if (!target_allows_fd) {
2462 binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
2463 proc->pid, thread->pid,
2464 in_reply_to ? "reply" : "transaction",
2465 fd);
2466 ret = -EPERM;
2467 goto err_fd_not_accepted;
2468 }
2469
2470 file = fget(fd);
2471 if (!file) {
2472 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
2473 proc->pid, thread->pid, fd);
2474 ret = -EBADF;
2475 goto err_fget;
2476 }
2477 ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file);
2478 if (ret < 0) {
2479 ret = -EPERM;
2480 goto err_security;
2481 }
2482
2483 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
2484 if (target_fd < 0) {
2485 ret = -ENOMEM;
2486 goto err_get_unused_fd;
2487 }
2488 task_fd_install(target_proc, target_fd, file);
2489 trace_binder_transaction_fd(t, fd, target_fd);
2490 binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n",
2491 fd, target_fd);
2492
2493 return target_fd;
2494
2495err_get_unused_fd:
2496err_security:
2497 fput(file);
2498err_fget:
2499err_fd_not_accepted:
2500 return ret;
2501}
2502
Martijn Coenendef95c72017-02-03 14:40:52 -08002503static int binder_translate_fd_array(struct binder_fd_array_object *fda,
2504 struct binder_buffer_object *parent,
2505 struct binder_transaction *t,
2506 struct binder_thread *thread,
2507 struct binder_transaction *in_reply_to)
2508{
2509 binder_size_t fdi, fd_buf_size, num_installed_fds;
2510 int target_fd;
2511 uintptr_t parent_buffer;
2512 u32 *fd_array;
2513 struct binder_proc *proc = thread->proc;
2514 struct binder_proc *target_proc = t->to_proc;
2515
2516 fd_buf_size = sizeof(u32) * fda->num_fds;
2517 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2518 binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
2519 proc->pid, thread->pid, (u64)fda->num_fds);
2520 return -EINVAL;
2521 }
2522 if (fd_buf_size > parent->length ||
2523 fda->parent_offset > parent->length - fd_buf_size) {
2524 /* No space for all file descriptors here. */
2525 binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
2526 proc->pid, thread->pid, (u64)fda->num_fds);
2527 return -EINVAL;
2528 }
2529 /*
2530 * Since the parent was already fixed up, convert it
2531 * back to the kernel address space to access it
2532 */
Todd Kjos19c98722017-06-29 12:01:40 -07002533 parent_buffer = parent->buffer -
2534 binder_alloc_get_user_buffer_offset(&target_proc->alloc);
Arnd Bergmann1c363eae2017-09-05 10:56:13 +02002535 fd_array = (u32 *)(parent_buffer + (uintptr_t)fda->parent_offset);
Martijn Coenendef95c72017-02-03 14:40:52 -08002536 if (!IS_ALIGNED((unsigned long)fd_array, sizeof(u32))) {
2537 binder_user_error("%d:%d parent offset not aligned correctly.\n",
2538 proc->pid, thread->pid);
2539 return -EINVAL;
2540 }
2541 for (fdi = 0; fdi < fda->num_fds; fdi++) {
2542 target_fd = binder_translate_fd(fd_array[fdi], t, thread,
2543 in_reply_to);
2544 if (target_fd < 0)
2545 goto err_translate_fd_failed;
2546 fd_array[fdi] = target_fd;
2547 }
2548 return 0;
2549
2550err_translate_fd_failed:
2551 /*
2552 * Failed to allocate fd or security error, free fds
2553 * installed so far.
2554 */
2555 num_installed_fds = fdi;
2556 for (fdi = 0; fdi < num_installed_fds; fdi++)
2557 task_close_fd(target_proc, fd_array[fdi]);
2558 return target_fd;
2559}
2560
Martijn Coenen79802402017-02-03 14:40:51 -08002561static int binder_fixup_parent(struct binder_transaction *t,
2562 struct binder_thread *thread,
2563 struct binder_buffer_object *bp,
2564 binder_size_t *off_start,
2565 binder_size_t num_valid,
2566 struct binder_buffer_object *last_fixup_obj,
2567 binder_size_t last_fixup_min_off)
2568{
2569 struct binder_buffer_object *parent;
2570 u8 *parent_buffer;
2571 struct binder_buffer *b = t->buffer;
2572 struct binder_proc *proc = thread->proc;
2573 struct binder_proc *target_proc = t->to_proc;
2574
2575 if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT))
2576 return 0;
2577
2578 parent = binder_validate_ptr(b, bp->parent, off_start, num_valid);
2579 if (!parent) {
2580 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
2581 proc->pid, thread->pid);
2582 return -EINVAL;
2583 }
2584
2585 if (!binder_validate_fixup(b, off_start,
2586 parent, bp->parent_offset,
2587 last_fixup_obj,
2588 last_fixup_min_off)) {
2589 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
2590 proc->pid, thread->pid);
2591 return -EINVAL;
2592 }
2593
2594 if (parent->length < sizeof(binder_uintptr_t) ||
2595 bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) {
2596 /* No space for a pointer here! */
2597 binder_user_error("%d:%d got transaction with invalid parent offset\n",
2598 proc->pid, thread->pid);
2599 return -EINVAL;
2600 }
Arnd Bergmann1c363eae2017-09-05 10:56:13 +02002601 parent_buffer = (u8 *)((uintptr_t)parent->buffer -
Todd Kjos19c98722017-06-29 12:01:40 -07002602 binder_alloc_get_user_buffer_offset(
2603 &target_proc->alloc));
Martijn Coenen79802402017-02-03 14:40:51 -08002604 *(binder_uintptr_t *)(parent_buffer + bp->parent_offset) = bp->buffer;
2605
2606 return 0;
2607}
2608
Martijn Coenen408c68b2017-08-31 10:04:19 +02002609/**
2610 * binder_proc_transaction() - sends a transaction to a process and wakes it up
2611 * @t: transaction to send
2612 * @proc: process to send the transaction to
2613 * @thread: thread in @proc to send the transaction to (may be NULL)
2614 *
2615 * This function queues a transaction to the specified process. It will try
2616 * to find a thread in the target process to handle the transaction and
2617 * wake it up. If no thread is found, the work is queued to the proc
2618 * waitqueue.
2619 *
2620 * If the @thread parameter is not NULL, the transaction is always queued
2621 * to the waitlist of that specific thread.
2622 *
2623 * Return: true if the transactions was successfully queued
2624 * false if the target process or thread is dead
2625 */
2626static bool binder_proc_transaction(struct binder_transaction *t,
2627 struct binder_proc *proc,
2628 struct binder_thread *thread)
2629{
Martijn Coenen408c68b2017-08-31 10:04:19 +02002630 struct binder_node *node = t->buffer->target_node;
2631 bool oneway = !!(t->flags & TF_ONE_WAY);
Martijn Coenen148ade22017-11-15 09:21:35 +01002632 bool pending_async = false;
Martijn Coenen408c68b2017-08-31 10:04:19 +02002633
2634 BUG_ON(!node);
2635 binder_node_lock(node);
2636 if (oneway) {
2637 BUG_ON(thread);
2638 if (node->has_async_transaction) {
Martijn Coenen148ade22017-11-15 09:21:35 +01002639 pending_async = true;
Martijn Coenen408c68b2017-08-31 10:04:19 +02002640 } else {
Gustavo A. R. Silva197410a2018-01-23 12:04:27 -06002641 node->has_async_transaction = true;
Martijn Coenen408c68b2017-08-31 10:04:19 +02002642 }
2643 }
2644
2645 binder_inner_proc_lock(proc);
2646
2647 if (proc->is_dead || (thread && thread->is_dead)) {
2648 binder_inner_proc_unlock(proc);
2649 binder_node_unlock(node);
2650 return false;
2651 }
2652
Martijn Coenen148ade22017-11-15 09:21:35 +01002653 if (!thread && !pending_async)
Martijn Coenen408c68b2017-08-31 10:04:19 +02002654 thread = binder_select_thread_ilocked(proc);
2655
2656 if (thread)
Martijn Coenen148ade22017-11-15 09:21:35 +01002657 binder_enqueue_thread_work_ilocked(thread, &t->work);
2658 else if (!pending_async)
2659 binder_enqueue_work_ilocked(&t->work, &proc->todo);
Martijn Coenen408c68b2017-08-31 10:04:19 +02002660 else
Martijn Coenen148ade22017-11-15 09:21:35 +01002661 binder_enqueue_work_ilocked(&t->work, &node->async_todo);
Martijn Coenen408c68b2017-08-31 10:04:19 +02002662
Martijn Coenen148ade22017-11-15 09:21:35 +01002663 if (!pending_async)
Martijn Coenen408c68b2017-08-31 10:04:19 +02002664 binder_wakeup_thread_ilocked(proc, thread, !oneway /* sync */);
2665
2666 binder_inner_proc_unlock(proc);
2667 binder_node_unlock(node);
2668
2669 return true;
2670}
2671
Todd Kjos512cf462017-09-29 15:39:49 -07002672/**
2673 * binder_get_node_refs_for_txn() - Get required refs on node for txn
2674 * @node: struct binder_node for which to get refs
2675 * @proc: returns @node->proc if valid
2676 * @error: if no @proc then returns BR_DEAD_REPLY
2677 *
2678 * User-space normally keeps the node alive when creating a transaction
2679 * since it has a reference to the target. The local strong ref keeps it
2680 * alive if the sending process dies before the target process processes
2681 * the transaction. If the source process is malicious or has a reference
2682 * counting bug, relying on the local strong ref can fail.
2683 *
2684 * Since user-space can cause the local strong ref to go away, we also take
2685 * a tmpref on the node to ensure it survives while we are constructing
2686 * the transaction. We also need a tmpref on the proc while we are
2687 * constructing the transaction, so we take that here as well.
2688 *
2689 * Return: The target_node with refs taken or NULL if no @node->proc is NULL.
2690 * Also sets @proc if valid. If the @node->proc is NULL indicating that the
2691 * target proc has died, @error is set to BR_DEAD_REPLY
2692 */
2693static struct binder_node *binder_get_node_refs_for_txn(
2694 struct binder_node *node,
2695 struct binder_proc **procp,
2696 uint32_t *error)
2697{
2698 struct binder_node *target_node = NULL;
2699
2700 binder_node_inner_lock(node);
2701 if (node->proc) {
2702 target_node = node;
2703 binder_inc_node_nilocked(node, 1, 0, NULL);
2704 binder_inc_node_tmpref_ilocked(node);
2705 node->proc->tmp_ref++;
2706 *procp = node->proc;
2707 } else
2708 *error = BR_DEAD_REPLY;
2709 binder_node_inner_unlock(node);
2710
2711 return target_node;
2712}
2713
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002714static void binder_transaction(struct binder_proc *proc,
2715 struct binder_thread *thread,
Martijn Coenen4bfac802017-02-03 14:40:50 -08002716 struct binder_transaction_data *tr, int reply,
2717 binder_size_t extra_buffers_size)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002718{
Martijn Coenena056af42017-02-03 14:40:49 -08002719 int ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002720 struct binder_transaction *t;
Sherry Yang44b73962018-08-13 17:28:53 -07002721 struct binder_work *w;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002722 struct binder_work *tcomplete;
Martijn Coenen79802402017-02-03 14:40:51 -08002723 binder_size_t *offp, *off_end, *off_start;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08002724 binder_size_t off_min;
Martijn Coenen79802402017-02-03 14:40:51 -08002725 u8 *sg_bufp, *sg_buf_end;
Todd Kjos7a4408c2017-06-29 12:01:57 -07002726 struct binder_proc *target_proc = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002727 struct binder_thread *target_thread = NULL;
2728 struct binder_node *target_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002729 struct binder_transaction *in_reply_to = NULL;
2730 struct binder_transaction_log_entry *e;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002731 uint32_t return_error = 0;
2732 uint32_t return_error_param = 0;
2733 uint32_t return_error_line = 0;
Martijn Coenen79802402017-02-03 14:40:51 -08002734 struct binder_buffer_object *last_fixup_obj = NULL;
2735 binder_size_t last_fixup_min_off = 0;
Martijn Coenen342e5c92017-02-03 14:40:46 -08002736 struct binder_context *context = proc->context;
Todd Kjosd99c7332017-06-29 12:01:53 -07002737 int t_debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002738
2739 e = binder_transaction_log_add(&binder_transaction_log);
Todd Kjosd99c7332017-06-29 12:01:53 -07002740 e->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002741 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
2742 e->from_proc = proc->pid;
2743 e->from_thread = thread->pid;
2744 e->target_handle = tr->target.handle;
2745 e->data_size = tr->data_size;
2746 e->offsets_size = tr->offsets_size;
Martijn Coenen14db3182017-02-03 14:40:47 -08002747 e->context_name = proc->context->name;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002748
2749 if (reply) {
Martijn Coenen0b89d692017-06-29 12:02:06 -07002750 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002751 in_reply_to = thread->transaction_stack;
2752 if (in_reply_to == NULL) {
Martijn Coenen0b89d692017-06-29 12:02:06 -07002753 binder_inner_proc_unlock(proc);
Anmol Sarma56b468f2012-10-30 22:35:43 +05302754 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002755 proc->pid, thread->pid);
2756 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002757 return_error_param = -EPROTO;
2758 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002759 goto err_empty_call_stack;
2760 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002761 if (in_reply_to->to_thread != thread) {
Todd Kjos7a4408c2017-06-29 12:01:57 -07002762 spin_lock(&in_reply_to->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05302763 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 +09002764 proc->pid, thread->pid, in_reply_to->debug_id,
2765 in_reply_to->to_proc ?
2766 in_reply_to->to_proc->pid : 0,
2767 in_reply_to->to_thread ?
2768 in_reply_to->to_thread->pid : 0);
Todd Kjos7a4408c2017-06-29 12:01:57 -07002769 spin_unlock(&in_reply_to->lock);
Martijn Coenen0b89d692017-06-29 12:02:06 -07002770 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002771 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002772 return_error_param = -EPROTO;
2773 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002774 in_reply_to = NULL;
2775 goto err_bad_call_stack;
2776 }
2777 thread->transaction_stack = in_reply_to->to_parent;
Martijn Coenen0b89d692017-06-29 12:02:06 -07002778 binder_inner_proc_unlock(proc);
2779 binder_set_nice(in_reply_to->saved_priority);
2780 target_thread = binder_get_txn_from_and_acq_inner(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002781 if (target_thread == NULL) {
2782 return_error = BR_DEAD_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002783 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002784 goto err_dead_binder;
2785 }
2786 if (target_thread->transaction_stack != in_reply_to) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302787 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 +09002788 proc->pid, thread->pid,
2789 target_thread->transaction_stack ?
2790 target_thread->transaction_stack->debug_id : 0,
2791 in_reply_to->debug_id);
Martijn Coenen0b89d692017-06-29 12:02:06 -07002792 binder_inner_proc_unlock(target_thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002793 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002794 return_error_param = -EPROTO;
2795 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002796 in_reply_to = NULL;
2797 target_thread = NULL;
2798 goto err_dead_binder;
2799 }
2800 target_proc = target_thread->proc;
Todd Kjos7a4408c2017-06-29 12:01:57 -07002801 target_proc->tmp_ref++;
Martijn Coenen0b89d692017-06-29 12:02:06 -07002802 binder_inner_proc_unlock(target_thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002803 } else {
2804 if (tr->target.handle) {
2805 struct binder_ref *ref;
Seunghun Lee10f62862014-05-01 01:30:23 +09002806
Todd Kjoseb349832017-06-29 12:01:56 -07002807 /*
2808 * There must already be a strong ref
2809 * on this node. If so, do a strong
2810 * increment on the node to ensure it
2811 * stays alive until the transaction is
2812 * done.
2813 */
Todd Kjos2c1838d2017-06-29 12:02:08 -07002814 binder_proc_lock(proc);
2815 ref = binder_get_ref_olocked(proc, tr->target.handle,
2816 true);
Todd Kjoseb349832017-06-29 12:01:56 -07002817 if (ref) {
Todd Kjos512cf462017-09-29 15:39:49 -07002818 target_node = binder_get_node_refs_for_txn(
2819 ref->node, &target_proc,
2820 &return_error);
2821 } else {
2822 binder_user_error("%d:%d got transaction to invalid handle\n",
2823 proc->pid, thread->pid);
2824 return_error = BR_FAILED_REPLY;
Todd Kjoseb349832017-06-29 12:01:56 -07002825 }
Todd Kjos2c1838d2017-06-29 12:02:08 -07002826 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002827 } else {
Todd Kjosc44b1232017-06-29 12:01:43 -07002828 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen342e5c92017-02-03 14:40:46 -08002829 target_node = context->binder_context_mgr_node;
Todd Kjos512cf462017-09-29 15:39:49 -07002830 if (target_node)
2831 target_node = binder_get_node_refs_for_txn(
2832 target_node, &target_proc,
2833 &return_error);
2834 else
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002835 return_error = BR_DEAD_REPLY;
Todd Kjosc44b1232017-06-29 12:01:43 -07002836 mutex_unlock(&context->context_mgr_node_lock);
Martijn Coenen7aa135f2018-03-28 11:14:50 +02002837 if (target_node && target_proc == proc) {
2838 binder_user_error("%d:%d got transaction to context manager from process owning it\n",
2839 proc->pid, thread->pid);
2840 return_error = BR_FAILED_REPLY;
2841 return_error_param = -EINVAL;
2842 return_error_line = __LINE__;
2843 goto err_invalid_target_handle;
2844 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002845 }
Todd Kjos512cf462017-09-29 15:39:49 -07002846 if (!target_node) {
2847 /*
2848 * return_error is set above
2849 */
2850 return_error_param = -EINVAL;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002851 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002852 goto err_dead_binder;
2853 }
Todd Kjos512cf462017-09-29 15:39:49 -07002854 e->to_node = target_node->debug_id;
Stephen Smalley79af7302015-01-21 10:54:10 -05002855 if (security_binder_transaction(proc->tsk,
2856 target_proc->tsk) < 0) {
2857 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002858 return_error_param = -EPERM;
2859 return_error_line = __LINE__;
Stephen Smalley79af7302015-01-21 10:54:10 -05002860 goto err_invalid_target_handle;
2861 }
Martijn Coenen0b89d692017-06-29 12:02:06 -07002862 binder_inner_proc_lock(proc);
Sherry Yang44b73962018-08-13 17:28:53 -07002863
2864 w = list_first_entry_or_null(&thread->todo,
2865 struct binder_work, entry);
2866 if (!(tr->flags & TF_ONE_WAY) && w &&
2867 w->type == BINDER_WORK_TRANSACTION) {
2868 /*
2869 * Do not allow new outgoing transaction from a
2870 * thread that has a transaction at the head of
2871 * its todo list. Only need to check the head
2872 * because binder_select_thread_ilocked picks a
2873 * thread from proc->waiting_threads to enqueue
2874 * the transaction, and nothing is queued to the
2875 * todo list while the thread is on waiting_threads.
2876 */
2877 binder_user_error("%d:%d new transaction not allowed when there is a transaction on thread todo\n",
2878 proc->pid, thread->pid);
2879 binder_inner_proc_unlock(proc);
2880 return_error = BR_FAILED_REPLY;
2881 return_error_param = -EPROTO;
2882 return_error_line = __LINE__;
2883 goto err_bad_todo_list;
2884 }
2885
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002886 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
2887 struct binder_transaction *tmp;
Seunghun Lee10f62862014-05-01 01:30:23 +09002888
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002889 tmp = thread->transaction_stack;
2890 if (tmp->to_thread != thread) {
Todd Kjos7a4408c2017-06-29 12:01:57 -07002891 spin_lock(&tmp->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05302892 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 +09002893 proc->pid, thread->pid, tmp->debug_id,
2894 tmp->to_proc ? tmp->to_proc->pid : 0,
2895 tmp->to_thread ?
2896 tmp->to_thread->pid : 0);
Todd Kjos7a4408c2017-06-29 12:01:57 -07002897 spin_unlock(&tmp->lock);
Martijn Coenen0b89d692017-06-29 12:02:06 -07002898 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002899 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002900 return_error_param = -EPROTO;
2901 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002902 goto err_bad_call_stack;
2903 }
2904 while (tmp) {
Todd Kjos7a4408c2017-06-29 12:01:57 -07002905 struct binder_thread *from;
2906
2907 spin_lock(&tmp->lock);
2908 from = tmp->from;
2909 if (from && from->proc == target_proc) {
2910 atomic_inc(&from->tmp_ref);
2911 target_thread = from;
2912 spin_unlock(&tmp->lock);
2913 break;
2914 }
2915 spin_unlock(&tmp->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002916 tmp = tmp->from_parent;
2917 }
2918 }
Martijn Coenen0b89d692017-06-29 12:02:06 -07002919 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002920 }
Martijn Coenen408c68b2017-08-31 10:04:19 +02002921 if (target_thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002922 e->to_thread = target_thread->pid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002923 e->to_proc = target_proc->pid;
2924
2925 /* TODO: reuse incoming transaction for reply */
2926 t = kzalloc(sizeof(*t), GFP_KERNEL);
2927 if (t == NULL) {
2928 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002929 return_error_param = -ENOMEM;
2930 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002931 goto err_alloc_t_failed;
2932 }
2933 binder_stats_created(BINDER_STAT_TRANSACTION);
Todd Kjos7a4408c2017-06-29 12:01:57 -07002934 spin_lock_init(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002935
2936 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
2937 if (tcomplete == NULL) {
2938 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002939 return_error_param = -ENOMEM;
2940 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002941 goto err_alloc_tcomplete_failed;
2942 }
2943 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
2944
Todd Kjosd99c7332017-06-29 12:01:53 -07002945 t->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002946
2947 if (reply)
2948 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen4bfac802017-02-03 14:40:50 -08002949 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002950 proc->pid, thread->pid, t->debug_id,
2951 target_proc->pid, target_thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002952 (u64)tr->data.ptr.buffer,
2953 (u64)tr->data.ptr.offsets,
Martijn Coenen4bfac802017-02-03 14:40:50 -08002954 (u64)tr->data_size, (u64)tr->offsets_size,
2955 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002956 else
2957 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen4bfac802017-02-03 14:40:50 -08002958 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002959 proc->pid, thread->pid, t->debug_id,
2960 target_proc->pid, target_node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002961 (u64)tr->data.ptr.buffer,
2962 (u64)tr->data.ptr.offsets,
Martijn Coenen4bfac802017-02-03 14:40:50 -08002963 (u64)tr->data_size, (u64)tr->offsets_size,
2964 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002965
2966 if (!reply && !(tr->flags & TF_ONE_WAY))
2967 t->from = thread;
2968 else
2969 t->from = NULL;
Tair Rzayev57bab7c2014-05-31 22:43:34 +03002970 t->sender_euid = task_euid(proc->tsk);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002971 t->to_proc = target_proc;
2972 t->to_thread = target_thread;
2973 t->code = tr->code;
2974 t->flags = tr->flags;
2975 t->priority = task_nice(current);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002976
2977 trace_binder_transaction(reply, t, target_node);
2978
Todd Kjos19c98722017-06-29 12:01:40 -07002979 t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size,
Martijn Coenen4bfac802017-02-03 14:40:50 -08002980 tr->offsets_size, extra_buffers_size,
2981 !reply && (t->flags & TF_ONE_WAY));
Todd Kjos57ada2f2017-06-29 12:01:46 -07002982 if (IS_ERR(t->buffer)) {
2983 /*
2984 * -ESRCH indicates VMA cleared. The target is dying.
2985 */
2986 return_error_param = PTR_ERR(t->buffer);
2987 return_error = return_error_param == -ESRCH ?
2988 BR_DEAD_REPLY : BR_FAILED_REPLY;
2989 return_error_line = __LINE__;
2990 t->buffer = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002991 goto err_binder_alloc_buf_failed;
2992 }
2993 t->buffer->allow_user_free = 0;
2994 t->buffer->debug_id = t->debug_id;
2995 t->buffer->transaction = t;
2996 t->buffer->target_node = target_node;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002997 trace_binder_transaction_alloc_buf(t->buffer);
Martijn Coenen79802402017-02-03 14:40:51 -08002998 off_start = (binder_size_t *)(t->buffer->data +
2999 ALIGN(tr->data_size, sizeof(void *)));
3000 offp = off_start;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003001
Arve Hjønnevågda498892014-02-21 14:40:26 -08003002 if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t)
3003 tr->data.ptr.buffer, tr->data_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303004 binder_user_error("%d:%d got transaction with invalid data ptr\n",
3005 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003006 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003007 return_error_param = -EFAULT;
3008 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003009 goto err_copy_data_failed;
3010 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08003011 if (copy_from_user(offp, (const void __user *)(uintptr_t)
3012 tr->data.ptr.offsets, tr->offsets_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303013 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3014 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003015 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003016 return_error_param = -EFAULT;
3017 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003018 goto err_copy_data_failed;
3019 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08003020 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
3021 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
3022 proc->pid, thread->pid, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003023 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003024 return_error_param = -EINVAL;
3025 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003026 goto err_bad_offset;
3027 }
Martijn Coenen79802402017-02-03 14:40:51 -08003028 if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) {
3029 binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
3030 proc->pid, thread->pid,
3031 (u64)extra_buffers_size);
3032 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003033 return_error_param = -EINVAL;
3034 return_error_line = __LINE__;
Martijn Coenen79802402017-02-03 14:40:51 -08003035 goto err_bad_offset;
3036 }
3037 off_end = (void *)off_start + tr->offsets_size;
3038 sg_bufp = (u8 *)(PTR_ALIGN(off_end, sizeof(void *)));
3039 sg_buf_end = sg_bufp + extra_buffers_size;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08003040 off_min = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003041 for (; offp < off_end; offp++) {
Martijn Coenenfeba3902017-02-03 14:40:45 -08003042 struct binder_object_header *hdr;
3043 size_t object_size = binder_validate_object(t->buffer, *offp);
Seunghun Lee10f62862014-05-01 01:30:23 +09003044
Martijn Coenenfeba3902017-02-03 14:40:45 -08003045 if (object_size == 0 || *offp < off_min) {
3046 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 -08003047 proc->pid, thread->pid, (u64)*offp,
3048 (u64)off_min,
Martijn Coenenfeba3902017-02-03 14:40:45 -08003049 (u64)t->buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003050 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003051 return_error_param = -EINVAL;
3052 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003053 goto err_bad_offset;
3054 }
Martijn Coenenfeba3902017-02-03 14:40:45 -08003055
3056 hdr = (struct binder_object_header *)(t->buffer->data + *offp);
3057 off_min = *offp + object_size;
3058 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003059 case BINDER_TYPE_BINDER:
3060 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08003061 struct flat_binder_object *fp;
Seunghun Lee10f62862014-05-01 01:30:23 +09003062
Martijn Coenenfeba3902017-02-03 14:40:45 -08003063 fp = to_flat_binder_object(hdr);
Martijn Coenena056af42017-02-03 14:40:49 -08003064 ret = binder_translate_binder(fp, t, thread);
3065 if (ret < 0) {
Christian Engelmayer7d420432014-05-07 21:44:53 +02003066 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003067 return_error_param = ret;
3068 return_error_line = __LINE__;
Martijn Coenena056af42017-02-03 14:40:49 -08003069 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003070 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003071 } break;
3072 case BINDER_TYPE_HANDLE:
3073 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08003074 struct flat_binder_object *fp;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02003075
Martijn Coenenfeba3902017-02-03 14:40:45 -08003076 fp = to_flat_binder_object(hdr);
Martijn Coenena056af42017-02-03 14:40:49 -08003077 ret = binder_translate_handle(fp, t, thread);
3078 if (ret < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003079 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003080 return_error_param = ret;
3081 return_error_line = __LINE__;
Martijn Coenena056af42017-02-03 14:40:49 -08003082 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003083 }
3084 } break;
3085
3086 case BINDER_TYPE_FD: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08003087 struct binder_fd_object *fp = to_binder_fd_object(hdr);
Martijn Coenena056af42017-02-03 14:40:49 -08003088 int target_fd = binder_translate_fd(fp->fd, t, thread,
3089 in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003090
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003091 if (target_fd < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003092 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003093 return_error_param = target_fd;
3094 return_error_line = __LINE__;
Martijn Coenena056af42017-02-03 14:40:49 -08003095 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003096 }
Martijn Coenenfeba3902017-02-03 14:40:45 -08003097 fp->pad_binder = 0;
3098 fp->fd = target_fd;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003099 } break;
Martijn Coenendef95c72017-02-03 14:40:52 -08003100 case BINDER_TYPE_FDA: {
3101 struct binder_fd_array_object *fda =
3102 to_binder_fd_array_object(hdr);
3103 struct binder_buffer_object *parent =
3104 binder_validate_ptr(t->buffer, fda->parent,
3105 off_start,
3106 offp - off_start);
3107 if (!parent) {
3108 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
3109 proc->pid, thread->pid);
3110 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003111 return_error_param = -EINVAL;
3112 return_error_line = __LINE__;
Martijn Coenendef95c72017-02-03 14:40:52 -08003113 goto err_bad_parent;
3114 }
3115 if (!binder_validate_fixup(t->buffer, off_start,
3116 parent, fda->parent_offset,
3117 last_fixup_obj,
3118 last_fixup_min_off)) {
3119 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
3120 proc->pid, thread->pid);
3121 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003122 return_error_param = -EINVAL;
3123 return_error_line = __LINE__;
Martijn Coenendef95c72017-02-03 14:40:52 -08003124 goto err_bad_parent;
3125 }
3126 ret = binder_translate_fd_array(fda, parent, t, thread,
3127 in_reply_to);
3128 if (ret < 0) {
3129 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003130 return_error_param = ret;
3131 return_error_line = __LINE__;
Martijn Coenendef95c72017-02-03 14:40:52 -08003132 goto err_translate_failed;
3133 }
3134 last_fixup_obj = parent;
3135 last_fixup_min_off =
3136 fda->parent_offset + sizeof(u32) * fda->num_fds;
3137 } break;
Martijn Coenen79802402017-02-03 14:40:51 -08003138 case BINDER_TYPE_PTR: {
3139 struct binder_buffer_object *bp =
3140 to_binder_buffer_object(hdr);
3141 size_t buf_left = sg_buf_end - sg_bufp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003142
Martijn Coenen79802402017-02-03 14:40:51 -08003143 if (bp->length > buf_left) {
3144 binder_user_error("%d:%d got transaction with too large buffer\n",
3145 proc->pid, thread->pid);
3146 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003147 return_error_param = -EINVAL;
3148 return_error_line = __LINE__;
Martijn Coenen79802402017-02-03 14:40:51 -08003149 goto err_bad_offset;
3150 }
3151 if (copy_from_user(sg_bufp,
3152 (const void __user *)(uintptr_t)
3153 bp->buffer, bp->length)) {
3154 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3155 proc->pid, thread->pid);
Todd Kjos57ada2f2017-06-29 12:01:46 -07003156 return_error_param = -EFAULT;
Martijn Coenen79802402017-02-03 14:40:51 -08003157 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003158 return_error_line = __LINE__;
Martijn Coenen79802402017-02-03 14:40:51 -08003159 goto err_copy_data_failed;
3160 }
3161 /* Fixup buffer pointer to target proc address space */
3162 bp->buffer = (uintptr_t)sg_bufp +
Todd Kjos19c98722017-06-29 12:01:40 -07003163 binder_alloc_get_user_buffer_offset(
3164 &target_proc->alloc);
Martijn Coenen79802402017-02-03 14:40:51 -08003165 sg_bufp += ALIGN(bp->length, sizeof(u64));
3166
3167 ret = binder_fixup_parent(t, thread, bp, off_start,
3168 offp - off_start,
3169 last_fixup_obj,
3170 last_fixup_min_off);
3171 if (ret < 0) {
3172 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003173 return_error_param = ret;
3174 return_error_line = __LINE__;
Martijn Coenen79802402017-02-03 14:40:51 -08003175 goto err_translate_failed;
3176 }
3177 last_fixup_obj = bp;
3178 last_fixup_min_off = 0;
3179 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003180 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01003181 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
Martijn Coenenfeba3902017-02-03 14:40:45 -08003182 proc->pid, thread->pid, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003183 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003184 return_error_param = -EINVAL;
3185 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003186 goto err_bad_object_type;
3187 }
3188 }
Todd Kjosccae6f62017-06-29 12:01:48 -07003189 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
Todd Kjos673068e2017-06-29 12:02:03 -07003190 t->work.type = BINDER_WORK_TRANSACTION;
Todd Kjosccae6f62017-06-29 12:01:48 -07003191
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003192 if (reply) {
Martijn Coenen148ade22017-11-15 09:21:35 +01003193 binder_enqueue_thread_work(thread, tcomplete);
Martijn Coenen0b89d692017-06-29 12:02:06 -07003194 binder_inner_proc_lock(target_proc);
3195 if (target_thread->is_dead) {
3196 binder_inner_proc_unlock(target_proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07003197 goto err_dead_proc_or_thread;
Martijn Coenen0b89d692017-06-29 12:02:06 -07003198 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003199 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen0b89d692017-06-29 12:02:06 -07003200 binder_pop_transaction_ilocked(target_thread, in_reply_to);
Martijn Coenen148ade22017-11-15 09:21:35 +01003201 binder_enqueue_thread_work_ilocked(target_thread, &t->work);
Martijn Coenen0b89d692017-06-29 12:02:06 -07003202 binder_inner_proc_unlock(target_proc);
Martijn Coenen408c68b2017-08-31 10:04:19 +02003203 wake_up_interruptible_sync(&target_thread->wait);
Todd Kjosb6d282c2017-06-29 12:01:54 -07003204 binder_free_transaction(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003205 } else if (!(t->flags & TF_ONE_WAY)) {
3206 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen0b89d692017-06-29 12:02:06 -07003207 binder_inner_proc_lock(proc);
Martijn Coenen148ade22017-11-15 09:21:35 +01003208 /*
3209 * Defer the TRANSACTION_COMPLETE, so we don't return to
3210 * userspace immediately; this allows the target process to
3211 * immediately start processing this transaction, reducing
3212 * latency. We will then return the TRANSACTION_COMPLETE when
3213 * the target replies (or there is an error).
3214 */
3215 binder_enqueue_deferred_thread_work_ilocked(thread, tcomplete);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003216 t->need_reply = 1;
3217 t->from_parent = thread->transaction_stack;
3218 thread->transaction_stack = t;
Martijn Coenen0b89d692017-06-29 12:02:06 -07003219 binder_inner_proc_unlock(proc);
Martijn Coenen408c68b2017-08-31 10:04:19 +02003220 if (!binder_proc_transaction(t, target_proc, target_thread)) {
Martijn Coenen0b89d692017-06-29 12:02:06 -07003221 binder_inner_proc_lock(proc);
3222 binder_pop_transaction_ilocked(thread, t);
3223 binder_inner_proc_unlock(proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07003224 goto err_dead_proc_or_thread;
3225 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003226 } else {
3227 BUG_ON(target_node == NULL);
3228 BUG_ON(t->buffer->async_transaction != 1);
Martijn Coenen148ade22017-11-15 09:21:35 +01003229 binder_enqueue_thread_work(thread, tcomplete);
Martijn Coenen408c68b2017-08-31 10:04:19 +02003230 if (!binder_proc_transaction(t, target_proc, NULL))
Todd Kjos7a4408c2017-06-29 12:01:57 -07003231 goto err_dead_proc_or_thread;
Riley Andrews00b40d62017-06-29 12:01:37 -07003232 }
Todd Kjos7a4408c2017-06-29 12:01:57 -07003233 if (target_thread)
3234 binder_thread_dec_tmpref(target_thread);
3235 binder_proc_dec_tmpref(target_proc);
Todd Kjos512cf462017-09-29 15:39:49 -07003236 if (target_node)
3237 binder_dec_node_tmpref(target_node);
Todd Kjosd99c7332017-06-29 12:01:53 -07003238 /*
3239 * write barrier to synchronize with initialization
3240 * of log entry
3241 */
3242 smp_wmb();
3243 WRITE_ONCE(e->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003244 return;
3245
Todd Kjos7a4408c2017-06-29 12:01:57 -07003246err_dead_proc_or_thread:
3247 return_error = BR_DEAD_REPLY;
3248 return_error_line = __LINE__;
Xu YiPingd53bebd2017-09-05 10:21:52 -07003249 binder_dequeue_work(proc, tcomplete);
Martijn Coenena056af42017-02-03 14:40:49 -08003250err_translate_failed:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003251err_bad_object_type:
3252err_bad_offset:
Martijn Coenendef95c72017-02-03 14:40:52 -08003253err_bad_parent:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003254err_copy_data_failed:
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003255 trace_binder_transaction_failed_buffer_release(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003256 binder_transaction_buffer_release(target_proc, t->buffer, offp);
Todd Kjos512cf462017-09-29 15:39:49 -07003257 if (target_node)
3258 binder_dec_node_tmpref(target_node);
Todd Kjoseb349832017-06-29 12:01:56 -07003259 target_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003260 t->buffer->transaction = NULL;
Todd Kjos19c98722017-06-29 12:01:40 -07003261 binder_alloc_free_buf(&target_proc->alloc, t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003262err_binder_alloc_buf_failed:
3263 kfree(tcomplete);
3264 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3265err_alloc_tcomplete_failed:
3266 kfree(t);
3267 binder_stats_deleted(BINDER_STAT_TRANSACTION);
3268err_alloc_t_failed:
Sherry Yang44b73962018-08-13 17:28:53 -07003269err_bad_todo_list:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003270err_bad_call_stack:
3271err_empty_call_stack:
3272err_dead_binder:
3273err_invalid_target_handle:
Todd Kjos7a4408c2017-06-29 12:01:57 -07003274 if (target_thread)
3275 binder_thread_dec_tmpref(target_thread);
3276 if (target_proc)
3277 binder_proc_dec_tmpref(target_proc);
Todd Kjos512cf462017-09-29 15:39:49 -07003278 if (target_node) {
Todd Kjoseb349832017-06-29 12:01:56 -07003279 binder_dec_node(target_node, 1, 0);
Todd Kjos512cf462017-09-29 15:39:49 -07003280 binder_dec_node_tmpref(target_node);
3281 }
Todd Kjoseb349832017-06-29 12:01:56 -07003282
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003283 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Todd Kjos57ada2f2017-06-29 12:01:46 -07003284 "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
3285 proc->pid, thread->pid, return_error, return_error_param,
3286 (u64)tr->data_size, (u64)tr->offsets_size,
3287 return_error_line);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003288
3289 {
3290 struct binder_transaction_log_entry *fe;
Seunghun Lee10f62862014-05-01 01:30:23 +09003291
Todd Kjos57ada2f2017-06-29 12:01:46 -07003292 e->return_error = return_error;
3293 e->return_error_param = return_error_param;
3294 e->return_error_line = return_error_line;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003295 fe = binder_transaction_log_add(&binder_transaction_log_failed);
3296 *fe = *e;
Todd Kjosd99c7332017-06-29 12:01:53 -07003297 /*
3298 * write barrier to synchronize with initialization
3299 * of log entry
3300 */
3301 smp_wmb();
3302 WRITE_ONCE(e->debug_id_done, t_debug_id);
3303 WRITE_ONCE(fe->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003304 }
3305
Todd Kjos26549d12017-06-29 12:01:55 -07003306 BUG_ON(thread->return_error.cmd != BR_OK);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003307 if (in_reply_to) {
Todd Kjos26549d12017-06-29 12:01:55 -07003308 thread->return_error.cmd = BR_TRANSACTION_COMPLETE;
Martijn Coenen148ade22017-11-15 09:21:35 +01003309 binder_enqueue_thread_work(thread, &thread->return_error.work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003310 binder_send_failed_reply(in_reply_to, return_error);
Todd Kjos26549d12017-06-29 12:01:55 -07003311 } else {
3312 thread->return_error.cmd = return_error;
Martijn Coenen148ade22017-11-15 09:21:35 +01003313 binder_enqueue_thread_work(thread, &thread->return_error.work);
Todd Kjos26549d12017-06-29 12:01:55 -07003314 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003315}
3316
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02003317static int binder_thread_write(struct binder_proc *proc,
3318 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003319 binder_uintptr_t binder_buffer, size_t size,
3320 binder_size_t *consumed)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003321{
3322 uint32_t cmd;
Martijn Coenen342e5c92017-02-03 14:40:46 -08003323 struct binder_context *context = proc->context;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003324 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003325 void __user *ptr = buffer + *consumed;
3326 void __user *end = buffer + size;
3327
Todd Kjos26549d12017-06-29 12:01:55 -07003328 while (ptr < end && thread->return_error.cmd == BR_OK) {
Todd Kjos372e3142017-06-29 12:01:58 -07003329 int ret;
3330
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003331 if (get_user(cmd, (uint32_t __user *)ptr))
3332 return -EFAULT;
3333 ptr += sizeof(uint32_t);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003334 trace_binder_command(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003335 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07003336 atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]);
3337 atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]);
3338 atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003339 }
3340 switch (cmd) {
3341 case BC_INCREFS:
3342 case BC_ACQUIRE:
3343 case BC_RELEASE:
3344 case BC_DECREFS: {
3345 uint32_t target;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003346 const char *debug_string;
Todd Kjos372e3142017-06-29 12:01:58 -07003347 bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE;
3348 bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE;
3349 struct binder_ref_data rdata;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003350
3351 if (get_user(target, (uint32_t __user *)ptr))
3352 return -EFAULT;
Todd Kjosc44b1232017-06-29 12:01:43 -07003353
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003354 ptr += sizeof(uint32_t);
Todd Kjos372e3142017-06-29 12:01:58 -07003355 ret = -1;
3356 if (increment && !target) {
Todd Kjosc44b1232017-06-29 12:01:43 -07003357 struct binder_node *ctx_mgr_node;
Todd Kjosc44b1232017-06-29 12:01:43 -07003358 mutex_lock(&context->context_mgr_node_lock);
3359 ctx_mgr_node = context->binder_context_mgr_node;
Todd Kjos372e3142017-06-29 12:01:58 -07003360 if (ctx_mgr_node)
3361 ret = binder_inc_ref_for_node(
3362 proc, ctx_mgr_node,
3363 strong, NULL, &rdata);
Todd Kjosc44b1232017-06-29 12:01:43 -07003364 mutex_unlock(&context->context_mgr_node_lock);
3365 }
Todd Kjos372e3142017-06-29 12:01:58 -07003366 if (ret)
3367 ret = binder_update_ref_for_handle(
3368 proc, target, increment, strong,
3369 &rdata);
3370 if (!ret && rdata.desc != target) {
3371 binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
3372 proc->pid, thread->pid,
3373 target, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003374 }
3375 switch (cmd) {
3376 case BC_INCREFS:
3377 debug_string = "IncRefs";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003378 break;
3379 case BC_ACQUIRE:
3380 debug_string = "Acquire";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003381 break;
3382 case BC_RELEASE:
3383 debug_string = "Release";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003384 break;
3385 case BC_DECREFS:
3386 default:
3387 debug_string = "DecRefs";
Todd Kjos372e3142017-06-29 12:01:58 -07003388 break;
3389 }
3390 if (ret) {
3391 binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
3392 proc->pid, thread->pid, debug_string,
3393 strong, target, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003394 break;
3395 }
3396 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjos372e3142017-06-29 12:01:58 -07003397 "%d:%d %s ref %d desc %d s %d w %d\n",
3398 proc->pid, thread->pid, debug_string,
3399 rdata.debug_id, rdata.desc, rdata.strong,
3400 rdata.weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003401 break;
3402 }
3403 case BC_INCREFS_DONE:
3404 case BC_ACQUIRE_DONE: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003405 binder_uintptr_t node_ptr;
3406 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003407 struct binder_node *node;
Todd Kjos673068e2017-06-29 12:02:03 -07003408 bool free_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003409
Arve Hjønnevågda498892014-02-21 14:40:26 -08003410 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003411 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003412 ptr += sizeof(binder_uintptr_t);
3413 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003414 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003415 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003416 node = binder_get_node(proc, node_ptr);
3417 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003418 binder_user_error("%d:%d %s u%016llx no match\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003419 proc->pid, thread->pid,
3420 cmd == BC_INCREFS_DONE ?
3421 "BC_INCREFS_DONE" :
3422 "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003423 (u64)node_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003424 break;
3425 }
3426 if (cookie != node->cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003427 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003428 proc->pid, thread->pid,
3429 cmd == BC_INCREFS_DONE ?
3430 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003431 (u64)node_ptr, node->debug_id,
3432 (u64)cookie, (u64)node->cookie);
Todd Kjosadc18842017-06-29 12:01:59 -07003433 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003434 break;
3435 }
Todd Kjos673068e2017-06-29 12:02:03 -07003436 binder_node_inner_lock(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003437 if (cmd == BC_ACQUIRE_DONE) {
3438 if (node->pending_strong_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303439 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003440 proc->pid, thread->pid,
3441 node->debug_id);
Todd Kjos673068e2017-06-29 12:02:03 -07003442 binder_node_inner_unlock(node);
Todd Kjosadc18842017-06-29 12:01:59 -07003443 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003444 break;
3445 }
3446 node->pending_strong_ref = 0;
3447 } else {
3448 if (node->pending_weak_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303449 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003450 proc->pid, thread->pid,
3451 node->debug_id);
Todd Kjos673068e2017-06-29 12:02:03 -07003452 binder_node_inner_unlock(node);
Todd Kjosadc18842017-06-29 12:01:59 -07003453 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003454 break;
3455 }
3456 node->pending_weak_ref = 0;
3457 }
Todd Kjos673068e2017-06-29 12:02:03 -07003458 free_node = binder_dec_node_nilocked(node,
3459 cmd == BC_ACQUIRE_DONE, 0);
3460 WARN_ON(free_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003461 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjosadc18842017-06-29 12:01:59 -07003462 "%d:%d %s node %d ls %d lw %d tr %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003463 proc->pid, thread->pid,
3464 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Todd Kjosadc18842017-06-29 12:01:59 -07003465 node->debug_id, node->local_strong_refs,
3466 node->local_weak_refs, node->tmp_refs);
Todd Kjos673068e2017-06-29 12:02:03 -07003467 binder_node_inner_unlock(node);
Todd Kjosadc18842017-06-29 12:01:59 -07003468 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003469 break;
3470 }
3471 case BC_ATTEMPT_ACQUIRE:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303472 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003473 return -EINVAL;
3474 case BC_ACQUIRE_RESULT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303475 pr_err("BC_ACQUIRE_RESULT not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003476 return -EINVAL;
3477
3478 case BC_FREE_BUFFER: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003479 binder_uintptr_t data_ptr;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003480 struct binder_buffer *buffer;
3481
Arve Hjønnevågda498892014-02-21 14:40:26 -08003482 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003483 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003484 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003485
Todd Kjos53d311cf2017-06-29 12:01:51 -07003486 buffer = binder_alloc_prepare_to_free(&proc->alloc,
3487 data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003488 if (buffer == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003489 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
3490 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003491 break;
3492 }
3493 if (!buffer->allow_user_free) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003494 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
3495 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003496 break;
3497 }
3498 binder_debug(BINDER_DEBUG_FREE_BUFFER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003499 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
3500 proc->pid, thread->pid, (u64)data_ptr,
3501 buffer->debug_id,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003502 buffer->transaction ? "active" : "finished");
3503
3504 if (buffer->transaction) {
3505 buffer->transaction->buffer = NULL;
3506 buffer->transaction = NULL;
3507 }
3508 if (buffer->async_transaction && buffer->target_node) {
Todd Kjos72196392017-06-29 12:02:02 -07003509 struct binder_node *buf_node;
3510 struct binder_work *w;
3511
3512 buf_node = buffer->target_node;
Todd Kjos673068e2017-06-29 12:02:03 -07003513 binder_node_inner_lock(buf_node);
Todd Kjos72196392017-06-29 12:02:02 -07003514 BUG_ON(!buf_node->has_async_transaction);
3515 BUG_ON(buf_node->proc != proc);
Todd Kjos72196392017-06-29 12:02:02 -07003516 w = binder_dequeue_work_head_ilocked(
3517 &buf_node->async_todo);
Martijn Coenen3a6430c2017-08-31 10:04:29 +02003518 if (!w) {
Gustavo A. R. Silva197410a2018-01-23 12:04:27 -06003519 buf_node->has_async_transaction = false;
Martijn Coenen3a6430c2017-08-31 10:04:29 +02003520 } else {
Todd Kjos72196392017-06-29 12:02:02 -07003521 binder_enqueue_work_ilocked(
Martijn Coenen3a6430c2017-08-31 10:04:29 +02003522 w, &proc->todo);
3523 binder_wakeup_proc_ilocked(proc);
3524 }
Todd Kjos673068e2017-06-29 12:02:03 -07003525 binder_node_inner_unlock(buf_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003526 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003527 trace_binder_transaction_buffer_release(buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003528 binder_transaction_buffer_release(proc, buffer, NULL);
Todd Kjos19c98722017-06-29 12:01:40 -07003529 binder_alloc_free_buf(&proc->alloc, buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003530 break;
3531 }
3532
Martijn Coenen79802402017-02-03 14:40:51 -08003533 case BC_TRANSACTION_SG:
3534 case BC_REPLY_SG: {
3535 struct binder_transaction_data_sg tr;
3536
3537 if (copy_from_user(&tr, ptr, sizeof(tr)))
3538 return -EFAULT;
3539 ptr += sizeof(tr);
3540 binder_transaction(proc, thread, &tr.transaction_data,
3541 cmd == BC_REPLY_SG, tr.buffers_size);
3542 break;
3543 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003544 case BC_TRANSACTION:
3545 case BC_REPLY: {
3546 struct binder_transaction_data tr;
3547
3548 if (copy_from_user(&tr, ptr, sizeof(tr)))
3549 return -EFAULT;
3550 ptr += sizeof(tr);
Martijn Coenen4bfac802017-02-03 14:40:50 -08003551 binder_transaction(proc, thread, &tr,
3552 cmd == BC_REPLY, 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003553 break;
3554 }
3555
3556 case BC_REGISTER_LOOPER:
3557 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303558 "%d:%d BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003559 proc->pid, thread->pid);
Todd Kjosb3e68612017-06-29 12:02:07 -07003560 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003561 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
3562 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303563 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003564 proc->pid, thread->pid);
3565 } else if (proc->requested_threads == 0) {
3566 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303567 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003568 proc->pid, thread->pid);
3569 } else {
3570 proc->requested_threads--;
3571 proc->requested_threads_started++;
3572 }
3573 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
Todd Kjosb3e68612017-06-29 12:02:07 -07003574 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003575 break;
3576 case BC_ENTER_LOOPER:
3577 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303578 "%d:%d BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003579 proc->pid, thread->pid);
3580 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
3581 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303582 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003583 proc->pid, thread->pid);
3584 }
3585 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
3586 break;
3587 case BC_EXIT_LOOPER:
3588 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303589 "%d:%d BC_EXIT_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003590 proc->pid, thread->pid);
3591 thread->looper |= BINDER_LOOPER_STATE_EXITED;
3592 break;
3593
3594 case BC_REQUEST_DEATH_NOTIFICATION:
3595 case BC_CLEAR_DEATH_NOTIFICATION: {
3596 uint32_t target;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003597 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003598 struct binder_ref *ref;
Todd Kjos2c1838d2017-06-29 12:02:08 -07003599 struct binder_ref_death *death = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003600
3601 if (get_user(target, (uint32_t __user *)ptr))
3602 return -EFAULT;
3603 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08003604 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003605 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003606 ptr += sizeof(binder_uintptr_t);
Todd Kjos2c1838d2017-06-29 12:02:08 -07003607 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3608 /*
3609 * Allocate memory for death notification
3610 * before taking lock
3611 */
3612 death = kzalloc(sizeof(*death), GFP_KERNEL);
3613 if (death == NULL) {
3614 WARN_ON(thread->return_error.cmd !=
3615 BR_OK);
3616 thread->return_error.cmd = BR_ERROR;
Martijn Coenen148ade22017-11-15 09:21:35 +01003617 binder_enqueue_thread_work(
3618 thread,
3619 &thread->return_error.work);
Todd Kjos2c1838d2017-06-29 12:02:08 -07003620 binder_debug(
3621 BINDER_DEBUG_FAILED_TRANSACTION,
3622 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
3623 proc->pid, thread->pid);
3624 break;
3625 }
3626 }
3627 binder_proc_lock(proc);
3628 ref = binder_get_ref_olocked(proc, target, false);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003629 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303630 binder_user_error("%d:%d %s invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003631 proc->pid, thread->pid,
3632 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3633 "BC_REQUEST_DEATH_NOTIFICATION" :
3634 "BC_CLEAR_DEATH_NOTIFICATION",
3635 target);
Todd Kjos2c1838d2017-06-29 12:02:08 -07003636 binder_proc_unlock(proc);
3637 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003638 break;
3639 }
3640
3641 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003642 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003643 proc->pid, thread->pid,
3644 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3645 "BC_REQUEST_DEATH_NOTIFICATION" :
3646 "BC_CLEAR_DEATH_NOTIFICATION",
Todd Kjos372e3142017-06-29 12:01:58 -07003647 (u64)cookie, ref->data.debug_id,
3648 ref->data.desc, ref->data.strong,
3649 ref->data.weak, ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003650
Martijn Coenenab51ec62017-06-29 12:02:10 -07003651 binder_node_lock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003652 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3653 if (ref->death) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303654 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003655 proc->pid, thread->pid);
Martijn Coenenab51ec62017-06-29 12:02:10 -07003656 binder_node_unlock(ref->node);
Todd Kjos2c1838d2017-06-29 12:02:08 -07003657 binder_proc_unlock(proc);
3658 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003659 break;
3660 }
3661 binder_stats_created(BINDER_STAT_DEATH);
3662 INIT_LIST_HEAD(&death->work.entry);
3663 death->cookie = cookie;
3664 ref->death = death;
3665 if (ref->node->proc == NULL) {
3666 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
Martijn Coenenbb745622017-08-31 10:04:28 +02003667
3668 binder_inner_proc_lock(proc);
3669 binder_enqueue_work_ilocked(
3670 &ref->death->work, &proc->todo);
3671 binder_wakeup_proc_ilocked(proc);
3672 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003673 }
3674 } else {
3675 if (ref->death == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303676 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003677 proc->pid, thread->pid);
Todd Kjos673068e2017-06-29 12:02:03 -07003678 binder_node_unlock(ref->node);
Todd Kjos2c1838d2017-06-29 12:02:08 -07003679 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003680 break;
3681 }
3682 death = ref->death;
3683 if (death->cookie != cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003684 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003685 proc->pid, thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003686 (u64)death->cookie,
3687 (u64)cookie);
Todd Kjos673068e2017-06-29 12:02:03 -07003688 binder_node_unlock(ref->node);
Todd Kjos2c1838d2017-06-29 12:02:08 -07003689 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003690 break;
3691 }
3692 ref->death = NULL;
Todd Kjos72196392017-06-29 12:02:02 -07003693 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003694 if (list_empty(&death->work.entry)) {
3695 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos72196392017-06-29 12:02:02 -07003696 if (thread->looper &
3697 (BINDER_LOOPER_STATE_REGISTERED |
3698 BINDER_LOOPER_STATE_ENTERED))
Martijn Coenen148ade22017-11-15 09:21:35 +01003699 binder_enqueue_thread_work_ilocked(
3700 thread,
3701 &death->work);
Todd Kjos72196392017-06-29 12:02:02 -07003702 else {
3703 binder_enqueue_work_ilocked(
3704 &death->work,
3705 &proc->todo);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02003706 binder_wakeup_proc_ilocked(
Martijn Coenen408c68b2017-08-31 10:04:19 +02003707 proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003708 }
3709 } else {
3710 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
3711 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
3712 }
Todd Kjos72196392017-06-29 12:02:02 -07003713 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003714 }
Martijn Coenenab51ec62017-06-29 12:02:10 -07003715 binder_node_unlock(ref->node);
Todd Kjos2c1838d2017-06-29 12:02:08 -07003716 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003717 } break;
3718 case BC_DEAD_BINDER_DONE: {
3719 struct binder_work *w;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003720 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003721 struct binder_ref_death *death = NULL;
Seunghun Lee10f62862014-05-01 01:30:23 +09003722
Arve Hjønnevågda498892014-02-21 14:40:26 -08003723 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003724 return -EFAULT;
3725
Lisa Du7a64cd82016-02-17 09:32:52 +08003726 ptr += sizeof(cookie);
Todd Kjos72196392017-06-29 12:02:02 -07003727 binder_inner_proc_lock(proc);
3728 list_for_each_entry(w, &proc->delivered_death,
3729 entry) {
3730 struct binder_ref_death *tmp_death =
3731 container_of(w,
3732 struct binder_ref_death,
3733 work);
Seunghun Lee10f62862014-05-01 01:30:23 +09003734
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003735 if (tmp_death->cookie == cookie) {
3736 death = tmp_death;
3737 break;
3738 }
3739 }
3740 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Todd Kjos8ca86f12018-02-07 13:57:37 -08003741 "%d:%d BC_DEAD_BINDER_DONE %016llx found %pK\n",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003742 proc->pid, thread->pid, (u64)cookie,
3743 death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003744 if (death == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003745 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
3746 proc->pid, thread->pid, (u64)cookie);
Todd Kjos72196392017-06-29 12:02:02 -07003747 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003748 break;
3749 }
Todd Kjos72196392017-06-29 12:02:02 -07003750 binder_dequeue_work_ilocked(&death->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003751 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
3752 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos72196392017-06-29 12:02:02 -07003753 if (thread->looper &
3754 (BINDER_LOOPER_STATE_REGISTERED |
3755 BINDER_LOOPER_STATE_ENTERED))
Martijn Coenen148ade22017-11-15 09:21:35 +01003756 binder_enqueue_thread_work_ilocked(
3757 thread, &death->work);
Todd Kjos72196392017-06-29 12:02:02 -07003758 else {
3759 binder_enqueue_work_ilocked(
3760 &death->work,
3761 &proc->todo);
Martijn Coenen408c68b2017-08-31 10:04:19 +02003762 binder_wakeup_proc_ilocked(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003763 }
3764 }
Todd Kjos72196392017-06-29 12:02:02 -07003765 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003766 } break;
3767
3768 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303769 pr_err("%d:%d unknown command %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003770 proc->pid, thread->pid, cmd);
3771 return -EINVAL;
3772 }
3773 *consumed = ptr - buffer;
3774 }
3775 return 0;
3776}
3777
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02003778static void binder_stat_br(struct binder_proc *proc,
3779 struct binder_thread *thread, uint32_t cmd)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003780{
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003781 trace_binder_return(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003782 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07003783 atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
3784 atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
3785 atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003786 }
3787}
3788
Todd Kjos26b47d82017-06-29 12:01:47 -07003789static int binder_put_node_cmd(struct binder_proc *proc,
3790 struct binder_thread *thread,
3791 void __user **ptrp,
3792 binder_uintptr_t node_ptr,
3793 binder_uintptr_t node_cookie,
3794 int node_debug_id,
3795 uint32_t cmd, const char *cmd_name)
3796{
3797 void __user *ptr = *ptrp;
3798
3799 if (put_user(cmd, (uint32_t __user *)ptr))
3800 return -EFAULT;
3801 ptr += sizeof(uint32_t);
3802
3803 if (put_user(node_ptr, (binder_uintptr_t __user *)ptr))
3804 return -EFAULT;
3805 ptr += sizeof(binder_uintptr_t);
3806
3807 if (put_user(node_cookie, (binder_uintptr_t __user *)ptr))
3808 return -EFAULT;
3809 ptr += sizeof(binder_uintptr_t);
3810
3811 binder_stat_br(proc, thread, cmd);
3812 binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n",
3813 proc->pid, thread->pid, cmd_name, node_debug_id,
3814 (u64)node_ptr, (u64)node_cookie);
3815
3816 *ptrp = ptr;
3817 return 0;
3818}
3819
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02003820static int binder_wait_for_work(struct binder_thread *thread,
3821 bool do_proc_work)
3822{
3823 DEFINE_WAIT(wait);
3824 struct binder_proc *proc = thread->proc;
3825 int ret = 0;
3826
3827 freezer_do_not_count();
3828 binder_inner_proc_lock(proc);
3829 for (;;) {
3830 prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE);
3831 if (binder_has_work_ilocked(thread, do_proc_work))
3832 break;
3833 if (do_proc_work)
3834 list_add(&thread->waiting_thread_node,
3835 &proc->waiting_threads);
3836 binder_inner_proc_unlock(proc);
3837 schedule();
3838 binder_inner_proc_lock(proc);
3839 list_del_init(&thread->waiting_thread_node);
3840 if (signal_pending(current)) {
3841 ret = -ERESTARTSYS;
3842 break;
3843 }
3844 }
3845 finish_wait(&thread->wait, &wait);
3846 binder_inner_proc_unlock(proc);
3847 freezer_count();
3848
3849 return ret;
3850}
3851
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003852static int binder_thread_read(struct binder_proc *proc,
3853 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003854 binder_uintptr_t binder_buffer, size_t size,
3855 binder_size_t *consumed, int non_block)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003856{
Arve Hjønnevågda498892014-02-21 14:40:26 -08003857 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003858 void __user *ptr = buffer + *consumed;
3859 void __user *end = buffer + size;
3860
3861 int ret = 0;
3862 int wait_for_proc_work;
3863
3864 if (*consumed == 0) {
3865 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
3866 return -EFAULT;
3867 ptr += sizeof(uint32_t);
3868 }
3869
3870retry:
Martijn Coenen0b89d692017-06-29 12:02:06 -07003871 binder_inner_proc_lock(proc);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02003872 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
Martijn Coenen0b89d692017-06-29 12:02:06 -07003873 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003874
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003875 thread->looper |= BINDER_LOOPER_STATE_WAITING;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003876
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003877 trace_binder_wait_for_work(wait_for_proc_work,
3878 !!thread->transaction_stack,
Todd Kjos72196392017-06-29 12:02:02 -07003879 !binder_worklist_empty(proc, &thread->todo));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003880 if (wait_for_proc_work) {
3881 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
3882 BINDER_LOOPER_STATE_ENTERED))) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303883 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 +09003884 proc->pid, thread->pid, thread->looper);
3885 wait_event_interruptible(binder_user_error_wait,
3886 binder_stop_on_user_error < 2);
3887 }
3888 binder_set_nice(proc->default_priority);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003889 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003890
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02003891 if (non_block) {
3892 if (!binder_has_work(thread, wait_for_proc_work))
3893 ret = -EAGAIN;
3894 } else {
3895 ret = binder_wait_for_work(thread, wait_for_proc_work);
3896 }
3897
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003898 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
3899
3900 if (ret)
3901 return ret;
3902
3903 while (1) {
3904 uint32_t cmd;
3905 struct binder_transaction_data tr;
Todd Kjos72196392017-06-29 12:02:02 -07003906 struct binder_work *w = NULL;
3907 struct list_head *list = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003908 struct binder_transaction *t = NULL;
Todd Kjos7a4408c2017-06-29 12:01:57 -07003909 struct binder_thread *t_from;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003910
Todd Kjosed297212017-06-29 12:02:01 -07003911 binder_inner_proc_lock(proc);
Todd Kjos72196392017-06-29 12:02:02 -07003912 if (!binder_worklist_empty_ilocked(&thread->todo))
3913 list = &thread->todo;
3914 else if (!binder_worklist_empty_ilocked(&proc->todo) &&
3915 wait_for_proc_work)
3916 list = &proc->todo;
3917 else {
3918 binder_inner_proc_unlock(proc);
3919
Dmitry Voytik395262a2014-09-08 18:16:34 +04003920 /* no data added */
Todd Kjos08dabce2017-06-29 12:01:49 -07003921 if (ptr - buffer == 4 && !thread->looper_need_return)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003922 goto retry;
3923 break;
3924 }
3925
Todd Kjosed297212017-06-29 12:02:01 -07003926 if (end - ptr < sizeof(tr) + 4) {
3927 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003928 break;
Todd Kjosed297212017-06-29 12:02:01 -07003929 }
Todd Kjos72196392017-06-29 12:02:02 -07003930 w = binder_dequeue_work_head_ilocked(list);
Martijn Coenen148ade22017-11-15 09:21:35 +01003931 if (binder_worklist_empty_ilocked(&thread->todo))
3932 thread->process_todo = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003933
3934 switch (w->type) {
3935 case BINDER_WORK_TRANSACTION: {
Todd Kjosed297212017-06-29 12:02:01 -07003936 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003937 t = container_of(w, struct binder_transaction, work);
3938 } break;
Todd Kjos26549d12017-06-29 12:01:55 -07003939 case BINDER_WORK_RETURN_ERROR: {
3940 struct binder_error *e = container_of(
3941 w, struct binder_error, work);
3942
3943 WARN_ON(e->cmd == BR_OK);
Todd Kjosed297212017-06-29 12:02:01 -07003944 binder_inner_proc_unlock(proc);
Todd Kjos26549d12017-06-29 12:01:55 -07003945 if (put_user(e->cmd, (uint32_t __user *)ptr))
3946 return -EFAULT;
宋金时838d5562018-05-10 02:05:03 +00003947 cmd = e->cmd;
Todd Kjos26549d12017-06-29 12:01:55 -07003948 e->cmd = BR_OK;
3949 ptr += sizeof(uint32_t);
3950
宋金时838d5562018-05-10 02:05:03 +00003951 binder_stat_br(proc, thread, cmd);
Todd Kjos26549d12017-06-29 12:01:55 -07003952 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003953 case BINDER_WORK_TRANSACTION_COMPLETE: {
Todd Kjosed297212017-06-29 12:02:01 -07003954 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003955 cmd = BR_TRANSACTION_COMPLETE;
3956 if (put_user(cmd, (uint32_t __user *)ptr))
3957 return -EFAULT;
3958 ptr += sizeof(uint32_t);
3959
3960 binder_stat_br(proc, thread, cmd);
3961 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303962 "%d:%d BR_TRANSACTION_COMPLETE\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003963 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003964 kfree(w);
3965 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3966 } break;
3967 case BINDER_WORK_NODE: {
3968 struct binder_node *node = container_of(w, struct binder_node, work);
Todd Kjos26b47d82017-06-29 12:01:47 -07003969 int strong, weak;
3970 binder_uintptr_t node_ptr = node->ptr;
3971 binder_uintptr_t node_cookie = node->cookie;
3972 int node_debug_id = node->debug_id;
3973 int has_weak_ref;
3974 int has_strong_ref;
3975 void __user *orig_ptr = ptr;
Seunghun Lee10f62862014-05-01 01:30:23 +09003976
Todd Kjos26b47d82017-06-29 12:01:47 -07003977 BUG_ON(proc != node->proc);
3978 strong = node->internal_strong_refs ||
3979 node->local_strong_refs;
3980 weak = !hlist_empty(&node->refs) ||
Todd Kjosadc18842017-06-29 12:01:59 -07003981 node->local_weak_refs ||
3982 node->tmp_refs || strong;
Todd Kjos26b47d82017-06-29 12:01:47 -07003983 has_strong_ref = node->has_strong_ref;
3984 has_weak_ref = node->has_weak_ref;
3985
3986 if (weak && !has_weak_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003987 node->has_weak_ref = 1;
3988 node->pending_weak_ref = 1;
3989 node->local_weak_refs++;
Todd Kjos26b47d82017-06-29 12:01:47 -07003990 }
3991 if (strong && !has_strong_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003992 node->has_strong_ref = 1;
3993 node->pending_strong_ref = 1;
3994 node->local_strong_refs++;
Todd Kjos26b47d82017-06-29 12:01:47 -07003995 }
3996 if (!strong && has_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003997 node->has_strong_ref = 0;
Todd Kjos26b47d82017-06-29 12:01:47 -07003998 if (!weak && has_weak_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003999 node->has_weak_ref = 0;
Todd Kjos26b47d82017-06-29 12:01:47 -07004000 if (!weak && !strong) {
4001 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4002 "%d:%d node %d u%016llx c%016llx deleted\n",
4003 proc->pid, thread->pid,
4004 node_debug_id,
4005 (u64)node_ptr,
4006 (u64)node_cookie);
4007 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjosed297212017-06-29 12:02:01 -07004008 binder_inner_proc_unlock(proc);
Todd Kjos673068e2017-06-29 12:02:03 -07004009 binder_node_lock(node);
4010 /*
4011 * Acquire the node lock before freeing the
4012 * node to serialize with other threads that
4013 * may have been holding the node lock while
4014 * decrementing this node (avoids race where
4015 * this thread frees while the other thread
4016 * is unlocking the node after the final
4017 * decrement)
4018 */
4019 binder_node_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07004020 binder_free_node(node);
4021 } else
4022 binder_inner_proc_unlock(proc);
4023
Todd Kjos26b47d82017-06-29 12:01:47 -07004024 if (weak && !has_weak_ref)
4025 ret = binder_put_node_cmd(
4026 proc, thread, &ptr, node_ptr,
4027 node_cookie, node_debug_id,
4028 BR_INCREFS, "BR_INCREFS");
4029 if (!ret && strong && !has_strong_ref)
4030 ret = binder_put_node_cmd(
4031 proc, thread, &ptr, node_ptr,
4032 node_cookie, node_debug_id,
4033 BR_ACQUIRE, "BR_ACQUIRE");
4034 if (!ret && !strong && has_strong_ref)
4035 ret = binder_put_node_cmd(
4036 proc, thread, &ptr, node_ptr,
4037 node_cookie, node_debug_id,
4038 BR_RELEASE, "BR_RELEASE");
4039 if (!ret && !weak && has_weak_ref)
4040 ret = binder_put_node_cmd(
4041 proc, thread, &ptr, node_ptr,
4042 node_cookie, node_debug_id,
4043 BR_DECREFS, "BR_DECREFS");
4044 if (orig_ptr == ptr)
4045 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4046 "%d:%d node %d u%016llx c%016llx state unchanged\n",
4047 proc->pid, thread->pid,
4048 node_debug_id,
4049 (u64)node_ptr,
4050 (u64)node_cookie);
4051 if (ret)
4052 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004053 } break;
4054 case BINDER_WORK_DEAD_BINDER:
4055 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4056 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4057 struct binder_ref_death *death;
4058 uint32_t cmd;
Martijn Coenenab51ec62017-06-29 12:02:10 -07004059 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004060
4061 death = container_of(w, struct binder_ref_death, work);
4062 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
4063 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
4064 else
4065 cmd = BR_DEAD_BINDER;
Martijn Coenenab51ec62017-06-29 12:02:10 -07004066 cookie = death->cookie;
4067
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004068 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004069 "%d:%d %s %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004070 proc->pid, thread->pid,
4071 cmd == BR_DEAD_BINDER ?
4072 "BR_DEAD_BINDER" :
4073 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
Martijn Coenenab51ec62017-06-29 12:02:10 -07004074 (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004075 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
Martijn Coenenab51ec62017-06-29 12:02:10 -07004076 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004077 kfree(death);
4078 binder_stats_deleted(BINDER_STAT_DEATH);
Todd Kjosed297212017-06-29 12:02:01 -07004079 } else {
Todd Kjos72196392017-06-29 12:02:02 -07004080 binder_enqueue_work_ilocked(
4081 w, &proc->delivered_death);
Todd Kjosed297212017-06-29 12:02:01 -07004082 binder_inner_proc_unlock(proc);
4083 }
Martijn Coenenab51ec62017-06-29 12:02:10 -07004084 if (put_user(cmd, (uint32_t __user *)ptr))
4085 return -EFAULT;
4086 ptr += sizeof(uint32_t);
4087 if (put_user(cookie,
4088 (binder_uintptr_t __user *)ptr))
4089 return -EFAULT;
4090 ptr += sizeof(binder_uintptr_t);
4091 binder_stat_br(proc, thread, cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004092 if (cmd == BR_DEAD_BINDER)
4093 goto done; /* DEAD_BINDER notifications can cause transactions */
4094 } break;
4095 }
4096
4097 if (!t)
4098 continue;
4099
4100 BUG_ON(t->buffer == NULL);
4101 if (t->buffer->target_node) {
4102 struct binder_node *target_node = t->buffer->target_node;
Seunghun Lee10f62862014-05-01 01:30:23 +09004103
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004104 tr.target.ptr = target_node->ptr;
4105 tr.cookie = target_node->cookie;
4106 t->saved_priority = task_nice(current);
4107 if (t->priority < target_node->min_priority &&
4108 !(t->flags & TF_ONE_WAY))
4109 binder_set_nice(t->priority);
4110 else if (!(t->flags & TF_ONE_WAY) ||
4111 t->saved_priority > target_node->min_priority)
4112 binder_set_nice(target_node->min_priority);
4113 cmd = BR_TRANSACTION;
4114 } else {
Arve Hjønnevågda498892014-02-21 14:40:26 -08004115 tr.target.ptr = 0;
4116 tr.cookie = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004117 cmd = BR_REPLY;
4118 }
4119 tr.code = t->code;
4120 tr.flags = t->flags;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -06004121 tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004122
Todd Kjos7a4408c2017-06-29 12:01:57 -07004123 t_from = binder_get_txn_from(t);
4124 if (t_from) {
4125 struct task_struct *sender = t_from->proc->tsk;
Seunghun Lee10f62862014-05-01 01:30:23 +09004126
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004127 tr.sender_pid = task_tgid_nr_ns(sender,
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08004128 task_active_pid_ns(current));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004129 } else {
4130 tr.sender_pid = 0;
4131 }
4132
4133 tr.data_size = t->buffer->data_size;
4134 tr.offsets_size = t->buffer->offsets_size;
Todd Kjos19c98722017-06-29 12:01:40 -07004135 tr.data.ptr.buffer = (binder_uintptr_t)
4136 ((uintptr_t)t->buffer->data +
4137 binder_alloc_get_user_buffer_offset(&proc->alloc));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004138 tr.data.ptr.offsets = tr.data.ptr.buffer +
4139 ALIGN(t->buffer->data_size,
4140 sizeof(void *));
4141
Todd Kjos7a4408c2017-06-29 12:01:57 -07004142 if (put_user(cmd, (uint32_t __user *)ptr)) {
4143 if (t_from)
4144 binder_thread_dec_tmpref(t_from);
Martijn Coenenfb2c4452017-11-13 10:06:08 +01004145
4146 binder_cleanup_transaction(t, "put_user failed",
4147 BR_FAILED_REPLY);
4148
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004149 return -EFAULT;
Todd Kjos7a4408c2017-06-29 12:01:57 -07004150 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004151 ptr += sizeof(uint32_t);
Todd Kjos7a4408c2017-06-29 12:01:57 -07004152 if (copy_to_user(ptr, &tr, sizeof(tr))) {
4153 if (t_from)
4154 binder_thread_dec_tmpref(t_from);
Martijn Coenenfb2c4452017-11-13 10:06:08 +01004155
4156 binder_cleanup_transaction(t, "copy_to_user failed",
4157 BR_FAILED_REPLY);
4158
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004159 return -EFAULT;
Todd Kjos7a4408c2017-06-29 12:01:57 -07004160 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004161 ptr += sizeof(tr);
4162
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004163 trace_binder_transaction_received(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004164 binder_stat_br(proc, thread, cmd);
4165 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004166 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004167 proc->pid, thread->pid,
4168 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
4169 "BR_REPLY",
Todd Kjos7a4408c2017-06-29 12:01:57 -07004170 t->debug_id, t_from ? t_from->proc->pid : 0,
4171 t_from ? t_from->pid : 0, cmd,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004172 t->buffer->data_size, t->buffer->offsets_size,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004173 (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004174
Todd Kjos7a4408c2017-06-29 12:01:57 -07004175 if (t_from)
4176 binder_thread_dec_tmpref(t_from);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004177 t->buffer->allow_user_free = 1;
4178 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
Martijn Coenen0b89d692017-06-29 12:02:06 -07004179 binder_inner_proc_lock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004180 t->to_parent = thread->transaction_stack;
4181 t->to_thread = thread;
4182 thread->transaction_stack = t;
Martijn Coenen0b89d692017-06-29 12:02:06 -07004183 binder_inner_proc_unlock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004184 } else {
Todd Kjosb6d282c2017-06-29 12:01:54 -07004185 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004186 }
4187 break;
4188 }
4189
4190done:
4191
4192 *consumed = ptr - buffer;
Todd Kjosb3e68612017-06-29 12:02:07 -07004193 binder_inner_proc_lock(proc);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004194 if (proc->requested_threads == 0 &&
4195 list_empty(&thread->proc->waiting_threads) &&
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004196 proc->requested_threads_started < proc->max_threads &&
4197 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4198 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
4199 /*spawn a new thread if we leave this out */) {
4200 proc->requested_threads++;
Todd Kjosb3e68612017-06-29 12:02:07 -07004201 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004202 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304203 "%d:%d BR_SPAWN_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004204 proc->pid, thread->pid);
4205 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
4206 return -EFAULT;
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07004207 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
Todd Kjosb3e68612017-06-29 12:02:07 -07004208 } else
4209 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004210 return 0;
4211}
4212
Todd Kjos72196392017-06-29 12:02:02 -07004213static void binder_release_work(struct binder_proc *proc,
4214 struct list_head *list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004215{
4216 struct binder_work *w;
Seunghun Lee10f62862014-05-01 01:30:23 +09004217
Todd Kjos72196392017-06-29 12:02:02 -07004218 while (1) {
4219 w = binder_dequeue_work_head(proc, list);
4220 if (!w)
4221 return;
4222
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004223 switch (w->type) {
4224 case BINDER_WORK_TRANSACTION: {
4225 struct binder_transaction *t;
4226
4227 t = container_of(w, struct binder_transaction, work);
Martijn Coenenfb2c4452017-11-13 10:06:08 +01004228
4229 binder_cleanup_transaction(t, "process died.",
4230 BR_DEAD_REPLY);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004231 } break;
Todd Kjos26549d12017-06-29 12:01:55 -07004232 case BINDER_WORK_RETURN_ERROR: {
4233 struct binder_error *e = container_of(
4234 w, struct binder_error, work);
4235
4236 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4237 "undelivered TRANSACTION_ERROR: %u\n",
4238 e->cmd);
4239 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004240 case BINDER_WORK_TRANSACTION_COMPLETE: {
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004241 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304242 "undelivered TRANSACTION_COMPLETE\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004243 kfree(w);
4244 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4245 } break;
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004246 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4247 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4248 struct binder_ref_death *death;
4249
4250 death = container_of(w, struct binder_ref_death, work);
4251 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004252 "undelivered death notification, %016llx\n",
4253 (u64)death->cookie);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004254 kfree(death);
4255 binder_stats_deleted(BINDER_STAT_DEATH);
4256 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004257 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304258 pr_err("unexpected work type, %d, not freed\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004259 w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004260 break;
4261 }
4262 }
4263
4264}
4265
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004266static struct binder_thread *binder_get_thread_ilocked(
4267 struct binder_proc *proc, struct binder_thread *new_thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004268{
4269 struct binder_thread *thread = NULL;
4270 struct rb_node *parent = NULL;
4271 struct rb_node **p = &proc->threads.rb_node;
4272
4273 while (*p) {
4274 parent = *p;
4275 thread = rb_entry(parent, struct binder_thread, rb_node);
4276
4277 if (current->pid < thread->pid)
4278 p = &(*p)->rb_left;
4279 else if (current->pid > thread->pid)
4280 p = &(*p)->rb_right;
4281 else
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004282 return thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004283 }
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004284 if (!new_thread)
4285 return NULL;
4286 thread = new_thread;
4287 binder_stats_created(BINDER_STAT_THREAD);
4288 thread->proc = proc;
4289 thread->pid = current->pid;
4290 atomic_set(&thread->tmp_ref, 0);
4291 init_waitqueue_head(&thread->wait);
4292 INIT_LIST_HEAD(&thread->todo);
4293 rb_link_node(&thread->rb_node, parent, p);
4294 rb_insert_color(&thread->rb_node, &proc->threads);
4295 thread->looper_need_return = true;
4296 thread->return_error.work.type = BINDER_WORK_RETURN_ERROR;
4297 thread->return_error.cmd = BR_OK;
4298 thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR;
4299 thread->reply_error.cmd = BR_OK;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004300 INIT_LIST_HEAD(&new_thread->waiting_thread_node);
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004301 return thread;
4302}
4303
4304static struct binder_thread *binder_get_thread(struct binder_proc *proc)
4305{
4306 struct binder_thread *thread;
4307 struct binder_thread *new_thread;
4308
4309 binder_inner_proc_lock(proc);
4310 thread = binder_get_thread_ilocked(proc, NULL);
4311 binder_inner_proc_unlock(proc);
4312 if (!thread) {
4313 new_thread = kzalloc(sizeof(*thread), GFP_KERNEL);
4314 if (new_thread == NULL)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004315 return NULL;
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004316 binder_inner_proc_lock(proc);
4317 thread = binder_get_thread_ilocked(proc, new_thread);
4318 binder_inner_proc_unlock(proc);
4319 if (thread != new_thread)
4320 kfree(new_thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004321 }
4322 return thread;
4323}
4324
Todd Kjos7a4408c2017-06-29 12:01:57 -07004325static void binder_free_proc(struct binder_proc *proc)
4326{
4327 BUG_ON(!list_empty(&proc->todo));
4328 BUG_ON(!list_empty(&proc->delivered_death));
4329 binder_alloc_deferred_release(&proc->alloc);
4330 put_task_struct(proc->tsk);
4331 binder_stats_deleted(BINDER_STAT_PROC);
4332 kfree(proc);
4333}
4334
4335static void binder_free_thread(struct binder_thread *thread)
4336{
4337 BUG_ON(!list_empty(&thread->todo));
4338 binder_stats_deleted(BINDER_STAT_THREAD);
4339 binder_proc_dec_tmpref(thread->proc);
4340 kfree(thread);
4341}
4342
4343static int binder_thread_release(struct binder_proc *proc,
4344 struct binder_thread *thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004345{
4346 struct binder_transaction *t;
4347 struct binder_transaction *send_reply = NULL;
4348 int active_transactions = 0;
Todd Kjos7a4408c2017-06-29 12:01:57 -07004349 struct binder_transaction *last_t = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004350
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004351 binder_inner_proc_lock(thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07004352 /*
4353 * take a ref on the proc so it survives
4354 * after we remove this thread from proc->threads.
4355 * The corresponding dec is when we actually
4356 * free the thread in binder_free_thread()
4357 */
4358 proc->tmp_ref++;
4359 /*
4360 * take a ref on this thread to ensure it
4361 * survives while we are releasing it
4362 */
4363 atomic_inc(&thread->tmp_ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004364 rb_erase(&thread->rb_node, &proc->threads);
4365 t = thread->transaction_stack;
Todd Kjos7a4408c2017-06-29 12:01:57 -07004366 if (t) {
4367 spin_lock(&t->lock);
4368 if (t->to_thread == thread)
4369 send_reply = t;
4370 }
4371 thread->is_dead = true;
4372
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004373 while (t) {
Todd Kjos7a4408c2017-06-29 12:01:57 -07004374 last_t = t;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004375 active_transactions++;
4376 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304377 "release %d:%d transaction %d %s, still active\n",
4378 proc->pid, thread->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004379 t->debug_id,
4380 (t->to_thread == thread) ? "in" : "out");
4381
4382 if (t->to_thread == thread) {
4383 t->to_proc = NULL;
4384 t->to_thread = NULL;
4385 if (t->buffer) {
4386 t->buffer->transaction = NULL;
4387 t->buffer = NULL;
4388 }
4389 t = t->to_parent;
4390 } else if (t->from == thread) {
4391 t->from = NULL;
4392 t = t->from_parent;
4393 } else
4394 BUG();
Todd Kjos7a4408c2017-06-29 12:01:57 -07004395 spin_unlock(&last_t->lock);
4396 if (t)
4397 spin_lock(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004398 }
Martijn Coenenf5cb7792018-01-05 11:27:07 +01004399
4400 /*
4401 * If this thread used poll, make sure we remove the waitqueue
4402 * from any epoll data structures holding it with POLLFREE.
4403 * waitqueue_active() is safe to use here because we're holding
4404 * the inner lock.
4405 */
4406 if ((thread->looper & BINDER_LOOPER_STATE_POLL) &&
4407 waitqueue_active(&thread->wait)) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08004408 wake_up_poll(&thread->wait, EPOLLHUP | POLLFREE);
Martijn Coenenf5cb7792018-01-05 11:27:07 +01004409 }
4410
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004411 binder_inner_proc_unlock(thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07004412
Martijn Coenen5eeb2ca2018-02-16 09:47:15 +01004413 /*
4414 * This is needed to avoid races between wake_up_poll() above and
4415 * and ep_remove_waitqueue() called for other reasons (eg the epoll file
4416 * descriptor being closed); ep_remove_waitqueue() holds an RCU read
4417 * lock, so we can be sure it's done after calling synchronize_rcu().
4418 */
4419 if (thread->looper & BINDER_LOOPER_STATE_POLL)
4420 synchronize_rcu();
4421
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004422 if (send_reply)
4423 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
Todd Kjos72196392017-06-29 12:02:02 -07004424 binder_release_work(proc, &thread->todo);
Todd Kjos7a4408c2017-06-29 12:01:57 -07004425 binder_thread_dec_tmpref(thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004426 return active_transactions;
4427}
4428
Al Viroafc9a422017-07-03 06:39:46 -04004429static __poll_t binder_poll(struct file *filp,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004430 struct poll_table_struct *wait)
4431{
4432 struct binder_proc *proc = filp->private_data;
4433 struct binder_thread *thread = NULL;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004434 bool wait_for_proc_work;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004435
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004436 thread = binder_get_thread(proc);
Eric Biggersf8898262018-01-30 23:11:24 -08004437 if (!thread)
4438 return POLLERR;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004439
Martijn Coenen0b89d692017-06-29 12:02:06 -07004440 binder_inner_proc_lock(thread->proc);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004441 thread->looper |= BINDER_LOOPER_STATE_POLL;
4442 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
4443
Martijn Coenen0b89d692017-06-29 12:02:06 -07004444 binder_inner_proc_unlock(thread->proc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004445
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004446 poll_wait(filp, &thread->wait, wait);
4447
Martijn Coenen66b83a42017-10-09 14:26:56 +02004448 if (binder_has_work(thread, wait_for_proc_work))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08004449 return EPOLLIN;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004450
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004451 return 0;
4452}
4453
Tair Rzayev78260ac2014-06-03 22:27:21 +03004454static int binder_ioctl_write_read(struct file *filp,
4455 unsigned int cmd, unsigned long arg,
4456 struct binder_thread *thread)
4457{
4458 int ret = 0;
4459 struct binder_proc *proc = filp->private_data;
4460 unsigned int size = _IOC_SIZE(cmd);
4461 void __user *ubuf = (void __user *)arg;
4462 struct binder_write_read bwr;
4463
4464 if (size != sizeof(struct binder_write_read)) {
4465 ret = -EINVAL;
4466 goto out;
4467 }
4468 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
4469 ret = -EFAULT;
4470 goto out;
4471 }
4472 binder_debug(BINDER_DEBUG_READ_WRITE,
4473 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
4474 proc->pid, thread->pid,
4475 (u64)bwr.write_size, (u64)bwr.write_buffer,
4476 (u64)bwr.read_size, (u64)bwr.read_buffer);
4477
4478 if (bwr.write_size > 0) {
4479 ret = binder_thread_write(proc, thread,
4480 bwr.write_buffer,
4481 bwr.write_size,
4482 &bwr.write_consumed);
4483 trace_binder_write_done(ret);
4484 if (ret < 0) {
4485 bwr.read_consumed = 0;
4486 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4487 ret = -EFAULT;
4488 goto out;
4489 }
4490 }
4491 if (bwr.read_size > 0) {
4492 ret = binder_thread_read(proc, thread, bwr.read_buffer,
4493 bwr.read_size,
4494 &bwr.read_consumed,
4495 filp->f_flags & O_NONBLOCK);
4496 trace_binder_read_done(ret);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004497 binder_inner_proc_lock(proc);
4498 if (!binder_worklist_empty_ilocked(&proc->todo))
Martijn Coenen408c68b2017-08-31 10:04:19 +02004499 binder_wakeup_proc_ilocked(proc);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004500 binder_inner_proc_unlock(proc);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004501 if (ret < 0) {
4502 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4503 ret = -EFAULT;
4504 goto out;
4505 }
4506 }
4507 binder_debug(BINDER_DEBUG_READ_WRITE,
4508 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
4509 proc->pid, thread->pid,
4510 (u64)bwr.write_consumed, (u64)bwr.write_size,
4511 (u64)bwr.read_consumed, (u64)bwr.read_size);
4512 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
4513 ret = -EFAULT;
4514 goto out;
4515 }
4516out:
4517 return ret;
4518}
4519
4520static int binder_ioctl_set_ctx_mgr(struct file *filp)
4521{
4522 int ret = 0;
4523 struct binder_proc *proc = filp->private_data;
Martijn Coenen342e5c92017-02-03 14:40:46 -08004524 struct binder_context *context = proc->context;
Todd Kjosc44b1232017-06-29 12:01:43 -07004525 struct binder_node *new_node;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004526 kuid_t curr_euid = current_euid();
4527
Todd Kjosc44b1232017-06-29 12:01:43 -07004528 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen342e5c92017-02-03 14:40:46 -08004529 if (context->binder_context_mgr_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004530 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
4531 ret = -EBUSY;
4532 goto out;
4533 }
Stephen Smalley79af7302015-01-21 10:54:10 -05004534 ret = security_binder_set_context_mgr(proc->tsk);
4535 if (ret < 0)
4536 goto out;
Martijn Coenen342e5c92017-02-03 14:40:46 -08004537 if (uid_valid(context->binder_context_mgr_uid)) {
4538 if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004539 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
4540 from_kuid(&init_user_ns, curr_euid),
4541 from_kuid(&init_user_ns,
Martijn Coenen342e5c92017-02-03 14:40:46 -08004542 context->binder_context_mgr_uid));
Tair Rzayev78260ac2014-06-03 22:27:21 +03004543 ret = -EPERM;
4544 goto out;
4545 }
4546 } else {
Martijn Coenen342e5c92017-02-03 14:40:46 -08004547 context->binder_context_mgr_uid = curr_euid;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004548 }
Todd Kjos673068e2017-06-29 12:02:03 -07004549 new_node = binder_new_node(proc, NULL);
Todd Kjosc44b1232017-06-29 12:01:43 -07004550 if (!new_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004551 ret = -ENOMEM;
4552 goto out;
4553 }
Todd Kjos673068e2017-06-29 12:02:03 -07004554 binder_node_lock(new_node);
Todd Kjosc44b1232017-06-29 12:01:43 -07004555 new_node->local_weak_refs++;
4556 new_node->local_strong_refs++;
4557 new_node->has_strong_ref = 1;
4558 new_node->has_weak_ref = 1;
4559 context->binder_context_mgr_node = new_node;
Todd Kjos673068e2017-06-29 12:02:03 -07004560 binder_node_unlock(new_node);
Todd Kjosadc18842017-06-29 12:01:59 -07004561 binder_put_node(new_node);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004562out:
Todd Kjosc44b1232017-06-29 12:01:43 -07004563 mutex_unlock(&context->context_mgr_node_lock);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004564 return ret;
4565}
4566
Colin Crossabcc6152017-08-31 10:04:24 +02004567static int binder_ioctl_get_node_debug_info(struct binder_proc *proc,
4568 struct binder_node_debug_info *info)
4569{
4570 struct rb_node *n;
4571 binder_uintptr_t ptr = info->ptr;
4572
4573 memset(info, 0, sizeof(*info));
4574
4575 binder_inner_proc_lock(proc);
4576 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
4577 struct binder_node *node = rb_entry(n, struct binder_node,
4578 rb_node);
4579 if (node->ptr > ptr) {
4580 info->ptr = node->ptr;
4581 info->cookie = node->cookie;
4582 info->has_strong_ref = node->has_strong_ref;
4583 info->has_weak_ref = node->has_weak_ref;
4584 break;
4585 }
4586 }
4587 binder_inner_proc_unlock(proc);
4588
4589 return 0;
4590}
4591
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004592static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4593{
4594 int ret;
4595 struct binder_proc *proc = filp->private_data;
4596 struct binder_thread *thread;
4597 unsigned int size = _IOC_SIZE(cmd);
4598 void __user *ubuf = (void __user *)arg;
4599
Tair Rzayev78260ac2014-06-03 22:27:21 +03004600 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
4601 proc->pid, current->pid, cmd, arg);*/
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004602
Sherry Yang4175e2b2017-08-23 08:46:40 -07004603 binder_selftest_alloc(&proc->alloc);
4604
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004605 trace_binder_ioctl(cmd, arg);
4606
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004607 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4608 if (ret)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004609 goto err_unlocked;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004610
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004611 thread = binder_get_thread(proc);
4612 if (thread == NULL) {
4613 ret = -ENOMEM;
4614 goto err;
4615 }
4616
4617 switch (cmd) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004618 case BINDER_WRITE_READ:
4619 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
4620 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004621 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004622 break;
Todd Kjosb3e68612017-06-29 12:02:07 -07004623 case BINDER_SET_MAX_THREADS: {
4624 int max_threads;
4625
4626 if (copy_from_user(&max_threads, ubuf,
4627 sizeof(max_threads))) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004628 ret = -EINVAL;
4629 goto err;
4630 }
Todd Kjosb3e68612017-06-29 12:02:07 -07004631 binder_inner_proc_lock(proc);
4632 proc->max_threads = max_threads;
4633 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004634 break;
Todd Kjosb3e68612017-06-29 12:02:07 -07004635 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004636 case BINDER_SET_CONTEXT_MGR:
Tair Rzayev78260ac2014-06-03 22:27:21 +03004637 ret = binder_ioctl_set_ctx_mgr(filp);
4638 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004639 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004640 break;
4641 case BINDER_THREAD_EXIT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304642 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004643 proc->pid, thread->pid);
Todd Kjos7a4408c2017-06-29 12:01:57 -07004644 binder_thread_release(proc, thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004645 thread = NULL;
4646 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02004647 case BINDER_VERSION: {
4648 struct binder_version __user *ver = ubuf;
4649
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004650 if (size != sizeof(struct binder_version)) {
4651 ret = -EINVAL;
4652 goto err;
4653 }
Mathieu Maret36c89c02014-04-15 12:03:05 +02004654 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
4655 &ver->protocol_version)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004656 ret = -EINVAL;
4657 goto err;
4658 }
4659 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02004660 }
Colin Crossabcc6152017-08-31 10:04:24 +02004661 case BINDER_GET_NODE_DEBUG_INFO: {
4662 struct binder_node_debug_info info;
4663
4664 if (copy_from_user(&info, ubuf, sizeof(info))) {
4665 ret = -EFAULT;
4666 goto err;
4667 }
4668
4669 ret = binder_ioctl_get_node_debug_info(proc, &info);
4670 if (ret < 0)
4671 goto err;
4672
4673 if (copy_to_user(ubuf, &info, sizeof(info))) {
4674 ret = -EFAULT;
4675 goto err;
4676 }
4677 break;
4678 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004679 default:
4680 ret = -EINVAL;
4681 goto err;
4682 }
4683 ret = 0;
4684err:
4685 if (thread)
Todd Kjos08dabce2017-06-29 12:01:49 -07004686 thread->looper_need_return = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004687 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4688 if (ret && ret != -ERESTARTSYS)
Anmol Sarma56b468f2012-10-30 22:35:43 +05304689 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 -07004690err_unlocked:
4691 trace_binder_ioctl_done(ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004692 return ret;
4693}
4694
4695static void binder_vma_open(struct vm_area_struct *vma)
4696{
4697 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004698
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004699 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304700 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004701 proc->pid, vma->vm_start, vma->vm_end,
4702 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4703 (unsigned long)pgprot_val(vma->vm_page_prot));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004704}
4705
4706static void binder_vma_close(struct vm_area_struct *vma)
4707{
4708 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004709
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004710 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304711 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004712 proc->pid, vma->vm_start, vma->vm_end,
4713 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4714 (unsigned long)pgprot_val(vma->vm_page_prot));
Todd Kjos19c98722017-06-29 12:01:40 -07004715 binder_alloc_vma_close(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004716 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
4717}
4718
Souptick Joardere19f70a2018-04-23 21:54:00 +05304719static vm_fault_t binder_vm_fault(struct vm_fault *vmf)
Vinayak Menonddac7d52014-06-02 18:17:59 +05304720{
4721 return VM_FAULT_SIGBUS;
4722}
4723
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07004724static const struct vm_operations_struct binder_vm_ops = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004725 .open = binder_vma_open,
4726 .close = binder_vma_close,
Vinayak Menonddac7d52014-06-02 18:17:59 +05304727 .fault = binder_vm_fault,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004728};
4729
Todd Kjos19c98722017-06-29 12:01:40 -07004730static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
4731{
4732 int ret;
4733 struct binder_proc *proc = filp->private_data;
4734 const char *failure_string;
4735
4736 if (proc->tsk != current->group_leader)
4737 return -EINVAL;
4738
4739 if ((vma->vm_end - vma->vm_start) > SZ_4M)
4740 vma->vm_end = vma->vm_start + SZ_4M;
4741
4742 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4743 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
4744 __func__, proc->pid, vma->vm_start, vma->vm_end,
4745 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4746 (unsigned long)pgprot_val(vma->vm_page_prot));
4747
4748 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
4749 ret = -EPERM;
4750 failure_string = "bad vm_flags";
4751 goto err_bad_arg;
4752 }
Minchan Kim720c24192018-05-07 23:15:37 +09004753 vma->vm_flags |= VM_DONTCOPY | VM_MIXEDMAP;
4754 vma->vm_flags &= ~VM_MAYWRITE;
4755
Todd Kjos19c98722017-06-29 12:01:40 -07004756 vma->vm_ops = &binder_vm_ops;
4757 vma->vm_private_data = proc;
4758
4759 ret = binder_alloc_mmap_handler(&proc->alloc, vma);
4760 if (ret)
4761 return ret;
Todd Kjos7f3dc002017-11-27 09:32:33 -08004762 mutex_lock(&proc->files_lock);
Todd Kjos19c98722017-06-29 12:01:40 -07004763 proc->files = get_files_struct(current);
Todd Kjos7f3dc002017-11-27 09:32:33 -08004764 mutex_unlock(&proc->files_lock);
Todd Kjos19c98722017-06-29 12:01:40 -07004765 return 0;
4766
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004767err_bad_arg:
Elad Wexler00c41cd2017-12-29 11:03:37 +02004768 pr_err("%s: %d %lx-%lx %s failed %d\n", __func__,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004769 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
4770 return ret;
4771}
4772
4773static int binder_open(struct inode *nodp, struct file *filp)
4774{
4775 struct binder_proc *proc;
Martijn Coenenac4812c2017-02-03 14:40:48 -08004776 struct binder_device *binder_dev;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004777
Elad Wexler00c41cd2017-12-29 11:03:37 +02004778 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "%s: %d:%d\n", __func__,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004779 current->group_leader->pid, current->pid);
4780
4781 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
4782 if (proc == NULL)
4783 return -ENOMEM;
Todd Kjos9630fe82017-06-29 12:02:00 -07004784 spin_lock_init(&proc->inner_lock);
4785 spin_lock_init(&proc->outer_lock);
Todd Kjosc4ea41b2017-06-29 12:01:36 -07004786 get_task_struct(current->group_leader);
4787 proc->tsk = current->group_leader;
Todd Kjos7f3dc002017-11-27 09:32:33 -08004788 mutex_init(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004789 INIT_LIST_HEAD(&proc->todo);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004790 proc->default_priority = task_nice(current);
Martijn Coenenac4812c2017-02-03 14:40:48 -08004791 binder_dev = container_of(filp->private_data, struct binder_device,
4792 miscdev);
4793 proc->context = &binder_dev->context;
Todd Kjos19c98722017-06-29 12:01:40 -07004794 binder_alloc_init(&proc->alloc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004795
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004796 binder_stats_created(BINDER_STAT_PROC);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004797 proc->pid = current->group_leader->pid;
4798 INIT_LIST_HEAD(&proc->delivered_death);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004799 INIT_LIST_HEAD(&proc->waiting_threads);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004800 filp->private_data = proc;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004801
Todd Kjosc44b1232017-06-29 12:01:43 -07004802 mutex_lock(&binder_procs_lock);
4803 hlist_add_head(&proc->proc_node, &binder_procs);
4804 mutex_unlock(&binder_procs_lock);
4805
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004806 if (binder_debugfs_dir_entry_proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004807 char strbuf[11];
Seunghun Lee10f62862014-05-01 01:30:23 +09004808
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004809 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
Martijn Coenen14db3182017-02-03 14:40:47 -08004810 /*
4811 * proc debug entries are shared between contexts, so
4812 * this will fail if the process tries to open the driver
4813 * again with a different context. The priting code will
4814 * anyway print all contexts that a given PID has, so this
4815 * is not a problem.
4816 */
Harsh Shandilya21d02dd2017-12-22 19:37:02 +05304817 proc->debugfs_entry = debugfs_create_file(strbuf, 0444,
Martijn Coenen14db3182017-02-03 14:40:47 -08004818 binder_debugfs_dir_entry_proc,
4819 (void *)(unsigned long)proc->pid,
4820 &binder_proc_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004821 }
4822
4823 return 0;
4824}
4825
4826static int binder_flush(struct file *filp, fl_owner_t id)
4827{
4828 struct binder_proc *proc = filp->private_data;
4829
4830 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
4831
4832 return 0;
4833}
4834
4835static void binder_deferred_flush(struct binder_proc *proc)
4836{
4837 struct rb_node *n;
4838 int wake_count = 0;
Seunghun Lee10f62862014-05-01 01:30:23 +09004839
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004840 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004841 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
4842 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
Seunghun Lee10f62862014-05-01 01:30:23 +09004843
Todd Kjos08dabce2017-06-29 12:01:49 -07004844 thread->looper_need_return = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004845 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
4846 wake_up_interruptible(&thread->wait);
4847 wake_count++;
4848 }
4849 }
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004850 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004851
4852 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4853 "binder_flush: %d woke %d threads\n", proc->pid,
4854 wake_count);
4855}
4856
4857static int binder_release(struct inode *nodp, struct file *filp)
4858{
4859 struct binder_proc *proc = filp->private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004860
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004861 debugfs_remove(proc->debugfs_entry);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004862 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
4863
4864 return 0;
4865}
4866
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004867static int binder_node_release(struct binder_node *node, int refs)
4868{
4869 struct binder_ref *ref;
4870 int death = 0;
Todd Kjosed297212017-06-29 12:02:01 -07004871 struct binder_proc *proc = node->proc;
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004872
Todd Kjos72196392017-06-29 12:02:02 -07004873 binder_release_work(proc, &node->async_todo);
Todd Kjosed297212017-06-29 12:02:01 -07004874
Todd Kjos673068e2017-06-29 12:02:03 -07004875 binder_node_lock(node);
Todd Kjosed297212017-06-29 12:02:01 -07004876 binder_inner_proc_lock(proc);
Todd Kjos72196392017-06-29 12:02:02 -07004877 binder_dequeue_work_ilocked(&node->work);
Todd Kjosadc18842017-06-29 12:01:59 -07004878 /*
4879 * The caller must have taken a temporary ref on the node,
4880 */
4881 BUG_ON(!node->tmp_refs);
4882 if (hlist_empty(&node->refs) && node->tmp_refs == 1) {
Todd Kjosed297212017-06-29 12:02:01 -07004883 binder_inner_proc_unlock(proc);
Todd Kjos673068e2017-06-29 12:02:03 -07004884 binder_node_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07004885 binder_free_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004886
4887 return refs;
4888 }
4889
4890 node->proc = NULL;
4891 node->local_strong_refs = 0;
4892 node->local_weak_refs = 0;
Todd Kjosed297212017-06-29 12:02:01 -07004893 binder_inner_proc_unlock(proc);
Todd Kjosc44b1232017-06-29 12:01:43 -07004894
4895 spin_lock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004896 hlist_add_head(&node->dead_node, &binder_dead_nodes);
Todd Kjosc44b1232017-06-29 12:01:43 -07004897 spin_unlock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004898
4899 hlist_for_each_entry(ref, &node->refs, node_entry) {
4900 refs++;
Martijn Coenenab51ec62017-06-29 12:02:10 -07004901 /*
4902 * Need the node lock to synchronize
4903 * with new notification requests and the
4904 * inner lock to synchronize with queued
4905 * death notifications.
4906 */
4907 binder_inner_proc_lock(ref->proc);
4908 if (!ref->death) {
4909 binder_inner_proc_unlock(ref->proc);
Arve Hjønnevåge194fd82014-02-17 13:58:29 -08004910 continue;
Martijn Coenenab51ec62017-06-29 12:02:10 -07004911 }
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004912
4913 death++;
4914
Martijn Coenenab51ec62017-06-29 12:02:10 -07004915 BUG_ON(!list_empty(&ref->death->work.entry));
4916 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
4917 binder_enqueue_work_ilocked(&ref->death->work,
4918 &ref->proc->todo);
Martijn Coenen408c68b2017-08-31 10:04:19 +02004919 binder_wakeup_proc_ilocked(ref->proc);
Todd Kjos72196392017-06-29 12:02:02 -07004920 binder_inner_proc_unlock(ref->proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004921 }
4922
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004923 binder_debug(BINDER_DEBUG_DEAD_BINDER,
4924 "node %d now dead, refs %d, death %d\n",
4925 node->debug_id, refs, death);
Todd Kjos673068e2017-06-29 12:02:03 -07004926 binder_node_unlock(node);
Todd Kjosadc18842017-06-29 12:01:59 -07004927 binder_put_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004928
4929 return refs;
4930}
4931
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004932static void binder_deferred_release(struct binder_proc *proc)
4933{
Martijn Coenen342e5c92017-02-03 14:40:46 -08004934 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004935 struct rb_node *n;
Todd Kjos19c98722017-06-29 12:01:40 -07004936 int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004937
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004938 BUG_ON(proc->files);
4939
Todd Kjosc44b1232017-06-29 12:01:43 -07004940 mutex_lock(&binder_procs_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004941 hlist_del(&proc->proc_node);
Todd Kjosc44b1232017-06-29 12:01:43 -07004942 mutex_unlock(&binder_procs_lock);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004943
Todd Kjosc44b1232017-06-29 12:01:43 -07004944 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen342e5c92017-02-03 14:40:46 -08004945 if (context->binder_context_mgr_node &&
4946 context->binder_context_mgr_node->proc == proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004947 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01004948 "%s: %d context_mgr_node gone\n",
4949 __func__, proc->pid);
Martijn Coenen342e5c92017-02-03 14:40:46 -08004950 context->binder_context_mgr_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004951 }
Todd Kjosc44b1232017-06-29 12:01:43 -07004952 mutex_unlock(&context->context_mgr_node_lock);
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004953 binder_inner_proc_lock(proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07004954 /*
4955 * Make sure proc stays alive after we
4956 * remove all the threads
4957 */
4958 proc->tmp_ref++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004959
Todd Kjos7a4408c2017-06-29 12:01:57 -07004960 proc->is_dead = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004961 threads = 0;
4962 active_transactions = 0;
4963 while ((n = rb_first(&proc->threads))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004964 struct binder_thread *thread;
4965
4966 thread = rb_entry(n, struct binder_thread, rb_node);
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004967 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004968 threads++;
Todd Kjos7a4408c2017-06-29 12:01:57 -07004969 active_transactions += binder_thread_release(proc, thread);
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004970 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004971 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004972
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004973 nodes = 0;
4974 incoming_refs = 0;
4975 while ((n = rb_first(&proc->nodes))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004976 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004977
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004978 node = rb_entry(n, struct binder_node, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004979 nodes++;
Todd Kjosadc18842017-06-29 12:01:59 -07004980 /*
4981 * take a temporary ref on the node before
4982 * calling binder_node_release() which will either
4983 * kfree() the node or call binder_put_node()
4984 */
Todd Kjosda0fa9e2017-06-29 12:02:04 -07004985 binder_inc_node_tmpref_ilocked(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004986 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07004987 binder_inner_proc_unlock(proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004988 incoming_refs = binder_node_release(node, incoming_refs);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07004989 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004990 }
Todd Kjosda0fa9e2017-06-29 12:02:04 -07004991 binder_inner_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004992
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004993 outgoing_refs = 0;
Todd Kjos2c1838d2017-06-29 12:02:08 -07004994 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004995 while ((n = rb_first(&proc->refs_by_desc))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004996 struct binder_ref *ref;
4997
4998 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004999 outgoing_refs++;
Todd Kjos2c1838d2017-06-29 12:02:08 -07005000 binder_cleanup_ref_olocked(ref);
5001 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07005002 binder_free_ref(ref);
Todd Kjos2c1838d2017-06-29 12:02:08 -07005003 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005004 }
Todd Kjos2c1838d2017-06-29 12:02:08 -07005005 binder_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005006
Todd Kjos72196392017-06-29 12:02:02 -07005007 binder_release_work(proc, &proc->todo);
5008 binder_release_work(proc, &proc->delivered_death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005009
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005010 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Todd Kjos19c98722017-06-29 12:01:40 -07005011 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01005012 __func__, proc->pid, threads, nodes, incoming_refs,
Todd Kjos19c98722017-06-29 12:01:40 -07005013 outgoing_refs, active_transactions);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005014
Todd Kjos7a4408c2017-06-29 12:01:57 -07005015 binder_proc_dec_tmpref(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005016}
5017
5018static void binder_deferred_func(struct work_struct *work)
5019{
5020 struct binder_proc *proc;
5021 struct files_struct *files;
5022
5023 int defer;
Seunghun Lee10f62862014-05-01 01:30:23 +09005024
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005025 do {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005026 mutex_lock(&binder_deferred_lock);
5027 if (!hlist_empty(&binder_deferred_list)) {
5028 proc = hlist_entry(binder_deferred_list.first,
5029 struct binder_proc, deferred_work_node);
5030 hlist_del_init(&proc->deferred_work_node);
5031 defer = proc->deferred_work;
5032 proc->deferred_work = 0;
5033 } else {
5034 proc = NULL;
5035 defer = 0;
5036 }
5037 mutex_unlock(&binder_deferred_lock);
5038
5039 files = NULL;
5040 if (defer & BINDER_DEFERRED_PUT_FILES) {
Todd Kjos7f3dc002017-11-27 09:32:33 -08005041 mutex_lock(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005042 files = proc->files;
5043 if (files)
5044 proc->files = NULL;
Todd Kjos7f3dc002017-11-27 09:32:33 -08005045 mutex_unlock(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005046 }
5047
5048 if (defer & BINDER_DEFERRED_FLUSH)
5049 binder_deferred_flush(proc);
5050
5051 if (defer & BINDER_DEFERRED_RELEASE)
5052 binder_deferred_release(proc); /* frees proc */
5053
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005054 if (files)
5055 put_files_struct(files);
5056 } while (proc);
5057}
5058static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
5059
5060static void
5061binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
5062{
5063 mutex_lock(&binder_deferred_lock);
5064 proc->deferred_work |= defer;
5065 if (hlist_unhashed(&proc->deferred_work_node)) {
5066 hlist_add_head(&proc->deferred_work_node,
5067 &binder_deferred_list);
Bhaktipriya Shridhar1beba522016-08-13 22:16:24 +05305068 schedule_work(&binder_deferred_work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005069 }
5070 mutex_unlock(&binder_deferred_lock);
5071}
5072
Todd Kjos5f2f6362017-06-29 12:02:09 -07005073static void print_binder_transaction_ilocked(struct seq_file *m,
5074 struct binder_proc *proc,
5075 const char *prefix,
5076 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005077{
Todd Kjos5f2f6362017-06-29 12:02:09 -07005078 struct binder_proc *to_proc;
5079 struct binder_buffer *buffer = t->buffer;
5080
Todd Kjos7a4408c2017-06-29 12:01:57 -07005081 spin_lock(&t->lock);
Todd Kjos5f2f6362017-06-29 12:02:09 -07005082 to_proc = t->to_proc;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005083 seq_printf(m,
Todd Kjos8ca86f12018-02-07 13:57:37 -08005084 "%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 -07005085 prefix, t->debug_id, t,
5086 t->from ? t->from->proc->pid : 0,
5087 t->from ? t->from->pid : 0,
Todd Kjos5f2f6362017-06-29 12:02:09 -07005088 to_proc ? to_proc->pid : 0,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005089 t->to_thread ? t->to_thread->pid : 0,
5090 t->code, t->flags, t->priority, t->need_reply);
Todd Kjos7a4408c2017-06-29 12:01:57 -07005091 spin_unlock(&t->lock);
5092
Todd Kjos5f2f6362017-06-29 12:02:09 -07005093 if (proc != to_proc) {
5094 /*
5095 * Can only safely deref buffer if we are holding the
5096 * correct proc inner lock for this node
5097 */
5098 seq_puts(m, "\n");
5099 return;
5100 }
5101
5102 if (buffer == NULL) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005103 seq_puts(m, " buffer free\n");
5104 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005105 }
Todd Kjos5f2f6362017-06-29 12:02:09 -07005106 if (buffer->target_node)
5107 seq_printf(m, " node %d", buffer->target_node->debug_id);
Todd Kjos8ca86f12018-02-07 13:57:37 -08005108 seq_printf(m, " size %zd:%zd data %pK\n",
Todd Kjos5f2f6362017-06-29 12:02:09 -07005109 buffer->data_size, buffer->offsets_size,
5110 buffer->data);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005111}
5112
Todd Kjos5f2f6362017-06-29 12:02:09 -07005113static void print_binder_work_ilocked(struct seq_file *m,
5114 struct binder_proc *proc,
5115 const char *prefix,
5116 const char *transaction_prefix,
5117 struct binder_work *w)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005118{
5119 struct binder_node *node;
5120 struct binder_transaction *t;
5121
5122 switch (w->type) {
5123 case BINDER_WORK_TRANSACTION:
5124 t = container_of(w, struct binder_transaction, work);
Todd Kjos5f2f6362017-06-29 12:02:09 -07005125 print_binder_transaction_ilocked(
5126 m, proc, transaction_prefix, t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005127 break;
Todd Kjos26549d12017-06-29 12:01:55 -07005128 case BINDER_WORK_RETURN_ERROR: {
5129 struct binder_error *e = container_of(
5130 w, struct binder_error, work);
5131
5132 seq_printf(m, "%stransaction error: %u\n",
5133 prefix, e->cmd);
5134 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005135 case BINDER_WORK_TRANSACTION_COMPLETE:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005136 seq_printf(m, "%stransaction complete\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005137 break;
5138 case BINDER_WORK_NODE:
5139 node = container_of(w, struct binder_node, work);
Arve Hjønnevågda498892014-02-21 14:40:26 -08005140 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
5141 prefix, node->debug_id,
5142 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005143 break;
5144 case BINDER_WORK_DEAD_BINDER:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005145 seq_printf(m, "%shas dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005146 break;
5147 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005148 seq_printf(m, "%shas cleared dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005149 break;
5150 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005151 seq_printf(m, "%shas cleared death notification\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005152 break;
5153 default:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005154 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005155 break;
5156 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005157}
5158
Todd Kjos72196392017-06-29 12:02:02 -07005159static void print_binder_thread_ilocked(struct seq_file *m,
5160 struct binder_thread *thread,
5161 int print_always)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005162{
5163 struct binder_transaction *t;
5164 struct binder_work *w;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005165 size_t start_pos = m->count;
5166 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005167
Todd Kjos7a4408c2017-06-29 12:01:57 -07005168 seq_printf(m, " thread %d: l %02x need_return %d tr %d\n",
Todd Kjos08dabce2017-06-29 12:01:49 -07005169 thread->pid, thread->looper,
Todd Kjos7a4408c2017-06-29 12:01:57 -07005170 thread->looper_need_return,
5171 atomic_read(&thread->tmp_ref));
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005172 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005173 t = thread->transaction_stack;
5174 while (t) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005175 if (t->from == thread) {
Todd Kjos5f2f6362017-06-29 12:02:09 -07005176 print_binder_transaction_ilocked(m, thread->proc,
5177 " outgoing transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005178 t = t->from_parent;
5179 } else if (t->to_thread == thread) {
Todd Kjos5f2f6362017-06-29 12:02:09 -07005180 print_binder_transaction_ilocked(m, thread->proc,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005181 " incoming transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005182 t = t->to_parent;
5183 } else {
Todd Kjos5f2f6362017-06-29 12:02:09 -07005184 print_binder_transaction_ilocked(m, thread->proc,
5185 " bad transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005186 t = NULL;
5187 }
5188 }
5189 list_for_each_entry(w, &thread->todo, entry) {
Todd Kjos5f2f6362017-06-29 12:02:09 -07005190 print_binder_work_ilocked(m, thread->proc, " ",
Todd Kjos72196392017-06-29 12:02:02 -07005191 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005192 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005193 if (!print_always && m->count == header_pos)
5194 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005195}
5196
Todd Kjosda0fa9e2017-06-29 12:02:04 -07005197static void print_binder_node_nilocked(struct seq_file *m,
5198 struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005199{
5200 struct binder_ref *ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005201 struct binder_work *w;
5202 int count;
5203
5204 count = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08005205 hlist_for_each_entry(ref, &node->refs, node_entry)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005206 count++;
5207
Todd Kjosadc18842017-06-29 12:01:59 -07005208 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 -08005209 node->debug_id, (u64)node->ptr, (u64)node->cookie,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005210 node->has_strong_ref, node->has_weak_ref,
5211 node->local_strong_refs, node->local_weak_refs,
Todd Kjosadc18842017-06-29 12:01:59 -07005212 node->internal_strong_refs, count, node->tmp_refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005213 if (count) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005214 seq_puts(m, " proc");
Sasha Levinb67bfe02013-02-27 17:06:00 -08005215 hlist_for_each_entry(ref, &node->refs, node_entry)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005216 seq_printf(m, " %d", ref->proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005217 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005218 seq_puts(m, "\n");
Todd Kjos72196392017-06-29 12:02:02 -07005219 if (node->proc) {
Todd Kjos72196392017-06-29 12:02:02 -07005220 list_for_each_entry(w, &node->async_todo, entry)
Todd Kjos5f2f6362017-06-29 12:02:09 -07005221 print_binder_work_ilocked(m, node->proc, " ",
Todd Kjos72196392017-06-29 12:02:02 -07005222 " pending async transaction", w);
Todd Kjos72196392017-06-29 12:02:02 -07005223 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005224}
5225
Todd Kjos2c1838d2017-06-29 12:02:08 -07005226static void print_binder_ref_olocked(struct seq_file *m,
5227 struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005228{
Todd Kjos673068e2017-06-29 12:02:03 -07005229 binder_node_lock(ref->node);
Todd Kjos372e3142017-06-29 12:01:58 -07005230 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
5231 ref->data.debug_id, ref->data.desc,
5232 ref->node->proc ? "" : "dead ",
5233 ref->node->debug_id, ref->data.strong,
5234 ref->data.weak, ref->death);
Todd Kjos673068e2017-06-29 12:02:03 -07005235 binder_node_unlock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005236}
5237
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005238static void print_binder_proc(struct seq_file *m,
5239 struct binder_proc *proc, int print_all)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005240{
5241 struct binder_work *w;
5242 struct rb_node *n;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005243 size_t start_pos = m->count;
5244 size_t header_pos;
Todd Kjosda0fa9e2017-06-29 12:02:04 -07005245 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005246
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005247 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen14db3182017-02-03 14:40:47 -08005248 seq_printf(m, "context %s\n", proc->context->name);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005249 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005250
Todd Kjos72196392017-06-29 12:02:02 -07005251 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005252 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
Todd Kjos72196392017-06-29 12:02:02 -07005253 print_binder_thread_ilocked(m, rb_entry(n, struct binder_thread,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005254 rb_node), print_all);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07005255
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005256 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005257 struct binder_node *node = rb_entry(n, struct binder_node,
5258 rb_node);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07005259 /*
5260 * take a temporary reference on the node so it
5261 * survives and isn't removed from the tree
5262 * while we print it.
5263 */
5264 binder_inc_node_tmpref_ilocked(node);
5265 /* Need to drop inner lock to take node lock */
5266 binder_inner_proc_unlock(proc);
5267 if (last_node)
5268 binder_put_node(last_node);
5269 binder_node_inner_lock(node);
5270 print_binder_node_nilocked(m, node);
5271 binder_node_inner_unlock(node);
5272 last_node = node;
5273 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005274 }
Todd Kjosda0fa9e2017-06-29 12:02:04 -07005275 binder_inner_proc_unlock(proc);
5276 if (last_node)
5277 binder_put_node(last_node);
5278
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005279 if (print_all) {
Todd Kjos2c1838d2017-06-29 12:02:08 -07005280 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005281 for (n = rb_first(&proc->refs_by_desc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005282 n != NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005283 n = rb_next(n))
Todd Kjos2c1838d2017-06-29 12:02:08 -07005284 print_binder_ref_olocked(m, rb_entry(n,
5285 struct binder_ref,
5286 rb_node_desc));
5287 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005288 }
Todd Kjos19c98722017-06-29 12:01:40 -07005289 binder_alloc_print_allocated(m, &proc->alloc);
Todd Kjos72196392017-06-29 12:02:02 -07005290 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005291 list_for_each_entry(w, &proc->todo, entry)
Todd Kjos5f2f6362017-06-29 12:02:09 -07005292 print_binder_work_ilocked(m, proc, " ",
5293 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005294 list_for_each_entry(w, &proc->delivered_death, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005295 seq_puts(m, " has delivered dead binder\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005296 break;
5297 }
Todd Kjos72196392017-06-29 12:02:02 -07005298 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005299 if (!print_all && m->count == header_pos)
5300 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005301}
5302
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005303static const char * const binder_return_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005304 "BR_ERROR",
5305 "BR_OK",
5306 "BR_TRANSACTION",
5307 "BR_REPLY",
5308 "BR_ACQUIRE_RESULT",
5309 "BR_DEAD_REPLY",
5310 "BR_TRANSACTION_COMPLETE",
5311 "BR_INCREFS",
5312 "BR_ACQUIRE",
5313 "BR_RELEASE",
5314 "BR_DECREFS",
5315 "BR_ATTEMPT_ACQUIRE",
5316 "BR_NOOP",
5317 "BR_SPAWN_LOOPER",
5318 "BR_FINISHED",
5319 "BR_DEAD_BINDER",
5320 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
5321 "BR_FAILED_REPLY"
5322};
5323
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005324static const char * const binder_command_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005325 "BC_TRANSACTION",
5326 "BC_REPLY",
5327 "BC_ACQUIRE_RESULT",
5328 "BC_FREE_BUFFER",
5329 "BC_INCREFS",
5330 "BC_ACQUIRE",
5331 "BC_RELEASE",
5332 "BC_DECREFS",
5333 "BC_INCREFS_DONE",
5334 "BC_ACQUIRE_DONE",
5335 "BC_ATTEMPT_ACQUIRE",
5336 "BC_REGISTER_LOOPER",
5337 "BC_ENTER_LOOPER",
5338 "BC_EXIT_LOOPER",
5339 "BC_REQUEST_DEATH_NOTIFICATION",
5340 "BC_CLEAR_DEATH_NOTIFICATION",
Martijn Coenen79802402017-02-03 14:40:51 -08005341 "BC_DEAD_BINDER_DONE",
5342 "BC_TRANSACTION_SG",
5343 "BC_REPLY_SG",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005344};
5345
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005346static const char * const binder_objstat_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005347 "proc",
5348 "thread",
5349 "node",
5350 "ref",
5351 "death",
5352 "transaction",
5353 "transaction_complete"
5354};
5355
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005356static void print_binder_stats(struct seq_file *m, const char *prefix,
5357 struct binder_stats *stats)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005358{
5359 int i;
5360
5361 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005362 ARRAY_SIZE(binder_command_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005363 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07005364 int temp = atomic_read(&stats->bc[i]);
5365
5366 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005367 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07005368 binder_command_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005369 }
5370
5371 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005372 ARRAY_SIZE(binder_return_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005373 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07005374 int temp = atomic_read(&stats->br[i]);
5375
5376 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005377 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07005378 binder_return_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005379 }
5380
5381 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005382 ARRAY_SIZE(binder_objstat_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005383 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005384 ARRAY_SIZE(stats->obj_deleted));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005385 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07005386 int created = atomic_read(&stats->obj_created[i]);
5387 int deleted = atomic_read(&stats->obj_deleted[i]);
5388
5389 if (created || deleted)
5390 seq_printf(m, "%s%s: active %d total %d\n",
5391 prefix,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005392 binder_objstat_strings[i],
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07005393 created - deleted,
5394 created);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005395 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005396}
5397
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005398static void print_binder_proc_stats(struct seq_file *m,
5399 struct binder_proc *proc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005400{
5401 struct binder_work *w;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02005402 struct binder_thread *thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005403 struct rb_node *n;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02005404 int count, strong, weak, ready_threads;
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07005405 size_t free_async_space =
5406 binder_alloc_get_free_async_space(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005407
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005408 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen14db3182017-02-03 14:40:47 -08005409 seq_printf(m, "context %s\n", proc->context->name);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005410 count = 0;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02005411 ready_threads = 0;
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07005412 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005413 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
5414 count++;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02005415
5416 list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node)
5417 ready_threads++;
5418
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005419 seq_printf(m, " threads: %d\n", count);
5420 seq_printf(m, " requested threads: %d+%d/%d\n"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005421 " ready threads %d\n"
5422 " free async space %zd\n", proc->requested_threads,
5423 proc->requested_threads_started, proc->max_threads,
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02005424 ready_threads,
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07005425 free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005426 count = 0;
5427 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
5428 count++;
Todd Kjosda0fa9e2017-06-29 12:02:04 -07005429 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005430 seq_printf(m, " nodes: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005431 count = 0;
5432 strong = 0;
5433 weak = 0;
Todd Kjos2c1838d2017-06-29 12:02:08 -07005434 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005435 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
5436 struct binder_ref *ref = rb_entry(n, struct binder_ref,
5437 rb_node_desc);
5438 count++;
Todd Kjos372e3142017-06-29 12:01:58 -07005439 strong += ref->data.strong;
5440 weak += ref->data.weak;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005441 }
Todd Kjos2c1838d2017-06-29 12:02:08 -07005442 binder_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005443 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005444
Todd Kjos19c98722017-06-29 12:01:40 -07005445 count = binder_alloc_get_allocated_count(&proc->alloc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005446 seq_printf(m, " buffers: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005447
Sherry Yang8ef46652017-08-31 11:56:36 -07005448 binder_alloc_print_pages(m, &proc->alloc);
5449
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005450 count = 0;
Todd Kjos72196392017-06-29 12:02:02 -07005451 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005452 list_for_each_entry(w, &proc->todo, entry) {
Todd Kjos72196392017-06-29 12:02:02 -07005453 if (w->type == BINDER_WORK_TRANSACTION)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005454 count++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005455 }
Todd Kjos72196392017-06-29 12:02:02 -07005456 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005457 seq_printf(m, " pending transactions: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005458
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005459 print_binder_stats(m, " ", &proc->stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005460}
5461
5462
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005463static int binder_state_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005464{
5465 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005466 struct binder_node *node;
Todd Kjos673068e2017-06-29 12:02:03 -07005467 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005468
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005469 seq_puts(m, "binder state:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005470
Todd Kjosc44b1232017-06-29 12:01:43 -07005471 spin_lock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005472 if (!hlist_empty(&binder_dead_nodes))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005473 seq_puts(m, "dead nodes:\n");
Todd Kjos673068e2017-06-29 12:02:03 -07005474 hlist_for_each_entry(node, &binder_dead_nodes, dead_node) {
5475 /*
5476 * take a temporary reference on the node so it
5477 * survives and isn't removed from the list
5478 * while we print it.
5479 */
5480 node->tmp_refs++;
5481 spin_unlock(&binder_dead_nodes_lock);
5482 if (last_node)
5483 binder_put_node(last_node);
5484 binder_node_lock(node);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07005485 print_binder_node_nilocked(m, node);
Todd Kjos673068e2017-06-29 12:02:03 -07005486 binder_node_unlock(node);
5487 last_node = node;
5488 spin_lock(&binder_dead_nodes_lock);
5489 }
Todd Kjosc44b1232017-06-29 12:01:43 -07005490 spin_unlock(&binder_dead_nodes_lock);
Todd Kjos673068e2017-06-29 12:02:03 -07005491 if (last_node)
5492 binder_put_node(last_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005493
Todd Kjosc44b1232017-06-29 12:01:43 -07005494 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005495 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005496 print_binder_proc(m, proc, 1);
Todd Kjosc44b1232017-06-29 12:01:43 -07005497 mutex_unlock(&binder_procs_lock);
Todd Kjosa60b8902017-06-29 12:02:11 -07005498
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005499 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005500}
5501
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005502static int binder_stats_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005503{
5504 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005505
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005506 seq_puts(m, "binder stats:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005507
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005508 print_binder_stats(m, "", &binder_stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005509
Todd Kjosc44b1232017-06-29 12:01:43 -07005510 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005511 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005512 print_binder_proc_stats(m, proc);
Todd Kjosc44b1232017-06-29 12:01:43 -07005513 mutex_unlock(&binder_procs_lock);
Todd Kjosa60b8902017-06-29 12:02:11 -07005514
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005515 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005516}
5517
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005518static int binder_transactions_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005519{
5520 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005521
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005522 seq_puts(m, "binder transactions:\n");
Todd Kjosc44b1232017-06-29 12:01:43 -07005523 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005524 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005525 print_binder_proc(m, proc, 0);
Todd Kjosc44b1232017-06-29 12:01:43 -07005526 mutex_unlock(&binder_procs_lock);
Todd Kjosa60b8902017-06-29 12:02:11 -07005527
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005528 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005529}
5530
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005531static int binder_proc_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005532{
Riley Andrews83050a42016-02-09 21:05:33 -08005533 struct binder_proc *itr;
Martijn Coenen14db3182017-02-03 14:40:47 -08005534 int pid = (unsigned long)m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005535
Todd Kjosc44b1232017-06-29 12:01:43 -07005536 mutex_lock(&binder_procs_lock);
Riley Andrews83050a42016-02-09 21:05:33 -08005537 hlist_for_each_entry(itr, &binder_procs, proc_node) {
Martijn Coenen14db3182017-02-03 14:40:47 -08005538 if (itr->pid == pid) {
5539 seq_puts(m, "binder proc state:\n");
5540 print_binder_proc(m, itr, 1);
Riley Andrews83050a42016-02-09 21:05:33 -08005541 }
5542 }
Todd Kjosc44b1232017-06-29 12:01:43 -07005543 mutex_unlock(&binder_procs_lock);
5544
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005545 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005546}
5547
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005548static void print_binder_transaction_log_entry(struct seq_file *m,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005549 struct binder_transaction_log_entry *e)
5550{
Todd Kjosd99c7332017-06-29 12:01:53 -07005551 int debug_id = READ_ONCE(e->debug_id_done);
5552 /*
5553 * read barrier to guarantee debug_id_done read before
5554 * we print the log values
5555 */
5556 smp_rmb();
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005557 seq_printf(m,
Todd Kjosd99c7332017-06-29 12:01:53 -07005558 "%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 -07005559 e->debug_id, (e->call_type == 2) ? "reply" :
5560 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
Martijn Coenen14db3182017-02-03 14:40:47 -08005561 e->from_thread, e->to_proc, e->to_thread, e->context_name,
Todd Kjos57ada2f2017-06-29 12:01:46 -07005562 e->to_node, e->target_handle, e->data_size, e->offsets_size,
5563 e->return_error, e->return_error_param,
5564 e->return_error_line);
Todd Kjosd99c7332017-06-29 12:01:53 -07005565 /*
5566 * read-barrier to guarantee read of debug_id_done after
5567 * done printing the fields of the entry
5568 */
5569 smp_rmb();
5570 seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ?
5571 "\n" : " (incomplete)\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005572}
5573
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005574static int binder_transaction_log_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005575{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005576 struct binder_transaction_log *log = m->private;
Todd Kjosd99c7332017-06-29 12:01:53 -07005577 unsigned int log_cur = atomic_read(&log->cur);
5578 unsigned int count;
5579 unsigned int cur;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005580 int i;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005581
Todd Kjosd99c7332017-06-29 12:01:53 -07005582 count = log_cur + 1;
5583 cur = count < ARRAY_SIZE(log->entry) && !log->full ?
5584 0 : count % ARRAY_SIZE(log->entry);
5585 if (count > ARRAY_SIZE(log->entry) || log->full)
5586 count = ARRAY_SIZE(log->entry);
5587 for (i = 0; i < count; i++) {
5588 unsigned int index = cur++ % ARRAY_SIZE(log->entry);
5589
5590 print_binder_transaction_log_entry(m, &log->entry[index]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005591 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005592 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005593}
5594
5595static const struct file_operations binder_fops = {
5596 .owner = THIS_MODULE,
5597 .poll = binder_poll,
5598 .unlocked_ioctl = binder_ioctl,
Arve Hjønnevågda498892014-02-21 14:40:26 -08005599 .compat_ioctl = binder_ioctl,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005600 .mmap = binder_mmap,
5601 .open = binder_open,
5602 .flush = binder_flush,
5603 .release = binder_release,
5604};
5605
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005606BINDER_DEBUG_ENTRY(state);
5607BINDER_DEBUG_ENTRY(stats);
5608BINDER_DEBUG_ENTRY(transactions);
5609BINDER_DEBUG_ENTRY(transaction_log);
5610
Martijn Coenenac4812c2017-02-03 14:40:48 -08005611static int __init init_binder_device(const char *name)
5612{
5613 int ret;
5614 struct binder_device *binder_device;
5615
5616 binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
5617 if (!binder_device)
5618 return -ENOMEM;
5619
5620 binder_device->miscdev.fops = &binder_fops;
5621 binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
5622 binder_device->miscdev.name = name;
5623
5624 binder_device->context.binder_context_mgr_uid = INVALID_UID;
5625 binder_device->context.name = name;
Todd Kjosc44b1232017-06-29 12:01:43 -07005626 mutex_init(&binder_device->context.context_mgr_node_lock);
Martijn Coenenac4812c2017-02-03 14:40:48 -08005627
5628 ret = misc_register(&binder_device->miscdev);
5629 if (ret < 0) {
5630 kfree(binder_device);
5631 return ret;
5632 }
5633
5634 hlist_add_head(&binder_device->hlist, &binder_devices);
5635
5636 return ret;
5637}
5638
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005639static int __init binder_init(void)
5640{
5641 int ret;
Christian Brauner22eb9472017-08-21 16:13:28 +02005642 char *device_name, *device_names, *device_tmp;
Martijn Coenenac4812c2017-02-03 14:40:48 -08005643 struct binder_device *device;
5644 struct hlist_node *tmp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005645
Tetsuo Handa533dfb22017-11-29 22:29:47 +09005646 ret = binder_alloc_shrinker_init();
5647 if (ret)
5648 return ret;
Sherry Yangf2517eb2017-08-23 08:46:42 -07005649
Todd Kjosd99c7332017-06-29 12:01:53 -07005650 atomic_set(&binder_transaction_log.cur, ~0U);
5651 atomic_set(&binder_transaction_log_failed.cur, ~0U);
5652
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005653 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
5654 if (binder_debugfs_dir_entry_root)
5655 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
5656 binder_debugfs_dir_entry_root);
Martijn Coenenac4812c2017-02-03 14:40:48 -08005657
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005658 if (binder_debugfs_dir_entry_root) {
5659 debugfs_create_file("state",
Harsh Shandilya21d02dd2017-12-22 19:37:02 +05305660 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005661 binder_debugfs_dir_entry_root,
5662 NULL,
5663 &binder_state_fops);
5664 debugfs_create_file("stats",
Harsh Shandilya21d02dd2017-12-22 19:37:02 +05305665 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005666 binder_debugfs_dir_entry_root,
5667 NULL,
5668 &binder_stats_fops);
5669 debugfs_create_file("transactions",
Harsh Shandilya21d02dd2017-12-22 19:37:02 +05305670 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005671 binder_debugfs_dir_entry_root,
5672 NULL,
5673 &binder_transactions_fops);
5674 debugfs_create_file("transaction_log",
Harsh Shandilya21d02dd2017-12-22 19:37:02 +05305675 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005676 binder_debugfs_dir_entry_root,
5677 &binder_transaction_log,
5678 &binder_transaction_log_fops);
5679 debugfs_create_file("failed_transaction_log",
Harsh Shandilya21d02dd2017-12-22 19:37:02 +05305680 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005681 binder_debugfs_dir_entry_root,
5682 &binder_transaction_log_failed,
5683 &binder_transaction_log_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005684 }
Martijn Coenenac4812c2017-02-03 14:40:48 -08005685
5686 /*
5687 * Copy the module_parameter string, because we don't want to
5688 * tokenize it in-place.
5689 */
5690 device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL);
5691 if (!device_names) {
5692 ret = -ENOMEM;
5693 goto err_alloc_device_names_failed;
5694 }
5695 strcpy(device_names, binder_devices_param);
5696
Christian Brauner22eb9472017-08-21 16:13:28 +02005697 device_tmp = device_names;
5698 while ((device_name = strsep(&device_tmp, ","))) {
Martijn Coenenac4812c2017-02-03 14:40:48 -08005699 ret = init_binder_device(device_name);
5700 if (ret)
5701 goto err_init_binder_device_failed;
5702 }
5703
5704 return ret;
5705
5706err_init_binder_device_failed:
5707 hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
5708 misc_deregister(&device->miscdev);
5709 hlist_del(&device->hlist);
5710 kfree(device);
5711 }
Christian Brauner22eb9472017-08-21 16:13:28 +02005712
5713 kfree(device_names);
5714
Martijn Coenenac4812c2017-02-03 14:40:48 -08005715err_alloc_device_names_failed:
5716 debugfs_remove_recursive(binder_debugfs_dir_entry_root);
5717
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005718 return ret;
5719}
5720
5721device_initcall(binder_init);
5722
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07005723#define CREATE_TRACE_POINTS
5724#include "binder_trace.h"
5725
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005726MODULE_LICENSE("GPL v2");