blob: e7e4560e4c6e4f0a0721f192f461fbb535e4f45c [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 Kjosfc7a7e22017-05-29 16:44:24 -070018/*
19 * Locking overview
20 *
21 * There are 3 main spinlocks which must be acquired in the
22 * order shown:
23 *
24 * 1) proc->outer_lock : protects binder_ref
25 * binder_proc_lock() and binder_proc_unlock() are
26 * used to acq/rel.
27 * 2) node->lock : protects most fields of binder_node.
28 * binder_node_lock() and binder_node_unlock() are
29 * used to acq/rel
30 * 3) proc->inner_lock : protects the thread and node lists
Martijn Coenen22d64e4322017-06-02 11:15:44 -070031 * (proc->threads, proc->waiting_threads, proc->nodes)
32 * and all todo lists associated with the binder_proc
33 * (proc->todo, thread->todo, proc->delivered_death and
34 * node->async_todo), as well as thread->transaction_stack
Todd Kjosfc7a7e22017-05-29 16:44:24 -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>
67#include <linux/sched.h>
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070068#include <linux/seq_file.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090069#include <linux/uaccess.h>
Eric W. Biederman17cf22c2010-03-02 14:51:53 -080070#include <linux/pid_namespace.h>
Stephen Smalley79af7302015-01-21 10:54:10 -050071#include <linux/security.h>
Todd Kjosfc7a7e22017-05-29 16:44:24 -070072#include <linux/spinlock.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090073
Greg Kroah-Hartman9246a4a2014-10-16 15:26:51 +020074#ifdef CONFIG_ANDROID_BINDER_IPC_32BIT
75#define BINDER_IPC_32BIT 1
76#endif
77
78#include <uapi/linux/android/binder.h>
Todd Kjosb9341022016-10-10 10:40:53 -070079#include "binder_alloc.h"
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -070080#include "binder_trace.h"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090081
Todd Kjos8d9f6f32016-10-17 12:33:15 -070082static HLIST_HEAD(binder_deferred_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090083static DEFINE_MUTEX(binder_deferred_lock);
84
Martijn Coenen6b7c7122016-09-30 16:08:09 +020085static HLIST_HEAD(binder_devices);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090086static HLIST_HEAD(binder_procs);
Todd Kjos8d9f6f32016-10-17 12:33:15 -070087static DEFINE_MUTEX(binder_procs_lock);
88
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090089static HLIST_HEAD(binder_dead_nodes);
Todd Kjos8d9f6f32016-10-17 12:33:15 -070090static DEFINE_SPINLOCK(binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090091
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070092static struct dentry *binder_debugfs_dir_entry_root;
93static struct dentry *binder_debugfs_dir_entry_proc;
Todd Kjosc4bd08b2017-05-25 10:56:00 -070094static atomic_t binder_last_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090095
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070096#define BINDER_DEBUG_ENTRY(name) \
97static int binder_##name##_open(struct inode *inode, struct file *file) \
98{ \
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070099 return single_open(file, binder_##name##_show, inode->i_private); \
Arve Hjønnevåg5249f482009-04-28 20:57:50 -0700100} \
101\
102static const struct file_operations binder_##name##_fops = { \
103 .owner = THIS_MODULE, \
104 .open = binder_##name##_open, \
105 .read = seq_read, \
106 .llseek = seq_lseek, \
107 .release = single_release, \
108}
109
110static int binder_proc_show(struct seq_file *m, void *unused);
111BINDER_DEBUG_ENTRY(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900112
113/* This is only defined in include/asm-arm/sizes.h */
114#ifndef SZ_1K
115#define SZ_1K 0x400
116#endif
117
118#ifndef SZ_4M
119#define SZ_4M 0x400000
120#endif
121
122#define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
123
124#define BINDER_SMALL_BUF_SIZE (PAGE_SIZE * 64)
125
126enum {
127 BINDER_DEBUG_USER_ERROR = 1U << 0,
128 BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1,
129 BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2,
130 BINDER_DEBUG_OPEN_CLOSE = 1U << 3,
131 BINDER_DEBUG_DEAD_BINDER = 1U << 4,
132 BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5,
133 BINDER_DEBUG_READ_WRITE = 1U << 6,
134 BINDER_DEBUG_USER_REFS = 1U << 7,
135 BINDER_DEBUG_THREADS = 1U << 8,
136 BINDER_DEBUG_TRANSACTION = 1U << 9,
137 BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10,
138 BINDER_DEBUG_FREE_BUFFER = 1U << 11,
139 BINDER_DEBUG_INTERNAL_REFS = 1U << 12,
Todd Kjosd325d372016-10-10 10:40:53 -0700140 BINDER_DEBUG_PRIORITY_CAP = 1U << 13,
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700141 BINDER_DEBUG_SPINLOCKS = 1U << 14,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900142};
143static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR |
144 BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION;
145module_param_named(debug_mask, binder_debug_mask, uint, S_IWUSR | S_IRUGO);
146
Martijn Coenen6b7c7122016-09-30 16:08:09 +0200147static char *binder_devices_param = CONFIG_ANDROID_BINDER_DEVICES;
148module_param_named(devices, binder_devices_param, charp, S_IRUGO);
149
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900150static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
151static int binder_stop_on_user_error;
152
153static int binder_set_stop_on_user_error(const char *val,
Kees Cook24da2c82017-10-17 19:04:42 -0700154 const struct kernel_param *kp)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900155{
156 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +0900157
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900158 ret = param_set_int(val, kp);
159 if (binder_stop_on_user_error < 2)
160 wake_up(&binder_user_error_wait);
161 return ret;
162}
163module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
164 param_get_int, &binder_stop_on_user_error, S_IWUSR | S_IRUGO);
165
166#define binder_debug(mask, x...) \
167 do { \
168 if (binder_debug_mask & mask) \
Sherwin Soltani258767f2012-06-26 02:00:30 -0400169 pr_info(x); \
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900170 } while (0)
171
172#define binder_user_error(x...) \
173 do { \
174 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
Sherwin Soltani258767f2012-06-26 02:00:30 -0400175 pr_info(x); \
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900176 if (binder_stop_on_user_error) \
177 binder_stop_on_user_error = 2; \
178 } while (0)
179
Martijn Coenen00c80372016-07-13 12:06:49 +0200180#define to_flat_binder_object(hdr) \
181 container_of(hdr, struct flat_binder_object, hdr)
182
183#define to_binder_fd_object(hdr) container_of(hdr, struct binder_fd_object, hdr)
184
Martijn Coenen5a6da532016-09-30 14:10:07 +0200185#define to_binder_buffer_object(hdr) \
186 container_of(hdr, struct binder_buffer_object, hdr)
187
Martijn Coenene3e0f4802016-10-18 13:58:55 +0200188#define to_binder_fd_array_object(hdr) \
189 container_of(hdr, struct binder_fd_array_object, hdr)
190
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900191enum binder_stat_types {
192 BINDER_STAT_PROC,
193 BINDER_STAT_THREAD,
194 BINDER_STAT_NODE,
195 BINDER_STAT_REF,
196 BINDER_STAT_DEATH,
197 BINDER_STAT_TRANSACTION,
198 BINDER_STAT_TRANSACTION_COMPLETE,
199 BINDER_STAT_COUNT
200};
201
202struct binder_stats {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -0700203 atomic_t br[_IOC_NR(BR_FAILED_REPLY) + 1];
204 atomic_t bc[_IOC_NR(BC_REPLY_SG) + 1];
205 atomic_t obj_created[BINDER_STAT_COUNT];
206 atomic_t obj_deleted[BINDER_STAT_COUNT];
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900207};
208
209static struct binder_stats binder_stats;
210
211static inline void binder_stats_deleted(enum binder_stat_types type)
212{
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -0700213 atomic_inc(&binder_stats.obj_deleted[type]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900214}
215
216static inline void binder_stats_created(enum binder_stat_types type)
217{
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -0700218 atomic_inc(&binder_stats.obj_created[type]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900219}
220
221struct binder_transaction_log_entry {
222 int debug_id;
Todd Kjos1cfe6272017-05-24 13:33:28 -0700223 int debug_id_done;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900224 int call_type;
225 int from_proc;
226 int from_thread;
227 int target_handle;
228 int to_proc;
229 int to_thread;
230 int to_node;
231 int data_size;
232 int offsets_size;
Todd Kjose598d172017-03-22 17:19:52 -0700233 int return_error_line;
234 uint32_t return_error;
235 uint32_t return_error_param;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +0200236 const char *context_name;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900237};
238struct binder_transaction_log {
Todd Kjos1cfe6272017-05-24 13:33:28 -0700239 atomic_t cur;
240 bool full;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900241 struct binder_transaction_log_entry entry[32];
242};
243static struct binder_transaction_log binder_transaction_log;
244static struct binder_transaction_log binder_transaction_log_failed;
245
246static struct binder_transaction_log_entry *binder_transaction_log_add(
247 struct binder_transaction_log *log)
248{
249 struct binder_transaction_log_entry *e;
Todd Kjos1cfe6272017-05-24 13:33:28 -0700250 unsigned int cur = atomic_inc_return(&log->cur);
Seunghun Lee10f62862014-05-01 01:30:23 +0900251
Todd Kjos1cfe6272017-05-24 13:33:28 -0700252 if (cur >= ARRAY_SIZE(log->entry))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900253 log->full = 1;
Todd Kjos1cfe6272017-05-24 13:33:28 -0700254 e = &log->entry[cur % ARRAY_SIZE(log->entry)];
255 WRITE_ONCE(e->debug_id_done, 0);
256 /*
257 * write-barrier to synchronize access to e->debug_id_done.
258 * We make sure the initialized 0 value is seen before
259 * memset() other fields are zeroed by memset.
260 */
261 smp_wmb();
262 memset(e, 0, sizeof(*e));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900263 return e;
264}
265
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200266struct binder_context {
267 struct binder_node *binder_context_mgr_node;
Todd Kjos8d9f6f32016-10-17 12:33:15 -0700268 struct mutex context_mgr_node_lock;
269
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200270 kuid_t binder_context_mgr_uid;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +0200271 const char *name;
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200272};
273
Martijn Coenen6b7c7122016-09-30 16:08:09 +0200274struct binder_device {
275 struct hlist_node hlist;
276 struct miscdevice miscdev;
277 struct binder_context context;
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200278};
279
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700280/**
281 * struct binder_work - work enqueued on a worklist
282 * @entry: node enqueued on list
283 * @type: type of work to be performed
284 *
285 * There are separate work lists for proc, thread, and node (async).
286 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900287struct binder_work {
288 struct list_head entry;
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700289
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900290 enum {
291 BINDER_WORK_TRANSACTION = 1,
292 BINDER_WORK_TRANSACTION_COMPLETE,
Todd Kjos858b8da2017-04-21 17:35:12 -0700293 BINDER_WORK_RETURN_ERROR,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900294 BINDER_WORK_NODE,
295 BINDER_WORK_DEAD_BINDER,
296 BINDER_WORK_DEAD_BINDER_AND_CLEAR,
297 BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
298 } type;
299};
300
Todd Kjos858b8da2017-04-21 17:35:12 -0700301struct binder_error {
302 struct binder_work work;
303 uint32_t cmd;
304};
305
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700306/**
307 * struct binder_node - binder node bookkeeping
308 * @debug_id: unique ID for debugging
309 * (invariant after initialized)
310 * @lock: lock for node fields
311 * @work: worklist element for node work
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700312 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700313 * @rb_node: element for proc->nodes tree
Todd Kjos425d23f2017-06-12 12:07:26 -0700314 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700315 * @dead_node: element for binder_dead_nodes list
316 * (protected by binder_dead_nodes_lock)
317 * @proc: binder_proc that owns this node
318 * (invariant after initialized)
319 * @refs: list of references on this node
Todd Kjoscbcbbd62017-06-08 13:45:59 -0700320 * (protected by @lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700321 * @internal_strong_refs: used to take strong references when
322 * initiating a transaction
Todd Kjose7f23ed2017-03-21 13:06:01 -0700323 * (protected by @proc->inner_lock if @proc
324 * and by @lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700325 * @local_weak_refs: weak user refs from local process
Todd Kjose7f23ed2017-03-21 13:06:01 -0700326 * (protected by @proc->inner_lock if @proc
327 * and by @lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700328 * @local_strong_refs: strong user refs from local process
Todd Kjose7f23ed2017-03-21 13:06:01 -0700329 * (protected by @proc->inner_lock if @proc
330 * and by @lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700331 * @tmp_refs: temporary kernel refs
Todd Kjose7f23ed2017-03-21 13:06:01 -0700332 * (protected by @proc->inner_lock while @proc
333 * is valid, and by binder_dead_nodes_lock
334 * if @proc is NULL. During inc/dec and node release
335 * it is also protected by @lock to provide safety
336 * as the node dies and @proc becomes NULL)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700337 * @ptr: userspace pointer for node
338 * (invariant, no lock needed)
339 * @cookie: userspace cookie for node
340 * (invariant, no lock needed)
341 * @has_strong_ref: userspace notified of strong ref
Todd Kjose7f23ed2017-03-21 13:06:01 -0700342 * (protected by @proc->inner_lock if @proc
343 * and by @lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700344 * @pending_strong_ref: userspace has acked notification of strong ref
Todd Kjose7f23ed2017-03-21 13:06:01 -0700345 * (protected by @proc->inner_lock if @proc
346 * and by @lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700347 * @has_weak_ref: userspace notified of weak ref
Todd Kjose7f23ed2017-03-21 13:06:01 -0700348 * (protected by @proc->inner_lock if @proc
349 * and by @lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700350 * @pending_weak_ref: userspace has acked notification of weak ref
Todd Kjose7f23ed2017-03-21 13:06:01 -0700351 * (protected by @proc->inner_lock if @proc
352 * and by @lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700353 * @has_async_transaction: async transaction to node in progress
Todd Kjoscbcbbd62017-06-08 13:45:59 -0700354 * (protected by @lock)
Martijn Coenen6aac9792017-06-07 09:29:14 -0700355 * @sched_policy: minimum scheduling policy for node
356 * (invariant after initialized)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700357 * @accept_fds: file descriptor operations supported for node
358 * (invariant after initialized)
359 * @min_priority: minimum scheduling priority
360 * (invariant after initialized)
Martijn Coenenc46810c2017-06-23 10:13:43 -0700361 * @inherit_rt: inherit RT scheduling policy from caller
362 * (invariant after initialized)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700363 * @async_todo: list of async work items
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700364 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700365 *
366 * Bookkeeping structure for binder nodes.
367 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900368struct binder_node {
369 int debug_id;
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700370 spinlock_t lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900371 struct binder_work work;
372 union {
373 struct rb_node rb_node;
374 struct hlist_node dead_node;
375 };
376 struct binder_proc *proc;
377 struct hlist_head refs;
378 int internal_strong_refs;
379 int local_weak_refs;
380 int local_strong_refs;
Todd Kjosf22abc72017-05-09 11:08:05 -0700381 int tmp_refs;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800382 binder_uintptr_t ptr;
383 binder_uintptr_t cookie;
Todd Kjose7f23ed2017-03-21 13:06:01 -0700384 struct {
385 /*
386 * bitfield elements protected by
387 * proc inner_lock
388 */
389 u8 has_strong_ref:1;
390 u8 pending_strong_ref:1;
391 u8 has_weak_ref:1;
392 u8 pending_weak_ref:1;
393 };
394 struct {
395 /*
396 * invariant after initialization
397 */
Martijn Coenen6aac9792017-06-07 09:29:14 -0700398 u8 sched_policy:2;
Martijn Coenenc46810c2017-06-23 10:13:43 -0700399 u8 inherit_rt:1;
Todd Kjose7f23ed2017-03-21 13:06:01 -0700400 u8 accept_fds:1;
401 u8 min_priority;
402 };
403 bool has_async_transaction;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900404 struct list_head async_todo;
405};
406
407struct binder_ref_death {
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700408 /**
409 * @work: worklist element for death notifications
410 * (protected by inner_lock of the proc that
411 * this ref belongs to)
412 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900413 struct binder_work work;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800414 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900415};
416
Todd Kjosb0117bb2017-05-08 09:16:27 -0700417/**
418 * struct binder_ref_data - binder_ref counts and id
419 * @debug_id: unique ID for the ref
420 * @desc: unique userspace handle for ref
421 * @strong: strong ref count (debugging only if not locked)
422 * @weak: weak ref count (debugging only if not locked)
423 *
424 * Structure to hold ref count and ref id information. Since
425 * the actual ref can only be accessed with a lock, this structure
426 * is used to return information about the ref to callers of
427 * ref inc/dec functions.
428 */
429struct binder_ref_data {
430 int debug_id;
431 uint32_t desc;
432 int strong;
433 int weak;
434};
435
436/**
437 * struct binder_ref - struct to track references on nodes
438 * @data: binder_ref_data containing id, handle, and current refcounts
439 * @rb_node_desc: node for lookup by @data.desc in proc's rb_tree
440 * @rb_node_node: node for lookup by @node in proc's rb_tree
441 * @node_entry: list entry for node->refs list in target node
Todd Kjoscbcbbd62017-06-08 13:45:59 -0700442 * (protected by @node->lock)
Todd Kjosb0117bb2017-05-08 09:16:27 -0700443 * @proc: binder_proc containing ref
444 * @node: binder_node of target node. When cleaning up a
445 * ref for deletion in binder_cleanup_ref, a non-NULL
446 * @node indicates the node must be freed
447 * @death: pointer to death notification (ref_death) if requested
Martijn Coenenf9eac642017-05-22 11:26:23 -0700448 * (protected by @node->lock)
Todd Kjosb0117bb2017-05-08 09:16:27 -0700449 *
450 * Structure to track references from procA to target node (on procB). This
451 * structure is unsafe to access without holding @proc->outer_lock.
452 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900453struct binder_ref {
454 /* Lookups needed: */
455 /* node + proc => ref (transaction) */
456 /* desc + proc => ref (transaction, inc/dec ref) */
457 /* node => refs + procs (proc exit) */
Todd Kjosb0117bb2017-05-08 09:16:27 -0700458 struct binder_ref_data data;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900459 struct rb_node rb_node_desc;
460 struct rb_node rb_node_node;
461 struct hlist_node node_entry;
462 struct binder_proc *proc;
463 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900464 struct binder_ref_death *death;
465};
466
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900467enum binder_deferred_state {
Todd Kjosf09daf12017-11-10 15:30:27 -0800468 BINDER_DEFERRED_FLUSH = 0x01,
469 BINDER_DEFERRED_RELEASE = 0x02,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900470};
471
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700472/**
Martijn Coenen57b2ac62017-06-06 17:04:42 -0700473 * struct binder_priority - scheduler policy and priority
474 * @sched_policy scheduler policy
475 * @prio [100..139] for SCHED_NORMAL, [0..99] for FIFO/RT
476 *
477 * The binder driver supports inheriting the following scheduler policies:
478 * SCHED_NORMAL
479 * SCHED_BATCH
480 * SCHED_FIFO
481 * SCHED_RR
482 */
483struct binder_priority {
484 unsigned int sched_policy;
485 int prio;
486};
487
488/**
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700489 * struct binder_proc - binder process bookkeeping
490 * @proc_node: element for binder_procs list
491 * @threads: rbtree of binder_threads in this proc
Todd Kjosb4827902017-05-25 15:52:17 -0700492 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700493 * @nodes: rbtree of binder nodes associated with
494 * this proc ordered by node->ptr
Todd Kjos425d23f2017-06-12 12:07:26 -0700495 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700496 * @refs_by_desc: rbtree of refs ordered by ref->desc
Todd Kjos5346bf32016-10-20 16:43:34 -0700497 * (protected by @outer_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700498 * @refs_by_node: rbtree of refs ordered by ref->node
Todd Kjos5346bf32016-10-20 16:43:34 -0700499 * (protected by @outer_lock)
Martijn Coenen22d64e4322017-06-02 11:15:44 -0700500 * @waiting_threads: threads currently waiting for proc work
501 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700502 * @pid PID of group_leader of process
503 * (invariant after initialized)
504 * @tsk task_struct for group_leader of process
505 * (invariant after initialized)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700506 * @deferred_work_node: element for binder_deferred_list
507 * (protected by binder_deferred_lock)
508 * @deferred_work: bitmap of deferred work to perform
509 * (protected by binder_deferred_lock)
510 * @is_dead: process is dead and awaiting free
511 * when outstanding transactions are cleaned up
Todd Kjosb4827902017-05-25 15:52:17 -0700512 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700513 * @todo: list of work for this process
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700514 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700515 * @stats: per-process binder statistics
516 * (atomics, no lock needed)
517 * @delivered_death: list of delivered death notification
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700518 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700519 * @max_threads: cap on number of binder threads
Todd Kjosd600e902017-05-25 17:35:02 -0700520 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700521 * @requested_threads: number of binder threads requested but not
522 * yet started. In current implementation, can
523 * only be 0 or 1.
Todd Kjosd600e902017-05-25 17:35:02 -0700524 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700525 * @requested_threads_started: number binder threads started
Todd Kjosd600e902017-05-25 17:35:02 -0700526 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700527 * @tmp_ref: temporary reference to indicate proc is in use
Todd Kjosb4827902017-05-25 15:52:17 -0700528 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700529 * @default_priority: default scheduler priority
530 * (invariant after initialized)
531 * @debugfs_entry: debugfs node
532 * @alloc: binder allocator bookkeeping
533 * @context: binder_context for this proc
534 * (invariant after initialized)
535 * @inner_lock: can nest under outer_lock and/or node lock
536 * @outer_lock: no nesting under innor or node lock
537 * Lock order: 1) outer, 2) node, 3) inner
538 *
539 * Bookkeeping structure for binder processes
540 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900541struct binder_proc {
542 struct hlist_node proc_node;
543 struct rb_root threads;
544 struct rb_root nodes;
545 struct rb_root refs_by_desc;
546 struct rb_root refs_by_node;
Martijn Coenen22d64e4322017-06-02 11:15:44 -0700547 struct list_head waiting_threads;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900548 int pid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900549 struct task_struct *tsk;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900550 struct hlist_node deferred_work_node;
551 int deferred_work;
Todd Kjos2f993e22017-05-12 14:42:55 -0700552 bool is_dead;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900553
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900554 struct list_head todo;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900555 struct binder_stats stats;
556 struct list_head delivered_death;
557 int max_threads;
558 int requested_threads;
559 int requested_threads_started;
Todd Kjos2f993e22017-05-12 14:42:55 -0700560 int tmp_ref;
Martijn Coenen57b2ac62017-06-06 17:04:42 -0700561 struct binder_priority default_priority;
Arve Hjønnevåg16b66552009-04-28 20:57:50 -0700562 struct dentry *debugfs_entry;
Todd Kjosf85d2292016-10-10 10:39:59 -0700563 struct binder_alloc alloc;
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200564 struct binder_context *context;
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700565 spinlock_t inner_lock;
566 spinlock_t outer_lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900567};
568
569enum {
570 BINDER_LOOPER_STATE_REGISTERED = 0x01,
571 BINDER_LOOPER_STATE_ENTERED = 0x02,
572 BINDER_LOOPER_STATE_EXITED = 0x04,
573 BINDER_LOOPER_STATE_INVALID = 0x08,
574 BINDER_LOOPER_STATE_WAITING = 0x10,
Martijn Coenen22d64e4322017-06-02 11:15:44 -0700575 BINDER_LOOPER_STATE_POLL = 0x20,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900576};
577
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700578/**
579 * struct binder_thread - binder thread bookkeeping
580 * @proc: binder process for this thread
581 * (invariant after initialization)
582 * @rb_node: element for proc->threads rbtree
Todd Kjosb4827902017-05-25 15:52:17 -0700583 * (protected by @proc->inner_lock)
Martijn Coenen22d64e4322017-06-02 11:15:44 -0700584 * @waiting_thread_node: element for @proc->waiting_threads list
585 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700586 * @pid: PID for this thread
587 * (invariant after initialization)
588 * @looper: bitmap of looping state
589 * (only accessed by this thread)
590 * @looper_needs_return: looping thread needs to exit driver
591 * (no lock needed)
592 * @transaction_stack: stack of in-progress transactions for this thread
Martijn Coenen995a36e2017-06-02 13:36:52 -0700593 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700594 * @todo: list of work to do for this thread
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700595 * (protected by @proc->inner_lock)
Martijn Coenen1af61802017-10-19 15:04:46 +0200596 * @process_todo: whether work in @todo should be processed
597 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700598 * @return_error: transaction errors reported by this thread
599 * (only accessed by this thread)
600 * @reply_error: transaction errors reported by target thread
Martijn Coenen995a36e2017-06-02 13:36:52 -0700601 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700602 * @wait: wait queue for thread work
603 * @stats: per-thread statistics
604 * (atomics, no lock needed)
605 * @tmp_ref: temporary reference to indicate thread is in use
606 * (atomic since @proc->inner_lock cannot
607 * always be acquired)
608 * @is_dead: thread is dead and awaiting free
609 * when outstanding transactions are cleaned up
Todd Kjosb4827902017-05-25 15:52:17 -0700610 * (protected by @proc->inner_lock)
Martijn Coenen07a30fe2017-06-07 10:02:12 -0700611 * @task: struct task_struct for this thread
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700612 *
613 * Bookkeeping structure for binder threads.
614 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900615struct binder_thread {
616 struct binder_proc *proc;
617 struct rb_node rb_node;
Martijn Coenen22d64e4322017-06-02 11:15:44 -0700618 struct list_head waiting_thread_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900619 int pid;
Todd Kjos6798e6d2017-01-06 14:19:25 -0800620 int looper; /* only modified by this thread */
621 bool looper_need_return; /* can be written by other thread */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900622 struct binder_transaction *transaction_stack;
623 struct list_head todo;
Martijn Coenen1af61802017-10-19 15:04:46 +0200624 bool process_todo;
Todd Kjos858b8da2017-04-21 17:35:12 -0700625 struct binder_error return_error;
626 struct binder_error reply_error;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900627 wait_queue_head_t wait;
628 struct binder_stats stats;
Todd Kjos2f993e22017-05-12 14:42:55 -0700629 atomic_t tmp_ref;
630 bool is_dead;
Martijn Coenen07a30fe2017-06-07 10:02:12 -0700631 struct task_struct *task;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900632};
633
634struct binder_transaction {
635 int debug_id;
636 struct binder_work work;
637 struct binder_thread *from;
638 struct binder_transaction *from_parent;
639 struct binder_proc *to_proc;
640 struct binder_thread *to_thread;
641 struct binder_transaction *to_parent;
642 unsigned need_reply:1;
643 /* unsigned is_dead:1; */ /* not used at the moment */
644
645 struct binder_buffer *buffer;
646 unsigned int code;
647 unsigned int flags;
Martijn Coenen57b2ac62017-06-06 17:04:42 -0700648 struct binder_priority priority;
649 struct binder_priority saved_priority;
Martijn Coenen07a30fe2017-06-07 10:02:12 -0700650 bool set_priority_called;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -0600651 kuid_t sender_euid;
Todd Kjos2f993e22017-05-12 14:42:55 -0700652 /**
653 * @lock: protects @from, @to_proc, and @to_thread
654 *
655 * @from, @to_proc, and @to_thread can be set to NULL
656 * during thread teardown
657 */
658 spinlock_t lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900659};
660
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700661/**
662 * binder_proc_lock() - Acquire outer lock for given binder_proc
663 * @proc: struct binder_proc to acquire
664 *
665 * Acquires proc->outer_lock. Used to protect binder_ref
666 * structures associated with the given proc.
667 */
668#define binder_proc_lock(proc) _binder_proc_lock(proc, __LINE__)
669static void
670_binder_proc_lock(struct binder_proc *proc, int line)
671{
672 binder_debug(BINDER_DEBUG_SPINLOCKS,
673 "%s: line=%d\n", __func__, line);
674 spin_lock(&proc->outer_lock);
675}
676
677/**
678 * binder_proc_unlock() - Release spinlock for given binder_proc
679 * @proc: struct binder_proc to acquire
680 *
681 * Release lock acquired via binder_proc_lock()
682 */
683#define binder_proc_unlock(_proc) _binder_proc_unlock(_proc, __LINE__)
684static void
685_binder_proc_unlock(struct binder_proc *proc, int line)
686{
687 binder_debug(BINDER_DEBUG_SPINLOCKS,
688 "%s: line=%d\n", __func__, line);
689 spin_unlock(&proc->outer_lock);
690}
691
692/**
693 * binder_inner_proc_lock() - Acquire inner lock for given binder_proc
694 * @proc: struct binder_proc to acquire
695 *
696 * Acquires proc->inner_lock. Used to protect todo lists
697 */
698#define binder_inner_proc_lock(proc) _binder_inner_proc_lock(proc, __LINE__)
699static void
700_binder_inner_proc_lock(struct binder_proc *proc, int line)
701{
702 binder_debug(BINDER_DEBUG_SPINLOCKS,
703 "%s: line=%d\n", __func__, line);
704 spin_lock(&proc->inner_lock);
705}
706
707/**
708 * binder_inner_proc_unlock() - Release inner lock for given binder_proc
709 * @proc: struct binder_proc to acquire
710 *
711 * Release lock acquired via binder_inner_proc_lock()
712 */
713#define binder_inner_proc_unlock(proc) _binder_inner_proc_unlock(proc, __LINE__)
714static void
715_binder_inner_proc_unlock(struct binder_proc *proc, int line)
716{
717 binder_debug(BINDER_DEBUG_SPINLOCKS,
718 "%s: line=%d\n", __func__, line);
719 spin_unlock(&proc->inner_lock);
720}
721
722/**
723 * binder_node_lock() - Acquire spinlock for given binder_node
724 * @node: struct binder_node to acquire
725 *
726 * Acquires node->lock. Used to protect binder_node fields
727 */
728#define binder_node_lock(node) _binder_node_lock(node, __LINE__)
729static void
730_binder_node_lock(struct binder_node *node, int line)
731{
732 binder_debug(BINDER_DEBUG_SPINLOCKS,
733 "%s: line=%d\n", __func__, line);
734 spin_lock(&node->lock);
735}
736
737/**
738 * binder_node_unlock() - Release spinlock for given binder_proc
739 * @node: struct binder_node to acquire
740 *
741 * Release lock acquired via binder_node_lock()
742 */
743#define binder_node_unlock(node) _binder_node_unlock(node, __LINE__)
744static void
745_binder_node_unlock(struct binder_node *node, int line)
746{
747 binder_debug(BINDER_DEBUG_SPINLOCKS,
748 "%s: line=%d\n", __func__, line);
749 spin_unlock(&node->lock);
750}
751
Todd Kjoscbcbbd62017-06-08 13:45:59 -0700752/**
753 * binder_node_inner_lock() - Acquire node and inner locks
754 * @node: struct binder_node to acquire
755 *
756 * Acquires node->lock. If node->proc also acquires
757 * proc->inner_lock. Used to protect binder_node fields
758 */
759#define binder_node_inner_lock(node) _binder_node_inner_lock(node, __LINE__)
760static void
761_binder_node_inner_lock(struct binder_node *node, int line)
762{
763 binder_debug(BINDER_DEBUG_SPINLOCKS,
764 "%s: line=%d\n", __func__, line);
765 spin_lock(&node->lock);
766 if (node->proc)
767 binder_inner_proc_lock(node->proc);
768}
769
770/**
771 * binder_node_unlock() - Release node and inner locks
772 * @node: struct binder_node to acquire
773 *
774 * Release lock acquired via binder_node_lock()
775 */
776#define binder_node_inner_unlock(node) _binder_node_inner_unlock(node, __LINE__)
777static void
778_binder_node_inner_unlock(struct binder_node *node, int line)
779{
780 struct binder_proc *proc = node->proc;
781
782 binder_debug(BINDER_DEBUG_SPINLOCKS,
783 "%s: line=%d\n", __func__, line);
784 if (proc)
785 binder_inner_proc_unlock(proc);
786 spin_unlock(&node->lock);
787}
788
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700789static bool binder_worklist_empty_ilocked(struct list_head *list)
790{
791 return list_empty(list);
792}
793
794/**
795 * binder_worklist_empty() - Check if no items on the work list
796 * @proc: binder_proc associated with list
797 * @list: list to check
798 *
799 * Return: true if there are no items on list, else false
800 */
801static bool binder_worklist_empty(struct binder_proc *proc,
802 struct list_head *list)
803{
804 bool ret;
805
806 binder_inner_proc_lock(proc);
807 ret = binder_worklist_empty_ilocked(list);
808 binder_inner_proc_unlock(proc);
809 return ret;
810}
811
Martijn Coenen1af61802017-10-19 15:04:46 +0200812/**
813 * binder_enqueue_work_ilocked() - Add an item to the work list
814 * @work: struct binder_work to add to list
815 * @target_list: list to add work to
816 *
817 * Adds the work to the specified list. Asserts that work
818 * is not already on a list.
819 *
820 * Requires the proc->inner_lock to be held.
821 */
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700822static void
823binder_enqueue_work_ilocked(struct binder_work *work,
824 struct list_head *target_list)
825{
826 BUG_ON(target_list == NULL);
827 BUG_ON(work->entry.next && !list_empty(&work->entry));
828 list_add_tail(&work->entry, target_list);
829}
830
831/**
Martijn Coenendac2e9c2017-11-13 09:55:21 +0100832 * binder_enqueue_deferred_thread_work_ilocked() - Add deferred thread work
Martijn Coenen1af61802017-10-19 15:04:46 +0200833 * @thread: thread to queue work to
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700834 * @work: struct binder_work to add to list
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700835 *
Martijn Coenen1af61802017-10-19 15:04:46 +0200836 * Adds the work to the todo list of the thread. Doesn't set the process_todo
837 * flag, which means that (if it wasn't already set) the thread will go to
838 * sleep without handling this work when it calls read.
839 *
840 * Requires the proc->inner_lock to be held.
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700841 */
842static void
Martijn Coenendac2e9c2017-11-13 09:55:21 +0100843binder_enqueue_deferred_thread_work_ilocked(struct binder_thread *thread,
844 struct binder_work *work)
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700845{
Martijn Coenen1af61802017-10-19 15:04:46 +0200846 binder_enqueue_work_ilocked(work, &thread->todo);
847}
848
849/**
850 * binder_enqueue_thread_work_ilocked() - Add an item to the thread work list
851 * @thread: thread to queue work to
852 * @work: struct binder_work to add to list
853 *
854 * Adds the work to the todo list of the thread, and enables processing
855 * of the todo queue.
856 *
857 * Requires the proc->inner_lock to be held.
858 */
859static void
860binder_enqueue_thread_work_ilocked(struct binder_thread *thread,
861 struct binder_work *work)
862{
863 binder_enqueue_work_ilocked(work, &thread->todo);
864 thread->process_todo = true;
865}
866
867/**
868 * binder_enqueue_thread_work() - Add an item to the thread work list
869 * @thread: thread to queue work to
870 * @work: struct binder_work to add to list
871 *
872 * Adds the work to the todo list of the thread, and enables processing
873 * of the todo queue.
874 */
875static void
876binder_enqueue_thread_work(struct binder_thread *thread,
877 struct binder_work *work)
878{
879 binder_inner_proc_lock(thread->proc);
880 binder_enqueue_thread_work_ilocked(thread, work);
881 binder_inner_proc_unlock(thread->proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700882}
883
884static void
885binder_dequeue_work_ilocked(struct binder_work *work)
886{
887 list_del_init(&work->entry);
888}
889
890/**
891 * binder_dequeue_work() - Removes an item from the work list
892 * @proc: binder_proc associated with list
893 * @work: struct binder_work to remove from list
894 *
895 * Removes the specified work item from whatever list it is on.
896 * Can safely be called if work is not on any list.
897 */
898static void
899binder_dequeue_work(struct binder_proc *proc, struct binder_work *work)
900{
901 binder_inner_proc_lock(proc);
902 binder_dequeue_work_ilocked(work);
903 binder_inner_proc_unlock(proc);
904}
905
906static struct binder_work *binder_dequeue_work_head_ilocked(
907 struct list_head *list)
908{
909 struct binder_work *w;
910
911 w = list_first_entry_or_null(list, struct binder_work, entry);
912 if (w)
913 list_del_init(&w->entry);
914 return w;
915}
916
917/**
918 * binder_dequeue_work_head() - Dequeues the item at head of list
919 * @proc: binder_proc associated with list
920 * @list: list to dequeue head
921 *
922 * Removes the head of the list if there are items on the list
923 *
924 * Return: pointer dequeued binder_work, NULL if list was empty
925 */
926static struct binder_work *binder_dequeue_work_head(
927 struct binder_proc *proc,
928 struct list_head *list)
929{
930 struct binder_work *w;
931
932 binder_inner_proc_lock(proc);
933 w = binder_dequeue_work_head_ilocked(list);
934 binder_inner_proc_unlock(proc);
935 return w;
936}
937
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900938static void
939binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
Todd Kjos2f993e22017-05-12 14:42:55 -0700940static void binder_free_thread(struct binder_thread *thread);
941static void binder_free_proc(struct binder_proc *proc);
Todd Kjos425d23f2017-06-12 12:07:26 -0700942static void binder_inc_node_tmpref_ilocked(struct binder_node *node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900943
Todd Kjosf09daf12017-11-10 15:30:27 -0800944struct files_struct *binder_get_files_struct(struct binder_proc *proc)
945{
946 return get_files_struct(proc->tsk);
947}
948
Sachin Kamatefde99c2012-08-17 16:39:36 +0530949static int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900950{
Todd Kjosf09daf12017-11-10 15:30:27 -0800951 struct files_struct *files;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900952 unsigned long rlim_cur;
953 unsigned long irqs;
Todd Kjosf09daf12017-11-10 15:30:27 -0800954 int ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900955
Todd Kjosf09daf12017-11-10 15:30:27 -0800956 files = binder_get_files_struct(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900957 if (files == NULL)
958 return -ESRCH;
959
Todd Kjosf09daf12017-11-10 15:30:27 -0800960 if (!lock_task_sighand(proc->tsk, &irqs)) {
961 ret = -EMFILE;
962 goto err;
963 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900964
Al Virodcfadfa2012-08-12 17:27:30 -0400965 rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
966 unlock_task_sighand(proc->tsk, &irqs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900967
Todd Kjosf09daf12017-11-10 15:30:27 -0800968 ret = __alloc_fd(files, 0, rlim_cur, flags);
969err:
970 put_files_struct(files);
971 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900972}
973
974/*
975 * copied from fd_install
976 */
977static void task_fd_install(
978 struct binder_proc *proc, unsigned int fd, struct file *file)
979{
Todd Kjosf09daf12017-11-10 15:30:27 -0800980 struct files_struct *files = binder_get_files_struct(proc);
981
982 if (files) {
983 __fd_install(files, fd, file);
984 put_files_struct(files);
985 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900986}
987
988/*
989 * copied from sys_close
990 */
991static long task_close_fd(struct binder_proc *proc, unsigned int fd)
992{
Todd Kjosf09daf12017-11-10 15:30:27 -0800993 struct files_struct *files = binder_get_files_struct(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900994 int retval;
995
Todd Kjosf09daf12017-11-10 15:30:27 -0800996 if (files == NULL)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900997 return -ESRCH;
998
Todd Kjosf09daf12017-11-10 15:30:27 -0800999 retval = __close_fd(files, fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001000 /* can't restart close syscall because file table entry was cleared */
1001 if (unlikely(retval == -ERESTARTSYS ||
1002 retval == -ERESTARTNOINTR ||
1003 retval == -ERESTARTNOHAND ||
1004 retval == -ERESTART_RESTARTBLOCK))
1005 retval = -EINTR;
Todd Kjosf09daf12017-11-10 15:30:27 -08001006 put_files_struct(files);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001007
1008 return retval;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001009}
1010
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001011static bool binder_has_work_ilocked(struct binder_thread *thread,
1012 bool do_proc_work)
1013{
Martijn Coenen1af61802017-10-19 15:04:46 +02001014 return thread->process_todo ||
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001015 thread->looper_need_return ||
1016 (do_proc_work &&
1017 !binder_worklist_empty_ilocked(&thread->proc->todo));
1018}
1019
1020static bool binder_has_work(struct binder_thread *thread, bool do_proc_work)
1021{
1022 bool has_work;
1023
1024 binder_inner_proc_lock(thread->proc);
1025 has_work = binder_has_work_ilocked(thread, do_proc_work);
1026 binder_inner_proc_unlock(thread->proc);
1027
1028 return has_work;
1029}
1030
1031static bool binder_available_for_proc_work_ilocked(struct binder_thread *thread)
1032{
1033 return !thread->transaction_stack &&
1034 binder_worklist_empty_ilocked(&thread->todo) &&
1035 (thread->looper & (BINDER_LOOPER_STATE_ENTERED |
1036 BINDER_LOOPER_STATE_REGISTERED));
1037}
1038
1039static void binder_wakeup_poll_threads_ilocked(struct binder_proc *proc,
1040 bool sync)
1041{
1042 struct rb_node *n;
1043 struct binder_thread *thread;
1044
1045 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
1046 thread = rb_entry(n, struct binder_thread, rb_node);
1047 if (thread->looper & BINDER_LOOPER_STATE_POLL &&
1048 binder_available_for_proc_work_ilocked(thread)) {
1049 if (sync)
1050 wake_up_interruptible_sync(&thread->wait);
1051 else
1052 wake_up_interruptible(&thread->wait);
1053 }
1054 }
1055}
1056
Martijn Coenen053be422017-06-06 15:17:46 -07001057/**
1058 * binder_select_thread_ilocked() - selects a thread for doing proc work.
1059 * @proc: process to select a thread from
1060 *
1061 * Note that calling this function moves the thread off the waiting_threads
1062 * list, so it can only be woken up by the caller of this function, or a
1063 * signal. Therefore, callers *should* always wake up the thread this function
1064 * returns.
1065 *
1066 * Return: If there's a thread currently waiting for process work,
1067 * returns that thread. Otherwise returns NULL.
1068 */
1069static struct binder_thread *
1070binder_select_thread_ilocked(struct binder_proc *proc)
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001071{
1072 struct binder_thread *thread;
1073
Martijn Coenened323352017-07-27 23:52:24 +02001074 assert_spin_locked(&proc->inner_lock);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001075 thread = list_first_entry_or_null(&proc->waiting_threads,
1076 struct binder_thread,
1077 waiting_thread_node);
1078
Martijn Coenen053be422017-06-06 15:17:46 -07001079 if (thread)
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001080 list_del_init(&thread->waiting_thread_node);
Martijn Coenen053be422017-06-06 15:17:46 -07001081
1082 return thread;
1083}
1084
1085/**
1086 * binder_wakeup_thread_ilocked() - wakes up a thread for doing proc work.
1087 * @proc: process to wake up a thread in
1088 * @thread: specific thread to wake-up (may be NULL)
1089 * @sync: whether to do a synchronous wake-up
1090 *
1091 * This function wakes up a thread in the @proc process.
1092 * The caller may provide a specific thread to wake-up in
1093 * the @thread parameter. If @thread is NULL, this function
1094 * will wake up threads that have called poll().
1095 *
1096 * Note that for this function to work as expected, callers
1097 * should first call binder_select_thread() to find a thread
1098 * to handle the work (if they don't have a thread already),
1099 * and pass the result into the @thread parameter.
1100 */
1101static void binder_wakeup_thread_ilocked(struct binder_proc *proc,
1102 struct binder_thread *thread,
1103 bool sync)
1104{
Martijn Coenened323352017-07-27 23:52:24 +02001105 assert_spin_locked(&proc->inner_lock);
Martijn Coenen053be422017-06-06 15:17:46 -07001106
1107 if (thread) {
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001108 if (sync)
1109 wake_up_interruptible_sync(&thread->wait);
1110 else
1111 wake_up_interruptible(&thread->wait);
1112 return;
1113 }
1114
1115 /* Didn't find a thread waiting for proc work; this can happen
1116 * in two scenarios:
1117 * 1. All threads are busy handling transactions
1118 * In that case, one of those threads should call back into
1119 * the kernel driver soon and pick up this work.
1120 * 2. Threads are using the (e)poll interface, in which case
1121 * they may be blocked on the waitqueue without having been
1122 * added to waiting_threads. For this case, we just iterate
1123 * over all threads not handling transaction work, and
1124 * wake them all up. We wake all because we don't know whether
1125 * a thread that called into (e)poll is handling non-binder
1126 * work currently.
1127 */
1128 binder_wakeup_poll_threads_ilocked(proc, sync);
1129}
1130
Martijn Coenen053be422017-06-06 15:17:46 -07001131static void binder_wakeup_proc_ilocked(struct binder_proc *proc)
1132{
1133 struct binder_thread *thread = binder_select_thread_ilocked(proc);
1134
1135 binder_wakeup_thread_ilocked(proc, thread, /* sync = */false);
1136}
1137
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001138static bool is_rt_policy(int policy)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001139{
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001140 return policy == SCHED_FIFO || policy == SCHED_RR;
1141}
Seunghun Lee10f62862014-05-01 01:30:23 +09001142
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001143static bool is_fair_policy(int policy)
1144{
1145 return policy == SCHED_NORMAL || policy == SCHED_BATCH;
1146}
1147
1148static bool binder_supported_policy(int policy)
1149{
1150 return is_fair_policy(policy) || is_rt_policy(policy);
1151}
1152
1153static int to_userspace_prio(int policy, int kernel_priority)
1154{
1155 if (is_fair_policy(policy))
1156 return PRIO_TO_NICE(kernel_priority);
1157 else
1158 return MAX_USER_RT_PRIO - 1 - kernel_priority;
1159}
1160
1161static int to_kernel_prio(int policy, int user_priority)
1162{
1163 if (is_fair_policy(policy))
1164 return NICE_TO_PRIO(user_priority);
1165 else
1166 return MAX_USER_RT_PRIO - 1 - user_priority;
1167}
1168
Martijn Coenenecd972d2017-05-26 10:48:56 -07001169static void binder_do_set_priority(struct task_struct *task,
1170 struct binder_priority desired,
1171 bool verify)
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001172{
1173 int priority; /* user-space prio value */
1174 bool has_cap_nice;
1175 unsigned int policy = desired.sched_policy;
1176
1177 if (task->policy == policy && task->normal_prio == desired.prio)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001178 return;
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001179
1180 has_cap_nice = has_capability_noaudit(task, CAP_SYS_NICE);
1181
1182 priority = to_userspace_prio(policy, desired.prio);
1183
Martijn Coenenecd972d2017-05-26 10:48:56 -07001184 if (verify && is_rt_policy(policy) && !has_cap_nice) {
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001185 long max_rtprio = task_rlimit(task, RLIMIT_RTPRIO);
1186
1187 if (max_rtprio == 0) {
1188 policy = SCHED_NORMAL;
1189 priority = MIN_NICE;
1190 } else if (priority > max_rtprio) {
1191 priority = max_rtprio;
1192 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001193 }
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001194
Martijn Coenenecd972d2017-05-26 10:48:56 -07001195 if (verify && is_fair_policy(policy) && !has_cap_nice) {
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001196 long min_nice = rlimit_to_nice(task_rlimit(task, RLIMIT_NICE));
1197
1198 if (min_nice > MAX_NICE) {
1199 binder_user_error("%d RLIMIT_NICE not set\n",
1200 task->pid);
1201 return;
1202 } else if (priority < min_nice) {
1203 priority = min_nice;
1204 }
1205 }
1206
1207 if (policy != desired.sched_policy ||
1208 to_kernel_prio(policy, priority) != desired.prio)
1209 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
1210 "%d: priority %d not allowed, using %d instead\n",
1211 task->pid, desired.prio,
1212 to_kernel_prio(policy, priority));
1213
Martijn Coenen81402ea2017-05-08 09:33:22 -07001214 trace_binder_set_priority(task->tgid, task->pid, task->normal_prio,
1215 to_kernel_prio(policy, priority),
1216 desired.prio);
1217
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001218 /* Set the actual priority */
1219 if (task->policy != policy || is_rt_policy(policy)) {
1220 struct sched_param params;
1221
1222 params.sched_priority = is_rt_policy(policy) ? priority : 0;
1223
1224 sched_setscheduler_nocheck(task,
1225 policy | SCHED_RESET_ON_FORK,
1226 &params);
1227 }
1228 if (is_fair_policy(policy))
1229 set_user_nice(task, priority);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001230}
1231
Martijn Coenenecd972d2017-05-26 10:48:56 -07001232static void binder_set_priority(struct task_struct *task,
1233 struct binder_priority desired)
1234{
1235 binder_do_set_priority(task, desired, /* verify = */ true);
1236}
1237
1238static void binder_restore_priority(struct task_struct *task,
1239 struct binder_priority desired)
1240{
1241 binder_do_set_priority(task, desired, /* verify = */ false);
1242}
1243
Martijn Coenen07a30fe2017-06-07 10:02:12 -07001244static void binder_transaction_priority(struct task_struct *task,
1245 struct binder_transaction *t,
Martijn Coenenc46810c2017-06-23 10:13:43 -07001246 struct binder_priority node_prio,
1247 bool inherit_rt)
Martijn Coenen07a30fe2017-06-07 10:02:12 -07001248{
Ganesh Mahendran9add7c42017-09-27 15:12:25 +08001249 struct binder_priority desired_prio = t->priority;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07001250
1251 if (t->set_priority_called)
1252 return;
1253
1254 t->set_priority_called = true;
1255 t->saved_priority.sched_policy = task->policy;
1256 t->saved_priority.prio = task->normal_prio;
1257
Martijn Coenenc46810c2017-06-23 10:13:43 -07001258 if (!inherit_rt && is_rt_policy(desired_prio.sched_policy)) {
1259 desired_prio.prio = NICE_TO_PRIO(0);
1260 desired_prio.sched_policy = SCHED_NORMAL;
Martijn Coenenc46810c2017-06-23 10:13:43 -07001261 }
Martijn Coenen07a30fe2017-06-07 10:02:12 -07001262
1263 if (node_prio.prio < t->priority.prio ||
1264 (node_prio.prio == t->priority.prio &&
1265 node_prio.sched_policy == SCHED_FIFO)) {
1266 /*
1267 * In case the minimum priority on the node is
1268 * higher (lower value), use that priority. If
1269 * the priority is the same, but the node uses
1270 * SCHED_FIFO, prefer SCHED_FIFO, since it can
1271 * run unbounded, unlike SCHED_RR.
1272 */
1273 desired_prio = node_prio;
1274 }
1275
1276 binder_set_priority(task, desired_prio);
1277}
1278
Todd Kjos425d23f2017-06-12 12:07:26 -07001279static struct binder_node *binder_get_node_ilocked(struct binder_proc *proc,
1280 binder_uintptr_t ptr)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001281{
1282 struct rb_node *n = proc->nodes.rb_node;
1283 struct binder_node *node;
1284
Martijn Coenened323352017-07-27 23:52:24 +02001285 assert_spin_locked(&proc->inner_lock);
Todd Kjos425d23f2017-06-12 12:07:26 -07001286
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001287 while (n) {
1288 node = rb_entry(n, struct binder_node, rb_node);
1289
1290 if (ptr < node->ptr)
1291 n = n->rb_left;
1292 else if (ptr > node->ptr)
1293 n = n->rb_right;
Todd Kjosf22abc72017-05-09 11:08:05 -07001294 else {
1295 /*
1296 * take an implicit weak reference
1297 * to ensure node stays alive until
1298 * call to binder_put_node()
1299 */
Todd Kjos425d23f2017-06-12 12:07:26 -07001300 binder_inc_node_tmpref_ilocked(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001301 return node;
Todd Kjosf22abc72017-05-09 11:08:05 -07001302 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001303 }
1304 return NULL;
1305}
1306
Todd Kjos425d23f2017-06-12 12:07:26 -07001307static struct binder_node *binder_get_node(struct binder_proc *proc,
1308 binder_uintptr_t ptr)
1309{
1310 struct binder_node *node;
1311
1312 binder_inner_proc_lock(proc);
1313 node = binder_get_node_ilocked(proc, ptr);
1314 binder_inner_proc_unlock(proc);
1315 return node;
1316}
1317
1318static struct binder_node *binder_init_node_ilocked(
1319 struct binder_proc *proc,
1320 struct binder_node *new_node,
1321 struct flat_binder_object *fp)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001322{
1323 struct rb_node **p = &proc->nodes.rb_node;
1324 struct rb_node *parent = NULL;
1325 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001326 binder_uintptr_t ptr = fp ? fp->binder : 0;
1327 binder_uintptr_t cookie = fp ? fp->cookie : 0;
1328 __u32 flags = fp ? fp->flags : 0;
Martijn Coenen6aac9792017-06-07 09:29:14 -07001329 s8 priority;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001330
Martijn Coenened323352017-07-27 23:52:24 +02001331 assert_spin_locked(&proc->inner_lock);
1332
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001333 while (*p) {
Todd Kjos425d23f2017-06-12 12:07:26 -07001334
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001335 parent = *p;
1336 node = rb_entry(parent, struct binder_node, rb_node);
1337
1338 if (ptr < node->ptr)
1339 p = &(*p)->rb_left;
1340 else if (ptr > node->ptr)
1341 p = &(*p)->rb_right;
Todd Kjos425d23f2017-06-12 12:07:26 -07001342 else {
1343 /*
1344 * A matching node is already in
1345 * the rb tree. Abandon the init
1346 * and return it.
1347 */
1348 binder_inc_node_tmpref_ilocked(node);
1349 return node;
1350 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001351 }
Todd Kjos425d23f2017-06-12 12:07:26 -07001352 node = new_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001353 binder_stats_created(BINDER_STAT_NODE);
Todd Kjosf22abc72017-05-09 11:08:05 -07001354 node->tmp_refs++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001355 rb_link_node(&node->rb_node, parent, p);
1356 rb_insert_color(&node->rb_node, &proc->nodes);
Todd Kjosc4bd08b2017-05-25 10:56:00 -07001357 node->debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001358 node->proc = proc;
1359 node->ptr = ptr;
1360 node->cookie = cookie;
1361 node->work.type = BINDER_WORK_NODE;
Martijn Coenen6aac9792017-06-07 09:29:14 -07001362 priority = flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
Ganesh Mahendran6cd26312017-09-26 17:56:25 +08001363 node->sched_policy = (flags & FLAT_BINDER_FLAG_SCHED_POLICY_MASK) >>
Martijn Coenen6aac9792017-06-07 09:29:14 -07001364 FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT;
1365 node->min_priority = to_kernel_prio(node->sched_policy, priority);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001366 node->accept_fds = !!(flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
Martijn Coenenc46810c2017-06-23 10:13:43 -07001367 node->inherit_rt = !!(flags & FLAT_BINDER_FLAG_INHERIT_RT);
Todd Kjosfc7a7e22017-05-29 16:44:24 -07001368 spin_lock_init(&node->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001369 INIT_LIST_HEAD(&node->work.entry);
1370 INIT_LIST_HEAD(&node->async_todo);
1371 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001372 "%d:%d node %d u%016llx c%016llx created\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001373 proc->pid, current->pid, node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001374 (u64)node->ptr, (u64)node->cookie);
Todd Kjos425d23f2017-06-12 12:07:26 -07001375
1376 return node;
1377}
1378
1379static struct binder_node *binder_new_node(struct binder_proc *proc,
1380 struct flat_binder_object *fp)
1381{
1382 struct binder_node *node;
1383 struct binder_node *new_node = kzalloc(sizeof(*node), GFP_KERNEL);
1384
1385 if (!new_node)
1386 return NULL;
1387 binder_inner_proc_lock(proc);
1388 node = binder_init_node_ilocked(proc, new_node, fp);
1389 binder_inner_proc_unlock(proc);
1390 if (node != new_node)
1391 /*
1392 * The node was already added by another thread
1393 */
1394 kfree(new_node);
1395
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001396 return node;
1397}
1398
Todd Kjose7f23ed2017-03-21 13:06:01 -07001399static void binder_free_node(struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001400{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001401 kfree(node);
1402 binder_stats_deleted(BINDER_STAT_NODE);
1403}
1404
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001405static int binder_inc_node_nilocked(struct binder_node *node, int strong,
1406 int internal,
1407 struct list_head *target_list)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001408{
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001409 struct binder_proc *proc = node->proc;
1410
Martijn Coenened323352017-07-27 23:52:24 +02001411 assert_spin_locked(&node->lock);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001412 if (proc)
Martijn Coenened323352017-07-27 23:52:24 +02001413 assert_spin_locked(&proc->inner_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001414 if (strong) {
1415 if (internal) {
1416 if (target_list == NULL &&
1417 node->internal_strong_refs == 0 &&
Martijn Coenen0b3311e2016-09-30 15:51:48 +02001418 !(node->proc &&
1419 node == node->proc->context->
1420 binder_context_mgr_node &&
1421 node->has_strong_ref)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301422 pr_err("invalid inc strong node for %d\n",
1423 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001424 return -EINVAL;
1425 }
1426 node->internal_strong_refs++;
1427 } else
1428 node->local_strong_refs++;
1429 if (!node->has_strong_ref && target_list) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001430 binder_dequeue_work_ilocked(&node->work);
Martijn Coenen1af61802017-10-19 15:04:46 +02001431 /*
1432 * Note: this function is the only place where we queue
1433 * directly to a thread->todo without using the
1434 * corresponding binder_enqueue_thread_work() helper
1435 * functions; in this case it's ok to not set the
1436 * process_todo flag, since we know this node work will
1437 * always be followed by other work that starts queue
1438 * processing: in case of synchronous transactions, a
1439 * BR_REPLY or BR_ERROR; in case of oneway
1440 * transactions, a BR_TRANSACTION_COMPLETE.
1441 */
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001442 binder_enqueue_work_ilocked(&node->work, target_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001443 }
1444 } else {
1445 if (!internal)
1446 node->local_weak_refs++;
1447 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
1448 if (target_list == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301449 pr_err("invalid inc weak node for %d\n",
1450 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001451 return -EINVAL;
1452 }
Martijn Coenen1af61802017-10-19 15:04:46 +02001453 /*
1454 * See comment above
1455 */
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001456 binder_enqueue_work_ilocked(&node->work, target_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001457 }
1458 }
1459 return 0;
1460}
1461
Todd Kjose7f23ed2017-03-21 13:06:01 -07001462static int binder_inc_node(struct binder_node *node, int strong, int internal,
1463 struct list_head *target_list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001464{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001465 int ret;
1466
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001467 binder_node_inner_lock(node);
1468 ret = binder_inc_node_nilocked(node, strong, internal, target_list);
1469 binder_node_inner_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001470
1471 return ret;
1472}
1473
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001474static bool binder_dec_node_nilocked(struct binder_node *node,
1475 int strong, int internal)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001476{
1477 struct binder_proc *proc = node->proc;
1478
Martijn Coenened323352017-07-27 23:52:24 +02001479 assert_spin_locked(&node->lock);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001480 if (proc)
Martijn Coenened323352017-07-27 23:52:24 +02001481 assert_spin_locked(&proc->inner_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001482 if (strong) {
1483 if (internal)
1484 node->internal_strong_refs--;
1485 else
1486 node->local_strong_refs--;
1487 if (node->local_strong_refs || node->internal_strong_refs)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001488 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001489 } else {
1490 if (!internal)
1491 node->local_weak_refs--;
Todd Kjosf22abc72017-05-09 11:08:05 -07001492 if (node->local_weak_refs || node->tmp_refs ||
1493 !hlist_empty(&node->refs))
Todd Kjose7f23ed2017-03-21 13:06:01 -07001494 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001495 }
Todd Kjose7f23ed2017-03-21 13:06:01 -07001496
1497 if (proc && (node->has_strong_ref || node->has_weak_ref)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001498 if (list_empty(&node->work.entry)) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001499 binder_enqueue_work_ilocked(&node->work, &proc->todo);
Martijn Coenen053be422017-06-06 15:17:46 -07001500 binder_wakeup_proc_ilocked(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001501 }
1502 } else {
1503 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
Todd Kjosf22abc72017-05-09 11:08:05 -07001504 !node->local_weak_refs && !node->tmp_refs) {
Todd Kjose7f23ed2017-03-21 13:06:01 -07001505 if (proc) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001506 binder_dequeue_work_ilocked(&node->work);
1507 rb_erase(&node->rb_node, &proc->nodes);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001508 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301509 "refless node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001510 node->debug_id);
1511 } else {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001512 BUG_ON(!list_empty(&node->work.entry));
Todd Kjos8d9f6f32016-10-17 12:33:15 -07001513 spin_lock(&binder_dead_nodes_lock);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001514 /*
1515 * tmp_refs could have changed so
1516 * check it again
1517 */
1518 if (node->tmp_refs) {
1519 spin_unlock(&binder_dead_nodes_lock);
1520 return false;
1521 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001522 hlist_del(&node->dead_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07001523 spin_unlock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001524 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301525 "dead node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001526 node->debug_id);
1527 }
Todd Kjose7f23ed2017-03-21 13:06:01 -07001528 return true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001529 }
1530 }
Todd Kjose7f23ed2017-03-21 13:06:01 -07001531 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001532}
1533
Todd Kjose7f23ed2017-03-21 13:06:01 -07001534static void binder_dec_node(struct binder_node *node, int strong, int internal)
1535{
1536 bool free_node;
1537
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001538 binder_node_inner_lock(node);
1539 free_node = binder_dec_node_nilocked(node, strong, internal);
1540 binder_node_inner_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001541 if (free_node)
1542 binder_free_node(node);
1543}
1544
1545static void binder_inc_node_tmpref_ilocked(struct binder_node *node)
Todd Kjosf22abc72017-05-09 11:08:05 -07001546{
1547 /*
1548 * No call to binder_inc_node() is needed since we
1549 * don't need to inform userspace of any changes to
1550 * tmp_refs
1551 */
1552 node->tmp_refs++;
1553}
1554
1555/**
Todd Kjose7f23ed2017-03-21 13:06:01 -07001556 * binder_inc_node_tmpref() - take a temporary reference on node
1557 * @node: node to reference
1558 *
1559 * Take reference on node to prevent the node from being freed
1560 * while referenced only by a local variable. The inner lock is
1561 * needed to serialize with the node work on the queue (which
1562 * isn't needed after the node is dead). If the node is dead
1563 * (node->proc is NULL), use binder_dead_nodes_lock to protect
1564 * node->tmp_refs against dead-node-only cases where the node
1565 * lock cannot be acquired (eg traversing the dead node list to
1566 * print nodes)
1567 */
1568static void binder_inc_node_tmpref(struct binder_node *node)
1569{
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001570 binder_node_lock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001571 if (node->proc)
1572 binder_inner_proc_lock(node->proc);
1573 else
1574 spin_lock(&binder_dead_nodes_lock);
1575 binder_inc_node_tmpref_ilocked(node);
1576 if (node->proc)
1577 binder_inner_proc_unlock(node->proc);
1578 else
1579 spin_unlock(&binder_dead_nodes_lock);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001580 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001581}
1582
1583/**
Todd Kjosf22abc72017-05-09 11:08:05 -07001584 * binder_dec_node_tmpref() - remove a temporary reference on node
1585 * @node: node to reference
1586 *
1587 * Release temporary reference on node taken via binder_inc_node_tmpref()
1588 */
1589static void binder_dec_node_tmpref(struct binder_node *node)
1590{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001591 bool free_node;
1592
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001593 binder_node_inner_lock(node);
1594 if (!node->proc)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001595 spin_lock(&binder_dead_nodes_lock);
Todd Kjosf22abc72017-05-09 11:08:05 -07001596 node->tmp_refs--;
1597 BUG_ON(node->tmp_refs < 0);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001598 if (!node->proc)
1599 spin_unlock(&binder_dead_nodes_lock);
Todd Kjosf22abc72017-05-09 11:08:05 -07001600 /*
1601 * Call binder_dec_node() to check if all refcounts are 0
1602 * and cleanup is needed. Calling with strong=0 and internal=1
1603 * causes no actual reference to be released in binder_dec_node().
1604 * If that changes, a change is needed here too.
1605 */
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001606 free_node = binder_dec_node_nilocked(node, 0, 1);
1607 binder_node_inner_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001608 if (free_node)
1609 binder_free_node(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07001610}
1611
1612static void binder_put_node(struct binder_node *node)
1613{
1614 binder_dec_node_tmpref(node);
1615}
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001616
Todd Kjos5346bf32016-10-20 16:43:34 -07001617static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc,
1618 u32 desc, bool need_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001619{
1620 struct rb_node *n = proc->refs_by_desc.rb_node;
1621 struct binder_ref *ref;
1622
1623 while (n) {
1624 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1625
Todd Kjosb0117bb2017-05-08 09:16:27 -07001626 if (desc < ref->data.desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001627 n = n->rb_left;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001628 } else if (desc > ref->data.desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001629 n = n->rb_right;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001630 } else if (need_strong_ref && !ref->data.strong) {
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001631 binder_user_error("tried to use weak ref as strong ref\n");
1632 return NULL;
1633 } else {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001634 return ref;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001635 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001636 }
1637 return NULL;
1638}
1639
Todd Kjosb0117bb2017-05-08 09:16:27 -07001640/**
Todd Kjos5346bf32016-10-20 16:43:34 -07001641 * binder_get_ref_for_node_olocked() - get the ref associated with given node
Todd Kjosb0117bb2017-05-08 09:16:27 -07001642 * @proc: binder_proc that owns the ref
1643 * @node: binder_node of target
1644 * @new_ref: newly allocated binder_ref to be initialized or %NULL
1645 *
1646 * Look up the ref for the given node and return it if it exists
1647 *
1648 * If it doesn't exist and the caller provides a newly allocated
1649 * ref, initialize the fields of the newly allocated ref and insert
1650 * into the given proc rb_trees and node refs list.
1651 *
1652 * Return: the ref for node. It is possible that another thread
1653 * allocated/initialized the ref first in which case the
1654 * returned ref would be different than the passed-in
1655 * new_ref. new_ref must be kfree'd by the caller in
1656 * this case.
1657 */
Todd Kjos5346bf32016-10-20 16:43:34 -07001658static struct binder_ref *binder_get_ref_for_node_olocked(
1659 struct binder_proc *proc,
1660 struct binder_node *node,
1661 struct binder_ref *new_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001662{
Todd Kjosb0117bb2017-05-08 09:16:27 -07001663 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001664 struct rb_node **p = &proc->refs_by_node.rb_node;
1665 struct rb_node *parent = NULL;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001666 struct binder_ref *ref;
1667 struct rb_node *n;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001668
1669 while (*p) {
1670 parent = *p;
1671 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1672
1673 if (node < ref->node)
1674 p = &(*p)->rb_left;
1675 else if (node > ref->node)
1676 p = &(*p)->rb_right;
1677 else
1678 return ref;
1679 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001680 if (!new_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001681 return NULL;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001682
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001683 binder_stats_created(BINDER_STAT_REF);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001684 new_ref->data.debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001685 new_ref->proc = proc;
1686 new_ref->node = node;
1687 rb_link_node(&new_ref->rb_node_node, parent, p);
1688 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1689
Todd Kjosb0117bb2017-05-08 09:16:27 -07001690 new_ref->data.desc = (node == context->binder_context_mgr_node) ? 0 : 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001691 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1692 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001693 if (ref->data.desc > new_ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001694 break;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001695 new_ref->data.desc = ref->data.desc + 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001696 }
1697
1698 p = &proc->refs_by_desc.rb_node;
1699 while (*p) {
1700 parent = *p;
1701 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1702
Todd Kjosb0117bb2017-05-08 09:16:27 -07001703 if (new_ref->data.desc < ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001704 p = &(*p)->rb_left;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001705 else if (new_ref->data.desc > ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001706 p = &(*p)->rb_right;
1707 else
1708 BUG();
1709 }
1710 rb_link_node(&new_ref->rb_node_desc, parent, p);
1711 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001712
1713 binder_node_lock(node);
Todd Kjos4cbe5752017-05-01 17:21:51 -07001714 hlist_add_head(&new_ref->node_entry, &node->refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001715
Todd Kjos4cbe5752017-05-01 17:21:51 -07001716 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1717 "%d new ref %d desc %d for node %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001718 proc->pid, new_ref->data.debug_id, new_ref->data.desc,
Todd Kjos4cbe5752017-05-01 17:21:51 -07001719 node->debug_id);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001720 binder_node_unlock(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001721 return new_ref;
1722}
1723
Todd Kjos5346bf32016-10-20 16:43:34 -07001724static void binder_cleanup_ref_olocked(struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001725{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001726 bool delete_node = false;
Todd Kjose7f23ed2017-03-21 13:06:01 -07001727
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001728 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301729 "%d delete ref %d desc %d for node %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001730 ref->proc->pid, ref->data.debug_id, ref->data.desc,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301731 ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001732
1733 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1734 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001735
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001736 binder_node_inner_lock(ref->node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001737 if (ref->data.strong)
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001738 binder_dec_node_nilocked(ref->node, 1, 1);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001739
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001740 hlist_del(&ref->node_entry);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001741 delete_node = binder_dec_node_nilocked(ref->node, 0, 1);
1742 binder_node_inner_unlock(ref->node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001743 /*
1744 * Clear ref->node unless we want the caller to free the node
1745 */
1746 if (!delete_node) {
1747 /*
1748 * The caller uses ref->node to determine
1749 * whether the node needs to be freed. Clear
1750 * it since the node is still alive.
1751 */
1752 ref->node = NULL;
1753 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001754
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001755 if (ref->death) {
1756 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301757 "%d delete ref %d desc %d has death notification\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001758 ref->proc->pid, ref->data.debug_id,
1759 ref->data.desc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001760 binder_dequeue_work(ref->proc, &ref->death->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001761 binder_stats_deleted(BINDER_STAT_DEATH);
1762 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001763 binder_stats_deleted(BINDER_STAT_REF);
1764}
1765
Todd Kjosb0117bb2017-05-08 09:16:27 -07001766/**
Todd Kjos5346bf32016-10-20 16:43:34 -07001767 * binder_inc_ref_olocked() - increment the ref for given handle
Todd Kjosb0117bb2017-05-08 09:16:27 -07001768 * @ref: ref to be incremented
1769 * @strong: if true, strong increment, else weak
1770 * @target_list: list to queue node work on
1771 *
Todd Kjos5346bf32016-10-20 16:43:34 -07001772 * Increment the ref. @ref->proc->outer_lock must be held on entry
Todd Kjosb0117bb2017-05-08 09:16:27 -07001773 *
1774 * Return: 0, if successful, else errno
1775 */
Todd Kjos5346bf32016-10-20 16:43:34 -07001776static int binder_inc_ref_olocked(struct binder_ref *ref, int strong,
1777 struct list_head *target_list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001778{
1779 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +09001780
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001781 if (strong) {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001782 if (ref->data.strong == 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001783 ret = binder_inc_node(ref->node, 1, 1, target_list);
1784 if (ret)
1785 return ret;
1786 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001787 ref->data.strong++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001788 } else {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001789 if (ref->data.weak == 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001790 ret = binder_inc_node(ref->node, 0, 1, target_list);
1791 if (ret)
1792 return ret;
1793 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001794 ref->data.weak++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001795 }
1796 return 0;
1797}
1798
Todd Kjosb0117bb2017-05-08 09:16:27 -07001799/**
1800 * binder_dec_ref() - dec the ref for given handle
1801 * @ref: ref to be decremented
1802 * @strong: if true, strong decrement, else weak
1803 *
1804 * Decrement the ref.
1805 *
Todd Kjosb0117bb2017-05-08 09:16:27 -07001806 * Return: true if ref is cleaned up and ready to be freed
1807 */
Todd Kjos5346bf32016-10-20 16:43:34 -07001808static bool binder_dec_ref_olocked(struct binder_ref *ref, int strong)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001809{
1810 if (strong) {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001811 if (ref->data.strong == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301812 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001813 ref->proc->pid, ref->data.debug_id,
1814 ref->data.desc, ref->data.strong,
1815 ref->data.weak);
1816 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001817 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001818 ref->data.strong--;
Todd Kjose7f23ed2017-03-21 13:06:01 -07001819 if (ref->data.strong == 0)
1820 binder_dec_node(ref->node, strong, 1);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001821 } else {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001822 if (ref->data.weak == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301823 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001824 ref->proc->pid, ref->data.debug_id,
1825 ref->data.desc, ref->data.strong,
1826 ref->data.weak);
1827 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001828 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001829 ref->data.weak--;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001830 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001831 if (ref->data.strong == 0 && ref->data.weak == 0) {
Todd Kjos5346bf32016-10-20 16:43:34 -07001832 binder_cleanup_ref_olocked(ref);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001833 return true;
1834 }
1835 return false;
1836}
1837
1838/**
1839 * binder_get_node_from_ref() - get the node from the given proc/desc
1840 * @proc: proc containing the ref
1841 * @desc: the handle associated with the ref
1842 * @need_strong_ref: if true, only return node if ref is strong
1843 * @rdata: the id/refcount data for the ref
1844 *
1845 * Given a proc and ref handle, return the associated binder_node
1846 *
1847 * Return: a binder_node or NULL if not found or not strong when strong required
1848 */
1849static struct binder_node *binder_get_node_from_ref(
1850 struct binder_proc *proc,
1851 u32 desc, bool need_strong_ref,
1852 struct binder_ref_data *rdata)
1853{
1854 struct binder_node *node;
1855 struct binder_ref *ref;
1856
Todd Kjos5346bf32016-10-20 16:43:34 -07001857 binder_proc_lock(proc);
1858 ref = binder_get_ref_olocked(proc, desc, need_strong_ref);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001859 if (!ref)
1860 goto err_no_ref;
1861 node = ref->node;
Todd Kjosf22abc72017-05-09 11:08:05 -07001862 /*
1863 * Take an implicit reference on the node to ensure
1864 * it stays alive until the call to binder_put_node()
1865 */
1866 binder_inc_node_tmpref(node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001867 if (rdata)
1868 *rdata = ref->data;
Todd Kjos5346bf32016-10-20 16:43:34 -07001869 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001870
1871 return node;
1872
1873err_no_ref:
Todd Kjos5346bf32016-10-20 16:43:34 -07001874 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001875 return NULL;
1876}
1877
1878/**
1879 * binder_free_ref() - free the binder_ref
1880 * @ref: ref to free
1881 *
Todd Kjose7f23ed2017-03-21 13:06:01 -07001882 * Free the binder_ref. Free the binder_node indicated by ref->node
1883 * (if non-NULL) and the binder_ref_death indicated by ref->death.
Todd Kjosb0117bb2017-05-08 09:16:27 -07001884 */
1885static void binder_free_ref(struct binder_ref *ref)
1886{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001887 if (ref->node)
1888 binder_free_node(ref->node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001889 kfree(ref->death);
1890 kfree(ref);
1891}
1892
1893/**
1894 * binder_update_ref_for_handle() - inc/dec the ref for given handle
1895 * @proc: proc containing the ref
1896 * @desc: the handle associated with the ref
1897 * @increment: true=inc reference, false=dec reference
1898 * @strong: true=strong reference, false=weak reference
1899 * @rdata: the id/refcount data for the ref
1900 *
1901 * Given a proc and ref handle, increment or decrement the ref
1902 * according to "increment" arg.
1903 *
1904 * Return: 0 if successful, else errno
1905 */
1906static int binder_update_ref_for_handle(struct binder_proc *proc,
1907 uint32_t desc, bool increment, bool strong,
1908 struct binder_ref_data *rdata)
1909{
1910 int ret = 0;
1911 struct binder_ref *ref;
1912 bool delete_ref = false;
1913
Todd Kjos5346bf32016-10-20 16:43:34 -07001914 binder_proc_lock(proc);
1915 ref = binder_get_ref_olocked(proc, desc, strong);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001916 if (!ref) {
1917 ret = -EINVAL;
1918 goto err_no_ref;
1919 }
1920 if (increment)
Todd Kjos5346bf32016-10-20 16:43:34 -07001921 ret = binder_inc_ref_olocked(ref, strong, NULL);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001922 else
Todd Kjos5346bf32016-10-20 16:43:34 -07001923 delete_ref = binder_dec_ref_olocked(ref, strong);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001924
1925 if (rdata)
1926 *rdata = ref->data;
Todd Kjos5346bf32016-10-20 16:43:34 -07001927 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001928
1929 if (delete_ref)
1930 binder_free_ref(ref);
1931 return ret;
1932
1933err_no_ref:
Todd Kjos5346bf32016-10-20 16:43:34 -07001934 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001935 return ret;
1936}
1937
1938/**
1939 * binder_dec_ref_for_handle() - dec the ref for given handle
1940 * @proc: proc containing the ref
1941 * @desc: the handle associated with the ref
1942 * @strong: true=strong reference, false=weak reference
1943 * @rdata: the id/refcount data for the ref
1944 *
1945 * Just calls binder_update_ref_for_handle() to decrement the ref.
1946 *
1947 * Return: 0 if successful, else errno
1948 */
1949static int binder_dec_ref_for_handle(struct binder_proc *proc,
1950 uint32_t desc, bool strong, struct binder_ref_data *rdata)
1951{
1952 return binder_update_ref_for_handle(proc, desc, false, strong, rdata);
1953}
1954
1955
1956/**
1957 * binder_inc_ref_for_node() - increment the ref for given proc/node
1958 * @proc: proc containing the ref
1959 * @node: target node
1960 * @strong: true=strong reference, false=weak reference
1961 * @target_list: worklist to use if node is incremented
1962 * @rdata: the id/refcount data for the ref
1963 *
1964 * Given a proc and node, increment the ref. Create the ref if it
1965 * doesn't already exist
1966 *
1967 * Return: 0 if successful, else errno
1968 */
1969static int binder_inc_ref_for_node(struct binder_proc *proc,
1970 struct binder_node *node,
1971 bool strong,
1972 struct list_head *target_list,
1973 struct binder_ref_data *rdata)
1974{
1975 struct binder_ref *ref;
1976 struct binder_ref *new_ref = NULL;
1977 int ret = 0;
1978
Todd Kjos5346bf32016-10-20 16:43:34 -07001979 binder_proc_lock(proc);
1980 ref = binder_get_ref_for_node_olocked(proc, node, NULL);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001981 if (!ref) {
Todd Kjos5346bf32016-10-20 16:43:34 -07001982 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001983 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1984 if (!new_ref)
1985 return -ENOMEM;
Todd Kjos5346bf32016-10-20 16:43:34 -07001986 binder_proc_lock(proc);
1987 ref = binder_get_ref_for_node_olocked(proc, node, new_ref);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001988 }
Todd Kjos5346bf32016-10-20 16:43:34 -07001989 ret = binder_inc_ref_olocked(ref, strong, target_list);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001990 *rdata = ref->data;
Todd Kjos5346bf32016-10-20 16:43:34 -07001991 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001992 if (new_ref && ref != new_ref)
1993 /*
1994 * Another thread created the ref first so
1995 * free the one we allocated
1996 */
1997 kfree(new_ref);
1998 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001999}
2000
Martijn Coenen995a36e2017-06-02 13:36:52 -07002001static void binder_pop_transaction_ilocked(struct binder_thread *target_thread,
2002 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002003{
Todd Kjos21ef40a2017-03-30 18:02:13 -07002004 BUG_ON(!target_thread);
Martijn Coenened323352017-07-27 23:52:24 +02002005 assert_spin_locked(&target_thread->proc->inner_lock);
Todd Kjos21ef40a2017-03-30 18:02:13 -07002006 BUG_ON(target_thread->transaction_stack != t);
2007 BUG_ON(target_thread->transaction_stack->from != target_thread);
2008 target_thread->transaction_stack =
2009 target_thread->transaction_stack->from_parent;
2010 t->from = NULL;
2011}
2012
Todd Kjos2f993e22017-05-12 14:42:55 -07002013/**
2014 * binder_thread_dec_tmpref() - decrement thread->tmp_ref
2015 * @thread: thread to decrement
2016 *
2017 * A thread needs to be kept alive while being used to create or
2018 * handle a transaction. binder_get_txn_from() is used to safely
2019 * extract t->from from a binder_transaction and keep the thread
2020 * indicated by t->from from being freed. When done with that
2021 * binder_thread, this function is called to decrement the
2022 * tmp_ref and free if appropriate (thread has been released
2023 * and no transaction being processed by the driver)
2024 */
2025static void binder_thread_dec_tmpref(struct binder_thread *thread)
2026{
2027 /*
2028 * atomic is used to protect the counter value while
2029 * it cannot reach zero or thread->is_dead is false
Todd Kjos2f993e22017-05-12 14:42:55 -07002030 */
Todd Kjosb4827902017-05-25 15:52:17 -07002031 binder_inner_proc_lock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002032 atomic_dec(&thread->tmp_ref);
2033 if (thread->is_dead && !atomic_read(&thread->tmp_ref)) {
Todd Kjosb4827902017-05-25 15:52:17 -07002034 binder_inner_proc_unlock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002035 binder_free_thread(thread);
2036 return;
2037 }
Todd Kjosb4827902017-05-25 15:52:17 -07002038 binder_inner_proc_unlock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002039}
2040
2041/**
2042 * binder_proc_dec_tmpref() - decrement proc->tmp_ref
2043 * @proc: proc to decrement
2044 *
2045 * A binder_proc needs to be kept alive while being used to create or
2046 * handle a transaction. proc->tmp_ref is incremented when
2047 * creating a new transaction or the binder_proc is currently in-use
2048 * by threads that are being released. When done with the binder_proc,
2049 * this function is called to decrement the counter and free the
2050 * proc if appropriate (proc has been released, all threads have
2051 * been released and not currenly in-use to process a transaction).
2052 */
2053static void binder_proc_dec_tmpref(struct binder_proc *proc)
2054{
Todd Kjosb4827902017-05-25 15:52:17 -07002055 binder_inner_proc_lock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002056 proc->tmp_ref--;
2057 if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) &&
2058 !proc->tmp_ref) {
Todd Kjosb4827902017-05-25 15:52:17 -07002059 binder_inner_proc_unlock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002060 binder_free_proc(proc);
2061 return;
2062 }
Todd Kjosb4827902017-05-25 15:52:17 -07002063 binder_inner_proc_unlock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002064}
2065
2066/**
2067 * binder_get_txn_from() - safely extract the "from" thread in transaction
2068 * @t: binder transaction for t->from
2069 *
2070 * Atomically return the "from" thread and increment the tmp_ref
2071 * count for the thread to ensure it stays alive until
2072 * binder_thread_dec_tmpref() is called.
2073 *
2074 * Return: the value of t->from
2075 */
2076static struct binder_thread *binder_get_txn_from(
2077 struct binder_transaction *t)
2078{
2079 struct binder_thread *from;
2080
2081 spin_lock(&t->lock);
2082 from = t->from;
2083 if (from)
2084 atomic_inc(&from->tmp_ref);
2085 spin_unlock(&t->lock);
2086 return from;
2087}
2088
Martijn Coenen995a36e2017-06-02 13:36:52 -07002089/**
2090 * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock
2091 * @t: binder transaction for t->from
2092 *
2093 * Same as binder_get_txn_from() except it also acquires the proc->inner_lock
2094 * to guarantee that the thread cannot be released while operating on it.
2095 * The caller must call binder_inner_proc_unlock() to release the inner lock
2096 * as well as call binder_dec_thread_txn() to release the reference.
2097 *
2098 * Return: the value of t->from
2099 */
2100static struct binder_thread *binder_get_txn_from_and_acq_inner(
2101 struct binder_transaction *t)
2102{
2103 struct binder_thread *from;
2104
2105 from = binder_get_txn_from(t);
2106 if (!from)
2107 return NULL;
2108 binder_inner_proc_lock(from->proc);
2109 if (t->from) {
2110 BUG_ON(from != t->from);
2111 return from;
2112 }
2113 binder_inner_proc_unlock(from->proc);
2114 binder_thread_dec_tmpref(from);
2115 return NULL;
2116}
2117
Todd Kjos21ef40a2017-03-30 18:02:13 -07002118static void binder_free_transaction(struct binder_transaction *t)
2119{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002120 if (t->buffer)
2121 t->buffer->transaction = NULL;
2122 kfree(t);
2123 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2124}
2125
2126static void binder_send_failed_reply(struct binder_transaction *t,
2127 uint32_t error_code)
2128{
2129 struct binder_thread *target_thread;
Lucas Tanured4ec15e2014-07-13 21:31:05 -03002130 struct binder_transaction *next;
Seunghun Lee10f62862014-05-01 01:30:23 +09002131
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002132 BUG_ON(t->flags & TF_ONE_WAY);
2133 while (1) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07002134 target_thread = binder_get_txn_from_and_acq_inner(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002135 if (target_thread) {
Todd Kjos858b8da2017-04-21 17:35:12 -07002136 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2137 "send failed reply for transaction %d to %d:%d\n",
2138 t->debug_id,
2139 target_thread->proc->pid,
2140 target_thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002141
Martijn Coenen995a36e2017-06-02 13:36:52 -07002142 binder_pop_transaction_ilocked(target_thread, t);
Todd Kjos858b8da2017-04-21 17:35:12 -07002143 if (target_thread->reply_error.cmd == BR_OK) {
2144 target_thread->reply_error.cmd = error_code;
Martijn Coenen1af61802017-10-19 15:04:46 +02002145 binder_enqueue_thread_work_ilocked(
2146 target_thread,
2147 &target_thread->reply_error.work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002148 wake_up_interruptible(&target_thread->wait);
2149 } else {
Todd Kjosd3a2afb2018-02-07 12:38:47 -08002150 /*
2151 * Cannot get here for normal operation, but
2152 * we can if multiple synchronous transactions
2153 * are sent without blocking for responses.
2154 * Just ignore the 2nd error in this case.
2155 */
2156 pr_warn("Unexpected reply error: %u\n",
2157 target_thread->reply_error.cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002158 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07002159 binder_inner_proc_unlock(target_thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002160 binder_thread_dec_tmpref(target_thread);
Todd Kjos858b8da2017-04-21 17:35:12 -07002161 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002162 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002163 }
Lucas Tanured4ec15e2014-07-13 21:31:05 -03002164 next = t->from_parent;
2165
2166 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2167 "send failed reply for transaction %d, target dead\n",
2168 t->debug_id);
2169
Todd Kjos21ef40a2017-03-30 18:02:13 -07002170 binder_free_transaction(t);
Lucas Tanured4ec15e2014-07-13 21:31:05 -03002171 if (next == NULL) {
2172 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2173 "reply failed, no target thread at root\n");
2174 return;
2175 }
2176 t = next;
2177 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2178 "reply failed, no target thread -- retry %d\n",
2179 t->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002180 }
2181}
2182
Martijn Coenen00c80372016-07-13 12:06:49 +02002183/**
Martijn Coenen3217ccc2017-08-24 15:23:36 +02002184 * binder_cleanup_transaction() - cleans up undelivered transaction
2185 * @t: transaction that needs to be cleaned up
2186 * @reason: reason the transaction wasn't delivered
2187 * @error_code: error to return to caller (if synchronous call)
2188 */
2189static void binder_cleanup_transaction(struct binder_transaction *t,
2190 const char *reason,
2191 uint32_t error_code)
2192{
2193 if (t->buffer->target_node && !(t->flags & TF_ONE_WAY)) {
2194 binder_send_failed_reply(t, error_code);
2195 } else {
2196 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2197 "undelivered transaction %d, %s\n",
2198 t->debug_id, reason);
2199 binder_free_transaction(t);
2200 }
2201}
2202
2203/**
Martijn Coenen00c80372016-07-13 12:06:49 +02002204 * binder_validate_object() - checks for a valid metadata object in a buffer.
2205 * @buffer: binder_buffer that we're parsing.
2206 * @offset: offset in the buffer at which to validate an object.
2207 *
2208 * Return: If there's a valid metadata object at @offset in @buffer, the
2209 * size of that object. Otherwise, it returns zero.
2210 */
2211static size_t binder_validate_object(struct binder_buffer *buffer, u64 offset)
2212{
2213 /* Check if we can read a header first */
2214 struct binder_object_header *hdr;
2215 size_t object_size = 0;
2216
2217 if (offset > buffer->data_size - sizeof(*hdr) ||
2218 buffer->data_size < sizeof(*hdr) ||
2219 !IS_ALIGNED(offset, sizeof(u32)))
2220 return 0;
2221
2222 /* Ok, now see if we can read a complete object. */
2223 hdr = (struct binder_object_header *)(buffer->data + offset);
2224 switch (hdr->type) {
2225 case BINDER_TYPE_BINDER:
2226 case BINDER_TYPE_WEAK_BINDER:
2227 case BINDER_TYPE_HANDLE:
2228 case BINDER_TYPE_WEAK_HANDLE:
2229 object_size = sizeof(struct flat_binder_object);
2230 break;
2231 case BINDER_TYPE_FD:
2232 object_size = sizeof(struct binder_fd_object);
2233 break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002234 case BINDER_TYPE_PTR:
2235 object_size = sizeof(struct binder_buffer_object);
2236 break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002237 case BINDER_TYPE_FDA:
2238 object_size = sizeof(struct binder_fd_array_object);
2239 break;
Martijn Coenen00c80372016-07-13 12:06:49 +02002240 default:
2241 return 0;
2242 }
2243 if (offset <= buffer->data_size - object_size &&
2244 buffer->data_size >= object_size)
2245 return object_size;
2246 else
2247 return 0;
2248}
2249
Martijn Coenen5a6da532016-09-30 14:10:07 +02002250/**
2251 * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
2252 * @b: binder_buffer containing the object
2253 * @index: index in offset array at which the binder_buffer_object is
2254 * located
2255 * @start: points to the start of the offset array
2256 * @num_valid: the number of valid offsets in the offset array
2257 *
2258 * Return: If @index is within the valid range of the offset array
2259 * described by @start and @num_valid, and if there's a valid
2260 * binder_buffer_object at the offset found in index @index
2261 * of the offset array, that object is returned. Otherwise,
2262 * %NULL is returned.
2263 * Note that the offset found in index @index itself is not
2264 * verified; this function assumes that @num_valid elements
2265 * from @start were previously verified to have valid offsets.
2266 */
2267static struct binder_buffer_object *binder_validate_ptr(struct binder_buffer *b,
2268 binder_size_t index,
2269 binder_size_t *start,
2270 binder_size_t num_valid)
2271{
2272 struct binder_buffer_object *buffer_obj;
2273 binder_size_t *offp;
2274
2275 if (index >= num_valid)
2276 return NULL;
2277
2278 offp = start + index;
2279 buffer_obj = (struct binder_buffer_object *)(b->data + *offp);
2280 if (buffer_obj->hdr.type != BINDER_TYPE_PTR)
2281 return NULL;
2282
2283 return buffer_obj;
2284}
2285
2286/**
2287 * binder_validate_fixup() - validates pointer/fd fixups happen in order.
2288 * @b: transaction buffer
2289 * @objects_start start of objects buffer
2290 * @buffer: binder_buffer_object in which to fix up
2291 * @offset: start offset in @buffer to fix up
2292 * @last_obj: last binder_buffer_object that we fixed up in
2293 * @last_min_offset: minimum fixup offset in @last_obj
2294 *
2295 * Return: %true if a fixup in buffer @buffer at offset @offset is
2296 * allowed.
2297 *
2298 * For safety reasons, we only allow fixups inside a buffer to happen
2299 * at increasing offsets; additionally, we only allow fixup on the last
2300 * buffer object that was verified, or one of its parents.
2301 *
2302 * Example of what is allowed:
2303 *
2304 * A
2305 * B (parent = A, offset = 0)
2306 * C (parent = A, offset = 16)
2307 * D (parent = C, offset = 0)
2308 * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
2309 *
2310 * Examples of what is not allowed:
2311 *
2312 * Decreasing offsets within the same parent:
2313 * A
2314 * C (parent = A, offset = 16)
2315 * B (parent = A, offset = 0) // decreasing offset within A
2316 *
2317 * Referring to a parent that wasn't the last object or any of its parents:
2318 * A
2319 * B (parent = A, offset = 0)
2320 * C (parent = A, offset = 0)
2321 * C (parent = A, offset = 16)
2322 * D (parent = B, offset = 0) // B is not A or any of A's parents
2323 */
2324static bool binder_validate_fixup(struct binder_buffer *b,
2325 binder_size_t *objects_start,
2326 struct binder_buffer_object *buffer,
2327 binder_size_t fixup_offset,
2328 struct binder_buffer_object *last_obj,
2329 binder_size_t last_min_offset)
2330{
2331 if (!last_obj) {
2332 /* Nothing to fix up in */
2333 return false;
2334 }
2335
2336 while (last_obj != buffer) {
2337 /*
2338 * Safe to retrieve the parent of last_obj, since it
2339 * was already previously verified by the driver.
2340 */
2341 if ((last_obj->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0)
2342 return false;
2343 last_min_offset = last_obj->parent_offset + sizeof(uintptr_t);
2344 last_obj = (struct binder_buffer_object *)
2345 (b->data + *(objects_start + last_obj->parent));
2346 }
2347 return (fixup_offset >= last_min_offset);
2348}
2349
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002350static void binder_transaction_buffer_release(struct binder_proc *proc,
2351 struct binder_buffer *buffer,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002352 binder_size_t *failed_at)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002353{
Martijn Coenen5a6da532016-09-30 14:10:07 +02002354 binder_size_t *offp, *off_start, *off_end;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002355 int debug_id = buffer->debug_id;
2356
2357 binder_debug(BINDER_DEBUG_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302358 "%d buffer release %d, size %zd-%zd, failed at %p\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002359 proc->pid, buffer->debug_id,
2360 buffer->data_size, buffer->offsets_size, failed_at);
2361
2362 if (buffer->target_node)
2363 binder_dec_node(buffer->target_node, 1, 0);
2364
Martijn Coenen5a6da532016-09-30 14:10:07 +02002365 off_start = (binder_size_t *)(buffer->data +
2366 ALIGN(buffer->data_size, sizeof(void *)));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002367 if (failed_at)
2368 off_end = failed_at;
2369 else
Martijn Coenen5a6da532016-09-30 14:10:07 +02002370 off_end = (void *)off_start + buffer->offsets_size;
2371 for (offp = off_start; offp < off_end; offp++) {
Martijn Coenen00c80372016-07-13 12:06:49 +02002372 struct binder_object_header *hdr;
2373 size_t object_size = binder_validate_object(buffer, *offp);
Seunghun Lee10f62862014-05-01 01:30:23 +09002374
Martijn Coenen00c80372016-07-13 12:06:49 +02002375 if (object_size == 0) {
2376 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
Arve Hjønnevågda498892014-02-21 14:40:26 -08002377 debug_id, (u64)*offp, buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002378 continue;
2379 }
Martijn Coenen00c80372016-07-13 12:06:49 +02002380 hdr = (struct binder_object_header *)(buffer->data + *offp);
2381 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002382 case BINDER_TYPE_BINDER:
2383 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenen00c80372016-07-13 12:06:49 +02002384 struct flat_binder_object *fp;
2385 struct binder_node *node;
Seunghun Lee10f62862014-05-01 01:30:23 +09002386
Martijn Coenen00c80372016-07-13 12:06:49 +02002387 fp = to_flat_binder_object(hdr);
2388 node = binder_get_node(proc, fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002389 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002390 pr_err("transaction release %d bad node %016llx\n",
2391 debug_id, (u64)fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002392 break;
2393 }
2394 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002395 " node %d u%016llx\n",
2396 node->debug_id, (u64)node->ptr);
Martijn Coenen00c80372016-07-13 12:06:49 +02002397 binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER,
2398 0);
Todd Kjosf22abc72017-05-09 11:08:05 -07002399 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002400 } break;
2401 case BINDER_TYPE_HANDLE:
2402 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenen00c80372016-07-13 12:06:49 +02002403 struct flat_binder_object *fp;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002404 struct binder_ref_data rdata;
2405 int ret;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02002406
Martijn Coenen00c80372016-07-13 12:06:49 +02002407 fp = to_flat_binder_object(hdr);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002408 ret = binder_dec_ref_for_handle(proc, fp->handle,
2409 hdr->type == BINDER_TYPE_HANDLE, &rdata);
2410
2411 if (ret) {
2412 pr_err("transaction release %d bad handle %d, ret = %d\n",
2413 debug_id, fp->handle, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002414 break;
2415 }
2416 binder_debug(BINDER_DEBUG_TRANSACTION,
Todd Kjosb0117bb2017-05-08 09:16:27 -07002417 " ref %d desc %d\n",
2418 rdata.debug_id, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002419 } break;
2420
Martijn Coenen00c80372016-07-13 12:06:49 +02002421 case BINDER_TYPE_FD: {
2422 struct binder_fd_object *fp = to_binder_fd_object(hdr);
2423
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002424 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen00c80372016-07-13 12:06:49 +02002425 " fd %d\n", fp->fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002426 if (failed_at)
Martijn Coenen00c80372016-07-13 12:06:49 +02002427 task_close_fd(proc, fp->fd);
2428 } break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002429 case BINDER_TYPE_PTR:
2430 /*
2431 * Nothing to do here, this will get cleaned up when the
2432 * transaction buffer gets freed
2433 */
2434 break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002435 case BINDER_TYPE_FDA: {
2436 struct binder_fd_array_object *fda;
2437 struct binder_buffer_object *parent;
2438 uintptr_t parent_buffer;
2439 u32 *fd_array;
2440 size_t fd_index;
2441 binder_size_t fd_buf_size;
2442
2443 fda = to_binder_fd_array_object(hdr);
2444 parent = binder_validate_ptr(buffer, fda->parent,
2445 off_start,
2446 offp - off_start);
2447 if (!parent) {
2448 pr_err("transaction release %d bad parent offset",
2449 debug_id);
2450 continue;
2451 }
2452 /*
2453 * Since the parent was already fixed up, convert it
2454 * back to kernel address space to access it
2455 */
2456 parent_buffer = parent->buffer -
Todd Kjosd325d372016-10-10 10:40:53 -07002457 binder_alloc_get_user_buffer_offset(
2458 &proc->alloc);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002459
2460 fd_buf_size = sizeof(u32) * fda->num_fds;
2461 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2462 pr_err("transaction release %d invalid number of fds (%lld)\n",
2463 debug_id, (u64)fda->num_fds);
2464 continue;
2465 }
2466 if (fd_buf_size > parent->length ||
2467 fda->parent_offset > parent->length - fd_buf_size) {
2468 /* No space for all file descriptors here. */
2469 pr_err("transaction release %d not enough space for %lld fds in buffer\n",
2470 debug_id, (u64)fda->num_fds);
2471 continue;
2472 }
Arnd Bergmanne312c3f2017-09-05 10:56:13 +02002473 fd_array = (u32 *)(parent_buffer + (uintptr_t)fda->parent_offset);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002474 for (fd_index = 0; fd_index < fda->num_fds; fd_index++)
2475 task_close_fd(proc, fd_array[fd_index]);
2476 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002477 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01002478 pr_err("transaction release %d bad object type %x\n",
Martijn Coenen00c80372016-07-13 12:06:49 +02002479 debug_id, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002480 break;
2481 }
2482 }
2483}
2484
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002485static int binder_translate_binder(struct flat_binder_object *fp,
2486 struct binder_transaction *t,
2487 struct binder_thread *thread)
2488{
2489 struct binder_node *node;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002490 struct binder_proc *proc = thread->proc;
2491 struct binder_proc *target_proc = t->to_proc;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002492 struct binder_ref_data rdata;
Todd Kjosf22abc72017-05-09 11:08:05 -07002493 int ret = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002494
2495 node = binder_get_node(proc, fp->binder);
2496 if (!node) {
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002497 node = binder_new_node(proc, fp);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002498 if (!node)
2499 return -ENOMEM;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002500 }
2501 if (fp->cookie != node->cookie) {
2502 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
2503 proc->pid, thread->pid, (u64)fp->binder,
2504 node->debug_id, (u64)fp->cookie,
2505 (u64)node->cookie);
Todd Kjosf22abc72017-05-09 11:08:05 -07002506 ret = -EINVAL;
2507 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002508 }
Todd Kjosf22abc72017-05-09 11:08:05 -07002509 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2510 ret = -EPERM;
2511 goto done;
2512 }
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002513
Todd Kjosb0117bb2017-05-08 09:16:27 -07002514 ret = binder_inc_ref_for_node(target_proc, node,
2515 fp->hdr.type == BINDER_TYPE_BINDER,
2516 &thread->todo, &rdata);
2517 if (ret)
Todd Kjosf22abc72017-05-09 11:08:05 -07002518 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002519
2520 if (fp->hdr.type == BINDER_TYPE_BINDER)
2521 fp->hdr.type = BINDER_TYPE_HANDLE;
2522 else
2523 fp->hdr.type = BINDER_TYPE_WEAK_HANDLE;
2524 fp->binder = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002525 fp->handle = rdata.desc;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002526 fp->cookie = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002527
Todd Kjosb0117bb2017-05-08 09:16:27 -07002528 trace_binder_transaction_node_to_ref(t, node, &rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002529 binder_debug(BINDER_DEBUG_TRANSACTION,
2530 " node %d u%016llx -> ref %d desc %d\n",
2531 node->debug_id, (u64)node->ptr,
Todd Kjosb0117bb2017-05-08 09:16:27 -07002532 rdata.debug_id, rdata.desc);
Todd Kjosf22abc72017-05-09 11:08:05 -07002533done:
2534 binder_put_node(node);
2535 return ret;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002536}
2537
2538static int binder_translate_handle(struct flat_binder_object *fp,
2539 struct binder_transaction *t,
2540 struct binder_thread *thread)
2541{
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002542 struct binder_proc *proc = thread->proc;
2543 struct binder_proc *target_proc = t->to_proc;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002544 struct binder_node *node;
2545 struct binder_ref_data src_rdata;
Todd Kjosf22abc72017-05-09 11:08:05 -07002546 int ret = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002547
Todd Kjosb0117bb2017-05-08 09:16:27 -07002548 node = binder_get_node_from_ref(proc, fp->handle,
2549 fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata);
2550 if (!node) {
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002551 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
2552 proc->pid, thread->pid, fp->handle);
2553 return -EINVAL;
2554 }
Todd Kjosf22abc72017-05-09 11:08:05 -07002555 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2556 ret = -EPERM;
2557 goto done;
2558 }
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002559
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002560 binder_node_lock(node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002561 if (node->proc == target_proc) {
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002562 if (fp->hdr.type == BINDER_TYPE_HANDLE)
2563 fp->hdr.type = BINDER_TYPE_BINDER;
2564 else
2565 fp->hdr.type = BINDER_TYPE_WEAK_BINDER;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002566 fp->binder = node->ptr;
2567 fp->cookie = node->cookie;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002568 if (node->proc)
2569 binder_inner_proc_lock(node->proc);
2570 binder_inc_node_nilocked(node,
2571 fp->hdr.type == BINDER_TYPE_BINDER,
2572 0, NULL);
2573 if (node->proc)
2574 binder_inner_proc_unlock(node->proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002575 trace_binder_transaction_ref_to_node(t, node, &src_rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002576 binder_debug(BINDER_DEBUG_TRANSACTION,
2577 " ref %d desc %d -> node %d u%016llx\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07002578 src_rdata.debug_id, src_rdata.desc, node->debug_id,
2579 (u64)node->ptr);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002580 binder_node_unlock(node);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002581 } else {
Todd Kjosb0117bb2017-05-08 09:16:27 -07002582 struct binder_ref_data dest_rdata;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002583
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002584 binder_node_unlock(node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002585 ret = binder_inc_ref_for_node(target_proc, node,
2586 fp->hdr.type == BINDER_TYPE_HANDLE,
2587 NULL, &dest_rdata);
2588 if (ret)
Todd Kjosf22abc72017-05-09 11:08:05 -07002589 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002590
2591 fp->binder = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002592 fp->handle = dest_rdata.desc;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002593 fp->cookie = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002594 trace_binder_transaction_ref_to_ref(t, node, &src_rdata,
2595 &dest_rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002596 binder_debug(BINDER_DEBUG_TRANSACTION,
2597 " ref %d desc %d -> ref %d desc %d (node %d)\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07002598 src_rdata.debug_id, src_rdata.desc,
2599 dest_rdata.debug_id, dest_rdata.desc,
2600 node->debug_id);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002601 }
Todd Kjosf22abc72017-05-09 11:08:05 -07002602done:
2603 binder_put_node(node);
2604 return ret;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002605}
2606
2607static int binder_translate_fd(int fd,
2608 struct binder_transaction *t,
2609 struct binder_thread *thread,
2610 struct binder_transaction *in_reply_to)
2611{
2612 struct binder_proc *proc = thread->proc;
2613 struct binder_proc *target_proc = t->to_proc;
2614 int target_fd;
2615 struct file *file;
2616 int ret;
2617 bool target_allows_fd;
2618
2619 if (in_reply_to)
2620 target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS);
2621 else
2622 target_allows_fd = t->buffer->target_node->accept_fds;
2623 if (!target_allows_fd) {
2624 binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
2625 proc->pid, thread->pid,
2626 in_reply_to ? "reply" : "transaction",
2627 fd);
2628 ret = -EPERM;
2629 goto err_fd_not_accepted;
2630 }
2631
2632 file = fget(fd);
2633 if (!file) {
2634 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
2635 proc->pid, thread->pid, fd);
2636 ret = -EBADF;
2637 goto err_fget;
2638 }
2639 ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file);
2640 if (ret < 0) {
2641 ret = -EPERM;
2642 goto err_security;
2643 }
2644
2645 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
2646 if (target_fd < 0) {
2647 ret = -ENOMEM;
2648 goto err_get_unused_fd;
2649 }
2650 task_fd_install(target_proc, target_fd, file);
2651 trace_binder_transaction_fd(t, fd, target_fd);
2652 binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n",
2653 fd, target_fd);
2654
2655 return target_fd;
2656
2657err_get_unused_fd:
2658err_security:
2659 fput(file);
2660err_fget:
2661err_fd_not_accepted:
2662 return ret;
2663}
2664
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002665static int binder_translate_fd_array(struct binder_fd_array_object *fda,
2666 struct binder_buffer_object *parent,
2667 struct binder_transaction *t,
2668 struct binder_thread *thread,
2669 struct binder_transaction *in_reply_to)
2670{
2671 binder_size_t fdi, fd_buf_size, num_installed_fds;
2672 int target_fd;
2673 uintptr_t parent_buffer;
2674 u32 *fd_array;
2675 struct binder_proc *proc = thread->proc;
2676 struct binder_proc *target_proc = t->to_proc;
2677
2678 fd_buf_size = sizeof(u32) * fda->num_fds;
2679 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2680 binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
2681 proc->pid, thread->pid, (u64)fda->num_fds);
2682 return -EINVAL;
2683 }
2684 if (fd_buf_size > parent->length ||
2685 fda->parent_offset > parent->length - fd_buf_size) {
2686 /* No space for all file descriptors here. */
2687 binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
2688 proc->pid, thread->pid, (u64)fda->num_fds);
2689 return -EINVAL;
2690 }
2691 /*
2692 * Since the parent was already fixed up, convert it
2693 * back to the kernel address space to access it
2694 */
Todd Kjosd325d372016-10-10 10:40:53 -07002695 parent_buffer = parent->buffer -
2696 binder_alloc_get_user_buffer_offset(&target_proc->alloc);
Arnd Bergmanne312c3f2017-09-05 10:56:13 +02002697 fd_array = (u32 *)(parent_buffer + (uintptr_t)fda->parent_offset);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002698 if (!IS_ALIGNED((unsigned long)fd_array, sizeof(u32))) {
2699 binder_user_error("%d:%d parent offset not aligned correctly.\n",
2700 proc->pid, thread->pid);
2701 return -EINVAL;
2702 }
2703 for (fdi = 0; fdi < fda->num_fds; fdi++) {
2704 target_fd = binder_translate_fd(fd_array[fdi], t, thread,
2705 in_reply_to);
2706 if (target_fd < 0)
2707 goto err_translate_fd_failed;
2708 fd_array[fdi] = target_fd;
2709 }
2710 return 0;
2711
2712err_translate_fd_failed:
2713 /*
2714 * Failed to allocate fd or security error, free fds
2715 * installed so far.
2716 */
2717 num_installed_fds = fdi;
2718 for (fdi = 0; fdi < num_installed_fds; fdi++)
2719 task_close_fd(target_proc, fd_array[fdi]);
2720 return target_fd;
2721}
2722
Martijn Coenen5a6da532016-09-30 14:10:07 +02002723static int binder_fixup_parent(struct binder_transaction *t,
2724 struct binder_thread *thread,
2725 struct binder_buffer_object *bp,
2726 binder_size_t *off_start,
2727 binder_size_t num_valid,
2728 struct binder_buffer_object *last_fixup_obj,
2729 binder_size_t last_fixup_min_off)
2730{
2731 struct binder_buffer_object *parent;
2732 u8 *parent_buffer;
2733 struct binder_buffer *b = t->buffer;
2734 struct binder_proc *proc = thread->proc;
2735 struct binder_proc *target_proc = t->to_proc;
2736
2737 if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT))
2738 return 0;
2739
2740 parent = binder_validate_ptr(b, bp->parent, off_start, num_valid);
2741 if (!parent) {
2742 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
2743 proc->pid, thread->pid);
2744 return -EINVAL;
2745 }
2746
2747 if (!binder_validate_fixup(b, off_start,
2748 parent, bp->parent_offset,
2749 last_fixup_obj,
2750 last_fixup_min_off)) {
2751 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
2752 proc->pid, thread->pid);
2753 return -EINVAL;
2754 }
2755
2756 if (parent->length < sizeof(binder_uintptr_t) ||
2757 bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) {
2758 /* No space for a pointer here! */
2759 binder_user_error("%d:%d got transaction with invalid parent offset\n",
2760 proc->pid, thread->pid);
2761 return -EINVAL;
2762 }
Arnd Bergmanne312c3f2017-09-05 10:56:13 +02002763 parent_buffer = (u8 *)((uintptr_t)parent->buffer -
Todd Kjosd325d372016-10-10 10:40:53 -07002764 binder_alloc_get_user_buffer_offset(
2765 &target_proc->alloc));
Martijn Coenen5a6da532016-09-30 14:10:07 +02002766 *(binder_uintptr_t *)(parent_buffer + bp->parent_offset) = bp->buffer;
2767
2768 return 0;
2769}
2770
Martijn Coenen053be422017-06-06 15:17:46 -07002771/**
2772 * binder_proc_transaction() - sends a transaction to a process and wakes it up
2773 * @t: transaction to send
2774 * @proc: process to send the transaction to
2775 * @thread: thread in @proc to send the transaction to (may be NULL)
2776 *
2777 * This function queues a transaction to the specified process. It will try
2778 * to find a thread in the target process to handle the transaction and
2779 * wake it up. If no thread is found, the work is queued to the proc
2780 * waitqueue.
2781 *
2782 * If the @thread parameter is not NULL, the transaction is always queued
2783 * to the waitlist of that specific thread.
2784 *
2785 * Return: true if the transactions was successfully queued
2786 * false if the target process or thread is dead
2787 */
2788static bool binder_proc_transaction(struct binder_transaction *t,
2789 struct binder_proc *proc,
2790 struct binder_thread *thread)
2791{
Martijn Coenen053be422017-06-06 15:17:46 -07002792 struct binder_node *node = t->buffer->target_node;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002793 struct binder_priority node_prio;
Martijn Coenen053be422017-06-06 15:17:46 -07002794 bool oneway = !!(t->flags & TF_ONE_WAY);
Martijn Coenen1af61802017-10-19 15:04:46 +02002795 bool pending_async = false;
Martijn Coenen053be422017-06-06 15:17:46 -07002796
2797 BUG_ON(!node);
2798 binder_node_lock(node);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002799 node_prio.prio = node->min_priority;
2800 node_prio.sched_policy = node->sched_policy;
2801
Martijn Coenen053be422017-06-06 15:17:46 -07002802 if (oneway) {
2803 BUG_ON(thread);
2804 if (node->has_async_transaction) {
Martijn Coenen1af61802017-10-19 15:04:46 +02002805 pending_async = true;
Martijn Coenen053be422017-06-06 15:17:46 -07002806 } else {
2807 node->has_async_transaction = 1;
2808 }
2809 }
2810
2811 binder_inner_proc_lock(proc);
2812
2813 if (proc->is_dead || (thread && thread->is_dead)) {
2814 binder_inner_proc_unlock(proc);
2815 binder_node_unlock(node);
2816 return false;
2817 }
2818
Martijn Coenen1af61802017-10-19 15:04:46 +02002819 if (!thread && !pending_async)
Martijn Coenen053be422017-06-06 15:17:46 -07002820 thread = binder_select_thread_ilocked(proc);
2821
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002822 if (thread) {
Martijn Coenenc46810c2017-06-23 10:13:43 -07002823 binder_transaction_priority(thread->task, t, node_prio,
2824 node->inherit_rt);
Martijn Coenen1af61802017-10-19 15:04:46 +02002825 binder_enqueue_thread_work_ilocked(thread, &t->work);
2826 } else if (!pending_async) {
2827 binder_enqueue_work_ilocked(&t->work, &proc->todo);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002828 } else {
Martijn Coenen1af61802017-10-19 15:04:46 +02002829 binder_enqueue_work_ilocked(&t->work, &node->async_todo);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002830 }
Martijn Coenen053be422017-06-06 15:17:46 -07002831
Martijn Coenen1af61802017-10-19 15:04:46 +02002832 if (!pending_async)
Martijn Coenen053be422017-06-06 15:17:46 -07002833 binder_wakeup_thread_ilocked(proc, thread, !oneway /* sync */);
2834
2835 binder_inner_proc_unlock(proc);
2836 binder_node_unlock(node);
2837
2838 return true;
2839}
2840
Todd Kjos291d9682017-09-25 08:55:09 -07002841/**
2842 * binder_get_node_refs_for_txn() - Get required refs on node for txn
2843 * @node: struct binder_node for which to get refs
2844 * @proc: returns @node->proc if valid
2845 * @error: if no @proc then returns BR_DEAD_REPLY
2846 *
2847 * User-space normally keeps the node alive when creating a transaction
2848 * since it has a reference to the target. The local strong ref keeps it
2849 * alive if the sending process dies before the target process processes
2850 * the transaction. If the source process is malicious or has a reference
2851 * counting bug, relying on the local strong ref can fail.
2852 *
2853 * Since user-space can cause the local strong ref to go away, we also take
2854 * a tmpref on the node to ensure it survives while we are constructing
2855 * the transaction. We also need a tmpref on the proc while we are
2856 * constructing the transaction, so we take that here as well.
2857 *
2858 * Return: The target_node with refs taken or NULL if no @node->proc is NULL.
2859 * Also sets @proc if valid. If the @node->proc is NULL indicating that the
2860 * target proc has died, @error is set to BR_DEAD_REPLY
2861 */
2862static struct binder_node *binder_get_node_refs_for_txn(
2863 struct binder_node *node,
2864 struct binder_proc **procp,
2865 uint32_t *error)
2866{
2867 struct binder_node *target_node = NULL;
2868
2869 binder_node_inner_lock(node);
2870 if (node->proc) {
2871 target_node = node;
2872 binder_inc_node_nilocked(node, 1, 0, NULL);
2873 binder_inc_node_tmpref_ilocked(node);
2874 node->proc->tmp_ref++;
2875 *procp = node->proc;
2876 } else
2877 *error = BR_DEAD_REPLY;
2878 binder_node_inner_unlock(node);
2879
2880 return target_node;
2881}
2882
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002883static void binder_transaction(struct binder_proc *proc,
2884 struct binder_thread *thread,
Martijn Coenen59878d72016-09-30 14:05:40 +02002885 struct binder_transaction_data *tr, int reply,
2886 binder_size_t extra_buffers_size)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002887{
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002888 int ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002889 struct binder_transaction *t;
2890 struct binder_work *tcomplete;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002891 binder_size_t *offp, *off_end, *off_start;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08002892 binder_size_t off_min;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002893 u8 *sg_bufp, *sg_buf_end;
Todd Kjos2f993e22017-05-12 14:42:55 -07002894 struct binder_proc *target_proc = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002895 struct binder_thread *target_thread = NULL;
2896 struct binder_node *target_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002897 struct binder_transaction *in_reply_to = NULL;
2898 struct binder_transaction_log_entry *e;
Todd Kjose598d172017-03-22 17:19:52 -07002899 uint32_t return_error = 0;
2900 uint32_t return_error_param = 0;
2901 uint32_t return_error_line = 0;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002902 struct binder_buffer_object *last_fixup_obj = NULL;
2903 binder_size_t last_fixup_min_off = 0;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02002904 struct binder_context *context = proc->context;
Todd Kjos1cfe6272017-05-24 13:33:28 -07002905 int t_debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002906
2907 e = binder_transaction_log_add(&binder_transaction_log);
Todd Kjos1cfe6272017-05-24 13:33:28 -07002908 e->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002909 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
2910 e->from_proc = proc->pid;
2911 e->from_thread = thread->pid;
2912 e->target_handle = tr->target.handle;
2913 e->data_size = tr->data_size;
2914 e->offsets_size = tr->offsets_size;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02002915 e->context_name = proc->context->name;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002916
2917 if (reply) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07002918 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002919 in_reply_to = thread->transaction_stack;
2920 if (in_reply_to == NULL) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07002921 binder_inner_proc_unlock(proc);
Anmol Sarma56b468f2012-10-30 22:35:43 +05302922 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002923 proc->pid, thread->pid);
2924 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002925 return_error_param = -EPROTO;
2926 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002927 goto err_empty_call_stack;
2928 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002929 if (in_reply_to->to_thread != thread) {
Todd Kjos2f993e22017-05-12 14:42:55 -07002930 spin_lock(&in_reply_to->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05302931 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 +09002932 proc->pid, thread->pid, in_reply_to->debug_id,
2933 in_reply_to->to_proc ?
2934 in_reply_to->to_proc->pid : 0,
2935 in_reply_to->to_thread ?
2936 in_reply_to->to_thread->pid : 0);
Todd Kjos2f993e22017-05-12 14:42:55 -07002937 spin_unlock(&in_reply_to->lock);
Martijn Coenen995a36e2017-06-02 13:36:52 -07002938 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002939 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002940 return_error_param = -EPROTO;
2941 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002942 in_reply_to = NULL;
2943 goto err_bad_call_stack;
2944 }
2945 thread->transaction_stack = in_reply_to->to_parent;
Martijn Coenen995a36e2017-06-02 13:36:52 -07002946 binder_inner_proc_unlock(proc);
Martijn Coenen995a36e2017-06-02 13:36:52 -07002947 target_thread = binder_get_txn_from_and_acq_inner(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002948 if (target_thread == NULL) {
2949 return_error = BR_DEAD_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002950 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002951 goto err_dead_binder;
2952 }
2953 if (target_thread->transaction_stack != in_reply_to) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302954 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 +09002955 proc->pid, thread->pid,
2956 target_thread->transaction_stack ?
2957 target_thread->transaction_stack->debug_id : 0,
2958 in_reply_to->debug_id);
Martijn Coenen995a36e2017-06-02 13:36:52 -07002959 binder_inner_proc_unlock(target_thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002960 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002961 return_error_param = -EPROTO;
2962 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002963 in_reply_to = NULL;
2964 target_thread = NULL;
2965 goto err_dead_binder;
2966 }
2967 target_proc = target_thread->proc;
Todd Kjos2f993e22017-05-12 14:42:55 -07002968 target_proc->tmp_ref++;
Martijn Coenen995a36e2017-06-02 13:36:52 -07002969 binder_inner_proc_unlock(target_thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002970 } else {
2971 if (tr->target.handle) {
2972 struct binder_ref *ref;
Seunghun Lee10f62862014-05-01 01:30:23 +09002973
Todd Kjosc37162d2017-05-26 11:56:29 -07002974 /*
2975 * There must already be a strong ref
2976 * on this node. If so, do a strong
2977 * increment on the node to ensure it
2978 * stays alive until the transaction is
2979 * done.
2980 */
Todd Kjos5346bf32016-10-20 16:43:34 -07002981 binder_proc_lock(proc);
2982 ref = binder_get_ref_olocked(proc, tr->target.handle,
2983 true);
Todd Kjosc37162d2017-05-26 11:56:29 -07002984 if (ref) {
Todd Kjos291d9682017-09-25 08:55:09 -07002985 target_node = binder_get_node_refs_for_txn(
2986 ref->node, &target_proc,
2987 &return_error);
2988 } else {
2989 binder_user_error("%d:%d got transaction to invalid handle\n",
2990 proc->pid, thread->pid);
2991 return_error = BR_FAILED_REPLY;
Todd Kjosc37162d2017-05-26 11:56:29 -07002992 }
Todd Kjos5346bf32016-10-20 16:43:34 -07002993 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002994 } else {
Todd Kjos8d9f6f32016-10-17 12:33:15 -07002995 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02002996 target_node = context->binder_context_mgr_node;
Todd Kjos291d9682017-09-25 08:55:09 -07002997 if (target_node)
2998 target_node = binder_get_node_refs_for_txn(
2999 target_node, &target_proc,
3000 &return_error);
3001 else
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003002 return_error = BR_DEAD_REPLY;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003003 mutex_unlock(&context->context_mgr_node_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003004 }
Todd Kjos291d9682017-09-25 08:55:09 -07003005 if (!target_node) {
3006 /*
3007 * return_error is set above
3008 */
3009 return_error_param = -EINVAL;
Todd Kjose598d172017-03-22 17:19:52 -07003010 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003011 goto err_dead_binder;
3012 }
Todd Kjos291d9682017-09-25 08:55:09 -07003013 e->to_node = target_node->debug_id;
Stephen Smalley79af7302015-01-21 10:54:10 -05003014 if (security_binder_transaction(proc->tsk,
3015 target_proc->tsk) < 0) {
3016 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003017 return_error_param = -EPERM;
3018 return_error_line = __LINE__;
Stephen Smalley79af7302015-01-21 10:54:10 -05003019 goto err_invalid_target_handle;
3020 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07003021 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003022 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
3023 struct binder_transaction *tmp;
Seunghun Lee10f62862014-05-01 01:30:23 +09003024
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003025 tmp = thread->transaction_stack;
3026 if (tmp->to_thread != thread) {
Todd Kjos2f993e22017-05-12 14:42:55 -07003027 spin_lock(&tmp->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05303028 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 +09003029 proc->pid, thread->pid, tmp->debug_id,
3030 tmp->to_proc ? tmp->to_proc->pid : 0,
3031 tmp->to_thread ?
3032 tmp->to_thread->pid : 0);
Todd Kjos2f993e22017-05-12 14:42:55 -07003033 spin_unlock(&tmp->lock);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003034 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003035 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003036 return_error_param = -EPROTO;
3037 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003038 goto err_bad_call_stack;
3039 }
3040 while (tmp) {
Todd Kjos2f993e22017-05-12 14:42:55 -07003041 struct binder_thread *from;
3042
3043 spin_lock(&tmp->lock);
3044 from = tmp->from;
3045 if (from && from->proc == target_proc) {
3046 atomic_inc(&from->tmp_ref);
3047 target_thread = from;
3048 spin_unlock(&tmp->lock);
3049 break;
3050 }
3051 spin_unlock(&tmp->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003052 tmp = tmp->from_parent;
3053 }
3054 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07003055 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003056 }
Martijn Coenen053be422017-06-06 15:17:46 -07003057 if (target_thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003058 e->to_thread = target_thread->pid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003059 e->to_proc = target_proc->pid;
3060
3061 /* TODO: reuse incoming transaction for reply */
3062 t = kzalloc(sizeof(*t), GFP_KERNEL);
3063 if (t == NULL) {
3064 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003065 return_error_param = -ENOMEM;
3066 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003067 goto err_alloc_t_failed;
3068 }
3069 binder_stats_created(BINDER_STAT_TRANSACTION);
Todd Kjos2f993e22017-05-12 14:42:55 -07003070 spin_lock_init(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003071
3072 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
3073 if (tcomplete == NULL) {
3074 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003075 return_error_param = -ENOMEM;
3076 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003077 goto err_alloc_tcomplete_failed;
3078 }
3079 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
3080
Todd Kjos1cfe6272017-05-24 13:33:28 -07003081 t->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003082
3083 if (reply)
3084 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen59878d72016-09-30 14:05:40 +02003085 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003086 proc->pid, thread->pid, t->debug_id,
3087 target_proc->pid, target_thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003088 (u64)tr->data.ptr.buffer,
3089 (u64)tr->data.ptr.offsets,
Martijn Coenen59878d72016-09-30 14:05:40 +02003090 (u64)tr->data_size, (u64)tr->offsets_size,
3091 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003092 else
3093 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen59878d72016-09-30 14:05:40 +02003094 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003095 proc->pid, thread->pid, t->debug_id,
3096 target_proc->pid, target_node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003097 (u64)tr->data.ptr.buffer,
3098 (u64)tr->data.ptr.offsets,
Martijn Coenen59878d72016-09-30 14:05:40 +02003099 (u64)tr->data_size, (u64)tr->offsets_size,
3100 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003101
3102 if (!reply && !(tr->flags & TF_ONE_WAY))
3103 t->from = thread;
3104 else
3105 t->from = NULL;
Tair Rzayev57bab7c2014-05-31 22:43:34 +03003106 t->sender_euid = task_euid(proc->tsk);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003107 t->to_proc = target_proc;
3108 t->to_thread = target_thread;
3109 t->code = tr->code;
3110 t->flags = tr->flags;
Martijn Coenen57b2ac62017-06-06 17:04:42 -07003111 if (!(t->flags & TF_ONE_WAY) &&
3112 binder_supported_policy(current->policy)) {
3113 /* Inherit supported policies for synchronous transactions */
3114 t->priority.sched_policy = current->policy;
3115 t->priority.prio = current->normal_prio;
3116 } else {
3117 /* Otherwise, fall back to the default priority */
3118 t->priority = target_proc->default_priority;
3119 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003120
3121 trace_binder_transaction(reply, t, target_node);
3122
Todd Kjosd325d372016-10-10 10:40:53 -07003123 t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size,
Martijn Coenen59878d72016-09-30 14:05:40 +02003124 tr->offsets_size, extra_buffers_size,
3125 !reply && (t->flags & TF_ONE_WAY));
Todd Kjose598d172017-03-22 17:19:52 -07003126 if (IS_ERR(t->buffer)) {
3127 /*
3128 * -ESRCH indicates VMA cleared. The target is dying.
3129 */
3130 return_error_param = PTR_ERR(t->buffer);
3131 return_error = return_error_param == -ESRCH ?
3132 BR_DEAD_REPLY : BR_FAILED_REPLY;
3133 return_error_line = __LINE__;
3134 t->buffer = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003135 goto err_binder_alloc_buf_failed;
3136 }
3137 t->buffer->allow_user_free = 0;
3138 t->buffer->debug_id = t->debug_id;
3139 t->buffer->transaction = t;
3140 t->buffer->target_node = target_node;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003141 trace_binder_transaction_alloc_buf(t->buffer);
Martijn Coenen5a6da532016-09-30 14:10:07 +02003142 off_start = (binder_size_t *)(t->buffer->data +
3143 ALIGN(tr->data_size, sizeof(void *)));
3144 offp = off_start;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003145
Arve Hjønnevågda498892014-02-21 14:40:26 -08003146 if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t)
3147 tr->data.ptr.buffer, tr->data_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303148 binder_user_error("%d:%d got transaction with invalid data ptr\n",
3149 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003150 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003151 return_error_param = -EFAULT;
3152 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003153 goto err_copy_data_failed;
3154 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08003155 if (copy_from_user(offp, (const void __user *)(uintptr_t)
3156 tr->data.ptr.offsets, tr->offsets_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303157 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3158 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003159 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003160 return_error_param = -EFAULT;
3161 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003162 goto err_copy_data_failed;
3163 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08003164 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
3165 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
3166 proc->pid, thread->pid, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003167 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003168 return_error_param = -EINVAL;
3169 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003170 goto err_bad_offset;
3171 }
Martijn Coenen5a6da532016-09-30 14:10:07 +02003172 if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) {
3173 binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
3174 proc->pid, thread->pid,
Amit Pundir44cbb182017-02-01 12:53:45 +05303175 (u64)extra_buffers_size);
Martijn Coenen5a6da532016-09-30 14:10:07 +02003176 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003177 return_error_param = -EINVAL;
3178 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003179 goto err_bad_offset;
3180 }
3181 off_end = (void *)off_start + tr->offsets_size;
3182 sg_bufp = (u8 *)(PTR_ALIGN(off_end, sizeof(void *)));
3183 sg_buf_end = sg_bufp + extra_buffers_size;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08003184 off_min = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003185 for (; offp < off_end; offp++) {
Martijn Coenen00c80372016-07-13 12:06:49 +02003186 struct binder_object_header *hdr;
3187 size_t object_size = binder_validate_object(t->buffer, *offp);
Seunghun Lee10f62862014-05-01 01:30:23 +09003188
Martijn Coenen00c80372016-07-13 12:06:49 +02003189 if (object_size == 0 || *offp < off_min) {
3190 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 -08003191 proc->pid, thread->pid, (u64)*offp,
3192 (u64)off_min,
Martijn Coenen00c80372016-07-13 12:06:49 +02003193 (u64)t->buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003194 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003195 return_error_param = -EINVAL;
3196 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003197 goto err_bad_offset;
3198 }
Martijn Coenen00c80372016-07-13 12:06:49 +02003199
3200 hdr = (struct binder_object_header *)(t->buffer->data + *offp);
3201 off_min = *offp + object_size;
3202 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003203 case BINDER_TYPE_BINDER:
3204 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003205 struct flat_binder_object *fp;
Seunghun Lee10f62862014-05-01 01:30:23 +09003206
Martijn Coenen00c80372016-07-13 12:06:49 +02003207 fp = to_flat_binder_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003208 ret = binder_translate_binder(fp, t, thread);
3209 if (ret < 0) {
Christian Engelmayer7d420432014-05-07 21:44:53 +02003210 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003211 return_error_param = ret;
3212 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003213 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003214 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003215 } break;
3216 case BINDER_TYPE_HANDLE:
3217 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003218 struct flat_binder_object *fp;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02003219
Martijn Coenen00c80372016-07-13 12:06:49 +02003220 fp = to_flat_binder_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003221 ret = binder_translate_handle(fp, t, thread);
3222 if (ret < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003223 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003224 return_error_param = ret;
3225 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003226 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003227 }
3228 } break;
3229
3230 case BINDER_TYPE_FD: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003231 struct binder_fd_object *fp = to_binder_fd_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003232 int target_fd = binder_translate_fd(fp->fd, t, thread,
3233 in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003234
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003235 if (target_fd < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003236 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003237 return_error_param = target_fd;
3238 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003239 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003240 }
Martijn Coenen00c80372016-07-13 12:06:49 +02003241 fp->pad_binder = 0;
3242 fp->fd = target_fd;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003243 } break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003244 case BINDER_TYPE_FDA: {
3245 struct binder_fd_array_object *fda =
3246 to_binder_fd_array_object(hdr);
3247 struct binder_buffer_object *parent =
3248 binder_validate_ptr(t->buffer, fda->parent,
3249 off_start,
3250 offp - off_start);
3251 if (!parent) {
3252 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
3253 proc->pid, thread->pid);
3254 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003255 return_error_param = -EINVAL;
3256 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003257 goto err_bad_parent;
3258 }
3259 if (!binder_validate_fixup(t->buffer, off_start,
3260 parent, fda->parent_offset,
3261 last_fixup_obj,
3262 last_fixup_min_off)) {
3263 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
3264 proc->pid, thread->pid);
3265 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003266 return_error_param = -EINVAL;
3267 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003268 goto err_bad_parent;
3269 }
3270 ret = binder_translate_fd_array(fda, parent, t, thread,
3271 in_reply_to);
3272 if (ret < 0) {
3273 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003274 return_error_param = ret;
3275 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003276 goto err_translate_failed;
3277 }
3278 last_fixup_obj = parent;
3279 last_fixup_min_off =
3280 fda->parent_offset + sizeof(u32) * fda->num_fds;
3281 } break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003282 case BINDER_TYPE_PTR: {
3283 struct binder_buffer_object *bp =
3284 to_binder_buffer_object(hdr);
3285 size_t buf_left = sg_buf_end - sg_bufp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003286
Martijn Coenen5a6da532016-09-30 14:10:07 +02003287 if (bp->length > buf_left) {
3288 binder_user_error("%d:%d got transaction with too large buffer\n",
3289 proc->pid, thread->pid);
3290 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003291 return_error_param = -EINVAL;
3292 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003293 goto err_bad_offset;
3294 }
3295 if (copy_from_user(sg_bufp,
3296 (const void __user *)(uintptr_t)
3297 bp->buffer, bp->length)) {
3298 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3299 proc->pid, thread->pid);
Todd Kjose598d172017-03-22 17:19:52 -07003300 return_error_param = -EFAULT;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003301 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003302 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003303 goto err_copy_data_failed;
3304 }
3305 /* Fixup buffer pointer to target proc address space */
3306 bp->buffer = (uintptr_t)sg_bufp +
Todd Kjosd325d372016-10-10 10:40:53 -07003307 binder_alloc_get_user_buffer_offset(
3308 &target_proc->alloc);
Martijn Coenen5a6da532016-09-30 14:10:07 +02003309 sg_bufp += ALIGN(bp->length, sizeof(u64));
3310
3311 ret = binder_fixup_parent(t, thread, bp, off_start,
3312 offp - off_start,
3313 last_fixup_obj,
3314 last_fixup_min_off);
3315 if (ret < 0) {
3316 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003317 return_error_param = ret;
3318 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003319 goto err_translate_failed;
3320 }
3321 last_fixup_obj = bp;
3322 last_fixup_min_off = 0;
3323 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003324 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01003325 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
Martijn Coenen00c80372016-07-13 12:06:49 +02003326 proc->pid, thread->pid, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003327 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003328 return_error_param = -EINVAL;
3329 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003330 goto err_bad_object_type;
3331 }
3332 }
Todd Kjos8dedb0c2017-05-09 08:31:32 -07003333 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003334 t->work.type = BINDER_WORK_TRANSACTION;
Todd Kjos8dedb0c2017-05-09 08:31:32 -07003335
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003336 if (reply) {
Martijn Coenen1af61802017-10-19 15:04:46 +02003337 binder_enqueue_thread_work(thread, tcomplete);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003338 binder_inner_proc_lock(target_proc);
3339 if (target_thread->is_dead) {
3340 binder_inner_proc_unlock(target_proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07003341 goto err_dead_proc_or_thread;
Martijn Coenen995a36e2017-06-02 13:36:52 -07003342 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003343 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003344 binder_pop_transaction_ilocked(target_thread, in_reply_to);
Martijn Coenen1af61802017-10-19 15:04:46 +02003345 binder_enqueue_thread_work_ilocked(target_thread, &t->work);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003346 binder_inner_proc_unlock(target_proc);
Martijn Coenen053be422017-06-06 15:17:46 -07003347 wake_up_interruptible_sync(&target_thread->wait);
Martijn Coenenecd972d2017-05-26 10:48:56 -07003348 binder_restore_priority(current, in_reply_to->saved_priority);
Todd Kjos21ef40a2017-03-30 18:02:13 -07003349 binder_free_transaction(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003350 } else if (!(t->flags & TF_ONE_WAY)) {
3351 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003352 binder_inner_proc_lock(proc);
Martijn Coenendac2e9c2017-11-13 09:55:21 +01003353 /*
3354 * Defer the TRANSACTION_COMPLETE, so we don't return to
3355 * userspace immediately; this allows the target process to
3356 * immediately start processing this transaction, reducing
3357 * latency. We will then return the TRANSACTION_COMPLETE when
3358 * the target replies (or there is an error).
3359 */
3360 binder_enqueue_deferred_thread_work_ilocked(thread, tcomplete);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003361 t->need_reply = 1;
3362 t->from_parent = thread->transaction_stack;
3363 thread->transaction_stack = t;
Martijn Coenen995a36e2017-06-02 13:36:52 -07003364 binder_inner_proc_unlock(proc);
Martijn Coenen053be422017-06-06 15:17:46 -07003365 if (!binder_proc_transaction(t, target_proc, target_thread)) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07003366 binder_inner_proc_lock(proc);
3367 binder_pop_transaction_ilocked(thread, t);
3368 binder_inner_proc_unlock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07003369 goto err_dead_proc_or_thread;
3370 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003371 } else {
3372 BUG_ON(target_node == NULL);
3373 BUG_ON(t->buffer->async_transaction != 1);
Martijn Coenen1af61802017-10-19 15:04:46 +02003374 binder_enqueue_thread_work(thread, tcomplete);
Martijn Coenen053be422017-06-06 15:17:46 -07003375 if (!binder_proc_transaction(t, target_proc, NULL))
Todd Kjos2f993e22017-05-12 14:42:55 -07003376 goto err_dead_proc_or_thread;
Riley Andrewsb5968812015-09-01 12:42:07 -07003377 }
Todd Kjos2f993e22017-05-12 14:42:55 -07003378 if (target_thread)
3379 binder_thread_dec_tmpref(target_thread);
3380 binder_proc_dec_tmpref(target_proc);
Todd Kjos291d9682017-09-25 08:55:09 -07003381 if (target_node)
3382 binder_dec_node_tmpref(target_node);
Todd Kjos1cfe6272017-05-24 13:33:28 -07003383 /*
3384 * write barrier to synchronize with initialization
3385 * of log entry
3386 */
3387 smp_wmb();
3388 WRITE_ONCE(e->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003389 return;
3390
Todd Kjos2f993e22017-05-12 14:42:55 -07003391err_dead_proc_or_thread:
3392 return_error = BR_DEAD_REPLY;
3393 return_error_line = __LINE__;
Xu YiPing86578a02017-05-22 11:26:23 -07003394 binder_dequeue_work(proc, tcomplete);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003395err_translate_failed:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003396err_bad_object_type:
3397err_bad_offset:
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003398err_bad_parent:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003399err_copy_data_failed:
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003400 trace_binder_transaction_failed_buffer_release(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003401 binder_transaction_buffer_release(target_proc, t->buffer, offp);
Todd Kjos291d9682017-09-25 08:55:09 -07003402 if (target_node)
3403 binder_dec_node_tmpref(target_node);
Todd Kjosc37162d2017-05-26 11:56:29 -07003404 target_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003405 t->buffer->transaction = NULL;
Todd Kjosd325d372016-10-10 10:40:53 -07003406 binder_alloc_free_buf(&target_proc->alloc, t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003407err_binder_alloc_buf_failed:
3408 kfree(tcomplete);
3409 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3410err_alloc_tcomplete_failed:
3411 kfree(t);
3412 binder_stats_deleted(BINDER_STAT_TRANSACTION);
3413err_alloc_t_failed:
3414err_bad_call_stack:
3415err_empty_call_stack:
3416err_dead_binder:
3417err_invalid_target_handle:
Todd Kjos2f993e22017-05-12 14:42:55 -07003418 if (target_thread)
3419 binder_thread_dec_tmpref(target_thread);
3420 if (target_proc)
3421 binder_proc_dec_tmpref(target_proc);
Todd Kjos291d9682017-09-25 08:55:09 -07003422 if (target_node) {
Todd Kjosc37162d2017-05-26 11:56:29 -07003423 binder_dec_node(target_node, 1, 0);
Todd Kjos291d9682017-09-25 08:55:09 -07003424 binder_dec_node_tmpref(target_node);
3425 }
Todd Kjosc37162d2017-05-26 11:56:29 -07003426
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003427 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Todd Kjose598d172017-03-22 17:19:52 -07003428 "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
3429 proc->pid, thread->pid, return_error, return_error_param,
3430 (u64)tr->data_size, (u64)tr->offsets_size,
3431 return_error_line);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003432
3433 {
3434 struct binder_transaction_log_entry *fe;
Seunghun Lee10f62862014-05-01 01:30:23 +09003435
Todd Kjose598d172017-03-22 17:19:52 -07003436 e->return_error = return_error;
3437 e->return_error_param = return_error_param;
3438 e->return_error_line = return_error_line;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003439 fe = binder_transaction_log_add(&binder_transaction_log_failed);
3440 *fe = *e;
Todd Kjos1cfe6272017-05-24 13:33:28 -07003441 /*
3442 * write barrier to synchronize with initialization
3443 * of log entry
3444 */
3445 smp_wmb();
3446 WRITE_ONCE(e->debug_id_done, t_debug_id);
3447 WRITE_ONCE(fe->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003448 }
3449
Todd Kjos858b8da2017-04-21 17:35:12 -07003450 BUG_ON(thread->return_error.cmd != BR_OK);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003451 if (in_reply_to) {
Martijn Coenenecd972d2017-05-26 10:48:56 -07003452 binder_restore_priority(current, in_reply_to->saved_priority);
Todd Kjos858b8da2017-04-21 17:35:12 -07003453 thread->return_error.cmd = BR_TRANSACTION_COMPLETE;
Martijn Coenen1af61802017-10-19 15:04:46 +02003454 binder_enqueue_thread_work(thread, &thread->return_error.work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003455 binder_send_failed_reply(in_reply_to, return_error);
Todd Kjos858b8da2017-04-21 17:35:12 -07003456 } else {
3457 thread->return_error.cmd = return_error;
Martijn Coenen1af61802017-10-19 15:04:46 +02003458 binder_enqueue_thread_work(thread, &thread->return_error.work);
Todd Kjos858b8da2017-04-21 17:35:12 -07003459 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003460}
3461
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02003462static int binder_thread_write(struct binder_proc *proc,
3463 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003464 binder_uintptr_t binder_buffer, size_t size,
3465 binder_size_t *consumed)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003466{
3467 uint32_t cmd;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003468 struct binder_context *context = proc->context;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003469 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003470 void __user *ptr = buffer + *consumed;
3471 void __user *end = buffer + size;
3472
Todd Kjos858b8da2017-04-21 17:35:12 -07003473 while (ptr < end && thread->return_error.cmd == BR_OK) {
Todd Kjosb0117bb2017-05-08 09:16:27 -07003474 int ret;
3475
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003476 if (get_user(cmd, (uint32_t __user *)ptr))
3477 return -EFAULT;
3478 ptr += sizeof(uint32_t);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003479 trace_binder_command(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003480 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07003481 atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]);
3482 atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]);
3483 atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003484 }
3485 switch (cmd) {
3486 case BC_INCREFS:
3487 case BC_ACQUIRE:
3488 case BC_RELEASE:
3489 case BC_DECREFS: {
3490 uint32_t target;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003491 const char *debug_string;
Todd Kjosb0117bb2017-05-08 09:16:27 -07003492 bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE;
3493 bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE;
3494 struct binder_ref_data rdata;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003495
3496 if (get_user(target, (uint32_t __user *)ptr))
3497 return -EFAULT;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003498
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003499 ptr += sizeof(uint32_t);
Todd Kjosb0117bb2017-05-08 09:16:27 -07003500 ret = -1;
3501 if (increment && !target) {
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003502 struct binder_node *ctx_mgr_node;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003503 mutex_lock(&context->context_mgr_node_lock);
3504 ctx_mgr_node = context->binder_context_mgr_node;
Todd Kjosb0117bb2017-05-08 09:16:27 -07003505 if (ctx_mgr_node)
3506 ret = binder_inc_ref_for_node(
3507 proc, ctx_mgr_node,
3508 strong, NULL, &rdata);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003509 mutex_unlock(&context->context_mgr_node_lock);
3510 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07003511 if (ret)
3512 ret = binder_update_ref_for_handle(
3513 proc, target, increment, strong,
3514 &rdata);
3515 if (!ret && rdata.desc != target) {
3516 binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
3517 proc->pid, thread->pid,
3518 target, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003519 }
3520 switch (cmd) {
3521 case BC_INCREFS:
3522 debug_string = "IncRefs";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003523 break;
3524 case BC_ACQUIRE:
3525 debug_string = "Acquire";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003526 break;
3527 case BC_RELEASE:
3528 debug_string = "Release";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003529 break;
3530 case BC_DECREFS:
3531 default:
3532 debug_string = "DecRefs";
Todd Kjosb0117bb2017-05-08 09:16:27 -07003533 break;
3534 }
3535 if (ret) {
3536 binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
3537 proc->pid, thread->pid, debug_string,
3538 strong, target, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003539 break;
3540 }
3541 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjosb0117bb2017-05-08 09:16:27 -07003542 "%d:%d %s ref %d desc %d s %d w %d\n",
3543 proc->pid, thread->pid, debug_string,
3544 rdata.debug_id, rdata.desc, rdata.strong,
3545 rdata.weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003546 break;
3547 }
3548 case BC_INCREFS_DONE:
3549 case BC_ACQUIRE_DONE: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003550 binder_uintptr_t node_ptr;
3551 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003552 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003553 bool free_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003554
Arve Hjønnevågda498892014-02-21 14:40:26 -08003555 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003556 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003557 ptr += sizeof(binder_uintptr_t);
3558 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003559 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003560 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003561 node = binder_get_node(proc, node_ptr);
3562 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003563 binder_user_error("%d:%d %s u%016llx no match\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003564 proc->pid, thread->pid,
3565 cmd == BC_INCREFS_DONE ?
3566 "BC_INCREFS_DONE" :
3567 "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003568 (u64)node_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003569 break;
3570 }
3571 if (cookie != node->cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003572 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003573 proc->pid, thread->pid,
3574 cmd == BC_INCREFS_DONE ?
3575 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003576 (u64)node_ptr, node->debug_id,
3577 (u64)cookie, (u64)node->cookie);
Todd Kjosf22abc72017-05-09 11:08:05 -07003578 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003579 break;
3580 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003581 binder_node_inner_lock(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003582 if (cmd == BC_ACQUIRE_DONE) {
3583 if (node->pending_strong_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303584 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003585 proc->pid, thread->pid,
3586 node->debug_id);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003587 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003588 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003589 break;
3590 }
3591 node->pending_strong_ref = 0;
3592 } else {
3593 if (node->pending_weak_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303594 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003595 proc->pid, thread->pid,
3596 node->debug_id);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003597 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003598 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003599 break;
3600 }
3601 node->pending_weak_ref = 0;
3602 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003603 free_node = binder_dec_node_nilocked(node,
3604 cmd == BC_ACQUIRE_DONE, 0);
3605 WARN_ON(free_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003606 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjosf22abc72017-05-09 11:08:05 -07003607 "%d:%d %s node %d ls %d lw %d tr %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003608 proc->pid, thread->pid,
3609 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Todd Kjosf22abc72017-05-09 11:08:05 -07003610 node->debug_id, node->local_strong_refs,
3611 node->local_weak_refs, node->tmp_refs);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003612 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003613 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003614 break;
3615 }
3616 case BC_ATTEMPT_ACQUIRE:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303617 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003618 return -EINVAL;
3619 case BC_ACQUIRE_RESULT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303620 pr_err("BC_ACQUIRE_RESULT not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003621 return -EINVAL;
3622
3623 case BC_FREE_BUFFER: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003624 binder_uintptr_t data_ptr;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003625 struct binder_buffer *buffer;
3626
Arve Hjønnevågda498892014-02-21 14:40:26 -08003627 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003628 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003629 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003630
Todd Kjos076072a2017-04-21 14:32:11 -07003631 buffer = binder_alloc_prepare_to_free(&proc->alloc,
3632 data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003633 if (buffer == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003634 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
3635 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003636 break;
3637 }
3638 if (!buffer->allow_user_free) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003639 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
3640 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003641 break;
3642 }
3643 binder_debug(BINDER_DEBUG_FREE_BUFFER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003644 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
3645 proc->pid, thread->pid, (u64)data_ptr,
3646 buffer->debug_id,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003647 buffer->transaction ? "active" : "finished");
3648
3649 if (buffer->transaction) {
3650 buffer->transaction->buffer = NULL;
3651 buffer->transaction = NULL;
3652 }
3653 if (buffer->async_transaction && buffer->target_node) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003654 struct binder_node *buf_node;
3655 struct binder_work *w;
3656
3657 buf_node = buffer->target_node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003658 binder_node_inner_lock(buf_node);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003659 BUG_ON(!buf_node->has_async_transaction);
3660 BUG_ON(buf_node->proc != proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003661 w = binder_dequeue_work_head_ilocked(
3662 &buf_node->async_todo);
Martijn Coenen4501c042017-08-10 13:56:16 +02003663 if (!w) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003664 buf_node->has_async_transaction = 0;
Martijn Coenen4501c042017-08-10 13:56:16 +02003665 } else {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003666 binder_enqueue_work_ilocked(
Martijn Coenen4501c042017-08-10 13:56:16 +02003667 w, &proc->todo);
3668 binder_wakeup_proc_ilocked(proc);
3669 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003670 binder_node_inner_unlock(buf_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003671 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003672 trace_binder_transaction_buffer_release(buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003673 binder_transaction_buffer_release(proc, buffer, NULL);
Todd Kjosd325d372016-10-10 10:40:53 -07003674 binder_alloc_free_buf(&proc->alloc, buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003675 break;
3676 }
3677
Martijn Coenen5a6da532016-09-30 14:10:07 +02003678 case BC_TRANSACTION_SG:
3679 case BC_REPLY_SG: {
3680 struct binder_transaction_data_sg tr;
3681
3682 if (copy_from_user(&tr, ptr, sizeof(tr)))
3683 return -EFAULT;
3684 ptr += sizeof(tr);
3685 binder_transaction(proc, thread, &tr.transaction_data,
3686 cmd == BC_REPLY_SG, tr.buffers_size);
3687 break;
3688 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003689 case BC_TRANSACTION:
3690 case BC_REPLY: {
3691 struct binder_transaction_data tr;
3692
3693 if (copy_from_user(&tr, ptr, sizeof(tr)))
3694 return -EFAULT;
3695 ptr += sizeof(tr);
Martijn Coenen59878d72016-09-30 14:05:40 +02003696 binder_transaction(proc, thread, &tr,
3697 cmd == BC_REPLY, 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003698 break;
3699 }
3700
3701 case BC_REGISTER_LOOPER:
3702 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303703 "%d:%d BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003704 proc->pid, thread->pid);
Todd Kjosd600e902017-05-25 17:35:02 -07003705 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003706 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
3707 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303708 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003709 proc->pid, thread->pid);
3710 } else if (proc->requested_threads == 0) {
3711 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303712 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003713 proc->pid, thread->pid);
3714 } else {
3715 proc->requested_threads--;
3716 proc->requested_threads_started++;
3717 }
3718 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
Todd Kjosd600e902017-05-25 17:35:02 -07003719 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003720 break;
3721 case BC_ENTER_LOOPER:
3722 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303723 "%d:%d BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003724 proc->pid, thread->pid);
3725 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
3726 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303727 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003728 proc->pid, thread->pid);
3729 }
3730 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
3731 break;
3732 case BC_EXIT_LOOPER:
3733 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303734 "%d:%d BC_EXIT_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003735 proc->pid, thread->pid);
3736 thread->looper |= BINDER_LOOPER_STATE_EXITED;
3737 break;
3738
3739 case BC_REQUEST_DEATH_NOTIFICATION:
3740 case BC_CLEAR_DEATH_NOTIFICATION: {
3741 uint32_t target;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003742 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003743 struct binder_ref *ref;
Todd Kjos5346bf32016-10-20 16:43:34 -07003744 struct binder_ref_death *death = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003745
3746 if (get_user(target, (uint32_t __user *)ptr))
3747 return -EFAULT;
3748 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08003749 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003750 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003751 ptr += sizeof(binder_uintptr_t);
Todd Kjos5346bf32016-10-20 16:43:34 -07003752 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3753 /*
3754 * Allocate memory for death notification
3755 * before taking lock
3756 */
3757 death = kzalloc(sizeof(*death), GFP_KERNEL);
3758 if (death == NULL) {
3759 WARN_ON(thread->return_error.cmd !=
3760 BR_OK);
3761 thread->return_error.cmd = BR_ERROR;
Martijn Coenen1af61802017-10-19 15:04:46 +02003762 binder_enqueue_thread_work(
3763 thread,
3764 &thread->return_error.work);
Todd Kjos5346bf32016-10-20 16:43:34 -07003765 binder_debug(
3766 BINDER_DEBUG_FAILED_TRANSACTION,
3767 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
3768 proc->pid, thread->pid);
3769 break;
3770 }
3771 }
3772 binder_proc_lock(proc);
3773 ref = binder_get_ref_olocked(proc, target, false);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003774 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303775 binder_user_error("%d:%d %s invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003776 proc->pid, thread->pid,
3777 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3778 "BC_REQUEST_DEATH_NOTIFICATION" :
3779 "BC_CLEAR_DEATH_NOTIFICATION",
3780 target);
Todd Kjos5346bf32016-10-20 16:43:34 -07003781 binder_proc_unlock(proc);
3782 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003783 break;
3784 }
3785
3786 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003787 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003788 proc->pid, thread->pid,
3789 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3790 "BC_REQUEST_DEATH_NOTIFICATION" :
3791 "BC_CLEAR_DEATH_NOTIFICATION",
Todd Kjosb0117bb2017-05-08 09:16:27 -07003792 (u64)cookie, ref->data.debug_id,
3793 ref->data.desc, ref->data.strong,
3794 ref->data.weak, ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003795
Martijn Coenenf9eac642017-05-22 11:26:23 -07003796 binder_node_lock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003797 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3798 if (ref->death) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303799 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003800 proc->pid, thread->pid);
Martijn Coenenf9eac642017-05-22 11:26:23 -07003801 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003802 binder_proc_unlock(proc);
3803 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003804 break;
3805 }
3806 binder_stats_created(BINDER_STAT_DEATH);
3807 INIT_LIST_HEAD(&death->work.entry);
3808 death->cookie = cookie;
3809 ref->death = death;
3810 if (ref->node->proc == NULL) {
3811 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
Martijn Coenen3bdbe4c2017-08-10 13:50:52 +02003812
3813 binder_inner_proc_lock(proc);
3814 binder_enqueue_work_ilocked(
3815 &ref->death->work, &proc->todo);
3816 binder_wakeup_proc_ilocked(proc);
3817 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003818 }
3819 } else {
3820 if (ref->death == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303821 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003822 proc->pid, thread->pid);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003823 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003824 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003825 break;
3826 }
3827 death = ref->death;
3828 if (death->cookie != cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003829 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003830 proc->pid, thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003831 (u64)death->cookie,
3832 (u64)cookie);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003833 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003834 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003835 break;
3836 }
3837 ref->death = NULL;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003838 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003839 if (list_empty(&death->work.entry)) {
3840 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003841 if (thread->looper &
3842 (BINDER_LOOPER_STATE_REGISTERED |
3843 BINDER_LOOPER_STATE_ENTERED))
Martijn Coenen1af61802017-10-19 15:04:46 +02003844 binder_enqueue_thread_work_ilocked(
3845 thread,
3846 &death->work);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003847 else {
3848 binder_enqueue_work_ilocked(
3849 &death->work,
3850 &proc->todo);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07003851 binder_wakeup_proc_ilocked(
Martijn Coenen053be422017-06-06 15:17:46 -07003852 proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003853 }
3854 } else {
3855 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
3856 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
3857 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003858 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003859 }
Martijn Coenenf9eac642017-05-22 11:26:23 -07003860 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003861 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003862 } break;
3863 case BC_DEAD_BINDER_DONE: {
3864 struct binder_work *w;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003865 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003866 struct binder_ref_death *death = NULL;
Seunghun Lee10f62862014-05-01 01:30:23 +09003867
Arve Hjønnevågda498892014-02-21 14:40:26 -08003868 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003869 return -EFAULT;
3870
Lisa Du7a64cd82016-02-17 09:32:52 +08003871 ptr += sizeof(cookie);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003872 binder_inner_proc_lock(proc);
3873 list_for_each_entry(w, &proc->delivered_death,
3874 entry) {
3875 struct binder_ref_death *tmp_death =
3876 container_of(w,
3877 struct binder_ref_death,
3878 work);
Seunghun Lee10f62862014-05-01 01:30:23 +09003879
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003880 if (tmp_death->cookie == cookie) {
3881 death = tmp_death;
3882 break;
3883 }
3884 }
3885 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003886 "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n",
3887 proc->pid, thread->pid, (u64)cookie,
3888 death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003889 if (death == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003890 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
3891 proc->pid, thread->pid, (u64)cookie);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003892 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003893 break;
3894 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003895 binder_dequeue_work_ilocked(&death->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003896 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
3897 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003898 if (thread->looper &
3899 (BINDER_LOOPER_STATE_REGISTERED |
3900 BINDER_LOOPER_STATE_ENTERED))
Martijn Coenen1af61802017-10-19 15:04:46 +02003901 binder_enqueue_thread_work_ilocked(
3902 thread, &death->work);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003903 else {
3904 binder_enqueue_work_ilocked(
3905 &death->work,
3906 &proc->todo);
Martijn Coenen053be422017-06-06 15:17:46 -07003907 binder_wakeup_proc_ilocked(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003908 }
3909 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003910 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003911 } break;
3912
3913 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303914 pr_err("%d:%d unknown command %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003915 proc->pid, thread->pid, cmd);
3916 return -EINVAL;
3917 }
3918 *consumed = ptr - buffer;
3919 }
3920 return 0;
3921}
3922
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02003923static void binder_stat_br(struct binder_proc *proc,
3924 struct binder_thread *thread, uint32_t cmd)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003925{
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003926 trace_binder_return(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003927 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07003928 atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
3929 atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
3930 atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003931 }
3932}
3933
Todd Kjos60792612017-05-24 10:51:01 -07003934static int binder_put_node_cmd(struct binder_proc *proc,
3935 struct binder_thread *thread,
3936 void __user **ptrp,
3937 binder_uintptr_t node_ptr,
3938 binder_uintptr_t node_cookie,
3939 int node_debug_id,
3940 uint32_t cmd, const char *cmd_name)
3941{
3942 void __user *ptr = *ptrp;
3943
3944 if (put_user(cmd, (uint32_t __user *)ptr))
3945 return -EFAULT;
3946 ptr += sizeof(uint32_t);
3947
3948 if (put_user(node_ptr, (binder_uintptr_t __user *)ptr))
3949 return -EFAULT;
3950 ptr += sizeof(binder_uintptr_t);
3951
3952 if (put_user(node_cookie, (binder_uintptr_t __user *)ptr))
3953 return -EFAULT;
3954 ptr += sizeof(binder_uintptr_t);
3955
3956 binder_stat_br(proc, thread, cmd);
3957 binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n",
3958 proc->pid, thread->pid, cmd_name, node_debug_id,
3959 (u64)node_ptr, (u64)node_cookie);
3960
3961 *ptrp = ptr;
3962 return 0;
3963}
3964
Martijn Coenen22d64e4322017-06-02 11:15:44 -07003965static int binder_wait_for_work(struct binder_thread *thread,
3966 bool do_proc_work)
3967{
3968 DEFINE_WAIT(wait);
3969 struct binder_proc *proc = thread->proc;
3970 int ret = 0;
3971
3972 freezer_do_not_count();
3973 binder_inner_proc_lock(proc);
3974 for (;;) {
3975 prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE);
3976 if (binder_has_work_ilocked(thread, do_proc_work))
3977 break;
3978 if (do_proc_work)
3979 list_add(&thread->waiting_thread_node,
3980 &proc->waiting_threads);
3981 binder_inner_proc_unlock(proc);
3982 schedule();
3983 binder_inner_proc_lock(proc);
3984 list_del_init(&thread->waiting_thread_node);
3985 if (signal_pending(current)) {
3986 ret = -ERESTARTSYS;
3987 break;
3988 }
3989 }
3990 finish_wait(&thread->wait, &wait);
3991 binder_inner_proc_unlock(proc);
3992 freezer_count();
3993
3994 return ret;
3995}
3996
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003997static int binder_thread_read(struct binder_proc *proc,
3998 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003999 binder_uintptr_t binder_buffer, size_t size,
4000 binder_size_t *consumed, int non_block)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004001{
Arve Hjønnevågda498892014-02-21 14:40:26 -08004002 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004003 void __user *ptr = buffer + *consumed;
4004 void __user *end = buffer + size;
4005
4006 int ret = 0;
4007 int wait_for_proc_work;
4008
4009 if (*consumed == 0) {
4010 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
4011 return -EFAULT;
4012 ptr += sizeof(uint32_t);
4013 }
4014
4015retry:
Martijn Coenen995a36e2017-06-02 13:36:52 -07004016 binder_inner_proc_lock(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004017 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
Martijn Coenen995a36e2017-06-02 13:36:52 -07004018 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004019
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004020 thread->looper |= BINDER_LOOPER_STATE_WAITING;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004021
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004022 trace_binder_wait_for_work(wait_for_proc_work,
4023 !!thread->transaction_stack,
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004024 !binder_worklist_empty(proc, &thread->todo));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004025 if (wait_for_proc_work) {
4026 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4027 BINDER_LOOPER_STATE_ENTERED))) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05304028 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 +09004029 proc->pid, thread->pid, thread->looper);
4030 wait_event_interruptible(binder_user_error_wait,
4031 binder_stop_on_user_error < 2);
4032 }
Martijn Coenenecd972d2017-05-26 10:48:56 -07004033 binder_restore_priority(current, proc->default_priority);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004034 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004035
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004036 if (non_block) {
4037 if (!binder_has_work(thread, wait_for_proc_work))
4038 ret = -EAGAIN;
4039 } else {
4040 ret = binder_wait_for_work(thread, wait_for_proc_work);
4041 }
4042
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004043 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
4044
4045 if (ret)
4046 return ret;
4047
4048 while (1) {
4049 uint32_t cmd;
4050 struct binder_transaction_data tr;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004051 struct binder_work *w = NULL;
4052 struct list_head *list = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004053 struct binder_transaction *t = NULL;
Todd Kjos2f993e22017-05-12 14:42:55 -07004054 struct binder_thread *t_from;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004055
Todd Kjose7f23ed2017-03-21 13:06:01 -07004056 binder_inner_proc_lock(proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004057 if (!binder_worklist_empty_ilocked(&thread->todo))
4058 list = &thread->todo;
4059 else if (!binder_worklist_empty_ilocked(&proc->todo) &&
4060 wait_for_proc_work)
4061 list = &proc->todo;
4062 else {
4063 binder_inner_proc_unlock(proc);
4064
Dmitry Voytik395262a2014-09-08 18:16:34 +04004065 /* no data added */
Todd Kjos6798e6d2017-01-06 14:19:25 -08004066 if (ptr - buffer == 4 && !thread->looper_need_return)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004067 goto retry;
4068 break;
4069 }
4070
Todd Kjose7f23ed2017-03-21 13:06:01 -07004071 if (end - ptr < sizeof(tr) + 4) {
4072 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004073 break;
Todd Kjose7f23ed2017-03-21 13:06:01 -07004074 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004075 w = binder_dequeue_work_head_ilocked(list);
Martijn Coenen1af61802017-10-19 15:04:46 +02004076 if (binder_worklist_empty_ilocked(&thread->todo))
4077 thread->process_todo = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004078
4079 switch (w->type) {
4080 case BINDER_WORK_TRANSACTION: {
Todd Kjose7f23ed2017-03-21 13:06:01 -07004081 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004082 t = container_of(w, struct binder_transaction, work);
4083 } break;
Todd Kjos858b8da2017-04-21 17:35:12 -07004084 case BINDER_WORK_RETURN_ERROR: {
4085 struct binder_error *e = container_of(
4086 w, struct binder_error, work);
4087
4088 WARN_ON(e->cmd == BR_OK);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004089 binder_inner_proc_unlock(proc);
Todd Kjos858b8da2017-04-21 17:35:12 -07004090 if (put_user(e->cmd, (uint32_t __user *)ptr))
4091 return -EFAULT;
4092 e->cmd = BR_OK;
4093 ptr += sizeof(uint32_t);
4094
4095 binder_stat_br(proc, thread, cmd);
Todd Kjos858b8da2017-04-21 17:35:12 -07004096 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004097 case BINDER_WORK_TRANSACTION_COMPLETE: {
Todd Kjose7f23ed2017-03-21 13:06:01 -07004098 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004099 cmd = BR_TRANSACTION_COMPLETE;
4100 if (put_user(cmd, (uint32_t __user *)ptr))
4101 return -EFAULT;
4102 ptr += sizeof(uint32_t);
4103
4104 binder_stat_br(proc, thread, cmd);
4105 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304106 "%d:%d BR_TRANSACTION_COMPLETE\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004107 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004108 kfree(w);
4109 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4110 } break;
4111 case BINDER_WORK_NODE: {
4112 struct binder_node *node = container_of(w, struct binder_node, work);
Todd Kjos60792612017-05-24 10:51:01 -07004113 int strong, weak;
4114 binder_uintptr_t node_ptr = node->ptr;
4115 binder_uintptr_t node_cookie = node->cookie;
4116 int node_debug_id = node->debug_id;
4117 int has_weak_ref;
4118 int has_strong_ref;
4119 void __user *orig_ptr = ptr;
Seunghun Lee10f62862014-05-01 01:30:23 +09004120
Todd Kjos60792612017-05-24 10:51:01 -07004121 BUG_ON(proc != node->proc);
4122 strong = node->internal_strong_refs ||
4123 node->local_strong_refs;
4124 weak = !hlist_empty(&node->refs) ||
Todd Kjosf22abc72017-05-09 11:08:05 -07004125 node->local_weak_refs ||
4126 node->tmp_refs || strong;
Todd Kjos60792612017-05-24 10:51:01 -07004127 has_strong_ref = node->has_strong_ref;
4128 has_weak_ref = node->has_weak_ref;
4129
4130 if (weak && !has_weak_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004131 node->has_weak_ref = 1;
4132 node->pending_weak_ref = 1;
4133 node->local_weak_refs++;
Todd Kjos60792612017-05-24 10:51:01 -07004134 }
4135 if (strong && !has_strong_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004136 node->has_strong_ref = 1;
4137 node->pending_strong_ref = 1;
4138 node->local_strong_refs++;
Todd Kjos60792612017-05-24 10:51:01 -07004139 }
4140 if (!strong && has_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004141 node->has_strong_ref = 0;
Todd Kjos60792612017-05-24 10:51:01 -07004142 if (!weak && has_weak_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004143 node->has_weak_ref = 0;
Todd Kjos60792612017-05-24 10:51:01 -07004144 if (!weak && !strong) {
4145 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4146 "%d:%d node %d u%016llx c%016llx deleted\n",
4147 proc->pid, thread->pid,
4148 node_debug_id,
4149 (u64)node_ptr,
4150 (u64)node_cookie);
4151 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004152 binder_inner_proc_unlock(proc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004153 binder_node_lock(node);
4154 /*
4155 * Acquire the node lock before freeing the
4156 * node to serialize with other threads that
4157 * may have been holding the node lock while
4158 * decrementing this node (avoids race where
4159 * this thread frees while the other thread
4160 * is unlocking the node after the final
4161 * decrement)
4162 */
4163 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004164 binder_free_node(node);
4165 } else
4166 binder_inner_proc_unlock(proc);
4167
Todd Kjos60792612017-05-24 10:51:01 -07004168 if (weak && !has_weak_ref)
4169 ret = binder_put_node_cmd(
4170 proc, thread, &ptr, node_ptr,
4171 node_cookie, node_debug_id,
4172 BR_INCREFS, "BR_INCREFS");
4173 if (!ret && strong && !has_strong_ref)
4174 ret = binder_put_node_cmd(
4175 proc, thread, &ptr, node_ptr,
4176 node_cookie, node_debug_id,
4177 BR_ACQUIRE, "BR_ACQUIRE");
4178 if (!ret && !strong && has_strong_ref)
4179 ret = binder_put_node_cmd(
4180 proc, thread, &ptr, node_ptr,
4181 node_cookie, node_debug_id,
4182 BR_RELEASE, "BR_RELEASE");
4183 if (!ret && !weak && has_weak_ref)
4184 ret = binder_put_node_cmd(
4185 proc, thread, &ptr, node_ptr,
4186 node_cookie, node_debug_id,
4187 BR_DECREFS, "BR_DECREFS");
4188 if (orig_ptr == ptr)
4189 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4190 "%d:%d node %d u%016llx c%016llx state unchanged\n",
4191 proc->pid, thread->pid,
4192 node_debug_id,
4193 (u64)node_ptr,
4194 (u64)node_cookie);
4195 if (ret)
4196 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004197 } break;
4198 case BINDER_WORK_DEAD_BINDER:
4199 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4200 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4201 struct binder_ref_death *death;
4202 uint32_t cmd;
Martijn Coenenf9eac642017-05-22 11:26:23 -07004203 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004204
4205 death = container_of(w, struct binder_ref_death, work);
4206 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
4207 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
4208 else
4209 cmd = BR_DEAD_BINDER;
Martijn Coenenf9eac642017-05-22 11:26:23 -07004210 cookie = death->cookie;
4211
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004212 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004213 "%d:%d %s %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004214 proc->pid, thread->pid,
4215 cmd == BR_DEAD_BINDER ?
4216 "BR_DEAD_BINDER" :
4217 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
Martijn Coenenf9eac642017-05-22 11:26:23 -07004218 (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004219 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
Martijn Coenenf9eac642017-05-22 11:26:23 -07004220 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004221 kfree(death);
4222 binder_stats_deleted(BINDER_STAT_DEATH);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004223 } else {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004224 binder_enqueue_work_ilocked(
4225 w, &proc->delivered_death);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004226 binder_inner_proc_unlock(proc);
4227 }
Martijn Coenenf9eac642017-05-22 11:26:23 -07004228 if (put_user(cmd, (uint32_t __user *)ptr))
4229 return -EFAULT;
4230 ptr += sizeof(uint32_t);
4231 if (put_user(cookie,
4232 (binder_uintptr_t __user *)ptr))
4233 return -EFAULT;
4234 ptr += sizeof(binder_uintptr_t);
4235 binder_stat_br(proc, thread, cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004236 if (cmd == BR_DEAD_BINDER)
4237 goto done; /* DEAD_BINDER notifications can cause transactions */
4238 } break;
4239 }
4240
4241 if (!t)
4242 continue;
4243
4244 BUG_ON(t->buffer == NULL);
4245 if (t->buffer->target_node) {
4246 struct binder_node *target_node = t->buffer->target_node;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004247 struct binder_priority node_prio;
Seunghun Lee10f62862014-05-01 01:30:23 +09004248
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004249 tr.target.ptr = target_node->ptr;
4250 tr.cookie = target_node->cookie;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004251 node_prio.sched_policy = target_node->sched_policy;
4252 node_prio.prio = target_node->min_priority;
Martijn Coenenc46810c2017-06-23 10:13:43 -07004253 binder_transaction_priority(current, t, node_prio,
4254 target_node->inherit_rt);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004255 cmd = BR_TRANSACTION;
4256 } else {
Arve Hjønnevågda498892014-02-21 14:40:26 -08004257 tr.target.ptr = 0;
4258 tr.cookie = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004259 cmd = BR_REPLY;
4260 }
4261 tr.code = t->code;
4262 tr.flags = t->flags;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -06004263 tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004264
Todd Kjos2f993e22017-05-12 14:42:55 -07004265 t_from = binder_get_txn_from(t);
4266 if (t_from) {
4267 struct task_struct *sender = t_from->proc->tsk;
Seunghun Lee10f62862014-05-01 01:30:23 +09004268
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004269 tr.sender_pid = task_tgid_nr_ns(sender,
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08004270 task_active_pid_ns(current));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004271 } else {
4272 tr.sender_pid = 0;
4273 }
4274
4275 tr.data_size = t->buffer->data_size;
4276 tr.offsets_size = t->buffer->offsets_size;
Todd Kjosd325d372016-10-10 10:40:53 -07004277 tr.data.ptr.buffer = (binder_uintptr_t)
4278 ((uintptr_t)t->buffer->data +
4279 binder_alloc_get_user_buffer_offset(&proc->alloc));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004280 tr.data.ptr.offsets = tr.data.ptr.buffer +
4281 ALIGN(t->buffer->data_size,
4282 sizeof(void *));
4283
Todd Kjos2f993e22017-05-12 14:42:55 -07004284 if (put_user(cmd, (uint32_t __user *)ptr)) {
4285 if (t_from)
4286 binder_thread_dec_tmpref(t_from);
Martijn Coenen3217ccc2017-08-24 15:23:36 +02004287
4288 binder_cleanup_transaction(t, "put_user failed",
4289 BR_FAILED_REPLY);
4290
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004291 return -EFAULT;
Todd Kjos2f993e22017-05-12 14:42:55 -07004292 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004293 ptr += sizeof(uint32_t);
Todd Kjos2f993e22017-05-12 14:42:55 -07004294 if (copy_to_user(ptr, &tr, sizeof(tr))) {
4295 if (t_from)
4296 binder_thread_dec_tmpref(t_from);
Martijn Coenen3217ccc2017-08-24 15:23:36 +02004297
4298 binder_cleanup_transaction(t, "copy_to_user failed",
4299 BR_FAILED_REPLY);
4300
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004301 return -EFAULT;
Todd Kjos2f993e22017-05-12 14:42:55 -07004302 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004303 ptr += sizeof(tr);
4304
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004305 trace_binder_transaction_received(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004306 binder_stat_br(proc, thread, cmd);
4307 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004308 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004309 proc->pid, thread->pid,
4310 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
4311 "BR_REPLY",
Todd Kjos2f993e22017-05-12 14:42:55 -07004312 t->debug_id, t_from ? t_from->proc->pid : 0,
4313 t_from ? t_from->pid : 0, cmd,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004314 t->buffer->data_size, t->buffer->offsets_size,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004315 (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004316
Todd Kjos2f993e22017-05-12 14:42:55 -07004317 if (t_from)
4318 binder_thread_dec_tmpref(t_from);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004319 t->buffer->allow_user_free = 1;
4320 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07004321 binder_inner_proc_lock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004322 t->to_parent = thread->transaction_stack;
4323 t->to_thread = thread;
4324 thread->transaction_stack = t;
Martijn Coenen995a36e2017-06-02 13:36:52 -07004325 binder_inner_proc_unlock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004326 } else {
Todd Kjos21ef40a2017-03-30 18:02:13 -07004327 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004328 }
4329 break;
4330 }
4331
4332done:
4333
4334 *consumed = ptr - buffer;
Todd Kjosd600e902017-05-25 17:35:02 -07004335 binder_inner_proc_lock(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004336 if (proc->requested_threads == 0 &&
4337 list_empty(&thread->proc->waiting_threads) &&
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004338 proc->requested_threads_started < proc->max_threads &&
4339 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4340 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
4341 /*spawn a new thread if we leave this out */) {
4342 proc->requested_threads++;
Todd Kjosd600e902017-05-25 17:35:02 -07004343 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004344 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304345 "%d:%d BR_SPAWN_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004346 proc->pid, thread->pid);
4347 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
4348 return -EFAULT;
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07004349 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
Todd Kjosd600e902017-05-25 17:35:02 -07004350 } else
4351 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004352 return 0;
4353}
4354
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004355static void binder_release_work(struct binder_proc *proc,
4356 struct list_head *list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004357{
4358 struct binder_work *w;
Seunghun Lee10f62862014-05-01 01:30:23 +09004359
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004360 while (1) {
4361 w = binder_dequeue_work_head(proc, list);
4362 if (!w)
4363 return;
4364
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004365 switch (w->type) {
4366 case BINDER_WORK_TRANSACTION: {
4367 struct binder_transaction *t;
4368
4369 t = container_of(w, struct binder_transaction, work);
Martijn Coenen3217ccc2017-08-24 15:23:36 +02004370
4371 binder_cleanup_transaction(t, "process died.",
4372 BR_DEAD_REPLY);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004373 } break;
Todd Kjos858b8da2017-04-21 17:35:12 -07004374 case BINDER_WORK_RETURN_ERROR: {
4375 struct binder_error *e = container_of(
4376 w, struct binder_error, work);
4377
4378 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4379 "undelivered TRANSACTION_ERROR: %u\n",
4380 e->cmd);
4381 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004382 case BINDER_WORK_TRANSACTION_COMPLETE: {
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004383 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304384 "undelivered TRANSACTION_COMPLETE\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004385 kfree(w);
4386 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4387 } break;
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004388 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4389 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4390 struct binder_ref_death *death;
4391
4392 death = container_of(w, struct binder_ref_death, work);
4393 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004394 "undelivered death notification, %016llx\n",
4395 (u64)death->cookie);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004396 kfree(death);
4397 binder_stats_deleted(BINDER_STAT_DEATH);
4398 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004399 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304400 pr_err("unexpected work type, %d, not freed\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004401 w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004402 break;
4403 }
4404 }
4405
4406}
4407
Todd Kjosb4827902017-05-25 15:52:17 -07004408static struct binder_thread *binder_get_thread_ilocked(
4409 struct binder_proc *proc, struct binder_thread *new_thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004410{
4411 struct binder_thread *thread = NULL;
4412 struct rb_node *parent = NULL;
4413 struct rb_node **p = &proc->threads.rb_node;
4414
4415 while (*p) {
4416 parent = *p;
4417 thread = rb_entry(parent, struct binder_thread, rb_node);
4418
4419 if (current->pid < thread->pid)
4420 p = &(*p)->rb_left;
4421 else if (current->pid > thread->pid)
4422 p = &(*p)->rb_right;
4423 else
Todd Kjosb4827902017-05-25 15:52:17 -07004424 return thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004425 }
Todd Kjosb4827902017-05-25 15:52:17 -07004426 if (!new_thread)
4427 return NULL;
4428 thread = new_thread;
4429 binder_stats_created(BINDER_STAT_THREAD);
4430 thread->proc = proc;
4431 thread->pid = current->pid;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004432 get_task_struct(current);
4433 thread->task = current;
Todd Kjosb4827902017-05-25 15:52:17 -07004434 atomic_set(&thread->tmp_ref, 0);
4435 init_waitqueue_head(&thread->wait);
4436 INIT_LIST_HEAD(&thread->todo);
4437 rb_link_node(&thread->rb_node, parent, p);
4438 rb_insert_color(&thread->rb_node, &proc->threads);
4439 thread->looper_need_return = true;
4440 thread->return_error.work.type = BINDER_WORK_RETURN_ERROR;
4441 thread->return_error.cmd = BR_OK;
4442 thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR;
4443 thread->reply_error.cmd = BR_OK;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004444 INIT_LIST_HEAD(&new_thread->waiting_thread_node);
Todd Kjosb4827902017-05-25 15:52:17 -07004445 return thread;
4446}
4447
4448static struct binder_thread *binder_get_thread(struct binder_proc *proc)
4449{
4450 struct binder_thread *thread;
4451 struct binder_thread *new_thread;
4452
4453 binder_inner_proc_lock(proc);
4454 thread = binder_get_thread_ilocked(proc, NULL);
4455 binder_inner_proc_unlock(proc);
4456 if (!thread) {
4457 new_thread = kzalloc(sizeof(*thread), GFP_KERNEL);
4458 if (new_thread == NULL)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004459 return NULL;
Todd Kjosb4827902017-05-25 15:52:17 -07004460 binder_inner_proc_lock(proc);
4461 thread = binder_get_thread_ilocked(proc, new_thread);
4462 binder_inner_proc_unlock(proc);
4463 if (thread != new_thread)
4464 kfree(new_thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004465 }
4466 return thread;
4467}
4468
Todd Kjos2f993e22017-05-12 14:42:55 -07004469static void binder_free_proc(struct binder_proc *proc)
4470{
4471 BUG_ON(!list_empty(&proc->todo));
4472 BUG_ON(!list_empty(&proc->delivered_death));
4473 binder_alloc_deferred_release(&proc->alloc);
4474 put_task_struct(proc->tsk);
4475 binder_stats_deleted(BINDER_STAT_PROC);
4476 kfree(proc);
4477}
4478
4479static void binder_free_thread(struct binder_thread *thread)
4480{
4481 BUG_ON(!list_empty(&thread->todo));
4482 binder_stats_deleted(BINDER_STAT_THREAD);
4483 binder_proc_dec_tmpref(thread->proc);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004484 put_task_struct(thread->task);
Todd Kjos2f993e22017-05-12 14:42:55 -07004485 kfree(thread);
4486}
4487
4488static int binder_thread_release(struct binder_proc *proc,
4489 struct binder_thread *thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004490{
4491 struct binder_transaction *t;
4492 struct binder_transaction *send_reply = NULL;
4493 int active_transactions = 0;
Todd Kjos2f993e22017-05-12 14:42:55 -07004494 struct binder_transaction *last_t = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004495
Todd Kjosb4827902017-05-25 15:52:17 -07004496 binder_inner_proc_lock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07004497 /*
4498 * take a ref on the proc so it survives
4499 * after we remove this thread from proc->threads.
4500 * The corresponding dec is when we actually
4501 * free the thread in binder_free_thread()
4502 */
4503 proc->tmp_ref++;
4504 /*
4505 * take a ref on this thread to ensure it
4506 * survives while we are releasing it
4507 */
4508 atomic_inc(&thread->tmp_ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004509 rb_erase(&thread->rb_node, &proc->threads);
4510 t = thread->transaction_stack;
Todd Kjos2f993e22017-05-12 14:42:55 -07004511 if (t) {
4512 spin_lock(&t->lock);
4513 if (t->to_thread == thread)
4514 send_reply = t;
4515 }
4516 thread->is_dead = true;
4517
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004518 while (t) {
Todd Kjos2f993e22017-05-12 14:42:55 -07004519 last_t = t;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004520 active_transactions++;
4521 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304522 "release %d:%d transaction %d %s, still active\n",
4523 proc->pid, thread->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004524 t->debug_id,
4525 (t->to_thread == thread) ? "in" : "out");
4526
4527 if (t->to_thread == thread) {
4528 t->to_proc = NULL;
4529 t->to_thread = NULL;
4530 if (t->buffer) {
4531 t->buffer->transaction = NULL;
4532 t->buffer = NULL;
4533 }
4534 t = t->to_parent;
4535 } else if (t->from == thread) {
4536 t->from = NULL;
4537 t = t->from_parent;
4538 } else
4539 BUG();
Todd Kjos2f993e22017-05-12 14:42:55 -07004540 spin_unlock(&last_t->lock);
4541 if (t)
4542 spin_lock(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004543 }
Martijn Coenen550c01d2018-01-05 11:27:07 +01004544
4545 /*
4546 * If this thread used poll, make sure we remove the waitqueue
4547 * from any epoll data structures holding it with POLLFREE.
4548 * waitqueue_active() is safe to use here because we're holding
4549 * the inner lock.
4550 */
4551 if ((thread->looper & BINDER_LOOPER_STATE_POLL) &&
4552 waitqueue_active(&thread->wait)) {
4553 wake_up_poll(&thread->wait, POLLHUP | POLLFREE);
4554 }
4555
Todd Kjosb4827902017-05-25 15:52:17 -07004556 binder_inner_proc_unlock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07004557
Martijn Coenen72766d72018-02-16 09:47:15 +01004558 /*
4559 * This is needed to avoid races between wake_up_poll() above and
4560 * and ep_remove_waitqueue() called for other reasons (eg the epoll file
4561 * descriptor being closed); ep_remove_waitqueue() holds an RCU read
4562 * lock, so we can be sure it's done after calling synchronize_rcu().
4563 */
4564 if (thread->looper & BINDER_LOOPER_STATE_POLL)
4565 synchronize_rcu();
4566
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004567 if (send_reply)
4568 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004569 binder_release_work(proc, &thread->todo);
Todd Kjos2f993e22017-05-12 14:42:55 -07004570 binder_thread_dec_tmpref(thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004571 return active_transactions;
4572}
4573
4574static unsigned int binder_poll(struct file *filp,
4575 struct poll_table_struct *wait)
4576{
4577 struct binder_proc *proc = filp->private_data;
4578 struct binder_thread *thread = NULL;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004579 bool wait_for_proc_work;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004580
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004581 thread = binder_get_thread(proc);
Greg Kroah-Hartman6e463bb2018-02-28 17:17:14 +01004582 if (!thread)
Eric Biggers4be5a282018-01-30 23:11:24 -08004583 return POLLERR;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004584
Martijn Coenen995a36e2017-06-02 13:36:52 -07004585 binder_inner_proc_lock(thread->proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004586 thread->looper |= BINDER_LOOPER_STATE_POLL;
4587 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
4588
Martijn Coenen995a36e2017-06-02 13:36:52 -07004589 binder_inner_proc_unlock(thread->proc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004590
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004591 poll_wait(filp, &thread->wait, wait);
4592
Martijn Coenen47810932017-08-10 12:32:00 +02004593 if (binder_has_work(thread, wait_for_proc_work))
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004594 return POLLIN;
4595
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004596 return 0;
4597}
4598
Tair Rzayev78260ac2014-06-03 22:27:21 +03004599static int binder_ioctl_write_read(struct file *filp,
4600 unsigned int cmd, unsigned long arg,
4601 struct binder_thread *thread)
4602{
4603 int ret = 0;
4604 struct binder_proc *proc = filp->private_data;
4605 unsigned int size = _IOC_SIZE(cmd);
4606 void __user *ubuf = (void __user *)arg;
4607 struct binder_write_read bwr;
4608
4609 if (size != sizeof(struct binder_write_read)) {
4610 ret = -EINVAL;
4611 goto out;
4612 }
4613 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
4614 ret = -EFAULT;
4615 goto out;
4616 }
4617 binder_debug(BINDER_DEBUG_READ_WRITE,
4618 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
4619 proc->pid, thread->pid,
4620 (u64)bwr.write_size, (u64)bwr.write_buffer,
4621 (u64)bwr.read_size, (u64)bwr.read_buffer);
4622
4623 if (bwr.write_size > 0) {
4624 ret = binder_thread_write(proc, thread,
4625 bwr.write_buffer,
4626 bwr.write_size,
4627 &bwr.write_consumed);
4628 trace_binder_write_done(ret);
4629 if (ret < 0) {
4630 bwr.read_consumed = 0;
4631 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4632 ret = -EFAULT;
4633 goto out;
4634 }
4635 }
4636 if (bwr.read_size > 0) {
4637 ret = binder_thread_read(proc, thread, bwr.read_buffer,
4638 bwr.read_size,
4639 &bwr.read_consumed,
4640 filp->f_flags & O_NONBLOCK);
4641 trace_binder_read_done(ret);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004642 binder_inner_proc_lock(proc);
4643 if (!binder_worklist_empty_ilocked(&proc->todo))
Martijn Coenen053be422017-06-06 15:17:46 -07004644 binder_wakeup_proc_ilocked(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004645 binder_inner_proc_unlock(proc);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004646 if (ret < 0) {
4647 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4648 ret = -EFAULT;
4649 goto out;
4650 }
4651 }
4652 binder_debug(BINDER_DEBUG_READ_WRITE,
4653 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
4654 proc->pid, thread->pid,
4655 (u64)bwr.write_consumed, (u64)bwr.write_size,
4656 (u64)bwr.read_consumed, (u64)bwr.read_size);
4657 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
4658 ret = -EFAULT;
4659 goto out;
4660 }
4661out:
4662 return ret;
4663}
4664
4665static int binder_ioctl_set_ctx_mgr(struct file *filp)
4666{
4667 int ret = 0;
4668 struct binder_proc *proc = filp->private_data;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004669 struct binder_context *context = proc->context;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004670 struct binder_node *new_node;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004671 kuid_t curr_euid = current_euid();
4672
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004673 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004674 if (context->binder_context_mgr_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004675 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
4676 ret = -EBUSY;
4677 goto out;
4678 }
Stephen Smalley79af7302015-01-21 10:54:10 -05004679 ret = security_binder_set_context_mgr(proc->tsk);
4680 if (ret < 0)
4681 goto out;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004682 if (uid_valid(context->binder_context_mgr_uid)) {
4683 if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004684 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
4685 from_kuid(&init_user_ns, curr_euid),
4686 from_kuid(&init_user_ns,
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004687 context->binder_context_mgr_uid));
Tair Rzayev78260ac2014-06-03 22:27:21 +03004688 ret = -EPERM;
4689 goto out;
4690 }
4691 } else {
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004692 context->binder_context_mgr_uid = curr_euid;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004693 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004694 new_node = binder_new_node(proc, NULL);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004695 if (!new_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004696 ret = -ENOMEM;
4697 goto out;
4698 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004699 binder_node_lock(new_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004700 new_node->local_weak_refs++;
4701 new_node->local_strong_refs++;
4702 new_node->has_strong_ref = 1;
4703 new_node->has_weak_ref = 1;
4704 context->binder_context_mgr_node = new_node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004705 binder_node_unlock(new_node);
Todd Kjosf22abc72017-05-09 11:08:05 -07004706 binder_put_node(new_node);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004707out:
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004708 mutex_unlock(&context->context_mgr_node_lock);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004709 return ret;
4710}
4711
Colin Cross833babb32017-06-20 13:54:44 -07004712static int binder_ioctl_get_node_debug_info(struct binder_proc *proc,
4713 struct binder_node_debug_info *info) {
4714 struct rb_node *n;
4715 binder_uintptr_t ptr = info->ptr;
4716
4717 memset(info, 0, sizeof(*info));
4718
4719 binder_inner_proc_lock(proc);
4720 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
4721 struct binder_node *node = rb_entry(n, struct binder_node,
4722 rb_node);
4723 if (node->ptr > ptr) {
4724 info->ptr = node->ptr;
4725 info->cookie = node->cookie;
4726 info->has_strong_ref = node->has_strong_ref;
4727 info->has_weak_ref = node->has_weak_ref;
4728 break;
4729 }
4730 }
4731 binder_inner_proc_unlock(proc);
4732
4733 return 0;
4734}
4735
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004736static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4737{
4738 int ret;
4739 struct binder_proc *proc = filp->private_data;
4740 struct binder_thread *thread;
4741 unsigned int size = _IOC_SIZE(cmd);
4742 void __user *ubuf = (void __user *)arg;
4743
Tair Rzayev78260ac2014-06-03 22:27:21 +03004744 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
4745 proc->pid, current->pid, cmd, arg);*/
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004746
Sherry Yang435416b2017-06-22 14:37:45 -07004747 binder_selftest_alloc(&proc->alloc);
4748
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004749 trace_binder_ioctl(cmd, arg);
4750
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004751 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4752 if (ret)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004753 goto err_unlocked;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004754
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004755 thread = binder_get_thread(proc);
4756 if (thread == NULL) {
4757 ret = -ENOMEM;
4758 goto err;
4759 }
4760
4761 switch (cmd) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004762 case BINDER_WRITE_READ:
4763 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
4764 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004765 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004766 break;
Todd Kjosd600e902017-05-25 17:35:02 -07004767 case BINDER_SET_MAX_THREADS: {
4768 int max_threads;
4769
4770 if (copy_from_user(&max_threads, ubuf,
4771 sizeof(max_threads))) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004772 ret = -EINVAL;
4773 goto err;
4774 }
Todd Kjosd600e902017-05-25 17:35:02 -07004775 binder_inner_proc_lock(proc);
4776 proc->max_threads = max_threads;
4777 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004778 break;
Todd Kjosd600e902017-05-25 17:35:02 -07004779 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004780 case BINDER_SET_CONTEXT_MGR:
Tair Rzayev78260ac2014-06-03 22:27:21 +03004781 ret = binder_ioctl_set_ctx_mgr(filp);
4782 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004783 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004784 break;
4785 case BINDER_THREAD_EXIT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304786 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004787 proc->pid, thread->pid);
Todd Kjos2f993e22017-05-12 14:42:55 -07004788 binder_thread_release(proc, thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004789 thread = NULL;
4790 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02004791 case BINDER_VERSION: {
4792 struct binder_version __user *ver = ubuf;
4793
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004794 if (size != sizeof(struct binder_version)) {
4795 ret = -EINVAL;
4796 goto err;
4797 }
Mathieu Maret36c89c02014-04-15 12:03:05 +02004798 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
4799 &ver->protocol_version)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004800 ret = -EINVAL;
4801 goto err;
4802 }
4803 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02004804 }
Colin Cross833babb32017-06-20 13:54:44 -07004805 case BINDER_GET_NODE_DEBUG_INFO: {
4806 struct binder_node_debug_info info;
4807
4808 if (copy_from_user(&info, ubuf, sizeof(info))) {
4809 ret = -EFAULT;
4810 goto err;
4811 }
4812
4813 ret = binder_ioctl_get_node_debug_info(proc, &info);
4814 if (ret < 0)
4815 goto err;
4816
4817 if (copy_to_user(ubuf, &info, sizeof(info))) {
4818 ret = -EFAULT;
4819 goto err;
4820 }
4821 break;
4822 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004823 default:
4824 ret = -EINVAL;
4825 goto err;
4826 }
4827 ret = 0;
4828err:
4829 if (thread)
Todd Kjos6798e6d2017-01-06 14:19:25 -08004830 thread->looper_need_return = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004831 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4832 if (ret && ret != -ERESTARTSYS)
Anmol Sarma56b468f2012-10-30 22:35:43 +05304833 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 -07004834err_unlocked:
4835 trace_binder_ioctl_done(ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004836 return ret;
4837}
4838
4839static void binder_vma_open(struct vm_area_struct *vma)
4840{
4841 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004842
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004843 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304844 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004845 proc->pid, vma->vm_start, vma->vm_end,
4846 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4847 (unsigned long)pgprot_val(vma->vm_page_prot));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004848}
4849
4850static void binder_vma_close(struct vm_area_struct *vma)
4851{
4852 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004853
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004854 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304855 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004856 proc->pid, vma->vm_start, vma->vm_end,
4857 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4858 (unsigned long)pgprot_val(vma->vm_page_prot));
Todd Kjosd325d372016-10-10 10:40:53 -07004859 binder_alloc_vma_close(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004860}
4861
Vinayak Menonddac7d52014-06-02 18:17:59 +05304862static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
4863{
4864 return VM_FAULT_SIGBUS;
4865}
4866
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07004867static const struct vm_operations_struct binder_vm_ops = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004868 .open = binder_vma_open,
4869 .close = binder_vma_close,
Vinayak Menonddac7d52014-06-02 18:17:59 +05304870 .fault = binder_vm_fault,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004871};
4872
Todd Kjosd325d372016-10-10 10:40:53 -07004873static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
4874{
4875 int ret;
4876 struct binder_proc *proc = filp->private_data;
4877 const char *failure_string;
4878
4879 if (proc->tsk != current->group_leader)
4880 return -EINVAL;
4881
4882 if ((vma->vm_end - vma->vm_start) > SZ_4M)
4883 vma->vm_end = vma->vm_start + SZ_4M;
4884
4885 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4886 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
4887 __func__, proc->pid, vma->vm_start, vma->vm_end,
4888 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4889 (unsigned long)pgprot_val(vma->vm_page_prot));
4890
4891 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
4892 ret = -EPERM;
4893 failure_string = "bad vm_flags";
4894 goto err_bad_arg;
4895 }
4896 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
4897 vma->vm_ops = &binder_vm_ops;
4898 vma->vm_private_data = proc;
4899
4900 ret = binder_alloc_mmap_handler(&proc->alloc, vma);
Todd Kjosf09daf12017-11-10 15:30:27 -08004901
4902 return ret;
Todd Kjosd325d372016-10-10 10:40:53 -07004903
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004904err_bad_arg:
Sherwin Soltani258767f2012-06-26 02:00:30 -04004905 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004906 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
4907 return ret;
4908}
4909
4910static int binder_open(struct inode *nodp, struct file *filp)
4911{
4912 struct binder_proc *proc;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02004913 struct binder_device *binder_dev;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004914
4915 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
4916 current->group_leader->pid, current->pid);
4917
4918 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
4919 if (proc == NULL)
4920 return -ENOMEM;
Todd Kjosfc7a7e22017-05-29 16:44:24 -07004921 spin_lock_init(&proc->inner_lock);
4922 spin_lock_init(&proc->outer_lock);
Martijn Coenen872c26e2017-03-07 15:51:18 +01004923 get_task_struct(current->group_leader);
4924 proc->tsk = current->group_leader;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004925 INIT_LIST_HEAD(&proc->todo);
Martijn Coenen57b2ac62017-06-06 17:04:42 -07004926 if (binder_supported_policy(current->policy)) {
4927 proc->default_priority.sched_policy = current->policy;
4928 proc->default_priority.prio = current->normal_prio;
4929 } else {
4930 proc->default_priority.sched_policy = SCHED_NORMAL;
4931 proc->default_priority.prio = NICE_TO_PRIO(0);
4932 }
4933
Martijn Coenen6b7c7122016-09-30 16:08:09 +02004934 binder_dev = container_of(filp->private_data, struct binder_device,
4935 miscdev);
4936 proc->context = &binder_dev->context;
Todd Kjosd325d372016-10-10 10:40:53 -07004937 binder_alloc_init(&proc->alloc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004938
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004939 binder_stats_created(BINDER_STAT_PROC);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004940 proc->pid = current->group_leader->pid;
4941 INIT_LIST_HEAD(&proc->delivered_death);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004942 INIT_LIST_HEAD(&proc->waiting_threads);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004943 filp->private_data = proc;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004944
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004945 mutex_lock(&binder_procs_lock);
4946 hlist_add_head(&proc->proc_node, &binder_procs);
4947 mutex_unlock(&binder_procs_lock);
4948
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004949 if (binder_debugfs_dir_entry_proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004950 char strbuf[11];
Seunghun Lee10f62862014-05-01 01:30:23 +09004951
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004952 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02004953 /*
4954 * proc debug entries are shared between contexts, so
4955 * this will fail if the process tries to open the driver
4956 * again with a different context. The priting code will
4957 * anyway print all contexts that a given PID has, so this
4958 * is not a problem.
4959 */
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004960 proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02004961 binder_debugfs_dir_entry_proc,
4962 (void *)(unsigned long)proc->pid,
4963 &binder_proc_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004964 }
4965
4966 return 0;
4967}
4968
4969static int binder_flush(struct file *filp, fl_owner_t id)
4970{
4971 struct binder_proc *proc = filp->private_data;
4972
4973 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
4974
4975 return 0;
4976}
4977
4978static void binder_deferred_flush(struct binder_proc *proc)
4979{
4980 struct rb_node *n;
4981 int wake_count = 0;
Seunghun Lee10f62862014-05-01 01:30:23 +09004982
Todd Kjosb4827902017-05-25 15:52:17 -07004983 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004984 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
4985 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
Seunghun Lee10f62862014-05-01 01:30:23 +09004986
Todd Kjos6798e6d2017-01-06 14:19:25 -08004987 thread->looper_need_return = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004988 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
4989 wake_up_interruptible(&thread->wait);
4990 wake_count++;
4991 }
4992 }
Todd Kjosb4827902017-05-25 15:52:17 -07004993 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004994
4995 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4996 "binder_flush: %d woke %d threads\n", proc->pid,
4997 wake_count);
4998}
4999
5000static int binder_release(struct inode *nodp, struct file *filp)
5001{
5002 struct binder_proc *proc = filp->private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09005003
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005004 debugfs_remove(proc->debugfs_entry);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005005 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
5006
5007 return 0;
5008}
5009
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005010static int binder_node_release(struct binder_node *node, int refs)
5011{
5012 struct binder_ref *ref;
5013 int death = 0;
Todd Kjose7f23ed2017-03-21 13:06:01 -07005014 struct binder_proc *proc = node->proc;
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005015
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005016 binder_release_work(proc, &node->async_todo);
Todd Kjose7f23ed2017-03-21 13:06:01 -07005017
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005018 binder_node_lock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07005019 binder_inner_proc_lock(proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005020 binder_dequeue_work_ilocked(&node->work);
Todd Kjosf22abc72017-05-09 11:08:05 -07005021 /*
5022 * The caller must have taken a temporary ref on the node,
5023 */
5024 BUG_ON(!node->tmp_refs);
5025 if (hlist_empty(&node->refs) && node->tmp_refs == 1) {
Todd Kjose7f23ed2017-03-21 13:06:01 -07005026 binder_inner_proc_unlock(proc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005027 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07005028 binder_free_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005029
5030 return refs;
5031 }
5032
5033 node->proc = NULL;
5034 node->local_strong_refs = 0;
5035 node->local_weak_refs = 0;
Todd Kjose7f23ed2017-03-21 13:06:01 -07005036 binder_inner_proc_unlock(proc);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005037
5038 spin_lock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005039 hlist_add_head(&node->dead_node, &binder_dead_nodes);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005040 spin_unlock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005041
5042 hlist_for_each_entry(ref, &node->refs, node_entry) {
5043 refs++;
Martijn Coenenf9eac642017-05-22 11:26:23 -07005044 /*
5045 * Need the node lock to synchronize
5046 * with new notification requests and the
5047 * inner lock to synchronize with queued
5048 * death notifications.
5049 */
5050 binder_inner_proc_lock(ref->proc);
5051 if (!ref->death) {
5052 binder_inner_proc_unlock(ref->proc);
Arve Hjønnevåge194fd82014-02-17 13:58:29 -08005053 continue;
Martijn Coenenf9eac642017-05-22 11:26:23 -07005054 }
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005055
5056 death++;
5057
Martijn Coenenf9eac642017-05-22 11:26:23 -07005058 BUG_ON(!list_empty(&ref->death->work.entry));
5059 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
5060 binder_enqueue_work_ilocked(&ref->death->work,
5061 &ref->proc->todo);
Martijn Coenen053be422017-06-06 15:17:46 -07005062 binder_wakeup_proc_ilocked(ref->proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005063 binder_inner_proc_unlock(ref->proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005064 }
5065
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005066 binder_debug(BINDER_DEBUG_DEAD_BINDER,
5067 "node %d now dead, refs %d, death %d\n",
5068 node->debug_id, refs, death);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005069 binder_node_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07005070 binder_put_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005071
5072 return refs;
5073}
5074
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005075static void binder_deferred_release(struct binder_proc *proc)
5076{
Martijn Coenen0b3311e2016-09-30 15:51:48 +02005077 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005078 struct rb_node *n;
Todd Kjosd325d372016-10-10 10:40:53 -07005079 int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005080
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005081 mutex_lock(&binder_procs_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005082 hlist_del(&proc->proc_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005083 mutex_unlock(&binder_procs_lock);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005084
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005085 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02005086 if (context->binder_context_mgr_node &&
5087 context->binder_context_mgr_node->proc == proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005088 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01005089 "%s: %d context_mgr_node gone\n",
5090 __func__, proc->pid);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02005091 context->binder_context_mgr_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005092 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005093 mutex_unlock(&context->context_mgr_node_lock);
Todd Kjosb4827902017-05-25 15:52:17 -07005094 binder_inner_proc_lock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07005095 /*
5096 * Make sure proc stays alive after we
5097 * remove all the threads
5098 */
5099 proc->tmp_ref++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005100
Todd Kjos2f993e22017-05-12 14:42:55 -07005101 proc->is_dead = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005102 threads = 0;
5103 active_transactions = 0;
5104 while ((n = rb_first(&proc->threads))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005105 struct binder_thread *thread;
5106
5107 thread = rb_entry(n, struct binder_thread, rb_node);
Todd Kjosb4827902017-05-25 15:52:17 -07005108 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005109 threads++;
Todd Kjos2f993e22017-05-12 14:42:55 -07005110 active_transactions += binder_thread_release(proc, thread);
Todd Kjosb4827902017-05-25 15:52:17 -07005111 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005112 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005113
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005114 nodes = 0;
5115 incoming_refs = 0;
5116 while ((n = rb_first(&proc->nodes))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005117 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005118
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005119 node = rb_entry(n, struct binder_node, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005120 nodes++;
Todd Kjosf22abc72017-05-09 11:08:05 -07005121 /*
5122 * take a temporary ref on the node before
5123 * calling binder_node_release() which will either
5124 * kfree() the node or call binder_put_node()
5125 */
Todd Kjos425d23f2017-06-12 12:07:26 -07005126 binder_inc_node_tmpref_ilocked(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005127 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjos425d23f2017-06-12 12:07:26 -07005128 binder_inner_proc_unlock(proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005129 incoming_refs = binder_node_release(node, incoming_refs);
Todd Kjos425d23f2017-06-12 12:07:26 -07005130 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005131 }
Todd Kjos425d23f2017-06-12 12:07:26 -07005132 binder_inner_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005133
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005134 outgoing_refs = 0;
Todd Kjos5346bf32016-10-20 16:43:34 -07005135 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005136 while ((n = rb_first(&proc->refs_by_desc))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005137 struct binder_ref *ref;
5138
5139 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005140 outgoing_refs++;
Todd Kjos5346bf32016-10-20 16:43:34 -07005141 binder_cleanup_ref_olocked(ref);
5142 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07005143 binder_free_ref(ref);
Todd Kjos5346bf32016-10-20 16:43:34 -07005144 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005145 }
Todd Kjos5346bf32016-10-20 16:43:34 -07005146 binder_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005147
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005148 binder_release_work(proc, &proc->todo);
5149 binder_release_work(proc, &proc->delivered_death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005150
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005151 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Todd Kjosd325d372016-10-10 10:40:53 -07005152 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01005153 __func__, proc->pid, threads, nodes, incoming_refs,
Todd Kjosd325d372016-10-10 10:40:53 -07005154 outgoing_refs, active_transactions);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005155
Todd Kjos2f993e22017-05-12 14:42:55 -07005156 binder_proc_dec_tmpref(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005157}
5158
5159static void binder_deferred_func(struct work_struct *work)
5160{
5161 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005162 int defer;
Seunghun Lee10f62862014-05-01 01:30:23 +09005163
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005164 do {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005165 mutex_lock(&binder_deferred_lock);
5166 if (!hlist_empty(&binder_deferred_list)) {
5167 proc = hlist_entry(binder_deferred_list.first,
5168 struct binder_proc, deferred_work_node);
5169 hlist_del_init(&proc->deferred_work_node);
5170 defer = proc->deferred_work;
5171 proc->deferred_work = 0;
5172 } else {
5173 proc = NULL;
5174 defer = 0;
5175 }
5176 mutex_unlock(&binder_deferred_lock);
5177
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005178 if (defer & BINDER_DEFERRED_FLUSH)
5179 binder_deferred_flush(proc);
5180
5181 if (defer & BINDER_DEFERRED_RELEASE)
5182 binder_deferred_release(proc); /* frees proc */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005183 } while (proc);
5184}
5185static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
5186
5187static void
5188binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
5189{
5190 mutex_lock(&binder_deferred_lock);
5191 proc->deferred_work |= defer;
5192 if (hlist_unhashed(&proc->deferred_work_node)) {
5193 hlist_add_head(&proc->deferred_work_node,
5194 &binder_deferred_list);
Bhaktipriya Shridhar1beba522016-08-13 22:16:24 +05305195 schedule_work(&binder_deferred_work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005196 }
5197 mutex_unlock(&binder_deferred_lock);
5198}
5199
Todd Kjos6d241a42017-04-21 14:32:11 -07005200static void print_binder_transaction_ilocked(struct seq_file *m,
5201 struct binder_proc *proc,
5202 const char *prefix,
5203 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005204{
Todd Kjos6d241a42017-04-21 14:32:11 -07005205 struct binder_proc *to_proc;
5206 struct binder_buffer *buffer = t->buffer;
5207
Todd Kjos2f993e22017-05-12 14:42:55 -07005208 spin_lock(&t->lock);
Todd Kjos6d241a42017-04-21 14:32:11 -07005209 to_proc = t->to_proc;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005210 seq_printf(m,
Martijn Coenen57b2ac62017-06-06 17:04:42 -07005211 "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %d:%d r%d",
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005212 prefix, t->debug_id, t,
5213 t->from ? t->from->proc->pid : 0,
5214 t->from ? t->from->pid : 0,
Todd Kjos6d241a42017-04-21 14:32:11 -07005215 to_proc ? to_proc->pid : 0,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005216 t->to_thread ? t->to_thread->pid : 0,
Martijn Coenen57b2ac62017-06-06 17:04:42 -07005217 t->code, t->flags, t->priority.sched_policy,
5218 t->priority.prio, t->need_reply);
Todd Kjos2f993e22017-05-12 14:42:55 -07005219 spin_unlock(&t->lock);
5220
Todd Kjos6d241a42017-04-21 14:32:11 -07005221 if (proc != to_proc) {
5222 /*
5223 * Can only safely deref buffer if we are holding the
5224 * correct proc inner lock for this node
5225 */
5226 seq_puts(m, "\n");
5227 return;
5228 }
5229
5230 if (buffer == NULL) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005231 seq_puts(m, " buffer free\n");
5232 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005233 }
Todd Kjos6d241a42017-04-21 14:32:11 -07005234 if (buffer->target_node)
5235 seq_printf(m, " node %d", buffer->target_node->debug_id);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005236 seq_printf(m, " size %zd:%zd data %p\n",
Todd Kjos6d241a42017-04-21 14:32:11 -07005237 buffer->data_size, buffer->offsets_size,
5238 buffer->data);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005239}
5240
Todd Kjos6d241a42017-04-21 14:32:11 -07005241static void print_binder_work_ilocked(struct seq_file *m,
5242 struct binder_proc *proc,
5243 const char *prefix,
5244 const char *transaction_prefix,
5245 struct binder_work *w)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005246{
5247 struct binder_node *node;
5248 struct binder_transaction *t;
5249
5250 switch (w->type) {
5251 case BINDER_WORK_TRANSACTION:
5252 t = container_of(w, struct binder_transaction, work);
Todd Kjos6d241a42017-04-21 14:32:11 -07005253 print_binder_transaction_ilocked(
5254 m, proc, transaction_prefix, t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005255 break;
Todd Kjos858b8da2017-04-21 17:35:12 -07005256 case BINDER_WORK_RETURN_ERROR: {
5257 struct binder_error *e = container_of(
5258 w, struct binder_error, work);
5259
5260 seq_printf(m, "%stransaction error: %u\n",
5261 prefix, e->cmd);
5262 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005263 case BINDER_WORK_TRANSACTION_COMPLETE:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005264 seq_printf(m, "%stransaction complete\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005265 break;
5266 case BINDER_WORK_NODE:
5267 node = container_of(w, struct binder_node, work);
Arve Hjønnevågda498892014-02-21 14:40:26 -08005268 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
5269 prefix, node->debug_id,
5270 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005271 break;
5272 case BINDER_WORK_DEAD_BINDER:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005273 seq_printf(m, "%shas dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005274 break;
5275 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005276 seq_printf(m, "%shas cleared dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005277 break;
5278 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005279 seq_printf(m, "%shas cleared death notification\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005280 break;
5281 default:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005282 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005283 break;
5284 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005285}
5286
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005287static void print_binder_thread_ilocked(struct seq_file *m,
5288 struct binder_thread *thread,
5289 int print_always)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005290{
5291 struct binder_transaction *t;
5292 struct binder_work *w;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005293 size_t start_pos = m->count;
5294 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005295
Todd Kjos2f993e22017-05-12 14:42:55 -07005296 seq_printf(m, " thread %d: l %02x need_return %d tr %d\n",
Todd Kjos6798e6d2017-01-06 14:19:25 -08005297 thread->pid, thread->looper,
Todd Kjos2f993e22017-05-12 14:42:55 -07005298 thread->looper_need_return,
5299 atomic_read(&thread->tmp_ref));
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005300 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005301 t = thread->transaction_stack;
5302 while (t) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005303 if (t->from == thread) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005304 print_binder_transaction_ilocked(m, thread->proc,
5305 " outgoing transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005306 t = t->from_parent;
5307 } else if (t->to_thread == thread) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005308 print_binder_transaction_ilocked(m, thread->proc,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005309 " incoming transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005310 t = t->to_parent;
5311 } else {
Todd Kjos6d241a42017-04-21 14:32:11 -07005312 print_binder_transaction_ilocked(m, thread->proc,
5313 " bad transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005314 t = NULL;
5315 }
5316 }
5317 list_for_each_entry(w, &thread->todo, entry) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005318 print_binder_work_ilocked(m, thread->proc, " ",
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005319 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005320 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005321 if (!print_always && m->count == header_pos)
5322 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005323}
5324
Todd Kjos425d23f2017-06-12 12:07:26 -07005325static void print_binder_node_nilocked(struct seq_file *m,
5326 struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005327{
5328 struct binder_ref *ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005329 struct binder_work *w;
5330 int count;
5331
5332 count = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08005333 hlist_for_each_entry(ref, &node->refs, node_entry)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005334 count++;
5335
Martijn Coenen6aac9792017-06-07 09:29:14 -07005336 seq_printf(m, " node %d: u%016llx c%016llx pri %d:%d hs %d hw %d ls %d lw %d is %d iw %d tr %d",
Arve Hjønnevågda498892014-02-21 14:40:26 -08005337 node->debug_id, (u64)node->ptr, (u64)node->cookie,
Martijn Coenen6aac9792017-06-07 09:29:14 -07005338 node->sched_policy, node->min_priority,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005339 node->has_strong_ref, node->has_weak_ref,
5340 node->local_strong_refs, node->local_weak_refs,
Todd Kjosf22abc72017-05-09 11:08:05 -07005341 node->internal_strong_refs, count, node->tmp_refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005342 if (count) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005343 seq_puts(m, " proc");
Sasha Levinb67bfe02013-02-27 17:06:00 -08005344 hlist_for_each_entry(ref, &node->refs, node_entry)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005345 seq_printf(m, " %d", ref->proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005346 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005347 seq_puts(m, "\n");
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005348 if (node->proc) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005349 list_for_each_entry(w, &node->async_todo, entry)
Todd Kjos6d241a42017-04-21 14:32:11 -07005350 print_binder_work_ilocked(m, node->proc, " ",
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005351 " pending async transaction", w);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005352 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005353}
5354
Todd Kjos5346bf32016-10-20 16:43:34 -07005355static void print_binder_ref_olocked(struct seq_file *m,
5356 struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005357{
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005358 binder_node_lock(ref->node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07005359 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
5360 ref->data.debug_id, ref->data.desc,
5361 ref->node->proc ? "" : "dead ",
5362 ref->node->debug_id, ref->data.strong,
5363 ref->data.weak, ref->death);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005364 binder_node_unlock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005365}
5366
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005367static void print_binder_proc(struct seq_file *m,
5368 struct binder_proc *proc, int print_all)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005369{
5370 struct binder_work *w;
5371 struct rb_node *n;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005372 size_t start_pos = m->count;
5373 size_t header_pos;
Todd Kjos425d23f2017-06-12 12:07:26 -07005374 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005375
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005376 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005377 seq_printf(m, "context %s\n", proc->context->name);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005378 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005379
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005380 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005381 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005382 print_binder_thread_ilocked(m, rb_entry(n, struct binder_thread,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005383 rb_node), print_all);
Todd Kjos425d23f2017-06-12 12:07:26 -07005384
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005385 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005386 struct binder_node *node = rb_entry(n, struct binder_node,
5387 rb_node);
Todd Kjos425d23f2017-06-12 12:07:26 -07005388 /*
5389 * take a temporary reference on the node so it
5390 * survives and isn't removed from the tree
5391 * while we print it.
5392 */
5393 binder_inc_node_tmpref_ilocked(node);
5394 /* Need to drop inner lock to take node lock */
5395 binder_inner_proc_unlock(proc);
5396 if (last_node)
5397 binder_put_node(last_node);
5398 binder_node_inner_lock(node);
5399 print_binder_node_nilocked(m, node);
5400 binder_node_inner_unlock(node);
5401 last_node = node;
5402 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005403 }
Todd Kjos425d23f2017-06-12 12:07:26 -07005404 binder_inner_proc_unlock(proc);
5405 if (last_node)
5406 binder_put_node(last_node);
5407
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005408 if (print_all) {
Todd Kjos5346bf32016-10-20 16:43:34 -07005409 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005410 for (n = rb_first(&proc->refs_by_desc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005411 n != NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005412 n = rb_next(n))
Todd Kjos5346bf32016-10-20 16:43:34 -07005413 print_binder_ref_olocked(m, rb_entry(n,
5414 struct binder_ref,
5415 rb_node_desc));
5416 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005417 }
Todd Kjosd325d372016-10-10 10:40:53 -07005418 binder_alloc_print_allocated(m, &proc->alloc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005419 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005420 list_for_each_entry(w, &proc->todo, entry)
Todd Kjos6d241a42017-04-21 14:32:11 -07005421 print_binder_work_ilocked(m, proc, " ",
5422 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005423 list_for_each_entry(w, &proc->delivered_death, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005424 seq_puts(m, " has delivered dead binder\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005425 break;
5426 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005427 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005428 if (!print_all && m->count == header_pos)
5429 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005430}
5431
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005432static const char * const binder_return_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005433 "BR_ERROR",
5434 "BR_OK",
5435 "BR_TRANSACTION",
5436 "BR_REPLY",
5437 "BR_ACQUIRE_RESULT",
5438 "BR_DEAD_REPLY",
5439 "BR_TRANSACTION_COMPLETE",
5440 "BR_INCREFS",
5441 "BR_ACQUIRE",
5442 "BR_RELEASE",
5443 "BR_DECREFS",
5444 "BR_ATTEMPT_ACQUIRE",
5445 "BR_NOOP",
5446 "BR_SPAWN_LOOPER",
5447 "BR_FINISHED",
5448 "BR_DEAD_BINDER",
5449 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
5450 "BR_FAILED_REPLY"
5451};
5452
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005453static const char * const binder_command_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005454 "BC_TRANSACTION",
5455 "BC_REPLY",
5456 "BC_ACQUIRE_RESULT",
5457 "BC_FREE_BUFFER",
5458 "BC_INCREFS",
5459 "BC_ACQUIRE",
5460 "BC_RELEASE",
5461 "BC_DECREFS",
5462 "BC_INCREFS_DONE",
5463 "BC_ACQUIRE_DONE",
5464 "BC_ATTEMPT_ACQUIRE",
5465 "BC_REGISTER_LOOPER",
5466 "BC_ENTER_LOOPER",
5467 "BC_EXIT_LOOPER",
5468 "BC_REQUEST_DEATH_NOTIFICATION",
5469 "BC_CLEAR_DEATH_NOTIFICATION",
Martijn Coenen5a6da532016-09-30 14:10:07 +02005470 "BC_DEAD_BINDER_DONE",
5471 "BC_TRANSACTION_SG",
5472 "BC_REPLY_SG",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005473};
5474
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005475static const char * const binder_objstat_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005476 "proc",
5477 "thread",
5478 "node",
5479 "ref",
5480 "death",
5481 "transaction",
5482 "transaction_complete"
5483};
5484
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005485static void print_binder_stats(struct seq_file *m, const char *prefix,
5486 struct binder_stats *stats)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005487{
5488 int i;
5489
5490 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005491 ARRAY_SIZE(binder_command_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005492 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005493 int temp = atomic_read(&stats->bc[i]);
5494
5495 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005496 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005497 binder_command_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005498 }
5499
5500 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005501 ARRAY_SIZE(binder_return_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005502 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005503 int temp = atomic_read(&stats->br[i]);
5504
5505 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005506 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005507 binder_return_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005508 }
5509
5510 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005511 ARRAY_SIZE(binder_objstat_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005512 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005513 ARRAY_SIZE(stats->obj_deleted));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005514 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005515 int created = atomic_read(&stats->obj_created[i]);
5516 int deleted = atomic_read(&stats->obj_deleted[i]);
5517
5518 if (created || deleted)
5519 seq_printf(m, "%s%s: active %d total %d\n",
5520 prefix,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005521 binder_objstat_strings[i],
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005522 created - deleted,
5523 created);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005524 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005525}
5526
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005527static void print_binder_proc_stats(struct seq_file *m,
5528 struct binder_proc *proc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005529{
5530 struct binder_work *w;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005531 struct binder_thread *thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005532 struct rb_node *n;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005533 int count, strong, weak, ready_threads;
Todd Kjosb4827902017-05-25 15:52:17 -07005534 size_t free_async_space =
5535 binder_alloc_get_free_async_space(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005536
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005537 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005538 seq_printf(m, "context %s\n", proc->context->name);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005539 count = 0;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005540 ready_threads = 0;
Todd Kjosb4827902017-05-25 15:52:17 -07005541 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005542 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
5543 count++;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005544
5545 list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node)
5546 ready_threads++;
5547
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005548 seq_printf(m, " threads: %d\n", count);
5549 seq_printf(m, " requested threads: %d+%d/%d\n"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005550 " ready threads %d\n"
5551 " free async space %zd\n", proc->requested_threads,
5552 proc->requested_threads_started, proc->max_threads,
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005553 ready_threads,
Todd Kjosb4827902017-05-25 15:52:17 -07005554 free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005555 count = 0;
5556 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
5557 count++;
Todd Kjos425d23f2017-06-12 12:07:26 -07005558 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005559 seq_printf(m, " nodes: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005560 count = 0;
5561 strong = 0;
5562 weak = 0;
Todd Kjos5346bf32016-10-20 16:43:34 -07005563 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005564 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
5565 struct binder_ref *ref = rb_entry(n, struct binder_ref,
5566 rb_node_desc);
5567 count++;
Todd Kjosb0117bb2017-05-08 09:16:27 -07005568 strong += ref->data.strong;
5569 weak += ref->data.weak;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005570 }
Todd Kjos5346bf32016-10-20 16:43:34 -07005571 binder_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005572 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005573
Todd Kjosd325d372016-10-10 10:40:53 -07005574 count = binder_alloc_get_allocated_count(&proc->alloc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005575 seq_printf(m, " buffers: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005576
Sherry Yang91004422017-08-22 17:26:57 -07005577 binder_alloc_print_pages(m, &proc->alloc);
5578
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005579 count = 0;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005580 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005581 list_for_each_entry(w, &proc->todo, entry) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005582 if (w->type == BINDER_WORK_TRANSACTION)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005583 count++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005584 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005585 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005586 seq_printf(m, " pending transactions: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005587
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005588 print_binder_stats(m, " ", &proc->stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005589}
5590
5591
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005592static int binder_state_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005593{
5594 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005595 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005596 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005597
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005598 seq_puts(m, "binder state:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005599
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005600 spin_lock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005601 if (!hlist_empty(&binder_dead_nodes))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005602 seq_puts(m, "dead nodes:\n");
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005603 hlist_for_each_entry(node, &binder_dead_nodes, dead_node) {
5604 /*
5605 * take a temporary reference on the node so it
5606 * survives and isn't removed from the list
5607 * while we print it.
5608 */
5609 node->tmp_refs++;
5610 spin_unlock(&binder_dead_nodes_lock);
5611 if (last_node)
5612 binder_put_node(last_node);
5613 binder_node_lock(node);
Todd Kjos425d23f2017-06-12 12:07:26 -07005614 print_binder_node_nilocked(m, node);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005615 binder_node_unlock(node);
5616 last_node = node;
5617 spin_lock(&binder_dead_nodes_lock);
5618 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005619 spin_unlock(&binder_dead_nodes_lock);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005620 if (last_node)
5621 binder_put_node(last_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005622
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005623 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005624 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005625 print_binder_proc(m, proc, 1);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005626 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005627
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005628 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005629}
5630
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005631static int binder_stats_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005632{
5633 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005634
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005635 seq_puts(m, "binder stats:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005636
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005637 print_binder_stats(m, "", &binder_stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005638
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005639 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005640 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005641 print_binder_proc_stats(m, proc);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005642 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005643
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005644 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005645}
5646
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005647static int binder_transactions_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005648{
5649 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005650
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005651 seq_puts(m, "binder transactions:\n");
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005652 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005653 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005654 print_binder_proc(m, proc, 0);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005655 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005656
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005657 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005658}
5659
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005660static int binder_proc_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005661{
Riley Andrews83050a42016-02-09 21:05:33 -08005662 struct binder_proc *itr;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005663 int pid = (unsigned long)m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005664
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005665 mutex_lock(&binder_procs_lock);
Riley Andrews83050a42016-02-09 21:05:33 -08005666 hlist_for_each_entry(itr, &binder_procs, proc_node) {
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005667 if (itr->pid == pid) {
5668 seq_puts(m, "binder proc state:\n");
5669 print_binder_proc(m, itr, 1);
Riley Andrews83050a42016-02-09 21:05:33 -08005670 }
5671 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005672 mutex_unlock(&binder_procs_lock);
5673
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005674 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005675}
5676
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005677static void print_binder_transaction_log_entry(struct seq_file *m,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005678 struct binder_transaction_log_entry *e)
5679{
Todd Kjos1cfe6272017-05-24 13:33:28 -07005680 int debug_id = READ_ONCE(e->debug_id_done);
5681 /*
5682 * read barrier to guarantee debug_id_done read before
5683 * we print the log values
5684 */
5685 smp_rmb();
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005686 seq_printf(m,
Todd Kjos1cfe6272017-05-24 13:33:28 -07005687 "%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 -07005688 e->debug_id, (e->call_type == 2) ? "reply" :
5689 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005690 e->from_thread, e->to_proc, e->to_thread, e->context_name,
Todd Kjose598d172017-03-22 17:19:52 -07005691 e->to_node, e->target_handle, e->data_size, e->offsets_size,
5692 e->return_error, e->return_error_param,
5693 e->return_error_line);
Todd Kjos1cfe6272017-05-24 13:33:28 -07005694 /*
5695 * read-barrier to guarantee read of debug_id_done after
5696 * done printing the fields of the entry
5697 */
5698 smp_rmb();
5699 seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ?
5700 "\n" : " (incomplete)\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005701}
5702
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005703static int binder_transaction_log_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005704{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005705 struct binder_transaction_log *log = m->private;
Todd Kjos1cfe6272017-05-24 13:33:28 -07005706 unsigned int log_cur = atomic_read(&log->cur);
5707 unsigned int count;
5708 unsigned int cur;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005709 int i;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005710
Todd Kjos1cfe6272017-05-24 13:33:28 -07005711 count = log_cur + 1;
5712 cur = count < ARRAY_SIZE(log->entry) && !log->full ?
5713 0 : count % ARRAY_SIZE(log->entry);
5714 if (count > ARRAY_SIZE(log->entry) || log->full)
5715 count = ARRAY_SIZE(log->entry);
5716 for (i = 0; i < count; i++) {
5717 unsigned int index = cur++ % ARRAY_SIZE(log->entry);
5718
5719 print_binder_transaction_log_entry(m, &log->entry[index]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005720 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005721 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005722}
5723
5724static const struct file_operations binder_fops = {
5725 .owner = THIS_MODULE,
5726 .poll = binder_poll,
5727 .unlocked_ioctl = binder_ioctl,
Arve Hjønnevågda498892014-02-21 14:40:26 -08005728 .compat_ioctl = binder_ioctl,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005729 .mmap = binder_mmap,
5730 .open = binder_open,
5731 .flush = binder_flush,
5732 .release = binder_release,
5733};
5734
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005735BINDER_DEBUG_ENTRY(state);
5736BINDER_DEBUG_ENTRY(stats);
5737BINDER_DEBUG_ENTRY(transactions);
5738BINDER_DEBUG_ENTRY(transaction_log);
5739
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005740static int __init init_binder_device(const char *name)
5741{
5742 int ret;
5743 struct binder_device *binder_device;
5744
5745 binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
5746 if (!binder_device)
5747 return -ENOMEM;
5748
5749 binder_device->miscdev.fops = &binder_fops;
5750 binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
5751 binder_device->miscdev.name = name;
5752
5753 binder_device->context.binder_context_mgr_uid = INVALID_UID;
5754 binder_device->context.name = name;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005755 mutex_init(&binder_device->context.context_mgr_node_lock);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005756
5757 ret = misc_register(&binder_device->miscdev);
5758 if (ret < 0) {
5759 kfree(binder_device);
5760 return ret;
5761 }
5762
5763 hlist_add_head(&binder_device->hlist, &binder_devices);
5764
5765 return ret;
5766}
5767
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005768static int __init binder_init(void)
5769{
5770 int ret;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005771 char *device_name, *device_names;
5772 struct binder_device *device;
5773 struct hlist_node *tmp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005774
Sherry Yang5828d702017-07-29 13:24:11 -07005775 binder_alloc_shrinker_init();
5776
Todd Kjos1cfe6272017-05-24 13:33:28 -07005777 atomic_set(&binder_transaction_log.cur, ~0U);
5778 atomic_set(&binder_transaction_log_failed.cur, ~0U);
5779
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005780 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
5781 if (binder_debugfs_dir_entry_root)
5782 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
5783 binder_debugfs_dir_entry_root);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005784
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005785 if (binder_debugfs_dir_entry_root) {
5786 debugfs_create_file("state",
5787 S_IRUGO,
5788 binder_debugfs_dir_entry_root,
5789 NULL,
5790 &binder_state_fops);
5791 debugfs_create_file("stats",
5792 S_IRUGO,
5793 binder_debugfs_dir_entry_root,
5794 NULL,
5795 &binder_stats_fops);
5796 debugfs_create_file("transactions",
5797 S_IRUGO,
5798 binder_debugfs_dir_entry_root,
5799 NULL,
5800 &binder_transactions_fops);
5801 debugfs_create_file("transaction_log",
5802 S_IRUGO,
5803 binder_debugfs_dir_entry_root,
5804 &binder_transaction_log,
5805 &binder_transaction_log_fops);
5806 debugfs_create_file("failed_transaction_log",
5807 S_IRUGO,
5808 binder_debugfs_dir_entry_root,
5809 &binder_transaction_log_failed,
5810 &binder_transaction_log_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005811 }
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005812
5813 /*
5814 * Copy the module_parameter string, because we don't want to
5815 * tokenize it in-place.
5816 */
5817 device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL);
5818 if (!device_names) {
5819 ret = -ENOMEM;
5820 goto err_alloc_device_names_failed;
5821 }
5822 strcpy(device_names, binder_devices_param);
5823
5824 while ((device_name = strsep(&device_names, ","))) {
5825 ret = init_binder_device(device_name);
5826 if (ret)
5827 goto err_init_binder_device_failed;
5828 }
5829
5830 return ret;
5831
5832err_init_binder_device_failed:
5833 hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
5834 misc_deregister(&device->miscdev);
5835 hlist_del(&device->hlist);
5836 kfree(device);
5837 }
5838err_alloc_device_names_failed:
5839 debugfs_remove_recursive(binder_debugfs_dir_entry_root);
5840
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005841 return ret;
5842}
5843
5844device_initcall(binder_init);
5845
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07005846#define CREATE_TRACE_POINTS
5847#include "binder_trace.h"
5848
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005849MODULE_LICENSE("GPL v2");