blob: fa3ab3caca745563250c6af20c9e1c71c67a4095 [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,
Todd Kjos8539b1e2019-02-08 10:35:20 -08002406 binder_size_t failed_at,
2407 bool is_failure)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002408{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002409 int debug_id = buffer->debug_id;
Todd Kjos8539b1e2019-02-08 10:35:20 -08002410 binder_size_t off_start_offset, buffer_offset, off_end_offset;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002411
2412 binder_debug(BINDER_DEBUG_TRANSACTION,
Todd Kjos8539b1e2019-02-08 10:35:20 -08002413 "%d buffer release %d, size %zd-%zd, failed at %llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002414 proc->pid, buffer->debug_id,
Todd Kjos8539b1e2019-02-08 10:35:20 -08002415 buffer->data_size, buffer->offsets_size,
2416 (unsigned long long)failed_at);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002417
2418 if (buffer->target_node)
2419 binder_dec_node(buffer->target_node, 1, 0);
2420
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002421 off_start_offset = ALIGN(buffer->data_size, sizeof(void *));
Todd Kjos8539b1e2019-02-08 10:35:20 -08002422 off_end_offset = is_failure ? failed_at :
2423 off_start_offset + buffer->offsets_size;
2424 for (buffer_offset = off_start_offset; buffer_offset < off_end_offset;
2425 buffer_offset += sizeof(binder_size_t)) {
Martijn Coenen00c80372016-07-13 12:06:49 +02002426 struct binder_object_header *hdr;
Todd Kjos90a570c2019-02-08 10:35:15 -08002427 size_t object_size;
Todd Kjosd73356a2019-02-08 10:35:16 -08002428 struct binder_object object;
Todd Kjos90a570c2019-02-08 10:35:15 -08002429 binder_size_t object_offset;
Seunghun Lee10f62862014-05-01 01:30:23 +09002430
Todd Kjos90a570c2019-02-08 10:35:15 -08002431 binder_alloc_copy_from_buffer(&proc->alloc, &object_offset,
2432 buffer, buffer_offset,
2433 sizeof(object_offset));
Todd Kjosd73356a2019-02-08 10:35:16 -08002434 object_size = binder_get_object(proc, buffer,
2435 object_offset, &object);
Martijn Coenen00c80372016-07-13 12:06:49 +02002436 if (object_size == 0) {
2437 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
Todd Kjos90a570c2019-02-08 10:35:15 -08002438 debug_id, (u64)object_offset, buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002439 continue;
2440 }
Todd Kjosd73356a2019-02-08 10:35:16 -08002441 hdr = &object.hdr;
Martijn Coenen00c80372016-07-13 12:06:49 +02002442 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002443 case BINDER_TYPE_BINDER:
2444 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenen00c80372016-07-13 12:06:49 +02002445 struct flat_binder_object *fp;
2446 struct binder_node *node;
Seunghun Lee10f62862014-05-01 01:30:23 +09002447
Martijn Coenen00c80372016-07-13 12:06:49 +02002448 fp = to_flat_binder_object(hdr);
2449 node = binder_get_node(proc, fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002450 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002451 pr_err("transaction release %d bad node %016llx\n",
2452 debug_id, (u64)fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002453 break;
2454 }
2455 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002456 " node %d u%016llx\n",
2457 node->debug_id, (u64)node->ptr);
Martijn Coenen00c80372016-07-13 12:06:49 +02002458 binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER,
2459 0);
Todd Kjosf22abc72017-05-09 11:08:05 -07002460 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002461 } break;
2462 case BINDER_TYPE_HANDLE:
2463 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenen00c80372016-07-13 12:06:49 +02002464 struct flat_binder_object *fp;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002465 struct binder_ref_data rdata;
2466 int ret;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02002467
Martijn Coenen00c80372016-07-13 12:06:49 +02002468 fp = to_flat_binder_object(hdr);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002469 ret = binder_dec_ref_for_handle(proc, fp->handle,
2470 hdr->type == BINDER_TYPE_HANDLE, &rdata);
2471
2472 if (ret) {
2473 pr_err("transaction release %d bad handle %d, ret = %d\n",
2474 debug_id, fp->handle, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002475 break;
2476 }
2477 binder_debug(BINDER_DEBUG_TRANSACTION,
Todd Kjosb0117bb2017-05-08 09:16:27 -07002478 " ref %d desc %d\n",
2479 rdata.debug_id, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002480 } break;
2481
Martijn Coenen00c80372016-07-13 12:06:49 +02002482 case BINDER_TYPE_FD: {
2483 struct binder_fd_object *fp = to_binder_fd_object(hdr);
2484
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002485 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen00c80372016-07-13 12:06:49 +02002486 " fd %d\n", fp->fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002487 if (failed_at)
Martijn Coenen00c80372016-07-13 12:06:49 +02002488 task_close_fd(proc, fp->fd);
2489 } break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002490 case BINDER_TYPE_PTR:
2491 /*
2492 * Nothing to do here, this will get cleaned up when the
2493 * transaction buffer gets freed
2494 */
2495 break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002496 case BINDER_TYPE_FDA: {
2497 struct binder_fd_array_object *fda;
2498 struct binder_buffer_object *parent;
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002499 struct binder_object ptr_object;
Todd Kjos8539b1e2019-02-08 10:35:20 -08002500 binder_size_t fda_offset;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002501 size_t fd_index;
2502 binder_size_t fd_buf_size;
Todd Kjos8539b1e2019-02-08 10:35:20 -08002503 binder_size_t num_valid;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002504
Todd Kjos8539b1e2019-02-08 10:35:20 -08002505 num_valid = (buffer_offset - off_start_offset) /
2506 sizeof(binder_size_t);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002507 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,
Todd Kjos8539b1e2019-02-08 10:35:20 -08002512 num_valid);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002513 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 Kjos8539b1e2019-02-08 10:35:20 -08002531 /*
2532 * the source data for binder_buffer_object is visible
2533 * to user-space and the @buffer element is the user
2534 * pointer to the buffer_object containing the fd_array.
2535 * Convert the address to an offset relative to
2536 * the base of the transaction buffer.
2537 */
2538 fda_offset =
2539 (parent->buffer - (uintptr_t)buffer->user_data) +
2540 fda->parent_offset;
Todd Kjos90a570c2019-02-08 10:35:15 -08002541 for (fd_index = 0; fd_index < fda->num_fds;
2542 fd_index++) {
2543 u32 fd;
Todd Kjos8539b1e2019-02-08 10:35:20 -08002544 binder_size_t offset = fda_offset +
2545 fd_index * sizeof(fd);
Todd Kjos90a570c2019-02-08 10:35:15 -08002546
2547 binder_alloc_copy_from_buffer(&proc->alloc,
2548 &fd,
2549 buffer,
2550 offset,
2551 sizeof(fd));
2552 task_close_fd(proc, fd);
2553 }
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002554 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002555 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01002556 pr_err("transaction release %d bad object type %x\n",
Martijn Coenen00c80372016-07-13 12:06:49 +02002557 debug_id, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002558 break;
2559 }
2560 }
2561}
2562
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002563static int binder_translate_binder(struct flat_binder_object *fp,
2564 struct binder_transaction *t,
2565 struct binder_thread *thread)
2566{
2567 struct binder_node *node;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002568 struct binder_proc *proc = thread->proc;
2569 struct binder_proc *target_proc = t->to_proc;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002570 struct binder_ref_data rdata;
Todd Kjosf22abc72017-05-09 11:08:05 -07002571 int ret = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002572
2573 node = binder_get_node(proc, fp->binder);
2574 if (!node) {
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002575 node = binder_new_node(proc, fp);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002576 if (!node)
2577 return -ENOMEM;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002578 }
2579 if (fp->cookie != node->cookie) {
2580 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
2581 proc->pid, thread->pid, (u64)fp->binder,
2582 node->debug_id, (u64)fp->cookie,
2583 (u64)node->cookie);
Todd Kjosf22abc72017-05-09 11:08:05 -07002584 ret = -EINVAL;
2585 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002586 }
Todd Kjosf22abc72017-05-09 11:08:05 -07002587 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2588 ret = -EPERM;
2589 goto done;
2590 }
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002591
Todd Kjosb0117bb2017-05-08 09:16:27 -07002592 ret = binder_inc_ref_for_node(target_proc, node,
2593 fp->hdr.type == BINDER_TYPE_BINDER,
2594 &thread->todo, &rdata);
2595 if (ret)
Todd Kjosf22abc72017-05-09 11:08:05 -07002596 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002597
2598 if (fp->hdr.type == BINDER_TYPE_BINDER)
2599 fp->hdr.type = BINDER_TYPE_HANDLE;
2600 else
2601 fp->hdr.type = BINDER_TYPE_WEAK_HANDLE;
2602 fp->binder = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002603 fp->handle = rdata.desc;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002604 fp->cookie = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002605
Todd Kjosb0117bb2017-05-08 09:16:27 -07002606 trace_binder_transaction_node_to_ref(t, node, &rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002607 binder_debug(BINDER_DEBUG_TRANSACTION,
2608 " node %d u%016llx -> ref %d desc %d\n",
2609 node->debug_id, (u64)node->ptr,
Todd Kjosb0117bb2017-05-08 09:16:27 -07002610 rdata.debug_id, rdata.desc);
Todd Kjosf22abc72017-05-09 11:08:05 -07002611done:
2612 binder_put_node(node);
2613 return ret;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002614}
2615
2616static int binder_translate_handle(struct flat_binder_object *fp,
2617 struct binder_transaction *t,
2618 struct binder_thread *thread)
2619{
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002620 struct binder_proc *proc = thread->proc;
2621 struct binder_proc *target_proc = t->to_proc;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002622 struct binder_node *node;
2623 struct binder_ref_data src_rdata;
Todd Kjosf22abc72017-05-09 11:08:05 -07002624 int ret = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002625
Todd Kjosb0117bb2017-05-08 09:16:27 -07002626 node = binder_get_node_from_ref(proc, fp->handle,
2627 fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata);
2628 if (!node) {
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002629 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
2630 proc->pid, thread->pid, fp->handle);
2631 return -EINVAL;
2632 }
Todd Kjosf22abc72017-05-09 11:08:05 -07002633 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2634 ret = -EPERM;
2635 goto done;
2636 }
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002637
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002638 binder_node_lock(node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002639 if (node->proc == target_proc) {
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002640 if (fp->hdr.type == BINDER_TYPE_HANDLE)
2641 fp->hdr.type = BINDER_TYPE_BINDER;
2642 else
2643 fp->hdr.type = BINDER_TYPE_WEAK_BINDER;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002644 fp->binder = node->ptr;
2645 fp->cookie = node->cookie;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002646 if (node->proc)
2647 binder_inner_proc_lock(node->proc);
2648 binder_inc_node_nilocked(node,
2649 fp->hdr.type == BINDER_TYPE_BINDER,
2650 0, NULL);
2651 if (node->proc)
2652 binder_inner_proc_unlock(node->proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002653 trace_binder_transaction_ref_to_node(t, node, &src_rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002654 binder_debug(BINDER_DEBUG_TRANSACTION,
2655 " ref %d desc %d -> node %d u%016llx\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07002656 src_rdata.debug_id, src_rdata.desc, node->debug_id,
2657 (u64)node->ptr);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002658 binder_node_unlock(node);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002659 } else {
Todd Kjosb0117bb2017-05-08 09:16:27 -07002660 struct binder_ref_data dest_rdata;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002661
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002662 binder_node_unlock(node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002663 ret = binder_inc_ref_for_node(target_proc, node,
2664 fp->hdr.type == BINDER_TYPE_HANDLE,
2665 NULL, &dest_rdata);
2666 if (ret)
Todd Kjosf22abc72017-05-09 11:08:05 -07002667 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002668
2669 fp->binder = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002670 fp->handle = dest_rdata.desc;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002671 fp->cookie = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002672 trace_binder_transaction_ref_to_ref(t, node, &src_rdata,
2673 &dest_rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002674 binder_debug(BINDER_DEBUG_TRANSACTION,
2675 " ref %d desc %d -> ref %d desc %d (node %d)\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07002676 src_rdata.debug_id, src_rdata.desc,
2677 dest_rdata.debug_id, dest_rdata.desc,
2678 node->debug_id);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002679 }
Todd Kjosf22abc72017-05-09 11:08:05 -07002680done:
2681 binder_put_node(node);
2682 return ret;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002683}
2684
2685static int binder_translate_fd(int fd,
2686 struct binder_transaction *t,
2687 struct binder_thread *thread,
2688 struct binder_transaction *in_reply_to)
2689{
2690 struct binder_proc *proc = thread->proc;
2691 struct binder_proc *target_proc = t->to_proc;
2692 int target_fd;
2693 struct file *file;
2694 int ret;
2695 bool target_allows_fd;
2696
2697 if (in_reply_to)
2698 target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS);
2699 else
2700 target_allows_fd = t->buffer->target_node->accept_fds;
2701 if (!target_allows_fd) {
2702 binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
2703 proc->pid, thread->pid,
2704 in_reply_to ? "reply" : "transaction",
2705 fd);
2706 ret = -EPERM;
2707 goto err_fd_not_accepted;
2708 }
2709
2710 file = fget(fd);
2711 if (!file) {
2712 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
2713 proc->pid, thread->pid, fd);
2714 ret = -EBADF;
2715 goto err_fget;
2716 }
2717 ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file);
2718 if (ret < 0) {
2719 ret = -EPERM;
2720 goto err_security;
2721 }
2722
2723 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
2724 if (target_fd < 0) {
2725 ret = -ENOMEM;
2726 goto err_get_unused_fd;
2727 }
2728 task_fd_install(target_proc, target_fd, file);
2729 trace_binder_transaction_fd(t, fd, target_fd);
2730 binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n",
2731 fd, target_fd);
2732
2733 return target_fd;
2734
2735err_get_unused_fd:
2736err_security:
2737 fput(file);
2738err_fget:
2739err_fd_not_accepted:
2740 return ret;
2741}
2742
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002743static int binder_translate_fd_array(struct binder_fd_array_object *fda,
2744 struct binder_buffer_object *parent,
2745 struct binder_transaction *t,
2746 struct binder_thread *thread,
2747 struct binder_transaction *in_reply_to)
2748{
2749 binder_size_t fdi, fd_buf_size, num_installed_fds;
Todd Kjos8539b1e2019-02-08 10:35:20 -08002750 binder_size_t fda_offset;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002751 int target_fd;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002752 struct binder_proc *proc = thread->proc;
2753 struct binder_proc *target_proc = t->to_proc;
2754
2755 fd_buf_size = sizeof(u32) * fda->num_fds;
2756 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2757 binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
2758 proc->pid, thread->pid, (u64)fda->num_fds);
2759 return -EINVAL;
2760 }
2761 if (fd_buf_size > parent->length ||
2762 fda->parent_offset > parent->length - fd_buf_size) {
2763 /* No space for all file descriptors here. */
2764 binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
2765 proc->pid, thread->pid, (u64)fda->num_fds);
2766 return -EINVAL;
2767 }
Todd Kjos8539b1e2019-02-08 10:35:20 -08002768 /*
2769 * the source data for binder_buffer_object is visible
2770 * to user-space and the @buffer element is the user
2771 * pointer to the buffer_object containing the fd_array.
2772 * Convert the address to an offset relative to
2773 * the base of the transaction buffer.
2774 */
2775 fda_offset = (parent->buffer - (uintptr_t)t->buffer->user_data) +
2776 fda->parent_offset;
2777 if (!IS_ALIGNED((unsigned long)fda_offset, sizeof(u32))) {
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002778 binder_user_error("%d:%d parent offset not aligned correctly.\n",
2779 proc->pid, thread->pid);
2780 return -EINVAL;
2781 }
2782 for (fdi = 0; fdi < fda->num_fds; fdi++) {
Todd Kjos90a570c2019-02-08 10:35:15 -08002783 u32 fd;
2784 int target_fd;
Todd Kjos8539b1e2019-02-08 10:35:20 -08002785 binder_size_t offset = fda_offset + fdi * sizeof(fd);
Todd Kjos90a570c2019-02-08 10:35:15 -08002786
2787 binder_alloc_copy_from_buffer(&target_proc->alloc,
2788 &fd, t->buffer,
2789 offset, sizeof(fd));
2790 target_fd = binder_translate_fd(fd, t, thread, in_reply_to);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002791 if (target_fd < 0)
2792 goto err_translate_fd_failed;
Todd Kjos90a570c2019-02-08 10:35:15 -08002793 binder_alloc_copy_to_buffer(&target_proc->alloc,
2794 t->buffer, offset,
2795 &target_fd, sizeof(fd));
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002796 }
2797 return 0;
2798
2799err_translate_fd_failed:
2800 /*
2801 * Failed to allocate fd or security error, free fds
2802 * installed so far.
2803 */
2804 num_installed_fds = fdi;
Todd Kjos90a570c2019-02-08 10:35:15 -08002805 for (fdi = 0; fdi < num_installed_fds; fdi++) {
2806 u32 fd;
Todd Kjos8539b1e2019-02-08 10:35:20 -08002807 binder_size_t offset = fda_offset + fdi * sizeof(fd);
Todd Kjos90a570c2019-02-08 10:35:15 -08002808 binder_alloc_copy_from_buffer(&target_proc->alloc,
2809 &fd, t->buffer,
2810 offset, sizeof(fd));
2811 task_close_fd(target_proc, fd);
2812 }
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002813 return target_fd;
2814}
2815
Martijn Coenen5a6da532016-09-30 14:10:07 +02002816static int binder_fixup_parent(struct binder_transaction *t,
2817 struct binder_thread *thread,
2818 struct binder_buffer_object *bp,
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002819 binder_size_t off_start_offset,
Martijn Coenen5a6da532016-09-30 14:10:07 +02002820 binder_size_t num_valid,
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002821 binder_size_t last_fixup_obj_off,
Martijn Coenen5a6da532016-09-30 14:10:07 +02002822 binder_size_t last_fixup_min_off)
2823{
2824 struct binder_buffer_object *parent;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002825 struct binder_buffer *b = t->buffer;
2826 struct binder_proc *proc = thread->proc;
2827 struct binder_proc *target_proc = t->to_proc;
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002828 struct binder_object object;
2829 binder_size_t buffer_offset;
2830 binder_size_t parent_offset;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002831
2832 if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT))
2833 return 0;
2834
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002835 parent = binder_validate_ptr(target_proc, b, &object, bp->parent,
2836 off_start_offset, &parent_offset,
2837 num_valid);
Martijn Coenen5a6da532016-09-30 14:10:07 +02002838 if (!parent) {
2839 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
2840 proc->pid, thread->pid);
2841 return -EINVAL;
2842 }
2843
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002844 if (!binder_validate_fixup(target_proc, b, off_start_offset,
2845 parent_offset, bp->parent_offset,
2846 last_fixup_obj_off,
Martijn Coenen5a6da532016-09-30 14:10:07 +02002847 last_fixup_min_off)) {
2848 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
2849 proc->pid, thread->pid);
2850 return -EINVAL;
2851 }
2852
2853 if (parent->length < sizeof(binder_uintptr_t) ||
2854 bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) {
2855 /* No space for a pointer here! */
2856 binder_user_error("%d:%d got transaction with invalid parent offset\n",
2857 proc->pid, thread->pid);
2858 return -EINVAL;
2859 }
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002860 buffer_offset = bp->parent_offset +
Todd Kjos8539b1e2019-02-08 10:35:20 -08002861 (uintptr_t)parent->buffer - (uintptr_t)b->user_data;
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08002862 binder_alloc_copy_to_buffer(&target_proc->alloc, b, buffer_offset,
2863 &bp->buffer, sizeof(bp->buffer));
Martijn Coenen5a6da532016-09-30 14:10:07 +02002864
2865 return 0;
2866}
2867
Martijn Coenen053be422017-06-06 15:17:46 -07002868/**
2869 * binder_proc_transaction() - sends a transaction to a process and wakes it up
2870 * @t: transaction to send
2871 * @proc: process to send the transaction to
2872 * @thread: thread in @proc to send the transaction to (may be NULL)
2873 *
2874 * This function queues a transaction to the specified process. It will try
2875 * to find a thread in the target process to handle the transaction and
2876 * wake it up. If no thread is found, the work is queued to the proc
2877 * waitqueue.
2878 *
2879 * If the @thread parameter is not NULL, the transaction is always queued
2880 * to the waitlist of that specific thread.
2881 *
2882 * Return: true if the transactions was successfully queued
2883 * false if the target process or thread is dead
2884 */
2885static bool binder_proc_transaction(struct binder_transaction *t,
2886 struct binder_proc *proc,
2887 struct binder_thread *thread)
2888{
Martijn Coenen053be422017-06-06 15:17:46 -07002889 struct binder_node *node = t->buffer->target_node;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002890 struct binder_priority node_prio;
Martijn Coenen053be422017-06-06 15:17:46 -07002891 bool oneway = !!(t->flags & TF_ONE_WAY);
Martijn Coenen1af61802017-10-19 15:04:46 +02002892 bool pending_async = false;
Martijn Coenen053be422017-06-06 15:17:46 -07002893
2894 BUG_ON(!node);
2895 binder_node_lock(node);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002896 node_prio.prio = node->min_priority;
2897 node_prio.sched_policy = node->sched_policy;
2898
Martijn Coenen053be422017-06-06 15:17:46 -07002899 if (oneway) {
2900 BUG_ON(thread);
2901 if (node->has_async_transaction) {
Martijn Coenen1af61802017-10-19 15:04:46 +02002902 pending_async = true;
Martijn Coenen053be422017-06-06 15:17:46 -07002903 } else {
Gustavo A. R. Silvae62dd6f2018-01-23 12:04:27 -06002904 node->has_async_transaction = true;
Martijn Coenen053be422017-06-06 15:17:46 -07002905 }
2906 }
2907
2908 binder_inner_proc_lock(proc);
2909
2910 if (proc->is_dead || (thread && thread->is_dead)) {
2911 binder_inner_proc_unlock(proc);
2912 binder_node_unlock(node);
2913 return false;
2914 }
2915
Martijn Coenen1af61802017-10-19 15:04:46 +02002916 if (!thread && !pending_async)
Martijn Coenen053be422017-06-06 15:17:46 -07002917 thread = binder_select_thread_ilocked(proc);
2918
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002919 if (thread) {
Martijn Coenenc46810c2017-06-23 10:13:43 -07002920 binder_transaction_priority(thread->task, t, node_prio,
2921 node->inherit_rt);
Martijn Coenen1af61802017-10-19 15:04:46 +02002922 binder_enqueue_thread_work_ilocked(thread, &t->work);
2923 } else if (!pending_async) {
2924 binder_enqueue_work_ilocked(&t->work, &proc->todo);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002925 } else {
Martijn Coenen1af61802017-10-19 15:04:46 +02002926 binder_enqueue_work_ilocked(&t->work, &node->async_todo);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002927 }
Martijn Coenen053be422017-06-06 15:17:46 -07002928
Martijn Coenen1af61802017-10-19 15:04:46 +02002929 if (!pending_async)
Martijn Coenen053be422017-06-06 15:17:46 -07002930 binder_wakeup_thread_ilocked(proc, thread, !oneway /* sync */);
2931
2932 binder_inner_proc_unlock(proc);
2933 binder_node_unlock(node);
2934
2935 return true;
2936}
2937
Todd Kjos291d9682017-09-25 08:55:09 -07002938/**
2939 * binder_get_node_refs_for_txn() - Get required refs on node for txn
2940 * @node: struct binder_node for which to get refs
2941 * @proc: returns @node->proc if valid
2942 * @error: if no @proc then returns BR_DEAD_REPLY
2943 *
2944 * User-space normally keeps the node alive when creating a transaction
2945 * since it has a reference to the target. The local strong ref keeps it
2946 * alive if the sending process dies before the target process processes
2947 * the transaction. If the source process is malicious or has a reference
2948 * counting bug, relying on the local strong ref can fail.
2949 *
2950 * Since user-space can cause the local strong ref to go away, we also take
2951 * a tmpref on the node to ensure it survives while we are constructing
2952 * the transaction. We also need a tmpref on the proc while we are
2953 * constructing the transaction, so we take that here as well.
2954 *
2955 * Return: The target_node with refs taken or NULL if no @node->proc is NULL.
2956 * Also sets @proc if valid. If the @node->proc is NULL indicating that the
2957 * target proc has died, @error is set to BR_DEAD_REPLY
2958 */
2959static struct binder_node *binder_get_node_refs_for_txn(
2960 struct binder_node *node,
2961 struct binder_proc **procp,
2962 uint32_t *error)
2963{
2964 struct binder_node *target_node = NULL;
2965
2966 binder_node_inner_lock(node);
2967 if (node->proc) {
2968 target_node = node;
2969 binder_inc_node_nilocked(node, 1, 0, NULL);
2970 binder_inc_node_tmpref_ilocked(node);
2971 node->proc->tmp_ref++;
2972 *procp = node->proc;
2973 } else
2974 *error = BR_DEAD_REPLY;
2975 binder_node_inner_unlock(node);
2976
2977 return target_node;
2978}
2979
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002980static void binder_transaction(struct binder_proc *proc,
2981 struct binder_thread *thread,
Martijn Coenen59878d72016-09-30 14:05:40 +02002982 struct binder_transaction_data *tr, int reply,
2983 binder_size_t extra_buffers_size)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002984{
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002985 int ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002986 struct binder_transaction *t;
2987 struct binder_work *tcomplete;
Todd Kjos8539b1e2019-02-08 10:35:20 -08002988 binder_size_t buffer_offset = 0;
2989 binder_size_t off_start_offset, off_end_offset;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08002990 binder_size_t off_min;
Todd Kjos8539b1e2019-02-08 10:35:20 -08002991 binder_size_t sg_buf_offset, sg_buf_end_offset;
Todd Kjos2f993e22017-05-12 14:42:55 -07002992 struct binder_proc *target_proc = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002993 struct binder_thread *target_thread = NULL;
2994 struct binder_node *target_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002995 struct binder_transaction *in_reply_to = NULL;
2996 struct binder_transaction_log_entry *e;
Todd Kjose598d172017-03-22 17:19:52 -07002997 uint32_t return_error = 0;
2998 uint32_t return_error_param = 0;
2999 uint32_t return_error_line = 0;
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003000 binder_size_t last_fixup_obj_off = 0;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003001 binder_size_t last_fixup_min_off = 0;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003002 struct binder_context *context = proc->context;
Todd Kjos1cfe6272017-05-24 13:33:28 -07003003 int t_debug_id = atomic_inc_return(&binder_last_id);
Todd Kjos63e0afa2019-01-14 09:10:21 -08003004 char *secctx = NULL;
3005 u32 secctx_sz = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003006
3007 e = binder_transaction_log_add(&binder_transaction_log);
Todd Kjos1cfe6272017-05-24 13:33:28 -07003008 e->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003009 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
3010 e->from_proc = proc->pid;
3011 e->from_thread = thread->pid;
3012 e->target_handle = tr->target.handle;
3013 e->data_size = tr->data_size;
3014 e->offsets_size = tr->offsets_size;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02003015 e->context_name = proc->context->name;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003016
3017 if (reply) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07003018 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003019 in_reply_to = thread->transaction_stack;
3020 if (in_reply_to == NULL) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07003021 binder_inner_proc_unlock(proc);
Anmol Sarma56b468f2012-10-30 22:35:43 +05303022 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003023 proc->pid, thread->pid);
3024 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003025 return_error_param = -EPROTO;
3026 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003027 goto err_empty_call_stack;
3028 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003029 if (in_reply_to->to_thread != thread) {
Todd Kjos2f993e22017-05-12 14:42:55 -07003030 spin_lock(&in_reply_to->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05303031 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 +09003032 proc->pid, thread->pid, in_reply_to->debug_id,
3033 in_reply_to->to_proc ?
3034 in_reply_to->to_proc->pid : 0,
3035 in_reply_to->to_thread ?
3036 in_reply_to->to_thread->pid : 0);
Todd Kjos2f993e22017-05-12 14:42:55 -07003037 spin_unlock(&in_reply_to->lock);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003038 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003039 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003040 return_error_param = -EPROTO;
3041 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003042 in_reply_to = NULL;
3043 goto err_bad_call_stack;
3044 }
3045 thread->transaction_stack = in_reply_to->to_parent;
Martijn Coenen995a36e2017-06-02 13:36:52 -07003046 binder_inner_proc_unlock(proc);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003047 target_thread = binder_get_txn_from_and_acq_inner(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003048 if (target_thread == NULL) {
3049 return_error = BR_DEAD_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003050 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003051 goto err_dead_binder;
3052 }
3053 if (target_thread->transaction_stack != in_reply_to) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303054 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 +09003055 proc->pid, thread->pid,
3056 target_thread->transaction_stack ?
3057 target_thread->transaction_stack->debug_id : 0,
3058 in_reply_to->debug_id);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003059 binder_inner_proc_unlock(target_thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003060 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003061 return_error_param = -EPROTO;
3062 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003063 in_reply_to = NULL;
3064 target_thread = NULL;
3065 goto err_dead_binder;
3066 }
3067 target_proc = target_thread->proc;
Todd Kjos2f993e22017-05-12 14:42:55 -07003068 target_proc->tmp_ref++;
Martijn Coenen995a36e2017-06-02 13:36:52 -07003069 binder_inner_proc_unlock(target_thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003070 } else {
3071 if (tr->target.handle) {
3072 struct binder_ref *ref;
Seunghun Lee10f62862014-05-01 01:30:23 +09003073
Todd Kjosc37162d2017-05-26 11:56:29 -07003074 /*
3075 * There must already be a strong ref
3076 * on this node. If so, do a strong
3077 * increment on the node to ensure it
3078 * stays alive until the transaction is
3079 * done.
3080 */
Todd Kjos5346bf32016-10-20 16:43:34 -07003081 binder_proc_lock(proc);
3082 ref = binder_get_ref_olocked(proc, tr->target.handle,
3083 true);
Todd Kjosc37162d2017-05-26 11:56:29 -07003084 if (ref) {
Todd Kjos291d9682017-09-25 08:55:09 -07003085 target_node = binder_get_node_refs_for_txn(
3086 ref->node, &target_proc,
3087 &return_error);
3088 } else {
3089 binder_user_error("%d:%d got transaction to invalid handle\n",
3090 proc->pid, thread->pid);
3091 return_error = BR_FAILED_REPLY;
Todd Kjosc37162d2017-05-26 11:56:29 -07003092 }
Todd Kjos5346bf32016-10-20 16:43:34 -07003093 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003094 } else {
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003095 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003096 target_node = context->binder_context_mgr_node;
Todd Kjos291d9682017-09-25 08:55:09 -07003097 if (target_node)
3098 target_node = binder_get_node_refs_for_txn(
3099 target_node, &target_proc,
3100 &return_error);
3101 else
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003102 return_error = BR_DEAD_REPLY;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003103 mutex_unlock(&context->context_mgr_node_lock);
Martijn Coenenc4048b22018-03-28 11:14:50 +02003104 if (target_node && target_proc == proc) {
3105 binder_user_error("%d:%d got transaction to context manager from process owning it\n",
3106 proc->pid, thread->pid);
3107 return_error = BR_FAILED_REPLY;
3108 return_error_param = -EINVAL;
3109 return_error_line = __LINE__;
3110 goto err_invalid_target_handle;
3111 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003112 }
Todd Kjos291d9682017-09-25 08:55:09 -07003113 if (!target_node) {
3114 /*
3115 * return_error is set above
3116 */
3117 return_error_param = -EINVAL;
Todd Kjose598d172017-03-22 17:19:52 -07003118 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003119 goto err_dead_binder;
3120 }
Todd Kjos291d9682017-09-25 08:55:09 -07003121 e->to_node = target_node->debug_id;
Stephen Smalley79af7302015-01-21 10:54:10 -05003122 if (security_binder_transaction(proc->tsk,
3123 target_proc->tsk) < 0) {
3124 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003125 return_error_param = -EPERM;
3126 return_error_line = __LINE__;
Stephen Smalley79af7302015-01-21 10:54:10 -05003127 goto err_invalid_target_handle;
3128 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07003129 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003130 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
3131 struct binder_transaction *tmp;
Seunghun Lee10f62862014-05-01 01:30:23 +09003132
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003133 tmp = thread->transaction_stack;
3134 if (tmp->to_thread != thread) {
Todd Kjos2f993e22017-05-12 14:42:55 -07003135 spin_lock(&tmp->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05303136 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 +09003137 proc->pid, thread->pid, tmp->debug_id,
3138 tmp->to_proc ? tmp->to_proc->pid : 0,
3139 tmp->to_thread ?
3140 tmp->to_thread->pid : 0);
Todd Kjos2f993e22017-05-12 14:42:55 -07003141 spin_unlock(&tmp->lock);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003142 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003143 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003144 return_error_param = -EPROTO;
3145 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003146 goto err_bad_call_stack;
3147 }
3148 while (tmp) {
Todd Kjos2f993e22017-05-12 14:42:55 -07003149 struct binder_thread *from;
3150
3151 spin_lock(&tmp->lock);
3152 from = tmp->from;
3153 if (from && from->proc == target_proc) {
3154 atomic_inc(&from->tmp_ref);
3155 target_thread = from;
3156 spin_unlock(&tmp->lock);
3157 break;
3158 }
3159 spin_unlock(&tmp->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003160 tmp = tmp->from_parent;
3161 }
3162 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07003163 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003164 }
Martijn Coenen053be422017-06-06 15:17:46 -07003165 if (target_thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003166 e->to_thread = target_thread->pid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003167 e->to_proc = target_proc->pid;
3168
3169 /* TODO: reuse incoming transaction for reply */
3170 t = kzalloc(sizeof(*t), GFP_KERNEL);
3171 if (t == 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_t_failed;
3176 }
3177 binder_stats_created(BINDER_STAT_TRANSACTION);
Todd Kjos2f993e22017-05-12 14:42:55 -07003178 spin_lock_init(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003179
3180 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
3181 if (tcomplete == NULL) {
3182 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003183 return_error_param = -ENOMEM;
3184 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003185 goto err_alloc_tcomplete_failed;
3186 }
3187 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
3188
Todd Kjos1cfe6272017-05-24 13:33:28 -07003189 t->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003190
3191 if (reply)
3192 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen59878d72016-09-30 14:05:40 +02003193 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003194 proc->pid, thread->pid, t->debug_id,
3195 target_proc->pid, target_thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003196 (u64)tr->data.ptr.buffer,
3197 (u64)tr->data.ptr.offsets,
Martijn Coenen59878d72016-09-30 14:05:40 +02003198 (u64)tr->data_size, (u64)tr->offsets_size,
3199 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003200 else
3201 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen59878d72016-09-30 14:05:40 +02003202 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003203 proc->pid, thread->pid, t->debug_id,
3204 target_proc->pid, target_node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003205 (u64)tr->data.ptr.buffer,
3206 (u64)tr->data.ptr.offsets,
Martijn Coenen59878d72016-09-30 14:05:40 +02003207 (u64)tr->data_size, (u64)tr->offsets_size,
3208 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003209
3210 if (!reply && !(tr->flags & TF_ONE_WAY))
3211 t->from = thread;
3212 else
3213 t->from = NULL;
Tair Rzayev57bab7c2014-05-31 22:43:34 +03003214 t->sender_euid = task_euid(proc->tsk);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003215 t->to_proc = target_proc;
3216 t->to_thread = target_thread;
3217 t->code = tr->code;
3218 t->flags = tr->flags;
Martijn Coenen57b2ac62017-06-06 17:04:42 -07003219 if (!(t->flags & TF_ONE_WAY) &&
3220 binder_supported_policy(current->policy)) {
3221 /* Inherit supported policies for synchronous transactions */
3222 t->priority.sched_policy = current->policy;
3223 t->priority.prio = current->normal_prio;
3224 } else {
3225 /* Otherwise, fall back to the default priority */
3226 t->priority = target_proc->default_priority;
3227 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003228
Todd Kjos63e0afa2019-01-14 09:10:21 -08003229 if (target_node && target_node->txn_security_ctx) {
3230 u32 secid;
3231
3232 security_task_getsecid(proc->tsk, &secid);
3233 ret = security_secid_to_secctx(secid, &secctx, &secctx_sz);
3234 if (ret) {
3235 return_error = BR_FAILED_REPLY;
3236 return_error_param = ret;
3237 return_error_line = __LINE__;
3238 goto err_get_secctx_failed;
3239 }
3240 extra_buffers_size += ALIGN(secctx_sz, sizeof(u64));
3241 }
3242
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003243 trace_binder_transaction(reply, t, target_node);
3244
Todd Kjosd325d372016-10-10 10:40:53 -07003245 t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size,
Martijn Coenen59878d72016-09-30 14:05:40 +02003246 tr->offsets_size, extra_buffers_size,
3247 !reply && (t->flags & TF_ONE_WAY));
Todd Kjose598d172017-03-22 17:19:52 -07003248 if (IS_ERR(t->buffer)) {
3249 /*
3250 * -ESRCH indicates VMA cleared. The target is dying.
3251 */
3252 return_error_param = PTR_ERR(t->buffer);
3253 return_error = return_error_param == -ESRCH ?
3254 BR_DEAD_REPLY : BR_FAILED_REPLY;
3255 return_error_line = __LINE__;
3256 t->buffer = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003257 goto err_binder_alloc_buf_failed;
3258 }
Todd Kjos63e0afa2019-01-14 09:10:21 -08003259 if (secctx) {
3260 size_t buf_offset = ALIGN(tr->data_size, sizeof(void *)) +
3261 ALIGN(tr->offsets_size, sizeof(void *)) +
3262 ALIGN(extra_buffers_size, sizeof(void *)) -
3263 ALIGN(secctx_sz, sizeof(u64));
Todd Kjos63e0afa2019-01-14 09:10:21 -08003264
Todd Kjos8539b1e2019-02-08 10:35:20 -08003265 t->security_ctx = (uintptr_t)t->buffer->user_data + buf_offset;
Todd Kjos90a570c2019-02-08 10:35:15 -08003266 binder_alloc_copy_to_buffer(&target_proc->alloc,
3267 t->buffer, buf_offset,
3268 secctx, secctx_sz);
Todd Kjos63e0afa2019-01-14 09:10:21 -08003269 security_release_secctx(secctx, secctx_sz);
3270 secctx = NULL;
3271 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003272 t->buffer->debug_id = t->debug_id;
3273 t->buffer->transaction = t;
3274 t->buffer->target_node = target_node;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003275 trace_binder_transaction_alloc_buf(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003276
Todd Kjosd5049492019-02-08 10:35:14 -08003277 if (binder_alloc_copy_user_to_buffer(
3278 &target_proc->alloc,
3279 t->buffer, 0,
3280 (const void __user *)
3281 (uintptr_t)tr->data.ptr.buffer,
3282 tr->data_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303283 binder_user_error("%d:%d got transaction with invalid data ptr\n",
3284 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003285 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003286 return_error_param = -EFAULT;
3287 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003288 goto err_copy_data_failed;
3289 }
Todd Kjosd5049492019-02-08 10:35:14 -08003290 if (binder_alloc_copy_user_to_buffer(
3291 &target_proc->alloc,
3292 t->buffer,
3293 ALIGN(tr->data_size, sizeof(void *)),
3294 (const void __user *)
3295 (uintptr_t)tr->data.ptr.offsets,
3296 tr->offsets_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303297 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3298 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003299 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003300 return_error_param = -EFAULT;
3301 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003302 goto err_copy_data_failed;
3303 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08003304 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
3305 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
3306 proc->pid, thread->pid, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003307 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003308 return_error_param = -EINVAL;
3309 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003310 goto err_bad_offset;
3311 }
Martijn Coenen5a6da532016-09-30 14:10:07 +02003312 if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) {
3313 binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
3314 proc->pid, thread->pid,
Amit Pundir44cbb182017-02-01 12:53:45 +05303315 (u64)extra_buffers_size);
Martijn Coenen5a6da532016-09-30 14:10:07 +02003316 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003317 return_error_param = -EINVAL;
3318 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003319 goto err_bad_offset;
3320 }
Todd Kjos8539b1e2019-02-08 10:35:20 -08003321 off_start_offset = ALIGN(tr->data_size, sizeof(void *));
3322 buffer_offset = off_start_offset;
3323 off_end_offset = off_start_offset + tr->offsets_size;
3324 sg_buf_offset = ALIGN(off_end_offset, sizeof(void *));
3325 sg_buf_end_offset = sg_buf_offset + extra_buffers_size;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08003326 off_min = 0;
Todd Kjos8539b1e2019-02-08 10:35:20 -08003327 for (buffer_offset = off_start_offset; buffer_offset < off_end_offset;
3328 buffer_offset += sizeof(binder_size_t)) {
Martijn Coenen00c80372016-07-13 12:06:49 +02003329 struct binder_object_header *hdr;
Todd Kjos90a570c2019-02-08 10:35:15 -08003330 size_t object_size;
Todd Kjosd73356a2019-02-08 10:35:16 -08003331 struct binder_object object;
Todd Kjos90a570c2019-02-08 10:35:15 -08003332 binder_size_t object_offset;
Seunghun Lee10f62862014-05-01 01:30:23 +09003333
Todd Kjos90a570c2019-02-08 10:35:15 -08003334 binder_alloc_copy_from_buffer(&target_proc->alloc,
3335 &object_offset,
3336 t->buffer,
3337 buffer_offset,
3338 sizeof(object_offset));
Todd Kjosd73356a2019-02-08 10:35:16 -08003339 object_size = binder_get_object(target_proc, t->buffer,
3340 object_offset, &object);
Todd Kjos90a570c2019-02-08 10:35:15 -08003341 if (object_size == 0 || object_offset < off_min) {
Martijn Coenen00c80372016-07-13 12:06:49 +02003342 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 -08003343 proc->pid, thread->pid,
3344 (u64)object_offset,
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08003345 (u64)off_min,
Martijn Coenen00c80372016-07-13 12:06:49 +02003346 (u64)t->buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003347 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003348 return_error_param = -EINVAL;
3349 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003350 goto err_bad_offset;
3351 }
Martijn Coenen00c80372016-07-13 12:06:49 +02003352
Todd Kjosd73356a2019-02-08 10:35:16 -08003353 hdr = &object.hdr;
Todd Kjos90a570c2019-02-08 10:35:15 -08003354 off_min = object_offset + object_size;
Martijn Coenen00c80372016-07-13 12:06:49 +02003355 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003356 case BINDER_TYPE_BINDER:
3357 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003358 struct flat_binder_object *fp;
Seunghun Lee10f62862014-05-01 01:30:23 +09003359
Martijn Coenen00c80372016-07-13 12:06:49 +02003360 fp = to_flat_binder_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003361 ret = binder_translate_binder(fp, t, thread);
3362 if (ret < 0) {
Christian Engelmayer7d420432014-05-07 21:44:53 +02003363 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003364 return_error_param = ret;
3365 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003366 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003367 }
Todd Kjosd73356a2019-02-08 10:35:16 -08003368 binder_alloc_copy_to_buffer(&target_proc->alloc,
3369 t->buffer, object_offset,
3370 fp, sizeof(*fp));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003371 } break;
3372 case BINDER_TYPE_HANDLE:
3373 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003374 struct flat_binder_object *fp;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02003375
Martijn Coenen00c80372016-07-13 12:06:49 +02003376 fp = to_flat_binder_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003377 ret = binder_translate_handle(fp, t, thread);
3378 if (ret < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003379 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003380 return_error_param = ret;
3381 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003382 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003383 }
Todd Kjosd73356a2019-02-08 10:35:16 -08003384 binder_alloc_copy_to_buffer(&target_proc->alloc,
3385 t->buffer, object_offset,
3386 fp, sizeof(*fp));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003387 } break;
3388
3389 case BINDER_TYPE_FD: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003390 struct binder_fd_object *fp = to_binder_fd_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003391 int target_fd = binder_translate_fd(fp->fd, t, thread,
3392 in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003393
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003394 if (target_fd < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003395 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003396 return_error_param = target_fd;
3397 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003398 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003399 }
Martijn Coenen00c80372016-07-13 12:06:49 +02003400 fp->pad_binder = 0;
3401 fp->fd = target_fd;
Todd Kjosd73356a2019-02-08 10:35:16 -08003402 binder_alloc_copy_to_buffer(&target_proc->alloc,
3403 t->buffer, object_offset,
3404 fp, sizeof(*fp));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003405 } break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003406 case BINDER_TYPE_FDA: {
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003407 struct binder_object ptr_object;
3408 binder_size_t parent_offset;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003409 struct binder_fd_array_object *fda =
3410 to_binder_fd_array_object(hdr);
Todd Kjos8539b1e2019-02-08 10:35:20 -08003411 size_t num_valid = (buffer_offset - off_start_offset) *
3412 sizeof(binder_size_t);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003413 struct binder_buffer_object *parent =
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003414 binder_validate_ptr(target_proc, t->buffer,
3415 &ptr_object, fda->parent,
3416 off_start_offset,
3417 &parent_offset,
Todd Kjos8539b1e2019-02-08 10:35:20 -08003418 num_valid);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003419 if (!parent) {
3420 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
3421 proc->pid, thread->pid);
3422 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003423 return_error_param = -EINVAL;
3424 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003425 goto err_bad_parent;
3426 }
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003427 if (!binder_validate_fixup(target_proc, t->buffer,
3428 off_start_offset,
3429 parent_offset,
3430 fda->parent_offset,
3431 last_fixup_obj_off,
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003432 last_fixup_min_off)) {
3433 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
3434 proc->pid, thread->pid);
3435 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003436 return_error_param = -EINVAL;
3437 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003438 goto err_bad_parent;
3439 }
3440 ret = binder_translate_fd_array(fda, parent, t, thread,
3441 in_reply_to);
3442 if (ret < 0) {
3443 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003444 return_error_param = ret;
3445 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003446 goto err_translate_failed;
3447 }
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003448 last_fixup_obj_off = parent_offset;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003449 last_fixup_min_off =
3450 fda->parent_offset + sizeof(u32) * fda->num_fds;
3451 } break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003452 case BINDER_TYPE_PTR: {
3453 struct binder_buffer_object *bp =
3454 to_binder_buffer_object(hdr);
Todd Kjos8539b1e2019-02-08 10:35:20 -08003455 size_t buf_left = sg_buf_end_offset - sg_buf_offset;
3456 size_t num_valid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003457
Martijn Coenen5a6da532016-09-30 14:10:07 +02003458 if (bp->length > buf_left) {
3459 binder_user_error("%d:%d got transaction with too large buffer\n",
3460 proc->pid, thread->pid);
3461 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003462 return_error_param = -EINVAL;
3463 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003464 goto err_bad_offset;
3465 }
Todd Kjosd5049492019-02-08 10:35:14 -08003466 if (binder_alloc_copy_user_to_buffer(
3467 &target_proc->alloc,
3468 t->buffer,
3469 sg_buf_offset,
3470 (const void __user *)
3471 (uintptr_t)bp->buffer,
3472 bp->length)) {
Martijn Coenen5a6da532016-09-30 14:10:07 +02003473 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3474 proc->pid, thread->pid);
Todd Kjose598d172017-03-22 17:19:52 -07003475 return_error_param = -EFAULT;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003476 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003477 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003478 goto err_copy_data_failed;
3479 }
3480 /* Fixup buffer pointer to target proc address space */
Todd Kjos8539b1e2019-02-08 10:35:20 -08003481 bp->buffer = (uintptr_t)
3482 t->buffer->user_data + sg_buf_offset;
3483 sg_buf_offset += ALIGN(bp->length, sizeof(u64));
Martijn Coenen5a6da532016-09-30 14:10:07 +02003484
Todd Kjos8539b1e2019-02-08 10:35:20 -08003485 num_valid = (buffer_offset - off_start_offset) *
3486 sizeof(binder_size_t);
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003487 ret = binder_fixup_parent(t, thread, bp,
3488 off_start_offset,
Todd Kjos8539b1e2019-02-08 10:35:20 -08003489 num_valid,
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003490 last_fixup_obj_off,
Martijn Coenen5a6da532016-09-30 14:10:07 +02003491 last_fixup_min_off);
3492 if (ret < 0) {
3493 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003494 return_error_param = ret;
3495 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003496 goto err_translate_failed;
3497 }
Todd Kjosd73356a2019-02-08 10:35:16 -08003498 binder_alloc_copy_to_buffer(&target_proc->alloc,
3499 t->buffer, object_offset,
3500 bp, sizeof(*bp));
Todd Kjosa2dbf9a2019-02-08 10:35:17 -08003501 last_fixup_obj_off = object_offset;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003502 last_fixup_min_off = 0;
3503 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003504 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01003505 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
Martijn Coenen00c80372016-07-13 12:06:49 +02003506 proc->pid, thread->pid, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003507 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003508 return_error_param = -EINVAL;
3509 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003510 goto err_bad_object_type;
3511 }
3512 }
Todd Kjos8dedb0c2017-05-09 08:31:32 -07003513 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003514 t->work.type = BINDER_WORK_TRANSACTION;
Todd Kjos8dedb0c2017-05-09 08:31:32 -07003515
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003516 if (reply) {
Martijn Coenen1af61802017-10-19 15:04:46 +02003517 binder_enqueue_thread_work(thread, tcomplete);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003518 binder_inner_proc_lock(target_proc);
3519 if (target_thread->is_dead) {
3520 binder_inner_proc_unlock(target_proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07003521 goto err_dead_proc_or_thread;
Martijn Coenen995a36e2017-06-02 13:36:52 -07003522 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003523 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003524 binder_pop_transaction_ilocked(target_thread, in_reply_to);
Martijn Coenen1af61802017-10-19 15:04:46 +02003525 binder_enqueue_thread_work_ilocked(target_thread, &t->work);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003526 binder_inner_proc_unlock(target_proc);
Martijn Coenen053be422017-06-06 15:17:46 -07003527 wake_up_interruptible_sync(&target_thread->wait);
Martijn Coenenecd972d2017-05-26 10:48:56 -07003528 binder_restore_priority(current, in_reply_to->saved_priority);
Todd Kjos21ef40a2017-03-30 18:02:13 -07003529 binder_free_transaction(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003530 } else if (!(t->flags & TF_ONE_WAY)) {
3531 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003532 binder_inner_proc_lock(proc);
Martijn Coenendac2e9c2017-11-13 09:55:21 +01003533 /*
3534 * Defer the TRANSACTION_COMPLETE, so we don't return to
3535 * userspace immediately; this allows the target process to
3536 * immediately start processing this transaction, reducing
3537 * latency. We will then return the TRANSACTION_COMPLETE when
3538 * the target replies (or there is an error).
3539 */
3540 binder_enqueue_deferred_thread_work_ilocked(thread, tcomplete);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003541 t->need_reply = 1;
3542 t->from_parent = thread->transaction_stack;
3543 thread->transaction_stack = t;
Martijn Coenen995a36e2017-06-02 13:36:52 -07003544 binder_inner_proc_unlock(proc);
Martijn Coenen053be422017-06-06 15:17:46 -07003545 if (!binder_proc_transaction(t, target_proc, target_thread)) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07003546 binder_inner_proc_lock(proc);
3547 binder_pop_transaction_ilocked(thread, t);
3548 binder_inner_proc_unlock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07003549 goto err_dead_proc_or_thread;
3550 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003551 } else {
3552 BUG_ON(target_node == NULL);
3553 BUG_ON(t->buffer->async_transaction != 1);
Martijn Coenen1af61802017-10-19 15:04:46 +02003554 binder_enqueue_thread_work(thread, tcomplete);
Martijn Coenen053be422017-06-06 15:17:46 -07003555 if (!binder_proc_transaction(t, target_proc, NULL))
Todd Kjos2f993e22017-05-12 14:42:55 -07003556 goto err_dead_proc_or_thread;
Riley Andrewsb5968812015-09-01 12:42:07 -07003557 }
Todd Kjos2f993e22017-05-12 14:42:55 -07003558 if (target_thread)
3559 binder_thread_dec_tmpref(target_thread);
3560 binder_proc_dec_tmpref(target_proc);
Todd Kjos291d9682017-09-25 08:55:09 -07003561 if (target_node)
3562 binder_dec_node_tmpref(target_node);
Todd Kjos1cfe6272017-05-24 13:33:28 -07003563 /*
3564 * write barrier to synchronize with initialization
3565 * of log entry
3566 */
3567 smp_wmb();
3568 WRITE_ONCE(e->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003569 return;
3570
Todd Kjos2f993e22017-05-12 14:42:55 -07003571err_dead_proc_or_thread:
3572 return_error = BR_DEAD_REPLY;
3573 return_error_line = __LINE__;
Xu YiPing86578a02017-05-22 11:26:23 -07003574 binder_dequeue_work(proc, tcomplete);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003575err_translate_failed:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003576err_bad_object_type:
3577err_bad_offset:
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003578err_bad_parent:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003579err_copy_data_failed:
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003580 trace_binder_transaction_failed_buffer_release(t->buffer);
Todd Kjos8539b1e2019-02-08 10:35:20 -08003581 binder_transaction_buffer_release(target_proc, t->buffer,
3582 buffer_offset, true);
Todd Kjos291d9682017-09-25 08:55:09 -07003583 if (target_node)
3584 binder_dec_node_tmpref(target_node);
Todd Kjosc37162d2017-05-26 11:56:29 -07003585 target_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003586 t->buffer->transaction = NULL;
Todd Kjosd325d372016-10-10 10:40:53 -07003587 binder_alloc_free_buf(&target_proc->alloc, t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003588err_binder_alloc_buf_failed:
Todd Kjos63e0afa2019-01-14 09:10:21 -08003589 if (secctx)
3590 security_release_secctx(secctx, secctx_sz);
3591err_get_secctx_failed:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003592 kfree(tcomplete);
3593 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3594err_alloc_tcomplete_failed:
3595 kfree(t);
3596 binder_stats_deleted(BINDER_STAT_TRANSACTION);
3597err_alloc_t_failed:
3598err_bad_call_stack:
3599err_empty_call_stack:
3600err_dead_binder:
3601err_invalid_target_handle:
Todd Kjos2f993e22017-05-12 14:42:55 -07003602 if (target_thread)
3603 binder_thread_dec_tmpref(target_thread);
3604 if (target_proc)
3605 binder_proc_dec_tmpref(target_proc);
Todd Kjos291d9682017-09-25 08:55:09 -07003606 if (target_node) {
Todd Kjosc37162d2017-05-26 11:56:29 -07003607 binder_dec_node(target_node, 1, 0);
Todd Kjos291d9682017-09-25 08:55:09 -07003608 binder_dec_node_tmpref(target_node);
3609 }
Todd Kjosc37162d2017-05-26 11:56:29 -07003610
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003611 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Todd Kjose598d172017-03-22 17:19:52 -07003612 "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
3613 proc->pid, thread->pid, return_error, return_error_param,
3614 (u64)tr->data_size, (u64)tr->offsets_size,
3615 return_error_line);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003616
3617 {
3618 struct binder_transaction_log_entry *fe;
Seunghun Lee10f62862014-05-01 01:30:23 +09003619
Todd Kjose598d172017-03-22 17:19:52 -07003620 e->return_error = return_error;
3621 e->return_error_param = return_error_param;
3622 e->return_error_line = return_error_line;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003623 fe = binder_transaction_log_add(&binder_transaction_log_failed);
3624 *fe = *e;
Todd Kjos1cfe6272017-05-24 13:33:28 -07003625 /*
3626 * write barrier to synchronize with initialization
3627 * of log entry
3628 */
3629 smp_wmb();
3630 WRITE_ONCE(e->debug_id_done, t_debug_id);
3631 WRITE_ONCE(fe->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003632 }
3633
Todd Kjos858b8da2017-04-21 17:35:12 -07003634 BUG_ON(thread->return_error.cmd != BR_OK);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003635 if (in_reply_to) {
Martijn Coenenecd972d2017-05-26 10:48:56 -07003636 binder_restore_priority(current, in_reply_to->saved_priority);
Todd Kjos858b8da2017-04-21 17:35:12 -07003637 thread->return_error.cmd = BR_TRANSACTION_COMPLETE;
Martijn Coenen1af61802017-10-19 15:04:46 +02003638 binder_enqueue_thread_work(thread, &thread->return_error.work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003639 binder_send_failed_reply(in_reply_to, return_error);
Todd Kjos858b8da2017-04-21 17:35:12 -07003640 } else {
3641 thread->return_error.cmd = return_error;
Martijn Coenen1af61802017-10-19 15:04:46 +02003642 binder_enqueue_thread_work(thread, &thread->return_error.work);
Todd Kjos858b8da2017-04-21 17:35:12 -07003643 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003644}
3645
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02003646static int binder_thread_write(struct binder_proc *proc,
3647 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003648 binder_uintptr_t binder_buffer, size_t size,
3649 binder_size_t *consumed)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003650{
3651 uint32_t cmd;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003652 struct binder_context *context = proc->context;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003653 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003654 void __user *ptr = buffer + *consumed;
3655 void __user *end = buffer + size;
3656
Todd Kjos858b8da2017-04-21 17:35:12 -07003657 while (ptr < end && thread->return_error.cmd == BR_OK) {
Todd Kjosb0117bb2017-05-08 09:16:27 -07003658 int ret;
3659
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003660 if (get_user(cmd, (uint32_t __user *)ptr))
3661 return -EFAULT;
3662 ptr += sizeof(uint32_t);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003663 trace_binder_command(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003664 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07003665 atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]);
3666 atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]);
3667 atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003668 }
3669 switch (cmd) {
3670 case BC_INCREFS:
3671 case BC_ACQUIRE:
3672 case BC_RELEASE:
3673 case BC_DECREFS: {
3674 uint32_t target;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003675 const char *debug_string;
Todd Kjosb0117bb2017-05-08 09:16:27 -07003676 bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE;
3677 bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE;
3678 struct binder_ref_data rdata;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003679
3680 if (get_user(target, (uint32_t __user *)ptr))
3681 return -EFAULT;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003682
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003683 ptr += sizeof(uint32_t);
Todd Kjosb0117bb2017-05-08 09:16:27 -07003684 ret = -1;
3685 if (increment && !target) {
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003686 struct binder_node *ctx_mgr_node;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003687 mutex_lock(&context->context_mgr_node_lock);
3688 ctx_mgr_node = context->binder_context_mgr_node;
Todd Kjosb0117bb2017-05-08 09:16:27 -07003689 if (ctx_mgr_node)
3690 ret = binder_inc_ref_for_node(
3691 proc, ctx_mgr_node,
3692 strong, NULL, &rdata);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003693 mutex_unlock(&context->context_mgr_node_lock);
3694 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07003695 if (ret)
3696 ret = binder_update_ref_for_handle(
3697 proc, target, increment, strong,
3698 &rdata);
3699 if (!ret && rdata.desc != target) {
3700 binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
3701 proc->pid, thread->pid,
3702 target, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003703 }
3704 switch (cmd) {
3705 case BC_INCREFS:
3706 debug_string = "IncRefs";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003707 break;
3708 case BC_ACQUIRE:
3709 debug_string = "Acquire";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003710 break;
3711 case BC_RELEASE:
3712 debug_string = "Release";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003713 break;
3714 case BC_DECREFS:
3715 default:
3716 debug_string = "DecRefs";
Todd Kjosb0117bb2017-05-08 09:16:27 -07003717 break;
3718 }
3719 if (ret) {
3720 binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
3721 proc->pid, thread->pid, debug_string,
3722 strong, target, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003723 break;
3724 }
3725 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjosb0117bb2017-05-08 09:16:27 -07003726 "%d:%d %s ref %d desc %d s %d w %d\n",
3727 proc->pid, thread->pid, debug_string,
3728 rdata.debug_id, rdata.desc, rdata.strong,
3729 rdata.weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003730 break;
3731 }
3732 case BC_INCREFS_DONE:
3733 case BC_ACQUIRE_DONE: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003734 binder_uintptr_t node_ptr;
3735 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003736 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003737 bool free_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003738
Arve Hjønnevågda498892014-02-21 14:40:26 -08003739 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003740 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003741 ptr += sizeof(binder_uintptr_t);
3742 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003743 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003744 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003745 node = binder_get_node(proc, node_ptr);
3746 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003747 binder_user_error("%d:%d %s u%016llx no match\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003748 proc->pid, thread->pid,
3749 cmd == BC_INCREFS_DONE ?
3750 "BC_INCREFS_DONE" :
3751 "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003752 (u64)node_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003753 break;
3754 }
3755 if (cookie != node->cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003756 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003757 proc->pid, thread->pid,
3758 cmd == BC_INCREFS_DONE ?
3759 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003760 (u64)node_ptr, node->debug_id,
3761 (u64)cookie, (u64)node->cookie);
Todd Kjosf22abc72017-05-09 11:08:05 -07003762 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003763 break;
3764 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003765 binder_node_inner_lock(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003766 if (cmd == BC_ACQUIRE_DONE) {
3767 if (node->pending_strong_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303768 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003769 proc->pid, thread->pid,
3770 node->debug_id);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003771 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003772 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003773 break;
3774 }
3775 node->pending_strong_ref = 0;
3776 } else {
3777 if (node->pending_weak_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303778 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003779 proc->pid, thread->pid,
3780 node->debug_id);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003781 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003782 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003783 break;
3784 }
3785 node->pending_weak_ref = 0;
3786 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003787 free_node = binder_dec_node_nilocked(node,
3788 cmd == BC_ACQUIRE_DONE, 0);
3789 WARN_ON(free_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003790 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjosf22abc72017-05-09 11:08:05 -07003791 "%d:%d %s node %d ls %d lw %d tr %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003792 proc->pid, thread->pid,
3793 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Todd Kjosf22abc72017-05-09 11:08:05 -07003794 node->debug_id, node->local_strong_refs,
3795 node->local_weak_refs, node->tmp_refs);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003796 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003797 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003798 break;
3799 }
3800 case BC_ATTEMPT_ACQUIRE:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303801 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003802 return -EINVAL;
3803 case BC_ACQUIRE_RESULT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303804 pr_err("BC_ACQUIRE_RESULT not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003805 return -EINVAL;
3806
3807 case BC_FREE_BUFFER: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003808 binder_uintptr_t data_ptr;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003809 struct binder_buffer *buffer;
3810
Arve Hjønnevågda498892014-02-21 14:40:26 -08003811 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003812 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003813 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003814
Todd Kjos076072a2017-04-21 14:32:11 -07003815 buffer = binder_alloc_prepare_to_free(&proc->alloc,
3816 data_ptr);
Todd Kjosd29b73e2018-11-06 15:55:32 -08003817 if (IS_ERR_OR_NULL(buffer)) {
3818 if (PTR_ERR(buffer) == -EPERM) {
3819 binder_user_error(
3820 "%d:%d BC_FREE_BUFFER u%016llx matched unreturned or currently freeing buffer\n",
3821 proc->pid, thread->pid,
3822 (u64)data_ptr);
3823 } else {
3824 binder_user_error(
3825 "%d:%d BC_FREE_BUFFER u%016llx no match\n",
3826 proc->pid, thread->pid,
3827 (u64)data_ptr);
3828 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003829 break;
3830 }
3831 binder_debug(BINDER_DEBUG_FREE_BUFFER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003832 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
3833 proc->pid, thread->pid, (u64)data_ptr,
3834 buffer->debug_id,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003835 buffer->transaction ? "active" : "finished");
3836
3837 if (buffer->transaction) {
3838 buffer->transaction->buffer = NULL;
3839 buffer->transaction = NULL;
3840 }
3841 if (buffer->async_transaction && buffer->target_node) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003842 struct binder_node *buf_node;
3843 struct binder_work *w;
3844
3845 buf_node = buffer->target_node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003846 binder_node_inner_lock(buf_node);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003847 BUG_ON(!buf_node->has_async_transaction);
3848 BUG_ON(buf_node->proc != proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003849 w = binder_dequeue_work_head_ilocked(
3850 &buf_node->async_todo);
Martijn Coenen4501c042017-08-10 13:56:16 +02003851 if (!w) {
Gustavo A. R. Silvae62dd6f2018-01-23 12:04:27 -06003852 buf_node->has_async_transaction = false;
Martijn Coenen4501c042017-08-10 13:56:16 +02003853 } else {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003854 binder_enqueue_work_ilocked(
Martijn Coenen4501c042017-08-10 13:56:16 +02003855 w, &proc->todo);
3856 binder_wakeup_proc_ilocked(proc);
3857 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003858 binder_node_inner_unlock(buf_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003859 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003860 trace_binder_transaction_buffer_release(buffer);
Todd Kjos8539b1e2019-02-08 10:35:20 -08003861 binder_transaction_buffer_release(proc, buffer, 0, false);
Todd Kjosd325d372016-10-10 10:40:53 -07003862 binder_alloc_free_buf(&proc->alloc, buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003863 break;
3864 }
3865
Martijn Coenen5a6da532016-09-30 14:10:07 +02003866 case BC_TRANSACTION_SG:
3867 case BC_REPLY_SG: {
3868 struct binder_transaction_data_sg tr;
3869
3870 if (copy_from_user(&tr, ptr, sizeof(tr)))
3871 return -EFAULT;
3872 ptr += sizeof(tr);
3873 binder_transaction(proc, thread, &tr.transaction_data,
3874 cmd == BC_REPLY_SG, tr.buffers_size);
3875 break;
3876 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003877 case BC_TRANSACTION:
3878 case BC_REPLY: {
3879 struct binder_transaction_data tr;
3880
3881 if (copy_from_user(&tr, ptr, sizeof(tr)))
3882 return -EFAULT;
3883 ptr += sizeof(tr);
Martijn Coenen59878d72016-09-30 14:05:40 +02003884 binder_transaction(proc, thread, &tr,
3885 cmd == BC_REPLY, 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003886 break;
3887 }
3888
3889 case BC_REGISTER_LOOPER:
3890 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303891 "%d:%d BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003892 proc->pid, thread->pid);
Todd Kjosd600e902017-05-25 17:35:02 -07003893 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003894 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
3895 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303896 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003897 proc->pid, thread->pid);
3898 } else if (proc->requested_threads == 0) {
3899 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303900 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003901 proc->pid, thread->pid);
3902 } else {
3903 proc->requested_threads--;
3904 proc->requested_threads_started++;
3905 }
3906 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
Todd Kjosd600e902017-05-25 17:35:02 -07003907 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003908 break;
3909 case BC_ENTER_LOOPER:
3910 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303911 "%d:%d BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003912 proc->pid, thread->pid);
3913 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
3914 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303915 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003916 proc->pid, thread->pid);
3917 }
3918 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
3919 break;
3920 case BC_EXIT_LOOPER:
3921 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303922 "%d:%d BC_EXIT_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003923 proc->pid, thread->pid);
3924 thread->looper |= BINDER_LOOPER_STATE_EXITED;
3925 break;
3926
3927 case BC_REQUEST_DEATH_NOTIFICATION:
3928 case BC_CLEAR_DEATH_NOTIFICATION: {
3929 uint32_t target;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003930 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003931 struct binder_ref *ref;
Todd Kjos5346bf32016-10-20 16:43:34 -07003932 struct binder_ref_death *death = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003933
3934 if (get_user(target, (uint32_t __user *)ptr))
3935 return -EFAULT;
3936 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08003937 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003938 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003939 ptr += sizeof(binder_uintptr_t);
Todd Kjos5346bf32016-10-20 16:43:34 -07003940 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3941 /*
3942 * Allocate memory for death notification
3943 * before taking lock
3944 */
3945 death = kzalloc(sizeof(*death), GFP_KERNEL);
3946 if (death == NULL) {
3947 WARN_ON(thread->return_error.cmd !=
3948 BR_OK);
3949 thread->return_error.cmd = BR_ERROR;
Martijn Coenen1af61802017-10-19 15:04:46 +02003950 binder_enqueue_thread_work(
3951 thread,
3952 &thread->return_error.work);
Todd Kjos5346bf32016-10-20 16:43:34 -07003953 binder_debug(
3954 BINDER_DEBUG_FAILED_TRANSACTION,
3955 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
3956 proc->pid, thread->pid);
3957 break;
3958 }
3959 }
3960 binder_proc_lock(proc);
3961 ref = binder_get_ref_olocked(proc, target, false);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003962 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303963 binder_user_error("%d:%d %s invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003964 proc->pid, thread->pid,
3965 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3966 "BC_REQUEST_DEATH_NOTIFICATION" :
3967 "BC_CLEAR_DEATH_NOTIFICATION",
3968 target);
Todd Kjos5346bf32016-10-20 16:43:34 -07003969 binder_proc_unlock(proc);
3970 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003971 break;
3972 }
3973
3974 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003975 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003976 proc->pid, thread->pid,
3977 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3978 "BC_REQUEST_DEATH_NOTIFICATION" :
3979 "BC_CLEAR_DEATH_NOTIFICATION",
Todd Kjosb0117bb2017-05-08 09:16:27 -07003980 (u64)cookie, ref->data.debug_id,
3981 ref->data.desc, ref->data.strong,
3982 ref->data.weak, ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003983
Martijn Coenenf9eac642017-05-22 11:26:23 -07003984 binder_node_lock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003985 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3986 if (ref->death) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303987 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003988 proc->pid, thread->pid);
Martijn Coenenf9eac642017-05-22 11:26:23 -07003989 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003990 binder_proc_unlock(proc);
3991 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003992 break;
3993 }
3994 binder_stats_created(BINDER_STAT_DEATH);
3995 INIT_LIST_HEAD(&death->work.entry);
3996 death->cookie = cookie;
3997 ref->death = death;
3998 if (ref->node->proc == NULL) {
3999 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
Martijn Coenen3bdbe4c2017-08-10 13:50:52 +02004000
4001 binder_inner_proc_lock(proc);
4002 binder_enqueue_work_ilocked(
4003 &ref->death->work, &proc->todo);
4004 binder_wakeup_proc_ilocked(proc);
4005 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004006 }
4007 } else {
4008 if (ref->death == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05304009 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004010 proc->pid, thread->pid);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004011 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07004012 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004013 break;
4014 }
4015 death = ref->death;
4016 if (death->cookie != cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08004017 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004018 proc->pid, thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004019 (u64)death->cookie,
4020 (u64)cookie);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004021 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07004022 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004023 break;
4024 }
4025 ref->death = NULL;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004026 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004027 if (list_empty(&death->work.entry)) {
4028 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004029 if (thread->looper &
4030 (BINDER_LOOPER_STATE_REGISTERED |
4031 BINDER_LOOPER_STATE_ENTERED))
Martijn Coenen1af61802017-10-19 15:04:46 +02004032 binder_enqueue_thread_work_ilocked(
4033 thread,
4034 &death->work);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004035 else {
4036 binder_enqueue_work_ilocked(
4037 &death->work,
4038 &proc->todo);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004039 binder_wakeup_proc_ilocked(
Martijn Coenen053be422017-06-06 15:17:46 -07004040 proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004041 }
4042 } else {
4043 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
4044 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
4045 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004046 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004047 }
Martijn Coenenf9eac642017-05-22 11:26:23 -07004048 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07004049 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004050 } break;
4051 case BC_DEAD_BINDER_DONE: {
4052 struct binder_work *w;
Arve Hjønnevågda498892014-02-21 14:40:26 -08004053 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004054 struct binder_ref_death *death = NULL;
Seunghun Lee10f62862014-05-01 01:30:23 +09004055
Arve Hjønnevågda498892014-02-21 14:40:26 -08004056 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004057 return -EFAULT;
4058
Lisa Du7a64cd82016-02-17 09:32:52 +08004059 ptr += sizeof(cookie);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004060 binder_inner_proc_lock(proc);
4061 list_for_each_entry(w, &proc->delivered_death,
4062 entry) {
4063 struct binder_ref_death *tmp_death =
4064 container_of(w,
4065 struct binder_ref_death,
4066 work);
Seunghun Lee10f62862014-05-01 01:30:23 +09004067
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004068 if (tmp_death->cookie == cookie) {
4069 death = tmp_death;
4070 break;
4071 }
4072 }
4073 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Todd Kjosf540ce02018-02-07 13:57:37 -08004074 "%d:%d BC_DEAD_BINDER_DONE %016llx found %pK\n",
Arve Hjønnevågda498892014-02-21 14:40:26 -08004075 proc->pid, thread->pid, (u64)cookie,
4076 death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004077 if (death == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08004078 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
4079 proc->pid, thread->pid, (u64)cookie);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004080 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004081 break;
4082 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004083 binder_dequeue_work_ilocked(&death->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004084 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
4085 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004086 if (thread->looper &
4087 (BINDER_LOOPER_STATE_REGISTERED |
4088 BINDER_LOOPER_STATE_ENTERED))
Martijn Coenen1af61802017-10-19 15:04:46 +02004089 binder_enqueue_thread_work_ilocked(
4090 thread, &death->work);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004091 else {
4092 binder_enqueue_work_ilocked(
4093 &death->work,
4094 &proc->todo);
Martijn Coenen053be422017-06-06 15:17:46 -07004095 binder_wakeup_proc_ilocked(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004096 }
4097 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004098 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004099 } break;
4100
4101 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304102 pr_err("%d:%d unknown command %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004103 proc->pid, thread->pid, cmd);
4104 return -EINVAL;
4105 }
4106 *consumed = ptr - buffer;
4107 }
4108 return 0;
4109}
4110
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02004111static void binder_stat_br(struct binder_proc *proc,
4112 struct binder_thread *thread, uint32_t cmd)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004113{
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004114 trace_binder_return(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004115 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07004116 atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
4117 atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
4118 atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004119 }
4120}
4121
Todd Kjos60792612017-05-24 10:51:01 -07004122static int binder_put_node_cmd(struct binder_proc *proc,
4123 struct binder_thread *thread,
4124 void __user **ptrp,
4125 binder_uintptr_t node_ptr,
4126 binder_uintptr_t node_cookie,
4127 int node_debug_id,
4128 uint32_t cmd, const char *cmd_name)
4129{
4130 void __user *ptr = *ptrp;
4131
4132 if (put_user(cmd, (uint32_t __user *)ptr))
4133 return -EFAULT;
4134 ptr += sizeof(uint32_t);
4135
4136 if (put_user(node_ptr, (binder_uintptr_t __user *)ptr))
4137 return -EFAULT;
4138 ptr += sizeof(binder_uintptr_t);
4139
4140 if (put_user(node_cookie, (binder_uintptr_t __user *)ptr))
4141 return -EFAULT;
4142 ptr += sizeof(binder_uintptr_t);
4143
4144 binder_stat_br(proc, thread, cmd);
4145 binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n",
4146 proc->pid, thread->pid, cmd_name, node_debug_id,
4147 (u64)node_ptr, (u64)node_cookie);
4148
4149 *ptrp = ptr;
4150 return 0;
4151}
4152
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004153static int binder_wait_for_work(struct binder_thread *thread,
4154 bool do_proc_work)
4155{
4156 DEFINE_WAIT(wait);
4157 struct binder_proc *proc = thread->proc;
4158 int ret = 0;
4159
4160 freezer_do_not_count();
4161 binder_inner_proc_lock(proc);
4162 for (;;) {
4163 prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE);
4164 if (binder_has_work_ilocked(thread, do_proc_work))
4165 break;
4166 if (do_proc_work)
4167 list_add(&thread->waiting_thread_node,
4168 &proc->waiting_threads);
4169 binder_inner_proc_unlock(proc);
4170 schedule();
4171 binder_inner_proc_lock(proc);
4172 list_del_init(&thread->waiting_thread_node);
4173 if (signal_pending(current)) {
4174 ret = -ERESTARTSYS;
4175 break;
4176 }
4177 }
4178 finish_wait(&thread->wait, &wait);
4179 binder_inner_proc_unlock(proc);
4180 freezer_count();
4181
4182 return ret;
4183}
4184
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004185static int binder_thread_read(struct binder_proc *proc,
4186 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004187 binder_uintptr_t binder_buffer, size_t size,
4188 binder_size_t *consumed, int non_block)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004189{
Arve Hjønnevågda498892014-02-21 14:40:26 -08004190 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004191 void __user *ptr = buffer + *consumed;
4192 void __user *end = buffer + size;
4193
4194 int ret = 0;
4195 int wait_for_proc_work;
4196
4197 if (*consumed == 0) {
4198 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
4199 return -EFAULT;
4200 ptr += sizeof(uint32_t);
4201 }
4202
4203retry:
Martijn Coenen995a36e2017-06-02 13:36:52 -07004204 binder_inner_proc_lock(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004205 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
Martijn Coenen995a36e2017-06-02 13:36:52 -07004206 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004207
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004208 thread->looper |= BINDER_LOOPER_STATE_WAITING;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004209
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004210 trace_binder_wait_for_work(wait_for_proc_work,
4211 !!thread->transaction_stack,
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004212 !binder_worklist_empty(proc, &thread->todo));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004213 if (wait_for_proc_work) {
4214 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4215 BINDER_LOOPER_STATE_ENTERED))) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05304216 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 +09004217 proc->pid, thread->pid, thread->looper);
4218 wait_event_interruptible(binder_user_error_wait,
4219 binder_stop_on_user_error < 2);
4220 }
Martijn Coenenecd972d2017-05-26 10:48:56 -07004221 binder_restore_priority(current, proc->default_priority);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004222 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004223
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004224 if (non_block) {
4225 if (!binder_has_work(thread, wait_for_proc_work))
4226 ret = -EAGAIN;
4227 } else {
4228 ret = binder_wait_for_work(thread, wait_for_proc_work);
4229 }
4230
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004231 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
4232
4233 if (ret)
4234 return ret;
4235
4236 while (1) {
4237 uint32_t cmd;
Todd Kjos63e0afa2019-01-14 09:10:21 -08004238 struct binder_transaction_data_secctx tr;
4239 struct binder_transaction_data *trd = &tr.transaction_data;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004240 struct binder_work *w = NULL;
4241 struct list_head *list = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004242 struct binder_transaction *t = NULL;
Todd Kjos2f993e22017-05-12 14:42:55 -07004243 struct binder_thread *t_from;
Todd Kjos63e0afa2019-01-14 09:10:21 -08004244 size_t trsize = sizeof(*trd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004245
Todd Kjose7f23ed2017-03-21 13:06:01 -07004246 binder_inner_proc_lock(proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004247 if (!binder_worklist_empty_ilocked(&thread->todo))
4248 list = &thread->todo;
4249 else if (!binder_worklist_empty_ilocked(&proc->todo) &&
4250 wait_for_proc_work)
4251 list = &proc->todo;
4252 else {
4253 binder_inner_proc_unlock(proc);
4254
Dmitry Voytik395262a2014-09-08 18:16:34 +04004255 /* no data added */
Todd Kjos6798e6d2017-01-06 14:19:25 -08004256 if (ptr - buffer == 4 && !thread->looper_need_return)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004257 goto retry;
4258 break;
4259 }
4260
Todd Kjose7f23ed2017-03-21 13:06:01 -07004261 if (end - ptr < sizeof(tr) + 4) {
4262 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004263 break;
Todd Kjose7f23ed2017-03-21 13:06:01 -07004264 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004265 w = binder_dequeue_work_head_ilocked(list);
Martijn Coenen1af61802017-10-19 15:04:46 +02004266 if (binder_worklist_empty_ilocked(&thread->todo))
4267 thread->process_todo = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004268
4269 switch (w->type) {
4270 case BINDER_WORK_TRANSACTION: {
Todd Kjose7f23ed2017-03-21 13:06:01 -07004271 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004272 t = container_of(w, struct binder_transaction, work);
4273 } break;
Todd Kjos858b8da2017-04-21 17:35:12 -07004274 case BINDER_WORK_RETURN_ERROR: {
4275 struct binder_error *e = container_of(
4276 w, struct binder_error, work);
4277
4278 WARN_ON(e->cmd == BR_OK);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004279 binder_inner_proc_unlock(proc);
Todd Kjos858b8da2017-04-21 17:35:12 -07004280 if (put_user(e->cmd, (uint32_t __user *)ptr))
4281 return -EFAULT;
宋金时e1b1a8b2018-05-10 02:05:03 +00004282 cmd = e->cmd;
Todd Kjos858b8da2017-04-21 17:35:12 -07004283 e->cmd = BR_OK;
4284 ptr += sizeof(uint32_t);
4285
4286 binder_stat_br(proc, thread, cmd);
Todd Kjos858b8da2017-04-21 17:35:12 -07004287 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004288 case BINDER_WORK_TRANSACTION_COMPLETE: {
Todd Kjose7f23ed2017-03-21 13:06:01 -07004289 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004290 cmd = BR_TRANSACTION_COMPLETE;
4291 if (put_user(cmd, (uint32_t __user *)ptr))
4292 return -EFAULT;
4293 ptr += sizeof(uint32_t);
4294
4295 binder_stat_br(proc, thread, cmd);
4296 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304297 "%d:%d BR_TRANSACTION_COMPLETE\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004298 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004299 kfree(w);
4300 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4301 } break;
4302 case BINDER_WORK_NODE: {
4303 struct binder_node *node = container_of(w, struct binder_node, work);
Todd Kjos60792612017-05-24 10:51:01 -07004304 int strong, weak;
4305 binder_uintptr_t node_ptr = node->ptr;
4306 binder_uintptr_t node_cookie = node->cookie;
4307 int node_debug_id = node->debug_id;
4308 int has_weak_ref;
4309 int has_strong_ref;
4310 void __user *orig_ptr = ptr;
Seunghun Lee10f62862014-05-01 01:30:23 +09004311
Todd Kjos60792612017-05-24 10:51:01 -07004312 BUG_ON(proc != node->proc);
4313 strong = node->internal_strong_refs ||
4314 node->local_strong_refs;
4315 weak = !hlist_empty(&node->refs) ||
Todd Kjosf22abc72017-05-09 11:08:05 -07004316 node->local_weak_refs ||
4317 node->tmp_refs || strong;
Todd Kjos60792612017-05-24 10:51:01 -07004318 has_strong_ref = node->has_strong_ref;
4319 has_weak_ref = node->has_weak_ref;
4320
4321 if (weak && !has_weak_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004322 node->has_weak_ref = 1;
4323 node->pending_weak_ref = 1;
4324 node->local_weak_refs++;
Todd Kjos60792612017-05-24 10:51:01 -07004325 }
4326 if (strong && !has_strong_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004327 node->has_strong_ref = 1;
4328 node->pending_strong_ref = 1;
4329 node->local_strong_refs++;
Todd Kjos60792612017-05-24 10:51:01 -07004330 }
4331 if (!strong && has_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004332 node->has_strong_ref = 0;
Todd Kjos60792612017-05-24 10:51:01 -07004333 if (!weak && has_weak_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004334 node->has_weak_ref = 0;
Todd Kjos60792612017-05-24 10:51:01 -07004335 if (!weak && !strong) {
4336 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4337 "%d:%d node %d u%016llx c%016llx deleted\n",
4338 proc->pid, thread->pid,
4339 node_debug_id,
4340 (u64)node_ptr,
4341 (u64)node_cookie);
4342 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004343 binder_inner_proc_unlock(proc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004344 binder_node_lock(node);
4345 /*
4346 * Acquire the node lock before freeing the
4347 * node to serialize with other threads that
4348 * may have been holding the node lock while
4349 * decrementing this node (avoids race where
4350 * this thread frees while the other thread
4351 * is unlocking the node after the final
4352 * decrement)
4353 */
4354 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004355 binder_free_node(node);
4356 } else
4357 binder_inner_proc_unlock(proc);
4358
Todd Kjos60792612017-05-24 10:51:01 -07004359 if (weak && !has_weak_ref)
4360 ret = binder_put_node_cmd(
4361 proc, thread, &ptr, node_ptr,
4362 node_cookie, node_debug_id,
4363 BR_INCREFS, "BR_INCREFS");
4364 if (!ret && strong && !has_strong_ref)
4365 ret = binder_put_node_cmd(
4366 proc, thread, &ptr, node_ptr,
4367 node_cookie, node_debug_id,
4368 BR_ACQUIRE, "BR_ACQUIRE");
4369 if (!ret && !strong && has_strong_ref)
4370 ret = binder_put_node_cmd(
4371 proc, thread, &ptr, node_ptr,
4372 node_cookie, node_debug_id,
4373 BR_RELEASE, "BR_RELEASE");
4374 if (!ret && !weak && has_weak_ref)
4375 ret = binder_put_node_cmd(
4376 proc, thread, &ptr, node_ptr,
4377 node_cookie, node_debug_id,
4378 BR_DECREFS, "BR_DECREFS");
4379 if (orig_ptr == ptr)
4380 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4381 "%d:%d node %d u%016llx c%016llx state unchanged\n",
4382 proc->pid, thread->pid,
4383 node_debug_id,
4384 (u64)node_ptr,
4385 (u64)node_cookie);
4386 if (ret)
4387 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004388 } break;
4389 case BINDER_WORK_DEAD_BINDER:
4390 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4391 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4392 struct binder_ref_death *death;
4393 uint32_t cmd;
Martijn Coenenf9eac642017-05-22 11:26:23 -07004394 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004395
4396 death = container_of(w, struct binder_ref_death, work);
4397 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
4398 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
4399 else
4400 cmd = BR_DEAD_BINDER;
Martijn Coenenf9eac642017-05-22 11:26:23 -07004401 cookie = death->cookie;
4402
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004403 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004404 "%d:%d %s %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004405 proc->pid, thread->pid,
4406 cmd == BR_DEAD_BINDER ?
4407 "BR_DEAD_BINDER" :
4408 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
Martijn Coenenf9eac642017-05-22 11:26:23 -07004409 (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004410 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
Martijn Coenenf9eac642017-05-22 11:26:23 -07004411 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004412 kfree(death);
4413 binder_stats_deleted(BINDER_STAT_DEATH);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004414 } else {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004415 binder_enqueue_work_ilocked(
4416 w, &proc->delivered_death);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004417 binder_inner_proc_unlock(proc);
4418 }
Martijn Coenenf9eac642017-05-22 11:26:23 -07004419 if (put_user(cmd, (uint32_t __user *)ptr))
4420 return -EFAULT;
4421 ptr += sizeof(uint32_t);
4422 if (put_user(cookie,
4423 (binder_uintptr_t __user *)ptr))
4424 return -EFAULT;
4425 ptr += sizeof(binder_uintptr_t);
4426 binder_stat_br(proc, thread, cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004427 if (cmd == BR_DEAD_BINDER)
4428 goto done; /* DEAD_BINDER notifications can cause transactions */
4429 } break;
4430 }
4431
4432 if (!t)
4433 continue;
4434
4435 BUG_ON(t->buffer == NULL);
4436 if (t->buffer->target_node) {
4437 struct binder_node *target_node = t->buffer->target_node;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004438 struct binder_priority node_prio;
Seunghun Lee10f62862014-05-01 01:30:23 +09004439
Todd Kjos63e0afa2019-01-14 09:10:21 -08004440 trd->target.ptr = target_node->ptr;
4441 trd->cookie = target_node->cookie;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004442 node_prio.sched_policy = target_node->sched_policy;
4443 node_prio.prio = target_node->min_priority;
Martijn Coenenc46810c2017-06-23 10:13:43 -07004444 binder_transaction_priority(current, t, node_prio,
4445 target_node->inherit_rt);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004446 cmd = BR_TRANSACTION;
4447 } else {
Todd Kjos63e0afa2019-01-14 09:10:21 -08004448 trd->target.ptr = 0;
4449 trd->cookie = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004450 cmd = BR_REPLY;
4451 }
Todd Kjos63e0afa2019-01-14 09:10:21 -08004452 trd->code = t->code;
4453 trd->flags = t->flags;
4454 trd->sender_euid = from_kuid(current_user_ns(), t->sender_euid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004455
Todd Kjos2f993e22017-05-12 14:42:55 -07004456 t_from = binder_get_txn_from(t);
4457 if (t_from) {
4458 struct task_struct *sender = t_from->proc->tsk;
Seunghun Lee10f62862014-05-01 01:30:23 +09004459
Todd Kjos63e0afa2019-01-14 09:10:21 -08004460 trd->sender_pid =
4461 task_tgid_nr_ns(sender,
4462 task_active_pid_ns(current));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004463 } else {
Todd Kjos63e0afa2019-01-14 09:10:21 -08004464 trd->sender_pid = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004465 }
4466
Todd Kjos63e0afa2019-01-14 09:10:21 -08004467 trd->data_size = t->buffer->data_size;
4468 trd->offsets_size = t->buffer->offsets_size;
Todd Kjos8539b1e2019-02-08 10:35:20 -08004469 trd->data.ptr.buffer = (uintptr_t)t->buffer->user_data;
Todd Kjos63e0afa2019-01-14 09:10:21 -08004470 trd->data.ptr.offsets = trd->data.ptr.buffer +
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004471 ALIGN(t->buffer->data_size,
4472 sizeof(void *));
4473
Todd Kjos63e0afa2019-01-14 09:10:21 -08004474 tr.secctx = t->security_ctx;
4475 if (t->security_ctx) {
4476 cmd = BR_TRANSACTION_SEC_CTX;
4477 trsize = sizeof(tr);
4478 }
Todd Kjos2f993e22017-05-12 14:42:55 -07004479 if (put_user(cmd, (uint32_t __user *)ptr)) {
4480 if (t_from)
4481 binder_thread_dec_tmpref(t_from);
Martijn Coenen3217ccc2017-08-24 15:23:36 +02004482
4483 binder_cleanup_transaction(t, "put_user failed",
4484 BR_FAILED_REPLY);
4485
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004486 return -EFAULT;
Todd Kjos2f993e22017-05-12 14:42:55 -07004487 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004488 ptr += sizeof(uint32_t);
Todd Kjos63e0afa2019-01-14 09:10:21 -08004489 if (copy_to_user(ptr, &tr, trsize)) {
Todd Kjos2f993e22017-05-12 14:42:55 -07004490 if (t_from)
4491 binder_thread_dec_tmpref(t_from);
Martijn Coenen3217ccc2017-08-24 15:23:36 +02004492
4493 binder_cleanup_transaction(t, "copy_to_user failed",
4494 BR_FAILED_REPLY);
4495
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004496 return -EFAULT;
Todd Kjos2f993e22017-05-12 14:42:55 -07004497 }
Todd Kjos63e0afa2019-01-14 09:10:21 -08004498 ptr += trsize;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004499
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004500 trace_binder_transaction_received(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004501 binder_stat_br(proc, thread, cmd);
4502 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004503 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004504 proc->pid, thread->pid,
4505 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
Todd Kjos63e0afa2019-01-14 09:10:21 -08004506 (cmd == BR_TRANSACTION_SEC_CTX) ?
4507 "BR_TRANSACTION_SEC_CTX" : "BR_REPLY",
Todd Kjos2f993e22017-05-12 14:42:55 -07004508 t->debug_id, t_from ? t_from->proc->pid : 0,
4509 t_from ? t_from->pid : 0, cmd,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004510 t->buffer->data_size, t->buffer->offsets_size,
Todd Kjos63e0afa2019-01-14 09:10:21 -08004511 (u64)trd->data.ptr.buffer,
4512 (u64)trd->data.ptr.offsets);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004513
Todd Kjos2f993e22017-05-12 14:42:55 -07004514 if (t_from)
4515 binder_thread_dec_tmpref(t_from);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004516 t->buffer->allow_user_free = 1;
Todd Kjos63e0afa2019-01-14 09:10:21 -08004517 if (cmd != BR_REPLY && !(t->flags & TF_ONE_WAY)) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07004518 binder_inner_proc_lock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004519 t->to_parent = thread->transaction_stack;
4520 t->to_thread = thread;
4521 thread->transaction_stack = t;
Martijn Coenen995a36e2017-06-02 13:36:52 -07004522 binder_inner_proc_unlock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004523 } else {
Todd Kjos21ef40a2017-03-30 18:02:13 -07004524 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004525 }
4526 break;
4527 }
4528
4529done:
4530
4531 *consumed = ptr - buffer;
Todd Kjosd600e902017-05-25 17:35:02 -07004532 binder_inner_proc_lock(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004533 if (proc->requested_threads == 0 &&
4534 list_empty(&thread->proc->waiting_threads) &&
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004535 proc->requested_threads_started < proc->max_threads &&
4536 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4537 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
4538 /*spawn a new thread if we leave this out */) {
4539 proc->requested_threads++;
Todd Kjosd600e902017-05-25 17:35:02 -07004540 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004541 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304542 "%d:%d BR_SPAWN_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004543 proc->pid, thread->pid);
4544 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
4545 return -EFAULT;
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07004546 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
Todd Kjosd600e902017-05-25 17:35:02 -07004547 } else
4548 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004549 return 0;
4550}
4551
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004552static void binder_release_work(struct binder_proc *proc,
4553 struct list_head *list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004554{
4555 struct binder_work *w;
Seunghun Lee10f62862014-05-01 01:30:23 +09004556
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004557 while (1) {
4558 w = binder_dequeue_work_head(proc, list);
4559 if (!w)
4560 return;
4561
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004562 switch (w->type) {
4563 case BINDER_WORK_TRANSACTION: {
4564 struct binder_transaction *t;
4565
4566 t = container_of(w, struct binder_transaction, work);
Martijn Coenen3217ccc2017-08-24 15:23:36 +02004567
4568 binder_cleanup_transaction(t, "process died.",
4569 BR_DEAD_REPLY);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004570 } break;
Todd Kjos858b8da2017-04-21 17:35:12 -07004571 case BINDER_WORK_RETURN_ERROR: {
4572 struct binder_error *e = container_of(
4573 w, struct binder_error, work);
4574
4575 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4576 "undelivered TRANSACTION_ERROR: %u\n",
4577 e->cmd);
4578 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004579 case BINDER_WORK_TRANSACTION_COMPLETE: {
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004580 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304581 "undelivered TRANSACTION_COMPLETE\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004582 kfree(w);
4583 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4584 } break;
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004585 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4586 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4587 struct binder_ref_death *death;
4588
4589 death = container_of(w, struct binder_ref_death, work);
4590 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004591 "undelivered death notification, %016llx\n",
4592 (u64)death->cookie);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004593 kfree(death);
4594 binder_stats_deleted(BINDER_STAT_DEATH);
4595 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004596 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304597 pr_err("unexpected work type, %d, not freed\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004598 w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004599 break;
4600 }
4601 }
4602
4603}
4604
Todd Kjosb4827902017-05-25 15:52:17 -07004605static struct binder_thread *binder_get_thread_ilocked(
4606 struct binder_proc *proc, struct binder_thread *new_thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004607{
4608 struct binder_thread *thread = NULL;
4609 struct rb_node *parent = NULL;
4610 struct rb_node **p = &proc->threads.rb_node;
4611
4612 while (*p) {
4613 parent = *p;
4614 thread = rb_entry(parent, struct binder_thread, rb_node);
4615
4616 if (current->pid < thread->pid)
4617 p = &(*p)->rb_left;
4618 else if (current->pid > thread->pid)
4619 p = &(*p)->rb_right;
4620 else
Todd Kjosb4827902017-05-25 15:52:17 -07004621 return thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004622 }
Todd Kjosb4827902017-05-25 15:52:17 -07004623 if (!new_thread)
4624 return NULL;
4625 thread = new_thread;
4626 binder_stats_created(BINDER_STAT_THREAD);
4627 thread->proc = proc;
4628 thread->pid = current->pid;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004629 get_task_struct(current);
4630 thread->task = current;
Todd Kjosb4827902017-05-25 15:52:17 -07004631 atomic_set(&thread->tmp_ref, 0);
4632 init_waitqueue_head(&thread->wait);
4633 INIT_LIST_HEAD(&thread->todo);
4634 rb_link_node(&thread->rb_node, parent, p);
4635 rb_insert_color(&thread->rb_node, &proc->threads);
4636 thread->looper_need_return = true;
4637 thread->return_error.work.type = BINDER_WORK_RETURN_ERROR;
4638 thread->return_error.cmd = BR_OK;
4639 thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR;
4640 thread->reply_error.cmd = BR_OK;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004641 INIT_LIST_HEAD(&new_thread->waiting_thread_node);
Todd Kjosb4827902017-05-25 15:52:17 -07004642 return thread;
4643}
4644
4645static struct binder_thread *binder_get_thread(struct binder_proc *proc)
4646{
4647 struct binder_thread *thread;
4648 struct binder_thread *new_thread;
4649
4650 binder_inner_proc_lock(proc);
4651 thread = binder_get_thread_ilocked(proc, NULL);
4652 binder_inner_proc_unlock(proc);
4653 if (!thread) {
4654 new_thread = kzalloc(sizeof(*thread), GFP_KERNEL);
4655 if (new_thread == NULL)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004656 return NULL;
Todd Kjosb4827902017-05-25 15:52:17 -07004657 binder_inner_proc_lock(proc);
4658 thread = binder_get_thread_ilocked(proc, new_thread);
4659 binder_inner_proc_unlock(proc);
4660 if (thread != new_thread)
4661 kfree(new_thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004662 }
4663 return thread;
4664}
4665
Todd Kjos2f993e22017-05-12 14:42:55 -07004666static void binder_free_proc(struct binder_proc *proc)
4667{
4668 BUG_ON(!list_empty(&proc->todo));
4669 BUG_ON(!list_empty(&proc->delivered_death));
4670 binder_alloc_deferred_release(&proc->alloc);
4671 put_task_struct(proc->tsk);
4672 binder_stats_deleted(BINDER_STAT_PROC);
4673 kfree(proc);
4674}
4675
4676static void binder_free_thread(struct binder_thread *thread)
4677{
4678 BUG_ON(!list_empty(&thread->todo));
4679 binder_stats_deleted(BINDER_STAT_THREAD);
4680 binder_proc_dec_tmpref(thread->proc);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004681 put_task_struct(thread->task);
Todd Kjos2f993e22017-05-12 14:42:55 -07004682 kfree(thread);
4683}
4684
4685static int binder_thread_release(struct binder_proc *proc,
4686 struct binder_thread *thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004687{
4688 struct binder_transaction *t;
4689 struct binder_transaction *send_reply = NULL;
4690 int active_transactions = 0;
Todd Kjos2f993e22017-05-12 14:42:55 -07004691 struct binder_transaction *last_t = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004692
Todd Kjosb4827902017-05-25 15:52:17 -07004693 binder_inner_proc_lock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07004694 /*
4695 * take a ref on the proc so it survives
4696 * after we remove this thread from proc->threads.
4697 * The corresponding dec is when we actually
4698 * free the thread in binder_free_thread()
4699 */
4700 proc->tmp_ref++;
4701 /*
4702 * take a ref on this thread to ensure it
4703 * survives while we are releasing it
4704 */
4705 atomic_inc(&thread->tmp_ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004706 rb_erase(&thread->rb_node, &proc->threads);
4707 t = thread->transaction_stack;
Todd Kjos2f993e22017-05-12 14:42:55 -07004708 if (t) {
4709 spin_lock(&t->lock);
4710 if (t->to_thread == thread)
4711 send_reply = t;
4712 }
4713 thread->is_dead = true;
4714
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004715 while (t) {
Todd Kjos2f993e22017-05-12 14:42:55 -07004716 last_t = t;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004717 active_transactions++;
4718 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304719 "release %d:%d transaction %d %s, still active\n",
4720 proc->pid, thread->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004721 t->debug_id,
4722 (t->to_thread == thread) ? "in" : "out");
4723
4724 if (t->to_thread == thread) {
4725 t->to_proc = NULL;
4726 t->to_thread = NULL;
4727 if (t->buffer) {
4728 t->buffer->transaction = NULL;
4729 t->buffer = NULL;
4730 }
4731 t = t->to_parent;
4732 } else if (t->from == thread) {
4733 t->from = NULL;
4734 t = t->from_parent;
4735 } else
4736 BUG();
Todd Kjos2f993e22017-05-12 14:42:55 -07004737 spin_unlock(&last_t->lock);
4738 if (t)
4739 spin_lock(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004740 }
Martijn Coenen550c01d2018-01-05 11:27:07 +01004741
4742 /*
4743 * If this thread used poll, make sure we remove the waitqueue
4744 * from any epoll data structures holding it with POLLFREE.
4745 * waitqueue_active() is safe to use here because we're holding
4746 * the inner lock.
4747 */
4748 if ((thread->looper & BINDER_LOOPER_STATE_POLL) &&
4749 waitqueue_active(&thread->wait)) {
4750 wake_up_poll(&thread->wait, POLLHUP | POLLFREE);
4751 }
4752
Todd Kjosb4827902017-05-25 15:52:17 -07004753 binder_inner_proc_unlock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07004754
Martijn Coenen72766d72018-02-16 09:47:15 +01004755 /*
4756 * This is needed to avoid races between wake_up_poll() above and
4757 * and ep_remove_waitqueue() called for other reasons (eg the epoll file
4758 * descriptor being closed); ep_remove_waitqueue() holds an RCU read
4759 * lock, so we can be sure it's done after calling synchronize_rcu().
4760 */
4761 if (thread->looper & BINDER_LOOPER_STATE_POLL)
4762 synchronize_rcu();
4763
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004764 if (send_reply)
4765 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004766 binder_release_work(proc, &thread->todo);
Todd Kjos2f993e22017-05-12 14:42:55 -07004767 binder_thread_dec_tmpref(thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004768 return active_transactions;
4769}
4770
4771static unsigned int binder_poll(struct file *filp,
4772 struct poll_table_struct *wait)
4773{
4774 struct binder_proc *proc = filp->private_data;
4775 struct binder_thread *thread = NULL;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004776 bool wait_for_proc_work;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004777
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004778 thread = binder_get_thread(proc);
Greg Kroah-Hartman6e463bb2018-02-28 17:17:14 +01004779 if (!thread)
Eric Biggers4be5a282018-01-30 23:11:24 -08004780 return POLLERR;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004781
Martijn Coenen995a36e2017-06-02 13:36:52 -07004782 binder_inner_proc_lock(thread->proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004783 thread->looper |= BINDER_LOOPER_STATE_POLL;
4784 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
4785
Martijn Coenen995a36e2017-06-02 13:36:52 -07004786 binder_inner_proc_unlock(thread->proc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004787
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004788 poll_wait(filp, &thread->wait, wait);
4789
Martijn Coenen47810932017-08-10 12:32:00 +02004790 if (binder_has_work(thread, wait_for_proc_work))
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004791 return POLLIN;
4792
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004793 return 0;
4794}
4795
Tair Rzayev78260ac2014-06-03 22:27:21 +03004796static int binder_ioctl_write_read(struct file *filp,
4797 unsigned int cmd, unsigned long arg,
4798 struct binder_thread *thread)
4799{
4800 int ret = 0;
4801 struct binder_proc *proc = filp->private_data;
4802 unsigned int size = _IOC_SIZE(cmd);
4803 void __user *ubuf = (void __user *)arg;
4804 struct binder_write_read bwr;
4805
4806 if (size != sizeof(struct binder_write_read)) {
4807 ret = -EINVAL;
4808 goto out;
4809 }
4810 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
4811 ret = -EFAULT;
4812 goto out;
4813 }
4814 binder_debug(BINDER_DEBUG_READ_WRITE,
4815 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
4816 proc->pid, thread->pid,
4817 (u64)bwr.write_size, (u64)bwr.write_buffer,
4818 (u64)bwr.read_size, (u64)bwr.read_buffer);
4819
4820 if (bwr.write_size > 0) {
4821 ret = binder_thread_write(proc, thread,
4822 bwr.write_buffer,
4823 bwr.write_size,
4824 &bwr.write_consumed);
4825 trace_binder_write_done(ret);
4826 if (ret < 0) {
4827 bwr.read_consumed = 0;
4828 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4829 ret = -EFAULT;
4830 goto out;
4831 }
4832 }
4833 if (bwr.read_size > 0) {
4834 ret = binder_thread_read(proc, thread, bwr.read_buffer,
4835 bwr.read_size,
4836 &bwr.read_consumed,
4837 filp->f_flags & O_NONBLOCK);
4838 trace_binder_read_done(ret);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004839 binder_inner_proc_lock(proc);
4840 if (!binder_worklist_empty_ilocked(&proc->todo))
Martijn Coenen053be422017-06-06 15:17:46 -07004841 binder_wakeup_proc_ilocked(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004842 binder_inner_proc_unlock(proc);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004843 if (ret < 0) {
4844 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4845 ret = -EFAULT;
4846 goto out;
4847 }
4848 }
4849 binder_debug(BINDER_DEBUG_READ_WRITE,
4850 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
4851 proc->pid, thread->pid,
4852 (u64)bwr.write_consumed, (u64)bwr.write_size,
4853 (u64)bwr.read_consumed, (u64)bwr.read_size);
4854 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
4855 ret = -EFAULT;
4856 goto out;
4857 }
4858out:
4859 return ret;
4860}
4861
Todd Kjos63e0afa2019-01-14 09:10:21 -08004862static int binder_ioctl_set_ctx_mgr(struct file *filp,
4863 struct flat_binder_object *fbo)
Tair Rzayev78260ac2014-06-03 22:27:21 +03004864{
4865 int ret = 0;
4866 struct binder_proc *proc = filp->private_data;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004867 struct binder_context *context = proc->context;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004868 struct binder_node *new_node;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004869 kuid_t curr_euid = current_euid();
4870
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004871 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004872 if (context->binder_context_mgr_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004873 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
4874 ret = -EBUSY;
4875 goto out;
4876 }
Stephen Smalley79af7302015-01-21 10:54:10 -05004877 ret = security_binder_set_context_mgr(proc->tsk);
4878 if (ret < 0)
4879 goto out;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004880 if (uid_valid(context->binder_context_mgr_uid)) {
4881 if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004882 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
4883 from_kuid(&init_user_ns, curr_euid),
4884 from_kuid(&init_user_ns,
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004885 context->binder_context_mgr_uid));
Tair Rzayev78260ac2014-06-03 22:27:21 +03004886 ret = -EPERM;
4887 goto out;
4888 }
4889 } else {
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004890 context->binder_context_mgr_uid = curr_euid;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004891 }
Todd Kjos63e0afa2019-01-14 09:10:21 -08004892 new_node = binder_new_node(proc, fbo);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004893 if (!new_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004894 ret = -ENOMEM;
4895 goto out;
4896 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004897 binder_node_lock(new_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004898 new_node->local_weak_refs++;
4899 new_node->local_strong_refs++;
4900 new_node->has_strong_ref = 1;
4901 new_node->has_weak_ref = 1;
4902 context->binder_context_mgr_node = new_node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004903 binder_node_unlock(new_node);
Todd Kjosf22abc72017-05-09 11:08:05 -07004904 binder_put_node(new_node);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004905out:
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004906 mutex_unlock(&context->context_mgr_node_lock);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004907 return ret;
4908}
4909
Martijn Coenen1c57ba42018-08-25 13:50:56 -07004910static int binder_ioctl_get_node_info_for_ref(struct binder_proc *proc,
4911 struct binder_node_info_for_ref *info)
4912{
4913 struct binder_node *node;
4914 struct binder_context *context = proc->context;
4915 __u32 handle = info->handle;
4916
4917 if (info->strong_count || info->weak_count || info->reserved1 ||
4918 info->reserved2 || info->reserved3) {
4919 binder_user_error("%d BINDER_GET_NODE_INFO_FOR_REF: only handle may be non-zero.",
4920 proc->pid);
4921 return -EINVAL;
4922 }
4923
4924 /* This ioctl may only be used by the context manager */
4925 mutex_lock(&context->context_mgr_node_lock);
4926 if (!context->binder_context_mgr_node ||
4927 context->binder_context_mgr_node->proc != proc) {
4928 mutex_unlock(&context->context_mgr_node_lock);
4929 return -EPERM;
4930 }
4931 mutex_unlock(&context->context_mgr_node_lock);
4932
4933 node = binder_get_node_from_ref(proc, handle, true, NULL);
4934 if (!node)
4935 return -EINVAL;
4936
4937 info->strong_count = node->local_strong_refs +
4938 node->internal_strong_refs;
4939 info->weak_count = node->local_weak_refs;
4940
4941 binder_put_node(node);
4942
4943 return 0;
4944}
4945
Colin Cross833babb32017-06-20 13:54:44 -07004946static int binder_ioctl_get_node_debug_info(struct binder_proc *proc,
4947 struct binder_node_debug_info *info) {
4948 struct rb_node *n;
4949 binder_uintptr_t ptr = info->ptr;
4950
4951 memset(info, 0, sizeof(*info));
4952
4953 binder_inner_proc_lock(proc);
4954 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
4955 struct binder_node *node = rb_entry(n, struct binder_node,
4956 rb_node);
4957 if (node->ptr > ptr) {
4958 info->ptr = node->ptr;
4959 info->cookie = node->cookie;
4960 info->has_strong_ref = node->has_strong_ref;
4961 info->has_weak_ref = node->has_weak_ref;
4962 break;
4963 }
4964 }
4965 binder_inner_proc_unlock(proc);
4966
4967 return 0;
4968}
4969
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004970static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4971{
4972 int ret;
4973 struct binder_proc *proc = filp->private_data;
4974 struct binder_thread *thread;
4975 unsigned int size = _IOC_SIZE(cmd);
4976 void __user *ubuf = (void __user *)arg;
4977
Tair Rzayev78260ac2014-06-03 22:27:21 +03004978 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
4979 proc->pid, current->pid, cmd, arg);*/
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004980
Sherry Yang435416b2017-06-22 14:37:45 -07004981 binder_selftest_alloc(&proc->alloc);
4982
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004983 trace_binder_ioctl(cmd, arg);
4984
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004985 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4986 if (ret)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004987 goto err_unlocked;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004988
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004989 thread = binder_get_thread(proc);
4990 if (thread == NULL) {
4991 ret = -ENOMEM;
4992 goto err;
4993 }
4994
4995 switch (cmd) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004996 case BINDER_WRITE_READ:
4997 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
4998 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004999 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005000 break;
Todd Kjosd600e902017-05-25 17:35:02 -07005001 case BINDER_SET_MAX_THREADS: {
5002 int max_threads;
5003
5004 if (copy_from_user(&max_threads, ubuf,
5005 sizeof(max_threads))) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005006 ret = -EINVAL;
5007 goto err;
5008 }
Todd Kjosd600e902017-05-25 17:35:02 -07005009 binder_inner_proc_lock(proc);
5010 proc->max_threads = max_threads;
5011 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005012 break;
Todd Kjosd600e902017-05-25 17:35:02 -07005013 }
Todd Kjos63e0afa2019-01-14 09:10:21 -08005014 case BINDER_SET_CONTEXT_MGR_EXT: {
5015 struct flat_binder_object fbo;
5016
5017 if (copy_from_user(&fbo, ubuf, sizeof(fbo))) {
5018 ret = -EINVAL;
5019 goto err;
5020 }
5021 ret = binder_ioctl_set_ctx_mgr(filp, &fbo);
5022 if (ret)
5023 goto err;
5024 break;
5025 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005026 case BINDER_SET_CONTEXT_MGR:
Todd Kjos63e0afa2019-01-14 09:10:21 -08005027 ret = binder_ioctl_set_ctx_mgr(filp, NULL);
Tair Rzayev78260ac2014-06-03 22:27:21 +03005028 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005029 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005030 break;
5031 case BINDER_THREAD_EXIT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05305032 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005033 proc->pid, thread->pid);
Todd Kjos2f993e22017-05-12 14:42:55 -07005034 binder_thread_release(proc, thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005035 thread = NULL;
5036 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02005037 case BINDER_VERSION: {
5038 struct binder_version __user *ver = ubuf;
5039
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005040 if (size != sizeof(struct binder_version)) {
5041 ret = -EINVAL;
5042 goto err;
5043 }
Mathieu Maret36c89c02014-04-15 12:03:05 +02005044 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
5045 &ver->protocol_version)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005046 ret = -EINVAL;
5047 goto err;
5048 }
5049 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02005050 }
Martijn Coenen1c57ba42018-08-25 13:50:56 -07005051 case BINDER_GET_NODE_INFO_FOR_REF: {
5052 struct binder_node_info_for_ref info;
5053
5054 if (copy_from_user(&info, ubuf, sizeof(info))) {
5055 ret = -EFAULT;
5056 goto err;
5057 }
5058
5059 ret = binder_ioctl_get_node_info_for_ref(proc, &info);
5060 if (ret < 0)
5061 goto err;
5062
5063 if (copy_to_user(ubuf, &info, sizeof(info))) {
5064 ret = -EFAULT;
5065 goto err;
5066 }
5067
5068 break;
5069 }
Colin Cross833babb32017-06-20 13:54:44 -07005070 case BINDER_GET_NODE_DEBUG_INFO: {
5071 struct binder_node_debug_info info;
5072
5073 if (copy_from_user(&info, ubuf, sizeof(info))) {
5074 ret = -EFAULT;
5075 goto err;
5076 }
5077
5078 ret = binder_ioctl_get_node_debug_info(proc, &info);
5079 if (ret < 0)
5080 goto err;
5081
5082 if (copy_to_user(ubuf, &info, sizeof(info))) {
5083 ret = -EFAULT;
5084 goto err;
5085 }
5086 break;
5087 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005088 default:
5089 ret = -EINVAL;
5090 goto err;
5091 }
5092 ret = 0;
5093err:
5094 if (thread)
Todd Kjos6798e6d2017-01-06 14:19:25 -08005095 thread->looper_need_return = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005096 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
5097 if (ret && ret != -ERESTARTSYS)
Anmol Sarma56b468f2012-10-30 22:35:43 +05305098 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 -07005099err_unlocked:
5100 trace_binder_ioctl_done(ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005101 return ret;
5102}
5103
5104static void binder_vma_open(struct vm_area_struct *vma)
5105{
5106 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09005107
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005108 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05305109 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005110 proc->pid, vma->vm_start, vma->vm_end,
5111 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
5112 (unsigned long)pgprot_val(vma->vm_page_prot));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005113}
5114
5115static void binder_vma_close(struct vm_area_struct *vma)
5116{
5117 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09005118
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005119 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05305120 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005121 proc->pid, vma->vm_start, vma->vm_end,
5122 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
5123 (unsigned long)pgprot_val(vma->vm_page_prot));
Todd Kjosd325d372016-10-10 10:40:53 -07005124 binder_alloc_vma_close(&proc->alloc);
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005125 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005126}
5127
Vinayak Menonddac7d52014-06-02 18:17:59 +05305128static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
5129{
5130 return VM_FAULT_SIGBUS;
5131}
5132
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07005133static const struct vm_operations_struct binder_vm_ops = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005134 .open = binder_vma_open,
5135 .close = binder_vma_close,
Vinayak Menonddac7d52014-06-02 18:17:59 +05305136 .fault = binder_vm_fault,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005137};
5138
Todd Kjosd325d372016-10-10 10:40:53 -07005139static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
5140{
5141 int ret;
5142 struct binder_proc *proc = filp->private_data;
5143 const char *failure_string;
5144
5145 if (proc->tsk != current->group_leader)
5146 return -EINVAL;
5147
5148 if ((vma->vm_end - vma->vm_start) > SZ_4M)
5149 vma->vm_end = vma->vm_start + SZ_4M;
5150
5151 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5152 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
5153 __func__, proc->pid, vma->vm_start, vma->vm_end,
5154 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
5155 (unsigned long)pgprot_val(vma->vm_page_prot));
5156
5157 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
5158 ret = -EPERM;
5159 failure_string = "bad vm_flags";
5160 goto err_bad_arg;
5161 }
Minchan Kim2cafd5b2018-05-07 23:15:37 +09005162 vma->vm_flags |= VM_DONTCOPY | VM_MIXEDMAP;
5163 vma->vm_flags &= ~VM_MAYWRITE;
5164
Todd Kjosd325d372016-10-10 10:40:53 -07005165 vma->vm_ops = &binder_vm_ops;
5166 vma->vm_private_data = proc;
5167
5168 ret = binder_alloc_mmap_handler(&proc->alloc, vma);
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005169 if (ret)
5170 return ret;
Todd Kjosfbb43392017-11-27 09:32:33 -08005171 mutex_lock(&proc->files_lock);
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005172 proc->files = get_files_struct(current);
Todd Kjosfbb43392017-11-27 09:32:33 -08005173 mutex_unlock(&proc->files_lock);
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005174 return 0;
Todd Kjosd325d372016-10-10 10:40:53 -07005175
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005176err_bad_arg:
Elad Wexler6b646402017-12-29 11:03:37 +02005177 pr_err("%s: %d %lx-%lx %s failed %d\n", __func__,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005178 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
5179 return ret;
5180}
5181
5182static int binder_open(struct inode *nodp, struct file *filp)
5183{
5184 struct binder_proc *proc;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005185 struct binder_device *binder_dev;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005186
Elad Wexler6b646402017-12-29 11:03:37 +02005187 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "%s: %d:%d\n", __func__,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005188 current->group_leader->pid, current->pid);
5189
5190 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
5191 if (proc == NULL)
5192 return -ENOMEM;
Todd Kjosfc7a7e22017-05-29 16:44:24 -07005193 spin_lock_init(&proc->inner_lock);
5194 spin_lock_init(&proc->outer_lock);
Martijn Coenen872c26e2017-03-07 15:51:18 +01005195 get_task_struct(current->group_leader);
5196 proc->tsk = current->group_leader;
Todd Kjosfbb43392017-11-27 09:32:33 -08005197 mutex_init(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005198 INIT_LIST_HEAD(&proc->todo);
Martijn Coenen57b2ac62017-06-06 17:04:42 -07005199 if (binder_supported_policy(current->policy)) {
5200 proc->default_priority.sched_policy = current->policy;
5201 proc->default_priority.prio = current->normal_prio;
5202 } else {
5203 proc->default_priority.sched_policy = SCHED_NORMAL;
5204 proc->default_priority.prio = NICE_TO_PRIO(0);
5205 }
5206
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005207 binder_dev = container_of(filp->private_data, struct binder_device,
5208 miscdev);
5209 proc->context = &binder_dev->context;
Todd Kjosd325d372016-10-10 10:40:53 -07005210 binder_alloc_init(&proc->alloc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07005211
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005212 binder_stats_created(BINDER_STAT_PROC);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005213 proc->pid = current->group_leader->pid;
5214 INIT_LIST_HEAD(&proc->delivered_death);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005215 INIT_LIST_HEAD(&proc->waiting_threads);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005216 filp->private_data = proc;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07005217
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005218 mutex_lock(&binder_procs_lock);
5219 hlist_add_head(&proc->proc_node, &binder_procs);
5220 mutex_unlock(&binder_procs_lock);
5221
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005222 if (binder_debugfs_dir_entry_proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005223 char strbuf[11];
Seunghun Lee10f62862014-05-01 01:30:23 +09005224
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005225 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005226 /*
5227 * proc debug entries are shared between contexts, so
5228 * this will fail if the process tries to open the driver
5229 * again with a different context. The priting code will
5230 * anyway print all contexts that a given PID has, so this
5231 * is not a problem.
5232 */
Harsh Shandilya174562a2017-12-22 19:37:02 +05305233 proc->debugfs_entry = debugfs_create_file(strbuf, 0444,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005234 binder_debugfs_dir_entry_proc,
5235 (void *)(unsigned long)proc->pid,
5236 &binder_proc_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005237 }
5238
5239 return 0;
5240}
5241
5242static int binder_flush(struct file *filp, fl_owner_t id)
5243{
5244 struct binder_proc *proc = filp->private_data;
5245
5246 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
5247
5248 return 0;
5249}
5250
5251static void binder_deferred_flush(struct binder_proc *proc)
5252{
5253 struct rb_node *n;
5254 int wake_count = 0;
Seunghun Lee10f62862014-05-01 01:30:23 +09005255
Todd Kjosb4827902017-05-25 15:52:17 -07005256 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005257 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
5258 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
Seunghun Lee10f62862014-05-01 01:30:23 +09005259
Todd Kjos6798e6d2017-01-06 14:19:25 -08005260 thread->looper_need_return = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005261 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
5262 wake_up_interruptible(&thread->wait);
5263 wake_count++;
5264 }
5265 }
Todd Kjosb4827902017-05-25 15:52:17 -07005266 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005267
5268 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5269 "binder_flush: %d woke %d threads\n", proc->pid,
5270 wake_count);
5271}
5272
5273static int binder_release(struct inode *nodp, struct file *filp)
5274{
5275 struct binder_proc *proc = filp->private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09005276
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005277 debugfs_remove(proc->debugfs_entry);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005278 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
5279
5280 return 0;
5281}
5282
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005283static int binder_node_release(struct binder_node *node, int refs)
5284{
5285 struct binder_ref *ref;
5286 int death = 0;
Todd Kjose7f23ed2017-03-21 13:06:01 -07005287 struct binder_proc *proc = node->proc;
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005288
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005289 binder_release_work(proc, &node->async_todo);
Todd Kjose7f23ed2017-03-21 13:06:01 -07005290
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005291 binder_node_lock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07005292 binder_inner_proc_lock(proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005293 binder_dequeue_work_ilocked(&node->work);
Todd Kjosf22abc72017-05-09 11:08:05 -07005294 /*
5295 * The caller must have taken a temporary ref on the node,
5296 */
5297 BUG_ON(!node->tmp_refs);
5298 if (hlist_empty(&node->refs) && node->tmp_refs == 1) {
Todd Kjose7f23ed2017-03-21 13:06:01 -07005299 binder_inner_proc_unlock(proc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005300 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07005301 binder_free_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005302
5303 return refs;
5304 }
5305
5306 node->proc = NULL;
5307 node->local_strong_refs = 0;
5308 node->local_weak_refs = 0;
Todd Kjose7f23ed2017-03-21 13:06:01 -07005309 binder_inner_proc_unlock(proc);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005310
5311 spin_lock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005312 hlist_add_head(&node->dead_node, &binder_dead_nodes);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005313 spin_unlock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005314
5315 hlist_for_each_entry(ref, &node->refs, node_entry) {
5316 refs++;
Martijn Coenenf9eac642017-05-22 11:26:23 -07005317 /*
5318 * Need the node lock to synchronize
5319 * with new notification requests and the
5320 * inner lock to synchronize with queued
5321 * death notifications.
5322 */
5323 binder_inner_proc_lock(ref->proc);
5324 if (!ref->death) {
5325 binder_inner_proc_unlock(ref->proc);
Arve Hjønnevåge194fd82014-02-17 13:58:29 -08005326 continue;
Martijn Coenenf9eac642017-05-22 11:26:23 -07005327 }
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005328
5329 death++;
5330
Martijn Coenenf9eac642017-05-22 11:26:23 -07005331 BUG_ON(!list_empty(&ref->death->work.entry));
5332 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
5333 binder_enqueue_work_ilocked(&ref->death->work,
5334 &ref->proc->todo);
Martijn Coenen053be422017-06-06 15:17:46 -07005335 binder_wakeup_proc_ilocked(ref->proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005336 binder_inner_proc_unlock(ref->proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005337 }
5338
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005339 binder_debug(BINDER_DEBUG_DEAD_BINDER,
5340 "node %d now dead, refs %d, death %d\n",
5341 node->debug_id, refs, death);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005342 binder_node_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07005343 binder_put_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005344
5345 return refs;
5346}
5347
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005348static void binder_deferred_release(struct binder_proc *proc)
5349{
Martijn Coenen0b3311e2016-09-30 15:51:48 +02005350 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005351 struct rb_node *n;
Todd Kjosd325d372016-10-10 10:40:53 -07005352 int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005353
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005354 BUG_ON(proc->files);
5355
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005356 mutex_lock(&binder_procs_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005357 hlist_del(&proc->proc_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005358 mutex_unlock(&binder_procs_lock);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005359
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005360 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02005361 if (context->binder_context_mgr_node &&
5362 context->binder_context_mgr_node->proc == proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005363 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01005364 "%s: %d context_mgr_node gone\n",
5365 __func__, proc->pid);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02005366 context->binder_context_mgr_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005367 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005368 mutex_unlock(&context->context_mgr_node_lock);
Todd Kjosb4827902017-05-25 15:52:17 -07005369 binder_inner_proc_lock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07005370 /*
5371 * Make sure proc stays alive after we
5372 * remove all the threads
5373 */
5374 proc->tmp_ref++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005375
Todd Kjos2f993e22017-05-12 14:42:55 -07005376 proc->is_dead = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005377 threads = 0;
5378 active_transactions = 0;
5379 while ((n = rb_first(&proc->threads))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005380 struct binder_thread *thread;
5381
5382 thread = rb_entry(n, struct binder_thread, rb_node);
Todd Kjosb4827902017-05-25 15:52:17 -07005383 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005384 threads++;
Todd Kjos2f993e22017-05-12 14:42:55 -07005385 active_transactions += binder_thread_release(proc, thread);
Todd Kjosb4827902017-05-25 15:52:17 -07005386 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005387 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005388
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005389 nodes = 0;
5390 incoming_refs = 0;
5391 while ((n = rb_first(&proc->nodes))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005392 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005393
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005394 node = rb_entry(n, struct binder_node, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005395 nodes++;
Todd Kjosf22abc72017-05-09 11:08:05 -07005396 /*
5397 * take a temporary ref on the node before
5398 * calling binder_node_release() which will either
5399 * kfree() the node or call binder_put_node()
5400 */
Todd Kjos425d23f2017-06-12 12:07:26 -07005401 binder_inc_node_tmpref_ilocked(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005402 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjos425d23f2017-06-12 12:07:26 -07005403 binder_inner_proc_unlock(proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005404 incoming_refs = binder_node_release(node, incoming_refs);
Todd Kjos425d23f2017-06-12 12:07:26 -07005405 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005406 }
Todd Kjos425d23f2017-06-12 12:07:26 -07005407 binder_inner_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005408
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005409 outgoing_refs = 0;
Todd Kjos5346bf32016-10-20 16:43:34 -07005410 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005411 while ((n = rb_first(&proc->refs_by_desc))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005412 struct binder_ref *ref;
5413
5414 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005415 outgoing_refs++;
Todd Kjos5346bf32016-10-20 16:43:34 -07005416 binder_cleanup_ref_olocked(ref);
5417 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07005418 binder_free_ref(ref);
Todd Kjos5346bf32016-10-20 16:43:34 -07005419 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005420 }
Todd Kjos5346bf32016-10-20 16:43:34 -07005421 binder_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005422
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005423 binder_release_work(proc, &proc->todo);
5424 binder_release_work(proc, &proc->delivered_death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005425
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005426 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Todd Kjosd325d372016-10-10 10:40:53 -07005427 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01005428 __func__, proc->pid, threads, nodes, incoming_refs,
Todd Kjosd325d372016-10-10 10:40:53 -07005429 outgoing_refs, active_transactions);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005430
Todd Kjos2f993e22017-05-12 14:42:55 -07005431 binder_proc_dec_tmpref(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005432}
5433
5434static void binder_deferred_func(struct work_struct *work)
5435{
5436 struct binder_proc *proc;
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005437 struct files_struct *files;
5438
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005439 int defer;
Seunghun Lee10f62862014-05-01 01:30:23 +09005440
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005441 do {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005442 mutex_lock(&binder_deferred_lock);
5443 if (!hlist_empty(&binder_deferred_list)) {
5444 proc = hlist_entry(binder_deferred_list.first,
5445 struct binder_proc, deferred_work_node);
5446 hlist_del_init(&proc->deferred_work_node);
5447 defer = proc->deferred_work;
5448 proc->deferred_work = 0;
5449 } else {
5450 proc = NULL;
5451 defer = 0;
5452 }
5453 mutex_unlock(&binder_deferred_lock);
5454
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005455 files = NULL;
5456 if (defer & BINDER_DEFERRED_PUT_FILES) {
Todd Kjosfbb43392017-11-27 09:32:33 -08005457 mutex_lock(&proc->files_lock);
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005458 files = proc->files;
5459 if (files)
5460 proc->files = NULL;
Todd Kjosfbb43392017-11-27 09:32:33 -08005461 mutex_unlock(&proc->files_lock);
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005462 }
5463
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005464 if (defer & BINDER_DEFERRED_FLUSH)
5465 binder_deferred_flush(proc);
5466
5467 if (defer & BINDER_DEFERRED_RELEASE)
5468 binder_deferred_release(proc); /* frees proc */
Martijn Coenen6f7e5f92018-06-15 11:53:36 +02005469
5470 if (files)
5471 put_files_struct(files);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005472 } while (proc);
5473}
5474static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
5475
5476static void
5477binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
5478{
5479 mutex_lock(&binder_deferred_lock);
5480 proc->deferred_work |= defer;
5481 if (hlist_unhashed(&proc->deferred_work_node)) {
5482 hlist_add_head(&proc->deferred_work_node,
5483 &binder_deferred_list);
Bhaktipriya Shridhar1beba522016-08-13 22:16:24 +05305484 schedule_work(&binder_deferred_work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005485 }
5486 mutex_unlock(&binder_deferred_lock);
5487}
5488
Todd Kjos6d241a42017-04-21 14:32:11 -07005489static void print_binder_transaction_ilocked(struct seq_file *m,
5490 struct binder_proc *proc,
5491 const char *prefix,
5492 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005493{
Todd Kjos6d241a42017-04-21 14:32:11 -07005494 struct binder_proc *to_proc;
5495 struct binder_buffer *buffer = t->buffer;
5496
Todd Kjos2f993e22017-05-12 14:42:55 -07005497 spin_lock(&t->lock);
Todd Kjos6d241a42017-04-21 14:32:11 -07005498 to_proc = t->to_proc;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005499 seq_printf(m,
Todd Kjosf540ce02018-02-07 13:57:37 -08005500 "%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 -07005501 prefix, t->debug_id, t,
5502 t->from ? t->from->proc->pid : 0,
5503 t->from ? t->from->pid : 0,
Todd Kjos6d241a42017-04-21 14:32:11 -07005504 to_proc ? to_proc->pid : 0,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005505 t->to_thread ? t->to_thread->pid : 0,
Martijn Coenen57b2ac62017-06-06 17:04:42 -07005506 t->code, t->flags, t->priority.sched_policy,
5507 t->priority.prio, t->need_reply);
Todd Kjos2f993e22017-05-12 14:42:55 -07005508 spin_unlock(&t->lock);
5509
Todd Kjos6d241a42017-04-21 14:32:11 -07005510 if (proc != to_proc) {
5511 /*
5512 * Can only safely deref buffer if we are holding the
5513 * correct proc inner lock for this node
5514 */
5515 seq_puts(m, "\n");
5516 return;
5517 }
5518
5519 if (buffer == NULL) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005520 seq_puts(m, " buffer free\n");
5521 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005522 }
Todd Kjos6d241a42017-04-21 14:32:11 -07005523 if (buffer->target_node)
5524 seq_printf(m, " node %d", buffer->target_node->debug_id);
Todd Kjosf540ce02018-02-07 13:57:37 -08005525 seq_printf(m, " size %zd:%zd data %pK\n",
Todd Kjos6d241a42017-04-21 14:32:11 -07005526 buffer->data_size, buffer->offsets_size,
Todd Kjos8539b1e2019-02-08 10:35:20 -08005527 buffer->user_data);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005528}
5529
Todd Kjos6d241a42017-04-21 14:32:11 -07005530static void print_binder_work_ilocked(struct seq_file *m,
5531 struct binder_proc *proc,
5532 const char *prefix,
5533 const char *transaction_prefix,
5534 struct binder_work *w)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005535{
5536 struct binder_node *node;
5537 struct binder_transaction *t;
5538
5539 switch (w->type) {
5540 case BINDER_WORK_TRANSACTION:
5541 t = container_of(w, struct binder_transaction, work);
Todd Kjos6d241a42017-04-21 14:32:11 -07005542 print_binder_transaction_ilocked(
5543 m, proc, transaction_prefix, t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005544 break;
Todd Kjos858b8da2017-04-21 17:35:12 -07005545 case BINDER_WORK_RETURN_ERROR: {
5546 struct binder_error *e = container_of(
5547 w, struct binder_error, work);
5548
5549 seq_printf(m, "%stransaction error: %u\n",
5550 prefix, e->cmd);
5551 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005552 case BINDER_WORK_TRANSACTION_COMPLETE:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005553 seq_printf(m, "%stransaction complete\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005554 break;
5555 case BINDER_WORK_NODE:
5556 node = container_of(w, struct binder_node, work);
Arve Hjønnevågda498892014-02-21 14:40:26 -08005557 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
5558 prefix, node->debug_id,
5559 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005560 break;
5561 case BINDER_WORK_DEAD_BINDER:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005562 seq_printf(m, "%shas dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005563 break;
5564 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005565 seq_printf(m, "%shas cleared dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005566 break;
5567 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005568 seq_printf(m, "%shas cleared death notification\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005569 break;
5570 default:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005571 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005572 break;
5573 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005574}
5575
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005576static void print_binder_thread_ilocked(struct seq_file *m,
5577 struct binder_thread *thread,
5578 int print_always)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005579{
5580 struct binder_transaction *t;
5581 struct binder_work *w;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005582 size_t start_pos = m->count;
5583 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005584
Todd Kjos2f993e22017-05-12 14:42:55 -07005585 seq_printf(m, " thread %d: l %02x need_return %d tr %d\n",
Todd Kjos6798e6d2017-01-06 14:19:25 -08005586 thread->pid, thread->looper,
Todd Kjos2f993e22017-05-12 14:42:55 -07005587 thread->looper_need_return,
5588 atomic_read(&thread->tmp_ref));
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005589 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005590 t = thread->transaction_stack;
5591 while (t) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005592 if (t->from == thread) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005593 print_binder_transaction_ilocked(m, thread->proc,
5594 " outgoing transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005595 t = t->from_parent;
5596 } else if (t->to_thread == thread) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005597 print_binder_transaction_ilocked(m, thread->proc,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005598 " incoming transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005599 t = t->to_parent;
5600 } else {
Todd Kjos6d241a42017-04-21 14:32:11 -07005601 print_binder_transaction_ilocked(m, thread->proc,
5602 " bad transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005603 t = NULL;
5604 }
5605 }
5606 list_for_each_entry(w, &thread->todo, entry) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005607 print_binder_work_ilocked(m, thread->proc, " ",
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005608 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005609 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005610 if (!print_always && m->count == header_pos)
5611 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005612}
5613
Todd Kjos425d23f2017-06-12 12:07:26 -07005614static void print_binder_node_nilocked(struct seq_file *m,
5615 struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005616{
5617 struct binder_ref *ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005618 struct binder_work *w;
5619 int count;
5620
5621 count = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08005622 hlist_for_each_entry(ref, &node->refs, node_entry)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005623 count++;
5624
Martijn Coenen6aac9792017-06-07 09:29:14 -07005625 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 -08005626 node->debug_id, (u64)node->ptr, (u64)node->cookie,
Martijn Coenen6aac9792017-06-07 09:29:14 -07005627 node->sched_policy, node->min_priority,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005628 node->has_strong_ref, node->has_weak_ref,
5629 node->local_strong_refs, node->local_weak_refs,
Todd Kjosf22abc72017-05-09 11:08:05 -07005630 node->internal_strong_refs, count, node->tmp_refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005631 if (count) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005632 seq_puts(m, " proc");
Sasha Levinb67bfe02013-02-27 17:06:00 -08005633 hlist_for_each_entry(ref, &node->refs, node_entry)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005634 seq_printf(m, " %d", ref->proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005635 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005636 seq_puts(m, "\n");
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005637 if (node->proc) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005638 list_for_each_entry(w, &node->async_todo, entry)
Todd Kjos6d241a42017-04-21 14:32:11 -07005639 print_binder_work_ilocked(m, node->proc, " ",
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005640 " pending async transaction", w);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005641 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005642}
5643
Todd Kjos5346bf32016-10-20 16:43:34 -07005644static void print_binder_ref_olocked(struct seq_file *m,
5645 struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005646{
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005647 binder_node_lock(ref->node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07005648 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
5649 ref->data.debug_id, ref->data.desc,
5650 ref->node->proc ? "" : "dead ",
5651 ref->node->debug_id, ref->data.strong,
5652 ref->data.weak, ref->death);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005653 binder_node_unlock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005654}
5655
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005656static void print_binder_proc(struct seq_file *m,
5657 struct binder_proc *proc, int print_all)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005658{
5659 struct binder_work *w;
5660 struct rb_node *n;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005661 size_t start_pos = m->count;
5662 size_t header_pos;
Todd Kjos425d23f2017-06-12 12:07:26 -07005663 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005664
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005665 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005666 seq_printf(m, "context %s\n", proc->context->name);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005667 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005668
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005669 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005670 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005671 print_binder_thread_ilocked(m, rb_entry(n, struct binder_thread,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005672 rb_node), print_all);
Todd Kjos425d23f2017-06-12 12:07:26 -07005673
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005674 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005675 struct binder_node *node = rb_entry(n, struct binder_node,
5676 rb_node);
Todd Kjosd79aa412018-12-05 15:19:26 -08005677 if (!print_all && !node->has_async_transaction)
5678 continue;
5679
Todd Kjos425d23f2017-06-12 12:07:26 -07005680 /*
5681 * take a temporary reference on the node so it
5682 * survives and isn't removed from the tree
5683 * while we print it.
5684 */
5685 binder_inc_node_tmpref_ilocked(node);
5686 /* Need to drop inner lock to take node lock */
5687 binder_inner_proc_unlock(proc);
5688 if (last_node)
5689 binder_put_node(last_node);
5690 binder_node_inner_lock(node);
5691 print_binder_node_nilocked(m, node);
5692 binder_node_inner_unlock(node);
5693 last_node = node;
5694 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005695 }
Todd Kjos425d23f2017-06-12 12:07:26 -07005696 binder_inner_proc_unlock(proc);
5697 if (last_node)
5698 binder_put_node(last_node);
5699
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005700 if (print_all) {
Todd Kjos5346bf32016-10-20 16:43:34 -07005701 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005702 for (n = rb_first(&proc->refs_by_desc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005703 n != NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005704 n = rb_next(n))
Todd Kjos5346bf32016-10-20 16:43:34 -07005705 print_binder_ref_olocked(m, rb_entry(n,
5706 struct binder_ref,
5707 rb_node_desc));
5708 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005709 }
Todd Kjosd325d372016-10-10 10:40:53 -07005710 binder_alloc_print_allocated(m, &proc->alloc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005711 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005712 list_for_each_entry(w, &proc->todo, entry)
Todd Kjos6d241a42017-04-21 14:32:11 -07005713 print_binder_work_ilocked(m, proc, " ",
5714 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005715 list_for_each_entry(w, &proc->delivered_death, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005716 seq_puts(m, " has delivered dead binder\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005717 break;
5718 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005719 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005720 if (!print_all && m->count == header_pos)
5721 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005722}
5723
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005724static const char * const binder_return_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005725 "BR_ERROR",
5726 "BR_OK",
5727 "BR_TRANSACTION",
5728 "BR_REPLY",
5729 "BR_ACQUIRE_RESULT",
5730 "BR_DEAD_REPLY",
5731 "BR_TRANSACTION_COMPLETE",
5732 "BR_INCREFS",
5733 "BR_ACQUIRE",
5734 "BR_RELEASE",
5735 "BR_DECREFS",
5736 "BR_ATTEMPT_ACQUIRE",
5737 "BR_NOOP",
5738 "BR_SPAWN_LOOPER",
5739 "BR_FINISHED",
5740 "BR_DEAD_BINDER",
5741 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
5742 "BR_FAILED_REPLY"
5743};
5744
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005745static const char * const binder_command_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005746 "BC_TRANSACTION",
5747 "BC_REPLY",
5748 "BC_ACQUIRE_RESULT",
5749 "BC_FREE_BUFFER",
5750 "BC_INCREFS",
5751 "BC_ACQUIRE",
5752 "BC_RELEASE",
5753 "BC_DECREFS",
5754 "BC_INCREFS_DONE",
5755 "BC_ACQUIRE_DONE",
5756 "BC_ATTEMPT_ACQUIRE",
5757 "BC_REGISTER_LOOPER",
5758 "BC_ENTER_LOOPER",
5759 "BC_EXIT_LOOPER",
5760 "BC_REQUEST_DEATH_NOTIFICATION",
5761 "BC_CLEAR_DEATH_NOTIFICATION",
Martijn Coenen5a6da532016-09-30 14:10:07 +02005762 "BC_DEAD_BINDER_DONE",
5763 "BC_TRANSACTION_SG",
5764 "BC_REPLY_SG",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005765};
5766
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005767static const char * const binder_objstat_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005768 "proc",
5769 "thread",
5770 "node",
5771 "ref",
5772 "death",
5773 "transaction",
5774 "transaction_complete"
5775};
5776
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005777static void print_binder_stats(struct seq_file *m, const char *prefix,
5778 struct binder_stats *stats)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005779{
5780 int i;
5781
5782 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005783 ARRAY_SIZE(binder_command_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005784 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005785 int temp = atomic_read(&stats->bc[i]);
5786
5787 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005788 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005789 binder_command_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005790 }
5791
5792 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005793 ARRAY_SIZE(binder_return_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005794 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005795 int temp = atomic_read(&stats->br[i]);
5796
5797 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005798 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005799 binder_return_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005800 }
5801
5802 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005803 ARRAY_SIZE(binder_objstat_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005804 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005805 ARRAY_SIZE(stats->obj_deleted));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005806 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005807 int created = atomic_read(&stats->obj_created[i]);
5808 int deleted = atomic_read(&stats->obj_deleted[i]);
5809
5810 if (created || deleted)
5811 seq_printf(m, "%s%s: active %d total %d\n",
5812 prefix,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005813 binder_objstat_strings[i],
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005814 created - deleted,
5815 created);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005816 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005817}
5818
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005819static void print_binder_proc_stats(struct seq_file *m,
5820 struct binder_proc *proc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005821{
5822 struct binder_work *w;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005823 struct binder_thread *thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005824 struct rb_node *n;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005825 int count, strong, weak, ready_threads;
Todd Kjosb4827902017-05-25 15:52:17 -07005826 size_t free_async_space =
5827 binder_alloc_get_free_async_space(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005828
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005829 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005830 seq_printf(m, "context %s\n", proc->context->name);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005831 count = 0;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005832 ready_threads = 0;
Todd Kjosb4827902017-05-25 15:52:17 -07005833 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005834 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
5835 count++;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005836
5837 list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node)
5838 ready_threads++;
5839
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005840 seq_printf(m, " threads: %d\n", count);
5841 seq_printf(m, " requested threads: %d+%d/%d\n"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005842 " ready threads %d\n"
5843 " free async space %zd\n", proc->requested_threads,
5844 proc->requested_threads_started, proc->max_threads,
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005845 ready_threads,
Todd Kjosb4827902017-05-25 15:52:17 -07005846 free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005847 count = 0;
5848 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
5849 count++;
Todd Kjos425d23f2017-06-12 12:07:26 -07005850 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005851 seq_printf(m, " nodes: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005852 count = 0;
5853 strong = 0;
5854 weak = 0;
Todd Kjos5346bf32016-10-20 16:43:34 -07005855 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005856 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
5857 struct binder_ref *ref = rb_entry(n, struct binder_ref,
5858 rb_node_desc);
5859 count++;
Todd Kjosb0117bb2017-05-08 09:16:27 -07005860 strong += ref->data.strong;
5861 weak += ref->data.weak;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005862 }
Todd Kjos5346bf32016-10-20 16:43:34 -07005863 binder_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005864 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005865
Todd Kjosd325d372016-10-10 10:40:53 -07005866 count = binder_alloc_get_allocated_count(&proc->alloc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005867 seq_printf(m, " buffers: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005868
Sherry Yang91004422017-08-22 17:26:57 -07005869 binder_alloc_print_pages(m, &proc->alloc);
5870
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005871 count = 0;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005872 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005873 list_for_each_entry(w, &proc->todo, entry) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005874 if (w->type == BINDER_WORK_TRANSACTION)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005875 count++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005876 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005877 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005878 seq_printf(m, " pending transactions: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005879
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005880 print_binder_stats(m, " ", &proc->stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005881}
5882
5883
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005884static int binder_state_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005885{
5886 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005887 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005888 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005889
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005890 seq_puts(m, "binder state:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005891
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005892 spin_lock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005893 if (!hlist_empty(&binder_dead_nodes))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005894 seq_puts(m, "dead nodes:\n");
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005895 hlist_for_each_entry(node, &binder_dead_nodes, dead_node) {
5896 /*
5897 * take a temporary reference on the node so it
5898 * survives and isn't removed from the list
5899 * while we print it.
5900 */
5901 node->tmp_refs++;
5902 spin_unlock(&binder_dead_nodes_lock);
5903 if (last_node)
5904 binder_put_node(last_node);
5905 binder_node_lock(node);
Todd Kjos425d23f2017-06-12 12:07:26 -07005906 print_binder_node_nilocked(m, node);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005907 binder_node_unlock(node);
5908 last_node = node;
5909 spin_lock(&binder_dead_nodes_lock);
5910 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005911 spin_unlock(&binder_dead_nodes_lock);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005912 if (last_node)
5913 binder_put_node(last_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005914
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005915 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005916 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005917 print_binder_proc(m, proc, 1);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005918 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005919
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005920 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005921}
5922
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005923static int binder_stats_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005924{
5925 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005926
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005927 seq_puts(m, "binder stats:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005928
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005929 print_binder_stats(m, "", &binder_stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005930
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_stats(m, proc);
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_transactions_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005940{
5941 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005942
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005943 seq_puts(m, "binder transactions:\n");
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005944 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005945 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005946 print_binder_proc(m, proc, 0);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005947 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005948
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005949 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005950}
5951
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005952static int binder_proc_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005953{
Riley Andrews83050a42016-02-09 21:05:33 -08005954 struct binder_proc *itr;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005955 int pid = (unsigned long)m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005956
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005957 mutex_lock(&binder_procs_lock);
Riley Andrews83050a42016-02-09 21:05:33 -08005958 hlist_for_each_entry(itr, &binder_procs, proc_node) {
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005959 if (itr->pid == pid) {
5960 seq_puts(m, "binder proc state:\n");
5961 print_binder_proc(m, itr, 1);
Riley Andrews83050a42016-02-09 21:05:33 -08005962 }
5963 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005964 mutex_unlock(&binder_procs_lock);
5965
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005966 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005967}
5968
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005969static void print_binder_transaction_log_entry(struct seq_file *m,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005970 struct binder_transaction_log_entry *e)
5971{
Todd Kjos1cfe6272017-05-24 13:33:28 -07005972 int debug_id = READ_ONCE(e->debug_id_done);
5973 /*
5974 * read barrier to guarantee debug_id_done read before
5975 * we print the log values
5976 */
5977 smp_rmb();
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005978 seq_printf(m,
Todd Kjos1cfe6272017-05-24 13:33:28 -07005979 "%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 -07005980 e->debug_id, (e->call_type == 2) ? "reply" :
5981 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005982 e->from_thread, e->to_proc, e->to_thread, e->context_name,
Todd Kjose598d172017-03-22 17:19:52 -07005983 e->to_node, e->target_handle, e->data_size, e->offsets_size,
5984 e->return_error, e->return_error_param,
5985 e->return_error_line);
Todd Kjos1cfe6272017-05-24 13:33:28 -07005986 /*
5987 * read-barrier to guarantee read of debug_id_done after
5988 * done printing the fields of the entry
5989 */
5990 smp_rmb();
5991 seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ?
5992 "\n" : " (incomplete)\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005993}
5994
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005995static int binder_transaction_log_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005996{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005997 struct binder_transaction_log *log = m->private;
Todd Kjos1cfe6272017-05-24 13:33:28 -07005998 unsigned int log_cur = atomic_read(&log->cur);
5999 unsigned int count;
6000 unsigned int cur;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006001 int i;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006002
Todd Kjos1cfe6272017-05-24 13:33:28 -07006003 count = log_cur + 1;
6004 cur = count < ARRAY_SIZE(log->entry) && !log->full ?
6005 0 : count % ARRAY_SIZE(log->entry);
6006 if (count > ARRAY_SIZE(log->entry) || log->full)
6007 count = ARRAY_SIZE(log->entry);
6008 for (i = 0; i < count; i++) {
6009 unsigned int index = cur++ % ARRAY_SIZE(log->entry);
6010
6011 print_binder_transaction_log_entry(m, &log->entry[index]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006012 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006013 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006014}
6015
6016static const struct file_operations binder_fops = {
6017 .owner = THIS_MODULE,
6018 .poll = binder_poll,
6019 .unlocked_ioctl = binder_ioctl,
Arve Hjønnevågda498892014-02-21 14:40:26 -08006020 .compat_ioctl = binder_ioctl,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006021 .mmap = binder_mmap,
6022 .open = binder_open,
6023 .flush = binder_flush,
6024 .release = binder_release,
6025};
6026
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006027BINDER_DEBUG_ENTRY(state);
6028BINDER_DEBUG_ENTRY(stats);
6029BINDER_DEBUG_ENTRY(transactions);
6030BINDER_DEBUG_ENTRY(transaction_log);
6031
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006032static int __init init_binder_device(const char *name)
6033{
6034 int ret;
6035 struct binder_device *binder_device;
6036
6037 binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
6038 if (!binder_device)
6039 return -ENOMEM;
6040
6041 binder_device->miscdev.fops = &binder_fops;
6042 binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
6043 binder_device->miscdev.name = name;
6044
6045 binder_device->context.binder_context_mgr_uid = INVALID_UID;
6046 binder_device->context.name = name;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07006047 mutex_init(&binder_device->context.context_mgr_node_lock);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006048
6049 ret = misc_register(&binder_device->miscdev);
6050 if (ret < 0) {
6051 kfree(binder_device);
6052 return ret;
6053 }
6054
6055 hlist_add_head(&binder_device->hlist, &binder_devices);
6056
6057 return ret;
6058}
6059
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006060static int __init binder_init(void)
6061{
6062 int ret;
Christian Brauner558ee932017-08-21 16:13:28 +02006063 char *device_name, *device_names, *device_tmp;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006064 struct binder_device *device;
6065 struct hlist_node *tmp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006066
Tetsuo Handaf8cb8222017-11-29 22:29:47 +09006067 ret = binder_alloc_shrinker_init();
6068 if (ret)
6069 return ret;
Sherry Yang5828d702017-07-29 13:24:11 -07006070
Todd Kjos1cfe6272017-05-24 13:33:28 -07006071 atomic_set(&binder_transaction_log.cur, ~0U);
6072 atomic_set(&binder_transaction_log_failed.cur, ~0U);
6073
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006074 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
6075 if (binder_debugfs_dir_entry_root)
6076 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
6077 binder_debugfs_dir_entry_root);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006078
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006079 if (binder_debugfs_dir_entry_root) {
6080 debugfs_create_file("state",
Harsh Shandilya174562a2017-12-22 19:37:02 +05306081 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006082 binder_debugfs_dir_entry_root,
6083 NULL,
6084 &binder_state_fops);
6085 debugfs_create_file("stats",
Harsh Shandilya174562a2017-12-22 19:37:02 +05306086 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006087 binder_debugfs_dir_entry_root,
6088 NULL,
6089 &binder_stats_fops);
6090 debugfs_create_file("transactions",
Harsh Shandilya174562a2017-12-22 19:37:02 +05306091 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006092 binder_debugfs_dir_entry_root,
6093 NULL,
6094 &binder_transactions_fops);
6095 debugfs_create_file("transaction_log",
Harsh Shandilya174562a2017-12-22 19:37:02 +05306096 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006097 binder_debugfs_dir_entry_root,
6098 &binder_transaction_log,
6099 &binder_transaction_log_fops);
6100 debugfs_create_file("failed_transaction_log",
Harsh Shandilya174562a2017-12-22 19:37:02 +05306101 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006102 binder_debugfs_dir_entry_root,
6103 &binder_transaction_log_failed,
6104 &binder_transaction_log_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006105 }
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006106
6107 /*
6108 * Copy the module_parameter string, because we don't want to
6109 * tokenize it in-place.
6110 */
6111 device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL);
6112 if (!device_names) {
6113 ret = -ENOMEM;
6114 goto err_alloc_device_names_failed;
6115 }
6116 strcpy(device_names, binder_devices_param);
6117
Christian Brauner558ee932017-08-21 16:13:28 +02006118 device_tmp = device_names;
6119 while ((device_name = strsep(&device_tmp, ","))) {
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006120 ret = init_binder_device(device_name);
6121 if (ret)
6122 goto err_init_binder_device_failed;
6123 }
6124
6125 return ret;
6126
6127err_init_binder_device_failed:
6128 hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
6129 misc_deregister(&device->miscdev);
6130 hlist_del(&device->hlist);
6131 kfree(device);
6132 }
Christian Brauner558ee932017-08-21 16:13:28 +02006133
6134 kfree(device_names);
6135
Martijn Coenen6b7c7122016-09-30 16:08:09 +02006136err_alloc_device_names_failed:
6137 debugfs_remove_recursive(binder_debugfs_dir_entry_root);
6138
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006139 return ret;
6140}
6141
6142device_initcall(binder_init);
6143
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07006144#define CREATE_TRACE_POINTS
6145#include "binder_trace.h"
6146
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006147MODULE_LICENSE("GPL v2");