blob: 06b239ae4cca81b5877bf046718c0e5d95242e1d [file] [log] [blame]
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001/* binder.c
2 *
3 * Android IPC Subsystem
4 *
5 * Copyright (C) 2007-2008 Google, Inc.
6 *
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
Todd Kjosfc7a7e22017-05-29 16:44:24 -070018/*
19 * Locking overview
20 *
21 * There are 3 main spinlocks which must be acquired in the
22 * order shown:
23 *
24 * 1) proc->outer_lock : protects binder_ref
25 * binder_proc_lock() and binder_proc_unlock() are
26 * used to acq/rel.
27 * 2) node->lock : protects most fields of binder_node.
28 * binder_node_lock() and binder_node_unlock() are
29 * used to acq/rel
30 * 3) proc->inner_lock : protects the thread and node lists
Martijn Coenen22d64e4322017-06-02 11:15:44 -070031 * (proc->threads, proc->waiting_threads, proc->nodes)
32 * and all todo lists associated with the binder_proc
33 * (proc->todo, thread->todo, proc->delivered_death and
34 * node->async_todo), as well as thread->transaction_stack
Todd Kjosfc7a7e22017-05-29 16:44:24 -070035 * binder_inner_proc_lock() and binder_inner_proc_unlock()
36 * are used to acq/rel
37 *
38 * Any lock under procA must never be nested under any lock at the same
39 * level or below on procB.
40 *
41 * Functions that require a lock held on entry indicate which lock
42 * in the suffix of the function name:
43 *
44 * foo_olocked() : requires node->outer_lock
45 * foo_nlocked() : requires node->lock
46 * foo_ilocked() : requires proc->inner_lock
47 * foo_oilocked(): requires proc->outer_lock and proc->inner_lock
48 * foo_nilocked(): requires node->lock and proc->inner_lock
49 * ...
50 */
51
Anmol Sarma56b468f2012-10-30 22:35:43 +053052#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
53
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090054#include <asm/cacheflush.h>
55#include <linux/fdtable.h>
56#include <linux/file.h>
Colin Crosse2610b22013-05-06 23:50:15 +000057#include <linux/freezer.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090058#include <linux/fs.h>
59#include <linux/list.h>
60#include <linux/miscdevice.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090061#include <linux/module.h>
62#include <linux/mutex.h>
63#include <linux/nsproxy.h>
64#include <linux/poll.h>
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070065#include <linux/debugfs.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090066#include <linux/rbtree.h>
67#include <linux/sched.h>
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070068#include <linux/seq_file.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090069#include <linux/uaccess.h>
Eric W. Biederman17cf22c2010-03-02 14:51:53 -080070#include <linux/pid_namespace.h>
Stephen Smalley79af7302015-01-21 10:54:10 -050071#include <linux/security.h>
Todd Kjosfc7a7e22017-05-29 16:44:24 -070072#include <linux/spinlock.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090073
Greg Kroah-Hartman9246a4a2014-10-16 15:26:51 +020074#include <uapi/linux/android/binder.h>
Todd Kjosb9341022016-10-10 10:40:53 -070075#include "binder_alloc.h"
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -070076#include "binder_trace.h"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090077
Todd Kjos8d9f6f32016-10-17 12:33:15 -070078static HLIST_HEAD(binder_deferred_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090079static DEFINE_MUTEX(binder_deferred_lock);
80
Martijn Coenen6b7c7122016-09-30 16:08:09 +020081static HLIST_HEAD(binder_devices);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090082static HLIST_HEAD(binder_procs);
Todd Kjos8d9f6f32016-10-17 12:33:15 -070083static DEFINE_MUTEX(binder_procs_lock);
84
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090085static HLIST_HEAD(binder_dead_nodes);
Todd Kjos8d9f6f32016-10-17 12:33:15 -070086static DEFINE_SPINLOCK(binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090087
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070088static struct dentry *binder_debugfs_dir_entry_root;
89static struct dentry *binder_debugfs_dir_entry_proc;
Todd Kjosc4bd08b2017-05-25 10:56:00 -070090static atomic_t binder_last_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090091
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070092#define BINDER_DEBUG_ENTRY(name) \
93static int binder_##name##_open(struct inode *inode, struct file *file) \
94{ \
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070095 return single_open(file, binder_##name##_show, inode->i_private); \
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070096} \
97\
98static const struct file_operations binder_##name##_fops = { \
99 .owner = THIS_MODULE, \
100 .open = binder_##name##_open, \
101 .read = seq_read, \
102 .llseek = seq_lseek, \
103 .release = single_release, \
104}
105
106static int binder_proc_show(struct seq_file *m, void *unused);
107BINDER_DEBUG_ENTRY(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900108
109/* This is only defined in include/asm-arm/sizes.h */
110#ifndef SZ_1K
111#define SZ_1K 0x400
112#endif
113
114#ifndef SZ_4M
115#define SZ_4M 0x400000
116#endif
117
118#define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
119
120#define BINDER_SMALL_BUF_SIZE (PAGE_SIZE * 64)
121
122enum {
123 BINDER_DEBUG_USER_ERROR = 1U << 0,
124 BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1,
125 BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2,
126 BINDER_DEBUG_OPEN_CLOSE = 1U << 3,
127 BINDER_DEBUG_DEAD_BINDER = 1U << 4,
128 BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5,
129 BINDER_DEBUG_READ_WRITE = 1U << 6,
130 BINDER_DEBUG_USER_REFS = 1U << 7,
131 BINDER_DEBUG_THREADS = 1U << 8,
132 BINDER_DEBUG_TRANSACTION = 1U << 9,
133 BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10,
134 BINDER_DEBUG_FREE_BUFFER = 1U << 11,
135 BINDER_DEBUG_INTERNAL_REFS = 1U << 12,
Todd Kjosd325d372016-10-10 10:40:53 -0700136 BINDER_DEBUG_PRIORITY_CAP = 1U << 13,
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700137 BINDER_DEBUG_SPINLOCKS = 1U << 14,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900138};
139static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR |
140 BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION;
Harsh Shandilya174562a2017-12-22 19:37:02 +0530141module_param_named(debug_mask, binder_debug_mask, uint, 0644);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900142
Martijn Coenen6b7c7122016-09-30 16:08:09 +0200143static char *binder_devices_param = CONFIG_ANDROID_BINDER_DEVICES;
144module_param_named(devices, binder_devices_param, charp, S_IRUGO);
145
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900146static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
147static int binder_stop_on_user_error;
148
149static int binder_set_stop_on_user_error(const char *val,
Kees Cook24da2c82017-10-17 19:04:42 -0700150 const struct kernel_param *kp)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900151{
152 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +0900153
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900154 ret = param_set_int(val, kp);
155 if (binder_stop_on_user_error < 2)
156 wake_up(&binder_user_error_wait);
157 return ret;
158}
159module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
Harsh Shandilya174562a2017-12-22 19:37:02 +0530160 param_get_int, &binder_stop_on_user_error, 0644);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900161
162#define binder_debug(mask, x...) \
163 do { \
164 if (binder_debug_mask & mask) \
Sherwin Soltani258767f2012-06-26 02:00:30 -0400165 pr_info(x); \
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900166 } while (0)
167
168#define binder_user_error(x...) \
169 do { \
170 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
Sherwin Soltani258767f2012-06-26 02:00:30 -0400171 pr_info(x); \
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900172 if (binder_stop_on_user_error) \
173 binder_stop_on_user_error = 2; \
174 } while (0)
175
Martijn Coenen00c80372016-07-13 12:06:49 +0200176#define to_flat_binder_object(hdr) \
177 container_of(hdr, struct flat_binder_object, hdr)
178
179#define to_binder_fd_object(hdr) container_of(hdr, struct binder_fd_object, hdr)
180
Martijn Coenen5a6da532016-09-30 14:10:07 +0200181#define to_binder_buffer_object(hdr) \
182 container_of(hdr, struct binder_buffer_object, hdr)
183
Martijn Coenene3e0f4802016-10-18 13:58:55 +0200184#define to_binder_fd_array_object(hdr) \
185 container_of(hdr, struct binder_fd_array_object, hdr)
186
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900187enum binder_stat_types {
188 BINDER_STAT_PROC,
189 BINDER_STAT_THREAD,
190 BINDER_STAT_NODE,
191 BINDER_STAT_REF,
192 BINDER_STAT_DEATH,
193 BINDER_STAT_TRANSACTION,
194 BINDER_STAT_TRANSACTION_COMPLETE,
195 BINDER_STAT_COUNT
196};
197
198struct binder_stats {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -0700199 atomic_t br[_IOC_NR(BR_FAILED_REPLY) + 1];
200 atomic_t bc[_IOC_NR(BC_REPLY_SG) + 1];
201 atomic_t obj_created[BINDER_STAT_COUNT];
202 atomic_t obj_deleted[BINDER_STAT_COUNT];
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900203};
204
205static struct binder_stats binder_stats;
206
207static inline void binder_stats_deleted(enum binder_stat_types type)
208{
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -0700209 atomic_inc(&binder_stats.obj_deleted[type]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900210}
211
212static inline void binder_stats_created(enum binder_stat_types type)
213{
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -0700214 atomic_inc(&binder_stats.obj_created[type]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900215}
216
217struct binder_transaction_log_entry {
218 int debug_id;
Todd Kjos1cfe6272017-05-24 13:33:28 -0700219 int debug_id_done;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900220 int call_type;
221 int from_proc;
222 int from_thread;
223 int target_handle;
224 int to_proc;
225 int to_thread;
226 int to_node;
227 int data_size;
228 int offsets_size;
Todd Kjose598d172017-03-22 17:19:52 -0700229 int return_error_line;
230 uint32_t return_error;
231 uint32_t return_error_param;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +0200232 const char *context_name;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900233};
234struct binder_transaction_log {
Todd Kjos1cfe6272017-05-24 13:33:28 -0700235 atomic_t cur;
236 bool full;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900237 struct binder_transaction_log_entry entry[32];
238};
239static struct binder_transaction_log binder_transaction_log;
240static struct binder_transaction_log binder_transaction_log_failed;
241
242static struct binder_transaction_log_entry *binder_transaction_log_add(
243 struct binder_transaction_log *log)
244{
245 struct binder_transaction_log_entry *e;
Todd Kjos1cfe6272017-05-24 13:33:28 -0700246 unsigned int cur = atomic_inc_return(&log->cur);
Seunghun Lee10f62862014-05-01 01:30:23 +0900247
Todd Kjos1cfe6272017-05-24 13:33:28 -0700248 if (cur >= ARRAY_SIZE(log->entry))
Gustavo A. R. Silvae62dd6f2018-01-23 12:04:27 -0600249 log->full = true;
Todd Kjos1cfe6272017-05-24 13:33:28 -0700250 e = &log->entry[cur % ARRAY_SIZE(log->entry)];
251 WRITE_ONCE(e->debug_id_done, 0);
252 /*
253 * write-barrier to synchronize access to e->debug_id_done.
254 * We make sure the initialized 0 value is seen before
255 * memset() other fields are zeroed by memset.
256 */
257 smp_wmb();
258 memset(e, 0, sizeof(*e));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900259 return e;
260}
261
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200262struct binder_context {
263 struct binder_node *binder_context_mgr_node;
Todd Kjos8d9f6f32016-10-17 12:33:15 -0700264 struct mutex context_mgr_node_lock;
265
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200266 kuid_t binder_context_mgr_uid;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +0200267 const char *name;
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200268};
269
Martijn Coenen6b7c7122016-09-30 16:08:09 +0200270struct binder_device {
271 struct hlist_node hlist;
272 struct miscdevice miscdev;
273 struct binder_context context;
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200274};
275
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700276/**
277 * struct binder_work - work enqueued on a worklist
278 * @entry: node enqueued on list
279 * @type: type of work to be performed
280 *
281 * There are separate work lists for proc, thread, and node (async).
282 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900283struct binder_work {
284 struct list_head entry;
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700285
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900286 enum {
287 BINDER_WORK_TRANSACTION = 1,
288 BINDER_WORK_TRANSACTION_COMPLETE,
Todd Kjos858b8da2017-04-21 17:35:12 -0700289 BINDER_WORK_RETURN_ERROR,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900290 BINDER_WORK_NODE,
291 BINDER_WORK_DEAD_BINDER,
292 BINDER_WORK_DEAD_BINDER_AND_CLEAR,
293 BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
294 } type;
295};
296
Todd Kjos858b8da2017-04-21 17:35:12 -0700297struct binder_error {
298 struct binder_work work;
299 uint32_t cmd;
300};
301
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700302/**
303 * struct binder_node - binder node bookkeeping
304 * @debug_id: unique ID for debugging
305 * (invariant after initialized)
306 * @lock: lock for node fields
307 * @work: worklist element for node work
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700308 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700309 * @rb_node: element for proc->nodes tree
Todd Kjos425d23f2017-06-12 12:07:26 -0700310 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700311 * @dead_node: element for binder_dead_nodes list
312 * (protected by binder_dead_nodes_lock)
313 * @proc: binder_proc that owns this node
314 * (invariant after initialized)
315 * @refs: list of references on this node
Todd Kjoscbcbbd62017-06-08 13:45:59 -0700316 * (protected by @lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700317 * @internal_strong_refs: used to take strong references when
318 * initiating a transaction
Todd Kjose7f23ed2017-03-21 13:06:01 -0700319 * (protected by @proc->inner_lock if @proc
320 * and by @lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700321 * @local_weak_refs: weak user refs from local process
Todd Kjose7f23ed2017-03-21 13:06:01 -0700322 * (protected by @proc->inner_lock if @proc
323 * and by @lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700324 * @local_strong_refs: strong user refs from local process
Todd Kjose7f23ed2017-03-21 13:06:01 -0700325 * (protected by @proc->inner_lock if @proc
326 * and by @lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700327 * @tmp_refs: temporary kernel refs
Todd Kjose7f23ed2017-03-21 13:06:01 -0700328 * (protected by @proc->inner_lock while @proc
329 * is valid, and by binder_dead_nodes_lock
330 * if @proc is NULL. During inc/dec and node release
331 * it is also protected by @lock to provide safety
332 * as the node dies and @proc becomes NULL)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700333 * @ptr: userspace pointer for node
334 * (invariant, no lock needed)
335 * @cookie: userspace cookie for node
336 * (invariant, no lock needed)
337 * @has_strong_ref: userspace notified of strong ref
Todd Kjose7f23ed2017-03-21 13:06:01 -0700338 * (protected by @proc->inner_lock if @proc
339 * and by @lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700340 * @pending_strong_ref: userspace has acked notification of strong ref
Todd Kjose7f23ed2017-03-21 13:06:01 -0700341 * (protected by @proc->inner_lock if @proc
342 * and by @lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700343 * @has_weak_ref: userspace notified of weak ref
Todd Kjose7f23ed2017-03-21 13:06:01 -0700344 * (protected by @proc->inner_lock if @proc
345 * and by @lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700346 * @pending_weak_ref: userspace has acked notification of weak ref
Todd Kjose7f23ed2017-03-21 13:06:01 -0700347 * (protected by @proc->inner_lock if @proc
348 * and by @lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700349 * @has_async_transaction: async transaction to node in progress
Todd Kjoscbcbbd62017-06-08 13:45:59 -0700350 * (protected by @lock)
Martijn Coenen6aac9792017-06-07 09:29:14 -0700351 * @sched_policy: minimum scheduling policy for node
352 * (invariant after initialized)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700353 * @accept_fds: file descriptor operations supported for node
354 * (invariant after initialized)
355 * @min_priority: minimum scheduling priority
356 * (invariant after initialized)
Martijn Coenenc46810c2017-06-23 10:13:43 -0700357 * @inherit_rt: inherit RT scheduling policy from caller
Todd Kjos63e0afa2019-01-14 09:10:21 -0800358 * @txn_security_ctx: require sender's security context
Martijn Coenenc46810c2017-06-23 10:13:43 -0700359 * (invariant after initialized)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700360 * @async_todo: list of async work items
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700361 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700362 *
363 * Bookkeeping structure for binder nodes.
364 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900365struct binder_node {
366 int debug_id;
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700367 spinlock_t lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900368 struct binder_work work;
369 union {
370 struct rb_node rb_node;
371 struct hlist_node dead_node;
372 };
373 struct binder_proc *proc;
374 struct hlist_head refs;
375 int internal_strong_refs;
376 int local_weak_refs;
377 int local_strong_refs;
Todd Kjosf22abc72017-05-09 11:08:05 -0700378 int tmp_refs;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800379 binder_uintptr_t ptr;
380 binder_uintptr_t cookie;
Todd Kjose7f23ed2017-03-21 13:06:01 -0700381 struct {
382 /*
383 * bitfield elements protected by
384 * proc inner_lock
385 */
386 u8 has_strong_ref:1;
387 u8 pending_strong_ref:1;
388 u8 has_weak_ref:1;
389 u8 pending_weak_ref:1;
390 };
391 struct {
392 /*
393 * invariant after initialization
394 */
Martijn Coenen6aac9792017-06-07 09:29:14 -0700395 u8 sched_policy:2;
Martijn Coenenc46810c2017-06-23 10:13:43 -0700396 u8 inherit_rt:1;
Todd Kjose7f23ed2017-03-21 13:06:01 -0700397 u8 accept_fds:1;
Todd Kjos63e0afa2019-01-14 09:10:21 -0800398 u8 txn_security_ctx:1;
Todd Kjose7f23ed2017-03-21 13:06:01 -0700399 u8 min_priority;
400 };
401 bool has_async_transaction;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900402 struct list_head async_todo;
403};
404
405struct binder_ref_death {
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700406 /**
407 * @work: worklist element for death notifications
408 * (protected by inner_lock of the proc that
409 * this ref belongs to)
410 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900411 struct binder_work work;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800412 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900413};
414
Todd Kjosb0117bb2017-05-08 09:16:27 -0700415/**
416 * struct binder_ref_data - binder_ref counts and id
417 * @debug_id: unique ID for the ref
418 * @desc: unique userspace handle for ref
419 * @strong: strong ref count (debugging only if not locked)
420 * @weak: weak ref count (debugging only if not locked)
421 *
422 * Structure to hold ref count and ref id information. Since
423 * the actual ref can only be accessed with a lock, this structure
424 * is used to return information about the ref to callers of
425 * ref inc/dec functions.
426 */
427struct binder_ref_data {
428 int debug_id;
429 uint32_t desc;
430 int strong;
431 int weak;
432};
433
434/**
435 * struct binder_ref - struct to track references on nodes
436 * @data: binder_ref_data containing id, handle, and current refcounts
437 * @rb_node_desc: node for lookup by @data.desc in proc's rb_tree
438 * @rb_node_node: node for lookup by @node in proc's rb_tree
439 * @node_entry: list entry for node->refs list in target node
Todd Kjoscbcbbd62017-06-08 13:45:59 -0700440 * (protected by @node->lock)
Todd Kjosb0117bb2017-05-08 09:16:27 -0700441 * @proc: binder_proc containing ref
442 * @node: binder_node of target node. When cleaning up a
443 * ref for deletion in binder_cleanup_ref, a non-NULL
444 * @node indicates the node must be freed
445 * @death: pointer to death notification (ref_death) if requested
Martijn Coenenf9eac642017-05-22 11:26:23 -0700446 * (protected by @node->lock)
Todd Kjosb0117bb2017-05-08 09:16:27 -0700447 *
448 * Structure to track references from procA to target node (on procB). This
449 * structure is unsafe to access without holding @proc->outer_lock.
450 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900451struct binder_ref {
452 /* Lookups needed: */
453 /* node + proc => ref (transaction) */
454 /* desc + proc => ref (transaction, inc/dec ref) */
455 /* node => refs + procs (proc exit) */
Todd Kjosb0117bb2017-05-08 09:16:27 -0700456 struct binder_ref_data data;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900457 struct rb_node rb_node_desc;
458 struct rb_node rb_node_node;
459 struct hlist_node node_entry;
460 struct binder_proc *proc;
461 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900462 struct binder_ref_death *death;
463};
464
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900465enum binder_deferred_state {
Martijn Coenen6f7e5f92018-06-15 11:53:36 +0200466 BINDER_DEFERRED_PUT_FILES = 0x01,
467 BINDER_DEFERRED_FLUSH = 0x02,
468 BINDER_DEFERRED_RELEASE = 0x04,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900469};
470
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700471/**
Martijn Coenen57b2ac62017-06-06 17:04:42 -0700472 * struct binder_priority - scheduler policy and priority
473 * @sched_policy scheduler policy
474 * @prio [100..139] for SCHED_NORMAL, [0..99] for FIFO/RT
475 *
476 * The binder driver supports inheriting the following scheduler policies:
477 * SCHED_NORMAL
478 * SCHED_BATCH
479 * SCHED_FIFO
480 * SCHED_RR
481 */
482struct binder_priority {
483 unsigned int sched_policy;
484 int prio;
485};
486
487/**
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700488 * struct binder_proc - binder process bookkeeping
489 * @proc_node: element for binder_procs list
490 * @threads: rbtree of binder_threads in this proc
Todd Kjosb4827902017-05-25 15:52:17 -0700491 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700492 * @nodes: rbtree of binder nodes associated with
493 * this proc ordered by node->ptr
Todd Kjos425d23f2017-06-12 12:07:26 -0700494 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700495 * @refs_by_desc: rbtree of refs ordered by ref->desc
Todd Kjos5346bf32016-10-20 16:43:34 -0700496 * (protected by @outer_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700497 * @refs_by_node: rbtree of refs ordered by ref->node
Todd Kjos5346bf32016-10-20 16:43:34 -0700498 * (protected by @outer_lock)
Martijn Coenen22d64e4322017-06-02 11:15:44 -0700499 * @waiting_threads: threads currently waiting for proc work
500 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700501 * @pid PID of group_leader of process
502 * (invariant after initialized)
503 * @tsk task_struct for group_leader of process
504 * (invariant after initialized)
Martijn Coenen6f7e5f92018-06-15 11:53:36 +0200505 * @files files_struct for process
Todd Kjosfbb43392017-11-27 09:32:33 -0800506 * (protected by @files_lock)
507 * @files_lock mutex to protect @files
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700508 * @deferred_work_node: element for binder_deferred_list
509 * (protected by binder_deferred_lock)
510 * @deferred_work: bitmap of deferred work to perform
511 * (protected by binder_deferred_lock)
512 * @is_dead: process is dead and awaiting free
513 * when outstanding transactions are cleaned up
Todd Kjosb4827902017-05-25 15:52:17 -0700514 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700515 * @todo: list of work for this process
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700516 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700517 * @stats: per-process binder statistics
518 * (atomics, no lock needed)
519 * @delivered_death: list of delivered death notification
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700520 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700521 * @max_threads: cap on number of binder threads
Todd Kjosd600e902017-05-25 17:35:02 -0700522 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700523 * @requested_threads: number of binder threads requested but not
524 * yet started. In current implementation, can
525 * only be 0 or 1.
Todd Kjosd600e902017-05-25 17:35:02 -0700526 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700527 * @requested_threads_started: number binder threads started
Todd Kjosd600e902017-05-25 17:35:02 -0700528 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700529 * @tmp_ref: temporary reference to indicate proc is in use
Todd Kjosdc734d72019-06-10 09:14:25 -0700530 * (atomic since @proc->inner_lock cannot
531 * always be acquired)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700532 * @default_priority: default scheduler priority
533 * (invariant after initialized)
534 * @debugfs_entry: debugfs node
535 * @alloc: binder allocator bookkeeping
536 * @context: binder_context for this proc
537 * (invariant after initialized)
538 * @inner_lock: can nest under outer_lock and/or node lock
539 * @outer_lock: no nesting under innor or node lock
540 * Lock order: 1) outer, 2) node, 3) inner
541 *
542 * Bookkeeping structure for binder processes
543 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900544struct binder_proc {
545 struct hlist_node proc_node;
546 struct rb_root threads;
547 struct rb_root nodes;
548 struct rb_root refs_by_desc;
549 struct rb_root refs_by_node;
Martijn Coenen22d64e4322017-06-02 11:15:44 -0700550 struct list_head waiting_threads;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900551 int pid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900552 struct task_struct *tsk;
Martijn Coenen6f7e5f92018-06-15 11:53:36 +0200553 struct files_struct *files;
Todd Kjosfbb43392017-11-27 09:32:33 -0800554 struct mutex files_lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900555 struct hlist_node deferred_work_node;
556 int deferred_work;
Todd Kjos2f993e22017-05-12 14:42:55 -0700557 bool is_dead;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900558
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900559 struct list_head todo;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900560 struct binder_stats stats;
561 struct list_head delivered_death;
562 int max_threads;
563 int requested_threads;
564 int requested_threads_started;
Todd Kjosdc734d72019-06-10 09:14:25 -0700565 atomic_t tmp_ref;
Martijn Coenen57b2ac62017-06-06 17:04:42 -0700566 struct binder_priority default_priority;
Arve Hjønnevåg16b66552009-04-28 20:57:50 -0700567 struct dentry *debugfs_entry;
Todd Kjosf85d2292016-10-10 10:39:59 -0700568 struct binder_alloc alloc;
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200569 struct binder_context *context;
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700570 spinlock_t inner_lock;
571 spinlock_t outer_lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900572};
573
574enum {
575 BINDER_LOOPER_STATE_REGISTERED = 0x01,
576 BINDER_LOOPER_STATE_ENTERED = 0x02,
577 BINDER_LOOPER_STATE_EXITED = 0x04,
578 BINDER_LOOPER_STATE_INVALID = 0x08,
579 BINDER_LOOPER_STATE_WAITING = 0x10,
Martijn Coenen22d64e4322017-06-02 11:15:44 -0700580 BINDER_LOOPER_STATE_POLL = 0x20,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900581};
582
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700583/**
584 * struct binder_thread - binder thread bookkeeping
585 * @proc: binder process for this thread
586 * (invariant after initialization)
587 * @rb_node: element for proc->threads rbtree
Todd Kjosb4827902017-05-25 15:52:17 -0700588 * (protected by @proc->inner_lock)
Martijn Coenen22d64e4322017-06-02 11:15:44 -0700589 * @waiting_thread_node: element for @proc->waiting_threads list
590 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700591 * @pid: PID for this thread
592 * (invariant after initialization)
593 * @looper: bitmap of looping state
594 * (only accessed by this thread)
595 * @looper_needs_return: looping thread needs to exit driver
596 * (no lock needed)
597 * @transaction_stack: stack of in-progress transactions for this thread
Martijn Coenen995a36e2017-06-02 13:36:52 -0700598 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700599 * @todo: list of work to do for this thread
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700600 * (protected by @proc->inner_lock)
Martijn Coenen1af61802017-10-19 15:04:46 +0200601 * @process_todo: whether work in @todo should be processed
602 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700603 * @return_error: transaction errors reported by this thread
604 * (only accessed by this thread)
605 * @reply_error: transaction errors reported by target thread
Martijn Coenen995a36e2017-06-02 13:36:52 -0700606 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700607 * @wait: wait queue for thread work
608 * @stats: per-thread statistics
609 * (atomics, no lock needed)
610 * @tmp_ref: temporary reference to indicate thread is in use
611 * (atomic since @proc->inner_lock cannot
612 * always be acquired)
613 * @is_dead: thread is dead and awaiting free
614 * when outstanding transactions are cleaned up
Todd Kjosb4827902017-05-25 15:52:17 -0700615 * (protected by @proc->inner_lock)
Martijn Coenen07a30fe2017-06-07 10:02:12 -0700616 * @task: struct task_struct for this thread
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700617 *
618 * Bookkeeping structure for binder threads.
619 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900620struct binder_thread {
621 struct binder_proc *proc;
622 struct rb_node rb_node;
Martijn Coenen22d64e4322017-06-02 11:15:44 -0700623 struct list_head waiting_thread_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900624 int pid;
Todd Kjos6798e6d2017-01-06 14:19:25 -0800625 int looper; /* only modified by this thread */
626 bool looper_need_return; /* can be written by other thread */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900627 struct binder_transaction *transaction_stack;
628 struct list_head todo;
Martijn Coenen1af61802017-10-19 15:04:46 +0200629 bool process_todo;
Todd Kjos858b8da2017-04-21 17:35:12 -0700630 struct binder_error return_error;
631 struct binder_error reply_error;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900632 wait_queue_head_t wait;
633 struct binder_stats stats;
Todd Kjos2f993e22017-05-12 14:42:55 -0700634 atomic_t tmp_ref;
635 bool is_dead;
Martijn Coenen07a30fe2017-06-07 10:02:12 -0700636 struct task_struct *task;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900637};
638
639struct binder_transaction {
640 int debug_id;
641 struct binder_work work;
642 struct binder_thread *from;
643 struct binder_transaction *from_parent;
644 struct binder_proc *to_proc;
645 struct binder_thread *to_thread;
646 struct binder_transaction *to_parent;
647 unsigned need_reply:1;
648 /* unsigned is_dead:1; */ /* not used at the moment */
649
650 struct binder_buffer *buffer;
651 unsigned int code;
652 unsigned int flags;
Martijn Coenen57b2ac62017-06-06 17:04:42 -0700653 struct binder_priority priority;
654 struct binder_priority saved_priority;
Martijn Coenen07a30fe2017-06-07 10:02:12 -0700655 bool set_priority_called;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -0600656 kuid_t sender_euid;
Todd Kjos63e0afa2019-01-14 09:10:21 -0800657 binder_uintptr_t security_ctx;
Todd Kjos2f993e22017-05-12 14:42:55 -0700658 /**
659 * @lock: protects @from, @to_proc, and @to_thread
660 *
661 * @from, @to_proc, and @to_thread can be set to NULL
662 * during thread teardown
663 */
664 spinlock_t lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900665};
666
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700667/**
Todd Kjosd73356a2019-02-08 10:35:16 -0800668 * struct binder_object - union of flat binder object types
669 * @hdr: generic object header
670 * @fbo: binder object (nodes and refs)
671 * @fdo: file descriptor object
672 * @bbo: binder buffer pointer
673 * @fdao: file descriptor array
674 *
675 * Used for type-independent object copies
676 */
677struct binder_object {
678 union {
679 struct binder_object_header hdr;
680 struct flat_binder_object fbo;
681 struct binder_fd_object fdo;
682 struct binder_buffer_object bbo;
683 struct binder_fd_array_object fdao;
684 };
685};
686
687/**
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700688 * binder_proc_lock() - Acquire outer lock for given binder_proc
689 * @proc: struct binder_proc to acquire
690 *
691 * Acquires proc->outer_lock. Used to protect binder_ref
692 * structures associated with the given proc.
693 */
694#define binder_proc_lock(proc) _binder_proc_lock(proc, __LINE__)
695static void
696_binder_proc_lock(struct binder_proc *proc, int line)
697{
698 binder_debug(BINDER_DEBUG_SPINLOCKS,
699 "%s: line=%d\n", __func__, line);
700 spin_lock(&proc->outer_lock);
701}
702
703/**
704 * binder_proc_unlock() - Release spinlock for given binder_proc
705 * @proc: struct binder_proc to acquire
706 *
707 * Release lock acquired via binder_proc_lock()
708 */
709#define binder_proc_unlock(_proc) _binder_proc_unlock(_proc, __LINE__)
710static void
711_binder_proc_unlock(struct binder_proc *proc, int line)
712{
713 binder_debug(BINDER_DEBUG_SPINLOCKS,
714 "%s: line=%d\n", __func__, line);
715 spin_unlock(&proc->outer_lock);
716}
717
718/**
719 * binder_inner_proc_lock() - Acquire inner lock for given binder_proc
720 * @proc: struct binder_proc to acquire
721 *
722 * Acquires proc->inner_lock. Used to protect todo lists
723 */
724#define binder_inner_proc_lock(proc) _binder_inner_proc_lock(proc, __LINE__)
725static void
726_binder_inner_proc_lock(struct binder_proc *proc, int line)
727{
728 binder_debug(BINDER_DEBUG_SPINLOCKS,
729 "%s: line=%d\n", __func__, line);
730 spin_lock(&proc->inner_lock);
731}
732
733/**
734 * binder_inner_proc_unlock() - Release inner lock for given binder_proc
735 * @proc: struct binder_proc to acquire
736 *
737 * Release lock acquired via binder_inner_proc_lock()
738 */
739#define binder_inner_proc_unlock(proc) _binder_inner_proc_unlock(proc, __LINE__)
740static void
741_binder_inner_proc_unlock(struct binder_proc *proc, int line)
742{
743 binder_debug(BINDER_DEBUG_SPINLOCKS,
744 "%s: line=%d\n", __func__, line);
745 spin_unlock(&proc->inner_lock);
746}
747
748/**
749 * binder_node_lock() - Acquire spinlock for given binder_node
750 * @node: struct binder_node to acquire
751 *
752 * Acquires node->lock. Used to protect binder_node fields
753 */
754#define binder_node_lock(node) _binder_node_lock(node, __LINE__)
755static void
756_binder_node_lock(struct binder_node *node, int line)
757{
758 binder_debug(BINDER_DEBUG_SPINLOCKS,
759 "%s: line=%d\n", __func__, line);
760 spin_lock(&node->lock);
761}
762
763/**
764 * binder_node_unlock() - Release spinlock for given binder_proc
765 * @node: struct binder_node to acquire
766 *
767 * Release lock acquired via binder_node_lock()
768 */
769#define binder_node_unlock(node) _binder_node_unlock(node, __LINE__)
770static void
771_binder_node_unlock(struct binder_node *node, int line)
772{
773 binder_debug(BINDER_DEBUG_SPINLOCKS,
774 "%s: line=%d\n", __func__, line);
775 spin_unlock(&node->lock);
776}
777
Todd Kjoscbcbbd62017-06-08 13:45:59 -0700778/**
779 * binder_node_inner_lock() - Acquire node and inner locks
780 * @node: struct binder_node to acquire
781 *
782 * Acquires node->lock. If node->proc also acquires
783 * proc->inner_lock. Used to protect binder_node fields
784 */
785#define binder_node_inner_lock(node) _binder_node_inner_lock(node, __LINE__)
786static void
787_binder_node_inner_lock(struct binder_node *node, int line)
788{
789 binder_debug(BINDER_DEBUG_SPINLOCKS,
790 "%s: line=%d\n", __func__, line);
791 spin_lock(&node->lock);
792 if (node->proc)
793 binder_inner_proc_lock(node->proc);
794}
795
796/**
797 * binder_node_unlock() - Release node and inner locks
798 * @node: struct binder_node to acquire
799 *
800 * Release lock acquired via binder_node_lock()
801 */
802#define binder_node_inner_unlock(node) _binder_node_inner_unlock(node, __LINE__)
803static void
804_binder_node_inner_unlock(struct binder_node *node, int line)
805{
806 struct binder_proc *proc = node->proc;
807
808 binder_debug(BINDER_DEBUG_SPINLOCKS,
809 "%s: line=%d\n", __func__, line);
810 if (proc)
811 binder_inner_proc_unlock(proc);
812 spin_unlock(&node->lock);
813}
814
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700815static bool binder_worklist_empty_ilocked(struct list_head *list)
816{
817 return list_empty(list);
818}
819
820/**
821 * binder_worklist_empty() - Check if no items on the work list
822 * @proc: binder_proc associated with list
823 * @list: list to check
824 *
825 * Return: true if there are no items on list, else false
826 */
827static bool binder_worklist_empty(struct binder_proc *proc,
828 struct list_head *list)
829{
830 bool ret;
831
832 binder_inner_proc_lock(proc);
833 ret = binder_worklist_empty_ilocked(list);
834 binder_inner_proc_unlock(proc);
835 return ret;
836}
837
Martijn Coenen1af61802017-10-19 15:04:46 +0200838/**
839 * binder_enqueue_work_ilocked() - Add an item to the work list
840 * @work: struct binder_work to add to list
841 * @target_list: list to add work to
842 *
843 * Adds the work to the specified list. Asserts that work
844 * is not already on a list.
845 *
846 * Requires the proc->inner_lock to be held.
847 */
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700848static void
849binder_enqueue_work_ilocked(struct binder_work *work,
850 struct list_head *target_list)
851{
852 BUG_ON(target_list == NULL);
853 BUG_ON(work->entry.next && !list_empty(&work->entry));
854 list_add_tail(&work->entry, target_list);
855}
856
857/**
Martijn Coenendac2e9c2017-11-13 09:55:21 +0100858 * binder_enqueue_deferred_thread_work_ilocked() - Add deferred thread work
Martijn Coenen1af61802017-10-19 15:04:46 +0200859 * @thread: thread to queue work to
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700860 * @work: struct binder_work to add to list
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700861 *
Martijn Coenen1af61802017-10-19 15:04:46 +0200862 * Adds the work to the todo list of the thread. Doesn't set the process_todo
863 * flag, which means that (if it wasn't already set) the thread will go to
864 * sleep without handling this work when it calls read.
865 *
866 * Requires the proc->inner_lock to be held.
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700867 */
868static void
Martijn Coenendac2e9c2017-11-13 09:55:21 +0100869binder_enqueue_deferred_thread_work_ilocked(struct binder_thread *thread,
870 struct binder_work *work)
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700871{
Martijn Coenen1af61802017-10-19 15:04:46 +0200872 binder_enqueue_work_ilocked(work, &thread->todo);
873}
874
875/**
876 * binder_enqueue_thread_work_ilocked() - Add an item to the thread work list
877 * @thread: thread to queue work to
878 * @work: struct binder_work to add to list
879 *
880 * Adds the work to the todo list of the thread, and enables processing
881 * of the todo queue.
882 *
883 * Requires the proc->inner_lock to be held.
884 */
885static void
886binder_enqueue_thread_work_ilocked(struct binder_thread *thread,
887 struct binder_work *work)
888{
889 binder_enqueue_work_ilocked(work, &thread->todo);
890 thread->process_todo = true;
891}
892
893/**
894 * binder_enqueue_thread_work() - Add an item to the thread work list
895 * @thread: thread to queue work to
896 * @work: struct binder_work to add to list
897 *
898 * Adds the work to the todo list of the thread, and enables processing
899 * of the todo queue.
900 */
901static void
902binder_enqueue_thread_work(struct binder_thread *thread,
903 struct binder_work *work)
904{
905 binder_inner_proc_lock(thread->proc);
906 binder_enqueue_thread_work_ilocked(thread, work);
907 binder_inner_proc_unlock(thread->proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700908}
909
910static void
911binder_dequeue_work_ilocked(struct binder_work *work)
912{
913 list_del_init(&work->entry);
914}
915
916/**
917 * binder_dequeue_work() - Removes an item from the work list
918 * @proc: binder_proc associated with list
919 * @work: struct binder_work to remove from list
920 *
921 * Removes the specified work item from whatever list it is on.
922 * Can safely be called if work is not on any list.
923 */
924static void
925binder_dequeue_work(struct binder_proc *proc, struct binder_work *work)
926{
927 binder_inner_proc_lock(proc);
928 binder_dequeue_work_ilocked(work);
929 binder_inner_proc_unlock(proc);
930}
931
932static struct binder_work *binder_dequeue_work_head_ilocked(
933 struct list_head *list)
934{
935 struct binder_work *w;
936
937 w = list_first_entry_or_null(list, struct binder_work, entry);
938 if (w)
939 list_del_init(&w->entry);
940 return w;
941}
942
943/**
944 * binder_dequeue_work_head() - Dequeues the item at head of list
945 * @proc: binder_proc associated with list
946 * @list: list to dequeue head
947 *
948 * Removes the head of the list if there are items on the list
949 *
950 * Return: pointer dequeued binder_work, NULL if list was empty
951 */
952static struct binder_work *binder_dequeue_work_head(
953 struct binder_proc *proc,
954 struct list_head *list)
955{
956 struct binder_work *w;
957
958 binder_inner_proc_lock(proc);
959 w = binder_dequeue_work_head_ilocked(list);
960 binder_inner_proc_unlock(proc);
961 return w;
962}
963
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900964static void
965binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
Todd Kjos2f993e22017-05-12 14:42:55 -0700966static void binder_free_thread(struct binder_thread *thread);
967static void binder_free_proc(struct binder_proc *proc);
Todd Kjos425d23f2017-06-12 12:07:26 -0700968static void binder_inc_node_tmpref_ilocked(struct binder_node *node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900969
Sachin Kamatefde99c2012-08-17 16:39:36 +0530970static int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900971{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900972 unsigned long rlim_cur;
973 unsigned long irqs;
Todd Kjosfbb43392017-11-27 09:32:33 -0800974 int ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900975
Todd Kjosfbb43392017-11-27 09:32:33 -0800976 mutex_lock(&proc->files_lock);
977 if (proc->files == NULL) {
978 ret = -ESRCH;
979 goto err;
980 }
981 if (!lock_task_sighand(proc->tsk, &irqs)) {
982 ret = -EMFILE;
983 goto err;
984 }
Al Virodcfadfa2012-08-12 17:27:30 -0400985 rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
986 unlock_task_sighand(proc->tsk, &irqs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900987
Todd Kjosfbb43392017-11-27 09:32:33 -0800988 ret = __alloc_fd(proc->files, 0, rlim_cur, flags);
989err:
990 mutex_unlock(&proc->files_lock);
991 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900992}
993
994/*
995 * copied from fd_install
996 */
997static void task_fd_install(
998 struct binder_proc *proc, unsigned int fd, struct file *file)
999{
Todd Kjosfbb43392017-11-27 09:32:33 -08001000 mutex_lock(&proc->files_lock);
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02001001 if (proc->files)
1002 __fd_install(proc->files, fd, file);
Todd Kjosfbb43392017-11-27 09:32:33 -08001003 mutex_unlock(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001004}
1005
1006/*
1007 * copied from sys_close
1008 */
1009static long task_close_fd(struct binder_proc *proc, unsigned int fd)
1010{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001011 int retval;
1012
Todd Kjosfbb43392017-11-27 09:32:33 -08001013 mutex_lock(&proc->files_lock);
1014 if (proc->files == NULL) {
1015 retval = -ESRCH;
1016 goto err;
1017 }
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02001018 retval = __close_fd(proc->files, fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001019 /* can't restart close syscall because file table entry was cleared */
1020 if (unlikely(retval == -ERESTARTSYS ||
1021 retval == -ERESTARTNOINTR ||
1022 retval == -ERESTARTNOHAND ||
1023 retval == -ERESTART_RESTARTBLOCK))
1024 retval = -EINTR;
Todd Kjosfbb43392017-11-27 09:32:33 -08001025err:
1026 mutex_unlock(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001027 return retval;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001028}
1029
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001030static bool binder_has_work_ilocked(struct binder_thread *thread,
1031 bool do_proc_work)
1032{
Martijn Coenen1af61802017-10-19 15:04:46 +02001033 return thread->process_todo ||
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001034 thread->looper_need_return ||
1035 (do_proc_work &&
1036 !binder_worklist_empty_ilocked(&thread->proc->todo));
1037}
1038
1039static bool binder_has_work(struct binder_thread *thread, bool do_proc_work)
1040{
1041 bool has_work;
1042
1043 binder_inner_proc_lock(thread->proc);
1044 has_work = binder_has_work_ilocked(thread, do_proc_work);
1045 binder_inner_proc_unlock(thread->proc);
1046
1047 return has_work;
1048}
1049
1050static bool binder_available_for_proc_work_ilocked(struct binder_thread *thread)
1051{
1052 return !thread->transaction_stack &&
1053 binder_worklist_empty_ilocked(&thread->todo) &&
1054 (thread->looper & (BINDER_LOOPER_STATE_ENTERED |
1055 BINDER_LOOPER_STATE_REGISTERED));
1056}
1057
1058static void binder_wakeup_poll_threads_ilocked(struct binder_proc *proc,
1059 bool sync)
1060{
1061 struct rb_node *n;
1062 struct binder_thread *thread;
1063
1064 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
1065 thread = rb_entry(n, struct binder_thread, rb_node);
1066 if (thread->looper & BINDER_LOOPER_STATE_POLL &&
1067 binder_available_for_proc_work_ilocked(thread)) {
1068 if (sync)
1069 wake_up_interruptible_sync(&thread->wait);
1070 else
1071 wake_up_interruptible(&thread->wait);
1072 }
1073 }
1074}
1075
Martijn Coenen053be422017-06-06 15:17:46 -07001076/**
1077 * binder_select_thread_ilocked() - selects a thread for doing proc work.
1078 * @proc: process to select a thread from
1079 *
1080 * Note that calling this function moves the thread off the waiting_threads
1081 * list, so it can only be woken up by the caller of this function, or a
1082 * signal. Therefore, callers *should* always wake up the thread this function
1083 * returns.
1084 *
1085 * Return: If there's a thread currently waiting for process work,
1086 * returns that thread. Otherwise returns NULL.
1087 */
1088static struct binder_thread *
1089binder_select_thread_ilocked(struct binder_proc *proc)
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001090{
1091 struct binder_thread *thread;
1092
Martijn Coenened323352017-07-27 23:52:24 +02001093 assert_spin_locked(&proc->inner_lock);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001094 thread = list_first_entry_or_null(&proc->waiting_threads,
1095 struct binder_thread,
1096 waiting_thread_node);
1097
Martijn Coenen053be422017-06-06 15:17:46 -07001098 if (thread)
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001099 list_del_init(&thread->waiting_thread_node);
Martijn Coenen053be422017-06-06 15:17:46 -07001100
1101 return thread;
1102}
1103
1104/**
1105 * binder_wakeup_thread_ilocked() - wakes up a thread for doing proc work.
1106 * @proc: process to wake up a thread in
1107 * @thread: specific thread to wake-up (may be NULL)
1108 * @sync: whether to do a synchronous wake-up
1109 *
1110 * This function wakes up a thread in the @proc process.
1111 * The caller may provide a specific thread to wake-up in
1112 * the @thread parameter. If @thread is NULL, this function
1113 * will wake up threads that have called poll().
1114 *
1115 * Note that for this function to work as expected, callers
1116 * should first call binder_select_thread() to find a thread
1117 * to handle the work (if they don't have a thread already),
1118 * and pass the result into the @thread parameter.
1119 */
1120static void binder_wakeup_thread_ilocked(struct binder_proc *proc,
1121 struct binder_thread *thread,
1122 bool sync)
1123{
Martijn Coenened323352017-07-27 23:52:24 +02001124 assert_spin_locked(&proc->inner_lock);
Martijn Coenen053be422017-06-06 15:17:46 -07001125
1126 if (thread) {
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001127 if (sync)
1128 wake_up_interruptible_sync(&thread->wait);
1129 else
1130 wake_up_interruptible(&thread->wait);
1131 return;
1132 }
1133
1134 /* Didn't find a thread waiting for proc work; this can happen
1135 * in two scenarios:
1136 * 1. All threads are busy handling transactions
1137 * In that case, one of those threads should call back into
1138 * the kernel driver soon and pick up this work.
1139 * 2. Threads are using the (e)poll interface, in which case
1140 * they may be blocked on the waitqueue without having been
1141 * added to waiting_threads. For this case, we just iterate
1142 * over all threads not handling transaction work, and
1143 * wake them all up. We wake all because we don't know whether
1144 * a thread that called into (e)poll is handling non-binder
1145 * work currently.
1146 */
1147 binder_wakeup_poll_threads_ilocked(proc, sync);
1148}
1149
Martijn Coenen053be422017-06-06 15:17:46 -07001150static void binder_wakeup_proc_ilocked(struct binder_proc *proc)
1151{
1152 struct binder_thread *thread = binder_select_thread_ilocked(proc);
1153
1154 binder_wakeup_thread_ilocked(proc, thread, /* sync = */false);
1155}
1156
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001157static bool is_rt_policy(int policy)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001158{
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001159 return policy == SCHED_FIFO || policy == SCHED_RR;
1160}
Seunghun Lee10f62862014-05-01 01:30:23 +09001161
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001162static bool is_fair_policy(int policy)
1163{
1164 return policy == SCHED_NORMAL || policy == SCHED_BATCH;
1165}
1166
1167static bool binder_supported_policy(int policy)
1168{
1169 return is_fair_policy(policy) || is_rt_policy(policy);
1170}
1171
1172static int to_userspace_prio(int policy, int kernel_priority)
1173{
1174 if (is_fair_policy(policy))
1175 return PRIO_TO_NICE(kernel_priority);
1176 else
1177 return MAX_USER_RT_PRIO - 1 - kernel_priority;
1178}
1179
1180static int to_kernel_prio(int policy, int user_priority)
1181{
1182 if (is_fair_policy(policy))
1183 return NICE_TO_PRIO(user_priority);
1184 else
1185 return MAX_USER_RT_PRIO - 1 - user_priority;
1186}
1187
Martijn Coenenecd972d2017-05-26 10:48:56 -07001188static void binder_do_set_priority(struct task_struct *task,
1189 struct binder_priority desired,
1190 bool verify)
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001191{
1192 int priority; /* user-space prio value */
1193 bool has_cap_nice;
1194 unsigned int policy = desired.sched_policy;
1195
1196 if (task->policy == policy && task->normal_prio == desired.prio)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001197 return;
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001198
1199 has_cap_nice = has_capability_noaudit(task, CAP_SYS_NICE);
1200
1201 priority = to_userspace_prio(policy, desired.prio);
1202
Martijn Coenenecd972d2017-05-26 10:48:56 -07001203 if (verify && is_rt_policy(policy) && !has_cap_nice) {
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001204 long max_rtprio = task_rlimit(task, RLIMIT_RTPRIO);
1205
1206 if (max_rtprio == 0) {
1207 policy = SCHED_NORMAL;
1208 priority = MIN_NICE;
1209 } else if (priority > max_rtprio) {
1210 priority = max_rtprio;
1211 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001212 }
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001213
Martijn Coenenecd972d2017-05-26 10:48:56 -07001214 if (verify && is_fair_policy(policy) && !has_cap_nice) {
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001215 long min_nice = rlimit_to_nice(task_rlimit(task, RLIMIT_NICE));
1216
1217 if (min_nice > MAX_NICE) {
1218 binder_user_error("%d RLIMIT_NICE not set\n",
1219 task->pid);
1220 return;
1221 } else if (priority < min_nice) {
1222 priority = min_nice;
1223 }
1224 }
1225
1226 if (policy != desired.sched_policy ||
1227 to_kernel_prio(policy, priority) != desired.prio)
1228 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
1229 "%d: priority %d not allowed, using %d instead\n",
1230 task->pid, desired.prio,
1231 to_kernel_prio(policy, priority));
1232
Martijn Coenen81402ea2017-05-08 09:33:22 -07001233 trace_binder_set_priority(task->tgid, task->pid, task->normal_prio,
1234 to_kernel_prio(policy, priority),
1235 desired.prio);
1236
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001237 /* Set the actual priority */
1238 if (task->policy != policy || is_rt_policy(policy)) {
1239 struct sched_param params;
1240
1241 params.sched_priority = is_rt_policy(policy) ? priority : 0;
1242
1243 sched_setscheduler_nocheck(task,
1244 policy | SCHED_RESET_ON_FORK,
1245 &params);
1246 }
1247 if (is_fair_policy(policy))
1248 set_user_nice(task, priority);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001249}
1250
Martijn Coenenecd972d2017-05-26 10:48:56 -07001251static void binder_set_priority(struct task_struct *task,
1252 struct binder_priority desired)
1253{
1254 binder_do_set_priority(task, desired, /* verify = */ true);
1255}
1256
1257static void binder_restore_priority(struct task_struct *task,
1258 struct binder_priority desired)
1259{
1260 binder_do_set_priority(task, desired, /* verify = */ false);
1261}
1262
Martijn Coenen07a30fe2017-06-07 10:02:12 -07001263static void binder_transaction_priority(struct task_struct *task,
1264 struct binder_transaction *t,
Martijn Coenenc46810c2017-06-23 10:13:43 -07001265 struct binder_priority node_prio,
1266 bool inherit_rt)
Martijn Coenen07a30fe2017-06-07 10:02:12 -07001267{
Ganesh Mahendran9add7c42017-09-27 15:12:25 +08001268 struct binder_priority desired_prio = t->priority;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07001269
1270 if (t->set_priority_called)
1271 return;
1272
1273 t->set_priority_called = true;
1274 t->saved_priority.sched_policy = task->policy;
1275 t->saved_priority.prio = task->normal_prio;
1276
Martijn Coenenc46810c2017-06-23 10:13:43 -07001277 if (!inherit_rt && is_rt_policy(desired_prio.sched_policy)) {
1278 desired_prio.prio = NICE_TO_PRIO(0);
1279 desired_prio.sched_policy = SCHED_NORMAL;
Martijn Coenenc46810c2017-06-23 10:13:43 -07001280 }
Martijn Coenen07a30fe2017-06-07 10:02:12 -07001281
1282 if (node_prio.prio < t->priority.prio ||
1283 (node_prio.prio == t->priority.prio &&
1284 node_prio.sched_policy == SCHED_FIFO)) {
1285 /*
1286 * In case the minimum priority on the node is
1287 * higher (lower value), use that priority. If
1288 * the priority is the same, but the node uses
1289 * SCHED_FIFO, prefer SCHED_FIFO, since it can
1290 * run unbounded, unlike SCHED_RR.
1291 */
1292 desired_prio = node_prio;
1293 }
1294
1295 binder_set_priority(task, desired_prio);
1296}
1297
Todd Kjos425d23f2017-06-12 12:07:26 -07001298static struct binder_node *binder_get_node_ilocked(struct binder_proc *proc,
1299 binder_uintptr_t ptr)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001300{
1301 struct rb_node *n = proc->nodes.rb_node;
1302 struct binder_node *node;
1303
Martijn Coenened323352017-07-27 23:52:24 +02001304 assert_spin_locked(&proc->inner_lock);
Todd Kjos425d23f2017-06-12 12:07:26 -07001305
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001306 while (n) {
1307 node = rb_entry(n, struct binder_node, rb_node);
1308
1309 if (ptr < node->ptr)
1310 n = n->rb_left;
1311 else if (ptr > node->ptr)
1312 n = n->rb_right;
Todd Kjosf22abc72017-05-09 11:08:05 -07001313 else {
1314 /*
1315 * take an implicit weak reference
1316 * to ensure node stays alive until
1317 * call to binder_put_node()
1318 */
Todd Kjos425d23f2017-06-12 12:07:26 -07001319 binder_inc_node_tmpref_ilocked(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001320 return node;
Todd Kjosf22abc72017-05-09 11:08:05 -07001321 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001322 }
1323 return NULL;
1324}
1325
Todd Kjos425d23f2017-06-12 12:07:26 -07001326static struct binder_node *binder_get_node(struct binder_proc *proc,
1327 binder_uintptr_t ptr)
1328{
1329 struct binder_node *node;
1330
1331 binder_inner_proc_lock(proc);
1332 node = binder_get_node_ilocked(proc, ptr);
1333 binder_inner_proc_unlock(proc);
1334 return node;
1335}
1336
1337static struct binder_node *binder_init_node_ilocked(
1338 struct binder_proc *proc,
1339 struct binder_node *new_node,
1340 struct flat_binder_object *fp)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001341{
1342 struct rb_node **p = &proc->nodes.rb_node;
1343 struct rb_node *parent = NULL;
1344 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001345 binder_uintptr_t ptr = fp ? fp->binder : 0;
1346 binder_uintptr_t cookie = fp ? fp->cookie : 0;
1347 __u32 flags = fp ? fp->flags : 0;
Martijn Coenen6aac9792017-06-07 09:29:14 -07001348 s8 priority;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001349
Martijn Coenened323352017-07-27 23:52:24 +02001350 assert_spin_locked(&proc->inner_lock);
1351
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001352 while (*p) {
Todd Kjos425d23f2017-06-12 12:07:26 -07001353
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001354 parent = *p;
1355 node = rb_entry(parent, struct binder_node, rb_node);
1356
1357 if (ptr < node->ptr)
1358 p = &(*p)->rb_left;
1359 else if (ptr > node->ptr)
1360 p = &(*p)->rb_right;
Todd Kjos425d23f2017-06-12 12:07:26 -07001361 else {
1362 /*
1363 * A matching node is already in
1364 * the rb tree. Abandon the init
1365 * and return it.
1366 */
1367 binder_inc_node_tmpref_ilocked(node);
1368 return node;
1369 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001370 }
Todd Kjos425d23f2017-06-12 12:07:26 -07001371 node = new_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001372 binder_stats_created(BINDER_STAT_NODE);
Todd Kjosf22abc72017-05-09 11:08:05 -07001373 node->tmp_refs++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001374 rb_link_node(&node->rb_node, parent, p);
1375 rb_insert_color(&node->rb_node, &proc->nodes);
Todd Kjosc4bd08b2017-05-25 10:56:00 -07001376 node->debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001377 node->proc = proc;
1378 node->ptr = ptr;
1379 node->cookie = cookie;
1380 node->work.type = BINDER_WORK_NODE;
Martijn Coenen6aac9792017-06-07 09:29:14 -07001381 priority = flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
Ganesh Mahendran6cd26312017-09-26 17:56:25 +08001382 node->sched_policy = (flags & FLAT_BINDER_FLAG_SCHED_POLICY_MASK) >>
Martijn Coenen6aac9792017-06-07 09:29:14 -07001383 FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT;
1384 node->min_priority = to_kernel_prio(node->sched_policy, priority);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001385 node->accept_fds = !!(flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
Martijn Coenenc46810c2017-06-23 10:13:43 -07001386 node->inherit_rt = !!(flags & FLAT_BINDER_FLAG_INHERIT_RT);
Todd Kjos63e0afa2019-01-14 09:10:21 -08001387 node->txn_security_ctx = !!(flags & FLAT_BINDER_FLAG_TXN_SECURITY_CTX);
Todd Kjosfc7a7e22017-05-29 16:44:24 -07001388 spin_lock_init(&node->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001389 INIT_LIST_HEAD(&node->work.entry);
1390 INIT_LIST_HEAD(&node->async_todo);
1391 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001392 "%d:%d node %d u%016llx c%016llx created\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001393 proc->pid, current->pid, node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001394 (u64)node->ptr, (u64)node->cookie);
Todd Kjos425d23f2017-06-12 12:07:26 -07001395
1396 return node;
1397}
1398
1399static struct binder_node *binder_new_node(struct binder_proc *proc,
1400 struct flat_binder_object *fp)
1401{
1402 struct binder_node *node;
1403 struct binder_node *new_node = kzalloc(sizeof(*node), GFP_KERNEL);
1404
1405 if (!new_node)
1406 return NULL;
1407 binder_inner_proc_lock(proc);
1408 node = binder_init_node_ilocked(proc, new_node, fp);
1409 binder_inner_proc_unlock(proc);
1410 if (node != new_node)
1411 /*
1412 * The node was already added by another thread
1413 */
1414 kfree(new_node);
1415
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001416 return node;
1417}
1418
Todd Kjose7f23ed2017-03-21 13:06:01 -07001419static void binder_free_node(struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001420{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001421 kfree(node);
1422 binder_stats_deleted(BINDER_STAT_NODE);
1423}
1424
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001425static int binder_inc_node_nilocked(struct binder_node *node, int strong,
1426 int internal,
1427 struct list_head *target_list)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001428{
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001429 struct binder_proc *proc = node->proc;
1430
Martijn Coenened323352017-07-27 23:52:24 +02001431 assert_spin_locked(&node->lock);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001432 if (proc)
Martijn Coenened323352017-07-27 23:52:24 +02001433 assert_spin_locked(&proc->inner_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001434 if (strong) {
1435 if (internal) {
1436 if (target_list == NULL &&
1437 node->internal_strong_refs == 0 &&
Martijn Coenen0b3311e2016-09-30 15:51:48 +02001438 !(node->proc &&
1439 node == node->proc->context->
1440 binder_context_mgr_node &&
1441 node->has_strong_ref)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301442 pr_err("invalid inc strong node for %d\n",
1443 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001444 return -EINVAL;
1445 }
1446 node->internal_strong_refs++;
1447 } else
1448 node->local_strong_refs++;
1449 if (!node->has_strong_ref && target_list) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001450 binder_dequeue_work_ilocked(&node->work);
Martijn Coenen1af61802017-10-19 15:04:46 +02001451 /*
1452 * Note: this function is the only place where we queue
1453 * directly to a thread->todo without using the
1454 * corresponding binder_enqueue_thread_work() helper
1455 * functions; in this case it's ok to not set the
1456 * process_todo flag, since we know this node work will
1457 * always be followed by other work that starts queue
1458 * processing: in case of synchronous transactions, a
1459 * BR_REPLY or BR_ERROR; in case of oneway
1460 * transactions, a BR_TRANSACTION_COMPLETE.
1461 */
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001462 binder_enqueue_work_ilocked(&node->work, target_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001463 }
1464 } else {
1465 if (!internal)
1466 node->local_weak_refs++;
1467 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
1468 if (target_list == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301469 pr_err("invalid inc weak node for %d\n",
1470 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001471 return -EINVAL;
1472 }
Martijn Coenen1af61802017-10-19 15:04:46 +02001473 /*
1474 * See comment above
1475 */
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001476 binder_enqueue_work_ilocked(&node->work, target_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001477 }
1478 }
1479 return 0;
1480}
1481
Todd Kjose7f23ed2017-03-21 13:06:01 -07001482static int binder_inc_node(struct binder_node *node, int strong, int internal,
1483 struct list_head *target_list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001484{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001485 int ret;
1486
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001487 binder_node_inner_lock(node);
1488 ret = binder_inc_node_nilocked(node, strong, internal, target_list);
1489 binder_node_inner_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001490
1491 return ret;
1492}
1493
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001494static bool binder_dec_node_nilocked(struct binder_node *node,
1495 int strong, int internal)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001496{
1497 struct binder_proc *proc = node->proc;
1498
Martijn Coenened323352017-07-27 23:52:24 +02001499 assert_spin_locked(&node->lock);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001500 if (proc)
Martijn Coenened323352017-07-27 23:52:24 +02001501 assert_spin_locked(&proc->inner_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001502 if (strong) {
1503 if (internal)
1504 node->internal_strong_refs--;
1505 else
1506 node->local_strong_refs--;
1507 if (node->local_strong_refs || node->internal_strong_refs)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001508 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001509 } else {
1510 if (!internal)
1511 node->local_weak_refs--;
Todd Kjosf22abc72017-05-09 11:08:05 -07001512 if (node->local_weak_refs || node->tmp_refs ||
1513 !hlist_empty(&node->refs))
Todd Kjose7f23ed2017-03-21 13:06:01 -07001514 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001515 }
Todd Kjose7f23ed2017-03-21 13:06:01 -07001516
1517 if (proc && (node->has_strong_ref || node->has_weak_ref)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001518 if (list_empty(&node->work.entry)) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001519 binder_enqueue_work_ilocked(&node->work, &proc->todo);
Martijn Coenen053be422017-06-06 15:17:46 -07001520 binder_wakeup_proc_ilocked(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001521 }
1522 } else {
1523 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
Todd Kjosf22abc72017-05-09 11:08:05 -07001524 !node->local_weak_refs && !node->tmp_refs) {
Todd Kjose7f23ed2017-03-21 13:06:01 -07001525 if (proc) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001526 binder_dequeue_work_ilocked(&node->work);
1527 rb_erase(&node->rb_node, &proc->nodes);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001528 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301529 "refless node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001530 node->debug_id);
1531 } else {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001532 BUG_ON(!list_empty(&node->work.entry));
Todd Kjos8d9f6f32016-10-17 12:33:15 -07001533 spin_lock(&binder_dead_nodes_lock);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001534 /*
1535 * tmp_refs could have changed so
1536 * check it again
1537 */
1538 if (node->tmp_refs) {
1539 spin_unlock(&binder_dead_nodes_lock);
1540 return false;
1541 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001542 hlist_del(&node->dead_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07001543 spin_unlock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001544 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301545 "dead node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001546 node->debug_id);
1547 }
Todd Kjose7f23ed2017-03-21 13:06:01 -07001548 return true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001549 }
1550 }
Todd Kjose7f23ed2017-03-21 13:06:01 -07001551 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001552}
1553
Todd Kjose7f23ed2017-03-21 13:06:01 -07001554static void binder_dec_node(struct binder_node *node, int strong, int internal)
1555{
1556 bool free_node;
1557
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001558 binder_node_inner_lock(node);
1559 free_node = binder_dec_node_nilocked(node, strong, internal);
1560 binder_node_inner_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001561 if (free_node)
1562 binder_free_node(node);
1563}
1564
1565static void binder_inc_node_tmpref_ilocked(struct binder_node *node)
Todd Kjosf22abc72017-05-09 11:08:05 -07001566{
1567 /*
1568 * No call to binder_inc_node() is needed since we
1569 * don't need to inform userspace of any changes to
1570 * tmp_refs
1571 */
1572 node->tmp_refs++;
1573}
1574
1575/**
Todd Kjose7f23ed2017-03-21 13:06:01 -07001576 * binder_inc_node_tmpref() - take a temporary reference on node
1577 * @node: node to reference
1578 *
1579 * Take reference on node to prevent the node from being freed
1580 * while referenced only by a local variable. The inner lock is
1581 * needed to serialize with the node work on the queue (which
1582 * isn't needed after the node is dead). If the node is dead
1583 * (node->proc is NULL), use binder_dead_nodes_lock to protect
1584 * node->tmp_refs against dead-node-only cases where the node
1585 * lock cannot be acquired (eg traversing the dead node list to
1586 * print nodes)
1587 */
1588static void binder_inc_node_tmpref(struct binder_node *node)
1589{
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001590 binder_node_lock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001591 if (node->proc)
1592 binder_inner_proc_lock(node->proc);
1593 else
1594 spin_lock(&binder_dead_nodes_lock);
1595 binder_inc_node_tmpref_ilocked(node);
1596 if (node->proc)
1597 binder_inner_proc_unlock(node->proc);
1598 else
1599 spin_unlock(&binder_dead_nodes_lock);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001600 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001601}
1602
1603/**
Todd Kjosf22abc72017-05-09 11:08:05 -07001604 * binder_dec_node_tmpref() - remove a temporary reference on node
1605 * @node: node to reference
1606 *
1607 * Release temporary reference on node taken via binder_inc_node_tmpref()
1608 */
1609static void binder_dec_node_tmpref(struct binder_node *node)
1610{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001611 bool free_node;
1612
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001613 binder_node_inner_lock(node);
1614 if (!node->proc)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001615 spin_lock(&binder_dead_nodes_lock);
Todd Kjosf22abc72017-05-09 11:08:05 -07001616 node->tmp_refs--;
1617 BUG_ON(node->tmp_refs < 0);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001618 if (!node->proc)
1619 spin_unlock(&binder_dead_nodes_lock);
Todd Kjosf22abc72017-05-09 11:08:05 -07001620 /*
1621 * Call binder_dec_node() to check if all refcounts are 0
1622 * and cleanup is needed. Calling with strong=0 and internal=1
1623 * causes no actual reference to be released in binder_dec_node().
1624 * If that changes, a change is needed here too.
1625 */
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001626 free_node = binder_dec_node_nilocked(node, 0, 1);
1627 binder_node_inner_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001628 if (free_node)
1629 binder_free_node(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07001630}
1631
1632static void binder_put_node(struct binder_node *node)
1633{
1634 binder_dec_node_tmpref(node);
1635}
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001636
Todd Kjos5346bf32016-10-20 16:43:34 -07001637static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc,
1638 u32 desc, bool need_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001639{
1640 struct rb_node *n = proc->refs_by_desc.rb_node;
1641 struct binder_ref *ref;
1642
1643 while (n) {
1644 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1645
Todd Kjosb0117bb2017-05-08 09:16:27 -07001646 if (desc < ref->data.desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001647 n = n->rb_left;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001648 } else if (desc > ref->data.desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001649 n = n->rb_right;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001650 } else if (need_strong_ref && !ref->data.strong) {
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001651 binder_user_error("tried to use weak ref as strong ref\n");
1652 return NULL;
1653 } else {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001654 return ref;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001655 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001656 }
1657 return NULL;
1658}
1659
Todd Kjosb0117bb2017-05-08 09:16:27 -07001660/**
Todd Kjos5346bf32016-10-20 16:43:34 -07001661 * binder_get_ref_for_node_olocked() - get the ref associated with given node
Todd Kjosb0117bb2017-05-08 09:16:27 -07001662 * @proc: binder_proc that owns the ref
1663 * @node: binder_node of target
1664 * @new_ref: newly allocated binder_ref to be initialized or %NULL
1665 *
1666 * Look up the ref for the given node and return it if it exists
1667 *
1668 * If it doesn't exist and the caller provides a newly allocated
1669 * ref, initialize the fields of the newly allocated ref and insert
1670 * into the given proc rb_trees and node refs list.
1671 *
1672 * Return: the ref for node. It is possible that another thread
1673 * allocated/initialized the ref first in which case the
1674 * returned ref would be different than the passed-in
1675 * new_ref. new_ref must be kfree'd by the caller in
1676 * this case.
1677 */
Todd Kjos5346bf32016-10-20 16:43:34 -07001678static struct binder_ref *binder_get_ref_for_node_olocked(
1679 struct binder_proc *proc,
1680 struct binder_node *node,
1681 struct binder_ref *new_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001682{
Todd Kjosb0117bb2017-05-08 09:16:27 -07001683 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001684 struct rb_node **p = &proc->refs_by_node.rb_node;
1685 struct rb_node *parent = NULL;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001686 struct binder_ref *ref;
1687 struct rb_node *n;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001688
1689 while (*p) {
1690 parent = *p;
1691 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1692
1693 if (node < ref->node)
1694 p = &(*p)->rb_left;
1695 else if (node > ref->node)
1696 p = &(*p)->rb_right;
1697 else
1698 return ref;
1699 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001700 if (!new_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001701 return NULL;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001702
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001703 binder_stats_created(BINDER_STAT_REF);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001704 new_ref->data.debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001705 new_ref->proc = proc;
1706 new_ref->node = node;
1707 rb_link_node(&new_ref->rb_node_node, parent, p);
1708 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1709
Todd Kjosb0117bb2017-05-08 09:16:27 -07001710 new_ref->data.desc = (node == context->binder_context_mgr_node) ? 0 : 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001711 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1712 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001713 if (ref->data.desc > new_ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001714 break;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001715 new_ref->data.desc = ref->data.desc + 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001716 }
1717
1718 p = &proc->refs_by_desc.rb_node;
1719 while (*p) {
1720 parent = *p;
1721 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1722
Todd Kjosb0117bb2017-05-08 09:16:27 -07001723 if (new_ref->data.desc < ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001724 p = &(*p)->rb_left;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001725 else if (new_ref->data.desc > ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001726 p = &(*p)->rb_right;
1727 else
1728 BUG();
1729 }
1730 rb_link_node(&new_ref->rb_node_desc, parent, p);
1731 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001732
1733 binder_node_lock(node);
Todd Kjos4cbe5752017-05-01 17:21:51 -07001734 hlist_add_head(&new_ref->node_entry, &node->refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001735
Todd Kjos4cbe5752017-05-01 17:21:51 -07001736 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1737 "%d new ref %d desc %d for node %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001738 proc->pid, new_ref->data.debug_id, new_ref->data.desc,
Todd Kjos4cbe5752017-05-01 17:21:51 -07001739 node->debug_id);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001740 binder_node_unlock(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001741 return new_ref;
1742}
1743
Todd Kjos5346bf32016-10-20 16:43:34 -07001744static void binder_cleanup_ref_olocked(struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001745{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001746 bool delete_node = false;
Todd Kjose7f23ed2017-03-21 13:06:01 -07001747
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001748 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301749 "%d delete ref %d desc %d for node %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001750 ref->proc->pid, ref->data.debug_id, ref->data.desc,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301751 ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001752
1753 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1754 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001755
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001756 binder_node_inner_lock(ref->node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001757 if (ref->data.strong)
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001758 binder_dec_node_nilocked(ref->node, 1, 1);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001759
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001760 hlist_del(&ref->node_entry);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001761 delete_node = binder_dec_node_nilocked(ref->node, 0, 1);
1762 binder_node_inner_unlock(ref->node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001763 /*
1764 * Clear ref->node unless we want the caller to free the node
1765 */
1766 if (!delete_node) {
1767 /*
1768 * The caller uses ref->node to determine
1769 * whether the node needs to be freed. Clear
1770 * it since the node is still alive.
1771 */
1772 ref->node = NULL;
1773 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001774
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001775 if (ref->death) {
1776 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301777 "%d delete ref %d desc %d has death notification\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001778 ref->proc->pid, ref->data.debug_id,
1779 ref->data.desc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001780 binder_dequeue_work(ref->proc, &ref->death->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001781 binder_stats_deleted(BINDER_STAT_DEATH);
1782 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001783 binder_stats_deleted(BINDER_STAT_REF);
1784}
1785
Todd Kjosb0117bb2017-05-08 09:16:27 -07001786/**
Todd Kjos5346bf32016-10-20 16:43:34 -07001787 * binder_inc_ref_olocked() - increment the ref for given handle
Todd Kjosb0117bb2017-05-08 09:16:27 -07001788 * @ref: ref to be incremented
1789 * @strong: if true, strong increment, else weak
1790 * @target_list: list to queue node work on
1791 *
Todd Kjos5346bf32016-10-20 16:43:34 -07001792 * Increment the ref. @ref->proc->outer_lock must be held on entry
Todd Kjosb0117bb2017-05-08 09:16:27 -07001793 *
1794 * Return: 0, if successful, else errno
1795 */
Todd Kjos5346bf32016-10-20 16:43:34 -07001796static int binder_inc_ref_olocked(struct binder_ref *ref, int strong,
1797 struct list_head *target_list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001798{
1799 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +09001800
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001801 if (strong) {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001802 if (ref->data.strong == 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001803 ret = binder_inc_node(ref->node, 1, 1, target_list);
1804 if (ret)
1805 return ret;
1806 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001807 ref->data.strong++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001808 } else {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001809 if (ref->data.weak == 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001810 ret = binder_inc_node(ref->node, 0, 1, target_list);
1811 if (ret)
1812 return ret;
1813 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001814 ref->data.weak++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001815 }
1816 return 0;
1817}
1818
Todd Kjosb0117bb2017-05-08 09:16:27 -07001819/**
1820 * binder_dec_ref() - dec the ref for given handle
1821 * @ref: ref to be decremented
1822 * @strong: if true, strong decrement, else weak
1823 *
1824 * Decrement the ref.
1825 *
Todd Kjosb0117bb2017-05-08 09:16:27 -07001826 * Return: true if ref is cleaned up and ready to be freed
1827 */
Todd Kjos5346bf32016-10-20 16:43:34 -07001828static bool binder_dec_ref_olocked(struct binder_ref *ref, int strong)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001829{
1830 if (strong) {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001831 if (ref->data.strong == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301832 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001833 ref->proc->pid, ref->data.debug_id,
1834 ref->data.desc, ref->data.strong,
1835 ref->data.weak);
1836 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001837 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001838 ref->data.strong--;
Todd Kjose7f23ed2017-03-21 13:06:01 -07001839 if (ref->data.strong == 0)
1840 binder_dec_node(ref->node, strong, 1);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001841 } else {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001842 if (ref->data.weak == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301843 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001844 ref->proc->pid, ref->data.debug_id,
1845 ref->data.desc, ref->data.strong,
1846 ref->data.weak);
1847 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001848 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001849 ref->data.weak--;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001850 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001851 if (ref->data.strong == 0 && ref->data.weak == 0) {
Todd Kjos5346bf32016-10-20 16:43:34 -07001852 binder_cleanup_ref_olocked(ref);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001853 return true;
1854 }
1855 return false;
1856}
1857
1858/**
1859 * binder_get_node_from_ref() - get the node from the given proc/desc
1860 * @proc: proc containing the ref
1861 * @desc: the handle associated with the ref
1862 * @need_strong_ref: if true, only return node if ref is strong
1863 * @rdata: the id/refcount data for the ref
1864 *
1865 * Given a proc and ref handle, return the associated binder_node
1866 *
1867 * Return: a binder_node or NULL if not found or not strong when strong required
1868 */
1869static struct binder_node *binder_get_node_from_ref(
1870 struct binder_proc *proc,
1871 u32 desc, bool need_strong_ref,
1872 struct binder_ref_data *rdata)
1873{
1874 struct binder_node *node;
1875 struct binder_ref *ref;
1876
Todd Kjos5346bf32016-10-20 16:43:34 -07001877 binder_proc_lock(proc);
1878 ref = binder_get_ref_olocked(proc, desc, need_strong_ref);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001879 if (!ref)
1880 goto err_no_ref;
1881 node = ref->node;
Todd Kjosf22abc72017-05-09 11:08:05 -07001882 /*
1883 * Take an implicit reference on the node to ensure
1884 * it stays alive until the call to binder_put_node()
1885 */
1886 binder_inc_node_tmpref(node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001887 if (rdata)
1888 *rdata = ref->data;
Todd Kjos5346bf32016-10-20 16:43:34 -07001889 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001890
1891 return node;
1892
1893err_no_ref:
Todd Kjos5346bf32016-10-20 16:43:34 -07001894 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001895 return NULL;
1896}
1897
1898/**
1899 * binder_free_ref() - free the binder_ref
1900 * @ref: ref to free
1901 *
Todd Kjose7f23ed2017-03-21 13:06:01 -07001902 * Free the binder_ref. Free the binder_node indicated by ref->node
1903 * (if non-NULL) and the binder_ref_death indicated by ref->death.
Todd Kjosb0117bb2017-05-08 09:16:27 -07001904 */
1905static void binder_free_ref(struct binder_ref *ref)
1906{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001907 if (ref->node)
1908 binder_free_node(ref->node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001909 kfree(ref->death);
1910 kfree(ref);
1911}
1912
1913/**
1914 * binder_update_ref_for_handle() - inc/dec the ref for given handle
1915 * @proc: proc containing the ref
1916 * @desc: the handle associated with the ref
1917 * @increment: true=inc reference, false=dec reference
1918 * @strong: true=strong reference, false=weak reference
1919 * @rdata: the id/refcount data for the ref
1920 *
1921 * Given a proc and ref handle, increment or decrement the ref
1922 * according to "increment" arg.
1923 *
1924 * Return: 0 if successful, else errno
1925 */
1926static int binder_update_ref_for_handle(struct binder_proc *proc,
1927 uint32_t desc, bool increment, bool strong,
1928 struct binder_ref_data *rdata)
1929{
1930 int ret = 0;
1931 struct binder_ref *ref;
1932 bool delete_ref = false;
1933
Todd Kjos5346bf32016-10-20 16:43:34 -07001934 binder_proc_lock(proc);
1935 ref = binder_get_ref_olocked(proc, desc, strong);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001936 if (!ref) {
1937 ret = -EINVAL;
1938 goto err_no_ref;
1939 }
1940 if (increment)
Todd Kjos5346bf32016-10-20 16:43:34 -07001941 ret = binder_inc_ref_olocked(ref, strong, NULL);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001942 else
Todd Kjos5346bf32016-10-20 16:43:34 -07001943 delete_ref = binder_dec_ref_olocked(ref, strong);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001944
1945 if (rdata)
1946 *rdata = ref->data;
Todd Kjos5346bf32016-10-20 16:43:34 -07001947 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001948
1949 if (delete_ref)
1950 binder_free_ref(ref);
1951 return ret;
1952
1953err_no_ref:
Todd Kjos5346bf32016-10-20 16:43:34 -07001954 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001955 return ret;
1956}
1957
1958/**
1959 * binder_dec_ref_for_handle() - dec the ref for given handle
1960 * @proc: proc containing the ref
1961 * @desc: the handle associated with the ref
1962 * @strong: true=strong reference, false=weak reference
1963 * @rdata: the id/refcount data for the ref
1964 *
1965 * Just calls binder_update_ref_for_handle() to decrement the ref.
1966 *
1967 * Return: 0 if successful, else errno
1968 */
1969static int binder_dec_ref_for_handle(struct binder_proc *proc,
1970 uint32_t desc, bool strong, struct binder_ref_data *rdata)
1971{
1972 return binder_update_ref_for_handle(proc, desc, false, strong, rdata);
1973}
1974
1975
1976/**
1977 * binder_inc_ref_for_node() - increment the ref for given proc/node
1978 * @proc: proc containing the ref
1979 * @node: target node
1980 * @strong: true=strong reference, false=weak reference
1981 * @target_list: worklist to use if node is incremented
1982 * @rdata: the id/refcount data for the ref
1983 *
1984 * Given a proc and node, increment the ref. Create the ref if it
1985 * doesn't already exist
1986 *
1987 * Return: 0 if successful, else errno
1988 */
1989static int binder_inc_ref_for_node(struct binder_proc *proc,
1990 struct binder_node *node,
1991 bool strong,
1992 struct list_head *target_list,
1993 struct binder_ref_data *rdata)
1994{
1995 struct binder_ref *ref;
1996 struct binder_ref *new_ref = NULL;
1997 int ret = 0;
1998
Todd Kjos5346bf32016-10-20 16:43:34 -07001999 binder_proc_lock(proc);
2000 ref = binder_get_ref_for_node_olocked(proc, node, NULL);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002001 if (!ref) {
Todd Kjos5346bf32016-10-20 16:43:34 -07002002 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002003 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
2004 if (!new_ref)
2005 return -ENOMEM;
Todd Kjos5346bf32016-10-20 16:43:34 -07002006 binder_proc_lock(proc);
2007 ref = binder_get_ref_for_node_olocked(proc, node, new_ref);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002008 }
Todd Kjos5346bf32016-10-20 16:43:34 -07002009 ret = binder_inc_ref_olocked(ref, strong, target_list);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002010 *rdata = ref->data;
Todd Kjos5346bf32016-10-20 16:43:34 -07002011 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002012 if (new_ref && ref != new_ref)
2013 /*
2014 * Another thread created the ref first so
2015 * free the one we allocated
2016 */
2017 kfree(new_ref);
2018 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002019}
2020
Martijn Coenen995a36e2017-06-02 13:36:52 -07002021static void binder_pop_transaction_ilocked(struct binder_thread *target_thread,
2022 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002023{
Todd Kjos21ef40a2017-03-30 18:02:13 -07002024 BUG_ON(!target_thread);
Martijn Coenened323352017-07-27 23:52:24 +02002025 assert_spin_locked(&target_thread->proc->inner_lock);
Todd Kjos21ef40a2017-03-30 18:02:13 -07002026 BUG_ON(target_thread->transaction_stack != t);
2027 BUG_ON(target_thread->transaction_stack->from != target_thread);
2028 target_thread->transaction_stack =
2029 target_thread->transaction_stack->from_parent;
2030 t->from = NULL;
2031}
2032
Todd Kjos2f993e22017-05-12 14:42:55 -07002033/**
2034 * binder_thread_dec_tmpref() - decrement thread->tmp_ref
2035 * @thread: thread to decrement
2036 *
2037 * A thread needs to be kept alive while being used to create or
2038 * handle a transaction. binder_get_txn_from() is used to safely
2039 * extract t->from from a binder_transaction and keep the thread
2040 * indicated by t->from from being freed. When done with that
2041 * binder_thread, this function is called to decrement the
2042 * tmp_ref and free if appropriate (thread has been released
2043 * and no transaction being processed by the driver)
2044 */
2045static void binder_thread_dec_tmpref(struct binder_thread *thread)
2046{
2047 /*
2048 * atomic is used to protect the counter value while
2049 * it cannot reach zero or thread->is_dead is false
Todd Kjos2f993e22017-05-12 14:42:55 -07002050 */
Todd Kjosb4827902017-05-25 15:52:17 -07002051 binder_inner_proc_lock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002052 atomic_dec(&thread->tmp_ref);
2053 if (thread->is_dead && !atomic_read(&thread->tmp_ref)) {
Todd Kjosb4827902017-05-25 15:52:17 -07002054 binder_inner_proc_unlock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002055 binder_free_thread(thread);
2056 return;
2057 }
Todd Kjosb4827902017-05-25 15:52:17 -07002058 binder_inner_proc_unlock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002059}
2060
2061/**
2062 * binder_proc_dec_tmpref() - decrement proc->tmp_ref
2063 * @proc: proc to decrement
2064 *
2065 * A binder_proc needs to be kept alive while being used to create or
2066 * handle a transaction. proc->tmp_ref is incremented when
2067 * creating a new transaction or the binder_proc is currently in-use
2068 * by threads that are being released. When done with the binder_proc,
2069 * this function is called to decrement the counter and free the
2070 * proc if appropriate (proc has been released, all threads have
2071 * been released and not currenly in-use to process a transaction).
2072 */
2073static void binder_proc_dec_tmpref(struct binder_proc *proc)
2074{
Todd Kjosb4827902017-05-25 15:52:17 -07002075 binder_inner_proc_lock(proc);
Todd Kjosdc734d72019-06-10 09:14:25 -07002076 atomic_dec(&proc->tmp_ref);
Todd Kjos2f993e22017-05-12 14:42:55 -07002077 if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) &&
Todd Kjosdc734d72019-06-10 09:14:25 -07002078 !atomic_read(&proc->tmp_ref)) {
Todd Kjosb4827902017-05-25 15:52:17 -07002079 binder_inner_proc_unlock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002080 binder_free_proc(proc);
2081 return;
2082 }
Todd Kjosb4827902017-05-25 15:52:17 -07002083 binder_inner_proc_unlock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002084}
2085
2086/**
2087 * binder_get_txn_from() - safely extract the "from" thread in transaction
2088 * @t: binder transaction for t->from
2089 *
2090 * Atomically return the "from" thread and increment the tmp_ref
2091 * count for the thread to ensure it stays alive until
2092 * binder_thread_dec_tmpref() is called.
2093 *
2094 * Return: the value of t->from
2095 */
2096static struct binder_thread *binder_get_txn_from(
2097 struct binder_transaction *t)
2098{
2099 struct binder_thread *from;
2100
2101 spin_lock(&t->lock);
2102 from = t->from;
2103 if (from)
2104 atomic_inc(&from->tmp_ref);
2105 spin_unlock(&t->lock);
2106 return from;
2107}
2108
Martijn Coenen995a36e2017-06-02 13:36:52 -07002109/**
2110 * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock
2111 * @t: binder transaction for t->from
2112 *
2113 * Same as binder_get_txn_from() except it also acquires the proc->inner_lock
2114 * to guarantee that the thread cannot be released while operating on it.
2115 * The caller must call binder_inner_proc_unlock() to release the inner lock
2116 * as well as call binder_dec_thread_txn() to release the reference.
2117 *
2118 * Return: the value of t->from
2119 */
2120static struct binder_thread *binder_get_txn_from_and_acq_inner(
2121 struct binder_transaction *t)
2122{
2123 struct binder_thread *from;
2124
2125 from = binder_get_txn_from(t);
2126 if (!from)
2127 return NULL;
2128 binder_inner_proc_lock(from->proc);
2129 if (t->from) {
2130 BUG_ON(from != t->from);
2131 return from;
2132 }
2133 binder_inner_proc_unlock(from->proc);
2134 binder_thread_dec_tmpref(from);
2135 return NULL;
2136}
2137
Todd Kjos21ef40a2017-03-30 18:02:13 -07002138static void binder_free_transaction(struct binder_transaction *t)
2139{
Todd Kjosdc734d72019-06-10 09:14:25 -07002140 struct binder_proc *target_proc;
2141
2142 spin_lock(&t->lock);
2143 target_proc = t->to_proc;
2144 if (target_proc) {
2145 atomic_inc(&target_proc->tmp_ref);
2146 spin_unlock(&t->lock);
2147
2148 binder_inner_proc_lock(target_proc);
2149 if (t->buffer)
2150 t->buffer->transaction = NULL;
2151 binder_inner_proc_unlock(target_proc);
2152 binder_proc_dec_tmpref(target_proc);
2153 } else {
2154 /*
2155 * If the transaction has no target_proc, then
2156 * t->buffer->transaction * has already been cleared.
2157 */
2158 spin_unlock(&t->lock);
2159 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002160 kfree(t);
2161 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2162}
2163
2164static void binder_send_failed_reply(struct binder_transaction *t,
2165 uint32_t error_code)
2166{
2167 struct binder_thread *target_thread;
Lucas Tanured4ec15e2014-07-13 21:31:05 -03002168 struct binder_transaction *next;
Seunghun Lee10f62862014-05-01 01:30:23 +09002169
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002170 BUG_ON(t->flags & TF_ONE_WAY);
2171 while (1) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07002172 target_thread = binder_get_txn_from_and_acq_inner(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002173 if (target_thread) {
Todd Kjos858b8da2017-04-21 17:35:12 -07002174 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2175 "send failed reply for transaction %d to %d:%d\n",
2176 t->debug_id,
2177 target_thread->proc->pid,
2178 target_thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002179
Martijn Coenen995a36e2017-06-02 13:36:52 -07002180 binder_pop_transaction_ilocked(target_thread, t);
Todd Kjos858b8da2017-04-21 17:35:12 -07002181 if (target_thread->reply_error.cmd == BR_OK) {
2182 target_thread->reply_error.cmd = error_code;
Martijn Coenen1af61802017-10-19 15:04:46 +02002183 binder_enqueue_thread_work_ilocked(
2184 target_thread,
2185 &target_thread->reply_error.work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002186 wake_up_interruptible(&target_thread->wait);
2187 } else {
Todd Kjosd3a2afb2018-02-07 12:38:47 -08002188 /*
2189 * Cannot get here for normal operation, but
2190 * we can if multiple synchronous transactions
2191 * are sent without blocking for responses.
2192 * Just ignore the 2nd error in this case.
2193 */
2194 pr_warn("Unexpected reply error: %u\n",
2195 target_thread->reply_error.cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002196 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07002197 binder_inner_proc_unlock(target_thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002198 binder_thread_dec_tmpref(target_thread);
Todd Kjos858b8da2017-04-21 17:35:12 -07002199 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002200 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002201 }
Lucas Tanured4ec15e2014-07-13 21:31:05 -03002202 next = t->from_parent;
2203
2204 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2205 "send failed reply for transaction %d, target dead\n",
2206 t->debug_id);
2207
Todd Kjos21ef40a2017-03-30 18:02:13 -07002208 binder_free_transaction(t);
Lucas Tanured4ec15e2014-07-13 21:31:05 -03002209 if (next == NULL) {
2210 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2211 "reply failed, no target thread at root\n");
2212 return;
2213 }
2214 t = next;
2215 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2216 "reply failed, no target thread -- retry %d\n",
2217 t->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002218 }
2219}
2220
Martijn Coenen00c80372016-07-13 12:06:49 +02002221/**
Martijn Coenen3217ccc2017-08-24 15:23:36 +02002222 * binder_cleanup_transaction() - cleans up undelivered transaction
2223 * @t: transaction that needs to be cleaned up
2224 * @reason: reason the transaction wasn't delivered
2225 * @error_code: error to return to caller (if synchronous call)
2226 */
2227static void binder_cleanup_transaction(struct binder_transaction *t,
2228 const char *reason,
2229 uint32_t error_code)
2230{
2231 if (t->buffer->target_node && !(t->flags & TF_ONE_WAY)) {
2232 binder_send_failed_reply(t, error_code);
2233 } else {
2234 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2235 "undelivered transaction %d, %s\n",
2236 t->debug_id, reason);
2237 binder_free_transaction(t);
2238 }
2239}
2240
2241/**
Todd Kjosd73356a2019-02-08 10:35:16 -08002242 * binder_get_object() - gets object and checks for valid metadata
2243 * @proc: binder_proc owning the buffer
Martijn Coenen00c80372016-07-13 12:06:49 +02002244 * @buffer: binder_buffer that we're parsing.
Todd Kjosd73356a2019-02-08 10:35:16 -08002245 * @offset: offset in the @buffer at which to validate an object.
2246 * @object: struct binder_object to read into
Martijn Coenen00c80372016-07-13 12:06:49 +02002247 *
2248 * Return: If there's a valid metadata object at @offset in @buffer, the
Todd Kjosd73356a2019-02-08 10:35:16 -08002249 * size of that object. Otherwise, it returns zero. The object
2250 * is read into the struct binder_object pointed to by @object.
Martijn Coenen00c80372016-07-13 12:06:49 +02002251 */
Todd Kjosd73356a2019-02-08 10:35:16 -08002252static size_t binder_get_object(struct binder_proc *proc,
2253 struct binder_buffer *buffer,
2254 unsigned long offset,
2255 struct binder_object *object)
Martijn Coenen00c80372016-07-13 12:06:49 +02002256{
Todd Kjosd73356a2019-02-08 10:35:16 -08002257 size_t read_size;
Martijn Coenen00c80372016-07-13 12:06:49 +02002258 struct binder_object_header *hdr;
2259 size_t object_size = 0;
2260
Todd Kjosd73356a2019-02-08 10:35:16 -08002261 read_size = min_t(size_t, sizeof(*object), buffer->data_size - offset);
Todd Kjosc8ddc8c2019-03-19 09:53:01 -07002262 if (offset > buffer->data_size || read_size < sizeof(*hdr) ||
2263 !IS_ALIGNED(offset, sizeof(u32)))
Martijn Coenen00c80372016-07-13 12:06:49 +02002264 return 0;
Todd Kjosd73356a2019-02-08 10:35:16 -08002265 binder_alloc_copy_from_buffer(&proc->alloc, object, buffer,
2266 offset, read_size);
Martijn Coenen00c80372016-07-13 12:06:49 +02002267
Todd Kjosd73356a2019-02-08 10:35:16 -08002268 /* Ok, now see if we read a complete object. */
2269 hdr = &object->hdr;
Martijn Coenen00c80372016-07-13 12:06:49 +02002270 switch (hdr->type) {
2271 case BINDER_TYPE_BINDER:
2272 case BINDER_TYPE_WEAK_BINDER:
2273 case BINDER_TYPE_HANDLE:
2274 case BINDER_TYPE_WEAK_HANDLE:
2275 object_size = sizeof(struct flat_binder_object);
2276 break;
2277 case BINDER_TYPE_FD:
2278 object_size = sizeof(struct binder_fd_object);
2279 break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002280 case BINDER_TYPE_PTR:
2281 object_size = sizeof(struct binder_buffer_object);
2282 break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002283 case BINDER_TYPE_FDA:
2284 object_size = sizeof(struct binder_fd_array_object);
2285 break;
Martijn Coenen00c80372016-07-13 12:06:49 +02002286 default:
2287 return 0;
2288 }
2289 if (offset <= buffer->data_size - object_size &&
2290 buffer->data_size >= object_size)
2291 return object_size;
2292 else
2293 return 0;
2294}
2295
Martijn Coenen5a6da532016-09-30 14:10:07 +02002296/**
2297 * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002298 * @proc: binder_proc owning the buffer
Martijn Coenen5a6da532016-09-30 14:10:07 +02002299 * @b: binder_buffer containing the object
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002300 * @object: struct binder_object to read into
Martijn Coenen5a6da532016-09-30 14:10:07 +02002301 * @index: index in offset array at which the binder_buffer_object is
2302 * located
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002303 * @start_offset: points to the start of the offset array
2304 * @object_offsetp: offset of @object read from @b
Martijn Coenen5a6da532016-09-30 14:10:07 +02002305 * @num_valid: the number of valid offsets in the offset array
2306 *
2307 * Return: If @index is within the valid range of the offset array
2308 * described by @start and @num_valid, and if there's a valid
2309 * binder_buffer_object at the offset found in index @index
2310 * of the offset array, that object is returned. Otherwise,
2311 * %NULL is returned.
2312 * Note that the offset found in index @index itself is not
2313 * verified; this function assumes that @num_valid elements
2314 * from @start were previously verified to have valid offsets.
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002315 * If @object_offsetp is non-NULL, then the offset within
2316 * @b is written to it.
Martijn Coenen5a6da532016-09-30 14:10:07 +02002317 */
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002318static struct binder_buffer_object *binder_validate_ptr(
2319 struct binder_proc *proc,
2320 struct binder_buffer *b,
2321 struct binder_object *object,
2322 binder_size_t index,
2323 binder_size_t start_offset,
2324 binder_size_t *object_offsetp,
2325 binder_size_t num_valid)
Martijn Coenen5a6da532016-09-30 14:10:07 +02002326{
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002327 size_t object_size;
2328 binder_size_t object_offset;
2329 unsigned long buffer_offset;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002330
2331 if (index >= num_valid)
2332 return NULL;
2333
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002334 buffer_offset = start_offset + sizeof(binder_size_t) * index;
2335 binder_alloc_copy_from_buffer(&proc->alloc, &object_offset,
2336 b, buffer_offset, sizeof(object_offset));
2337 object_size = binder_get_object(proc, b, object_offset, object);
2338 if (!object_size || object->hdr.type != BINDER_TYPE_PTR)
Martijn Coenen5a6da532016-09-30 14:10:07 +02002339 return NULL;
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002340 if (object_offsetp)
2341 *object_offsetp = object_offset;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002342
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002343 return &object->bbo;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002344}
2345
2346/**
2347 * binder_validate_fixup() - validates pointer/fd fixups happen in order.
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002348 * @proc: binder_proc owning the buffer
Martijn Coenen5a6da532016-09-30 14:10:07 +02002349 * @b: transaction buffer
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002350 * @objects_start_offset: offset to start of objects buffer
2351 * @buffer_obj_offset: offset to binder_buffer_object in which to fix up
2352 * @fixup_offset: start offset in @buffer to fix up
2353 * @last_obj_offset: offset to last binder_buffer_object that we fixed
2354 * @last_min_offset: minimum fixup offset in object at @last_obj_offset
Martijn Coenen5a6da532016-09-30 14:10:07 +02002355 *
2356 * Return: %true if a fixup in buffer @buffer at offset @offset is
2357 * allowed.
2358 *
2359 * For safety reasons, we only allow fixups inside a buffer to happen
2360 * at increasing offsets; additionally, we only allow fixup on the last
2361 * buffer object that was verified, or one of its parents.
2362 *
2363 * Example of what is allowed:
2364 *
2365 * A
2366 * B (parent = A, offset = 0)
2367 * C (parent = A, offset = 16)
2368 * D (parent = C, offset = 0)
2369 * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
2370 *
2371 * Examples of what is not allowed:
2372 *
2373 * Decreasing offsets within the same parent:
2374 * A
2375 * C (parent = A, offset = 16)
2376 * B (parent = A, offset = 0) // decreasing offset within A
2377 *
2378 * Referring to a parent that wasn't the last object or any of its parents:
2379 * A
2380 * B (parent = A, offset = 0)
2381 * C (parent = A, offset = 0)
2382 * C (parent = A, offset = 16)
2383 * D (parent = B, offset = 0) // B is not A or any of A's parents
2384 */
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002385static bool binder_validate_fixup(struct binder_proc *proc,
2386 struct binder_buffer *b,
2387 binder_size_t objects_start_offset,
2388 binder_size_t buffer_obj_offset,
Martijn Coenen5a6da532016-09-30 14:10:07 +02002389 binder_size_t fixup_offset,
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002390 binder_size_t last_obj_offset,
Martijn Coenen5a6da532016-09-30 14:10:07 +02002391 binder_size_t last_min_offset)
2392{
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002393 if (!last_obj_offset) {
Martijn Coenen5a6da532016-09-30 14:10:07 +02002394 /* Nothing to fix up in */
2395 return false;
2396 }
2397
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002398 while (last_obj_offset != buffer_obj_offset) {
2399 unsigned long buffer_offset;
2400 struct binder_object last_object;
2401 struct binder_buffer_object *last_bbo;
2402 size_t object_size = binder_get_object(proc, b, last_obj_offset,
2403 &last_object);
2404 if (object_size != sizeof(*last_bbo))
2405 return false;
2406
2407 last_bbo = &last_object.bbo;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002408 /*
2409 * Safe to retrieve the parent of last_obj, since it
2410 * was already previously verified by the driver.
2411 */
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002412 if ((last_bbo->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0)
Martijn Coenen5a6da532016-09-30 14:10:07 +02002413 return false;
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002414 last_min_offset = last_bbo->parent_offset + sizeof(uintptr_t);
2415 buffer_offset = objects_start_offset +
2416 sizeof(binder_size_t) * last_bbo->parent,
2417 binder_alloc_copy_from_buffer(&proc->alloc, &last_obj_offset,
2418 b, buffer_offset,
2419 sizeof(last_obj_offset));
Martijn Coenen5a6da532016-09-30 14:10:07 +02002420 }
2421 return (fixup_offset >= last_min_offset);
2422}
2423
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002424static void binder_transaction_buffer_release(struct binder_proc *proc,
2425 struct binder_buffer *buffer,
Todd Kjos8539b1e2019-02-08 10:35:20 -08002426 binder_size_t failed_at,
2427 bool is_failure)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002428{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002429 int debug_id = buffer->debug_id;
Todd Kjos8539b1e2019-02-08 10:35:20 -08002430 binder_size_t off_start_offset, buffer_offset, off_end_offset;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002431
2432 binder_debug(BINDER_DEBUG_TRANSACTION,
Todd Kjos8539b1e2019-02-08 10:35:20 -08002433 "%d buffer release %d, size %zd-%zd, failed at %llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002434 proc->pid, buffer->debug_id,
Todd Kjos8539b1e2019-02-08 10:35:20 -08002435 buffer->data_size, buffer->offsets_size,
2436 (unsigned long long)failed_at);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002437
2438 if (buffer->target_node)
2439 binder_dec_node(buffer->target_node, 1, 0);
2440
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002441 off_start_offset = ALIGN(buffer->data_size, sizeof(void *));
Todd Kjos8539b1e2019-02-08 10:35:20 -08002442 off_end_offset = is_failure ? failed_at :
2443 off_start_offset + buffer->offsets_size;
2444 for (buffer_offset = off_start_offset; buffer_offset < off_end_offset;
2445 buffer_offset += sizeof(binder_size_t)) {
Martijn Coenen00c80372016-07-13 12:06:49 +02002446 struct binder_object_header *hdr;
Todd Kjos90a570c2019-02-08 10:35:15 -08002447 size_t object_size;
Todd Kjosd73356a2019-02-08 10:35:16 -08002448 struct binder_object object;
Todd Kjos90a570c2019-02-08 10:35:15 -08002449 binder_size_t object_offset;
Seunghun Lee10f62862014-05-01 01:30:23 +09002450
Todd Kjos90a570c2019-02-08 10:35:15 -08002451 binder_alloc_copy_from_buffer(&proc->alloc, &object_offset,
2452 buffer, buffer_offset,
2453 sizeof(object_offset));
Todd Kjosd73356a2019-02-08 10:35:16 -08002454 object_size = binder_get_object(proc, buffer,
2455 object_offset, &object);
Martijn Coenen00c80372016-07-13 12:06:49 +02002456 if (object_size == 0) {
2457 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
Todd Kjos90a570c2019-02-08 10:35:15 -08002458 debug_id, (u64)object_offset, buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002459 continue;
2460 }
Todd Kjosd73356a2019-02-08 10:35:16 -08002461 hdr = &object.hdr;
Martijn Coenen00c80372016-07-13 12:06:49 +02002462 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002463 case BINDER_TYPE_BINDER:
2464 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenen00c80372016-07-13 12:06:49 +02002465 struct flat_binder_object *fp;
2466 struct binder_node *node;
Seunghun Lee10f62862014-05-01 01:30:23 +09002467
Martijn Coenen00c80372016-07-13 12:06:49 +02002468 fp = to_flat_binder_object(hdr);
2469 node = binder_get_node(proc, fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002470 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002471 pr_err("transaction release %d bad node %016llx\n",
2472 debug_id, (u64)fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002473 break;
2474 }
2475 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002476 " node %d u%016llx\n",
2477 node->debug_id, (u64)node->ptr);
Martijn Coenen00c80372016-07-13 12:06:49 +02002478 binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER,
2479 0);
Todd Kjosf22abc72017-05-09 11:08:05 -07002480 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002481 } break;
2482 case BINDER_TYPE_HANDLE:
2483 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenen00c80372016-07-13 12:06:49 +02002484 struct flat_binder_object *fp;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002485 struct binder_ref_data rdata;
2486 int ret;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02002487
Martijn Coenen00c80372016-07-13 12:06:49 +02002488 fp = to_flat_binder_object(hdr);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002489 ret = binder_dec_ref_for_handle(proc, fp->handle,
2490 hdr->type == BINDER_TYPE_HANDLE, &rdata);
2491
2492 if (ret) {
2493 pr_err("transaction release %d bad handle %d, ret = %d\n",
2494 debug_id, fp->handle, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002495 break;
2496 }
2497 binder_debug(BINDER_DEBUG_TRANSACTION,
Todd Kjosb0117bb2017-05-08 09:16:27 -07002498 " ref %d desc %d\n",
2499 rdata.debug_id, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002500 } break;
2501
Martijn Coenen00c80372016-07-13 12:06:49 +02002502 case BINDER_TYPE_FD: {
2503 struct binder_fd_object *fp = to_binder_fd_object(hdr);
2504
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002505 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen00c80372016-07-13 12:06:49 +02002506 " fd %d\n", fp->fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002507 if (failed_at)
Martijn Coenen00c80372016-07-13 12:06:49 +02002508 task_close_fd(proc, fp->fd);
2509 } break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002510 case BINDER_TYPE_PTR:
2511 /*
2512 * Nothing to do here, this will get cleaned up when the
2513 * transaction buffer gets freed
2514 */
2515 break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002516 case BINDER_TYPE_FDA: {
2517 struct binder_fd_array_object *fda;
2518 struct binder_buffer_object *parent;
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002519 struct binder_object ptr_object;
Todd Kjos8539b1e2019-02-08 10:35:20 -08002520 binder_size_t fda_offset;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002521 size_t fd_index;
2522 binder_size_t fd_buf_size;
Todd Kjos8539b1e2019-02-08 10:35:20 -08002523 binder_size_t num_valid;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002524
Todd Kjos8539b1e2019-02-08 10:35:20 -08002525 num_valid = (buffer_offset - off_start_offset) /
2526 sizeof(binder_size_t);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002527 fda = to_binder_fd_array_object(hdr);
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002528 parent = binder_validate_ptr(proc, buffer, &ptr_object,
2529 fda->parent,
2530 off_start_offset,
2531 NULL,
Todd Kjos8539b1e2019-02-08 10:35:20 -08002532 num_valid);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002533 if (!parent) {
2534 pr_err("transaction release %d bad parent offset",
2535 debug_id);
2536 continue;
2537 }
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002538 fd_buf_size = sizeof(u32) * fda->num_fds;
2539 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2540 pr_err("transaction release %d invalid number of fds (%lld)\n",
2541 debug_id, (u64)fda->num_fds);
2542 continue;
2543 }
2544 if (fd_buf_size > parent->length ||
2545 fda->parent_offset > parent->length - fd_buf_size) {
2546 /* No space for all file descriptors here. */
2547 pr_err("transaction release %d not enough space for %lld fds in buffer\n",
2548 debug_id, (u64)fda->num_fds);
2549 continue;
2550 }
Todd Kjos8539b1e2019-02-08 10:35:20 -08002551 /*
2552 * the source data for binder_buffer_object is visible
2553 * to user-space and the @buffer element is the user
2554 * pointer to the buffer_object containing the fd_array.
2555 * Convert the address to an offset relative to
2556 * the base of the transaction buffer.
2557 */
2558 fda_offset =
2559 (parent->buffer - (uintptr_t)buffer->user_data) +
2560 fda->parent_offset;
Todd Kjos90a570c2019-02-08 10:35:15 -08002561 for (fd_index = 0; fd_index < fda->num_fds;
2562 fd_index++) {
2563 u32 fd;
Todd Kjos8539b1e2019-02-08 10:35:20 -08002564 binder_size_t offset = fda_offset +
2565 fd_index * sizeof(fd);
Todd Kjos90a570c2019-02-08 10:35:15 -08002566
2567 binder_alloc_copy_from_buffer(&proc->alloc,
2568 &fd,
2569 buffer,
2570 offset,
2571 sizeof(fd));
2572 task_close_fd(proc, fd);
2573 }
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002574 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002575 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01002576 pr_err("transaction release %d bad object type %x\n",
Martijn Coenen00c80372016-07-13 12:06:49 +02002577 debug_id, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002578 break;
2579 }
2580 }
2581}
2582
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002583static int binder_translate_binder(struct flat_binder_object *fp,
2584 struct binder_transaction *t,
2585 struct binder_thread *thread)
2586{
2587 struct binder_node *node;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002588 struct binder_proc *proc = thread->proc;
2589 struct binder_proc *target_proc = t->to_proc;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002590 struct binder_ref_data rdata;
Todd Kjosf22abc72017-05-09 11:08:05 -07002591 int ret = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002592
2593 node = binder_get_node(proc, fp->binder);
2594 if (!node) {
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002595 node = binder_new_node(proc, fp);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002596 if (!node)
2597 return -ENOMEM;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002598 }
2599 if (fp->cookie != node->cookie) {
2600 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
2601 proc->pid, thread->pid, (u64)fp->binder,
2602 node->debug_id, (u64)fp->cookie,
2603 (u64)node->cookie);
Todd Kjosf22abc72017-05-09 11:08:05 -07002604 ret = -EINVAL;
2605 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002606 }
Todd Kjosf22abc72017-05-09 11:08:05 -07002607 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2608 ret = -EPERM;
2609 goto done;
2610 }
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002611
Todd Kjosb0117bb2017-05-08 09:16:27 -07002612 ret = binder_inc_ref_for_node(target_proc, node,
2613 fp->hdr.type == BINDER_TYPE_BINDER,
2614 &thread->todo, &rdata);
2615 if (ret)
Todd Kjosf22abc72017-05-09 11:08:05 -07002616 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002617
2618 if (fp->hdr.type == BINDER_TYPE_BINDER)
2619 fp->hdr.type = BINDER_TYPE_HANDLE;
2620 else
2621 fp->hdr.type = BINDER_TYPE_WEAK_HANDLE;
2622 fp->binder = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002623 fp->handle = rdata.desc;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002624 fp->cookie = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002625
Todd Kjosb0117bb2017-05-08 09:16:27 -07002626 trace_binder_transaction_node_to_ref(t, node, &rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002627 binder_debug(BINDER_DEBUG_TRANSACTION,
2628 " node %d u%016llx -> ref %d desc %d\n",
2629 node->debug_id, (u64)node->ptr,
Todd Kjosb0117bb2017-05-08 09:16:27 -07002630 rdata.debug_id, rdata.desc);
Todd Kjosf22abc72017-05-09 11:08:05 -07002631done:
2632 binder_put_node(node);
2633 return ret;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002634}
2635
2636static int binder_translate_handle(struct flat_binder_object *fp,
2637 struct binder_transaction *t,
2638 struct binder_thread *thread)
2639{
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002640 struct binder_proc *proc = thread->proc;
2641 struct binder_proc *target_proc = t->to_proc;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002642 struct binder_node *node;
2643 struct binder_ref_data src_rdata;
Todd Kjosf22abc72017-05-09 11:08:05 -07002644 int ret = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002645
Todd Kjosb0117bb2017-05-08 09:16:27 -07002646 node = binder_get_node_from_ref(proc, fp->handle,
2647 fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata);
2648 if (!node) {
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002649 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
2650 proc->pid, thread->pid, fp->handle);
2651 return -EINVAL;
2652 }
Todd Kjosf22abc72017-05-09 11:08:05 -07002653 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2654 ret = -EPERM;
2655 goto done;
2656 }
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002657
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002658 binder_node_lock(node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002659 if (node->proc == target_proc) {
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002660 if (fp->hdr.type == BINDER_TYPE_HANDLE)
2661 fp->hdr.type = BINDER_TYPE_BINDER;
2662 else
2663 fp->hdr.type = BINDER_TYPE_WEAK_BINDER;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002664 fp->binder = node->ptr;
2665 fp->cookie = node->cookie;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002666 if (node->proc)
2667 binder_inner_proc_lock(node->proc);
2668 binder_inc_node_nilocked(node,
2669 fp->hdr.type == BINDER_TYPE_BINDER,
2670 0, NULL);
2671 if (node->proc)
2672 binder_inner_proc_unlock(node->proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002673 trace_binder_transaction_ref_to_node(t, node, &src_rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002674 binder_debug(BINDER_DEBUG_TRANSACTION,
2675 " ref %d desc %d -> node %d u%016llx\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07002676 src_rdata.debug_id, src_rdata.desc, node->debug_id,
2677 (u64)node->ptr);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002678 binder_node_unlock(node);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002679 } else {
Todd Kjosb0117bb2017-05-08 09:16:27 -07002680 struct binder_ref_data dest_rdata;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002681
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002682 binder_node_unlock(node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002683 ret = binder_inc_ref_for_node(target_proc, node,
2684 fp->hdr.type == BINDER_TYPE_HANDLE,
2685 NULL, &dest_rdata);
2686 if (ret)
Todd Kjosf22abc72017-05-09 11:08:05 -07002687 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002688
2689 fp->binder = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002690 fp->handle = dest_rdata.desc;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002691 fp->cookie = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002692 trace_binder_transaction_ref_to_ref(t, node, &src_rdata,
2693 &dest_rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002694 binder_debug(BINDER_DEBUG_TRANSACTION,
2695 " ref %d desc %d -> ref %d desc %d (node %d)\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07002696 src_rdata.debug_id, src_rdata.desc,
2697 dest_rdata.debug_id, dest_rdata.desc,
2698 node->debug_id);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002699 }
Todd Kjosf22abc72017-05-09 11:08:05 -07002700done:
2701 binder_put_node(node);
2702 return ret;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002703}
2704
2705static int binder_translate_fd(int fd,
2706 struct binder_transaction *t,
2707 struct binder_thread *thread,
2708 struct binder_transaction *in_reply_to)
2709{
2710 struct binder_proc *proc = thread->proc;
2711 struct binder_proc *target_proc = t->to_proc;
2712 int target_fd;
2713 struct file *file;
2714 int ret;
2715 bool target_allows_fd;
2716
2717 if (in_reply_to)
2718 target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS);
2719 else
2720 target_allows_fd = t->buffer->target_node->accept_fds;
2721 if (!target_allows_fd) {
2722 binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
2723 proc->pid, thread->pid,
2724 in_reply_to ? "reply" : "transaction",
2725 fd);
2726 ret = -EPERM;
2727 goto err_fd_not_accepted;
2728 }
2729
2730 file = fget(fd);
2731 if (!file) {
2732 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
2733 proc->pid, thread->pid, fd);
2734 ret = -EBADF;
2735 goto err_fget;
2736 }
2737 ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file);
2738 if (ret < 0) {
2739 ret = -EPERM;
2740 goto err_security;
2741 }
2742
2743 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
2744 if (target_fd < 0) {
2745 ret = -ENOMEM;
2746 goto err_get_unused_fd;
2747 }
2748 task_fd_install(target_proc, target_fd, file);
2749 trace_binder_transaction_fd(t, fd, target_fd);
2750 binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n",
2751 fd, target_fd);
2752
2753 return target_fd;
2754
2755err_get_unused_fd:
2756err_security:
2757 fput(file);
2758err_fget:
2759err_fd_not_accepted:
2760 return ret;
2761}
2762
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002763static int binder_translate_fd_array(struct binder_fd_array_object *fda,
2764 struct binder_buffer_object *parent,
2765 struct binder_transaction *t,
2766 struct binder_thread *thread,
2767 struct binder_transaction *in_reply_to)
2768{
2769 binder_size_t fdi, fd_buf_size, num_installed_fds;
Todd Kjos8539b1e2019-02-08 10:35:20 -08002770 binder_size_t fda_offset;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002771 int target_fd;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002772 struct binder_proc *proc = thread->proc;
2773 struct binder_proc *target_proc = t->to_proc;
2774
2775 fd_buf_size = sizeof(u32) * fda->num_fds;
2776 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2777 binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
2778 proc->pid, thread->pid, (u64)fda->num_fds);
2779 return -EINVAL;
2780 }
2781 if (fd_buf_size > parent->length ||
2782 fda->parent_offset > parent->length - fd_buf_size) {
2783 /* No space for all file descriptors here. */
2784 binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
2785 proc->pid, thread->pid, (u64)fda->num_fds);
2786 return -EINVAL;
2787 }
Todd Kjos8539b1e2019-02-08 10:35:20 -08002788 /*
2789 * the source data for binder_buffer_object is visible
2790 * to user-space and the @buffer element is the user
2791 * pointer to the buffer_object containing the fd_array.
2792 * Convert the address to an offset relative to
2793 * the base of the transaction buffer.
2794 */
2795 fda_offset = (parent->buffer - (uintptr_t)t->buffer->user_data) +
2796 fda->parent_offset;
2797 if (!IS_ALIGNED((unsigned long)fda_offset, sizeof(u32))) {
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002798 binder_user_error("%d:%d parent offset not aligned correctly.\n",
2799 proc->pid, thread->pid);
2800 return -EINVAL;
2801 }
2802 for (fdi = 0; fdi < fda->num_fds; fdi++) {
Todd Kjos90a570c2019-02-08 10:35:15 -08002803 u32 fd;
Todd Kjos0b90a822019-03-27 16:12:31 -07002804
Todd Kjos8539b1e2019-02-08 10:35:20 -08002805 binder_size_t offset = fda_offset + fdi * sizeof(fd);
Todd Kjos90a570c2019-02-08 10:35:15 -08002806
2807 binder_alloc_copy_from_buffer(&target_proc->alloc,
2808 &fd, t->buffer,
2809 offset, sizeof(fd));
2810 target_fd = binder_translate_fd(fd, t, thread, in_reply_to);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002811 if (target_fd < 0)
2812 goto err_translate_fd_failed;
Todd Kjos90a570c2019-02-08 10:35:15 -08002813 binder_alloc_copy_to_buffer(&target_proc->alloc,
2814 t->buffer, offset,
2815 &target_fd, sizeof(fd));
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002816 }
2817 return 0;
2818
2819err_translate_fd_failed:
2820 /*
2821 * Failed to allocate fd or security error, free fds
2822 * installed so far.
2823 */
2824 num_installed_fds = fdi;
Todd Kjos90a570c2019-02-08 10:35:15 -08002825 for (fdi = 0; fdi < num_installed_fds; fdi++) {
2826 u32 fd;
Todd Kjos8539b1e2019-02-08 10:35:20 -08002827 binder_size_t offset = fda_offset + fdi * sizeof(fd);
Todd Kjos90a570c2019-02-08 10:35:15 -08002828 binder_alloc_copy_from_buffer(&target_proc->alloc,
2829 &fd, t->buffer,
2830 offset, sizeof(fd));
2831 task_close_fd(target_proc, fd);
2832 }
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002833 return target_fd;
2834}
2835
Martijn Coenen5a6da532016-09-30 14:10:07 +02002836static int binder_fixup_parent(struct binder_transaction *t,
2837 struct binder_thread *thread,
2838 struct binder_buffer_object *bp,
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002839 binder_size_t off_start_offset,
Martijn Coenen5a6da532016-09-30 14:10:07 +02002840 binder_size_t num_valid,
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002841 binder_size_t last_fixup_obj_off,
Martijn Coenen5a6da532016-09-30 14:10:07 +02002842 binder_size_t last_fixup_min_off)
2843{
2844 struct binder_buffer_object *parent;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002845 struct binder_buffer *b = t->buffer;
2846 struct binder_proc *proc = thread->proc;
2847 struct binder_proc *target_proc = t->to_proc;
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002848 struct binder_object object;
2849 binder_size_t buffer_offset;
2850 binder_size_t parent_offset;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002851
2852 if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT))
2853 return 0;
2854
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002855 parent = binder_validate_ptr(target_proc, b, &object, bp->parent,
2856 off_start_offset, &parent_offset,
2857 num_valid);
Martijn Coenen5a6da532016-09-30 14:10:07 +02002858 if (!parent) {
2859 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
2860 proc->pid, thread->pid);
2861 return -EINVAL;
2862 }
2863
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002864 if (!binder_validate_fixup(target_proc, b, off_start_offset,
2865 parent_offset, bp->parent_offset,
2866 last_fixup_obj_off,
Martijn Coenen5a6da532016-09-30 14:10:07 +02002867 last_fixup_min_off)) {
2868 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
2869 proc->pid, thread->pid);
2870 return -EINVAL;
2871 }
2872
2873 if (parent->length < sizeof(binder_uintptr_t) ||
2874 bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) {
2875 /* No space for a pointer here! */
2876 binder_user_error("%d:%d got transaction with invalid parent offset\n",
2877 proc->pid, thread->pid);
2878 return -EINVAL;
2879 }
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002880 buffer_offset = bp->parent_offset +
Todd Kjos8539b1e2019-02-08 10:35:20 -08002881 (uintptr_t)parent->buffer - (uintptr_t)b->user_data;
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002882 binder_alloc_copy_to_buffer(&target_proc->alloc, b, buffer_offset,
2883 &bp->buffer, sizeof(bp->buffer));
Martijn Coenen5a6da532016-09-30 14:10:07 +02002884
2885 return 0;
2886}
2887
Martijn Coenen053be422017-06-06 15:17:46 -07002888/**
2889 * binder_proc_transaction() - sends a transaction to a process and wakes it up
2890 * @t: transaction to send
2891 * @proc: process to send the transaction to
2892 * @thread: thread in @proc to send the transaction to (may be NULL)
2893 *
2894 * This function queues a transaction to the specified process. It will try
2895 * to find a thread in the target process to handle the transaction and
2896 * wake it up. If no thread is found, the work is queued to the proc
2897 * waitqueue.
2898 *
2899 * If the @thread parameter is not NULL, the transaction is always queued
2900 * to the waitlist of that specific thread.
2901 *
2902 * Return: true if the transactions was successfully queued
2903 * false if the target process or thread is dead
2904 */
2905static bool binder_proc_transaction(struct binder_transaction *t,
2906 struct binder_proc *proc,
2907 struct binder_thread *thread)
2908{
Martijn Coenen053be422017-06-06 15:17:46 -07002909 struct binder_node *node = t->buffer->target_node;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002910 struct binder_priority node_prio;
Martijn Coenen053be422017-06-06 15:17:46 -07002911 bool oneway = !!(t->flags & TF_ONE_WAY);
Martijn Coenen1af61802017-10-19 15:04:46 +02002912 bool pending_async = false;
Martijn Coenen053be422017-06-06 15:17:46 -07002913
2914 BUG_ON(!node);
2915 binder_node_lock(node);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002916 node_prio.prio = node->min_priority;
2917 node_prio.sched_policy = node->sched_policy;
2918
Martijn Coenen053be422017-06-06 15:17:46 -07002919 if (oneway) {
2920 BUG_ON(thread);
2921 if (node->has_async_transaction) {
Martijn Coenen1af61802017-10-19 15:04:46 +02002922 pending_async = true;
Martijn Coenen053be422017-06-06 15:17:46 -07002923 } else {
Gustavo A. R. Silvae62dd6f2018-01-23 12:04:27 -06002924 node->has_async_transaction = true;
Martijn Coenen053be422017-06-06 15:17:46 -07002925 }
2926 }
2927
2928 binder_inner_proc_lock(proc);
2929
2930 if (proc->is_dead || (thread && thread->is_dead)) {
2931 binder_inner_proc_unlock(proc);
2932 binder_node_unlock(node);
2933 return false;
2934 }
2935
Martijn Coenen1af61802017-10-19 15:04:46 +02002936 if (!thread && !pending_async)
Martijn Coenen053be422017-06-06 15:17:46 -07002937 thread = binder_select_thread_ilocked(proc);
2938
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002939 if (thread) {
Martijn Coenenc46810c2017-06-23 10:13:43 -07002940 binder_transaction_priority(thread->task, t, node_prio,
2941 node->inherit_rt);
Martijn Coenen1af61802017-10-19 15:04:46 +02002942 binder_enqueue_thread_work_ilocked(thread, &t->work);
2943 } else if (!pending_async) {
2944 binder_enqueue_work_ilocked(&t->work, &proc->todo);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002945 } else {
Martijn Coenen1af61802017-10-19 15:04:46 +02002946 binder_enqueue_work_ilocked(&t->work, &node->async_todo);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002947 }
Martijn Coenen053be422017-06-06 15:17:46 -07002948
Martijn Coenen1af61802017-10-19 15:04:46 +02002949 if (!pending_async)
Martijn Coenen053be422017-06-06 15:17:46 -07002950 binder_wakeup_thread_ilocked(proc, thread, !oneway /* sync */);
2951
2952 binder_inner_proc_unlock(proc);
2953 binder_node_unlock(node);
2954
2955 return true;
2956}
2957
Todd Kjos291d9682017-09-25 08:55:09 -07002958/**
2959 * binder_get_node_refs_for_txn() - Get required refs on node for txn
2960 * @node: struct binder_node for which to get refs
2961 * @proc: returns @node->proc if valid
2962 * @error: if no @proc then returns BR_DEAD_REPLY
2963 *
2964 * User-space normally keeps the node alive when creating a transaction
2965 * since it has a reference to the target. The local strong ref keeps it
2966 * alive if the sending process dies before the target process processes
2967 * the transaction. If the source process is malicious or has a reference
2968 * counting bug, relying on the local strong ref can fail.
2969 *
2970 * Since user-space can cause the local strong ref to go away, we also take
2971 * a tmpref on the node to ensure it survives while we are constructing
2972 * the transaction. We also need a tmpref on the proc while we are
2973 * constructing the transaction, so we take that here as well.
2974 *
2975 * Return: The target_node with refs taken or NULL if no @node->proc is NULL.
2976 * Also sets @proc if valid. If the @node->proc is NULL indicating that the
2977 * target proc has died, @error is set to BR_DEAD_REPLY
2978 */
2979static struct binder_node *binder_get_node_refs_for_txn(
2980 struct binder_node *node,
2981 struct binder_proc **procp,
2982 uint32_t *error)
2983{
2984 struct binder_node *target_node = NULL;
2985
2986 binder_node_inner_lock(node);
2987 if (node->proc) {
2988 target_node = node;
2989 binder_inc_node_nilocked(node, 1, 0, NULL);
2990 binder_inc_node_tmpref_ilocked(node);
Todd Kjosdc734d72019-06-10 09:14:25 -07002991 atomic_inc(&node->proc->tmp_ref);
Todd Kjos291d9682017-09-25 08:55:09 -07002992 *procp = node->proc;
2993 } else
2994 *error = BR_DEAD_REPLY;
2995 binder_node_inner_unlock(node);
2996
2997 return target_node;
2998}
2999
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003000static void binder_transaction(struct binder_proc *proc,
3001 struct binder_thread *thread,
Martijn Coenen59878d72016-09-30 14:05:40 +02003002 struct binder_transaction_data *tr, int reply,
3003 binder_size_t extra_buffers_size)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003004{
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003005 int ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003006 struct binder_transaction *t;
3007 struct binder_work *tcomplete;
Todd Kjos8539b1e2019-02-08 10:35:20 -08003008 binder_size_t buffer_offset = 0;
3009 binder_size_t off_start_offset, off_end_offset;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08003010 binder_size_t off_min;
Todd Kjos8539b1e2019-02-08 10:35:20 -08003011 binder_size_t sg_buf_offset, sg_buf_end_offset;
Todd Kjos2f993e22017-05-12 14:42:55 -07003012 struct binder_proc *target_proc = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003013 struct binder_thread *target_thread = NULL;
3014 struct binder_node *target_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003015 struct binder_transaction *in_reply_to = NULL;
3016 struct binder_transaction_log_entry *e;
Todd Kjose598d172017-03-22 17:19:52 -07003017 uint32_t return_error = 0;
3018 uint32_t return_error_param = 0;
3019 uint32_t return_error_line = 0;
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003020 binder_size_t last_fixup_obj_off = 0;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003021 binder_size_t last_fixup_min_off = 0;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003022 struct binder_context *context = proc->context;
Todd Kjos1cfe6272017-05-24 13:33:28 -07003023 int t_debug_id = atomic_inc_return(&binder_last_id);
Todd Kjos63e0afa2019-01-14 09:10:21 -08003024 char *secctx = NULL;
3025 u32 secctx_sz = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003026
3027 e = binder_transaction_log_add(&binder_transaction_log);
Todd Kjos1cfe6272017-05-24 13:33:28 -07003028 e->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003029 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
3030 e->from_proc = proc->pid;
3031 e->from_thread = thread->pid;
3032 e->target_handle = tr->target.handle;
3033 e->data_size = tr->data_size;
3034 e->offsets_size = tr->offsets_size;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02003035 e->context_name = proc->context->name;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003036
3037 if (reply) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07003038 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003039 in_reply_to = thread->transaction_stack;
3040 if (in_reply_to == NULL) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07003041 binder_inner_proc_unlock(proc);
Anmol Sarma56b468f2012-10-30 22:35:43 +05303042 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003043 proc->pid, thread->pid);
3044 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003045 return_error_param = -EPROTO;
3046 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003047 goto err_empty_call_stack;
3048 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003049 if (in_reply_to->to_thread != thread) {
Todd Kjos2f993e22017-05-12 14:42:55 -07003050 spin_lock(&in_reply_to->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05303051 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 +09003052 proc->pid, thread->pid, in_reply_to->debug_id,
3053 in_reply_to->to_proc ?
3054 in_reply_to->to_proc->pid : 0,
3055 in_reply_to->to_thread ?
3056 in_reply_to->to_thread->pid : 0);
Todd Kjos2f993e22017-05-12 14:42:55 -07003057 spin_unlock(&in_reply_to->lock);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003058 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003059 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003060 return_error_param = -EPROTO;
3061 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003062 in_reply_to = NULL;
3063 goto err_bad_call_stack;
3064 }
3065 thread->transaction_stack = in_reply_to->to_parent;
Martijn Coenen995a36e2017-06-02 13:36:52 -07003066 binder_inner_proc_unlock(proc);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003067 target_thread = binder_get_txn_from_and_acq_inner(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003068 if (target_thread == NULL) {
3069 return_error = BR_DEAD_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003070 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003071 goto err_dead_binder;
3072 }
3073 if (target_thread->transaction_stack != in_reply_to) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303074 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 +09003075 proc->pid, thread->pid,
3076 target_thread->transaction_stack ?
3077 target_thread->transaction_stack->debug_id : 0,
3078 in_reply_to->debug_id);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003079 binder_inner_proc_unlock(target_thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003080 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003081 return_error_param = -EPROTO;
3082 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003083 in_reply_to = NULL;
3084 target_thread = NULL;
3085 goto err_dead_binder;
3086 }
3087 target_proc = target_thread->proc;
Todd Kjosdc734d72019-06-10 09:14:25 -07003088 atomic_inc(&target_proc->tmp_ref);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003089 binder_inner_proc_unlock(target_thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003090 } else {
3091 if (tr->target.handle) {
3092 struct binder_ref *ref;
Seunghun Lee10f62862014-05-01 01:30:23 +09003093
Todd Kjosc37162d2017-05-26 11:56:29 -07003094 /*
3095 * There must already be a strong ref
3096 * on this node. If so, do a strong
3097 * increment on the node to ensure it
3098 * stays alive until the transaction is
3099 * done.
3100 */
Todd Kjos5346bf32016-10-20 16:43:34 -07003101 binder_proc_lock(proc);
3102 ref = binder_get_ref_olocked(proc, tr->target.handle,
3103 true);
Todd Kjosc37162d2017-05-26 11:56:29 -07003104 if (ref) {
Todd Kjos291d9682017-09-25 08:55:09 -07003105 target_node = binder_get_node_refs_for_txn(
3106 ref->node, &target_proc,
3107 &return_error);
3108 } else {
3109 binder_user_error("%d:%d got transaction to invalid handle\n",
3110 proc->pid, thread->pid);
3111 return_error = BR_FAILED_REPLY;
Todd Kjosc37162d2017-05-26 11:56:29 -07003112 }
Todd Kjos5346bf32016-10-20 16:43:34 -07003113 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003114 } else {
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003115 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003116 target_node = context->binder_context_mgr_node;
Todd Kjos291d9682017-09-25 08:55:09 -07003117 if (target_node)
3118 target_node = binder_get_node_refs_for_txn(
3119 target_node, &target_proc,
3120 &return_error);
3121 else
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003122 return_error = BR_DEAD_REPLY;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003123 mutex_unlock(&context->context_mgr_node_lock);
Martijn Coenenc4048b22018-03-28 11:14:50 +02003124 if (target_node && target_proc == proc) {
3125 binder_user_error("%d:%d got transaction to context manager from process owning it\n",
3126 proc->pid, thread->pid);
3127 return_error = BR_FAILED_REPLY;
3128 return_error_param = -EINVAL;
3129 return_error_line = __LINE__;
3130 goto err_invalid_target_handle;
3131 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003132 }
Todd Kjos291d9682017-09-25 08:55:09 -07003133 if (!target_node) {
3134 /*
3135 * return_error is set above
3136 */
3137 return_error_param = -EINVAL;
Todd Kjose598d172017-03-22 17:19:52 -07003138 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003139 goto err_dead_binder;
3140 }
Todd Kjos291d9682017-09-25 08:55:09 -07003141 e->to_node = target_node->debug_id;
Stephen Smalley79af7302015-01-21 10:54:10 -05003142 if (security_binder_transaction(proc->tsk,
3143 target_proc->tsk) < 0) {
3144 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003145 return_error_param = -EPERM;
3146 return_error_line = __LINE__;
Stephen Smalley79af7302015-01-21 10:54:10 -05003147 goto err_invalid_target_handle;
3148 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07003149 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003150 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
3151 struct binder_transaction *tmp;
Seunghun Lee10f62862014-05-01 01:30:23 +09003152
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003153 tmp = thread->transaction_stack;
3154 if (tmp->to_thread != thread) {
Todd Kjos2f993e22017-05-12 14:42:55 -07003155 spin_lock(&tmp->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05303156 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 +09003157 proc->pid, thread->pid, tmp->debug_id,
3158 tmp->to_proc ? tmp->to_proc->pid : 0,
3159 tmp->to_thread ?
3160 tmp->to_thread->pid : 0);
Todd Kjos2f993e22017-05-12 14:42:55 -07003161 spin_unlock(&tmp->lock);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003162 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003163 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003164 return_error_param = -EPROTO;
3165 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003166 goto err_bad_call_stack;
3167 }
3168 while (tmp) {
Todd Kjos2f993e22017-05-12 14:42:55 -07003169 struct binder_thread *from;
3170
3171 spin_lock(&tmp->lock);
3172 from = tmp->from;
3173 if (from && from->proc == target_proc) {
3174 atomic_inc(&from->tmp_ref);
3175 target_thread = from;
3176 spin_unlock(&tmp->lock);
3177 break;
3178 }
3179 spin_unlock(&tmp->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003180 tmp = tmp->from_parent;
3181 }
3182 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07003183 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003184 }
Martijn Coenen053be422017-06-06 15:17:46 -07003185 if (target_thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003186 e->to_thread = target_thread->pid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003187 e->to_proc = target_proc->pid;
3188
3189 /* TODO: reuse incoming transaction for reply */
3190 t = kzalloc(sizeof(*t), GFP_KERNEL);
3191 if (t == NULL) {
3192 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003193 return_error_param = -ENOMEM;
3194 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003195 goto err_alloc_t_failed;
3196 }
3197 binder_stats_created(BINDER_STAT_TRANSACTION);
Todd Kjos2f993e22017-05-12 14:42:55 -07003198 spin_lock_init(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003199
3200 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
3201 if (tcomplete == NULL) {
3202 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003203 return_error_param = -ENOMEM;
3204 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003205 goto err_alloc_tcomplete_failed;
3206 }
3207 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
3208
Todd Kjos1cfe6272017-05-24 13:33:28 -07003209 t->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003210
3211 if (reply)
3212 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen59878d72016-09-30 14:05:40 +02003213 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003214 proc->pid, thread->pid, t->debug_id,
3215 target_proc->pid, target_thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003216 (u64)tr->data.ptr.buffer,
3217 (u64)tr->data.ptr.offsets,
Martijn Coenen59878d72016-09-30 14:05:40 +02003218 (u64)tr->data_size, (u64)tr->offsets_size,
3219 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003220 else
3221 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen59878d72016-09-30 14:05:40 +02003222 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003223 proc->pid, thread->pid, t->debug_id,
3224 target_proc->pid, target_node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003225 (u64)tr->data.ptr.buffer,
3226 (u64)tr->data.ptr.offsets,
Martijn Coenen59878d72016-09-30 14:05:40 +02003227 (u64)tr->data_size, (u64)tr->offsets_size,
3228 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003229
3230 if (!reply && !(tr->flags & TF_ONE_WAY))
3231 t->from = thread;
3232 else
3233 t->from = NULL;
Tair Rzayev57bab7c2014-05-31 22:43:34 +03003234 t->sender_euid = task_euid(proc->tsk);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003235 t->to_proc = target_proc;
3236 t->to_thread = target_thread;
3237 t->code = tr->code;
3238 t->flags = tr->flags;
Martijn Coenen57b2ac62017-06-06 17:04:42 -07003239 if (!(t->flags & TF_ONE_WAY) &&
3240 binder_supported_policy(current->policy)) {
3241 /* Inherit supported policies for synchronous transactions */
3242 t->priority.sched_policy = current->policy;
3243 t->priority.prio = current->normal_prio;
3244 } else {
3245 /* Otherwise, fall back to the default priority */
3246 t->priority = target_proc->default_priority;
3247 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003248
Todd Kjos63e0afa2019-01-14 09:10:21 -08003249 if (target_node && target_node->txn_security_ctx) {
3250 u32 secid;
Todd Kjosf65c15f2019-04-24 12:31:18 -07003251 size_t added_size;
Todd Kjos63e0afa2019-01-14 09:10:21 -08003252
3253 security_task_getsecid(proc->tsk, &secid);
3254 ret = security_secid_to_secctx(secid, &secctx, &secctx_sz);
3255 if (ret) {
3256 return_error = BR_FAILED_REPLY;
3257 return_error_param = ret;
3258 return_error_line = __LINE__;
3259 goto err_get_secctx_failed;
3260 }
Todd Kjosf65c15f2019-04-24 12:31:18 -07003261 added_size = ALIGN(secctx_sz, sizeof(u64));
3262 extra_buffers_size += added_size;
3263 if (extra_buffers_size < added_size) {
3264 /* integer overflow of extra_buffers_size */
3265 return_error = BR_FAILED_REPLY;
3266 return_error_param = EINVAL;
3267 return_error_line = __LINE__;
3268 goto err_bad_extra_size;
3269 }
Todd Kjos63e0afa2019-01-14 09:10:21 -08003270 }
3271
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003272 trace_binder_transaction(reply, t, target_node);
3273
Todd Kjosd325d372016-10-10 10:40:53 -07003274 t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size,
Martijn Coenen59878d72016-09-30 14:05:40 +02003275 tr->offsets_size, extra_buffers_size,
3276 !reply && (t->flags & TF_ONE_WAY));
Todd Kjose598d172017-03-22 17:19:52 -07003277 if (IS_ERR(t->buffer)) {
3278 /*
3279 * -ESRCH indicates VMA cleared. The target is dying.
3280 */
3281 return_error_param = PTR_ERR(t->buffer);
3282 return_error = return_error_param == -ESRCH ?
3283 BR_DEAD_REPLY : BR_FAILED_REPLY;
3284 return_error_line = __LINE__;
3285 t->buffer = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003286 goto err_binder_alloc_buf_failed;
3287 }
Todd Kjos63e0afa2019-01-14 09:10:21 -08003288 if (secctx) {
3289 size_t buf_offset = ALIGN(tr->data_size, sizeof(void *)) +
3290 ALIGN(tr->offsets_size, sizeof(void *)) +
3291 ALIGN(extra_buffers_size, sizeof(void *)) -
3292 ALIGN(secctx_sz, sizeof(u64));
Todd Kjos63e0afa2019-01-14 09:10:21 -08003293
Todd Kjos8539b1e2019-02-08 10:35:20 -08003294 t->security_ctx = (uintptr_t)t->buffer->user_data + buf_offset;
Todd Kjos90a570c2019-02-08 10:35:15 -08003295 binder_alloc_copy_to_buffer(&target_proc->alloc,
3296 t->buffer, buf_offset,
3297 secctx, secctx_sz);
Todd Kjos63e0afa2019-01-14 09:10:21 -08003298 security_release_secctx(secctx, secctx_sz);
3299 secctx = NULL;
3300 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003301 t->buffer->debug_id = t->debug_id;
3302 t->buffer->transaction = t;
3303 t->buffer->target_node = target_node;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003304 trace_binder_transaction_alloc_buf(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003305
Todd Kjosd5049492019-02-08 10:35:14 -08003306 if (binder_alloc_copy_user_to_buffer(
3307 &target_proc->alloc,
3308 t->buffer, 0,
3309 (const void __user *)
3310 (uintptr_t)tr->data.ptr.buffer,
3311 tr->data_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303312 binder_user_error("%d:%d got transaction with invalid data ptr\n",
3313 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003314 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003315 return_error_param = -EFAULT;
3316 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003317 goto err_copy_data_failed;
3318 }
Todd Kjosd5049492019-02-08 10:35:14 -08003319 if (binder_alloc_copy_user_to_buffer(
3320 &target_proc->alloc,
3321 t->buffer,
3322 ALIGN(tr->data_size, sizeof(void *)),
3323 (const void __user *)
3324 (uintptr_t)tr->data.ptr.offsets,
3325 tr->offsets_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303326 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3327 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003328 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003329 return_error_param = -EFAULT;
3330 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003331 goto err_copy_data_failed;
3332 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08003333 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
3334 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
3335 proc->pid, thread->pid, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003336 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003337 return_error_param = -EINVAL;
3338 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003339 goto err_bad_offset;
3340 }
Martijn Coenen5a6da532016-09-30 14:10:07 +02003341 if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) {
3342 binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
3343 proc->pid, thread->pid,
Amit Pundir44cbb182017-02-01 12:53:45 +05303344 (u64)extra_buffers_size);
Martijn Coenen5a6da532016-09-30 14:10:07 +02003345 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003346 return_error_param = -EINVAL;
3347 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003348 goto err_bad_offset;
3349 }
Todd Kjos8539b1e2019-02-08 10:35:20 -08003350 off_start_offset = ALIGN(tr->data_size, sizeof(void *));
3351 buffer_offset = off_start_offset;
3352 off_end_offset = off_start_offset + tr->offsets_size;
3353 sg_buf_offset = ALIGN(off_end_offset, sizeof(void *));
Martijn Coenen76d4c942019-07-09 13:09:23 +02003354 sg_buf_end_offset = sg_buf_offset + extra_buffers_size -
3355 ALIGN(secctx_sz, sizeof(u64));
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08003356 off_min = 0;
Todd Kjos8539b1e2019-02-08 10:35:20 -08003357 for (buffer_offset = off_start_offset; buffer_offset < off_end_offset;
3358 buffer_offset += sizeof(binder_size_t)) {
Martijn Coenen00c80372016-07-13 12:06:49 +02003359 struct binder_object_header *hdr;
Todd Kjos90a570c2019-02-08 10:35:15 -08003360 size_t object_size;
Todd Kjosd73356a2019-02-08 10:35:16 -08003361 struct binder_object object;
Todd Kjos90a570c2019-02-08 10:35:15 -08003362 binder_size_t object_offset;
Seunghun Lee10f62862014-05-01 01:30:23 +09003363
Todd Kjos90a570c2019-02-08 10:35:15 -08003364 binder_alloc_copy_from_buffer(&target_proc->alloc,
3365 &object_offset,
3366 t->buffer,
3367 buffer_offset,
3368 sizeof(object_offset));
Todd Kjosd73356a2019-02-08 10:35:16 -08003369 object_size = binder_get_object(target_proc, t->buffer,
3370 object_offset, &object);
Todd Kjos90a570c2019-02-08 10:35:15 -08003371 if (object_size == 0 || object_offset < off_min) {
Martijn Coenen00c80372016-07-13 12:06:49 +02003372 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 -08003373 proc->pid, thread->pid,
3374 (u64)object_offset,
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08003375 (u64)off_min,
Martijn Coenen00c80372016-07-13 12:06:49 +02003376 (u64)t->buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003377 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003378 return_error_param = -EINVAL;
3379 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003380 goto err_bad_offset;
3381 }
Martijn Coenen00c80372016-07-13 12:06:49 +02003382
Todd Kjosd73356a2019-02-08 10:35:16 -08003383 hdr = &object.hdr;
Todd Kjos90a570c2019-02-08 10:35:15 -08003384 off_min = object_offset + object_size;
Martijn Coenen00c80372016-07-13 12:06:49 +02003385 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003386 case BINDER_TYPE_BINDER:
3387 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003388 struct flat_binder_object *fp;
Seunghun Lee10f62862014-05-01 01:30:23 +09003389
Martijn Coenen00c80372016-07-13 12:06:49 +02003390 fp = to_flat_binder_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003391 ret = binder_translate_binder(fp, t, thread);
3392 if (ret < 0) {
Christian Engelmayer7d420432014-05-07 21:44:53 +02003393 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003394 return_error_param = ret;
3395 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003396 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003397 }
Todd Kjosd73356a2019-02-08 10:35:16 -08003398 binder_alloc_copy_to_buffer(&target_proc->alloc,
3399 t->buffer, object_offset,
3400 fp, sizeof(*fp));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003401 } break;
3402 case BINDER_TYPE_HANDLE:
3403 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003404 struct flat_binder_object *fp;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02003405
Martijn Coenen00c80372016-07-13 12:06:49 +02003406 fp = to_flat_binder_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003407 ret = binder_translate_handle(fp, t, thread);
3408 if (ret < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003409 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003410 return_error_param = ret;
3411 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003412 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003413 }
Todd Kjosd73356a2019-02-08 10:35:16 -08003414 binder_alloc_copy_to_buffer(&target_proc->alloc,
3415 t->buffer, object_offset,
3416 fp, sizeof(*fp));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003417 } break;
3418
3419 case BINDER_TYPE_FD: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003420 struct binder_fd_object *fp = to_binder_fd_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003421 int target_fd = binder_translate_fd(fp->fd, t, thread,
3422 in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003423
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003424 if (target_fd < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003425 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003426 return_error_param = target_fd;
3427 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003428 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003429 }
Martijn Coenen00c80372016-07-13 12:06:49 +02003430 fp->pad_binder = 0;
3431 fp->fd = target_fd;
Todd Kjosd73356a2019-02-08 10:35:16 -08003432 binder_alloc_copy_to_buffer(&target_proc->alloc,
3433 t->buffer, object_offset,
3434 fp, sizeof(*fp));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003435 } break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003436 case BINDER_TYPE_FDA: {
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003437 struct binder_object ptr_object;
3438 binder_size_t parent_offset;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003439 struct binder_fd_array_object *fda =
3440 to_binder_fd_array_object(hdr);
Todd Kjos8539b1e2019-02-08 10:35:20 -08003441 size_t num_valid = (buffer_offset - off_start_offset) *
3442 sizeof(binder_size_t);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003443 struct binder_buffer_object *parent =
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003444 binder_validate_ptr(target_proc, t->buffer,
3445 &ptr_object, fda->parent,
3446 off_start_offset,
3447 &parent_offset,
Todd Kjos8539b1e2019-02-08 10:35:20 -08003448 num_valid);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003449 if (!parent) {
3450 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
3451 proc->pid, thread->pid);
3452 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003453 return_error_param = -EINVAL;
3454 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003455 goto err_bad_parent;
3456 }
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003457 if (!binder_validate_fixup(target_proc, t->buffer,
3458 off_start_offset,
3459 parent_offset,
3460 fda->parent_offset,
3461 last_fixup_obj_off,
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003462 last_fixup_min_off)) {
3463 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
3464 proc->pid, thread->pid);
3465 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003466 return_error_param = -EINVAL;
3467 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003468 goto err_bad_parent;
3469 }
3470 ret = binder_translate_fd_array(fda, parent, t, thread,
3471 in_reply_to);
3472 if (ret < 0) {
3473 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003474 return_error_param = ret;
3475 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003476 goto err_translate_failed;
3477 }
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003478 last_fixup_obj_off = parent_offset;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003479 last_fixup_min_off =
3480 fda->parent_offset + sizeof(u32) * fda->num_fds;
3481 } break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003482 case BINDER_TYPE_PTR: {
3483 struct binder_buffer_object *bp =
3484 to_binder_buffer_object(hdr);
Todd Kjos8539b1e2019-02-08 10:35:20 -08003485 size_t buf_left = sg_buf_end_offset - sg_buf_offset;
3486 size_t num_valid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003487
Martijn Coenen5a6da532016-09-30 14:10:07 +02003488 if (bp->length > buf_left) {
3489 binder_user_error("%d:%d got transaction with too large buffer\n",
3490 proc->pid, thread->pid);
3491 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003492 return_error_param = -EINVAL;
3493 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003494 goto err_bad_offset;
3495 }
Todd Kjosd5049492019-02-08 10:35:14 -08003496 if (binder_alloc_copy_user_to_buffer(
3497 &target_proc->alloc,
3498 t->buffer,
3499 sg_buf_offset,
3500 (const void __user *)
3501 (uintptr_t)bp->buffer,
3502 bp->length)) {
Martijn Coenen5a6da532016-09-30 14:10:07 +02003503 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3504 proc->pid, thread->pid);
Todd Kjose598d172017-03-22 17:19:52 -07003505 return_error_param = -EFAULT;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003506 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003507 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003508 goto err_copy_data_failed;
3509 }
3510 /* Fixup buffer pointer to target proc address space */
Todd Kjos8539b1e2019-02-08 10:35:20 -08003511 bp->buffer = (uintptr_t)
3512 t->buffer->user_data + sg_buf_offset;
3513 sg_buf_offset += ALIGN(bp->length, sizeof(u64));
Martijn Coenen5a6da532016-09-30 14:10:07 +02003514
Todd Kjos8539b1e2019-02-08 10:35:20 -08003515 num_valid = (buffer_offset - off_start_offset) *
3516 sizeof(binder_size_t);
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003517 ret = binder_fixup_parent(t, thread, bp,
3518 off_start_offset,
Todd Kjos8539b1e2019-02-08 10:35:20 -08003519 num_valid,
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003520 last_fixup_obj_off,
Martijn Coenen5a6da532016-09-30 14:10:07 +02003521 last_fixup_min_off);
3522 if (ret < 0) {
3523 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003524 return_error_param = ret;
3525 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003526 goto err_translate_failed;
3527 }
Todd Kjosd73356a2019-02-08 10:35:16 -08003528 binder_alloc_copy_to_buffer(&target_proc->alloc,
3529 t->buffer, object_offset,
3530 bp, sizeof(*bp));
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003531 last_fixup_obj_off = object_offset;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003532 last_fixup_min_off = 0;
3533 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003534 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01003535 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
Martijn Coenen00c80372016-07-13 12:06:49 +02003536 proc->pid, thread->pid, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003537 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003538 return_error_param = -EINVAL;
3539 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003540 goto err_bad_object_type;
3541 }
3542 }
Todd Kjos8dedb0c2017-05-09 08:31:32 -07003543 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003544 t->work.type = BINDER_WORK_TRANSACTION;
Todd Kjos8dedb0c2017-05-09 08:31:32 -07003545
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003546 if (reply) {
Martijn Coenen1af61802017-10-19 15:04:46 +02003547 binder_enqueue_thread_work(thread, tcomplete);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003548 binder_inner_proc_lock(target_proc);
3549 if (target_thread->is_dead) {
3550 binder_inner_proc_unlock(target_proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07003551 goto err_dead_proc_or_thread;
Martijn Coenen995a36e2017-06-02 13:36:52 -07003552 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003553 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003554 binder_pop_transaction_ilocked(target_thread, in_reply_to);
Martijn Coenen1af61802017-10-19 15:04:46 +02003555 binder_enqueue_thread_work_ilocked(target_thread, &t->work);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003556 binder_inner_proc_unlock(target_proc);
Martijn Coenen053be422017-06-06 15:17:46 -07003557 wake_up_interruptible_sync(&target_thread->wait);
Martijn Coenenecd972d2017-05-26 10:48:56 -07003558 binder_restore_priority(current, in_reply_to->saved_priority);
Todd Kjos21ef40a2017-03-30 18:02:13 -07003559 binder_free_transaction(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003560 } else if (!(t->flags & TF_ONE_WAY)) {
3561 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003562 binder_inner_proc_lock(proc);
Martijn Coenendac2e9c2017-11-13 09:55:21 +01003563 /*
3564 * Defer the TRANSACTION_COMPLETE, so we don't return to
3565 * userspace immediately; this allows the target process to
3566 * immediately start processing this transaction, reducing
3567 * latency. We will then return the TRANSACTION_COMPLETE when
3568 * the target replies (or there is an error).
3569 */
3570 binder_enqueue_deferred_thread_work_ilocked(thread, tcomplete);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003571 t->need_reply = 1;
3572 t->from_parent = thread->transaction_stack;
3573 thread->transaction_stack = t;
Martijn Coenen995a36e2017-06-02 13:36:52 -07003574 binder_inner_proc_unlock(proc);
Martijn Coenen053be422017-06-06 15:17:46 -07003575 if (!binder_proc_transaction(t, target_proc, target_thread)) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07003576 binder_inner_proc_lock(proc);
3577 binder_pop_transaction_ilocked(thread, t);
3578 binder_inner_proc_unlock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07003579 goto err_dead_proc_or_thread;
3580 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003581 } else {
3582 BUG_ON(target_node == NULL);
3583 BUG_ON(t->buffer->async_transaction != 1);
Martijn Coenen1af61802017-10-19 15:04:46 +02003584 binder_enqueue_thread_work(thread, tcomplete);
Martijn Coenen053be422017-06-06 15:17:46 -07003585 if (!binder_proc_transaction(t, target_proc, NULL))
Todd Kjos2f993e22017-05-12 14:42:55 -07003586 goto err_dead_proc_or_thread;
Riley Andrewsb5968812015-09-01 12:42:07 -07003587 }
Todd Kjos2f993e22017-05-12 14:42:55 -07003588 if (target_thread)
3589 binder_thread_dec_tmpref(target_thread);
3590 binder_proc_dec_tmpref(target_proc);
Todd Kjos291d9682017-09-25 08:55:09 -07003591 if (target_node)
3592 binder_dec_node_tmpref(target_node);
Todd Kjos1cfe6272017-05-24 13:33:28 -07003593 /*
3594 * write barrier to synchronize with initialization
3595 * of log entry
3596 */
3597 smp_wmb();
3598 WRITE_ONCE(e->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003599 return;
3600
Todd Kjos2f993e22017-05-12 14:42:55 -07003601err_dead_proc_or_thread:
3602 return_error = BR_DEAD_REPLY;
3603 return_error_line = __LINE__;
Xu YiPing86578a02017-05-22 11:26:23 -07003604 binder_dequeue_work(proc, tcomplete);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003605err_translate_failed:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003606err_bad_object_type:
3607err_bad_offset:
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003608err_bad_parent:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003609err_copy_data_failed:
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003610 trace_binder_transaction_failed_buffer_release(t->buffer);
Todd Kjos8539b1e2019-02-08 10:35:20 -08003611 binder_transaction_buffer_release(target_proc, t->buffer,
3612 buffer_offset, true);
Todd Kjos291d9682017-09-25 08:55:09 -07003613 if (target_node)
3614 binder_dec_node_tmpref(target_node);
Todd Kjosc37162d2017-05-26 11:56:29 -07003615 target_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003616 t->buffer->transaction = NULL;
Todd Kjosd325d372016-10-10 10:40:53 -07003617 binder_alloc_free_buf(&target_proc->alloc, t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003618err_binder_alloc_buf_failed:
Todd Kjosf65c15f2019-04-24 12:31:18 -07003619err_bad_extra_size:
Todd Kjos63e0afa2019-01-14 09:10:21 -08003620 if (secctx)
3621 security_release_secctx(secctx, secctx_sz);
3622err_get_secctx_failed:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003623 kfree(tcomplete);
3624 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3625err_alloc_tcomplete_failed:
3626 kfree(t);
3627 binder_stats_deleted(BINDER_STAT_TRANSACTION);
3628err_alloc_t_failed:
3629err_bad_call_stack:
3630err_empty_call_stack:
3631err_dead_binder:
3632err_invalid_target_handle:
Todd Kjos2f993e22017-05-12 14:42:55 -07003633 if (target_thread)
3634 binder_thread_dec_tmpref(target_thread);
3635 if (target_proc)
3636 binder_proc_dec_tmpref(target_proc);
Todd Kjos291d9682017-09-25 08:55:09 -07003637 if (target_node) {
Todd Kjosc37162d2017-05-26 11:56:29 -07003638 binder_dec_node(target_node, 1, 0);
Todd Kjos291d9682017-09-25 08:55:09 -07003639 binder_dec_node_tmpref(target_node);
3640 }
Todd Kjosc37162d2017-05-26 11:56:29 -07003641
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003642 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Todd Kjose598d172017-03-22 17:19:52 -07003643 "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
3644 proc->pid, thread->pid, return_error, return_error_param,
3645 (u64)tr->data_size, (u64)tr->offsets_size,
3646 return_error_line);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003647
3648 {
3649 struct binder_transaction_log_entry *fe;
Seunghun Lee10f62862014-05-01 01:30:23 +09003650
Todd Kjose598d172017-03-22 17:19:52 -07003651 e->return_error = return_error;
3652 e->return_error_param = return_error_param;
3653 e->return_error_line = return_error_line;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003654 fe = binder_transaction_log_add(&binder_transaction_log_failed);
3655 *fe = *e;
Todd Kjos1cfe6272017-05-24 13:33:28 -07003656 /*
3657 * write barrier to synchronize with initialization
3658 * of log entry
3659 */
3660 smp_wmb();
3661 WRITE_ONCE(e->debug_id_done, t_debug_id);
3662 WRITE_ONCE(fe->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003663 }
3664
Todd Kjos858b8da2017-04-21 17:35:12 -07003665 BUG_ON(thread->return_error.cmd != BR_OK);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003666 if (in_reply_to) {
Martijn Coenenecd972d2017-05-26 10:48:56 -07003667 binder_restore_priority(current, in_reply_to->saved_priority);
Todd Kjos858b8da2017-04-21 17:35:12 -07003668 thread->return_error.cmd = BR_TRANSACTION_COMPLETE;
Martijn Coenen1af61802017-10-19 15:04:46 +02003669 binder_enqueue_thread_work(thread, &thread->return_error.work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003670 binder_send_failed_reply(in_reply_to, return_error);
Todd Kjos858b8da2017-04-21 17:35:12 -07003671 } else {
3672 thread->return_error.cmd = return_error;
Martijn Coenen1af61802017-10-19 15:04:46 +02003673 binder_enqueue_thread_work(thread, &thread->return_error.work);
Todd Kjos858b8da2017-04-21 17:35:12 -07003674 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003675}
3676
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02003677static int binder_thread_write(struct binder_proc *proc,
3678 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003679 binder_uintptr_t binder_buffer, size_t size,
3680 binder_size_t *consumed)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003681{
3682 uint32_t cmd;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003683 struct binder_context *context = proc->context;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003684 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003685 void __user *ptr = buffer + *consumed;
3686 void __user *end = buffer + size;
3687
Todd Kjos858b8da2017-04-21 17:35:12 -07003688 while (ptr < end && thread->return_error.cmd == BR_OK) {
Todd Kjosb0117bb2017-05-08 09:16:27 -07003689 int ret;
3690
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003691 if (get_user(cmd, (uint32_t __user *)ptr))
3692 return -EFAULT;
3693 ptr += sizeof(uint32_t);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003694 trace_binder_command(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003695 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07003696 atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]);
3697 atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]);
3698 atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003699 }
3700 switch (cmd) {
3701 case BC_INCREFS:
3702 case BC_ACQUIRE:
3703 case BC_RELEASE:
3704 case BC_DECREFS: {
3705 uint32_t target;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003706 const char *debug_string;
Todd Kjosb0117bb2017-05-08 09:16:27 -07003707 bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE;
3708 bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE;
3709 struct binder_ref_data rdata;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003710
3711 if (get_user(target, (uint32_t __user *)ptr))
3712 return -EFAULT;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003713
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003714 ptr += sizeof(uint32_t);
Todd Kjosb0117bb2017-05-08 09:16:27 -07003715 ret = -1;
3716 if (increment && !target) {
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003717 struct binder_node *ctx_mgr_node;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003718 mutex_lock(&context->context_mgr_node_lock);
3719 ctx_mgr_node = context->binder_context_mgr_node;
Todd Kjosb0117bb2017-05-08 09:16:27 -07003720 if (ctx_mgr_node)
3721 ret = binder_inc_ref_for_node(
3722 proc, ctx_mgr_node,
3723 strong, NULL, &rdata);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003724 mutex_unlock(&context->context_mgr_node_lock);
3725 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07003726 if (ret)
3727 ret = binder_update_ref_for_handle(
3728 proc, target, increment, strong,
3729 &rdata);
3730 if (!ret && rdata.desc != target) {
3731 binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
3732 proc->pid, thread->pid,
3733 target, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003734 }
3735 switch (cmd) {
3736 case BC_INCREFS:
3737 debug_string = "IncRefs";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003738 break;
3739 case BC_ACQUIRE:
3740 debug_string = "Acquire";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003741 break;
3742 case BC_RELEASE:
3743 debug_string = "Release";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003744 break;
3745 case BC_DECREFS:
3746 default:
3747 debug_string = "DecRefs";
Todd Kjosb0117bb2017-05-08 09:16:27 -07003748 break;
3749 }
3750 if (ret) {
3751 binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
3752 proc->pid, thread->pid, debug_string,
3753 strong, target, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003754 break;
3755 }
3756 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjosb0117bb2017-05-08 09:16:27 -07003757 "%d:%d %s ref %d desc %d s %d w %d\n",
3758 proc->pid, thread->pid, debug_string,
3759 rdata.debug_id, rdata.desc, rdata.strong,
3760 rdata.weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003761 break;
3762 }
3763 case BC_INCREFS_DONE:
3764 case BC_ACQUIRE_DONE: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003765 binder_uintptr_t node_ptr;
3766 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003767 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003768 bool free_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003769
Arve Hjønnevågda498892014-02-21 14:40:26 -08003770 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003771 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003772 ptr += sizeof(binder_uintptr_t);
3773 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003774 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003775 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003776 node = binder_get_node(proc, node_ptr);
3777 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003778 binder_user_error("%d:%d %s u%016llx no match\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003779 proc->pid, thread->pid,
3780 cmd == BC_INCREFS_DONE ?
3781 "BC_INCREFS_DONE" :
3782 "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003783 (u64)node_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003784 break;
3785 }
3786 if (cookie != node->cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003787 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003788 proc->pid, thread->pid,
3789 cmd == BC_INCREFS_DONE ?
3790 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003791 (u64)node_ptr, node->debug_id,
3792 (u64)cookie, (u64)node->cookie);
Todd Kjosf22abc72017-05-09 11:08:05 -07003793 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003794 break;
3795 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003796 binder_node_inner_lock(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003797 if (cmd == BC_ACQUIRE_DONE) {
3798 if (node->pending_strong_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303799 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003800 proc->pid, thread->pid,
3801 node->debug_id);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003802 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003803 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003804 break;
3805 }
3806 node->pending_strong_ref = 0;
3807 } else {
3808 if (node->pending_weak_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303809 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003810 proc->pid, thread->pid,
3811 node->debug_id);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003812 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003813 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003814 break;
3815 }
3816 node->pending_weak_ref = 0;
3817 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003818 free_node = binder_dec_node_nilocked(node,
3819 cmd == BC_ACQUIRE_DONE, 0);
3820 WARN_ON(free_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003821 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjosf22abc72017-05-09 11:08:05 -07003822 "%d:%d %s node %d ls %d lw %d tr %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003823 proc->pid, thread->pid,
3824 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Todd Kjosf22abc72017-05-09 11:08:05 -07003825 node->debug_id, node->local_strong_refs,
3826 node->local_weak_refs, node->tmp_refs);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003827 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003828 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003829 break;
3830 }
3831 case BC_ATTEMPT_ACQUIRE:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303832 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003833 return -EINVAL;
3834 case BC_ACQUIRE_RESULT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303835 pr_err("BC_ACQUIRE_RESULT not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003836 return -EINVAL;
3837
3838 case BC_FREE_BUFFER: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003839 binder_uintptr_t data_ptr;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003840 struct binder_buffer *buffer;
3841
Arve Hjønnevågda498892014-02-21 14:40:26 -08003842 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003843 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003844 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003845
Todd Kjos076072a2017-04-21 14:32:11 -07003846 buffer = binder_alloc_prepare_to_free(&proc->alloc,
3847 data_ptr);
Todd Kjosd29b73e2018-11-06 15:55:32 -08003848 if (IS_ERR_OR_NULL(buffer)) {
3849 if (PTR_ERR(buffer) == -EPERM) {
3850 binder_user_error(
3851 "%d:%d BC_FREE_BUFFER u%016llx matched unreturned or currently freeing buffer\n",
3852 proc->pid, thread->pid,
3853 (u64)data_ptr);
3854 } else {
3855 binder_user_error(
3856 "%d:%d BC_FREE_BUFFER u%016llx no match\n",
3857 proc->pid, thread->pid,
3858 (u64)data_ptr);
3859 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003860 break;
3861 }
3862 binder_debug(BINDER_DEBUG_FREE_BUFFER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003863 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
3864 proc->pid, thread->pid, (u64)data_ptr,
3865 buffer->debug_id,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003866 buffer->transaction ? "active" : "finished");
3867
Todd Kjosdc734d72019-06-10 09:14:25 -07003868 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003869 if (buffer->transaction) {
3870 buffer->transaction->buffer = NULL;
3871 buffer->transaction = NULL;
3872 }
Todd Kjosdc734d72019-06-10 09:14:25 -07003873 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003874 if (buffer->async_transaction && buffer->target_node) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003875 struct binder_node *buf_node;
3876 struct binder_work *w;
3877
3878 buf_node = buffer->target_node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003879 binder_node_inner_lock(buf_node);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003880 BUG_ON(!buf_node->has_async_transaction);
3881 BUG_ON(buf_node->proc != proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003882 w = binder_dequeue_work_head_ilocked(
3883 &buf_node->async_todo);
Martijn Coenen4501c042017-08-10 13:56:16 +02003884 if (!w) {
Gustavo A. R. Silvae62dd6f2018-01-23 12:04:27 -06003885 buf_node->has_async_transaction = false;
Martijn Coenen4501c042017-08-10 13:56:16 +02003886 } else {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003887 binder_enqueue_work_ilocked(
Martijn Coenen4501c042017-08-10 13:56:16 +02003888 w, &proc->todo);
3889 binder_wakeup_proc_ilocked(proc);
3890 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003891 binder_node_inner_unlock(buf_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003892 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003893 trace_binder_transaction_buffer_release(buffer);
Todd Kjos8539b1e2019-02-08 10:35:20 -08003894 binder_transaction_buffer_release(proc, buffer, 0, false);
Todd Kjosd325d372016-10-10 10:40:53 -07003895 binder_alloc_free_buf(&proc->alloc, buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003896 break;
3897 }
3898
Martijn Coenen5a6da532016-09-30 14:10:07 +02003899 case BC_TRANSACTION_SG:
3900 case BC_REPLY_SG: {
3901 struct binder_transaction_data_sg tr;
3902
3903 if (copy_from_user(&tr, ptr, sizeof(tr)))
3904 return -EFAULT;
3905 ptr += sizeof(tr);
3906 binder_transaction(proc, thread, &tr.transaction_data,
3907 cmd == BC_REPLY_SG, tr.buffers_size);
3908 break;
3909 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003910 case BC_TRANSACTION:
3911 case BC_REPLY: {
3912 struct binder_transaction_data tr;
3913
3914 if (copy_from_user(&tr, ptr, sizeof(tr)))
3915 return -EFAULT;
3916 ptr += sizeof(tr);
Martijn Coenen59878d72016-09-30 14:05:40 +02003917 binder_transaction(proc, thread, &tr,
3918 cmd == BC_REPLY, 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003919 break;
3920 }
3921
3922 case BC_REGISTER_LOOPER:
3923 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303924 "%d:%d BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003925 proc->pid, thread->pid);
Todd Kjosd600e902017-05-25 17:35:02 -07003926 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003927 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
3928 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303929 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003930 proc->pid, thread->pid);
3931 } else if (proc->requested_threads == 0) {
3932 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303933 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003934 proc->pid, thread->pid);
3935 } else {
3936 proc->requested_threads--;
3937 proc->requested_threads_started++;
3938 }
3939 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
Todd Kjosd600e902017-05-25 17:35:02 -07003940 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003941 break;
3942 case BC_ENTER_LOOPER:
3943 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303944 "%d:%d BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003945 proc->pid, thread->pid);
3946 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
3947 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303948 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003949 proc->pid, thread->pid);
3950 }
3951 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
3952 break;
3953 case BC_EXIT_LOOPER:
3954 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303955 "%d:%d BC_EXIT_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003956 proc->pid, thread->pid);
3957 thread->looper |= BINDER_LOOPER_STATE_EXITED;
3958 break;
3959
3960 case BC_REQUEST_DEATH_NOTIFICATION:
3961 case BC_CLEAR_DEATH_NOTIFICATION: {
3962 uint32_t target;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003963 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003964 struct binder_ref *ref;
Todd Kjos5346bf32016-10-20 16:43:34 -07003965 struct binder_ref_death *death = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003966
3967 if (get_user(target, (uint32_t __user *)ptr))
3968 return -EFAULT;
3969 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08003970 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003971 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003972 ptr += sizeof(binder_uintptr_t);
Todd Kjos5346bf32016-10-20 16:43:34 -07003973 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3974 /*
3975 * Allocate memory for death notification
3976 * before taking lock
3977 */
3978 death = kzalloc(sizeof(*death), GFP_KERNEL);
3979 if (death == NULL) {
3980 WARN_ON(thread->return_error.cmd !=
3981 BR_OK);
3982 thread->return_error.cmd = BR_ERROR;
Martijn Coenen1af61802017-10-19 15:04:46 +02003983 binder_enqueue_thread_work(
3984 thread,
3985 &thread->return_error.work);
Todd Kjos5346bf32016-10-20 16:43:34 -07003986 binder_debug(
3987 BINDER_DEBUG_FAILED_TRANSACTION,
3988 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
3989 proc->pid, thread->pid);
3990 break;
3991 }
3992 }
3993 binder_proc_lock(proc);
3994 ref = binder_get_ref_olocked(proc, target, false);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003995 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303996 binder_user_error("%d:%d %s invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003997 proc->pid, thread->pid,
3998 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3999 "BC_REQUEST_DEATH_NOTIFICATION" :
4000 "BC_CLEAR_DEATH_NOTIFICATION",
4001 target);
Todd Kjos5346bf32016-10-20 16:43:34 -07004002 binder_proc_unlock(proc);
4003 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004004 break;
4005 }
4006
4007 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004008 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004009 proc->pid, thread->pid,
4010 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
4011 "BC_REQUEST_DEATH_NOTIFICATION" :
4012 "BC_CLEAR_DEATH_NOTIFICATION",
Todd Kjosb0117bb2017-05-08 09:16:27 -07004013 (u64)cookie, ref->data.debug_id,
4014 ref->data.desc, ref->data.strong,
4015 ref->data.weak, ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004016
Martijn Coenenf9eac642017-05-22 11:26:23 -07004017 binder_node_lock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004018 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
4019 if (ref->death) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05304020 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004021 proc->pid, thread->pid);
Martijn Coenenf9eac642017-05-22 11:26:23 -07004022 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07004023 binder_proc_unlock(proc);
4024 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004025 break;
4026 }
4027 binder_stats_created(BINDER_STAT_DEATH);
4028 INIT_LIST_HEAD(&death->work.entry);
4029 death->cookie = cookie;
4030 ref->death = death;
4031 if (ref->node->proc == NULL) {
4032 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
Martijn Coenen3bdbe4c2017-08-10 13:50:52 +02004033
4034 binder_inner_proc_lock(proc);
4035 binder_enqueue_work_ilocked(
4036 &ref->death->work, &proc->todo);
4037 binder_wakeup_proc_ilocked(proc);
4038 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004039 }
4040 } else {
4041 if (ref->death == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05304042 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004043 proc->pid, thread->pid);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004044 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07004045 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004046 break;
4047 }
4048 death = ref->death;
4049 if (death->cookie != cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08004050 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004051 proc->pid, thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004052 (u64)death->cookie,
4053 (u64)cookie);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004054 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07004055 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004056 break;
4057 }
4058 ref->death = NULL;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004059 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004060 if (list_empty(&death->work.entry)) {
4061 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004062 if (thread->looper &
4063 (BINDER_LOOPER_STATE_REGISTERED |
4064 BINDER_LOOPER_STATE_ENTERED))
Martijn Coenen1af61802017-10-19 15:04:46 +02004065 binder_enqueue_thread_work_ilocked(
4066 thread,
4067 &death->work);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004068 else {
4069 binder_enqueue_work_ilocked(
4070 &death->work,
4071 &proc->todo);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004072 binder_wakeup_proc_ilocked(
Martijn Coenen053be422017-06-06 15:17:46 -07004073 proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004074 }
4075 } else {
4076 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
4077 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
4078 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004079 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004080 }
Martijn Coenenf9eac642017-05-22 11:26:23 -07004081 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07004082 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004083 } break;
4084 case BC_DEAD_BINDER_DONE: {
4085 struct binder_work *w;
Arve Hjønnevågda498892014-02-21 14:40:26 -08004086 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004087 struct binder_ref_death *death = NULL;
Seunghun Lee10f62862014-05-01 01:30:23 +09004088
Arve Hjønnevågda498892014-02-21 14:40:26 -08004089 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004090 return -EFAULT;
4091
Lisa Du7a64cd82016-02-17 09:32:52 +08004092 ptr += sizeof(cookie);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004093 binder_inner_proc_lock(proc);
4094 list_for_each_entry(w, &proc->delivered_death,
4095 entry) {
4096 struct binder_ref_death *tmp_death =
4097 container_of(w,
4098 struct binder_ref_death,
4099 work);
Seunghun Lee10f62862014-05-01 01:30:23 +09004100
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004101 if (tmp_death->cookie == cookie) {
4102 death = tmp_death;
4103 break;
4104 }
4105 }
4106 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Todd Kjosf540ce02018-02-07 13:57:37 -08004107 "%d:%d BC_DEAD_BINDER_DONE %016llx found %pK\n",
Arve Hjønnevågda498892014-02-21 14:40:26 -08004108 proc->pid, thread->pid, (u64)cookie,
4109 death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004110 if (death == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08004111 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
4112 proc->pid, thread->pid, (u64)cookie);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004113 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004114 break;
4115 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004116 binder_dequeue_work_ilocked(&death->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004117 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
4118 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004119 if (thread->looper &
4120 (BINDER_LOOPER_STATE_REGISTERED |
4121 BINDER_LOOPER_STATE_ENTERED))
Martijn Coenen1af61802017-10-19 15:04:46 +02004122 binder_enqueue_thread_work_ilocked(
4123 thread, &death->work);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004124 else {
4125 binder_enqueue_work_ilocked(
4126 &death->work,
4127 &proc->todo);
Martijn Coenen053be422017-06-06 15:17:46 -07004128 binder_wakeup_proc_ilocked(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004129 }
4130 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004131 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004132 } break;
4133
4134 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304135 pr_err("%d:%d unknown command %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004136 proc->pid, thread->pid, cmd);
4137 return -EINVAL;
4138 }
4139 *consumed = ptr - buffer;
4140 }
4141 return 0;
4142}
4143
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02004144static void binder_stat_br(struct binder_proc *proc,
4145 struct binder_thread *thread, uint32_t cmd)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004146{
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004147 trace_binder_return(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004148 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07004149 atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
4150 atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
4151 atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004152 }
4153}
4154
Todd Kjos60792612017-05-24 10:51:01 -07004155static int binder_put_node_cmd(struct binder_proc *proc,
4156 struct binder_thread *thread,
4157 void __user **ptrp,
4158 binder_uintptr_t node_ptr,
4159 binder_uintptr_t node_cookie,
4160 int node_debug_id,
4161 uint32_t cmd, const char *cmd_name)
4162{
4163 void __user *ptr = *ptrp;
4164
4165 if (put_user(cmd, (uint32_t __user *)ptr))
4166 return -EFAULT;
4167 ptr += sizeof(uint32_t);
4168
4169 if (put_user(node_ptr, (binder_uintptr_t __user *)ptr))
4170 return -EFAULT;
4171 ptr += sizeof(binder_uintptr_t);
4172
4173 if (put_user(node_cookie, (binder_uintptr_t __user *)ptr))
4174 return -EFAULT;
4175 ptr += sizeof(binder_uintptr_t);
4176
4177 binder_stat_br(proc, thread, cmd);
4178 binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n",
4179 proc->pid, thread->pid, cmd_name, node_debug_id,
4180 (u64)node_ptr, (u64)node_cookie);
4181
4182 *ptrp = ptr;
4183 return 0;
4184}
4185
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004186static int binder_wait_for_work(struct binder_thread *thread,
4187 bool do_proc_work)
4188{
4189 DEFINE_WAIT(wait);
4190 struct binder_proc *proc = thread->proc;
4191 int ret = 0;
4192
4193 freezer_do_not_count();
4194 binder_inner_proc_lock(proc);
4195 for (;;) {
4196 prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE);
4197 if (binder_has_work_ilocked(thread, do_proc_work))
4198 break;
4199 if (do_proc_work)
4200 list_add(&thread->waiting_thread_node,
4201 &proc->waiting_threads);
4202 binder_inner_proc_unlock(proc);
4203 schedule();
4204 binder_inner_proc_lock(proc);
4205 list_del_init(&thread->waiting_thread_node);
4206 if (signal_pending(current)) {
4207 ret = -ERESTARTSYS;
4208 break;
4209 }
4210 }
4211 finish_wait(&thread->wait, &wait);
4212 binder_inner_proc_unlock(proc);
4213 freezer_count();
4214
4215 return ret;
4216}
4217
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004218static int binder_thread_read(struct binder_proc *proc,
4219 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004220 binder_uintptr_t binder_buffer, size_t size,
4221 binder_size_t *consumed, int non_block)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004222{
Arve Hjønnevågda498892014-02-21 14:40:26 -08004223 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004224 void __user *ptr = buffer + *consumed;
4225 void __user *end = buffer + size;
4226
4227 int ret = 0;
4228 int wait_for_proc_work;
4229
4230 if (*consumed == 0) {
4231 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
4232 return -EFAULT;
4233 ptr += sizeof(uint32_t);
4234 }
4235
4236retry:
Martijn Coenen995a36e2017-06-02 13:36:52 -07004237 binder_inner_proc_lock(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004238 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
Martijn Coenen995a36e2017-06-02 13:36:52 -07004239 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004240
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004241 thread->looper |= BINDER_LOOPER_STATE_WAITING;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004242
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004243 trace_binder_wait_for_work(wait_for_proc_work,
4244 !!thread->transaction_stack,
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004245 !binder_worklist_empty(proc, &thread->todo));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004246 if (wait_for_proc_work) {
4247 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4248 BINDER_LOOPER_STATE_ENTERED))) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05304249 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 +09004250 proc->pid, thread->pid, thread->looper);
4251 wait_event_interruptible(binder_user_error_wait,
4252 binder_stop_on_user_error < 2);
4253 }
Martijn Coenenecd972d2017-05-26 10:48:56 -07004254 binder_restore_priority(current, proc->default_priority);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004255 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004256
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004257 if (non_block) {
4258 if (!binder_has_work(thread, wait_for_proc_work))
4259 ret = -EAGAIN;
4260 } else {
4261 ret = binder_wait_for_work(thread, wait_for_proc_work);
4262 }
4263
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004264 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
4265
4266 if (ret)
4267 return ret;
4268
4269 while (1) {
4270 uint32_t cmd;
Todd Kjos63e0afa2019-01-14 09:10:21 -08004271 struct binder_transaction_data_secctx tr;
4272 struct binder_transaction_data *trd = &tr.transaction_data;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004273 struct binder_work *w = NULL;
4274 struct list_head *list = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004275 struct binder_transaction *t = NULL;
Todd Kjos2f993e22017-05-12 14:42:55 -07004276 struct binder_thread *t_from;
Todd Kjos63e0afa2019-01-14 09:10:21 -08004277 size_t trsize = sizeof(*trd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004278
Todd Kjose7f23ed2017-03-21 13:06:01 -07004279 binder_inner_proc_lock(proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004280 if (!binder_worklist_empty_ilocked(&thread->todo))
4281 list = &thread->todo;
4282 else if (!binder_worklist_empty_ilocked(&proc->todo) &&
4283 wait_for_proc_work)
4284 list = &proc->todo;
4285 else {
4286 binder_inner_proc_unlock(proc);
4287
Dmitry Voytik395262a2014-09-08 18:16:34 +04004288 /* no data added */
Todd Kjos6798e6d2017-01-06 14:19:25 -08004289 if (ptr - buffer == 4 && !thread->looper_need_return)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004290 goto retry;
4291 break;
4292 }
4293
Todd Kjose7f23ed2017-03-21 13:06:01 -07004294 if (end - ptr < sizeof(tr) + 4) {
4295 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004296 break;
Todd Kjose7f23ed2017-03-21 13:06:01 -07004297 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004298 w = binder_dequeue_work_head_ilocked(list);
Martijn Coenen1af61802017-10-19 15:04:46 +02004299 if (binder_worklist_empty_ilocked(&thread->todo))
4300 thread->process_todo = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004301
4302 switch (w->type) {
4303 case BINDER_WORK_TRANSACTION: {
Todd Kjose7f23ed2017-03-21 13:06:01 -07004304 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004305 t = container_of(w, struct binder_transaction, work);
4306 } break;
Todd Kjos858b8da2017-04-21 17:35:12 -07004307 case BINDER_WORK_RETURN_ERROR: {
4308 struct binder_error *e = container_of(
4309 w, struct binder_error, work);
4310
4311 WARN_ON(e->cmd == BR_OK);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004312 binder_inner_proc_unlock(proc);
Todd Kjos858b8da2017-04-21 17:35:12 -07004313 if (put_user(e->cmd, (uint32_t __user *)ptr))
4314 return -EFAULT;
宋金时e1b1a8b2018-05-10 02:05:03 +00004315 cmd = e->cmd;
Todd Kjos858b8da2017-04-21 17:35:12 -07004316 e->cmd = BR_OK;
4317 ptr += sizeof(uint32_t);
4318
4319 binder_stat_br(proc, thread, cmd);
Todd Kjos858b8da2017-04-21 17:35:12 -07004320 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004321 case BINDER_WORK_TRANSACTION_COMPLETE: {
Todd Kjose7f23ed2017-03-21 13:06:01 -07004322 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004323 cmd = BR_TRANSACTION_COMPLETE;
4324 if (put_user(cmd, (uint32_t __user *)ptr))
4325 return -EFAULT;
4326 ptr += sizeof(uint32_t);
4327
4328 binder_stat_br(proc, thread, cmd);
4329 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304330 "%d:%d BR_TRANSACTION_COMPLETE\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004331 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004332 kfree(w);
4333 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4334 } break;
4335 case BINDER_WORK_NODE: {
4336 struct binder_node *node = container_of(w, struct binder_node, work);
Todd Kjos60792612017-05-24 10:51:01 -07004337 int strong, weak;
4338 binder_uintptr_t node_ptr = node->ptr;
4339 binder_uintptr_t node_cookie = node->cookie;
4340 int node_debug_id = node->debug_id;
4341 int has_weak_ref;
4342 int has_strong_ref;
4343 void __user *orig_ptr = ptr;
Seunghun Lee10f62862014-05-01 01:30:23 +09004344
Todd Kjos60792612017-05-24 10:51:01 -07004345 BUG_ON(proc != node->proc);
4346 strong = node->internal_strong_refs ||
4347 node->local_strong_refs;
4348 weak = !hlist_empty(&node->refs) ||
Todd Kjosf22abc72017-05-09 11:08:05 -07004349 node->local_weak_refs ||
4350 node->tmp_refs || strong;
Todd Kjos60792612017-05-24 10:51:01 -07004351 has_strong_ref = node->has_strong_ref;
4352 has_weak_ref = node->has_weak_ref;
4353
4354 if (weak && !has_weak_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004355 node->has_weak_ref = 1;
4356 node->pending_weak_ref = 1;
4357 node->local_weak_refs++;
Todd Kjos60792612017-05-24 10:51:01 -07004358 }
4359 if (strong && !has_strong_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004360 node->has_strong_ref = 1;
4361 node->pending_strong_ref = 1;
4362 node->local_strong_refs++;
Todd Kjos60792612017-05-24 10:51:01 -07004363 }
4364 if (!strong && has_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004365 node->has_strong_ref = 0;
Todd Kjos60792612017-05-24 10:51:01 -07004366 if (!weak && has_weak_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004367 node->has_weak_ref = 0;
Todd Kjos60792612017-05-24 10:51:01 -07004368 if (!weak && !strong) {
4369 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4370 "%d:%d node %d u%016llx c%016llx deleted\n",
4371 proc->pid, thread->pid,
4372 node_debug_id,
4373 (u64)node_ptr,
4374 (u64)node_cookie);
4375 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004376 binder_inner_proc_unlock(proc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004377 binder_node_lock(node);
4378 /*
4379 * Acquire the node lock before freeing the
4380 * node to serialize with other threads that
4381 * may have been holding the node lock while
4382 * decrementing this node (avoids race where
4383 * this thread frees while the other thread
4384 * is unlocking the node after the final
4385 * decrement)
4386 */
4387 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004388 binder_free_node(node);
4389 } else
4390 binder_inner_proc_unlock(proc);
4391
Todd Kjos60792612017-05-24 10:51:01 -07004392 if (weak && !has_weak_ref)
4393 ret = binder_put_node_cmd(
4394 proc, thread, &ptr, node_ptr,
4395 node_cookie, node_debug_id,
4396 BR_INCREFS, "BR_INCREFS");
4397 if (!ret && strong && !has_strong_ref)
4398 ret = binder_put_node_cmd(
4399 proc, thread, &ptr, node_ptr,
4400 node_cookie, node_debug_id,
4401 BR_ACQUIRE, "BR_ACQUIRE");
4402 if (!ret && !strong && has_strong_ref)
4403 ret = binder_put_node_cmd(
4404 proc, thread, &ptr, node_ptr,
4405 node_cookie, node_debug_id,
4406 BR_RELEASE, "BR_RELEASE");
4407 if (!ret && !weak && has_weak_ref)
4408 ret = binder_put_node_cmd(
4409 proc, thread, &ptr, node_ptr,
4410 node_cookie, node_debug_id,
4411 BR_DECREFS, "BR_DECREFS");
4412 if (orig_ptr == ptr)
4413 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4414 "%d:%d node %d u%016llx c%016llx state unchanged\n",
4415 proc->pid, thread->pid,
4416 node_debug_id,
4417 (u64)node_ptr,
4418 (u64)node_cookie);
4419 if (ret)
4420 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004421 } break;
4422 case BINDER_WORK_DEAD_BINDER:
4423 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4424 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4425 struct binder_ref_death *death;
4426 uint32_t cmd;
Martijn Coenenf9eac642017-05-22 11:26:23 -07004427 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004428
4429 death = container_of(w, struct binder_ref_death, work);
4430 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
4431 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
4432 else
4433 cmd = BR_DEAD_BINDER;
Martijn Coenenf9eac642017-05-22 11:26:23 -07004434 cookie = death->cookie;
4435
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004436 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004437 "%d:%d %s %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004438 proc->pid, thread->pid,
4439 cmd == BR_DEAD_BINDER ?
4440 "BR_DEAD_BINDER" :
4441 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
Martijn Coenenf9eac642017-05-22 11:26:23 -07004442 (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004443 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
Martijn Coenenf9eac642017-05-22 11:26:23 -07004444 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004445 kfree(death);
4446 binder_stats_deleted(BINDER_STAT_DEATH);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004447 } else {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004448 binder_enqueue_work_ilocked(
4449 w, &proc->delivered_death);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004450 binder_inner_proc_unlock(proc);
4451 }
Martijn Coenenf9eac642017-05-22 11:26:23 -07004452 if (put_user(cmd, (uint32_t __user *)ptr))
4453 return -EFAULT;
4454 ptr += sizeof(uint32_t);
4455 if (put_user(cookie,
4456 (binder_uintptr_t __user *)ptr))
4457 return -EFAULT;
4458 ptr += sizeof(binder_uintptr_t);
4459 binder_stat_br(proc, thread, cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004460 if (cmd == BR_DEAD_BINDER)
4461 goto done; /* DEAD_BINDER notifications can cause transactions */
4462 } break;
4463 }
4464
4465 if (!t)
4466 continue;
4467
4468 BUG_ON(t->buffer == NULL);
4469 if (t->buffer->target_node) {
4470 struct binder_node *target_node = t->buffer->target_node;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004471 struct binder_priority node_prio;
Seunghun Lee10f62862014-05-01 01:30:23 +09004472
Todd Kjos63e0afa2019-01-14 09:10:21 -08004473 trd->target.ptr = target_node->ptr;
4474 trd->cookie = target_node->cookie;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004475 node_prio.sched_policy = target_node->sched_policy;
4476 node_prio.prio = target_node->min_priority;
Martijn Coenenc46810c2017-06-23 10:13:43 -07004477 binder_transaction_priority(current, t, node_prio,
4478 target_node->inherit_rt);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004479 cmd = BR_TRANSACTION;
4480 } else {
Todd Kjos63e0afa2019-01-14 09:10:21 -08004481 trd->target.ptr = 0;
4482 trd->cookie = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004483 cmd = BR_REPLY;
4484 }
Todd Kjos63e0afa2019-01-14 09:10:21 -08004485 trd->code = t->code;
4486 trd->flags = t->flags;
4487 trd->sender_euid = from_kuid(current_user_ns(), t->sender_euid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004488
Todd Kjos2f993e22017-05-12 14:42:55 -07004489 t_from = binder_get_txn_from(t);
4490 if (t_from) {
4491 struct task_struct *sender = t_from->proc->tsk;
Seunghun Lee10f62862014-05-01 01:30:23 +09004492
Todd Kjos63e0afa2019-01-14 09:10:21 -08004493 trd->sender_pid =
4494 task_tgid_nr_ns(sender,
4495 task_active_pid_ns(current));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004496 } else {
Todd Kjos63e0afa2019-01-14 09:10:21 -08004497 trd->sender_pid = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004498 }
4499
Todd Kjos63e0afa2019-01-14 09:10:21 -08004500 trd->data_size = t->buffer->data_size;
4501 trd->offsets_size = t->buffer->offsets_size;
Todd Kjos8539b1e2019-02-08 10:35:20 -08004502 trd->data.ptr.buffer = (uintptr_t)t->buffer->user_data;
Todd Kjos63e0afa2019-01-14 09:10:21 -08004503 trd->data.ptr.offsets = trd->data.ptr.buffer +
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004504 ALIGN(t->buffer->data_size,
4505 sizeof(void *));
4506
Todd Kjos63e0afa2019-01-14 09:10:21 -08004507 tr.secctx = t->security_ctx;
4508 if (t->security_ctx) {
4509 cmd = BR_TRANSACTION_SEC_CTX;
4510 trsize = sizeof(tr);
4511 }
Todd Kjos2f993e22017-05-12 14:42:55 -07004512 if (put_user(cmd, (uint32_t __user *)ptr)) {
4513 if (t_from)
4514 binder_thread_dec_tmpref(t_from);
Martijn Coenen3217ccc2017-08-24 15:23:36 +02004515
4516 binder_cleanup_transaction(t, "put_user failed",
4517 BR_FAILED_REPLY);
4518
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004519 return -EFAULT;
Todd Kjos2f993e22017-05-12 14:42:55 -07004520 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004521 ptr += sizeof(uint32_t);
Todd Kjos63e0afa2019-01-14 09:10:21 -08004522 if (copy_to_user(ptr, &tr, trsize)) {
Todd Kjos2f993e22017-05-12 14:42:55 -07004523 if (t_from)
4524 binder_thread_dec_tmpref(t_from);
Martijn Coenen3217ccc2017-08-24 15:23:36 +02004525
4526 binder_cleanup_transaction(t, "copy_to_user failed",
4527 BR_FAILED_REPLY);
4528
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004529 return -EFAULT;
Todd Kjos2f993e22017-05-12 14:42:55 -07004530 }
Todd Kjos63e0afa2019-01-14 09:10:21 -08004531 ptr += trsize;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004532
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004533 trace_binder_transaction_received(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004534 binder_stat_br(proc, thread, cmd);
4535 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004536 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004537 proc->pid, thread->pid,
4538 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
Todd Kjos63e0afa2019-01-14 09:10:21 -08004539 (cmd == BR_TRANSACTION_SEC_CTX) ?
4540 "BR_TRANSACTION_SEC_CTX" : "BR_REPLY",
Todd Kjos2f993e22017-05-12 14:42:55 -07004541 t->debug_id, t_from ? t_from->proc->pid : 0,
4542 t_from ? t_from->pid : 0, cmd,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004543 t->buffer->data_size, t->buffer->offsets_size,
Todd Kjos63e0afa2019-01-14 09:10:21 -08004544 (u64)trd->data.ptr.buffer,
4545 (u64)trd->data.ptr.offsets);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004546
Todd Kjos2f993e22017-05-12 14:42:55 -07004547 if (t_from)
4548 binder_thread_dec_tmpref(t_from);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004549 t->buffer->allow_user_free = 1;
Todd Kjos63e0afa2019-01-14 09:10:21 -08004550 if (cmd != BR_REPLY && !(t->flags & TF_ONE_WAY)) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07004551 binder_inner_proc_lock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004552 t->to_parent = thread->transaction_stack;
4553 t->to_thread = thread;
4554 thread->transaction_stack = t;
Martijn Coenen995a36e2017-06-02 13:36:52 -07004555 binder_inner_proc_unlock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004556 } else {
Todd Kjos21ef40a2017-03-30 18:02:13 -07004557 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004558 }
4559 break;
4560 }
4561
4562done:
4563
4564 *consumed = ptr - buffer;
Todd Kjosd600e902017-05-25 17:35:02 -07004565 binder_inner_proc_lock(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004566 if (proc->requested_threads == 0 &&
4567 list_empty(&thread->proc->waiting_threads) &&
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004568 proc->requested_threads_started < proc->max_threads &&
4569 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4570 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
4571 /*spawn a new thread if we leave this out */) {
4572 proc->requested_threads++;
Todd Kjosd600e902017-05-25 17:35:02 -07004573 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004574 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304575 "%d:%d BR_SPAWN_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004576 proc->pid, thread->pid);
4577 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
4578 return -EFAULT;
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07004579 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
Todd Kjosd600e902017-05-25 17:35:02 -07004580 } else
4581 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004582 return 0;
4583}
4584
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004585static void binder_release_work(struct binder_proc *proc,
4586 struct list_head *list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004587{
4588 struct binder_work *w;
Seunghun Lee10f62862014-05-01 01:30:23 +09004589
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004590 while (1) {
4591 w = binder_dequeue_work_head(proc, list);
4592 if (!w)
4593 return;
4594
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004595 switch (w->type) {
4596 case BINDER_WORK_TRANSACTION: {
4597 struct binder_transaction *t;
4598
4599 t = container_of(w, struct binder_transaction, work);
Martijn Coenen3217ccc2017-08-24 15:23:36 +02004600
4601 binder_cleanup_transaction(t, "process died.",
4602 BR_DEAD_REPLY);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004603 } break;
Todd Kjos858b8da2017-04-21 17:35:12 -07004604 case BINDER_WORK_RETURN_ERROR: {
4605 struct binder_error *e = container_of(
4606 w, struct binder_error, work);
4607
4608 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4609 "undelivered TRANSACTION_ERROR: %u\n",
4610 e->cmd);
4611 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004612 case BINDER_WORK_TRANSACTION_COMPLETE: {
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004613 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304614 "undelivered TRANSACTION_COMPLETE\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004615 kfree(w);
4616 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4617 } break;
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004618 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4619 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4620 struct binder_ref_death *death;
4621
4622 death = container_of(w, struct binder_ref_death, work);
4623 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004624 "undelivered death notification, %016llx\n",
4625 (u64)death->cookie);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004626 kfree(death);
4627 binder_stats_deleted(BINDER_STAT_DEATH);
4628 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004629 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304630 pr_err("unexpected work type, %d, not freed\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004631 w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004632 break;
4633 }
4634 }
4635
4636}
4637
Todd Kjosb4827902017-05-25 15:52:17 -07004638static struct binder_thread *binder_get_thread_ilocked(
4639 struct binder_proc *proc, struct binder_thread *new_thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004640{
4641 struct binder_thread *thread = NULL;
4642 struct rb_node *parent = NULL;
4643 struct rb_node **p = &proc->threads.rb_node;
4644
4645 while (*p) {
4646 parent = *p;
4647 thread = rb_entry(parent, struct binder_thread, rb_node);
4648
4649 if (current->pid < thread->pid)
4650 p = &(*p)->rb_left;
4651 else if (current->pid > thread->pid)
4652 p = &(*p)->rb_right;
4653 else
Todd Kjosb4827902017-05-25 15:52:17 -07004654 return thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004655 }
Todd Kjosb4827902017-05-25 15:52:17 -07004656 if (!new_thread)
4657 return NULL;
4658 thread = new_thread;
4659 binder_stats_created(BINDER_STAT_THREAD);
4660 thread->proc = proc;
4661 thread->pid = current->pid;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004662 get_task_struct(current);
4663 thread->task = current;
Todd Kjosb4827902017-05-25 15:52:17 -07004664 atomic_set(&thread->tmp_ref, 0);
4665 init_waitqueue_head(&thread->wait);
4666 INIT_LIST_HEAD(&thread->todo);
4667 rb_link_node(&thread->rb_node, parent, p);
4668 rb_insert_color(&thread->rb_node, &proc->threads);
4669 thread->looper_need_return = true;
4670 thread->return_error.work.type = BINDER_WORK_RETURN_ERROR;
4671 thread->return_error.cmd = BR_OK;
4672 thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR;
4673 thread->reply_error.cmd = BR_OK;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004674 INIT_LIST_HEAD(&new_thread->waiting_thread_node);
Todd Kjosb4827902017-05-25 15:52:17 -07004675 return thread;
4676}
4677
4678static struct binder_thread *binder_get_thread(struct binder_proc *proc)
4679{
4680 struct binder_thread *thread;
4681 struct binder_thread *new_thread;
4682
4683 binder_inner_proc_lock(proc);
4684 thread = binder_get_thread_ilocked(proc, NULL);
4685 binder_inner_proc_unlock(proc);
4686 if (!thread) {
4687 new_thread = kzalloc(sizeof(*thread), GFP_KERNEL);
4688 if (new_thread == NULL)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004689 return NULL;
Todd Kjosb4827902017-05-25 15:52:17 -07004690 binder_inner_proc_lock(proc);
4691 thread = binder_get_thread_ilocked(proc, new_thread);
4692 binder_inner_proc_unlock(proc);
4693 if (thread != new_thread)
4694 kfree(new_thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004695 }
4696 return thread;
4697}
4698
Todd Kjos2f993e22017-05-12 14:42:55 -07004699static void binder_free_proc(struct binder_proc *proc)
4700{
4701 BUG_ON(!list_empty(&proc->todo));
4702 BUG_ON(!list_empty(&proc->delivered_death));
4703 binder_alloc_deferred_release(&proc->alloc);
4704 put_task_struct(proc->tsk);
4705 binder_stats_deleted(BINDER_STAT_PROC);
4706 kfree(proc);
4707}
4708
4709static void binder_free_thread(struct binder_thread *thread)
4710{
4711 BUG_ON(!list_empty(&thread->todo));
4712 binder_stats_deleted(BINDER_STAT_THREAD);
4713 binder_proc_dec_tmpref(thread->proc);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004714 put_task_struct(thread->task);
Todd Kjos2f993e22017-05-12 14:42:55 -07004715 kfree(thread);
4716}
4717
4718static int binder_thread_release(struct binder_proc *proc,
4719 struct binder_thread *thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004720{
4721 struct binder_transaction *t;
4722 struct binder_transaction *send_reply = NULL;
4723 int active_transactions = 0;
Todd Kjos2f993e22017-05-12 14:42:55 -07004724 struct binder_transaction *last_t = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004725
Todd Kjosb4827902017-05-25 15:52:17 -07004726 binder_inner_proc_lock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07004727 /*
4728 * take a ref on the proc so it survives
4729 * after we remove this thread from proc->threads.
4730 * The corresponding dec is when we actually
4731 * free the thread in binder_free_thread()
4732 */
Todd Kjosdc734d72019-06-10 09:14:25 -07004733 atomic_inc(&proc->tmp_ref);
Todd Kjos2f993e22017-05-12 14:42:55 -07004734 /*
4735 * take a ref on this thread to ensure it
4736 * survives while we are releasing it
4737 */
4738 atomic_inc(&thread->tmp_ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004739 rb_erase(&thread->rb_node, &proc->threads);
4740 t = thread->transaction_stack;
Todd Kjos2f993e22017-05-12 14:42:55 -07004741 if (t) {
4742 spin_lock(&t->lock);
4743 if (t->to_thread == thread)
4744 send_reply = t;
4745 }
4746 thread->is_dead = true;
4747
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004748 while (t) {
Todd Kjos2f993e22017-05-12 14:42:55 -07004749 last_t = t;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004750 active_transactions++;
4751 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304752 "release %d:%d transaction %d %s, still active\n",
4753 proc->pid, thread->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004754 t->debug_id,
4755 (t->to_thread == thread) ? "in" : "out");
4756
4757 if (t->to_thread == thread) {
4758 t->to_proc = NULL;
4759 t->to_thread = NULL;
4760 if (t->buffer) {
4761 t->buffer->transaction = NULL;
4762 t->buffer = NULL;
4763 }
4764 t = t->to_parent;
4765 } else if (t->from == thread) {
4766 t->from = NULL;
4767 t = t->from_parent;
4768 } else
4769 BUG();
Todd Kjos2f993e22017-05-12 14:42:55 -07004770 spin_unlock(&last_t->lock);
4771 if (t)
4772 spin_lock(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004773 }
Martijn Coenen550c01d2018-01-05 11:27:07 +01004774
4775 /*
4776 * If this thread used poll, make sure we remove the waitqueue
4777 * from any epoll data structures holding it with POLLFREE.
4778 * waitqueue_active() is safe to use here because we're holding
4779 * the inner lock.
4780 */
4781 if ((thread->looper & BINDER_LOOPER_STATE_POLL) &&
4782 waitqueue_active(&thread->wait)) {
4783 wake_up_poll(&thread->wait, POLLHUP | POLLFREE);
4784 }
4785
Todd Kjosb4827902017-05-25 15:52:17 -07004786 binder_inner_proc_unlock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07004787
Martijn Coenen72766d72018-02-16 09:47:15 +01004788 /*
4789 * This is needed to avoid races between wake_up_poll() above and
4790 * and ep_remove_waitqueue() called for other reasons (eg the epoll file
4791 * descriptor being closed); ep_remove_waitqueue() holds an RCU read
4792 * lock, so we can be sure it's done after calling synchronize_rcu().
4793 */
4794 if (thread->looper & BINDER_LOOPER_STATE_POLL)
4795 synchronize_rcu();
4796
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004797 if (send_reply)
4798 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004799 binder_release_work(proc, &thread->todo);
Todd Kjos2f993e22017-05-12 14:42:55 -07004800 binder_thread_dec_tmpref(thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004801 return active_transactions;
4802}
4803
4804static unsigned int binder_poll(struct file *filp,
4805 struct poll_table_struct *wait)
4806{
4807 struct binder_proc *proc = filp->private_data;
4808 struct binder_thread *thread = NULL;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004809 bool wait_for_proc_work;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004810
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004811 thread = binder_get_thread(proc);
Greg Kroah-Hartman6e463bb2018-02-28 17:17:14 +01004812 if (!thread)
Eric Biggers4be5a282018-01-30 23:11:24 -08004813 return POLLERR;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004814
Martijn Coenen995a36e2017-06-02 13:36:52 -07004815 binder_inner_proc_lock(thread->proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004816 thread->looper |= BINDER_LOOPER_STATE_POLL;
4817 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
4818
Martijn Coenen995a36e2017-06-02 13:36:52 -07004819 binder_inner_proc_unlock(thread->proc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004820
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004821 poll_wait(filp, &thread->wait, wait);
4822
Martijn Coenen47810932017-08-10 12:32:00 +02004823 if (binder_has_work(thread, wait_for_proc_work))
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004824 return POLLIN;
4825
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004826 return 0;
4827}
4828
Tair Rzayev78260ac2014-06-03 22:27:21 +03004829static int binder_ioctl_write_read(struct file *filp,
4830 unsigned int cmd, unsigned long arg,
4831 struct binder_thread *thread)
4832{
4833 int ret = 0;
4834 struct binder_proc *proc = filp->private_data;
4835 unsigned int size = _IOC_SIZE(cmd);
4836 void __user *ubuf = (void __user *)arg;
4837 struct binder_write_read bwr;
4838
4839 if (size != sizeof(struct binder_write_read)) {
4840 ret = -EINVAL;
4841 goto out;
4842 }
4843 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
4844 ret = -EFAULT;
4845 goto out;
4846 }
4847 binder_debug(BINDER_DEBUG_READ_WRITE,
4848 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
4849 proc->pid, thread->pid,
4850 (u64)bwr.write_size, (u64)bwr.write_buffer,
4851 (u64)bwr.read_size, (u64)bwr.read_buffer);
4852
4853 if (bwr.write_size > 0) {
4854 ret = binder_thread_write(proc, thread,
4855 bwr.write_buffer,
4856 bwr.write_size,
4857 &bwr.write_consumed);
4858 trace_binder_write_done(ret);
4859 if (ret < 0) {
4860 bwr.read_consumed = 0;
4861 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4862 ret = -EFAULT;
4863 goto out;
4864 }
4865 }
4866 if (bwr.read_size > 0) {
4867 ret = binder_thread_read(proc, thread, bwr.read_buffer,
4868 bwr.read_size,
4869 &bwr.read_consumed,
4870 filp->f_flags & O_NONBLOCK);
4871 trace_binder_read_done(ret);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004872 binder_inner_proc_lock(proc);
4873 if (!binder_worklist_empty_ilocked(&proc->todo))
Martijn Coenen053be422017-06-06 15:17:46 -07004874 binder_wakeup_proc_ilocked(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004875 binder_inner_proc_unlock(proc);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004876 if (ret < 0) {
4877 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4878 ret = -EFAULT;
4879 goto out;
4880 }
4881 }
4882 binder_debug(BINDER_DEBUG_READ_WRITE,
4883 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
4884 proc->pid, thread->pid,
4885 (u64)bwr.write_consumed, (u64)bwr.write_size,
4886 (u64)bwr.read_consumed, (u64)bwr.read_size);
4887 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
4888 ret = -EFAULT;
4889 goto out;
4890 }
4891out:
4892 return ret;
4893}
4894
Todd Kjos63e0afa2019-01-14 09:10:21 -08004895static int binder_ioctl_set_ctx_mgr(struct file *filp,
4896 struct flat_binder_object *fbo)
Tair Rzayev78260ac2014-06-03 22:27:21 +03004897{
4898 int ret = 0;
4899 struct binder_proc *proc = filp->private_data;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004900 struct binder_context *context = proc->context;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004901 struct binder_node *new_node;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004902 kuid_t curr_euid = current_euid();
4903
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004904 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004905 if (context->binder_context_mgr_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004906 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
4907 ret = -EBUSY;
4908 goto out;
4909 }
Stephen Smalley79af7302015-01-21 10:54:10 -05004910 ret = security_binder_set_context_mgr(proc->tsk);
4911 if (ret < 0)
4912 goto out;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004913 if (uid_valid(context->binder_context_mgr_uid)) {
4914 if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004915 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
4916 from_kuid(&init_user_ns, curr_euid),
4917 from_kuid(&init_user_ns,
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004918 context->binder_context_mgr_uid));
Tair Rzayev78260ac2014-06-03 22:27:21 +03004919 ret = -EPERM;
4920 goto out;
4921 }
4922 } else {
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004923 context->binder_context_mgr_uid = curr_euid;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004924 }
Todd Kjos63e0afa2019-01-14 09:10:21 -08004925 new_node = binder_new_node(proc, fbo);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004926 if (!new_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004927 ret = -ENOMEM;
4928 goto out;
4929 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004930 binder_node_lock(new_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004931 new_node->local_weak_refs++;
4932 new_node->local_strong_refs++;
4933 new_node->has_strong_ref = 1;
4934 new_node->has_weak_ref = 1;
4935 context->binder_context_mgr_node = new_node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004936 binder_node_unlock(new_node);
Todd Kjosf22abc72017-05-09 11:08:05 -07004937 binder_put_node(new_node);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004938out:
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004939 mutex_unlock(&context->context_mgr_node_lock);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004940 return ret;
4941}
4942
Martijn Coenen1c57ba42018-08-25 13:50:56 -07004943static int binder_ioctl_get_node_info_for_ref(struct binder_proc *proc,
4944 struct binder_node_info_for_ref *info)
4945{
4946 struct binder_node *node;
4947 struct binder_context *context = proc->context;
4948 __u32 handle = info->handle;
4949
4950 if (info->strong_count || info->weak_count || info->reserved1 ||
4951 info->reserved2 || info->reserved3) {
4952 binder_user_error("%d BINDER_GET_NODE_INFO_FOR_REF: only handle may be non-zero.",
4953 proc->pid);
4954 return -EINVAL;
4955 }
4956
4957 /* This ioctl may only be used by the context manager */
4958 mutex_lock(&context->context_mgr_node_lock);
4959 if (!context->binder_context_mgr_node ||
4960 context->binder_context_mgr_node->proc != proc) {
4961 mutex_unlock(&context->context_mgr_node_lock);
4962 return -EPERM;
4963 }
4964 mutex_unlock(&context->context_mgr_node_lock);
4965
4966 node = binder_get_node_from_ref(proc, handle, true, NULL);
4967 if (!node)
4968 return -EINVAL;
4969
4970 info->strong_count = node->local_strong_refs +
4971 node->internal_strong_refs;
4972 info->weak_count = node->local_weak_refs;
4973
4974 binder_put_node(node);
4975
4976 return 0;
4977}
4978
Colin Cross833babb32017-06-20 13:54:44 -07004979static int binder_ioctl_get_node_debug_info(struct binder_proc *proc,
4980 struct binder_node_debug_info *info) {
4981 struct rb_node *n;
4982 binder_uintptr_t ptr = info->ptr;
4983
4984 memset(info, 0, sizeof(*info));
4985
4986 binder_inner_proc_lock(proc);
4987 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
4988 struct binder_node *node = rb_entry(n, struct binder_node,
4989 rb_node);
4990 if (node->ptr > ptr) {
4991 info->ptr = node->ptr;
4992 info->cookie = node->cookie;
4993 info->has_strong_ref = node->has_strong_ref;
4994 info->has_weak_ref = node->has_weak_ref;
4995 break;
4996 }
4997 }
4998 binder_inner_proc_unlock(proc);
4999
5000 return 0;
5001}
5002
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005003static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
5004{
5005 int ret;
5006 struct binder_proc *proc = filp->private_data;
5007 struct binder_thread *thread;
5008 unsigned int size = _IOC_SIZE(cmd);
5009 void __user *ubuf = (void __user *)arg;
5010
Tair Rzayev78260ac2014-06-03 22:27:21 +03005011 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
5012 proc->pid, current->pid, cmd, arg);*/
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005013
Sherry Yang435416b2017-06-22 14:37:45 -07005014 binder_selftest_alloc(&proc->alloc);
5015
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07005016 trace_binder_ioctl(cmd, arg);
5017
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005018 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
5019 if (ret)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07005020 goto err_unlocked;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005021
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005022 thread = binder_get_thread(proc);
5023 if (thread == NULL) {
5024 ret = -ENOMEM;
5025 goto err;
5026 }
5027
5028 switch (cmd) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03005029 case BINDER_WRITE_READ:
5030 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
5031 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005032 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005033 break;
Todd Kjosd600e902017-05-25 17:35:02 -07005034 case BINDER_SET_MAX_THREADS: {
5035 int max_threads;
5036
5037 if (copy_from_user(&max_threads, ubuf,
5038 sizeof(max_threads))) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005039 ret = -EINVAL;
5040 goto err;
5041 }
Todd Kjosd600e902017-05-25 17:35:02 -07005042 binder_inner_proc_lock(proc);
5043 proc->max_threads = max_threads;
5044 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005045 break;
Todd Kjosd600e902017-05-25 17:35:02 -07005046 }
Todd Kjos63e0afa2019-01-14 09:10:21 -08005047 case BINDER_SET_CONTEXT_MGR_EXT: {
5048 struct flat_binder_object fbo;
5049
5050 if (copy_from_user(&fbo, ubuf, sizeof(fbo))) {
5051 ret = -EINVAL;
5052 goto err;
5053 }
5054 ret = binder_ioctl_set_ctx_mgr(filp, &fbo);
5055 if (ret)
5056 goto err;
5057 break;
5058 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005059 case BINDER_SET_CONTEXT_MGR:
Todd Kjos63e0afa2019-01-14 09:10:21 -08005060 ret = binder_ioctl_set_ctx_mgr(filp, NULL);
Tair Rzayev78260ac2014-06-03 22:27:21 +03005061 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005062 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005063 break;
5064 case BINDER_THREAD_EXIT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05305065 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005066 proc->pid, thread->pid);
Todd Kjos2f993e22017-05-12 14:42:55 -07005067 binder_thread_release(proc, thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005068 thread = NULL;
5069 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02005070 case BINDER_VERSION: {
5071 struct binder_version __user *ver = ubuf;
5072
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005073 if (size != sizeof(struct binder_version)) {
5074 ret = -EINVAL;
5075 goto err;
5076 }
Mathieu Maret36c89c02014-04-15 12:03:05 +02005077 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
5078 &ver->protocol_version)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005079 ret = -EINVAL;
5080 goto err;
5081 }
5082 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02005083 }
Martijn Coenen1c57ba42018-08-25 13:50:56 -07005084 case BINDER_GET_NODE_INFO_FOR_REF: {
5085 struct binder_node_info_for_ref info;
5086
5087 if (copy_from_user(&info, ubuf, sizeof(info))) {
5088 ret = -EFAULT;
5089 goto err;
5090 }
5091
5092 ret = binder_ioctl_get_node_info_for_ref(proc, &info);
5093 if (ret < 0)
5094 goto err;
5095
5096 if (copy_to_user(ubuf, &info, sizeof(info))) {
5097 ret = -EFAULT;
5098 goto err;
5099 }
5100
5101 break;
5102 }
Colin Cross833babb32017-06-20 13:54:44 -07005103 case BINDER_GET_NODE_DEBUG_INFO: {
5104 struct binder_node_debug_info info;
5105
5106 if (copy_from_user(&info, ubuf, sizeof(info))) {
5107 ret = -EFAULT;
5108 goto err;
5109 }
5110
5111 ret = binder_ioctl_get_node_debug_info(proc, &info);
5112 if (ret < 0)
5113 goto err;
5114
5115 if (copy_to_user(ubuf, &info, sizeof(info))) {
5116 ret = -EFAULT;
5117 goto err;
5118 }
5119 break;
5120 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005121 default:
5122 ret = -EINVAL;
5123 goto err;
5124 }
5125 ret = 0;
5126err:
5127 if (thread)
Todd Kjos6798e6d2017-01-06 14:19:25 -08005128 thread->looper_need_return = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005129 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
5130 if (ret && ret != -ERESTARTSYS)
Anmol Sarma56b468f2012-10-30 22:35:43 +05305131 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 -07005132err_unlocked:
5133 trace_binder_ioctl_done(ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005134 return ret;
5135}
5136
5137static void binder_vma_open(struct vm_area_struct *vma)
5138{
5139 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09005140
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005141 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05305142 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005143 proc->pid, vma->vm_start, vma->vm_end,
5144 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
5145 (unsigned long)pgprot_val(vma->vm_page_prot));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005146}
5147
5148static void binder_vma_close(struct vm_area_struct *vma)
5149{
5150 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09005151
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005152 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05305153 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005154 proc->pid, vma->vm_start, vma->vm_end,
5155 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
5156 (unsigned long)pgprot_val(vma->vm_page_prot));
Todd Kjosd325d372016-10-10 10:40:53 -07005157 binder_alloc_vma_close(&proc->alloc);
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005158 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005159}
5160
Vinayak Menonddac7d52014-06-02 18:17:59 +05305161static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
5162{
5163 return VM_FAULT_SIGBUS;
5164}
5165
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07005166static const struct vm_operations_struct binder_vm_ops = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005167 .open = binder_vma_open,
5168 .close = binder_vma_close,
Vinayak Menonddac7d52014-06-02 18:17:59 +05305169 .fault = binder_vm_fault,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005170};
5171
Todd Kjosd325d372016-10-10 10:40:53 -07005172static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
5173{
5174 int ret;
5175 struct binder_proc *proc = filp->private_data;
5176 const char *failure_string;
5177
5178 if (proc->tsk != current->group_leader)
5179 return -EINVAL;
5180
5181 if ((vma->vm_end - vma->vm_start) > SZ_4M)
5182 vma->vm_end = vma->vm_start + SZ_4M;
5183
5184 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5185 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
5186 __func__, proc->pid, vma->vm_start, vma->vm_end,
5187 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
5188 (unsigned long)pgprot_val(vma->vm_page_prot));
5189
5190 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
5191 ret = -EPERM;
5192 failure_string = "bad vm_flags";
5193 goto err_bad_arg;
5194 }
Minchan Kim2cafd5b2018-05-07 23:15:37 +09005195 vma->vm_flags |= VM_DONTCOPY | VM_MIXEDMAP;
5196 vma->vm_flags &= ~VM_MAYWRITE;
5197
Todd Kjosd325d372016-10-10 10:40:53 -07005198 vma->vm_ops = &binder_vm_ops;
5199 vma->vm_private_data = proc;
5200
5201 ret = binder_alloc_mmap_handler(&proc->alloc, vma);
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005202 if (ret)
5203 return ret;
Todd Kjosfbb43392017-11-27 09:32:33 -08005204 mutex_lock(&proc->files_lock);
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005205 proc->files = get_files_struct(current);
Todd Kjosfbb43392017-11-27 09:32:33 -08005206 mutex_unlock(&proc->files_lock);
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005207 return 0;
Todd Kjosd325d372016-10-10 10:40:53 -07005208
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005209err_bad_arg:
Elad Wexler6b646402017-12-29 11:03:37 +02005210 pr_err("%s: %d %lx-%lx %s failed %d\n", __func__,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005211 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
5212 return ret;
5213}
5214
5215static int binder_open(struct inode *nodp, struct file *filp)
5216{
5217 struct binder_proc *proc;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005218 struct binder_device *binder_dev;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005219
Elad Wexler6b646402017-12-29 11:03:37 +02005220 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "%s: %d:%d\n", __func__,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005221 current->group_leader->pid, current->pid);
5222
5223 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
5224 if (proc == NULL)
5225 return -ENOMEM;
Todd Kjosfc7a7e22017-05-29 16:44:24 -07005226 spin_lock_init(&proc->inner_lock);
5227 spin_lock_init(&proc->outer_lock);
Todd Kjosdc734d72019-06-10 09:14:25 -07005228 atomic_set(&proc->tmp_ref, 0);
Martijn Coenen872c26e2017-03-07 15:51:18 +01005229 get_task_struct(current->group_leader);
5230 proc->tsk = current->group_leader;
Todd Kjosfbb43392017-11-27 09:32:33 -08005231 mutex_init(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005232 INIT_LIST_HEAD(&proc->todo);
Martijn Coenen57b2ac62017-06-06 17:04:42 -07005233 if (binder_supported_policy(current->policy)) {
5234 proc->default_priority.sched_policy = current->policy;
5235 proc->default_priority.prio = current->normal_prio;
5236 } else {
5237 proc->default_priority.sched_policy = SCHED_NORMAL;
5238 proc->default_priority.prio = NICE_TO_PRIO(0);
5239 }
5240
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005241 binder_dev = container_of(filp->private_data, struct binder_device,
5242 miscdev);
5243 proc->context = &binder_dev->context;
Todd Kjosd325d372016-10-10 10:40:53 -07005244 binder_alloc_init(&proc->alloc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07005245
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005246 binder_stats_created(BINDER_STAT_PROC);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005247 proc->pid = current->group_leader->pid;
5248 INIT_LIST_HEAD(&proc->delivered_death);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005249 INIT_LIST_HEAD(&proc->waiting_threads);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005250 filp->private_data = proc;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07005251
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005252 mutex_lock(&binder_procs_lock);
5253 hlist_add_head(&proc->proc_node, &binder_procs);
5254 mutex_unlock(&binder_procs_lock);
5255
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005256 if (binder_debugfs_dir_entry_proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005257 char strbuf[11];
Seunghun Lee10f62862014-05-01 01:30:23 +09005258
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005259 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005260 /*
5261 * proc debug entries are shared between contexts, so
5262 * this will fail if the process tries to open the driver
5263 * again with a different context. The priting code will
5264 * anyway print all contexts that a given PID has, so this
5265 * is not a problem.
5266 */
Harsh Shandilya174562a2017-12-22 19:37:02 +05305267 proc->debugfs_entry = debugfs_create_file(strbuf, 0444,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005268 binder_debugfs_dir_entry_proc,
5269 (void *)(unsigned long)proc->pid,
5270 &binder_proc_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005271 }
5272
5273 return 0;
5274}
5275
5276static int binder_flush(struct file *filp, fl_owner_t id)
5277{
5278 struct binder_proc *proc = filp->private_data;
5279
5280 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
5281
5282 return 0;
5283}
5284
5285static void binder_deferred_flush(struct binder_proc *proc)
5286{
5287 struct rb_node *n;
5288 int wake_count = 0;
Seunghun Lee10f62862014-05-01 01:30:23 +09005289
Todd Kjosb4827902017-05-25 15:52:17 -07005290 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005291 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
5292 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
Seunghun Lee10f62862014-05-01 01:30:23 +09005293
Todd Kjos6798e6d2017-01-06 14:19:25 -08005294 thread->looper_need_return = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005295 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
5296 wake_up_interruptible(&thread->wait);
5297 wake_count++;
5298 }
5299 }
Todd Kjosb4827902017-05-25 15:52:17 -07005300 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005301
5302 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5303 "binder_flush: %d woke %d threads\n", proc->pid,
5304 wake_count);
5305}
5306
5307static int binder_release(struct inode *nodp, struct file *filp)
5308{
5309 struct binder_proc *proc = filp->private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09005310
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005311 debugfs_remove(proc->debugfs_entry);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005312 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
5313
5314 return 0;
5315}
5316
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005317static int binder_node_release(struct binder_node *node, int refs)
5318{
5319 struct binder_ref *ref;
5320 int death = 0;
Todd Kjose7f23ed2017-03-21 13:06:01 -07005321 struct binder_proc *proc = node->proc;
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005322
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005323 binder_release_work(proc, &node->async_todo);
Todd Kjose7f23ed2017-03-21 13:06:01 -07005324
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005325 binder_node_lock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07005326 binder_inner_proc_lock(proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005327 binder_dequeue_work_ilocked(&node->work);
Todd Kjosf22abc72017-05-09 11:08:05 -07005328 /*
5329 * The caller must have taken a temporary ref on the node,
5330 */
5331 BUG_ON(!node->tmp_refs);
5332 if (hlist_empty(&node->refs) && node->tmp_refs == 1) {
Todd Kjose7f23ed2017-03-21 13:06:01 -07005333 binder_inner_proc_unlock(proc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005334 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07005335 binder_free_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005336
5337 return refs;
5338 }
5339
5340 node->proc = NULL;
5341 node->local_strong_refs = 0;
5342 node->local_weak_refs = 0;
Todd Kjose7f23ed2017-03-21 13:06:01 -07005343 binder_inner_proc_unlock(proc);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005344
5345 spin_lock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005346 hlist_add_head(&node->dead_node, &binder_dead_nodes);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005347 spin_unlock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005348
5349 hlist_for_each_entry(ref, &node->refs, node_entry) {
5350 refs++;
Martijn Coenenf9eac642017-05-22 11:26:23 -07005351 /*
5352 * Need the node lock to synchronize
5353 * with new notification requests and the
5354 * inner lock to synchronize with queued
5355 * death notifications.
5356 */
5357 binder_inner_proc_lock(ref->proc);
5358 if (!ref->death) {
5359 binder_inner_proc_unlock(ref->proc);
Arve Hjønnevåge194fd82014-02-17 13:58:29 -08005360 continue;
Martijn Coenenf9eac642017-05-22 11:26:23 -07005361 }
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005362
5363 death++;
5364
Martijn Coenenf9eac642017-05-22 11:26:23 -07005365 BUG_ON(!list_empty(&ref->death->work.entry));
5366 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
5367 binder_enqueue_work_ilocked(&ref->death->work,
5368 &ref->proc->todo);
Martijn Coenen053be422017-06-06 15:17:46 -07005369 binder_wakeup_proc_ilocked(ref->proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005370 binder_inner_proc_unlock(ref->proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005371 }
5372
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005373 binder_debug(BINDER_DEBUG_DEAD_BINDER,
5374 "node %d now dead, refs %d, death %d\n",
5375 node->debug_id, refs, death);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005376 binder_node_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07005377 binder_put_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005378
5379 return refs;
5380}
5381
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005382static void binder_deferred_release(struct binder_proc *proc)
5383{
Martijn Coenen0b3311e2016-09-30 15:51:48 +02005384 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005385 struct rb_node *n;
Todd Kjosd325d372016-10-10 10:40:53 -07005386 int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005387
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005388 BUG_ON(proc->files);
5389
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005390 mutex_lock(&binder_procs_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005391 hlist_del(&proc->proc_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005392 mutex_unlock(&binder_procs_lock);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005393
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005394 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02005395 if (context->binder_context_mgr_node &&
5396 context->binder_context_mgr_node->proc == proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005397 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01005398 "%s: %d context_mgr_node gone\n",
5399 __func__, proc->pid);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02005400 context->binder_context_mgr_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005401 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005402 mutex_unlock(&context->context_mgr_node_lock);
Todd Kjosb4827902017-05-25 15:52:17 -07005403 binder_inner_proc_lock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07005404 /*
5405 * Make sure proc stays alive after we
5406 * remove all the threads
5407 */
Todd Kjosdc734d72019-06-10 09:14:25 -07005408 atomic_inc(&proc->tmp_ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005409
Todd Kjos2f993e22017-05-12 14:42:55 -07005410 proc->is_dead = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005411 threads = 0;
5412 active_transactions = 0;
5413 while ((n = rb_first(&proc->threads))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005414 struct binder_thread *thread;
5415
5416 thread = rb_entry(n, struct binder_thread, rb_node);
Todd Kjosb4827902017-05-25 15:52:17 -07005417 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005418 threads++;
Todd Kjos2f993e22017-05-12 14:42:55 -07005419 active_transactions += binder_thread_release(proc, thread);
Todd Kjosb4827902017-05-25 15:52:17 -07005420 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005421 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005422
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005423 nodes = 0;
5424 incoming_refs = 0;
5425 while ((n = rb_first(&proc->nodes))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005426 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005427
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005428 node = rb_entry(n, struct binder_node, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005429 nodes++;
Todd Kjosf22abc72017-05-09 11:08:05 -07005430 /*
5431 * take a temporary ref on the node before
5432 * calling binder_node_release() which will either
5433 * kfree() the node or call binder_put_node()
5434 */
Todd Kjos425d23f2017-06-12 12:07:26 -07005435 binder_inc_node_tmpref_ilocked(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005436 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjos425d23f2017-06-12 12:07:26 -07005437 binder_inner_proc_unlock(proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005438 incoming_refs = binder_node_release(node, incoming_refs);
Todd Kjos425d23f2017-06-12 12:07:26 -07005439 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005440 }
Todd Kjos425d23f2017-06-12 12:07:26 -07005441 binder_inner_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005442
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005443 outgoing_refs = 0;
Todd Kjos5346bf32016-10-20 16:43:34 -07005444 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005445 while ((n = rb_first(&proc->refs_by_desc))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005446 struct binder_ref *ref;
5447
5448 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005449 outgoing_refs++;
Todd Kjos5346bf32016-10-20 16:43:34 -07005450 binder_cleanup_ref_olocked(ref);
5451 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07005452 binder_free_ref(ref);
Todd Kjos5346bf32016-10-20 16:43:34 -07005453 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005454 }
Todd Kjos5346bf32016-10-20 16:43:34 -07005455 binder_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005456
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005457 binder_release_work(proc, &proc->todo);
5458 binder_release_work(proc, &proc->delivered_death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005459
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005460 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Todd Kjosd325d372016-10-10 10:40:53 -07005461 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01005462 __func__, proc->pid, threads, nodes, incoming_refs,
Todd Kjosd325d372016-10-10 10:40:53 -07005463 outgoing_refs, active_transactions);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005464
Todd Kjos2f993e22017-05-12 14:42:55 -07005465 binder_proc_dec_tmpref(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005466}
5467
5468static void binder_deferred_func(struct work_struct *work)
5469{
5470 struct binder_proc *proc;
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005471 struct files_struct *files;
5472
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005473 int defer;
Seunghun Lee10f62862014-05-01 01:30:23 +09005474
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005475 do {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005476 mutex_lock(&binder_deferred_lock);
5477 if (!hlist_empty(&binder_deferred_list)) {
5478 proc = hlist_entry(binder_deferred_list.first,
5479 struct binder_proc, deferred_work_node);
5480 hlist_del_init(&proc->deferred_work_node);
5481 defer = proc->deferred_work;
5482 proc->deferred_work = 0;
5483 } else {
5484 proc = NULL;
5485 defer = 0;
5486 }
5487 mutex_unlock(&binder_deferred_lock);
5488
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005489 files = NULL;
5490 if (defer & BINDER_DEFERRED_PUT_FILES) {
Todd Kjosfbb43392017-11-27 09:32:33 -08005491 mutex_lock(&proc->files_lock);
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005492 files = proc->files;
5493 if (files)
5494 proc->files = NULL;
Todd Kjosfbb43392017-11-27 09:32:33 -08005495 mutex_unlock(&proc->files_lock);
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005496 }
5497
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005498 if (defer & BINDER_DEFERRED_FLUSH)
5499 binder_deferred_flush(proc);
5500
5501 if (defer & BINDER_DEFERRED_RELEASE)
5502 binder_deferred_release(proc); /* frees proc */
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005503
5504 if (files)
5505 put_files_struct(files);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005506 } while (proc);
5507}
5508static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
5509
5510static void
5511binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
5512{
5513 mutex_lock(&binder_deferred_lock);
5514 proc->deferred_work |= defer;
5515 if (hlist_unhashed(&proc->deferred_work_node)) {
5516 hlist_add_head(&proc->deferred_work_node,
5517 &binder_deferred_list);
Bhaktipriya Shridhar1beba522016-08-13 22:16:24 +05305518 schedule_work(&binder_deferred_work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005519 }
5520 mutex_unlock(&binder_deferred_lock);
5521}
5522
Todd Kjos6d241a42017-04-21 14:32:11 -07005523static void print_binder_transaction_ilocked(struct seq_file *m,
5524 struct binder_proc *proc,
5525 const char *prefix,
5526 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005527{
Todd Kjos6d241a42017-04-21 14:32:11 -07005528 struct binder_proc *to_proc;
5529 struct binder_buffer *buffer = t->buffer;
5530
Todd Kjos2f993e22017-05-12 14:42:55 -07005531 spin_lock(&t->lock);
Todd Kjos6d241a42017-04-21 14:32:11 -07005532 to_proc = t->to_proc;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005533 seq_printf(m,
Todd Kjosf540ce02018-02-07 13:57:37 -08005534 "%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 -07005535 prefix, t->debug_id, t,
5536 t->from ? t->from->proc->pid : 0,
5537 t->from ? t->from->pid : 0,
Todd Kjos6d241a42017-04-21 14:32:11 -07005538 to_proc ? to_proc->pid : 0,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005539 t->to_thread ? t->to_thread->pid : 0,
Martijn Coenen57b2ac62017-06-06 17:04:42 -07005540 t->code, t->flags, t->priority.sched_policy,
5541 t->priority.prio, t->need_reply);
Todd Kjos2f993e22017-05-12 14:42:55 -07005542 spin_unlock(&t->lock);
5543
Todd Kjos6d241a42017-04-21 14:32:11 -07005544 if (proc != to_proc) {
5545 /*
5546 * Can only safely deref buffer if we are holding the
5547 * correct proc inner lock for this node
5548 */
5549 seq_puts(m, "\n");
5550 return;
5551 }
5552
5553 if (buffer == NULL) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005554 seq_puts(m, " buffer free\n");
5555 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005556 }
Todd Kjos6d241a42017-04-21 14:32:11 -07005557 if (buffer->target_node)
5558 seq_printf(m, " node %d", buffer->target_node->debug_id);
Todd Kjosf540ce02018-02-07 13:57:37 -08005559 seq_printf(m, " size %zd:%zd data %pK\n",
Todd Kjos6d241a42017-04-21 14:32:11 -07005560 buffer->data_size, buffer->offsets_size,
Todd Kjos8539b1e2019-02-08 10:35:20 -08005561 buffer->user_data);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005562}
5563
Todd Kjos6d241a42017-04-21 14:32:11 -07005564static void print_binder_work_ilocked(struct seq_file *m,
5565 struct binder_proc *proc,
5566 const char *prefix,
5567 const char *transaction_prefix,
5568 struct binder_work *w)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005569{
5570 struct binder_node *node;
5571 struct binder_transaction *t;
5572
5573 switch (w->type) {
5574 case BINDER_WORK_TRANSACTION:
5575 t = container_of(w, struct binder_transaction, work);
Todd Kjos6d241a42017-04-21 14:32:11 -07005576 print_binder_transaction_ilocked(
5577 m, proc, transaction_prefix, t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005578 break;
Todd Kjos858b8da2017-04-21 17:35:12 -07005579 case BINDER_WORK_RETURN_ERROR: {
5580 struct binder_error *e = container_of(
5581 w, struct binder_error, work);
5582
5583 seq_printf(m, "%stransaction error: %u\n",
5584 prefix, e->cmd);
5585 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005586 case BINDER_WORK_TRANSACTION_COMPLETE:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005587 seq_printf(m, "%stransaction complete\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005588 break;
5589 case BINDER_WORK_NODE:
5590 node = container_of(w, struct binder_node, work);
Arve Hjønnevågda498892014-02-21 14:40:26 -08005591 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
5592 prefix, node->debug_id,
5593 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005594 break;
5595 case BINDER_WORK_DEAD_BINDER:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005596 seq_printf(m, "%shas dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005597 break;
5598 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005599 seq_printf(m, "%shas cleared dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005600 break;
5601 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005602 seq_printf(m, "%shas cleared death notification\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005603 break;
5604 default:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005605 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005606 break;
5607 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005608}
5609
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005610static void print_binder_thread_ilocked(struct seq_file *m,
5611 struct binder_thread *thread,
5612 int print_always)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005613{
5614 struct binder_transaction *t;
5615 struct binder_work *w;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005616 size_t start_pos = m->count;
5617 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005618
Todd Kjos2f993e22017-05-12 14:42:55 -07005619 seq_printf(m, " thread %d: l %02x need_return %d tr %d\n",
Todd Kjos6798e6d2017-01-06 14:19:25 -08005620 thread->pid, thread->looper,
Todd Kjos2f993e22017-05-12 14:42:55 -07005621 thread->looper_need_return,
5622 atomic_read(&thread->tmp_ref));
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005623 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005624 t = thread->transaction_stack;
5625 while (t) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005626 if (t->from == thread) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005627 print_binder_transaction_ilocked(m, thread->proc,
5628 " outgoing transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005629 t = t->from_parent;
5630 } else if (t->to_thread == thread) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005631 print_binder_transaction_ilocked(m, thread->proc,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005632 " incoming transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005633 t = t->to_parent;
5634 } else {
Todd Kjos6d241a42017-04-21 14:32:11 -07005635 print_binder_transaction_ilocked(m, thread->proc,
5636 " bad transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005637 t = NULL;
5638 }
5639 }
5640 list_for_each_entry(w, &thread->todo, entry) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005641 print_binder_work_ilocked(m, thread->proc, " ",
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005642 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005643 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005644 if (!print_always && m->count == header_pos)
5645 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005646}
5647
Todd Kjos425d23f2017-06-12 12:07:26 -07005648static void print_binder_node_nilocked(struct seq_file *m,
5649 struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005650{
5651 struct binder_ref *ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005652 struct binder_work *w;
5653 int count;
5654
5655 count = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08005656 hlist_for_each_entry(ref, &node->refs, node_entry)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005657 count++;
5658
Martijn Coenen6aac9792017-06-07 09:29:14 -07005659 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 -08005660 node->debug_id, (u64)node->ptr, (u64)node->cookie,
Martijn Coenen6aac9792017-06-07 09:29:14 -07005661 node->sched_policy, node->min_priority,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005662 node->has_strong_ref, node->has_weak_ref,
5663 node->local_strong_refs, node->local_weak_refs,
Todd Kjosf22abc72017-05-09 11:08:05 -07005664 node->internal_strong_refs, count, node->tmp_refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005665 if (count) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005666 seq_puts(m, " proc");
Sasha Levinb67bfe02013-02-27 17:06:00 -08005667 hlist_for_each_entry(ref, &node->refs, node_entry)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005668 seq_printf(m, " %d", ref->proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005669 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005670 seq_puts(m, "\n");
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005671 if (node->proc) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005672 list_for_each_entry(w, &node->async_todo, entry)
Todd Kjos6d241a42017-04-21 14:32:11 -07005673 print_binder_work_ilocked(m, node->proc, " ",
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005674 " pending async transaction", w);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005675 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005676}
5677
Todd Kjos5346bf32016-10-20 16:43:34 -07005678static void print_binder_ref_olocked(struct seq_file *m,
5679 struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005680{
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005681 binder_node_lock(ref->node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07005682 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
5683 ref->data.debug_id, ref->data.desc,
5684 ref->node->proc ? "" : "dead ",
5685 ref->node->debug_id, ref->data.strong,
5686 ref->data.weak, ref->death);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005687 binder_node_unlock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005688}
5689
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005690static void print_binder_proc(struct seq_file *m,
5691 struct binder_proc *proc, int print_all)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005692{
5693 struct binder_work *w;
5694 struct rb_node *n;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005695 size_t start_pos = m->count;
5696 size_t header_pos;
Todd Kjos425d23f2017-06-12 12:07:26 -07005697 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005698
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005699 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005700 seq_printf(m, "context %s\n", proc->context->name);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005701 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005702
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005703 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005704 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005705 print_binder_thread_ilocked(m, rb_entry(n, struct binder_thread,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005706 rb_node), print_all);
Todd Kjos425d23f2017-06-12 12:07:26 -07005707
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005708 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005709 struct binder_node *node = rb_entry(n, struct binder_node,
5710 rb_node);
Todd Kjosd79aa412018-12-05 15:19:26 -08005711 if (!print_all && !node->has_async_transaction)
5712 continue;
5713
Todd Kjos425d23f2017-06-12 12:07:26 -07005714 /*
5715 * take a temporary reference on the node so it
5716 * survives and isn't removed from the tree
5717 * while we print it.
5718 */
5719 binder_inc_node_tmpref_ilocked(node);
5720 /* Need to drop inner lock to take node lock */
5721 binder_inner_proc_unlock(proc);
5722 if (last_node)
5723 binder_put_node(last_node);
5724 binder_node_inner_lock(node);
5725 print_binder_node_nilocked(m, node);
5726 binder_node_inner_unlock(node);
5727 last_node = node;
5728 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005729 }
Todd Kjos425d23f2017-06-12 12:07:26 -07005730 binder_inner_proc_unlock(proc);
5731 if (last_node)
5732 binder_put_node(last_node);
5733
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005734 if (print_all) {
Todd Kjos5346bf32016-10-20 16:43:34 -07005735 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005736 for (n = rb_first(&proc->refs_by_desc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005737 n != NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005738 n = rb_next(n))
Todd Kjos5346bf32016-10-20 16:43:34 -07005739 print_binder_ref_olocked(m, rb_entry(n,
5740 struct binder_ref,
5741 rb_node_desc));
5742 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005743 }
Todd Kjosd325d372016-10-10 10:40:53 -07005744 binder_alloc_print_allocated(m, &proc->alloc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005745 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005746 list_for_each_entry(w, &proc->todo, entry)
Todd Kjos6d241a42017-04-21 14:32:11 -07005747 print_binder_work_ilocked(m, proc, " ",
5748 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005749 list_for_each_entry(w, &proc->delivered_death, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005750 seq_puts(m, " has delivered dead binder\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005751 break;
5752 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005753 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005754 if (!print_all && m->count == header_pos)
5755 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005756}
5757
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005758static const char * const binder_return_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005759 "BR_ERROR",
5760 "BR_OK",
5761 "BR_TRANSACTION",
5762 "BR_REPLY",
5763 "BR_ACQUIRE_RESULT",
5764 "BR_DEAD_REPLY",
5765 "BR_TRANSACTION_COMPLETE",
5766 "BR_INCREFS",
5767 "BR_ACQUIRE",
5768 "BR_RELEASE",
5769 "BR_DECREFS",
5770 "BR_ATTEMPT_ACQUIRE",
5771 "BR_NOOP",
5772 "BR_SPAWN_LOOPER",
5773 "BR_FINISHED",
5774 "BR_DEAD_BINDER",
5775 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
5776 "BR_FAILED_REPLY"
5777};
5778
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005779static const char * const binder_command_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005780 "BC_TRANSACTION",
5781 "BC_REPLY",
5782 "BC_ACQUIRE_RESULT",
5783 "BC_FREE_BUFFER",
5784 "BC_INCREFS",
5785 "BC_ACQUIRE",
5786 "BC_RELEASE",
5787 "BC_DECREFS",
5788 "BC_INCREFS_DONE",
5789 "BC_ACQUIRE_DONE",
5790 "BC_ATTEMPT_ACQUIRE",
5791 "BC_REGISTER_LOOPER",
5792 "BC_ENTER_LOOPER",
5793 "BC_EXIT_LOOPER",
5794 "BC_REQUEST_DEATH_NOTIFICATION",
5795 "BC_CLEAR_DEATH_NOTIFICATION",
Martijn Coenen5a6da532016-09-30 14:10:07 +02005796 "BC_DEAD_BINDER_DONE",
5797 "BC_TRANSACTION_SG",
5798 "BC_REPLY_SG",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005799};
5800
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005801static const char * const binder_objstat_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005802 "proc",
5803 "thread",
5804 "node",
5805 "ref",
5806 "death",
5807 "transaction",
5808 "transaction_complete"
5809};
5810
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005811static void print_binder_stats(struct seq_file *m, const char *prefix,
5812 struct binder_stats *stats)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005813{
5814 int i;
5815
5816 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005817 ARRAY_SIZE(binder_command_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005818 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005819 int temp = atomic_read(&stats->bc[i]);
5820
5821 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005822 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005823 binder_command_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005824 }
5825
5826 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005827 ARRAY_SIZE(binder_return_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005828 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005829 int temp = atomic_read(&stats->br[i]);
5830
5831 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005832 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005833 binder_return_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005834 }
5835
5836 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005837 ARRAY_SIZE(binder_objstat_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005838 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005839 ARRAY_SIZE(stats->obj_deleted));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005840 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005841 int created = atomic_read(&stats->obj_created[i]);
5842 int deleted = atomic_read(&stats->obj_deleted[i]);
5843
5844 if (created || deleted)
5845 seq_printf(m, "%s%s: active %d total %d\n",
5846 prefix,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005847 binder_objstat_strings[i],
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005848 created - deleted,
5849 created);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005850 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005851}
5852
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005853static void print_binder_proc_stats(struct seq_file *m,
5854 struct binder_proc *proc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005855{
5856 struct binder_work *w;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005857 struct binder_thread *thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005858 struct rb_node *n;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005859 int count, strong, weak, ready_threads;
Todd Kjosb4827902017-05-25 15:52:17 -07005860 size_t free_async_space =
5861 binder_alloc_get_free_async_space(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005862
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005863 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005864 seq_printf(m, "context %s\n", proc->context->name);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005865 count = 0;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005866 ready_threads = 0;
Todd Kjosb4827902017-05-25 15:52:17 -07005867 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005868 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
5869 count++;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005870
5871 list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node)
5872 ready_threads++;
5873
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005874 seq_printf(m, " threads: %d\n", count);
5875 seq_printf(m, " requested threads: %d+%d/%d\n"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005876 " ready threads %d\n"
5877 " free async space %zd\n", proc->requested_threads,
5878 proc->requested_threads_started, proc->max_threads,
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005879 ready_threads,
Todd Kjosb4827902017-05-25 15:52:17 -07005880 free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005881 count = 0;
5882 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
5883 count++;
Todd Kjos425d23f2017-06-12 12:07:26 -07005884 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005885 seq_printf(m, " nodes: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005886 count = 0;
5887 strong = 0;
5888 weak = 0;
Todd Kjos5346bf32016-10-20 16:43:34 -07005889 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005890 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
5891 struct binder_ref *ref = rb_entry(n, struct binder_ref,
5892 rb_node_desc);
5893 count++;
Todd Kjosb0117bb2017-05-08 09:16:27 -07005894 strong += ref->data.strong;
5895 weak += ref->data.weak;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005896 }
Todd Kjos5346bf32016-10-20 16:43:34 -07005897 binder_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005898 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005899
Todd Kjosd325d372016-10-10 10:40:53 -07005900 count = binder_alloc_get_allocated_count(&proc->alloc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005901 seq_printf(m, " buffers: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005902
Sherry Yang91004422017-08-22 17:26:57 -07005903 binder_alloc_print_pages(m, &proc->alloc);
5904
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005905 count = 0;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005906 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005907 list_for_each_entry(w, &proc->todo, entry) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005908 if (w->type == BINDER_WORK_TRANSACTION)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005909 count++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005910 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005911 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005912 seq_printf(m, " pending transactions: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005913
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005914 print_binder_stats(m, " ", &proc->stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005915}
5916
5917
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005918static int binder_state_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005919{
5920 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005921 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005922 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005923
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005924 seq_puts(m, "binder state:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005925
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005926 spin_lock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005927 if (!hlist_empty(&binder_dead_nodes))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005928 seq_puts(m, "dead nodes:\n");
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005929 hlist_for_each_entry(node, &binder_dead_nodes, dead_node) {
5930 /*
5931 * take a temporary reference on the node so it
5932 * survives and isn't removed from the list
5933 * while we print it.
5934 */
5935 node->tmp_refs++;
5936 spin_unlock(&binder_dead_nodes_lock);
5937 if (last_node)
5938 binder_put_node(last_node);
5939 binder_node_lock(node);
Todd Kjos425d23f2017-06-12 12:07:26 -07005940 print_binder_node_nilocked(m, node);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005941 binder_node_unlock(node);
5942 last_node = node;
5943 spin_lock(&binder_dead_nodes_lock);
5944 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005945 spin_unlock(&binder_dead_nodes_lock);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005946 if (last_node)
5947 binder_put_node(last_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005948
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005949 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005950 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005951 print_binder_proc(m, proc, 1);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005952 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005953
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005954 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005955}
5956
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005957static int binder_stats_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005958{
5959 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005960
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005961 seq_puts(m, "binder stats:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005962
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005963 print_binder_stats(m, "", &binder_stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005964
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005965 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005966 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005967 print_binder_proc_stats(m, proc);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005968 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005969
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005970 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005971}
5972
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005973static int binder_transactions_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005974{
5975 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005976
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005977 seq_puts(m, "binder transactions:\n");
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005978 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005979 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005980 print_binder_proc(m, proc, 0);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005981 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005982
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 int binder_proc_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005987{
Riley Andrews83050a42016-02-09 21:05:33 -08005988 struct binder_proc *itr;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005989 int pid = (unsigned long)m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005990
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005991 mutex_lock(&binder_procs_lock);
Riley Andrews83050a42016-02-09 21:05:33 -08005992 hlist_for_each_entry(itr, &binder_procs, proc_node) {
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005993 if (itr->pid == pid) {
5994 seq_puts(m, "binder proc state:\n");
5995 print_binder_proc(m, itr, 1);
Riley Andrews83050a42016-02-09 21:05:33 -08005996 }
5997 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005998 mutex_unlock(&binder_procs_lock);
5999
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006000 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006001}
6002
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006003static void print_binder_transaction_log_entry(struct seq_file *m,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006004 struct binder_transaction_log_entry *e)
6005{
Todd Kjos1cfe6272017-05-24 13:33:28 -07006006 int debug_id = READ_ONCE(e->debug_id_done);
6007 /*
6008 * read barrier to guarantee debug_id_done read before
6009 * we print the log values
6010 */
6011 smp_rmb();
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006012 seq_printf(m,
Todd Kjos1cfe6272017-05-24 13:33:28 -07006013 "%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 -07006014 e->debug_id, (e->call_type == 2) ? "reply" :
6015 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02006016 e->from_thread, e->to_proc, e->to_thread, e->context_name,
Todd Kjose598d172017-03-22 17:19:52 -07006017 e->to_node, e->target_handle, e->data_size, e->offsets_size,
6018 e->return_error, e->return_error_param,
6019 e->return_error_line);
Todd Kjos1cfe6272017-05-24 13:33:28 -07006020 /*
6021 * read-barrier to guarantee read of debug_id_done after
6022 * done printing the fields of the entry
6023 */
6024 smp_rmb();
6025 seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ?
6026 "\n" : " (incomplete)\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006027}
6028
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006029static int binder_transaction_log_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006030{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006031 struct binder_transaction_log *log = m->private;
Todd Kjos1cfe6272017-05-24 13:33:28 -07006032 unsigned int log_cur = atomic_read(&log->cur);
6033 unsigned int count;
6034 unsigned int cur;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006035 int i;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006036
Todd Kjos1cfe6272017-05-24 13:33:28 -07006037 count = log_cur + 1;
6038 cur = count < ARRAY_SIZE(log->entry) && !log->full ?
6039 0 : count % ARRAY_SIZE(log->entry);
6040 if (count > ARRAY_SIZE(log->entry) || log->full)
6041 count = ARRAY_SIZE(log->entry);
6042 for (i = 0; i < count; i++) {
6043 unsigned int index = cur++ % ARRAY_SIZE(log->entry);
6044
6045 print_binder_transaction_log_entry(m, &log->entry[index]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006046 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006047 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006048}
6049
6050static const struct file_operations binder_fops = {
6051 .owner = THIS_MODULE,
6052 .poll = binder_poll,
6053 .unlocked_ioctl = binder_ioctl,
Arve Hjønnevågda498892014-02-21 14:40:26 -08006054 .compat_ioctl = binder_ioctl,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006055 .mmap = binder_mmap,
6056 .open = binder_open,
6057 .flush = binder_flush,
6058 .release = binder_release,
6059};
6060
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006061BINDER_DEBUG_ENTRY(state);
6062BINDER_DEBUG_ENTRY(stats);
6063BINDER_DEBUG_ENTRY(transactions);
6064BINDER_DEBUG_ENTRY(transaction_log);
6065
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006066static int __init init_binder_device(const char *name)
6067{
6068 int ret;
6069 struct binder_device *binder_device;
6070
6071 binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
6072 if (!binder_device)
6073 return -ENOMEM;
6074
6075 binder_device->miscdev.fops = &binder_fops;
6076 binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
6077 binder_device->miscdev.name = name;
6078
6079 binder_device->context.binder_context_mgr_uid = INVALID_UID;
6080 binder_device->context.name = name;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07006081 mutex_init(&binder_device->context.context_mgr_node_lock);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006082
6083 ret = misc_register(&binder_device->miscdev);
6084 if (ret < 0) {
6085 kfree(binder_device);
6086 return ret;
6087 }
6088
6089 hlist_add_head(&binder_device->hlist, &binder_devices);
6090
6091 return ret;
6092}
6093
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006094static int __init binder_init(void)
6095{
6096 int ret;
Christian Brauner558ee932017-08-21 16:13:28 +02006097 char *device_name, *device_names, *device_tmp;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006098 struct binder_device *device;
6099 struct hlist_node *tmp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006100
Tetsuo Handaf8cb8222017-11-29 22:29:47 +09006101 ret = binder_alloc_shrinker_init();
6102 if (ret)
6103 return ret;
Sherry Yang5828d702017-07-29 13:24:11 -07006104
Todd Kjos1cfe6272017-05-24 13:33:28 -07006105 atomic_set(&binder_transaction_log.cur, ~0U);
6106 atomic_set(&binder_transaction_log_failed.cur, ~0U);
6107
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006108 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
6109 if (binder_debugfs_dir_entry_root)
6110 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
6111 binder_debugfs_dir_entry_root);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006112
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006113 if (binder_debugfs_dir_entry_root) {
6114 debugfs_create_file("state",
Harsh Shandilya174562a2017-12-22 19:37:02 +05306115 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006116 binder_debugfs_dir_entry_root,
6117 NULL,
6118 &binder_state_fops);
6119 debugfs_create_file("stats",
Harsh Shandilya174562a2017-12-22 19:37:02 +05306120 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006121 binder_debugfs_dir_entry_root,
6122 NULL,
6123 &binder_stats_fops);
6124 debugfs_create_file("transactions",
Harsh Shandilya174562a2017-12-22 19:37:02 +05306125 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006126 binder_debugfs_dir_entry_root,
6127 NULL,
6128 &binder_transactions_fops);
6129 debugfs_create_file("transaction_log",
Harsh Shandilya174562a2017-12-22 19:37:02 +05306130 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006131 binder_debugfs_dir_entry_root,
6132 &binder_transaction_log,
6133 &binder_transaction_log_fops);
6134 debugfs_create_file("failed_transaction_log",
Harsh Shandilya174562a2017-12-22 19:37:02 +05306135 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006136 binder_debugfs_dir_entry_root,
6137 &binder_transaction_log_failed,
6138 &binder_transaction_log_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006139 }
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006140
6141 /*
6142 * Copy the module_parameter string, because we don't want to
6143 * tokenize it in-place.
6144 */
6145 device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL);
6146 if (!device_names) {
6147 ret = -ENOMEM;
6148 goto err_alloc_device_names_failed;
6149 }
6150 strcpy(device_names, binder_devices_param);
6151
Christian Brauner558ee932017-08-21 16:13:28 +02006152 device_tmp = device_names;
6153 while ((device_name = strsep(&device_tmp, ","))) {
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006154 ret = init_binder_device(device_name);
6155 if (ret)
6156 goto err_init_binder_device_failed;
6157 }
6158
6159 return ret;
6160
6161err_init_binder_device_failed:
6162 hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
6163 misc_deregister(&device->miscdev);
6164 hlist_del(&device->hlist);
6165 kfree(device);
6166 }
Christian Brauner558ee932017-08-21 16:13:28 +02006167
6168 kfree(device_names);
6169
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006170err_alloc_device_names_failed:
6171 debugfs_remove_recursive(binder_debugfs_dir_entry_root);
6172
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006173 return ret;
6174}
6175
6176device_initcall(binder_init);
6177
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07006178#define CREATE_TRACE_POINTS
6179#include "binder_trace.h"
6180
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006181MODULE_LICENSE("GPL v2");