blob: 957eb3cfab7953f2df13d4a36bf979a183c88162 [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);
Martijn Coenenc4048b22018-03-28 11:14:50 +02003004 if (target_node && target_proc == proc) {
3005 binder_user_error("%d:%d got transaction to context manager from process owning it\n",
3006 proc->pid, thread->pid);
3007 return_error = BR_FAILED_REPLY;
3008 return_error_param = -EINVAL;
3009 return_error_line = __LINE__;
3010 goto err_invalid_target_handle;
3011 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003012 }
Todd Kjos291d9682017-09-25 08:55:09 -07003013 if (!target_node) {
3014 /*
3015 * return_error is set above
3016 */
3017 return_error_param = -EINVAL;
Todd Kjose598d172017-03-22 17:19:52 -07003018 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003019 goto err_dead_binder;
3020 }
Todd Kjos291d9682017-09-25 08:55:09 -07003021 e->to_node = target_node->debug_id;
Stephen Smalley79af7302015-01-21 10:54:10 -05003022 if (security_binder_transaction(proc->tsk,
3023 target_proc->tsk) < 0) {
3024 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003025 return_error_param = -EPERM;
3026 return_error_line = __LINE__;
Stephen Smalley79af7302015-01-21 10:54:10 -05003027 goto err_invalid_target_handle;
3028 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07003029 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003030 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
3031 struct binder_transaction *tmp;
Seunghun Lee10f62862014-05-01 01:30:23 +09003032
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003033 tmp = thread->transaction_stack;
3034 if (tmp->to_thread != thread) {
Todd Kjos2f993e22017-05-12 14:42:55 -07003035 spin_lock(&tmp->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05303036 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 +09003037 proc->pid, thread->pid, tmp->debug_id,
3038 tmp->to_proc ? tmp->to_proc->pid : 0,
3039 tmp->to_thread ?
3040 tmp->to_thread->pid : 0);
Todd Kjos2f993e22017-05-12 14:42:55 -07003041 spin_unlock(&tmp->lock);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003042 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003043 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003044 return_error_param = -EPROTO;
3045 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003046 goto err_bad_call_stack;
3047 }
3048 while (tmp) {
Todd Kjos2f993e22017-05-12 14:42:55 -07003049 struct binder_thread *from;
3050
3051 spin_lock(&tmp->lock);
3052 from = tmp->from;
3053 if (from && from->proc == target_proc) {
3054 atomic_inc(&from->tmp_ref);
3055 target_thread = from;
3056 spin_unlock(&tmp->lock);
3057 break;
3058 }
3059 spin_unlock(&tmp->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003060 tmp = tmp->from_parent;
3061 }
3062 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07003063 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003064 }
Martijn Coenen053be422017-06-06 15:17:46 -07003065 if (target_thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003066 e->to_thread = target_thread->pid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003067 e->to_proc = target_proc->pid;
3068
3069 /* TODO: reuse incoming transaction for reply */
3070 t = kzalloc(sizeof(*t), GFP_KERNEL);
3071 if (t == NULL) {
3072 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003073 return_error_param = -ENOMEM;
3074 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003075 goto err_alloc_t_failed;
3076 }
3077 binder_stats_created(BINDER_STAT_TRANSACTION);
Todd Kjos2f993e22017-05-12 14:42:55 -07003078 spin_lock_init(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003079
3080 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
3081 if (tcomplete == NULL) {
3082 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003083 return_error_param = -ENOMEM;
3084 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003085 goto err_alloc_tcomplete_failed;
3086 }
3087 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
3088
Todd Kjos1cfe6272017-05-24 13:33:28 -07003089 t->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003090
3091 if (reply)
3092 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen59878d72016-09-30 14:05:40 +02003093 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003094 proc->pid, thread->pid, t->debug_id,
3095 target_proc->pid, target_thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003096 (u64)tr->data.ptr.buffer,
3097 (u64)tr->data.ptr.offsets,
Martijn Coenen59878d72016-09-30 14:05:40 +02003098 (u64)tr->data_size, (u64)tr->offsets_size,
3099 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003100 else
3101 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen59878d72016-09-30 14:05:40 +02003102 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003103 proc->pid, thread->pid, t->debug_id,
3104 target_proc->pid, target_node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003105 (u64)tr->data.ptr.buffer,
3106 (u64)tr->data.ptr.offsets,
Martijn Coenen59878d72016-09-30 14:05:40 +02003107 (u64)tr->data_size, (u64)tr->offsets_size,
3108 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003109
3110 if (!reply && !(tr->flags & TF_ONE_WAY))
3111 t->from = thread;
3112 else
3113 t->from = NULL;
Tair Rzayev57bab7c2014-05-31 22:43:34 +03003114 t->sender_euid = task_euid(proc->tsk);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003115 t->to_proc = target_proc;
3116 t->to_thread = target_thread;
3117 t->code = tr->code;
3118 t->flags = tr->flags;
Martijn Coenen57b2ac62017-06-06 17:04:42 -07003119 if (!(t->flags & TF_ONE_WAY) &&
3120 binder_supported_policy(current->policy)) {
3121 /* Inherit supported policies for synchronous transactions */
3122 t->priority.sched_policy = current->policy;
3123 t->priority.prio = current->normal_prio;
3124 } else {
3125 /* Otherwise, fall back to the default priority */
3126 t->priority = target_proc->default_priority;
3127 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003128
3129 trace_binder_transaction(reply, t, target_node);
3130
Todd Kjosd325d372016-10-10 10:40:53 -07003131 t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size,
Martijn Coenen59878d72016-09-30 14:05:40 +02003132 tr->offsets_size, extra_buffers_size,
3133 !reply && (t->flags & TF_ONE_WAY));
Todd Kjose598d172017-03-22 17:19:52 -07003134 if (IS_ERR(t->buffer)) {
3135 /*
3136 * -ESRCH indicates VMA cleared. The target is dying.
3137 */
3138 return_error_param = PTR_ERR(t->buffer);
3139 return_error = return_error_param == -ESRCH ?
3140 BR_DEAD_REPLY : BR_FAILED_REPLY;
3141 return_error_line = __LINE__;
3142 t->buffer = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003143 goto err_binder_alloc_buf_failed;
3144 }
3145 t->buffer->allow_user_free = 0;
3146 t->buffer->debug_id = t->debug_id;
3147 t->buffer->transaction = t;
3148 t->buffer->target_node = target_node;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003149 trace_binder_transaction_alloc_buf(t->buffer);
Martijn Coenen5a6da532016-09-30 14:10:07 +02003150 off_start = (binder_size_t *)(t->buffer->data +
3151 ALIGN(tr->data_size, sizeof(void *)));
3152 offp = off_start;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003153
Arve Hjønnevågda498892014-02-21 14:40:26 -08003154 if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t)
3155 tr->data.ptr.buffer, tr->data_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303156 binder_user_error("%d:%d got transaction with invalid data ptr\n",
3157 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003158 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003159 return_error_param = -EFAULT;
3160 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003161 goto err_copy_data_failed;
3162 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08003163 if (copy_from_user(offp, (const void __user *)(uintptr_t)
3164 tr->data.ptr.offsets, tr->offsets_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303165 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3166 proc->pid, thread->pid);
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 = -EFAULT;
3169 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003170 goto err_copy_data_failed;
3171 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08003172 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
3173 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
3174 proc->pid, thread->pid, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003175 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003176 return_error_param = -EINVAL;
3177 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003178 goto err_bad_offset;
3179 }
Martijn Coenen5a6da532016-09-30 14:10:07 +02003180 if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) {
3181 binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
3182 proc->pid, thread->pid,
Amit Pundir44cbb182017-02-01 12:53:45 +05303183 (u64)extra_buffers_size);
Martijn Coenen5a6da532016-09-30 14:10:07 +02003184 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003185 return_error_param = -EINVAL;
3186 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003187 goto err_bad_offset;
3188 }
3189 off_end = (void *)off_start + tr->offsets_size;
3190 sg_bufp = (u8 *)(PTR_ALIGN(off_end, sizeof(void *)));
3191 sg_buf_end = sg_bufp + extra_buffers_size;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08003192 off_min = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003193 for (; offp < off_end; offp++) {
Martijn Coenen00c80372016-07-13 12:06:49 +02003194 struct binder_object_header *hdr;
3195 size_t object_size = binder_validate_object(t->buffer, *offp);
Seunghun Lee10f62862014-05-01 01:30:23 +09003196
Martijn Coenen00c80372016-07-13 12:06:49 +02003197 if (object_size == 0 || *offp < off_min) {
3198 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 -08003199 proc->pid, thread->pid, (u64)*offp,
3200 (u64)off_min,
Martijn Coenen00c80372016-07-13 12:06:49 +02003201 (u64)t->buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003202 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003203 return_error_param = -EINVAL;
3204 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003205 goto err_bad_offset;
3206 }
Martijn Coenen00c80372016-07-13 12:06:49 +02003207
3208 hdr = (struct binder_object_header *)(t->buffer->data + *offp);
3209 off_min = *offp + object_size;
3210 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003211 case BINDER_TYPE_BINDER:
3212 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003213 struct flat_binder_object *fp;
Seunghun Lee10f62862014-05-01 01:30:23 +09003214
Martijn Coenen00c80372016-07-13 12:06:49 +02003215 fp = to_flat_binder_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003216 ret = binder_translate_binder(fp, t, thread);
3217 if (ret < 0) {
Christian Engelmayer7d420432014-05-07 21:44:53 +02003218 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003219 return_error_param = ret;
3220 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003221 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003222 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003223 } break;
3224 case BINDER_TYPE_HANDLE:
3225 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003226 struct flat_binder_object *fp;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02003227
Martijn Coenen00c80372016-07-13 12:06:49 +02003228 fp = to_flat_binder_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003229 ret = binder_translate_handle(fp, t, thread);
3230 if (ret < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003231 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003232 return_error_param = ret;
3233 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003234 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003235 }
3236 } break;
3237
3238 case BINDER_TYPE_FD: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003239 struct binder_fd_object *fp = to_binder_fd_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003240 int target_fd = binder_translate_fd(fp->fd, t, thread,
3241 in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003242
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003243 if (target_fd < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003244 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003245 return_error_param = target_fd;
3246 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003247 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003248 }
Martijn Coenen00c80372016-07-13 12:06:49 +02003249 fp->pad_binder = 0;
3250 fp->fd = target_fd;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003251 } break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003252 case BINDER_TYPE_FDA: {
3253 struct binder_fd_array_object *fda =
3254 to_binder_fd_array_object(hdr);
3255 struct binder_buffer_object *parent =
3256 binder_validate_ptr(t->buffer, fda->parent,
3257 off_start,
3258 offp - off_start);
3259 if (!parent) {
3260 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
3261 proc->pid, thread->pid);
3262 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003263 return_error_param = -EINVAL;
3264 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003265 goto err_bad_parent;
3266 }
3267 if (!binder_validate_fixup(t->buffer, off_start,
3268 parent, fda->parent_offset,
3269 last_fixup_obj,
3270 last_fixup_min_off)) {
3271 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
3272 proc->pid, thread->pid);
3273 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003274 return_error_param = -EINVAL;
3275 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003276 goto err_bad_parent;
3277 }
3278 ret = binder_translate_fd_array(fda, parent, t, thread,
3279 in_reply_to);
3280 if (ret < 0) {
3281 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003282 return_error_param = ret;
3283 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003284 goto err_translate_failed;
3285 }
3286 last_fixup_obj = parent;
3287 last_fixup_min_off =
3288 fda->parent_offset + sizeof(u32) * fda->num_fds;
3289 } break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003290 case BINDER_TYPE_PTR: {
3291 struct binder_buffer_object *bp =
3292 to_binder_buffer_object(hdr);
3293 size_t buf_left = sg_buf_end - sg_bufp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003294
Martijn Coenen5a6da532016-09-30 14:10:07 +02003295 if (bp->length > buf_left) {
3296 binder_user_error("%d:%d got transaction with too large buffer\n",
3297 proc->pid, thread->pid);
3298 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003299 return_error_param = -EINVAL;
3300 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003301 goto err_bad_offset;
3302 }
3303 if (copy_from_user(sg_bufp,
3304 (const void __user *)(uintptr_t)
3305 bp->buffer, bp->length)) {
3306 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3307 proc->pid, thread->pid);
Todd Kjose598d172017-03-22 17:19:52 -07003308 return_error_param = -EFAULT;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003309 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003310 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003311 goto err_copy_data_failed;
3312 }
3313 /* Fixup buffer pointer to target proc address space */
3314 bp->buffer = (uintptr_t)sg_bufp +
Todd Kjosd325d372016-10-10 10:40:53 -07003315 binder_alloc_get_user_buffer_offset(
3316 &target_proc->alloc);
Martijn Coenen5a6da532016-09-30 14:10:07 +02003317 sg_bufp += ALIGN(bp->length, sizeof(u64));
3318
3319 ret = binder_fixup_parent(t, thread, bp, off_start,
3320 offp - off_start,
3321 last_fixup_obj,
3322 last_fixup_min_off);
3323 if (ret < 0) {
3324 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003325 return_error_param = ret;
3326 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003327 goto err_translate_failed;
3328 }
3329 last_fixup_obj = bp;
3330 last_fixup_min_off = 0;
3331 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003332 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01003333 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
Martijn Coenen00c80372016-07-13 12:06:49 +02003334 proc->pid, thread->pid, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003335 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003336 return_error_param = -EINVAL;
3337 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003338 goto err_bad_object_type;
3339 }
3340 }
Todd Kjos8dedb0c2017-05-09 08:31:32 -07003341 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003342 t->work.type = BINDER_WORK_TRANSACTION;
Todd Kjos8dedb0c2017-05-09 08:31:32 -07003343
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003344 if (reply) {
Martijn Coenen1af61802017-10-19 15:04:46 +02003345 binder_enqueue_thread_work(thread, tcomplete);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003346 binder_inner_proc_lock(target_proc);
3347 if (target_thread->is_dead) {
3348 binder_inner_proc_unlock(target_proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07003349 goto err_dead_proc_or_thread;
Martijn Coenen995a36e2017-06-02 13:36:52 -07003350 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003351 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003352 binder_pop_transaction_ilocked(target_thread, in_reply_to);
Martijn Coenen1af61802017-10-19 15:04:46 +02003353 binder_enqueue_thread_work_ilocked(target_thread, &t->work);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003354 binder_inner_proc_unlock(target_proc);
Martijn Coenen053be422017-06-06 15:17:46 -07003355 wake_up_interruptible_sync(&target_thread->wait);
Martijn Coenenecd972d2017-05-26 10:48:56 -07003356 binder_restore_priority(current, in_reply_to->saved_priority);
Todd Kjos21ef40a2017-03-30 18:02:13 -07003357 binder_free_transaction(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003358 } else if (!(t->flags & TF_ONE_WAY)) {
3359 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003360 binder_inner_proc_lock(proc);
Martijn Coenendac2e9c2017-11-13 09:55:21 +01003361 /*
3362 * Defer the TRANSACTION_COMPLETE, so we don't return to
3363 * userspace immediately; this allows the target process to
3364 * immediately start processing this transaction, reducing
3365 * latency. We will then return the TRANSACTION_COMPLETE when
3366 * the target replies (or there is an error).
3367 */
3368 binder_enqueue_deferred_thread_work_ilocked(thread, tcomplete);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003369 t->need_reply = 1;
3370 t->from_parent = thread->transaction_stack;
3371 thread->transaction_stack = t;
Martijn Coenen995a36e2017-06-02 13:36:52 -07003372 binder_inner_proc_unlock(proc);
Martijn Coenen053be422017-06-06 15:17:46 -07003373 if (!binder_proc_transaction(t, target_proc, target_thread)) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07003374 binder_inner_proc_lock(proc);
3375 binder_pop_transaction_ilocked(thread, t);
3376 binder_inner_proc_unlock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07003377 goto err_dead_proc_or_thread;
3378 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003379 } else {
3380 BUG_ON(target_node == NULL);
3381 BUG_ON(t->buffer->async_transaction != 1);
Martijn Coenen1af61802017-10-19 15:04:46 +02003382 binder_enqueue_thread_work(thread, tcomplete);
Martijn Coenen053be422017-06-06 15:17:46 -07003383 if (!binder_proc_transaction(t, target_proc, NULL))
Todd Kjos2f993e22017-05-12 14:42:55 -07003384 goto err_dead_proc_or_thread;
Riley Andrewsb5968812015-09-01 12:42:07 -07003385 }
Todd Kjos2f993e22017-05-12 14:42:55 -07003386 if (target_thread)
3387 binder_thread_dec_tmpref(target_thread);
3388 binder_proc_dec_tmpref(target_proc);
Todd Kjos291d9682017-09-25 08:55:09 -07003389 if (target_node)
3390 binder_dec_node_tmpref(target_node);
Todd Kjos1cfe6272017-05-24 13:33:28 -07003391 /*
3392 * write barrier to synchronize with initialization
3393 * of log entry
3394 */
3395 smp_wmb();
3396 WRITE_ONCE(e->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003397 return;
3398
Todd Kjos2f993e22017-05-12 14:42:55 -07003399err_dead_proc_or_thread:
3400 return_error = BR_DEAD_REPLY;
3401 return_error_line = __LINE__;
Xu YiPing86578a02017-05-22 11:26:23 -07003402 binder_dequeue_work(proc, tcomplete);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003403err_translate_failed:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003404err_bad_object_type:
3405err_bad_offset:
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003406err_bad_parent:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003407err_copy_data_failed:
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003408 trace_binder_transaction_failed_buffer_release(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003409 binder_transaction_buffer_release(target_proc, t->buffer, offp);
Todd Kjos291d9682017-09-25 08:55:09 -07003410 if (target_node)
3411 binder_dec_node_tmpref(target_node);
Todd Kjosc37162d2017-05-26 11:56:29 -07003412 target_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003413 t->buffer->transaction = NULL;
Todd Kjosd325d372016-10-10 10:40:53 -07003414 binder_alloc_free_buf(&target_proc->alloc, t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003415err_binder_alloc_buf_failed:
3416 kfree(tcomplete);
3417 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3418err_alloc_tcomplete_failed:
3419 kfree(t);
3420 binder_stats_deleted(BINDER_STAT_TRANSACTION);
3421err_alloc_t_failed:
3422err_bad_call_stack:
3423err_empty_call_stack:
3424err_dead_binder:
3425err_invalid_target_handle:
Todd Kjos2f993e22017-05-12 14:42:55 -07003426 if (target_thread)
3427 binder_thread_dec_tmpref(target_thread);
3428 if (target_proc)
3429 binder_proc_dec_tmpref(target_proc);
Todd Kjos291d9682017-09-25 08:55:09 -07003430 if (target_node) {
Todd Kjosc37162d2017-05-26 11:56:29 -07003431 binder_dec_node(target_node, 1, 0);
Todd Kjos291d9682017-09-25 08:55:09 -07003432 binder_dec_node_tmpref(target_node);
3433 }
Todd Kjosc37162d2017-05-26 11:56:29 -07003434
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003435 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Todd Kjose598d172017-03-22 17:19:52 -07003436 "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
3437 proc->pid, thread->pid, return_error, return_error_param,
3438 (u64)tr->data_size, (u64)tr->offsets_size,
3439 return_error_line);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003440
3441 {
3442 struct binder_transaction_log_entry *fe;
Seunghun Lee10f62862014-05-01 01:30:23 +09003443
Todd Kjose598d172017-03-22 17:19:52 -07003444 e->return_error = return_error;
3445 e->return_error_param = return_error_param;
3446 e->return_error_line = return_error_line;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003447 fe = binder_transaction_log_add(&binder_transaction_log_failed);
3448 *fe = *e;
Todd Kjos1cfe6272017-05-24 13:33:28 -07003449 /*
3450 * write barrier to synchronize with initialization
3451 * of log entry
3452 */
3453 smp_wmb();
3454 WRITE_ONCE(e->debug_id_done, t_debug_id);
3455 WRITE_ONCE(fe->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003456 }
3457
Todd Kjos858b8da2017-04-21 17:35:12 -07003458 BUG_ON(thread->return_error.cmd != BR_OK);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003459 if (in_reply_to) {
Martijn Coenenecd972d2017-05-26 10:48:56 -07003460 binder_restore_priority(current, in_reply_to->saved_priority);
Todd Kjos858b8da2017-04-21 17:35:12 -07003461 thread->return_error.cmd = BR_TRANSACTION_COMPLETE;
Martijn Coenen1af61802017-10-19 15:04:46 +02003462 binder_enqueue_thread_work(thread, &thread->return_error.work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003463 binder_send_failed_reply(in_reply_to, return_error);
Todd Kjos858b8da2017-04-21 17:35:12 -07003464 } else {
3465 thread->return_error.cmd = return_error;
Martijn Coenen1af61802017-10-19 15:04:46 +02003466 binder_enqueue_thread_work(thread, &thread->return_error.work);
Todd Kjos858b8da2017-04-21 17:35:12 -07003467 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003468}
3469
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02003470static int binder_thread_write(struct binder_proc *proc,
3471 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003472 binder_uintptr_t binder_buffer, size_t size,
3473 binder_size_t *consumed)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003474{
3475 uint32_t cmd;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003476 struct binder_context *context = proc->context;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003477 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003478 void __user *ptr = buffer + *consumed;
3479 void __user *end = buffer + size;
3480
Todd Kjos858b8da2017-04-21 17:35:12 -07003481 while (ptr < end && thread->return_error.cmd == BR_OK) {
Todd Kjosb0117bb2017-05-08 09:16:27 -07003482 int ret;
3483
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003484 if (get_user(cmd, (uint32_t __user *)ptr))
3485 return -EFAULT;
3486 ptr += sizeof(uint32_t);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003487 trace_binder_command(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003488 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07003489 atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]);
3490 atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]);
3491 atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003492 }
3493 switch (cmd) {
3494 case BC_INCREFS:
3495 case BC_ACQUIRE:
3496 case BC_RELEASE:
3497 case BC_DECREFS: {
3498 uint32_t target;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003499 const char *debug_string;
Todd Kjosb0117bb2017-05-08 09:16:27 -07003500 bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE;
3501 bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE;
3502 struct binder_ref_data rdata;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003503
3504 if (get_user(target, (uint32_t __user *)ptr))
3505 return -EFAULT;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003506
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003507 ptr += sizeof(uint32_t);
Todd Kjosb0117bb2017-05-08 09:16:27 -07003508 ret = -1;
3509 if (increment && !target) {
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003510 struct binder_node *ctx_mgr_node;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003511 mutex_lock(&context->context_mgr_node_lock);
3512 ctx_mgr_node = context->binder_context_mgr_node;
Todd Kjosb0117bb2017-05-08 09:16:27 -07003513 if (ctx_mgr_node)
3514 ret = binder_inc_ref_for_node(
3515 proc, ctx_mgr_node,
3516 strong, NULL, &rdata);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003517 mutex_unlock(&context->context_mgr_node_lock);
3518 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07003519 if (ret)
3520 ret = binder_update_ref_for_handle(
3521 proc, target, increment, strong,
3522 &rdata);
3523 if (!ret && rdata.desc != target) {
3524 binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
3525 proc->pid, thread->pid,
3526 target, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003527 }
3528 switch (cmd) {
3529 case BC_INCREFS:
3530 debug_string = "IncRefs";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003531 break;
3532 case BC_ACQUIRE:
3533 debug_string = "Acquire";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003534 break;
3535 case BC_RELEASE:
3536 debug_string = "Release";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003537 break;
3538 case BC_DECREFS:
3539 default:
3540 debug_string = "DecRefs";
Todd Kjosb0117bb2017-05-08 09:16:27 -07003541 break;
3542 }
3543 if (ret) {
3544 binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
3545 proc->pid, thread->pid, debug_string,
3546 strong, target, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003547 break;
3548 }
3549 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjosb0117bb2017-05-08 09:16:27 -07003550 "%d:%d %s ref %d desc %d s %d w %d\n",
3551 proc->pid, thread->pid, debug_string,
3552 rdata.debug_id, rdata.desc, rdata.strong,
3553 rdata.weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003554 break;
3555 }
3556 case BC_INCREFS_DONE:
3557 case BC_ACQUIRE_DONE: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003558 binder_uintptr_t node_ptr;
3559 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003560 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003561 bool free_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003562
Arve Hjønnevågda498892014-02-21 14:40:26 -08003563 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003564 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003565 ptr += sizeof(binder_uintptr_t);
3566 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003567 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003568 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003569 node = binder_get_node(proc, node_ptr);
3570 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003571 binder_user_error("%d:%d %s u%016llx no match\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003572 proc->pid, thread->pid,
3573 cmd == BC_INCREFS_DONE ?
3574 "BC_INCREFS_DONE" :
3575 "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003576 (u64)node_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003577 break;
3578 }
3579 if (cookie != node->cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003580 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003581 proc->pid, thread->pid,
3582 cmd == BC_INCREFS_DONE ?
3583 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003584 (u64)node_ptr, node->debug_id,
3585 (u64)cookie, (u64)node->cookie);
Todd Kjosf22abc72017-05-09 11:08:05 -07003586 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003587 break;
3588 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003589 binder_node_inner_lock(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003590 if (cmd == BC_ACQUIRE_DONE) {
3591 if (node->pending_strong_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303592 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003593 proc->pid, thread->pid,
3594 node->debug_id);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003595 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003596 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003597 break;
3598 }
3599 node->pending_strong_ref = 0;
3600 } else {
3601 if (node->pending_weak_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303602 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003603 proc->pid, thread->pid,
3604 node->debug_id);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003605 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003606 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003607 break;
3608 }
3609 node->pending_weak_ref = 0;
3610 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003611 free_node = binder_dec_node_nilocked(node,
3612 cmd == BC_ACQUIRE_DONE, 0);
3613 WARN_ON(free_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003614 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjosf22abc72017-05-09 11:08:05 -07003615 "%d:%d %s node %d ls %d lw %d tr %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003616 proc->pid, thread->pid,
3617 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Todd Kjosf22abc72017-05-09 11:08:05 -07003618 node->debug_id, node->local_strong_refs,
3619 node->local_weak_refs, node->tmp_refs);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003620 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003621 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003622 break;
3623 }
3624 case BC_ATTEMPT_ACQUIRE:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303625 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003626 return -EINVAL;
3627 case BC_ACQUIRE_RESULT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303628 pr_err("BC_ACQUIRE_RESULT not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003629 return -EINVAL;
3630
3631 case BC_FREE_BUFFER: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003632 binder_uintptr_t data_ptr;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003633 struct binder_buffer *buffer;
3634
Arve Hjønnevågda498892014-02-21 14:40:26 -08003635 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003636 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003637 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003638
Todd Kjos076072a2017-04-21 14:32:11 -07003639 buffer = binder_alloc_prepare_to_free(&proc->alloc,
3640 data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003641 if (buffer == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003642 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
3643 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003644 break;
3645 }
3646 if (!buffer->allow_user_free) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003647 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
3648 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003649 break;
3650 }
3651 binder_debug(BINDER_DEBUG_FREE_BUFFER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003652 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
3653 proc->pid, thread->pid, (u64)data_ptr,
3654 buffer->debug_id,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003655 buffer->transaction ? "active" : "finished");
3656
3657 if (buffer->transaction) {
3658 buffer->transaction->buffer = NULL;
3659 buffer->transaction = NULL;
3660 }
3661 if (buffer->async_transaction && buffer->target_node) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003662 struct binder_node *buf_node;
3663 struct binder_work *w;
3664
3665 buf_node = buffer->target_node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003666 binder_node_inner_lock(buf_node);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003667 BUG_ON(!buf_node->has_async_transaction);
3668 BUG_ON(buf_node->proc != proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003669 w = binder_dequeue_work_head_ilocked(
3670 &buf_node->async_todo);
Martijn Coenen4501c042017-08-10 13:56:16 +02003671 if (!w) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003672 buf_node->has_async_transaction = 0;
Martijn Coenen4501c042017-08-10 13:56:16 +02003673 } else {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003674 binder_enqueue_work_ilocked(
Martijn Coenen4501c042017-08-10 13:56:16 +02003675 w, &proc->todo);
3676 binder_wakeup_proc_ilocked(proc);
3677 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003678 binder_node_inner_unlock(buf_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003679 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003680 trace_binder_transaction_buffer_release(buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003681 binder_transaction_buffer_release(proc, buffer, NULL);
Todd Kjosd325d372016-10-10 10:40:53 -07003682 binder_alloc_free_buf(&proc->alloc, buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003683 break;
3684 }
3685
Martijn Coenen5a6da532016-09-30 14:10:07 +02003686 case BC_TRANSACTION_SG:
3687 case BC_REPLY_SG: {
3688 struct binder_transaction_data_sg tr;
3689
3690 if (copy_from_user(&tr, ptr, sizeof(tr)))
3691 return -EFAULT;
3692 ptr += sizeof(tr);
3693 binder_transaction(proc, thread, &tr.transaction_data,
3694 cmd == BC_REPLY_SG, tr.buffers_size);
3695 break;
3696 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003697 case BC_TRANSACTION:
3698 case BC_REPLY: {
3699 struct binder_transaction_data tr;
3700
3701 if (copy_from_user(&tr, ptr, sizeof(tr)))
3702 return -EFAULT;
3703 ptr += sizeof(tr);
Martijn Coenen59878d72016-09-30 14:05:40 +02003704 binder_transaction(proc, thread, &tr,
3705 cmd == BC_REPLY, 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003706 break;
3707 }
3708
3709 case BC_REGISTER_LOOPER:
3710 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303711 "%d:%d BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003712 proc->pid, thread->pid);
Todd Kjosd600e902017-05-25 17:35:02 -07003713 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003714 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
3715 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303716 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003717 proc->pid, thread->pid);
3718 } else if (proc->requested_threads == 0) {
3719 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303720 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003721 proc->pid, thread->pid);
3722 } else {
3723 proc->requested_threads--;
3724 proc->requested_threads_started++;
3725 }
3726 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
Todd Kjosd600e902017-05-25 17:35:02 -07003727 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003728 break;
3729 case BC_ENTER_LOOPER:
3730 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303731 "%d:%d BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003732 proc->pid, thread->pid);
3733 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
3734 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303735 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003736 proc->pid, thread->pid);
3737 }
3738 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
3739 break;
3740 case BC_EXIT_LOOPER:
3741 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303742 "%d:%d BC_EXIT_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003743 proc->pid, thread->pid);
3744 thread->looper |= BINDER_LOOPER_STATE_EXITED;
3745 break;
3746
3747 case BC_REQUEST_DEATH_NOTIFICATION:
3748 case BC_CLEAR_DEATH_NOTIFICATION: {
3749 uint32_t target;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003750 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003751 struct binder_ref *ref;
Todd Kjos5346bf32016-10-20 16:43:34 -07003752 struct binder_ref_death *death = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003753
3754 if (get_user(target, (uint32_t __user *)ptr))
3755 return -EFAULT;
3756 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08003757 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003758 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003759 ptr += sizeof(binder_uintptr_t);
Todd Kjos5346bf32016-10-20 16:43:34 -07003760 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3761 /*
3762 * Allocate memory for death notification
3763 * before taking lock
3764 */
3765 death = kzalloc(sizeof(*death), GFP_KERNEL);
3766 if (death == NULL) {
3767 WARN_ON(thread->return_error.cmd !=
3768 BR_OK);
3769 thread->return_error.cmd = BR_ERROR;
Martijn Coenen1af61802017-10-19 15:04:46 +02003770 binder_enqueue_thread_work(
3771 thread,
3772 &thread->return_error.work);
Todd Kjos5346bf32016-10-20 16:43:34 -07003773 binder_debug(
3774 BINDER_DEBUG_FAILED_TRANSACTION,
3775 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
3776 proc->pid, thread->pid);
3777 break;
3778 }
3779 }
3780 binder_proc_lock(proc);
3781 ref = binder_get_ref_olocked(proc, target, false);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003782 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303783 binder_user_error("%d:%d %s invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003784 proc->pid, thread->pid,
3785 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3786 "BC_REQUEST_DEATH_NOTIFICATION" :
3787 "BC_CLEAR_DEATH_NOTIFICATION",
3788 target);
Todd Kjos5346bf32016-10-20 16:43:34 -07003789 binder_proc_unlock(proc);
3790 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003791 break;
3792 }
3793
3794 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003795 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003796 proc->pid, thread->pid,
3797 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3798 "BC_REQUEST_DEATH_NOTIFICATION" :
3799 "BC_CLEAR_DEATH_NOTIFICATION",
Todd Kjosb0117bb2017-05-08 09:16:27 -07003800 (u64)cookie, ref->data.debug_id,
3801 ref->data.desc, ref->data.strong,
3802 ref->data.weak, ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003803
Martijn Coenenf9eac642017-05-22 11:26:23 -07003804 binder_node_lock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003805 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3806 if (ref->death) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303807 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003808 proc->pid, thread->pid);
Martijn Coenenf9eac642017-05-22 11:26:23 -07003809 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003810 binder_proc_unlock(proc);
3811 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003812 break;
3813 }
3814 binder_stats_created(BINDER_STAT_DEATH);
3815 INIT_LIST_HEAD(&death->work.entry);
3816 death->cookie = cookie;
3817 ref->death = death;
3818 if (ref->node->proc == NULL) {
3819 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
Martijn Coenen3bdbe4c2017-08-10 13:50:52 +02003820
3821 binder_inner_proc_lock(proc);
3822 binder_enqueue_work_ilocked(
3823 &ref->death->work, &proc->todo);
3824 binder_wakeup_proc_ilocked(proc);
3825 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003826 }
3827 } else {
3828 if (ref->death == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303829 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003830 proc->pid, thread->pid);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003831 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003832 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003833 break;
3834 }
3835 death = ref->death;
3836 if (death->cookie != cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003837 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003838 proc->pid, thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003839 (u64)death->cookie,
3840 (u64)cookie);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003841 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003842 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003843 break;
3844 }
3845 ref->death = NULL;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003846 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003847 if (list_empty(&death->work.entry)) {
3848 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003849 if (thread->looper &
3850 (BINDER_LOOPER_STATE_REGISTERED |
3851 BINDER_LOOPER_STATE_ENTERED))
Martijn Coenen1af61802017-10-19 15:04:46 +02003852 binder_enqueue_thread_work_ilocked(
3853 thread,
3854 &death->work);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003855 else {
3856 binder_enqueue_work_ilocked(
3857 &death->work,
3858 &proc->todo);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07003859 binder_wakeup_proc_ilocked(
Martijn Coenen053be422017-06-06 15:17:46 -07003860 proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003861 }
3862 } else {
3863 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
3864 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
3865 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003866 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003867 }
Martijn Coenenf9eac642017-05-22 11:26:23 -07003868 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003869 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003870 } break;
3871 case BC_DEAD_BINDER_DONE: {
3872 struct binder_work *w;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003873 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003874 struct binder_ref_death *death = NULL;
Seunghun Lee10f62862014-05-01 01:30:23 +09003875
Arve Hjønnevågda498892014-02-21 14:40:26 -08003876 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003877 return -EFAULT;
3878
Lisa Du7a64cd82016-02-17 09:32:52 +08003879 ptr += sizeof(cookie);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003880 binder_inner_proc_lock(proc);
3881 list_for_each_entry(w, &proc->delivered_death,
3882 entry) {
3883 struct binder_ref_death *tmp_death =
3884 container_of(w,
3885 struct binder_ref_death,
3886 work);
Seunghun Lee10f62862014-05-01 01:30:23 +09003887
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003888 if (tmp_death->cookie == cookie) {
3889 death = tmp_death;
3890 break;
3891 }
3892 }
3893 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003894 "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n",
3895 proc->pid, thread->pid, (u64)cookie,
3896 death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003897 if (death == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003898 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
3899 proc->pid, thread->pid, (u64)cookie);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003900 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003901 break;
3902 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003903 binder_dequeue_work_ilocked(&death->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003904 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
3905 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003906 if (thread->looper &
3907 (BINDER_LOOPER_STATE_REGISTERED |
3908 BINDER_LOOPER_STATE_ENTERED))
Martijn Coenen1af61802017-10-19 15:04:46 +02003909 binder_enqueue_thread_work_ilocked(
3910 thread, &death->work);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003911 else {
3912 binder_enqueue_work_ilocked(
3913 &death->work,
3914 &proc->todo);
Martijn Coenen053be422017-06-06 15:17:46 -07003915 binder_wakeup_proc_ilocked(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003916 }
3917 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003918 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003919 } break;
3920
3921 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303922 pr_err("%d:%d unknown command %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003923 proc->pid, thread->pid, cmd);
3924 return -EINVAL;
3925 }
3926 *consumed = ptr - buffer;
3927 }
3928 return 0;
3929}
3930
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02003931static void binder_stat_br(struct binder_proc *proc,
3932 struct binder_thread *thread, uint32_t cmd)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003933{
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003934 trace_binder_return(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003935 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07003936 atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
3937 atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
3938 atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003939 }
3940}
3941
Todd Kjos60792612017-05-24 10:51:01 -07003942static int binder_put_node_cmd(struct binder_proc *proc,
3943 struct binder_thread *thread,
3944 void __user **ptrp,
3945 binder_uintptr_t node_ptr,
3946 binder_uintptr_t node_cookie,
3947 int node_debug_id,
3948 uint32_t cmd, const char *cmd_name)
3949{
3950 void __user *ptr = *ptrp;
3951
3952 if (put_user(cmd, (uint32_t __user *)ptr))
3953 return -EFAULT;
3954 ptr += sizeof(uint32_t);
3955
3956 if (put_user(node_ptr, (binder_uintptr_t __user *)ptr))
3957 return -EFAULT;
3958 ptr += sizeof(binder_uintptr_t);
3959
3960 if (put_user(node_cookie, (binder_uintptr_t __user *)ptr))
3961 return -EFAULT;
3962 ptr += sizeof(binder_uintptr_t);
3963
3964 binder_stat_br(proc, thread, cmd);
3965 binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n",
3966 proc->pid, thread->pid, cmd_name, node_debug_id,
3967 (u64)node_ptr, (u64)node_cookie);
3968
3969 *ptrp = ptr;
3970 return 0;
3971}
3972
Martijn Coenen22d64e4322017-06-02 11:15:44 -07003973static int binder_wait_for_work(struct binder_thread *thread,
3974 bool do_proc_work)
3975{
3976 DEFINE_WAIT(wait);
3977 struct binder_proc *proc = thread->proc;
3978 int ret = 0;
3979
3980 freezer_do_not_count();
3981 binder_inner_proc_lock(proc);
3982 for (;;) {
3983 prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE);
3984 if (binder_has_work_ilocked(thread, do_proc_work))
3985 break;
3986 if (do_proc_work)
3987 list_add(&thread->waiting_thread_node,
3988 &proc->waiting_threads);
3989 binder_inner_proc_unlock(proc);
3990 schedule();
3991 binder_inner_proc_lock(proc);
3992 list_del_init(&thread->waiting_thread_node);
3993 if (signal_pending(current)) {
3994 ret = -ERESTARTSYS;
3995 break;
3996 }
3997 }
3998 finish_wait(&thread->wait, &wait);
3999 binder_inner_proc_unlock(proc);
4000 freezer_count();
4001
4002 return ret;
4003}
4004
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004005static int binder_thread_read(struct binder_proc *proc,
4006 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004007 binder_uintptr_t binder_buffer, size_t size,
4008 binder_size_t *consumed, int non_block)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004009{
Arve Hjønnevågda498892014-02-21 14:40:26 -08004010 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004011 void __user *ptr = buffer + *consumed;
4012 void __user *end = buffer + size;
4013
4014 int ret = 0;
4015 int wait_for_proc_work;
4016
4017 if (*consumed == 0) {
4018 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
4019 return -EFAULT;
4020 ptr += sizeof(uint32_t);
4021 }
4022
4023retry:
Martijn Coenen995a36e2017-06-02 13:36:52 -07004024 binder_inner_proc_lock(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004025 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
Martijn Coenen995a36e2017-06-02 13:36:52 -07004026 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004027
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004028 thread->looper |= BINDER_LOOPER_STATE_WAITING;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004029
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004030 trace_binder_wait_for_work(wait_for_proc_work,
4031 !!thread->transaction_stack,
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004032 !binder_worklist_empty(proc, &thread->todo));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004033 if (wait_for_proc_work) {
4034 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4035 BINDER_LOOPER_STATE_ENTERED))) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05304036 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 +09004037 proc->pid, thread->pid, thread->looper);
4038 wait_event_interruptible(binder_user_error_wait,
4039 binder_stop_on_user_error < 2);
4040 }
Martijn Coenenecd972d2017-05-26 10:48:56 -07004041 binder_restore_priority(current, proc->default_priority);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004042 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004043
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004044 if (non_block) {
4045 if (!binder_has_work(thread, wait_for_proc_work))
4046 ret = -EAGAIN;
4047 } else {
4048 ret = binder_wait_for_work(thread, wait_for_proc_work);
4049 }
4050
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004051 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
4052
4053 if (ret)
4054 return ret;
4055
4056 while (1) {
4057 uint32_t cmd;
4058 struct binder_transaction_data tr;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004059 struct binder_work *w = NULL;
4060 struct list_head *list = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004061 struct binder_transaction *t = NULL;
Todd Kjos2f993e22017-05-12 14:42:55 -07004062 struct binder_thread *t_from;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004063
Todd Kjose7f23ed2017-03-21 13:06:01 -07004064 binder_inner_proc_lock(proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004065 if (!binder_worklist_empty_ilocked(&thread->todo))
4066 list = &thread->todo;
4067 else if (!binder_worklist_empty_ilocked(&proc->todo) &&
4068 wait_for_proc_work)
4069 list = &proc->todo;
4070 else {
4071 binder_inner_proc_unlock(proc);
4072
Dmitry Voytik395262a2014-09-08 18:16:34 +04004073 /* no data added */
Todd Kjos6798e6d2017-01-06 14:19:25 -08004074 if (ptr - buffer == 4 && !thread->looper_need_return)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004075 goto retry;
4076 break;
4077 }
4078
Todd Kjose7f23ed2017-03-21 13:06:01 -07004079 if (end - ptr < sizeof(tr) + 4) {
4080 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004081 break;
Todd Kjose7f23ed2017-03-21 13:06:01 -07004082 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004083 w = binder_dequeue_work_head_ilocked(list);
Martijn Coenen1af61802017-10-19 15:04:46 +02004084 if (binder_worklist_empty_ilocked(&thread->todo))
4085 thread->process_todo = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004086
4087 switch (w->type) {
4088 case BINDER_WORK_TRANSACTION: {
Todd Kjose7f23ed2017-03-21 13:06:01 -07004089 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004090 t = container_of(w, struct binder_transaction, work);
4091 } break;
Todd Kjos858b8da2017-04-21 17:35:12 -07004092 case BINDER_WORK_RETURN_ERROR: {
4093 struct binder_error *e = container_of(
4094 w, struct binder_error, work);
4095
4096 WARN_ON(e->cmd == BR_OK);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004097 binder_inner_proc_unlock(proc);
Todd Kjos858b8da2017-04-21 17:35:12 -07004098 if (put_user(e->cmd, (uint32_t __user *)ptr))
4099 return -EFAULT;
4100 e->cmd = BR_OK;
4101 ptr += sizeof(uint32_t);
4102
4103 binder_stat_br(proc, thread, cmd);
Todd Kjos858b8da2017-04-21 17:35:12 -07004104 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004105 case BINDER_WORK_TRANSACTION_COMPLETE: {
Todd Kjose7f23ed2017-03-21 13:06:01 -07004106 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004107 cmd = BR_TRANSACTION_COMPLETE;
4108 if (put_user(cmd, (uint32_t __user *)ptr))
4109 return -EFAULT;
4110 ptr += sizeof(uint32_t);
4111
4112 binder_stat_br(proc, thread, cmd);
4113 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304114 "%d:%d BR_TRANSACTION_COMPLETE\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004115 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004116 kfree(w);
4117 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4118 } break;
4119 case BINDER_WORK_NODE: {
4120 struct binder_node *node = container_of(w, struct binder_node, work);
Todd Kjos60792612017-05-24 10:51:01 -07004121 int strong, weak;
4122 binder_uintptr_t node_ptr = node->ptr;
4123 binder_uintptr_t node_cookie = node->cookie;
4124 int node_debug_id = node->debug_id;
4125 int has_weak_ref;
4126 int has_strong_ref;
4127 void __user *orig_ptr = ptr;
Seunghun Lee10f62862014-05-01 01:30:23 +09004128
Todd Kjos60792612017-05-24 10:51:01 -07004129 BUG_ON(proc != node->proc);
4130 strong = node->internal_strong_refs ||
4131 node->local_strong_refs;
4132 weak = !hlist_empty(&node->refs) ||
Todd Kjosf22abc72017-05-09 11:08:05 -07004133 node->local_weak_refs ||
4134 node->tmp_refs || strong;
Todd Kjos60792612017-05-24 10:51:01 -07004135 has_strong_ref = node->has_strong_ref;
4136 has_weak_ref = node->has_weak_ref;
4137
4138 if (weak && !has_weak_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004139 node->has_weak_ref = 1;
4140 node->pending_weak_ref = 1;
4141 node->local_weak_refs++;
Todd Kjos60792612017-05-24 10:51:01 -07004142 }
4143 if (strong && !has_strong_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004144 node->has_strong_ref = 1;
4145 node->pending_strong_ref = 1;
4146 node->local_strong_refs++;
Todd Kjos60792612017-05-24 10:51:01 -07004147 }
4148 if (!strong && has_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004149 node->has_strong_ref = 0;
Todd Kjos60792612017-05-24 10:51:01 -07004150 if (!weak && has_weak_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004151 node->has_weak_ref = 0;
Todd Kjos60792612017-05-24 10:51:01 -07004152 if (!weak && !strong) {
4153 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4154 "%d:%d node %d u%016llx c%016llx deleted\n",
4155 proc->pid, thread->pid,
4156 node_debug_id,
4157 (u64)node_ptr,
4158 (u64)node_cookie);
4159 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004160 binder_inner_proc_unlock(proc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004161 binder_node_lock(node);
4162 /*
4163 * Acquire the node lock before freeing the
4164 * node to serialize with other threads that
4165 * may have been holding the node lock while
4166 * decrementing this node (avoids race where
4167 * this thread frees while the other thread
4168 * is unlocking the node after the final
4169 * decrement)
4170 */
4171 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004172 binder_free_node(node);
4173 } else
4174 binder_inner_proc_unlock(proc);
4175
Todd Kjos60792612017-05-24 10:51:01 -07004176 if (weak && !has_weak_ref)
4177 ret = binder_put_node_cmd(
4178 proc, thread, &ptr, node_ptr,
4179 node_cookie, node_debug_id,
4180 BR_INCREFS, "BR_INCREFS");
4181 if (!ret && strong && !has_strong_ref)
4182 ret = binder_put_node_cmd(
4183 proc, thread, &ptr, node_ptr,
4184 node_cookie, node_debug_id,
4185 BR_ACQUIRE, "BR_ACQUIRE");
4186 if (!ret && !strong && has_strong_ref)
4187 ret = binder_put_node_cmd(
4188 proc, thread, &ptr, node_ptr,
4189 node_cookie, node_debug_id,
4190 BR_RELEASE, "BR_RELEASE");
4191 if (!ret && !weak && has_weak_ref)
4192 ret = binder_put_node_cmd(
4193 proc, thread, &ptr, node_ptr,
4194 node_cookie, node_debug_id,
4195 BR_DECREFS, "BR_DECREFS");
4196 if (orig_ptr == ptr)
4197 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4198 "%d:%d node %d u%016llx c%016llx state unchanged\n",
4199 proc->pid, thread->pid,
4200 node_debug_id,
4201 (u64)node_ptr,
4202 (u64)node_cookie);
4203 if (ret)
4204 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004205 } break;
4206 case BINDER_WORK_DEAD_BINDER:
4207 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4208 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4209 struct binder_ref_death *death;
4210 uint32_t cmd;
Martijn Coenenf9eac642017-05-22 11:26:23 -07004211 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004212
4213 death = container_of(w, struct binder_ref_death, work);
4214 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
4215 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
4216 else
4217 cmd = BR_DEAD_BINDER;
Martijn Coenenf9eac642017-05-22 11:26:23 -07004218 cookie = death->cookie;
4219
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004220 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004221 "%d:%d %s %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004222 proc->pid, thread->pid,
4223 cmd == BR_DEAD_BINDER ?
4224 "BR_DEAD_BINDER" :
4225 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
Martijn Coenenf9eac642017-05-22 11:26:23 -07004226 (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004227 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
Martijn Coenenf9eac642017-05-22 11:26:23 -07004228 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004229 kfree(death);
4230 binder_stats_deleted(BINDER_STAT_DEATH);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004231 } else {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004232 binder_enqueue_work_ilocked(
4233 w, &proc->delivered_death);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004234 binder_inner_proc_unlock(proc);
4235 }
Martijn Coenenf9eac642017-05-22 11:26:23 -07004236 if (put_user(cmd, (uint32_t __user *)ptr))
4237 return -EFAULT;
4238 ptr += sizeof(uint32_t);
4239 if (put_user(cookie,
4240 (binder_uintptr_t __user *)ptr))
4241 return -EFAULT;
4242 ptr += sizeof(binder_uintptr_t);
4243 binder_stat_br(proc, thread, cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004244 if (cmd == BR_DEAD_BINDER)
4245 goto done; /* DEAD_BINDER notifications can cause transactions */
4246 } break;
4247 }
4248
4249 if (!t)
4250 continue;
4251
4252 BUG_ON(t->buffer == NULL);
4253 if (t->buffer->target_node) {
4254 struct binder_node *target_node = t->buffer->target_node;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004255 struct binder_priority node_prio;
Seunghun Lee10f62862014-05-01 01:30:23 +09004256
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004257 tr.target.ptr = target_node->ptr;
4258 tr.cookie = target_node->cookie;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004259 node_prio.sched_policy = target_node->sched_policy;
4260 node_prio.prio = target_node->min_priority;
Martijn Coenenc46810c2017-06-23 10:13:43 -07004261 binder_transaction_priority(current, t, node_prio,
4262 target_node->inherit_rt);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004263 cmd = BR_TRANSACTION;
4264 } else {
Arve Hjønnevågda498892014-02-21 14:40:26 -08004265 tr.target.ptr = 0;
4266 tr.cookie = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004267 cmd = BR_REPLY;
4268 }
4269 tr.code = t->code;
4270 tr.flags = t->flags;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -06004271 tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004272
Todd Kjos2f993e22017-05-12 14:42:55 -07004273 t_from = binder_get_txn_from(t);
4274 if (t_from) {
4275 struct task_struct *sender = t_from->proc->tsk;
Seunghun Lee10f62862014-05-01 01:30:23 +09004276
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004277 tr.sender_pid = task_tgid_nr_ns(sender,
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08004278 task_active_pid_ns(current));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004279 } else {
4280 tr.sender_pid = 0;
4281 }
4282
4283 tr.data_size = t->buffer->data_size;
4284 tr.offsets_size = t->buffer->offsets_size;
Todd Kjosd325d372016-10-10 10:40:53 -07004285 tr.data.ptr.buffer = (binder_uintptr_t)
4286 ((uintptr_t)t->buffer->data +
4287 binder_alloc_get_user_buffer_offset(&proc->alloc));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004288 tr.data.ptr.offsets = tr.data.ptr.buffer +
4289 ALIGN(t->buffer->data_size,
4290 sizeof(void *));
4291
Todd Kjos2f993e22017-05-12 14:42:55 -07004292 if (put_user(cmd, (uint32_t __user *)ptr)) {
4293 if (t_from)
4294 binder_thread_dec_tmpref(t_from);
Martijn Coenen3217ccc2017-08-24 15:23:36 +02004295
4296 binder_cleanup_transaction(t, "put_user failed",
4297 BR_FAILED_REPLY);
4298
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004299 return -EFAULT;
Todd Kjos2f993e22017-05-12 14:42:55 -07004300 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004301 ptr += sizeof(uint32_t);
Todd Kjos2f993e22017-05-12 14:42:55 -07004302 if (copy_to_user(ptr, &tr, sizeof(tr))) {
4303 if (t_from)
4304 binder_thread_dec_tmpref(t_from);
Martijn Coenen3217ccc2017-08-24 15:23:36 +02004305
4306 binder_cleanup_transaction(t, "copy_to_user failed",
4307 BR_FAILED_REPLY);
4308
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004309 return -EFAULT;
Todd Kjos2f993e22017-05-12 14:42:55 -07004310 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004311 ptr += sizeof(tr);
4312
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004313 trace_binder_transaction_received(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004314 binder_stat_br(proc, thread, cmd);
4315 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004316 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004317 proc->pid, thread->pid,
4318 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
4319 "BR_REPLY",
Todd Kjos2f993e22017-05-12 14:42:55 -07004320 t->debug_id, t_from ? t_from->proc->pid : 0,
4321 t_from ? t_from->pid : 0, cmd,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004322 t->buffer->data_size, t->buffer->offsets_size,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004323 (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004324
Todd Kjos2f993e22017-05-12 14:42:55 -07004325 if (t_from)
4326 binder_thread_dec_tmpref(t_from);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004327 t->buffer->allow_user_free = 1;
4328 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07004329 binder_inner_proc_lock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004330 t->to_parent = thread->transaction_stack;
4331 t->to_thread = thread;
4332 thread->transaction_stack = t;
Martijn Coenen995a36e2017-06-02 13:36:52 -07004333 binder_inner_proc_unlock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004334 } else {
Todd Kjos21ef40a2017-03-30 18:02:13 -07004335 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004336 }
4337 break;
4338 }
4339
4340done:
4341
4342 *consumed = ptr - buffer;
Todd Kjosd600e902017-05-25 17:35:02 -07004343 binder_inner_proc_lock(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004344 if (proc->requested_threads == 0 &&
4345 list_empty(&thread->proc->waiting_threads) &&
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004346 proc->requested_threads_started < proc->max_threads &&
4347 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4348 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
4349 /*spawn a new thread if we leave this out */) {
4350 proc->requested_threads++;
Todd Kjosd600e902017-05-25 17:35:02 -07004351 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004352 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304353 "%d:%d BR_SPAWN_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004354 proc->pid, thread->pid);
4355 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
4356 return -EFAULT;
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07004357 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
Todd Kjosd600e902017-05-25 17:35:02 -07004358 } else
4359 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004360 return 0;
4361}
4362
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004363static void binder_release_work(struct binder_proc *proc,
4364 struct list_head *list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004365{
4366 struct binder_work *w;
Seunghun Lee10f62862014-05-01 01:30:23 +09004367
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004368 while (1) {
4369 w = binder_dequeue_work_head(proc, list);
4370 if (!w)
4371 return;
4372
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004373 switch (w->type) {
4374 case BINDER_WORK_TRANSACTION: {
4375 struct binder_transaction *t;
4376
4377 t = container_of(w, struct binder_transaction, work);
Martijn Coenen3217ccc2017-08-24 15:23:36 +02004378
4379 binder_cleanup_transaction(t, "process died.",
4380 BR_DEAD_REPLY);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004381 } break;
Todd Kjos858b8da2017-04-21 17:35:12 -07004382 case BINDER_WORK_RETURN_ERROR: {
4383 struct binder_error *e = container_of(
4384 w, struct binder_error, work);
4385
4386 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4387 "undelivered TRANSACTION_ERROR: %u\n",
4388 e->cmd);
4389 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004390 case BINDER_WORK_TRANSACTION_COMPLETE: {
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004391 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304392 "undelivered TRANSACTION_COMPLETE\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004393 kfree(w);
4394 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4395 } break;
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004396 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4397 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4398 struct binder_ref_death *death;
4399
4400 death = container_of(w, struct binder_ref_death, work);
4401 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004402 "undelivered death notification, %016llx\n",
4403 (u64)death->cookie);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004404 kfree(death);
4405 binder_stats_deleted(BINDER_STAT_DEATH);
4406 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004407 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304408 pr_err("unexpected work type, %d, not freed\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004409 w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004410 break;
4411 }
4412 }
4413
4414}
4415
Todd Kjosb4827902017-05-25 15:52:17 -07004416static struct binder_thread *binder_get_thread_ilocked(
4417 struct binder_proc *proc, struct binder_thread *new_thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004418{
4419 struct binder_thread *thread = NULL;
4420 struct rb_node *parent = NULL;
4421 struct rb_node **p = &proc->threads.rb_node;
4422
4423 while (*p) {
4424 parent = *p;
4425 thread = rb_entry(parent, struct binder_thread, rb_node);
4426
4427 if (current->pid < thread->pid)
4428 p = &(*p)->rb_left;
4429 else if (current->pid > thread->pid)
4430 p = &(*p)->rb_right;
4431 else
Todd Kjosb4827902017-05-25 15:52:17 -07004432 return thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004433 }
Todd Kjosb4827902017-05-25 15:52:17 -07004434 if (!new_thread)
4435 return NULL;
4436 thread = new_thread;
4437 binder_stats_created(BINDER_STAT_THREAD);
4438 thread->proc = proc;
4439 thread->pid = current->pid;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004440 get_task_struct(current);
4441 thread->task = current;
Todd Kjosb4827902017-05-25 15:52:17 -07004442 atomic_set(&thread->tmp_ref, 0);
4443 init_waitqueue_head(&thread->wait);
4444 INIT_LIST_HEAD(&thread->todo);
4445 rb_link_node(&thread->rb_node, parent, p);
4446 rb_insert_color(&thread->rb_node, &proc->threads);
4447 thread->looper_need_return = true;
4448 thread->return_error.work.type = BINDER_WORK_RETURN_ERROR;
4449 thread->return_error.cmd = BR_OK;
4450 thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR;
4451 thread->reply_error.cmd = BR_OK;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004452 INIT_LIST_HEAD(&new_thread->waiting_thread_node);
Todd Kjosb4827902017-05-25 15:52:17 -07004453 return thread;
4454}
4455
4456static struct binder_thread *binder_get_thread(struct binder_proc *proc)
4457{
4458 struct binder_thread *thread;
4459 struct binder_thread *new_thread;
4460
4461 binder_inner_proc_lock(proc);
4462 thread = binder_get_thread_ilocked(proc, NULL);
4463 binder_inner_proc_unlock(proc);
4464 if (!thread) {
4465 new_thread = kzalloc(sizeof(*thread), GFP_KERNEL);
4466 if (new_thread == NULL)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004467 return NULL;
Todd Kjosb4827902017-05-25 15:52:17 -07004468 binder_inner_proc_lock(proc);
4469 thread = binder_get_thread_ilocked(proc, new_thread);
4470 binder_inner_proc_unlock(proc);
4471 if (thread != new_thread)
4472 kfree(new_thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004473 }
4474 return thread;
4475}
4476
Todd Kjos2f993e22017-05-12 14:42:55 -07004477static void binder_free_proc(struct binder_proc *proc)
4478{
4479 BUG_ON(!list_empty(&proc->todo));
4480 BUG_ON(!list_empty(&proc->delivered_death));
4481 binder_alloc_deferred_release(&proc->alloc);
4482 put_task_struct(proc->tsk);
4483 binder_stats_deleted(BINDER_STAT_PROC);
4484 kfree(proc);
4485}
4486
4487static void binder_free_thread(struct binder_thread *thread)
4488{
4489 BUG_ON(!list_empty(&thread->todo));
4490 binder_stats_deleted(BINDER_STAT_THREAD);
4491 binder_proc_dec_tmpref(thread->proc);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004492 put_task_struct(thread->task);
Todd Kjos2f993e22017-05-12 14:42:55 -07004493 kfree(thread);
4494}
4495
4496static int binder_thread_release(struct binder_proc *proc,
4497 struct binder_thread *thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004498{
4499 struct binder_transaction *t;
4500 struct binder_transaction *send_reply = NULL;
4501 int active_transactions = 0;
Todd Kjos2f993e22017-05-12 14:42:55 -07004502 struct binder_transaction *last_t = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004503
Todd Kjosb4827902017-05-25 15:52:17 -07004504 binder_inner_proc_lock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07004505 /*
4506 * take a ref on the proc so it survives
4507 * after we remove this thread from proc->threads.
4508 * The corresponding dec is when we actually
4509 * free the thread in binder_free_thread()
4510 */
4511 proc->tmp_ref++;
4512 /*
4513 * take a ref on this thread to ensure it
4514 * survives while we are releasing it
4515 */
4516 atomic_inc(&thread->tmp_ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004517 rb_erase(&thread->rb_node, &proc->threads);
4518 t = thread->transaction_stack;
Todd Kjos2f993e22017-05-12 14:42:55 -07004519 if (t) {
4520 spin_lock(&t->lock);
4521 if (t->to_thread == thread)
4522 send_reply = t;
4523 }
4524 thread->is_dead = true;
4525
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004526 while (t) {
Todd Kjos2f993e22017-05-12 14:42:55 -07004527 last_t = t;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004528 active_transactions++;
4529 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304530 "release %d:%d transaction %d %s, still active\n",
4531 proc->pid, thread->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004532 t->debug_id,
4533 (t->to_thread == thread) ? "in" : "out");
4534
4535 if (t->to_thread == thread) {
4536 t->to_proc = NULL;
4537 t->to_thread = NULL;
4538 if (t->buffer) {
4539 t->buffer->transaction = NULL;
4540 t->buffer = NULL;
4541 }
4542 t = t->to_parent;
4543 } else if (t->from == thread) {
4544 t->from = NULL;
4545 t = t->from_parent;
4546 } else
4547 BUG();
Todd Kjos2f993e22017-05-12 14:42:55 -07004548 spin_unlock(&last_t->lock);
4549 if (t)
4550 spin_lock(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004551 }
Martijn Coenen550c01d2018-01-05 11:27:07 +01004552
4553 /*
4554 * If this thread used poll, make sure we remove the waitqueue
4555 * from any epoll data structures holding it with POLLFREE.
4556 * waitqueue_active() is safe to use here because we're holding
4557 * the inner lock.
4558 */
4559 if ((thread->looper & BINDER_LOOPER_STATE_POLL) &&
4560 waitqueue_active(&thread->wait)) {
4561 wake_up_poll(&thread->wait, POLLHUP | POLLFREE);
4562 }
4563
Todd Kjosb4827902017-05-25 15:52:17 -07004564 binder_inner_proc_unlock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07004565
Martijn Coenen72766d72018-02-16 09:47:15 +01004566 /*
4567 * This is needed to avoid races between wake_up_poll() above and
4568 * and ep_remove_waitqueue() called for other reasons (eg the epoll file
4569 * descriptor being closed); ep_remove_waitqueue() holds an RCU read
4570 * lock, so we can be sure it's done after calling synchronize_rcu().
4571 */
4572 if (thread->looper & BINDER_LOOPER_STATE_POLL)
4573 synchronize_rcu();
4574
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004575 if (send_reply)
4576 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004577 binder_release_work(proc, &thread->todo);
Todd Kjos2f993e22017-05-12 14:42:55 -07004578 binder_thread_dec_tmpref(thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004579 return active_transactions;
4580}
4581
4582static unsigned int binder_poll(struct file *filp,
4583 struct poll_table_struct *wait)
4584{
4585 struct binder_proc *proc = filp->private_data;
4586 struct binder_thread *thread = NULL;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004587 bool wait_for_proc_work;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004588
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004589 thread = binder_get_thread(proc);
Greg Kroah-Hartman6e463bb2018-02-28 17:17:14 +01004590 if (!thread)
Eric Biggers4be5a282018-01-30 23:11:24 -08004591 return POLLERR;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004592
Martijn Coenen995a36e2017-06-02 13:36:52 -07004593 binder_inner_proc_lock(thread->proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004594 thread->looper |= BINDER_LOOPER_STATE_POLL;
4595 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
4596
Martijn Coenen995a36e2017-06-02 13:36:52 -07004597 binder_inner_proc_unlock(thread->proc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004598
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004599 poll_wait(filp, &thread->wait, wait);
4600
Martijn Coenen47810932017-08-10 12:32:00 +02004601 if (binder_has_work(thread, wait_for_proc_work))
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004602 return POLLIN;
4603
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004604 return 0;
4605}
4606
Tair Rzayev78260ac2014-06-03 22:27:21 +03004607static int binder_ioctl_write_read(struct file *filp,
4608 unsigned int cmd, unsigned long arg,
4609 struct binder_thread *thread)
4610{
4611 int ret = 0;
4612 struct binder_proc *proc = filp->private_data;
4613 unsigned int size = _IOC_SIZE(cmd);
4614 void __user *ubuf = (void __user *)arg;
4615 struct binder_write_read bwr;
4616
4617 if (size != sizeof(struct binder_write_read)) {
4618 ret = -EINVAL;
4619 goto out;
4620 }
4621 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
4622 ret = -EFAULT;
4623 goto out;
4624 }
4625 binder_debug(BINDER_DEBUG_READ_WRITE,
4626 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
4627 proc->pid, thread->pid,
4628 (u64)bwr.write_size, (u64)bwr.write_buffer,
4629 (u64)bwr.read_size, (u64)bwr.read_buffer);
4630
4631 if (bwr.write_size > 0) {
4632 ret = binder_thread_write(proc, thread,
4633 bwr.write_buffer,
4634 bwr.write_size,
4635 &bwr.write_consumed);
4636 trace_binder_write_done(ret);
4637 if (ret < 0) {
4638 bwr.read_consumed = 0;
4639 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4640 ret = -EFAULT;
4641 goto out;
4642 }
4643 }
4644 if (bwr.read_size > 0) {
4645 ret = binder_thread_read(proc, thread, bwr.read_buffer,
4646 bwr.read_size,
4647 &bwr.read_consumed,
4648 filp->f_flags & O_NONBLOCK);
4649 trace_binder_read_done(ret);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004650 binder_inner_proc_lock(proc);
4651 if (!binder_worklist_empty_ilocked(&proc->todo))
Martijn Coenen053be422017-06-06 15:17:46 -07004652 binder_wakeup_proc_ilocked(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004653 binder_inner_proc_unlock(proc);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004654 if (ret < 0) {
4655 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4656 ret = -EFAULT;
4657 goto out;
4658 }
4659 }
4660 binder_debug(BINDER_DEBUG_READ_WRITE,
4661 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
4662 proc->pid, thread->pid,
4663 (u64)bwr.write_consumed, (u64)bwr.write_size,
4664 (u64)bwr.read_consumed, (u64)bwr.read_size);
4665 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
4666 ret = -EFAULT;
4667 goto out;
4668 }
4669out:
4670 return ret;
4671}
4672
4673static int binder_ioctl_set_ctx_mgr(struct file *filp)
4674{
4675 int ret = 0;
4676 struct binder_proc *proc = filp->private_data;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004677 struct binder_context *context = proc->context;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004678 struct binder_node *new_node;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004679 kuid_t curr_euid = current_euid();
4680
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004681 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004682 if (context->binder_context_mgr_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004683 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
4684 ret = -EBUSY;
4685 goto out;
4686 }
Stephen Smalley79af7302015-01-21 10:54:10 -05004687 ret = security_binder_set_context_mgr(proc->tsk);
4688 if (ret < 0)
4689 goto out;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004690 if (uid_valid(context->binder_context_mgr_uid)) {
4691 if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004692 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
4693 from_kuid(&init_user_ns, curr_euid),
4694 from_kuid(&init_user_ns,
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004695 context->binder_context_mgr_uid));
Tair Rzayev78260ac2014-06-03 22:27:21 +03004696 ret = -EPERM;
4697 goto out;
4698 }
4699 } else {
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004700 context->binder_context_mgr_uid = curr_euid;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004701 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004702 new_node = binder_new_node(proc, NULL);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004703 if (!new_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004704 ret = -ENOMEM;
4705 goto out;
4706 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004707 binder_node_lock(new_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004708 new_node->local_weak_refs++;
4709 new_node->local_strong_refs++;
4710 new_node->has_strong_ref = 1;
4711 new_node->has_weak_ref = 1;
4712 context->binder_context_mgr_node = new_node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004713 binder_node_unlock(new_node);
Todd Kjosf22abc72017-05-09 11:08:05 -07004714 binder_put_node(new_node);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004715out:
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004716 mutex_unlock(&context->context_mgr_node_lock);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004717 return ret;
4718}
4719
Colin Cross833babb32017-06-20 13:54:44 -07004720static int binder_ioctl_get_node_debug_info(struct binder_proc *proc,
4721 struct binder_node_debug_info *info) {
4722 struct rb_node *n;
4723 binder_uintptr_t ptr = info->ptr;
4724
4725 memset(info, 0, sizeof(*info));
4726
4727 binder_inner_proc_lock(proc);
4728 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
4729 struct binder_node *node = rb_entry(n, struct binder_node,
4730 rb_node);
4731 if (node->ptr > ptr) {
4732 info->ptr = node->ptr;
4733 info->cookie = node->cookie;
4734 info->has_strong_ref = node->has_strong_ref;
4735 info->has_weak_ref = node->has_weak_ref;
4736 break;
4737 }
4738 }
4739 binder_inner_proc_unlock(proc);
4740
4741 return 0;
4742}
4743
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004744static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4745{
4746 int ret;
4747 struct binder_proc *proc = filp->private_data;
4748 struct binder_thread *thread;
4749 unsigned int size = _IOC_SIZE(cmd);
4750 void __user *ubuf = (void __user *)arg;
4751
Tair Rzayev78260ac2014-06-03 22:27:21 +03004752 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
4753 proc->pid, current->pid, cmd, arg);*/
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004754
Sherry Yang435416b2017-06-22 14:37:45 -07004755 binder_selftest_alloc(&proc->alloc);
4756
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004757 trace_binder_ioctl(cmd, arg);
4758
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004759 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4760 if (ret)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004761 goto err_unlocked;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004762
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004763 thread = binder_get_thread(proc);
4764 if (thread == NULL) {
4765 ret = -ENOMEM;
4766 goto err;
4767 }
4768
4769 switch (cmd) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004770 case BINDER_WRITE_READ:
4771 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
4772 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004773 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004774 break;
Todd Kjosd600e902017-05-25 17:35:02 -07004775 case BINDER_SET_MAX_THREADS: {
4776 int max_threads;
4777
4778 if (copy_from_user(&max_threads, ubuf,
4779 sizeof(max_threads))) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004780 ret = -EINVAL;
4781 goto err;
4782 }
Todd Kjosd600e902017-05-25 17:35:02 -07004783 binder_inner_proc_lock(proc);
4784 proc->max_threads = max_threads;
4785 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004786 break;
Todd Kjosd600e902017-05-25 17:35:02 -07004787 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004788 case BINDER_SET_CONTEXT_MGR:
Tair Rzayev78260ac2014-06-03 22:27:21 +03004789 ret = binder_ioctl_set_ctx_mgr(filp);
4790 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004791 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004792 break;
4793 case BINDER_THREAD_EXIT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304794 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004795 proc->pid, thread->pid);
Todd Kjos2f993e22017-05-12 14:42:55 -07004796 binder_thread_release(proc, thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004797 thread = NULL;
4798 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02004799 case BINDER_VERSION: {
4800 struct binder_version __user *ver = ubuf;
4801
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004802 if (size != sizeof(struct binder_version)) {
4803 ret = -EINVAL;
4804 goto err;
4805 }
Mathieu Maret36c89c02014-04-15 12:03:05 +02004806 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
4807 &ver->protocol_version)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004808 ret = -EINVAL;
4809 goto err;
4810 }
4811 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02004812 }
Colin Cross833babb32017-06-20 13:54:44 -07004813 case BINDER_GET_NODE_DEBUG_INFO: {
4814 struct binder_node_debug_info info;
4815
4816 if (copy_from_user(&info, ubuf, sizeof(info))) {
4817 ret = -EFAULT;
4818 goto err;
4819 }
4820
4821 ret = binder_ioctl_get_node_debug_info(proc, &info);
4822 if (ret < 0)
4823 goto err;
4824
4825 if (copy_to_user(ubuf, &info, sizeof(info))) {
4826 ret = -EFAULT;
4827 goto err;
4828 }
4829 break;
4830 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004831 default:
4832 ret = -EINVAL;
4833 goto err;
4834 }
4835 ret = 0;
4836err:
4837 if (thread)
Todd Kjos6798e6d2017-01-06 14:19:25 -08004838 thread->looper_need_return = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004839 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4840 if (ret && ret != -ERESTARTSYS)
Anmol Sarma56b468f2012-10-30 22:35:43 +05304841 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 -07004842err_unlocked:
4843 trace_binder_ioctl_done(ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004844 return ret;
4845}
4846
4847static void binder_vma_open(struct vm_area_struct *vma)
4848{
4849 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004850
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004851 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304852 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004853 proc->pid, vma->vm_start, vma->vm_end,
4854 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4855 (unsigned long)pgprot_val(vma->vm_page_prot));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004856}
4857
4858static void binder_vma_close(struct vm_area_struct *vma)
4859{
4860 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004861
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004862 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304863 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004864 proc->pid, vma->vm_start, vma->vm_end,
4865 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4866 (unsigned long)pgprot_val(vma->vm_page_prot));
Todd Kjosd325d372016-10-10 10:40:53 -07004867 binder_alloc_vma_close(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004868}
4869
Vinayak Menonddac7d52014-06-02 18:17:59 +05304870static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
4871{
4872 return VM_FAULT_SIGBUS;
4873}
4874
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07004875static const struct vm_operations_struct binder_vm_ops = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004876 .open = binder_vma_open,
4877 .close = binder_vma_close,
Vinayak Menonddac7d52014-06-02 18:17:59 +05304878 .fault = binder_vm_fault,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004879};
4880
Todd Kjosd325d372016-10-10 10:40:53 -07004881static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
4882{
4883 int ret;
4884 struct binder_proc *proc = filp->private_data;
4885 const char *failure_string;
4886
4887 if (proc->tsk != current->group_leader)
4888 return -EINVAL;
4889
4890 if ((vma->vm_end - vma->vm_start) > SZ_4M)
4891 vma->vm_end = vma->vm_start + SZ_4M;
4892
4893 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4894 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
4895 __func__, proc->pid, vma->vm_start, vma->vm_end,
4896 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4897 (unsigned long)pgprot_val(vma->vm_page_prot));
4898
4899 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
4900 ret = -EPERM;
4901 failure_string = "bad vm_flags";
4902 goto err_bad_arg;
4903 }
4904 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
4905 vma->vm_ops = &binder_vm_ops;
4906 vma->vm_private_data = proc;
4907
4908 ret = binder_alloc_mmap_handler(&proc->alloc, vma);
Todd Kjosf09daf12017-11-10 15:30:27 -08004909
4910 return ret;
Todd Kjosd325d372016-10-10 10:40:53 -07004911
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004912err_bad_arg:
Sherwin Soltani258767f2012-06-26 02:00:30 -04004913 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004914 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
4915 return ret;
4916}
4917
4918static int binder_open(struct inode *nodp, struct file *filp)
4919{
4920 struct binder_proc *proc;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02004921 struct binder_device *binder_dev;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004922
4923 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
4924 current->group_leader->pid, current->pid);
4925
4926 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
4927 if (proc == NULL)
4928 return -ENOMEM;
Todd Kjosfc7a7e22017-05-29 16:44:24 -07004929 spin_lock_init(&proc->inner_lock);
4930 spin_lock_init(&proc->outer_lock);
Martijn Coenen872c26e2017-03-07 15:51:18 +01004931 get_task_struct(current->group_leader);
4932 proc->tsk = current->group_leader;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004933 INIT_LIST_HEAD(&proc->todo);
Martijn Coenen57b2ac62017-06-06 17:04:42 -07004934 if (binder_supported_policy(current->policy)) {
4935 proc->default_priority.sched_policy = current->policy;
4936 proc->default_priority.prio = current->normal_prio;
4937 } else {
4938 proc->default_priority.sched_policy = SCHED_NORMAL;
4939 proc->default_priority.prio = NICE_TO_PRIO(0);
4940 }
4941
Martijn Coenen6b7c7122016-09-30 16:08:09 +02004942 binder_dev = container_of(filp->private_data, struct binder_device,
4943 miscdev);
4944 proc->context = &binder_dev->context;
Todd Kjosd325d372016-10-10 10:40:53 -07004945 binder_alloc_init(&proc->alloc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004946
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004947 binder_stats_created(BINDER_STAT_PROC);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004948 proc->pid = current->group_leader->pid;
4949 INIT_LIST_HEAD(&proc->delivered_death);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004950 INIT_LIST_HEAD(&proc->waiting_threads);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004951 filp->private_data = proc;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004952
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004953 mutex_lock(&binder_procs_lock);
4954 hlist_add_head(&proc->proc_node, &binder_procs);
4955 mutex_unlock(&binder_procs_lock);
4956
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004957 if (binder_debugfs_dir_entry_proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004958 char strbuf[11];
Seunghun Lee10f62862014-05-01 01:30:23 +09004959
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004960 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02004961 /*
4962 * proc debug entries are shared between contexts, so
4963 * this will fail if the process tries to open the driver
4964 * again with a different context. The priting code will
4965 * anyway print all contexts that a given PID has, so this
4966 * is not a problem.
4967 */
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004968 proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02004969 binder_debugfs_dir_entry_proc,
4970 (void *)(unsigned long)proc->pid,
4971 &binder_proc_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004972 }
4973
4974 return 0;
4975}
4976
4977static int binder_flush(struct file *filp, fl_owner_t id)
4978{
4979 struct binder_proc *proc = filp->private_data;
4980
4981 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
4982
4983 return 0;
4984}
4985
4986static void binder_deferred_flush(struct binder_proc *proc)
4987{
4988 struct rb_node *n;
4989 int wake_count = 0;
Seunghun Lee10f62862014-05-01 01:30:23 +09004990
Todd Kjosb4827902017-05-25 15:52:17 -07004991 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004992 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
4993 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
Seunghun Lee10f62862014-05-01 01:30:23 +09004994
Todd Kjos6798e6d2017-01-06 14:19:25 -08004995 thread->looper_need_return = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004996 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
4997 wake_up_interruptible(&thread->wait);
4998 wake_count++;
4999 }
5000 }
Todd Kjosb4827902017-05-25 15:52:17 -07005001 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005002
5003 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5004 "binder_flush: %d woke %d threads\n", proc->pid,
5005 wake_count);
5006}
5007
5008static int binder_release(struct inode *nodp, struct file *filp)
5009{
5010 struct binder_proc *proc = filp->private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09005011
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005012 debugfs_remove(proc->debugfs_entry);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005013 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
5014
5015 return 0;
5016}
5017
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005018static int binder_node_release(struct binder_node *node, int refs)
5019{
5020 struct binder_ref *ref;
5021 int death = 0;
Todd Kjose7f23ed2017-03-21 13:06:01 -07005022 struct binder_proc *proc = node->proc;
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005023
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005024 binder_release_work(proc, &node->async_todo);
Todd Kjose7f23ed2017-03-21 13:06:01 -07005025
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005026 binder_node_lock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07005027 binder_inner_proc_lock(proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005028 binder_dequeue_work_ilocked(&node->work);
Todd Kjosf22abc72017-05-09 11:08:05 -07005029 /*
5030 * The caller must have taken a temporary ref on the node,
5031 */
5032 BUG_ON(!node->tmp_refs);
5033 if (hlist_empty(&node->refs) && node->tmp_refs == 1) {
Todd Kjose7f23ed2017-03-21 13:06:01 -07005034 binder_inner_proc_unlock(proc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005035 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07005036 binder_free_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005037
5038 return refs;
5039 }
5040
5041 node->proc = NULL;
5042 node->local_strong_refs = 0;
5043 node->local_weak_refs = 0;
Todd Kjose7f23ed2017-03-21 13:06:01 -07005044 binder_inner_proc_unlock(proc);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005045
5046 spin_lock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005047 hlist_add_head(&node->dead_node, &binder_dead_nodes);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005048 spin_unlock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005049
5050 hlist_for_each_entry(ref, &node->refs, node_entry) {
5051 refs++;
Martijn Coenenf9eac642017-05-22 11:26:23 -07005052 /*
5053 * Need the node lock to synchronize
5054 * with new notification requests and the
5055 * inner lock to synchronize with queued
5056 * death notifications.
5057 */
5058 binder_inner_proc_lock(ref->proc);
5059 if (!ref->death) {
5060 binder_inner_proc_unlock(ref->proc);
Arve Hjønnevåge194fd82014-02-17 13:58:29 -08005061 continue;
Martijn Coenenf9eac642017-05-22 11:26:23 -07005062 }
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005063
5064 death++;
5065
Martijn Coenenf9eac642017-05-22 11:26:23 -07005066 BUG_ON(!list_empty(&ref->death->work.entry));
5067 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
5068 binder_enqueue_work_ilocked(&ref->death->work,
5069 &ref->proc->todo);
Martijn Coenen053be422017-06-06 15:17:46 -07005070 binder_wakeup_proc_ilocked(ref->proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005071 binder_inner_proc_unlock(ref->proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005072 }
5073
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005074 binder_debug(BINDER_DEBUG_DEAD_BINDER,
5075 "node %d now dead, refs %d, death %d\n",
5076 node->debug_id, refs, death);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005077 binder_node_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07005078 binder_put_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005079
5080 return refs;
5081}
5082
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005083static void binder_deferred_release(struct binder_proc *proc)
5084{
Martijn Coenen0b3311e2016-09-30 15:51:48 +02005085 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005086 struct rb_node *n;
Todd Kjosd325d372016-10-10 10:40:53 -07005087 int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005088
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005089 mutex_lock(&binder_procs_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005090 hlist_del(&proc->proc_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005091 mutex_unlock(&binder_procs_lock);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005092
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005093 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02005094 if (context->binder_context_mgr_node &&
5095 context->binder_context_mgr_node->proc == proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005096 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01005097 "%s: %d context_mgr_node gone\n",
5098 __func__, proc->pid);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02005099 context->binder_context_mgr_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005100 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005101 mutex_unlock(&context->context_mgr_node_lock);
Todd Kjosb4827902017-05-25 15:52:17 -07005102 binder_inner_proc_lock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07005103 /*
5104 * Make sure proc stays alive after we
5105 * remove all the threads
5106 */
5107 proc->tmp_ref++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005108
Todd Kjos2f993e22017-05-12 14:42:55 -07005109 proc->is_dead = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005110 threads = 0;
5111 active_transactions = 0;
5112 while ((n = rb_first(&proc->threads))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005113 struct binder_thread *thread;
5114
5115 thread = rb_entry(n, struct binder_thread, rb_node);
Todd Kjosb4827902017-05-25 15:52:17 -07005116 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005117 threads++;
Todd Kjos2f993e22017-05-12 14:42:55 -07005118 active_transactions += binder_thread_release(proc, thread);
Todd Kjosb4827902017-05-25 15:52:17 -07005119 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005120 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005121
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005122 nodes = 0;
5123 incoming_refs = 0;
5124 while ((n = rb_first(&proc->nodes))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005125 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005126
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005127 node = rb_entry(n, struct binder_node, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005128 nodes++;
Todd Kjosf22abc72017-05-09 11:08:05 -07005129 /*
5130 * take a temporary ref on the node before
5131 * calling binder_node_release() which will either
5132 * kfree() the node or call binder_put_node()
5133 */
Todd Kjos425d23f2017-06-12 12:07:26 -07005134 binder_inc_node_tmpref_ilocked(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005135 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjos425d23f2017-06-12 12:07:26 -07005136 binder_inner_proc_unlock(proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005137 incoming_refs = binder_node_release(node, incoming_refs);
Todd Kjos425d23f2017-06-12 12:07:26 -07005138 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005139 }
Todd Kjos425d23f2017-06-12 12:07:26 -07005140 binder_inner_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005141
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005142 outgoing_refs = 0;
Todd Kjos5346bf32016-10-20 16:43:34 -07005143 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005144 while ((n = rb_first(&proc->refs_by_desc))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005145 struct binder_ref *ref;
5146
5147 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005148 outgoing_refs++;
Todd Kjos5346bf32016-10-20 16:43:34 -07005149 binder_cleanup_ref_olocked(ref);
5150 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07005151 binder_free_ref(ref);
Todd Kjos5346bf32016-10-20 16:43:34 -07005152 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005153 }
Todd Kjos5346bf32016-10-20 16:43:34 -07005154 binder_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005155
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005156 binder_release_work(proc, &proc->todo);
5157 binder_release_work(proc, &proc->delivered_death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005158
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005159 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Todd Kjosd325d372016-10-10 10:40:53 -07005160 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01005161 __func__, proc->pid, threads, nodes, incoming_refs,
Todd Kjosd325d372016-10-10 10:40:53 -07005162 outgoing_refs, active_transactions);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005163
Todd Kjos2f993e22017-05-12 14:42:55 -07005164 binder_proc_dec_tmpref(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005165}
5166
5167static void binder_deferred_func(struct work_struct *work)
5168{
5169 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005170 int defer;
Seunghun Lee10f62862014-05-01 01:30:23 +09005171
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005172 do {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005173 mutex_lock(&binder_deferred_lock);
5174 if (!hlist_empty(&binder_deferred_list)) {
5175 proc = hlist_entry(binder_deferred_list.first,
5176 struct binder_proc, deferred_work_node);
5177 hlist_del_init(&proc->deferred_work_node);
5178 defer = proc->deferred_work;
5179 proc->deferred_work = 0;
5180 } else {
5181 proc = NULL;
5182 defer = 0;
5183 }
5184 mutex_unlock(&binder_deferred_lock);
5185
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005186 if (defer & BINDER_DEFERRED_FLUSH)
5187 binder_deferred_flush(proc);
5188
5189 if (defer & BINDER_DEFERRED_RELEASE)
5190 binder_deferred_release(proc); /* frees proc */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005191 } while (proc);
5192}
5193static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
5194
5195static void
5196binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
5197{
5198 mutex_lock(&binder_deferred_lock);
5199 proc->deferred_work |= defer;
5200 if (hlist_unhashed(&proc->deferred_work_node)) {
5201 hlist_add_head(&proc->deferred_work_node,
5202 &binder_deferred_list);
Bhaktipriya Shridhar1beba522016-08-13 22:16:24 +05305203 schedule_work(&binder_deferred_work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005204 }
5205 mutex_unlock(&binder_deferred_lock);
5206}
5207
Todd Kjos6d241a42017-04-21 14:32:11 -07005208static void print_binder_transaction_ilocked(struct seq_file *m,
5209 struct binder_proc *proc,
5210 const char *prefix,
5211 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005212{
Todd Kjos6d241a42017-04-21 14:32:11 -07005213 struct binder_proc *to_proc;
5214 struct binder_buffer *buffer = t->buffer;
5215
Todd Kjos2f993e22017-05-12 14:42:55 -07005216 spin_lock(&t->lock);
Todd Kjos6d241a42017-04-21 14:32:11 -07005217 to_proc = t->to_proc;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005218 seq_printf(m,
Martijn Coenen57b2ac62017-06-06 17:04:42 -07005219 "%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 -07005220 prefix, t->debug_id, t,
5221 t->from ? t->from->proc->pid : 0,
5222 t->from ? t->from->pid : 0,
Todd Kjos6d241a42017-04-21 14:32:11 -07005223 to_proc ? to_proc->pid : 0,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005224 t->to_thread ? t->to_thread->pid : 0,
Martijn Coenen57b2ac62017-06-06 17:04:42 -07005225 t->code, t->flags, t->priority.sched_policy,
5226 t->priority.prio, t->need_reply);
Todd Kjos2f993e22017-05-12 14:42:55 -07005227 spin_unlock(&t->lock);
5228
Todd Kjos6d241a42017-04-21 14:32:11 -07005229 if (proc != to_proc) {
5230 /*
5231 * Can only safely deref buffer if we are holding the
5232 * correct proc inner lock for this node
5233 */
5234 seq_puts(m, "\n");
5235 return;
5236 }
5237
5238 if (buffer == NULL) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005239 seq_puts(m, " buffer free\n");
5240 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005241 }
Todd Kjos6d241a42017-04-21 14:32:11 -07005242 if (buffer->target_node)
5243 seq_printf(m, " node %d", buffer->target_node->debug_id);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005244 seq_printf(m, " size %zd:%zd data %p\n",
Todd Kjos6d241a42017-04-21 14:32:11 -07005245 buffer->data_size, buffer->offsets_size,
5246 buffer->data);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005247}
5248
Todd Kjos6d241a42017-04-21 14:32:11 -07005249static void print_binder_work_ilocked(struct seq_file *m,
5250 struct binder_proc *proc,
5251 const char *prefix,
5252 const char *transaction_prefix,
5253 struct binder_work *w)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005254{
5255 struct binder_node *node;
5256 struct binder_transaction *t;
5257
5258 switch (w->type) {
5259 case BINDER_WORK_TRANSACTION:
5260 t = container_of(w, struct binder_transaction, work);
Todd Kjos6d241a42017-04-21 14:32:11 -07005261 print_binder_transaction_ilocked(
5262 m, proc, transaction_prefix, t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005263 break;
Todd Kjos858b8da2017-04-21 17:35:12 -07005264 case BINDER_WORK_RETURN_ERROR: {
5265 struct binder_error *e = container_of(
5266 w, struct binder_error, work);
5267
5268 seq_printf(m, "%stransaction error: %u\n",
5269 prefix, e->cmd);
5270 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005271 case BINDER_WORK_TRANSACTION_COMPLETE:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005272 seq_printf(m, "%stransaction complete\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005273 break;
5274 case BINDER_WORK_NODE:
5275 node = container_of(w, struct binder_node, work);
Arve Hjønnevågda498892014-02-21 14:40:26 -08005276 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
5277 prefix, node->debug_id,
5278 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005279 break;
5280 case BINDER_WORK_DEAD_BINDER:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005281 seq_printf(m, "%shas dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005282 break;
5283 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005284 seq_printf(m, "%shas cleared dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005285 break;
5286 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005287 seq_printf(m, "%shas cleared death notification\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005288 break;
5289 default:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005290 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005291 break;
5292 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005293}
5294
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005295static void print_binder_thread_ilocked(struct seq_file *m,
5296 struct binder_thread *thread,
5297 int print_always)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005298{
5299 struct binder_transaction *t;
5300 struct binder_work *w;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005301 size_t start_pos = m->count;
5302 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005303
Todd Kjos2f993e22017-05-12 14:42:55 -07005304 seq_printf(m, " thread %d: l %02x need_return %d tr %d\n",
Todd Kjos6798e6d2017-01-06 14:19:25 -08005305 thread->pid, thread->looper,
Todd Kjos2f993e22017-05-12 14:42:55 -07005306 thread->looper_need_return,
5307 atomic_read(&thread->tmp_ref));
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005308 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005309 t = thread->transaction_stack;
5310 while (t) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005311 if (t->from == thread) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005312 print_binder_transaction_ilocked(m, thread->proc,
5313 " outgoing transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005314 t = t->from_parent;
5315 } else if (t->to_thread == thread) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005316 print_binder_transaction_ilocked(m, thread->proc,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005317 " incoming transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005318 t = t->to_parent;
5319 } else {
Todd Kjos6d241a42017-04-21 14:32:11 -07005320 print_binder_transaction_ilocked(m, thread->proc,
5321 " bad transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005322 t = NULL;
5323 }
5324 }
5325 list_for_each_entry(w, &thread->todo, entry) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005326 print_binder_work_ilocked(m, thread->proc, " ",
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005327 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005328 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005329 if (!print_always && m->count == header_pos)
5330 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005331}
5332
Todd Kjos425d23f2017-06-12 12:07:26 -07005333static void print_binder_node_nilocked(struct seq_file *m,
5334 struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005335{
5336 struct binder_ref *ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005337 struct binder_work *w;
5338 int count;
5339
5340 count = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08005341 hlist_for_each_entry(ref, &node->refs, node_entry)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005342 count++;
5343
Martijn Coenen6aac9792017-06-07 09:29:14 -07005344 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 -08005345 node->debug_id, (u64)node->ptr, (u64)node->cookie,
Martijn Coenen6aac9792017-06-07 09:29:14 -07005346 node->sched_policy, node->min_priority,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005347 node->has_strong_ref, node->has_weak_ref,
5348 node->local_strong_refs, node->local_weak_refs,
Todd Kjosf22abc72017-05-09 11:08:05 -07005349 node->internal_strong_refs, count, node->tmp_refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005350 if (count) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005351 seq_puts(m, " proc");
Sasha Levinb67bfe02013-02-27 17:06:00 -08005352 hlist_for_each_entry(ref, &node->refs, node_entry)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005353 seq_printf(m, " %d", ref->proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005354 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005355 seq_puts(m, "\n");
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005356 if (node->proc) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005357 list_for_each_entry(w, &node->async_todo, entry)
Todd Kjos6d241a42017-04-21 14:32:11 -07005358 print_binder_work_ilocked(m, node->proc, " ",
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005359 " pending async transaction", w);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005360 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005361}
5362
Todd Kjos5346bf32016-10-20 16:43:34 -07005363static void print_binder_ref_olocked(struct seq_file *m,
5364 struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005365{
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005366 binder_node_lock(ref->node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07005367 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
5368 ref->data.debug_id, ref->data.desc,
5369 ref->node->proc ? "" : "dead ",
5370 ref->node->debug_id, ref->data.strong,
5371 ref->data.weak, ref->death);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005372 binder_node_unlock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005373}
5374
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005375static void print_binder_proc(struct seq_file *m,
5376 struct binder_proc *proc, int print_all)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005377{
5378 struct binder_work *w;
5379 struct rb_node *n;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005380 size_t start_pos = m->count;
5381 size_t header_pos;
Todd Kjos425d23f2017-06-12 12:07:26 -07005382 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005383
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005384 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005385 seq_printf(m, "context %s\n", proc->context->name);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005386 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005387
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005388 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005389 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005390 print_binder_thread_ilocked(m, rb_entry(n, struct binder_thread,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005391 rb_node), print_all);
Todd Kjos425d23f2017-06-12 12:07:26 -07005392
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005393 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005394 struct binder_node *node = rb_entry(n, struct binder_node,
5395 rb_node);
Todd Kjos425d23f2017-06-12 12:07:26 -07005396 /*
5397 * take a temporary reference on the node so it
5398 * survives and isn't removed from the tree
5399 * while we print it.
5400 */
5401 binder_inc_node_tmpref_ilocked(node);
5402 /* Need to drop inner lock to take node lock */
5403 binder_inner_proc_unlock(proc);
5404 if (last_node)
5405 binder_put_node(last_node);
5406 binder_node_inner_lock(node);
5407 print_binder_node_nilocked(m, node);
5408 binder_node_inner_unlock(node);
5409 last_node = node;
5410 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005411 }
Todd Kjos425d23f2017-06-12 12:07:26 -07005412 binder_inner_proc_unlock(proc);
5413 if (last_node)
5414 binder_put_node(last_node);
5415
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005416 if (print_all) {
Todd Kjos5346bf32016-10-20 16:43:34 -07005417 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005418 for (n = rb_first(&proc->refs_by_desc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005419 n != NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005420 n = rb_next(n))
Todd Kjos5346bf32016-10-20 16:43:34 -07005421 print_binder_ref_olocked(m, rb_entry(n,
5422 struct binder_ref,
5423 rb_node_desc));
5424 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005425 }
Todd Kjosd325d372016-10-10 10:40:53 -07005426 binder_alloc_print_allocated(m, &proc->alloc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005427 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005428 list_for_each_entry(w, &proc->todo, entry)
Todd Kjos6d241a42017-04-21 14:32:11 -07005429 print_binder_work_ilocked(m, proc, " ",
5430 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005431 list_for_each_entry(w, &proc->delivered_death, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005432 seq_puts(m, " has delivered dead binder\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005433 break;
5434 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005435 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005436 if (!print_all && m->count == header_pos)
5437 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005438}
5439
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005440static const char * const binder_return_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005441 "BR_ERROR",
5442 "BR_OK",
5443 "BR_TRANSACTION",
5444 "BR_REPLY",
5445 "BR_ACQUIRE_RESULT",
5446 "BR_DEAD_REPLY",
5447 "BR_TRANSACTION_COMPLETE",
5448 "BR_INCREFS",
5449 "BR_ACQUIRE",
5450 "BR_RELEASE",
5451 "BR_DECREFS",
5452 "BR_ATTEMPT_ACQUIRE",
5453 "BR_NOOP",
5454 "BR_SPAWN_LOOPER",
5455 "BR_FINISHED",
5456 "BR_DEAD_BINDER",
5457 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
5458 "BR_FAILED_REPLY"
5459};
5460
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005461static const char * const binder_command_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005462 "BC_TRANSACTION",
5463 "BC_REPLY",
5464 "BC_ACQUIRE_RESULT",
5465 "BC_FREE_BUFFER",
5466 "BC_INCREFS",
5467 "BC_ACQUIRE",
5468 "BC_RELEASE",
5469 "BC_DECREFS",
5470 "BC_INCREFS_DONE",
5471 "BC_ACQUIRE_DONE",
5472 "BC_ATTEMPT_ACQUIRE",
5473 "BC_REGISTER_LOOPER",
5474 "BC_ENTER_LOOPER",
5475 "BC_EXIT_LOOPER",
5476 "BC_REQUEST_DEATH_NOTIFICATION",
5477 "BC_CLEAR_DEATH_NOTIFICATION",
Martijn Coenen5a6da532016-09-30 14:10:07 +02005478 "BC_DEAD_BINDER_DONE",
5479 "BC_TRANSACTION_SG",
5480 "BC_REPLY_SG",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005481};
5482
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005483static const char * const binder_objstat_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005484 "proc",
5485 "thread",
5486 "node",
5487 "ref",
5488 "death",
5489 "transaction",
5490 "transaction_complete"
5491};
5492
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005493static void print_binder_stats(struct seq_file *m, const char *prefix,
5494 struct binder_stats *stats)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005495{
5496 int i;
5497
5498 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005499 ARRAY_SIZE(binder_command_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005500 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005501 int temp = atomic_read(&stats->bc[i]);
5502
5503 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005504 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005505 binder_command_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005506 }
5507
5508 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005509 ARRAY_SIZE(binder_return_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005510 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005511 int temp = atomic_read(&stats->br[i]);
5512
5513 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005514 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005515 binder_return_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005516 }
5517
5518 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005519 ARRAY_SIZE(binder_objstat_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005520 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005521 ARRAY_SIZE(stats->obj_deleted));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005522 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005523 int created = atomic_read(&stats->obj_created[i]);
5524 int deleted = atomic_read(&stats->obj_deleted[i]);
5525
5526 if (created || deleted)
5527 seq_printf(m, "%s%s: active %d total %d\n",
5528 prefix,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005529 binder_objstat_strings[i],
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005530 created - deleted,
5531 created);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005532 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005533}
5534
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005535static void print_binder_proc_stats(struct seq_file *m,
5536 struct binder_proc *proc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005537{
5538 struct binder_work *w;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005539 struct binder_thread *thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005540 struct rb_node *n;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005541 int count, strong, weak, ready_threads;
Todd Kjosb4827902017-05-25 15:52:17 -07005542 size_t free_async_space =
5543 binder_alloc_get_free_async_space(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005544
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005545 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005546 seq_printf(m, "context %s\n", proc->context->name);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005547 count = 0;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005548 ready_threads = 0;
Todd Kjosb4827902017-05-25 15:52:17 -07005549 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005550 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
5551 count++;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005552
5553 list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node)
5554 ready_threads++;
5555
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005556 seq_printf(m, " threads: %d\n", count);
5557 seq_printf(m, " requested threads: %d+%d/%d\n"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005558 " ready threads %d\n"
5559 " free async space %zd\n", proc->requested_threads,
5560 proc->requested_threads_started, proc->max_threads,
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005561 ready_threads,
Todd Kjosb4827902017-05-25 15:52:17 -07005562 free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005563 count = 0;
5564 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
5565 count++;
Todd Kjos425d23f2017-06-12 12:07:26 -07005566 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005567 seq_printf(m, " nodes: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005568 count = 0;
5569 strong = 0;
5570 weak = 0;
Todd Kjos5346bf32016-10-20 16:43:34 -07005571 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005572 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
5573 struct binder_ref *ref = rb_entry(n, struct binder_ref,
5574 rb_node_desc);
5575 count++;
Todd Kjosb0117bb2017-05-08 09:16:27 -07005576 strong += ref->data.strong;
5577 weak += ref->data.weak;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005578 }
Todd Kjos5346bf32016-10-20 16:43:34 -07005579 binder_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005580 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005581
Todd Kjosd325d372016-10-10 10:40:53 -07005582 count = binder_alloc_get_allocated_count(&proc->alloc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005583 seq_printf(m, " buffers: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005584
Sherry Yang91004422017-08-22 17:26:57 -07005585 binder_alloc_print_pages(m, &proc->alloc);
5586
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005587 count = 0;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005588 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005589 list_for_each_entry(w, &proc->todo, entry) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005590 if (w->type == BINDER_WORK_TRANSACTION)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005591 count++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005592 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005593 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005594 seq_printf(m, " pending transactions: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005595
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005596 print_binder_stats(m, " ", &proc->stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005597}
5598
5599
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005600static int binder_state_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005601{
5602 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005603 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005604 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005605
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005606 seq_puts(m, "binder state:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005607
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005608 spin_lock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005609 if (!hlist_empty(&binder_dead_nodes))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005610 seq_puts(m, "dead nodes:\n");
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005611 hlist_for_each_entry(node, &binder_dead_nodes, dead_node) {
5612 /*
5613 * take a temporary reference on the node so it
5614 * survives and isn't removed from the list
5615 * while we print it.
5616 */
5617 node->tmp_refs++;
5618 spin_unlock(&binder_dead_nodes_lock);
5619 if (last_node)
5620 binder_put_node(last_node);
5621 binder_node_lock(node);
Todd Kjos425d23f2017-06-12 12:07:26 -07005622 print_binder_node_nilocked(m, node);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005623 binder_node_unlock(node);
5624 last_node = node;
5625 spin_lock(&binder_dead_nodes_lock);
5626 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005627 spin_unlock(&binder_dead_nodes_lock);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005628 if (last_node)
5629 binder_put_node(last_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005630
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005631 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005632 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005633 print_binder_proc(m, proc, 1);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005634 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005635
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005636 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005637}
5638
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005639static int binder_stats_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005640{
5641 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005642
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005643 seq_puts(m, "binder stats:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005644
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005645 print_binder_stats(m, "", &binder_stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005646
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005647 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005648 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005649 print_binder_proc_stats(m, proc);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005650 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005651
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005652 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005653}
5654
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005655static int binder_transactions_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005656{
5657 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005658
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005659 seq_puts(m, "binder transactions:\n");
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005660 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005661 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005662 print_binder_proc(m, proc, 0);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005663 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005664
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005665 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005666}
5667
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005668static int binder_proc_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005669{
Riley Andrews83050a42016-02-09 21:05:33 -08005670 struct binder_proc *itr;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005671 int pid = (unsigned long)m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005672
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005673 mutex_lock(&binder_procs_lock);
Riley Andrews83050a42016-02-09 21:05:33 -08005674 hlist_for_each_entry(itr, &binder_procs, proc_node) {
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005675 if (itr->pid == pid) {
5676 seq_puts(m, "binder proc state:\n");
5677 print_binder_proc(m, itr, 1);
Riley Andrews83050a42016-02-09 21:05:33 -08005678 }
5679 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005680 mutex_unlock(&binder_procs_lock);
5681
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005682 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005683}
5684
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005685static void print_binder_transaction_log_entry(struct seq_file *m,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005686 struct binder_transaction_log_entry *e)
5687{
Todd Kjos1cfe6272017-05-24 13:33:28 -07005688 int debug_id = READ_ONCE(e->debug_id_done);
5689 /*
5690 * read barrier to guarantee debug_id_done read before
5691 * we print the log values
5692 */
5693 smp_rmb();
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005694 seq_printf(m,
Todd Kjos1cfe6272017-05-24 13:33:28 -07005695 "%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 -07005696 e->debug_id, (e->call_type == 2) ? "reply" :
5697 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005698 e->from_thread, e->to_proc, e->to_thread, e->context_name,
Todd Kjose598d172017-03-22 17:19:52 -07005699 e->to_node, e->target_handle, e->data_size, e->offsets_size,
5700 e->return_error, e->return_error_param,
5701 e->return_error_line);
Todd Kjos1cfe6272017-05-24 13:33:28 -07005702 /*
5703 * read-barrier to guarantee read of debug_id_done after
5704 * done printing the fields of the entry
5705 */
5706 smp_rmb();
5707 seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ?
5708 "\n" : " (incomplete)\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005709}
5710
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005711static int binder_transaction_log_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005712{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005713 struct binder_transaction_log *log = m->private;
Todd Kjos1cfe6272017-05-24 13:33:28 -07005714 unsigned int log_cur = atomic_read(&log->cur);
5715 unsigned int count;
5716 unsigned int cur;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005717 int i;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005718
Todd Kjos1cfe6272017-05-24 13:33:28 -07005719 count = log_cur + 1;
5720 cur = count < ARRAY_SIZE(log->entry) && !log->full ?
5721 0 : count % ARRAY_SIZE(log->entry);
5722 if (count > ARRAY_SIZE(log->entry) || log->full)
5723 count = ARRAY_SIZE(log->entry);
5724 for (i = 0; i < count; i++) {
5725 unsigned int index = cur++ % ARRAY_SIZE(log->entry);
5726
5727 print_binder_transaction_log_entry(m, &log->entry[index]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005728 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005729 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005730}
5731
5732static const struct file_operations binder_fops = {
5733 .owner = THIS_MODULE,
5734 .poll = binder_poll,
5735 .unlocked_ioctl = binder_ioctl,
Arve Hjønnevågda498892014-02-21 14:40:26 -08005736 .compat_ioctl = binder_ioctl,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005737 .mmap = binder_mmap,
5738 .open = binder_open,
5739 .flush = binder_flush,
5740 .release = binder_release,
5741};
5742
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005743BINDER_DEBUG_ENTRY(state);
5744BINDER_DEBUG_ENTRY(stats);
5745BINDER_DEBUG_ENTRY(transactions);
5746BINDER_DEBUG_ENTRY(transaction_log);
5747
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005748static int __init init_binder_device(const char *name)
5749{
5750 int ret;
5751 struct binder_device *binder_device;
5752
5753 binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
5754 if (!binder_device)
5755 return -ENOMEM;
5756
5757 binder_device->miscdev.fops = &binder_fops;
5758 binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
5759 binder_device->miscdev.name = name;
5760
5761 binder_device->context.binder_context_mgr_uid = INVALID_UID;
5762 binder_device->context.name = name;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005763 mutex_init(&binder_device->context.context_mgr_node_lock);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005764
5765 ret = misc_register(&binder_device->miscdev);
5766 if (ret < 0) {
5767 kfree(binder_device);
5768 return ret;
5769 }
5770
5771 hlist_add_head(&binder_device->hlist, &binder_devices);
5772
5773 return ret;
5774}
5775
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005776static int __init binder_init(void)
5777{
5778 int ret;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005779 char *device_name, *device_names;
5780 struct binder_device *device;
5781 struct hlist_node *tmp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005782
Sherry Yang5828d702017-07-29 13:24:11 -07005783 binder_alloc_shrinker_init();
5784
Todd Kjos1cfe6272017-05-24 13:33:28 -07005785 atomic_set(&binder_transaction_log.cur, ~0U);
5786 atomic_set(&binder_transaction_log_failed.cur, ~0U);
5787
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005788 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
5789 if (binder_debugfs_dir_entry_root)
5790 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
5791 binder_debugfs_dir_entry_root);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005792
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005793 if (binder_debugfs_dir_entry_root) {
5794 debugfs_create_file("state",
5795 S_IRUGO,
5796 binder_debugfs_dir_entry_root,
5797 NULL,
5798 &binder_state_fops);
5799 debugfs_create_file("stats",
5800 S_IRUGO,
5801 binder_debugfs_dir_entry_root,
5802 NULL,
5803 &binder_stats_fops);
5804 debugfs_create_file("transactions",
5805 S_IRUGO,
5806 binder_debugfs_dir_entry_root,
5807 NULL,
5808 &binder_transactions_fops);
5809 debugfs_create_file("transaction_log",
5810 S_IRUGO,
5811 binder_debugfs_dir_entry_root,
5812 &binder_transaction_log,
5813 &binder_transaction_log_fops);
5814 debugfs_create_file("failed_transaction_log",
5815 S_IRUGO,
5816 binder_debugfs_dir_entry_root,
5817 &binder_transaction_log_failed,
5818 &binder_transaction_log_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005819 }
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005820
5821 /*
5822 * Copy the module_parameter string, because we don't want to
5823 * tokenize it in-place.
5824 */
5825 device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL);
5826 if (!device_names) {
5827 ret = -ENOMEM;
5828 goto err_alloc_device_names_failed;
5829 }
5830 strcpy(device_names, binder_devices_param);
5831
5832 while ((device_name = strsep(&device_names, ","))) {
5833 ret = init_binder_device(device_name);
5834 if (ret)
5835 goto err_init_binder_device_failed;
5836 }
5837
5838 return ret;
5839
5840err_init_binder_device_failed:
5841 hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
5842 misc_deregister(&device->miscdev);
5843 hlist_del(&device->hlist);
5844 kfree(device);
5845 }
5846err_alloc_device_names_failed:
5847 debugfs_remove_recursive(binder_debugfs_dir_entry_root);
5848
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005849 return ret;
5850}
5851
5852device_initcall(binder_init);
5853
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07005854#define CREATE_TRACE_POINTS
5855#include "binder_trace.h"
5856
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005857MODULE_LICENSE("GPL v2");