blob: 51bef2482149c3c84e633b1131ff81f17c6e188f [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
Todd Kjosa18908e2020-07-20 21:14:33 -0700284 enum binder_work_type {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900285 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
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900941static void
942binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
Todd Kjos2f993e22017-05-12 14:42:55 -0700943static void binder_free_thread(struct binder_thread *thread);
944static void binder_free_proc(struct binder_proc *proc);
Todd Kjos425d23f2017-06-12 12:07:26 -0700945static void binder_inc_node_tmpref_ilocked(struct binder_node *node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900946
Sachin Kamatefde99c2012-08-17 16:39:36 +0530947static int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900948{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900949 unsigned long rlim_cur;
950 unsigned long irqs;
Todd Kjosfbb43392017-11-27 09:32:33 -0800951 int ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900952
Todd Kjosfbb43392017-11-27 09:32:33 -0800953 mutex_lock(&proc->files_lock);
954 if (proc->files == NULL) {
955 ret = -ESRCH;
956 goto err;
957 }
958 if (!lock_task_sighand(proc->tsk, &irqs)) {
959 ret = -EMFILE;
960 goto err;
961 }
Al Virodcfadfa2012-08-12 17:27:30 -0400962 rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
963 unlock_task_sighand(proc->tsk, &irqs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900964
Todd Kjosfbb43392017-11-27 09:32:33 -0800965 ret = __alloc_fd(proc->files, 0, rlim_cur, flags);
966err:
967 mutex_unlock(&proc->files_lock);
968 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900969}
970
971/*
972 * copied from fd_install
973 */
974static void task_fd_install(
975 struct binder_proc *proc, unsigned int fd, struct file *file)
976{
Todd Kjosfbb43392017-11-27 09:32:33 -0800977 mutex_lock(&proc->files_lock);
Martijn Coenen6f7e5f92018-06-15 11:53:36 +0200978 if (proc->files)
979 __fd_install(proc->files, fd, file);
Todd Kjosfbb43392017-11-27 09:32:33 -0800980 mutex_unlock(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900981}
982
983/*
984 * copied from sys_close
985 */
986static long task_close_fd(struct binder_proc *proc, unsigned int fd)
987{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900988 int retval;
989
Todd Kjosfbb43392017-11-27 09:32:33 -0800990 mutex_lock(&proc->files_lock);
991 if (proc->files == NULL) {
992 retval = -ESRCH;
993 goto err;
994 }
Martijn Coenen6f7e5f92018-06-15 11:53:36 +0200995 retval = __close_fd(proc->files, fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900996 /* can't restart close syscall because file table entry was cleared */
997 if (unlikely(retval == -ERESTARTSYS ||
998 retval == -ERESTARTNOINTR ||
999 retval == -ERESTARTNOHAND ||
1000 retval == -ERESTART_RESTARTBLOCK))
1001 retval = -EINTR;
Todd Kjosfbb43392017-11-27 09:32:33 -08001002err:
1003 mutex_unlock(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001004 return retval;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001005}
1006
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001007static bool binder_has_work_ilocked(struct binder_thread *thread,
1008 bool do_proc_work)
1009{
Martijn Coenen1af61802017-10-19 15:04:46 +02001010 return thread->process_todo ||
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001011 thread->looper_need_return ||
1012 (do_proc_work &&
1013 !binder_worklist_empty_ilocked(&thread->proc->todo));
1014}
1015
1016static bool binder_has_work(struct binder_thread *thread, bool do_proc_work)
1017{
1018 bool has_work;
1019
1020 binder_inner_proc_lock(thread->proc);
1021 has_work = binder_has_work_ilocked(thread, do_proc_work);
1022 binder_inner_proc_unlock(thread->proc);
1023
1024 return has_work;
1025}
1026
1027static bool binder_available_for_proc_work_ilocked(struct binder_thread *thread)
1028{
1029 return !thread->transaction_stack &&
1030 binder_worklist_empty_ilocked(&thread->todo) &&
1031 (thread->looper & (BINDER_LOOPER_STATE_ENTERED |
1032 BINDER_LOOPER_STATE_REGISTERED));
1033}
1034
1035static void binder_wakeup_poll_threads_ilocked(struct binder_proc *proc,
1036 bool sync)
1037{
1038 struct rb_node *n;
1039 struct binder_thread *thread;
1040
1041 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
1042 thread = rb_entry(n, struct binder_thread, rb_node);
1043 if (thread->looper & BINDER_LOOPER_STATE_POLL &&
1044 binder_available_for_proc_work_ilocked(thread)) {
1045 if (sync)
1046 wake_up_interruptible_sync(&thread->wait);
1047 else
1048 wake_up_interruptible(&thread->wait);
1049 }
1050 }
1051}
1052
Martijn Coenen053be422017-06-06 15:17:46 -07001053/**
1054 * binder_select_thread_ilocked() - selects a thread for doing proc work.
1055 * @proc: process to select a thread from
1056 *
1057 * Note that calling this function moves the thread off the waiting_threads
1058 * list, so it can only be woken up by the caller of this function, or a
1059 * signal. Therefore, callers *should* always wake up the thread this function
1060 * returns.
1061 *
1062 * Return: If there's a thread currently waiting for process work,
1063 * returns that thread. Otherwise returns NULL.
1064 */
1065static struct binder_thread *
1066binder_select_thread_ilocked(struct binder_proc *proc)
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001067{
1068 struct binder_thread *thread;
1069
Martijn Coenened323352017-07-27 23:52:24 +02001070 assert_spin_locked(&proc->inner_lock);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001071 thread = list_first_entry_or_null(&proc->waiting_threads,
1072 struct binder_thread,
1073 waiting_thread_node);
1074
Martijn Coenen053be422017-06-06 15:17:46 -07001075 if (thread)
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001076 list_del_init(&thread->waiting_thread_node);
Martijn Coenen053be422017-06-06 15:17:46 -07001077
1078 return thread;
1079}
1080
1081/**
1082 * binder_wakeup_thread_ilocked() - wakes up a thread for doing proc work.
1083 * @proc: process to wake up a thread in
1084 * @thread: specific thread to wake-up (may be NULL)
1085 * @sync: whether to do a synchronous wake-up
1086 *
1087 * This function wakes up a thread in the @proc process.
1088 * The caller may provide a specific thread to wake-up in
1089 * the @thread parameter. If @thread is NULL, this function
1090 * will wake up threads that have called poll().
1091 *
1092 * Note that for this function to work as expected, callers
1093 * should first call binder_select_thread() to find a thread
1094 * to handle the work (if they don't have a thread already),
1095 * and pass the result into the @thread parameter.
1096 */
1097static void binder_wakeup_thread_ilocked(struct binder_proc *proc,
1098 struct binder_thread *thread,
1099 bool sync)
1100{
Martijn Coenened323352017-07-27 23:52:24 +02001101 assert_spin_locked(&proc->inner_lock);
Martijn Coenen053be422017-06-06 15:17:46 -07001102
1103 if (thread) {
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001104 if (sync)
1105 wake_up_interruptible_sync(&thread->wait);
1106 else
1107 wake_up_interruptible(&thread->wait);
1108 return;
1109 }
1110
1111 /* Didn't find a thread waiting for proc work; this can happen
1112 * in two scenarios:
1113 * 1. All threads are busy handling transactions
1114 * In that case, one of those threads should call back into
1115 * the kernel driver soon and pick up this work.
1116 * 2. Threads are using the (e)poll interface, in which case
1117 * they may be blocked on the waitqueue without having been
1118 * added to waiting_threads. For this case, we just iterate
1119 * over all threads not handling transaction work, and
1120 * wake them all up. We wake all because we don't know whether
1121 * a thread that called into (e)poll is handling non-binder
1122 * work currently.
1123 */
1124 binder_wakeup_poll_threads_ilocked(proc, sync);
1125}
1126
Martijn Coenen053be422017-06-06 15:17:46 -07001127static void binder_wakeup_proc_ilocked(struct binder_proc *proc)
1128{
1129 struct binder_thread *thread = binder_select_thread_ilocked(proc);
1130
1131 binder_wakeup_thread_ilocked(proc, thread, /* sync = */false);
1132}
1133
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001134static bool is_rt_policy(int policy)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001135{
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001136 return policy == SCHED_FIFO || policy == SCHED_RR;
1137}
Seunghun Lee10f62862014-05-01 01:30:23 +09001138
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001139static bool is_fair_policy(int policy)
1140{
1141 return policy == SCHED_NORMAL || policy == SCHED_BATCH;
1142}
1143
1144static bool binder_supported_policy(int policy)
1145{
1146 return is_fair_policy(policy) || is_rt_policy(policy);
1147}
1148
1149static int to_userspace_prio(int policy, int kernel_priority)
1150{
1151 if (is_fair_policy(policy))
1152 return PRIO_TO_NICE(kernel_priority);
1153 else
1154 return MAX_USER_RT_PRIO - 1 - kernel_priority;
1155}
1156
1157static int to_kernel_prio(int policy, int user_priority)
1158{
1159 if (is_fair_policy(policy))
1160 return NICE_TO_PRIO(user_priority);
1161 else
1162 return MAX_USER_RT_PRIO - 1 - user_priority;
1163}
1164
Martijn Coenenecd972d2017-05-26 10:48:56 -07001165static void binder_do_set_priority(struct task_struct *task,
1166 struct binder_priority desired,
1167 bool verify)
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001168{
1169 int priority; /* user-space prio value */
1170 bool has_cap_nice;
1171 unsigned int policy = desired.sched_policy;
1172
1173 if (task->policy == policy && task->normal_prio == desired.prio)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001174 return;
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001175
1176 has_cap_nice = has_capability_noaudit(task, CAP_SYS_NICE);
1177
1178 priority = to_userspace_prio(policy, desired.prio);
1179
Martijn Coenenecd972d2017-05-26 10:48:56 -07001180 if (verify && is_rt_policy(policy) && !has_cap_nice) {
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001181 long max_rtprio = task_rlimit(task, RLIMIT_RTPRIO);
1182
1183 if (max_rtprio == 0) {
1184 policy = SCHED_NORMAL;
1185 priority = MIN_NICE;
1186 } else if (priority > max_rtprio) {
1187 priority = max_rtprio;
1188 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001189 }
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001190
Martijn Coenenecd972d2017-05-26 10:48:56 -07001191 if (verify && is_fair_policy(policy) && !has_cap_nice) {
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001192 long min_nice = rlimit_to_nice(task_rlimit(task, RLIMIT_NICE));
1193
1194 if (min_nice > MAX_NICE) {
1195 binder_user_error("%d RLIMIT_NICE not set\n",
1196 task->pid);
1197 return;
1198 } else if (priority < min_nice) {
1199 priority = min_nice;
1200 }
1201 }
1202
1203 if (policy != desired.sched_policy ||
1204 to_kernel_prio(policy, priority) != desired.prio)
1205 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
1206 "%d: priority %d not allowed, using %d instead\n",
1207 task->pid, desired.prio,
1208 to_kernel_prio(policy, priority));
1209
Martijn Coenen81402ea2017-05-08 09:33:22 -07001210 trace_binder_set_priority(task->tgid, task->pid, task->normal_prio,
1211 to_kernel_prio(policy, priority),
1212 desired.prio);
1213
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001214 /* Set the actual priority */
1215 if (task->policy != policy || is_rt_policy(policy)) {
1216 struct sched_param params;
1217
1218 params.sched_priority = is_rt_policy(policy) ? priority : 0;
1219
1220 sched_setscheduler_nocheck(task,
1221 policy | SCHED_RESET_ON_FORK,
1222 &params);
1223 }
1224 if (is_fair_policy(policy))
1225 set_user_nice(task, priority);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001226}
1227
Martijn Coenenecd972d2017-05-26 10:48:56 -07001228static void binder_set_priority(struct task_struct *task,
1229 struct binder_priority desired)
1230{
1231 binder_do_set_priority(task, desired, /* verify = */ true);
1232}
1233
1234static void binder_restore_priority(struct task_struct *task,
1235 struct binder_priority desired)
1236{
1237 binder_do_set_priority(task, desired, /* verify = */ false);
1238}
1239
Martijn Coenen07a30fe2017-06-07 10:02:12 -07001240static void binder_transaction_priority(struct task_struct *task,
1241 struct binder_transaction *t,
Martijn Coenenc46810c2017-06-23 10:13:43 -07001242 struct binder_priority node_prio,
1243 bool inherit_rt)
Martijn Coenen07a30fe2017-06-07 10:02:12 -07001244{
Ganesh Mahendran9add7c42017-09-27 15:12:25 +08001245 struct binder_priority desired_prio = t->priority;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07001246
1247 if (t->set_priority_called)
1248 return;
1249
1250 t->set_priority_called = true;
1251 t->saved_priority.sched_policy = task->policy;
1252 t->saved_priority.prio = task->normal_prio;
1253
Martijn Coenenc46810c2017-06-23 10:13:43 -07001254 if (!inherit_rt && is_rt_policy(desired_prio.sched_policy)) {
1255 desired_prio.prio = NICE_TO_PRIO(0);
1256 desired_prio.sched_policy = SCHED_NORMAL;
Martijn Coenenc46810c2017-06-23 10:13:43 -07001257 }
Martijn Coenen07a30fe2017-06-07 10:02:12 -07001258
1259 if (node_prio.prio < t->priority.prio ||
1260 (node_prio.prio == t->priority.prio &&
1261 node_prio.sched_policy == SCHED_FIFO)) {
1262 /*
1263 * In case the minimum priority on the node is
1264 * higher (lower value), use that priority. If
1265 * the priority is the same, but the node uses
1266 * SCHED_FIFO, prefer SCHED_FIFO, since it can
1267 * run unbounded, unlike SCHED_RR.
1268 */
1269 desired_prio = node_prio;
1270 }
1271
1272 binder_set_priority(task, desired_prio);
1273}
1274
Todd Kjos425d23f2017-06-12 12:07:26 -07001275static struct binder_node *binder_get_node_ilocked(struct binder_proc *proc,
1276 binder_uintptr_t ptr)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001277{
1278 struct rb_node *n = proc->nodes.rb_node;
1279 struct binder_node *node;
1280
Martijn Coenened323352017-07-27 23:52:24 +02001281 assert_spin_locked(&proc->inner_lock);
Todd Kjos425d23f2017-06-12 12:07:26 -07001282
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001283 while (n) {
1284 node = rb_entry(n, struct binder_node, rb_node);
1285
1286 if (ptr < node->ptr)
1287 n = n->rb_left;
1288 else if (ptr > node->ptr)
1289 n = n->rb_right;
Todd Kjosf22abc72017-05-09 11:08:05 -07001290 else {
1291 /*
1292 * take an implicit weak reference
1293 * to ensure node stays alive until
1294 * call to binder_put_node()
1295 */
Todd Kjos425d23f2017-06-12 12:07:26 -07001296 binder_inc_node_tmpref_ilocked(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001297 return node;
Todd Kjosf22abc72017-05-09 11:08:05 -07001298 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001299 }
1300 return NULL;
1301}
1302
Todd Kjos425d23f2017-06-12 12:07:26 -07001303static struct binder_node *binder_get_node(struct binder_proc *proc,
1304 binder_uintptr_t ptr)
1305{
1306 struct binder_node *node;
1307
1308 binder_inner_proc_lock(proc);
1309 node = binder_get_node_ilocked(proc, ptr);
1310 binder_inner_proc_unlock(proc);
1311 return node;
1312}
1313
1314static struct binder_node *binder_init_node_ilocked(
1315 struct binder_proc *proc,
1316 struct binder_node *new_node,
1317 struct flat_binder_object *fp)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001318{
1319 struct rb_node **p = &proc->nodes.rb_node;
1320 struct rb_node *parent = NULL;
1321 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001322 binder_uintptr_t ptr = fp ? fp->binder : 0;
1323 binder_uintptr_t cookie = fp ? fp->cookie : 0;
1324 __u32 flags = fp ? fp->flags : 0;
Martijn Coenen6aac9792017-06-07 09:29:14 -07001325 s8 priority;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001326
Martijn Coenened323352017-07-27 23:52:24 +02001327 assert_spin_locked(&proc->inner_lock);
1328
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001329 while (*p) {
Todd Kjos425d23f2017-06-12 12:07:26 -07001330
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001331 parent = *p;
1332 node = rb_entry(parent, struct binder_node, rb_node);
1333
1334 if (ptr < node->ptr)
1335 p = &(*p)->rb_left;
1336 else if (ptr > node->ptr)
1337 p = &(*p)->rb_right;
Todd Kjos425d23f2017-06-12 12:07:26 -07001338 else {
1339 /*
1340 * A matching node is already in
1341 * the rb tree. Abandon the init
1342 * and return it.
1343 */
1344 binder_inc_node_tmpref_ilocked(node);
1345 return node;
1346 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001347 }
Todd Kjos425d23f2017-06-12 12:07:26 -07001348 node = new_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001349 binder_stats_created(BINDER_STAT_NODE);
Todd Kjosf22abc72017-05-09 11:08:05 -07001350 node->tmp_refs++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001351 rb_link_node(&node->rb_node, parent, p);
1352 rb_insert_color(&node->rb_node, &proc->nodes);
Todd Kjosc4bd08b2017-05-25 10:56:00 -07001353 node->debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001354 node->proc = proc;
1355 node->ptr = ptr;
1356 node->cookie = cookie;
1357 node->work.type = BINDER_WORK_NODE;
Martijn Coenen6aac9792017-06-07 09:29:14 -07001358 priority = flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
Ganesh Mahendran6cd26312017-09-26 17:56:25 +08001359 node->sched_policy = (flags & FLAT_BINDER_FLAG_SCHED_POLICY_MASK) >>
Martijn Coenen6aac9792017-06-07 09:29:14 -07001360 FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT;
1361 node->min_priority = to_kernel_prio(node->sched_policy, priority);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001362 node->accept_fds = !!(flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
Martijn Coenenc46810c2017-06-23 10:13:43 -07001363 node->inherit_rt = !!(flags & FLAT_BINDER_FLAG_INHERIT_RT);
Todd Kjos63e0afa2019-01-14 09:10:21 -08001364 node->txn_security_ctx = !!(flags & FLAT_BINDER_FLAG_TXN_SECURITY_CTX);
Todd Kjosfc7a7e22017-05-29 16:44:24 -07001365 spin_lock_init(&node->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001366 INIT_LIST_HEAD(&node->work.entry);
1367 INIT_LIST_HEAD(&node->async_todo);
1368 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001369 "%d:%d node %d u%016llx c%016llx created\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001370 proc->pid, current->pid, node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001371 (u64)node->ptr, (u64)node->cookie);
Todd Kjos425d23f2017-06-12 12:07:26 -07001372
1373 return node;
1374}
1375
1376static struct binder_node *binder_new_node(struct binder_proc *proc,
1377 struct flat_binder_object *fp)
1378{
1379 struct binder_node *node;
1380 struct binder_node *new_node = kzalloc(sizeof(*node), GFP_KERNEL);
1381
1382 if (!new_node)
1383 return NULL;
1384 binder_inner_proc_lock(proc);
1385 node = binder_init_node_ilocked(proc, new_node, fp);
1386 binder_inner_proc_unlock(proc);
1387 if (node != new_node)
1388 /*
1389 * The node was already added by another thread
1390 */
1391 kfree(new_node);
1392
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001393 return node;
1394}
1395
Todd Kjose7f23ed2017-03-21 13:06:01 -07001396static void binder_free_node(struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001397{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001398 kfree(node);
1399 binder_stats_deleted(BINDER_STAT_NODE);
1400}
1401
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001402static int binder_inc_node_nilocked(struct binder_node *node, int strong,
1403 int internal,
1404 struct list_head *target_list)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001405{
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001406 struct binder_proc *proc = node->proc;
1407
Martijn Coenened323352017-07-27 23:52:24 +02001408 assert_spin_locked(&node->lock);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001409 if (proc)
Martijn Coenened323352017-07-27 23:52:24 +02001410 assert_spin_locked(&proc->inner_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001411 if (strong) {
1412 if (internal) {
1413 if (target_list == NULL &&
1414 node->internal_strong_refs == 0 &&
Martijn Coenen0b3311e2016-09-30 15:51:48 +02001415 !(node->proc &&
1416 node == node->proc->context->
1417 binder_context_mgr_node &&
1418 node->has_strong_ref)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301419 pr_err("invalid inc strong node for %d\n",
1420 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001421 return -EINVAL;
1422 }
1423 node->internal_strong_refs++;
1424 } else
1425 node->local_strong_refs++;
1426 if (!node->has_strong_ref && target_list) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001427 binder_dequeue_work_ilocked(&node->work);
Martijn Coenen1af61802017-10-19 15:04:46 +02001428 /*
1429 * Note: this function is the only place where we queue
1430 * directly to a thread->todo without using the
1431 * corresponding binder_enqueue_thread_work() helper
1432 * functions; in this case it's ok to not set the
1433 * process_todo flag, since we know this node work will
1434 * always be followed by other work that starts queue
1435 * processing: in case of synchronous transactions, a
1436 * BR_REPLY or BR_ERROR; in case of oneway
1437 * transactions, a BR_TRANSACTION_COMPLETE.
1438 */
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001439 binder_enqueue_work_ilocked(&node->work, target_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001440 }
1441 } else {
1442 if (!internal)
1443 node->local_weak_refs++;
1444 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
1445 if (target_list == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301446 pr_err("invalid inc weak node for %d\n",
1447 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001448 return -EINVAL;
1449 }
Martijn Coenen1af61802017-10-19 15:04:46 +02001450 /*
1451 * See comment above
1452 */
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001453 binder_enqueue_work_ilocked(&node->work, target_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001454 }
1455 }
1456 return 0;
1457}
1458
Todd Kjose7f23ed2017-03-21 13:06:01 -07001459static int binder_inc_node(struct binder_node *node, int strong, int internal,
1460 struct list_head *target_list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001461{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001462 int ret;
1463
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001464 binder_node_inner_lock(node);
1465 ret = binder_inc_node_nilocked(node, strong, internal, target_list);
1466 binder_node_inner_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001467
1468 return ret;
1469}
1470
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001471static bool binder_dec_node_nilocked(struct binder_node *node,
1472 int strong, int internal)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001473{
1474 struct binder_proc *proc = node->proc;
1475
Martijn Coenened323352017-07-27 23:52:24 +02001476 assert_spin_locked(&node->lock);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001477 if (proc)
Martijn Coenened323352017-07-27 23:52:24 +02001478 assert_spin_locked(&proc->inner_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001479 if (strong) {
1480 if (internal)
1481 node->internal_strong_refs--;
1482 else
1483 node->local_strong_refs--;
1484 if (node->local_strong_refs || node->internal_strong_refs)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001485 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001486 } else {
1487 if (!internal)
1488 node->local_weak_refs--;
Todd Kjosf22abc72017-05-09 11:08:05 -07001489 if (node->local_weak_refs || node->tmp_refs ||
1490 !hlist_empty(&node->refs))
Todd Kjose7f23ed2017-03-21 13:06:01 -07001491 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001492 }
Todd Kjose7f23ed2017-03-21 13:06:01 -07001493
1494 if (proc && (node->has_strong_ref || node->has_weak_ref)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001495 if (list_empty(&node->work.entry)) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001496 binder_enqueue_work_ilocked(&node->work, &proc->todo);
Martijn Coenen053be422017-06-06 15:17:46 -07001497 binder_wakeup_proc_ilocked(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001498 }
1499 } else {
1500 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
Todd Kjosf22abc72017-05-09 11:08:05 -07001501 !node->local_weak_refs && !node->tmp_refs) {
Todd Kjose7f23ed2017-03-21 13:06:01 -07001502 if (proc) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001503 binder_dequeue_work_ilocked(&node->work);
1504 rb_erase(&node->rb_node, &proc->nodes);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001505 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301506 "refless node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001507 node->debug_id);
1508 } else {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001509 BUG_ON(!list_empty(&node->work.entry));
Todd Kjos8d9f6f32016-10-17 12:33:15 -07001510 spin_lock(&binder_dead_nodes_lock);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001511 /*
1512 * tmp_refs could have changed so
1513 * check it again
1514 */
1515 if (node->tmp_refs) {
1516 spin_unlock(&binder_dead_nodes_lock);
1517 return false;
1518 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001519 hlist_del(&node->dead_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07001520 spin_unlock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001521 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301522 "dead node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001523 node->debug_id);
1524 }
Todd Kjose7f23ed2017-03-21 13:06:01 -07001525 return true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001526 }
1527 }
Todd Kjose7f23ed2017-03-21 13:06:01 -07001528 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001529}
1530
Todd Kjose7f23ed2017-03-21 13:06:01 -07001531static void binder_dec_node(struct binder_node *node, int strong, int internal)
1532{
1533 bool free_node;
1534
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001535 binder_node_inner_lock(node);
1536 free_node = binder_dec_node_nilocked(node, strong, internal);
1537 binder_node_inner_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001538 if (free_node)
1539 binder_free_node(node);
1540}
1541
1542static void binder_inc_node_tmpref_ilocked(struct binder_node *node)
Todd Kjosf22abc72017-05-09 11:08:05 -07001543{
1544 /*
1545 * No call to binder_inc_node() is needed since we
1546 * don't need to inform userspace of any changes to
1547 * tmp_refs
1548 */
1549 node->tmp_refs++;
1550}
1551
1552/**
Todd Kjose7f23ed2017-03-21 13:06:01 -07001553 * binder_inc_node_tmpref() - take a temporary reference on node
1554 * @node: node to reference
1555 *
1556 * Take reference on node to prevent the node from being freed
1557 * while referenced only by a local variable. The inner lock is
1558 * needed to serialize with the node work on the queue (which
1559 * isn't needed after the node is dead). If the node is dead
1560 * (node->proc is NULL), use binder_dead_nodes_lock to protect
1561 * node->tmp_refs against dead-node-only cases where the node
1562 * lock cannot be acquired (eg traversing the dead node list to
1563 * print nodes)
1564 */
1565static void binder_inc_node_tmpref(struct binder_node *node)
1566{
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001567 binder_node_lock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001568 if (node->proc)
1569 binder_inner_proc_lock(node->proc);
1570 else
1571 spin_lock(&binder_dead_nodes_lock);
1572 binder_inc_node_tmpref_ilocked(node);
1573 if (node->proc)
1574 binder_inner_proc_unlock(node->proc);
1575 else
1576 spin_unlock(&binder_dead_nodes_lock);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001577 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001578}
1579
1580/**
Todd Kjosf22abc72017-05-09 11:08:05 -07001581 * binder_dec_node_tmpref() - remove a temporary reference on node
1582 * @node: node to reference
1583 *
1584 * Release temporary reference on node taken via binder_inc_node_tmpref()
1585 */
1586static void binder_dec_node_tmpref(struct binder_node *node)
1587{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001588 bool free_node;
1589
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001590 binder_node_inner_lock(node);
1591 if (!node->proc)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001592 spin_lock(&binder_dead_nodes_lock);
Todd Kjosf22abc72017-05-09 11:08:05 -07001593 node->tmp_refs--;
1594 BUG_ON(node->tmp_refs < 0);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001595 if (!node->proc)
1596 spin_unlock(&binder_dead_nodes_lock);
Todd Kjosf22abc72017-05-09 11:08:05 -07001597 /*
1598 * Call binder_dec_node() to check if all refcounts are 0
1599 * and cleanup is needed. Calling with strong=0 and internal=1
1600 * causes no actual reference to be released in binder_dec_node().
1601 * If that changes, a change is needed here too.
1602 */
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001603 free_node = binder_dec_node_nilocked(node, 0, 1);
1604 binder_node_inner_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001605 if (free_node)
1606 binder_free_node(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07001607}
1608
1609static void binder_put_node(struct binder_node *node)
1610{
1611 binder_dec_node_tmpref(node);
1612}
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001613
Todd Kjos5346bf32016-10-20 16:43:34 -07001614static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc,
1615 u32 desc, bool need_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001616{
1617 struct rb_node *n = proc->refs_by_desc.rb_node;
1618 struct binder_ref *ref;
1619
1620 while (n) {
1621 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1622
Todd Kjosb0117bb2017-05-08 09:16:27 -07001623 if (desc < ref->data.desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001624 n = n->rb_left;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001625 } else if (desc > ref->data.desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001626 n = n->rb_right;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001627 } else if (need_strong_ref && !ref->data.strong) {
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001628 binder_user_error("tried to use weak ref as strong ref\n");
1629 return NULL;
1630 } else {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001631 return ref;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001632 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001633 }
1634 return NULL;
1635}
1636
Todd Kjosb0117bb2017-05-08 09:16:27 -07001637/**
Todd Kjos5346bf32016-10-20 16:43:34 -07001638 * binder_get_ref_for_node_olocked() - get the ref associated with given node
Todd Kjosb0117bb2017-05-08 09:16:27 -07001639 * @proc: binder_proc that owns the ref
1640 * @node: binder_node of target
1641 * @new_ref: newly allocated binder_ref to be initialized or %NULL
1642 *
1643 * Look up the ref for the given node and return it if it exists
1644 *
1645 * If it doesn't exist and the caller provides a newly allocated
1646 * ref, initialize the fields of the newly allocated ref and insert
1647 * into the given proc rb_trees and node refs list.
1648 *
1649 * Return: the ref for node. It is possible that another thread
1650 * allocated/initialized the ref first in which case the
1651 * returned ref would be different than the passed-in
1652 * new_ref. new_ref must be kfree'd by the caller in
1653 * this case.
1654 */
Todd Kjos5346bf32016-10-20 16:43:34 -07001655static struct binder_ref *binder_get_ref_for_node_olocked(
1656 struct binder_proc *proc,
1657 struct binder_node *node,
1658 struct binder_ref *new_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001659{
Todd Kjosb0117bb2017-05-08 09:16:27 -07001660 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001661 struct rb_node **p = &proc->refs_by_node.rb_node;
1662 struct rb_node *parent = NULL;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001663 struct binder_ref *ref;
1664 struct rb_node *n;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001665
1666 while (*p) {
1667 parent = *p;
1668 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1669
1670 if (node < ref->node)
1671 p = &(*p)->rb_left;
1672 else if (node > ref->node)
1673 p = &(*p)->rb_right;
1674 else
1675 return ref;
1676 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001677 if (!new_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001678 return NULL;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001679
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001680 binder_stats_created(BINDER_STAT_REF);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001681 new_ref->data.debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001682 new_ref->proc = proc;
1683 new_ref->node = node;
1684 rb_link_node(&new_ref->rb_node_node, parent, p);
1685 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1686
Todd Kjosb0117bb2017-05-08 09:16:27 -07001687 new_ref->data.desc = (node == context->binder_context_mgr_node) ? 0 : 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001688 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1689 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001690 if (ref->data.desc > new_ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001691 break;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001692 new_ref->data.desc = ref->data.desc + 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001693 }
1694
1695 p = &proc->refs_by_desc.rb_node;
1696 while (*p) {
1697 parent = *p;
1698 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1699
Todd Kjosb0117bb2017-05-08 09:16:27 -07001700 if (new_ref->data.desc < ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001701 p = &(*p)->rb_left;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001702 else if (new_ref->data.desc > ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001703 p = &(*p)->rb_right;
1704 else
1705 BUG();
1706 }
1707 rb_link_node(&new_ref->rb_node_desc, parent, p);
1708 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001709
1710 binder_node_lock(node);
Todd Kjos4cbe5752017-05-01 17:21:51 -07001711 hlist_add_head(&new_ref->node_entry, &node->refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001712
Todd Kjos4cbe5752017-05-01 17:21:51 -07001713 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1714 "%d new ref %d desc %d for node %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001715 proc->pid, new_ref->data.debug_id, new_ref->data.desc,
Todd Kjos4cbe5752017-05-01 17:21:51 -07001716 node->debug_id);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001717 binder_node_unlock(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001718 return new_ref;
1719}
1720
Todd Kjos5346bf32016-10-20 16:43:34 -07001721static void binder_cleanup_ref_olocked(struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001722{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001723 bool delete_node = false;
Todd Kjose7f23ed2017-03-21 13:06:01 -07001724
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001725 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301726 "%d delete ref %d desc %d for node %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001727 ref->proc->pid, ref->data.debug_id, ref->data.desc,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301728 ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001729
1730 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1731 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001732
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001733 binder_node_inner_lock(ref->node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001734 if (ref->data.strong)
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001735 binder_dec_node_nilocked(ref->node, 1, 1);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001736
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001737 hlist_del(&ref->node_entry);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001738 delete_node = binder_dec_node_nilocked(ref->node, 0, 1);
1739 binder_node_inner_unlock(ref->node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001740 /*
1741 * Clear ref->node unless we want the caller to free the node
1742 */
1743 if (!delete_node) {
1744 /*
1745 * The caller uses ref->node to determine
1746 * whether the node needs to be freed. Clear
1747 * it since the node is still alive.
1748 */
1749 ref->node = NULL;
1750 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001751
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001752 if (ref->death) {
1753 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301754 "%d delete ref %d desc %d has death notification\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001755 ref->proc->pid, ref->data.debug_id,
1756 ref->data.desc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001757 binder_dequeue_work(ref->proc, &ref->death->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001758 binder_stats_deleted(BINDER_STAT_DEATH);
1759 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001760 binder_stats_deleted(BINDER_STAT_REF);
1761}
1762
Todd Kjosb0117bb2017-05-08 09:16:27 -07001763/**
Todd Kjos5346bf32016-10-20 16:43:34 -07001764 * binder_inc_ref_olocked() - increment the ref for given handle
Todd Kjosb0117bb2017-05-08 09:16:27 -07001765 * @ref: ref to be incremented
1766 * @strong: if true, strong increment, else weak
1767 * @target_list: list to queue node work on
1768 *
Todd Kjos5346bf32016-10-20 16:43:34 -07001769 * Increment the ref. @ref->proc->outer_lock must be held on entry
Todd Kjosb0117bb2017-05-08 09:16:27 -07001770 *
1771 * Return: 0, if successful, else errno
1772 */
Todd Kjos5346bf32016-10-20 16:43:34 -07001773static int binder_inc_ref_olocked(struct binder_ref *ref, int strong,
1774 struct list_head *target_list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001775{
1776 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +09001777
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001778 if (strong) {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001779 if (ref->data.strong == 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001780 ret = binder_inc_node(ref->node, 1, 1, target_list);
1781 if (ret)
1782 return ret;
1783 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001784 ref->data.strong++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001785 } else {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001786 if (ref->data.weak == 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001787 ret = binder_inc_node(ref->node, 0, 1, target_list);
1788 if (ret)
1789 return ret;
1790 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001791 ref->data.weak++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001792 }
1793 return 0;
1794}
1795
Todd Kjosb0117bb2017-05-08 09:16:27 -07001796/**
1797 * binder_dec_ref() - dec the ref for given handle
1798 * @ref: ref to be decremented
1799 * @strong: if true, strong decrement, else weak
1800 *
1801 * Decrement the ref.
1802 *
Todd Kjosb0117bb2017-05-08 09:16:27 -07001803 * Return: true if ref is cleaned up and ready to be freed
1804 */
Todd Kjos5346bf32016-10-20 16:43:34 -07001805static bool binder_dec_ref_olocked(struct binder_ref *ref, int strong)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001806{
1807 if (strong) {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001808 if (ref->data.strong == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301809 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001810 ref->proc->pid, ref->data.debug_id,
1811 ref->data.desc, ref->data.strong,
1812 ref->data.weak);
1813 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001814 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001815 ref->data.strong--;
Todd Kjose7f23ed2017-03-21 13:06:01 -07001816 if (ref->data.strong == 0)
1817 binder_dec_node(ref->node, strong, 1);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001818 } else {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001819 if (ref->data.weak == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301820 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001821 ref->proc->pid, ref->data.debug_id,
1822 ref->data.desc, ref->data.strong,
1823 ref->data.weak);
1824 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001825 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001826 ref->data.weak--;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001827 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001828 if (ref->data.strong == 0 && ref->data.weak == 0) {
Todd Kjos5346bf32016-10-20 16:43:34 -07001829 binder_cleanup_ref_olocked(ref);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001830 return true;
1831 }
1832 return false;
1833}
1834
1835/**
1836 * binder_get_node_from_ref() - get the node from the given proc/desc
1837 * @proc: proc containing the ref
1838 * @desc: the handle associated with the ref
1839 * @need_strong_ref: if true, only return node if ref is strong
1840 * @rdata: the id/refcount data for the ref
1841 *
1842 * Given a proc and ref handle, return the associated binder_node
1843 *
1844 * Return: a binder_node or NULL if not found or not strong when strong required
1845 */
1846static struct binder_node *binder_get_node_from_ref(
1847 struct binder_proc *proc,
1848 u32 desc, bool need_strong_ref,
1849 struct binder_ref_data *rdata)
1850{
1851 struct binder_node *node;
1852 struct binder_ref *ref;
1853
Todd Kjos5346bf32016-10-20 16:43:34 -07001854 binder_proc_lock(proc);
1855 ref = binder_get_ref_olocked(proc, desc, need_strong_ref);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001856 if (!ref)
1857 goto err_no_ref;
1858 node = ref->node;
Todd Kjosf22abc72017-05-09 11:08:05 -07001859 /*
1860 * Take an implicit reference on the node to ensure
1861 * it stays alive until the call to binder_put_node()
1862 */
1863 binder_inc_node_tmpref(node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001864 if (rdata)
1865 *rdata = ref->data;
Todd Kjos5346bf32016-10-20 16:43:34 -07001866 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001867
1868 return node;
1869
1870err_no_ref:
Todd Kjos5346bf32016-10-20 16:43:34 -07001871 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001872 return NULL;
1873}
1874
1875/**
1876 * binder_free_ref() - free the binder_ref
1877 * @ref: ref to free
1878 *
Todd Kjose7f23ed2017-03-21 13:06:01 -07001879 * Free the binder_ref. Free the binder_node indicated by ref->node
1880 * (if non-NULL) and the binder_ref_death indicated by ref->death.
Todd Kjosb0117bb2017-05-08 09:16:27 -07001881 */
1882static void binder_free_ref(struct binder_ref *ref)
1883{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001884 if (ref->node)
1885 binder_free_node(ref->node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001886 kfree(ref->death);
1887 kfree(ref);
1888}
1889
1890/**
1891 * binder_update_ref_for_handle() - inc/dec the ref for given handle
1892 * @proc: proc containing the ref
1893 * @desc: the handle associated with the ref
1894 * @increment: true=inc reference, false=dec reference
1895 * @strong: true=strong reference, false=weak reference
1896 * @rdata: the id/refcount data for the ref
1897 *
1898 * Given a proc and ref handle, increment or decrement the ref
1899 * according to "increment" arg.
1900 *
1901 * Return: 0 if successful, else errno
1902 */
1903static int binder_update_ref_for_handle(struct binder_proc *proc,
1904 uint32_t desc, bool increment, bool strong,
1905 struct binder_ref_data *rdata)
1906{
1907 int ret = 0;
1908 struct binder_ref *ref;
1909 bool delete_ref = false;
1910
Todd Kjos5346bf32016-10-20 16:43:34 -07001911 binder_proc_lock(proc);
1912 ref = binder_get_ref_olocked(proc, desc, strong);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001913 if (!ref) {
1914 ret = -EINVAL;
1915 goto err_no_ref;
1916 }
1917 if (increment)
Todd Kjos5346bf32016-10-20 16:43:34 -07001918 ret = binder_inc_ref_olocked(ref, strong, NULL);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001919 else
Todd Kjos5346bf32016-10-20 16:43:34 -07001920 delete_ref = binder_dec_ref_olocked(ref, strong);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001921
1922 if (rdata)
1923 *rdata = ref->data;
Todd Kjos5346bf32016-10-20 16:43:34 -07001924 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001925
1926 if (delete_ref)
1927 binder_free_ref(ref);
1928 return ret;
1929
1930err_no_ref:
Todd Kjos5346bf32016-10-20 16:43:34 -07001931 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001932 return ret;
1933}
1934
1935/**
1936 * binder_dec_ref_for_handle() - dec the ref for given handle
1937 * @proc: proc containing the ref
1938 * @desc: the handle associated with the ref
1939 * @strong: true=strong reference, false=weak reference
1940 * @rdata: the id/refcount data for the ref
1941 *
1942 * Just calls binder_update_ref_for_handle() to decrement the ref.
1943 *
1944 * Return: 0 if successful, else errno
1945 */
1946static int binder_dec_ref_for_handle(struct binder_proc *proc,
1947 uint32_t desc, bool strong, struct binder_ref_data *rdata)
1948{
1949 return binder_update_ref_for_handle(proc, desc, false, strong, rdata);
1950}
1951
1952
1953/**
1954 * binder_inc_ref_for_node() - increment the ref for given proc/node
1955 * @proc: proc containing the ref
1956 * @node: target node
1957 * @strong: true=strong reference, false=weak reference
1958 * @target_list: worklist to use if node is incremented
1959 * @rdata: the id/refcount data for the ref
1960 *
1961 * Given a proc and node, increment the ref. Create the ref if it
1962 * doesn't already exist
1963 *
1964 * Return: 0 if successful, else errno
1965 */
1966static int binder_inc_ref_for_node(struct binder_proc *proc,
1967 struct binder_node *node,
1968 bool strong,
1969 struct list_head *target_list,
1970 struct binder_ref_data *rdata)
1971{
1972 struct binder_ref *ref;
1973 struct binder_ref *new_ref = NULL;
1974 int ret = 0;
1975
Todd Kjos5346bf32016-10-20 16:43:34 -07001976 binder_proc_lock(proc);
1977 ref = binder_get_ref_for_node_olocked(proc, node, NULL);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001978 if (!ref) {
Todd Kjos5346bf32016-10-20 16:43:34 -07001979 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001980 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1981 if (!new_ref)
1982 return -ENOMEM;
Todd Kjos5346bf32016-10-20 16:43:34 -07001983 binder_proc_lock(proc);
1984 ref = binder_get_ref_for_node_olocked(proc, node, new_ref);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001985 }
Todd Kjos5346bf32016-10-20 16:43:34 -07001986 ret = binder_inc_ref_olocked(ref, strong, target_list);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001987 *rdata = ref->data;
Todd Kjos5346bf32016-10-20 16:43:34 -07001988 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001989 if (new_ref && ref != new_ref)
1990 /*
1991 * Another thread created the ref first so
1992 * free the one we allocated
1993 */
1994 kfree(new_ref);
1995 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001996}
1997
Martijn Coenen995a36e2017-06-02 13:36:52 -07001998static void binder_pop_transaction_ilocked(struct binder_thread *target_thread,
1999 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002000{
Todd Kjos21ef40a2017-03-30 18:02:13 -07002001 BUG_ON(!target_thread);
Martijn Coenened323352017-07-27 23:52:24 +02002002 assert_spin_locked(&target_thread->proc->inner_lock);
Todd Kjos21ef40a2017-03-30 18:02:13 -07002003 BUG_ON(target_thread->transaction_stack != t);
2004 BUG_ON(target_thread->transaction_stack->from != target_thread);
2005 target_thread->transaction_stack =
2006 target_thread->transaction_stack->from_parent;
2007 t->from = NULL;
2008}
2009
Todd Kjos2f993e22017-05-12 14:42:55 -07002010/**
2011 * binder_thread_dec_tmpref() - decrement thread->tmp_ref
2012 * @thread: thread to decrement
2013 *
2014 * A thread needs to be kept alive while being used to create or
2015 * handle a transaction. binder_get_txn_from() is used to safely
2016 * extract t->from from a binder_transaction and keep the thread
2017 * indicated by t->from from being freed. When done with that
2018 * binder_thread, this function is called to decrement the
2019 * tmp_ref and free if appropriate (thread has been released
2020 * and no transaction being processed by the driver)
2021 */
2022static void binder_thread_dec_tmpref(struct binder_thread *thread)
2023{
2024 /*
2025 * atomic is used to protect the counter value while
2026 * it cannot reach zero or thread->is_dead is false
Todd Kjos2f993e22017-05-12 14:42:55 -07002027 */
Todd Kjosb4827902017-05-25 15:52:17 -07002028 binder_inner_proc_lock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002029 atomic_dec(&thread->tmp_ref);
2030 if (thread->is_dead && !atomic_read(&thread->tmp_ref)) {
Todd Kjosb4827902017-05-25 15:52:17 -07002031 binder_inner_proc_unlock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002032 binder_free_thread(thread);
2033 return;
2034 }
Todd Kjosb4827902017-05-25 15:52:17 -07002035 binder_inner_proc_unlock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002036}
2037
2038/**
2039 * binder_proc_dec_tmpref() - decrement proc->tmp_ref
2040 * @proc: proc to decrement
2041 *
2042 * A binder_proc needs to be kept alive while being used to create or
2043 * handle a transaction. proc->tmp_ref is incremented when
2044 * creating a new transaction or the binder_proc is currently in-use
2045 * by threads that are being released. When done with the binder_proc,
2046 * this function is called to decrement the counter and free the
2047 * proc if appropriate (proc has been released, all threads have
2048 * been released and not currenly in-use to process a transaction).
2049 */
2050static void binder_proc_dec_tmpref(struct binder_proc *proc)
2051{
Todd Kjosb4827902017-05-25 15:52:17 -07002052 binder_inner_proc_lock(proc);
Todd Kjosf4e6de32019-06-10 09:14:25 -07002053 atomic_dec(&proc->tmp_ref);
Todd Kjos2f993e22017-05-12 14:42:55 -07002054 if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) &&
Todd Kjosf4e6de32019-06-10 09:14:25 -07002055 !atomic_read(&proc->tmp_ref)) {
Todd Kjosb4827902017-05-25 15:52:17 -07002056 binder_inner_proc_unlock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002057 binder_free_proc(proc);
2058 return;
2059 }
Todd Kjosb4827902017-05-25 15:52:17 -07002060 binder_inner_proc_unlock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002061}
2062
2063/**
2064 * binder_get_txn_from() - safely extract the "from" thread in transaction
2065 * @t: binder transaction for t->from
2066 *
2067 * Atomically return the "from" thread and increment the tmp_ref
2068 * count for the thread to ensure it stays alive until
2069 * binder_thread_dec_tmpref() is called.
2070 *
2071 * Return: the value of t->from
2072 */
2073static struct binder_thread *binder_get_txn_from(
2074 struct binder_transaction *t)
2075{
2076 struct binder_thread *from;
2077
2078 spin_lock(&t->lock);
2079 from = t->from;
2080 if (from)
2081 atomic_inc(&from->tmp_ref);
2082 spin_unlock(&t->lock);
2083 return from;
2084}
2085
Martijn Coenen995a36e2017-06-02 13:36:52 -07002086/**
2087 * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock
2088 * @t: binder transaction for t->from
2089 *
2090 * Same as binder_get_txn_from() except it also acquires the proc->inner_lock
2091 * to guarantee that the thread cannot be released while operating on it.
2092 * The caller must call binder_inner_proc_unlock() to release the inner lock
2093 * as well as call binder_dec_thread_txn() to release the reference.
2094 *
2095 * Return: the value of t->from
2096 */
2097static struct binder_thread *binder_get_txn_from_and_acq_inner(
2098 struct binder_transaction *t)
2099{
2100 struct binder_thread *from;
2101
2102 from = binder_get_txn_from(t);
2103 if (!from)
2104 return NULL;
2105 binder_inner_proc_lock(from->proc);
2106 if (t->from) {
2107 BUG_ON(from != t->from);
2108 return from;
2109 }
2110 binder_inner_proc_unlock(from->proc);
2111 binder_thread_dec_tmpref(from);
2112 return NULL;
2113}
2114
Todd Kjos21ef40a2017-03-30 18:02:13 -07002115static void binder_free_transaction(struct binder_transaction *t)
2116{
Todd Kjosf4e6de32019-06-10 09:14:25 -07002117 struct binder_proc *target_proc;
2118
2119 spin_lock(&t->lock);
2120 target_proc = t->to_proc;
2121 if (target_proc) {
2122 atomic_inc(&target_proc->tmp_ref);
2123 spin_unlock(&t->lock);
2124
2125 binder_inner_proc_lock(target_proc);
2126 if (t->buffer)
2127 t->buffer->transaction = NULL;
2128 binder_inner_proc_unlock(target_proc);
2129 binder_proc_dec_tmpref(target_proc);
2130 } else {
2131 /*
2132 * If the transaction has no target_proc, then
2133 * t->buffer->transaction * has already been cleared.
2134 */
2135 spin_unlock(&t->lock);
2136 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002137 kfree(t);
2138 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2139}
2140
2141static void binder_send_failed_reply(struct binder_transaction *t,
2142 uint32_t error_code)
2143{
2144 struct binder_thread *target_thread;
Lucas Tanured4ec15e2014-07-13 21:31:05 -03002145 struct binder_transaction *next;
Seunghun Lee10f62862014-05-01 01:30:23 +09002146
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002147 BUG_ON(t->flags & TF_ONE_WAY);
2148 while (1) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07002149 target_thread = binder_get_txn_from_and_acq_inner(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002150 if (target_thread) {
Todd Kjos858b8da2017-04-21 17:35:12 -07002151 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2152 "send failed reply for transaction %d to %d:%d\n",
2153 t->debug_id,
2154 target_thread->proc->pid,
2155 target_thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002156
Martijn Coenen995a36e2017-06-02 13:36:52 -07002157 binder_pop_transaction_ilocked(target_thread, t);
Todd Kjos858b8da2017-04-21 17:35:12 -07002158 if (target_thread->reply_error.cmd == BR_OK) {
2159 target_thread->reply_error.cmd = error_code;
Martijn Coenen1af61802017-10-19 15:04:46 +02002160 binder_enqueue_thread_work_ilocked(
2161 target_thread,
2162 &target_thread->reply_error.work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002163 wake_up_interruptible(&target_thread->wait);
2164 } else {
Todd Kjosd3a2afb2018-02-07 12:38:47 -08002165 /*
2166 * Cannot get here for normal operation, but
2167 * we can if multiple synchronous transactions
2168 * are sent without blocking for responses.
2169 * Just ignore the 2nd error in this case.
2170 */
2171 pr_warn("Unexpected reply error: %u\n",
2172 target_thread->reply_error.cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002173 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07002174 binder_inner_proc_unlock(target_thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002175 binder_thread_dec_tmpref(target_thread);
Todd Kjos858b8da2017-04-21 17:35:12 -07002176 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002177 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002178 }
Lucas Tanured4ec15e2014-07-13 21:31:05 -03002179 next = t->from_parent;
2180
2181 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2182 "send failed reply for transaction %d, target dead\n",
2183 t->debug_id);
2184
Todd Kjos21ef40a2017-03-30 18:02:13 -07002185 binder_free_transaction(t);
Lucas Tanured4ec15e2014-07-13 21:31:05 -03002186 if (next == NULL) {
2187 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2188 "reply failed, no target thread at root\n");
2189 return;
2190 }
2191 t = next;
2192 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2193 "reply failed, no target thread -- retry %d\n",
2194 t->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002195 }
2196}
2197
Martijn Coenen00c80372016-07-13 12:06:49 +02002198/**
Martijn Coenen3217ccc2017-08-24 15:23:36 +02002199 * binder_cleanup_transaction() - cleans up undelivered transaction
2200 * @t: transaction that needs to be cleaned up
2201 * @reason: reason the transaction wasn't delivered
2202 * @error_code: error to return to caller (if synchronous call)
2203 */
2204static void binder_cleanup_transaction(struct binder_transaction *t,
2205 const char *reason,
2206 uint32_t error_code)
2207{
2208 if (t->buffer->target_node && !(t->flags & TF_ONE_WAY)) {
2209 binder_send_failed_reply(t, error_code);
2210 } else {
2211 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2212 "undelivered transaction %d, %s\n",
2213 t->debug_id, reason);
2214 binder_free_transaction(t);
2215 }
2216}
2217
2218/**
Todd Kjosd73356a2019-02-08 10:35:16 -08002219 * binder_get_object() - gets object and checks for valid metadata
2220 * @proc: binder_proc owning the buffer
Martijn Coenen00c80372016-07-13 12:06:49 +02002221 * @buffer: binder_buffer that we're parsing.
Todd Kjosd73356a2019-02-08 10:35:16 -08002222 * @offset: offset in the @buffer at which to validate an object.
2223 * @object: struct binder_object to read into
Martijn Coenen00c80372016-07-13 12:06:49 +02002224 *
2225 * Return: If there's a valid metadata object at @offset in @buffer, the
Todd Kjosd73356a2019-02-08 10:35:16 -08002226 * size of that object. Otherwise, it returns zero. The object
2227 * is read into the struct binder_object pointed to by @object.
Martijn Coenen00c80372016-07-13 12:06:49 +02002228 */
Todd Kjosd73356a2019-02-08 10:35:16 -08002229static size_t binder_get_object(struct binder_proc *proc,
2230 struct binder_buffer *buffer,
2231 unsigned long offset,
2232 struct binder_object *object)
Martijn Coenen00c80372016-07-13 12:06:49 +02002233{
Todd Kjosd73356a2019-02-08 10:35:16 -08002234 size_t read_size;
Martijn Coenen00c80372016-07-13 12:06:49 +02002235 struct binder_object_header *hdr;
2236 size_t object_size = 0;
2237
Todd Kjosd73356a2019-02-08 10:35:16 -08002238 read_size = min_t(size_t, sizeof(*object), buffer->data_size - offset);
Todd Kjosc8ddc8c2019-03-19 09:53:01 -07002239 if (offset > buffer->data_size || read_size < sizeof(*hdr) ||
Martijn Coenen00c80372016-07-13 12:06:49 +02002240 !IS_ALIGNED(offset, sizeof(u32)))
2241 return 0;
Todd Kjosd73356a2019-02-08 10:35:16 -08002242 binder_alloc_copy_from_buffer(&proc->alloc, object, buffer,
2243 offset, read_size);
Martijn Coenen00c80372016-07-13 12:06:49 +02002244
Todd Kjosd73356a2019-02-08 10:35:16 -08002245 /* Ok, now see if we read a complete object. */
2246 hdr = &object->hdr;
Martijn Coenen00c80372016-07-13 12:06:49 +02002247 switch (hdr->type) {
2248 case BINDER_TYPE_BINDER:
2249 case BINDER_TYPE_WEAK_BINDER:
2250 case BINDER_TYPE_HANDLE:
2251 case BINDER_TYPE_WEAK_HANDLE:
2252 object_size = sizeof(struct flat_binder_object);
2253 break;
2254 case BINDER_TYPE_FD:
2255 object_size = sizeof(struct binder_fd_object);
2256 break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002257 case BINDER_TYPE_PTR:
2258 object_size = sizeof(struct binder_buffer_object);
2259 break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002260 case BINDER_TYPE_FDA:
2261 object_size = sizeof(struct binder_fd_array_object);
2262 break;
Martijn Coenen00c80372016-07-13 12:06:49 +02002263 default:
2264 return 0;
2265 }
2266 if (offset <= buffer->data_size - object_size &&
2267 buffer->data_size >= object_size)
2268 return object_size;
2269 else
2270 return 0;
2271}
2272
Martijn Coenen5a6da532016-09-30 14:10:07 +02002273/**
2274 * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002275 * @proc: binder_proc owning the buffer
Martijn Coenen5a6da532016-09-30 14:10:07 +02002276 * @b: binder_buffer containing the object
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002277 * @object: struct binder_object to read into
Martijn Coenen5a6da532016-09-30 14:10:07 +02002278 * @index: index in offset array at which the binder_buffer_object is
2279 * located
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002280 * @start_offset: points to the start of the offset array
2281 * @object_offsetp: offset of @object read from @b
Martijn Coenen5a6da532016-09-30 14:10:07 +02002282 * @num_valid: the number of valid offsets in the offset array
2283 *
2284 * Return: If @index is within the valid range of the offset array
2285 * described by @start and @num_valid, and if there's a valid
2286 * binder_buffer_object at the offset found in index @index
2287 * of the offset array, that object is returned. Otherwise,
2288 * %NULL is returned.
2289 * Note that the offset found in index @index itself is not
2290 * verified; this function assumes that @num_valid elements
2291 * from @start were previously verified to have valid offsets.
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002292 * If @object_offsetp is non-NULL, then the offset within
2293 * @b is written to it.
Martijn Coenen5a6da532016-09-30 14:10:07 +02002294 */
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002295static struct binder_buffer_object *binder_validate_ptr(
2296 struct binder_proc *proc,
2297 struct binder_buffer *b,
2298 struct binder_object *object,
2299 binder_size_t index,
2300 binder_size_t start_offset,
2301 binder_size_t *object_offsetp,
2302 binder_size_t num_valid)
Martijn Coenen5a6da532016-09-30 14:10:07 +02002303{
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002304 size_t object_size;
2305 binder_size_t object_offset;
2306 unsigned long buffer_offset;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002307
2308 if (index >= num_valid)
2309 return NULL;
2310
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002311 buffer_offset = start_offset + sizeof(binder_size_t) * index;
2312 binder_alloc_copy_from_buffer(&proc->alloc, &object_offset,
2313 b, buffer_offset, sizeof(object_offset));
2314 object_size = binder_get_object(proc, b, object_offset, object);
2315 if (!object_size || object->hdr.type != BINDER_TYPE_PTR)
Martijn Coenen5a6da532016-09-30 14:10:07 +02002316 return NULL;
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002317 if (object_offsetp)
2318 *object_offsetp = object_offset;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002319
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002320 return &object->bbo;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002321}
2322
2323/**
2324 * binder_validate_fixup() - validates pointer/fd fixups happen in order.
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002325 * @proc: binder_proc owning the buffer
Martijn Coenen5a6da532016-09-30 14:10:07 +02002326 * @b: transaction buffer
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002327 * @objects_start_offset: offset to start of objects buffer
2328 * @buffer_obj_offset: offset to binder_buffer_object in which to fix up
2329 * @fixup_offset: start offset in @buffer to fix up
2330 * @last_obj_offset: offset to last binder_buffer_object that we fixed
2331 * @last_min_offset: minimum fixup offset in object at @last_obj_offset
Martijn Coenen5a6da532016-09-30 14:10:07 +02002332 *
2333 * Return: %true if a fixup in buffer @buffer at offset @offset is
2334 * allowed.
2335 *
2336 * For safety reasons, we only allow fixups inside a buffer to happen
2337 * at increasing offsets; additionally, we only allow fixup on the last
2338 * buffer object that was verified, or one of its parents.
2339 *
2340 * Example of what is allowed:
2341 *
2342 * A
2343 * B (parent = A, offset = 0)
2344 * C (parent = A, offset = 16)
2345 * D (parent = C, offset = 0)
2346 * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
2347 *
2348 * Examples of what is not allowed:
2349 *
2350 * Decreasing offsets within the same parent:
2351 * A
2352 * C (parent = A, offset = 16)
2353 * B (parent = A, offset = 0) // decreasing offset within A
2354 *
2355 * Referring to a parent that wasn't the last object or any of its parents:
2356 * A
2357 * B (parent = A, offset = 0)
2358 * C (parent = A, offset = 0)
2359 * C (parent = A, offset = 16)
2360 * D (parent = B, offset = 0) // B is not A or any of A's parents
2361 */
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002362static bool binder_validate_fixup(struct binder_proc *proc,
2363 struct binder_buffer *b,
2364 binder_size_t objects_start_offset,
2365 binder_size_t buffer_obj_offset,
Martijn Coenen5a6da532016-09-30 14:10:07 +02002366 binder_size_t fixup_offset,
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002367 binder_size_t last_obj_offset,
Martijn Coenen5a6da532016-09-30 14:10:07 +02002368 binder_size_t last_min_offset)
2369{
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002370 if (!last_obj_offset) {
Martijn Coenen5a6da532016-09-30 14:10:07 +02002371 /* Nothing to fix up in */
2372 return false;
2373 }
2374
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002375 while (last_obj_offset != buffer_obj_offset) {
2376 unsigned long buffer_offset;
2377 struct binder_object last_object;
2378 struct binder_buffer_object *last_bbo;
2379 size_t object_size = binder_get_object(proc, b, last_obj_offset,
2380 &last_object);
2381 if (object_size != sizeof(*last_bbo))
2382 return false;
2383
2384 last_bbo = &last_object.bbo;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002385 /*
2386 * Safe to retrieve the parent of last_obj, since it
2387 * was already previously verified by the driver.
2388 */
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002389 if ((last_bbo->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0)
Martijn Coenen5a6da532016-09-30 14:10:07 +02002390 return false;
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002391 last_min_offset = last_bbo->parent_offset + sizeof(uintptr_t);
2392 buffer_offset = objects_start_offset +
2393 sizeof(binder_size_t) * last_bbo->parent,
2394 binder_alloc_copy_from_buffer(&proc->alloc, &last_obj_offset,
2395 b, buffer_offset,
2396 sizeof(last_obj_offset));
Martijn Coenen5a6da532016-09-30 14:10:07 +02002397 }
2398 return (fixup_offset >= last_min_offset);
2399}
2400
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002401static void binder_transaction_buffer_release(struct binder_proc *proc,
2402 struct binder_buffer *buffer,
Todd Kjos8539b1e2019-02-08 10:35:20 -08002403 binder_size_t failed_at,
2404 bool is_failure)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002405{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002406 int debug_id = buffer->debug_id;
Todd Kjos8539b1e2019-02-08 10:35:20 -08002407 binder_size_t off_start_offset, buffer_offset, off_end_offset;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002408
2409 binder_debug(BINDER_DEBUG_TRANSACTION,
Todd Kjos8539b1e2019-02-08 10:35:20 -08002410 "%d buffer release %d, size %zd-%zd, failed at %llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002411 proc->pid, buffer->debug_id,
Todd Kjos8539b1e2019-02-08 10:35:20 -08002412 buffer->data_size, buffer->offsets_size,
2413 (unsigned long long)failed_at);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002414
2415 if (buffer->target_node)
2416 binder_dec_node(buffer->target_node, 1, 0);
2417
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002418 off_start_offset = ALIGN(buffer->data_size, sizeof(void *));
Todd Kjos8539b1e2019-02-08 10:35:20 -08002419 off_end_offset = is_failure ? failed_at :
2420 off_start_offset + buffer->offsets_size;
2421 for (buffer_offset = off_start_offset; buffer_offset < off_end_offset;
2422 buffer_offset += sizeof(binder_size_t)) {
Martijn Coenen00c80372016-07-13 12:06:49 +02002423 struct binder_object_header *hdr;
Todd Kjos90a570c2019-02-08 10:35:15 -08002424 size_t object_size;
Todd Kjosd73356a2019-02-08 10:35:16 -08002425 struct binder_object object;
Todd Kjos90a570c2019-02-08 10:35:15 -08002426 binder_size_t object_offset;
Seunghun Lee10f62862014-05-01 01:30:23 +09002427
Todd Kjos90a570c2019-02-08 10:35:15 -08002428 binder_alloc_copy_from_buffer(&proc->alloc, &object_offset,
2429 buffer, buffer_offset,
2430 sizeof(object_offset));
Todd Kjosd73356a2019-02-08 10:35:16 -08002431 object_size = binder_get_object(proc, buffer,
2432 object_offset, &object);
Martijn Coenen00c80372016-07-13 12:06:49 +02002433 if (object_size == 0) {
2434 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
Todd Kjos90a570c2019-02-08 10:35:15 -08002435 debug_id, (u64)object_offset, buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002436 continue;
2437 }
Todd Kjosd73356a2019-02-08 10:35:16 -08002438 hdr = &object.hdr;
Martijn Coenen00c80372016-07-13 12:06:49 +02002439 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002440 case BINDER_TYPE_BINDER:
2441 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenen00c80372016-07-13 12:06:49 +02002442 struct flat_binder_object *fp;
2443 struct binder_node *node;
Seunghun Lee10f62862014-05-01 01:30:23 +09002444
Martijn Coenen00c80372016-07-13 12:06:49 +02002445 fp = to_flat_binder_object(hdr);
2446 node = binder_get_node(proc, fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002447 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002448 pr_err("transaction release %d bad node %016llx\n",
2449 debug_id, (u64)fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002450 break;
2451 }
2452 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002453 " node %d u%016llx\n",
2454 node->debug_id, (u64)node->ptr);
Martijn Coenen00c80372016-07-13 12:06:49 +02002455 binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER,
2456 0);
Todd Kjosf22abc72017-05-09 11:08:05 -07002457 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002458 } break;
2459 case BINDER_TYPE_HANDLE:
2460 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenen00c80372016-07-13 12:06:49 +02002461 struct flat_binder_object *fp;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002462 struct binder_ref_data rdata;
2463 int ret;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02002464
Martijn Coenen00c80372016-07-13 12:06:49 +02002465 fp = to_flat_binder_object(hdr);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002466 ret = binder_dec_ref_for_handle(proc, fp->handle,
2467 hdr->type == BINDER_TYPE_HANDLE, &rdata);
2468
2469 if (ret) {
2470 pr_err("transaction release %d bad handle %d, ret = %d\n",
2471 debug_id, fp->handle, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002472 break;
2473 }
2474 binder_debug(BINDER_DEBUG_TRANSACTION,
Todd Kjosb0117bb2017-05-08 09:16:27 -07002475 " ref %d desc %d\n",
2476 rdata.debug_id, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002477 } break;
2478
Martijn Coenen00c80372016-07-13 12:06:49 +02002479 case BINDER_TYPE_FD: {
2480 struct binder_fd_object *fp = to_binder_fd_object(hdr);
2481
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002482 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen00c80372016-07-13 12:06:49 +02002483 " fd %d\n", fp->fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002484 if (failed_at)
Martijn Coenen00c80372016-07-13 12:06:49 +02002485 task_close_fd(proc, fp->fd);
2486 } break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002487 case BINDER_TYPE_PTR:
2488 /*
2489 * Nothing to do here, this will get cleaned up when the
2490 * transaction buffer gets freed
2491 */
2492 break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002493 case BINDER_TYPE_FDA: {
2494 struct binder_fd_array_object *fda;
2495 struct binder_buffer_object *parent;
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002496 struct binder_object ptr_object;
Todd Kjos8539b1e2019-02-08 10:35:20 -08002497 binder_size_t fda_offset;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002498 size_t fd_index;
2499 binder_size_t fd_buf_size;
Todd Kjos8539b1e2019-02-08 10:35:20 -08002500 binder_size_t num_valid;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002501
Todd Kjos8539b1e2019-02-08 10:35:20 -08002502 num_valid = (buffer_offset - off_start_offset) /
2503 sizeof(binder_size_t);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002504 fda = to_binder_fd_array_object(hdr);
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002505 parent = binder_validate_ptr(proc, buffer, &ptr_object,
2506 fda->parent,
2507 off_start_offset,
2508 NULL,
Todd Kjos8539b1e2019-02-08 10:35:20 -08002509 num_valid);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002510 if (!parent) {
2511 pr_err("transaction release %d bad parent offset",
2512 debug_id);
2513 continue;
2514 }
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002515 fd_buf_size = sizeof(u32) * fda->num_fds;
2516 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2517 pr_err("transaction release %d invalid number of fds (%lld)\n",
2518 debug_id, (u64)fda->num_fds);
2519 continue;
2520 }
2521 if (fd_buf_size > parent->length ||
2522 fda->parent_offset > parent->length - fd_buf_size) {
2523 /* No space for all file descriptors here. */
2524 pr_err("transaction release %d not enough space for %lld fds in buffer\n",
2525 debug_id, (u64)fda->num_fds);
2526 continue;
2527 }
Todd Kjos8539b1e2019-02-08 10:35:20 -08002528 /*
2529 * the source data for binder_buffer_object is visible
2530 * to user-space and the @buffer element is the user
2531 * pointer to the buffer_object containing the fd_array.
2532 * Convert the address to an offset relative to
2533 * the base of the transaction buffer.
2534 */
2535 fda_offset =
2536 (parent->buffer - (uintptr_t)buffer->user_data) +
2537 fda->parent_offset;
Todd Kjos90a570c2019-02-08 10:35:15 -08002538 for (fd_index = 0; fd_index < fda->num_fds;
2539 fd_index++) {
2540 u32 fd;
Todd Kjos8539b1e2019-02-08 10:35:20 -08002541 binder_size_t offset = fda_offset +
2542 fd_index * sizeof(fd);
Todd Kjos90a570c2019-02-08 10:35:15 -08002543
2544 binder_alloc_copy_from_buffer(&proc->alloc,
2545 &fd,
2546 buffer,
2547 offset,
2548 sizeof(fd));
2549 task_close_fd(proc, fd);
2550 }
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002551 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002552 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01002553 pr_err("transaction release %d bad object type %x\n",
Martijn Coenen00c80372016-07-13 12:06:49 +02002554 debug_id, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002555 break;
2556 }
2557 }
2558}
2559
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002560static int binder_translate_binder(struct flat_binder_object *fp,
2561 struct binder_transaction *t,
2562 struct binder_thread *thread)
2563{
2564 struct binder_node *node;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002565 struct binder_proc *proc = thread->proc;
2566 struct binder_proc *target_proc = t->to_proc;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002567 struct binder_ref_data rdata;
Todd Kjosf22abc72017-05-09 11:08:05 -07002568 int ret = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002569
2570 node = binder_get_node(proc, fp->binder);
2571 if (!node) {
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002572 node = binder_new_node(proc, fp);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002573 if (!node)
2574 return -ENOMEM;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002575 }
2576 if (fp->cookie != node->cookie) {
2577 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
2578 proc->pid, thread->pid, (u64)fp->binder,
2579 node->debug_id, (u64)fp->cookie,
2580 (u64)node->cookie);
Todd Kjosf22abc72017-05-09 11:08:05 -07002581 ret = -EINVAL;
2582 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002583 }
Todd Kjosf22abc72017-05-09 11:08:05 -07002584 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2585 ret = -EPERM;
2586 goto done;
2587 }
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002588
Todd Kjosb0117bb2017-05-08 09:16:27 -07002589 ret = binder_inc_ref_for_node(target_proc, node,
2590 fp->hdr.type == BINDER_TYPE_BINDER,
2591 &thread->todo, &rdata);
2592 if (ret)
Todd Kjosf22abc72017-05-09 11:08:05 -07002593 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002594
2595 if (fp->hdr.type == BINDER_TYPE_BINDER)
2596 fp->hdr.type = BINDER_TYPE_HANDLE;
2597 else
2598 fp->hdr.type = BINDER_TYPE_WEAK_HANDLE;
2599 fp->binder = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002600 fp->handle = rdata.desc;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002601 fp->cookie = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002602
Todd Kjosb0117bb2017-05-08 09:16:27 -07002603 trace_binder_transaction_node_to_ref(t, node, &rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002604 binder_debug(BINDER_DEBUG_TRANSACTION,
2605 " node %d u%016llx -> ref %d desc %d\n",
2606 node->debug_id, (u64)node->ptr,
Todd Kjosb0117bb2017-05-08 09:16:27 -07002607 rdata.debug_id, rdata.desc);
Todd Kjosf22abc72017-05-09 11:08:05 -07002608done:
2609 binder_put_node(node);
2610 return ret;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002611}
2612
2613static int binder_translate_handle(struct flat_binder_object *fp,
2614 struct binder_transaction *t,
2615 struct binder_thread *thread)
2616{
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002617 struct binder_proc *proc = thread->proc;
2618 struct binder_proc *target_proc = t->to_proc;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002619 struct binder_node *node;
2620 struct binder_ref_data src_rdata;
Todd Kjosf22abc72017-05-09 11:08:05 -07002621 int ret = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002622
Todd Kjosb0117bb2017-05-08 09:16:27 -07002623 node = binder_get_node_from_ref(proc, fp->handle,
2624 fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata);
2625 if (!node) {
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002626 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
2627 proc->pid, thread->pid, fp->handle);
2628 return -EINVAL;
2629 }
Todd Kjosf22abc72017-05-09 11:08:05 -07002630 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2631 ret = -EPERM;
2632 goto done;
2633 }
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002634
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002635 binder_node_lock(node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002636 if (node->proc == target_proc) {
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002637 if (fp->hdr.type == BINDER_TYPE_HANDLE)
2638 fp->hdr.type = BINDER_TYPE_BINDER;
2639 else
2640 fp->hdr.type = BINDER_TYPE_WEAK_BINDER;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002641 fp->binder = node->ptr;
2642 fp->cookie = node->cookie;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002643 if (node->proc)
2644 binder_inner_proc_lock(node->proc);
2645 binder_inc_node_nilocked(node,
2646 fp->hdr.type == BINDER_TYPE_BINDER,
2647 0, NULL);
2648 if (node->proc)
2649 binder_inner_proc_unlock(node->proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002650 trace_binder_transaction_ref_to_node(t, node, &src_rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002651 binder_debug(BINDER_DEBUG_TRANSACTION,
2652 " ref %d desc %d -> node %d u%016llx\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07002653 src_rdata.debug_id, src_rdata.desc, node->debug_id,
2654 (u64)node->ptr);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002655 binder_node_unlock(node);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002656 } else {
Todd Kjosb0117bb2017-05-08 09:16:27 -07002657 struct binder_ref_data dest_rdata;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002658
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002659 binder_node_unlock(node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002660 ret = binder_inc_ref_for_node(target_proc, node,
2661 fp->hdr.type == BINDER_TYPE_HANDLE,
2662 NULL, &dest_rdata);
2663 if (ret)
Todd Kjosf22abc72017-05-09 11:08:05 -07002664 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002665
2666 fp->binder = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002667 fp->handle = dest_rdata.desc;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002668 fp->cookie = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002669 trace_binder_transaction_ref_to_ref(t, node, &src_rdata,
2670 &dest_rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002671 binder_debug(BINDER_DEBUG_TRANSACTION,
2672 " ref %d desc %d -> ref %d desc %d (node %d)\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07002673 src_rdata.debug_id, src_rdata.desc,
2674 dest_rdata.debug_id, dest_rdata.desc,
2675 node->debug_id);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002676 }
Todd Kjosf22abc72017-05-09 11:08:05 -07002677done:
2678 binder_put_node(node);
2679 return ret;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002680}
2681
2682static int binder_translate_fd(int fd,
2683 struct binder_transaction *t,
2684 struct binder_thread *thread,
2685 struct binder_transaction *in_reply_to)
2686{
2687 struct binder_proc *proc = thread->proc;
2688 struct binder_proc *target_proc = t->to_proc;
2689 int target_fd;
2690 struct file *file;
2691 int ret;
2692 bool target_allows_fd;
2693
2694 if (in_reply_to)
2695 target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS);
2696 else
2697 target_allows_fd = t->buffer->target_node->accept_fds;
2698 if (!target_allows_fd) {
2699 binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
2700 proc->pid, thread->pid,
2701 in_reply_to ? "reply" : "transaction",
2702 fd);
2703 ret = -EPERM;
2704 goto err_fd_not_accepted;
2705 }
2706
2707 file = fget(fd);
2708 if (!file) {
2709 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
2710 proc->pid, thread->pid, fd);
2711 ret = -EBADF;
2712 goto err_fget;
2713 }
2714 ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file);
2715 if (ret < 0) {
2716 ret = -EPERM;
2717 goto err_security;
2718 }
2719
2720 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
2721 if (target_fd < 0) {
2722 ret = -ENOMEM;
2723 goto err_get_unused_fd;
2724 }
2725 task_fd_install(target_proc, target_fd, file);
2726 trace_binder_transaction_fd(t, fd, target_fd);
2727 binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n",
2728 fd, target_fd);
2729
2730 return target_fd;
2731
2732err_get_unused_fd:
2733err_security:
2734 fput(file);
2735err_fget:
2736err_fd_not_accepted:
2737 return ret;
2738}
2739
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002740static int binder_translate_fd_array(struct binder_fd_array_object *fda,
2741 struct binder_buffer_object *parent,
2742 struct binder_transaction *t,
2743 struct binder_thread *thread,
2744 struct binder_transaction *in_reply_to)
2745{
2746 binder_size_t fdi, fd_buf_size, num_installed_fds;
Todd Kjos8539b1e2019-02-08 10:35:20 -08002747 binder_size_t fda_offset;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002748 int target_fd;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002749 struct binder_proc *proc = thread->proc;
2750 struct binder_proc *target_proc = t->to_proc;
2751
2752 fd_buf_size = sizeof(u32) * fda->num_fds;
2753 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2754 binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
2755 proc->pid, thread->pid, (u64)fda->num_fds);
2756 return -EINVAL;
2757 }
2758 if (fd_buf_size > parent->length ||
2759 fda->parent_offset > parent->length - fd_buf_size) {
2760 /* No space for all file descriptors here. */
2761 binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
2762 proc->pid, thread->pid, (u64)fda->num_fds);
2763 return -EINVAL;
2764 }
2765 /*
Todd Kjos8539b1e2019-02-08 10:35:20 -08002766 * the source data for binder_buffer_object is visible
2767 * to user-space and the @buffer element is the user
2768 * pointer to the buffer_object containing the fd_array.
2769 * Convert the address to an offset relative to
2770 * the base of the transaction buffer.
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002771 */
Todd Kjos8539b1e2019-02-08 10:35:20 -08002772 fda_offset = (parent->buffer - (uintptr_t)t->buffer->user_data) +
2773 fda->parent_offset;
2774 if (!IS_ALIGNED((unsigned long)fda_offset, sizeof(u32))) {
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002775 binder_user_error("%d:%d parent offset not aligned correctly.\n",
2776 proc->pid, thread->pid);
2777 return -EINVAL;
2778 }
2779 for (fdi = 0; fdi < fda->num_fds; fdi++) {
Todd Kjos90a570c2019-02-08 10:35:15 -08002780 u32 fd;
Todd Kjos0b90a822019-03-27 16:12:31 -07002781
Todd Kjos8539b1e2019-02-08 10:35:20 -08002782 binder_size_t offset = fda_offset + fdi * sizeof(fd);
Todd Kjos90a570c2019-02-08 10:35:15 -08002783
2784 binder_alloc_copy_from_buffer(&target_proc->alloc,
2785 &fd, t->buffer,
2786 offset, sizeof(fd));
2787 target_fd = binder_translate_fd(fd, t, thread, in_reply_to);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002788 if (target_fd < 0)
2789 goto err_translate_fd_failed;
Todd Kjos90a570c2019-02-08 10:35:15 -08002790 binder_alloc_copy_to_buffer(&target_proc->alloc,
2791 t->buffer, offset,
2792 &target_fd, sizeof(fd));
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002793 }
2794 return 0;
2795
2796err_translate_fd_failed:
2797 /*
2798 * Failed to allocate fd or security error, free fds
2799 * installed so far.
2800 */
2801 num_installed_fds = fdi;
Todd Kjos90a570c2019-02-08 10:35:15 -08002802 for (fdi = 0; fdi < num_installed_fds; fdi++) {
2803 u32 fd;
Todd Kjos8539b1e2019-02-08 10:35:20 -08002804 binder_size_t offset = fda_offset + fdi * sizeof(fd);
Todd Kjos90a570c2019-02-08 10:35:15 -08002805 binder_alloc_copy_from_buffer(&target_proc->alloc,
2806 &fd, t->buffer,
2807 offset, sizeof(fd));
2808 task_close_fd(target_proc, fd);
2809 }
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002810 return target_fd;
2811}
2812
Martijn Coenen5a6da532016-09-30 14:10:07 +02002813static int binder_fixup_parent(struct binder_transaction *t,
2814 struct binder_thread *thread,
2815 struct binder_buffer_object *bp,
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002816 binder_size_t off_start_offset,
Martijn Coenen5a6da532016-09-30 14:10:07 +02002817 binder_size_t num_valid,
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002818 binder_size_t last_fixup_obj_off,
Martijn Coenen5a6da532016-09-30 14:10:07 +02002819 binder_size_t last_fixup_min_off)
2820{
2821 struct binder_buffer_object *parent;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002822 struct binder_buffer *b = t->buffer;
2823 struct binder_proc *proc = thread->proc;
2824 struct binder_proc *target_proc = t->to_proc;
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002825 struct binder_object object;
2826 binder_size_t buffer_offset;
2827 binder_size_t parent_offset;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002828
2829 if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT))
2830 return 0;
2831
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002832 parent = binder_validate_ptr(target_proc, b, &object, bp->parent,
2833 off_start_offset, &parent_offset,
2834 num_valid);
Martijn Coenen5a6da532016-09-30 14:10:07 +02002835 if (!parent) {
2836 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
2837 proc->pid, thread->pid);
2838 return -EINVAL;
2839 }
2840
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002841 if (!binder_validate_fixup(target_proc, b, off_start_offset,
2842 parent_offset, bp->parent_offset,
2843 last_fixup_obj_off,
Martijn Coenen5a6da532016-09-30 14:10:07 +02002844 last_fixup_min_off)) {
2845 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
2846 proc->pid, thread->pid);
2847 return -EINVAL;
2848 }
2849
2850 if (parent->length < sizeof(binder_uintptr_t) ||
2851 bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) {
2852 /* No space for a pointer here! */
2853 binder_user_error("%d:%d got transaction with invalid parent offset\n",
2854 proc->pid, thread->pid);
2855 return -EINVAL;
2856 }
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002857 buffer_offset = bp->parent_offset +
Todd Kjos8539b1e2019-02-08 10:35:20 -08002858 (uintptr_t)parent->buffer - (uintptr_t)b->user_data;
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002859 binder_alloc_copy_to_buffer(&target_proc->alloc, b, buffer_offset,
2860 &bp->buffer, sizeof(bp->buffer));
Martijn Coenen5a6da532016-09-30 14:10:07 +02002861
2862 return 0;
2863}
2864
Martijn Coenen053be422017-06-06 15:17:46 -07002865/**
2866 * binder_proc_transaction() - sends a transaction to a process and wakes it up
2867 * @t: transaction to send
2868 * @proc: process to send the transaction to
2869 * @thread: thread in @proc to send the transaction to (may be NULL)
2870 *
2871 * This function queues a transaction to the specified process. It will try
2872 * to find a thread in the target process to handle the transaction and
2873 * wake it up. If no thread is found, the work is queued to the proc
2874 * waitqueue.
2875 *
2876 * If the @thread parameter is not NULL, the transaction is always queued
2877 * to the waitlist of that specific thread.
2878 *
2879 * Return: true if the transactions was successfully queued
2880 * false if the target process or thread is dead
2881 */
2882static bool binder_proc_transaction(struct binder_transaction *t,
2883 struct binder_proc *proc,
2884 struct binder_thread *thread)
2885{
Martijn Coenen053be422017-06-06 15:17:46 -07002886 struct binder_node *node = t->buffer->target_node;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002887 struct binder_priority node_prio;
Martijn Coenen053be422017-06-06 15:17:46 -07002888 bool oneway = !!(t->flags & TF_ONE_WAY);
Martijn Coenen1af61802017-10-19 15:04:46 +02002889 bool pending_async = false;
Martijn Coenen053be422017-06-06 15:17:46 -07002890
2891 BUG_ON(!node);
2892 binder_node_lock(node);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002893 node_prio.prio = node->min_priority;
2894 node_prio.sched_policy = node->sched_policy;
2895
Martijn Coenen053be422017-06-06 15:17:46 -07002896 if (oneway) {
2897 BUG_ON(thread);
2898 if (node->has_async_transaction) {
Martijn Coenen1af61802017-10-19 15:04:46 +02002899 pending_async = true;
Martijn Coenen053be422017-06-06 15:17:46 -07002900 } else {
Gustavo A. R. Silvae62dd6f2018-01-23 12:04:27 -06002901 node->has_async_transaction = true;
Martijn Coenen053be422017-06-06 15:17:46 -07002902 }
2903 }
2904
2905 binder_inner_proc_lock(proc);
2906
2907 if (proc->is_dead || (thread && thread->is_dead)) {
2908 binder_inner_proc_unlock(proc);
2909 binder_node_unlock(node);
2910 return false;
2911 }
2912
Martijn Coenen1af61802017-10-19 15:04:46 +02002913 if (!thread && !pending_async)
Martijn Coenen053be422017-06-06 15:17:46 -07002914 thread = binder_select_thread_ilocked(proc);
2915
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002916 if (thread) {
Martijn Coenenc46810c2017-06-23 10:13:43 -07002917 binder_transaction_priority(thread->task, t, node_prio,
2918 node->inherit_rt);
Martijn Coenen1af61802017-10-19 15:04:46 +02002919 binder_enqueue_thread_work_ilocked(thread, &t->work);
2920 } else if (!pending_async) {
2921 binder_enqueue_work_ilocked(&t->work, &proc->todo);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002922 } else {
Martijn Coenen1af61802017-10-19 15:04:46 +02002923 binder_enqueue_work_ilocked(&t->work, &node->async_todo);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002924 }
Martijn Coenen053be422017-06-06 15:17:46 -07002925
Martijn Coenen1af61802017-10-19 15:04:46 +02002926 if (!pending_async)
Martijn Coenen053be422017-06-06 15:17:46 -07002927 binder_wakeup_thread_ilocked(proc, thread, !oneway /* sync */);
2928
2929 binder_inner_proc_unlock(proc);
2930 binder_node_unlock(node);
2931
2932 return true;
2933}
2934
Todd Kjos291d9682017-09-25 08:55:09 -07002935/**
2936 * binder_get_node_refs_for_txn() - Get required refs on node for txn
2937 * @node: struct binder_node for which to get refs
2938 * @proc: returns @node->proc if valid
2939 * @error: if no @proc then returns BR_DEAD_REPLY
2940 *
2941 * User-space normally keeps the node alive when creating a transaction
2942 * since it has a reference to the target. The local strong ref keeps it
2943 * alive if the sending process dies before the target process processes
2944 * the transaction. If the source process is malicious or has a reference
2945 * counting bug, relying on the local strong ref can fail.
2946 *
2947 * Since user-space can cause the local strong ref to go away, we also take
2948 * a tmpref on the node to ensure it survives while we are constructing
2949 * the transaction. We also need a tmpref on the proc while we are
2950 * constructing the transaction, so we take that here as well.
2951 *
2952 * Return: The target_node with refs taken or NULL if no @node->proc is NULL.
2953 * Also sets @proc if valid. If the @node->proc is NULL indicating that the
2954 * target proc has died, @error is set to BR_DEAD_REPLY
2955 */
2956static struct binder_node *binder_get_node_refs_for_txn(
2957 struct binder_node *node,
2958 struct binder_proc **procp,
2959 uint32_t *error)
2960{
2961 struct binder_node *target_node = NULL;
2962
2963 binder_node_inner_lock(node);
2964 if (node->proc) {
2965 target_node = node;
2966 binder_inc_node_nilocked(node, 1, 0, NULL);
2967 binder_inc_node_tmpref_ilocked(node);
Todd Kjosf4e6de32019-06-10 09:14:25 -07002968 atomic_inc(&node->proc->tmp_ref);
Todd Kjos291d9682017-09-25 08:55:09 -07002969 *procp = node->proc;
2970 } else
2971 *error = BR_DEAD_REPLY;
2972 binder_node_inner_unlock(node);
2973
2974 return target_node;
2975}
2976
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002977static void binder_transaction(struct binder_proc *proc,
2978 struct binder_thread *thread,
Martijn Coenen59878d72016-09-30 14:05:40 +02002979 struct binder_transaction_data *tr, int reply,
2980 binder_size_t extra_buffers_size)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002981{
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002982 int ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002983 struct binder_transaction *t;
2984 struct binder_work *tcomplete;
Todd Kjos8539b1e2019-02-08 10:35:20 -08002985 binder_size_t buffer_offset = 0;
2986 binder_size_t off_start_offset, off_end_offset;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08002987 binder_size_t off_min;
Todd Kjos8539b1e2019-02-08 10:35:20 -08002988 binder_size_t sg_buf_offset, sg_buf_end_offset;
Todd Kjos2f993e22017-05-12 14:42:55 -07002989 struct binder_proc *target_proc = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002990 struct binder_thread *target_thread = NULL;
2991 struct binder_node *target_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002992 struct binder_transaction *in_reply_to = NULL;
2993 struct binder_transaction_log_entry *e;
Todd Kjose598d172017-03-22 17:19:52 -07002994 uint32_t return_error = 0;
2995 uint32_t return_error_param = 0;
2996 uint32_t return_error_line = 0;
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002997 binder_size_t last_fixup_obj_off = 0;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002998 binder_size_t last_fixup_min_off = 0;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02002999 struct binder_context *context = proc->context;
Todd Kjos1cfe6272017-05-24 13:33:28 -07003000 int t_debug_id = atomic_inc_return(&binder_last_id);
Todd Kjos63e0afa2019-01-14 09:10:21 -08003001 char *secctx = NULL;
3002 u32 secctx_sz = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003003
3004 e = binder_transaction_log_add(&binder_transaction_log);
Todd Kjos1cfe6272017-05-24 13:33:28 -07003005 e->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003006 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
3007 e->from_proc = proc->pid;
3008 e->from_thread = thread->pid;
3009 e->target_handle = tr->target.handle;
3010 e->data_size = tr->data_size;
3011 e->offsets_size = tr->offsets_size;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02003012 e->context_name = proc->context->name;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003013
3014 if (reply) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07003015 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003016 in_reply_to = thread->transaction_stack;
3017 if (in_reply_to == NULL) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07003018 binder_inner_proc_unlock(proc);
Anmol Sarma56b468f2012-10-30 22:35:43 +05303019 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003020 proc->pid, thread->pid);
3021 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003022 return_error_param = -EPROTO;
3023 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003024 goto err_empty_call_stack;
3025 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003026 if (in_reply_to->to_thread != thread) {
Todd Kjos2f993e22017-05-12 14:42:55 -07003027 spin_lock(&in_reply_to->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05303028 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 +09003029 proc->pid, thread->pid, in_reply_to->debug_id,
3030 in_reply_to->to_proc ?
3031 in_reply_to->to_proc->pid : 0,
3032 in_reply_to->to_thread ?
3033 in_reply_to->to_thread->pid : 0);
Todd Kjos2f993e22017-05-12 14:42:55 -07003034 spin_unlock(&in_reply_to->lock);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003035 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003036 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003037 return_error_param = -EPROTO;
3038 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003039 in_reply_to = NULL;
3040 goto err_bad_call_stack;
3041 }
3042 thread->transaction_stack = in_reply_to->to_parent;
Martijn Coenen995a36e2017-06-02 13:36:52 -07003043 binder_inner_proc_unlock(proc);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003044 target_thread = binder_get_txn_from_and_acq_inner(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003045 if (target_thread == NULL) {
3046 return_error = BR_DEAD_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003047 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003048 goto err_dead_binder;
3049 }
3050 if (target_thread->transaction_stack != in_reply_to) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303051 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 +09003052 proc->pid, thread->pid,
3053 target_thread->transaction_stack ?
3054 target_thread->transaction_stack->debug_id : 0,
3055 in_reply_to->debug_id);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003056 binder_inner_proc_unlock(target_thread->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 target_thread = NULL;
3062 goto err_dead_binder;
3063 }
3064 target_proc = target_thread->proc;
Todd Kjosf4e6de32019-06-10 09:14:25 -07003065 atomic_inc(&target_proc->tmp_ref);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003066 binder_inner_proc_unlock(target_thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003067 } else {
3068 if (tr->target.handle) {
3069 struct binder_ref *ref;
Seunghun Lee10f62862014-05-01 01:30:23 +09003070
Todd Kjosc37162d2017-05-26 11:56:29 -07003071 /*
3072 * There must already be a strong ref
3073 * on this node. If so, do a strong
3074 * increment on the node to ensure it
3075 * stays alive until the transaction is
3076 * done.
3077 */
Todd Kjos5346bf32016-10-20 16:43:34 -07003078 binder_proc_lock(proc);
3079 ref = binder_get_ref_olocked(proc, tr->target.handle,
3080 true);
Todd Kjosc37162d2017-05-26 11:56:29 -07003081 if (ref) {
Todd Kjos291d9682017-09-25 08:55:09 -07003082 target_node = binder_get_node_refs_for_txn(
3083 ref->node, &target_proc,
3084 &return_error);
3085 } else {
3086 binder_user_error("%d:%d got transaction to invalid handle\n",
3087 proc->pid, thread->pid);
3088 return_error = BR_FAILED_REPLY;
Todd Kjosc37162d2017-05-26 11:56:29 -07003089 }
Todd Kjos5346bf32016-10-20 16:43:34 -07003090 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003091 } else {
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003092 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003093 target_node = context->binder_context_mgr_node;
Todd Kjos291d9682017-09-25 08:55:09 -07003094 if (target_node)
3095 target_node = binder_get_node_refs_for_txn(
3096 target_node, &target_proc,
3097 &return_error);
3098 else
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003099 return_error = BR_DEAD_REPLY;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003100 mutex_unlock(&context->context_mgr_node_lock);
Martijn Coenenc4048b22018-03-28 11:14:50 +02003101 if (target_node && target_proc == proc) {
3102 binder_user_error("%d:%d got transaction to context manager from process owning it\n",
3103 proc->pid, thread->pid);
3104 return_error = BR_FAILED_REPLY;
3105 return_error_param = -EINVAL;
3106 return_error_line = __LINE__;
3107 goto err_invalid_target_handle;
3108 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003109 }
Todd Kjos291d9682017-09-25 08:55:09 -07003110 if (!target_node) {
3111 /*
3112 * return_error is set above
3113 */
3114 return_error_param = -EINVAL;
Todd Kjose598d172017-03-22 17:19:52 -07003115 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003116 goto err_dead_binder;
3117 }
Todd Kjos291d9682017-09-25 08:55:09 -07003118 e->to_node = target_node->debug_id;
Stephen Smalley79af7302015-01-21 10:54:10 -05003119 if (security_binder_transaction(proc->tsk,
3120 target_proc->tsk) < 0) {
3121 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003122 return_error_param = -EPERM;
3123 return_error_line = __LINE__;
Stephen Smalley79af7302015-01-21 10:54:10 -05003124 goto err_invalid_target_handle;
3125 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07003126 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003127 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
3128 struct binder_transaction *tmp;
Seunghun Lee10f62862014-05-01 01:30:23 +09003129
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003130 tmp = thread->transaction_stack;
3131 if (tmp->to_thread != thread) {
Todd Kjos2f993e22017-05-12 14:42:55 -07003132 spin_lock(&tmp->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05303133 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 +09003134 proc->pid, thread->pid, tmp->debug_id,
3135 tmp->to_proc ? tmp->to_proc->pid : 0,
3136 tmp->to_thread ?
3137 tmp->to_thread->pid : 0);
Todd Kjos2f993e22017-05-12 14:42:55 -07003138 spin_unlock(&tmp->lock);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003139 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003140 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003141 return_error_param = -EPROTO;
3142 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003143 goto err_bad_call_stack;
3144 }
3145 while (tmp) {
Todd Kjos2f993e22017-05-12 14:42:55 -07003146 struct binder_thread *from;
3147
3148 spin_lock(&tmp->lock);
3149 from = tmp->from;
3150 if (from && from->proc == target_proc) {
3151 atomic_inc(&from->tmp_ref);
3152 target_thread = from;
3153 spin_unlock(&tmp->lock);
3154 break;
3155 }
3156 spin_unlock(&tmp->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003157 tmp = tmp->from_parent;
3158 }
3159 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07003160 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003161 }
Martijn Coenen053be422017-06-06 15:17:46 -07003162 if (target_thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003163 e->to_thread = target_thread->pid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003164 e->to_proc = target_proc->pid;
3165
3166 /* TODO: reuse incoming transaction for reply */
3167 t = kzalloc(sizeof(*t), GFP_KERNEL);
3168 if (t == NULL) {
3169 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003170 return_error_param = -ENOMEM;
3171 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003172 goto err_alloc_t_failed;
3173 }
3174 binder_stats_created(BINDER_STAT_TRANSACTION);
Todd Kjos2f993e22017-05-12 14:42:55 -07003175 spin_lock_init(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003176
3177 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
3178 if (tcomplete == NULL) {
3179 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003180 return_error_param = -ENOMEM;
3181 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003182 goto err_alloc_tcomplete_failed;
3183 }
3184 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
3185
Todd Kjos1cfe6272017-05-24 13:33:28 -07003186 t->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003187
3188 if (reply)
3189 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen59878d72016-09-30 14:05:40 +02003190 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003191 proc->pid, thread->pid, t->debug_id,
3192 target_proc->pid, target_thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003193 (u64)tr->data.ptr.buffer,
3194 (u64)tr->data.ptr.offsets,
Martijn Coenen59878d72016-09-30 14:05:40 +02003195 (u64)tr->data_size, (u64)tr->offsets_size,
3196 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003197 else
3198 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen59878d72016-09-30 14:05:40 +02003199 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003200 proc->pid, thread->pid, t->debug_id,
3201 target_proc->pid, target_node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003202 (u64)tr->data.ptr.buffer,
3203 (u64)tr->data.ptr.offsets,
Martijn Coenen59878d72016-09-30 14:05:40 +02003204 (u64)tr->data_size, (u64)tr->offsets_size,
3205 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003206
3207 if (!reply && !(tr->flags & TF_ONE_WAY))
3208 t->from = thread;
3209 else
3210 t->from = NULL;
Tair Rzayev57bab7c2014-05-31 22:43:34 +03003211 t->sender_euid = task_euid(proc->tsk);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003212 t->to_proc = target_proc;
3213 t->to_thread = target_thread;
3214 t->code = tr->code;
3215 t->flags = tr->flags;
Martijn Coenen57b2ac62017-06-06 17:04:42 -07003216 if (!(t->flags & TF_ONE_WAY) &&
3217 binder_supported_policy(current->policy)) {
3218 /* Inherit supported policies for synchronous transactions */
3219 t->priority.sched_policy = current->policy;
3220 t->priority.prio = current->normal_prio;
3221 } else {
3222 /* Otherwise, fall back to the default priority */
3223 t->priority = target_proc->default_priority;
3224 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003225
Todd Kjos63e0afa2019-01-14 09:10:21 -08003226 if (target_node && target_node->txn_security_ctx) {
3227 u32 secid;
Todd Kjosb8f2f4b2019-04-24 12:31:18 -07003228 size_t added_size;
Todd Kjos63e0afa2019-01-14 09:10:21 -08003229
3230 security_task_getsecid(proc->tsk, &secid);
3231 ret = security_secid_to_secctx(secid, &secctx, &secctx_sz);
3232 if (ret) {
3233 return_error = BR_FAILED_REPLY;
3234 return_error_param = ret;
3235 return_error_line = __LINE__;
3236 goto err_get_secctx_failed;
3237 }
Todd Kjosb8f2f4b2019-04-24 12:31:18 -07003238 added_size = ALIGN(secctx_sz, sizeof(u64));
3239 extra_buffers_size += added_size;
3240 if (extra_buffers_size < added_size) {
3241 /* integer overflow of extra_buffers_size */
3242 return_error = BR_FAILED_REPLY;
3243 return_error_param = EINVAL;
3244 return_error_line = __LINE__;
3245 goto err_bad_extra_size;
3246 }
Todd Kjos63e0afa2019-01-14 09:10:21 -08003247 }
3248
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003249 trace_binder_transaction(reply, t, target_node);
3250
Todd Kjosd325d372016-10-10 10:40:53 -07003251 t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size,
Martijn Coenen59878d72016-09-30 14:05:40 +02003252 tr->offsets_size, extra_buffers_size,
3253 !reply && (t->flags & TF_ONE_WAY));
Todd Kjose598d172017-03-22 17:19:52 -07003254 if (IS_ERR(t->buffer)) {
3255 /*
3256 * -ESRCH indicates VMA cleared. The target is dying.
3257 */
3258 return_error_param = PTR_ERR(t->buffer);
3259 return_error = return_error_param == -ESRCH ?
3260 BR_DEAD_REPLY : BR_FAILED_REPLY;
3261 return_error_line = __LINE__;
3262 t->buffer = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003263 goto err_binder_alloc_buf_failed;
3264 }
Todd Kjos63e0afa2019-01-14 09:10:21 -08003265 if (secctx) {
3266 size_t buf_offset = ALIGN(tr->data_size, sizeof(void *)) +
3267 ALIGN(tr->offsets_size, sizeof(void *)) +
3268 ALIGN(extra_buffers_size, sizeof(void *)) -
3269 ALIGN(secctx_sz, sizeof(u64));
Todd Kjos63e0afa2019-01-14 09:10:21 -08003270
Todd Kjos8539b1e2019-02-08 10:35:20 -08003271 t->security_ctx = (uintptr_t)t->buffer->user_data + buf_offset;
Todd Kjos90a570c2019-02-08 10:35:15 -08003272 binder_alloc_copy_to_buffer(&target_proc->alloc,
3273 t->buffer, buf_offset,
3274 secctx, secctx_sz);
Todd Kjos63e0afa2019-01-14 09:10:21 -08003275 security_release_secctx(secctx, secctx_sz);
3276 secctx = NULL;
3277 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003278 t->buffer->debug_id = t->debug_id;
3279 t->buffer->transaction = t;
3280 t->buffer->target_node = target_node;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003281 trace_binder_transaction_alloc_buf(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003282
Todd Kjosd5049492019-02-08 10:35:14 -08003283 if (binder_alloc_copy_user_to_buffer(
3284 &target_proc->alloc,
3285 t->buffer, 0,
3286 (const void __user *)
3287 (uintptr_t)tr->data.ptr.buffer,
3288 tr->data_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303289 binder_user_error("%d:%d got transaction with invalid data ptr\n",
3290 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003291 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003292 return_error_param = -EFAULT;
3293 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003294 goto err_copy_data_failed;
3295 }
Todd Kjosd5049492019-02-08 10:35:14 -08003296 if (binder_alloc_copy_user_to_buffer(
3297 &target_proc->alloc,
3298 t->buffer,
3299 ALIGN(tr->data_size, sizeof(void *)),
3300 (const void __user *)
3301 (uintptr_t)tr->data.ptr.offsets,
3302 tr->offsets_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303303 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3304 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003305 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003306 return_error_param = -EFAULT;
3307 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003308 goto err_copy_data_failed;
3309 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08003310 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
3311 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
3312 proc->pid, thread->pid, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003313 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003314 return_error_param = -EINVAL;
3315 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003316 goto err_bad_offset;
3317 }
Martijn Coenen5a6da532016-09-30 14:10:07 +02003318 if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) {
3319 binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
3320 proc->pid, thread->pid,
Amit Pundir44cbb182017-02-01 12:53:45 +05303321 (u64)extra_buffers_size);
Martijn Coenen5a6da532016-09-30 14:10:07 +02003322 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003323 return_error_param = -EINVAL;
3324 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003325 goto err_bad_offset;
3326 }
Todd Kjos8539b1e2019-02-08 10:35:20 -08003327 off_start_offset = ALIGN(tr->data_size, sizeof(void *));
3328 buffer_offset = off_start_offset;
3329 off_end_offset = off_start_offset + tr->offsets_size;
3330 sg_buf_offset = ALIGN(off_end_offset, sizeof(void *));
Martijn Coenen3244efe2019-07-09 13:09:23 +02003331 sg_buf_end_offset = sg_buf_offset + extra_buffers_size -
3332 ALIGN(secctx_sz, sizeof(u64));
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08003333 off_min = 0;
Todd Kjos8539b1e2019-02-08 10:35:20 -08003334 for (buffer_offset = off_start_offset; buffer_offset < off_end_offset;
3335 buffer_offset += sizeof(binder_size_t)) {
Martijn Coenen00c80372016-07-13 12:06:49 +02003336 struct binder_object_header *hdr;
Todd Kjos90a570c2019-02-08 10:35:15 -08003337 size_t object_size;
Todd Kjosd73356a2019-02-08 10:35:16 -08003338 struct binder_object object;
Todd Kjos90a570c2019-02-08 10:35:15 -08003339 binder_size_t object_offset;
Seunghun Lee10f62862014-05-01 01:30:23 +09003340
Todd Kjos90a570c2019-02-08 10:35:15 -08003341 binder_alloc_copy_from_buffer(&target_proc->alloc,
3342 &object_offset,
3343 t->buffer,
3344 buffer_offset,
3345 sizeof(object_offset));
Todd Kjosd73356a2019-02-08 10:35:16 -08003346 object_size = binder_get_object(target_proc, t->buffer,
3347 object_offset, &object);
Todd Kjos90a570c2019-02-08 10:35:15 -08003348 if (object_size == 0 || object_offset < off_min) {
Martijn Coenen00c80372016-07-13 12:06:49 +02003349 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 -08003350 proc->pid, thread->pid,
3351 (u64)object_offset,
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08003352 (u64)off_min,
Martijn Coenen00c80372016-07-13 12:06:49 +02003353 (u64)t->buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003354 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003355 return_error_param = -EINVAL;
3356 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003357 goto err_bad_offset;
3358 }
Martijn Coenen00c80372016-07-13 12:06:49 +02003359
Todd Kjosd73356a2019-02-08 10:35:16 -08003360 hdr = &object.hdr;
Todd Kjos90a570c2019-02-08 10:35:15 -08003361 off_min = object_offset + object_size;
Martijn Coenen00c80372016-07-13 12:06:49 +02003362 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003363 case BINDER_TYPE_BINDER:
3364 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003365 struct flat_binder_object *fp;
Seunghun Lee10f62862014-05-01 01:30:23 +09003366
Martijn Coenen00c80372016-07-13 12:06:49 +02003367 fp = to_flat_binder_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003368 ret = binder_translate_binder(fp, t, thread);
3369 if (ret < 0) {
Christian Engelmayer7d420432014-05-07 21:44:53 +02003370 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003371 return_error_param = ret;
3372 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003373 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003374 }
Todd Kjosd73356a2019-02-08 10:35:16 -08003375 binder_alloc_copy_to_buffer(&target_proc->alloc,
3376 t->buffer, object_offset,
3377 fp, sizeof(*fp));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003378 } break;
3379 case BINDER_TYPE_HANDLE:
3380 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003381 struct flat_binder_object *fp;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02003382
Martijn Coenen00c80372016-07-13 12:06:49 +02003383 fp = to_flat_binder_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003384 ret = binder_translate_handle(fp, t, thread);
3385 if (ret < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003386 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003387 return_error_param = ret;
3388 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003389 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003390 }
Todd Kjosd73356a2019-02-08 10:35:16 -08003391 binder_alloc_copy_to_buffer(&target_proc->alloc,
3392 t->buffer, object_offset,
3393 fp, sizeof(*fp));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003394 } break;
3395
3396 case BINDER_TYPE_FD: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003397 struct binder_fd_object *fp = to_binder_fd_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003398 int target_fd = binder_translate_fd(fp->fd, t, thread,
3399 in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003400
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003401 if (target_fd < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003402 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003403 return_error_param = target_fd;
3404 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003405 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003406 }
Martijn Coenen00c80372016-07-13 12:06:49 +02003407 fp->pad_binder = 0;
3408 fp->fd = target_fd;
Todd Kjosd73356a2019-02-08 10:35:16 -08003409 binder_alloc_copy_to_buffer(&target_proc->alloc,
3410 t->buffer, object_offset,
3411 fp, sizeof(*fp));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003412 } break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003413 case BINDER_TYPE_FDA: {
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003414 struct binder_object ptr_object;
3415 binder_size_t parent_offset;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003416 struct binder_fd_array_object *fda =
3417 to_binder_fd_array_object(hdr);
Todd Kjos2ea90f02019-12-13 12:25:31 -08003418 size_t num_valid = (buffer_offset - off_start_offset) /
Todd Kjos8539b1e2019-02-08 10:35:20 -08003419 sizeof(binder_size_t);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003420 struct binder_buffer_object *parent =
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003421 binder_validate_ptr(target_proc, t->buffer,
3422 &ptr_object, fda->parent,
3423 off_start_offset,
3424 &parent_offset,
Todd Kjos8539b1e2019-02-08 10:35:20 -08003425 num_valid);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003426 if (!parent) {
3427 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
3428 proc->pid, thread->pid);
3429 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003430 return_error_param = -EINVAL;
3431 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003432 goto err_bad_parent;
3433 }
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003434 if (!binder_validate_fixup(target_proc, t->buffer,
3435 off_start_offset,
3436 parent_offset,
3437 fda->parent_offset,
3438 last_fixup_obj_off,
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003439 last_fixup_min_off)) {
3440 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
3441 proc->pid, thread->pid);
3442 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003443 return_error_param = -EINVAL;
3444 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003445 goto err_bad_parent;
3446 }
3447 ret = binder_translate_fd_array(fda, parent, t, thread,
3448 in_reply_to);
3449 if (ret < 0) {
3450 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003451 return_error_param = ret;
3452 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003453 goto err_translate_failed;
3454 }
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003455 last_fixup_obj_off = parent_offset;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003456 last_fixup_min_off =
3457 fda->parent_offset + sizeof(u32) * fda->num_fds;
3458 } break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003459 case BINDER_TYPE_PTR: {
3460 struct binder_buffer_object *bp =
3461 to_binder_buffer_object(hdr);
Todd Kjos8539b1e2019-02-08 10:35:20 -08003462 size_t buf_left = sg_buf_end_offset - sg_buf_offset;
3463 size_t num_valid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003464
Martijn Coenen5a6da532016-09-30 14:10:07 +02003465 if (bp->length > buf_left) {
3466 binder_user_error("%d:%d got transaction with too large buffer\n",
3467 proc->pid, thread->pid);
3468 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003469 return_error_param = -EINVAL;
3470 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003471 goto err_bad_offset;
3472 }
Todd Kjosd5049492019-02-08 10:35:14 -08003473 if (binder_alloc_copy_user_to_buffer(
3474 &target_proc->alloc,
3475 t->buffer,
3476 sg_buf_offset,
3477 (const void __user *)
3478 (uintptr_t)bp->buffer,
3479 bp->length)) {
Martijn Coenen5a6da532016-09-30 14:10:07 +02003480 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3481 proc->pid, thread->pid);
Todd Kjose598d172017-03-22 17:19:52 -07003482 return_error_param = -EFAULT;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003483 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003484 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003485 goto err_copy_data_failed;
3486 }
3487 /* Fixup buffer pointer to target proc address space */
Todd Kjos8539b1e2019-02-08 10:35:20 -08003488 bp->buffer = (uintptr_t)
3489 t->buffer->user_data + sg_buf_offset;
3490 sg_buf_offset += ALIGN(bp->length, sizeof(u64));
Martijn Coenen5a6da532016-09-30 14:10:07 +02003491
Todd Kjos2ea90f02019-12-13 12:25:31 -08003492 num_valid = (buffer_offset - off_start_offset) /
Todd Kjos8539b1e2019-02-08 10:35:20 -08003493 sizeof(binder_size_t);
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003494 ret = binder_fixup_parent(t, thread, bp,
3495 off_start_offset,
Todd Kjos8539b1e2019-02-08 10:35:20 -08003496 num_valid,
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003497 last_fixup_obj_off,
Martijn Coenen5a6da532016-09-30 14:10:07 +02003498 last_fixup_min_off);
3499 if (ret < 0) {
3500 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003501 return_error_param = ret;
3502 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003503 goto err_translate_failed;
3504 }
Todd Kjosd73356a2019-02-08 10:35:16 -08003505 binder_alloc_copy_to_buffer(&target_proc->alloc,
3506 t->buffer, object_offset,
3507 bp, sizeof(*bp));
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003508 last_fixup_obj_off = object_offset;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003509 last_fixup_min_off = 0;
3510 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003511 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01003512 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
Martijn Coenen00c80372016-07-13 12:06:49 +02003513 proc->pid, thread->pid, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003514 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003515 return_error_param = -EINVAL;
3516 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003517 goto err_bad_object_type;
3518 }
3519 }
Todd Kjos8dedb0c2017-05-09 08:31:32 -07003520 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003521 t->work.type = BINDER_WORK_TRANSACTION;
Todd Kjos8dedb0c2017-05-09 08:31:32 -07003522
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003523 if (reply) {
Martijn Coenen1af61802017-10-19 15:04:46 +02003524 binder_enqueue_thread_work(thread, tcomplete);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003525 binder_inner_proc_lock(target_proc);
3526 if (target_thread->is_dead) {
3527 binder_inner_proc_unlock(target_proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07003528 goto err_dead_proc_or_thread;
Martijn Coenen995a36e2017-06-02 13:36:52 -07003529 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003530 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003531 binder_pop_transaction_ilocked(target_thread, in_reply_to);
Martijn Coenen1af61802017-10-19 15:04:46 +02003532 binder_enqueue_thread_work_ilocked(target_thread, &t->work);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003533 binder_inner_proc_unlock(target_proc);
Martijn Coenen053be422017-06-06 15:17:46 -07003534 wake_up_interruptible_sync(&target_thread->wait);
Martijn Coenenecd972d2017-05-26 10:48:56 -07003535 binder_restore_priority(current, in_reply_to->saved_priority);
Todd Kjos21ef40a2017-03-30 18:02:13 -07003536 binder_free_transaction(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003537 } else if (!(t->flags & TF_ONE_WAY)) {
3538 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003539 binder_inner_proc_lock(proc);
Martijn Coenendac2e9c2017-11-13 09:55:21 +01003540 /*
3541 * Defer the TRANSACTION_COMPLETE, so we don't return to
3542 * userspace immediately; this allows the target process to
3543 * immediately start processing this transaction, reducing
3544 * latency. We will then return the TRANSACTION_COMPLETE when
3545 * the target replies (or there is an error).
3546 */
3547 binder_enqueue_deferred_thread_work_ilocked(thread, tcomplete);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003548 t->need_reply = 1;
3549 t->from_parent = thread->transaction_stack;
3550 thread->transaction_stack = t;
Martijn Coenen995a36e2017-06-02 13:36:52 -07003551 binder_inner_proc_unlock(proc);
Martijn Coenen053be422017-06-06 15:17:46 -07003552 if (!binder_proc_transaction(t, target_proc, target_thread)) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07003553 binder_inner_proc_lock(proc);
3554 binder_pop_transaction_ilocked(thread, t);
3555 binder_inner_proc_unlock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07003556 goto err_dead_proc_or_thread;
3557 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003558 } else {
3559 BUG_ON(target_node == NULL);
3560 BUG_ON(t->buffer->async_transaction != 1);
Martijn Coenen1af61802017-10-19 15:04:46 +02003561 binder_enqueue_thread_work(thread, tcomplete);
Martijn Coenen053be422017-06-06 15:17:46 -07003562 if (!binder_proc_transaction(t, target_proc, NULL))
Todd Kjos2f993e22017-05-12 14:42:55 -07003563 goto err_dead_proc_or_thread;
Riley Andrewsb5968812015-09-01 12:42:07 -07003564 }
Todd Kjos2f993e22017-05-12 14:42:55 -07003565 if (target_thread)
3566 binder_thread_dec_tmpref(target_thread);
3567 binder_proc_dec_tmpref(target_proc);
Todd Kjos291d9682017-09-25 08:55:09 -07003568 if (target_node)
3569 binder_dec_node_tmpref(target_node);
Todd Kjos1cfe6272017-05-24 13:33:28 -07003570 /*
3571 * write barrier to synchronize with initialization
3572 * of log entry
3573 */
3574 smp_wmb();
3575 WRITE_ONCE(e->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003576 return;
3577
Todd Kjos2f993e22017-05-12 14:42:55 -07003578err_dead_proc_or_thread:
3579 return_error = BR_DEAD_REPLY;
3580 return_error_line = __LINE__;
Xu YiPing86578a02017-05-22 11:26:23 -07003581 binder_dequeue_work(proc, tcomplete);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003582err_translate_failed:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003583err_bad_object_type:
3584err_bad_offset:
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003585err_bad_parent:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003586err_copy_data_failed:
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003587 trace_binder_transaction_failed_buffer_release(t->buffer);
Todd Kjos8539b1e2019-02-08 10:35:20 -08003588 binder_transaction_buffer_release(target_proc, t->buffer,
3589 buffer_offset, true);
Todd Kjos291d9682017-09-25 08:55:09 -07003590 if (target_node)
3591 binder_dec_node_tmpref(target_node);
Todd Kjosc37162d2017-05-26 11:56:29 -07003592 target_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003593 t->buffer->transaction = NULL;
Todd Kjosd325d372016-10-10 10:40:53 -07003594 binder_alloc_free_buf(&target_proc->alloc, t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003595err_binder_alloc_buf_failed:
Todd Kjosb8f2f4b2019-04-24 12:31:18 -07003596err_bad_extra_size:
Todd Kjos63e0afa2019-01-14 09:10:21 -08003597 if (secctx)
3598 security_release_secctx(secctx, secctx_sz);
3599err_get_secctx_failed:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003600 kfree(tcomplete);
3601 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3602err_alloc_tcomplete_failed:
3603 kfree(t);
3604 binder_stats_deleted(BINDER_STAT_TRANSACTION);
3605err_alloc_t_failed:
3606err_bad_call_stack:
3607err_empty_call_stack:
3608err_dead_binder:
3609err_invalid_target_handle:
Todd Kjos2f993e22017-05-12 14:42:55 -07003610 if (target_thread)
3611 binder_thread_dec_tmpref(target_thread);
3612 if (target_proc)
3613 binder_proc_dec_tmpref(target_proc);
Todd Kjos291d9682017-09-25 08:55:09 -07003614 if (target_node) {
Todd Kjosc37162d2017-05-26 11:56:29 -07003615 binder_dec_node(target_node, 1, 0);
Todd Kjos291d9682017-09-25 08:55:09 -07003616 binder_dec_node_tmpref(target_node);
3617 }
Todd Kjosc37162d2017-05-26 11:56:29 -07003618
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003619 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Todd Kjose598d172017-03-22 17:19:52 -07003620 "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
3621 proc->pid, thread->pid, return_error, return_error_param,
3622 (u64)tr->data_size, (u64)tr->offsets_size,
3623 return_error_line);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003624
3625 {
3626 struct binder_transaction_log_entry *fe;
Seunghun Lee10f62862014-05-01 01:30:23 +09003627
Todd Kjose598d172017-03-22 17:19:52 -07003628 e->return_error = return_error;
3629 e->return_error_param = return_error_param;
3630 e->return_error_line = return_error_line;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003631 fe = binder_transaction_log_add(&binder_transaction_log_failed);
3632 *fe = *e;
Todd Kjos1cfe6272017-05-24 13:33:28 -07003633 /*
3634 * write barrier to synchronize with initialization
3635 * of log entry
3636 */
3637 smp_wmb();
3638 WRITE_ONCE(e->debug_id_done, t_debug_id);
3639 WRITE_ONCE(fe->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003640 }
3641
Todd Kjos858b8da2017-04-21 17:35:12 -07003642 BUG_ON(thread->return_error.cmd != BR_OK);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003643 if (in_reply_to) {
Martijn Coenenecd972d2017-05-26 10:48:56 -07003644 binder_restore_priority(current, in_reply_to->saved_priority);
Todd Kjos858b8da2017-04-21 17:35:12 -07003645 thread->return_error.cmd = BR_TRANSACTION_COMPLETE;
Martijn Coenen1af61802017-10-19 15:04:46 +02003646 binder_enqueue_thread_work(thread, &thread->return_error.work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003647 binder_send_failed_reply(in_reply_to, return_error);
Todd Kjos858b8da2017-04-21 17:35:12 -07003648 } else {
3649 thread->return_error.cmd = return_error;
Martijn Coenen1af61802017-10-19 15:04:46 +02003650 binder_enqueue_thread_work(thread, &thread->return_error.work);
Todd Kjos858b8da2017-04-21 17:35:12 -07003651 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003652}
3653
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02003654static int binder_thread_write(struct binder_proc *proc,
3655 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003656 binder_uintptr_t binder_buffer, size_t size,
3657 binder_size_t *consumed)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003658{
3659 uint32_t cmd;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003660 struct binder_context *context = proc->context;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003661 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003662 void __user *ptr = buffer + *consumed;
3663 void __user *end = buffer + size;
3664
Todd Kjos858b8da2017-04-21 17:35:12 -07003665 while (ptr < end && thread->return_error.cmd == BR_OK) {
Todd Kjosb0117bb2017-05-08 09:16:27 -07003666 int ret;
3667
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003668 if (get_user(cmd, (uint32_t __user *)ptr))
3669 return -EFAULT;
3670 ptr += sizeof(uint32_t);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003671 trace_binder_command(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003672 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07003673 atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]);
3674 atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]);
3675 atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003676 }
3677 switch (cmd) {
3678 case BC_INCREFS:
3679 case BC_ACQUIRE:
3680 case BC_RELEASE:
3681 case BC_DECREFS: {
3682 uint32_t target;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003683 const char *debug_string;
Todd Kjosb0117bb2017-05-08 09:16:27 -07003684 bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE;
3685 bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE;
3686 struct binder_ref_data rdata;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003687
3688 if (get_user(target, (uint32_t __user *)ptr))
3689 return -EFAULT;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003690
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003691 ptr += sizeof(uint32_t);
Todd Kjosb0117bb2017-05-08 09:16:27 -07003692 ret = -1;
3693 if (increment && !target) {
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003694 struct binder_node *ctx_mgr_node;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003695 mutex_lock(&context->context_mgr_node_lock);
3696 ctx_mgr_node = context->binder_context_mgr_node;
Todd Kjosb0117bb2017-05-08 09:16:27 -07003697 if (ctx_mgr_node)
3698 ret = binder_inc_ref_for_node(
3699 proc, ctx_mgr_node,
3700 strong, NULL, &rdata);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003701 mutex_unlock(&context->context_mgr_node_lock);
3702 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07003703 if (ret)
3704 ret = binder_update_ref_for_handle(
3705 proc, target, increment, strong,
3706 &rdata);
3707 if (!ret && rdata.desc != target) {
3708 binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
3709 proc->pid, thread->pid,
3710 target, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003711 }
3712 switch (cmd) {
3713 case BC_INCREFS:
3714 debug_string = "IncRefs";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003715 break;
3716 case BC_ACQUIRE:
3717 debug_string = "Acquire";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003718 break;
3719 case BC_RELEASE:
3720 debug_string = "Release";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003721 break;
3722 case BC_DECREFS:
3723 default:
3724 debug_string = "DecRefs";
Todd Kjosb0117bb2017-05-08 09:16:27 -07003725 break;
3726 }
3727 if (ret) {
3728 binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
3729 proc->pid, thread->pid, debug_string,
3730 strong, target, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003731 break;
3732 }
3733 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjosb0117bb2017-05-08 09:16:27 -07003734 "%d:%d %s ref %d desc %d s %d w %d\n",
3735 proc->pid, thread->pid, debug_string,
3736 rdata.debug_id, rdata.desc, rdata.strong,
3737 rdata.weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003738 break;
3739 }
3740 case BC_INCREFS_DONE:
3741 case BC_ACQUIRE_DONE: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003742 binder_uintptr_t node_ptr;
3743 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003744 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003745 bool free_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003746
Arve Hjønnevågda498892014-02-21 14:40:26 -08003747 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003748 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003749 ptr += sizeof(binder_uintptr_t);
3750 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003751 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003752 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003753 node = binder_get_node(proc, node_ptr);
3754 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003755 binder_user_error("%d:%d %s u%016llx no match\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003756 proc->pid, thread->pid,
3757 cmd == BC_INCREFS_DONE ?
3758 "BC_INCREFS_DONE" :
3759 "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003760 (u64)node_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003761 break;
3762 }
3763 if (cookie != node->cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003764 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003765 proc->pid, thread->pid,
3766 cmd == BC_INCREFS_DONE ?
3767 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003768 (u64)node_ptr, node->debug_id,
3769 (u64)cookie, (u64)node->cookie);
Todd Kjosf22abc72017-05-09 11:08:05 -07003770 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003771 break;
3772 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003773 binder_node_inner_lock(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003774 if (cmd == BC_ACQUIRE_DONE) {
3775 if (node->pending_strong_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303776 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003777 proc->pid, thread->pid,
3778 node->debug_id);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003779 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003780 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003781 break;
3782 }
3783 node->pending_strong_ref = 0;
3784 } else {
3785 if (node->pending_weak_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303786 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003787 proc->pid, thread->pid,
3788 node->debug_id);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003789 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003790 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003791 break;
3792 }
3793 node->pending_weak_ref = 0;
3794 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003795 free_node = binder_dec_node_nilocked(node,
3796 cmd == BC_ACQUIRE_DONE, 0);
3797 WARN_ON(free_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003798 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjosf22abc72017-05-09 11:08:05 -07003799 "%d:%d %s node %d ls %d lw %d tr %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003800 proc->pid, thread->pid,
3801 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Todd Kjosf22abc72017-05-09 11:08:05 -07003802 node->debug_id, node->local_strong_refs,
3803 node->local_weak_refs, node->tmp_refs);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003804 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003805 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003806 break;
3807 }
3808 case BC_ATTEMPT_ACQUIRE:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303809 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003810 return -EINVAL;
3811 case BC_ACQUIRE_RESULT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303812 pr_err("BC_ACQUIRE_RESULT not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003813 return -EINVAL;
3814
3815 case BC_FREE_BUFFER: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003816 binder_uintptr_t data_ptr;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003817 struct binder_buffer *buffer;
3818
Arve Hjønnevågda498892014-02-21 14:40:26 -08003819 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003820 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003821 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003822
Todd Kjos076072a2017-04-21 14:32:11 -07003823 buffer = binder_alloc_prepare_to_free(&proc->alloc,
3824 data_ptr);
Todd Kjosd29b73e2018-11-06 15:55:32 -08003825 if (IS_ERR_OR_NULL(buffer)) {
3826 if (PTR_ERR(buffer) == -EPERM) {
3827 binder_user_error(
3828 "%d:%d BC_FREE_BUFFER u%016llx matched unreturned or currently freeing buffer\n",
3829 proc->pid, thread->pid,
3830 (u64)data_ptr);
3831 } else {
3832 binder_user_error(
3833 "%d:%d BC_FREE_BUFFER u%016llx no match\n",
3834 proc->pid, thread->pid,
3835 (u64)data_ptr);
3836 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003837 break;
3838 }
3839 binder_debug(BINDER_DEBUG_FREE_BUFFER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003840 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
3841 proc->pid, thread->pid, (u64)data_ptr,
3842 buffer->debug_id,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003843 buffer->transaction ? "active" : "finished");
3844
Todd Kjosf4e6de32019-06-10 09:14:25 -07003845 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003846 if (buffer->transaction) {
3847 buffer->transaction->buffer = NULL;
3848 buffer->transaction = NULL;
3849 }
Todd Kjosf4e6de32019-06-10 09:14:25 -07003850 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003851 if (buffer->async_transaction && buffer->target_node) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003852 struct binder_node *buf_node;
3853 struct binder_work *w;
3854
3855 buf_node = buffer->target_node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003856 binder_node_inner_lock(buf_node);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003857 BUG_ON(!buf_node->has_async_transaction);
3858 BUG_ON(buf_node->proc != proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003859 w = binder_dequeue_work_head_ilocked(
3860 &buf_node->async_todo);
Martijn Coenen4501c042017-08-10 13:56:16 +02003861 if (!w) {
Gustavo A. R. Silvae62dd6f2018-01-23 12:04:27 -06003862 buf_node->has_async_transaction = false;
Martijn Coenen4501c042017-08-10 13:56:16 +02003863 } else {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003864 binder_enqueue_work_ilocked(
Martijn Coenen4501c042017-08-10 13:56:16 +02003865 w, &proc->todo);
3866 binder_wakeup_proc_ilocked(proc);
3867 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003868 binder_node_inner_unlock(buf_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003869 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003870 trace_binder_transaction_buffer_release(buffer);
Todd Kjos8539b1e2019-02-08 10:35:20 -08003871 binder_transaction_buffer_release(proc, buffer, 0, false);
Todd Kjosd325d372016-10-10 10:40:53 -07003872 binder_alloc_free_buf(&proc->alloc, buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003873 break;
3874 }
3875
Martijn Coenen5a6da532016-09-30 14:10:07 +02003876 case BC_TRANSACTION_SG:
3877 case BC_REPLY_SG: {
3878 struct binder_transaction_data_sg tr;
3879
3880 if (copy_from_user(&tr, ptr, sizeof(tr)))
3881 return -EFAULT;
3882 ptr += sizeof(tr);
3883 binder_transaction(proc, thread, &tr.transaction_data,
3884 cmd == BC_REPLY_SG, tr.buffers_size);
3885 break;
3886 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003887 case BC_TRANSACTION:
3888 case BC_REPLY: {
3889 struct binder_transaction_data tr;
3890
3891 if (copy_from_user(&tr, ptr, sizeof(tr)))
3892 return -EFAULT;
3893 ptr += sizeof(tr);
Martijn Coenen59878d72016-09-30 14:05:40 +02003894 binder_transaction(proc, thread, &tr,
3895 cmd == BC_REPLY, 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003896 break;
3897 }
3898
3899 case BC_REGISTER_LOOPER:
3900 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303901 "%d:%d BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003902 proc->pid, thread->pid);
Todd Kjosd600e902017-05-25 17:35:02 -07003903 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003904 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
3905 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303906 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003907 proc->pid, thread->pid);
3908 } else if (proc->requested_threads == 0) {
3909 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303910 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003911 proc->pid, thread->pid);
3912 } else {
3913 proc->requested_threads--;
3914 proc->requested_threads_started++;
3915 }
3916 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
Todd Kjosd600e902017-05-25 17:35:02 -07003917 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003918 break;
3919 case BC_ENTER_LOOPER:
3920 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303921 "%d:%d BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003922 proc->pid, thread->pid);
3923 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
3924 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303925 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003926 proc->pid, thread->pid);
3927 }
3928 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
3929 break;
3930 case BC_EXIT_LOOPER:
3931 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303932 "%d:%d BC_EXIT_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003933 proc->pid, thread->pid);
3934 thread->looper |= BINDER_LOOPER_STATE_EXITED;
3935 break;
3936
3937 case BC_REQUEST_DEATH_NOTIFICATION:
3938 case BC_CLEAR_DEATH_NOTIFICATION: {
3939 uint32_t target;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003940 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003941 struct binder_ref *ref;
Todd Kjos5346bf32016-10-20 16:43:34 -07003942 struct binder_ref_death *death = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003943
3944 if (get_user(target, (uint32_t __user *)ptr))
3945 return -EFAULT;
3946 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08003947 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003948 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003949 ptr += sizeof(binder_uintptr_t);
Todd Kjos5346bf32016-10-20 16:43:34 -07003950 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3951 /*
3952 * Allocate memory for death notification
3953 * before taking lock
3954 */
3955 death = kzalloc(sizeof(*death), GFP_KERNEL);
3956 if (death == NULL) {
3957 WARN_ON(thread->return_error.cmd !=
3958 BR_OK);
3959 thread->return_error.cmd = BR_ERROR;
Martijn Coenen1af61802017-10-19 15:04:46 +02003960 binder_enqueue_thread_work(
3961 thread,
3962 &thread->return_error.work);
Todd Kjos5346bf32016-10-20 16:43:34 -07003963 binder_debug(
3964 BINDER_DEBUG_FAILED_TRANSACTION,
3965 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
3966 proc->pid, thread->pid);
3967 break;
3968 }
3969 }
3970 binder_proc_lock(proc);
3971 ref = binder_get_ref_olocked(proc, target, false);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003972 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303973 binder_user_error("%d:%d %s invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003974 proc->pid, thread->pid,
3975 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3976 "BC_REQUEST_DEATH_NOTIFICATION" :
3977 "BC_CLEAR_DEATH_NOTIFICATION",
3978 target);
Todd Kjos5346bf32016-10-20 16:43:34 -07003979 binder_proc_unlock(proc);
3980 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003981 break;
3982 }
3983
3984 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003985 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003986 proc->pid, thread->pid,
3987 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3988 "BC_REQUEST_DEATH_NOTIFICATION" :
3989 "BC_CLEAR_DEATH_NOTIFICATION",
Todd Kjosb0117bb2017-05-08 09:16:27 -07003990 (u64)cookie, ref->data.debug_id,
3991 ref->data.desc, ref->data.strong,
3992 ref->data.weak, ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003993
Martijn Coenenf9eac642017-05-22 11:26:23 -07003994 binder_node_lock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003995 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3996 if (ref->death) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303997 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003998 proc->pid, thread->pid);
Martijn Coenenf9eac642017-05-22 11:26:23 -07003999 binder_node_unlock(ref->node);
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 binder_stats_created(BINDER_STAT_DEATH);
4005 INIT_LIST_HEAD(&death->work.entry);
4006 death->cookie = cookie;
4007 ref->death = death;
4008 if (ref->node->proc == NULL) {
4009 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
Martijn Coenen3bdbe4c2017-08-10 13:50:52 +02004010
4011 binder_inner_proc_lock(proc);
4012 binder_enqueue_work_ilocked(
4013 &ref->death->work, &proc->todo);
4014 binder_wakeup_proc_ilocked(proc);
4015 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004016 }
4017 } else {
4018 if (ref->death == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05304019 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004020 proc->pid, thread->pid);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004021 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07004022 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004023 break;
4024 }
4025 death = ref->death;
4026 if (death->cookie != cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08004027 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004028 proc->pid, thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004029 (u64)death->cookie,
4030 (u64)cookie);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004031 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07004032 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004033 break;
4034 }
4035 ref->death = NULL;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004036 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004037 if (list_empty(&death->work.entry)) {
4038 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004039 if (thread->looper &
4040 (BINDER_LOOPER_STATE_REGISTERED |
4041 BINDER_LOOPER_STATE_ENTERED))
Martijn Coenen1af61802017-10-19 15:04:46 +02004042 binder_enqueue_thread_work_ilocked(
4043 thread,
4044 &death->work);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004045 else {
4046 binder_enqueue_work_ilocked(
4047 &death->work,
4048 &proc->todo);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004049 binder_wakeup_proc_ilocked(
Martijn Coenen053be422017-06-06 15:17:46 -07004050 proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004051 }
4052 } else {
4053 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
4054 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
4055 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004056 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004057 }
Martijn Coenenf9eac642017-05-22 11:26:23 -07004058 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07004059 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004060 } break;
4061 case BC_DEAD_BINDER_DONE: {
4062 struct binder_work *w;
Arve Hjønnevågda498892014-02-21 14:40:26 -08004063 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004064 struct binder_ref_death *death = NULL;
Seunghun Lee10f62862014-05-01 01:30:23 +09004065
Arve Hjønnevågda498892014-02-21 14:40:26 -08004066 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004067 return -EFAULT;
4068
Lisa Du7a64cd82016-02-17 09:32:52 +08004069 ptr += sizeof(cookie);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004070 binder_inner_proc_lock(proc);
4071 list_for_each_entry(w, &proc->delivered_death,
4072 entry) {
4073 struct binder_ref_death *tmp_death =
4074 container_of(w,
4075 struct binder_ref_death,
4076 work);
Seunghun Lee10f62862014-05-01 01:30:23 +09004077
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004078 if (tmp_death->cookie == cookie) {
4079 death = tmp_death;
4080 break;
4081 }
4082 }
4083 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Todd Kjosf540ce02018-02-07 13:57:37 -08004084 "%d:%d BC_DEAD_BINDER_DONE %016llx found %pK\n",
Arve Hjønnevågda498892014-02-21 14:40:26 -08004085 proc->pid, thread->pid, (u64)cookie,
4086 death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004087 if (death == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08004088 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
4089 proc->pid, thread->pid, (u64)cookie);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004090 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004091 break;
4092 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004093 binder_dequeue_work_ilocked(&death->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004094 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
4095 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004096 if (thread->looper &
4097 (BINDER_LOOPER_STATE_REGISTERED |
4098 BINDER_LOOPER_STATE_ENTERED))
Martijn Coenen1af61802017-10-19 15:04:46 +02004099 binder_enqueue_thread_work_ilocked(
4100 thread, &death->work);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004101 else {
4102 binder_enqueue_work_ilocked(
4103 &death->work,
4104 &proc->todo);
Martijn Coenen053be422017-06-06 15:17:46 -07004105 binder_wakeup_proc_ilocked(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004106 }
4107 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004108 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004109 } break;
4110
4111 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304112 pr_err("%d:%d unknown command %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004113 proc->pid, thread->pid, cmd);
4114 return -EINVAL;
4115 }
4116 *consumed = ptr - buffer;
4117 }
4118 return 0;
4119}
4120
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02004121static void binder_stat_br(struct binder_proc *proc,
4122 struct binder_thread *thread, uint32_t cmd)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004123{
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004124 trace_binder_return(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004125 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07004126 atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
4127 atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
4128 atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004129 }
4130}
4131
Todd Kjos60792612017-05-24 10:51:01 -07004132static int binder_put_node_cmd(struct binder_proc *proc,
4133 struct binder_thread *thread,
4134 void __user **ptrp,
4135 binder_uintptr_t node_ptr,
4136 binder_uintptr_t node_cookie,
4137 int node_debug_id,
4138 uint32_t cmd, const char *cmd_name)
4139{
4140 void __user *ptr = *ptrp;
4141
4142 if (put_user(cmd, (uint32_t __user *)ptr))
4143 return -EFAULT;
4144 ptr += sizeof(uint32_t);
4145
4146 if (put_user(node_ptr, (binder_uintptr_t __user *)ptr))
4147 return -EFAULT;
4148 ptr += sizeof(binder_uintptr_t);
4149
4150 if (put_user(node_cookie, (binder_uintptr_t __user *)ptr))
4151 return -EFAULT;
4152 ptr += sizeof(binder_uintptr_t);
4153
4154 binder_stat_br(proc, thread, cmd);
4155 binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n",
4156 proc->pid, thread->pid, cmd_name, node_debug_id,
4157 (u64)node_ptr, (u64)node_cookie);
4158
4159 *ptrp = ptr;
4160 return 0;
4161}
4162
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004163static int binder_wait_for_work(struct binder_thread *thread,
4164 bool do_proc_work)
4165{
4166 DEFINE_WAIT(wait);
4167 struct binder_proc *proc = thread->proc;
4168 int ret = 0;
4169
4170 freezer_do_not_count();
4171 binder_inner_proc_lock(proc);
4172 for (;;) {
4173 prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE);
4174 if (binder_has_work_ilocked(thread, do_proc_work))
4175 break;
4176 if (do_proc_work)
4177 list_add(&thread->waiting_thread_node,
4178 &proc->waiting_threads);
4179 binder_inner_proc_unlock(proc);
4180 schedule();
4181 binder_inner_proc_lock(proc);
4182 list_del_init(&thread->waiting_thread_node);
4183 if (signal_pending(current)) {
4184 ret = -ERESTARTSYS;
4185 break;
4186 }
4187 }
4188 finish_wait(&thread->wait, &wait);
4189 binder_inner_proc_unlock(proc);
4190 freezer_count();
4191
4192 return ret;
4193}
4194
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004195static int binder_thread_read(struct binder_proc *proc,
4196 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004197 binder_uintptr_t binder_buffer, size_t size,
4198 binder_size_t *consumed, int non_block)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004199{
Arve Hjønnevågda498892014-02-21 14:40:26 -08004200 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004201 void __user *ptr = buffer + *consumed;
4202 void __user *end = buffer + size;
4203
4204 int ret = 0;
4205 int wait_for_proc_work;
4206
4207 if (*consumed == 0) {
4208 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
4209 return -EFAULT;
4210 ptr += sizeof(uint32_t);
4211 }
4212
4213retry:
Martijn Coenen995a36e2017-06-02 13:36:52 -07004214 binder_inner_proc_lock(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004215 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
Martijn Coenen995a36e2017-06-02 13:36:52 -07004216 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004217
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004218 thread->looper |= BINDER_LOOPER_STATE_WAITING;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004219
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004220 trace_binder_wait_for_work(wait_for_proc_work,
4221 !!thread->transaction_stack,
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004222 !binder_worklist_empty(proc, &thread->todo));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004223 if (wait_for_proc_work) {
4224 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4225 BINDER_LOOPER_STATE_ENTERED))) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05304226 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 +09004227 proc->pid, thread->pid, thread->looper);
4228 wait_event_interruptible(binder_user_error_wait,
4229 binder_stop_on_user_error < 2);
4230 }
Martijn Coenenecd972d2017-05-26 10:48:56 -07004231 binder_restore_priority(current, proc->default_priority);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004232 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004233
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004234 if (non_block) {
4235 if (!binder_has_work(thread, wait_for_proc_work))
4236 ret = -EAGAIN;
4237 } else {
4238 ret = binder_wait_for_work(thread, wait_for_proc_work);
4239 }
4240
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004241 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
4242
4243 if (ret)
4244 return ret;
4245
4246 while (1) {
4247 uint32_t cmd;
Todd Kjos63e0afa2019-01-14 09:10:21 -08004248 struct binder_transaction_data_secctx tr;
4249 struct binder_transaction_data *trd = &tr.transaction_data;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004250 struct binder_work *w = NULL;
4251 struct list_head *list = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004252 struct binder_transaction *t = NULL;
Todd Kjos2f993e22017-05-12 14:42:55 -07004253 struct binder_thread *t_from;
Todd Kjos63e0afa2019-01-14 09:10:21 -08004254 size_t trsize = sizeof(*trd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004255
Todd Kjose7f23ed2017-03-21 13:06:01 -07004256 binder_inner_proc_lock(proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004257 if (!binder_worklist_empty_ilocked(&thread->todo))
4258 list = &thread->todo;
4259 else if (!binder_worklist_empty_ilocked(&proc->todo) &&
4260 wait_for_proc_work)
4261 list = &proc->todo;
4262 else {
4263 binder_inner_proc_unlock(proc);
4264
Dmitry Voytik395262a2014-09-08 18:16:34 +04004265 /* no data added */
Todd Kjos6798e6d2017-01-06 14:19:25 -08004266 if (ptr - buffer == 4 && !thread->looper_need_return)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004267 goto retry;
4268 break;
4269 }
4270
Todd Kjose7f23ed2017-03-21 13:06:01 -07004271 if (end - ptr < sizeof(tr) + 4) {
4272 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004273 break;
Todd Kjose7f23ed2017-03-21 13:06:01 -07004274 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004275 w = binder_dequeue_work_head_ilocked(list);
Martijn Coenen1af61802017-10-19 15:04:46 +02004276 if (binder_worklist_empty_ilocked(&thread->todo))
4277 thread->process_todo = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004278
4279 switch (w->type) {
4280 case BINDER_WORK_TRANSACTION: {
Todd Kjose7f23ed2017-03-21 13:06:01 -07004281 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004282 t = container_of(w, struct binder_transaction, work);
4283 } break;
Todd Kjos858b8da2017-04-21 17:35:12 -07004284 case BINDER_WORK_RETURN_ERROR: {
4285 struct binder_error *e = container_of(
4286 w, struct binder_error, work);
4287
4288 WARN_ON(e->cmd == BR_OK);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004289 binder_inner_proc_unlock(proc);
Todd Kjos858b8da2017-04-21 17:35:12 -07004290 if (put_user(e->cmd, (uint32_t __user *)ptr))
4291 return -EFAULT;
宋金时e1b1a8b2018-05-10 02:05:03 +00004292 cmd = e->cmd;
Todd Kjos858b8da2017-04-21 17:35:12 -07004293 e->cmd = BR_OK;
4294 ptr += sizeof(uint32_t);
4295
4296 binder_stat_br(proc, thread, cmd);
Todd Kjos858b8da2017-04-21 17:35:12 -07004297 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004298 case BINDER_WORK_TRANSACTION_COMPLETE: {
Todd Kjose7f23ed2017-03-21 13:06:01 -07004299 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004300 cmd = BR_TRANSACTION_COMPLETE;
4301 if (put_user(cmd, (uint32_t __user *)ptr))
4302 return -EFAULT;
4303 ptr += sizeof(uint32_t);
4304
4305 binder_stat_br(proc, thread, cmd);
4306 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304307 "%d:%d BR_TRANSACTION_COMPLETE\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004308 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004309 kfree(w);
4310 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4311 } break;
4312 case BINDER_WORK_NODE: {
4313 struct binder_node *node = container_of(w, struct binder_node, work);
Todd Kjos60792612017-05-24 10:51:01 -07004314 int strong, weak;
4315 binder_uintptr_t node_ptr = node->ptr;
4316 binder_uintptr_t node_cookie = node->cookie;
4317 int node_debug_id = node->debug_id;
4318 int has_weak_ref;
4319 int has_strong_ref;
4320 void __user *orig_ptr = ptr;
Seunghun Lee10f62862014-05-01 01:30:23 +09004321
Todd Kjos60792612017-05-24 10:51:01 -07004322 BUG_ON(proc != node->proc);
4323 strong = node->internal_strong_refs ||
4324 node->local_strong_refs;
4325 weak = !hlist_empty(&node->refs) ||
Todd Kjosf22abc72017-05-09 11:08:05 -07004326 node->local_weak_refs ||
4327 node->tmp_refs || strong;
Todd Kjos60792612017-05-24 10:51:01 -07004328 has_strong_ref = node->has_strong_ref;
4329 has_weak_ref = node->has_weak_ref;
4330
4331 if (weak && !has_weak_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004332 node->has_weak_ref = 1;
4333 node->pending_weak_ref = 1;
4334 node->local_weak_refs++;
Todd Kjos60792612017-05-24 10:51:01 -07004335 }
4336 if (strong && !has_strong_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004337 node->has_strong_ref = 1;
4338 node->pending_strong_ref = 1;
4339 node->local_strong_refs++;
Todd Kjos60792612017-05-24 10:51:01 -07004340 }
4341 if (!strong && has_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004342 node->has_strong_ref = 0;
Todd Kjos60792612017-05-24 10:51:01 -07004343 if (!weak && has_weak_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004344 node->has_weak_ref = 0;
Todd Kjos60792612017-05-24 10:51:01 -07004345 if (!weak && !strong) {
4346 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4347 "%d:%d node %d u%016llx c%016llx deleted\n",
4348 proc->pid, thread->pid,
4349 node_debug_id,
4350 (u64)node_ptr,
4351 (u64)node_cookie);
4352 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004353 binder_inner_proc_unlock(proc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004354 binder_node_lock(node);
4355 /*
4356 * Acquire the node lock before freeing the
4357 * node to serialize with other threads that
4358 * may have been holding the node lock while
4359 * decrementing this node (avoids race where
4360 * this thread frees while the other thread
4361 * is unlocking the node after the final
4362 * decrement)
4363 */
4364 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004365 binder_free_node(node);
4366 } else
4367 binder_inner_proc_unlock(proc);
4368
Todd Kjos60792612017-05-24 10:51:01 -07004369 if (weak && !has_weak_ref)
4370 ret = binder_put_node_cmd(
4371 proc, thread, &ptr, node_ptr,
4372 node_cookie, node_debug_id,
4373 BR_INCREFS, "BR_INCREFS");
4374 if (!ret && strong && !has_strong_ref)
4375 ret = binder_put_node_cmd(
4376 proc, thread, &ptr, node_ptr,
4377 node_cookie, node_debug_id,
4378 BR_ACQUIRE, "BR_ACQUIRE");
4379 if (!ret && !strong && has_strong_ref)
4380 ret = binder_put_node_cmd(
4381 proc, thread, &ptr, node_ptr,
4382 node_cookie, node_debug_id,
4383 BR_RELEASE, "BR_RELEASE");
4384 if (!ret && !weak && has_weak_ref)
4385 ret = binder_put_node_cmd(
4386 proc, thread, &ptr, node_ptr,
4387 node_cookie, node_debug_id,
4388 BR_DECREFS, "BR_DECREFS");
4389 if (orig_ptr == ptr)
4390 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4391 "%d:%d node %d u%016llx c%016llx state unchanged\n",
4392 proc->pid, thread->pid,
4393 node_debug_id,
4394 (u64)node_ptr,
4395 (u64)node_cookie);
4396 if (ret)
4397 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004398 } break;
4399 case BINDER_WORK_DEAD_BINDER:
4400 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4401 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4402 struct binder_ref_death *death;
4403 uint32_t cmd;
Martijn Coenenf9eac642017-05-22 11:26:23 -07004404 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004405
4406 death = container_of(w, struct binder_ref_death, work);
4407 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
4408 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
4409 else
4410 cmd = BR_DEAD_BINDER;
Martijn Coenenf9eac642017-05-22 11:26:23 -07004411 cookie = death->cookie;
4412
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004413 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004414 "%d:%d %s %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004415 proc->pid, thread->pid,
4416 cmd == BR_DEAD_BINDER ?
4417 "BR_DEAD_BINDER" :
4418 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
Martijn Coenenf9eac642017-05-22 11:26:23 -07004419 (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004420 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
Martijn Coenenf9eac642017-05-22 11:26:23 -07004421 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004422 kfree(death);
4423 binder_stats_deleted(BINDER_STAT_DEATH);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004424 } else {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004425 binder_enqueue_work_ilocked(
4426 w, &proc->delivered_death);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004427 binder_inner_proc_unlock(proc);
4428 }
Martijn Coenenf9eac642017-05-22 11:26:23 -07004429 if (put_user(cmd, (uint32_t __user *)ptr))
4430 return -EFAULT;
4431 ptr += sizeof(uint32_t);
4432 if (put_user(cookie,
4433 (binder_uintptr_t __user *)ptr))
4434 return -EFAULT;
4435 ptr += sizeof(binder_uintptr_t);
4436 binder_stat_br(proc, thread, cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004437 if (cmd == BR_DEAD_BINDER)
4438 goto done; /* DEAD_BINDER notifications can cause transactions */
4439 } break;
4440 }
4441
4442 if (!t)
4443 continue;
4444
4445 BUG_ON(t->buffer == NULL);
4446 if (t->buffer->target_node) {
4447 struct binder_node *target_node = t->buffer->target_node;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004448 struct binder_priority node_prio;
Seunghun Lee10f62862014-05-01 01:30:23 +09004449
Todd Kjos63e0afa2019-01-14 09:10:21 -08004450 trd->target.ptr = target_node->ptr;
4451 trd->cookie = target_node->cookie;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004452 node_prio.sched_policy = target_node->sched_policy;
4453 node_prio.prio = target_node->min_priority;
Martijn Coenenc46810c2017-06-23 10:13:43 -07004454 binder_transaction_priority(current, t, node_prio,
4455 target_node->inherit_rt);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004456 cmd = BR_TRANSACTION;
4457 } else {
Todd Kjos63e0afa2019-01-14 09:10:21 -08004458 trd->target.ptr = 0;
4459 trd->cookie = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004460 cmd = BR_REPLY;
4461 }
Todd Kjos63e0afa2019-01-14 09:10:21 -08004462 trd->code = t->code;
4463 trd->flags = t->flags;
4464 trd->sender_euid = from_kuid(current_user_ns(), t->sender_euid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004465
Todd Kjos2f993e22017-05-12 14:42:55 -07004466 t_from = binder_get_txn_from(t);
4467 if (t_from) {
4468 struct task_struct *sender = t_from->proc->tsk;
Seunghun Lee10f62862014-05-01 01:30:23 +09004469
Todd Kjos63e0afa2019-01-14 09:10:21 -08004470 trd->sender_pid =
4471 task_tgid_nr_ns(sender,
4472 task_active_pid_ns(current));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004473 } else {
Todd Kjos63e0afa2019-01-14 09:10:21 -08004474 trd->sender_pid = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004475 }
4476
Todd Kjos63e0afa2019-01-14 09:10:21 -08004477 trd->data_size = t->buffer->data_size;
4478 trd->offsets_size = t->buffer->offsets_size;
Todd Kjos8539b1e2019-02-08 10:35:20 -08004479 trd->data.ptr.buffer = (uintptr_t)t->buffer->user_data;
Todd Kjos63e0afa2019-01-14 09:10:21 -08004480 trd->data.ptr.offsets = trd->data.ptr.buffer +
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004481 ALIGN(t->buffer->data_size,
4482 sizeof(void *));
4483
Todd Kjos63e0afa2019-01-14 09:10:21 -08004484 tr.secctx = t->security_ctx;
4485 if (t->security_ctx) {
4486 cmd = BR_TRANSACTION_SEC_CTX;
4487 trsize = sizeof(tr);
4488 }
Todd Kjos2f993e22017-05-12 14:42:55 -07004489 if (put_user(cmd, (uint32_t __user *)ptr)) {
4490 if (t_from)
4491 binder_thread_dec_tmpref(t_from);
Martijn Coenen3217ccc2017-08-24 15:23:36 +02004492
4493 binder_cleanup_transaction(t, "put_user failed",
4494 BR_FAILED_REPLY);
4495
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004496 return -EFAULT;
Todd Kjos2f993e22017-05-12 14:42:55 -07004497 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004498 ptr += sizeof(uint32_t);
Todd Kjos63e0afa2019-01-14 09:10:21 -08004499 if (copy_to_user(ptr, &tr, trsize)) {
Todd Kjos2f993e22017-05-12 14:42:55 -07004500 if (t_from)
4501 binder_thread_dec_tmpref(t_from);
Martijn Coenen3217ccc2017-08-24 15:23:36 +02004502
4503 binder_cleanup_transaction(t, "copy_to_user failed",
4504 BR_FAILED_REPLY);
4505
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004506 return -EFAULT;
Todd Kjos2f993e22017-05-12 14:42:55 -07004507 }
Todd Kjos63e0afa2019-01-14 09:10:21 -08004508 ptr += trsize;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004509
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004510 trace_binder_transaction_received(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004511 binder_stat_br(proc, thread, cmd);
4512 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004513 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004514 proc->pid, thread->pid,
4515 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
Todd Kjos63e0afa2019-01-14 09:10:21 -08004516 (cmd == BR_TRANSACTION_SEC_CTX) ?
4517 "BR_TRANSACTION_SEC_CTX" : "BR_REPLY",
Todd Kjos2f993e22017-05-12 14:42:55 -07004518 t->debug_id, t_from ? t_from->proc->pid : 0,
4519 t_from ? t_from->pid : 0, cmd,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004520 t->buffer->data_size, t->buffer->offsets_size,
Todd Kjos63e0afa2019-01-14 09:10:21 -08004521 (u64)trd->data.ptr.buffer,
4522 (u64)trd->data.ptr.offsets);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004523
Todd Kjos2f993e22017-05-12 14:42:55 -07004524 if (t_from)
4525 binder_thread_dec_tmpref(t_from);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004526 t->buffer->allow_user_free = 1;
Todd Kjos63e0afa2019-01-14 09:10:21 -08004527 if (cmd != BR_REPLY && !(t->flags & TF_ONE_WAY)) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07004528 binder_inner_proc_lock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004529 t->to_parent = thread->transaction_stack;
4530 t->to_thread = thread;
4531 thread->transaction_stack = t;
Martijn Coenen995a36e2017-06-02 13:36:52 -07004532 binder_inner_proc_unlock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004533 } else {
Todd Kjos21ef40a2017-03-30 18:02:13 -07004534 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004535 }
4536 break;
4537 }
4538
4539done:
4540
4541 *consumed = ptr - buffer;
Todd Kjosd600e902017-05-25 17:35:02 -07004542 binder_inner_proc_lock(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004543 if (proc->requested_threads == 0 &&
4544 list_empty(&thread->proc->waiting_threads) &&
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004545 proc->requested_threads_started < proc->max_threads &&
4546 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4547 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
4548 /*spawn a new thread if we leave this out */) {
4549 proc->requested_threads++;
Todd Kjosd600e902017-05-25 17:35:02 -07004550 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004551 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304552 "%d:%d BR_SPAWN_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004553 proc->pid, thread->pid);
4554 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
4555 return -EFAULT;
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07004556 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
Todd Kjosd600e902017-05-25 17:35:02 -07004557 } else
4558 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004559 return 0;
4560}
4561
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004562static void binder_release_work(struct binder_proc *proc,
4563 struct list_head *list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004564{
4565 struct binder_work *w;
Todd Kjosa18908e2020-07-20 21:14:33 -07004566 enum binder_work_type wtype;
Seunghun Lee10f62862014-05-01 01:30:23 +09004567
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004568 while (1) {
Todd Kjosa18908e2020-07-20 21:14:33 -07004569 binder_inner_proc_lock(proc);
4570 w = binder_dequeue_work_head_ilocked(list);
4571 wtype = w ? w->type : 0;
4572 binder_inner_proc_unlock(proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004573 if (!w)
4574 return;
4575
Todd Kjosa18908e2020-07-20 21:14:33 -07004576 switch (wtype) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004577 case BINDER_WORK_TRANSACTION: {
4578 struct binder_transaction *t;
4579
4580 t = container_of(w, struct binder_transaction, work);
Martijn Coenen3217ccc2017-08-24 15:23:36 +02004581
4582 binder_cleanup_transaction(t, "process died.",
4583 BR_DEAD_REPLY);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004584 } break;
Todd Kjos858b8da2017-04-21 17:35:12 -07004585 case BINDER_WORK_RETURN_ERROR: {
4586 struct binder_error *e = container_of(
4587 w, struct binder_error, work);
4588
4589 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4590 "undelivered TRANSACTION_ERROR: %u\n",
4591 e->cmd);
4592 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004593 case BINDER_WORK_TRANSACTION_COMPLETE: {
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004594 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304595 "undelivered TRANSACTION_COMPLETE\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004596 kfree(w);
4597 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4598 } break;
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004599 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4600 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4601 struct binder_ref_death *death;
4602
4603 death = container_of(w, struct binder_ref_death, work);
4604 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004605 "undelivered death notification, %016llx\n",
4606 (u64)death->cookie);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004607 kfree(death);
4608 binder_stats_deleted(BINDER_STAT_DEATH);
4609 } break;
Todd Kjosa18908e2020-07-20 21:14:33 -07004610 case BINDER_WORK_NODE:
4611 break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004612 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304613 pr_err("unexpected work type, %d, not freed\n",
Todd Kjosa18908e2020-07-20 21:14:33 -07004614 wtype);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004615 break;
4616 }
4617 }
4618
4619}
4620
Todd Kjosb4827902017-05-25 15:52:17 -07004621static struct binder_thread *binder_get_thread_ilocked(
4622 struct binder_proc *proc, struct binder_thread *new_thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004623{
4624 struct binder_thread *thread = NULL;
4625 struct rb_node *parent = NULL;
4626 struct rb_node **p = &proc->threads.rb_node;
4627
4628 while (*p) {
4629 parent = *p;
4630 thread = rb_entry(parent, struct binder_thread, rb_node);
4631
4632 if (current->pid < thread->pid)
4633 p = &(*p)->rb_left;
4634 else if (current->pid > thread->pid)
4635 p = &(*p)->rb_right;
4636 else
Todd Kjosb4827902017-05-25 15:52:17 -07004637 return thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004638 }
Todd Kjosb4827902017-05-25 15:52:17 -07004639 if (!new_thread)
4640 return NULL;
4641 thread = new_thread;
4642 binder_stats_created(BINDER_STAT_THREAD);
4643 thread->proc = proc;
4644 thread->pid = current->pid;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004645 get_task_struct(current);
4646 thread->task = current;
Todd Kjosb4827902017-05-25 15:52:17 -07004647 atomic_set(&thread->tmp_ref, 0);
4648 init_waitqueue_head(&thread->wait);
4649 INIT_LIST_HEAD(&thread->todo);
4650 rb_link_node(&thread->rb_node, parent, p);
4651 rb_insert_color(&thread->rb_node, &proc->threads);
4652 thread->looper_need_return = true;
4653 thread->return_error.work.type = BINDER_WORK_RETURN_ERROR;
4654 thread->return_error.cmd = BR_OK;
4655 thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR;
4656 thread->reply_error.cmd = BR_OK;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004657 INIT_LIST_HEAD(&new_thread->waiting_thread_node);
Todd Kjosb4827902017-05-25 15:52:17 -07004658 return thread;
4659}
4660
4661static struct binder_thread *binder_get_thread(struct binder_proc *proc)
4662{
4663 struct binder_thread *thread;
4664 struct binder_thread *new_thread;
4665
4666 binder_inner_proc_lock(proc);
4667 thread = binder_get_thread_ilocked(proc, NULL);
4668 binder_inner_proc_unlock(proc);
4669 if (!thread) {
4670 new_thread = kzalloc(sizeof(*thread), GFP_KERNEL);
4671 if (new_thread == NULL)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004672 return NULL;
Todd Kjosb4827902017-05-25 15:52:17 -07004673 binder_inner_proc_lock(proc);
4674 thread = binder_get_thread_ilocked(proc, new_thread);
4675 binder_inner_proc_unlock(proc);
4676 if (thread != new_thread)
4677 kfree(new_thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004678 }
4679 return thread;
4680}
4681
Todd Kjos2f993e22017-05-12 14:42:55 -07004682static void binder_free_proc(struct binder_proc *proc)
4683{
4684 BUG_ON(!list_empty(&proc->todo));
4685 BUG_ON(!list_empty(&proc->delivered_death));
4686 binder_alloc_deferred_release(&proc->alloc);
4687 put_task_struct(proc->tsk);
4688 binder_stats_deleted(BINDER_STAT_PROC);
4689 kfree(proc);
4690}
4691
4692static void binder_free_thread(struct binder_thread *thread)
4693{
4694 BUG_ON(!list_empty(&thread->todo));
4695 binder_stats_deleted(BINDER_STAT_THREAD);
4696 binder_proc_dec_tmpref(thread->proc);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004697 put_task_struct(thread->task);
Todd Kjos2f993e22017-05-12 14:42:55 -07004698 kfree(thread);
4699}
4700
4701static int binder_thread_release(struct binder_proc *proc,
4702 struct binder_thread *thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004703{
4704 struct binder_transaction *t;
4705 struct binder_transaction *send_reply = NULL;
4706 int active_transactions = 0;
Todd Kjos2f993e22017-05-12 14:42:55 -07004707 struct binder_transaction *last_t = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004708
Todd Kjosb4827902017-05-25 15:52:17 -07004709 binder_inner_proc_lock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07004710 /*
4711 * take a ref on the proc so it survives
4712 * after we remove this thread from proc->threads.
4713 * The corresponding dec is when we actually
4714 * free the thread in binder_free_thread()
4715 */
Todd Kjosf4e6de32019-06-10 09:14:25 -07004716 atomic_inc(&proc->tmp_ref);
Todd Kjos2f993e22017-05-12 14:42:55 -07004717 /*
4718 * take a ref on this thread to ensure it
4719 * survives while we are releasing it
4720 */
4721 atomic_inc(&thread->tmp_ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004722 rb_erase(&thread->rb_node, &proc->threads);
4723 t = thread->transaction_stack;
Todd Kjos2f993e22017-05-12 14:42:55 -07004724 if (t) {
4725 spin_lock(&t->lock);
4726 if (t->to_thread == thread)
4727 send_reply = t;
4728 }
4729 thread->is_dead = true;
4730
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004731 while (t) {
Todd Kjos2f993e22017-05-12 14:42:55 -07004732 last_t = t;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004733 active_transactions++;
4734 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304735 "release %d:%d transaction %d %s, still active\n",
4736 proc->pid, thread->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004737 t->debug_id,
4738 (t->to_thread == thread) ? "in" : "out");
4739
4740 if (t->to_thread == thread) {
4741 t->to_proc = NULL;
4742 t->to_thread = NULL;
4743 if (t->buffer) {
4744 t->buffer->transaction = NULL;
4745 t->buffer = NULL;
4746 }
4747 t = t->to_parent;
4748 } else if (t->from == thread) {
4749 t->from = NULL;
4750 t = t->from_parent;
4751 } else
4752 BUG();
Todd Kjos2f993e22017-05-12 14:42:55 -07004753 spin_unlock(&last_t->lock);
4754 if (t)
4755 spin_lock(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004756 }
Martijn Coenen550c01d2018-01-05 11:27:07 +01004757
4758 /*
4759 * If this thread used poll, make sure we remove the waitqueue
4760 * from any epoll data structures holding it with POLLFREE.
4761 * waitqueue_active() is safe to use here because we're holding
4762 * the inner lock.
4763 */
4764 if ((thread->looper & BINDER_LOOPER_STATE_POLL) &&
4765 waitqueue_active(&thread->wait)) {
4766 wake_up_poll(&thread->wait, POLLHUP | POLLFREE);
4767 }
4768
Todd Kjosb4827902017-05-25 15:52:17 -07004769 binder_inner_proc_unlock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07004770
Martijn Coenen72766d72018-02-16 09:47:15 +01004771 /*
4772 * This is needed to avoid races between wake_up_poll() above and
4773 * and ep_remove_waitqueue() called for other reasons (eg the epoll file
4774 * descriptor being closed); ep_remove_waitqueue() holds an RCU read
4775 * lock, so we can be sure it's done after calling synchronize_rcu().
4776 */
4777 if (thread->looper & BINDER_LOOPER_STATE_POLL)
4778 synchronize_rcu();
4779
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004780 if (send_reply)
4781 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004782 binder_release_work(proc, &thread->todo);
Todd Kjos2f993e22017-05-12 14:42:55 -07004783 binder_thread_dec_tmpref(thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004784 return active_transactions;
4785}
4786
4787static unsigned int binder_poll(struct file *filp,
4788 struct poll_table_struct *wait)
4789{
4790 struct binder_proc *proc = filp->private_data;
4791 struct binder_thread *thread = NULL;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004792 bool wait_for_proc_work;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004793
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004794 thread = binder_get_thread(proc);
Greg Kroah-Hartman6e463bb2018-02-28 17:17:14 +01004795 if (!thread)
Eric Biggers4be5a282018-01-30 23:11:24 -08004796 return POLLERR;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004797
Martijn Coenen995a36e2017-06-02 13:36:52 -07004798 binder_inner_proc_lock(thread->proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004799 thread->looper |= BINDER_LOOPER_STATE_POLL;
4800 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
4801
Martijn Coenen995a36e2017-06-02 13:36:52 -07004802 binder_inner_proc_unlock(thread->proc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004803
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004804 poll_wait(filp, &thread->wait, wait);
4805
Martijn Coenen47810932017-08-10 12:32:00 +02004806 if (binder_has_work(thread, wait_for_proc_work))
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004807 return POLLIN;
4808
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004809 return 0;
4810}
4811
Tair Rzayev78260ac2014-06-03 22:27:21 +03004812static int binder_ioctl_write_read(struct file *filp,
4813 unsigned int cmd, unsigned long arg,
4814 struct binder_thread *thread)
4815{
4816 int ret = 0;
4817 struct binder_proc *proc = filp->private_data;
4818 unsigned int size = _IOC_SIZE(cmd);
4819 void __user *ubuf = (void __user *)arg;
4820 struct binder_write_read bwr;
4821
4822 if (size != sizeof(struct binder_write_read)) {
4823 ret = -EINVAL;
4824 goto out;
4825 }
4826 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
4827 ret = -EFAULT;
4828 goto out;
4829 }
4830 binder_debug(BINDER_DEBUG_READ_WRITE,
4831 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
4832 proc->pid, thread->pid,
4833 (u64)bwr.write_size, (u64)bwr.write_buffer,
4834 (u64)bwr.read_size, (u64)bwr.read_buffer);
4835
4836 if (bwr.write_size > 0) {
4837 ret = binder_thread_write(proc, thread,
4838 bwr.write_buffer,
4839 bwr.write_size,
4840 &bwr.write_consumed);
4841 trace_binder_write_done(ret);
4842 if (ret < 0) {
4843 bwr.read_consumed = 0;
4844 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4845 ret = -EFAULT;
4846 goto out;
4847 }
4848 }
4849 if (bwr.read_size > 0) {
4850 ret = binder_thread_read(proc, thread, bwr.read_buffer,
4851 bwr.read_size,
4852 &bwr.read_consumed,
4853 filp->f_flags & O_NONBLOCK);
4854 trace_binder_read_done(ret);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004855 binder_inner_proc_lock(proc);
4856 if (!binder_worklist_empty_ilocked(&proc->todo))
Martijn Coenen053be422017-06-06 15:17:46 -07004857 binder_wakeup_proc_ilocked(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004858 binder_inner_proc_unlock(proc);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004859 if (ret < 0) {
4860 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4861 ret = -EFAULT;
4862 goto out;
4863 }
4864 }
4865 binder_debug(BINDER_DEBUG_READ_WRITE,
4866 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
4867 proc->pid, thread->pid,
4868 (u64)bwr.write_consumed, (u64)bwr.write_size,
4869 (u64)bwr.read_consumed, (u64)bwr.read_size);
4870 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
4871 ret = -EFAULT;
4872 goto out;
4873 }
4874out:
4875 return ret;
4876}
4877
Todd Kjos63e0afa2019-01-14 09:10:21 -08004878static int binder_ioctl_set_ctx_mgr(struct file *filp,
4879 struct flat_binder_object *fbo)
Tair Rzayev78260ac2014-06-03 22:27:21 +03004880{
4881 int ret = 0;
4882 struct binder_proc *proc = filp->private_data;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004883 struct binder_context *context = proc->context;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004884 struct binder_node *new_node;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004885 kuid_t curr_euid = current_euid();
4886
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004887 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004888 if (context->binder_context_mgr_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004889 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
4890 ret = -EBUSY;
4891 goto out;
4892 }
Stephen Smalley79af7302015-01-21 10:54:10 -05004893 ret = security_binder_set_context_mgr(proc->tsk);
4894 if (ret < 0)
4895 goto out;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004896 if (uid_valid(context->binder_context_mgr_uid)) {
4897 if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004898 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
4899 from_kuid(&init_user_ns, curr_euid),
4900 from_kuid(&init_user_ns,
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004901 context->binder_context_mgr_uid));
Tair Rzayev78260ac2014-06-03 22:27:21 +03004902 ret = -EPERM;
4903 goto out;
4904 }
4905 } else {
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004906 context->binder_context_mgr_uid = curr_euid;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004907 }
Todd Kjos63e0afa2019-01-14 09:10:21 -08004908 new_node = binder_new_node(proc, fbo);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004909 if (!new_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004910 ret = -ENOMEM;
4911 goto out;
4912 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004913 binder_node_lock(new_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004914 new_node->local_weak_refs++;
4915 new_node->local_strong_refs++;
4916 new_node->has_strong_ref = 1;
4917 new_node->has_weak_ref = 1;
4918 context->binder_context_mgr_node = new_node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004919 binder_node_unlock(new_node);
Todd Kjosf22abc72017-05-09 11:08:05 -07004920 binder_put_node(new_node);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004921out:
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004922 mutex_unlock(&context->context_mgr_node_lock);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004923 return ret;
4924}
4925
Martijn Coenen1c57ba42018-08-25 13:50:56 -07004926static int binder_ioctl_get_node_info_for_ref(struct binder_proc *proc,
4927 struct binder_node_info_for_ref *info)
4928{
4929 struct binder_node *node;
4930 struct binder_context *context = proc->context;
4931 __u32 handle = info->handle;
4932
4933 if (info->strong_count || info->weak_count || info->reserved1 ||
4934 info->reserved2 || info->reserved3) {
4935 binder_user_error("%d BINDER_GET_NODE_INFO_FOR_REF: only handle may be non-zero.",
4936 proc->pid);
4937 return -EINVAL;
4938 }
4939
4940 /* This ioctl may only be used by the context manager */
4941 mutex_lock(&context->context_mgr_node_lock);
4942 if (!context->binder_context_mgr_node ||
4943 context->binder_context_mgr_node->proc != proc) {
4944 mutex_unlock(&context->context_mgr_node_lock);
4945 return -EPERM;
4946 }
4947 mutex_unlock(&context->context_mgr_node_lock);
4948
4949 node = binder_get_node_from_ref(proc, handle, true, NULL);
4950 if (!node)
4951 return -EINVAL;
4952
4953 info->strong_count = node->local_strong_refs +
4954 node->internal_strong_refs;
4955 info->weak_count = node->local_weak_refs;
4956
4957 binder_put_node(node);
4958
4959 return 0;
4960}
4961
Colin Cross833babb32017-06-20 13:54:44 -07004962static int binder_ioctl_get_node_debug_info(struct binder_proc *proc,
4963 struct binder_node_debug_info *info) {
4964 struct rb_node *n;
4965 binder_uintptr_t ptr = info->ptr;
4966
4967 memset(info, 0, sizeof(*info));
4968
4969 binder_inner_proc_lock(proc);
4970 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
4971 struct binder_node *node = rb_entry(n, struct binder_node,
4972 rb_node);
4973 if (node->ptr > ptr) {
4974 info->ptr = node->ptr;
4975 info->cookie = node->cookie;
4976 info->has_strong_ref = node->has_strong_ref;
4977 info->has_weak_ref = node->has_weak_ref;
4978 break;
4979 }
4980 }
4981 binder_inner_proc_unlock(proc);
4982
4983 return 0;
4984}
4985
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004986static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4987{
4988 int ret;
4989 struct binder_proc *proc = filp->private_data;
4990 struct binder_thread *thread;
4991 unsigned int size = _IOC_SIZE(cmd);
4992 void __user *ubuf = (void __user *)arg;
4993
Tair Rzayev78260ac2014-06-03 22:27:21 +03004994 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
4995 proc->pid, current->pid, cmd, arg);*/
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004996
Sherry Yang435416b2017-06-22 14:37:45 -07004997 binder_selftest_alloc(&proc->alloc);
4998
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004999 trace_binder_ioctl(cmd, arg);
5000
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005001 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
5002 if (ret)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07005003 goto err_unlocked;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005004
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005005 thread = binder_get_thread(proc);
5006 if (thread == NULL) {
5007 ret = -ENOMEM;
5008 goto err;
5009 }
5010
5011 switch (cmd) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03005012 case BINDER_WRITE_READ:
5013 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
5014 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005015 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005016 break;
Todd Kjosd600e902017-05-25 17:35:02 -07005017 case BINDER_SET_MAX_THREADS: {
5018 int max_threads;
5019
5020 if (copy_from_user(&max_threads, ubuf,
5021 sizeof(max_threads))) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005022 ret = -EINVAL;
5023 goto err;
5024 }
Todd Kjosd600e902017-05-25 17:35:02 -07005025 binder_inner_proc_lock(proc);
5026 proc->max_threads = max_threads;
5027 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005028 break;
Todd Kjosd600e902017-05-25 17:35:02 -07005029 }
Todd Kjos63e0afa2019-01-14 09:10:21 -08005030 case BINDER_SET_CONTEXT_MGR_EXT: {
5031 struct flat_binder_object fbo;
5032
5033 if (copy_from_user(&fbo, ubuf, sizeof(fbo))) {
5034 ret = -EINVAL;
5035 goto err;
5036 }
5037 ret = binder_ioctl_set_ctx_mgr(filp, &fbo);
5038 if (ret)
5039 goto err;
5040 break;
5041 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005042 case BINDER_SET_CONTEXT_MGR:
Todd Kjos63e0afa2019-01-14 09:10:21 -08005043 ret = binder_ioctl_set_ctx_mgr(filp, NULL);
Tair Rzayev78260ac2014-06-03 22:27:21 +03005044 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005045 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005046 break;
5047 case BINDER_THREAD_EXIT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05305048 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005049 proc->pid, thread->pid);
Todd Kjos2f993e22017-05-12 14:42:55 -07005050 binder_thread_release(proc, thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005051 thread = NULL;
5052 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02005053 case BINDER_VERSION: {
5054 struct binder_version __user *ver = ubuf;
5055
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005056 if (size != sizeof(struct binder_version)) {
5057 ret = -EINVAL;
5058 goto err;
5059 }
Mathieu Maret36c89c02014-04-15 12:03:05 +02005060 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
5061 &ver->protocol_version)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005062 ret = -EINVAL;
5063 goto err;
5064 }
5065 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02005066 }
Martijn Coenen1c57ba42018-08-25 13:50:56 -07005067 case BINDER_GET_NODE_INFO_FOR_REF: {
5068 struct binder_node_info_for_ref info;
5069
5070 if (copy_from_user(&info, ubuf, sizeof(info))) {
5071 ret = -EFAULT;
5072 goto err;
5073 }
5074
5075 ret = binder_ioctl_get_node_info_for_ref(proc, &info);
5076 if (ret < 0)
5077 goto err;
5078
5079 if (copy_to_user(ubuf, &info, sizeof(info))) {
5080 ret = -EFAULT;
5081 goto err;
5082 }
5083
5084 break;
5085 }
Colin Cross833babb32017-06-20 13:54:44 -07005086 case BINDER_GET_NODE_DEBUG_INFO: {
5087 struct binder_node_debug_info info;
5088
5089 if (copy_from_user(&info, ubuf, sizeof(info))) {
5090 ret = -EFAULT;
5091 goto err;
5092 }
5093
5094 ret = binder_ioctl_get_node_debug_info(proc, &info);
5095 if (ret < 0)
5096 goto err;
5097
5098 if (copy_to_user(ubuf, &info, sizeof(info))) {
5099 ret = -EFAULT;
5100 goto err;
5101 }
5102 break;
5103 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005104 default:
5105 ret = -EINVAL;
5106 goto err;
5107 }
5108 ret = 0;
5109err:
5110 if (thread)
Todd Kjos6798e6d2017-01-06 14:19:25 -08005111 thread->looper_need_return = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005112 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
5113 if (ret && ret != -ERESTARTSYS)
Anmol Sarma56b468f2012-10-30 22:35:43 +05305114 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 -07005115err_unlocked:
5116 trace_binder_ioctl_done(ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005117 return ret;
5118}
5119
5120static void binder_vma_open(struct vm_area_struct *vma)
5121{
5122 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09005123
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005124 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05305125 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005126 proc->pid, vma->vm_start, vma->vm_end,
5127 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
5128 (unsigned long)pgprot_val(vma->vm_page_prot));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005129}
5130
5131static void binder_vma_close(struct vm_area_struct *vma)
5132{
5133 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09005134
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005135 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05305136 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005137 proc->pid, vma->vm_start, vma->vm_end,
5138 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
5139 (unsigned long)pgprot_val(vma->vm_page_prot));
Todd Kjosd325d372016-10-10 10:40:53 -07005140 binder_alloc_vma_close(&proc->alloc);
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005141 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005142}
5143
Vinayak Menonddac7d52014-06-02 18:17:59 +05305144static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
5145{
5146 return VM_FAULT_SIGBUS;
5147}
5148
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07005149static const struct vm_operations_struct binder_vm_ops = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005150 .open = binder_vma_open,
5151 .close = binder_vma_close,
Vinayak Menonddac7d52014-06-02 18:17:59 +05305152 .fault = binder_vm_fault,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005153};
5154
Todd Kjosd325d372016-10-10 10:40:53 -07005155static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
5156{
5157 int ret;
5158 struct binder_proc *proc = filp->private_data;
5159 const char *failure_string;
5160
5161 if (proc->tsk != current->group_leader)
5162 return -EINVAL;
5163
5164 if ((vma->vm_end - vma->vm_start) > SZ_4M)
5165 vma->vm_end = vma->vm_start + SZ_4M;
5166
5167 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5168 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
5169 __func__, proc->pid, vma->vm_start, vma->vm_end,
5170 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
5171 (unsigned long)pgprot_val(vma->vm_page_prot));
5172
5173 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
5174 ret = -EPERM;
5175 failure_string = "bad vm_flags";
5176 goto err_bad_arg;
5177 }
Minchan Kim2cafd5b2018-05-07 23:15:37 +09005178 vma->vm_flags |= VM_DONTCOPY | VM_MIXEDMAP;
5179 vma->vm_flags &= ~VM_MAYWRITE;
5180
Todd Kjosd325d372016-10-10 10:40:53 -07005181 vma->vm_ops = &binder_vm_ops;
5182 vma->vm_private_data = proc;
5183
5184 ret = binder_alloc_mmap_handler(&proc->alloc, vma);
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005185 if (ret)
5186 return ret;
Todd Kjosfbb43392017-11-27 09:32:33 -08005187 mutex_lock(&proc->files_lock);
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005188 proc->files = get_files_struct(current);
Todd Kjosfbb43392017-11-27 09:32:33 -08005189 mutex_unlock(&proc->files_lock);
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005190 return 0;
Todd Kjosd325d372016-10-10 10:40:53 -07005191
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005192err_bad_arg:
Elad Wexler6b646402017-12-29 11:03:37 +02005193 pr_err("%s: %d %lx-%lx %s failed %d\n", __func__,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005194 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
5195 return ret;
5196}
5197
5198static int binder_open(struct inode *nodp, struct file *filp)
5199{
5200 struct binder_proc *proc;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005201 struct binder_device *binder_dev;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005202
Elad Wexler6b646402017-12-29 11:03:37 +02005203 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "%s: %d:%d\n", __func__,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005204 current->group_leader->pid, current->pid);
5205
5206 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
5207 if (proc == NULL)
5208 return -ENOMEM;
Todd Kjosfc7a7e22017-05-29 16:44:24 -07005209 spin_lock_init(&proc->inner_lock);
5210 spin_lock_init(&proc->outer_lock);
Todd Kjosf4e6de32019-06-10 09:14:25 -07005211 atomic_set(&proc->tmp_ref, 0);
Martijn Coenen872c26e2017-03-07 15:51:18 +01005212 get_task_struct(current->group_leader);
5213 proc->tsk = current->group_leader;
Todd Kjosfbb43392017-11-27 09:32:33 -08005214 mutex_init(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005215 INIT_LIST_HEAD(&proc->todo);
Martijn Coenen57b2ac62017-06-06 17:04:42 -07005216 if (binder_supported_policy(current->policy)) {
5217 proc->default_priority.sched_policy = current->policy;
5218 proc->default_priority.prio = current->normal_prio;
5219 } else {
5220 proc->default_priority.sched_policy = SCHED_NORMAL;
5221 proc->default_priority.prio = NICE_TO_PRIO(0);
5222 }
5223
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005224 binder_dev = container_of(filp->private_data, struct binder_device,
5225 miscdev);
5226 proc->context = &binder_dev->context;
Todd Kjosd325d372016-10-10 10:40:53 -07005227 binder_alloc_init(&proc->alloc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07005228
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005229 binder_stats_created(BINDER_STAT_PROC);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005230 proc->pid = current->group_leader->pid;
5231 INIT_LIST_HEAD(&proc->delivered_death);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005232 INIT_LIST_HEAD(&proc->waiting_threads);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005233 filp->private_data = proc;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07005234
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005235 mutex_lock(&binder_procs_lock);
5236 hlist_add_head(&proc->proc_node, &binder_procs);
5237 mutex_unlock(&binder_procs_lock);
5238
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005239 if (binder_debugfs_dir_entry_proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005240 char strbuf[11];
Seunghun Lee10f62862014-05-01 01:30:23 +09005241
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005242 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005243 /*
5244 * proc debug entries are shared between contexts, so
5245 * this will fail if the process tries to open the driver
5246 * again with a different context. The priting code will
5247 * anyway print all contexts that a given PID has, so this
5248 * is not a problem.
5249 */
Harsh Shandilya174562a2017-12-22 19:37:02 +05305250 proc->debugfs_entry = debugfs_create_file(strbuf, 0444,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005251 binder_debugfs_dir_entry_proc,
5252 (void *)(unsigned long)proc->pid,
5253 &binder_proc_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005254 }
5255
5256 return 0;
5257}
5258
5259static int binder_flush(struct file *filp, fl_owner_t id)
5260{
5261 struct binder_proc *proc = filp->private_data;
5262
5263 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
5264
5265 return 0;
5266}
5267
5268static void binder_deferred_flush(struct binder_proc *proc)
5269{
5270 struct rb_node *n;
5271 int wake_count = 0;
Seunghun Lee10f62862014-05-01 01:30:23 +09005272
Todd Kjosb4827902017-05-25 15:52:17 -07005273 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005274 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
5275 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
Seunghun Lee10f62862014-05-01 01:30:23 +09005276
Todd Kjos6798e6d2017-01-06 14:19:25 -08005277 thread->looper_need_return = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005278 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
5279 wake_up_interruptible(&thread->wait);
5280 wake_count++;
5281 }
5282 }
Todd Kjosb4827902017-05-25 15:52:17 -07005283 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005284
5285 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5286 "binder_flush: %d woke %d threads\n", proc->pid,
5287 wake_count);
5288}
5289
5290static int binder_release(struct inode *nodp, struct file *filp)
5291{
5292 struct binder_proc *proc = filp->private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09005293
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005294 debugfs_remove(proc->debugfs_entry);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005295 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
5296
5297 return 0;
5298}
5299
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005300static int binder_node_release(struct binder_node *node, int refs)
5301{
5302 struct binder_ref *ref;
5303 int death = 0;
Todd Kjose7f23ed2017-03-21 13:06:01 -07005304 struct binder_proc *proc = node->proc;
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005305
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005306 binder_release_work(proc, &node->async_todo);
Todd Kjose7f23ed2017-03-21 13:06:01 -07005307
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005308 binder_node_lock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07005309 binder_inner_proc_lock(proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005310 binder_dequeue_work_ilocked(&node->work);
Todd Kjosf22abc72017-05-09 11:08:05 -07005311 /*
5312 * The caller must have taken a temporary ref on the node,
5313 */
5314 BUG_ON(!node->tmp_refs);
5315 if (hlist_empty(&node->refs) && node->tmp_refs == 1) {
Todd Kjose7f23ed2017-03-21 13:06:01 -07005316 binder_inner_proc_unlock(proc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005317 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07005318 binder_free_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005319
5320 return refs;
5321 }
5322
5323 node->proc = NULL;
5324 node->local_strong_refs = 0;
5325 node->local_weak_refs = 0;
Todd Kjose7f23ed2017-03-21 13:06:01 -07005326 binder_inner_proc_unlock(proc);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005327
5328 spin_lock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005329 hlist_add_head(&node->dead_node, &binder_dead_nodes);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005330 spin_unlock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005331
5332 hlist_for_each_entry(ref, &node->refs, node_entry) {
5333 refs++;
Martijn Coenenf9eac642017-05-22 11:26:23 -07005334 /*
5335 * Need the node lock to synchronize
5336 * with new notification requests and the
5337 * inner lock to synchronize with queued
5338 * death notifications.
5339 */
5340 binder_inner_proc_lock(ref->proc);
5341 if (!ref->death) {
5342 binder_inner_proc_unlock(ref->proc);
Arve Hjønnevåge194fd82014-02-17 13:58:29 -08005343 continue;
Martijn Coenenf9eac642017-05-22 11:26:23 -07005344 }
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005345
5346 death++;
5347
Martijn Coenenf9eac642017-05-22 11:26:23 -07005348 BUG_ON(!list_empty(&ref->death->work.entry));
5349 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
5350 binder_enqueue_work_ilocked(&ref->death->work,
5351 &ref->proc->todo);
Martijn Coenen053be422017-06-06 15:17:46 -07005352 binder_wakeup_proc_ilocked(ref->proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005353 binder_inner_proc_unlock(ref->proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005354 }
5355
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005356 binder_debug(BINDER_DEBUG_DEAD_BINDER,
5357 "node %d now dead, refs %d, death %d\n",
5358 node->debug_id, refs, death);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005359 binder_node_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07005360 binder_put_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005361
5362 return refs;
5363}
5364
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005365static void binder_deferred_release(struct binder_proc *proc)
5366{
Martijn Coenen0b3311e2016-09-30 15:51:48 +02005367 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005368 struct rb_node *n;
Todd Kjosd325d372016-10-10 10:40:53 -07005369 int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005370
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005371 BUG_ON(proc->files);
5372
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005373 mutex_lock(&binder_procs_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005374 hlist_del(&proc->proc_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005375 mutex_unlock(&binder_procs_lock);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005376
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005377 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02005378 if (context->binder_context_mgr_node &&
5379 context->binder_context_mgr_node->proc == proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005380 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01005381 "%s: %d context_mgr_node gone\n",
5382 __func__, proc->pid);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02005383 context->binder_context_mgr_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005384 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005385 mutex_unlock(&context->context_mgr_node_lock);
Todd Kjosb4827902017-05-25 15:52:17 -07005386 binder_inner_proc_lock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07005387 /*
5388 * Make sure proc stays alive after we
5389 * remove all the threads
5390 */
Todd Kjosf4e6de32019-06-10 09:14:25 -07005391 atomic_inc(&proc->tmp_ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005392
Todd Kjos2f993e22017-05-12 14:42:55 -07005393 proc->is_dead = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005394 threads = 0;
5395 active_transactions = 0;
5396 while ((n = rb_first(&proc->threads))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005397 struct binder_thread *thread;
5398
5399 thread = rb_entry(n, struct binder_thread, rb_node);
Todd Kjosb4827902017-05-25 15:52:17 -07005400 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005401 threads++;
Todd Kjos2f993e22017-05-12 14:42:55 -07005402 active_transactions += binder_thread_release(proc, thread);
Todd Kjosb4827902017-05-25 15:52:17 -07005403 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005404 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005405
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005406 nodes = 0;
5407 incoming_refs = 0;
5408 while ((n = rb_first(&proc->nodes))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005409 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005410
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005411 node = rb_entry(n, struct binder_node, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005412 nodes++;
Todd Kjosf22abc72017-05-09 11:08:05 -07005413 /*
5414 * take a temporary ref on the node before
5415 * calling binder_node_release() which will either
5416 * kfree() the node or call binder_put_node()
5417 */
Todd Kjos425d23f2017-06-12 12:07:26 -07005418 binder_inc_node_tmpref_ilocked(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005419 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjos425d23f2017-06-12 12:07:26 -07005420 binder_inner_proc_unlock(proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005421 incoming_refs = binder_node_release(node, incoming_refs);
Todd Kjos425d23f2017-06-12 12:07:26 -07005422 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005423 }
Todd Kjos425d23f2017-06-12 12:07:26 -07005424 binder_inner_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005425
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005426 outgoing_refs = 0;
Todd Kjos5346bf32016-10-20 16:43:34 -07005427 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005428 while ((n = rb_first(&proc->refs_by_desc))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005429 struct binder_ref *ref;
5430
5431 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005432 outgoing_refs++;
Todd Kjos5346bf32016-10-20 16:43:34 -07005433 binder_cleanup_ref_olocked(ref);
5434 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07005435 binder_free_ref(ref);
Todd Kjos5346bf32016-10-20 16:43:34 -07005436 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005437 }
Todd Kjos5346bf32016-10-20 16:43:34 -07005438 binder_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005439
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005440 binder_release_work(proc, &proc->todo);
5441 binder_release_work(proc, &proc->delivered_death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005442
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005443 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Todd Kjosd325d372016-10-10 10:40:53 -07005444 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01005445 __func__, proc->pid, threads, nodes, incoming_refs,
Todd Kjosd325d372016-10-10 10:40:53 -07005446 outgoing_refs, active_transactions);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005447
Todd Kjos2f993e22017-05-12 14:42:55 -07005448 binder_proc_dec_tmpref(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005449}
5450
5451static void binder_deferred_func(struct work_struct *work)
5452{
5453 struct binder_proc *proc;
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005454 struct files_struct *files;
5455
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005456 int defer;
Seunghun Lee10f62862014-05-01 01:30:23 +09005457
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005458 do {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005459 mutex_lock(&binder_deferred_lock);
5460 if (!hlist_empty(&binder_deferred_list)) {
5461 proc = hlist_entry(binder_deferred_list.first,
5462 struct binder_proc, deferred_work_node);
5463 hlist_del_init(&proc->deferred_work_node);
5464 defer = proc->deferred_work;
5465 proc->deferred_work = 0;
5466 } else {
5467 proc = NULL;
5468 defer = 0;
5469 }
5470 mutex_unlock(&binder_deferred_lock);
5471
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005472 files = NULL;
5473 if (defer & BINDER_DEFERRED_PUT_FILES) {
Todd Kjosfbb43392017-11-27 09:32:33 -08005474 mutex_lock(&proc->files_lock);
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005475 files = proc->files;
5476 if (files)
5477 proc->files = NULL;
Todd Kjosfbb43392017-11-27 09:32:33 -08005478 mutex_unlock(&proc->files_lock);
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005479 }
5480
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005481 if (defer & BINDER_DEFERRED_FLUSH)
5482 binder_deferred_flush(proc);
5483
5484 if (defer & BINDER_DEFERRED_RELEASE)
5485 binder_deferred_release(proc); /* frees proc */
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005486
5487 if (files)
5488 put_files_struct(files);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005489 } while (proc);
5490}
5491static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
5492
5493static void
5494binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
5495{
5496 mutex_lock(&binder_deferred_lock);
5497 proc->deferred_work |= defer;
5498 if (hlist_unhashed(&proc->deferred_work_node)) {
5499 hlist_add_head(&proc->deferred_work_node,
5500 &binder_deferred_list);
Bhaktipriya Shridhar1beba522016-08-13 22:16:24 +05305501 schedule_work(&binder_deferred_work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005502 }
5503 mutex_unlock(&binder_deferred_lock);
5504}
5505
Todd Kjos6d241a42017-04-21 14:32:11 -07005506static void print_binder_transaction_ilocked(struct seq_file *m,
5507 struct binder_proc *proc,
5508 const char *prefix,
5509 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005510{
Todd Kjos6d241a42017-04-21 14:32:11 -07005511 struct binder_proc *to_proc;
5512 struct binder_buffer *buffer = t->buffer;
5513
Todd Kjos2f993e22017-05-12 14:42:55 -07005514 spin_lock(&t->lock);
Todd Kjos6d241a42017-04-21 14:32:11 -07005515 to_proc = t->to_proc;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005516 seq_printf(m,
Todd Kjosf540ce02018-02-07 13:57:37 -08005517 "%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 -07005518 prefix, t->debug_id, t,
5519 t->from ? t->from->proc->pid : 0,
5520 t->from ? t->from->pid : 0,
Todd Kjos6d241a42017-04-21 14:32:11 -07005521 to_proc ? to_proc->pid : 0,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005522 t->to_thread ? t->to_thread->pid : 0,
Martijn Coenen57b2ac62017-06-06 17:04:42 -07005523 t->code, t->flags, t->priority.sched_policy,
5524 t->priority.prio, t->need_reply);
Todd Kjos2f993e22017-05-12 14:42:55 -07005525 spin_unlock(&t->lock);
5526
Todd Kjos6d241a42017-04-21 14:32:11 -07005527 if (proc != to_proc) {
5528 /*
5529 * Can only safely deref buffer if we are holding the
5530 * correct proc inner lock for this node
5531 */
5532 seq_puts(m, "\n");
5533 return;
5534 }
5535
5536 if (buffer == NULL) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005537 seq_puts(m, " buffer free\n");
5538 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005539 }
Todd Kjos6d241a42017-04-21 14:32:11 -07005540 if (buffer->target_node)
5541 seq_printf(m, " node %d", buffer->target_node->debug_id);
Todd Kjosf540ce02018-02-07 13:57:37 -08005542 seq_printf(m, " size %zd:%zd data %pK\n",
Todd Kjos6d241a42017-04-21 14:32:11 -07005543 buffer->data_size, buffer->offsets_size,
Todd Kjos8539b1e2019-02-08 10:35:20 -08005544 buffer->user_data);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005545}
5546
Todd Kjos6d241a42017-04-21 14:32:11 -07005547static void print_binder_work_ilocked(struct seq_file *m,
5548 struct binder_proc *proc,
5549 const char *prefix,
5550 const char *transaction_prefix,
5551 struct binder_work *w)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005552{
5553 struct binder_node *node;
5554 struct binder_transaction *t;
5555
5556 switch (w->type) {
5557 case BINDER_WORK_TRANSACTION:
5558 t = container_of(w, struct binder_transaction, work);
Todd Kjos6d241a42017-04-21 14:32:11 -07005559 print_binder_transaction_ilocked(
5560 m, proc, transaction_prefix, t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005561 break;
Todd Kjos858b8da2017-04-21 17:35:12 -07005562 case BINDER_WORK_RETURN_ERROR: {
5563 struct binder_error *e = container_of(
5564 w, struct binder_error, work);
5565
5566 seq_printf(m, "%stransaction error: %u\n",
5567 prefix, e->cmd);
5568 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005569 case BINDER_WORK_TRANSACTION_COMPLETE:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005570 seq_printf(m, "%stransaction complete\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005571 break;
5572 case BINDER_WORK_NODE:
5573 node = container_of(w, struct binder_node, work);
Arve Hjønnevågda498892014-02-21 14:40:26 -08005574 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
5575 prefix, node->debug_id,
5576 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005577 break;
5578 case BINDER_WORK_DEAD_BINDER:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005579 seq_printf(m, "%shas dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005580 break;
5581 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005582 seq_printf(m, "%shas cleared dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005583 break;
5584 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005585 seq_printf(m, "%shas cleared death notification\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005586 break;
5587 default:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005588 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005589 break;
5590 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005591}
5592
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005593static void print_binder_thread_ilocked(struct seq_file *m,
5594 struct binder_thread *thread,
5595 int print_always)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005596{
5597 struct binder_transaction *t;
5598 struct binder_work *w;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005599 size_t start_pos = m->count;
5600 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005601
Todd Kjos2f993e22017-05-12 14:42:55 -07005602 seq_printf(m, " thread %d: l %02x need_return %d tr %d\n",
Todd Kjos6798e6d2017-01-06 14:19:25 -08005603 thread->pid, thread->looper,
Todd Kjos2f993e22017-05-12 14:42:55 -07005604 thread->looper_need_return,
5605 atomic_read(&thread->tmp_ref));
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005606 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005607 t = thread->transaction_stack;
5608 while (t) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005609 if (t->from == thread) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005610 print_binder_transaction_ilocked(m, thread->proc,
5611 " outgoing transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005612 t = t->from_parent;
5613 } else if (t->to_thread == thread) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005614 print_binder_transaction_ilocked(m, thread->proc,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005615 " incoming transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005616 t = t->to_parent;
5617 } else {
Todd Kjos6d241a42017-04-21 14:32:11 -07005618 print_binder_transaction_ilocked(m, thread->proc,
5619 " bad transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005620 t = NULL;
5621 }
5622 }
5623 list_for_each_entry(w, &thread->todo, entry) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005624 print_binder_work_ilocked(m, thread->proc, " ",
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005625 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005626 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005627 if (!print_always && m->count == header_pos)
5628 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005629}
5630
Todd Kjos425d23f2017-06-12 12:07:26 -07005631static void print_binder_node_nilocked(struct seq_file *m,
5632 struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005633{
5634 struct binder_ref *ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005635 struct binder_work *w;
5636 int count;
5637
5638 count = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08005639 hlist_for_each_entry(ref, &node->refs, node_entry)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005640 count++;
5641
Martijn Coenen6aac9792017-06-07 09:29:14 -07005642 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 -08005643 node->debug_id, (u64)node->ptr, (u64)node->cookie,
Martijn Coenen6aac9792017-06-07 09:29:14 -07005644 node->sched_policy, node->min_priority,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005645 node->has_strong_ref, node->has_weak_ref,
5646 node->local_strong_refs, node->local_weak_refs,
Todd Kjosf22abc72017-05-09 11:08:05 -07005647 node->internal_strong_refs, count, node->tmp_refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005648 if (count) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005649 seq_puts(m, " proc");
Sasha Levinb67bfe02013-02-27 17:06:00 -08005650 hlist_for_each_entry(ref, &node->refs, node_entry)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005651 seq_printf(m, " %d", ref->proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005652 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005653 seq_puts(m, "\n");
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005654 if (node->proc) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005655 list_for_each_entry(w, &node->async_todo, entry)
Todd Kjos6d241a42017-04-21 14:32:11 -07005656 print_binder_work_ilocked(m, node->proc, " ",
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005657 " pending async transaction", w);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005658 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005659}
5660
Todd Kjos5346bf32016-10-20 16:43:34 -07005661static void print_binder_ref_olocked(struct seq_file *m,
5662 struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005663{
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005664 binder_node_lock(ref->node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07005665 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
5666 ref->data.debug_id, ref->data.desc,
5667 ref->node->proc ? "" : "dead ",
5668 ref->node->debug_id, ref->data.strong,
5669 ref->data.weak, ref->death);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005670 binder_node_unlock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005671}
5672
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005673static void print_binder_proc(struct seq_file *m,
5674 struct binder_proc *proc, int print_all)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005675{
5676 struct binder_work *w;
5677 struct rb_node *n;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005678 size_t start_pos = m->count;
5679 size_t header_pos;
Todd Kjos425d23f2017-06-12 12:07:26 -07005680 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005681
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005682 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005683 seq_printf(m, "context %s\n", proc->context->name);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005684 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005685
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005686 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005687 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005688 print_binder_thread_ilocked(m, rb_entry(n, struct binder_thread,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005689 rb_node), print_all);
Todd Kjos425d23f2017-06-12 12:07:26 -07005690
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005691 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005692 struct binder_node *node = rb_entry(n, struct binder_node,
5693 rb_node);
Todd Kjosd79aa412018-12-05 15:19:26 -08005694 if (!print_all && !node->has_async_transaction)
5695 continue;
5696
Todd Kjos425d23f2017-06-12 12:07:26 -07005697 /*
5698 * take a temporary reference on the node so it
5699 * survives and isn't removed from the tree
5700 * while we print it.
5701 */
5702 binder_inc_node_tmpref_ilocked(node);
5703 /* Need to drop inner lock to take node lock */
5704 binder_inner_proc_unlock(proc);
5705 if (last_node)
5706 binder_put_node(last_node);
5707 binder_node_inner_lock(node);
5708 print_binder_node_nilocked(m, node);
5709 binder_node_inner_unlock(node);
5710 last_node = node;
5711 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005712 }
Todd Kjos425d23f2017-06-12 12:07:26 -07005713 binder_inner_proc_unlock(proc);
5714 if (last_node)
5715 binder_put_node(last_node);
5716
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005717 if (print_all) {
Todd Kjos5346bf32016-10-20 16:43:34 -07005718 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005719 for (n = rb_first(&proc->refs_by_desc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005720 n != NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005721 n = rb_next(n))
Todd Kjos5346bf32016-10-20 16:43:34 -07005722 print_binder_ref_olocked(m, rb_entry(n,
5723 struct binder_ref,
5724 rb_node_desc));
5725 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005726 }
Todd Kjosd325d372016-10-10 10:40:53 -07005727 binder_alloc_print_allocated(m, &proc->alloc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005728 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005729 list_for_each_entry(w, &proc->todo, entry)
Todd Kjos6d241a42017-04-21 14:32:11 -07005730 print_binder_work_ilocked(m, proc, " ",
5731 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005732 list_for_each_entry(w, &proc->delivered_death, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005733 seq_puts(m, " has delivered dead binder\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005734 break;
5735 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005736 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005737 if (!print_all && m->count == header_pos)
5738 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005739}
5740
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005741static const char * const binder_return_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005742 "BR_ERROR",
5743 "BR_OK",
5744 "BR_TRANSACTION",
5745 "BR_REPLY",
5746 "BR_ACQUIRE_RESULT",
5747 "BR_DEAD_REPLY",
5748 "BR_TRANSACTION_COMPLETE",
5749 "BR_INCREFS",
5750 "BR_ACQUIRE",
5751 "BR_RELEASE",
5752 "BR_DECREFS",
5753 "BR_ATTEMPT_ACQUIRE",
5754 "BR_NOOP",
5755 "BR_SPAWN_LOOPER",
5756 "BR_FINISHED",
5757 "BR_DEAD_BINDER",
5758 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
5759 "BR_FAILED_REPLY"
5760};
5761
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005762static const char * const binder_command_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005763 "BC_TRANSACTION",
5764 "BC_REPLY",
5765 "BC_ACQUIRE_RESULT",
5766 "BC_FREE_BUFFER",
5767 "BC_INCREFS",
5768 "BC_ACQUIRE",
5769 "BC_RELEASE",
5770 "BC_DECREFS",
5771 "BC_INCREFS_DONE",
5772 "BC_ACQUIRE_DONE",
5773 "BC_ATTEMPT_ACQUIRE",
5774 "BC_REGISTER_LOOPER",
5775 "BC_ENTER_LOOPER",
5776 "BC_EXIT_LOOPER",
5777 "BC_REQUEST_DEATH_NOTIFICATION",
5778 "BC_CLEAR_DEATH_NOTIFICATION",
Martijn Coenen5a6da532016-09-30 14:10:07 +02005779 "BC_DEAD_BINDER_DONE",
5780 "BC_TRANSACTION_SG",
5781 "BC_REPLY_SG",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005782};
5783
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005784static const char * const binder_objstat_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005785 "proc",
5786 "thread",
5787 "node",
5788 "ref",
5789 "death",
5790 "transaction",
5791 "transaction_complete"
5792};
5793
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005794static void print_binder_stats(struct seq_file *m, const char *prefix,
5795 struct binder_stats *stats)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005796{
5797 int i;
5798
5799 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005800 ARRAY_SIZE(binder_command_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005801 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005802 int temp = atomic_read(&stats->bc[i]);
5803
5804 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005805 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005806 binder_command_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005807 }
5808
5809 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005810 ARRAY_SIZE(binder_return_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005811 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005812 int temp = atomic_read(&stats->br[i]);
5813
5814 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005815 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005816 binder_return_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005817 }
5818
5819 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005820 ARRAY_SIZE(binder_objstat_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005821 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005822 ARRAY_SIZE(stats->obj_deleted));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005823 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005824 int created = atomic_read(&stats->obj_created[i]);
5825 int deleted = atomic_read(&stats->obj_deleted[i]);
5826
5827 if (created || deleted)
5828 seq_printf(m, "%s%s: active %d total %d\n",
5829 prefix,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005830 binder_objstat_strings[i],
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005831 created - deleted,
5832 created);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005833 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005834}
5835
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005836static void print_binder_proc_stats(struct seq_file *m,
5837 struct binder_proc *proc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005838{
5839 struct binder_work *w;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005840 struct binder_thread *thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005841 struct rb_node *n;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005842 int count, strong, weak, ready_threads;
Todd Kjosb4827902017-05-25 15:52:17 -07005843 size_t free_async_space =
5844 binder_alloc_get_free_async_space(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005845
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005846 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005847 seq_printf(m, "context %s\n", proc->context->name);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005848 count = 0;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005849 ready_threads = 0;
Todd Kjosb4827902017-05-25 15:52:17 -07005850 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005851 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
5852 count++;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005853
5854 list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node)
5855 ready_threads++;
5856
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005857 seq_printf(m, " threads: %d\n", count);
5858 seq_printf(m, " requested threads: %d+%d/%d\n"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005859 " ready threads %d\n"
5860 " free async space %zd\n", proc->requested_threads,
5861 proc->requested_threads_started, proc->max_threads,
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005862 ready_threads,
Todd Kjosb4827902017-05-25 15:52:17 -07005863 free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005864 count = 0;
5865 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
5866 count++;
Todd Kjos425d23f2017-06-12 12:07:26 -07005867 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005868 seq_printf(m, " nodes: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005869 count = 0;
5870 strong = 0;
5871 weak = 0;
Todd Kjos5346bf32016-10-20 16:43:34 -07005872 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005873 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
5874 struct binder_ref *ref = rb_entry(n, struct binder_ref,
5875 rb_node_desc);
5876 count++;
Todd Kjosb0117bb2017-05-08 09:16:27 -07005877 strong += ref->data.strong;
5878 weak += ref->data.weak;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005879 }
Todd Kjos5346bf32016-10-20 16:43:34 -07005880 binder_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005881 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005882
Todd Kjosd325d372016-10-10 10:40:53 -07005883 count = binder_alloc_get_allocated_count(&proc->alloc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005884 seq_printf(m, " buffers: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005885
Sherry Yang91004422017-08-22 17:26:57 -07005886 binder_alloc_print_pages(m, &proc->alloc);
5887
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005888 count = 0;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005889 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005890 list_for_each_entry(w, &proc->todo, entry) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005891 if (w->type == BINDER_WORK_TRANSACTION)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005892 count++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005893 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005894 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005895 seq_printf(m, " pending transactions: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005896
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005897 print_binder_stats(m, " ", &proc->stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005898}
5899
5900
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005901static int binder_state_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005902{
5903 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005904 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005905 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005906
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005907 seq_puts(m, "binder state:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005908
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005909 spin_lock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005910 if (!hlist_empty(&binder_dead_nodes))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005911 seq_puts(m, "dead nodes:\n");
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005912 hlist_for_each_entry(node, &binder_dead_nodes, dead_node) {
5913 /*
5914 * take a temporary reference on the node so it
5915 * survives and isn't removed from the list
5916 * while we print it.
5917 */
5918 node->tmp_refs++;
5919 spin_unlock(&binder_dead_nodes_lock);
5920 if (last_node)
5921 binder_put_node(last_node);
5922 binder_node_lock(node);
Todd Kjos425d23f2017-06-12 12:07:26 -07005923 print_binder_node_nilocked(m, node);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005924 binder_node_unlock(node);
5925 last_node = node;
5926 spin_lock(&binder_dead_nodes_lock);
5927 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005928 spin_unlock(&binder_dead_nodes_lock);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005929 if (last_node)
5930 binder_put_node(last_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005931
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005932 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005933 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005934 print_binder_proc(m, proc, 1);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005935 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005936
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005937 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005938}
5939
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005940static int binder_stats_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005941{
5942 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005943
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005944 seq_puts(m, "binder stats:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005945
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005946 print_binder_stats(m, "", &binder_stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005947
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005948 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005949 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005950 print_binder_proc_stats(m, proc);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005951 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005952
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005953 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005954}
5955
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005956static int binder_transactions_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005957{
5958 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005959
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005960 seq_puts(m, "binder transactions:\n");
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005961 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005962 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005963 print_binder_proc(m, proc, 0);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005964 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005965
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005966 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005967}
5968
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005969static int binder_proc_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005970{
Riley Andrews83050a42016-02-09 21:05:33 -08005971 struct binder_proc *itr;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005972 int pid = (unsigned long)m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005973
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005974 mutex_lock(&binder_procs_lock);
Riley Andrews83050a42016-02-09 21:05:33 -08005975 hlist_for_each_entry(itr, &binder_procs, proc_node) {
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005976 if (itr->pid == pid) {
5977 seq_puts(m, "binder proc state:\n");
5978 print_binder_proc(m, itr, 1);
Riley Andrews83050a42016-02-09 21:05:33 -08005979 }
5980 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005981 mutex_unlock(&binder_procs_lock);
5982
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005983 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005984}
5985
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005986static void print_binder_transaction_log_entry(struct seq_file *m,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005987 struct binder_transaction_log_entry *e)
5988{
Todd Kjos1cfe6272017-05-24 13:33:28 -07005989 int debug_id = READ_ONCE(e->debug_id_done);
5990 /*
5991 * read barrier to guarantee debug_id_done read before
5992 * we print the log values
5993 */
5994 smp_rmb();
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005995 seq_printf(m,
Todd Kjos1cfe6272017-05-24 13:33:28 -07005996 "%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 -07005997 e->debug_id, (e->call_type == 2) ? "reply" :
5998 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005999 e->from_thread, e->to_proc, e->to_thread, e->context_name,
Todd Kjose598d172017-03-22 17:19:52 -07006000 e->to_node, e->target_handle, e->data_size, e->offsets_size,
6001 e->return_error, e->return_error_param,
6002 e->return_error_line);
Todd Kjos1cfe6272017-05-24 13:33:28 -07006003 /*
6004 * read-barrier to guarantee read of debug_id_done after
6005 * done printing the fields of the entry
6006 */
6007 smp_rmb();
6008 seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ?
6009 "\n" : " (incomplete)\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006010}
6011
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006012static int binder_transaction_log_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006013{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006014 struct binder_transaction_log *log = m->private;
Todd Kjos1cfe6272017-05-24 13:33:28 -07006015 unsigned int log_cur = atomic_read(&log->cur);
6016 unsigned int count;
6017 unsigned int cur;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006018 int i;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006019
Todd Kjos1cfe6272017-05-24 13:33:28 -07006020 count = log_cur + 1;
6021 cur = count < ARRAY_SIZE(log->entry) && !log->full ?
6022 0 : count % ARRAY_SIZE(log->entry);
6023 if (count > ARRAY_SIZE(log->entry) || log->full)
6024 count = ARRAY_SIZE(log->entry);
6025 for (i = 0; i < count; i++) {
6026 unsigned int index = cur++ % ARRAY_SIZE(log->entry);
6027
6028 print_binder_transaction_log_entry(m, &log->entry[index]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006029 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006030 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006031}
6032
6033static const struct file_operations binder_fops = {
6034 .owner = THIS_MODULE,
6035 .poll = binder_poll,
6036 .unlocked_ioctl = binder_ioctl,
Arve Hjønnevågda498892014-02-21 14:40:26 -08006037 .compat_ioctl = binder_ioctl,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006038 .mmap = binder_mmap,
6039 .open = binder_open,
6040 .flush = binder_flush,
6041 .release = binder_release,
6042};
6043
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006044BINDER_DEBUG_ENTRY(state);
6045BINDER_DEBUG_ENTRY(stats);
6046BINDER_DEBUG_ENTRY(transactions);
6047BINDER_DEBUG_ENTRY(transaction_log);
6048
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006049static int __init init_binder_device(const char *name)
6050{
6051 int ret;
6052 struct binder_device *binder_device;
6053
6054 binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
6055 if (!binder_device)
6056 return -ENOMEM;
6057
6058 binder_device->miscdev.fops = &binder_fops;
6059 binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
6060 binder_device->miscdev.name = name;
6061
6062 binder_device->context.binder_context_mgr_uid = INVALID_UID;
6063 binder_device->context.name = name;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07006064 mutex_init(&binder_device->context.context_mgr_node_lock);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006065
6066 ret = misc_register(&binder_device->miscdev);
6067 if (ret < 0) {
6068 kfree(binder_device);
6069 return ret;
6070 }
6071
6072 hlist_add_head(&binder_device->hlist, &binder_devices);
6073
6074 return ret;
6075}
6076
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006077static int __init binder_init(void)
6078{
6079 int ret;
Christian Brauner558ee932017-08-21 16:13:28 +02006080 char *device_name, *device_names, *device_tmp;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006081 struct binder_device *device;
6082 struct hlist_node *tmp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006083
Tetsuo Handaf8cb8222017-11-29 22:29:47 +09006084 ret = binder_alloc_shrinker_init();
6085 if (ret)
6086 return ret;
Sherry Yang5828d702017-07-29 13:24:11 -07006087
Todd Kjos1cfe6272017-05-24 13:33:28 -07006088 atomic_set(&binder_transaction_log.cur, ~0U);
6089 atomic_set(&binder_transaction_log_failed.cur, ~0U);
6090
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006091 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
6092 if (binder_debugfs_dir_entry_root)
6093 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
6094 binder_debugfs_dir_entry_root);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006095
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006096 if (binder_debugfs_dir_entry_root) {
6097 debugfs_create_file("state",
Harsh Shandilya174562a2017-12-22 19:37:02 +05306098 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006099 binder_debugfs_dir_entry_root,
6100 NULL,
6101 &binder_state_fops);
6102 debugfs_create_file("stats",
Harsh Shandilya174562a2017-12-22 19:37:02 +05306103 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006104 binder_debugfs_dir_entry_root,
6105 NULL,
6106 &binder_stats_fops);
6107 debugfs_create_file("transactions",
Harsh Shandilya174562a2017-12-22 19:37:02 +05306108 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006109 binder_debugfs_dir_entry_root,
6110 NULL,
6111 &binder_transactions_fops);
6112 debugfs_create_file("transaction_log",
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 &binder_transaction_log,
6116 &binder_transaction_log_fops);
6117 debugfs_create_file("failed_transaction_log",
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 &binder_transaction_log_failed,
6121 &binder_transaction_log_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006122 }
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006123
6124 /*
6125 * Copy the module_parameter string, because we don't want to
6126 * tokenize it in-place.
6127 */
6128 device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL);
6129 if (!device_names) {
6130 ret = -ENOMEM;
6131 goto err_alloc_device_names_failed;
6132 }
6133 strcpy(device_names, binder_devices_param);
6134
Christian Brauner558ee932017-08-21 16:13:28 +02006135 device_tmp = device_names;
6136 while ((device_name = strsep(&device_tmp, ","))) {
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006137 ret = init_binder_device(device_name);
6138 if (ret)
6139 goto err_init_binder_device_failed;
6140 }
6141
6142 return ret;
6143
6144err_init_binder_device_failed:
6145 hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
6146 misc_deregister(&device->miscdev);
6147 hlist_del(&device->hlist);
6148 kfree(device);
6149 }
Christian Brauner558ee932017-08-21 16:13:28 +02006150
6151 kfree(device_names);
6152
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006153err_alloc_device_names_failed:
6154 debugfs_remove_recursive(binder_debugfs_dir_entry_root);
6155
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006156 return ret;
6157}
6158
6159device_initcall(binder_init);
6160
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07006161#define CREATE_TRACE_POINTS
6162#include "binder_trace.h"
6163
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006164MODULE_LICENSE("GPL v2");