blob: b0f039500d808daed30ea25494b74fcbba333874 [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#ifdef CONFIG_ANDROID_BINDER_IPC_32BIT
76#define BINDER_IPC_32BIT 1
77#endif
78
79#include <uapi/linux/android/binder.h>
Todd Kjos0c972a02017-06-29 12:01:41 -070080#include "binder_alloc.h"
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -070081#include "binder_trace.h"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090082
Todd Kjosc44b1232017-06-29 12:01:43 -070083static HLIST_HEAD(binder_deferred_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090084static DEFINE_MUTEX(binder_deferred_lock);
85
Martijn Coenenac4812c2017-02-03 14:40:48 -080086static HLIST_HEAD(binder_devices);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090087static HLIST_HEAD(binder_procs);
Todd Kjosc44b1232017-06-29 12:01:43 -070088static DEFINE_MUTEX(binder_procs_lock);
89
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090090static HLIST_HEAD(binder_dead_nodes);
Todd Kjosc44b1232017-06-29 12:01:43 -070091static DEFINE_SPINLOCK(binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090092
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070093static struct dentry *binder_debugfs_dir_entry_root;
94static struct dentry *binder_debugfs_dir_entry_proc;
Todd Kjos656a8002017-06-29 12:01:45 -070095static atomic_t binder_last_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090096
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070097#define BINDER_DEBUG_ENTRY(name) \
98static int binder_##name##_open(struct inode *inode, struct file *file) \
99{ \
Arve Hjønnevåg16b66552009-04-28 20:57:50 -0700100 return single_open(file, binder_##name##_show, inode->i_private); \
Arve Hjønnevåg5249f482009-04-28 20:57:50 -0700101} \
102\
103static const struct file_operations binder_##name##_fops = { \
104 .owner = THIS_MODULE, \
105 .open = binder_##name##_open, \
106 .read = seq_read, \
107 .llseek = seq_lseek, \
108 .release = single_release, \
109}
110
111static int binder_proc_show(struct seq_file *m, void *unused);
112BINDER_DEBUG_ENTRY(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900113
114/* This is only defined in include/asm-arm/sizes.h */
115#ifndef SZ_1K
116#define SZ_1K 0x400
117#endif
118
119#ifndef SZ_4M
120#define SZ_4M 0x400000
121#endif
122
123#define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
124
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900125enum {
126 BINDER_DEBUG_USER_ERROR = 1U << 0,
127 BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1,
128 BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2,
129 BINDER_DEBUG_OPEN_CLOSE = 1U << 3,
130 BINDER_DEBUG_DEAD_BINDER = 1U << 4,
131 BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5,
132 BINDER_DEBUG_READ_WRITE = 1U << 6,
133 BINDER_DEBUG_USER_REFS = 1U << 7,
134 BINDER_DEBUG_THREADS = 1U << 8,
135 BINDER_DEBUG_TRANSACTION = 1U << 9,
136 BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10,
137 BINDER_DEBUG_FREE_BUFFER = 1U << 11,
138 BINDER_DEBUG_INTERNAL_REFS = 1U << 12,
Todd Kjos19c98722017-06-29 12:01:40 -0700139 BINDER_DEBUG_PRIORITY_CAP = 1U << 13,
Todd Kjos9630fe82017-06-29 12:02:00 -0700140 BINDER_DEBUG_SPINLOCKS = 1U << 14,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900141};
142static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR |
143 BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION;
144module_param_named(debug_mask, binder_debug_mask, uint, S_IWUSR | S_IRUGO);
145
Martijn Coenenac4812c2017-02-03 14:40:48 -0800146static char *binder_devices_param = CONFIG_ANDROID_BINDER_DEVICES;
147module_param_named(devices, binder_devices_param, charp, 0444);
148
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900149static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
150static int binder_stop_on_user_error;
151
152static int binder_set_stop_on_user_error(const char *val,
153 struct kernel_param *kp)
154{
155 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +0900156
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900157 ret = param_set_int(val, kp);
158 if (binder_stop_on_user_error < 2)
159 wake_up(&binder_user_error_wait);
160 return ret;
161}
162module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
163 param_get_int, &binder_stop_on_user_error, S_IWUSR | S_IRUGO);
164
165#define binder_debug(mask, x...) \
166 do { \
167 if (binder_debug_mask & mask) \
Sherwin Soltani258767f2012-06-26 02:00:30 -0400168 pr_info(x); \
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900169 } while (0)
170
171#define binder_user_error(x...) \
172 do { \
173 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
Sherwin Soltani258767f2012-06-26 02:00:30 -0400174 pr_info(x); \
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900175 if (binder_stop_on_user_error) \
176 binder_stop_on_user_error = 2; \
177 } while (0)
178
Martijn Coenenfeba3902017-02-03 14:40:45 -0800179#define to_flat_binder_object(hdr) \
180 container_of(hdr, struct flat_binder_object, hdr)
181
182#define to_binder_fd_object(hdr) container_of(hdr, struct binder_fd_object, hdr)
183
Martijn Coenen79802402017-02-03 14:40:51 -0800184#define to_binder_buffer_object(hdr) \
185 container_of(hdr, struct binder_buffer_object, hdr)
186
Martijn Coenendef95c72017-02-03 14:40:52 -0800187#define to_binder_fd_array_object(hdr) \
188 container_of(hdr, struct binder_fd_array_object, hdr)
189
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900190enum binder_stat_types {
191 BINDER_STAT_PROC,
192 BINDER_STAT_THREAD,
193 BINDER_STAT_NODE,
194 BINDER_STAT_REF,
195 BINDER_STAT_DEATH,
196 BINDER_STAT_TRANSACTION,
197 BINDER_STAT_TRANSACTION_COMPLETE,
198 BINDER_STAT_COUNT
199};
200
201struct binder_stats {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -0700202 atomic_t br[_IOC_NR(BR_FAILED_REPLY) + 1];
203 atomic_t bc[_IOC_NR(BC_REPLY_SG) + 1];
204 atomic_t obj_created[BINDER_STAT_COUNT];
205 atomic_t obj_deleted[BINDER_STAT_COUNT];
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900206};
207
208static struct binder_stats binder_stats;
209
210static inline void binder_stats_deleted(enum binder_stat_types type)
211{
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -0700212 atomic_inc(&binder_stats.obj_deleted[type]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900213}
214
215static inline void binder_stats_created(enum binder_stat_types type)
216{
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -0700217 atomic_inc(&binder_stats.obj_created[type]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900218}
219
220struct binder_transaction_log_entry {
221 int debug_id;
Todd Kjosd99c7332017-06-29 12:01:53 -0700222 int debug_id_done;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900223 int call_type;
224 int from_proc;
225 int from_thread;
226 int target_handle;
227 int to_proc;
228 int to_thread;
229 int to_node;
230 int data_size;
231 int offsets_size;
Todd Kjos57ada2f2017-06-29 12:01:46 -0700232 int return_error_line;
233 uint32_t return_error;
234 uint32_t return_error_param;
Martijn Coenen14db3182017-02-03 14:40:47 -0800235 const char *context_name;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900236};
237struct binder_transaction_log {
Todd Kjosd99c7332017-06-29 12:01:53 -0700238 atomic_t cur;
239 bool full;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900240 struct binder_transaction_log_entry entry[32];
241};
242static struct binder_transaction_log binder_transaction_log;
243static struct binder_transaction_log binder_transaction_log_failed;
244
245static struct binder_transaction_log_entry *binder_transaction_log_add(
246 struct binder_transaction_log *log)
247{
248 struct binder_transaction_log_entry *e;
Todd Kjosd99c7332017-06-29 12:01:53 -0700249 unsigned int cur = atomic_inc_return(&log->cur);
Seunghun Lee10f62862014-05-01 01:30:23 +0900250
Todd Kjosd99c7332017-06-29 12:01:53 -0700251 if (cur >= ARRAY_SIZE(log->entry))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900252 log->full = 1;
Todd Kjosd99c7332017-06-29 12:01:53 -0700253 e = &log->entry[cur % ARRAY_SIZE(log->entry)];
254 WRITE_ONCE(e->debug_id_done, 0);
255 /*
256 * write-barrier to synchronize access to e->debug_id_done.
257 * We make sure the initialized 0 value is seen before
258 * memset() other fields are zeroed by memset.
259 */
260 smp_wmb();
261 memset(e, 0, sizeof(*e));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900262 return e;
263}
264
Martijn Coenen342e5c92017-02-03 14:40:46 -0800265struct binder_context {
266 struct binder_node *binder_context_mgr_node;
Todd Kjosc44b1232017-06-29 12:01:43 -0700267 struct mutex context_mgr_node_lock;
268
Martijn Coenen342e5c92017-02-03 14:40:46 -0800269 kuid_t binder_context_mgr_uid;
Martijn Coenen14db3182017-02-03 14:40:47 -0800270 const char *name;
Martijn Coenen342e5c92017-02-03 14:40:46 -0800271};
272
Martijn Coenenac4812c2017-02-03 14:40:48 -0800273struct binder_device {
274 struct hlist_node hlist;
275 struct miscdevice miscdev;
276 struct binder_context context;
Martijn Coenen342e5c92017-02-03 14:40:46 -0800277};
278
Todd Kjos72196392017-06-29 12:02:02 -0700279/**
280 * struct binder_work - work enqueued on a worklist
281 * @entry: node enqueued on list
282 * @type: type of work to be performed
283 *
284 * There are separate work lists for proc, thread, and node (async).
285 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900286struct binder_work {
287 struct list_head entry;
Todd Kjos72196392017-06-29 12:02:02 -0700288
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900289 enum {
290 BINDER_WORK_TRANSACTION = 1,
291 BINDER_WORK_TRANSACTION_COMPLETE,
Todd Kjos26549d12017-06-29 12:01:55 -0700292 BINDER_WORK_RETURN_ERROR,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900293 BINDER_WORK_NODE,
294 BINDER_WORK_DEAD_BINDER,
295 BINDER_WORK_DEAD_BINDER_AND_CLEAR,
296 BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
297 } type;
298};
299
Todd Kjos26549d12017-06-29 12:01:55 -0700300struct binder_error {
301 struct binder_work work;
302 uint32_t cmd;
303};
304
Todd Kjos9630fe82017-06-29 12:02:00 -0700305/**
306 * struct binder_node - binder node bookkeeping
307 * @debug_id: unique ID for debugging
308 * (invariant after initialized)
309 * @lock: lock for node fields
310 * @work: worklist element for node work
Todd Kjos72196392017-06-29 12:02:02 -0700311 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700312 * @rb_node: element for proc->nodes tree
Todd Kjosda0fa9e2017-06-29 12:02:04 -0700313 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700314 * @dead_node: element for binder_dead_nodes list
315 * (protected by binder_dead_nodes_lock)
316 * @proc: binder_proc that owns this node
317 * (invariant after initialized)
318 * @refs: list of references on this node
Todd Kjos673068e2017-06-29 12:02:03 -0700319 * (protected by @lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700320 * @internal_strong_refs: used to take strong references when
321 * initiating a transaction
Todd Kjosed297212017-06-29 12:02:01 -0700322 * (protected by @proc->inner_lock if @proc
323 * and by @lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700324 * @local_weak_refs: weak user refs from local process
Todd Kjosed297212017-06-29 12:02:01 -0700325 * (protected by @proc->inner_lock if @proc
326 * and by @lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700327 * @local_strong_refs: strong user refs from local process
Todd Kjosed297212017-06-29 12:02:01 -0700328 * (protected by @proc->inner_lock if @proc
329 * and by @lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700330 * @tmp_refs: temporary kernel refs
Todd Kjosed297212017-06-29 12:02:01 -0700331 * (protected by @proc->inner_lock while @proc
332 * is valid, and by binder_dead_nodes_lock
333 * if @proc is NULL. During inc/dec and node release
334 * it is also protected by @lock to provide safety
335 * as the node dies and @proc becomes NULL)
Todd Kjos9630fe82017-06-29 12:02:00 -0700336 * @ptr: userspace pointer for node
337 * (invariant, no lock needed)
338 * @cookie: userspace cookie for node
339 * (invariant, no lock needed)
340 * @has_strong_ref: userspace notified of strong ref
Todd Kjosed297212017-06-29 12:02:01 -0700341 * (protected by @proc->inner_lock if @proc
342 * and by @lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700343 * @pending_strong_ref: userspace has acked notification of strong ref
Todd Kjosed297212017-06-29 12:02:01 -0700344 * (protected by @proc->inner_lock if @proc
345 * and by @lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700346 * @has_weak_ref: userspace notified of weak ref
Todd Kjosed297212017-06-29 12:02:01 -0700347 * (protected by @proc->inner_lock if @proc
348 * and by @lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700349 * @pending_weak_ref: userspace has acked notification of weak ref
Todd Kjosed297212017-06-29 12:02:01 -0700350 * (protected by @proc->inner_lock if @proc
351 * and by @lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700352 * @has_async_transaction: async transaction to node in progress
Todd Kjos673068e2017-06-29 12:02:03 -0700353 * (protected by @lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700354 * @accept_fds: file descriptor operations supported for node
355 * (invariant after initialized)
356 * @min_priority: minimum scheduling priority
357 * (invariant after initialized)
358 * @async_todo: list of async work items
Todd Kjos72196392017-06-29 12:02:02 -0700359 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700360 *
361 * Bookkeeping structure for binder nodes.
362 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900363struct binder_node {
364 int debug_id;
Todd Kjos9630fe82017-06-29 12:02:00 -0700365 spinlock_t lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900366 struct binder_work work;
367 union {
368 struct rb_node rb_node;
369 struct hlist_node dead_node;
370 };
371 struct binder_proc *proc;
372 struct hlist_head refs;
373 int internal_strong_refs;
374 int local_weak_refs;
375 int local_strong_refs;
Todd Kjosadc18842017-06-29 12:01:59 -0700376 int tmp_refs;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800377 binder_uintptr_t ptr;
378 binder_uintptr_t cookie;
Todd Kjosed297212017-06-29 12:02:01 -0700379 struct {
380 /*
381 * bitfield elements protected by
382 * proc inner_lock
383 */
384 u8 has_strong_ref:1;
385 u8 pending_strong_ref:1;
386 u8 has_weak_ref:1;
387 u8 pending_weak_ref:1;
388 };
389 struct {
390 /*
391 * invariant after initialization
392 */
393 u8 accept_fds:1;
394 u8 min_priority;
395 };
396 bool has_async_transaction;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900397 struct list_head async_todo;
398};
399
400struct binder_ref_death {
Todd Kjos72196392017-06-29 12:02:02 -0700401 /**
402 * @work: worklist element for death notifications
403 * (protected by inner_lock of the proc that
404 * this ref belongs to)
405 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900406 struct binder_work work;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800407 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900408};
409
Todd Kjos372e3142017-06-29 12:01:58 -0700410/**
411 * struct binder_ref_data - binder_ref counts and id
412 * @debug_id: unique ID for the ref
413 * @desc: unique userspace handle for ref
414 * @strong: strong ref count (debugging only if not locked)
415 * @weak: weak ref count (debugging only if not locked)
416 *
417 * Structure to hold ref count and ref id information. Since
418 * the actual ref can only be accessed with a lock, this structure
419 * is used to return information about the ref to callers of
420 * ref inc/dec functions.
421 */
422struct binder_ref_data {
423 int debug_id;
424 uint32_t desc;
425 int strong;
426 int weak;
427};
428
429/**
430 * struct binder_ref - struct to track references on nodes
431 * @data: binder_ref_data containing id, handle, and current refcounts
432 * @rb_node_desc: node for lookup by @data.desc in proc's rb_tree
433 * @rb_node_node: node for lookup by @node in proc's rb_tree
434 * @node_entry: list entry for node->refs list in target node
Todd Kjos673068e2017-06-29 12:02:03 -0700435 * (protected by @node->lock)
Todd Kjos372e3142017-06-29 12:01:58 -0700436 * @proc: binder_proc containing ref
437 * @node: binder_node of target node. When cleaning up a
438 * ref for deletion in binder_cleanup_ref, a non-NULL
439 * @node indicates the node must be freed
440 * @death: pointer to death notification (ref_death) if requested
Martijn Coenenab51ec62017-06-29 12:02:10 -0700441 * (protected by @node->lock)
Todd Kjos372e3142017-06-29 12:01:58 -0700442 *
443 * Structure to track references from procA to target node (on procB). This
444 * structure is unsafe to access without holding @proc->outer_lock.
445 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900446struct binder_ref {
447 /* Lookups needed: */
448 /* node + proc => ref (transaction) */
449 /* desc + proc => ref (transaction, inc/dec ref) */
450 /* node => refs + procs (proc exit) */
Todd Kjos372e3142017-06-29 12:01:58 -0700451 struct binder_ref_data data;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900452 struct rb_node rb_node_desc;
453 struct rb_node rb_node_node;
454 struct hlist_node node_entry;
455 struct binder_proc *proc;
456 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900457 struct binder_ref_death *death;
458};
459
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900460enum binder_deferred_state {
461 BINDER_DEFERRED_PUT_FILES = 0x01,
462 BINDER_DEFERRED_FLUSH = 0x02,
463 BINDER_DEFERRED_RELEASE = 0x04,
464};
465
Todd Kjos9630fe82017-06-29 12:02:00 -0700466/**
467 * struct binder_proc - binder process bookkeeping
468 * @proc_node: element for binder_procs list
469 * @threads: rbtree of binder_threads in this proc
Todd Kjos7bd7b0e2017-06-29 12:02:05 -0700470 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700471 * @nodes: rbtree of binder nodes associated with
472 * this proc ordered by node->ptr
Todd Kjosda0fa9e2017-06-29 12:02:04 -0700473 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700474 * @refs_by_desc: rbtree of refs ordered by ref->desc
Todd Kjos2c1838d2017-06-29 12:02:08 -0700475 * (protected by @outer_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700476 * @refs_by_node: rbtree of refs ordered by ref->node
Todd Kjos2c1838d2017-06-29 12:02:08 -0700477 * (protected by @outer_lock)
Martijn Coenen1b77e9d2017-08-31 10:04:18 +0200478 * @waiting_threads: threads currently waiting for proc work
479 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700480 * @pid PID of group_leader of process
481 * (invariant after initialized)
482 * @tsk task_struct for group_leader of process
483 * (invariant after initialized)
484 * @files files_struct for process
485 * (invariant after initialized)
486 * @deferred_work_node: element for binder_deferred_list
487 * (protected by binder_deferred_lock)
488 * @deferred_work: bitmap of deferred work to perform
489 * (protected by binder_deferred_lock)
490 * @is_dead: process is dead and awaiting free
491 * when outstanding transactions are cleaned up
Todd Kjos7bd7b0e2017-06-29 12:02:05 -0700492 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700493 * @todo: list of work for this process
Todd Kjos72196392017-06-29 12:02:02 -0700494 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700495 * @wait: wait queue head to wait for proc work
496 * (invariant after initialized)
497 * @stats: per-process binder statistics
498 * (atomics, no lock needed)
499 * @delivered_death: list of delivered death notification
Todd Kjos72196392017-06-29 12:02:02 -0700500 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700501 * @max_threads: cap on number of binder threads
Todd Kjosb3e68612017-06-29 12:02:07 -0700502 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700503 * @requested_threads: number of binder threads requested but not
504 * yet started. In current implementation, can
505 * only be 0 or 1.
Todd Kjosb3e68612017-06-29 12:02:07 -0700506 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700507 * @requested_threads_started: number binder threads started
Todd Kjosb3e68612017-06-29 12:02:07 -0700508 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700509 * @tmp_ref: temporary reference to indicate proc is in use
Todd Kjos7bd7b0e2017-06-29 12:02:05 -0700510 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700511 * @default_priority: default scheduler priority
512 * (invariant after initialized)
513 * @debugfs_entry: debugfs node
514 * @alloc: binder allocator bookkeeping
515 * @context: binder_context for this proc
516 * (invariant after initialized)
517 * @inner_lock: can nest under outer_lock and/or node lock
518 * @outer_lock: no nesting under innor or node lock
519 * Lock order: 1) outer, 2) node, 3) inner
520 *
521 * Bookkeeping structure for binder processes
522 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900523struct binder_proc {
524 struct hlist_node proc_node;
525 struct rb_root threads;
526 struct rb_root nodes;
527 struct rb_root refs_by_desc;
528 struct rb_root refs_by_node;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +0200529 struct list_head waiting_threads;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900530 int pid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900531 struct task_struct *tsk;
532 struct files_struct *files;
533 struct hlist_node deferred_work_node;
534 int deferred_work;
Todd Kjos7a4408c2017-06-29 12:01:57 -0700535 bool is_dead;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900536
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900537 struct list_head todo;
538 wait_queue_head_t wait;
539 struct binder_stats stats;
540 struct list_head delivered_death;
541 int max_threads;
542 int requested_threads;
543 int requested_threads_started;
Todd Kjos7a4408c2017-06-29 12:01:57 -0700544 int tmp_ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900545 long default_priority;
Arve Hjønnevåg16b66552009-04-28 20:57:50 -0700546 struct dentry *debugfs_entry;
Todd Kjosfdfb4a92017-06-29 12:01:38 -0700547 struct binder_alloc alloc;
Martijn Coenen342e5c92017-02-03 14:40:46 -0800548 struct binder_context *context;
Todd Kjos9630fe82017-06-29 12:02:00 -0700549 spinlock_t inner_lock;
550 spinlock_t outer_lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900551};
552
553enum {
554 BINDER_LOOPER_STATE_REGISTERED = 0x01,
555 BINDER_LOOPER_STATE_ENTERED = 0x02,
556 BINDER_LOOPER_STATE_EXITED = 0x04,
557 BINDER_LOOPER_STATE_INVALID = 0x08,
558 BINDER_LOOPER_STATE_WAITING = 0x10,
Martijn Coenen1b77e9d2017-08-31 10:04:18 +0200559 BINDER_LOOPER_STATE_POLL = 0x20,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900560};
561
Todd Kjos9630fe82017-06-29 12:02:00 -0700562/**
563 * struct binder_thread - binder thread bookkeeping
564 * @proc: binder process for this thread
565 * (invariant after initialization)
566 * @rb_node: element for proc->threads rbtree
Todd Kjos7bd7b0e2017-06-29 12:02:05 -0700567 * (protected by @proc->inner_lock)
Martijn Coenen1b77e9d2017-08-31 10:04:18 +0200568 * @waiting_thread_node: element for @proc->waiting_threads list
569 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700570 * @pid: PID for this thread
571 * (invariant after initialization)
572 * @looper: bitmap of looping state
573 * (only accessed by this thread)
574 * @looper_needs_return: looping thread needs to exit driver
575 * (no lock needed)
576 * @transaction_stack: stack of in-progress transactions for this thread
Martijn Coenen0b89d692017-06-29 12:02:06 -0700577 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700578 * @todo: list of work to do for this thread
Todd Kjos72196392017-06-29 12:02:02 -0700579 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700580 * @return_error: transaction errors reported by this thread
581 * (only accessed by this thread)
582 * @reply_error: transaction errors reported by target thread
Martijn Coenen0b89d692017-06-29 12:02:06 -0700583 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700584 * @wait: wait queue for thread work
585 * @stats: per-thread statistics
586 * (atomics, no lock needed)
587 * @tmp_ref: temporary reference to indicate thread is in use
588 * (atomic since @proc->inner_lock cannot
589 * always be acquired)
590 * @is_dead: thread is dead and awaiting free
591 * when outstanding transactions are cleaned up
Todd Kjos7bd7b0e2017-06-29 12:02:05 -0700592 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700593 *
594 * Bookkeeping structure for binder threads.
595 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900596struct binder_thread {
597 struct binder_proc *proc;
598 struct rb_node rb_node;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +0200599 struct list_head waiting_thread_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900600 int pid;
Todd Kjos08dabce2017-06-29 12:01:49 -0700601 int looper; /* only modified by this thread */
602 bool looper_need_return; /* can be written by other thread */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900603 struct binder_transaction *transaction_stack;
604 struct list_head todo;
Todd Kjos26549d12017-06-29 12:01:55 -0700605 struct binder_error return_error;
606 struct binder_error reply_error;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900607 wait_queue_head_t wait;
608 struct binder_stats stats;
Todd Kjos7a4408c2017-06-29 12:01:57 -0700609 atomic_t tmp_ref;
610 bool is_dead;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900611};
612
613struct binder_transaction {
614 int debug_id;
615 struct binder_work work;
616 struct binder_thread *from;
617 struct binder_transaction *from_parent;
618 struct binder_proc *to_proc;
619 struct binder_thread *to_thread;
620 struct binder_transaction *to_parent;
621 unsigned need_reply:1;
622 /* unsigned is_dead:1; */ /* not used at the moment */
623
624 struct binder_buffer *buffer;
625 unsigned int code;
626 unsigned int flags;
627 long priority;
628 long saved_priority;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -0600629 kuid_t sender_euid;
Todd Kjos7a4408c2017-06-29 12:01:57 -0700630 /**
631 * @lock: protects @from, @to_proc, and @to_thread
632 *
633 * @from, @to_proc, and @to_thread can be set to NULL
634 * during thread teardown
635 */
636 spinlock_t lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900637};
638
Todd Kjos9630fe82017-06-29 12:02:00 -0700639/**
640 * binder_proc_lock() - Acquire outer lock for given binder_proc
641 * @proc: struct binder_proc to acquire
642 *
643 * Acquires proc->outer_lock. Used to protect binder_ref
644 * structures associated with the given proc.
645 */
646#define binder_proc_lock(proc) _binder_proc_lock(proc, __LINE__)
647static void
648_binder_proc_lock(struct binder_proc *proc, int line)
649{
650 binder_debug(BINDER_DEBUG_SPINLOCKS,
651 "%s: line=%d\n", __func__, line);
652 spin_lock(&proc->outer_lock);
653}
654
655/**
656 * binder_proc_unlock() - Release spinlock for given binder_proc
657 * @proc: struct binder_proc to acquire
658 *
659 * Release lock acquired via binder_proc_lock()
660 */
661#define binder_proc_unlock(_proc) _binder_proc_unlock(_proc, __LINE__)
662static void
663_binder_proc_unlock(struct binder_proc *proc, int line)
664{
665 binder_debug(BINDER_DEBUG_SPINLOCKS,
666 "%s: line=%d\n", __func__, line);
667 spin_unlock(&proc->outer_lock);
668}
669
670/**
671 * binder_inner_proc_lock() - Acquire inner lock for given binder_proc
672 * @proc: struct binder_proc to acquire
673 *
674 * Acquires proc->inner_lock. Used to protect todo lists
675 */
676#define binder_inner_proc_lock(proc) _binder_inner_proc_lock(proc, __LINE__)
677static void
678_binder_inner_proc_lock(struct binder_proc *proc, int line)
679{
680 binder_debug(BINDER_DEBUG_SPINLOCKS,
681 "%s: line=%d\n", __func__, line);
682 spin_lock(&proc->inner_lock);
683}
684
685/**
686 * binder_inner_proc_unlock() - Release inner lock for given binder_proc
687 * @proc: struct binder_proc to acquire
688 *
689 * Release lock acquired via binder_inner_proc_lock()
690 */
691#define binder_inner_proc_unlock(proc) _binder_inner_proc_unlock(proc, __LINE__)
692static void
693_binder_inner_proc_unlock(struct binder_proc *proc, int line)
694{
695 binder_debug(BINDER_DEBUG_SPINLOCKS,
696 "%s: line=%d\n", __func__, line);
697 spin_unlock(&proc->inner_lock);
698}
699
700/**
701 * binder_node_lock() - Acquire spinlock for given binder_node
702 * @node: struct binder_node to acquire
703 *
704 * Acquires node->lock. Used to protect binder_node fields
705 */
706#define binder_node_lock(node) _binder_node_lock(node, __LINE__)
707static void
708_binder_node_lock(struct binder_node *node, int line)
709{
710 binder_debug(BINDER_DEBUG_SPINLOCKS,
711 "%s: line=%d\n", __func__, line);
712 spin_lock(&node->lock);
713}
714
715/**
716 * binder_node_unlock() - Release spinlock for given binder_proc
717 * @node: struct binder_node to acquire
718 *
719 * Release lock acquired via binder_node_lock()
720 */
721#define binder_node_unlock(node) _binder_node_unlock(node, __LINE__)
722static void
723_binder_node_unlock(struct binder_node *node, int line)
724{
725 binder_debug(BINDER_DEBUG_SPINLOCKS,
726 "%s: line=%d\n", __func__, line);
727 spin_unlock(&node->lock);
728}
729
Todd Kjos673068e2017-06-29 12:02:03 -0700730/**
731 * binder_node_inner_lock() - Acquire node and inner locks
732 * @node: struct binder_node to acquire
733 *
734 * Acquires node->lock. If node->proc also acquires
735 * proc->inner_lock. Used to protect binder_node fields
736 */
737#define binder_node_inner_lock(node) _binder_node_inner_lock(node, __LINE__)
738static void
739_binder_node_inner_lock(struct binder_node *node, int line)
740{
741 binder_debug(BINDER_DEBUG_SPINLOCKS,
742 "%s: line=%d\n", __func__, line);
743 spin_lock(&node->lock);
744 if (node->proc)
745 binder_inner_proc_lock(node->proc);
746}
747
748/**
749 * binder_node_unlock() - Release node and inner locks
750 * @node: struct binder_node to acquire
751 *
752 * Release lock acquired via binder_node_lock()
753 */
754#define binder_node_inner_unlock(node) _binder_node_inner_unlock(node, __LINE__)
755static void
756_binder_node_inner_unlock(struct binder_node *node, int line)
757{
758 struct binder_proc *proc = node->proc;
759
760 binder_debug(BINDER_DEBUG_SPINLOCKS,
761 "%s: line=%d\n", __func__, line);
762 if (proc)
763 binder_inner_proc_unlock(proc);
764 spin_unlock(&node->lock);
765}
766
Todd Kjos72196392017-06-29 12:02:02 -0700767static bool binder_worklist_empty_ilocked(struct list_head *list)
768{
769 return list_empty(list);
770}
771
772/**
773 * binder_worklist_empty() - Check if no items on the work list
774 * @proc: binder_proc associated with list
775 * @list: list to check
776 *
777 * Return: true if there are no items on list, else false
778 */
779static bool binder_worklist_empty(struct binder_proc *proc,
780 struct list_head *list)
781{
782 bool ret;
783
784 binder_inner_proc_lock(proc);
785 ret = binder_worklist_empty_ilocked(list);
786 binder_inner_proc_unlock(proc);
787 return ret;
788}
789
790static void
791binder_enqueue_work_ilocked(struct binder_work *work,
792 struct list_head *target_list)
793{
794 BUG_ON(target_list == NULL);
795 BUG_ON(work->entry.next && !list_empty(&work->entry));
796 list_add_tail(&work->entry, target_list);
797}
798
799/**
800 * binder_enqueue_work() - Add an item to the work list
801 * @proc: binder_proc associated with list
802 * @work: struct binder_work to add to list
803 * @target_list: list to add work to
804 *
805 * Adds the work to the specified list. Asserts that work
806 * is not already on a list.
807 */
808static void
809binder_enqueue_work(struct binder_proc *proc,
810 struct binder_work *work,
811 struct list_head *target_list)
812{
813 binder_inner_proc_lock(proc);
814 binder_enqueue_work_ilocked(work, target_list);
815 binder_inner_proc_unlock(proc);
816}
817
818static void
819binder_dequeue_work_ilocked(struct binder_work *work)
820{
821 list_del_init(&work->entry);
822}
823
824/**
825 * binder_dequeue_work() - Removes an item from the work list
826 * @proc: binder_proc associated with list
827 * @work: struct binder_work to remove from list
828 *
829 * Removes the specified work item from whatever list it is on.
830 * Can safely be called if work is not on any list.
831 */
832static void
833binder_dequeue_work(struct binder_proc *proc, struct binder_work *work)
834{
835 binder_inner_proc_lock(proc);
836 binder_dequeue_work_ilocked(work);
837 binder_inner_proc_unlock(proc);
838}
839
840static struct binder_work *binder_dequeue_work_head_ilocked(
841 struct list_head *list)
842{
843 struct binder_work *w;
844
845 w = list_first_entry_or_null(list, struct binder_work, entry);
846 if (w)
847 list_del_init(&w->entry);
848 return w;
849}
850
851/**
852 * binder_dequeue_work_head() - Dequeues the item at head of list
853 * @proc: binder_proc associated with list
854 * @list: list to dequeue head
855 *
856 * Removes the head of the list if there are items on the list
857 *
858 * Return: pointer dequeued binder_work, NULL if list was empty
859 */
860static struct binder_work *binder_dequeue_work_head(
861 struct binder_proc *proc,
862 struct list_head *list)
863{
864 struct binder_work *w;
865
866 binder_inner_proc_lock(proc);
867 w = binder_dequeue_work_head_ilocked(list);
868 binder_inner_proc_unlock(proc);
869 return w;
870}
871
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900872static void
873binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
Todd Kjos7a4408c2017-06-29 12:01:57 -0700874static void binder_free_thread(struct binder_thread *thread);
875static void binder_free_proc(struct binder_proc *proc);
Todd Kjosda0fa9e2017-06-29 12:02:04 -0700876static void binder_inc_node_tmpref_ilocked(struct binder_node *node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900877
Sachin Kamatefde99c2012-08-17 16:39:36 +0530878static int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900879{
880 struct files_struct *files = proc->files;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900881 unsigned long rlim_cur;
882 unsigned long irqs;
883
884 if (files == NULL)
885 return -ESRCH;
886
Al Virodcfadfa2012-08-12 17:27:30 -0400887 if (!lock_task_sighand(proc->tsk, &irqs))
888 return -EMFILE;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900889
Al Virodcfadfa2012-08-12 17:27:30 -0400890 rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
891 unlock_task_sighand(proc->tsk, &irqs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900892
Al Virodcfadfa2012-08-12 17:27:30 -0400893 return __alloc_fd(files, 0, rlim_cur, flags);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900894}
895
896/*
897 * copied from fd_install
898 */
899static void task_fd_install(
900 struct binder_proc *proc, unsigned int fd, struct file *file)
901{
Al Virof869e8a2012-08-15 21:06:33 -0400902 if (proc->files)
903 __fd_install(proc->files, fd, file);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900904}
905
906/*
907 * copied from sys_close
908 */
909static long task_close_fd(struct binder_proc *proc, unsigned int fd)
910{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900911 int retval;
912
Al Viro483ce1d2012-08-19 12:04:24 -0400913 if (proc->files == NULL)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900914 return -ESRCH;
915
Al Viro483ce1d2012-08-19 12:04:24 -0400916 retval = __close_fd(proc->files, fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900917 /* can't restart close syscall because file table entry was cleared */
918 if (unlikely(retval == -ERESTARTSYS ||
919 retval == -ERESTARTNOINTR ||
920 retval == -ERESTARTNOHAND ||
921 retval == -ERESTART_RESTARTBLOCK))
922 retval = -EINTR;
923
924 return retval;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900925}
926
Martijn Coenen1b77e9d2017-08-31 10:04:18 +0200927static bool binder_has_work_ilocked(struct binder_thread *thread,
928 bool do_proc_work)
929{
930 return !binder_worklist_empty_ilocked(&thread->todo) ||
931 thread->looper_need_return ||
932 (do_proc_work &&
933 !binder_worklist_empty_ilocked(&thread->proc->todo));
934}
935
936static bool binder_has_work(struct binder_thread *thread, bool do_proc_work)
937{
938 bool has_work;
939
940 binder_inner_proc_lock(thread->proc);
941 has_work = binder_has_work_ilocked(thread, do_proc_work);
942 binder_inner_proc_unlock(thread->proc);
943
944 return has_work;
945}
946
947static bool binder_available_for_proc_work_ilocked(struct binder_thread *thread)
948{
949 return !thread->transaction_stack &&
950 binder_worklist_empty_ilocked(&thread->todo) &&
951 (thread->looper & (BINDER_LOOPER_STATE_ENTERED |
952 BINDER_LOOPER_STATE_REGISTERED));
953}
954
955static void binder_wakeup_poll_threads_ilocked(struct binder_proc *proc,
956 bool sync)
957{
958 struct rb_node *n;
959 struct binder_thread *thread;
960
961 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
962 thread = rb_entry(n, struct binder_thread, rb_node);
963 if (thread->looper & BINDER_LOOPER_STATE_POLL &&
964 binder_available_for_proc_work_ilocked(thread)) {
965 if (sync)
966 wake_up_interruptible_sync(&thread->wait);
967 else
968 wake_up_interruptible(&thread->wait);
969 }
970 }
971}
972
Martijn Coenen408c68b2017-08-31 10:04:19 +0200973/**
974 * binder_select_thread_ilocked() - selects a thread for doing proc work.
975 * @proc: process to select a thread from
976 *
977 * Note that calling this function moves the thread off the waiting_threads
978 * list, so it can only be woken up by the caller of this function, or a
979 * signal. Therefore, callers *should* always wake up the thread this function
980 * returns.
981 *
982 * Return: If there's a thread currently waiting for process work,
983 * returns that thread. Otherwise returns NULL.
984 */
985static struct binder_thread *
986binder_select_thread_ilocked(struct binder_proc *proc)
Martijn Coenen1b77e9d2017-08-31 10:04:18 +0200987{
988 struct binder_thread *thread;
989
Martijn Coenen858b2712017-08-31 10:04:26 +0200990 assert_spin_locked(&proc->inner_lock);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +0200991 thread = list_first_entry_or_null(&proc->waiting_threads,
992 struct binder_thread,
993 waiting_thread_node);
994
Martijn Coenen408c68b2017-08-31 10:04:19 +0200995 if (thread)
Martijn Coenen1b77e9d2017-08-31 10:04:18 +0200996 list_del_init(&thread->waiting_thread_node);
Martijn Coenen408c68b2017-08-31 10:04:19 +0200997
998 return thread;
999}
1000
1001/**
1002 * binder_wakeup_thread_ilocked() - wakes up a thread for doing proc work.
1003 * @proc: process to wake up a thread in
1004 * @thread: specific thread to wake-up (may be NULL)
1005 * @sync: whether to do a synchronous wake-up
1006 *
1007 * This function wakes up a thread in the @proc process.
1008 * The caller may provide a specific thread to wake-up in
1009 * the @thread parameter. If @thread is NULL, this function
1010 * will wake up threads that have called poll().
1011 *
1012 * Note that for this function to work as expected, callers
1013 * should first call binder_select_thread() to find a thread
1014 * to handle the work (if they don't have a thread already),
1015 * and pass the result into the @thread parameter.
1016 */
1017static void binder_wakeup_thread_ilocked(struct binder_proc *proc,
1018 struct binder_thread *thread,
1019 bool sync)
1020{
Martijn Coenen858b2712017-08-31 10:04:26 +02001021 assert_spin_locked(&proc->inner_lock);
Martijn Coenen408c68b2017-08-31 10:04:19 +02001022
1023 if (thread) {
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02001024 if (sync)
1025 wake_up_interruptible_sync(&thread->wait);
1026 else
1027 wake_up_interruptible(&thread->wait);
1028 return;
1029 }
1030
1031 /* Didn't find a thread waiting for proc work; this can happen
1032 * in two scenarios:
1033 * 1. All threads are busy handling transactions
1034 * In that case, one of those threads should call back into
1035 * the kernel driver soon and pick up this work.
1036 * 2. Threads are using the (e)poll interface, in which case
1037 * they may be blocked on the waitqueue without having been
1038 * added to waiting_threads. For this case, we just iterate
1039 * over all threads not handling transaction work, and
1040 * wake them all up. We wake all because we don't know whether
1041 * a thread that called into (e)poll is handling non-binder
1042 * work currently.
1043 */
1044 binder_wakeup_poll_threads_ilocked(proc, sync);
1045}
1046
Martijn Coenen408c68b2017-08-31 10:04:19 +02001047static void binder_wakeup_proc_ilocked(struct binder_proc *proc)
1048{
1049 struct binder_thread *thread = binder_select_thread_ilocked(proc);
1050
1051 binder_wakeup_thread_ilocked(proc, thread, /* sync = */false);
1052}
1053
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001054static void binder_set_nice(long nice)
1055{
1056 long min_nice;
Seunghun Lee10f62862014-05-01 01:30:23 +09001057
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001058 if (can_nice(current, nice)) {
1059 set_user_nice(current, nice);
1060 return;
1061 }
Krzysztof Opasiakc3643b62017-07-05 19:24:44 +02001062 min_nice = rlimit_to_nice(rlimit(RLIMIT_NICE));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001063 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301064 "%d: nice value %ld not allowed use %ld instead\n",
1065 current->pid, nice, min_nice);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001066 set_user_nice(current, min_nice);
Dongsheng Yang8698a742014-03-11 18:09:12 +08001067 if (min_nice <= MAX_NICE)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001068 return;
Anmol Sarma56b468f2012-10-30 22:35:43 +05301069 binder_user_error("%d RLIMIT_NICE not set\n", current->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001070}
1071
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001072static struct binder_node *binder_get_node_ilocked(struct binder_proc *proc,
1073 binder_uintptr_t ptr)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001074{
1075 struct rb_node *n = proc->nodes.rb_node;
1076 struct binder_node *node;
1077
Martijn Coenen858b2712017-08-31 10:04:26 +02001078 assert_spin_locked(&proc->inner_lock);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001079
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001080 while (n) {
1081 node = rb_entry(n, struct binder_node, rb_node);
1082
1083 if (ptr < node->ptr)
1084 n = n->rb_left;
1085 else if (ptr > node->ptr)
1086 n = n->rb_right;
Todd Kjosadc18842017-06-29 12:01:59 -07001087 else {
1088 /*
1089 * take an implicit weak reference
1090 * to ensure node stays alive until
1091 * call to binder_put_node()
1092 */
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001093 binder_inc_node_tmpref_ilocked(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001094 return node;
Todd Kjosadc18842017-06-29 12:01:59 -07001095 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001096 }
1097 return NULL;
1098}
1099
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001100static struct binder_node *binder_get_node(struct binder_proc *proc,
1101 binder_uintptr_t ptr)
1102{
1103 struct binder_node *node;
1104
1105 binder_inner_proc_lock(proc);
1106 node = binder_get_node_ilocked(proc, ptr);
1107 binder_inner_proc_unlock(proc);
1108 return node;
1109}
1110
1111static struct binder_node *binder_init_node_ilocked(
1112 struct binder_proc *proc,
1113 struct binder_node *new_node,
1114 struct flat_binder_object *fp)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001115{
1116 struct rb_node **p = &proc->nodes.rb_node;
1117 struct rb_node *parent = NULL;
1118 struct binder_node *node;
Todd Kjos673068e2017-06-29 12:02:03 -07001119 binder_uintptr_t ptr = fp ? fp->binder : 0;
1120 binder_uintptr_t cookie = fp ? fp->cookie : 0;
1121 __u32 flags = fp ? fp->flags : 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001122
Martijn Coenen858b2712017-08-31 10:04:26 +02001123 assert_spin_locked(&proc->inner_lock);
1124
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001125 while (*p) {
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001126
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001127 parent = *p;
1128 node = rb_entry(parent, struct binder_node, rb_node);
1129
1130 if (ptr < node->ptr)
1131 p = &(*p)->rb_left;
1132 else if (ptr > node->ptr)
1133 p = &(*p)->rb_right;
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001134 else {
1135 /*
1136 * A matching node is already in
1137 * the rb tree. Abandon the init
1138 * and return it.
1139 */
1140 binder_inc_node_tmpref_ilocked(node);
1141 return node;
1142 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001143 }
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001144 node = new_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001145 binder_stats_created(BINDER_STAT_NODE);
Todd Kjosadc18842017-06-29 12:01:59 -07001146 node->tmp_refs++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001147 rb_link_node(&node->rb_node, parent, p);
1148 rb_insert_color(&node->rb_node, &proc->nodes);
Todd Kjos656a8002017-06-29 12:01:45 -07001149 node->debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001150 node->proc = proc;
1151 node->ptr = ptr;
1152 node->cookie = cookie;
1153 node->work.type = BINDER_WORK_NODE;
Todd Kjos673068e2017-06-29 12:02:03 -07001154 node->min_priority = flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
1155 node->accept_fds = !!(flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
Todd Kjos9630fe82017-06-29 12:02:00 -07001156 spin_lock_init(&node->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001157 INIT_LIST_HEAD(&node->work.entry);
1158 INIT_LIST_HEAD(&node->async_todo);
1159 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001160 "%d:%d node %d u%016llx c%016llx created\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001161 proc->pid, current->pid, node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001162 (u64)node->ptr, (u64)node->cookie);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001163
1164 return node;
1165}
1166
1167static struct binder_node *binder_new_node(struct binder_proc *proc,
1168 struct flat_binder_object *fp)
1169{
1170 struct binder_node *node;
1171 struct binder_node *new_node = kzalloc(sizeof(*node), GFP_KERNEL);
1172
1173 if (!new_node)
1174 return NULL;
1175 binder_inner_proc_lock(proc);
1176 node = binder_init_node_ilocked(proc, new_node, fp);
1177 binder_inner_proc_unlock(proc);
1178 if (node != new_node)
1179 /*
1180 * The node was already added by another thread
1181 */
1182 kfree(new_node);
1183
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001184 return node;
1185}
1186
Todd Kjosed297212017-06-29 12:02:01 -07001187static void binder_free_node(struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001188{
Todd Kjosed297212017-06-29 12:02:01 -07001189 kfree(node);
1190 binder_stats_deleted(BINDER_STAT_NODE);
1191}
1192
Todd Kjos673068e2017-06-29 12:02:03 -07001193static int binder_inc_node_nilocked(struct binder_node *node, int strong,
1194 int internal,
1195 struct list_head *target_list)
Todd Kjosed297212017-06-29 12:02:01 -07001196{
Todd Kjos673068e2017-06-29 12:02:03 -07001197 struct binder_proc *proc = node->proc;
1198
Martijn Coenen858b2712017-08-31 10:04:26 +02001199 assert_spin_locked(&node->lock);
Todd Kjos673068e2017-06-29 12:02:03 -07001200 if (proc)
Martijn Coenen858b2712017-08-31 10:04:26 +02001201 assert_spin_locked(&proc->inner_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001202 if (strong) {
1203 if (internal) {
1204 if (target_list == NULL &&
1205 node->internal_strong_refs == 0 &&
Martijn Coenen342e5c92017-02-03 14:40:46 -08001206 !(node->proc &&
1207 node == node->proc->context->binder_context_mgr_node &&
1208 node->has_strong_ref)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301209 pr_err("invalid inc strong node for %d\n",
1210 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001211 return -EINVAL;
1212 }
1213 node->internal_strong_refs++;
1214 } else
1215 node->local_strong_refs++;
1216 if (!node->has_strong_ref && target_list) {
Todd Kjos72196392017-06-29 12:02:02 -07001217 binder_dequeue_work_ilocked(&node->work);
1218 binder_enqueue_work_ilocked(&node->work, target_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001219 }
1220 } else {
1221 if (!internal)
1222 node->local_weak_refs++;
1223 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
1224 if (target_list == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301225 pr_err("invalid inc weak node for %d\n",
1226 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001227 return -EINVAL;
1228 }
Todd Kjos72196392017-06-29 12:02:02 -07001229 binder_enqueue_work_ilocked(&node->work, target_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001230 }
1231 }
1232 return 0;
1233}
1234
Todd Kjosed297212017-06-29 12:02:01 -07001235static int binder_inc_node(struct binder_node *node, int strong, int internal,
1236 struct list_head *target_list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001237{
Todd Kjosed297212017-06-29 12:02:01 -07001238 int ret;
1239
Todd Kjos673068e2017-06-29 12:02:03 -07001240 binder_node_inner_lock(node);
1241 ret = binder_inc_node_nilocked(node, strong, internal, target_list);
1242 binder_node_inner_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07001243
1244 return ret;
1245}
1246
Todd Kjos673068e2017-06-29 12:02:03 -07001247static bool binder_dec_node_nilocked(struct binder_node *node,
1248 int strong, int internal)
Todd Kjosed297212017-06-29 12:02:01 -07001249{
1250 struct binder_proc *proc = node->proc;
1251
Martijn Coenen858b2712017-08-31 10:04:26 +02001252 assert_spin_locked(&node->lock);
Todd Kjosed297212017-06-29 12:02:01 -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 node->internal_strong_refs--;
1258 else
1259 node->local_strong_refs--;
1260 if (node->local_strong_refs || node->internal_strong_refs)
Todd Kjosed297212017-06-29 12:02:01 -07001261 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001262 } else {
1263 if (!internal)
1264 node->local_weak_refs--;
Todd Kjosadc18842017-06-29 12:01:59 -07001265 if (node->local_weak_refs || node->tmp_refs ||
1266 !hlist_empty(&node->refs))
Todd Kjosed297212017-06-29 12:02:01 -07001267 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001268 }
Todd Kjosed297212017-06-29 12:02:01 -07001269
1270 if (proc && (node->has_strong_ref || node->has_weak_ref)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001271 if (list_empty(&node->work.entry)) {
Todd Kjos72196392017-06-29 12:02:02 -07001272 binder_enqueue_work_ilocked(&node->work, &proc->todo);
Martijn Coenen408c68b2017-08-31 10:04:19 +02001273 binder_wakeup_proc_ilocked(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001274 }
1275 } else {
1276 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
Todd Kjosadc18842017-06-29 12:01:59 -07001277 !node->local_weak_refs && !node->tmp_refs) {
Todd Kjosed297212017-06-29 12:02:01 -07001278 if (proc) {
Todd Kjos72196392017-06-29 12:02:02 -07001279 binder_dequeue_work_ilocked(&node->work);
1280 rb_erase(&node->rb_node, &proc->nodes);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001281 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301282 "refless node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001283 node->debug_id);
1284 } else {
Todd Kjos72196392017-06-29 12:02:02 -07001285 BUG_ON(!list_empty(&node->work.entry));
Todd Kjosc44b1232017-06-29 12:01:43 -07001286 spin_lock(&binder_dead_nodes_lock);
Todd Kjosed297212017-06-29 12:02:01 -07001287 /*
1288 * tmp_refs could have changed so
1289 * check it again
1290 */
1291 if (node->tmp_refs) {
1292 spin_unlock(&binder_dead_nodes_lock);
1293 return false;
1294 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001295 hlist_del(&node->dead_node);
Todd Kjosc44b1232017-06-29 12:01:43 -07001296 spin_unlock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001297 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301298 "dead node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001299 node->debug_id);
1300 }
Todd Kjosed297212017-06-29 12:02:01 -07001301 return true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001302 }
1303 }
Todd Kjosed297212017-06-29 12:02:01 -07001304 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001305}
1306
Todd Kjosed297212017-06-29 12:02:01 -07001307static void binder_dec_node(struct binder_node *node, int strong, int internal)
1308{
1309 bool free_node;
1310
Todd Kjos673068e2017-06-29 12:02:03 -07001311 binder_node_inner_lock(node);
1312 free_node = binder_dec_node_nilocked(node, strong, internal);
1313 binder_node_inner_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07001314 if (free_node)
1315 binder_free_node(node);
1316}
1317
1318static void binder_inc_node_tmpref_ilocked(struct binder_node *node)
Todd Kjosadc18842017-06-29 12:01:59 -07001319{
1320 /*
1321 * No call to binder_inc_node() is needed since we
1322 * don't need to inform userspace of any changes to
1323 * tmp_refs
1324 */
1325 node->tmp_refs++;
1326}
1327
1328/**
Todd Kjosed297212017-06-29 12:02:01 -07001329 * binder_inc_node_tmpref() - take a temporary reference on node
1330 * @node: node to reference
1331 *
1332 * Take reference on node to prevent the node from being freed
1333 * while referenced only by a local variable. The inner lock is
1334 * needed to serialize with the node work on the queue (which
1335 * isn't needed after the node is dead). If the node is dead
1336 * (node->proc is NULL), use binder_dead_nodes_lock to protect
1337 * node->tmp_refs against dead-node-only cases where the node
1338 * lock cannot be acquired (eg traversing the dead node list to
1339 * print nodes)
1340 */
1341static void binder_inc_node_tmpref(struct binder_node *node)
1342{
Todd Kjos673068e2017-06-29 12:02:03 -07001343 binder_node_lock(node);
Todd Kjosed297212017-06-29 12:02:01 -07001344 if (node->proc)
1345 binder_inner_proc_lock(node->proc);
1346 else
1347 spin_lock(&binder_dead_nodes_lock);
1348 binder_inc_node_tmpref_ilocked(node);
1349 if (node->proc)
1350 binder_inner_proc_unlock(node->proc);
1351 else
1352 spin_unlock(&binder_dead_nodes_lock);
Todd Kjos673068e2017-06-29 12:02:03 -07001353 binder_node_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07001354}
1355
1356/**
Todd Kjosadc18842017-06-29 12:01:59 -07001357 * binder_dec_node_tmpref() - remove a temporary reference on node
1358 * @node: node to reference
1359 *
1360 * Release temporary reference on node taken via binder_inc_node_tmpref()
1361 */
1362static void binder_dec_node_tmpref(struct binder_node *node)
1363{
Todd Kjosed297212017-06-29 12:02:01 -07001364 bool free_node;
1365
Todd Kjos673068e2017-06-29 12:02:03 -07001366 binder_node_inner_lock(node);
1367 if (!node->proc)
Todd Kjosed297212017-06-29 12:02:01 -07001368 spin_lock(&binder_dead_nodes_lock);
Todd Kjosadc18842017-06-29 12:01:59 -07001369 node->tmp_refs--;
1370 BUG_ON(node->tmp_refs < 0);
Todd Kjosed297212017-06-29 12:02:01 -07001371 if (!node->proc)
1372 spin_unlock(&binder_dead_nodes_lock);
Todd Kjosadc18842017-06-29 12:01:59 -07001373 /*
1374 * Call binder_dec_node() to check if all refcounts are 0
1375 * and cleanup is needed. Calling with strong=0 and internal=1
1376 * causes no actual reference to be released in binder_dec_node().
1377 * If that changes, a change is needed here too.
1378 */
Todd Kjos673068e2017-06-29 12:02:03 -07001379 free_node = binder_dec_node_nilocked(node, 0, 1);
1380 binder_node_inner_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07001381 if (free_node)
1382 binder_free_node(node);
Todd Kjosadc18842017-06-29 12:01:59 -07001383}
1384
1385static void binder_put_node(struct binder_node *node)
1386{
1387 binder_dec_node_tmpref(node);
1388}
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001389
Todd Kjos2c1838d2017-06-29 12:02:08 -07001390static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc,
1391 u32 desc, bool need_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001392{
1393 struct rb_node *n = proc->refs_by_desc.rb_node;
1394 struct binder_ref *ref;
1395
1396 while (n) {
1397 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1398
Todd Kjos372e3142017-06-29 12:01:58 -07001399 if (desc < ref->data.desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001400 n = n->rb_left;
Todd Kjos372e3142017-06-29 12:01:58 -07001401 } else if (desc > ref->data.desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001402 n = n->rb_right;
Todd Kjos372e3142017-06-29 12:01:58 -07001403 } else if (need_strong_ref && !ref->data.strong) {
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001404 binder_user_error("tried to use weak ref as strong ref\n");
1405 return NULL;
1406 } else {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001407 return ref;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001408 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001409 }
1410 return NULL;
1411}
1412
Todd Kjos372e3142017-06-29 12:01:58 -07001413/**
Todd Kjos2c1838d2017-06-29 12:02:08 -07001414 * binder_get_ref_for_node_olocked() - get the ref associated with given node
Todd Kjos372e3142017-06-29 12:01:58 -07001415 * @proc: binder_proc that owns the ref
1416 * @node: binder_node of target
1417 * @new_ref: newly allocated binder_ref to be initialized or %NULL
1418 *
1419 * Look up the ref for the given node and return it if it exists
1420 *
1421 * If it doesn't exist and the caller provides a newly allocated
1422 * ref, initialize the fields of the newly allocated ref and insert
1423 * into the given proc rb_trees and node refs list.
1424 *
1425 * Return: the ref for node. It is possible that another thread
1426 * allocated/initialized the ref first in which case the
1427 * returned ref would be different than the passed-in
1428 * new_ref. new_ref must be kfree'd by the caller in
1429 * this case.
1430 */
Todd Kjos2c1838d2017-06-29 12:02:08 -07001431static struct binder_ref *binder_get_ref_for_node_olocked(
1432 struct binder_proc *proc,
1433 struct binder_node *node,
1434 struct binder_ref *new_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001435{
Todd Kjos372e3142017-06-29 12:01:58 -07001436 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001437 struct rb_node **p = &proc->refs_by_node.rb_node;
1438 struct rb_node *parent = NULL;
Todd Kjos372e3142017-06-29 12:01:58 -07001439 struct binder_ref *ref;
1440 struct rb_node *n;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001441
1442 while (*p) {
1443 parent = *p;
1444 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1445
1446 if (node < ref->node)
1447 p = &(*p)->rb_left;
1448 else if (node > ref->node)
1449 p = &(*p)->rb_right;
1450 else
1451 return ref;
1452 }
Todd Kjos372e3142017-06-29 12:01:58 -07001453 if (!new_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001454 return NULL;
Todd Kjos372e3142017-06-29 12:01:58 -07001455
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001456 binder_stats_created(BINDER_STAT_REF);
Todd Kjos372e3142017-06-29 12:01:58 -07001457 new_ref->data.debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001458 new_ref->proc = proc;
1459 new_ref->node = node;
1460 rb_link_node(&new_ref->rb_node_node, parent, p);
1461 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1462
Todd Kjos372e3142017-06-29 12:01:58 -07001463 new_ref->data.desc = (node == context->binder_context_mgr_node) ? 0 : 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001464 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1465 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Todd Kjos372e3142017-06-29 12:01:58 -07001466 if (ref->data.desc > new_ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001467 break;
Todd Kjos372e3142017-06-29 12:01:58 -07001468 new_ref->data.desc = ref->data.desc + 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001469 }
1470
1471 p = &proc->refs_by_desc.rb_node;
1472 while (*p) {
1473 parent = *p;
1474 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1475
Todd Kjos372e3142017-06-29 12:01:58 -07001476 if (new_ref->data.desc < ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001477 p = &(*p)->rb_left;
Todd Kjos372e3142017-06-29 12:01:58 -07001478 else if (new_ref->data.desc > ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001479 p = &(*p)->rb_right;
1480 else
1481 BUG();
1482 }
1483 rb_link_node(&new_ref->rb_node_desc, parent, p);
1484 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
Todd Kjos673068e2017-06-29 12:02:03 -07001485
1486 binder_node_lock(node);
Todd Kjose4cffcf2017-06-29 12:01:50 -07001487 hlist_add_head(&new_ref->node_entry, &node->refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001488
Todd Kjose4cffcf2017-06-29 12:01:50 -07001489 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1490 "%d new ref %d desc %d for node %d\n",
Todd Kjos372e3142017-06-29 12:01:58 -07001491 proc->pid, new_ref->data.debug_id, new_ref->data.desc,
Todd Kjose4cffcf2017-06-29 12:01:50 -07001492 node->debug_id);
Todd Kjos673068e2017-06-29 12:02:03 -07001493 binder_node_unlock(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001494 return new_ref;
1495}
1496
Todd Kjos2c1838d2017-06-29 12:02:08 -07001497static void binder_cleanup_ref_olocked(struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001498{
Todd Kjosed297212017-06-29 12:02:01 -07001499 bool delete_node = false;
Todd Kjosed297212017-06-29 12:02:01 -07001500
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001501 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301502 "%d delete ref %d desc %d for node %d\n",
Todd Kjos372e3142017-06-29 12:01:58 -07001503 ref->proc->pid, ref->data.debug_id, ref->data.desc,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301504 ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001505
1506 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1507 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
Todd Kjos372e3142017-06-29 12:01:58 -07001508
Todd Kjos673068e2017-06-29 12:02:03 -07001509 binder_node_inner_lock(ref->node);
Todd Kjos372e3142017-06-29 12:01:58 -07001510 if (ref->data.strong)
Todd Kjos673068e2017-06-29 12:02:03 -07001511 binder_dec_node_nilocked(ref->node, 1, 1);
Todd Kjos372e3142017-06-29 12:01:58 -07001512
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001513 hlist_del(&ref->node_entry);
Todd Kjos673068e2017-06-29 12:02:03 -07001514 delete_node = binder_dec_node_nilocked(ref->node, 0, 1);
1515 binder_node_inner_unlock(ref->node);
Todd Kjosed297212017-06-29 12:02:01 -07001516 /*
1517 * Clear ref->node unless we want the caller to free the node
1518 */
1519 if (!delete_node) {
1520 /*
1521 * The caller uses ref->node to determine
1522 * whether the node needs to be freed. Clear
1523 * it since the node is still alive.
1524 */
1525 ref->node = NULL;
1526 }
Todd Kjos372e3142017-06-29 12:01:58 -07001527
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001528 if (ref->death) {
1529 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301530 "%d delete ref %d desc %d has death notification\n",
Todd Kjos372e3142017-06-29 12:01:58 -07001531 ref->proc->pid, ref->data.debug_id,
1532 ref->data.desc);
Todd Kjos72196392017-06-29 12:02:02 -07001533 binder_dequeue_work(ref->proc, &ref->death->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001534 binder_stats_deleted(BINDER_STAT_DEATH);
1535 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001536 binder_stats_deleted(BINDER_STAT_REF);
1537}
1538
Todd Kjos372e3142017-06-29 12:01:58 -07001539/**
Todd Kjos2c1838d2017-06-29 12:02:08 -07001540 * binder_inc_ref_olocked() - increment the ref for given handle
Todd Kjos372e3142017-06-29 12:01:58 -07001541 * @ref: ref to be incremented
1542 * @strong: if true, strong increment, else weak
1543 * @target_list: list to queue node work on
1544 *
Todd Kjos2c1838d2017-06-29 12:02:08 -07001545 * Increment the ref. @ref->proc->outer_lock must be held on entry
Todd Kjos372e3142017-06-29 12:01:58 -07001546 *
1547 * Return: 0, if successful, else errno
1548 */
Todd Kjos2c1838d2017-06-29 12:02:08 -07001549static int binder_inc_ref_olocked(struct binder_ref *ref, int strong,
1550 struct list_head *target_list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001551{
1552 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +09001553
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001554 if (strong) {
Todd Kjos372e3142017-06-29 12:01:58 -07001555 if (ref->data.strong == 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001556 ret = binder_inc_node(ref->node, 1, 1, target_list);
1557 if (ret)
1558 return ret;
1559 }
Todd Kjos372e3142017-06-29 12:01:58 -07001560 ref->data.strong++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001561 } else {
Todd Kjos372e3142017-06-29 12:01:58 -07001562 if (ref->data.weak == 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001563 ret = binder_inc_node(ref->node, 0, 1, target_list);
1564 if (ret)
1565 return ret;
1566 }
Todd Kjos372e3142017-06-29 12:01:58 -07001567 ref->data.weak++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001568 }
1569 return 0;
1570}
1571
Todd Kjos372e3142017-06-29 12:01:58 -07001572/**
1573 * binder_dec_ref() - dec the ref for given handle
1574 * @ref: ref to be decremented
1575 * @strong: if true, strong decrement, else weak
1576 *
1577 * Decrement the ref.
1578 *
Todd Kjos372e3142017-06-29 12:01:58 -07001579 * Return: true if ref is cleaned up and ready to be freed
1580 */
Todd Kjos2c1838d2017-06-29 12:02:08 -07001581static bool binder_dec_ref_olocked(struct binder_ref *ref, int strong)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001582{
1583 if (strong) {
Todd Kjos372e3142017-06-29 12:01:58 -07001584 if (ref->data.strong == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301585 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
Todd Kjos372e3142017-06-29 12:01:58 -07001586 ref->proc->pid, ref->data.debug_id,
1587 ref->data.desc, ref->data.strong,
1588 ref->data.weak);
1589 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001590 }
Todd Kjos372e3142017-06-29 12:01:58 -07001591 ref->data.strong--;
Todd Kjosed297212017-06-29 12:02:01 -07001592 if (ref->data.strong == 0)
1593 binder_dec_node(ref->node, strong, 1);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001594 } else {
Todd Kjos372e3142017-06-29 12:01:58 -07001595 if (ref->data.weak == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301596 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
Todd Kjos372e3142017-06-29 12:01:58 -07001597 ref->proc->pid, ref->data.debug_id,
1598 ref->data.desc, ref->data.strong,
1599 ref->data.weak);
1600 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001601 }
Todd Kjos372e3142017-06-29 12:01:58 -07001602 ref->data.weak--;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001603 }
Todd Kjos372e3142017-06-29 12:01:58 -07001604 if (ref->data.strong == 0 && ref->data.weak == 0) {
Todd Kjos2c1838d2017-06-29 12:02:08 -07001605 binder_cleanup_ref_olocked(ref);
Todd Kjos372e3142017-06-29 12:01:58 -07001606 return true;
1607 }
1608 return false;
1609}
1610
1611/**
1612 * binder_get_node_from_ref() - get the node from the given proc/desc
1613 * @proc: proc containing the ref
1614 * @desc: the handle associated with the ref
1615 * @need_strong_ref: if true, only return node if ref is strong
1616 * @rdata: the id/refcount data for the ref
1617 *
1618 * Given a proc and ref handle, return the associated binder_node
1619 *
1620 * Return: a binder_node or NULL if not found or not strong when strong required
1621 */
1622static struct binder_node *binder_get_node_from_ref(
1623 struct binder_proc *proc,
1624 u32 desc, bool need_strong_ref,
1625 struct binder_ref_data *rdata)
1626{
1627 struct binder_node *node;
1628 struct binder_ref *ref;
1629
Todd Kjos2c1838d2017-06-29 12:02:08 -07001630 binder_proc_lock(proc);
1631 ref = binder_get_ref_olocked(proc, desc, need_strong_ref);
Todd Kjos372e3142017-06-29 12:01:58 -07001632 if (!ref)
1633 goto err_no_ref;
1634 node = ref->node;
Todd Kjosadc18842017-06-29 12:01:59 -07001635 /*
1636 * Take an implicit reference on the node to ensure
1637 * it stays alive until the call to binder_put_node()
1638 */
1639 binder_inc_node_tmpref(node);
Todd Kjos372e3142017-06-29 12:01:58 -07001640 if (rdata)
1641 *rdata = ref->data;
Todd Kjos2c1838d2017-06-29 12:02:08 -07001642 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07001643
1644 return node;
1645
1646err_no_ref:
Todd Kjos2c1838d2017-06-29 12:02:08 -07001647 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07001648 return NULL;
1649}
1650
1651/**
1652 * binder_free_ref() - free the binder_ref
1653 * @ref: ref to free
1654 *
Todd Kjosed297212017-06-29 12:02:01 -07001655 * Free the binder_ref. Free the binder_node indicated by ref->node
1656 * (if non-NULL) and the binder_ref_death indicated by ref->death.
Todd Kjos372e3142017-06-29 12:01:58 -07001657 */
1658static void binder_free_ref(struct binder_ref *ref)
1659{
Todd Kjosed297212017-06-29 12:02:01 -07001660 if (ref->node)
1661 binder_free_node(ref->node);
Todd Kjos372e3142017-06-29 12:01:58 -07001662 kfree(ref->death);
1663 kfree(ref);
1664}
1665
1666/**
1667 * binder_update_ref_for_handle() - inc/dec the ref for given handle
1668 * @proc: proc containing the ref
1669 * @desc: the handle associated with the ref
1670 * @increment: true=inc reference, false=dec reference
1671 * @strong: true=strong reference, false=weak reference
1672 * @rdata: the id/refcount data for the ref
1673 *
1674 * Given a proc and ref handle, increment or decrement the ref
1675 * according to "increment" arg.
1676 *
1677 * Return: 0 if successful, else errno
1678 */
1679static int binder_update_ref_for_handle(struct binder_proc *proc,
1680 uint32_t desc, bool increment, bool strong,
1681 struct binder_ref_data *rdata)
1682{
1683 int ret = 0;
1684 struct binder_ref *ref;
1685 bool delete_ref = false;
1686
Todd Kjos2c1838d2017-06-29 12:02:08 -07001687 binder_proc_lock(proc);
1688 ref = binder_get_ref_olocked(proc, desc, strong);
Todd Kjos372e3142017-06-29 12:01:58 -07001689 if (!ref) {
1690 ret = -EINVAL;
1691 goto err_no_ref;
1692 }
1693 if (increment)
Todd Kjos2c1838d2017-06-29 12:02:08 -07001694 ret = binder_inc_ref_olocked(ref, strong, NULL);
Todd Kjos372e3142017-06-29 12:01:58 -07001695 else
Todd Kjos2c1838d2017-06-29 12:02:08 -07001696 delete_ref = binder_dec_ref_olocked(ref, strong);
Todd Kjos372e3142017-06-29 12:01:58 -07001697
1698 if (rdata)
1699 *rdata = ref->data;
Todd Kjos2c1838d2017-06-29 12:02:08 -07001700 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07001701
1702 if (delete_ref)
1703 binder_free_ref(ref);
1704 return ret;
1705
1706err_no_ref:
Todd Kjos2c1838d2017-06-29 12:02:08 -07001707 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07001708 return ret;
1709}
1710
1711/**
1712 * binder_dec_ref_for_handle() - dec the ref for given handle
1713 * @proc: proc containing the ref
1714 * @desc: the handle associated with the ref
1715 * @strong: true=strong reference, false=weak reference
1716 * @rdata: the id/refcount data for the ref
1717 *
1718 * Just calls binder_update_ref_for_handle() to decrement the ref.
1719 *
1720 * Return: 0 if successful, else errno
1721 */
1722static int binder_dec_ref_for_handle(struct binder_proc *proc,
1723 uint32_t desc, bool strong, struct binder_ref_data *rdata)
1724{
1725 return binder_update_ref_for_handle(proc, desc, false, strong, rdata);
1726}
1727
1728
1729/**
1730 * binder_inc_ref_for_node() - increment the ref for given proc/node
1731 * @proc: proc containing the ref
1732 * @node: target node
1733 * @strong: true=strong reference, false=weak reference
1734 * @target_list: worklist to use if node is incremented
1735 * @rdata: the id/refcount data for the ref
1736 *
1737 * Given a proc and node, increment the ref. Create the ref if it
1738 * doesn't already exist
1739 *
1740 * Return: 0 if successful, else errno
1741 */
1742static int binder_inc_ref_for_node(struct binder_proc *proc,
1743 struct binder_node *node,
1744 bool strong,
1745 struct list_head *target_list,
1746 struct binder_ref_data *rdata)
1747{
1748 struct binder_ref *ref;
1749 struct binder_ref *new_ref = NULL;
1750 int ret = 0;
1751
Todd Kjos2c1838d2017-06-29 12:02:08 -07001752 binder_proc_lock(proc);
1753 ref = binder_get_ref_for_node_olocked(proc, node, NULL);
Todd Kjos372e3142017-06-29 12:01:58 -07001754 if (!ref) {
Todd Kjos2c1838d2017-06-29 12:02:08 -07001755 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07001756 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1757 if (!new_ref)
1758 return -ENOMEM;
Todd Kjos2c1838d2017-06-29 12:02:08 -07001759 binder_proc_lock(proc);
1760 ref = binder_get_ref_for_node_olocked(proc, node, new_ref);
Todd Kjos372e3142017-06-29 12:01:58 -07001761 }
Todd Kjos2c1838d2017-06-29 12:02:08 -07001762 ret = binder_inc_ref_olocked(ref, strong, target_list);
Todd Kjos372e3142017-06-29 12:01:58 -07001763 *rdata = ref->data;
Todd Kjos2c1838d2017-06-29 12:02:08 -07001764 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07001765 if (new_ref && ref != new_ref)
1766 /*
1767 * Another thread created the ref first so
1768 * free the one we allocated
1769 */
1770 kfree(new_ref);
1771 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001772}
1773
Martijn Coenen0b89d692017-06-29 12:02:06 -07001774static void binder_pop_transaction_ilocked(struct binder_thread *target_thread,
1775 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001776{
Todd Kjosb6d282c2017-06-29 12:01:54 -07001777 BUG_ON(!target_thread);
Martijn Coenen858b2712017-08-31 10:04:26 +02001778 assert_spin_locked(&target_thread->proc->inner_lock);
Todd Kjosb6d282c2017-06-29 12:01:54 -07001779 BUG_ON(target_thread->transaction_stack != t);
1780 BUG_ON(target_thread->transaction_stack->from != target_thread);
1781 target_thread->transaction_stack =
1782 target_thread->transaction_stack->from_parent;
1783 t->from = NULL;
1784}
1785
Todd Kjos7a4408c2017-06-29 12:01:57 -07001786/**
1787 * binder_thread_dec_tmpref() - decrement thread->tmp_ref
1788 * @thread: thread to decrement
1789 *
1790 * A thread needs to be kept alive while being used to create or
1791 * handle a transaction. binder_get_txn_from() is used to safely
1792 * extract t->from from a binder_transaction and keep the thread
1793 * indicated by t->from from being freed. When done with that
1794 * binder_thread, this function is called to decrement the
1795 * tmp_ref and free if appropriate (thread has been released
1796 * and no transaction being processed by the driver)
1797 */
1798static void binder_thread_dec_tmpref(struct binder_thread *thread)
1799{
1800 /*
1801 * atomic is used to protect the counter value while
1802 * it cannot reach zero or thread->is_dead is false
Todd Kjos7a4408c2017-06-29 12:01:57 -07001803 */
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07001804 binder_inner_proc_lock(thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07001805 atomic_dec(&thread->tmp_ref);
1806 if (thread->is_dead && !atomic_read(&thread->tmp_ref)) {
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07001807 binder_inner_proc_unlock(thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07001808 binder_free_thread(thread);
1809 return;
1810 }
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07001811 binder_inner_proc_unlock(thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07001812}
1813
1814/**
1815 * binder_proc_dec_tmpref() - decrement proc->tmp_ref
1816 * @proc: proc to decrement
1817 *
1818 * A binder_proc needs to be kept alive while being used to create or
1819 * handle a transaction. proc->tmp_ref is incremented when
1820 * creating a new transaction or the binder_proc is currently in-use
1821 * by threads that are being released. When done with the binder_proc,
1822 * this function is called to decrement the counter and free the
1823 * proc if appropriate (proc has been released, all threads have
1824 * been released and not currenly in-use to process a transaction).
1825 */
1826static void binder_proc_dec_tmpref(struct binder_proc *proc)
1827{
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07001828 binder_inner_proc_lock(proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07001829 proc->tmp_ref--;
1830 if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) &&
1831 !proc->tmp_ref) {
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07001832 binder_inner_proc_unlock(proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07001833 binder_free_proc(proc);
1834 return;
1835 }
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07001836 binder_inner_proc_unlock(proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07001837}
1838
1839/**
1840 * binder_get_txn_from() - safely extract the "from" thread in transaction
1841 * @t: binder transaction for t->from
1842 *
1843 * Atomically return the "from" thread and increment the tmp_ref
1844 * count for the thread to ensure it stays alive until
1845 * binder_thread_dec_tmpref() is called.
1846 *
1847 * Return: the value of t->from
1848 */
1849static struct binder_thread *binder_get_txn_from(
1850 struct binder_transaction *t)
1851{
1852 struct binder_thread *from;
1853
1854 spin_lock(&t->lock);
1855 from = t->from;
1856 if (from)
1857 atomic_inc(&from->tmp_ref);
1858 spin_unlock(&t->lock);
1859 return from;
1860}
1861
Martijn Coenen0b89d692017-06-29 12:02:06 -07001862/**
1863 * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock
1864 * @t: binder transaction for t->from
1865 *
1866 * Same as binder_get_txn_from() except it also acquires the proc->inner_lock
1867 * to guarantee that the thread cannot be released while operating on it.
1868 * The caller must call binder_inner_proc_unlock() to release the inner lock
1869 * as well as call binder_dec_thread_txn() to release the reference.
1870 *
1871 * Return: the value of t->from
1872 */
1873static struct binder_thread *binder_get_txn_from_and_acq_inner(
1874 struct binder_transaction *t)
1875{
1876 struct binder_thread *from;
1877
1878 from = binder_get_txn_from(t);
1879 if (!from)
1880 return NULL;
1881 binder_inner_proc_lock(from->proc);
1882 if (t->from) {
1883 BUG_ON(from != t->from);
1884 return from;
1885 }
1886 binder_inner_proc_unlock(from->proc);
1887 binder_thread_dec_tmpref(from);
1888 return NULL;
1889}
1890
Todd Kjosb6d282c2017-06-29 12:01:54 -07001891static void binder_free_transaction(struct binder_transaction *t)
1892{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001893 if (t->buffer)
1894 t->buffer->transaction = NULL;
1895 kfree(t);
1896 binder_stats_deleted(BINDER_STAT_TRANSACTION);
1897}
1898
1899static void binder_send_failed_reply(struct binder_transaction *t,
1900 uint32_t error_code)
1901{
1902 struct binder_thread *target_thread;
Lucas Tanured4ec15e2014-07-13 21:31:05 -03001903 struct binder_transaction *next;
Seunghun Lee10f62862014-05-01 01:30:23 +09001904
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001905 BUG_ON(t->flags & TF_ONE_WAY);
1906 while (1) {
Martijn Coenen0b89d692017-06-29 12:02:06 -07001907 target_thread = binder_get_txn_from_and_acq_inner(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001908 if (target_thread) {
Todd Kjos26549d12017-06-29 12:01:55 -07001909 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1910 "send failed reply for transaction %d to %d:%d\n",
1911 t->debug_id,
1912 target_thread->proc->pid,
1913 target_thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001914
Martijn Coenen0b89d692017-06-29 12:02:06 -07001915 binder_pop_transaction_ilocked(target_thread, t);
Todd Kjos26549d12017-06-29 12:01:55 -07001916 if (target_thread->reply_error.cmd == BR_OK) {
1917 target_thread->reply_error.cmd = error_code;
Martijn Coenen0b89d692017-06-29 12:02:06 -07001918 binder_enqueue_work_ilocked(
Todd Kjos72196392017-06-29 12:02:02 -07001919 &target_thread->reply_error.work,
Todd Kjos26549d12017-06-29 12:01:55 -07001920 &target_thread->todo);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001921 wake_up_interruptible(&target_thread->wait);
1922 } else {
Todd Kjos26549d12017-06-29 12:01:55 -07001923 WARN(1, "Unexpected reply error: %u\n",
1924 target_thread->reply_error.cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001925 }
Martijn Coenen0b89d692017-06-29 12:02:06 -07001926 binder_inner_proc_unlock(target_thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07001927 binder_thread_dec_tmpref(target_thread);
Todd Kjos26549d12017-06-29 12:01:55 -07001928 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001929 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001930 }
Lucas Tanured4ec15e2014-07-13 21:31:05 -03001931 next = t->from_parent;
1932
1933 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1934 "send failed reply for transaction %d, target dead\n",
1935 t->debug_id);
1936
Todd Kjosb6d282c2017-06-29 12:01:54 -07001937 binder_free_transaction(t);
Lucas Tanured4ec15e2014-07-13 21:31:05 -03001938 if (next == NULL) {
1939 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1940 "reply failed, no target thread at root\n");
1941 return;
1942 }
1943 t = next;
1944 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1945 "reply failed, no target thread -- retry %d\n",
1946 t->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001947 }
1948}
1949
Martijn Coenenfeba3902017-02-03 14:40:45 -08001950/**
1951 * binder_validate_object() - checks for a valid metadata object in a buffer.
1952 * @buffer: binder_buffer that we're parsing.
1953 * @offset: offset in the buffer at which to validate an object.
1954 *
1955 * Return: If there's a valid metadata object at @offset in @buffer, the
1956 * size of that object. Otherwise, it returns zero.
1957 */
1958static size_t binder_validate_object(struct binder_buffer *buffer, u64 offset)
1959{
1960 /* Check if we can read a header first */
1961 struct binder_object_header *hdr;
1962 size_t object_size = 0;
1963
1964 if (offset > buffer->data_size - sizeof(*hdr) ||
1965 buffer->data_size < sizeof(*hdr) ||
1966 !IS_ALIGNED(offset, sizeof(u32)))
1967 return 0;
1968
1969 /* Ok, now see if we can read a complete object. */
1970 hdr = (struct binder_object_header *)(buffer->data + offset);
1971 switch (hdr->type) {
1972 case BINDER_TYPE_BINDER:
1973 case BINDER_TYPE_WEAK_BINDER:
1974 case BINDER_TYPE_HANDLE:
1975 case BINDER_TYPE_WEAK_HANDLE:
1976 object_size = sizeof(struct flat_binder_object);
1977 break;
1978 case BINDER_TYPE_FD:
1979 object_size = sizeof(struct binder_fd_object);
1980 break;
Martijn Coenen79802402017-02-03 14:40:51 -08001981 case BINDER_TYPE_PTR:
1982 object_size = sizeof(struct binder_buffer_object);
1983 break;
Martijn Coenendef95c72017-02-03 14:40:52 -08001984 case BINDER_TYPE_FDA:
1985 object_size = sizeof(struct binder_fd_array_object);
1986 break;
Martijn Coenenfeba3902017-02-03 14:40:45 -08001987 default:
1988 return 0;
1989 }
1990 if (offset <= buffer->data_size - object_size &&
1991 buffer->data_size >= object_size)
1992 return object_size;
1993 else
1994 return 0;
1995}
1996
Martijn Coenen79802402017-02-03 14:40:51 -08001997/**
1998 * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
1999 * @b: binder_buffer containing the object
2000 * @index: index in offset array at which the binder_buffer_object is
2001 * located
2002 * @start: points to the start of the offset array
2003 * @num_valid: the number of valid offsets in the offset array
2004 *
2005 * Return: If @index is within the valid range of the offset array
2006 * described by @start and @num_valid, and if there's a valid
2007 * binder_buffer_object at the offset found in index @index
2008 * of the offset array, that object is returned. Otherwise,
2009 * %NULL is returned.
2010 * Note that the offset found in index @index itself is not
2011 * verified; this function assumes that @num_valid elements
2012 * from @start were previously verified to have valid offsets.
2013 */
2014static struct binder_buffer_object *binder_validate_ptr(struct binder_buffer *b,
2015 binder_size_t index,
2016 binder_size_t *start,
2017 binder_size_t num_valid)
2018{
2019 struct binder_buffer_object *buffer_obj;
2020 binder_size_t *offp;
2021
2022 if (index >= num_valid)
2023 return NULL;
2024
2025 offp = start + index;
2026 buffer_obj = (struct binder_buffer_object *)(b->data + *offp);
2027 if (buffer_obj->hdr.type != BINDER_TYPE_PTR)
2028 return NULL;
2029
2030 return buffer_obj;
2031}
2032
2033/**
2034 * binder_validate_fixup() - validates pointer/fd fixups happen in order.
2035 * @b: transaction buffer
2036 * @objects_start start of objects buffer
2037 * @buffer: binder_buffer_object in which to fix up
2038 * @offset: start offset in @buffer to fix up
2039 * @last_obj: last binder_buffer_object that we fixed up in
2040 * @last_min_offset: minimum fixup offset in @last_obj
2041 *
2042 * Return: %true if a fixup in buffer @buffer at offset @offset is
2043 * allowed.
2044 *
2045 * For safety reasons, we only allow fixups inside a buffer to happen
2046 * at increasing offsets; additionally, we only allow fixup on the last
2047 * buffer object that was verified, or one of its parents.
2048 *
2049 * Example of what is allowed:
2050 *
2051 * A
2052 * B (parent = A, offset = 0)
2053 * C (parent = A, offset = 16)
2054 * D (parent = C, offset = 0)
2055 * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
2056 *
2057 * Examples of what is not allowed:
2058 *
2059 * Decreasing offsets within the same parent:
2060 * A
2061 * C (parent = A, offset = 16)
2062 * B (parent = A, offset = 0) // decreasing offset within A
2063 *
2064 * Referring to a parent that wasn't the last object or any of its parents:
2065 * A
2066 * B (parent = A, offset = 0)
2067 * C (parent = A, offset = 0)
2068 * C (parent = A, offset = 16)
2069 * D (parent = B, offset = 0) // B is not A or any of A's parents
2070 */
2071static bool binder_validate_fixup(struct binder_buffer *b,
2072 binder_size_t *objects_start,
2073 struct binder_buffer_object *buffer,
2074 binder_size_t fixup_offset,
2075 struct binder_buffer_object *last_obj,
2076 binder_size_t last_min_offset)
2077{
2078 if (!last_obj) {
2079 /* Nothing to fix up in */
2080 return false;
2081 }
2082
2083 while (last_obj != buffer) {
2084 /*
2085 * Safe to retrieve the parent of last_obj, since it
2086 * was already previously verified by the driver.
2087 */
2088 if ((last_obj->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0)
2089 return false;
2090 last_min_offset = last_obj->parent_offset + sizeof(uintptr_t);
2091 last_obj = (struct binder_buffer_object *)
2092 (b->data + *(objects_start + last_obj->parent));
2093 }
2094 return (fixup_offset >= last_min_offset);
2095}
2096
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002097static void binder_transaction_buffer_release(struct binder_proc *proc,
2098 struct binder_buffer *buffer,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002099 binder_size_t *failed_at)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002100{
Martijn Coenen79802402017-02-03 14:40:51 -08002101 binder_size_t *offp, *off_start, *off_end;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002102 int debug_id = buffer->debug_id;
2103
2104 binder_debug(BINDER_DEBUG_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302105 "%d buffer release %d, size %zd-%zd, failed at %p\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002106 proc->pid, buffer->debug_id,
2107 buffer->data_size, buffer->offsets_size, failed_at);
2108
2109 if (buffer->target_node)
2110 binder_dec_node(buffer->target_node, 1, 0);
2111
Martijn Coenen79802402017-02-03 14:40:51 -08002112 off_start = (binder_size_t *)(buffer->data +
2113 ALIGN(buffer->data_size, sizeof(void *)));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002114 if (failed_at)
2115 off_end = failed_at;
2116 else
Martijn Coenen79802402017-02-03 14:40:51 -08002117 off_end = (void *)off_start + buffer->offsets_size;
2118 for (offp = off_start; offp < off_end; offp++) {
Martijn Coenenfeba3902017-02-03 14:40:45 -08002119 struct binder_object_header *hdr;
2120 size_t object_size = binder_validate_object(buffer, *offp);
Seunghun Lee10f62862014-05-01 01:30:23 +09002121
Martijn Coenenfeba3902017-02-03 14:40:45 -08002122 if (object_size == 0) {
2123 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
Arve Hjønnevågda498892014-02-21 14:40:26 -08002124 debug_id, (u64)*offp, buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002125 continue;
2126 }
Martijn Coenenfeba3902017-02-03 14:40:45 -08002127 hdr = (struct binder_object_header *)(buffer->data + *offp);
2128 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002129 case BINDER_TYPE_BINDER:
2130 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08002131 struct flat_binder_object *fp;
2132 struct binder_node *node;
Seunghun Lee10f62862014-05-01 01:30:23 +09002133
Martijn Coenenfeba3902017-02-03 14:40:45 -08002134 fp = to_flat_binder_object(hdr);
2135 node = binder_get_node(proc, fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002136 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002137 pr_err("transaction release %d bad node %016llx\n",
2138 debug_id, (u64)fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002139 break;
2140 }
2141 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002142 " node %d u%016llx\n",
2143 node->debug_id, (u64)node->ptr);
Martijn Coenenfeba3902017-02-03 14:40:45 -08002144 binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER,
2145 0);
Todd Kjosadc18842017-06-29 12:01:59 -07002146 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002147 } break;
2148 case BINDER_TYPE_HANDLE:
2149 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08002150 struct flat_binder_object *fp;
Todd Kjos372e3142017-06-29 12:01:58 -07002151 struct binder_ref_data rdata;
2152 int ret;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02002153
Martijn Coenenfeba3902017-02-03 14:40:45 -08002154 fp = to_flat_binder_object(hdr);
Todd Kjos372e3142017-06-29 12:01:58 -07002155 ret = binder_dec_ref_for_handle(proc, fp->handle,
2156 hdr->type == BINDER_TYPE_HANDLE, &rdata);
2157
2158 if (ret) {
2159 pr_err("transaction release %d bad handle %d, ret = %d\n",
2160 debug_id, fp->handle, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002161 break;
2162 }
2163 binder_debug(BINDER_DEBUG_TRANSACTION,
Todd Kjos372e3142017-06-29 12:01:58 -07002164 " ref %d desc %d\n",
2165 rdata.debug_id, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002166 } break;
2167
Martijn Coenenfeba3902017-02-03 14:40:45 -08002168 case BINDER_TYPE_FD: {
2169 struct binder_fd_object *fp = to_binder_fd_object(hdr);
2170
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002171 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenenfeba3902017-02-03 14:40:45 -08002172 " fd %d\n", fp->fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002173 if (failed_at)
Martijn Coenenfeba3902017-02-03 14:40:45 -08002174 task_close_fd(proc, fp->fd);
2175 } break;
Martijn Coenen79802402017-02-03 14:40:51 -08002176 case BINDER_TYPE_PTR:
2177 /*
2178 * Nothing to do here, this will get cleaned up when the
2179 * transaction buffer gets freed
2180 */
2181 break;
Martijn Coenendef95c72017-02-03 14:40:52 -08002182 case BINDER_TYPE_FDA: {
2183 struct binder_fd_array_object *fda;
2184 struct binder_buffer_object *parent;
2185 uintptr_t parent_buffer;
2186 u32 *fd_array;
2187 size_t fd_index;
2188 binder_size_t fd_buf_size;
2189
2190 fda = to_binder_fd_array_object(hdr);
2191 parent = binder_validate_ptr(buffer, fda->parent,
2192 off_start,
2193 offp - off_start);
2194 if (!parent) {
2195 pr_err("transaction release %d bad parent offset",
2196 debug_id);
2197 continue;
2198 }
2199 /*
2200 * Since the parent was already fixed up, convert it
2201 * back to kernel address space to access it
2202 */
2203 parent_buffer = parent->buffer -
Todd Kjos19c98722017-06-29 12:01:40 -07002204 binder_alloc_get_user_buffer_offset(
2205 &proc->alloc);
Martijn Coenendef95c72017-02-03 14:40:52 -08002206
2207 fd_buf_size = sizeof(u32) * fda->num_fds;
2208 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2209 pr_err("transaction release %d invalid number of fds (%lld)\n",
2210 debug_id, (u64)fda->num_fds);
2211 continue;
2212 }
2213 if (fd_buf_size > parent->length ||
2214 fda->parent_offset > parent->length - fd_buf_size) {
2215 /* No space for all file descriptors here. */
2216 pr_err("transaction release %d not enough space for %lld fds in buffer\n",
2217 debug_id, (u64)fda->num_fds);
2218 continue;
2219 }
2220 fd_array = (u32 *)(parent_buffer + fda->parent_offset);
2221 for (fd_index = 0; fd_index < fda->num_fds; fd_index++)
2222 task_close_fd(proc, fd_array[fd_index]);
2223 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002224 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01002225 pr_err("transaction release %d bad object type %x\n",
Martijn Coenenfeba3902017-02-03 14:40:45 -08002226 debug_id, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002227 break;
2228 }
2229 }
2230}
2231
Martijn Coenena056af42017-02-03 14:40:49 -08002232static int binder_translate_binder(struct flat_binder_object *fp,
2233 struct binder_transaction *t,
2234 struct binder_thread *thread)
2235{
2236 struct binder_node *node;
Martijn Coenena056af42017-02-03 14:40:49 -08002237 struct binder_proc *proc = thread->proc;
2238 struct binder_proc *target_proc = t->to_proc;
Todd Kjos372e3142017-06-29 12:01:58 -07002239 struct binder_ref_data rdata;
Todd Kjosadc18842017-06-29 12:01:59 -07002240 int ret = 0;
Martijn Coenena056af42017-02-03 14:40:49 -08002241
2242 node = binder_get_node(proc, fp->binder);
2243 if (!node) {
Todd Kjos673068e2017-06-29 12:02:03 -07002244 node = binder_new_node(proc, fp);
Martijn Coenena056af42017-02-03 14:40:49 -08002245 if (!node)
2246 return -ENOMEM;
Martijn Coenena056af42017-02-03 14:40:49 -08002247 }
2248 if (fp->cookie != node->cookie) {
2249 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
2250 proc->pid, thread->pid, (u64)fp->binder,
2251 node->debug_id, (u64)fp->cookie,
2252 (u64)node->cookie);
Todd Kjosadc18842017-06-29 12:01:59 -07002253 ret = -EINVAL;
2254 goto done;
Martijn Coenena056af42017-02-03 14:40:49 -08002255 }
Todd Kjosadc18842017-06-29 12:01:59 -07002256 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2257 ret = -EPERM;
2258 goto done;
2259 }
Martijn Coenena056af42017-02-03 14:40:49 -08002260
Todd Kjos372e3142017-06-29 12:01:58 -07002261 ret = binder_inc_ref_for_node(target_proc, node,
2262 fp->hdr.type == BINDER_TYPE_BINDER,
2263 &thread->todo, &rdata);
2264 if (ret)
Todd Kjosadc18842017-06-29 12:01:59 -07002265 goto done;
Martijn Coenena056af42017-02-03 14:40:49 -08002266
2267 if (fp->hdr.type == BINDER_TYPE_BINDER)
2268 fp->hdr.type = BINDER_TYPE_HANDLE;
2269 else
2270 fp->hdr.type = BINDER_TYPE_WEAK_HANDLE;
2271 fp->binder = 0;
Todd Kjos372e3142017-06-29 12:01:58 -07002272 fp->handle = rdata.desc;
Martijn Coenena056af42017-02-03 14:40:49 -08002273 fp->cookie = 0;
Martijn Coenena056af42017-02-03 14:40:49 -08002274
Todd Kjos372e3142017-06-29 12:01:58 -07002275 trace_binder_transaction_node_to_ref(t, node, &rdata);
Martijn Coenena056af42017-02-03 14:40:49 -08002276 binder_debug(BINDER_DEBUG_TRANSACTION,
2277 " node %d u%016llx -> ref %d desc %d\n",
2278 node->debug_id, (u64)node->ptr,
Todd Kjos372e3142017-06-29 12:01:58 -07002279 rdata.debug_id, rdata.desc);
Todd Kjosadc18842017-06-29 12:01:59 -07002280done:
2281 binder_put_node(node);
2282 return ret;
Martijn Coenena056af42017-02-03 14:40:49 -08002283}
2284
2285static int binder_translate_handle(struct flat_binder_object *fp,
2286 struct binder_transaction *t,
2287 struct binder_thread *thread)
2288{
Martijn Coenena056af42017-02-03 14:40:49 -08002289 struct binder_proc *proc = thread->proc;
2290 struct binder_proc *target_proc = t->to_proc;
Todd Kjos372e3142017-06-29 12:01:58 -07002291 struct binder_node *node;
2292 struct binder_ref_data src_rdata;
Todd Kjosadc18842017-06-29 12:01:59 -07002293 int ret = 0;
Martijn Coenena056af42017-02-03 14:40:49 -08002294
Todd Kjos372e3142017-06-29 12:01:58 -07002295 node = binder_get_node_from_ref(proc, fp->handle,
2296 fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata);
2297 if (!node) {
Martijn Coenena056af42017-02-03 14:40:49 -08002298 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
2299 proc->pid, thread->pid, fp->handle);
2300 return -EINVAL;
2301 }
Todd Kjosadc18842017-06-29 12:01:59 -07002302 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2303 ret = -EPERM;
2304 goto done;
2305 }
Martijn Coenena056af42017-02-03 14:40:49 -08002306
Todd Kjos673068e2017-06-29 12:02:03 -07002307 binder_node_lock(node);
Todd Kjos372e3142017-06-29 12:01:58 -07002308 if (node->proc == target_proc) {
Martijn Coenena056af42017-02-03 14:40:49 -08002309 if (fp->hdr.type == BINDER_TYPE_HANDLE)
2310 fp->hdr.type = BINDER_TYPE_BINDER;
2311 else
2312 fp->hdr.type = BINDER_TYPE_WEAK_BINDER;
Todd Kjos372e3142017-06-29 12:01:58 -07002313 fp->binder = node->ptr;
2314 fp->cookie = node->cookie;
Todd Kjos673068e2017-06-29 12:02:03 -07002315 if (node->proc)
2316 binder_inner_proc_lock(node->proc);
2317 binder_inc_node_nilocked(node,
2318 fp->hdr.type == BINDER_TYPE_BINDER,
2319 0, NULL);
2320 if (node->proc)
2321 binder_inner_proc_unlock(node->proc);
Todd Kjos372e3142017-06-29 12:01:58 -07002322 trace_binder_transaction_ref_to_node(t, node, &src_rdata);
Martijn Coenena056af42017-02-03 14:40:49 -08002323 binder_debug(BINDER_DEBUG_TRANSACTION,
2324 " ref %d desc %d -> node %d u%016llx\n",
Todd Kjos372e3142017-06-29 12:01:58 -07002325 src_rdata.debug_id, src_rdata.desc, node->debug_id,
2326 (u64)node->ptr);
Todd Kjos673068e2017-06-29 12:02:03 -07002327 binder_node_unlock(node);
Martijn Coenena056af42017-02-03 14:40:49 -08002328 } else {
Todd Kjos372e3142017-06-29 12:01:58 -07002329 int ret;
2330 struct binder_ref_data dest_rdata;
Martijn Coenena056af42017-02-03 14:40:49 -08002331
Todd Kjos673068e2017-06-29 12:02:03 -07002332 binder_node_unlock(node);
Todd Kjos372e3142017-06-29 12:01:58 -07002333 ret = binder_inc_ref_for_node(target_proc, node,
2334 fp->hdr.type == BINDER_TYPE_HANDLE,
2335 NULL, &dest_rdata);
2336 if (ret)
Todd Kjosadc18842017-06-29 12:01:59 -07002337 goto done;
Martijn Coenena056af42017-02-03 14:40:49 -08002338
2339 fp->binder = 0;
Todd Kjos372e3142017-06-29 12:01:58 -07002340 fp->handle = dest_rdata.desc;
Martijn Coenena056af42017-02-03 14:40:49 -08002341 fp->cookie = 0;
Todd Kjos372e3142017-06-29 12:01:58 -07002342 trace_binder_transaction_ref_to_ref(t, node, &src_rdata,
2343 &dest_rdata);
Martijn Coenena056af42017-02-03 14:40:49 -08002344 binder_debug(BINDER_DEBUG_TRANSACTION,
2345 " ref %d desc %d -> ref %d desc %d (node %d)\n",
Todd Kjos372e3142017-06-29 12:01:58 -07002346 src_rdata.debug_id, src_rdata.desc,
2347 dest_rdata.debug_id, dest_rdata.desc,
2348 node->debug_id);
Martijn Coenena056af42017-02-03 14:40:49 -08002349 }
Todd Kjosadc18842017-06-29 12:01:59 -07002350done:
2351 binder_put_node(node);
2352 return ret;
Martijn Coenena056af42017-02-03 14:40:49 -08002353}
2354
2355static int binder_translate_fd(int fd,
2356 struct binder_transaction *t,
2357 struct binder_thread *thread,
2358 struct binder_transaction *in_reply_to)
2359{
2360 struct binder_proc *proc = thread->proc;
2361 struct binder_proc *target_proc = t->to_proc;
2362 int target_fd;
2363 struct file *file;
2364 int ret;
2365 bool target_allows_fd;
2366
2367 if (in_reply_to)
2368 target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS);
2369 else
2370 target_allows_fd = t->buffer->target_node->accept_fds;
2371 if (!target_allows_fd) {
2372 binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
2373 proc->pid, thread->pid,
2374 in_reply_to ? "reply" : "transaction",
2375 fd);
2376 ret = -EPERM;
2377 goto err_fd_not_accepted;
2378 }
2379
2380 file = fget(fd);
2381 if (!file) {
2382 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
2383 proc->pid, thread->pid, fd);
2384 ret = -EBADF;
2385 goto err_fget;
2386 }
2387 ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file);
2388 if (ret < 0) {
2389 ret = -EPERM;
2390 goto err_security;
2391 }
2392
2393 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
2394 if (target_fd < 0) {
2395 ret = -ENOMEM;
2396 goto err_get_unused_fd;
2397 }
2398 task_fd_install(target_proc, target_fd, file);
2399 trace_binder_transaction_fd(t, fd, target_fd);
2400 binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n",
2401 fd, target_fd);
2402
2403 return target_fd;
2404
2405err_get_unused_fd:
2406err_security:
2407 fput(file);
2408err_fget:
2409err_fd_not_accepted:
2410 return ret;
2411}
2412
Martijn Coenendef95c72017-02-03 14:40:52 -08002413static int binder_translate_fd_array(struct binder_fd_array_object *fda,
2414 struct binder_buffer_object *parent,
2415 struct binder_transaction *t,
2416 struct binder_thread *thread,
2417 struct binder_transaction *in_reply_to)
2418{
2419 binder_size_t fdi, fd_buf_size, num_installed_fds;
2420 int target_fd;
2421 uintptr_t parent_buffer;
2422 u32 *fd_array;
2423 struct binder_proc *proc = thread->proc;
2424 struct binder_proc *target_proc = t->to_proc;
2425
2426 fd_buf_size = sizeof(u32) * fda->num_fds;
2427 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2428 binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
2429 proc->pid, thread->pid, (u64)fda->num_fds);
2430 return -EINVAL;
2431 }
2432 if (fd_buf_size > parent->length ||
2433 fda->parent_offset > parent->length - fd_buf_size) {
2434 /* No space for all file descriptors here. */
2435 binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
2436 proc->pid, thread->pid, (u64)fda->num_fds);
2437 return -EINVAL;
2438 }
2439 /*
2440 * Since the parent was already fixed up, convert it
2441 * back to the kernel address space to access it
2442 */
Todd Kjos19c98722017-06-29 12:01:40 -07002443 parent_buffer = parent->buffer -
2444 binder_alloc_get_user_buffer_offset(&target_proc->alloc);
Martijn Coenendef95c72017-02-03 14:40:52 -08002445 fd_array = (u32 *)(parent_buffer + fda->parent_offset);
2446 if (!IS_ALIGNED((unsigned long)fd_array, sizeof(u32))) {
2447 binder_user_error("%d:%d parent offset not aligned correctly.\n",
2448 proc->pid, thread->pid);
2449 return -EINVAL;
2450 }
2451 for (fdi = 0; fdi < fda->num_fds; fdi++) {
2452 target_fd = binder_translate_fd(fd_array[fdi], t, thread,
2453 in_reply_to);
2454 if (target_fd < 0)
2455 goto err_translate_fd_failed;
2456 fd_array[fdi] = target_fd;
2457 }
2458 return 0;
2459
2460err_translate_fd_failed:
2461 /*
2462 * Failed to allocate fd or security error, free fds
2463 * installed so far.
2464 */
2465 num_installed_fds = fdi;
2466 for (fdi = 0; fdi < num_installed_fds; fdi++)
2467 task_close_fd(target_proc, fd_array[fdi]);
2468 return target_fd;
2469}
2470
Martijn Coenen79802402017-02-03 14:40:51 -08002471static int binder_fixup_parent(struct binder_transaction *t,
2472 struct binder_thread *thread,
2473 struct binder_buffer_object *bp,
2474 binder_size_t *off_start,
2475 binder_size_t num_valid,
2476 struct binder_buffer_object *last_fixup_obj,
2477 binder_size_t last_fixup_min_off)
2478{
2479 struct binder_buffer_object *parent;
2480 u8 *parent_buffer;
2481 struct binder_buffer *b = t->buffer;
2482 struct binder_proc *proc = thread->proc;
2483 struct binder_proc *target_proc = t->to_proc;
2484
2485 if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT))
2486 return 0;
2487
2488 parent = binder_validate_ptr(b, bp->parent, off_start, num_valid);
2489 if (!parent) {
2490 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
2491 proc->pid, thread->pid);
2492 return -EINVAL;
2493 }
2494
2495 if (!binder_validate_fixup(b, off_start,
2496 parent, bp->parent_offset,
2497 last_fixup_obj,
2498 last_fixup_min_off)) {
2499 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
2500 proc->pid, thread->pid);
2501 return -EINVAL;
2502 }
2503
2504 if (parent->length < sizeof(binder_uintptr_t) ||
2505 bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) {
2506 /* No space for a pointer here! */
2507 binder_user_error("%d:%d got transaction with invalid parent offset\n",
2508 proc->pid, thread->pid);
2509 return -EINVAL;
2510 }
2511 parent_buffer = (u8 *)(parent->buffer -
Todd Kjos19c98722017-06-29 12:01:40 -07002512 binder_alloc_get_user_buffer_offset(
2513 &target_proc->alloc));
Martijn Coenen79802402017-02-03 14:40:51 -08002514 *(binder_uintptr_t *)(parent_buffer + bp->parent_offset) = bp->buffer;
2515
2516 return 0;
2517}
2518
Martijn Coenen408c68b2017-08-31 10:04:19 +02002519/**
2520 * binder_proc_transaction() - sends a transaction to a process and wakes it up
2521 * @t: transaction to send
2522 * @proc: process to send the transaction to
2523 * @thread: thread in @proc to send the transaction to (may be NULL)
2524 *
2525 * This function queues a transaction to the specified process. It will try
2526 * to find a thread in the target process to handle the transaction and
2527 * wake it up. If no thread is found, the work is queued to the proc
2528 * waitqueue.
2529 *
2530 * If the @thread parameter is not NULL, the transaction is always queued
2531 * to the waitlist of that specific thread.
2532 *
2533 * Return: true if the transactions was successfully queued
2534 * false if the target process or thread is dead
2535 */
2536static bool binder_proc_transaction(struct binder_transaction *t,
2537 struct binder_proc *proc,
2538 struct binder_thread *thread)
2539{
2540 struct list_head *target_list = NULL;
2541 struct binder_node *node = t->buffer->target_node;
2542 bool oneway = !!(t->flags & TF_ONE_WAY);
2543 bool wakeup = true;
2544
2545 BUG_ON(!node);
2546 binder_node_lock(node);
2547 if (oneway) {
2548 BUG_ON(thread);
2549 if (node->has_async_transaction) {
2550 target_list = &node->async_todo;
2551 wakeup = false;
2552 } else {
2553 node->has_async_transaction = 1;
2554 }
2555 }
2556
2557 binder_inner_proc_lock(proc);
2558
2559 if (proc->is_dead || (thread && thread->is_dead)) {
2560 binder_inner_proc_unlock(proc);
2561 binder_node_unlock(node);
2562 return false;
2563 }
2564
2565 if (!thread && !target_list)
2566 thread = binder_select_thread_ilocked(proc);
2567
2568 if (thread)
2569 target_list = &thread->todo;
2570 else if (!target_list)
2571 target_list = &proc->todo;
2572 else
2573 BUG_ON(target_list != &node->async_todo);
2574
2575 binder_enqueue_work_ilocked(&t->work, target_list);
2576
2577 if (wakeup)
2578 binder_wakeup_thread_ilocked(proc, thread, !oneway /* sync */);
2579
2580 binder_inner_proc_unlock(proc);
2581 binder_node_unlock(node);
2582
2583 return true;
2584}
2585
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002586static void binder_transaction(struct binder_proc *proc,
2587 struct binder_thread *thread,
Martijn Coenen4bfac802017-02-03 14:40:50 -08002588 struct binder_transaction_data *tr, int reply,
2589 binder_size_t extra_buffers_size)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002590{
Martijn Coenena056af42017-02-03 14:40:49 -08002591 int ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002592 struct binder_transaction *t;
2593 struct binder_work *tcomplete;
Martijn Coenen79802402017-02-03 14:40:51 -08002594 binder_size_t *offp, *off_end, *off_start;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08002595 binder_size_t off_min;
Martijn Coenen79802402017-02-03 14:40:51 -08002596 u8 *sg_bufp, *sg_buf_end;
Todd Kjos7a4408c2017-06-29 12:01:57 -07002597 struct binder_proc *target_proc = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002598 struct binder_thread *target_thread = NULL;
2599 struct binder_node *target_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002600 struct binder_transaction *in_reply_to = NULL;
2601 struct binder_transaction_log_entry *e;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002602 uint32_t return_error = 0;
2603 uint32_t return_error_param = 0;
2604 uint32_t return_error_line = 0;
Martijn Coenen79802402017-02-03 14:40:51 -08002605 struct binder_buffer_object *last_fixup_obj = NULL;
2606 binder_size_t last_fixup_min_off = 0;
Martijn Coenen342e5c92017-02-03 14:40:46 -08002607 struct binder_context *context = proc->context;
Todd Kjosd99c7332017-06-29 12:01:53 -07002608 int t_debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002609
2610 e = binder_transaction_log_add(&binder_transaction_log);
Todd Kjosd99c7332017-06-29 12:01:53 -07002611 e->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002612 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
2613 e->from_proc = proc->pid;
2614 e->from_thread = thread->pid;
2615 e->target_handle = tr->target.handle;
2616 e->data_size = tr->data_size;
2617 e->offsets_size = tr->offsets_size;
Martijn Coenen14db3182017-02-03 14:40:47 -08002618 e->context_name = proc->context->name;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002619
2620 if (reply) {
Martijn Coenen0b89d692017-06-29 12:02:06 -07002621 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002622 in_reply_to = thread->transaction_stack;
2623 if (in_reply_to == NULL) {
Martijn Coenen0b89d692017-06-29 12:02:06 -07002624 binder_inner_proc_unlock(proc);
Anmol Sarma56b468f2012-10-30 22:35:43 +05302625 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002626 proc->pid, thread->pid);
2627 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002628 return_error_param = -EPROTO;
2629 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002630 goto err_empty_call_stack;
2631 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002632 if (in_reply_to->to_thread != thread) {
Todd Kjos7a4408c2017-06-29 12:01:57 -07002633 spin_lock(&in_reply_to->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05302634 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 +09002635 proc->pid, thread->pid, in_reply_to->debug_id,
2636 in_reply_to->to_proc ?
2637 in_reply_to->to_proc->pid : 0,
2638 in_reply_to->to_thread ?
2639 in_reply_to->to_thread->pid : 0);
Todd Kjos7a4408c2017-06-29 12:01:57 -07002640 spin_unlock(&in_reply_to->lock);
Martijn Coenen0b89d692017-06-29 12:02:06 -07002641 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002642 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002643 return_error_param = -EPROTO;
2644 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002645 in_reply_to = NULL;
2646 goto err_bad_call_stack;
2647 }
2648 thread->transaction_stack = in_reply_to->to_parent;
Martijn Coenen0b89d692017-06-29 12:02:06 -07002649 binder_inner_proc_unlock(proc);
2650 binder_set_nice(in_reply_to->saved_priority);
2651 target_thread = binder_get_txn_from_and_acq_inner(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002652 if (target_thread == NULL) {
2653 return_error = BR_DEAD_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002654 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002655 goto err_dead_binder;
2656 }
2657 if (target_thread->transaction_stack != in_reply_to) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302658 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 +09002659 proc->pid, thread->pid,
2660 target_thread->transaction_stack ?
2661 target_thread->transaction_stack->debug_id : 0,
2662 in_reply_to->debug_id);
Martijn Coenen0b89d692017-06-29 12:02:06 -07002663 binder_inner_proc_unlock(target_thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002664 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002665 return_error_param = -EPROTO;
2666 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002667 in_reply_to = NULL;
2668 target_thread = NULL;
2669 goto err_dead_binder;
2670 }
2671 target_proc = target_thread->proc;
Todd Kjos7a4408c2017-06-29 12:01:57 -07002672 target_proc->tmp_ref++;
Martijn Coenen0b89d692017-06-29 12:02:06 -07002673 binder_inner_proc_unlock(target_thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002674 } else {
2675 if (tr->target.handle) {
2676 struct binder_ref *ref;
Seunghun Lee10f62862014-05-01 01:30:23 +09002677
Todd Kjoseb349832017-06-29 12:01:56 -07002678 /*
2679 * There must already be a strong ref
2680 * on this node. If so, do a strong
2681 * increment on the node to ensure it
2682 * stays alive until the transaction is
2683 * done.
2684 */
Todd Kjos2c1838d2017-06-29 12:02:08 -07002685 binder_proc_lock(proc);
2686 ref = binder_get_ref_olocked(proc, tr->target.handle,
2687 true);
Todd Kjoseb349832017-06-29 12:01:56 -07002688 if (ref) {
2689 binder_inc_node(ref->node, 1, 0, NULL);
2690 target_node = ref->node;
2691 }
Todd Kjos2c1838d2017-06-29 12:02:08 -07002692 binder_proc_unlock(proc);
Todd Kjoseb349832017-06-29 12:01:56 -07002693 if (target_node == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302694 binder_user_error("%d:%d got transaction to invalid handle\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002695 proc->pid, thread->pid);
2696 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002697 return_error_param = -EINVAL;
2698 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002699 goto err_invalid_target_handle;
2700 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002701 } else {
Todd Kjosc44b1232017-06-29 12:01:43 -07002702 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen342e5c92017-02-03 14:40:46 -08002703 target_node = context->binder_context_mgr_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002704 if (target_node == NULL) {
2705 return_error = BR_DEAD_REPLY;
Todd Kjosc44b1232017-06-29 12:01:43 -07002706 mutex_unlock(&context->context_mgr_node_lock);
Todd Kjos57ada2f2017-06-29 12:01:46 -07002707 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002708 goto err_no_context_mgr_node;
2709 }
Todd Kjoseb349832017-06-29 12:01:56 -07002710 binder_inc_node(target_node, 1, 0, NULL);
Todd Kjosc44b1232017-06-29 12:01:43 -07002711 mutex_unlock(&context->context_mgr_node_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002712 }
2713 e->to_node = target_node->debug_id;
Todd Kjos673068e2017-06-29 12:02:03 -07002714 binder_node_lock(target_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002715 target_proc = target_node->proc;
2716 if (target_proc == NULL) {
Todd Kjos673068e2017-06-29 12:02:03 -07002717 binder_node_unlock(target_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002718 return_error = BR_DEAD_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002719 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002720 goto err_dead_binder;
2721 }
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07002722 binder_inner_proc_lock(target_proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07002723 target_proc->tmp_ref++;
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07002724 binder_inner_proc_unlock(target_proc);
Todd Kjos673068e2017-06-29 12:02:03 -07002725 binder_node_unlock(target_node);
Stephen Smalley79af7302015-01-21 10:54:10 -05002726 if (security_binder_transaction(proc->tsk,
2727 target_proc->tsk) < 0) {
2728 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002729 return_error_param = -EPERM;
2730 return_error_line = __LINE__;
Stephen Smalley79af7302015-01-21 10:54:10 -05002731 goto err_invalid_target_handle;
2732 }
Martijn Coenen0b89d692017-06-29 12:02:06 -07002733 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002734 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
2735 struct binder_transaction *tmp;
Seunghun Lee10f62862014-05-01 01:30:23 +09002736
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002737 tmp = thread->transaction_stack;
2738 if (tmp->to_thread != thread) {
Todd Kjos7a4408c2017-06-29 12:01:57 -07002739 spin_lock(&tmp->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05302740 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 +09002741 proc->pid, thread->pid, tmp->debug_id,
2742 tmp->to_proc ? tmp->to_proc->pid : 0,
2743 tmp->to_thread ?
2744 tmp->to_thread->pid : 0);
Todd Kjos7a4408c2017-06-29 12:01:57 -07002745 spin_unlock(&tmp->lock);
Martijn Coenen0b89d692017-06-29 12:02:06 -07002746 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002747 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002748 return_error_param = -EPROTO;
2749 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002750 goto err_bad_call_stack;
2751 }
2752 while (tmp) {
Todd Kjos7a4408c2017-06-29 12:01:57 -07002753 struct binder_thread *from;
2754
2755 spin_lock(&tmp->lock);
2756 from = tmp->from;
2757 if (from && from->proc == target_proc) {
2758 atomic_inc(&from->tmp_ref);
2759 target_thread = from;
2760 spin_unlock(&tmp->lock);
2761 break;
2762 }
2763 spin_unlock(&tmp->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002764 tmp = tmp->from_parent;
2765 }
2766 }
Martijn Coenen0b89d692017-06-29 12:02:06 -07002767 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002768 }
Martijn Coenen408c68b2017-08-31 10:04:19 +02002769 if (target_thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002770 e->to_thread = target_thread->pid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002771 e->to_proc = target_proc->pid;
2772
2773 /* TODO: reuse incoming transaction for reply */
2774 t = kzalloc(sizeof(*t), GFP_KERNEL);
2775 if (t == NULL) {
2776 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002777 return_error_param = -ENOMEM;
2778 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002779 goto err_alloc_t_failed;
2780 }
2781 binder_stats_created(BINDER_STAT_TRANSACTION);
Todd Kjos7a4408c2017-06-29 12:01:57 -07002782 spin_lock_init(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002783
2784 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
2785 if (tcomplete == NULL) {
2786 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002787 return_error_param = -ENOMEM;
2788 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002789 goto err_alloc_tcomplete_failed;
2790 }
2791 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
2792
Todd Kjosd99c7332017-06-29 12:01:53 -07002793 t->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002794
2795 if (reply)
2796 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen4bfac802017-02-03 14:40:50 -08002797 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002798 proc->pid, thread->pid, t->debug_id,
2799 target_proc->pid, target_thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002800 (u64)tr->data.ptr.buffer,
2801 (u64)tr->data.ptr.offsets,
Martijn Coenen4bfac802017-02-03 14:40:50 -08002802 (u64)tr->data_size, (u64)tr->offsets_size,
2803 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002804 else
2805 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen4bfac802017-02-03 14:40:50 -08002806 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002807 proc->pid, thread->pid, t->debug_id,
2808 target_proc->pid, target_node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002809 (u64)tr->data.ptr.buffer,
2810 (u64)tr->data.ptr.offsets,
Martijn Coenen4bfac802017-02-03 14:40:50 -08002811 (u64)tr->data_size, (u64)tr->offsets_size,
2812 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002813
2814 if (!reply && !(tr->flags & TF_ONE_WAY))
2815 t->from = thread;
2816 else
2817 t->from = NULL;
Tair Rzayev57bab7c2014-05-31 22:43:34 +03002818 t->sender_euid = task_euid(proc->tsk);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002819 t->to_proc = target_proc;
2820 t->to_thread = target_thread;
2821 t->code = tr->code;
2822 t->flags = tr->flags;
2823 t->priority = task_nice(current);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002824
2825 trace_binder_transaction(reply, t, target_node);
2826
Todd Kjos19c98722017-06-29 12:01:40 -07002827 t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size,
Martijn Coenen4bfac802017-02-03 14:40:50 -08002828 tr->offsets_size, extra_buffers_size,
2829 !reply && (t->flags & TF_ONE_WAY));
Todd Kjos57ada2f2017-06-29 12:01:46 -07002830 if (IS_ERR(t->buffer)) {
2831 /*
2832 * -ESRCH indicates VMA cleared. The target is dying.
2833 */
2834 return_error_param = PTR_ERR(t->buffer);
2835 return_error = return_error_param == -ESRCH ?
2836 BR_DEAD_REPLY : BR_FAILED_REPLY;
2837 return_error_line = __LINE__;
2838 t->buffer = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002839 goto err_binder_alloc_buf_failed;
2840 }
2841 t->buffer->allow_user_free = 0;
2842 t->buffer->debug_id = t->debug_id;
2843 t->buffer->transaction = t;
2844 t->buffer->target_node = target_node;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002845 trace_binder_transaction_alloc_buf(t->buffer);
Martijn Coenen79802402017-02-03 14:40:51 -08002846 off_start = (binder_size_t *)(t->buffer->data +
2847 ALIGN(tr->data_size, sizeof(void *)));
2848 offp = off_start;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002849
Arve Hjønnevågda498892014-02-21 14:40:26 -08002850 if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t)
2851 tr->data.ptr.buffer, tr->data_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302852 binder_user_error("%d:%d got transaction with invalid data ptr\n",
2853 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002854 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002855 return_error_param = -EFAULT;
2856 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002857 goto err_copy_data_failed;
2858 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08002859 if (copy_from_user(offp, (const void __user *)(uintptr_t)
2860 tr->data.ptr.offsets, tr->offsets_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302861 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
2862 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002863 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002864 return_error_param = -EFAULT;
2865 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002866 goto err_copy_data_failed;
2867 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08002868 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
2869 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
2870 proc->pid, thread->pid, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002871 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002872 return_error_param = -EINVAL;
2873 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002874 goto err_bad_offset;
2875 }
Martijn Coenen79802402017-02-03 14:40:51 -08002876 if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) {
2877 binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
2878 proc->pid, thread->pid,
2879 (u64)extra_buffers_size);
2880 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002881 return_error_param = -EINVAL;
2882 return_error_line = __LINE__;
Martijn Coenen79802402017-02-03 14:40:51 -08002883 goto err_bad_offset;
2884 }
2885 off_end = (void *)off_start + tr->offsets_size;
2886 sg_bufp = (u8 *)(PTR_ALIGN(off_end, sizeof(void *)));
2887 sg_buf_end = sg_bufp + extra_buffers_size;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08002888 off_min = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002889 for (; offp < off_end; offp++) {
Martijn Coenenfeba3902017-02-03 14:40:45 -08002890 struct binder_object_header *hdr;
2891 size_t object_size = binder_validate_object(t->buffer, *offp);
Seunghun Lee10f62862014-05-01 01:30:23 +09002892
Martijn Coenenfeba3902017-02-03 14:40:45 -08002893 if (object_size == 0 || *offp < off_min) {
2894 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 -08002895 proc->pid, thread->pid, (u64)*offp,
2896 (u64)off_min,
Martijn Coenenfeba3902017-02-03 14:40:45 -08002897 (u64)t->buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002898 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002899 return_error_param = -EINVAL;
2900 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002901 goto err_bad_offset;
2902 }
Martijn Coenenfeba3902017-02-03 14:40:45 -08002903
2904 hdr = (struct binder_object_header *)(t->buffer->data + *offp);
2905 off_min = *offp + object_size;
2906 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002907 case BINDER_TYPE_BINDER:
2908 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08002909 struct flat_binder_object *fp;
Seunghun Lee10f62862014-05-01 01:30:23 +09002910
Martijn Coenenfeba3902017-02-03 14:40:45 -08002911 fp = to_flat_binder_object(hdr);
Martijn Coenena056af42017-02-03 14:40:49 -08002912 ret = binder_translate_binder(fp, t, thread);
2913 if (ret < 0) {
Christian Engelmayer7d420432014-05-07 21:44:53 +02002914 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002915 return_error_param = ret;
2916 return_error_line = __LINE__;
Martijn Coenena056af42017-02-03 14:40:49 -08002917 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002918 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002919 } break;
2920 case BINDER_TYPE_HANDLE:
2921 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08002922 struct flat_binder_object *fp;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02002923
Martijn Coenenfeba3902017-02-03 14:40:45 -08002924 fp = to_flat_binder_object(hdr);
Martijn Coenena056af42017-02-03 14:40:49 -08002925 ret = binder_translate_handle(fp, t, thread);
2926 if (ret < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002927 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002928 return_error_param = ret;
2929 return_error_line = __LINE__;
Martijn Coenena056af42017-02-03 14:40:49 -08002930 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002931 }
2932 } break;
2933
2934 case BINDER_TYPE_FD: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08002935 struct binder_fd_object *fp = to_binder_fd_object(hdr);
Martijn Coenena056af42017-02-03 14:40:49 -08002936 int target_fd = binder_translate_fd(fp->fd, t, thread,
2937 in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002938
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002939 if (target_fd < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002940 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002941 return_error_param = target_fd;
2942 return_error_line = __LINE__;
Martijn Coenena056af42017-02-03 14:40:49 -08002943 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002944 }
Martijn Coenenfeba3902017-02-03 14:40:45 -08002945 fp->pad_binder = 0;
2946 fp->fd = target_fd;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002947 } break;
Martijn Coenendef95c72017-02-03 14:40:52 -08002948 case BINDER_TYPE_FDA: {
2949 struct binder_fd_array_object *fda =
2950 to_binder_fd_array_object(hdr);
2951 struct binder_buffer_object *parent =
2952 binder_validate_ptr(t->buffer, fda->parent,
2953 off_start,
2954 offp - off_start);
2955 if (!parent) {
2956 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
2957 proc->pid, thread->pid);
2958 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002959 return_error_param = -EINVAL;
2960 return_error_line = __LINE__;
Martijn Coenendef95c72017-02-03 14:40:52 -08002961 goto err_bad_parent;
2962 }
2963 if (!binder_validate_fixup(t->buffer, off_start,
2964 parent, fda->parent_offset,
2965 last_fixup_obj,
2966 last_fixup_min_off)) {
2967 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
2968 proc->pid, thread->pid);
2969 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002970 return_error_param = -EINVAL;
2971 return_error_line = __LINE__;
Martijn Coenendef95c72017-02-03 14:40:52 -08002972 goto err_bad_parent;
2973 }
2974 ret = binder_translate_fd_array(fda, parent, t, thread,
2975 in_reply_to);
2976 if (ret < 0) {
2977 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002978 return_error_param = ret;
2979 return_error_line = __LINE__;
Martijn Coenendef95c72017-02-03 14:40:52 -08002980 goto err_translate_failed;
2981 }
2982 last_fixup_obj = parent;
2983 last_fixup_min_off =
2984 fda->parent_offset + sizeof(u32) * fda->num_fds;
2985 } break;
Martijn Coenen79802402017-02-03 14:40:51 -08002986 case BINDER_TYPE_PTR: {
2987 struct binder_buffer_object *bp =
2988 to_binder_buffer_object(hdr);
2989 size_t buf_left = sg_buf_end - sg_bufp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002990
Martijn Coenen79802402017-02-03 14:40:51 -08002991 if (bp->length > buf_left) {
2992 binder_user_error("%d:%d got transaction with too large buffer\n",
2993 proc->pid, thread->pid);
2994 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002995 return_error_param = -EINVAL;
2996 return_error_line = __LINE__;
Martijn Coenen79802402017-02-03 14:40:51 -08002997 goto err_bad_offset;
2998 }
2999 if (copy_from_user(sg_bufp,
3000 (const void __user *)(uintptr_t)
3001 bp->buffer, bp->length)) {
3002 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3003 proc->pid, thread->pid);
Todd Kjos57ada2f2017-06-29 12:01:46 -07003004 return_error_param = -EFAULT;
Martijn Coenen79802402017-02-03 14:40:51 -08003005 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003006 return_error_line = __LINE__;
Martijn Coenen79802402017-02-03 14:40:51 -08003007 goto err_copy_data_failed;
3008 }
3009 /* Fixup buffer pointer to target proc address space */
3010 bp->buffer = (uintptr_t)sg_bufp +
Todd Kjos19c98722017-06-29 12:01:40 -07003011 binder_alloc_get_user_buffer_offset(
3012 &target_proc->alloc);
Martijn Coenen79802402017-02-03 14:40:51 -08003013 sg_bufp += ALIGN(bp->length, sizeof(u64));
3014
3015 ret = binder_fixup_parent(t, thread, bp, off_start,
3016 offp - off_start,
3017 last_fixup_obj,
3018 last_fixup_min_off);
3019 if (ret < 0) {
3020 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003021 return_error_param = ret;
3022 return_error_line = __LINE__;
Martijn Coenen79802402017-02-03 14:40:51 -08003023 goto err_translate_failed;
3024 }
3025 last_fixup_obj = bp;
3026 last_fixup_min_off = 0;
3027 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003028 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01003029 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
Martijn Coenenfeba3902017-02-03 14:40:45 -08003030 proc->pid, thread->pid, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003031 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003032 return_error_param = -EINVAL;
3033 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003034 goto err_bad_object_type;
3035 }
3036 }
Todd Kjosccae6f62017-06-29 12:01:48 -07003037 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
Todd Kjos72196392017-06-29 12:02:02 -07003038 binder_enqueue_work(proc, tcomplete, &thread->todo);
Todd Kjos673068e2017-06-29 12:02:03 -07003039 t->work.type = BINDER_WORK_TRANSACTION;
Todd Kjosccae6f62017-06-29 12:01:48 -07003040
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003041 if (reply) {
Martijn Coenen0b89d692017-06-29 12:02:06 -07003042 binder_inner_proc_lock(target_proc);
3043 if (target_thread->is_dead) {
3044 binder_inner_proc_unlock(target_proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07003045 goto err_dead_proc_or_thread;
Martijn Coenen0b89d692017-06-29 12:02:06 -07003046 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003047 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen0b89d692017-06-29 12:02:06 -07003048 binder_pop_transaction_ilocked(target_thread, in_reply_to);
Martijn Coenen408c68b2017-08-31 10:04:19 +02003049 binder_enqueue_work_ilocked(&t->work, &target_thread->todo);
Martijn Coenen0b89d692017-06-29 12:02:06 -07003050 binder_inner_proc_unlock(target_proc);
Martijn Coenen408c68b2017-08-31 10:04:19 +02003051 wake_up_interruptible_sync(&target_thread->wait);
Todd Kjosb6d282c2017-06-29 12:01:54 -07003052 binder_free_transaction(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003053 } else if (!(t->flags & TF_ONE_WAY)) {
3054 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen0b89d692017-06-29 12:02:06 -07003055 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003056 t->need_reply = 1;
3057 t->from_parent = thread->transaction_stack;
3058 thread->transaction_stack = t;
Martijn Coenen0b89d692017-06-29 12:02:06 -07003059 binder_inner_proc_unlock(proc);
Martijn Coenen408c68b2017-08-31 10:04:19 +02003060 if (!binder_proc_transaction(t, target_proc, target_thread)) {
Martijn Coenen0b89d692017-06-29 12:02:06 -07003061 binder_inner_proc_lock(proc);
3062 binder_pop_transaction_ilocked(thread, t);
3063 binder_inner_proc_unlock(proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07003064 goto err_dead_proc_or_thread;
3065 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003066 } else {
3067 BUG_ON(target_node == NULL);
3068 BUG_ON(t->buffer->async_transaction != 1);
Martijn Coenen408c68b2017-08-31 10:04:19 +02003069 if (!binder_proc_transaction(t, target_proc, NULL))
Todd Kjos7a4408c2017-06-29 12:01:57 -07003070 goto err_dead_proc_or_thread;
Riley Andrews00b40d62017-06-29 12:01:37 -07003071 }
Todd Kjos7a4408c2017-06-29 12:01:57 -07003072 if (target_thread)
3073 binder_thread_dec_tmpref(target_thread);
3074 binder_proc_dec_tmpref(target_proc);
Todd Kjosd99c7332017-06-29 12:01:53 -07003075 /*
3076 * write barrier to synchronize with initialization
3077 * of log entry
3078 */
3079 smp_wmb();
3080 WRITE_ONCE(e->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003081 return;
3082
Todd Kjos7a4408c2017-06-29 12:01:57 -07003083err_dead_proc_or_thread:
3084 return_error = BR_DEAD_REPLY;
3085 return_error_line = __LINE__;
Martijn Coenena056af42017-02-03 14:40:49 -08003086err_translate_failed:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003087err_bad_object_type:
3088err_bad_offset:
Martijn Coenendef95c72017-02-03 14:40:52 -08003089err_bad_parent:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003090err_copy_data_failed:
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003091 trace_binder_transaction_failed_buffer_release(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003092 binder_transaction_buffer_release(target_proc, t->buffer, offp);
Todd Kjoseb349832017-06-29 12:01:56 -07003093 target_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003094 t->buffer->transaction = NULL;
Todd Kjos19c98722017-06-29 12:01:40 -07003095 binder_alloc_free_buf(&target_proc->alloc, t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003096err_binder_alloc_buf_failed:
3097 kfree(tcomplete);
3098 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3099err_alloc_tcomplete_failed:
3100 kfree(t);
3101 binder_stats_deleted(BINDER_STAT_TRANSACTION);
3102err_alloc_t_failed:
3103err_bad_call_stack:
3104err_empty_call_stack:
3105err_dead_binder:
3106err_invalid_target_handle:
3107err_no_context_mgr_node:
Todd Kjos7a4408c2017-06-29 12:01:57 -07003108 if (target_thread)
3109 binder_thread_dec_tmpref(target_thread);
3110 if (target_proc)
3111 binder_proc_dec_tmpref(target_proc);
Todd Kjoseb349832017-06-29 12:01:56 -07003112 if (target_node)
3113 binder_dec_node(target_node, 1, 0);
3114
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003115 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Todd Kjos57ada2f2017-06-29 12:01:46 -07003116 "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
3117 proc->pid, thread->pid, return_error, return_error_param,
3118 (u64)tr->data_size, (u64)tr->offsets_size,
3119 return_error_line);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003120
3121 {
3122 struct binder_transaction_log_entry *fe;
Seunghun Lee10f62862014-05-01 01:30:23 +09003123
Todd Kjos57ada2f2017-06-29 12:01:46 -07003124 e->return_error = return_error;
3125 e->return_error_param = return_error_param;
3126 e->return_error_line = return_error_line;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003127 fe = binder_transaction_log_add(&binder_transaction_log_failed);
3128 *fe = *e;
Todd Kjosd99c7332017-06-29 12:01:53 -07003129 /*
3130 * write barrier to synchronize with initialization
3131 * of log entry
3132 */
3133 smp_wmb();
3134 WRITE_ONCE(e->debug_id_done, t_debug_id);
3135 WRITE_ONCE(fe->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003136 }
3137
Todd Kjos26549d12017-06-29 12:01:55 -07003138 BUG_ON(thread->return_error.cmd != BR_OK);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003139 if (in_reply_to) {
Todd Kjos26549d12017-06-29 12:01:55 -07003140 thread->return_error.cmd = BR_TRANSACTION_COMPLETE;
Todd Kjos72196392017-06-29 12:02:02 -07003141 binder_enqueue_work(thread->proc,
3142 &thread->return_error.work,
3143 &thread->todo);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003144 binder_send_failed_reply(in_reply_to, return_error);
Todd Kjos26549d12017-06-29 12:01:55 -07003145 } else {
3146 thread->return_error.cmd = return_error;
Todd Kjos72196392017-06-29 12:02:02 -07003147 binder_enqueue_work(thread->proc,
3148 &thread->return_error.work,
3149 &thread->todo);
Todd Kjos26549d12017-06-29 12:01:55 -07003150 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003151}
3152
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02003153static int binder_thread_write(struct binder_proc *proc,
3154 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003155 binder_uintptr_t binder_buffer, size_t size,
3156 binder_size_t *consumed)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003157{
3158 uint32_t cmd;
Martijn Coenen342e5c92017-02-03 14:40:46 -08003159 struct binder_context *context = proc->context;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003160 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003161 void __user *ptr = buffer + *consumed;
3162 void __user *end = buffer + size;
3163
Todd Kjos26549d12017-06-29 12:01:55 -07003164 while (ptr < end && thread->return_error.cmd == BR_OK) {
Todd Kjos372e3142017-06-29 12:01:58 -07003165 int ret;
3166
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003167 if (get_user(cmd, (uint32_t __user *)ptr))
3168 return -EFAULT;
3169 ptr += sizeof(uint32_t);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003170 trace_binder_command(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003171 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07003172 atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]);
3173 atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]);
3174 atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003175 }
3176 switch (cmd) {
3177 case BC_INCREFS:
3178 case BC_ACQUIRE:
3179 case BC_RELEASE:
3180 case BC_DECREFS: {
3181 uint32_t target;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003182 const char *debug_string;
Todd Kjos372e3142017-06-29 12:01:58 -07003183 bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE;
3184 bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE;
3185 struct binder_ref_data rdata;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003186
3187 if (get_user(target, (uint32_t __user *)ptr))
3188 return -EFAULT;
Todd Kjosc44b1232017-06-29 12:01:43 -07003189
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003190 ptr += sizeof(uint32_t);
Todd Kjos372e3142017-06-29 12:01:58 -07003191 ret = -1;
3192 if (increment && !target) {
Todd Kjosc44b1232017-06-29 12:01:43 -07003193 struct binder_node *ctx_mgr_node;
Todd Kjosc44b1232017-06-29 12:01:43 -07003194 mutex_lock(&context->context_mgr_node_lock);
3195 ctx_mgr_node = context->binder_context_mgr_node;
Todd Kjos372e3142017-06-29 12:01:58 -07003196 if (ctx_mgr_node)
3197 ret = binder_inc_ref_for_node(
3198 proc, ctx_mgr_node,
3199 strong, NULL, &rdata);
Todd Kjosc44b1232017-06-29 12:01:43 -07003200 mutex_unlock(&context->context_mgr_node_lock);
3201 }
Todd Kjos372e3142017-06-29 12:01:58 -07003202 if (ret)
3203 ret = binder_update_ref_for_handle(
3204 proc, target, increment, strong,
3205 &rdata);
3206 if (!ret && rdata.desc != target) {
3207 binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
3208 proc->pid, thread->pid,
3209 target, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003210 }
3211 switch (cmd) {
3212 case BC_INCREFS:
3213 debug_string = "IncRefs";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003214 break;
3215 case BC_ACQUIRE:
3216 debug_string = "Acquire";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003217 break;
3218 case BC_RELEASE:
3219 debug_string = "Release";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003220 break;
3221 case BC_DECREFS:
3222 default:
3223 debug_string = "DecRefs";
Todd Kjos372e3142017-06-29 12:01:58 -07003224 break;
3225 }
3226 if (ret) {
3227 binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
3228 proc->pid, thread->pid, debug_string,
3229 strong, target, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003230 break;
3231 }
3232 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjos372e3142017-06-29 12:01:58 -07003233 "%d:%d %s ref %d desc %d s %d w %d\n",
3234 proc->pid, thread->pid, debug_string,
3235 rdata.debug_id, rdata.desc, rdata.strong,
3236 rdata.weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003237 break;
3238 }
3239 case BC_INCREFS_DONE:
3240 case BC_ACQUIRE_DONE: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003241 binder_uintptr_t node_ptr;
3242 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003243 struct binder_node *node;
Todd Kjos673068e2017-06-29 12:02:03 -07003244 bool free_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003245
Arve Hjønnevågda498892014-02-21 14:40:26 -08003246 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003247 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003248 ptr += sizeof(binder_uintptr_t);
3249 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003250 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003251 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003252 node = binder_get_node(proc, node_ptr);
3253 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003254 binder_user_error("%d:%d %s u%016llx no match\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003255 proc->pid, thread->pid,
3256 cmd == BC_INCREFS_DONE ?
3257 "BC_INCREFS_DONE" :
3258 "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003259 (u64)node_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003260 break;
3261 }
3262 if (cookie != node->cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003263 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003264 proc->pid, thread->pid,
3265 cmd == BC_INCREFS_DONE ?
3266 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003267 (u64)node_ptr, node->debug_id,
3268 (u64)cookie, (u64)node->cookie);
Todd Kjosadc18842017-06-29 12:01:59 -07003269 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003270 break;
3271 }
Todd Kjos673068e2017-06-29 12:02:03 -07003272 binder_node_inner_lock(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003273 if (cmd == BC_ACQUIRE_DONE) {
3274 if (node->pending_strong_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303275 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003276 proc->pid, thread->pid,
3277 node->debug_id);
Todd Kjos673068e2017-06-29 12:02:03 -07003278 binder_node_inner_unlock(node);
Todd Kjosadc18842017-06-29 12:01:59 -07003279 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003280 break;
3281 }
3282 node->pending_strong_ref = 0;
3283 } else {
3284 if (node->pending_weak_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303285 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003286 proc->pid, thread->pid,
3287 node->debug_id);
Todd Kjos673068e2017-06-29 12:02:03 -07003288 binder_node_inner_unlock(node);
Todd Kjosadc18842017-06-29 12:01:59 -07003289 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003290 break;
3291 }
3292 node->pending_weak_ref = 0;
3293 }
Todd Kjos673068e2017-06-29 12:02:03 -07003294 free_node = binder_dec_node_nilocked(node,
3295 cmd == BC_ACQUIRE_DONE, 0);
3296 WARN_ON(free_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003297 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjosadc18842017-06-29 12:01:59 -07003298 "%d:%d %s node %d ls %d lw %d tr %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003299 proc->pid, thread->pid,
3300 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Todd Kjosadc18842017-06-29 12:01:59 -07003301 node->debug_id, node->local_strong_refs,
3302 node->local_weak_refs, node->tmp_refs);
Todd Kjos673068e2017-06-29 12:02:03 -07003303 binder_node_inner_unlock(node);
Todd Kjosadc18842017-06-29 12:01:59 -07003304 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003305 break;
3306 }
3307 case BC_ATTEMPT_ACQUIRE:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303308 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003309 return -EINVAL;
3310 case BC_ACQUIRE_RESULT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303311 pr_err("BC_ACQUIRE_RESULT not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003312 return -EINVAL;
3313
3314 case BC_FREE_BUFFER: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003315 binder_uintptr_t data_ptr;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003316 struct binder_buffer *buffer;
3317
Arve Hjønnevågda498892014-02-21 14:40:26 -08003318 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003319 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003320 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003321
Todd Kjos53d311cf2017-06-29 12:01:51 -07003322 buffer = binder_alloc_prepare_to_free(&proc->alloc,
3323 data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003324 if (buffer == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003325 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
3326 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003327 break;
3328 }
3329 if (!buffer->allow_user_free) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003330 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
3331 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003332 break;
3333 }
3334 binder_debug(BINDER_DEBUG_FREE_BUFFER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003335 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
3336 proc->pid, thread->pid, (u64)data_ptr,
3337 buffer->debug_id,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003338 buffer->transaction ? "active" : "finished");
3339
3340 if (buffer->transaction) {
3341 buffer->transaction->buffer = NULL;
3342 buffer->transaction = NULL;
3343 }
3344 if (buffer->async_transaction && buffer->target_node) {
Todd Kjos72196392017-06-29 12:02:02 -07003345 struct binder_node *buf_node;
3346 struct binder_work *w;
3347
3348 buf_node = buffer->target_node;
Todd Kjos673068e2017-06-29 12:02:03 -07003349 binder_node_inner_lock(buf_node);
Todd Kjos72196392017-06-29 12:02:02 -07003350 BUG_ON(!buf_node->has_async_transaction);
3351 BUG_ON(buf_node->proc != proc);
Todd Kjos72196392017-06-29 12:02:02 -07003352 w = binder_dequeue_work_head_ilocked(
3353 &buf_node->async_todo);
3354 if (!w)
3355 buf_node->has_async_transaction = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003356 else
Todd Kjos72196392017-06-29 12:02:02 -07003357 binder_enqueue_work_ilocked(
3358 w, &thread->todo);
Todd Kjos673068e2017-06-29 12:02:03 -07003359 binder_node_inner_unlock(buf_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003360 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003361 trace_binder_transaction_buffer_release(buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003362 binder_transaction_buffer_release(proc, buffer, NULL);
Todd Kjos19c98722017-06-29 12:01:40 -07003363 binder_alloc_free_buf(&proc->alloc, buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003364 break;
3365 }
3366
Martijn Coenen79802402017-02-03 14:40:51 -08003367 case BC_TRANSACTION_SG:
3368 case BC_REPLY_SG: {
3369 struct binder_transaction_data_sg tr;
3370
3371 if (copy_from_user(&tr, ptr, sizeof(tr)))
3372 return -EFAULT;
3373 ptr += sizeof(tr);
3374 binder_transaction(proc, thread, &tr.transaction_data,
3375 cmd == BC_REPLY_SG, tr.buffers_size);
3376 break;
3377 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003378 case BC_TRANSACTION:
3379 case BC_REPLY: {
3380 struct binder_transaction_data tr;
3381
3382 if (copy_from_user(&tr, ptr, sizeof(tr)))
3383 return -EFAULT;
3384 ptr += sizeof(tr);
Martijn Coenen4bfac802017-02-03 14:40:50 -08003385 binder_transaction(proc, thread, &tr,
3386 cmd == BC_REPLY, 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003387 break;
3388 }
3389
3390 case BC_REGISTER_LOOPER:
3391 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303392 "%d:%d BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003393 proc->pid, thread->pid);
Todd Kjosb3e68612017-06-29 12:02:07 -07003394 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003395 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
3396 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303397 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003398 proc->pid, thread->pid);
3399 } else if (proc->requested_threads == 0) {
3400 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303401 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003402 proc->pid, thread->pid);
3403 } else {
3404 proc->requested_threads--;
3405 proc->requested_threads_started++;
3406 }
3407 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
Todd Kjosb3e68612017-06-29 12:02:07 -07003408 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003409 break;
3410 case BC_ENTER_LOOPER:
3411 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303412 "%d:%d BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003413 proc->pid, thread->pid);
3414 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
3415 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303416 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003417 proc->pid, thread->pid);
3418 }
3419 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
3420 break;
3421 case BC_EXIT_LOOPER:
3422 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303423 "%d:%d BC_EXIT_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003424 proc->pid, thread->pid);
3425 thread->looper |= BINDER_LOOPER_STATE_EXITED;
3426 break;
3427
3428 case BC_REQUEST_DEATH_NOTIFICATION:
3429 case BC_CLEAR_DEATH_NOTIFICATION: {
3430 uint32_t target;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003431 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003432 struct binder_ref *ref;
Todd Kjos2c1838d2017-06-29 12:02:08 -07003433 struct binder_ref_death *death = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003434
3435 if (get_user(target, (uint32_t __user *)ptr))
3436 return -EFAULT;
3437 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08003438 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003439 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003440 ptr += sizeof(binder_uintptr_t);
Todd Kjos2c1838d2017-06-29 12:02:08 -07003441 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3442 /*
3443 * Allocate memory for death notification
3444 * before taking lock
3445 */
3446 death = kzalloc(sizeof(*death), GFP_KERNEL);
3447 if (death == NULL) {
3448 WARN_ON(thread->return_error.cmd !=
3449 BR_OK);
3450 thread->return_error.cmd = BR_ERROR;
3451 binder_enqueue_work(
3452 thread->proc,
3453 &thread->return_error.work,
3454 &thread->todo);
3455 binder_debug(
3456 BINDER_DEBUG_FAILED_TRANSACTION,
3457 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
3458 proc->pid, thread->pid);
3459 break;
3460 }
3461 }
3462 binder_proc_lock(proc);
3463 ref = binder_get_ref_olocked(proc, target, false);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003464 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303465 binder_user_error("%d:%d %s invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003466 proc->pid, thread->pid,
3467 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3468 "BC_REQUEST_DEATH_NOTIFICATION" :
3469 "BC_CLEAR_DEATH_NOTIFICATION",
3470 target);
Todd Kjos2c1838d2017-06-29 12:02:08 -07003471 binder_proc_unlock(proc);
3472 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003473 break;
3474 }
3475
3476 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003477 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003478 proc->pid, thread->pid,
3479 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3480 "BC_REQUEST_DEATH_NOTIFICATION" :
3481 "BC_CLEAR_DEATH_NOTIFICATION",
Todd Kjos372e3142017-06-29 12:01:58 -07003482 (u64)cookie, ref->data.debug_id,
3483 ref->data.desc, ref->data.strong,
3484 ref->data.weak, ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003485
Martijn Coenenab51ec62017-06-29 12:02:10 -07003486 binder_node_lock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003487 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3488 if (ref->death) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303489 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003490 proc->pid, thread->pid);
Martijn Coenenab51ec62017-06-29 12:02:10 -07003491 binder_node_unlock(ref->node);
Todd Kjos2c1838d2017-06-29 12:02:08 -07003492 binder_proc_unlock(proc);
3493 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003494 break;
3495 }
3496 binder_stats_created(BINDER_STAT_DEATH);
3497 INIT_LIST_HEAD(&death->work.entry);
3498 death->cookie = cookie;
3499 ref->death = death;
3500 if (ref->node->proc == NULL) {
3501 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
Martijn Coenenbb745622017-08-31 10:04:28 +02003502
3503 binder_inner_proc_lock(proc);
3504 binder_enqueue_work_ilocked(
3505 &ref->death->work, &proc->todo);
3506 binder_wakeup_proc_ilocked(proc);
3507 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003508 }
3509 } else {
3510 if (ref->death == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303511 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003512 proc->pid, thread->pid);
Todd Kjos673068e2017-06-29 12:02:03 -07003513 binder_node_unlock(ref->node);
Todd Kjos2c1838d2017-06-29 12:02:08 -07003514 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003515 break;
3516 }
3517 death = ref->death;
3518 if (death->cookie != cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003519 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003520 proc->pid, thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003521 (u64)death->cookie,
3522 (u64)cookie);
Todd Kjos673068e2017-06-29 12:02:03 -07003523 binder_node_unlock(ref->node);
Todd Kjos2c1838d2017-06-29 12:02:08 -07003524 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003525 break;
3526 }
3527 ref->death = NULL;
Todd Kjos72196392017-06-29 12:02:02 -07003528 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003529 if (list_empty(&death->work.entry)) {
3530 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos72196392017-06-29 12:02:02 -07003531 if (thread->looper &
3532 (BINDER_LOOPER_STATE_REGISTERED |
3533 BINDER_LOOPER_STATE_ENTERED))
3534 binder_enqueue_work_ilocked(
3535 &death->work,
3536 &thread->todo);
3537 else {
3538 binder_enqueue_work_ilocked(
3539 &death->work,
3540 &proc->todo);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02003541 binder_wakeup_proc_ilocked(
Martijn Coenen408c68b2017-08-31 10:04:19 +02003542 proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003543 }
3544 } else {
3545 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
3546 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
3547 }
Todd Kjos72196392017-06-29 12:02:02 -07003548 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003549 }
Martijn Coenenab51ec62017-06-29 12:02:10 -07003550 binder_node_unlock(ref->node);
Todd Kjos2c1838d2017-06-29 12:02:08 -07003551 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003552 } break;
3553 case BC_DEAD_BINDER_DONE: {
3554 struct binder_work *w;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003555 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003556 struct binder_ref_death *death = NULL;
Seunghun Lee10f62862014-05-01 01:30:23 +09003557
Arve Hjønnevågda498892014-02-21 14:40:26 -08003558 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003559 return -EFAULT;
3560
Lisa Du7a64cd82016-02-17 09:32:52 +08003561 ptr += sizeof(cookie);
Todd Kjos72196392017-06-29 12:02:02 -07003562 binder_inner_proc_lock(proc);
3563 list_for_each_entry(w, &proc->delivered_death,
3564 entry) {
3565 struct binder_ref_death *tmp_death =
3566 container_of(w,
3567 struct binder_ref_death,
3568 work);
Seunghun Lee10f62862014-05-01 01:30:23 +09003569
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003570 if (tmp_death->cookie == cookie) {
3571 death = tmp_death;
3572 break;
3573 }
3574 }
3575 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003576 "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n",
3577 proc->pid, thread->pid, (u64)cookie,
3578 death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003579 if (death == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003580 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
3581 proc->pid, thread->pid, (u64)cookie);
Todd Kjos72196392017-06-29 12:02:02 -07003582 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003583 break;
3584 }
Todd Kjos72196392017-06-29 12:02:02 -07003585 binder_dequeue_work_ilocked(&death->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003586 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
3587 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos72196392017-06-29 12:02:02 -07003588 if (thread->looper &
3589 (BINDER_LOOPER_STATE_REGISTERED |
3590 BINDER_LOOPER_STATE_ENTERED))
3591 binder_enqueue_work_ilocked(
3592 &death->work, &thread->todo);
3593 else {
3594 binder_enqueue_work_ilocked(
3595 &death->work,
3596 &proc->todo);
Martijn Coenen408c68b2017-08-31 10:04:19 +02003597 binder_wakeup_proc_ilocked(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003598 }
3599 }
Todd Kjos72196392017-06-29 12:02:02 -07003600 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003601 } break;
3602
3603 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303604 pr_err("%d:%d unknown command %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003605 proc->pid, thread->pid, cmd);
3606 return -EINVAL;
3607 }
3608 *consumed = ptr - buffer;
3609 }
3610 return 0;
3611}
3612
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02003613static void binder_stat_br(struct binder_proc *proc,
3614 struct binder_thread *thread, uint32_t cmd)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003615{
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003616 trace_binder_return(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003617 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07003618 atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
3619 atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
3620 atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003621 }
3622}
3623
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003624static int binder_has_thread_work(struct binder_thread *thread)
3625{
Todd Kjos72196392017-06-29 12:02:02 -07003626 return !binder_worklist_empty(thread->proc, &thread->todo) ||
3627 thread->looper_need_return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003628}
3629
Todd Kjos26b47d82017-06-29 12:01:47 -07003630static int binder_put_node_cmd(struct binder_proc *proc,
3631 struct binder_thread *thread,
3632 void __user **ptrp,
3633 binder_uintptr_t node_ptr,
3634 binder_uintptr_t node_cookie,
3635 int node_debug_id,
3636 uint32_t cmd, const char *cmd_name)
3637{
3638 void __user *ptr = *ptrp;
3639
3640 if (put_user(cmd, (uint32_t __user *)ptr))
3641 return -EFAULT;
3642 ptr += sizeof(uint32_t);
3643
3644 if (put_user(node_ptr, (binder_uintptr_t __user *)ptr))
3645 return -EFAULT;
3646 ptr += sizeof(binder_uintptr_t);
3647
3648 if (put_user(node_cookie, (binder_uintptr_t __user *)ptr))
3649 return -EFAULT;
3650 ptr += sizeof(binder_uintptr_t);
3651
3652 binder_stat_br(proc, thread, cmd);
3653 binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n",
3654 proc->pid, thread->pid, cmd_name, node_debug_id,
3655 (u64)node_ptr, (u64)node_cookie);
3656
3657 *ptrp = ptr;
3658 return 0;
3659}
3660
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02003661static int binder_wait_for_work(struct binder_thread *thread,
3662 bool do_proc_work)
3663{
3664 DEFINE_WAIT(wait);
3665 struct binder_proc *proc = thread->proc;
3666 int ret = 0;
3667
3668 freezer_do_not_count();
3669 binder_inner_proc_lock(proc);
3670 for (;;) {
3671 prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE);
3672 if (binder_has_work_ilocked(thread, do_proc_work))
3673 break;
3674 if (do_proc_work)
3675 list_add(&thread->waiting_thread_node,
3676 &proc->waiting_threads);
3677 binder_inner_proc_unlock(proc);
3678 schedule();
3679 binder_inner_proc_lock(proc);
3680 list_del_init(&thread->waiting_thread_node);
3681 if (signal_pending(current)) {
3682 ret = -ERESTARTSYS;
3683 break;
3684 }
3685 }
3686 finish_wait(&thread->wait, &wait);
3687 binder_inner_proc_unlock(proc);
3688 freezer_count();
3689
3690 return ret;
3691}
3692
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003693static int binder_thread_read(struct binder_proc *proc,
3694 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003695 binder_uintptr_t binder_buffer, size_t size,
3696 binder_size_t *consumed, int non_block)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003697{
Arve Hjønnevågda498892014-02-21 14:40:26 -08003698 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003699 void __user *ptr = buffer + *consumed;
3700 void __user *end = buffer + size;
3701
3702 int ret = 0;
3703 int wait_for_proc_work;
3704
3705 if (*consumed == 0) {
3706 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
3707 return -EFAULT;
3708 ptr += sizeof(uint32_t);
3709 }
3710
3711retry:
Martijn Coenen0b89d692017-06-29 12:02:06 -07003712 binder_inner_proc_lock(proc);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02003713 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
Martijn Coenen0b89d692017-06-29 12:02:06 -07003714 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003715
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003716 thread->looper |= BINDER_LOOPER_STATE_WAITING;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003717
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003718 trace_binder_wait_for_work(wait_for_proc_work,
3719 !!thread->transaction_stack,
Todd Kjos72196392017-06-29 12:02:02 -07003720 !binder_worklist_empty(proc, &thread->todo));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003721 if (wait_for_proc_work) {
3722 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
3723 BINDER_LOOPER_STATE_ENTERED))) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303724 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 +09003725 proc->pid, thread->pid, thread->looper);
3726 wait_event_interruptible(binder_user_error_wait,
3727 binder_stop_on_user_error < 2);
3728 }
3729 binder_set_nice(proc->default_priority);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003730 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003731
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02003732 if (non_block) {
3733 if (!binder_has_work(thread, wait_for_proc_work))
3734 ret = -EAGAIN;
3735 } else {
3736 ret = binder_wait_for_work(thread, wait_for_proc_work);
3737 }
3738
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003739 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
3740
3741 if (ret)
3742 return ret;
3743
3744 while (1) {
3745 uint32_t cmd;
3746 struct binder_transaction_data tr;
Todd Kjos72196392017-06-29 12:02:02 -07003747 struct binder_work *w = NULL;
3748 struct list_head *list = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003749 struct binder_transaction *t = NULL;
Todd Kjos7a4408c2017-06-29 12:01:57 -07003750 struct binder_thread *t_from;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003751
Todd Kjosed297212017-06-29 12:02:01 -07003752 binder_inner_proc_lock(proc);
Todd Kjos72196392017-06-29 12:02:02 -07003753 if (!binder_worklist_empty_ilocked(&thread->todo))
3754 list = &thread->todo;
3755 else if (!binder_worklist_empty_ilocked(&proc->todo) &&
3756 wait_for_proc_work)
3757 list = &proc->todo;
3758 else {
3759 binder_inner_proc_unlock(proc);
3760
Dmitry Voytik395262a2014-09-08 18:16:34 +04003761 /* no data added */
Todd Kjos08dabce2017-06-29 12:01:49 -07003762 if (ptr - buffer == 4 && !thread->looper_need_return)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003763 goto retry;
3764 break;
3765 }
3766
Todd Kjosed297212017-06-29 12:02:01 -07003767 if (end - ptr < sizeof(tr) + 4) {
3768 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003769 break;
Todd Kjosed297212017-06-29 12:02:01 -07003770 }
Todd Kjos72196392017-06-29 12:02:02 -07003771 w = binder_dequeue_work_head_ilocked(list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003772
3773 switch (w->type) {
3774 case BINDER_WORK_TRANSACTION: {
Todd Kjosed297212017-06-29 12:02:01 -07003775 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003776 t = container_of(w, struct binder_transaction, work);
3777 } break;
Todd Kjos26549d12017-06-29 12:01:55 -07003778 case BINDER_WORK_RETURN_ERROR: {
3779 struct binder_error *e = container_of(
3780 w, struct binder_error, work);
3781
3782 WARN_ON(e->cmd == BR_OK);
Todd Kjosed297212017-06-29 12:02:01 -07003783 binder_inner_proc_unlock(proc);
Todd Kjos26549d12017-06-29 12:01:55 -07003784 if (put_user(e->cmd, (uint32_t __user *)ptr))
3785 return -EFAULT;
3786 e->cmd = BR_OK;
3787 ptr += sizeof(uint32_t);
3788
Todd Kjos4f9adc82017-08-08 15:48:36 -07003789 binder_stat_br(proc, thread, e->cmd);
Todd Kjos26549d12017-06-29 12:01:55 -07003790 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003791 case BINDER_WORK_TRANSACTION_COMPLETE: {
Todd Kjosed297212017-06-29 12:02:01 -07003792 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003793 cmd = BR_TRANSACTION_COMPLETE;
3794 if (put_user(cmd, (uint32_t __user *)ptr))
3795 return -EFAULT;
3796 ptr += sizeof(uint32_t);
3797
3798 binder_stat_br(proc, thread, cmd);
3799 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303800 "%d:%d BR_TRANSACTION_COMPLETE\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003801 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003802 kfree(w);
3803 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3804 } break;
3805 case BINDER_WORK_NODE: {
3806 struct binder_node *node = container_of(w, struct binder_node, work);
Todd Kjos26b47d82017-06-29 12:01:47 -07003807 int strong, weak;
3808 binder_uintptr_t node_ptr = node->ptr;
3809 binder_uintptr_t node_cookie = node->cookie;
3810 int node_debug_id = node->debug_id;
3811 int has_weak_ref;
3812 int has_strong_ref;
3813 void __user *orig_ptr = ptr;
Seunghun Lee10f62862014-05-01 01:30:23 +09003814
Todd Kjos26b47d82017-06-29 12:01:47 -07003815 BUG_ON(proc != node->proc);
3816 strong = node->internal_strong_refs ||
3817 node->local_strong_refs;
3818 weak = !hlist_empty(&node->refs) ||
Todd Kjosadc18842017-06-29 12:01:59 -07003819 node->local_weak_refs ||
3820 node->tmp_refs || strong;
Todd Kjos26b47d82017-06-29 12:01:47 -07003821 has_strong_ref = node->has_strong_ref;
3822 has_weak_ref = node->has_weak_ref;
3823
3824 if (weak && !has_weak_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003825 node->has_weak_ref = 1;
3826 node->pending_weak_ref = 1;
3827 node->local_weak_refs++;
Todd Kjos26b47d82017-06-29 12:01:47 -07003828 }
3829 if (strong && !has_strong_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003830 node->has_strong_ref = 1;
3831 node->pending_strong_ref = 1;
3832 node->local_strong_refs++;
Todd Kjos26b47d82017-06-29 12:01:47 -07003833 }
3834 if (!strong && has_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003835 node->has_strong_ref = 0;
Todd Kjos26b47d82017-06-29 12:01:47 -07003836 if (!weak && has_weak_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003837 node->has_weak_ref = 0;
Todd Kjos26b47d82017-06-29 12:01:47 -07003838 if (!weak && !strong) {
3839 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
3840 "%d:%d node %d u%016llx c%016llx deleted\n",
3841 proc->pid, thread->pid,
3842 node_debug_id,
3843 (u64)node_ptr,
3844 (u64)node_cookie);
3845 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjosed297212017-06-29 12:02:01 -07003846 binder_inner_proc_unlock(proc);
Todd Kjos673068e2017-06-29 12:02:03 -07003847 binder_node_lock(node);
3848 /*
3849 * Acquire the node lock before freeing the
3850 * node to serialize with other threads that
3851 * may have been holding the node lock while
3852 * decrementing this node (avoids race where
3853 * this thread frees while the other thread
3854 * is unlocking the node after the final
3855 * decrement)
3856 */
3857 binder_node_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07003858 binder_free_node(node);
3859 } else
3860 binder_inner_proc_unlock(proc);
3861
Todd Kjos26b47d82017-06-29 12:01:47 -07003862 if (weak && !has_weak_ref)
3863 ret = binder_put_node_cmd(
3864 proc, thread, &ptr, node_ptr,
3865 node_cookie, node_debug_id,
3866 BR_INCREFS, "BR_INCREFS");
3867 if (!ret && strong && !has_strong_ref)
3868 ret = binder_put_node_cmd(
3869 proc, thread, &ptr, node_ptr,
3870 node_cookie, node_debug_id,
3871 BR_ACQUIRE, "BR_ACQUIRE");
3872 if (!ret && !strong && has_strong_ref)
3873 ret = binder_put_node_cmd(
3874 proc, thread, &ptr, node_ptr,
3875 node_cookie, node_debug_id,
3876 BR_RELEASE, "BR_RELEASE");
3877 if (!ret && !weak && has_weak_ref)
3878 ret = binder_put_node_cmd(
3879 proc, thread, &ptr, node_ptr,
3880 node_cookie, node_debug_id,
3881 BR_DECREFS, "BR_DECREFS");
3882 if (orig_ptr == ptr)
3883 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
3884 "%d:%d node %d u%016llx c%016llx state unchanged\n",
3885 proc->pid, thread->pid,
3886 node_debug_id,
3887 (u64)node_ptr,
3888 (u64)node_cookie);
3889 if (ret)
3890 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003891 } break;
3892 case BINDER_WORK_DEAD_BINDER:
3893 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
3894 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
3895 struct binder_ref_death *death;
3896 uint32_t cmd;
Martijn Coenenab51ec62017-06-29 12:02:10 -07003897 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003898
3899 death = container_of(w, struct binder_ref_death, work);
3900 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
3901 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
3902 else
3903 cmd = BR_DEAD_BINDER;
Martijn Coenenab51ec62017-06-29 12:02:10 -07003904 cookie = death->cookie;
3905
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003906 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003907 "%d:%d %s %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003908 proc->pid, thread->pid,
3909 cmd == BR_DEAD_BINDER ?
3910 "BR_DEAD_BINDER" :
3911 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
Martijn Coenenab51ec62017-06-29 12:02:10 -07003912 (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003913 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
Martijn Coenenab51ec62017-06-29 12:02:10 -07003914 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003915 kfree(death);
3916 binder_stats_deleted(BINDER_STAT_DEATH);
Todd Kjosed297212017-06-29 12:02:01 -07003917 } else {
Todd Kjos72196392017-06-29 12:02:02 -07003918 binder_enqueue_work_ilocked(
3919 w, &proc->delivered_death);
Todd Kjosed297212017-06-29 12:02:01 -07003920 binder_inner_proc_unlock(proc);
3921 }
Martijn Coenenab51ec62017-06-29 12:02:10 -07003922 if (put_user(cmd, (uint32_t __user *)ptr))
3923 return -EFAULT;
3924 ptr += sizeof(uint32_t);
3925 if (put_user(cookie,
3926 (binder_uintptr_t __user *)ptr))
3927 return -EFAULT;
3928 ptr += sizeof(binder_uintptr_t);
3929 binder_stat_br(proc, thread, cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003930 if (cmd == BR_DEAD_BINDER)
3931 goto done; /* DEAD_BINDER notifications can cause transactions */
3932 } break;
3933 }
3934
3935 if (!t)
3936 continue;
3937
3938 BUG_ON(t->buffer == NULL);
3939 if (t->buffer->target_node) {
3940 struct binder_node *target_node = t->buffer->target_node;
Seunghun Lee10f62862014-05-01 01:30:23 +09003941
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003942 tr.target.ptr = target_node->ptr;
3943 tr.cookie = target_node->cookie;
3944 t->saved_priority = task_nice(current);
3945 if (t->priority < target_node->min_priority &&
3946 !(t->flags & TF_ONE_WAY))
3947 binder_set_nice(t->priority);
3948 else if (!(t->flags & TF_ONE_WAY) ||
3949 t->saved_priority > target_node->min_priority)
3950 binder_set_nice(target_node->min_priority);
3951 cmd = BR_TRANSACTION;
3952 } else {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003953 tr.target.ptr = 0;
3954 tr.cookie = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003955 cmd = BR_REPLY;
3956 }
3957 tr.code = t->code;
3958 tr.flags = t->flags;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -06003959 tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003960
Todd Kjos7a4408c2017-06-29 12:01:57 -07003961 t_from = binder_get_txn_from(t);
3962 if (t_from) {
3963 struct task_struct *sender = t_from->proc->tsk;
Seunghun Lee10f62862014-05-01 01:30:23 +09003964
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003965 tr.sender_pid = task_tgid_nr_ns(sender,
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003966 task_active_pid_ns(current));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003967 } else {
3968 tr.sender_pid = 0;
3969 }
3970
3971 tr.data_size = t->buffer->data_size;
3972 tr.offsets_size = t->buffer->offsets_size;
Todd Kjos19c98722017-06-29 12:01:40 -07003973 tr.data.ptr.buffer = (binder_uintptr_t)
3974 ((uintptr_t)t->buffer->data +
3975 binder_alloc_get_user_buffer_offset(&proc->alloc));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003976 tr.data.ptr.offsets = tr.data.ptr.buffer +
3977 ALIGN(t->buffer->data_size,
3978 sizeof(void *));
3979
Todd Kjos7a4408c2017-06-29 12:01:57 -07003980 if (put_user(cmd, (uint32_t __user *)ptr)) {
3981 if (t_from)
3982 binder_thread_dec_tmpref(t_from);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003983 return -EFAULT;
Todd Kjos7a4408c2017-06-29 12:01:57 -07003984 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003985 ptr += sizeof(uint32_t);
Todd Kjos7a4408c2017-06-29 12:01:57 -07003986 if (copy_to_user(ptr, &tr, sizeof(tr))) {
3987 if (t_from)
3988 binder_thread_dec_tmpref(t_from);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003989 return -EFAULT;
Todd Kjos7a4408c2017-06-29 12:01:57 -07003990 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003991 ptr += sizeof(tr);
3992
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003993 trace_binder_transaction_received(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003994 binder_stat_br(proc, thread, cmd);
3995 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003996 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003997 proc->pid, thread->pid,
3998 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
3999 "BR_REPLY",
Todd Kjos7a4408c2017-06-29 12:01:57 -07004000 t->debug_id, t_from ? t_from->proc->pid : 0,
4001 t_from ? t_from->pid : 0, cmd,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004002 t->buffer->data_size, t->buffer->offsets_size,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004003 (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004004
Todd Kjos7a4408c2017-06-29 12:01:57 -07004005 if (t_from)
4006 binder_thread_dec_tmpref(t_from);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004007 t->buffer->allow_user_free = 1;
4008 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
Martijn Coenen0b89d692017-06-29 12:02:06 -07004009 binder_inner_proc_lock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004010 t->to_parent = thread->transaction_stack;
4011 t->to_thread = thread;
4012 thread->transaction_stack = t;
Martijn Coenen0b89d692017-06-29 12:02:06 -07004013 binder_inner_proc_unlock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004014 } else {
Todd Kjosb6d282c2017-06-29 12:01:54 -07004015 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004016 }
4017 break;
4018 }
4019
4020done:
4021
4022 *consumed = ptr - buffer;
Todd Kjosb3e68612017-06-29 12:02:07 -07004023 binder_inner_proc_lock(proc);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004024 if (proc->requested_threads == 0 &&
4025 list_empty(&thread->proc->waiting_threads) &&
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004026 proc->requested_threads_started < proc->max_threads &&
4027 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4028 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
4029 /*spawn a new thread if we leave this out */) {
4030 proc->requested_threads++;
Todd Kjosb3e68612017-06-29 12:02:07 -07004031 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004032 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304033 "%d:%d BR_SPAWN_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004034 proc->pid, thread->pid);
4035 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
4036 return -EFAULT;
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07004037 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
Todd Kjosb3e68612017-06-29 12:02:07 -07004038 } else
4039 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004040 return 0;
4041}
4042
Todd Kjos72196392017-06-29 12:02:02 -07004043static void binder_release_work(struct binder_proc *proc,
4044 struct list_head *list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004045{
4046 struct binder_work *w;
Seunghun Lee10f62862014-05-01 01:30:23 +09004047
Todd Kjos72196392017-06-29 12:02:02 -07004048 while (1) {
4049 w = binder_dequeue_work_head(proc, list);
4050 if (!w)
4051 return;
4052
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004053 switch (w->type) {
4054 case BINDER_WORK_TRANSACTION: {
4055 struct binder_transaction *t;
4056
4057 t = container_of(w, struct binder_transaction, work);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004058 if (t->buffer->target_node &&
4059 !(t->flags & TF_ONE_WAY)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004060 binder_send_failed_reply(t, BR_DEAD_REPLY);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004061 } else {
4062 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304063 "undelivered transaction %d\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004064 t->debug_id);
Todd Kjosb6d282c2017-06-29 12:01:54 -07004065 binder_free_transaction(t);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004066 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004067 } break;
Todd Kjos26549d12017-06-29 12:01:55 -07004068 case BINDER_WORK_RETURN_ERROR: {
4069 struct binder_error *e = container_of(
4070 w, struct binder_error, work);
4071
4072 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4073 "undelivered TRANSACTION_ERROR: %u\n",
4074 e->cmd);
4075 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004076 case BINDER_WORK_TRANSACTION_COMPLETE: {
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004077 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304078 "undelivered TRANSACTION_COMPLETE\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004079 kfree(w);
4080 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4081 } break;
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004082 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4083 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4084 struct binder_ref_death *death;
4085
4086 death = container_of(w, struct binder_ref_death, work);
4087 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004088 "undelivered death notification, %016llx\n",
4089 (u64)death->cookie);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004090 kfree(death);
4091 binder_stats_deleted(BINDER_STAT_DEATH);
4092 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004093 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304094 pr_err("unexpected work type, %d, not freed\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004095 w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004096 break;
4097 }
4098 }
4099
4100}
4101
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004102static struct binder_thread *binder_get_thread_ilocked(
4103 struct binder_proc *proc, struct binder_thread *new_thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004104{
4105 struct binder_thread *thread = NULL;
4106 struct rb_node *parent = NULL;
4107 struct rb_node **p = &proc->threads.rb_node;
4108
4109 while (*p) {
4110 parent = *p;
4111 thread = rb_entry(parent, struct binder_thread, rb_node);
4112
4113 if (current->pid < thread->pid)
4114 p = &(*p)->rb_left;
4115 else if (current->pid > thread->pid)
4116 p = &(*p)->rb_right;
4117 else
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004118 return thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004119 }
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004120 if (!new_thread)
4121 return NULL;
4122 thread = new_thread;
4123 binder_stats_created(BINDER_STAT_THREAD);
4124 thread->proc = proc;
4125 thread->pid = current->pid;
4126 atomic_set(&thread->tmp_ref, 0);
4127 init_waitqueue_head(&thread->wait);
4128 INIT_LIST_HEAD(&thread->todo);
4129 rb_link_node(&thread->rb_node, parent, p);
4130 rb_insert_color(&thread->rb_node, &proc->threads);
4131 thread->looper_need_return = true;
4132 thread->return_error.work.type = BINDER_WORK_RETURN_ERROR;
4133 thread->return_error.cmd = BR_OK;
4134 thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR;
4135 thread->reply_error.cmd = BR_OK;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004136 INIT_LIST_HEAD(&new_thread->waiting_thread_node);
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004137 return thread;
4138}
4139
4140static struct binder_thread *binder_get_thread(struct binder_proc *proc)
4141{
4142 struct binder_thread *thread;
4143 struct binder_thread *new_thread;
4144
4145 binder_inner_proc_lock(proc);
4146 thread = binder_get_thread_ilocked(proc, NULL);
4147 binder_inner_proc_unlock(proc);
4148 if (!thread) {
4149 new_thread = kzalloc(sizeof(*thread), GFP_KERNEL);
4150 if (new_thread == NULL)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004151 return NULL;
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004152 binder_inner_proc_lock(proc);
4153 thread = binder_get_thread_ilocked(proc, new_thread);
4154 binder_inner_proc_unlock(proc);
4155 if (thread != new_thread)
4156 kfree(new_thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004157 }
4158 return thread;
4159}
4160
Todd Kjos7a4408c2017-06-29 12:01:57 -07004161static void binder_free_proc(struct binder_proc *proc)
4162{
4163 BUG_ON(!list_empty(&proc->todo));
4164 BUG_ON(!list_empty(&proc->delivered_death));
4165 binder_alloc_deferred_release(&proc->alloc);
4166 put_task_struct(proc->tsk);
4167 binder_stats_deleted(BINDER_STAT_PROC);
4168 kfree(proc);
4169}
4170
4171static void binder_free_thread(struct binder_thread *thread)
4172{
4173 BUG_ON(!list_empty(&thread->todo));
4174 binder_stats_deleted(BINDER_STAT_THREAD);
4175 binder_proc_dec_tmpref(thread->proc);
4176 kfree(thread);
4177}
4178
4179static int binder_thread_release(struct binder_proc *proc,
4180 struct binder_thread *thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004181{
4182 struct binder_transaction *t;
4183 struct binder_transaction *send_reply = NULL;
4184 int active_transactions = 0;
Todd Kjos7a4408c2017-06-29 12:01:57 -07004185 struct binder_transaction *last_t = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004186
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004187 binder_inner_proc_lock(thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07004188 /*
4189 * take a ref on the proc so it survives
4190 * after we remove this thread from proc->threads.
4191 * The corresponding dec is when we actually
4192 * free the thread in binder_free_thread()
4193 */
4194 proc->tmp_ref++;
4195 /*
4196 * take a ref on this thread to ensure it
4197 * survives while we are releasing it
4198 */
4199 atomic_inc(&thread->tmp_ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004200 rb_erase(&thread->rb_node, &proc->threads);
4201 t = thread->transaction_stack;
Todd Kjos7a4408c2017-06-29 12:01:57 -07004202 if (t) {
4203 spin_lock(&t->lock);
4204 if (t->to_thread == thread)
4205 send_reply = t;
4206 }
4207 thread->is_dead = true;
4208
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004209 while (t) {
Todd Kjos7a4408c2017-06-29 12:01:57 -07004210 last_t = t;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004211 active_transactions++;
4212 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304213 "release %d:%d transaction %d %s, still active\n",
4214 proc->pid, thread->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004215 t->debug_id,
4216 (t->to_thread == thread) ? "in" : "out");
4217
4218 if (t->to_thread == thread) {
4219 t->to_proc = NULL;
4220 t->to_thread = NULL;
4221 if (t->buffer) {
4222 t->buffer->transaction = NULL;
4223 t->buffer = NULL;
4224 }
4225 t = t->to_parent;
4226 } else if (t->from == thread) {
4227 t->from = NULL;
4228 t = t->from_parent;
4229 } else
4230 BUG();
Todd Kjos7a4408c2017-06-29 12:01:57 -07004231 spin_unlock(&last_t->lock);
4232 if (t)
4233 spin_lock(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004234 }
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004235 binder_inner_proc_unlock(thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07004236
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004237 if (send_reply)
4238 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
Todd Kjos72196392017-06-29 12:02:02 -07004239 binder_release_work(proc, &thread->todo);
Todd Kjos7a4408c2017-06-29 12:01:57 -07004240 binder_thread_dec_tmpref(thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004241 return active_transactions;
4242}
4243
4244static unsigned int binder_poll(struct file *filp,
4245 struct poll_table_struct *wait)
4246{
4247 struct binder_proc *proc = filp->private_data;
4248 struct binder_thread *thread = NULL;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004249 bool wait_for_proc_work;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004250
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004251 thread = binder_get_thread(proc);
4252
Martijn Coenen0b89d692017-06-29 12:02:06 -07004253 binder_inner_proc_lock(thread->proc);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004254 thread->looper |= BINDER_LOOPER_STATE_POLL;
4255 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
4256
Martijn Coenen0b89d692017-06-29 12:02:06 -07004257 binder_inner_proc_unlock(thread->proc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004258
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004259 if (binder_has_work(thread, wait_for_proc_work))
4260 return POLLIN;
4261
4262 poll_wait(filp, &thread->wait, wait);
4263
4264 if (binder_has_thread_work(thread))
4265 return POLLIN;
4266
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004267 return 0;
4268}
4269
Tair Rzayev78260ac2014-06-03 22:27:21 +03004270static int binder_ioctl_write_read(struct file *filp,
4271 unsigned int cmd, unsigned long arg,
4272 struct binder_thread *thread)
4273{
4274 int ret = 0;
4275 struct binder_proc *proc = filp->private_data;
4276 unsigned int size = _IOC_SIZE(cmd);
4277 void __user *ubuf = (void __user *)arg;
4278 struct binder_write_read bwr;
4279
4280 if (size != sizeof(struct binder_write_read)) {
4281 ret = -EINVAL;
4282 goto out;
4283 }
4284 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
4285 ret = -EFAULT;
4286 goto out;
4287 }
4288 binder_debug(BINDER_DEBUG_READ_WRITE,
4289 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
4290 proc->pid, thread->pid,
4291 (u64)bwr.write_size, (u64)bwr.write_buffer,
4292 (u64)bwr.read_size, (u64)bwr.read_buffer);
4293
4294 if (bwr.write_size > 0) {
4295 ret = binder_thread_write(proc, thread,
4296 bwr.write_buffer,
4297 bwr.write_size,
4298 &bwr.write_consumed);
4299 trace_binder_write_done(ret);
4300 if (ret < 0) {
4301 bwr.read_consumed = 0;
4302 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4303 ret = -EFAULT;
4304 goto out;
4305 }
4306 }
4307 if (bwr.read_size > 0) {
4308 ret = binder_thread_read(proc, thread, bwr.read_buffer,
4309 bwr.read_size,
4310 &bwr.read_consumed,
4311 filp->f_flags & O_NONBLOCK);
4312 trace_binder_read_done(ret);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004313 binder_inner_proc_lock(proc);
4314 if (!binder_worklist_empty_ilocked(&proc->todo))
Martijn Coenen408c68b2017-08-31 10:04:19 +02004315 binder_wakeup_proc_ilocked(proc);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004316 binder_inner_proc_unlock(proc);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004317 if (ret < 0) {
4318 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4319 ret = -EFAULT;
4320 goto out;
4321 }
4322 }
4323 binder_debug(BINDER_DEBUG_READ_WRITE,
4324 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
4325 proc->pid, thread->pid,
4326 (u64)bwr.write_consumed, (u64)bwr.write_size,
4327 (u64)bwr.read_consumed, (u64)bwr.read_size);
4328 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
4329 ret = -EFAULT;
4330 goto out;
4331 }
4332out:
4333 return ret;
4334}
4335
4336static int binder_ioctl_set_ctx_mgr(struct file *filp)
4337{
4338 int ret = 0;
4339 struct binder_proc *proc = filp->private_data;
Martijn Coenen342e5c92017-02-03 14:40:46 -08004340 struct binder_context *context = proc->context;
Todd Kjosc44b1232017-06-29 12:01:43 -07004341 struct binder_node *new_node;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004342 kuid_t curr_euid = current_euid();
4343
Todd Kjosc44b1232017-06-29 12:01:43 -07004344 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen342e5c92017-02-03 14:40:46 -08004345 if (context->binder_context_mgr_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004346 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
4347 ret = -EBUSY;
4348 goto out;
4349 }
Stephen Smalley79af7302015-01-21 10:54:10 -05004350 ret = security_binder_set_context_mgr(proc->tsk);
4351 if (ret < 0)
4352 goto out;
Martijn Coenen342e5c92017-02-03 14:40:46 -08004353 if (uid_valid(context->binder_context_mgr_uid)) {
4354 if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004355 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
4356 from_kuid(&init_user_ns, curr_euid),
4357 from_kuid(&init_user_ns,
Martijn Coenen342e5c92017-02-03 14:40:46 -08004358 context->binder_context_mgr_uid));
Tair Rzayev78260ac2014-06-03 22:27:21 +03004359 ret = -EPERM;
4360 goto out;
4361 }
4362 } else {
Martijn Coenen342e5c92017-02-03 14:40:46 -08004363 context->binder_context_mgr_uid = curr_euid;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004364 }
Todd Kjos673068e2017-06-29 12:02:03 -07004365 new_node = binder_new_node(proc, NULL);
Todd Kjosc44b1232017-06-29 12:01:43 -07004366 if (!new_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004367 ret = -ENOMEM;
4368 goto out;
4369 }
Todd Kjos673068e2017-06-29 12:02:03 -07004370 binder_node_lock(new_node);
Todd Kjosc44b1232017-06-29 12:01:43 -07004371 new_node->local_weak_refs++;
4372 new_node->local_strong_refs++;
4373 new_node->has_strong_ref = 1;
4374 new_node->has_weak_ref = 1;
4375 context->binder_context_mgr_node = new_node;
Todd Kjos673068e2017-06-29 12:02:03 -07004376 binder_node_unlock(new_node);
Todd Kjosadc18842017-06-29 12:01:59 -07004377 binder_put_node(new_node);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004378out:
Todd Kjosc44b1232017-06-29 12:01:43 -07004379 mutex_unlock(&context->context_mgr_node_lock);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004380 return ret;
4381}
4382
Colin Crossabcc6152017-08-31 10:04:24 +02004383static int binder_ioctl_get_node_debug_info(struct binder_proc *proc,
4384 struct binder_node_debug_info *info)
4385{
4386 struct rb_node *n;
4387 binder_uintptr_t ptr = info->ptr;
4388
4389 memset(info, 0, sizeof(*info));
4390
4391 binder_inner_proc_lock(proc);
4392 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
4393 struct binder_node *node = rb_entry(n, struct binder_node,
4394 rb_node);
4395 if (node->ptr > ptr) {
4396 info->ptr = node->ptr;
4397 info->cookie = node->cookie;
4398 info->has_strong_ref = node->has_strong_ref;
4399 info->has_weak_ref = node->has_weak_ref;
4400 break;
4401 }
4402 }
4403 binder_inner_proc_unlock(proc);
4404
4405 return 0;
4406}
4407
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004408static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4409{
4410 int ret;
4411 struct binder_proc *proc = filp->private_data;
4412 struct binder_thread *thread;
4413 unsigned int size = _IOC_SIZE(cmd);
4414 void __user *ubuf = (void __user *)arg;
4415
Tair Rzayev78260ac2014-06-03 22:27:21 +03004416 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
4417 proc->pid, current->pid, cmd, arg);*/
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004418
Sherry Yang4175e2b2017-08-23 08:46:40 -07004419 binder_selftest_alloc(&proc->alloc);
4420
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004421 trace_binder_ioctl(cmd, arg);
4422
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004423 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4424 if (ret)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004425 goto err_unlocked;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004426
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004427 thread = binder_get_thread(proc);
4428 if (thread == NULL) {
4429 ret = -ENOMEM;
4430 goto err;
4431 }
4432
4433 switch (cmd) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004434 case BINDER_WRITE_READ:
4435 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
4436 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004437 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004438 break;
Todd Kjosb3e68612017-06-29 12:02:07 -07004439 case BINDER_SET_MAX_THREADS: {
4440 int max_threads;
4441
4442 if (copy_from_user(&max_threads, ubuf,
4443 sizeof(max_threads))) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004444 ret = -EINVAL;
4445 goto err;
4446 }
Todd Kjosb3e68612017-06-29 12:02:07 -07004447 binder_inner_proc_lock(proc);
4448 proc->max_threads = max_threads;
4449 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004450 break;
Todd Kjosb3e68612017-06-29 12:02:07 -07004451 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004452 case BINDER_SET_CONTEXT_MGR:
Tair Rzayev78260ac2014-06-03 22:27:21 +03004453 ret = binder_ioctl_set_ctx_mgr(filp);
4454 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004455 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004456 break;
4457 case BINDER_THREAD_EXIT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304458 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004459 proc->pid, thread->pid);
Todd Kjos7a4408c2017-06-29 12:01:57 -07004460 binder_thread_release(proc, thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004461 thread = NULL;
4462 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02004463 case BINDER_VERSION: {
4464 struct binder_version __user *ver = ubuf;
4465
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004466 if (size != sizeof(struct binder_version)) {
4467 ret = -EINVAL;
4468 goto err;
4469 }
Mathieu Maret36c89c02014-04-15 12:03:05 +02004470 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
4471 &ver->protocol_version)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004472 ret = -EINVAL;
4473 goto err;
4474 }
4475 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02004476 }
Colin Crossabcc6152017-08-31 10:04:24 +02004477 case BINDER_GET_NODE_DEBUG_INFO: {
4478 struct binder_node_debug_info info;
4479
4480 if (copy_from_user(&info, ubuf, sizeof(info))) {
4481 ret = -EFAULT;
4482 goto err;
4483 }
4484
4485 ret = binder_ioctl_get_node_debug_info(proc, &info);
4486 if (ret < 0)
4487 goto err;
4488
4489 if (copy_to_user(ubuf, &info, sizeof(info))) {
4490 ret = -EFAULT;
4491 goto err;
4492 }
4493 break;
4494 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004495 default:
4496 ret = -EINVAL;
4497 goto err;
4498 }
4499 ret = 0;
4500err:
4501 if (thread)
Todd Kjos08dabce2017-06-29 12:01:49 -07004502 thread->looper_need_return = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004503 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4504 if (ret && ret != -ERESTARTSYS)
Anmol Sarma56b468f2012-10-30 22:35:43 +05304505 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 -07004506err_unlocked:
4507 trace_binder_ioctl_done(ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004508 return ret;
4509}
4510
4511static void binder_vma_open(struct vm_area_struct *vma)
4512{
4513 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004514
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004515 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304516 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004517 proc->pid, vma->vm_start, vma->vm_end,
4518 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4519 (unsigned long)pgprot_val(vma->vm_page_prot));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004520}
4521
4522static void binder_vma_close(struct vm_area_struct *vma)
4523{
4524 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004525
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004526 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304527 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004528 proc->pid, vma->vm_start, vma->vm_end,
4529 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4530 (unsigned long)pgprot_val(vma->vm_page_prot));
Todd Kjos19c98722017-06-29 12:01:40 -07004531 binder_alloc_vma_close(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004532 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
4533}
4534
Dave Jiang11bac802017-02-24 14:56:41 -08004535static int binder_vm_fault(struct vm_fault *vmf)
Vinayak Menonddac7d52014-06-02 18:17:59 +05304536{
4537 return VM_FAULT_SIGBUS;
4538}
4539
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07004540static const struct vm_operations_struct binder_vm_ops = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004541 .open = binder_vma_open,
4542 .close = binder_vma_close,
Vinayak Menonddac7d52014-06-02 18:17:59 +05304543 .fault = binder_vm_fault,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004544};
4545
Todd Kjos19c98722017-06-29 12:01:40 -07004546static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
4547{
4548 int ret;
4549 struct binder_proc *proc = filp->private_data;
4550 const char *failure_string;
4551
4552 if (proc->tsk != current->group_leader)
4553 return -EINVAL;
4554
4555 if ((vma->vm_end - vma->vm_start) > SZ_4M)
4556 vma->vm_end = vma->vm_start + SZ_4M;
4557
4558 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4559 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
4560 __func__, proc->pid, vma->vm_start, vma->vm_end,
4561 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4562 (unsigned long)pgprot_val(vma->vm_page_prot));
4563
4564 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
4565 ret = -EPERM;
4566 failure_string = "bad vm_flags";
4567 goto err_bad_arg;
4568 }
4569 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
4570 vma->vm_ops = &binder_vm_ops;
4571 vma->vm_private_data = proc;
4572
4573 ret = binder_alloc_mmap_handler(&proc->alloc, vma);
4574 if (ret)
4575 return ret;
4576 proc->files = get_files_struct(current);
4577 return 0;
4578
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004579err_bad_arg:
Sherwin Soltani258767f2012-06-26 02:00:30 -04004580 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004581 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
4582 return ret;
4583}
4584
4585static int binder_open(struct inode *nodp, struct file *filp)
4586{
4587 struct binder_proc *proc;
Martijn Coenenac4812c2017-02-03 14:40:48 -08004588 struct binder_device *binder_dev;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004589
4590 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
4591 current->group_leader->pid, current->pid);
4592
4593 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
4594 if (proc == NULL)
4595 return -ENOMEM;
Todd Kjos9630fe82017-06-29 12:02:00 -07004596 spin_lock_init(&proc->inner_lock);
4597 spin_lock_init(&proc->outer_lock);
Todd Kjosc4ea41b2017-06-29 12:01:36 -07004598 get_task_struct(current->group_leader);
4599 proc->tsk = current->group_leader;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004600 INIT_LIST_HEAD(&proc->todo);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004601 proc->default_priority = task_nice(current);
Martijn Coenenac4812c2017-02-03 14:40:48 -08004602 binder_dev = container_of(filp->private_data, struct binder_device,
4603 miscdev);
4604 proc->context = &binder_dev->context;
Todd Kjos19c98722017-06-29 12:01:40 -07004605 binder_alloc_init(&proc->alloc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004606
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004607 binder_stats_created(BINDER_STAT_PROC);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004608 proc->pid = current->group_leader->pid;
4609 INIT_LIST_HEAD(&proc->delivered_death);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004610 INIT_LIST_HEAD(&proc->waiting_threads);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004611 filp->private_data = proc;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004612
Todd Kjosc44b1232017-06-29 12:01:43 -07004613 mutex_lock(&binder_procs_lock);
4614 hlist_add_head(&proc->proc_node, &binder_procs);
4615 mutex_unlock(&binder_procs_lock);
4616
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004617 if (binder_debugfs_dir_entry_proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004618 char strbuf[11];
Seunghun Lee10f62862014-05-01 01:30:23 +09004619
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004620 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
Martijn Coenen14db3182017-02-03 14:40:47 -08004621 /*
4622 * proc debug entries are shared between contexts, so
4623 * this will fail if the process tries to open the driver
4624 * again with a different context. The priting code will
4625 * anyway print all contexts that a given PID has, so this
4626 * is not a problem.
4627 */
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004628 proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
Martijn Coenen14db3182017-02-03 14:40:47 -08004629 binder_debugfs_dir_entry_proc,
4630 (void *)(unsigned long)proc->pid,
4631 &binder_proc_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004632 }
4633
4634 return 0;
4635}
4636
4637static int binder_flush(struct file *filp, fl_owner_t id)
4638{
4639 struct binder_proc *proc = filp->private_data;
4640
4641 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
4642
4643 return 0;
4644}
4645
4646static void binder_deferred_flush(struct binder_proc *proc)
4647{
4648 struct rb_node *n;
4649 int wake_count = 0;
Seunghun Lee10f62862014-05-01 01:30:23 +09004650
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004651 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004652 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
4653 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
Seunghun Lee10f62862014-05-01 01:30:23 +09004654
Todd Kjos08dabce2017-06-29 12:01:49 -07004655 thread->looper_need_return = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004656 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
4657 wake_up_interruptible(&thread->wait);
4658 wake_count++;
4659 }
4660 }
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004661 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004662
4663 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4664 "binder_flush: %d woke %d threads\n", proc->pid,
4665 wake_count);
4666}
4667
4668static int binder_release(struct inode *nodp, struct file *filp)
4669{
4670 struct binder_proc *proc = filp->private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004671
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004672 debugfs_remove(proc->debugfs_entry);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004673 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
4674
4675 return 0;
4676}
4677
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004678static int binder_node_release(struct binder_node *node, int refs)
4679{
4680 struct binder_ref *ref;
4681 int death = 0;
Todd Kjosed297212017-06-29 12:02:01 -07004682 struct binder_proc *proc = node->proc;
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004683
Todd Kjos72196392017-06-29 12:02:02 -07004684 binder_release_work(proc, &node->async_todo);
Todd Kjosed297212017-06-29 12:02:01 -07004685
Todd Kjos673068e2017-06-29 12:02:03 -07004686 binder_node_lock(node);
Todd Kjosed297212017-06-29 12:02:01 -07004687 binder_inner_proc_lock(proc);
Todd Kjos72196392017-06-29 12:02:02 -07004688 binder_dequeue_work_ilocked(&node->work);
Todd Kjosadc18842017-06-29 12:01:59 -07004689 /*
4690 * The caller must have taken a temporary ref on the node,
4691 */
4692 BUG_ON(!node->tmp_refs);
4693 if (hlist_empty(&node->refs) && node->tmp_refs == 1) {
Todd Kjosed297212017-06-29 12:02:01 -07004694 binder_inner_proc_unlock(proc);
Todd Kjos673068e2017-06-29 12:02:03 -07004695 binder_node_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07004696 binder_free_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004697
4698 return refs;
4699 }
4700
4701 node->proc = NULL;
4702 node->local_strong_refs = 0;
4703 node->local_weak_refs = 0;
Todd Kjosed297212017-06-29 12:02:01 -07004704 binder_inner_proc_unlock(proc);
Todd Kjosc44b1232017-06-29 12:01:43 -07004705
4706 spin_lock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004707 hlist_add_head(&node->dead_node, &binder_dead_nodes);
Todd Kjosc44b1232017-06-29 12:01:43 -07004708 spin_unlock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004709
4710 hlist_for_each_entry(ref, &node->refs, node_entry) {
4711 refs++;
Martijn Coenenab51ec62017-06-29 12:02:10 -07004712 /*
4713 * Need the node lock to synchronize
4714 * with new notification requests and the
4715 * inner lock to synchronize with queued
4716 * death notifications.
4717 */
4718 binder_inner_proc_lock(ref->proc);
4719 if (!ref->death) {
4720 binder_inner_proc_unlock(ref->proc);
Arve Hjønnevåge194fd82014-02-17 13:58:29 -08004721 continue;
Martijn Coenenab51ec62017-06-29 12:02:10 -07004722 }
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004723
4724 death++;
4725
Martijn Coenenab51ec62017-06-29 12:02:10 -07004726 BUG_ON(!list_empty(&ref->death->work.entry));
4727 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
4728 binder_enqueue_work_ilocked(&ref->death->work,
4729 &ref->proc->todo);
Martijn Coenen408c68b2017-08-31 10:04:19 +02004730 binder_wakeup_proc_ilocked(ref->proc);
Todd Kjos72196392017-06-29 12:02:02 -07004731 binder_inner_proc_unlock(ref->proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004732 }
4733
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004734 binder_debug(BINDER_DEBUG_DEAD_BINDER,
4735 "node %d now dead, refs %d, death %d\n",
4736 node->debug_id, refs, death);
Todd Kjos673068e2017-06-29 12:02:03 -07004737 binder_node_unlock(node);
Todd Kjosadc18842017-06-29 12:01:59 -07004738 binder_put_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004739
4740 return refs;
4741}
4742
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004743static void binder_deferred_release(struct binder_proc *proc)
4744{
Martijn Coenen342e5c92017-02-03 14:40:46 -08004745 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004746 struct rb_node *n;
Todd Kjos19c98722017-06-29 12:01:40 -07004747 int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004748
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004749 BUG_ON(proc->files);
4750
Todd Kjosc44b1232017-06-29 12:01:43 -07004751 mutex_lock(&binder_procs_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004752 hlist_del(&proc->proc_node);
Todd Kjosc44b1232017-06-29 12:01:43 -07004753 mutex_unlock(&binder_procs_lock);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004754
Todd Kjosc44b1232017-06-29 12:01:43 -07004755 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen342e5c92017-02-03 14:40:46 -08004756 if (context->binder_context_mgr_node &&
4757 context->binder_context_mgr_node->proc == proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004758 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01004759 "%s: %d context_mgr_node gone\n",
4760 __func__, proc->pid);
Martijn Coenen342e5c92017-02-03 14:40:46 -08004761 context->binder_context_mgr_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004762 }
Todd Kjosc44b1232017-06-29 12:01:43 -07004763 mutex_unlock(&context->context_mgr_node_lock);
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004764 binder_inner_proc_lock(proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07004765 /*
4766 * Make sure proc stays alive after we
4767 * remove all the threads
4768 */
4769 proc->tmp_ref++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004770
Todd Kjos7a4408c2017-06-29 12:01:57 -07004771 proc->is_dead = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004772 threads = 0;
4773 active_transactions = 0;
4774 while ((n = rb_first(&proc->threads))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004775 struct binder_thread *thread;
4776
4777 thread = rb_entry(n, struct binder_thread, rb_node);
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004778 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004779 threads++;
Todd Kjos7a4408c2017-06-29 12:01:57 -07004780 active_transactions += binder_thread_release(proc, thread);
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004781 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004782 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004783
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004784 nodes = 0;
4785 incoming_refs = 0;
4786 while ((n = rb_first(&proc->nodes))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004787 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004788
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004789 node = rb_entry(n, struct binder_node, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004790 nodes++;
Todd Kjosadc18842017-06-29 12:01:59 -07004791 /*
4792 * take a temporary ref on the node before
4793 * calling binder_node_release() which will either
4794 * kfree() the node or call binder_put_node()
4795 */
Todd Kjosda0fa9e2017-06-29 12:02:04 -07004796 binder_inc_node_tmpref_ilocked(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004797 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07004798 binder_inner_proc_unlock(proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004799 incoming_refs = binder_node_release(node, incoming_refs);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07004800 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004801 }
Todd Kjosda0fa9e2017-06-29 12:02:04 -07004802 binder_inner_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004803
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004804 outgoing_refs = 0;
Todd Kjos2c1838d2017-06-29 12:02:08 -07004805 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004806 while ((n = rb_first(&proc->refs_by_desc))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004807 struct binder_ref *ref;
4808
4809 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004810 outgoing_refs++;
Todd Kjos2c1838d2017-06-29 12:02:08 -07004811 binder_cleanup_ref_olocked(ref);
4812 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07004813 binder_free_ref(ref);
Todd Kjos2c1838d2017-06-29 12:02:08 -07004814 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004815 }
Todd Kjos2c1838d2017-06-29 12:02:08 -07004816 binder_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004817
Todd Kjos72196392017-06-29 12:02:02 -07004818 binder_release_work(proc, &proc->todo);
4819 binder_release_work(proc, &proc->delivered_death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004820
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004821 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Todd Kjos19c98722017-06-29 12:01:40 -07004822 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01004823 __func__, proc->pid, threads, nodes, incoming_refs,
Todd Kjos19c98722017-06-29 12:01:40 -07004824 outgoing_refs, active_transactions);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004825
Todd Kjos7a4408c2017-06-29 12:01:57 -07004826 binder_proc_dec_tmpref(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004827}
4828
4829static void binder_deferred_func(struct work_struct *work)
4830{
4831 struct binder_proc *proc;
4832 struct files_struct *files;
4833
4834 int defer;
Seunghun Lee10f62862014-05-01 01:30:23 +09004835
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004836 do {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004837 mutex_lock(&binder_deferred_lock);
4838 if (!hlist_empty(&binder_deferred_list)) {
4839 proc = hlist_entry(binder_deferred_list.first,
4840 struct binder_proc, deferred_work_node);
4841 hlist_del_init(&proc->deferred_work_node);
4842 defer = proc->deferred_work;
4843 proc->deferred_work = 0;
4844 } else {
4845 proc = NULL;
4846 defer = 0;
4847 }
4848 mutex_unlock(&binder_deferred_lock);
4849
4850 files = NULL;
4851 if (defer & BINDER_DEFERRED_PUT_FILES) {
4852 files = proc->files;
4853 if (files)
4854 proc->files = NULL;
4855 }
4856
4857 if (defer & BINDER_DEFERRED_FLUSH)
4858 binder_deferred_flush(proc);
4859
4860 if (defer & BINDER_DEFERRED_RELEASE)
4861 binder_deferred_release(proc); /* frees proc */
4862
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004863 if (files)
4864 put_files_struct(files);
4865 } while (proc);
4866}
4867static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
4868
4869static void
4870binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
4871{
4872 mutex_lock(&binder_deferred_lock);
4873 proc->deferred_work |= defer;
4874 if (hlist_unhashed(&proc->deferred_work_node)) {
4875 hlist_add_head(&proc->deferred_work_node,
4876 &binder_deferred_list);
Bhaktipriya Shridhar1beba522016-08-13 22:16:24 +05304877 schedule_work(&binder_deferred_work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004878 }
4879 mutex_unlock(&binder_deferred_lock);
4880}
4881
Todd Kjos5f2f6362017-06-29 12:02:09 -07004882static void print_binder_transaction_ilocked(struct seq_file *m,
4883 struct binder_proc *proc,
4884 const char *prefix,
4885 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004886{
Todd Kjos5f2f6362017-06-29 12:02:09 -07004887 struct binder_proc *to_proc;
4888 struct binder_buffer *buffer = t->buffer;
4889
Todd Kjos7a4408c2017-06-29 12:01:57 -07004890 spin_lock(&t->lock);
Todd Kjos5f2f6362017-06-29 12:02:09 -07004891 to_proc = t->to_proc;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004892 seq_printf(m,
4893 "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %ld r%d",
4894 prefix, t->debug_id, t,
4895 t->from ? t->from->proc->pid : 0,
4896 t->from ? t->from->pid : 0,
Todd Kjos5f2f6362017-06-29 12:02:09 -07004897 to_proc ? to_proc->pid : 0,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004898 t->to_thread ? t->to_thread->pid : 0,
4899 t->code, t->flags, t->priority, t->need_reply);
Todd Kjos7a4408c2017-06-29 12:01:57 -07004900 spin_unlock(&t->lock);
4901
Todd Kjos5f2f6362017-06-29 12:02:09 -07004902 if (proc != to_proc) {
4903 /*
4904 * Can only safely deref buffer if we are holding the
4905 * correct proc inner lock for this node
4906 */
4907 seq_puts(m, "\n");
4908 return;
4909 }
4910
4911 if (buffer == NULL) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004912 seq_puts(m, " buffer free\n");
4913 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004914 }
Todd Kjos5f2f6362017-06-29 12:02:09 -07004915 if (buffer->target_node)
4916 seq_printf(m, " node %d", buffer->target_node->debug_id);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004917 seq_printf(m, " size %zd:%zd data %p\n",
Todd Kjos5f2f6362017-06-29 12:02:09 -07004918 buffer->data_size, buffer->offsets_size,
4919 buffer->data);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004920}
4921
Todd Kjos5f2f6362017-06-29 12:02:09 -07004922static void print_binder_work_ilocked(struct seq_file *m,
4923 struct binder_proc *proc,
4924 const char *prefix,
4925 const char *transaction_prefix,
4926 struct binder_work *w)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004927{
4928 struct binder_node *node;
4929 struct binder_transaction *t;
4930
4931 switch (w->type) {
4932 case BINDER_WORK_TRANSACTION:
4933 t = container_of(w, struct binder_transaction, work);
Todd Kjos5f2f6362017-06-29 12:02:09 -07004934 print_binder_transaction_ilocked(
4935 m, proc, transaction_prefix, t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004936 break;
Todd Kjos26549d12017-06-29 12:01:55 -07004937 case BINDER_WORK_RETURN_ERROR: {
4938 struct binder_error *e = container_of(
4939 w, struct binder_error, work);
4940
4941 seq_printf(m, "%stransaction error: %u\n",
4942 prefix, e->cmd);
4943 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004944 case BINDER_WORK_TRANSACTION_COMPLETE:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004945 seq_printf(m, "%stransaction complete\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004946 break;
4947 case BINDER_WORK_NODE:
4948 node = container_of(w, struct binder_node, work);
Arve Hjønnevågda498892014-02-21 14:40:26 -08004949 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
4950 prefix, node->debug_id,
4951 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004952 break;
4953 case BINDER_WORK_DEAD_BINDER:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004954 seq_printf(m, "%shas dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004955 break;
4956 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004957 seq_printf(m, "%shas cleared dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004958 break;
4959 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004960 seq_printf(m, "%shas cleared death notification\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004961 break;
4962 default:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004963 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004964 break;
4965 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004966}
4967
Todd Kjos72196392017-06-29 12:02:02 -07004968static void print_binder_thread_ilocked(struct seq_file *m,
4969 struct binder_thread *thread,
4970 int print_always)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004971{
4972 struct binder_transaction *t;
4973 struct binder_work *w;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004974 size_t start_pos = m->count;
4975 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004976
Todd Kjos7a4408c2017-06-29 12:01:57 -07004977 seq_printf(m, " thread %d: l %02x need_return %d tr %d\n",
Todd Kjos08dabce2017-06-29 12:01:49 -07004978 thread->pid, thread->looper,
Todd Kjos7a4408c2017-06-29 12:01:57 -07004979 thread->looper_need_return,
4980 atomic_read(&thread->tmp_ref));
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004981 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004982 t = thread->transaction_stack;
4983 while (t) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004984 if (t->from == thread) {
Todd Kjos5f2f6362017-06-29 12:02:09 -07004985 print_binder_transaction_ilocked(m, thread->proc,
4986 " outgoing transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004987 t = t->from_parent;
4988 } else if (t->to_thread == thread) {
Todd Kjos5f2f6362017-06-29 12:02:09 -07004989 print_binder_transaction_ilocked(m, thread->proc,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004990 " incoming transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004991 t = t->to_parent;
4992 } else {
Todd Kjos5f2f6362017-06-29 12:02:09 -07004993 print_binder_transaction_ilocked(m, thread->proc,
4994 " bad transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004995 t = NULL;
4996 }
4997 }
4998 list_for_each_entry(w, &thread->todo, entry) {
Todd Kjos5f2f6362017-06-29 12:02:09 -07004999 print_binder_work_ilocked(m, thread->proc, " ",
Todd Kjos72196392017-06-29 12:02:02 -07005000 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005001 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005002 if (!print_always && m->count == header_pos)
5003 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005004}
5005
Todd Kjosda0fa9e2017-06-29 12:02:04 -07005006static void print_binder_node_nilocked(struct seq_file *m,
5007 struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005008{
5009 struct binder_ref *ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005010 struct binder_work *w;
5011 int count;
5012
5013 count = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08005014 hlist_for_each_entry(ref, &node->refs, node_entry)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005015 count++;
5016
Todd Kjosadc18842017-06-29 12:01:59 -07005017 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 -08005018 node->debug_id, (u64)node->ptr, (u64)node->cookie,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005019 node->has_strong_ref, node->has_weak_ref,
5020 node->local_strong_refs, node->local_weak_refs,
Todd Kjosadc18842017-06-29 12:01:59 -07005021 node->internal_strong_refs, count, node->tmp_refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005022 if (count) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005023 seq_puts(m, " proc");
Sasha Levinb67bfe02013-02-27 17:06:00 -08005024 hlist_for_each_entry(ref, &node->refs, node_entry)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005025 seq_printf(m, " %d", ref->proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005026 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005027 seq_puts(m, "\n");
Todd Kjos72196392017-06-29 12:02:02 -07005028 if (node->proc) {
Todd Kjos72196392017-06-29 12:02:02 -07005029 list_for_each_entry(w, &node->async_todo, entry)
Todd Kjos5f2f6362017-06-29 12:02:09 -07005030 print_binder_work_ilocked(m, node->proc, " ",
Todd Kjos72196392017-06-29 12:02:02 -07005031 " pending async transaction", w);
Todd Kjos72196392017-06-29 12:02:02 -07005032 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005033}
5034
Todd Kjos2c1838d2017-06-29 12:02:08 -07005035static void print_binder_ref_olocked(struct seq_file *m,
5036 struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005037{
Todd Kjos673068e2017-06-29 12:02:03 -07005038 binder_node_lock(ref->node);
Todd Kjos372e3142017-06-29 12:01:58 -07005039 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
5040 ref->data.debug_id, ref->data.desc,
5041 ref->node->proc ? "" : "dead ",
5042 ref->node->debug_id, ref->data.strong,
5043 ref->data.weak, ref->death);
Todd Kjos673068e2017-06-29 12:02:03 -07005044 binder_node_unlock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005045}
5046
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005047static void print_binder_proc(struct seq_file *m,
5048 struct binder_proc *proc, int print_all)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005049{
5050 struct binder_work *w;
5051 struct rb_node *n;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005052 size_t start_pos = m->count;
5053 size_t header_pos;
Todd Kjosda0fa9e2017-06-29 12:02:04 -07005054 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005055
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005056 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen14db3182017-02-03 14:40:47 -08005057 seq_printf(m, "context %s\n", proc->context->name);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005058 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005059
Todd Kjos72196392017-06-29 12:02:02 -07005060 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005061 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
Todd Kjos72196392017-06-29 12:02:02 -07005062 print_binder_thread_ilocked(m, rb_entry(n, struct binder_thread,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005063 rb_node), print_all);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07005064
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005065 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005066 struct binder_node *node = rb_entry(n, struct binder_node,
5067 rb_node);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07005068 /*
5069 * take a temporary reference on the node so it
5070 * survives and isn't removed from the tree
5071 * while we print it.
5072 */
5073 binder_inc_node_tmpref_ilocked(node);
5074 /* Need to drop inner lock to take node lock */
5075 binder_inner_proc_unlock(proc);
5076 if (last_node)
5077 binder_put_node(last_node);
5078 binder_node_inner_lock(node);
5079 print_binder_node_nilocked(m, node);
5080 binder_node_inner_unlock(node);
5081 last_node = node;
5082 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005083 }
Todd Kjosda0fa9e2017-06-29 12:02:04 -07005084 binder_inner_proc_unlock(proc);
5085 if (last_node)
5086 binder_put_node(last_node);
5087
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005088 if (print_all) {
Todd Kjos2c1838d2017-06-29 12:02:08 -07005089 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005090 for (n = rb_first(&proc->refs_by_desc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005091 n != NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005092 n = rb_next(n))
Todd Kjos2c1838d2017-06-29 12:02:08 -07005093 print_binder_ref_olocked(m, rb_entry(n,
5094 struct binder_ref,
5095 rb_node_desc));
5096 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005097 }
Todd Kjos19c98722017-06-29 12:01:40 -07005098 binder_alloc_print_allocated(m, &proc->alloc);
Todd Kjos72196392017-06-29 12:02:02 -07005099 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005100 list_for_each_entry(w, &proc->todo, entry)
Todd Kjos5f2f6362017-06-29 12:02:09 -07005101 print_binder_work_ilocked(m, proc, " ",
5102 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005103 list_for_each_entry(w, &proc->delivered_death, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005104 seq_puts(m, " has delivered dead binder\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005105 break;
5106 }
Todd Kjos72196392017-06-29 12:02:02 -07005107 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005108 if (!print_all && m->count == header_pos)
5109 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005110}
5111
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005112static const char * const binder_return_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005113 "BR_ERROR",
5114 "BR_OK",
5115 "BR_TRANSACTION",
5116 "BR_REPLY",
5117 "BR_ACQUIRE_RESULT",
5118 "BR_DEAD_REPLY",
5119 "BR_TRANSACTION_COMPLETE",
5120 "BR_INCREFS",
5121 "BR_ACQUIRE",
5122 "BR_RELEASE",
5123 "BR_DECREFS",
5124 "BR_ATTEMPT_ACQUIRE",
5125 "BR_NOOP",
5126 "BR_SPAWN_LOOPER",
5127 "BR_FINISHED",
5128 "BR_DEAD_BINDER",
5129 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
5130 "BR_FAILED_REPLY"
5131};
5132
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005133static const char * const binder_command_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005134 "BC_TRANSACTION",
5135 "BC_REPLY",
5136 "BC_ACQUIRE_RESULT",
5137 "BC_FREE_BUFFER",
5138 "BC_INCREFS",
5139 "BC_ACQUIRE",
5140 "BC_RELEASE",
5141 "BC_DECREFS",
5142 "BC_INCREFS_DONE",
5143 "BC_ACQUIRE_DONE",
5144 "BC_ATTEMPT_ACQUIRE",
5145 "BC_REGISTER_LOOPER",
5146 "BC_ENTER_LOOPER",
5147 "BC_EXIT_LOOPER",
5148 "BC_REQUEST_DEATH_NOTIFICATION",
5149 "BC_CLEAR_DEATH_NOTIFICATION",
Martijn Coenen79802402017-02-03 14:40:51 -08005150 "BC_DEAD_BINDER_DONE",
5151 "BC_TRANSACTION_SG",
5152 "BC_REPLY_SG",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005153};
5154
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005155static const char * const binder_objstat_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005156 "proc",
5157 "thread",
5158 "node",
5159 "ref",
5160 "death",
5161 "transaction",
5162 "transaction_complete"
5163};
5164
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005165static void print_binder_stats(struct seq_file *m, const char *prefix,
5166 struct binder_stats *stats)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005167{
5168 int i;
5169
5170 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005171 ARRAY_SIZE(binder_command_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005172 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07005173 int temp = atomic_read(&stats->bc[i]);
5174
5175 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005176 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07005177 binder_command_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005178 }
5179
5180 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005181 ARRAY_SIZE(binder_return_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005182 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07005183 int temp = atomic_read(&stats->br[i]);
5184
5185 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005186 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07005187 binder_return_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005188 }
5189
5190 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005191 ARRAY_SIZE(binder_objstat_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005192 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005193 ARRAY_SIZE(stats->obj_deleted));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005194 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07005195 int created = atomic_read(&stats->obj_created[i]);
5196 int deleted = atomic_read(&stats->obj_deleted[i]);
5197
5198 if (created || deleted)
5199 seq_printf(m, "%s%s: active %d total %d\n",
5200 prefix,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005201 binder_objstat_strings[i],
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07005202 created - deleted,
5203 created);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005204 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005205}
5206
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005207static void print_binder_proc_stats(struct seq_file *m,
5208 struct binder_proc *proc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005209{
5210 struct binder_work *w;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02005211 struct binder_thread *thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005212 struct rb_node *n;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02005213 int count, strong, weak, ready_threads;
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07005214 size_t free_async_space =
5215 binder_alloc_get_free_async_space(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005216
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005217 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen14db3182017-02-03 14:40:47 -08005218 seq_printf(m, "context %s\n", proc->context->name);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005219 count = 0;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02005220 ready_threads = 0;
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07005221 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005222 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
5223 count++;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02005224
5225 list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node)
5226 ready_threads++;
5227
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005228 seq_printf(m, " threads: %d\n", count);
5229 seq_printf(m, " requested threads: %d+%d/%d\n"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005230 " ready threads %d\n"
5231 " free async space %zd\n", proc->requested_threads,
5232 proc->requested_threads_started, proc->max_threads,
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02005233 ready_threads,
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07005234 free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005235 count = 0;
5236 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
5237 count++;
Todd Kjosda0fa9e2017-06-29 12:02:04 -07005238 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005239 seq_printf(m, " nodes: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005240 count = 0;
5241 strong = 0;
5242 weak = 0;
Todd Kjos2c1838d2017-06-29 12:02:08 -07005243 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005244 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
5245 struct binder_ref *ref = rb_entry(n, struct binder_ref,
5246 rb_node_desc);
5247 count++;
Todd Kjos372e3142017-06-29 12:01:58 -07005248 strong += ref->data.strong;
5249 weak += ref->data.weak;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005250 }
Todd Kjos2c1838d2017-06-29 12:02:08 -07005251 binder_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005252 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005253
Todd Kjos19c98722017-06-29 12:01:40 -07005254 count = binder_alloc_get_allocated_count(&proc->alloc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005255 seq_printf(m, " buffers: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005256
Sherry Yang8ef46652017-08-31 11:56:36 -07005257 binder_alloc_print_pages(m, &proc->alloc);
5258
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005259 count = 0;
Todd Kjos72196392017-06-29 12:02:02 -07005260 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005261 list_for_each_entry(w, &proc->todo, entry) {
Todd Kjos72196392017-06-29 12:02:02 -07005262 if (w->type == BINDER_WORK_TRANSACTION)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005263 count++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005264 }
Todd Kjos72196392017-06-29 12:02:02 -07005265 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005266 seq_printf(m, " pending transactions: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005267
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005268 print_binder_stats(m, " ", &proc->stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005269}
5270
5271
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005272static int binder_state_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005273{
5274 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005275 struct binder_node *node;
Todd Kjos673068e2017-06-29 12:02:03 -07005276 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005277
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005278 seq_puts(m, "binder state:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005279
Todd Kjosc44b1232017-06-29 12:01:43 -07005280 spin_lock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005281 if (!hlist_empty(&binder_dead_nodes))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005282 seq_puts(m, "dead nodes:\n");
Todd Kjos673068e2017-06-29 12:02:03 -07005283 hlist_for_each_entry(node, &binder_dead_nodes, dead_node) {
5284 /*
5285 * take a temporary reference on the node so it
5286 * survives and isn't removed from the list
5287 * while we print it.
5288 */
5289 node->tmp_refs++;
5290 spin_unlock(&binder_dead_nodes_lock);
5291 if (last_node)
5292 binder_put_node(last_node);
5293 binder_node_lock(node);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07005294 print_binder_node_nilocked(m, node);
Todd Kjos673068e2017-06-29 12:02:03 -07005295 binder_node_unlock(node);
5296 last_node = node;
5297 spin_lock(&binder_dead_nodes_lock);
5298 }
Todd Kjosc44b1232017-06-29 12:01:43 -07005299 spin_unlock(&binder_dead_nodes_lock);
Todd Kjos673068e2017-06-29 12:02:03 -07005300 if (last_node)
5301 binder_put_node(last_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005302
Todd Kjosc44b1232017-06-29 12:01:43 -07005303 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005304 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005305 print_binder_proc(m, proc, 1);
Todd Kjosc44b1232017-06-29 12:01:43 -07005306 mutex_unlock(&binder_procs_lock);
Todd Kjosa60b8902017-06-29 12:02:11 -07005307
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005308 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005309}
5310
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005311static int binder_stats_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005312{
5313 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005314
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005315 seq_puts(m, "binder stats:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005316
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005317 print_binder_stats(m, "", &binder_stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005318
Todd Kjosc44b1232017-06-29 12:01:43 -07005319 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005320 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005321 print_binder_proc_stats(m, proc);
Todd Kjosc44b1232017-06-29 12:01:43 -07005322 mutex_unlock(&binder_procs_lock);
Todd Kjosa60b8902017-06-29 12:02:11 -07005323
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005324 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005325}
5326
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005327static int binder_transactions_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005328{
5329 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005330
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005331 seq_puts(m, "binder transactions:\n");
Todd Kjosc44b1232017-06-29 12:01:43 -07005332 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005333 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005334 print_binder_proc(m, proc, 0);
Todd Kjosc44b1232017-06-29 12:01:43 -07005335 mutex_unlock(&binder_procs_lock);
Todd Kjosa60b8902017-06-29 12:02:11 -07005336
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005337 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005338}
5339
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005340static int binder_proc_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005341{
Riley Andrews83050a42016-02-09 21:05:33 -08005342 struct binder_proc *itr;
Martijn Coenen14db3182017-02-03 14:40:47 -08005343 int pid = (unsigned long)m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005344
Todd Kjosc44b1232017-06-29 12:01:43 -07005345 mutex_lock(&binder_procs_lock);
Riley Andrews83050a42016-02-09 21:05:33 -08005346 hlist_for_each_entry(itr, &binder_procs, proc_node) {
Martijn Coenen14db3182017-02-03 14:40:47 -08005347 if (itr->pid == pid) {
5348 seq_puts(m, "binder proc state:\n");
5349 print_binder_proc(m, itr, 1);
Riley Andrews83050a42016-02-09 21:05:33 -08005350 }
5351 }
Todd Kjosc44b1232017-06-29 12:01:43 -07005352 mutex_unlock(&binder_procs_lock);
5353
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005354 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005355}
5356
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005357static void print_binder_transaction_log_entry(struct seq_file *m,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005358 struct binder_transaction_log_entry *e)
5359{
Todd Kjosd99c7332017-06-29 12:01:53 -07005360 int debug_id = READ_ONCE(e->debug_id_done);
5361 /*
5362 * read barrier to guarantee debug_id_done read before
5363 * we print the log values
5364 */
5365 smp_rmb();
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005366 seq_printf(m,
Todd Kjosd99c7332017-06-29 12:01:53 -07005367 "%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 -07005368 e->debug_id, (e->call_type == 2) ? "reply" :
5369 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
Martijn Coenen14db3182017-02-03 14:40:47 -08005370 e->from_thread, e->to_proc, e->to_thread, e->context_name,
Todd Kjos57ada2f2017-06-29 12:01:46 -07005371 e->to_node, e->target_handle, e->data_size, e->offsets_size,
5372 e->return_error, e->return_error_param,
5373 e->return_error_line);
Todd Kjosd99c7332017-06-29 12:01:53 -07005374 /*
5375 * read-barrier to guarantee read of debug_id_done after
5376 * done printing the fields of the entry
5377 */
5378 smp_rmb();
5379 seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ?
5380 "\n" : " (incomplete)\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005381}
5382
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005383static int binder_transaction_log_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005384{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005385 struct binder_transaction_log *log = m->private;
Todd Kjosd99c7332017-06-29 12:01:53 -07005386 unsigned int log_cur = atomic_read(&log->cur);
5387 unsigned int count;
5388 unsigned int cur;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005389 int i;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005390
Todd Kjosd99c7332017-06-29 12:01:53 -07005391 count = log_cur + 1;
5392 cur = count < ARRAY_SIZE(log->entry) && !log->full ?
5393 0 : count % ARRAY_SIZE(log->entry);
5394 if (count > ARRAY_SIZE(log->entry) || log->full)
5395 count = ARRAY_SIZE(log->entry);
5396 for (i = 0; i < count; i++) {
5397 unsigned int index = cur++ % ARRAY_SIZE(log->entry);
5398
5399 print_binder_transaction_log_entry(m, &log->entry[index]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005400 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005401 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005402}
5403
5404static const struct file_operations binder_fops = {
5405 .owner = THIS_MODULE,
5406 .poll = binder_poll,
5407 .unlocked_ioctl = binder_ioctl,
Arve Hjønnevågda498892014-02-21 14:40:26 -08005408 .compat_ioctl = binder_ioctl,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005409 .mmap = binder_mmap,
5410 .open = binder_open,
5411 .flush = binder_flush,
5412 .release = binder_release,
5413};
5414
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005415BINDER_DEBUG_ENTRY(state);
5416BINDER_DEBUG_ENTRY(stats);
5417BINDER_DEBUG_ENTRY(transactions);
5418BINDER_DEBUG_ENTRY(transaction_log);
5419
Martijn Coenenac4812c2017-02-03 14:40:48 -08005420static int __init init_binder_device(const char *name)
5421{
5422 int ret;
5423 struct binder_device *binder_device;
5424
5425 binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
5426 if (!binder_device)
5427 return -ENOMEM;
5428
5429 binder_device->miscdev.fops = &binder_fops;
5430 binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
5431 binder_device->miscdev.name = name;
5432
5433 binder_device->context.binder_context_mgr_uid = INVALID_UID;
5434 binder_device->context.name = name;
Todd Kjosc44b1232017-06-29 12:01:43 -07005435 mutex_init(&binder_device->context.context_mgr_node_lock);
Martijn Coenenac4812c2017-02-03 14:40:48 -08005436
5437 ret = misc_register(&binder_device->miscdev);
5438 if (ret < 0) {
5439 kfree(binder_device);
5440 return ret;
5441 }
5442
5443 hlist_add_head(&binder_device->hlist, &binder_devices);
5444
5445 return ret;
5446}
5447
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005448static int __init binder_init(void)
5449{
5450 int ret;
Christian Brauner22eb9472017-08-21 16:13:28 +02005451 char *device_name, *device_names, *device_tmp;
Martijn Coenenac4812c2017-02-03 14:40:48 -08005452 struct binder_device *device;
5453 struct hlist_node *tmp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005454
Sherry Yangf2517eb2017-08-23 08:46:42 -07005455 binder_alloc_shrinker_init();
5456
Todd Kjosd99c7332017-06-29 12:01:53 -07005457 atomic_set(&binder_transaction_log.cur, ~0U);
5458 atomic_set(&binder_transaction_log_failed.cur, ~0U);
5459
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005460 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
5461 if (binder_debugfs_dir_entry_root)
5462 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
5463 binder_debugfs_dir_entry_root);
Martijn Coenenac4812c2017-02-03 14:40:48 -08005464
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005465 if (binder_debugfs_dir_entry_root) {
5466 debugfs_create_file("state",
5467 S_IRUGO,
5468 binder_debugfs_dir_entry_root,
5469 NULL,
5470 &binder_state_fops);
5471 debugfs_create_file("stats",
5472 S_IRUGO,
5473 binder_debugfs_dir_entry_root,
5474 NULL,
5475 &binder_stats_fops);
5476 debugfs_create_file("transactions",
5477 S_IRUGO,
5478 binder_debugfs_dir_entry_root,
5479 NULL,
5480 &binder_transactions_fops);
5481 debugfs_create_file("transaction_log",
5482 S_IRUGO,
5483 binder_debugfs_dir_entry_root,
5484 &binder_transaction_log,
5485 &binder_transaction_log_fops);
5486 debugfs_create_file("failed_transaction_log",
5487 S_IRUGO,
5488 binder_debugfs_dir_entry_root,
5489 &binder_transaction_log_failed,
5490 &binder_transaction_log_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005491 }
Martijn Coenenac4812c2017-02-03 14:40:48 -08005492
5493 /*
5494 * Copy the module_parameter string, because we don't want to
5495 * tokenize it in-place.
5496 */
5497 device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL);
5498 if (!device_names) {
5499 ret = -ENOMEM;
5500 goto err_alloc_device_names_failed;
5501 }
5502 strcpy(device_names, binder_devices_param);
5503
Christian Brauner22eb9472017-08-21 16:13:28 +02005504 device_tmp = device_names;
5505 while ((device_name = strsep(&device_tmp, ","))) {
Martijn Coenenac4812c2017-02-03 14:40:48 -08005506 ret = init_binder_device(device_name);
5507 if (ret)
5508 goto err_init_binder_device_failed;
5509 }
5510
5511 return ret;
5512
5513err_init_binder_device_failed:
5514 hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
5515 misc_deregister(&device->miscdev);
5516 hlist_del(&device->hlist);
5517 kfree(device);
5518 }
Christian Brauner22eb9472017-08-21 16:13:28 +02005519
5520 kfree(device_names);
5521
Martijn Coenenac4812c2017-02-03 14:40:48 -08005522err_alloc_device_names_failed:
5523 debugfs_remove_recursive(binder_debugfs_dir_entry_root);
5524
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005525 return ret;
5526}
5527
5528device_initcall(binder_init);
5529
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07005530#define CREATE_TRACE_POINTS
5531#include "binder_trace.h"
5532
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005533MODULE_LICENSE("GPL v2");