blob: 8898a2298e54ed03cce965b339ee9f97625ced56 [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 Kjosb4827902017-05-25 15:52:17 -0700530 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700531 * @default_priority: default scheduler priority
532 * (invariant after initialized)
533 * @debugfs_entry: debugfs node
534 * @alloc: binder allocator bookkeeping
535 * @context: binder_context for this proc
536 * (invariant after initialized)
537 * @inner_lock: can nest under outer_lock and/or node lock
538 * @outer_lock: no nesting under innor or node lock
539 * Lock order: 1) outer, 2) node, 3) inner
540 *
541 * Bookkeeping structure for binder processes
542 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900543struct binder_proc {
544 struct hlist_node proc_node;
545 struct rb_root threads;
546 struct rb_root nodes;
547 struct rb_root refs_by_desc;
548 struct rb_root refs_by_node;
Martijn Coenen22d64e4322017-06-02 11:15:44 -0700549 struct list_head waiting_threads;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900550 int pid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900551 struct task_struct *tsk;
Martijn Coenen6f7e5f92018-06-15 11:53:36 +0200552 struct files_struct *files;
Todd Kjosfbb43392017-11-27 09:32:33 -0800553 struct mutex files_lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900554 struct hlist_node deferred_work_node;
555 int deferred_work;
Todd Kjos2f993e22017-05-12 14:42:55 -0700556 bool is_dead;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900557
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900558 struct list_head todo;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900559 struct binder_stats stats;
560 struct list_head delivered_death;
561 int max_threads;
562 int requested_threads;
563 int requested_threads_started;
Todd Kjos2f993e22017-05-12 14:42:55 -0700564 int tmp_ref;
Martijn Coenen57b2ac62017-06-06 17:04:42 -0700565 struct binder_priority default_priority;
Arve Hjønnevåg16b66552009-04-28 20:57:50 -0700566 struct dentry *debugfs_entry;
Todd Kjosf85d2292016-10-10 10:39:59 -0700567 struct binder_alloc alloc;
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200568 struct binder_context *context;
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700569 spinlock_t inner_lock;
570 spinlock_t outer_lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900571};
572
573enum {
574 BINDER_LOOPER_STATE_REGISTERED = 0x01,
575 BINDER_LOOPER_STATE_ENTERED = 0x02,
576 BINDER_LOOPER_STATE_EXITED = 0x04,
577 BINDER_LOOPER_STATE_INVALID = 0x08,
578 BINDER_LOOPER_STATE_WAITING = 0x10,
Martijn Coenen22d64e4322017-06-02 11:15:44 -0700579 BINDER_LOOPER_STATE_POLL = 0x20,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900580};
581
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700582/**
583 * struct binder_thread - binder thread bookkeeping
584 * @proc: binder process for this thread
585 * (invariant after initialization)
586 * @rb_node: element for proc->threads rbtree
Todd Kjosb4827902017-05-25 15:52:17 -0700587 * (protected by @proc->inner_lock)
Martijn Coenen22d64e4322017-06-02 11:15:44 -0700588 * @waiting_thread_node: element for @proc->waiting_threads list
589 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700590 * @pid: PID for this thread
591 * (invariant after initialization)
592 * @looper: bitmap of looping state
593 * (only accessed by this thread)
594 * @looper_needs_return: looping thread needs to exit driver
595 * (no lock needed)
596 * @transaction_stack: stack of in-progress transactions for this thread
Martijn Coenen995a36e2017-06-02 13:36:52 -0700597 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700598 * @todo: list of work to do for this thread
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700599 * (protected by @proc->inner_lock)
Martijn Coenen1af61802017-10-19 15:04:46 +0200600 * @process_todo: whether work in @todo should be processed
601 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700602 * @return_error: transaction errors reported by this thread
603 * (only accessed by this thread)
604 * @reply_error: transaction errors reported by target thread
Martijn Coenen995a36e2017-06-02 13:36:52 -0700605 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700606 * @wait: wait queue for thread work
607 * @stats: per-thread statistics
608 * (atomics, no lock needed)
609 * @tmp_ref: temporary reference to indicate thread is in use
610 * (atomic since @proc->inner_lock cannot
611 * always be acquired)
612 * @is_dead: thread is dead and awaiting free
613 * when outstanding transactions are cleaned up
Todd Kjosb4827902017-05-25 15:52:17 -0700614 * (protected by @proc->inner_lock)
Martijn Coenen07a30fe2017-06-07 10:02:12 -0700615 * @task: struct task_struct for this thread
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700616 *
617 * Bookkeeping structure for binder threads.
618 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900619struct binder_thread {
620 struct binder_proc *proc;
621 struct rb_node rb_node;
Martijn Coenen22d64e4322017-06-02 11:15:44 -0700622 struct list_head waiting_thread_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900623 int pid;
Todd Kjos6798e6d2017-01-06 14:19:25 -0800624 int looper; /* only modified by this thread */
625 bool looper_need_return; /* can be written by other thread */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900626 struct binder_transaction *transaction_stack;
627 struct list_head todo;
Martijn Coenen1af61802017-10-19 15:04:46 +0200628 bool process_todo;
Todd Kjos858b8da2017-04-21 17:35:12 -0700629 struct binder_error return_error;
630 struct binder_error reply_error;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900631 wait_queue_head_t wait;
632 struct binder_stats stats;
Todd Kjos2f993e22017-05-12 14:42:55 -0700633 atomic_t tmp_ref;
634 bool is_dead;
Martijn Coenen07a30fe2017-06-07 10:02:12 -0700635 struct task_struct *task;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900636};
637
638struct binder_transaction {
639 int debug_id;
640 struct binder_work work;
641 struct binder_thread *from;
642 struct binder_transaction *from_parent;
643 struct binder_proc *to_proc;
644 struct binder_thread *to_thread;
645 struct binder_transaction *to_parent;
646 unsigned need_reply:1;
647 /* unsigned is_dead:1; */ /* not used at the moment */
648
649 struct binder_buffer *buffer;
650 unsigned int code;
651 unsigned int flags;
Martijn Coenen57b2ac62017-06-06 17:04:42 -0700652 struct binder_priority priority;
653 struct binder_priority saved_priority;
Martijn Coenen07a30fe2017-06-07 10:02:12 -0700654 bool set_priority_called;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -0600655 kuid_t sender_euid;
Todd Kjos63e0afa2019-01-14 09:10:21 -0800656 binder_uintptr_t security_ctx;
Todd Kjos2f993e22017-05-12 14:42:55 -0700657 /**
658 * @lock: protects @from, @to_proc, and @to_thread
659 *
660 * @from, @to_proc, and @to_thread can be set to NULL
661 * during thread teardown
662 */
663 spinlock_t lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900664};
665
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700666/**
Todd Kjosd73356a2019-02-08 10:35:16 -0800667 * struct binder_object - union of flat binder object types
668 * @hdr: generic object header
669 * @fbo: binder object (nodes and refs)
670 * @fdo: file descriptor object
671 * @bbo: binder buffer pointer
672 * @fdao: file descriptor array
673 *
674 * Used for type-independent object copies
675 */
676struct binder_object {
677 union {
678 struct binder_object_header hdr;
679 struct flat_binder_object fbo;
680 struct binder_fd_object fdo;
681 struct binder_buffer_object bbo;
682 struct binder_fd_array_object fdao;
683 };
684};
685
686/**
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700687 * binder_proc_lock() - Acquire outer lock for given binder_proc
688 * @proc: struct binder_proc to acquire
689 *
690 * Acquires proc->outer_lock. Used to protect binder_ref
691 * structures associated with the given proc.
692 */
693#define binder_proc_lock(proc) _binder_proc_lock(proc, __LINE__)
694static void
695_binder_proc_lock(struct binder_proc *proc, int line)
696{
697 binder_debug(BINDER_DEBUG_SPINLOCKS,
698 "%s: line=%d\n", __func__, line);
699 spin_lock(&proc->outer_lock);
700}
701
702/**
703 * binder_proc_unlock() - Release spinlock for given binder_proc
704 * @proc: struct binder_proc to acquire
705 *
706 * Release lock acquired via binder_proc_lock()
707 */
708#define binder_proc_unlock(_proc) _binder_proc_unlock(_proc, __LINE__)
709static void
710_binder_proc_unlock(struct binder_proc *proc, int line)
711{
712 binder_debug(BINDER_DEBUG_SPINLOCKS,
713 "%s: line=%d\n", __func__, line);
714 spin_unlock(&proc->outer_lock);
715}
716
717/**
718 * binder_inner_proc_lock() - Acquire inner lock for given binder_proc
719 * @proc: struct binder_proc to acquire
720 *
721 * Acquires proc->inner_lock. Used to protect todo lists
722 */
723#define binder_inner_proc_lock(proc) _binder_inner_proc_lock(proc, __LINE__)
724static void
725_binder_inner_proc_lock(struct binder_proc *proc, int line)
726{
727 binder_debug(BINDER_DEBUG_SPINLOCKS,
728 "%s: line=%d\n", __func__, line);
729 spin_lock(&proc->inner_lock);
730}
731
732/**
733 * binder_inner_proc_unlock() - Release inner lock for given binder_proc
734 * @proc: struct binder_proc to acquire
735 *
736 * Release lock acquired via binder_inner_proc_lock()
737 */
738#define binder_inner_proc_unlock(proc) _binder_inner_proc_unlock(proc, __LINE__)
739static void
740_binder_inner_proc_unlock(struct binder_proc *proc, int line)
741{
742 binder_debug(BINDER_DEBUG_SPINLOCKS,
743 "%s: line=%d\n", __func__, line);
744 spin_unlock(&proc->inner_lock);
745}
746
747/**
748 * binder_node_lock() - Acquire spinlock for given binder_node
749 * @node: struct binder_node to acquire
750 *
751 * Acquires node->lock. Used to protect binder_node fields
752 */
753#define binder_node_lock(node) _binder_node_lock(node, __LINE__)
754static void
755_binder_node_lock(struct binder_node *node, int line)
756{
757 binder_debug(BINDER_DEBUG_SPINLOCKS,
758 "%s: line=%d\n", __func__, line);
759 spin_lock(&node->lock);
760}
761
762/**
763 * binder_node_unlock() - Release spinlock for given binder_proc
764 * @node: struct binder_node to acquire
765 *
766 * Release lock acquired via binder_node_lock()
767 */
768#define binder_node_unlock(node) _binder_node_unlock(node, __LINE__)
769static void
770_binder_node_unlock(struct binder_node *node, int line)
771{
772 binder_debug(BINDER_DEBUG_SPINLOCKS,
773 "%s: line=%d\n", __func__, line);
774 spin_unlock(&node->lock);
775}
776
Todd Kjoscbcbbd62017-06-08 13:45:59 -0700777/**
778 * binder_node_inner_lock() - Acquire node and inner locks
779 * @node: struct binder_node to acquire
780 *
781 * Acquires node->lock. If node->proc also acquires
782 * proc->inner_lock. Used to protect binder_node fields
783 */
784#define binder_node_inner_lock(node) _binder_node_inner_lock(node, __LINE__)
785static void
786_binder_node_inner_lock(struct binder_node *node, int line)
787{
788 binder_debug(BINDER_DEBUG_SPINLOCKS,
789 "%s: line=%d\n", __func__, line);
790 spin_lock(&node->lock);
791 if (node->proc)
792 binder_inner_proc_lock(node->proc);
793}
794
795/**
796 * binder_node_unlock() - Release node and inner locks
797 * @node: struct binder_node to acquire
798 *
799 * Release lock acquired via binder_node_lock()
800 */
801#define binder_node_inner_unlock(node) _binder_node_inner_unlock(node, __LINE__)
802static void
803_binder_node_inner_unlock(struct binder_node *node, int line)
804{
805 struct binder_proc *proc = node->proc;
806
807 binder_debug(BINDER_DEBUG_SPINLOCKS,
808 "%s: line=%d\n", __func__, line);
809 if (proc)
810 binder_inner_proc_unlock(proc);
811 spin_unlock(&node->lock);
812}
813
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700814static bool binder_worklist_empty_ilocked(struct list_head *list)
815{
816 return list_empty(list);
817}
818
819/**
820 * binder_worklist_empty() - Check if no items on the work list
821 * @proc: binder_proc associated with list
822 * @list: list to check
823 *
824 * Return: true if there are no items on list, else false
825 */
826static bool binder_worklist_empty(struct binder_proc *proc,
827 struct list_head *list)
828{
829 bool ret;
830
831 binder_inner_proc_lock(proc);
832 ret = binder_worklist_empty_ilocked(list);
833 binder_inner_proc_unlock(proc);
834 return ret;
835}
836
Martijn Coenen1af61802017-10-19 15:04:46 +0200837/**
838 * binder_enqueue_work_ilocked() - Add an item to the work list
839 * @work: struct binder_work to add to list
840 * @target_list: list to add work to
841 *
842 * Adds the work to the specified list. Asserts that work
843 * is not already on a list.
844 *
845 * Requires the proc->inner_lock to be held.
846 */
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700847static void
848binder_enqueue_work_ilocked(struct binder_work *work,
849 struct list_head *target_list)
850{
851 BUG_ON(target_list == NULL);
852 BUG_ON(work->entry.next && !list_empty(&work->entry));
853 list_add_tail(&work->entry, target_list);
854}
855
856/**
Martijn Coenendac2e9c2017-11-13 09:55:21 +0100857 * binder_enqueue_deferred_thread_work_ilocked() - Add deferred thread work
Martijn Coenen1af61802017-10-19 15:04:46 +0200858 * @thread: thread to queue work to
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700859 * @work: struct binder_work to add to list
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700860 *
Martijn Coenen1af61802017-10-19 15:04:46 +0200861 * Adds the work to the todo list of the thread. Doesn't set the process_todo
862 * flag, which means that (if it wasn't already set) the thread will go to
863 * sleep without handling this work when it calls read.
864 *
865 * Requires the proc->inner_lock to be held.
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700866 */
867static void
Martijn Coenendac2e9c2017-11-13 09:55:21 +0100868binder_enqueue_deferred_thread_work_ilocked(struct binder_thread *thread,
869 struct binder_work *work)
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700870{
Martijn Coenen1af61802017-10-19 15:04:46 +0200871 binder_enqueue_work_ilocked(work, &thread->todo);
872}
873
874/**
875 * binder_enqueue_thread_work_ilocked() - Add an item to the thread work list
876 * @thread: thread to queue work to
877 * @work: struct binder_work to add to list
878 *
879 * Adds the work to the todo list of the thread, and enables processing
880 * of the todo queue.
881 *
882 * Requires the proc->inner_lock to be held.
883 */
884static void
885binder_enqueue_thread_work_ilocked(struct binder_thread *thread,
886 struct binder_work *work)
887{
888 binder_enqueue_work_ilocked(work, &thread->todo);
889 thread->process_todo = true;
890}
891
892/**
893 * binder_enqueue_thread_work() - Add an item to the thread work list
894 * @thread: thread to queue work to
895 * @work: struct binder_work to add to list
896 *
897 * Adds the work to the todo list of the thread, and enables processing
898 * of the todo queue.
899 */
900static void
901binder_enqueue_thread_work(struct binder_thread *thread,
902 struct binder_work *work)
903{
904 binder_inner_proc_lock(thread->proc);
905 binder_enqueue_thread_work_ilocked(thread, work);
906 binder_inner_proc_unlock(thread->proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700907}
908
909static void
910binder_dequeue_work_ilocked(struct binder_work *work)
911{
912 list_del_init(&work->entry);
913}
914
915/**
916 * binder_dequeue_work() - Removes an item from the work list
917 * @proc: binder_proc associated with list
918 * @work: struct binder_work to remove from list
919 *
920 * Removes the specified work item from whatever list it is on.
921 * Can safely be called if work is not on any list.
922 */
923static void
924binder_dequeue_work(struct binder_proc *proc, struct binder_work *work)
925{
926 binder_inner_proc_lock(proc);
927 binder_dequeue_work_ilocked(work);
928 binder_inner_proc_unlock(proc);
929}
930
931static struct binder_work *binder_dequeue_work_head_ilocked(
932 struct list_head *list)
933{
934 struct binder_work *w;
935
936 w = list_first_entry_or_null(list, struct binder_work, entry);
937 if (w)
938 list_del_init(&w->entry);
939 return w;
940}
941
942/**
943 * binder_dequeue_work_head() - Dequeues the item at head of list
944 * @proc: binder_proc associated with list
945 * @list: list to dequeue head
946 *
947 * Removes the head of the list if there are items on the list
948 *
949 * Return: pointer dequeued binder_work, NULL if list was empty
950 */
951static struct binder_work *binder_dequeue_work_head(
952 struct binder_proc *proc,
953 struct list_head *list)
954{
955 struct binder_work *w;
956
957 binder_inner_proc_lock(proc);
958 w = binder_dequeue_work_head_ilocked(list);
959 binder_inner_proc_unlock(proc);
960 return w;
961}
962
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900963static void
964binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
Todd Kjos2f993e22017-05-12 14:42:55 -0700965static void binder_free_thread(struct binder_thread *thread);
966static void binder_free_proc(struct binder_proc *proc);
Todd Kjos425d23f2017-06-12 12:07:26 -0700967static void binder_inc_node_tmpref_ilocked(struct binder_node *node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900968
Sachin Kamatefde99c2012-08-17 16:39:36 +0530969static int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900970{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900971 unsigned long rlim_cur;
972 unsigned long irqs;
Todd Kjosfbb43392017-11-27 09:32:33 -0800973 int ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900974
Todd Kjosfbb43392017-11-27 09:32:33 -0800975 mutex_lock(&proc->files_lock);
976 if (proc->files == NULL) {
977 ret = -ESRCH;
978 goto err;
979 }
980 if (!lock_task_sighand(proc->tsk, &irqs)) {
981 ret = -EMFILE;
982 goto err;
983 }
Al Virodcfadfa2012-08-12 17:27:30 -0400984 rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
985 unlock_task_sighand(proc->tsk, &irqs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900986
Todd Kjosfbb43392017-11-27 09:32:33 -0800987 ret = __alloc_fd(proc->files, 0, rlim_cur, flags);
988err:
989 mutex_unlock(&proc->files_lock);
990 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900991}
992
993/*
994 * copied from fd_install
995 */
996static void task_fd_install(
997 struct binder_proc *proc, unsigned int fd, struct file *file)
998{
Todd Kjosfbb43392017-11-27 09:32:33 -0800999 mutex_lock(&proc->files_lock);
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02001000 if (proc->files)
1001 __fd_install(proc->files, fd, file);
Todd Kjosfbb43392017-11-27 09:32:33 -08001002 mutex_unlock(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001003}
1004
1005/*
1006 * copied from sys_close
1007 */
1008static long task_close_fd(struct binder_proc *proc, unsigned int fd)
1009{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001010 int retval;
1011
Todd Kjosfbb43392017-11-27 09:32:33 -08001012 mutex_lock(&proc->files_lock);
1013 if (proc->files == NULL) {
1014 retval = -ESRCH;
1015 goto err;
1016 }
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02001017 retval = __close_fd(proc->files, fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001018 /* can't restart close syscall because file table entry was cleared */
1019 if (unlikely(retval == -ERESTARTSYS ||
1020 retval == -ERESTARTNOINTR ||
1021 retval == -ERESTARTNOHAND ||
1022 retval == -ERESTART_RESTARTBLOCK))
1023 retval = -EINTR;
Todd Kjosfbb43392017-11-27 09:32:33 -08001024err:
1025 mutex_unlock(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001026 return retval;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001027}
1028
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001029static bool binder_has_work_ilocked(struct binder_thread *thread,
1030 bool do_proc_work)
1031{
Martijn Coenen1af61802017-10-19 15:04:46 +02001032 return thread->process_todo ||
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001033 thread->looper_need_return ||
1034 (do_proc_work &&
1035 !binder_worklist_empty_ilocked(&thread->proc->todo));
1036}
1037
1038static bool binder_has_work(struct binder_thread *thread, bool do_proc_work)
1039{
1040 bool has_work;
1041
1042 binder_inner_proc_lock(thread->proc);
1043 has_work = binder_has_work_ilocked(thread, do_proc_work);
1044 binder_inner_proc_unlock(thread->proc);
1045
1046 return has_work;
1047}
1048
1049static bool binder_available_for_proc_work_ilocked(struct binder_thread *thread)
1050{
1051 return !thread->transaction_stack &&
1052 binder_worklist_empty_ilocked(&thread->todo) &&
1053 (thread->looper & (BINDER_LOOPER_STATE_ENTERED |
1054 BINDER_LOOPER_STATE_REGISTERED));
1055}
1056
1057static void binder_wakeup_poll_threads_ilocked(struct binder_proc *proc,
1058 bool sync)
1059{
1060 struct rb_node *n;
1061 struct binder_thread *thread;
1062
1063 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
1064 thread = rb_entry(n, struct binder_thread, rb_node);
1065 if (thread->looper & BINDER_LOOPER_STATE_POLL &&
1066 binder_available_for_proc_work_ilocked(thread)) {
1067 if (sync)
1068 wake_up_interruptible_sync(&thread->wait);
1069 else
1070 wake_up_interruptible(&thread->wait);
1071 }
1072 }
1073}
1074
Martijn Coenen053be422017-06-06 15:17:46 -07001075/**
1076 * binder_select_thread_ilocked() - selects a thread for doing proc work.
1077 * @proc: process to select a thread from
1078 *
1079 * Note that calling this function moves the thread off the waiting_threads
1080 * list, so it can only be woken up by the caller of this function, or a
1081 * signal. Therefore, callers *should* always wake up the thread this function
1082 * returns.
1083 *
1084 * Return: If there's a thread currently waiting for process work,
1085 * returns that thread. Otherwise returns NULL.
1086 */
1087static struct binder_thread *
1088binder_select_thread_ilocked(struct binder_proc *proc)
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001089{
1090 struct binder_thread *thread;
1091
Martijn Coenened323352017-07-27 23:52:24 +02001092 assert_spin_locked(&proc->inner_lock);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001093 thread = list_first_entry_or_null(&proc->waiting_threads,
1094 struct binder_thread,
1095 waiting_thread_node);
1096
Martijn Coenen053be422017-06-06 15:17:46 -07001097 if (thread)
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001098 list_del_init(&thread->waiting_thread_node);
Martijn Coenen053be422017-06-06 15:17:46 -07001099
1100 return thread;
1101}
1102
1103/**
1104 * binder_wakeup_thread_ilocked() - wakes up a thread for doing proc work.
1105 * @proc: process to wake up a thread in
1106 * @thread: specific thread to wake-up (may be NULL)
1107 * @sync: whether to do a synchronous wake-up
1108 *
1109 * This function wakes up a thread in the @proc process.
1110 * The caller may provide a specific thread to wake-up in
1111 * the @thread parameter. If @thread is NULL, this function
1112 * will wake up threads that have called poll().
1113 *
1114 * Note that for this function to work as expected, callers
1115 * should first call binder_select_thread() to find a thread
1116 * to handle the work (if they don't have a thread already),
1117 * and pass the result into the @thread parameter.
1118 */
1119static void binder_wakeup_thread_ilocked(struct binder_proc *proc,
1120 struct binder_thread *thread,
1121 bool sync)
1122{
Martijn Coenened323352017-07-27 23:52:24 +02001123 assert_spin_locked(&proc->inner_lock);
Martijn Coenen053be422017-06-06 15:17:46 -07001124
1125 if (thread) {
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001126 if (sync)
1127 wake_up_interruptible_sync(&thread->wait);
1128 else
1129 wake_up_interruptible(&thread->wait);
1130 return;
1131 }
1132
1133 /* Didn't find a thread waiting for proc work; this can happen
1134 * in two scenarios:
1135 * 1. All threads are busy handling transactions
1136 * In that case, one of those threads should call back into
1137 * the kernel driver soon and pick up this work.
1138 * 2. Threads are using the (e)poll interface, in which case
1139 * they may be blocked on the waitqueue without having been
1140 * added to waiting_threads. For this case, we just iterate
1141 * over all threads not handling transaction work, and
1142 * wake them all up. We wake all because we don't know whether
1143 * a thread that called into (e)poll is handling non-binder
1144 * work currently.
1145 */
1146 binder_wakeup_poll_threads_ilocked(proc, sync);
1147}
1148
Martijn Coenen053be422017-06-06 15:17:46 -07001149static void binder_wakeup_proc_ilocked(struct binder_proc *proc)
1150{
1151 struct binder_thread *thread = binder_select_thread_ilocked(proc);
1152
1153 binder_wakeup_thread_ilocked(proc, thread, /* sync = */false);
1154}
1155
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001156static bool is_rt_policy(int policy)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001157{
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001158 return policy == SCHED_FIFO || policy == SCHED_RR;
1159}
Seunghun Lee10f62862014-05-01 01:30:23 +09001160
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001161static bool is_fair_policy(int policy)
1162{
1163 return policy == SCHED_NORMAL || policy == SCHED_BATCH;
1164}
1165
1166static bool binder_supported_policy(int policy)
1167{
1168 return is_fair_policy(policy) || is_rt_policy(policy);
1169}
1170
1171static int to_userspace_prio(int policy, int kernel_priority)
1172{
1173 if (is_fair_policy(policy))
1174 return PRIO_TO_NICE(kernel_priority);
1175 else
1176 return MAX_USER_RT_PRIO - 1 - kernel_priority;
1177}
1178
1179static int to_kernel_prio(int policy, int user_priority)
1180{
1181 if (is_fair_policy(policy))
1182 return NICE_TO_PRIO(user_priority);
1183 else
1184 return MAX_USER_RT_PRIO - 1 - user_priority;
1185}
1186
Martijn Coenenecd972d2017-05-26 10:48:56 -07001187static void binder_do_set_priority(struct task_struct *task,
1188 struct binder_priority desired,
1189 bool verify)
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001190{
1191 int priority; /* user-space prio value */
1192 bool has_cap_nice;
1193 unsigned int policy = desired.sched_policy;
1194
1195 if (task->policy == policy && task->normal_prio == desired.prio)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001196 return;
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001197
1198 has_cap_nice = has_capability_noaudit(task, CAP_SYS_NICE);
1199
1200 priority = to_userspace_prio(policy, desired.prio);
1201
Martijn Coenenecd972d2017-05-26 10:48:56 -07001202 if (verify && is_rt_policy(policy) && !has_cap_nice) {
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001203 long max_rtprio = task_rlimit(task, RLIMIT_RTPRIO);
1204
1205 if (max_rtprio == 0) {
1206 policy = SCHED_NORMAL;
1207 priority = MIN_NICE;
1208 } else if (priority > max_rtprio) {
1209 priority = max_rtprio;
1210 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001211 }
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001212
Martijn Coenenecd972d2017-05-26 10:48:56 -07001213 if (verify && is_fair_policy(policy) && !has_cap_nice) {
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001214 long min_nice = rlimit_to_nice(task_rlimit(task, RLIMIT_NICE));
1215
1216 if (min_nice > MAX_NICE) {
1217 binder_user_error("%d RLIMIT_NICE not set\n",
1218 task->pid);
1219 return;
1220 } else if (priority < min_nice) {
1221 priority = min_nice;
1222 }
1223 }
1224
1225 if (policy != desired.sched_policy ||
1226 to_kernel_prio(policy, priority) != desired.prio)
1227 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
1228 "%d: priority %d not allowed, using %d instead\n",
1229 task->pid, desired.prio,
1230 to_kernel_prio(policy, priority));
1231
Martijn Coenen81402ea2017-05-08 09:33:22 -07001232 trace_binder_set_priority(task->tgid, task->pid, task->normal_prio,
1233 to_kernel_prio(policy, priority),
1234 desired.prio);
1235
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001236 /* Set the actual priority */
1237 if (task->policy != policy || is_rt_policy(policy)) {
1238 struct sched_param params;
1239
1240 params.sched_priority = is_rt_policy(policy) ? priority : 0;
1241
1242 sched_setscheduler_nocheck(task,
1243 policy | SCHED_RESET_ON_FORK,
1244 &params);
1245 }
1246 if (is_fair_policy(policy))
1247 set_user_nice(task, priority);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001248}
1249
Martijn Coenenecd972d2017-05-26 10:48:56 -07001250static void binder_set_priority(struct task_struct *task,
1251 struct binder_priority desired)
1252{
1253 binder_do_set_priority(task, desired, /* verify = */ true);
1254}
1255
1256static void binder_restore_priority(struct task_struct *task,
1257 struct binder_priority desired)
1258{
1259 binder_do_set_priority(task, desired, /* verify = */ false);
1260}
1261
Martijn Coenen07a30fe2017-06-07 10:02:12 -07001262static void binder_transaction_priority(struct task_struct *task,
1263 struct binder_transaction *t,
Martijn Coenenc46810c2017-06-23 10:13:43 -07001264 struct binder_priority node_prio,
1265 bool inherit_rt)
Martijn Coenen07a30fe2017-06-07 10:02:12 -07001266{
Ganesh Mahendran9add7c42017-09-27 15:12:25 +08001267 struct binder_priority desired_prio = t->priority;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07001268
1269 if (t->set_priority_called)
1270 return;
1271
1272 t->set_priority_called = true;
1273 t->saved_priority.sched_policy = task->policy;
1274 t->saved_priority.prio = task->normal_prio;
1275
Martijn Coenenc46810c2017-06-23 10:13:43 -07001276 if (!inherit_rt && is_rt_policy(desired_prio.sched_policy)) {
1277 desired_prio.prio = NICE_TO_PRIO(0);
1278 desired_prio.sched_policy = SCHED_NORMAL;
Martijn Coenenc46810c2017-06-23 10:13:43 -07001279 }
Martijn Coenen07a30fe2017-06-07 10:02:12 -07001280
1281 if (node_prio.prio < t->priority.prio ||
1282 (node_prio.prio == t->priority.prio &&
1283 node_prio.sched_policy == SCHED_FIFO)) {
1284 /*
1285 * In case the minimum priority on the node is
1286 * higher (lower value), use that priority. If
1287 * the priority is the same, but the node uses
1288 * SCHED_FIFO, prefer SCHED_FIFO, since it can
1289 * run unbounded, unlike SCHED_RR.
1290 */
1291 desired_prio = node_prio;
1292 }
1293
1294 binder_set_priority(task, desired_prio);
1295}
1296
Todd Kjos425d23f2017-06-12 12:07:26 -07001297static struct binder_node *binder_get_node_ilocked(struct binder_proc *proc,
1298 binder_uintptr_t ptr)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001299{
1300 struct rb_node *n = proc->nodes.rb_node;
1301 struct binder_node *node;
1302
Martijn Coenened323352017-07-27 23:52:24 +02001303 assert_spin_locked(&proc->inner_lock);
Todd Kjos425d23f2017-06-12 12:07:26 -07001304
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001305 while (n) {
1306 node = rb_entry(n, struct binder_node, rb_node);
1307
1308 if (ptr < node->ptr)
1309 n = n->rb_left;
1310 else if (ptr > node->ptr)
1311 n = n->rb_right;
Todd Kjosf22abc72017-05-09 11:08:05 -07001312 else {
1313 /*
1314 * take an implicit weak reference
1315 * to ensure node stays alive until
1316 * call to binder_put_node()
1317 */
Todd Kjos425d23f2017-06-12 12:07:26 -07001318 binder_inc_node_tmpref_ilocked(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001319 return node;
Todd Kjosf22abc72017-05-09 11:08:05 -07001320 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001321 }
1322 return NULL;
1323}
1324
Todd Kjos425d23f2017-06-12 12:07:26 -07001325static struct binder_node *binder_get_node(struct binder_proc *proc,
1326 binder_uintptr_t ptr)
1327{
1328 struct binder_node *node;
1329
1330 binder_inner_proc_lock(proc);
1331 node = binder_get_node_ilocked(proc, ptr);
1332 binder_inner_proc_unlock(proc);
1333 return node;
1334}
1335
1336static struct binder_node *binder_init_node_ilocked(
1337 struct binder_proc *proc,
1338 struct binder_node *new_node,
1339 struct flat_binder_object *fp)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001340{
1341 struct rb_node **p = &proc->nodes.rb_node;
1342 struct rb_node *parent = NULL;
1343 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001344 binder_uintptr_t ptr = fp ? fp->binder : 0;
1345 binder_uintptr_t cookie = fp ? fp->cookie : 0;
1346 __u32 flags = fp ? fp->flags : 0;
Martijn Coenen6aac9792017-06-07 09:29:14 -07001347 s8 priority;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001348
Martijn Coenened323352017-07-27 23:52:24 +02001349 assert_spin_locked(&proc->inner_lock);
1350
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001351 while (*p) {
Todd Kjos425d23f2017-06-12 12:07:26 -07001352
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001353 parent = *p;
1354 node = rb_entry(parent, struct binder_node, rb_node);
1355
1356 if (ptr < node->ptr)
1357 p = &(*p)->rb_left;
1358 else if (ptr > node->ptr)
1359 p = &(*p)->rb_right;
Todd Kjos425d23f2017-06-12 12:07:26 -07001360 else {
1361 /*
1362 * A matching node is already in
1363 * the rb tree. Abandon the init
1364 * and return it.
1365 */
1366 binder_inc_node_tmpref_ilocked(node);
1367 return node;
1368 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001369 }
Todd Kjos425d23f2017-06-12 12:07:26 -07001370 node = new_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001371 binder_stats_created(BINDER_STAT_NODE);
Todd Kjosf22abc72017-05-09 11:08:05 -07001372 node->tmp_refs++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001373 rb_link_node(&node->rb_node, parent, p);
1374 rb_insert_color(&node->rb_node, &proc->nodes);
Todd Kjosc4bd08b2017-05-25 10:56:00 -07001375 node->debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001376 node->proc = proc;
1377 node->ptr = ptr;
1378 node->cookie = cookie;
1379 node->work.type = BINDER_WORK_NODE;
Martijn Coenen6aac9792017-06-07 09:29:14 -07001380 priority = flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
Ganesh Mahendran6cd26312017-09-26 17:56:25 +08001381 node->sched_policy = (flags & FLAT_BINDER_FLAG_SCHED_POLICY_MASK) >>
Martijn Coenen6aac9792017-06-07 09:29:14 -07001382 FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT;
1383 node->min_priority = to_kernel_prio(node->sched_policy, priority);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001384 node->accept_fds = !!(flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
Martijn Coenenc46810c2017-06-23 10:13:43 -07001385 node->inherit_rt = !!(flags & FLAT_BINDER_FLAG_INHERIT_RT);
Todd Kjos63e0afa2019-01-14 09:10:21 -08001386 node->txn_security_ctx = !!(flags & FLAT_BINDER_FLAG_TXN_SECURITY_CTX);
Todd Kjosfc7a7e22017-05-29 16:44:24 -07001387 spin_lock_init(&node->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001388 INIT_LIST_HEAD(&node->work.entry);
1389 INIT_LIST_HEAD(&node->async_todo);
1390 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001391 "%d:%d node %d u%016llx c%016llx created\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001392 proc->pid, current->pid, node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001393 (u64)node->ptr, (u64)node->cookie);
Todd Kjos425d23f2017-06-12 12:07:26 -07001394
1395 return node;
1396}
1397
1398static struct binder_node *binder_new_node(struct binder_proc *proc,
1399 struct flat_binder_object *fp)
1400{
1401 struct binder_node *node;
1402 struct binder_node *new_node = kzalloc(sizeof(*node), GFP_KERNEL);
1403
1404 if (!new_node)
1405 return NULL;
1406 binder_inner_proc_lock(proc);
1407 node = binder_init_node_ilocked(proc, new_node, fp);
1408 binder_inner_proc_unlock(proc);
1409 if (node != new_node)
1410 /*
1411 * The node was already added by another thread
1412 */
1413 kfree(new_node);
1414
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001415 return node;
1416}
1417
Todd Kjose7f23ed2017-03-21 13:06:01 -07001418static void binder_free_node(struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001419{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001420 kfree(node);
1421 binder_stats_deleted(BINDER_STAT_NODE);
1422}
1423
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001424static int binder_inc_node_nilocked(struct binder_node *node, int strong,
1425 int internal,
1426 struct list_head *target_list)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001427{
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001428 struct binder_proc *proc = node->proc;
1429
Martijn Coenened323352017-07-27 23:52:24 +02001430 assert_spin_locked(&node->lock);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001431 if (proc)
Martijn Coenened323352017-07-27 23:52:24 +02001432 assert_spin_locked(&proc->inner_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001433 if (strong) {
1434 if (internal) {
1435 if (target_list == NULL &&
1436 node->internal_strong_refs == 0 &&
Martijn Coenen0b3311e2016-09-30 15:51:48 +02001437 !(node->proc &&
1438 node == node->proc->context->
1439 binder_context_mgr_node &&
1440 node->has_strong_ref)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301441 pr_err("invalid inc strong node for %d\n",
1442 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001443 return -EINVAL;
1444 }
1445 node->internal_strong_refs++;
1446 } else
1447 node->local_strong_refs++;
1448 if (!node->has_strong_ref && target_list) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001449 binder_dequeue_work_ilocked(&node->work);
Martijn Coenen1af61802017-10-19 15:04:46 +02001450 /*
1451 * Note: this function is the only place where we queue
1452 * directly to a thread->todo without using the
1453 * corresponding binder_enqueue_thread_work() helper
1454 * functions; in this case it's ok to not set the
1455 * process_todo flag, since we know this node work will
1456 * always be followed by other work that starts queue
1457 * processing: in case of synchronous transactions, a
1458 * BR_REPLY or BR_ERROR; in case of oneway
1459 * transactions, a BR_TRANSACTION_COMPLETE.
1460 */
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001461 binder_enqueue_work_ilocked(&node->work, target_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001462 }
1463 } else {
1464 if (!internal)
1465 node->local_weak_refs++;
1466 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
1467 if (target_list == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301468 pr_err("invalid inc weak node for %d\n",
1469 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001470 return -EINVAL;
1471 }
Martijn Coenen1af61802017-10-19 15:04:46 +02001472 /*
1473 * See comment above
1474 */
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001475 binder_enqueue_work_ilocked(&node->work, target_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001476 }
1477 }
1478 return 0;
1479}
1480
Todd Kjose7f23ed2017-03-21 13:06:01 -07001481static int binder_inc_node(struct binder_node *node, int strong, int internal,
1482 struct list_head *target_list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001483{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001484 int ret;
1485
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001486 binder_node_inner_lock(node);
1487 ret = binder_inc_node_nilocked(node, strong, internal, target_list);
1488 binder_node_inner_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001489
1490 return ret;
1491}
1492
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001493static bool binder_dec_node_nilocked(struct binder_node *node,
1494 int strong, int internal)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001495{
1496 struct binder_proc *proc = node->proc;
1497
Martijn Coenened323352017-07-27 23:52:24 +02001498 assert_spin_locked(&node->lock);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001499 if (proc)
Martijn Coenened323352017-07-27 23:52:24 +02001500 assert_spin_locked(&proc->inner_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001501 if (strong) {
1502 if (internal)
1503 node->internal_strong_refs--;
1504 else
1505 node->local_strong_refs--;
1506 if (node->local_strong_refs || node->internal_strong_refs)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001507 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001508 } else {
1509 if (!internal)
1510 node->local_weak_refs--;
Todd Kjosf22abc72017-05-09 11:08:05 -07001511 if (node->local_weak_refs || node->tmp_refs ||
1512 !hlist_empty(&node->refs))
Todd Kjose7f23ed2017-03-21 13:06:01 -07001513 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001514 }
Todd Kjose7f23ed2017-03-21 13:06:01 -07001515
1516 if (proc && (node->has_strong_ref || node->has_weak_ref)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001517 if (list_empty(&node->work.entry)) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001518 binder_enqueue_work_ilocked(&node->work, &proc->todo);
Martijn Coenen053be422017-06-06 15:17:46 -07001519 binder_wakeup_proc_ilocked(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001520 }
1521 } else {
1522 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
Todd Kjosf22abc72017-05-09 11:08:05 -07001523 !node->local_weak_refs && !node->tmp_refs) {
Todd Kjose7f23ed2017-03-21 13:06:01 -07001524 if (proc) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001525 binder_dequeue_work_ilocked(&node->work);
1526 rb_erase(&node->rb_node, &proc->nodes);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001527 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301528 "refless node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001529 node->debug_id);
1530 } else {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001531 BUG_ON(!list_empty(&node->work.entry));
Todd Kjos8d9f6f32016-10-17 12:33:15 -07001532 spin_lock(&binder_dead_nodes_lock);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001533 /*
1534 * tmp_refs could have changed so
1535 * check it again
1536 */
1537 if (node->tmp_refs) {
1538 spin_unlock(&binder_dead_nodes_lock);
1539 return false;
1540 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001541 hlist_del(&node->dead_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07001542 spin_unlock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001543 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301544 "dead node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001545 node->debug_id);
1546 }
Todd Kjose7f23ed2017-03-21 13:06:01 -07001547 return true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001548 }
1549 }
Todd Kjose7f23ed2017-03-21 13:06:01 -07001550 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001551}
1552
Todd Kjose7f23ed2017-03-21 13:06:01 -07001553static void binder_dec_node(struct binder_node *node, int strong, int internal)
1554{
1555 bool free_node;
1556
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001557 binder_node_inner_lock(node);
1558 free_node = binder_dec_node_nilocked(node, strong, internal);
1559 binder_node_inner_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001560 if (free_node)
1561 binder_free_node(node);
1562}
1563
1564static void binder_inc_node_tmpref_ilocked(struct binder_node *node)
Todd Kjosf22abc72017-05-09 11:08:05 -07001565{
1566 /*
1567 * No call to binder_inc_node() is needed since we
1568 * don't need to inform userspace of any changes to
1569 * tmp_refs
1570 */
1571 node->tmp_refs++;
1572}
1573
1574/**
Todd Kjose7f23ed2017-03-21 13:06:01 -07001575 * binder_inc_node_tmpref() - take a temporary reference on node
1576 * @node: node to reference
1577 *
1578 * Take reference on node to prevent the node from being freed
1579 * while referenced only by a local variable. The inner lock is
1580 * needed to serialize with the node work on the queue (which
1581 * isn't needed after the node is dead). If the node is dead
1582 * (node->proc is NULL), use binder_dead_nodes_lock to protect
1583 * node->tmp_refs against dead-node-only cases where the node
1584 * lock cannot be acquired (eg traversing the dead node list to
1585 * print nodes)
1586 */
1587static void binder_inc_node_tmpref(struct binder_node *node)
1588{
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001589 binder_node_lock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001590 if (node->proc)
1591 binder_inner_proc_lock(node->proc);
1592 else
1593 spin_lock(&binder_dead_nodes_lock);
1594 binder_inc_node_tmpref_ilocked(node);
1595 if (node->proc)
1596 binder_inner_proc_unlock(node->proc);
1597 else
1598 spin_unlock(&binder_dead_nodes_lock);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001599 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001600}
1601
1602/**
Todd Kjosf22abc72017-05-09 11:08:05 -07001603 * binder_dec_node_tmpref() - remove a temporary reference on node
1604 * @node: node to reference
1605 *
1606 * Release temporary reference on node taken via binder_inc_node_tmpref()
1607 */
1608static void binder_dec_node_tmpref(struct binder_node *node)
1609{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001610 bool free_node;
1611
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001612 binder_node_inner_lock(node);
1613 if (!node->proc)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001614 spin_lock(&binder_dead_nodes_lock);
Todd Kjosf22abc72017-05-09 11:08:05 -07001615 node->tmp_refs--;
1616 BUG_ON(node->tmp_refs < 0);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001617 if (!node->proc)
1618 spin_unlock(&binder_dead_nodes_lock);
Todd Kjosf22abc72017-05-09 11:08:05 -07001619 /*
1620 * Call binder_dec_node() to check if all refcounts are 0
1621 * and cleanup is needed. Calling with strong=0 and internal=1
1622 * causes no actual reference to be released in binder_dec_node().
1623 * If that changes, a change is needed here too.
1624 */
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001625 free_node = binder_dec_node_nilocked(node, 0, 1);
1626 binder_node_inner_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001627 if (free_node)
1628 binder_free_node(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07001629}
1630
1631static void binder_put_node(struct binder_node *node)
1632{
1633 binder_dec_node_tmpref(node);
1634}
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001635
Todd Kjos5346bf32016-10-20 16:43:34 -07001636static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc,
1637 u32 desc, bool need_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001638{
1639 struct rb_node *n = proc->refs_by_desc.rb_node;
1640 struct binder_ref *ref;
1641
1642 while (n) {
1643 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1644
Todd Kjosb0117bb2017-05-08 09:16:27 -07001645 if (desc < ref->data.desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001646 n = n->rb_left;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001647 } else if (desc > ref->data.desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001648 n = n->rb_right;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001649 } else if (need_strong_ref && !ref->data.strong) {
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001650 binder_user_error("tried to use weak ref as strong ref\n");
1651 return NULL;
1652 } else {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001653 return ref;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001654 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001655 }
1656 return NULL;
1657}
1658
Todd Kjosb0117bb2017-05-08 09:16:27 -07001659/**
Todd Kjos5346bf32016-10-20 16:43:34 -07001660 * binder_get_ref_for_node_olocked() - get the ref associated with given node
Todd Kjosb0117bb2017-05-08 09:16:27 -07001661 * @proc: binder_proc that owns the ref
1662 * @node: binder_node of target
1663 * @new_ref: newly allocated binder_ref to be initialized or %NULL
1664 *
1665 * Look up the ref for the given node and return it if it exists
1666 *
1667 * If it doesn't exist and the caller provides a newly allocated
1668 * ref, initialize the fields of the newly allocated ref and insert
1669 * into the given proc rb_trees and node refs list.
1670 *
1671 * Return: the ref for node. It is possible that another thread
1672 * allocated/initialized the ref first in which case the
1673 * returned ref would be different than the passed-in
1674 * new_ref. new_ref must be kfree'd by the caller in
1675 * this case.
1676 */
Todd Kjos5346bf32016-10-20 16:43:34 -07001677static struct binder_ref *binder_get_ref_for_node_olocked(
1678 struct binder_proc *proc,
1679 struct binder_node *node,
1680 struct binder_ref *new_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001681{
Todd Kjosb0117bb2017-05-08 09:16:27 -07001682 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001683 struct rb_node **p = &proc->refs_by_node.rb_node;
1684 struct rb_node *parent = NULL;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001685 struct binder_ref *ref;
1686 struct rb_node *n;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001687
1688 while (*p) {
1689 parent = *p;
1690 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1691
1692 if (node < ref->node)
1693 p = &(*p)->rb_left;
1694 else if (node > ref->node)
1695 p = &(*p)->rb_right;
1696 else
1697 return ref;
1698 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001699 if (!new_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001700 return NULL;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001701
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001702 binder_stats_created(BINDER_STAT_REF);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001703 new_ref->data.debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001704 new_ref->proc = proc;
1705 new_ref->node = node;
1706 rb_link_node(&new_ref->rb_node_node, parent, p);
1707 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1708
Todd Kjosb0117bb2017-05-08 09:16:27 -07001709 new_ref->data.desc = (node == context->binder_context_mgr_node) ? 0 : 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001710 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1711 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001712 if (ref->data.desc > new_ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001713 break;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001714 new_ref->data.desc = ref->data.desc + 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001715 }
1716
1717 p = &proc->refs_by_desc.rb_node;
1718 while (*p) {
1719 parent = *p;
1720 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1721
Todd Kjosb0117bb2017-05-08 09:16:27 -07001722 if (new_ref->data.desc < ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001723 p = &(*p)->rb_left;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001724 else if (new_ref->data.desc > ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001725 p = &(*p)->rb_right;
1726 else
1727 BUG();
1728 }
1729 rb_link_node(&new_ref->rb_node_desc, parent, p);
1730 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001731
1732 binder_node_lock(node);
Todd Kjos4cbe5752017-05-01 17:21:51 -07001733 hlist_add_head(&new_ref->node_entry, &node->refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001734
Todd Kjos4cbe5752017-05-01 17:21:51 -07001735 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1736 "%d new ref %d desc %d for node %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001737 proc->pid, new_ref->data.debug_id, new_ref->data.desc,
Todd Kjos4cbe5752017-05-01 17:21:51 -07001738 node->debug_id);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001739 binder_node_unlock(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001740 return new_ref;
1741}
1742
Todd Kjos5346bf32016-10-20 16:43:34 -07001743static void binder_cleanup_ref_olocked(struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001744{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001745 bool delete_node = false;
Todd Kjose7f23ed2017-03-21 13:06:01 -07001746
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001747 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301748 "%d delete ref %d desc %d for node %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001749 ref->proc->pid, ref->data.debug_id, ref->data.desc,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301750 ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001751
1752 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1753 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001754
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001755 binder_node_inner_lock(ref->node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001756 if (ref->data.strong)
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001757 binder_dec_node_nilocked(ref->node, 1, 1);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001758
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001759 hlist_del(&ref->node_entry);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001760 delete_node = binder_dec_node_nilocked(ref->node, 0, 1);
1761 binder_node_inner_unlock(ref->node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001762 /*
1763 * Clear ref->node unless we want the caller to free the node
1764 */
1765 if (!delete_node) {
1766 /*
1767 * The caller uses ref->node to determine
1768 * whether the node needs to be freed. Clear
1769 * it since the node is still alive.
1770 */
1771 ref->node = NULL;
1772 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001773
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001774 if (ref->death) {
1775 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301776 "%d delete ref %d desc %d has death notification\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001777 ref->proc->pid, ref->data.debug_id,
1778 ref->data.desc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001779 binder_dequeue_work(ref->proc, &ref->death->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001780 binder_stats_deleted(BINDER_STAT_DEATH);
1781 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001782 binder_stats_deleted(BINDER_STAT_REF);
1783}
1784
Todd Kjosb0117bb2017-05-08 09:16:27 -07001785/**
Todd Kjos5346bf32016-10-20 16:43:34 -07001786 * binder_inc_ref_olocked() - increment the ref for given handle
Todd Kjosb0117bb2017-05-08 09:16:27 -07001787 * @ref: ref to be incremented
1788 * @strong: if true, strong increment, else weak
1789 * @target_list: list to queue node work on
1790 *
Todd Kjos5346bf32016-10-20 16:43:34 -07001791 * Increment the ref. @ref->proc->outer_lock must be held on entry
Todd Kjosb0117bb2017-05-08 09:16:27 -07001792 *
1793 * Return: 0, if successful, else errno
1794 */
Todd Kjos5346bf32016-10-20 16:43:34 -07001795static int binder_inc_ref_olocked(struct binder_ref *ref, int strong,
1796 struct list_head *target_list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001797{
1798 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +09001799
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001800 if (strong) {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001801 if (ref->data.strong == 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001802 ret = binder_inc_node(ref->node, 1, 1, target_list);
1803 if (ret)
1804 return ret;
1805 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001806 ref->data.strong++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001807 } else {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001808 if (ref->data.weak == 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001809 ret = binder_inc_node(ref->node, 0, 1, target_list);
1810 if (ret)
1811 return ret;
1812 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001813 ref->data.weak++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001814 }
1815 return 0;
1816}
1817
Todd Kjosb0117bb2017-05-08 09:16:27 -07001818/**
1819 * binder_dec_ref() - dec the ref for given handle
1820 * @ref: ref to be decremented
1821 * @strong: if true, strong decrement, else weak
1822 *
1823 * Decrement the ref.
1824 *
Todd Kjosb0117bb2017-05-08 09:16:27 -07001825 * Return: true if ref is cleaned up and ready to be freed
1826 */
Todd Kjos5346bf32016-10-20 16:43:34 -07001827static bool binder_dec_ref_olocked(struct binder_ref *ref, int strong)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001828{
1829 if (strong) {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001830 if (ref->data.strong == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301831 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001832 ref->proc->pid, ref->data.debug_id,
1833 ref->data.desc, ref->data.strong,
1834 ref->data.weak);
1835 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001836 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001837 ref->data.strong--;
Todd Kjose7f23ed2017-03-21 13:06:01 -07001838 if (ref->data.strong == 0)
1839 binder_dec_node(ref->node, strong, 1);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001840 } else {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001841 if (ref->data.weak == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301842 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001843 ref->proc->pid, ref->data.debug_id,
1844 ref->data.desc, ref->data.strong,
1845 ref->data.weak);
1846 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001847 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001848 ref->data.weak--;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001849 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001850 if (ref->data.strong == 0 && ref->data.weak == 0) {
Todd Kjos5346bf32016-10-20 16:43:34 -07001851 binder_cleanup_ref_olocked(ref);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001852 return true;
1853 }
1854 return false;
1855}
1856
1857/**
1858 * binder_get_node_from_ref() - get the node from the given proc/desc
1859 * @proc: proc containing the ref
1860 * @desc: the handle associated with the ref
1861 * @need_strong_ref: if true, only return node if ref is strong
1862 * @rdata: the id/refcount data for the ref
1863 *
1864 * Given a proc and ref handle, return the associated binder_node
1865 *
1866 * Return: a binder_node or NULL if not found or not strong when strong required
1867 */
1868static struct binder_node *binder_get_node_from_ref(
1869 struct binder_proc *proc,
1870 u32 desc, bool need_strong_ref,
1871 struct binder_ref_data *rdata)
1872{
1873 struct binder_node *node;
1874 struct binder_ref *ref;
1875
Todd Kjos5346bf32016-10-20 16:43:34 -07001876 binder_proc_lock(proc);
1877 ref = binder_get_ref_olocked(proc, desc, need_strong_ref);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001878 if (!ref)
1879 goto err_no_ref;
1880 node = ref->node;
Todd Kjosf22abc72017-05-09 11:08:05 -07001881 /*
1882 * Take an implicit reference on the node to ensure
1883 * it stays alive until the call to binder_put_node()
1884 */
1885 binder_inc_node_tmpref(node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001886 if (rdata)
1887 *rdata = ref->data;
Todd Kjos5346bf32016-10-20 16:43:34 -07001888 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001889
1890 return node;
1891
1892err_no_ref:
Todd Kjos5346bf32016-10-20 16:43:34 -07001893 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001894 return NULL;
1895}
1896
1897/**
1898 * binder_free_ref() - free the binder_ref
1899 * @ref: ref to free
1900 *
Todd Kjose7f23ed2017-03-21 13:06:01 -07001901 * Free the binder_ref. Free the binder_node indicated by ref->node
1902 * (if non-NULL) and the binder_ref_death indicated by ref->death.
Todd Kjosb0117bb2017-05-08 09:16:27 -07001903 */
1904static void binder_free_ref(struct binder_ref *ref)
1905{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001906 if (ref->node)
1907 binder_free_node(ref->node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001908 kfree(ref->death);
1909 kfree(ref);
1910}
1911
1912/**
1913 * binder_update_ref_for_handle() - inc/dec the ref for given handle
1914 * @proc: proc containing the ref
1915 * @desc: the handle associated with the ref
1916 * @increment: true=inc reference, false=dec reference
1917 * @strong: true=strong reference, false=weak reference
1918 * @rdata: the id/refcount data for the ref
1919 *
1920 * Given a proc and ref handle, increment or decrement the ref
1921 * according to "increment" arg.
1922 *
1923 * Return: 0 if successful, else errno
1924 */
1925static int binder_update_ref_for_handle(struct binder_proc *proc,
1926 uint32_t desc, bool increment, bool strong,
1927 struct binder_ref_data *rdata)
1928{
1929 int ret = 0;
1930 struct binder_ref *ref;
1931 bool delete_ref = false;
1932
Todd Kjos5346bf32016-10-20 16:43:34 -07001933 binder_proc_lock(proc);
1934 ref = binder_get_ref_olocked(proc, desc, strong);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001935 if (!ref) {
1936 ret = -EINVAL;
1937 goto err_no_ref;
1938 }
1939 if (increment)
Todd Kjos5346bf32016-10-20 16:43:34 -07001940 ret = binder_inc_ref_olocked(ref, strong, NULL);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001941 else
Todd Kjos5346bf32016-10-20 16:43:34 -07001942 delete_ref = binder_dec_ref_olocked(ref, strong);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001943
1944 if (rdata)
1945 *rdata = ref->data;
Todd Kjos5346bf32016-10-20 16:43:34 -07001946 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001947
1948 if (delete_ref)
1949 binder_free_ref(ref);
1950 return ret;
1951
1952err_no_ref:
Todd Kjos5346bf32016-10-20 16:43:34 -07001953 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001954 return ret;
1955}
1956
1957/**
1958 * binder_dec_ref_for_handle() - dec the ref for given handle
1959 * @proc: proc containing the ref
1960 * @desc: the handle associated with the ref
1961 * @strong: true=strong reference, false=weak reference
1962 * @rdata: the id/refcount data for the ref
1963 *
1964 * Just calls binder_update_ref_for_handle() to decrement the ref.
1965 *
1966 * Return: 0 if successful, else errno
1967 */
1968static int binder_dec_ref_for_handle(struct binder_proc *proc,
1969 uint32_t desc, bool strong, struct binder_ref_data *rdata)
1970{
1971 return binder_update_ref_for_handle(proc, desc, false, strong, rdata);
1972}
1973
1974
1975/**
1976 * binder_inc_ref_for_node() - increment the ref for given proc/node
1977 * @proc: proc containing the ref
1978 * @node: target node
1979 * @strong: true=strong reference, false=weak reference
1980 * @target_list: worklist to use if node is incremented
1981 * @rdata: the id/refcount data for the ref
1982 *
1983 * Given a proc and node, increment the ref. Create the ref if it
1984 * doesn't already exist
1985 *
1986 * Return: 0 if successful, else errno
1987 */
1988static int binder_inc_ref_for_node(struct binder_proc *proc,
1989 struct binder_node *node,
1990 bool strong,
1991 struct list_head *target_list,
1992 struct binder_ref_data *rdata)
1993{
1994 struct binder_ref *ref;
1995 struct binder_ref *new_ref = NULL;
1996 int ret = 0;
1997
Todd Kjos5346bf32016-10-20 16:43:34 -07001998 binder_proc_lock(proc);
1999 ref = binder_get_ref_for_node_olocked(proc, node, NULL);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002000 if (!ref) {
Todd Kjos5346bf32016-10-20 16:43:34 -07002001 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002002 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
2003 if (!new_ref)
2004 return -ENOMEM;
Todd Kjos5346bf32016-10-20 16:43:34 -07002005 binder_proc_lock(proc);
2006 ref = binder_get_ref_for_node_olocked(proc, node, new_ref);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002007 }
Todd Kjos5346bf32016-10-20 16:43:34 -07002008 ret = binder_inc_ref_olocked(ref, strong, target_list);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002009 *rdata = ref->data;
Todd Kjos5346bf32016-10-20 16:43:34 -07002010 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002011 if (new_ref && ref != new_ref)
2012 /*
2013 * Another thread created the ref first so
2014 * free the one we allocated
2015 */
2016 kfree(new_ref);
2017 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002018}
2019
Martijn Coenen995a36e2017-06-02 13:36:52 -07002020static void binder_pop_transaction_ilocked(struct binder_thread *target_thread,
2021 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002022{
Todd Kjos21ef40a2017-03-30 18:02:13 -07002023 BUG_ON(!target_thread);
Martijn Coenened323352017-07-27 23:52:24 +02002024 assert_spin_locked(&target_thread->proc->inner_lock);
Todd Kjos21ef40a2017-03-30 18:02:13 -07002025 BUG_ON(target_thread->transaction_stack != t);
2026 BUG_ON(target_thread->transaction_stack->from != target_thread);
2027 target_thread->transaction_stack =
2028 target_thread->transaction_stack->from_parent;
2029 t->from = NULL;
2030}
2031
Todd Kjos2f993e22017-05-12 14:42:55 -07002032/**
2033 * binder_thread_dec_tmpref() - decrement thread->tmp_ref
2034 * @thread: thread to decrement
2035 *
2036 * A thread needs to be kept alive while being used to create or
2037 * handle a transaction. binder_get_txn_from() is used to safely
2038 * extract t->from from a binder_transaction and keep the thread
2039 * indicated by t->from from being freed. When done with that
2040 * binder_thread, this function is called to decrement the
2041 * tmp_ref and free if appropriate (thread has been released
2042 * and no transaction being processed by the driver)
2043 */
2044static void binder_thread_dec_tmpref(struct binder_thread *thread)
2045{
2046 /*
2047 * atomic is used to protect the counter value while
2048 * it cannot reach zero or thread->is_dead is false
Todd Kjos2f993e22017-05-12 14:42:55 -07002049 */
Todd Kjosb4827902017-05-25 15:52:17 -07002050 binder_inner_proc_lock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002051 atomic_dec(&thread->tmp_ref);
2052 if (thread->is_dead && !atomic_read(&thread->tmp_ref)) {
Todd Kjosb4827902017-05-25 15:52:17 -07002053 binder_inner_proc_unlock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002054 binder_free_thread(thread);
2055 return;
2056 }
Todd Kjosb4827902017-05-25 15:52:17 -07002057 binder_inner_proc_unlock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002058}
2059
2060/**
2061 * binder_proc_dec_tmpref() - decrement proc->tmp_ref
2062 * @proc: proc to decrement
2063 *
2064 * A binder_proc needs to be kept alive while being used to create or
2065 * handle a transaction. proc->tmp_ref is incremented when
2066 * creating a new transaction or the binder_proc is currently in-use
2067 * by threads that are being released. When done with the binder_proc,
2068 * this function is called to decrement the counter and free the
2069 * proc if appropriate (proc has been released, all threads have
2070 * been released and not currenly in-use to process a transaction).
2071 */
2072static void binder_proc_dec_tmpref(struct binder_proc *proc)
2073{
Todd Kjosb4827902017-05-25 15:52:17 -07002074 binder_inner_proc_lock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002075 proc->tmp_ref--;
2076 if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) &&
2077 !proc->tmp_ref) {
Todd Kjosb4827902017-05-25 15:52:17 -07002078 binder_inner_proc_unlock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002079 binder_free_proc(proc);
2080 return;
2081 }
Todd Kjosb4827902017-05-25 15:52:17 -07002082 binder_inner_proc_unlock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002083}
2084
2085/**
2086 * binder_get_txn_from() - safely extract the "from" thread in transaction
2087 * @t: binder transaction for t->from
2088 *
2089 * Atomically return the "from" thread and increment the tmp_ref
2090 * count for the thread to ensure it stays alive until
2091 * binder_thread_dec_tmpref() is called.
2092 *
2093 * Return: the value of t->from
2094 */
2095static struct binder_thread *binder_get_txn_from(
2096 struct binder_transaction *t)
2097{
2098 struct binder_thread *from;
2099
2100 spin_lock(&t->lock);
2101 from = t->from;
2102 if (from)
2103 atomic_inc(&from->tmp_ref);
2104 spin_unlock(&t->lock);
2105 return from;
2106}
2107
Martijn Coenen995a36e2017-06-02 13:36:52 -07002108/**
2109 * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock
2110 * @t: binder transaction for t->from
2111 *
2112 * Same as binder_get_txn_from() except it also acquires the proc->inner_lock
2113 * to guarantee that the thread cannot be released while operating on it.
2114 * The caller must call binder_inner_proc_unlock() to release the inner lock
2115 * as well as call binder_dec_thread_txn() to release the reference.
2116 *
2117 * Return: the value of t->from
2118 */
2119static struct binder_thread *binder_get_txn_from_and_acq_inner(
2120 struct binder_transaction *t)
2121{
2122 struct binder_thread *from;
2123
2124 from = binder_get_txn_from(t);
2125 if (!from)
2126 return NULL;
2127 binder_inner_proc_lock(from->proc);
2128 if (t->from) {
2129 BUG_ON(from != t->from);
2130 return from;
2131 }
2132 binder_inner_proc_unlock(from->proc);
2133 binder_thread_dec_tmpref(from);
2134 return NULL;
2135}
2136
Todd Kjos21ef40a2017-03-30 18:02:13 -07002137static void binder_free_transaction(struct binder_transaction *t)
2138{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002139 if (t->buffer)
2140 t->buffer->transaction = NULL;
2141 kfree(t);
2142 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2143}
2144
2145static void binder_send_failed_reply(struct binder_transaction *t,
2146 uint32_t error_code)
2147{
2148 struct binder_thread *target_thread;
Lucas Tanured4ec15e2014-07-13 21:31:05 -03002149 struct binder_transaction *next;
Seunghun Lee10f62862014-05-01 01:30:23 +09002150
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002151 BUG_ON(t->flags & TF_ONE_WAY);
2152 while (1) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07002153 target_thread = binder_get_txn_from_and_acq_inner(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002154 if (target_thread) {
Todd Kjos858b8da2017-04-21 17:35:12 -07002155 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2156 "send failed reply for transaction %d to %d:%d\n",
2157 t->debug_id,
2158 target_thread->proc->pid,
2159 target_thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002160
Martijn Coenen995a36e2017-06-02 13:36:52 -07002161 binder_pop_transaction_ilocked(target_thread, t);
Todd Kjos858b8da2017-04-21 17:35:12 -07002162 if (target_thread->reply_error.cmd == BR_OK) {
2163 target_thread->reply_error.cmd = error_code;
Martijn Coenen1af61802017-10-19 15:04:46 +02002164 binder_enqueue_thread_work_ilocked(
2165 target_thread,
2166 &target_thread->reply_error.work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002167 wake_up_interruptible(&target_thread->wait);
2168 } else {
Todd Kjosd3a2afb2018-02-07 12:38:47 -08002169 /*
2170 * Cannot get here for normal operation, but
2171 * we can if multiple synchronous transactions
2172 * are sent without blocking for responses.
2173 * Just ignore the 2nd error in this case.
2174 */
2175 pr_warn("Unexpected reply error: %u\n",
2176 target_thread->reply_error.cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002177 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07002178 binder_inner_proc_unlock(target_thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002179 binder_thread_dec_tmpref(target_thread);
Todd Kjos858b8da2017-04-21 17:35:12 -07002180 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002181 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002182 }
Lucas Tanured4ec15e2014-07-13 21:31:05 -03002183 next = t->from_parent;
2184
2185 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2186 "send failed reply for transaction %d, target dead\n",
2187 t->debug_id);
2188
Todd Kjos21ef40a2017-03-30 18:02:13 -07002189 binder_free_transaction(t);
Lucas Tanured4ec15e2014-07-13 21:31:05 -03002190 if (next == NULL) {
2191 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2192 "reply failed, no target thread at root\n");
2193 return;
2194 }
2195 t = next;
2196 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2197 "reply failed, no target thread -- retry %d\n",
2198 t->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002199 }
2200}
2201
Martijn Coenen00c80372016-07-13 12:06:49 +02002202/**
Martijn Coenen3217ccc2017-08-24 15:23:36 +02002203 * binder_cleanup_transaction() - cleans up undelivered transaction
2204 * @t: transaction that needs to be cleaned up
2205 * @reason: reason the transaction wasn't delivered
2206 * @error_code: error to return to caller (if synchronous call)
2207 */
2208static void binder_cleanup_transaction(struct binder_transaction *t,
2209 const char *reason,
2210 uint32_t error_code)
2211{
2212 if (t->buffer->target_node && !(t->flags & TF_ONE_WAY)) {
2213 binder_send_failed_reply(t, error_code);
2214 } else {
2215 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2216 "undelivered transaction %d, %s\n",
2217 t->debug_id, reason);
2218 binder_free_transaction(t);
2219 }
2220}
2221
2222/**
Todd Kjosd73356a2019-02-08 10:35:16 -08002223 * binder_get_object() - gets object and checks for valid metadata
2224 * @proc: binder_proc owning the buffer
Martijn Coenen00c80372016-07-13 12:06:49 +02002225 * @buffer: binder_buffer that we're parsing.
Todd Kjosd73356a2019-02-08 10:35:16 -08002226 * @offset: offset in the @buffer at which to validate an object.
2227 * @object: struct binder_object to read into
Martijn Coenen00c80372016-07-13 12:06:49 +02002228 *
2229 * Return: If there's a valid metadata object at @offset in @buffer, the
Todd Kjosd73356a2019-02-08 10:35:16 -08002230 * size of that object. Otherwise, it returns zero. The object
2231 * is read into the struct binder_object pointed to by @object.
Martijn Coenen00c80372016-07-13 12:06:49 +02002232 */
Todd Kjosd73356a2019-02-08 10:35:16 -08002233static size_t binder_get_object(struct binder_proc *proc,
2234 struct binder_buffer *buffer,
2235 unsigned long offset,
2236 struct binder_object *object)
Martijn Coenen00c80372016-07-13 12:06:49 +02002237{
Todd Kjosd73356a2019-02-08 10:35:16 -08002238 size_t read_size;
Martijn Coenen00c80372016-07-13 12:06:49 +02002239 struct binder_object_header *hdr;
2240 size_t object_size = 0;
2241
Todd Kjosd73356a2019-02-08 10:35:16 -08002242 read_size = min_t(size_t, sizeof(*object), buffer->data_size - offset);
Todd Kjosc8ddc8c2019-03-19 09:53:01 -07002243 if (offset > buffer->data_size || read_size < sizeof(*hdr) ||
2244 !IS_ALIGNED(offset, sizeof(u32)))
Martijn Coenen00c80372016-07-13 12:06:49 +02002245 return 0;
Todd Kjosd73356a2019-02-08 10:35:16 -08002246 binder_alloc_copy_from_buffer(&proc->alloc, object, buffer,
2247 offset, read_size);
Martijn Coenen00c80372016-07-13 12:06:49 +02002248
Todd Kjosd73356a2019-02-08 10:35:16 -08002249 /* Ok, now see if we read a complete object. */
2250 hdr = &object->hdr;
Martijn Coenen00c80372016-07-13 12:06:49 +02002251 switch (hdr->type) {
2252 case BINDER_TYPE_BINDER:
2253 case BINDER_TYPE_WEAK_BINDER:
2254 case BINDER_TYPE_HANDLE:
2255 case BINDER_TYPE_WEAK_HANDLE:
2256 object_size = sizeof(struct flat_binder_object);
2257 break;
2258 case BINDER_TYPE_FD:
2259 object_size = sizeof(struct binder_fd_object);
2260 break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002261 case BINDER_TYPE_PTR:
2262 object_size = sizeof(struct binder_buffer_object);
2263 break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002264 case BINDER_TYPE_FDA:
2265 object_size = sizeof(struct binder_fd_array_object);
2266 break;
Martijn Coenen00c80372016-07-13 12:06:49 +02002267 default:
2268 return 0;
2269 }
2270 if (offset <= buffer->data_size - object_size &&
2271 buffer->data_size >= object_size)
2272 return object_size;
2273 else
2274 return 0;
2275}
2276
Martijn Coenen5a6da532016-09-30 14:10:07 +02002277/**
2278 * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002279 * @proc: binder_proc owning the buffer
Martijn Coenen5a6da532016-09-30 14:10:07 +02002280 * @b: binder_buffer containing the object
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002281 * @object: struct binder_object to read into
Martijn Coenen5a6da532016-09-30 14:10:07 +02002282 * @index: index in offset array at which the binder_buffer_object is
2283 * located
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002284 * @start_offset: points to the start of the offset array
2285 * @object_offsetp: offset of @object read from @b
Martijn Coenen5a6da532016-09-30 14:10:07 +02002286 * @num_valid: the number of valid offsets in the offset array
2287 *
2288 * Return: If @index is within the valid range of the offset array
2289 * described by @start and @num_valid, and if there's a valid
2290 * binder_buffer_object at the offset found in index @index
2291 * of the offset array, that object is returned. Otherwise,
2292 * %NULL is returned.
2293 * Note that the offset found in index @index itself is not
2294 * verified; this function assumes that @num_valid elements
2295 * from @start were previously verified to have valid offsets.
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002296 * If @object_offsetp is non-NULL, then the offset within
2297 * @b is written to it.
Martijn Coenen5a6da532016-09-30 14:10:07 +02002298 */
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002299static struct binder_buffer_object *binder_validate_ptr(
2300 struct binder_proc *proc,
2301 struct binder_buffer *b,
2302 struct binder_object *object,
2303 binder_size_t index,
2304 binder_size_t start_offset,
2305 binder_size_t *object_offsetp,
2306 binder_size_t num_valid)
Martijn Coenen5a6da532016-09-30 14:10:07 +02002307{
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002308 size_t object_size;
2309 binder_size_t object_offset;
2310 unsigned long buffer_offset;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002311
2312 if (index >= num_valid)
2313 return NULL;
2314
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002315 buffer_offset = start_offset + sizeof(binder_size_t) * index;
2316 binder_alloc_copy_from_buffer(&proc->alloc, &object_offset,
2317 b, buffer_offset, sizeof(object_offset));
2318 object_size = binder_get_object(proc, b, object_offset, object);
2319 if (!object_size || object->hdr.type != BINDER_TYPE_PTR)
Martijn Coenen5a6da532016-09-30 14:10:07 +02002320 return NULL;
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002321 if (object_offsetp)
2322 *object_offsetp = object_offset;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002323
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002324 return &object->bbo;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002325}
2326
2327/**
2328 * binder_validate_fixup() - validates pointer/fd fixups happen in order.
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002329 * @proc: binder_proc owning the buffer
Martijn Coenen5a6da532016-09-30 14:10:07 +02002330 * @b: transaction buffer
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002331 * @objects_start_offset: offset to start of objects buffer
2332 * @buffer_obj_offset: offset to binder_buffer_object in which to fix up
2333 * @fixup_offset: start offset in @buffer to fix up
2334 * @last_obj_offset: offset to last binder_buffer_object that we fixed
2335 * @last_min_offset: minimum fixup offset in object at @last_obj_offset
Martijn Coenen5a6da532016-09-30 14:10:07 +02002336 *
2337 * Return: %true if a fixup in buffer @buffer at offset @offset is
2338 * allowed.
2339 *
2340 * For safety reasons, we only allow fixups inside a buffer to happen
2341 * at increasing offsets; additionally, we only allow fixup on the last
2342 * buffer object that was verified, or one of its parents.
2343 *
2344 * Example of what is allowed:
2345 *
2346 * A
2347 * B (parent = A, offset = 0)
2348 * C (parent = A, offset = 16)
2349 * D (parent = C, offset = 0)
2350 * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
2351 *
2352 * Examples of what is not allowed:
2353 *
2354 * Decreasing offsets within the same parent:
2355 * A
2356 * C (parent = A, offset = 16)
2357 * B (parent = A, offset = 0) // decreasing offset within A
2358 *
2359 * Referring to a parent that wasn't the last object or any of its parents:
2360 * A
2361 * B (parent = A, offset = 0)
2362 * C (parent = A, offset = 0)
2363 * C (parent = A, offset = 16)
2364 * D (parent = B, offset = 0) // B is not A or any of A's parents
2365 */
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002366static bool binder_validate_fixup(struct binder_proc *proc,
2367 struct binder_buffer *b,
2368 binder_size_t objects_start_offset,
2369 binder_size_t buffer_obj_offset,
Martijn Coenen5a6da532016-09-30 14:10:07 +02002370 binder_size_t fixup_offset,
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002371 binder_size_t last_obj_offset,
Martijn Coenen5a6da532016-09-30 14:10:07 +02002372 binder_size_t last_min_offset)
2373{
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002374 if (!last_obj_offset) {
Martijn Coenen5a6da532016-09-30 14:10:07 +02002375 /* Nothing to fix up in */
2376 return false;
2377 }
2378
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002379 while (last_obj_offset != buffer_obj_offset) {
2380 unsigned long buffer_offset;
2381 struct binder_object last_object;
2382 struct binder_buffer_object *last_bbo;
2383 size_t object_size = binder_get_object(proc, b, last_obj_offset,
2384 &last_object);
2385 if (object_size != sizeof(*last_bbo))
2386 return false;
2387
2388 last_bbo = &last_object.bbo;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002389 /*
2390 * Safe to retrieve the parent of last_obj, since it
2391 * was already previously verified by the driver.
2392 */
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002393 if ((last_bbo->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0)
Martijn Coenen5a6da532016-09-30 14:10:07 +02002394 return false;
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002395 last_min_offset = last_bbo->parent_offset + sizeof(uintptr_t);
2396 buffer_offset = objects_start_offset +
2397 sizeof(binder_size_t) * last_bbo->parent,
2398 binder_alloc_copy_from_buffer(&proc->alloc, &last_obj_offset,
2399 b, buffer_offset,
2400 sizeof(last_obj_offset));
Martijn Coenen5a6da532016-09-30 14:10:07 +02002401 }
2402 return (fixup_offset >= last_min_offset);
2403}
2404
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002405static void binder_transaction_buffer_release(struct binder_proc *proc,
2406 struct binder_buffer *buffer,
Todd Kjos8539b1e2019-02-08 10:35:20 -08002407 binder_size_t failed_at,
2408 bool is_failure)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002409{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002410 int debug_id = buffer->debug_id;
Todd Kjos8539b1e2019-02-08 10:35:20 -08002411 binder_size_t off_start_offset, buffer_offset, off_end_offset;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002412
2413 binder_debug(BINDER_DEBUG_TRANSACTION,
Todd Kjos8539b1e2019-02-08 10:35:20 -08002414 "%d buffer release %d, size %zd-%zd, failed at %llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002415 proc->pid, buffer->debug_id,
Todd Kjos8539b1e2019-02-08 10:35:20 -08002416 buffer->data_size, buffer->offsets_size,
2417 (unsigned long long)failed_at);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002418
2419 if (buffer->target_node)
2420 binder_dec_node(buffer->target_node, 1, 0);
2421
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002422 off_start_offset = ALIGN(buffer->data_size, sizeof(void *));
Todd Kjos8539b1e2019-02-08 10:35:20 -08002423 off_end_offset = is_failure ? failed_at :
2424 off_start_offset + buffer->offsets_size;
2425 for (buffer_offset = off_start_offset; buffer_offset < off_end_offset;
2426 buffer_offset += sizeof(binder_size_t)) {
Martijn Coenen00c80372016-07-13 12:06:49 +02002427 struct binder_object_header *hdr;
Todd Kjos90a570c2019-02-08 10:35:15 -08002428 size_t object_size;
Todd Kjosd73356a2019-02-08 10:35:16 -08002429 struct binder_object object;
Todd Kjos90a570c2019-02-08 10:35:15 -08002430 binder_size_t object_offset;
Seunghun Lee10f62862014-05-01 01:30:23 +09002431
Todd Kjos90a570c2019-02-08 10:35:15 -08002432 binder_alloc_copy_from_buffer(&proc->alloc, &object_offset,
2433 buffer, buffer_offset,
2434 sizeof(object_offset));
Todd Kjosd73356a2019-02-08 10:35:16 -08002435 object_size = binder_get_object(proc, buffer,
2436 object_offset, &object);
Martijn Coenen00c80372016-07-13 12:06:49 +02002437 if (object_size == 0) {
2438 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
Todd Kjos90a570c2019-02-08 10:35:15 -08002439 debug_id, (u64)object_offset, buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002440 continue;
2441 }
Todd Kjosd73356a2019-02-08 10:35:16 -08002442 hdr = &object.hdr;
Martijn Coenen00c80372016-07-13 12:06:49 +02002443 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002444 case BINDER_TYPE_BINDER:
2445 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenen00c80372016-07-13 12:06:49 +02002446 struct flat_binder_object *fp;
2447 struct binder_node *node;
Seunghun Lee10f62862014-05-01 01:30:23 +09002448
Martijn Coenen00c80372016-07-13 12:06:49 +02002449 fp = to_flat_binder_object(hdr);
2450 node = binder_get_node(proc, fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002451 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002452 pr_err("transaction release %d bad node %016llx\n",
2453 debug_id, (u64)fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002454 break;
2455 }
2456 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002457 " node %d u%016llx\n",
2458 node->debug_id, (u64)node->ptr);
Martijn Coenen00c80372016-07-13 12:06:49 +02002459 binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER,
2460 0);
Todd Kjosf22abc72017-05-09 11:08:05 -07002461 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002462 } break;
2463 case BINDER_TYPE_HANDLE:
2464 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenen00c80372016-07-13 12:06:49 +02002465 struct flat_binder_object *fp;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002466 struct binder_ref_data rdata;
2467 int ret;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02002468
Martijn Coenen00c80372016-07-13 12:06:49 +02002469 fp = to_flat_binder_object(hdr);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002470 ret = binder_dec_ref_for_handle(proc, fp->handle,
2471 hdr->type == BINDER_TYPE_HANDLE, &rdata);
2472
2473 if (ret) {
2474 pr_err("transaction release %d bad handle %d, ret = %d\n",
2475 debug_id, fp->handle, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002476 break;
2477 }
2478 binder_debug(BINDER_DEBUG_TRANSACTION,
Todd Kjosb0117bb2017-05-08 09:16:27 -07002479 " ref %d desc %d\n",
2480 rdata.debug_id, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002481 } break;
2482
Martijn Coenen00c80372016-07-13 12:06:49 +02002483 case BINDER_TYPE_FD: {
2484 struct binder_fd_object *fp = to_binder_fd_object(hdr);
2485
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002486 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen00c80372016-07-13 12:06:49 +02002487 " fd %d\n", fp->fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002488 if (failed_at)
Martijn Coenen00c80372016-07-13 12:06:49 +02002489 task_close_fd(proc, fp->fd);
2490 } break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002491 case BINDER_TYPE_PTR:
2492 /*
2493 * Nothing to do here, this will get cleaned up when the
2494 * transaction buffer gets freed
2495 */
2496 break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002497 case BINDER_TYPE_FDA: {
2498 struct binder_fd_array_object *fda;
2499 struct binder_buffer_object *parent;
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002500 struct binder_object ptr_object;
Todd Kjos8539b1e2019-02-08 10:35:20 -08002501 binder_size_t fda_offset;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002502 size_t fd_index;
2503 binder_size_t fd_buf_size;
Todd Kjos8539b1e2019-02-08 10:35:20 -08002504 binder_size_t num_valid;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002505
Todd Kjos8539b1e2019-02-08 10:35:20 -08002506 num_valid = (buffer_offset - off_start_offset) /
2507 sizeof(binder_size_t);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002508 fda = to_binder_fd_array_object(hdr);
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002509 parent = binder_validate_ptr(proc, buffer, &ptr_object,
2510 fda->parent,
2511 off_start_offset,
2512 NULL,
Todd Kjos8539b1e2019-02-08 10:35:20 -08002513 num_valid);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002514 if (!parent) {
2515 pr_err("transaction release %d bad parent offset",
2516 debug_id);
2517 continue;
2518 }
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002519 fd_buf_size = sizeof(u32) * fda->num_fds;
2520 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2521 pr_err("transaction release %d invalid number of fds (%lld)\n",
2522 debug_id, (u64)fda->num_fds);
2523 continue;
2524 }
2525 if (fd_buf_size > parent->length ||
2526 fda->parent_offset > parent->length - fd_buf_size) {
2527 /* No space for all file descriptors here. */
2528 pr_err("transaction release %d not enough space for %lld fds in buffer\n",
2529 debug_id, (u64)fda->num_fds);
2530 continue;
2531 }
Todd Kjos8539b1e2019-02-08 10:35:20 -08002532 /*
2533 * the source data for binder_buffer_object is visible
2534 * to user-space and the @buffer element is the user
2535 * pointer to the buffer_object containing the fd_array.
2536 * Convert the address to an offset relative to
2537 * the base of the transaction buffer.
2538 */
2539 fda_offset =
2540 (parent->buffer - (uintptr_t)buffer->user_data) +
2541 fda->parent_offset;
Todd Kjos90a570c2019-02-08 10:35:15 -08002542 for (fd_index = 0; fd_index < fda->num_fds;
2543 fd_index++) {
2544 u32 fd;
Todd Kjos8539b1e2019-02-08 10:35:20 -08002545 binder_size_t offset = fda_offset +
2546 fd_index * sizeof(fd);
Todd Kjos90a570c2019-02-08 10:35:15 -08002547
2548 binder_alloc_copy_from_buffer(&proc->alloc,
2549 &fd,
2550 buffer,
2551 offset,
2552 sizeof(fd));
2553 task_close_fd(proc, fd);
2554 }
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002555 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002556 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01002557 pr_err("transaction release %d bad object type %x\n",
Martijn Coenen00c80372016-07-13 12:06:49 +02002558 debug_id, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002559 break;
2560 }
2561 }
2562}
2563
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002564static int binder_translate_binder(struct flat_binder_object *fp,
2565 struct binder_transaction *t,
2566 struct binder_thread *thread)
2567{
2568 struct binder_node *node;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002569 struct binder_proc *proc = thread->proc;
2570 struct binder_proc *target_proc = t->to_proc;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002571 struct binder_ref_data rdata;
Todd Kjosf22abc72017-05-09 11:08:05 -07002572 int ret = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002573
2574 node = binder_get_node(proc, fp->binder);
2575 if (!node) {
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002576 node = binder_new_node(proc, fp);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002577 if (!node)
2578 return -ENOMEM;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002579 }
2580 if (fp->cookie != node->cookie) {
2581 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
2582 proc->pid, thread->pid, (u64)fp->binder,
2583 node->debug_id, (u64)fp->cookie,
2584 (u64)node->cookie);
Todd Kjosf22abc72017-05-09 11:08:05 -07002585 ret = -EINVAL;
2586 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002587 }
Todd Kjosf22abc72017-05-09 11:08:05 -07002588 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2589 ret = -EPERM;
2590 goto done;
2591 }
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002592
Todd Kjosb0117bb2017-05-08 09:16:27 -07002593 ret = binder_inc_ref_for_node(target_proc, node,
2594 fp->hdr.type == BINDER_TYPE_BINDER,
2595 &thread->todo, &rdata);
2596 if (ret)
Todd Kjosf22abc72017-05-09 11:08:05 -07002597 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002598
2599 if (fp->hdr.type == BINDER_TYPE_BINDER)
2600 fp->hdr.type = BINDER_TYPE_HANDLE;
2601 else
2602 fp->hdr.type = BINDER_TYPE_WEAK_HANDLE;
2603 fp->binder = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002604 fp->handle = rdata.desc;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002605 fp->cookie = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002606
Todd Kjosb0117bb2017-05-08 09:16:27 -07002607 trace_binder_transaction_node_to_ref(t, node, &rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002608 binder_debug(BINDER_DEBUG_TRANSACTION,
2609 " node %d u%016llx -> ref %d desc %d\n",
2610 node->debug_id, (u64)node->ptr,
Todd Kjosb0117bb2017-05-08 09:16:27 -07002611 rdata.debug_id, rdata.desc);
Todd Kjosf22abc72017-05-09 11:08:05 -07002612done:
2613 binder_put_node(node);
2614 return ret;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002615}
2616
2617static int binder_translate_handle(struct flat_binder_object *fp,
2618 struct binder_transaction *t,
2619 struct binder_thread *thread)
2620{
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002621 struct binder_proc *proc = thread->proc;
2622 struct binder_proc *target_proc = t->to_proc;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002623 struct binder_node *node;
2624 struct binder_ref_data src_rdata;
Todd Kjosf22abc72017-05-09 11:08:05 -07002625 int ret = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002626
Todd Kjosb0117bb2017-05-08 09:16:27 -07002627 node = binder_get_node_from_ref(proc, fp->handle,
2628 fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata);
2629 if (!node) {
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002630 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
2631 proc->pid, thread->pid, fp->handle);
2632 return -EINVAL;
2633 }
Todd Kjosf22abc72017-05-09 11:08:05 -07002634 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2635 ret = -EPERM;
2636 goto done;
2637 }
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002638
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002639 binder_node_lock(node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002640 if (node->proc == target_proc) {
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002641 if (fp->hdr.type == BINDER_TYPE_HANDLE)
2642 fp->hdr.type = BINDER_TYPE_BINDER;
2643 else
2644 fp->hdr.type = BINDER_TYPE_WEAK_BINDER;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002645 fp->binder = node->ptr;
2646 fp->cookie = node->cookie;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002647 if (node->proc)
2648 binder_inner_proc_lock(node->proc);
2649 binder_inc_node_nilocked(node,
2650 fp->hdr.type == BINDER_TYPE_BINDER,
2651 0, NULL);
2652 if (node->proc)
2653 binder_inner_proc_unlock(node->proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002654 trace_binder_transaction_ref_to_node(t, node, &src_rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002655 binder_debug(BINDER_DEBUG_TRANSACTION,
2656 " ref %d desc %d -> node %d u%016llx\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07002657 src_rdata.debug_id, src_rdata.desc, node->debug_id,
2658 (u64)node->ptr);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002659 binder_node_unlock(node);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002660 } else {
Todd Kjosb0117bb2017-05-08 09:16:27 -07002661 struct binder_ref_data dest_rdata;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002662
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002663 binder_node_unlock(node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002664 ret = binder_inc_ref_for_node(target_proc, node,
2665 fp->hdr.type == BINDER_TYPE_HANDLE,
2666 NULL, &dest_rdata);
2667 if (ret)
Todd Kjosf22abc72017-05-09 11:08:05 -07002668 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002669
2670 fp->binder = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002671 fp->handle = dest_rdata.desc;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002672 fp->cookie = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002673 trace_binder_transaction_ref_to_ref(t, node, &src_rdata,
2674 &dest_rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002675 binder_debug(BINDER_DEBUG_TRANSACTION,
2676 " ref %d desc %d -> ref %d desc %d (node %d)\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07002677 src_rdata.debug_id, src_rdata.desc,
2678 dest_rdata.debug_id, dest_rdata.desc,
2679 node->debug_id);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002680 }
Todd Kjosf22abc72017-05-09 11:08:05 -07002681done:
2682 binder_put_node(node);
2683 return ret;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002684}
2685
2686static int binder_translate_fd(int fd,
2687 struct binder_transaction *t,
2688 struct binder_thread *thread,
2689 struct binder_transaction *in_reply_to)
2690{
2691 struct binder_proc *proc = thread->proc;
2692 struct binder_proc *target_proc = t->to_proc;
2693 int target_fd;
2694 struct file *file;
2695 int ret;
2696 bool target_allows_fd;
2697
2698 if (in_reply_to)
2699 target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS);
2700 else
2701 target_allows_fd = t->buffer->target_node->accept_fds;
2702 if (!target_allows_fd) {
2703 binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
2704 proc->pid, thread->pid,
2705 in_reply_to ? "reply" : "transaction",
2706 fd);
2707 ret = -EPERM;
2708 goto err_fd_not_accepted;
2709 }
2710
2711 file = fget(fd);
2712 if (!file) {
2713 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
2714 proc->pid, thread->pid, fd);
2715 ret = -EBADF;
2716 goto err_fget;
2717 }
2718 ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file);
2719 if (ret < 0) {
2720 ret = -EPERM;
2721 goto err_security;
2722 }
2723
2724 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
2725 if (target_fd < 0) {
2726 ret = -ENOMEM;
2727 goto err_get_unused_fd;
2728 }
2729 task_fd_install(target_proc, target_fd, file);
2730 trace_binder_transaction_fd(t, fd, target_fd);
2731 binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n",
2732 fd, target_fd);
2733
2734 return target_fd;
2735
2736err_get_unused_fd:
2737err_security:
2738 fput(file);
2739err_fget:
2740err_fd_not_accepted:
2741 return ret;
2742}
2743
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002744static int binder_translate_fd_array(struct binder_fd_array_object *fda,
2745 struct binder_buffer_object *parent,
2746 struct binder_transaction *t,
2747 struct binder_thread *thread,
2748 struct binder_transaction *in_reply_to)
2749{
2750 binder_size_t fdi, fd_buf_size, num_installed_fds;
Todd Kjos8539b1e2019-02-08 10:35:20 -08002751 binder_size_t fda_offset;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002752 int target_fd;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002753 struct binder_proc *proc = thread->proc;
2754 struct binder_proc *target_proc = t->to_proc;
2755
2756 fd_buf_size = sizeof(u32) * fda->num_fds;
2757 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2758 binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
2759 proc->pid, thread->pid, (u64)fda->num_fds);
2760 return -EINVAL;
2761 }
2762 if (fd_buf_size > parent->length ||
2763 fda->parent_offset > parent->length - fd_buf_size) {
2764 /* No space for all file descriptors here. */
2765 binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
2766 proc->pid, thread->pid, (u64)fda->num_fds);
2767 return -EINVAL;
2768 }
Todd Kjos8539b1e2019-02-08 10:35:20 -08002769 /*
2770 * the source data for binder_buffer_object is visible
2771 * to user-space and the @buffer element is the user
2772 * pointer to the buffer_object containing the fd_array.
2773 * Convert the address to an offset relative to
2774 * the base of the transaction buffer.
2775 */
2776 fda_offset = (parent->buffer - (uintptr_t)t->buffer->user_data) +
2777 fda->parent_offset;
2778 if (!IS_ALIGNED((unsigned long)fda_offset, sizeof(u32))) {
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002779 binder_user_error("%d:%d parent offset not aligned correctly.\n",
2780 proc->pid, thread->pid);
2781 return -EINVAL;
2782 }
2783 for (fdi = 0; fdi < fda->num_fds; fdi++) {
Todd Kjos90a570c2019-02-08 10:35:15 -08002784 u32 fd;
2785 int target_fd;
Todd Kjos8539b1e2019-02-08 10:35:20 -08002786 binder_size_t offset = fda_offset + fdi * sizeof(fd);
Todd Kjos90a570c2019-02-08 10:35:15 -08002787
2788 binder_alloc_copy_from_buffer(&target_proc->alloc,
2789 &fd, t->buffer,
2790 offset, sizeof(fd));
2791 target_fd = binder_translate_fd(fd, t, thread, in_reply_to);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002792 if (target_fd < 0)
2793 goto err_translate_fd_failed;
Todd Kjos90a570c2019-02-08 10:35:15 -08002794 binder_alloc_copy_to_buffer(&target_proc->alloc,
2795 t->buffer, offset,
2796 &target_fd, sizeof(fd));
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002797 }
2798 return 0;
2799
2800err_translate_fd_failed:
2801 /*
2802 * Failed to allocate fd or security error, free fds
2803 * installed so far.
2804 */
2805 num_installed_fds = fdi;
Todd Kjos90a570c2019-02-08 10:35:15 -08002806 for (fdi = 0; fdi < num_installed_fds; fdi++) {
2807 u32 fd;
Todd Kjos8539b1e2019-02-08 10:35:20 -08002808 binder_size_t offset = fda_offset + fdi * sizeof(fd);
Todd Kjos90a570c2019-02-08 10:35:15 -08002809 binder_alloc_copy_from_buffer(&target_proc->alloc,
2810 &fd, t->buffer,
2811 offset, sizeof(fd));
2812 task_close_fd(target_proc, fd);
2813 }
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002814 return target_fd;
2815}
2816
Martijn Coenen5a6da532016-09-30 14:10:07 +02002817static int binder_fixup_parent(struct binder_transaction *t,
2818 struct binder_thread *thread,
2819 struct binder_buffer_object *bp,
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002820 binder_size_t off_start_offset,
Martijn Coenen5a6da532016-09-30 14:10:07 +02002821 binder_size_t num_valid,
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002822 binder_size_t last_fixup_obj_off,
Martijn Coenen5a6da532016-09-30 14:10:07 +02002823 binder_size_t last_fixup_min_off)
2824{
2825 struct binder_buffer_object *parent;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002826 struct binder_buffer *b = t->buffer;
2827 struct binder_proc *proc = thread->proc;
2828 struct binder_proc *target_proc = t->to_proc;
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002829 struct binder_object object;
2830 binder_size_t buffer_offset;
2831 binder_size_t parent_offset;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002832
2833 if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT))
2834 return 0;
2835
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002836 parent = binder_validate_ptr(target_proc, b, &object, bp->parent,
2837 off_start_offset, &parent_offset,
2838 num_valid);
Martijn Coenen5a6da532016-09-30 14:10:07 +02002839 if (!parent) {
2840 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
2841 proc->pid, thread->pid);
2842 return -EINVAL;
2843 }
2844
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002845 if (!binder_validate_fixup(target_proc, b, off_start_offset,
2846 parent_offset, bp->parent_offset,
2847 last_fixup_obj_off,
Martijn Coenen5a6da532016-09-30 14:10:07 +02002848 last_fixup_min_off)) {
2849 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
2850 proc->pid, thread->pid);
2851 return -EINVAL;
2852 }
2853
2854 if (parent->length < sizeof(binder_uintptr_t) ||
2855 bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) {
2856 /* No space for a pointer here! */
2857 binder_user_error("%d:%d got transaction with invalid parent offset\n",
2858 proc->pid, thread->pid);
2859 return -EINVAL;
2860 }
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002861 buffer_offset = bp->parent_offset +
Todd Kjos8539b1e2019-02-08 10:35:20 -08002862 (uintptr_t)parent->buffer - (uintptr_t)b->user_data;
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002863 binder_alloc_copy_to_buffer(&target_proc->alloc, b, buffer_offset,
2864 &bp->buffer, sizeof(bp->buffer));
Martijn Coenen5a6da532016-09-30 14:10:07 +02002865
2866 return 0;
2867}
2868
Martijn Coenen053be422017-06-06 15:17:46 -07002869/**
2870 * binder_proc_transaction() - sends a transaction to a process and wakes it up
2871 * @t: transaction to send
2872 * @proc: process to send the transaction to
2873 * @thread: thread in @proc to send the transaction to (may be NULL)
2874 *
2875 * This function queues a transaction to the specified process. It will try
2876 * to find a thread in the target process to handle the transaction and
2877 * wake it up. If no thread is found, the work is queued to the proc
2878 * waitqueue.
2879 *
2880 * If the @thread parameter is not NULL, the transaction is always queued
2881 * to the waitlist of that specific thread.
2882 *
2883 * Return: true if the transactions was successfully queued
2884 * false if the target process or thread is dead
2885 */
2886static bool binder_proc_transaction(struct binder_transaction *t,
2887 struct binder_proc *proc,
2888 struct binder_thread *thread)
2889{
Martijn Coenen053be422017-06-06 15:17:46 -07002890 struct binder_node *node = t->buffer->target_node;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002891 struct binder_priority node_prio;
Martijn Coenen053be422017-06-06 15:17:46 -07002892 bool oneway = !!(t->flags & TF_ONE_WAY);
Martijn Coenen1af61802017-10-19 15:04:46 +02002893 bool pending_async = false;
Martijn Coenen053be422017-06-06 15:17:46 -07002894
2895 BUG_ON(!node);
2896 binder_node_lock(node);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002897 node_prio.prio = node->min_priority;
2898 node_prio.sched_policy = node->sched_policy;
2899
Martijn Coenen053be422017-06-06 15:17:46 -07002900 if (oneway) {
2901 BUG_ON(thread);
2902 if (node->has_async_transaction) {
Martijn Coenen1af61802017-10-19 15:04:46 +02002903 pending_async = true;
Martijn Coenen053be422017-06-06 15:17:46 -07002904 } else {
Gustavo A. R. Silvae62dd6f2018-01-23 12:04:27 -06002905 node->has_async_transaction = true;
Martijn Coenen053be422017-06-06 15:17:46 -07002906 }
2907 }
2908
2909 binder_inner_proc_lock(proc);
2910
2911 if (proc->is_dead || (thread && thread->is_dead)) {
2912 binder_inner_proc_unlock(proc);
2913 binder_node_unlock(node);
2914 return false;
2915 }
2916
Martijn Coenen1af61802017-10-19 15:04:46 +02002917 if (!thread && !pending_async)
Martijn Coenen053be422017-06-06 15:17:46 -07002918 thread = binder_select_thread_ilocked(proc);
2919
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002920 if (thread) {
Martijn Coenenc46810c2017-06-23 10:13:43 -07002921 binder_transaction_priority(thread->task, t, node_prio,
2922 node->inherit_rt);
Martijn Coenen1af61802017-10-19 15:04:46 +02002923 binder_enqueue_thread_work_ilocked(thread, &t->work);
2924 } else if (!pending_async) {
2925 binder_enqueue_work_ilocked(&t->work, &proc->todo);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002926 } else {
Martijn Coenen1af61802017-10-19 15:04:46 +02002927 binder_enqueue_work_ilocked(&t->work, &node->async_todo);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002928 }
Martijn Coenen053be422017-06-06 15:17:46 -07002929
Martijn Coenen1af61802017-10-19 15:04:46 +02002930 if (!pending_async)
Martijn Coenen053be422017-06-06 15:17:46 -07002931 binder_wakeup_thread_ilocked(proc, thread, !oneway /* sync */);
2932
2933 binder_inner_proc_unlock(proc);
2934 binder_node_unlock(node);
2935
2936 return true;
2937}
2938
Todd Kjos291d9682017-09-25 08:55:09 -07002939/**
2940 * binder_get_node_refs_for_txn() - Get required refs on node for txn
2941 * @node: struct binder_node for which to get refs
2942 * @proc: returns @node->proc if valid
2943 * @error: if no @proc then returns BR_DEAD_REPLY
2944 *
2945 * User-space normally keeps the node alive when creating a transaction
2946 * since it has a reference to the target. The local strong ref keeps it
2947 * alive if the sending process dies before the target process processes
2948 * the transaction. If the source process is malicious or has a reference
2949 * counting bug, relying on the local strong ref can fail.
2950 *
2951 * Since user-space can cause the local strong ref to go away, we also take
2952 * a tmpref on the node to ensure it survives while we are constructing
2953 * the transaction. We also need a tmpref on the proc while we are
2954 * constructing the transaction, so we take that here as well.
2955 *
2956 * Return: The target_node with refs taken or NULL if no @node->proc is NULL.
2957 * Also sets @proc if valid. If the @node->proc is NULL indicating that the
2958 * target proc has died, @error is set to BR_DEAD_REPLY
2959 */
2960static struct binder_node *binder_get_node_refs_for_txn(
2961 struct binder_node *node,
2962 struct binder_proc **procp,
2963 uint32_t *error)
2964{
2965 struct binder_node *target_node = NULL;
2966
2967 binder_node_inner_lock(node);
2968 if (node->proc) {
2969 target_node = node;
2970 binder_inc_node_nilocked(node, 1, 0, NULL);
2971 binder_inc_node_tmpref_ilocked(node);
2972 node->proc->tmp_ref++;
2973 *procp = node->proc;
2974 } else
2975 *error = BR_DEAD_REPLY;
2976 binder_node_inner_unlock(node);
2977
2978 return target_node;
2979}
2980
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002981static void binder_transaction(struct binder_proc *proc,
2982 struct binder_thread *thread,
Martijn Coenen59878d72016-09-30 14:05:40 +02002983 struct binder_transaction_data *tr, int reply,
2984 binder_size_t extra_buffers_size)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002985{
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002986 int ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002987 struct binder_transaction *t;
2988 struct binder_work *tcomplete;
Todd Kjos8539b1e2019-02-08 10:35:20 -08002989 binder_size_t buffer_offset = 0;
2990 binder_size_t off_start_offset, off_end_offset;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08002991 binder_size_t off_min;
Todd Kjos8539b1e2019-02-08 10:35:20 -08002992 binder_size_t sg_buf_offset, sg_buf_end_offset;
Todd Kjos2f993e22017-05-12 14:42:55 -07002993 struct binder_proc *target_proc = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002994 struct binder_thread *target_thread = NULL;
2995 struct binder_node *target_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002996 struct binder_transaction *in_reply_to = NULL;
2997 struct binder_transaction_log_entry *e;
Todd Kjose598d172017-03-22 17:19:52 -07002998 uint32_t return_error = 0;
2999 uint32_t return_error_param = 0;
3000 uint32_t return_error_line = 0;
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003001 binder_size_t last_fixup_obj_off = 0;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003002 binder_size_t last_fixup_min_off = 0;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003003 struct binder_context *context = proc->context;
Todd Kjos1cfe6272017-05-24 13:33:28 -07003004 int t_debug_id = atomic_inc_return(&binder_last_id);
Todd Kjos63e0afa2019-01-14 09:10:21 -08003005 char *secctx = NULL;
3006 u32 secctx_sz = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003007
3008 e = binder_transaction_log_add(&binder_transaction_log);
Todd Kjos1cfe6272017-05-24 13:33:28 -07003009 e->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003010 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
3011 e->from_proc = proc->pid;
3012 e->from_thread = thread->pid;
3013 e->target_handle = tr->target.handle;
3014 e->data_size = tr->data_size;
3015 e->offsets_size = tr->offsets_size;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02003016 e->context_name = proc->context->name;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003017
3018 if (reply) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07003019 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003020 in_reply_to = thread->transaction_stack;
3021 if (in_reply_to == NULL) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07003022 binder_inner_proc_unlock(proc);
Anmol Sarma56b468f2012-10-30 22:35:43 +05303023 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003024 proc->pid, thread->pid);
3025 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003026 return_error_param = -EPROTO;
3027 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003028 goto err_empty_call_stack;
3029 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003030 if (in_reply_to->to_thread != thread) {
Todd Kjos2f993e22017-05-12 14:42:55 -07003031 spin_lock(&in_reply_to->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05303032 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 +09003033 proc->pid, thread->pid, in_reply_to->debug_id,
3034 in_reply_to->to_proc ?
3035 in_reply_to->to_proc->pid : 0,
3036 in_reply_to->to_thread ?
3037 in_reply_to->to_thread->pid : 0);
Todd Kjos2f993e22017-05-12 14:42:55 -07003038 spin_unlock(&in_reply_to->lock);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003039 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003040 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003041 return_error_param = -EPROTO;
3042 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003043 in_reply_to = NULL;
3044 goto err_bad_call_stack;
3045 }
3046 thread->transaction_stack = in_reply_to->to_parent;
Martijn Coenen995a36e2017-06-02 13:36:52 -07003047 binder_inner_proc_unlock(proc);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003048 target_thread = binder_get_txn_from_and_acq_inner(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003049 if (target_thread == NULL) {
3050 return_error = BR_DEAD_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003051 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003052 goto err_dead_binder;
3053 }
3054 if (target_thread->transaction_stack != in_reply_to) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303055 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 +09003056 proc->pid, thread->pid,
3057 target_thread->transaction_stack ?
3058 target_thread->transaction_stack->debug_id : 0,
3059 in_reply_to->debug_id);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003060 binder_inner_proc_unlock(target_thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003061 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003062 return_error_param = -EPROTO;
3063 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003064 in_reply_to = NULL;
3065 target_thread = NULL;
3066 goto err_dead_binder;
3067 }
3068 target_proc = target_thread->proc;
Todd Kjos2f993e22017-05-12 14:42:55 -07003069 target_proc->tmp_ref++;
Martijn Coenen995a36e2017-06-02 13:36:52 -07003070 binder_inner_proc_unlock(target_thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003071 } else {
3072 if (tr->target.handle) {
3073 struct binder_ref *ref;
Seunghun Lee10f62862014-05-01 01:30:23 +09003074
Todd Kjosc37162d2017-05-26 11:56:29 -07003075 /*
3076 * There must already be a strong ref
3077 * on this node. If so, do a strong
3078 * increment on the node to ensure it
3079 * stays alive until the transaction is
3080 * done.
3081 */
Todd Kjos5346bf32016-10-20 16:43:34 -07003082 binder_proc_lock(proc);
3083 ref = binder_get_ref_olocked(proc, tr->target.handle,
3084 true);
Todd Kjosc37162d2017-05-26 11:56:29 -07003085 if (ref) {
Todd Kjos291d9682017-09-25 08:55:09 -07003086 target_node = binder_get_node_refs_for_txn(
3087 ref->node, &target_proc,
3088 &return_error);
3089 } else {
3090 binder_user_error("%d:%d got transaction to invalid handle\n",
3091 proc->pid, thread->pid);
3092 return_error = BR_FAILED_REPLY;
Todd Kjosc37162d2017-05-26 11:56:29 -07003093 }
Todd Kjos5346bf32016-10-20 16:43:34 -07003094 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003095 } else {
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003096 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003097 target_node = context->binder_context_mgr_node;
Todd Kjos291d9682017-09-25 08:55:09 -07003098 if (target_node)
3099 target_node = binder_get_node_refs_for_txn(
3100 target_node, &target_proc,
3101 &return_error);
3102 else
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003103 return_error = BR_DEAD_REPLY;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003104 mutex_unlock(&context->context_mgr_node_lock);
Martijn Coenenc4048b22018-03-28 11:14:50 +02003105 if (target_node && target_proc == proc) {
3106 binder_user_error("%d:%d got transaction to context manager from process owning it\n",
3107 proc->pid, thread->pid);
3108 return_error = BR_FAILED_REPLY;
3109 return_error_param = -EINVAL;
3110 return_error_line = __LINE__;
3111 goto err_invalid_target_handle;
3112 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003113 }
Todd Kjos291d9682017-09-25 08:55:09 -07003114 if (!target_node) {
3115 /*
3116 * return_error is set above
3117 */
3118 return_error_param = -EINVAL;
Todd Kjose598d172017-03-22 17:19:52 -07003119 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003120 goto err_dead_binder;
3121 }
Todd Kjos291d9682017-09-25 08:55:09 -07003122 e->to_node = target_node->debug_id;
Stephen Smalley79af7302015-01-21 10:54:10 -05003123 if (security_binder_transaction(proc->tsk,
3124 target_proc->tsk) < 0) {
3125 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003126 return_error_param = -EPERM;
3127 return_error_line = __LINE__;
Stephen Smalley79af7302015-01-21 10:54:10 -05003128 goto err_invalid_target_handle;
3129 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07003130 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003131 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
3132 struct binder_transaction *tmp;
Seunghun Lee10f62862014-05-01 01:30:23 +09003133
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003134 tmp = thread->transaction_stack;
3135 if (tmp->to_thread != thread) {
Todd Kjos2f993e22017-05-12 14:42:55 -07003136 spin_lock(&tmp->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05303137 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 +09003138 proc->pid, thread->pid, tmp->debug_id,
3139 tmp->to_proc ? tmp->to_proc->pid : 0,
3140 tmp->to_thread ?
3141 tmp->to_thread->pid : 0);
Todd Kjos2f993e22017-05-12 14:42:55 -07003142 spin_unlock(&tmp->lock);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003143 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003144 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003145 return_error_param = -EPROTO;
3146 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003147 goto err_bad_call_stack;
3148 }
3149 while (tmp) {
Todd Kjos2f993e22017-05-12 14:42:55 -07003150 struct binder_thread *from;
3151
3152 spin_lock(&tmp->lock);
3153 from = tmp->from;
3154 if (from && from->proc == target_proc) {
3155 atomic_inc(&from->tmp_ref);
3156 target_thread = from;
3157 spin_unlock(&tmp->lock);
3158 break;
3159 }
3160 spin_unlock(&tmp->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003161 tmp = tmp->from_parent;
3162 }
3163 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07003164 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003165 }
Martijn Coenen053be422017-06-06 15:17:46 -07003166 if (target_thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003167 e->to_thread = target_thread->pid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003168 e->to_proc = target_proc->pid;
3169
3170 /* TODO: reuse incoming transaction for reply */
3171 t = kzalloc(sizeof(*t), GFP_KERNEL);
3172 if (t == NULL) {
3173 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003174 return_error_param = -ENOMEM;
3175 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003176 goto err_alloc_t_failed;
3177 }
3178 binder_stats_created(BINDER_STAT_TRANSACTION);
Todd Kjos2f993e22017-05-12 14:42:55 -07003179 spin_lock_init(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003180
3181 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
3182 if (tcomplete == NULL) {
3183 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003184 return_error_param = -ENOMEM;
3185 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003186 goto err_alloc_tcomplete_failed;
3187 }
3188 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
3189
Todd Kjos1cfe6272017-05-24 13:33:28 -07003190 t->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003191
3192 if (reply)
3193 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen59878d72016-09-30 14:05:40 +02003194 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003195 proc->pid, thread->pid, t->debug_id,
3196 target_proc->pid, target_thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003197 (u64)tr->data.ptr.buffer,
3198 (u64)tr->data.ptr.offsets,
Martijn Coenen59878d72016-09-30 14:05:40 +02003199 (u64)tr->data_size, (u64)tr->offsets_size,
3200 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003201 else
3202 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen59878d72016-09-30 14:05:40 +02003203 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003204 proc->pid, thread->pid, t->debug_id,
3205 target_proc->pid, target_node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003206 (u64)tr->data.ptr.buffer,
3207 (u64)tr->data.ptr.offsets,
Martijn Coenen59878d72016-09-30 14:05:40 +02003208 (u64)tr->data_size, (u64)tr->offsets_size,
3209 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003210
3211 if (!reply && !(tr->flags & TF_ONE_WAY))
3212 t->from = thread;
3213 else
3214 t->from = NULL;
Tair Rzayev57bab7c2014-05-31 22:43:34 +03003215 t->sender_euid = task_euid(proc->tsk);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003216 t->to_proc = target_proc;
3217 t->to_thread = target_thread;
3218 t->code = tr->code;
3219 t->flags = tr->flags;
Martijn Coenen57b2ac62017-06-06 17:04:42 -07003220 if (!(t->flags & TF_ONE_WAY) &&
3221 binder_supported_policy(current->policy)) {
3222 /* Inherit supported policies for synchronous transactions */
3223 t->priority.sched_policy = current->policy;
3224 t->priority.prio = current->normal_prio;
3225 } else {
3226 /* Otherwise, fall back to the default priority */
3227 t->priority = target_proc->default_priority;
3228 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003229
Todd Kjos63e0afa2019-01-14 09:10:21 -08003230 if (target_node && target_node->txn_security_ctx) {
3231 u32 secid;
3232
3233 security_task_getsecid(proc->tsk, &secid);
3234 ret = security_secid_to_secctx(secid, &secctx, &secctx_sz);
3235 if (ret) {
3236 return_error = BR_FAILED_REPLY;
3237 return_error_param = ret;
3238 return_error_line = __LINE__;
3239 goto err_get_secctx_failed;
3240 }
3241 extra_buffers_size += ALIGN(secctx_sz, sizeof(u64));
3242 }
3243
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003244 trace_binder_transaction(reply, t, target_node);
3245
Todd Kjosd325d372016-10-10 10:40:53 -07003246 t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size,
Martijn Coenen59878d72016-09-30 14:05:40 +02003247 tr->offsets_size, extra_buffers_size,
3248 !reply && (t->flags & TF_ONE_WAY));
Todd Kjose598d172017-03-22 17:19:52 -07003249 if (IS_ERR(t->buffer)) {
3250 /*
3251 * -ESRCH indicates VMA cleared. The target is dying.
3252 */
3253 return_error_param = PTR_ERR(t->buffer);
3254 return_error = return_error_param == -ESRCH ?
3255 BR_DEAD_REPLY : BR_FAILED_REPLY;
3256 return_error_line = __LINE__;
3257 t->buffer = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003258 goto err_binder_alloc_buf_failed;
3259 }
Todd Kjos63e0afa2019-01-14 09:10:21 -08003260 if (secctx) {
3261 size_t buf_offset = ALIGN(tr->data_size, sizeof(void *)) +
3262 ALIGN(tr->offsets_size, sizeof(void *)) +
3263 ALIGN(extra_buffers_size, sizeof(void *)) -
3264 ALIGN(secctx_sz, sizeof(u64));
Todd Kjos63e0afa2019-01-14 09:10:21 -08003265
Todd Kjos8539b1e2019-02-08 10:35:20 -08003266 t->security_ctx = (uintptr_t)t->buffer->user_data + buf_offset;
Todd Kjos90a570c2019-02-08 10:35:15 -08003267 binder_alloc_copy_to_buffer(&target_proc->alloc,
3268 t->buffer, buf_offset,
3269 secctx, secctx_sz);
Todd Kjos63e0afa2019-01-14 09:10:21 -08003270 security_release_secctx(secctx, secctx_sz);
3271 secctx = NULL;
3272 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003273 t->buffer->debug_id = t->debug_id;
3274 t->buffer->transaction = t;
3275 t->buffer->target_node = target_node;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003276 trace_binder_transaction_alloc_buf(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003277
Todd Kjosd5049492019-02-08 10:35:14 -08003278 if (binder_alloc_copy_user_to_buffer(
3279 &target_proc->alloc,
3280 t->buffer, 0,
3281 (const void __user *)
3282 (uintptr_t)tr->data.ptr.buffer,
3283 tr->data_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303284 binder_user_error("%d:%d got transaction with invalid data ptr\n",
3285 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003286 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003287 return_error_param = -EFAULT;
3288 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003289 goto err_copy_data_failed;
3290 }
Todd Kjosd5049492019-02-08 10:35:14 -08003291 if (binder_alloc_copy_user_to_buffer(
3292 &target_proc->alloc,
3293 t->buffer,
3294 ALIGN(tr->data_size, sizeof(void *)),
3295 (const void __user *)
3296 (uintptr_t)tr->data.ptr.offsets,
3297 tr->offsets_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303298 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3299 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003300 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003301 return_error_param = -EFAULT;
3302 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003303 goto err_copy_data_failed;
3304 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08003305 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
3306 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
3307 proc->pid, thread->pid, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003308 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003309 return_error_param = -EINVAL;
3310 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003311 goto err_bad_offset;
3312 }
Martijn Coenen5a6da532016-09-30 14:10:07 +02003313 if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) {
3314 binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
3315 proc->pid, thread->pid,
Amit Pundir44cbb182017-02-01 12:53:45 +05303316 (u64)extra_buffers_size);
Martijn Coenen5a6da532016-09-30 14:10:07 +02003317 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003318 return_error_param = -EINVAL;
3319 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003320 goto err_bad_offset;
3321 }
Todd Kjos8539b1e2019-02-08 10:35:20 -08003322 off_start_offset = ALIGN(tr->data_size, sizeof(void *));
3323 buffer_offset = off_start_offset;
3324 off_end_offset = off_start_offset + tr->offsets_size;
3325 sg_buf_offset = ALIGN(off_end_offset, sizeof(void *));
3326 sg_buf_end_offset = sg_buf_offset + extra_buffers_size;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08003327 off_min = 0;
Todd Kjos8539b1e2019-02-08 10:35:20 -08003328 for (buffer_offset = off_start_offset; buffer_offset < off_end_offset;
3329 buffer_offset += sizeof(binder_size_t)) {
Martijn Coenen00c80372016-07-13 12:06:49 +02003330 struct binder_object_header *hdr;
Todd Kjos90a570c2019-02-08 10:35:15 -08003331 size_t object_size;
Todd Kjosd73356a2019-02-08 10:35:16 -08003332 struct binder_object object;
Todd Kjos90a570c2019-02-08 10:35:15 -08003333 binder_size_t object_offset;
Seunghun Lee10f62862014-05-01 01:30:23 +09003334
Todd Kjos90a570c2019-02-08 10:35:15 -08003335 binder_alloc_copy_from_buffer(&target_proc->alloc,
3336 &object_offset,
3337 t->buffer,
3338 buffer_offset,
3339 sizeof(object_offset));
Todd Kjosd73356a2019-02-08 10:35:16 -08003340 object_size = binder_get_object(target_proc, t->buffer,
3341 object_offset, &object);
Todd Kjos90a570c2019-02-08 10:35:15 -08003342 if (object_size == 0 || object_offset < off_min) {
Martijn Coenen00c80372016-07-13 12:06:49 +02003343 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 -08003344 proc->pid, thread->pid,
3345 (u64)object_offset,
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08003346 (u64)off_min,
Martijn Coenen00c80372016-07-13 12:06:49 +02003347 (u64)t->buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003348 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003349 return_error_param = -EINVAL;
3350 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003351 goto err_bad_offset;
3352 }
Martijn Coenen00c80372016-07-13 12:06:49 +02003353
Todd Kjosd73356a2019-02-08 10:35:16 -08003354 hdr = &object.hdr;
Todd Kjos90a570c2019-02-08 10:35:15 -08003355 off_min = object_offset + object_size;
Martijn Coenen00c80372016-07-13 12:06:49 +02003356 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003357 case BINDER_TYPE_BINDER:
3358 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003359 struct flat_binder_object *fp;
Seunghun Lee10f62862014-05-01 01:30:23 +09003360
Martijn Coenen00c80372016-07-13 12:06:49 +02003361 fp = to_flat_binder_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003362 ret = binder_translate_binder(fp, t, thread);
3363 if (ret < 0) {
Christian Engelmayer7d420432014-05-07 21:44:53 +02003364 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003365 return_error_param = ret;
3366 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003367 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003368 }
Todd Kjosd73356a2019-02-08 10:35:16 -08003369 binder_alloc_copy_to_buffer(&target_proc->alloc,
3370 t->buffer, object_offset,
3371 fp, sizeof(*fp));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003372 } break;
3373 case BINDER_TYPE_HANDLE:
3374 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003375 struct flat_binder_object *fp;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02003376
Martijn Coenen00c80372016-07-13 12:06:49 +02003377 fp = to_flat_binder_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003378 ret = binder_translate_handle(fp, t, thread);
3379 if (ret < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003380 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003381 return_error_param = ret;
3382 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003383 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003384 }
Todd Kjosd73356a2019-02-08 10:35:16 -08003385 binder_alloc_copy_to_buffer(&target_proc->alloc,
3386 t->buffer, object_offset,
3387 fp, sizeof(*fp));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003388 } break;
3389
3390 case BINDER_TYPE_FD: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003391 struct binder_fd_object *fp = to_binder_fd_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003392 int target_fd = binder_translate_fd(fp->fd, t, thread,
3393 in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003394
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003395 if (target_fd < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003396 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003397 return_error_param = target_fd;
3398 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003399 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003400 }
Martijn Coenen00c80372016-07-13 12:06:49 +02003401 fp->pad_binder = 0;
3402 fp->fd = target_fd;
Todd Kjosd73356a2019-02-08 10:35:16 -08003403 binder_alloc_copy_to_buffer(&target_proc->alloc,
3404 t->buffer, object_offset,
3405 fp, sizeof(*fp));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003406 } break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003407 case BINDER_TYPE_FDA: {
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003408 struct binder_object ptr_object;
3409 binder_size_t parent_offset;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003410 struct binder_fd_array_object *fda =
3411 to_binder_fd_array_object(hdr);
Todd Kjos8539b1e2019-02-08 10:35:20 -08003412 size_t num_valid = (buffer_offset - off_start_offset) *
3413 sizeof(binder_size_t);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003414 struct binder_buffer_object *parent =
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003415 binder_validate_ptr(target_proc, t->buffer,
3416 &ptr_object, fda->parent,
3417 off_start_offset,
3418 &parent_offset,
Todd Kjos8539b1e2019-02-08 10:35:20 -08003419 num_valid);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003420 if (!parent) {
3421 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
3422 proc->pid, thread->pid);
3423 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003424 return_error_param = -EINVAL;
3425 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003426 goto err_bad_parent;
3427 }
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003428 if (!binder_validate_fixup(target_proc, t->buffer,
3429 off_start_offset,
3430 parent_offset,
3431 fda->parent_offset,
3432 last_fixup_obj_off,
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003433 last_fixup_min_off)) {
3434 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
3435 proc->pid, thread->pid);
3436 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003437 return_error_param = -EINVAL;
3438 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003439 goto err_bad_parent;
3440 }
3441 ret = binder_translate_fd_array(fda, parent, t, thread,
3442 in_reply_to);
3443 if (ret < 0) {
3444 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003445 return_error_param = ret;
3446 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003447 goto err_translate_failed;
3448 }
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003449 last_fixup_obj_off = parent_offset;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003450 last_fixup_min_off =
3451 fda->parent_offset + sizeof(u32) * fda->num_fds;
3452 } break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003453 case BINDER_TYPE_PTR: {
3454 struct binder_buffer_object *bp =
3455 to_binder_buffer_object(hdr);
Todd Kjos8539b1e2019-02-08 10:35:20 -08003456 size_t buf_left = sg_buf_end_offset - sg_buf_offset;
3457 size_t num_valid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003458
Martijn Coenen5a6da532016-09-30 14:10:07 +02003459 if (bp->length > buf_left) {
3460 binder_user_error("%d:%d got transaction with too large buffer\n",
3461 proc->pid, thread->pid);
3462 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003463 return_error_param = -EINVAL;
3464 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003465 goto err_bad_offset;
3466 }
Todd Kjosd5049492019-02-08 10:35:14 -08003467 if (binder_alloc_copy_user_to_buffer(
3468 &target_proc->alloc,
3469 t->buffer,
3470 sg_buf_offset,
3471 (const void __user *)
3472 (uintptr_t)bp->buffer,
3473 bp->length)) {
Martijn Coenen5a6da532016-09-30 14:10:07 +02003474 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3475 proc->pid, thread->pid);
Todd Kjose598d172017-03-22 17:19:52 -07003476 return_error_param = -EFAULT;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003477 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003478 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003479 goto err_copy_data_failed;
3480 }
3481 /* Fixup buffer pointer to target proc address space */
Todd Kjos8539b1e2019-02-08 10:35:20 -08003482 bp->buffer = (uintptr_t)
3483 t->buffer->user_data + sg_buf_offset;
3484 sg_buf_offset += ALIGN(bp->length, sizeof(u64));
Martijn Coenen5a6da532016-09-30 14:10:07 +02003485
Todd Kjos8539b1e2019-02-08 10:35:20 -08003486 num_valid = (buffer_offset - off_start_offset) *
3487 sizeof(binder_size_t);
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003488 ret = binder_fixup_parent(t, thread, bp,
3489 off_start_offset,
Todd Kjos8539b1e2019-02-08 10:35:20 -08003490 num_valid,
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003491 last_fixup_obj_off,
Martijn Coenen5a6da532016-09-30 14:10:07 +02003492 last_fixup_min_off);
3493 if (ret < 0) {
3494 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003495 return_error_param = ret;
3496 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003497 goto err_translate_failed;
3498 }
Todd Kjosd73356a2019-02-08 10:35:16 -08003499 binder_alloc_copy_to_buffer(&target_proc->alloc,
3500 t->buffer, object_offset,
3501 bp, sizeof(*bp));
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003502 last_fixup_obj_off = object_offset;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003503 last_fixup_min_off = 0;
3504 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003505 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01003506 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
Martijn Coenen00c80372016-07-13 12:06:49 +02003507 proc->pid, thread->pid, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003508 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003509 return_error_param = -EINVAL;
3510 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003511 goto err_bad_object_type;
3512 }
3513 }
Todd Kjos8dedb0c2017-05-09 08:31:32 -07003514 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003515 t->work.type = BINDER_WORK_TRANSACTION;
Todd Kjos8dedb0c2017-05-09 08:31:32 -07003516
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003517 if (reply) {
Martijn Coenen1af61802017-10-19 15:04:46 +02003518 binder_enqueue_thread_work(thread, tcomplete);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003519 binder_inner_proc_lock(target_proc);
3520 if (target_thread->is_dead) {
3521 binder_inner_proc_unlock(target_proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07003522 goto err_dead_proc_or_thread;
Martijn Coenen995a36e2017-06-02 13:36:52 -07003523 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003524 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003525 binder_pop_transaction_ilocked(target_thread, in_reply_to);
Martijn Coenen1af61802017-10-19 15:04:46 +02003526 binder_enqueue_thread_work_ilocked(target_thread, &t->work);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003527 binder_inner_proc_unlock(target_proc);
Martijn Coenen053be422017-06-06 15:17:46 -07003528 wake_up_interruptible_sync(&target_thread->wait);
Martijn Coenenecd972d2017-05-26 10:48:56 -07003529 binder_restore_priority(current, in_reply_to->saved_priority);
Todd Kjos21ef40a2017-03-30 18:02:13 -07003530 binder_free_transaction(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003531 } else if (!(t->flags & TF_ONE_WAY)) {
3532 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003533 binder_inner_proc_lock(proc);
Martijn Coenendac2e9c2017-11-13 09:55:21 +01003534 /*
3535 * Defer the TRANSACTION_COMPLETE, so we don't return to
3536 * userspace immediately; this allows the target process to
3537 * immediately start processing this transaction, reducing
3538 * latency. We will then return the TRANSACTION_COMPLETE when
3539 * the target replies (or there is an error).
3540 */
3541 binder_enqueue_deferred_thread_work_ilocked(thread, tcomplete);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003542 t->need_reply = 1;
3543 t->from_parent = thread->transaction_stack;
3544 thread->transaction_stack = t;
Martijn Coenen995a36e2017-06-02 13:36:52 -07003545 binder_inner_proc_unlock(proc);
Martijn Coenen053be422017-06-06 15:17:46 -07003546 if (!binder_proc_transaction(t, target_proc, target_thread)) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07003547 binder_inner_proc_lock(proc);
3548 binder_pop_transaction_ilocked(thread, t);
3549 binder_inner_proc_unlock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07003550 goto err_dead_proc_or_thread;
3551 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003552 } else {
3553 BUG_ON(target_node == NULL);
3554 BUG_ON(t->buffer->async_transaction != 1);
Martijn Coenen1af61802017-10-19 15:04:46 +02003555 binder_enqueue_thread_work(thread, tcomplete);
Martijn Coenen053be422017-06-06 15:17:46 -07003556 if (!binder_proc_transaction(t, target_proc, NULL))
Todd Kjos2f993e22017-05-12 14:42:55 -07003557 goto err_dead_proc_or_thread;
Riley Andrewsb5968812015-09-01 12:42:07 -07003558 }
Todd Kjos2f993e22017-05-12 14:42:55 -07003559 if (target_thread)
3560 binder_thread_dec_tmpref(target_thread);
3561 binder_proc_dec_tmpref(target_proc);
Todd Kjos291d9682017-09-25 08:55:09 -07003562 if (target_node)
3563 binder_dec_node_tmpref(target_node);
Todd Kjos1cfe6272017-05-24 13:33:28 -07003564 /*
3565 * write barrier to synchronize with initialization
3566 * of log entry
3567 */
3568 smp_wmb();
3569 WRITE_ONCE(e->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003570 return;
3571
Todd Kjos2f993e22017-05-12 14:42:55 -07003572err_dead_proc_or_thread:
3573 return_error = BR_DEAD_REPLY;
3574 return_error_line = __LINE__;
Xu YiPing86578a02017-05-22 11:26:23 -07003575 binder_dequeue_work(proc, tcomplete);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003576err_translate_failed:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003577err_bad_object_type:
3578err_bad_offset:
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003579err_bad_parent:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003580err_copy_data_failed:
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003581 trace_binder_transaction_failed_buffer_release(t->buffer);
Todd Kjos8539b1e2019-02-08 10:35:20 -08003582 binder_transaction_buffer_release(target_proc, t->buffer,
3583 buffer_offset, true);
Todd Kjos291d9682017-09-25 08:55:09 -07003584 if (target_node)
3585 binder_dec_node_tmpref(target_node);
Todd Kjosc37162d2017-05-26 11:56:29 -07003586 target_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003587 t->buffer->transaction = NULL;
Todd Kjosd325d372016-10-10 10:40:53 -07003588 binder_alloc_free_buf(&target_proc->alloc, t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003589err_binder_alloc_buf_failed:
Todd Kjos63e0afa2019-01-14 09:10:21 -08003590 if (secctx)
3591 security_release_secctx(secctx, secctx_sz);
3592err_get_secctx_failed:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003593 kfree(tcomplete);
3594 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3595err_alloc_tcomplete_failed:
3596 kfree(t);
3597 binder_stats_deleted(BINDER_STAT_TRANSACTION);
3598err_alloc_t_failed:
3599err_bad_call_stack:
3600err_empty_call_stack:
3601err_dead_binder:
3602err_invalid_target_handle:
Todd Kjos2f993e22017-05-12 14:42:55 -07003603 if (target_thread)
3604 binder_thread_dec_tmpref(target_thread);
3605 if (target_proc)
3606 binder_proc_dec_tmpref(target_proc);
Todd Kjos291d9682017-09-25 08:55:09 -07003607 if (target_node) {
Todd Kjosc37162d2017-05-26 11:56:29 -07003608 binder_dec_node(target_node, 1, 0);
Todd Kjos291d9682017-09-25 08:55:09 -07003609 binder_dec_node_tmpref(target_node);
3610 }
Todd Kjosc37162d2017-05-26 11:56:29 -07003611
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003612 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Todd Kjose598d172017-03-22 17:19:52 -07003613 "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
3614 proc->pid, thread->pid, return_error, return_error_param,
3615 (u64)tr->data_size, (u64)tr->offsets_size,
3616 return_error_line);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003617
3618 {
3619 struct binder_transaction_log_entry *fe;
Seunghun Lee10f62862014-05-01 01:30:23 +09003620
Todd Kjose598d172017-03-22 17:19:52 -07003621 e->return_error = return_error;
3622 e->return_error_param = return_error_param;
3623 e->return_error_line = return_error_line;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003624 fe = binder_transaction_log_add(&binder_transaction_log_failed);
3625 *fe = *e;
Todd Kjos1cfe6272017-05-24 13:33:28 -07003626 /*
3627 * write barrier to synchronize with initialization
3628 * of log entry
3629 */
3630 smp_wmb();
3631 WRITE_ONCE(e->debug_id_done, t_debug_id);
3632 WRITE_ONCE(fe->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003633 }
3634
Todd Kjos858b8da2017-04-21 17:35:12 -07003635 BUG_ON(thread->return_error.cmd != BR_OK);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003636 if (in_reply_to) {
Martijn Coenenecd972d2017-05-26 10:48:56 -07003637 binder_restore_priority(current, in_reply_to->saved_priority);
Todd Kjos858b8da2017-04-21 17:35:12 -07003638 thread->return_error.cmd = BR_TRANSACTION_COMPLETE;
Martijn Coenen1af61802017-10-19 15:04:46 +02003639 binder_enqueue_thread_work(thread, &thread->return_error.work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003640 binder_send_failed_reply(in_reply_to, return_error);
Todd Kjos858b8da2017-04-21 17:35:12 -07003641 } else {
3642 thread->return_error.cmd = return_error;
Martijn Coenen1af61802017-10-19 15:04:46 +02003643 binder_enqueue_thread_work(thread, &thread->return_error.work);
Todd Kjos858b8da2017-04-21 17:35:12 -07003644 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003645}
3646
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02003647static int binder_thread_write(struct binder_proc *proc,
3648 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003649 binder_uintptr_t binder_buffer, size_t size,
3650 binder_size_t *consumed)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003651{
3652 uint32_t cmd;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003653 struct binder_context *context = proc->context;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003654 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003655 void __user *ptr = buffer + *consumed;
3656 void __user *end = buffer + size;
3657
Todd Kjos858b8da2017-04-21 17:35:12 -07003658 while (ptr < end && thread->return_error.cmd == BR_OK) {
Todd Kjosb0117bb2017-05-08 09:16:27 -07003659 int ret;
3660
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003661 if (get_user(cmd, (uint32_t __user *)ptr))
3662 return -EFAULT;
3663 ptr += sizeof(uint32_t);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003664 trace_binder_command(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003665 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07003666 atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]);
3667 atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]);
3668 atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003669 }
3670 switch (cmd) {
3671 case BC_INCREFS:
3672 case BC_ACQUIRE:
3673 case BC_RELEASE:
3674 case BC_DECREFS: {
3675 uint32_t target;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003676 const char *debug_string;
Todd Kjosb0117bb2017-05-08 09:16:27 -07003677 bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE;
3678 bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE;
3679 struct binder_ref_data rdata;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003680
3681 if (get_user(target, (uint32_t __user *)ptr))
3682 return -EFAULT;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003683
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003684 ptr += sizeof(uint32_t);
Todd Kjosb0117bb2017-05-08 09:16:27 -07003685 ret = -1;
3686 if (increment && !target) {
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003687 struct binder_node *ctx_mgr_node;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003688 mutex_lock(&context->context_mgr_node_lock);
3689 ctx_mgr_node = context->binder_context_mgr_node;
Todd Kjosb0117bb2017-05-08 09:16:27 -07003690 if (ctx_mgr_node)
3691 ret = binder_inc_ref_for_node(
3692 proc, ctx_mgr_node,
3693 strong, NULL, &rdata);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003694 mutex_unlock(&context->context_mgr_node_lock);
3695 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07003696 if (ret)
3697 ret = binder_update_ref_for_handle(
3698 proc, target, increment, strong,
3699 &rdata);
3700 if (!ret && rdata.desc != target) {
3701 binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
3702 proc->pid, thread->pid,
3703 target, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003704 }
3705 switch (cmd) {
3706 case BC_INCREFS:
3707 debug_string = "IncRefs";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003708 break;
3709 case BC_ACQUIRE:
3710 debug_string = "Acquire";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003711 break;
3712 case BC_RELEASE:
3713 debug_string = "Release";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003714 break;
3715 case BC_DECREFS:
3716 default:
3717 debug_string = "DecRefs";
Todd Kjosb0117bb2017-05-08 09:16:27 -07003718 break;
3719 }
3720 if (ret) {
3721 binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
3722 proc->pid, thread->pid, debug_string,
3723 strong, target, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003724 break;
3725 }
3726 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjosb0117bb2017-05-08 09:16:27 -07003727 "%d:%d %s ref %d desc %d s %d w %d\n",
3728 proc->pid, thread->pid, debug_string,
3729 rdata.debug_id, rdata.desc, rdata.strong,
3730 rdata.weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003731 break;
3732 }
3733 case BC_INCREFS_DONE:
3734 case BC_ACQUIRE_DONE: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003735 binder_uintptr_t node_ptr;
3736 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003737 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003738 bool free_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003739
Arve Hjønnevågda498892014-02-21 14:40:26 -08003740 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003741 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003742 ptr += sizeof(binder_uintptr_t);
3743 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003744 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003745 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003746 node = binder_get_node(proc, node_ptr);
3747 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003748 binder_user_error("%d:%d %s u%016llx no match\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003749 proc->pid, thread->pid,
3750 cmd == BC_INCREFS_DONE ?
3751 "BC_INCREFS_DONE" :
3752 "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003753 (u64)node_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003754 break;
3755 }
3756 if (cookie != node->cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003757 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003758 proc->pid, thread->pid,
3759 cmd == BC_INCREFS_DONE ?
3760 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003761 (u64)node_ptr, node->debug_id,
3762 (u64)cookie, (u64)node->cookie);
Todd Kjosf22abc72017-05-09 11:08:05 -07003763 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003764 break;
3765 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003766 binder_node_inner_lock(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003767 if (cmd == BC_ACQUIRE_DONE) {
3768 if (node->pending_strong_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303769 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003770 proc->pid, thread->pid,
3771 node->debug_id);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003772 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003773 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003774 break;
3775 }
3776 node->pending_strong_ref = 0;
3777 } else {
3778 if (node->pending_weak_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303779 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003780 proc->pid, thread->pid,
3781 node->debug_id);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003782 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003783 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003784 break;
3785 }
3786 node->pending_weak_ref = 0;
3787 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003788 free_node = binder_dec_node_nilocked(node,
3789 cmd == BC_ACQUIRE_DONE, 0);
3790 WARN_ON(free_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003791 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjosf22abc72017-05-09 11:08:05 -07003792 "%d:%d %s node %d ls %d lw %d tr %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003793 proc->pid, thread->pid,
3794 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Todd Kjosf22abc72017-05-09 11:08:05 -07003795 node->debug_id, node->local_strong_refs,
3796 node->local_weak_refs, node->tmp_refs);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003797 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003798 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003799 break;
3800 }
3801 case BC_ATTEMPT_ACQUIRE:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303802 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003803 return -EINVAL;
3804 case BC_ACQUIRE_RESULT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303805 pr_err("BC_ACQUIRE_RESULT not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003806 return -EINVAL;
3807
3808 case BC_FREE_BUFFER: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003809 binder_uintptr_t data_ptr;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003810 struct binder_buffer *buffer;
3811
Arve Hjønnevågda498892014-02-21 14:40:26 -08003812 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003813 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003814 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003815
Todd Kjos076072a2017-04-21 14:32:11 -07003816 buffer = binder_alloc_prepare_to_free(&proc->alloc,
3817 data_ptr);
Todd Kjosd29b73e2018-11-06 15:55:32 -08003818 if (IS_ERR_OR_NULL(buffer)) {
3819 if (PTR_ERR(buffer) == -EPERM) {
3820 binder_user_error(
3821 "%d:%d BC_FREE_BUFFER u%016llx matched unreturned or currently freeing buffer\n",
3822 proc->pid, thread->pid,
3823 (u64)data_ptr);
3824 } else {
3825 binder_user_error(
3826 "%d:%d BC_FREE_BUFFER u%016llx no match\n",
3827 proc->pid, thread->pid,
3828 (u64)data_ptr);
3829 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003830 break;
3831 }
3832 binder_debug(BINDER_DEBUG_FREE_BUFFER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003833 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
3834 proc->pid, thread->pid, (u64)data_ptr,
3835 buffer->debug_id,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003836 buffer->transaction ? "active" : "finished");
3837
3838 if (buffer->transaction) {
3839 buffer->transaction->buffer = NULL;
3840 buffer->transaction = NULL;
3841 }
3842 if (buffer->async_transaction && buffer->target_node) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003843 struct binder_node *buf_node;
3844 struct binder_work *w;
3845
3846 buf_node = buffer->target_node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003847 binder_node_inner_lock(buf_node);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003848 BUG_ON(!buf_node->has_async_transaction);
3849 BUG_ON(buf_node->proc != proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003850 w = binder_dequeue_work_head_ilocked(
3851 &buf_node->async_todo);
Martijn Coenen4501c042017-08-10 13:56:16 +02003852 if (!w) {
Gustavo A. R. Silvae62dd6f2018-01-23 12:04:27 -06003853 buf_node->has_async_transaction = false;
Martijn Coenen4501c042017-08-10 13:56:16 +02003854 } else {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003855 binder_enqueue_work_ilocked(
Martijn Coenen4501c042017-08-10 13:56:16 +02003856 w, &proc->todo);
3857 binder_wakeup_proc_ilocked(proc);
3858 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003859 binder_node_inner_unlock(buf_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003860 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003861 trace_binder_transaction_buffer_release(buffer);
Todd Kjos8539b1e2019-02-08 10:35:20 -08003862 binder_transaction_buffer_release(proc, buffer, 0, false);
Todd Kjosd325d372016-10-10 10:40:53 -07003863 binder_alloc_free_buf(&proc->alloc, buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003864 break;
3865 }
3866
Martijn Coenen5a6da532016-09-30 14:10:07 +02003867 case BC_TRANSACTION_SG:
3868 case BC_REPLY_SG: {
3869 struct binder_transaction_data_sg tr;
3870
3871 if (copy_from_user(&tr, ptr, sizeof(tr)))
3872 return -EFAULT;
3873 ptr += sizeof(tr);
3874 binder_transaction(proc, thread, &tr.transaction_data,
3875 cmd == BC_REPLY_SG, tr.buffers_size);
3876 break;
3877 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003878 case BC_TRANSACTION:
3879 case BC_REPLY: {
3880 struct binder_transaction_data tr;
3881
3882 if (copy_from_user(&tr, ptr, sizeof(tr)))
3883 return -EFAULT;
3884 ptr += sizeof(tr);
Martijn Coenen59878d72016-09-30 14:05:40 +02003885 binder_transaction(proc, thread, &tr,
3886 cmd == BC_REPLY, 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003887 break;
3888 }
3889
3890 case BC_REGISTER_LOOPER:
3891 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303892 "%d:%d BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003893 proc->pid, thread->pid);
Todd Kjosd600e902017-05-25 17:35:02 -07003894 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003895 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
3896 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303897 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003898 proc->pid, thread->pid);
3899 } else if (proc->requested_threads == 0) {
3900 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303901 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003902 proc->pid, thread->pid);
3903 } else {
3904 proc->requested_threads--;
3905 proc->requested_threads_started++;
3906 }
3907 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
Todd Kjosd600e902017-05-25 17:35:02 -07003908 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003909 break;
3910 case BC_ENTER_LOOPER:
3911 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303912 "%d:%d BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003913 proc->pid, thread->pid);
3914 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
3915 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303916 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003917 proc->pid, thread->pid);
3918 }
3919 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
3920 break;
3921 case BC_EXIT_LOOPER:
3922 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303923 "%d:%d BC_EXIT_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003924 proc->pid, thread->pid);
3925 thread->looper |= BINDER_LOOPER_STATE_EXITED;
3926 break;
3927
3928 case BC_REQUEST_DEATH_NOTIFICATION:
3929 case BC_CLEAR_DEATH_NOTIFICATION: {
3930 uint32_t target;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003931 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003932 struct binder_ref *ref;
Todd Kjos5346bf32016-10-20 16:43:34 -07003933 struct binder_ref_death *death = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003934
3935 if (get_user(target, (uint32_t __user *)ptr))
3936 return -EFAULT;
3937 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08003938 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003939 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003940 ptr += sizeof(binder_uintptr_t);
Todd Kjos5346bf32016-10-20 16:43:34 -07003941 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3942 /*
3943 * Allocate memory for death notification
3944 * before taking lock
3945 */
3946 death = kzalloc(sizeof(*death), GFP_KERNEL);
3947 if (death == NULL) {
3948 WARN_ON(thread->return_error.cmd !=
3949 BR_OK);
3950 thread->return_error.cmd = BR_ERROR;
Martijn Coenen1af61802017-10-19 15:04:46 +02003951 binder_enqueue_thread_work(
3952 thread,
3953 &thread->return_error.work);
Todd Kjos5346bf32016-10-20 16:43:34 -07003954 binder_debug(
3955 BINDER_DEBUG_FAILED_TRANSACTION,
3956 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
3957 proc->pid, thread->pid);
3958 break;
3959 }
3960 }
3961 binder_proc_lock(proc);
3962 ref = binder_get_ref_olocked(proc, target, false);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003963 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303964 binder_user_error("%d:%d %s invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003965 proc->pid, thread->pid,
3966 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3967 "BC_REQUEST_DEATH_NOTIFICATION" :
3968 "BC_CLEAR_DEATH_NOTIFICATION",
3969 target);
Todd Kjos5346bf32016-10-20 16:43:34 -07003970 binder_proc_unlock(proc);
3971 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003972 break;
3973 }
3974
3975 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003976 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003977 proc->pid, thread->pid,
3978 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3979 "BC_REQUEST_DEATH_NOTIFICATION" :
3980 "BC_CLEAR_DEATH_NOTIFICATION",
Todd Kjosb0117bb2017-05-08 09:16:27 -07003981 (u64)cookie, ref->data.debug_id,
3982 ref->data.desc, ref->data.strong,
3983 ref->data.weak, ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003984
Martijn Coenenf9eac642017-05-22 11:26:23 -07003985 binder_node_lock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003986 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3987 if (ref->death) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303988 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003989 proc->pid, thread->pid);
Martijn Coenenf9eac642017-05-22 11:26:23 -07003990 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003991 binder_proc_unlock(proc);
3992 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003993 break;
3994 }
3995 binder_stats_created(BINDER_STAT_DEATH);
3996 INIT_LIST_HEAD(&death->work.entry);
3997 death->cookie = cookie;
3998 ref->death = death;
3999 if (ref->node->proc == NULL) {
4000 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
Martijn Coenen3bdbe4c2017-08-10 13:50:52 +02004001
4002 binder_inner_proc_lock(proc);
4003 binder_enqueue_work_ilocked(
4004 &ref->death->work, &proc->todo);
4005 binder_wakeup_proc_ilocked(proc);
4006 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004007 }
4008 } else {
4009 if (ref->death == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05304010 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004011 proc->pid, thread->pid);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004012 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07004013 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004014 break;
4015 }
4016 death = ref->death;
4017 if (death->cookie != cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08004018 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004019 proc->pid, thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004020 (u64)death->cookie,
4021 (u64)cookie);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004022 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07004023 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004024 break;
4025 }
4026 ref->death = NULL;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004027 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004028 if (list_empty(&death->work.entry)) {
4029 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004030 if (thread->looper &
4031 (BINDER_LOOPER_STATE_REGISTERED |
4032 BINDER_LOOPER_STATE_ENTERED))
Martijn Coenen1af61802017-10-19 15:04:46 +02004033 binder_enqueue_thread_work_ilocked(
4034 thread,
4035 &death->work);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004036 else {
4037 binder_enqueue_work_ilocked(
4038 &death->work,
4039 &proc->todo);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004040 binder_wakeup_proc_ilocked(
Martijn Coenen053be422017-06-06 15:17:46 -07004041 proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004042 }
4043 } else {
4044 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
4045 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
4046 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004047 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004048 }
Martijn Coenenf9eac642017-05-22 11:26:23 -07004049 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07004050 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004051 } break;
4052 case BC_DEAD_BINDER_DONE: {
4053 struct binder_work *w;
Arve Hjønnevågda498892014-02-21 14:40:26 -08004054 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004055 struct binder_ref_death *death = NULL;
Seunghun Lee10f62862014-05-01 01:30:23 +09004056
Arve Hjønnevågda498892014-02-21 14:40:26 -08004057 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004058 return -EFAULT;
4059
Lisa Du7a64cd82016-02-17 09:32:52 +08004060 ptr += sizeof(cookie);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004061 binder_inner_proc_lock(proc);
4062 list_for_each_entry(w, &proc->delivered_death,
4063 entry) {
4064 struct binder_ref_death *tmp_death =
4065 container_of(w,
4066 struct binder_ref_death,
4067 work);
Seunghun Lee10f62862014-05-01 01:30:23 +09004068
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004069 if (tmp_death->cookie == cookie) {
4070 death = tmp_death;
4071 break;
4072 }
4073 }
4074 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Todd Kjosf540ce02018-02-07 13:57:37 -08004075 "%d:%d BC_DEAD_BINDER_DONE %016llx found %pK\n",
Arve Hjønnevågda498892014-02-21 14:40:26 -08004076 proc->pid, thread->pid, (u64)cookie,
4077 death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004078 if (death == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08004079 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
4080 proc->pid, thread->pid, (u64)cookie);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004081 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004082 break;
4083 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004084 binder_dequeue_work_ilocked(&death->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004085 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
4086 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004087 if (thread->looper &
4088 (BINDER_LOOPER_STATE_REGISTERED |
4089 BINDER_LOOPER_STATE_ENTERED))
Martijn Coenen1af61802017-10-19 15:04:46 +02004090 binder_enqueue_thread_work_ilocked(
4091 thread, &death->work);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004092 else {
4093 binder_enqueue_work_ilocked(
4094 &death->work,
4095 &proc->todo);
Martijn Coenen053be422017-06-06 15:17:46 -07004096 binder_wakeup_proc_ilocked(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004097 }
4098 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004099 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004100 } break;
4101
4102 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304103 pr_err("%d:%d unknown command %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004104 proc->pid, thread->pid, cmd);
4105 return -EINVAL;
4106 }
4107 *consumed = ptr - buffer;
4108 }
4109 return 0;
4110}
4111
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02004112static void binder_stat_br(struct binder_proc *proc,
4113 struct binder_thread *thread, uint32_t cmd)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004114{
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004115 trace_binder_return(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004116 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07004117 atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
4118 atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
4119 atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004120 }
4121}
4122
Todd Kjos60792612017-05-24 10:51:01 -07004123static int binder_put_node_cmd(struct binder_proc *proc,
4124 struct binder_thread *thread,
4125 void __user **ptrp,
4126 binder_uintptr_t node_ptr,
4127 binder_uintptr_t node_cookie,
4128 int node_debug_id,
4129 uint32_t cmd, const char *cmd_name)
4130{
4131 void __user *ptr = *ptrp;
4132
4133 if (put_user(cmd, (uint32_t __user *)ptr))
4134 return -EFAULT;
4135 ptr += sizeof(uint32_t);
4136
4137 if (put_user(node_ptr, (binder_uintptr_t __user *)ptr))
4138 return -EFAULT;
4139 ptr += sizeof(binder_uintptr_t);
4140
4141 if (put_user(node_cookie, (binder_uintptr_t __user *)ptr))
4142 return -EFAULT;
4143 ptr += sizeof(binder_uintptr_t);
4144
4145 binder_stat_br(proc, thread, cmd);
4146 binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n",
4147 proc->pid, thread->pid, cmd_name, node_debug_id,
4148 (u64)node_ptr, (u64)node_cookie);
4149
4150 *ptrp = ptr;
4151 return 0;
4152}
4153
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004154static int binder_wait_for_work(struct binder_thread *thread,
4155 bool do_proc_work)
4156{
4157 DEFINE_WAIT(wait);
4158 struct binder_proc *proc = thread->proc;
4159 int ret = 0;
4160
4161 freezer_do_not_count();
4162 binder_inner_proc_lock(proc);
4163 for (;;) {
4164 prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE);
4165 if (binder_has_work_ilocked(thread, do_proc_work))
4166 break;
4167 if (do_proc_work)
4168 list_add(&thread->waiting_thread_node,
4169 &proc->waiting_threads);
4170 binder_inner_proc_unlock(proc);
4171 schedule();
4172 binder_inner_proc_lock(proc);
4173 list_del_init(&thread->waiting_thread_node);
4174 if (signal_pending(current)) {
4175 ret = -ERESTARTSYS;
4176 break;
4177 }
4178 }
4179 finish_wait(&thread->wait, &wait);
4180 binder_inner_proc_unlock(proc);
4181 freezer_count();
4182
4183 return ret;
4184}
4185
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004186static int binder_thread_read(struct binder_proc *proc,
4187 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004188 binder_uintptr_t binder_buffer, size_t size,
4189 binder_size_t *consumed, int non_block)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004190{
Arve Hjønnevågda498892014-02-21 14:40:26 -08004191 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004192 void __user *ptr = buffer + *consumed;
4193 void __user *end = buffer + size;
4194
4195 int ret = 0;
4196 int wait_for_proc_work;
4197
4198 if (*consumed == 0) {
4199 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
4200 return -EFAULT;
4201 ptr += sizeof(uint32_t);
4202 }
4203
4204retry:
Martijn Coenen995a36e2017-06-02 13:36:52 -07004205 binder_inner_proc_lock(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004206 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
Martijn Coenen995a36e2017-06-02 13:36:52 -07004207 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004208
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004209 thread->looper |= BINDER_LOOPER_STATE_WAITING;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004210
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004211 trace_binder_wait_for_work(wait_for_proc_work,
4212 !!thread->transaction_stack,
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004213 !binder_worklist_empty(proc, &thread->todo));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004214 if (wait_for_proc_work) {
4215 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4216 BINDER_LOOPER_STATE_ENTERED))) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05304217 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 +09004218 proc->pid, thread->pid, thread->looper);
4219 wait_event_interruptible(binder_user_error_wait,
4220 binder_stop_on_user_error < 2);
4221 }
Martijn Coenenecd972d2017-05-26 10:48:56 -07004222 binder_restore_priority(current, proc->default_priority);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004223 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004224
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004225 if (non_block) {
4226 if (!binder_has_work(thread, wait_for_proc_work))
4227 ret = -EAGAIN;
4228 } else {
4229 ret = binder_wait_for_work(thread, wait_for_proc_work);
4230 }
4231
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004232 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
4233
4234 if (ret)
4235 return ret;
4236
4237 while (1) {
4238 uint32_t cmd;
Todd Kjos63e0afa2019-01-14 09:10:21 -08004239 struct binder_transaction_data_secctx tr;
4240 struct binder_transaction_data *trd = &tr.transaction_data;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004241 struct binder_work *w = NULL;
4242 struct list_head *list = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004243 struct binder_transaction *t = NULL;
Todd Kjos2f993e22017-05-12 14:42:55 -07004244 struct binder_thread *t_from;
Todd Kjos63e0afa2019-01-14 09:10:21 -08004245 size_t trsize = sizeof(*trd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004246
Todd Kjose7f23ed2017-03-21 13:06:01 -07004247 binder_inner_proc_lock(proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004248 if (!binder_worklist_empty_ilocked(&thread->todo))
4249 list = &thread->todo;
4250 else if (!binder_worklist_empty_ilocked(&proc->todo) &&
4251 wait_for_proc_work)
4252 list = &proc->todo;
4253 else {
4254 binder_inner_proc_unlock(proc);
4255
Dmitry Voytik395262a2014-09-08 18:16:34 +04004256 /* no data added */
Todd Kjos6798e6d2017-01-06 14:19:25 -08004257 if (ptr - buffer == 4 && !thread->looper_need_return)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004258 goto retry;
4259 break;
4260 }
4261
Todd Kjose7f23ed2017-03-21 13:06:01 -07004262 if (end - ptr < sizeof(tr) + 4) {
4263 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004264 break;
Todd Kjose7f23ed2017-03-21 13:06:01 -07004265 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004266 w = binder_dequeue_work_head_ilocked(list);
Martijn Coenen1af61802017-10-19 15:04:46 +02004267 if (binder_worklist_empty_ilocked(&thread->todo))
4268 thread->process_todo = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004269
4270 switch (w->type) {
4271 case BINDER_WORK_TRANSACTION: {
Todd Kjose7f23ed2017-03-21 13:06:01 -07004272 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004273 t = container_of(w, struct binder_transaction, work);
4274 } break;
Todd Kjos858b8da2017-04-21 17:35:12 -07004275 case BINDER_WORK_RETURN_ERROR: {
4276 struct binder_error *e = container_of(
4277 w, struct binder_error, work);
4278
4279 WARN_ON(e->cmd == BR_OK);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004280 binder_inner_proc_unlock(proc);
Todd Kjos858b8da2017-04-21 17:35:12 -07004281 if (put_user(e->cmd, (uint32_t __user *)ptr))
4282 return -EFAULT;
宋金时e1b1a8b2018-05-10 02:05:03 +00004283 cmd = e->cmd;
Todd Kjos858b8da2017-04-21 17:35:12 -07004284 e->cmd = BR_OK;
4285 ptr += sizeof(uint32_t);
4286
4287 binder_stat_br(proc, thread, cmd);
Todd Kjos858b8da2017-04-21 17:35:12 -07004288 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004289 case BINDER_WORK_TRANSACTION_COMPLETE: {
Todd Kjose7f23ed2017-03-21 13:06:01 -07004290 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004291 cmd = BR_TRANSACTION_COMPLETE;
4292 if (put_user(cmd, (uint32_t __user *)ptr))
4293 return -EFAULT;
4294 ptr += sizeof(uint32_t);
4295
4296 binder_stat_br(proc, thread, cmd);
4297 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304298 "%d:%d BR_TRANSACTION_COMPLETE\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004299 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004300 kfree(w);
4301 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4302 } break;
4303 case BINDER_WORK_NODE: {
4304 struct binder_node *node = container_of(w, struct binder_node, work);
Todd Kjos60792612017-05-24 10:51:01 -07004305 int strong, weak;
4306 binder_uintptr_t node_ptr = node->ptr;
4307 binder_uintptr_t node_cookie = node->cookie;
4308 int node_debug_id = node->debug_id;
4309 int has_weak_ref;
4310 int has_strong_ref;
4311 void __user *orig_ptr = ptr;
Seunghun Lee10f62862014-05-01 01:30:23 +09004312
Todd Kjos60792612017-05-24 10:51:01 -07004313 BUG_ON(proc != node->proc);
4314 strong = node->internal_strong_refs ||
4315 node->local_strong_refs;
4316 weak = !hlist_empty(&node->refs) ||
Todd Kjosf22abc72017-05-09 11:08:05 -07004317 node->local_weak_refs ||
4318 node->tmp_refs || strong;
Todd Kjos60792612017-05-24 10:51:01 -07004319 has_strong_ref = node->has_strong_ref;
4320 has_weak_ref = node->has_weak_ref;
4321
4322 if (weak && !has_weak_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004323 node->has_weak_ref = 1;
4324 node->pending_weak_ref = 1;
4325 node->local_weak_refs++;
Todd Kjos60792612017-05-24 10:51:01 -07004326 }
4327 if (strong && !has_strong_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004328 node->has_strong_ref = 1;
4329 node->pending_strong_ref = 1;
4330 node->local_strong_refs++;
Todd Kjos60792612017-05-24 10:51:01 -07004331 }
4332 if (!strong && has_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004333 node->has_strong_ref = 0;
Todd Kjos60792612017-05-24 10:51:01 -07004334 if (!weak && has_weak_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004335 node->has_weak_ref = 0;
Todd Kjos60792612017-05-24 10:51:01 -07004336 if (!weak && !strong) {
4337 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4338 "%d:%d node %d u%016llx c%016llx deleted\n",
4339 proc->pid, thread->pid,
4340 node_debug_id,
4341 (u64)node_ptr,
4342 (u64)node_cookie);
4343 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004344 binder_inner_proc_unlock(proc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004345 binder_node_lock(node);
4346 /*
4347 * Acquire the node lock before freeing the
4348 * node to serialize with other threads that
4349 * may have been holding the node lock while
4350 * decrementing this node (avoids race where
4351 * this thread frees while the other thread
4352 * is unlocking the node after the final
4353 * decrement)
4354 */
4355 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004356 binder_free_node(node);
4357 } else
4358 binder_inner_proc_unlock(proc);
4359
Todd Kjos60792612017-05-24 10:51:01 -07004360 if (weak && !has_weak_ref)
4361 ret = binder_put_node_cmd(
4362 proc, thread, &ptr, node_ptr,
4363 node_cookie, node_debug_id,
4364 BR_INCREFS, "BR_INCREFS");
4365 if (!ret && strong && !has_strong_ref)
4366 ret = binder_put_node_cmd(
4367 proc, thread, &ptr, node_ptr,
4368 node_cookie, node_debug_id,
4369 BR_ACQUIRE, "BR_ACQUIRE");
4370 if (!ret && !strong && has_strong_ref)
4371 ret = binder_put_node_cmd(
4372 proc, thread, &ptr, node_ptr,
4373 node_cookie, node_debug_id,
4374 BR_RELEASE, "BR_RELEASE");
4375 if (!ret && !weak && has_weak_ref)
4376 ret = binder_put_node_cmd(
4377 proc, thread, &ptr, node_ptr,
4378 node_cookie, node_debug_id,
4379 BR_DECREFS, "BR_DECREFS");
4380 if (orig_ptr == ptr)
4381 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4382 "%d:%d node %d u%016llx c%016llx state unchanged\n",
4383 proc->pid, thread->pid,
4384 node_debug_id,
4385 (u64)node_ptr,
4386 (u64)node_cookie);
4387 if (ret)
4388 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004389 } break;
4390 case BINDER_WORK_DEAD_BINDER:
4391 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4392 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4393 struct binder_ref_death *death;
4394 uint32_t cmd;
Martijn Coenenf9eac642017-05-22 11:26:23 -07004395 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004396
4397 death = container_of(w, struct binder_ref_death, work);
4398 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
4399 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
4400 else
4401 cmd = BR_DEAD_BINDER;
Martijn Coenenf9eac642017-05-22 11:26:23 -07004402 cookie = death->cookie;
4403
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004404 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004405 "%d:%d %s %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004406 proc->pid, thread->pid,
4407 cmd == BR_DEAD_BINDER ?
4408 "BR_DEAD_BINDER" :
4409 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
Martijn Coenenf9eac642017-05-22 11:26:23 -07004410 (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004411 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
Martijn Coenenf9eac642017-05-22 11:26:23 -07004412 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004413 kfree(death);
4414 binder_stats_deleted(BINDER_STAT_DEATH);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004415 } else {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004416 binder_enqueue_work_ilocked(
4417 w, &proc->delivered_death);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004418 binder_inner_proc_unlock(proc);
4419 }
Martijn Coenenf9eac642017-05-22 11:26:23 -07004420 if (put_user(cmd, (uint32_t __user *)ptr))
4421 return -EFAULT;
4422 ptr += sizeof(uint32_t);
4423 if (put_user(cookie,
4424 (binder_uintptr_t __user *)ptr))
4425 return -EFAULT;
4426 ptr += sizeof(binder_uintptr_t);
4427 binder_stat_br(proc, thread, cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004428 if (cmd == BR_DEAD_BINDER)
4429 goto done; /* DEAD_BINDER notifications can cause transactions */
4430 } break;
4431 }
4432
4433 if (!t)
4434 continue;
4435
4436 BUG_ON(t->buffer == NULL);
4437 if (t->buffer->target_node) {
4438 struct binder_node *target_node = t->buffer->target_node;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004439 struct binder_priority node_prio;
Seunghun Lee10f62862014-05-01 01:30:23 +09004440
Todd Kjos63e0afa2019-01-14 09:10:21 -08004441 trd->target.ptr = target_node->ptr;
4442 trd->cookie = target_node->cookie;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004443 node_prio.sched_policy = target_node->sched_policy;
4444 node_prio.prio = target_node->min_priority;
Martijn Coenenc46810c2017-06-23 10:13:43 -07004445 binder_transaction_priority(current, t, node_prio,
4446 target_node->inherit_rt);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004447 cmd = BR_TRANSACTION;
4448 } else {
Todd Kjos63e0afa2019-01-14 09:10:21 -08004449 trd->target.ptr = 0;
4450 trd->cookie = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004451 cmd = BR_REPLY;
4452 }
Todd Kjos63e0afa2019-01-14 09:10:21 -08004453 trd->code = t->code;
4454 trd->flags = t->flags;
4455 trd->sender_euid = from_kuid(current_user_ns(), t->sender_euid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004456
Todd Kjos2f993e22017-05-12 14:42:55 -07004457 t_from = binder_get_txn_from(t);
4458 if (t_from) {
4459 struct task_struct *sender = t_from->proc->tsk;
Seunghun Lee10f62862014-05-01 01:30:23 +09004460
Todd Kjos63e0afa2019-01-14 09:10:21 -08004461 trd->sender_pid =
4462 task_tgid_nr_ns(sender,
4463 task_active_pid_ns(current));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004464 } else {
Todd Kjos63e0afa2019-01-14 09:10:21 -08004465 trd->sender_pid = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004466 }
4467
Todd Kjos63e0afa2019-01-14 09:10:21 -08004468 trd->data_size = t->buffer->data_size;
4469 trd->offsets_size = t->buffer->offsets_size;
Todd Kjos8539b1e2019-02-08 10:35:20 -08004470 trd->data.ptr.buffer = (uintptr_t)t->buffer->user_data;
Todd Kjos63e0afa2019-01-14 09:10:21 -08004471 trd->data.ptr.offsets = trd->data.ptr.buffer +
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004472 ALIGN(t->buffer->data_size,
4473 sizeof(void *));
4474
Todd Kjos63e0afa2019-01-14 09:10:21 -08004475 tr.secctx = t->security_ctx;
4476 if (t->security_ctx) {
4477 cmd = BR_TRANSACTION_SEC_CTX;
4478 trsize = sizeof(tr);
4479 }
Todd Kjos2f993e22017-05-12 14:42:55 -07004480 if (put_user(cmd, (uint32_t __user *)ptr)) {
4481 if (t_from)
4482 binder_thread_dec_tmpref(t_from);
Martijn Coenen3217ccc2017-08-24 15:23:36 +02004483
4484 binder_cleanup_transaction(t, "put_user failed",
4485 BR_FAILED_REPLY);
4486
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004487 return -EFAULT;
Todd Kjos2f993e22017-05-12 14:42:55 -07004488 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004489 ptr += sizeof(uint32_t);
Todd Kjos63e0afa2019-01-14 09:10:21 -08004490 if (copy_to_user(ptr, &tr, trsize)) {
Todd Kjos2f993e22017-05-12 14:42:55 -07004491 if (t_from)
4492 binder_thread_dec_tmpref(t_from);
Martijn Coenen3217ccc2017-08-24 15:23:36 +02004493
4494 binder_cleanup_transaction(t, "copy_to_user failed",
4495 BR_FAILED_REPLY);
4496
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004497 return -EFAULT;
Todd Kjos2f993e22017-05-12 14:42:55 -07004498 }
Todd Kjos63e0afa2019-01-14 09:10:21 -08004499 ptr += trsize;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004500
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004501 trace_binder_transaction_received(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004502 binder_stat_br(proc, thread, cmd);
4503 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004504 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004505 proc->pid, thread->pid,
4506 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
Todd Kjos63e0afa2019-01-14 09:10:21 -08004507 (cmd == BR_TRANSACTION_SEC_CTX) ?
4508 "BR_TRANSACTION_SEC_CTX" : "BR_REPLY",
Todd Kjos2f993e22017-05-12 14:42:55 -07004509 t->debug_id, t_from ? t_from->proc->pid : 0,
4510 t_from ? t_from->pid : 0, cmd,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004511 t->buffer->data_size, t->buffer->offsets_size,
Todd Kjos63e0afa2019-01-14 09:10:21 -08004512 (u64)trd->data.ptr.buffer,
4513 (u64)trd->data.ptr.offsets);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004514
Todd Kjos2f993e22017-05-12 14:42:55 -07004515 if (t_from)
4516 binder_thread_dec_tmpref(t_from);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004517 t->buffer->allow_user_free = 1;
Todd Kjos63e0afa2019-01-14 09:10:21 -08004518 if (cmd != BR_REPLY && !(t->flags & TF_ONE_WAY)) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07004519 binder_inner_proc_lock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004520 t->to_parent = thread->transaction_stack;
4521 t->to_thread = thread;
4522 thread->transaction_stack = t;
Martijn Coenen995a36e2017-06-02 13:36:52 -07004523 binder_inner_proc_unlock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004524 } else {
Todd Kjos21ef40a2017-03-30 18:02:13 -07004525 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004526 }
4527 break;
4528 }
4529
4530done:
4531
4532 *consumed = ptr - buffer;
Todd Kjosd600e902017-05-25 17:35:02 -07004533 binder_inner_proc_lock(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004534 if (proc->requested_threads == 0 &&
4535 list_empty(&thread->proc->waiting_threads) &&
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004536 proc->requested_threads_started < proc->max_threads &&
4537 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4538 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
4539 /*spawn a new thread if we leave this out */) {
4540 proc->requested_threads++;
Todd Kjosd600e902017-05-25 17:35:02 -07004541 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004542 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304543 "%d:%d BR_SPAWN_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004544 proc->pid, thread->pid);
4545 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
4546 return -EFAULT;
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07004547 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
Todd Kjosd600e902017-05-25 17:35:02 -07004548 } else
4549 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004550 return 0;
4551}
4552
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004553static void binder_release_work(struct binder_proc *proc,
4554 struct list_head *list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004555{
4556 struct binder_work *w;
Seunghun Lee10f62862014-05-01 01:30:23 +09004557
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004558 while (1) {
4559 w = binder_dequeue_work_head(proc, list);
4560 if (!w)
4561 return;
4562
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004563 switch (w->type) {
4564 case BINDER_WORK_TRANSACTION: {
4565 struct binder_transaction *t;
4566
4567 t = container_of(w, struct binder_transaction, work);
Martijn Coenen3217ccc2017-08-24 15:23:36 +02004568
4569 binder_cleanup_transaction(t, "process died.",
4570 BR_DEAD_REPLY);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004571 } break;
Todd Kjos858b8da2017-04-21 17:35:12 -07004572 case BINDER_WORK_RETURN_ERROR: {
4573 struct binder_error *e = container_of(
4574 w, struct binder_error, work);
4575
4576 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4577 "undelivered TRANSACTION_ERROR: %u\n",
4578 e->cmd);
4579 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004580 case BINDER_WORK_TRANSACTION_COMPLETE: {
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004581 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304582 "undelivered TRANSACTION_COMPLETE\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004583 kfree(w);
4584 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4585 } break;
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004586 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4587 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4588 struct binder_ref_death *death;
4589
4590 death = container_of(w, struct binder_ref_death, work);
4591 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004592 "undelivered death notification, %016llx\n",
4593 (u64)death->cookie);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004594 kfree(death);
4595 binder_stats_deleted(BINDER_STAT_DEATH);
4596 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004597 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304598 pr_err("unexpected work type, %d, not freed\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004599 w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004600 break;
4601 }
4602 }
4603
4604}
4605
Todd Kjosb4827902017-05-25 15:52:17 -07004606static struct binder_thread *binder_get_thread_ilocked(
4607 struct binder_proc *proc, struct binder_thread *new_thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004608{
4609 struct binder_thread *thread = NULL;
4610 struct rb_node *parent = NULL;
4611 struct rb_node **p = &proc->threads.rb_node;
4612
4613 while (*p) {
4614 parent = *p;
4615 thread = rb_entry(parent, struct binder_thread, rb_node);
4616
4617 if (current->pid < thread->pid)
4618 p = &(*p)->rb_left;
4619 else if (current->pid > thread->pid)
4620 p = &(*p)->rb_right;
4621 else
Todd Kjosb4827902017-05-25 15:52:17 -07004622 return thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004623 }
Todd Kjosb4827902017-05-25 15:52:17 -07004624 if (!new_thread)
4625 return NULL;
4626 thread = new_thread;
4627 binder_stats_created(BINDER_STAT_THREAD);
4628 thread->proc = proc;
4629 thread->pid = current->pid;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004630 get_task_struct(current);
4631 thread->task = current;
Todd Kjosb4827902017-05-25 15:52:17 -07004632 atomic_set(&thread->tmp_ref, 0);
4633 init_waitqueue_head(&thread->wait);
4634 INIT_LIST_HEAD(&thread->todo);
4635 rb_link_node(&thread->rb_node, parent, p);
4636 rb_insert_color(&thread->rb_node, &proc->threads);
4637 thread->looper_need_return = true;
4638 thread->return_error.work.type = BINDER_WORK_RETURN_ERROR;
4639 thread->return_error.cmd = BR_OK;
4640 thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR;
4641 thread->reply_error.cmd = BR_OK;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004642 INIT_LIST_HEAD(&new_thread->waiting_thread_node);
Todd Kjosb4827902017-05-25 15:52:17 -07004643 return thread;
4644}
4645
4646static struct binder_thread *binder_get_thread(struct binder_proc *proc)
4647{
4648 struct binder_thread *thread;
4649 struct binder_thread *new_thread;
4650
4651 binder_inner_proc_lock(proc);
4652 thread = binder_get_thread_ilocked(proc, NULL);
4653 binder_inner_proc_unlock(proc);
4654 if (!thread) {
4655 new_thread = kzalloc(sizeof(*thread), GFP_KERNEL);
4656 if (new_thread == NULL)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004657 return NULL;
Todd Kjosb4827902017-05-25 15:52:17 -07004658 binder_inner_proc_lock(proc);
4659 thread = binder_get_thread_ilocked(proc, new_thread);
4660 binder_inner_proc_unlock(proc);
4661 if (thread != new_thread)
4662 kfree(new_thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004663 }
4664 return thread;
4665}
4666
Todd Kjos2f993e22017-05-12 14:42:55 -07004667static void binder_free_proc(struct binder_proc *proc)
4668{
4669 BUG_ON(!list_empty(&proc->todo));
4670 BUG_ON(!list_empty(&proc->delivered_death));
4671 binder_alloc_deferred_release(&proc->alloc);
4672 put_task_struct(proc->tsk);
4673 binder_stats_deleted(BINDER_STAT_PROC);
4674 kfree(proc);
4675}
4676
4677static void binder_free_thread(struct binder_thread *thread)
4678{
4679 BUG_ON(!list_empty(&thread->todo));
4680 binder_stats_deleted(BINDER_STAT_THREAD);
4681 binder_proc_dec_tmpref(thread->proc);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004682 put_task_struct(thread->task);
Todd Kjos2f993e22017-05-12 14:42:55 -07004683 kfree(thread);
4684}
4685
4686static int binder_thread_release(struct binder_proc *proc,
4687 struct binder_thread *thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004688{
4689 struct binder_transaction *t;
4690 struct binder_transaction *send_reply = NULL;
4691 int active_transactions = 0;
Todd Kjos2f993e22017-05-12 14:42:55 -07004692 struct binder_transaction *last_t = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004693
Todd Kjosb4827902017-05-25 15:52:17 -07004694 binder_inner_proc_lock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07004695 /*
4696 * take a ref on the proc so it survives
4697 * after we remove this thread from proc->threads.
4698 * The corresponding dec is when we actually
4699 * free the thread in binder_free_thread()
4700 */
4701 proc->tmp_ref++;
4702 /*
4703 * take a ref on this thread to ensure it
4704 * survives while we are releasing it
4705 */
4706 atomic_inc(&thread->tmp_ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004707 rb_erase(&thread->rb_node, &proc->threads);
4708 t = thread->transaction_stack;
Todd Kjos2f993e22017-05-12 14:42:55 -07004709 if (t) {
4710 spin_lock(&t->lock);
4711 if (t->to_thread == thread)
4712 send_reply = t;
4713 }
4714 thread->is_dead = true;
4715
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004716 while (t) {
Todd Kjos2f993e22017-05-12 14:42:55 -07004717 last_t = t;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004718 active_transactions++;
4719 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304720 "release %d:%d transaction %d %s, still active\n",
4721 proc->pid, thread->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004722 t->debug_id,
4723 (t->to_thread == thread) ? "in" : "out");
4724
4725 if (t->to_thread == thread) {
4726 t->to_proc = NULL;
4727 t->to_thread = NULL;
4728 if (t->buffer) {
4729 t->buffer->transaction = NULL;
4730 t->buffer = NULL;
4731 }
4732 t = t->to_parent;
4733 } else if (t->from == thread) {
4734 t->from = NULL;
4735 t = t->from_parent;
4736 } else
4737 BUG();
Todd Kjos2f993e22017-05-12 14:42:55 -07004738 spin_unlock(&last_t->lock);
4739 if (t)
4740 spin_lock(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004741 }
Martijn Coenen550c01d2018-01-05 11:27:07 +01004742
4743 /*
4744 * If this thread used poll, make sure we remove the waitqueue
4745 * from any epoll data structures holding it with POLLFREE.
4746 * waitqueue_active() is safe to use here because we're holding
4747 * the inner lock.
4748 */
4749 if ((thread->looper & BINDER_LOOPER_STATE_POLL) &&
4750 waitqueue_active(&thread->wait)) {
4751 wake_up_poll(&thread->wait, POLLHUP | POLLFREE);
4752 }
4753
Todd Kjosb4827902017-05-25 15:52:17 -07004754 binder_inner_proc_unlock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07004755
Martijn Coenen72766d72018-02-16 09:47:15 +01004756 /*
4757 * This is needed to avoid races between wake_up_poll() above and
4758 * and ep_remove_waitqueue() called for other reasons (eg the epoll file
4759 * descriptor being closed); ep_remove_waitqueue() holds an RCU read
4760 * lock, so we can be sure it's done after calling synchronize_rcu().
4761 */
4762 if (thread->looper & BINDER_LOOPER_STATE_POLL)
4763 synchronize_rcu();
4764
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004765 if (send_reply)
4766 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004767 binder_release_work(proc, &thread->todo);
Todd Kjos2f993e22017-05-12 14:42:55 -07004768 binder_thread_dec_tmpref(thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004769 return active_transactions;
4770}
4771
4772static unsigned int binder_poll(struct file *filp,
4773 struct poll_table_struct *wait)
4774{
4775 struct binder_proc *proc = filp->private_data;
4776 struct binder_thread *thread = NULL;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004777 bool wait_for_proc_work;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004778
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004779 thread = binder_get_thread(proc);
Greg Kroah-Hartman6e463bb2018-02-28 17:17:14 +01004780 if (!thread)
Eric Biggers4be5a282018-01-30 23:11:24 -08004781 return POLLERR;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004782
Martijn Coenen995a36e2017-06-02 13:36:52 -07004783 binder_inner_proc_lock(thread->proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004784 thread->looper |= BINDER_LOOPER_STATE_POLL;
4785 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
4786
Martijn Coenen995a36e2017-06-02 13:36:52 -07004787 binder_inner_proc_unlock(thread->proc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004788
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004789 poll_wait(filp, &thread->wait, wait);
4790
Martijn Coenen47810932017-08-10 12:32:00 +02004791 if (binder_has_work(thread, wait_for_proc_work))
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004792 return POLLIN;
4793
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004794 return 0;
4795}
4796
Tair Rzayev78260ac2014-06-03 22:27:21 +03004797static int binder_ioctl_write_read(struct file *filp,
4798 unsigned int cmd, unsigned long arg,
4799 struct binder_thread *thread)
4800{
4801 int ret = 0;
4802 struct binder_proc *proc = filp->private_data;
4803 unsigned int size = _IOC_SIZE(cmd);
4804 void __user *ubuf = (void __user *)arg;
4805 struct binder_write_read bwr;
4806
4807 if (size != sizeof(struct binder_write_read)) {
4808 ret = -EINVAL;
4809 goto out;
4810 }
4811 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
4812 ret = -EFAULT;
4813 goto out;
4814 }
4815 binder_debug(BINDER_DEBUG_READ_WRITE,
4816 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
4817 proc->pid, thread->pid,
4818 (u64)bwr.write_size, (u64)bwr.write_buffer,
4819 (u64)bwr.read_size, (u64)bwr.read_buffer);
4820
4821 if (bwr.write_size > 0) {
4822 ret = binder_thread_write(proc, thread,
4823 bwr.write_buffer,
4824 bwr.write_size,
4825 &bwr.write_consumed);
4826 trace_binder_write_done(ret);
4827 if (ret < 0) {
4828 bwr.read_consumed = 0;
4829 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4830 ret = -EFAULT;
4831 goto out;
4832 }
4833 }
4834 if (bwr.read_size > 0) {
4835 ret = binder_thread_read(proc, thread, bwr.read_buffer,
4836 bwr.read_size,
4837 &bwr.read_consumed,
4838 filp->f_flags & O_NONBLOCK);
4839 trace_binder_read_done(ret);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004840 binder_inner_proc_lock(proc);
4841 if (!binder_worklist_empty_ilocked(&proc->todo))
Martijn Coenen053be422017-06-06 15:17:46 -07004842 binder_wakeup_proc_ilocked(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004843 binder_inner_proc_unlock(proc);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004844 if (ret < 0) {
4845 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4846 ret = -EFAULT;
4847 goto out;
4848 }
4849 }
4850 binder_debug(BINDER_DEBUG_READ_WRITE,
4851 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
4852 proc->pid, thread->pid,
4853 (u64)bwr.write_consumed, (u64)bwr.write_size,
4854 (u64)bwr.read_consumed, (u64)bwr.read_size);
4855 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
4856 ret = -EFAULT;
4857 goto out;
4858 }
4859out:
4860 return ret;
4861}
4862
Todd Kjos63e0afa2019-01-14 09:10:21 -08004863static int binder_ioctl_set_ctx_mgr(struct file *filp,
4864 struct flat_binder_object *fbo)
Tair Rzayev78260ac2014-06-03 22:27:21 +03004865{
4866 int ret = 0;
4867 struct binder_proc *proc = filp->private_data;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004868 struct binder_context *context = proc->context;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004869 struct binder_node *new_node;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004870 kuid_t curr_euid = current_euid();
4871
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004872 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004873 if (context->binder_context_mgr_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004874 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
4875 ret = -EBUSY;
4876 goto out;
4877 }
Stephen Smalley79af7302015-01-21 10:54:10 -05004878 ret = security_binder_set_context_mgr(proc->tsk);
4879 if (ret < 0)
4880 goto out;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004881 if (uid_valid(context->binder_context_mgr_uid)) {
4882 if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004883 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
4884 from_kuid(&init_user_ns, curr_euid),
4885 from_kuid(&init_user_ns,
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004886 context->binder_context_mgr_uid));
Tair Rzayev78260ac2014-06-03 22:27:21 +03004887 ret = -EPERM;
4888 goto out;
4889 }
4890 } else {
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004891 context->binder_context_mgr_uid = curr_euid;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004892 }
Todd Kjos63e0afa2019-01-14 09:10:21 -08004893 new_node = binder_new_node(proc, fbo);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004894 if (!new_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004895 ret = -ENOMEM;
4896 goto out;
4897 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004898 binder_node_lock(new_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004899 new_node->local_weak_refs++;
4900 new_node->local_strong_refs++;
4901 new_node->has_strong_ref = 1;
4902 new_node->has_weak_ref = 1;
4903 context->binder_context_mgr_node = new_node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004904 binder_node_unlock(new_node);
Todd Kjosf22abc72017-05-09 11:08:05 -07004905 binder_put_node(new_node);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004906out:
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004907 mutex_unlock(&context->context_mgr_node_lock);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004908 return ret;
4909}
4910
Martijn Coenen1c57ba42018-08-25 13:50:56 -07004911static int binder_ioctl_get_node_info_for_ref(struct binder_proc *proc,
4912 struct binder_node_info_for_ref *info)
4913{
4914 struct binder_node *node;
4915 struct binder_context *context = proc->context;
4916 __u32 handle = info->handle;
4917
4918 if (info->strong_count || info->weak_count || info->reserved1 ||
4919 info->reserved2 || info->reserved3) {
4920 binder_user_error("%d BINDER_GET_NODE_INFO_FOR_REF: only handle may be non-zero.",
4921 proc->pid);
4922 return -EINVAL;
4923 }
4924
4925 /* This ioctl may only be used by the context manager */
4926 mutex_lock(&context->context_mgr_node_lock);
4927 if (!context->binder_context_mgr_node ||
4928 context->binder_context_mgr_node->proc != proc) {
4929 mutex_unlock(&context->context_mgr_node_lock);
4930 return -EPERM;
4931 }
4932 mutex_unlock(&context->context_mgr_node_lock);
4933
4934 node = binder_get_node_from_ref(proc, handle, true, NULL);
4935 if (!node)
4936 return -EINVAL;
4937
4938 info->strong_count = node->local_strong_refs +
4939 node->internal_strong_refs;
4940 info->weak_count = node->local_weak_refs;
4941
4942 binder_put_node(node);
4943
4944 return 0;
4945}
4946
Colin Cross833babb32017-06-20 13:54:44 -07004947static int binder_ioctl_get_node_debug_info(struct binder_proc *proc,
4948 struct binder_node_debug_info *info) {
4949 struct rb_node *n;
4950 binder_uintptr_t ptr = info->ptr;
4951
4952 memset(info, 0, sizeof(*info));
4953
4954 binder_inner_proc_lock(proc);
4955 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
4956 struct binder_node *node = rb_entry(n, struct binder_node,
4957 rb_node);
4958 if (node->ptr > ptr) {
4959 info->ptr = node->ptr;
4960 info->cookie = node->cookie;
4961 info->has_strong_ref = node->has_strong_ref;
4962 info->has_weak_ref = node->has_weak_ref;
4963 break;
4964 }
4965 }
4966 binder_inner_proc_unlock(proc);
4967
4968 return 0;
4969}
4970
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004971static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4972{
4973 int ret;
4974 struct binder_proc *proc = filp->private_data;
4975 struct binder_thread *thread;
4976 unsigned int size = _IOC_SIZE(cmd);
4977 void __user *ubuf = (void __user *)arg;
4978
Tair Rzayev78260ac2014-06-03 22:27:21 +03004979 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
4980 proc->pid, current->pid, cmd, arg);*/
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004981
Sherry Yang435416b2017-06-22 14:37:45 -07004982 binder_selftest_alloc(&proc->alloc);
4983
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004984 trace_binder_ioctl(cmd, arg);
4985
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004986 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4987 if (ret)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004988 goto err_unlocked;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004989
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004990 thread = binder_get_thread(proc);
4991 if (thread == NULL) {
4992 ret = -ENOMEM;
4993 goto err;
4994 }
4995
4996 switch (cmd) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004997 case BINDER_WRITE_READ:
4998 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
4999 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005000 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005001 break;
Todd Kjosd600e902017-05-25 17:35:02 -07005002 case BINDER_SET_MAX_THREADS: {
5003 int max_threads;
5004
5005 if (copy_from_user(&max_threads, ubuf,
5006 sizeof(max_threads))) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005007 ret = -EINVAL;
5008 goto err;
5009 }
Todd Kjosd600e902017-05-25 17:35:02 -07005010 binder_inner_proc_lock(proc);
5011 proc->max_threads = max_threads;
5012 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005013 break;
Todd Kjosd600e902017-05-25 17:35:02 -07005014 }
Todd Kjos63e0afa2019-01-14 09:10:21 -08005015 case BINDER_SET_CONTEXT_MGR_EXT: {
5016 struct flat_binder_object fbo;
5017
5018 if (copy_from_user(&fbo, ubuf, sizeof(fbo))) {
5019 ret = -EINVAL;
5020 goto err;
5021 }
5022 ret = binder_ioctl_set_ctx_mgr(filp, &fbo);
5023 if (ret)
5024 goto err;
5025 break;
5026 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005027 case BINDER_SET_CONTEXT_MGR:
Todd Kjos63e0afa2019-01-14 09:10:21 -08005028 ret = binder_ioctl_set_ctx_mgr(filp, NULL);
Tair Rzayev78260ac2014-06-03 22:27:21 +03005029 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005030 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005031 break;
5032 case BINDER_THREAD_EXIT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05305033 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005034 proc->pid, thread->pid);
Todd Kjos2f993e22017-05-12 14:42:55 -07005035 binder_thread_release(proc, thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005036 thread = NULL;
5037 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02005038 case BINDER_VERSION: {
5039 struct binder_version __user *ver = ubuf;
5040
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005041 if (size != sizeof(struct binder_version)) {
5042 ret = -EINVAL;
5043 goto err;
5044 }
Mathieu Maret36c89c02014-04-15 12:03:05 +02005045 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
5046 &ver->protocol_version)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005047 ret = -EINVAL;
5048 goto err;
5049 }
5050 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02005051 }
Martijn Coenen1c57ba42018-08-25 13:50:56 -07005052 case BINDER_GET_NODE_INFO_FOR_REF: {
5053 struct binder_node_info_for_ref info;
5054
5055 if (copy_from_user(&info, ubuf, sizeof(info))) {
5056 ret = -EFAULT;
5057 goto err;
5058 }
5059
5060 ret = binder_ioctl_get_node_info_for_ref(proc, &info);
5061 if (ret < 0)
5062 goto err;
5063
5064 if (copy_to_user(ubuf, &info, sizeof(info))) {
5065 ret = -EFAULT;
5066 goto err;
5067 }
5068
5069 break;
5070 }
Colin Cross833babb32017-06-20 13:54:44 -07005071 case BINDER_GET_NODE_DEBUG_INFO: {
5072 struct binder_node_debug_info info;
5073
5074 if (copy_from_user(&info, ubuf, sizeof(info))) {
5075 ret = -EFAULT;
5076 goto err;
5077 }
5078
5079 ret = binder_ioctl_get_node_debug_info(proc, &info);
5080 if (ret < 0)
5081 goto err;
5082
5083 if (copy_to_user(ubuf, &info, sizeof(info))) {
5084 ret = -EFAULT;
5085 goto err;
5086 }
5087 break;
5088 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005089 default:
5090 ret = -EINVAL;
5091 goto err;
5092 }
5093 ret = 0;
5094err:
5095 if (thread)
Todd Kjos6798e6d2017-01-06 14:19:25 -08005096 thread->looper_need_return = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005097 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
5098 if (ret && ret != -ERESTARTSYS)
Anmol Sarma56b468f2012-10-30 22:35:43 +05305099 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 -07005100err_unlocked:
5101 trace_binder_ioctl_done(ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005102 return ret;
5103}
5104
5105static void binder_vma_open(struct vm_area_struct *vma)
5106{
5107 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09005108
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005109 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05305110 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005111 proc->pid, vma->vm_start, vma->vm_end,
5112 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
5113 (unsigned long)pgprot_val(vma->vm_page_prot));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005114}
5115
5116static void binder_vma_close(struct vm_area_struct *vma)
5117{
5118 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09005119
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005120 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05305121 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005122 proc->pid, vma->vm_start, vma->vm_end,
5123 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
5124 (unsigned long)pgprot_val(vma->vm_page_prot));
Todd Kjosd325d372016-10-10 10:40:53 -07005125 binder_alloc_vma_close(&proc->alloc);
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005126 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005127}
5128
Vinayak Menonddac7d52014-06-02 18:17:59 +05305129static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
5130{
5131 return VM_FAULT_SIGBUS;
5132}
5133
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07005134static const struct vm_operations_struct binder_vm_ops = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005135 .open = binder_vma_open,
5136 .close = binder_vma_close,
Vinayak Menonddac7d52014-06-02 18:17:59 +05305137 .fault = binder_vm_fault,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005138};
5139
Todd Kjosd325d372016-10-10 10:40:53 -07005140static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
5141{
5142 int ret;
5143 struct binder_proc *proc = filp->private_data;
5144 const char *failure_string;
5145
5146 if (proc->tsk != current->group_leader)
5147 return -EINVAL;
5148
5149 if ((vma->vm_end - vma->vm_start) > SZ_4M)
5150 vma->vm_end = vma->vm_start + SZ_4M;
5151
5152 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5153 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
5154 __func__, 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));
5157
5158 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
5159 ret = -EPERM;
5160 failure_string = "bad vm_flags";
5161 goto err_bad_arg;
5162 }
Minchan Kim2cafd5b2018-05-07 23:15:37 +09005163 vma->vm_flags |= VM_DONTCOPY | VM_MIXEDMAP;
5164 vma->vm_flags &= ~VM_MAYWRITE;
5165
Todd Kjosd325d372016-10-10 10:40:53 -07005166 vma->vm_ops = &binder_vm_ops;
5167 vma->vm_private_data = proc;
5168
5169 ret = binder_alloc_mmap_handler(&proc->alloc, vma);
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005170 if (ret)
5171 return ret;
Todd Kjosfbb43392017-11-27 09:32:33 -08005172 mutex_lock(&proc->files_lock);
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005173 proc->files = get_files_struct(current);
Todd Kjosfbb43392017-11-27 09:32:33 -08005174 mutex_unlock(&proc->files_lock);
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005175 return 0;
Todd Kjosd325d372016-10-10 10:40:53 -07005176
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005177err_bad_arg:
Elad Wexler6b646402017-12-29 11:03:37 +02005178 pr_err("%s: %d %lx-%lx %s failed %d\n", __func__,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005179 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
5180 return ret;
5181}
5182
5183static int binder_open(struct inode *nodp, struct file *filp)
5184{
5185 struct binder_proc *proc;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005186 struct binder_device *binder_dev;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005187
Elad Wexler6b646402017-12-29 11:03:37 +02005188 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "%s: %d:%d\n", __func__,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005189 current->group_leader->pid, current->pid);
5190
5191 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
5192 if (proc == NULL)
5193 return -ENOMEM;
Todd Kjosfc7a7e22017-05-29 16:44:24 -07005194 spin_lock_init(&proc->inner_lock);
5195 spin_lock_init(&proc->outer_lock);
Martijn Coenen872c26e2017-03-07 15:51:18 +01005196 get_task_struct(current->group_leader);
5197 proc->tsk = current->group_leader;
Todd Kjosfbb43392017-11-27 09:32:33 -08005198 mutex_init(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005199 INIT_LIST_HEAD(&proc->todo);
Martijn Coenen57b2ac62017-06-06 17:04:42 -07005200 if (binder_supported_policy(current->policy)) {
5201 proc->default_priority.sched_policy = current->policy;
5202 proc->default_priority.prio = current->normal_prio;
5203 } else {
5204 proc->default_priority.sched_policy = SCHED_NORMAL;
5205 proc->default_priority.prio = NICE_TO_PRIO(0);
5206 }
5207
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005208 binder_dev = container_of(filp->private_data, struct binder_device,
5209 miscdev);
5210 proc->context = &binder_dev->context;
Todd Kjosd325d372016-10-10 10:40:53 -07005211 binder_alloc_init(&proc->alloc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07005212
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005213 binder_stats_created(BINDER_STAT_PROC);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005214 proc->pid = current->group_leader->pid;
5215 INIT_LIST_HEAD(&proc->delivered_death);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005216 INIT_LIST_HEAD(&proc->waiting_threads);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005217 filp->private_data = proc;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07005218
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005219 mutex_lock(&binder_procs_lock);
5220 hlist_add_head(&proc->proc_node, &binder_procs);
5221 mutex_unlock(&binder_procs_lock);
5222
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005223 if (binder_debugfs_dir_entry_proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005224 char strbuf[11];
Seunghun Lee10f62862014-05-01 01:30:23 +09005225
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005226 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005227 /*
5228 * proc debug entries are shared between contexts, so
5229 * this will fail if the process tries to open the driver
5230 * again with a different context. The priting code will
5231 * anyway print all contexts that a given PID has, so this
5232 * is not a problem.
5233 */
Harsh Shandilya174562a2017-12-22 19:37:02 +05305234 proc->debugfs_entry = debugfs_create_file(strbuf, 0444,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005235 binder_debugfs_dir_entry_proc,
5236 (void *)(unsigned long)proc->pid,
5237 &binder_proc_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005238 }
5239
5240 return 0;
5241}
5242
5243static int binder_flush(struct file *filp, fl_owner_t id)
5244{
5245 struct binder_proc *proc = filp->private_data;
5246
5247 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
5248
5249 return 0;
5250}
5251
5252static void binder_deferred_flush(struct binder_proc *proc)
5253{
5254 struct rb_node *n;
5255 int wake_count = 0;
Seunghun Lee10f62862014-05-01 01:30:23 +09005256
Todd Kjosb4827902017-05-25 15:52:17 -07005257 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005258 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
5259 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
Seunghun Lee10f62862014-05-01 01:30:23 +09005260
Todd Kjos6798e6d2017-01-06 14:19:25 -08005261 thread->looper_need_return = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005262 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
5263 wake_up_interruptible(&thread->wait);
5264 wake_count++;
5265 }
5266 }
Todd Kjosb4827902017-05-25 15:52:17 -07005267 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005268
5269 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5270 "binder_flush: %d woke %d threads\n", proc->pid,
5271 wake_count);
5272}
5273
5274static int binder_release(struct inode *nodp, struct file *filp)
5275{
5276 struct binder_proc *proc = filp->private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09005277
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005278 debugfs_remove(proc->debugfs_entry);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005279 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
5280
5281 return 0;
5282}
5283
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005284static int binder_node_release(struct binder_node *node, int refs)
5285{
5286 struct binder_ref *ref;
5287 int death = 0;
Todd Kjose7f23ed2017-03-21 13:06:01 -07005288 struct binder_proc *proc = node->proc;
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005289
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005290 binder_release_work(proc, &node->async_todo);
Todd Kjose7f23ed2017-03-21 13:06:01 -07005291
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005292 binder_node_lock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07005293 binder_inner_proc_lock(proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005294 binder_dequeue_work_ilocked(&node->work);
Todd Kjosf22abc72017-05-09 11:08:05 -07005295 /*
5296 * The caller must have taken a temporary ref on the node,
5297 */
5298 BUG_ON(!node->tmp_refs);
5299 if (hlist_empty(&node->refs) && node->tmp_refs == 1) {
Todd Kjose7f23ed2017-03-21 13:06:01 -07005300 binder_inner_proc_unlock(proc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005301 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07005302 binder_free_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005303
5304 return refs;
5305 }
5306
5307 node->proc = NULL;
5308 node->local_strong_refs = 0;
5309 node->local_weak_refs = 0;
Todd Kjose7f23ed2017-03-21 13:06:01 -07005310 binder_inner_proc_unlock(proc);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005311
5312 spin_lock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005313 hlist_add_head(&node->dead_node, &binder_dead_nodes);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005314 spin_unlock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005315
5316 hlist_for_each_entry(ref, &node->refs, node_entry) {
5317 refs++;
Martijn Coenenf9eac642017-05-22 11:26:23 -07005318 /*
5319 * Need the node lock to synchronize
5320 * with new notification requests and the
5321 * inner lock to synchronize with queued
5322 * death notifications.
5323 */
5324 binder_inner_proc_lock(ref->proc);
5325 if (!ref->death) {
5326 binder_inner_proc_unlock(ref->proc);
Arve Hjønnevåge194fd82014-02-17 13:58:29 -08005327 continue;
Martijn Coenenf9eac642017-05-22 11:26:23 -07005328 }
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005329
5330 death++;
5331
Martijn Coenenf9eac642017-05-22 11:26:23 -07005332 BUG_ON(!list_empty(&ref->death->work.entry));
5333 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
5334 binder_enqueue_work_ilocked(&ref->death->work,
5335 &ref->proc->todo);
Martijn Coenen053be422017-06-06 15:17:46 -07005336 binder_wakeup_proc_ilocked(ref->proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005337 binder_inner_proc_unlock(ref->proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005338 }
5339
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005340 binder_debug(BINDER_DEBUG_DEAD_BINDER,
5341 "node %d now dead, refs %d, death %d\n",
5342 node->debug_id, refs, death);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005343 binder_node_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07005344 binder_put_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005345
5346 return refs;
5347}
5348
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005349static void binder_deferred_release(struct binder_proc *proc)
5350{
Martijn Coenen0b3311e2016-09-30 15:51:48 +02005351 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005352 struct rb_node *n;
Todd Kjosd325d372016-10-10 10:40:53 -07005353 int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005354
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005355 BUG_ON(proc->files);
5356
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005357 mutex_lock(&binder_procs_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005358 hlist_del(&proc->proc_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005359 mutex_unlock(&binder_procs_lock);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005360
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005361 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02005362 if (context->binder_context_mgr_node &&
5363 context->binder_context_mgr_node->proc == proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005364 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01005365 "%s: %d context_mgr_node gone\n",
5366 __func__, proc->pid);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02005367 context->binder_context_mgr_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005368 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005369 mutex_unlock(&context->context_mgr_node_lock);
Todd Kjosb4827902017-05-25 15:52:17 -07005370 binder_inner_proc_lock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07005371 /*
5372 * Make sure proc stays alive after we
5373 * remove all the threads
5374 */
5375 proc->tmp_ref++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005376
Todd Kjos2f993e22017-05-12 14:42:55 -07005377 proc->is_dead = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005378 threads = 0;
5379 active_transactions = 0;
5380 while ((n = rb_first(&proc->threads))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005381 struct binder_thread *thread;
5382
5383 thread = rb_entry(n, struct binder_thread, rb_node);
Todd Kjosb4827902017-05-25 15:52:17 -07005384 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005385 threads++;
Todd Kjos2f993e22017-05-12 14:42:55 -07005386 active_transactions += binder_thread_release(proc, thread);
Todd Kjosb4827902017-05-25 15:52:17 -07005387 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005388 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005389
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005390 nodes = 0;
5391 incoming_refs = 0;
5392 while ((n = rb_first(&proc->nodes))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005393 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005394
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005395 node = rb_entry(n, struct binder_node, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005396 nodes++;
Todd Kjosf22abc72017-05-09 11:08:05 -07005397 /*
5398 * take a temporary ref on the node before
5399 * calling binder_node_release() which will either
5400 * kfree() the node or call binder_put_node()
5401 */
Todd Kjos425d23f2017-06-12 12:07:26 -07005402 binder_inc_node_tmpref_ilocked(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005403 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjos425d23f2017-06-12 12:07:26 -07005404 binder_inner_proc_unlock(proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005405 incoming_refs = binder_node_release(node, incoming_refs);
Todd Kjos425d23f2017-06-12 12:07:26 -07005406 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005407 }
Todd Kjos425d23f2017-06-12 12:07:26 -07005408 binder_inner_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005409
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005410 outgoing_refs = 0;
Todd Kjos5346bf32016-10-20 16:43:34 -07005411 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005412 while ((n = rb_first(&proc->refs_by_desc))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005413 struct binder_ref *ref;
5414
5415 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005416 outgoing_refs++;
Todd Kjos5346bf32016-10-20 16:43:34 -07005417 binder_cleanup_ref_olocked(ref);
5418 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07005419 binder_free_ref(ref);
Todd Kjos5346bf32016-10-20 16:43:34 -07005420 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005421 }
Todd Kjos5346bf32016-10-20 16:43:34 -07005422 binder_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005423
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005424 binder_release_work(proc, &proc->todo);
5425 binder_release_work(proc, &proc->delivered_death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005426
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005427 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Todd Kjosd325d372016-10-10 10:40:53 -07005428 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01005429 __func__, proc->pid, threads, nodes, incoming_refs,
Todd Kjosd325d372016-10-10 10:40:53 -07005430 outgoing_refs, active_transactions);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005431
Todd Kjos2f993e22017-05-12 14:42:55 -07005432 binder_proc_dec_tmpref(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005433}
5434
5435static void binder_deferred_func(struct work_struct *work)
5436{
5437 struct binder_proc *proc;
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005438 struct files_struct *files;
5439
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005440 int defer;
Seunghun Lee10f62862014-05-01 01:30:23 +09005441
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005442 do {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005443 mutex_lock(&binder_deferred_lock);
5444 if (!hlist_empty(&binder_deferred_list)) {
5445 proc = hlist_entry(binder_deferred_list.first,
5446 struct binder_proc, deferred_work_node);
5447 hlist_del_init(&proc->deferred_work_node);
5448 defer = proc->deferred_work;
5449 proc->deferred_work = 0;
5450 } else {
5451 proc = NULL;
5452 defer = 0;
5453 }
5454 mutex_unlock(&binder_deferred_lock);
5455
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005456 files = NULL;
5457 if (defer & BINDER_DEFERRED_PUT_FILES) {
Todd Kjosfbb43392017-11-27 09:32:33 -08005458 mutex_lock(&proc->files_lock);
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005459 files = proc->files;
5460 if (files)
5461 proc->files = NULL;
Todd Kjosfbb43392017-11-27 09:32:33 -08005462 mutex_unlock(&proc->files_lock);
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005463 }
5464
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005465 if (defer & BINDER_DEFERRED_FLUSH)
5466 binder_deferred_flush(proc);
5467
5468 if (defer & BINDER_DEFERRED_RELEASE)
5469 binder_deferred_release(proc); /* frees proc */
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005470
5471 if (files)
5472 put_files_struct(files);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005473 } while (proc);
5474}
5475static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
5476
5477static void
5478binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
5479{
5480 mutex_lock(&binder_deferred_lock);
5481 proc->deferred_work |= defer;
5482 if (hlist_unhashed(&proc->deferred_work_node)) {
5483 hlist_add_head(&proc->deferred_work_node,
5484 &binder_deferred_list);
Bhaktipriya Shridhar1beba522016-08-13 22:16:24 +05305485 schedule_work(&binder_deferred_work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005486 }
5487 mutex_unlock(&binder_deferred_lock);
5488}
5489
Todd Kjos6d241a42017-04-21 14:32:11 -07005490static void print_binder_transaction_ilocked(struct seq_file *m,
5491 struct binder_proc *proc,
5492 const char *prefix,
5493 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005494{
Todd Kjos6d241a42017-04-21 14:32:11 -07005495 struct binder_proc *to_proc;
5496 struct binder_buffer *buffer = t->buffer;
5497
Todd Kjos2f993e22017-05-12 14:42:55 -07005498 spin_lock(&t->lock);
Todd Kjos6d241a42017-04-21 14:32:11 -07005499 to_proc = t->to_proc;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005500 seq_printf(m,
Todd Kjosf540ce02018-02-07 13:57:37 -08005501 "%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 -07005502 prefix, t->debug_id, t,
5503 t->from ? t->from->proc->pid : 0,
5504 t->from ? t->from->pid : 0,
Todd Kjos6d241a42017-04-21 14:32:11 -07005505 to_proc ? to_proc->pid : 0,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005506 t->to_thread ? t->to_thread->pid : 0,
Martijn Coenen57b2ac62017-06-06 17:04:42 -07005507 t->code, t->flags, t->priority.sched_policy,
5508 t->priority.prio, t->need_reply);
Todd Kjos2f993e22017-05-12 14:42:55 -07005509 spin_unlock(&t->lock);
5510
Todd Kjos6d241a42017-04-21 14:32:11 -07005511 if (proc != to_proc) {
5512 /*
5513 * Can only safely deref buffer if we are holding the
5514 * correct proc inner lock for this node
5515 */
5516 seq_puts(m, "\n");
5517 return;
5518 }
5519
5520 if (buffer == NULL) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005521 seq_puts(m, " buffer free\n");
5522 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005523 }
Todd Kjos6d241a42017-04-21 14:32:11 -07005524 if (buffer->target_node)
5525 seq_printf(m, " node %d", buffer->target_node->debug_id);
Todd Kjosf540ce02018-02-07 13:57:37 -08005526 seq_printf(m, " size %zd:%zd data %pK\n",
Todd Kjos6d241a42017-04-21 14:32:11 -07005527 buffer->data_size, buffer->offsets_size,
Todd Kjos8539b1e2019-02-08 10:35:20 -08005528 buffer->user_data);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005529}
5530
Todd Kjos6d241a42017-04-21 14:32:11 -07005531static void print_binder_work_ilocked(struct seq_file *m,
5532 struct binder_proc *proc,
5533 const char *prefix,
5534 const char *transaction_prefix,
5535 struct binder_work *w)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005536{
5537 struct binder_node *node;
5538 struct binder_transaction *t;
5539
5540 switch (w->type) {
5541 case BINDER_WORK_TRANSACTION:
5542 t = container_of(w, struct binder_transaction, work);
Todd Kjos6d241a42017-04-21 14:32:11 -07005543 print_binder_transaction_ilocked(
5544 m, proc, transaction_prefix, t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005545 break;
Todd Kjos858b8da2017-04-21 17:35:12 -07005546 case BINDER_WORK_RETURN_ERROR: {
5547 struct binder_error *e = container_of(
5548 w, struct binder_error, work);
5549
5550 seq_printf(m, "%stransaction error: %u\n",
5551 prefix, e->cmd);
5552 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005553 case BINDER_WORK_TRANSACTION_COMPLETE:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005554 seq_printf(m, "%stransaction complete\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005555 break;
5556 case BINDER_WORK_NODE:
5557 node = container_of(w, struct binder_node, work);
Arve Hjønnevågda498892014-02-21 14:40:26 -08005558 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
5559 prefix, node->debug_id,
5560 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005561 break;
5562 case BINDER_WORK_DEAD_BINDER:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005563 seq_printf(m, "%shas dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005564 break;
5565 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005566 seq_printf(m, "%shas cleared dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005567 break;
5568 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005569 seq_printf(m, "%shas cleared death notification\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005570 break;
5571 default:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005572 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005573 break;
5574 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005575}
5576
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005577static void print_binder_thread_ilocked(struct seq_file *m,
5578 struct binder_thread *thread,
5579 int print_always)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005580{
5581 struct binder_transaction *t;
5582 struct binder_work *w;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005583 size_t start_pos = m->count;
5584 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005585
Todd Kjos2f993e22017-05-12 14:42:55 -07005586 seq_printf(m, " thread %d: l %02x need_return %d tr %d\n",
Todd Kjos6798e6d2017-01-06 14:19:25 -08005587 thread->pid, thread->looper,
Todd Kjos2f993e22017-05-12 14:42:55 -07005588 thread->looper_need_return,
5589 atomic_read(&thread->tmp_ref));
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005590 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005591 t = thread->transaction_stack;
5592 while (t) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005593 if (t->from == thread) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005594 print_binder_transaction_ilocked(m, thread->proc,
5595 " outgoing transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005596 t = t->from_parent;
5597 } else if (t->to_thread == thread) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005598 print_binder_transaction_ilocked(m, thread->proc,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005599 " incoming transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005600 t = t->to_parent;
5601 } else {
Todd Kjos6d241a42017-04-21 14:32:11 -07005602 print_binder_transaction_ilocked(m, thread->proc,
5603 " bad transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005604 t = NULL;
5605 }
5606 }
5607 list_for_each_entry(w, &thread->todo, entry) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005608 print_binder_work_ilocked(m, thread->proc, " ",
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005609 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005610 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005611 if (!print_always && m->count == header_pos)
5612 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005613}
5614
Todd Kjos425d23f2017-06-12 12:07:26 -07005615static void print_binder_node_nilocked(struct seq_file *m,
5616 struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005617{
5618 struct binder_ref *ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005619 struct binder_work *w;
5620 int count;
5621
5622 count = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08005623 hlist_for_each_entry(ref, &node->refs, node_entry)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005624 count++;
5625
Martijn Coenen6aac9792017-06-07 09:29:14 -07005626 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 -08005627 node->debug_id, (u64)node->ptr, (u64)node->cookie,
Martijn Coenen6aac9792017-06-07 09:29:14 -07005628 node->sched_policy, node->min_priority,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005629 node->has_strong_ref, node->has_weak_ref,
5630 node->local_strong_refs, node->local_weak_refs,
Todd Kjosf22abc72017-05-09 11:08:05 -07005631 node->internal_strong_refs, count, node->tmp_refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005632 if (count) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005633 seq_puts(m, " proc");
Sasha Levinb67bfe02013-02-27 17:06:00 -08005634 hlist_for_each_entry(ref, &node->refs, node_entry)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005635 seq_printf(m, " %d", ref->proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005636 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005637 seq_puts(m, "\n");
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005638 if (node->proc) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005639 list_for_each_entry(w, &node->async_todo, entry)
Todd Kjos6d241a42017-04-21 14:32:11 -07005640 print_binder_work_ilocked(m, node->proc, " ",
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005641 " pending async transaction", w);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005642 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005643}
5644
Todd Kjos5346bf32016-10-20 16:43:34 -07005645static void print_binder_ref_olocked(struct seq_file *m,
5646 struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005647{
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005648 binder_node_lock(ref->node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07005649 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
5650 ref->data.debug_id, ref->data.desc,
5651 ref->node->proc ? "" : "dead ",
5652 ref->node->debug_id, ref->data.strong,
5653 ref->data.weak, ref->death);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005654 binder_node_unlock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005655}
5656
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005657static void print_binder_proc(struct seq_file *m,
5658 struct binder_proc *proc, int print_all)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005659{
5660 struct binder_work *w;
5661 struct rb_node *n;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005662 size_t start_pos = m->count;
5663 size_t header_pos;
Todd Kjos425d23f2017-06-12 12:07:26 -07005664 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005665
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005666 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005667 seq_printf(m, "context %s\n", proc->context->name);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005668 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005669
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005670 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005671 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005672 print_binder_thread_ilocked(m, rb_entry(n, struct binder_thread,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005673 rb_node), print_all);
Todd Kjos425d23f2017-06-12 12:07:26 -07005674
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005675 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005676 struct binder_node *node = rb_entry(n, struct binder_node,
5677 rb_node);
Todd Kjosd79aa412018-12-05 15:19:26 -08005678 if (!print_all && !node->has_async_transaction)
5679 continue;
5680
Todd Kjos425d23f2017-06-12 12:07:26 -07005681 /*
5682 * take a temporary reference on the node so it
5683 * survives and isn't removed from the tree
5684 * while we print it.
5685 */
5686 binder_inc_node_tmpref_ilocked(node);
5687 /* Need to drop inner lock to take node lock */
5688 binder_inner_proc_unlock(proc);
5689 if (last_node)
5690 binder_put_node(last_node);
5691 binder_node_inner_lock(node);
5692 print_binder_node_nilocked(m, node);
5693 binder_node_inner_unlock(node);
5694 last_node = node;
5695 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005696 }
Todd Kjos425d23f2017-06-12 12:07:26 -07005697 binder_inner_proc_unlock(proc);
5698 if (last_node)
5699 binder_put_node(last_node);
5700
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005701 if (print_all) {
Todd Kjos5346bf32016-10-20 16:43:34 -07005702 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005703 for (n = rb_first(&proc->refs_by_desc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005704 n != NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005705 n = rb_next(n))
Todd Kjos5346bf32016-10-20 16:43:34 -07005706 print_binder_ref_olocked(m, rb_entry(n,
5707 struct binder_ref,
5708 rb_node_desc));
5709 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005710 }
Todd Kjosd325d372016-10-10 10:40:53 -07005711 binder_alloc_print_allocated(m, &proc->alloc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005712 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005713 list_for_each_entry(w, &proc->todo, entry)
Todd Kjos6d241a42017-04-21 14:32:11 -07005714 print_binder_work_ilocked(m, proc, " ",
5715 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005716 list_for_each_entry(w, &proc->delivered_death, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005717 seq_puts(m, " has delivered dead binder\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005718 break;
5719 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005720 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005721 if (!print_all && m->count == header_pos)
5722 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005723}
5724
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005725static const char * const binder_return_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005726 "BR_ERROR",
5727 "BR_OK",
5728 "BR_TRANSACTION",
5729 "BR_REPLY",
5730 "BR_ACQUIRE_RESULT",
5731 "BR_DEAD_REPLY",
5732 "BR_TRANSACTION_COMPLETE",
5733 "BR_INCREFS",
5734 "BR_ACQUIRE",
5735 "BR_RELEASE",
5736 "BR_DECREFS",
5737 "BR_ATTEMPT_ACQUIRE",
5738 "BR_NOOP",
5739 "BR_SPAWN_LOOPER",
5740 "BR_FINISHED",
5741 "BR_DEAD_BINDER",
5742 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
5743 "BR_FAILED_REPLY"
5744};
5745
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005746static const char * const binder_command_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005747 "BC_TRANSACTION",
5748 "BC_REPLY",
5749 "BC_ACQUIRE_RESULT",
5750 "BC_FREE_BUFFER",
5751 "BC_INCREFS",
5752 "BC_ACQUIRE",
5753 "BC_RELEASE",
5754 "BC_DECREFS",
5755 "BC_INCREFS_DONE",
5756 "BC_ACQUIRE_DONE",
5757 "BC_ATTEMPT_ACQUIRE",
5758 "BC_REGISTER_LOOPER",
5759 "BC_ENTER_LOOPER",
5760 "BC_EXIT_LOOPER",
5761 "BC_REQUEST_DEATH_NOTIFICATION",
5762 "BC_CLEAR_DEATH_NOTIFICATION",
Martijn Coenen5a6da532016-09-30 14:10:07 +02005763 "BC_DEAD_BINDER_DONE",
5764 "BC_TRANSACTION_SG",
5765 "BC_REPLY_SG",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005766};
5767
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005768static const char * const binder_objstat_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005769 "proc",
5770 "thread",
5771 "node",
5772 "ref",
5773 "death",
5774 "transaction",
5775 "transaction_complete"
5776};
5777
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005778static void print_binder_stats(struct seq_file *m, const char *prefix,
5779 struct binder_stats *stats)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005780{
5781 int i;
5782
5783 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005784 ARRAY_SIZE(binder_command_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005785 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005786 int temp = atomic_read(&stats->bc[i]);
5787
5788 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005789 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005790 binder_command_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005791 }
5792
5793 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005794 ARRAY_SIZE(binder_return_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005795 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005796 int temp = atomic_read(&stats->br[i]);
5797
5798 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005799 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005800 binder_return_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005801 }
5802
5803 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005804 ARRAY_SIZE(binder_objstat_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005805 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005806 ARRAY_SIZE(stats->obj_deleted));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005807 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005808 int created = atomic_read(&stats->obj_created[i]);
5809 int deleted = atomic_read(&stats->obj_deleted[i]);
5810
5811 if (created || deleted)
5812 seq_printf(m, "%s%s: active %d total %d\n",
5813 prefix,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005814 binder_objstat_strings[i],
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005815 created - deleted,
5816 created);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005817 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005818}
5819
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005820static void print_binder_proc_stats(struct seq_file *m,
5821 struct binder_proc *proc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005822{
5823 struct binder_work *w;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005824 struct binder_thread *thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005825 struct rb_node *n;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005826 int count, strong, weak, ready_threads;
Todd Kjosb4827902017-05-25 15:52:17 -07005827 size_t free_async_space =
5828 binder_alloc_get_free_async_space(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005829
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005830 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005831 seq_printf(m, "context %s\n", proc->context->name);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005832 count = 0;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005833 ready_threads = 0;
Todd Kjosb4827902017-05-25 15:52:17 -07005834 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005835 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
5836 count++;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005837
5838 list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node)
5839 ready_threads++;
5840
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005841 seq_printf(m, " threads: %d\n", count);
5842 seq_printf(m, " requested threads: %d+%d/%d\n"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005843 " ready threads %d\n"
5844 " free async space %zd\n", proc->requested_threads,
5845 proc->requested_threads_started, proc->max_threads,
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005846 ready_threads,
Todd Kjosb4827902017-05-25 15:52:17 -07005847 free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005848 count = 0;
5849 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
5850 count++;
Todd Kjos425d23f2017-06-12 12:07:26 -07005851 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005852 seq_printf(m, " nodes: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005853 count = 0;
5854 strong = 0;
5855 weak = 0;
Todd Kjos5346bf32016-10-20 16:43:34 -07005856 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005857 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
5858 struct binder_ref *ref = rb_entry(n, struct binder_ref,
5859 rb_node_desc);
5860 count++;
Todd Kjosb0117bb2017-05-08 09:16:27 -07005861 strong += ref->data.strong;
5862 weak += ref->data.weak;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005863 }
Todd Kjos5346bf32016-10-20 16:43:34 -07005864 binder_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005865 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005866
Todd Kjosd325d372016-10-10 10:40:53 -07005867 count = binder_alloc_get_allocated_count(&proc->alloc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005868 seq_printf(m, " buffers: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005869
Sherry Yang91004422017-08-22 17:26:57 -07005870 binder_alloc_print_pages(m, &proc->alloc);
5871
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005872 count = 0;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005873 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005874 list_for_each_entry(w, &proc->todo, entry) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005875 if (w->type == BINDER_WORK_TRANSACTION)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005876 count++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005877 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005878 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005879 seq_printf(m, " pending transactions: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005880
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005881 print_binder_stats(m, " ", &proc->stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005882}
5883
5884
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005885static int binder_state_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005886{
5887 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005888 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005889 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005890
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005891 seq_puts(m, "binder state:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005892
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005893 spin_lock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005894 if (!hlist_empty(&binder_dead_nodes))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005895 seq_puts(m, "dead nodes:\n");
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005896 hlist_for_each_entry(node, &binder_dead_nodes, dead_node) {
5897 /*
5898 * take a temporary reference on the node so it
5899 * survives and isn't removed from the list
5900 * while we print it.
5901 */
5902 node->tmp_refs++;
5903 spin_unlock(&binder_dead_nodes_lock);
5904 if (last_node)
5905 binder_put_node(last_node);
5906 binder_node_lock(node);
Todd Kjos425d23f2017-06-12 12:07:26 -07005907 print_binder_node_nilocked(m, node);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005908 binder_node_unlock(node);
5909 last_node = node;
5910 spin_lock(&binder_dead_nodes_lock);
5911 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005912 spin_unlock(&binder_dead_nodes_lock);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005913 if (last_node)
5914 binder_put_node(last_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005915
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005916 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005917 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005918 print_binder_proc(m, proc, 1);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005919 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005920
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005921 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005922}
5923
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005924static int binder_stats_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005925{
5926 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005927
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005928 seq_puts(m, "binder stats:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005929
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005930 print_binder_stats(m, "", &binder_stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005931
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005932 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005933 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005934 print_binder_proc_stats(m, proc);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005935 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005936
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005937 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005938}
5939
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005940static int binder_transactions_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005941{
5942 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005943
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005944 seq_puts(m, "binder transactions:\n");
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005945 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005946 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005947 print_binder_proc(m, proc, 0);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005948 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005949
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005950 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005951}
5952
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005953static int binder_proc_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005954{
Riley Andrews83050a42016-02-09 21:05:33 -08005955 struct binder_proc *itr;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005956 int pid = (unsigned long)m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005957
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005958 mutex_lock(&binder_procs_lock);
Riley Andrews83050a42016-02-09 21:05:33 -08005959 hlist_for_each_entry(itr, &binder_procs, proc_node) {
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005960 if (itr->pid == pid) {
5961 seq_puts(m, "binder proc state:\n");
5962 print_binder_proc(m, itr, 1);
Riley Andrews83050a42016-02-09 21:05:33 -08005963 }
5964 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005965 mutex_unlock(&binder_procs_lock);
5966
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005967 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005968}
5969
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005970static void print_binder_transaction_log_entry(struct seq_file *m,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005971 struct binder_transaction_log_entry *e)
5972{
Todd Kjos1cfe6272017-05-24 13:33:28 -07005973 int debug_id = READ_ONCE(e->debug_id_done);
5974 /*
5975 * read barrier to guarantee debug_id_done read before
5976 * we print the log values
5977 */
5978 smp_rmb();
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005979 seq_printf(m,
Todd Kjos1cfe6272017-05-24 13:33:28 -07005980 "%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 -07005981 e->debug_id, (e->call_type == 2) ? "reply" :
5982 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005983 e->from_thread, e->to_proc, e->to_thread, e->context_name,
Todd Kjose598d172017-03-22 17:19:52 -07005984 e->to_node, e->target_handle, e->data_size, e->offsets_size,
5985 e->return_error, e->return_error_param,
5986 e->return_error_line);
Todd Kjos1cfe6272017-05-24 13:33:28 -07005987 /*
5988 * read-barrier to guarantee read of debug_id_done after
5989 * done printing the fields of the entry
5990 */
5991 smp_rmb();
5992 seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ?
5993 "\n" : " (incomplete)\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005994}
5995
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005996static int binder_transaction_log_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005997{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005998 struct binder_transaction_log *log = m->private;
Todd Kjos1cfe6272017-05-24 13:33:28 -07005999 unsigned int log_cur = atomic_read(&log->cur);
6000 unsigned int count;
6001 unsigned int cur;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006002 int i;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006003
Todd Kjos1cfe6272017-05-24 13:33:28 -07006004 count = log_cur + 1;
6005 cur = count < ARRAY_SIZE(log->entry) && !log->full ?
6006 0 : count % ARRAY_SIZE(log->entry);
6007 if (count > ARRAY_SIZE(log->entry) || log->full)
6008 count = ARRAY_SIZE(log->entry);
6009 for (i = 0; i < count; i++) {
6010 unsigned int index = cur++ % ARRAY_SIZE(log->entry);
6011
6012 print_binder_transaction_log_entry(m, &log->entry[index]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006013 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006014 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006015}
6016
6017static const struct file_operations binder_fops = {
6018 .owner = THIS_MODULE,
6019 .poll = binder_poll,
6020 .unlocked_ioctl = binder_ioctl,
Arve Hjønnevågda498892014-02-21 14:40:26 -08006021 .compat_ioctl = binder_ioctl,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006022 .mmap = binder_mmap,
6023 .open = binder_open,
6024 .flush = binder_flush,
6025 .release = binder_release,
6026};
6027
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006028BINDER_DEBUG_ENTRY(state);
6029BINDER_DEBUG_ENTRY(stats);
6030BINDER_DEBUG_ENTRY(transactions);
6031BINDER_DEBUG_ENTRY(transaction_log);
6032
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006033static int __init init_binder_device(const char *name)
6034{
6035 int ret;
6036 struct binder_device *binder_device;
6037
6038 binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
6039 if (!binder_device)
6040 return -ENOMEM;
6041
6042 binder_device->miscdev.fops = &binder_fops;
6043 binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
6044 binder_device->miscdev.name = name;
6045
6046 binder_device->context.binder_context_mgr_uid = INVALID_UID;
6047 binder_device->context.name = name;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07006048 mutex_init(&binder_device->context.context_mgr_node_lock);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006049
6050 ret = misc_register(&binder_device->miscdev);
6051 if (ret < 0) {
6052 kfree(binder_device);
6053 return ret;
6054 }
6055
6056 hlist_add_head(&binder_device->hlist, &binder_devices);
6057
6058 return ret;
6059}
6060
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006061static int __init binder_init(void)
6062{
6063 int ret;
Christian Brauner558ee932017-08-21 16:13:28 +02006064 char *device_name, *device_names, *device_tmp;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006065 struct binder_device *device;
6066 struct hlist_node *tmp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006067
Tetsuo Handaf8cb8222017-11-29 22:29:47 +09006068 ret = binder_alloc_shrinker_init();
6069 if (ret)
6070 return ret;
Sherry Yang5828d702017-07-29 13:24:11 -07006071
Todd Kjos1cfe6272017-05-24 13:33:28 -07006072 atomic_set(&binder_transaction_log.cur, ~0U);
6073 atomic_set(&binder_transaction_log_failed.cur, ~0U);
6074
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006075 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
6076 if (binder_debugfs_dir_entry_root)
6077 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
6078 binder_debugfs_dir_entry_root);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006079
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006080 if (binder_debugfs_dir_entry_root) {
6081 debugfs_create_file("state",
Harsh Shandilya174562a2017-12-22 19:37:02 +05306082 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006083 binder_debugfs_dir_entry_root,
6084 NULL,
6085 &binder_state_fops);
6086 debugfs_create_file("stats",
Harsh Shandilya174562a2017-12-22 19:37:02 +05306087 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006088 binder_debugfs_dir_entry_root,
6089 NULL,
6090 &binder_stats_fops);
6091 debugfs_create_file("transactions",
Harsh Shandilya174562a2017-12-22 19:37:02 +05306092 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006093 binder_debugfs_dir_entry_root,
6094 NULL,
6095 &binder_transactions_fops);
6096 debugfs_create_file("transaction_log",
Harsh Shandilya174562a2017-12-22 19:37:02 +05306097 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006098 binder_debugfs_dir_entry_root,
6099 &binder_transaction_log,
6100 &binder_transaction_log_fops);
6101 debugfs_create_file("failed_transaction_log",
Harsh Shandilya174562a2017-12-22 19:37:02 +05306102 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006103 binder_debugfs_dir_entry_root,
6104 &binder_transaction_log_failed,
6105 &binder_transaction_log_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006106 }
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006107
6108 /*
6109 * Copy the module_parameter string, because we don't want to
6110 * tokenize it in-place.
6111 */
6112 device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL);
6113 if (!device_names) {
6114 ret = -ENOMEM;
6115 goto err_alloc_device_names_failed;
6116 }
6117 strcpy(device_names, binder_devices_param);
6118
Christian Brauner558ee932017-08-21 16:13:28 +02006119 device_tmp = device_names;
6120 while ((device_name = strsep(&device_tmp, ","))) {
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006121 ret = init_binder_device(device_name);
6122 if (ret)
6123 goto err_init_binder_device_failed;
6124 }
6125
6126 return ret;
6127
6128err_init_binder_device_failed:
6129 hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
6130 misc_deregister(&device->miscdev);
6131 hlist_del(&device->hlist);
6132 kfree(device);
6133 }
Christian Brauner558ee932017-08-21 16:13:28 +02006134
6135 kfree(device_names);
6136
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006137err_alloc_device_names_failed:
6138 debugfs_remove_recursive(binder_debugfs_dir_entry_root);
6139
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006140 return ret;
6141}
6142
6143device_initcall(binder_init);
6144
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07006145#define CREATE_TRACE_POINTS
6146#include "binder_trace.h"
6147
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006148MODULE_LICENSE("GPL v2");