blob: 9c30d38c6f0d893cb85e0df3f15cbe62ae351dce [file] [log] [blame]
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001/* binder.c
2 *
3 * Android IPC Subsystem
4 *
5 * Copyright (C) 2007-2008 Google, Inc.
6 *
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
Todd Kjos9630fe82017-06-29 12:02:00 -070018/*
19 * Locking overview
20 *
21 * There are 3 main spinlocks which must be acquired in the
22 * order shown:
23 *
24 * 1) proc->outer_lock : protects binder_ref
25 * binder_proc_lock() and binder_proc_unlock() are
26 * used to acq/rel.
27 * 2) node->lock : protects most fields of binder_node.
28 * binder_node_lock() and binder_node_unlock() are
29 * used to acq/rel
30 * 3) proc->inner_lock : protects the thread and node lists
Martijn Coenen1b77e9d2017-08-31 10:04:18 +020031 * (proc->threads, proc->waiting_threads, proc->nodes)
32 * and all todo lists associated with the binder_proc
33 * (proc->todo, thread->todo, proc->delivered_death and
34 * node->async_todo), as well as thread->transaction_stack
Todd Kjos9630fe82017-06-29 12:02:00 -070035 * binder_inner_proc_lock() and binder_inner_proc_unlock()
36 * are used to acq/rel
37 *
38 * Any lock under procA must never be nested under any lock at the same
39 * level or below on procB.
40 *
41 * Functions that require a lock held on entry indicate which lock
42 * in the suffix of the function name:
43 *
44 * foo_olocked() : requires node->outer_lock
45 * foo_nlocked() : requires node->lock
46 * foo_ilocked() : requires proc->inner_lock
47 * foo_oilocked(): requires proc->outer_lock and proc->inner_lock
48 * foo_nilocked(): requires node->lock and proc->inner_lock
49 * ...
50 */
51
Anmol Sarma56b468f2012-10-30 22:35:43 +053052#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
53
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090054#include <asm/cacheflush.h>
55#include <linux/fdtable.h>
56#include <linux/file.h>
Colin Crosse2610b22013-05-06 23:50:15 +000057#include <linux/freezer.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090058#include <linux/fs.h>
59#include <linux/list.h>
60#include <linux/miscdevice.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090061#include <linux/module.h>
62#include <linux/mutex.h>
63#include <linux/nsproxy.h>
64#include <linux/poll.h>
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070065#include <linux/debugfs.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090066#include <linux/rbtree.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010067#include <linux/sched/signal.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010068#include <linux/sched/mm.h>
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070069#include <linux/seq_file.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090070#include <linux/uaccess.h>
Eric W. Biederman17cf22c2010-03-02 14:51:53 -080071#include <linux/pid_namespace.h>
Stephen Smalley79af7302015-01-21 10:54:10 -050072#include <linux/security.h>
Todd Kjos9630fe82017-06-29 12:02:00 -070073#include <linux/spinlock.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090074
Greg Kroah-Hartman9246a4a2014-10-16 15:26:51 +020075#include <uapi/linux/android/binder.h>
Todd Kjos0c972a02017-06-29 12:01:41 -070076#include "binder_alloc.h"
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -070077#include "binder_trace.h"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090078
Todd Kjosc44b1232017-06-29 12:01:43 -070079static HLIST_HEAD(binder_deferred_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090080static DEFINE_MUTEX(binder_deferred_lock);
81
Martijn Coenenac4812c2017-02-03 14:40:48 -080082static HLIST_HEAD(binder_devices);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090083static HLIST_HEAD(binder_procs);
Todd Kjosc44b1232017-06-29 12:01:43 -070084static DEFINE_MUTEX(binder_procs_lock);
85
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090086static HLIST_HEAD(binder_dead_nodes);
Todd Kjosc44b1232017-06-29 12:01:43 -070087static DEFINE_SPINLOCK(binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090088
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070089static struct dentry *binder_debugfs_dir_entry_root;
90static struct dentry *binder_debugfs_dir_entry_proc;
Todd Kjos656a8002017-06-29 12:01:45 -070091static atomic_t binder_last_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090092
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070093#define BINDER_DEBUG_ENTRY(name) \
94static int binder_##name##_open(struct inode *inode, struct file *file) \
95{ \
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070096 return single_open(file, binder_##name##_show, inode->i_private); \
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070097} \
98\
99static const struct file_operations binder_##name##_fops = { \
100 .owner = THIS_MODULE, \
101 .open = binder_##name##_open, \
102 .read = seq_read, \
103 .llseek = seq_lseek, \
104 .release = single_release, \
105}
106
107static int binder_proc_show(struct seq_file *m, void *unused);
108BINDER_DEBUG_ENTRY(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900109
110/* This is only defined in include/asm-arm/sizes.h */
111#ifndef SZ_1K
112#define SZ_1K 0x400
113#endif
114
115#ifndef SZ_4M
116#define SZ_4M 0x400000
117#endif
118
119#define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
120
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900121enum {
122 BINDER_DEBUG_USER_ERROR = 1U << 0,
123 BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1,
124 BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2,
125 BINDER_DEBUG_OPEN_CLOSE = 1U << 3,
126 BINDER_DEBUG_DEAD_BINDER = 1U << 4,
127 BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5,
128 BINDER_DEBUG_READ_WRITE = 1U << 6,
129 BINDER_DEBUG_USER_REFS = 1U << 7,
130 BINDER_DEBUG_THREADS = 1U << 8,
131 BINDER_DEBUG_TRANSACTION = 1U << 9,
132 BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10,
133 BINDER_DEBUG_FREE_BUFFER = 1U << 11,
134 BINDER_DEBUG_INTERNAL_REFS = 1U << 12,
Todd Kjos19c98722017-06-29 12:01:40 -0700135 BINDER_DEBUG_PRIORITY_CAP = 1U << 13,
Todd Kjos9630fe82017-06-29 12:02:00 -0700136 BINDER_DEBUG_SPINLOCKS = 1U << 14,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900137};
138static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR |
139 BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION;
Harsh Shandilya21d02dd2017-12-22 19:37:02 +0530140module_param_named(debug_mask, binder_debug_mask, uint, 0644);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900141
Martijn Coenenac4812c2017-02-03 14:40:48 -0800142static char *binder_devices_param = CONFIG_ANDROID_BINDER_DEVICES;
143module_param_named(devices, binder_devices_param, charp, 0444);
144
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900145static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
146static int binder_stop_on_user_error;
147
148static int binder_set_stop_on_user_error(const char *val,
Kees Cooke4dca7b2017-10-17 19:04:42 -0700149 const struct kernel_param *kp)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900150{
151 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +0900152
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900153 ret = param_set_int(val, kp);
154 if (binder_stop_on_user_error < 2)
155 wake_up(&binder_user_error_wait);
156 return ret;
157}
158module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
Harsh Shandilya21d02dd2017-12-22 19:37:02 +0530159 param_get_int, &binder_stop_on_user_error, 0644);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900160
161#define binder_debug(mask, x...) \
162 do { \
163 if (binder_debug_mask & mask) \
Sherwin Soltani258767f2012-06-26 02:00:30 -0400164 pr_info(x); \
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900165 } while (0)
166
167#define binder_user_error(x...) \
168 do { \
169 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
Sherwin Soltani258767f2012-06-26 02:00:30 -0400170 pr_info(x); \
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900171 if (binder_stop_on_user_error) \
172 binder_stop_on_user_error = 2; \
173 } while (0)
174
Martijn Coenenfeba3902017-02-03 14:40:45 -0800175#define to_flat_binder_object(hdr) \
176 container_of(hdr, struct flat_binder_object, hdr)
177
178#define to_binder_fd_object(hdr) container_of(hdr, struct binder_fd_object, hdr)
179
Martijn Coenen79802402017-02-03 14:40:51 -0800180#define to_binder_buffer_object(hdr) \
181 container_of(hdr, struct binder_buffer_object, hdr)
182
Martijn Coenendef95c72017-02-03 14:40:52 -0800183#define to_binder_fd_array_object(hdr) \
184 container_of(hdr, struct binder_fd_array_object, hdr)
185
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900186enum binder_stat_types {
187 BINDER_STAT_PROC,
188 BINDER_STAT_THREAD,
189 BINDER_STAT_NODE,
190 BINDER_STAT_REF,
191 BINDER_STAT_DEATH,
192 BINDER_STAT_TRANSACTION,
193 BINDER_STAT_TRANSACTION_COMPLETE,
194 BINDER_STAT_COUNT
195};
196
197struct binder_stats {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -0700198 atomic_t br[_IOC_NR(BR_FAILED_REPLY) + 1];
199 atomic_t bc[_IOC_NR(BC_REPLY_SG) + 1];
200 atomic_t obj_created[BINDER_STAT_COUNT];
201 atomic_t obj_deleted[BINDER_STAT_COUNT];
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900202};
203
204static struct binder_stats binder_stats;
205
206static inline void binder_stats_deleted(enum binder_stat_types type)
207{
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -0700208 atomic_inc(&binder_stats.obj_deleted[type]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900209}
210
211static inline void binder_stats_created(enum binder_stat_types type)
212{
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -0700213 atomic_inc(&binder_stats.obj_created[type]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900214}
215
216struct binder_transaction_log_entry {
217 int debug_id;
Todd Kjosd99c7332017-06-29 12:01:53 -0700218 int debug_id_done;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900219 int call_type;
220 int from_proc;
221 int from_thread;
222 int target_handle;
223 int to_proc;
224 int to_thread;
225 int to_node;
226 int data_size;
227 int offsets_size;
Todd Kjos57ada2f2017-06-29 12:01:46 -0700228 int return_error_line;
229 uint32_t return_error;
230 uint32_t return_error_param;
Martijn Coenen14db3182017-02-03 14:40:47 -0800231 const char *context_name;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900232};
233struct binder_transaction_log {
Todd Kjosd99c7332017-06-29 12:01:53 -0700234 atomic_t cur;
235 bool full;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900236 struct binder_transaction_log_entry entry[32];
237};
238static struct binder_transaction_log binder_transaction_log;
239static struct binder_transaction_log binder_transaction_log_failed;
240
241static struct binder_transaction_log_entry *binder_transaction_log_add(
242 struct binder_transaction_log *log)
243{
244 struct binder_transaction_log_entry *e;
Todd Kjosd99c7332017-06-29 12:01:53 -0700245 unsigned int cur = atomic_inc_return(&log->cur);
Seunghun Lee10f62862014-05-01 01:30:23 +0900246
Todd Kjosd99c7332017-06-29 12:01:53 -0700247 if (cur >= ARRAY_SIZE(log->entry))
Gustavo A. R. Silva197410a2018-01-23 12:04:27 -0600248 log->full = true;
Todd Kjosd99c7332017-06-29 12:01:53 -0700249 e = &log->entry[cur % ARRAY_SIZE(log->entry)];
250 WRITE_ONCE(e->debug_id_done, 0);
251 /*
252 * write-barrier to synchronize access to e->debug_id_done.
253 * We make sure the initialized 0 value is seen before
254 * memset() other fields are zeroed by memset.
255 */
256 smp_wmb();
257 memset(e, 0, sizeof(*e));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900258 return e;
259}
260
Martijn Coenen342e5c92017-02-03 14:40:46 -0800261struct binder_context {
262 struct binder_node *binder_context_mgr_node;
Todd Kjosc44b1232017-06-29 12:01:43 -0700263 struct mutex context_mgr_node_lock;
264
Martijn Coenen342e5c92017-02-03 14:40:46 -0800265 kuid_t binder_context_mgr_uid;
Martijn Coenen14db3182017-02-03 14:40:47 -0800266 const char *name;
Martijn Coenen342e5c92017-02-03 14:40:46 -0800267};
268
Martijn Coenenac4812c2017-02-03 14:40:48 -0800269struct binder_device {
270 struct hlist_node hlist;
271 struct miscdevice miscdev;
272 struct binder_context context;
Martijn Coenen342e5c92017-02-03 14:40:46 -0800273};
274
Todd Kjos72196392017-06-29 12:02:02 -0700275/**
276 * struct binder_work - work enqueued on a worklist
277 * @entry: node enqueued on list
278 * @type: type of work to be performed
279 *
280 * There are separate work lists for proc, thread, and node (async).
281 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900282struct binder_work {
283 struct list_head entry;
Todd Kjos72196392017-06-29 12:02:02 -0700284
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900285 enum {
286 BINDER_WORK_TRANSACTION = 1,
287 BINDER_WORK_TRANSACTION_COMPLETE,
Todd Kjos26549d12017-06-29 12:01:55 -0700288 BINDER_WORK_RETURN_ERROR,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900289 BINDER_WORK_NODE,
290 BINDER_WORK_DEAD_BINDER,
291 BINDER_WORK_DEAD_BINDER_AND_CLEAR,
292 BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
293 } type;
294};
295
Todd Kjos26549d12017-06-29 12:01:55 -0700296struct binder_error {
297 struct binder_work work;
298 uint32_t cmd;
299};
300
Todd Kjos9630fe82017-06-29 12:02:00 -0700301/**
302 * struct binder_node - binder node bookkeeping
303 * @debug_id: unique ID for debugging
304 * (invariant after initialized)
305 * @lock: lock for node fields
306 * @work: worklist element for node work
Todd Kjos72196392017-06-29 12:02:02 -0700307 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700308 * @rb_node: element for proc->nodes tree
Todd Kjosda0fa9e2017-06-29 12:02:04 -0700309 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700310 * @dead_node: element for binder_dead_nodes list
311 * (protected by binder_dead_nodes_lock)
312 * @proc: binder_proc that owns this node
313 * (invariant after initialized)
314 * @refs: list of references on this node
Todd Kjos673068e2017-06-29 12:02:03 -0700315 * (protected by @lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700316 * @internal_strong_refs: used to take strong references when
317 * initiating a transaction
Todd Kjosed297212017-06-29 12:02:01 -0700318 * (protected by @proc->inner_lock if @proc
319 * and by @lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700320 * @local_weak_refs: weak user refs from local process
Todd Kjosed297212017-06-29 12:02:01 -0700321 * (protected by @proc->inner_lock if @proc
322 * and by @lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700323 * @local_strong_refs: strong user refs from local process
Todd Kjosed297212017-06-29 12:02:01 -0700324 * (protected by @proc->inner_lock if @proc
325 * and by @lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700326 * @tmp_refs: temporary kernel refs
Todd Kjosed297212017-06-29 12:02:01 -0700327 * (protected by @proc->inner_lock while @proc
328 * is valid, and by binder_dead_nodes_lock
329 * if @proc is NULL. During inc/dec and node release
330 * it is also protected by @lock to provide safety
331 * as the node dies and @proc becomes NULL)
Todd Kjos9630fe82017-06-29 12:02:00 -0700332 * @ptr: userspace pointer for node
333 * (invariant, no lock needed)
334 * @cookie: userspace cookie for node
335 * (invariant, no lock needed)
336 * @has_strong_ref: userspace notified of strong ref
Todd Kjosed297212017-06-29 12:02:01 -0700337 * (protected by @proc->inner_lock if @proc
338 * and by @lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700339 * @pending_strong_ref: userspace has acked notification of strong ref
Todd Kjosed297212017-06-29 12:02:01 -0700340 * (protected by @proc->inner_lock if @proc
341 * and by @lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700342 * @has_weak_ref: userspace notified of weak ref
Todd Kjosed297212017-06-29 12:02:01 -0700343 * (protected by @proc->inner_lock if @proc
344 * and by @lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700345 * @pending_weak_ref: userspace has acked notification of weak ref
Todd Kjosed297212017-06-29 12:02:01 -0700346 * (protected by @proc->inner_lock if @proc
347 * and by @lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700348 * @has_async_transaction: async transaction to node in progress
Todd Kjos673068e2017-06-29 12:02:03 -0700349 * (protected by @lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700350 * @accept_fds: file descriptor operations supported for node
351 * (invariant after initialized)
352 * @min_priority: minimum scheduling priority
353 * (invariant after initialized)
354 * @async_todo: list of async work items
Todd Kjos72196392017-06-29 12:02:02 -0700355 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700356 *
357 * Bookkeeping structure for binder nodes.
358 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900359struct binder_node {
360 int debug_id;
Todd Kjos9630fe82017-06-29 12:02:00 -0700361 spinlock_t lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900362 struct binder_work work;
363 union {
364 struct rb_node rb_node;
365 struct hlist_node dead_node;
366 };
367 struct binder_proc *proc;
368 struct hlist_head refs;
369 int internal_strong_refs;
370 int local_weak_refs;
371 int local_strong_refs;
Todd Kjosadc18842017-06-29 12:01:59 -0700372 int tmp_refs;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800373 binder_uintptr_t ptr;
374 binder_uintptr_t cookie;
Todd Kjosed297212017-06-29 12:02:01 -0700375 struct {
376 /*
377 * bitfield elements protected by
378 * proc inner_lock
379 */
380 u8 has_strong_ref:1;
381 u8 pending_strong_ref:1;
382 u8 has_weak_ref:1;
383 u8 pending_weak_ref:1;
384 };
385 struct {
386 /*
387 * invariant after initialization
388 */
389 u8 accept_fds:1;
390 u8 min_priority;
391 };
392 bool has_async_transaction;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900393 struct list_head async_todo;
394};
395
396struct binder_ref_death {
Todd Kjos72196392017-06-29 12:02:02 -0700397 /**
398 * @work: worklist element for death notifications
399 * (protected by inner_lock of the proc that
400 * this ref belongs to)
401 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900402 struct binder_work work;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800403 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900404};
405
Todd Kjos372e3142017-06-29 12:01:58 -0700406/**
407 * struct binder_ref_data - binder_ref counts and id
408 * @debug_id: unique ID for the ref
409 * @desc: unique userspace handle for ref
410 * @strong: strong ref count (debugging only if not locked)
411 * @weak: weak ref count (debugging only if not locked)
412 *
413 * Structure to hold ref count and ref id information. Since
414 * the actual ref can only be accessed with a lock, this structure
415 * is used to return information about the ref to callers of
416 * ref inc/dec functions.
417 */
418struct binder_ref_data {
419 int debug_id;
420 uint32_t desc;
421 int strong;
422 int weak;
423};
424
425/**
426 * struct binder_ref - struct to track references on nodes
427 * @data: binder_ref_data containing id, handle, and current refcounts
428 * @rb_node_desc: node for lookup by @data.desc in proc's rb_tree
429 * @rb_node_node: node for lookup by @node in proc's rb_tree
430 * @node_entry: list entry for node->refs list in target node
Todd Kjos673068e2017-06-29 12:02:03 -0700431 * (protected by @node->lock)
Todd Kjos372e3142017-06-29 12:01:58 -0700432 * @proc: binder_proc containing ref
433 * @node: binder_node of target node. When cleaning up a
434 * ref for deletion in binder_cleanup_ref, a non-NULL
435 * @node indicates the node must be freed
436 * @death: pointer to death notification (ref_death) if requested
Martijn Coenenab51ec62017-06-29 12:02:10 -0700437 * (protected by @node->lock)
Todd Kjos372e3142017-06-29 12:01:58 -0700438 *
439 * Structure to track references from procA to target node (on procB). This
440 * structure is unsafe to access without holding @proc->outer_lock.
441 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900442struct binder_ref {
443 /* Lookups needed: */
444 /* node + proc => ref (transaction) */
445 /* desc + proc => ref (transaction, inc/dec ref) */
446 /* node => refs + procs (proc exit) */
Todd Kjos372e3142017-06-29 12:01:58 -0700447 struct binder_ref_data data;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900448 struct rb_node rb_node_desc;
449 struct rb_node rb_node_node;
450 struct hlist_node node_entry;
451 struct binder_proc *proc;
452 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900453 struct binder_ref_death *death;
454};
455
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900456enum binder_deferred_state {
457 BINDER_DEFERRED_PUT_FILES = 0x01,
458 BINDER_DEFERRED_FLUSH = 0x02,
459 BINDER_DEFERRED_RELEASE = 0x04,
460};
461
Todd Kjos9630fe82017-06-29 12:02:00 -0700462/**
463 * struct binder_proc - binder process bookkeeping
464 * @proc_node: element for binder_procs list
465 * @threads: rbtree of binder_threads in this proc
Todd Kjos7bd7b0e2017-06-29 12:02:05 -0700466 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700467 * @nodes: rbtree of binder nodes associated with
468 * this proc ordered by node->ptr
Todd Kjosda0fa9e2017-06-29 12:02:04 -0700469 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700470 * @refs_by_desc: rbtree of refs ordered by ref->desc
Todd Kjos2c1838d2017-06-29 12:02:08 -0700471 * (protected by @outer_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700472 * @refs_by_node: rbtree of refs ordered by ref->node
Todd Kjos2c1838d2017-06-29 12:02:08 -0700473 * (protected by @outer_lock)
Martijn Coenen1b77e9d2017-08-31 10:04:18 +0200474 * @waiting_threads: threads currently waiting for proc work
475 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700476 * @pid PID of group_leader of process
477 * (invariant after initialized)
478 * @tsk task_struct for group_leader of process
479 * (invariant after initialized)
480 * @files files_struct for process
Todd Kjos7f3dc002017-11-27 09:32:33 -0800481 * (protected by @files_lock)
482 * @files_lock mutex to protect @files
Todd Kjos9630fe82017-06-29 12:02:00 -0700483 * @deferred_work_node: element for binder_deferred_list
484 * (protected by binder_deferred_lock)
485 * @deferred_work: bitmap of deferred work to perform
486 * (protected by binder_deferred_lock)
487 * @is_dead: process is dead and awaiting free
488 * when outstanding transactions are cleaned up
Todd Kjos7bd7b0e2017-06-29 12:02:05 -0700489 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700490 * @todo: list of work for this process
Todd Kjos72196392017-06-29 12:02:02 -0700491 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700492 * @stats: per-process binder statistics
493 * (atomics, no lock needed)
494 * @delivered_death: list of delivered death notification
Todd Kjos72196392017-06-29 12:02:02 -0700495 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700496 * @max_threads: cap on number of binder threads
Todd Kjosb3e68612017-06-29 12:02:07 -0700497 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700498 * @requested_threads: number of binder threads requested but not
499 * yet started. In current implementation, can
500 * only be 0 or 1.
Todd Kjosb3e68612017-06-29 12:02:07 -0700501 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700502 * @requested_threads_started: number binder threads started
Todd Kjosb3e68612017-06-29 12:02:07 -0700503 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700504 * @tmp_ref: temporary reference to indicate proc is in use
Todd Kjos7bd7b0e2017-06-29 12:02:05 -0700505 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700506 * @default_priority: default scheduler priority
507 * (invariant after initialized)
508 * @debugfs_entry: debugfs node
509 * @alloc: binder allocator bookkeeping
510 * @context: binder_context for this proc
511 * (invariant after initialized)
512 * @inner_lock: can nest under outer_lock and/or node lock
513 * @outer_lock: no nesting under innor or node lock
514 * Lock order: 1) outer, 2) node, 3) inner
515 *
516 * Bookkeeping structure for binder processes
517 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900518struct binder_proc {
519 struct hlist_node proc_node;
520 struct rb_root threads;
521 struct rb_root nodes;
522 struct rb_root refs_by_desc;
523 struct rb_root refs_by_node;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +0200524 struct list_head waiting_threads;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900525 int pid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900526 struct task_struct *tsk;
527 struct files_struct *files;
Todd Kjos7f3dc002017-11-27 09:32:33 -0800528 struct mutex files_lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900529 struct hlist_node deferred_work_node;
530 int deferred_work;
Todd Kjos7a4408c2017-06-29 12:01:57 -0700531 bool is_dead;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900532
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900533 struct list_head todo;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900534 struct binder_stats stats;
535 struct list_head delivered_death;
536 int max_threads;
537 int requested_threads;
538 int requested_threads_started;
Todd Kjos7a4408c2017-06-29 12:01:57 -0700539 int tmp_ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900540 long default_priority;
Arve Hjønnevåg16b66552009-04-28 20:57:50 -0700541 struct dentry *debugfs_entry;
Todd Kjosfdfb4a92017-06-29 12:01:38 -0700542 struct binder_alloc alloc;
Martijn Coenen342e5c92017-02-03 14:40:46 -0800543 struct binder_context *context;
Todd Kjos9630fe82017-06-29 12:02:00 -0700544 spinlock_t inner_lock;
545 spinlock_t outer_lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900546};
547
548enum {
549 BINDER_LOOPER_STATE_REGISTERED = 0x01,
550 BINDER_LOOPER_STATE_ENTERED = 0x02,
551 BINDER_LOOPER_STATE_EXITED = 0x04,
552 BINDER_LOOPER_STATE_INVALID = 0x08,
553 BINDER_LOOPER_STATE_WAITING = 0x10,
Martijn Coenen1b77e9d2017-08-31 10:04:18 +0200554 BINDER_LOOPER_STATE_POLL = 0x20,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900555};
556
Todd Kjos9630fe82017-06-29 12:02:00 -0700557/**
558 * struct binder_thread - binder thread bookkeeping
559 * @proc: binder process for this thread
560 * (invariant after initialization)
561 * @rb_node: element for proc->threads rbtree
Todd Kjos7bd7b0e2017-06-29 12:02:05 -0700562 * (protected by @proc->inner_lock)
Martijn Coenen1b77e9d2017-08-31 10:04:18 +0200563 * @waiting_thread_node: element for @proc->waiting_threads list
564 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700565 * @pid: PID for this thread
566 * (invariant after initialization)
567 * @looper: bitmap of looping state
568 * (only accessed by this thread)
569 * @looper_needs_return: looping thread needs to exit driver
570 * (no lock needed)
571 * @transaction_stack: stack of in-progress transactions for this thread
Martijn Coenen0b89d692017-06-29 12:02:06 -0700572 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700573 * @todo: list of work to do for this thread
Todd Kjos72196392017-06-29 12:02:02 -0700574 * (protected by @proc->inner_lock)
Martijn Coenen148ade22017-11-15 09:21:35 +0100575 * @process_todo: whether work in @todo should be processed
576 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700577 * @return_error: transaction errors reported by this thread
578 * (only accessed by this thread)
579 * @reply_error: transaction errors reported by target thread
Martijn Coenen0b89d692017-06-29 12:02:06 -0700580 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700581 * @wait: wait queue for thread work
582 * @stats: per-thread statistics
583 * (atomics, no lock needed)
584 * @tmp_ref: temporary reference to indicate thread is in use
585 * (atomic since @proc->inner_lock cannot
586 * always be acquired)
587 * @is_dead: thread is dead and awaiting free
588 * when outstanding transactions are cleaned up
Todd Kjos7bd7b0e2017-06-29 12:02:05 -0700589 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700590 *
591 * Bookkeeping structure for binder threads.
592 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900593struct binder_thread {
594 struct binder_proc *proc;
595 struct rb_node rb_node;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +0200596 struct list_head waiting_thread_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900597 int pid;
Todd Kjos08dabce2017-06-29 12:01:49 -0700598 int looper; /* only modified by this thread */
599 bool looper_need_return; /* can be written by other thread */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900600 struct binder_transaction *transaction_stack;
601 struct list_head todo;
Martijn Coenen148ade22017-11-15 09:21:35 +0100602 bool process_todo;
Todd Kjos26549d12017-06-29 12:01:55 -0700603 struct binder_error return_error;
604 struct binder_error reply_error;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900605 wait_queue_head_t wait;
606 struct binder_stats stats;
Todd Kjos7a4408c2017-06-29 12:01:57 -0700607 atomic_t tmp_ref;
608 bool is_dead;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900609};
610
611struct binder_transaction {
612 int debug_id;
613 struct binder_work work;
614 struct binder_thread *from;
615 struct binder_transaction *from_parent;
616 struct binder_proc *to_proc;
617 struct binder_thread *to_thread;
618 struct binder_transaction *to_parent;
619 unsigned need_reply:1;
620 /* unsigned is_dead:1; */ /* not used at the moment */
621
622 struct binder_buffer *buffer;
623 unsigned int code;
624 unsigned int flags;
625 long priority;
626 long saved_priority;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -0600627 kuid_t sender_euid;
Todd Kjos7a4408c2017-06-29 12:01:57 -0700628 /**
629 * @lock: protects @from, @to_proc, and @to_thread
630 *
631 * @from, @to_proc, and @to_thread can be set to NULL
632 * during thread teardown
633 */
634 spinlock_t lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900635};
636
Todd Kjos9630fe82017-06-29 12:02:00 -0700637/**
638 * binder_proc_lock() - Acquire outer lock for given binder_proc
639 * @proc: struct binder_proc to acquire
640 *
641 * Acquires proc->outer_lock. Used to protect binder_ref
642 * structures associated with the given proc.
643 */
644#define binder_proc_lock(proc) _binder_proc_lock(proc, __LINE__)
645static void
646_binder_proc_lock(struct binder_proc *proc, int line)
647{
648 binder_debug(BINDER_DEBUG_SPINLOCKS,
649 "%s: line=%d\n", __func__, line);
650 spin_lock(&proc->outer_lock);
651}
652
653/**
654 * binder_proc_unlock() - Release spinlock for given binder_proc
655 * @proc: struct binder_proc to acquire
656 *
657 * Release lock acquired via binder_proc_lock()
658 */
659#define binder_proc_unlock(_proc) _binder_proc_unlock(_proc, __LINE__)
660static void
661_binder_proc_unlock(struct binder_proc *proc, int line)
662{
663 binder_debug(BINDER_DEBUG_SPINLOCKS,
664 "%s: line=%d\n", __func__, line);
665 spin_unlock(&proc->outer_lock);
666}
667
668/**
669 * binder_inner_proc_lock() - Acquire inner lock for given binder_proc
670 * @proc: struct binder_proc to acquire
671 *
672 * Acquires proc->inner_lock. Used to protect todo lists
673 */
674#define binder_inner_proc_lock(proc) _binder_inner_proc_lock(proc, __LINE__)
675static void
676_binder_inner_proc_lock(struct binder_proc *proc, int line)
677{
678 binder_debug(BINDER_DEBUG_SPINLOCKS,
679 "%s: line=%d\n", __func__, line);
680 spin_lock(&proc->inner_lock);
681}
682
683/**
684 * binder_inner_proc_unlock() - Release inner lock for given binder_proc
685 * @proc: struct binder_proc to acquire
686 *
687 * Release lock acquired via binder_inner_proc_lock()
688 */
689#define binder_inner_proc_unlock(proc) _binder_inner_proc_unlock(proc, __LINE__)
690static void
691_binder_inner_proc_unlock(struct binder_proc *proc, int line)
692{
693 binder_debug(BINDER_DEBUG_SPINLOCKS,
694 "%s: line=%d\n", __func__, line);
695 spin_unlock(&proc->inner_lock);
696}
697
698/**
699 * binder_node_lock() - Acquire spinlock for given binder_node
700 * @node: struct binder_node to acquire
701 *
702 * Acquires node->lock. Used to protect binder_node fields
703 */
704#define binder_node_lock(node) _binder_node_lock(node, __LINE__)
705static void
706_binder_node_lock(struct binder_node *node, int line)
707{
708 binder_debug(BINDER_DEBUG_SPINLOCKS,
709 "%s: line=%d\n", __func__, line);
710 spin_lock(&node->lock);
711}
712
713/**
714 * binder_node_unlock() - Release spinlock for given binder_proc
715 * @node: struct binder_node to acquire
716 *
717 * Release lock acquired via binder_node_lock()
718 */
719#define binder_node_unlock(node) _binder_node_unlock(node, __LINE__)
720static void
721_binder_node_unlock(struct binder_node *node, int line)
722{
723 binder_debug(BINDER_DEBUG_SPINLOCKS,
724 "%s: line=%d\n", __func__, line);
725 spin_unlock(&node->lock);
726}
727
Todd Kjos673068e2017-06-29 12:02:03 -0700728/**
729 * binder_node_inner_lock() - Acquire node and inner locks
730 * @node: struct binder_node to acquire
731 *
732 * Acquires node->lock. If node->proc also acquires
733 * proc->inner_lock. Used to protect binder_node fields
734 */
735#define binder_node_inner_lock(node) _binder_node_inner_lock(node, __LINE__)
736static void
737_binder_node_inner_lock(struct binder_node *node, int line)
738{
739 binder_debug(BINDER_DEBUG_SPINLOCKS,
740 "%s: line=%d\n", __func__, line);
741 spin_lock(&node->lock);
742 if (node->proc)
743 binder_inner_proc_lock(node->proc);
744}
745
746/**
747 * binder_node_unlock() - Release node and inner locks
748 * @node: struct binder_node to acquire
749 *
750 * Release lock acquired via binder_node_lock()
751 */
752#define binder_node_inner_unlock(node) _binder_node_inner_unlock(node, __LINE__)
753static void
754_binder_node_inner_unlock(struct binder_node *node, int line)
755{
756 struct binder_proc *proc = node->proc;
757
758 binder_debug(BINDER_DEBUG_SPINLOCKS,
759 "%s: line=%d\n", __func__, line);
760 if (proc)
761 binder_inner_proc_unlock(proc);
762 spin_unlock(&node->lock);
763}
764
Todd Kjos72196392017-06-29 12:02:02 -0700765static bool binder_worklist_empty_ilocked(struct list_head *list)
766{
767 return list_empty(list);
768}
769
770/**
771 * binder_worklist_empty() - Check if no items on the work list
772 * @proc: binder_proc associated with list
773 * @list: list to check
774 *
775 * Return: true if there are no items on list, else false
776 */
777static bool binder_worklist_empty(struct binder_proc *proc,
778 struct list_head *list)
779{
780 bool ret;
781
782 binder_inner_proc_lock(proc);
783 ret = binder_worklist_empty_ilocked(list);
784 binder_inner_proc_unlock(proc);
785 return ret;
786}
787
Martijn Coenen148ade22017-11-15 09:21:35 +0100788/**
789 * binder_enqueue_work_ilocked() - Add an item to the work list
790 * @work: struct binder_work to add to list
791 * @target_list: list to add work to
792 *
793 * Adds the work to the specified list. Asserts that work
794 * is not already on a list.
795 *
796 * Requires the proc->inner_lock to be held.
797 */
Todd Kjos72196392017-06-29 12:02:02 -0700798static void
799binder_enqueue_work_ilocked(struct binder_work *work,
800 struct list_head *target_list)
801{
802 BUG_ON(target_list == NULL);
803 BUG_ON(work->entry.next && !list_empty(&work->entry));
804 list_add_tail(&work->entry, target_list);
805}
806
807/**
Martijn Coenen148ade22017-11-15 09:21:35 +0100808 * binder_enqueue_deferred_thread_work_ilocked() - Add deferred thread work
809 * @thread: thread to queue work to
Todd Kjos72196392017-06-29 12:02:02 -0700810 * @work: struct binder_work to add to list
Todd Kjos72196392017-06-29 12:02:02 -0700811 *
Martijn Coenen148ade22017-11-15 09:21:35 +0100812 * Adds the work to the todo list of the thread. Doesn't set the process_todo
813 * flag, which means that (if it wasn't already set) the thread will go to
814 * sleep without handling this work when it calls read.
815 *
816 * Requires the proc->inner_lock to be held.
Todd Kjos72196392017-06-29 12:02:02 -0700817 */
818static void
Martijn Coenen148ade22017-11-15 09:21:35 +0100819binder_enqueue_deferred_thread_work_ilocked(struct binder_thread *thread,
820 struct binder_work *work)
Todd Kjos72196392017-06-29 12:02:02 -0700821{
Martijn Coenen148ade22017-11-15 09:21:35 +0100822 binder_enqueue_work_ilocked(work, &thread->todo);
823}
824
825/**
826 * binder_enqueue_thread_work_ilocked() - Add an item to the thread work list
827 * @thread: thread to queue work to
828 * @work: struct binder_work to add to list
829 *
830 * Adds the work to the todo list of the thread, and enables processing
831 * of the todo queue.
832 *
833 * Requires the proc->inner_lock to be held.
834 */
835static void
836binder_enqueue_thread_work_ilocked(struct binder_thread *thread,
837 struct binder_work *work)
838{
839 binder_enqueue_work_ilocked(work, &thread->todo);
840 thread->process_todo = true;
841}
842
843/**
844 * binder_enqueue_thread_work() - Add an item to the thread work list
845 * @thread: thread to queue work to
846 * @work: struct binder_work to add to list
847 *
848 * Adds the work to the todo list of the thread, and enables processing
849 * of the todo queue.
850 */
851static void
852binder_enqueue_thread_work(struct binder_thread *thread,
853 struct binder_work *work)
854{
855 binder_inner_proc_lock(thread->proc);
856 binder_enqueue_thread_work_ilocked(thread, work);
857 binder_inner_proc_unlock(thread->proc);
Todd Kjos72196392017-06-29 12:02:02 -0700858}
859
860static void
861binder_dequeue_work_ilocked(struct binder_work *work)
862{
863 list_del_init(&work->entry);
864}
865
866/**
867 * binder_dequeue_work() - Removes an item from the work list
868 * @proc: binder_proc associated with list
869 * @work: struct binder_work to remove from list
870 *
871 * Removes the specified work item from whatever list it is on.
872 * Can safely be called if work is not on any list.
873 */
874static void
875binder_dequeue_work(struct binder_proc *proc, struct binder_work *work)
876{
877 binder_inner_proc_lock(proc);
878 binder_dequeue_work_ilocked(work);
879 binder_inner_proc_unlock(proc);
880}
881
882static struct binder_work *binder_dequeue_work_head_ilocked(
883 struct list_head *list)
884{
885 struct binder_work *w;
886
887 w = list_first_entry_or_null(list, struct binder_work, entry);
888 if (w)
889 list_del_init(&w->entry);
890 return w;
891}
892
893/**
894 * binder_dequeue_work_head() - Dequeues the item at head of list
895 * @proc: binder_proc associated with list
896 * @list: list to dequeue head
897 *
898 * Removes the head of the list if there are items on the list
899 *
900 * Return: pointer dequeued binder_work, NULL if list was empty
901 */
902static struct binder_work *binder_dequeue_work_head(
903 struct binder_proc *proc,
904 struct list_head *list)
905{
906 struct binder_work *w;
907
908 binder_inner_proc_lock(proc);
909 w = binder_dequeue_work_head_ilocked(list);
910 binder_inner_proc_unlock(proc);
911 return w;
912}
913
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900914static void
915binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
Todd Kjos7a4408c2017-06-29 12:01:57 -0700916static void binder_free_thread(struct binder_thread *thread);
917static void binder_free_proc(struct binder_proc *proc);
Todd Kjosda0fa9e2017-06-29 12:02:04 -0700918static void binder_inc_node_tmpref_ilocked(struct binder_node *node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900919
Sachin Kamatefde99c2012-08-17 16:39:36 +0530920static int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900921{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900922 unsigned long rlim_cur;
923 unsigned long irqs;
Todd Kjos7f3dc002017-11-27 09:32:33 -0800924 int ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900925
Todd Kjos7f3dc002017-11-27 09:32:33 -0800926 mutex_lock(&proc->files_lock);
927 if (proc->files == NULL) {
928 ret = -ESRCH;
929 goto err;
930 }
931 if (!lock_task_sighand(proc->tsk, &irqs)) {
932 ret = -EMFILE;
933 goto err;
934 }
Al Virodcfadfa2012-08-12 17:27:30 -0400935 rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
936 unlock_task_sighand(proc->tsk, &irqs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900937
Todd Kjos7f3dc002017-11-27 09:32:33 -0800938 ret = __alloc_fd(proc->files, 0, rlim_cur, flags);
939err:
940 mutex_unlock(&proc->files_lock);
941 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900942}
943
944/*
945 * copied from fd_install
946 */
947static void task_fd_install(
948 struct binder_proc *proc, unsigned int fd, struct file *file)
949{
Todd Kjos7f3dc002017-11-27 09:32:33 -0800950 mutex_lock(&proc->files_lock);
Al Virof869e8a2012-08-15 21:06:33 -0400951 if (proc->files)
952 __fd_install(proc->files, fd, file);
Todd Kjos7f3dc002017-11-27 09:32:33 -0800953 mutex_unlock(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900954}
955
956/*
957 * copied from sys_close
958 */
959static long task_close_fd(struct binder_proc *proc, unsigned int fd)
960{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900961 int retval;
962
Todd Kjos7f3dc002017-11-27 09:32:33 -0800963 mutex_lock(&proc->files_lock);
964 if (proc->files == NULL) {
965 retval = -ESRCH;
966 goto err;
967 }
Al Viro483ce1d2012-08-19 12:04:24 -0400968 retval = __close_fd(proc->files, fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900969 /* can't restart close syscall because file table entry was cleared */
970 if (unlikely(retval == -ERESTARTSYS ||
971 retval == -ERESTARTNOINTR ||
972 retval == -ERESTARTNOHAND ||
973 retval == -ERESTART_RESTARTBLOCK))
974 retval = -EINTR;
Todd Kjos7f3dc002017-11-27 09:32:33 -0800975err:
976 mutex_unlock(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900977 return retval;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900978}
979
Martijn Coenen1b77e9d2017-08-31 10:04:18 +0200980static bool binder_has_work_ilocked(struct binder_thread *thread,
981 bool do_proc_work)
982{
Martijn Coenen148ade22017-11-15 09:21:35 +0100983 return thread->process_todo ||
Martijn Coenen1b77e9d2017-08-31 10:04:18 +0200984 thread->looper_need_return ||
985 (do_proc_work &&
986 !binder_worklist_empty_ilocked(&thread->proc->todo));
987}
988
989static bool binder_has_work(struct binder_thread *thread, bool do_proc_work)
990{
991 bool has_work;
992
993 binder_inner_proc_lock(thread->proc);
994 has_work = binder_has_work_ilocked(thread, do_proc_work);
995 binder_inner_proc_unlock(thread->proc);
996
997 return has_work;
998}
999
1000static bool binder_available_for_proc_work_ilocked(struct binder_thread *thread)
1001{
1002 return !thread->transaction_stack &&
1003 binder_worklist_empty_ilocked(&thread->todo) &&
1004 (thread->looper & (BINDER_LOOPER_STATE_ENTERED |
1005 BINDER_LOOPER_STATE_REGISTERED));
1006}
1007
1008static void binder_wakeup_poll_threads_ilocked(struct binder_proc *proc,
1009 bool sync)
1010{
1011 struct rb_node *n;
1012 struct binder_thread *thread;
1013
1014 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
1015 thread = rb_entry(n, struct binder_thread, rb_node);
1016 if (thread->looper & BINDER_LOOPER_STATE_POLL &&
1017 binder_available_for_proc_work_ilocked(thread)) {
1018 if (sync)
1019 wake_up_interruptible_sync(&thread->wait);
1020 else
1021 wake_up_interruptible(&thread->wait);
1022 }
1023 }
1024}
1025
Martijn Coenen408c68b2017-08-31 10:04:19 +02001026/**
1027 * binder_select_thread_ilocked() - selects a thread for doing proc work.
1028 * @proc: process to select a thread from
1029 *
1030 * Note that calling this function moves the thread off the waiting_threads
1031 * list, so it can only be woken up by the caller of this function, or a
1032 * signal. Therefore, callers *should* always wake up the thread this function
1033 * returns.
1034 *
1035 * Return: If there's a thread currently waiting for process work,
1036 * returns that thread. Otherwise returns NULL.
1037 */
1038static struct binder_thread *
1039binder_select_thread_ilocked(struct binder_proc *proc)
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02001040{
1041 struct binder_thread *thread;
1042
Martijn Coenen858b2712017-08-31 10:04:26 +02001043 assert_spin_locked(&proc->inner_lock);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02001044 thread = list_first_entry_or_null(&proc->waiting_threads,
1045 struct binder_thread,
1046 waiting_thread_node);
1047
Martijn Coenen408c68b2017-08-31 10:04:19 +02001048 if (thread)
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02001049 list_del_init(&thread->waiting_thread_node);
Martijn Coenen408c68b2017-08-31 10:04:19 +02001050
1051 return thread;
1052}
1053
1054/**
1055 * binder_wakeup_thread_ilocked() - wakes up a thread for doing proc work.
1056 * @proc: process to wake up a thread in
1057 * @thread: specific thread to wake-up (may be NULL)
1058 * @sync: whether to do a synchronous wake-up
1059 *
1060 * This function wakes up a thread in the @proc process.
1061 * The caller may provide a specific thread to wake-up in
1062 * the @thread parameter. If @thread is NULL, this function
1063 * will wake up threads that have called poll().
1064 *
1065 * Note that for this function to work as expected, callers
1066 * should first call binder_select_thread() to find a thread
1067 * to handle the work (if they don't have a thread already),
1068 * and pass the result into the @thread parameter.
1069 */
1070static void binder_wakeup_thread_ilocked(struct binder_proc *proc,
1071 struct binder_thread *thread,
1072 bool sync)
1073{
Martijn Coenen858b2712017-08-31 10:04:26 +02001074 assert_spin_locked(&proc->inner_lock);
Martijn Coenen408c68b2017-08-31 10:04:19 +02001075
1076 if (thread) {
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02001077 if (sync)
1078 wake_up_interruptible_sync(&thread->wait);
1079 else
1080 wake_up_interruptible(&thread->wait);
1081 return;
1082 }
1083
1084 /* Didn't find a thread waiting for proc work; this can happen
1085 * in two scenarios:
1086 * 1. All threads are busy handling transactions
1087 * In that case, one of those threads should call back into
1088 * the kernel driver soon and pick up this work.
1089 * 2. Threads are using the (e)poll interface, in which case
1090 * they may be blocked on the waitqueue without having been
1091 * added to waiting_threads. For this case, we just iterate
1092 * over all threads not handling transaction work, and
1093 * wake them all up. We wake all because we don't know whether
1094 * a thread that called into (e)poll is handling non-binder
1095 * work currently.
1096 */
1097 binder_wakeup_poll_threads_ilocked(proc, sync);
1098}
1099
Martijn Coenen408c68b2017-08-31 10:04:19 +02001100static void binder_wakeup_proc_ilocked(struct binder_proc *proc)
1101{
1102 struct binder_thread *thread = binder_select_thread_ilocked(proc);
1103
1104 binder_wakeup_thread_ilocked(proc, thread, /* sync = */false);
1105}
1106
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001107static void binder_set_nice(long nice)
1108{
1109 long min_nice;
Seunghun Lee10f62862014-05-01 01:30:23 +09001110
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001111 if (can_nice(current, nice)) {
1112 set_user_nice(current, nice);
1113 return;
1114 }
Krzysztof Opasiakc3643b62017-07-05 19:24:44 +02001115 min_nice = rlimit_to_nice(rlimit(RLIMIT_NICE));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001116 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301117 "%d: nice value %ld not allowed use %ld instead\n",
1118 current->pid, nice, min_nice);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001119 set_user_nice(current, min_nice);
Dongsheng Yang8698a742014-03-11 18:09:12 +08001120 if (min_nice <= MAX_NICE)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001121 return;
Anmol Sarma56b468f2012-10-30 22:35:43 +05301122 binder_user_error("%d RLIMIT_NICE not set\n", current->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001123}
1124
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001125static struct binder_node *binder_get_node_ilocked(struct binder_proc *proc,
1126 binder_uintptr_t ptr)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001127{
1128 struct rb_node *n = proc->nodes.rb_node;
1129 struct binder_node *node;
1130
Martijn Coenen858b2712017-08-31 10:04:26 +02001131 assert_spin_locked(&proc->inner_lock);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001132
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001133 while (n) {
1134 node = rb_entry(n, struct binder_node, rb_node);
1135
1136 if (ptr < node->ptr)
1137 n = n->rb_left;
1138 else if (ptr > node->ptr)
1139 n = n->rb_right;
Todd Kjosadc18842017-06-29 12:01:59 -07001140 else {
1141 /*
1142 * take an implicit weak reference
1143 * to ensure node stays alive until
1144 * call to binder_put_node()
1145 */
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001146 binder_inc_node_tmpref_ilocked(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001147 return node;
Todd Kjosadc18842017-06-29 12:01:59 -07001148 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001149 }
1150 return NULL;
1151}
1152
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001153static struct binder_node *binder_get_node(struct binder_proc *proc,
1154 binder_uintptr_t ptr)
1155{
1156 struct binder_node *node;
1157
1158 binder_inner_proc_lock(proc);
1159 node = binder_get_node_ilocked(proc, ptr);
1160 binder_inner_proc_unlock(proc);
1161 return node;
1162}
1163
1164static struct binder_node *binder_init_node_ilocked(
1165 struct binder_proc *proc,
1166 struct binder_node *new_node,
1167 struct flat_binder_object *fp)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001168{
1169 struct rb_node **p = &proc->nodes.rb_node;
1170 struct rb_node *parent = NULL;
1171 struct binder_node *node;
Todd Kjos673068e2017-06-29 12:02:03 -07001172 binder_uintptr_t ptr = fp ? fp->binder : 0;
1173 binder_uintptr_t cookie = fp ? fp->cookie : 0;
1174 __u32 flags = fp ? fp->flags : 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001175
Martijn Coenen858b2712017-08-31 10:04:26 +02001176 assert_spin_locked(&proc->inner_lock);
1177
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001178 while (*p) {
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001179
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001180 parent = *p;
1181 node = rb_entry(parent, struct binder_node, rb_node);
1182
1183 if (ptr < node->ptr)
1184 p = &(*p)->rb_left;
1185 else if (ptr > node->ptr)
1186 p = &(*p)->rb_right;
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001187 else {
1188 /*
1189 * A matching node is already in
1190 * the rb tree. Abandon the init
1191 * and return it.
1192 */
1193 binder_inc_node_tmpref_ilocked(node);
1194 return node;
1195 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001196 }
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001197 node = new_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001198 binder_stats_created(BINDER_STAT_NODE);
Todd Kjosadc18842017-06-29 12:01:59 -07001199 node->tmp_refs++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001200 rb_link_node(&node->rb_node, parent, p);
1201 rb_insert_color(&node->rb_node, &proc->nodes);
Todd Kjos656a8002017-06-29 12:01:45 -07001202 node->debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001203 node->proc = proc;
1204 node->ptr = ptr;
1205 node->cookie = cookie;
1206 node->work.type = BINDER_WORK_NODE;
Todd Kjos673068e2017-06-29 12:02:03 -07001207 node->min_priority = flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
1208 node->accept_fds = !!(flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
Todd Kjos9630fe82017-06-29 12:02:00 -07001209 spin_lock_init(&node->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001210 INIT_LIST_HEAD(&node->work.entry);
1211 INIT_LIST_HEAD(&node->async_todo);
1212 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001213 "%d:%d node %d u%016llx c%016llx created\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001214 proc->pid, current->pid, node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001215 (u64)node->ptr, (u64)node->cookie);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001216
1217 return node;
1218}
1219
1220static struct binder_node *binder_new_node(struct binder_proc *proc,
1221 struct flat_binder_object *fp)
1222{
1223 struct binder_node *node;
1224 struct binder_node *new_node = kzalloc(sizeof(*node), GFP_KERNEL);
1225
1226 if (!new_node)
1227 return NULL;
1228 binder_inner_proc_lock(proc);
1229 node = binder_init_node_ilocked(proc, new_node, fp);
1230 binder_inner_proc_unlock(proc);
1231 if (node != new_node)
1232 /*
1233 * The node was already added by another thread
1234 */
1235 kfree(new_node);
1236
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001237 return node;
1238}
1239
Todd Kjosed297212017-06-29 12:02:01 -07001240static void binder_free_node(struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001241{
Todd Kjosed297212017-06-29 12:02:01 -07001242 kfree(node);
1243 binder_stats_deleted(BINDER_STAT_NODE);
1244}
1245
Todd Kjos673068e2017-06-29 12:02:03 -07001246static int binder_inc_node_nilocked(struct binder_node *node, int strong,
1247 int internal,
1248 struct list_head *target_list)
Todd Kjosed297212017-06-29 12:02:01 -07001249{
Todd Kjos673068e2017-06-29 12:02:03 -07001250 struct binder_proc *proc = node->proc;
1251
Martijn Coenen858b2712017-08-31 10:04:26 +02001252 assert_spin_locked(&node->lock);
Todd Kjos673068e2017-06-29 12:02:03 -07001253 if (proc)
Martijn Coenen858b2712017-08-31 10:04:26 +02001254 assert_spin_locked(&proc->inner_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001255 if (strong) {
1256 if (internal) {
1257 if (target_list == NULL &&
1258 node->internal_strong_refs == 0 &&
Martijn Coenen342e5c92017-02-03 14:40:46 -08001259 !(node->proc &&
1260 node == node->proc->context->binder_context_mgr_node &&
1261 node->has_strong_ref)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301262 pr_err("invalid inc strong node for %d\n",
1263 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001264 return -EINVAL;
1265 }
1266 node->internal_strong_refs++;
1267 } else
1268 node->local_strong_refs++;
1269 if (!node->has_strong_ref && target_list) {
Todd Kjos72196392017-06-29 12:02:02 -07001270 binder_dequeue_work_ilocked(&node->work);
Martijn Coenen148ade22017-11-15 09:21:35 +01001271 /*
1272 * Note: this function is the only place where we queue
1273 * directly to a thread->todo without using the
1274 * corresponding binder_enqueue_thread_work() helper
1275 * functions; in this case it's ok to not set the
1276 * process_todo flag, since we know this node work will
1277 * always be followed by other work that starts queue
1278 * processing: in case of synchronous transactions, a
1279 * BR_REPLY or BR_ERROR; in case of oneway
1280 * transactions, a BR_TRANSACTION_COMPLETE.
1281 */
Todd Kjos72196392017-06-29 12:02:02 -07001282 binder_enqueue_work_ilocked(&node->work, target_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001283 }
1284 } else {
1285 if (!internal)
1286 node->local_weak_refs++;
1287 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
1288 if (target_list == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301289 pr_err("invalid inc weak node for %d\n",
1290 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001291 return -EINVAL;
1292 }
Martijn Coenen148ade22017-11-15 09:21:35 +01001293 /*
1294 * See comment above
1295 */
Todd Kjos72196392017-06-29 12:02:02 -07001296 binder_enqueue_work_ilocked(&node->work, target_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001297 }
1298 }
1299 return 0;
1300}
1301
Todd Kjosed297212017-06-29 12:02:01 -07001302static int binder_inc_node(struct binder_node *node, int strong, int internal,
1303 struct list_head *target_list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001304{
Todd Kjosed297212017-06-29 12:02:01 -07001305 int ret;
1306
Todd Kjos673068e2017-06-29 12:02:03 -07001307 binder_node_inner_lock(node);
1308 ret = binder_inc_node_nilocked(node, strong, internal, target_list);
1309 binder_node_inner_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07001310
1311 return ret;
1312}
1313
Todd Kjos673068e2017-06-29 12:02:03 -07001314static bool binder_dec_node_nilocked(struct binder_node *node,
1315 int strong, int internal)
Todd Kjosed297212017-06-29 12:02:01 -07001316{
1317 struct binder_proc *proc = node->proc;
1318
Martijn Coenen858b2712017-08-31 10:04:26 +02001319 assert_spin_locked(&node->lock);
Todd Kjosed297212017-06-29 12:02:01 -07001320 if (proc)
Martijn Coenen858b2712017-08-31 10:04:26 +02001321 assert_spin_locked(&proc->inner_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001322 if (strong) {
1323 if (internal)
1324 node->internal_strong_refs--;
1325 else
1326 node->local_strong_refs--;
1327 if (node->local_strong_refs || node->internal_strong_refs)
Todd Kjosed297212017-06-29 12:02:01 -07001328 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001329 } else {
1330 if (!internal)
1331 node->local_weak_refs--;
Todd Kjosadc18842017-06-29 12:01:59 -07001332 if (node->local_weak_refs || node->tmp_refs ||
1333 !hlist_empty(&node->refs))
Todd Kjosed297212017-06-29 12:02:01 -07001334 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001335 }
Todd Kjosed297212017-06-29 12:02:01 -07001336
1337 if (proc && (node->has_strong_ref || node->has_weak_ref)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001338 if (list_empty(&node->work.entry)) {
Todd Kjos72196392017-06-29 12:02:02 -07001339 binder_enqueue_work_ilocked(&node->work, &proc->todo);
Martijn Coenen408c68b2017-08-31 10:04:19 +02001340 binder_wakeup_proc_ilocked(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001341 }
1342 } else {
1343 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
Todd Kjosadc18842017-06-29 12:01:59 -07001344 !node->local_weak_refs && !node->tmp_refs) {
Todd Kjosed297212017-06-29 12:02:01 -07001345 if (proc) {
Todd Kjos72196392017-06-29 12:02:02 -07001346 binder_dequeue_work_ilocked(&node->work);
1347 rb_erase(&node->rb_node, &proc->nodes);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001348 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301349 "refless node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001350 node->debug_id);
1351 } else {
Todd Kjos72196392017-06-29 12:02:02 -07001352 BUG_ON(!list_empty(&node->work.entry));
Todd Kjosc44b1232017-06-29 12:01:43 -07001353 spin_lock(&binder_dead_nodes_lock);
Todd Kjosed297212017-06-29 12:02:01 -07001354 /*
1355 * tmp_refs could have changed so
1356 * check it again
1357 */
1358 if (node->tmp_refs) {
1359 spin_unlock(&binder_dead_nodes_lock);
1360 return false;
1361 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001362 hlist_del(&node->dead_node);
Todd Kjosc44b1232017-06-29 12:01:43 -07001363 spin_unlock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001364 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301365 "dead node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001366 node->debug_id);
1367 }
Todd Kjosed297212017-06-29 12:02:01 -07001368 return true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001369 }
1370 }
Todd Kjosed297212017-06-29 12:02:01 -07001371 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001372}
1373
Todd Kjosed297212017-06-29 12:02:01 -07001374static void binder_dec_node(struct binder_node *node, int strong, int internal)
1375{
1376 bool free_node;
1377
Todd Kjos673068e2017-06-29 12:02:03 -07001378 binder_node_inner_lock(node);
1379 free_node = binder_dec_node_nilocked(node, strong, internal);
1380 binder_node_inner_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07001381 if (free_node)
1382 binder_free_node(node);
1383}
1384
1385static void binder_inc_node_tmpref_ilocked(struct binder_node *node)
Todd Kjosadc18842017-06-29 12:01:59 -07001386{
1387 /*
1388 * No call to binder_inc_node() is needed since we
1389 * don't need to inform userspace of any changes to
1390 * tmp_refs
1391 */
1392 node->tmp_refs++;
1393}
1394
1395/**
Todd Kjosed297212017-06-29 12:02:01 -07001396 * binder_inc_node_tmpref() - take a temporary reference on node
1397 * @node: node to reference
1398 *
1399 * Take reference on node to prevent the node from being freed
1400 * while referenced only by a local variable. The inner lock is
1401 * needed to serialize with the node work on the queue (which
1402 * isn't needed after the node is dead). If the node is dead
1403 * (node->proc is NULL), use binder_dead_nodes_lock to protect
1404 * node->tmp_refs against dead-node-only cases where the node
1405 * lock cannot be acquired (eg traversing the dead node list to
1406 * print nodes)
1407 */
1408static void binder_inc_node_tmpref(struct binder_node *node)
1409{
Todd Kjos673068e2017-06-29 12:02:03 -07001410 binder_node_lock(node);
Todd Kjosed297212017-06-29 12:02:01 -07001411 if (node->proc)
1412 binder_inner_proc_lock(node->proc);
1413 else
1414 spin_lock(&binder_dead_nodes_lock);
1415 binder_inc_node_tmpref_ilocked(node);
1416 if (node->proc)
1417 binder_inner_proc_unlock(node->proc);
1418 else
1419 spin_unlock(&binder_dead_nodes_lock);
Todd Kjos673068e2017-06-29 12:02:03 -07001420 binder_node_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07001421}
1422
1423/**
Todd Kjosadc18842017-06-29 12:01:59 -07001424 * binder_dec_node_tmpref() - remove a temporary reference on node
1425 * @node: node to reference
1426 *
1427 * Release temporary reference on node taken via binder_inc_node_tmpref()
1428 */
1429static void binder_dec_node_tmpref(struct binder_node *node)
1430{
Todd Kjosed297212017-06-29 12:02:01 -07001431 bool free_node;
1432
Todd Kjos673068e2017-06-29 12:02:03 -07001433 binder_node_inner_lock(node);
1434 if (!node->proc)
Todd Kjosed297212017-06-29 12:02:01 -07001435 spin_lock(&binder_dead_nodes_lock);
Todd Kjosadc18842017-06-29 12:01:59 -07001436 node->tmp_refs--;
1437 BUG_ON(node->tmp_refs < 0);
Todd Kjosed297212017-06-29 12:02:01 -07001438 if (!node->proc)
1439 spin_unlock(&binder_dead_nodes_lock);
Todd Kjosadc18842017-06-29 12:01:59 -07001440 /*
1441 * Call binder_dec_node() to check if all refcounts are 0
1442 * and cleanup is needed. Calling with strong=0 and internal=1
1443 * causes no actual reference to be released in binder_dec_node().
1444 * If that changes, a change is needed here too.
1445 */
Todd Kjos673068e2017-06-29 12:02:03 -07001446 free_node = binder_dec_node_nilocked(node, 0, 1);
1447 binder_node_inner_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07001448 if (free_node)
1449 binder_free_node(node);
Todd Kjosadc18842017-06-29 12:01:59 -07001450}
1451
1452static void binder_put_node(struct binder_node *node)
1453{
1454 binder_dec_node_tmpref(node);
1455}
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001456
Todd Kjos2c1838d2017-06-29 12:02:08 -07001457static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc,
1458 u32 desc, bool need_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001459{
1460 struct rb_node *n = proc->refs_by_desc.rb_node;
1461 struct binder_ref *ref;
1462
1463 while (n) {
1464 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1465
Todd Kjos372e3142017-06-29 12:01:58 -07001466 if (desc < ref->data.desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001467 n = n->rb_left;
Todd Kjos372e3142017-06-29 12:01:58 -07001468 } else if (desc > ref->data.desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001469 n = n->rb_right;
Todd Kjos372e3142017-06-29 12:01:58 -07001470 } else if (need_strong_ref && !ref->data.strong) {
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001471 binder_user_error("tried to use weak ref as strong ref\n");
1472 return NULL;
1473 } else {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001474 return ref;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001475 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001476 }
1477 return NULL;
1478}
1479
Todd Kjos372e3142017-06-29 12:01:58 -07001480/**
Todd Kjos2c1838d2017-06-29 12:02:08 -07001481 * binder_get_ref_for_node_olocked() - get the ref associated with given node
Todd Kjos372e3142017-06-29 12:01:58 -07001482 * @proc: binder_proc that owns the ref
1483 * @node: binder_node of target
1484 * @new_ref: newly allocated binder_ref to be initialized or %NULL
1485 *
1486 * Look up the ref for the given node and return it if it exists
1487 *
1488 * If it doesn't exist and the caller provides a newly allocated
1489 * ref, initialize the fields of the newly allocated ref and insert
1490 * into the given proc rb_trees and node refs list.
1491 *
1492 * Return: the ref for node. It is possible that another thread
1493 * allocated/initialized the ref first in which case the
1494 * returned ref would be different than the passed-in
1495 * new_ref. new_ref must be kfree'd by the caller in
1496 * this case.
1497 */
Todd Kjos2c1838d2017-06-29 12:02:08 -07001498static struct binder_ref *binder_get_ref_for_node_olocked(
1499 struct binder_proc *proc,
1500 struct binder_node *node,
1501 struct binder_ref *new_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001502{
Todd Kjos372e3142017-06-29 12:01:58 -07001503 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001504 struct rb_node **p = &proc->refs_by_node.rb_node;
1505 struct rb_node *parent = NULL;
Todd Kjos372e3142017-06-29 12:01:58 -07001506 struct binder_ref *ref;
1507 struct rb_node *n;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001508
1509 while (*p) {
1510 parent = *p;
1511 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1512
1513 if (node < ref->node)
1514 p = &(*p)->rb_left;
1515 else if (node > ref->node)
1516 p = &(*p)->rb_right;
1517 else
1518 return ref;
1519 }
Todd Kjos372e3142017-06-29 12:01:58 -07001520 if (!new_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001521 return NULL;
Todd Kjos372e3142017-06-29 12:01:58 -07001522
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001523 binder_stats_created(BINDER_STAT_REF);
Todd Kjos372e3142017-06-29 12:01:58 -07001524 new_ref->data.debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001525 new_ref->proc = proc;
1526 new_ref->node = node;
1527 rb_link_node(&new_ref->rb_node_node, parent, p);
1528 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1529
Todd Kjos372e3142017-06-29 12:01:58 -07001530 new_ref->data.desc = (node == context->binder_context_mgr_node) ? 0 : 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001531 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1532 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Todd Kjos372e3142017-06-29 12:01:58 -07001533 if (ref->data.desc > new_ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001534 break;
Todd Kjos372e3142017-06-29 12:01:58 -07001535 new_ref->data.desc = ref->data.desc + 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001536 }
1537
1538 p = &proc->refs_by_desc.rb_node;
1539 while (*p) {
1540 parent = *p;
1541 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1542
Todd Kjos372e3142017-06-29 12:01:58 -07001543 if (new_ref->data.desc < ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001544 p = &(*p)->rb_left;
Todd Kjos372e3142017-06-29 12:01:58 -07001545 else if (new_ref->data.desc > ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001546 p = &(*p)->rb_right;
1547 else
1548 BUG();
1549 }
1550 rb_link_node(&new_ref->rb_node_desc, parent, p);
1551 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
Todd Kjos673068e2017-06-29 12:02:03 -07001552
1553 binder_node_lock(node);
Todd Kjose4cffcf2017-06-29 12:01:50 -07001554 hlist_add_head(&new_ref->node_entry, &node->refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001555
Todd Kjose4cffcf2017-06-29 12:01:50 -07001556 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1557 "%d new ref %d desc %d for node %d\n",
Todd Kjos372e3142017-06-29 12:01:58 -07001558 proc->pid, new_ref->data.debug_id, new_ref->data.desc,
Todd Kjose4cffcf2017-06-29 12:01:50 -07001559 node->debug_id);
Todd Kjos673068e2017-06-29 12:02:03 -07001560 binder_node_unlock(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001561 return new_ref;
1562}
1563
Todd Kjos2c1838d2017-06-29 12:02:08 -07001564static void binder_cleanup_ref_olocked(struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001565{
Todd Kjosed297212017-06-29 12:02:01 -07001566 bool delete_node = false;
Todd Kjosed297212017-06-29 12:02:01 -07001567
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001568 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301569 "%d delete ref %d desc %d for node %d\n",
Todd Kjos372e3142017-06-29 12:01:58 -07001570 ref->proc->pid, ref->data.debug_id, ref->data.desc,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301571 ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001572
1573 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1574 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
Todd Kjos372e3142017-06-29 12:01:58 -07001575
Todd Kjos673068e2017-06-29 12:02:03 -07001576 binder_node_inner_lock(ref->node);
Todd Kjos372e3142017-06-29 12:01:58 -07001577 if (ref->data.strong)
Todd Kjos673068e2017-06-29 12:02:03 -07001578 binder_dec_node_nilocked(ref->node, 1, 1);
Todd Kjos372e3142017-06-29 12:01:58 -07001579
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001580 hlist_del(&ref->node_entry);
Todd Kjos673068e2017-06-29 12:02:03 -07001581 delete_node = binder_dec_node_nilocked(ref->node, 0, 1);
1582 binder_node_inner_unlock(ref->node);
Todd Kjosed297212017-06-29 12:02:01 -07001583 /*
1584 * Clear ref->node unless we want the caller to free the node
1585 */
1586 if (!delete_node) {
1587 /*
1588 * The caller uses ref->node to determine
1589 * whether the node needs to be freed. Clear
1590 * it since the node is still alive.
1591 */
1592 ref->node = NULL;
1593 }
Todd Kjos372e3142017-06-29 12:01:58 -07001594
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001595 if (ref->death) {
1596 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301597 "%d delete ref %d desc %d has death notification\n",
Todd Kjos372e3142017-06-29 12:01:58 -07001598 ref->proc->pid, ref->data.debug_id,
1599 ref->data.desc);
Todd Kjos72196392017-06-29 12:02:02 -07001600 binder_dequeue_work(ref->proc, &ref->death->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001601 binder_stats_deleted(BINDER_STAT_DEATH);
1602 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001603 binder_stats_deleted(BINDER_STAT_REF);
1604}
1605
Todd Kjos372e3142017-06-29 12:01:58 -07001606/**
Todd Kjos2c1838d2017-06-29 12:02:08 -07001607 * binder_inc_ref_olocked() - increment the ref for given handle
Todd Kjos372e3142017-06-29 12:01:58 -07001608 * @ref: ref to be incremented
1609 * @strong: if true, strong increment, else weak
1610 * @target_list: list to queue node work on
1611 *
Todd Kjos2c1838d2017-06-29 12:02:08 -07001612 * Increment the ref. @ref->proc->outer_lock must be held on entry
Todd Kjos372e3142017-06-29 12:01:58 -07001613 *
1614 * Return: 0, if successful, else errno
1615 */
Todd Kjos2c1838d2017-06-29 12:02:08 -07001616static int binder_inc_ref_olocked(struct binder_ref *ref, int strong,
1617 struct list_head *target_list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001618{
1619 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +09001620
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001621 if (strong) {
Todd Kjos372e3142017-06-29 12:01:58 -07001622 if (ref->data.strong == 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001623 ret = binder_inc_node(ref->node, 1, 1, target_list);
1624 if (ret)
1625 return ret;
1626 }
Todd Kjos372e3142017-06-29 12:01:58 -07001627 ref->data.strong++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001628 } else {
Todd Kjos372e3142017-06-29 12:01:58 -07001629 if (ref->data.weak == 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001630 ret = binder_inc_node(ref->node, 0, 1, target_list);
1631 if (ret)
1632 return ret;
1633 }
Todd Kjos372e3142017-06-29 12:01:58 -07001634 ref->data.weak++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001635 }
1636 return 0;
1637}
1638
Todd Kjos372e3142017-06-29 12:01:58 -07001639/**
1640 * binder_dec_ref() - dec the ref for given handle
1641 * @ref: ref to be decremented
1642 * @strong: if true, strong decrement, else weak
1643 *
1644 * Decrement the ref.
1645 *
Todd Kjos372e3142017-06-29 12:01:58 -07001646 * Return: true if ref is cleaned up and ready to be freed
1647 */
Todd Kjos2c1838d2017-06-29 12:02:08 -07001648static bool binder_dec_ref_olocked(struct binder_ref *ref, int strong)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001649{
1650 if (strong) {
Todd Kjos372e3142017-06-29 12:01:58 -07001651 if (ref->data.strong == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301652 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
Todd Kjos372e3142017-06-29 12:01:58 -07001653 ref->proc->pid, ref->data.debug_id,
1654 ref->data.desc, ref->data.strong,
1655 ref->data.weak);
1656 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001657 }
Todd Kjos372e3142017-06-29 12:01:58 -07001658 ref->data.strong--;
Todd Kjosed297212017-06-29 12:02:01 -07001659 if (ref->data.strong == 0)
1660 binder_dec_node(ref->node, strong, 1);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001661 } else {
Todd Kjos372e3142017-06-29 12:01:58 -07001662 if (ref->data.weak == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301663 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
Todd Kjos372e3142017-06-29 12:01:58 -07001664 ref->proc->pid, ref->data.debug_id,
1665 ref->data.desc, ref->data.strong,
1666 ref->data.weak);
1667 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001668 }
Todd Kjos372e3142017-06-29 12:01:58 -07001669 ref->data.weak--;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001670 }
Todd Kjos372e3142017-06-29 12:01:58 -07001671 if (ref->data.strong == 0 && ref->data.weak == 0) {
Todd Kjos2c1838d2017-06-29 12:02:08 -07001672 binder_cleanup_ref_olocked(ref);
Todd Kjos372e3142017-06-29 12:01:58 -07001673 return true;
1674 }
1675 return false;
1676}
1677
1678/**
1679 * binder_get_node_from_ref() - get the node from the given proc/desc
1680 * @proc: proc containing the ref
1681 * @desc: the handle associated with the ref
1682 * @need_strong_ref: if true, only return node if ref is strong
1683 * @rdata: the id/refcount data for the ref
1684 *
1685 * Given a proc and ref handle, return the associated binder_node
1686 *
1687 * Return: a binder_node or NULL if not found or not strong when strong required
1688 */
1689static struct binder_node *binder_get_node_from_ref(
1690 struct binder_proc *proc,
1691 u32 desc, bool need_strong_ref,
1692 struct binder_ref_data *rdata)
1693{
1694 struct binder_node *node;
1695 struct binder_ref *ref;
1696
Todd Kjos2c1838d2017-06-29 12:02:08 -07001697 binder_proc_lock(proc);
1698 ref = binder_get_ref_olocked(proc, desc, need_strong_ref);
Todd Kjos372e3142017-06-29 12:01:58 -07001699 if (!ref)
1700 goto err_no_ref;
1701 node = ref->node;
Todd Kjosadc18842017-06-29 12:01:59 -07001702 /*
1703 * Take an implicit reference on the node to ensure
1704 * it stays alive until the call to binder_put_node()
1705 */
1706 binder_inc_node_tmpref(node);
Todd Kjos372e3142017-06-29 12:01:58 -07001707 if (rdata)
1708 *rdata = ref->data;
Todd Kjos2c1838d2017-06-29 12:02:08 -07001709 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07001710
1711 return node;
1712
1713err_no_ref:
Todd Kjos2c1838d2017-06-29 12:02:08 -07001714 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07001715 return NULL;
1716}
1717
1718/**
1719 * binder_free_ref() - free the binder_ref
1720 * @ref: ref to free
1721 *
Todd Kjosed297212017-06-29 12:02:01 -07001722 * Free the binder_ref. Free the binder_node indicated by ref->node
1723 * (if non-NULL) and the binder_ref_death indicated by ref->death.
Todd Kjos372e3142017-06-29 12:01:58 -07001724 */
1725static void binder_free_ref(struct binder_ref *ref)
1726{
Todd Kjosed297212017-06-29 12:02:01 -07001727 if (ref->node)
1728 binder_free_node(ref->node);
Todd Kjos372e3142017-06-29 12:01:58 -07001729 kfree(ref->death);
1730 kfree(ref);
1731}
1732
1733/**
1734 * binder_update_ref_for_handle() - inc/dec the ref for given handle
1735 * @proc: proc containing the ref
1736 * @desc: the handle associated with the ref
1737 * @increment: true=inc reference, false=dec reference
1738 * @strong: true=strong reference, false=weak reference
1739 * @rdata: the id/refcount data for the ref
1740 *
1741 * Given a proc and ref handle, increment or decrement the ref
1742 * according to "increment" arg.
1743 *
1744 * Return: 0 if successful, else errno
1745 */
1746static int binder_update_ref_for_handle(struct binder_proc *proc,
1747 uint32_t desc, bool increment, bool strong,
1748 struct binder_ref_data *rdata)
1749{
1750 int ret = 0;
1751 struct binder_ref *ref;
1752 bool delete_ref = false;
1753
Todd Kjos2c1838d2017-06-29 12:02:08 -07001754 binder_proc_lock(proc);
1755 ref = binder_get_ref_olocked(proc, desc, strong);
Todd Kjos372e3142017-06-29 12:01:58 -07001756 if (!ref) {
1757 ret = -EINVAL;
1758 goto err_no_ref;
1759 }
1760 if (increment)
Todd Kjos2c1838d2017-06-29 12:02:08 -07001761 ret = binder_inc_ref_olocked(ref, strong, NULL);
Todd Kjos372e3142017-06-29 12:01:58 -07001762 else
Todd Kjos2c1838d2017-06-29 12:02:08 -07001763 delete_ref = binder_dec_ref_olocked(ref, strong);
Todd Kjos372e3142017-06-29 12:01:58 -07001764
1765 if (rdata)
1766 *rdata = ref->data;
Todd Kjos2c1838d2017-06-29 12:02:08 -07001767 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07001768
1769 if (delete_ref)
1770 binder_free_ref(ref);
1771 return ret;
1772
1773err_no_ref:
Todd Kjos2c1838d2017-06-29 12:02:08 -07001774 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07001775 return ret;
1776}
1777
1778/**
1779 * binder_dec_ref_for_handle() - dec the ref for given handle
1780 * @proc: proc containing the ref
1781 * @desc: the handle associated with the ref
1782 * @strong: true=strong reference, false=weak reference
1783 * @rdata: the id/refcount data for the ref
1784 *
1785 * Just calls binder_update_ref_for_handle() to decrement the ref.
1786 *
1787 * Return: 0 if successful, else errno
1788 */
1789static int binder_dec_ref_for_handle(struct binder_proc *proc,
1790 uint32_t desc, bool strong, struct binder_ref_data *rdata)
1791{
1792 return binder_update_ref_for_handle(proc, desc, false, strong, rdata);
1793}
1794
1795
1796/**
1797 * binder_inc_ref_for_node() - increment the ref for given proc/node
1798 * @proc: proc containing the ref
1799 * @node: target node
1800 * @strong: true=strong reference, false=weak reference
1801 * @target_list: worklist to use if node is incremented
1802 * @rdata: the id/refcount data for the ref
1803 *
1804 * Given a proc and node, increment the ref. Create the ref if it
1805 * doesn't already exist
1806 *
1807 * Return: 0 if successful, else errno
1808 */
1809static int binder_inc_ref_for_node(struct binder_proc *proc,
1810 struct binder_node *node,
1811 bool strong,
1812 struct list_head *target_list,
1813 struct binder_ref_data *rdata)
1814{
1815 struct binder_ref *ref;
1816 struct binder_ref *new_ref = NULL;
1817 int ret = 0;
1818
Todd Kjos2c1838d2017-06-29 12:02:08 -07001819 binder_proc_lock(proc);
1820 ref = binder_get_ref_for_node_olocked(proc, node, NULL);
Todd Kjos372e3142017-06-29 12:01:58 -07001821 if (!ref) {
Todd Kjos2c1838d2017-06-29 12:02:08 -07001822 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07001823 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1824 if (!new_ref)
1825 return -ENOMEM;
Todd Kjos2c1838d2017-06-29 12:02:08 -07001826 binder_proc_lock(proc);
1827 ref = binder_get_ref_for_node_olocked(proc, node, new_ref);
Todd Kjos372e3142017-06-29 12:01:58 -07001828 }
Todd Kjos2c1838d2017-06-29 12:02:08 -07001829 ret = binder_inc_ref_olocked(ref, strong, target_list);
Todd Kjos372e3142017-06-29 12:01:58 -07001830 *rdata = ref->data;
Todd Kjos2c1838d2017-06-29 12:02:08 -07001831 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07001832 if (new_ref && ref != new_ref)
1833 /*
1834 * Another thread created the ref first so
1835 * free the one we allocated
1836 */
1837 kfree(new_ref);
1838 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001839}
1840
Martijn Coenen0b89d692017-06-29 12:02:06 -07001841static void binder_pop_transaction_ilocked(struct binder_thread *target_thread,
1842 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001843{
Todd Kjosb6d282c2017-06-29 12:01:54 -07001844 BUG_ON(!target_thread);
Martijn Coenen858b2712017-08-31 10:04:26 +02001845 assert_spin_locked(&target_thread->proc->inner_lock);
Todd Kjosb6d282c2017-06-29 12:01:54 -07001846 BUG_ON(target_thread->transaction_stack != t);
1847 BUG_ON(target_thread->transaction_stack->from != target_thread);
1848 target_thread->transaction_stack =
1849 target_thread->transaction_stack->from_parent;
1850 t->from = NULL;
1851}
1852
Todd Kjos7a4408c2017-06-29 12:01:57 -07001853/**
1854 * binder_thread_dec_tmpref() - decrement thread->tmp_ref
1855 * @thread: thread to decrement
1856 *
1857 * A thread needs to be kept alive while being used to create or
1858 * handle a transaction. binder_get_txn_from() is used to safely
1859 * extract t->from from a binder_transaction and keep the thread
1860 * indicated by t->from from being freed. When done with that
1861 * binder_thread, this function is called to decrement the
1862 * tmp_ref and free if appropriate (thread has been released
1863 * and no transaction being processed by the driver)
1864 */
1865static void binder_thread_dec_tmpref(struct binder_thread *thread)
1866{
1867 /*
1868 * atomic is used to protect the counter value while
1869 * it cannot reach zero or thread->is_dead is false
Todd Kjos7a4408c2017-06-29 12:01:57 -07001870 */
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07001871 binder_inner_proc_lock(thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07001872 atomic_dec(&thread->tmp_ref);
1873 if (thread->is_dead && !atomic_read(&thread->tmp_ref)) {
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07001874 binder_inner_proc_unlock(thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07001875 binder_free_thread(thread);
1876 return;
1877 }
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07001878 binder_inner_proc_unlock(thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07001879}
1880
1881/**
1882 * binder_proc_dec_tmpref() - decrement proc->tmp_ref
1883 * @proc: proc to decrement
1884 *
1885 * A binder_proc needs to be kept alive while being used to create or
1886 * handle a transaction. proc->tmp_ref is incremented when
1887 * creating a new transaction or the binder_proc is currently in-use
1888 * by threads that are being released. When done with the binder_proc,
1889 * this function is called to decrement the counter and free the
1890 * proc if appropriate (proc has been released, all threads have
1891 * been released and not currenly in-use to process a transaction).
1892 */
1893static void binder_proc_dec_tmpref(struct binder_proc *proc)
1894{
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07001895 binder_inner_proc_lock(proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07001896 proc->tmp_ref--;
1897 if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) &&
1898 !proc->tmp_ref) {
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07001899 binder_inner_proc_unlock(proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07001900 binder_free_proc(proc);
1901 return;
1902 }
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07001903 binder_inner_proc_unlock(proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07001904}
1905
1906/**
1907 * binder_get_txn_from() - safely extract the "from" thread in transaction
1908 * @t: binder transaction for t->from
1909 *
1910 * Atomically return the "from" thread and increment the tmp_ref
1911 * count for the thread to ensure it stays alive until
1912 * binder_thread_dec_tmpref() is called.
1913 *
1914 * Return: the value of t->from
1915 */
1916static struct binder_thread *binder_get_txn_from(
1917 struct binder_transaction *t)
1918{
1919 struct binder_thread *from;
1920
1921 spin_lock(&t->lock);
1922 from = t->from;
1923 if (from)
1924 atomic_inc(&from->tmp_ref);
1925 spin_unlock(&t->lock);
1926 return from;
1927}
1928
Martijn Coenen0b89d692017-06-29 12:02:06 -07001929/**
1930 * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock
1931 * @t: binder transaction for t->from
1932 *
1933 * Same as binder_get_txn_from() except it also acquires the proc->inner_lock
1934 * to guarantee that the thread cannot be released while operating on it.
1935 * The caller must call binder_inner_proc_unlock() to release the inner lock
1936 * as well as call binder_dec_thread_txn() to release the reference.
1937 *
1938 * Return: the value of t->from
1939 */
1940static struct binder_thread *binder_get_txn_from_and_acq_inner(
1941 struct binder_transaction *t)
1942{
1943 struct binder_thread *from;
1944
1945 from = binder_get_txn_from(t);
1946 if (!from)
1947 return NULL;
1948 binder_inner_proc_lock(from->proc);
1949 if (t->from) {
1950 BUG_ON(from != t->from);
1951 return from;
1952 }
1953 binder_inner_proc_unlock(from->proc);
1954 binder_thread_dec_tmpref(from);
1955 return NULL;
1956}
1957
Todd Kjosb6d282c2017-06-29 12:01:54 -07001958static void binder_free_transaction(struct binder_transaction *t)
1959{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001960 if (t->buffer)
1961 t->buffer->transaction = NULL;
1962 kfree(t);
1963 binder_stats_deleted(BINDER_STAT_TRANSACTION);
1964}
1965
1966static void binder_send_failed_reply(struct binder_transaction *t,
1967 uint32_t error_code)
1968{
1969 struct binder_thread *target_thread;
Lucas Tanured4ec15e2014-07-13 21:31:05 -03001970 struct binder_transaction *next;
Seunghun Lee10f62862014-05-01 01:30:23 +09001971
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001972 BUG_ON(t->flags & TF_ONE_WAY);
1973 while (1) {
Martijn Coenen0b89d692017-06-29 12:02:06 -07001974 target_thread = binder_get_txn_from_and_acq_inner(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001975 if (target_thread) {
Todd Kjos26549d12017-06-29 12:01:55 -07001976 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1977 "send failed reply for transaction %d to %d:%d\n",
1978 t->debug_id,
1979 target_thread->proc->pid,
1980 target_thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001981
Martijn Coenen0b89d692017-06-29 12:02:06 -07001982 binder_pop_transaction_ilocked(target_thread, t);
Todd Kjos26549d12017-06-29 12:01:55 -07001983 if (target_thread->reply_error.cmd == BR_OK) {
1984 target_thread->reply_error.cmd = error_code;
Martijn Coenen148ade22017-11-15 09:21:35 +01001985 binder_enqueue_thread_work_ilocked(
1986 target_thread,
1987 &target_thread->reply_error.work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001988 wake_up_interruptible(&target_thread->wait);
1989 } else {
Todd Kjose46a3b32018-02-07 12:38:47 -08001990 /*
1991 * Cannot get here for normal operation, but
1992 * we can if multiple synchronous transactions
1993 * are sent without blocking for responses.
1994 * Just ignore the 2nd error in this case.
1995 */
1996 pr_warn("Unexpected reply error: %u\n",
1997 target_thread->reply_error.cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001998 }
Martijn Coenen0b89d692017-06-29 12:02:06 -07001999 binder_inner_proc_unlock(target_thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07002000 binder_thread_dec_tmpref(target_thread);
Todd Kjos26549d12017-06-29 12:01:55 -07002001 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002002 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002003 }
Lucas Tanured4ec15e2014-07-13 21:31:05 -03002004 next = t->from_parent;
2005
2006 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2007 "send failed reply for transaction %d, target dead\n",
2008 t->debug_id);
2009
Todd Kjosb6d282c2017-06-29 12:01:54 -07002010 binder_free_transaction(t);
Lucas Tanured4ec15e2014-07-13 21:31:05 -03002011 if (next == NULL) {
2012 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2013 "reply failed, no target thread at root\n");
2014 return;
2015 }
2016 t = next;
2017 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2018 "reply failed, no target thread -- retry %d\n",
2019 t->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002020 }
2021}
2022
Martijn Coenenfeba3902017-02-03 14:40:45 -08002023/**
Martijn Coenenfb2c4452017-11-13 10:06:08 +01002024 * binder_cleanup_transaction() - cleans up undelivered transaction
2025 * @t: transaction that needs to be cleaned up
2026 * @reason: reason the transaction wasn't delivered
2027 * @error_code: error to return to caller (if synchronous call)
2028 */
2029static void binder_cleanup_transaction(struct binder_transaction *t,
2030 const char *reason,
2031 uint32_t error_code)
2032{
2033 if (t->buffer->target_node && !(t->flags & TF_ONE_WAY)) {
2034 binder_send_failed_reply(t, error_code);
2035 } else {
2036 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2037 "undelivered transaction %d, %s\n",
2038 t->debug_id, reason);
2039 binder_free_transaction(t);
2040 }
2041}
2042
2043/**
Martijn Coenenfeba3902017-02-03 14:40:45 -08002044 * binder_validate_object() - checks for a valid metadata object in a buffer.
2045 * @buffer: binder_buffer that we're parsing.
2046 * @offset: offset in the buffer at which to validate an object.
2047 *
2048 * Return: If there's a valid metadata object at @offset in @buffer, the
2049 * size of that object. Otherwise, it returns zero.
2050 */
2051static size_t binder_validate_object(struct binder_buffer *buffer, u64 offset)
2052{
2053 /* Check if we can read a header first */
2054 struct binder_object_header *hdr;
2055 size_t object_size = 0;
2056
Dan Carpenter361f2dd2018-03-29 12:14:40 +03002057 if (buffer->data_size < sizeof(*hdr) ||
2058 offset > buffer->data_size - sizeof(*hdr) ||
Martijn Coenenfeba3902017-02-03 14:40:45 -08002059 !IS_ALIGNED(offset, sizeof(u32)))
2060 return 0;
2061
2062 /* Ok, now see if we can read a complete object. */
2063 hdr = (struct binder_object_header *)(buffer->data + offset);
2064 switch (hdr->type) {
2065 case BINDER_TYPE_BINDER:
2066 case BINDER_TYPE_WEAK_BINDER:
2067 case BINDER_TYPE_HANDLE:
2068 case BINDER_TYPE_WEAK_HANDLE:
2069 object_size = sizeof(struct flat_binder_object);
2070 break;
2071 case BINDER_TYPE_FD:
2072 object_size = sizeof(struct binder_fd_object);
2073 break;
Martijn Coenen79802402017-02-03 14:40:51 -08002074 case BINDER_TYPE_PTR:
2075 object_size = sizeof(struct binder_buffer_object);
2076 break;
Martijn Coenendef95c72017-02-03 14:40:52 -08002077 case BINDER_TYPE_FDA:
2078 object_size = sizeof(struct binder_fd_array_object);
2079 break;
Martijn Coenenfeba3902017-02-03 14:40:45 -08002080 default:
2081 return 0;
2082 }
2083 if (offset <= buffer->data_size - object_size &&
2084 buffer->data_size >= object_size)
2085 return object_size;
2086 else
2087 return 0;
2088}
2089
Martijn Coenen79802402017-02-03 14:40:51 -08002090/**
2091 * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
2092 * @b: binder_buffer containing the object
2093 * @index: index in offset array at which the binder_buffer_object is
2094 * located
2095 * @start: points to the start of the offset array
2096 * @num_valid: the number of valid offsets in the offset array
2097 *
2098 * Return: If @index is within the valid range of the offset array
2099 * described by @start and @num_valid, and if there's a valid
2100 * binder_buffer_object at the offset found in index @index
2101 * of the offset array, that object is returned. Otherwise,
2102 * %NULL is returned.
2103 * Note that the offset found in index @index itself is not
2104 * verified; this function assumes that @num_valid elements
2105 * from @start were previously verified to have valid offsets.
2106 */
2107static struct binder_buffer_object *binder_validate_ptr(struct binder_buffer *b,
2108 binder_size_t index,
2109 binder_size_t *start,
2110 binder_size_t num_valid)
2111{
2112 struct binder_buffer_object *buffer_obj;
2113 binder_size_t *offp;
2114
2115 if (index >= num_valid)
2116 return NULL;
2117
2118 offp = start + index;
2119 buffer_obj = (struct binder_buffer_object *)(b->data + *offp);
2120 if (buffer_obj->hdr.type != BINDER_TYPE_PTR)
2121 return NULL;
2122
2123 return buffer_obj;
2124}
2125
2126/**
2127 * binder_validate_fixup() - validates pointer/fd fixups happen in order.
2128 * @b: transaction buffer
2129 * @objects_start start of objects buffer
2130 * @buffer: binder_buffer_object in which to fix up
2131 * @offset: start offset in @buffer to fix up
2132 * @last_obj: last binder_buffer_object that we fixed up in
2133 * @last_min_offset: minimum fixup offset in @last_obj
2134 *
2135 * Return: %true if a fixup in buffer @buffer at offset @offset is
2136 * allowed.
2137 *
2138 * For safety reasons, we only allow fixups inside a buffer to happen
2139 * at increasing offsets; additionally, we only allow fixup on the last
2140 * buffer object that was verified, or one of its parents.
2141 *
2142 * Example of what is allowed:
2143 *
2144 * A
2145 * B (parent = A, offset = 0)
2146 * C (parent = A, offset = 16)
2147 * D (parent = C, offset = 0)
2148 * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
2149 *
2150 * Examples of what is not allowed:
2151 *
2152 * Decreasing offsets within the same parent:
2153 * A
2154 * C (parent = A, offset = 16)
2155 * B (parent = A, offset = 0) // decreasing offset within A
2156 *
2157 * Referring to a parent that wasn't the last object or any of its parents:
2158 * A
2159 * B (parent = A, offset = 0)
2160 * C (parent = A, offset = 0)
2161 * C (parent = A, offset = 16)
2162 * D (parent = B, offset = 0) // B is not A or any of A's parents
2163 */
2164static bool binder_validate_fixup(struct binder_buffer *b,
2165 binder_size_t *objects_start,
2166 struct binder_buffer_object *buffer,
2167 binder_size_t fixup_offset,
2168 struct binder_buffer_object *last_obj,
2169 binder_size_t last_min_offset)
2170{
2171 if (!last_obj) {
2172 /* Nothing to fix up in */
2173 return false;
2174 }
2175
2176 while (last_obj != buffer) {
2177 /*
2178 * Safe to retrieve the parent of last_obj, since it
2179 * was already previously verified by the driver.
2180 */
2181 if ((last_obj->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0)
2182 return false;
2183 last_min_offset = last_obj->parent_offset + sizeof(uintptr_t);
2184 last_obj = (struct binder_buffer_object *)
2185 (b->data + *(objects_start + last_obj->parent));
2186 }
2187 return (fixup_offset >= last_min_offset);
2188}
2189
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002190static void binder_transaction_buffer_release(struct binder_proc *proc,
2191 struct binder_buffer *buffer,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002192 binder_size_t *failed_at)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002193{
Martijn Coenen79802402017-02-03 14:40:51 -08002194 binder_size_t *offp, *off_start, *off_end;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002195 int debug_id = buffer->debug_id;
2196
2197 binder_debug(BINDER_DEBUG_TRANSACTION,
Todd Kjos8ca86f12018-02-07 13:57:37 -08002198 "%d buffer release %d, size %zd-%zd, failed at %pK\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002199 proc->pid, buffer->debug_id,
2200 buffer->data_size, buffer->offsets_size, failed_at);
2201
2202 if (buffer->target_node)
2203 binder_dec_node(buffer->target_node, 1, 0);
2204
Martijn Coenen79802402017-02-03 14:40:51 -08002205 off_start = (binder_size_t *)(buffer->data +
2206 ALIGN(buffer->data_size, sizeof(void *)));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002207 if (failed_at)
2208 off_end = failed_at;
2209 else
Martijn Coenen79802402017-02-03 14:40:51 -08002210 off_end = (void *)off_start + buffer->offsets_size;
2211 for (offp = off_start; offp < off_end; offp++) {
Martijn Coenenfeba3902017-02-03 14:40:45 -08002212 struct binder_object_header *hdr;
2213 size_t object_size = binder_validate_object(buffer, *offp);
Seunghun Lee10f62862014-05-01 01:30:23 +09002214
Martijn Coenenfeba3902017-02-03 14:40:45 -08002215 if (object_size == 0) {
2216 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
Arve Hjønnevågda498892014-02-21 14:40:26 -08002217 debug_id, (u64)*offp, buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002218 continue;
2219 }
Martijn Coenenfeba3902017-02-03 14:40:45 -08002220 hdr = (struct binder_object_header *)(buffer->data + *offp);
2221 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002222 case BINDER_TYPE_BINDER:
2223 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08002224 struct flat_binder_object *fp;
2225 struct binder_node *node;
Seunghun Lee10f62862014-05-01 01:30:23 +09002226
Martijn Coenenfeba3902017-02-03 14:40:45 -08002227 fp = to_flat_binder_object(hdr);
2228 node = binder_get_node(proc, fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002229 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002230 pr_err("transaction release %d bad node %016llx\n",
2231 debug_id, (u64)fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002232 break;
2233 }
2234 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002235 " node %d u%016llx\n",
2236 node->debug_id, (u64)node->ptr);
Martijn Coenenfeba3902017-02-03 14:40:45 -08002237 binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER,
2238 0);
Todd Kjosadc18842017-06-29 12:01:59 -07002239 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002240 } break;
2241 case BINDER_TYPE_HANDLE:
2242 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08002243 struct flat_binder_object *fp;
Todd Kjos372e3142017-06-29 12:01:58 -07002244 struct binder_ref_data rdata;
2245 int ret;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02002246
Martijn Coenenfeba3902017-02-03 14:40:45 -08002247 fp = to_flat_binder_object(hdr);
Todd Kjos372e3142017-06-29 12:01:58 -07002248 ret = binder_dec_ref_for_handle(proc, fp->handle,
2249 hdr->type == BINDER_TYPE_HANDLE, &rdata);
2250
2251 if (ret) {
2252 pr_err("transaction release %d bad handle %d, ret = %d\n",
2253 debug_id, fp->handle, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002254 break;
2255 }
2256 binder_debug(BINDER_DEBUG_TRANSACTION,
Todd Kjos372e3142017-06-29 12:01:58 -07002257 " ref %d desc %d\n",
2258 rdata.debug_id, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002259 } break;
2260
Martijn Coenenfeba3902017-02-03 14:40:45 -08002261 case BINDER_TYPE_FD: {
2262 struct binder_fd_object *fp = to_binder_fd_object(hdr);
2263
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002264 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenenfeba3902017-02-03 14:40:45 -08002265 " fd %d\n", fp->fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002266 if (failed_at)
Martijn Coenenfeba3902017-02-03 14:40:45 -08002267 task_close_fd(proc, fp->fd);
2268 } break;
Martijn Coenen79802402017-02-03 14:40:51 -08002269 case BINDER_TYPE_PTR:
2270 /*
2271 * Nothing to do here, this will get cleaned up when the
2272 * transaction buffer gets freed
2273 */
2274 break;
Martijn Coenendef95c72017-02-03 14:40:52 -08002275 case BINDER_TYPE_FDA: {
2276 struct binder_fd_array_object *fda;
2277 struct binder_buffer_object *parent;
2278 uintptr_t parent_buffer;
2279 u32 *fd_array;
2280 size_t fd_index;
2281 binder_size_t fd_buf_size;
2282
2283 fda = to_binder_fd_array_object(hdr);
2284 parent = binder_validate_ptr(buffer, fda->parent,
2285 off_start,
2286 offp - off_start);
2287 if (!parent) {
Arvind Yadavf7f84fd2017-09-25 12:52:11 +05302288 pr_err("transaction release %d bad parent offset\n",
Martijn Coenendef95c72017-02-03 14:40:52 -08002289 debug_id);
2290 continue;
2291 }
2292 /*
2293 * Since the parent was already fixed up, convert it
2294 * back to kernel address space to access it
2295 */
2296 parent_buffer = parent->buffer -
Todd Kjos19c98722017-06-29 12:01:40 -07002297 binder_alloc_get_user_buffer_offset(
2298 &proc->alloc);
Martijn Coenendef95c72017-02-03 14:40:52 -08002299
2300 fd_buf_size = sizeof(u32) * fda->num_fds;
2301 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2302 pr_err("transaction release %d invalid number of fds (%lld)\n",
2303 debug_id, (u64)fda->num_fds);
2304 continue;
2305 }
2306 if (fd_buf_size > parent->length ||
2307 fda->parent_offset > parent->length - fd_buf_size) {
2308 /* No space for all file descriptors here. */
2309 pr_err("transaction release %d not enough space for %lld fds in buffer\n",
2310 debug_id, (u64)fda->num_fds);
2311 continue;
2312 }
Arnd Bergmann1c363ea2017-09-05 10:56:13 +02002313 fd_array = (u32 *)(parent_buffer + (uintptr_t)fda->parent_offset);
Martijn Coenendef95c72017-02-03 14:40:52 -08002314 for (fd_index = 0; fd_index < fda->num_fds; fd_index++)
2315 task_close_fd(proc, fd_array[fd_index]);
2316 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002317 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01002318 pr_err("transaction release %d bad object type %x\n",
Martijn Coenenfeba3902017-02-03 14:40:45 -08002319 debug_id, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002320 break;
2321 }
2322 }
2323}
2324
Martijn Coenena056af42017-02-03 14:40:49 -08002325static int binder_translate_binder(struct flat_binder_object *fp,
2326 struct binder_transaction *t,
2327 struct binder_thread *thread)
2328{
2329 struct binder_node *node;
Martijn Coenena056af42017-02-03 14:40:49 -08002330 struct binder_proc *proc = thread->proc;
2331 struct binder_proc *target_proc = t->to_proc;
Todd Kjos372e3142017-06-29 12:01:58 -07002332 struct binder_ref_data rdata;
Todd Kjosadc18842017-06-29 12:01:59 -07002333 int ret = 0;
Martijn Coenena056af42017-02-03 14:40:49 -08002334
2335 node = binder_get_node(proc, fp->binder);
2336 if (!node) {
Todd Kjos673068e2017-06-29 12:02:03 -07002337 node = binder_new_node(proc, fp);
Martijn Coenena056af42017-02-03 14:40:49 -08002338 if (!node)
2339 return -ENOMEM;
Martijn Coenena056af42017-02-03 14:40:49 -08002340 }
2341 if (fp->cookie != node->cookie) {
2342 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
2343 proc->pid, thread->pid, (u64)fp->binder,
2344 node->debug_id, (u64)fp->cookie,
2345 (u64)node->cookie);
Todd Kjosadc18842017-06-29 12:01:59 -07002346 ret = -EINVAL;
2347 goto done;
Martijn Coenena056af42017-02-03 14:40:49 -08002348 }
Todd Kjosadc18842017-06-29 12:01:59 -07002349 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2350 ret = -EPERM;
2351 goto done;
2352 }
Martijn Coenena056af42017-02-03 14:40:49 -08002353
Todd Kjos372e3142017-06-29 12:01:58 -07002354 ret = binder_inc_ref_for_node(target_proc, node,
2355 fp->hdr.type == BINDER_TYPE_BINDER,
2356 &thread->todo, &rdata);
2357 if (ret)
Todd Kjosadc18842017-06-29 12:01:59 -07002358 goto done;
Martijn Coenena056af42017-02-03 14:40:49 -08002359
2360 if (fp->hdr.type == BINDER_TYPE_BINDER)
2361 fp->hdr.type = BINDER_TYPE_HANDLE;
2362 else
2363 fp->hdr.type = BINDER_TYPE_WEAK_HANDLE;
2364 fp->binder = 0;
Todd Kjos372e3142017-06-29 12:01:58 -07002365 fp->handle = rdata.desc;
Martijn Coenena056af42017-02-03 14:40:49 -08002366 fp->cookie = 0;
Martijn Coenena056af42017-02-03 14:40:49 -08002367
Todd Kjos372e3142017-06-29 12:01:58 -07002368 trace_binder_transaction_node_to_ref(t, node, &rdata);
Martijn Coenena056af42017-02-03 14:40:49 -08002369 binder_debug(BINDER_DEBUG_TRANSACTION,
2370 " node %d u%016llx -> ref %d desc %d\n",
2371 node->debug_id, (u64)node->ptr,
Todd Kjos372e3142017-06-29 12:01:58 -07002372 rdata.debug_id, rdata.desc);
Todd Kjosadc18842017-06-29 12:01:59 -07002373done:
2374 binder_put_node(node);
2375 return ret;
Martijn Coenena056af42017-02-03 14:40:49 -08002376}
2377
2378static int binder_translate_handle(struct flat_binder_object *fp,
2379 struct binder_transaction *t,
2380 struct binder_thread *thread)
2381{
Martijn Coenena056af42017-02-03 14:40:49 -08002382 struct binder_proc *proc = thread->proc;
2383 struct binder_proc *target_proc = t->to_proc;
Todd Kjos372e3142017-06-29 12:01:58 -07002384 struct binder_node *node;
2385 struct binder_ref_data src_rdata;
Todd Kjosadc18842017-06-29 12:01:59 -07002386 int ret = 0;
Martijn Coenena056af42017-02-03 14:40:49 -08002387
Todd Kjos372e3142017-06-29 12:01:58 -07002388 node = binder_get_node_from_ref(proc, fp->handle,
2389 fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata);
2390 if (!node) {
Martijn Coenena056af42017-02-03 14:40:49 -08002391 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
2392 proc->pid, thread->pid, fp->handle);
2393 return -EINVAL;
2394 }
Todd Kjosadc18842017-06-29 12:01:59 -07002395 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2396 ret = -EPERM;
2397 goto done;
2398 }
Martijn Coenena056af42017-02-03 14:40:49 -08002399
Todd Kjos673068e2017-06-29 12:02:03 -07002400 binder_node_lock(node);
Todd Kjos372e3142017-06-29 12:01:58 -07002401 if (node->proc == target_proc) {
Martijn Coenena056af42017-02-03 14:40:49 -08002402 if (fp->hdr.type == BINDER_TYPE_HANDLE)
2403 fp->hdr.type = BINDER_TYPE_BINDER;
2404 else
2405 fp->hdr.type = BINDER_TYPE_WEAK_BINDER;
Todd Kjos372e3142017-06-29 12:01:58 -07002406 fp->binder = node->ptr;
2407 fp->cookie = node->cookie;
Todd Kjos673068e2017-06-29 12:02:03 -07002408 if (node->proc)
2409 binder_inner_proc_lock(node->proc);
2410 binder_inc_node_nilocked(node,
2411 fp->hdr.type == BINDER_TYPE_BINDER,
2412 0, NULL);
2413 if (node->proc)
2414 binder_inner_proc_unlock(node->proc);
Todd Kjos372e3142017-06-29 12:01:58 -07002415 trace_binder_transaction_ref_to_node(t, node, &src_rdata);
Martijn Coenena056af42017-02-03 14:40:49 -08002416 binder_debug(BINDER_DEBUG_TRANSACTION,
2417 " ref %d desc %d -> node %d u%016llx\n",
Todd Kjos372e3142017-06-29 12:01:58 -07002418 src_rdata.debug_id, src_rdata.desc, node->debug_id,
2419 (u64)node->ptr);
Todd Kjos673068e2017-06-29 12:02:03 -07002420 binder_node_unlock(node);
Martijn Coenena056af42017-02-03 14:40:49 -08002421 } else {
Todd Kjos372e3142017-06-29 12:01:58 -07002422 struct binder_ref_data dest_rdata;
Martijn Coenena056af42017-02-03 14:40:49 -08002423
Todd Kjos673068e2017-06-29 12:02:03 -07002424 binder_node_unlock(node);
Todd Kjos372e3142017-06-29 12:01:58 -07002425 ret = binder_inc_ref_for_node(target_proc, node,
2426 fp->hdr.type == BINDER_TYPE_HANDLE,
2427 NULL, &dest_rdata);
2428 if (ret)
Todd Kjosadc18842017-06-29 12:01:59 -07002429 goto done;
Martijn Coenena056af42017-02-03 14:40:49 -08002430
2431 fp->binder = 0;
Todd Kjos372e3142017-06-29 12:01:58 -07002432 fp->handle = dest_rdata.desc;
Martijn Coenena056af42017-02-03 14:40:49 -08002433 fp->cookie = 0;
Todd Kjos372e3142017-06-29 12:01:58 -07002434 trace_binder_transaction_ref_to_ref(t, node, &src_rdata,
2435 &dest_rdata);
Martijn Coenena056af42017-02-03 14:40:49 -08002436 binder_debug(BINDER_DEBUG_TRANSACTION,
2437 " ref %d desc %d -> ref %d desc %d (node %d)\n",
Todd Kjos372e3142017-06-29 12:01:58 -07002438 src_rdata.debug_id, src_rdata.desc,
2439 dest_rdata.debug_id, dest_rdata.desc,
2440 node->debug_id);
Martijn Coenena056af42017-02-03 14:40:49 -08002441 }
Todd Kjosadc18842017-06-29 12:01:59 -07002442done:
2443 binder_put_node(node);
2444 return ret;
Martijn Coenena056af42017-02-03 14:40:49 -08002445}
2446
2447static int binder_translate_fd(int fd,
2448 struct binder_transaction *t,
2449 struct binder_thread *thread,
2450 struct binder_transaction *in_reply_to)
2451{
2452 struct binder_proc *proc = thread->proc;
2453 struct binder_proc *target_proc = t->to_proc;
2454 int target_fd;
2455 struct file *file;
2456 int ret;
2457 bool target_allows_fd;
2458
2459 if (in_reply_to)
2460 target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS);
2461 else
2462 target_allows_fd = t->buffer->target_node->accept_fds;
2463 if (!target_allows_fd) {
2464 binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
2465 proc->pid, thread->pid,
2466 in_reply_to ? "reply" : "transaction",
2467 fd);
2468 ret = -EPERM;
2469 goto err_fd_not_accepted;
2470 }
2471
2472 file = fget(fd);
2473 if (!file) {
2474 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
2475 proc->pid, thread->pid, fd);
2476 ret = -EBADF;
2477 goto err_fget;
2478 }
2479 ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file);
2480 if (ret < 0) {
2481 ret = -EPERM;
2482 goto err_security;
2483 }
2484
2485 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
2486 if (target_fd < 0) {
2487 ret = -ENOMEM;
2488 goto err_get_unused_fd;
2489 }
2490 task_fd_install(target_proc, target_fd, file);
2491 trace_binder_transaction_fd(t, fd, target_fd);
2492 binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n",
2493 fd, target_fd);
2494
2495 return target_fd;
2496
2497err_get_unused_fd:
2498err_security:
2499 fput(file);
2500err_fget:
2501err_fd_not_accepted:
2502 return ret;
2503}
2504
Martijn Coenendef95c72017-02-03 14:40:52 -08002505static int binder_translate_fd_array(struct binder_fd_array_object *fda,
2506 struct binder_buffer_object *parent,
2507 struct binder_transaction *t,
2508 struct binder_thread *thread,
2509 struct binder_transaction *in_reply_to)
2510{
2511 binder_size_t fdi, fd_buf_size, num_installed_fds;
2512 int target_fd;
2513 uintptr_t parent_buffer;
2514 u32 *fd_array;
2515 struct binder_proc *proc = thread->proc;
2516 struct binder_proc *target_proc = t->to_proc;
2517
2518 fd_buf_size = sizeof(u32) * fda->num_fds;
2519 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2520 binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
2521 proc->pid, thread->pid, (u64)fda->num_fds);
2522 return -EINVAL;
2523 }
2524 if (fd_buf_size > parent->length ||
2525 fda->parent_offset > parent->length - fd_buf_size) {
2526 /* No space for all file descriptors here. */
2527 binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
2528 proc->pid, thread->pid, (u64)fda->num_fds);
2529 return -EINVAL;
2530 }
2531 /*
2532 * Since the parent was already fixed up, convert it
2533 * back to the kernel address space to access it
2534 */
Todd Kjos19c98722017-06-29 12:01:40 -07002535 parent_buffer = parent->buffer -
2536 binder_alloc_get_user_buffer_offset(&target_proc->alloc);
Arnd Bergmann1c363ea2017-09-05 10:56:13 +02002537 fd_array = (u32 *)(parent_buffer + (uintptr_t)fda->parent_offset);
Martijn Coenendef95c72017-02-03 14:40:52 -08002538 if (!IS_ALIGNED((unsigned long)fd_array, sizeof(u32))) {
2539 binder_user_error("%d:%d parent offset not aligned correctly.\n",
2540 proc->pid, thread->pid);
2541 return -EINVAL;
2542 }
2543 for (fdi = 0; fdi < fda->num_fds; fdi++) {
2544 target_fd = binder_translate_fd(fd_array[fdi], t, thread,
2545 in_reply_to);
2546 if (target_fd < 0)
2547 goto err_translate_fd_failed;
2548 fd_array[fdi] = target_fd;
2549 }
2550 return 0;
2551
2552err_translate_fd_failed:
2553 /*
2554 * Failed to allocate fd or security error, free fds
2555 * installed so far.
2556 */
2557 num_installed_fds = fdi;
2558 for (fdi = 0; fdi < num_installed_fds; fdi++)
2559 task_close_fd(target_proc, fd_array[fdi]);
2560 return target_fd;
2561}
2562
Martijn Coenen79802402017-02-03 14:40:51 -08002563static int binder_fixup_parent(struct binder_transaction *t,
2564 struct binder_thread *thread,
2565 struct binder_buffer_object *bp,
2566 binder_size_t *off_start,
2567 binder_size_t num_valid,
2568 struct binder_buffer_object *last_fixup_obj,
2569 binder_size_t last_fixup_min_off)
2570{
2571 struct binder_buffer_object *parent;
2572 u8 *parent_buffer;
2573 struct binder_buffer *b = t->buffer;
2574 struct binder_proc *proc = thread->proc;
2575 struct binder_proc *target_proc = t->to_proc;
2576
2577 if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT))
2578 return 0;
2579
2580 parent = binder_validate_ptr(b, bp->parent, off_start, num_valid);
2581 if (!parent) {
2582 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
2583 proc->pid, thread->pid);
2584 return -EINVAL;
2585 }
2586
2587 if (!binder_validate_fixup(b, off_start,
2588 parent, bp->parent_offset,
2589 last_fixup_obj,
2590 last_fixup_min_off)) {
2591 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
2592 proc->pid, thread->pid);
2593 return -EINVAL;
2594 }
2595
2596 if (parent->length < sizeof(binder_uintptr_t) ||
2597 bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) {
2598 /* No space for a pointer here! */
2599 binder_user_error("%d:%d got transaction with invalid parent offset\n",
2600 proc->pid, thread->pid);
2601 return -EINVAL;
2602 }
Arnd Bergmann1c363ea2017-09-05 10:56:13 +02002603 parent_buffer = (u8 *)((uintptr_t)parent->buffer -
Todd Kjos19c98722017-06-29 12:01:40 -07002604 binder_alloc_get_user_buffer_offset(
2605 &target_proc->alloc));
Martijn Coenen79802402017-02-03 14:40:51 -08002606 *(binder_uintptr_t *)(parent_buffer + bp->parent_offset) = bp->buffer;
2607
2608 return 0;
2609}
2610
Martijn Coenen408c68b2017-08-31 10:04:19 +02002611/**
2612 * binder_proc_transaction() - sends a transaction to a process and wakes it up
2613 * @t: transaction to send
2614 * @proc: process to send the transaction to
2615 * @thread: thread in @proc to send the transaction to (may be NULL)
2616 *
2617 * This function queues a transaction to the specified process. It will try
2618 * to find a thread in the target process to handle the transaction and
2619 * wake it up. If no thread is found, the work is queued to the proc
2620 * waitqueue.
2621 *
2622 * If the @thread parameter is not NULL, the transaction is always queued
2623 * to the waitlist of that specific thread.
2624 *
2625 * Return: true if the transactions was successfully queued
2626 * false if the target process or thread is dead
2627 */
2628static bool binder_proc_transaction(struct binder_transaction *t,
2629 struct binder_proc *proc,
2630 struct binder_thread *thread)
2631{
Martijn Coenen408c68b2017-08-31 10:04:19 +02002632 struct binder_node *node = t->buffer->target_node;
2633 bool oneway = !!(t->flags & TF_ONE_WAY);
Martijn Coenen148ade22017-11-15 09:21:35 +01002634 bool pending_async = false;
Martijn Coenen408c68b2017-08-31 10:04:19 +02002635
2636 BUG_ON(!node);
2637 binder_node_lock(node);
2638 if (oneway) {
2639 BUG_ON(thread);
2640 if (node->has_async_transaction) {
Martijn Coenen148ade22017-11-15 09:21:35 +01002641 pending_async = true;
Martijn Coenen408c68b2017-08-31 10:04:19 +02002642 } else {
Gustavo A. R. Silva197410a2018-01-23 12:04:27 -06002643 node->has_async_transaction = true;
Martijn Coenen408c68b2017-08-31 10:04:19 +02002644 }
2645 }
2646
2647 binder_inner_proc_lock(proc);
2648
2649 if (proc->is_dead || (thread && thread->is_dead)) {
2650 binder_inner_proc_unlock(proc);
2651 binder_node_unlock(node);
2652 return false;
2653 }
2654
Martijn Coenen148ade22017-11-15 09:21:35 +01002655 if (!thread && !pending_async)
Martijn Coenen408c68b2017-08-31 10:04:19 +02002656 thread = binder_select_thread_ilocked(proc);
2657
2658 if (thread)
Martijn Coenen148ade22017-11-15 09:21:35 +01002659 binder_enqueue_thread_work_ilocked(thread, &t->work);
2660 else if (!pending_async)
2661 binder_enqueue_work_ilocked(&t->work, &proc->todo);
Martijn Coenen408c68b2017-08-31 10:04:19 +02002662 else
Martijn Coenen148ade22017-11-15 09:21:35 +01002663 binder_enqueue_work_ilocked(&t->work, &node->async_todo);
Martijn Coenen408c68b2017-08-31 10:04:19 +02002664
Martijn Coenen148ade22017-11-15 09:21:35 +01002665 if (!pending_async)
Martijn Coenen408c68b2017-08-31 10:04:19 +02002666 binder_wakeup_thread_ilocked(proc, thread, !oneway /* sync */);
2667
2668 binder_inner_proc_unlock(proc);
2669 binder_node_unlock(node);
2670
2671 return true;
2672}
2673
Todd Kjos512cf462017-09-29 15:39:49 -07002674/**
2675 * binder_get_node_refs_for_txn() - Get required refs on node for txn
2676 * @node: struct binder_node for which to get refs
2677 * @proc: returns @node->proc if valid
2678 * @error: if no @proc then returns BR_DEAD_REPLY
2679 *
2680 * User-space normally keeps the node alive when creating a transaction
2681 * since it has a reference to the target. The local strong ref keeps it
2682 * alive if the sending process dies before the target process processes
2683 * the transaction. If the source process is malicious or has a reference
2684 * counting bug, relying on the local strong ref can fail.
2685 *
2686 * Since user-space can cause the local strong ref to go away, we also take
2687 * a tmpref on the node to ensure it survives while we are constructing
2688 * the transaction. We also need a tmpref on the proc while we are
2689 * constructing the transaction, so we take that here as well.
2690 *
2691 * Return: The target_node with refs taken or NULL if no @node->proc is NULL.
2692 * Also sets @proc if valid. If the @node->proc is NULL indicating that the
2693 * target proc has died, @error is set to BR_DEAD_REPLY
2694 */
2695static struct binder_node *binder_get_node_refs_for_txn(
2696 struct binder_node *node,
2697 struct binder_proc **procp,
2698 uint32_t *error)
2699{
2700 struct binder_node *target_node = NULL;
2701
2702 binder_node_inner_lock(node);
2703 if (node->proc) {
2704 target_node = node;
2705 binder_inc_node_nilocked(node, 1, 0, NULL);
2706 binder_inc_node_tmpref_ilocked(node);
2707 node->proc->tmp_ref++;
2708 *procp = node->proc;
2709 } else
2710 *error = BR_DEAD_REPLY;
2711 binder_node_inner_unlock(node);
2712
2713 return target_node;
2714}
2715
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002716static void binder_transaction(struct binder_proc *proc,
2717 struct binder_thread *thread,
Martijn Coenen4bfac802017-02-03 14:40:50 -08002718 struct binder_transaction_data *tr, int reply,
2719 binder_size_t extra_buffers_size)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002720{
Martijn Coenena056af42017-02-03 14:40:49 -08002721 int ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002722 struct binder_transaction *t;
2723 struct binder_work *tcomplete;
Martijn Coenen79802402017-02-03 14:40:51 -08002724 binder_size_t *offp, *off_end, *off_start;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08002725 binder_size_t off_min;
Martijn Coenen79802402017-02-03 14:40:51 -08002726 u8 *sg_bufp, *sg_buf_end;
Todd Kjos7a4408c2017-06-29 12:01:57 -07002727 struct binder_proc *target_proc = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002728 struct binder_thread *target_thread = NULL;
2729 struct binder_node *target_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002730 struct binder_transaction *in_reply_to = NULL;
2731 struct binder_transaction_log_entry *e;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002732 uint32_t return_error = 0;
2733 uint32_t return_error_param = 0;
2734 uint32_t return_error_line = 0;
Martijn Coenen79802402017-02-03 14:40:51 -08002735 struct binder_buffer_object *last_fixup_obj = NULL;
2736 binder_size_t last_fixup_min_off = 0;
Martijn Coenen342e5c92017-02-03 14:40:46 -08002737 struct binder_context *context = proc->context;
Todd Kjosd99c7332017-06-29 12:01:53 -07002738 int t_debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002739
2740 e = binder_transaction_log_add(&binder_transaction_log);
Todd Kjosd99c7332017-06-29 12:01:53 -07002741 e->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002742 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
2743 e->from_proc = proc->pid;
2744 e->from_thread = thread->pid;
2745 e->target_handle = tr->target.handle;
2746 e->data_size = tr->data_size;
2747 e->offsets_size = tr->offsets_size;
Martijn Coenen14db3182017-02-03 14:40:47 -08002748 e->context_name = proc->context->name;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002749
2750 if (reply) {
Martijn Coenen0b89d692017-06-29 12:02:06 -07002751 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002752 in_reply_to = thread->transaction_stack;
2753 if (in_reply_to == NULL) {
Martijn Coenen0b89d692017-06-29 12:02:06 -07002754 binder_inner_proc_unlock(proc);
Anmol Sarma56b468f2012-10-30 22:35:43 +05302755 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002756 proc->pid, thread->pid);
2757 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002758 return_error_param = -EPROTO;
2759 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002760 goto err_empty_call_stack;
2761 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002762 if (in_reply_to->to_thread != thread) {
Todd Kjos7a4408c2017-06-29 12:01:57 -07002763 spin_lock(&in_reply_to->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05302764 binder_user_error("%d:%d got reply transaction with bad transaction stack, transaction %d has target %d:%d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002765 proc->pid, thread->pid, in_reply_to->debug_id,
2766 in_reply_to->to_proc ?
2767 in_reply_to->to_proc->pid : 0,
2768 in_reply_to->to_thread ?
2769 in_reply_to->to_thread->pid : 0);
Todd Kjos7a4408c2017-06-29 12:01:57 -07002770 spin_unlock(&in_reply_to->lock);
Martijn Coenen0b89d692017-06-29 12:02:06 -07002771 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002772 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002773 return_error_param = -EPROTO;
2774 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002775 in_reply_to = NULL;
2776 goto err_bad_call_stack;
2777 }
2778 thread->transaction_stack = in_reply_to->to_parent;
Martijn Coenen0b89d692017-06-29 12:02:06 -07002779 binder_inner_proc_unlock(proc);
2780 binder_set_nice(in_reply_to->saved_priority);
2781 target_thread = binder_get_txn_from_and_acq_inner(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002782 if (target_thread == NULL) {
2783 return_error = BR_DEAD_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002784 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002785 goto err_dead_binder;
2786 }
2787 if (target_thread->transaction_stack != in_reply_to) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302788 binder_user_error("%d:%d got reply transaction with bad target transaction stack %d, expected %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002789 proc->pid, thread->pid,
2790 target_thread->transaction_stack ?
2791 target_thread->transaction_stack->debug_id : 0,
2792 in_reply_to->debug_id);
Martijn Coenen0b89d692017-06-29 12:02:06 -07002793 binder_inner_proc_unlock(target_thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002794 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002795 return_error_param = -EPROTO;
2796 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002797 in_reply_to = NULL;
2798 target_thread = NULL;
2799 goto err_dead_binder;
2800 }
2801 target_proc = target_thread->proc;
Todd Kjos7a4408c2017-06-29 12:01:57 -07002802 target_proc->tmp_ref++;
Martijn Coenen0b89d692017-06-29 12:02:06 -07002803 binder_inner_proc_unlock(target_thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002804 } else {
2805 if (tr->target.handle) {
2806 struct binder_ref *ref;
Seunghun Lee10f62862014-05-01 01:30:23 +09002807
Todd Kjoseb349832017-06-29 12:01:56 -07002808 /*
2809 * There must already be a strong ref
2810 * on this node. If so, do a strong
2811 * increment on the node to ensure it
2812 * stays alive until the transaction is
2813 * done.
2814 */
Todd Kjos2c1838d2017-06-29 12:02:08 -07002815 binder_proc_lock(proc);
2816 ref = binder_get_ref_olocked(proc, tr->target.handle,
2817 true);
Todd Kjoseb349832017-06-29 12:01:56 -07002818 if (ref) {
Todd Kjos512cf462017-09-29 15:39:49 -07002819 target_node = binder_get_node_refs_for_txn(
2820 ref->node, &target_proc,
2821 &return_error);
2822 } else {
2823 binder_user_error("%d:%d got transaction to invalid handle\n",
2824 proc->pid, thread->pid);
2825 return_error = BR_FAILED_REPLY;
Todd Kjoseb349832017-06-29 12:01:56 -07002826 }
Todd Kjos2c1838d2017-06-29 12:02:08 -07002827 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002828 } else {
Todd Kjosc44b1232017-06-29 12:01:43 -07002829 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen342e5c92017-02-03 14:40:46 -08002830 target_node = context->binder_context_mgr_node;
Todd Kjos512cf462017-09-29 15:39:49 -07002831 if (target_node)
2832 target_node = binder_get_node_refs_for_txn(
2833 target_node, &target_proc,
2834 &return_error);
2835 else
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002836 return_error = BR_DEAD_REPLY;
Todd Kjosc44b1232017-06-29 12:01:43 -07002837 mutex_unlock(&context->context_mgr_node_lock);
Martijn Coenen7aa135f2018-03-28 11:14:50 +02002838 if (target_node && target_proc == proc) {
2839 binder_user_error("%d:%d got transaction to context manager from process owning it\n",
2840 proc->pid, thread->pid);
2841 return_error = BR_FAILED_REPLY;
2842 return_error_param = -EINVAL;
2843 return_error_line = __LINE__;
2844 goto err_invalid_target_handle;
2845 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002846 }
Todd Kjos512cf462017-09-29 15:39:49 -07002847 if (!target_node) {
2848 /*
2849 * return_error is set above
2850 */
2851 return_error_param = -EINVAL;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002852 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002853 goto err_dead_binder;
2854 }
Todd Kjos512cf462017-09-29 15:39:49 -07002855 e->to_node = target_node->debug_id;
Stephen Smalley79af7302015-01-21 10:54:10 -05002856 if (security_binder_transaction(proc->tsk,
2857 target_proc->tsk) < 0) {
2858 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002859 return_error_param = -EPERM;
2860 return_error_line = __LINE__;
Stephen Smalley79af7302015-01-21 10:54:10 -05002861 goto err_invalid_target_handle;
2862 }
Martijn Coenen0b89d692017-06-29 12:02:06 -07002863 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002864 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
2865 struct binder_transaction *tmp;
Seunghun Lee10f62862014-05-01 01:30:23 +09002866
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002867 tmp = thread->transaction_stack;
2868 if (tmp->to_thread != thread) {
Todd Kjos7a4408c2017-06-29 12:01:57 -07002869 spin_lock(&tmp->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05302870 binder_user_error("%d:%d got new transaction with bad transaction stack, transaction %d has target %d:%d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002871 proc->pid, thread->pid, tmp->debug_id,
2872 tmp->to_proc ? tmp->to_proc->pid : 0,
2873 tmp->to_thread ?
2874 tmp->to_thread->pid : 0);
Todd Kjos7a4408c2017-06-29 12:01:57 -07002875 spin_unlock(&tmp->lock);
Martijn Coenen0b89d692017-06-29 12:02:06 -07002876 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002877 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002878 return_error_param = -EPROTO;
2879 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002880 goto err_bad_call_stack;
2881 }
2882 while (tmp) {
Todd Kjos7a4408c2017-06-29 12:01:57 -07002883 struct binder_thread *from;
2884
2885 spin_lock(&tmp->lock);
2886 from = tmp->from;
2887 if (from && from->proc == target_proc) {
2888 atomic_inc(&from->tmp_ref);
2889 target_thread = from;
2890 spin_unlock(&tmp->lock);
2891 break;
2892 }
2893 spin_unlock(&tmp->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002894 tmp = tmp->from_parent;
2895 }
2896 }
Martijn Coenen0b89d692017-06-29 12:02:06 -07002897 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002898 }
Martijn Coenen408c68b2017-08-31 10:04:19 +02002899 if (target_thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002900 e->to_thread = target_thread->pid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002901 e->to_proc = target_proc->pid;
2902
2903 /* TODO: reuse incoming transaction for reply */
2904 t = kzalloc(sizeof(*t), GFP_KERNEL);
2905 if (t == NULL) {
2906 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002907 return_error_param = -ENOMEM;
2908 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002909 goto err_alloc_t_failed;
2910 }
2911 binder_stats_created(BINDER_STAT_TRANSACTION);
Todd Kjos7a4408c2017-06-29 12:01:57 -07002912 spin_lock_init(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002913
2914 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
2915 if (tcomplete == NULL) {
2916 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002917 return_error_param = -ENOMEM;
2918 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002919 goto err_alloc_tcomplete_failed;
2920 }
2921 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
2922
Todd Kjosd99c7332017-06-29 12:01:53 -07002923 t->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002924
2925 if (reply)
2926 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen4bfac802017-02-03 14:40:50 -08002927 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002928 proc->pid, thread->pid, t->debug_id,
2929 target_proc->pid, target_thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002930 (u64)tr->data.ptr.buffer,
2931 (u64)tr->data.ptr.offsets,
Martijn Coenen4bfac802017-02-03 14:40:50 -08002932 (u64)tr->data_size, (u64)tr->offsets_size,
2933 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002934 else
2935 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen4bfac802017-02-03 14:40:50 -08002936 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002937 proc->pid, thread->pid, t->debug_id,
2938 target_proc->pid, target_node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002939 (u64)tr->data.ptr.buffer,
2940 (u64)tr->data.ptr.offsets,
Martijn Coenen4bfac802017-02-03 14:40:50 -08002941 (u64)tr->data_size, (u64)tr->offsets_size,
2942 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002943
2944 if (!reply && !(tr->flags & TF_ONE_WAY))
2945 t->from = thread;
2946 else
2947 t->from = NULL;
Tair Rzayev57bab7c2014-05-31 22:43:34 +03002948 t->sender_euid = task_euid(proc->tsk);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002949 t->to_proc = target_proc;
2950 t->to_thread = target_thread;
2951 t->code = tr->code;
2952 t->flags = tr->flags;
2953 t->priority = task_nice(current);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002954
2955 trace_binder_transaction(reply, t, target_node);
2956
Todd Kjos19c98722017-06-29 12:01:40 -07002957 t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size,
Martijn Coenen4bfac802017-02-03 14:40:50 -08002958 tr->offsets_size, extra_buffers_size,
2959 !reply && (t->flags & TF_ONE_WAY));
Todd Kjos57ada2f2017-06-29 12:01:46 -07002960 if (IS_ERR(t->buffer)) {
2961 /*
2962 * -ESRCH indicates VMA cleared. The target is dying.
2963 */
2964 return_error_param = PTR_ERR(t->buffer);
2965 return_error = return_error_param == -ESRCH ?
2966 BR_DEAD_REPLY : BR_FAILED_REPLY;
2967 return_error_line = __LINE__;
2968 t->buffer = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002969 goto err_binder_alloc_buf_failed;
2970 }
2971 t->buffer->allow_user_free = 0;
2972 t->buffer->debug_id = t->debug_id;
2973 t->buffer->transaction = t;
2974 t->buffer->target_node = target_node;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002975 trace_binder_transaction_alloc_buf(t->buffer);
Martijn Coenen79802402017-02-03 14:40:51 -08002976 off_start = (binder_size_t *)(t->buffer->data +
2977 ALIGN(tr->data_size, sizeof(void *)));
2978 offp = off_start;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002979
Arve Hjønnevågda498892014-02-21 14:40:26 -08002980 if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t)
2981 tr->data.ptr.buffer, tr->data_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302982 binder_user_error("%d:%d got transaction with invalid data ptr\n",
2983 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002984 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002985 return_error_param = -EFAULT;
2986 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002987 goto err_copy_data_failed;
2988 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08002989 if (copy_from_user(offp, (const void __user *)(uintptr_t)
2990 tr->data.ptr.offsets, tr->offsets_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302991 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
2992 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002993 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002994 return_error_param = -EFAULT;
2995 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002996 goto err_copy_data_failed;
2997 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08002998 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
2999 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
3000 proc->pid, thread->pid, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003001 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003002 return_error_param = -EINVAL;
3003 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003004 goto err_bad_offset;
3005 }
Martijn Coenen79802402017-02-03 14:40:51 -08003006 if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) {
3007 binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
3008 proc->pid, thread->pid,
3009 (u64)extra_buffers_size);
3010 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003011 return_error_param = -EINVAL;
3012 return_error_line = __LINE__;
Martijn Coenen79802402017-02-03 14:40:51 -08003013 goto err_bad_offset;
3014 }
3015 off_end = (void *)off_start + tr->offsets_size;
3016 sg_bufp = (u8 *)(PTR_ALIGN(off_end, sizeof(void *)));
3017 sg_buf_end = sg_bufp + extra_buffers_size;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08003018 off_min = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003019 for (; offp < off_end; offp++) {
Martijn Coenenfeba3902017-02-03 14:40:45 -08003020 struct binder_object_header *hdr;
3021 size_t object_size = binder_validate_object(t->buffer, *offp);
Seunghun Lee10f62862014-05-01 01:30:23 +09003022
Martijn Coenenfeba3902017-02-03 14:40:45 -08003023 if (object_size == 0 || *offp < off_min) {
3024 binder_user_error("%d:%d got transaction with invalid offset (%lld, min %lld max %lld) or object.\n",
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08003025 proc->pid, thread->pid, (u64)*offp,
3026 (u64)off_min,
Martijn Coenenfeba3902017-02-03 14:40:45 -08003027 (u64)t->buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003028 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003029 return_error_param = -EINVAL;
3030 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003031 goto err_bad_offset;
3032 }
Martijn Coenenfeba3902017-02-03 14:40:45 -08003033
3034 hdr = (struct binder_object_header *)(t->buffer->data + *offp);
3035 off_min = *offp + object_size;
3036 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003037 case BINDER_TYPE_BINDER:
3038 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08003039 struct flat_binder_object *fp;
Seunghun Lee10f62862014-05-01 01:30:23 +09003040
Martijn Coenenfeba3902017-02-03 14:40:45 -08003041 fp = to_flat_binder_object(hdr);
Martijn Coenena056af42017-02-03 14:40:49 -08003042 ret = binder_translate_binder(fp, t, thread);
3043 if (ret < 0) {
Christian Engelmayer7d420432014-05-07 21:44:53 +02003044 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003045 return_error_param = ret;
3046 return_error_line = __LINE__;
Martijn Coenena056af42017-02-03 14:40:49 -08003047 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003048 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003049 } break;
3050 case BINDER_TYPE_HANDLE:
3051 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08003052 struct flat_binder_object *fp;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02003053
Martijn Coenenfeba3902017-02-03 14:40:45 -08003054 fp = to_flat_binder_object(hdr);
Martijn Coenena056af42017-02-03 14:40:49 -08003055 ret = binder_translate_handle(fp, t, thread);
3056 if (ret < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003057 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003058 return_error_param = ret;
3059 return_error_line = __LINE__;
Martijn Coenena056af42017-02-03 14:40:49 -08003060 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003061 }
3062 } break;
3063
3064 case BINDER_TYPE_FD: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08003065 struct binder_fd_object *fp = to_binder_fd_object(hdr);
Martijn Coenena056af42017-02-03 14:40:49 -08003066 int target_fd = binder_translate_fd(fp->fd, t, thread,
3067 in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003068
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003069 if (target_fd < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003070 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003071 return_error_param = target_fd;
3072 return_error_line = __LINE__;
Martijn Coenena056af42017-02-03 14:40:49 -08003073 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003074 }
Martijn Coenenfeba3902017-02-03 14:40:45 -08003075 fp->pad_binder = 0;
3076 fp->fd = target_fd;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003077 } break;
Martijn Coenendef95c72017-02-03 14:40:52 -08003078 case BINDER_TYPE_FDA: {
3079 struct binder_fd_array_object *fda =
3080 to_binder_fd_array_object(hdr);
3081 struct binder_buffer_object *parent =
3082 binder_validate_ptr(t->buffer, fda->parent,
3083 off_start,
3084 offp - off_start);
3085 if (!parent) {
3086 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
3087 proc->pid, thread->pid);
3088 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003089 return_error_param = -EINVAL;
3090 return_error_line = __LINE__;
Martijn Coenendef95c72017-02-03 14:40:52 -08003091 goto err_bad_parent;
3092 }
3093 if (!binder_validate_fixup(t->buffer, off_start,
3094 parent, fda->parent_offset,
3095 last_fixup_obj,
3096 last_fixup_min_off)) {
3097 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
3098 proc->pid, thread->pid);
3099 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003100 return_error_param = -EINVAL;
3101 return_error_line = __LINE__;
Martijn Coenendef95c72017-02-03 14:40:52 -08003102 goto err_bad_parent;
3103 }
3104 ret = binder_translate_fd_array(fda, parent, t, thread,
3105 in_reply_to);
3106 if (ret < 0) {
3107 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003108 return_error_param = ret;
3109 return_error_line = __LINE__;
Martijn Coenendef95c72017-02-03 14:40:52 -08003110 goto err_translate_failed;
3111 }
3112 last_fixup_obj = parent;
3113 last_fixup_min_off =
3114 fda->parent_offset + sizeof(u32) * fda->num_fds;
3115 } break;
Martijn Coenen79802402017-02-03 14:40:51 -08003116 case BINDER_TYPE_PTR: {
3117 struct binder_buffer_object *bp =
3118 to_binder_buffer_object(hdr);
3119 size_t buf_left = sg_buf_end - sg_bufp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003120
Martijn Coenen79802402017-02-03 14:40:51 -08003121 if (bp->length > buf_left) {
3122 binder_user_error("%d:%d got transaction with too large buffer\n",
3123 proc->pid, thread->pid);
3124 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003125 return_error_param = -EINVAL;
3126 return_error_line = __LINE__;
Martijn Coenen79802402017-02-03 14:40:51 -08003127 goto err_bad_offset;
3128 }
3129 if (copy_from_user(sg_bufp,
3130 (const void __user *)(uintptr_t)
3131 bp->buffer, bp->length)) {
3132 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3133 proc->pid, thread->pid);
Todd Kjos57ada2f2017-06-29 12:01:46 -07003134 return_error_param = -EFAULT;
Martijn Coenen79802402017-02-03 14:40:51 -08003135 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003136 return_error_line = __LINE__;
Martijn Coenen79802402017-02-03 14:40:51 -08003137 goto err_copy_data_failed;
3138 }
3139 /* Fixup buffer pointer to target proc address space */
3140 bp->buffer = (uintptr_t)sg_bufp +
Todd Kjos19c98722017-06-29 12:01:40 -07003141 binder_alloc_get_user_buffer_offset(
3142 &target_proc->alloc);
Martijn Coenen79802402017-02-03 14:40:51 -08003143 sg_bufp += ALIGN(bp->length, sizeof(u64));
3144
3145 ret = binder_fixup_parent(t, thread, bp, off_start,
3146 offp - off_start,
3147 last_fixup_obj,
3148 last_fixup_min_off);
3149 if (ret < 0) {
3150 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003151 return_error_param = ret;
3152 return_error_line = __LINE__;
Martijn Coenen79802402017-02-03 14:40:51 -08003153 goto err_translate_failed;
3154 }
3155 last_fixup_obj = bp;
3156 last_fixup_min_off = 0;
3157 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003158 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01003159 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
Martijn Coenenfeba3902017-02-03 14:40:45 -08003160 proc->pid, thread->pid, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003161 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003162 return_error_param = -EINVAL;
3163 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003164 goto err_bad_object_type;
3165 }
3166 }
Todd Kjosccae6f62017-06-29 12:01:48 -07003167 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
Todd Kjos673068e2017-06-29 12:02:03 -07003168 t->work.type = BINDER_WORK_TRANSACTION;
Todd Kjosccae6f62017-06-29 12:01:48 -07003169
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003170 if (reply) {
Martijn Coenen148ade22017-11-15 09:21:35 +01003171 binder_enqueue_thread_work(thread, tcomplete);
Martijn Coenen0b89d692017-06-29 12:02:06 -07003172 binder_inner_proc_lock(target_proc);
3173 if (target_thread->is_dead) {
3174 binder_inner_proc_unlock(target_proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07003175 goto err_dead_proc_or_thread;
Martijn Coenen0b89d692017-06-29 12:02:06 -07003176 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003177 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen0b89d692017-06-29 12:02:06 -07003178 binder_pop_transaction_ilocked(target_thread, in_reply_to);
Martijn Coenen148ade22017-11-15 09:21:35 +01003179 binder_enqueue_thread_work_ilocked(target_thread, &t->work);
Martijn Coenen0b89d692017-06-29 12:02:06 -07003180 binder_inner_proc_unlock(target_proc);
Martijn Coenen408c68b2017-08-31 10:04:19 +02003181 wake_up_interruptible_sync(&target_thread->wait);
Todd Kjosb6d282c2017-06-29 12:01:54 -07003182 binder_free_transaction(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003183 } else if (!(t->flags & TF_ONE_WAY)) {
3184 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen0b89d692017-06-29 12:02:06 -07003185 binder_inner_proc_lock(proc);
Martijn Coenen148ade22017-11-15 09:21:35 +01003186 /*
3187 * Defer the TRANSACTION_COMPLETE, so we don't return to
3188 * userspace immediately; this allows the target process to
3189 * immediately start processing this transaction, reducing
3190 * latency. We will then return the TRANSACTION_COMPLETE when
3191 * the target replies (or there is an error).
3192 */
3193 binder_enqueue_deferred_thread_work_ilocked(thread, tcomplete);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003194 t->need_reply = 1;
3195 t->from_parent = thread->transaction_stack;
3196 thread->transaction_stack = t;
Martijn Coenen0b89d692017-06-29 12:02:06 -07003197 binder_inner_proc_unlock(proc);
Martijn Coenen408c68b2017-08-31 10:04:19 +02003198 if (!binder_proc_transaction(t, target_proc, target_thread)) {
Martijn Coenen0b89d692017-06-29 12:02:06 -07003199 binder_inner_proc_lock(proc);
3200 binder_pop_transaction_ilocked(thread, t);
3201 binder_inner_proc_unlock(proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07003202 goto err_dead_proc_or_thread;
3203 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003204 } else {
3205 BUG_ON(target_node == NULL);
3206 BUG_ON(t->buffer->async_transaction != 1);
Martijn Coenen148ade22017-11-15 09:21:35 +01003207 binder_enqueue_thread_work(thread, tcomplete);
Martijn Coenen408c68b2017-08-31 10:04:19 +02003208 if (!binder_proc_transaction(t, target_proc, NULL))
Todd Kjos7a4408c2017-06-29 12:01:57 -07003209 goto err_dead_proc_or_thread;
Riley Andrews00b40d62017-06-29 12:01:37 -07003210 }
Todd Kjos7a4408c2017-06-29 12:01:57 -07003211 if (target_thread)
3212 binder_thread_dec_tmpref(target_thread);
3213 binder_proc_dec_tmpref(target_proc);
Todd Kjos512cf462017-09-29 15:39:49 -07003214 if (target_node)
3215 binder_dec_node_tmpref(target_node);
Todd Kjosd99c7332017-06-29 12:01:53 -07003216 /*
3217 * write barrier to synchronize with initialization
3218 * of log entry
3219 */
3220 smp_wmb();
3221 WRITE_ONCE(e->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003222 return;
3223
Todd Kjos7a4408c2017-06-29 12:01:57 -07003224err_dead_proc_or_thread:
3225 return_error = BR_DEAD_REPLY;
3226 return_error_line = __LINE__;
Xu YiPingd53bebd2017-09-05 10:21:52 -07003227 binder_dequeue_work(proc, tcomplete);
Martijn Coenena056af42017-02-03 14:40:49 -08003228err_translate_failed:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003229err_bad_object_type:
3230err_bad_offset:
Martijn Coenendef95c72017-02-03 14:40:52 -08003231err_bad_parent:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003232err_copy_data_failed:
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003233 trace_binder_transaction_failed_buffer_release(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003234 binder_transaction_buffer_release(target_proc, t->buffer, offp);
Todd Kjos512cf462017-09-29 15:39:49 -07003235 if (target_node)
3236 binder_dec_node_tmpref(target_node);
Todd Kjoseb349832017-06-29 12:01:56 -07003237 target_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003238 t->buffer->transaction = NULL;
Todd Kjos19c98722017-06-29 12:01:40 -07003239 binder_alloc_free_buf(&target_proc->alloc, t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003240err_binder_alloc_buf_failed:
3241 kfree(tcomplete);
3242 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3243err_alloc_tcomplete_failed:
3244 kfree(t);
3245 binder_stats_deleted(BINDER_STAT_TRANSACTION);
3246err_alloc_t_failed:
3247err_bad_call_stack:
3248err_empty_call_stack:
3249err_dead_binder:
3250err_invalid_target_handle:
Todd Kjos7a4408c2017-06-29 12:01:57 -07003251 if (target_thread)
3252 binder_thread_dec_tmpref(target_thread);
3253 if (target_proc)
3254 binder_proc_dec_tmpref(target_proc);
Todd Kjos512cf462017-09-29 15:39:49 -07003255 if (target_node) {
Todd Kjoseb349832017-06-29 12:01:56 -07003256 binder_dec_node(target_node, 1, 0);
Todd Kjos512cf462017-09-29 15:39:49 -07003257 binder_dec_node_tmpref(target_node);
3258 }
Todd Kjoseb349832017-06-29 12:01:56 -07003259
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003260 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Todd Kjos57ada2f2017-06-29 12:01:46 -07003261 "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
3262 proc->pid, thread->pid, return_error, return_error_param,
3263 (u64)tr->data_size, (u64)tr->offsets_size,
3264 return_error_line);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003265
3266 {
3267 struct binder_transaction_log_entry *fe;
Seunghun Lee10f62862014-05-01 01:30:23 +09003268
Todd Kjos57ada2f2017-06-29 12:01:46 -07003269 e->return_error = return_error;
3270 e->return_error_param = return_error_param;
3271 e->return_error_line = return_error_line;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003272 fe = binder_transaction_log_add(&binder_transaction_log_failed);
3273 *fe = *e;
Todd Kjosd99c7332017-06-29 12:01:53 -07003274 /*
3275 * write barrier to synchronize with initialization
3276 * of log entry
3277 */
3278 smp_wmb();
3279 WRITE_ONCE(e->debug_id_done, t_debug_id);
3280 WRITE_ONCE(fe->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003281 }
3282
Todd Kjos26549d12017-06-29 12:01:55 -07003283 BUG_ON(thread->return_error.cmd != BR_OK);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003284 if (in_reply_to) {
Todd Kjos26549d12017-06-29 12:01:55 -07003285 thread->return_error.cmd = BR_TRANSACTION_COMPLETE;
Martijn Coenen148ade22017-11-15 09:21:35 +01003286 binder_enqueue_thread_work(thread, &thread->return_error.work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003287 binder_send_failed_reply(in_reply_to, return_error);
Todd Kjos26549d12017-06-29 12:01:55 -07003288 } else {
3289 thread->return_error.cmd = return_error;
Martijn Coenen148ade22017-11-15 09:21:35 +01003290 binder_enqueue_thread_work(thread, &thread->return_error.work);
Todd Kjos26549d12017-06-29 12:01:55 -07003291 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003292}
3293
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02003294static int binder_thread_write(struct binder_proc *proc,
3295 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003296 binder_uintptr_t binder_buffer, size_t size,
3297 binder_size_t *consumed)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003298{
3299 uint32_t cmd;
Martijn Coenen342e5c92017-02-03 14:40:46 -08003300 struct binder_context *context = proc->context;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003301 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003302 void __user *ptr = buffer + *consumed;
3303 void __user *end = buffer + size;
3304
Todd Kjos26549d12017-06-29 12:01:55 -07003305 while (ptr < end && thread->return_error.cmd == BR_OK) {
Todd Kjos372e3142017-06-29 12:01:58 -07003306 int ret;
3307
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003308 if (get_user(cmd, (uint32_t __user *)ptr))
3309 return -EFAULT;
3310 ptr += sizeof(uint32_t);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003311 trace_binder_command(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003312 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07003313 atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]);
3314 atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]);
3315 atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003316 }
3317 switch (cmd) {
3318 case BC_INCREFS:
3319 case BC_ACQUIRE:
3320 case BC_RELEASE:
3321 case BC_DECREFS: {
3322 uint32_t target;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003323 const char *debug_string;
Todd Kjos372e3142017-06-29 12:01:58 -07003324 bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE;
3325 bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE;
3326 struct binder_ref_data rdata;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003327
3328 if (get_user(target, (uint32_t __user *)ptr))
3329 return -EFAULT;
Todd Kjosc44b1232017-06-29 12:01:43 -07003330
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003331 ptr += sizeof(uint32_t);
Todd Kjos372e3142017-06-29 12:01:58 -07003332 ret = -1;
3333 if (increment && !target) {
Todd Kjosc44b1232017-06-29 12:01:43 -07003334 struct binder_node *ctx_mgr_node;
Todd Kjosc44b1232017-06-29 12:01:43 -07003335 mutex_lock(&context->context_mgr_node_lock);
3336 ctx_mgr_node = context->binder_context_mgr_node;
Todd Kjos372e3142017-06-29 12:01:58 -07003337 if (ctx_mgr_node)
3338 ret = binder_inc_ref_for_node(
3339 proc, ctx_mgr_node,
3340 strong, NULL, &rdata);
Todd Kjosc44b1232017-06-29 12:01:43 -07003341 mutex_unlock(&context->context_mgr_node_lock);
3342 }
Todd Kjos372e3142017-06-29 12:01:58 -07003343 if (ret)
3344 ret = binder_update_ref_for_handle(
3345 proc, target, increment, strong,
3346 &rdata);
3347 if (!ret && rdata.desc != target) {
3348 binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
3349 proc->pid, thread->pid,
3350 target, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003351 }
3352 switch (cmd) {
3353 case BC_INCREFS:
3354 debug_string = "IncRefs";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003355 break;
3356 case BC_ACQUIRE:
3357 debug_string = "Acquire";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003358 break;
3359 case BC_RELEASE:
3360 debug_string = "Release";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003361 break;
3362 case BC_DECREFS:
3363 default:
3364 debug_string = "DecRefs";
Todd Kjos372e3142017-06-29 12:01:58 -07003365 break;
3366 }
3367 if (ret) {
3368 binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
3369 proc->pid, thread->pid, debug_string,
3370 strong, target, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003371 break;
3372 }
3373 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjos372e3142017-06-29 12:01:58 -07003374 "%d:%d %s ref %d desc %d s %d w %d\n",
3375 proc->pid, thread->pid, debug_string,
3376 rdata.debug_id, rdata.desc, rdata.strong,
3377 rdata.weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003378 break;
3379 }
3380 case BC_INCREFS_DONE:
3381 case BC_ACQUIRE_DONE: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003382 binder_uintptr_t node_ptr;
3383 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003384 struct binder_node *node;
Todd Kjos673068e2017-06-29 12:02:03 -07003385 bool free_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003386
Arve Hjønnevågda498892014-02-21 14:40:26 -08003387 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003388 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003389 ptr += sizeof(binder_uintptr_t);
3390 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003391 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003392 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003393 node = binder_get_node(proc, node_ptr);
3394 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003395 binder_user_error("%d:%d %s u%016llx no match\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003396 proc->pid, thread->pid,
3397 cmd == BC_INCREFS_DONE ?
3398 "BC_INCREFS_DONE" :
3399 "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003400 (u64)node_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003401 break;
3402 }
3403 if (cookie != node->cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003404 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003405 proc->pid, thread->pid,
3406 cmd == BC_INCREFS_DONE ?
3407 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003408 (u64)node_ptr, node->debug_id,
3409 (u64)cookie, (u64)node->cookie);
Todd Kjosadc18842017-06-29 12:01:59 -07003410 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003411 break;
3412 }
Todd Kjos673068e2017-06-29 12:02:03 -07003413 binder_node_inner_lock(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003414 if (cmd == BC_ACQUIRE_DONE) {
3415 if (node->pending_strong_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303416 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003417 proc->pid, thread->pid,
3418 node->debug_id);
Todd Kjos673068e2017-06-29 12:02:03 -07003419 binder_node_inner_unlock(node);
Todd Kjosadc18842017-06-29 12:01:59 -07003420 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003421 break;
3422 }
3423 node->pending_strong_ref = 0;
3424 } else {
3425 if (node->pending_weak_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303426 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003427 proc->pid, thread->pid,
3428 node->debug_id);
Todd Kjos673068e2017-06-29 12:02:03 -07003429 binder_node_inner_unlock(node);
Todd Kjosadc18842017-06-29 12:01:59 -07003430 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003431 break;
3432 }
3433 node->pending_weak_ref = 0;
3434 }
Todd Kjos673068e2017-06-29 12:02:03 -07003435 free_node = binder_dec_node_nilocked(node,
3436 cmd == BC_ACQUIRE_DONE, 0);
3437 WARN_ON(free_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003438 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjosadc18842017-06-29 12:01:59 -07003439 "%d:%d %s node %d ls %d lw %d tr %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003440 proc->pid, thread->pid,
3441 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Todd Kjosadc18842017-06-29 12:01:59 -07003442 node->debug_id, node->local_strong_refs,
3443 node->local_weak_refs, node->tmp_refs);
Todd Kjos673068e2017-06-29 12:02:03 -07003444 binder_node_inner_unlock(node);
Todd Kjosadc18842017-06-29 12:01:59 -07003445 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003446 break;
3447 }
3448 case BC_ATTEMPT_ACQUIRE:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303449 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003450 return -EINVAL;
3451 case BC_ACQUIRE_RESULT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303452 pr_err("BC_ACQUIRE_RESULT not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003453 return -EINVAL;
3454
3455 case BC_FREE_BUFFER: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003456 binder_uintptr_t data_ptr;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003457 struct binder_buffer *buffer;
3458
Arve Hjønnevågda498892014-02-21 14:40:26 -08003459 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003460 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003461 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003462
Todd Kjos53d311cf2017-06-29 12:01:51 -07003463 buffer = binder_alloc_prepare_to_free(&proc->alloc,
3464 data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003465 if (buffer == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003466 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
3467 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003468 break;
3469 }
3470 if (!buffer->allow_user_free) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003471 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
3472 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003473 break;
3474 }
3475 binder_debug(BINDER_DEBUG_FREE_BUFFER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003476 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
3477 proc->pid, thread->pid, (u64)data_ptr,
3478 buffer->debug_id,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003479 buffer->transaction ? "active" : "finished");
3480
3481 if (buffer->transaction) {
3482 buffer->transaction->buffer = NULL;
3483 buffer->transaction = NULL;
3484 }
3485 if (buffer->async_transaction && buffer->target_node) {
Todd Kjos72196392017-06-29 12:02:02 -07003486 struct binder_node *buf_node;
3487 struct binder_work *w;
3488
3489 buf_node = buffer->target_node;
Todd Kjos673068e2017-06-29 12:02:03 -07003490 binder_node_inner_lock(buf_node);
Todd Kjos72196392017-06-29 12:02:02 -07003491 BUG_ON(!buf_node->has_async_transaction);
3492 BUG_ON(buf_node->proc != proc);
Todd Kjos72196392017-06-29 12:02:02 -07003493 w = binder_dequeue_work_head_ilocked(
3494 &buf_node->async_todo);
Martijn Coenen3a6430c2017-08-31 10:04:29 +02003495 if (!w) {
Gustavo A. R. Silva197410a2018-01-23 12:04:27 -06003496 buf_node->has_async_transaction = false;
Martijn Coenen3a6430c2017-08-31 10:04:29 +02003497 } else {
Todd Kjos72196392017-06-29 12:02:02 -07003498 binder_enqueue_work_ilocked(
Martijn Coenen3a6430c2017-08-31 10:04:29 +02003499 w, &proc->todo);
3500 binder_wakeup_proc_ilocked(proc);
3501 }
Todd Kjos673068e2017-06-29 12:02:03 -07003502 binder_node_inner_unlock(buf_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003503 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003504 trace_binder_transaction_buffer_release(buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003505 binder_transaction_buffer_release(proc, buffer, NULL);
Todd Kjos19c98722017-06-29 12:01:40 -07003506 binder_alloc_free_buf(&proc->alloc, buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003507 break;
3508 }
3509
Martijn Coenen79802402017-02-03 14:40:51 -08003510 case BC_TRANSACTION_SG:
3511 case BC_REPLY_SG: {
3512 struct binder_transaction_data_sg tr;
3513
3514 if (copy_from_user(&tr, ptr, sizeof(tr)))
3515 return -EFAULT;
3516 ptr += sizeof(tr);
3517 binder_transaction(proc, thread, &tr.transaction_data,
3518 cmd == BC_REPLY_SG, tr.buffers_size);
3519 break;
3520 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003521 case BC_TRANSACTION:
3522 case BC_REPLY: {
3523 struct binder_transaction_data tr;
3524
3525 if (copy_from_user(&tr, ptr, sizeof(tr)))
3526 return -EFAULT;
3527 ptr += sizeof(tr);
Martijn Coenen4bfac802017-02-03 14:40:50 -08003528 binder_transaction(proc, thread, &tr,
3529 cmd == BC_REPLY, 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003530 break;
3531 }
3532
3533 case BC_REGISTER_LOOPER:
3534 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303535 "%d:%d BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003536 proc->pid, thread->pid);
Todd Kjosb3e68612017-06-29 12:02:07 -07003537 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003538 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
3539 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303540 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003541 proc->pid, thread->pid);
3542 } else if (proc->requested_threads == 0) {
3543 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303544 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003545 proc->pid, thread->pid);
3546 } else {
3547 proc->requested_threads--;
3548 proc->requested_threads_started++;
3549 }
3550 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
Todd Kjosb3e68612017-06-29 12:02:07 -07003551 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003552 break;
3553 case BC_ENTER_LOOPER:
3554 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303555 "%d:%d BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003556 proc->pid, thread->pid);
3557 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
3558 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303559 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003560 proc->pid, thread->pid);
3561 }
3562 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
3563 break;
3564 case BC_EXIT_LOOPER:
3565 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303566 "%d:%d BC_EXIT_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003567 proc->pid, thread->pid);
3568 thread->looper |= BINDER_LOOPER_STATE_EXITED;
3569 break;
3570
3571 case BC_REQUEST_DEATH_NOTIFICATION:
3572 case BC_CLEAR_DEATH_NOTIFICATION: {
3573 uint32_t target;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003574 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003575 struct binder_ref *ref;
Todd Kjos2c1838d2017-06-29 12:02:08 -07003576 struct binder_ref_death *death = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003577
3578 if (get_user(target, (uint32_t __user *)ptr))
3579 return -EFAULT;
3580 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08003581 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003582 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003583 ptr += sizeof(binder_uintptr_t);
Todd Kjos2c1838d2017-06-29 12:02:08 -07003584 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3585 /*
3586 * Allocate memory for death notification
3587 * before taking lock
3588 */
3589 death = kzalloc(sizeof(*death), GFP_KERNEL);
3590 if (death == NULL) {
3591 WARN_ON(thread->return_error.cmd !=
3592 BR_OK);
3593 thread->return_error.cmd = BR_ERROR;
Martijn Coenen148ade22017-11-15 09:21:35 +01003594 binder_enqueue_thread_work(
3595 thread,
3596 &thread->return_error.work);
Todd Kjos2c1838d2017-06-29 12:02:08 -07003597 binder_debug(
3598 BINDER_DEBUG_FAILED_TRANSACTION,
3599 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
3600 proc->pid, thread->pid);
3601 break;
3602 }
3603 }
3604 binder_proc_lock(proc);
3605 ref = binder_get_ref_olocked(proc, target, false);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003606 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303607 binder_user_error("%d:%d %s invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003608 proc->pid, thread->pid,
3609 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3610 "BC_REQUEST_DEATH_NOTIFICATION" :
3611 "BC_CLEAR_DEATH_NOTIFICATION",
3612 target);
Todd Kjos2c1838d2017-06-29 12:02:08 -07003613 binder_proc_unlock(proc);
3614 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003615 break;
3616 }
3617
3618 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003619 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003620 proc->pid, thread->pid,
3621 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3622 "BC_REQUEST_DEATH_NOTIFICATION" :
3623 "BC_CLEAR_DEATH_NOTIFICATION",
Todd Kjos372e3142017-06-29 12:01:58 -07003624 (u64)cookie, ref->data.debug_id,
3625 ref->data.desc, ref->data.strong,
3626 ref->data.weak, ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003627
Martijn Coenenab51ec62017-06-29 12:02:10 -07003628 binder_node_lock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003629 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3630 if (ref->death) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303631 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003632 proc->pid, thread->pid);
Martijn Coenenab51ec62017-06-29 12:02:10 -07003633 binder_node_unlock(ref->node);
Todd Kjos2c1838d2017-06-29 12:02:08 -07003634 binder_proc_unlock(proc);
3635 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003636 break;
3637 }
3638 binder_stats_created(BINDER_STAT_DEATH);
3639 INIT_LIST_HEAD(&death->work.entry);
3640 death->cookie = cookie;
3641 ref->death = death;
3642 if (ref->node->proc == NULL) {
3643 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
Martijn Coenenbb745622017-08-31 10:04:28 +02003644
3645 binder_inner_proc_lock(proc);
3646 binder_enqueue_work_ilocked(
3647 &ref->death->work, &proc->todo);
3648 binder_wakeup_proc_ilocked(proc);
3649 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003650 }
3651 } else {
3652 if (ref->death == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303653 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003654 proc->pid, thread->pid);
Todd Kjos673068e2017-06-29 12:02:03 -07003655 binder_node_unlock(ref->node);
Todd Kjos2c1838d2017-06-29 12:02:08 -07003656 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003657 break;
3658 }
3659 death = ref->death;
3660 if (death->cookie != cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003661 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003662 proc->pid, thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003663 (u64)death->cookie,
3664 (u64)cookie);
Todd Kjos673068e2017-06-29 12:02:03 -07003665 binder_node_unlock(ref->node);
Todd Kjos2c1838d2017-06-29 12:02:08 -07003666 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003667 break;
3668 }
3669 ref->death = NULL;
Todd Kjos72196392017-06-29 12:02:02 -07003670 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003671 if (list_empty(&death->work.entry)) {
3672 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos72196392017-06-29 12:02:02 -07003673 if (thread->looper &
3674 (BINDER_LOOPER_STATE_REGISTERED |
3675 BINDER_LOOPER_STATE_ENTERED))
Martijn Coenen148ade22017-11-15 09:21:35 +01003676 binder_enqueue_thread_work_ilocked(
3677 thread,
3678 &death->work);
Todd Kjos72196392017-06-29 12:02:02 -07003679 else {
3680 binder_enqueue_work_ilocked(
3681 &death->work,
3682 &proc->todo);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02003683 binder_wakeup_proc_ilocked(
Martijn Coenen408c68b2017-08-31 10:04:19 +02003684 proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003685 }
3686 } else {
3687 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
3688 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
3689 }
Todd Kjos72196392017-06-29 12:02:02 -07003690 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003691 }
Martijn Coenenab51ec62017-06-29 12:02:10 -07003692 binder_node_unlock(ref->node);
Todd Kjos2c1838d2017-06-29 12:02:08 -07003693 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003694 } break;
3695 case BC_DEAD_BINDER_DONE: {
3696 struct binder_work *w;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003697 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003698 struct binder_ref_death *death = NULL;
Seunghun Lee10f62862014-05-01 01:30:23 +09003699
Arve Hjønnevågda498892014-02-21 14:40:26 -08003700 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003701 return -EFAULT;
3702
Lisa Du7a64cd82016-02-17 09:32:52 +08003703 ptr += sizeof(cookie);
Todd Kjos72196392017-06-29 12:02:02 -07003704 binder_inner_proc_lock(proc);
3705 list_for_each_entry(w, &proc->delivered_death,
3706 entry) {
3707 struct binder_ref_death *tmp_death =
3708 container_of(w,
3709 struct binder_ref_death,
3710 work);
Seunghun Lee10f62862014-05-01 01:30:23 +09003711
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003712 if (tmp_death->cookie == cookie) {
3713 death = tmp_death;
3714 break;
3715 }
3716 }
3717 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Todd Kjos8ca86f12018-02-07 13:57:37 -08003718 "%d:%d BC_DEAD_BINDER_DONE %016llx found %pK\n",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003719 proc->pid, thread->pid, (u64)cookie,
3720 death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003721 if (death == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003722 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
3723 proc->pid, thread->pid, (u64)cookie);
Todd Kjos72196392017-06-29 12:02:02 -07003724 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003725 break;
3726 }
Todd Kjos72196392017-06-29 12:02:02 -07003727 binder_dequeue_work_ilocked(&death->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003728 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
3729 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos72196392017-06-29 12:02:02 -07003730 if (thread->looper &
3731 (BINDER_LOOPER_STATE_REGISTERED |
3732 BINDER_LOOPER_STATE_ENTERED))
Martijn Coenen148ade22017-11-15 09:21:35 +01003733 binder_enqueue_thread_work_ilocked(
3734 thread, &death->work);
Todd Kjos72196392017-06-29 12:02:02 -07003735 else {
3736 binder_enqueue_work_ilocked(
3737 &death->work,
3738 &proc->todo);
Martijn Coenen408c68b2017-08-31 10:04:19 +02003739 binder_wakeup_proc_ilocked(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003740 }
3741 }
Todd Kjos72196392017-06-29 12:02:02 -07003742 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003743 } break;
3744
3745 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303746 pr_err("%d:%d unknown command %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003747 proc->pid, thread->pid, cmd);
3748 return -EINVAL;
3749 }
3750 *consumed = ptr - buffer;
3751 }
3752 return 0;
3753}
3754
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02003755static void binder_stat_br(struct binder_proc *proc,
3756 struct binder_thread *thread, uint32_t cmd)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003757{
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003758 trace_binder_return(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003759 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07003760 atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
3761 atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
3762 atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003763 }
3764}
3765
Todd Kjos26b47d82017-06-29 12:01:47 -07003766static int binder_put_node_cmd(struct binder_proc *proc,
3767 struct binder_thread *thread,
3768 void __user **ptrp,
3769 binder_uintptr_t node_ptr,
3770 binder_uintptr_t node_cookie,
3771 int node_debug_id,
3772 uint32_t cmd, const char *cmd_name)
3773{
3774 void __user *ptr = *ptrp;
3775
3776 if (put_user(cmd, (uint32_t __user *)ptr))
3777 return -EFAULT;
3778 ptr += sizeof(uint32_t);
3779
3780 if (put_user(node_ptr, (binder_uintptr_t __user *)ptr))
3781 return -EFAULT;
3782 ptr += sizeof(binder_uintptr_t);
3783
3784 if (put_user(node_cookie, (binder_uintptr_t __user *)ptr))
3785 return -EFAULT;
3786 ptr += sizeof(binder_uintptr_t);
3787
3788 binder_stat_br(proc, thread, cmd);
3789 binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n",
3790 proc->pid, thread->pid, cmd_name, node_debug_id,
3791 (u64)node_ptr, (u64)node_cookie);
3792
3793 *ptrp = ptr;
3794 return 0;
3795}
3796
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02003797static int binder_wait_for_work(struct binder_thread *thread,
3798 bool do_proc_work)
3799{
3800 DEFINE_WAIT(wait);
3801 struct binder_proc *proc = thread->proc;
3802 int ret = 0;
3803
3804 freezer_do_not_count();
3805 binder_inner_proc_lock(proc);
3806 for (;;) {
3807 prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE);
3808 if (binder_has_work_ilocked(thread, do_proc_work))
3809 break;
3810 if (do_proc_work)
3811 list_add(&thread->waiting_thread_node,
3812 &proc->waiting_threads);
3813 binder_inner_proc_unlock(proc);
3814 schedule();
3815 binder_inner_proc_lock(proc);
3816 list_del_init(&thread->waiting_thread_node);
3817 if (signal_pending(current)) {
3818 ret = -ERESTARTSYS;
3819 break;
3820 }
3821 }
3822 finish_wait(&thread->wait, &wait);
3823 binder_inner_proc_unlock(proc);
3824 freezer_count();
3825
3826 return ret;
3827}
3828
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003829static int binder_thread_read(struct binder_proc *proc,
3830 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003831 binder_uintptr_t binder_buffer, size_t size,
3832 binder_size_t *consumed, int non_block)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003833{
Arve Hjønnevågda498892014-02-21 14:40:26 -08003834 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003835 void __user *ptr = buffer + *consumed;
3836 void __user *end = buffer + size;
3837
3838 int ret = 0;
3839 int wait_for_proc_work;
3840
3841 if (*consumed == 0) {
3842 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
3843 return -EFAULT;
3844 ptr += sizeof(uint32_t);
3845 }
3846
3847retry:
Martijn Coenen0b89d692017-06-29 12:02:06 -07003848 binder_inner_proc_lock(proc);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02003849 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
Martijn Coenen0b89d692017-06-29 12:02:06 -07003850 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003851
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003852 thread->looper |= BINDER_LOOPER_STATE_WAITING;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003853
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003854 trace_binder_wait_for_work(wait_for_proc_work,
3855 !!thread->transaction_stack,
Todd Kjos72196392017-06-29 12:02:02 -07003856 !binder_worklist_empty(proc, &thread->todo));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003857 if (wait_for_proc_work) {
3858 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
3859 BINDER_LOOPER_STATE_ENTERED))) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303860 binder_user_error("%d:%d ERROR: Thread waiting for process work before calling BC_REGISTER_LOOPER or BC_ENTER_LOOPER (state %x)\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003861 proc->pid, thread->pid, thread->looper);
3862 wait_event_interruptible(binder_user_error_wait,
3863 binder_stop_on_user_error < 2);
3864 }
3865 binder_set_nice(proc->default_priority);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003866 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003867
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02003868 if (non_block) {
3869 if (!binder_has_work(thread, wait_for_proc_work))
3870 ret = -EAGAIN;
3871 } else {
3872 ret = binder_wait_for_work(thread, wait_for_proc_work);
3873 }
3874
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003875 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
3876
3877 if (ret)
3878 return ret;
3879
3880 while (1) {
3881 uint32_t cmd;
3882 struct binder_transaction_data tr;
Todd Kjos72196392017-06-29 12:02:02 -07003883 struct binder_work *w = NULL;
3884 struct list_head *list = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003885 struct binder_transaction *t = NULL;
Todd Kjos7a4408c2017-06-29 12:01:57 -07003886 struct binder_thread *t_from;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003887
Todd Kjosed297212017-06-29 12:02:01 -07003888 binder_inner_proc_lock(proc);
Todd Kjos72196392017-06-29 12:02:02 -07003889 if (!binder_worklist_empty_ilocked(&thread->todo))
3890 list = &thread->todo;
3891 else if (!binder_worklist_empty_ilocked(&proc->todo) &&
3892 wait_for_proc_work)
3893 list = &proc->todo;
3894 else {
3895 binder_inner_proc_unlock(proc);
3896
Dmitry Voytik395262a2014-09-08 18:16:34 +04003897 /* no data added */
Todd Kjos08dabce2017-06-29 12:01:49 -07003898 if (ptr - buffer == 4 && !thread->looper_need_return)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003899 goto retry;
3900 break;
3901 }
3902
Todd Kjosed297212017-06-29 12:02:01 -07003903 if (end - ptr < sizeof(tr) + 4) {
3904 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003905 break;
Todd Kjosed297212017-06-29 12:02:01 -07003906 }
Todd Kjos72196392017-06-29 12:02:02 -07003907 w = binder_dequeue_work_head_ilocked(list);
Martijn Coenen148ade22017-11-15 09:21:35 +01003908 if (binder_worklist_empty_ilocked(&thread->todo))
3909 thread->process_todo = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003910
3911 switch (w->type) {
3912 case BINDER_WORK_TRANSACTION: {
Todd Kjosed297212017-06-29 12:02:01 -07003913 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003914 t = container_of(w, struct binder_transaction, work);
3915 } break;
Todd Kjos26549d12017-06-29 12:01:55 -07003916 case BINDER_WORK_RETURN_ERROR: {
3917 struct binder_error *e = container_of(
3918 w, struct binder_error, work);
3919
3920 WARN_ON(e->cmd == BR_OK);
Todd Kjosed297212017-06-29 12:02:01 -07003921 binder_inner_proc_unlock(proc);
Todd Kjos26549d12017-06-29 12:01:55 -07003922 if (put_user(e->cmd, (uint32_t __user *)ptr))
3923 return -EFAULT;
宋金时838d5562018-05-10 02:05:03 +00003924 cmd = e->cmd;
Todd Kjos26549d12017-06-29 12:01:55 -07003925 e->cmd = BR_OK;
3926 ptr += sizeof(uint32_t);
3927
宋金时838d5562018-05-10 02:05:03 +00003928 binder_stat_br(proc, thread, cmd);
Todd Kjos26549d12017-06-29 12:01:55 -07003929 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003930 case BINDER_WORK_TRANSACTION_COMPLETE: {
Todd Kjosed297212017-06-29 12:02:01 -07003931 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003932 cmd = BR_TRANSACTION_COMPLETE;
3933 if (put_user(cmd, (uint32_t __user *)ptr))
3934 return -EFAULT;
3935 ptr += sizeof(uint32_t);
3936
3937 binder_stat_br(proc, thread, cmd);
3938 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303939 "%d:%d BR_TRANSACTION_COMPLETE\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003940 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003941 kfree(w);
3942 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3943 } break;
3944 case BINDER_WORK_NODE: {
3945 struct binder_node *node = container_of(w, struct binder_node, work);
Todd Kjos26b47d82017-06-29 12:01:47 -07003946 int strong, weak;
3947 binder_uintptr_t node_ptr = node->ptr;
3948 binder_uintptr_t node_cookie = node->cookie;
3949 int node_debug_id = node->debug_id;
3950 int has_weak_ref;
3951 int has_strong_ref;
3952 void __user *orig_ptr = ptr;
Seunghun Lee10f62862014-05-01 01:30:23 +09003953
Todd Kjos26b47d82017-06-29 12:01:47 -07003954 BUG_ON(proc != node->proc);
3955 strong = node->internal_strong_refs ||
3956 node->local_strong_refs;
3957 weak = !hlist_empty(&node->refs) ||
Todd Kjosadc18842017-06-29 12:01:59 -07003958 node->local_weak_refs ||
3959 node->tmp_refs || strong;
Todd Kjos26b47d82017-06-29 12:01:47 -07003960 has_strong_ref = node->has_strong_ref;
3961 has_weak_ref = node->has_weak_ref;
3962
3963 if (weak && !has_weak_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003964 node->has_weak_ref = 1;
3965 node->pending_weak_ref = 1;
3966 node->local_weak_refs++;
Todd Kjos26b47d82017-06-29 12:01:47 -07003967 }
3968 if (strong && !has_strong_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003969 node->has_strong_ref = 1;
3970 node->pending_strong_ref = 1;
3971 node->local_strong_refs++;
Todd Kjos26b47d82017-06-29 12:01:47 -07003972 }
3973 if (!strong && has_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003974 node->has_strong_ref = 0;
Todd Kjos26b47d82017-06-29 12:01:47 -07003975 if (!weak && has_weak_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003976 node->has_weak_ref = 0;
Todd Kjos26b47d82017-06-29 12:01:47 -07003977 if (!weak && !strong) {
3978 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
3979 "%d:%d node %d u%016llx c%016llx deleted\n",
3980 proc->pid, thread->pid,
3981 node_debug_id,
3982 (u64)node_ptr,
3983 (u64)node_cookie);
3984 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjosed297212017-06-29 12:02:01 -07003985 binder_inner_proc_unlock(proc);
Todd Kjos673068e2017-06-29 12:02:03 -07003986 binder_node_lock(node);
3987 /*
3988 * Acquire the node lock before freeing the
3989 * node to serialize with other threads that
3990 * may have been holding the node lock while
3991 * decrementing this node (avoids race where
3992 * this thread frees while the other thread
3993 * is unlocking the node after the final
3994 * decrement)
3995 */
3996 binder_node_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07003997 binder_free_node(node);
3998 } else
3999 binder_inner_proc_unlock(proc);
4000
Todd Kjos26b47d82017-06-29 12:01:47 -07004001 if (weak && !has_weak_ref)
4002 ret = binder_put_node_cmd(
4003 proc, thread, &ptr, node_ptr,
4004 node_cookie, node_debug_id,
4005 BR_INCREFS, "BR_INCREFS");
4006 if (!ret && strong && !has_strong_ref)
4007 ret = binder_put_node_cmd(
4008 proc, thread, &ptr, node_ptr,
4009 node_cookie, node_debug_id,
4010 BR_ACQUIRE, "BR_ACQUIRE");
4011 if (!ret && !strong && has_strong_ref)
4012 ret = binder_put_node_cmd(
4013 proc, thread, &ptr, node_ptr,
4014 node_cookie, node_debug_id,
4015 BR_RELEASE, "BR_RELEASE");
4016 if (!ret && !weak && has_weak_ref)
4017 ret = binder_put_node_cmd(
4018 proc, thread, &ptr, node_ptr,
4019 node_cookie, node_debug_id,
4020 BR_DECREFS, "BR_DECREFS");
4021 if (orig_ptr == ptr)
4022 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4023 "%d:%d node %d u%016llx c%016llx state unchanged\n",
4024 proc->pid, thread->pid,
4025 node_debug_id,
4026 (u64)node_ptr,
4027 (u64)node_cookie);
4028 if (ret)
4029 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004030 } break;
4031 case BINDER_WORK_DEAD_BINDER:
4032 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4033 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4034 struct binder_ref_death *death;
4035 uint32_t cmd;
Martijn Coenenab51ec62017-06-29 12:02:10 -07004036 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004037
4038 death = container_of(w, struct binder_ref_death, work);
4039 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
4040 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
4041 else
4042 cmd = BR_DEAD_BINDER;
Martijn Coenenab51ec62017-06-29 12:02:10 -07004043 cookie = death->cookie;
4044
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004045 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004046 "%d:%d %s %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004047 proc->pid, thread->pid,
4048 cmd == BR_DEAD_BINDER ?
4049 "BR_DEAD_BINDER" :
4050 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
Martijn Coenenab51ec62017-06-29 12:02:10 -07004051 (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004052 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
Martijn Coenenab51ec62017-06-29 12:02:10 -07004053 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004054 kfree(death);
4055 binder_stats_deleted(BINDER_STAT_DEATH);
Todd Kjosed297212017-06-29 12:02:01 -07004056 } else {
Todd Kjos72196392017-06-29 12:02:02 -07004057 binder_enqueue_work_ilocked(
4058 w, &proc->delivered_death);
Todd Kjosed297212017-06-29 12:02:01 -07004059 binder_inner_proc_unlock(proc);
4060 }
Martijn Coenenab51ec62017-06-29 12:02:10 -07004061 if (put_user(cmd, (uint32_t __user *)ptr))
4062 return -EFAULT;
4063 ptr += sizeof(uint32_t);
4064 if (put_user(cookie,
4065 (binder_uintptr_t __user *)ptr))
4066 return -EFAULT;
4067 ptr += sizeof(binder_uintptr_t);
4068 binder_stat_br(proc, thread, cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004069 if (cmd == BR_DEAD_BINDER)
4070 goto done; /* DEAD_BINDER notifications can cause transactions */
4071 } break;
4072 }
4073
4074 if (!t)
4075 continue;
4076
4077 BUG_ON(t->buffer == NULL);
4078 if (t->buffer->target_node) {
4079 struct binder_node *target_node = t->buffer->target_node;
Seunghun Lee10f62862014-05-01 01:30:23 +09004080
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004081 tr.target.ptr = target_node->ptr;
4082 tr.cookie = target_node->cookie;
4083 t->saved_priority = task_nice(current);
4084 if (t->priority < target_node->min_priority &&
4085 !(t->flags & TF_ONE_WAY))
4086 binder_set_nice(t->priority);
4087 else if (!(t->flags & TF_ONE_WAY) ||
4088 t->saved_priority > target_node->min_priority)
4089 binder_set_nice(target_node->min_priority);
4090 cmd = BR_TRANSACTION;
4091 } else {
Arve Hjønnevågda498892014-02-21 14:40:26 -08004092 tr.target.ptr = 0;
4093 tr.cookie = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004094 cmd = BR_REPLY;
4095 }
4096 tr.code = t->code;
4097 tr.flags = t->flags;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -06004098 tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004099
Todd Kjos7a4408c2017-06-29 12:01:57 -07004100 t_from = binder_get_txn_from(t);
4101 if (t_from) {
4102 struct task_struct *sender = t_from->proc->tsk;
Seunghun Lee10f62862014-05-01 01:30:23 +09004103
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004104 tr.sender_pid = task_tgid_nr_ns(sender,
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08004105 task_active_pid_ns(current));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004106 } else {
4107 tr.sender_pid = 0;
4108 }
4109
4110 tr.data_size = t->buffer->data_size;
4111 tr.offsets_size = t->buffer->offsets_size;
Todd Kjos19c98722017-06-29 12:01:40 -07004112 tr.data.ptr.buffer = (binder_uintptr_t)
4113 ((uintptr_t)t->buffer->data +
4114 binder_alloc_get_user_buffer_offset(&proc->alloc));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004115 tr.data.ptr.offsets = tr.data.ptr.buffer +
4116 ALIGN(t->buffer->data_size,
4117 sizeof(void *));
4118
Todd Kjos7a4408c2017-06-29 12:01:57 -07004119 if (put_user(cmd, (uint32_t __user *)ptr)) {
4120 if (t_from)
4121 binder_thread_dec_tmpref(t_from);
Martijn Coenenfb2c4452017-11-13 10:06:08 +01004122
4123 binder_cleanup_transaction(t, "put_user failed",
4124 BR_FAILED_REPLY);
4125
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004126 return -EFAULT;
Todd Kjos7a4408c2017-06-29 12:01:57 -07004127 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004128 ptr += sizeof(uint32_t);
Todd Kjos7a4408c2017-06-29 12:01:57 -07004129 if (copy_to_user(ptr, &tr, sizeof(tr))) {
4130 if (t_from)
4131 binder_thread_dec_tmpref(t_from);
Martijn Coenenfb2c4452017-11-13 10:06:08 +01004132
4133 binder_cleanup_transaction(t, "copy_to_user failed",
4134 BR_FAILED_REPLY);
4135
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004136 return -EFAULT;
Todd Kjos7a4408c2017-06-29 12:01:57 -07004137 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004138 ptr += sizeof(tr);
4139
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004140 trace_binder_transaction_received(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004141 binder_stat_br(proc, thread, cmd);
4142 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004143 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004144 proc->pid, thread->pid,
4145 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
4146 "BR_REPLY",
Todd Kjos7a4408c2017-06-29 12:01:57 -07004147 t->debug_id, t_from ? t_from->proc->pid : 0,
4148 t_from ? t_from->pid : 0, cmd,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004149 t->buffer->data_size, t->buffer->offsets_size,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004150 (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004151
Todd Kjos7a4408c2017-06-29 12:01:57 -07004152 if (t_from)
4153 binder_thread_dec_tmpref(t_from);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004154 t->buffer->allow_user_free = 1;
4155 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
Martijn Coenen0b89d692017-06-29 12:02:06 -07004156 binder_inner_proc_lock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004157 t->to_parent = thread->transaction_stack;
4158 t->to_thread = thread;
4159 thread->transaction_stack = t;
Martijn Coenen0b89d692017-06-29 12:02:06 -07004160 binder_inner_proc_unlock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004161 } else {
Todd Kjosb6d282c2017-06-29 12:01:54 -07004162 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004163 }
4164 break;
4165 }
4166
4167done:
4168
4169 *consumed = ptr - buffer;
Todd Kjosb3e68612017-06-29 12:02:07 -07004170 binder_inner_proc_lock(proc);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004171 if (proc->requested_threads == 0 &&
4172 list_empty(&thread->proc->waiting_threads) &&
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004173 proc->requested_threads_started < proc->max_threads &&
4174 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4175 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
4176 /*spawn a new thread if we leave this out */) {
4177 proc->requested_threads++;
Todd Kjosb3e68612017-06-29 12:02:07 -07004178 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004179 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304180 "%d:%d BR_SPAWN_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004181 proc->pid, thread->pid);
4182 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
4183 return -EFAULT;
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07004184 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
Todd Kjosb3e68612017-06-29 12:02:07 -07004185 } else
4186 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004187 return 0;
4188}
4189
Todd Kjos72196392017-06-29 12:02:02 -07004190static void binder_release_work(struct binder_proc *proc,
4191 struct list_head *list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004192{
4193 struct binder_work *w;
Seunghun Lee10f62862014-05-01 01:30:23 +09004194
Todd Kjos72196392017-06-29 12:02:02 -07004195 while (1) {
4196 w = binder_dequeue_work_head(proc, list);
4197 if (!w)
4198 return;
4199
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004200 switch (w->type) {
4201 case BINDER_WORK_TRANSACTION: {
4202 struct binder_transaction *t;
4203
4204 t = container_of(w, struct binder_transaction, work);
Martijn Coenenfb2c4452017-11-13 10:06:08 +01004205
4206 binder_cleanup_transaction(t, "process died.",
4207 BR_DEAD_REPLY);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004208 } break;
Todd Kjos26549d12017-06-29 12:01:55 -07004209 case BINDER_WORK_RETURN_ERROR: {
4210 struct binder_error *e = container_of(
4211 w, struct binder_error, work);
4212
4213 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4214 "undelivered TRANSACTION_ERROR: %u\n",
4215 e->cmd);
4216 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004217 case BINDER_WORK_TRANSACTION_COMPLETE: {
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004218 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304219 "undelivered TRANSACTION_COMPLETE\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004220 kfree(w);
4221 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4222 } break;
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004223 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4224 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4225 struct binder_ref_death *death;
4226
4227 death = container_of(w, struct binder_ref_death, work);
4228 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004229 "undelivered death notification, %016llx\n",
4230 (u64)death->cookie);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004231 kfree(death);
4232 binder_stats_deleted(BINDER_STAT_DEATH);
4233 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004234 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304235 pr_err("unexpected work type, %d, not freed\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004236 w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004237 break;
4238 }
4239 }
4240
4241}
4242
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004243static struct binder_thread *binder_get_thread_ilocked(
4244 struct binder_proc *proc, struct binder_thread *new_thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004245{
4246 struct binder_thread *thread = NULL;
4247 struct rb_node *parent = NULL;
4248 struct rb_node **p = &proc->threads.rb_node;
4249
4250 while (*p) {
4251 parent = *p;
4252 thread = rb_entry(parent, struct binder_thread, rb_node);
4253
4254 if (current->pid < thread->pid)
4255 p = &(*p)->rb_left;
4256 else if (current->pid > thread->pid)
4257 p = &(*p)->rb_right;
4258 else
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004259 return thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004260 }
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004261 if (!new_thread)
4262 return NULL;
4263 thread = new_thread;
4264 binder_stats_created(BINDER_STAT_THREAD);
4265 thread->proc = proc;
4266 thread->pid = current->pid;
4267 atomic_set(&thread->tmp_ref, 0);
4268 init_waitqueue_head(&thread->wait);
4269 INIT_LIST_HEAD(&thread->todo);
4270 rb_link_node(&thread->rb_node, parent, p);
4271 rb_insert_color(&thread->rb_node, &proc->threads);
4272 thread->looper_need_return = true;
4273 thread->return_error.work.type = BINDER_WORK_RETURN_ERROR;
4274 thread->return_error.cmd = BR_OK;
4275 thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR;
4276 thread->reply_error.cmd = BR_OK;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004277 INIT_LIST_HEAD(&new_thread->waiting_thread_node);
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004278 return thread;
4279}
4280
4281static struct binder_thread *binder_get_thread(struct binder_proc *proc)
4282{
4283 struct binder_thread *thread;
4284 struct binder_thread *new_thread;
4285
4286 binder_inner_proc_lock(proc);
4287 thread = binder_get_thread_ilocked(proc, NULL);
4288 binder_inner_proc_unlock(proc);
4289 if (!thread) {
4290 new_thread = kzalloc(sizeof(*thread), GFP_KERNEL);
4291 if (new_thread == NULL)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004292 return NULL;
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004293 binder_inner_proc_lock(proc);
4294 thread = binder_get_thread_ilocked(proc, new_thread);
4295 binder_inner_proc_unlock(proc);
4296 if (thread != new_thread)
4297 kfree(new_thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004298 }
4299 return thread;
4300}
4301
Todd Kjos7a4408c2017-06-29 12:01:57 -07004302static void binder_free_proc(struct binder_proc *proc)
4303{
4304 BUG_ON(!list_empty(&proc->todo));
4305 BUG_ON(!list_empty(&proc->delivered_death));
4306 binder_alloc_deferred_release(&proc->alloc);
4307 put_task_struct(proc->tsk);
4308 binder_stats_deleted(BINDER_STAT_PROC);
4309 kfree(proc);
4310}
4311
4312static void binder_free_thread(struct binder_thread *thread)
4313{
4314 BUG_ON(!list_empty(&thread->todo));
4315 binder_stats_deleted(BINDER_STAT_THREAD);
4316 binder_proc_dec_tmpref(thread->proc);
4317 kfree(thread);
4318}
4319
4320static int binder_thread_release(struct binder_proc *proc,
4321 struct binder_thread *thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004322{
4323 struct binder_transaction *t;
4324 struct binder_transaction *send_reply = NULL;
4325 int active_transactions = 0;
Todd Kjos7a4408c2017-06-29 12:01:57 -07004326 struct binder_transaction *last_t = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004327
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004328 binder_inner_proc_lock(thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07004329 /*
4330 * take a ref on the proc so it survives
4331 * after we remove this thread from proc->threads.
4332 * The corresponding dec is when we actually
4333 * free the thread in binder_free_thread()
4334 */
4335 proc->tmp_ref++;
4336 /*
4337 * take a ref on this thread to ensure it
4338 * survives while we are releasing it
4339 */
4340 atomic_inc(&thread->tmp_ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004341 rb_erase(&thread->rb_node, &proc->threads);
4342 t = thread->transaction_stack;
Todd Kjos7a4408c2017-06-29 12:01:57 -07004343 if (t) {
4344 spin_lock(&t->lock);
4345 if (t->to_thread == thread)
4346 send_reply = t;
4347 }
4348 thread->is_dead = true;
4349
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004350 while (t) {
Todd Kjos7a4408c2017-06-29 12:01:57 -07004351 last_t = t;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004352 active_transactions++;
4353 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304354 "release %d:%d transaction %d %s, still active\n",
4355 proc->pid, thread->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004356 t->debug_id,
4357 (t->to_thread == thread) ? "in" : "out");
4358
4359 if (t->to_thread == thread) {
4360 t->to_proc = NULL;
4361 t->to_thread = NULL;
4362 if (t->buffer) {
4363 t->buffer->transaction = NULL;
4364 t->buffer = NULL;
4365 }
4366 t = t->to_parent;
4367 } else if (t->from == thread) {
4368 t->from = NULL;
4369 t = t->from_parent;
4370 } else
4371 BUG();
Todd Kjos7a4408c2017-06-29 12:01:57 -07004372 spin_unlock(&last_t->lock);
4373 if (t)
4374 spin_lock(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004375 }
Martijn Coenenf5cb7792018-01-05 11:27:07 +01004376
4377 /*
4378 * If this thread used poll, make sure we remove the waitqueue
4379 * from any epoll data structures holding it with POLLFREE.
4380 * waitqueue_active() is safe to use here because we're holding
4381 * the inner lock.
4382 */
4383 if ((thread->looper & BINDER_LOOPER_STATE_POLL) &&
4384 waitqueue_active(&thread->wait)) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08004385 wake_up_poll(&thread->wait, EPOLLHUP | POLLFREE);
Martijn Coenenf5cb7792018-01-05 11:27:07 +01004386 }
4387
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004388 binder_inner_proc_unlock(thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07004389
Martijn Coenen5eeb2ca2018-02-16 09:47:15 +01004390 /*
4391 * This is needed to avoid races between wake_up_poll() above and
4392 * and ep_remove_waitqueue() called for other reasons (eg the epoll file
4393 * descriptor being closed); ep_remove_waitqueue() holds an RCU read
4394 * lock, so we can be sure it's done after calling synchronize_rcu().
4395 */
4396 if (thread->looper & BINDER_LOOPER_STATE_POLL)
4397 synchronize_rcu();
4398
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004399 if (send_reply)
4400 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
Todd Kjos72196392017-06-29 12:02:02 -07004401 binder_release_work(proc, &thread->todo);
Todd Kjos7a4408c2017-06-29 12:01:57 -07004402 binder_thread_dec_tmpref(thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004403 return active_transactions;
4404}
4405
Al Viroafc9a422017-07-03 06:39:46 -04004406static __poll_t binder_poll(struct file *filp,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004407 struct poll_table_struct *wait)
4408{
4409 struct binder_proc *proc = filp->private_data;
4410 struct binder_thread *thread = NULL;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004411 bool wait_for_proc_work;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004412
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004413 thread = binder_get_thread(proc);
Eric Biggersf8898262018-01-30 23:11:24 -08004414 if (!thread)
4415 return POLLERR;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004416
Martijn Coenen0b89d692017-06-29 12:02:06 -07004417 binder_inner_proc_lock(thread->proc);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004418 thread->looper |= BINDER_LOOPER_STATE_POLL;
4419 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
4420
Martijn Coenen0b89d692017-06-29 12:02:06 -07004421 binder_inner_proc_unlock(thread->proc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004422
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004423 poll_wait(filp, &thread->wait, wait);
4424
Martijn Coenen66b83a42017-10-09 14:26:56 +02004425 if (binder_has_work(thread, wait_for_proc_work))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08004426 return EPOLLIN;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004427
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004428 return 0;
4429}
4430
Tair Rzayev78260ac2014-06-03 22:27:21 +03004431static int binder_ioctl_write_read(struct file *filp,
4432 unsigned int cmd, unsigned long arg,
4433 struct binder_thread *thread)
4434{
4435 int ret = 0;
4436 struct binder_proc *proc = filp->private_data;
4437 unsigned int size = _IOC_SIZE(cmd);
4438 void __user *ubuf = (void __user *)arg;
4439 struct binder_write_read bwr;
4440
4441 if (size != sizeof(struct binder_write_read)) {
4442 ret = -EINVAL;
4443 goto out;
4444 }
4445 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
4446 ret = -EFAULT;
4447 goto out;
4448 }
4449 binder_debug(BINDER_DEBUG_READ_WRITE,
4450 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
4451 proc->pid, thread->pid,
4452 (u64)bwr.write_size, (u64)bwr.write_buffer,
4453 (u64)bwr.read_size, (u64)bwr.read_buffer);
4454
4455 if (bwr.write_size > 0) {
4456 ret = binder_thread_write(proc, thread,
4457 bwr.write_buffer,
4458 bwr.write_size,
4459 &bwr.write_consumed);
4460 trace_binder_write_done(ret);
4461 if (ret < 0) {
4462 bwr.read_consumed = 0;
4463 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4464 ret = -EFAULT;
4465 goto out;
4466 }
4467 }
4468 if (bwr.read_size > 0) {
4469 ret = binder_thread_read(proc, thread, bwr.read_buffer,
4470 bwr.read_size,
4471 &bwr.read_consumed,
4472 filp->f_flags & O_NONBLOCK);
4473 trace_binder_read_done(ret);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004474 binder_inner_proc_lock(proc);
4475 if (!binder_worklist_empty_ilocked(&proc->todo))
Martijn Coenen408c68b2017-08-31 10:04:19 +02004476 binder_wakeup_proc_ilocked(proc);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004477 binder_inner_proc_unlock(proc);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004478 if (ret < 0) {
4479 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4480 ret = -EFAULT;
4481 goto out;
4482 }
4483 }
4484 binder_debug(BINDER_DEBUG_READ_WRITE,
4485 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
4486 proc->pid, thread->pid,
4487 (u64)bwr.write_consumed, (u64)bwr.write_size,
4488 (u64)bwr.read_consumed, (u64)bwr.read_size);
4489 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
4490 ret = -EFAULT;
4491 goto out;
4492 }
4493out:
4494 return ret;
4495}
4496
4497static int binder_ioctl_set_ctx_mgr(struct file *filp)
4498{
4499 int ret = 0;
4500 struct binder_proc *proc = filp->private_data;
Martijn Coenen342e5c92017-02-03 14:40:46 -08004501 struct binder_context *context = proc->context;
Todd Kjosc44b1232017-06-29 12:01:43 -07004502 struct binder_node *new_node;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004503 kuid_t curr_euid = current_euid();
4504
Todd Kjosc44b1232017-06-29 12:01:43 -07004505 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen342e5c92017-02-03 14:40:46 -08004506 if (context->binder_context_mgr_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004507 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
4508 ret = -EBUSY;
4509 goto out;
4510 }
Stephen Smalley79af7302015-01-21 10:54:10 -05004511 ret = security_binder_set_context_mgr(proc->tsk);
4512 if (ret < 0)
4513 goto out;
Martijn Coenen342e5c92017-02-03 14:40:46 -08004514 if (uid_valid(context->binder_context_mgr_uid)) {
4515 if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004516 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
4517 from_kuid(&init_user_ns, curr_euid),
4518 from_kuid(&init_user_ns,
Martijn Coenen342e5c92017-02-03 14:40:46 -08004519 context->binder_context_mgr_uid));
Tair Rzayev78260ac2014-06-03 22:27:21 +03004520 ret = -EPERM;
4521 goto out;
4522 }
4523 } else {
Martijn Coenen342e5c92017-02-03 14:40:46 -08004524 context->binder_context_mgr_uid = curr_euid;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004525 }
Todd Kjos673068e2017-06-29 12:02:03 -07004526 new_node = binder_new_node(proc, NULL);
Todd Kjosc44b1232017-06-29 12:01:43 -07004527 if (!new_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004528 ret = -ENOMEM;
4529 goto out;
4530 }
Todd Kjos673068e2017-06-29 12:02:03 -07004531 binder_node_lock(new_node);
Todd Kjosc44b1232017-06-29 12:01:43 -07004532 new_node->local_weak_refs++;
4533 new_node->local_strong_refs++;
4534 new_node->has_strong_ref = 1;
4535 new_node->has_weak_ref = 1;
4536 context->binder_context_mgr_node = new_node;
Todd Kjos673068e2017-06-29 12:02:03 -07004537 binder_node_unlock(new_node);
Todd Kjosadc18842017-06-29 12:01:59 -07004538 binder_put_node(new_node);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004539out:
Todd Kjosc44b1232017-06-29 12:01:43 -07004540 mutex_unlock(&context->context_mgr_node_lock);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004541 return ret;
4542}
4543
Colin Crossabcc6152017-08-31 10:04:24 +02004544static int binder_ioctl_get_node_debug_info(struct binder_proc *proc,
4545 struct binder_node_debug_info *info)
4546{
4547 struct rb_node *n;
4548 binder_uintptr_t ptr = info->ptr;
4549
4550 memset(info, 0, sizeof(*info));
4551
4552 binder_inner_proc_lock(proc);
4553 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
4554 struct binder_node *node = rb_entry(n, struct binder_node,
4555 rb_node);
4556 if (node->ptr > ptr) {
4557 info->ptr = node->ptr;
4558 info->cookie = node->cookie;
4559 info->has_strong_ref = node->has_strong_ref;
4560 info->has_weak_ref = node->has_weak_ref;
4561 break;
4562 }
4563 }
4564 binder_inner_proc_unlock(proc);
4565
4566 return 0;
4567}
4568
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004569static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4570{
4571 int ret;
4572 struct binder_proc *proc = filp->private_data;
4573 struct binder_thread *thread;
4574 unsigned int size = _IOC_SIZE(cmd);
4575 void __user *ubuf = (void __user *)arg;
4576
Tair Rzayev78260ac2014-06-03 22:27:21 +03004577 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
4578 proc->pid, current->pid, cmd, arg);*/
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004579
Sherry Yang4175e2b2017-08-23 08:46:40 -07004580 binder_selftest_alloc(&proc->alloc);
4581
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004582 trace_binder_ioctl(cmd, arg);
4583
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004584 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4585 if (ret)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004586 goto err_unlocked;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004587
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004588 thread = binder_get_thread(proc);
4589 if (thread == NULL) {
4590 ret = -ENOMEM;
4591 goto err;
4592 }
4593
4594 switch (cmd) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004595 case BINDER_WRITE_READ:
4596 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
4597 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004598 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004599 break;
Todd Kjosb3e68612017-06-29 12:02:07 -07004600 case BINDER_SET_MAX_THREADS: {
4601 int max_threads;
4602
4603 if (copy_from_user(&max_threads, ubuf,
4604 sizeof(max_threads))) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004605 ret = -EINVAL;
4606 goto err;
4607 }
Todd Kjosb3e68612017-06-29 12:02:07 -07004608 binder_inner_proc_lock(proc);
4609 proc->max_threads = max_threads;
4610 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004611 break;
Todd Kjosb3e68612017-06-29 12:02:07 -07004612 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004613 case BINDER_SET_CONTEXT_MGR:
Tair Rzayev78260ac2014-06-03 22:27:21 +03004614 ret = binder_ioctl_set_ctx_mgr(filp);
4615 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004616 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004617 break;
4618 case BINDER_THREAD_EXIT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304619 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004620 proc->pid, thread->pid);
Todd Kjos7a4408c2017-06-29 12:01:57 -07004621 binder_thread_release(proc, thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004622 thread = NULL;
4623 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02004624 case BINDER_VERSION: {
4625 struct binder_version __user *ver = ubuf;
4626
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004627 if (size != sizeof(struct binder_version)) {
4628 ret = -EINVAL;
4629 goto err;
4630 }
Mathieu Maret36c89c02014-04-15 12:03:05 +02004631 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
4632 &ver->protocol_version)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004633 ret = -EINVAL;
4634 goto err;
4635 }
4636 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02004637 }
Colin Crossabcc6152017-08-31 10:04:24 +02004638 case BINDER_GET_NODE_DEBUG_INFO: {
4639 struct binder_node_debug_info info;
4640
4641 if (copy_from_user(&info, ubuf, sizeof(info))) {
4642 ret = -EFAULT;
4643 goto err;
4644 }
4645
4646 ret = binder_ioctl_get_node_debug_info(proc, &info);
4647 if (ret < 0)
4648 goto err;
4649
4650 if (copy_to_user(ubuf, &info, sizeof(info))) {
4651 ret = -EFAULT;
4652 goto err;
4653 }
4654 break;
4655 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004656 default:
4657 ret = -EINVAL;
4658 goto err;
4659 }
4660 ret = 0;
4661err:
4662 if (thread)
Todd Kjos08dabce2017-06-29 12:01:49 -07004663 thread->looper_need_return = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004664 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4665 if (ret && ret != -ERESTARTSYS)
Anmol Sarma56b468f2012-10-30 22:35:43 +05304666 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 -07004667err_unlocked:
4668 trace_binder_ioctl_done(ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004669 return ret;
4670}
4671
4672static void binder_vma_open(struct vm_area_struct *vma)
4673{
4674 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004675
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004676 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304677 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004678 proc->pid, vma->vm_start, vma->vm_end,
4679 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4680 (unsigned long)pgprot_val(vma->vm_page_prot));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004681}
4682
4683static void binder_vma_close(struct vm_area_struct *vma)
4684{
4685 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004686
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004687 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304688 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004689 proc->pid, vma->vm_start, vma->vm_end,
4690 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4691 (unsigned long)pgprot_val(vma->vm_page_prot));
Todd Kjos19c98722017-06-29 12:01:40 -07004692 binder_alloc_vma_close(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004693 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
4694}
4695
Dave Jiang11bac802017-02-24 14:56:41 -08004696static int binder_vm_fault(struct vm_fault *vmf)
Vinayak Menonddac7d52014-06-02 18:17:59 +05304697{
4698 return VM_FAULT_SIGBUS;
4699}
4700
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07004701static const struct vm_operations_struct binder_vm_ops = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004702 .open = binder_vma_open,
4703 .close = binder_vma_close,
Vinayak Menonddac7d52014-06-02 18:17:59 +05304704 .fault = binder_vm_fault,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004705};
4706
Todd Kjos19c98722017-06-29 12:01:40 -07004707static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
4708{
4709 int ret;
4710 struct binder_proc *proc = filp->private_data;
4711 const char *failure_string;
4712
4713 if (proc->tsk != current->group_leader)
4714 return -EINVAL;
4715
4716 if ((vma->vm_end - vma->vm_start) > SZ_4M)
4717 vma->vm_end = vma->vm_start + SZ_4M;
4718
4719 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4720 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
4721 __func__, proc->pid, vma->vm_start, vma->vm_end,
4722 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4723 (unsigned long)pgprot_val(vma->vm_page_prot));
4724
4725 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
4726 ret = -EPERM;
4727 failure_string = "bad vm_flags";
4728 goto err_bad_arg;
4729 }
4730 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
4731 vma->vm_ops = &binder_vm_ops;
4732 vma->vm_private_data = proc;
4733
4734 ret = binder_alloc_mmap_handler(&proc->alloc, vma);
4735 if (ret)
4736 return ret;
Todd Kjos7f3dc002017-11-27 09:32:33 -08004737 mutex_lock(&proc->files_lock);
Todd Kjos19c98722017-06-29 12:01:40 -07004738 proc->files = get_files_struct(current);
Todd Kjos7f3dc002017-11-27 09:32:33 -08004739 mutex_unlock(&proc->files_lock);
Todd Kjos19c98722017-06-29 12:01:40 -07004740 return 0;
4741
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004742err_bad_arg:
Elad Wexler00c41cd2017-12-29 11:03:37 +02004743 pr_err("%s: %d %lx-%lx %s failed %d\n", __func__,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004744 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
4745 return ret;
4746}
4747
4748static int binder_open(struct inode *nodp, struct file *filp)
4749{
4750 struct binder_proc *proc;
Martijn Coenenac4812c2017-02-03 14:40:48 -08004751 struct binder_device *binder_dev;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004752
Elad Wexler00c41cd2017-12-29 11:03:37 +02004753 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "%s: %d:%d\n", __func__,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004754 current->group_leader->pid, current->pid);
4755
4756 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
4757 if (proc == NULL)
4758 return -ENOMEM;
Todd Kjos9630fe82017-06-29 12:02:00 -07004759 spin_lock_init(&proc->inner_lock);
4760 spin_lock_init(&proc->outer_lock);
Todd Kjosc4ea41b2017-06-29 12:01:36 -07004761 get_task_struct(current->group_leader);
4762 proc->tsk = current->group_leader;
Todd Kjos7f3dc002017-11-27 09:32:33 -08004763 mutex_init(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004764 INIT_LIST_HEAD(&proc->todo);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004765 proc->default_priority = task_nice(current);
Martijn Coenenac4812c2017-02-03 14:40:48 -08004766 binder_dev = container_of(filp->private_data, struct binder_device,
4767 miscdev);
4768 proc->context = &binder_dev->context;
Todd Kjos19c98722017-06-29 12:01:40 -07004769 binder_alloc_init(&proc->alloc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004770
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004771 binder_stats_created(BINDER_STAT_PROC);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004772 proc->pid = current->group_leader->pid;
4773 INIT_LIST_HEAD(&proc->delivered_death);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004774 INIT_LIST_HEAD(&proc->waiting_threads);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004775 filp->private_data = proc;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004776
Todd Kjosc44b1232017-06-29 12:01:43 -07004777 mutex_lock(&binder_procs_lock);
4778 hlist_add_head(&proc->proc_node, &binder_procs);
4779 mutex_unlock(&binder_procs_lock);
4780
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004781 if (binder_debugfs_dir_entry_proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004782 char strbuf[11];
Seunghun Lee10f62862014-05-01 01:30:23 +09004783
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004784 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
Martijn Coenen14db3182017-02-03 14:40:47 -08004785 /*
4786 * proc debug entries are shared between contexts, so
4787 * this will fail if the process tries to open the driver
4788 * again with a different context. The priting code will
4789 * anyway print all contexts that a given PID has, so this
4790 * is not a problem.
4791 */
Harsh Shandilya21d02dd2017-12-22 19:37:02 +05304792 proc->debugfs_entry = debugfs_create_file(strbuf, 0444,
Martijn Coenen14db3182017-02-03 14:40:47 -08004793 binder_debugfs_dir_entry_proc,
4794 (void *)(unsigned long)proc->pid,
4795 &binder_proc_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004796 }
4797
4798 return 0;
4799}
4800
4801static int binder_flush(struct file *filp, fl_owner_t id)
4802{
4803 struct binder_proc *proc = filp->private_data;
4804
4805 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
4806
4807 return 0;
4808}
4809
4810static void binder_deferred_flush(struct binder_proc *proc)
4811{
4812 struct rb_node *n;
4813 int wake_count = 0;
Seunghun Lee10f62862014-05-01 01:30:23 +09004814
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004815 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004816 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
4817 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
Seunghun Lee10f62862014-05-01 01:30:23 +09004818
Todd Kjos08dabce2017-06-29 12:01:49 -07004819 thread->looper_need_return = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004820 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
4821 wake_up_interruptible(&thread->wait);
4822 wake_count++;
4823 }
4824 }
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004825 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004826
4827 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4828 "binder_flush: %d woke %d threads\n", proc->pid,
4829 wake_count);
4830}
4831
4832static int binder_release(struct inode *nodp, struct file *filp)
4833{
4834 struct binder_proc *proc = filp->private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004835
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004836 debugfs_remove(proc->debugfs_entry);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004837 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
4838
4839 return 0;
4840}
4841
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004842static int binder_node_release(struct binder_node *node, int refs)
4843{
4844 struct binder_ref *ref;
4845 int death = 0;
Todd Kjosed297212017-06-29 12:02:01 -07004846 struct binder_proc *proc = node->proc;
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004847
Todd Kjos72196392017-06-29 12:02:02 -07004848 binder_release_work(proc, &node->async_todo);
Todd Kjosed297212017-06-29 12:02:01 -07004849
Todd Kjos673068e2017-06-29 12:02:03 -07004850 binder_node_lock(node);
Todd Kjosed297212017-06-29 12:02:01 -07004851 binder_inner_proc_lock(proc);
Todd Kjos72196392017-06-29 12:02:02 -07004852 binder_dequeue_work_ilocked(&node->work);
Todd Kjosadc18842017-06-29 12:01:59 -07004853 /*
4854 * The caller must have taken a temporary ref on the node,
4855 */
4856 BUG_ON(!node->tmp_refs);
4857 if (hlist_empty(&node->refs) && node->tmp_refs == 1) {
Todd Kjosed297212017-06-29 12:02:01 -07004858 binder_inner_proc_unlock(proc);
Todd Kjos673068e2017-06-29 12:02:03 -07004859 binder_node_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07004860 binder_free_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004861
4862 return refs;
4863 }
4864
4865 node->proc = NULL;
4866 node->local_strong_refs = 0;
4867 node->local_weak_refs = 0;
Todd Kjosed297212017-06-29 12:02:01 -07004868 binder_inner_proc_unlock(proc);
Todd Kjosc44b1232017-06-29 12:01:43 -07004869
4870 spin_lock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004871 hlist_add_head(&node->dead_node, &binder_dead_nodes);
Todd Kjosc44b1232017-06-29 12:01:43 -07004872 spin_unlock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004873
4874 hlist_for_each_entry(ref, &node->refs, node_entry) {
4875 refs++;
Martijn Coenenab51ec62017-06-29 12:02:10 -07004876 /*
4877 * Need the node lock to synchronize
4878 * with new notification requests and the
4879 * inner lock to synchronize with queued
4880 * death notifications.
4881 */
4882 binder_inner_proc_lock(ref->proc);
4883 if (!ref->death) {
4884 binder_inner_proc_unlock(ref->proc);
Arve Hjønnevåge194fd82014-02-17 13:58:29 -08004885 continue;
Martijn Coenenab51ec62017-06-29 12:02:10 -07004886 }
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004887
4888 death++;
4889
Martijn Coenenab51ec62017-06-29 12:02:10 -07004890 BUG_ON(!list_empty(&ref->death->work.entry));
4891 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
4892 binder_enqueue_work_ilocked(&ref->death->work,
4893 &ref->proc->todo);
Martijn Coenen408c68b2017-08-31 10:04:19 +02004894 binder_wakeup_proc_ilocked(ref->proc);
Todd Kjos72196392017-06-29 12:02:02 -07004895 binder_inner_proc_unlock(ref->proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004896 }
4897
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004898 binder_debug(BINDER_DEBUG_DEAD_BINDER,
4899 "node %d now dead, refs %d, death %d\n",
4900 node->debug_id, refs, death);
Todd Kjos673068e2017-06-29 12:02:03 -07004901 binder_node_unlock(node);
Todd Kjosadc18842017-06-29 12:01:59 -07004902 binder_put_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004903
4904 return refs;
4905}
4906
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004907static void binder_deferred_release(struct binder_proc *proc)
4908{
Martijn Coenen342e5c92017-02-03 14:40:46 -08004909 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004910 struct rb_node *n;
Todd Kjos19c98722017-06-29 12:01:40 -07004911 int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004912
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004913 BUG_ON(proc->files);
4914
Todd Kjosc44b1232017-06-29 12:01:43 -07004915 mutex_lock(&binder_procs_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004916 hlist_del(&proc->proc_node);
Todd Kjosc44b1232017-06-29 12:01:43 -07004917 mutex_unlock(&binder_procs_lock);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004918
Todd Kjosc44b1232017-06-29 12:01:43 -07004919 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen342e5c92017-02-03 14:40:46 -08004920 if (context->binder_context_mgr_node &&
4921 context->binder_context_mgr_node->proc == proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004922 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01004923 "%s: %d context_mgr_node gone\n",
4924 __func__, proc->pid);
Martijn Coenen342e5c92017-02-03 14:40:46 -08004925 context->binder_context_mgr_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004926 }
Todd Kjosc44b1232017-06-29 12:01:43 -07004927 mutex_unlock(&context->context_mgr_node_lock);
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004928 binder_inner_proc_lock(proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07004929 /*
4930 * Make sure proc stays alive after we
4931 * remove all the threads
4932 */
4933 proc->tmp_ref++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004934
Todd Kjos7a4408c2017-06-29 12:01:57 -07004935 proc->is_dead = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004936 threads = 0;
4937 active_transactions = 0;
4938 while ((n = rb_first(&proc->threads))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004939 struct binder_thread *thread;
4940
4941 thread = rb_entry(n, struct binder_thread, rb_node);
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004942 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004943 threads++;
Todd Kjos7a4408c2017-06-29 12:01:57 -07004944 active_transactions += binder_thread_release(proc, thread);
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004945 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004946 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004947
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004948 nodes = 0;
4949 incoming_refs = 0;
4950 while ((n = rb_first(&proc->nodes))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004951 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004952
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004953 node = rb_entry(n, struct binder_node, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004954 nodes++;
Todd Kjosadc18842017-06-29 12:01:59 -07004955 /*
4956 * take a temporary ref on the node before
4957 * calling binder_node_release() which will either
4958 * kfree() the node or call binder_put_node()
4959 */
Todd Kjosda0fa9e2017-06-29 12:02:04 -07004960 binder_inc_node_tmpref_ilocked(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004961 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07004962 binder_inner_proc_unlock(proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004963 incoming_refs = binder_node_release(node, incoming_refs);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07004964 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004965 }
Todd Kjosda0fa9e2017-06-29 12:02:04 -07004966 binder_inner_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004967
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004968 outgoing_refs = 0;
Todd Kjos2c1838d2017-06-29 12:02:08 -07004969 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004970 while ((n = rb_first(&proc->refs_by_desc))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004971 struct binder_ref *ref;
4972
4973 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004974 outgoing_refs++;
Todd Kjos2c1838d2017-06-29 12:02:08 -07004975 binder_cleanup_ref_olocked(ref);
4976 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07004977 binder_free_ref(ref);
Todd Kjos2c1838d2017-06-29 12:02:08 -07004978 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004979 }
Todd Kjos2c1838d2017-06-29 12:02:08 -07004980 binder_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004981
Todd Kjos72196392017-06-29 12:02:02 -07004982 binder_release_work(proc, &proc->todo);
4983 binder_release_work(proc, &proc->delivered_death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004984
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004985 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Todd Kjos19c98722017-06-29 12:01:40 -07004986 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01004987 __func__, proc->pid, threads, nodes, incoming_refs,
Todd Kjos19c98722017-06-29 12:01:40 -07004988 outgoing_refs, active_transactions);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004989
Todd Kjos7a4408c2017-06-29 12:01:57 -07004990 binder_proc_dec_tmpref(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004991}
4992
4993static void binder_deferred_func(struct work_struct *work)
4994{
4995 struct binder_proc *proc;
4996 struct files_struct *files;
4997
4998 int defer;
Seunghun Lee10f62862014-05-01 01:30:23 +09004999
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005000 do {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005001 mutex_lock(&binder_deferred_lock);
5002 if (!hlist_empty(&binder_deferred_list)) {
5003 proc = hlist_entry(binder_deferred_list.first,
5004 struct binder_proc, deferred_work_node);
5005 hlist_del_init(&proc->deferred_work_node);
5006 defer = proc->deferred_work;
5007 proc->deferred_work = 0;
5008 } else {
5009 proc = NULL;
5010 defer = 0;
5011 }
5012 mutex_unlock(&binder_deferred_lock);
5013
5014 files = NULL;
5015 if (defer & BINDER_DEFERRED_PUT_FILES) {
Todd Kjos7f3dc002017-11-27 09:32:33 -08005016 mutex_lock(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005017 files = proc->files;
5018 if (files)
5019 proc->files = NULL;
Todd Kjos7f3dc002017-11-27 09:32:33 -08005020 mutex_unlock(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005021 }
5022
5023 if (defer & BINDER_DEFERRED_FLUSH)
5024 binder_deferred_flush(proc);
5025
5026 if (defer & BINDER_DEFERRED_RELEASE)
5027 binder_deferred_release(proc); /* frees proc */
5028
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005029 if (files)
5030 put_files_struct(files);
5031 } while (proc);
5032}
5033static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
5034
5035static void
5036binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
5037{
5038 mutex_lock(&binder_deferred_lock);
5039 proc->deferred_work |= defer;
5040 if (hlist_unhashed(&proc->deferred_work_node)) {
5041 hlist_add_head(&proc->deferred_work_node,
5042 &binder_deferred_list);
Bhaktipriya Shridhar1beba522016-08-13 22:16:24 +05305043 schedule_work(&binder_deferred_work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005044 }
5045 mutex_unlock(&binder_deferred_lock);
5046}
5047
Todd Kjos5f2f6362017-06-29 12:02:09 -07005048static void print_binder_transaction_ilocked(struct seq_file *m,
5049 struct binder_proc *proc,
5050 const char *prefix,
5051 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005052{
Todd Kjos5f2f6362017-06-29 12:02:09 -07005053 struct binder_proc *to_proc;
5054 struct binder_buffer *buffer = t->buffer;
5055
Todd Kjos7a4408c2017-06-29 12:01:57 -07005056 spin_lock(&t->lock);
Todd Kjos5f2f6362017-06-29 12:02:09 -07005057 to_proc = t->to_proc;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005058 seq_printf(m,
Todd Kjos8ca86f12018-02-07 13:57:37 -08005059 "%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 -07005060 prefix, t->debug_id, t,
5061 t->from ? t->from->proc->pid : 0,
5062 t->from ? t->from->pid : 0,
Todd Kjos5f2f6362017-06-29 12:02:09 -07005063 to_proc ? to_proc->pid : 0,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005064 t->to_thread ? t->to_thread->pid : 0,
5065 t->code, t->flags, t->priority, t->need_reply);
Todd Kjos7a4408c2017-06-29 12:01:57 -07005066 spin_unlock(&t->lock);
5067
Todd Kjos5f2f6362017-06-29 12:02:09 -07005068 if (proc != to_proc) {
5069 /*
5070 * Can only safely deref buffer if we are holding the
5071 * correct proc inner lock for this node
5072 */
5073 seq_puts(m, "\n");
5074 return;
5075 }
5076
5077 if (buffer == NULL) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005078 seq_puts(m, " buffer free\n");
5079 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005080 }
Todd Kjos5f2f6362017-06-29 12:02:09 -07005081 if (buffer->target_node)
5082 seq_printf(m, " node %d", buffer->target_node->debug_id);
Todd Kjos8ca86f12018-02-07 13:57:37 -08005083 seq_printf(m, " size %zd:%zd data %pK\n",
Todd Kjos5f2f6362017-06-29 12:02:09 -07005084 buffer->data_size, buffer->offsets_size,
5085 buffer->data);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005086}
5087
Todd Kjos5f2f6362017-06-29 12:02:09 -07005088static void print_binder_work_ilocked(struct seq_file *m,
5089 struct binder_proc *proc,
5090 const char *prefix,
5091 const char *transaction_prefix,
5092 struct binder_work *w)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005093{
5094 struct binder_node *node;
5095 struct binder_transaction *t;
5096
5097 switch (w->type) {
5098 case BINDER_WORK_TRANSACTION:
5099 t = container_of(w, struct binder_transaction, work);
Todd Kjos5f2f6362017-06-29 12:02:09 -07005100 print_binder_transaction_ilocked(
5101 m, proc, transaction_prefix, t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005102 break;
Todd Kjos26549d12017-06-29 12:01:55 -07005103 case BINDER_WORK_RETURN_ERROR: {
5104 struct binder_error *e = container_of(
5105 w, struct binder_error, work);
5106
5107 seq_printf(m, "%stransaction error: %u\n",
5108 prefix, e->cmd);
5109 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005110 case BINDER_WORK_TRANSACTION_COMPLETE:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005111 seq_printf(m, "%stransaction complete\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005112 break;
5113 case BINDER_WORK_NODE:
5114 node = container_of(w, struct binder_node, work);
Arve Hjønnevågda498892014-02-21 14:40:26 -08005115 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
5116 prefix, node->debug_id,
5117 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005118 break;
5119 case BINDER_WORK_DEAD_BINDER:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005120 seq_printf(m, "%shas dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005121 break;
5122 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005123 seq_printf(m, "%shas cleared dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005124 break;
5125 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005126 seq_printf(m, "%shas cleared death notification\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005127 break;
5128 default:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005129 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005130 break;
5131 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005132}
5133
Todd Kjos72196392017-06-29 12:02:02 -07005134static void print_binder_thread_ilocked(struct seq_file *m,
5135 struct binder_thread *thread,
5136 int print_always)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005137{
5138 struct binder_transaction *t;
5139 struct binder_work *w;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005140 size_t start_pos = m->count;
5141 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005142
Todd Kjos7a4408c2017-06-29 12:01:57 -07005143 seq_printf(m, " thread %d: l %02x need_return %d tr %d\n",
Todd Kjos08dabce2017-06-29 12:01:49 -07005144 thread->pid, thread->looper,
Todd Kjos7a4408c2017-06-29 12:01:57 -07005145 thread->looper_need_return,
5146 atomic_read(&thread->tmp_ref));
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005147 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005148 t = thread->transaction_stack;
5149 while (t) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005150 if (t->from == thread) {
Todd Kjos5f2f6362017-06-29 12:02:09 -07005151 print_binder_transaction_ilocked(m, thread->proc,
5152 " outgoing transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005153 t = t->from_parent;
5154 } else if (t->to_thread == thread) {
Todd Kjos5f2f6362017-06-29 12:02:09 -07005155 print_binder_transaction_ilocked(m, thread->proc,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005156 " incoming transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005157 t = t->to_parent;
5158 } else {
Todd Kjos5f2f6362017-06-29 12:02:09 -07005159 print_binder_transaction_ilocked(m, thread->proc,
5160 " bad transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005161 t = NULL;
5162 }
5163 }
5164 list_for_each_entry(w, &thread->todo, entry) {
Todd Kjos5f2f6362017-06-29 12:02:09 -07005165 print_binder_work_ilocked(m, thread->proc, " ",
Todd Kjos72196392017-06-29 12:02:02 -07005166 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005167 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005168 if (!print_always && m->count == header_pos)
5169 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005170}
5171
Todd Kjosda0fa9e2017-06-29 12:02:04 -07005172static void print_binder_node_nilocked(struct seq_file *m,
5173 struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005174{
5175 struct binder_ref *ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005176 struct binder_work *w;
5177 int count;
5178
5179 count = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08005180 hlist_for_each_entry(ref, &node->refs, node_entry)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005181 count++;
5182
Todd Kjosadc18842017-06-29 12:01:59 -07005183 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 -08005184 node->debug_id, (u64)node->ptr, (u64)node->cookie,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005185 node->has_strong_ref, node->has_weak_ref,
5186 node->local_strong_refs, node->local_weak_refs,
Todd Kjosadc18842017-06-29 12:01:59 -07005187 node->internal_strong_refs, count, node->tmp_refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005188 if (count) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005189 seq_puts(m, " proc");
Sasha Levinb67bfe02013-02-27 17:06:00 -08005190 hlist_for_each_entry(ref, &node->refs, node_entry)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005191 seq_printf(m, " %d", ref->proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005192 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005193 seq_puts(m, "\n");
Todd Kjos72196392017-06-29 12:02:02 -07005194 if (node->proc) {
Todd Kjos72196392017-06-29 12:02:02 -07005195 list_for_each_entry(w, &node->async_todo, entry)
Todd Kjos5f2f6362017-06-29 12:02:09 -07005196 print_binder_work_ilocked(m, node->proc, " ",
Todd Kjos72196392017-06-29 12:02:02 -07005197 " pending async transaction", w);
Todd Kjos72196392017-06-29 12:02:02 -07005198 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005199}
5200
Todd Kjos2c1838d2017-06-29 12:02:08 -07005201static void print_binder_ref_olocked(struct seq_file *m,
5202 struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005203{
Todd Kjos673068e2017-06-29 12:02:03 -07005204 binder_node_lock(ref->node);
Todd Kjos372e3142017-06-29 12:01:58 -07005205 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
5206 ref->data.debug_id, ref->data.desc,
5207 ref->node->proc ? "" : "dead ",
5208 ref->node->debug_id, ref->data.strong,
5209 ref->data.weak, ref->death);
Todd Kjos673068e2017-06-29 12:02:03 -07005210 binder_node_unlock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005211}
5212
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005213static void print_binder_proc(struct seq_file *m,
5214 struct binder_proc *proc, int print_all)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005215{
5216 struct binder_work *w;
5217 struct rb_node *n;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005218 size_t start_pos = m->count;
5219 size_t header_pos;
Todd Kjosda0fa9e2017-06-29 12:02:04 -07005220 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005221
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005222 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen14db3182017-02-03 14:40:47 -08005223 seq_printf(m, "context %s\n", proc->context->name);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005224 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005225
Todd Kjos72196392017-06-29 12:02:02 -07005226 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005227 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
Todd Kjos72196392017-06-29 12:02:02 -07005228 print_binder_thread_ilocked(m, rb_entry(n, struct binder_thread,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005229 rb_node), print_all);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07005230
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005231 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005232 struct binder_node *node = rb_entry(n, struct binder_node,
5233 rb_node);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07005234 /*
5235 * take a temporary reference on the node so it
5236 * survives and isn't removed from the tree
5237 * while we print it.
5238 */
5239 binder_inc_node_tmpref_ilocked(node);
5240 /* Need to drop inner lock to take node lock */
5241 binder_inner_proc_unlock(proc);
5242 if (last_node)
5243 binder_put_node(last_node);
5244 binder_node_inner_lock(node);
5245 print_binder_node_nilocked(m, node);
5246 binder_node_inner_unlock(node);
5247 last_node = node;
5248 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005249 }
Todd Kjosda0fa9e2017-06-29 12:02:04 -07005250 binder_inner_proc_unlock(proc);
5251 if (last_node)
5252 binder_put_node(last_node);
5253
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005254 if (print_all) {
Todd Kjos2c1838d2017-06-29 12:02:08 -07005255 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005256 for (n = rb_first(&proc->refs_by_desc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005257 n != NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005258 n = rb_next(n))
Todd Kjos2c1838d2017-06-29 12:02:08 -07005259 print_binder_ref_olocked(m, rb_entry(n,
5260 struct binder_ref,
5261 rb_node_desc));
5262 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005263 }
Todd Kjos19c98722017-06-29 12:01:40 -07005264 binder_alloc_print_allocated(m, &proc->alloc);
Todd Kjos72196392017-06-29 12:02:02 -07005265 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005266 list_for_each_entry(w, &proc->todo, entry)
Todd Kjos5f2f6362017-06-29 12:02:09 -07005267 print_binder_work_ilocked(m, proc, " ",
5268 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005269 list_for_each_entry(w, &proc->delivered_death, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005270 seq_puts(m, " has delivered dead binder\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005271 break;
5272 }
Todd Kjos72196392017-06-29 12:02:02 -07005273 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005274 if (!print_all && m->count == header_pos)
5275 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005276}
5277
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005278static const char * const binder_return_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005279 "BR_ERROR",
5280 "BR_OK",
5281 "BR_TRANSACTION",
5282 "BR_REPLY",
5283 "BR_ACQUIRE_RESULT",
5284 "BR_DEAD_REPLY",
5285 "BR_TRANSACTION_COMPLETE",
5286 "BR_INCREFS",
5287 "BR_ACQUIRE",
5288 "BR_RELEASE",
5289 "BR_DECREFS",
5290 "BR_ATTEMPT_ACQUIRE",
5291 "BR_NOOP",
5292 "BR_SPAWN_LOOPER",
5293 "BR_FINISHED",
5294 "BR_DEAD_BINDER",
5295 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
5296 "BR_FAILED_REPLY"
5297};
5298
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005299static const char * const binder_command_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005300 "BC_TRANSACTION",
5301 "BC_REPLY",
5302 "BC_ACQUIRE_RESULT",
5303 "BC_FREE_BUFFER",
5304 "BC_INCREFS",
5305 "BC_ACQUIRE",
5306 "BC_RELEASE",
5307 "BC_DECREFS",
5308 "BC_INCREFS_DONE",
5309 "BC_ACQUIRE_DONE",
5310 "BC_ATTEMPT_ACQUIRE",
5311 "BC_REGISTER_LOOPER",
5312 "BC_ENTER_LOOPER",
5313 "BC_EXIT_LOOPER",
5314 "BC_REQUEST_DEATH_NOTIFICATION",
5315 "BC_CLEAR_DEATH_NOTIFICATION",
Martijn Coenen79802402017-02-03 14:40:51 -08005316 "BC_DEAD_BINDER_DONE",
5317 "BC_TRANSACTION_SG",
5318 "BC_REPLY_SG",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005319};
5320
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005321static const char * const binder_objstat_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005322 "proc",
5323 "thread",
5324 "node",
5325 "ref",
5326 "death",
5327 "transaction",
5328 "transaction_complete"
5329};
5330
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005331static void print_binder_stats(struct seq_file *m, const char *prefix,
5332 struct binder_stats *stats)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005333{
5334 int i;
5335
5336 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005337 ARRAY_SIZE(binder_command_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005338 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07005339 int temp = atomic_read(&stats->bc[i]);
5340
5341 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005342 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07005343 binder_command_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005344 }
5345
5346 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005347 ARRAY_SIZE(binder_return_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005348 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07005349 int temp = atomic_read(&stats->br[i]);
5350
5351 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005352 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07005353 binder_return_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005354 }
5355
5356 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005357 ARRAY_SIZE(binder_objstat_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005358 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005359 ARRAY_SIZE(stats->obj_deleted));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005360 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07005361 int created = atomic_read(&stats->obj_created[i]);
5362 int deleted = atomic_read(&stats->obj_deleted[i]);
5363
5364 if (created || deleted)
5365 seq_printf(m, "%s%s: active %d total %d\n",
5366 prefix,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005367 binder_objstat_strings[i],
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07005368 created - deleted,
5369 created);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005370 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005371}
5372
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005373static void print_binder_proc_stats(struct seq_file *m,
5374 struct binder_proc *proc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005375{
5376 struct binder_work *w;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02005377 struct binder_thread *thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005378 struct rb_node *n;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02005379 int count, strong, weak, ready_threads;
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07005380 size_t free_async_space =
5381 binder_alloc_get_free_async_space(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005382
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005383 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen14db3182017-02-03 14:40:47 -08005384 seq_printf(m, "context %s\n", proc->context->name);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005385 count = 0;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02005386 ready_threads = 0;
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07005387 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005388 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
5389 count++;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02005390
5391 list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node)
5392 ready_threads++;
5393
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005394 seq_printf(m, " threads: %d\n", count);
5395 seq_printf(m, " requested threads: %d+%d/%d\n"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005396 " ready threads %d\n"
5397 " free async space %zd\n", proc->requested_threads,
5398 proc->requested_threads_started, proc->max_threads,
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02005399 ready_threads,
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07005400 free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005401 count = 0;
5402 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
5403 count++;
Todd Kjosda0fa9e2017-06-29 12:02:04 -07005404 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005405 seq_printf(m, " nodes: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005406 count = 0;
5407 strong = 0;
5408 weak = 0;
Todd Kjos2c1838d2017-06-29 12:02:08 -07005409 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005410 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
5411 struct binder_ref *ref = rb_entry(n, struct binder_ref,
5412 rb_node_desc);
5413 count++;
Todd Kjos372e3142017-06-29 12:01:58 -07005414 strong += ref->data.strong;
5415 weak += ref->data.weak;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005416 }
Todd Kjos2c1838d2017-06-29 12:02:08 -07005417 binder_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005418 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005419
Todd Kjos19c98722017-06-29 12:01:40 -07005420 count = binder_alloc_get_allocated_count(&proc->alloc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005421 seq_printf(m, " buffers: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005422
Sherry Yang8ef46652017-08-31 11:56:36 -07005423 binder_alloc_print_pages(m, &proc->alloc);
5424
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005425 count = 0;
Todd Kjos72196392017-06-29 12:02:02 -07005426 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005427 list_for_each_entry(w, &proc->todo, entry) {
Todd Kjos72196392017-06-29 12:02:02 -07005428 if (w->type == BINDER_WORK_TRANSACTION)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005429 count++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005430 }
Todd Kjos72196392017-06-29 12:02:02 -07005431 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005432 seq_printf(m, " pending transactions: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005433
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005434 print_binder_stats(m, " ", &proc->stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005435}
5436
5437
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005438static int binder_state_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005439{
5440 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005441 struct binder_node *node;
Todd Kjos673068e2017-06-29 12:02:03 -07005442 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005443
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005444 seq_puts(m, "binder state:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005445
Todd Kjosc44b1232017-06-29 12:01:43 -07005446 spin_lock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005447 if (!hlist_empty(&binder_dead_nodes))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005448 seq_puts(m, "dead nodes:\n");
Todd Kjos673068e2017-06-29 12:02:03 -07005449 hlist_for_each_entry(node, &binder_dead_nodes, dead_node) {
5450 /*
5451 * take a temporary reference on the node so it
5452 * survives and isn't removed from the list
5453 * while we print it.
5454 */
5455 node->tmp_refs++;
5456 spin_unlock(&binder_dead_nodes_lock);
5457 if (last_node)
5458 binder_put_node(last_node);
5459 binder_node_lock(node);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07005460 print_binder_node_nilocked(m, node);
Todd Kjos673068e2017-06-29 12:02:03 -07005461 binder_node_unlock(node);
5462 last_node = node;
5463 spin_lock(&binder_dead_nodes_lock);
5464 }
Todd Kjosc44b1232017-06-29 12:01:43 -07005465 spin_unlock(&binder_dead_nodes_lock);
Todd Kjos673068e2017-06-29 12:02:03 -07005466 if (last_node)
5467 binder_put_node(last_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005468
Todd Kjosc44b1232017-06-29 12:01:43 -07005469 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005470 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005471 print_binder_proc(m, proc, 1);
Todd Kjosc44b1232017-06-29 12:01:43 -07005472 mutex_unlock(&binder_procs_lock);
Todd Kjosa60b8902017-06-29 12:02:11 -07005473
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005474 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005475}
5476
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005477static int binder_stats_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005478{
5479 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005480
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005481 seq_puts(m, "binder stats:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005482
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005483 print_binder_stats(m, "", &binder_stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005484
Todd Kjosc44b1232017-06-29 12:01:43 -07005485 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005486 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005487 print_binder_proc_stats(m, proc);
Todd Kjosc44b1232017-06-29 12:01:43 -07005488 mutex_unlock(&binder_procs_lock);
Todd Kjosa60b8902017-06-29 12:02:11 -07005489
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005490 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005491}
5492
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005493static int binder_transactions_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005494{
5495 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005496
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005497 seq_puts(m, "binder transactions:\n");
Todd Kjosc44b1232017-06-29 12:01:43 -07005498 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005499 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005500 print_binder_proc(m, proc, 0);
Todd Kjosc44b1232017-06-29 12:01:43 -07005501 mutex_unlock(&binder_procs_lock);
Todd Kjosa60b8902017-06-29 12:02:11 -07005502
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005503 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005504}
5505
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005506static int binder_proc_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005507{
Riley Andrews83050a42016-02-09 21:05:33 -08005508 struct binder_proc *itr;
Martijn Coenen14db3182017-02-03 14:40:47 -08005509 int pid = (unsigned long)m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005510
Todd Kjosc44b1232017-06-29 12:01:43 -07005511 mutex_lock(&binder_procs_lock);
Riley Andrews83050a42016-02-09 21:05:33 -08005512 hlist_for_each_entry(itr, &binder_procs, proc_node) {
Martijn Coenen14db3182017-02-03 14:40:47 -08005513 if (itr->pid == pid) {
5514 seq_puts(m, "binder proc state:\n");
5515 print_binder_proc(m, itr, 1);
Riley Andrews83050a42016-02-09 21:05:33 -08005516 }
5517 }
Todd Kjosc44b1232017-06-29 12:01:43 -07005518 mutex_unlock(&binder_procs_lock);
5519
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005520 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005521}
5522
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005523static void print_binder_transaction_log_entry(struct seq_file *m,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005524 struct binder_transaction_log_entry *e)
5525{
Todd Kjosd99c7332017-06-29 12:01:53 -07005526 int debug_id = READ_ONCE(e->debug_id_done);
5527 /*
5528 * read barrier to guarantee debug_id_done read before
5529 * we print the log values
5530 */
5531 smp_rmb();
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005532 seq_printf(m,
Todd Kjosd99c7332017-06-29 12:01:53 -07005533 "%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 -07005534 e->debug_id, (e->call_type == 2) ? "reply" :
5535 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
Martijn Coenen14db3182017-02-03 14:40:47 -08005536 e->from_thread, e->to_proc, e->to_thread, e->context_name,
Todd Kjos57ada2f2017-06-29 12:01:46 -07005537 e->to_node, e->target_handle, e->data_size, e->offsets_size,
5538 e->return_error, e->return_error_param,
5539 e->return_error_line);
Todd Kjosd99c7332017-06-29 12:01:53 -07005540 /*
5541 * read-barrier to guarantee read of debug_id_done after
5542 * done printing the fields of the entry
5543 */
5544 smp_rmb();
5545 seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ?
5546 "\n" : " (incomplete)\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005547}
5548
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005549static int binder_transaction_log_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005550{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005551 struct binder_transaction_log *log = m->private;
Todd Kjosd99c7332017-06-29 12:01:53 -07005552 unsigned int log_cur = atomic_read(&log->cur);
5553 unsigned int count;
5554 unsigned int cur;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005555 int i;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005556
Todd Kjosd99c7332017-06-29 12:01:53 -07005557 count = log_cur + 1;
5558 cur = count < ARRAY_SIZE(log->entry) && !log->full ?
5559 0 : count % ARRAY_SIZE(log->entry);
5560 if (count > ARRAY_SIZE(log->entry) || log->full)
5561 count = ARRAY_SIZE(log->entry);
5562 for (i = 0; i < count; i++) {
5563 unsigned int index = cur++ % ARRAY_SIZE(log->entry);
5564
5565 print_binder_transaction_log_entry(m, &log->entry[index]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005566 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005567 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005568}
5569
5570static const struct file_operations binder_fops = {
5571 .owner = THIS_MODULE,
5572 .poll = binder_poll,
5573 .unlocked_ioctl = binder_ioctl,
Arve Hjønnevågda498892014-02-21 14:40:26 -08005574 .compat_ioctl = binder_ioctl,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005575 .mmap = binder_mmap,
5576 .open = binder_open,
5577 .flush = binder_flush,
5578 .release = binder_release,
5579};
5580
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005581BINDER_DEBUG_ENTRY(state);
5582BINDER_DEBUG_ENTRY(stats);
5583BINDER_DEBUG_ENTRY(transactions);
5584BINDER_DEBUG_ENTRY(transaction_log);
5585
Martijn Coenenac4812c2017-02-03 14:40:48 -08005586static int __init init_binder_device(const char *name)
5587{
5588 int ret;
5589 struct binder_device *binder_device;
5590
5591 binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
5592 if (!binder_device)
5593 return -ENOMEM;
5594
5595 binder_device->miscdev.fops = &binder_fops;
5596 binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
5597 binder_device->miscdev.name = name;
5598
5599 binder_device->context.binder_context_mgr_uid = INVALID_UID;
5600 binder_device->context.name = name;
Todd Kjosc44b1232017-06-29 12:01:43 -07005601 mutex_init(&binder_device->context.context_mgr_node_lock);
Martijn Coenenac4812c2017-02-03 14:40:48 -08005602
5603 ret = misc_register(&binder_device->miscdev);
5604 if (ret < 0) {
5605 kfree(binder_device);
5606 return ret;
5607 }
5608
5609 hlist_add_head(&binder_device->hlist, &binder_devices);
5610
5611 return ret;
5612}
5613
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005614static int __init binder_init(void)
5615{
5616 int ret;
Christian Brauner22eb9472017-08-21 16:13:28 +02005617 char *device_name, *device_names, *device_tmp;
Martijn Coenenac4812c2017-02-03 14:40:48 -08005618 struct binder_device *device;
5619 struct hlist_node *tmp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005620
Tetsuo Handa533dfb22017-11-29 22:29:47 +09005621 ret = binder_alloc_shrinker_init();
5622 if (ret)
5623 return ret;
Sherry Yangf2517eb2017-08-23 08:46:42 -07005624
Todd Kjosd99c7332017-06-29 12:01:53 -07005625 atomic_set(&binder_transaction_log.cur, ~0U);
5626 atomic_set(&binder_transaction_log_failed.cur, ~0U);
5627
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005628 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
5629 if (binder_debugfs_dir_entry_root)
5630 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
5631 binder_debugfs_dir_entry_root);
Martijn Coenenac4812c2017-02-03 14:40:48 -08005632
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005633 if (binder_debugfs_dir_entry_root) {
5634 debugfs_create_file("state",
Harsh Shandilya21d02dd2017-12-22 19:37:02 +05305635 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005636 binder_debugfs_dir_entry_root,
5637 NULL,
5638 &binder_state_fops);
5639 debugfs_create_file("stats",
Harsh Shandilya21d02dd2017-12-22 19:37:02 +05305640 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005641 binder_debugfs_dir_entry_root,
5642 NULL,
5643 &binder_stats_fops);
5644 debugfs_create_file("transactions",
Harsh Shandilya21d02dd2017-12-22 19:37:02 +05305645 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005646 binder_debugfs_dir_entry_root,
5647 NULL,
5648 &binder_transactions_fops);
5649 debugfs_create_file("transaction_log",
Harsh Shandilya21d02dd2017-12-22 19:37:02 +05305650 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005651 binder_debugfs_dir_entry_root,
5652 &binder_transaction_log,
5653 &binder_transaction_log_fops);
5654 debugfs_create_file("failed_transaction_log",
Harsh Shandilya21d02dd2017-12-22 19:37:02 +05305655 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005656 binder_debugfs_dir_entry_root,
5657 &binder_transaction_log_failed,
5658 &binder_transaction_log_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005659 }
Martijn Coenenac4812c2017-02-03 14:40:48 -08005660
5661 /*
5662 * Copy the module_parameter string, because we don't want to
5663 * tokenize it in-place.
5664 */
5665 device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL);
5666 if (!device_names) {
5667 ret = -ENOMEM;
5668 goto err_alloc_device_names_failed;
5669 }
5670 strcpy(device_names, binder_devices_param);
5671
Christian Brauner22eb9472017-08-21 16:13:28 +02005672 device_tmp = device_names;
5673 while ((device_name = strsep(&device_tmp, ","))) {
Martijn Coenenac4812c2017-02-03 14:40:48 -08005674 ret = init_binder_device(device_name);
5675 if (ret)
5676 goto err_init_binder_device_failed;
5677 }
5678
5679 return ret;
5680
5681err_init_binder_device_failed:
5682 hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
5683 misc_deregister(&device->miscdev);
5684 hlist_del(&device->hlist);
5685 kfree(device);
5686 }
Christian Brauner22eb9472017-08-21 16:13:28 +02005687
5688 kfree(device_names);
5689
Martijn Coenenac4812c2017-02-03 14:40:48 -08005690err_alloc_device_names_failed:
5691 debugfs_remove_recursive(binder_debugfs_dir_entry_root);
5692
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005693 return ret;
5694}
5695
5696device_initcall(binder_init);
5697
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07005698#define CREATE_TRACE_POINTS
5699#include "binder_trace.h"
5700
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005701MODULE_LICENSE("GPL v2");