blob: f7f9a20d5d98304a1039218c4c3ce90c6bb865d1 [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>
Todd Kjosb9341022016-10-10 10:40:53 -070073#include "binder_alloc.h"
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -070074#include "binder_trace.h"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090075
Todd Kjos8d9f6f32016-10-17 12:33:15 -070076static HLIST_HEAD(binder_deferred_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090077static DEFINE_MUTEX(binder_deferred_lock);
78
Martijn Coenen6b7c7122016-09-30 16:08:09 +020079static HLIST_HEAD(binder_devices);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090080static HLIST_HEAD(binder_procs);
Todd Kjos8d9f6f32016-10-17 12:33:15 -070081static DEFINE_MUTEX(binder_procs_lock);
82
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090083static HLIST_HEAD(binder_dead_nodes);
Todd Kjos8d9f6f32016-10-17 12:33:15 -070084static DEFINE_SPINLOCK(binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090085
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070086static struct dentry *binder_debugfs_dir_entry_root;
87static struct dentry *binder_debugfs_dir_entry_proc;
Todd Kjosc4bd08b2017-05-25 10:56:00 -070088static atomic_t binder_last_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090089
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070090#define BINDER_DEBUG_ENTRY(name) \
91static int binder_##name##_open(struct inode *inode, struct file *file) \
92{ \
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070093 return single_open(file, binder_##name##_show, inode->i_private); \
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070094} \
95\
96static const struct file_operations binder_##name##_fops = { \
97 .owner = THIS_MODULE, \
98 .open = binder_##name##_open, \
99 .read = seq_read, \
100 .llseek = seq_lseek, \
101 .release = single_release, \
102}
103
104static int binder_proc_show(struct seq_file *m, void *unused);
105BINDER_DEBUG_ENTRY(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900106
107/* This is only defined in include/asm-arm/sizes.h */
108#ifndef SZ_1K
109#define SZ_1K 0x400
110#endif
111
112#ifndef SZ_4M
113#define SZ_4M 0x400000
114#endif
115
116#define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
117
118#define BINDER_SMALL_BUF_SIZE (PAGE_SIZE * 64)
119
120enum {
121 BINDER_DEBUG_USER_ERROR = 1U << 0,
122 BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1,
123 BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2,
124 BINDER_DEBUG_OPEN_CLOSE = 1U << 3,
125 BINDER_DEBUG_DEAD_BINDER = 1U << 4,
126 BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5,
127 BINDER_DEBUG_READ_WRITE = 1U << 6,
128 BINDER_DEBUG_USER_REFS = 1U << 7,
129 BINDER_DEBUG_THREADS = 1U << 8,
130 BINDER_DEBUG_TRANSACTION = 1U << 9,
131 BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10,
132 BINDER_DEBUG_FREE_BUFFER = 1U << 11,
133 BINDER_DEBUG_INTERNAL_REFS = 1U << 12,
Todd Kjosd325d372016-10-10 10:40:53 -0700134 BINDER_DEBUG_PRIORITY_CAP = 1U << 13,
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700135 BINDER_DEBUG_SPINLOCKS = 1U << 14,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900136};
137static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR |
138 BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION;
Harsh Shandilya174562a2017-12-22 19:37:02 +0530139module_param_named(debug_mask, binder_debug_mask, uint, 0644);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900140
Martijn Coenen6b7c7122016-09-30 16:08:09 +0200141static char *binder_devices_param = CONFIG_ANDROID_BINDER_DEVICES;
142module_param_named(devices, binder_devices_param, charp, S_IRUGO);
143
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900144static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
145static int binder_stop_on_user_error;
146
147static int binder_set_stop_on_user_error(const char *val,
Kees Cook24da2c82017-10-17 19:04:42 -0700148 const struct kernel_param *kp)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900149{
150 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +0900151
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900152 ret = param_set_int(val, kp);
153 if (binder_stop_on_user_error < 2)
154 wake_up(&binder_user_error_wait);
155 return ret;
156}
157module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
Harsh Shandilya174562a2017-12-22 19:37:02 +0530158 param_get_int, &binder_stop_on_user_error, 0644);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900159
160#define binder_debug(mask, x...) \
161 do { \
162 if (binder_debug_mask & mask) \
Sherwin Soltani258767f2012-06-26 02:00:30 -0400163 pr_info(x); \
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900164 } while (0)
165
166#define binder_user_error(x...) \
167 do { \
168 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
Sherwin Soltani258767f2012-06-26 02:00:30 -0400169 pr_info(x); \
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900170 if (binder_stop_on_user_error) \
171 binder_stop_on_user_error = 2; \
172 } while (0)
173
Martijn Coenen00c80372016-07-13 12:06:49 +0200174#define to_flat_binder_object(hdr) \
175 container_of(hdr, struct flat_binder_object, hdr)
176
177#define to_binder_fd_object(hdr) container_of(hdr, struct binder_fd_object, hdr)
178
Martijn Coenen5a6da532016-09-30 14:10:07 +0200179#define to_binder_buffer_object(hdr) \
180 container_of(hdr, struct binder_buffer_object, hdr)
181
Martijn Coenene3e0f4802016-10-18 13:58:55 +0200182#define to_binder_fd_array_object(hdr) \
183 container_of(hdr, struct binder_fd_array_object, hdr)
184
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900185enum binder_stat_types {
186 BINDER_STAT_PROC,
187 BINDER_STAT_THREAD,
188 BINDER_STAT_NODE,
189 BINDER_STAT_REF,
190 BINDER_STAT_DEATH,
191 BINDER_STAT_TRANSACTION,
192 BINDER_STAT_TRANSACTION_COMPLETE,
193 BINDER_STAT_COUNT
194};
195
196struct binder_stats {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -0700197 atomic_t br[_IOC_NR(BR_FAILED_REPLY) + 1];
198 atomic_t bc[_IOC_NR(BC_REPLY_SG) + 1];
199 atomic_t obj_created[BINDER_STAT_COUNT];
200 atomic_t obj_deleted[BINDER_STAT_COUNT];
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900201};
202
203static struct binder_stats binder_stats;
204
205static inline void binder_stats_deleted(enum binder_stat_types type)
206{
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -0700207 atomic_inc(&binder_stats.obj_deleted[type]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900208}
209
210static inline void binder_stats_created(enum binder_stat_types type)
211{
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -0700212 atomic_inc(&binder_stats.obj_created[type]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900213}
214
215struct binder_transaction_log_entry {
216 int debug_id;
Todd Kjos1cfe6272017-05-24 13:33:28 -0700217 int debug_id_done;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900218 int call_type;
219 int from_proc;
220 int from_thread;
221 int target_handle;
222 int to_proc;
223 int to_thread;
224 int to_node;
225 int data_size;
226 int offsets_size;
Todd Kjose598d172017-03-22 17:19:52 -0700227 int return_error_line;
228 uint32_t return_error;
229 uint32_t return_error_param;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +0200230 const char *context_name;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900231};
232struct binder_transaction_log {
Todd Kjos1cfe6272017-05-24 13:33:28 -0700233 atomic_t cur;
234 bool full;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900235 struct binder_transaction_log_entry entry[32];
236};
237static struct binder_transaction_log binder_transaction_log;
238static struct binder_transaction_log binder_transaction_log_failed;
239
240static struct binder_transaction_log_entry *binder_transaction_log_add(
241 struct binder_transaction_log *log)
242{
243 struct binder_transaction_log_entry *e;
Todd Kjos1cfe6272017-05-24 13:33:28 -0700244 unsigned int cur = atomic_inc_return(&log->cur);
Seunghun Lee10f62862014-05-01 01:30:23 +0900245
Todd Kjos1cfe6272017-05-24 13:33:28 -0700246 if (cur >= ARRAY_SIZE(log->entry))
Gustavo A. R. Silvae62dd6f2018-01-23 12:04:27 -0600247 log->full = true;
Todd Kjos1cfe6272017-05-24 13:33:28 -0700248 e = &log->entry[cur % ARRAY_SIZE(log->entry)];
249 WRITE_ONCE(e->debug_id_done, 0);
250 /*
251 * write-barrier to synchronize access to e->debug_id_done.
252 * We make sure the initialized 0 value is seen before
253 * memset() other fields are zeroed by memset.
254 */
255 smp_wmb();
256 memset(e, 0, sizeof(*e));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900257 return e;
258}
259
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200260struct binder_context {
261 struct binder_node *binder_context_mgr_node;
Todd Kjos8d9f6f32016-10-17 12:33:15 -0700262 struct mutex context_mgr_node_lock;
263
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200264 kuid_t binder_context_mgr_uid;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +0200265 const char *name;
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200266};
267
Martijn Coenen6b7c7122016-09-30 16:08:09 +0200268struct binder_device {
269 struct hlist_node hlist;
270 struct miscdevice miscdev;
271 struct binder_context context;
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200272};
273
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700274/**
275 * struct binder_work - work enqueued on a worklist
276 * @entry: node enqueued on list
277 * @type: type of work to be performed
278 *
279 * There are separate work lists for proc, thread, and node (async).
280 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900281struct binder_work {
282 struct list_head entry;
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700283
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900284 enum {
285 BINDER_WORK_TRANSACTION = 1,
286 BINDER_WORK_TRANSACTION_COMPLETE,
Todd Kjos858b8da2017-04-21 17:35:12 -0700287 BINDER_WORK_RETURN_ERROR,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900288 BINDER_WORK_NODE,
289 BINDER_WORK_DEAD_BINDER,
290 BINDER_WORK_DEAD_BINDER_AND_CLEAR,
291 BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
292 } type;
293};
294
Todd Kjos858b8da2017-04-21 17:35:12 -0700295struct binder_error {
296 struct binder_work work;
297 uint32_t cmd;
298};
299
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700300/**
301 * struct binder_node - binder node bookkeeping
302 * @debug_id: unique ID for debugging
303 * (invariant after initialized)
304 * @lock: lock for node fields
305 * @work: worklist element for node work
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700306 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700307 * @rb_node: element for proc->nodes tree
Todd Kjos425d23f2017-06-12 12:07:26 -0700308 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700309 * @dead_node: element for binder_dead_nodes list
310 * (protected by binder_dead_nodes_lock)
311 * @proc: binder_proc that owns this node
312 * (invariant after initialized)
313 * @refs: list of references on this node
Todd Kjoscbcbbd62017-06-08 13:45:59 -0700314 * (protected by @lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700315 * @internal_strong_refs: used to take strong references when
316 * initiating a transaction
Todd Kjose7f23ed2017-03-21 13:06:01 -0700317 * (protected by @proc->inner_lock if @proc
318 * and by @lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700319 * @local_weak_refs: weak user refs from local process
Todd Kjose7f23ed2017-03-21 13:06:01 -0700320 * (protected by @proc->inner_lock if @proc
321 * and by @lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700322 * @local_strong_refs: strong user refs from local process
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 * @tmp_refs: temporary kernel refs
Todd Kjose7f23ed2017-03-21 13:06:01 -0700326 * (protected by @proc->inner_lock while @proc
327 * is valid, and by binder_dead_nodes_lock
328 * if @proc is NULL. During inc/dec and node release
329 * it is also protected by @lock to provide safety
330 * as the node dies and @proc becomes NULL)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700331 * @ptr: userspace pointer for node
332 * (invariant, no lock needed)
333 * @cookie: userspace cookie for node
334 * (invariant, no lock needed)
335 * @has_strong_ref: userspace notified of strong ref
Todd Kjose7f23ed2017-03-21 13:06:01 -0700336 * (protected by @proc->inner_lock if @proc
337 * and by @lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700338 * @pending_strong_ref: userspace has acked notification of strong ref
Todd Kjose7f23ed2017-03-21 13:06:01 -0700339 * (protected by @proc->inner_lock if @proc
340 * and by @lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700341 * @has_weak_ref: userspace notified of weak 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_weak_ref: userspace has acked notification of weak 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_async_transaction: async transaction to node in progress
Todd Kjoscbcbbd62017-06-08 13:45:59 -0700348 * (protected by @lock)
Martijn Coenen6aac9792017-06-07 09:29:14 -0700349 * @sched_policy: minimum scheduling policy for node
350 * (invariant after initialized)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700351 * @accept_fds: file descriptor operations supported for node
352 * (invariant after initialized)
353 * @min_priority: minimum scheduling priority
354 * (invariant after initialized)
Martijn Coenenc46810c2017-06-23 10:13:43 -0700355 * @inherit_rt: inherit RT scheduling policy from caller
Todd Kjos63e0afa2019-01-14 09:10:21 -0800356 * @txn_security_ctx: require sender's security context
Martijn Coenenc46810c2017-06-23 10:13:43 -0700357 * (invariant after initialized)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700358 * @async_todo: list of async work items
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700359 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700360 *
361 * Bookkeeping structure for binder nodes.
362 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900363struct binder_node {
364 int debug_id;
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700365 spinlock_t lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900366 struct binder_work work;
367 union {
368 struct rb_node rb_node;
369 struct hlist_node dead_node;
370 };
371 struct binder_proc *proc;
372 struct hlist_head refs;
373 int internal_strong_refs;
374 int local_weak_refs;
375 int local_strong_refs;
Todd Kjosf22abc72017-05-09 11:08:05 -0700376 int tmp_refs;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800377 binder_uintptr_t ptr;
378 binder_uintptr_t cookie;
Todd Kjose7f23ed2017-03-21 13:06:01 -0700379 struct {
380 /*
381 * bitfield elements protected by
382 * proc inner_lock
383 */
384 u8 has_strong_ref:1;
385 u8 pending_strong_ref:1;
386 u8 has_weak_ref:1;
387 u8 pending_weak_ref:1;
388 };
389 struct {
390 /*
391 * invariant after initialization
392 */
Martijn Coenen6aac9792017-06-07 09:29:14 -0700393 u8 sched_policy:2;
Martijn Coenenc46810c2017-06-23 10:13:43 -0700394 u8 inherit_rt:1;
Todd Kjose7f23ed2017-03-21 13:06:01 -0700395 u8 accept_fds:1;
Todd Kjos63e0afa2019-01-14 09:10:21 -0800396 u8 txn_security_ctx:1;
Todd Kjose7f23ed2017-03-21 13:06:01 -0700397 u8 min_priority;
398 };
399 bool has_async_transaction;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900400 struct list_head async_todo;
401};
402
403struct binder_ref_death {
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700404 /**
405 * @work: worklist element for death notifications
406 * (protected by inner_lock of the proc that
407 * this ref belongs to)
408 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900409 struct binder_work work;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800410 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900411};
412
Todd Kjosb0117bb2017-05-08 09:16:27 -0700413/**
414 * struct binder_ref_data - binder_ref counts and id
415 * @debug_id: unique ID for the ref
416 * @desc: unique userspace handle for ref
417 * @strong: strong ref count (debugging only if not locked)
418 * @weak: weak ref count (debugging only if not locked)
419 *
420 * Structure to hold ref count and ref id information. Since
421 * the actual ref can only be accessed with a lock, this structure
422 * is used to return information about the ref to callers of
423 * ref inc/dec functions.
424 */
425struct binder_ref_data {
426 int debug_id;
427 uint32_t desc;
428 int strong;
429 int weak;
430};
431
432/**
433 * struct binder_ref - struct to track references on nodes
434 * @data: binder_ref_data containing id, handle, and current refcounts
435 * @rb_node_desc: node for lookup by @data.desc in proc's rb_tree
436 * @rb_node_node: node for lookup by @node in proc's rb_tree
437 * @node_entry: list entry for node->refs list in target node
Todd Kjoscbcbbd62017-06-08 13:45:59 -0700438 * (protected by @node->lock)
Todd Kjosb0117bb2017-05-08 09:16:27 -0700439 * @proc: binder_proc containing ref
440 * @node: binder_node of target node. When cleaning up a
441 * ref for deletion in binder_cleanup_ref, a non-NULL
442 * @node indicates the node must be freed
443 * @death: pointer to death notification (ref_death) if requested
Martijn Coenenf9eac642017-05-22 11:26:23 -0700444 * (protected by @node->lock)
Todd Kjosb0117bb2017-05-08 09:16:27 -0700445 *
446 * Structure to track references from procA to target node (on procB). This
447 * structure is unsafe to access without holding @proc->outer_lock.
448 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900449struct binder_ref {
450 /* Lookups needed: */
451 /* node + proc => ref (transaction) */
452 /* desc + proc => ref (transaction, inc/dec ref) */
453 /* node => refs + procs (proc exit) */
Todd Kjosb0117bb2017-05-08 09:16:27 -0700454 struct binder_ref_data data;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900455 struct rb_node rb_node_desc;
456 struct rb_node rb_node_node;
457 struct hlist_node node_entry;
458 struct binder_proc *proc;
459 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900460 struct binder_ref_death *death;
461};
462
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900463enum binder_deferred_state {
Martijn Coenen6f7e5f92018-06-15 11:53:36 +0200464 BINDER_DEFERRED_PUT_FILES = 0x01,
465 BINDER_DEFERRED_FLUSH = 0x02,
466 BINDER_DEFERRED_RELEASE = 0x04,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900467};
468
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700469/**
Martijn Coenen57b2ac62017-06-06 17:04:42 -0700470 * struct binder_priority - scheduler policy and priority
471 * @sched_policy scheduler policy
472 * @prio [100..139] for SCHED_NORMAL, [0..99] for FIFO/RT
473 *
474 * The binder driver supports inheriting the following scheduler policies:
475 * SCHED_NORMAL
476 * SCHED_BATCH
477 * SCHED_FIFO
478 * SCHED_RR
479 */
480struct binder_priority {
481 unsigned int sched_policy;
482 int prio;
483};
484
485/**
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700486 * struct binder_proc - binder process bookkeeping
487 * @proc_node: element for binder_procs list
488 * @threads: rbtree of binder_threads in this proc
Todd Kjosb4827902017-05-25 15:52:17 -0700489 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700490 * @nodes: rbtree of binder nodes associated with
491 * this proc ordered by node->ptr
Todd Kjos425d23f2017-06-12 12:07:26 -0700492 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700493 * @refs_by_desc: rbtree of refs ordered by ref->desc
Todd Kjos5346bf32016-10-20 16:43:34 -0700494 * (protected by @outer_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700495 * @refs_by_node: rbtree of refs ordered by ref->node
Todd Kjos5346bf32016-10-20 16:43:34 -0700496 * (protected by @outer_lock)
Martijn Coenen22d64e4322017-06-02 11:15:44 -0700497 * @waiting_threads: threads currently waiting for proc work
498 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700499 * @pid PID of group_leader of process
500 * (invariant after initialized)
501 * @tsk task_struct for group_leader of process
502 * (invariant after initialized)
Martijn Coenen6f7e5f92018-06-15 11:53:36 +0200503 * @files files_struct for process
Todd Kjosfbb43392017-11-27 09:32:33 -0800504 * (protected by @files_lock)
505 * @files_lock mutex to protect @files
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 Kjosf4e6de32019-06-10 09:14:25 -0700528 * (atomic since @proc->inner_lock cannot
529 * always be acquired)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700530 * @default_priority: default scheduler priority
531 * (invariant after initialized)
532 * @debugfs_entry: debugfs node
533 * @alloc: binder allocator bookkeeping
534 * @context: binder_context for this proc
535 * (invariant after initialized)
536 * @inner_lock: can nest under outer_lock and/or node lock
537 * @outer_lock: no nesting under innor or node lock
538 * Lock order: 1) outer, 2) node, 3) inner
539 *
540 * Bookkeeping structure for binder processes
541 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900542struct binder_proc {
543 struct hlist_node proc_node;
544 struct rb_root threads;
545 struct rb_root nodes;
546 struct rb_root refs_by_desc;
547 struct rb_root refs_by_node;
Martijn Coenen22d64e4322017-06-02 11:15:44 -0700548 struct list_head waiting_threads;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900549 int pid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900550 struct task_struct *tsk;
Martijn Coenen6f7e5f92018-06-15 11:53:36 +0200551 struct files_struct *files;
Todd Kjosfbb43392017-11-27 09:32:33 -0800552 struct mutex files_lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900553 struct hlist_node deferred_work_node;
554 int deferred_work;
Todd Kjos2f993e22017-05-12 14:42:55 -0700555 bool is_dead;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900556
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900557 struct list_head todo;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900558 struct binder_stats stats;
559 struct list_head delivered_death;
560 int max_threads;
561 int requested_threads;
562 int requested_threads_started;
Todd Kjosf4e6de32019-06-10 09:14:25 -0700563 atomic_t tmp_ref;
Martijn Coenen57b2ac62017-06-06 17:04:42 -0700564 struct binder_priority default_priority;
Arve Hjønnevåg16b66552009-04-28 20:57:50 -0700565 struct dentry *debugfs_entry;
Todd Kjosf85d2292016-10-10 10:39:59 -0700566 struct binder_alloc alloc;
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200567 struct binder_context *context;
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700568 spinlock_t inner_lock;
569 spinlock_t outer_lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900570};
571
572enum {
573 BINDER_LOOPER_STATE_REGISTERED = 0x01,
574 BINDER_LOOPER_STATE_ENTERED = 0x02,
575 BINDER_LOOPER_STATE_EXITED = 0x04,
576 BINDER_LOOPER_STATE_INVALID = 0x08,
577 BINDER_LOOPER_STATE_WAITING = 0x10,
Martijn Coenen22d64e4322017-06-02 11:15:44 -0700578 BINDER_LOOPER_STATE_POLL = 0x20,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900579};
580
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700581/**
582 * struct binder_thread - binder thread bookkeeping
583 * @proc: binder process for this thread
584 * (invariant after initialization)
585 * @rb_node: element for proc->threads rbtree
Todd Kjosb4827902017-05-25 15:52:17 -0700586 * (protected by @proc->inner_lock)
Martijn Coenen22d64e4322017-06-02 11:15:44 -0700587 * @waiting_thread_node: element for @proc->waiting_threads list
588 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700589 * @pid: PID for this thread
590 * (invariant after initialization)
591 * @looper: bitmap of looping state
592 * (only accessed by this thread)
593 * @looper_needs_return: looping thread needs to exit driver
594 * (no lock needed)
595 * @transaction_stack: stack of in-progress transactions for this thread
Martijn Coenen995a36e2017-06-02 13:36:52 -0700596 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700597 * @todo: list of work to do for this thread
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700598 * (protected by @proc->inner_lock)
Martijn Coenen1af61802017-10-19 15:04:46 +0200599 * @process_todo: whether work in @todo should be processed
600 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700601 * @return_error: transaction errors reported by this thread
602 * (only accessed by this thread)
603 * @reply_error: transaction errors reported by target thread
Martijn Coenen995a36e2017-06-02 13:36:52 -0700604 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700605 * @wait: wait queue for thread work
606 * @stats: per-thread statistics
607 * (atomics, no lock needed)
608 * @tmp_ref: temporary reference to indicate thread is in use
609 * (atomic since @proc->inner_lock cannot
610 * always be acquired)
611 * @is_dead: thread is dead and awaiting free
612 * when outstanding transactions are cleaned up
Todd Kjosb4827902017-05-25 15:52:17 -0700613 * (protected by @proc->inner_lock)
Martijn Coenen07a30fe2017-06-07 10:02:12 -0700614 * @task: struct task_struct for this thread
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700615 *
616 * Bookkeeping structure for binder threads.
617 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900618struct binder_thread {
619 struct binder_proc *proc;
620 struct rb_node rb_node;
Martijn Coenen22d64e4322017-06-02 11:15:44 -0700621 struct list_head waiting_thread_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900622 int pid;
Todd Kjos6798e6d2017-01-06 14:19:25 -0800623 int looper; /* only modified by this thread */
624 bool looper_need_return; /* can be written by other thread */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900625 struct binder_transaction *transaction_stack;
626 struct list_head todo;
Martijn Coenen1af61802017-10-19 15:04:46 +0200627 bool process_todo;
Todd Kjos858b8da2017-04-21 17:35:12 -0700628 struct binder_error return_error;
629 struct binder_error reply_error;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900630 wait_queue_head_t wait;
631 struct binder_stats stats;
Todd Kjos2f993e22017-05-12 14:42:55 -0700632 atomic_t tmp_ref;
633 bool is_dead;
Martijn Coenen07a30fe2017-06-07 10:02:12 -0700634 struct task_struct *task;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900635};
636
637struct binder_transaction {
638 int debug_id;
639 struct binder_work work;
640 struct binder_thread *from;
641 struct binder_transaction *from_parent;
642 struct binder_proc *to_proc;
643 struct binder_thread *to_thread;
644 struct binder_transaction *to_parent;
645 unsigned need_reply:1;
646 /* unsigned is_dead:1; */ /* not used at the moment */
647
648 struct binder_buffer *buffer;
649 unsigned int code;
650 unsigned int flags;
Martijn Coenen57b2ac62017-06-06 17:04:42 -0700651 struct binder_priority priority;
652 struct binder_priority saved_priority;
Martijn Coenen07a30fe2017-06-07 10:02:12 -0700653 bool set_priority_called;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -0600654 kuid_t sender_euid;
Todd Kjos63e0afa2019-01-14 09:10:21 -0800655 binder_uintptr_t security_ctx;
Todd Kjos2f993e22017-05-12 14:42:55 -0700656 /**
657 * @lock: protects @from, @to_proc, and @to_thread
658 *
659 * @from, @to_proc, and @to_thread can be set to NULL
660 * during thread teardown
661 */
662 spinlock_t lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900663};
664
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700665/**
Todd Kjosd73356a2019-02-08 10:35:16 -0800666 * struct binder_object - union of flat binder object types
667 * @hdr: generic object header
668 * @fbo: binder object (nodes and refs)
669 * @fdo: file descriptor object
670 * @bbo: binder buffer pointer
671 * @fdao: file descriptor array
672 *
673 * Used for type-independent object copies
674 */
675struct binder_object {
676 union {
677 struct binder_object_header hdr;
678 struct flat_binder_object fbo;
679 struct binder_fd_object fdo;
680 struct binder_buffer_object bbo;
681 struct binder_fd_array_object fdao;
682 };
683};
684
685/**
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700686 * binder_proc_lock() - Acquire outer lock for given binder_proc
687 * @proc: struct binder_proc to acquire
688 *
689 * Acquires proc->outer_lock. Used to protect binder_ref
690 * structures associated with the given proc.
691 */
692#define binder_proc_lock(proc) _binder_proc_lock(proc, __LINE__)
693static void
694_binder_proc_lock(struct binder_proc *proc, int line)
695{
696 binder_debug(BINDER_DEBUG_SPINLOCKS,
697 "%s: line=%d\n", __func__, line);
698 spin_lock(&proc->outer_lock);
699}
700
701/**
702 * binder_proc_unlock() - Release spinlock for given binder_proc
703 * @proc: struct binder_proc to acquire
704 *
705 * Release lock acquired via binder_proc_lock()
706 */
707#define binder_proc_unlock(_proc) _binder_proc_unlock(_proc, __LINE__)
708static void
709_binder_proc_unlock(struct binder_proc *proc, int line)
710{
711 binder_debug(BINDER_DEBUG_SPINLOCKS,
712 "%s: line=%d\n", __func__, line);
713 spin_unlock(&proc->outer_lock);
714}
715
716/**
717 * binder_inner_proc_lock() - Acquire inner lock for given binder_proc
718 * @proc: struct binder_proc to acquire
719 *
720 * Acquires proc->inner_lock. Used to protect todo lists
721 */
722#define binder_inner_proc_lock(proc) _binder_inner_proc_lock(proc, __LINE__)
723static void
724_binder_inner_proc_lock(struct binder_proc *proc, int line)
725{
726 binder_debug(BINDER_DEBUG_SPINLOCKS,
727 "%s: line=%d\n", __func__, line);
728 spin_lock(&proc->inner_lock);
729}
730
731/**
732 * binder_inner_proc_unlock() - Release inner lock for given binder_proc
733 * @proc: struct binder_proc to acquire
734 *
735 * Release lock acquired via binder_inner_proc_lock()
736 */
737#define binder_inner_proc_unlock(proc) _binder_inner_proc_unlock(proc, __LINE__)
738static void
739_binder_inner_proc_unlock(struct binder_proc *proc, int line)
740{
741 binder_debug(BINDER_DEBUG_SPINLOCKS,
742 "%s: line=%d\n", __func__, line);
743 spin_unlock(&proc->inner_lock);
744}
745
746/**
747 * binder_node_lock() - Acquire spinlock for given binder_node
748 * @node: struct binder_node to acquire
749 *
750 * Acquires node->lock. Used to protect binder_node fields
751 */
752#define binder_node_lock(node) _binder_node_lock(node, __LINE__)
753static void
754_binder_node_lock(struct binder_node *node, int line)
755{
756 binder_debug(BINDER_DEBUG_SPINLOCKS,
757 "%s: line=%d\n", __func__, line);
758 spin_lock(&node->lock);
759}
760
761/**
762 * binder_node_unlock() - Release spinlock for given binder_proc
763 * @node: struct binder_node to acquire
764 *
765 * Release lock acquired via binder_node_lock()
766 */
767#define binder_node_unlock(node) _binder_node_unlock(node, __LINE__)
768static void
769_binder_node_unlock(struct binder_node *node, int line)
770{
771 binder_debug(BINDER_DEBUG_SPINLOCKS,
772 "%s: line=%d\n", __func__, line);
773 spin_unlock(&node->lock);
774}
775
Todd Kjoscbcbbd62017-06-08 13:45:59 -0700776/**
777 * binder_node_inner_lock() - Acquire node and inner locks
778 * @node: struct binder_node to acquire
779 *
780 * Acquires node->lock. If node->proc also acquires
781 * proc->inner_lock. Used to protect binder_node fields
782 */
783#define binder_node_inner_lock(node) _binder_node_inner_lock(node, __LINE__)
784static void
785_binder_node_inner_lock(struct binder_node *node, int line)
786{
787 binder_debug(BINDER_DEBUG_SPINLOCKS,
788 "%s: line=%d\n", __func__, line);
789 spin_lock(&node->lock);
790 if (node->proc)
791 binder_inner_proc_lock(node->proc);
792}
793
794/**
795 * binder_node_unlock() - Release node and inner locks
796 * @node: struct binder_node to acquire
797 *
798 * Release lock acquired via binder_node_lock()
799 */
800#define binder_node_inner_unlock(node) _binder_node_inner_unlock(node, __LINE__)
801static void
802_binder_node_inner_unlock(struct binder_node *node, int line)
803{
804 struct binder_proc *proc = node->proc;
805
806 binder_debug(BINDER_DEBUG_SPINLOCKS,
807 "%s: line=%d\n", __func__, line);
808 if (proc)
809 binder_inner_proc_unlock(proc);
810 spin_unlock(&node->lock);
811}
812
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700813static bool binder_worklist_empty_ilocked(struct list_head *list)
814{
815 return list_empty(list);
816}
817
818/**
819 * binder_worklist_empty() - Check if no items on the work list
820 * @proc: binder_proc associated with list
821 * @list: list to check
822 *
823 * Return: true if there are no items on list, else false
824 */
825static bool binder_worklist_empty(struct binder_proc *proc,
826 struct list_head *list)
827{
828 bool ret;
829
830 binder_inner_proc_lock(proc);
831 ret = binder_worklist_empty_ilocked(list);
832 binder_inner_proc_unlock(proc);
833 return ret;
834}
835
Martijn Coenen1af61802017-10-19 15:04:46 +0200836/**
837 * binder_enqueue_work_ilocked() - Add an item to the work list
838 * @work: struct binder_work to add to list
839 * @target_list: list to add work to
840 *
841 * Adds the work to the specified list. Asserts that work
842 * is not already on a list.
843 *
844 * Requires the proc->inner_lock to be held.
845 */
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700846static void
847binder_enqueue_work_ilocked(struct binder_work *work,
848 struct list_head *target_list)
849{
850 BUG_ON(target_list == NULL);
851 BUG_ON(work->entry.next && !list_empty(&work->entry));
852 list_add_tail(&work->entry, target_list);
853}
854
855/**
Martijn Coenendac2e9c2017-11-13 09:55:21 +0100856 * binder_enqueue_deferred_thread_work_ilocked() - Add deferred thread work
Martijn Coenen1af61802017-10-19 15:04:46 +0200857 * @thread: thread to queue work to
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700858 * @work: struct binder_work to add to list
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700859 *
Martijn Coenen1af61802017-10-19 15:04:46 +0200860 * Adds the work to the todo list of the thread. Doesn't set the process_todo
861 * flag, which means that (if it wasn't already set) the thread will go to
862 * sleep without handling this work when it calls read.
863 *
864 * Requires the proc->inner_lock to be held.
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700865 */
866static void
Martijn Coenendac2e9c2017-11-13 09:55:21 +0100867binder_enqueue_deferred_thread_work_ilocked(struct binder_thread *thread,
868 struct binder_work *work)
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700869{
Martijn Coenen1af61802017-10-19 15:04:46 +0200870 binder_enqueue_work_ilocked(work, &thread->todo);
871}
872
873/**
874 * binder_enqueue_thread_work_ilocked() - Add an item to the thread work list
875 * @thread: thread to queue work to
876 * @work: struct binder_work to add to list
877 *
878 * Adds the work to the todo list of the thread, and enables processing
879 * of the todo queue.
880 *
881 * Requires the proc->inner_lock to be held.
882 */
883static void
884binder_enqueue_thread_work_ilocked(struct binder_thread *thread,
885 struct binder_work *work)
886{
887 binder_enqueue_work_ilocked(work, &thread->todo);
888 thread->process_todo = true;
889}
890
891/**
892 * binder_enqueue_thread_work() - Add an item to the thread work list
893 * @thread: thread to queue work to
894 * @work: struct binder_work to add to list
895 *
896 * Adds the work to the todo list of the thread, and enables processing
897 * of the todo queue.
898 */
899static void
900binder_enqueue_thread_work(struct binder_thread *thread,
901 struct binder_work *work)
902{
903 binder_inner_proc_lock(thread->proc);
904 binder_enqueue_thread_work_ilocked(thread, work);
905 binder_inner_proc_unlock(thread->proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700906}
907
908static void
909binder_dequeue_work_ilocked(struct binder_work *work)
910{
911 list_del_init(&work->entry);
912}
913
914/**
915 * binder_dequeue_work() - Removes an item from the work list
916 * @proc: binder_proc associated with list
917 * @work: struct binder_work to remove from list
918 *
919 * Removes the specified work item from whatever list it is on.
920 * Can safely be called if work is not on any list.
921 */
922static void
923binder_dequeue_work(struct binder_proc *proc, struct binder_work *work)
924{
925 binder_inner_proc_lock(proc);
926 binder_dequeue_work_ilocked(work);
927 binder_inner_proc_unlock(proc);
928}
929
930static struct binder_work *binder_dequeue_work_head_ilocked(
931 struct list_head *list)
932{
933 struct binder_work *w;
934
935 w = list_first_entry_or_null(list, struct binder_work, entry);
936 if (w)
937 list_del_init(&w->entry);
938 return w;
939}
940
941/**
942 * binder_dequeue_work_head() - Dequeues the item at head of list
943 * @proc: binder_proc associated with list
944 * @list: list to dequeue head
945 *
946 * Removes the head of the list if there are items on the list
947 *
948 * Return: pointer dequeued binder_work, NULL if list was empty
949 */
950static struct binder_work *binder_dequeue_work_head(
951 struct binder_proc *proc,
952 struct list_head *list)
953{
954 struct binder_work *w;
955
956 binder_inner_proc_lock(proc);
957 w = binder_dequeue_work_head_ilocked(list);
958 binder_inner_proc_unlock(proc);
959 return w;
960}
961
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900962static void
963binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
Todd Kjos2f993e22017-05-12 14:42:55 -0700964static void binder_free_thread(struct binder_thread *thread);
965static void binder_free_proc(struct binder_proc *proc);
Todd Kjos425d23f2017-06-12 12:07:26 -0700966static void binder_inc_node_tmpref_ilocked(struct binder_node *node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900967
Sachin Kamatefde99c2012-08-17 16:39:36 +0530968static int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900969{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900970 unsigned long rlim_cur;
971 unsigned long irqs;
Todd Kjosfbb43392017-11-27 09:32:33 -0800972 int ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900973
Todd Kjosfbb43392017-11-27 09:32:33 -0800974 mutex_lock(&proc->files_lock);
975 if (proc->files == NULL) {
976 ret = -ESRCH;
977 goto err;
978 }
979 if (!lock_task_sighand(proc->tsk, &irqs)) {
980 ret = -EMFILE;
981 goto err;
982 }
Al Virodcfadfa2012-08-12 17:27:30 -0400983 rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
984 unlock_task_sighand(proc->tsk, &irqs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900985
Todd Kjosfbb43392017-11-27 09:32:33 -0800986 ret = __alloc_fd(proc->files, 0, rlim_cur, flags);
987err:
988 mutex_unlock(&proc->files_lock);
989 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900990}
991
992/*
993 * copied from fd_install
994 */
995static void task_fd_install(
996 struct binder_proc *proc, unsigned int fd, struct file *file)
997{
Todd Kjosfbb43392017-11-27 09:32:33 -0800998 mutex_lock(&proc->files_lock);
Martijn Coenen6f7e5f92018-06-15 11:53:36 +0200999 if (proc->files)
1000 __fd_install(proc->files, fd, file);
Todd Kjosfbb43392017-11-27 09:32:33 -08001001 mutex_unlock(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001002}
1003
1004/*
1005 * copied from sys_close
1006 */
1007static long task_close_fd(struct binder_proc *proc, unsigned int fd)
1008{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001009 int retval;
1010
Todd Kjosfbb43392017-11-27 09:32:33 -08001011 mutex_lock(&proc->files_lock);
1012 if (proc->files == NULL) {
1013 retval = -ESRCH;
1014 goto err;
1015 }
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02001016 retval = __close_fd(proc->files, fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001017 /* can't restart close syscall because file table entry was cleared */
1018 if (unlikely(retval == -ERESTARTSYS ||
1019 retval == -ERESTARTNOINTR ||
1020 retval == -ERESTARTNOHAND ||
1021 retval == -ERESTART_RESTARTBLOCK))
1022 retval = -EINTR;
Todd Kjosfbb43392017-11-27 09:32:33 -08001023err:
1024 mutex_unlock(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001025 return retval;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001026}
1027
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001028static bool binder_has_work_ilocked(struct binder_thread *thread,
1029 bool do_proc_work)
1030{
Martijn Coenen1af61802017-10-19 15:04:46 +02001031 return thread->process_todo ||
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001032 thread->looper_need_return ||
1033 (do_proc_work &&
1034 !binder_worklist_empty_ilocked(&thread->proc->todo));
1035}
1036
1037static bool binder_has_work(struct binder_thread *thread, bool do_proc_work)
1038{
1039 bool has_work;
1040
1041 binder_inner_proc_lock(thread->proc);
1042 has_work = binder_has_work_ilocked(thread, do_proc_work);
1043 binder_inner_proc_unlock(thread->proc);
1044
1045 return has_work;
1046}
1047
1048static bool binder_available_for_proc_work_ilocked(struct binder_thread *thread)
1049{
1050 return !thread->transaction_stack &&
1051 binder_worklist_empty_ilocked(&thread->todo) &&
1052 (thread->looper & (BINDER_LOOPER_STATE_ENTERED |
1053 BINDER_LOOPER_STATE_REGISTERED));
1054}
1055
1056static void binder_wakeup_poll_threads_ilocked(struct binder_proc *proc,
1057 bool sync)
1058{
1059 struct rb_node *n;
1060 struct binder_thread *thread;
1061
1062 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
1063 thread = rb_entry(n, struct binder_thread, rb_node);
1064 if (thread->looper & BINDER_LOOPER_STATE_POLL &&
1065 binder_available_for_proc_work_ilocked(thread)) {
1066 if (sync)
1067 wake_up_interruptible_sync(&thread->wait);
1068 else
1069 wake_up_interruptible(&thread->wait);
1070 }
1071 }
1072}
1073
Martijn Coenen053be422017-06-06 15:17:46 -07001074/**
1075 * binder_select_thread_ilocked() - selects a thread for doing proc work.
1076 * @proc: process to select a thread from
1077 *
1078 * Note that calling this function moves the thread off the waiting_threads
1079 * list, so it can only be woken up by the caller of this function, or a
1080 * signal. Therefore, callers *should* always wake up the thread this function
1081 * returns.
1082 *
1083 * Return: If there's a thread currently waiting for process work,
1084 * returns that thread. Otherwise returns NULL.
1085 */
1086static struct binder_thread *
1087binder_select_thread_ilocked(struct binder_proc *proc)
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001088{
1089 struct binder_thread *thread;
1090
Martijn Coenened323352017-07-27 23:52:24 +02001091 assert_spin_locked(&proc->inner_lock);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001092 thread = list_first_entry_or_null(&proc->waiting_threads,
1093 struct binder_thread,
1094 waiting_thread_node);
1095
Martijn Coenen053be422017-06-06 15:17:46 -07001096 if (thread)
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001097 list_del_init(&thread->waiting_thread_node);
Martijn Coenen053be422017-06-06 15:17:46 -07001098
1099 return thread;
1100}
1101
1102/**
1103 * binder_wakeup_thread_ilocked() - wakes up a thread for doing proc work.
1104 * @proc: process to wake up a thread in
1105 * @thread: specific thread to wake-up (may be NULL)
1106 * @sync: whether to do a synchronous wake-up
1107 *
1108 * This function wakes up a thread in the @proc process.
1109 * The caller may provide a specific thread to wake-up in
1110 * the @thread parameter. If @thread is NULL, this function
1111 * will wake up threads that have called poll().
1112 *
1113 * Note that for this function to work as expected, callers
1114 * should first call binder_select_thread() to find a thread
1115 * to handle the work (if they don't have a thread already),
1116 * and pass the result into the @thread parameter.
1117 */
1118static void binder_wakeup_thread_ilocked(struct binder_proc *proc,
1119 struct binder_thread *thread,
1120 bool sync)
1121{
Martijn Coenened323352017-07-27 23:52:24 +02001122 assert_spin_locked(&proc->inner_lock);
Martijn Coenen053be422017-06-06 15:17:46 -07001123
1124 if (thread) {
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001125 if (sync)
1126 wake_up_interruptible_sync(&thread->wait);
1127 else
1128 wake_up_interruptible(&thread->wait);
1129 return;
1130 }
1131
1132 /* Didn't find a thread waiting for proc work; this can happen
1133 * in two scenarios:
1134 * 1. All threads are busy handling transactions
1135 * In that case, one of those threads should call back into
1136 * the kernel driver soon and pick up this work.
1137 * 2. Threads are using the (e)poll interface, in which case
1138 * they may be blocked on the waitqueue without having been
1139 * added to waiting_threads. For this case, we just iterate
1140 * over all threads not handling transaction work, and
1141 * wake them all up. We wake all because we don't know whether
1142 * a thread that called into (e)poll is handling non-binder
1143 * work currently.
1144 */
1145 binder_wakeup_poll_threads_ilocked(proc, sync);
1146}
1147
Martijn Coenen053be422017-06-06 15:17:46 -07001148static void binder_wakeup_proc_ilocked(struct binder_proc *proc)
1149{
1150 struct binder_thread *thread = binder_select_thread_ilocked(proc);
1151
1152 binder_wakeup_thread_ilocked(proc, thread, /* sync = */false);
1153}
1154
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001155static bool is_rt_policy(int policy)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001156{
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001157 return policy == SCHED_FIFO || policy == SCHED_RR;
1158}
Seunghun Lee10f62862014-05-01 01:30:23 +09001159
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001160static bool is_fair_policy(int policy)
1161{
1162 return policy == SCHED_NORMAL || policy == SCHED_BATCH;
1163}
1164
1165static bool binder_supported_policy(int policy)
1166{
1167 return is_fair_policy(policy) || is_rt_policy(policy);
1168}
1169
1170static int to_userspace_prio(int policy, int kernel_priority)
1171{
1172 if (is_fair_policy(policy))
1173 return PRIO_TO_NICE(kernel_priority);
1174 else
1175 return MAX_USER_RT_PRIO - 1 - kernel_priority;
1176}
1177
1178static int to_kernel_prio(int policy, int user_priority)
1179{
1180 if (is_fair_policy(policy))
1181 return NICE_TO_PRIO(user_priority);
1182 else
1183 return MAX_USER_RT_PRIO - 1 - user_priority;
1184}
1185
Martijn Coenenecd972d2017-05-26 10:48:56 -07001186static void binder_do_set_priority(struct task_struct *task,
1187 struct binder_priority desired,
1188 bool verify)
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001189{
1190 int priority; /* user-space prio value */
1191 bool has_cap_nice;
1192 unsigned int policy = desired.sched_policy;
1193
1194 if (task->policy == policy && task->normal_prio == desired.prio)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001195 return;
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001196
1197 has_cap_nice = has_capability_noaudit(task, CAP_SYS_NICE);
1198
1199 priority = to_userspace_prio(policy, desired.prio);
1200
Martijn Coenenecd972d2017-05-26 10:48:56 -07001201 if (verify && is_rt_policy(policy) && !has_cap_nice) {
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001202 long max_rtprio = task_rlimit(task, RLIMIT_RTPRIO);
1203
1204 if (max_rtprio == 0) {
1205 policy = SCHED_NORMAL;
1206 priority = MIN_NICE;
1207 } else if (priority > max_rtprio) {
1208 priority = max_rtprio;
1209 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001210 }
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001211
Martijn Coenenecd972d2017-05-26 10:48:56 -07001212 if (verify && is_fair_policy(policy) && !has_cap_nice) {
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001213 long min_nice = rlimit_to_nice(task_rlimit(task, RLIMIT_NICE));
1214
1215 if (min_nice > MAX_NICE) {
1216 binder_user_error("%d RLIMIT_NICE not set\n",
1217 task->pid);
1218 return;
1219 } else if (priority < min_nice) {
1220 priority = min_nice;
1221 }
1222 }
1223
1224 if (policy != desired.sched_policy ||
1225 to_kernel_prio(policy, priority) != desired.prio)
1226 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
1227 "%d: priority %d not allowed, using %d instead\n",
1228 task->pid, desired.prio,
1229 to_kernel_prio(policy, priority));
1230
Martijn Coenen81402ea2017-05-08 09:33:22 -07001231 trace_binder_set_priority(task->tgid, task->pid, task->normal_prio,
1232 to_kernel_prio(policy, priority),
1233 desired.prio);
1234
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001235 /* Set the actual priority */
1236 if (task->policy != policy || is_rt_policy(policy)) {
1237 struct sched_param params;
1238
1239 params.sched_priority = is_rt_policy(policy) ? priority : 0;
1240
1241 sched_setscheduler_nocheck(task,
1242 policy | SCHED_RESET_ON_FORK,
1243 &params);
1244 }
1245 if (is_fair_policy(policy))
1246 set_user_nice(task, priority);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001247}
1248
Martijn Coenenecd972d2017-05-26 10:48:56 -07001249static void binder_set_priority(struct task_struct *task,
1250 struct binder_priority desired)
1251{
1252 binder_do_set_priority(task, desired, /* verify = */ true);
1253}
1254
1255static void binder_restore_priority(struct task_struct *task,
1256 struct binder_priority desired)
1257{
1258 binder_do_set_priority(task, desired, /* verify = */ false);
1259}
1260
Martijn Coenen07a30fe2017-06-07 10:02:12 -07001261static void binder_transaction_priority(struct task_struct *task,
1262 struct binder_transaction *t,
Martijn Coenenc46810c2017-06-23 10:13:43 -07001263 struct binder_priority node_prio,
1264 bool inherit_rt)
Martijn Coenen07a30fe2017-06-07 10:02:12 -07001265{
Ganesh Mahendran9add7c42017-09-27 15:12:25 +08001266 struct binder_priority desired_prio = t->priority;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07001267
1268 if (t->set_priority_called)
1269 return;
1270
1271 t->set_priority_called = true;
1272 t->saved_priority.sched_policy = task->policy;
1273 t->saved_priority.prio = task->normal_prio;
1274
Martijn Coenenc46810c2017-06-23 10:13:43 -07001275 if (!inherit_rt && is_rt_policy(desired_prio.sched_policy)) {
1276 desired_prio.prio = NICE_TO_PRIO(0);
1277 desired_prio.sched_policy = SCHED_NORMAL;
Martijn Coenenc46810c2017-06-23 10:13:43 -07001278 }
Martijn Coenen07a30fe2017-06-07 10:02:12 -07001279
1280 if (node_prio.prio < t->priority.prio ||
1281 (node_prio.prio == t->priority.prio &&
1282 node_prio.sched_policy == SCHED_FIFO)) {
1283 /*
1284 * In case the minimum priority on the node is
1285 * higher (lower value), use that priority. If
1286 * the priority is the same, but the node uses
1287 * SCHED_FIFO, prefer SCHED_FIFO, since it can
1288 * run unbounded, unlike SCHED_RR.
1289 */
1290 desired_prio = node_prio;
1291 }
1292
1293 binder_set_priority(task, desired_prio);
1294}
1295
Todd Kjos425d23f2017-06-12 12:07:26 -07001296static struct binder_node *binder_get_node_ilocked(struct binder_proc *proc,
1297 binder_uintptr_t ptr)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001298{
1299 struct rb_node *n = proc->nodes.rb_node;
1300 struct binder_node *node;
1301
Martijn Coenened323352017-07-27 23:52:24 +02001302 assert_spin_locked(&proc->inner_lock);
Todd Kjos425d23f2017-06-12 12:07:26 -07001303
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001304 while (n) {
1305 node = rb_entry(n, struct binder_node, rb_node);
1306
1307 if (ptr < node->ptr)
1308 n = n->rb_left;
1309 else if (ptr > node->ptr)
1310 n = n->rb_right;
Todd Kjosf22abc72017-05-09 11:08:05 -07001311 else {
1312 /*
1313 * take an implicit weak reference
1314 * to ensure node stays alive until
1315 * call to binder_put_node()
1316 */
Todd Kjos425d23f2017-06-12 12:07:26 -07001317 binder_inc_node_tmpref_ilocked(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001318 return node;
Todd Kjosf22abc72017-05-09 11:08:05 -07001319 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001320 }
1321 return NULL;
1322}
1323
Todd Kjos425d23f2017-06-12 12:07:26 -07001324static struct binder_node *binder_get_node(struct binder_proc *proc,
1325 binder_uintptr_t ptr)
1326{
1327 struct binder_node *node;
1328
1329 binder_inner_proc_lock(proc);
1330 node = binder_get_node_ilocked(proc, ptr);
1331 binder_inner_proc_unlock(proc);
1332 return node;
1333}
1334
1335static struct binder_node *binder_init_node_ilocked(
1336 struct binder_proc *proc,
1337 struct binder_node *new_node,
1338 struct flat_binder_object *fp)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001339{
1340 struct rb_node **p = &proc->nodes.rb_node;
1341 struct rb_node *parent = NULL;
1342 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001343 binder_uintptr_t ptr = fp ? fp->binder : 0;
1344 binder_uintptr_t cookie = fp ? fp->cookie : 0;
1345 __u32 flags = fp ? fp->flags : 0;
Martijn Coenen6aac9792017-06-07 09:29:14 -07001346 s8 priority;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001347
Martijn Coenened323352017-07-27 23:52:24 +02001348 assert_spin_locked(&proc->inner_lock);
1349
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001350 while (*p) {
Todd Kjos425d23f2017-06-12 12:07:26 -07001351
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001352 parent = *p;
1353 node = rb_entry(parent, struct binder_node, rb_node);
1354
1355 if (ptr < node->ptr)
1356 p = &(*p)->rb_left;
1357 else if (ptr > node->ptr)
1358 p = &(*p)->rb_right;
Todd Kjos425d23f2017-06-12 12:07:26 -07001359 else {
1360 /*
1361 * A matching node is already in
1362 * the rb tree. Abandon the init
1363 * and return it.
1364 */
1365 binder_inc_node_tmpref_ilocked(node);
1366 return node;
1367 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001368 }
Todd Kjos425d23f2017-06-12 12:07:26 -07001369 node = new_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001370 binder_stats_created(BINDER_STAT_NODE);
Todd Kjosf22abc72017-05-09 11:08:05 -07001371 node->tmp_refs++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001372 rb_link_node(&node->rb_node, parent, p);
1373 rb_insert_color(&node->rb_node, &proc->nodes);
Todd Kjosc4bd08b2017-05-25 10:56:00 -07001374 node->debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001375 node->proc = proc;
1376 node->ptr = ptr;
1377 node->cookie = cookie;
1378 node->work.type = BINDER_WORK_NODE;
Martijn Coenen6aac9792017-06-07 09:29:14 -07001379 priority = flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
Ganesh Mahendran6cd26312017-09-26 17:56:25 +08001380 node->sched_policy = (flags & FLAT_BINDER_FLAG_SCHED_POLICY_MASK) >>
Martijn Coenen6aac9792017-06-07 09:29:14 -07001381 FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT;
1382 node->min_priority = to_kernel_prio(node->sched_policy, priority);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001383 node->accept_fds = !!(flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
Martijn Coenenc46810c2017-06-23 10:13:43 -07001384 node->inherit_rt = !!(flags & FLAT_BINDER_FLAG_INHERIT_RT);
Todd Kjos63e0afa2019-01-14 09:10:21 -08001385 node->txn_security_ctx = !!(flags & FLAT_BINDER_FLAG_TXN_SECURITY_CTX);
Todd Kjosfc7a7e22017-05-29 16:44:24 -07001386 spin_lock_init(&node->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001387 INIT_LIST_HEAD(&node->work.entry);
1388 INIT_LIST_HEAD(&node->async_todo);
1389 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001390 "%d:%d node %d u%016llx c%016llx created\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001391 proc->pid, current->pid, node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001392 (u64)node->ptr, (u64)node->cookie);
Todd Kjos425d23f2017-06-12 12:07:26 -07001393
1394 return node;
1395}
1396
1397static struct binder_node *binder_new_node(struct binder_proc *proc,
1398 struct flat_binder_object *fp)
1399{
1400 struct binder_node *node;
1401 struct binder_node *new_node = kzalloc(sizeof(*node), GFP_KERNEL);
1402
1403 if (!new_node)
1404 return NULL;
1405 binder_inner_proc_lock(proc);
1406 node = binder_init_node_ilocked(proc, new_node, fp);
1407 binder_inner_proc_unlock(proc);
1408 if (node != new_node)
1409 /*
1410 * The node was already added by another thread
1411 */
1412 kfree(new_node);
1413
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001414 return node;
1415}
1416
Todd Kjose7f23ed2017-03-21 13:06:01 -07001417static void binder_free_node(struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001418{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001419 kfree(node);
1420 binder_stats_deleted(BINDER_STAT_NODE);
1421}
1422
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001423static int binder_inc_node_nilocked(struct binder_node *node, int strong,
1424 int internal,
1425 struct list_head *target_list)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001426{
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001427 struct binder_proc *proc = node->proc;
1428
Martijn Coenened323352017-07-27 23:52:24 +02001429 assert_spin_locked(&node->lock);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001430 if (proc)
Martijn Coenened323352017-07-27 23:52:24 +02001431 assert_spin_locked(&proc->inner_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001432 if (strong) {
1433 if (internal) {
1434 if (target_list == NULL &&
1435 node->internal_strong_refs == 0 &&
Martijn Coenen0b3311e2016-09-30 15:51:48 +02001436 !(node->proc &&
1437 node == node->proc->context->
1438 binder_context_mgr_node &&
1439 node->has_strong_ref)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301440 pr_err("invalid inc strong node for %d\n",
1441 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001442 return -EINVAL;
1443 }
1444 node->internal_strong_refs++;
1445 } else
1446 node->local_strong_refs++;
1447 if (!node->has_strong_ref && target_list) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001448 binder_dequeue_work_ilocked(&node->work);
Martijn Coenen1af61802017-10-19 15:04:46 +02001449 /*
1450 * Note: this function is the only place where we queue
1451 * directly to a thread->todo without using the
1452 * corresponding binder_enqueue_thread_work() helper
1453 * functions; in this case it's ok to not set the
1454 * process_todo flag, since we know this node work will
1455 * always be followed by other work that starts queue
1456 * processing: in case of synchronous transactions, a
1457 * BR_REPLY or BR_ERROR; in case of oneway
1458 * transactions, a BR_TRANSACTION_COMPLETE.
1459 */
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001460 binder_enqueue_work_ilocked(&node->work, target_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001461 }
1462 } else {
1463 if (!internal)
1464 node->local_weak_refs++;
1465 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
1466 if (target_list == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301467 pr_err("invalid inc weak node for %d\n",
1468 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001469 return -EINVAL;
1470 }
Martijn Coenen1af61802017-10-19 15:04:46 +02001471 /*
1472 * See comment above
1473 */
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001474 binder_enqueue_work_ilocked(&node->work, target_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001475 }
1476 }
1477 return 0;
1478}
1479
Todd Kjose7f23ed2017-03-21 13:06:01 -07001480static int binder_inc_node(struct binder_node *node, int strong, int internal,
1481 struct list_head *target_list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001482{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001483 int ret;
1484
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001485 binder_node_inner_lock(node);
1486 ret = binder_inc_node_nilocked(node, strong, internal, target_list);
1487 binder_node_inner_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001488
1489 return ret;
1490}
1491
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001492static bool binder_dec_node_nilocked(struct binder_node *node,
1493 int strong, int internal)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001494{
1495 struct binder_proc *proc = node->proc;
1496
Martijn Coenened323352017-07-27 23:52:24 +02001497 assert_spin_locked(&node->lock);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001498 if (proc)
Martijn Coenened323352017-07-27 23:52:24 +02001499 assert_spin_locked(&proc->inner_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001500 if (strong) {
1501 if (internal)
1502 node->internal_strong_refs--;
1503 else
1504 node->local_strong_refs--;
1505 if (node->local_strong_refs || node->internal_strong_refs)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001506 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001507 } else {
1508 if (!internal)
1509 node->local_weak_refs--;
Todd Kjosf22abc72017-05-09 11:08:05 -07001510 if (node->local_weak_refs || node->tmp_refs ||
1511 !hlist_empty(&node->refs))
Todd Kjose7f23ed2017-03-21 13:06:01 -07001512 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001513 }
Todd Kjose7f23ed2017-03-21 13:06:01 -07001514
1515 if (proc && (node->has_strong_ref || node->has_weak_ref)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001516 if (list_empty(&node->work.entry)) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001517 binder_enqueue_work_ilocked(&node->work, &proc->todo);
Martijn Coenen053be422017-06-06 15:17:46 -07001518 binder_wakeup_proc_ilocked(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001519 }
1520 } else {
1521 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
Todd Kjosf22abc72017-05-09 11:08:05 -07001522 !node->local_weak_refs && !node->tmp_refs) {
Todd Kjose7f23ed2017-03-21 13:06:01 -07001523 if (proc) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001524 binder_dequeue_work_ilocked(&node->work);
1525 rb_erase(&node->rb_node, &proc->nodes);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001526 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301527 "refless node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001528 node->debug_id);
1529 } else {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001530 BUG_ON(!list_empty(&node->work.entry));
Todd Kjos8d9f6f32016-10-17 12:33:15 -07001531 spin_lock(&binder_dead_nodes_lock);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001532 /*
1533 * tmp_refs could have changed so
1534 * check it again
1535 */
1536 if (node->tmp_refs) {
1537 spin_unlock(&binder_dead_nodes_lock);
1538 return false;
1539 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001540 hlist_del(&node->dead_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07001541 spin_unlock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001542 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301543 "dead node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001544 node->debug_id);
1545 }
Todd Kjose7f23ed2017-03-21 13:06:01 -07001546 return true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001547 }
1548 }
Todd Kjose7f23ed2017-03-21 13:06:01 -07001549 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001550}
1551
Todd Kjose7f23ed2017-03-21 13:06:01 -07001552static void binder_dec_node(struct binder_node *node, int strong, int internal)
1553{
1554 bool free_node;
1555
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001556 binder_node_inner_lock(node);
1557 free_node = binder_dec_node_nilocked(node, strong, internal);
1558 binder_node_inner_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001559 if (free_node)
1560 binder_free_node(node);
1561}
1562
1563static void binder_inc_node_tmpref_ilocked(struct binder_node *node)
Todd Kjosf22abc72017-05-09 11:08:05 -07001564{
1565 /*
1566 * No call to binder_inc_node() is needed since we
1567 * don't need to inform userspace of any changes to
1568 * tmp_refs
1569 */
1570 node->tmp_refs++;
1571}
1572
1573/**
Todd Kjose7f23ed2017-03-21 13:06:01 -07001574 * binder_inc_node_tmpref() - take a temporary reference on node
1575 * @node: node to reference
1576 *
1577 * Take reference on node to prevent the node from being freed
1578 * while referenced only by a local variable. The inner lock is
1579 * needed to serialize with the node work on the queue (which
1580 * isn't needed after the node is dead). If the node is dead
1581 * (node->proc is NULL), use binder_dead_nodes_lock to protect
1582 * node->tmp_refs against dead-node-only cases where the node
1583 * lock cannot be acquired (eg traversing the dead node list to
1584 * print nodes)
1585 */
1586static void binder_inc_node_tmpref(struct binder_node *node)
1587{
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001588 binder_node_lock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001589 if (node->proc)
1590 binder_inner_proc_lock(node->proc);
1591 else
1592 spin_lock(&binder_dead_nodes_lock);
1593 binder_inc_node_tmpref_ilocked(node);
1594 if (node->proc)
1595 binder_inner_proc_unlock(node->proc);
1596 else
1597 spin_unlock(&binder_dead_nodes_lock);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001598 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001599}
1600
1601/**
Todd Kjosf22abc72017-05-09 11:08:05 -07001602 * binder_dec_node_tmpref() - remove a temporary reference on node
1603 * @node: node to reference
1604 *
1605 * Release temporary reference on node taken via binder_inc_node_tmpref()
1606 */
1607static void binder_dec_node_tmpref(struct binder_node *node)
1608{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001609 bool free_node;
1610
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001611 binder_node_inner_lock(node);
1612 if (!node->proc)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001613 spin_lock(&binder_dead_nodes_lock);
Todd Kjosf22abc72017-05-09 11:08:05 -07001614 node->tmp_refs--;
1615 BUG_ON(node->tmp_refs < 0);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001616 if (!node->proc)
1617 spin_unlock(&binder_dead_nodes_lock);
Todd Kjosf22abc72017-05-09 11:08:05 -07001618 /*
1619 * Call binder_dec_node() to check if all refcounts are 0
1620 * and cleanup is needed. Calling with strong=0 and internal=1
1621 * causes no actual reference to be released in binder_dec_node().
1622 * If that changes, a change is needed here too.
1623 */
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001624 free_node = binder_dec_node_nilocked(node, 0, 1);
1625 binder_node_inner_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001626 if (free_node)
1627 binder_free_node(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07001628}
1629
1630static void binder_put_node(struct binder_node *node)
1631{
1632 binder_dec_node_tmpref(node);
1633}
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001634
Todd Kjos5346bf32016-10-20 16:43:34 -07001635static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc,
1636 u32 desc, bool need_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001637{
1638 struct rb_node *n = proc->refs_by_desc.rb_node;
1639 struct binder_ref *ref;
1640
1641 while (n) {
1642 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1643
Todd Kjosb0117bb2017-05-08 09:16:27 -07001644 if (desc < ref->data.desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001645 n = n->rb_left;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001646 } else if (desc > ref->data.desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001647 n = n->rb_right;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001648 } else if (need_strong_ref && !ref->data.strong) {
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001649 binder_user_error("tried to use weak ref as strong ref\n");
1650 return NULL;
1651 } else {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001652 return ref;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001653 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001654 }
1655 return NULL;
1656}
1657
Todd Kjosb0117bb2017-05-08 09:16:27 -07001658/**
Todd Kjos5346bf32016-10-20 16:43:34 -07001659 * binder_get_ref_for_node_olocked() - get the ref associated with given node
Todd Kjosb0117bb2017-05-08 09:16:27 -07001660 * @proc: binder_proc that owns the ref
1661 * @node: binder_node of target
1662 * @new_ref: newly allocated binder_ref to be initialized or %NULL
1663 *
1664 * Look up the ref for the given node and return it if it exists
1665 *
1666 * If it doesn't exist and the caller provides a newly allocated
1667 * ref, initialize the fields of the newly allocated ref and insert
1668 * into the given proc rb_trees and node refs list.
1669 *
1670 * Return: the ref for node. It is possible that another thread
1671 * allocated/initialized the ref first in which case the
1672 * returned ref would be different than the passed-in
1673 * new_ref. new_ref must be kfree'd by the caller in
1674 * this case.
1675 */
Todd Kjos5346bf32016-10-20 16:43:34 -07001676static struct binder_ref *binder_get_ref_for_node_olocked(
1677 struct binder_proc *proc,
1678 struct binder_node *node,
1679 struct binder_ref *new_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001680{
Todd Kjosb0117bb2017-05-08 09:16:27 -07001681 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001682 struct rb_node **p = &proc->refs_by_node.rb_node;
1683 struct rb_node *parent = NULL;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001684 struct binder_ref *ref;
1685 struct rb_node *n;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001686
1687 while (*p) {
1688 parent = *p;
1689 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1690
1691 if (node < ref->node)
1692 p = &(*p)->rb_left;
1693 else if (node > ref->node)
1694 p = &(*p)->rb_right;
1695 else
1696 return ref;
1697 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001698 if (!new_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001699 return NULL;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001700
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001701 binder_stats_created(BINDER_STAT_REF);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001702 new_ref->data.debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001703 new_ref->proc = proc;
1704 new_ref->node = node;
1705 rb_link_node(&new_ref->rb_node_node, parent, p);
1706 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1707
Todd Kjosb0117bb2017-05-08 09:16:27 -07001708 new_ref->data.desc = (node == context->binder_context_mgr_node) ? 0 : 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001709 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1710 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001711 if (ref->data.desc > new_ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001712 break;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001713 new_ref->data.desc = ref->data.desc + 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001714 }
1715
1716 p = &proc->refs_by_desc.rb_node;
1717 while (*p) {
1718 parent = *p;
1719 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1720
Todd Kjosb0117bb2017-05-08 09:16:27 -07001721 if (new_ref->data.desc < ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001722 p = &(*p)->rb_left;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001723 else if (new_ref->data.desc > ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001724 p = &(*p)->rb_right;
1725 else
1726 BUG();
1727 }
1728 rb_link_node(&new_ref->rb_node_desc, parent, p);
1729 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001730
1731 binder_node_lock(node);
Todd Kjos4cbe5752017-05-01 17:21:51 -07001732 hlist_add_head(&new_ref->node_entry, &node->refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001733
Todd Kjos4cbe5752017-05-01 17:21:51 -07001734 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1735 "%d new ref %d desc %d for node %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001736 proc->pid, new_ref->data.debug_id, new_ref->data.desc,
Todd Kjos4cbe5752017-05-01 17:21:51 -07001737 node->debug_id);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001738 binder_node_unlock(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001739 return new_ref;
1740}
1741
Todd Kjos5346bf32016-10-20 16:43:34 -07001742static void binder_cleanup_ref_olocked(struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001743{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001744 bool delete_node = false;
Todd Kjose7f23ed2017-03-21 13:06:01 -07001745
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001746 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301747 "%d delete ref %d desc %d for node %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001748 ref->proc->pid, ref->data.debug_id, ref->data.desc,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301749 ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001750
1751 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1752 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001753
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001754 binder_node_inner_lock(ref->node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001755 if (ref->data.strong)
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001756 binder_dec_node_nilocked(ref->node, 1, 1);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001757
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001758 hlist_del(&ref->node_entry);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001759 delete_node = binder_dec_node_nilocked(ref->node, 0, 1);
1760 binder_node_inner_unlock(ref->node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001761 /*
1762 * Clear ref->node unless we want the caller to free the node
1763 */
1764 if (!delete_node) {
1765 /*
1766 * The caller uses ref->node to determine
1767 * whether the node needs to be freed. Clear
1768 * it since the node is still alive.
1769 */
1770 ref->node = NULL;
1771 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001772
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001773 if (ref->death) {
1774 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301775 "%d delete ref %d desc %d has death notification\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001776 ref->proc->pid, ref->data.debug_id,
1777 ref->data.desc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001778 binder_dequeue_work(ref->proc, &ref->death->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001779 binder_stats_deleted(BINDER_STAT_DEATH);
1780 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001781 binder_stats_deleted(BINDER_STAT_REF);
1782}
1783
Todd Kjosb0117bb2017-05-08 09:16:27 -07001784/**
Todd Kjos5346bf32016-10-20 16:43:34 -07001785 * binder_inc_ref_olocked() - increment the ref for given handle
Todd Kjosb0117bb2017-05-08 09:16:27 -07001786 * @ref: ref to be incremented
1787 * @strong: if true, strong increment, else weak
1788 * @target_list: list to queue node work on
1789 *
Todd Kjos5346bf32016-10-20 16:43:34 -07001790 * Increment the ref. @ref->proc->outer_lock must be held on entry
Todd Kjosb0117bb2017-05-08 09:16:27 -07001791 *
1792 * Return: 0, if successful, else errno
1793 */
Todd Kjos5346bf32016-10-20 16:43:34 -07001794static int binder_inc_ref_olocked(struct binder_ref *ref, int strong,
1795 struct list_head *target_list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001796{
1797 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +09001798
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001799 if (strong) {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001800 if (ref->data.strong == 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001801 ret = binder_inc_node(ref->node, 1, 1, target_list);
1802 if (ret)
1803 return ret;
1804 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001805 ref->data.strong++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001806 } else {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001807 if (ref->data.weak == 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001808 ret = binder_inc_node(ref->node, 0, 1, target_list);
1809 if (ret)
1810 return ret;
1811 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001812 ref->data.weak++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001813 }
1814 return 0;
1815}
1816
Todd Kjosb0117bb2017-05-08 09:16:27 -07001817/**
1818 * binder_dec_ref() - dec the ref for given handle
1819 * @ref: ref to be decremented
1820 * @strong: if true, strong decrement, else weak
1821 *
1822 * Decrement the ref.
1823 *
Todd Kjosb0117bb2017-05-08 09:16:27 -07001824 * Return: true if ref is cleaned up and ready to be freed
1825 */
Todd Kjos5346bf32016-10-20 16:43:34 -07001826static bool binder_dec_ref_olocked(struct binder_ref *ref, int strong)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001827{
1828 if (strong) {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001829 if (ref->data.strong == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301830 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001831 ref->proc->pid, ref->data.debug_id,
1832 ref->data.desc, ref->data.strong,
1833 ref->data.weak);
1834 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001835 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001836 ref->data.strong--;
Todd Kjose7f23ed2017-03-21 13:06:01 -07001837 if (ref->data.strong == 0)
1838 binder_dec_node(ref->node, strong, 1);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001839 } else {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001840 if (ref->data.weak == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301841 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001842 ref->proc->pid, ref->data.debug_id,
1843 ref->data.desc, ref->data.strong,
1844 ref->data.weak);
1845 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001846 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001847 ref->data.weak--;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001848 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001849 if (ref->data.strong == 0 && ref->data.weak == 0) {
Todd Kjos5346bf32016-10-20 16:43:34 -07001850 binder_cleanup_ref_olocked(ref);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001851 return true;
1852 }
1853 return false;
1854}
1855
1856/**
1857 * binder_get_node_from_ref() - get the node from the given proc/desc
1858 * @proc: proc containing the ref
1859 * @desc: the handle associated with the ref
1860 * @need_strong_ref: if true, only return node if ref is strong
1861 * @rdata: the id/refcount data for the ref
1862 *
1863 * Given a proc and ref handle, return the associated binder_node
1864 *
1865 * Return: a binder_node or NULL if not found or not strong when strong required
1866 */
1867static struct binder_node *binder_get_node_from_ref(
1868 struct binder_proc *proc,
1869 u32 desc, bool need_strong_ref,
1870 struct binder_ref_data *rdata)
1871{
1872 struct binder_node *node;
1873 struct binder_ref *ref;
1874
Todd Kjos5346bf32016-10-20 16:43:34 -07001875 binder_proc_lock(proc);
1876 ref = binder_get_ref_olocked(proc, desc, need_strong_ref);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001877 if (!ref)
1878 goto err_no_ref;
1879 node = ref->node;
Todd Kjosf22abc72017-05-09 11:08:05 -07001880 /*
1881 * Take an implicit reference on the node to ensure
1882 * it stays alive until the call to binder_put_node()
1883 */
1884 binder_inc_node_tmpref(node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001885 if (rdata)
1886 *rdata = ref->data;
Todd Kjos5346bf32016-10-20 16:43:34 -07001887 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001888
1889 return node;
1890
1891err_no_ref:
Todd Kjos5346bf32016-10-20 16:43:34 -07001892 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001893 return NULL;
1894}
1895
1896/**
1897 * binder_free_ref() - free the binder_ref
1898 * @ref: ref to free
1899 *
Todd Kjose7f23ed2017-03-21 13:06:01 -07001900 * Free the binder_ref. Free the binder_node indicated by ref->node
1901 * (if non-NULL) and the binder_ref_death indicated by ref->death.
Todd Kjosb0117bb2017-05-08 09:16:27 -07001902 */
1903static void binder_free_ref(struct binder_ref *ref)
1904{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001905 if (ref->node)
1906 binder_free_node(ref->node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001907 kfree(ref->death);
1908 kfree(ref);
1909}
1910
1911/**
1912 * binder_update_ref_for_handle() - inc/dec the ref for given handle
1913 * @proc: proc containing the ref
1914 * @desc: the handle associated with the ref
1915 * @increment: true=inc reference, false=dec reference
1916 * @strong: true=strong reference, false=weak reference
1917 * @rdata: the id/refcount data for the ref
1918 *
1919 * Given a proc and ref handle, increment or decrement the ref
1920 * according to "increment" arg.
1921 *
1922 * Return: 0 if successful, else errno
1923 */
1924static int binder_update_ref_for_handle(struct binder_proc *proc,
1925 uint32_t desc, bool increment, bool strong,
1926 struct binder_ref_data *rdata)
1927{
1928 int ret = 0;
1929 struct binder_ref *ref;
1930 bool delete_ref = false;
1931
Todd Kjos5346bf32016-10-20 16:43:34 -07001932 binder_proc_lock(proc);
1933 ref = binder_get_ref_olocked(proc, desc, strong);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001934 if (!ref) {
1935 ret = -EINVAL;
1936 goto err_no_ref;
1937 }
1938 if (increment)
Todd Kjos5346bf32016-10-20 16:43:34 -07001939 ret = binder_inc_ref_olocked(ref, strong, NULL);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001940 else
Todd Kjos5346bf32016-10-20 16:43:34 -07001941 delete_ref = binder_dec_ref_olocked(ref, strong);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001942
1943 if (rdata)
1944 *rdata = ref->data;
Todd Kjos5346bf32016-10-20 16:43:34 -07001945 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001946
1947 if (delete_ref)
1948 binder_free_ref(ref);
1949 return ret;
1950
1951err_no_ref:
Todd Kjos5346bf32016-10-20 16:43:34 -07001952 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001953 return ret;
1954}
1955
1956/**
1957 * binder_dec_ref_for_handle() - dec the ref for given handle
1958 * @proc: proc containing the ref
1959 * @desc: the handle associated with the ref
1960 * @strong: true=strong reference, false=weak reference
1961 * @rdata: the id/refcount data for the ref
1962 *
1963 * Just calls binder_update_ref_for_handle() to decrement the ref.
1964 *
1965 * Return: 0 if successful, else errno
1966 */
1967static int binder_dec_ref_for_handle(struct binder_proc *proc,
1968 uint32_t desc, bool strong, struct binder_ref_data *rdata)
1969{
1970 return binder_update_ref_for_handle(proc, desc, false, strong, rdata);
1971}
1972
1973
1974/**
1975 * binder_inc_ref_for_node() - increment the ref for given proc/node
1976 * @proc: proc containing the ref
1977 * @node: target node
1978 * @strong: true=strong reference, false=weak reference
1979 * @target_list: worklist to use if node is incremented
1980 * @rdata: the id/refcount data for the ref
1981 *
1982 * Given a proc and node, increment the ref. Create the ref if it
1983 * doesn't already exist
1984 *
1985 * Return: 0 if successful, else errno
1986 */
1987static int binder_inc_ref_for_node(struct binder_proc *proc,
1988 struct binder_node *node,
1989 bool strong,
1990 struct list_head *target_list,
1991 struct binder_ref_data *rdata)
1992{
1993 struct binder_ref *ref;
1994 struct binder_ref *new_ref = NULL;
1995 int ret = 0;
1996
Todd Kjos5346bf32016-10-20 16:43:34 -07001997 binder_proc_lock(proc);
1998 ref = binder_get_ref_for_node_olocked(proc, node, NULL);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001999 if (!ref) {
Todd Kjos5346bf32016-10-20 16:43:34 -07002000 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002001 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
2002 if (!new_ref)
2003 return -ENOMEM;
Todd Kjos5346bf32016-10-20 16:43:34 -07002004 binder_proc_lock(proc);
2005 ref = binder_get_ref_for_node_olocked(proc, node, new_ref);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002006 }
Todd Kjos5346bf32016-10-20 16:43:34 -07002007 ret = binder_inc_ref_olocked(ref, strong, target_list);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002008 *rdata = ref->data;
Todd Kjos5346bf32016-10-20 16:43:34 -07002009 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002010 if (new_ref && ref != new_ref)
2011 /*
2012 * Another thread created the ref first so
2013 * free the one we allocated
2014 */
2015 kfree(new_ref);
2016 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002017}
2018
Martijn Coenen995a36e2017-06-02 13:36:52 -07002019static void binder_pop_transaction_ilocked(struct binder_thread *target_thread,
2020 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002021{
Todd Kjos21ef40a2017-03-30 18:02:13 -07002022 BUG_ON(!target_thread);
Martijn Coenened323352017-07-27 23:52:24 +02002023 assert_spin_locked(&target_thread->proc->inner_lock);
Todd Kjos21ef40a2017-03-30 18:02:13 -07002024 BUG_ON(target_thread->transaction_stack != t);
2025 BUG_ON(target_thread->transaction_stack->from != target_thread);
2026 target_thread->transaction_stack =
2027 target_thread->transaction_stack->from_parent;
2028 t->from = NULL;
2029}
2030
Todd Kjos2f993e22017-05-12 14:42:55 -07002031/**
2032 * binder_thread_dec_tmpref() - decrement thread->tmp_ref
2033 * @thread: thread to decrement
2034 *
2035 * A thread needs to be kept alive while being used to create or
2036 * handle a transaction. binder_get_txn_from() is used to safely
2037 * extract t->from from a binder_transaction and keep the thread
2038 * indicated by t->from from being freed. When done with that
2039 * binder_thread, this function is called to decrement the
2040 * tmp_ref and free if appropriate (thread has been released
2041 * and no transaction being processed by the driver)
2042 */
2043static void binder_thread_dec_tmpref(struct binder_thread *thread)
2044{
2045 /*
2046 * atomic is used to protect the counter value while
2047 * it cannot reach zero or thread->is_dead is false
Todd Kjos2f993e22017-05-12 14:42:55 -07002048 */
Todd Kjosb4827902017-05-25 15:52:17 -07002049 binder_inner_proc_lock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002050 atomic_dec(&thread->tmp_ref);
2051 if (thread->is_dead && !atomic_read(&thread->tmp_ref)) {
Todd Kjosb4827902017-05-25 15:52:17 -07002052 binder_inner_proc_unlock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002053 binder_free_thread(thread);
2054 return;
2055 }
Todd Kjosb4827902017-05-25 15:52:17 -07002056 binder_inner_proc_unlock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002057}
2058
2059/**
2060 * binder_proc_dec_tmpref() - decrement proc->tmp_ref
2061 * @proc: proc to decrement
2062 *
2063 * A binder_proc needs to be kept alive while being used to create or
2064 * handle a transaction. proc->tmp_ref is incremented when
2065 * creating a new transaction or the binder_proc is currently in-use
2066 * by threads that are being released. When done with the binder_proc,
2067 * this function is called to decrement the counter and free the
2068 * proc if appropriate (proc has been released, all threads have
2069 * been released and not currenly in-use to process a transaction).
2070 */
2071static void binder_proc_dec_tmpref(struct binder_proc *proc)
2072{
Todd Kjosb4827902017-05-25 15:52:17 -07002073 binder_inner_proc_lock(proc);
Todd Kjosf4e6de32019-06-10 09:14:25 -07002074 atomic_dec(&proc->tmp_ref);
Todd Kjos2f993e22017-05-12 14:42:55 -07002075 if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) &&
Todd Kjosf4e6de32019-06-10 09:14:25 -07002076 !atomic_read(&proc->tmp_ref)) {
Todd Kjosb4827902017-05-25 15:52:17 -07002077 binder_inner_proc_unlock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002078 binder_free_proc(proc);
2079 return;
2080 }
Todd Kjosb4827902017-05-25 15:52:17 -07002081 binder_inner_proc_unlock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002082}
2083
2084/**
2085 * binder_get_txn_from() - safely extract the "from" thread in transaction
2086 * @t: binder transaction for t->from
2087 *
2088 * Atomically return the "from" thread and increment the tmp_ref
2089 * count for the thread to ensure it stays alive until
2090 * binder_thread_dec_tmpref() is called.
2091 *
2092 * Return: the value of t->from
2093 */
2094static struct binder_thread *binder_get_txn_from(
2095 struct binder_transaction *t)
2096{
2097 struct binder_thread *from;
2098
2099 spin_lock(&t->lock);
2100 from = t->from;
2101 if (from)
2102 atomic_inc(&from->tmp_ref);
2103 spin_unlock(&t->lock);
2104 return from;
2105}
2106
Martijn Coenen995a36e2017-06-02 13:36:52 -07002107/**
2108 * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock
2109 * @t: binder transaction for t->from
2110 *
2111 * Same as binder_get_txn_from() except it also acquires the proc->inner_lock
2112 * to guarantee that the thread cannot be released while operating on it.
2113 * The caller must call binder_inner_proc_unlock() to release the inner lock
2114 * as well as call binder_dec_thread_txn() to release the reference.
2115 *
2116 * Return: the value of t->from
2117 */
2118static struct binder_thread *binder_get_txn_from_and_acq_inner(
2119 struct binder_transaction *t)
2120{
2121 struct binder_thread *from;
2122
2123 from = binder_get_txn_from(t);
2124 if (!from)
2125 return NULL;
2126 binder_inner_proc_lock(from->proc);
2127 if (t->from) {
2128 BUG_ON(from != t->from);
2129 return from;
2130 }
2131 binder_inner_proc_unlock(from->proc);
2132 binder_thread_dec_tmpref(from);
2133 return NULL;
2134}
2135
Todd Kjos21ef40a2017-03-30 18:02:13 -07002136static void binder_free_transaction(struct binder_transaction *t)
2137{
Todd Kjosf4e6de32019-06-10 09:14:25 -07002138 struct binder_proc *target_proc;
2139
2140 spin_lock(&t->lock);
2141 target_proc = t->to_proc;
2142 if (target_proc) {
2143 atomic_inc(&target_proc->tmp_ref);
2144 spin_unlock(&t->lock);
2145
2146 binder_inner_proc_lock(target_proc);
2147 if (t->buffer)
2148 t->buffer->transaction = NULL;
2149 binder_inner_proc_unlock(target_proc);
2150 binder_proc_dec_tmpref(target_proc);
2151 } else {
2152 /*
2153 * If the transaction has no target_proc, then
2154 * t->buffer->transaction * has already been cleared.
2155 */
2156 spin_unlock(&t->lock);
2157 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002158 kfree(t);
2159 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2160}
2161
2162static void binder_send_failed_reply(struct binder_transaction *t,
2163 uint32_t error_code)
2164{
2165 struct binder_thread *target_thread;
Lucas Tanured4ec15e2014-07-13 21:31:05 -03002166 struct binder_transaction *next;
Seunghun Lee10f62862014-05-01 01:30:23 +09002167
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002168 BUG_ON(t->flags & TF_ONE_WAY);
2169 while (1) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07002170 target_thread = binder_get_txn_from_and_acq_inner(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002171 if (target_thread) {
Todd Kjos858b8da2017-04-21 17:35:12 -07002172 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2173 "send failed reply for transaction %d to %d:%d\n",
2174 t->debug_id,
2175 target_thread->proc->pid,
2176 target_thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002177
Martijn Coenen995a36e2017-06-02 13:36:52 -07002178 binder_pop_transaction_ilocked(target_thread, t);
Todd Kjos858b8da2017-04-21 17:35:12 -07002179 if (target_thread->reply_error.cmd == BR_OK) {
2180 target_thread->reply_error.cmd = error_code;
Martijn Coenen1af61802017-10-19 15:04:46 +02002181 binder_enqueue_thread_work_ilocked(
2182 target_thread,
2183 &target_thread->reply_error.work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002184 wake_up_interruptible(&target_thread->wait);
2185 } else {
Todd Kjosd3a2afb2018-02-07 12:38:47 -08002186 /*
2187 * Cannot get here for normal operation, but
2188 * we can if multiple synchronous transactions
2189 * are sent without blocking for responses.
2190 * Just ignore the 2nd error in this case.
2191 */
2192 pr_warn("Unexpected reply error: %u\n",
2193 target_thread->reply_error.cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002194 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07002195 binder_inner_proc_unlock(target_thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002196 binder_thread_dec_tmpref(target_thread);
Todd Kjos858b8da2017-04-21 17:35:12 -07002197 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002198 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002199 }
Lucas Tanured4ec15e2014-07-13 21:31:05 -03002200 next = t->from_parent;
2201
2202 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2203 "send failed reply for transaction %d, target dead\n",
2204 t->debug_id);
2205
Todd Kjos21ef40a2017-03-30 18:02:13 -07002206 binder_free_transaction(t);
Lucas Tanured4ec15e2014-07-13 21:31:05 -03002207 if (next == NULL) {
2208 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2209 "reply failed, no target thread at root\n");
2210 return;
2211 }
2212 t = next;
2213 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2214 "reply failed, no target thread -- retry %d\n",
2215 t->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002216 }
2217}
2218
Martijn Coenen00c80372016-07-13 12:06:49 +02002219/**
Martijn Coenen3217ccc2017-08-24 15:23:36 +02002220 * binder_cleanup_transaction() - cleans up undelivered transaction
2221 * @t: transaction that needs to be cleaned up
2222 * @reason: reason the transaction wasn't delivered
2223 * @error_code: error to return to caller (if synchronous call)
2224 */
2225static void binder_cleanup_transaction(struct binder_transaction *t,
2226 const char *reason,
2227 uint32_t error_code)
2228{
2229 if (t->buffer->target_node && !(t->flags & TF_ONE_WAY)) {
2230 binder_send_failed_reply(t, error_code);
2231 } else {
2232 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2233 "undelivered transaction %d, %s\n",
2234 t->debug_id, reason);
2235 binder_free_transaction(t);
2236 }
2237}
2238
2239/**
Todd Kjosd73356a2019-02-08 10:35:16 -08002240 * binder_get_object() - gets object and checks for valid metadata
2241 * @proc: binder_proc owning the buffer
Martijn Coenen00c80372016-07-13 12:06:49 +02002242 * @buffer: binder_buffer that we're parsing.
Todd Kjosd73356a2019-02-08 10:35:16 -08002243 * @offset: offset in the @buffer at which to validate an object.
2244 * @object: struct binder_object to read into
Martijn Coenen00c80372016-07-13 12:06:49 +02002245 *
2246 * Return: If there's a valid metadata object at @offset in @buffer, the
Todd Kjosd73356a2019-02-08 10:35:16 -08002247 * size of that object. Otherwise, it returns zero. The object
2248 * is read into the struct binder_object pointed to by @object.
Martijn Coenen00c80372016-07-13 12:06:49 +02002249 */
Todd Kjosd73356a2019-02-08 10:35:16 -08002250static size_t binder_get_object(struct binder_proc *proc,
2251 struct binder_buffer *buffer,
2252 unsigned long offset,
2253 struct binder_object *object)
Martijn Coenen00c80372016-07-13 12:06:49 +02002254{
Todd Kjosd73356a2019-02-08 10:35:16 -08002255 size_t read_size;
Martijn Coenen00c80372016-07-13 12:06:49 +02002256 struct binder_object_header *hdr;
2257 size_t object_size = 0;
2258
Todd Kjosd73356a2019-02-08 10:35:16 -08002259 read_size = min_t(size_t, sizeof(*object), buffer->data_size - offset);
Todd Kjosc8ddc8c2019-03-19 09:53:01 -07002260 if (offset > buffer->data_size || read_size < sizeof(*hdr) ||
Martijn Coenen00c80372016-07-13 12:06:49 +02002261 !IS_ALIGNED(offset, sizeof(u32)))
2262 return 0;
Todd Kjosd73356a2019-02-08 10:35:16 -08002263 binder_alloc_copy_from_buffer(&proc->alloc, object, buffer,
2264 offset, read_size);
Martijn Coenen00c80372016-07-13 12:06:49 +02002265
Todd Kjosd73356a2019-02-08 10:35:16 -08002266 /* Ok, now see if we read a complete object. */
2267 hdr = &object->hdr;
Martijn Coenen00c80372016-07-13 12:06:49 +02002268 switch (hdr->type) {
2269 case BINDER_TYPE_BINDER:
2270 case BINDER_TYPE_WEAK_BINDER:
2271 case BINDER_TYPE_HANDLE:
2272 case BINDER_TYPE_WEAK_HANDLE:
2273 object_size = sizeof(struct flat_binder_object);
2274 break;
2275 case BINDER_TYPE_FD:
2276 object_size = sizeof(struct binder_fd_object);
2277 break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002278 case BINDER_TYPE_PTR:
2279 object_size = sizeof(struct binder_buffer_object);
2280 break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002281 case BINDER_TYPE_FDA:
2282 object_size = sizeof(struct binder_fd_array_object);
2283 break;
Martijn Coenen00c80372016-07-13 12:06:49 +02002284 default:
2285 return 0;
2286 }
2287 if (offset <= buffer->data_size - object_size &&
2288 buffer->data_size >= object_size)
2289 return object_size;
2290 else
2291 return 0;
2292}
2293
Martijn Coenen5a6da532016-09-30 14:10:07 +02002294/**
2295 * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002296 * @proc: binder_proc owning the buffer
Martijn Coenen5a6da532016-09-30 14:10:07 +02002297 * @b: binder_buffer containing the object
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002298 * @object: struct binder_object to read into
Martijn Coenen5a6da532016-09-30 14:10:07 +02002299 * @index: index in offset array at which the binder_buffer_object is
2300 * located
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002301 * @start_offset: points to the start of the offset array
2302 * @object_offsetp: offset of @object read from @b
Martijn Coenen5a6da532016-09-30 14:10:07 +02002303 * @num_valid: the number of valid offsets in the offset array
2304 *
2305 * Return: If @index is within the valid range of the offset array
2306 * described by @start and @num_valid, and if there's a valid
2307 * binder_buffer_object at the offset found in index @index
2308 * of the offset array, that object is returned. Otherwise,
2309 * %NULL is returned.
2310 * Note that the offset found in index @index itself is not
2311 * verified; this function assumes that @num_valid elements
2312 * from @start were previously verified to have valid offsets.
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002313 * If @object_offsetp is non-NULL, then the offset within
2314 * @b is written to it.
Martijn Coenen5a6da532016-09-30 14:10:07 +02002315 */
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002316static struct binder_buffer_object *binder_validate_ptr(
2317 struct binder_proc *proc,
2318 struct binder_buffer *b,
2319 struct binder_object *object,
2320 binder_size_t index,
2321 binder_size_t start_offset,
2322 binder_size_t *object_offsetp,
2323 binder_size_t num_valid)
Martijn Coenen5a6da532016-09-30 14:10:07 +02002324{
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002325 size_t object_size;
2326 binder_size_t object_offset;
2327 unsigned long buffer_offset;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002328
2329 if (index >= num_valid)
2330 return NULL;
2331
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002332 buffer_offset = start_offset + sizeof(binder_size_t) * index;
2333 binder_alloc_copy_from_buffer(&proc->alloc, &object_offset,
2334 b, buffer_offset, sizeof(object_offset));
2335 object_size = binder_get_object(proc, b, object_offset, object);
2336 if (!object_size || object->hdr.type != BINDER_TYPE_PTR)
Martijn Coenen5a6da532016-09-30 14:10:07 +02002337 return NULL;
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002338 if (object_offsetp)
2339 *object_offsetp = object_offset;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002340
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002341 return &object->bbo;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002342}
2343
2344/**
2345 * binder_validate_fixup() - validates pointer/fd fixups happen in order.
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002346 * @proc: binder_proc owning the buffer
Martijn Coenen5a6da532016-09-30 14:10:07 +02002347 * @b: transaction buffer
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002348 * @objects_start_offset: offset to start of objects buffer
2349 * @buffer_obj_offset: offset to binder_buffer_object in which to fix up
2350 * @fixup_offset: start offset in @buffer to fix up
2351 * @last_obj_offset: offset to last binder_buffer_object that we fixed
2352 * @last_min_offset: minimum fixup offset in object at @last_obj_offset
Martijn Coenen5a6da532016-09-30 14:10:07 +02002353 *
2354 * Return: %true if a fixup in buffer @buffer at offset @offset is
2355 * allowed.
2356 *
2357 * For safety reasons, we only allow fixups inside a buffer to happen
2358 * at increasing offsets; additionally, we only allow fixup on the last
2359 * buffer object that was verified, or one of its parents.
2360 *
2361 * Example of what is allowed:
2362 *
2363 * A
2364 * B (parent = A, offset = 0)
2365 * C (parent = A, offset = 16)
2366 * D (parent = C, offset = 0)
2367 * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
2368 *
2369 * Examples of what is not allowed:
2370 *
2371 * Decreasing offsets within the same parent:
2372 * A
2373 * C (parent = A, offset = 16)
2374 * B (parent = A, offset = 0) // decreasing offset within A
2375 *
2376 * Referring to a parent that wasn't the last object or any of its parents:
2377 * A
2378 * B (parent = A, offset = 0)
2379 * C (parent = A, offset = 0)
2380 * C (parent = A, offset = 16)
2381 * D (parent = B, offset = 0) // B is not A or any of A's parents
2382 */
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002383static bool binder_validate_fixup(struct binder_proc *proc,
2384 struct binder_buffer *b,
2385 binder_size_t objects_start_offset,
2386 binder_size_t buffer_obj_offset,
Martijn Coenen5a6da532016-09-30 14:10:07 +02002387 binder_size_t fixup_offset,
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002388 binder_size_t last_obj_offset,
Martijn Coenen5a6da532016-09-30 14:10:07 +02002389 binder_size_t last_min_offset)
2390{
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002391 if (!last_obj_offset) {
Martijn Coenen5a6da532016-09-30 14:10:07 +02002392 /* Nothing to fix up in */
2393 return false;
2394 }
2395
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002396 while (last_obj_offset != buffer_obj_offset) {
2397 unsigned long buffer_offset;
2398 struct binder_object last_object;
2399 struct binder_buffer_object *last_bbo;
2400 size_t object_size = binder_get_object(proc, b, last_obj_offset,
2401 &last_object);
2402 if (object_size != sizeof(*last_bbo))
2403 return false;
2404
2405 last_bbo = &last_object.bbo;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002406 /*
2407 * Safe to retrieve the parent of last_obj, since it
2408 * was already previously verified by the driver.
2409 */
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002410 if ((last_bbo->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0)
Martijn Coenen5a6da532016-09-30 14:10:07 +02002411 return false;
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002412 last_min_offset = last_bbo->parent_offset + sizeof(uintptr_t);
2413 buffer_offset = objects_start_offset +
2414 sizeof(binder_size_t) * last_bbo->parent,
2415 binder_alloc_copy_from_buffer(&proc->alloc, &last_obj_offset,
2416 b, buffer_offset,
2417 sizeof(last_obj_offset));
Martijn Coenen5a6da532016-09-30 14:10:07 +02002418 }
2419 return (fixup_offset >= last_min_offset);
2420}
2421
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002422static void binder_transaction_buffer_release(struct binder_proc *proc,
2423 struct binder_buffer *buffer,
Todd Kjos8539b1e2019-02-08 10:35:20 -08002424 binder_size_t failed_at,
2425 bool is_failure)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002426{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002427 int debug_id = buffer->debug_id;
Todd Kjos8539b1e2019-02-08 10:35:20 -08002428 binder_size_t off_start_offset, buffer_offset, off_end_offset;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002429
2430 binder_debug(BINDER_DEBUG_TRANSACTION,
Todd Kjos8539b1e2019-02-08 10:35:20 -08002431 "%d buffer release %d, size %zd-%zd, failed at %llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002432 proc->pid, buffer->debug_id,
Todd Kjos8539b1e2019-02-08 10:35:20 -08002433 buffer->data_size, buffer->offsets_size,
2434 (unsigned long long)failed_at);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002435
2436 if (buffer->target_node)
2437 binder_dec_node(buffer->target_node, 1, 0);
2438
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002439 off_start_offset = ALIGN(buffer->data_size, sizeof(void *));
Todd Kjos8539b1e2019-02-08 10:35:20 -08002440 off_end_offset = is_failure ? failed_at :
2441 off_start_offset + buffer->offsets_size;
2442 for (buffer_offset = off_start_offset; buffer_offset < off_end_offset;
2443 buffer_offset += sizeof(binder_size_t)) {
Martijn Coenen00c80372016-07-13 12:06:49 +02002444 struct binder_object_header *hdr;
Todd Kjos90a570c2019-02-08 10:35:15 -08002445 size_t object_size;
Todd Kjosd73356a2019-02-08 10:35:16 -08002446 struct binder_object object;
Todd Kjos90a570c2019-02-08 10:35:15 -08002447 binder_size_t object_offset;
Seunghun Lee10f62862014-05-01 01:30:23 +09002448
Todd Kjos90a570c2019-02-08 10:35:15 -08002449 binder_alloc_copy_from_buffer(&proc->alloc, &object_offset,
2450 buffer, buffer_offset,
2451 sizeof(object_offset));
Todd Kjosd73356a2019-02-08 10:35:16 -08002452 object_size = binder_get_object(proc, buffer,
2453 object_offset, &object);
Martijn Coenen00c80372016-07-13 12:06:49 +02002454 if (object_size == 0) {
2455 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
Todd Kjos90a570c2019-02-08 10:35:15 -08002456 debug_id, (u64)object_offset, buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002457 continue;
2458 }
Todd Kjosd73356a2019-02-08 10:35:16 -08002459 hdr = &object.hdr;
Martijn Coenen00c80372016-07-13 12:06:49 +02002460 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002461 case BINDER_TYPE_BINDER:
2462 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenen00c80372016-07-13 12:06:49 +02002463 struct flat_binder_object *fp;
2464 struct binder_node *node;
Seunghun Lee10f62862014-05-01 01:30:23 +09002465
Martijn Coenen00c80372016-07-13 12:06:49 +02002466 fp = to_flat_binder_object(hdr);
2467 node = binder_get_node(proc, fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002468 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002469 pr_err("transaction release %d bad node %016llx\n",
2470 debug_id, (u64)fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002471 break;
2472 }
2473 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002474 " node %d u%016llx\n",
2475 node->debug_id, (u64)node->ptr);
Martijn Coenen00c80372016-07-13 12:06:49 +02002476 binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER,
2477 0);
Todd Kjosf22abc72017-05-09 11:08:05 -07002478 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002479 } break;
2480 case BINDER_TYPE_HANDLE:
2481 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenen00c80372016-07-13 12:06:49 +02002482 struct flat_binder_object *fp;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002483 struct binder_ref_data rdata;
2484 int ret;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02002485
Martijn Coenen00c80372016-07-13 12:06:49 +02002486 fp = to_flat_binder_object(hdr);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002487 ret = binder_dec_ref_for_handle(proc, fp->handle,
2488 hdr->type == BINDER_TYPE_HANDLE, &rdata);
2489
2490 if (ret) {
2491 pr_err("transaction release %d bad handle %d, ret = %d\n",
2492 debug_id, fp->handle, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002493 break;
2494 }
2495 binder_debug(BINDER_DEBUG_TRANSACTION,
Todd Kjosb0117bb2017-05-08 09:16:27 -07002496 " ref %d desc %d\n",
2497 rdata.debug_id, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002498 } break;
2499
Martijn Coenen00c80372016-07-13 12:06:49 +02002500 case BINDER_TYPE_FD: {
2501 struct binder_fd_object *fp = to_binder_fd_object(hdr);
2502
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002503 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen00c80372016-07-13 12:06:49 +02002504 " fd %d\n", fp->fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002505 if (failed_at)
Martijn Coenen00c80372016-07-13 12:06:49 +02002506 task_close_fd(proc, fp->fd);
2507 } break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002508 case BINDER_TYPE_PTR:
2509 /*
2510 * Nothing to do here, this will get cleaned up when the
2511 * transaction buffer gets freed
2512 */
2513 break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002514 case BINDER_TYPE_FDA: {
2515 struct binder_fd_array_object *fda;
2516 struct binder_buffer_object *parent;
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002517 struct binder_object ptr_object;
Todd Kjos8539b1e2019-02-08 10:35:20 -08002518 binder_size_t fda_offset;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002519 size_t fd_index;
2520 binder_size_t fd_buf_size;
Todd Kjos8539b1e2019-02-08 10:35:20 -08002521 binder_size_t num_valid;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002522
Todd Kjos8539b1e2019-02-08 10:35:20 -08002523 num_valid = (buffer_offset - off_start_offset) /
2524 sizeof(binder_size_t);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002525 fda = to_binder_fd_array_object(hdr);
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002526 parent = binder_validate_ptr(proc, buffer, &ptr_object,
2527 fda->parent,
2528 off_start_offset,
2529 NULL,
Todd Kjos8539b1e2019-02-08 10:35:20 -08002530 num_valid);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002531 if (!parent) {
2532 pr_err("transaction release %d bad parent offset",
2533 debug_id);
2534 continue;
2535 }
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002536 fd_buf_size = sizeof(u32) * fda->num_fds;
2537 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2538 pr_err("transaction release %d invalid number of fds (%lld)\n",
2539 debug_id, (u64)fda->num_fds);
2540 continue;
2541 }
2542 if (fd_buf_size > parent->length ||
2543 fda->parent_offset > parent->length - fd_buf_size) {
2544 /* No space for all file descriptors here. */
2545 pr_err("transaction release %d not enough space for %lld fds in buffer\n",
2546 debug_id, (u64)fda->num_fds);
2547 continue;
2548 }
Todd Kjos8539b1e2019-02-08 10:35:20 -08002549 /*
2550 * the source data for binder_buffer_object is visible
2551 * to user-space and the @buffer element is the user
2552 * pointer to the buffer_object containing the fd_array.
2553 * Convert the address to an offset relative to
2554 * the base of the transaction buffer.
2555 */
2556 fda_offset =
2557 (parent->buffer - (uintptr_t)buffer->user_data) +
2558 fda->parent_offset;
Todd Kjos90a570c2019-02-08 10:35:15 -08002559 for (fd_index = 0; fd_index < fda->num_fds;
2560 fd_index++) {
2561 u32 fd;
Todd Kjos8539b1e2019-02-08 10:35:20 -08002562 binder_size_t offset = fda_offset +
2563 fd_index * sizeof(fd);
Todd Kjos90a570c2019-02-08 10:35:15 -08002564
2565 binder_alloc_copy_from_buffer(&proc->alloc,
2566 &fd,
2567 buffer,
2568 offset,
2569 sizeof(fd));
2570 task_close_fd(proc, fd);
2571 }
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002572 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002573 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01002574 pr_err("transaction release %d bad object type %x\n",
Martijn Coenen00c80372016-07-13 12:06:49 +02002575 debug_id, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002576 break;
2577 }
2578 }
2579}
2580
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002581static int binder_translate_binder(struct flat_binder_object *fp,
2582 struct binder_transaction *t,
2583 struct binder_thread *thread)
2584{
2585 struct binder_node *node;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002586 struct binder_proc *proc = thread->proc;
2587 struct binder_proc *target_proc = t->to_proc;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002588 struct binder_ref_data rdata;
Todd Kjosf22abc72017-05-09 11:08:05 -07002589 int ret = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002590
2591 node = binder_get_node(proc, fp->binder);
2592 if (!node) {
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002593 node = binder_new_node(proc, fp);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002594 if (!node)
2595 return -ENOMEM;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002596 }
2597 if (fp->cookie != node->cookie) {
2598 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
2599 proc->pid, thread->pid, (u64)fp->binder,
2600 node->debug_id, (u64)fp->cookie,
2601 (u64)node->cookie);
Todd Kjosf22abc72017-05-09 11:08:05 -07002602 ret = -EINVAL;
2603 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002604 }
Todd Kjosf22abc72017-05-09 11:08:05 -07002605 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2606 ret = -EPERM;
2607 goto done;
2608 }
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002609
Todd Kjosb0117bb2017-05-08 09:16:27 -07002610 ret = binder_inc_ref_for_node(target_proc, node,
2611 fp->hdr.type == BINDER_TYPE_BINDER,
2612 &thread->todo, &rdata);
2613 if (ret)
Todd Kjosf22abc72017-05-09 11:08:05 -07002614 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002615
2616 if (fp->hdr.type == BINDER_TYPE_BINDER)
2617 fp->hdr.type = BINDER_TYPE_HANDLE;
2618 else
2619 fp->hdr.type = BINDER_TYPE_WEAK_HANDLE;
2620 fp->binder = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002621 fp->handle = rdata.desc;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002622 fp->cookie = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002623
Todd Kjosb0117bb2017-05-08 09:16:27 -07002624 trace_binder_transaction_node_to_ref(t, node, &rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002625 binder_debug(BINDER_DEBUG_TRANSACTION,
2626 " node %d u%016llx -> ref %d desc %d\n",
2627 node->debug_id, (u64)node->ptr,
Todd Kjosb0117bb2017-05-08 09:16:27 -07002628 rdata.debug_id, rdata.desc);
Todd Kjosf22abc72017-05-09 11:08:05 -07002629done:
2630 binder_put_node(node);
2631 return ret;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002632}
2633
2634static int binder_translate_handle(struct flat_binder_object *fp,
2635 struct binder_transaction *t,
2636 struct binder_thread *thread)
2637{
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002638 struct binder_proc *proc = thread->proc;
2639 struct binder_proc *target_proc = t->to_proc;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002640 struct binder_node *node;
2641 struct binder_ref_data src_rdata;
Todd Kjosf22abc72017-05-09 11:08:05 -07002642 int ret = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002643
Todd Kjosb0117bb2017-05-08 09:16:27 -07002644 node = binder_get_node_from_ref(proc, fp->handle,
2645 fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata);
2646 if (!node) {
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002647 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
2648 proc->pid, thread->pid, fp->handle);
2649 return -EINVAL;
2650 }
Todd Kjosf22abc72017-05-09 11:08:05 -07002651 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2652 ret = -EPERM;
2653 goto done;
2654 }
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002655
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002656 binder_node_lock(node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002657 if (node->proc == target_proc) {
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002658 if (fp->hdr.type == BINDER_TYPE_HANDLE)
2659 fp->hdr.type = BINDER_TYPE_BINDER;
2660 else
2661 fp->hdr.type = BINDER_TYPE_WEAK_BINDER;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002662 fp->binder = node->ptr;
2663 fp->cookie = node->cookie;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002664 if (node->proc)
2665 binder_inner_proc_lock(node->proc);
2666 binder_inc_node_nilocked(node,
2667 fp->hdr.type == BINDER_TYPE_BINDER,
2668 0, NULL);
2669 if (node->proc)
2670 binder_inner_proc_unlock(node->proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002671 trace_binder_transaction_ref_to_node(t, node, &src_rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002672 binder_debug(BINDER_DEBUG_TRANSACTION,
2673 " ref %d desc %d -> node %d u%016llx\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07002674 src_rdata.debug_id, src_rdata.desc, node->debug_id,
2675 (u64)node->ptr);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002676 binder_node_unlock(node);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002677 } else {
Todd Kjosb0117bb2017-05-08 09:16:27 -07002678 struct binder_ref_data dest_rdata;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002679
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002680 binder_node_unlock(node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002681 ret = binder_inc_ref_for_node(target_proc, node,
2682 fp->hdr.type == BINDER_TYPE_HANDLE,
2683 NULL, &dest_rdata);
2684 if (ret)
Todd Kjosf22abc72017-05-09 11:08:05 -07002685 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002686
2687 fp->binder = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002688 fp->handle = dest_rdata.desc;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002689 fp->cookie = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002690 trace_binder_transaction_ref_to_ref(t, node, &src_rdata,
2691 &dest_rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002692 binder_debug(BINDER_DEBUG_TRANSACTION,
2693 " ref %d desc %d -> ref %d desc %d (node %d)\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07002694 src_rdata.debug_id, src_rdata.desc,
2695 dest_rdata.debug_id, dest_rdata.desc,
2696 node->debug_id);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002697 }
Todd Kjosf22abc72017-05-09 11:08:05 -07002698done:
2699 binder_put_node(node);
2700 return ret;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002701}
2702
2703static int binder_translate_fd(int fd,
2704 struct binder_transaction *t,
2705 struct binder_thread *thread,
2706 struct binder_transaction *in_reply_to)
2707{
2708 struct binder_proc *proc = thread->proc;
2709 struct binder_proc *target_proc = t->to_proc;
2710 int target_fd;
2711 struct file *file;
2712 int ret;
2713 bool target_allows_fd;
2714
2715 if (in_reply_to)
2716 target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS);
2717 else
2718 target_allows_fd = t->buffer->target_node->accept_fds;
2719 if (!target_allows_fd) {
2720 binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
2721 proc->pid, thread->pid,
2722 in_reply_to ? "reply" : "transaction",
2723 fd);
2724 ret = -EPERM;
2725 goto err_fd_not_accepted;
2726 }
2727
2728 file = fget(fd);
2729 if (!file) {
2730 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
2731 proc->pid, thread->pid, fd);
2732 ret = -EBADF;
2733 goto err_fget;
2734 }
2735 ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file);
2736 if (ret < 0) {
2737 ret = -EPERM;
2738 goto err_security;
2739 }
2740
2741 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
2742 if (target_fd < 0) {
2743 ret = -ENOMEM;
2744 goto err_get_unused_fd;
2745 }
2746 task_fd_install(target_proc, target_fd, file);
2747 trace_binder_transaction_fd(t, fd, target_fd);
2748 binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n",
2749 fd, target_fd);
2750
2751 return target_fd;
2752
2753err_get_unused_fd:
2754err_security:
2755 fput(file);
2756err_fget:
2757err_fd_not_accepted:
2758 return ret;
2759}
2760
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002761static int binder_translate_fd_array(struct binder_fd_array_object *fda,
2762 struct binder_buffer_object *parent,
2763 struct binder_transaction *t,
2764 struct binder_thread *thread,
2765 struct binder_transaction *in_reply_to)
2766{
2767 binder_size_t fdi, fd_buf_size, num_installed_fds;
Todd Kjos8539b1e2019-02-08 10:35:20 -08002768 binder_size_t fda_offset;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002769 int target_fd;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002770 struct binder_proc *proc = thread->proc;
2771 struct binder_proc *target_proc = t->to_proc;
2772
2773 fd_buf_size = sizeof(u32) * fda->num_fds;
2774 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2775 binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
2776 proc->pid, thread->pid, (u64)fda->num_fds);
2777 return -EINVAL;
2778 }
2779 if (fd_buf_size > parent->length ||
2780 fda->parent_offset > parent->length - fd_buf_size) {
2781 /* No space for all file descriptors here. */
2782 binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
2783 proc->pid, thread->pid, (u64)fda->num_fds);
2784 return -EINVAL;
2785 }
2786 /*
Todd Kjos8539b1e2019-02-08 10:35:20 -08002787 * the source data for binder_buffer_object is visible
2788 * to user-space and the @buffer element is the user
2789 * pointer to the buffer_object containing the fd_array.
2790 * Convert the address to an offset relative to
2791 * the base of the transaction buffer.
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002792 */
Todd Kjos8539b1e2019-02-08 10:35:20 -08002793 fda_offset = (parent->buffer - (uintptr_t)t->buffer->user_data) +
2794 fda->parent_offset;
2795 if (!IS_ALIGNED((unsigned long)fda_offset, sizeof(u32))) {
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002796 binder_user_error("%d:%d parent offset not aligned correctly.\n",
2797 proc->pid, thread->pid);
2798 return -EINVAL;
2799 }
2800 for (fdi = 0; fdi < fda->num_fds; fdi++) {
Todd Kjos90a570c2019-02-08 10:35:15 -08002801 u32 fd;
Todd Kjos0b90a822019-03-27 16:12:31 -07002802
Todd Kjos8539b1e2019-02-08 10:35:20 -08002803 binder_size_t offset = fda_offset + fdi * sizeof(fd);
Todd Kjos90a570c2019-02-08 10:35:15 -08002804
2805 binder_alloc_copy_from_buffer(&target_proc->alloc,
2806 &fd, t->buffer,
2807 offset, sizeof(fd));
2808 target_fd = binder_translate_fd(fd, t, thread, in_reply_to);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002809 if (target_fd < 0)
2810 goto err_translate_fd_failed;
Todd Kjos90a570c2019-02-08 10:35:15 -08002811 binder_alloc_copy_to_buffer(&target_proc->alloc,
2812 t->buffer, offset,
2813 &target_fd, sizeof(fd));
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002814 }
2815 return 0;
2816
2817err_translate_fd_failed:
2818 /*
2819 * Failed to allocate fd or security error, free fds
2820 * installed so far.
2821 */
2822 num_installed_fds = fdi;
Todd Kjos90a570c2019-02-08 10:35:15 -08002823 for (fdi = 0; fdi < num_installed_fds; fdi++) {
2824 u32 fd;
Todd Kjos8539b1e2019-02-08 10:35:20 -08002825 binder_size_t offset = fda_offset + fdi * sizeof(fd);
Todd Kjos90a570c2019-02-08 10:35:15 -08002826 binder_alloc_copy_from_buffer(&target_proc->alloc,
2827 &fd, t->buffer,
2828 offset, sizeof(fd));
2829 task_close_fd(target_proc, fd);
2830 }
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002831 return target_fd;
2832}
2833
Martijn Coenen5a6da532016-09-30 14:10:07 +02002834static int binder_fixup_parent(struct binder_transaction *t,
2835 struct binder_thread *thread,
2836 struct binder_buffer_object *bp,
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002837 binder_size_t off_start_offset,
Martijn Coenen5a6da532016-09-30 14:10:07 +02002838 binder_size_t num_valid,
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002839 binder_size_t last_fixup_obj_off,
Martijn Coenen5a6da532016-09-30 14:10:07 +02002840 binder_size_t last_fixup_min_off)
2841{
2842 struct binder_buffer_object *parent;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002843 struct binder_buffer *b = t->buffer;
2844 struct binder_proc *proc = thread->proc;
2845 struct binder_proc *target_proc = t->to_proc;
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002846 struct binder_object object;
2847 binder_size_t buffer_offset;
2848 binder_size_t parent_offset;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002849
2850 if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT))
2851 return 0;
2852
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002853 parent = binder_validate_ptr(target_proc, b, &object, bp->parent,
2854 off_start_offset, &parent_offset,
2855 num_valid);
Martijn Coenen5a6da532016-09-30 14:10:07 +02002856 if (!parent) {
2857 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
2858 proc->pid, thread->pid);
2859 return -EINVAL;
2860 }
2861
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002862 if (!binder_validate_fixup(target_proc, b, off_start_offset,
2863 parent_offset, bp->parent_offset,
2864 last_fixup_obj_off,
Martijn Coenen5a6da532016-09-30 14:10:07 +02002865 last_fixup_min_off)) {
2866 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
2867 proc->pid, thread->pid);
2868 return -EINVAL;
2869 }
2870
2871 if (parent->length < sizeof(binder_uintptr_t) ||
2872 bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) {
2873 /* No space for a pointer here! */
2874 binder_user_error("%d:%d got transaction with invalid parent offset\n",
2875 proc->pid, thread->pid);
2876 return -EINVAL;
2877 }
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002878 buffer_offset = bp->parent_offset +
Todd Kjos8539b1e2019-02-08 10:35:20 -08002879 (uintptr_t)parent->buffer - (uintptr_t)b->user_data;
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002880 binder_alloc_copy_to_buffer(&target_proc->alloc, b, buffer_offset,
2881 &bp->buffer, sizeof(bp->buffer));
Martijn Coenen5a6da532016-09-30 14:10:07 +02002882
2883 return 0;
2884}
2885
Martijn Coenen053be422017-06-06 15:17:46 -07002886/**
2887 * binder_proc_transaction() - sends a transaction to a process and wakes it up
2888 * @t: transaction to send
2889 * @proc: process to send the transaction to
2890 * @thread: thread in @proc to send the transaction to (may be NULL)
2891 *
2892 * This function queues a transaction to the specified process. It will try
2893 * to find a thread in the target process to handle the transaction and
2894 * wake it up. If no thread is found, the work is queued to the proc
2895 * waitqueue.
2896 *
2897 * If the @thread parameter is not NULL, the transaction is always queued
2898 * to the waitlist of that specific thread.
2899 *
2900 * Return: true if the transactions was successfully queued
2901 * false if the target process or thread is dead
2902 */
2903static bool binder_proc_transaction(struct binder_transaction *t,
2904 struct binder_proc *proc,
2905 struct binder_thread *thread)
2906{
Martijn Coenen053be422017-06-06 15:17:46 -07002907 struct binder_node *node = t->buffer->target_node;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002908 struct binder_priority node_prio;
Martijn Coenen053be422017-06-06 15:17:46 -07002909 bool oneway = !!(t->flags & TF_ONE_WAY);
Martijn Coenen1af61802017-10-19 15:04:46 +02002910 bool pending_async = false;
Martijn Coenen053be422017-06-06 15:17:46 -07002911
2912 BUG_ON(!node);
2913 binder_node_lock(node);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002914 node_prio.prio = node->min_priority;
2915 node_prio.sched_policy = node->sched_policy;
2916
Martijn Coenen053be422017-06-06 15:17:46 -07002917 if (oneway) {
2918 BUG_ON(thread);
2919 if (node->has_async_transaction) {
Martijn Coenen1af61802017-10-19 15:04:46 +02002920 pending_async = true;
Martijn Coenen053be422017-06-06 15:17:46 -07002921 } else {
Gustavo A. R. Silvae62dd6f2018-01-23 12:04:27 -06002922 node->has_async_transaction = true;
Martijn Coenen053be422017-06-06 15:17:46 -07002923 }
2924 }
2925
2926 binder_inner_proc_lock(proc);
2927
2928 if (proc->is_dead || (thread && thread->is_dead)) {
2929 binder_inner_proc_unlock(proc);
2930 binder_node_unlock(node);
2931 return false;
2932 }
2933
Martijn Coenen1af61802017-10-19 15:04:46 +02002934 if (!thread && !pending_async)
Martijn Coenen053be422017-06-06 15:17:46 -07002935 thread = binder_select_thread_ilocked(proc);
2936
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002937 if (thread) {
Martijn Coenenc46810c2017-06-23 10:13:43 -07002938 binder_transaction_priority(thread->task, t, node_prio,
2939 node->inherit_rt);
Martijn Coenen1af61802017-10-19 15:04:46 +02002940 binder_enqueue_thread_work_ilocked(thread, &t->work);
2941 } else if (!pending_async) {
2942 binder_enqueue_work_ilocked(&t->work, &proc->todo);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002943 } else {
Martijn Coenen1af61802017-10-19 15:04:46 +02002944 binder_enqueue_work_ilocked(&t->work, &node->async_todo);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002945 }
Martijn Coenen053be422017-06-06 15:17:46 -07002946
Martijn Coenen1af61802017-10-19 15:04:46 +02002947 if (!pending_async)
Martijn Coenen053be422017-06-06 15:17:46 -07002948 binder_wakeup_thread_ilocked(proc, thread, !oneway /* sync */);
2949
2950 binder_inner_proc_unlock(proc);
2951 binder_node_unlock(node);
2952
2953 return true;
2954}
2955
Todd Kjos291d9682017-09-25 08:55:09 -07002956/**
2957 * binder_get_node_refs_for_txn() - Get required refs on node for txn
2958 * @node: struct binder_node for which to get refs
2959 * @proc: returns @node->proc if valid
2960 * @error: if no @proc then returns BR_DEAD_REPLY
2961 *
2962 * User-space normally keeps the node alive when creating a transaction
2963 * since it has a reference to the target. The local strong ref keeps it
2964 * alive if the sending process dies before the target process processes
2965 * the transaction. If the source process is malicious or has a reference
2966 * counting bug, relying on the local strong ref can fail.
2967 *
2968 * Since user-space can cause the local strong ref to go away, we also take
2969 * a tmpref on the node to ensure it survives while we are constructing
2970 * the transaction. We also need a tmpref on the proc while we are
2971 * constructing the transaction, so we take that here as well.
2972 *
2973 * Return: The target_node with refs taken or NULL if no @node->proc is NULL.
2974 * Also sets @proc if valid. If the @node->proc is NULL indicating that the
2975 * target proc has died, @error is set to BR_DEAD_REPLY
2976 */
2977static struct binder_node *binder_get_node_refs_for_txn(
2978 struct binder_node *node,
2979 struct binder_proc **procp,
2980 uint32_t *error)
2981{
2982 struct binder_node *target_node = NULL;
2983
2984 binder_node_inner_lock(node);
2985 if (node->proc) {
2986 target_node = node;
2987 binder_inc_node_nilocked(node, 1, 0, NULL);
2988 binder_inc_node_tmpref_ilocked(node);
Todd Kjosf4e6de32019-06-10 09:14:25 -07002989 atomic_inc(&node->proc->tmp_ref);
Todd Kjos291d9682017-09-25 08:55:09 -07002990 *procp = node->proc;
2991 } else
2992 *error = BR_DEAD_REPLY;
2993 binder_node_inner_unlock(node);
2994
2995 return target_node;
2996}
2997
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002998static void binder_transaction(struct binder_proc *proc,
2999 struct binder_thread *thread,
Martijn Coenen59878d72016-09-30 14:05:40 +02003000 struct binder_transaction_data *tr, int reply,
3001 binder_size_t extra_buffers_size)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003002{
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003003 int ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003004 struct binder_transaction *t;
3005 struct binder_work *tcomplete;
Todd Kjos8539b1e2019-02-08 10:35:20 -08003006 binder_size_t buffer_offset = 0;
3007 binder_size_t off_start_offset, off_end_offset;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08003008 binder_size_t off_min;
Todd Kjos8539b1e2019-02-08 10:35:20 -08003009 binder_size_t sg_buf_offset, sg_buf_end_offset;
Todd Kjos2f993e22017-05-12 14:42:55 -07003010 struct binder_proc *target_proc = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003011 struct binder_thread *target_thread = NULL;
3012 struct binder_node *target_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003013 struct binder_transaction *in_reply_to = NULL;
3014 struct binder_transaction_log_entry *e;
Todd Kjose598d172017-03-22 17:19:52 -07003015 uint32_t return_error = 0;
3016 uint32_t return_error_param = 0;
3017 uint32_t return_error_line = 0;
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003018 binder_size_t last_fixup_obj_off = 0;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003019 binder_size_t last_fixup_min_off = 0;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003020 struct binder_context *context = proc->context;
Todd Kjos1cfe6272017-05-24 13:33:28 -07003021 int t_debug_id = atomic_inc_return(&binder_last_id);
Todd Kjos63e0afa2019-01-14 09:10:21 -08003022 char *secctx = NULL;
3023 u32 secctx_sz = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003024
3025 e = binder_transaction_log_add(&binder_transaction_log);
Todd Kjos1cfe6272017-05-24 13:33:28 -07003026 e->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003027 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
3028 e->from_proc = proc->pid;
3029 e->from_thread = thread->pid;
3030 e->target_handle = tr->target.handle;
3031 e->data_size = tr->data_size;
3032 e->offsets_size = tr->offsets_size;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02003033 e->context_name = proc->context->name;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003034
3035 if (reply) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07003036 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003037 in_reply_to = thread->transaction_stack;
3038 if (in_reply_to == NULL) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07003039 binder_inner_proc_unlock(proc);
Anmol Sarma56b468f2012-10-30 22:35:43 +05303040 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003041 proc->pid, thread->pid);
3042 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003043 return_error_param = -EPROTO;
3044 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003045 goto err_empty_call_stack;
3046 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003047 if (in_reply_to->to_thread != thread) {
Todd Kjos2f993e22017-05-12 14:42:55 -07003048 spin_lock(&in_reply_to->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05303049 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 +09003050 proc->pid, thread->pid, in_reply_to->debug_id,
3051 in_reply_to->to_proc ?
3052 in_reply_to->to_proc->pid : 0,
3053 in_reply_to->to_thread ?
3054 in_reply_to->to_thread->pid : 0);
Todd Kjos2f993e22017-05-12 14:42:55 -07003055 spin_unlock(&in_reply_to->lock);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003056 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003057 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003058 return_error_param = -EPROTO;
3059 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003060 in_reply_to = NULL;
3061 goto err_bad_call_stack;
3062 }
3063 thread->transaction_stack = in_reply_to->to_parent;
Martijn Coenen995a36e2017-06-02 13:36:52 -07003064 binder_inner_proc_unlock(proc);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003065 target_thread = binder_get_txn_from_and_acq_inner(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003066 if (target_thread == NULL) {
3067 return_error = BR_DEAD_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003068 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003069 goto err_dead_binder;
3070 }
3071 if (target_thread->transaction_stack != in_reply_to) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303072 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 +09003073 proc->pid, thread->pid,
3074 target_thread->transaction_stack ?
3075 target_thread->transaction_stack->debug_id : 0,
3076 in_reply_to->debug_id);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003077 binder_inner_proc_unlock(target_thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003078 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003079 return_error_param = -EPROTO;
3080 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003081 in_reply_to = NULL;
3082 target_thread = NULL;
3083 goto err_dead_binder;
3084 }
3085 target_proc = target_thread->proc;
Todd Kjosf4e6de32019-06-10 09:14:25 -07003086 atomic_inc(&target_proc->tmp_ref);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003087 binder_inner_proc_unlock(target_thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003088 } else {
3089 if (tr->target.handle) {
3090 struct binder_ref *ref;
Seunghun Lee10f62862014-05-01 01:30:23 +09003091
Todd Kjosc37162d2017-05-26 11:56:29 -07003092 /*
3093 * There must already be a strong ref
3094 * on this node. If so, do a strong
3095 * increment on the node to ensure it
3096 * stays alive until the transaction is
3097 * done.
3098 */
Todd Kjos5346bf32016-10-20 16:43:34 -07003099 binder_proc_lock(proc);
3100 ref = binder_get_ref_olocked(proc, tr->target.handle,
3101 true);
Todd Kjosc37162d2017-05-26 11:56:29 -07003102 if (ref) {
Todd Kjos291d9682017-09-25 08:55:09 -07003103 target_node = binder_get_node_refs_for_txn(
3104 ref->node, &target_proc,
3105 &return_error);
3106 } else {
3107 binder_user_error("%d:%d got transaction to invalid handle\n",
3108 proc->pid, thread->pid);
3109 return_error = BR_FAILED_REPLY;
Todd Kjosc37162d2017-05-26 11:56:29 -07003110 }
Todd Kjos5346bf32016-10-20 16:43:34 -07003111 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003112 } else {
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003113 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003114 target_node = context->binder_context_mgr_node;
Todd Kjos291d9682017-09-25 08:55:09 -07003115 if (target_node)
3116 target_node = binder_get_node_refs_for_txn(
3117 target_node, &target_proc,
3118 &return_error);
3119 else
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003120 return_error = BR_DEAD_REPLY;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003121 mutex_unlock(&context->context_mgr_node_lock);
Martijn Coenenc4048b22018-03-28 11:14:50 +02003122 if (target_node && target_proc == proc) {
3123 binder_user_error("%d:%d got transaction to context manager from process owning it\n",
3124 proc->pid, thread->pid);
3125 return_error = BR_FAILED_REPLY;
3126 return_error_param = -EINVAL;
3127 return_error_line = __LINE__;
3128 goto err_invalid_target_handle;
3129 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003130 }
Todd Kjos291d9682017-09-25 08:55:09 -07003131 if (!target_node) {
3132 /*
3133 * return_error is set above
3134 */
3135 return_error_param = -EINVAL;
Todd Kjose598d172017-03-22 17:19:52 -07003136 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003137 goto err_dead_binder;
3138 }
Todd Kjos291d9682017-09-25 08:55:09 -07003139 e->to_node = target_node->debug_id;
Stephen Smalley79af7302015-01-21 10:54:10 -05003140 if (security_binder_transaction(proc->tsk,
3141 target_proc->tsk) < 0) {
3142 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003143 return_error_param = -EPERM;
3144 return_error_line = __LINE__;
Stephen Smalley79af7302015-01-21 10:54:10 -05003145 goto err_invalid_target_handle;
3146 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07003147 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003148 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
3149 struct binder_transaction *tmp;
Seunghun Lee10f62862014-05-01 01:30:23 +09003150
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003151 tmp = thread->transaction_stack;
3152 if (tmp->to_thread != thread) {
Todd Kjos2f993e22017-05-12 14:42:55 -07003153 spin_lock(&tmp->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05303154 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 +09003155 proc->pid, thread->pid, tmp->debug_id,
3156 tmp->to_proc ? tmp->to_proc->pid : 0,
3157 tmp->to_thread ?
3158 tmp->to_thread->pid : 0);
Todd Kjos2f993e22017-05-12 14:42:55 -07003159 spin_unlock(&tmp->lock);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003160 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003161 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003162 return_error_param = -EPROTO;
3163 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003164 goto err_bad_call_stack;
3165 }
3166 while (tmp) {
Todd Kjos2f993e22017-05-12 14:42:55 -07003167 struct binder_thread *from;
3168
3169 spin_lock(&tmp->lock);
3170 from = tmp->from;
3171 if (from && from->proc == target_proc) {
3172 atomic_inc(&from->tmp_ref);
3173 target_thread = from;
3174 spin_unlock(&tmp->lock);
3175 break;
3176 }
3177 spin_unlock(&tmp->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003178 tmp = tmp->from_parent;
3179 }
3180 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07003181 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003182 }
Martijn Coenen053be422017-06-06 15:17:46 -07003183 if (target_thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003184 e->to_thread = target_thread->pid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003185 e->to_proc = target_proc->pid;
3186
3187 /* TODO: reuse incoming transaction for reply */
3188 t = kzalloc(sizeof(*t), GFP_KERNEL);
3189 if (t == NULL) {
3190 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003191 return_error_param = -ENOMEM;
3192 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003193 goto err_alloc_t_failed;
3194 }
3195 binder_stats_created(BINDER_STAT_TRANSACTION);
Todd Kjos2f993e22017-05-12 14:42:55 -07003196 spin_lock_init(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003197
3198 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
3199 if (tcomplete == NULL) {
3200 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003201 return_error_param = -ENOMEM;
3202 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003203 goto err_alloc_tcomplete_failed;
3204 }
3205 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
3206
Todd Kjos1cfe6272017-05-24 13:33:28 -07003207 t->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003208
3209 if (reply)
3210 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen59878d72016-09-30 14:05:40 +02003211 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003212 proc->pid, thread->pid, t->debug_id,
3213 target_proc->pid, target_thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003214 (u64)tr->data.ptr.buffer,
3215 (u64)tr->data.ptr.offsets,
Martijn Coenen59878d72016-09-30 14:05:40 +02003216 (u64)tr->data_size, (u64)tr->offsets_size,
3217 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003218 else
3219 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen59878d72016-09-30 14:05:40 +02003220 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003221 proc->pid, thread->pid, t->debug_id,
3222 target_proc->pid, target_node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003223 (u64)tr->data.ptr.buffer,
3224 (u64)tr->data.ptr.offsets,
Martijn Coenen59878d72016-09-30 14:05:40 +02003225 (u64)tr->data_size, (u64)tr->offsets_size,
3226 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003227
3228 if (!reply && !(tr->flags & TF_ONE_WAY))
3229 t->from = thread;
3230 else
3231 t->from = NULL;
Tair Rzayev57bab7c2014-05-31 22:43:34 +03003232 t->sender_euid = task_euid(proc->tsk);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003233 t->to_proc = target_proc;
3234 t->to_thread = target_thread;
3235 t->code = tr->code;
3236 t->flags = tr->flags;
Martijn Coenen57b2ac62017-06-06 17:04:42 -07003237 if (!(t->flags & TF_ONE_WAY) &&
3238 binder_supported_policy(current->policy)) {
3239 /* Inherit supported policies for synchronous transactions */
3240 t->priority.sched_policy = current->policy;
3241 t->priority.prio = current->normal_prio;
3242 } else {
3243 /* Otherwise, fall back to the default priority */
3244 t->priority = target_proc->default_priority;
3245 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003246
Todd Kjos63e0afa2019-01-14 09:10:21 -08003247 if (target_node && target_node->txn_security_ctx) {
3248 u32 secid;
Todd Kjosb8f2f4b2019-04-24 12:31:18 -07003249 size_t added_size;
Todd Kjos63e0afa2019-01-14 09:10:21 -08003250
3251 security_task_getsecid(proc->tsk, &secid);
3252 ret = security_secid_to_secctx(secid, &secctx, &secctx_sz);
3253 if (ret) {
3254 return_error = BR_FAILED_REPLY;
3255 return_error_param = ret;
3256 return_error_line = __LINE__;
3257 goto err_get_secctx_failed;
3258 }
Todd Kjosb8f2f4b2019-04-24 12:31:18 -07003259 added_size = ALIGN(secctx_sz, sizeof(u64));
3260 extra_buffers_size += added_size;
3261 if (extra_buffers_size < added_size) {
3262 /* integer overflow of extra_buffers_size */
3263 return_error = BR_FAILED_REPLY;
3264 return_error_param = EINVAL;
3265 return_error_line = __LINE__;
3266 goto err_bad_extra_size;
3267 }
Todd Kjos63e0afa2019-01-14 09:10:21 -08003268 }
3269
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003270 trace_binder_transaction(reply, t, target_node);
3271
Todd Kjosd325d372016-10-10 10:40:53 -07003272 t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size,
Martijn Coenen59878d72016-09-30 14:05:40 +02003273 tr->offsets_size, extra_buffers_size,
3274 !reply && (t->flags & TF_ONE_WAY));
Todd Kjose598d172017-03-22 17:19:52 -07003275 if (IS_ERR(t->buffer)) {
3276 /*
3277 * -ESRCH indicates VMA cleared. The target is dying.
3278 */
3279 return_error_param = PTR_ERR(t->buffer);
3280 return_error = return_error_param == -ESRCH ?
3281 BR_DEAD_REPLY : BR_FAILED_REPLY;
3282 return_error_line = __LINE__;
3283 t->buffer = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003284 goto err_binder_alloc_buf_failed;
3285 }
Todd Kjos63e0afa2019-01-14 09:10:21 -08003286 if (secctx) {
3287 size_t buf_offset = ALIGN(tr->data_size, sizeof(void *)) +
3288 ALIGN(tr->offsets_size, sizeof(void *)) +
3289 ALIGN(extra_buffers_size, sizeof(void *)) -
3290 ALIGN(secctx_sz, sizeof(u64));
Todd Kjos63e0afa2019-01-14 09:10:21 -08003291
Todd Kjos8539b1e2019-02-08 10:35:20 -08003292 t->security_ctx = (uintptr_t)t->buffer->user_data + buf_offset;
Todd Kjos90a570c2019-02-08 10:35:15 -08003293 binder_alloc_copy_to_buffer(&target_proc->alloc,
3294 t->buffer, buf_offset,
3295 secctx, secctx_sz);
Todd Kjos63e0afa2019-01-14 09:10:21 -08003296 security_release_secctx(secctx, secctx_sz);
3297 secctx = NULL;
3298 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003299 t->buffer->debug_id = t->debug_id;
3300 t->buffer->transaction = t;
3301 t->buffer->target_node = target_node;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003302 trace_binder_transaction_alloc_buf(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003303
Todd Kjosd5049492019-02-08 10:35:14 -08003304 if (binder_alloc_copy_user_to_buffer(
3305 &target_proc->alloc,
3306 t->buffer, 0,
3307 (const void __user *)
3308 (uintptr_t)tr->data.ptr.buffer,
3309 tr->data_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303310 binder_user_error("%d:%d got transaction with invalid data ptr\n",
3311 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003312 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003313 return_error_param = -EFAULT;
3314 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003315 goto err_copy_data_failed;
3316 }
Todd Kjosd5049492019-02-08 10:35:14 -08003317 if (binder_alloc_copy_user_to_buffer(
3318 &target_proc->alloc,
3319 t->buffer,
3320 ALIGN(tr->data_size, sizeof(void *)),
3321 (const void __user *)
3322 (uintptr_t)tr->data.ptr.offsets,
3323 tr->offsets_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303324 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3325 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003326 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003327 return_error_param = -EFAULT;
3328 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003329 goto err_copy_data_failed;
3330 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08003331 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
3332 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
3333 proc->pid, thread->pid, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003334 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003335 return_error_param = -EINVAL;
3336 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003337 goto err_bad_offset;
3338 }
Martijn Coenen5a6da532016-09-30 14:10:07 +02003339 if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) {
3340 binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
3341 proc->pid, thread->pid,
Amit Pundir44cbb182017-02-01 12:53:45 +05303342 (u64)extra_buffers_size);
Martijn Coenen5a6da532016-09-30 14:10:07 +02003343 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003344 return_error_param = -EINVAL;
3345 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003346 goto err_bad_offset;
3347 }
Todd Kjos8539b1e2019-02-08 10:35:20 -08003348 off_start_offset = ALIGN(tr->data_size, sizeof(void *));
3349 buffer_offset = off_start_offset;
3350 off_end_offset = off_start_offset + tr->offsets_size;
3351 sg_buf_offset = ALIGN(off_end_offset, sizeof(void *));
Martijn Coenen3244efe2019-07-09 13:09:23 +02003352 sg_buf_end_offset = sg_buf_offset + extra_buffers_size -
3353 ALIGN(secctx_sz, sizeof(u64));
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08003354 off_min = 0;
Todd Kjos8539b1e2019-02-08 10:35:20 -08003355 for (buffer_offset = off_start_offset; buffer_offset < off_end_offset;
3356 buffer_offset += sizeof(binder_size_t)) {
Martijn Coenen00c80372016-07-13 12:06:49 +02003357 struct binder_object_header *hdr;
Todd Kjos90a570c2019-02-08 10:35:15 -08003358 size_t object_size;
Todd Kjosd73356a2019-02-08 10:35:16 -08003359 struct binder_object object;
Todd Kjos90a570c2019-02-08 10:35:15 -08003360 binder_size_t object_offset;
Seunghun Lee10f62862014-05-01 01:30:23 +09003361
Todd Kjos90a570c2019-02-08 10:35:15 -08003362 binder_alloc_copy_from_buffer(&target_proc->alloc,
3363 &object_offset,
3364 t->buffer,
3365 buffer_offset,
3366 sizeof(object_offset));
Todd Kjosd73356a2019-02-08 10:35:16 -08003367 object_size = binder_get_object(target_proc, t->buffer,
3368 object_offset, &object);
Todd Kjos90a570c2019-02-08 10:35:15 -08003369 if (object_size == 0 || object_offset < off_min) {
Martijn Coenen00c80372016-07-13 12:06:49 +02003370 binder_user_error("%d:%d got transaction with invalid offset (%lld, min %lld max %lld) or object.\n",
Todd Kjos90a570c2019-02-08 10:35:15 -08003371 proc->pid, thread->pid,
3372 (u64)object_offset,
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08003373 (u64)off_min,
Martijn Coenen00c80372016-07-13 12:06:49 +02003374 (u64)t->buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003375 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003376 return_error_param = -EINVAL;
3377 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003378 goto err_bad_offset;
3379 }
Martijn Coenen00c80372016-07-13 12:06:49 +02003380
Todd Kjosd73356a2019-02-08 10:35:16 -08003381 hdr = &object.hdr;
Todd Kjos90a570c2019-02-08 10:35:15 -08003382 off_min = object_offset + object_size;
Martijn Coenen00c80372016-07-13 12:06:49 +02003383 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003384 case BINDER_TYPE_BINDER:
3385 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003386 struct flat_binder_object *fp;
Seunghun Lee10f62862014-05-01 01:30:23 +09003387
Martijn Coenen00c80372016-07-13 12:06:49 +02003388 fp = to_flat_binder_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003389 ret = binder_translate_binder(fp, t, thread);
3390 if (ret < 0) {
Christian Engelmayer7d420432014-05-07 21:44:53 +02003391 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003392 return_error_param = ret;
3393 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003394 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003395 }
Todd Kjosd73356a2019-02-08 10:35:16 -08003396 binder_alloc_copy_to_buffer(&target_proc->alloc,
3397 t->buffer, object_offset,
3398 fp, sizeof(*fp));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003399 } break;
3400 case BINDER_TYPE_HANDLE:
3401 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003402 struct flat_binder_object *fp;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02003403
Martijn Coenen00c80372016-07-13 12:06:49 +02003404 fp = to_flat_binder_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003405 ret = binder_translate_handle(fp, t, thread);
3406 if (ret < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003407 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003408 return_error_param = ret;
3409 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003410 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003411 }
Todd Kjosd73356a2019-02-08 10:35:16 -08003412 binder_alloc_copy_to_buffer(&target_proc->alloc,
3413 t->buffer, object_offset,
3414 fp, sizeof(*fp));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003415 } break;
3416
3417 case BINDER_TYPE_FD: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003418 struct binder_fd_object *fp = to_binder_fd_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003419 int target_fd = binder_translate_fd(fp->fd, t, thread,
3420 in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003421
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003422 if (target_fd < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003423 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003424 return_error_param = target_fd;
3425 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003426 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003427 }
Martijn Coenen00c80372016-07-13 12:06:49 +02003428 fp->pad_binder = 0;
3429 fp->fd = target_fd;
Todd Kjosd73356a2019-02-08 10:35:16 -08003430 binder_alloc_copy_to_buffer(&target_proc->alloc,
3431 t->buffer, object_offset,
3432 fp, sizeof(*fp));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003433 } break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003434 case BINDER_TYPE_FDA: {
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003435 struct binder_object ptr_object;
3436 binder_size_t parent_offset;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003437 struct binder_fd_array_object *fda =
3438 to_binder_fd_array_object(hdr);
Todd Kjos2ea90f02019-12-13 12:25:31 -08003439 size_t num_valid = (buffer_offset - off_start_offset) /
Todd Kjos8539b1e2019-02-08 10:35:20 -08003440 sizeof(binder_size_t);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003441 struct binder_buffer_object *parent =
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003442 binder_validate_ptr(target_proc, t->buffer,
3443 &ptr_object, fda->parent,
3444 off_start_offset,
3445 &parent_offset,
Todd Kjos8539b1e2019-02-08 10:35:20 -08003446 num_valid);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003447 if (!parent) {
3448 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
3449 proc->pid, thread->pid);
3450 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003451 return_error_param = -EINVAL;
3452 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003453 goto err_bad_parent;
3454 }
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003455 if (!binder_validate_fixup(target_proc, t->buffer,
3456 off_start_offset,
3457 parent_offset,
3458 fda->parent_offset,
3459 last_fixup_obj_off,
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003460 last_fixup_min_off)) {
3461 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
3462 proc->pid, thread->pid);
3463 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003464 return_error_param = -EINVAL;
3465 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003466 goto err_bad_parent;
3467 }
3468 ret = binder_translate_fd_array(fda, parent, t, thread,
3469 in_reply_to);
3470 if (ret < 0) {
3471 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003472 return_error_param = ret;
3473 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003474 goto err_translate_failed;
3475 }
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003476 last_fixup_obj_off = parent_offset;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003477 last_fixup_min_off =
3478 fda->parent_offset + sizeof(u32) * fda->num_fds;
3479 } break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003480 case BINDER_TYPE_PTR: {
3481 struct binder_buffer_object *bp =
3482 to_binder_buffer_object(hdr);
Todd Kjos8539b1e2019-02-08 10:35:20 -08003483 size_t buf_left = sg_buf_end_offset - sg_buf_offset;
3484 size_t num_valid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003485
Martijn Coenen5a6da532016-09-30 14:10:07 +02003486 if (bp->length > buf_left) {
3487 binder_user_error("%d:%d got transaction with too large buffer\n",
3488 proc->pid, thread->pid);
3489 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003490 return_error_param = -EINVAL;
3491 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003492 goto err_bad_offset;
3493 }
Todd Kjosd5049492019-02-08 10:35:14 -08003494 if (binder_alloc_copy_user_to_buffer(
3495 &target_proc->alloc,
3496 t->buffer,
3497 sg_buf_offset,
3498 (const void __user *)
3499 (uintptr_t)bp->buffer,
3500 bp->length)) {
Martijn Coenen5a6da532016-09-30 14:10:07 +02003501 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3502 proc->pid, thread->pid);
Todd Kjose598d172017-03-22 17:19:52 -07003503 return_error_param = -EFAULT;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003504 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003505 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003506 goto err_copy_data_failed;
3507 }
3508 /* Fixup buffer pointer to target proc address space */
Todd Kjos8539b1e2019-02-08 10:35:20 -08003509 bp->buffer = (uintptr_t)
3510 t->buffer->user_data + sg_buf_offset;
3511 sg_buf_offset += ALIGN(bp->length, sizeof(u64));
Martijn Coenen5a6da532016-09-30 14:10:07 +02003512
Todd Kjos2ea90f02019-12-13 12:25:31 -08003513 num_valid = (buffer_offset - off_start_offset) /
Todd Kjos8539b1e2019-02-08 10:35:20 -08003514 sizeof(binder_size_t);
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003515 ret = binder_fixup_parent(t, thread, bp,
3516 off_start_offset,
Todd Kjos8539b1e2019-02-08 10:35:20 -08003517 num_valid,
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003518 last_fixup_obj_off,
Martijn Coenen5a6da532016-09-30 14:10:07 +02003519 last_fixup_min_off);
3520 if (ret < 0) {
3521 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003522 return_error_param = ret;
3523 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003524 goto err_translate_failed;
3525 }
Todd Kjosd73356a2019-02-08 10:35:16 -08003526 binder_alloc_copy_to_buffer(&target_proc->alloc,
3527 t->buffer, object_offset,
3528 bp, sizeof(*bp));
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003529 last_fixup_obj_off = object_offset;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003530 last_fixup_min_off = 0;
3531 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003532 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01003533 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
Martijn Coenen00c80372016-07-13 12:06:49 +02003534 proc->pid, thread->pid, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003535 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003536 return_error_param = -EINVAL;
3537 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003538 goto err_bad_object_type;
3539 }
3540 }
Todd Kjos8dedb0c2017-05-09 08:31:32 -07003541 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003542 t->work.type = BINDER_WORK_TRANSACTION;
Todd Kjos8dedb0c2017-05-09 08:31:32 -07003543
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003544 if (reply) {
Martijn Coenen1af61802017-10-19 15:04:46 +02003545 binder_enqueue_thread_work(thread, tcomplete);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003546 binder_inner_proc_lock(target_proc);
3547 if (target_thread->is_dead) {
3548 binder_inner_proc_unlock(target_proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07003549 goto err_dead_proc_or_thread;
Martijn Coenen995a36e2017-06-02 13:36:52 -07003550 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003551 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003552 binder_pop_transaction_ilocked(target_thread, in_reply_to);
Martijn Coenen1af61802017-10-19 15:04:46 +02003553 binder_enqueue_thread_work_ilocked(target_thread, &t->work);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003554 binder_inner_proc_unlock(target_proc);
Martijn Coenen053be422017-06-06 15:17:46 -07003555 wake_up_interruptible_sync(&target_thread->wait);
Martijn Coenenecd972d2017-05-26 10:48:56 -07003556 binder_restore_priority(current, in_reply_to->saved_priority);
Todd Kjos21ef40a2017-03-30 18:02:13 -07003557 binder_free_transaction(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003558 } else if (!(t->flags & TF_ONE_WAY)) {
3559 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003560 binder_inner_proc_lock(proc);
Martijn Coenendac2e9c2017-11-13 09:55:21 +01003561 /*
3562 * Defer the TRANSACTION_COMPLETE, so we don't return to
3563 * userspace immediately; this allows the target process to
3564 * immediately start processing this transaction, reducing
3565 * latency. We will then return the TRANSACTION_COMPLETE when
3566 * the target replies (or there is an error).
3567 */
3568 binder_enqueue_deferred_thread_work_ilocked(thread, tcomplete);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003569 t->need_reply = 1;
3570 t->from_parent = thread->transaction_stack;
3571 thread->transaction_stack = t;
Martijn Coenen995a36e2017-06-02 13:36:52 -07003572 binder_inner_proc_unlock(proc);
Martijn Coenen053be422017-06-06 15:17:46 -07003573 if (!binder_proc_transaction(t, target_proc, target_thread)) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07003574 binder_inner_proc_lock(proc);
3575 binder_pop_transaction_ilocked(thread, t);
3576 binder_inner_proc_unlock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07003577 goto err_dead_proc_or_thread;
3578 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003579 } else {
3580 BUG_ON(target_node == NULL);
3581 BUG_ON(t->buffer->async_transaction != 1);
Martijn Coenen1af61802017-10-19 15:04:46 +02003582 binder_enqueue_thread_work(thread, tcomplete);
Martijn Coenen053be422017-06-06 15:17:46 -07003583 if (!binder_proc_transaction(t, target_proc, NULL))
Todd Kjos2f993e22017-05-12 14:42:55 -07003584 goto err_dead_proc_or_thread;
Riley Andrewsb5968812015-09-01 12:42:07 -07003585 }
Todd Kjos2f993e22017-05-12 14:42:55 -07003586 if (target_thread)
3587 binder_thread_dec_tmpref(target_thread);
3588 binder_proc_dec_tmpref(target_proc);
Todd Kjos291d9682017-09-25 08:55:09 -07003589 if (target_node)
3590 binder_dec_node_tmpref(target_node);
Todd Kjos1cfe6272017-05-24 13:33:28 -07003591 /*
3592 * write barrier to synchronize with initialization
3593 * of log entry
3594 */
3595 smp_wmb();
3596 WRITE_ONCE(e->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003597 return;
3598
Todd Kjos2f993e22017-05-12 14:42:55 -07003599err_dead_proc_or_thread:
3600 return_error = BR_DEAD_REPLY;
3601 return_error_line = __LINE__;
Xu YiPing86578a02017-05-22 11:26:23 -07003602 binder_dequeue_work(proc, tcomplete);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003603err_translate_failed:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003604err_bad_object_type:
3605err_bad_offset:
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003606err_bad_parent:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003607err_copy_data_failed:
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003608 trace_binder_transaction_failed_buffer_release(t->buffer);
Todd Kjos8539b1e2019-02-08 10:35:20 -08003609 binder_transaction_buffer_release(target_proc, t->buffer,
3610 buffer_offset, true);
Todd Kjos291d9682017-09-25 08:55:09 -07003611 if (target_node)
3612 binder_dec_node_tmpref(target_node);
Todd Kjosc37162d2017-05-26 11:56:29 -07003613 target_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003614 t->buffer->transaction = NULL;
Todd Kjosd325d372016-10-10 10:40:53 -07003615 binder_alloc_free_buf(&target_proc->alloc, t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003616err_binder_alloc_buf_failed:
Todd Kjosb8f2f4b2019-04-24 12:31:18 -07003617err_bad_extra_size:
Todd Kjos63e0afa2019-01-14 09:10:21 -08003618 if (secctx)
3619 security_release_secctx(secctx, secctx_sz);
3620err_get_secctx_failed:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003621 kfree(tcomplete);
3622 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3623err_alloc_tcomplete_failed:
3624 kfree(t);
3625 binder_stats_deleted(BINDER_STAT_TRANSACTION);
3626err_alloc_t_failed:
3627err_bad_call_stack:
3628err_empty_call_stack:
3629err_dead_binder:
3630err_invalid_target_handle:
Todd Kjos2f993e22017-05-12 14:42:55 -07003631 if (target_thread)
3632 binder_thread_dec_tmpref(target_thread);
3633 if (target_proc)
3634 binder_proc_dec_tmpref(target_proc);
Todd Kjos291d9682017-09-25 08:55:09 -07003635 if (target_node) {
Todd Kjosc37162d2017-05-26 11:56:29 -07003636 binder_dec_node(target_node, 1, 0);
Todd Kjos291d9682017-09-25 08:55:09 -07003637 binder_dec_node_tmpref(target_node);
3638 }
Todd Kjosc37162d2017-05-26 11:56:29 -07003639
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003640 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Todd Kjose598d172017-03-22 17:19:52 -07003641 "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
3642 proc->pid, thread->pid, return_error, return_error_param,
3643 (u64)tr->data_size, (u64)tr->offsets_size,
3644 return_error_line);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003645
3646 {
3647 struct binder_transaction_log_entry *fe;
Seunghun Lee10f62862014-05-01 01:30:23 +09003648
Todd Kjose598d172017-03-22 17:19:52 -07003649 e->return_error = return_error;
3650 e->return_error_param = return_error_param;
3651 e->return_error_line = return_error_line;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003652 fe = binder_transaction_log_add(&binder_transaction_log_failed);
3653 *fe = *e;
Todd Kjos1cfe6272017-05-24 13:33:28 -07003654 /*
3655 * write barrier to synchronize with initialization
3656 * of log entry
3657 */
3658 smp_wmb();
3659 WRITE_ONCE(e->debug_id_done, t_debug_id);
3660 WRITE_ONCE(fe->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003661 }
3662
Todd Kjos858b8da2017-04-21 17:35:12 -07003663 BUG_ON(thread->return_error.cmd != BR_OK);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003664 if (in_reply_to) {
Martijn Coenenecd972d2017-05-26 10:48:56 -07003665 binder_restore_priority(current, in_reply_to->saved_priority);
Todd Kjos858b8da2017-04-21 17:35:12 -07003666 thread->return_error.cmd = BR_TRANSACTION_COMPLETE;
Martijn Coenen1af61802017-10-19 15:04:46 +02003667 binder_enqueue_thread_work(thread, &thread->return_error.work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003668 binder_send_failed_reply(in_reply_to, return_error);
Todd Kjos858b8da2017-04-21 17:35:12 -07003669 } else {
3670 thread->return_error.cmd = return_error;
Martijn Coenen1af61802017-10-19 15:04:46 +02003671 binder_enqueue_thread_work(thread, &thread->return_error.work);
Todd Kjos858b8da2017-04-21 17:35:12 -07003672 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003673}
3674
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02003675static int binder_thread_write(struct binder_proc *proc,
3676 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003677 binder_uintptr_t binder_buffer, size_t size,
3678 binder_size_t *consumed)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003679{
3680 uint32_t cmd;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003681 struct binder_context *context = proc->context;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003682 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003683 void __user *ptr = buffer + *consumed;
3684 void __user *end = buffer + size;
3685
Todd Kjos858b8da2017-04-21 17:35:12 -07003686 while (ptr < end && thread->return_error.cmd == BR_OK) {
Todd Kjosb0117bb2017-05-08 09:16:27 -07003687 int ret;
3688
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003689 if (get_user(cmd, (uint32_t __user *)ptr))
3690 return -EFAULT;
3691 ptr += sizeof(uint32_t);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003692 trace_binder_command(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003693 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07003694 atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]);
3695 atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]);
3696 atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003697 }
3698 switch (cmd) {
3699 case BC_INCREFS:
3700 case BC_ACQUIRE:
3701 case BC_RELEASE:
3702 case BC_DECREFS: {
3703 uint32_t target;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003704 const char *debug_string;
Todd Kjosb0117bb2017-05-08 09:16:27 -07003705 bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE;
3706 bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE;
3707 struct binder_ref_data rdata;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003708
3709 if (get_user(target, (uint32_t __user *)ptr))
3710 return -EFAULT;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003711
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003712 ptr += sizeof(uint32_t);
Todd Kjosb0117bb2017-05-08 09:16:27 -07003713 ret = -1;
3714 if (increment && !target) {
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003715 struct binder_node *ctx_mgr_node;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003716 mutex_lock(&context->context_mgr_node_lock);
3717 ctx_mgr_node = context->binder_context_mgr_node;
Todd Kjosb0117bb2017-05-08 09:16:27 -07003718 if (ctx_mgr_node)
3719 ret = binder_inc_ref_for_node(
3720 proc, ctx_mgr_node,
3721 strong, NULL, &rdata);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003722 mutex_unlock(&context->context_mgr_node_lock);
3723 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07003724 if (ret)
3725 ret = binder_update_ref_for_handle(
3726 proc, target, increment, strong,
3727 &rdata);
3728 if (!ret && rdata.desc != target) {
3729 binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
3730 proc->pid, thread->pid,
3731 target, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003732 }
3733 switch (cmd) {
3734 case BC_INCREFS:
3735 debug_string = "IncRefs";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003736 break;
3737 case BC_ACQUIRE:
3738 debug_string = "Acquire";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003739 break;
3740 case BC_RELEASE:
3741 debug_string = "Release";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003742 break;
3743 case BC_DECREFS:
3744 default:
3745 debug_string = "DecRefs";
Todd Kjosb0117bb2017-05-08 09:16:27 -07003746 break;
3747 }
3748 if (ret) {
3749 binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
3750 proc->pid, thread->pid, debug_string,
3751 strong, target, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003752 break;
3753 }
3754 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjosb0117bb2017-05-08 09:16:27 -07003755 "%d:%d %s ref %d desc %d s %d w %d\n",
3756 proc->pid, thread->pid, debug_string,
3757 rdata.debug_id, rdata.desc, rdata.strong,
3758 rdata.weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003759 break;
3760 }
3761 case BC_INCREFS_DONE:
3762 case BC_ACQUIRE_DONE: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003763 binder_uintptr_t node_ptr;
3764 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003765 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003766 bool free_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003767
Arve Hjønnevågda498892014-02-21 14:40:26 -08003768 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003769 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003770 ptr += sizeof(binder_uintptr_t);
3771 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003772 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003773 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003774 node = binder_get_node(proc, node_ptr);
3775 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003776 binder_user_error("%d:%d %s u%016llx no match\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003777 proc->pid, thread->pid,
3778 cmd == BC_INCREFS_DONE ?
3779 "BC_INCREFS_DONE" :
3780 "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003781 (u64)node_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003782 break;
3783 }
3784 if (cookie != node->cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003785 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003786 proc->pid, thread->pid,
3787 cmd == BC_INCREFS_DONE ?
3788 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003789 (u64)node_ptr, node->debug_id,
3790 (u64)cookie, (u64)node->cookie);
Todd Kjosf22abc72017-05-09 11:08:05 -07003791 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003792 break;
3793 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003794 binder_node_inner_lock(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003795 if (cmd == BC_ACQUIRE_DONE) {
3796 if (node->pending_strong_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303797 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003798 proc->pid, thread->pid,
3799 node->debug_id);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003800 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003801 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003802 break;
3803 }
3804 node->pending_strong_ref = 0;
3805 } else {
3806 if (node->pending_weak_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303807 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003808 proc->pid, thread->pid,
3809 node->debug_id);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003810 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003811 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003812 break;
3813 }
3814 node->pending_weak_ref = 0;
3815 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003816 free_node = binder_dec_node_nilocked(node,
3817 cmd == BC_ACQUIRE_DONE, 0);
3818 WARN_ON(free_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003819 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjosf22abc72017-05-09 11:08:05 -07003820 "%d:%d %s node %d ls %d lw %d tr %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003821 proc->pid, thread->pid,
3822 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Todd Kjosf22abc72017-05-09 11:08:05 -07003823 node->debug_id, node->local_strong_refs,
3824 node->local_weak_refs, node->tmp_refs);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003825 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003826 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003827 break;
3828 }
3829 case BC_ATTEMPT_ACQUIRE:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303830 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003831 return -EINVAL;
3832 case BC_ACQUIRE_RESULT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303833 pr_err("BC_ACQUIRE_RESULT not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003834 return -EINVAL;
3835
3836 case BC_FREE_BUFFER: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003837 binder_uintptr_t data_ptr;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003838 struct binder_buffer *buffer;
3839
Arve Hjønnevågda498892014-02-21 14:40:26 -08003840 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003841 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003842 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003843
Todd Kjos076072a2017-04-21 14:32:11 -07003844 buffer = binder_alloc_prepare_to_free(&proc->alloc,
3845 data_ptr);
Todd Kjosd29b73e2018-11-06 15:55:32 -08003846 if (IS_ERR_OR_NULL(buffer)) {
3847 if (PTR_ERR(buffer) == -EPERM) {
3848 binder_user_error(
3849 "%d:%d BC_FREE_BUFFER u%016llx matched unreturned or currently freeing buffer\n",
3850 proc->pid, thread->pid,
3851 (u64)data_ptr);
3852 } else {
3853 binder_user_error(
3854 "%d:%d BC_FREE_BUFFER u%016llx no match\n",
3855 proc->pid, thread->pid,
3856 (u64)data_ptr);
3857 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003858 break;
3859 }
3860 binder_debug(BINDER_DEBUG_FREE_BUFFER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003861 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
3862 proc->pid, thread->pid, (u64)data_ptr,
3863 buffer->debug_id,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003864 buffer->transaction ? "active" : "finished");
3865
Todd Kjosf4e6de32019-06-10 09:14:25 -07003866 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003867 if (buffer->transaction) {
3868 buffer->transaction->buffer = NULL;
3869 buffer->transaction = NULL;
3870 }
Todd Kjosf4e6de32019-06-10 09:14:25 -07003871 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003872 if (buffer->async_transaction && buffer->target_node) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003873 struct binder_node *buf_node;
3874 struct binder_work *w;
3875
3876 buf_node = buffer->target_node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003877 binder_node_inner_lock(buf_node);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003878 BUG_ON(!buf_node->has_async_transaction);
3879 BUG_ON(buf_node->proc != proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003880 w = binder_dequeue_work_head_ilocked(
3881 &buf_node->async_todo);
Martijn Coenen4501c042017-08-10 13:56:16 +02003882 if (!w) {
Gustavo A. R. Silvae62dd6f2018-01-23 12:04:27 -06003883 buf_node->has_async_transaction = false;
Martijn Coenen4501c042017-08-10 13:56:16 +02003884 } else {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003885 binder_enqueue_work_ilocked(
Martijn Coenen4501c042017-08-10 13:56:16 +02003886 w, &proc->todo);
3887 binder_wakeup_proc_ilocked(proc);
3888 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003889 binder_node_inner_unlock(buf_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003890 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003891 trace_binder_transaction_buffer_release(buffer);
Todd Kjos8539b1e2019-02-08 10:35:20 -08003892 binder_transaction_buffer_release(proc, buffer, 0, false);
Todd Kjosd325d372016-10-10 10:40:53 -07003893 binder_alloc_free_buf(&proc->alloc, buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003894 break;
3895 }
3896
Martijn Coenen5a6da532016-09-30 14:10:07 +02003897 case BC_TRANSACTION_SG:
3898 case BC_REPLY_SG: {
3899 struct binder_transaction_data_sg tr;
3900
3901 if (copy_from_user(&tr, ptr, sizeof(tr)))
3902 return -EFAULT;
3903 ptr += sizeof(tr);
3904 binder_transaction(proc, thread, &tr.transaction_data,
3905 cmd == BC_REPLY_SG, tr.buffers_size);
3906 break;
3907 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003908 case BC_TRANSACTION:
3909 case BC_REPLY: {
3910 struct binder_transaction_data tr;
3911
3912 if (copy_from_user(&tr, ptr, sizeof(tr)))
3913 return -EFAULT;
3914 ptr += sizeof(tr);
Martijn Coenen59878d72016-09-30 14:05:40 +02003915 binder_transaction(proc, thread, &tr,
3916 cmd == BC_REPLY, 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003917 break;
3918 }
3919
3920 case BC_REGISTER_LOOPER:
3921 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303922 "%d:%d BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003923 proc->pid, thread->pid);
Todd Kjosd600e902017-05-25 17:35:02 -07003924 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003925 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
3926 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303927 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003928 proc->pid, thread->pid);
3929 } else if (proc->requested_threads == 0) {
3930 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303931 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003932 proc->pid, thread->pid);
3933 } else {
3934 proc->requested_threads--;
3935 proc->requested_threads_started++;
3936 }
3937 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
Todd Kjosd600e902017-05-25 17:35:02 -07003938 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003939 break;
3940 case BC_ENTER_LOOPER:
3941 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303942 "%d:%d BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003943 proc->pid, thread->pid);
3944 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
3945 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303946 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003947 proc->pid, thread->pid);
3948 }
3949 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
3950 break;
3951 case BC_EXIT_LOOPER:
3952 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303953 "%d:%d BC_EXIT_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003954 proc->pid, thread->pid);
3955 thread->looper |= BINDER_LOOPER_STATE_EXITED;
3956 break;
3957
3958 case BC_REQUEST_DEATH_NOTIFICATION:
3959 case BC_CLEAR_DEATH_NOTIFICATION: {
3960 uint32_t target;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003961 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003962 struct binder_ref *ref;
Todd Kjos5346bf32016-10-20 16:43:34 -07003963 struct binder_ref_death *death = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003964
3965 if (get_user(target, (uint32_t __user *)ptr))
3966 return -EFAULT;
3967 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08003968 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003969 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003970 ptr += sizeof(binder_uintptr_t);
Todd Kjos5346bf32016-10-20 16:43:34 -07003971 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3972 /*
3973 * Allocate memory for death notification
3974 * before taking lock
3975 */
3976 death = kzalloc(sizeof(*death), GFP_KERNEL);
3977 if (death == NULL) {
3978 WARN_ON(thread->return_error.cmd !=
3979 BR_OK);
3980 thread->return_error.cmd = BR_ERROR;
Martijn Coenen1af61802017-10-19 15:04:46 +02003981 binder_enqueue_thread_work(
3982 thread,
3983 &thread->return_error.work);
Todd Kjos5346bf32016-10-20 16:43:34 -07003984 binder_debug(
3985 BINDER_DEBUG_FAILED_TRANSACTION,
3986 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
3987 proc->pid, thread->pid);
3988 break;
3989 }
3990 }
3991 binder_proc_lock(proc);
3992 ref = binder_get_ref_olocked(proc, target, false);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003993 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303994 binder_user_error("%d:%d %s invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003995 proc->pid, thread->pid,
3996 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3997 "BC_REQUEST_DEATH_NOTIFICATION" :
3998 "BC_CLEAR_DEATH_NOTIFICATION",
3999 target);
Todd Kjos5346bf32016-10-20 16:43:34 -07004000 binder_proc_unlock(proc);
4001 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004002 break;
4003 }
4004
4005 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004006 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004007 proc->pid, thread->pid,
4008 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
4009 "BC_REQUEST_DEATH_NOTIFICATION" :
4010 "BC_CLEAR_DEATH_NOTIFICATION",
Todd Kjosb0117bb2017-05-08 09:16:27 -07004011 (u64)cookie, ref->data.debug_id,
4012 ref->data.desc, ref->data.strong,
4013 ref->data.weak, ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004014
Martijn Coenenf9eac642017-05-22 11:26:23 -07004015 binder_node_lock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004016 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
4017 if (ref->death) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05304018 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004019 proc->pid, thread->pid);
Martijn Coenenf9eac642017-05-22 11:26:23 -07004020 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07004021 binder_proc_unlock(proc);
4022 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004023 break;
4024 }
4025 binder_stats_created(BINDER_STAT_DEATH);
4026 INIT_LIST_HEAD(&death->work.entry);
4027 death->cookie = cookie;
4028 ref->death = death;
4029 if (ref->node->proc == NULL) {
4030 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
Martijn Coenen3bdbe4c2017-08-10 13:50:52 +02004031
4032 binder_inner_proc_lock(proc);
4033 binder_enqueue_work_ilocked(
4034 &ref->death->work, &proc->todo);
4035 binder_wakeup_proc_ilocked(proc);
4036 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004037 }
4038 } else {
4039 if (ref->death == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05304040 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004041 proc->pid, thread->pid);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004042 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07004043 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004044 break;
4045 }
4046 death = ref->death;
4047 if (death->cookie != cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08004048 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004049 proc->pid, thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004050 (u64)death->cookie,
4051 (u64)cookie);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004052 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07004053 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004054 break;
4055 }
4056 ref->death = NULL;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004057 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004058 if (list_empty(&death->work.entry)) {
4059 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004060 if (thread->looper &
4061 (BINDER_LOOPER_STATE_REGISTERED |
4062 BINDER_LOOPER_STATE_ENTERED))
Martijn Coenen1af61802017-10-19 15:04:46 +02004063 binder_enqueue_thread_work_ilocked(
4064 thread,
4065 &death->work);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004066 else {
4067 binder_enqueue_work_ilocked(
4068 &death->work,
4069 &proc->todo);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004070 binder_wakeup_proc_ilocked(
Martijn Coenen053be422017-06-06 15:17:46 -07004071 proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004072 }
4073 } else {
4074 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
4075 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
4076 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004077 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004078 }
Martijn Coenenf9eac642017-05-22 11:26:23 -07004079 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07004080 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004081 } break;
4082 case BC_DEAD_BINDER_DONE: {
4083 struct binder_work *w;
Arve Hjønnevågda498892014-02-21 14:40:26 -08004084 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004085 struct binder_ref_death *death = NULL;
Seunghun Lee10f62862014-05-01 01:30:23 +09004086
Arve Hjønnevågda498892014-02-21 14:40:26 -08004087 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004088 return -EFAULT;
4089
Lisa Du7a64cd82016-02-17 09:32:52 +08004090 ptr += sizeof(cookie);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004091 binder_inner_proc_lock(proc);
4092 list_for_each_entry(w, &proc->delivered_death,
4093 entry) {
4094 struct binder_ref_death *tmp_death =
4095 container_of(w,
4096 struct binder_ref_death,
4097 work);
Seunghun Lee10f62862014-05-01 01:30:23 +09004098
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004099 if (tmp_death->cookie == cookie) {
4100 death = tmp_death;
4101 break;
4102 }
4103 }
4104 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Todd Kjosf540ce02018-02-07 13:57:37 -08004105 "%d:%d BC_DEAD_BINDER_DONE %016llx found %pK\n",
Arve Hjønnevågda498892014-02-21 14:40:26 -08004106 proc->pid, thread->pid, (u64)cookie,
4107 death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004108 if (death == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08004109 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
4110 proc->pid, thread->pid, (u64)cookie);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004111 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004112 break;
4113 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004114 binder_dequeue_work_ilocked(&death->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004115 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
4116 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004117 if (thread->looper &
4118 (BINDER_LOOPER_STATE_REGISTERED |
4119 BINDER_LOOPER_STATE_ENTERED))
Martijn Coenen1af61802017-10-19 15:04:46 +02004120 binder_enqueue_thread_work_ilocked(
4121 thread, &death->work);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004122 else {
4123 binder_enqueue_work_ilocked(
4124 &death->work,
4125 &proc->todo);
Martijn Coenen053be422017-06-06 15:17:46 -07004126 binder_wakeup_proc_ilocked(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004127 }
4128 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004129 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004130 } break;
4131
4132 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304133 pr_err("%d:%d unknown command %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004134 proc->pid, thread->pid, cmd);
4135 return -EINVAL;
4136 }
4137 *consumed = ptr - buffer;
4138 }
4139 return 0;
4140}
4141
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02004142static void binder_stat_br(struct binder_proc *proc,
4143 struct binder_thread *thread, uint32_t cmd)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004144{
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004145 trace_binder_return(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004146 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07004147 atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
4148 atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
4149 atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004150 }
4151}
4152
Todd Kjos60792612017-05-24 10:51:01 -07004153static int binder_put_node_cmd(struct binder_proc *proc,
4154 struct binder_thread *thread,
4155 void __user **ptrp,
4156 binder_uintptr_t node_ptr,
4157 binder_uintptr_t node_cookie,
4158 int node_debug_id,
4159 uint32_t cmd, const char *cmd_name)
4160{
4161 void __user *ptr = *ptrp;
4162
4163 if (put_user(cmd, (uint32_t __user *)ptr))
4164 return -EFAULT;
4165 ptr += sizeof(uint32_t);
4166
4167 if (put_user(node_ptr, (binder_uintptr_t __user *)ptr))
4168 return -EFAULT;
4169 ptr += sizeof(binder_uintptr_t);
4170
4171 if (put_user(node_cookie, (binder_uintptr_t __user *)ptr))
4172 return -EFAULT;
4173 ptr += sizeof(binder_uintptr_t);
4174
4175 binder_stat_br(proc, thread, cmd);
4176 binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n",
4177 proc->pid, thread->pid, cmd_name, node_debug_id,
4178 (u64)node_ptr, (u64)node_cookie);
4179
4180 *ptrp = ptr;
4181 return 0;
4182}
4183
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004184static int binder_wait_for_work(struct binder_thread *thread,
4185 bool do_proc_work)
4186{
4187 DEFINE_WAIT(wait);
4188 struct binder_proc *proc = thread->proc;
4189 int ret = 0;
4190
4191 freezer_do_not_count();
4192 binder_inner_proc_lock(proc);
4193 for (;;) {
4194 prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE);
4195 if (binder_has_work_ilocked(thread, do_proc_work))
4196 break;
4197 if (do_proc_work)
4198 list_add(&thread->waiting_thread_node,
4199 &proc->waiting_threads);
4200 binder_inner_proc_unlock(proc);
4201 schedule();
4202 binder_inner_proc_lock(proc);
4203 list_del_init(&thread->waiting_thread_node);
4204 if (signal_pending(current)) {
4205 ret = -ERESTARTSYS;
4206 break;
4207 }
4208 }
4209 finish_wait(&thread->wait, &wait);
4210 binder_inner_proc_unlock(proc);
4211 freezer_count();
4212
4213 return ret;
4214}
4215
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004216static int binder_thread_read(struct binder_proc *proc,
4217 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004218 binder_uintptr_t binder_buffer, size_t size,
4219 binder_size_t *consumed, int non_block)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004220{
Arve Hjønnevågda498892014-02-21 14:40:26 -08004221 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004222 void __user *ptr = buffer + *consumed;
4223 void __user *end = buffer + size;
4224
4225 int ret = 0;
4226 int wait_for_proc_work;
4227
4228 if (*consumed == 0) {
4229 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
4230 return -EFAULT;
4231 ptr += sizeof(uint32_t);
4232 }
4233
4234retry:
Martijn Coenen995a36e2017-06-02 13:36:52 -07004235 binder_inner_proc_lock(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004236 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
Martijn Coenen995a36e2017-06-02 13:36:52 -07004237 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004238
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004239 thread->looper |= BINDER_LOOPER_STATE_WAITING;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004240
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004241 trace_binder_wait_for_work(wait_for_proc_work,
4242 !!thread->transaction_stack,
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004243 !binder_worklist_empty(proc, &thread->todo));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004244 if (wait_for_proc_work) {
4245 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4246 BINDER_LOOPER_STATE_ENTERED))) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05304247 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 +09004248 proc->pid, thread->pid, thread->looper);
4249 wait_event_interruptible(binder_user_error_wait,
4250 binder_stop_on_user_error < 2);
4251 }
Martijn Coenenecd972d2017-05-26 10:48:56 -07004252 binder_restore_priority(current, proc->default_priority);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004253 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004254
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004255 if (non_block) {
4256 if (!binder_has_work(thread, wait_for_proc_work))
4257 ret = -EAGAIN;
4258 } else {
4259 ret = binder_wait_for_work(thread, wait_for_proc_work);
4260 }
4261
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004262 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
4263
4264 if (ret)
4265 return ret;
4266
4267 while (1) {
4268 uint32_t cmd;
Todd Kjos63e0afa2019-01-14 09:10:21 -08004269 struct binder_transaction_data_secctx tr;
4270 struct binder_transaction_data *trd = &tr.transaction_data;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004271 struct binder_work *w = NULL;
4272 struct list_head *list = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004273 struct binder_transaction *t = NULL;
Todd Kjos2f993e22017-05-12 14:42:55 -07004274 struct binder_thread *t_from;
Todd Kjos63e0afa2019-01-14 09:10:21 -08004275 size_t trsize = sizeof(*trd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004276
Todd Kjose7f23ed2017-03-21 13:06:01 -07004277 binder_inner_proc_lock(proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004278 if (!binder_worklist_empty_ilocked(&thread->todo))
4279 list = &thread->todo;
4280 else if (!binder_worklist_empty_ilocked(&proc->todo) &&
4281 wait_for_proc_work)
4282 list = &proc->todo;
4283 else {
4284 binder_inner_proc_unlock(proc);
4285
Dmitry Voytik395262a2014-09-08 18:16:34 +04004286 /* no data added */
Todd Kjos6798e6d2017-01-06 14:19:25 -08004287 if (ptr - buffer == 4 && !thread->looper_need_return)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004288 goto retry;
4289 break;
4290 }
4291
Todd Kjose7f23ed2017-03-21 13:06:01 -07004292 if (end - ptr < sizeof(tr) + 4) {
4293 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004294 break;
Todd Kjose7f23ed2017-03-21 13:06:01 -07004295 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004296 w = binder_dequeue_work_head_ilocked(list);
Martijn Coenen1af61802017-10-19 15:04:46 +02004297 if (binder_worklist_empty_ilocked(&thread->todo))
4298 thread->process_todo = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004299
4300 switch (w->type) {
4301 case BINDER_WORK_TRANSACTION: {
Todd Kjose7f23ed2017-03-21 13:06:01 -07004302 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004303 t = container_of(w, struct binder_transaction, work);
4304 } break;
Todd Kjos858b8da2017-04-21 17:35:12 -07004305 case BINDER_WORK_RETURN_ERROR: {
4306 struct binder_error *e = container_of(
4307 w, struct binder_error, work);
4308
4309 WARN_ON(e->cmd == BR_OK);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004310 binder_inner_proc_unlock(proc);
Todd Kjos858b8da2017-04-21 17:35:12 -07004311 if (put_user(e->cmd, (uint32_t __user *)ptr))
4312 return -EFAULT;
宋金时e1b1a8b2018-05-10 02:05:03 +00004313 cmd = e->cmd;
Todd Kjos858b8da2017-04-21 17:35:12 -07004314 e->cmd = BR_OK;
4315 ptr += sizeof(uint32_t);
4316
4317 binder_stat_br(proc, thread, cmd);
Todd Kjos858b8da2017-04-21 17:35:12 -07004318 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004319 case BINDER_WORK_TRANSACTION_COMPLETE: {
Todd Kjose7f23ed2017-03-21 13:06:01 -07004320 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004321 cmd = BR_TRANSACTION_COMPLETE;
4322 if (put_user(cmd, (uint32_t __user *)ptr))
4323 return -EFAULT;
4324 ptr += sizeof(uint32_t);
4325
4326 binder_stat_br(proc, thread, cmd);
4327 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304328 "%d:%d BR_TRANSACTION_COMPLETE\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004329 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004330 kfree(w);
4331 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4332 } break;
4333 case BINDER_WORK_NODE: {
4334 struct binder_node *node = container_of(w, struct binder_node, work);
Todd Kjos60792612017-05-24 10:51:01 -07004335 int strong, weak;
4336 binder_uintptr_t node_ptr = node->ptr;
4337 binder_uintptr_t node_cookie = node->cookie;
4338 int node_debug_id = node->debug_id;
4339 int has_weak_ref;
4340 int has_strong_ref;
4341 void __user *orig_ptr = ptr;
Seunghun Lee10f62862014-05-01 01:30:23 +09004342
Todd Kjos60792612017-05-24 10:51:01 -07004343 BUG_ON(proc != node->proc);
4344 strong = node->internal_strong_refs ||
4345 node->local_strong_refs;
4346 weak = !hlist_empty(&node->refs) ||
Todd Kjosf22abc72017-05-09 11:08:05 -07004347 node->local_weak_refs ||
4348 node->tmp_refs || strong;
Todd Kjos60792612017-05-24 10:51:01 -07004349 has_strong_ref = node->has_strong_ref;
4350 has_weak_ref = node->has_weak_ref;
4351
4352 if (weak && !has_weak_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004353 node->has_weak_ref = 1;
4354 node->pending_weak_ref = 1;
4355 node->local_weak_refs++;
Todd Kjos60792612017-05-24 10:51:01 -07004356 }
4357 if (strong && !has_strong_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004358 node->has_strong_ref = 1;
4359 node->pending_strong_ref = 1;
4360 node->local_strong_refs++;
Todd Kjos60792612017-05-24 10:51:01 -07004361 }
4362 if (!strong && has_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004363 node->has_strong_ref = 0;
Todd Kjos60792612017-05-24 10:51:01 -07004364 if (!weak && has_weak_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004365 node->has_weak_ref = 0;
Todd Kjos60792612017-05-24 10:51:01 -07004366 if (!weak && !strong) {
4367 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4368 "%d:%d node %d u%016llx c%016llx deleted\n",
4369 proc->pid, thread->pid,
4370 node_debug_id,
4371 (u64)node_ptr,
4372 (u64)node_cookie);
4373 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004374 binder_inner_proc_unlock(proc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004375 binder_node_lock(node);
4376 /*
4377 * Acquire the node lock before freeing the
4378 * node to serialize with other threads that
4379 * may have been holding the node lock while
4380 * decrementing this node (avoids race where
4381 * this thread frees while the other thread
4382 * is unlocking the node after the final
4383 * decrement)
4384 */
4385 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004386 binder_free_node(node);
4387 } else
4388 binder_inner_proc_unlock(proc);
4389
Todd Kjos60792612017-05-24 10:51:01 -07004390 if (weak && !has_weak_ref)
4391 ret = binder_put_node_cmd(
4392 proc, thread, &ptr, node_ptr,
4393 node_cookie, node_debug_id,
4394 BR_INCREFS, "BR_INCREFS");
4395 if (!ret && strong && !has_strong_ref)
4396 ret = binder_put_node_cmd(
4397 proc, thread, &ptr, node_ptr,
4398 node_cookie, node_debug_id,
4399 BR_ACQUIRE, "BR_ACQUIRE");
4400 if (!ret && !strong && has_strong_ref)
4401 ret = binder_put_node_cmd(
4402 proc, thread, &ptr, node_ptr,
4403 node_cookie, node_debug_id,
4404 BR_RELEASE, "BR_RELEASE");
4405 if (!ret && !weak && has_weak_ref)
4406 ret = binder_put_node_cmd(
4407 proc, thread, &ptr, node_ptr,
4408 node_cookie, node_debug_id,
4409 BR_DECREFS, "BR_DECREFS");
4410 if (orig_ptr == ptr)
4411 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4412 "%d:%d node %d u%016llx c%016llx state unchanged\n",
4413 proc->pid, thread->pid,
4414 node_debug_id,
4415 (u64)node_ptr,
4416 (u64)node_cookie);
4417 if (ret)
4418 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004419 } break;
4420 case BINDER_WORK_DEAD_BINDER:
4421 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4422 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4423 struct binder_ref_death *death;
4424 uint32_t cmd;
Martijn Coenenf9eac642017-05-22 11:26:23 -07004425 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004426
4427 death = container_of(w, struct binder_ref_death, work);
4428 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
4429 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
4430 else
4431 cmd = BR_DEAD_BINDER;
Martijn Coenenf9eac642017-05-22 11:26:23 -07004432 cookie = death->cookie;
4433
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004434 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004435 "%d:%d %s %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004436 proc->pid, thread->pid,
4437 cmd == BR_DEAD_BINDER ?
4438 "BR_DEAD_BINDER" :
4439 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
Martijn Coenenf9eac642017-05-22 11:26:23 -07004440 (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004441 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
Martijn Coenenf9eac642017-05-22 11:26:23 -07004442 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004443 kfree(death);
4444 binder_stats_deleted(BINDER_STAT_DEATH);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004445 } else {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004446 binder_enqueue_work_ilocked(
4447 w, &proc->delivered_death);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004448 binder_inner_proc_unlock(proc);
4449 }
Martijn Coenenf9eac642017-05-22 11:26:23 -07004450 if (put_user(cmd, (uint32_t __user *)ptr))
4451 return -EFAULT;
4452 ptr += sizeof(uint32_t);
4453 if (put_user(cookie,
4454 (binder_uintptr_t __user *)ptr))
4455 return -EFAULT;
4456 ptr += sizeof(binder_uintptr_t);
4457 binder_stat_br(proc, thread, cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004458 if (cmd == BR_DEAD_BINDER)
4459 goto done; /* DEAD_BINDER notifications can cause transactions */
4460 } break;
4461 }
4462
4463 if (!t)
4464 continue;
4465
4466 BUG_ON(t->buffer == NULL);
4467 if (t->buffer->target_node) {
4468 struct binder_node *target_node = t->buffer->target_node;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004469 struct binder_priority node_prio;
Seunghun Lee10f62862014-05-01 01:30:23 +09004470
Todd Kjos63e0afa2019-01-14 09:10:21 -08004471 trd->target.ptr = target_node->ptr;
4472 trd->cookie = target_node->cookie;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004473 node_prio.sched_policy = target_node->sched_policy;
4474 node_prio.prio = target_node->min_priority;
Martijn Coenenc46810c2017-06-23 10:13:43 -07004475 binder_transaction_priority(current, t, node_prio,
4476 target_node->inherit_rt);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004477 cmd = BR_TRANSACTION;
4478 } else {
Todd Kjos63e0afa2019-01-14 09:10:21 -08004479 trd->target.ptr = 0;
4480 trd->cookie = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004481 cmd = BR_REPLY;
4482 }
Todd Kjos63e0afa2019-01-14 09:10:21 -08004483 trd->code = t->code;
4484 trd->flags = t->flags;
4485 trd->sender_euid = from_kuid(current_user_ns(), t->sender_euid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004486
Todd Kjos2f993e22017-05-12 14:42:55 -07004487 t_from = binder_get_txn_from(t);
4488 if (t_from) {
4489 struct task_struct *sender = t_from->proc->tsk;
Seunghun Lee10f62862014-05-01 01:30:23 +09004490
Todd Kjos63e0afa2019-01-14 09:10:21 -08004491 trd->sender_pid =
4492 task_tgid_nr_ns(sender,
4493 task_active_pid_ns(current));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004494 } else {
Todd Kjos63e0afa2019-01-14 09:10:21 -08004495 trd->sender_pid = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004496 }
4497
Todd Kjos63e0afa2019-01-14 09:10:21 -08004498 trd->data_size = t->buffer->data_size;
4499 trd->offsets_size = t->buffer->offsets_size;
Todd Kjos8539b1e2019-02-08 10:35:20 -08004500 trd->data.ptr.buffer = (uintptr_t)t->buffer->user_data;
Todd Kjos63e0afa2019-01-14 09:10:21 -08004501 trd->data.ptr.offsets = trd->data.ptr.buffer +
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004502 ALIGN(t->buffer->data_size,
4503 sizeof(void *));
4504
Todd Kjos63e0afa2019-01-14 09:10:21 -08004505 tr.secctx = t->security_ctx;
4506 if (t->security_ctx) {
4507 cmd = BR_TRANSACTION_SEC_CTX;
4508 trsize = sizeof(tr);
4509 }
Todd Kjos2f993e22017-05-12 14:42:55 -07004510 if (put_user(cmd, (uint32_t __user *)ptr)) {
4511 if (t_from)
4512 binder_thread_dec_tmpref(t_from);
Martijn Coenen3217ccc2017-08-24 15:23:36 +02004513
4514 binder_cleanup_transaction(t, "put_user failed",
4515 BR_FAILED_REPLY);
4516
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004517 return -EFAULT;
Todd Kjos2f993e22017-05-12 14:42:55 -07004518 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004519 ptr += sizeof(uint32_t);
Todd Kjos63e0afa2019-01-14 09:10:21 -08004520 if (copy_to_user(ptr, &tr, trsize)) {
Todd Kjos2f993e22017-05-12 14:42:55 -07004521 if (t_from)
4522 binder_thread_dec_tmpref(t_from);
Martijn Coenen3217ccc2017-08-24 15:23:36 +02004523
4524 binder_cleanup_transaction(t, "copy_to_user failed",
4525 BR_FAILED_REPLY);
4526
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004527 return -EFAULT;
Todd Kjos2f993e22017-05-12 14:42:55 -07004528 }
Todd Kjos63e0afa2019-01-14 09:10:21 -08004529 ptr += trsize;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004530
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004531 trace_binder_transaction_received(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004532 binder_stat_br(proc, thread, cmd);
4533 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004534 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004535 proc->pid, thread->pid,
4536 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
Todd Kjos63e0afa2019-01-14 09:10:21 -08004537 (cmd == BR_TRANSACTION_SEC_CTX) ?
4538 "BR_TRANSACTION_SEC_CTX" : "BR_REPLY",
Todd Kjos2f993e22017-05-12 14:42:55 -07004539 t->debug_id, t_from ? t_from->proc->pid : 0,
4540 t_from ? t_from->pid : 0, cmd,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004541 t->buffer->data_size, t->buffer->offsets_size,
Todd Kjos63e0afa2019-01-14 09:10:21 -08004542 (u64)trd->data.ptr.buffer,
4543 (u64)trd->data.ptr.offsets);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004544
Todd Kjos2f993e22017-05-12 14:42:55 -07004545 if (t_from)
4546 binder_thread_dec_tmpref(t_from);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004547 t->buffer->allow_user_free = 1;
Todd Kjos63e0afa2019-01-14 09:10:21 -08004548 if (cmd != BR_REPLY && !(t->flags & TF_ONE_WAY)) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07004549 binder_inner_proc_lock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004550 t->to_parent = thread->transaction_stack;
4551 t->to_thread = thread;
4552 thread->transaction_stack = t;
Martijn Coenen995a36e2017-06-02 13:36:52 -07004553 binder_inner_proc_unlock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004554 } else {
Todd Kjos21ef40a2017-03-30 18:02:13 -07004555 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004556 }
4557 break;
4558 }
4559
4560done:
4561
4562 *consumed = ptr - buffer;
Todd Kjosd600e902017-05-25 17:35:02 -07004563 binder_inner_proc_lock(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004564 if (proc->requested_threads == 0 &&
4565 list_empty(&thread->proc->waiting_threads) &&
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004566 proc->requested_threads_started < proc->max_threads &&
4567 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4568 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
4569 /*spawn a new thread if we leave this out */) {
4570 proc->requested_threads++;
Todd Kjosd600e902017-05-25 17:35:02 -07004571 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004572 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304573 "%d:%d BR_SPAWN_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004574 proc->pid, thread->pid);
4575 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
4576 return -EFAULT;
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07004577 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
Todd Kjosd600e902017-05-25 17:35:02 -07004578 } else
4579 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004580 return 0;
4581}
4582
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004583static void binder_release_work(struct binder_proc *proc,
4584 struct list_head *list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004585{
4586 struct binder_work *w;
Seunghun Lee10f62862014-05-01 01:30:23 +09004587
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004588 while (1) {
4589 w = binder_dequeue_work_head(proc, list);
4590 if (!w)
4591 return;
4592
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004593 switch (w->type) {
4594 case BINDER_WORK_TRANSACTION: {
4595 struct binder_transaction *t;
4596
4597 t = container_of(w, struct binder_transaction, work);
Martijn Coenen3217ccc2017-08-24 15:23:36 +02004598
4599 binder_cleanup_transaction(t, "process died.",
4600 BR_DEAD_REPLY);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004601 } break;
Todd Kjos858b8da2017-04-21 17:35:12 -07004602 case BINDER_WORK_RETURN_ERROR: {
4603 struct binder_error *e = container_of(
4604 w, struct binder_error, work);
4605
4606 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4607 "undelivered TRANSACTION_ERROR: %u\n",
4608 e->cmd);
4609 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004610 case BINDER_WORK_TRANSACTION_COMPLETE: {
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004611 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304612 "undelivered TRANSACTION_COMPLETE\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004613 kfree(w);
4614 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4615 } break;
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004616 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4617 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4618 struct binder_ref_death *death;
4619
4620 death = container_of(w, struct binder_ref_death, work);
4621 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004622 "undelivered death notification, %016llx\n",
4623 (u64)death->cookie);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004624 kfree(death);
4625 binder_stats_deleted(BINDER_STAT_DEATH);
4626 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004627 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304628 pr_err("unexpected work type, %d, not freed\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004629 w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004630 break;
4631 }
4632 }
4633
4634}
4635
Todd Kjosb4827902017-05-25 15:52:17 -07004636static struct binder_thread *binder_get_thread_ilocked(
4637 struct binder_proc *proc, struct binder_thread *new_thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004638{
4639 struct binder_thread *thread = NULL;
4640 struct rb_node *parent = NULL;
4641 struct rb_node **p = &proc->threads.rb_node;
4642
4643 while (*p) {
4644 parent = *p;
4645 thread = rb_entry(parent, struct binder_thread, rb_node);
4646
4647 if (current->pid < thread->pid)
4648 p = &(*p)->rb_left;
4649 else if (current->pid > thread->pid)
4650 p = &(*p)->rb_right;
4651 else
Todd Kjosb4827902017-05-25 15:52:17 -07004652 return thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004653 }
Todd Kjosb4827902017-05-25 15:52:17 -07004654 if (!new_thread)
4655 return NULL;
4656 thread = new_thread;
4657 binder_stats_created(BINDER_STAT_THREAD);
4658 thread->proc = proc;
4659 thread->pid = current->pid;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004660 get_task_struct(current);
4661 thread->task = current;
Todd Kjosb4827902017-05-25 15:52:17 -07004662 atomic_set(&thread->tmp_ref, 0);
4663 init_waitqueue_head(&thread->wait);
4664 INIT_LIST_HEAD(&thread->todo);
4665 rb_link_node(&thread->rb_node, parent, p);
4666 rb_insert_color(&thread->rb_node, &proc->threads);
4667 thread->looper_need_return = true;
4668 thread->return_error.work.type = BINDER_WORK_RETURN_ERROR;
4669 thread->return_error.cmd = BR_OK;
4670 thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR;
4671 thread->reply_error.cmd = BR_OK;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004672 INIT_LIST_HEAD(&new_thread->waiting_thread_node);
Todd Kjosb4827902017-05-25 15:52:17 -07004673 return thread;
4674}
4675
4676static struct binder_thread *binder_get_thread(struct binder_proc *proc)
4677{
4678 struct binder_thread *thread;
4679 struct binder_thread *new_thread;
4680
4681 binder_inner_proc_lock(proc);
4682 thread = binder_get_thread_ilocked(proc, NULL);
4683 binder_inner_proc_unlock(proc);
4684 if (!thread) {
4685 new_thread = kzalloc(sizeof(*thread), GFP_KERNEL);
4686 if (new_thread == NULL)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004687 return NULL;
Todd Kjosb4827902017-05-25 15:52:17 -07004688 binder_inner_proc_lock(proc);
4689 thread = binder_get_thread_ilocked(proc, new_thread);
4690 binder_inner_proc_unlock(proc);
4691 if (thread != new_thread)
4692 kfree(new_thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004693 }
4694 return thread;
4695}
4696
Todd Kjos2f993e22017-05-12 14:42:55 -07004697static void binder_free_proc(struct binder_proc *proc)
4698{
4699 BUG_ON(!list_empty(&proc->todo));
4700 BUG_ON(!list_empty(&proc->delivered_death));
4701 binder_alloc_deferred_release(&proc->alloc);
4702 put_task_struct(proc->tsk);
4703 binder_stats_deleted(BINDER_STAT_PROC);
4704 kfree(proc);
4705}
4706
4707static void binder_free_thread(struct binder_thread *thread)
4708{
4709 BUG_ON(!list_empty(&thread->todo));
4710 binder_stats_deleted(BINDER_STAT_THREAD);
4711 binder_proc_dec_tmpref(thread->proc);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004712 put_task_struct(thread->task);
Todd Kjos2f993e22017-05-12 14:42:55 -07004713 kfree(thread);
4714}
4715
4716static int binder_thread_release(struct binder_proc *proc,
4717 struct binder_thread *thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004718{
4719 struct binder_transaction *t;
4720 struct binder_transaction *send_reply = NULL;
4721 int active_transactions = 0;
Todd Kjos2f993e22017-05-12 14:42:55 -07004722 struct binder_transaction *last_t = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004723
Todd Kjosb4827902017-05-25 15:52:17 -07004724 binder_inner_proc_lock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07004725 /*
4726 * take a ref on the proc so it survives
4727 * after we remove this thread from proc->threads.
4728 * The corresponding dec is when we actually
4729 * free the thread in binder_free_thread()
4730 */
Todd Kjosf4e6de32019-06-10 09:14:25 -07004731 atomic_inc(&proc->tmp_ref);
Todd Kjos2f993e22017-05-12 14:42:55 -07004732 /*
4733 * take a ref on this thread to ensure it
4734 * survives while we are releasing it
4735 */
4736 atomic_inc(&thread->tmp_ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004737 rb_erase(&thread->rb_node, &proc->threads);
4738 t = thread->transaction_stack;
Todd Kjos2f993e22017-05-12 14:42:55 -07004739 if (t) {
4740 spin_lock(&t->lock);
4741 if (t->to_thread == thread)
4742 send_reply = t;
4743 }
4744 thread->is_dead = true;
4745
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004746 while (t) {
Todd Kjos2f993e22017-05-12 14:42:55 -07004747 last_t = t;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004748 active_transactions++;
4749 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304750 "release %d:%d transaction %d %s, still active\n",
4751 proc->pid, thread->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004752 t->debug_id,
4753 (t->to_thread == thread) ? "in" : "out");
4754
4755 if (t->to_thread == thread) {
4756 t->to_proc = NULL;
4757 t->to_thread = NULL;
4758 if (t->buffer) {
4759 t->buffer->transaction = NULL;
4760 t->buffer = NULL;
4761 }
4762 t = t->to_parent;
4763 } else if (t->from == thread) {
4764 t->from = NULL;
4765 t = t->from_parent;
4766 } else
4767 BUG();
Todd Kjos2f993e22017-05-12 14:42:55 -07004768 spin_unlock(&last_t->lock);
4769 if (t)
4770 spin_lock(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004771 }
Martijn Coenen550c01d2018-01-05 11:27:07 +01004772
4773 /*
4774 * If this thread used poll, make sure we remove the waitqueue
4775 * from any epoll data structures holding it with POLLFREE.
4776 * waitqueue_active() is safe to use here because we're holding
4777 * the inner lock.
4778 */
4779 if ((thread->looper & BINDER_LOOPER_STATE_POLL) &&
4780 waitqueue_active(&thread->wait)) {
4781 wake_up_poll(&thread->wait, POLLHUP | POLLFREE);
4782 }
4783
Todd Kjosb4827902017-05-25 15:52:17 -07004784 binder_inner_proc_unlock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07004785
Martijn Coenen72766d72018-02-16 09:47:15 +01004786 /*
4787 * This is needed to avoid races between wake_up_poll() above and
4788 * and ep_remove_waitqueue() called for other reasons (eg the epoll file
4789 * descriptor being closed); ep_remove_waitqueue() holds an RCU read
4790 * lock, so we can be sure it's done after calling synchronize_rcu().
4791 */
4792 if (thread->looper & BINDER_LOOPER_STATE_POLL)
4793 synchronize_rcu();
4794
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004795 if (send_reply)
4796 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004797 binder_release_work(proc, &thread->todo);
Todd Kjos2f993e22017-05-12 14:42:55 -07004798 binder_thread_dec_tmpref(thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004799 return active_transactions;
4800}
4801
4802static unsigned int binder_poll(struct file *filp,
4803 struct poll_table_struct *wait)
4804{
4805 struct binder_proc *proc = filp->private_data;
4806 struct binder_thread *thread = NULL;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004807 bool wait_for_proc_work;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004808
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004809 thread = binder_get_thread(proc);
Greg Kroah-Hartman6e463bb2018-02-28 17:17:14 +01004810 if (!thread)
Eric Biggers4be5a282018-01-30 23:11:24 -08004811 return POLLERR;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004812
Martijn Coenen995a36e2017-06-02 13:36:52 -07004813 binder_inner_proc_lock(thread->proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004814 thread->looper |= BINDER_LOOPER_STATE_POLL;
4815 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
4816
Martijn Coenen995a36e2017-06-02 13:36:52 -07004817 binder_inner_proc_unlock(thread->proc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004818
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004819 poll_wait(filp, &thread->wait, wait);
4820
Martijn Coenen47810932017-08-10 12:32:00 +02004821 if (binder_has_work(thread, wait_for_proc_work))
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004822 return POLLIN;
4823
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004824 return 0;
4825}
4826
Tair Rzayev78260ac2014-06-03 22:27:21 +03004827static int binder_ioctl_write_read(struct file *filp,
4828 unsigned int cmd, unsigned long arg,
4829 struct binder_thread *thread)
4830{
4831 int ret = 0;
4832 struct binder_proc *proc = filp->private_data;
4833 unsigned int size = _IOC_SIZE(cmd);
4834 void __user *ubuf = (void __user *)arg;
4835 struct binder_write_read bwr;
4836
4837 if (size != sizeof(struct binder_write_read)) {
4838 ret = -EINVAL;
4839 goto out;
4840 }
4841 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
4842 ret = -EFAULT;
4843 goto out;
4844 }
4845 binder_debug(BINDER_DEBUG_READ_WRITE,
4846 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
4847 proc->pid, thread->pid,
4848 (u64)bwr.write_size, (u64)bwr.write_buffer,
4849 (u64)bwr.read_size, (u64)bwr.read_buffer);
4850
4851 if (bwr.write_size > 0) {
4852 ret = binder_thread_write(proc, thread,
4853 bwr.write_buffer,
4854 bwr.write_size,
4855 &bwr.write_consumed);
4856 trace_binder_write_done(ret);
4857 if (ret < 0) {
4858 bwr.read_consumed = 0;
4859 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4860 ret = -EFAULT;
4861 goto out;
4862 }
4863 }
4864 if (bwr.read_size > 0) {
4865 ret = binder_thread_read(proc, thread, bwr.read_buffer,
4866 bwr.read_size,
4867 &bwr.read_consumed,
4868 filp->f_flags & O_NONBLOCK);
4869 trace_binder_read_done(ret);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004870 binder_inner_proc_lock(proc);
4871 if (!binder_worklist_empty_ilocked(&proc->todo))
Martijn Coenen053be422017-06-06 15:17:46 -07004872 binder_wakeup_proc_ilocked(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004873 binder_inner_proc_unlock(proc);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004874 if (ret < 0) {
4875 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4876 ret = -EFAULT;
4877 goto out;
4878 }
4879 }
4880 binder_debug(BINDER_DEBUG_READ_WRITE,
4881 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
4882 proc->pid, thread->pid,
4883 (u64)bwr.write_consumed, (u64)bwr.write_size,
4884 (u64)bwr.read_consumed, (u64)bwr.read_size);
4885 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
4886 ret = -EFAULT;
4887 goto out;
4888 }
4889out:
4890 return ret;
4891}
4892
Todd Kjos63e0afa2019-01-14 09:10:21 -08004893static int binder_ioctl_set_ctx_mgr(struct file *filp,
4894 struct flat_binder_object *fbo)
Tair Rzayev78260ac2014-06-03 22:27:21 +03004895{
4896 int ret = 0;
4897 struct binder_proc *proc = filp->private_data;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004898 struct binder_context *context = proc->context;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004899 struct binder_node *new_node;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004900 kuid_t curr_euid = current_euid();
4901
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004902 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004903 if (context->binder_context_mgr_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004904 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
4905 ret = -EBUSY;
4906 goto out;
4907 }
Stephen Smalley79af7302015-01-21 10:54:10 -05004908 ret = security_binder_set_context_mgr(proc->tsk);
4909 if (ret < 0)
4910 goto out;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004911 if (uid_valid(context->binder_context_mgr_uid)) {
4912 if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004913 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
4914 from_kuid(&init_user_ns, curr_euid),
4915 from_kuid(&init_user_ns,
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004916 context->binder_context_mgr_uid));
Tair Rzayev78260ac2014-06-03 22:27:21 +03004917 ret = -EPERM;
4918 goto out;
4919 }
4920 } else {
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004921 context->binder_context_mgr_uid = curr_euid;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004922 }
Todd Kjos63e0afa2019-01-14 09:10:21 -08004923 new_node = binder_new_node(proc, fbo);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004924 if (!new_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004925 ret = -ENOMEM;
4926 goto out;
4927 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004928 binder_node_lock(new_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004929 new_node->local_weak_refs++;
4930 new_node->local_strong_refs++;
4931 new_node->has_strong_ref = 1;
4932 new_node->has_weak_ref = 1;
4933 context->binder_context_mgr_node = new_node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004934 binder_node_unlock(new_node);
Todd Kjosf22abc72017-05-09 11:08:05 -07004935 binder_put_node(new_node);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004936out:
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004937 mutex_unlock(&context->context_mgr_node_lock);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004938 return ret;
4939}
4940
Martijn Coenen1c57ba42018-08-25 13:50:56 -07004941static int binder_ioctl_get_node_info_for_ref(struct binder_proc *proc,
4942 struct binder_node_info_for_ref *info)
4943{
4944 struct binder_node *node;
4945 struct binder_context *context = proc->context;
4946 __u32 handle = info->handle;
4947
4948 if (info->strong_count || info->weak_count || info->reserved1 ||
4949 info->reserved2 || info->reserved3) {
4950 binder_user_error("%d BINDER_GET_NODE_INFO_FOR_REF: only handle may be non-zero.",
4951 proc->pid);
4952 return -EINVAL;
4953 }
4954
4955 /* This ioctl may only be used by the context manager */
4956 mutex_lock(&context->context_mgr_node_lock);
4957 if (!context->binder_context_mgr_node ||
4958 context->binder_context_mgr_node->proc != proc) {
4959 mutex_unlock(&context->context_mgr_node_lock);
4960 return -EPERM;
4961 }
4962 mutex_unlock(&context->context_mgr_node_lock);
4963
4964 node = binder_get_node_from_ref(proc, handle, true, NULL);
4965 if (!node)
4966 return -EINVAL;
4967
4968 info->strong_count = node->local_strong_refs +
4969 node->internal_strong_refs;
4970 info->weak_count = node->local_weak_refs;
4971
4972 binder_put_node(node);
4973
4974 return 0;
4975}
4976
Colin Cross833babb32017-06-20 13:54:44 -07004977static int binder_ioctl_get_node_debug_info(struct binder_proc *proc,
4978 struct binder_node_debug_info *info) {
4979 struct rb_node *n;
4980 binder_uintptr_t ptr = info->ptr;
4981
4982 memset(info, 0, sizeof(*info));
4983
4984 binder_inner_proc_lock(proc);
4985 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
4986 struct binder_node *node = rb_entry(n, struct binder_node,
4987 rb_node);
4988 if (node->ptr > ptr) {
4989 info->ptr = node->ptr;
4990 info->cookie = node->cookie;
4991 info->has_strong_ref = node->has_strong_ref;
4992 info->has_weak_ref = node->has_weak_ref;
4993 break;
4994 }
4995 }
4996 binder_inner_proc_unlock(proc);
4997
4998 return 0;
4999}
5000
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005001static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
5002{
5003 int ret;
5004 struct binder_proc *proc = filp->private_data;
5005 struct binder_thread *thread;
5006 unsigned int size = _IOC_SIZE(cmd);
5007 void __user *ubuf = (void __user *)arg;
5008
Tair Rzayev78260ac2014-06-03 22:27:21 +03005009 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
5010 proc->pid, current->pid, cmd, arg);*/
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005011
Sherry Yang435416b2017-06-22 14:37:45 -07005012 binder_selftest_alloc(&proc->alloc);
5013
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07005014 trace_binder_ioctl(cmd, arg);
5015
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005016 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
5017 if (ret)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07005018 goto err_unlocked;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005019
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005020 thread = binder_get_thread(proc);
5021 if (thread == NULL) {
5022 ret = -ENOMEM;
5023 goto err;
5024 }
5025
5026 switch (cmd) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03005027 case BINDER_WRITE_READ:
5028 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
5029 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005030 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005031 break;
Todd Kjosd600e902017-05-25 17:35:02 -07005032 case BINDER_SET_MAX_THREADS: {
5033 int max_threads;
5034
5035 if (copy_from_user(&max_threads, ubuf,
5036 sizeof(max_threads))) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005037 ret = -EINVAL;
5038 goto err;
5039 }
Todd Kjosd600e902017-05-25 17:35:02 -07005040 binder_inner_proc_lock(proc);
5041 proc->max_threads = max_threads;
5042 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005043 break;
Todd Kjosd600e902017-05-25 17:35:02 -07005044 }
Todd Kjos63e0afa2019-01-14 09:10:21 -08005045 case BINDER_SET_CONTEXT_MGR_EXT: {
5046 struct flat_binder_object fbo;
5047
5048 if (copy_from_user(&fbo, ubuf, sizeof(fbo))) {
5049 ret = -EINVAL;
5050 goto err;
5051 }
5052 ret = binder_ioctl_set_ctx_mgr(filp, &fbo);
5053 if (ret)
5054 goto err;
5055 break;
5056 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005057 case BINDER_SET_CONTEXT_MGR:
Todd Kjos63e0afa2019-01-14 09:10:21 -08005058 ret = binder_ioctl_set_ctx_mgr(filp, NULL);
Tair Rzayev78260ac2014-06-03 22:27:21 +03005059 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005060 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005061 break;
5062 case BINDER_THREAD_EXIT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05305063 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005064 proc->pid, thread->pid);
Todd Kjos2f993e22017-05-12 14:42:55 -07005065 binder_thread_release(proc, thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005066 thread = NULL;
5067 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02005068 case BINDER_VERSION: {
5069 struct binder_version __user *ver = ubuf;
5070
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005071 if (size != sizeof(struct binder_version)) {
5072 ret = -EINVAL;
5073 goto err;
5074 }
Mathieu Maret36c89c02014-04-15 12:03:05 +02005075 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
5076 &ver->protocol_version)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005077 ret = -EINVAL;
5078 goto err;
5079 }
5080 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02005081 }
Martijn Coenen1c57ba42018-08-25 13:50:56 -07005082 case BINDER_GET_NODE_INFO_FOR_REF: {
5083 struct binder_node_info_for_ref info;
5084
5085 if (copy_from_user(&info, ubuf, sizeof(info))) {
5086 ret = -EFAULT;
5087 goto err;
5088 }
5089
5090 ret = binder_ioctl_get_node_info_for_ref(proc, &info);
5091 if (ret < 0)
5092 goto err;
5093
5094 if (copy_to_user(ubuf, &info, sizeof(info))) {
5095 ret = -EFAULT;
5096 goto err;
5097 }
5098
5099 break;
5100 }
Colin Cross833babb32017-06-20 13:54:44 -07005101 case BINDER_GET_NODE_DEBUG_INFO: {
5102 struct binder_node_debug_info info;
5103
5104 if (copy_from_user(&info, ubuf, sizeof(info))) {
5105 ret = -EFAULT;
5106 goto err;
5107 }
5108
5109 ret = binder_ioctl_get_node_debug_info(proc, &info);
5110 if (ret < 0)
5111 goto err;
5112
5113 if (copy_to_user(ubuf, &info, sizeof(info))) {
5114 ret = -EFAULT;
5115 goto err;
5116 }
5117 break;
5118 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005119 default:
5120 ret = -EINVAL;
5121 goto err;
5122 }
5123 ret = 0;
5124err:
5125 if (thread)
Todd Kjos6798e6d2017-01-06 14:19:25 -08005126 thread->looper_need_return = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005127 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
5128 if (ret && ret != -ERESTARTSYS)
Anmol Sarma56b468f2012-10-30 22:35:43 +05305129 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 -07005130err_unlocked:
5131 trace_binder_ioctl_done(ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005132 return ret;
5133}
5134
5135static void binder_vma_open(struct vm_area_struct *vma)
5136{
5137 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09005138
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005139 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05305140 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005141 proc->pid, vma->vm_start, vma->vm_end,
5142 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
5143 (unsigned long)pgprot_val(vma->vm_page_prot));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005144}
5145
5146static void binder_vma_close(struct vm_area_struct *vma)
5147{
5148 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09005149
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005150 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05305151 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005152 proc->pid, vma->vm_start, vma->vm_end,
5153 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
5154 (unsigned long)pgprot_val(vma->vm_page_prot));
Todd Kjosd325d372016-10-10 10:40:53 -07005155 binder_alloc_vma_close(&proc->alloc);
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005156 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005157}
5158
Vinayak Menonddac7d52014-06-02 18:17:59 +05305159static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
5160{
5161 return VM_FAULT_SIGBUS;
5162}
5163
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07005164static const struct vm_operations_struct binder_vm_ops = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005165 .open = binder_vma_open,
5166 .close = binder_vma_close,
Vinayak Menonddac7d52014-06-02 18:17:59 +05305167 .fault = binder_vm_fault,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005168};
5169
Todd Kjosd325d372016-10-10 10:40:53 -07005170static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
5171{
5172 int ret;
5173 struct binder_proc *proc = filp->private_data;
5174 const char *failure_string;
5175
5176 if (proc->tsk != current->group_leader)
5177 return -EINVAL;
5178
5179 if ((vma->vm_end - vma->vm_start) > SZ_4M)
5180 vma->vm_end = vma->vm_start + SZ_4M;
5181
5182 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5183 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
5184 __func__, proc->pid, vma->vm_start, vma->vm_end,
5185 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
5186 (unsigned long)pgprot_val(vma->vm_page_prot));
5187
5188 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
5189 ret = -EPERM;
5190 failure_string = "bad vm_flags";
5191 goto err_bad_arg;
5192 }
Minchan Kim2cafd5b2018-05-07 23:15:37 +09005193 vma->vm_flags |= VM_DONTCOPY | VM_MIXEDMAP;
5194 vma->vm_flags &= ~VM_MAYWRITE;
5195
Todd Kjosd325d372016-10-10 10:40:53 -07005196 vma->vm_ops = &binder_vm_ops;
5197 vma->vm_private_data = proc;
5198
5199 ret = binder_alloc_mmap_handler(&proc->alloc, vma);
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005200 if (ret)
5201 return ret;
Todd Kjosfbb43392017-11-27 09:32:33 -08005202 mutex_lock(&proc->files_lock);
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005203 proc->files = get_files_struct(current);
Todd Kjosfbb43392017-11-27 09:32:33 -08005204 mutex_unlock(&proc->files_lock);
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005205 return 0;
Todd Kjosd325d372016-10-10 10:40:53 -07005206
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005207err_bad_arg:
Elad Wexler6b646402017-12-29 11:03:37 +02005208 pr_err("%s: %d %lx-%lx %s failed %d\n", __func__,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005209 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
5210 return ret;
5211}
5212
5213static int binder_open(struct inode *nodp, struct file *filp)
5214{
5215 struct binder_proc *proc;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005216 struct binder_device *binder_dev;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005217
Elad Wexler6b646402017-12-29 11:03:37 +02005218 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "%s: %d:%d\n", __func__,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005219 current->group_leader->pid, current->pid);
5220
5221 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
5222 if (proc == NULL)
5223 return -ENOMEM;
Todd Kjosfc7a7e22017-05-29 16:44:24 -07005224 spin_lock_init(&proc->inner_lock);
5225 spin_lock_init(&proc->outer_lock);
Todd Kjosf4e6de32019-06-10 09:14:25 -07005226 atomic_set(&proc->tmp_ref, 0);
Martijn Coenen872c26e2017-03-07 15:51:18 +01005227 get_task_struct(current->group_leader);
5228 proc->tsk = current->group_leader;
Todd Kjosfbb43392017-11-27 09:32:33 -08005229 mutex_init(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005230 INIT_LIST_HEAD(&proc->todo);
Martijn Coenen57b2ac62017-06-06 17:04:42 -07005231 if (binder_supported_policy(current->policy)) {
5232 proc->default_priority.sched_policy = current->policy;
5233 proc->default_priority.prio = current->normal_prio;
5234 } else {
5235 proc->default_priority.sched_policy = SCHED_NORMAL;
5236 proc->default_priority.prio = NICE_TO_PRIO(0);
5237 }
5238
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005239 binder_dev = container_of(filp->private_data, struct binder_device,
5240 miscdev);
5241 proc->context = &binder_dev->context;
Todd Kjosd325d372016-10-10 10:40:53 -07005242 binder_alloc_init(&proc->alloc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07005243
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005244 binder_stats_created(BINDER_STAT_PROC);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005245 proc->pid = current->group_leader->pid;
5246 INIT_LIST_HEAD(&proc->delivered_death);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005247 INIT_LIST_HEAD(&proc->waiting_threads);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005248 filp->private_data = proc;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07005249
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005250 mutex_lock(&binder_procs_lock);
5251 hlist_add_head(&proc->proc_node, &binder_procs);
5252 mutex_unlock(&binder_procs_lock);
5253
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005254 if (binder_debugfs_dir_entry_proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005255 char strbuf[11];
Seunghun Lee10f62862014-05-01 01:30:23 +09005256
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005257 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005258 /*
5259 * proc debug entries are shared between contexts, so
5260 * this will fail if the process tries to open the driver
5261 * again with a different context. The priting code will
5262 * anyway print all contexts that a given PID has, so this
5263 * is not a problem.
5264 */
Harsh Shandilya174562a2017-12-22 19:37:02 +05305265 proc->debugfs_entry = debugfs_create_file(strbuf, 0444,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005266 binder_debugfs_dir_entry_proc,
5267 (void *)(unsigned long)proc->pid,
5268 &binder_proc_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005269 }
5270
5271 return 0;
5272}
5273
5274static int binder_flush(struct file *filp, fl_owner_t id)
5275{
5276 struct binder_proc *proc = filp->private_data;
5277
5278 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
5279
5280 return 0;
5281}
5282
5283static void binder_deferred_flush(struct binder_proc *proc)
5284{
5285 struct rb_node *n;
5286 int wake_count = 0;
Seunghun Lee10f62862014-05-01 01:30:23 +09005287
Todd Kjosb4827902017-05-25 15:52:17 -07005288 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005289 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
5290 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
Seunghun Lee10f62862014-05-01 01:30:23 +09005291
Todd Kjos6798e6d2017-01-06 14:19:25 -08005292 thread->looper_need_return = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005293 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
5294 wake_up_interruptible(&thread->wait);
5295 wake_count++;
5296 }
5297 }
Todd Kjosb4827902017-05-25 15:52:17 -07005298 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005299
5300 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5301 "binder_flush: %d woke %d threads\n", proc->pid,
5302 wake_count);
5303}
5304
5305static int binder_release(struct inode *nodp, struct file *filp)
5306{
5307 struct binder_proc *proc = filp->private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09005308
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005309 debugfs_remove(proc->debugfs_entry);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005310 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
5311
5312 return 0;
5313}
5314
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005315static int binder_node_release(struct binder_node *node, int refs)
5316{
5317 struct binder_ref *ref;
5318 int death = 0;
Todd Kjose7f23ed2017-03-21 13:06:01 -07005319 struct binder_proc *proc = node->proc;
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005320
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005321 binder_release_work(proc, &node->async_todo);
Todd Kjose7f23ed2017-03-21 13:06:01 -07005322
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005323 binder_node_lock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07005324 binder_inner_proc_lock(proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005325 binder_dequeue_work_ilocked(&node->work);
Todd Kjosf22abc72017-05-09 11:08:05 -07005326 /*
5327 * The caller must have taken a temporary ref on the node,
5328 */
5329 BUG_ON(!node->tmp_refs);
5330 if (hlist_empty(&node->refs) && node->tmp_refs == 1) {
Todd Kjose7f23ed2017-03-21 13:06:01 -07005331 binder_inner_proc_unlock(proc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005332 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07005333 binder_free_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005334
5335 return refs;
5336 }
5337
5338 node->proc = NULL;
5339 node->local_strong_refs = 0;
5340 node->local_weak_refs = 0;
Todd Kjose7f23ed2017-03-21 13:06:01 -07005341 binder_inner_proc_unlock(proc);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005342
5343 spin_lock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005344 hlist_add_head(&node->dead_node, &binder_dead_nodes);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005345 spin_unlock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005346
5347 hlist_for_each_entry(ref, &node->refs, node_entry) {
5348 refs++;
Martijn Coenenf9eac642017-05-22 11:26:23 -07005349 /*
5350 * Need the node lock to synchronize
5351 * with new notification requests and the
5352 * inner lock to synchronize with queued
5353 * death notifications.
5354 */
5355 binder_inner_proc_lock(ref->proc);
5356 if (!ref->death) {
5357 binder_inner_proc_unlock(ref->proc);
Arve Hjønnevåge194fd82014-02-17 13:58:29 -08005358 continue;
Martijn Coenenf9eac642017-05-22 11:26:23 -07005359 }
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005360
5361 death++;
5362
Martijn Coenenf9eac642017-05-22 11:26:23 -07005363 BUG_ON(!list_empty(&ref->death->work.entry));
5364 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
5365 binder_enqueue_work_ilocked(&ref->death->work,
5366 &ref->proc->todo);
Martijn Coenen053be422017-06-06 15:17:46 -07005367 binder_wakeup_proc_ilocked(ref->proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005368 binder_inner_proc_unlock(ref->proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005369 }
5370
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005371 binder_debug(BINDER_DEBUG_DEAD_BINDER,
5372 "node %d now dead, refs %d, death %d\n",
5373 node->debug_id, refs, death);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005374 binder_node_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07005375 binder_put_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005376
5377 return refs;
5378}
5379
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005380static void binder_deferred_release(struct binder_proc *proc)
5381{
Martijn Coenen0b3311e2016-09-30 15:51:48 +02005382 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005383 struct rb_node *n;
Todd Kjosd325d372016-10-10 10:40:53 -07005384 int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005385
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005386 BUG_ON(proc->files);
5387
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005388 mutex_lock(&binder_procs_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005389 hlist_del(&proc->proc_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005390 mutex_unlock(&binder_procs_lock);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005391
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005392 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02005393 if (context->binder_context_mgr_node &&
5394 context->binder_context_mgr_node->proc == proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005395 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01005396 "%s: %d context_mgr_node gone\n",
5397 __func__, proc->pid);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02005398 context->binder_context_mgr_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005399 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005400 mutex_unlock(&context->context_mgr_node_lock);
Todd Kjosb4827902017-05-25 15:52:17 -07005401 binder_inner_proc_lock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07005402 /*
5403 * Make sure proc stays alive after we
5404 * remove all the threads
5405 */
Todd Kjosf4e6de32019-06-10 09:14:25 -07005406 atomic_inc(&proc->tmp_ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005407
Todd Kjos2f993e22017-05-12 14:42:55 -07005408 proc->is_dead = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005409 threads = 0;
5410 active_transactions = 0;
5411 while ((n = rb_first(&proc->threads))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005412 struct binder_thread *thread;
5413
5414 thread = rb_entry(n, struct binder_thread, rb_node);
Todd Kjosb4827902017-05-25 15:52:17 -07005415 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005416 threads++;
Todd Kjos2f993e22017-05-12 14:42:55 -07005417 active_transactions += binder_thread_release(proc, thread);
Todd Kjosb4827902017-05-25 15:52:17 -07005418 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005419 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005420
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005421 nodes = 0;
5422 incoming_refs = 0;
5423 while ((n = rb_first(&proc->nodes))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005424 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005425
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005426 node = rb_entry(n, struct binder_node, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005427 nodes++;
Todd Kjosf22abc72017-05-09 11:08:05 -07005428 /*
5429 * take a temporary ref on the node before
5430 * calling binder_node_release() which will either
5431 * kfree() the node or call binder_put_node()
5432 */
Todd Kjos425d23f2017-06-12 12:07:26 -07005433 binder_inc_node_tmpref_ilocked(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005434 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjos425d23f2017-06-12 12:07:26 -07005435 binder_inner_proc_unlock(proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005436 incoming_refs = binder_node_release(node, incoming_refs);
Todd Kjos425d23f2017-06-12 12:07:26 -07005437 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005438 }
Todd Kjos425d23f2017-06-12 12:07:26 -07005439 binder_inner_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005440
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005441 outgoing_refs = 0;
Todd Kjos5346bf32016-10-20 16:43:34 -07005442 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005443 while ((n = rb_first(&proc->refs_by_desc))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005444 struct binder_ref *ref;
5445
5446 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005447 outgoing_refs++;
Todd Kjos5346bf32016-10-20 16:43:34 -07005448 binder_cleanup_ref_olocked(ref);
5449 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07005450 binder_free_ref(ref);
Todd Kjos5346bf32016-10-20 16:43:34 -07005451 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005452 }
Todd Kjos5346bf32016-10-20 16:43:34 -07005453 binder_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005454
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005455 binder_release_work(proc, &proc->todo);
5456 binder_release_work(proc, &proc->delivered_death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005457
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005458 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Todd Kjosd325d372016-10-10 10:40:53 -07005459 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01005460 __func__, proc->pid, threads, nodes, incoming_refs,
Todd Kjosd325d372016-10-10 10:40:53 -07005461 outgoing_refs, active_transactions);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005462
Todd Kjos2f993e22017-05-12 14:42:55 -07005463 binder_proc_dec_tmpref(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005464}
5465
5466static void binder_deferred_func(struct work_struct *work)
5467{
5468 struct binder_proc *proc;
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005469 struct files_struct *files;
5470
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005471 int defer;
Seunghun Lee10f62862014-05-01 01:30:23 +09005472
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005473 do {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005474 mutex_lock(&binder_deferred_lock);
5475 if (!hlist_empty(&binder_deferred_list)) {
5476 proc = hlist_entry(binder_deferred_list.first,
5477 struct binder_proc, deferred_work_node);
5478 hlist_del_init(&proc->deferred_work_node);
5479 defer = proc->deferred_work;
5480 proc->deferred_work = 0;
5481 } else {
5482 proc = NULL;
5483 defer = 0;
5484 }
5485 mutex_unlock(&binder_deferred_lock);
5486
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005487 files = NULL;
5488 if (defer & BINDER_DEFERRED_PUT_FILES) {
Todd Kjosfbb43392017-11-27 09:32:33 -08005489 mutex_lock(&proc->files_lock);
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005490 files = proc->files;
5491 if (files)
5492 proc->files = NULL;
Todd Kjosfbb43392017-11-27 09:32:33 -08005493 mutex_unlock(&proc->files_lock);
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005494 }
5495
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005496 if (defer & BINDER_DEFERRED_FLUSH)
5497 binder_deferred_flush(proc);
5498
5499 if (defer & BINDER_DEFERRED_RELEASE)
5500 binder_deferred_release(proc); /* frees proc */
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005501
5502 if (files)
5503 put_files_struct(files);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005504 } while (proc);
5505}
5506static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
5507
5508static void
5509binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
5510{
5511 mutex_lock(&binder_deferred_lock);
5512 proc->deferred_work |= defer;
5513 if (hlist_unhashed(&proc->deferred_work_node)) {
5514 hlist_add_head(&proc->deferred_work_node,
5515 &binder_deferred_list);
Bhaktipriya Shridhar1beba522016-08-13 22:16:24 +05305516 schedule_work(&binder_deferred_work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005517 }
5518 mutex_unlock(&binder_deferred_lock);
5519}
5520
Todd Kjos6d241a42017-04-21 14:32:11 -07005521static void print_binder_transaction_ilocked(struct seq_file *m,
5522 struct binder_proc *proc,
5523 const char *prefix,
5524 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005525{
Todd Kjos6d241a42017-04-21 14:32:11 -07005526 struct binder_proc *to_proc;
5527 struct binder_buffer *buffer = t->buffer;
5528
Todd Kjos2f993e22017-05-12 14:42:55 -07005529 spin_lock(&t->lock);
Todd Kjos6d241a42017-04-21 14:32:11 -07005530 to_proc = t->to_proc;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005531 seq_printf(m,
Todd Kjosf540ce02018-02-07 13:57:37 -08005532 "%s %d: %pK from %d:%d to %d:%d code %x flags %x pri %d:%d r%d",
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005533 prefix, t->debug_id, t,
5534 t->from ? t->from->proc->pid : 0,
5535 t->from ? t->from->pid : 0,
Todd Kjos6d241a42017-04-21 14:32:11 -07005536 to_proc ? to_proc->pid : 0,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005537 t->to_thread ? t->to_thread->pid : 0,
Martijn Coenen57b2ac62017-06-06 17:04:42 -07005538 t->code, t->flags, t->priority.sched_policy,
5539 t->priority.prio, t->need_reply);
Todd Kjos2f993e22017-05-12 14:42:55 -07005540 spin_unlock(&t->lock);
5541
Todd Kjos6d241a42017-04-21 14:32:11 -07005542 if (proc != to_proc) {
5543 /*
5544 * Can only safely deref buffer if we are holding the
5545 * correct proc inner lock for this node
5546 */
5547 seq_puts(m, "\n");
5548 return;
5549 }
5550
5551 if (buffer == NULL) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005552 seq_puts(m, " buffer free\n");
5553 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005554 }
Todd Kjos6d241a42017-04-21 14:32:11 -07005555 if (buffer->target_node)
5556 seq_printf(m, " node %d", buffer->target_node->debug_id);
Todd Kjosf540ce02018-02-07 13:57:37 -08005557 seq_printf(m, " size %zd:%zd data %pK\n",
Todd Kjos6d241a42017-04-21 14:32:11 -07005558 buffer->data_size, buffer->offsets_size,
Todd Kjos8539b1e2019-02-08 10:35:20 -08005559 buffer->user_data);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005560}
5561
Todd Kjos6d241a42017-04-21 14:32:11 -07005562static void print_binder_work_ilocked(struct seq_file *m,
5563 struct binder_proc *proc,
5564 const char *prefix,
5565 const char *transaction_prefix,
5566 struct binder_work *w)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005567{
5568 struct binder_node *node;
5569 struct binder_transaction *t;
5570
5571 switch (w->type) {
5572 case BINDER_WORK_TRANSACTION:
5573 t = container_of(w, struct binder_transaction, work);
Todd Kjos6d241a42017-04-21 14:32:11 -07005574 print_binder_transaction_ilocked(
5575 m, proc, transaction_prefix, t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005576 break;
Todd Kjos858b8da2017-04-21 17:35:12 -07005577 case BINDER_WORK_RETURN_ERROR: {
5578 struct binder_error *e = container_of(
5579 w, struct binder_error, work);
5580
5581 seq_printf(m, "%stransaction error: %u\n",
5582 prefix, e->cmd);
5583 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005584 case BINDER_WORK_TRANSACTION_COMPLETE:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005585 seq_printf(m, "%stransaction complete\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005586 break;
5587 case BINDER_WORK_NODE:
5588 node = container_of(w, struct binder_node, work);
Arve Hjønnevågda498892014-02-21 14:40:26 -08005589 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
5590 prefix, node->debug_id,
5591 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005592 break;
5593 case BINDER_WORK_DEAD_BINDER:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005594 seq_printf(m, "%shas dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005595 break;
5596 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005597 seq_printf(m, "%shas cleared dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005598 break;
5599 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005600 seq_printf(m, "%shas cleared death notification\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005601 break;
5602 default:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005603 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005604 break;
5605 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005606}
5607
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005608static void print_binder_thread_ilocked(struct seq_file *m,
5609 struct binder_thread *thread,
5610 int print_always)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005611{
5612 struct binder_transaction *t;
5613 struct binder_work *w;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005614 size_t start_pos = m->count;
5615 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005616
Todd Kjos2f993e22017-05-12 14:42:55 -07005617 seq_printf(m, " thread %d: l %02x need_return %d tr %d\n",
Todd Kjos6798e6d2017-01-06 14:19:25 -08005618 thread->pid, thread->looper,
Todd Kjos2f993e22017-05-12 14:42:55 -07005619 thread->looper_need_return,
5620 atomic_read(&thread->tmp_ref));
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005621 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005622 t = thread->transaction_stack;
5623 while (t) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005624 if (t->from == thread) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005625 print_binder_transaction_ilocked(m, thread->proc,
5626 " outgoing transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005627 t = t->from_parent;
5628 } else if (t->to_thread == thread) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005629 print_binder_transaction_ilocked(m, thread->proc,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005630 " incoming transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005631 t = t->to_parent;
5632 } else {
Todd Kjos6d241a42017-04-21 14:32:11 -07005633 print_binder_transaction_ilocked(m, thread->proc,
5634 " bad transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005635 t = NULL;
5636 }
5637 }
5638 list_for_each_entry(w, &thread->todo, entry) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005639 print_binder_work_ilocked(m, thread->proc, " ",
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005640 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005641 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005642 if (!print_always && m->count == header_pos)
5643 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005644}
5645
Todd Kjos425d23f2017-06-12 12:07:26 -07005646static void print_binder_node_nilocked(struct seq_file *m,
5647 struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005648{
5649 struct binder_ref *ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005650 struct binder_work *w;
5651 int count;
5652
5653 count = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08005654 hlist_for_each_entry(ref, &node->refs, node_entry)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005655 count++;
5656
Martijn Coenen6aac9792017-06-07 09:29:14 -07005657 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 -08005658 node->debug_id, (u64)node->ptr, (u64)node->cookie,
Martijn Coenen6aac9792017-06-07 09:29:14 -07005659 node->sched_policy, node->min_priority,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005660 node->has_strong_ref, node->has_weak_ref,
5661 node->local_strong_refs, node->local_weak_refs,
Todd Kjosf22abc72017-05-09 11:08:05 -07005662 node->internal_strong_refs, count, node->tmp_refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005663 if (count) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005664 seq_puts(m, " proc");
Sasha Levinb67bfe02013-02-27 17:06:00 -08005665 hlist_for_each_entry(ref, &node->refs, node_entry)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005666 seq_printf(m, " %d", ref->proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005667 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005668 seq_puts(m, "\n");
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005669 if (node->proc) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005670 list_for_each_entry(w, &node->async_todo, entry)
Todd Kjos6d241a42017-04-21 14:32:11 -07005671 print_binder_work_ilocked(m, node->proc, " ",
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005672 " pending async transaction", w);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005673 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005674}
5675
Todd Kjos5346bf32016-10-20 16:43:34 -07005676static void print_binder_ref_olocked(struct seq_file *m,
5677 struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005678{
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005679 binder_node_lock(ref->node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07005680 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
5681 ref->data.debug_id, ref->data.desc,
5682 ref->node->proc ? "" : "dead ",
5683 ref->node->debug_id, ref->data.strong,
5684 ref->data.weak, ref->death);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005685 binder_node_unlock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005686}
5687
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005688static void print_binder_proc(struct seq_file *m,
5689 struct binder_proc *proc, int print_all)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005690{
5691 struct binder_work *w;
5692 struct rb_node *n;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005693 size_t start_pos = m->count;
5694 size_t header_pos;
Todd Kjos425d23f2017-06-12 12:07:26 -07005695 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005696
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005697 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005698 seq_printf(m, "context %s\n", proc->context->name);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005699 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005700
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005701 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005702 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005703 print_binder_thread_ilocked(m, rb_entry(n, struct binder_thread,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005704 rb_node), print_all);
Todd Kjos425d23f2017-06-12 12:07:26 -07005705
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005706 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005707 struct binder_node *node = rb_entry(n, struct binder_node,
5708 rb_node);
Todd Kjosd79aa412018-12-05 15:19:26 -08005709 if (!print_all && !node->has_async_transaction)
5710 continue;
5711
Todd Kjos425d23f2017-06-12 12:07:26 -07005712 /*
5713 * take a temporary reference on the node so it
5714 * survives and isn't removed from the tree
5715 * while we print it.
5716 */
5717 binder_inc_node_tmpref_ilocked(node);
5718 /* Need to drop inner lock to take node lock */
5719 binder_inner_proc_unlock(proc);
5720 if (last_node)
5721 binder_put_node(last_node);
5722 binder_node_inner_lock(node);
5723 print_binder_node_nilocked(m, node);
5724 binder_node_inner_unlock(node);
5725 last_node = node;
5726 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005727 }
Todd Kjos425d23f2017-06-12 12:07:26 -07005728 binder_inner_proc_unlock(proc);
5729 if (last_node)
5730 binder_put_node(last_node);
5731
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005732 if (print_all) {
Todd Kjos5346bf32016-10-20 16:43:34 -07005733 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005734 for (n = rb_first(&proc->refs_by_desc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005735 n != NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005736 n = rb_next(n))
Todd Kjos5346bf32016-10-20 16:43:34 -07005737 print_binder_ref_olocked(m, rb_entry(n,
5738 struct binder_ref,
5739 rb_node_desc));
5740 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005741 }
Todd Kjosd325d372016-10-10 10:40:53 -07005742 binder_alloc_print_allocated(m, &proc->alloc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005743 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005744 list_for_each_entry(w, &proc->todo, entry)
Todd Kjos6d241a42017-04-21 14:32:11 -07005745 print_binder_work_ilocked(m, proc, " ",
5746 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005747 list_for_each_entry(w, &proc->delivered_death, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005748 seq_puts(m, " has delivered dead binder\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005749 break;
5750 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005751 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005752 if (!print_all && m->count == header_pos)
5753 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005754}
5755
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005756static const char * const binder_return_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005757 "BR_ERROR",
5758 "BR_OK",
5759 "BR_TRANSACTION",
5760 "BR_REPLY",
5761 "BR_ACQUIRE_RESULT",
5762 "BR_DEAD_REPLY",
5763 "BR_TRANSACTION_COMPLETE",
5764 "BR_INCREFS",
5765 "BR_ACQUIRE",
5766 "BR_RELEASE",
5767 "BR_DECREFS",
5768 "BR_ATTEMPT_ACQUIRE",
5769 "BR_NOOP",
5770 "BR_SPAWN_LOOPER",
5771 "BR_FINISHED",
5772 "BR_DEAD_BINDER",
5773 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
5774 "BR_FAILED_REPLY"
5775};
5776
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005777static const char * const binder_command_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005778 "BC_TRANSACTION",
5779 "BC_REPLY",
5780 "BC_ACQUIRE_RESULT",
5781 "BC_FREE_BUFFER",
5782 "BC_INCREFS",
5783 "BC_ACQUIRE",
5784 "BC_RELEASE",
5785 "BC_DECREFS",
5786 "BC_INCREFS_DONE",
5787 "BC_ACQUIRE_DONE",
5788 "BC_ATTEMPT_ACQUIRE",
5789 "BC_REGISTER_LOOPER",
5790 "BC_ENTER_LOOPER",
5791 "BC_EXIT_LOOPER",
5792 "BC_REQUEST_DEATH_NOTIFICATION",
5793 "BC_CLEAR_DEATH_NOTIFICATION",
Martijn Coenen5a6da532016-09-30 14:10:07 +02005794 "BC_DEAD_BINDER_DONE",
5795 "BC_TRANSACTION_SG",
5796 "BC_REPLY_SG",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005797};
5798
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005799static const char * const binder_objstat_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005800 "proc",
5801 "thread",
5802 "node",
5803 "ref",
5804 "death",
5805 "transaction",
5806 "transaction_complete"
5807};
5808
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005809static void print_binder_stats(struct seq_file *m, const char *prefix,
5810 struct binder_stats *stats)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005811{
5812 int i;
5813
5814 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005815 ARRAY_SIZE(binder_command_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005816 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005817 int temp = atomic_read(&stats->bc[i]);
5818
5819 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005820 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005821 binder_command_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005822 }
5823
5824 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005825 ARRAY_SIZE(binder_return_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005826 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005827 int temp = atomic_read(&stats->br[i]);
5828
5829 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005830 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005831 binder_return_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005832 }
5833
5834 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005835 ARRAY_SIZE(binder_objstat_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005836 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005837 ARRAY_SIZE(stats->obj_deleted));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005838 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005839 int created = atomic_read(&stats->obj_created[i]);
5840 int deleted = atomic_read(&stats->obj_deleted[i]);
5841
5842 if (created || deleted)
5843 seq_printf(m, "%s%s: active %d total %d\n",
5844 prefix,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005845 binder_objstat_strings[i],
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005846 created - deleted,
5847 created);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005848 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005849}
5850
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005851static void print_binder_proc_stats(struct seq_file *m,
5852 struct binder_proc *proc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005853{
5854 struct binder_work *w;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005855 struct binder_thread *thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005856 struct rb_node *n;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005857 int count, strong, weak, ready_threads;
Todd Kjosb4827902017-05-25 15:52:17 -07005858 size_t free_async_space =
5859 binder_alloc_get_free_async_space(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005860
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005861 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005862 seq_printf(m, "context %s\n", proc->context->name);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005863 count = 0;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005864 ready_threads = 0;
Todd Kjosb4827902017-05-25 15:52:17 -07005865 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005866 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
5867 count++;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005868
5869 list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node)
5870 ready_threads++;
5871
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005872 seq_printf(m, " threads: %d\n", count);
5873 seq_printf(m, " requested threads: %d+%d/%d\n"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005874 " ready threads %d\n"
5875 " free async space %zd\n", proc->requested_threads,
5876 proc->requested_threads_started, proc->max_threads,
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005877 ready_threads,
Todd Kjosb4827902017-05-25 15:52:17 -07005878 free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005879 count = 0;
5880 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
5881 count++;
Todd Kjos425d23f2017-06-12 12:07:26 -07005882 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005883 seq_printf(m, " nodes: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005884 count = 0;
5885 strong = 0;
5886 weak = 0;
Todd Kjos5346bf32016-10-20 16:43:34 -07005887 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005888 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
5889 struct binder_ref *ref = rb_entry(n, struct binder_ref,
5890 rb_node_desc);
5891 count++;
Todd Kjosb0117bb2017-05-08 09:16:27 -07005892 strong += ref->data.strong;
5893 weak += ref->data.weak;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005894 }
Todd Kjos5346bf32016-10-20 16:43:34 -07005895 binder_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005896 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005897
Todd Kjosd325d372016-10-10 10:40:53 -07005898 count = binder_alloc_get_allocated_count(&proc->alloc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005899 seq_printf(m, " buffers: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005900
Sherry Yang91004422017-08-22 17:26:57 -07005901 binder_alloc_print_pages(m, &proc->alloc);
5902
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005903 count = 0;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005904 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005905 list_for_each_entry(w, &proc->todo, entry) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005906 if (w->type == BINDER_WORK_TRANSACTION)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005907 count++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005908 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005909 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005910 seq_printf(m, " pending transactions: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005911
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005912 print_binder_stats(m, " ", &proc->stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005913}
5914
5915
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005916static int binder_state_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005917{
5918 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005919 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005920 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005921
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005922 seq_puts(m, "binder state:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005923
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005924 spin_lock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005925 if (!hlist_empty(&binder_dead_nodes))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005926 seq_puts(m, "dead nodes:\n");
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005927 hlist_for_each_entry(node, &binder_dead_nodes, dead_node) {
5928 /*
5929 * take a temporary reference on the node so it
5930 * survives and isn't removed from the list
5931 * while we print it.
5932 */
5933 node->tmp_refs++;
5934 spin_unlock(&binder_dead_nodes_lock);
5935 if (last_node)
5936 binder_put_node(last_node);
5937 binder_node_lock(node);
Todd Kjos425d23f2017-06-12 12:07:26 -07005938 print_binder_node_nilocked(m, node);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005939 binder_node_unlock(node);
5940 last_node = node;
5941 spin_lock(&binder_dead_nodes_lock);
5942 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005943 spin_unlock(&binder_dead_nodes_lock);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005944 if (last_node)
5945 binder_put_node(last_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005946
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005947 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005948 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005949 print_binder_proc(m, proc, 1);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005950 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005951
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005952 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005953}
5954
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005955static int binder_stats_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005956{
5957 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005958
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005959 seq_puts(m, "binder stats:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005960
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005961 print_binder_stats(m, "", &binder_stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005962
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005963 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005964 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005965 print_binder_proc_stats(m, proc);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005966 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005967
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005968 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005969}
5970
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005971static int binder_transactions_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005972{
5973 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005974
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005975 seq_puts(m, "binder transactions:\n");
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005976 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005977 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005978 print_binder_proc(m, proc, 0);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005979 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005980
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005981 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005982}
5983
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005984static int binder_proc_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005985{
Riley Andrews83050a42016-02-09 21:05:33 -08005986 struct binder_proc *itr;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005987 int pid = (unsigned long)m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005988
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005989 mutex_lock(&binder_procs_lock);
Riley Andrews83050a42016-02-09 21:05:33 -08005990 hlist_for_each_entry(itr, &binder_procs, proc_node) {
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005991 if (itr->pid == pid) {
5992 seq_puts(m, "binder proc state:\n");
5993 print_binder_proc(m, itr, 1);
Riley Andrews83050a42016-02-09 21:05:33 -08005994 }
5995 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005996 mutex_unlock(&binder_procs_lock);
5997
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005998 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005999}
6000
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006001static void print_binder_transaction_log_entry(struct seq_file *m,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006002 struct binder_transaction_log_entry *e)
6003{
Todd Kjos1cfe6272017-05-24 13:33:28 -07006004 int debug_id = READ_ONCE(e->debug_id_done);
6005 /*
6006 * read barrier to guarantee debug_id_done read before
6007 * we print the log values
6008 */
6009 smp_rmb();
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006010 seq_printf(m,
Todd Kjos1cfe6272017-05-24 13:33:28 -07006011 "%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 -07006012 e->debug_id, (e->call_type == 2) ? "reply" :
6013 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02006014 e->from_thread, e->to_proc, e->to_thread, e->context_name,
Todd Kjose598d172017-03-22 17:19:52 -07006015 e->to_node, e->target_handle, e->data_size, e->offsets_size,
6016 e->return_error, e->return_error_param,
6017 e->return_error_line);
Todd Kjos1cfe6272017-05-24 13:33:28 -07006018 /*
6019 * read-barrier to guarantee read of debug_id_done after
6020 * done printing the fields of the entry
6021 */
6022 smp_rmb();
6023 seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ?
6024 "\n" : " (incomplete)\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006025}
6026
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006027static int binder_transaction_log_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006028{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006029 struct binder_transaction_log *log = m->private;
Todd Kjos1cfe6272017-05-24 13:33:28 -07006030 unsigned int log_cur = atomic_read(&log->cur);
6031 unsigned int count;
6032 unsigned int cur;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006033 int i;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006034
Todd Kjos1cfe6272017-05-24 13:33:28 -07006035 count = log_cur + 1;
6036 cur = count < ARRAY_SIZE(log->entry) && !log->full ?
6037 0 : count % ARRAY_SIZE(log->entry);
6038 if (count > ARRAY_SIZE(log->entry) || log->full)
6039 count = ARRAY_SIZE(log->entry);
6040 for (i = 0; i < count; i++) {
6041 unsigned int index = cur++ % ARRAY_SIZE(log->entry);
6042
6043 print_binder_transaction_log_entry(m, &log->entry[index]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006044 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006045 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006046}
6047
6048static const struct file_operations binder_fops = {
6049 .owner = THIS_MODULE,
6050 .poll = binder_poll,
6051 .unlocked_ioctl = binder_ioctl,
Arve Hjønnevågda498892014-02-21 14:40:26 -08006052 .compat_ioctl = binder_ioctl,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006053 .mmap = binder_mmap,
6054 .open = binder_open,
6055 .flush = binder_flush,
6056 .release = binder_release,
6057};
6058
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006059BINDER_DEBUG_ENTRY(state);
6060BINDER_DEBUG_ENTRY(stats);
6061BINDER_DEBUG_ENTRY(transactions);
6062BINDER_DEBUG_ENTRY(transaction_log);
6063
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006064static int __init init_binder_device(const char *name)
6065{
6066 int ret;
6067 struct binder_device *binder_device;
6068
6069 binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
6070 if (!binder_device)
6071 return -ENOMEM;
6072
6073 binder_device->miscdev.fops = &binder_fops;
6074 binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
6075 binder_device->miscdev.name = name;
6076
6077 binder_device->context.binder_context_mgr_uid = INVALID_UID;
6078 binder_device->context.name = name;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07006079 mutex_init(&binder_device->context.context_mgr_node_lock);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006080
6081 ret = misc_register(&binder_device->miscdev);
6082 if (ret < 0) {
6083 kfree(binder_device);
6084 return ret;
6085 }
6086
6087 hlist_add_head(&binder_device->hlist, &binder_devices);
6088
6089 return ret;
6090}
6091
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006092static int __init binder_init(void)
6093{
6094 int ret;
Christian Brauner558ee932017-08-21 16:13:28 +02006095 char *device_name, *device_names, *device_tmp;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006096 struct binder_device *device;
6097 struct hlist_node *tmp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006098
Tetsuo Handaf8cb8222017-11-29 22:29:47 +09006099 ret = binder_alloc_shrinker_init();
6100 if (ret)
6101 return ret;
Sherry Yang5828d702017-07-29 13:24:11 -07006102
Todd Kjos1cfe6272017-05-24 13:33:28 -07006103 atomic_set(&binder_transaction_log.cur, ~0U);
6104 atomic_set(&binder_transaction_log_failed.cur, ~0U);
6105
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006106 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
6107 if (binder_debugfs_dir_entry_root)
6108 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
6109 binder_debugfs_dir_entry_root);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006110
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006111 if (binder_debugfs_dir_entry_root) {
6112 debugfs_create_file("state",
Harsh Shandilya174562a2017-12-22 19:37:02 +05306113 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006114 binder_debugfs_dir_entry_root,
6115 NULL,
6116 &binder_state_fops);
6117 debugfs_create_file("stats",
Harsh Shandilya174562a2017-12-22 19:37:02 +05306118 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006119 binder_debugfs_dir_entry_root,
6120 NULL,
6121 &binder_stats_fops);
6122 debugfs_create_file("transactions",
Harsh Shandilya174562a2017-12-22 19:37:02 +05306123 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006124 binder_debugfs_dir_entry_root,
6125 NULL,
6126 &binder_transactions_fops);
6127 debugfs_create_file("transaction_log",
Harsh Shandilya174562a2017-12-22 19:37:02 +05306128 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006129 binder_debugfs_dir_entry_root,
6130 &binder_transaction_log,
6131 &binder_transaction_log_fops);
6132 debugfs_create_file("failed_transaction_log",
Harsh Shandilya174562a2017-12-22 19:37:02 +05306133 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006134 binder_debugfs_dir_entry_root,
6135 &binder_transaction_log_failed,
6136 &binder_transaction_log_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006137 }
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006138
6139 /*
6140 * Copy the module_parameter string, because we don't want to
6141 * tokenize it in-place.
6142 */
6143 device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL);
6144 if (!device_names) {
6145 ret = -ENOMEM;
6146 goto err_alloc_device_names_failed;
6147 }
6148 strcpy(device_names, binder_devices_param);
6149
Christian Brauner558ee932017-08-21 16:13:28 +02006150 device_tmp = device_names;
6151 while ((device_name = strsep(&device_tmp, ","))) {
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006152 ret = init_binder_device(device_name);
6153 if (ret)
6154 goto err_init_binder_device_failed;
6155 }
6156
6157 return ret;
6158
6159err_init_binder_device_failed:
6160 hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
6161 misc_deregister(&device->miscdev);
6162 hlist_del(&device->hlist);
6163 kfree(device);
6164 }
Christian Brauner558ee932017-08-21 16:13:28 +02006165
6166 kfree(device_names);
6167
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006168err_alloc_device_names_failed:
6169 debugfs_remove_recursive(binder_debugfs_dir_entry_root);
6170
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006171 return ret;
6172}
6173
6174device_initcall(binder_init);
6175
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07006176#define CREATE_TRACE_POINTS
6177#include "binder_trace.h"
6178
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006179MODULE_LICENSE("GPL v2");