blob: 8cf48a8ab692f6046aae363b26c35fecec3a8a77 [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);
2243 if (read_size < sizeof(*hdr))
Martijn Coenen00c80372016-07-13 12:06:49 +02002244 return 0;
Todd Kjosd73356a2019-02-08 10:35:16 -08002245 binder_alloc_copy_from_buffer(&proc->alloc, object, buffer,
2246 offset, read_size);
Martijn Coenen00c80372016-07-13 12:06:49 +02002247
Todd Kjosd73356a2019-02-08 10:35:16 -08002248 /* Ok, now see if we read a complete object. */
2249 hdr = &object->hdr;
Martijn Coenen00c80372016-07-13 12:06:49 +02002250 switch (hdr->type) {
2251 case BINDER_TYPE_BINDER:
2252 case BINDER_TYPE_WEAK_BINDER:
2253 case BINDER_TYPE_HANDLE:
2254 case BINDER_TYPE_WEAK_HANDLE:
2255 object_size = sizeof(struct flat_binder_object);
2256 break;
2257 case BINDER_TYPE_FD:
2258 object_size = sizeof(struct binder_fd_object);
2259 break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002260 case BINDER_TYPE_PTR:
2261 object_size = sizeof(struct binder_buffer_object);
2262 break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002263 case BINDER_TYPE_FDA:
2264 object_size = sizeof(struct binder_fd_array_object);
2265 break;
Martijn Coenen00c80372016-07-13 12:06:49 +02002266 default:
2267 return 0;
2268 }
2269 if (offset <= buffer->data_size - object_size &&
2270 buffer->data_size >= object_size)
2271 return object_size;
2272 else
2273 return 0;
2274}
2275
Martijn Coenen5a6da532016-09-30 14:10:07 +02002276/**
2277 * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002278 * @proc: binder_proc owning the buffer
Martijn Coenen5a6da532016-09-30 14:10:07 +02002279 * @b: binder_buffer containing the object
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002280 * @object: struct binder_object to read into
Martijn Coenen5a6da532016-09-30 14:10:07 +02002281 * @index: index in offset array at which the binder_buffer_object is
2282 * located
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002283 * @start_offset: points to the start of the offset array
2284 * @object_offsetp: offset of @object read from @b
Martijn Coenen5a6da532016-09-30 14:10:07 +02002285 * @num_valid: the number of valid offsets in the offset array
2286 *
2287 * Return: If @index is within the valid range of the offset array
2288 * described by @start and @num_valid, and if there's a valid
2289 * binder_buffer_object at the offset found in index @index
2290 * of the offset array, that object is returned. Otherwise,
2291 * %NULL is returned.
2292 * Note that the offset found in index @index itself is not
2293 * verified; this function assumes that @num_valid elements
2294 * from @start were previously verified to have valid offsets.
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002295 * If @object_offsetp is non-NULL, then the offset within
2296 * @b is written to it.
Martijn Coenen5a6da532016-09-30 14:10:07 +02002297 */
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002298static struct binder_buffer_object *binder_validate_ptr(
2299 struct binder_proc *proc,
2300 struct binder_buffer *b,
2301 struct binder_object *object,
2302 binder_size_t index,
2303 binder_size_t start_offset,
2304 binder_size_t *object_offsetp,
2305 binder_size_t num_valid)
Martijn Coenen5a6da532016-09-30 14:10:07 +02002306{
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002307 size_t object_size;
2308 binder_size_t object_offset;
2309 unsigned long buffer_offset;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002310
2311 if (index >= num_valid)
2312 return NULL;
2313
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002314 buffer_offset = start_offset + sizeof(binder_size_t) * index;
2315 binder_alloc_copy_from_buffer(&proc->alloc, &object_offset,
2316 b, buffer_offset, sizeof(object_offset));
2317 object_size = binder_get_object(proc, b, object_offset, object);
2318 if (!object_size || object->hdr.type != BINDER_TYPE_PTR)
Martijn Coenen5a6da532016-09-30 14:10:07 +02002319 return NULL;
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002320 if (object_offsetp)
2321 *object_offsetp = object_offset;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002322
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002323 return &object->bbo;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002324}
2325
2326/**
2327 * binder_validate_fixup() - validates pointer/fd fixups happen in order.
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002328 * @proc: binder_proc owning the buffer
Martijn Coenen5a6da532016-09-30 14:10:07 +02002329 * @b: transaction buffer
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002330 * @objects_start_offset: offset to start of objects buffer
2331 * @buffer_obj_offset: offset to binder_buffer_object in which to fix up
2332 * @fixup_offset: start offset in @buffer to fix up
2333 * @last_obj_offset: offset to last binder_buffer_object that we fixed
2334 * @last_min_offset: minimum fixup offset in object at @last_obj_offset
Martijn Coenen5a6da532016-09-30 14:10:07 +02002335 *
2336 * Return: %true if a fixup in buffer @buffer at offset @offset is
2337 * allowed.
2338 *
2339 * For safety reasons, we only allow fixups inside a buffer to happen
2340 * at increasing offsets; additionally, we only allow fixup on the last
2341 * buffer object that was verified, or one of its parents.
2342 *
2343 * Example of what is allowed:
2344 *
2345 * A
2346 * B (parent = A, offset = 0)
2347 * C (parent = A, offset = 16)
2348 * D (parent = C, offset = 0)
2349 * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
2350 *
2351 * Examples of what is not allowed:
2352 *
2353 * Decreasing offsets within the same parent:
2354 * A
2355 * C (parent = A, offset = 16)
2356 * B (parent = A, offset = 0) // decreasing offset within A
2357 *
2358 * Referring to a parent that wasn't the last object or any of its parents:
2359 * A
2360 * B (parent = A, offset = 0)
2361 * C (parent = A, offset = 0)
2362 * C (parent = A, offset = 16)
2363 * D (parent = B, offset = 0) // B is not A or any of A's parents
2364 */
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002365static bool binder_validate_fixup(struct binder_proc *proc,
2366 struct binder_buffer *b,
2367 binder_size_t objects_start_offset,
2368 binder_size_t buffer_obj_offset,
Martijn Coenen5a6da532016-09-30 14:10:07 +02002369 binder_size_t fixup_offset,
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002370 binder_size_t last_obj_offset,
Martijn Coenen5a6da532016-09-30 14:10:07 +02002371 binder_size_t last_min_offset)
2372{
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002373 if (!last_obj_offset) {
Martijn Coenen5a6da532016-09-30 14:10:07 +02002374 /* Nothing to fix up in */
2375 return false;
2376 }
2377
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002378 while (last_obj_offset != buffer_obj_offset) {
2379 unsigned long buffer_offset;
2380 struct binder_object last_object;
2381 struct binder_buffer_object *last_bbo;
2382 size_t object_size = binder_get_object(proc, b, last_obj_offset,
2383 &last_object);
2384 if (object_size != sizeof(*last_bbo))
2385 return false;
2386
2387 last_bbo = &last_object.bbo;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002388 /*
2389 * Safe to retrieve the parent of last_obj, since it
2390 * was already previously verified by the driver.
2391 */
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002392 if ((last_bbo->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0)
Martijn Coenen5a6da532016-09-30 14:10:07 +02002393 return false;
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002394 last_min_offset = last_bbo->parent_offset + sizeof(uintptr_t);
2395 buffer_offset = objects_start_offset +
2396 sizeof(binder_size_t) * last_bbo->parent,
2397 binder_alloc_copy_from_buffer(&proc->alloc, &last_obj_offset,
2398 b, buffer_offset,
2399 sizeof(last_obj_offset));
Martijn Coenen5a6da532016-09-30 14:10:07 +02002400 }
2401 return (fixup_offset >= last_min_offset);
2402}
2403
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002404static void binder_transaction_buffer_release(struct binder_proc *proc,
2405 struct binder_buffer *buffer,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002406 binder_size_t *failed_at)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002407{
Martijn Coenen5a6da532016-09-30 14:10:07 +02002408 binder_size_t *offp, *off_start, *off_end;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002409 int debug_id = buffer->debug_id;
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002410 binder_size_t off_start_offset;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002411
2412 binder_debug(BINDER_DEBUG_TRANSACTION,
Todd Kjosf540ce02018-02-07 13:57:37 -08002413 "%d buffer release %d, size %zd-%zd, failed at %pK\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002414 proc->pid, buffer->debug_id,
2415 buffer->data_size, buffer->offsets_size, failed_at);
2416
2417 if (buffer->target_node)
2418 binder_dec_node(buffer->target_node, 1, 0);
2419
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002420 off_start_offset = ALIGN(buffer->data_size, sizeof(void *));
2421 off_start = (binder_size_t *)(buffer->data + off_start_offset);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002422 if (failed_at)
2423 off_end = failed_at;
2424 else
Martijn Coenen5a6da532016-09-30 14:10:07 +02002425 off_end = (void *)off_start + buffer->offsets_size;
2426 for (offp = off_start; offp < off_end; offp++) {
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;
2431 binder_size_t buffer_offset = (uintptr_t)offp -
2432 (uintptr_t)buffer->data;
Seunghun Lee10f62862014-05-01 01:30:23 +09002433
Todd Kjos90a570c2019-02-08 10:35:15 -08002434 binder_alloc_copy_from_buffer(&proc->alloc, &object_offset,
2435 buffer, buffer_offset,
2436 sizeof(object_offset));
Todd Kjosd73356a2019-02-08 10:35:16 -08002437 object_size = binder_get_object(proc, buffer,
2438 object_offset, &object);
Martijn Coenen00c80372016-07-13 12:06:49 +02002439 if (object_size == 0) {
2440 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
Todd Kjos90a570c2019-02-08 10:35:15 -08002441 debug_id, (u64)object_offset, buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002442 continue;
2443 }
Todd Kjosd73356a2019-02-08 10:35:16 -08002444 hdr = &object.hdr;
Martijn Coenen00c80372016-07-13 12:06:49 +02002445 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002446 case BINDER_TYPE_BINDER:
2447 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenen00c80372016-07-13 12:06:49 +02002448 struct flat_binder_object *fp;
2449 struct binder_node *node;
Seunghun Lee10f62862014-05-01 01:30:23 +09002450
Martijn Coenen00c80372016-07-13 12:06:49 +02002451 fp = to_flat_binder_object(hdr);
2452 node = binder_get_node(proc, fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002453 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002454 pr_err("transaction release %d bad node %016llx\n",
2455 debug_id, (u64)fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002456 break;
2457 }
2458 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002459 " node %d u%016llx\n",
2460 node->debug_id, (u64)node->ptr);
Martijn Coenen00c80372016-07-13 12:06:49 +02002461 binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER,
2462 0);
Todd Kjosf22abc72017-05-09 11:08:05 -07002463 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002464 } break;
2465 case BINDER_TYPE_HANDLE:
2466 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenen00c80372016-07-13 12:06:49 +02002467 struct flat_binder_object *fp;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002468 struct binder_ref_data rdata;
2469 int ret;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02002470
Martijn Coenen00c80372016-07-13 12:06:49 +02002471 fp = to_flat_binder_object(hdr);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002472 ret = binder_dec_ref_for_handle(proc, fp->handle,
2473 hdr->type == BINDER_TYPE_HANDLE, &rdata);
2474
2475 if (ret) {
2476 pr_err("transaction release %d bad handle %d, ret = %d\n",
2477 debug_id, fp->handle, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002478 break;
2479 }
2480 binder_debug(BINDER_DEBUG_TRANSACTION,
Todd Kjosb0117bb2017-05-08 09:16:27 -07002481 " ref %d desc %d\n",
2482 rdata.debug_id, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002483 } break;
2484
Martijn Coenen00c80372016-07-13 12:06:49 +02002485 case BINDER_TYPE_FD: {
2486 struct binder_fd_object *fp = to_binder_fd_object(hdr);
2487
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002488 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen00c80372016-07-13 12:06:49 +02002489 " fd %d\n", fp->fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002490 if (failed_at)
Martijn Coenen00c80372016-07-13 12:06:49 +02002491 task_close_fd(proc, fp->fd);
2492 } break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002493 case BINDER_TYPE_PTR:
2494 /*
2495 * Nothing to do here, this will get cleaned up when the
2496 * transaction buffer gets freed
2497 */
2498 break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002499 case BINDER_TYPE_FDA: {
2500 struct binder_fd_array_object *fda;
2501 struct binder_buffer_object *parent;
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002502 struct binder_object ptr_object;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002503 u32 *fd_array;
2504 size_t fd_index;
2505 binder_size_t fd_buf_size;
2506
2507 fda = to_binder_fd_array_object(hdr);
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002508 parent = binder_validate_ptr(proc, buffer, &ptr_object,
2509 fda->parent,
2510 off_start_offset,
2511 NULL,
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002512 offp - off_start);
2513 if (!parent) {
2514 pr_err("transaction release %d bad parent offset",
2515 debug_id);
2516 continue;
2517 }
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002518 fd_buf_size = sizeof(u32) * fda->num_fds;
2519 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2520 pr_err("transaction release %d invalid number of fds (%lld)\n",
2521 debug_id, (u64)fda->num_fds);
2522 continue;
2523 }
2524 if (fd_buf_size > parent->length ||
2525 fda->parent_offset > parent->length - fd_buf_size) {
2526 /* No space for all file descriptors here. */
2527 pr_err("transaction release %d not enough space for %lld fds in buffer\n",
2528 debug_id, (u64)fda->num_fds);
2529 continue;
2530 }
Todd Kjos8d24e2a2019-02-08 10:35:19 -08002531 fd_array = (u32 *)(uintptr_t)
2532 (parent->buffer + fda->parent_offset);
Todd Kjos90a570c2019-02-08 10:35:15 -08002533 for (fd_index = 0; fd_index < fda->num_fds;
2534 fd_index++) {
2535 u32 fd;
2536 binder_size_t offset =
2537 (uintptr_t)&fd_array[fd_index] -
2538 (uintptr_t)buffer->data;
2539
2540 binder_alloc_copy_from_buffer(&proc->alloc,
2541 &fd,
2542 buffer,
2543 offset,
2544 sizeof(fd));
2545 task_close_fd(proc, fd);
2546 }
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002547 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002548 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01002549 pr_err("transaction release %d bad object type %x\n",
Martijn Coenen00c80372016-07-13 12:06:49 +02002550 debug_id, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002551 break;
2552 }
2553 }
2554}
2555
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002556static int binder_translate_binder(struct flat_binder_object *fp,
2557 struct binder_transaction *t,
2558 struct binder_thread *thread)
2559{
2560 struct binder_node *node;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002561 struct binder_proc *proc = thread->proc;
2562 struct binder_proc *target_proc = t->to_proc;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002563 struct binder_ref_data rdata;
Todd Kjosf22abc72017-05-09 11:08:05 -07002564 int ret = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002565
2566 node = binder_get_node(proc, fp->binder);
2567 if (!node) {
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002568 node = binder_new_node(proc, fp);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002569 if (!node)
2570 return -ENOMEM;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002571 }
2572 if (fp->cookie != node->cookie) {
2573 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
2574 proc->pid, thread->pid, (u64)fp->binder,
2575 node->debug_id, (u64)fp->cookie,
2576 (u64)node->cookie);
Todd Kjosf22abc72017-05-09 11:08:05 -07002577 ret = -EINVAL;
2578 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002579 }
Todd Kjosf22abc72017-05-09 11:08:05 -07002580 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2581 ret = -EPERM;
2582 goto done;
2583 }
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002584
Todd Kjosb0117bb2017-05-08 09:16:27 -07002585 ret = binder_inc_ref_for_node(target_proc, node,
2586 fp->hdr.type == BINDER_TYPE_BINDER,
2587 &thread->todo, &rdata);
2588 if (ret)
Todd Kjosf22abc72017-05-09 11:08:05 -07002589 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002590
2591 if (fp->hdr.type == BINDER_TYPE_BINDER)
2592 fp->hdr.type = BINDER_TYPE_HANDLE;
2593 else
2594 fp->hdr.type = BINDER_TYPE_WEAK_HANDLE;
2595 fp->binder = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002596 fp->handle = rdata.desc;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002597 fp->cookie = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002598
Todd Kjosb0117bb2017-05-08 09:16:27 -07002599 trace_binder_transaction_node_to_ref(t, node, &rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002600 binder_debug(BINDER_DEBUG_TRANSACTION,
2601 " node %d u%016llx -> ref %d desc %d\n",
2602 node->debug_id, (u64)node->ptr,
Todd Kjosb0117bb2017-05-08 09:16:27 -07002603 rdata.debug_id, rdata.desc);
Todd Kjosf22abc72017-05-09 11:08:05 -07002604done:
2605 binder_put_node(node);
2606 return ret;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002607}
2608
2609static int binder_translate_handle(struct flat_binder_object *fp,
2610 struct binder_transaction *t,
2611 struct binder_thread *thread)
2612{
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002613 struct binder_proc *proc = thread->proc;
2614 struct binder_proc *target_proc = t->to_proc;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002615 struct binder_node *node;
2616 struct binder_ref_data src_rdata;
Todd Kjosf22abc72017-05-09 11:08:05 -07002617 int ret = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002618
Todd Kjosb0117bb2017-05-08 09:16:27 -07002619 node = binder_get_node_from_ref(proc, fp->handle,
2620 fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata);
2621 if (!node) {
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002622 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
2623 proc->pid, thread->pid, fp->handle);
2624 return -EINVAL;
2625 }
Todd Kjosf22abc72017-05-09 11:08:05 -07002626 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2627 ret = -EPERM;
2628 goto done;
2629 }
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002630
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002631 binder_node_lock(node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002632 if (node->proc == target_proc) {
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002633 if (fp->hdr.type == BINDER_TYPE_HANDLE)
2634 fp->hdr.type = BINDER_TYPE_BINDER;
2635 else
2636 fp->hdr.type = BINDER_TYPE_WEAK_BINDER;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002637 fp->binder = node->ptr;
2638 fp->cookie = node->cookie;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002639 if (node->proc)
2640 binder_inner_proc_lock(node->proc);
2641 binder_inc_node_nilocked(node,
2642 fp->hdr.type == BINDER_TYPE_BINDER,
2643 0, NULL);
2644 if (node->proc)
2645 binder_inner_proc_unlock(node->proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002646 trace_binder_transaction_ref_to_node(t, node, &src_rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002647 binder_debug(BINDER_DEBUG_TRANSACTION,
2648 " ref %d desc %d -> node %d u%016llx\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07002649 src_rdata.debug_id, src_rdata.desc, node->debug_id,
2650 (u64)node->ptr);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002651 binder_node_unlock(node);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002652 } else {
Todd Kjosb0117bb2017-05-08 09:16:27 -07002653 struct binder_ref_data dest_rdata;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002654
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002655 binder_node_unlock(node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002656 ret = binder_inc_ref_for_node(target_proc, node,
2657 fp->hdr.type == BINDER_TYPE_HANDLE,
2658 NULL, &dest_rdata);
2659 if (ret)
Todd Kjosf22abc72017-05-09 11:08:05 -07002660 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002661
2662 fp->binder = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002663 fp->handle = dest_rdata.desc;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002664 fp->cookie = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002665 trace_binder_transaction_ref_to_ref(t, node, &src_rdata,
2666 &dest_rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002667 binder_debug(BINDER_DEBUG_TRANSACTION,
2668 " ref %d desc %d -> ref %d desc %d (node %d)\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07002669 src_rdata.debug_id, src_rdata.desc,
2670 dest_rdata.debug_id, dest_rdata.desc,
2671 node->debug_id);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002672 }
Todd Kjosf22abc72017-05-09 11:08:05 -07002673done:
2674 binder_put_node(node);
2675 return ret;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002676}
2677
2678static int binder_translate_fd(int fd,
2679 struct binder_transaction *t,
2680 struct binder_thread *thread,
2681 struct binder_transaction *in_reply_to)
2682{
2683 struct binder_proc *proc = thread->proc;
2684 struct binder_proc *target_proc = t->to_proc;
2685 int target_fd;
2686 struct file *file;
2687 int ret;
2688 bool target_allows_fd;
2689
2690 if (in_reply_to)
2691 target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS);
2692 else
2693 target_allows_fd = t->buffer->target_node->accept_fds;
2694 if (!target_allows_fd) {
2695 binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
2696 proc->pid, thread->pid,
2697 in_reply_to ? "reply" : "transaction",
2698 fd);
2699 ret = -EPERM;
2700 goto err_fd_not_accepted;
2701 }
2702
2703 file = fget(fd);
2704 if (!file) {
2705 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
2706 proc->pid, thread->pid, fd);
2707 ret = -EBADF;
2708 goto err_fget;
2709 }
2710 ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file);
2711 if (ret < 0) {
2712 ret = -EPERM;
2713 goto err_security;
2714 }
2715
2716 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
2717 if (target_fd < 0) {
2718 ret = -ENOMEM;
2719 goto err_get_unused_fd;
2720 }
2721 task_fd_install(target_proc, target_fd, file);
2722 trace_binder_transaction_fd(t, fd, target_fd);
2723 binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n",
2724 fd, target_fd);
2725
2726 return target_fd;
2727
2728err_get_unused_fd:
2729err_security:
2730 fput(file);
2731err_fget:
2732err_fd_not_accepted:
2733 return ret;
2734}
2735
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002736static int binder_translate_fd_array(struct binder_fd_array_object *fda,
2737 struct binder_buffer_object *parent,
2738 struct binder_transaction *t,
2739 struct binder_thread *thread,
2740 struct binder_transaction *in_reply_to)
2741{
2742 binder_size_t fdi, fd_buf_size, num_installed_fds;
2743 int target_fd;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002744 u32 *fd_array;
2745 struct binder_proc *proc = thread->proc;
2746 struct binder_proc *target_proc = t->to_proc;
2747
2748 fd_buf_size = sizeof(u32) * fda->num_fds;
2749 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2750 binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
2751 proc->pid, thread->pid, (u64)fda->num_fds);
2752 return -EINVAL;
2753 }
2754 if (fd_buf_size > parent->length ||
2755 fda->parent_offset > parent->length - fd_buf_size) {
2756 /* No space for all file descriptors here. */
2757 binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
2758 proc->pid, thread->pid, (u64)fda->num_fds);
2759 return -EINVAL;
2760 }
Todd Kjos8d24e2a2019-02-08 10:35:19 -08002761 fd_array = (u32 *)(uintptr_t)(parent->buffer + fda->parent_offset);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002762 if (!IS_ALIGNED((unsigned long)fd_array, sizeof(u32))) {
2763 binder_user_error("%d:%d parent offset not aligned correctly.\n",
2764 proc->pid, thread->pid);
2765 return -EINVAL;
2766 }
2767 for (fdi = 0; fdi < fda->num_fds; fdi++) {
Todd Kjos90a570c2019-02-08 10:35:15 -08002768 u32 fd;
2769 int target_fd;
2770 binder_size_t offset =
2771 (uintptr_t)&fd_array[fdi] -
2772 (uintptr_t)t->buffer->data;
2773
2774 binder_alloc_copy_from_buffer(&target_proc->alloc,
2775 &fd, t->buffer,
2776 offset, sizeof(fd));
2777 target_fd = binder_translate_fd(fd, t, thread, in_reply_to);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002778 if (target_fd < 0)
2779 goto err_translate_fd_failed;
Todd Kjos90a570c2019-02-08 10:35:15 -08002780 binder_alloc_copy_to_buffer(&target_proc->alloc,
2781 t->buffer, offset,
2782 &target_fd, sizeof(fd));
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002783 }
2784 return 0;
2785
2786err_translate_fd_failed:
2787 /*
2788 * Failed to allocate fd or security error, free fds
2789 * installed so far.
2790 */
2791 num_installed_fds = fdi;
Todd Kjos90a570c2019-02-08 10:35:15 -08002792 for (fdi = 0; fdi < num_installed_fds; fdi++) {
2793 u32 fd;
2794 binder_size_t offset =
2795 (uintptr_t)&fd_array[fdi] -
2796 (uintptr_t)t->buffer->data;
2797
2798 binder_alloc_copy_from_buffer(&target_proc->alloc,
2799 &fd, t->buffer,
2800 offset, sizeof(fd));
2801 task_close_fd(target_proc, fd);
2802 }
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002803 return target_fd;
2804}
2805
Martijn Coenen5a6da532016-09-30 14:10:07 +02002806static int binder_fixup_parent(struct binder_transaction *t,
2807 struct binder_thread *thread,
2808 struct binder_buffer_object *bp,
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002809 binder_size_t off_start_offset,
Martijn Coenen5a6da532016-09-30 14:10:07 +02002810 binder_size_t num_valid,
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002811 binder_size_t last_fixup_obj_off,
Martijn Coenen5a6da532016-09-30 14:10:07 +02002812 binder_size_t last_fixup_min_off)
2813{
2814 struct binder_buffer_object *parent;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002815 struct binder_buffer *b = t->buffer;
2816 struct binder_proc *proc = thread->proc;
2817 struct binder_proc *target_proc = t->to_proc;
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002818 struct binder_object object;
2819 binder_size_t buffer_offset;
2820 binder_size_t parent_offset;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002821
2822 if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT))
2823 return 0;
2824
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002825 parent = binder_validate_ptr(target_proc, b, &object, bp->parent,
2826 off_start_offset, &parent_offset,
2827 num_valid);
Martijn Coenen5a6da532016-09-30 14:10:07 +02002828 if (!parent) {
2829 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
2830 proc->pid, thread->pid);
2831 return -EINVAL;
2832 }
2833
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002834 if (!binder_validate_fixup(target_proc, b, off_start_offset,
2835 parent_offset, bp->parent_offset,
2836 last_fixup_obj_off,
Martijn Coenen5a6da532016-09-30 14:10:07 +02002837 last_fixup_min_off)) {
2838 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
2839 proc->pid, thread->pid);
2840 return -EINVAL;
2841 }
2842
2843 if (parent->length < sizeof(binder_uintptr_t) ||
2844 bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) {
2845 /* No space for a pointer here! */
2846 binder_user_error("%d:%d got transaction with invalid parent offset\n",
2847 proc->pid, thread->pid);
2848 return -EINVAL;
2849 }
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002850 buffer_offset = bp->parent_offset +
Todd Kjos8d24e2a2019-02-08 10:35:19 -08002851 (uintptr_t)parent->buffer - (uintptr_t)b->data;
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002852 binder_alloc_copy_to_buffer(&target_proc->alloc, b, buffer_offset,
2853 &bp->buffer, sizeof(bp->buffer));
Martijn Coenen5a6da532016-09-30 14:10:07 +02002854
2855 return 0;
2856}
2857
Martijn Coenen053be422017-06-06 15:17:46 -07002858/**
2859 * binder_proc_transaction() - sends a transaction to a process and wakes it up
2860 * @t: transaction to send
2861 * @proc: process to send the transaction to
2862 * @thread: thread in @proc to send the transaction to (may be NULL)
2863 *
2864 * This function queues a transaction to the specified process. It will try
2865 * to find a thread in the target process to handle the transaction and
2866 * wake it up. If no thread is found, the work is queued to the proc
2867 * waitqueue.
2868 *
2869 * If the @thread parameter is not NULL, the transaction is always queued
2870 * to the waitlist of that specific thread.
2871 *
2872 * Return: true if the transactions was successfully queued
2873 * false if the target process or thread is dead
2874 */
2875static bool binder_proc_transaction(struct binder_transaction *t,
2876 struct binder_proc *proc,
2877 struct binder_thread *thread)
2878{
Martijn Coenen053be422017-06-06 15:17:46 -07002879 struct binder_node *node = t->buffer->target_node;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002880 struct binder_priority node_prio;
Martijn Coenen053be422017-06-06 15:17:46 -07002881 bool oneway = !!(t->flags & TF_ONE_WAY);
Martijn Coenen1af61802017-10-19 15:04:46 +02002882 bool pending_async = false;
Martijn Coenen053be422017-06-06 15:17:46 -07002883
2884 BUG_ON(!node);
2885 binder_node_lock(node);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002886 node_prio.prio = node->min_priority;
2887 node_prio.sched_policy = node->sched_policy;
2888
Martijn Coenen053be422017-06-06 15:17:46 -07002889 if (oneway) {
2890 BUG_ON(thread);
2891 if (node->has_async_transaction) {
Martijn Coenen1af61802017-10-19 15:04:46 +02002892 pending_async = true;
Martijn Coenen053be422017-06-06 15:17:46 -07002893 } else {
Gustavo A. R. Silvae62dd6f2018-01-23 12:04:27 -06002894 node->has_async_transaction = true;
Martijn Coenen053be422017-06-06 15:17:46 -07002895 }
2896 }
2897
2898 binder_inner_proc_lock(proc);
2899
2900 if (proc->is_dead || (thread && thread->is_dead)) {
2901 binder_inner_proc_unlock(proc);
2902 binder_node_unlock(node);
2903 return false;
2904 }
2905
Martijn Coenen1af61802017-10-19 15:04:46 +02002906 if (!thread && !pending_async)
Martijn Coenen053be422017-06-06 15:17:46 -07002907 thread = binder_select_thread_ilocked(proc);
2908
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002909 if (thread) {
Martijn Coenenc46810c2017-06-23 10:13:43 -07002910 binder_transaction_priority(thread->task, t, node_prio,
2911 node->inherit_rt);
Martijn Coenen1af61802017-10-19 15:04:46 +02002912 binder_enqueue_thread_work_ilocked(thread, &t->work);
2913 } else if (!pending_async) {
2914 binder_enqueue_work_ilocked(&t->work, &proc->todo);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002915 } else {
Martijn Coenen1af61802017-10-19 15:04:46 +02002916 binder_enqueue_work_ilocked(&t->work, &node->async_todo);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002917 }
Martijn Coenen053be422017-06-06 15:17:46 -07002918
Martijn Coenen1af61802017-10-19 15:04:46 +02002919 if (!pending_async)
Martijn Coenen053be422017-06-06 15:17:46 -07002920 binder_wakeup_thread_ilocked(proc, thread, !oneway /* sync */);
2921
2922 binder_inner_proc_unlock(proc);
2923 binder_node_unlock(node);
2924
2925 return true;
2926}
2927
Todd Kjos291d9682017-09-25 08:55:09 -07002928/**
2929 * binder_get_node_refs_for_txn() - Get required refs on node for txn
2930 * @node: struct binder_node for which to get refs
2931 * @proc: returns @node->proc if valid
2932 * @error: if no @proc then returns BR_DEAD_REPLY
2933 *
2934 * User-space normally keeps the node alive when creating a transaction
2935 * since it has a reference to the target. The local strong ref keeps it
2936 * alive if the sending process dies before the target process processes
2937 * the transaction. If the source process is malicious or has a reference
2938 * counting bug, relying on the local strong ref can fail.
2939 *
2940 * Since user-space can cause the local strong ref to go away, we also take
2941 * a tmpref on the node to ensure it survives while we are constructing
2942 * the transaction. We also need a tmpref on the proc while we are
2943 * constructing the transaction, so we take that here as well.
2944 *
2945 * Return: The target_node with refs taken or NULL if no @node->proc is NULL.
2946 * Also sets @proc if valid. If the @node->proc is NULL indicating that the
2947 * target proc has died, @error is set to BR_DEAD_REPLY
2948 */
2949static struct binder_node *binder_get_node_refs_for_txn(
2950 struct binder_node *node,
2951 struct binder_proc **procp,
2952 uint32_t *error)
2953{
2954 struct binder_node *target_node = NULL;
2955
2956 binder_node_inner_lock(node);
2957 if (node->proc) {
2958 target_node = node;
2959 binder_inc_node_nilocked(node, 1, 0, NULL);
2960 binder_inc_node_tmpref_ilocked(node);
2961 node->proc->tmp_ref++;
2962 *procp = node->proc;
2963 } else
2964 *error = BR_DEAD_REPLY;
2965 binder_node_inner_unlock(node);
2966
2967 return target_node;
2968}
2969
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002970static void binder_transaction(struct binder_proc *proc,
2971 struct binder_thread *thread,
Martijn Coenen59878d72016-09-30 14:05:40 +02002972 struct binder_transaction_data *tr, int reply,
2973 binder_size_t extra_buffers_size)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002974{
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002975 int ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002976 struct binder_transaction *t;
2977 struct binder_work *tcomplete;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002978 binder_size_t *offp, *off_end, *off_start;
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002979 binder_size_t off_start_offset;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08002980 binder_size_t off_min;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002981 u8 *sg_bufp, *sg_buf_end;
Todd Kjos2f993e22017-05-12 14:42:55 -07002982 struct binder_proc *target_proc = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002983 struct binder_thread *target_thread = NULL;
2984 struct binder_node *target_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002985 struct binder_transaction *in_reply_to = NULL;
2986 struct binder_transaction_log_entry *e;
Todd Kjose598d172017-03-22 17:19:52 -07002987 uint32_t return_error = 0;
2988 uint32_t return_error_param = 0;
2989 uint32_t return_error_line = 0;
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002990 binder_size_t last_fixup_obj_off = 0;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002991 binder_size_t last_fixup_min_off = 0;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02002992 struct binder_context *context = proc->context;
Todd Kjos1cfe6272017-05-24 13:33:28 -07002993 int t_debug_id = atomic_inc_return(&binder_last_id);
Todd Kjos63e0afa2019-01-14 09:10:21 -08002994 char *secctx = NULL;
2995 u32 secctx_sz = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002996
2997 e = binder_transaction_log_add(&binder_transaction_log);
Todd Kjos1cfe6272017-05-24 13:33:28 -07002998 e->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002999 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
3000 e->from_proc = proc->pid;
3001 e->from_thread = thread->pid;
3002 e->target_handle = tr->target.handle;
3003 e->data_size = tr->data_size;
3004 e->offsets_size = tr->offsets_size;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02003005 e->context_name = proc->context->name;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003006
3007 if (reply) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07003008 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003009 in_reply_to = thread->transaction_stack;
3010 if (in_reply_to == NULL) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07003011 binder_inner_proc_unlock(proc);
Anmol Sarma56b468f2012-10-30 22:35:43 +05303012 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003013 proc->pid, thread->pid);
3014 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003015 return_error_param = -EPROTO;
3016 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003017 goto err_empty_call_stack;
3018 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003019 if (in_reply_to->to_thread != thread) {
Todd Kjos2f993e22017-05-12 14:42:55 -07003020 spin_lock(&in_reply_to->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05303021 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 +09003022 proc->pid, thread->pid, in_reply_to->debug_id,
3023 in_reply_to->to_proc ?
3024 in_reply_to->to_proc->pid : 0,
3025 in_reply_to->to_thread ?
3026 in_reply_to->to_thread->pid : 0);
Todd Kjos2f993e22017-05-12 14:42:55 -07003027 spin_unlock(&in_reply_to->lock);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003028 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003029 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003030 return_error_param = -EPROTO;
3031 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003032 in_reply_to = NULL;
3033 goto err_bad_call_stack;
3034 }
3035 thread->transaction_stack = in_reply_to->to_parent;
Martijn Coenen995a36e2017-06-02 13:36:52 -07003036 binder_inner_proc_unlock(proc);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003037 target_thread = binder_get_txn_from_and_acq_inner(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003038 if (target_thread == NULL) {
3039 return_error = BR_DEAD_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003040 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003041 goto err_dead_binder;
3042 }
3043 if (target_thread->transaction_stack != in_reply_to) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303044 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 +09003045 proc->pid, thread->pid,
3046 target_thread->transaction_stack ?
3047 target_thread->transaction_stack->debug_id : 0,
3048 in_reply_to->debug_id);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003049 binder_inner_proc_unlock(target_thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003050 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003051 return_error_param = -EPROTO;
3052 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003053 in_reply_to = NULL;
3054 target_thread = NULL;
3055 goto err_dead_binder;
3056 }
3057 target_proc = target_thread->proc;
Todd Kjos2f993e22017-05-12 14:42:55 -07003058 target_proc->tmp_ref++;
Martijn Coenen995a36e2017-06-02 13:36:52 -07003059 binder_inner_proc_unlock(target_thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003060 } else {
3061 if (tr->target.handle) {
3062 struct binder_ref *ref;
Seunghun Lee10f62862014-05-01 01:30:23 +09003063
Todd Kjosc37162d2017-05-26 11:56:29 -07003064 /*
3065 * There must already be a strong ref
3066 * on this node. If so, do a strong
3067 * increment on the node to ensure it
3068 * stays alive until the transaction is
3069 * done.
3070 */
Todd Kjos5346bf32016-10-20 16:43:34 -07003071 binder_proc_lock(proc);
3072 ref = binder_get_ref_olocked(proc, tr->target.handle,
3073 true);
Todd Kjosc37162d2017-05-26 11:56:29 -07003074 if (ref) {
Todd Kjos291d9682017-09-25 08:55:09 -07003075 target_node = binder_get_node_refs_for_txn(
3076 ref->node, &target_proc,
3077 &return_error);
3078 } else {
3079 binder_user_error("%d:%d got transaction to invalid handle\n",
3080 proc->pid, thread->pid);
3081 return_error = BR_FAILED_REPLY;
Todd Kjosc37162d2017-05-26 11:56:29 -07003082 }
Todd Kjos5346bf32016-10-20 16:43:34 -07003083 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003084 } else {
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003085 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003086 target_node = context->binder_context_mgr_node;
Todd Kjos291d9682017-09-25 08:55:09 -07003087 if (target_node)
3088 target_node = binder_get_node_refs_for_txn(
3089 target_node, &target_proc,
3090 &return_error);
3091 else
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003092 return_error = BR_DEAD_REPLY;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003093 mutex_unlock(&context->context_mgr_node_lock);
Martijn Coenenc4048b22018-03-28 11:14:50 +02003094 if (target_node && target_proc == proc) {
3095 binder_user_error("%d:%d got transaction to context manager from process owning it\n",
3096 proc->pid, thread->pid);
3097 return_error = BR_FAILED_REPLY;
3098 return_error_param = -EINVAL;
3099 return_error_line = __LINE__;
3100 goto err_invalid_target_handle;
3101 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003102 }
Todd Kjos291d9682017-09-25 08:55:09 -07003103 if (!target_node) {
3104 /*
3105 * return_error is set above
3106 */
3107 return_error_param = -EINVAL;
Todd Kjose598d172017-03-22 17:19:52 -07003108 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003109 goto err_dead_binder;
3110 }
Todd Kjos291d9682017-09-25 08:55:09 -07003111 e->to_node = target_node->debug_id;
Stephen Smalley79af7302015-01-21 10:54:10 -05003112 if (security_binder_transaction(proc->tsk,
3113 target_proc->tsk) < 0) {
3114 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003115 return_error_param = -EPERM;
3116 return_error_line = __LINE__;
Stephen Smalley79af7302015-01-21 10:54:10 -05003117 goto err_invalid_target_handle;
3118 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07003119 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003120 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
3121 struct binder_transaction *tmp;
Seunghun Lee10f62862014-05-01 01:30:23 +09003122
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003123 tmp = thread->transaction_stack;
3124 if (tmp->to_thread != thread) {
Todd Kjos2f993e22017-05-12 14:42:55 -07003125 spin_lock(&tmp->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05303126 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 +09003127 proc->pid, thread->pid, tmp->debug_id,
3128 tmp->to_proc ? tmp->to_proc->pid : 0,
3129 tmp->to_thread ?
3130 tmp->to_thread->pid : 0);
Todd Kjos2f993e22017-05-12 14:42:55 -07003131 spin_unlock(&tmp->lock);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003132 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003133 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003134 return_error_param = -EPROTO;
3135 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003136 goto err_bad_call_stack;
3137 }
3138 while (tmp) {
Todd Kjos2f993e22017-05-12 14:42:55 -07003139 struct binder_thread *from;
3140
3141 spin_lock(&tmp->lock);
3142 from = tmp->from;
3143 if (from && from->proc == target_proc) {
3144 atomic_inc(&from->tmp_ref);
3145 target_thread = from;
3146 spin_unlock(&tmp->lock);
3147 break;
3148 }
3149 spin_unlock(&tmp->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003150 tmp = tmp->from_parent;
3151 }
3152 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07003153 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003154 }
Martijn Coenen053be422017-06-06 15:17:46 -07003155 if (target_thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003156 e->to_thread = target_thread->pid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003157 e->to_proc = target_proc->pid;
3158
3159 /* TODO: reuse incoming transaction for reply */
3160 t = kzalloc(sizeof(*t), GFP_KERNEL);
3161 if (t == NULL) {
3162 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003163 return_error_param = -ENOMEM;
3164 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003165 goto err_alloc_t_failed;
3166 }
3167 binder_stats_created(BINDER_STAT_TRANSACTION);
Todd Kjos2f993e22017-05-12 14:42:55 -07003168 spin_lock_init(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003169
3170 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
3171 if (tcomplete == NULL) {
3172 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003173 return_error_param = -ENOMEM;
3174 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003175 goto err_alloc_tcomplete_failed;
3176 }
3177 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
3178
Todd Kjos1cfe6272017-05-24 13:33:28 -07003179 t->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003180
3181 if (reply)
3182 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen59878d72016-09-30 14:05:40 +02003183 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003184 proc->pid, thread->pid, t->debug_id,
3185 target_proc->pid, target_thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003186 (u64)tr->data.ptr.buffer,
3187 (u64)tr->data.ptr.offsets,
Martijn Coenen59878d72016-09-30 14:05:40 +02003188 (u64)tr->data_size, (u64)tr->offsets_size,
3189 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003190 else
3191 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen59878d72016-09-30 14:05:40 +02003192 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003193 proc->pid, thread->pid, t->debug_id,
3194 target_proc->pid, target_node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003195 (u64)tr->data.ptr.buffer,
3196 (u64)tr->data.ptr.offsets,
Martijn Coenen59878d72016-09-30 14:05:40 +02003197 (u64)tr->data_size, (u64)tr->offsets_size,
3198 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003199
3200 if (!reply && !(tr->flags & TF_ONE_WAY))
3201 t->from = thread;
3202 else
3203 t->from = NULL;
Tair Rzayev57bab7c2014-05-31 22:43:34 +03003204 t->sender_euid = task_euid(proc->tsk);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003205 t->to_proc = target_proc;
3206 t->to_thread = target_thread;
3207 t->code = tr->code;
3208 t->flags = tr->flags;
Martijn Coenen57b2ac62017-06-06 17:04:42 -07003209 if (!(t->flags & TF_ONE_WAY) &&
3210 binder_supported_policy(current->policy)) {
3211 /* Inherit supported policies for synchronous transactions */
3212 t->priority.sched_policy = current->policy;
3213 t->priority.prio = current->normal_prio;
3214 } else {
3215 /* Otherwise, fall back to the default priority */
3216 t->priority = target_proc->default_priority;
3217 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003218
Todd Kjos63e0afa2019-01-14 09:10:21 -08003219 if (target_node && target_node->txn_security_ctx) {
3220 u32 secid;
3221
3222 security_task_getsecid(proc->tsk, &secid);
3223 ret = security_secid_to_secctx(secid, &secctx, &secctx_sz);
3224 if (ret) {
3225 return_error = BR_FAILED_REPLY;
3226 return_error_param = ret;
3227 return_error_line = __LINE__;
3228 goto err_get_secctx_failed;
3229 }
3230 extra_buffers_size += ALIGN(secctx_sz, sizeof(u64));
3231 }
3232
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003233 trace_binder_transaction(reply, t, target_node);
3234
Todd Kjosd325d372016-10-10 10:40:53 -07003235 t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size,
Martijn Coenen59878d72016-09-30 14:05:40 +02003236 tr->offsets_size, extra_buffers_size,
3237 !reply && (t->flags & TF_ONE_WAY));
Todd Kjose598d172017-03-22 17:19:52 -07003238 if (IS_ERR(t->buffer)) {
3239 /*
3240 * -ESRCH indicates VMA cleared. The target is dying.
3241 */
3242 return_error_param = PTR_ERR(t->buffer);
3243 return_error = return_error_param == -ESRCH ?
3244 BR_DEAD_REPLY : BR_FAILED_REPLY;
3245 return_error_line = __LINE__;
3246 t->buffer = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003247 goto err_binder_alloc_buf_failed;
3248 }
Todd Kjos63e0afa2019-01-14 09:10:21 -08003249 if (secctx) {
3250 size_t buf_offset = ALIGN(tr->data_size, sizeof(void *)) +
3251 ALIGN(tr->offsets_size, sizeof(void *)) +
3252 ALIGN(extra_buffers_size, sizeof(void *)) -
3253 ALIGN(secctx_sz, sizeof(u64));
Todd Kjos63e0afa2019-01-14 09:10:21 -08003254
Todd Kjos8d24e2a2019-02-08 10:35:19 -08003255 t->security_ctx = (uintptr_t)t->buffer->data + buf_offset;
Todd Kjos90a570c2019-02-08 10:35:15 -08003256 binder_alloc_copy_to_buffer(&target_proc->alloc,
3257 t->buffer, buf_offset,
3258 secctx, secctx_sz);
Todd Kjos63e0afa2019-01-14 09:10:21 -08003259 security_release_secctx(secctx, secctx_sz);
3260 secctx = NULL;
3261 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003262 t->buffer->debug_id = t->debug_id;
3263 t->buffer->transaction = t;
3264 t->buffer->target_node = target_node;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003265 trace_binder_transaction_alloc_buf(t->buffer);
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003266 off_start_offset = ALIGN(tr->data_size, sizeof(void *));
3267 off_start = (binder_size_t *)(t->buffer->data + off_start_offset);
Martijn Coenen5a6da532016-09-30 14:10:07 +02003268 offp = off_start;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003269
Todd Kjosd5049492019-02-08 10:35:14 -08003270 if (binder_alloc_copy_user_to_buffer(
3271 &target_proc->alloc,
3272 t->buffer, 0,
3273 (const void __user *)
3274 (uintptr_t)tr->data.ptr.buffer,
3275 tr->data_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303276 binder_user_error("%d:%d got transaction with invalid data ptr\n",
3277 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003278 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003279 return_error_param = -EFAULT;
3280 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003281 goto err_copy_data_failed;
3282 }
Todd Kjosd5049492019-02-08 10:35:14 -08003283 if (binder_alloc_copy_user_to_buffer(
3284 &target_proc->alloc,
3285 t->buffer,
3286 ALIGN(tr->data_size, sizeof(void *)),
3287 (const void __user *)
3288 (uintptr_t)tr->data.ptr.offsets,
3289 tr->offsets_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303290 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3291 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003292 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003293 return_error_param = -EFAULT;
3294 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003295 goto err_copy_data_failed;
3296 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08003297 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
3298 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
3299 proc->pid, thread->pid, (u64)tr->offsets_size);
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 = -EINVAL;
3302 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003303 goto err_bad_offset;
3304 }
Martijn Coenen5a6da532016-09-30 14:10:07 +02003305 if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) {
3306 binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
3307 proc->pid, thread->pid,
Amit Pundir44cbb182017-02-01 12:53:45 +05303308 (u64)extra_buffers_size);
Martijn Coenen5a6da532016-09-30 14:10:07 +02003309 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003310 return_error_param = -EINVAL;
3311 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003312 goto err_bad_offset;
3313 }
3314 off_end = (void *)off_start + tr->offsets_size;
3315 sg_bufp = (u8 *)(PTR_ALIGN(off_end, sizeof(void *)));
3316 sg_buf_end = sg_bufp + extra_buffers_size;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08003317 off_min = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003318 for (; offp < off_end; offp++) {
Martijn Coenen00c80372016-07-13 12:06:49 +02003319 struct binder_object_header *hdr;
Todd Kjos90a570c2019-02-08 10:35:15 -08003320 size_t object_size;
Todd Kjosd73356a2019-02-08 10:35:16 -08003321 struct binder_object object;
Todd Kjos90a570c2019-02-08 10:35:15 -08003322 binder_size_t object_offset;
3323 binder_size_t buffer_offset =
3324 (uintptr_t)offp - (uintptr_t)t->buffer->data;
Seunghun Lee10f62862014-05-01 01:30:23 +09003325
Todd Kjos90a570c2019-02-08 10:35:15 -08003326 binder_alloc_copy_from_buffer(&target_proc->alloc,
3327 &object_offset,
3328 t->buffer,
3329 buffer_offset,
3330 sizeof(object_offset));
Todd Kjosd73356a2019-02-08 10:35:16 -08003331 object_size = binder_get_object(target_proc, t->buffer,
3332 object_offset, &object);
Todd Kjos90a570c2019-02-08 10:35:15 -08003333 if (object_size == 0 || object_offset < off_min) {
Martijn Coenen00c80372016-07-13 12:06:49 +02003334 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 -08003335 proc->pid, thread->pid,
3336 (u64)object_offset,
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08003337 (u64)off_min,
Martijn Coenen00c80372016-07-13 12:06:49 +02003338 (u64)t->buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003339 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003340 return_error_param = -EINVAL;
3341 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003342 goto err_bad_offset;
3343 }
Martijn Coenen00c80372016-07-13 12:06:49 +02003344
Todd Kjosd73356a2019-02-08 10:35:16 -08003345 hdr = &object.hdr;
Todd Kjos90a570c2019-02-08 10:35:15 -08003346 off_min = object_offset + object_size;
Martijn Coenen00c80372016-07-13 12:06:49 +02003347 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003348 case BINDER_TYPE_BINDER:
3349 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003350 struct flat_binder_object *fp;
Seunghun Lee10f62862014-05-01 01:30:23 +09003351
Martijn Coenen00c80372016-07-13 12:06:49 +02003352 fp = to_flat_binder_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003353 ret = binder_translate_binder(fp, t, thread);
3354 if (ret < 0) {
Christian Engelmayer7d420432014-05-07 21:44:53 +02003355 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003356 return_error_param = ret;
3357 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003358 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003359 }
Todd Kjosd73356a2019-02-08 10:35:16 -08003360 binder_alloc_copy_to_buffer(&target_proc->alloc,
3361 t->buffer, object_offset,
3362 fp, sizeof(*fp));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003363 } break;
3364 case BINDER_TYPE_HANDLE:
3365 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003366 struct flat_binder_object *fp;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02003367
Martijn Coenen00c80372016-07-13 12:06:49 +02003368 fp = to_flat_binder_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003369 ret = binder_translate_handle(fp, t, thread);
3370 if (ret < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003371 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003372 return_error_param = ret;
3373 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003374 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003375 }
Todd Kjosd73356a2019-02-08 10:35:16 -08003376 binder_alloc_copy_to_buffer(&target_proc->alloc,
3377 t->buffer, object_offset,
3378 fp, sizeof(*fp));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003379 } break;
3380
3381 case BINDER_TYPE_FD: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003382 struct binder_fd_object *fp = to_binder_fd_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003383 int target_fd = binder_translate_fd(fp->fd, t, thread,
3384 in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003385
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003386 if (target_fd < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003387 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003388 return_error_param = target_fd;
3389 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003390 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003391 }
Martijn Coenen00c80372016-07-13 12:06:49 +02003392 fp->pad_binder = 0;
3393 fp->fd = target_fd;
Todd Kjosd73356a2019-02-08 10:35:16 -08003394 binder_alloc_copy_to_buffer(&target_proc->alloc,
3395 t->buffer, object_offset,
3396 fp, sizeof(*fp));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003397 } break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003398 case BINDER_TYPE_FDA: {
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003399 struct binder_object ptr_object;
3400 binder_size_t parent_offset;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003401 struct binder_fd_array_object *fda =
3402 to_binder_fd_array_object(hdr);
3403 struct binder_buffer_object *parent =
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003404 binder_validate_ptr(target_proc, t->buffer,
3405 &ptr_object, fda->parent,
3406 off_start_offset,
3407 &parent_offset,
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003408 offp - off_start);
3409 if (!parent) {
3410 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
3411 proc->pid, thread->pid);
3412 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003413 return_error_param = -EINVAL;
3414 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003415 goto err_bad_parent;
3416 }
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003417 if (!binder_validate_fixup(target_proc, t->buffer,
3418 off_start_offset,
3419 parent_offset,
3420 fda->parent_offset,
3421 last_fixup_obj_off,
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003422 last_fixup_min_off)) {
3423 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
3424 proc->pid, thread->pid);
3425 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003426 return_error_param = -EINVAL;
3427 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003428 goto err_bad_parent;
3429 }
3430 ret = binder_translate_fd_array(fda, parent, t, thread,
3431 in_reply_to);
3432 if (ret < 0) {
3433 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003434 return_error_param = ret;
3435 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003436 goto err_translate_failed;
3437 }
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003438 last_fixup_obj_off = parent_offset;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003439 last_fixup_min_off =
3440 fda->parent_offset + sizeof(u32) * fda->num_fds;
3441 } break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003442 case BINDER_TYPE_PTR: {
3443 struct binder_buffer_object *bp =
3444 to_binder_buffer_object(hdr);
3445 size_t buf_left = sg_buf_end - sg_bufp;
Todd Kjosd5049492019-02-08 10:35:14 -08003446 binder_size_t sg_buf_offset = (uintptr_t)sg_bufp -
3447 (uintptr_t)t->buffer->data;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003448
Martijn Coenen5a6da532016-09-30 14:10:07 +02003449 if (bp->length > buf_left) {
3450 binder_user_error("%d:%d got transaction with too large buffer\n",
3451 proc->pid, thread->pid);
3452 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003453 return_error_param = -EINVAL;
3454 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003455 goto err_bad_offset;
3456 }
Todd Kjosd5049492019-02-08 10:35:14 -08003457 if (binder_alloc_copy_user_to_buffer(
3458 &target_proc->alloc,
3459 t->buffer,
3460 sg_buf_offset,
3461 (const void __user *)
3462 (uintptr_t)bp->buffer,
3463 bp->length)) {
Martijn Coenen5a6da532016-09-30 14:10:07 +02003464 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3465 proc->pid, thread->pid);
Todd Kjose598d172017-03-22 17:19:52 -07003466 return_error_param = -EFAULT;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003467 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003468 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003469 goto err_copy_data_failed;
3470 }
3471 /* Fixup buffer pointer to target proc address space */
Todd Kjos8d24e2a2019-02-08 10:35:19 -08003472 bp->buffer = (uintptr_t)sg_bufp;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003473 sg_bufp += ALIGN(bp->length, sizeof(u64));
3474
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003475 ret = binder_fixup_parent(t, thread, bp,
3476 off_start_offset,
Martijn Coenen5a6da532016-09-30 14:10:07 +02003477 offp - off_start,
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003478 last_fixup_obj_off,
Martijn Coenen5a6da532016-09-30 14:10:07 +02003479 last_fixup_min_off);
3480 if (ret < 0) {
3481 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003482 return_error_param = ret;
3483 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003484 goto err_translate_failed;
3485 }
Todd Kjosd73356a2019-02-08 10:35:16 -08003486 binder_alloc_copy_to_buffer(&target_proc->alloc,
3487 t->buffer, object_offset,
3488 bp, sizeof(*bp));
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003489 last_fixup_obj_off = object_offset;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003490 last_fixup_min_off = 0;
3491 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003492 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01003493 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
Martijn Coenen00c80372016-07-13 12:06:49 +02003494 proc->pid, thread->pid, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003495 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003496 return_error_param = -EINVAL;
3497 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003498 goto err_bad_object_type;
3499 }
3500 }
Todd Kjos8dedb0c2017-05-09 08:31:32 -07003501 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003502 t->work.type = BINDER_WORK_TRANSACTION;
Todd Kjos8dedb0c2017-05-09 08:31:32 -07003503
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003504 if (reply) {
Martijn Coenen1af61802017-10-19 15:04:46 +02003505 binder_enqueue_thread_work(thread, tcomplete);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003506 binder_inner_proc_lock(target_proc);
3507 if (target_thread->is_dead) {
3508 binder_inner_proc_unlock(target_proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07003509 goto err_dead_proc_or_thread;
Martijn Coenen995a36e2017-06-02 13:36:52 -07003510 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003511 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003512 binder_pop_transaction_ilocked(target_thread, in_reply_to);
Martijn Coenen1af61802017-10-19 15:04:46 +02003513 binder_enqueue_thread_work_ilocked(target_thread, &t->work);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003514 binder_inner_proc_unlock(target_proc);
Martijn Coenen053be422017-06-06 15:17:46 -07003515 wake_up_interruptible_sync(&target_thread->wait);
Martijn Coenenecd972d2017-05-26 10:48:56 -07003516 binder_restore_priority(current, in_reply_to->saved_priority);
Todd Kjos21ef40a2017-03-30 18:02:13 -07003517 binder_free_transaction(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003518 } else if (!(t->flags & TF_ONE_WAY)) {
3519 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003520 binder_inner_proc_lock(proc);
Martijn Coenendac2e9c2017-11-13 09:55:21 +01003521 /*
3522 * Defer the TRANSACTION_COMPLETE, so we don't return to
3523 * userspace immediately; this allows the target process to
3524 * immediately start processing this transaction, reducing
3525 * latency. We will then return the TRANSACTION_COMPLETE when
3526 * the target replies (or there is an error).
3527 */
3528 binder_enqueue_deferred_thread_work_ilocked(thread, tcomplete);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003529 t->need_reply = 1;
3530 t->from_parent = thread->transaction_stack;
3531 thread->transaction_stack = t;
Martijn Coenen995a36e2017-06-02 13:36:52 -07003532 binder_inner_proc_unlock(proc);
Martijn Coenen053be422017-06-06 15:17:46 -07003533 if (!binder_proc_transaction(t, target_proc, target_thread)) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07003534 binder_inner_proc_lock(proc);
3535 binder_pop_transaction_ilocked(thread, t);
3536 binder_inner_proc_unlock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07003537 goto err_dead_proc_or_thread;
3538 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003539 } else {
3540 BUG_ON(target_node == NULL);
3541 BUG_ON(t->buffer->async_transaction != 1);
Martijn Coenen1af61802017-10-19 15:04:46 +02003542 binder_enqueue_thread_work(thread, tcomplete);
Martijn Coenen053be422017-06-06 15:17:46 -07003543 if (!binder_proc_transaction(t, target_proc, NULL))
Todd Kjos2f993e22017-05-12 14:42:55 -07003544 goto err_dead_proc_or_thread;
Riley Andrewsb5968812015-09-01 12:42:07 -07003545 }
Todd Kjos2f993e22017-05-12 14:42:55 -07003546 if (target_thread)
3547 binder_thread_dec_tmpref(target_thread);
3548 binder_proc_dec_tmpref(target_proc);
Todd Kjos291d9682017-09-25 08:55:09 -07003549 if (target_node)
3550 binder_dec_node_tmpref(target_node);
Todd Kjos1cfe6272017-05-24 13:33:28 -07003551 /*
3552 * write barrier to synchronize with initialization
3553 * of log entry
3554 */
3555 smp_wmb();
3556 WRITE_ONCE(e->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003557 return;
3558
Todd Kjos2f993e22017-05-12 14:42:55 -07003559err_dead_proc_or_thread:
3560 return_error = BR_DEAD_REPLY;
3561 return_error_line = __LINE__;
Xu YiPing86578a02017-05-22 11:26:23 -07003562 binder_dequeue_work(proc, tcomplete);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003563err_translate_failed:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003564err_bad_object_type:
3565err_bad_offset:
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003566err_bad_parent:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003567err_copy_data_failed:
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003568 trace_binder_transaction_failed_buffer_release(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003569 binder_transaction_buffer_release(target_proc, t->buffer, offp);
Todd Kjos291d9682017-09-25 08:55:09 -07003570 if (target_node)
3571 binder_dec_node_tmpref(target_node);
Todd Kjosc37162d2017-05-26 11:56:29 -07003572 target_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003573 t->buffer->transaction = NULL;
Todd Kjosd325d372016-10-10 10:40:53 -07003574 binder_alloc_free_buf(&target_proc->alloc, t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003575err_binder_alloc_buf_failed:
Todd Kjos63e0afa2019-01-14 09:10:21 -08003576 if (secctx)
3577 security_release_secctx(secctx, secctx_sz);
3578err_get_secctx_failed:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003579 kfree(tcomplete);
3580 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3581err_alloc_tcomplete_failed:
3582 kfree(t);
3583 binder_stats_deleted(BINDER_STAT_TRANSACTION);
3584err_alloc_t_failed:
3585err_bad_call_stack:
3586err_empty_call_stack:
3587err_dead_binder:
3588err_invalid_target_handle:
Todd Kjos2f993e22017-05-12 14:42:55 -07003589 if (target_thread)
3590 binder_thread_dec_tmpref(target_thread);
3591 if (target_proc)
3592 binder_proc_dec_tmpref(target_proc);
Todd Kjos291d9682017-09-25 08:55:09 -07003593 if (target_node) {
Todd Kjosc37162d2017-05-26 11:56:29 -07003594 binder_dec_node(target_node, 1, 0);
Todd Kjos291d9682017-09-25 08:55:09 -07003595 binder_dec_node_tmpref(target_node);
3596 }
Todd Kjosc37162d2017-05-26 11:56:29 -07003597
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003598 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Todd Kjose598d172017-03-22 17:19:52 -07003599 "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
3600 proc->pid, thread->pid, return_error, return_error_param,
3601 (u64)tr->data_size, (u64)tr->offsets_size,
3602 return_error_line);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003603
3604 {
3605 struct binder_transaction_log_entry *fe;
Seunghun Lee10f62862014-05-01 01:30:23 +09003606
Todd Kjose598d172017-03-22 17:19:52 -07003607 e->return_error = return_error;
3608 e->return_error_param = return_error_param;
3609 e->return_error_line = return_error_line;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003610 fe = binder_transaction_log_add(&binder_transaction_log_failed);
3611 *fe = *e;
Todd Kjos1cfe6272017-05-24 13:33:28 -07003612 /*
3613 * write barrier to synchronize with initialization
3614 * of log entry
3615 */
3616 smp_wmb();
3617 WRITE_ONCE(e->debug_id_done, t_debug_id);
3618 WRITE_ONCE(fe->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003619 }
3620
Todd Kjos858b8da2017-04-21 17:35:12 -07003621 BUG_ON(thread->return_error.cmd != BR_OK);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003622 if (in_reply_to) {
Martijn Coenenecd972d2017-05-26 10:48:56 -07003623 binder_restore_priority(current, in_reply_to->saved_priority);
Todd Kjos858b8da2017-04-21 17:35:12 -07003624 thread->return_error.cmd = BR_TRANSACTION_COMPLETE;
Martijn Coenen1af61802017-10-19 15:04:46 +02003625 binder_enqueue_thread_work(thread, &thread->return_error.work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003626 binder_send_failed_reply(in_reply_to, return_error);
Todd Kjos858b8da2017-04-21 17:35:12 -07003627 } else {
3628 thread->return_error.cmd = return_error;
Martijn Coenen1af61802017-10-19 15:04:46 +02003629 binder_enqueue_thread_work(thread, &thread->return_error.work);
Todd Kjos858b8da2017-04-21 17:35:12 -07003630 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003631}
3632
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02003633static int binder_thread_write(struct binder_proc *proc,
3634 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003635 binder_uintptr_t binder_buffer, size_t size,
3636 binder_size_t *consumed)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003637{
3638 uint32_t cmd;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003639 struct binder_context *context = proc->context;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003640 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003641 void __user *ptr = buffer + *consumed;
3642 void __user *end = buffer + size;
3643
Todd Kjos858b8da2017-04-21 17:35:12 -07003644 while (ptr < end && thread->return_error.cmd == BR_OK) {
Todd Kjosb0117bb2017-05-08 09:16:27 -07003645 int ret;
3646
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003647 if (get_user(cmd, (uint32_t __user *)ptr))
3648 return -EFAULT;
3649 ptr += sizeof(uint32_t);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003650 trace_binder_command(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003651 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07003652 atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]);
3653 atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]);
3654 atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003655 }
3656 switch (cmd) {
3657 case BC_INCREFS:
3658 case BC_ACQUIRE:
3659 case BC_RELEASE:
3660 case BC_DECREFS: {
3661 uint32_t target;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003662 const char *debug_string;
Todd Kjosb0117bb2017-05-08 09:16:27 -07003663 bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE;
3664 bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE;
3665 struct binder_ref_data rdata;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003666
3667 if (get_user(target, (uint32_t __user *)ptr))
3668 return -EFAULT;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003669
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003670 ptr += sizeof(uint32_t);
Todd Kjosb0117bb2017-05-08 09:16:27 -07003671 ret = -1;
3672 if (increment && !target) {
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003673 struct binder_node *ctx_mgr_node;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003674 mutex_lock(&context->context_mgr_node_lock);
3675 ctx_mgr_node = context->binder_context_mgr_node;
Todd Kjosb0117bb2017-05-08 09:16:27 -07003676 if (ctx_mgr_node)
3677 ret = binder_inc_ref_for_node(
3678 proc, ctx_mgr_node,
3679 strong, NULL, &rdata);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003680 mutex_unlock(&context->context_mgr_node_lock);
3681 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07003682 if (ret)
3683 ret = binder_update_ref_for_handle(
3684 proc, target, increment, strong,
3685 &rdata);
3686 if (!ret && rdata.desc != target) {
3687 binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
3688 proc->pid, thread->pid,
3689 target, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003690 }
3691 switch (cmd) {
3692 case BC_INCREFS:
3693 debug_string = "IncRefs";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003694 break;
3695 case BC_ACQUIRE:
3696 debug_string = "Acquire";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003697 break;
3698 case BC_RELEASE:
3699 debug_string = "Release";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003700 break;
3701 case BC_DECREFS:
3702 default:
3703 debug_string = "DecRefs";
Todd Kjosb0117bb2017-05-08 09:16:27 -07003704 break;
3705 }
3706 if (ret) {
3707 binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
3708 proc->pid, thread->pid, debug_string,
3709 strong, target, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003710 break;
3711 }
3712 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjosb0117bb2017-05-08 09:16:27 -07003713 "%d:%d %s ref %d desc %d s %d w %d\n",
3714 proc->pid, thread->pid, debug_string,
3715 rdata.debug_id, rdata.desc, rdata.strong,
3716 rdata.weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003717 break;
3718 }
3719 case BC_INCREFS_DONE:
3720 case BC_ACQUIRE_DONE: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003721 binder_uintptr_t node_ptr;
3722 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003723 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003724 bool free_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003725
Arve Hjønnevågda498892014-02-21 14:40:26 -08003726 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003727 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003728 ptr += sizeof(binder_uintptr_t);
3729 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003730 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003731 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003732 node = binder_get_node(proc, node_ptr);
3733 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003734 binder_user_error("%d:%d %s u%016llx no match\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003735 proc->pid, thread->pid,
3736 cmd == BC_INCREFS_DONE ?
3737 "BC_INCREFS_DONE" :
3738 "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003739 (u64)node_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003740 break;
3741 }
3742 if (cookie != node->cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003743 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003744 proc->pid, thread->pid,
3745 cmd == BC_INCREFS_DONE ?
3746 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003747 (u64)node_ptr, node->debug_id,
3748 (u64)cookie, (u64)node->cookie);
Todd Kjosf22abc72017-05-09 11:08:05 -07003749 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003750 break;
3751 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003752 binder_node_inner_lock(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003753 if (cmd == BC_ACQUIRE_DONE) {
3754 if (node->pending_strong_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303755 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003756 proc->pid, thread->pid,
3757 node->debug_id);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003758 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003759 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003760 break;
3761 }
3762 node->pending_strong_ref = 0;
3763 } else {
3764 if (node->pending_weak_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303765 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003766 proc->pid, thread->pid,
3767 node->debug_id);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003768 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003769 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003770 break;
3771 }
3772 node->pending_weak_ref = 0;
3773 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003774 free_node = binder_dec_node_nilocked(node,
3775 cmd == BC_ACQUIRE_DONE, 0);
3776 WARN_ON(free_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003777 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjosf22abc72017-05-09 11:08:05 -07003778 "%d:%d %s node %d ls %d lw %d tr %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003779 proc->pid, thread->pid,
3780 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Todd Kjosf22abc72017-05-09 11:08:05 -07003781 node->debug_id, node->local_strong_refs,
3782 node->local_weak_refs, node->tmp_refs);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003783 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003784 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003785 break;
3786 }
3787 case BC_ATTEMPT_ACQUIRE:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303788 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003789 return -EINVAL;
3790 case BC_ACQUIRE_RESULT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303791 pr_err("BC_ACQUIRE_RESULT not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003792 return -EINVAL;
3793
3794 case BC_FREE_BUFFER: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003795 binder_uintptr_t data_ptr;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003796 struct binder_buffer *buffer;
3797
Arve Hjønnevågda498892014-02-21 14:40:26 -08003798 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003799 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003800 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003801
Todd Kjos076072a2017-04-21 14:32:11 -07003802 buffer = binder_alloc_prepare_to_free(&proc->alloc,
3803 data_ptr);
Todd Kjosd29b73e2018-11-06 15:55:32 -08003804 if (IS_ERR_OR_NULL(buffer)) {
3805 if (PTR_ERR(buffer) == -EPERM) {
3806 binder_user_error(
3807 "%d:%d BC_FREE_BUFFER u%016llx matched unreturned or currently freeing buffer\n",
3808 proc->pid, thread->pid,
3809 (u64)data_ptr);
3810 } else {
3811 binder_user_error(
3812 "%d:%d BC_FREE_BUFFER u%016llx no match\n",
3813 proc->pid, thread->pid,
3814 (u64)data_ptr);
3815 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003816 break;
3817 }
3818 binder_debug(BINDER_DEBUG_FREE_BUFFER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003819 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
3820 proc->pid, thread->pid, (u64)data_ptr,
3821 buffer->debug_id,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003822 buffer->transaction ? "active" : "finished");
3823
3824 if (buffer->transaction) {
3825 buffer->transaction->buffer = NULL;
3826 buffer->transaction = NULL;
3827 }
3828 if (buffer->async_transaction && buffer->target_node) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003829 struct binder_node *buf_node;
3830 struct binder_work *w;
3831
3832 buf_node = buffer->target_node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003833 binder_node_inner_lock(buf_node);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003834 BUG_ON(!buf_node->has_async_transaction);
3835 BUG_ON(buf_node->proc != proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003836 w = binder_dequeue_work_head_ilocked(
3837 &buf_node->async_todo);
Martijn Coenen4501c042017-08-10 13:56:16 +02003838 if (!w) {
Gustavo A. R. Silvae62dd6f2018-01-23 12:04:27 -06003839 buf_node->has_async_transaction = false;
Martijn Coenen4501c042017-08-10 13:56:16 +02003840 } else {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003841 binder_enqueue_work_ilocked(
Martijn Coenen4501c042017-08-10 13:56:16 +02003842 w, &proc->todo);
3843 binder_wakeup_proc_ilocked(proc);
3844 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003845 binder_node_inner_unlock(buf_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003846 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003847 trace_binder_transaction_buffer_release(buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003848 binder_transaction_buffer_release(proc, buffer, NULL);
Todd Kjosd325d372016-10-10 10:40:53 -07003849 binder_alloc_free_buf(&proc->alloc, buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003850 break;
3851 }
3852
Martijn Coenen5a6da532016-09-30 14:10:07 +02003853 case BC_TRANSACTION_SG:
3854 case BC_REPLY_SG: {
3855 struct binder_transaction_data_sg tr;
3856
3857 if (copy_from_user(&tr, ptr, sizeof(tr)))
3858 return -EFAULT;
3859 ptr += sizeof(tr);
3860 binder_transaction(proc, thread, &tr.transaction_data,
3861 cmd == BC_REPLY_SG, tr.buffers_size);
3862 break;
3863 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003864 case BC_TRANSACTION:
3865 case BC_REPLY: {
3866 struct binder_transaction_data tr;
3867
3868 if (copy_from_user(&tr, ptr, sizeof(tr)))
3869 return -EFAULT;
3870 ptr += sizeof(tr);
Martijn Coenen59878d72016-09-30 14:05:40 +02003871 binder_transaction(proc, thread, &tr,
3872 cmd == BC_REPLY, 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003873 break;
3874 }
3875
3876 case BC_REGISTER_LOOPER:
3877 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303878 "%d:%d BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003879 proc->pid, thread->pid);
Todd Kjosd600e902017-05-25 17:35:02 -07003880 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003881 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
3882 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303883 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003884 proc->pid, thread->pid);
3885 } else if (proc->requested_threads == 0) {
3886 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303887 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003888 proc->pid, thread->pid);
3889 } else {
3890 proc->requested_threads--;
3891 proc->requested_threads_started++;
3892 }
3893 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
Todd Kjosd600e902017-05-25 17:35:02 -07003894 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003895 break;
3896 case BC_ENTER_LOOPER:
3897 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303898 "%d:%d BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003899 proc->pid, thread->pid);
3900 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
3901 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303902 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003903 proc->pid, thread->pid);
3904 }
3905 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
3906 break;
3907 case BC_EXIT_LOOPER:
3908 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303909 "%d:%d BC_EXIT_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003910 proc->pid, thread->pid);
3911 thread->looper |= BINDER_LOOPER_STATE_EXITED;
3912 break;
3913
3914 case BC_REQUEST_DEATH_NOTIFICATION:
3915 case BC_CLEAR_DEATH_NOTIFICATION: {
3916 uint32_t target;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003917 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003918 struct binder_ref *ref;
Todd Kjos5346bf32016-10-20 16:43:34 -07003919 struct binder_ref_death *death = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003920
3921 if (get_user(target, (uint32_t __user *)ptr))
3922 return -EFAULT;
3923 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08003924 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003925 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003926 ptr += sizeof(binder_uintptr_t);
Todd Kjos5346bf32016-10-20 16:43:34 -07003927 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3928 /*
3929 * Allocate memory for death notification
3930 * before taking lock
3931 */
3932 death = kzalloc(sizeof(*death), GFP_KERNEL);
3933 if (death == NULL) {
3934 WARN_ON(thread->return_error.cmd !=
3935 BR_OK);
3936 thread->return_error.cmd = BR_ERROR;
Martijn Coenen1af61802017-10-19 15:04:46 +02003937 binder_enqueue_thread_work(
3938 thread,
3939 &thread->return_error.work);
Todd Kjos5346bf32016-10-20 16:43:34 -07003940 binder_debug(
3941 BINDER_DEBUG_FAILED_TRANSACTION,
3942 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
3943 proc->pid, thread->pid);
3944 break;
3945 }
3946 }
3947 binder_proc_lock(proc);
3948 ref = binder_get_ref_olocked(proc, target, false);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003949 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303950 binder_user_error("%d:%d %s invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003951 proc->pid, thread->pid,
3952 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3953 "BC_REQUEST_DEATH_NOTIFICATION" :
3954 "BC_CLEAR_DEATH_NOTIFICATION",
3955 target);
Todd Kjos5346bf32016-10-20 16:43:34 -07003956 binder_proc_unlock(proc);
3957 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003958 break;
3959 }
3960
3961 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003962 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003963 proc->pid, thread->pid,
3964 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3965 "BC_REQUEST_DEATH_NOTIFICATION" :
3966 "BC_CLEAR_DEATH_NOTIFICATION",
Todd Kjosb0117bb2017-05-08 09:16:27 -07003967 (u64)cookie, ref->data.debug_id,
3968 ref->data.desc, ref->data.strong,
3969 ref->data.weak, ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003970
Martijn Coenenf9eac642017-05-22 11:26:23 -07003971 binder_node_lock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003972 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3973 if (ref->death) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303974 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003975 proc->pid, thread->pid);
Martijn Coenenf9eac642017-05-22 11:26:23 -07003976 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003977 binder_proc_unlock(proc);
3978 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003979 break;
3980 }
3981 binder_stats_created(BINDER_STAT_DEATH);
3982 INIT_LIST_HEAD(&death->work.entry);
3983 death->cookie = cookie;
3984 ref->death = death;
3985 if (ref->node->proc == NULL) {
3986 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
Martijn Coenen3bdbe4c2017-08-10 13:50:52 +02003987
3988 binder_inner_proc_lock(proc);
3989 binder_enqueue_work_ilocked(
3990 &ref->death->work, &proc->todo);
3991 binder_wakeup_proc_ilocked(proc);
3992 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003993 }
3994 } else {
3995 if (ref->death == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303996 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003997 proc->pid, thread->pid);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003998 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003999 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004000 break;
4001 }
4002 death = ref->death;
4003 if (death->cookie != cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08004004 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004005 proc->pid, thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004006 (u64)death->cookie,
4007 (u64)cookie);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004008 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07004009 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004010 break;
4011 }
4012 ref->death = NULL;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004013 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004014 if (list_empty(&death->work.entry)) {
4015 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004016 if (thread->looper &
4017 (BINDER_LOOPER_STATE_REGISTERED |
4018 BINDER_LOOPER_STATE_ENTERED))
Martijn Coenen1af61802017-10-19 15:04:46 +02004019 binder_enqueue_thread_work_ilocked(
4020 thread,
4021 &death->work);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004022 else {
4023 binder_enqueue_work_ilocked(
4024 &death->work,
4025 &proc->todo);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004026 binder_wakeup_proc_ilocked(
Martijn Coenen053be422017-06-06 15:17:46 -07004027 proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004028 }
4029 } else {
4030 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
4031 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
4032 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004033 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004034 }
Martijn Coenenf9eac642017-05-22 11:26:23 -07004035 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07004036 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004037 } break;
4038 case BC_DEAD_BINDER_DONE: {
4039 struct binder_work *w;
Arve Hjønnevågda498892014-02-21 14:40:26 -08004040 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004041 struct binder_ref_death *death = NULL;
Seunghun Lee10f62862014-05-01 01:30:23 +09004042
Arve Hjønnevågda498892014-02-21 14:40:26 -08004043 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004044 return -EFAULT;
4045
Lisa Du7a64cd82016-02-17 09:32:52 +08004046 ptr += sizeof(cookie);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004047 binder_inner_proc_lock(proc);
4048 list_for_each_entry(w, &proc->delivered_death,
4049 entry) {
4050 struct binder_ref_death *tmp_death =
4051 container_of(w,
4052 struct binder_ref_death,
4053 work);
Seunghun Lee10f62862014-05-01 01:30:23 +09004054
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004055 if (tmp_death->cookie == cookie) {
4056 death = tmp_death;
4057 break;
4058 }
4059 }
4060 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Todd Kjosf540ce02018-02-07 13:57:37 -08004061 "%d:%d BC_DEAD_BINDER_DONE %016llx found %pK\n",
Arve Hjønnevågda498892014-02-21 14:40:26 -08004062 proc->pid, thread->pid, (u64)cookie,
4063 death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004064 if (death == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08004065 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
4066 proc->pid, thread->pid, (u64)cookie);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004067 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004068 break;
4069 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004070 binder_dequeue_work_ilocked(&death->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004071 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
4072 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004073 if (thread->looper &
4074 (BINDER_LOOPER_STATE_REGISTERED |
4075 BINDER_LOOPER_STATE_ENTERED))
Martijn Coenen1af61802017-10-19 15:04:46 +02004076 binder_enqueue_thread_work_ilocked(
4077 thread, &death->work);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004078 else {
4079 binder_enqueue_work_ilocked(
4080 &death->work,
4081 &proc->todo);
Martijn Coenen053be422017-06-06 15:17:46 -07004082 binder_wakeup_proc_ilocked(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004083 }
4084 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004085 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004086 } break;
4087
4088 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304089 pr_err("%d:%d unknown command %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004090 proc->pid, thread->pid, cmd);
4091 return -EINVAL;
4092 }
4093 *consumed = ptr - buffer;
4094 }
4095 return 0;
4096}
4097
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02004098static void binder_stat_br(struct binder_proc *proc,
4099 struct binder_thread *thread, uint32_t cmd)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004100{
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004101 trace_binder_return(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004102 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07004103 atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
4104 atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
4105 atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004106 }
4107}
4108
Todd Kjos60792612017-05-24 10:51:01 -07004109static int binder_put_node_cmd(struct binder_proc *proc,
4110 struct binder_thread *thread,
4111 void __user **ptrp,
4112 binder_uintptr_t node_ptr,
4113 binder_uintptr_t node_cookie,
4114 int node_debug_id,
4115 uint32_t cmd, const char *cmd_name)
4116{
4117 void __user *ptr = *ptrp;
4118
4119 if (put_user(cmd, (uint32_t __user *)ptr))
4120 return -EFAULT;
4121 ptr += sizeof(uint32_t);
4122
4123 if (put_user(node_ptr, (binder_uintptr_t __user *)ptr))
4124 return -EFAULT;
4125 ptr += sizeof(binder_uintptr_t);
4126
4127 if (put_user(node_cookie, (binder_uintptr_t __user *)ptr))
4128 return -EFAULT;
4129 ptr += sizeof(binder_uintptr_t);
4130
4131 binder_stat_br(proc, thread, cmd);
4132 binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n",
4133 proc->pid, thread->pid, cmd_name, node_debug_id,
4134 (u64)node_ptr, (u64)node_cookie);
4135
4136 *ptrp = ptr;
4137 return 0;
4138}
4139
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004140static int binder_wait_for_work(struct binder_thread *thread,
4141 bool do_proc_work)
4142{
4143 DEFINE_WAIT(wait);
4144 struct binder_proc *proc = thread->proc;
4145 int ret = 0;
4146
4147 freezer_do_not_count();
4148 binder_inner_proc_lock(proc);
4149 for (;;) {
4150 prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE);
4151 if (binder_has_work_ilocked(thread, do_proc_work))
4152 break;
4153 if (do_proc_work)
4154 list_add(&thread->waiting_thread_node,
4155 &proc->waiting_threads);
4156 binder_inner_proc_unlock(proc);
4157 schedule();
4158 binder_inner_proc_lock(proc);
4159 list_del_init(&thread->waiting_thread_node);
4160 if (signal_pending(current)) {
4161 ret = -ERESTARTSYS;
4162 break;
4163 }
4164 }
4165 finish_wait(&thread->wait, &wait);
4166 binder_inner_proc_unlock(proc);
4167 freezer_count();
4168
4169 return ret;
4170}
4171
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004172static int binder_thread_read(struct binder_proc *proc,
4173 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004174 binder_uintptr_t binder_buffer, size_t size,
4175 binder_size_t *consumed, int non_block)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004176{
Arve Hjønnevågda498892014-02-21 14:40:26 -08004177 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004178 void __user *ptr = buffer + *consumed;
4179 void __user *end = buffer + size;
4180
4181 int ret = 0;
4182 int wait_for_proc_work;
4183
4184 if (*consumed == 0) {
4185 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
4186 return -EFAULT;
4187 ptr += sizeof(uint32_t);
4188 }
4189
4190retry:
Martijn Coenen995a36e2017-06-02 13:36:52 -07004191 binder_inner_proc_lock(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004192 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
Martijn Coenen995a36e2017-06-02 13:36:52 -07004193 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004194
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004195 thread->looper |= BINDER_LOOPER_STATE_WAITING;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004196
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004197 trace_binder_wait_for_work(wait_for_proc_work,
4198 !!thread->transaction_stack,
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004199 !binder_worklist_empty(proc, &thread->todo));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004200 if (wait_for_proc_work) {
4201 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4202 BINDER_LOOPER_STATE_ENTERED))) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05304203 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 +09004204 proc->pid, thread->pid, thread->looper);
4205 wait_event_interruptible(binder_user_error_wait,
4206 binder_stop_on_user_error < 2);
4207 }
Martijn Coenenecd972d2017-05-26 10:48:56 -07004208 binder_restore_priority(current, proc->default_priority);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004209 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004210
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004211 if (non_block) {
4212 if (!binder_has_work(thread, wait_for_proc_work))
4213 ret = -EAGAIN;
4214 } else {
4215 ret = binder_wait_for_work(thread, wait_for_proc_work);
4216 }
4217
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004218 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
4219
4220 if (ret)
4221 return ret;
4222
4223 while (1) {
4224 uint32_t cmd;
Todd Kjos63e0afa2019-01-14 09:10:21 -08004225 struct binder_transaction_data_secctx tr;
4226 struct binder_transaction_data *trd = &tr.transaction_data;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004227 struct binder_work *w = NULL;
4228 struct list_head *list = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004229 struct binder_transaction *t = NULL;
Todd Kjos2f993e22017-05-12 14:42:55 -07004230 struct binder_thread *t_from;
Todd Kjos63e0afa2019-01-14 09:10:21 -08004231 size_t trsize = sizeof(*trd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004232
Todd Kjose7f23ed2017-03-21 13:06:01 -07004233 binder_inner_proc_lock(proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004234 if (!binder_worklist_empty_ilocked(&thread->todo))
4235 list = &thread->todo;
4236 else if (!binder_worklist_empty_ilocked(&proc->todo) &&
4237 wait_for_proc_work)
4238 list = &proc->todo;
4239 else {
4240 binder_inner_proc_unlock(proc);
4241
Dmitry Voytik395262a2014-09-08 18:16:34 +04004242 /* no data added */
Todd Kjos6798e6d2017-01-06 14:19:25 -08004243 if (ptr - buffer == 4 && !thread->looper_need_return)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004244 goto retry;
4245 break;
4246 }
4247
Todd Kjose7f23ed2017-03-21 13:06:01 -07004248 if (end - ptr < sizeof(tr) + 4) {
4249 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004250 break;
Todd Kjose7f23ed2017-03-21 13:06:01 -07004251 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004252 w = binder_dequeue_work_head_ilocked(list);
Martijn Coenen1af61802017-10-19 15:04:46 +02004253 if (binder_worklist_empty_ilocked(&thread->todo))
4254 thread->process_todo = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004255
4256 switch (w->type) {
4257 case BINDER_WORK_TRANSACTION: {
Todd Kjose7f23ed2017-03-21 13:06:01 -07004258 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004259 t = container_of(w, struct binder_transaction, work);
4260 } break;
Todd Kjos858b8da2017-04-21 17:35:12 -07004261 case BINDER_WORK_RETURN_ERROR: {
4262 struct binder_error *e = container_of(
4263 w, struct binder_error, work);
4264
4265 WARN_ON(e->cmd == BR_OK);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004266 binder_inner_proc_unlock(proc);
Todd Kjos858b8da2017-04-21 17:35:12 -07004267 if (put_user(e->cmd, (uint32_t __user *)ptr))
4268 return -EFAULT;
宋金时e1b1a8b2018-05-10 02:05:03 +00004269 cmd = e->cmd;
Todd Kjos858b8da2017-04-21 17:35:12 -07004270 e->cmd = BR_OK;
4271 ptr += sizeof(uint32_t);
4272
4273 binder_stat_br(proc, thread, cmd);
Todd Kjos858b8da2017-04-21 17:35:12 -07004274 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004275 case BINDER_WORK_TRANSACTION_COMPLETE: {
Todd Kjose7f23ed2017-03-21 13:06:01 -07004276 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004277 cmd = BR_TRANSACTION_COMPLETE;
4278 if (put_user(cmd, (uint32_t __user *)ptr))
4279 return -EFAULT;
4280 ptr += sizeof(uint32_t);
4281
4282 binder_stat_br(proc, thread, cmd);
4283 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304284 "%d:%d BR_TRANSACTION_COMPLETE\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004285 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004286 kfree(w);
4287 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4288 } break;
4289 case BINDER_WORK_NODE: {
4290 struct binder_node *node = container_of(w, struct binder_node, work);
Todd Kjos60792612017-05-24 10:51:01 -07004291 int strong, weak;
4292 binder_uintptr_t node_ptr = node->ptr;
4293 binder_uintptr_t node_cookie = node->cookie;
4294 int node_debug_id = node->debug_id;
4295 int has_weak_ref;
4296 int has_strong_ref;
4297 void __user *orig_ptr = ptr;
Seunghun Lee10f62862014-05-01 01:30:23 +09004298
Todd Kjos60792612017-05-24 10:51:01 -07004299 BUG_ON(proc != node->proc);
4300 strong = node->internal_strong_refs ||
4301 node->local_strong_refs;
4302 weak = !hlist_empty(&node->refs) ||
Todd Kjosf22abc72017-05-09 11:08:05 -07004303 node->local_weak_refs ||
4304 node->tmp_refs || strong;
Todd Kjos60792612017-05-24 10:51:01 -07004305 has_strong_ref = node->has_strong_ref;
4306 has_weak_ref = node->has_weak_ref;
4307
4308 if (weak && !has_weak_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004309 node->has_weak_ref = 1;
4310 node->pending_weak_ref = 1;
4311 node->local_weak_refs++;
Todd Kjos60792612017-05-24 10:51:01 -07004312 }
4313 if (strong && !has_strong_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004314 node->has_strong_ref = 1;
4315 node->pending_strong_ref = 1;
4316 node->local_strong_refs++;
Todd Kjos60792612017-05-24 10:51:01 -07004317 }
4318 if (!strong && has_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004319 node->has_strong_ref = 0;
Todd Kjos60792612017-05-24 10:51:01 -07004320 if (!weak && has_weak_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004321 node->has_weak_ref = 0;
Todd Kjos60792612017-05-24 10:51:01 -07004322 if (!weak && !strong) {
4323 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4324 "%d:%d node %d u%016llx c%016llx deleted\n",
4325 proc->pid, thread->pid,
4326 node_debug_id,
4327 (u64)node_ptr,
4328 (u64)node_cookie);
4329 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004330 binder_inner_proc_unlock(proc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004331 binder_node_lock(node);
4332 /*
4333 * Acquire the node lock before freeing the
4334 * node to serialize with other threads that
4335 * may have been holding the node lock while
4336 * decrementing this node (avoids race where
4337 * this thread frees while the other thread
4338 * is unlocking the node after the final
4339 * decrement)
4340 */
4341 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004342 binder_free_node(node);
4343 } else
4344 binder_inner_proc_unlock(proc);
4345
Todd Kjos60792612017-05-24 10:51:01 -07004346 if (weak && !has_weak_ref)
4347 ret = binder_put_node_cmd(
4348 proc, thread, &ptr, node_ptr,
4349 node_cookie, node_debug_id,
4350 BR_INCREFS, "BR_INCREFS");
4351 if (!ret && strong && !has_strong_ref)
4352 ret = binder_put_node_cmd(
4353 proc, thread, &ptr, node_ptr,
4354 node_cookie, node_debug_id,
4355 BR_ACQUIRE, "BR_ACQUIRE");
4356 if (!ret && !strong && has_strong_ref)
4357 ret = binder_put_node_cmd(
4358 proc, thread, &ptr, node_ptr,
4359 node_cookie, node_debug_id,
4360 BR_RELEASE, "BR_RELEASE");
4361 if (!ret && !weak && has_weak_ref)
4362 ret = binder_put_node_cmd(
4363 proc, thread, &ptr, node_ptr,
4364 node_cookie, node_debug_id,
4365 BR_DECREFS, "BR_DECREFS");
4366 if (orig_ptr == ptr)
4367 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4368 "%d:%d node %d u%016llx c%016llx state unchanged\n",
4369 proc->pid, thread->pid,
4370 node_debug_id,
4371 (u64)node_ptr,
4372 (u64)node_cookie);
4373 if (ret)
4374 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004375 } break;
4376 case BINDER_WORK_DEAD_BINDER:
4377 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4378 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4379 struct binder_ref_death *death;
4380 uint32_t cmd;
Martijn Coenenf9eac642017-05-22 11:26:23 -07004381 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004382
4383 death = container_of(w, struct binder_ref_death, work);
4384 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
4385 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
4386 else
4387 cmd = BR_DEAD_BINDER;
Martijn Coenenf9eac642017-05-22 11:26:23 -07004388 cookie = death->cookie;
4389
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004390 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004391 "%d:%d %s %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004392 proc->pid, thread->pid,
4393 cmd == BR_DEAD_BINDER ?
4394 "BR_DEAD_BINDER" :
4395 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
Martijn Coenenf9eac642017-05-22 11:26:23 -07004396 (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004397 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
Martijn Coenenf9eac642017-05-22 11:26:23 -07004398 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004399 kfree(death);
4400 binder_stats_deleted(BINDER_STAT_DEATH);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004401 } else {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004402 binder_enqueue_work_ilocked(
4403 w, &proc->delivered_death);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004404 binder_inner_proc_unlock(proc);
4405 }
Martijn Coenenf9eac642017-05-22 11:26:23 -07004406 if (put_user(cmd, (uint32_t __user *)ptr))
4407 return -EFAULT;
4408 ptr += sizeof(uint32_t);
4409 if (put_user(cookie,
4410 (binder_uintptr_t __user *)ptr))
4411 return -EFAULT;
4412 ptr += sizeof(binder_uintptr_t);
4413 binder_stat_br(proc, thread, cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004414 if (cmd == BR_DEAD_BINDER)
4415 goto done; /* DEAD_BINDER notifications can cause transactions */
4416 } break;
4417 }
4418
4419 if (!t)
4420 continue;
4421
4422 BUG_ON(t->buffer == NULL);
4423 if (t->buffer->target_node) {
4424 struct binder_node *target_node = t->buffer->target_node;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004425 struct binder_priority node_prio;
Seunghun Lee10f62862014-05-01 01:30:23 +09004426
Todd Kjos63e0afa2019-01-14 09:10:21 -08004427 trd->target.ptr = target_node->ptr;
4428 trd->cookie = target_node->cookie;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004429 node_prio.sched_policy = target_node->sched_policy;
4430 node_prio.prio = target_node->min_priority;
Martijn Coenenc46810c2017-06-23 10:13:43 -07004431 binder_transaction_priority(current, t, node_prio,
4432 target_node->inherit_rt);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004433 cmd = BR_TRANSACTION;
4434 } else {
Todd Kjos63e0afa2019-01-14 09:10:21 -08004435 trd->target.ptr = 0;
4436 trd->cookie = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004437 cmd = BR_REPLY;
4438 }
Todd Kjos63e0afa2019-01-14 09:10:21 -08004439 trd->code = t->code;
4440 trd->flags = t->flags;
4441 trd->sender_euid = from_kuid(current_user_ns(), t->sender_euid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004442
Todd Kjos2f993e22017-05-12 14:42:55 -07004443 t_from = binder_get_txn_from(t);
4444 if (t_from) {
4445 struct task_struct *sender = t_from->proc->tsk;
Seunghun Lee10f62862014-05-01 01:30:23 +09004446
Todd Kjos63e0afa2019-01-14 09:10:21 -08004447 trd->sender_pid =
4448 task_tgid_nr_ns(sender,
4449 task_active_pid_ns(current));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004450 } else {
Todd Kjos63e0afa2019-01-14 09:10:21 -08004451 trd->sender_pid = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004452 }
4453
Todd Kjos63e0afa2019-01-14 09:10:21 -08004454 trd->data_size = t->buffer->data_size;
4455 trd->offsets_size = t->buffer->offsets_size;
Todd Kjos8d24e2a2019-02-08 10:35:19 -08004456 trd->data.ptr.buffer = (uintptr_t)t->buffer->data;
Todd Kjos63e0afa2019-01-14 09:10:21 -08004457 trd->data.ptr.offsets = trd->data.ptr.buffer +
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004458 ALIGN(t->buffer->data_size,
4459 sizeof(void *));
4460
Todd Kjos63e0afa2019-01-14 09:10:21 -08004461 tr.secctx = t->security_ctx;
4462 if (t->security_ctx) {
4463 cmd = BR_TRANSACTION_SEC_CTX;
4464 trsize = sizeof(tr);
4465 }
Todd Kjos2f993e22017-05-12 14:42:55 -07004466 if (put_user(cmd, (uint32_t __user *)ptr)) {
4467 if (t_from)
4468 binder_thread_dec_tmpref(t_from);
Martijn Coenen3217ccc2017-08-24 15:23:36 +02004469
4470 binder_cleanup_transaction(t, "put_user failed",
4471 BR_FAILED_REPLY);
4472
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004473 return -EFAULT;
Todd Kjos2f993e22017-05-12 14:42:55 -07004474 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004475 ptr += sizeof(uint32_t);
Todd Kjos63e0afa2019-01-14 09:10:21 -08004476 if (copy_to_user(ptr, &tr, trsize)) {
Todd Kjos2f993e22017-05-12 14:42:55 -07004477 if (t_from)
4478 binder_thread_dec_tmpref(t_from);
Martijn Coenen3217ccc2017-08-24 15:23:36 +02004479
4480 binder_cleanup_transaction(t, "copy_to_user failed",
4481 BR_FAILED_REPLY);
4482
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004483 return -EFAULT;
Todd Kjos2f993e22017-05-12 14:42:55 -07004484 }
Todd Kjos63e0afa2019-01-14 09:10:21 -08004485 ptr += trsize;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004486
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004487 trace_binder_transaction_received(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004488 binder_stat_br(proc, thread, cmd);
4489 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004490 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004491 proc->pid, thread->pid,
4492 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
Todd Kjos63e0afa2019-01-14 09:10:21 -08004493 (cmd == BR_TRANSACTION_SEC_CTX) ?
4494 "BR_TRANSACTION_SEC_CTX" : "BR_REPLY",
Todd Kjos2f993e22017-05-12 14:42:55 -07004495 t->debug_id, t_from ? t_from->proc->pid : 0,
4496 t_from ? t_from->pid : 0, cmd,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004497 t->buffer->data_size, t->buffer->offsets_size,
Todd Kjos63e0afa2019-01-14 09:10:21 -08004498 (u64)trd->data.ptr.buffer,
4499 (u64)trd->data.ptr.offsets);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004500
Todd Kjos2f993e22017-05-12 14:42:55 -07004501 if (t_from)
4502 binder_thread_dec_tmpref(t_from);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004503 t->buffer->allow_user_free = 1;
Todd Kjos63e0afa2019-01-14 09:10:21 -08004504 if (cmd != BR_REPLY && !(t->flags & TF_ONE_WAY)) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07004505 binder_inner_proc_lock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004506 t->to_parent = thread->transaction_stack;
4507 t->to_thread = thread;
4508 thread->transaction_stack = t;
Martijn Coenen995a36e2017-06-02 13:36:52 -07004509 binder_inner_proc_unlock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004510 } else {
Todd Kjos21ef40a2017-03-30 18:02:13 -07004511 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004512 }
4513 break;
4514 }
4515
4516done:
4517
4518 *consumed = ptr - buffer;
Todd Kjosd600e902017-05-25 17:35:02 -07004519 binder_inner_proc_lock(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004520 if (proc->requested_threads == 0 &&
4521 list_empty(&thread->proc->waiting_threads) &&
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004522 proc->requested_threads_started < proc->max_threads &&
4523 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4524 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
4525 /*spawn a new thread if we leave this out */) {
4526 proc->requested_threads++;
Todd Kjosd600e902017-05-25 17:35:02 -07004527 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004528 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304529 "%d:%d BR_SPAWN_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004530 proc->pid, thread->pid);
4531 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
4532 return -EFAULT;
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07004533 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
Todd Kjosd600e902017-05-25 17:35:02 -07004534 } else
4535 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004536 return 0;
4537}
4538
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004539static void binder_release_work(struct binder_proc *proc,
4540 struct list_head *list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004541{
4542 struct binder_work *w;
Seunghun Lee10f62862014-05-01 01:30:23 +09004543
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004544 while (1) {
4545 w = binder_dequeue_work_head(proc, list);
4546 if (!w)
4547 return;
4548
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004549 switch (w->type) {
4550 case BINDER_WORK_TRANSACTION: {
4551 struct binder_transaction *t;
4552
4553 t = container_of(w, struct binder_transaction, work);
Martijn Coenen3217ccc2017-08-24 15:23:36 +02004554
4555 binder_cleanup_transaction(t, "process died.",
4556 BR_DEAD_REPLY);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004557 } break;
Todd Kjos858b8da2017-04-21 17:35:12 -07004558 case BINDER_WORK_RETURN_ERROR: {
4559 struct binder_error *e = container_of(
4560 w, struct binder_error, work);
4561
4562 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4563 "undelivered TRANSACTION_ERROR: %u\n",
4564 e->cmd);
4565 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004566 case BINDER_WORK_TRANSACTION_COMPLETE: {
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004567 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304568 "undelivered TRANSACTION_COMPLETE\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004569 kfree(w);
4570 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4571 } break;
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004572 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4573 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4574 struct binder_ref_death *death;
4575
4576 death = container_of(w, struct binder_ref_death, work);
4577 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004578 "undelivered death notification, %016llx\n",
4579 (u64)death->cookie);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004580 kfree(death);
4581 binder_stats_deleted(BINDER_STAT_DEATH);
4582 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004583 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304584 pr_err("unexpected work type, %d, not freed\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004585 w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004586 break;
4587 }
4588 }
4589
4590}
4591
Todd Kjosb4827902017-05-25 15:52:17 -07004592static struct binder_thread *binder_get_thread_ilocked(
4593 struct binder_proc *proc, struct binder_thread *new_thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004594{
4595 struct binder_thread *thread = NULL;
4596 struct rb_node *parent = NULL;
4597 struct rb_node **p = &proc->threads.rb_node;
4598
4599 while (*p) {
4600 parent = *p;
4601 thread = rb_entry(parent, struct binder_thread, rb_node);
4602
4603 if (current->pid < thread->pid)
4604 p = &(*p)->rb_left;
4605 else if (current->pid > thread->pid)
4606 p = &(*p)->rb_right;
4607 else
Todd Kjosb4827902017-05-25 15:52:17 -07004608 return thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004609 }
Todd Kjosb4827902017-05-25 15:52:17 -07004610 if (!new_thread)
4611 return NULL;
4612 thread = new_thread;
4613 binder_stats_created(BINDER_STAT_THREAD);
4614 thread->proc = proc;
4615 thread->pid = current->pid;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004616 get_task_struct(current);
4617 thread->task = current;
Todd Kjosb4827902017-05-25 15:52:17 -07004618 atomic_set(&thread->tmp_ref, 0);
4619 init_waitqueue_head(&thread->wait);
4620 INIT_LIST_HEAD(&thread->todo);
4621 rb_link_node(&thread->rb_node, parent, p);
4622 rb_insert_color(&thread->rb_node, &proc->threads);
4623 thread->looper_need_return = true;
4624 thread->return_error.work.type = BINDER_WORK_RETURN_ERROR;
4625 thread->return_error.cmd = BR_OK;
4626 thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR;
4627 thread->reply_error.cmd = BR_OK;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004628 INIT_LIST_HEAD(&new_thread->waiting_thread_node);
Todd Kjosb4827902017-05-25 15:52:17 -07004629 return thread;
4630}
4631
4632static struct binder_thread *binder_get_thread(struct binder_proc *proc)
4633{
4634 struct binder_thread *thread;
4635 struct binder_thread *new_thread;
4636
4637 binder_inner_proc_lock(proc);
4638 thread = binder_get_thread_ilocked(proc, NULL);
4639 binder_inner_proc_unlock(proc);
4640 if (!thread) {
4641 new_thread = kzalloc(sizeof(*thread), GFP_KERNEL);
4642 if (new_thread == NULL)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004643 return NULL;
Todd Kjosb4827902017-05-25 15:52:17 -07004644 binder_inner_proc_lock(proc);
4645 thread = binder_get_thread_ilocked(proc, new_thread);
4646 binder_inner_proc_unlock(proc);
4647 if (thread != new_thread)
4648 kfree(new_thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004649 }
4650 return thread;
4651}
4652
Todd Kjos2f993e22017-05-12 14:42:55 -07004653static void binder_free_proc(struct binder_proc *proc)
4654{
4655 BUG_ON(!list_empty(&proc->todo));
4656 BUG_ON(!list_empty(&proc->delivered_death));
4657 binder_alloc_deferred_release(&proc->alloc);
4658 put_task_struct(proc->tsk);
4659 binder_stats_deleted(BINDER_STAT_PROC);
4660 kfree(proc);
4661}
4662
4663static void binder_free_thread(struct binder_thread *thread)
4664{
4665 BUG_ON(!list_empty(&thread->todo));
4666 binder_stats_deleted(BINDER_STAT_THREAD);
4667 binder_proc_dec_tmpref(thread->proc);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004668 put_task_struct(thread->task);
Todd Kjos2f993e22017-05-12 14:42:55 -07004669 kfree(thread);
4670}
4671
4672static int binder_thread_release(struct binder_proc *proc,
4673 struct binder_thread *thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004674{
4675 struct binder_transaction *t;
4676 struct binder_transaction *send_reply = NULL;
4677 int active_transactions = 0;
Todd Kjos2f993e22017-05-12 14:42:55 -07004678 struct binder_transaction *last_t = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004679
Todd Kjosb4827902017-05-25 15:52:17 -07004680 binder_inner_proc_lock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07004681 /*
4682 * take a ref on the proc so it survives
4683 * after we remove this thread from proc->threads.
4684 * The corresponding dec is when we actually
4685 * free the thread in binder_free_thread()
4686 */
4687 proc->tmp_ref++;
4688 /*
4689 * take a ref on this thread to ensure it
4690 * survives while we are releasing it
4691 */
4692 atomic_inc(&thread->tmp_ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004693 rb_erase(&thread->rb_node, &proc->threads);
4694 t = thread->transaction_stack;
Todd Kjos2f993e22017-05-12 14:42:55 -07004695 if (t) {
4696 spin_lock(&t->lock);
4697 if (t->to_thread == thread)
4698 send_reply = t;
4699 }
4700 thread->is_dead = true;
4701
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004702 while (t) {
Todd Kjos2f993e22017-05-12 14:42:55 -07004703 last_t = t;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004704 active_transactions++;
4705 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304706 "release %d:%d transaction %d %s, still active\n",
4707 proc->pid, thread->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004708 t->debug_id,
4709 (t->to_thread == thread) ? "in" : "out");
4710
4711 if (t->to_thread == thread) {
4712 t->to_proc = NULL;
4713 t->to_thread = NULL;
4714 if (t->buffer) {
4715 t->buffer->transaction = NULL;
4716 t->buffer = NULL;
4717 }
4718 t = t->to_parent;
4719 } else if (t->from == thread) {
4720 t->from = NULL;
4721 t = t->from_parent;
4722 } else
4723 BUG();
Todd Kjos2f993e22017-05-12 14:42:55 -07004724 spin_unlock(&last_t->lock);
4725 if (t)
4726 spin_lock(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004727 }
Martijn Coenen550c01d2018-01-05 11:27:07 +01004728
4729 /*
4730 * If this thread used poll, make sure we remove the waitqueue
4731 * from any epoll data structures holding it with POLLFREE.
4732 * waitqueue_active() is safe to use here because we're holding
4733 * the inner lock.
4734 */
4735 if ((thread->looper & BINDER_LOOPER_STATE_POLL) &&
4736 waitqueue_active(&thread->wait)) {
4737 wake_up_poll(&thread->wait, POLLHUP | POLLFREE);
4738 }
4739
Todd Kjosb4827902017-05-25 15:52:17 -07004740 binder_inner_proc_unlock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07004741
Martijn Coenen72766d72018-02-16 09:47:15 +01004742 /*
4743 * This is needed to avoid races between wake_up_poll() above and
4744 * and ep_remove_waitqueue() called for other reasons (eg the epoll file
4745 * descriptor being closed); ep_remove_waitqueue() holds an RCU read
4746 * lock, so we can be sure it's done after calling synchronize_rcu().
4747 */
4748 if (thread->looper & BINDER_LOOPER_STATE_POLL)
4749 synchronize_rcu();
4750
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004751 if (send_reply)
4752 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004753 binder_release_work(proc, &thread->todo);
Todd Kjos2f993e22017-05-12 14:42:55 -07004754 binder_thread_dec_tmpref(thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004755 return active_transactions;
4756}
4757
4758static unsigned int binder_poll(struct file *filp,
4759 struct poll_table_struct *wait)
4760{
4761 struct binder_proc *proc = filp->private_data;
4762 struct binder_thread *thread = NULL;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004763 bool wait_for_proc_work;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004764
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004765 thread = binder_get_thread(proc);
Greg Kroah-Hartman6e463bb2018-02-28 17:17:14 +01004766 if (!thread)
Eric Biggers4be5a282018-01-30 23:11:24 -08004767 return POLLERR;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004768
Martijn Coenen995a36e2017-06-02 13:36:52 -07004769 binder_inner_proc_lock(thread->proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004770 thread->looper |= BINDER_LOOPER_STATE_POLL;
4771 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
4772
Martijn Coenen995a36e2017-06-02 13:36:52 -07004773 binder_inner_proc_unlock(thread->proc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004774
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004775 poll_wait(filp, &thread->wait, wait);
4776
Martijn Coenen47810932017-08-10 12:32:00 +02004777 if (binder_has_work(thread, wait_for_proc_work))
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004778 return POLLIN;
4779
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004780 return 0;
4781}
4782
Tair Rzayev78260ac2014-06-03 22:27:21 +03004783static int binder_ioctl_write_read(struct file *filp,
4784 unsigned int cmd, unsigned long arg,
4785 struct binder_thread *thread)
4786{
4787 int ret = 0;
4788 struct binder_proc *proc = filp->private_data;
4789 unsigned int size = _IOC_SIZE(cmd);
4790 void __user *ubuf = (void __user *)arg;
4791 struct binder_write_read bwr;
4792
4793 if (size != sizeof(struct binder_write_read)) {
4794 ret = -EINVAL;
4795 goto out;
4796 }
4797 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
4798 ret = -EFAULT;
4799 goto out;
4800 }
4801 binder_debug(BINDER_DEBUG_READ_WRITE,
4802 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
4803 proc->pid, thread->pid,
4804 (u64)bwr.write_size, (u64)bwr.write_buffer,
4805 (u64)bwr.read_size, (u64)bwr.read_buffer);
4806
4807 if (bwr.write_size > 0) {
4808 ret = binder_thread_write(proc, thread,
4809 bwr.write_buffer,
4810 bwr.write_size,
4811 &bwr.write_consumed);
4812 trace_binder_write_done(ret);
4813 if (ret < 0) {
4814 bwr.read_consumed = 0;
4815 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4816 ret = -EFAULT;
4817 goto out;
4818 }
4819 }
4820 if (bwr.read_size > 0) {
4821 ret = binder_thread_read(proc, thread, bwr.read_buffer,
4822 bwr.read_size,
4823 &bwr.read_consumed,
4824 filp->f_flags & O_NONBLOCK);
4825 trace_binder_read_done(ret);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004826 binder_inner_proc_lock(proc);
4827 if (!binder_worklist_empty_ilocked(&proc->todo))
Martijn Coenen053be422017-06-06 15:17:46 -07004828 binder_wakeup_proc_ilocked(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004829 binder_inner_proc_unlock(proc);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004830 if (ret < 0) {
4831 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4832 ret = -EFAULT;
4833 goto out;
4834 }
4835 }
4836 binder_debug(BINDER_DEBUG_READ_WRITE,
4837 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
4838 proc->pid, thread->pid,
4839 (u64)bwr.write_consumed, (u64)bwr.write_size,
4840 (u64)bwr.read_consumed, (u64)bwr.read_size);
4841 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
4842 ret = -EFAULT;
4843 goto out;
4844 }
4845out:
4846 return ret;
4847}
4848
Todd Kjos63e0afa2019-01-14 09:10:21 -08004849static int binder_ioctl_set_ctx_mgr(struct file *filp,
4850 struct flat_binder_object *fbo)
Tair Rzayev78260ac2014-06-03 22:27:21 +03004851{
4852 int ret = 0;
4853 struct binder_proc *proc = filp->private_data;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004854 struct binder_context *context = proc->context;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004855 struct binder_node *new_node;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004856 kuid_t curr_euid = current_euid();
4857
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004858 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004859 if (context->binder_context_mgr_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004860 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
4861 ret = -EBUSY;
4862 goto out;
4863 }
Stephen Smalley79af7302015-01-21 10:54:10 -05004864 ret = security_binder_set_context_mgr(proc->tsk);
4865 if (ret < 0)
4866 goto out;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004867 if (uid_valid(context->binder_context_mgr_uid)) {
4868 if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004869 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
4870 from_kuid(&init_user_ns, curr_euid),
4871 from_kuid(&init_user_ns,
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004872 context->binder_context_mgr_uid));
Tair Rzayev78260ac2014-06-03 22:27:21 +03004873 ret = -EPERM;
4874 goto out;
4875 }
4876 } else {
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004877 context->binder_context_mgr_uid = curr_euid;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004878 }
Todd Kjos63e0afa2019-01-14 09:10:21 -08004879 new_node = binder_new_node(proc, fbo);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004880 if (!new_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004881 ret = -ENOMEM;
4882 goto out;
4883 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004884 binder_node_lock(new_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004885 new_node->local_weak_refs++;
4886 new_node->local_strong_refs++;
4887 new_node->has_strong_ref = 1;
4888 new_node->has_weak_ref = 1;
4889 context->binder_context_mgr_node = new_node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004890 binder_node_unlock(new_node);
Todd Kjosf22abc72017-05-09 11:08:05 -07004891 binder_put_node(new_node);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004892out:
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004893 mutex_unlock(&context->context_mgr_node_lock);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004894 return ret;
4895}
4896
Martijn Coenen1c57ba42018-08-25 13:50:56 -07004897static int binder_ioctl_get_node_info_for_ref(struct binder_proc *proc,
4898 struct binder_node_info_for_ref *info)
4899{
4900 struct binder_node *node;
4901 struct binder_context *context = proc->context;
4902 __u32 handle = info->handle;
4903
4904 if (info->strong_count || info->weak_count || info->reserved1 ||
4905 info->reserved2 || info->reserved3) {
4906 binder_user_error("%d BINDER_GET_NODE_INFO_FOR_REF: only handle may be non-zero.",
4907 proc->pid);
4908 return -EINVAL;
4909 }
4910
4911 /* This ioctl may only be used by the context manager */
4912 mutex_lock(&context->context_mgr_node_lock);
4913 if (!context->binder_context_mgr_node ||
4914 context->binder_context_mgr_node->proc != proc) {
4915 mutex_unlock(&context->context_mgr_node_lock);
4916 return -EPERM;
4917 }
4918 mutex_unlock(&context->context_mgr_node_lock);
4919
4920 node = binder_get_node_from_ref(proc, handle, true, NULL);
4921 if (!node)
4922 return -EINVAL;
4923
4924 info->strong_count = node->local_strong_refs +
4925 node->internal_strong_refs;
4926 info->weak_count = node->local_weak_refs;
4927
4928 binder_put_node(node);
4929
4930 return 0;
4931}
4932
Colin Cross833babb32017-06-20 13:54:44 -07004933static int binder_ioctl_get_node_debug_info(struct binder_proc *proc,
4934 struct binder_node_debug_info *info) {
4935 struct rb_node *n;
4936 binder_uintptr_t ptr = info->ptr;
4937
4938 memset(info, 0, sizeof(*info));
4939
4940 binder_inner_proc_lock(proc);
4941 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
4942 struct binder_node *node = rb_entry(n, struct binder_node,
4943 rb_node);
4944 if (node->ptr > ptr) {
4945 info->ptr = node->ptr;
4946 info->cookie = node->cookie;
4947 info->has_strong_ref = node->has_strong_ref;
4948 info->has_weak_ref = node->has_weak_ref;
4949 break;
4950 }
4951 }
4952 binder_inner_proc_unlock(proc);
4953
4954 return 0;
4955}
4956
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004957static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4958{
4959 int ret;
4960 struct binder_proc *proc = filp->private_data;
4961 struct binder_thread *thread;
4962 unsigned int size = _IOC_SIZE(cmd);
4963 void __user *ubuf = (void __user *)arg;
4964
Tair Rzayev78260ac2014-06-03 22:27:21 +03004965 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
4966 proc->pid, current->pid, cmd, arg);*/
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004967
Sherry Yang435416b2017-06-22 14:37:45 -07004968 binder_selftest_alloc(&proc->alloc);
4969
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004970 trace_binder_ioctl(cmd, arg);
4971
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004972 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4973 if (ret)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004974 goto err_unlocked;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004975
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004976 thread = binder_get_thread(proc);
4977 if (thread == NULL) {
4978 ret = -ENOMEM;
4979 goto err;
4980 }
4981
4982 switch (cmd) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004983 case BINDER_WRITE_READ:
4984 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
4985 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004986 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004987 break;
Todd Kjosd600e902017-05-25 17:35:02 -07004988 case BINDER_SET_MAX_THREADS: {
4989 int max_threads;
4990
4991 if (copy_from_user(&max_threads, ubuf,
4992 sizeof(max_threads))) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004993 ret = -EINVAL;
4994 goto err;
4995 }
Todd Kjosd600e902017-05-25 17:35:02 -07004996 binder_inner_proc_lock(proc);
4997 proc->max_threads = max_threads;
4998 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004999 break;
Todd Kjosd600e902017-05-25 17:35:02 -07005000 }
Todd Kjos63e0afa2019-01-14 09:10:21 -08005001 case BINDER_SET_CONTEXT_MGR_EXT: {
5002 struct flat_binder_object fbo;
5003
5004 if (copy_from_user(&fbo, ubuf, sizeof(fbo))) {
5005 ret = -EINVAL;
5006 goto err;
5007 }
5008 ret = binder_ioctl_set_ctx_mgr(filp, &fbo);
5009 if (ret)
5010 goto err;
5011 break;
5012 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005013 case BINDER_SET_CONTEXT_MGR:
Todd Kjos63e0afa2019-01-14 09:10:21 -08005014 ret = binder_ioctl_set_ctx_mgr(filp, NULL);
Tair Rzayev78260ac2014-06-03 22:27:21 +03005015 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005016 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005017 break;
5018 case BINDER_THREAD_EXIT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05305019 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005020 proc->pid, thread->pid);
Todd Kjos2f993e22017-05-12 14:42:55 -07005021 binder_thread_release(proc, thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005022 thread = NULL;
5023 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02005024 case BINDER_VERSION: {
5025 struct binder_version __user *ver = ubuf;
5026
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005027 if (size != sizeof(struct binder_version)) {
5028 ret = -EINVAL;
5029 goto err;
5030 }
Mathieu Maret36c89c02014-04-15 12:03:05 +02005031 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
5032 &ver->protocol_version)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005033 ret = -EINVAL;
5034 goto err;
5035 }
5036 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02005037 }
Martijn Coenen1c57ba42018-08-25 13:50:56 -07005038 case BINDER_GET_NODE_INFO_FOR_REF: {
5039 struct binder_node_info_for_ref info;
5040
5041 if (copy_from_user(&info, ubuf, sizeof(info))) {
5042 ret = -EFAULT;
5043 goto err;
5044 }
5045
5046 ret = binder_ioctl_get_node_info_for_ref(proc, &info);
5047 if (ret < 0)
5048 goto err;
5049
5050 if (copy_to_user(ubuf, &info, sizeof(info))) {
5051 ret = -EFAULT;
5052 goto err;
5053 }
5054
5055 break;
5056 }
Colin Cross833babb32017-06-20 13:54:44 -07005057 case BINDER_GET_NODE_DEBUG_INFO: {
5058 struct binder_node_debug_info info;
5059
5060 if (copy_from_user(&info, ubuf, sizeof(info))) {
5061 ret = -EFAULT;
5062 goto err;
5063 }
5064
5065 ret = binder_ioctl_get_node_debug_info(proc, &info);
5066 if (ret < 0)
5067 goto err;
5068
5069 if (copy_to_user(ubuf, &info, sizeof(info))) {
5070 ret = -EFAULT;
5071 goto err;
5072 }
5073 break;
5074 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005075 default:
5076 ret = -EINVAL;
5077 goto err;
5078 }
5079 ret = 0;
5080err:
5081 if (thread)
Todd Kjos6798e6d2017-01-06 14:19:25 -08005082 thread->looper_need_return = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005083 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
5084 if (ret && ret != -ERESTARTSYS)
Anmol Sarma56b468f2012-10-30 22:35:43 +05305085 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 -07005086err_unlocked:
5087 trace_binder_ioctl_done(ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005088 return ret;
5089}
5090
5091static void binder_vma_open(struct vm_area_struct *vma)
5092{
5093 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09005094
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005095 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05305096 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005097 proc->pid, vma->vm_start, vma->vm_end,
5098 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
5099 (unsigned long)pgprot_val(vma->vm_page_prot));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005100}
5101
5102static void binder_vma_close(struct vm_area_struct *vma)
5103{
5104 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09005105
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005106 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05305107 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005108 proc->pid, vma->vm_start, vma->vm_end,
5109 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
5110 (unsigned long)pgprot_val(vma->vm_page_prot));
Todd Kjosd325d372016-10-10 10:40:53 -07005111 binder_alloc_vma_close(&proc->alloc);
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005112 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005113}
5114
Vinayak Menonddac7d52014-06-02 18:17:59 +05305115static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
5116{
5117 return VM_FAULT_SIGBUS;
5118}
5119
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07005120static const struct vm_operations_struct binder_vm_ops = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005121 .open = binder_vma_open,
5122 .close = binder_vma_close,
Vinayak Menonddac7d52014-06-02 18:17:59 +05305123 .fault = binder_vm_fault,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005124};
5125
Todd Kjosd325d372016-10-10 10:40:53 -07005126static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
5127{
5128 int ret;
5129 struct binder_proc *proc = filp->private_data;
5130 const char *failure_string;
5131
5132 if (proc->tsk != current->group_leader)
5133 return -EINVAL;
5134
5135 if ((vma->vm_end - vma->vm_start) > SZ_4M)
5136 vma->vm_end = vma->vm_start + SZ_4M;
5137
5138 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5139 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
5140 __func__, proc->pid, vma->vm_start, vma->vm_end,
5141 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
5142 (unsigned long)pgprot_val(vma->vm_page_prot));
5143
5144 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
5145 ret = -EPERM;
5146 failure_string = "bad vm_flags";
5147 goto err_bad_arg;
5148 }
Minchan Kim2cafd5b2018-05-07 23:15:37 +09005149 vma->vm_flags |= VM_DONTCOPY | VM_MIXEDMAP;
5150 vma->vm_flags &= ~VM_MAYWRITE;
5151
Todd Kjosd325d372016-10-10 10:40:53 -07005152 vma->vm_ops = &binder_vm_ops;
5153 vma->vm_private_data = proc;
5154
5155 ret = binder_alloc_mmap_handler(&proc->alloc, vma);
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005156 if (ret)
5157 return ret;
Todd Kjosfbb43392017-11-27 09:32:33 -08005158 mutex_lock(&proc->files_lock);
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005159 proc->files = get_files_struct(current);
Todd Kjosfbb43392017-11-27 09:32:33 -08005160 mutex_unlock(&proc->files_lock);
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005161 return 0;
Todd Kjosd325d372016-10-10 10:40:53 -07005162
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005163err_bad_arg:
Elad Wexler6b646402017-12-29 11:03:37 +02005164 pr_err("%s: %d %lx-%lx %s failed %d\n", __func__,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005165 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
5166 return ret;
5167}
5168
5169static int binder_open(struct inode *nodp, struct file *filp)
5170{
5171 struct binder_proc *proc;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005172 struct binder_device *binder_dev;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005173
Elad Wexler6b646402017-12-29 11:03:37 +02005174 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "%s: %d:%d\n", __func__,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005175 current->group_leader->pid, current->pid);
5176
5177 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
5178 if (proc == NULL)
5179 return -ENOMEM;
Todd Kjosfc7a7e22017-05-29 16:44:24 -07005180 spin_lock_init(&proc->inner_lock);
5181 spin_lock_init(&proc->outer_lock);
Martijn Coenen872c26e2017-03-07 15:51:18 +01005182 get_task_struct(current->group_leader);
5183 proc->tsk = current->group_leader;
Todd Kjosfbb43392017-11-27 09:32:33 -08005184 mutex_init(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005185 INIT_LIST_HEAD(&proc->todo);
Martijn Coenen57b2ac62017-06-06 17:04:42 -07005186 if (binder_supported_policy(current->policy)) {
5187 proc->default_priority.sched_policy = current->policy;
5188 proc->default_priority.prio = current->normal_prio;
5189 } else {
5190 proc->default_priority.sched_policy = SCHED_NORMAL;
5191 proc->default_priority.prio = NICE_TO_PRIO(0);
5192 }
5193
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005194 binder_dev = container_of(filp->private_data, struct binder_device,
5195 miscdev);
5196 proc->context = &binder_dev->context;
Todd Kjosd325d372016-10-10 10:40:53 -07005197 binder_alloc_init(&proc->alloc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07005198
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005199 binder_stats_created(BINDER_STAT_PROC);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005200 proc->pid = current->group_leader->pid;
5201 INIT_LIST_HEAD(&proc->delivered_death);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005202 INIT_LIST_HEAD(&proc->waiting_threads);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005203 filp->private_data = proc;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07005204
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005205 mutex_lock(&binder_procs_lock);
5206 hlist_add_head(&proc->proc_node, &binder_procs);
5207 mutex_unlock(&binder_procs_lock);
5208
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005209 if (binder_debugfs_dir_entry_proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005210 char strbuf[11];
Seunghun Lee10f62862014-05-01 01:30:23 +09005211
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005212 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005213 /*
5214 * proc debug entries are shared between contexts, so
5215 * this will fail if the process tries to open the driver
5216 * again with a different context. The priting code will
5217 * anyway print all contexts that a given PID has, so this
5218 * is not a problem.
5219 */
Harsh Shandilya174562a2017-12-22 19:37:02 +05305220 proc->debugfs_entry = debugfs_create_file(strbuf, 0444,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005221 binder_debugfs_dir_entry_proc,
5222 (void *)(unsigned long)proc->pid,
5223 &binder_proc_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005224 }
5225
5226 return 0;
5227}
5228
5229static int binder_flush(struct file *filp, fl_owner_t id)
5230{
5231 struct binder_proc *proc = filp->private_data;
5232
5233 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
5234
5235 return 0;
5236}
5237
5238static void binder_deferred_flush(struct binder_proc *proc)
5239{
5240 struct rb_node *n;
5241 int wake_count = 0;
Seunghun Lee10f62862014-05-01 01:30:23 +09005242
Todd Kjosb4827902017-05-25 15:52:17 -07005243 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005244 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
5245 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
Seunghun Lee10f62862014-05-01 01:30:23 +09005246
Todd Kjos6798e6d2017-01-06 14:19:25 -08005247 thread->looper_need_return = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005248 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
5249 wake_up_interruptible(&thread->wait);
5250 wake_count++;
5251 }
5252 }
Todd Kjosb4827902017-05-25 15:52:17 -07005253 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005254
5255 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5256 "binder_flush: %d woke %d threads\n", proc->pid,
5257 wake_count);
5258}
5259
5260static int binder_release(struct inode *nodp, struct file *filp)
5261{
5262 struct binder_proc *proc = filp->private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09005263
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005264 debugfs_remove(proc->debugfs_entry);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005265 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
5266
5267 return 0;
5268}
5269
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005270static int binder_node_release(struct binder_node *node, int refs)
5271{
5272 struct binder_ref *ref;
5273 int death = 0;
Todd Kjose7f23ed2017-03-21 13:06:01 -07005274 struct binder_proc *proc = node->proc;
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005275
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005276 binder_release_work(proc, &node->async_todo);
Todd Kjose7f23ed2017-03-21 13:06:01 -07005277
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005278 binder_node_lock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07005279 binder_inner_proc_lock(proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005280 binder_dequeue_work_ilocked(&node->work);
Todd Kjosf22abc72017-05-09 11:08:05 -07005281 /*
5282 * The caller must have taken a temporary ref on the node,
5283 */
5284 BUG_ON(!node->tmp_refs);
5285 if (hlist_empty(&node->refs) && node->tmp_refs == 1) {
Todd Kjose7f23ed2017-03-21 13:06:01 -07005286 binder_inner_proc_unlock(proc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005287 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07005288 binder_free_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005289
5290 return refs;
5291 }
5292
5293 node->proc = NULL;
5294 node->local_strong_refs = 0;
5295 node->local_weak_refs = 0;
Todd Kjose7f23ed2017-03-21 13:06:01 -07005296 binder_inner_proc_unlock(proc);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005297
5298 spin_lock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005299 hlist_add_head(&node->dead_node, &binder_dead_nodes);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005300 spin_unlock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005301
5302 hlist_for_each_entry(ref, &node->refs, node_entry) {
5303 refs++;
Martijn Coenenf9eac642017-05-22 11:26:23 -07005304 /*
5305 * Need the node lock to synchronize
5306 * with new notification requests and the
5307 * inner lock to synchronize with queued
5308 * death notifications.
5309 */
5310 binder_inner_proc_lock(ref->proc);
5311 if (!ref->death) {
5312 binder_inner_proc_unlock(ref->proc);
Arve Hjønnevåge194fd82014-02-17 13:58:29 -08005313 continue;
Martijn Coenenf9eac642017-05-22 11:26:23 -07005314 }
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005315
5316 death++;
5317
Martijn Coenenf9eac642017-05-22 11:26:23 -07005318 BUG_ON(!list_empty(&ref->death->work.entry));
5319 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
5320 binder_enqueue_work_ilocked(&ref->death->work,
5321 &ref->proc->todo);
Martijn Coenen053be422017-06-06 15:17:46 -07005322 binder_wakeup_proc_ilocked(ref->proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005323 binder_inner_proc_unlock(ref->proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005324 }
5325
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005326 binder_debug(BINDER_DEBUG_DEAD_BINDER,
5327 "node %d now dead, refs %d, death %d\n",
5328 node->debug_id, refs, death);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005329 binder_node_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07005330 binder_put_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005331
5332 return refs;
5333}
5334
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005335static void binder_deferred_release(struct binder_proc *proc)
5336{
Martijn Coenen0b3311e2016-09-30 15:51:48 +02005337 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005338 struct rb_node *n;
Todd Kjosd325d372016-10-10 10:40:53 -07005339 int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005340
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005341 BUG_ON(proc->files);
5342
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005343 mutex_lock(&binder_procs_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005344 hlist_del(&proc->proc_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005345 mutex_unlock(&binder_procs_lock);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005346
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005347 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02005348 if (context->binder_context_mgr_node &&
5349 context->binder_context_mgr_node->proc == proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005350 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01005351 "%s: %d context_mgr_node gone\n",
5352 __func__, proc->pid);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02005353 context->binder_context_mgr_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005354 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005355 mutex_unlock(&context->context_mgr_node_lock);
Todd Kjosb4827902017-05-25 15:52:17 -07005356 binder_inner_proc_lock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07005357 /*
5358 * Make sure proc stays alive after we
5359 * remove all the threads
5360 */
5361 proc->tmp_ref++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005362
Todd Kjos2f993e22017-05-12 14:42:55 -07005363 proc->is_dead = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005364 threads = 0;
5365 active_transactions = 0;
5366 while ((n = rb_first(&proc->threads))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005367 struct binder_thread *thread;
5368
5369 thread = rb_entry(n, struct binder_thread, rb_node);
Todd Kjosb4827902017-05-25 15:52:17 -07005370 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005371 threads++;
Todd Kjos2f993e22017-05-12 14:42:55 -07005372 active_transactions += binder_thread_release(proc, thread);
Todd Kjosb4827902017-05-25 15:52:17 -07005373 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005374 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005375
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005376 nodes = 0;
5377 incoming_refs = 0;
5378 while ((n = rb_first(&proc->nodes))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005379 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005380
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005381 node = rb_entry(n, struct binder_node, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005382 nodes++;
Todd Kjosf22abc72017-05-09 11:08:05 -07005383 /*
5384 * take a temporary ref on the node before
5385 * calling binder_node_release() which will either
5386 * kfree() the node or call binder_put_node()
5387 */
Todd Kjos425d23f2017-06-12 12:07:26 -07005388 binder_inc_node_tmpref_ilocked(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005389 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjos425d23f2017-06-12 12:07:26 -07005390 binder_inner_proc_unlock(proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005391 incoming_refs = binder_node_release(node, incoming_refs);
Todd Kjos425d23f2017-06-12 12:07:26 -07005392 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005393 }
Todd Kjos425d23f2017-06-12 12:07:26 -07005394 binder_inner_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005395
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005396 outgoing_refs = 0;
Todd Kjos5346bf32016-10-20 16:43:34 -07005397 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005398 while ((n = rb_first(&proc->refs_by_desc))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005399 struct binder_ref *ref;
5400
5401 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005402 outgoing_refs++;
Todd Kjos5346bf32016-10-20 16:43:34 -07005403 binder_cleanup_ref_olocked(ref);
5404 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07005405 binder_free_ref(ref);
Todd Kjos5346bf32016-10-20 16:43:34 -07005406 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005407 }
Todd Kjos5346bf32016-10-20 16:43:34 -07005408 binder_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005409
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005410 binder_release_work(proc, &proc->todo);
5411 binder_release_work(proc, &proc->delivered_death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005412
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005413 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Todd Kjosd325d372016-10-10 10:40:53 -07005414 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01005415 __func__, proc->pid, threads, nodes, incoming_refs,
Todd Kjosd325d372016-10-10 10:40:53 -07005416 outgoing_refs, active_transactions);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005417
Todd Kjos2f993e22017-05-12 14:42:55 -07005418 binder_proc_dec_tmpref(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005419}
5420
5421static void binder_deferred_func(struct work_struct *work)
5422{
5423 struct binder_proc *proc;
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005424 struct files_struct *files;
5425
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005426 int defer;
Seunghun Lee10f62862014-05-01 01:30:23 +09005427
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005428 do {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005429 mutex_lock(&binder_deferred_lock);
5430 if (!hlist_empty(&binder_deferred_list)) {
5431 proc = hlist_entry(binder_deferred_list.first,
5432 struct binder_proc, deferred_work_node);
5433 hlist_del_init(&proc->deferred_work_node);
5434 defer = proc->deferred_work;
5435 proc->deferred_work = 0;
5436 } else {
5437 proc = NULL;
5438 defer = 0;
5439 }
5440 mutex_unlock(&binder_deferred_lock);
5441
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005442 files = NULL;
5443 if (defer & BINDER_DEFERRED_PUT_FILES) {
Todd Kjosfbb43392017-11-27 09:32:33 -08005444 mutex_lock(&proc->files_lock);
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005445 files = proc->files;
5446 if (files)
5447 proc->files = NULL;
Todd Kjosfbb43392017-11-27 09:32:33 -08005448 mutex_unlock(&proc->files_lock);
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005449 }
5450
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005451 if (defer & BINDER_DEFERRED_FLUSH)
5452 binder_deferred_flush(proc);
5453
5454 if (defer & BINDER_DEFERRED_RELEASE)
5455 binder_deferred_release(proc); /* frees proc */
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005456
5457 if (files)
5458 put_files_struct(files);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005459 } while (proc);
5460}
5461static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
5462
5463static void
5464binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
5465{
5466 mutex_lock(&binder_deferred_lock);
5467 proc->deferred_work |= defer;
5468 if (hlist_unhashed(&proc->deferred_work_node)) {
5469 hlist_add_head(&proc->deferred_work_node,
5470 &binder_deferred_list);
Bhaktipriya Shridhar1beba522016-08-13 22:16:24 +05305471 schedule_work(&binder_deferred_work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005472 }
5473 mutex_unlock(&binder_deferred_lock);
5474}
5475
Todd Kjos6d241a42017-04-21 14:32:11 -07005476static void print_binder_transaction_ilocked(struct seq_file *m,
5477 struct binder_proc *proc,
5478 const char *prefix,
5479 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005480{
Todd Kjos6d241a42017-04-21 14:32:11 -07005481 struct binder_proc *to_proc;
5482 struct binder_buffer *buffer = t->buffer;
5483
Todd Kjos2f993e22017-05-12 14:42:55 -07005484 spin_lock(&t->lock);
Todd Kjos6d241a42017-04-21 14:32:11 -07005485 to_proc = t->to_proc;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005486 seq_printf(m,
Todd Kjosf540ce02018-02-07 13:57:37 -08005487 "%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 -07005488 prefix, t->debug_id, t,
5489 t->from ? t->from->proc->pid : 0,
5490 t->from ? t->from->pid : 0,
Todd Kjos6d241a42017-04-21 14:32:11 -07005491 to_proc ? to_proc->pid : 0,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005492 t->to_thread ? t->to_thread->pid : 0,
Martijn Coenen57b2ac62017-06-06 17:04:42 -07005493 t->code, t->flags, t->priority.sched_policy,
5494 t->priority.prio, t->need_reply);
Todd Kjos2f993e22017-05-12 14:42:55 -07005495 spin_unlock(&t->lock);
5496
Todd Kjos6d241a42017-04-21 14:32:11 -07005497 if (proc != to_proc) {
5498 /*
5499 * Can only safely deref buffer if we are holding the
5500 * correct proc inner lock for this node
5501 */
5502 seq_puts(m, "\n");
5503 return;
5504 }
5505
5506 if (buffer == NULL) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005507 seq_puts(m, " buffer free\n");
5508 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005509 }
Todd Kjos6d241a42017-04-21 14:32:11 -07005510 if (buffer->target_node)
5511 seq_printf(m, " node %d", buffer->target_node->debug_id);
Todd Kjosf540ce02018-02-07 13:57:37 -08005512 seq_printf(m, " size %zd:%zd data %pK\n",
Todd Kjos6d241a42017-04-21 14:32:11 -07005513 buffer->data_size, buffer->offsets_size,
5514 buffer->data);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005515}
5516
Todd Kjos6d241a42017-04-21 14:32:11 -07005517static void print_binder_work_ilocked(struct seq_file *m,
5518 struct binder_proc *proc,
5519 const char *prefix,
5520 const char *transaction_prefix,
5521 struct binder_work *w)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005522{
5523 struct binder_node *node;
5524 struct binder_transaction *t;
5525
5526 switch (w->type) {
5527 case BINDER_WORK_TRANSACTION:
5528 t = container_of(w, struct binder_transaction, work);
Todd Kjos6d241a42017-04-21 14:32:11 -07005529 print_binder_transaction_ilocked(
5530 m, proc, transaction_prefix, t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005531 break;
Todd Kjos858b8da2017-04-21 17:35:12 -07005532 case BINDER_WORK_RETURN_ERROR: {
5533 struct binder_error *e = container_of(
5534 w, struct binder_error, work);
5535
5536 seq_printf(m, "%stransaction error: %u\n",
5537 prefix, e->cmd);
5538 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005539 case BINDER_WORK_TRANSACTION_COMPLETE:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005540 seq_printf(m, "%stransaction complete\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005541 break;
5542 case BINDER_WORK_NODE:
5543 node = container_of(w, struct binder_node, work);
Arve Hjønnevågda498892014-02-21 14:40:26 -08005544 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
5545 prefix, node->debug_id,
5546 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005547 break;
5548 case BINDER_WORK_DEAD_BINDER:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005549 seq_printf(m, "%shas dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005550 break;
5551 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005552 seq_printf(m, "%shas cleared dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005553 break;
5554 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005555 seq_printf(m, "%shas cleared death notification\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005556 break;
5557 default:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005558 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005559 break;
5560 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005561}
5562
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005563static void print_binder_thread_ilocked(struct seq_file *m,
5564 struct binder_thread *thread,
5565 int print_always)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005566{
5567 struct binder_transaction *t;
5568 struct binder_work *w;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005569 size_t start_pos = m->count;
5570 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005571
Todd Kjos2f993e22017-05-12 14:42:55 -07005572 seq_printf(m, " thread %d: l %02x need_return %d tr %d\n",
Todd Kjos6798e6d2017-01-06 14:19:25 -08005573 thread->pid, thread->looper,
Todd Kjos2f993e22017-05-12 14:42:55 -07005574 thread->looper_need_return,
5575 atomic_read(&thread->tmp_ref));
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005576 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005577 t = thread->transaction_stack;
5578 while (t) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005579 if (t->from == thread) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005580 print_binder_transaction_ilocked(m, thread->proc,
5581 " outgoing transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005582 t = t->from_parent;
5583 } else if (t->to_thread == thread) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005584 print_binder_transaction_ilocked(m, thread->proc,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005585 " incoming transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005586 t = t->to_parent;
5587 } else {
Todd Kjos6d241a42017-04-21 14:32:11 -07005588 print_binder_transaction_ilocked(m, thread->proc,
5589 " bad transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005590 t = NULL;
5591 }
5592 }
5593 list_for_each_entry(w, &thread->todo, entry) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005594 print_binder_work_ilocked(m, thread->proc, " ",
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005595 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005596 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005597 if (!print_always && m->count == header_pos)
5598 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005599}
5600
Todd Kjos425d23f2017-06-12 12:07:26 -07005601static void print_binder_node_nilocked(struct seq_file *m,
5602 struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005603{
5604 struct binder_ref *ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005605 struct binder_work *w;
5606 int count;
5607
5608 count = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08005609 hlist_for_each_entry(ref, &node->refs, node_entry)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005610 count++;
5611
Martijn Coenen6aac9792017-06-07 09:29:14 -07005612 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 -08005613 node->debug_id, (u64)node->ptr, (u64)node->cookie,
Martijn Coenen6aac9792017-06-07 09:29:14 -07005614 node->sched_policy, node->min_priority,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005615 node->has_strong_ref, node->has_weak_ref,
5616 node->local_strong_refs, node->local_weak_refs,
Todd Kjosf22abc72017-05-09 11:08:05 -07005617 node->internal_strong_refs, count, node->tmp_refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005618 if (count) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005619 seq_puts(m, " proc");
Sasha Levinb67bfe02013-02-27 17:06:00 -08005620 hlist_for_each_entry(ref, &node->refs, node_entry)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005621 seq_printf(m, " %d", ref->proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005622 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005623 seq_puts(m, "\n");
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005624 if (node->proc) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005625 list_for_each_entry(w, &node->async_todo, entry)
Todd Kjos6d241a42017-04-21 14:32:11 -07005626 print_binder_work_ilocked(m, node->proc, " ",
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005627 " pending async transaction", w);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005628 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005629}
5630
Todd Kjos5346bf32016-10-20 16:43:34 -07005631static void print_binder_ref_olocked(struct seq_file *m,
5632 struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005633{
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005634 binder_node_lock(ref->node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07005635 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
5636 ref->data.debug_id, ref->data.desc,
5637 ref->node->proc ? "" : "dead ",
5638 ref->node->debug_id, ref->data.strong,
5639 ref->data.weak, ref->death);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005640 binder_node_unlock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005641}
5642
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005643static void print_binder_proc(struct seq_file *m,
5644 struct binder_proc *proc, int print_all)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005645{
5646 struct binder_work *w;
5647 struct rb_node *n;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005648 size_t start_pos = m->count;
5649 size_t header_pos;
Todd Kjos425d23f2017-06-12 12:07:26 -07005650 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005651
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005652 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005653 seq_printf(m, "context %s\n", proc->context->name);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005654 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005655
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005656 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005657 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005658 print_binder_thread_ilocked(m, rb_entry(n, struct binder_thread,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005659 rb_node), print_all);
Todd Kjos425d23f2017-06-12 12:07:26 -07005660
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005661 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005662 struct binder_node *node = rb_entry(n, struct binder_node,
5663 rb_node);
Todd Kjosd79aa412018-12-05 15:19:26 -08005664 if (!print_all && !node->has_async_transaction)
5665 continue;
5666
Todd Kjos425d23f2017-06-12 12:07:26 -07005667 /*
5668 * take a temporary reference on the node so it
5669 * survives and isn't removed from the tree
5670 * while we print it.
5671 */
5672 binder_inc_node_tmpref_ilocked(node);
5673 /* Need to drop inner lock to take node lock */
5674 binder_inner_proc_unlock(proc);
5675 if (last_node)
5676 binder_put_node(last_node);
5677 binder_node_inner_lock(node);
5678 print_binder_node_nilocked(m, node);
5679 binder_node_inner_unlock(node);
5680 last_node = node;
5681 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005682 }
Todd Kjos425d23f2017-06-12 12:07:26 -07005683 binder_inner_proc_unlock(proc);
5684 if (last_node)
5685 binder_put_node(last_node);
5686
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005687 if (print_all) {
Todd Kjos5346bf32016-10-20 16:43:34 -07005688 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005689 for (n = rb_first(&proc->refs_by_desc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005690 n != NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005691 n = rb_next(n))
Todd Kjos5346bf32016-10-20 16:43:34 -07005692 print_binder_ref_olocked(m, rb_entry(n,
5693 struct binder_ref,
5694 rb_node_desc));
5695 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005696 }
Todd Kjosd325d372016-10-10 10:40:53 -07005697 binder_alloc_print_allocated(m, &proc->alloc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005698 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005699 list_for_each_entry(w, &proc->todo, entry)
Todd Kjos6d241a42017-04-21 14:32:11 -07005700 print_binder_work_ilocked(m, proc, " ",
5701 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005702 list_for_each_entry(w, &proc->delivered_death, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005703 seq_puts(m, " has delivered dead binder\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005704 break;
5705 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005706 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005707 if (!print_all && m->count == header_pos)
5708 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005709}
5710
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005711static const char * const binder_return_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005712 "BR_ERROR",
5713 "BR_OK",
5714 "BR_TRANSACTION",
5715 "BR_REPLY",
5716 "BR_ACQUIRE_RESULT",
5717 "BR_DEAD_REPLY",
5718 "BR_TRANSACTION_COMPLETE",
5719 "BR_INCREFS",
5720 "BR_ACQUIRE",
5721 "BR_RELEASE",
5722 "BR_DECREFS",
5723 "BR_ATTEMPT_ACQUIRE",
5724 "BR_NOOP",
5725 "BR_SPAWN_LOOPER",
5726 "BR_FINISHED",
5727 "BR_DEAD_BINDER",
5728 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
5729 "BR_FAILED_REPLY"
5730};
5731
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005732static const char * const binder_command_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005733 "BC_TRANSACTION",
5734 "BC_REPLY",
5735 "BC_ACQUIRE_RESULT",
5736 "BC_FREE_BUFFER",
5737 "BC_INCREFS",
5738 "BC_ACQUIRE",
5739 "BC_RELEASE",
5740 "BC_DECREFS",
5741 "BC_INCREFS_DONE",
5742 "BC_ACQUIRE_DONE",
5743 "BC_ATTEMPT_ACQUIRE",
5744 "BC_REGISTER_LOOPER",
5745 "BC_ENTER_LOOPER",
5746 "BC_EXIT_LOOPER",
5747 "BC_REQUEST_DEATH_NOTIFICATION",
5748 "BC_CLEAR_DEATH_NOTIFICATION",
Martijn Coenen5a6da532016-09-30 14:10:07 +02005749 "BC_DEAD_BINDER_DONE",
5750 "BC_TRANSACTION_SG",
5751 "BC_REPLY_SG",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005752};
5753
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005754static const char * const binder_objstat_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005755 "proc",
5756 "thread",
5757 "node",
5758 "ref",
5759 "death",
5760 "transaction",
5761 "transaction_complete"
5762};
5763
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005764static void print_binder_stats(struct seq_file *m, const char *prefix,
5765 struct binder_stats *stats)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005766{
5767 int i;
5768
5769 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005770 ARRAY_SIZE(binder_command_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005771 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005772 int temp = atomic_read(&stats->bc[i]);
5773
5774 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005775 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005776 binder_command_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005777 }
5778
5779 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005780 ARRAY_SIZE(binder_return_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005781 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005782 int temp = atomic_read(&stats->br[i]);
5783
5784 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005785 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005786 binder_return_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005787 }
5788
5789 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005790 ARRAY_SIZE(binder_objstat_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005791 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005792 ARRAY_SIZE(stats->obj_deleted));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005793 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005794 int created = atomic_read(&stats->obj_created[i]);
5795 int deleted = atomic_read(&stats->obj_deleted[i]);
5796
5797 if (created || deleted)
5798 seq_printf(m, "%s%s: active %d total %d\n",
5799 prefix,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005800 binder_objstat_strings[i],
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005801 created - deleted,
5802 created);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005803 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005804}
5805
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005806static void print_binder_proc_stats(struct seq_file *m,
5807 struct binder_proc *proc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005808{
5809 struct binder_work *w;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005810 struct binder_thread *thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005811 struct rb_node *n;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005812 int count, strong, weak, ready_threads;
Todd Kjosb4827902017-05-25 15:52:17 -07005813 size_t free_async_space =
5814 binder_alloc_get_free_async_space(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005815
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005816 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005817 seq_printf(m, "context %s\n", proc->context->name);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005818 count = 0;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005819 ready_threads = 0;
Todd Kjosb4827902017-05-25 15:52:17 -07005820 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005821 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
5822 count++;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005823
5824 list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node)
5825 ready_threads++;
5826
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005827 seq_printf(m, " threads: %d\n", count);
5828 seq_printf(m, " requested threads: %d+%d/%d\n"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005829 " ready threads %d\n"
5830 " free async space %zd\n", proc->requested_threads,
5831 proc->requested_threads_started, proc->max_threads,
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005832 ready_threads,
Todd Kjosb4827902017-05-25 15:52:17 -07005833 free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005834 count = 0;
5835 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
5836 count++;
Todd Kjos425d23f2017-06-12 12:07:26 -07005837 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005838 seq_printf(m, " nodes: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005839 count = 0;
5840 strong = 0;
5841 weak = 0;
Todd Kjos5346bf32016-10-20 16:43:34 -07005842 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005843 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
5844 struct binder_ref *ref = rb_entry(n, struct binder_ref,
5845 rb_node_desc);
5846 count++;
Todd Kjosb0117bb2017-05-08 09:16:27 -07005847 strong += ref->data.strong;
5848 weak += ref->data.weak;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005849 }
Todd Kjos5346bf32016-10-20 16:43:34 -07005850 binder_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005851 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005852
Todd Kjosd325d372016-10-10 10:40:53 -07005853 count = binder_alloc_get_allocated_count(&proc->alloc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005854 seq_printf(m, " buffers: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005855
Sherry Yang91004422017-08-22 17:26:57 -07005856 binder_alloc_print_pages(m, &proc->alloc);
5857
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005858 count = 0;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005859 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005860 list_for_each_entry(w, &proc->todo, entry) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005861 if (w->type == BINDER_WORK_TRANSACTION)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005862 count++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005863 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005864 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005865 seq_printf(m, " pending transactions: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005866
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005867 print_binder_stats(m, " ", &proc->stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005868}
5869
5870
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005871static int binder_state_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005872{
5873 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005874 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005875 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005876
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005877 seq_puts(m, "binder state:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005878
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005879 spin_lock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005880 if (!hlist_empty(&binder_dead_nodes))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005881 seq_puts(m, "dead nodes:\n");
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005882 hlist_for_each_entry(node, &binder_dead_nodes, dead_node) {
5883 /*
5884 * take a temporary reference on the node so it
5885 * survives and isn't removed from the list
5886 * while we print it.
5887 */
5888 node->tmp_refs++;
5889 spin_unlock(&binder_dead_nodes_lock);
5890 if (last_node)
5891 binder_put_node(last_node);
5892 binder_node_lock(node);
Todd Kjos425d23f2017-06-12 12:07:26 -07005893 print_binder_node_nilocked(m, node);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005894 binder_node_unlock(node);
5895 last_node = node;
5896 spin_lock(&binder_dead_nodes_lock);
5897 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005898 spin_unlock(&binder_dead_nodes_lock);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005899 if (last_node)
5900 binder_put_node(last_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005901
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005902 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005903 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005904 print_binder_proc(m, proc, 1);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005905 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005906
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005907 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005908}
5909
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005910static int binder_stats_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005911{
5912 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005913
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005914 seq_puts(m, "binder stats:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005915
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005916 print_binder_stats(m, "", &binder_stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005917
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005918 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005919 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005920 print_binder_proc_stats(m, proc);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005921 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005922
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005923 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005924}
5925
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005926static int binder_transactions_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005927{
5928 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005929
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005930 seq_puts(m, "binder transactions:\n");
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005931 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005932 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005933 print_binder_proc(m, proc, 0);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005934 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005935
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005936 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005937}
5938
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005939static int binder_proc_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005940{
Riley Andrews83050a42016-02-09 21:05:33 -08005941 struct binder_proc *itr;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005942 int pid = (unsigned long)m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005943
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005944 mutex_lock(&binder_procs_lock);
Riley Andrews83050a42016-02-09 21:05:33 -08005945 hlist_for_each_entry(itr, &binder_procs, proc_node) {
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005946 if (itr->pid == pid) {
5947 seq_puts(m, "binder proc state:\n");
5948 print_binder_proc(m, itr, 1);
Riley Andrews83050a42016-02-09 21:05:33 -08005949 }
5950 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005951 mutex_unlock(&binder_procs_lock);
5952
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005953 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005954}
5955
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005956static void print_binder_transaction_log_entry(struct seq_file *m,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005957 struct binder_transaction_log_entry *e)
5958{
Todd Kjos1cfe6272017-05-24 13:33:28 -07005959 int debug_id = READ_ONCE(e->debug_id_done);
5960 /*
5961 * read barrier to guarantee debug_id_done read before
5962 * we print the log values
5963 */
5964 smp_rmb();
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005965 seq_printf(m,
Todd Kjos1cfe6272017-05-24 13:33:28 -07005966 "%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 -07005967 e->debug_id, (e->call_type == 2) ? "reply" :
5968 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005969 e->from_thread, e->to_proc, e->to_thread, e->context_name,
Todd Kjose598d172017-03-22 17:19:52 -07005970 e->to_node, e->target_handle, e->data_size, e->offsets_size,
5971 e->return_error, e->return_error_param,
5972 e->return_error_line);
Todd Kjos1cfe6272017-05-24 13:33:28 -07005973 /*
5974 * read-barrier to guarantee read of debug_id_done after
5975 * done printing the fields of the entry
5976 */
5977 smp_rmb();
5978 seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ?
5979 "\n" : " (incomplete)\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005980}
5981
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005982static int binder_transaction_log_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005983{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005984 struct binder_transaction_log *log = m->private;
Todd Kjos1cfe6272017-05-24 13:33:28 -07005985 unsigned int log_cur = atomic_read(&log->cur);
5986 unsigned int count;
5987 unsigned int cur;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005988 int i;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005989
Todd Kjos1cfe6272017-05-24 13:33:28 -07005990 count = log_cur + 1;
5991 cur = count < ARRAY_SIZE(log->entry) && !log->full ?
5992 0 : count % ARRAY_SIZE(log->entry);
5993 if (count > ARRAY_SIZE(log->entry) || log->full)
5994 count = ARRAY_SIZE(log->entry);
5995 for (i = 0; i < count; i++) {
5996 unsigned int index = cur++ % ARRAY_SIZE(log->entry);
5997
5998 print_binder_transaction_log_entry(m, &log->entry[index]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005999 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006000 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006001}
6002
6003static const struct file_operations binder_fops = {
6004 .owner = THIS_MODULE,
6005 .poll = binder_poll,
6006 .unlocked_ioctl = binder_ioctl,
Arve Hjønnevågda498892014-02-21 14:40:26 -08006007 .compat_ioctl = binder_ioctl,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006008 .mmap = binder_mmap,
6009 .open = binder_open,
6010 .flush = binder_flush,
6011 .release = binder_release,
6012};
6013
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006014BINDER_DEBUG_ENTRY(state);
6015BINDER_DEBUG_ENTRY(stats);
6016BINDER_DEBUG_ENTRY(transactions);
6017BINDER_DEBUG_ENTRY(transaction_log);
6018
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006019static int __init init_binder_device(const char *name)
6020{
6021 int ret;
6022 struct binder_device *binder_device;
6023
6024 binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
6025 if (!binder_device)
6026 return -ENOMEM;
6027
6028 binder_device->miscdev.fops = &binder_fops;
6029 binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
6030 binder_device->miscdev.name = name;
6031
6032 binder_device->context.binder_context_mgr_uid = INVALID_UID;
6033 binder_device->context.name = name;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07006034 mutex_init(&binder_device->context.context_mgr_node_lock);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006035
6036 ret = misc_register(&binder_device->miscdev);
6037 if (ret < 0) {
6038 kfree(binder_device);
6039 return ret;
6040 }
6041
6042 hlist_add_head(&binder_device->hlist, &binder_devices);
6043
6044 return ret;
6045}
6046
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006047static int __init binder_init(void)
6048{
6049 int ret;
Christian Brauner558ee932017-08-21 16:13:28 +02006050 char *device_name, *device_names, *device_tmp;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006051 struct binder_device *device;
6052 struct hlist_node *tmp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006053
Tetsuo Handaf8cb8222017-11-29 22:29:47 +09006054 ret = binder_alloc_shrinker_init();
6055 if (ret)
6056 return ret;
Sherry Yang5828d702017-07-29 13:24:11 -07006057
Todd Kjos1cfe6272017-05-24 13:33:28 -07006058 atomic_set(&binder_transaction_log.cur, ~0U);
6059 atomic_set(&binder_transaction_log_failed.cur, ~0U);
6060
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006061 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
6062 if (binder_debugfs_dir_entry_root)
6063 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
6064 binder_debugfs_dir_entry_root);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006065
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006066 if (binder_debugfs_dir_entry_root) {
6067 debugfs_create_file("state",
Harsh Shandilya174562a2017-12-22 19:37:02 +05306068 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006069 binder_debugfs_dir_entry_root,
6070 NULL,
6071 &binder_state_fops);
6072 debugfs_create_file("stats",
Harsh Shandilya174562a2017-12-22 19:37:02 +05306073 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006074 binder_debugfs_dir_entry_root,
6075 NULL,
6076 &binder_stats_fops);
6077 debugfs_create_file("transactions",
Harsh Shandilya174562a2017-12-22 19:37:02 +05306078 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006079 binder_debugfs_dir_entry_root,
6080 NULL,
6081 &binder_transactions_fops);
6082 debugfs_create_file("transaction_log",
Harsh Shandilya174562a2017-12-22 19:37:02 +05306083 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006084 binder_debugfs_dir_entry_root,
6085 &binder_transaction_log,
6086 &binder_transaction_log_fops);
6087 debugfs_create_file("failed_transaction_log",
Harsh Shandilya174562a2017-12-22 19:37:02 +05306088 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006089 binder_debugfs_dir_entry_root,
6090 &binder_transaction_log_failed,
6091 &binder_transaction_log_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006092 }
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006093
6094 /*
6095 * Copy the module_parameter string, because we don't want to
6096 * tokenize it in-place.
6097 */
6098 device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL);
6099 if (!device_names) {
6100 ret = -ENOMEM;
6101 goto err_alloc_device_names_failed;
6102 }
6103 strcpy(device_names, binder_devices_param);
6104
Christian Brauner558ee932017-08-21 16:13:28 +02006105 device_tmp = device_names;
6106 while ((device_name = strsep(&device_tmp, ","))) {
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006107 ret = init_binder_device(device_name);
6108 if (ret)
6109 goto err_init_binder_device_failed;
6110 }
6111
6112 return ret;
6113
6114err_init_binder_device_failed:
6115 hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
6116 misc_deregister(&device->miscdev);
6117 hlist_del(&device->hlist);
6118 kfree(device);
6119 }
Christian Brauner558ee932017-08-21 16:13:28 +02006120
6121 kfree(device_names);
6122
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006123err_alloc_device_names_failed:
6124 debugfs_remove_recursive(binder_debugfs_dir_entry_root);
6125
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006126 return ret;
6127}
6128
6129device_initcall(binder_init);
6130
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07006131#define CREATE_TRACE_POINTS
6132#include "binder_trace.h"
6133
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006134MODULE_LICENSE("GPL v2");