blob: 0572038eec3f2e10f406e8c36e9e76175ad8e925 [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#ifdef CONFIG_ANDROID_BINDER_IPC_32BIT
75#define BINDER_IPC_32BIT 1
76#endif
77
78#include <uapi/linux/android/binder.h>
Todd Kjosb9341022016-10-10 10:40:53 -070079#include "binder_alloc.h"
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -070080#include "binder_trace.h"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090081
Todd Kjos8d9f6f32016-10-17 12:33:15 -070082static HLIST_HEAD(binder_deferred_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090083static DEFINE_MUTEX(binder_deferred_lock);
84
Martijn Coenen6b7c7122016-09-30 16:08:09 +020085static HLIST_HEAD(binder_devices);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090086static HLIST_HEAD(binder_procs);
Todd Kjos8d9f6f32016-10-17 12:33:15 -070087static DEFINE_MUTEX(binder_procs_lock);
88
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090089static HLIST_HEAD(binder_dead_nodes);
Todd Kjos8d9f6f32016-10-17 12:33:15 -070090static DEFINE_SPINLOCK(binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090091
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070092static struct dentry *binder_debugfs_dir_entry_root;
93static struct dentry *binder_debugfs_dir_entry_proc;
Todd Kjosc4bd08b2017-05-25 10:56:00 -070094static atomic_t binder_last_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090095
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070096#define BINDER_DEBUG_ENTRY(name) \
97static int binder_##name##_open(struct inode *inode, struct file *file) \
98{ \
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070099 return single_open(file, binder_##name##_show, inode->i_private); \
Arve Hjønnevåg5249f482009-04-28 20:57:50 -0700100} \
101\
102static const struct file_operations binder_##name##_fops = { \
103 .owner = THIS_MODULE, \
104 .open = binder_##name##_open, \
105 .read = seq_read, \
106 .llseek = seq_lseek, \
107 .release = single_release, \
108}
109
110static int binder_proc_show(struct seq_file *m, void *unused);
111BINDER_DEBUG_ENTRY(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900112
113/* This is only defined in include/asm-arm/sizes.h */
114#ifndef SZ_1K
115#define SZ_1K 0x400
116#endif
117
118#ifndef SZ_4M
119#define SZ_4M 0x400000
120#endif
121
122#define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
123
124#define BINDER_SMALL_BUF_SIZE (PAGE_SIZE * 64)
125
126enum {
127 BINDER_DEBUG_USER_ERROR = 1U << 0,
128 BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1,
129 BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2,
130 BINDER_DEBUG_OPEN_CLOSE = 1U << 3,
131 BINDER_DEBUG_DEAD_BINDER = 1U << 4,
132 BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5,
133 BINDER_DEBUG_READ_WRITE = 1U << 6,
134 BINDER_DEBUG_USER_REFS = 1U << 7,
135 BINDER_DEBUG_THREADS = 1U << 8,
136 BINDER_DEBUG_TRANSACTION = 1U << 9,
137 BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10,
138 BINDER_DEBUG_FREE_BUFFER = 1U << 11,
139 BINDER_DEBUG_INTERNAL_REFS = 1U << 12,
Todd Kjosd325d372016-10-10 10:40:53 -0700140 BINDER_DEBUG_PRIORITY_CAP = 1U << 13,
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700141 BINDER_DEBUG_SPINLOCKS = 1U << 14,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900142};
143static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR |
144 BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION;
145module_param_named(debug_mask, binder_debug_mask, uint, S_IWUSR | S_IRUGO);
146
Martijn Coenen6b7c7122016-09-30 16:08:09 +0200147static char *binder_devices_param = CONFIG_ANDROID_BINDER_DEVICES;
148module_param_named(devices, binder_devices_param, charp, S_IRUGO);
149
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900150static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
151static int binder_stop_on_user_error;
152
153static int binder_set_stop_on_user_error(const char *val,
154 struct kernel_param *kp)
155{
156 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +0900157
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900158 ret = param_set_int(val, kp);
159 if (binder_stop_on_user_error < 2)
160 wake_up(&binder_user_error_wait);
161 return ret;
162}
163module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
164 param_get_int, &binder_stop_on_user_error, S_IWUSR | S_IRUGO);
165
166#define binder_debug(mask, x...) \
167 do { \
168 if (binder_debug_mask & mask) \
Sherwin Soltani258767f2012-06-26 02:00:30 -0400169 pr_info(x); \
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900170 } while (0)
171
172#define binder_user_error(x...) \
173 do { \
174 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
Sherwin Soltani258767f2012-06-26 02:00:30 -0400175 pr_info(x); \
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900176 if (binder_stop_on_user_error) \
177 binder_stop_on_user_error = 2; \
178 } while (0)
179
Martijn Coenen00c80372016-07-13 12:06:49 +0200180#define to_flat_binder_object(hdr) \
181 container_of(hdr, struct flat_binder_object, hdr)
182
183#define to_binder_fd_object(hdr) container_of(hdr, struct binder_fd_object, hdr)
184
Martijn Coenen5a6da532016-09-30 14:10:07 +0200185#define to_binder_buffer_object(hdr) \
186 container_of(hdr, struct binder_buffer_object, hdr)
187
Martijn Coenene3e0f4802016-10-18 13:58:55 +0200188#define to_binder_fd_array_object(hdr) \
189 container_of(hdr, struct binder_fd_array_object, hdr)
190
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900191enum binder_stat_types {
192 BINDER_STAT_PROC,
193 BINDER_STAT_THREAD,
194 BINDER_STAT_NODE,
195 BINDER_STAT_REF,
196 BINDER_STAT_DEATH,
197 BINDER_STAT_TRANSACTION,
198 BINDER_STAT_TRANSACTION_COMPLETE,
199 BINDER_STAT_COUNT
200};
201
202struct binder_stats {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -0700203 atomic_t br[_IOC_NR(BR_FAILED_REPLY) + 1];
204 atomic_t bc[_IOC_NR(BC_REPLY_SG) + 1];
205 atomic_t obj_created[BINDER_STAT_COUNT];
206 atomic_t obj_deleted[BINDER_STAT_COUNT];
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900207};
208
209static struct binder_stats binder_stats;
210
211static inline void binder_stats_deleted(enum binder_stat_types type)
212{
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -0700213 atomic_inc(&binder_stats.obj_deleted[type]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900214}
215
216static inline void binder_stats_created(enum binder_stat_types type)
217{
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -0700218 atomic_inc(&binder_stats.obj_created[type]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900219}
220
221struct binder_transaction_log_entry {
222 int debug_id;
Todd Kjos1cfe6272017-05-24 13:33:28 -0700223 int debug_id_done;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900224 int call_type;
225 int from_proc;
226 int from_thread;
227 int target_handle;
228 int to_proc;
229 int to_thread;
230 int to_node;
231 int data_size;
232 int offsets_size;
Todd Kjose598d172017-03-22 17:19:52 -0700233 int return_error_line;
234 uint32_t return_error;
235 uint32_t return_error_param;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +0200236 const char *context_name;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900237};
238struct binder_transaction_log {
Todd Kjos1cfe6272017-05-24 13:33:28 -0700239 atomic_t cur;
240 bool full;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900241 struct binder_transaction_log_entry entry[32];
242};
243static struct binder_transaction_log binder_transaction_log;
244static struct binder_transaction_log binder_transaction_log_failed;
245
246static struct binder_transaction_log_entry *binder_transaction_log_add(
247 struct binder_transaction_log *log)
248{
249 struct binder_transaction_log_entry *e;
Todd Kjos1cfe6272017-05-24 13:33:28 -0700250 unsigned int cur = atomic_inc_return(&log->cur);
Seunghun Lee10f62862014-05-01 01:30:23 +0900251
Todd Kjos1cfe6272017-05-24 13:33:28 -0700252 if (cur >= ARRAY_SIZE(log->entry))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900253 log->full = 1;
Todd Kjos1cfe6272017-05-24 13:33:28 -0700254 e = &log->entry[cur % ARRAY_SIZE(log->entry)];
255 WRITE_ONCE(e->debug_id_done, 0);
256 /*
257 * write-barrier to synchronize access to e->debug_id_done.
258 * We make sure the initialized 0 value is seen before
259 * memset() other fields are zeroed by memset.
260 */
261 smp_wmb();
262 memset(e, 0, sizeof(*e));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900263 return e;
264}
265
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200266struct binder_context {
267 struct binder_node *binder_context_mgr_node;
Todd Kjos8d9f6f32016-10-17 12:33:15 -0700268 struct mutex context_mgr_node_lock;
269
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200270 kuid_t binder_context_mgr_uid;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +0200271 const char *name;
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200272};
273
Martijn Coenen6b7c7122016-09-30 16:08:09 +0200274struct binder_device {
275 struct hlist_node hlist;
276 struct miscdevice miscdev;
277 struct binder_context context;
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200278};
279
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700280/**
281 * struct binder_work - work enqueued on a worklist
282 * @entry: node enqueued on list
283 * @type: type of work to be performed
284 *
285 * There are separate work lists for proc, thread, and node (async).
286 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900287struct binder_work {
288 struct list_head entry;
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700289
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900290 enum {
291 BINDER_WORK_TRANSACTION = 1,
292 BINDER_WORK_TRANSACTION_COMPLETE,
Todd Kjos858b8da2017-04-21 17:35:12 -0700293 BINDER_WORK_RETURN_ERROR,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900294 BINDER_WORK_NODE,
295 BINDER_WORK_DEAD_BINDER,
296 BINDER_WORK_DEAD_BINDER_AND_CLEAR,
297 BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
298 } type;
299};
300
Todd Kjos858b8da2017-04-21 17:35:12 -0700301struct binder_error {
302 struct binder_work work;
303 uint32_t cmd;
304};
305
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700306/**
307 * struct binder_node - binder node bookkeeping
308 * @debug_id: unique ID for debugging
309 * (invariant after initialized)
310 * @lock: lock for node fields
311 * @work: worklist element for node work
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700312 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700313 * @rb_node: element for proc->nodes tree
Todd Kjos425d23f2017-06-12 12:07:26 -0700314 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700315 * @dead_node: element for binder_dead_nodes list
316 * (protected by binder_dead_nodes_lock)
317 * @proc: binder_proc that owns this node
318 * (invariant after initialized)
319 * @refs: list of references on this node
Todd Kjoscbcbbd62017-06-08 13:45:59 -0700320 * (protected by @lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700321 * @internal_strong_refs: used to take strong references when
322 * initiating a transaction
Todd Kjose7f23ed2017-03-21 13:06:01 -0700323 * (protected by @proc->inner_lock if @proc
324 * and by @lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700325 * @local_weak_refs: weak user refs from local process
Todd Kjose7f23ed2017-03-21 13:06:01 -0700326 * (protected by @proc->inner_lock if @proc
327 * and by @lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700328 * @local_strong_refs: strong user refs from local process
Todd Kjose7f23ed2017-03-21 13:06:01 -0700329 * (protected by @proc->inner_lock if @proc
330 * and by @lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700331 * @tmp_refs: temporary kernel refs
Todd Kjose7f23ed2017-03-21 13:06:01 -0700332 * (protected by @proc->inner_lock while @proc
333 * is valid, and by binder_dead_nodes_lock
334 * if @proc is NULL. During inc/dec and node release
335 * it is also protected by @lock to provide safety
336 * as the node dies and @proc becomes NULL)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700337 * @ptr: userspace pointer for node
338 * (invariant, no lock needed)
339 * @cookie: userspace cookie for node
340 * (invariant, no lock needed)
341 * @has_strong_ref: userspace notified of strong ref
Todd Kjose7f23ed2017-03-21 13:06:01 -0700342 * (protected by @proc->inner_lock if @proc
343 * and by @lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700344 * @pending_strong_ref: userspace has acked notification of strong ref
Todd Kjose7f23ed2017-03-21 13:06:01 -0700345 * (protected by @proc->inner_lock if @proc
346 * and by @lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700347 * @has_weak_ref: userspace notified of weak ref
Todd Kjose7f23ed2017-03-21 13:06:01 -0700348 * (protected by @proc->inner_lock if @proc
349 * and by @lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700350 * @pending_weak_ref: userspace has acked notification of weak ref
Todd Kjose7f23ed2017-03-21 13:06:01 -0700351 * (protected by @proc->inner_lock if @proc
352 * and by @lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700353 * @has_async_transaction: async transaction to node in progress
Todd Kjoscbcbbd62017-06-08 13:45:59 -0700354 * (protected by @lock)
Martijn Coenen6aac9792017-06-07 09:29:14 -0700355 * @sched_policy: minimum scheduling policy for node
356 * (invariant after initialized)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700357 * @accept_fds: file descriptor operations supported for node
358 * (invariant after initialized)
359 * @min_priority: minimum scheduling priority
360 * (invariant after initialized)
Martijn Coenenc46810c2017-06-23 10:13:43 -0700361 * @inherit_rt: inherit RT scheduling policy from caller
362 * (invariant after initialized)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700363 * @async_todo: list of async work items
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700364 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700365 *
366 * Bookkeeping structure for binder nodes.
367 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900368struct binder_node {
369 int debug_id;
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700370 spinlock_t lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900371 struct binder_work work;
372 union {
373 struct rb_node rb_node;
374 struct hlist_node dead_node;
375 };
376 struct binder_proc *proc;
377 struct hlist_head refs;
378 int internal_strong_refs;
379 int local_weak_refs;
380 int local_strong_refs;
Todd Kjosf22abc72017-05-09 11:08:05 -0700381 int tmp_refs;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800382 binder_uintptr_t ptr;
383 binder_uintptr_t cookie;
Todd Kjose7f23ed2017-03-21 13:06:01 -0700384 struct {
385 /*
386 * bitfield elements protected by
387 * proc inner_lock
388 */
389 u8 has_strong_ref:1;
390 u8 pending_strong_ref:1;
391 u8 has_weak_ref:1;
392 u8 pending_weak_ref:1;
393 };
394 struct {
395 /*
396 * invariant after initialization
397 */
Martijn Coenen6aac9792017-06-07 09:29:14 -0700398 u8 sched_policy:2;
Martijn Coenenc46810c2017-06-23 10:13:43 -0700399 u8 inherit_rt:1;
Todd Kjose7f23ed2017-03-21 13:06:01 -0700400 u8 accept_fds:1;
401 u8 min_priority;
402 };
403 bool has_async_transaction;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900404 struct list_head async_todo;
405};
406
407struct binder_ref_death {
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700408 /**
409 * @work: worklist element for death notifications
410 * (protected by inner_lock of the proc that
411 * this ref belongs to)
412 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900413 struct binder_work work;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800414 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900415};
416
Todd Kjosb0117bb2017-05-08 09:16:27 -0700417/**
418 * struct binder_ref_data - binder_ref counts and id
419 * @debug_id: unique ID for the ref
420 * @desc: unique userspace handle for ref
421 * @strong: strong ref count (debugging only if not locked)
422 * @weak: weak ref count (debugging only if not locked)
423 *
424 * Structure to hold ref count and ref id information. Since
425 * the actual ref can only be accessed with a lock, this structure
426 * is used to return information about the ref to callers of
427 * ref inc/dec functions.
428 */
429struct binder_ref_data {
430 int debug_id;
431 uint32_t desc;
432 int strong;
433 int weak;
434};
435
436/**
437 * struct binder_ref - struct to track references on nodes
438 * @data: binder_ref_data containing id, handle, and current refcounts
439 * @rb_node_desc: node for lookup by @data.desc in proc's rb_tree
440 * @rb_node_node: node for lookup by @node in proc's rb_tree
441 * @node_entry: list entry for node->refs list in target node
Todd Kjoscbcbbd62017-06-08 13:45:59 -0700442 * (protected by @node->lock)
Todd Kjosb0117bb2017-05-08 09:16:27 -0700443 * @proc: binder_proc containing ref
444 * @node: binder_node of target node. When cleaning up a
445 * ref for deletion in binder_cleanup_ref, a non-NULL
446 * @node indicates the node must be freed
447 * @death: pointer to death notification (ref_death) if requested
Martijn Coenenf9eac642017-05-22 11:26:23 -0700448 * (protected by @node->lock)
Todd Kjosb0117bb2017-05-08 09:16:27 -0700449 *
450 * Structure to track references from procA to target node (on procB). This
451 * structure is unsafe to access without holding @proc->outer_lock.
452 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900453struct binder_ref {
454 /* Lookups needed: */
455 /* node + proc => ref (transaction) */
456 /* desc + proc => ref (transaction, inc/dec ref) */
457 /* node => refs + procs (proc exit) */
Todd Kjosb0117bb2017-05-08 09:16:27 -0700458 struct binder_ref_data data;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900459 struct rb_node rb_node_desc;
460 struct rb_node rb_node_node;
461 struct hlist_node node_entry;
462 struct binder_proc *proc;
463 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900464 struct binder_ref_death *death;
465};
466
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900467enum binder_deferred_state {
468 BINDER_DEFERRED_PUT_FILES = 0x01,
469 BINDER_DEFERRED_FLUSH = 0x02,
470 BINDER_DEFERRED_RELEASE = 0x04,
471};
472
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700473/**
Martijn Coenen57b2ac62017-06-06 17:04:42 -0700474 * struct binder_priority - scheduler policy and priority
475 * @sched_policy scheduler policy
476 * @prio [100..139] for SCHED_NORMAL, [0..99] for FIFO/RT
477 *
478 * The binder driver supports inheriting the following scheduler policies:
479 * SCHED_NORMAL
480 * SCHED_BATCH
481 * SCHED_FIFO
482 * SCHED_RR
483 */
484struct binder_priority {
485 unsigned int sched_policy;
486 int prio;
487};
488
489/**
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700490 * struct binder_proc - binder process bookkeeping
491 * @proc_node: element for binder_procs list
492 * @threads: rbtree of binder_threads in this proc
Todd Kjosb4827902017-05-25 15:52:17 -0700493 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700494 * @nodes: rbtree of binder nodes associated with
495 * this proc ordered by node->ptr
Todd Kjos425d23f2017-06-12 12:07:26 -0700496 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700497 * @refs_by_desc: rbtree of refs ordered by ref->desc
Todd Kjos5346bf32016-10-20 16:43:34 -0700498 * (protected by @outer_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700499 * @refs_by_node: rbtree of refs ordered by ref->node
Todd Kjos5346bf32016-10-20 16:43:34 -0700500 * (protected by @outer_lock)
Martijn Coenen22d64e4322017-06-02 11:15:44 -0700501 * @waiting_threads: threads currently waiting for proc work
502 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700503 * @pid PID of group_leader of process
504 * (invariant after initialized)
505 * @tsk task_struct for group_leader of process
506 * (invariant after initialized)
507 * @files files_struct for process
508 * (invariant after initialized)
509 * @deferred_work_node: element for binder_deferred_list
510 * (protected by binder_deferred_lock)
511 * @deferred_work: bitmap of deferred work to perform
512 * (protected by binder_deferred_lock)
513 * @is_dead: process is dead and awaiting free
514 * when outstanding transactions are cleaned up
Todd Kjosb4827902017-05-25 15:52:17 -0700515 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700516 * @todo: list of work for this process
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700517 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700518 * @wait: wait queue head to wait for proc work
519 * (invariant after initialized)
520 * @stats: per-process binder statistics
521 * (atomics, no lock needed)
522 * @delivered_death: list of delivered death notification
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700523 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700524 * @max_threads: cap on number of binder threads
Todd Kjosd600e902017-05-25 17:35:02 -0700525 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700526 * @requested_threads: number of binder threads requested but not
527 * yet started. In current implementation, can
528 * only be 0 or 1.
Todd Kjosd600e902017-05-25 17:35:02 -0700529 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700530 * @requested_threads_started: number binder threads started
Todd Kjosd600e902017-05-25 17:35:02 -0700531 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700532 * @tmp_ref: temporary reference to indicate proc is in use
Todd Kjosb4827902017-05-25 15:52:17 -0700533 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700534 * @default_priority: default scheduler priority
535 * (invariant after initialized)
536 * @debugfs_entry: debugfs node
537 * @alloc: binder allocator bookkeeping
538 * @context: binder_context for this proc
539 * (invariant after initialized)
540 * @inner_lock: can nest under outer_lock and/or node lock
541 * @outer_lock: no nesting under innor or node lock
542 * Lock order: 1) outer, 2) node, 3) inner
543 *
544 * Bookkeeping structure for binder processes
545 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900546struct binder_proc {
547 struct hlist_node proc_node;
548 struct rb_root threads;
549 struct rb_root nodes;
550 struct rb_root refs_by_desc;
551 struct rb_root refs_by_node;
Martijn Coenen22d64e4322017-06-02 11:15:44 -0700552 struct list_head waiting_threads;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900553 int pid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900554 struct task_struct *tsk;
555 struct files_struct *files;
556 struct hlist_node deferred_work_node;
557 int deferred_work;
Todd Kjos2f993e22017-05-12 14:42:55 -0700558 bool is_dead;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900559
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900560 struct list_head todo;
561 wait_queue_head_t wait;
562 struct binder_stats stats;
563 struct list_head delivered_death;
564 int max_threads;
565 int requested_threads;
566 int requested_threads_started;
Todd Kjos2f993e22017-05-12 14:42:55 -0700567 int tmp_ref;
Martijn Coenen57b2ac62017-06-06 17:04:42 -0700568 struct binder_priority default_priority;
Arve Hjønnevåg16b66552009-04-28 20:57:50 -0700569 struct dentry *debugfs_entry;
Todd Kjosf85d2292016-10-10 10:39:59 -0700570 struct binder_alloc alloc;
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200571 struct binder_context *context;
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700572 spinlock_t inner_lock;
573 spinlock_t outer_lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900574};
575
576enum {
577 BINDER_LOOPER_STATE_REGISTERED = 0x01,
578 BINDER_LOOPER_STATE_ENTERED = 0x02,
579 BINDER_LOOPER_STATE_EXITED = 0x04,
580 BINDER_LOOPER_STATE_INVALID = 0x08,
581 BINDER_LOOPER_STATE_WAITING = 0x10,
Martijn Coenen22d64e4322017-06-02 11:15:44 -0700582 BINDER_LOOPER_STATE_POLL = 0x20,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900583};
584
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700585/**
586 * struct binder_thread - binder thread bookkeeping
587 * @proc: binder process for this thread
588 * (invariant after initialization)
589 * @rb_node: element for proc->threads rbtree
Todd Kjosb4827902017-05-25 15:52:17 -0700590 * (protected by @proc->inner_lock)
Martijn Coenen22d64e4322017-06-02 11:15:44 -0700591 * @waiting_thread_node: element for @proc->waiting_threads list
592 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700593 * @pid: PID for this thread
594 * (invariant after initialization)
595 * @looper: bitmap of looping state
596 * (only accessed by this thread)
597 * @looper_needs_return: looping thread needs to exit driver
598 * (no lock needed)
599 * @transaction_stack: stack of in-progress transactions for this thread
Martijn Coenen995a36e2017-06-02 13:36:52 -0700600 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700601 * @todo: list of work to do for this thread
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700602 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700603 * @return_error: transaction errors reported by this thread
604 * (only accessed by this thread)
605 * @reply_error: transaction errors reported by target thread
Martijn Coenen995a36e2017-06-02 13:36:52 -0700606 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700607 * @wait: wait queue for thread work
608 * @stats: per-thread statistics
609 * (atomics, no lock needed)
610 * @tmp_ref: temporary reference to indicate thread is in use
611 * (atomic since @proc->inner_lock cannot
612 * always be acquired)
613 * @is_dead: thread is dead and awaiting free
614 * when outstanding transactions are cleaned up
Todd Kjosb4827902017-05-25 15:52:17 -0700615 * (protected by @proc->inner_lock)
Martijn Coenen07a30fe2017-06-07 10:02:12 -0700616 * @task: struct task_struct for this thread
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700617 *
618 * Bookkeeping structure for binder threads.
619 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900620struct binder_thread {
621 struct binder_proc *proc;
622 struct rb_node rb_node;
Martijn Coenen22d64e4322017-06-02 11:15:44 -0700623 struct list_head waiting_thread_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900624 int pid;
Todd Kjos6798e6d2017-01-06 14:19:25 -0800625 int looper; /* only modified by this thread */
626 bool looper_need_return; /* can be written by other thread */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900627 struct binder_transaction *transaction_stack;
628 struct list_head todo;
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 Kjos2f993e22017-05-12 14:42:55 -0700656 /**
657 * @lock: protects @from, @to_proc, and @to_thread
658 *
659 * @from, @to_proc, and @to_thread can be set to NULL
660 * during thread teardown
661 */
662 spinlock_t lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900663};
664
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700665/**
666 * binder_proc_lock() - Acquire outer lock for given binder_proc
667 * @proc: struct binder_proc to acquire
668 *
669 * Acquires proc->outer_lock. Used to protect binder_ref
670 * structures associated with the given proc.
671 */
672#define binder_proc_lock(proc) _binder_proc_lock(proc, __LINE__)
673static void
674_binder_proc_lock(struct binder_proc *proc, int line)
675{
676 binder_debug(BINDER_DEBUG_SPINLOCKS,
677 "%s: line=%d\n", __func__, line);
678 spin_lock(&proc->outer_lock);
679}
680
681/**
682 * binder_proc_unlock() - Release spinlock for given binder_proc
683 * @proc: struct binder_proc to acquire
684 *
685 * Release lock acquired via binder_proc_lock()
686 */
687#define binder_proc_unlock(_proc) _binder_proc_unlock(_proc, __LINE__)
688static void
689_binder_proc_unlock(struct binder_proc *proc, int line)
690{
691 binder_debug(BINDER_DEBUG_SPINLOCKS,
692 "%s: line=%d\n", __func__, line);
693 spin_unlock(&proc->outer_lock);
694}
695
696/**
697 * binder_inner_proc_lock() - Acquire inner lock for given binder_proc
698 * @proc: struct binder_proc to acquire
699 *
700 * Acquires proc->inner_lock. Used to protect todo lists
701 */
702#define binder_inner_proc_lock(proc) _binder_inner_proc_lock(proc, __LINE__)
703static void
704_binder_inner_proc_lock(struct binder_proc *proc, int line)
705{
706 binder_debug(BINDER_DEBUG_SPINLOCKS,
707 "%s: line=%d\n", __func__, line);
708 spin_lock(&proc->inner_lock);
709}
710
711/**
712 * binder_inner_proc_unlock() - Release inner lock for given binder_proc
713 * @proc: struct binder_proc to acquire
714 *
715 * Release lock acquired via binder_inner_proc_lock()
716 */
717#define binder_inner_proc_unlock(proc) _binder_inner_proc_unlock(proc, __LINE__)
718static void
719_binder_inner_proc_unlock(struct binder_proc *proc, int line)
720{
721 binder_debug(BINDER_DEBUG_SPINLOCKS,
722 "%s: line=%d\n", __func__, line);
723 spin_unlock(&proc->inner_lock);
724}
725
726/**
727 * binder_node_lock() - Acquire spinlock for given binder_node
728 * @node: struct binder_node to acquire
729 *
730 * Acquires node->lock. Used to protect binder_node fields
731 */
732#define binder_node_lock(node) _binder_node_lock(node, __LINE__)
733static void
734_binder_node_lock(struct binder_node *node, int line)
735{
736 binder_debug(BINDER_DEBUG_SPINLOCKS,
737 "%s: line=%d\n", __func__, line);
738 spin_lock(&node->lock);
739}
740
741/**
742 * binder_node_unlock() - Release spinlock for given binder_proc
743 * @node: struct binder_node to acquire
744 *
745 * Release lock acquired via binder_node_lock()
746 */
747#define binder_node_unlock(node) _binder_node_unlock(node, __LINE__)
748static void
749_binder_node_unlock(struct binder_node *node, int line)
750{
751 binder_debug(BINDER_DEBUG_SPINLOCKS,
752 "%s: line=%d\n", __func__, line);
753 spin_unlock(&node->lock);
754}
755
Todd Kjoscbcbbd62017-06-08 13:45:59 -0700756/**
757 * binder_node_inner_lock() - Acquire node and inner locks
758 * @node: struct binder_node to acquire
759 *
760 * Acquires node->lock. If node->proc also acquires
761 * proc->inner_lock. Used to protect binder_node fields
762 */
763#define binder_node_inner_lock(node) _binder_node_inner_lock(node, __LINE__)
764static void
765_binder_node_inner_lock(struct binder_node *node, int line)
766{
767 binder_debug(BINDER_DEBUG_SPINLOCKS,
768 "%s: line=%d\n", __func__, line);
769 spin_lock(&node->lock);
770 if (node->proc)
771 binder_inner_proc_lock(node->proc);
772}
773
774/**
775 * binder_node_unlock() - Release node and inner locks
776 * @node: struct binder_node to acquire
777 *
778 * Release lock acquired via binder_node_lock()
779 */
780#define binder_node_inner_unlock(node) _binder_node_inner_unlock(node, __LINE__)
781static void
782_binder_node_inner_unlock(struct binder_node *node, int line)
783{
784 struct binder_proc *proc = node->proc;
785
786 binder_debug(BINDER_DEBUG_SPINLOCKS,
787 "%s: line=%d\n", __func__, line);
788 if (proc)
789 binder_inner_proc_unlock(proc);
790 spin_unlock(&node->lock);
791}
792
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700793static bool binder_worklist_empty_ilocked(struct list_head *list)
794{
795 return list_empty(list);
796}
797
798/**
799 * binder_worklist_empty() - Check if no items on the work list
800 * @proc: binder_proc associated with list
801 * @list: list to check
802 *
803 * Return: true if there are no items on list, else false
804 */
805static bool binder_worklist_empty(struct binder_proc *proc,
806 struct list_head *list)
807{
808 bool ret;
809
810 binder_inner_proc_lock(proc);
811 ret = binder_worklist_empty_ilocked(list);
812 binder_inner_proc_unlock(proc);
813 return ret;
814}
815
816static void
817binder_enqueue_work_ilocked(struct binder_work *work,
818 struct list_head *target_list)
819{
820 BUG_ON(target_list == NULL);
821 BUG_ON(work->entry.next && !list_empty(&work->entry));
822 list_add_tail(&work->entry, target_list);
823}
824
825/**
826 * binder_enqueue_work() - Add an item to the work list
827 * @proc: binder_proc associated with list
828 * @work: struct binder_work to add to list
829 * @target_list: list to add work to
830 *
831 * Adds the work to the specified list. Asserts that work
832 * is not already on a list.
833 */
834static void
835binder_enqueue_work(struct binder_proc *proc,
836 struct binder_work *work,
837 struct list_head *target_list)
838{
839 binder_inner_proc_lock(proc);
840 binder_enqueue_work_ilocked(work, target_list);
841 binder_inner_proc_unlock(proc);
842}
843
844static void
845binder_dequeue_work_ilocked(struct binder_work *work)
846{
847 list_del_init(&work->entry);
848}
849
850/**
851 * binder_dequeue_work() - Removes an item from the work list
852 * @proc: binder_proc associated with list
853 * @work: struct binder_work to remove from list
854 *
855 * Removes the specified work item from whatever list it is on.
856 * Can safely be called if work is not on any list.
857 */
858static void
859binder_dequeue_work(struct binder_proc *proc, struct binder_work *work)
860{
861 binder_inner_proc_lock(proc);
862 binder_dequeue_work_ilocked(work);
863 binder_inner_proc_unlock(proc);
864}
865
866static struct binder_work *binder_dequeue_work_head_ilocked(
867 struct list_head *list)
868{
869 struct binder_work *w;
870
871 w = list_first_entry_or_null(list, struct binder_work, entry);
872 if (w)
873 list_del_init(&w->entry);
874 return w;
875}
876
877/**
878 * binder_dequeue_work_head() - Dequeues the item at head of list
879 * @proc: binder_proc associated with list
880 * @list: list to dequeue head
881 *
882 * Removes the head of the list if there are items on the list
883 *
884 * Return: pointer dequeued binder_work, NULL if list was empty
885 */
886static struct binder_work *binder_dequeue_work_head(
887 struct binder_proc *proc,
888 struct list_head *list)
889{
890 struct binder_work *w;
891
892 binder_inner_proc_lock(proc);
893 w = binder_dequeue_work_head_ilocked(list);
894 binder_inner_proc_unlock(proc);
895 return w;
896}
897
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900898static void
899binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
Todd Kjos2f993e22017-05-12 14:42:55 -0700900static void binder_free_thread(struct binder_thread *thread);
901static void binder_free_proc(struct binder_proc *proc);
Todd Kjos425d23f2017-06-12 12:07:26 -0700902static void binder_inc_node_tmpref_ilocked(struct binder_node *node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900903
Sachin Kamatefde99c2012-08-17 16:39:36 +0530904static int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900905{
906 struct files_struct *files = proc->files;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900907 unsigned long rlim_cur;
908 unsigned long irqs;
909
910 if (files == NULL)
911 return -ESRCH;
912
Al Virodcfadfa2012-08-12 17:27:30 -0400913 if (!lock_task_sighand(proc->tsk, &irqs))
914 return -EMFILE;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900915
Al Virodcfadfa2012-08-12 17:27:30 -0400916 rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
917 unlock_task_sighand(proc->tsk, &irqs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900918
Al Virodcfadfa2012-08-12 17:27:30 -0400919 return __alloc_fd(files, 0, rlim_cur, flags);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900920}
921
922/*
923 * copied from fd_install
924 */
925static void task_fd_install(
926 struct binder_proc *proc, unsigned int fd, struct file *file)
927{
Al Virof869e8a2012-08-15 21:06:33 -0400928 if (proc->files)
929 __fd_install(proc->files, fd, file);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900930}
931
932/*
933 * copied from sys_close
934 */
935static long task_close_fd(struct binder_proc *proc, unsigned int fd)
936{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900937 int retval;
938
Al Viro483ce1d2012-08-19 12:04:24 -0400939 if (proc->files == NULL)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900940 return -ESRCH;
941
Al Viro483ce1d2012-08-19 12:04:24 -0400942 retval = __close_fd(proc->files, fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900943 /* can't restart close syscall because file table entry was cleared */
944 if (unlikely(retval == -ERESTARTSYS ||
945 retval == -ERESTARTNOINTR ||
946 retval == -ERESTARTNOHAND ||
947 retval == -ERESTART_RESTARTBLOCK))
948 retval = -EINTR;
949
950 return retval;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900951}
952
Martijn Coenen22d64e4322017-06-02 11:15:44 -0700953static bool binder_has_work_ilocked(struct binder_thread *thread,
954 bool do_proc_work)
955{
956 return !binder_worklist_empty_ilocked(&thread->todo) ||
957 thread->looper_need_return ||
958 (do_proc_work &&
959 !binder_worklist_empty_ilocked(&thread->proc->todo));
960}
961
962static bool binder_has_work(struct binder_thread *thread, bool do_proc_work)
963{
964 bool has_work;
965
966 binder_inner_proc_lock(thread->proc);
967 has_work = binder_has_work_ilocked(thread, do_proc_work);
968 binder_inner_proc_unlock(thread->proc);
969
970 return has_work;
971}
972
973static bool binder_available_for_proc_work_ilocked(struct binder_thread *thread)
974{
975 return !thread->transaction_stack &&
976 binder_worklist_empty_ilocked(&thread->todo) &&
977 (thread->looper & (BINDER_LOOPER_STATE_ENTERED |
978 BINDER_LOOPER_STATE_REGISTERED));
979}
980
981static void binder_wakeup_poll_threads_ilocked(struct binder_proc *proc,
982 bool sync)
983{
984 struct rb_node *n;
985 struct binder_thread *thread;
986
987 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
988 thread = rb_entry(n, struct binder_thread, rb_node);
989 if (thread->looper & BINDER_LOOPER_STATE_POLL &&
990 binder_available_for_proc_work_ilocked(thread)) {
991 if (sync)
992 wake_up_interruptible_sync(&thread->wait);
993 else
994 wake_up_interruptible(&thread->wait);
995 }
996 }
997}
998
Martijn Coenen053be422017-06-06 15:17:46 -0700999/**
1000 * binder_select_thread_ilocked() - selects a thread for doing proc work.
1001 * @proc: process to select a thread from
1002 *
1003 * Note that calling this function moves the thread off the waiting_threads
1004 * list, so it can only be woken up by the caller of this function, or a
1005 * signal. Therefore, callers *should* always wake up the thread this function
1006 * returns.
1007 *
1008 * Return: If there's a thread currently waiting for process work,
1009 * returns that thread. Otherwise returns NULL.
1010 */
1011static struct binder_thread *
1012binder_select_thread_ilocked(struct binder_proc *proc)
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001013{
1014 struct binder_thread *thread;
1015
1016 BUG_ON(!spin_is_locked(&proc->inner_lock));
1017 thread = list_first_entry_or_null(&proc->waiting_threads,
1018 struct binder_thread,
1019 waiting_thread_node);
1020
Martijn Coenen053be422017-06-06 15:17:46 -07001021 if (thread)
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001022 list_del_init(&thread->waiting_thread_node);
Martijn Coenen053be422017-06-06 15:17:46 -07001023
1024 return thread;
1025}
1026
1027/**
1028 * binder_wakeup_thread_ilocked() - wakes up a thread for doing proc work.
1029 * @proc: process to wake up a thread in
1030 * @thread: specific thread to wake-up (may be NULL)
1031 * @sync: whether to do a synchronous wake-up
1032 *
1033 * This function wakes up a thread in the @proc process.
1034 * The caller may provide a specific thread to wake-up in
1035 * the @thread parameter. If @thread is NULL, this function
1036 * will wake up threads that have called poll().
1037 *
1038 * Note that for this function to work as expected, callers
1039 * should first call binder_select_thread() to find a thread
1040 * to handle the work (if they don't have a thread already),
1041 * and pass the result into the @thread parameter.
1042 */
1043static void binder_wakeup_thread_ilocked(struct binder_proc *proc,
1044 struct binder_thread *thread,
1045 bool sync)
1046{
1047 BUG_ON(!spin_is_locked(&proc->inner_lock));
1048
1049 if (thread) {
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001050 if (sync)
1051 wake_up_interruptible_sync(&thread->wait);
1052 else
1053 wake_up_interruptible(&thread->wait);
1054 return;
1055 }
1056
1057 /* Didn't find a thread waiting for proc work; this can happen
1058 * in two scenarios:
1059 * 1. All threads are busy handling transactions
1060 * In that case, one of those threads should call back into
1061 * the kernel driver soon and pick up this work.
1062 * 2. Threads are using the (e)poll interface, in which case
1063 * they may be blocked on the waitqueue without having been
1064 * added to waiting_threads. For this case, we just iterate
1065 * over all threads not handling transaction work, and
1066 * wake them all up. We wake all because we don't know whether
1067 * a thread that called into (e)poll is handling non-binder
1068 * work currently.
1069 */
1070 binder_wakeup_poll_threads_ilocked(proc, sync);
1071}
1072
Martijn Coenen053be422017-06-06 15:17:46 -07001073static void binder_wakeup_proc_ilocked(struct binder_proc *proc)
1074{
1075 struct binder_thread *thread = binder_select_thread_ilocked(proc);
1076
1077 binder_wakeup_thread_ilocked(proc, thread, /* sync = */false);
1078}
1079
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001080static bool is_rt_policy(int policy)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001081{
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001082 return policy == SCHED_FIFO || policy == SCHED_RR;
1083}
Seunghun Lee10f62862014-05-01 01:30:23 +09001084
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001085static bool is_fair_policy(int policy)
1086{
1087 return policy == SCHED_NORMAL || policy == SCHED_BATCH;
1088}
1089
1090static bool binder_supported_policy(int policy)
1091{
1092 return is_fair_policy(policy) || is_rt_policy(policy);
1093}
1094
1095static int to_userspace_prio(int policy, int kernel_priority)
1096{
1097 if (is_fair_policy(policy))
1098 return PRIO_TO_NICE(kernel_priority);
1099 else
1100 return MAX_USER_RT_PRIO - 1 - kernel_priority;
1101}
1102
1103static int to_kernel_prio(int policy, int user_priority)
1104{
1105 if (is_fair_policy(policy))
1106 return NICE_TO_PRIO(user_priority);
1107 else
1108 return MAX_USER_RT_PRIO - 1 - user_priority;
1109}
1110
1111static void binder_set_priority(struct task_struct *task,
1112 struct binder_priority desired)
1113{
1114 int priority; /* user-space prio value */
1115 bool has_cap_nice;
1116 unsigned int policy = desired.sched_policy;
1117
1118 if (task->policy == policy && task->normal_prio == desired.prio)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001119 return;
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001120
1121 has_cap_nice = has_capability_noaudit(task, CAP_SYS_NICE);
1122
1123 priority = to_userspace_prio(policy, desired.prio);
1124
1125 if (is_rt_policy(policy) && !has_cap_nice) {
1126 long max_rtprio = task_rlimit(task, RLIMIT_RTPRIO);
1127
1128 if (max_rtprio == 0) {
1129 policy = SCHED_NORMAL;
1130 priority = MIN_NICE;
1131 } else if (priority > max_rtprio) {
1132 priority = max_rtprio;
1133 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001134 }
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001135
1136 if (is_fair_policy(policy) && !has_cap_nice) {
1137 long min_nice = rlimit_to_nice(task_rlimit(task, RLIMIT_NICE));
1138
1139 if (min_nice > MAX_NICE) {
1140 binder_user_error("%d RLIMIT_NICE not set\n",
1141 task->pid);
1142 return;
1143 } else if (priority < min_nice) {
1144 priority = min_nice;
1145 }
1146 }
1147
1148 if (policy != desired.sched_policy ||
1149 to_kernel_prio(policy, priority) != desired.prio)
1150 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
1151 "%d: priority %d not allowed, using %d instead\n",
1152 task->pid, desired.prio,
1153 to_kernel_prio(policy, priority));
1154
1155 /* Set the actual priority */
1156 if (task->policy != policy || is_rt_policy(policy)) {
1157 struct sched_param params;
1158
1159 params.sched_priority = is_rt_policy(policy) ? priority : 0;
1160
1161 sched_setscheduler_nocheck(task,
1162 policy | SCHED_RESET_ON_FORK,
1163 &params);
1164 }
1165 if (is_fair_policy(policy))
1166 set_user_nice(task, priority);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001167}
1168
Martijn Coenen07a30fe2017-06-07 10:02:12 -07001169static void binder_transaction_priority(struct task_struct *task,
1170 struct binder_transaction *t,
Martijn Coenenc46810c2017-06-23 10:13:43 -07001171 struct binder_priority node_prio,
1172 bool inherit_rt)
Martijn Coenen07a30fe2017-06-07 10:02:12 -07001173{
1174 struct binder_priority desired_prio;
1175
1176 if (t->set_priority_called)
1177 return;
1178
1179 t->set_priority_called = true;
1180 t->saved_priority.sched_policy = task->policy;
1181 t->saved_priority.prio = task->normal_prio;
1182
Martijn Coenenc46810c2017-06-23 10:13:43 -07001183 if (!inherit_rt && is_rt_policy(desired_prio.sched_policy)) {
1184 desired_prio.prio = NICE_TO_PRIO(0);
1185 desired_prio.sched_policy = SCHED_NORMAL;
1186 } else {
1187 desired_prio.prio = t->priority.prio;
1188 desired_prio.sched_policy = t->priority.sched_policy;
1189 }
Martijn Coenen07a30fe2017-06-07 10:02:12 -07001190
1191 if (node_prio.prio < t->priority.prio ||
1192 (node_prio.prio == t->priority.prio &&
1193 node_prio.sched_policy == SCHED_FIFO)) {
1194 /*
1195 * In case the minimum priority on the node is
1196 * higher (lower value), use that priority. If
1197 * the priority is the same, but the node uses
1198 * SCHED_FIFO, prefer SCHED_FIFO, since it can
1199 * run unbounded, unlike SCHED_RR.
1200 */
1201 desired_prio = node_prio;
1202 }
1203
1204 binder_set_priority(task, desired_prio);
1205}
1206
Todd Kjos425d23f2017-06-12 12:07:26 -07001207static struct binder_node *binder_get_node_ilocked(struct binder_proc *proc,
1208 binder_uintptr_t ptr)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001209{
1210 struct rb_node *n = proc->nodes.rb_node;
1211 struct binder_node *node;
1212
Todd Kjos425d23f2017-06-12 12:07:26 -07001213 BUG_ON(!spin_is_locked(&proc->inner_lock));
1214
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001215 while (n) {
1216 node = rb_entry(n, struct binder_node, rb_node);
1217
1218 if (ptr < node->ptr)
1219 n = n->rb_left;
1220 else if (ptr > node->ptr)
1221 n = n->rb_right;
Todd Kjosf22abc72017-05-09 11:08:05 -07001222 else {
1223 /*
1224 * take an implicit weak reference
1225 * to ensure node stays alive until
1226 * call to binder_put_node()
1227 */
Todd Kjos425d23f2017-06-12 12:07:26 -07001228 binder_inc_node_tmpref_ilocked(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001229 return node;
Todd Kjosf22abc72017-05-09 11:08:05 -07001230 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001231 }
1232 return NULL;
1233}
1234
Todd Kjos425d23f2017-06-12 12:07:26 -07001235static struct binder_node *binder_get_node(struct binder_proc *proc,
1236 binder_uintptr_t ptr)
1237{
1238 struct binder_node *node;
1239
1240 binder_inner_proc_lock(proc);
1241 node = binder_get_node_ilocked(proc, ptr);
1242 binder_inner_proc_unlock(proc);
1243 return node;
1244}
1245
1246static struct binder_node *binder_init_node_ilocked(
1247 struct binder_proc *proc,
1248 struct binder_node *new_node,
1249 struct flat_binder_object *fp)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001250{
1251 struct rb_node **p = &proc->nodes.rb_node;
1252 struct rb_node *parent = NULL;
1253 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001254 binder_uintptr_t ptr = fp ? fp->binder : 0;
1255 binder_uintptr_t cookie = fp ? fp->cookie : 0;
1256 __u32 flags = fp ? fp->flags : 0;
Martijn Coenen6aac9792017-06-07 09:29:14 -07001257 s8 priority;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001258
Todd Kjos425d23f2017-06-12 12:07:26 -07001259 BUG_ON(!spin_is_locked(&proc->inner_lock));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001260 while (*p) {
Todd Kjos425d23f2017-06-12 12:07:26 -07001261
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001262 parent = *p;
1263 node = rb_entry(parent, struct binder_node, rb_node);
1264
1265 if (ptr < node->ptr)
1266 p = &(*p)->rb_left;
1267 else if (ptr > node->ptr)
1268 p = &(*p)->rb_right;
Todd Kjos425d23f2017-06-12 12:07:26 -07001269 else {
1270 /*
1271 * A matching node is already in
1272 * the rb tree. Abandon the init
1273 * and return it.
1274 */
1275 binder_inc_node_tmpref_ilocked(node);
1276 return node;
1277 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001278 }
Todd Kjos425d23f2017-06-12 12:07:26 -07001279 node = new_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001280 binder_stats_created(BINDER_STAT_NODE);
Todd Kjosf22abc72017-05-09 11:08:05 -07001281 node->tmp_refs++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001282 rb_link_node(&node->rb_node, parent, p);
1283 rb_insert_color(&node->rb_node, &proc->nodes);
Todd Kjosc4bd08b2017-05-25 10:56:00 -07001284 node->debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001285 node->proc = proc;
1286 node->ptr = ptr;
1287 node->cookie = cookie;
1288 node->work.type = BINDER_WORK_NODE;
Martijn Coenen6aac9792017-06-07 09:29:14 -07001289 priority = flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
1290 node->sched_policy = (flags & FLAT_BINDER_FLAG_PRIORITY_MASK) >>
1291 FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT;
1292 node->min_priority = to_kernel_prio(node->sched_policy, priority);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001293 node->accept_fds = !!(flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
Martijn Coenenc46810c2017-06-23 10:13:43 -07001294 node->inherit_rt = !!(flags & FLAT_BINDER_FLAG_INHERIT_RT);
Todd Kjosfc7a7e22017-05-29 16:44:24 -07001295 spin_lock_init(&node->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001296 INIT_LIST_HEAD(&node->work.entry);
1297 INIT_LIST_HEAD(&node->async_todo);
1298 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001299 "%d:%d node %d u%016llx c%016llx created\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001300 proc->pid, current->pid, node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001301 (u64)node->ptr, (u64)node->cookie);
Todd Kjos425d23f2017-06-12 12:07:26 -07001302
1303 return node;
1304}
1305
1306static struct binder_node *binder_new_node(struct binder_proc *proc,
1307 struct flat_binder_object *fp)
1308{
1309 struct binder_node *node;
1310 struct binder_node *new_node = kzalloc(sizeof(*node), GFP_KERNEL);
1311
1312 if (!new_node)
1313 return NULL;
1314 binder_inner_proc_lock(proc);
1315 node = binder_init_node_ilocked(proc, new_node, fp);
1316 binder_inner_proc_unlock(proc);
1317 if (node != new_node)
1318 /*
1319 * The node was already added by another thread
1320 */
1321 kfree(new_node);
1322
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001323 return node;
1324}
1325
Todd Kjose7f23ed2017-03-21 13:06:01 -07001326static void binder_free_node(struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001327{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001328 kfree(node);
1329 binder_stats_deleted(BINDER_STAT_NODE);
1330}
1331
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001332static int binder_inc_node_nilocked(struct binder_node *node, int strong,
1333 int internal,
1334 struct list_head *target_list)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001335{
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001336 struct binder_proc *proc = node->proc;
1337
1338 BUG_ON(!spin_is_locked(&node->lock));
1339 if (proc)
1340 BUG_ON(!spin_is_locked(&proc->inner_lock));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001341 if (strong) {
1342 if (internal) {
1343 if (target_list == NULL &&
1344 node->internal_strong_refs == 0 &&
Martijn Coenen0b3311e2016-09-30 15:51:48 +02001345 !(node->proc &&
1346 node == node->proc->context->
1347 binder_context_mgr_node &&
1348 node->has_strong_ref)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301349 pr_err("invalid inc strong node for %d\n",
1350 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001351 return -EINVAL;
1352 }
1353 node->internal_strong_refs++;
1354 } else
1355 node->local_strong_refs++;
1356 if (!node->has_strong_ref && target_list) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001357 binder_dequeue_work_ilocked(&node->work);
1358 binder_enqueue_work_ilocked(&node->work, target_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001359 }
1360 } else {
1361 if (!internal)
1362 node->local_weak_refs++;
1363 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
1364 if (target_list == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301365 pr_err("invalid inc weak node for %d\n",
1366 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001367 return -EINVAL;
1368 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001369 binder_enqueue_work_ilocked(&node->work, target_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001370 }
1371 }
1372 return 0;
1373}
1374
Todd Kjose7f23ed2017-03-21 13:06:01 -07001375static int binder_inc_node(struct binder_node *node, int strong, int internal,
1376 struct list_head *target_list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001377{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001378 int ret;
1379
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001380 binder_node_inner_lock(node);
1381 ret = binder_inc_node_nilocked(node, strong, internal, target_list);
1382 binder_node_inner_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001383
1384 return ret;
1385}
1386
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001387static bool binder_dec_node_nilocked(struct binder_node *node,
1388 int strong, int internal)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001389{
1390 struct binder_proc *proc = node->proc;
1391
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001392 BUG_ON(!spin_is_locked(&node->lock));
Todd Kjose7f23ed2017-03-21 13:06:01 -07001393 if (proc)
1394 BUG_ON(!spin_is_locked(&proc->inner_lock));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001395 if (strong) {
1396 if (internal)
1397 node->internal_strong_refs--;
1398 else
1399 node->local_strong_refs--;
1400 if (node->local_strong_refs || node->internal_strong_refs)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001401 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001402 } else {
1403 if (!internal)
1404 node->local_weak_refs--;
Todd Kjosf22abc72017-05-09 11:08:05 -07001405 if (node->local_weak_refs || node->tmp_refs ||
1406 !hlist_empty(&node->refs))
Todd Kjose7f23ed2017-03-21 13:06:01 -07001407 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001408 }
Todd Kjose7f23ed2017-03-21 13:06:01 -07001409
1410 if (proc && (node->has_strong_ref || node->has_weak_ref)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001411 if (list_empty(&node->work.entry)) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001412 binder_enqueue_work_ilocked(&node->work, &proc->todo);
Martijn Coenen053be422017-06-06 15:17:46 -07001413 binder_wakeup_proc_ilocked(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001414 }
1415 } else {
1416 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
Todd Kjosf22abc72017-05-09 11:08:05 -07001417 !node->local_weak_refs && !node->tmp_refs) {
Todd Kjose7f23ed2017-03-21 13:06:01 -07001418 if (proc) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001419 binder_dequeue_work_ilocked(&node->work);
1420 rb_erase(&node->rb_node, &proc->nodes);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001421 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301422 "refless node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001423 node->debug_id);
1424 } else {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001425 BUG_ON(!list_empty(&node->work.entry));
Todd Kjos8d9f6f32016-10-17 12:33:15 -07001426 spin_lock(&binder_dead_nodes_lock);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001427 /*
1428 * tmp_refs could have changed so
1429 * check it again
1430 */
1431 if (node->tmp_refs) {
1432 spin_unlock(&binder_dead_nodes_lock);
1433 return false;
1434 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001435 hlist_del(&node->dead_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07001436 spin_unlock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001437 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301438 "dead node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001439 node->debug_id);
1440 }
Todd Kjose7f23ed2017-03-21 13:06:01 -07001441 return true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001442 }
1443 }
Todd Kjose7f23ed2017-03-21 13:06:01 -07001444 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001445}
1446
Todd Kjose7f23ed2017-03-21 13:06:01 -07001447static void binder_dec_node(struct binder_node *node, int strong, int internal)
1448{
1449 bool free_node;
1450
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001451 binder_node_inner_lock(node);
1452 free_node = binder_dec_node_nilocked(node, strong, internal);
1453 binder_node_inner_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001454 if (free_node)
1455 binder_free_node(node);
1456}
1457
1458static void binder_inc_node_tmpref_ilocked(struct binder_node *node)
Todd Kjosf22abc72017-05-09 11:08:05 -07001459{
1460 /*
1461 * No call to binder_inc_node() is needed since we
1462 * don't need to inform userspace of any changes to
1463 * tmp_refs
1464 */
1465 node->tmp_refs++;
1466}
1467
1468/**
Todd Kjose7f23ed2017-03-21 13:06:01 -07001469 * binder_inc_node_tmpref() - take a temporary reference on node
1470 * @node: node to reference
1471 *
1472 * Take reference on node to prevent the node from being freed
1473 * while referenced only by a local variable. The inner lock is
1474 * needed to serialize with the node work on the queue (which
1475 * isn't needed after the node is dead). If the node is dead
1476 * (node->proc is NULL), use binder_dead_nodes_lock to protect
1477 * node->tmp_refs against dead-node-only cases where the node
1478 * lock cannot be acquired (eg traversing the dead node list to
1479 * print nodes)
1480 */
1481static void binder_inc_node_tmpref(struct binder_node *node)
1482{
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001483 binder_node_lock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001484 if (node->proc)
1485 binder_inner_proc_lock(node->proc);
1486 else
1487 spin_lock(&binder_dead_nodes_lock);
1488 binder_inc_node_tmpref_ilocked(node);
1489 if (node->proc)
1490 binder_inner_proc_unlock(node->proc);
1491 else
1492 spin_unlock(&binder_dead_nodes_lock);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001493 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001494}
1495
1496/**
Todd Kjosf22abc72017-05-09 11:08:05 -07001497 * binder_dec_node_tmpref() - remove a temporary reference on node
1498 * @node: node to reference
1499 *
1500 * Release temporary reference on node taken via binder_inc_node_tmpref()
1501 */
1502static void binder_dec_node_tmpref(struct binder_node *node)
1503{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001504 bool free_node;
1505
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001506 binder_node_inner_lock(node);
1507 if (!node->proc)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001508 spin_lock(&binder_dead_nodes_lock);
Todd Kjosf22abc72017-05-09 11:08:05 -07001509 node->tmp_refs--;
1510 BUG_ON(node->tmp_refs < 0);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001511 if (!node->proc)
1512 spin_unlock(&binder_dead_nodes_lock);
Todd Kjosf22abc72017-05-09 11:08:05 -07001513 /*
1514 * Call binder_dec_node() to check if all refcounts are 0
1515 * and cleanup is needed. Calling with strong=0 and internal=1
1516 * causes no actual reference to be released in binder_dec_node().
1517 * If that changes, a change is needed here too.
1518 */
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001519 free_node = binder_dec_node_nilocked(node, 0, 1);
1520 binder_node_inner_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001521 if (free_node)
1522 binder_free_node(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07001523}
1524
1525static void binder_put_node(struct binder_node *node)
1526{
1527 binder_dec_node_tmpref(node);
1528}
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001529
Todd Kjos5346bf32016-10-20 16:43:34 -07001530static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc,
1531 u32 desc, bool need_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001532{
1533 struct rb_node *n = proc->refs_by_desc.rb_node;
1534 struct binder_ref *ref;
1535
1536 while (n) {
1537 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1538
Todd Kjosb0117bb2017-05-08 09:16:27 -07001539 if (desc < ref->data.desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001540 n = n->rb_left;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001541 } else if (desc > ref->data.desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001542 n = n->rb_right;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001543 } else if (need_strong_ref && !ref->data.strong) {
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001544 binder_user_error("tried to use weak ref as strong ref\n");
1545 return NULL;
1546 } else {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001547 return ref;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001548 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001549 }
1550 return NULL;
1551}
1552
Todd Kjosb0117bb2017-05-08 09:16:27 -07001553/**
Todd Kjos5346bf32016-10-20 16:43:34 -07001554 * binder_get_ref_for_node_olocked() - get the ref associated with given node
Todd Kjosb0117bb2017-05-08 09:16:27 -07001555 * @proc: binder_proc that owns the ref
1556 * @node: binder_node of target
1557 * @new_ref: newly allocated binder_ref to be initialized or %NULL
1558 *
1559 * Look up the ref for the given node and return it if it exists
1560 *
1561 * If it doesn't exist and the caller provides a newly allocated
1562 * ref, initialize the fields of the newly allocated ref and insert
1563 * into the given proc rb_trees and node refs list.
1564 *
1565 * Return: the ref for node. It is possible that another thread
1566 * allocated/initialized the ref first in which case the
1567 * returned ref would be different than the passed-in
1568 * new_ref. new_ref must be kfree'd by the caller in
1569 * this case.
1570 */
Todd Kjos5346bf32016-10-20 16:43:34 -07001571static struct binder_ref *binder_get_ref_for_node_olocked(
1572 struct binder_proc *proc,
1573 struct binder_node *node,
1574 struct binder_ref *new_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001575{
Todd Kjosb0117bb2017-05-08 09:16:27 -07001576 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001577 struct rb_node **p = &proc->refs_by_node.rb_node;
1578 struct rb_node *parent = NULL;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001579 struct binder_ref *ref;
1580 struct rb_node *n;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001581
1582 while (*p) {
1583 parent = *p;
1584 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1585
1586 if (node < ref->node)
1587 p = &(*p)->rb_left;
1588 else if (node > ref->node)
1589 p = &(*p)->rb_right;
1590 else
1591 return ref;
1592 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001593 if (!new_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001594 return NULL;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001595
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001596 binder_stats_created(BINDER_STAT_REF);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001597 new_ref->data.debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001598 new_ref->proc = proc;
1599 new_ref->node = node;
1600 rb_link_node(&new_ref->rb_node_node, parent, p);
1601 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1602
Todd Kjosb0117bb2017-05-08 09:16:27 -07001603 new_ref->data.desc = (node == context->binder_context_mgr_node) ? 0 : 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001604 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1605 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001606 if (ref->data.desc > new_ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001607 break;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001608 new_ref->data.desc = ref->data.desc + 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001609 }
1610
1611 p = &proc->refs_by_desc.rb_node;
1612 while (*p) {
1613 parent = *p;
1614 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1615
Todd Kjosb0117bb2017-05-08 09:16:27 -07001616 if (new_ref->data.desc < ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001617 p = &(*p)->rb_left;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001618 else if (new_ref->data.desc > ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001619 p = &(*p)->rb_right;
1620 else
1621 BUG();
1622 }
1623 rb_link_node(&new_ref->rb_node_desc, parent, p);
1624 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001625
1626 binder_node_lock(node);
Todd Kjos4cbe5752017-05-01 17:21:51 -07001627 hlist_add_head(&new_ref->node_entry, &node->refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001628
Todd Kjos4cbe5752017-05-01 17:21:51 -07001629 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1630 "%d new ref %d desc %d for node %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001631 proc->pid, new_ref->data.debug_id, new_ref->data.desc,
Todd Kjos4cbe5752017-05-01 17:21:51 -07001632 node->debug_id);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001633 binder_node_unlock(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001634 return new_ref;
1635}
1636
Todd Kjos5346bf32016-10-20 16:43:34 -07001637static void binder_cleanup_ref_olocked(struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001638{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001639 bool delete_node = false;
Todd Kjose7f23ed2017-03-21 13:06:01 -07001640
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001641 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301642 "%d delete ref %d desc %d for node %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001643 ref->proc->pid, ref->data.debug_id, ref->data.desc,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301644 ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001645
1646 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1647 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001648
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001649 binder_node_inner_lock(ref->node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001650 if (ref->data.strong)
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001651 binder_dec_node_nilocked(ref->node, 1, 1);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001652
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001653 hlist_del(&ref->node_entry);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001654 delete_node = binder_dec_node_nilocked(ref->node, 0, 1);
1655 binder_node_inner_unlock(ref->node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001656 /*
1657 * Clear ref->node unless we want the caller to free the node
1658 */
1659 if (!delete_node) {
1660 /*
1661 * The caller uses ref->node to determine
1662 * whether the node needs to be freed. Clear
1663 * it since the node is still alive.
1664 */
1665 ref->node = NULL;
1666 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001667
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001668 if (ref->death) {
1669 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301670 "%d delete ref %d desc %d has death notification\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001671 ref->proc->pid, ref->data.debug_id,
1672 ref->data.desc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001673 binder_dequeue_work(ref->proc, &ref->death->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001674 binder_stats_deleted(BINDER_STAT_DEATH);
1675 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001676 binder_stats_deleted(BINDER_STAT_REF);
1677}
1678
Todd Kjosb0117bb2017-05-08 09:16:27 -07001679/**
Todd Kjos5346bf32016-10-20 16:43:34 -07001680 * binder_inc_ref_olocked() - increment the ref for given handle
Todd Kjosb0117bb2017-05-08 09:16:27 -07001681 * @ref: ref to be incremented
1682 * @strong: if true, strong increment, else weak
1683 * @target_list: list to queue node work on
1684 *
Todd Kjos5346bf32016-10-20 16:43:34 -07001685 * Increment the ref. @ref->proc->outer_lock must be held on entry
Todd Kjosb0117bb2017-05-08 09:16:27 -07001686 *
1687 * Return: 0, if successful, else errno
1688 */
Todd Kjos5346bf32016-10-20 16:43:34 -07001689static int binder_inc_ref_olocked(struct binder_ref *ref, int strong,
1690 struct list_head *target_list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001691{
1692 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +09001693
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001694 if (strong) {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001695 if (ref->data.strong == 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001696 ret = binder_inc_node(ref->node, 1, 1, target_list);
1697 if (ret)
1698 return ret;
1699 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001700 ref->data.strong++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001701 } else {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001702 if (ref->data.weak == 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001703 ret = binder_inc_node(ref->node, 0, 1, target_list);
1704 if (ret)
1705 return ret;
1706 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001707 ref->data.weak++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001708 }
1709 return 0;
1710}
1711
Todd Kjosb0117bb2017-05-08 09:16:27 -07001712/**
1713 * binder_dec_ref() - dec the ref for given handle
1714 * @ref: ref to be decremented
1715 * @strong: if true, strong decrement, else weak
1716 *
1717 * Decrement the ref.
1718 *
Todd Kjosb0117bb2017-05-08 09:16:27 -07001719 * Return: true if ref is cleaned up and ready to be freed
1720 */
Todd Kjos5346bf32016-10-20 16:43:34 -07001721static bool binder_dec_ref_olocked(struct binder_ref *ref, int strong)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001722{
1723 if (strong) {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001724 if (ref->data.strong == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301725 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001726 ref->proc->pid, ref->data.debug_id,
1727 ref->data.desc, ref->data.strong,
1728 ref->data.weak);
1729 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001730 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001731 ref->data.strong--;
Todd Kjose7f23ed2017-03-21 13:06:01 -07001732 if (ref->data.strong == 0)
1733 binder_dec_node(ref->node, strong, 1);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001734 } else {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001735 if (ref->data.weak == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301736 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001737 ref->proc->pid, ref->data.debug_id,
1738 ref->data.desc, ref->data.strong,
1739 ref->data.weak);
1740 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001741 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001742 ref->data.weak--;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001743 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001744 if (ref->data.strong == 0 && ref->data.weak == 0) {
Todd Kjos5346bf32016-10-20 16:43:34 -07001745 binder_cleanup_ref_olocked(ref);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001746 return true;
1747 }
1748 return false;
1749}
1750
1751/**
1752 * binder_get_node_from_ref() - get the node from the given proc/desc
1753 * @proc: proc containing the ref
1754 * @desc: the handle associated with the ref
1755 * @need_strong_ref: if true, only return node if ref is strong
1756 * @rdata: the id/refcount data for the ref
1757 *
1758 * Given a proc and ref handle, return the associated binder_node
1759 *
1760 * Return: a binder_node or NULL if not found or not strong when strong required
1761 */
1762static struct binder_node *binder_get_node_from_ref(
1763 struct binder_proc *proc,
1764 u32 desc, bool need_strong_ref,
1765 struct binder_ref_data *rdata)
1766{
1767 struct binder_node *node;
1768 struct binder_ref *ref;
1769
Todd Kjos5346bf32016-10-20 16:43:34 -07001770 binder_proc_lock(proc);
1771 ref = binder_get_ref_olocked(proc, desc, need_strong_ref);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001772 if (!ref)
1773 goto err_no_ref;
1774 node = ref->node;
Todd Kjosf22abc72017-05-09 11:08:05 -07001775 /*
1776 * Take an implicit reference on the node to ensure
1777 * it stays alive until the call to binder_put_node()
1778 */
1779 binder_inc_node_tmpref(node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001780 if (rdata)
1781 *rdata = ref->data;
Todd Kjos5346bf32016-10-20 16:43:34 -07001782 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001783
1784 return node;
1785
1786err_no_ref:
Todd Kjos5346bf32016-10-20 16:43:34 -07001787 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001788 return NULL;
1789}
1790
1791/**
1792 * binder_free_ref() - free the binder_ref
1793 * @ref: ref to free
1794 *
Todd Kjose7f23ed2017-03-21 13:06:01 -07001795 * Free the binder_ref. Free the binder_node indicated by ref->node
1796 * (if non-NULL) and the binder_ref_death indicated by ref->death.
Todd Kjosb0117bb2017-05-08 09:16:27 -07001797 */
1798static void binder_free_ref(struct binder_ref *ref)
1799{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001800 if (ref->node)
1801 binder_free_node(ref->node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001802 kfree(ref->death);
1803 kfree(ref);
1804}
1805
1806/**
1807 * binder_update_ref_for_handle() - inc/dec the ref for given handle
1808 * @proc: proc containing the ref
1809 * @desc: the handle associated with the ref
1810 * @increment: true=inc reference, false=dec reference
1811 * @strong: true=strong reference, false=weak reference
1812 * @rdata: the id/refcount data for the ref
1813 *
1814 * Given a proc and ref handle, increment or decrement the ref
1815 * according to "increment" arg.
1816 *
1817 * Return: 0 if successful, else errno
1818 */
1819static int binder_update_ref_for_handle(struct binder_proc *proc,
1820 uint32_t desc, bool increment, bool strong,
1821 struct binder_ref_data *rdata)
1822{
1823 int ret = 0;
1824 struct binder_ref *ref;
1825 bool delete_ref = false;
1826
Todd Kjos5346bf32016-10-20 16:43:34 -07001827 binder_proc_lock(proc);
1828 ref = binder_get_ref_olocked(proc, desc, strong);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001829 if (!ref) {
1830 ret = -EINVAL;
1831 goto err_no_ref;
1832 }
1833 if (increment)
Todd Kjos5346bf32016-10-20 16:43:34 -07001834 ret = binder_inc_ref_olocked(ref, strong, NULL);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001835 else
Todd Kjos5346bf32016-10-20 16:43:34 -07001836 delete_ref = binder_dec_ref_olocked(ref, strong);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001837
1838 if (rdata)
1839 *rdata = ref->data;
Todd Kjos5346bf32016-10-20 16:43:34 -07001840 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001841
1842 if (delete_ref)
1843 binder_free_ref(ref);
1844 return ret;
1845
1846err_no_ref:
Todd Kjos5346bf32016-10-20 16:43:34 -07001847 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001848 return ret;
1849}
1850
1851/**
1852 * binder_dec_ref_for_handle() - dec the ref for given handle
1853 * @proc: proc containing the ref
1854 * @desc: the handle associated with the ref
1855 * @strong: true=strong reference, false=weak reference
1856 * @rdata: the id/refcount data for the ref
1857 *
1858 * Just calls binder_update_ref_for_handle() to decrement the ref.
1859 *
1860 * Return: 0 if successful, else errno
1861 */
1862static int binder_dec_ref_for_handle(struct binder_proc *proc,
1863 uint32_t desc, bool strong, struct binder_ref_data *rdata)
1864{
1865 return binder_update_ref_for_handle(proc, desc, false, strong, rdata);
1866}
1867
1868
1869/**
1870 * binder_inc_ref_for_node() - increment the ref for given proc/node
1871 * @proc: proc containing the ref
1872 * @node: target node
1873 * @strong: true=strong reference, false=weak reference
1874 * @target_list: worklist to use if node is incremented
1875 * @rdata: the id/refcount data for the ref
1876 *
1877 * Given a proc and node, increment the ref. Create the ref if it
1878 * doesn't already exist
1879 *
1880 * Return: 0 if successful, else errno
1881 */
1882static int binder_inc_ref_for_node(struct binder_proc *proc,
1883 struct binder_node *node,
1884 bool strong,
1885 struct list_head *target_list,
1886 struct binder_ref_data *rdata)
1887{
1888 struct binder_ref *ref;
1889 struct binder_ref *new_ref = NULL;
1890 int ret = 0;
1891
Todd Kjos5346bf32016-10-20 16:43:34 -07001892 binder_proc_lock(proc);
1893 ref = binder_get_ref_for_node_olocked(proc, node, NULL);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001894 if (!ref) {
Todd Kjos5346bf32016-10-20 16:43:34 -07001895 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001896 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1897 if (!new_ref)
1898 return -ENOMEM;
Todd Kjos5346bf32016-10-20 16:43:34 -07001899 binder_proc_lock(proc);
1900 ref = binder_get_ref_for_node_olocked(proc, node, new_ref);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001901 }
Todd Kjos5346bf32016-10-20 16:43:34 -07001902 ret = binder_inc_ref_olocked(ref, strong, target_list);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001903 *rdata = ref->data;
Todd Kjos5346bf32016-10-20 16:43:34 -07001904 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001905 if (new_ref && ref != new_ref)
1906 /*
1907 * Another thread created the ref first so
1908 * free the one we allocated
1909 */
1910 kfree(new_ref);
1911 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001912}
1913
Martijn Coenen995a36e2017-06-02 13:36:52 -07001914static void binder_pop_transaction_ilocked(struct binder_thread *target_thread,
1915 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001916{
Todd Kjos21ef40a2017-03-30 18:02:13 -07001917 BUG_ON(!target_thread);
Martijn Coenen995a36e2017-06-02 13:36:52 -07001918 BUG_ON(!spin_is_locked(&target_thread->proc->inner_lock));
Todd Kjos21ef40a2017-03-30 18:02:13 -07001919 BUG_ON(target_thread->transaction_stack != t);
1920 BUG_ON(target_thread->transaction_stack->from != target_thread);
1921 target_thread->transaction_stack =
1922 target_thread->transaction_stack->from_parent;
1923 t->from = NULL;
1924}
1925
Todd Kjos2f993e22017-05-12 14:42:55 -07001926/**
1927 * binder_thread_dec_tmpref() - decrement thread->tmp_ref
1928 * @thread: thread to decrement
1929 *
1930 * A thread needs to be kept alive while being used to create or
1931 * handle a transaction. binder_get_txn_from() is used to safely
1932 * extract t->from from a binder_transaction and keep the thread
1933 * indicated by t->from from being freed. When done with that
1934 * binder_thread, this function is called to decrement the
1935 * tmp_ref and free if appropriate (thread has been released
1936 * and no transaction being processed by the driver)
1937 */
1938static void binder_thread_dec_tmpref(struct binder_thread *thread)
1939{
1940 /*
1941 * atomic is used to protect the counter value while
1942 * it cannot reach zero or thread->is_dead is false
Todd Kjos2f993e22017-05-12 14:42:55 -07001943 */
Todd Kjosb4827902017-05-25 15:52:17 -07001944 binder_inner_proc_lock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07001945 atomic_dec(&thread->tmp_ref);
1946 if (thread->is_dead && !atomic_read(&thread->tmp_ref)) {
Todd Kjosb4827902017-05-25 15:52:17 -07001947 binder_inner_proc_unlock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07001948 binder_free_thread(thread);
1949 return;
1950 }
Todd Kjosb4827902017-05-25 15:52:17 -07001951 binder_inner_proc_unlock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07001952}
1953
1954/**
1955 * binder_proc_dec_tmpref() - decrement proc->tmp_ref
1956 * @proc: proc to decrement
1957 *
1958 * A binder_proc needs to be kept alive while being used to create or
1959 * handle a transaction. proc->tmp_ref is incremented when
1960 * creating a new transaction or the binder_proc is currently in-use
1961 * by threads that are being released. When done with the binder_proc,
1962 * this function is called to decrement the counter and free the
1963 * proc if appropriate (proc has been released, all threads have
1964 * been released and not currenly in-use to process a transaction).
1965 */
1966static void binder_proc_dec_tmpref(struct binder_proc *proc)
1967{
Todd Kjosb4827902017-05-25 15:52:17 -07001968 binder_inner_proc_lock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07001969 proc->tmp_ref--;
1970 if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) &&
1971 !proc->tmp_ref) {
Todd Kjosb4827902017-05-25 15:52:17 -07001972 binder_inner_proc_unlock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07001973 binder_free_proc(proc);
1974 return;
1975 }
Todd Kjosb4827902017-05-25 15:52:17 -07001976 binder_inner_proc_unlock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07001977}
1978
1979/**
1980 * binder_get_txn_from() - safely extract the "from" thread in transaction
1981 * @t: binder transaction for t->from
1982 *
1983 * Atomically return the "from" thread and increment the tmp_ref
1984 * count for the thread to ensure it stays alive until
1985 * binder_thread_dec_tmpref() is called.
1986 *
1987 * Return: the value of t->from
1988 */
1989static struct binder_thread *binder_get_txn_from(
1990 struct binder_transaction *t)
1991{
1992 struct binder_thread *from;
1993
1994 spin_lock(&t->lock);
1995 from = t->from;
1996 if (from)
1997 atomic_inc(&from->tmp_ref);
1998 spin_unlock(&t->lock);
1999 return from;
2000}
2001
Martijn Coenen995a36e2017-06-02 13:36:52 -07002002/**
2003 * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock
2004 * @t: binder transaction for t->from
2005 *
2006 * Same as binder_get_txn_from() except it also acquires the proc->inner_lock
2007 * to guarantee that the thread cannot be released while operating on it.
2008 * The caller must call binder_inner_proc_unlock() to release the inner lock
2009 * as well as call binder_dec_thread_txn() to release the reference.
2010 *
2011 * Return: the value of t->from
2012 */
2013static struct binder_thread *binder_get_txn_from_and_acq_inner(
2014 struct binder_transaction *t)
2015{
2016 struct binder_thread *from;
2017
2018 from = binder_get_txn_from(t);
2019 if (!from)
2020 return NULL;
2021 binder_inner_proc_lock(from->proc);
2022 if (t->from) {
2023 BUG_ON(from != t->from);
2024 return from;
2025 }
2026 binder_inner_proc_unlock(from->proc);
2027 binder_thread_dec_tmpref(from);
2028 return NULL;
2029}
2030
Todd Kjos21ef40a2017-03-30 18:02:13 -07002031static void binder_free_transaction(struct binder_transaction *t)
2032{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002033 if (t->buffer)
2034 t->buffer->transaction = NULL;
2035 kfree(t);
2036 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2037}
2038
2039static void binder_send_failed_reply(struct binder_transaction *t,
2040 uint32_t error_code)
2041{
2042 struct binder_thread *target_thread;
Lucas Tanured4ec15e2014-07-13 21:31:05 -03002043 struct binder_transaction *next;
Seunghun Lee10f62862014-05-01 01:30:23 +09002044
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002045 BUG_ON(t->flags & TF_ONE_WAY);
2046 while (1) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07002047 target_thread = binder_get_txn_from_and_acq_inner(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002048 if (target_thread) {
Todd Kjos858b8da2017-04-21 17:35:12 -07002049 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2050 "send failed reply for transaction %d to %d:%d\n",
2051 t->debug_id,
2052 target_thread->proc->pid,
2053 target_thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002054
Martijn Coenen995a36e2017-06-02 13:36:52 -07002055 binder_pop_transaction_ilocked(target_thread, t);
Todd Kjos858b8da2017-04-21 17:35:12 -07002056 if (target_thread->reply_error.cmd == BR_OK) {
2057 target_thread->reply_error.cmd = error_code;
Martijn Coenen995a36e2017-06-02 13:36:52 -07002058 binder_enqueue_work_ilocked(
Todd Kjos1c89e6b2016-10-20 10:33:00 -07002059 &target_thread->reply_error.work,
Todd Kjos858b8da2017-04-21 17:35:12 -07002060 &target_thread->todo);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002061 wake_up_interruptible(&target_thread->wait);
2062 } else {
Todd Kjos858b8da2017-04-21 17:35:12 -07002063 WARN(1, "Unexpected reply error: %u\n",
2064 target_thread->reply_error.cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002065 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07002066 binder_inner_proc_unlock(target_thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002067 binder_thread_dec_tmpref(target_thread);
Todd Kjos858b8da2017-04-21 17:35:12 -07002068 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002069 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002070 }
Lucas Tanured4ec15e2014-07-13 21:31:05 -03002071 next = t->from_parent;
2072
2073 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2074 "send failed reply for transaction %d, target dead\n",
2075 t->debug_id);
2076
Todd Kjos21ef40a2017-03-30 18:02:13 -07002077 binder_free_transaction(t);
Lucas Tanured4ec15e2014-07-13 21:31:05 -03002078 if (next == NULL) {
2079 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2080 "reply failed, no target thread at root\n");
2081 return;
2082 }
2083 t = next;
2084 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2085 "reply failed, no target thread -- retry %d\n",
2086 t->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002087 }
2088}
2089
Martijn Coenen00c80372016-07-13 12:06:49 +02002090/**
2091 * binder_validate_object() - checks for a valid metadata object in a buffer.
2092 * @buffer: binder_buffer that we're parsing.
2093 * @offset: offset in the buffer at which to validate an object.
2094 *
2095 * Return: If there's a valid metadata object at @offset in @buffer, the
2096 * size of that object. Otherwise, it returns zero.
2097 */
2098static size_t binder_validate_object(struct binder_buffer *buffer, u64 offset)
2099{
2100 /* Check if we can read a header first */
2101 struct binder_object_header *hdr;
2102 size_t object_size = 0;
2103
2104 if (offset > buffer->data_size - sizeof(*hdr) ||
2105 buffer->data_size < sizeof(*hdr) ||
2106 !IS_ALIGNED(offset, sizeof(u32)))
2107 return 0;
2108
2109 /* Ok, now see if we can read a complete object. */
2110 hdr = (struct binder_object_header *)(buffer->data + offset);
2111 switch (hdr->type) {
2112 case BINDER_TYPE_BINDER:
2113 case BINDER_TYPE_WEAK_BINDER:
2114 case BINDER_TYPE_HANDLE:
2115 case BINDER_TYPE_WEAK_HANDLE:
2116 object_size = sizeof(struct flat_binder_object);
2117 break;
2118 case BINDER_TYPE_FD:
2119 object_size = sizeof(struct binder_fd_object);
2120 break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002121 case BINDER_TYPE_PTR:
2122 object_size = sizeof(struct binder_buffer_object);
2123 break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002124 case BINDER_TYPE_FDA:
2125 object_size = sizeof(struct binder_fd_array_object);
2126 break;
Martijn Coenen00c80372016-07-13 12:06:49 +02002127 default:
2128 return 0;
2129 }
2130 if (offset <= buffer->data_size - object_size &&
2131 buffer->data_size >= object_size)
2132 return object_size;
2133 else
2134 return 0;
2135}
2136
Martijn Coenen5a6da532016-09-30 14:10:07 +02002137/**
2138 * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
2139 * @b: binder_buffer containing the object
2140 * @index: index in offset array at which the binder_buffer_object is
2141 * located
2142 * @start: points to the start of the offset array
2143 * @num_valid: the number of valid offsets in the offset array
2144 *
2145 * Return: If @index is within the valid range of the offset array
2146 * described by @start and @num_valid, and if there's a valid
2147 * binder_buffer_object at the offset found in index @index
2148 * of the offset array, that object is returned. Otherwise,
2149 * %NULL is returned.
2150 * Note that the offset found in index @index itself is not
2151 * verified; this function assumes that @num_valid elements
2152 * from @start were previously verified to have valid offsets.
2153 */
2154static struct binder_buffer_object *binder_validate_ptr(struct binder_buffer *b,
2155 binder_size_t index,
2156 binder_size_t *start,
2157 binder_size_t num_valid)
2158{
2159 struct binder_buffer_object *buffer_obj;
2160 binder_size_t *offp;
2161
2162 if (index >= num_valid)
2163 return NULL;
2164
2165 offp = start + index;
2166 buffer_obj = (struct binder_buffer_object *)(b->data + *offp);
2167 if (buffer_obj->hdr.type != BINDER_TYPE_PTR)
2168 return NULL;
2169
2170 return buffer_obj;
2171}
2172
2173/**
2174 * binder_validate_fixup() - validates pointer/fd fixups happen in order.
2175 * @b: transaction buffer
2176 * @objects_start start of objects buffer
2177 * @buffer: binder_buffer_object in which to fix up
2178 * @offset: start offset in @buffer to fix up
2179 * @last_obj: last binder_buffer_object that we fixed up in
2180 * @last_min_offset: minimum fixup offset in @last_obj
2181 *
2182 * Return: %true if a fixup in buffer @buffer at offset @offset is
2183 * allowed.
2184 *
2185 * For safety reasons, we only allow fixups inside a buffer to happen
2186 * at increasing offsets; additionally, we only allow fixup on the last
2187 * buffer object that was verified, or one of its parents.
2188 *
2189 * Example of what is allowed:
2190 *
2191 * A
2192 * B (parent = A, offset = 0)
2193 * C (parent = A, offset = 16)
2194 * D (parent = C, offset = 0)
2195 * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
2196 *
2197 * Examples of what is not allowed:
2198 *
2199 * Decreasing offsets within the same parent:
2200 * A
2201 * C (parent = A, offset = 16)
2202 * B (parent = A, offset = 0) // decreasing offset within A
2203 *
2204 * Referring to a parent that wasn't the last object or any of its parents:
2205 * A
2206 * B (parent = A, offset = 0)
2207 * C (parent = A, offset = 0)
2208 * C (parent = A, offset = 16)
2209 * D (parent = B, offset = 0) // B is not A or any of A's parents
2210 */
2211static bool binder_validate_fixup(struct binder_buffer *b,
2212 binder_size_t *objects_start,
2213 struct binder_buffer_object *buffer,
2214 binder_size_t fixup_offset,
2215 struct binder_buffer_object *last_obj,
2216 binder_size_t last_min_offset)
2217{
2218 if (!last_obj) {
2219 /* Nothing to fix up in */
2220 return false;
2221 }
2222
2223 while (last_obj != buffer) {
2224 /*
2225 * Safe to retrieve the parent of last_obj, since it
2226 * was already previously verified by the driver.
2227 */
2228 if ((last_obj->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0)
2229 return false;
2230 last_min_offset = last_obj->parent_offset + sizeof(uintptr_t);
2231 last_obj = (struct binder_buffer_object *)
2232 (b->data + *(objects_start + last_obj->parent));
2233 }
2234 return (fixup_offset >= last_min_offset);
2235}
2236
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002237static void binder_transaction_buffer_release(struct binder_proc *proc,
2238 struct binder_buffer *buffer,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002239 binder_size_t *failed_at)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002240{
Martijn Coenen5a6da532016-09-30 14:10:07 +02002241 binder_size_t *offp, *off_start, *off_end;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002242 int debug_id = buffer->debug_id;
2243
2244 binder_debug(BINDER_DEBUG_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302245 "%d buffer release %d, size %zd-%zd, failed at %p\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002246 proc->pid, buffer->debug_id,
2247 buffer->data_size, buffer->offsets_size, failed_at);
2248
2249 if (buffer->target_node)
2250 binder_dec_node(buffer->target_node, 1, 0);
2251
Martijn Coenen5a6da532016-09-30 14:10:07 +02002252 off_start = (binder_size_t *)(buffer->data +
2253 ALIGN(buffer->data_size, sizeof(void *)));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002254 if (failed_at)
2255 off_end = failed_at;
2256 else
Martijn Coenen5a6da532016-09-30 14:10:07 +02002257 off_end = (void *)off_start + buffer->offsets_size;
2258 for (offp = off_start; offp < off_end; offp++) {
Martijn Coenen00c80372016-07-13 12:06:49 +02002259 struct binder_object_header *hdr;
2260 size_t object_size = binder_validate_object(buffer, *offp);
Seunghun Lee10f62862014-05-01 01:30:23 +09002261
Martijn Coenen00c80372016-07-13 12:06:49 +02002262 if (object_size == 0) {
2263 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
Arve Hjønnevågda498892014-02-21 14:40:26 -08002264 debug_id, (u64)*offp, buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002265 continue;
2266 }
Martijn Coenen00c80372016-07-13 12:06:49 +02002267 hdr = (struct binder_object_header *)(buffer->data + *offp);
2268 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002269 case BINDER_TYPE_BINDER:
2270 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenen00c80372016-07-13 12:06:49 +02002271 struct flat_binder_object *fp;
2272 struct binder_node *node;
Seunghun Lee10f62862014-05-01 01:30:23 +09002273
Martijn Coenen00c80372016-07-13 12:06:49 +02002274 fp = to_flat_binder_object(hdr);
2275 node = binder_get_node(proc, fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002276 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002277 pr_err("transaction release %d bad node %016llx\n",
2278 debug_id, (u64)fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002279 break;
2280 }
2281 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002282 " node %d u%016llx\n",
2283 node->debug_id, (u64)node->ptr);
Martijn Coenen00c80372016-07-13 12:06:49 +02002284 binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER,
2285 0);
Todd Kjosf22abc72017-05-09 11:08:05 -07002286 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002287 } break;
2288 case BINDER_TYPE_HANDLE:
2289 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenen00c80372016-07-13 12:06:49 +02002290 struct flat_binder_object *fp;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002291 struct binder_ref_data rdata;
2292 int ret;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02002293
Martijn Coenen00c80372016-07-13 12:06:49 +02002294 fp = to_flat_binder_object(hdr);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002295 ret = binder_dec_ref_for_handle(proc, fp->handle,
2296 hdr->type == BINDER_TYPE_HANDLE, &rdata);
2297
2298 if (ret) {
2299 pr_err("transaction release %d bad handle %d, ret = %d\n",
2300 debug_id, fp->handle, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002301 break;
2302 }
2303 binder_debug(BINDER_DEBUG_TRANSACTION,
Todd Kjosb0117bb2017-05-08 09:16:27 -07002304 " ref %d desc %d\n",
2305 rdata.debug_id, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002306 } break;
2307
Martijn Coenen00c80372016-07-13 12:06:49 +02002308 case BINDER_TYPE_FD: {
2309 struct binder_fd_object *fp = to_binder_fd_object(hdr);
2310
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002311 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen00c80372016-07-13 12:06:49 +02002312 " fd %d\n", fp->fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002313 if (failed_at)
Martijn Coenen00c80372016-07-13 12:06:49 +02002314 task_close_fd(proc, fp->fd);
2315 } break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002316 case BINDER_TYPE_PTR:
2317 /*
2318 * Nothing to do here, this will get cleaned up when the
2319 * transaction buffer gets freed
2320 */
2321 break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002322 case BINDER_TYPE_FDA: {
2323 struct binder_fd_array_object *fda;
2324 struct binder_buffer_object *parent;
2325 uintptr_t parent_buffer;
2326 u32 *fd_array;
2327 size_t fd_index;
2328 binder_size_t fd_buf_size;
2329
2330 fda = to_binder_fd_array_object(hdr);
2331 parent = binder_validate_ptr(buffer, fda->parent,
2332 off_start,
2333 offp - off_start);
2334 if (!parent) {
2335 pr_err("transaction release %d bad parent offset",
2336 debug_id);
2337 continue;
2338 }
2339 /*
2340 * Since the parent was already fixed up, convert it
2341 * back to kernel address space to access it
2342 */
2343 parent_buffer = parent->buffer -
Todd Kjosd325d372016-10-10 10:40:53 -07002344 binder_alloc_get_user_buffer_offset(
2345 &proc->alloc);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002346
2347 fd_buf_size = sizeof(u32) * fda->num_fds;
2348 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2349 pr_err("transaction release %d invalid number of fds (%lld)\n",
2350 debug_id, (u64)fda->num_fds);
2351 continue;
2352 }
2353 if (fd_buf_size > parent->length ||
2354 fda->parent_offset > parent->length - fd_buf_size) {
2355 /* No space for all file descriptors here. */
2356 pr_err("transaction release %d not enough space for %lld fds in buffer\n",
2357 debug_id, (u64)fda->num_fds);
2358 continue;
2359 }
2360 fd_array = (u32 *)(parent_buffer + fda->parent_offset);
2361 for (fd_index = 0; fd_index < fda->num_fds; fd_index++)
2362 task_close_fd(proc, fd_array[fd_index]);
2363 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002364 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01002365 pr_err("transaction release %d bad object type %x\n",
Martijn Coenen00c80372016-07-13 12:06:49 +02002366 debug_id, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002367 break;
2368 }
2369 }
2370}
2371
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002372static int binder_translate_binder(struct flat_binder_object *fp,
2373 struct binder_transaction *t,
2374 struct binder_thread *thread)
2375{
2376 struct binder_node *node;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002377 struct binder_proc *proc = thread->proc;
2378 struct binder_proc *target_proc = t->to_proc;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002379 struct binder_ref_data rdata;
Todd Kjosf22abc72017-05-09 11:08:05 -07002380 int ret = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002381
2382 node = binder_get_node(proc, fp->binder);
2383 if (!node) {
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002384 node = binder_new_node(proc, fp);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002385 if (!node)
2386 return -ENOMEM;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002387 }
2388 if (fp->cookie != node->cookie) {
2389 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
2390 proc->pid, thread->pid, (u64)fp->binder,
2391 node->debug_id, (u64)fp->cookie,
2392 (u64)node->cookie);
Todd Kjosf22abc72017-05-09 11:08:05 -07002393 ret = -EINVAL;
2394 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002395 }
Todd Kjosf22abc72017-05-09 11:08:05 -07002396 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2397 ret = -EPERM;
2398 goto done;
2399 }
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002400
Todd Kjosb0117bb2017-05-08 09:16:27 -07002401 ret = binder_inc_ref_for_node(target_proc, node,
2402 fp->hdr.type == BINDER_TYPE_BINDER,
2403 &thread->todo, &rdata);
2404 if (ret)
Todd Kjosf22abc72017-05-09 11:08:05 -07002405 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002406
2407 if (fp->hdr.type == BINDER_TYPE_BINDER)
2408 fp->hdr.type = BINDER_TYPE_HANDLE;
2409 else
2410 fp->hdr.type = BINDER_TYPE_WEAK_HANDLE;
2411 fp->binder = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002412 fp->handle = rdata.desc;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002413 fp->cookie = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002414
Todd Kjosb0117bb2017-05-08 09:16:27 -07002415 trace_binder_transaction_node_to_ref(t, node, &rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002416 binder_debug(BINDER_DEBUG_TRANSACTION,
2417 " node %d u%016llx -> ref %d desc %d\n",
2418 node->debug_id, (u64)node->ptr,
Todd Kjosb0117bb2017-05-08 09:16:27 -07002419 rdata.debug_id, rdata.desc);
Todd Kjosf22abc72017-05-09 11:08:05 -07002420done:
2421 binder_put_node(node);
2422 return ret;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002423}
2424
2425static int binder_translate_handle(struct flat_binder_object *fp,
2426 struct binder_transaction *t,
2427 struct binder_thread *thread)
2428{
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002429 struct binder_proc *proc = thread->proc;
2430 struct binder_proc *target_proc = t->to_proc;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002431 struct binder_node *node;
2432 struct binder_ref_data src_rdata;
Todd Kjosf22abc72017-05-09 11:08:05 -07002433 int ret = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002434
Todd Kjosb0117bb2017-05-08 09:16:27 -07002435 node = binder_get_node_from_ref(proc, fp->handle,
2436 fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata);
2437 if (!node) {
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002438 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
2439 proc->pid, thread->pid, fp->handle);
2440 return -EINVAL;
2441 }
Todd Kjosf22abc72017-05-09 11:08:05 -07002442 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2443 ret = -EPERM;
2444 goto done;
2445 }
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002446
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002447 binder_node_lock(node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002448 if (node->proc == target_proc) {
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002449 if (fp->hdr.type == BINDER_TYPE_HANDLE)
2450 fp->hdr.type = BINDER_TYPE_BINDER;
2451 else
2452 fp->hdr.type = BINDER_TYPE_WEAK_BINDER;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002453 fp->binder = node->ptr;
2454 fp->cookie = node->cookie;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002455 if (node->proc)
2456 binder_inner_proc_lock(node->proc);
2457 binder_inc_node_nilocked(node,
2458 fp->hdr.type == BINDER_TYPE_BINDER,
2459 0, NULL);
2460 if (node->proc)
2461 binder_inner_proc_unlock(node->proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002462 trace_binder_transaction_ref_to_node(t, node, &src_rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002463 binder_debug(BINDER_DEBUG_TRANSACTION,
2464 " ref %d desc %d -> node %d u%016llx\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07002465 src_rdata.debug_id, src_rdata.desc, node->debug_id,
2466 (u64)node->ptr);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002467 binder_node_unlock(node);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002468 } else {
Todd Kjosb0117bb2017-05-08 09:16:27 -07002469 int ret;
2470 struct binder_ref_data dest_rdata;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002471
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002472 binder_node_unlock(node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002473 ret = binder_inc_ref_for_node(target_proc, node,
2474 fp->hdr.type == BINDER_TYPE_HANDLE,
2475 NULL, &dest_rdata);
2476 if (ret)
Todd Kjosf22abc72017-05-09 11:08:05 -07002477 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002478
2479 fp->binder = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002480 fp->handle = dest_rdata.desc;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002481 fp->cookie = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002482 trace_binder_transaction_ref_to_ref(t, node, &src_rdata,
2483 &dest_rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002484 binder_debug(BINDER_DEBUG_TRANSACTION,
2485 " ref %d desc %d -> ref %d desc %d (node %d)\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07002486 src_rdata.debug_id, src_rdata.desc,
2487 dest_rdata.debug_id, dest_rdata.desc,
2488 node->debug_id);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002489 }
Todd Kjosf22abc72017-05-09 11:08:05 -07002490done:
2491 binder_put_node(node);
2492 return ret;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002493}
2494
2495static int binder_translate_fd(int fd,
2496 struct binder_transaction *t,
2497 struct binder_thread *thread,
2498 struct binder_transaction *in_reply_to)
2499{
2500 struct binder_proc *proc = thread->proc;
2501 struct binder_proc *target_proc = t->to_proc;
2502 int target_fd;
2503 struct file *file;
2504 int ret;
2505 bool target_allows_fd;
2506
2507 if (in_reply_to)
2508 target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS);
2509 else
2510 target_allows_fd = t->buffer->target_node->accept_fds;
2511 if (!target_allows_fd) {
2512 binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
2513 proc->pid, thread->pid,
2514 in_reply_to ? "reply" : "transaction",
2515 fd);
2516 ret = -EPERM;
2517 goto err_fd_not_accepted;
2518 }
2519
2520 file = fget(fd);
2521 if (!file) {
2522 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
2523 proc->pid, thread->pid, fd);
2524 ret = -EBADF;
2525 goto err_fget;
2526 }
2527 ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file);
2528 if (ret < 0) {
2529 ret = -EPERM;
2530 goto err_security;
2531 }
2532
2533 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
2534 if (target_fd < 0) {
2535 ret = -ENOMEM;
2536 goto err_get_unused_fd;
2537 }
2538 task_fd_install(target_proc, target_fd, file);
2539 trace_binder_transaction_fd(t, fd, target_fd);
2540 binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n",
2541 fd, target_fd);
2542
2543 return target_fd;
2544
2545err_get_unused_fd:
2546err_security:
2547 fput(file);
2548err_fget:
2549err_fd_not_accepted:
2550 return ret;
2551}
2552
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002553static int binder_translate_fd_array(struct binder_fd_array_object *fda,
2554 struct binder_buffer_object *parent,
2555 struct binder_transaction *t,
2556 struct binder_thread *thread,
2557 struct binder_transaction *in_reply_to)
2558{
2559 binder_size_t fdi, fd_buf_size, num_installed_fds;
2560 int target_fd;
2561 uintptr_t parent_buffer;
2562 u32 *fd_array;
2563 struct binder_proc *proc = thread->proc;
2564 struct binder_proc *target_proc = t->to_proc;
2565
2566 fd_buf_size = sizeof(u32) * fda->num_fds;
2567 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2568 binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
2569 proc->pid, thread->pid, (u64)fda->num_fds);
2570 return -EINVAL;
2571 }
2572 if (fd_buf_size > parent->length ||
2573 fda->parent_offset > parent->length - fd_buf_size) {
2574 /* No space for all file descriptors here. */
2575 binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
2576 proc->pid, thread->pid, (u64)fda->num_fds);
2577 return -EINVAL;
2578 }
2579 /*
2580 * Since the parent was already fixed up, convert it
2581 * back to the kernel address space to access it
2582 */
Todd Kjosd325d372016-10-10 10:40:53 -07002583 parent_buffer = parent->buffer -
2584 binder_alloc_get_user_buffer_offset(&target_proc->alloc);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002585 fd_array = (u32 *)(parent_buffer + fda->parent_offset);
2586 if (!IS_ALIGNED((unsigned long)fd_array, sizeof(u32))) {
2587 binder_user_error("%d:%d parent offset not aligned correctly.\n",
2588 proc->pid, thread->pid);
2589 return -EINVAL;
2590 }
2591 for (fdi = 0; fdi < fda->num_fds; fdi++) {
2592 target_fd = binder_translate_fd(fd_array[fdi], t, thread,
2593 in_reply_to);
2594 if (target_fd < 0)
2595 goto err_translate_fd_failed;
2596 fd_array[fdi] = target_fd;
2597 }
2598 return 0;
2599
2600err_translate_fd_failed:
2601 /*
2602 * Failed to allocate fd or security error, free fds
2603 * installed so far.
2604 */
2605 num_installed_fds = fdi;
2606 for (fdi = 0; fdi < num_installed_fds; fdi++)
2607 task_close_fd(target_proc, fd_array[fdi]);
2608 return target_fd;
2609}
2610
Martijn Coenen5a6da532016-09-30 14:10:07 +02002611static int binder_fixup_parent(struct binder_transaction *t,
2612 struct binder_thread *thread,
2613 struct binder_buffer_object *bp,
2614 binder_size_t *off_start,
2615 binder_size_t num_valid,
2616 struct binder_buffer_object *last_fixup_obj,
2617 binder_size_t last_fixup_min_off)
2618{
2619 struct binder_buffer_object *parent;
2620 u8 *parent_buffer;
2621 struct binder_buffer *b = t->buffer;
2622 struct binder_proc *proc = thread->proc;
2623 struct binder_proc *target_proc = t->to_proc;
2624
2625 if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT))
2626 return 0;
2627
2628 parent = binder_validate_ptr(b, bp->parent, off_start, num_valid);
2629 if (!parent) {
2630 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
2631 proc->pid, thread->pid);
2632 return -EINVAL;
2633 }
2634
2635 if (!binder_validate_fixup(b, off_start,
2636 parent, bp->parent_offset,
2637 last_fixup_obj,
2638 last_fixup_min_off)) {
2639 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
2640 proc->pid, thread->pid);
2641 return -EINVAL;
2642 }
2643
2644 if (parent->length < sizeof(binder_uintptr_t) ||
2645 bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) {
2646 /* No space for a pointer here! */
2647 binder_user_error("%d:%d got transaction with invalid parent offset\n",
2648 proc->pid, thread->pid);
2649 return -EINVAL;
2650 }
2651 parent_buffer = (u8 *)(parent->buffer -
Todd Kjosd325d372016-10-10 10:40:53 -07002652 binder_alloc_get_user_buffer_offset(
2653 &target_proc->alloc));
Martijn Coenen5a6da532016-09-30 14:10:07 +02002654 *(binder_uintptr_t *)(parent_buffer + bp->parent_offset) = bp->buffer;
2655
2656 return 0;
2657}
2658
Martijn Coenen053be422017-06-06 15:17:46 -07002659/**
2660 * binder_proc_transaction() - sends a transaction to a process and wakes it up
2661 * @t: transaction to send
2662 * @proc: process to send the transaction to
2663 * @thread: thread in @proc to send the transaction to (may be NULL)
2664 *
2665 * This function queues a transaction to the specified process. It will try
2666 * to find a thread in the target process to handle the transaction and
2667 * wake it up. If no thread is found, the work is queued to the proc
2668 * waitqueue.
2669 *
2670 * If the @thread parameter is not NULL, the transaction is always queued
2671 * to the waitlist of that specific thread.
2672 *
2673 * Return: true if the transactions was successfully queued
2674 * false if the target process or thread is dead
2675 */
2676static bool binder_proc_transaction(struct binder_transaction *t,
2677 struct binder_proc *proc,
2678 struct binder_thread *thread)
2679{
2680 struct list_head *target_list = NULL;
2681 struct binder_node *node = t->buffer->target_node;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002682 struct binder_priority node_prio;
Martijn Coenen053be422017-06-06 15:17:46 -07002683 bool oneway = !!(t->flags & TF_ONE_WAY);
2684 bool wakeup = true;
2685
2686 BUG_ON(!node);
2687 binder_node_lock(node);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002688 node_prio.prio = node->min_priority;
2689 node_prio.sched_policy = node->sched_policy;
2690
Martijn Coenen053be422017-06-06 15:17:46 -07002691 if (oneway) {
2692 BUG_ON(thread);
2693 if (node->has_async_transaction) {
2694 target_list = &node->async_todo;
2695 wakeup = false;
2696 } else {
2697 node->has_async_transaction = 1;
2698 }
2699 }
2700
2701 binder_inner_proc_lock(proc);
2702
2703 if (proc->is_dead || (thread && thread->is_dead)) {
2704 binder_inner_proc_unlock(proc);
2705 binder_node_unlock(node);
2706 return false;
2707 }
2708
2709 if (!thread && !target_list)
2710 thread = binder_select_thread_ilocked(proc);
2711
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002712 if (thread) {
Martijn Coenen053be422017-06-06 15:17:46 -07002713 target_list = &thread->todo;
Martijn Coenenc46810c2017-06-23 10:13:43 -07002714 binder_transaction_priority(thread->task, t, node_prio,
2715 node->inherit_rt);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002716 } else if (!target_list) {
Martijn Coenen053be422017-06-06 15:17:46 -07002717 target_list = &proc->todo;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002718 } else {
Martijn Coenen053be422017-06-06 15:17:46 -07002719 BUG_ON(target_list != &node->async_todo);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002720 }
Martijn Coenen053be422017-06-06 15:17:46 -07002721
2722 binder_enqueue_work_ilocked(&t->work, target_list);
2723
2724 if (wakeup)
2725 binder_wakeup_thread_ilocked(proc, thread, !oneway /* sync */);
2726
2727 binder_inner_proc_unlock(proc);
2728 binder_node_unlock(node);
2729
2730 return true;
2731}
2732
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002733static void binder_transaction(struct binder_proc *proc,
2734 struct binder_thread *thread,
Martijn Coenen59878d72016-09-30 14:05:40 +02002735 struct binder_transaction_data *tr, int reply,
2736 binder_size_t extra_buffers_size)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002737{
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002738 int ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002739 struct binder_transaction *t;
2740 struct binder_work *tcomplete;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002741 binder_size_t *offp, *off_end, *off_start;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08002742 binder_size_t off_min;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002743 u8 *sg_bufp, *sg_buf_end;
Todd Kjos2f993e22017-05-12 14:42:55 -07002744 struct binder_proc *target_proc = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002745 struct binder_thread *target_thread = NULL;
2746 struct binder_node *target_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002747 struct binder_transaction *in_reply_to = NULL;
2748 struct binder_transaction_log_entry *e;
Todd Kjose598d172017-03-22 17:19:52 -07002749 uint32_t return_error = 0;
2750 uint32_t return_error_param = 0;
2751 uint32_t return_error_line = 0;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002752 struct binder_buffer_object *last_fixup_obj = NULL;
2753 binder_size_t last_fixup_min_off = 0;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02002754 struct binder_context *context = proc->context;
Todd Kjos1cfe6272017-05-24 13:33:28 -07002755 int t_debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002756
2757 e = binder_transaction_log_add(&binder_transaction_log);
Todd Kjos1cfe6272017-05-24 13:33:28 -07002758 e->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002759 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
2760 e->from_proc = proc->pid;
2761 e->from_thread = thread->pid;
2762 e->target_handle = tr->target.handle;
2763 e->data_size = tr->data_size;
2764 e->offsets_size = tr->offsets_size;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02002765 e->context_name = proc->context->name;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002766
2767 if (reply) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07002768 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002769 in_reply_to = thread->transaction_stack;
2770 if (in_reply_to == NULL) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07002771 binder_inner_proc_unlock(proc);
Anmol Sarma56b468f2012-10-30 22:35:43 +05302772 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002773 proc->pid, thread->pid);
2774 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002775 return_error_param = -EPROTO;
2776 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002777 goto err_empty_call_stack;
2778 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002779 if (in_reply_to->to_thread != thread) {
Todd Kjos2f993e22017-05-12 14:42:55 -07002780 spin_lock(&in_reply_to->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05302781 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 +09002782 proc->pid, thread->pid, in_reply_to->debug_id,
2783 in_reply_to->to_proc ?
2784 in_reply_to->to_proc->pid : 0,
2785 in_reply_to->to_thread ?
2786 in_reply_to->to_thread->pid : 0);
Todd Kjos2f993e22017-05-12 14:42:55 -07002787 spin_unlock(&in_reply_to->lock);
Martijn Coenen995a36e2017-06-02 13:36:52 -07002788 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002789 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002790 return_error_param = -EPROTO;
2791 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002792 in_reply_to = NULL;
2793 goto err_bad_call_stack;
2794 }
2795 thread->transaction_stack = in_reply_to->to_parent;
Martijn Coenen995a36e2017-06-02 13:36:52 -07002796 binder_inner_proc_unlock(proc);
Martijn Coenen995a36e2017-06-02 13:36:52 -07002797 target_thread = binder_get_txn_from_and_acq_inner(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002798 if (target_thread == NULL) {
2799 return_error = BR_DEAD_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002800 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002801 goto err_dead_binder;
2802 }
2803 if (target_thread->transaction_stack != in_reply_to) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302804 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 +09002805 proc->pid, thread->pid,
2806 target_thread->transaction_stack ?
2807 target_thread->transaction_stack->debug_id : 0,
2808 in_reply_to->debug_id);
Martijn Coenen995a36e2017-06-02 13:36:52 -07002809 binder_inner_proc_unlock(target_thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002810 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002811 return_error_param = -EPROTO;
2812 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002813 in_reply_to = NULL;
2814 target_thread = NULL;
2815 goto err_dead_binder;
2816 }
2817 target_proc = target_thread->proc;
Todd Kjos2f993e22017-05-12 14:42:55 -07002818 target_proc->tmp_ref++;
Martijn Coenen995a36e2017-06-02 13:36:52 -07002819 binder_inner_proc_unlock(target_thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002820 } else {
2821 if (tr->target.handle) {
2822 struct binder_ref *ref;
Seunghun Lee10f62862014-05-01 01:30:23 +09002823
Todd Kjosc37162d2017-05-26 11:56:29 -07002824 /*
2825 * There must already be a strong ref
2826 * on this node. If so, do a strong
2827 * increment on the node to ensure it
2828 * stays alive until the transaction is
2829 * done.
2830 */
Todd Kjos5346bf32016-10-20 16:43:34 -07002831 binder_proc_lock(proc);
2832 ref = binder_get_ref_olocked(proc, tr->target.handle,
2833 true);
Todd Kjosc37162d2017-05-26 11:56:29 -07002834 if (ref) {
2835 binder_inc_node(ref->node, 1, 0, NULL);
2836 target_node = ref->node;
2837 }
Todd Kjos5346bf32016-10-20 16:43:34 -07002838 binder_proc_unlock(proc);
Todd Kjosc37162d2017-05-26 11:56:29 -07002839 if (target_node == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302840 binder_user_error("%d:%d got transaction to invalid handle\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002841 proc->pid, thread->pid);
2842 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002843 return_error_param = -EINVAL;
2844 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002845 goto err_invalid_target_handle;
2846 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002847 } else {
Todd Kjos8d9f6f32016-10-17 12:33:15 -07002848 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02002849 target_node = context->binder_context_mgr_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002850 if (target_node == NULL) {
2851 return_error = BR_DEAD_REPLY;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07002852 mutex_unlock(&context->context_mgr_node_lock);
Todd Kjose598d172017-03-22 17:19:52 -07002853 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002854 goto err_no_context_mgr_node;
2855 }
Todd Kjosc37162d2017-05-26 11:56:29 -07002856 binder_inc_node(target_node, 1, 0, NULL);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07002857 mutex_unlock(&context->context_mgr_node_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002858 }
2859 e->to_node = target_node->debug_id;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002860 binder_node_lock(target_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002861 target_proc = target_node->proc;
2862 if (target_proc == NULL) {
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002863 binder_node_unlock(target_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002864 return_error = BR_DEAD_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002865 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002866 goto err_dead_binder;
2867 }
Todd Kjosb4827902017-05-25 15:52:17 -07002868 binder_inner_proc_lock(target_proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002869 target_proc->tmp_ref++;
Todd Kjosb4827902017-05-25 15:52:17 -07002870 binder_inner_proc_unlock(target_proc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002871 binder_node_unlock(target_node);
Stephen Smalley79af7302015-01-21 10:54:10 -05002872 if (security_binder_transaction(proc->tsk,
2873 target_proc->tsk) < 0) {
2874 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002875 return_error_param = -EPERM;
2876 return_error_line = __LINE__;
Stephen Smalley79af7302015-01-21 10:54:10 -05002877 goto err_invalid_target_handle;
2878 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07002879 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002880 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
2881 struct binder_transaction *tmp;
Seunghun Lee10f62862014-05-01 01:30:23 +09002882
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002883 tmp = thread->transaction_stack;
2884 if (tmp->to_thread != thread) {
Todd Kjos2f993e22017-05-12 14:42:55 -07002885 spin_lock(&tmp->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05302886 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 +09002887 proc->pid, thread->pid, tmp->debug_id,
2888 tmp->to_proc ? tmp->to_proc->pid : 0,
2889 tmp->to_thread ?
2890 tmp->to_thread->pid : 0);
Todd Kjos2f993e22017-05-12 14:42:55 -07002891 spin_unlock(&tmp->lock);
Martijn Coenen995a36e2017-06-02 13:36:52 -07002892 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002893 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002894 return_error_param = -EPROTO;
2895 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002896 goto err_bad_call_stack;
2897 }
2898 while (tmp) {
Todd Kjos2f993e22017-05-12 14:42:55 -07002899 struct binder_thread *from;
2900
2901 spin_lock(&tmp->lock);
2902 from = tmp->from;
2903 if (from && from->proc == target_proc) {
2904 atomic_inc(&from->tmp_ref);
2905 target_thread = from;
2906 spin_unlock(&tmp->lock);
2907 break;
2908 }
2909 spin_unlock(&tmp->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002910 tmp = tmp->from_parent;
2911 }
2912 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07002913 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002914 }
Martijn Coenen053be422017-06-06 15:17:46 -07002915 if (target_thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002916 e->to_thread = target_thread->pid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002917 e->to_proc = target_proc->pid;
2918
2919 /* TODO: reuse incoming transaction for reply */
2920 t = kzalloc(sizeof(*t), GFP_KERNEL);
2921 if (t == NULL) {
2922 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002923 return_error_param = -ENOMEM;
2924 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002925 goto err_alloc_t_failed;
2926 }
2927 binder_stats_created(BINDER_STAT_TRANSACTION);
Todd Kjos2f993e22017-05-12 14:42:55 -07002928 spin_lock_init(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002929
2930 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
2931 if (tcomplete == NULL) {
2932 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002933 return_error_param = -ENOMEM;
2934 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002935 goto err_alloc_tcomplete_failed;
2936 }
2937 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
2938
Todd Kjos1cfe6272017-05-24 13:33:28 -07002939 t->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002940
2941 if (reply)
2942 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen59878d72016-09-30 14:05:40 +02002943 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002944 proc->pid, thread->pid, t->debug_id,
2945 target_proc->pid, target_thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002946 (u64)tr->data.ptr.buffer,
2947 (u64)tr->data.ptr.offsets,
Martijn Coenen59878d72016-09-30 14:05:40 +02002948 (u64)tr->data_size, (u64)tr->offsets_size,
2949 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002950 else
2951 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen59878d72016-09-30 14:05:40 +02002952 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002953 proc->pid, thread->pid, t->debug_id,
2954 target_proc->pid, target_node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002955 (u64)tr->data.ptr.buffer,
2956 (u64)tr->data.ptr.offsets,
Martijn Coenen59878d72016-09-30 14:05:40 +02002957 (u64)tr->data_size, (u64)tr->offsets_size,
2958 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002959
2960 if (!reply && !(tr->flags & TF_ONE_WAY))
2961 t->from = thread;
2962 else
2963 t->from = NULL;
Tair Rzayev57bab7c2014-05-31 22:43:34 +03002964 t->sender_euid = task_euid(proc->tsk);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002965 t->to_proc = target_proc;
2966 t->to_thread = target_thread;
2967 t->code = tr->code;
2968 t->flags = tr->flags;
Martijn Coenen57b2ac62017-06-06 17:04:42 -07002969 if (!(t->flags & TF_ONE_WAY) &&
2970 binder_supported_policy(current->policy)) {
2971 /* Inherit supported policies for synchronous transactions */
2972 t->priority.sched_policy = current->policy;
2973 t->priority.prio = current->normal_prio;
2974 } else {
2975 /* Otherwise, fall back to the default priority */
2976 t->priority = target_proc->default_priority;
2977 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002978
2979 trace_binder_transaction(reply, t, target_node);
2980
Todd Kjosd325d372016-10-10 10:40:53 -07002981 t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size,
Martijn Coenen59878d72016-09-30 14:05:40 +02002982 tr->offsets_size, extra_buffers_size,
2983 !reply && (t->flags & TF_ONE_WAY));
Todd Kjose598d172017-03-22 17:19:52 -07002984 if (IS_ERR(t->buffer)) {
2985 /*
2986 * -ESRCH indicates VMA cleared. The target is dying.
2987 */
2988 return_error_param = PTR_ERR(t->buffer);
2989 return_error = return_error_param == -ESRCH ?
2990 BR_DEAD_REPLY : BR_FAILED_REPLY;
2991 return_error_line = __LINE__;
2992 t->buffer = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002993 goto err_binder_alloc_buf_failed;
2994 }
2995 t->buffer->allow_user_free = 0;
2996 t->buffer->debug_id = t->debug_id;
2997 t->buffer->transaction = t;
2998 t->buffer->target_node = target_node;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002999 trace_binder_transaction_alloc_buf(t->buffer);
Martijn Coenen5a6da532016-09-30 14:10:07 +02003000 off_start = (binder_size_t *)(t->buffer->data +
3001 ALIGN(tr->data_size, sizeof(void *)));
3002 offp = off_start;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003003
Arve Hjønnevågda498892014-02-21 14:40:26 -08003004 if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t)
3005 tr->data.ptr.buffer, tr->data_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303006 binder_user_error("%d:%d got transaction with invalid data ptr\n",
3007 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003008 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003009 return_error_param = -EFAULT;
3010 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003011 goto err_copy_data_failed;
3012 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08003013 if (copy_from_user(offp, (const void __user *)(uintptr_t)
3014 tr->data.ptr.offsets, tr->offsets_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303015 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3016 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003017 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003018 return_error_param = -EFAULT;
3019 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003020 goto err_copy_data_failed;
3021 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08003022 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
3023 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
3024 proc->pid, thread->pid, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003025 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003026 return_error_param = -EINVAL;
3027 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003028 goto err_bad_offset;
3029 }
Martijn Coenen5a6da532016-09-30 14:10:07 +02003030 if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) {
3031 binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
3032 proc->pid, thread->pid,
Amit Pundir44cbb182017-02-01 12:53:45 +05303033 (u64)extra_buffers_size);
Martijn Coenen5a6da532016-09-30 14:10:07 +02003034 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003035 return_error_param = -EINVAL;
3036 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003037 goto err_bad_offset;
3038 }
3039 off_end = (void *)off_start + tr->offsets_size;
3040 sg_bufp = (u8 *)(PTR_ALIGN(off_end, sizeof(void *)));
3041 sg_buf_end = sg_bufp + extra_buffers_size;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08003042 off_min = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003043 for (; offp < off_end; offp++) {
Martijn Coenen00c80372016-07-13 12:06:49 +02003044 struct binder_object_header *hdr;
3045 size_t object_size = binder_validate_object(t->buffer, *offp);
Seunghun Lee10f62862014-05-01 01:30:23 +09003046
Martijn Coenen00c80372016-07-13 12:06:49 +02003047 if (object_size == 0 || *offp < off_min) {
3048 binder_user_error("%d:%d got transaction with invalid offset (%lld, min %lld max %lld) or object.\n",
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08003049 proc->pid, thread->pid, (u64)*offp,
3050 (u64)off_min,
Martijn Coenen00c80372016-07-13 12:06:49 +02003051 (u64)t->buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003052 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003053 return_error_param = -EINVAL;
3054 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003055 goto err_bad_offset;
3056 }
Martijn Coenen00c80372016-07-13 12:06:49 +02003057
3058 hdr = (struct binder_object_header *)(t->buffer->data + *offp);
3059 off_min = *offp + object_size;
3060 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003061 case BINDER_TYPE_BINDER:
3062 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003063 struct flat_binder_object *fp;
Seunghun Lee10f62862014-05-01 01:30:23 +09003064
Martijn Coenen00c80372016-07-13 12:06:49 +02003065 fp = to_flat_binder_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003066 ret = binder_translate_binder(fp, t, thread);
3067 if (ret < 0) {
Christian Engelmayer7d420432014-05-07 21:44:53 +02003068 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003069 return_error_param = ret;
3070 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003071 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003072 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003073 } break;
3074 case BINDER_TYPE_HANDLE:
3075 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003076 struct flat_binder_object *fp;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02003077
Martijn Coenen00c80372016-07-13 12:06:49 +02003078 fp = to_flat_binder_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003079 ret = binder_translate_handle(fp, t, thread);
3080 if (ret < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003081 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003082 return_error_param = ret;
3083 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003084 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003085 }
3086 } break;
3087
3088 case BINDER_TYPE_FD: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003089 struct binder_fd_object *fp = to_binder_fd_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003090 int target_fd = binder_translate_fd(fp->fd, t, thread,
3091 in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003092
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003093 if (target_fd < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003094 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003095 return_error_param = target_fd;
3096 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003097 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003098 }
Martijn Coenen00c80372016-07-13 12:06:49 +02003099 fp->pad_binder = 0;
3100 fp->fd = target_fd;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003101 } break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003102 case BINDER_TYPE_FDA: {
3103 struct binder_fd_array_object *fda =
3104 to_binder_fd_array_object(hdr);
3105 struct binder_buffer_object *parent =
3106 binder_validate_ptr(t->buffer, fda->parent,
3107 off_start,
3108 offp - off_start);
3109 if (!parent) {
3110 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
3111 proc->pid, thread->pid);
3112 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003113 return_error_param = -EINVAL;
3114 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003115 goto err_bad_parent;
3116 }
3117 if (!binder_validate_fixup(t->buffer, off_start,
3118 parent, fda->parent_offset,
3119 last_fixup_obj,
3120 last_fixup_min_off)) {
3121 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
3122 proc->pid, thread->pid);
3123 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003124 return_error_param = -EINVAL;
3125 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003126 goto err_bad_parent;
3127 }
3128 ret = binder_translate_fd_array(fda, parent, t, thread,
3129 in_reply_to);
3130 if (ret < 0) {
3131 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003132 return_error_param = ret;
3133 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003134 goto err_translate_failed;
3135 }
3136 last_fixup_obj = parent;
3137 last_fixup_min_off =
3138 fda->parent_offset + sizeof(u32) * fda->num_fds;
3139 } break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003140 case BINDER_TYPE_PTR: {
3141 struct binder_buffer_object *bp =
3142 to_binder_buffer_object(hdr);
3143 size_t buf_left = sg_buf_end - sg_bufp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003144
Martijn Coenen5a6da532016-09-30 14:10:07 +02003145 if (bp->length > buf_left) {
3146 binder_user_error("%d:%d got transaction with too large buffer\n",
3147 proc->pid, thread->pid);
3148 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003149 return_error_param = -EINVAL;
3150 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003151 goto err_bad_offset;
3152 }
3153 if (copy_from_user(sg_bufp,
3154 (const void __user *)(uintptr_t)
3155 bp->buffer, bp->length)) {
3156 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3157 proc->pid, thread->pid);
Todd Kjose598d172017-03-22 17:19:52 -07003158 return_error_param = -EFAULT;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003159 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003160 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003161 goto err_copy_data_failed;
3162 }
3163 /* Fixup buffer pointer to target proc address space */
3164 bp->buffer = (uintptr_t)sg_bufp +
Todd Kjosd325d372016-10-10 10:40:53 -07003165 binder_alloc_get_user_buffer_offset(
3166 &target_proc->alloc);
Martijn Coenen5a6da532016-09-30 14:10:07 +02003167 sg_bufp += ALIGN(bp->length, sizeof(u64));
3168
3169 ret = binder_fixup_parent(t, thread, bp, off_start,
3170 offp - off_start,
3171 last_fixup_obj,
3172 last_fixup_min_off);
3173 if (ret < 0) {
3174 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003175 return_error_param = ret;
3176 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003177 goto err_translate_failed;
3178 }
3179 last_fixup_obj = bp;
3180 last_fixup_min_off = 0;
3181 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003182 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01003183 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
Martijn Coenen00c80372016-07-13 12:06:49 +02003184 proc->pid, thread->pid, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003185 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003186 return_error_param = -EINVAL;
3187 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003188 goto err_bad_object_type;
3189 }
3190 }
Todd Kjos8dedb0c2017-05-09 08:31:32 -07003191 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003192 binder_enqueue_work(proc, tcomplete, &thread->todo);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003193 t->work.type = BINDER_WORK_TRANSACTION;
Todd Kjos8dedb0c2017-05-09 08:31:32 -07003194
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003195 if (reply) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07003196 binder_inner_proc_lock(target_proc);
3197 if (target_thread->is_dead) {
3198 binder_inner_proc_unlock(target_proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07003199 goto err_dead_proc_or_thread;
Martijn Coenen995a36e2017-06-02 13:36:52 -07003200 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003201 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003202 binder_pop_transaction_ilocked(target_thread, in_reply_to);
Martijn Coenen053be422017-06-06 15:17:46 -07003203 binder_enqueue_work_ilocked(&t->work, &target_thread->todo);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003204 binder_inner_proc_unlock(target_proc);
Martijn Coenen053be422017-06-06 15:17:46 -07003205 wake_up_interruptible_sync(&target_thread->wait);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07003206 binder_set_priority(current, in_reply_to->saved_priority);
Todd Kjos21ef40a2017-03-30 18:02:13 -07003207 binder_free_transaction(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003208 } else if (!(t->flags & TF_ONE_WAY)) {
3209 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003210 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003211 t->need_reply = 1;
3212 t->from_parent = thread->transaction_stack;
3213 thread->transaction_stack = t;
Martijn Coenen995a36e2017-06-02 13:36:52 -07003214 binder_inner_proc_unlock(proc);
Martijn Coenen053be422017-06-06 15:17:46 -07003215 if (!binder_proc_transaction(t, target_proc, target_thread)) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07003216 binder_inner_proc_lock(proc);
3217 binder_pop_transaction_ilocked(thread, t);
3218 binder_inner_proc_unlock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07003219 goto err_dead_proc_or_thread;
3220 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003221 } else {
3222 BUG_ON(target_node == NULL);
3223 BUG_ON(t->buffer->async_transaction != 1);
Martijn Coenen053be422017-06-06 15:17:46 -07003224 if (!binder_proc_transaction(t, target_proc, NULL))
Todd Kjos2f993e22017-05-12 14:42:55 -07003225 goto err_dead_proc_or_thread;
Riley Andrewsb5968812015-09-01 12:42:07 -07003226 }
Todd Kjos2f993e22017-05-12 14:42:55 -07003227 if (target_thread)
3228 binder_thread_dec_tmpref(target_thread);
3229 binder_proc_dec_tmpref(target_proc);
Todd Kjos1cfe6272017-05-24 13:33:28 -07003230 /*
3231 * write barrier to synchronize with initialization
3232 * of log entry
3233 */
3234 smp_wmb();
3235 WRITE_ONCE(e->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003236 return;
3237
Todd Kjos2f993e22017-05-12 14:42:55 -07003238err_dead_proc_or_thread:
3239 return_error = BR_DEAD_REPLY;
3240 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003241err_translate_failed:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003242err_bad_object_type:
3243err_bad_offset:
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003244err_bad_parent:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003245err_copy_data_failed:
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003246 trace_binder_transaction_failed_buffer_release(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003247 binder_transaction_buffer_release(target_proc, t->buffer, offp);
Todd Kjosc37162d2017-05-26 11:56:29 -07003248 target_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003249 t->buffer->transaction = NULL;
Todd Kjosd325d372016-10-10 10:40:53 -07003250 binder_alloc_free_buf(&target_proc->alloc, t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003251err_binder_alloc_buf_failed:
3252 kfree(tcomplete);
3253 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3254err_alloc_tcomplete_failed:
3255 kfree(t);
3256 binder_stats_deleted(BINDER_STAT_TRANSACTION);
3257err_alloc_t_failed:
3258err_bad_call_stack:
3259err_empty_call_stack:
3260err_dead_binder:
3261err_invalid_target_handle:
3262err_no_context_mgr_node:
Todd Kjos2f993e22017-05-12 14:42:55 -07003263 if (target_thread)
3264 binder_thread_dec_tmpref(target_thread);
3265 if (target_proc)
3266 binder_proc_dec_tmpref(target_proc);
Todd Kjosc37162d2017-05-26 11:56:29 -07003267 if (target_node)
3268 binder_dec_node(target_node, 1, 0);
3269
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003270 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Todd Kjose598d172017-03-22 17:19:52 -07003271 "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
3272 proc->pid, thread->pid, return_error, return_error_param,
3273 (u64)tr->data_size, (u64)tr->offsets_size,
3274 return_error_line);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003275
3276 {
3277 struct binder_transaction_log_entry *fe;
Seunghun Lee10f62862014-05-01 01:30:23 +09003278
Todd Kjose598d172017-03-22 17:19:52 -07003279 e->return_error = return_error;
3280 e->return_error_param = return_error_param;
3281 e->return_error_line = return_error_line;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003282 fe = binder_transaction_log_add(&binder_transaction_log_failed);
3283 *fe = *e;
Todd Kjos1cfe6272017-05-24 13:33:28 -07003284 /*
3285 * write barrier to synchronize with initialization
3286 * of log entry
3287 */
3288 smp_wmb();
3289 WRITE_ONCE(e->debug_id_done, t_debug_id);
3290 WRITE_ONCE(fe->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003291 }
3292
Todd Kjos858b8da2017-04-21 17:35:12 -07003293 BUG_ON(thread->return_error.cmd != BR_OK);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003294 if (in_reply_to) {
Martijn Coenen07a30fe2017-06-07 10:02:12 -07003295 binder_set_priority(current, in_reply_to->saved_priority);
Todd Kjos858b8da2017-04-21 17:35:12 -07003296 thread->return_error.cmd = BR_TRANSACTION_COMPLETE;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003297 binder_enqueue_work(thread->proc,
3298 &thread->return_error.work,
3299 &thread->todo);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003300 binder_send_failed_reply(in_reply_to, return_error);
Todd Kjos858b8da2017-04-21 17:35:12 -07003301 } else {
3302 thread->return_error.cmd = return_error;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003303 binder_enqueue_work(thread->proc,
3304 &thread->return_error.work,
3305 &thread->todo);
Todd Kjos858b8da2017-04-21 17:35:12 -07003306 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003307}
3308
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02003309static int binder_thread_write(struct binder_proc *proc,
3310 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003311 binder_uintptr_t binder_buffer, size_t size,
3312 binder_size_t *consumed)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003313{
3314 uint32_t cmd;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003315 struct binder_context *context = proc->context;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003316 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003317 void __user *ptr = buffer + *consumed;
3318 void __user *end = buffer + size;
3319
Todd Kjos858b8da2017-04-21 17:35:12 -07003320 while (ptr < end && thread->return_error.cmd == BR_OK) {
Todd Kjosb0117bb2017-05-08 09:16:27 -07003321 int ret;
3322
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003323 if (get_user(cmd, (uint32_t __user *)ptr))
3324 return -EFAULT;
3325 ptr += sizeof(uint32_t);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003326 trace_binder_command(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003327 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07003328 atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]);
3329 atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]);
3330 atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003331 }
3332 switch (cmd) {
3333 case BC_INCREFS:
3334 case BC_ACQUIRE:
3335 case BC_RELEASE:
3336 case BC_DECREFS: {
3337 uint32_t target;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003338 const char *debug_string;
Todd Kjosb0117bb2017-05-08 09:16:27 -07003339 bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE;
3340 bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE;
3341 struct binder_ref_data rdata;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003342
3343 if (get_user(target, (uint32_t __user *)ptr))
3344 return -EFAULT;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003345
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003346 ptr += sizeof(uint32_t);
Todd Kjosb0117bb2017-05-08 09:16:27 -07003347 ret = -1;
3348 if (increment && !target) {
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003349 struct binder_node *ctx_mgr_node;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003350 mutex_lock(&context->context_mgr_node_lock);
3351 ctx_mgr_node = context->binder_context_mgr_node;
Todd Kjosb0117bb2017-05-08 09:16:27 -07003352 if (ctx_mgr_node)
3353 ret = binder_inc_ref_for_node(
3354 proc, ctx_mgr_node,
3355 strong, NULL, &rdata);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003356 mutex_unlock(&context->context_mgr_node_lock);
3357 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07003358 if (ret)
3359 ret = binder_update_ref_for_handle(
3360 proc, target, increment, strong,
3361 &rdata);
3362 if (!ret && rdata.desc != target) {
3363 binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
3364 proc->pid, thread->pid,
3365 target, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003366 }
3367 switch (cmd) {
3368 case BC_INCREFS:
3369 debug_string = "IncRefs";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003370 break;
3371 case BC_ACQUIRE:
3372 debug_string = "Acquire";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003373 break;
3374 case BC_RELEASE:
3375 debug_string = "Release";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003376 break;
3377 case BC_DECREFS:
3378 default:
3379 debug_string = "DecRefs";
Todd Kjosb0117bb2017-05-08 09:16:27 -07003380 break;
3381 }
3382 if (ret) {
3383 binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
3384 proc->pid, thread->pid, debug_string,
3385 strong, target, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003386 break;
3387 }
3388 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjosb0117bb2017-05-08 09:16:27 -07003389 "%d:%d %s ref %d desc %d s %d w %d\n",
3390 proc->pid, thread->pid, debug_string,
3391 rdata.debug_id, rdata.desc, rdata.strong,
3392 rdata.weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003393 break;
3394 }
3395 case BC_INCREFS_DONE:
3396 case BC_ACQUIRE_DONE: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003397 binder_uintptr_t node_ptr;
3398 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003399 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003400 bool free_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003401
Arve Hjønnevågda498892014-02-21 14:40:26 -08003402 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003403 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003404 ptr += sizeof(binder_uintptr_t);
3405 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003406 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003407 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003408 node = binder_get_node(proc, node_ptr);
3409 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003410 binder_user_error("%d:%d %s u%016llx no match\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003411 proc->pid, thread->pid,
3412 cmd == BC_INCREFS_DONE ?
3413 "BC_INCREFS_DONE" :
3414 "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003415 (u64)node_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003416 break;
3417 }
3418 if (cookie != node->cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003419 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003420 proc->pid, thread->pid,
3421 cmd == BC_INCREFS_DONE ?
3422 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003423 (u64)node_ptr, node->debug_id,
3424 (u64)cookie, (u64)node->cookie);
Todd Kjosf22abc72017-05-09 11:08:05 -07003425 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003426 break;
3427 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003428 binder_node_inner_lock(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003429 if (cmd == BC_ACQUIRE_DONE) {
3430 if (node->pending_strong_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303431 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003432 proc->pid, thread->pid,
3433 node->debug_id);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003434 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003435 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003436 break;
3437 }
3438 node->pending_strong_ref = 0;
3439 } else {
3440 if (node->pending_weak_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303441 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003442 proc->pid, thread->pid,
3443 node->debug_id);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003444 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003445 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003446 break;
3447 }
3448 node->pending_weak_ref = 0;
3449 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003450 free_node = binder_dec_node_nilocked(node,
3451 cmd == BC_ACQUIRE_DONE, 0);
3452 WARN_ON(free_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003453 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjosf22abc72017-05-09 11:08:05 -07003454 "%d:%d %s node %d ls %d lw %d tr %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003455 proc->pid, thread->pid,
3456 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Todd Kjosf22abc72017-05-09 11:08:05 -07003457 node->debug_id, node->local_strong_refs,
3458 node->local_weak_refs, node->tmp_refs);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003459 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003460 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003461 break;
3462 }
3463 case BC_ATTEMPT_ACQUIRE:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303464 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003465 return -EINVAL;
3466 case BC_ACQUIRE_RESULT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303467 pr_err("BC_ACQUIRE_RESULT not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003468 return -EINVAL;
3469
3470 case BC_FREE_BUFFER: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003471 binder_uintptr_t data_ptr;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003472 struct binder_buffer *buffer;
3473
Arve Hjønnevågda498892014-02-21 14:40:26 -08003474 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003475 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003476 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003477
Todd Kjos076072a2017-04-21 14:32:11 -07003478 buffer = binder_alloc_prepare_to_free(&proc->alloc,
3479 data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003480 if (buffer == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003481 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
3482 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003483 break;
3484 }
3485 if (!buffer->allow_user_free) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003486 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
3487 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003488 break;
3489 }
3490 binder_debug(BINDER_DEBUG_FREE_BUFFER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003491 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
3492 proc->pid, thread->pid, (u64)data_ptr,
3493 buffer->debug_id,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003494 buffer->transaction ? "active" : "finished");
3495
3496 if (buffer->transaction) {
3497 buffer->transaction->buffer = NULL;
3498 buffer->transaction = NULL;
3499 }
3500 if (buffer->async_transaction && buffer->target_node) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003501 struct binder_node *buf_node;
3502 struct binder_work *w;
3503
3504 buf_node = buffer->target_node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003505 binder_node_inner_lock(buf_node);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003506 BUG_ON(!buf_node->has_async_transaction);
3507 BUG_ON(buf_node->proc != proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003508 w = binder_dequeue_work_head_ilocked(
3509 &buf_node->async_todo);
3510 if (!w)
3511 buf_node->has_async_transaction = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003512 else
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003513 binder_enqueue_work_ilocked(
3514 w, &thread->todo);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003515 binder_node_inner_unlock(buf_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003516 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003517 trace_binder_transaction_buffer_release(buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003518 binder_transaction_buffer_release(proc, buffer, NULL);
Todd Kjosd325d372016-10-10 10:40:53 -07003519 binder_alloc_free_buf(&proc->alloc, buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003520 break;
3521 }
3522
Martijn Coenen5a6da532016-09-30 14:10:07 +02003523 case BC_TRANSACTION_SG:
3524 case BC_REPLY_SG: {
3525 struct binder_transaction_data_sg tr;
3526
3527 if (copy_from_user(&tr, ptr, sizeof(tr)))
3528 return -EFAULT;
3529 ptr += sizeof(tr);
3530 binder_transaction(proc, thread, &tr.transaction_data,
3531 cmd == BC_REPLY_SG, tr.buffers_size);
3532 break;
3533 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003534 case BC_TRANSACTION:
3535 case BC_REPLY: {
3536 struct binder_transaction_data tr;
3537
3538 if (copy_from_user(&tr, ptr, sizeof(tr)))
3539 return -EFAULT;
3540 ptr += sizeof(tr);
Martijn Coenen59878d72016-09-30 14:05:40 +02003541 binder_transaction(proc, thread, &tr,
3542 cmd == BC_REPLY, 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003543 break;
3544 }
3545
3546 case BC_REGISTER_LOOPER:
3547 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303548 "%d:%d BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003549 proc->pid, thread->pid);
Todd Kjosd600e902017-05-25 17:35:02 -07003550 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003551 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
3552 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303553 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003554 proc->pid, thread->pid);
3555 } else if (proc->requested_threads == 0) {
3556 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303557 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003558 proc->pid, thread->pid);
3559 } else {
3560 proc->requested_threads--;
3561 proc->requested_threads_started++;
3562 }
3563 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
Todd Kjosd600e902017-05-25 17:35:02 -07003564 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003565 break;
3566 case BC_ENTER_LOOPER:
3567 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303568 "%d:%d BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003569 proc->pid, thread->pid);
3570 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
3571 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303572 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003573 proc->pid, thread->pid);
3574 }
3575 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
3576 break;
3577 case BC_EXIT_LOOPER:
3578 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303579 "%d:%d BC_EXIT_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003580 proc->pid, thread->pid);
3581 thread->looper |= BINDER_LOOPER_STATE_EXITED;
3582 break;
3583
3584 case BC_REQUEST_DEATH_NOTIFICATION:
3585 case BC_CLEAR_DEATH_NOTIFICATION: {
3586 uint32_t target;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003587 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003588 struct binder_ref *ref;
Todd Kjos5346bf32016-10-20 16:43:34 -07003589 struct binder_ref_death *death = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003590
3591 if (get_user(target, (uint32_t __user *)ptr))
3592 return -EFAULT;
3593 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08003594 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003595 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003596 ptr += sizeof(binder_uintptr_t);
Todd Kjos5346bf32016-10-20 16:43:34 -07003597 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3598 /*
3599 * Allocate memory for death notification
3600 * before taking lock
3601 */
3602 death = kzalloc(sizeof(*death), GFP_KERNEL);
3603 if (death == NULL) {
3604 WARN_ON(thread->return_error.cmd !=
3605 BR_OK);
3606 thread->return_error.cmd = BR_ERROR;
3607 binder_enqueue_work(
3608 thread->proc,
3609 &thread->return_error.work,
3610 &thread->todo);
3611 binder_debug(
3612 BINDER_DEBUG_FAILED_TRANSACTION,
3613 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
3614 proc->pid, thread->pid);
3615 break;
3616 }
3617 }
3618 binder_proc_lock(proc);
3619 ref = binder_get_ref_olocked(proc, target, false);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003620 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303621 binder_user_error("%d:%d %s invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003622 proc->pid, thread->pid,
3623 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3624 "BC_REQUEST_DEATH_NOTIFICATION" :
3625 "BC_CLEAR_DEATH_NOTIFICATION",
3626 target);
Todd Kjos5346bf32016-10-20 16:43:34 -07003627 binder_proc_unlock(proc);
3628 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003629 break;
3630 }
3631
3632 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003633 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003634 proc->pid, thread->pid,
3635 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3636 "BC_REQUEST_DEATH_NOTIFICATION" :
3637 "BC_CLEAR_DEATH_NOTIFICATION",
Todd Kjosb0117bb2017-05-08 09:16:27 -07003638 (u64)cookie, ref->data.debug_id,
3639 ref->data.desc, ref->data.strong,
3640 ref->data.weak, ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003641
Martijn Coenenf9eac642017-05-22 11:26:23 -07003642 binder_node_lock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003643 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3644 if (ref->death) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303645 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003646 proc->pid, thread->pid);
Martijn Coenenf9eac642017-05-22 11:26:23 -07003647 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003648 binder_proc_unlock(proc);
3649 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003650 break;
3651 }
3652 binder_stats_created(BINDER_STAT_DEATH);
3653 INIT_LIST_HEAD(&death->work.entry);
3654 death->cookie = cookie;
3655 ref->death = death;
3656 if (ref->node->proc == NULL) {
3657 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003658 if (thread->looper &
3659 (BINDER_LOOPER_STATE_REGISTERED |
3660 BINDER_LOOPER_STATE_ENTERED))
3661 binder_enqueue_work(
3662 proc,
3663 &ref->death->work,
3664 &thread->todo);
3665 else {
Martijn Coenen22d64e4322017-06-02 11:15:44 -07003666 binder_inner_proc_lock(proc);
3667 binder_enqueue_work_ilocked(
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003668 &ref->death->work,
3669 &proc->todo);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07003670 binder_wakeup_proc_ilocked(
Martijn Coenen053be422017-06-06 15:17:46 -07003671 proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07003672 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003673 }
3674 }
3675 } else {
3676 if (ref->death == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303677 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003678 proc->pid, thread->pid);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003679 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003680 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003681 break;
3682 }
3683 death = ref->death;
3684 if (death->cookie != cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003685 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003686 proc->pid, thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003687 (u64)death->cookie,
3688 (u64)cookie);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003689 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003690 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003691 break;
3692 }
3693 ref->death = NULL;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003694 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003695 if (list_empty(&death->work.entry)) {
3696 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003697 if (thread->looper &
3698 (BINDER_LOOPER_STATE_REGISTERED |
3699 BINDER_LOOPER_STATE_ENTERED))
3700 binder_enqueue_work_ilocked(
3701 &death->work,
3702 &thread->todo);
3703 else {
3704 binder_enqueue_work_ilocked(
3705 &death->work,
3706 &proc->todo);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07003707 binder_wakeup_proc_ilocked(
Martijn Coenen053be422017-06-06 15:17:46 -07003708 proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003709 }
3710 } else {
3711 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
3712 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
3713 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003714 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003715 }
Martijn Coenenf9eac642017-05-22 11:26:23 -07003716 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003717 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003718 } break;
3719 case BC_DEAD_BINDER_DONE: {
3720 struct binder_work *w;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003721 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003722 struct binder_ref_death *death = NULL;
Seunghun Lee10f62862014-05-01 01:30:23 +09003723
Arve Hjønnevågda498892014-02-21 14:40:26 -08003724 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003725 return -EFAULT;
3726
Lisa Du7a64cd82016-02-17 09:32:52 +08003727 ptr += sizeof(cookie);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003728 binder_inner_proc_lock(proc);
3729 list_for_each_entry(w, &proc->delivered_death,
3730 entry) {
3731 struct binder_ref_death *tmp_death =
3732 container_of(w,
3733 struct binder_ref_death,
3734 work);
Seunghun Lee10f62862014-05-01 01:30:23 +09003735
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003736 if (tmp_death->cookie == cookie) {
3737 death = tmp_death;
3738 break;
3739 }
3740 }
3741 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003742 "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n",
3743 proc->pid, thread->pid, (u64)cookie,
3744 death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003745 if (death == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003746 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
3747 proc->pid, thread->pid, (u64)cookie);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003748 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003749 break;
3750 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003751 binder_dequeue_work_ilocked(&death->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003752 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
3753 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003754 if (thread->looper &
3755 (BINDER_LOOPER_STATE_REGISTERED |
3756 BINDER_LOOPER_STATE_ENTERED))
3757 binder_enqueue_work_ilocked(
3758 &death->work, &thread->todo);
3759 else {
3760 binder_enqueue_work_ilocked(
3761 &death->work,
3762 &proc->todo);
Martijn Coenen053be422017-06-06 15:17:46 -07003763 binder_wakeup_proc_ilocked(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003764 }
3765 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003766 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003767 } break;
3768
3769 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303770 pr_err("%d:%d unknown command %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003771 proc->pid, thread->pid, cmd);
3772 return -EINVAL;
3773 }
3774 *consumed = ptr - buffer;
3775 }
3776 return 0;
3777}
3778
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02003779static void binder_stat_br(struct binder_proc *proc,
3780 struct binder_thread *thread, uint32_t cmd)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003781{
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003782 trace_binder_return(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003783 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07003784 atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
3785 atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
3786 atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003787 }
3788}
3789
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003790static int binder_has_thread_work(struct binder_thread *thread)
3791{
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003792 return !binder_worklist_empty(thread->proc, &thread->todo) ||
3793 thread->looper_need_return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003794}
3795
Todd Kjos60792612017-05-24 10:51:01 -07003796static int binder_put_node_cmd(struct binder_proc *proc,
3797 struct binder_thread *thread,
3798 void __user **ptrp,
3799 binder_uintptr_t node_ptr,
3800 binder_uintptr_t node_cookie,
3801 int node_debug_id,
3802 uint32_t cmd, const char *cmd_name)
3803{
3804 void __user *ptr = *ptrp;
3805
3806 if (put_user(cmd, (uint32_t __user *)ptr))
3807 return -EFAULT;
3808 ptr += sizeof(uint32_t);
3809
3810 if (put_user(node_ptr, (binder_uintptr_t __user *)ptr))
3811 return -EFAULT;
3812 ptr += sizeof(binder_uintptr_t);
3813
3814 if (put_user(node_cookie, (binder_uintptr_t __user *)ptr))
3815 return -EFAULT;
3816 ptr += sizeof(binder_uintptr_t);
3817
3818 binder_stat_br(proc, thread, cmd);
3819 binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n",
3820 proc->pid, thread->pid, cmd_name, node_debug_id,
3821 (u64)node_ptr, (u64)node_cookie);
3822
3823 *ptrp = ptr;
3824 return 0;
3825}
3826
Martijn Coenen22d64e4322017-06-02 11:15:44 -07003827static int binder_wait_for_work(struct binder_thread *thread,
3828 bool do_proc_work)
3829{
3830 DEFINE_WAIT(wait);
3831 struct binder_proc *proc = thread->proc;
3832 int ret = 0;
3833
3834 freezer_do_not_count();
3835 binder_inner_proc_lock(proc);
3836 for (;;) {
3837 prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE);
3838 if (binder_has_work_ilocked(thread, do_proc_work))
3839 break;
3840 if (do_proc_work)
3841 list_add(&thread->waiting_thread_node,
3842 &proc->waiting_threads);
3843 binder_inner_proc_unlock(proc);
3844 schedule();
3845 binder_inner_proc_lock(proc);
3846 list_del_init(&thread->waiting_thread_node);
3847 if (signal_pending(current)) {
3848 ret = -ERESTARTSYS;
3849 break;
3850 }
3851 }
3852 finish_wait(&thread->wait, &wait);
3853 binder_inner_proc_unlock(proc);
3854 freezer_count();
3855
3856 return ret;
3857}
3858
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003859static int binder_thread_read(struct binder_proc *proc,
3860 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003861 binder_uintptr_t binder_buffer, size_t size,
3862 binder_size_t *consumed, int non_block)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003863{
Arve Hjønnevågda498892014-02-21 14:40:26 -08003864 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003865 void __user *ptr = buffer + *consumed;
3866 void __user *end = buffer + size;
3867
3868 int ret = 0;
3869 int wait_for_proc_work;
3870
3871 if (*consumed == 0) {
3872 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
3873 return -EFAULT;
3874 ptr += sizeof(uint32_t);
3875 }
3876
3877retry:
Martijn Coenen995a36e2017-06-02 13:36:52 -07003878 binder_inner_proc_lock(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07003879 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003880 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003881
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003882 thread->looper |= BINDER_LOOPER_STATE_WAITING;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003883
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003884 trace_binder_wait_for_work(wait_for_proc_work,
3885 !!thread->transaction_stack,
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003886 !binder_worklist_empty(proc, &thread->todo));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003887 if (wait_for_proc_work) {
3888 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
3889 BINDER_LOOPER_STATE_ENTERED))) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303890 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 +09003891 proc->pid, thread->pid, thread->looper);
3892 wait_event_interruptible(binder_user_error_wait,
3893 binder_stop_on_user_error < 2);
3894 }
Martijn Coenen57b2ac62017-06-06 17:04:42 -07003895 binder_set_priority(current, proc->default_priority);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003896 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003897
Martijn Coenen22d64e4322017-06-02 11:15:44 -07003898 if (non_block) {
3899 if (!binder_has_work(thread, wait_for_proc_work))
3900 ret = -EAGAIN;
3901 } else {
3902 ret = binder_wait_for_work(thread, wait_for_proc_work);
3903 }
3904
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003905 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
3906
3907 if (ret)
3908 return ret;
3909
3910 while (1) {
3911 uint32_t cmd;
3912 struct binder_transaction_data tr;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003913 struct binder_work *w = NULL;
3914 struct list_head *list = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003915 struct binder_transaction *t = NULL;
Todd Kjos2f993e22017-05-12 14:42:55 -07003916 struct binder_thread *t_from;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003917
Todd Kjose7f23ed2017-03-21 13:06:01 -07003918 binder_inner_proc_lock(proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003919 if (!binder_worklist_empty_ilocked(&thread->todo))
3920 list = &thread->todo;
3921 else if (!binder_worklist_empty_ilocked(&proc->todo) &&
3922 wait_for_proc_work)
3923 list = &proc->todo;
3924 else {
3925 binder_inner_proc_unlock(proc);
3926
Dmitry Voytik395262a2014-09-08 18:16:34 +04003927 /* no data added */
Todd Kjos6798e6d2017-01-06 14:19:25 -08003928 if (ptr - buffer == 4 && !thread->looper_need_return)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003929 goto retry;
3930 break;
3931 }
3932
Todd Kjose7f23ed2017-03-21 13:06:01 -07003933 if (end - ptr < sizeof(tr) + 4) {
3934 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003935 break;
Todd Kjose7f23ed2017-03-21 13:06:01 -07003936 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003937 w = binder_dequeue_work_head_ilocked(list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003938
3939 switch (w->type) {
3940 case BINDER_WORK_TRANSACTION: {
Todd Kjose7f23ed2017-03-21 13:06:01 -07003941 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003942 t = container_of(w, struct binder_transaction, work);
3943 } break;
Todd Kjos858b8da2017-04-21 17:35:12 -07003944 case BINDER_WORK_RETURN_ERROR: {
3945 struct binder_error *e = container_of(
3946 w, struct binder_error, work);
3947
3948 WARN_ON(e->cmd == BR_OK);
Todd Kjose7f23ed2017-03-21 13:06:01 -07003949 binder_inner_proc_unlock(proc);
Todd Kjos858b8da2017-04-21 17:35:12 -07003950 if (put_user(e->cmd, (uint32_t __user *)ptr))
3951 return -EFAULT;
3952 e->cmd = BR_OK;
3953 ptr += sizeof(uint32_t);
3954
3955 binder_stat_br(proc, thread, cmd);
Todd Kjos858b8da2017-04-21 17:35:12 -07003956 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003957 case BINDER_WORK_TRANSACTION_COMPLETE: {
Todd Kjose7f23ed2017-03-21 13:06:01 -07003958 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003959 cmd = BR_TRANSACTION_COMPLETE;
3960 if (put_user(cmd, (uint32_t __user *)ptr))
3961 return -EFAULT;
3962 ptr += sizeof(uint32_t);
3963
3964 binder_stat_br(proc, thread, cmd);
3965 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303966 "%d:%d BR_TRANSACTION_COMPLETE\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003967 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003968 kfree(w);
3969 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3970 } break;
3971 case BINDER_WORK_NODE: {
3972 struct binder_node *node = container_of(w, struct binder_node, work);
Todd Kjos60792612017-05-24 10:51:01 -07003973 int strong, weak;
3974 binder_uintptr_t node_ptr = node->ptr;
3975 binder_uintptr_t node_cookie = node->cookie;
3976 int node_debug_id = node->debug_id;
3977 int has_weak_ref;
3978 int has_strong_ref;
3979 void __user *orig_ptr = ptr;
Seunghun Lee10f62862014-05-01 01:30:23 +09003980
Todd Kjos60792612017-05-24 10:51:01 -07003981 BUG_ON(proc != node->proc);
3982 strong = node->internal_strong_refs ||
3983 node->local_strong_refs;
3984 weak = !hlist_empty(&node->refs) ||
Todd Kjosf22abc72017-05-09 11:08:05 -07003985 node->local_weak_refs ||
3986 node->tmp_refs || strong;
Todd Kjos60792612017-05-24 10:51:01 -07003987 has_strong_ref = node->has_strong_ref;
3988 has_weak_ref = node->has_weak_ref;
3989
3990 if (weak && !has_weak_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003991 node->has_weak_ref = 1;
3992 node->pending_weak_ref = 1;
3993 node->local_weak_refs++;
Todd Kjos60792612017-05-24 10:51:01 -07003994 }
3995 if (strong && !has_strong_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003996 node->has_strong_ref = 1;
3997 node->pending_strong_ref = 1;
3998 node->local_strong_refs++;
Todd Kjos60792612017-05-24 10:51:01 -07003999 }
4000 if (!strong && has_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004001 node->has_strong_ref = 0;
Todd Kjos60792612017-05-24 10:51:01 -07004002 if (!weak && has_weak_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004003 node->has_weak_ref = 0;
Todd Kjos60792612017-05-24 10:51:01 -07004004 if (!weak && !strong) {
4005 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4006 "%d:%d node %d u%016llx c%016llx deleted\n",
4007 proc->pid, thread->pid,
4008 node_debug_id,
4009 (u64)node_ptr,
4010 (u64)node_cookie);
4011 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004012 binder_inner_proc_unlock(proc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004013 binder_node_lock(node);
4014 /*
4015 * Acquire the node lock before freeing the
4016 * node to serialize with other threads that
4017 * may have been holding the node lock while
4018 * decrementing this node (avoids race where
4019 * this thread frees while the other thread
4020 * is unlocking the node after the final
4021 * decrement)
4022 */
4023 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004024 binder_free_node(node);
4025 } else
4026 binder_inner_proc_unlock(proc);
4027
Todd Kjos60792612017-05-24 10:51:01 -07004028 if (weak && !has_weak_ref)
4029 ret = binder_put_node_cmd(
4030 proc, thread, &ptr, node_ptr,
4031 node_cookie, node_debug_id,
4032 BR_INCREFS, "BR_INCREFS");
4033 if (!ret && strong && !has_strong_ref)
4034 ret = binder_put_node_cmd(
4035 proc, thread, &ptr, node_ptr,
4036 node_cookie, node_debug_id,
4037 BR_ACQUIRE, "BR_ACQUIRE");
4038 if (!ret && !strong && has_strong_ref)
4039 ret = binder_put_node_cmd(
4040 proc, thread, &ptr, node_ptr,
4041 node_cookie, node_debug_id,
4042 BR_RELEASE, "BR_RELEASE");
4043 if (!ret && !weak && has_weak_ref)
4044 ret = binder_put_node_cmd(
4045 proc, thread, &ptr, node_ptr,
4046 node_cookie, node_debug_id,
4047 BR_DECREFS, "BR_DECREFS");
4048 if (orig_ptr == ptr)
4049 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4050 "%d:%d node %d u%016llx c%016llx state unchanged\n",
4051 proc->pid, thread->pid,
4052 node_debug_id,
4053 (u64)node_ptr,
4054 (u64)node_cookie);
4055 if (ret)
4056 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004057 } break;
4058 case BINDER_WORK_DEAD_BINDER:
4059 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4060 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4061 struct binder_ref_death *death;
4062 uint32_t cmd;
Martijn Coenenf9eac642017-05-22 11:26:23 -07004063 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004064
4065 death = container_of(w, struct binder_ref_death, work);
4066 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
4067 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
4068 else
4069 cmd = BR_DEAD_BINDER;
Martijn Coenenf9eac642017-05-22 11:26:23 -07004070 cookie = death->cookie;
4071
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004072 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004073 "%d:%d %s %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004074 proc->pid, thread->pid,
4075 cmd == BR_DEAD_BINDER ?
4076 "BR_DEAD_BINDER" :
4077 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
Martijn Coenenf9eac642017-05-22 11:26:23 -07004078 (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004079 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
Martijn Coenenf9eac642017-05-22 11:26:23 -07004080 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004081 kfree(death);
4082 binder_stats_deleted(BINDER_STAT_DEATH);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004083 } else {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004084 binder_enqueue_work_ilocked(
4085 w, &proc->delivered_death);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004086 binder_inner_proc_unlock(proc);
4087 }
Martijn Coenenf9eac642017-05-22 11:26:23 -07004088 if (put_user(cmd, (uint32_t __user *)ptr))
4089 return -EFAULT;
4090 ptr += sizeof(uint32_t);
4091 if (put_user(cookie,
4092 (binder_uintptr_t __user *)ptr))
4093 return -EFAULT;
4094 ptr += sizeof(binder_uintptr_t);
4095 binder_stat_br(proc, thread, cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004096 if (cmd == BR_DEAD_BINDER)
4097 goto done; /* DEAD_BINDER notifications can cause transactions */
4098 } break;
4099 }
4100
4101 if (!t)
4102 continue;
4103
4104 BUG_ON(t->buffer == NULL);
4105 if (t->buffer->target_node) {
4106 struct binder_node *target_node = t->buffer->target_node;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004107 struct binder_priority node_prio;
Seunghun Lee10f62862014-05-01 01:30:23 +09004108
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004109 tr.target.ptr = target_node->ptr;
4110 tr.cookie = target_node->cookie;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004111 node_prio.sched_policy = target_node->sched_policy;
4112 node_prio.prio = target_node->min_priority;
Martijn Coenenc46810c2017-06-23 10:13:43 -07004113 binder_transaction_priority(current, t, node_prio,
4114 target_node->inherit_rt);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004115 cmd = BR_TRANSACTION;
4116 } else {
Arve Hjønnevågda498892014-02-21 14:40:26 -08004117 tr.target.ptr = 0;
4118 tr.cookie = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004119 cmd = BR_REPLY;
4120 }
4121 tr.code = t->code;
4122 tr.flags = t->flags;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -06004123 tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004124
Todd Kjos2f993e22017-05-12 14:42:55 -07004125 t_from = binder_get_txn_from(t);
4126 if (t_from) {
4127 struct task_struct *sender = t_from->proc->tsk;
Seunghun Lee10f62862014-05-01 01:30:23 +09004128
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004129 tr.sender_pid = task_tgid_nr_ns(sender,
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08004130 task_active_pid_ns(current));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004131 } else {
4132 tr.sender_pid = 0;
4133 }
4134
4135 tr.data_size = t->buffer->data_size;
4136 tr.offsets_size = t->buffer->offsets_size;
Todd Kjosd325d372016-10-10 10:40:53 -07004137 tr.data.ptr.buffer = (binder_uintptr_t)
4138 ((uintptr_t)t->buffer->data +
4139 binder_alloc_get_user_buffer_offset(&proc->alloc));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004140 tr.data.ptr.offsets = tr.data.ptr.buffer +
4141 ALIGN(t->buffer->data_size,
4142 sizeof(void *));
4143
Todd Kjos2f993e22017-05-12 14:42:55 -07004144 if (put_user(cmd, (uint32_t __user *)ptr)) {
4145 if (t_from)
4146 binder_thread_dec_tmpref(t_from);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004147 return -EFAULT;
Todd Kjos2f993e22017-05-12 14:42:55 -07004148 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004149 ptr += sizeof(uint32_t);
Todd Kjos2f993e22017-05-12 14:42:55 -07004150 if (copy_to_user(ptr, &tr, sizeof(tr))) {
4151 if (t_from)
4152 binder_thread_dec_tmpref(t_from);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004153 return -EFAULT;
Todd Kjos2f993e22017-05-12 14:42:55 -07004154 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004155 ptr += sizeof(tr);
4156
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004157 trace_binder_transaction_received(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004158 binder_stat_br(proc, thread, cmd);
4159 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004160 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004161 proc->pid, thread->pid,
4162 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
4163 "BR_REPLY",
Todd Kjos2f993e22017-05-12 14:42:55 -07004164 t->debug_id, t_from ? t_from->proc->pid : 0,
4165 t_from ? t_from->pid : 0, cmd,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004166 t->buffer->data_size, t->buffer->offsets_size,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004167 (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004168
Todd Kjos2f993e22017-05-12 14:42:55 -07004169 if (t_from)
4170 binder_thread_dec_tmpref(t_from);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004171 t->buffer->allow_user_free = 1;
4172 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07004173 binder_inner_proc_lock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004174 t->to_parent = thread->transaction_stack;
4175 t->to_thread = thread;
4176 thread->transaction_stack = t;
Martijn Coenen995a36e2017-06-02 13:36:52 -07004177 binder_inner_proc_unlock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004178 } else {
Todd Kjos21ef40a2017-03-30 18:02:13 -07004179 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004180 }
4181 break;
4182 }
4183
4184done:
4185
4186 *consumed = ptr - buffer;
Todd Kjosd600e902017-05-25 17:35:02 -07004187 binder_inner_proc_lock(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004188 if (proc->requested_threads == 0 &&
4189 list_empty(&thread->proc->waiting_threads) &&
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004190 proc->requested_threads_started < proc->max_threads &&
4191 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4192 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
4193 /*spawn a new thread if we leave this out */) {
4194 proc->requested_threads++;
Todd Kjosd600e902017-05-25 17:35:02 -07004195 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004196 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304197 "%d:%d BR_SPAWN_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004198 proc->pid, thread->pid);
4199 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
4200 return -EFAULT;
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07004201 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
Todd Kjosd600e902017-05-25 17:35:02 -07004202 } else
4203 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004204 return 0;
4205}
4206
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004207static void binder_release_work(struct binder_proc *proc,
4208 struct list_head *list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004209{
4210 struct binder_work *w;
Seunghun Lee10f62862014-05-01 01:30:23 +09004211
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004212 while (1) {
4213 w = binder_dequeue_work_head(proc, list);
4214 if (!w)
4215 return;
4216
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004217 switch (w->type) {
4218 case BINDER_WORK_TRANSACTION: {
4219 struct binder_transaction *t;
4220
4221 t = container_of(w, struct binder_transaction, work);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004222 if (t->buffer->target_node &&
4223 !(t->flags & TF_ONE_WAY)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004224 binder_send_failed_reply(t, BR_DEAD_REPLY);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004225 } else {
4226 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304227 "undelivered transaction %d\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004228 t->debug_id);
Todd Kjos21ef40a2017-03-30 18:02:13 -07004229 binder_free_transaction(t);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004230 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004231 } break;
Todd Kjos858b8da2017-04-21 17:35:12 -07004232 case BINDER_WORK_RETURN_ERROR: {
4233 struct binder_error *e = container_of(
4234 w, struct binder_error, work);
4235
4236 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4237 "undelivered TRANSACTION_ERROR: %u\n",
4238 e->cmd);
4239 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004240 case BINDER_WORK_TRANSACTION_COMPLETE: {
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004241 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304242 "undelivered TRANSACTION_COMPLETE\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004243 kfree(w);
4244 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4245 } break;
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004246 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4247 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4248 struct binder_ref_death *death;
4249
4250 death = container_of(w, struct binder_ref_death, work);
4251 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004252 "undelivered death notification, %016llx\n",
4253 (u64)death->cookie);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004254 kfree(death);
4255 binder_stats_deleted(BINDER_STAT_DEATH);
4256 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004257 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304258 pr_err("unexpected work type, %d, not freed\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004259 w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004260 break;
4261 }
4262 }
4263
4264}
4265
Todd Kjosb4827902017-05-25 15:52:17 -07004266static struct binder_thread *binder_get_thread_ilocked(
4267 struct binder_proc *proc, struct binder_thread *new_thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004268{
4269 struct binder_thread *thread = NULL;
4270 struct rb_node *parent = NULL;
4271 struct rb_node **p = &proc->threads.rb_node;
4272
4273 while (*p) {
4274 parent = *p;
4275 thread = rb_entry(parent, struct binder_thread, rb_node);
4276
4277 if (current->pid < thread->pid)
4278 p = &(*p)->rb_left;
4279 else if (current->pid > thread->pid)
4280 p = &(*p)->rb_right;
4281 else
Todd Kjosb4827902017-05-25 15:52:17 -07004282 return thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004283 }
Todd Kjosb4827902017-05-25 15:52:17 -07004284 if (!new_thread)
4285 return NULL;
4286 thread = new_thread;
4287 binder_stats_created(BINDER_STAT_THREAD);
4288 thread->proc = proc;
4289 thread->pid = current->pid;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004290 get_task_struct(current);
4291 thread->task = current;
Todd Kjosb4827902017-05-25 15:52:17 -07004292 atomic_set(&thread->tmp_ref, 0);
4293 init_waitqueue_head(&thread->wait);
4294 INIT_LIST_HEAD(&thread->todo);
4295 rb_link_node(&thread->rb_node, parent, p);
4296 rb_insert_color(&thread->rb_node, &proc->threads);
4297 thread->looper_need_return = true;
4298 thread->return_error.work.type = BINDER_WORK_RETURN_ERROR;
4299 thread->return_error.cmd = BR_OK;
4300 thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR;
4301 thread->reply_error.cmd = BR_OK;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004302 INIT_LIST_HEAD(&new_thread->waiting_thread_node);
Todd Kjosb4827902017-05-25 15:52:17 -07004303 return thread;
4304}
4305
4306static struct binder_thread *binder_get_thread(struct binder_proc *proc)
4307{
4308 struct binder_thread *thread;
4309 struct binder_thread *new_thread;
4310
4311 binder_inner_proc_lock(proc);
4312 thread = binder_get_thread_ilocked(proc, NULL);
4313 binder_inner_proc_unlock(proc);
4314 if (!thread) {
4315 new_thread = kzalloc(sizeof(*thread), GFP_KERNEL);
4316 if (new_thread == NULL)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004317 return NULL;
Todd Kjosb4827902017-05-25 15:52:17 -07004318 binder_inner_proc_lock(proc);
4319 thread = binder_get_thread_ilocked(proc, new_thread);
4320 binder_inner_proc_unlock(proc);
4321 if (thread != new_thread)
4322 kfree(new_thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004323 }
4324 return thread;
4325}
4326
Todd Kjos2f993e22017-05-12 14:42:55 -07004327static void binder_free_proc(struct binder_proc *proc)
4328{
4329 BUG_ON(!list_empty(&proc->todo));
4330 BUG_ON(!list_empty(&proc->delivered_death));
4331 binder_alloc_deferred_release(&proc->alloc);
4332 put_task_struct(proc->tsk);
4333 binder_stats_deleted(BINDER_STAT_PROC);
4334 kfree(proc);
4335}
4336
4337static void binder_free_thread(struct binder_thread *thread)
4338{
4339 BUG_ON(!list_empty(&thread->todo));
4340 binder_stats_deleted(BINDER_STAT_THREAD);
4341 binder_proc_dec_tmpref(thread->proc);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004342 put_task_struct(thread->task);
Todd Kjos2f993e22017-05-12 14:42:55 -07004343 kfree(thread);
4344}
4345
4346static int binder_thread_release(struct binder_proc *proc,
4347 struct binder_thread *thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004348{
4349 struct binder_transaction *t;
4350 struct binder_transaction *send_reply = NULL;
4351 int active_transactions = 0;
Todd Kjos2f993e22017-05-12 14:42:55 -07004352 struct binder_transaction *last_t = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004353
Todd Kjosb4827902017-05-25 15:52:17 -07004354 binder_inner_proc_lock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07004355 /*
4356 * take a ref on the proc so it survives
4357 * after we remove this thread from proc->threads.
4358 * The corresponding dec is when we actually
4359 * free the thread in binder_free_thread()
4360 */
4361 proc->tmp_ref++;
4362 /*
4363 * take a ref on this thread to ensure it
4364 * survives while we are releasing it
4365 */
4366 atomic_inc(&thread->tmp_ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004367 rb_erase(&thread->rb_node, &proc->threads);
4368 t = thread->transaction_stack;
Todd Kjos2f993e22017-05-12 14:42:55 -07004369 if (t) {
4370 spin_lock(&t->lock);
4371 if (t->to_thread == thread)
4372 send_reply = t;
4373 }
4374 thread->is_dead = true;
4375
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004376 while (t) {
Todd Kjos2f993e22017-05-12 14:42:55 -07004377 last_t = t;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004378 active_transactions++;
4379 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304380 "release %d:%d transaction %d %s, still active\n",
4381 proc->pid, thread->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004382 t->debug_id,
4383 (t->to_thread == thread) ? "in" : "out");
4384
4385 if (t->to_thread == thread) {
4386 t->to_proc = NULL;
4387 t->to_thread = NULL;
4388 if (t->buffer) {
4389 t->buffer->transaction = NULL;
4390 t->buffer = NULL;
4391 }
4392 t = t->to_parent;
4393 } else if (t->from == thread) {
4394 t->from = NULL;
4395 t = t->from_parent;
4396 } else
4397 BUG();
Todd Kjos2f993e22017-05-12 14:42:55 -07004398 spin_unlock(&last_t->lock);
4399 if (t)
4400 spin_lock(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004401 }
Todd Kjosb4827902017-05-25 15:52:17 -07004402 binder_inner_proc_unlock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07004403
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004404 if (send_reply)
4405 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004406 binder_release_work(proc, &thread->todo);
Todd Kjos2f993e22017-05-12 14:42:55 -07004407 binder_thread_dec_tmpref(thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004408 return active_transactions;
4409}
4410
4411static unsigned int binder_poll(struct file *filp,
4412 struct poll_table_struct *wait)
4413{
4414 struct binder_proc *proc = filp->private_data;
4415 struct binder_thread *thread = NULL;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004416 bool wait_for_proc_work;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004417
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004418 thread = binder_get_thread(proc);
4419
Martijn Coenen995a36e2017-06-02 13:36:52 -07004420 binder_inner_proc_lock(thread->proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004421 thread->looper |= BINDER_LOOPER_STATE_POLL;
4422 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
4423
Martijn Coenen995a36e2017-06-02 13:36:52 -07004424 binder_inner_proc_unlock(thread->proc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004425
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004426 if (binder_has_work(thread, wait_for_proc_work))
4427 return POLLIN;
4428
4429 poll_wait(filp, &thread->wait, wait);
4430
4431 if (binder_has_thread_work(thread))
4432 return POLLIN;
4433
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004434 return 0;
4435}
4436
Tair Rzayev78260ac2014-06-03 22:27:21 +03004437static int binder_ioctl_write_read(struct file *filp,
4438 unsigned int cmd, unsigned long arg,
4439 struct binder_thread *thread)
4440{
4441 int ret = 0;
4442 struct binder_proc *proc = filp->private_data;
4443 unsigned int size = _IOC_SIZE(cmd);
4444 void __user *ubuf = (void __user *)arg;
4445 struct binder_write_read bwr;
4446
4447 if (size != sizeof(struct binder_write_read)) {
4448 ret = -EINVAL;
4449 goto out;
4450 }
4451 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
4452 ret = -EFAULT;
4453 goto out;
4454 }
4455 binder_debug(BINDER_DEBUG_READ_WRITE,
4456 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
4457 proc->pid, thread->pid,
4458 (u64)bwr.write_size, (u64)bwr.write_buffer,
4459 (u64)bwr.read_size, (u64)bwr.read_buffer);
4460
4461 if (bwr.write_size > 0) {
4462 ret = binder_thread_write(proc, thread,
4463 bwr.write_buffer,
4464 bwr.write_size,
4465 &bwr.write_consumed);
4466 trace_binder_write_done(ret);
4467 if (ret < 0) {
4468 bwr.read_consumed = 0;
4469 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4470 ret = -EFAULT;
4471 goto out;
4472 }
4473 }
4474 if (bwr.read_size > 0) {
4475 ret = binder_thread_read(proc, thread, bwr.read_buffer,
4476 bwr.read_size,
4477 &bwr.read_consumed,
4478 filp->f_flags & O_NONBLOCK);
4479 trace_binder_read_done(ret);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004480 binder_inner_proc_lock(proc);
4481 if (!binder_worklist_empty_ilocked(&proc->todo))
Martijn Coenen053be422017-06-06 15:17:46 -07004482 binder_wakeup_proc_ilocked(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004483 binder_inner_proc_unlock(proc);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004484 if (ret < 0) {
4485 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4486 ret = -EFAULT;
4487 goto out;
4488 }
4489 }
4490 binder_debug(BINDER_DEBUG_READ_WRITE,
4491 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
4492 proc->pid, thread->pid,
4493 (u64)bwr.write_consumed, (u64)bwr.write_size,
4494 (u64)bwr.read_consumed, (u64)bwr.read_size);
4495 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
4496 ret = -EFAULT;
4497 goto out;
4498 }
4499out:
4500 return ret;
4501}
4502
4503static int binder_ioctl_set_ctx_mgr(struct file *filp)
4504{
4505 int ret = 0;
4506 struct binder_proc *proc = filp->private_data;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004507 struct binder_context *context = proc->context;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004508 struct binder_node *new_node;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004509 kuid_t curr_euid = current_euid();
4510
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004511 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004512 if (context->binder_context_mgr_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004513 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
4514 ret = -EBUSY;
4515 goto out;
4516 }
Stephen Smalley79af7302015-01-21 10:54:10 -05004517 ret = security_binder_set_context_mgr(proc->tsk);
4518 if (ret < 0)
4519 goto out;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004520 if (uid_valid(context->binder_context_mgr_uid)) {
4521 if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004522 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
4523 from_kuid(&init_user_ns, curr_euid),
4524 from_kuid(&init_user_ns,
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004525 context->binder_context_mgr_uid));
Tair Rzayev78260ac2014-06-03 22:27:21 +03004526 ret = -EPERM;
4527 goto out;
4528 }
4529 } else {
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004530 context->binder_context_mgr_uid = curr_euid;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004531 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004532 new_node = binder_new_node(proc, NULL);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004533 if (!new_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004534 ret = -ENOMEM;
4535 goto out;
4536 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004537 binder_node_lock(new_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004538 new_node->local_weak_refs++;
4539 new_node->local_strong_refs++;
4540 new_node->has_strong_ref = 1;
4541 new_node->has_weak_ref = 1;
4542 context->binder_context_mgr_node = new_node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004543 binder_node_unlock(new_node);
Todd Kjosf22abc72017-05-09 11:08:05 -07004544 binder_put_node(new_node);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004545out:
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004546 mutex_unlock(&context->context_mgr_node_lock);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004547 return ret;
4548}
4549
Colin Cross833babb32017-06-20 13:54:44 -07004550static int binder_ioctl_get_node_debug_info(struct binder_proc *proc,
4551 struct binder_node_debug_info *info) {
4552 struct rb_node *n;
4553 binder_uintptr_t ptr = info->ptr;
4554
4555 memset(info, 0, sizeof(*info));
4556
4557 binder_inner_proc_lock(proc);
4558 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
4559 struct binder_node *node = rb_entry(n, struct binder_node,
4560 rb_node);
4561 if (node->ptr > ptr) {
4562 info->ptr = node->ptr;
4563 info->cookie = node->cookie;
4564 info->has_strong_ref = node->has_strong_ref;
4565 info->has_weak_ref = node->has_weak_ref;
4566 break;
4567 }
4568 }
4569 binder_inner_proc_unlock(proc);
4570
4571 return 0;
4572}
4573
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004574static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4575{
4576 int ret;
4577 struct binder_proc *proc = filp->private_data;
4578 struct binder_thread *thread;
4579 unsigned int size = _IOC_SIZE(cmd);
4580 void __user *ubuf = (void __user *)arg;
4581
Tair Rzayev78260ac2014-06-03 22:27:21 +03004582 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
4583 proc->pid, current->pid, cmd, arg);*/
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004584
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004585 trace_binder_ioctl(cmd, arg);
4586
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004587 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4588 if (ret)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004589 goto err_unlocked;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004590
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004591 thread = binder_get_thread(proc);
4592 if (thread == NULL) {
4593 ret = -ENOMEM;
4594 goto err;
4595 }
4596
4597 switch (cmd) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004598 case BINDER_WRITE_READ:
4599 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
4600 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004601 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004602 break;
Todd Kjosd600e902017-05-25 17:35:02 -07004603 case BINDER_SET_MAX_THREADS: {
4604 int max_threads;
4605
4606 if (copy_from_user(&max_threads, ubuf,
4607 sizeof(max_threads))) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004608 ret = -EINVAL;
4609 goto err;
4610 }
Todd Kjosd600e902017-05-25 17:35:02 -07004611 binder_inner_proc_lock(proc);
4612 proc->max_threads = max_threads;
4613 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004614 break;
Todd Kjosd600e902017-05-25 17:35:02 -07004615 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004616 case BINDER_SET_CONTEXT_MGR:
Tair Rzayev78260ac2014-06-03 22:27:21 +03004617 ret = binder_ioctl_set_ctx_mgr(filp);
4618 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004619 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004620 break;
4621 case BINDER_THREAD_EXIT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304622 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004623 proc->pid, thread->pid);
Todd Kjos2f993e22017-05-12 14:42:55 -07004624 binder_thread_release(proc, thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004625 thread = NULL;
4626 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02004627 case BINDER_VERSION: {
4628 struct binder_version __user *ver = ubuf;
4629
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004630 if (size != sizeof(struct binder_version)) {
4631 ret = -EINVAL;
4632 goto err;
4633 }
Mathieu Maret36c89c02014-04-15 12:03:05 +02004634 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
4635 &ver->protocol_version)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004636 ret = -EINVAL;
4637 goto err;
4638 }
4639 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02004640 }
Colin Cross833babb32017-06-20 13:54:44 -07004641 case BINDER_GET_NODE_DEBUG_INFO: {
4642 struct binder_node_debug_info info;
4643
4644 if (copy_from_user(&info, ubuf, sizeof(info))) {
4645 ret = -EFAULT;
4646 goto err;
4647 }
4648
4649 ret = binder_ioctl_get_node_debug_info(proc, &info);
4650 if (ret < 0)
4651 goto err;
4652
4653 if (copy_to_user(ubuf, &info, sizeof(info))) {
4654 ret = -EFAULT;
4655 goto err;
4656 }
4657 break;
4658 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004659 default:
4660 ret = -EINVAL;
4661 goto err;
4662 }
4663 ret = 0;
4664err:
4665 if (thread)
Todd Kjos6798e6d2017-01-06 14:19:25 -08004666 thread->looper_need_return = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004667 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4668 if (ret && ret != -ERESTARTSYS)
Anmol Sarma56b468f2012-10-30 22:35:43 +05304669 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 -07004670err_unlocked:
4671 trace_binder_ioctl_done(ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004672 return ret;
4673}
4674
4675static void binder_vma_open(struct vm_area_struct *vma)
4676{
4677 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004678
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004679 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304680 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004681 proc->pid, vma->vm_start, vma->vm_end,
4682 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4683 (unsigned long)pgprot_val(vma->vm_page_prot));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004684}
4685
4686static void binder_vma_close(struct vm_area_struct *vma)
4687{
4688 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004689
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004690 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304691 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004692 proc->pid, vma->vm_start, vma->vm_end,
4693 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4694 (unsigned long)pgprot_val(vma->vm_page_prot));
Todd Kjosd325d372016-10-10 10:40:53 -07004695 binder_alloc_vma_close(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004696 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
4697}
4698
Vinayak Menonddac7d52014-06-02 18:17:59 +05304699static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
4700{
4701 return VM_FAULT_SIGBUS;
4702}
4703
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07004704static const struct vm_operations_struct binder_vm_ops = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004705 .open = binder_vma_open,
4706 .close = binder_vma_close,
Vinayak Menonddac7d52014-06-02 18:17:59 +05304707 .fault = binder_vm_fault,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004708};
4709
Todd Kjosd325d372016-10-10 10:40:53 -07004710static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
4711{
4712 int ret;
4713 struct binder_proc *proc = filp->private_data;
4714 const char *failure_string;
4715
4716 if (proc->tsk != current->group_leader)
4717 return -EINVAL;
4718
4719 if ((vma->vm_end - vma->vm_start) > SZ_4M)
4720 vma->vm_end = vma->vm_start + SZ_4M;
4721
4722 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4723 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
4724 __func__, proc->pid, vma->vm_start, vma->vm_end,
4725 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4726 (unsigned long)pgprot_val(vma->vm_page_prot));
4727
4728 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
4729 ret = -EPERM;
4730 failure_string = "bad vm_flags";
4731 goto err_bad_arg;
4732 }
4733 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
4734 vma->vm_ops = &binder_vm_ops;
4735 vma->vm_private_data = proc;
4736
4737 ret = binder_alloc_mmap_handler(&proc->alloc, vma);
4738 if (ret)
4739 return ret;
4740 proc->files = get_files_struct(current);
4741 return 0;
4742
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004743err_bad_arg:
Sherwin Soltani258767f2012-06-26 02:00:30 -04004744 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004745 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
4746 return ret;
4747}
4748
4749static int binder_open(struct inode *nodp, struct file *filp)
4750{
4751 struct binder_proc *proc;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02004752 struct binder_device *binder_dev;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004753
4754 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
4755 current->group_leader->pid, current->pid);
4756
4757 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
4758 if (proc == NULL)
4759 return -ENOMEM;
Todd Kjosfc7a7e22017-05-29 16:44:24 -07004760 spin_lock_init(&proc->inner_lock);
4761 spin_lock_init(&proc->outer_lock);
Martijn Coenen872c26e2017-03-07 15:51:18 +01004762 get_task_struct(current->group_leader);
4763 proc->tsk = current->group_leader;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004764 INIT_LIST_HEAD(&proc->todo);
Martijn Coenen57b2ac62017-06-06 17:04:42 -07004765 if (binder_supported_policy(current->policy)) {
4766 proc->default_priority.sched_policy = current->policy;
4767 proc->default_priority.prio = current->normal_prio;
4768 } else {
4769 proc->default_priority.sched_policy = SCHED_NORMAL;
4770 proc->default_priority.prio = NICE_TO_PRIO(0);
4771 }
4772
Martijn Coenen6b7c7122016-09-30 16:08:09 +02004773 binder_dev = container_of(filp->private_data, struct binder_device,
4774 miscdev);
4775 proc->context = &binder_dev->context;
Todd Kjosd325d372016-10-10 10:40:53 -07004776 binder_alloc_init(&proc->alloc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004777
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004778 binder_stats_created(BINDER_STAT_PROC);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004779 proc->pid = current->group_leader->pid;
4780 INIT_LIST_HEAD(&proc->delivered_death);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004781 INIT_LIST_HEAD(&proc->waiting_threads);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004782 filp->private_data = proc;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004783
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004784 mutex_lock(&binder_procs_lock);
4785 hlist_add_head(&proc->proc_node, &binder_procs);
4786 mutex_unlock(&binder_procs_lock);
4787
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004788 if (binder_debugfs_dir_entry_proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004789 char strbuf[11];
Seunghun Lee10f62862014-05-01 01:30:23 +09004790
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004791 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02004792 /*
4793 * proc debug entries are shared between contexts, so
4794 * this will fail if the process tries to open the driver
4795 * again with a different context. The priting code will
4796 * anyway print all contexts that a given PID has, so this
4797 * is not a problem.
4798 */
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004799 proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02004800 binder_debugfs_dir_entry_proc,
4801 (void *)(unsigned long)proc->pid,
4802 &binder_proc_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004803 }
4804
4805 return 0;
4806}
4807
4808static int binder_flush(struct file *filp, fl_owner_t id)
4809{
4810 struct binder_proc *proc = filp->private_data;
4811
4812 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
4813
4814 return 0;
4815}
4816
4817static void binder_deferred_flush(struct binder_proc *proc)
4818{
4819 struct rb_node *n;
4820 int wake_count = 0;
Seunghun Lee10f62862014-05-01 01:30:23 +09004821
Todd Kjosb4827902017-05-25 15:52:17 -07004822 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004823 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
4824 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
Seunghun Lee10f62862014-05-01 01:30:23 +09004825
Todd Kjos6798e6d2017-01-06 14:19:25 -08004826 thread->looper_need_return = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004827 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
4828 wake_up_interruptible(&thread->wait);
4829 wake_count++;
4830 }
4831 }
Todd Kjosb4827902017-05-25 15:52:17 -07004832 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004833
4834 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4835 "binder_flush: %d woke %d threads\n", proc->pid,
4836 wake_count);
4837}
4838
4839static int binder_release(struct inode *nodp, struct file *filp)
4840{
4841 struct binder_proc *proc = filp->private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004842
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004843 debugfs_remove(proc->debugfs_entry);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004844 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
4845
4846 return 0;
4847}
4848
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004849static int binder_node_release(struct binder_node *node, int refs)
4850{
4851 struct binder_ref *ref;
4852 int death = 0;
Todd Kjose7f23ed2017-03-21 13:06:01 -07004853 struct binder_proc *proc = node->proc;
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004854
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004855 binder_release_work(proc, &node->async_todo);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004856
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004857 binder_node_lock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004858 binder_inner_proc_lock(proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004859 binder_dequeue_work_ilocked(&node->work);
Todd Kjosf22abc72017-05-09 11:08:05 -07004860 /*
4861 * The caller must have taken a temporary ref on the node,
4862 */
4863 BUG_ON(!node->tmp_refs);
4864 if (hlist_empty(&node->refs) && node->tmp_refs == 1) {
Todd Kjose7f23ed2017-03-21 13:06:01 -07004865 binder_inner_proc_unlock(proc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004866 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004867 binder_free_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004868
4869 return refs;
4870 }
4871
4872 node->proc = NULL;
4873 node->local_strong_refs = 0;
4874 node->local_weak_refs = 0;
Todd Kjose7f23ed2017-03-21 13:06:01 -07004875 binder_inner_proc_unlock(proc);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004876
4877 spin_lock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004878 hlist_add_head(&node->dead_node, &binder_dead_nodes);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004879 spin_unlock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004880
4881 hlist_for_each_entry(ref, &node->refs, node_entry) {
4882 refs++;
Martijn Coenenf9eac642017-05-22 11:26:23 -07004883 /*
4884 * Need the node lock to synchronize
4885 * with new notification requests and the
4886 * inner lock to synchronize with queued
4887 * death notifications.
4888 */
4889 binder_inner_proc_lock(ref->proc);
4890 if (!ref->death) {
4891 binder_inner_proc_unlock(ref->proc);
Arve Hjønnevåge194fd82014-02-17 13:58:29 -08004892 continue;
Martijn Coenenf9eac642017-05-22 11:26:23 -07004893 }
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004894
4895 death++;
4896
Martijn Coenenf9eac642017-05-22 11:26:23 -07004897 BUG_ON(!list_empty(&ref->death->work.entry));
4898 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
4899 binder_enqueue_work_ilocked(&ref->death->work,
4900 &ref->proc->todo);
Martijn Coenen053be422017-06-06 15:17:46 -07004901 binder_wakeup_proc_ilocked(ref->proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004902 binder_inner_proc_unlock(ref->proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004903 }
4904
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004905 binder_debug(BINDER_DEBUG_DEAD_BINDER,
4906 "node %d now dead, refs %d, death %d\n",
4907 node->debug_id, refs, death);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004908 binder_node_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07004909 binder_put_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004910
4911 return refs;
4912}
4913
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004914static void binder_deferred_release(struct binder_proc *proc)
4915{
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004916 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004917 struct rb_node *n;
Todd Kjosd325d372016-10-10 10:40:53 -07004918 int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004919
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004920 BUG_ON(proc->files);
4921
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004922 mutex_lock(&binder_procs_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004923 hlist_del(&proc->proc_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004924 mutex_unlock(&binder_procs_lock);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004925
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004926 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004927 if (context->binder_context_mgr_node &&
4928 context->binder_context_mgr_node->proc == proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004929 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01004930 "%s: %d context_mgr_node gone\n",
4931 __func__, proc->pid);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004932 context->binder_context_mgr_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004933 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004934 mutex_unlock(&context->context_mgr_node_lock);
Todd Kjosb4827902017-05-25 15:52:17 -07004935 binder_inner_proc_lock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07004936 /*
4937 * Make sure proc stays alive after we
4938 * remove all the threads
4939 */
4940 proc->tmp_ref++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004941
Todd Kjos2f993e22017-05-12 14:42:55 -07004942 proc->is_dead = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004943 threads = 0;
4944 active_transactions = 0;
4945 while ((n = rb_first(&proc->threads))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004946 struct binder_thread *thread;
4947
4948 thread = rb_entry(n, struct binder_thread, rb_node);
Todd Kjosb4827902017-05-25 15:52:17 -07004949 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004950 threads++;
Todd Kjos2f993e22017-05-12 14:42:55 -07004951 active_transactions += binder_thread_release(proc, thread);
Todd Kjosb4827902017-05-25 15:52:17 -07004952 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004953 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004954
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004955 nodes = 0;
4956 incoming_refs = 0;
4957 while ((n = rb_first(&proc->nodes))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004958 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004959
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004960 node = rb_entry(n, struct binder_node, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004961 nodes++;
Todd Kjosf22abc72017-05-09 11:08:05 -07004962 /*
4963 * take a temporary ref on the node before
4964 * calling binder_node_release() which will either
4965 * kfree() the node or call binder_put_node()
4966 */
Todd Kjos425d23f2017-06-12 12:07:26 -07004967 binder_inc_node_tmpref_ilocked(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004968 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjos425d23f2017-06-12 12:07:26 -07004969 binder_inner_proc_unlock(proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004970 incoming_refs = binder_node_release(node, incoming_refs);
Todd Kjos425d23f2017-06-12 12:07:26 -07004971 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004972 }
Todd Kjos425d23f2017-06-12 12:07:26 -07004973 binder_inner_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004974
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004975 outgoing_refs = 0;
Todd Kjos5346bf32016-10-20 16:43:34 -07004976 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004977 while ((n = rb_first(&proc->refs_by_desc))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004978 struct binder_ref *ref;
4979
4980 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004981 outgoing_refs++;
Todd Kjos5346bf32016-10-20 16:43:34 -07004982 binder_cleanup_ref_olocked(ref);
4983 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07004984 binder_free_ref(ref);
Todd Kjos5346bf32016-10-20 16:43:34 -07004985 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004986 }
Todd Kjos5346bf32016-10-20 16:43:34 -07004987 binder_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004988
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004989 binder_release_work(proc, &proc->todo);
4990 binder_release_work(proc, &proc->delivered_death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004991
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004992 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Todd Kjosd325d372016-10-10 10:40:53 -07004993 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01004994 __func__, proc->pid, threads, nodes, incoming_refs,
Todd Kjosd325d372016-10-10 10:40:53 -07004995 outgoing_refs, active_transactions);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004996
Todd Kjos2f993e22017-05-12 14:42:55 -07004997 binder_proc_dec_tmpref(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004998}
4999
5000static void binder_deferred_func(struct work_struct *work)
5001{
5002 struct binder_proc *proc;
5003 struct files_struct *files;
5004
5005 int defer;
Seunghun Lee10f62862014-05-01 01:30:23 +09005006
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005007 do {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005008 mutex_lock(&binder_deferred_lock);
5009 if (!hlist_empty(&binder_deferred_list)) {
5010 proc = hlist_entry(binder_deferred_list.first,
5011 struct binder_proc, deferred_work_node);
5012 hlist_del_init(&proc->deferred_work_node);
5013 defer = proc->deferred_work;
5014 proc->deferred_work = 0;
5015 } else {
5016 proc = NULL;
5017 defer = 0;
5018 }
5019 mutex_unlock(&binder_deferred_lock);
5020
5021 files = NULL;
5022 if (defer & BINDER_DEFERRED_PUT_FILES) {
5023 files = proc->files;
5024 if (files)
5025 proc->files = NULL;
5026 }
5027
5028 if (defer & BINDER_DEFERRED_FLUSH)
5029 binder_deferred_flush(proc);
5030
5031 if (defer & BINDER_DEFERRED_RELEASE)
5032 binder_deferred_release(proc); /* frees proc */
5033
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005034 if (files)
5035 put_files_struct(files);
5036 } while (proc);
5037}
5038static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
5039
5040static void
5041binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
5042{
5043 mutex_lock(&binder_deferred_lock);
5044 proc->deferred_work |= defer;
5045 if (hlist_unhashed(&proc->deferred_work_node)) {
5046 hlist_add_head(&proc->deferred_work_node,
5047 &binder_deferred_list);
Bhaktipriya Shridhar1beba522016-08-13 22:16:24 +05305048 schedule_work(&binder_deferred_work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005049 }
5050 mutex_unlock(&binder_deferred_lock);
5051}
5052
Todd Kjos6d241a42017-04-21 14:32:11 -07005053static void print_binder_transaction_ilocked(struct seq_file *m,
5054 struct binder_proc *proc,
5055 const char *prefix,
5056 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005057{
Todd Kjos6d241a42017-04-21 14:32:11 -07005058 struct binder_proc *to_proc;
5059 struct binder_buffer *buffer = t->buffer;
5060
5061 WARN_ON(!spin_is_locked(&proc->inner_lock));
Todd Kjos2f993e22017-05-12 14:42:55 -07005062 spin_lock(&t->lock);
Todd Kjos6d241a42017-04-21 14:32:11 -07005063 to_proc = t->to_proc;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005064 seq_printf(m,
Martijn Coenen57b2ac62017-06-06 17:04:42 -07005065 "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %d:%d r%d",
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005066 prefix, t->debug_id, t,
5067 t->from ? t->from->proc->pid : 0,
5068 t->from ? t->from->pid : 0,
Todd Kjos6d241a42017-04-21 14:32:11 -07005069 to_proc ? to_proc->pid : 0,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005070 t->to_thread ? t->to_thread->pid : 0,
Martijn Coenen57b2ac62017-06-06 17:04:42 -07005071 t->code, t->flags, t->priority.sched_policy,
5072 t->priority.prio, t->need_reply);
Todd Kjos2f993e22017-05-12 14:42:55 -07005073 spin_unlock(&t->lock);
5074
Todd Kjos6d241a42017-04-21 14:32:11 -07005075 if (proc != to_proc) {
5076 /*
5077 * Can only safely deref buffer if we are holding the
5078 * correct proc inner lock for this node
5079 */
5080 seq_puts(m, "\n");
5081 return;
5082 }
5083
5084 if (buffer == NULL) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005085 seq_puts(m, " buffer free\n");
5086 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005087 }
Todd Kjos6d241a42017-04-21 14:32:11 -07005088 if (buffer->target_node)
5089 seq_printf(m, " node %d", buffer->target_node->debug_id);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005090 seq_printf(m, " size %zd:%zd data %p\n",
Todd Kjos6d241a42017-04-21 14:32:11 -07005091 buffer->data_size, buffer->offsets_size,
5092 buffer->data);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005093}
5094
Todd Kjos6d241a42017-04-21 14:32:11 -07005095static void print_binder_work_ilocked(struct seq_file *m,
5096 struct binder_proc *proc,
5097 const char *prefix,
5098 const char *transaction_prefix,
5099 struct binder_work *w)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005100{
5101 struct binder_node *node;
5102 struct binder_transaction *t;
5103
5104 switch (w->type) {
5105 case BINDER_WORK_TRANSACTION:
5106 t = container_of(w, struct binder_transaction, work);
Todd Kjos6d241a42017-04-21 14:32:11 -07005107 print_binder_transaction_ilocked(
5108 m, proc, transaction_prefix, t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005109 break;
Todd Kjos858b8da2017-04-21 17:35:12 -07005110 case BINDER_WORK_RETURN_ERROR: {
5111 struct binder_error *e = container_of(
5112 w, struct binder_error, work);
5113
5114 seq_printf(m, "%stransaction error: %u\n",
5115 prefix, e->cmd);
5116 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005117 case BINDER_WORK_TRANSACTION_COMPLETE:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005118 seq_printf(m, "%stransaction complete\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005119 break;
5120 case BINDER_WORK_NODE:
5121 node = container_of(w, struct binder_node, work);
Arve Hjønnevågda498892014-02-21 14:40:26 -08005122 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
5123 prefix, node->debug_id,
5124 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005125 break;
5126 case BINDER_WORK_DEAD_BINDER:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005127 seq_printf(m, "%shas dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005128 break;
5129 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005130 seq_printf(m, "%shas cleared dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005131 break;
5132 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005133 seq_printf(m, "%shas cleared death notification\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005134 break;
5135 default:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005136 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005137 break;
5138 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005139}
5140
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005141static void print_binder_thread_ilocked(struct seq_file *m,
5142 struct binder_thread *thread,
5143 int print_always)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005144{
5145 struct binder_transaction *t;
5146 struct binder_work *w;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005147 size_t start_pos = m->count;
5148 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005149
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005150 WARN_ON(!spin_is_locked(&thread->proc->inner_lock));
Todd Kjos2f993e22017-05-12 14:42:55 -07005151 seq_printf(m, " thread %d: l %02x need_return %d tr %d\n",
Todd Kjos6798e6d2017-01-06 14:19:25 -08005152 thread->pid, thread->looper,
Todd Kjos2f993e22017-05-12 14:42:55 -07005153 thread->looper_need_return,
5154 atomic_read(&thread->tmp_ref));
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005155 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005156 t = thread->transaction_stack;
5157 while (t) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005158 if (t->from == thread) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005159 print_binder_transaction_ilocked(m, thread->proc,
5160 " outgoing transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005161 t = t->from_parent;
5162 } else if (t->to_thread == thread) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005163 print_binder_transaction_ilocked(m, thread->proc,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005164 " incoming transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005165 t = t->to_parent;
5166 } else {
Todd Kjos6d241a42017-04-21 14:32:11 -07005167 print_binder_transaction_ilocked(m, thread->proc,
5168 " bad transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005169 t = NULL;
5170 }
5171 }
5172 list_for_each_entry(w, &thread->todo, entry) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005173 print_binder_work_ilocked(m, thread->proc, " ",
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005174 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005175 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005176 if (!print_always && m->count == header_pos)
5177 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005178}
5179
Todd Kjos425d23f2017-06-12 12:07:26 -07005180static void print_binder_node_nilocked(struct seq_file *m,
5181 struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005182{
5183 struct binder_ref *ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005184 struct binder_work *w;
5185 int count;
5186
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005187 WARN_ON(!spin_is_locked(&node->lock));
Todd Kjos425d23f2017-06-12 12:07:26 -07005188 if (node->proc)
5189 WARN_ON(!spin_is_locked(&node->proc->inner_lock));
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005190
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005191 count = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08005192 hlist_for_each_entry(ref, &node->refs, node_entry)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005193 count++;
5194
Martijn Coenen6aac9792017-06-07 09:29:14 -07005195 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 -08005196 node->debug_id, (u64)node->ptr, (u64)node->cookie,
Martijn Coenen6aac9792017-06-07 09:29:14 -07005197 node->sched_policy, node->min_priority,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005198 node->has_strong_ref, node->has_weak_ref,
5199 node->local_strong_refs, node->local_weak_refs,
Todd Kjosf22abc72017-05-09 11:08:05 -07005200 node->internal_strong_refs, count, node->tmp_refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005201 if (count) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005202 seq_puts(m, " proc");
Sasha Levinb67bfe02013-02-27 17:06:00 -08005203 hlist_for_each_entry(ref, &node->refs, node_entry)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005204 seq_printf(m, " %d", ref->proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005205 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005206 seq_puts(m, "\n");
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005207 if (node->proc) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005208 list_for_each_entry(w, &node->async_todo, entry)
Todd Kjos6d241a42017-04-21 14:32:11 -07005209 print_binder_work_ilocked(m, node->proc, " ",
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005210 " pending async transaction", w);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005211 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005212}
5213
Todd Kjos5346bf32016-10-20 16:43:34 -07005214static void print_binder_ref_olocked(struct seq_file *m,
5215 struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005216{
Todd Kjos5346bf32016-10-20 16:43:34 -07005217 WARN_ON(!spin_is_locked(&ref->proc->outer_lock));
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005218 binder_node_lock(ref->node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07005219 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
5220 ref->data.debug_id, ref->data.desc,
5221 ref->node->proc ? "" : "dead ",
5222 ref->node->debug_id, ref->data.strong,
5223 ref->data.weak, ref->death);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005224 binder_node_unlock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005225}
5226
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005227static void print_binder_proc(struct seq_file *m,
5228 struct binder_proc *proc, int print_all)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005229{
5230 struct binder_work *w;
5231 struct rb_node *n;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005232 size_t start_pos = m->count;
5233 size_t header_pos;
Todd Kjos425d23f2017-06-12 12:07:26 -07005234 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005235
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005236 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005237 seq_printf(m, "context %s\n", proc->context->name);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005238 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005239
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005240 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005241 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005242 print_binder_thread_ilocked(m, rb_entry(n, struct binder_thread,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005243 rb_node), print_all);
Todd Kjos425d23f2017-06-12 12:07:26 -07005244
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005245 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005246 struct binder_node *node = rb_entry(n, struct binder_node,
5247 rb_node);
Todd Kjos425d23f2017-06-12 12:07:26 -07005248 /*
5249 * take a temporary reference on the node so it
5250 * survives and isn't removed from the tree
5251 * while we print it.
5252 */
5253 binder_inc_node_tmpref_ilocked(node);
5254 /* Need to drop inner lock to take node lock */
5255 binder_inner_proc_unlock(proc);
5256 if (last_node)
5257 binder_put_node(last_node);
5258 binder_node_inner_lock(node);
5259 print_binder_node_nilocked(m, node);
5260 binder_node_inner_unlock(node);
5261 last_node = node;
5262 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005263 }
Todd Kjos425d23f2017-06-12 12:07:26 -07005264 binder_inner_proc_unlock(proc);
5265 if (last_node)
5266 binder_put_node(last_node);
5267
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005268 if (print_all) {
Todd Kjos5346bf32016-10-20 16:43:34 -07005269 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005270 for (n = rb_first(&proc->refs_by_desc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005271 n != NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005272 n = rb_next(n))
Todd Kjos5346bf32016-10-20 16:43:34 -07005273 print_binder_ref_olocked(m, rb_entry(n,
5274 struct binder_ref,
5275 rb_node_desc));
5276 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005277 }
Todd Kjosd325d372016-10-10 10:40:53 -07005278 binder_alloc_print_allocated(m, &proc->alloc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005279 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005280 list_for_each_entry(w, &proc->todo, entry)
Todd Kjos6d241a42017-04-21 14:32:11 -07005281 print_binder_work_ilocked(m, proc, " ",
5282 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005283 list_for_each_entry(w, &proc->delivered_death, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005284 seq_puts(m, " has delivered dead binder\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005285 break;
5286 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005287 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005288 if (!print_all && m->count == header_pos)
5289 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005290}
5291
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005292static const char * const binder_return_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005293 "BR_ERROR",
5294 "BR_OK",
5295 "BR_TRANSACTION",
5296 "BR_REPLY",
5297 "BR_ACQUIRE_RESULT",
5298 "BR_DEAD_REPLY",
5299 "BR_TRANSACTION_COMPLETE",
5300 "BR_INCREFS",
5301 "BR_ACQUIRE",
5302 "BR_RELEASE",
5303 "BR_DECREFS",
5304 "BR_ATTEMPT_ACQUIRE",
5305 "BR_NOOP",
5306 "BR_SPAWN_LOOPER",
5307 "BR_FINISHED",
5308 "BR_DEAD_BINDER",
5309 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
5310 "BR_FAILED_REPLY"
5311};
5312
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005313static const char * const binder_command_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005314 "BC_TRANSACTION",
5315 "BC_REPLY",
5316 "BC_ACQUIRE_RESULT",
5317 "BC_FREE_BUFFER",
5318 "BC_INCREFS",
5319 "BC_ACQUIRE",
5320 "BC_RELEASE",
5321 "BC_DECREFS",
5322 "BC_INCREFS_DONE",
5323 "BC_ACQUIRE_DONE",
5324 "BC_ATTEMPT_ACQUIRE",
5325 "BC_REGISTER_LOOPER",
5326 "BC_ENTER_LOOPER",
5327 "BC_EXIT_LOOPER",
5328 "BC_REQUEST_DEATH_NOTIFICATION",
5329 "BC_CLEAR_DEATH_NOTIFICATION",
Martijn Coenen5a6da532016-09-30 14:10:07 +02005330 "BC_DEAD_BINDER_DONE",
5331 "BC_TRANSACTION_SG",
5332 "BC_REPLY_SG",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005333};
5334
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005335static const char * const binder_objstat_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005336 "proc",
5337 "thread",
5338 "node",
5339 "ref",
5340 "death",
5341 "transaction",
5342 "transaction_complete"
5343};
5344
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005345static void print_binder_stats(struct seq_file *m, const char *prefix,
5346 struct binder_stats *stats)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005347{
5348 int i;
5349
5350 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005351 ARRAY_SIZE(binder_command_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005352 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005353 int temp = atomic_read(&stats->bc[i]);
5354
5355 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005356 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005357 binder_command_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005358 }
5359
5360 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005361 ARRAY_SIZE(binder_return_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005362 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005363 int temp = atomic_read(&stats->br[i]);
5364
5365 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005366 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005367 binder_return_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005368 }
5369
5370 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005371 ARRAY_SIZE(binder_objstat_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005372 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005373 ARRAY_SIZE(stats->obj_deleted));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005374 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005375 int created = atomic_read(&stats->obj_created[i]);
5376 int deleted = atomic_read(&stats->obj_deleted[i]);
5377
5378 if (created || deleted)
5379 seq_printf(m, "%s%s: active %d total %d\n",
5380 prefix,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005381 binder_objstat_strings[i],
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005382 created - deleted,
5383 created);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005384 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005385}
5386
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005387static void print_binder_proc_stats(struct seq_file *m,
5388 struct binder_proc *proc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005389{
5390 struct binder_work *w;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005391 struct binder_thread *thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005392 struct rb_node *n;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005393 int count, strong, weak, ready_threads;
Todd Kjosb4827902017-05-25 15:52:17 -07005394 size_t free_async_space =
5395 binder_alloc_get_free_async_space(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005396
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005397 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005398 seq_printf(m, "context %s\n", proc->context->name);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005399 count = 0;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005400 ready_threads = 0;
Todd Kjosb4827902017-05-25 15:52:17 -07005401 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005402 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
5403 count++;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005404
5405 list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node)
5406 ready_threads++;
5407
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005408 seq_printf(m, " threads: %d\n", count);
5409 seq_printf(m, " requested threads: %d+%d/%d\n"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005410 " ready threads %d\n"
5411 " free async space %zd\n", proc->requested_threads,
5412 proc->requested_threads_started, proc->max_threads,
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005413 ready_threads,
Todd Kjosb4827902017-05-25 15:52:17 -07005414 free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005415 count = 0;
5416 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
5417 count++;
Todd Kjos425d23f2017-06-12 12:07:26 -07005418 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005419 seq_printf(m, " nodes: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005420 count = 0;
5421 strong = 0;
5422 weak = 0;
Todd Kjos5346bf32016-10-20 16:43:34 -07005423 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005424 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
5425 struct binder_ref *ref = rb_entry(n, struct binder_ref,
5426 rb_node_desc);
5427 count++;
Todd Kjosb0117bb2017-05-08 09:16:27 -07005428 strong += ref->data.strong;
5429 weak += ref->data.weak;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005430 }
Todd Kjos5346bf32016-10-20 16:43:34 -07005431 binder_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005432 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005433
Todd Kjosd325d372016-10-10 10:40:53 -07005434 count = binder_alloc_get_allocated_count(&proc->alloc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005435 seq_printf(m, " buffers: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005436
5437 count = 0;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005438 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005439 list_for_each_entry(w, &proc->todo, entry) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005440 if (w->type == BINDER_WORK_TRANSACTION)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005441 count++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005442 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005443 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005444 seq_printf(m, " pending transactions: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005445
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005446 print_binder_stats(m, " ", &proc->stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005447}
5448
5449
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005450static int binder_state_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005451{
5452 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005453 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005454 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005455
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005456 seq_puts(m, "binder state:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005457
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005458 spin_lock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005459 if (!hlist_empty(&binder_dead_nodes))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005460 seq_puts(m, "dead nodes:\n");
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005461 hlist_for_each_entry(node, &binder_dead_nodes, dead_node) {
5462 /*
5463 * take a temporary reference on the node so it
5464 * survives and isn't removed from the list
5465 * while we print it.
5466 */
5467 node->tmp_refs++;
5468 spin_unlock(&binder_dead_nodes_lock);
5469 if (last_node)
5470 binder_put_node(last_node);
5471 binder_node_lock(node);
Todd Kjos425d23f2017-06-12 12:07:26 -07005472 print_binder_node_nilocked(m, node);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005473 binder_node_unlock(node);
5474 last_node = node;
5475 spin_lock(&binder_dead_nodes_lock);
5476 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005477 spin_unlock(&binder_dead_nodes_lock);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005478 if (last_node)
5479 binder_put_node(last_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005480
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005481 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005482 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005483 print_binder_proc(m, proc, 1);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005484 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005485
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005486 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005487}
5488
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005489static int binder_stats_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005490{
5491 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005492
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005493 seq_puts(m, "binder stats:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005494
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005495 print_binder_stats(m, "", &binder_stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005496
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005497 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005498 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005499 print_binder_proc_stats(m, proc);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005500 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005501
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005502 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005503}
5504
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005505static int binder_transactions_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005506{
5507 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005508
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005509 seq_puts(m, "binder transactions:\n");
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005510 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005511 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005512 print_binder_proc(m, proc, 0);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005513 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005514
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005515 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005516}
5517
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005518static int binder_proc_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005519{
Riley Andrews83050a42016-02-09 21:05:33 -08005520 struct binder_proc *itr;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005521 int pid = (unsigned long)m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005522
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005523 mutex_lock(&binder_procs_lock);
Riley Andrews83050a42016-02-09 21:05:33 -08005524 hlist_for_each_entry(itr, &binder_procs, proc_node) {
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005525 if (itr->pid == pid) {
5526 seq_puts(m, "binder proc state:\n");
5527 print_binder_proc(m, itr, 1);
Riley Andrews83050a42016-02-09 21:05:33 -08005528 }
5529 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005530 mutex_unlock(&binder_procs_lock);
5531
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005532 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005533}
5534
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005535static void print_binder_transaction_log_entry(struct seq_file *m,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005536 struct binder_transaction_log_entry *e)
5537{
Todd Kjos1cfe6272017-05-24 13:33:28 -07005538 int debug_id = READ_ONCE(e->debug_id_done);
5539 /*
5540 * read barrier to guarantee debug_id_done read before
5541 * we print the log values
5542 */
5543 smp_rmb();
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005544 seq_printf(m,
Todd Kjos1cfe6272017-05-24 13:33:28 -07005545 "%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 -07005546 e->debug_id, (e->call_type == 2) ? "reply" :
5547 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005548 e->from_thread, e->to_proc, e->to_thread, e->context_name,
Todd Kjose598d172017-03-22 17:19:52 -07005549 e->to_node, e->target_handle, e->data_size, e->offsets_size,
5550 e->return_error, e->return_error_param,
5551 e->return_error_line);
Todd Kjos1cfe6272017-05-24 13:33:28 -07005552 /*
5553 * read-barrier to guarantee read of debug_id_done after
5554 * done printing the fields of the entry
5555 */
5556 smp_rmb();
5557 seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ?
5558 "\n" : " (incomplete)\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005559}
5560
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005561static int binder_transaction_log_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005562{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005563 struct binder_transaction_log *log = m->private;
Todd Kjos1cfe6272017-05-24 13:33:28 -07005564 unsigned int log_cur = atomic_read(&log->cur);
5565 unsigned int count;
5566 unsigned int cur;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005567 int i;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005568
Todd Kjos1cfe6272017-05-24 13:33:28 -07005569 count = log_cur + 1;
5570 cur = count < ARRAY_SIZE(log->entry) && !log->full ?
5571 0 : count % ARRAY_SIZE(log->entry);
5572 if (count > ARRAY_SIZE(log->entry) || log->full)
5573 count = ARRAY_SIZE(log->entry);
5574 for (i = 0; i < count; i++) {
5575 unsigned int index = cur++ % ARRAY_SIZE(log->entry);
5576
5577 print_binder_transaction_log_entry(m, &log->entry[index]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005578 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005579 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005580}
5581
5582static const struct file_operations binder_fops = {
5583 .owner = THIS_MODULE,
5584 .poll = binder_poll,
5585 .unlocked_ioctl = binder_ioctl,
Arve Hjønnevågda498892014-02-21 14:40:26 -08005586 .compat_ioctl = binder_ioctl,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005587 .mmap = binder_mmap,
5588 .open = binder_open,
5589 .flush = binder_flush,
5590 .release = binder_release,
5591};
5592
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005593BINDER_DEBUG_ENTRY(state);
5594BINDER_DEBUG_ENTRY(stats);
5595BINDER_DEBUG_ENTRY(transactions);
5596BINDER_DEBUG_ENTRY(transaction_log);
5597
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005598static int __init init_binder_device(const char *name)
5599{
5600 int ret;
5601 struct binder_device *binder_device;
5602
5603 binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
5604 if (!binder_device)
5605 return -ENOMEM;
5606
5607 binder_device->miscdev.fops = &binder_fops;
5608 binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
5609 binder_device->miscdev.name = name;
5610
5611 binder_device->context.binder_context_mgr_uid = INVALID_UID;
5612 binder_device->context.name = name;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005613 mutex_init(&binder_device->context.context_mgr_node_lock);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005614
5615 ret = misc_register(&binder_device->miscdev);
5616 if (ret < 0) {
5617 kfree(binder_device);
5618 return ret;
5619 }
5620
5621 hlist_add_head(&binder_device->hlist, &binder_devices);
5622
5623 return ret;
5624}
5625
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005626static int __init binder_init(void)
5627{
5628 int ret;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005629 char *device_name, *device_names;
5630 struct binder_device *device;
5631 struct hlist_node *tmp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005632
Todd Kjos1cfe6272017-05-24 13:33:28 -07005633 atomic_set(&binder_transaction_log.cur, ~0U);
5634 atomic_set(&binder_transaction_log_failed.cur, ~0U);
5635
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005636 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
5637 if (binder_debugfs_dir_entry_root)
5638 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
5639 binder_debugfs_dir_entry_root);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005640
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005641 if (binder_debugfs_dir_entry_root) {
5642 debugfs_create_file("state",
5643 S_IRUGO,
5644 binder_debugfs_dir_entry_root,
5645 NULL,
5646 &binder_state_fops);
5647 debugfs_create_file("stats",
5648 S_IRUGO,
5649 binder_debugfs_dir_entry_root,
5650 NULL,
5651 &binder_stats_fops);
5652 debugfs_create_file("transactions",
5653 S_IRUGO,
5654 binder_debugfs_dir_entry_root,
5655 NULL,
5656 &binder_transactions_fops);
5657 debugfs_create_file("transaction_log",
5658 S_IRUGO,
5659 binder_debugfs_dir_entry_root,
5660 &binder_transaction_log,
5661 &binder_transaction_log_fops);
5662 debugfs_create_file("failed_transaction_log",
5663 S_IRUGO,
5664 binder_debugfs_dir_entry_root,
5665 &binder_transaction_log_failed,
5666 &binder_transaction_log_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005667 }
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005668
5669 /*
5670 * Copy the module_parameter string, because we don't want to
5671 * tokenize it in-place.
5672 */
5673 device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL);
5674 if (!device_names) {
5675 ret = -ENOMEM;
5676 goto err_alloc_device_names_failed;
5677 }
5678 strcpy(device_names, binder_devices_param);
5679
5680 while ((device_name = strsep(&device_names, ","))) {
5681 ret = init_binder_device(device_name);
5682 if (ret)
5683 goto err_init_binder_device_failed;
5684 }
5685
5686 return ret;
5687
5688err_init_binder_device_failed:
5689 hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
5690 misc_deregister(&device->miscdev);
5691 hlist_del(&device->hlist);
5692 kfree(device);
5693 }
5694err_alloc_device_names_failed:
5695 debugfs_remove_recursive(binder_debugfs_dir_entry_root);
5696
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005697 return ret;
5698}
5699
5700device_initcall(binder_init);
5701
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07005702#define CREATE_TRACE_POINTS
5703#include "binder_trace.h"
5704
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005705MODULE_LICENSE("GPL v2");