blob: 56b380292cc5d477cfb36957bfb8794b323360a9 [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
31 * (proc->threads, proc->nodes) and all todo lists associated
32 * with the binder_proc (proc->todo, thread->todo,
Martijn Coenen0b89d692017-06-29 12:02:06 -070033 * proc->delivered_death and node->async_todo), as well as
34 * 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)
Todd Kjos9630fe82017-06-29 12:02:00 -0700478 * @pid PID of group_leader of process
479 * (invariant after initialized)
480 * @tsk task_struct for group_leader of process
481 * (invariant after initialized)
482 * @files files_struct for process
483 * (invariant after initialized)
484 * @deferred_work_node: element for binder_deferred_list
485 * (protected by binder_deferred_lock)
486 * @deferred_work: bitmap of deferred work to perform
487 * (protected by binder_deferred_lock)
488 * @is_dead: process is dead and awaiting free
489 * when outstanding transactions are cleaned up
Todd Kjos7bd7b0e2017-06-29 12:02:05 -0700490 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700491 * @todo: list of work for this process
Todd Kjos72196392017-06-29 12:02:02 -0700492 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700493 * @wait: wait queue head to wait for proc work
494 * (invariant after initialized)
495 * @stats: per-process binder statistics
496 * (atomics, no lock needed)
497 * @delivered_death: list of delivered death notification
Todd Kjos72196392017-06-29 12:02:02 -0700498 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700499 * @max_threads: cap on number of binder threads
Todd Kjosb3e68612017-06-29 12:02:07 -0700500 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700501 * @requested_threads: number of binder threads requested but not
502 * yet started. In current implementation, can
503 * only be 0 or 1.
Todd Kjosb3e68612017-06-29 12:02:07 -0700504 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700505 * @requested_threads_started: number binder threads started
Todd Kjosb3e68612017-06-29 12:02:07 -0700506 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700507 * @ready_threads: number of threads waiting for proc work
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;
529 int pid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900530 struct task_struct *tsk;
531 struct files_struct *files;
532 struct hlist_node deferred_work_node;
533 int deferred_work;
Todd Kjos7a4408c2017-06-29 12:01:57 -0700534 bool is_dead;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900535
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900536 struct list_head todo;
537 wait_queue_head_t wait;
538 struct binder_stats stats;
539 struct list_head delivered_death;
540 int max_threads;
541 int requested_threads;
542 int requested_threads_started;
543 int ready_threads;
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,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900559};
560
Todd Kjos9630fe82017-06-29 12:02:00 -0700561/**
562 * struct binder_thread - binder thread bookkeeping
563 * @proc: binder process for this thread
564 * (invariant after initialization)
565 * @rb_node: element for proc->threads rbtree
Todd Kjos7bd7b0e2017-06-29 12:02:05 -0700566 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700567 * @pid: PID for this thread
568 * (invariant after initialization)
569 * @looper: bitmap of looping state
570 * (only accessed by this thread)
571 * @looper_needs_return: looping thread needs to exit driver
572 * (no lock needed)
573 * @transaction_stack: stack of in-progress transactions for this thread
Martijn Coenen0b89d692017-06-29 12:02:06 -0700574 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700575 * @todo: list of work to do for this thread
Todd Kjos72196392017-06-29 12:02:02 -0700576 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700577 * @return_error: transaction errors reported by this thread
578 * (only accessed by this thread)
579 * @reply_error: transaction errors reported by target thread
Martijn Coenen0b89d692017-06-29 12:02:06 -0700580 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700581 * @wait: wait queue for thread work
582 * @stats: per-thread statistics
583 * (atomics, no lock needed)
584 * @tmp_ref: temporary reference to indicate thread is in use
585 * (atomic since @proc->inner_lock cannot
586 * always be acquired)
587 * @is_dead: thread is dead and awaiting free
588 * when outstanding transactions are cleaned up
Todd Kjos7bd7b0e2017-06-29 12:02:05 -0700589 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700590 *
591 * Bookkeeping structure for binder threads.
592 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900593struct binder_thread {
594 struct binder_proc *proc;
595 struct rb_node rb_node;
596 int pid;
Todd Kjos08dabce2017-06-29 12:01:49 -0700597 int looper; /* only modified by this thread */
598 bool looper_need_return; /* can be written by other thread */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900599 struct binder_transaction *transaction_stack;
600 struct list_head todo;
Todd Kjos26549d12017-06-29 12:01:55 -0700601 struct binder_error return_error;
602 struct binder_error reply_error;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900603 wait_queue_head_t wait;
604 struct binder_stats stats;
Todd Kjos7a4408c2017-06-29 12:01:57 -0700605 atomic_t tmp_ref;
606 bool is_dead;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900607};
608
609struct binder_transaction {
610 int debug_id;
611 struct binder_work work;
612 struct binder_thread *from;
613 struct binder_transaction *from_parent;
614 struct binder_proc *to_proc;
615 struct binder_thread *to_thread;
616 struct binder_transaction *to_parent;
617 unsigned need_reply:1;
618 /* unsigned is_dead:1; */ /* not used at the moment */
619
620 struct binder_buffer *buffer;
621 unsigned int code;
622 unsigned int flags;
623 long priority;
624 long saved_priority;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -0600625 kuid_t sender_euid;
Todd Kjos7a4408c2017-06-29 12:01:57 -0700626 /**
627 * @lock: protects @from, @to_proc, and @to_thread
628 *
629 * @from, @to_proc, and @to_thread can be set to NULL
630 * during thread teardown
631 */
632 spinlock_t lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900633};
634
Todd Kjos9630fe82017-06-29 12:02:00 -0700635/**
636 * binder_proc_lock() - Acquire outer lock for given binder_proc
637 * @proc: struct binder_proc to acquire
638 *
639 * Acquires proc->outer_lock. Used to protect binder_ref
640 * structures associated with the given proc.
641 */
642#define binder_proc_lock(proc) _binder_proc_lock(proc, __LINE__)
643static void
644_binder_proc_lock(struct binder_proc *proc, int line)
645{
646 binder_debug(BINDER_DEBUG_SPINLOCKS,
647 "%s: line=%d\n", __func__, line);
648 spin_lock(&proc->outer_lock);
649}
650
651/**
652 * binder_proc_unlock() - Release spinlock for given binder_proc
653 * @proc: struct binder_proc to acquire
654 *
655 * Release lock acquired via binder_proc_lock()
656 */
657#define binder_proc_unlock(_proc) _binder_proc_unlock(_proc, __LINE__)
658static void
659_binder_proc_unlock(struct binder_proc *proc, int line)
660{
661 binder_debug(BINDER_DEBUG_SPINLOCKS,
662 "%s: line=%d\n", __func__, line);
663 spin_unlock(&proc->outer_lock);
664}
665
666/**
667 * binder_inner_proc_lock() - Acquire inner lock for given binder_proc
668 * @proc: struct binder_proc to acquire
669 *
670 * Acquires proc->inner_lock. Used to protect todo lists
671 */
672#define binder_inner_proc_lock(proc) _binder_inner_proc_lock(proc, __LINE__)
673static void
674_binder_inner_proc_lock(struct binder_proc *proc, int line)
675{
676 binder_debug(BINDER_DEBUG_SPINLOCKS,
677 "%s: line=%d\n", __func__, line);
678 spin_lock(&proc->inner_lock);
679}
680
681/**
682 * binder_inner_proc_unlock() - Release inner lock for given binder_proc
683 * @proc: struct binder_proc to acquire
684 *
685 * Release lock acquired via binder_inner_proc_lock()
686 */
687#define binder_inner_proc_unlock(proc) _binder_inner_proc_unlock(proc, __LINE__)
688static void
689_binder_inner_proc_unlock(struct binder_proc *proc, int line)
690{
691 binder_debug(BINDER_DEBUG_SPINLOCKS,
692 "%s: line=%d\n", __func__, line);
693 spin_unlock(&proc->inner_lock);
694}
695
696/**
697 * binder_node_lock() - Acquire spinlock for given binder_node
698 * @node: struct binder_node to acquire
699 *
700 * Acquires node->lock. Used to protect binder_node fields
701 */
702#define binder_node_lock(node) _binder_node_lock(node, __LINE__)
703static void
704_binder_node_lock(struct binder_node *node, int line)
705{
706 binder_debug(BINDER_DEBUG_SPINLOCKS,
707 "%s: line=%d\n", __func__, line);
708 spin_lock(&node->lock);
709}
710
711/**
712 * binder_node_unlock() - Release spinlock for given binder_proc
713 * @node: struct binder_node to acquire
714 *
715 * Release lock acquired via binder_node_lock()
716 */
717#define binder_node_unlock(node) _binder_node_unlock(node, __LINE__)
718static void
719_binder_node_unlock(struct binder_node *node, int line)
720{
721 binder_debug(BINDER_DEBUG_SPINLOCKS,
722 "%s: line=%d\n", __func__, line);
723 spin_unlock(&node->lock);
724}
725
Todd Kjos673068e2017-06-29 12:02:03 -0700726/**
727 * binder_node_inner_lock() - Acquire node and inner locks
728 * @node: struct binder_node to acquire
729 *
730 * Acquires node->lock. If node->proc also acquires
731 * proc->inner_lock. Used to protect binder_node fields
732 */
733#define binder_node_inner_lock(node) _binder_node_inner_lock(node, __LINE__)
734static void
735_binder_node_inner_lock(struct binder_node *node, int line)
736{
737 binder_debug(BINDER_DEBUG_SPINLOCKS,
738 "%s: line=%d\n", __func__, line);
739 spin_lock(&node->lock);
740 if (node->proc)
741 binder_inner_proc_lock(node->proc);
742}
743
744/**
745 * binder_node_unlock() - Release node and inner locks
746 * @node: struct binder_node to acquire
747 *
748 * Release lock acquired via binder_node_lock()
749 */
750#define binder_node_inner_unlock(node) _binder_node_inner_unlock(node, __LINE__)
751static void
752_binder_node_inner_unlock(struct binder_node *node, int line)
753{
754 struct binder_proc *proc = node->proc;
755
756 binder_debug(BINDER_DEBUG_SPINLOCKS,
757 "%s: line=%d\n", __func__, line);
758 if (proc)
759 binder_inner_proc_unlock(proc);
760 spin_unlock(&node->lock);
761}
762
Todd Kjos72196392017-06-29 12:02:02 -0700763static bool binder_worklist_empty_ilocked(struct list_head *list)
764{
765 return list_empty(list);
766}
767
768/**
769 * binder_worklist_empty() - Check if no items on the work list
770 * @proc: binder_proc associated with list
771 * @list: list to check
772 *
773 * Return: true if there are no items on list, else false
774 */
775static bool binder_worklist_empty(struct binder_proc *proc,
776 struct list_head *list)
777{
778 bool ret;
779
780 binder_inner_proc_lock(proc);
781 ret = binder_worklist_empty_ilocked(list);
782 binder_inner_proc_unlock(proc);
783 return ret;
784}
785
786static void
787binder_enqueue_work_ilocked(struct binder_work *work,
788 struct list_head *target_list)
789{
790 BUG_ON(target_list == NULL);
791 BUG_ON(work->entry.next && !list_empty(&work->entry));
792 list_add_tail(&work->entry, target_list);
793}
794
795/**
796 * binder_enqueue_work() - Add an item to the work list
797 * @proc: binder_proc associated with list
798 * @work: struct binder_work to add to list
799 * @target_list: list to add work to
800 *
801 * Adds the work to the specified list. Asserts that work
802 * is not already on a list.
803 */
804static void
805binder_enqueue_work(struct binder_proc *proc,
806 struct binder_work *work,
807 struct list_head *target_list)
808{
809 binder_inner_proc_lock(proc);
810 binder_enqueue_work_ilocked(work, target_list);
811 binder_inner_proc_unlock(proc);
812}
813
814static void
815binder_dequeue_work_ilocked(struct binder_work *work)
816{
817 list_del_init(&work->entry);
818}
819
820/**
821 * binder_dequeue_work() - Removes an item from the work list
822 * @proc: binder_proc associated with list
823 * @work: struct binder_work to remove from list
824 *
825 * Removes the specified work item from whatever list it is on.
826 * Can safely be called if work is not on any list.
827 */
828static void
829binder_dequeue_work(struct binder_proc *proc, struct binder_work *work)
830{
831 binder_inner_proc_lock(proc);
832 binder_dequeue_work_ilocked(work);
833 binder_inner_proc_unlock(proc);
834}
835
836static struct binder_work *binder_dequeue_work_head_ilocked(
837 struct list_head *list)
838{
839 struct binder_work *w;
840
841 w = list_first_entry_or_null(list, struct binder_work, entry);
842 if (w)
843 list_del_init(&w->entry);
844 return w;
845}
846
847/**
848 * binder_dequeue_work_head() - Dequeues the item at head of list
849 * @proc: binder_proc associated with list
850 * @list: list to dequeue head
851 *
852 * Removes the head of the list if there are items on the list
853 *
854 * Return: pointer dequeued binder_work, NULL if list was empty
855 */
856static struct binder_work *binder_dequeue_work_head(
857 struct binder_proc *proc,
858 struct list_head *list)
859{
860 struct binder_work *w;
861
862 binder_inner_proc_lock(proc);
863 w = binder_dequeue_work_head_ilocked(list);
864 binder_inner_proc_unlock(proc);
865 return w;
866}
867
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900868static void
869binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
Todd Kjos7a4408c2017-06-29 12:01:57 -0700870static void binder_free_thread(struct binder_thread *thread);
871static void binder_free_proc(struct binder_proc *proc);
Todd Kjosda0fa9e2017-06-29 12:02:04 -0700872static void binder_inc_node_tmpref_ilocked(struct binder_node *node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900873
Sachin Kamatefde99c2012-08-17 16:39:36 +0530874static int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900875{
876 struct files_struct *files = proc->files;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900877 unsigned long rlim_cur;
878 unsigned long irqs;
879
880 if (files == NULL)
881 return -ESRCH;
882
Al Virodcfadfa2012-08-12 17:27:30 -0400883 if (!lock_task_sighand(proc->tsk, &irqs))
884 return -EMFILE;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900885
Al Virodcfadfa2012-08-12 17:27:30 -0400886 rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
887 unlock_task_sighand(proc->tsk, &irqs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900888
Al Virodcfadfa2012-08-12 17:27:30 -0400889 return __alloc_fd(files, 0, rlim_cur, flags);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900890}
891
892/*
893 * copied from fd_install
894 */
895static void task_fd_install(
896 struct binder_proc *proc, unsigned int fd, struct file *file)
897{
Al Virof869e8a2012-08-15 21:06:33 -0400898 if (proc->files)
899 __fd_install(proc->files, fd, file);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900900}
901
902/*
903 * copied from sys_close
904 */
905static long task_close_fd(struct binder_proc *proc, unsigned int fd)
906{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900907 int retval;
908
Al Viro483ce1d2012-08-19 12:04:24 -0400909 if (proc->files == NULL)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900910 return -ESRCH;
911
Al Viro483ce1d2012-08-19 12:04:24 -0400912 retval = __close_fd(proc->files, fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900913 /* can't restart close syscall because file table entry was cleared */
914 if (unlikely(retval == -ERESTARTSYS ||
915 retval == -ERESTARTNOINTR ||
916 retval == -ERESTARTNOHAND ||
917 retval == -ERESTART_RESTARTBLOCK))
918 retval = -EINTR;
919
920 return retval;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900921}
922
923static void binder_set_nice(long nice)
924{
925 long min_nice;
Seunghun Lee10f62862014-05-01 01:30:23 +0900926
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900927 if (can_nice(current, nice)) {
928 set_user_nice(current, nice);
929 return;
930 }
Krzysztof Opasiakc3643b62017-07-05 19:24:44 +0200931 min_nice = rlimit_to_nice(rlimit(RLIMIT_NICE));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900932 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530933 "%d: nice value %ld not allowed use %ld instead\n",
934 current->pid, nice, min_nice);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900935 set_user_nice(current, min_nice);
Dongsheng Yang8698a742014-03-11 18:09:12 +0800936 if (min_nice <= MAX_NICE)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900937 return;
Anmol Sarma56b468f2012-10-30 22:35:43 +0530938 binder_user_error("%d RLIMIT_NICE not set\n", current->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900939}
940
Todd Kjosda0fa9e2017-06-29 12:02:04 -0700941static struct binder_node *binder_get_node_ilocked(struct binder_proc *proc,
942 binder_uintptr_t ptr)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900943{
944 struct rb_node *n = proc->nodes.rb_node;
945 struct binder_node *node;
946
Todd Kjosda0fa9e2017-06-29 12:02:04 -0700947 BUG_ON(!spin_is_locked(&proc->inner_lock));
948
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900949 while (n) {
950 node = rb_entry(n, struct binder_node, rb_node);
951
952 if (ptr < node->ptr)
953 n = n->rb_left;
954 else if (ptr > node->ptr)
955 n = n->rb_right;
Todd Kjosadc18842017-06-29 12:01:59 -0700956 else {
957 /*
958 * take an implicit weak reference
959 * to ensure node stays alive until
960 * call to binder_put_node()
961 */
Todd Kjosda0fa9e2017-06-29 12:02:04 -0700962 binder_inc_node_tmpref_ilocked(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900963 return node;
Todd Kjosadc18842017-06-29 12:01:59 -0700964 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900965 }
966 return NULL;
967}
968
Todd Kjosda0fa9e2017-06-29 12:02:04 -0700969static struct binder_node *binder_get_node(struct binder_proc *proc,
970 binder_uintptr_t ptr)
971{
972 struct binder_node *node;
973
974 binder_inner_proc_lock(proc);
975 node = binder_get_node_ilocked(proc, ptr);
976 binder_inner_proc_unlock(proc);
977 return node;
978}
979
980static struct binder_node *binder_init_node_ilocked(
981 struct binder_proc *proc,
982 struct binder_node *new_node,
983 struct flat_binder_object *fp)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900984{
985 struct rb_node **p = &proc->nodes.rb_node;
986 struct rb_node *parent = NULL;
987 struct binder_node *node;
Todd Kjos673068e2017-06-29 12:02:03 -0700988 binder_uintptr_t ptr = fp ? fp->binder : 0;
989 binder_uintptr_t cookie = fp ? fp->cookie : 0;
990 __u32 flags = fp ? fp->flags : 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900991
Todd Kjosda0fa9e2017-06-29 12:02:04 -0700992 BUG_ON(!spin_is_locked(&proc->inner_lock));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900993 while (*p) {
Todd Kjosda0fa9e2017-06-29 12:02:04 -0700994
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900995 parent = *p;
996 node = rb_entry(parent, struct binder_node, rb_node);
997
998 if (ptr < node->ptr)
999 p = &(*p)->rb_left;
1000 else if (ptr > node->ptr)
1001 p = &(*p)->rb_right;
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001002 else {
1003 /*
1004 * A matching node is already in
1005 * the rb tree. Abandon the init
1006 * and return it.
1007 */
1008 binder_inc_node_tmpref_ilocked(node);
1009 return node;
1010 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001011 }
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001012 node = new_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001013 binder_stats_created(BINDER_STAT_NODE);
Todd Kjosadc18842017-06-29 12:01:59 -07001014 node->tmp_refs++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001015 rb_link_node(&node->rb_node, parent, p);
1016 rb_insert_color(&node->rb_node, &proc->nodes);
Todd Kjos656a8002017-06-29 12:01:45 -07001017 node->debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001018 node->proc = proc;
1019 node->ptr = ptr;
1020 node->cookie = cookie;
1021 node->work.type = BINDER_WORK_NODE;
Todd Kjos673068e2017-06-29 12:02:03 -07001022 node->min_priority = flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
1023 node->accept_fds = !!(flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
Todd Kjos9630fe82017-06-29 12:02:00 -07001024 spin_lock_init(&node->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001025 INIT_LIST_HEAD(&node->work.entry);
1026 INIT_LIST_HEAD(&node->async_todo);
1027 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001028 "%d:%d node %d u%016llx c%016llx created\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001029 proc->pid, current->pid, node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001030 (u64)node->ptr, (u64)node->cookie);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001031
1032 return node;
1033}
1034
1035static struct binder_node *binder_new_node(struct binder_proc *proc,
1036 struct flat_binder_object *fp)
1037{
1038 struct binder_node *node;
1039 struct binder_node *new_node = kzalloc(sizeof(*node), GFP_KERNEL);
1040
1041 if (!new_node)
1042 return NULL;
1043 binder_inner_proc_lock(proc);
1044 node = binder_init_node_ilocked(proc, new_node, fp);
1045 binder_inner_proc_unlock(proc);
1046 if (node != new_node)
1047 /*
1048 * The node was already added by another thread
1049 */
1050 kfree(new_node);
1051
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001052 return node;
1053}
1054
Todd Kjosed297212017-06-29 12:02:01 -07001055static void binder_free_node(struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001056{
Todd Kjosed297212017-06-29 12:02:01 -07001057 kfree(node);
1058 binder_stats_deleted(BINDER_STAT_NODE);
1059}
1060
Todd Kjos673068e2017-06-29 12:02:03 -07001061static int binder_inc_node_nilocked(struct binder_node *node, int strong,
1062 int internal,
1063 struct list_head *target_list)
Todd Kjosed297212017-06-29 12:02:01 -07001064{
Todd Kjos673068e2017-06-29 12:02:03 -07001065 struct binder_proc *proc = node->proc;
1066
1067 BUG_ON(!spin_is_locked(&node->lock));
1068 if (proc)
1069 BUG_ON(!spin_is_locked(&proc->inner_lock));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001070 if (strong) {
1071 if (internal) {
1072 if (target_list == NULL &&
1073 node->internal_strong_refs == 0 &&
Martijn Coenen342e5c92017-02-03 14:40:46 -08001074 !(node->proc &&
1075 node == node->proc->context->binder_context_mgr_node &&
1076 node->has_strong_ref)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301077 pr_err("invalid inc strong node for %d\n",
1078 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001079 return -EINVAL;
1080 }
1081 node->internal_strong_refs++;
1082 } else
1083 node->local_strong_refs++;
1084 if (!node->has_strong_ref && target_list) {
Todd Kjos72196392017-06-29 12:02:02 -07001085 binder_dequeue_work_ilocked(&node->work);
1086 binder_enqueue_work_ilocked(&node->work, target_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001087 }
1088 } else {
1089 if (!internal)
1090 node->local_weak_refs++;
1091 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
1092 if (target_list == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301093 pr_err("invalid inc weak node for %d\n",
1094 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001095 return -EINVAL;
1096 }
Todd Kjos72196392017-06-29 12:02:02 -07001097 binder_enqueue_work_ilocked(&node->work, target_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001098 }
1099 }
1100 return 0;
1101}
1102
Todd Kjosed297212017-06-29 12:02:01 -07001103static int binder_inc_node(struct binder_node *node, int strong, int internal,
1104 struct list_head *target_list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001105{
Todd Kjosed297212017-06-29 12:02:01 -07001106 int ret;
1107
Todd Kjos673068e2017-06-29 12:02:03 -07001108 binder_node_inner_lock(node);
1109 ret = binder_inc_node_nilocked(node, strong, internal, target_list);
1110 binder_node_inner_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07001111
1112 return ret;
1113}
1114
Todd Kjos673068e2017-06-29 12:02:03 -07001115static bool binder_dec_node_nilocked(struct binder_node *node,
1116 int strong, int internal)
Todd Kjosed297212017-06-29 12:02:01 -07001117{
1118 struct binder_proc *proc = node->proc;
1119
Todd Kjos673068e2017-06-29 12:02:03 -07001120 BUG_ON(!spin_is_locked(&node->lock));
Todd Kjosed297212017-06-29 12:02:01 -07001121 if (proc)
1122 BUG_ON(!spin_is_locked(&proc->inner_lock));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001123 if (strong) {
1124 if (internal)
1125 node->internal_strong_refs--;
1126 else
1127 node->local_strong_refs--;
1128 if (node->local_strong_refs || node->internal_strong_refs)
Todd Kjosed297212017-06-29 12:02:01 -07001129 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001130 } else {
1131 if (!internal)
1132 node->local_weak_refs--;
Todd Kjosadc18842017-06-29 12:01:59 -07001133 if (node->local_weak_refs || node->tmp_refs ||
1134 !hlist_empty(&node->refs))
Todd Kjosed297212017-06-29 12:02:01 -07001135 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001136 }
Todd Kjosed297212017-06-29 12:02:01 -07001137
1138 if (proc && (node->has_strong_ref || node->has_weak_ref)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001139 if (list_empty(&node->work.entry)) {
Todd Kjos72196392017-06-29 12:02:02 -07001140 binder_enqueue_work_ilocked(&node->work, &proc->todo);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001141 wake_up_interruptible(&node->proc->wait);
1142 }
1143 } else {
1144 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
Todd Kjosadc18842017-06-29 12:01:59 -07001145 !node->local_weak_refs && !node->tmp_refs) {
Todd Kjosed297212017-06-29 12:02:01 -07001146 if (proc) {
Todd Kjos72196392017-06-29 12:02:02 -07001147 binder_dequeue_work_ilocked(&node->work);
1148 rb_erase(&node->rb_node, &proc->nodes);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001149 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301150 "refless node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001151 node->debug_id);
1152 } else {
Todd Kjos72196392017-06-29 12:02:02 -07001153 BUG_ON(!list_empty(&node->work.entry));
Todd Kjosc44b1232017-06-29 12:01:43 -07001154 spin_lock(&binder_dead_nodes_lock);
Todd Kjosed297212017-06-29 12:02:01 -07001155 /*
1156 * tmp_refs could have changed so
1157 * check it again
1158 */
1159 if (node->tmp_refs) {
1160 spin_unlock(&binder_dead_nodes_lock);
1161 return false;
1162 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001163 hlist_del(&node->dead_node);
Todd Kjosc44b1232017-06-29 12:01:43 -07001164 spin_unlock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001165 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301166 "dead node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001167 node->debug_id);
1168 }
Todd Kjosed297212017-06-29 12:02:01 -07001169 return true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001170 }
1171 }
Todd Kjosed297212017-06-29 12:02:01 -07001172 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001173}
1174
Todd Kjosed297212017-06-29 12:02:01 -07001175static void binder_dec_node(struct binder_node *node, int strong, int internal)
1176{
1177 bool free_node;
1178
Todd Kjos673068e2017-06-29 12:02:03 -07001179 binder_node_inner_lock(node);
1180 free_node = binder_dec_node_nilocked(node, strong, internal);
1181 binder_node_inner_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07001182 if (free_node)
1183 binder_free_node(node);
1184}
1185
1186static void binder_inc_node_tmpref_ilocked(struct binder_node *node)
Todd Kjosadc18842017-06-29 12:01:59 -07001187{
1188 /*
1189 * No call to binder_inc_node() is needed since we
1190 * don't need to inform userspace of any changes to
1191 * tmp_refs
1192 */
1193 node->tmp_refs++;
1194}
1195
1196/**
Todd Kjosed297212017-06-29 12:02:01 -07001197 * binder_inc_node_tmpref() - take a temporary reference on node
1198 * @node: node to reference
1199 *
1200 * Take reference on node to prevent the node from being freed
1201 * while referenced only by a local variable. The inner lock is
1202 * needed to serialize with the node work on the queue (which
1203 * isn't needed after the node is dead). If the node is dead
1204 * (node->proc is NULL), use binder_dead_nodes_lock to protect
1205 * node->tmp_refs against dead-node-only cases where the node
1206 * lock cannot be acquired (eg traversing the dead node list to
1207 * print nodes)
1208 */
1209static void binder_inc_node_tmpref(struct binder_node *node)
1210{
Todd Kjos673068e2017-06-29 12:02:03 -07001211 binder_node_lock(node);
Todd Kjosed297212017-06-29 12:02:01 -07001212 if (node->proc)
1213 binder_inner_proc_lock(node->proc);
1214 else
1215 spin_lock(&binder_dead_nodes_lock);
1216 binder_inc_node_tmpref_ilocked(node);
1217 if (node->proc)
1218 binder_inner_proc_unlock(node->proc);
1219 else
1220 spin_unlock(&binder_dead_nodes_lock);
Todd Kjos673068e2017-06-29 12:02:03 -07001221 binder_node_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07001222}
1223
1224/**
Todd Kjosadc18842017-06-29 12:01:59 -07001225 * binder_dec_node_tmpref() - remove a temporary reference on node
1226 * @node: node to reference
1227 *
1228 * Release temporary reference on node taken via binder_inc_node_tmpref()
1229 */
1230static void binder_dec_node_tmpref(struct binder_node *node)
1231{
Todd Kjosed297212017-06-29 12:02:01 -07001232 bool free_node;
1233
Todd Kjos673068e2017-06-29 12:02:03 -07001234 binder_node_inner_lock(node);
1235 if (!node->proc)
Todd Kjosed297212017-06-29 12:02:01 -07001236 spin_lock(&binder_dead_nodes_lock);
Todd Kjosadc18842017-06-29 12:01:59 -07001237 node->tmp_refs--;
1238 BUG_ON(node->tmp_refs < 0);
Todd Kjosed297212017-06-29 12:02:01 -07001239 if (!node->proc)
1240 spin_unlock(&binder_dead_nodes_lock);
Todd Kjosadc18842017-06-29 12:01:59 -07001241 /*
1242 * Call binder_dec_node() to check if all refcounts are 0
1243 * and cleanup is needed. Calling with strong=0 and internal=1
1244 * causes no actual reference to be released in binder_dec_node().
1245 * If that changes, a change is needed here too.
1246 */
Todd Kjos673068e2017-06-29 12:02:03 -07001247 free_node = binder_dec_node_nilocked(node, 0, 1);
1248 binder_node_inner_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07001249 if (free_node)
1250 binder_free_node(node);
Todd Kjosadc18842017-06-29 12:01:59 -07001251}
1252
1253static void binder_put_node(struct binder_node *node)
1254{
1255 binder_dec_node_tmpref(node);
1256}
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001257
Todd Kjos2c1838d2017-06-29 12:02:08 -07001258static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc,
1259 u32 desc, bool need_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001260{
1261 struct rb_node *n = proc->refs_by_desc.rb_node;
1262 struct binder_ref *ref;
1263
1264 while (n) {
1265 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1266
Todd Kjos372e3142017-06-29 12:01:58 -07001267 if (desc < ref->data.desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001268 n = n->rb_left;
Todd Kjos372e3142017-06-29 12:01:58 -07001269 } else if (desc > ref->data.desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001270 n = n->rb_right;
Todd Kjos372e3142017-06-29 12:01:58 -07001271 } else if (need_strong_ref && !ref->data.strong) {
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001272 binder_user_error("tried to use weak ref as strong ref\n");
1273 return NULL;
1274 } else {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001275 return ref;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001276 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001277 }
1278 return NULL;
1279}
1280
Todd Kjos372e3142017-06-29 12:01:58 -07001281/**
Todd Kjos2c1838d2017-06-29 12:02:08 -07001282 * binder_get_ref_for_node_olocked() - get the ref associated with given node
Todd Kjos372e3142017-06-29 12:01:58 -07001283 * @proc: binder_proc that owns the ref
1284 * @node: binder_node of target
1285 * @new_ref: newly allocated binder_ref to be initialized or %NULL
1286 *
1287 * Look up the ref for the given node and return it if it exists
1288 *
1289 * If it doesn't exist and the caller provides a newly allocated
1290 * ref, initialize the fields of the newly allocated ref and insert
1291 * into the given proc rb_trees and node refs list.
1292 *
1293 * Return: the ref for node. It is possible that another thread
1294 * allocated/initialized the ref first in which case the
1295 * returned ref would be different than the passed-in
1296 * new_ref. new_ref must be kfree'd by the caller in
1297 * this case.
1298 */
Todd Kjos2c1838d2017-06-29 12:02:08 -07001299static struct binder_ref *binder_get_ref_for_node_olocked(
1300 struct binder_proc *proc,
1301 struct binder_node *node,
1302 struct binder_ref *new_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001303{
Todd Kjos372e3142017-06-29 12:01:58 -07001304 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001305 struct rb_node **p = &proc->refs_by_node.rb_node;
1306 struct rb_node *parent = NULL;
Todd Kjos372e3142017-06-29 12:01:58 -07001307 struct binder_ref *ref;
1308 struct rb_node *n;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001309
1310 while (*p) {
1311 parent = *p;
1312 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1313
1314 if (node < ref->node)
1315 p = &(*p)->rb_left;
1316 else if (node > ref->node)
1317 p = &(*p)->rb_right;
1318 else
1319 return ref;
1320 }
Todd Kjos372e3142017-06-29 12:01:58 -07001321 if (!new_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001322 return NULL;
Todd Kjos372e3142017-06-29 12:01:58 -07001323
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001324 binder_stats_created(BINDER_STAT_REF);
Todd Kjos372e3142017-06-29 12:01:58 -07001325 new_ref->data.debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001326 new_ref->proc = proc;
1327 new_ref->node = node;
1328 rb_link_node(&new_ref->rb_node_node, parent, p);
1329 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1330
Todd Kjos372e3142017-06-29 12:01:58 -07001331 new_ref->data.desc = (node == context->binder_context_mgr_node) ? 0 : 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001332 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1333 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Todd Kjos372e3142017-06-29 12:01:58 -07001334 if (ref->data.desc > new_ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001335 break;
Todd Kjos372e3142017-06-29 12:01:58 -07001336 new_ref->data.desc = ref->data.desc + 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001337 }
1338
1339 p = &proc->refs_by_desc.rb_node;
1340 while (*p) {
1341 parent = *p;
1342 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1343
Todd Kjos372e3142017-06-29 12:01:58 -07001344 if (new_ref->data.desc < ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001345 p = &(*p)->rb_left;
Todd Kjos372e3142017-06-29 12:01:58 -07001346 else if (new_ref->data.desc > ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001347 p = &(*p)->rb_right;
1348 else
1349 BUG();
1350 }
1351 rb_link_node(&new_ref->rb_node_desc, parent, p);
1352 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
Todd Kjos673068e2017-06-29 12:02:03 -07001353
1354 binder_node_lock(node);
Todd Kjose4cffcf2017-06-29 12:01:50 -07001355 hlist_add_head(&new_ref->node_entry, &node->refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001356
Todd Kjose4cffcf2017-06-29 12:01:50 -07001357 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1358 "%d new ref %d desc %d for node %d\n",
Todd Kjos372e3142017-06-29 12:01:58 -07001359 proc->pid, new_ref->data.debug_id, new_ref->data.desc,
Todd Kjose4cffcf2017-06-29 12:01:50 -07001360 node->debug_id);
Todd Kjos673068e2017-06-29 12:02:03 -07001361 binder_node_unlock(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001362 return new_ref;
1363}
1364
Todd Kjos2c1838d2017-06-29 12:02:08 -07001365static void binder_cleanup_ref_olocked(struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001366{
Todd Kjosed297212017-06-29 12:02:01 -07001367 bool delete_node = false;
Todd Kjosed297212017-06-29 12:02:01 -07001368
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001369 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301370 "%d delete ref %d desc %d for node %d\n",
Todd Kjos372e3142017-06-29 12:01:58 -07001371 ref->proc->pid, ref->data.debug_id, ref->data.desc,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301372 ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001373
1374 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1375 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
Todd Kjos372e3142017-06-29 12:01:58 -07001376
Todd Kjos673068e2017-06-29 12:02:03 -07001377 binder_node_inner_lock(ref->node);
Todd Kjos372e3142017-06-29 12:01:58 -07001378 if (ref->data.strong)
Todd Kjos673068e2017-06-29 12:02:03 -07001379 binder_dec_node_nilocked(ref->node, 1, 1);
Todd Kjos372e3142017-06-29 12:01:58 -07001380
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001381 hlist_del(&ref->node_entry);
Todd Kjos673068e2017-06-29 12:02:03 -07001382 delete_node = binder_dec_node_nilocked(ref->node, 0, 1);
1383 binder_node_inner_unlock(ref->node);
Todd Kjosed297212017-06-29 12:02:01 -07001384 /*
1385 * Clear ref->node unless we want the caller to free the node
1386 */
1387 if (!delete_node) {
1388 /*
1389 * The caller uses ref->node to determine
1390 * whether the node needs to be freed. Clear
1391 * it since the node is still alive.
1392 */
1393 ref->node = NULL;
1394 }
Todd Kjos372e3142017-06-29 12:01:58 -07001395
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001396 if (ref->death) {
1397 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301398 "%d delete ref %d desc %d has death notification\n",
Todd Kjos372e3142017-06-29 12:01:58 -07001399 ref->proc->pid, ref->data.debug_id,
1400 ref->data.desc);
Todd Kjos72196392017-06-29 12:02:02 -07001401 binder_dequeue_work(ref->proc, &ref->death->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001402 binder_stats_deleted(BINDER_STAT_DEATH);
1403 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001404 binder_stats_deleted(BINDER_STAT_REF);
1405}
1406
Todd Kjos372e3142017-06-29 12:01:58 -07001407/**
Todd Kjos2c1838d2017-06-29 12:02:08 -07001408 * binder_inc_ref_olocked() - increment the ref for given handle
Todd Kjos372e3142017-06-29 12:01:58 -07001409 * @ref: ref to be incremented
1410 * @strong: if true, strong increment, else weak
1411 * @target_list: list to queue node work on
1412 *
Todd Kjos2c1838d2017-06-29 12:02:08 -07001413 * Increment the ref. @ref->proc->outer_lock must be held on entry
Todd Kjos372e3142017-06-29 12:01:58 -07001414 *
1415 * Return: 0, if successful, else errno
1416 */
Todd Kjos2c1838d2017-06-29 12:02:08 -07001417static int binder_inc_ref_olocked(struct binder_ref *ref, int strong,
1418 struct list_head *target_list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001419{
1420 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +09001421
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001422 if (strong) {
Todd Kjos372e3142017-06-29 12:01:58 -07001423 if (ref->data.strong == 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001424 ret = binder_inc_node(ref->node, 1, 1, target_list);
1425 if (ret)
1426 return ret;
1427 }
Todd Kjos372e3142017-06-29 12:01:58 -07001428 ref->data.strong++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001429 } else {
Todd Kjos372e3142017-06-29 12:01:58 -07001430 if (ref->data.weak == 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001431 ret = binder_inc_node(ref->node, 0, 1, target_list);
1432 if (ret)
1433 return ret;
1434 }
Todd Kjos372e3142017-06-29 12:01:58 -07001435 ref->data.weak++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001436 }
1437 return 0;
1438}
1439
Todd Kjos372e3142017-06-29 12:01:58 -07001440/**
1441 * binder_dec_ref() - dec the ref for given handle
1442 * @ref: ref to be decremented
1443 * @strong: if true, strong decrement, else weak
1444 *
1445 * Decrement the ref.
1446 *
Todd Kjos372e3142017-06-29 12:01:58 -07001447 * Return: true if ref is cleaned up and ready to be freed
1448 */
Todd Kjos2c1838d2017-06-29 12:02:08 -07001449static bool binder_dec_ref_olocked(struct binder_ref *ref, int strong)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001450{
1451 if (strong) {
Todd Kjos372e3142017-06-29 12:01:58 -07001452 if (ref->data.strong == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301453 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
Todd Kjos372e3142017-06-29 12:01:58 -07001454 ref->proc->pid, ref->data.debug_id,
1455 ref->data.desc, ref->data.strong,
1456 ref->data.weak);
1457 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001458 }
Todd Kjos372e3142017-06-29 12:01:58 -07001459 ref->data.strong--;
Todd Kjosed297212017-06-29 12:02:01 -07001460 if (ref->data.strong == 0)
1461 binder_dec_node(ref->node, strong, 1);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001462 } else {
Todd Kjos372e3142017-06-29 12:01:58 -07001463 if (ref->data.weak == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301464 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
Todd Kjos372e3142017-06-29 12:01:58 -07001465 ref->proc->pid, ref->data.debug_id,
1466 ref->data.desc, ref->data.strong,
1467 ref->data.weak);
1468 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001469 }
Todd Kjos372e3142017-06-29 12:01:58 -07001470 ref->data.weak--;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001471 }
Todd Kjos372e3142017-06-29 12:01:58 -07001472 if (ref->data.strong == 0 && ref->data.weak == 0) {
Todd Kjos2c1838d2017-06-29 12:02:08 -07001473 binder_cleanup_ref_olocked(ref);
Todd Kjos372e3142017-06-29 12:01:58 -07001474 return true;
1475 }
1476 return false;
1477}
1478
1479/**
1480 * binder_get_node_from_ref() - get the node from the given proc/desc
1481 * @proc: proc containing the ref
1482 * @desc: the handle associated with the ref
1483 * @need_strong_ref: if true, only return node if ref is strong
1484 * @rdata: the id/refcount data for the ref
1485 *
1486 * Given a proc and ref handle, return the associated binder_node
1487 *
1488 * Return: a binder_node or NULL if not found or not strong when strong required
1489 */
1490static struct binder_node *binder_get_node_from_ref(
1491 struct binder_proc *proc,
1492 u32 desc, bool need_strong_ref,
1493 struct binder_ref_data *rdata)
1494{
1495 struct binder_node *node;
1496 struct binder_ref *ref;
1497
Todd Kjos2c1838d2017-06-29 12:02:08 -07001498 binder_proc_lock(proc);
1499 ref = binder_get_ref_olocked(proc, desc, need_strong_ref);
Todd Kjos372e3142017-06-29 12:01:58 -07001500 if (!ref)
1501 goto err_no_ref;
1502 node = ref->node;
Todd Kjosadc18842017-06-29 12:01:59 -07001503 /*
1504 * Take an implicit reference on the node to ensure
1505 * it stays alive until the call to binder_put_node()
1506 */
1507 binder_inc_node_tmpref(node);
Todd Kjos372e3142017-06-29 12:01:58 -07001508 if (rdata)
1509 *rdata = ref->data;
Todd Kjos2c1838d2017-06-29 12:02:08 -07001510 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07001511
1512 return node;
1513
1514err_no_ref:
Todd Kjos2c1838d2017-06-29 12:02:08 -07001515 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07001516 return NULL;
1517}
1518
1519/**
1520 * binder_free_ref() - free the binder_ref
1521 * @ref: ref to free
1522 *
Todd Kjosed297212017-06-29 12:02:01 -07001523 * Free the binder_ref. Free the binder_node indicated by ref->node
1524 * (if non-NULL) and the binder_ref_death indicated by ref->death.
Todd Kjos372e3142017-06-29 12:01:58 -07001525 */
1526static void binder_free_ref(struct binder_ref *ref)
1527{
Todd Kjosed297212017-06-29 12:02:01 -07001528 if (ref->node)
1529 binder_free_node(ref->node);
Todd Kjos372e3142017-06-29 12:01:58 -07001530 kfree(ref->death);
1531 kfree(ref);
1532}
1533
1534/**
1535 * binder_update_ref_for_handle() - inc/dec the ref for given handle
1536 * @proc: proc containing the ref
1537 * @desc: the handle associated with the ref
1538 * @increment: true=inc reference, false=dec reference
1539 * @strong: true=strong reference, false=weak reference
1540 * @rdata: the id/refcount data for the ref
1541 *
1542 * Given a proc and ref handle, increment or decrement the ref
1543 * according to "increment" arg.
1544 *
1545 * Return: 0 if successful, else errno
1546 */
1547static int binder_update_ref_for_handle(struct binder_proc *proc,
1548 uint32_t desc, bool increment, bool strong,
1549 struct binder_ref_data *rdata)
1550{
1551 int ret = 0;
1552 struct binder_ref *ref;
1553 bool delete_ref = false;
1554
Todd Kjos2c1838d2017-06-29 12:02:08 -07001555 binder_proc_lock(proc);
1556 ref = binder_get_ref_olocked(proc, desc, strong);
Todd Kjos372e3142017-06-29 12:01:58 -07001557 if (!ref) {
1558 ret = -EINVAL;
1559 goto err_no_ref;
1560 }
1561 if (increment)
Todd Kjos2c1838d2017-06-29 12:02:08 -07001562 ret = binder_inc_ref_olocked(ref, strong, NULL);
Todd Kjos372e3142017-06-29 12:01:58 -07001563 else
Todd Kjos2c1838d2017-06-29 12:02:08 -07001564 delete_ref = binder_dec_ref_olocked(ref, strong);
Todd Kjos372e3142017-06-29 12:01:58 -07001565
1566 if (rdata)
1567 *rdata = ref->data;
Todd Kjos2c1838d2017-06-29 12:02:08 -07001568 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07001569
1570 if (delete_ref)
1571 binder_free_ref(ref);
1572 return ret;
1573
1574err_no_ref:
Todd Kjos2c1838d2017-06-29 12:02:08 -07001575 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07001576 return ret;
1577}
1578
1579/**
1580 * binder_dec_ref_for_handle() - dec the ref for given handle
1581 * @proc: proc containing the ref
1582 * @desc: the handle associated with the ref
1583 * @strong: true=strong reference, false=weak reference
1584 * @rdata: the id/refcount data for the ref
1585 *
1586 * Just calls binder_update_ref_for_handle() to decrement the ref.
1587 *
1588 * Return: 0 if successful, else errno
1589 */
1590static int binder_dec_ref_for_handle(struct binder_proc *proc,
1591 uint32_t desc, bool strong, struct binder_ref_data *rdata)
1592{
1593 return binder_update_ref_for_handle(proc, desc, false, strong, rdata);
1594}
1595
1596
1597/**
1598 * binder_inc_ref_for_node() - increment the ref for given proc/node
1599 * @proc: proc containing the ref
1600 * @node: target node
1601 * @strong: true=strong reference, false=weak reference
1602 * @target_list: worklist to use if node is incremented
1603 * @rdata: the id/refcount data for the ref
1604 *
1605 * Given a proc and node, increment the ref. Create the ref if it
1606 * doesn't already exist
1607 *
1608 * Return: 0 if successful, else errno
1609 */
1610static int binder_inc_ref_for_node(struct binder_proc *proc,
1611 struct binder_node *node,
1612 bool strong,
1613 struct list_head *target_list,
1614 struct binder_ref_data *rdata)
1615{
1616 struct binder_ref *ref;
1617 struct binder_ref *new_ref = NULL;
1618 int ret = 0;
1619
Todd Kjos2c1838d2017-06-29 12:02:08 -07001620 binder_proc_lock(proc);
1621 ref = binder_get_ref_for_node_olocked(proc, node, NULL);
Todd Kjos372e3142017-06-29 12:01:58 -07001622 if (!ref) {
Todd Kjos2c1838d2017-06-29 12:02:08 -07001623 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07001624 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1625 if (!new_ref)
1626 return -ENOMEM;
Todd Kjos2c1838d2017-06-29 12:02:08 -07001627 binder_proc_lock(proc);
1628 ref = binder_get_ref_for_node_olocked(proc, node, new_ref);
Todd Kjos372e3142017-06-29 12:01:58 -07001629 }
Todd Kjos2c1838d2017-06-29 12:02:08 -07001630 ret = binder_inc_ref_olocked(ref, strong, target_list);
Todd Kjos372e3142017-06-29 12:01:58 -07001631 *rdata = ref->data;
Todd Kjos2c1838d2017-06-29 12:02:08 -07001632 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07001633 if (new_ref && ref != new_ref)
1634 /*
1635 * Another thread created the ref first so
1636 * free the one we allocated
1637 */
1638 kfree(new_ref);
1639 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001640}
1641
Martijn Coenen0b89d692017-06-29 12:02:06 -07001642static void binder_pop_transaction_ilocked(struct binder_thread *target_thread,
1643 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001644{
Todd Kjosb6d282c2017-06-29 12:01:54 -07001645 BUG_ON(!target_thread);
Martijn Coenen0b89d692017-06-29 12:02:06 -07001646 BUG_ON(!spin_is_locked(&target_thread->proc->inner_lock));
Todd Kjosb6d282c2017-06-29 12:01:54 -07001647 BUG_ON(target_thread->transaction_stack != t);
1648 BUG_ON(target_thread->transaction_stack->from != target_thread);
1649 target_thread->transaction_stack =
1650 target_thread->transaction_stack->from_parent;
1651 t->from = NULL;
1652}
1653
Todd Kjos7a4408c2017-06-29 12:01:57 -07001654/**
1655 * binder_thread_dec_tmpref() - decrement thread->tmp_ref
1656 * @thread: thread to decrement
1657 *
1658 * A thread needs to be kept alive while being used to create or
1659 * handle a transaction. binder_get_txn_from() is used to safely
1660 * extract t->from from a binder_transaction and keep the thread
1661 * indicated by t->from from being freed. When done with that
1662 * binder_thread, this function is called to decrement the
1663 * tmp_ref and free if appropriate (thread has been released
1664 * and no transaction being processed by the driver)
1665 */
1666static void binder_thread_dec_tmpref(struct binder_thread *thread)
1667{
1668 /*
1669 * atomic is used to protect the counter value while
1670 * it cannot reach zero or thread->is_dead is false
Todd Kjos7a4408c2017-06-29 12:01:57 -07001671 */
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07001672 binder_inner_proc_lock(thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07001673 atomic_dec(&thread->tmp_ref);
1674 if (thread->is_dead && !atomic_read(&thread->tmp_ref)) {
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07001675 binder_inner_proc_unlock(thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07001676 binder_free_thread(thread);
1677 return;
1678 }
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07001679 binder_inner_proc_unlock(thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07001680}
1681
1682/**
1683 * binder_proc_dec_tmpref() - decrement proc->tmp_ref
1684 * @proc: proc to decrement
1685 *
1686 * A binder_proc needs to be kept alive while being used to create or
1687 * handle a transaction. proc->tmp_ref is incremented when
1688 * creating a new transaction or the binder_proc is currently in-use
1689 * by threads that are being released. When done with the binder_proc,
1690 * this function is called to decrement the counter and free the
1691 * proc if appropriate (proc has been released, all threads have
1692 * been released and not currenly in-use to process a transaction).
1693 */
1694static void binder_proc_dec_tmpref(struct binder_proc *proc)
1695{
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07001696 binder_inner_proc_lock(proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07001697 proc->tmp_ref--;
1698 if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) &&
1699 !proc->tmp_ref) {
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07001700 binder_inner_proc_unlock(proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07001701 binder_free_proc(proc);
1702 return;
1703 }
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07001704 binder_inner_proc_unlock(proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07001705}
1706
1707/**
1708 * binder_get_txn_from() - safely extract the "from" thread in transaction
1709 * @t: binder transaction for t->from
1710 *
1711 * Atomically return the "from" thread and increment the tmp_ref
1712 * count for the thread to ensure it stays alive until
1713 * binder_thread_dec_tmpref() is called.
1714 *
1715 * Return: the value of t->from
1716 */
1717static struct binder_thread *binder_get_txn_from(
1718 struct binder_transaction *t)
1719{
1720 struct binder_thread *from;
1721
1722 spin_lock(&t->lock);
1723 from = t->from;
1724 if (from)
1725 atomic_inc(&from->tmp_ref);
1726 spin_unlock(&t->lock);
1727 return from;
1728}
1729
Martijn Coenen0b89d692017-06-29 12:02:06 -07001730/**
1731 * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock
1732 * @t: binder transaction for t->from
1733 *
1734 * Same as binder_get_txn_from() except it also acquires the proc->inner_lock
1735 * to guarantee that the thread cannot be released while operating on it.
1736 * The caller must call binder_inner_proc_unlock() to release the inner lock
1737 * as well as call binder_dec_thread_txn() to release the reference.
1738 *
1739 * Return: the value of t->from
1740 */
1741static struct binder_thread *binder_get_txn_from_and_acq_inner(
1742 struct binder_transaction *t)
1743{
1744 struct binder_thread *from;
1745
1746 from = binder_get_txn_from(t);
1747 if (!from)
1748 return NULL;
1749 binder_inner_proc_lock(from->proc);
1750 if (t->from) {
1751 BUG_ON(from != t->from);
1752 return from;
1753 }
1754 binder_inner_proc_unlock(from->proc);
1755 binder_thread_dec_tmpref(from);
1756 return NULL;
1757}
1758
Todd Kjosb6d282c2017-06-29 12:01:54 -07001759static void binder_free_transaction(struct binder_transaction *t)
1760{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001761 if (t->buffer)
1762 t->buffer->transaction = NULL;
1763 kfree(t);
1764 binder_stats_deleted(BINDER_STAT_TRANSACTION);
1765}
1766
1767static void binder_send_failed_reply(struct binder_transaction *t,
1768 uint32_t error_code)
1769{
1770 struct binder_thread *target_thread;
Lucas Tanured4ec15e2014-07-13 21:31:05 -03001771 struct binder_transaction *next;
Seunghun Lee10f62862014-05-01 01:30:23 +09001772
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001773 BUG_ON(t->flags & TF_ONE_WAY);
1774 while (1) {
Martijn Coenen0b89d692017-06-29 12:02:06 -07001775 target_thread = binder_get_txn_from_and_acq_inner(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001776 if (target_thread) {
Todd Kjos26549d12017-06-29 12:01:55 -07001777 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1778 "send failed reply for transaction %d to %d:%d\n",
1779 t->debug_id,
1780 target_thread->proc->pid,
1781 target_thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001782
Martijn Coenen0b89d692017-06-29 12:02:06 -07001783 binder_pop_transaction_ilocked(target_thread, t);
Todd Kjos26549d12017-06-29 12:01:55 -07001784 if (target_thread->reply_error.cmd == BR_OK) {
1785 target_thread->reply_error.cmd = error_code;
Martijn Coenen0b89d692017-06-29 12:02:06 -07001786 binder_enqueue_work_ilocked(
Todd Kjos72196392017-06-29 12:02:02 -07001787 &target_thread->reply_error.work,
Todd Kjos26549d12017-06-29 12:01:55 -07001788 &target_thread->todo);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001789 wake_up_interruptible(&target_thread->wait);
1790 } else {
Todd Kjos26549d12017-06-29 12:01:55 -07001791 WARN(1, "Unexpected reply error: %u\n",
1792 target_thread->reply_error.cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001793 }
Martijn Coenen0b89d692017-06-29 12:02:06 -07001794 binder_inner_proc_unlock(target_thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07001795 binder_thread_dec_tmpref(target_thread);
Todd Kjos26549d12017-06-29 12:01:55 -07001796 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001797 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001798 }
Lucas Tanured4ec15e2014-07-13 21:31:05 -03001799 next = t->from_parent;
1800
1801 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1802 "send failed reply for transaction %d, target dead\n",
1803 t->debug_id);
1804
Todd Kjosb6d282c2017-06-29 12:01:54 -07001805 binder_free_transaction(t);
Lucas Tanured4ec15e2014-07-13 21:31:05 -03001806 if (next == NULL) {
1807 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1808 "reply failed, no target thread at root\n");
1809 return;
1810 }
1811 t = next;
1812 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1813 "reply failed, no target thread -- retry %d\n",
1814 t->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001815 }
1816}
1817
Martijn Coenenfeba3902017-02-03 14:40:45 -08001818/**
1819 * binder_validate_object() - checks for a valid metadata object in a buffer.
1820 * @buffer: binder_buffer that we're parsing.
1821 * @offset: offset in the buffer at which to validate an object.
1822 *
1823 * Return: If there's a valid metadata object at @offset in @buffer, the
1824 * size of that object. Otherwise, it returns zero.
1825 */
1826static size_t binder_validate_object(struct binder_buffer *buffer, u64 offset)
1827{
1828 /* Check if we can read a header first */
1829 struct binder_object_header *hdr;
1830 size_t object_size = 0;
1831
1832 if (offset > buffer->data_size - sizeof(*hdr) ||
1833 buffer->data_size < sizeof(*hdr) ||
1834 !IS_ALIGNED(offset, sizeof(u32)))
1835 return 0;
1836
1837 /* Ok, now see if we can read a complete object. */
1838 hdr = (struct binder_object_header *)(buffer->data + offset);
1839 switch (hdr->type) {
1840 case BINDER_TYPE_BINDER:
1841 case BINDER_TYPE_WEAK_BINDER:
1842 case BINDER_TYPE_HANDLE:
1843 case BINDER_TYPE_WEAK_HANDLE:
1844 object_size = sizeof(struct flat_binder_object);
1845 break;
1846 case BINDER_TYPE_FD:
1847 object_size = sizeof(struct binder_fd_object);
1848 break;
Martijn Coenen79802402017-02-03 14:40:51 -08001849 case BINDER_TYPE_PTR:
1850 object_size = sizeof(struct binder_buffer_object);
1851 break;
Martijn Coenendef95c72017-02-03 14:40:52 -08001852 case BINDER_TYPE_FDA:
1853 object_size = sizeof(struct binder_fd_array_object);
1854 break;
Martijn Coenenfeba3902017-02-03 14:40:45 -08001855 default:
1856 return 0;
1857 }
1858 if (offset <= buffer->data_size - object_size &&
1859 buffer->data_size >= object_size)
1860 return object_size;
1861 else
1862 return 0;
1863}
1864
Martijn Coenen79802402017-02-03 14:40:51 -08001865/**
1866 * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
1867 * @b: binder_buffer containing the object
1868 * @index: index in offset array at which the binder_buffer_object is
1869 * located
1870 * @start: points to the start of the offset array
1871 * @num_valid: the number of valid offsets in the offset array
1872 *
1873 * Return: If @index is within the valid range of the offset array
1874 * described by @start and @num_valid, and if there's a valid
1875 * binder_buffer_object at the offset found in index @index
1876 * of the offset array, that object is returned. Otherwise,
1877 * %NULL is returned.
1878 * Note that the offset found in index @index itself is not
1879 * verified; this function assumes that @num_valid elements
1880 * from @start were previously verified to have valid offsets.
1881 */
1882static struct binder_buffer_object *binder_validate_ptr(struct binder_buffer *b,
1883 binder_size_t index,
1884 binder_size_t *start,
1885 binder_size_t num_valid)
1886{
1887 struct binder_buffer_object *buffer_obj;
1888 binder_size_t *offp;
1889
1890 if (index >= num_valid)
1891 return NULL;
1892
1893 offp = start + index;
1894 buffer_obj = (struct binder_buffer_object *)(b->data + *offp);
1895 if (buffer_obj->hdr.type != BINDER_TYPE_PTR)
1896 return NULL;
1897
1898 return buffer_obj;
1899}
1900
1901/**
1902 * binder_validate_fixup() - validates pointer/fd fixups happen in order.
1903 * @b: transaction buffer
1904 * @objects_start start of objects buffer
1905 * @buffer: binder_buffer_object in which to fix up
1906 * @offset: start offset in @buffer to fix up
1907 * @last_obj: last binder_buffer_object that we fixed up in
1908 * @last_min_offset: minimum fixup offset in @last_obj
1909 *
1910 * Return: %true if a fixup in buffer @buffer at offset @offset is
1911 * allowed.
1912 *
1913 * For safety reasons, we only allow fixups inside a buffer to happen
1914 * at increasing offsets; additionally, we only allow fixup on the last
1915 * buffer object that was verified, or one of its parents.
1916 *
1917 * Example of what is allowed:
1918 *
1919 * A
1920 * B (parent = A, offset = 0)
1921 * C (parent = A, offset = 16)
1922 * D (parent = C, offset = 0)
1923 * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
1924 *
1925 * Examples of what is not allowed:
1926 *
1927 * Decreasing offsets within the same parent:
1928 * A
1929 * C (parent = A, offset = 16)
1930 * B (parent = A, offset = 0) // decreasing offset within A
1931 *
1932 * Referring to a parent that wasn't the last object or any of its parents:
1933 * A
1934 * B (parent = A, offset = 0)
1935 * C (parent = A, offset = 0)
1936 * C (parent = A, offset = 16)
1937 * D (parent = B, offset = 0) // B is not A or any of A's parents
1938 */
1939static bool binder_validate_fixup(struct binder_buffer *b,
1940 binder_size_t *objects_start,
1941 struct binder_buffer_object *buffer,
1942 binder_size_t fixup_offset,
1943 struct binder_buffer_object *last_obj,
1944 binder_size_t last_min_offset)
1945{
1946 if (!last_obj) {
1947 /* Nothing to fix up in */
1948 return false;
1949 }
1950
1951 while (last_obj != buffer) {
1952 /*
1953 * Safe to retrieve the parent of last_obj, since it
1954 * was already previously verified by the driver.
1955 */
1956 if ((last_obj->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0)
1957 return false;
1958 last_min_offset = last_obj->parent_offset + sizeof(uintptr_t);
1959 last_obj = (struct binder_buffer_object *)
1960 (b->data + *(objects_start + last_obj->parent));
1961 }
1962 return (fixup_offset >= last_min_offset);
1963}
1964
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001965static void binder_transaction_buffer_release(struct binder_proc *proc,
1966 struct binder_buffer *buffer,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001967 binder_size_t *failed_at)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001968{
Martijn Coenen79802402017-02-03 14:40:51 -08001969 binder_size_t *offp, *off_start, *off_end;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001970 int debug_id = buffer->debug_id;
1971
1972 binder_debug(BINDER_DEBUG_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301973 "%d buffer release %d, size %zd-%zd, failed at %p\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001974 proc->pid, buffer->debug_id,
1975 buffer->data_size, buffer->offsets_size, failed_at);
1976
1977 if (buffer->target_node)
1978 binder_dec_node(buffer->target_node, 1, 0);
1979
Martijn Coenen79802402017-02-03 14:40:51 -08001980 off_start = (binder_size_t *)(buffer->data +
1981 ALIGN(buffer->data_size, sizeof(void *)));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001982 if (failed_at)
1983 off_end = failed_at;
1984 else
Martijn Coenen79802402017-02-03 14:40:51 -08001985 off_end = (void *)off_start + buffer->offsets_size;
1986 for (offp = off_start; offp < off_end; offp++) {
Martijn Coenenfeba3902017-02-03 14:40:45 -08001987 struct binder_object_header *hdr;
1988 size_t object_size = binder_validate_object(buffer, *offp);
Seunghun Lee10f62862014-05-01 01:30:23 +09001989
Martijn Coenenfeba3902017-02-03 14:40:45 -08001990 if (object_size == 0) {
1991 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
Arve Hjønnevågda498892014-02-21 14:40:26 -08001992 debug_id, (u64)*offp, buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001993 continue;
1994 }
Martijn Coenenfeba3902017-02-03 14:40:45 -08001995 hdr = (struct binder_object_header *)(buffer->data + *offp);
1996 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001997 case BINDER_TYPE_BINDER:
1998 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08001999 struct flat_binder_object *fp;
2000 struct binder_node *node;
Seunghun Lee10f62862014-05-01 01:30:23 +09002001
Martijn Coenenfeba3902017-02-03 14:40:45 -08002002 fp = to_flat_binder_object(hdr);
2003 node = binder_get_node(proc, fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002004 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002005 pr_err("transaction release %d bad node %016llx\n",
2006 debug_id, (u64)fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002007 break;
2008 }
2009 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002010 " node %d u%016llx\n",
2011 node->debug_id, (u64)node->ptr);
Martijn Coenenfeba3902017-02-03 14:40:45 -08002012 binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER,
2013 0);
Todd Kjosadc18842017-06-29 12:01:59 -07002014 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002015 } break;
2016 case BINDER_TYPE_HANDLE:
2017 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08002018 struct flat_binder_object *fp;
Todd Kjos372e3142017-06-29 12:01:58 -07002019 struct binder_ref_data rdata;
2020 int ret;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02002021
Martijn Coenenfeba3902017-02-03 14:40:45 -08002022 fp = to_flat_binder_object(hdr);
Todd Kjos372e3142017-06-29 12:01:58 -07002023 ret = binder_dec_ref_for_handle(proc, fp->handle,
2024 hdr->type == BINDER_TYPE_HANDLE, &rdata);
2025
2026 if (ret) {
2027 pr_err("transaction release %d bad handle %d, ret = %d\n",
2028 debug_id, fp->handle, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002029 break;
2030 }
2031 binder_debug(BINDER_DEBUG_TRANSACTION,
Todd Kjos372e3142017-06-29 12:01:58 -07002032 " ref %d desc %d\n",
2033 rdata.debug_id, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002034 } break;
2035
Martijn Coenenfeba3902017-02-03 14:40:45 -08002036 case BINDER_TYPE_FD: {
2037 struct binder_fd_object *fp = to_binder_fd_object(hdr);
2038
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002039 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenenfeba3902017-02-03 14:40:45 -08002040 " fd %d\n", fp->fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002041 if (failed_at)
Martijn Coenenfeba3902017-02-03 14:40:45 -08002042 task_close_fd(proc, fp->fd);
2043 } break;
Martijn Coenen79802402017-02-03 14:40:51 -08002044 case BINDER_TYPE_PTR:
2045 /*
2046 * Nothing to do here, this will get cleaned up when the
2047 * transaction buffer gets freed
2048 */
2049 break;
Martijn Coenendef95c72017-02-03 14:40:52 -08002050 case BINDER_TYPE_FDA: {
2051 struct binder_fd_array_object *fda;
2052 struct binder_buffer_object *parent;
2053 uintptr_t parent_buffer;
2054 u32 *fd_array;
2055 size_t fd_index;
2056 binder_size_t fd_buf_size;
2057
2058 fda = to_binder_fd_array_object(hdr);
2059 parent = binder_validate_ptr(buffer, fda->parent,
2060 off_start,
2061 offp - off_start);
2062 if (!parent) {
2063 pr_err("transaction release %d bad parent offset",
2064 debug_id);
2065 continue;
2066 }
2067 /*
2068 * Since the parent was already fixed up, convert it
2069 * back to kernel address space to access it
2070 */
2071 parent_buffer = parent->buffer -
Todd Kjos19c98722017-06-29 12:01:40 -07002072 binder_alloc_get_user_buffer_offset(
2073 &proc->alloc);
Martijn Coenendef95c72017-02-03 14:40:52 -08002074
2075 fd_buf_size = sizeof(u32) * fda->num_fds;
2076 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2077 pr_err("transaction release %d invalid number of fds (%lld)\n",
2078 debug_id, (u64)fda->num_fds);
2079 continue;
2080 }
2081 if (fd_buf_size > parent->length ||
2082 fda->parent_offset > parent->length - fd_buf_size) {
2083 /* No space for all file descriptors here. */
2084 pr_err("transaction release %d not enough space for %lld fds in buffer\n",
2085 debug_id, (u64)fda->num_fds);
2086 continue;
2087 }
2088 fd_array = (u32 *)(parent_buffer + fda->parent_offset);
2089 for (fd_index = 0; fd_index < fda->num_fds; fd_index++)
2090 task_close_fd(proc, fd_array[fd_index]);
2091 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002092 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01002093 pr_err("transaction release %d bad object type %x\n",
Martijn Coenenfeba3902017-02-03 14:40:45 -08002094 debug_id, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002095 break;
2096 }
2097 }
2098}
2099
Martijn Coenena056af42017-02-03 14:40:49 -08002100static int binder_translate_binder(struct flat_binder_object *fp,
2101 struct binder_transaction *t,
2102 struct binder_thread *thread)
2103{
2104 struct binder_node *node;
Martijn Coenena056af42017-02-03 14:40:49 -08002105 struct binder_proc *proc = thread->proc;
2106 struct binder_proc *target_proc = t->to_proc;
Todd Kjos372e3142017-06-29 12:01:58 -07002107 struct binder_ref_data rdata;
Todd Kjosadc18842017-06-29 12:01:59 -07002108 int ret = 0;
Martijn Coenena056af42017-02-03 14:40:49 -08002109
2110 node = binder_get_node(proc, fp->binder);
2111 if (!node) {
Todd Kjos673068e2017-06-29 12:02:03 -07002112 node = binder_new_node(proc, fp);
Martijn Coenena056af42017-02-03 14:40:49 -08002113 if (!node)
2114 return -ENOMEM;
Martijn Coenena056af42017-02-03 14:40:49 -08002115 }
2116 if (fp->cookie != node->cookie) {
2117 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
2118 proc->pid, thread->pid, (u64)fp->binder,
2119 node->debug_id, (u64)fp->cookie,
2120 (u64)node->cookie);
Todd Kjosadc18842017-06-29 12:01:59 -07002121 ret = -EINVAL;
2122 goto done;
Martijn Coenena056af42017-02-03 14:40:49 -08002123 }
Todd Kjosadc18842017-06-29 12:01:59 -07002124 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2125 ret = -EPERM;
2126 goto done;
2127 }
Martijn Coenena056af42017-02-03 14:40:49 -08002128
Todd Kjos372e3142017-06-29 12:01:58 -07002129 ret = binder_inc_ref_for_node(target_proc, node,
2130 fp->hdr.type == BINDER_TYPE_BINDER,
2131 &thread->todo, &rdata);
2132 if (ret)
Todd Kjosadc18842017-06-29 12:01:59 -07002133 goto done;
Martijn Coenena056af42017-02-03 14:40:49 -08002134
2135 if (fp->hdr.type == BINDER_TYPE_BINDER)
2136 fp->hdr.type = BINDER_TYPE_HANDLE;
2137 else
2138 fp->hdr.type = BINDER_TYPE_WEAK_HANDLE;
2139 fp->binder = 0;
Todd Kjos372e3142017-06-29 12:01:58 -07002140 fp->handle = rdata.desc;
Martijn Coenena056af42017-02-03 14:40:49 -08002141 fp->cookie = 0;
Martijn Coenena056af42017-02-03 14:40:49 -08002142
Todd Kjos372e3142017-06-29 12:01:58 -07002143 trace_binder_transaction_node_to_ref(t, node, &rdata);
Martijn Coenena056af42017-02-03 14:40:49 -08002144 binder_debug(BINDER_DEBUG_TRANSACTION,
2145 " node %d u%016llx -> ref %d desc %d\n",
2146 node->debug_id, (u64)node->ptr,
Todd Kjos372e3142017-06-29 12:01:58 -07002147 rdata.debug_id, rdata.desc);
Todd Kjosadc18842017-06-29 12:01:59 -07002148done:
2149 binder_put_node(node);
2150 return ret;
Martijn Coenena056af42017-02-03 14:40:49 -08002151}
2152
2153static int binder_translate_handle(struct flat_binder_object *fp,
2154 struct binder_transaction *t,
2155 struct binder_thread *thread)
2156{
Martijn Coenena056af42017-02-03 14:40:49 -08002157 struct binder_proc *proc = thread->proc;
2158 struct binder_proc *target_proc = t->to_proc;
Todd Kjos372e3142017-06-29 12:01:58 -07002159 struct binder_node *node;
2160 struct binder_ref_data src_rdata;
Todd Kjosadc18842017-06-29 12:01:59 -07002161 int ret = 0;
Martijn Coenena056af42017-02-03 14:40:49 -08002162
Todd Kjos372e3142017-06-29 12:01:58 -07002163 node = binder_get_node_from_ref(proc, fp->handle,
2164 fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata);
2165 if (!node) {
Martijn Coenena056af42017-02-03 14:40:49 -08002166 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
2167 proc->pid, thread->pid, fp->handle);
2168 return -EINVAL;
2169 }
Todd Kjosadc18842017-06-29 12:01:59 -07002170 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2171 ret = -EPERM;
2172 goto done;
2173 }
Martijn Coenena056af42017-02-03 14:40:49 -08002174
Todd Kjos673068e2017-06-29 12:02:03 -07002175 binder_node_lock(node);
Todd Kjos372e3142017-06-29 12:01:58 -07002176 if (node->proc == target_proc) {
Martijn Coenena056af42017-02-03 14:40:49 -08002177 if (fp->hdr.type == BINDER_TYPE_HANDLE)
2178 fp->hdr.type = BINDER_TYPE_BINDER;
2179 else
2180 fp->hdr.type = BINDER_TYPE_WEAK_BINDER;
Todd Kjos372e3142017-06-29 12:01:58 -07002181 fp->binder = node->ptr;
2182 fp->cookie = node->cookie;
Todd Kjos673068e2017-06-29 12:02:03 -07002183 if (node->proc)
2184 binder_inner_proc_lock(node->proc);
2185 binder_inc_node_nilocked(node,
2186 fp->hdr.type == BINDER_TYPE_BINDER,
2187 0, NULL);
2188 if (node->proc)
2189 binder_inner_proc_unlock(node->proc);
Todd Kjos372e3142017-06-29 12:01:58 -07002190 trace_binder_transaction_ref_to_node(t, node, &src_rdata);
Martijn Coenena056af42017-02-03 14:40:49 -08002191 binder_debug(BINDER_DEBUG_TRANSACTION,
2192 " ref %d desc %d -> node %d u%016llx\n",
Todd Kjos372e3142017-06-29 12:01:58 -07002193 src_rdata.debug_id, src_rdata.desc, node->debug_id,
2194 (u64)node->ptr);
Todd Kjos673068e2017-06-29 12:02:03 -07002195 binder_node_unlock(node);
Martijn Coenena056af42017-02-03 14:40:49 -08002196 } else {
Todd Kjos372e3142017-06-29 12:01:58 -07002197 int ret;
2198 struct binder_ref_data dest_rdata;
Martijn Coenena056af42017-02-03 14:40:49 -08002199
Todd Kjos673068e2017-06-29 12:02:03 -07002200 binder_node_unlock(node);
Todd Kjos372e3142017-06-29 12:01:58 -07002201 ret = binder_inc_ref_for_node(target_proc, node,
2202 fp->hdr.type == BINDER_TYPE_HANDLE,
2203 NULL, &dest_rdata);
2204 if (ret)
Todd Kjosadc18842017-06-29 12:01:59 -07002205 goto done;
Martijn Coenena056af42017-02-03 14:40:49 -08002206
2207 fp->binder = 0;
Todd Kjos372e3142017-06-29 12:01:58 -07002208 fp->handle = dest_rdata.desc;
Martijn Coenena056af42017-02-03 14:40:49 -08002209 fp->cookie = 0;
Todd Kjos372e3142017-06-29 12:01:58 -07002210 trace_binder_transaction_ref_to_ref(t, node, &src_rdata,
2211 &dest_rdata);
Martijn Coenena056af42017-02-03 14:40:49 -08002212 binder_debug(BINDER_DEBUG_TRANSACTION,
2213 " ref %d desc %d -> ref %d desc %d (node %d)\n",
Todd Kjos372e3142017-06-29 12:01:58 -07002214 src_rdata.debug_id, src_rdata.desc,
2215 dest_rdata.debug_id, dest_rdata.desc,
2216 node->debug_id);
Martijn Coenena056af42017-02-03 14:40:49 -08002217 }
Todd Kjosadc18842017-06-29 12:01:59 -07002218done:
2219 binder_put_node(node);
2220 return ret;
Martijn Coenena056af42017-02-03 14:40:49 -08002221}
2222
2223static int binder_translate_fd(int fd,
2224 struct binder_transaction *t,
2225 struct binder_thread *thread,
2226 struct binder_transaction *in_reply_to)
2227{
2228 struct binder_proc *proc = thread->proc;
2229 struct binder_proc *target_proc = t->to_proc;
2230 int target_fd;
2231 struct file *file;
2232 int ret;
2233 bool target_allows_fd;
2234
2235 if (in_reply_to)
2236 target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS);
2237 else
2238 target_allows_fd = t->buffer->target_node->accept_fds;
2239 if (!target_allows_fd) {
2240 binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
2241 proc->pid, thread->pid,
2242 in_reply_to ? "reply" : "transaction",
2243 fd);
2244 ret = -EPERM;
2245 goto err_fd_not_accepted;
2246 }
2247
2248 file = fget(fd);
2249 if (!file) {
2250 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
2251 proc->pid, thread->pid, fd);
2252 ret = -EBADF;
2253 goto err_fget;
2254 }
2255 ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file);
2256 if (ret < 0) {
2257 ret = -EPERM;
2258 goto err_security;
2259 }
2260
2261 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
2262 if (target_fd < 0) {
2263 ret = -ENOMEM;
2264 goto err_get_unused_fd;
2265 }
2266 task_fd_install(target_proc, target_fd, file);
2267 trace_binder_transaction_fd(t, fd, target_fd);
2268 binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n",
2269 fd, target_fd);
2270
2271 return target_fd;
2272
2273err_get_unused_fd:
2274err_security:
2275 fput(file);
2276err_fget:
2277err_fd_not_accepted:
2278 return ret;
2279}
2280
Martijn Coenendef95c72017-02-03 14:40:52 -08002281static int binder_translate_fd_array(struct binder_fd_array_object *fda,
2282 struct binder_buffer_object *parent,
2283 struct binder_transaction *t,
2284 struct binder_thread *thread,
2285 struct binder_transaction *in_reply_to)
2286{
2287 binder_size_t fdi, fd_buf_size, num_installed_fds;
2288 int target_fd;
2289 uintptr_t parent_buffer;
2290 u32 *fd_array;
2291 struct binder_proc *proc = thread->proc;
2292 struct binder_proc *target_proc = t->to_proc;
2293
2294 fd_buf_size = sizeof(u32) * fda->num_fds;
2295 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2296 binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
2297 proc->pid, thread->pid, (u64)fda->num_fds);
2298 return -EINVAL;
2299 }
2300 if (fd_buf_size > parent->length ||
2301 fda->parent_offset > parent->length - fd_buf_size) {
2302 /* No space for all file descriptors here. */
2303 binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
2304 proc->pid, thread->pid, (u64)fda->num_fds);
2305 return -EINVAL;
2306 }
2307 /*
2308 * Since the parent was already fixed up, convert it
2309 * back to the kernel address space to access it
2310 */
Todd Kjos19c98722017-06-29 12:01:40 -07002311 parent_buffer = parent->buffer -
2312 binder_alloc_get_user_buffer_offset(&target_proc->alloc);
Martijn Coenendef95c72017-02-03 14:40:52 -08002313 fd_array = (u32 *)(parent_buffer + fda->parent_offset);
2314 if (!IS_ALIGNED((unsigned long)fd_array, sizeof(u32))) {
2315 binder_user_error("%d:%d parent offset not aligned correctly.\n",
2316 proc->pid, thread->pid);
2317 return -EINVAL;
2318 }
2319 for (fdi = 0; fdi < fda->num_fds; fdi++) {
2320 target_fd = binder_translate_fd(fd_array[fdi], t, thread,
2321 in_reply_to);
2322 if (target_fd < 0)
2323 goto err_translate_fd_failed;
2324 fd_array[fdi] = target_fd;
2325 }
2326 return 0;
2327
2328err_translate_fd_failed:
2329 /*
2330 * Failed to allocate fd or security error, free fds
2331 * installed so far.
2332 */
2333 num_installed_fds = fdi;
2334 for (fdi = 0; fdi < num_installed_fds; fdi++)
2335 task_close_fd(target_proc, fd_array[fdi]);
2336 return target_fd;
2337}
2338
Martijn Coenen79802402017-02-03 14:40:51 -08002339static int binder_fixup_parent(struct binder_transaction *t,
2340 struct binder_thread *thread,
2341 struct binder_buffer_object *bp,
2342 binder_size_t *off_start,
2343 binder_size_t num_valid,
2344 struct binder_buffer_object *last_fixup_obj,
2345 binder_size_t last_fixup_min_off)
2346{
2347 struct binder_buffer_object *parent;
2348 u8 *parent_buffer;
2349 struct binder_buffer *b = t->buffer;
2350 struct binder_proc *proc = thread->proc;
2351 struct binder_proc *target_proc = t->to_proc;
2352
2353 if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT))
2354 return 0;
2355
2356 parent = binder_validate_ptr(b, bp->parent, off_start, num_valid);
2357 if (!parent) {
2358 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
2359 proc->pid, thread->pid);
2360 return -EINVAL;
2361 }
2362
2363 if (!binder_validate_fixup(b, off_start,
2364 parent, bp->parent_offset,
2365 last_fixup_obj,
2366 last_fixup_min_off)) {
2367 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
2368 proc->pid, thread->pid);
2369 return -EINVAL;
2370 }
2371
2372 if (parent->length < sizeof(binder_uintptr_t) ||
2373 bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) {
2374 /* No space for a pointer here! */
2375 binder_user_error("%d:%d got transaction with invalid parent offset\n",
2376 proc->pid, thread->pid);
2377 return -EINVAL;
2378 }
2379 parent_buffer = (u8 *)(parent->buffer -
Todd Kjos19c98722017-06-29 12:01:40 -07002380 binder_alloc_get_user_buffer_offset(
2381 &target_proc->alloc));
Martijn Coenen79802402017-02-03 14:40:51 -08002382 *(binder_uintptr_t *)(parent_buffer + bp->parent_offset) = bp->buffer;
2383
2384 return 0;
2385}
2386
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002387static void binder_transaction(struct binder_proc *proc,
2388 struct binder_thread *thread,
Martijn Coenen4bfac802017-02-03 14:40:50 -08002389 struct binder_transaction_data *tr, int reply,
2390 binder_size_t extra_buffers_size)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002391{
Martijn Coenena056af42017-02-03 14:40:49 -08002392 int ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002393 struct binder_transaction *t;
2394 struct binder_work *tcomplete;
Martijn Coenen79802402017-02-03 14:40:51 -08002395 binder_size_t *offp, *off_end, *off_start;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08002396 binder_size_t off_min;
Martijn Coenen79802402017-02-03 14:40:51 -08002397 u8 *sg_bufp, *sg_buf_end;
Todd Kjos7a4408c2017-06-29 12:01:57 -07002398 struct binder_proc *target_proc = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002399 struct binder_thread *target_thread = NULL;
2400 struct binder_node *target_node = NULL;
2401 struct list_head *target_list;
2402 wait_queue_head_t *target_wait;
2403 struct binder_transaction *in_reply_to = NULL;
2404 struct binder_transaction_log_entry *e;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002405 uint32_t return_error = 0;
2406 uint32_t return_error_param = 0;
2407 uint32_t return_error_line = 0;
Martijn Coenen79802402017-02-03 14:40:51 -08002408 struct binder_buffer_object *last_fixup_obj = NULL;
2409 binder_size_t last_fixup_min_off = 0;
Martijn Coenen342e5c92017-02-03 14:40:46 -08002410 struct binder_context *context = proc->context;
Todd Kjosd99c7332017-06-29 12:01:53 -07002411 int t_debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002412
2413 e = binder_transaction_log_add(&binder_transaction_log);
Todd Kjosd99c7332017-06-29 12:01:53 -07002414 e->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002415 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
2416 e->from_proc = proc->pid;
2417 e->from_thread = thread->pid;
2418 e->target_handle = tr->target.handle;
2419 e->data_size = tr->data_size;
2420 e->offsets_size = tr->offsets_size;
Martijn Coenen14db3182017-02-03 14:40:47 -08002421 e->context_name = proc->context->name;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002422
2423 if (reply) {
Martijn Coenen0b89d692017-06-29 12:02:06 -07002424 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002425 in_reply_to = thread->transaction_stack;
2426 if (in_reply_to == NULL) {
Martijn Coenen0b89d692017-06-29 12:02:06 -07002427 binder_inner_proc_unlock(proc);
Anmol Sarma56b468f2012-10-30 22:35:43 +05302428 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002429 proc->pid, thread->pid);
2430 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002431 return_error_param = -EPROTO;
2432 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002433 goto err_empty_call_stack;
2434 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002435 if (in_reply_to->to_thread != thread) {
Todd Kjos7a4408c2017-06-29 12:01:57 -07002436 spin_lock(&in_reply_to->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05302437 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 +09002438 proc->pid, thread->pid, in_reply_to->debug_id,
2439 in_reply_to->to_proc ?
2440 in_reply_to->to_proc->pid : 0,
2441 in_reply_to->to_thread ?
2442 in_reply_to->to_thread->pid : 0);
Todd Kjos7a4408c2017-06-29 12:01:57 -07002443 spin_unlock(&in_reply_to->lock);
Martijn Coenen0b89d692017-06-29 12:02:06 -07002444 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002445 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002446 return_error_param = -EPROTO;
2447 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002448 in_reply_to = NULL;
2449 goto err_bad_call_stack;
2450 }
2451 thread->transaction_stack = in_reply_to->to_parent;
Martijn Coenen0b89d692017-06-29 12:02:06 -07002452 binder_inner_proc_unlock(proc);
2453 binder_set_nice(in_reply_to->saved_priority);
2454 target_thread = binder_get_txn_from_and_acq_inner(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002455 if (target_thread == NULL) {
2456 return_error = BR_DEAD_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002457 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002458 goto err_dead_binder;
2459 }
2460 if (target_thread->transaction_stack != in_reply_to) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302461 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 +09002462 proc->pid, thread->pid,
2463 target_thread->transaction_stack ?
2464 target_thread->transaction_stack->debug_id : 0,
2465 in_reply_to->debug_id);
Martijn Coenen0b89d692017-06-29 12:02:06 -07002466 binder_inner_proc_unlock(target_thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002467 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002468 return_error_param = -EPROTO;
2469 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002470 in_reply_to = NULL;
2471 target_thread = NULL;
2472 goto err_dead_binder;
2473 }
2474 target_proc = target_thread->proc;
Todd Kjos7a4408c2017-06-29 12:01:57 -07002475 target_proc->tmp_ref++;
Martijn Coenen0b89d692017-06-29 12:02:06 -07002476 binder_inner_proc_unlock(target_thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002477 } else {
2478 if (tr->target.handle) {
2479 struct binder_ref *ref;
Seunghun Lee10f62862014-05-01 01:30:23 +09002480
Todd Kjoseb349832017-06-29 12:01:56 -07002481 /*
2482 * There must already be a strong ref
2483 * on this node. If so, do a strong
2484 * increment on the node to ensure it
2485 * stays alive until the transaction is
2486 * done.
2487 */
Todd Kjos2c1838d2017-06-29 12:02:08 -07002488 binder_proc_lock(proc);
2489 ref = binder_get_ref_olocked(proc, tr->target.handle,
2490 true);
Todd Kjoseb349832017-06-29 12:01:56 -07002491 if (ref) {
2492 binder_inc_node(ref->node, 1, 0, NULL);
2493 target_node = ref->node;
2494 }
Todd Kjos2c1838d2017-06-29 12:02:08 -07002495 binder_proc_unlock(proc);
Todd Kjoseb349832017-06-29 12:01:56 -07002496 if (target_node == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302497 binder_user_error("%d:%d got transaction to invalid handle\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002498 proc->pid, thread->pid);
2499 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002500 return_error_param = -EINVAL;
2501 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002502 goto err_invalid_target_handle;
2503 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002504 } else {
Todd Kjosc44b1232017-06-29 12:01:43 -07002505 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen342e5c92017-02-03 14:40:46 -08002506 target_node = context->binder_context_mgr_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002507 if (target_node == NULL) {
2508 return_error = BR_DEAD_REPLY;
Todd Kjosc44b1232017-06-29 12:01:43 -07002509 mutex_unlock(&context->context_mgr_node_lock);
Todd Kjos57ada2f2017-06-29 12:01:46 -07002510 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002511 goto err_no_context_mgr_node;
2512 }
Todd Kjoseb349832017-06-29 12:01:56 -07002513 binder_inc_node(target_node, 1, 0, NULL);
Todd Kjosc44b1232017-06-29 12:01:43 -07002514 mutex_unlock(&context->context_mgr_node_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002515 }
2516 e->to_node = target_node->debug_id;
Todd Kjos673068e2017-06-29 12:02:03 -07002517 binder_node_lock(target_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002518 target_proc = target_node->proc;
2519 if (target_proc == NULL) {
Todd Kjos673068e2017-06-29 12:02:03 -07002520 binder_node_unlock(target_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002521 return_error = BR_DEAD_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002522 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002523 goto err_dead_binder;
2524 }
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07002525 binder_inner_proc_lock(target_proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07002526 target_proc->tmp_ref++;
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07002527 binder_inner_proc_unlock(target_proc);
Todd Kjos673068e2017-06-29 12:02:03 -07002528 binder_node_unlock(target_node);
Stephen Smalley79af7302015-01-21 10:54:10 -05002529 if (security_binder_transaction(proc->tsk,
2530 target_proc->tsk) < 0) {
2531 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002532 return_error_param = -EPERM;
2533 return_error_line = __LINE__;
Stephen Smalley79af7302015-01-21 10:54:10 -05002534 goto err_invalid_target_handle;
2535 }
Martijn Coenen0b89d692017-06-29 12:02:06 -07002536 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002537 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
2538 struct binder_transaction *tmp;
Seunghun Lee10f62862014-05-01 01:30:23 +09002539
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002540 tmp = thread->transaction_stack;
2541 if (tmp->to_thread != thread) {
Todd Kjos7a4408c2017-06-29 12:01:57 -07002542 spin_lock(&tmp->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05302543 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 +09002544 proc->pid, thread->pid, tmp->debug_id,
2545 tmp->to_proc ? tmp->to_proc->pid : 0,
2546 tmp->to_thread ?
2547 tmp->to_thread->pid : 0);
Todd Kjos7a4408c2017-06-29 12:01:57 -07002548 spin_unlock(&tmp->lock);
Martijn Coenen0b89d692017-06-29 12:02:06 -07002549 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002550 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002551 return_error_param = -EPROTO;
2552 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002553 goto err_bad_call_stack;
2554 }
2555 while (tmp) {
Todd Kjos7a4408c2017-06-29 12:01:57 -07002556 struct binder_thread *from;
2557
2558 spin_lock(&tmp->lock);
2559 from = tmp->from;
2560 if (from && from->proc == target_proc) {
2561 atomic_inc(&from->tmp_ref);
2562 target_thread = from;
2563 spin_unlock(&tmp->lock);
2564 break;
2565 }
2566 spin_unlock(&tmp->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002567 tmp = tmp->from_parent;
2568 }
2569 }
Martijn Coenen0b89d692017-06-29 12:02:06 -07002570 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002571 }
2572 if (target_thread) {
2573 e->to_thread = target_thread->pid;
2574 target_list = &target_thread->todo;
2575 target_wait = &target_thread->wait;
2576 } else {
2577 target_list = &target_proc->todo;
2578 target_wait = &target_proc->wait;
2579 }
2580 e->to_proc = target_proc->pid;
2581
2582 /* TODO: reuse incoming transaction for reply */
2583 t = kzalloc(sizeof(*t), GFP_KERNEL);
2584 if (t == NULL) {
2585 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002586 return_error_param = -ENOMEM;
2587 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002588 goto err_alloc_t_failed;
2589 }
2590 binder_stats_created(BINDER_STAT_TRANSACTION);
Todd Kjos7a4408c2017-06-29 12:01:57 -07002591 spin_lock_init(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002592
2593 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
2594 if (tcomplete == NULL) {
2595 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002596 return_error_param = -ENOMEM;
2597 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002598 goto err_alloc_tcomplete_failed;
2599 }
2600 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
2601
Todd Kjosd99c7332017-06-29 12:01:53 -07002602 t->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002603
2604 if (reply)
2605 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen4bfac802017-02-03 14:40:50 -08002606 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002607 proc->pid, thread->pid, t->debug_id,
2608 target_proc->pid, target_thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002609 (u64)tr->data.ptr.buffer,
2610 (u64)tr->data.ptr.offsets,
Martijn Coenen4bfac802017-02-03 14:40:50 -08002611 (u64)tr->data_size, (u64)tr->offsets_size,
2612 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002613 else
2614 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen4bfac802017-02-03 14:40:50 -08002615 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002616 proc->pid, thread->pid, t->debug_id,
2617 target_proc->pid, target_node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002618 (u64)tr->data.ptr.buffer,
2619 (u64)tr->data.ptr.offsets,
Martijn Coenen4bfac802017-02-03 14:40:50 -08002620 (u64)tr->data_size, (u64)tr->offsets_size,
2621 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002622
2623 if (!reply && !(tr->flags & TF_ONE_WAY))
2624 t->from = thread;
2625 else
2626 t->from = NULL;
Tair Rzayev57bab7c2014-05-31 22:43:34 +03002627 t->sender_euid = task_euid(proc->tsk);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002628 t->to_proc = target_proc;
2629 t->to_thread = target_thread;
2630 t->code = tr->code;
2631 t->flags = tr->flags;
2632 t->priority = task_nice(current);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002633
2634 trace_binder_transaction(reply, t, target_node);
2635
Todd Kjos19c98722017-06-29 12:01:40 -07002636 t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size,
Martijn Coenen4bfac802017-02-03 14:40:50 -08002637 tr->offsets_size, extra_buffers_size,
2638 !reply && (t->flags & TF_ONE_WAY));
Todd Kjos57ada2f2017-06-29 12:01:46 -07002639 if (IS_ERR(t->buffer)) {
2640 /*
2641 * -ESRCH indicates VMA cleared. The target is dying.
2642 */
2643 return_error_param = PTR_ERR(t->buffer);
2644 return_error = return_error_param == -ESRCH ?
2645 BR_DEAD_REPLY : BR_FAILED_REPLY;
2646 return_error_line = __LINE__;
2647 t->buffer = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002648 goto err_binder_alloc_buf_failed;
2649 }
2650 t->buffer->allow_user_free = 0;
2651 t->buffer->debug_id = t->debug_id;
2652 t->buffer->transaction = t;
2653 t->buffer->target_node = target_node;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002654 trace_binder_transaction_alloc_buf(t->buffer);
Martijn Coenen79802402017-02-03 14:40:51 -08002655 off_start = (binder_size_t *)(t->buffer->data +
2656 ALIGN(tr->data_size, sizeof(void *)));
2657 offp = off_start;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002658
Arve Hjønnevågda498892014-02-21 14:40:26 -08002659 if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t)
2660 tr->data.ptr.buffer, tr->data_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302661 binder_user_error("%d:%d got transaction with invalid data ptr\n",
2662 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002663 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002664 return_error_param = -EFAULT;
2665 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002666 goto err_copy_data_failed;
2667 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08002668 if (copy_from_user(offp, (const void __user *)(uintptr_t)
2669 tr->data.ptr.offsets, tr->offsets_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302670 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
2671 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002672 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002673 return_error_param = -EFAULT;
2674 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002675 goto err_copy_data_failed;
2676 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08002677 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
2678 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
2679 proc->pid, thread->pid, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002680 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002681 return_error_param = -EINVAL;
2682 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002683 goto err_bad_offset;
2684 }
Martijn Coenen79802402017-02-03 14:40:51 -08002685 if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) {
2686 binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
2687 proc->pid, thread->pid,
2688 (u64)extra_buffers_size);
2689 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002690 return_error_param = -EINVAL;
2691 return_error_line = __LINE__;
Martijn Coenen79802402017-02-03 14:40:51 -08002692 goto err_bad_offset;
2693 }
2694 off_end = (void *)off_start + tr->offsets_size;
2695 sg_bufp = (u8 *)(PTR_ALIGN(off_end, sizeof(void *)));
2696 sg_buf_end = sg_bufp + extra_buffers_size;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08002697 off_min = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002698 for (; offp < off_end; offp++) {
Martijn Coenenfeba3902017-02-03 14:40:45 -08002699 struct binder_object_header *hdr;
2700 size_t object_size = binder_validate_object(t->buffer, *offp);
Seunghun Lee10f62862014-05-01 01:30:23 +09002701
Martijn Coenenfeba3902017-02-03 14:40:45 -08002702 if (object_size == 0 || *offp < off_min) {
2703 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 -08002704 proc->pid, thread->pid, (u64)*offp,
2705 (u64)off_min,
Martijn Coenenfeba3902017-02-03 14:40:45 -08002706 (u64)t->buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002707 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002708 return_error_param = -EINVAL;
2709 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002710 goto err_bad_offset;
2711 }
Martijn Coenenfeba3902017-02-03 14:40:45 -08002712
2713 hdr = (struct binder_object_header *)(t->buffer->data + *offp);
2714 off_min = *offp + object_size;
2715 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002716 case BINDER_TYPE_BINDER:
2717 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08002718 struct flat_binder_object *fp;
Seunghun Lee10f62862014-05-01 01:30:23 +09002719
Martijn Coenenfeba3902017-02-03 14:40:45 -08002720 fp = to_flat_binder_object(hdr);
Martijn Coenena056af42017-02-03 14:40:49 -08002721 ret = binder_translate_binder(fp, t, thread);
2722 if (ret < 0) {
Christian Engelmayer7d420432014-05-07 21:44:53 +02002723 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002724 return_error_param = ret;
2725 return_error_line = __LINE__;
Martijn Coenena056af42017-02-03 14:40:49 -08002726 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002727 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002728 } break;
2729 case BINDER_TYPE_HANDLE:
2730 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08002731 struct flat_binder_object *fp;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02002732
Martijn Coenenfeba3902017-02-03 14:40:45 -08002733 fp = to_flat_binder_object(hdr);
Martijn Coenena056af42017-02-03 14:40:49 -08002734 ret = binder_translate_handle(fp, t, thread);
2735 if (ret < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002736 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002737 return_error_param = ret;
2738 return_error_line = __LINE__;
Martijn Coenena056af42017-02-03 14:40:49 -08002739 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002740 }
2741 } break;
2742
2743 case BINDER_TYPE_FD: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08002744 struct binder_fd_object *fp = to_binder_fd_object(hdr);
Martijn Coenena056af42017-02-03 14:40:49 -08002745 int target_fd = binder_translate_fd(fp->fd, t, thread,
2746 in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002747
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002748 if (target_fd < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002749 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002750 return_error_param = target_fd;
2751 return_error_line = __LINE__;
Martijn Coenena056af42017-02-03 14:40:49 -08002752 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002753 }
Martijn Coenenfeba3902017-02-03 14:40:45 -08002754 fp->pad_binder = 0;
2755 fp->fd = target_fd;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002756 } break;
Martijn Coenendef95c72017-02-03 14:40:52 -08002757 case BINDER_TYPE_FDA: {
2758 struct binder_fd_array_object *fda =
2759 to_binder_fd_array_object(hdr);
2760 struct binder_buffer_object *parent =
2761 binder_validate_ptr(t->buffer, fda->parent,
2762 off_start,
2763 offp - off_start);
2764 if (!parent) {
2765 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
2766 proc->pid, thread->pid);
2767 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002768 return_error_param = -EINVAL;
2769 return_error_line = __LINE__;
Martijn Coenendef95c72017-02-03 14:40:52 -08002770 goto err_bad_parent;
2771 }
2772 if (!binder_validate_fixup(t->buffer, off_start,
2773 parent, fda->parent_offset,
2774 last_fixup_obj,
2775 last_fixup_min_off)) {
2776 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
2777 proc->pid, thread->pid);
2778 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002779 return_error_param = -EINVAL;
2780 return_error_line = __LINE__;
Martijn Coenendef95c72017-02-03 14:40:52 -08002781 goto err_bad_parent;
2782 }
2783 ret = binder_translate_fd_array(fda, parent, t, thread,
2784 in_reply_to);
2785 if (ret < 0) {
2786 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002787 return_error_param = ret;
2788 return_error_line = __LINE__;
Martijn Coenendef95c72017-02-03 14:40:52 -08002789 goto err_translate_failed;
2790 }
2791 last_fixup_obj = parent;
2792 last_fixup_min_off =
2793 fda->parent_offset + sizeof(u32) * fda->num_fds;
2794 } break;
Martijn Coenen79802402017-02-03 14:40:51 -08002795 case BINDER_TYPE_PTR: {
2796 struct binder_buffer_object *bp =
2797 to_binder_buffer_object(hdr);
2798 size_t buf_left = sg_buf_end - sg_bufp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002799
Martijn Coenen79802402017-02-03 14:40:51 -08002800 if (bp->length > buf_left) {
2801 binder_user_error("%d:%d got transaction with too large buffer\n",
2802 proc->pid, thread->pid);
2803 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002804 return_error_param = -EINVAL;
2805 return_error_line = __LINE__;
Martijn Coenen79802402017-02-03 14:40:51 -08002806 goto err_bad_offset;
2807 }
2808 if (copy_from_user(sg_bufp,
2809 (const void __user *)(uintptr_t)
2810 bp->buffer, bp->length)) {
2811 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
2812 proc->pid, thread->pid);
Todd Kjos57ada2f2017-06-29 12:01:46 -07002813 return_error_param = -EFAULT;
Martijn Coenen79802402017-02-03 14:40:51 -08002814 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002815 return_error_line = __LINE__;
Martijn Coenen79802402017-02-03 14:40:51 -08002816 goto err_copy_data_failed;
2817 }
2818 /* Fixup buffer pointer to target proc address space */
2819 bp->buffer = (uintptr_t)sg_bufp +
Todd Kjos19c98722017-06-29 12:01:40 -07002820 binder_alloc_get_user_buffer_offset(
2821 &target_proc->alloc);
Martijn Coenen79802402017-02-03 14:40:51 -08002822 sg_bufp += ALIGN(bp->length, sizeof(u64));
2823
2824 ret = binder_fixup_parent(t, thread, bp, off_start,
2825 offp - off_start,
2826 last_fixup_obj,
2827 last_fixup_min_off);
2828 if (ret < 0) {
2829 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002830 return_error_param = ret;
2831 return_error_line = __LINE__;
Martijn Coenen79802402017-02-03 14:40:51 -08002832 goto err_translate_failed;
2833 }
2834 last_fixup_obj = bp;
2835 last_fixup_min_off = 0;
2836 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002837 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01002838 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
Martijn Coenenfeba3902017-02-03 14:40:45 -08002839 proc->pid, thread->pid, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002840 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07002841 return_error_param = -EINVAL;
2842 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002843 goto err_bad_object_type;
2844 }
2845 }
Todd Kjosccae6f62017-06-29 12:01:48 -07002846 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
Todd Kjos72196392017-06-29 12:02:02 -07002847 binder_enqueue_work(proc, tcomplete, &thread->todo);
Todd Kjos673068e2017-06-29 12:02:03 -07002848 t->work.type = BINDER_WORK_TRANSACTION;
Todd Kjosccae6f62017-06-29 12:01:48 -07002849
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002850 if (reply) {
Martijn Coenen0b89d692017-06-29 12:02:06 -07002851 binder_inner_proc_lock(target_proc);
2852 if (target_thread->is_dead) {
2853 binder_inner_proc_unlock(target_proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07002854 goto err_dead_proc_or_thread;
Martijn Coenen0b89d692017-06-29 12:02:06 -07002855 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002856 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen0b89d692017-06-29 12:02:06 -07002857 binder_pop_transaction_ilocked(target_thread, in_reply_to);
2858 binder_enqueue_work_ilocked(&t->work, target_list);
2859 binder_inner_proc_unlock(target_proc);
Todd Kjosb6d282c2017-06-29 12:01:54 -07002860 binder_free_transaction(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002861 } else if (!(t->flags & TF_ONE_WAY)) {
2862 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen0b89d692017-06-29 12:02:06 -07002863 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002864 t->need_reply = 1;
2865 t->from_parent = thread->transaction_stack;
2866 thread->transaction_stack = t;
Martijn Coenen0b89d692017-06-29 12:02:06 -07002867 binder_inner_proc_unlock(proc);
2868 binder_inner_proc_lock(target_proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07002869 if (target_proc->is_dead ||
2870 (target_thread && target_thread->is_dead)) {
Martijn Coenen0b89d692017-06-29 12:02:06 -07002871 binder_inner_proc_unlock(target_proc);
2872 binder_inner_proc_lock(proc);
2873 binder_pop_transaction_ilocked(thread, t);
2874 binder_inner_proc_unlock(proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07002875 goto err_dead_proc_or_thread;
2876 }
Martijn Coenen0b89d692017-06-29 12:02:06 -07002877 binder_enqueue_work_ilocked(&t->work, target_list);
2878 binder_inner_proc_unlock(target_proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002879 } else {
2880 BUG_ON(target_node == NULL);
2881 BUG_ON(t->buffer->async_transaction != 1);
Todd Kjos673068e2017-06-29 12:02:03 -07002882 binder_node_lock(target_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002883 if (target_node->has_async_transaction) {
2884 target_list = &target_node->async_todo;
2885 target_wait = NULL;
2886 } else
2887 target_node->has_async_transaction = 1;
Todd Kjos673068e2017-06-29 12:02:03 -07002888 /*
2889 * Test/set of has_async_transaction
2890 * must be atomic with enqueue on
2891 * async_todo
2892 */
Martijn Coenen0b89d692017-06-29 12:02:06 -07002893 binder_inner_proc_lock(target_proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07002894 if (target_proc->is_dead ||
Todd Kjos673068e2017-06-29 12:02:03 -07002895 (target_thread && target_thread->is_dead)) {
Martijn Coenen0b89d692017-06-29 12:02:06 -07002896 binder_inner_proc_unlock(target_proc);
Todd Kjos673068e2017-06-29 12:02:03 -07002897 binder_node_unlock(target_node);
Todd Kjos7a4408c2017-06-29 12:01:57 -07002898 goto err_dead_proc_or_thread;
Todd Kjos673068e2017-06-29 12:02:03 -07002899 }
Martijn Coenen0b89d692017-06-29 12:02:06 -07002900 binder_enqueue_work_ilocked(&t->work, target_list);
2901 binder_inner_proc_unlock(target_proc);
Todd Kjos673068e2017-06-29 12:02:03 -07002902 binder_node_unlock(target_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002903 }
Riley Andrews00b40d62017-06-29 12:01:37 -07002904 if (target_wait) {
Todd Kjosccae6f62017-06-29 12:01:48 -07002905 if (reply || !(tr->flags & TF_ONE_WAY))
Riley Andrews00b40d62017-06-29 12:01:37 -07002906 wake_up_interruptible_sync(target_wait);
2907 else
2908 wake_up_interruptible(target_wait);
2909 }
Todd Kjos7a4408c2017-06-29 12:01:57 -07002910 if (target_thread)
2911 binder_thread_dec_tmpref(target_thread);
2912 binder_proc_dec_tmpref(target_proc);
Todd Kjosd99c7332017-06-29 12:01:53 -07002913 /*
2914 * write barrier to synchronize with initialization
2915 * of log entry
2916 */
2917 smp_wmb();
2918 WRITE_ONCE(e->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002919 return;
2920
Todd Kjos7a4408c2017-06-29 12:01:57 -07002921err_dead_proc_or_thread:
2922 return_error = BR_DEAD_REPLY;
2923 return_error_line = __LINE__;
Martijn Coenena056af42017-02-03 14:40:49 -08002924err_translate_failed:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002925err_bad_object_type:
2926err_bad_offset:
Martijn Coenendef95c72017-02-03 14:40:52 -08002927err_bad_parent:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002928err_copy_data_failed:
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002929 trace_binder_transaction_failed_buffer_release(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002930 binder_transaction_buffer_release(target_proc, t->buffer, offp);
Todd Kjoseb349832017-06-29 12:01:56 -07002931 target_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002932 t->buffer->transaction = NULL;
Todd Kjos19c98722017-06-29 12:01:40 -07002933 binder_alloc_free_buf(&target_proc->alloc, t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002934err_binder_alloc_buf_failed:
2935 kfree(tcomplete);
2936 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2937err_alloc_tcomplete_failed:
2938 kfree(t);
2939 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2940err_alloc_t_failed:
2941err_bad_call_stack:
2942err_empty_call_stack:
2943err_dead_binder:
2944err_invalid_target_handle:
2945err_no_context_mgr_node:
Todd Kjos7a4408c2017-06-29 12:01:57 -07002946 if (target_thread)
2947 binder_thread_dec_tmpref(target_thread);
2948 if (target_proc)
2949 binder_proc_dec_tmpref(target_proc);
Todd Kjoseb349832017-06-29 12:01:56 -07002950 if (target_node)
2951 binder_dec_node(target_node, 1, 0);
2952
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002953 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Todd Kjos57ada2f2017-06-29 12:01:46 -07002954 "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
2955 proc->pid, thread->pid, return_error, return_error_param,
2956 (u64)tr->data_size, (u64)tr->offsets_size,
2957 return_error_line);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002958
2959 {
2960 struct binder_transaction_log_entry *fe;
Seunghun Lee10f62862014-05-01 01:30:23 +09002961
Todd Kjos57ada2f2017-06-29 12:01:46 -07002962 e->return_error = return_error;
2963 e->return_error_param = return_error_param;
2964 e->return_error_line = return_error_line;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002965 fe = binder_transaction_log_add(&binder_transaction_log_failed);
2966 *fe = *e;
Todd Kjosd99c7332017-06-29 12:01:53 -07002967 /*
2968 * write barrier to synchronize with initialization
2969 * of log entry
2970 */
2971 smp_wmb();
2972 WRITE_ONCE(e->debug_id_done, t_debug_id);
2973 WRITE_ONCE(fe->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002974 }
2975
Todd Kjos26549d12017-06-29 12:01:55 -07002976 BUG_ON(thread->return_error.cmd != BR_OK);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002977 if (in_reply_to) {
Todd Kjos26549d12017-06-29 12:01:55 -07002978 thread->return_error.cmd = BR_TRANSACTION_COMPLETE;
Todd Kjos72196392017-06-29 12:02:02 -07002979 binder_enqueue_work(thread->proc,
2980 &thread->return_error.work,
2981 &thread->todo);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002982 binder_send_failed_reply(in_reply_to, return_error);
Todd Kjos26549d12017-06-29 12:01:55 -07002983 } else {
2984 thread->return_error.cmd = return_error;
Todd Kjos72196392017-06-29 12:02:02 -07002985 binder_enqueue_work(thread->proc,
2986 &thread->return_error.work,
2987 &thread->todo);
Todd Kjos26549d12017-06-29 12:01:55 -07002988 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002989}
2990
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02002991static int binder_thread_write(struct binder_proc *proc,
2992 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002993 binder_uintptr_t binder_buffer, size_t size,
2994 binder_size_t *consumed)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002995{
2996 uint32_t cmd;
Martijn Coenen342e5c92017-02-03 14:40:46 -08002997 struct binder_context *context = proc->context;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002998 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002999 void __user *ptr = buffer + *consumed;
3000 void __user *end = buffer + size;
3001
Todd Kjos26549d12017-06-29 12:01:55 -07003002 while (ptr < end && thread->return_error.cmd == BR_OK) {
Todd Kjos372e3142017-06-29 12:01:58 -07003003 int ret;
3004
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003005 if (get_user(cmd, (uint32_t __user *)ptr))
3006 return -EFAULT;
3007 ptr += sizeof(uint32_t);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003008 trace_binder_command(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003009 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07003010 atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]);
3011 atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]);
3012 atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003013 }
3014 switch (cmd) {
3015 case BC_INCREFS:
3016 case BC_ACQUIRE:
3017 case BC_RELEASE:
3018 case BC_DECREFS: {
3019 uint32_t target;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003020 const char *debug_string;
Todd Kjos372e3142017-06-29 12:01:58 -07003021 bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE;
3022 bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE;
3023 struct binder_ref_data rdata;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003024
3025 if (get_user(target, (uint32_t __user *)ptr))
3026 return -EFAULT;
Todd Kjosc44b1232017-06-29 12:01:43 -07003027
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003028 ptr += sizeof(uint32_t);
Todd Kjos372e3142017-06-29 12:01:58 -07003029 ret = -1;
3030 if (increment && !target) {
Todd Kjosc44b1232017-06-29 12:01:43 -07003031 struct binder_node *ctx_mgr_node;
Todd Kjosc44b1232017-06-29 12:01:43 -07003032 mutex_lock(&context->context_mgr_node_lock);
3033 ctx_mgr_node = context->binder_context_mgr_node;
Todd Kjos372e3142017-06-29 12:01:58 -07003034 if (ctx_mgr_node)
3035 ret = binder_inc_ref_for_node(
3036 proc, ctx_mgr_node,
3037 strong, NULL, &rdata);
Todd Kjosc44b1232017-06-29 12:01:43 -07003038 mutex_unlock(&context->context_mgr_node_lock);
3039 }
Todd Kjos372e3142017-06-29 12:01:58 -07003040 if (ret)
3041 ret = binder_update_ref_for_handle(
3042 proc, target, increment, strong,
3043 &rdata);
3044 if (!ret && rdata.desc != target) {
3045 binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
3046 proc->pid, thread->pid,
3047 target, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003048 }
3049 switch (cmd) {
3050 case BC_INCREFS:
3051 debug_string = "IncRefs";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003052 break;
3053 case BC_ACQUIRE:
3054 debug_string = "Acquire";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003055 break;
3056 case BC_RELEASE:
3057 debug_string = "Release";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003058 break;
3059 case BC_DECREFS:
3060 default:
3061 debug_string = "DecRefs";
Todd Kjos372e3142017-06-29 12:01:58 -07003062 break;
3063 }
3064 if (ret) {
3065 binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
3066 proc->pid, thread->pid, debug_string,
3067 strong, target, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003068 break;
3069 }
3070 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjos372e3142017-06-29 12:01:58 -07003071 "%d:%d %s ref %d desc %d s %d w %d\n",
3072 proc->pid, thread->pid, debug_string,
3073 rdata.debug_id, rdata.desc, rdata.strong,
3074 rdata.weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003075 break;
3076 }
3077 case BC_INCREFS_DONE:
3078 case BC_ACQUIRE_DONE: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003079 binder_uintptr_t node_ptr;
3080 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003081 struct binder_node *node;
Todd Kjos673068e2017-06-29 12:02:03 -07003082 bool free_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003083
Arve Hjønnevågda498892014-02-21 14:40:26 -08003084 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003085 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003086 ptr += sizeof(binder_uintptr_t);
3087 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003088 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003089 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003090 node = binder_get_node(proc, node_ptr);
3091 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003092 binder_user_error("%d:%d %s u%016llx no match\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003093 proc->pid, thread->pid,
3094 cmd == BC_INCREFS_DONE ?
3095 "BC_INCREFS_DONE" :
3096 "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003097 (u64)node_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003098 break;
3099 }
3100 if (cookie != node->cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003101 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003102 proc->pid, thread->pid,
3103 cmd == BC_INCREFS_DONE ?
3104 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003105 (u64)node_ptr, node->debug_id,
3106 (u64)cookie, (u64)node->cookie);
Todd Kjosadc18842017-06-29 12:01:59 -07003107 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003108 break;
3109 }
Todd Kjos673068e2017-06-29 12:02:03 -07003110 binder_node_inner_lock(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003111 if (cmd == BC_ACQUIRE_DONE) {
3112 if (node->pending_strong_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303113 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003114 proc->pid, thread->pid,
3115 node->debug_id);
Todd Kjos673068e2017-06-29 12:02:03 -07003116 binder_node_inner_unlock(node);
Todd Kjosadc18842017-06-29 12:01:59 -07003117 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003118 break;
3119 }
3120 node->pending_strong_ref = 0;
3121 } else {
3122 if (node->pending_weak_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303123 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003124 proc->pid, thread->pid,
3125 node->debug_id);
Todd Kjos673068e2017-06-29 12:02:03 -07003126 binder_node_inner_unlock(node);
Todd Kjosadc18842017-06-29 12:01:59 -07003127 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003128 break;
3129 }
3130 node->pending_weak_ref = 0;
3131 }
Todd Kjos673068e2017-06-29 12:02:03 -07003132 free_node = binder_dec_node_nilocked(node,
3133 cmd == BC_ACQUIRE_DONE, 0);
3134 WARN_ON(free_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003135 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjosadc18842017-06-29 12:01:59 -07003136 "%d:%d %s node %d ls %d lw %d tr %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003137 proc->pid, thread->pid,
3138 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Todd Kjosadc18842017-06-29 12:01:59 -07003139 node->debug_id, node->local_strong_refs,
3140 node->local_weak_refs, node->tmp_refs);
Todd Kjos673068e2017-06-29 12:02:03 -07003141 binder_node_inner_unlock(node);
Todd Kjosadc18842017-06-29 12:01:59 -07003142 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003143 break;
3144 }
3145 case BC_ATTEMPT_ACQUIRE:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303146 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003147 return -EINVAL;
3148 case BC_ACQUIRE_RESULT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303149 pr_err("BC_ACQUIRE_RESULT not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003150 return -EINVAL;
3151
3152 case BC_FREE_BUFFER: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003153 binder_uintptr_t data_ptr;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003154 struct binder_buffer *buffer;
3155
Arve Hjønnevågda498892014-02-21 14:40:26 -08003156 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003157 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003158 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003159
Todd Kjos53d311cf2017-06-29 12:01:51 -07003160 buffer = binder_alloc_prepare_to_free(&proc->alloc,
3161 data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003162 if (buffer == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003163 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
3164 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003165 break;
3166 }
3167 if (!buffer->allow_user_free) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003168 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
3169 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003170 break;
3171 }
3172 binder_debug(BINDER_DEBUG_FREE_BUFFER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003173 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
3174 proc->pid, thread->pid, (u64)data_ptr,
3175 buffer->debug_id,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003176 buffer->transaction ? "active" : "finished");
3177
3178 if (buffer->transaction) {
3179 buffer->transaction->buffer = NULL;
3180 buffer->transaction = NULL;
3181 }
3182 if (buffer->async_transaction && buffer->target_node) {
Todd Kjos72196392017-06-29 12:02:02 -07003183 struct binder_node *buf_node;
3184 struct binder_work *w;
3185
3186 buf_node = buffer->target_node;
Todd Kjos673068e2017-06-29 12:02:03 -07003187 binder_node_inner_lock(buf_node);
Todd Kjos72196392017-06-29 12:02:02 -07003188 BUG_ON(!buf_node->has_async_transaction);
3189 BUG_ON(buf_node->proc != proc);
Todd Kjos72196392017-06-29 12:02:02 -07003190 w = binder_dequeue_work_head_ilocked(
3191 &buf_node->async_todo);
3192 if (!w)
3193 buf_node->has_async_transaction = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003194 else
Todd Kjos72196392017-06-29 12:02:02 -07003195 binder_enqueue_work_ilocked(
3196 w, &thread->todo);
Todd Kjos673068e2017-06-29 12:02:03 -07003197 binder_node_inner_unlock(buf_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003198 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003199 trace_binder_transaction_buffer_release(buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003200 binder_transaction_buffer_release(proc, buffer, NULL);
Todd Kjos19c98722017-06-29 12:01:40 -07003201 binder_alloc_free_buf(&proc->alloc, buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003202 break;
3203 }
3204
Martijn Coenen79802402017-02-03 14:40:51 -08003205 case BC_TRANSACTION_SG:
3206 case BC_REPLY_SG: {
3207 struct binder_transaction_data_sg tr;
3208
3209 if (copy_from_user(&tr, ptr, sizeof(tr)))
3210 return -EFAULT;
3211 ptr += sizeof(tr);
3212 binder_transaction(proc, thread, &tr.transaction_data,
3213 cmd == BC_REPLY_SG, tr.buffers_size);
3214 break;
3215 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003216 case BC_TRANSACTION:
3217 case BC_REPLY: {
3218 struct binder_transaction_data tr;
3219
3220 if (copy_from_user(&tr, ptr, sizeof(tr)))
3221 return -EFAULT;
3222 ptr += sizeof(tr);
Martijn Coenen4bfac802017-02-03 14:40:50 -08003223 binder_transaction(proc, thread, &tr,
3224 cmd == BC_REPLY, 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003225 break;
3226 }
3227
3228 case BC_REGISTER_LOOPER:
3229 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303230 "%d:%d BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003231 proc->pid, thread->pid);
Todd Kjosb3e68612017-06-29 12:02:07 -07003232 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003233 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
3234 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303235 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003236 proc->pid, thread->pid);
3237 } else if (proc->requested_threads == 0) {
3238 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303239 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003240 proc->pid, thread->pid);
3241 } else {
3242 proc->requested_threads--;
3243 proc->requested_threads_started++;
3244 }
3245 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
Todd Kjosb3e68612017-06-29 12:02:07 -07003246 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003247 break;
3248 case BC_ENTER_LOOPER:
3249 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303250 "%d:%d BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003251 proc->pid, thread->pid);
3252 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
3253 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303254 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003255 proc->pid, thread->pid);
3256 }
3257 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
3258 break;
3259 case BC_EXIT_LOOPER:
3260 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303261 "%d:%d BC_EXIT_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003262 proc->pid, thread->pid);
3263 thread->looper |= BINDER_LOOPER_STATE_EXITED;
3264 break;
3265
3266 case BC_REQUEST_DEATH_NOTIFICATION:
3267 case BC_CLEAR_DEATH_NOTIFICATION: {
3268 uint32_t target;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003269 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003270 struct binder_ref *ref;
Todd Kjos2c1838d2017-06-29 12:02:08 -07003271 struct binder_ref_death *death = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003272
3273 if (get_user(target, (uint32_t __user *)ptr))
3274 return -EFAULT;
3275 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08003276 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003277 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003278 ptr += sizeof(binder_uintptr_t);
Todd Kjos2c1838d2017-06-29 12:02:08 -07003279 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3280 /*
3281 * Allocate memory for death notification
3282 * before taking lock
3283 */
3284 death = kzalloc(sizeof(*death), GFP_KERNEL);
3285 if (death == NULL) {
3286 WARN_ON(thread->return_error.cmd !=
3287 BR_OK);
3288 thread->return_error.cmd = BR_ERROR;
3289 binder_enqueue_work(
3290 thread->proc,
3291 &thread->return_error.work,
3292 &thread->todo);
3293 binder_debug(
3294 BINDER_DEBUG_FAILED_TRANSACTION,
3295 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
3296 proc->pid, thread->pid);
3297 break;
3298 }
3299 }
3300 binder_proc_lock(proc);
3301 ref = binder_get_ref_olocked(proc, target, false);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003302 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303303 binder_user_error("%d:%d %s invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003304 proc->pid, thread->pid,
3305 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3306 "BC_REQUEST_DEATH_NOTIFICATION" :
3307 "BC_CLEAR_DEATH_NOTIFICATION",
3308 target);
Todd Kjos2c1838d2017-06-29 12:02:08 -07003309 binder_proc_unlock(proc);
3310 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003311 break;
3312 }
3313
3314 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003315 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003316 proc->pid, thread->pid,
3317 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3318 "BC_REQUEST_DEATH_NOTIFICATION" :
3319 "BC_CLEAR_DEATH_NOTIFICATION",
Todd Kjos372e3142017-06-29 12:01:58 -07003320 (u64)cookie, ref->data.debug_id,
3321 ref->data.desc, ref->data.strong,
3322 ref->data.weak, ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003323
Martijn Coenenab51ec62017-06-29 12:02:10 -07003324 binder_node_lock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003325 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3326 if (ref->death) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303327 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003328 proc->pid, thread->pid);
Martijn Coenenab51ec62017-06-29 12:02:10 -07003329 binder_node_unlock(ref->node);
Todd Kjos2c1838d2017-06-29 12:02:08 -07003330 binder_proc_unlock(proc);
3331 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003332 break;
3333 }
3334 binder_stats_created(BINDER_STAT_DEATH);
3335 INIT_LIST_HEAD(&death->work.entry);
3336 death->cookie = cookie;
3337 ref->death = death;
3338 if (ref->node->proc == NULL) {
3339 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
Todd Kjos72196392017-06-29 12:02:02 -07003340 if (thread->looper &
3341 (BINDER_LOOPER_STATE_REGISTERED |
3342 BINDER_LOOPER_STATE_ENTERED))
3343 binder_enqueue_work(
3344 proc,
3345 &ref->death->work,
3346 &thread->todo);
3347 else {
3348 binder_enqueue_work(
3349 proc,
3350 &ref->death->work,
3351 &proc->todo);
3352 wake_up_interruptible(
3353 &proc->wait);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003354 }
3355 }
3356 } else {
3357 if (ref->death == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303358 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003359 proc->pid, thread->pid);
Todd Kjos673068e2017-06-29 12:02:03 -07003360 binder_node_unlock(ref->node);
Todd Kjos2c1838d2017-06-29 12:02:08 -07003361 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003362 break;
3363 }
3364 death = ref->death;
3365 if (death->cookie != cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003366 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003367 proc->pid, thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003368 (u64)death->cookie,
3369 (u64)cookie);
Todd Kjos673068e2017-06-29 12:02:03 -07003370 binder_node_unlock(ref->node);
Todd Kjos2c1838d2017-06-29 12:02:08 -07003371 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003372 break;
3373 }
3374 ref->death = NULL;
Todd Kjos72196392017-06-29 12:02:02 -07003375 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003376 if (list_empty(&death->work.entry)) {
3377 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos72196392017-06-29 12:02:02 -07003378 if (thread->looper &
3379 (BINDER_LOOPER_STATE_REGISTERED |
3380 BINDER_LOOPER_STATE_ENTERED))
3381 binder_enqueue_work_ilocked(
3382 &death->work,
3383 &thread->todo);
3384 else {
3385 binder_enqueue_work_ilocked(
3386 &death->work,
3387 &proc->todo);
3388 wake_up_interruptible(
3389 &proc->wait);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003390 }
3391 } else {
3392 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
3393 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
3394 }
Todd Kjos72196392017-06-29 12:02:02 -07003395 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003396 }
Martijn Coenenab51ec62017-06-29 12:02:10 -07003397 binder_node_unlock(ref->node);
Todd Kjos2c1838d2017-06-29 12:02:08 -07003398 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003399 } break;
3400 case BC_DEAD_BINDER_DONE: {
3401 struct binder_work *w;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003402 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003403 struct binder_ref_death *death = NULL;
Seunghun Lee10f62862014-05-01 01:30:23 +09003404
Arve Hjønnevågda498892014-02-21 14:40:26 -08003405 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003406 return -EFAULT;
3407
Lisa Du7a64cd82016-02-17 09:32:52 +08003408 ptr += sizeof(cookie);
Todd Kjos72196392017-06-29 12:02:02 -07003409 binder_inner_proc_lock(proc);
3410 list_for_each_entry(w, &proc->delivered_death,
3411 entry) {
3412 struct binder_ref_death *tmp_death =
3413 container_of(w,
3414 struct binder_ref_death,
3415 work);
Seunghun Lee10f62862014-05-01 01:30:23 +09003416
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003417 if (tmp_death->cookie == cookie) {
3418 death = tmp_death;
3419 break;
3420 }
3421 }
3422 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003423 "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n",
3424 proc->pid, thread->pid, (u64)cookie,
3425 death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003426 if (death == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003427 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
3428 proc->pid, thread->pid, (u64)cookie);
Todd Kjos72196392017-06-29 12:02:02 -07003429 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003430 break;
3431 }
Todd Kjos72196392017-06-29 12:02:02 -07003432 binder_dequeue_work_ilocked(&death->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003433 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
3434 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos72196392017-06-29 12:02:02 -07003435 if (thread->looper &
3436 (BINDER_LOOPER_STATE_REGISTERED |
3437 BINDER_LOOPER_STATE_ENTERED))
3438 binder_enqueue_work_ilocked(
3439 &death->work, &thread->todo);
3440 else {
3441 binder_enqueue_work_ilocked(
3442 &death->work,
3443 &proc->todo);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003444 wake_up_interruptible(&proc->wait);
3445 }
3446 }
Todd Kjos72196392017-06-29 12:02:02 -07003447 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003448 } break;
3449
3450 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303451 pr_err("%d:%d unknown command %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003452 proc->pid, thread->pid, cmd);
3453 return -EINVAL;
3454 }
3455 *consumed = ptr - buffer;
3456 }
3457 return 0;
3458}
3459
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02003460static void binder_stat_br(struct binder_proc *proc,
3461 struct binder_thread *thread, uint32_t cmd)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003462{
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003463 trace_binder_return(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003464 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07003465 atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
3466 atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
3467 atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003468 }
3469}
3470
3471static int binder_has_proc_work(struct binder_proc *proc,
3472 struct binder_thread *thread)
3473{
Todd Kjos72196392017-06-29 12:02:02 -07003474 return !binder_worklist_empty(proc, &proc->todo) ||
3475 thread->looper_need_return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003476}
3477
3478static int binder_has_thread_work(struct binder_thread *thread)
3479{
Todd Kjos72196392017-06-29 12:02:02 -07003480 return !binder_worklist_empty(thread->proc, &thread->todo) ||
3481 thread->looper_need_return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003482}
3483
Todd Kjos26b47d82017-06-29 12:01:47 -07003484static int binder_put_node_cmd(struct binder_proc *proc,
3485 struct binder_thread *thread,
3486 void __user **ptrp,
3487 binder_uintptr_t node_ptr,
3488 binder_uintptr_t node_cookie,
3489 int node_debug_id,
3490 uint32_t cmd, const char *cmd_name)
3491{
3492 void __user *ptr = *ptrp;
3493
3494 if (put_user(cmd, (uint32_t __user *)ptr))
3495 return -EFAULT;
3496 ptr += sizeof(uint32_t);
3497
3498 if (put_user(node_ptr, (binder_uintptr_t __user *)ptr))
3499 return -EFAULT;
3500 ptr += sizeof(binder_uintptr_t);
3501
3502 if (put_user(node_cookie, (binder_uintptr_t __user *)ptr))
3503 return -EFAULT;
3504 ptr += sizeof(binder_uintptr_t);
3505
3506 binder_stat_br(proc, thread, cmd);
3507 binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n",
3508 proc->pid, thread->pid, cmd_name, node_debug_id,
3509 (u64)node_ptr, (u64)node_cookie);
3510
3511 *ptrp = ptr;
3512 return 0;
3513}
3514
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003515static int binder_thread_read(struct binder_proc *proc,
3516 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003517 binder_uintptr_t binder_buffer, size_t size,
3518 binder_size_t *consumed, int non_block)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003519{
Arve Hjønnevågda498892014-02-21 14:40:26 -08003520 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003521 void __user *ptr = buffer + *consumed;
3522 void __user *end = buffer + size;
3523
3524 int ret = 0;
3525 int wait_for_proc_work;
3526
3527 if (*consumed == 0) {
3528 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
3529 return -EFAULT;
3530 ptr += sizeof(uint32_t);
3531 }
3532
3533retry:
Martijn Coenen0b89d692017-06-29 12:02:06 -07003534 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003535 wait_for_proc_work = thread->transaction_stack == NULL &&
Martijn Coenen0b89d692017-06-29 12:02:06 -07003536 binder_worklist_empty_ilocked(&thread->todo);
Todd Kjosb3e68612017-06-29 12:02:07 -07003537 if (wait_for_proc_work)
3538 proc->ready_threads++;
Martijn Coenen0b89d692017-06-29 12:02:06 -07003539 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003540
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003541 thread->looper |= BINDER_LOOPER_STATE_WAITING;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003542
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003543 trace_binder_wait_for_work(wait_for_proc_work,
3544 !!thread->transaction_stack,
Todd Kjos72196392017-06-29 12:02:02 -07003545 !binder_worklist_empty(proc, &thread->todo));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003546 if (wait_for_proc_work) {
3547 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
3548 BINDER_LOOPER_STATE_ENTERED))) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303549 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 +09003550 proc->pid, thread->pid, thread->looper);
3551 wait_event_interruptible(binder_user_error_wait,
3552 binder_stop_on_user_error < 2);
3553 }
3554 binder_set_nice(proc->default_priority);
3555 if (non_block) {
3556 if (!binder_has_proc_work(proc, thread))
3557 ret = -EAGAIN;
3558 } else
Colin Crosse2610b22013-05-06 23:50:15 +00003559 ret = wait_event_freezable_exclusive(proc->wait, binder_has_proc_work(proc, thread));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003560 } else {
3561 if (non_block) {
3562 if (!binder_has_thread_work(thread))
3563 ret = -EAGAIN;
3564 } else
Colin Crosse2610b22013-05-06 23:50:15 +00003565 ret = wait_event_freezable(thread->wait, binder_has_thread_work(thread));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003566 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003567
Todd Kjosb3e68612017-06-29 12:02:07 -07003568 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003569 if (wait_for_proc_work)
3570 proc->ready_threads--;
Todd Kjosb3e68612017-06-29 12:02:07 -07003571 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003572 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
3573
3574 if (ret)
3575 return ret;
3576
3577 while (1) {
3578 uint32_t cmd;
3579 struct binder_transaction_data tr;
Todd Kjos72196392017-06-29 12:02:02 -07003580 struct binder_work *w = NULL;
3581 struct list_head *list = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003582 struct binder_transaction *t = NULL;
Todd Kjos7a4408c2017-06-29 12:01:57 -07003583 struct binder_thread *t_from;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003584
Todd Kjosed297212017-06-29 12:02:01 -07003585 binder_inner_proc_lock(proc);
Todd Kjos72196392017-06-29 12:02:02 -07003586 if (!binder_worklist_empty_ilocked(&thread->todo))
3587 list = &thread->todo;
3588 else if (!binder_worklist_empty_ilocked(&proc->todo) &&
3589 wait_for_proc_work)
3590 list = &proc->todo;
3591 else {
3592 binder_inner_proc_unlock(proc);
3593
Dmitry Voytik395262a2014-09-08 18:16:34 +04003594 /* no data added */
Todd Kjos08dabce2017-06-29 12:01:49 -07003595 if (ptr - buffer == 4 && !thread->looper_need_return)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003596 goto retry;
3597 break;
3598 }
3599
Todd Kjosed297212017-06-29 12:02:01 -07003600 if (end - ptr < sizeof(tr) + 4) {
3601 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003602 break;
Todd Kjosed297212017-06-29 12:02:01 -07003603 }
Todd Kjos72196392017-06-29 12:02:02 -07003604 w = binder_dequeue_work_head_ilocked(list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003605
3606 switch (w->type) {
3607 case BINDER_WORK_TRANSACTION: {
Todd Kjosed297212017-06-29 12:02:01 -07003608 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003609 t = container_of(w, struct binder_transaction, work);
3610 } break;
Todd Kjos26549d12017-06-29 12:01:55 -07003611 case BINDER_WORK_RETURN_ERROR: {
3612 struct binder_error *e = container_of(
3613 w, struct binder_error, work);
3614
3615 WARN_ON(e->cmd == BR_OK);
Todd Kjosed297212017-06-29 12:02:01 -07003616 binder_inner_proc_unlock(proc);
Todd Kjos26549d12017-06-29 12:01:55 -07003617 if (put_user(e->cmd, (uint32_t __user *)ptr))
3618 return -EFAULT;
3619 e->cmd = BR_OK;
3620 ptr += sizeof(uint32_t);
3621
Todd Kjos4f9adc82017-08-08 15:48:36 -07003622 binder_stat_br(proc, thread, e->cmd);
Todd Kjos26549d12017-06-29 12:01:55 -07003623 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003624 case BINDER_WORK_TRANSACTION_COMPLETE: {
Todd Kjosed297212017-06-29 12:02:01 -07003625 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003626 cmd = BR_TRANSACTION_COMPLETE;
3627 if (put_user(cmd, (uint32_t __user *)ptr))
3628 return -EFAULT;
3629 ptr += sizeof(uint32_t);
3630
3631 binder_stat_br(proc, thread, cmd);
3632 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303633 "%d:%d BR_TRANSACTION_COMPLETE\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003634 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003635 kfree(w);
3636 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3637 } break;
3638 case BINDER_WORK_NODE: {
3639 struct binder_node *node = container_of(w, struct binder_node, work);
Todd Kjos26b47d82017-06-29 12:01:47 -07003640 int strong, weak;
3641 binder_uintptr_t node_ptr = node->ptr;
3642 binder_uintptr_t node_cookie = node->cookie;
3643 int node_debug_id = node->debug_id;
3644 int has_weak_ref;
3645 int has_strong_ref;
3646 void __user *orig_ptr = ptr;
Seunghun Lee10f62862014-05-01 01:30:23 +09003647
Todd Kjos26b47d82017-06-29 12:01:47 -07003648 BUG_ON(proc != node->proc);
3649 strong = node->internal_strong_refs ||
3650 node->local_strong_refs;
3651 weak = !hlist_empty(&node->refs) ||
Todd Kjosadc18842017-06-29 12:01:59 -07003652 node->local_weak_refs ||
3653 node->tmp_refs || strong;
Todd Kjos26b47d82017-06-29 12:01:47 -07003654 has_strong_ref = node->has_strong_ref;
3655 has_weak_ref = node->has_weak_ref;
3656
3657 if (weak && !has_weak_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003658 node->has_weak_ref = 1;
3659 node->pending_weak_ref = 1;
3660 node->local_weak_refs++;
Todd Kjos26b47d82017-06-29 12:01:47 -07003661 }
3662 if (strong && !has_strong_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003663 node->has_strong_ref = 1;
3664 node->pending_strong_ref = 1;
3665 node->local_strong_refs++;
Todd Kjos26b47d82017-06-29 12:01:47 -07003666 }
3667 if (!strong && has_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003668 node->has_strong_ref = 0;
Todd Kjos26b47d82017-06-29 12:01:47 -07003669 if (!weak && has_weak_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003670 node->has_weak_ref = 0;
Todd Kjos26b47d82017-06-29 12:01:47 -07003671 if (!weak && !strong) {
3672 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
3673 "%d:%d node %d u%016llx c%016llx deleted\n",
3674 proc->pid, thread->pid,
3675 node_debug_id,
3676 (u64)node_ptr,
3677 (u64)node_cookie);
3678 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjosed297212017-06-29 12:02:01 -07003679 binder_inner_proc_unlock(proc);
Todd Kjos673068e2017-06-29 12:02:03 -07003680 binder_node_lock(node);
3681 /*
3682 * Acquire the node lock before freeing the
3683 * node to serialize with other threads that
3684 * may have been holding the node lock while
3685 * decrementing this node (avoids race where
3686 * this thread frees while the other thread
3687 * is unlocking the node after the final
3688 * decrement)
3689 */
3690 binder_node_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07003691 binder_free_node(node);
3692 } else
3693 binder_inner_proc_unlock(proc);
3694
Todd Kjos26b47d82017-06-29 12:01:47 -07003695 if (weak && !has_weak_ref)
3696 ret = binder_put_node_cmd(
3697 proc, thread, &ptr, node_ptr,
3698 node_cookie, node_debug_id,
3699 BR_INCREFS, "BR_INCREFS");
3700 if (!ret && strong && !has_strong_ref)
3701 ret = binder_put_node_cmd(
3702 proc, thread, &ptr, node_ptr,
3703 node_cookie, node_debug_id,
3704 BR_ACQUIRE, "BR_ACQUIRE");
3705 if (!ret && !strong && has_strong_ref)
3706 ret = binder_put_node_cmd(
3707 proc, thread, &ptr, node_ptr,
3708 node_cookie, node_debug_id,
3709 BR_RELEASE, "BR_RELEASE");
3710 if (!ret && !weak && has_weak_ref)
3711 ret = binder_put_node_cmd(
3712 proc, thread, &ptr, node_ptr,
3713 node_cookie, node_debug_id,
3714 BR_DECREFS, "BR_DECREFS");
3715 if (orig_ptr == ptr)
3716 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
3717 "%d:%d node %d u%016llx c%016llx state unchanged\n",
3718 proc->pid, thread->pid,
3719 node_debug_id,
3720 (u64)node_ptr,
3721 (u64)node_cookie);
3722 if (ret)
3723 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003724 } break;
3725 case BINDER_WORK_DEAD_BINDER:
3726 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
3727 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
3728 struct binder_ref_death *death;
3729 uint32_t cmd;
Martijn Coenenab51ec62017-06-29 12:02:10 -07003730 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003731
3732 death = container_of(w, struct binder_ref_death, work);
3733 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
3734 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
3735 else
3736 cmd = BR_DEAD_BINDER;
Martijn Coenenab51ec62017-06-29 12:02:10 -07003737 cookie = death->cookie;
3738
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003739 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003740 "%d:%d %s %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003741 proc->pid, thread->pid,
3742 cmd == BR_DEAD_BINDER ?
3743 "BR_DEAD_BINDER" :
3744 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
Martijn Coenenab51ec62017-06-29 12:02:10 -07003745 (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003746 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
Martijn Coenenab51ec62017-06-29 12:02:10 -07003747 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003748 kfree(death);
3749 binder_stats_deleted(BINDER_STAT_DEATH);
Todd Kjosed297212017-06-29 12:02:01 -07003750 } else {
Todd Kjos72196392017-06-29 12:02:02 -07003751 binder_enqueue_work_ilocked(
3752 w, &proc->delivered_death);
Todd Kjosed297212017-06-29 12:02:01 -07003753 binder_inner_proc_unlock(proc);
3754 }
Martijn Coenenab51ec62017-06-29 12:02:10 -07003755 if (put_user(cmd, (uint32_t __user *)ptr))
3756 return -EFAULT;
3757 ptr += sizeof(uint32_t);
3758 if (put_user(cookie,
3759 (binder_uintptr_t __user *)ptr))
3760 return -EFAULT;
3761 ptr += sizeof(binder_uintptr_t);
3762 binder_stat_br(proc, thread, cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003763 if (cmd == BR_DEAD_BINDER)
3764 goto done; /* DEAD_BINDER notifications can cause transactions */
3765 } break;
3766 }
3767
3768 if (!t)
3769 continue;
3770
3771 BUG_ON(t->buffer == NULL);
3772 if (t->buffer->target_node) {
3773 struct binder_node *target_node = t->buffer->target_node;
Seunghun Lee10f62862014-05-01 01:30:23 +09003774
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003775 tr.target.ptr = target_node->ptr;
3776 tr.cookie = target_node->cookie;
3777 t->saved_priority = task_nice(current);
3778 if (t->priority < target_node->min_priority &&
3779 !(t->flags & TF_ONE_WAY))
3780 binder_set_nice(t->priority);
3781 else if (!(t->flags & TF_ONE_WAY) ||
3782 t->saved_priority > target_node->min_priority)
3783 binder_set_nice(target_node->min_priority);
3784 cmd = BR_TRANSACTION;
3785 } else {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003786 tr.target.ptr = 0;
3787 tr.cookie = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003788 cmd = BR_REPLY;
3789 }
3790 tr.code = t->code;
3791 tr.flags = t->flags;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -06003792 tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003793
Todd Kjos7a4408c2017-06-29 12:01:57 -07003794 t_from = binder_get_txn_from(t);
3795 if (t_from) {
3796 struct task_struct *sender = t_from->proc->tsk;
Seunghun Lee10f62862014-05-01 01:30:23 +09003797
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003798 tr.sender_pid = task_tgid_nr_ns(sender,
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003799 task_active_pid_ns(current));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003800 } else {
3801 tr.sender_pid = 0;
3802 }
3803
3804 tr.data_size = t->buffer->data_size;
3805 tr.offsets_size = t->buffer->offsets_size;
Todd Kjos19c98722017-06-29 12:01:40 -07003806 tr.data.ptr.buffer = (binder_uintptr_t)
3807 ((uintptr_t)t->buffer->data +
3808 binder_alloc_get_user_buffer_offset(&proc->alloc));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003809 tr.data.ptr.offsets = tr.data.ptr.buffer +
3810 ALIGN(t->buffer->data_size,
3811 sizeof(void *));
3812
Todd Kjos7a4408c2017-06-29 12:01:57 -07003813 if (put_user(cmd, (uint32_t __user *)ptr)) {
3814 if (t_from)
3815 binder_thread_dec_tmpref(t_from);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003816 return -EFAULT;
Todd Kjos7a4408c2017-06-29 12:01:57 -07003817 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003818 ptr += sizeof(uint32_t);
Todd Kjos7a4408c2017-06-29 12:01:57 -07003819 if (copy_to_user(ptr, &tr, sizeof(tr))) {
3820 if (t_from)
3821 binder_thread_dec_tmpref(t_from);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003822 return -EFAULT;
Todd Kjos7a4408c2017-06-29 12:01:57 -07003823 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003824 ptr += sizeof(tr);
3825
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003826 trace_binder_transaction_received(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003827 binder_stat_br(proc, thread, cmd);
3828 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003829 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003830 proc->pid, thread->pid,
3831 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
3832 "BR_REPLY",
Todd Kjos7a4408c2017-06-29 12:01:57 -07003833 t->debug_id, t_from ? t_from->proc->pid : 0,
3834 t_from ? t_from->pid : 0, cmd,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003835 t->buffer->data_size, t->buffer->offsets_size,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003836 (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003837
Todd Kjos7a4408c2017-06-29 12:01:57 -07003838 if (t_from)
3839 binder_thread_dec_tmpref(t_from);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003840 t->buffer->allow_user_free = 1;
3841 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
Martijn Coenen0b89d692017-06-29 12:02:06 -07003842 binder_inner_proc_lock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003843 t->to_parent = thread->transaction_stack;
3844 t->to_thread = thread;
3845 thread->transaction_stack = t;
Martijn Coenen0b89d692017-06-29 12:02:06 -07003846 binder_inner_proc_unlock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003847 } else {
Todd Kjosb6d282c2017-06-29 12:01:54 -07003848 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003849 }
3850 break;
3851 }
3852
3853done:
3854
3855 *consumed = ptr - buffer;
Todd Kjosb3e68612017-06-29 12:02:07 -07003856 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003857 if (proc->requested_threads + proc->ready_threads == 0 &&
3858 proc->requested_threads_started < proc->max_threads &&
3859 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
3860 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
3861 /*spawn a new thread if we leave this out */) {
3862 proc->requested_threads++;
Todd Kjosb3e68612017-06-29 12:02:07 -07003863 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003864 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303865 "%d:%d BR_SPAWN_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003866 proc->pid, thread->pid);
3867 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
3868 return -EFAULT;
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07003869 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
Todd Kjosb3e68612017-06-29 12:02:07 -07003870 } else
3871 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003872 return 0;
3873}
3874
Todd Kjos72196392017-06-29 12:02:02 -07003875static void binder_release_work(struct binder_proc *proc,
3876 struct list_head *list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003877{
3878 struct binder_work *w;
Seunghun Lee10f62862014-05-01 01:30:23 +09003879
Todd Kjos72196392017-06-29 12:02:02 -07003880 while (1) {
3881 w = binder_dequeue_work_head(proc, list);
3882 if (!w)
3883 return;
3884
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003885 switch (w->type) {
3886 case BINDER_WORK_TRANSACTION: {
3887 struct binder_transaction *t;
3888
3889 t = container_of(w, struct binder_transaction, work);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003890 if (t->buffer->target_node &&
3891 !(t->flags & TF_ONE_WAY)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003892 binder_send_failed_reply(t, BR_DEAD_REPLY);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003893 } else {
3894 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303895 "undelivered transaction %d\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003896 t->debug_id);
Todd Kjosb6d282c2017-06-29 12:01:54 -07003897 binder_free_transaction(t);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003898 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003899 } break;
Todd Kjos26549d12017-06-29 12:01:55 -07003900 case BINDER_WORK_RETURN_ERROR: {
3901 struct binder_error *e = container_of(
3902 w, struct binder_error, work);
3903
3904 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
3905 "undelivered TRANSACTION_ERROR: %u\n",
3906 e->cmd);
3907 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003908 case BINDER_WORK_TRANSACTION_COMPLETE: {
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003909 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303910 "undelivered TRANSACTION_COMPLETE\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003911 kfree(w);
3912 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3913 } break;
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003914 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
3915 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
3916 struct binder_ref_death *death;
3917
3918 death = container_of(w, struct binder_ref_death, work);
3919 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003920 "undelivered death notification, %016llx\n",
3921 (u64)death->cookie);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003922 kfree(death);
3923 binder_stats_deleted(BINDER_STAT_DEATH);
3924 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003925 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303926 pr_err("unexpected work type, %d, not freed\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003927 w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003928 break;
3929 }
3930 }
3931
3932}
3933
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07003934static struct binder_thread *binder_get_thread_ilocked(
3935 struct binder_proc *proc, struct binder_thread *new_thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003936{
3937 struct binder_thread *thread = NULL;
3938 struct rb_node *parent = NULL;
3939 struct rb_node **p = &proc->threads.rb_node;
3940
3941 while (*p) {
3942 parent = *p;
3943 thread = rb_entry(parent, struct binder_thread, rb_node);
3944
3945 if (current->pid < thread->pid)
3946 p = &(*p)->rb_left;
3947 else if (current->pid > thread->pid)
3948 p = &(*p)->rb_right;
3949 else
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07003950 return thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003951 }
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07003952 if (!new_thread)
3953 return NULL;
3954 thread = new_thread;
3955 binder_stats_created(BINDER_STAT_THREAD);
3956 thread->proc = proc;
3957 thread->pid = current->pid;
3958 atomic_set(&thread->tmp_ref, 0);
3959 init_waitqueue_head(&thread->wait);
3960 INIT_LIST_HEAD(&thread->todo);
3961 rb_link_node(&thread->rb_node, parent, p);
3962 rb_insert_color(&thread->rb_node, &proc->threads);
3963 thread->looper_need_return = true;
3964 thread->return_error.work.type = BINDER_WORK_RETURN_ERROR;
3965 thread->return_error.cmd = BR_OK;
3966 thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR;
3967 thread->reply_error.cmd = BR_OK;
3968
3969 return thread;
3970}
3971
3972static struct binder_thread *binder_get_thread(struct binder_proc *proc)
3973{
3974 struct binder_thread *thread;
3975 struct binder_thread *new_thread;
3976
3977 binder_inner_proc_lock(proc);
3978 thread = binder_get_thread_ilocked(proc, NULL);
3979 binder_inner_proc_unlock(proc);
3980 if (!thread) {
3981 new_thread = kzalloc(sizeof(*thread), GFP_KERNEL);
3982 if (new_thread == NULL)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003983 return NULL;
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07003984 binder_inner_proc_lock(proc);
3985 thread = binder_get_thread_ilocked(proc, new_thread);
3986 binder_inner_proc_unlock(proc);
3987 if (thread != new_thread)
3988 kfree(new_thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003989 }
3990 return thread;
3991}
3992
Todd Kjos7a4408c2017-06-29 12:01:57 -07003993static void binder_free_proc(struct binder_proc *proc)
3994{
3995 BUG_ON(!list_empty(&proc->todo));
3996 BUG_ON(!list_empty(&proc->delivered_death));
3997 binder_alloc_deferred_release(&proc->alloc);
3998 put_task_struct(proc->tsk);
3999 binder_stats_deleted(BINDER_STAT_PROC);
4000 kfree(proc);
4001}
4002
4003static void binder_free_thread(struct binder_thread *thread)
4004{
4005 BUG_ON(!list_empty(&thread->todo));
4006 binder_stats_deleted(BINDER_STAT_THREAD);
4007 binder_proc_dec_tmpref(thread->proc);
4008 kfree(thread);
4009}
4010
4011static int binder_thread_release(struct binder_proc *proc,
4012 struct binder_thread *thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004013{
4014 struct binder_transaction *t;
4015 struct binder_transaction *send_reply = NULL;
4016 int active_transactions = 0;
Todd Kjos7a4408c2017-06-29 12:01:57 -07004017 struct binder_transaction *last_t = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004018
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004019 binder_inner_proc_lock(thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07004020 /*
4021 * take a ref on the proc so it survives
4022 * after we remove this thread from proc->threads.
4023 * The corresponding dec is when we actually
4024 * free the thread in binder_free_thread()
4025 */
4026 proc->tmp_ref++;
4027 /*
4028 * take a ref on this thread to ensure it
4029 * survives while we are releasing it
4030 */
4031 atomic_inc(&thread->tmp_ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004032 rb_erase(&thread->rb_node, &proc->threads);
4033 t = thread->transaction_stack;
Todd Kjos7a4408c2017-06-29 12:01:57 -07004034 if (t) {
4035 spin_lock(&t->lock);
4036 if (t->to_thread == thread)
4037 send_reply = t;
4038 }
4039 thread->is_dead = true;
4040
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004041 while (t) {
Todd Kjos7a4408c2017-06-29 12:01:57 -07004042 last_t = t;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004043 active_transactions++;
4044 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304045 "release %d:%d transaction %d %s, still active\n",
4046 proc->pid, thread->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004047 t->debug_id,
4048 (t->to_thread == thread) ? "in" : "out");
4049
4050 if (t->to_thread == thread) {
4051 t->to_proc = NULL;
4052 t->to_thread = NULL;
4053 if (t->buffer) {
4054 t->buffer->transaction = NULL;
4055 t->buffer = NULL;
4056 }
4057 t = t->to_parent;
4058 } else if (t->from == thread) {
4059 t->from = NULL;
4060 t = t->from_parent;
4061 } else
4062 BUG();
Todd Kjos7a4408c2017-06-29 12:01:57 -07004063 spin_unlock(&last_t->lock);
4064 if (t)
4065 spin_lock(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004066 }
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004067 binder_inner_proc_unlock(thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07004068
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004069 if (send_reply)
4070 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
Todd Kjos72196392017-06-29 12:02:02 -07004071 binder_release_work(proc, &thread->todo);
Todd Kjos7a4408c2017-06-29 12:01:57 -07004072 binder_thread_dec_tmpref(thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004073 return active_transactions;
4074}
4075
4076static unsigned int binder_poll(struct file *filp,
4077 struct poll_table_struct *wait)
4078{
4079 struct binder_proc *proc = filp->private_data;
4080 struct binder_thread *thread = NULL;
4081 int wait_for_proc_work;
4082
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004083 thread = binder_get_thread(proc);
4084
Martijn Coenen0b89d692017-06-29 12:02:06 -07004085 binder_inner_proc_lock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004086 wait_for_proc_work = thread->transaction_stack == NULL &&
Martijn Coenen0b89d692017-06-29 12:02:06 -07004087 binder_worklist_empty_ilocked(&thread->todo);
4088 binder_inner_proc_unlock(thread->proc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004089
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004090 if (wait_for_proc_work) {
4091 if (binder_has_proc_work(proc, thread))
4092 return POLLIN;
4093 poll_wait(filp, &proc->wait, wait);
4094 if (binder_has_proc_work(proc, thread))
4095 return POLLIN;
4096 } else {
4097 if (binder_has_thread_work(thread))
4098 return POLLIN;
4099 poll_wait(filp, &thread->wait, wait);
4100 if (binder_has_thread_work(thread))
4101 return POLLIN;
4102 }
4103 return 0;
4104}
4105
Tair Rzayev78260ac2014-06-03 22:27:21 +03004106static int binder_ioctl_write_read(struct file *filp,
4107 unsigned int cmd, unsigned long arg,
4108 struct binder_thread *thread)
4109{
4110 int ret = 0;
4111 struct binder_proc *proc = filp->private_data;
4112 unsigned int size = _IOC_SIZE(cmd);
4113 void __user *ubuf = (void __user *)arg;
4114 struct binder_write_read bwr;
4115
4116 if (size != sizeof(struct binder_write_read)) {
4117 ret = -EINVAL;
4118 goto out;
4119 }
4120 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
4121 ret = -EFAULT;
4122 goto out;
4123 }
4124 binder_debug(BINDER_DEBUG_READ_WRITE,
4125 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
4126 proc->pid, thread->pid,
4127 (u64)bwr.write_size, (u64)bwr.write_buffer,
4128 (u64)bwr.read_size, (u64)bwr.read_buffer);
4129
4130 if (bwr.write_size > 0) {
4131 ret = binder_thread_write(proc, thread,
4132 bwr.write_buffer,
4133 bwr.write_size,
4134 &bwr.write_consumed);
4135 trace_binder_write_done(ret);
4136 if (ret < 0) {
4137 bwr.read_consumed = 0;
4138 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4139 ret = -EFAULT;
4140 goto out;
4141 }
4142 }
4143 if (bwr.read_size > 0) {
4144 ret = binder_thread_read(proc, thread, bwr.read_buffer,
4145 bwr.read_size,
4146 &bwr.read_consumed,
4147 filp->f_flags & O_NONBLOCK);
4148 trace_binder_read_done(ret);
Todd Kjos72196392017-06-29 12:02:02 -07004149 if (!binder_worklist_empty(proc, &proc->todo))
Tair Rzayev78260ac2014-06-03 22:27:21 +03004150 wake_up_interruptible(&proc->wait);
4151 if (ret < 0) {
4152 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4153 ret = -EFAULT;
4154 goto out;
4155 }
4156 }
4157 binder_debug(BINDER_DEBUG_READ_WRITE,
4158 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
4159 proc->pid, thread->pid,
4160 (u64)bwr.write_consumed, (u64)bwr.write_size,
4161 (u64)bwr.read_consumed, (u64)bwr.read_size);
4162 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
4163 ret = -EFAULT;
4164 goto out;
4165 }
4166out:
4167 return ret;
4168}
4169
4170static int binder_ioctl_set_ctx_mgr(struct file *filp)
4171{
4172 int ret = 0;
4173 struct binder_proc *proc = filp->private_data;
Martijn Coenen342e5c92017-02-03 14:40:46 -08004174 struct binder_context *context = proc->context;
Todd Kjosc44b1232017-06-29 12:01:43 -07004175 struct binder_node *new_node;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004176 kuid_t curr_euid = current_euid();
4177
Todd Kjosc44b1232017-06-29 12:01:43 -07004178 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen342e5c92017-02-03 14:40:46 -08004179 if (context->binder_context_mgr_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004180 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
4181 ret = -EBUSY;
4182 goto out;
4183 }
Stephen Smalley79af7302015-01-21 10:54:10 -05004184 ret = security_binder_set_context_mgr(proc->tsk);
4185 if (ret < 0)
4186 goto out;
Martijn Coenen342e5c92017-02-03 14:40:46 -08004187 if (uid_valid(context->binder_context_mgr_uid)) {
4188 if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004189 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
4190 from_kuid(&init_user_ns, curr_euid),
4191 from_kuid(&init_user_ns,
Martijn Coenen342e5c92017-02-03 14:40:46 -08004192 context->binder_context_mgr_uid));
Tair Rzayev78260ac2014-06-03 22:27:21 +03004193 ret = -EPERM;
4194 goto out;
4195 }
4196 } else {
Martijn Coenen342e5c92017-02-03 14:40:46 -08004197 context->binder_context_mgr_uid = curr_euid;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004198 }
Todd Kjos673068e2017-06-29 12:02:03 -07004199 new_node = binder_new_node(proc, NULL);
Todd Kjosc44b1232017-06-29 12:01:43 -07004200 if (!new_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004201 ret = -ENOMEM;
4202 goto out;
4203 }
Todd Kjos673068e2017-06-29 12:02:03 -07004204 binder_node_lock(new_node);
Todd Kjosc44b1232017-06-29 12:01:43 -07004205 new_node->local_weak_refs++;
4206 new_node->local_strong_refs++;
4207 new_node->has_strong_ref = 1;
4208 new_node->has_weak_ref = 1;
4209 context->binder_context_mgr_node = new_node;
Todd Kjos673068e2017-06-29 12:02:03 -07004210 binder_node_unlock(new_node);
Todd Kjosadc18842017-06-29 12:01:59 -07004211 binder_put_node(new_node);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004212out:
Todd Kjosc44b1232017-06-29 12:01:43 -07004213 mutex_unlock(&context->context_mgr_node_lock);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004214 return ret;
4215}
4216
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004217static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4218{
4219 int ret;
4220 struct binder_proc *proc = filp->private_data;
4221 struct binder_thread *thread;
4222 unsigned int size = _IOC_SIZE(cmd);
4223 void __user *ubuf = (void __user *)arg;
4224
Tair Rzayev78260ac2014-06-03 22:27:21 +03004225 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
4226 proc->pid, current->pid, cmd, arg);*/
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004227
Sherry Yang4175e2b2017-08-23 08:46:40 -07004228 binder_selftest_alloc(&proc->alloc);
4229
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004230 trace_binder_ioctl(cmd, arg);
4231
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004232 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4233 if (ret)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004234 goto err_unlocked;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004235
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004236 thread = binder_get_thread(proc);
4237 if (thread == NULL) {
4238 ret = -ENOMEM;
4239 goto err;
4240 }
4241
4242 switch (cmd) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004243 case BINDER_WRITE_READ:
4244 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
4245 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004246 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004247 break;
Todd Kjosb3e68612017-06-29 12:02:07 -07004248 case BINDER_SET_MAX_THREADS: {
4249 int max_threads;
4250
4251 if (copy_from_user(&max_threads, ubuf,
4252 sizeof(max_threads))) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004253 ret = -EINVAL;
4254 goto err;
4255 }
Todd Kjosb3e68612017-06-29 12:02:07 -07004256 binder_inner_proc_lock(proc);
4257 proc->max_threads = max_threads;
4258 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004259 break;
Todd Kjosb3e68612017-06-29 12:02:07 -07004260 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004261 case BINDER_SET_CONTEXT_MGR:
Tair Rzayev78260ac2014-06-03 22:27:21 +03004262 ret = binder_ioctl_set_ctx_mgr(filp);
4263 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004264 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004265 break;
4266 case BINDER_THREAD_EXIT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304267 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004268 proc->pid, thread->pid);
Todd Kjos7a4408c2017-06-29 12:01:57 -07004269 binder_thread_release(proc, thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004270 thread = NULL;
4271 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02004272 case BINDER_VERSION: {
4273 struct binder_version __user *ver = ubuf;
4274
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004275 if (size != sizeof(struct binder_version)) {
4276 ret = -EINVAL;
4277 goto err;
4278 }
Mathieu Maret36c89c02014-04-15 12:03:05 +02004279 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
4280 &ver->protocol_version)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004281 ret = -EINVAL;
4282 goto err;
4283 }
4284 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02004285 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004286 default:
4287 ret = -EINVAL;
4288 goto err;
4289 }
4290 ret = 0;
4291err:
4292 if (thread)
Todd Kjos08dabce2017-06-29 12:01:49 -07004293 thread->looper_need_return = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004294 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4295 if (ret && ret != -ERESTARTSYS)
Anmol Sarma56b468f2012-10-30 22:35:43 +05304296 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 -07004297err_unlocked:
4298 trace_binder_ioctl_done(ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004299 return ret;
4300}
4301
4302static void binder_vma_open(struct vm_area_struct *vma)
4303{
4304 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004305
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004306 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304307 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004308 proc->pid, vma->vm_start, vma->vm_end,
4309 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4310 (unsigned long)pgprot_val(vma->vm_page_prot));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004311}
4312
4313static void binder_vma_close(struct vm_area_struct *vma)
4314{
4315 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004316
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004317 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304318 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004319 proc->pid, vma->vm_start, vma->vm_end,
4320 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4321 (unsigned long)pgprot_val(vma->vm_page_prot));
Todd Kjos19c98722017-06-29 12:01:40 -07004322 binder_alloc_vma_close(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004323 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
4324}
4325
Dave Jiang11bac802017-02-24 14:56:41 -08004326static int binder_vm_fault(struct vm_fault *vmf)
Vinayak Menonddac7d52014-06-02 18:17:59 +05304327{
4328 return VM_FAULT_SIGBUS;
4329}
4330
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07004331static const struct vm_operations_struct binder_vm_ops = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004332 .open = binder_vma_open,
4333 .close = binder_vma_close,
Vinayak Menonddac7d52014-06-02 18:17:59 +05304334 .fault = binder_vm_fault,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004335};
4336
Todd Kjos19c98722017-06-29 12:01:40 -07004337static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
4338{
4339 int ret;
4340 struct binder_proc *proc = filp->private_data;
4341 const char *failure_string;
4342
4343 if (proc->tsk != current->group_leader)
4344 return -EINVAL;
4345
4346 if ((vma->vm_end - vma->vm_start) > SZ_4M)
4347 vma->vm_end = vma->vm_start + SZ_4M;
4348
4349 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4350 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
4351 __func__, proc->pid, vma->vm_start, vma->vm_end,
4352 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4353 (unsigned long)pgprot_val(vma->vm_page_prot));
4354
4355 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
4356 ret = -EPERM;
4357 failure_string = "bad vm_flags";
4358 goto err_bad_arg;
4359 }
4360 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
4361 vma->vm_ops = &binder_vm_ops;
4362 vma->vm_private_data = proc;
4363
4364 ret = binder_alloc_mmap_handler(&proc->alloc, vma);
4365 if (ret)
4366 return ret;
4367 proc->files = get_files_struct(current);
4368 return 0;
4369
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004370err_bad_arg:
Sherwin Soltani258767f2012-06-26 02:00:30 -04004371 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004372 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
4373 return ret;
4374}
4375
4376static int binder_open(struct inode *nodp, struct file *filp)
4377{
4378 struct binder_proc *proc;
Martijn Coenenac4812c2017-02-03 14:40:48 -08004379 struct binder_device *binder_dev;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004380
4381 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
4382 current->group_leader->pid, current->pid);
4383
4384 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
4385 if (proc == NULL)
4386 return -ENOMEM;
Todd Kjos9630fe82017-06-29 12:02:00 -07004387 spin_lock_init(&proc->inner_lock);
4388 spin_lock_init(&proc->outer_lock);
Todd Kjosc4ea41b2017-06-29 12:01:36 -07004389 get_task_struct(current->group_leader);
4390 proc->tsk = current->group_leader;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004391 INIT_LIST_HEAD(&proc->todo);
4392 init_waitqueue_head(&proc->wait);
4393 proc->default_priority = task_nice(current);
Martijn Coenenac4812c2017-02-03 14:40:48 -08004394 binder_dev = container_of(filp->private_data, struct binder_device,
4395 miscdev);
4396 proc->context = &binder_dev->context;
Todd Kjos19c98722017-06-29 12:01:40 -07004397 binder_alloc_init(&proc->alloc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004398
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004399 binder_stats_created(BINDER_STAT_PROC);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004400 proc->pid = current->group_leader->pid;
4401 INIT_LIST_HEAD(&proc->delivered_death);
4402 filp->private_data = proc;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004403
Todd Kjosc44b1232017-06-29 12:01:43 -07004404 mutex_lock(&binder_procs_lock);
4405 hlist_add_head(&proc->proc_node, &binder_procs);
4406 mutex_unlock(&binder_procs_lock);
4407
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004408 if (binder_debugfs_dir_entry_proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004409 char strbuf[11];
Seunghun Lee10f62862014-05-01 01:30:23 +09004410
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004411 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
Martijn Coenen14db3182017-02-03 14:40:47 -08004412 /*
4413 * proc debug entries are shared between contexts, so
4414 * this will fail if the process tries to open the driver
4415 * again with a different context. The priting code will
4416 * anyway print all contexts that a given PID has, so this
4417 * is not a problem.
4418 */
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004419 proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
Martijn Coenen14db3182017-02-03 14:40:47 -08004420 binder_debugfs_dir_entry_proc,
4421 (void *)(unsigned long)proc->pid,
4422 &binder_proc_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004423 }
4424
4425 return 0;
4426}
4427
4428static int binder_flush(struct file *filp, fl_owner_t id)
4429{
4430 struct binder_proc *proc = filp->private_data;
4431
4432 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
4433
4434 return 0;
4435}
4436
4437static void binder_deferred_flush(struct binder_proc *proc)
4438{
4439 struct rb_node *n;
4440 int wake_count = 0;
Seunghun Lee10f62862014-05-01 01:30:23 +09004441
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004442 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004443 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
4444 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
Seunghun Lee10f62862014-05-01 01:30:23 +09004445
Todd Kjos08dabce2017-06-29 12:01:49 -07004446 thread->looper_need_return = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004447 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
4448 wake_up_interruptible(&thread->wait);
4449 wake_count++;
4450 }
4451 }
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004452 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004453 wake_up_interruptible_all(&proc->wait);
4454
4455 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4456 "binder_flush: %d woke %d threads\n", proc->pid,
4457 wake_count);
4458}
4459
4460static int binder_release(struct inode *nodp, struct file *filp)
4461{
4462 struct binder_proc *proc = filp->private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004463
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004464 debugfs_remove(proc->debugfs_entry);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004465 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
4466
4467 return 0;
4468}
4469
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004470static int binder_node_release(struct binder_node *node, int refs)
4471{
4472 struct binder_ref *ref;
4473 int death = 0;
Todd Kjosed297212017-06-29 12:02:01 -07004474 struct binder_proc *proc = node->proc;
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004475
Todd Kjos72196392017-06-29 12:02:02 -07004476 binder_release_work(proc, &node->async_todo);
Todd Kjosed297212017-06-29 12:02:01 -07004477
Todd Kjos673068e2017-06-29 12:02:03 -07004478 binder_node_lock(node);
Todd Kjosed297212017-06-29 12:02:01 -07004479 binder_inner_proc_lock(proc);
Todd Kjos72196392017-06-29 12:02:02 -07004480 binder_dequeue_work_ilocked(&node->work);
Todd Kjosadc18842017-06-29 12:01:59 -07004481 /*
4482 * The caller must have taken a temporary ref on the node,
4483 */
4484 BUG_ON(!node->tmp_refs);
4485 if (hlist_empty(&node->refs) && node->tmp_refs == 1) {
Todd Kjosed297212017-06-29 12:02:01 -07004486 binder_inner_proc_unlock(proc);
Todd Kjos673068e2017-06-29 12:02:03 -07004487 binder_node_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07004488 binder_free_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004489
4490 return refs;
4491 }
4492
4493 node->proc = NULL;
4494 node->local_strong_refs = 0;
4495 node->local_weak_refs = 0;
Todd Kjosed297212017-06-29 12:02:01 -07004496 binder_inner_proc_unlock(proc);
Todd Kjosc44b1232017-06-29 12:01:43 -07004497
4498 spin_lock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004499 hlist_add_head(&node->dead_node, &binder_dead_nodes);
Todd Kjosc44b1232017-06-29 12:01:43 -07004500 spin_unlock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004501
4502 hlist_for_each_entry(ref, &node->refs, node_entry) {
4503 refs++;
Martijn Coenenab51ec62017-06-29 12:02:10 -07004504 /*
4505 * Need the node lock to synchronize
4506 * with new notification requests and the
4507 * inner lock to synchronize with queued
4508 * death notifications.
4509 */
4510 binder_inner_proc_lock(ref->proc);
4511 if (!ref->death) {
4512 binder_inner_proc_unlock(ref->proc);
Arve Hjønnevåge194fd82014-02-17 13:58:29 -08004513 continue;
Martijn Coenenab51ec62017-06-29 12:02:10 -07004514 }
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004515
4516 death++;
4517
Martijn Coenenab51ec62017-06-29 12:02:10 -07004518 BUG_ON(!list_empty(&ref->death->work.entry));
4519 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
4520 binder_enqueue_work_ilocked(&ref->death->work,
4521 &ref->proc->todo);
4522 wake_up_interruptible(&ref->proc->wait);
Todd Kjos72196392017-06-29 12:02:02 -07004523 binder_inner_proc_unlock(ref->proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004524 }
4525
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004526 binder_debug(BINDER_DEBUG_DEAD_BINDER,
4527 "node %d now dead, refs %d, death %d\n",
4528 node->debug_id, refs, death);
Todd Kjos673068e2017-06-29 12:02:03 -07004529 binder_node_unlock(node);
Todd Kjosadc18842017-06-29 12:01:59 -07004530 binder_put_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004531
4532 return refs;
4533}
4534
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004535static void binder_deferred_release(struct binder_proc *proc)
4536{
Martijn Coenen342e5c92017-02-03 14:40:46 -08004537 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004538 struct rb_node *n;
Todd Kjos19c98722017-06-29 12:01:40 -07004539 int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004540
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004541 BUG_ON(proc->files);
4542
Todd Kjosc44b1232017-06-29 12:01:43 -07004543 mutex_lock(&binder_procs_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004544 hlist_del(&proc->proc_node);
Todd Kjosc44b1232017-06-29 12:01:43 -07004545 mutex_unlock(&binder_procs_lock);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004546
Todd Kjosc44b1232017-06-29 12:01:43 -07004547 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen342e5c92017-02-03 14:40:46 -08004548 if (context->binder_context_mgr_node &&
4549 context->binder_context_mgr_node->proc == proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004550 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01004551 "%s: %d context_mgr_node gone\n",
4552 __func__, proc->pid);
Martijn Coenen342e5c92017-02-03 14:40:46 -08004553 context->binder_context_mgr_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004554 }
Todd Kjosc44b1232017-06-29 12:01:43 -07004555 mutex_unlock(&context->context_mgr_node_lock);
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004556 binder_inner_proc_lock(proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07004557 /*
4558 * Make sure proc stays alive after we
4559 * remove all the threads
4560 */
4561 proc->tmp_ref++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004562
Todd Kjos7a4408c2017-06-29 12:01:57 -07004563 proc->is_dead = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004564 threads = 0;
4565 active_transactions = 0;
4566 while ((n = rb_first(&proc->threads))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004567 struct binder_thread *thread;
4568
4569 thread = rb_entry(n, struct binder_thread, rb_node);
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004570 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004571 threads++;
Todd Kjos7a4408c2017-06-29 12:01:57 -07004572 active_transactions += binder_thread_release(proc, thread);
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004573 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004574 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004575
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004576 nodes = 0;
4577 incoming_refs = 0;
4578 while ((n = rb_first(&proc->nodes))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004579 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004580
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004581 node = rb_entry(n, struct binder_node, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004582 nodes++;
Todd Kjosadc18842017-06-29 12:01:59 -07004583 /*
4584 * take a temporary ref on the node before
4585 * calling binder_node_release() which will either
4586 * kfree() the node or call binder_put_node()
4587 */
Todd Kjosda0fa9e2017-06-29 12:02:04 -07004588 binder_inc_node_tmpref_ilocked(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004589 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07004590 binder_inner_proc_unlock(proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004591 incoming_refs = binder_node_release(node, incoming_refs);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07004592 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004593 }
Todd Kjosda0fa9e2017-06-29 12:02:04 -07004594 binder_inner_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004595
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004596 outgoing_refs = 0;
Todd Kjos2c1838d2017-06-29 12:02:08 -07004597 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004598 while ((n = rb_first(&proc->refs_by_desc))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004599 struct binder_ref *ref;
4600
4601 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004602 outgoing_refs++;
Todd Kjos2c1838d2017-06-29 12:02:08 -07004603 binder_cleanup_ref_olocked(ref);
4604 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07004605 binder_free_ref(ref);
Todd Kjos2c1838d2017-06-29 12:02:08 -07004606 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004607 }
Todd Kjos2c1838d2017-06-29 12:02:08 -07004608 binder_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004609
Todd Kjos72196392017-06-29 12:02:02 -07004610 binder_release_work(proc, &proc->todo);
4611 binder_release_work(proc, &proc->delivered_death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004612
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004613 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Todd Kjos19c98722017-06-29 12:01:40 -07004614 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01004615 __func__, proc->pid, threads, nodes, incoming_refs,
Todd Kjos19c98722017-06-29 12:01:40 -07004616 outgoing_refs, active_transactions);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004617
Todd Kjos7a4408c2017-06-29 12:01:57 -07004618 binder_proc_dec_tmpref(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004619}
4620
4621static void binder_deferred_func(struct work_struct *work)
4622{
4623 struct binder_proc *proc;
4624 struct files_struct *files;
4625
4626 int defer;
Seunghun Lee10f62862014-05-01 01:30:23 +09004627
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004628 do {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004629 mutex_lock(&binder_deferred_lock);
4630 if (!hlist_empty(&binder_deferred_list)) {
4631 proc = hlist_entry(binder_deferred_list.first,
4632 struct binder_proc, deferred_work_node);
4633 hlist_del_init(&proc->deferred_work_node);
4634 defer = proc->deferred_work;
4635 proc->deferred_work = 0;
4636 } else {
4637 proc = NULL;
4638 defer = 0;
4639 }
4640 mutex_unlock(&binder_deferred_lock);
4641
4642 files = NULL;
4643 if (defer & BINDER_DEFERRED_PUT_FILES) {
4644 files = proc->files;
4645 if (files)
4646 proc->files = NULL;
4647 }
4648
4649 if (defer & BINDER_DEFERRED_FLUSH)
4650 binder_deferred_flush(proc);
4651
4652 if (defer & BINDER_DEFERRED_RELEASE)
4653 binder_deferred_release(proc); /* frees proc */
4654
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004655 if (files)
4656 put_files_struct(files);
4657 } while (proc);
4658}
4659static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
4660
4661static void
4662binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
4663{
4664 mutex_lock(&binder_deferred_lock);
4665 proc->deferred_work |= defer;
4666 if (hlist_unhashed(&proc->deferred_work_node)) {
4667 hlist_add_head(&proc->deferred_work_node,
4668 &binder_deferred_list);
Bhaktipriya Shridhar1beba522016-08-13 22:16:24 +05304669 schedule_work(&binder_deferred_work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004670 }
4671 mutex_unlock(&binder_deferred_lock);
4672}
4673
Todd Kjos5f2f63692017-06-29 12:02:09 -07004674static void print_binder_transaction_ilocked(struct seq_file *m,
4675 struct binder_proc *proc,
4676 const char *prefix,
4677 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004678{
Todd Kjos5f2f63692017-06-29 12:02:09 -07004679 struct binder_proc *to_proc;
4680 struct binder_buffer *buffer = t->buffer;
4681
4682 WARN_ON(!spin_is_locked(&proc->inner_lock));
Todd Kjos7a4408c2017-06-29 12:01:57 -07004683 spin_lock(&t->lock);
Todd Kjos5f2f63692017-06-29 12:02:09 -07004684 to_proc = t->to_proc;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004685 seq_printf(m,
4686 "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %ld r%d",
4687 prefix, t->debug_id, t,
4688 t->from ? t->from->proc->pid : 0,
4689 t->from ? t->from->pid : 0,
Todd Kjos5f2f63692017-06-29 12:02:09 -07004690 to_proc ? to_proc->pid : 0,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004691 t->to_thread ? t->to_thread->pid : 0,
4692 t->code, t->flags, t->priority, t->need_reply);
Todd Kjos7a4408c2017-06-29 12:01:57 -07004693 spin_unlock(&t->lock);
4694
Todd Kjos5f2f63692017-06-29 12:02:09 -07004695 if (proc != to_proc) {
4696 /*
4697 * Can only safely deref buffer if we are holding the
4698 * correct proc inner lock for this node
4699 */
4700 seq_puts(m, "\n");
4701 return;
4702 }
4703
4704 if (buffer == NULL) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004705 seq_puts(m, " buffer free\n");
4706 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004707 }
Todd Kjos5f2f63692017-06-29 12:02:09 -07004708 if (buffer->target_node)
4709 seq_printf(m, " node %d", buffer->target_node->debug_id);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004710 seq_printf(m, " size %zd:%zd data %p\n",
Todd Kjos5f2f63692017-06-29 12:02:09 -07004711 buffer->data_size, buffer->offsets_size,
4712 buffer->data);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004713}
4714
Todd Kjos5f2f63692017-06-29 12:02:09 -07004715static void print_binder_work_ilocked(struct seq_file *m,
4716 struct binder_proc *proc,
4717 const char *prefix,
4718 const char *transaction_prefix,
4719 struct binder_work *w)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004720{
4721 struct binder_node *node;
4722 struct binder_transaction *t;
4723
4724 switch (w->type) {
4725 case BINDER_WORK_TRANSACTION:
4726 t = container_of(w, struct binder_transaction, work);
Todd Kjos5f2f63692017-06-29 12:02:09 -07004727 print_binder_transaction_ilocked(
4728 m, proc, transaction_prefix, t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004729 break;
Todd Kjos26549d12017-06-29 12:01:55 -07004730 case BINDER_WORK_RETURN_ERROR: {
4731 struct binder_error *e = container_of(
4732 w, struct binder_error, work);
4733
4734 seq_printf(m, "%stransaction error: %u\n",
4735 prefix, e->cmd);
4736 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004737 case BINDER_WORK_TRANSACTION_COMPLETE:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004738 seq_printf(m, "%stransaction complete\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004739 break;
4740 case BINDER_WORK_NODE:
4741 node = container_of(w, struct binder_node, work);
Arve Hjønnevågda498892014-02-21 14:40:26 -08004742 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
4743 prefix, node->debug_id,
4744 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004745 break;
4746 case BINDER_WORK_DEAD_BINDER:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004747 seq_printf(m, "%shas dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004748 break;
4749 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004750 seq_printf(m, "%shas cleared dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004751 break;
4752 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004753 seq_printf(m, "%shas cleared death notification\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004754 break;
4755 default:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004756 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004757 break;
4758 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004759}
4760
Todd Kjos72196392017-06-29 12:02:02 -07004761static void print_binder_thread_ilocked(struct seq_file *m,
4762 struct binder_thread *thread,
4763 int print_always)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004764{
4765 struct binder_transaction *t;
4766 struct binder_work *w;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004767 size_t start_pos = m->count;
4768 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004769
Todd Kjos72196392017-06-29 12:02:02 -07004770 WARN_ON(!spin_is_locked(&thread->proc->inner_lock));
Todd Kjos7a4408c2017-06-29 12:01:57 -07004771 seq_printf(m, " thread %d: l %02x need_return %d tr %d\n",
Todd Kjos08dabce2017-06-29 12:01:49 -07004772 thread->pid, thread->looper,
Todd Kjos7a4408c2017-06-29 12:01:57 -07004773 thread->looper_need_return,
4774 atomic_read(&thread->tmp_ref));
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004775 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004776 t = thread->transaction_stack;
4777 while (t) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004778 if (t->from == thread) {
Todd Kjos5f2f63692017-06-29 12:02:09 -07004779 print_binder_transaction_ilocked(m, thread->proc,
4780 " outgoing transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004781 t = t->from_parent;
4782 } else if (t->to_thread == thread) {
Todd Kjos5f2f63692017-06-29 12:02:09 -07004783 print_binder_transaction_ilocked(m, thread->proc,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004784 " incoming transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004785 t = t->to_parent;
4786 } else {
Todd Kjos5f2f63692017-06-29 12:02:09 -07004787 print_binder_transaction_ilocked(m, thread->proc,
4788 " bad transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004789 t = NULL;
4790 }
4791 }
4792 list_for_each_entry(w, &thread->todo, entry) {
Todd Kjos5f2f63692017-06-29 12:02:09 -07004793 print_binder_work_ilocked(m, thread->proc, " ",
Todd Kjos72196392017-06-29 12:02:02 -07004794 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004795 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004796 if (!print_always && m->count == header_pos)
4797 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004798}
4799
Todd Kjosda0fa9e2017-06-29 12:02:04 -07004800static void print_binder_node_nilocked(struct seq_file *m,
4801 struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004802{
4803 struct binder_ref *ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004804 struct binder_work *w;
4805 int count;
4806
Todd Kjos673068e2017-06-29 12:02:03 -07004807 WARN_ON(!spin_is_locked(&node->lock));
Todd Kjosda0fa9e2017-06-29 12:02:04 -07004808 if (node->proc)
4809 WARN_ON(!spin_is_locked(&node->proc->inner_lock));
Todd Kjos673068e2017-06-29 12:02:03 -07004810
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004811 count = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004812 hlist_for_each_entry(ref, &node->refs, node_entry)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004813 count++;
4814
Todd Kjosadc18842017-06-29 12:01:59 -07004815 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 -08004816 node->debug_id, (u64)node->ptr, (u64)node->cookie,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004817 node->has_strong_ref, node->has_weak_ref,
4818 node->local_strong_refs, node->local_weak_refs,
Todd Kjosadc18842017-06-29 12:01:59 -07004819 node->internal_strong_refs, count, node->tmp_refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004820 if (count) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004821 seq_puts(m, " proc");
Sasha Levinb67bfe02013-02-27 17:06:00 -08004822 hlist_for_each_entry(ref, &node->refs, node_entry)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004823 seq_printf(m, " %d", ref->proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004824 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004825 seq_puts(m, "\n");
Todd Kjos72196392017-06-29 12:02:02 -07004826 if (node->proc) {
Todd Kjos72196392017-06-29 12:02:02 -07004827 list_for_each_entry(w, &node->async_todo, entry)
Todd Kjos5f2f63692017-06-29 12:02:09 -07004828 print_binder_work_ilocked(m, node->proc, " ",
Todd Kjos72196392017-06-29 12:02:02 -07004829 " pending async transaction", w);
Todd Kjos72196392017-06-29 12:02:02 -07004830 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004831}
4832
Todd Kjos2c1838d2017-06-29 12:02:08 -07004833static void print_binder_ref_olocked(struct seq_file *m,
4834 struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004835{
Todd Kjos2c1838d2017-06-29 12:02:08 -07004836 WARN_ON(!spin_is_locked(&ref->proc->outer_lock));
Todd Kjos673068e2017-06-29 12:02:03 -07004837 binder_node_lock(ref->node);
Todd Kjos372e3142017-06-29 12:01:58 -07004838 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
4839 ref->data.debug_id, ref->data.desc,
4840 ref->node->proc ? "" : "dead ",
4841 ref->node->debug_id, ref->data.strong,
4842 ref->data.weak, ref->death);
Todd Kjos673068e2017-06-29 12:02:03 -07004843 binder_node_unlock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004844}
4845
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004846static void print_binder_proc(struct seq_file *m,
4847 struct binder_proc *proc, int print_all)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004848{
4849 struct binder_work *w;
4850 struct rb_node *n;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004851 size_t start_pos = m->count;
4852 size_t header_pos;
Todd Kjosda0fa9e2017-06-29 12:02:04 -07004853 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004854
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004855 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen14db3182017-02-03 14:40:47 -08004856 seq_printf(m, "context %s\n", proc->context->name);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004857 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004858
Todd Kjos72196392017-06-29 12:02:02 -07004859 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004860 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
Todd Kjos72196392017-06-29 12:02:02 -07004861 print_binder_thread_ilocked(m, rb_entry(n, struct binder_thread,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004862 rb_node), print_all);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07004863
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004864 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004865 struct binder_node *node = rb_entry(n, struct binder_node,
4866 rb_node);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07004867 /*
4868 * take a temporary reference on the node so it
4869 * survives and isn't removed from the tree
4870 * while we print it.
4871 */
4872 binder_inc_node_tmpref_ilocked(node);
4873 /* Need to drop inner lock to take node lock */
4874 binder_inner_proc_unlock(proc);
4875 if (last_node)
4876 binder_put_node(last_node);
4877 binder_node_inner_lock(node);
4878 print_binder_node_nilocked(m, node);
4879 binder_node_inner_unlock(node);
4880 last_node = node;
4881 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004882 }
Todd Kjosda0fa9e2017-06-29 12:02:04 -07004883 binder_inner_proc_unlock(proc);
4884 if (last_node)
4885 binder_put_node(last_node);
4886
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004887 if (print_all) {
Todd Kjos2c1838d2017-06-29 12:02:08 -07004888 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004889 for (n = rb_first(&proc->refs_by_desc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004890 n != NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004891 n = rb_next(n))
Todd Kjos2c1838d2017-06-29 12:02:08 -07004892 print_binder_ref_olocked(m, rb_entry(n,
4893 struct binder_ref,
4894 rb_node_desc));
4895 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004896 }
Todd Kjos19c98722017-06-29 12:01:40 -07004897 binder_alloc_print_allocated(m, &proc->alloc);
Todd Kjos72196392017-06-29 12:02:02 -07004898 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004899 list_for_each_entry(w, &proc->todo, entry)
Todd Kjos5f2f63692017-06-29 12:02:09 -07004900 print_binder_work_ilocked(m, proc, " ",
4901 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004902 list_for_each_entry(w, &proc->delivered_death, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004903 seq_puts(m, " has delivered dead binder\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004904 break;
4905 }
Todd Kjos72196392017-06-29 12:02:02 -07004906 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004907 if (!print_all && m->count == header_pos)
4908 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004909}
4910
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10004911static const char * const binder_return_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004912 "BR_ERROR",
4913 "BR_OK",
4914 "BR_TRANSACTION",
4915 "BR_REPLY",
4916 "BR_ACQUIRE_RESULT",
4917 "BR_DEAD_REPLY",
4918 "BR_TRANSACTION_COMPLETE",
4919 "BR_INCREFS",
4920 "BR_ACQUIRE",
4921 "BR_RELEASE",
4922 "BR_DECREFS",
4923 "BR_ATTEMPT_ACQUIRE",
4924 "BR_NOOP",
4925 "BR_SPAWN_LOOPER",
4926 "BR_FINISHED",
4927 "BR_DEAD_BINDER",
4928 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
4929 "BR_FAILED_REPLY"
4930};
4931
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10004932static const char * const binder_command_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004933 "BC_TRANSACTION",
4934 "BC_REPLY",
4935 "BC_ACQUIRE_RESULT",
4936 "BC_FREE_BUFFER",
4937 "BC_INCREFS",
4938 "BC_ACQUIRE",
4939 "BC_RELEASE",
4940 "BC_DECREFS",
4941 "BC_INCREFS_DONE",
4942 "BC_ACQUIRE_DONE",
4943 "BC_ATTEMPT_ACQUIRE",
4944 "BC_REGISTER_LOOPER",
4945 "BC_ENTER_LOOPER",
4946 "BC_EXIT_LOOPER",
4947 "BC_REQUEST_DEATH_NOTIFICATION",
4948 "BC_CLEAR_DEATH_NOTIFICATION",
Martijn Coenen79802402017-02-03 14:40:51 -08004949 "BC_DEAD_BINDER_DONE",
4950 "BC_TRANSACTION_SG",
4951 "BC_REPLY_SG",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004952};
4953
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10004954static const char * const binder_objstat_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004955 "proc",
4956 "thread",
4957 "node",
4958 "ref",
4959 "death",
4960 "transaction",
4961 "transaction_complete"
4962};
4963
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004964static void print_binder_stats(struct seq_file *m, const char *prefix,
4965 struct binder_stats *stats)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004966{
4967 int i;
4968
4969 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004970 ARRAY_SIZE(binder_command_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004971 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07004972 int temp = atomic_read(&stats->bc[i]);
4973
4974 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004975 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07004976 binder_command_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004977 }
4978
4979 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004980 ARRAY_SIZE(binder_return_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004981 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07004982 int temp = atomic_read(&stats->br[i]);
4983
4984 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004985 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07004986 binder_return_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004987 }
4988
4989 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004990 ARRAY_SIZE(binder_objstat_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004991 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004992 ARRAY_SIZE(stats->obj_deleted));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004993 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07004994 int created = atomic_read(&stats->obj_created[i]);
4995 int deleted = atomic_read(&stats->obj_deleted[i]);
4996
4997 if (created || deleted)
4998 seq_printf(m, "%s%s: active %d total %d\n",
4999 prefix,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005000 binder_objstat_strings[i],
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07005001 created - deleted,
5002 created);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005003 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005004}
5005
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005006static void print_binder_proc_stats(struct seq_file *m,
5007 struct binder_proc *proc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005008{
5009 struct binder_work *w;
5010 struct rb_node *n;
5011 int count, strong, weak;
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07005012 size_t free_async_space =
5013 binder_alloc_get_free_async_space(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005014
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005015 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen14db3182017-02-03 14:40:47 -08005016 seq_printf(m, "context %s\n", proc->context->name);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005017 count = 0;
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07005018 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005019 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
5020 count++;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005021 seq_printf(m, " threads: %d\n", count);
5022 seq_printf(m, " requested threads: %d+%d/%d\n"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005023 " ready threads %d\n"
5024 " free async space %zd\n", proc->requested_threads,
5025 proc->requested_threads_started, proc->max_threads,
Todd Kjos19c98722017-06-29 12:01:40 -07005026 proc->ready_threads,
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07005027 free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005028 count = 0;
5029 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
5030 count++;
Todd Kjosda0fa9e2017-06-29 12:02:04 -07005031 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005032 seq_printf(m, " nodes: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005033 count = 0;
5034 strong = 0;
5035 weak = 0;
Todd Kjos2c1838d2017-06-29 12:02:08 -07005036 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005037 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
5038 struct binder_ref *ref = rb_entry(n, struct binder_ref,
5039 rb_node_desc);
5040 count++;
Todd Kjos372e3142017-06-29 12:01:58 -07005041 strong += ref->data.strong;
5042 weak += ref->data.weak;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005043 }
Todd Kjos2c1838d2017-06-29 12:02:08 -07005044 binder_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005045 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005046
Todd Kjos19c98722017-06-29 12:01:40 -07005047 count = binder_alloc_get_allocated_count(&proc->alloc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005048 seq_printf(m, " buffers: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005049
Sherry Yang8ef46652017-08-31 11:56:36 -07005050 binder_alloc_print_pages(m, &proc->alloc);
5051
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005052 count = 0;
Todd Kjos72196392017-06-29 12:02:02 -07005053 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005054 list_for_each_entry(w, &proc->todo, entry) {
Todd Kjos72196392017-06-29 12:02:02 -07005055 if (w->type == BINDER_WORK_TRANSACTION)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005056 count++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005057 }
Todd Kjos72196392017-06-29 12:02:02 -07005058 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005059 seq_printf(m, " pending transactions: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005060
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005061 print_binder_stats(m, " ", &proc->stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005062}
5063
5064
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005065static int binder_state_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005066{
5067 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005068 struct binder_node *node;
Todd Kjos673068e2017-06-29 12:02:03 -07005069 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005070
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005071 seq_puts(m, "binder state:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005072
Todd Kjosc44b1232017-06-29 12:01:43 -07005073 spin_lock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005074 if (!hlist_empty(&binder_dead_nodes))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005075 seq_puts(m, "dead nodes:\n");
Todd Kjos673068e2017-06-29 12:02:03 -07005076 hlist_for_each_entry(node, &binder_dead_nodes, dead_node) {
5077 /*
5078 * take a temporary reference on the node so it
5079 * survives and isn't removed from the list
5080 * while we print it.
5081 */
5082 node->tmp_refs++;
5083 spin_unlock(&binder_dead_nodes_lock);
5084 if (last_node)
5085 binder_put_node(last_node);
5086 binder_node_lock(node);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07005087 print_binder_node_nilocked(m, node);
Todd Kjos673068e2017-06-29 12:02:03 -07005088 binder_node_unlock(node);
5089 last_node = node;
5090 spin_lock(&binder_dead_nodes_lock);
5091 }
Todd Kjosc44b1232017-06-29 12:01:43 -07005092 spin_unlock(&binder_dead_nodes_lock);
Todd Kjos673068e2017-06-29 12:02:03 -07005093 if (last_node)
5094 binder_put_node(last_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005095
Todd Kjosc44b1232017-06-29 12:01:43 -07005096 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005097 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005098 print_binder_proc(m, proc, 1);
Todd Kjosc44b1232017-06-29 12:01:43 -07005099 mutex_unlock(&binder_procs_lock);
Todd Kjosa60b8902017-06-29 12:02:11 -07005100
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005101 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005102}
5103
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005104static int binder_stats_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005105{
5106 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005107
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005108 seq_puts(m, "binder stats:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005109
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005110 print_binder_stats(m, "", &binder_stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005111
Todd Kjosc44b1232017-06-29 12:01:43 -07005112 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005113 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005114 print_binder_proc_stats(m, proc);
Todd Kjosc44b1232017-06-29 12:01:43 -07005115 mutex_unlock(&binder_procs_lock);
Todd Kjosa60b8902017-06-29 12:02:11 -07005116
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005117 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005118}
5119
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005120static int binder_transactions_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005121{
5122 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005123
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005124 seq_puts(m, "binder transactions:\n");
Todd Kjosc44b1232017-06-29 12:01:43 -07005125 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005126 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005127 print_binder_proc(m, proc, 0);
Todd Kjosc44b1232017-06-29 12:01:43 -07005128 mutex_unlock(&binder_procs_lock);
Todd Kjosa60b8902017-06-29 12:02:11 -07005129
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005130 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005131}
5132
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005133static int binder_proc_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005134{
Riley Andrews83050a42016-02-09 21:05:33 -08005135 struct binder_proc *itr;
Martijn Coenen14db3182017-02-03 14:40:47 -08005136 int pid = (unsigned long)m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005137
Todd Kjosc44b1232017-06-29 12:01:43 -07005138 mutex_lock(&binder_procs_lock);
Riley Andrews83050a42016-02-09 21:05:33 -08005139 hlist_for_each_entry(itr, &binder_procs, proc_node) {
Martijn Coenen14db3182017-02-03 14:40:47 -08005140 if (itr->pid == pid) {
5141 seq_puts(m, "binder proc state:\n");
5142 print_binder_proc(m, itr, 1);
Riley Andrews83050a42016-02-09 21:05:33 -08005143 }
5144 }
Todd Kjosc44b1232017-06-29 12:01:43 -07005145 mutex_unlock(&binder_procs_lock);
5146
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005147 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005148}
5149
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005150static void print_binder_transaction_log_entry(struct seq_file *m,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005151 struct binder_transaction_log_entry *e)
5152{
Todd Kjosd99c7332017-06-29 12:01:53 -07005153 int debug_id = READ_ONCE(e->debug_id_done);
5154 /*
5155 * read barrier to guarantee debug_id_done read before
5156 * we print the log values
5157 */
5158 smp_rmb();
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005159 seq_printf(m,
Todd Kjosd99c7332017-06-29 12:01:53 -07005160 "%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 -07005161 e->debug_id, (e->call_type == 2) ? "reply" :
5162 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
Martijn Coenen14db3182017-02-03 14:40:47 -08005163 e->from_thread, e->to_proc, e->to_thread, e->context_name,
Todd Kjos57ada2f2017-06-29 12:01:46 -07005164 e->to_node, e->target_handle, e->data_size, e->offsets_size,
5165 e->return_error, e->return_error_param,
5166 e->return_error_line);
Todd Kjosd99c7332017-06-29 12:01:53 -07005167 /*
5168 * read-barrier to guarantee read of debug_id_done after
5169 * done printing the fields of the entry
5170 */
5171 smp_rmb();
5172 seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ?
5173 "\n" : " (incomplete)\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005174}
5175
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005176static int binder_transaction_log_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005177{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005178 struct binder_transaction_log *log = m->private;
Todd Kjosd99c7332017-06-29 12:01:53 -07005179 unsigned int log_cur = atomic_read(&log->cur);
5180 unsigned int count;
5181 unsigned int cur;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005182 int i;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005183
Todd Kjosd99c7332017-06-29 12:01:53 -07005184 count = log_cur + 1;
5185 cur = count < ARRAY_SIZE(log->entry) && !log->full ?
5186 0 : count % ARRAY_SIZE(log->entry);
5187 if (count > ARRAY_SIZE(log->entry) || log->full)
5188 count = ARRAY_SIZE(log->entry);
5189 for (i = 0; i < count; i++) {
5190 unsigned int index = cur++ % ARRAY_SIZE(log->entry);
5191
5192 print_binder_transaction_log_entry(m, &log->entry[index]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005193 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005194 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005195}
5196
5197static const struct file_operations binder_fops = {
5198 .owner = THIS_MODULE,
5199 .poll = binder_poll,
5200 .unlocked_ioctl = binder_ioctl,
Arve Hjønnevågda498892014-02-21 14:40:26 -08005201 .compat_ioctl = binder_ioctl,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005202 .mmap = binder_mmap,
5203 .open = binder_open,
5204 .flush = binder_flush,
5205 .release = binder_release,
5206};
5207
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005208BINDER_DEBUG_ENTRY(state);
5209BINDER_DEBUG_ENTRY(stats);
5210BINDER_DEBUG_ENTRY(transactions);
5211BINDER_DEBUG_ENTRY(transaction_log);
5212
Martijn Coenenac4812c2017-02-03 14:40:48 -08005213static int __init init_binder_device(const char *name)
5214{
5215 int ret;
5216 struct binder_device *binder_device;
5217
5218 binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
5219 if (!binder_device)
5220 return -ENOMEM;
5221
5222 binder_device->miscdev.fops = &binder_fops;
5223 binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
5224 binder_device->miscdev.name = name;
5225
5226 binder_device->context.binder_context_mgr_uid = INVALID_UID;
5227 binder_device->context.name = name;
Todd Kjosc44b1232017-06-29 12:01:43 -07005228 mutex_init(&binder_device->context.context_mgr_node_lock);
Martijn Coenenac4812c2017-02-03 14:40:48 -08005229
5230 ret = misc_register(&binder_device->miscdev);
5231 if (ret < 0) {
5232 kfree(binder_device);
5233 return ret;
5234 }
5235
5236 hlist_add_head(&binder_device->hlist, &binder_devices);
5237
5238 return ret;
5239}
5240
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005241static int __init binder_init(void)
5242{
5243 int ret;
Christian Brauner22eb9472017-08-21 16:13:28 +02005244 char *device_name, *device_names, *device_tmp;
Martijn Coenenac4812c2017-02-03 14:40:48 -08005245 struct binder_device *device;
5246 struct hlist_node *tmp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005247
Sherry Yangf2517eb2017-08-23 08:46:42 -07005248 binder_alloc_shrinker_init();
5249
Todd Kjosd99c7332017-06-29 12:01:53 -07005250 atomic_set(&binder_transaction_log.cur, ~0U);
5251 atomic_set(&binder_transaction_log_failed.cur, ~0U);
5252
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005253 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
5254 if (binder_debugfs_dir_entry_root)
5255 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
5256 binder_debugfs_dir_entry_root);
Martijn Coenenac4812c2017-02-03 14:40:48 -08005257
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005258 if (binder_debugfs_dir_entry_root) {
5259 debugfs_create_file("state",
5260 S_IRUGO,
5261 binder_debugfs_dir_entry_root,
5262 NULL,
5263 &binder_state_fops);
5264 debugfs_create_file("stats",
5265 S_IRUGO,
5266 binder_debugfs_dir_entry_root,
5267 NULL,
5268 &binder_stats_fops);
5269 debugfs_create_file("transactions",
5270 S_IRUGO,
5271 binder_debugfs_dir_entry_root,
5272 NULL,
5273 &binder_transactions_fops);
5274 debugfs_create_file("transaction_log",
5275 S_IRUGO,
5276 binder_debugfs_dir_entry_root,
5277 &binder_transaction_log,
5278 &binder_transaction_log_fops);
5279 debugfs_create_file("failed_transaction_log",
5280 S_IRUGO,
5281 binder_debugfs_dir_entry_root,
5282 &binder_transaction_log_failed,
5283 &binder_transaction_log_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005284 }
Martijn Coenenac4812c2017-02-03 14:40:48 -08005285
5286 /*
5287 * Copy the module_parameter string, because we don't want to
5288 * tokenize it in-place.
5289 */
5290 device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL);
5291 if (!device_names) {
5292 ret = -ENOMEM;
5293 goto err_alloc_device_names_failed;
5294 }
5295 strcpy(device_names, binder_devices_param);
5296
Christian Brauner22eb9472017-08-21 16:13:28 +02005297 device_tmp = device_names;
5298 while ((device_name = strsep(&device_tmp, ","))) {
Martijn Coenenac4812c2017-02-03 14:40:48 -08005299 ret = init_binder_device(device_name);
5300 if (ret)
5301 goto err_init_binder_device_failed;
5302 }
5303
5304 return ret;
5305
5306err_init_binder_device_failed:
5307 hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
5308 misc_deregister(&device->miscdev);
5309 hlist_del(&device->hlist);
5310 kfree(device);
5311 }
Christian Brauner22eb9472017-08-21 16:13:28 +02005312
5313 kfree(device_names);
5314
Martijn Coenenac4812c2017-02-03 14:40:48 -08005315err_alloc_device_names_failed:
5316 debugfs_remove_recursive(binder_debugfs_dir_entry_root);
5317
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005318 return ret;
5319}
5320
5321device_initcall(binder_init);
5322
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07005323#define CREATE_TRACE_POINTS
5324#include "binder_trace.h"
5325
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005326MODULE_LICENSE("GPL v2");