blob: 55a44c0b3b20e44de1446b299df5ab92a8f94575 [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
973static void binder_wakeup_proc_ilocked(struct binder_proc *proc, bool sync)
974{
975 struct binder_thread *thread;
976
977 BUG_ON(!spin_is_locked(&proc->inner_lock));
978 thread = list_first_entry_or_null(&proc->waiting_threads,
979 struct binder_thread,
980 waiting_thread_node);
981
982 if (thread) {
983 list_del_init(&thread->waiting_thread_node);
984 if (sync)
985 wake_up_interruptible_sync(&thread->wait);
986 else
987 wake_up_interruptible(&thread->wait);
988 return;
989 }
990
991 /* Didn't find a thread waiting for proc work; this can happen
992 * in two scenarios:
993 * 1. All threads are busy handling transactions
994 * In that case, one of those threads should call back into
995 * the kernel driver soon and pick up this work.
996 * 2. Threads are using the (e)poll interface, in which case
997 * they may be blocked on the waitqueue without having been
998 * added to waiting_threads. For this case, we just iterate
999 * over all threads not handling transaction work, and
1000 * wake them all up. We wake all because we don't know whether
1001 * a thread that called into (e)poll is handling non-binder
1002 * work currently.
1003 */
1004 binder_wakeup_poll_threads_ilocked(proc, sync);
1005}
1006
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001007static void binder_set_nice(long nice)
1008{
1009 long min_nice;
Seunghun Lee10f62862014-05-01 01:30:23 +09001010
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001011 if (can_nice(current, nice)) {
1012 set_user_nice(current, nice);
1013 return;
1014 }
Krzysztof Opasiakc3643b62017-07-05 19:24:44 +02001015 min_nice = rlimit_to_nice(rlimit(RLIMIT_NICE));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001016 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301017 "%d: nice value %ld not allowed use %ld instead\n",
1018 current->pid, nice, min_nice);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001019 set_user_nice(current, min_nice);
Dongsheng Yang8698a742014-03-11 18:09:12 +08001020 if (min_nice <= MAX_NICE)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001021 return;
Anmol Sarma56b468f2012-10-30 22:35:43 +05301022 binder_user_error("%d RLIMIT_NICE not set\n", current->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001023}
1024
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001025static struct binder_node *binder_get_node_ilocked(struct binder_proc *proc,
1026 binder_uintptr_t ptr)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001027{
1028 struct rb_node *n = proc->nodes.rb_node;
1029 struct binder_node *node;
1030
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001031 BUG_ON(!spin_is_locked(&proc->inner_lock));
1032
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001033 while (n) {
1034 node = rb_entry(n, struct binder_node, rb_node);
1035
1036 if (ptr < node->ptr)
1037 n = n->rb_left;
1038 else if (ptr > node->ptr)
1039 n = n->rb_right;
Todd Kjosadc18842017-06-29 12:01:59 -07001040 else {
1041 /*
1042 * take an implicit weak reference
1043 * to ensure node stays alive until
1044 * call to binder_put_node()
1045 */
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001046 binder_inc_node_tmpref_ilocked(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001047 return node;
Todd Kjosadc18842017-06-29 12:01:59 -07001048 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001049 }
1050 return NULL;
1051}
1052
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001053static struct binder_node *binder_get_node(struct binder_proc *proc,
1054 binder_uintptr_t ptr)
1055{
1056 struct binder_node *node;
1057
1058 binder_inner_proc_lock(proc);
1059 node = binder_get_node_ilocked(proc, ptr);
1060 binder_inner_proc_unlock(proc);
1061 return node;
1062}
1063
1064static struct binder_node *binder_init_node_ilocked(
1065 struct binder_proc *proc,
1066 struct binder_node *new_node,
1067 struct flat_binder_object *fp)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001068{
1069 struct rb_node **p = &proc->nodes.rb_node;
1070 struct rb_node *parent = NULL;
1071 struct binder_node *node;
Todd Kjos673068e2017-06-29 12:02:03 -07001072 binder_uintptr_t ptr = fp ? fp->binder : 0;
1073 binder_uintptr_t cookie = fp ? fp->cookie : 0;
1074 __u32 flags = fp ? fp->flags : 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001075
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001076 BUG_ON(!spin_is_locked(&proc->inner_lock));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001077 while (*p) {
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001078
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001079 parent = *p;
1080 node = rb_entry(parent, struct binder_node, rb_node);
1081
1082 if (ptr < node->ptr)
1083 p = &(*p)->rb_left;
1084 else if (ptr > node->ptr)
1085 p = &(*p)->rb_right;
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001086 else {
1087 /*
1088 * A matching node is already in
1089 * the rb tree. Abandon the init
1090 * and return it.
1091 */
1092 binder_inc_node_tmpref_ilocked(node);
1093 return node;
1094 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001095 }
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001096 node = new_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001097 binder_stats_created(BINDER_STAT_NODE);
Todd Kjosadc18842017-06-29 12:01:59 -07001098 node->tmp_refs++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001099 rb_link_node(&node->rb_node, parent, p);
1100 rb_insert_color(&node->rb_node, &proc->nodes);
Todd Kjos656a8002017-06-29 12:01:45 -07001101 node->debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001102 node->proc = proc;
1103 node->ptr = ptr;
1104 node->cookie = cookie;
1105 node->work.type = BINDER_WORK_NODE;
Todd Kjos673068e2017-06-29 12:02:03 -07001106 node->min_priority = flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
1107 node->accept_fds = !!(flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
Todd Kjos9630fe82017-06-29 12:02:00 -07001108 spin_lock_init(&node->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001109 INIT_LIST_HEAD(&node->work.entry);
1110 INIT_LIST_HEAD(&node->async_todo);
1111 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001112 "%d:%d node %d u%016llx c%016llx created\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001113 proc->pid, current->pid, node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001114 (u64)node->ptr, (u64)node->cookie);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001115
1116 return node;
1117}
1118
1119static struct binder_node *binder_new_node(struct binder_proc *proc,
1120 struct flat_binder_object *fp)
1121{
1122 struct binder_node *node;
1123 struct binder_node *new_node = kzalloc(sizeof(*node), GFP_KERNEL);
1124
1125 if (!new_node)
1126 return NULL;
1127 binder_inner_proc_lock(proc);
1128 node = binder_init_node_ilocked(proc, new_node, fp);
1129 binder_inner_proc_unlock(proc);
1130 if (node != new_node)
1131 /*
1132 * The node was already added by another thread
1133 */
1134 kfree(new_node);
1135
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001136 return node;
1137}
1138
Todd Kjosed297212017-06-29 12:02:01 -07001139static void binder_free_node(struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001140{
Todd Kjosed297212017-06-29 12:02:01 -07001141 kfree(node);
1142 binder_stats_deleted(BINDER_STAT_NODE);
1143}
1144
Todd Kjos673068e2017-06-29 12:02:03 -07001145static int binder_inc_node_nilocked(struct binder_node *node, int strong,
1146 int internal,
1147 struct list_head *target_list)
Todd Kjosed297212017-06-29 12:02:01 -07001148{
Todd Kjos673068e2017-06-29 12:02:03 -07001149 struct binder_proc *proc = node->proc;
1150
1151 BUG_ON(!spin_is_locked(&node->lock));
1152 if (proc)
1153 BUG_ON(!spin_is_locked(&proc->inner_lock));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001154 if (strong) {
1155 if (internal) {
1156 if (target_list == NULL &&
1157 node->internal_strong_refs == 0 &&
Martijn Coenen342e5c92017-02-03 14:40:46 -08001158 !(node->proc &&
1159 node == node->proc->context->binder_context_mgr_node &&
1160 node->has_strong_ref)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301161 pr_err("invalid inc strong node for %d\n",
1162 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001163 return -EINVAL;
1164 }
1165 node->internal_strong_refs++;
1166 } else
1167 node->local_strong_refs++;
1168 if (!node->has_strong_ref && target_list) {
Todd Kjos72196392017-06-29 12:02:02 -07001169 binder_dequeue_work_ilocked(&node->work);
1170 binder_enqueue_work_ilocked(&node->work, target_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001171 }
1172 } else {
1173 if (!internal)
1174 node->local_weak_refs++;
1175 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
1176 if (target_list == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301177 pr_err("invalid inc weak node for %d\n",
1178 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001179 return -EINVAL;
1180 }
Todd Kjos72196392017-06-29 12:02:02 -07001181 binder_enqueue_work_ilocked(&node->work, target_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001182 }
1183 }
1184 return 0;
1185}
1186
Todd Kjosed297212017-06-29 12:02:01 -07001187static int binder_inc_node(struct binder_node *node, int strong, int internal,
1188 struct list_head *target_list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001189{
Todd Kjosed297212017-06-29 12:02:01 -07001190 int ret;
1191
Todd Kjos673068e2017-06-29 12:02:03 -07001192 binder_node_inner_lock(node);
1193 ret = binder_inc_node_nilocked(node, strong, internal, target_list);
1194 binder_node_inner_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07001195
1196 return ret;
1197}
1198
Todd Kjos673068e2017-06-29 12:02:03 -07001199static bool binder_dec_node_nilocked(struct binder_node *node,
1200 int strong, int internal)
Todd Kjosed297212017-06-29 12:02:01 -07001201{
1202 struct binder_proc *proc = node->proc;
1203
Todd Kjos673068e2017-06-29 12:02:03 -07001204 BUG_ON(!spin_is_locked(&node->lock));
Todd Kjosed297212017-06-29 12:02:01 -07001205 if (proc)
1206 BUG_ON(!spin_is_locked(&proc->inner_lock));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001207 if (strong) {
1208 if (internal)
1209 node->internal_strong_refs--;
1210 else
1211 node->local_strong_refs--;
1212 if (node->local_strong_refs || node->internal_strong_refs)
Todd Kjosed297212017-06-29 12:02:01 -07001213 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001214 } else {
1215 if (!internal)
1216 node->local_weak_refs--;
Todd Kjosadc18842017-06-29 12:01:59 -07001217 if (node->local_weak_refs || node->tmp_refs ||
1218 !hlist_empty(&node->refs))
Todd Kjosed297212017-06-29 12:02:01 -07001219 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001220 }
Todd Kjosed297212017-06-29 12:02:01 -07001221
1222 if (proc && (node->has_strong_ref || node->has_weak_ref)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001223 if (list_empty(&node->work.entry)) {
Todd Kjos72196392017-06-29 12:02:02 -07001224 binder_enqueue_work_ilocked(&node->work, &proc->todo);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02001225 binder_wakeup_proc_ilocked(proc, false);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001226 }
1227 } else {
1228 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
Todd Kjosadc18842017-06-29 12:01:59 -07001229 !node->local_weak_refs && !node->tmp_refs) {
Todd Kjosed297212017-06-29 12:02:01 -07001230 if (proc) {
Todd Kjos72196392017-06-29 12:02:02 -07001231 binder_dequeue_work_ilocked(&node->work);
1232 rb_erase(&node->rb_node, &proc->nodes);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001233 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301234 "refless node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001235 node->debug_id);
1236 } else {
Todd Kjos72196392017-06-29 12:02:02 -07001237 BUG_ON(!list_empty(&node->work.entry));
Todd Kjosc44b1232017-06-29 12:01:43 -07001238 spin_lock(&binder_dead_nodes_lock);
Todd Kjosed297212017-06-29 12:02:01 -07001239 /*
1240 * tmp_refs could have changed so
1241 * check it again
1242 */
1243 if (node->tmp_refs) {
1244 spin_unlock(&binder_dead_nodes_lock);
1245 return false;
1246 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001247 hlist_del(&node->dead_node);
Todd Kjosc44b1232017-06-29 12:01:43 -07001248 spin_unlock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001249 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301250 "dead node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001251 node->debug_id);
1252 }
Todd Kjosed297212017-06-29 12:02:01 -07001253 return true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001254 }
1255 }
Todd Kjosed297212017-06-29 12:02:01 -07001256 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001257}
1258
Todd Kjosed297212017-06-29 12:02:01 -07001259static void binder_dec_node(struct binder_node *node, int strong, int internal)
1260{
1261 bool free_node;
1262
Todd Kjos673068e2017-06-29 12:02:03 -07001263 binder_node_inner_lock(node);
1264 free_node = binder_dec_node_nilocked(node, strong, internal);
1265 binder_node_inner_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07001266 if (free_node)
1267 binder_free_node(node);
1268}
1269
1270static void binder_inc_node_tmpref_ilocked(struct binder_node *node)
Todd Kjosadc18842017-06-29 12:01:59 -07001271{
1272 /*
1273 * No call to binder_inc_node() is needed since we
1274 * don't need to inform userspace of any changes to
1275 * tmp_refs
1276 */
1277 node->tmp_refs++;
1278}
1279
1280/**
Todd Kjosed297212017-06-29 12:02:01 -07001281 * binder_inc_node_tmpref() - take a temporary reference on node
1282 * @node: node to reference
1283 *
1284 * Take reference on node to prevent the node from being freed
1285 * while referenced only by a local variable. The inner lock is
1286 * needed to serialize with the node work on the queue (which
1287 * isn't needed after the node is dead). If the node is dead
1288 * (node->proc is NULL), use binder_dead_nodes_lock to protect
1289 * node->tmp_refs against dead-node-only cases where the node
1290 * lock cannot be acquired (eg traversing the dead node list to
1291 * print nodes)
1292 */
1293static void binder_inc_node_tmpref(struct binder_node *node)
1294{
Todd Kjos673068e2017-06-29 12:02:03 -07001295 binder_node_lock(node);
Todd Kjosed297212017-06-29 12:02:01 -07001296 if (node->proc)
1297 binder_inner_proc_lock(node->proc);
1298 else
1299 spin_lock(&binder_dead_nodes_lock);
1300 binder_inc_node_tmpref_ilocked(node);
1301 if (node->proc)
1302 binder_inner_proc_unlock(node->proc);
1303 else
1304 spin_unlock(&binder_dead_nodes_lock);
Todd Kjos673068e2017-06-29 12:02:03 -07001305 binder_node_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07001306}
1307
1308/**
Todd Kjosadc18842017-06-29 12:01:59 -07001309 * binder_dec_node_tmpref() - remove a temporary reference on node
1310 * @node: node to reference
1311 *
1312 * Release temporary reference on node taken via binder_inc_node_tmpref()
1313 */
1314static void binder_dec_node_tmpref(struct binder_node *node)
1315{
Todd Kjosed297212017-06-29 12:02:01 -07001316 bool free_node;
1317
Todd Kjos673068e2017-06-29 12:02:03 -07001318 binder_node_inner_lock(node);
1319 if (!node->proc)
Todd Kjosed297212017-06-29 12:02:01 -07001320 spin_lock(&binder_dead_nodes_lock);
Todd Kjosadc18842017-06-29 12:01:59 -07001321 node->tmp_refs--;
1322 BUG_ON(node->tmp_refs < 0);
Todd Kjosed297212017-06-29 12:02:01 -07001323 if (!node->proc)
1324 spin_unlock(&binder_dead_nodes_lock);
Todd Kjosadc18842017-06-29 12:01:59 -07001325 /*
1326 * Call binder_dec_node() to check if all refcounts are 0
1327 * and cleanup is needed. Calling with strong=0 and internal=1
1328 * causes no actual reference to be released in binder_dec_node().
1329 * If that changes, a change is needed here too.
1330 */
Todd Kjos673068e2017-06-29 12:02:03 -07001331 free_node = binder_dec_node_nilocked(node, 0, 1);
1332 binder_node_inner_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07001333 if (free_node)
1334 binder_free_node(node);
Todd Kjosadc18842017-06-29 12:01:59 -07001335}
1336
1337static void binder_put_node(struct binder_node *node)
1338{
1339 binder_dec_node_tmpref(node);
1340}
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001341
Todd Kjos2c1838d2017-06-29 12:02:08 -07001342static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc,
1343 u32 desc, bool need_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001344{
1345 struct rb_node *n = proc->refs_by_desc.rb_node;
1346 struct binder_ref *ref;
1347
1348 while (n) {
1349 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1350
Todd Kjos372e3142017-06-29 12:01:58 -07001351 if (desc < ref->data.desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001352 n = n->rb_left;
Todd Kjos372e3142017-06-29 12:01:58 -07001353 } else if (desc > ref->data.desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001354 n = n->rb_right;
Todd Kjos372e3142017-06-29 12:01:58 -07001355 } else if (need_strong_ref && !ref->data.strong) {
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001356 binder_user_error("tried to use weak ref as strong ref\n");
1357 return NULL;
1358 } else {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001359 return ref;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001360 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001361 }
1362 return NULL;
1363}
1364
Todd Kjos372e3142017-06-29 12:01:58 -07001365/**
Todd Kjos2c1838d2017-06-29 12:02:08 -07001366 * binder_get_ref_for_node_olocked() - get the ref associated with given node
Todd Kjos372e3142017-06-29 12:01:58 -07001367 * @proc: binder_proc that owns the ref
1368 * @node: binder_node of target
1369 * @new_ref: newly allocated binder_ref to be initialized or %NULL
1370 *
1371 * Look up the ref for the given node and return it if it exists
1372 *
1373 * If it doesn't exist and the caller provides a newly allocated
1374 * ref, initialize the fields of the newly allocated ref and insert
1375 * into the given proc rb_trees and node refs list.
1376 *
1377 * Return: the ref for node. It is possible that another thread
1378 * allocated/initialized the ref first in which case the
1379 * returned ref would be different than the passed-in
1380 * new_ref. new_ref must be kfree'd by the caller in
1381 * this case.
1382 */
Todd Kjos2c1838d2017-06-29 12:02:08 -07001383static struct binder_ref *binder_get_ref_for_node_olocked(
1384 struct binder_proc *proc,
1385 struct binder_node *node,
1386 struct binder_ref *new_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001387{
Todd Kjos372e3142017-06-29 12:01:58 -07001388 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001389 struct rb_node **p = &proc->refs_by_node.rb_node;
1390 struct rb_node *parent = NULL;
Todd Kjos372e3142017-06-29 12:01:58 -07001391 struct binder_ref *ref;
1392 struct rb_node *n;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001393
1394 while (*p) {
1395 parent = *p;
1396 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1397
1398 if (node < ref->node)
1399 p = &(*p)->rb_left;
1400 else if (node > ref->node)
1401 p = &(*p)->rb_right;
1402 else
1403 return ref;
1404 }
Todd Kjos372e3142017-06-29 12:01:58 -07001405 if (!new_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001406 return NULL;
Todd Kjos372e3142017-06-29 12:01:58 -07001407
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001408 binder_stats_created(BINDER_STAT_REF);
Todd Kjos372e3142017-06-29 12:01:58 -07001409 new_ref->data.debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001410 new_ref->proc = proc;
1411 new_ref->node = node;
1412 rb_link_node(&new_ref->rb_node_node, parent, p);
1413 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1414
Todd Kjos372e3142017-06-29 12:01:58 -07001415 new_ref->data.desc = (node == context->binder_context_mgr_node) ? 0 : 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001416 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1417 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Todd Kjos372e3142017-06-29 12:01:58 -07001418 if (ref->data.desc > new_ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001419 break;
Todd Kjos372e3142017-06-29 12:01:58 -07001420 new_ref->data.desc = ref->data.desc + 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001421 }
1422
1423 p = &proc->refs_by_desc.rb_node;
1424 while (*p) {
1425 parent = *p;
1426 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1427
Todd Kjos372e3142017-06-29 12:01:58 -07001428 if (new_ref->data.desc < ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001429 p = &(*p)->rb_left;
Todd Kjos372e3142017-06-29 12:01:58 -07001430 else if (new_ref->data.desc > ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001431 p = &(*p)->rb_right;
1432 else
1433 BUG();
1434 }
1435 rb_link_node(&new_ref->rb_node_desc, parent, p);
1436 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
Todd Kjos673068e2017-06-29 12:02:03 -07001437
1438 binder_node_lock(node);
Todd Kjose4cffcf2017-06-29 12:01:50 -07001439 hlist_add_head(&new_ref->node_entry, &node->refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001440
Todd Kjose4cffcf2017-06-29 12:01:50 -07001441 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1442 "%d new ref %d desc %d for node %d\n",
Todd Kjos372e3142017-06-29 12:01:58 -07001443 proc->pid, new_ref->data.debug_id, new_ref->data.desc,
Todd Kjose4cffcf2017-06-29 12:01:50 -07001444 node->debug_id);
Todd Kjos673068e2017-06-29 12:02:03 -07001445 binder_node_unlock(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001446 return new_ref;
1447}
1448
Todd Kjos2c1838d2017-06-29 12:02:08 -07001449static void binder_cleanup_ref_olocked(struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001450{
Todd Kjosed297212017-06-29 12:02:01 -07001451 bool delete_node = false;
Todd Kjosed297212017-06-29 12:02:01 -07001452
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001453 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301454 "%d delete ref %d desc %d for node %d\n",
Todd Kjos372e3142017-06-29 12:01:58 -07001455 ref->proc->pid, ref->data.debug_id, ref->data.desc,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301456 ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001457
1458 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1459 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
Todd Kjos372e3142017-06-29 12:01:58 -07001460
Todd Kjos673068e2017-06-29 12:02:03 -07001461 binder_node_inner_lock(ref->node);
Todd Kjos372e3142017-06-29 12:01:58 -07001462 if (ref->data.strong)
Todd Kjos673068e2017-06-29 12:02:03 -07001463 binder_dec_node_nilocked(ref->node, 1, 1);
Todd Kjos372e3142017-06-29 12:01:58 -07001464
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001465 hlist_del(&ref->node_entry);
Todd Kjos673068e2017-06-29 12:02:03 -07001466 delete_node = binder_dec_node_nilocked(ref->node, 0, 1);
1467 binder_node_inner_unlock(ref->node);
Todd Kjosed297212017-06-29 12:02:01 -07001468 /*
1469 * Clear ref->node unless we want the caller to free the node
1470 */
1471 if (!delete_node) {
1472 /*
1473 * The caller uses ref->node to determine
1474 * whether the node needs to be freed. Clear
1475 * it since the node is still alive.
1476 */
1477 ref->node = NULL;
1478 }
Todd Kjos372e3142017-06-29 12:01:58 -07001479
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001480 if (ref->death) {
1481 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301482 "%d delete ref %d desc %d has death notification\n",
Todd Kjos372e3142017-06-29 12:01:58 -07001483 ref->proc->pid, ref->data.debug_id,
1484 ref->data.desc);
Todd Kjos72196392017-06-29 12:02:02 -07001485 binder_dequeue_work(ref->proc, &ref->death->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001486 binder_stats_deleted(BINDER_STAT_DEATH);
1487 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001488 binder_stats_deleted(BINDER_STAT_REF);
1489}
1490
Todd Kjos372e3142017-06-29 12:01:58 -07001491/**
Todd Kjos2c1838d2017-06-29 12:02:08 -07001492 * binder_inc_ref_olocked() - increment the ref for given handle
Todd Kjos372e3142017-06-29 12:01:58 -07001493 * @ref: ref to be incremented
1494 * @strong: if true, strong increment, else weak
1495 * @target_list: list to queue node work on
1496 *
Todd Kjos2c1838d2017-06-29 12:02:08 -07001497 * Increment the ref. @ref->proc->outer_lock must be held on entry
Todd Kjos372e3142017-06-29 12:01:58 -07001498 *
1499 * Return: 0, if successful, else errno
1500 */
Todd Kjos2c1838d2017-06-29 12:02:08 -07001501static int binder_inc_ref_olocked(struct binder_ref *ref, int strong,
1502 struct list_head *target_list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001503{
1504 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +09001505
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001506 if (strong) {
Todd Kjos372e3142017-06-29 12:01:58 -07001507 if (ref->data.strong == 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001508 ret = binder_inc_node(ref->node, 1, 1, target_list);
1509 if (ret)
1510 return ret;
1511 }
Todd Kjos372e3142017-06-29 12:01:58 -07001512 ref->data.strong++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001513 } else {
Todd Kjos372e3142017-06-29 12:01:58 -07001514 if (ref->data.weak == 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001515 ret = binder_inc_node(ref->node, 0, 1, target_list);
1516 if (ret)
1517 return ret;
1518 }
Todd Kjos372e3142017-06-29 12:01:58 -07001519 ref->data.weak++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001520 }
1521 return 0;
1522}
1523
Todd Kjos372e3142017-06-29 12:01:58 -07001524/**
1525 * binder_dec_ref() - dec the ref for given handle
1526 * @ref: ref to be decremented
1527 * @strong: if true, strong decrement, else weak
1528 *
1529 * Decrement the ref.
1530 *
Todd Kjos372e3142017-06-29 12:01:58 -07001531 * Return: true if ref is cleaned up and ready to be freed
1532 */
Todd Kjos2c1838d2017-06-29 12:02:08 -07001533static bool binder_dec_ref_olocked(struct binder_ref *ref, int strong)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001534{
1535 if (strong) {
Todd Kjos372e3142017-06-29 12:01:58 -07001536 if (ref->data.strong == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301537 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
Todd Kjos372e3142017-06-29 12:01:58 -07001538 ref->proc->pid, ref->data.debug_id,
1539 ref->data.desc, ref->data.strong,
1540 ref->data.weak);
1541 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001542 }
Todd Kjos372e3142017-06-29 12:01:58 -07001543 ref->data.strong--;
Todd Kjosed297212017-06-29 12:02:01 -07001544 if (ref->data.strong == 0)
1545 binder_dec_node(ref->node, strong, 1);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001546 } else {
Todd Kjos372e3142017-06-29 12:01:58 -07001547 if (ref->data.weak == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301548 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
Todd Kjos372e3142017-06-29 12:01:58 -07001549 ref->proc->pid, ref->data.debug_id,
1550 ref->data.desc, ref->data.strong,
1551 ref->data.weak);
1552 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001553 }
Todd Kjos372e3142017-06-29 12:01:58 -07001554 ref->data.weak--;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001555 }
Todd Kjos372e3142017-06-29 12:01:58 -07001556 if (ref->data.strong == 0 && ref->data.weak == 0) {
Todd Kjos2c1838d2017-06-29 12:02:08 -07001557 binder_cleanup_ref_olocked(ref);
Todd Kjos372e3142017-06-29 12:01:58 -07001558 return true;
1559 }
1560 return false;
1561}
1562
1563/**
1564 * binder_get_node_from_ref() - get the node from the given proc/desc
1565 * @proc: proc containing the ref
1566 * @desc: the handle associated with the ref
1567 * @need_strong_ref: if true, only return node if ref is strong
1568 * @rdata: the id/refcount data for the ref
1569 *
1570 * Given a proc and ref handle, return the associated binder_node
1571 *
1572 * Return: a binder_node or NULL if not found or not strong when strong required
1573 */
1574static struct binder_node *binder_get_node_from_ref(
1575 struct binder_proc *proc,
1576 u32 desc, bool need_strong_ref,
1577 struct binder_ref_data *rdata)
1578{
1579 struct binder_node *node;
1580 struct binder_ref *ref;
1581
Todd Kjos2c1838d2017-06-29 12:02:08 -07001582 binder_proc_lock(proc);
1583 ref = binder_get_ref_olocked(proc, desc, need_strong_ref);
Todd Kjos372e3142017-06-29 12:01:58 -07001584 if (!ref)
1585 goto err_no_ref;
1586 node = ref->node;
Todd Kjosadc18842017-06-29 12:01:59 -07001587 /*
1588 * Take an implicit reference on the node to ensure
1589 * it stays alive until the call to binder_put_node()
1590 */
1591 binder_inc_node_tmpref(node);
Todd Kjos372e3142017-06-29 12:01:58 -07001592 if (rdata)
1593 *rdata = ref->data;
Todd Kjos2c1838d2017-06-29 12:02:08 -07001594 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07001595
1596 return node;
1597
1598err_no_ref:
Todd Kjos2c1838d2017-06-29 12:02:08 -07001599 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07001600 return NULL;
1601}
1602
1603/**
1604 * binder_free_ref() - free the binder_ref
1605 * @ref: ref to free
1606 *
Todd Kjosed297212017-06-29 12:02:01 -07001607 * Free the binder_ref. Free the binder_node indicated by ref->node
1608 * (if non-NULL) and the binder_ref_death indicated by ref->death.
Todd Kjos372e3142017-06-29 12:01:58 -07001609 */
1610static void binder_free_ref(struct binder_ref *ref)
1611{
Todd Kjosed297212017-06-29 12:02:01 -07001612 if (ref->node)
1613 binder_free_node(ref->node);
Todd Kjos372e3142017-06-29 12:01:58 -07001614 kfree(ref->death);
1615 kfree(ref);
1616}
1617
1618/**
1619 * binder_update_ref_for_handle() - inc/dec the ref for given handle
1620 * @proc: proc containing the ref
1621 * @desc: the handle associated with the ref
1622 * @increment: true=inc reference, false=dec reference
1623 * @strong: true=strong reference, false=weak reference
1624 * @rdata: the id/refcount data for the ref
1625 *
1626 * Given a proc and ref handle, increment or decrement the ref
1627 * according to "increment" arg.
1628 *
1629 * Return: 0 if successful, else errno
1630 */
1631static int binder_update_ref_for_handle(struct binder_proc *proc,
1632 uint32_t desc, bool increment, bool strong,
1633 struct binder_ref_data *rdata)
1634{
1635 int ret = 0;
1636 struct binder_ref *ref;
1637 bool delete_ref = false;
1638
Todd Kjos2c1838d2017-06-29 12:02:08 -07001639 binder_proc_lock(proc);
1640 ref = binder_get_ref_olocked(proc, desc, strong);
Todd Kjos372e3142017-06-29 12:01:58 -07001641 if (!ref) {
1642 ret = -EINVAL;
1643 goto err_no_ref;
1644 }
1645 if (increment)
Todd Kjos2c1838d2017-06-29 12:02:08 -07001646 ret = binder_inc_ref_olocked(ref, strong, NULL);
Todd Kjos372e3142017-06-29 12:01:58 -07001647 else
Todd Kjos2c1838d2017-06-29 12:02:08 -07001648 delete_ref = binder_dec_ref_olocked(ref, strong);
Todd Kjos372e3142017-06-29 12:01:58 -07001649
1650 if (rdata)
1651 *rdata = ref->data;
Todd Kjos2c1838d2017-06-29 12:02:08 -07001652 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07001653
1654 if (delete_ref)
1655 binder_free_ref(ref);
1656 return ret;
1657
1658err_no_ref:
Todd Kjos2c1838d2017-06-29 12:02:08 -07001659 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07001660 return ret;
1661}
1662
1663/**
1664 * binder_dec_ref_for_handle() - dec the ref for given handle
1665 * @proc: proc containing the ref
1666 * @desc: the handle associated with the ref
1667 * @strong: true=strong reference, false=weak reference
1668 * @rdata: the id/refcount data for the ref
1669 *
1670 * Just calls binder_update_ref_for_handle() to decrement the ref.
1671 *
1672 * Return: 0 if successful, else errno
1673 */
1674static int binder_dec_ref_for_handle(struct binder_proc *proc,
1675 uint32_t desc, bool strong, struct binder_ref_data *rdata)
1676{
1677 return binder_update_ref_for_handle(proc, desc, false, strong, rdata);
1678}
1679
1680
1681/**
1682 * binder_inc_ref_for_node() - increment the ref for given proc/node
1683 * @proc: proc containing the ref
1684 * @node: target node
1685 * @strong: true=strong reference, false=weak reference
1686 * @target_list: worklist to use if node is incremented
1687 * @rdata: the id/refcount data for the ref
1688 *
1689 * Given a proc and node, increment the ref. Create the ref if it
1690 * doesn't already exist
1691 *
1692 * Return: 0 if successful, else errno
1693 */
1694static int binder_inc_ref_for_node(struct binder_proc *proc,
1695 struct binder_node *node,
1696 bool strong,
1697 struct list_head *target_list,
1698 struct binder_ref_data *rdata)
1699{
1700 struct binder_ref *ref;
1701 struct binder_ref *new_ref = NULL;
1702 int ret = 0;
1703
Todd Kjos2c1838d2017-06-29 12:02:08 -07001704 binder_proc_lock(proc);
1705 ref = binder_get_ref_for_node_olocked(proc, node, NULL);
Todd Kjos372e3142017-06-29 12:01:58 -07001706 if (!ref) {
Todd Kjos2c1838d2017-06-29 12:02:08 -07001707 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07001708 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1709 if (!new_ref)
1710 return -ENOMEM;
Todd Kjos2c1838d2017-06-29 12:02:08 -07001711 binder_proc_lock(proc);
1712 ref = binder_get_ref_for_node_olocked(proc, node, new_ref);
Todd Kjos372e3142017-06-29 12:01:58 -07001713 }
Todd Kjos2c1838d2017-06-29 12:02:08 -07001714 ret = binder_inc_ref_olocked(ref, strong, target_list);
Todd Kjos372e3142017-06-29 12:01:58 -07001715 *rdata = ref->data;
Todd Kjos2c1838d2017-06-29 12:02:08 -07001716 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07001717 if (new_ref && ref != new_ref)
1718 /*
1719 * Another thread created the ref first so
1720 * free the one we allocated
1721 */
1722 kfree(new_ref);
1723 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001724}
1725
Martijn Coenen0b89d692017-06-29 12:02:06 -07001726static void binder_pop_transaction_ilocked(struct binder_thread *target_thread,
1727 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001728{
Todd Kjosb6d282c2017-06-29 12:01:54 -07001729 BUG_ON(!target_thread);
Martijn Coenen0b89d692017-06-29 12:02:06 -07001730 BUG_ON(!spin_is_locked(&target_thread->proc->inner_lock));
Todd Kjosb6d282c2017-06-29 12:01:54 -07001731 BUG_ON(target_thread->transaction_stack != t);
1732 BUG_ON(target_thread->transaction_stack->from != target_thread);
1733 target_thread->transaction_stack =
1734 target_thread->transaction_stack->from_parent;
1735 t->from = NULL;
1736}
1737
Todd Kjos7a4408c2017-06-29 12:01:57 -07001738/**
1739 * binder_thread_dec_tmpref() - decrement thread->tmp_ref
1740 * @thread: thread to decrement
1741 *
1742 * A thread needs to be kept alive while being used to create or
1743 * handle a transaction. binder_get_txn_from() is used to safely
1744 * extract t->from from a binder_transaction and keep the thread
1745 * indicated by t->from from being freed. When done with that
1746 * binder_thread, this function is called to decrement the
1747 * tmp_ref and free if appropriate (thread has been released
1748 * and no transaction being processed by the driver)
1749 */
1750static void binder_thread_dec_tmpref(struct binder_thread *thread)
1751{
1752 /*
1753 * atomic is used to protect the counter value while
1754 * it cannot reach zero or thread->is_dead is false
Todd Kjos7a4408c2017-06-29 12:01:57 -07001755 */
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07001756 binder_inner_proc_lock(thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07001757 atomic_dec(&thread->tmp_ref);
1758 if (thread->is_dead && !atomic_read(&thread->tmp_ref)) {
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07001759 binder_inner_proc_unlock(thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07001760 binder_free_thread(thread);
1761 return;
1762 }
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07001763 binder_inner_proc_unlock(thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07001764}
1765
1766/**
1767 * binder_proc_dec_tmpref() - decrement proc->tmp_ref
1768 * @proc: proc to decrement
1769 *
1770 * A binder_proc needs to be kept alive while being used to create or
1771 * handle a transaction. proc->tmp_ref is incremented when
1772 * creating a new transaction or the binder_proc is currently in-use
1773 * by threads that are being released. When done with the binder_proc,
1774 * this function is called to decrement the counter and free the
1775 * proc if appropriate (proc has been released, all threads have
1776 * been released and not currenly in-use to process a transaction).
1777 */
1778static void binder_proc_dec_tmpref(struct binder_proc *proc)
1779{
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07001780 binder_inner_proc_lock(proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07001781 proc->tmp_ref--;
1782 if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) &&
1783 !proc->tmp_ref) {
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07001784 binder_inner_proc_unlock(proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07001785 binder_free_proc(proc);
1786 return;
1787 }
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07001788 binder_inner_proc_unlock(proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07001789}
1790
1791/**
1792 * binder_get_txn_from() - safely extract the "from" thread in transaction
1793 * @t: binder transaction for t->from
1794 *
1795 * Atomically return the "from" thread and increment the tmp_ref
1796 * count for the thread to ensure it stays alive until
1797 * binder_thread_dec_tmpref() is called.
1798 *
1799 * Return: the value of t->from
1800 */
1801static struct binder_thread *binder_get_txn_from(
1802 struct binder_transaction *t)
1803{
1804 struct binder_thread *from;
1805
1806 spin_lock(&t->lock);
1807 from = t->from;
1808 if (from)
1809 atomic_inc(&from->tmp_ref);
1810 spin_unlock(&t->lock);
1811 return from;
1812}
1813
Martijn Coenen0b89d692017-06-29 12:02:06 -07001814/**
1815 * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock
1816 * @t: binder transaction for t->from
1817 *
1818 * Same as binder_get_txn_from() except it also acquires the proc->inner_lock
1819 * to guarantee that the thread cannot be released while operating on it.
1820 * The caller must call binder_inner_proc_unlock() to release the inner lock
1821 * as well as call binder_dec_thread_txn() to release the reference.
1822 *
1823 * Return: the value of t->from
1824 */
1825static struct binder_thread *binder_get_txn_from_and_acq_inner(
1826 struct binder_transaction *t)
1827{
1828 struct binder_thread *from;
1829
1830 from = binder_get_txn_from(t);
1831 if (!from)
1832 return NULL;
1833 binder_inner_proc_lock(from->proc);
1834 if (t->from) {
1835 BUG_ON(from != t->from);
1836 return from;
1837 }
1838 binder_inner_proc_unlock(from->proc);
1839 binder_thread_dec_tmpref(from);
1840 return NULL;
1841}
1842
Todd Kjosb6d282c2017-06-29 12:01:54 -07001843static void binder_free_transaction(struct binder_transaction *t)
1844{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001845 if (t->buffer)
1846 t->buffer->transaction = NULL;
1847 kfree(t);
1848 binder_stats_deleted(BINDER_STAT_TRANSACTION);
1849}
1850
1851static void binder_send_failed_reply(struct binder_transaction *t,
1852 uint32_t error_code)
1853{
1854 struct binder_thread *target_thread;
Lucas Tanured4ec15e2014-07-13 21:31:05 -03001855 struct binder_transaction *next;
Seunghun Lee10f62862014-05-01 01:30:23 +09001856
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001857 BUG_ON(t->flags & TF_ONE_WAY);
1858 while (1) {
Martijn Coenen0b89d692017-06-29 12:02:06 -07001859 target_thread = binder_get_txn_from_and_acq_inner(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001860 if (target_thread) {
Todd Kjos26549d12017-06-29 12:01:55 -07001861 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1862 "send failed reply for transaction %d to %d:%d\n",
1863 t->debug_id,
1864 target_thread->proc->pid,
1865 target_thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001866
Martijn Coenen0b89d692017-06-29 12:02:06 -07001867 binder_pop_transaction_ilocked(target_thread, t);
Todd Kjos26549d12017-06-29 12:01:55 -07001868 if (target_thread->reply_error.cmd == BR_OK) {
1869 target_thread->reply_error.cmd = error_code;
Martijn Coenen0b89d692017-06-29 12:02:06 -07001870 binder_enqueue_work_ilocked(
Todd Kjos72196392017-06-29 12:02:02 -07001871 &target_thread->reply_error.work,
Todd Kjos26549d12017-06-29 12:01:55 -07001872 &target_thread->todo);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001873 wake_up_interruptible(&target_thread->wait);
1874 } else {
Todd Kjos26549d12017-06-29 12:01:55 -07001875 WARN(1, "Unexpected reply error: %u\n",
1876 target_thread->reply_error.cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001877 }
Martijn Coenen0b89d692017-06-29 12:02:06 -07001878 binder_inner_proc_unlock(target_thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07001879 binder_thread_dec_tmpref(target_thread);
Todd Kjos26549d12017-06-29 12:01:55 -07001880 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001881 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001882 }
Lucas Tanured4ec15e2014-07-13 21:31:05 -03001883 next = t->from_parent;
1884
1885 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1886 "send failed reply for transaction %d, target dead\n",
1887 t->debug_id);
1888
Todd Kjosb6d282c2017-06-29 12:01:54 -07001889 binder_free_transaction(t);
Lucas Tanured4ec15e2014-07-13 21:31:05 -03001890 if (next == NULL) {
1891 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1892 "reply failed, no target thread at root\n");
1893 return;
1894 }
1895 t = next;
1896 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1897 "reply failed, no target thread -- retry %d\n",
1898 t->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001899 }
1900}
1901
Martijn Coenenfeba3902017-02-03 14:40:45 -08001902/**
1903 * binder_validate_object() - checks for a valid metadata object in a buffer.
1904 * @buffer: binder_buffer that we're parsing.
1905 * @offset: offset in the buffer at which to validate an object.
1906 *
1907 * Return: If there's a valid metadata object at @offset in @buffer, the
1908 * size of that object. Otherwise, it returns zero.
1909 */
1910static size_t binder_validate_object(struct binder_buffer *buffer, u64 offset)
1911{
1912 /* Check if we can read a header first */
1913 struct binder_object_header *hdr;
1914 size_t object_size = 0;
1915
1916 if (offset > buffer->data_size - sizeof(*hdr) ||
1917 buffer->data_size < sizeof(*hdr) ||
1918 !IS_ALIGNED(offset, sizeof(u32)))
1919 return 0;
1920
1921 /* Ok, now see if we can read a complete object. */
1922 hdr = (struct binder_object_header *)(buffer->data + offset);
1923 switch (hdr->type) {
1924 case BINDER_TYPE_BINDER:
1925 case BINDER_TYPE_WEAK_BINDER:
1926 case BINDER_TYPE_HANDLE:
1927 case BINDER_TYPE_WEAK_HANDLE:
1928 object_size = sizeof(struct flat_binder_object);
1929 break;
1930 case BINDER_TYPE_FD:
1931 object_size = sizeof(struct binder_fd_object);
1932 break;
Martijn Coenen79802402017-02-03 14:40:51 -08001933 case BINDER_TYPE_PTR:
1934 object_size = sizeof(struct binder_buffer_object);
1935 break;
Martijn Coenendef95c72017-02-03 14:40:52 -08001936 case BINDER_TYPE_FDA:
1937 object_size = sizeof(struct binder_fd_array_object);
1938 break;
Martijn Coenenfeba3902017-02-03 14:40:45 -08001939 default:
1940 return 0;
1941 }
1942 if (offset <= buffer->data_size - object_size &&
1943 buffer->data_size >= object_size)
1944 return object_size;
1945 else
1946 return 0;
1947}
1948
Martijn Coenen79802402017-02-03 14:40:51 -08001949/**
1950 * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
1951 * @b: binder_buffer containing the object
1952 * @index: index in offset array at which the binder_buffer_object is
1953 * located
1954 * @start: points to the start of the offset array
1955 * @num_valid: the number of valid offsets in the offset array
1956 *
1957 * Return: If @index is within the valid range of the offset array
1958 * described by @start and @num_valid, and if there's a valid
1959 * binder_buffer_object at the offset found in index @index
1960 * of the offset array, that object is returned. Otherwise,
1961 * %NULL is returned.
1962 * Note that the offset found in index @index itself is not
1963 * verified; this function assumes that @num_valid elements
1964 * from @start were previously verified to have valid offsets.
1965 */
1966static struct binder_buffer_object *binder_validate_ptr(struct binder_buffer *b,
1967 binder_size_t index,
1968 binder_size_t *start,
1969 binder_size_t num_valid)
1970{
1971 struct binder_buffer_object *buffer_obj;
1972 binder_size_t *offp;
1973
1974 if (index >= num_valid)
1975 return NULL;
1976
1977 offp = start + index;
1978 buffer_obj = (struct binder_buffer_object *)(b->data + *offp);
1979 if (buffer_obj->hdr.type != BINDER_TYPE_PTR)
1980 return NULL;
1981
1982 return buffer_obj;
1983}
1984
1985/**
1986 * binder_validate_fixup() - validates pointer/fd fixups happen in order.
1987 * @b: transaction buffer
1988 * @objects_start start of objects buffer
1989 * @buffer: binder_buffer_object in which to fix up
1990 * @offset: start offset in @buffer to fix up
1991 * @last_obj: last binder_buffer_object that we fixed up in
1992 * @last_min_offset: minimum fixup offset in @last_obj
1993 *
1994 * Return: %true if a fixup in buffer @buffer at offset @offset is
1995 * allowed.
1996 *
1997 * For safety reasons, we only allow fixups inside a buffer to happen
1998 * at increasing offsets; additionally, we only allow fixup on the last
1999 * buffer object that was verified, or one of its parents.
2000 *
2001 * Example of what is allowed:
2002 *
2003 * A
2004 * B (parent = A, offset = 0)
2005 * C (parent = A, offset = 16)
2006 * D (parent = C, offset = 0)
2007 * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
2008 *
2009 * Examples of what is not allowed:
2010 *
2011 * Decreasing offsets within the same parent:
2012 * A
2013 * C (parent = A, offset = 16)
2014 * B (parent = A, offset = 0) // decreasing offset within A
2015 *
2016 * Referring to a parent that wasn't the last object or any of its parents:
2017 * A
2018 * B (parent = A, offset = 0)
2019 * C (parent = A, offset = 0)
2020 * C (parent = A, offset = 16)
2021 * D (parent = B, offset = 0) // B is not A or any of A's parents
2022 */
2023static bool binder_validate_fixup(struct binder_buffer *b,
2024 binder_size_t *objects_start,
2025 struct binder_buffer_object *buffer,
2026 binder_size_t fixup_offset,
2027 struct binder_buffer_object *last_obj,
2028 binder_size_t last_min_offset)
2029{
2030 if (!last_obj) {
2031 /* Nothing to fix up in */
2032 return false;
2033 }
2034
2035 while (last_obj != buffer) {
2036 /*
2037 * Safe to retrieve the parent of last_obj, since it
2038 * was already previously verified by the driver.
2039 */
2040 if ((last_obj->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0)
2041 return false;
2042 last_min_offset = last_obj->parent_offset + sizeof(uintptr_t);
2043 last_obj = (struct binder_buffer_object *)
2044 (b->data + *(objects_start + last_obj->parent));
2045 }
2046 return (fixup_offset >= last_min_offset);
2047}
2048
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002049static void binder_transaction_buffer_release(struct binder_proc *proc,
2050 struct binder_buffer *buffer,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002051 binder_size_t *failed_at)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002052{
Martijn Coenen79802402017-02-03 14:40:51 -08002053 binder_size_t *offp, *off_start, *off_end;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002054 int debug_id = buffer->debug_id;
2055
2056 binder_debug(BINDER_DEBUG_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302057 "%d buffer release %d, size %zd-%zd, failed at %p\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002058 proc->pid, buffer->debug_id,
2059 buffer->data_size, buffer->offsets_size, failed_at);
2060
2061 if (buffer->target_node)
2062 binder_dec_node(buffer->target_node, 1, 0);
2063
Martijn Coenen79802402017-02-03 14:40:51 -08002064 off_start = (binder_size_t *)(buffer->data +
2065 ALIGN(buffer->data_size, sizeof(void *)));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002066 if (failed_at)
2067 off_end = failed_at;
2068 else
Martijn Coenen79802402017-02-03 14:40:51 -08002069 off_end = (void *)off_start + buffer->offsets_size;
2070 for (offp = off_start; offp < off_end; offp++) {
Martijn Coenenfeba3902017-02-03 14:40:45 -08002071 struct binder_object_header *hdr;
2072 size_t object_size = binder_validate_object(buffer, *offp);
Seunghun Lee10f62862014-05-01 01:30:23 +09002073
Martijn Coenenfeba3902017-02-03 14:40:45 -08002074 if (object_size == 0) {
2075 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
Arve Hjønnevågda498892014-02-21 14:40:26 -08002076 debug_id, (u64)*offp, buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002077 continue;
2078 }
Martijn Coenenfeba3902017-02-03 14:40:45 -08002079 hdr = (struct binder_object_header *)(buffer->data + *offp);
2080 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002081 case BINDER_TYPE_BINDER:
2082 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08002083 struct flat_binder_object *fp;
2084 struct binder_node *node;
Seunghun Lee10f62862014-05-01 01:30:23 +09002085
Martijn Coenenfeba3902017-02-03 14:40:45 -08002086 fp = to_flat_binder_object(hdr);
2087 node = binder_get_node(proc, fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002088 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002089 pr_err("transaction release %d bad node %016llx\n",
2090 debug_id, (u64)fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002091 break;
2092 }
2093 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002094 " node %d u%016llx\n",
2095 node->debug_id, (u64)node->ptr);
Martijn Coenenfeba3902017-02-03 14:40:45 -08002096 binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER,
2097 0);
Todd Kjosadc18842017-06-29 12:01:59 -07002098 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002099 } break;
2100 case BINDER_TYPE_HANDLE:
2101 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08002102 struct flat_binder_object *fp;
Todd Kjos372e3142017-06-29 12:01:58 -07002103 struct binder_ref_data rdata;
2104 int ret;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02002105
Martijn Coenenfeba3902017-02-03 14:40:45 -08002106 fp = to_flat_binder_object(hdr);
Todd Kjos372e3142017-06-29 12:01:58 -07002107 ret = binder_dec_ref_for_handle(proc, fp->handle,
2108 hdr->type == BINDER_TYPE_HANDLE, &rdata);
2109
2110 if (ret) {
2111 pr_err("transaction release %d bad handle %d, ret = %d\n",
2112 debug_id, fp->handle, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002113 break;
2114 }
2115 binder_debug(BINDER_DEBUG_TRANSACTION,
Todd Kjos372e3142017-06-29 12:01:58 -07002116 " ref %d desc %d\n",
2117 rdata.debug_id, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002118 } break;
2119
Martijn Coenenfeba3902017-02-03 14:40:45 -08002120 case BINDER_TYPE_FD: {
2121 struct binder_fd_object *fp = to_binder_fd_object(hdr);
2122
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002123 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenenfeba3902017-02-03 14:40:45 -08002124 " fd %d\n", fp->fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002125 if (failed_at)
Martijn Coenenfeba3902017-02-03 14:40:45 -08002126 task_close_fd(proc, fp->fd);
2127 } break;
Martijn Coenen79802402017-02-03 14:40:51 -08002128 case BINDER_TYPE_PTR:
2129 /*
2130 * Nothing to do here, this will get cleaned up when the
2131 * transaction buffer gets freed
2132 */
2133 break;
Martijn Coenendef95c72017-02-03 14:40:52 -08002134 case BINDER_TYPE_FDA: {
2135 struct binder_fd_array_object *fda;
2136 struct binder_buffer_object *parent;
2137 uintptr_t parent_buffer;
2138 u32 *fd_array;
2139 size_t fd_index;
2140 binder_size_t fd_buf_size;
2141
2142 fda = to_binder_fd_array_object(hdr);
2143 parent = binder_validate_ptr(buffer, fda->parent,
2144 off_start,
2145 offp - off_start);
2146 if (!parent) {
2147 pr_err("transaction release %d bad parent offset",
2148 debug_id);
2149 continue;
2150 }
2151 /*
2152 * Since the parent was already fixed up, convert it
2153 * back to kernel address space to access it
2154 */
2155 parent_buffer = parent->buffer -
Todd Kjos19c98722017-06-29 12:01:40 -07002156 binder_alloc_get_user_buffer_offset(
2157 &proc->alloc);
Martijn Coenendef95c72017-02-03 14:40:52 -08002158
2159 fd_buf_size = sizeof(u32) * fda->num_fds;
2160 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2161 pr_err("transaction release %d invalid number of fds (%lld)\n",
2162 debug_id, (u64)fda->num_fds);
2163 continue;
2164 }
2165 if (fd_buf_size > parent->length ||
2166 fda->parent_offset > parent->length - fd_buf_size) {
2167 /* No space for all file descriptors here. */
2168 pr_err("transaction release %d not enough space for %lld fds in buffer\n",
2169 debug_id, (u64)fda->num_fds);
2170 continue;
2171 }
2172 fd_array = (u32 *)(parent_buffer + fda->parent_offset);
2173 for (fd_index = 0; fd_index < fda->num_fds; fd_index++)
2174 task_close_fd(proc, fd_array[fd_index]);
2175 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002176 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01002177 pr_err("transaction release %d bad object type %x\n",
Martijn Coenenfeba3902017-02-03 14:40:45 -08002178 debug_id, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002179 break;
2180 }
2181 }
2182}
2183
Martijn Coenena056af42017-02-03 14:40:49 -08002184static int binder_translate_binder(struct flat_binder_object *fp,
2185 struct binder_transaction *t,
2186 struct binder_thread *thread)
2187{
2188 struct binder_node *node;
Martijn Coenena056af42017-02-03 14:40:49 -08002189 struct binder_proc *proc = thread->proc;
2190 struct binder_proc *target_proc = t->to_proc;
Todd Kjos372e3142017-06-29 12:01:58 -07002191 struct binder_ref_data rdata;
Todd Kjosadc18842017-06-29 12:01:59 -07002192 int ret = 0;
Martijn Coenena056af42017-02-03 14:40:49 -08002193
2194 node = binder_get_node(proc, fp->binder);
2195 if (!node) {
Todd Kjos673068e2017-06-29 12:02:03 -07002196 node = binder_new_node(proc, fp);
Martijn Coenena056af42017-02-03 14:40:49 -08002197 if (!node)
2198 return -ENOMEM;
Martijn Coenena056af42017-02-03 14:40:49 -08002199 }
2200 if (fp->cookie != node->cookie) {
2201 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
2202 proc->pid, thread->pid, (u64)fp->binder,
2203 node->debug_id, (u64)fp->cookie,
2204 (u64)node->cookie);
Todd Kjosadc18842017-06-29 12:01:59 -07002205 ret = -EINVAL;
2206 goto done;
Martijn Coenena056af42017-02-03 14:40:49 -08002207 }
Todd Kjosadc18842017-06-29 12:01:59 -07002208 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2209 ret = -EPERM;
2210 goto done;
2211 }
Martijn Coenena056af42017-02-03 14:40:49 -08002212
Todd Kjos372e3142017-06-29 12:01:58 -07002213 ret = binder_inc_ref_for_node(target_proc, node,
2214 fp->hdr.type == BINDER_TYPE_BINDER,
2215 &thread->todo, &rdata);
2216 if (ret)
Todd Kjosadc18842017-06-29 12:01:59 -07002217 goto done;
Martijn Coenena056af42017-02-03 14:40:49 -08002218
2219 if (fp->hdr.type == BINDER_TYPE_BINDER)
2220 fp->hdr.type = BINDER_TYPE_HANDLE;
2221 else
2222 fp->hdr.type = BINDER_TYPE_WEAK_HANDLE;
2223 fp->binder = 0;
Todd Kjos372e3142017-06-29 12:01:58 -07002224 fp->handle = rdata.desc;
Martijn Coenena056af42017-02-03 14:40:49 -08002225 fp->cookie = 0;
Martijn Coenena056af42017-02-03 14:40:49 -08002226
Todd Kjos372e3142017-06-29 12:01:58 -07002227 trace_binder_transaction_node_to_ref(t, node, &rdata);
Martijn Coenena056af42017-02-03 14:40:49 -08002228 binder_debug(BINDER_DEBUG_TRANSACTION,
2229 " node %d u%016llx -> ref %d desc %d\n",
2230 node->debug_id, (u64)node->ptr,
Todd Kjos372e3142017-06-29 12:01:58 -07002231 rdata.debug_id, rdata.desc);
Todd Kjosadc18842017-06-29 12:01:59 -07002232done:
2233 binder_put_node(node);
2234 return ret;
Martijn Coenena056af42017-02-03 14:40:49 -08002235}
2236
2237static int binder_translate_handle(struct flat_binder_object *fp,
2238 struct binder_transaction *t,
2239 struct binder_thread *thread)
2240{
Martijn Coenena056af42017-02-03 14:40:49 -08002241 struct binder_proc *proc = thread->proc;
2242 struct binder_proc *target_proc = t->to_proc;
Todd Kjos372e3142017-06-29 12:01:58 -07002243 struct binder_node *node;
2244 struct binder_ref_data src_rdata;
Todd Kjosadc18842017-06-29 12:01:59 -07002245 int ret = 0;
Martijn Coenena056af42017-02-03 14:40:49 -08002246
Todd Kjos372e3142017-06-29 12:01:58 -07002247 node = binder_get_node_from_ref(proc, fp->handle,
2248 fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata);
2249 if (!node) {
Martijn Coenena056af42017-02-03 14:40:49 -08002250 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
2251 proc->pid, thread->pid, fp->handle);
2252 return -EINVAL;
2253 }
Todd Kjosadc18842017-06-29 12:01:59 -07002254 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2255 ret = -EPERM;
2256 goto done;
2257 }
Martijn Coenena056af42017-02-03 14:40:49 -08002258
Todd Kjos673068e2017-06-29 12:02:03 -07002259 binder_node_lock(node);
Todd Kjos372e3142017-06-29 12:01:58 -07002260 if (node->proc == target_proc) {
Martijn Coenena056af42017-02-03 14:40:49 -08002261 if (fp->hdr.type == BINDER_TYPE_HANDLE)
2262 fp->hdr.type = BINDER_TYPE_BINDER;
2263 else
2264 fp->hdr.type = BINDER_TYPE_WEAK_BINDER;
Todd Kjos372e3142017-06-29 12:01:58 -07002265 fp->binder = node->ptr;
2266 fp->cookie = node->cookie;
Todd Kjos673068e2017-06-29 12:02:03 -07002267 if (node->proc)
2268 binder_inner_proc_lock(node->proc);
2269 binder_inc_node_nilocked(node,
2270 fp->hdr.type == BINDER_TYPE_BINDER,
2271 0, NULL);
2272 if (node->proc)
2273 binder_inner_proc_unlock(node->proc);
Todd Kjos372e3142017-06-29 12:01:58 -07002274 trace_binder_transaction_ref_to_node(t, node, &src_rdata);
Martijn Coenena056af42017-02-03 14:40:49 -08002275 binder_debug(BINDER_DEBUG_TRANSACTION,
2276 " ref %d desc %d -> node %d u%016llx\n",
Todd Kjos372e3142017-06-29 12:01:58 -07002277 src_rdata.debug_id, src_rdata.desc, node->debug_id,
2278 (u64)node->ptr);
Todd Kjos673068e2017-06-29 12:02:03 -07002279 binder_node_unlock(node);
Martijn Coenena056af42017-02-03 14:40:49 -08002280 } else {
Todd Kjos372e3142017-06-29 12:01:58 -07002281 int ret;
2282 struct binder_ref_data dest_rdata;
Martijn Coenena056af42017-02-03 14:40:49 -08002283
Todd Kjos673068e2017-06-29 12:02:03 -07002284 binder_node_unlock(node);
Todd Kjos372e3142017-06-29 12:01:58 -07002285 ret = binder_inc_ref_for_node(target_proc, node,
2286 fp->hdr.type == BINDER_TYPE_HANDLE,
2287 NULL, &dest_rdata);
2288 if (ret)
Todd Kjosadc18842017-06-29 12:01:59 -07002289 goto done;
Martijn Coenena056af42017-02-03 14:40:49 -08002290
2291 fp->binder = 0;
Todd Kjos372e3142017-06-29 12:01:58 -07002292 fp->handle = dest_rdata.desc;
Martijn Coenena056af42017-02-03 14:40:49 -08002293 fp->cookie = 0;
Todd Kjos372e3142017-06-29 12:01:58 -07002294 trace_binder_transaction_ref_to_ref(t, node, &src_rdata,
2295 &dest_rdata);
Martijn Coenena056af42017-02-03 14:40:49 -08002296 binder_debug(BINDER_DEBUG_TRANSACTION,
2297 " ref %d desc %d -> ref %d desc %d (node %d)\n",
Todd Kjos372e3142017-06-29 12:01:58 -07002298 src_rdata.debug_id, src_rdata.desc,
2299 dest_rdata.debug_id, dest_rdata.desc,
2300 node->debug_id);
Martijn Coenena056af42017-02-03 14:40:49 -08002301 }
Todd Kjosadc18842017-06-29 12:01:59 -07002302done:
2303 binder_put_node(node);
2304 return ret;
Martijn Coenena056af42017-02-03 14:40:49 -08002305}
2306
2307static int binder_translate_fd(int fd,
2308 struct binder_transaction *t,
2309 struct binder_thread *thread,
2310 struct binder_transaction *in_reply_to)
2311{
2312 struct binder_proc *proc = thread->proc;
2313 struct binder_proc *target_proc = t->to_proc;
2314 int target_fd;
2315 struct file *file;
2316 int ret;
2317 bool target_allows_fd;
2318
2319 if (in_reply_to)
2320 target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS);
2321 else
2322 target_allows_fd = t->buffer->target_node->accept_fds;
2323 if (!target_allows_fd) {
2324 binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
2325 proc->pid, thread->pid,
2326 in_reply_to ? "reply" : "transaction",
2327 fd);
2328 ret = -EPERM;
2329 goto err_fd_not_accepted;
2330 }
2331
2332 file = fget(fd);
2333 if (!file) {
2334 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
2335 proc->pid, thread->pid, fd);
2336 ret = -EBADF;
2337 goto err_fget;
2338 }
2339 ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file);
2340 if (ret < 0) {
2341 ret = -EPERM;
2342 goto err_security;
2343 }
2344
2345 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
2346 if (target_fd < 0) {
2347 ret = -ENOMEM;
2348 goto err_get_unused_fd;
2349 }
2350 task_fd_install(target_proc, target_fd, file);
2351 trace_binder_transaction_fd(t, fd, target_fd);
2352 binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n",
2353 fd, target_fd);
2354
2355 return target_fd;
2356
2357err_get_unused_fd:
2358err_security:
2359 fput(file);
2360err_fget:
2361err_fd_not_accepted:
2362 return ret;
2363}
2364
Martijn Coenendef95c72017-02-03 14:40:52 -08002365static int binder_translate_fd_array(struct binder_fd_array_object *fda,
2366 struct binder_buffer_object *parent,
2367 struct binder_transaction *t,
2368 struct binder_thread *thread,
2369 struct binder_transaction *in_reply_to)
2370{
2371 binder_size_t fdi, fd_buf_size, num_installed_fds;
2372 int target_fd;
2373 uintptr_t parent_buffer;
2374 u32 *fd_array;
2375 struct binder_proc *proc = thread->proc;
2376 struct binder_proc *target_proc = t->to_proc;
2377
2378 fd_buf_size = sizeof(u32) * fda->num_fds;
2379 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2380 binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
2381 proc->pid, thread->pid, (u64)fda->num_fds);
2382 return -EINVAL;
2383 }
2384 if (fd_buf_size > parent->length ||
2385 fda->parent_offset > parent->length - fd_buf_size) {
2386 /* No space for all file descriptors here. */
2387 binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
2388 proc->pid, thread->pid, (u64)fda->num_fds);
2389 return -EINVAL;
2390 }
2391 /*
2392 * Since the parent was already fixed up, convert it
2393 * back to the kernel address space to access it
2394 */
Todd Kjos19c98722017-06-29 12:01:40 -07002395 parent_buffer = parent->buffer -
2396 binder_alloc_get_user_buffer_offset(&target_proc->alloc);
Martijn Coenendef95c72017-02-03 14:40:52 -08002397 fd_array = (u32 *)(parent_buffer + fda->parent_offset);
2398 if (!IS_ALIGNED((unsigned long)fd_array, sizeof(u32))) {
2399 binder_user_error("%d:%d parent offset not aligned correctly.\n",
2400 proc->pid, thread->pid);
2401 return -EINVAL;
2402 }
2403 for (fdi = 0; fdi < fda->num_fds; fdi++) {
2404 target_fd = binder_translate_fd(fd_array[fdi], t, thread,
2405 in_reply_to);
2406 if (target_fd < 0)
2407 goto err_translate_fd_failed;
2408 fd_array[fdi] = target_fd;
2409 }
2410 return 0;
2411
2412err_translate_fd_failed:
2413 /*
2414 * Failed to allocate fd or security error, free fds
2415 * installed so far.
2416 */
2417 num_installed_fds = fdi;
2418 for (fdi = 0; fdi < num_installed_fds; fdi++)
2419 task_close_fd(target_proc, fd_array[fdi]);
2420 return target_fd;
2421}
2422
Martijn Coenen79802402017-02-03 14:40:51 -08002423static int binder_fixup_parent(struct binder_transaction *t,
2424 struct binder_thread *thread,
2425 struct binder_buffer_object *bp,
2426 binder_size_t *off_start,
2427 binder_size_t num_valid,
2428 struct binder_buffer_object *last_fixup_obj,
2429 binder_size_t last_fixup_min_off)
2430{
2431 struct binder_buffer_object *parent;
2432 u8 *parent_buffer;
2433 struct binder_buffer *b = t->buffer;
2434 struct binder_proc *proc = thread->proc;
2435 struct binder_proc *target_proc = t->to_proc;
2436
2437 if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT))
2438 return 0;
2439
2440 parent = binder_validate_ptr(b, bp->parent, off_start, num_valid);
2441 if (!parent) {
2442 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
2443 proc->pid, thread->pid);
2444 return -EINVAL;
2445 }
2446
2447 if (!binder_validate_fixup(b, off_start,
2448 parent, bp->parent_offset,
2449 last_fixup_obj,
2450 last_fixup_min_off)) {
2451 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
2452 proc->pid, thread->pid);
2453 return -EINVAL;
2454 }
2455
2456 if (parent->length < sizeof(binder_uintptr_t) ||
2457 bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) {
2458 /* No space for a pointer here! */
2459 binder_user_error("%d:%d got transaction with invalid parent offset\n",
2460 proc->pid, thread->pid);
2461 return -EINVAL;
2462 }
2463 parent_buffer = (u8 *)(parent->buffer -
Todd Kjos19c98722017-06-29 12:01:40 -07002464 binder_alloc_get_user_buffer_offset(
2465 &target_proc->alloc));
Martijn Coenen79802402017-02-03 14:40:51 -08002466 *(binder_uintptr_t *)(parent_buffer + bp->parent_offset) = bp->buffer;
2467
2468 return 0;
2469}
2470
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002471static void binder_transaction(struct binder_proc *proc,
2472 struct binder_thread *thread,
Martijn Coenen4bfac802017-02-03 14:40:50 -08002473 struct binder_transaction_data *tr, int reply,
2474 binder_size_t extra_buffers_size)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002475{
Martijn Coenena056af42017-02-03 14:40:49 -08002476 int ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002477 struct binder_transaction *t;
2478 struct binder_work *tcomplete;
Martijn Coenen79802402017-02-03 14:40:51 -08002479 binder_size_t *offp, *off_end, *off_start;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08002480 binder_size_t off_min;
Martijn Coenen79802402017-02-03 14:40:51 -08002481 u8 *sg_bufp, *sg_buf_end;
Todd Kjos7a4408c2017-06-29 12:01:57 -07002482 struct binder_proc *target_proc = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002483 struct binder_thread *target_thread = NULL;
2484 struct binder_node *target_node = NULL;
2485 struct list_head *target_list;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002486 struct binder_transaction *in_reply_to = NULL;
2487 struct binder_transaction_log_entry *e;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002488 uint32_t return_error = 0;
2489 uint32_t return_error_param = 0;
2490 uint32_t return_error_line = 0;
Martijn Coenen79802402017-02-03 14:40:51 -08002491 struct binder_buffer_object *last_fixup_obj = NULL;
2492 binder_size_t last_fixup_min_off = 0;
Martijn Coenen342e5c92017-02-03 14:40:46 -08002493 struct binder_context *context = proc->context;
Todd Kjosd99c7332017-06-29 12:01:53 -07002494 int t_debug_id = atomic_inc_return(&binder_last_id);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02002495 bool wakeup_for_proc_work = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002496
2497 e = binder_transaction_log_add(&binder_transaction_log);
Todd Kjosd99c7332017-06-29 12:01:53 -07002498 e->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002499 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
2500 e->from_proc = proc->pid;
2501 e->from_thread = thread->pid;
2502 e->target_handle = tr->target.handle;
2503 e->data_size = tr->data_size;
2504 e->offsets_size = tr->offsets_size;
Martijn Coenen14db3182017-02-03 14:40:47 -08002505 e->context_name = proc->context->name;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002506
2507 if (reply) {
Martijn Coenen0b89d692017-06-29 12:02:06 -07002508 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002509 in_reply_to = thread->transaction_stack;
2510 if (in_reply_to == NULL) {
Martijn Coenen0b89d692017-06-29 12:02:06 -07002511 binder_inner_proc_unlock(proc);
Anmol Sarma56b468f2012-10-30 22:35:43 +05302512 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002513 proc->pid, thread->pid);
2514 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002515 return_error_param = -EPROTO;
2516 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002517 goto err_empty_call_stack;
2518 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002519 if (in_reply_to->to_thread != thread) {
Todd Kjos7a4408c2017-06-29 12:01:57 -07002520 spin_lock(&in_reply_to->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05302521 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 +09002522 proc->pid, thread->pid, in_reply_to->debug_id,
2523 in_reply_to->to_proc ?
2524 in_reply_to->to_proc->pid : 0,
2525 in_reply_to->to_thread ?
2526 in_reply_to->to_thread->pid : 0);
Todd Kjos7a4408c2017-06-29 12:01:57 -07002527 spin_unlock(&in_reply_to->lock);
Martijn Coenen0b89d692017-06-29 12:02:06 -07002528 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002529 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002530 return_error_param = -EPROTO;
2531 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002532 in_reply_to = NULL;
2533 goto err_bad_call_stack;
2534 }
2535 thread->transaction_stack = in_reply_to->to_parent;
Martijn Coenen0b89d692017-06-29 12:02:06 -07002536 binder_inner_proc_unlock(proc);
2537 binder_set_nice(in_reply_to->saved_priority);
2538 target_thread = binder_get_txn_from_and_acq_inner(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002539 if (target_thread == NULL) {
2540 return_error = BR_DEAD_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002541 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002542 goto err_dead_binder;
2543 }
2544 if (target_thread->transaction_stack != in_reply_to) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302545 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 +09002546 proc->pid, thread->pid,
2547 target_thread->transaction_stack ?
2548 target_thread->transaction_stack->debug_id : 0,
2549 in_reply_to->debug_id);
Martijn Coenen0b89d692017-06-29 12:02:06 -07002550 binder_inner_proc_unlock(target_thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002551 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002552 return_error_param = -EPROTO;
2553 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002554 in_reply_to = NULL;
2555 target_thread = NULL;
2556 goto err_dead_binder;
2557 }
2558 target_proc = target_thread->proc;
Todd Kjos7a4408c2017-06-29 12:01:57 -07002559 target_proc->tmp_ref++;
Martijn Coenen0b89d692017-06-29 12:02:06 -07002560 binder_inner_proc_unlock(target_thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002561 } else {
2562 if (tr->target.handle) {
2563 struct binder_ref *ref;
Seunghun Lee10f62862014-05-01 01:30:23 +09002564
Todd Kjoseb349832017-06-29 12:01:56 -07002565 /*
2566 * There must already be a strong ref
2567 * on this node. If so, do a strong
2568 * increment on the node to ensure it
2569 * stays alive until the transaction is
2570 * done.
2571 */
Todd Kjos2c1838d2017-06-29 12:02:08 -07002572 binder_proc_lock(proc);
2573 ref = binder_get_ref_olocked(proc, tr->target.handle,
2574 true);
Todd Kjoseb349832017-06-29 12:01:56 -07002575 if (ref) {
2576 binder_inc_node(ref->node, 1, 0, NULL);
2577 target_node = ref->node;
2578 }
Todd Kjos2c1838d2017-06-29 12:02:08 -07002579 binder_proc_unlock(proc);
Todd Kjoseb349832017-06-29 12:01:56 -07002580 if (target_node == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302581 binder_user_error("%d:%d got transaction to invalid handle\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002582 proc->pid, thread->pid);
2583 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002584 return_error_param = -EINVAL;
2585 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002586 goto err_invalid_target_handle;
2587 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002588 } else {
Todd Kjosc44b1232017-06-29 12:01:43 -07002589 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen342e5c92017-02-03 14:40:46 -08002590 target_node = context->binder_context_mgr_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002591 if (target_node == NULL) {
2592 return_error = BR_DEAD_REPLY;
Todd Kjosc44b1232017-06-29 12:01:43 -07002593 mutex_unlock(&context->context_mgr_node_lock);
Todd Kjos57ada2f2017-06-29 12:01:46 -07002594 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002595 goto err_no_context_mgr_node;
2596 }
Todd Kjoseb349832017-06-29 12:01:56 -07002597 binder_inc_node(target_node, 1, 0, NULL);
Todd Kjosc44b1232017-06-29 12:01:43 -07002598 mutex_unlock(&context->context_mgr_node_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002599 }
2600 e->to_node = target_node->debug_id;
Todd Kjos673068e2017-06-29 12:02:03 -07002601 binder_node_lock(target_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002602 target_proc = target_node->proc;
2603 if (target_proc == NULL) {
Todd Kjos673068e2017-06-29 12:02:03 -07002604 binder_node_unlock(target_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002605 return_error = BR_DEAD_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002606 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002607 goto err_dead_binder;
2608 }
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07002609 binder_inner_proc_lock(target_proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07002610 target_proc->tmp_ref++;
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07002611 binder_inner_proc_unlock(target_proc);
Todd Kjos673068e2017-06-29 12:02:03 -07002612 binder_node_unlock(target_node);
Stephen Smalley79af7302015-01-21 10:54:10 -05002613 if (security_binder_transaction(proc->tsk,
2614 target_proc->tsk) < 0) {
2615 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002616 return_error_param = -EPERM;
2617 return_error_line = __LINE__;
Stephen Smalley79af7302015-01-21 10:54:10 -05002618 goto err_invalid_target_handle;
2619 }
Martijn Coenen0b89d692017-06-29 12:02:06 -07002620 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002621 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
2622 struct binder_transaction *tmp;
Seunghun Lee10f62862014-05-01 01:30:23 +09002623
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002624 tmp = thread->transaction_stack;
2625 if (tmp->to_thread != thread) {
Todd Kjos7a4408c2017-06-29 12:01:57 -07002626 spin_lock(&tmp->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05302627 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 +09002628 proc->pid, thread->pid, tmp->debug_id,
2629 tmp->to_proc ? tmp->to_proc->pid : 0,
2630 tmp->to_thread ?
2631 tmp->to_thread->pid : 0);
Todd Kjos7a4408c2017-06-29 12:01:57 -07002632 spin_unlock(&tmp->lock);
Martijn Coenen0b89d692017-06-29 12:02:06 -07002633 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002634 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002635 return_error_param = -EPROTO;
2636 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002637 goto err_bad_call_stack;
2638 }
2639 while (tmp) {
Todd Kjos7a4408c2017-06-29 12:01:57 -07002640 struct binder_thread *from;
2641
2642 spin_lock(&tmp->lock);
2643 from = tmp->from;
2644 if (from && from->proc == target_proc) {
2645 atomic_inc(&from->tmp_ref);
2646 target_thread = from;
2647 spin_unlock(&tmp->lock);
2648 break;
2649 }
2650 spin_unlock(&tmp->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002651 tmp = tmp->from_parent;
2652 }
2653 }
Martijn Coenen0b89d692017-06-29 12:02:06 -07002654 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002655 }
2656 if (target_thread) {
2657 e->to_thread = target_thread->pid;
2658 target_list = &target_thread->todo;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002659 } else {
2660 target_list = &target_proc->todo;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02002661 wakeup_for_proc_work = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002662 }
2663 e->to_proc = target_proc->pid;
2664
2665 /* TODO: reuse incoming transaction for reply */
2666 t = kzalloc(sizeof(*t), GFP_KERNEL);
2667 if (t == NULL) {
2668 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002669 return_error_param = -ENOMEM;
2670 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002671 goto err_alloc_t_failed;
2672 }
2673 binder_stats_created(BINDER_STAT_TRANSACTION);
Todd Kjos7a4408c2017-06-29 12:01:57 -07002674 spin_lock_init(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002675
2676 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
2677 if (tcomplete == NULL) {
2678 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002679 return_error_param = -ENOMEM;
2680 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002681 goto err_alloc_tcomplete_failed;
2682 }
2683 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
2684
Todd Kjosd99c7332017-06-29 12:01:53 -07002685 t->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002686
2687 if (reply)
2688 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen4bfac802017-02-03 14:40:50 -08002689 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002690 proc->pid, thread->pid, t->debug_id,
2691 target_proc->pid, target_thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002692 (u64)tr->data.ptr.buffer,
2693 (u64)tr->data.ptr.offsets,
Martijn Coenen4bfac802017-02-03 14:40:50 -08002694 (u64)tr->data_size, (u64)tr->offsets_size,
2695 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002696 else
2697 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen4bfac802017-02-03 14:40:50 -08002698 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002699 proc->pid, thread->pid, t->debug_id,
2700 target_proc->pid, target_node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002701 (u64)tr->data.ptr.buffer,
2702 (u64)tr->data.ptr.offsets,
Martijn Coenen4bfac802017-02-03 14:40:50 -08002703 (u64)tr->data_size, (u64)tr->offsets_size,
2704 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002705
2706 if (!reply && !(tr->flags & TF_ONE_WAY))
2707 t->from = thread;
2708 else
2709 t->from = NULL;
Tair Rzayev57bab7c2014-05-31 22:43:34 +03002710 t->sender_euid = task_euid(proc->tsk);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002711 t->to_proc = target_proc;
2712 t->to_thread = target_thread;
2713 t->code = tr->code;
2714 t->flags = tr->flags;
2715 t->priority = task_nice(current);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002716
2717 trace_binder_transaction(reply, t, target_node);
2718
Todd Kjos19c98722017-06-29 12:01:40 -07002719 t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size,
Martijn Coenen4bfac802017-02-03 14:40:50 -08002720 tr->offsets_size, extra_buffers_size,
2721 !reply && (t->flags & TF_ONE_WAY));
Todd Kjos57ada2f2017-06-29 12:01:46 -07002722 if (IS_ERR(t->buffer)) {
2723 /*
2724 * -ESRCH indicates VMA cleared. The target is dying.
2725 */
2726 return_error_param = PTR_ERR(t->buffer);
2727 return_error = return_error_param == -ESRCH ?
2728 BR_DEAD_REPLY : BR_FAILED_REPLY;
2729 return_error_line = __LINE__;
2730 t->buffer = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002731 goto err_binder_alloc_buf_failed;
2732 }
2733 t->buffer->allow_user_free = 0;
2734 t->buffer->debug_id = t->debug_id;
2735 t->buffer->transaction = t;
2736 t->buffer->target_node = target_node;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002737 trace_binder_transaction_alloc_buf(t->buffer);
Martijn Coenen79802402017-02-03 14:40:51 -08002738 off_start = (binder_size_t *)(t->buffer->data +
2739 ALIGN(tr->data_size, sizeof(void *)));
2740 offp = off_start;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002741
Arve Hjønnevågda498892014-02-21 14:40:26 -08002742 if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t)
2743 tr->data.ptr.buffer, tr->data_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302744 binder_user_error("%d:%d got transaction with invalid data ptr\n",
2745 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002746 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002747 return_error_param = -EFAULT;
2748 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002749 goto err_copy_data_failed;
2750 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08002751 if (copy_from_user(offp, (const void __user *)(uintptr_t)
2752 tr->data.ptr.offsets, tr->offsets_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302753 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
2754 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002755 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002756 return_error_param = -EFAULT;
2757 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002758 goto err_copy_data_failed;
2759 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08002760 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
2761 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
2762 proc->pid, thread->pid, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002763 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002764 return_error_param = -EINVAL;
2765 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002766 goto err_bad_offset;
2767 }
Martijn Coenen79802402017-02-03 14:40:51 -08002768 if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) {
2769 binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
2770 proc->pid, thread->pid,
2771 (u64)extra_buffers_size);
2772 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002773 return_error_param = -EINVAL;
2774 return_error_line = __LINE__;
Martijn Coenen79802402017-02-03 14:40:51 -08002775 goto err_bad_offset;
2776 }
2777 off_end = (void *)off_start + tr->offsets_size;
2778 sg_bufp = (u8 *)(PTR_ALIGN(off_end, sizeof(void *)));
2779 sg_buf_end = sg_bufp + extra_buffers_size;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08002780 off_min = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002781 for (; offp < off_end; offp++) {
Martijn Coenenfeba3902017-02-03 14:40:45 -08002782 struct binder_object_header *hdr;
2783 size_t object_size = binder_validate_object(t->buffer, *offp);
Seunghun Lee10f62862014-05-01 01:30:23 +09002784
Martijn Coenenfeba3902017-02-03 14:40:45 -08002785 if (object_size == 0 || *offp < off_min) {
2786 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 -08002787 proc->pid, thread->pid, (u64)*offp,
2788 (u64)off_min,
Martijn Coenenfeba3902017-02-03 14:40:45 -08002789 (u64)t->buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002790 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002791 return_error_param = -EINVAL;
2792 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002793 goto err_bad_offset;
2794 }
Martijn Coenenfeba3902017-02-03 14:40:45 -08002795
2796 hdr = (struct binder_object_header *)(t->buffer->data + *offp);
2797 off_min = *offp + object_size;
2798 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002799 case BINDER_TYPE_BINDER:
2800 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08002801 struct flat_binder_object *fp;
Seunghun Lee10f62862014-05-01 01:30:23 +09002802
Martijn Coenenfeba3902017-02-03 14:40:45 -08002803 fp = to_flat_binder_object(hdr);
Martijn Coenena056af42017-02-03 14:40:49 -08002804 ret = binder_translate_binder(fp, t, thread);
2805 if (ret < 0) {
Christian Engelmayer7d420432014-05-07 21:44:53 +02002806 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002807 return_error_param = ret;
2808 return_error_line = __LINE__;
Martijn Coenena056af42017-02-03 14:40:49 -08002809 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002810 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002811 } break;
2812 case BINDER_TYPE_HANDLE:
2813 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08002814 struct flat_binder_object *fp;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02002815
Martijn Coenenfeba3902017-02-03 14:40:45 -08002816 fp = to_flat_binder_object(hdr);
Martijn Coenena056af42017-02-03 14:40:49 -08002817 ret = binder_translate_handle(fp, t, thread);
2818 if (ret < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002819 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002820 return_error_param = ret;
2821 return_error_line = __LINE__;
Martijn Coenena056af42017-02-03 14:40:49 -08002822 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002823 }
2824 } break;
2825
2826 case BINDER_TYPE_FD: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08002827 struct binder_fd_object *fp = to_binder_fd_object(hdr);
Martijn Coenena056af42017-02-03 14:40:49 -08002828 int target_fd = binder_translate_fd(fp->fd, t, thread,
2829 in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002830
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002831 if (target_fd < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002832 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002833 return_error_param = target_fd;
2834 return_error_line = __LINE__;
Martijn Coenena056af42017-02-03 14:40:49 -08002835 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002836 }
Martijn Coenenfeba3902017-02-03 14:40:45 -08002837 fp->pad_binder = 0;
2838 fp->fd = target_fd;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002839 } break;
Martijn Coenendef95c72017-02-03 14:40:52 -08002840 case BINDER_TYPE_FDA: {
2841 struct binder_fd_array_object *fda =
2842 to_binder_fd_array_object(hdr);
2843 struct binder_buffer_object *parent =
2844 binder_validate_ptr(t->buffer, fda->parent,
2845 off_start,
2846 offp - off_start);
2847 if (!parent) {
2848 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
2849 proc->pid, thread->pid);
2850 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002851 return_error_param = -EINVAL;
2852 return_error_line = __LINE__;
Martijn Coenendef95c72017-02-03 14:40:52 -08002853 goto err_bad_parent;
2854 }
2855 if (!binder_validate_fixup(t->buffer, off_start,
2856 parent, fda->parent_offset,
2857 last_fixup_obj,
2858 last_fixup_min_off)) {
2859 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
2860 proc->pid, thread->pid);
2861 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002862 return_error_param = -EINVAL;
2863 return_error_line = __LINE__;
Martijn Coenendef95c72017-02-03 14:40:52 -08002864 goto err_bad_parent;
2865 }
2866 ret = binder_translate_fd_array(fda, parent, t, thread,
2867 in_reply_to);
2868 if (ret < 0) {
2869 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002870 return_error_param = ret;
2871 return_error_line = __LINE__;
Martijn Coenendef95c72017-02-03 14:40:52 -08002872 goto err_translate_failed;
2873 }
2874 last_fixup_obj = parent;
2875 last_fixup_min_off =
2876 fda->parent_offset + sizeof(u32) * fda->num_fds;
2877 } break;
Martijn Coenen79802402017-02-03 14:40:51 -08002878 case BINDER_TYPE_PTR: {
2879 struct binder_buffer_object *bp =
2880 to_binder_buffer_object(hdr);
2881 size_t buf_left = sg_buf_end - sg_bufp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002882
Martijn Coenen79802402017-02-03 14:40:51 -08002883 if (bp->length > buf_left) {
2884 binder_user_error("%d:%d got transaction with too large buffer\n",
2885 proc->pid, thread->pid);
2886 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002887 return_error_param = -EINVAL;
2888 return_error_line = __LINE__;
Martijn Coenen79802402017-02-03 14:40:51 -08002889 goto err_bad_offset;
2890 }
2891 if (copy_from_user(sg_bufp,
2892 (const void __user *)(uintptr_t)
2893 bp->buffer, bp->length)) {
2894 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
2895 proc->pid, thread->pid);
Todd Kjos57ada2f2017-06-29 12:01:46 -07002896 return_error_param = -EFAULT;
Martijn Coenen79802402017-02-03 14:40:51 -08002897 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002898 return_error_line = __LINE__;
Martijn Coenen79802402017-02-03 14:40:51 -08002899 goto err_copy_data_failed;
2900 }
2901 /* Fixup buffer pointer to target proc address space */
2902 bp->buffer = (uintptr_t)sg_bufp +
Todd Kjos19c98722017-06-29 12:01:40 -07002903 binder_alloc_get_user_buffer_offset(
2904 &target_proc->alloc);
Martijn Coenen79802402017-02-03 14:40:51 -08002905 sg_bufp += ALIGN(bp->length, sizeof(u64));
2906
2907 ret = binder_fixup_parent(t, thread, bp, off_start,
2908 offp - off_start,
2909 last_fixup_obj,
2910 last_fixup_min_off);
2911 if (ret < 0) {
2912 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002913 return_error_param = ret;
2914 return_error_line = __LINE__;
Martijn Coenen79802402017-02-03 14:40:51 -08002915 goto err_translate_failed;
2916 }
2917 last_fixup_obj = bp;
2918 last_fixup_min_off = 0;
2919 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002920 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01002921 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
Martijn Coenenfeba3902017-02-03 14:40:45 -08002922 proc->pid, thread->pid, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002923 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002924 return_error_param = -EINVAL;
2925 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002926 goto err_bad_object_type;
2927 }
2928 }
Todd Kjosccae6f62017-06-29 12:01:48 -07002929 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
Todd Kjos72196392017-06-29 12:02:02 -07002930 binder_enqueue_work(proc, tcomplete, &thread->todo);
Todd Kjos673068e2017-06-29 12:02:03 -07002931 t->work.type = BINDER_WORK_TRANSACTION;
Todd Kjosccae6f62017-06-29 12:01:48 -07002932
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002933 if (reply) {
Martijn Coenen0b89d692017-06-29 12:02:06 -07002934 binder_inner_proc_lock(target_proc);
2935 if (target_thread->is_dead) {
2936 binder_inner_proc_unlock(target_proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07002937 goto err_dead_proc_or_thread;
Martijn Coenen0b89d692017-06-29 12:02:06 -07002938 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002939 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen0b89d692017-06-29 12:02:06 -07002940 binder_pop_transaction_ilocked(target_thread, in_reply_to);
2941 binder_enqueue_work_ilocked(&t->work, target_list);
2942 binder_inner_proc_unlock(target_proc);
Todd Kjosb6d282c2017-06-29 12:01:54 -07002943 binder_free_transaction(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002944 } else if (!(t->flags & TF_ONE_WAY)) {
2945 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen0b89d692017-06-29 12:02:06 -07002946 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002947 t->need_reply = 1;
2948 t->from_parent = thread->transaction_stack;
2949 thread->transaction_stack = t;
Martijn Coenen0b89d692017-06-29 12:02:06 -07002950 binder_inner_proc_unlock(proc);
2951 binder_inner_proc_lock(target_proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07002952 if (target_proc->is_dead ||
2953 (target_thread && target_thread->is_dead)) {
Martijn Coenen0b89d692017-06-29 12:02:06 -07002954 binder_inner_proc_unlock(target_proc);
2955 binder_inner_proc_lock(proc);
2956 binder_pop_transaction_ilocked(thread, t);
2957 binder_inner_proc_unlock(proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07002958 goto err_dead_proc_or_thread;
2959 }
Martijn Coenen0b89d692017-06-29 12:02:06 -07002960 binder_enqueue_work_ilocked(&t->work, target_list);
2961 binder_inner_proc_unlock(target_proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002962 } else {
2963 BUG_ON(target_node == NULL);
2964 BUG_ON(t->buffer->async_transaction != 1);
Todd Kjos673068e2017-06-29 12:02:03 -07002965 binder_node_lock(target_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002966 if (target_node->has_async_transaction) {
2967 target_list = &target_node->async_todo;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02002968 wakeup_for_proc_work = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002969 } else
2970 target_node->has_async_transaction = 1;
Todd Kjos673068e2017-06-29 12:02:03 -07002971 /*
2972 * Test/set of has_async_transaction
2973 * must be atomic with enqueue on
2974 * async_todo
2975 */
Martijn Coenen0b89d692017-06-29 12:02:06 -07002976 binder_inner_proc_lock(target_proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07002977 if (target_proc->is_dead ||
Todd Kjos673068e2017-06-29 12:02:03 -07002978 (target_thread && target_thread->is_dead)) {
Martijn Coenen0b89d692017-06-29 12:02:06 -07002979 binder_inner_proc_unlock(target_proc);
Todd Kjos673068e2017-06-29 12:02:03 -07002980 binder_node_unlock(target_node);
Todd Kjos7a4408c2017-06-29 12:01:57 -07002981 goto err_dead_proc_or_thread;
Todd Kjos673068e2017-06-29 12:02:03 -07002982 }
Martijn Coenen0b89d692017-06-29 12:02:06 -07002983 binder_enqueue_work_ilocked(&t->work, target_list);
2984 binder_inner_proc_unlock(target_proc);
Todd Kjos673068e2017-06-29 12:02:03 -07002985 binder_node_unlock(target_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002986 }
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02002987 if (target_thread) {
2988 wake_up_interruptible_sync(&target_thread->wait);
2989 } else if (wakeup_for_proc_work) {
2990 binder_inner_proc_lock(target_proc);
2991 binder_wakeup_proc_ilocked(target_proc,
2992 !(tr->flags & TF_ONE_WAY));
2993 binder_inner_proc_unlock(target_proc);
Riley Andrews00b40d62017-06-29 12:01:37 -07002994 }
Todd Kjos7a4408c2017-06-29 12:01:57 -07002995 if (target_thread)
2996 binder_thread_dec_tmpref(target_thread);
2997 binder_proc_dec_tmpref(target_proc);
Todd Kjosd99c7332017-06-29 12:01:53 -07002998 /*
2999 * write barrier to synchronize with initialization
3000 * of log entry
3001 */
3002 smp_wmb();
3003 WRITE_ONCE(e->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003004 return;
3005
Todd Kjos7a4408c2017-06-29 12:01:57 -07003006err_dead_proc_or_thread:
3007 return_error = BR_DEAD_REPLY;
3008 return_error_line = __LINE__;
Martijn Coenena056af42017-02-03 14:40:49 -08003009err_translate_failed:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003010err_bad_object_type:
3011err_bad_offset:
Martijn Coenendef95c72017-02-03 14:40:52 -08003012err_bad_parent:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003013err_copy_data_failed:
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003014 trace_binder_transaction_failed_buffer_release(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003015 binder_transaction_buffer_release(target_proc, t->buffer, offp);
Todd Kjoseb349832017-06-29 12:01:56 -07003016 target_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003017 t->buffer->transaction = NULL;
Todd Kjos19c98722017-06-29 12:01:40 -07003018 binder_alloc_free_buf(&target_proc->alloc, t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003019err_binder_alloc_buf_failed:
3020 kfree(tcomplete);
3021 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3022err_alloc_tcomplete_failed:
3023 kfree(t);
3024 binder_stats_deleted(BINDER_STAT_TRANSACTION);
3025err_alloc_t_failed:
3026err_bad_call_stack:
3027err_empty_call_stack:
3028err_dead_binder:
3029err_invalid_target_handle:
3030err_no_context_mgr_node:
Todd Kjos7a4408c2017-06-29 12:01:57 -07003031 if (target_thread)
3032 binder_thread_dec_tmpref(target_thread);
3033 if (target_proc)
3034 binder_proc_dec_tmpref(target_proc);
Todd Kjoseb349832017-06-29 12:01:56 -07003035 if (target_node)
3036 binder_dec_node(target_node, 1, 0);
3037
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003038 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Todd Kjos57ada2f2017-06-29 12:01:46 -07003039 "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
3040 proc->pid, thread->pid, return_error, return_error_param,
3041 (u64)tr->data_size, (u64)tr->offsets_size,
3042 return_error_line);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003043
3044 {
3045 struct binder_transaction_log_entry *fe;
Seunghun Lee10f62862014-05-01 01:30:23 +09003046
Todd Kjos57ada2f2017-06-29 12:01:46 -07003047 e->return_error = return_error;
3048 e->return_error_param = return_error_param;
3049 e->return_error_line = return_error_line;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003050 fe = binder_transaction_log_add(&binder_transaction_log_failed);
3051 *fe = *e;
Todd Kjosd99c7332017-06-29 12:01:53 -07003052 /*
3053 * write barrier to synchronize with initialization
3054 * of log entry
3055 */
3056 smp_wmb();
3057 WRITE_ONCE(e->debug_id_done, t_debug_id);
3058 WRITE_ONCE(fe->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003059 }
3060
Todd Kjos26549d12017-06-29 12:01:55 -07003061 BUG_ON(thread->return_error.cmd != BR_OK);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003062 if (in_reply_to) {
Todd Kjos26549d12017-06-29 12:01:55 -07003063 thread->return_error.cmd = BR_TRANSACTION_COMPLETE;
Todd Kjos72196392017-06-29 12:02:02 -07003064 binder_enqueue_work(thread->proc,
3065 &thread->return_error.work,
3066 &thread->todo);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003067 binder_send_failed_reply(in_reply_to, return_error);
Todd Kjos26549d12017-06-29 12:01:55 -07003068 } else {
3069 thread->return_error.cmd = return_error;
Todd Kjos72196392017-06-29 12:02:02 -07003070 binder_enqueue_work(thread->proc,
3071 &thread->return_error.work,
3072 &thread->todo);
Todd Kjos26549d12017-06-29 12:01:55 -07003073 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003074}
3075
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02003076static int binder_thread_write(struct binder_proc *proc,
3077 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003078 binder_uintptr_t binder_buffer, size_t size,
3079 binder_size_t *consumed)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003080{
3081 uint32_t cmd;
Martijn Coenen342e5c92017-02-03 14:40:46 -08003082 struct binder_context *context = proc->context;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003083 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003084 void __user *ptr = buffer + *consumed;
3085 void __user *end = buffer + size;
3086
Todd Kjos26549d12017-06-29 12:01:55 -07003087 while (ptr < end && thread->return_error.cmd == BR_OK) {
Todd Kjos372e3142017-06-29 12:01:58 -07003088 int ret;
3089
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003090 if (get_user(cmd, (uint32_t __user *)ptr))
3091 return -EFAULT;
3092 ptr += sizeof(uint32_t);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003093 trace_binder_command(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003094 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07003095 atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]);
3096 atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]);
3097 atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003098 }
3099 switch (cmd) {
3100 case BC_INCREFS:
3101 case BC_ACQUIRE:
3102 case BC_RELEASE:
3103 case BC_DECREFS: {
3104 uint32_t target;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003105 const char *debug_string;
Todd Kjos372e3142017-06-29 12:01:58 -07003106 bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE;
3107 bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE;
3108 struct binder_ref_data rdata;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003109
3110 if (get_user(target, (uint32_t __user *)ptr))
3111 return -EFAULT;
Todd Kjosc44b1232017-06-29 12:01:43 -07003112
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003113 ptr += sizeof(uint32_t);
Todd Kjos372e3142017-06-29 12:01:58 -07003114 ret = -1;
3115 if (increment && !target) {
Todd Kjosc44b1232017-06-29 12:01:43 -07003116 struct binder_node *ctx_mgr_node;
Todd Kjosc44b1232017-06-29 12:01:43 -07003117 mutex_lock(&context->context_mgr_node_lock);
3118 ctx_mgr_node = context->binder_context_mgr_node;
Todd Kjos372e3142017-06-29 12:01:58 -07003119 if (ctx_mgr_node)
3120 ret = binder_inc_ref_for_node(
3121 proc, ctx_mgr_node,
3122 strong, NULL, &rdata);
Todd Kjosc44b1232017-06-29 12:01:43 -07003123 mutex_unlock(&context->context_mgr_node_lock);
3124 }
Todd Kjos372e3142017-06-29 12:01:58 -07003125 if (ret)
3126 ret = binder_update_ref_for_handle(
3127 proc, target, increment, strong,
3128 &rdata);
3129 if (!ret && rdata.desc != target) {
3130 binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
3131 proc->pid, thread->pid,
3132 target, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003133 }
3134 switch (cmd) {
3135 case BC_INCREFS:
3136 debug_string = "IncRefs";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003137 break;
3138 case BC_ACQUIRE:
3139 debug_string = "Acquire";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003140 break;
3141 case BC_RELEASE:
3142 debug_string = "Release";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003143 break;
3144 case BC_DECREFS:
3145 default:
3146 debug_string = "DecRefs";
Todd Kjos372e3142017-06-29 12:01:58 -07003147 break;
3148 }
3149 if (ret) {
3150 binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
3151 proc->pid, thread->pid, debug_string,
3152 strong, target, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003153 break;
3154 }
3155 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjos372e3142017-06-29 12:01:58 -07003156 "%d:%d %s ref %d desc %d s %d w %d\n",
3157 proc->pid, thread->pid, debug_string,
3158 rdata.debug_id, rdata.desc, rdata.strong,
3159 rdata.weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003160 break;
3161 }
3162 case BC_INCREFS_DONE:
3163 case BC_ACQUIRE_DONE: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003164 binder_uintptr_t node_ptr;
3165 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003166 struct binder_node *node;
Todd Kjos673068e2017-06-29 12:02:03 -07003167 bool free_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003168
Arve Hjønnevågda498892014-02-21 14:40:26 -08003169 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003170 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003171 ptr += sizeof(binder_uintptr_t);
3172 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003173 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003174 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003175 node = binder_get_node(proc, node_ptr);
3176 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003177 binder_user_error("%d:%d %s u%016llx no match\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003178 proc->pid, thread->pid,
3179 cmd == BC_INCREFS_DONE ?
3180 "BC_INCREFS_DONE" :
3181 "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003182 (u64)node_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003183 break;
3184 }
3185 if (cookie != node->cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003186 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003187 proc->pid, thread->pid,
3188 cmd == BC_INCREFS_DONE ?
3189 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003190 (u64)node_ptr, node->debug_id,
3191 (u64)cookie, (u64)node->cookie);
Todd Kjosadc18842017-06-29 12:01:59 -07003192 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003193 break;
3194 }
Todd Kjos673068e2017-06-29 12:02:03 -07003195 binder_node_inner_lock(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003196 if (cmd == BC_ACQUIRE_DONE) {
3197 if (node->pending_strong_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303198 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003199 proc->pid, thread->pid,
3200 node->debug_id);
Todd Kjos673068e2017-06-29 12:02:03 -07003201 binder_node_inner_unlock(node);
Todd Kjosadc18842017-06-29 12:01:59 -07003202 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003203 break;
3204 }
3205 node->pending_strong_ref = 0;
3206 } else {
3207 if (node->pending_weak_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303208 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003209 proc->pid, thread->pid,
3210 node->debug_id);
Todd Kjos673068e2017-06-29 12:02:03 -07003211 binder_node_inner_unlock(node);
Todd Kjosadc18842017-06-29 12:01:59 -07003212 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003213 break;
3214 }
3215 node->pending_weak_ref = 0;
3216 }
Todd Kjos673068e2017-06-29 12:02:03 -07003217 free_node = binder_dec_node_nilocked(node,
3218 cmd == BC_ACQUIRE_DONE, 0);
3219 WARN_ON(free_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003220 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjosadc18842017-06-29 12:01:59 -07003221 "%d:%d %s node %d ls %d lw %d tr %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003222 proc->pid, thread->pid,
3223 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Todd Kjosadc18842017-06-29 12:01:59 -07003224 node->debug_id, node->local_strong_refs,
3225 node->local_weak_refs, node->tmp_refs);
Todd Kjos673068e2017-06-29 12:02:03 -07003226 binder_node_inner_unlock(node);
Todd Kjosadc18842017-06-29 12:01:59 -07003227 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003228 break;
3229 }
3230 case BC_ATTEMPT_ACQUIRE:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303231 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003232 return -EINVAL;
3233 case BC_ACQUIRE_RESULT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303234 pr_err("BC_ACQUIRE_RESULT not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003235 return -EINVAL;
3236
3237 case BC_FREE_BUFFER: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003238 binder_uintptr_t data_ptr;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003239 struct binder_buffer *buffer;
3240
Arve Hjønnevågda498892014-02-21 14:40:26 -08003241 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003242 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003243 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003244
Todd Kjos53d311cf2017-06-29 12:01:51 -07003245 buffer = binder_alloc_prepare_to_free(&proc->alloc,
3246 data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003247 if (buffer == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003248 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
3249 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003250 break;
3251 }
3252 if (!buffer->allow_user_free) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003253 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
3254 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003255 break;
3256 }
3257 binder_debug(BINDER_DEBUG_FREE_BUFFER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003258 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
3259 proc->pid, thread->pid, (u64)data_ptr,
3260 buffer->debug_id,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003261 buffer->transaction ? "active" : "finished");
3262
3263 if (buffer->transaction) {
3264 buffer->transaction->buffer = NULL;
3265 buffer->transaction = NULL;
3266 }
3267 if (buffer->async_transaction && buffer->target_node) {
Todd Kjos72196392017-06-29 12:02:02 -07003268 struct binder_node *buf_node;
3269 struct binder_work *w;
3270
3271 buf_node = buffer->target_node;
Todd Kjos673068e2017-06-29 12:02:03 -07003272 binder_node_inner_lock(buf_node);
Todd Kjos72196392017-06-29 12:02:02 -07003273 BUG_ON(!buf_node->has_async_transaction);
3274 BUG_ON(buf_node->proc != proc);
Todd Kjos72196392017-06-29 12:02:02 -07003275 w = binder_dequeue_work_head_ilocked(
3276 &buf_node->async_todo);
3277 if (!w)
3278 buf_node->has_async_transaction = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003279 else
Todd Kjos72196392017-06-29 12:02:02 -07003280 binder_enqueue_work_ilocked(
3281 w, &thread->todo);
Todd Kjos673068e2017-06-29 12:02:03 -07003282 binder_node_inner_unlock(buf_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003283 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003284 trace_binder_transaction_buffer_release(buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003285 binder_transaction_buffer_release(proc, buffer, NULL);
Todd Kjos19c98722017-06-29 12:01:40 -07003286 binder_alloc_free_buf(&proc->alloc, buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003287 break;
3288 }
3289
Martijn Coenen79802402017-02-03 14:40:51 -08003290 case BC_TRANSACTION_SG:
3291 case BC_REPLY_SG: {
3292 struct binder_transaction_data_sg tr;
3293
3294 if (copy_from_user(&tr, ptr, sizeof(tr)))
3295 return -EFAULT;
3296 ptr += sizeof(tr);
3297 binder_transaction(proc, thread, &tr.transaction_data,
3298 cmd == BC_REPLY_SG, tr.buffers_size);
3299 break;
3300 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003301 case BC_TRANSACTION:
3302 case BC_REPLY: {
3303 struct binder_transaction_data tr;
3304
3305 if (copy_from_user(&tr, ptr, sizeof(tr)))
3306 return -EFAULT;
3307 ptr += sizeof(tr);
Martijn Coenen4bfac802017-02-03 14:40:50 -08003308 binder_transaction(proc, thread, &tr,
3309 cmd == BC_REPLY, 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003310 break;
3311 }
3312
3313 case BC_REGISTER_LOOPER:
3314 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303315 "%d:%d BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003316 proc->pid, thread->pid);
Todd Kjosb3e68612017-06-29 12:02:07 -07003317 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003318 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
3319 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303320 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003321 proc->pid, thread->pid);
3322 } else if (proc->requested_threads == 0) {
3323 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303324 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003325 proc->pid, thread->pid);
3326 } else {
3327 proc->requested_threads--;
3328 proc->requested_threads_started++;
3329 }
3330 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
Todd Kjosb3e68612017-06-29 12:02:07 -07003331 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003332 break;
3333 case BC_ENTER_LOOPER:
3334 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303335 "%d:%d BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003336 proc->pid, thread->pid);
3337 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
3338 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303339 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003340 proc->pid, thread->pid);
3341 }
3342 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
3343 break;
3344 case BC_EXIT_LOOPER:
3345 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303346 "%d:%d BC_EXIT_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003347 proc->pid, thread->pid);
3348 thread->looper |= BINDER_LOOPER_STATE_EXITED;
3349 break;
3350
3351 case BC_REQUEST_DEATH_NOTIFICATION:
3352 case BC_CLEAR_DEATH_NOTIFICATION: {
3353 uint32_t target;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003354 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003355 struct binder_ref *ref;
Todd Kjos2c1838d2017-06-29 12:02:08 -07003356 struct binder_ref_death *death = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003357
3358 if (get_user(target, (uint32_t __user *)ptr))
3359 return -EFAULT;
3360 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08003361 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003362 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003363 ptr += sizeof(binder_uintptr_t);
Todd Kjos2c1838d2017-06-29 12:02:08 -07003364 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3365 /*
3366 * Allocate memory for death notification
3367 * before taking lock
3368 */
3369 death = kzalloc(sizeof(*death), GFP_KERNEL);
3370 if (death == NULL) {
3371 WARN_ON(thread->return_error.cmd !=
3372 BR_OK);
3373 thread->return_error.cmd = BR_ERROR;
3374 binder_enqueue_work(
3375 thread->proc,
3376 &thread->return_error.work,
3377 &thread->todo);
3378 binder_debug(
3379 BINDER_DEBUG_FAILED_TRANSACTION,
3380 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
3381 proc->pid, thread->pid);
3382 break;
3383 }
3384 }
3385 binder_proc_lock(proc);
3386 ref = binder_get_ref_olocked(proc, target, false);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003387 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303388 binder_user_error("%d:%d %s invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003389 proc->pid, thread->pid,
3390 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3391 "BC_REQUEST_DEATH_NOTIFICATION" :
3392 "BC_CLEAR_DEATH_NOTIFICATION",
3393 target);
Todd Kjos2c1838d2017-06-29 12:02:08 -07003394 binder_proc_unlock(proc);
3395 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003396 break;
3397 }
3398
3399 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003400 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003401 proc->pid, thread->pid,
3402 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3403 "BC_REQUEST_DEATH_NOTIFICATION" :
3404 "BC_CLEAR_DEATH_NOTIFICATION",
Todd Kjos372e3142017-06-29 12:01:58 -07003405 (u64)cookie, ref->data.debug_id,
3406 ref->data.desc, ref->data.strong,
3407 ref->data.weak, ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003408
Martijn Coenenab51ec62017-06-29 12:02:10 -07003409 binder_node_lock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003410 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3411 if (ref->death) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303412 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003413 proc->pid, thread->pid);
Martijn Coenenab51ec62017-06-29 12:02:10 -07003414 binder_node_unlock(ref->node);
Todd Kjos2c1838d2017-06-29 12:02:08 -07003415 binder_proc_unlock(proc);
3416 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003417 break;
3418 }
3419 binder_stats_created(BINDER_STAT_DEATH);
3420 INIT_LIST_HEAD(&death->work.entry);
3421 death->cookie = cookie;
3422 ref->death = death;
3423 if (ref->node->proc == NULL) {
3424 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
Todd Kjos72196392017-06-29 12:02:02 -07003425 if (thread->looper &
3426 (BINDER_LOOPER_STATE_REGISTERED |
3427 BINDER_LOOPER_STATE_ENTERED))
3428 binder_enqueue_work(
3429 proc,
3430 &ref->death->work,
3431 &thread->todo);
3432 else {
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02003433 binder_inner_proc_lock(proc);
3434 binder_enqueue_work_ilocked(
Todd Kjos72196392017-06-29 12:02:02 -07003435 &ref->death->work,
3436 &proc->todo);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02003437 binder_wakeup_proc_ilocked(
3438 proc,
3439 false);
3440 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003441 }
3442 }
3443 } else {
3444 if (ref->death == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303445 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003446 proc->pid, thread->pid);
Todd Kjos673068e2017-06-29 12:02:03 -07003447 binder_node_unlock(ref->node);
Todd Kjos2c1838d2017-06-29 12:02:08 -07003448 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003449 break;
3450 }
3451 death = ref->death;
3452 if (death->cookie != cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003453 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003454 proc->pid, thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003455 (u64)death->cookie,
3456 (u64)cookie);
Todd Kjos673068e2017-06-29 12:02:03 -07003457 binder_node_unlock(ref->node);
Todd Kjos2c1838d2017-06-29 12:02:08 -07003458 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003459 break;
3460 }
3461 ref->death = NULL;
Todd Kjos72196392017-06-29 12:02:02 -07003462 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003463 if (list_empty(&death->work.entry)) {
3464 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos72196392017-06-29 12:02:02 -07003465 if (thread->looper &
3466 (BINDER_LOOPER_STATE_REGISTERED |
3467 BINDER_LOOPER_STATE_ENTERED))
3468 binder_enqueue_work_ilocked(
3469 &death->work,
3470 &thread->todo);
3471 else {
3472 binder_enqueue_work_ilocked(
3473 &death->work,
3474 &proc->todo);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02003475 binder_wakeup_proc_ilocked(
3476 proc,
3477 false);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003478 }
3479 } else {
3480 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
3481 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
3482 }
Todd Kjos72196392017-06-29 12:02:02 -07003483 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003484 }
Martijn Coenenab51ec62017-06-29 12:02:10 -07003485 binder_node_unlock(ref->node);
Todd Kjos2c1838d2017-06-29 12:02:08 -07003486 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003487 } break;
3488 case BC_DEAD_BINDER_DONE: {
3489 struct binder_work *w;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003490 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003491 struct binder_ref_death *death = NULL;
Seunghun Lee10f62862014-05-01 01:30:23 +09003492
Arve Hjønnevågda498892014-02-21 14:40:26 -08003493 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003494 return -EFAULT;
3495
Lisa Du7a64cd82016-02-17 09:32:52 +08003496 ptr += sizeof(cookie);
Todd Kjos72196392017-06-29 12:02:02 -07003497 binder_inner_proc_lock(proc);
3498 list_for_each_entry(w, &proc->delivered_death,
3499 entry) {
3500 struct binder_ref_death *tmp_death =
3501 container_of(w,
3502 struct binder_ref_death,
3503 work);
Seunghun Lee10f62862014-05-01 01:30:23 +09003504
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003505 if (tmp_death->cookie == cookie) {
3506 death = tmp_death;
3507 break;
3508 }
3509 }
3510 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003511 "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n",
3512 proc->pid, thread->pid, (u64)cookie,
3513 death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003514 if (death == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003515 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
3516 proc->pid, thread->pid, (u64)cookie);
Todd Kjos72196392017-06-29 12:02:02 -07003517 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003518 break;
3519 }
Todd Kjos72196392017-06-29 12:02:02 -07003520 binder_dequeue_work_ilocked(&death->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003521 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
3522 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos72196392017-06-29 12:02:02 -07003523 if (thread->looper &
3524 (BINDER_LOOPER_STATE_REGISTERED |
3525 BINDER_LOOPER_STATE_ENTERED))
3526 binder_enqueue_work_ilocked(
3527 &death->work, &thread->todo);
3528 else {
3529 binder_enqueue_work_ilocked(
3530 &death->work,
3531 &proc->todo);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02003532 binder_wakeup_proc_ilocked(
3533 proc, false);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003534 }
3535 }
Todd Kjos72196392017-06-29 12:02:02 -07003536 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003537 } break;
3538
3539 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303540 pr_err("%d:%d unknown command %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003541 proc->pid, thread->pid, cmd);
3542 return -EINVAL;
3543 }
3544 *consumed = ptr - buffer;
3545 }
3546 return 0;
3547}
3548
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02003549static void binder_stat_br(struct binder_proc *proc,
3550 struct binder_thread *thread, uint32_t cmd)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003551{
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003552 trace_binder_return(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003553 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07003554 atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
3555 atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
3556 atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003557 }
3558}
3559
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003560static int binder_has_thread_work(struct binder_thread *thread)
3561{
Todd Kjos72196392017-06-29 12:02:02 -07003562 return !binder_worklist_empty(thread->proc, &thread->todo) ||
3563 thread->looper_need_return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003564}
3565
Todd Kjos26b47d82017-06-29 12:01:47 -07003566static int binder_put_node_cmd(struct binder_proc *proc,
3567 struct binder_thread *thread,
3568 void __user **ptrp,
3569 binder_uintptr_t node_ptr,
3570 binder_uintptr_t node_cookie,
3571 int node_debug_id,
3572 uint32_t cmd, const char *cmd_name)
3573{
3574 void __user *ptr = *ptrp;
3575
3576 if (put_user(cmd, (uint32_t __user *)ptr))
3577 return -EFAULT;
3578 ptr += sizeof(uint32_t);
3579
3580 if (put_user(node_ptr, (binder_uintptr_t __user *)ptr))
3581 return -EFAULT;
3582 ptr += sizeof(binder_uintptr_t);
3583
3584 if (put_user(node_cookie, (binder_uintptr_t __user *)ptr))
3585 return -EFAULT;
3586 ptr += sizeof(binder_uintptr_t);
3587
3588 binder_stat_br(proc, thread, cmd);
3589 binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n",
3590 proc->pid, thread->pid, cmd_name, node_debug_id,
3591 (u64)node_ptr, (u64)node_cookie);
3592
3593 *ptrp = ptr;
3594 return 0;
3595}
3596
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02003597static int binder_wait_for_work(struct binder_thread *thread,
3598 bool do_proc_work)
3599{
3600 DEFINE_WAIT(wait);
3601 struct binder_proc *proc = thread->proc;
3602 int ret = 0;
3603
3604 freezer_do_not_count();
3605 binder_inner_proc_lock(proc);
3606 for (;;) {
3607 prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE);
3608 if (binder_has_work_ilocked(thread, do_proc_work))
3609 break;
3610 if (do_proc_work)
3611 list_add(&thread->waiting_thread_node,
3612 &proc->waiting_threads);
3613 binder_inner_proc_unlock(proc);
3614 schedule();
3615 binder_inner_proc_lock(proc);
3616 list_del_init(&thread->waiting_thread_node);
3617 if (signal_pending(current)) {
3618 ret = -ERESTARTSYS;
3619 break;
3620 }
3621 }
3622 finish_wait(&thread->wait, &wait);
3623 binder_inner_proc_unlock(proc);
3624 freezer_count();
3625
3626 return ret;
3627}
3628
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003629static int binder_thread_read(struct binder_proc *proc,
3630 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003631 binder_uintptr_t binder_buffer, size_t size,
3632 binder_size_t *consumed, int non_block)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003633{
Arve Hjønnevågda498892014-02-21 14:40:26 -08003634 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003635 void __user *ptr = buffer + *consumed;
3636 void __user *end = buffer + size;
3637
3638 int ret = 0;
3639 int wait_for_proc_work;
3640
3641 if (*consumed == 0) {
3642 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
3643 return -EFAULT;
3644 ptr += sizeof(uint32_t);
3645 }
3646
3647retry:
Martijn Coenen0b89d692017-06-29 12:02:06 -07003648 binder_inner_proc_lock(proc);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02003649 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
Martijn Coenen0b89d692017-06-29 12:02:06 -07003650 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003651
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003652 thread->looper |= BINDER_LOOPER_STATE_WAITING;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003653
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003654 trace_binder_wait_for_work(wait_for_proc_work,
3655 !!thread->transaction_stack,
Todd Kjos72196392017-06-29 12:02:02 -07003656 !binder_worklist_empty(proc, &thread->todo));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003657 if (wait_for_proc_work) {
3658 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
3659 BINDER_LOOPER_STATE_ENTERED))) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303660 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 +09003661 proc->pid, thread->pid, thread->looper);
3662 wait_event_interruptible(binder_user_error_wait,
3663 binder_stop_on_user_error < 2);
3664 }
3665 binder_set_nice(proc->default_priority);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003666 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003667
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02003668 if (non_block) {
3669 if (!binder_has_work(thread, wait_for_proc_work))
3670 ret = -EAGAIN;
3671 } else {
3672 ret = binder_wait_for_work(thread, wait_for_proc_work);
3673 }
3674
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003675 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
3676
3677 if (ret)
3678 return ret;
3679
3680 while (1) {
3681 uint32_t cmd;
3682 struct binder_transaction_data tr;
Todd Kjos72196392017-06-29 12:02:02 -07003683 struct binder_work *w = NULL;
3684 struct list_head *list = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003685 struct binder_transaction *t = NULL;
Todd Kjos7a4408c2017-06-29 12:01:57 -07003686 struct binder_thread *t_from;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003687
Todd Kjosed297212017-06-29 12:02:01 -07003688 binder_inner_proc_lock(proc);
Todd Kjos72196392017-06-29 12:02:02 -07003689 if (!binder_worklist_empty_ilocked(&thread->todo))
3690 list = &thread->todo;
3691 else if (!binder_worklist_empty_ilocked(&proc->todo) &&
3692 wait_for_proc_work)
3693 list = &proc->todo;
3694 else {
3695 binder_inner_proc_unlock(proc);
3696
Dmitry Voytik395262a2014-09-08 18:16:34 +04003697 /* no data added */
Todd Kjos08dabce2017-06-29 12:01:49 -07003698 if (ptr - buffer == 4 && !thread->looper_need_return)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003699 goto retry;
3700 break;
3701 }
3702
Todd Kjosed297212017-06-29 12:02:01 -07003703 if (end - ptr < sizeof(tr) + 4) {
3704 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003705 break;
Todd Kjosed297212017-06-29 12:02:01 -07003706 }
Todd Kjos72196392017-06-29 12:02:02 -07003707 w = binder_dequeue_work_head_ilocked(list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003708
3709 switch (w->type) {
3710 case BINDER_WORK_TRANSACTION: {
Todd Kjosed297212017-06-29 12:02:01 -07003711 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003712 t = container_of(w, struct binder_transaction, work);
3713 } break;
Todd Kjos26549d12017-06-29 12:01:55 -07003714 case BINDER_WORK_RETURN_ERROR: {
3715 struct binder_error *e = container_of(
3716 w, struct binder_error, work);
3717
3718 WARN_ON(e->cmd == BR_OK);
Todd Kjosed297212017-06-29 12:02:01 -07003719 binder_inner_proc_unlock(proc);
Todd Kjos26549d12017-06-29 12:01:55 -07003720 if (put_user(e->cmd, (uint32_t __user *)ptr))
3721 return -EFAULT;
3722 e->cmd = BR_OK;
3723 ptr += sizeof(uint32_t);
3724
Todd Kjos4f9adc82017-08-08 15:48:36 -07003725 binder_stat_br(proc, thread, e->cmd);
Todd Kjos26549d12017-06-29 12:01:55 -07003726 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003727 case BINDER_WORK_TRANSACTION_COMPLETE: {
Todd Kjosed297212017-06-29 12:02:01 -07003728 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003729 cmd = BR_TRANSACTION_COMPLETE;
3730 if (put_user(cmd, (uint32_t __user *)ptr))
3731 return -EFAULT;
3732 ptr += sizeof(uint32_t);
3733
3734 binder_stat_br(proc, thread, cmd);
3735 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303736 "%d:%d BR_TRANSACTION_COMPLETE\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003737 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003738 kfree(w);
3739 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3740 } break;
3741 case BINDER_WORK_NODE: {
3742 struct binder_node *node = container_of(w, struct binder_node, work);
Todd Kjos26b47d82017-06-29 12:01:47 -07003743 int strong, weak;
3744 binder_uintptr_t node_ptr = node->ptr;
3745 binder_uintptr_t node_cookie = node->cookie;
3746 int node_debug_id = node->debug_id;
3747 int has_weak_ref;
3748 int has_strong_ref;
3749 void __user *orig_ptr = ptr;
Seunghun Lee10f62862014-05-01 01:30:23 +09003750
Todd Kjos26b47d82017-06-29 12:01:47 -07003751 BUG_ON(proc != node->proc);
3752 strong = node->internal_strong_refs ||
3753 node->local_strong_refs;
3754 weak = !hlist_empty(&node->refs) ||
Todd Kjosadc18842017-06-29 12:01:59 -07003755 node->local_weak_refs ||
3756 node->tmp_refs || strong;
Todd Kjos26b47d82017-06-29 12:01:47 -07003757 has_strong_ref = node->has_strong_ref;
3758 has_weak_ref = node->has_weak_ref;
3759
3760 if (weak && !has_weak_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003761 node->has_weak_ref = 1;
3762 node->pending_weak_ref = 1;
3763 node->local_weak_refs++;
Todd Kjos26b47d82017-06-29 12:01:47 -07003764 }
3765 if (strong && !has_strong_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003766 node->has_strong_ref = 1;
3767 node->pending_strong_ref = 1;
3768 node->local_strong_refs++;
Todd Kjos26b47d82017-06-29 12:01:47 -07003769 }
3770 if (!strong && has_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003771 node->has_strong_ref = 0;
Todd Kjos26b47d82017-06-29 12:01:47 -07003772 if (!weak && has_weak_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003773 node->has_weak_ref = 0;
Todd Kjos26b47d82017-06-29 12:01:47 -07003774 if (!weak && !strong) {
3775 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
3776 "%d:%d node %d u%016llx c%016llx deleted\n",
3777 proc->pid, thread->pid,
3778 node_debug_id,
3779 (u64)node_ptr,
3780 (u64)node_cookie);
3781 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjosed297212017-06-29 12:02:01 -07003782 binder_inner_proc_unlock(proc);
Todd Kjos673068e2017-06-29 12:02:03 -07003783 binder_node_lock(node);
3784 /*
3785 * Acquire the node lock before freeing the
3786 * node to serialize with other threads that
3787 * may have been holding the node lock while
3788 * decrementing this node (avoids race where
3789 * this thread frees while the other thread
3790 * is unlocking the node after the final
3791 * decrement)
3792 */
3793 binder_node_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07003794 binder_free_node(node);
3795 } else
3796 binder_inner_proc_unlock(proc);
3797
Todd Kjos26b47d82017-06-29 12:01:47 -07003798 if (weak && !has_weak_ref)
3799 ret = binder_put_node_cmd(
3800 proc, thread, &ptr, node_ptr,
3801 node_cookie, node_debug_id,
3802 BR_INCREFS, "BR_INCREFS");
3803 if (!ret && strong && !has_strong_ref)
3804 ret = binder_put_node_cmd(
3805 proc, thread, &ptr, node_ptr,
3806 node_cookie, node_debug_id,
3807 BR_ACQUIRE, "BR_ACQUIRE");
3808 if (!ret && !strong && has_strong_ref)
3809 ret = binder_put_node_cmd(
3810 proc, thread, &ptr, node_ptr,
3811 node_cookie, node_debug_id,
3812 BR_RELEASE, "BR_RELEASE");
3813 if (!ret && !weak && has_weak_ref)
3814 ret = binder_put_node_cmd(
3815 proc, thread, &ptr, node_ptr,
3816 node_cookie, node_debug_id,
3817 BR_DECREFS, "BR_DECREFS");
3818 if (orig_ptr == ptr)
3819 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
3820 "%d:%d node %d u%016llx c%016llx state unchanged\n",
3821 proc->pid, thread->pid,
3822 node_debug_id,
3823 (u64)node_ptr,
3824 (u64)node_cookie);
3825 if (ret)
3826 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003827 } break;
3828 case BINDER_WORK_DEAD_BINDER:
3829 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
3830 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
3831 struct binder_ref_death *death;
3832 uint32_t cmd;
Martijn Coenenab51ec62017-06-29 12:02:10 -07003833 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003834
3835 death = container_of(w, struct binder_ref_death, work);
3836 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
3837 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
3838 else
3839 cmd = BR_DEAD_BINDER;
Martijn Coenenab51ec62017-06-29 12:02:10 -07003840 cookie = death->cookie;
3841
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003842 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003843 "%d:%d %s %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003844 proc->pid, thread->pid,
3845 cmd == BR_DEAD_BINDER ?
3846 "BR_DEAD_BINDER" :
3847 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
Martijn Coenenab51ec62017-06-29 12:02:10 -07003848 (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003849 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
Martijn Coenenab51ec62017-06-29 12:02:10 -07003850 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003851 kfree(death);
3852 binder_stats_deleted(BINDER_STAT_DEATH);
Todd Kjosed297212017-06-29 12:02:01 -07003853 } else {
Todd Kjos72196392017-06-29 12:02:02 -07003854 binder_enqueue_work_ilocked(
3855 w, &proc->delivered_death);
Todd Kjosed297212017-06-29 12:02:01 -07003856 binder_inner_proc_unlock(proc);
3857 }
Martijn Coenenab51ec62017-06-29 12:02:10 -07003858 if (put_user(cmd, (uint32_t __user *)ptr))
3859 return -EFAULT;
3860 ptr += sizeof(uint32_t);
3861 if (put_user(cookie,
3862 (binder_uintptr_t __user *)ptr))
3863 return -EFAULT;
3864 ptr += sizeof(binder_uintptr_t);
3865 binder_stat_br(proc, thread, cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003866 if (cmd == BR_DEAD_BINDER)
3867 goto done; /* DEAD_BINDER notifications can cause transactions */
3868 } break;
3869 }
3870
3871 if (!t)
3872 continue;
3873
3874 BUG_ON(t->buffer == NULL);
3875 if (t->buffer->target_node) {
3876 struct binder_node *target_node = t->buffer->target_node;
Seunghun Lee10f62862014-05-01 01:30:23 +09003877
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003878 tr.target.ptr = target_node->ptr;
3879 tr.cookie = target_node->cookie;
3880 t->saved_priority = task_nice(current);
3881 if (t->priority < target_node->min_priority &&
3882 !(t->flags & TF_ONE_WAY))
3883 binder_set_nice(t->priority);
3884 else if (!(t->flags & TF_ONE_WAY) ||
3885 t->saved_priority > target_node->min_priority)
3886 binder_set_nice(target_node->min_priority);
3887 cmd = BR_TRANSACTION;
3888 } else {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003889 tr.target.ptr = 0;
3890 tr.cookie = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003891 cmd = BR_REPLY;
3892 }
3893 tr.code = t->code;
3894 tr.flags = t->flags;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -06003895 tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003896
Todd Kjos7a4408c2017-06-29 12:01:57 -07003897 t_from = binder_get_txn_from(t);
3898 if (t_from) {
3899 struct task_struct *sender = t_from->proc->tsk;
Seunghun Lee10f62862014-05-01 01:30:23 +09003900
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003901 tr.sender_pid = task_tgid_nr_ns(sender,
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003902 task_active_pid_ns(current));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003903 } else {
3904 tr.sender_pid = 0;
3905 }
3906
3907 tr.data_size = t->buffer->data_size;
3908 tr.offsets_size = t->buffer->offsets_size;
Todd Kjos19c98722017-06-29 12:01:40 -07003909 tr.data.ptr.buffer = (binder_uintptr_t)
3910 ((uintptr_t)t->buffer->data +
3911 binder_alloc_get_user_buffer_offset(&proc->alloc));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003912 tr.data.ptr.offsets = tr.data.ptr.buffer +
3913 ALIGN(t->buffer->data_size,
3914 sizeof(void *));
3915
Todd Kjos7a4408c2017-06-29 12:01:57 -07003916 if (put_user(cmd, (uint32_t __user *)ptr)) {
3917 if (t_from)
3918 binder_thread_dec_tmpref(t_from);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003919 return -EFAULT;
Todd Kjos7a4408c2017-06-29 12:01:57 -07003920 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003921 ptr += sizeof(uint32_t);
Todd Kjos7a4408c2017-06-29 12:01:57 -07003922 if (copy_to_user(ptr, &tr, sizeof(tr))) {
3923 if (t_from)
3924 binder_thread_dec_tmpref(t_from);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003925 return -EFAULT;
Todd Kjos7a4408c2017-06-29 12:01:57 -07003926 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003927 ptr += sizeof(tr);
3928
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003929 trace_binder_transaction_received(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003930 binder_stat_br(proc, thread, cmd);
3931 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003932 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003933 proc->pid, thread->pid,
3934 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
3935 "BR_REPLY",
Todd Kjos7a4408c2017-06-29 12:01:57 -07003936 t->debug_id, t_from ? t_from->proc->pid : 0,
3937 t_from ? t_from->pid : 0, cmd,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003938 t->buffer->data_size, t->buffer->offsets_size,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003939 (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003940
Todd Kjos7a4408c2017-06-29 12:01:57 -07003941 if (t_from)
3942 binder_thread_dec_tmpref(t_from);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003943 t->buffer->allow_user_free = 1;
3944 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
Martijn Coenen0b89d692017-06-29 12:02:06 -07003945 binder_inner_proc_lock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003946 t->to_parent = thread->transaction_stack;
3947 t->to_thread = thread;
3948 thread->transaction_stack = t;
Martijn Coenen0b89d692017-06-29 12:02:06 -07003949 binder_inner_proc_unlock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003950 } else {
Todd Kjosb6d282c2017-06-29 12:01:54 -07003951 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003952 }
3953 break;
3954 }
3955
3956done:
3957
3958 *consumed = ptr - buffer;
Todd Kjosb3e68612017-06-29 12:02:07 -07003959 binder_inner_proc_lock(proc);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02003960 if (proc->requested_threads == 0 &&
3961 list_empty(&thread->proc->waiting_threads) &&
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003962 proc->requested_threads_started < proc->max_threads &&
3963 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
3964 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
3965 /*spawn a new thread if we leave this out */) {
3966 proc->requested_threads++;
Todd Kjosb3e68612017-06-29 12:02:07 -07003967 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003968 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303969 "%d:%d BR_SPAWN_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003970 proc->pid, thread->pid);
3971 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
3972 return -EFAULT;
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07003973 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
Todd Kjosb3e68612017-06-29 12:02:07 -07003974 } else
3975 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003976 return 0;
3977}
3978
Todd Kjos72196392017-06-29 12:02:02 -07003979static void binder_release_work(struct binder_proc *proc,
3980 struct list_head *list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003981{
3982 struct binder_work *w;
Seunghun Lee10f62862014-05-01 01:30:23 +09003983
Todd Kjos72196392017-06-29 12:02:02 -07003984 while (1) {
3985 w = binder_dequeue_work_head(proc, list);
3986 if (!w)
3987 return;
3988
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003989 switch (w->type) {
3990 case BINDER_WORK_TRANSACTION: {
3991 struct binder_transaction *t;
3992
3993 t = container_of(w, struct binder_transaction, work);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003994 if (t->buffer->target_node &&
3995 !(t->flags & TF_ONE_WAY)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003996 binder_send_failed_reply(t, BR_DEAD_REPLY);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003997 } else {
3998 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303999 "undelivered transaction %d\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004000 t->debug_id);
Todd Kjosb6d282c2017-06-29 12:01:54 -07004001 binder_free_transaction(t);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004002 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004003 } break;
Todd Kjos26549d12017-06-29 12:01:55 -07004004 case BINDER_WORK_RETURN_ERROR: {
4005 struct binder_error *e = container_of(
4006 w, struct binder_error, work);
4007
4008 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4009 "undelivered TRANSACTION_ERROR: %u\n",
4010 e->cmd);
4011 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004012 case BINDER_WORK_TRANSACTION_COMPLETE: {
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004013 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304014 "undelivered TRANSACTION_COMPLETE\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004015 kfree(w);
4016 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4017 } break;
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004018 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4019 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4020 struct binder_ref_death *death;
4021
4022 death = container_of(w, struct binder_ref_death, work);
4023 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004024 "undelivered death notification, %016llx\n",
4025 (u64)death->cookie);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004026 kfree(death);
4027 binder_stats_deleted(BINDER_STAT_DEATH);
4028 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004029 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304030 pr_err("unexpected work type, %d, not freed\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004031 w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004032 break;
4033 }
4034 }
4035
4036}
4037
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004038static struct binder_thread *binder_get_thread_ilocked(
4039 struct binder_proc *proc, struct binder_thread *new_thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004040{
4041 struct binder_thread *thread = NULL;
4042 struct rb_node *parent = NULL;
4043 struct rb_node **p = &proc->threads.rb_node;
4044
4045 while (*p) {
4046 parent = *p;
4047 thread = rb_entry(parent, struct binder_thread, rb_node);
4048
4049 if (current->pid < thread->pid)
4050 p = &(*p)->rb_left;
4051 else if (current->pid > thread->pid)
4052 p = &(*p)->rb_right;
4053 else
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004054 return thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004055 }
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004056 if (!new_thread)
4057 return NULL;
4058 thread = new_thread;
4059 binder_stats_created(BINDER_STAT_THREAD);
4060 thread->proc = proc;
4061 thread->pid = current->pid;
4062 atomic_set(&thread->tmp_ref, 0);
4063 init_waitqueue_head(&thread->wait);
4064 INIT_LIST_HEAD(&thread->todo);
4065 rb_link_node(&thread->rb_node, parent, p);
4066 rb_insert_color(&thread->rb_node, &proc->threads);
4067 thread->looper_need_return = true;
4068 thread->return_error.work.type = BINDER_WORK_RETURN_ERROR;
4069 thread->return_error.cmd = BR_OK;
4070 thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR;
4071 thread->reply_error.cmd = BR_OK;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004072 INIT_LIST_HEAD(&new_thread->waiting_thread_node);
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004073 return thread;
4074}
4075
4076static struct binder_thread *binder_get_thread(struct binder_proc *proc)
4077{
4078 struct binder_thread *thread;
4079 struct binder_thread *new_thread;
4080
4081 binder_inner_proc_lock(proc);
4082 thread = binder_get_thread_ilocked(proc, NULL);
4083 binder_inner_proc_unlock(proc);
4084 if (!thread) {
4085 new_thread = kzalloc(sizeof(*thread), GFP_KERNEL);
4086 if (new_thread == NULL)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004087 return NULL;
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004088 binder_inner_proc_lock(proc);
4089 thread = binder_get_thread_ilocked(proc, new_thread);
4090 binder_inner_proc_unlock(proc);
4091 if (thread != new_thread)
4092 kfree(new_thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004093 }
4094 return thread;
4095}
4096
Todd Kjos7a4408c2017-06-29 12:01:57 -07004097static void binder_free_proc(struct binder_proc *proc)
4098{
4099 BUG_ON(!list_empty(&proc->todo));
4100 BUG_ON(!list_empty(&proc->delivered_death));
4101 binder_alloc_deferred_release(&proc->alloc);
4102 put_task_struct(proc->tsk);
4103 binder_stats_deleted(BINDER_STAT_PROC);
4104 kfree(proc);
4105}
4106
4107static void binder_free_thread(struct binder_thread *thread)
4108{
4109 BUG_ON(!list_empty(&thread->todo));
4110 binder_stats_deleted(BINDER_STAT_THREAD);
4111 binder_proc_dec_tmpref(thread->proc);
4112 kfree(thread);
4113}
4114
4115static int binder_thread_release(struct binder_proc *proc,
4116 struct binder_thread *thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004117{
4118 struct binder_transaction *t;
4119 struct binder_transaction *send_reply = NULL;
4120 int active_transactions = 0;
Todd Kjos7a4408c2017-06-29 12:01:57 -07004121 struct binder_transaction *last_t = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004122
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004123 binder_inner_proc_lock(thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07004124 /*
4125 * take a ref on the proc so it survives
4126 * after we remove this thread from proc->threads.
4127 * The corresponding dec is when we actually
4128 * free the thread in binder_free_thread()
4129 */
4130 proc->tmp_ref++;
4131 /*
4132 * take a ref on this thread to ensure it
4133 * survives while we are releasing it
4134 */
4135 atomic_inc(&thread->tmp_ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004136 rb_erase(&thread->rb_node, &proc->threads);
4137 t = thread->transaction_stack;
Todd Kjos7a4408c2017-06-29 12:01:57 -07004138 if (t) {
4139 spin_lock(&t->lock);
4140 if (t->to_thread == thread)
4141 send_reply = t;
4142 }
4143 thread->is_dead = true;
4144
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004145 while (t) {
Todd Kjos7a4408c2017-06-29 12:01:57 -07004146 last_t = t;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004147 active_transactions++;
4148 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304149 "release %d:%d transaction %d %s, still active\n",
4150 proc->pid, thread->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004151 t->debug_id,
4152 (t->to_thread == thread) ? "in" : "out");
4153
4154 if (t->to_thread == thread) {
4155 t->to_proc = NULL;
4156 t->to_thread = NULL;
4157 if (t->buffer) {
4158 t->buffer->transaction = NULL;
4159 t->buffer = NULL;
4160 }
4161 t = t->to_parent;
4162 } else if (t->from == thread) {
4163 t->from = NULL;
4164 t = t->from_parent;
4165 } else
4166 BUG();
Todd Kjos7a4408c2017-06-29 12:01:57 -07004167 spin_unlock(&last_t->lock);
4168 if (t)
4169 spin_lock(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004170 }
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004171 binder_inner_proc_unlock(thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07004172
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004173 if (send_reply)
4174 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
Todd Kjos72196392017-06-29 12:02:02 -07004175 binder_release_work(proc, &thread->todo);
Todd Kjos7a4408c2017-06-29 12:01:57 -07004176 binder_thread_dec_tmpref(thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004177 return active_transactions;
4178}
4179
4180static unsigned int binder_poll(struct file *filp,
4181 struct poll_table_struct *wait)
4182{
4183 struct binder_proc *proc = filp->private_data;
4184 struct binder_thread *thread = NULL;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004185 bool wait_for_proc_work;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004186
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004187 thread = binder_get_thread(proc);
4188
Martijn Coenen0b89d692017-06-29 12:02:06 -07004189 binder_inner_proc_lock(thread->proc);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004190 thread->looper |= BINDER_LOOPER_STATE_POLL;
4191 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
4192
Martijn Coenen0b89d692017-06-29 12:02:06 -07004193 binder_inner_proc_unlock(thread->proc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004194
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004195 if (binder_has_work(thread, wait_for_proc_work))
4196 return POLLIN;
4197
4198 poll_wait(filp, &thread->wait, wait);
4199
4200 if (binder_has_thread_work(thread))
4201 return POLLIN;
4202
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004203 return 0;
4204}
4205
Tair Rzayev78260ac2014-06-03 22:27:21 +03004206static int binder_ioctl_write_read(struct file *filp,
4207 unsigned int cmd, unsigned long arg,
4208 struct binder_thread *thread)
4209{
4210 int ret = 0;
4211 struct binder_proc *proc = filp->private_data;
4212 unsigned int size = _IOC_SIZE(cmd);
4213 void __user *ubuf = (void __user *)arg;
4214 struct binder_write_read bwr;
4215
4216 if (size != sizeof(struct binder_write_read)) {
4217 ret = -EINVAL;
4218 goto out;
4219 }
4220 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
4221 ret = -EFAULT;
4222 goto out;
4223 }
4224 binder_debug(BINDER_DEBUG_READ_WRITE,
4225 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
4226 proc->pid, thread->pid,
4227 (u64)bwr.write_size, (u64)bwr.write_buffer,
4228 (u64)bwr.read_size, (u64)bwr.read_buffer);
4229
4230 if (bwr.write_size > 0) {
4231 ret = binder_thread_write(proc, thread,
4232 bwr.write_buffer,
4233 bwr.write_size,
4234 &bwr.write_consumed);
4235 trace_binder_write_done(ret);
4236 if (ret < 0) {
4237 bwr.read_consumed = 0;
4238 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4239 ret = -EFAULT;
4240 goto out;
4241 }
4242 }
4243 if (bwr.read_size > 0) {
4244 ret = binder_thread_read(proc, thread, bwr.read_buffer,
4245 bwr.read_size,
4246 &bwr.read_consumed,
4247 filp->f_flags & O_NONBLOCK);
4248 trace_binder_read_done(ret);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004249 binder_inner_proc_lock(proc);
4250 if (!binder_worklist_empty_ilocked(&proc->todo))
4251 binder_wakeup_proc_ilocked(proc, false);
4252 binder_inner_proc_unlock(proc);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004253 if (ret < 0) {
4254 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4255 ret = -EFAULT;
4256 goto out;
4257 }
4258 }
4259 binder_debug(BINDER_DEBUG_READ_WRITE,
4260 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
4261 proc->pid, thread->pid,
4262 (u64)bwr.write_consumed, (u64)bwr.write_size,
4263 (u64)bwr.read_consumed, (u64)bwr.read_size);
4264 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
4265 ret = -EFAULT;
4266 goto out;
4267 }
4268out:
4269 return ret;
4270}
4271
4272static int binder_ioctl_set_ctx_mgr(struct file *filp)
4273{
4274 int ret = 0;
4275 struct binder_proc *proc = filp->private_data;
Martijn Coenen342e5c92017-02-03 14:40:46 -08004276 struct binder_context *context = proc->context;
Todd Kjosc44b1232017-06-29 12:01:43 -07004277 struct binder_node *new_node;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004278 kuid_t curr_euid = current_euid();
4279
Todd Kjosc44b1232017-06-29 12:01:43 -07004280 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen342e5c92017-02-03 14:40:46 -08004281 if (context->binder_context_mgr_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004282 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
4283 ret = -EBUSY;
4284 goto out;
4285 }
Stephen Smalley79af7302015-01-21 10:54:10 -05004286 ret = security_binder_set_context_mgr(proc->tsk);
4287 if (ret < 0)
4288 goto out;
Martijn Coenen342e5c92017-02-03 14:40:46 -08004289 if (uid_valid(context->binder_context_mgr_uid)) {
4290 if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004291 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
4292 from_kuid(&init_user_ns, curr_euid),
4293 from_kuid(&init_user_ns,
Martijn Coenen342e5c92017-02-03 14:40:46 -08004294 context->binder_context_mgr_uid));
Tair Rzayev78260ac2014-06-03 22:27:21 +03004295 ret = -EPERM;
4296 goto out;
4297 }
4298 } else {
Martijn Coenen342e5c92017-02-03 14:40:46 -08004299 context->binder_context_mgr_uid = curr_euid;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004300 }
Todd Kjos673068e2017-06-29 12:02:03 -07004301 new_node = binder_new_node(proc, NULL);
Todd Kjosc44b1232017-06-29 12:01:43 -07004302 if (!new_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004303 ret = -ENOMEM;
4304 goto out;
4305 }
Todd Kjos673068e2017-06-29 12:02:03 -07004306 binder_node_lock(new_node);
Todd Kjosc44b1232017-06-29 12:01:43 -07004307 new_node->local_weak_refs++;
4308 new_node->local_strong_refs++;
4309 new_node->has_strong_ref = 1;
4310 new_node->has_weak_ref = 1;
4311 context->binder_context_mgr_node = new_node;
Todd Kjos673068e2017-06-29 12:02:03 -07004312 binder_node_unlock(new_node);
Todd Kjosadc18842017-06-29 12:01:59 -07004313 binder_put_node(new_node);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004314out:
Todd Kjosc44b1232017-06-29 12:01:43 -07004315 mutex_unlock(&context->context_mgr_node_lock);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004316 return ret;
4317}
4318
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004319static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4320{
4321 int ret;
4322 struct binder_proc *proc = filp->private_data;
4323 struct binder_thread *thread;
4324 unsigned int size = _IOC_SIZE(cmd);
4325 void __user *ubuf = (void __user *)arg;
4326
Tair Rzayev78260ac2014-06-03 22:27:21 +03004327 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
4328 proc->pid, current->pid, cmd, arg);*/
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004329
Sherry Yang4175e2b2017-08-23 08:46:40 -07004330 binder_selftest_alloc(&proc->alloc);
4331
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004332 trace_binder_ioctl(cmd, arg);
4333
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004334 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4335 if (ret)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004336 goto err_unlocked;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004337
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004338 thread = binder_get_thread(proc);
4339 if (thread == NULL) {
4340 ret = -ENOMEM;
4341 goto err;
4342 }
4343
4344 switch (cmd) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004345 case BINDER_WRITE_READ:
4346 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
4347 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004348 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004349 break;
Todd Kjosb3e68612017-06-29 12:02:07 -07004350 case BINDER_SET_MAX_THREADS: {
4351 int max_threads;
4352
4353 if (copy_from_user(&max_threads, ubuf,
4354 sizeof(max_threads))) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004355 ret = -EINVAL;
4356 goto err;
4357 }
Todd Kjosb3e68612017-06-29 12:02:07 -07004358 binder_inner_proc_lock(proc);
4359 proc->max_threads = max_threads;
4360 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004361 break;
Todd Kjosb3e68612017-06-29 12:02:07 -07004362 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004363 case BINDER_SET_CONTEXT_MGR:
Tair Rzayev78260ac2014-06-03 22:27:21 +03004364 ret = binder_ioctl_set_ctx_mgr(filp);
4365 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004366 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004367 break;
4368 case BINDER_THREAD_EXIT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304369 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004370 proc->pid, thread->pid);
Todd Kjos7a4408c2017-06-29 12:01:57 -07004371 binder_thread_release(proc, thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004372 thread = NULL;
4373 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02004374 case BINDER_VERSION: {
4375 struct binder_version __user *ver = ubuf;
4376
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004377 if (size != sizeof(struct binder_version)) {
4378 ret = -EINVAL;
4379 goto err;
4380 }
Mathieu Maret36c89c02014-04-15 12:03:05 +02004381 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
4382 &ver->protocol_version)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004383 ret = -EINVAL;
4384 goto err;
4385 }
4386 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02004387 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004388 default:
4389 ret = -EINVAL;
4390 goto err;
4391 }
4392 ret = 0;
4393err:
4394 if (thread)
Todd Kjos08dabce2017-06-29 12:01:49 -07004395 thread->looper_need_return = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004396 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4397 if (ret && ret != -ERESTARTSYS)
Anmol Sarma56b468f2012-10-30 22:35:43 +05304398 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 -07004399err_unlocked:
4400 trace_binder_ioctl_done(ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004401 return ret;
4402}
4403
4404static void binder_vma_open(struct vm_area_struct *vma)
4405{
4406 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004407
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004408 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304409 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004410 proc->pid, vma->vm_start, vma->vm_end,
4411 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4412 (unsigned long)pgprot_val(vma->vm_page_prot));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004413}
4414
4415static void binder_vma_close(struct vm_area_struct *vma)
4416{
4417 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004418
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004419 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304420 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004421 proc->pid, vma->vm_start, vma->vm_end,
4422 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4423 (unsigned long)pgprot_val(vma->vm_page_prot));
Todd Kjos19c98722017-06-29 12:01:40 -07004424 binder_alloc_vma_close(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004425 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
4426}
4427
Dave Jiang11bac802017-02-24 14:56:41 -08004428static int binder_vm_fault(struct vm_fault *vmf)
Vinayak Menonddac7d52014-06-02 18:17:59 +05304429{
4430 return VM_FAULT_SIGBUS;
4431}
4432
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07004433static const struct vm_operations_struct binder_vm_ops = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004434 .open = binder_vma_open,
4435 .close = binder_vma_close,
Vinayak Menonddac7d52014-06-02 18:17:59 +05304436 .fault = binder_vm_fault,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004437};
4438
Todd Kjos19c98722017-06-29 12:01:40 -07004439static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
4440{
4441 int ret;
4442 struct binder_proc *proc = filp->private_data;
4443 const char *failure_string;
4444
4445 if (proc->tsk != current->group_leader)
4446 return -EINVAL;
4447
4448 if ((vma->vm_end - vma->vm_start) > SZ_4M)
4449 vma->vm_end = vma->vm_start + SZ_4M;
4450
4451 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4452 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
4453 __func__, proc->pid, vma->vm_start, vma->vm_end,
4454 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4455 (unsigned long)pgprot_val(vma->vm_page_prot));
4456
4457 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
4458 ret = -EPERM;
4459 failure_string = "bad vm_flags";
4460 goto err_bad_arg;
4461 }
4462 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
4463 vma->vm_ops = &binder_vm_ops;
4464 vma->vm_private_data = proc;
4465
4466 ret = binder_alloc_mmap_handler(&proc->alloc, vma);
4467 if (ret)
4468 return ret;
4469 proc->files = get_files_struct(current);
4470 return 0;
4471
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004472err_bad_arg:
Sherwin Soltani258767f2012-06-26 02:00:30 -04004473 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004474 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
4475 return ret;
4476}
4477
4478static int binder_open(struct inode *nodp, struct file *filp)
4479{
4480 struct binder_proc *proc;
Martijn Coenenac4812c2017-02-03 14:40:48 -08004481 struct binder_device *binder_dev;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004482
4483 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
4484 current->group_leader->pid, current->pid);
4485
4486 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
4487 if (proc == NULL)
4488 return -ENOMEM;
Todd Kjos9630fe82017-06-29 12:02:00 -07004489 spin_lock_init(&proc->inner_lock);
4490 spin_lock_init(&proc->outer_lock);
Todd Kjosc4ea41b2017-06-29 12:01:36 -07004491 get_task_struct(current->group_leader);
4492 proc->tsk = current->group_leader;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004493 INIT_LIST_HEAD(&proc->todo);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004494 proc->default_priority = task_nice(current);
Martijn Coenenac4812c2017-02-03 14:40:48 -08004495 binder_dev = container_of(filp->private_data, struct binder_device,
4496 miscdev);
4497 proc->context = &binder_dev->context;
Todd Kjos19c98722017-06-29 12:01:40 -07004498 binder_alloc_init(&proc->alloc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004499
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004500 binder_stats_created(BINDER_STAT_PROC);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004501 proc->pid = current->group_leader->pid;
4502 INIT_LIST_HEAD(&proc->delivered_death);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004503 INIT_LIST_HEAD(&proc->waiting_threads);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004504 filp->private_data = proc;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004505
Todd Kjosc44b1232017-06-29 12:01:43 -07004506 mutex_lock(&binder_procs_lock);
4507 hlist_add_head(&proc->proc_node, &binder_procs);
4508 mutex_unlock(&binder_procs_lock);
4509
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004510 if (binder_debugfs_dir_entry_proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004511 char strbuf[11];
Seunghun Lee10f62862014-05-01 01:30:23 +09004512
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004513 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
Martijn Coenen14db3182017-02-03 14:40:47 -08004514 /*
4515 * proc debug entries are shared between contexts, so
4516 * this will fail if the process tries to open the driver
4517 * again with a different context. The priting code will
4518 * anyway print all contexts that a given PID has, so this
4519 * is not a problem.
4520 */
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004521 proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
Martijn Coenen14db3182017-02-03 14:40:47 -08004522 binder_debugfs_dir_entry_proc,
4523 (void *)(unsigned long)proc->pid,
4524 &binder_proc_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004525 }
4526
4527 return 0;
4528}
4529
4530static int binder_flush(struct file *filp, fl_owner_t id)
4531{
4532 struct binder_proc *proc = filp->private_data;
4533
4534 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
4535
4536 return 0;
4537}
4538
4539static void binder_deferred_flush(struct binder_proc *proc)
4540{
4541 struct rb_node *n;
4542 int wake_count = 0;
Seunghun Lee10f62862014-05-01 01:30:23 +09004543
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004544 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004545 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
4546 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
Seunghun Lee10f62862014-05-01 01:30:23 +09004547
Todd Kjos08dabce2017-06-29 12:01:49 -07004548 thread->looper_need_return = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004549 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
4550 wake_up_interruptible(&thread->wait);
4551 wake_count++;
4552 }
4553 }
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004554 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004555
4556 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4557 "binder_flush: %d woke %d threads\n", proc->pid,
4558 wake_count);
4559}
4560
4561static int binder_release(struct inode *nodp, struct file *filp)
4562{
4563 struct binder_proc *proc = filp->private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004564
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004565 debugfs_remove(proc->debugfs_entry);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004566 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
4567
4568 return 0;
4569}
4570
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004571static int binder_node_release(struct binder_node *node, int refs)
4572{
4573 struct binder_ref *ref;
4574 int death = 0;
Todd Kjosed297212017-06-29 12:02:01 -07004575 struct binder_proc *proc = node->proc;
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004576
Todd Kjos72196392017-06-29 12:02:02 -07004577 binder_release_work(proc, &node->async_todo);
Todd Kjosed297212017-06-29 12:02:01 -07004578
Todd Kjos673068e2017-06-29 12:02:03 -07004579 binder_node_lock(node);
Todd Kjosed297212017-06-29 12:02:01 -07004580 binder_inner_proc_lock(proc);
Todd Kjos72196392017-06-29 12:02:02 -07004581 binder_dequeue_work_ilocked(&node->work);
Todd Kjosadc18842017-06-29 12:01:59 -07004582 /*
4583 * The caller must have taken a temporary ref on the node,
4584 */
4585 BUG_ON(!node->tmp_refs);
4586 if (hlist_empty(&node->refs) && node->tmp_refs == 1) {
Todd Kjosed297212017-06-29 12:02:01 -07004587 binder_inner_proc_unlock(proc);
Todd Kjos673068e2017-06-29 12:02:03 -07004588 binder_node_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07004589 binder_free_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004590
4591 return refs;
4592 }
4593
4594 node->proc = NULL;
4595 node->local_strong_refs = 0;
4596 node->local_weak_refs = 0;
Todd Kjosed297212017-06-29 12:02:01 -07004597 binder_inner_proc_unlock(proc);
Todd Kjosc44b1232017-06-29 12:01:43 -07004598
4599 spin_lock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004600 hlist_add_head(&node->dead_node, &binder_dead_nodes);
Todd Kjosc44b1232017-06-29 12:01:43 -07004601 spin_unlock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004602
4603 hlist_for_each_entry(ref, &node->refs, node_entry) {
4604 refs++;
Martijn Coenenab51ec62017-06-29 12:02:10 -07004605 /*
4606 * Need the node lock to synchronize
4607 * with new notification requests and the
4608 * inner lock to synchronize with queued
4609 * death notifications.
4610 */
4611 binder_inner_proc_lock(ref->proc);
4612 if (!ref->death) {
4613 binder_inner_proc_unlock(ref->proc);
Arve Hjønnevåge194fd82014-02-17 13:58:29 -08004614 continue;
Martijn Coenenab51ec62017-06-29 12:02:10 -07004615 }
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004616
4617 death++;
4618
Martijn Coenenab51ec62017-06-29 12:02:10 -07004619 BUG_ON(!list_empty(&ref->death->work.entry));
4620 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
4621 binder_enqueue_work_ilocked(&ref->death->work,
4622 &ref->proc->todo);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004623 binder_wakeup_proc_ilocked(ref->proc, false);
Todd Kjos72196392017-06-29 12:02:02 -07004624 binder_inner_proc_unlock(ref->proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004625 }
4626
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004627 binder_debug(BINDER_DEBUG_DEAD_BINDER,
4628 "node %d now dead, refs %d, death %d\n",
4629 node->debug_id, refs, death);
Todd Kjos673068e2017-06-29 12:02:03 -07004630 binder_node_unlock(node);
Todd Kjosadc18842017-06-29 12:01:59 -07004631 binder_put_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004632
4633 return refs;
4634}
4635
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004636static void binder_deferred_release(struct binder_proc *proc)
4637{
Martijn Coenen342e5c92017-02-03 14:40:46 -08004638 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004639 struct rb_node *n;
Todd Kjos19c98722017-06-29 12:01:40 -07004640 int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004641
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004642 BUG_ON(proc->files);
4643
Todd Kjosc44b1232017-06-29 12:01:43 -07004644 mutex_lock(&binder_procs_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004645 hlist_del(&proc->proc_node);
Todd Kjosc44b1232017-06-29 12:01:43 -07004646 mutex_unlock(&binder_procs_lock);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004647
Todd Kjosc44b1232017-06-29 12:01:43 -07004648 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen342e5c92017-02-03 14:40:46 -08004649 if (context->binder_context_mgr_node &&
4650 context->binder_context_mgr_node->proc == proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004651 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01004652 "%s: %d context_mgr_node gone\n",
4653 __func__, proc->pid);
Martijn Coenen342e5c92017-02-03 14:40:46 -08004654 context->binder_context_mgr_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004655 }
Todd Kjosc44b1232017-06-29 12:01:43 -07004656 mutex_unlock(&context->context_mgr_node_lock);
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004657 binder_inner_proc_lock(proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07004658 /*
4659 * Make sure proc stays alive after we
4660 * remove all the threads
4661 */
4662 proc->tmp_ref++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004663
Todd Kjos7a4408c2017-06-29 12:01:57 -07004664 proc->is_dead = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004665 threads = 0;
4666 active_transactions = 0;
4667 while ((n = rb_first(&proc->threads))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004668 struct binder_thread *thread;
4669
4670 thread = rb_entry(n, struct binder_thread, rb_node);
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004671 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004672 threads++;
Todd Kjos7a4408c2017-06-29 12:01:57 -07004673 active_transactions += binder_thread_release(proc, thread);
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004674 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004675 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004676
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004677 nodes = 0;
4678 incoming_refs = 0;
4679 while ((n = rb_first(&proc->nodes))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004680 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004681
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004682 node = rb_entry(n, struct binder_node, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004683 nodes++;
Todd Kjosadc18842017-06-29 12:01:59 -07004684 /*
4685 * take a temporary ref on the node before
4686 * calling binder_node_release() which will either
4687 * kfree() the node or call binder_put_node()
4688 */
Todd Kjosda0fa9e2017-06-29 12:02:04 -07004689 binder_inc_node_tmpref_ilocked(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004690 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07004691 binder_inner_proc_unlock(proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004692 incoming_refs = binder_node_release(node, incoming_refs);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07004693 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004694 }
Todd Kjosda0fa9e2017-06-29 12:02:04 -07004695 binder_inner_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004696
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004697 outgoing_refs = 0;
Todd Kjos2c1838d2017-06-29 12:02:08 -07004698 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004699 while ((n = rb_first(&proc->refs_by_desc))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004700 struct binder_ref *ref;
4701
4702 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004703 outgoing_refs++;
Todd Kjos2c1838d2017-06-29 12:02:08 -07004704 binder_cleanup_ref_olocked(ref);
4705 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07004706 binder_free_ref(ref);
Todd Kjos2c1838d2017-06-29 12:02:08 -07004707 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004708 }
Todd Kjos2c1838d2017-06-29 12:02:08 -07004709 binder_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004710
Todd Kjos72196392017-06-29 12:02:02 -07004711 binder_release_work(proc, &proc->todo);
4712 binder_release_work(proc, &proc->delivered_death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004713
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004714 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Todd Kjos19c98722017-06-29 12:01:40 -07004715 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01004716 __func__, proc->pid, threads, nodes, incoming_refs,
Todd Kjos19c98722017-06-29 12:01:40 -07004717 outgoing_refs, active_transactions);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004718
Todd Kjos7a4408c2017-06-29 12:01:57 -07004719 binder_proc_dec_tmpref(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004720}
4721
4722static void binder_deferred_func(struct work_struct *work)
4723{
4724 struct binder_proc *proc;
4725 struct files_struct *files;
4726
4727 int defer;
Seunghun Lee10f62862014-05-01 01:30:23 +09004728
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004729 do {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004730 mutex_lock(&binder_deferred_lock);
4731 if (!hlist_empty(&binder_deferred_list)) {
4732 proc = hlist_entry(binder_deferred_list.first,
4733 struct binder_proc, deferred_work_node);
4734 hlist_del_init(&proc->deferred_work_node);
4735 defer = proc->deferred_work;
4736 proc->deferred_work = 0;
4737 } else {
4738 proc = NULL;
4739 defer = 0;
4740 }
4741 mutex_unlock(&binder_deferred_lock);
4742
4743 files = NULL;
4744 if (defer & BINDER_DEFERRED_PUT_FILES) {
4745 files = proc->files;
4746 if (files)
4747 proc->files = NULL;
4748 }
4749
4750 if (defer & BINDER_DEFERRED_FLUSH)
4751 binder_deferred_flush(proc);
4752
4753 if (defer & BINDER_DEFERRED_RELEASE)
4754 binder_deferred_release(proc); /* frees proc */
4755
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004756 if (files)
4757 put_files_struct(files);
4758 } while (proc);
4759}
4760static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
4761
4762static void
4763binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
4764{
4765 mutex_lock(&binder_deferred_lock);
4766 proc->deferred_work |= defer;
4767 if (hlist_unhashed(&proc->deferred_work_node)) {
4768 hlist_add_head(&proc->deferred_work_node,
4769 &binder_deferred_list);
Bhaktipriya Shridhar1beba522016-08-13 22:16:24 +05304770 schedule_work(&binder_deferred_work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004771 }
4772 mutex_unlock(&binder_deferred_lock);
4773}
4774
Todd Kjos5f2f6362017-06-29 12:02:09 -07004775static void print_binder_transaction_ilocked(struct seq_file *m,
4776 struct binder_proc *proc,
4777 const char *prefix,
4778 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004779{
Todd Kjos5f2f6362017-06-29 12:02:09 -07004780 struct binder_proc *to_proc;
4781 struct binder_buffer *buffer = t->buffer;
4782
4783 WARN_ON(!spin_is_locked(&proc->inner_lock));
Todd Kjos7a4408c2017-06-29 12:01:57 -07004784 spin_lock(&t->lock);
Todd Kjos5f2f6362017-06-29 12:02:09 -07004785 to_proc = t->to_proc;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004786 seq_printf(m,
4787 "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %ld r%d",
4788 prefix, t->debug_id, t,
4789 t->from ? t->from->proc->pid : 0,
4790 t->from ? t->from->pid : 0,
Todd Kjos5f2f6362017-06-29 12:02:09 -07004791 to_proc ? to_proc->pid : 0,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004792 t->to_thread ? t->to_thread->pid : 0,
4793 t->code, t->flags, t->priority, t->need_reply);
Todd Kjos7a4408c2017-06-29 12:01:57 -07004794 spin_unlock(&t->lock);
4795
Todd Kjos5f2f6362017-06-29 12:02:09 -07004796 if (proc != to_proc) {
4797 /*
4798 * Can only safely deref buffer if we are holding the
4799 * correct proc inner lock for this node
4800 */
4801 seq_puts(m, "\n");
4802 return;
4803 }
4804
4805 if (buffer == NULL) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004806 seq_puts(m, " buffer free\n");
4807 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004808 }
Todd Kjos5f2f6362017-06-29 12:02:09 -07004809 if (buffer->target_node)
4810 seq_printf(m, " node %d", buffer->target_node->debug_id);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004811 seq_printf(m, " size %zd:%zd data %p\n",
Todd Kjos5f2f6362017-06-29 12:02:09 -07004812 buffer->data_size, buffer->offsets_size,
4813 buffer->data);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004814}
4815
Todd Kjos5f2f6362017-06-29 12:02:09 -07004816static void print_binder_work_ilocked(struct seq_file *m,
4817 struct binder_proc *proc,
4818 const char *prefix,
4819 const char *transaction_prefix,
4820 struct binder_work *w)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004821{
4822 struct binder_node *node;
4823 struct binder_transaction *t;
4824
4825 switch (w->type) {
4826 case BINDER_WORK_TRANSACTION:
4827 t = container_of(w, struct binder_transaction, work);
Todd Kjos5f2f6362017-06-29 12:02:09 -07004828 print_binder_transaction_ilocked(
4829 m, proc, transaction_prefix, t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004830 break;
Todd Kjos26549d12017-06-29 12:01:55 -07004831 case BINDER_WORK_RETURN_ERROR: {
4832 struct binder_error *e = container_of(
4833 w, struct binder_error, work);
4834
4835 seq_printf(m, "%stransaction error: %u\n",
4836 prefix, e->cmd);
4837 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004838 case BINDER_WORK_TRANSACTION_COMPLETE:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004839 seq_printf(m, "%stransaction complete\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004840 break;
4841 case BINDER_WORK_NODE:
4842 node = container_of(w, struct binder_node, work);
Arve Hjønnevågda498892014-02-21 14:40:26 -08004843 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
4844 prefix, node->debug_id,
4845 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004846 break;
4847 case BINDER_WORK_DEAD_BINDER:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004848 seq_printf(m, "%shas dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004849 break;
4850 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004851 seq_printf(m, "%shas cleared dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004852 break;
4853 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004854 seq_printf(m, "%shas cleared death notification\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004855 break;
4856 default:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004857 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004858 break;
4859 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004860}
4861
Todd Kjos72196392017-06-29 12:02:02 -07004862static void print_binder_thread_ilocked(struct seq_file *m,
4863 struct binder_thread *thread,
4864 int print_always)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004865{
4866 struct binder_transaction *t;
4867 struct binder_work *w;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004868 size_t start_pos = m->count;
4869 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004870
Todd Kjos72196392017-06-29 12:02:02 -07004871 WARN_ON(!spin_is_locked(&thread->proc->inner_lock));
Todd Kjos7a4408c2017-06-29 12:01:57 -07004872 seq_printf(m, " thread %d: l %02x need_return %d tr %d\n",
Todd Kjos08dabce2017-06-29 12:01:49 -07004873 thread->pid, thread->looper,
Todd Kjos7a4408c2017-06-29 12:01:57 -07004874 thread->looper_need_return,
4875 atomic_read(&thread->tmp_ref));
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004876 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004877 t = thread->transaction_stack;
4878 while (t) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004879 if (t->from == thread) {
Todd Kjos5f2f6362017-06-29 12:02:09 -07004880 print_binder_transaction_ilocked(m, thread->proc,
4881 " outgoing transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004882 t = t->from_parent;
4883 } else if (t->to_thread == thread) {
Todd Kjos5f2f6362017-06-29 12:02:09 -07004884 print_binder_transaction_ilocked(m, thread->proc,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004885 " incoming transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004886 t = t->to_parent;
4887 } else {
Todd Kjos5f2f6362017-06-29 12:02:09 -07004888 print_binder_transaction_ilocked(m, thread->proc,
4889 " bad transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004890 t = NULL;
4891 }
4892 }
4893 list_for_each_entry(w, &thread->todo, entry) {
Todd Kjos5f2f6362017-06-29 12:02:09 -07004894 print_binder_work_ilocked(m, thread->proc, " ",
Todd Kjos72196392017-06-29 12:02:02 -07004895 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004896 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004897 if (!print_always && m->count == header_pos)
4898 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004899}
4900
Todd Kjosda0fa9e2017-06-29 12:02:04 -07004901static void print_binder_node_nilocked(struct seq_file *m,
4902 struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004903{
4904 struct binder_ref *ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004905 struct binder_work *w;
4906 int count;
4907
Todd Kjos673068e2017-06-29 12:02:03 -07004908 WARN_ON(!spin_is_locked(&node->lock));
Todd Kjosda0fa9e2017-06-29 12:02:04 -07004909 if (node->proc)
4910 WARN_ON(!spin_is_locked(&node->proc->inner_lock));
Todd Kjos673068e2017-06-29 12:02:03 -07004911
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004912 count = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004913 hlist_for_each_entry(ref, &node->refs, node_entry)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004914 count++;
4915
Todd Kjosadc18842017-06-29 12:01:59 -07004916 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 -08004917 node->debug_id, (u64)node->ptr, (u64)node->cookie,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004918 node->has_strong_ref, node->has_weak_ref,
4919 node->local_strong_refs, node->local_weak_refs,
Todd Kjosadc18842017-06-29 12:01:59 -07004920 node->internal_strong_refs, count, node->tmp_refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004921 if (count) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004922 seq_puts(m, " proc");
Sasha Levinb67bfe02013-02-27 17:06:00 -08004923 hlist_for_each_entry(ref, &node->refs, node_entry)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004924 seq_printf(m, " %d", ref->proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004925 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004926 seq_puts(m, "\n");
Todd Kjos72196392017-06-29 12:02:02 -07004927 if (node->proc) {
Todd Kjos72196392017-06-29 12:02:02 -07004928 list_for_each_entry(w, &node->async_todo, entry)
Todd Kjos5f2f6362017-06-29 12:02:09 -07004929 print_binder_work_ilocked(m, node->proc, " ",
Todd Kjos72196392017-06-29 12:02:02 -07004930 " pending async transaction", w);
Todd Kjos72196392017-06-29 12:02:02 -07004931 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004932}
4933
Todd Kjos2c1838d2017-06-29 12:02:08 -07004934static void print_binder_ref_olocked(struct seq_file *m,
4935 struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004936{
Todd Kjos2c1838d2017-06-29 12:02:08 -07004937 WARN_ON(!spin_is_locked(&ref->proc->outer_lock));
Todd Kjos673068e2017-06-29 12:02:03 -07004938 binder_node_lock(ref->node);
Todd Kjos372e3142017-06-29 12:01:58 -07004939 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
4940 ref->data.debug_id, ref->data.desc,
4941 ref->node->proc ? "" : "dead ",
4942 ref->node->debug_id, ref->data.strong,
4943 ref->data.weak, ref->death);
Todd Kjos673068e2017-06-29 12:02:03 -07004944 binder_node_unlock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004945}
4946
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004947static void print_binder_proc(struct seq_file *m,
4948 struct binder_proc *proc, int print_all)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004949{
4950 struct binder_work *w;
4951 struct rb_node *n;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004952 size_t start_pos = m->count;
4953 size_t header_pos;
Todd Kjosda0fa9e2017-06-29 12:02:04 -07004954 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004955
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004956 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen14db3182017-02-03 14:40:47 -08004957 seq_printf(m, "context %s\n", proc->context->name);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004958 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004959
Todd Kjos72196392017-06-29 12:02:02 -07004960 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004961 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
Todd Kjos72196392017-06-29 12:02:02 -07004962 print_binder_thread_ilocked(m, rb_entry(n, struct binder_thread,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004963 rb_node), print_all);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07004964
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004965 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004966 struct binder_node *node = rb_entry(n, struct binder_node,
4967 rb_node);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07004968 /*
4969 * take a temporary reference on the node so it
4970 * survives and isn't removed from the tree
4971 * while we print it.
4972 */
4973 binder_inc_node_tmpref_ilocked(node);
4974 /* Need to drop inner lock to take node lock */
4975 binder_inner_proc_unlock(proc);
4976 if (last_node)
4977 binder_put_node(last_node);
4978 binder_node_inner_lock(node);
4979 print_binder_node_nilocked(m, node);
4980 binder_node_inner_unlock(node);
4981 last_node = node;
4982 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004983 }
Todd Kjosda0fa9e2017-06-29 12:02:04 -07004984 binder_inner_proc_unlock(proc);
4985 if (last_node)
4986 binder_put_node(last_node);
4987
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004988 if (print_all) {
Todd Kjos2c1838d2017-06-29 12:02:08 -07004989 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004990 for (n = rb_first(&proc->refs_by_desc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004991 n != NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004992 n = rb_next(n))
Todd Kjos2c1838d2017-06-29 12:02:08 -07004993 print_binder_ref_olocked(m, rb_entry(n,
4994 struct binder_ref,
4995 rb_node_desc));
4996 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004997 }
Todd Kjos19c98722017-06-29 12:01:40 -07004998 binder_alloc_print_allocated(m, &proc->alloc);
Todd Kjos72196392017-06-29 12:02:02 -07004999 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005000 list_for_each_entry(w, &proc->todo, entry)
Todd Kjos5f2f6362017-06-29 12:02:09 -07005001 print_binder_work_ilocked(m, proc, " ",
5002 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005003 list_for_each_entry(w, &proc->delivered_death, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005004 seq_puts(m, " has delivered dead binder\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005005 break;
5006 }
Todd Kjos72196392017-06-29 12:02:02 -07005007 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005008 if (!print_all && m->count == header_pos)
5009 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005010}
5011
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005012static const char * const binder_return_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005013 "BR_ERROR",
5014 "BR_OK",
5015 "BR_TRANSACTION",
5016 "BR_REPLY",
5017 "BR_ACQUIRE_RESULT",
5018 "BR_DEAD_REPLY",
5019 "BR_TRANSACTION_COMPLETE",
5020 "BR_INCREFS",
5021 "BR_ACQUIRE",
5022 "BR_RELEASE",
5023 "BR_DECREFS",
5024 "BR_ATTEMPT_ACQUIRE",
5025 "BR_NOOP",
5026 "BR_SPAWN_LOOPER",
5027 "BR_FINISHED",
5028 "BR_DEAD_BINDER",
5029 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
5030 "BR_FAILED_REPLY"
5031};
5032
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005033static const char * const binder_command_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005034 "BC_TRANSACTION",
5035 "BC_REPLY",
5036 "BC_ACQUIRE_RESULT",
5037 "BC_FREE_BUFFER",
5038 "BC_INCREFS",
5039 "BC_ACQUIRE",
5040 "BC_RELEASE",
5041 "BC_DECREFS",
5042 "BC_INCREFS_DONE",
5043 "BC_ACQUIRE_DONE",
5044 "BC_ATTEMPT_ACQUIRE",
5045 "BC_REGISTER_LOOPER",
5046 "BC_ENTER_LOOPER",
5047 "BC_EXIT_LOOPER",
5048 "BC_REQUEST_DEATH_NOTIFICATION",
5049 "BC_CLEAR_DEATH_NOTIFICATION",
Martijn Coenen79802402017-02-03 14:40:51 -08005050 "BC_DEAD_BINDER_DONE",
5051 "BC_TRANSACTION_SG",
5052 "BC_REPLY_SG",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005053};
5054
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005055static const char * const binder_objstat_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005056 "proc",
5057 "thread",
5058 "node",
5059 "ref",
5060 "death",
5061 "transaction",
5062 "transaction_complete"
5063};
5064
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005065static void print_binder_stats(struct seq_file *m, const char *prefix,
5066 struct binder_stats *stats)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005067{
5068 int i;
5069
5070 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005071 ARRAY_SIZE(binder_command_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005072 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07005073 int temp = atomic_read(&stats->bc[i]);
5074
5075 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005076 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07005077 binder_command_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005078 }
5079
5080 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005081 ARRAY_SIZE(binder_return_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005082 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07005083 int temp = atomic_read(&stats->br[i]);
5084
5085 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005086 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07005087 binder_return_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005088 }
5089
5090 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005091 ARRAY_SIZE(binder_objstat_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005092 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005093 ARRAY_SIZE(stats->obj_deleted));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005094 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07005095 int created = atomic_read(&stats->obj_created[i]);
5096 int deleted = atomic_read(&stats->obj_deleted[i]);
5097
5098 if (created || deleted)
5099 seq_printf(m, "%s%s: active %d total %d\n",
5100 prefix,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005101 binder_objstat_strings[i],
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07005102 created - deleted,
5103 created);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005104 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005105}
5106
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005107static void print_binder_proc_stats(struct seq_file *m,
5108 struct binder_proc *proc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005109{
5110 struct binder_work *w;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02005111 struct binder_thread *thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005112 struct rb_node *n;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02005113 int count, strong, weak, ready_threads;
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07005114 size_t free_async_space =
5115 binder_alloc_get_free_async_space(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005116
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005117 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen14db3182017-02-03 14:40:47 -08005118 seq_printf(m, "context %s\n", proc->context->name);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005119 count = 0;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02005120 ready_threads = 0;
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07005121 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005122 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
5123 count++;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02005124
5125 list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node)
5126 ready_threads++;
5127
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005128 seq_printf(m, " threads: %d\n", count);
5129 seq_printf(m, " requested threads: %d+%d/%d\n"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005130 " ready threads %d\n"
5131 " free async space %zd\n", proc->requested_threads,
5132 proc->requested_threads_started, proc->max_threads,
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02005133 ready_threads,
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07005134 free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005135 count = 0;
5136 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
5137 count++;
Todd Kjosda0fa9e2017-06-29 12:02:04 -07005138 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005139 seq_printf(m, " nodes: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005140 count = 0;
5141 strong = 0;
5142 weak = 0;
Todd Kjos2c1838d2017-06-29 12:02:08 -07005143 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005144 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
5145 struct binder_ref *ref = rb_entry(n, struct binder_ref,
5146 rb_node_desc);
5147 count++;
Todd Kjos372e3142017-06-29 12:01:58 -07005148 strong += ref->data.strong;
5149 weak += ref->data.weak;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005150 }
Todd Kjos2c1838d2017-06-29 12:02:08 -07005151 binder_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005152 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005153
Todd Kjos19c98722017-06-29 12:01:40 -07005154 count = binder_alloc_get_allocated_count(&proc->alloc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005155 seq_printf(m, " buffers: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005156
Sherry Yang8ef46652017-08-31 11:56:36 -07005157 binder_alloc_print_pages(m, &proc->alloc);
5158
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005159 count = 0;
Todd Kjos72196392017-06-29 12:02:02 -07005160 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005161 list_for_each_entry(w, &proc->todo, entry) {
Todd Kjos72196392017-06-29 12:02:02 -07005162 if (w->type == BINDER_WORK_TRANSACTION)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005163 count++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005164 }
Todd Kjos72196392017-06-29 12:02:02 -07005165 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005166 seq_printf(m, " pending transactions: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005167
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005168 print_binder_stats(m, " ", &proc->stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005169}
5170
5171
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005172static int binder_state_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005173{
5174 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005175 struct binder_node *node;
Todd Kjos673068e2017-06-29 12:02:03 -07005176 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005177
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005178 seq_puts(m, "binder state:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005179
Todd Kjosc44b1232017-06-29 12:01:43 -07005180 spin_lock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005181 if (!hlist_empty(&binder_dead_nodes))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005182 seq_puts(m, "dead nodes:\n");
Todd Kjos673068e2017-06-29 12:02:03 -07005183 hlist_for_each_entry(node, &binder_dead_nodes, dead_node) {
5184 /*
5185 * take a temporary reference on the node so it
5186 * survives and isn't removed from the list
5187 * while we print it.
5188 */
5189 node->tmp_refs++;
5190 spin_unlock(&binder_dead_nodes_lock);
5191 if (last_node)
5192 binder_put_node(last_node);
5193 binder_node_lock(node);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07005194 print_binder_node_nilocked(m, node);
Todd Kjos673068e2017-06-29 12:02:03 -07005195 binder_node_unlock(node);
5196 last_node = node;
5197 spin_lock(&binder_dead_nodes_lock);
5198 }
Todd Kjosc44b1232017-06-29 12:01:43 -07005199 spin_unlock(&binder_dead_nodes_lock);
Todd Kjos673068e2017-06-29 12:02:03 -07005200 if (last_node)
5201 binder_put_node(last_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005202
Todd Kjosc44b1232017-06-29 12:01:43 -07005203 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005204 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005205 print_binder_proc(m, proc, 1);
Todd Kjosc44b1232017-06-29 12:01:43 -07005206 mutex_unlock(&binder_procs_lock);
Todd Kjosa60b8902017-06-29 12:02:11 -07005207
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005208 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005209}
5210
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005211static int binder_stats_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005212{
5213 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005214
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005215 seq_puts(m, "binder stats:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005216
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005217 print_binder_stats(m, "", &binder_stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005218
Todd Kjosc44b1232017-06-29 12:01:43 -07005219 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005220 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005221 print_binder_proc_stats(m, proc);
Todd Kjosc44b1232017-06-29 12:01:43 -07005222 mutex_unlock(&binder_procs_lock);
Todd Kjosa60b8902017-06-29 12:02:11 -07005223
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005224 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005225}
5226
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005227static int binder_transactions_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005228{
5229 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005230
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005231 seq_puts(m, "binder transactions:\n");
Todd Kjosc44b1232017-06-29 12:01:43 -07005232 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005233 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005234 print_binder_proc(m, proc, 0);
Todd Kjosc44b1232017-06-29 12:01:43 -07005235 mutex_unlock(&binder_procs_lock);
Todd Kjosa60b8902017-06-29 12:02:11 -07005236
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005237 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005238}
5239
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005240static int binder_proc_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005241{
Riley Andrews83050a42016-02-09 21:05:33 -08005242 struct binder_proc *itr;
Martijn Coenen14db3182017-02-03 14:40:47 -08005243 int pid = (unsigned long)m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005244
Todd Kjosc44b1232017-06-29 12:01:43 -07005245 mutex_lock(&binder_procs_lock);
Riley Andrews83050a42016-02-09 21:05:33 -08005246 hlist_for_each_entry(itr, &binder_procs, proc_node) {
Martijn Coenen14db3182017-02-03 14:40:47 -08005247 if (itr->pid == pid) {
5248 seq_puts(m, "binder proc state:\n");
5249 print_binder_proc(m, itr, 1);
Riley Andrews83050a42016-02-09 21:05:33 -08005250 }
5251 }
Todd Kjosc44b1232017-06-29 12:01:43 -07005252 mutex_unlock(&binder_procs_lock);
5253
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005254 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005255}
5256
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005257static void print_binder_transaction_log_entry(struct seq_file *m,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005258 struct binder_transaction_log_entry *e)
5259{
Todd Kjosd99c7332017-06-29 12:01:53 -07005260 int debug_id = READ_ONCE(e->debug_id_done);
5261 /*
5262 * read barrier to guarantee debug_id_done read before
5263 * we print the log values
5264 */
5265 smp_rmb();
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005266 seq_printf(m,
Todd Kjosd99c7332017-06-29 12:01:53 -07005267 "%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 -07005268 e->debug_id, (e->call_type == 2) ? "reply" :
5269 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
Martijn Coenen14db3182017-02-03 14:40:47 -08005270 e->from_thread, e->to_proc, e->to_thread, e->context_name,
Todd Kjos57ada2f2017-06-29 12:01:46 -07005271 e->to_node, e->target_handle, e->data_size, e->offsets_size,
5272 e->return_error, e->return_error_param,
5273 e->return_error_line);
Todd Kjosd99c7332017-06-29 12:01:53 -07005274 /*
5275 * read-barrier to guarantee read of debug_id_done after
5276 * done printing the fields of the entry
5277 */
5278 smp_rmb();
5279 seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ?
5280 "\n" : " (incomplete)\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005281}
5282
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005283static int binder_transaction_log_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005284{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005285 struct binder_transaction_log *log = m->private;
Todd Kjosd99c7332017-06-29 12:01:53 -07005286 unsigned int log_cur = atomic_read(&log->cur);
5287 unsigned int count;
5288 unsigned int cur;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005289 int i;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005290
Todd Kjosd99c7332017-06-29 12:01:53 -07005291 count = log_cur + 1;
5292 cur = count < ARRAY_SIZE(log->entry) && !log->full ?
5293 0 : count % ARRAY_SIZE(log->entry);
5294 if (count > ARRAY_SIZE(log->entry) || log->full)
5295 count = ARRAY_SIZE(log->entry);
5296 for (i = 0; i < count; i++) {
5297 unsigned int index = cur++ % ARRAY_SIZE(log->entry);
5298
5299 print_binder_transaction_log_entry(m, &log->entry[index]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005300 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005301 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005302}
5303
5304static const struct file_operations binder_fops = {
5305 .owner = THIS_MODULE,
5306 .poll = binder_poll,
5307 .unlocked_ioctl = binder_ioctl,
Arve Hjønnevågda498892014-02-21 14:40:26 -08005308 .compat_ioctl = binder_ioctl,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005309 .mmap = binder_mmap,
5310 .open = binder_open,
5311 .flush = binder_flush,
5312 .release = binder_release,
5313};
5314
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005315BINDER_DEBUG_ENTRY(state);
5316BINDER_DEBUG_ENTRY(stats);
5317BINDER_DEBUG_ENTRY(transactions);
5318BINDER_DEBUG_ENTRY(transaction_log);
5319
Martijn Coenenac4812c2017-02-03 14:40:48 -08005320static int __init init_binder_device(const char *name)
5321{
5322 int ret;
5323 struct binder_device *binder_device;
5324
5325 binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
5326 if (!binder_device)
5327 return -ENOMEM;
5328
5329 binder_device->miscdev.fops = &binder_fops;
5330 binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
5331 binder_device->miscdev.name = name;
5332
5333 binder_device->context.binder_context_mgr_uid = INVALID_UID;
5334 binder_device->context.name = name;
Todd Kjosc44b1232017-06-29 12:01:43 -07005335 mutex_init(&binder_device->context.context_mgr_node_lock);
Martijn Coenenac4812c2017-02-03 14:40:48 -08005336
5337 ret = misc_register(&binder_device->miscdev);
5338 if (ret < 0) {
5339 kfree(binder_device);
5340 return ret;
5341 }
5342
5343 hlist_add_head(&binder_device->hlist, &binder_devices);
5344
5345 return ret;
5346}
5347
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005348static int __init binder_init(void)
5349{
5350 int ret;
Christian Brauner22eb9472017-08-21 16:13:28 +02005351 char *device_name, *device_names, *device_tmp;
Martijn Coenenac4812c2017-02-03 14:40:48 -08005352 struct binder_device *device;
5353 struct hlist_node *tmp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005354
Sherry Yangf2517eb2017-08-23 08:46:42 -07005355 binder_alloc_shrinker_init();
5356
Todd Kjosd99c7332017-06-29 12:01:53 -07005357 atomic_set(&binder_transaction_log.cur, ~0U);
5358 atomic_set(&binder_transaction_log_failed.cur, ~0U);
5359
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005360 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
5361 if (binder_debugfs_dir_entry_root)
5362 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
5363 binder_debugfs_dir_entry_root);
Martijn Coenenac4812c2017-02-03 14:40:48 -08005364
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005365 if (binder_debugfs_dir_entry_root) {
5366 debugfs_create_file("state",
5367 S_IRUGO,
5368 binder_debugfs_dir_entry_root,
5369 NULL,
5370 &binder_state_fops);
5371 debugfs_create_file("stats",
5372 S_IRUGO,
5373 binder_debugfs_dir_entry_root,
5374 NULL,
5375 &binder_stats_fops);
5376 debugfs_create_file("transactions",
5377 S_IRUGO,
5378 binder_debugfs_dir_entry_root,
5379 NULL,
5380 &binder_transactions_fops);
5381 debugfs_create_file("transaction_log",
5382 S_IRUGO,
5383 binder_debugfs_dir_entry_root,
5384 &binder_transaction_log,
5385 &binder_transaction_log_fops);
5386 debugfs_create_file("failed_transaction_log",
5387 S_IRUGO,
5388 binder_debugfs_dir_entry_root,
5389 &binder_transaction_log_failed,
5390 &binder_transaction_log_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005391 }
Martijn Coenenac4812c2017-02-03 14:40:48 -08005392
5393 /*
5394 * Copy the module_parameter string, because we don't want to
5395 * tokenize it in-place.
5396 */
5397 device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL);
5398 if (!device_names) {
5399 ret = -ENOMEM;
5400 goto err_alloc_device_names_failed;
5401 }
5402 strcpy(device_names, binder_devices_param);
5403
Christian Brauner22eb9472017-08-21 16:13:28 +02005404 device_tmp = device_names;
5405 while ((device_name = strsep(&device_tmp, ","))) {
Martijn Coenenac4812c2017-02-03 14:40:48 -08005406 ret = init_binder_device(device_name);
5407 if (ret)
5408 goto err_init_binder_device_failed;
5409 }
5410
5411 return ret;
5412
5413err_init_binder_device_failed:
5414 hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
5415 misc_deregister(&device->miscdev);
5416 hlist_del(&device->hlist);
5417 kfree(device);
5418 }
Christian Brauner22eb9472017-08-21 16:13:28 +02005419
5420 kfree(device_names);
5421
Martijn Coenenac4812c2017-02-03 14:40:48 -08005422err_alloc_device_names_failed:
5423 debugfs_remove_recursive(binder_debugfs_dir_entry_root);
5424
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005425 return ret;
5426}
5427
5428device_initcall(binder_init);
5429
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07005430#define CREATE_TRACE_POINTS
5431#include "binder_trace.h"
5432
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005433MODULE_LICENSE("GPL v2");